diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index 292dceccbe..51264e295e 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -1,2 +1,6 @@ + + Changes proposed in this pull request: - diff --git a/CHANGELOG.md b/CHANGELOG.md index aaac2222cf..015ab34043 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,265 @@ # SQLCipher Change Log -All notable changes to this project will be documented in this file. +Notable changes to this project are documented in this file. -## [Unreleased][unreleased] +## [4.13.0] - (January 2026 - [4.13.0 changes]) +- Updates baseline to SQLite 3.51.2 +- Corrects encoding for `sqlcipher_export()` function registration -## [3.4.0] - 2016-04-05 +## [4.12.0] - (December 2025 - [4.12.0 changes]) +- Updates baseline to SQLite 3.51.1 +- Adds `PRAGMA cipher_status` so applications can verify a database handle is using encryption +- Improves guards against key/rekey/attach misuse +- Adds criteria for `PRAGMA cipher_migrate` tests +- Fixes check for `__has_feature` macro to separate it from use +- Fixes CHANGELOG.md markdown formatting, typos, and inline code snippets +- Fixes conditional in SQLCipher pragma handling +- Removes deprecated providers for LibTomCrypt and NSS +- Removes unnecessary shutdown and URI config changes in core tests +- Ensures all test suite database handles are closed before delete + +## [4.11.0] - (October 2025 - [4.11.0 changes]) +- Converts log output to UTF-16 when writing to stdout or stderr on Windows +- Fixes scope issues to allow `--disable-amalgamation` to work properly +- Replaces fortuna seeding mechanism for libtomcrypt with `rng_get_bytes()` +- Removes CocoaPods support (`SQLCipher.podspec.json`) +- Fixes includes and macros to support non-amalgamated builds +- Fixes check for `__has_feature` to resolve issue with compilers that don't support it +- Corrects return value from `sqlcipher_fprintf` +- Fixes use of provider `free_ctx` +- Fixes some compiler warnings + +## [4.10.0] - (August 2025 - [4.10.0 changes]) +- Updates baseline to SQLite 3.50.4 +- Allows compile time override of default log level via `SQLCIPHER_LOG_LEVEL_DEFAULT` macro +- Fixes issue building with `-fsanitize=address` on macOS +- Fixes detection of CommonCrypto version on macOS +- Improves CommonCrypto version detection on iOS + +## [4.9.0] - (May 2025 - [4.9.0 changes]) +- Updates baseline to upstream SQLite 3.49.2 +- Removes use of static mutex in `sqlcipher_extra_shutdown()` + +## [4.8.0] - (April 2025 - [4.8.0 changes]) +- Fixes regression in `PRAGMA cipher_migrate` where an error would be thrown when migrating a current-version database +- Adds selective locking in critical sections of the library for shared cache connections (Note: use of shared cache is still strongly discouraged) +- Standardizes initial private heap size to 48KB to ensure `mlock` under constrained limits +- Removes changes to windows working set sizes +- Improvements to logging of memory stats and other cleanup + +## [4.7.0] - (March 2025 - [4.7.0 changes]) +- Updates baseline to upstream SQLite 3.49.1, including complete upstream SQLite refactoring of build system to use autosetup +- Significantly refactors and optimizes library initialization and cleanup +- Allocates majority of requisite memory at startup to improve memory locking on constrained platforms (i.e. Android and Windows) and reduce fragmentation +- Expands `sqlcipher_provider` interface to include `init` and `shutdown` functions +- Adds support for `.recover` shell command on corrupt databases with a full plaintext first page +- Performs fast random overwrite of freed memory segments for improved security +- Adds basic obfuscation of context key material for improved security +- Generates keyspecs dynamically on demand instead of storing them +- Expands keyspec/raw key format to accept key, HMAC key, and salt +- Improves error handling in `sqlcipher_export()` and `PRAGMA cipher_migrate` +- Allows setting custom compile-time default cryptographic provider via the `SQLCIPHER_CRYPTO_CUSTOM` macro +- Removes support for end-of-life OpenSSL versions older than 3.0 +- __BREAKING CHANGE__: `SELECT` statements (now also including schema independent queries like `SELECT 1`) cannot be executed on encrypted databases prior to setting the database key (behavior inherited from upstream SQLite) +- __BREAKING CHANGE__: Renames `configure` flag `--enable-tempstore=yes` to `--with-tempstore=yes` for alignment with SQLite (change required for upstream SQLite autosetup) +- __BREAKING CHANGE__: Renames default executable and library build outputs from `sqlcipher` and `libsqlcipher` to `sqlite3` and `libsqlite3` (for alignment with SQLite) +- __BREAKING CHANGE__: Removes `configure` flag `--with-crypto-lib` (replace with appropriate `-DSQLCIPHER_CRYPTO_*` CFLAG) +- __BREAKING CHANGE__: Requires defining `SQLITE_EXTRA_INIT=sqlcipher_extra_init` and `SQLITE_EXTRA_SHUTDOWN=sqlcipher_extra_shutdown` at compile time for optimized library initialization and cleanup +- __BREAKING CHANGE__: Enforces thread safe mode (i.e. `SQLITE_THREADSAFE` of 1 or 2) and temporary storage (i.e. `SQLITE_TEMP_STORE` of 2 or 3) settings at compile time + +## [4.6.1] - (August 2024 - [4.6.1 changes]) +- Updates baseline to upstream SQLite 3.46.1 +- Significant refactor to merge `crypto.h`, `crypto.c`, and `crypto_impl.c` into a single `sqlcipher.c` source file for simplicity. +- Updates minimum working set size on windows to increase lockable pages +- Adds new `PRAGMA cipher_log_source` for filtering log output on higher verbosity levels +- Improves log output by including the log level and source prior to message +- Improves error logging in `PRAGMA cipher_migrate` +- Fixes issue where log level and target would be overwritten if set prior to initialization +- Corrects Podspec license element to use specific BSD 3 Clause +- Fixes default log output to console for macOS + +## [4.6.0] - (May 2024 - [4.6.0 changes]) +- Sets default log level to WARN +- Sends default log output to: logcat for Android; Console for iOS and macOS; and stderr for all other platforms +- General improvements to log level assignments, output, and sanitization +- Fixes Apple Privacy Manifest by removing empty NSPrivacyCollectedDataType from PrivacyInfo.xcprivacy +- Moves Swift support defines for podspec user_target_xcconfig so they only apply to the consuming project + +## [4.5.7] - (April 2024 - [4.5.7 changes]) +- Updates baseline to upstream SQLite 3.45.3 +- Adds "device" logging and profile target using `os_log` for Apple (and logcat on Android) +- Fixes issues compiling with `SQLITE_OMIT_LOG` +- Fixes malformed man page caused by old merge conflict +- Updates podspec for current Xcode versions, improved Swift support, and Privacy Manifest + +## [4.5.6] - (January 2024 - [4.5.6 changes]) +- Updates baseline to upstream SQLite 3.44.2 +- Improves `PRAGMA cipher_integrity_check` to report expected page size if invalid +- Implements `PRAGMA page_size` compatibility with `PRAGMA cipher_page_size` so both will operate properly on encrypted databases +- Updates `LICENSE.md` with SQLCipher license to avoid ambiguity and remove redundancy + +## [4.5.5] - (August 2023 - [4.5.5 changes]) +- Updates baseline to upstream SQLite 3.42.0 +- Do not allow key to be changed on a connection after it has been successfully used for an encryption or decryption operation to prevent accidental database corruption +- Raise an error if a rekey operation is attempted on an unencrypted database +- Raise an error when a key or rekey operation is passed an empty key +- Minor improvements to constant time functions +- Miscellaneous code and comment cleanup + +## [4.5.4] - (April 2023 - [4.5.4 changes]) +- Updates baseline to upstream SQLite 3.41.2 +- Updates minimum Apple SDK versions in podspec for new Xcode compatibility +- Return runtime OpenSSL version from `PRAGMA cipher_provider_version` (instead of hardcoded value) +- Adds guard against zero block size and crash if cryptographic provider initialization fails +- When an ATTACH occurs creating a new encrypted database as the first operation after keying the main database, the new database will have the same salt value. + +## [4.5.3] - (December 2022 - [4.5.3 changes]) +- Updates baseline to upstream SQLite 3.39.4 + +## [4.5.2] - (August 2022 - [4.5.2 changes]) +- Updates source code baseline to upstream SQLite 3.39.2 +- Simplifies OpenSSL version conditional code +- Fixes issue where `PRAGMA cipher_memory_security` could report OFF when it was actually ON +- Fixes unfreed OpenSSL allocation when compiled against version 3 +- Fixes support for building against recent versions of BoringSSL + +## [4.5.1] - (March 2022 - [4.5.1 changes]) +- Updates source code baseline to upstream SQLite 3.37.2 +- Adds `PRAGMA cipher_log` and `PRAGMA cipher_log_level` features to allow logging of TRACE, DEBUG, INFO, WARN, and ERROR messages to stdout, stderr, file, or logcat +- Modifies `PRAGMA cipher_profile` to use `sqlite3_trace_v2` and adds logcat target for Android +- Updates OpenSSL provider to use `EVP_MAC` API with version 3+ +- Adds new `PRAGMA cipher_test_on`, `PRAGMA cipher_test_off`, and `PRAGMA cipher_test_rand` (available when compiled with `-DSQLCIPHER_TEST`) to facilitate simulation of error conditions +- Fixes `PRAGMA cipher_integrity_check` to work properly with databases larger that 2GB +- Fixes missing `munlock` before free for context internal buffer (thanks to Fedor Indutny) + +## [4.5.0] - (October 2021 - [4.5.0 changes]) +- Updates baseline to upstream SQLite 3.36.0 +- Changes the enhanced memory security feature to be DISABLED by default; once enabled by `PRAGMA cipher_memory_security = ON`, it can't be turned off for the lifetime of the process +- Changes `PRAGMA cipher_migrate` to permanently enter an error state if a migration fails +- Fixes memory locking/unlocking issue with realloc implementation on hardened runtimes when memory security is enabled +- Fixes `PRAGMA cipher_migrate` to clean up the temporary database if a migration fails +- Removes logging of non-string pointers when compiling with trace level logging + +## [4.4.3] - (February 2021 - [4.4.3 changes]) +- Updates baseline to upstream SQLite 3.34.1 +- Fixes `sqlcipher_export` handling of NULL parameters +- Removes randomization of rekey-delete tests to avoid false test failures +- Changes internal usage of `sqlite_master` to `sqlite_schema` +- Omits unused profiling function under certain defines to avoid compiler warnings + +## [4.4.2] - (November 2020 - [4.4.2 changes]) +- Improves error handling to resolve potential corruption if an encryption operation failed while operating in WAL mode +- Changes to OpenSSL library cryptographic provider to reduce initialization complexity +- Adjust `cipher_integrity_check` to skip locking page to avoid a spurious error report for very large databases +- Miscellaneous code and comment cleanup + +## [4.4.1] - (October 2020 - [4.4.1 changes]) +- Updates baseline to upstream SQLite 3.33.0 +- Fixes double-free bug in `cipher_default_plaintext_header_size` +- Changes SQLCipher tests to use suite runner +- Improvement to `cipher_integrity_check` tests to minimize false negatives +- Deprecates `PRAGMA cipher_store_pass` + +## [4.4.0] - (May 2020 - [4.4.0 changes]) +- Updates baseline to upstream SQLite 3.31.0 +- Adjusts shell to report SQLCipher version alongside SQLite version +- Fixes various build warnings under several compilers +- Removes unused id and status functions from provider interface + +## [4.3.0] - (November 2019 - [4.3.0 changes]) +- Updates baseline to upstream SQLite 3.30.1 +- `PRAGMA key` now returns text result value "ok" after execution +- Adjusts backup API so that encrypted to encrypted backups are permitted +- Adds NSS crypto provider implementation +- Fixes OpenSSL provider compatibility with BoringSSL +- Separates memory related traces to reduce verbosity of logging +- Fixes output of `PRAGMA cipher_integrity_check` on big endian platforms +- Cryptographic provider interface cleanup +- Rework of mutex allocation and management +- Resolves miscellaneous build warnings +- Force error state at database pager level if SQLCipher initialization fails + +## [4.2.0] - (May 2019 - [4.2.0 changes]) +- Adds `PRAGMA cipher_integrity_check` to perform independent verification of page HMACs +- Updates baseline to upstream SQLite 3.28.0 +- Improves `PRAGMA cipher_migrate` to handle keys containing non-terminating zero bytes + +## [4.1.0] - (March 2019 - [4.1.0 changes]) +- Defer reading salt from header until key derivation is triggered +- Clarify usage of `sqlite3_rekey` for plaintext databases in header +- Normalize attach behavior when key is not yet derived +- Adds `PRAGMA cipher_settings` to query current database codec settings +- Adds `PRAGMA cipher_default_settings` to query current default SQLCipher options +- `PRAGMA cipher_hmac_pgno` is now deprecated +- `PRAGMA cipher_hmac_salt_mask` is now deprecated +- `PRAGMA fast_kdf_iter` is now deprecated +- Improve `sqlcipher_export` routine and restore all database flags +- Clear codec data buffers if a cryptographic provider operation fails +- Disable backup API for encrypted databases (this was previously documented as not-working and non-supported, but will now explicitly error out on initialization) +- Updates baseline to upstream SQLite 3.27.2 + +## [4.0.1] - (December 2018 - [4.0.1 changes]) +- Based on upstream SQLite 3.26.0 (addresses SQLite “Magellan” issue) +- Adds `PRAGMA cipher_compatibility` and `PRAGMA cipher_default_compatibility` which automatically configure appropriate compatibility settings for the specified SQLCipher major version number +- Filters attach statements with `KEY` parameters from readline history +- Fixes crash in command line shell with empty input (i.e. ^D) +- Fixes warnings when compiled with strict-prototypes + +## [4.0.0] - (November 2018 - [4.0.0 changes]) +### Changed +- Default page size for databases increased to 4096 bytes (up from 1024) * +- Default PBKDF2 iterations increased to 256,000 (up from 64,000) * +- Default KDF algorithm is now PBKDF2-HMAC-SHA512 (from PBKDF2-HMAC-SHA1) * +- Default HMAC algorithm is now HMAC-SHA512 (from HMAC-SHA1) * +- `PRAGMA cipher` is now disabled and no longer supported (after multi-year deprecation) * +- `PRAGMA rekey_cipher` is now disabled and no longer supported * +- `PRAGMA rekey_kdf_iter` is now disabled and no longer supported * +- By default all memory allocated internally by SQLite is wiped before it is freed +- `PRAGMA cipher_memory_security`: allows full memory wiping to be disabled for performance when the feature is not required +- `PRAGMA cipher_kdf_algorithm`, `PRAGMA cipher_default_kdf_algorithm` to control KDF algorithm selection between PBKDF2-HMAC-SHA1, PBKDF2-HMAC-SHA256 and PBKDF2-HMAC-SHA512 +- `PRAGMA cipher_hmac_algorithm`, `PRAGMA cipher_default_hmac_algorithm` to control HMAC algorithm selection between HMAC-SHA1, HMAC-SHA256 and PBKDF2-HMAC-SHA512 +- Based on upstream SQLite 3.25.2 +- When compiled with readline support, `PRAGMA key` and `PRAGMA rekey` lines will no longer be + saved to history +- Adds second optional parameter to `sqlcipher_export` to specify source database to + support bidirectional exports +- Fixes compatibility with LibreSSL 2.7.0+ +- Fixes compatibility with OpenSSL 1.1.x +- Simplified and improved performance for `PRAGMA cipher_migrate` when migrating older database versions +- Refactoring of SQLCipher tests into separate files by test type +- `PRAGMA cipher_plaintext_header_size` and `PRAGMA cipher_default_plaintext_header_size`: allocates a portion of the database header which will not be encrypted to allow identification as a SQLite database +- `PRAGMA cipher_salt`: retrieve or set the salt value for the database +- Adds Podspec for using tagged versions of SQLCipher +- Define `SQLCIPHER_PROFILE_USE_FOPEN` for WinXP support +- Improved error handling for cryptographic providers +- Improved memory handling for `PRAGMA` commands that return values +- Improved version reporting to assist with identification of distribution +- Major rewrite and simplification of internal codec and pager extension +- Fixes compilation with `--disable-amalgamation` +- Removes `sqlcipher.xcodeproj` build support + +## [3.4.2] - (December 2017 - [3.4.2 changes]) +### Added +- Added support for building with LibreSSL + +### Changed +- Merge upstream SQLite 3.20.1 +- Text strings for `SQLITE_ERROR` and `SQLITE_NOTADB` changed to match upstream SQLite +- Remove static modifier for codec password functions +- Page alignment for `mlock` +- Fix segfault in `sqlcipher_cipher_ctx_cmp` during rekey operation +- Fix `sqlcipher_export` and `PRAGMA cipher_migrate` when tracing API in use +- Validate codec page size when setting +- Guard OpenSSL initialization and cleanup routines +- Allow additional linker options to be passed via command line for Windows platforms + +## [3.4.1] - (December 2016 - [3.4.1 changes]) +### Added +- Added support for OpenSSL 1.1.0 + +### Changed +- Merged upstream SQLite 3.15.2 + +## [3.4.0] - (April 2016 - [3.4.0 changes]) ### Added - Added `PRAGMA cipher_provider_version` @@ -13,14 +269,14 @@ All notable changes to this project will be documented in this file. ### Deprecated - Deprecated `PRAGMA cipher` command -## [3.3.1] - 2015-07-13 +## [3.3.1] - (July 2015 - [3.3.1 changes]) ### Changed - Merge upstream SQLite 3.8.10.2 - Fixed segfault when provided an invalid cipher name - Check for codec context when performing `PRAGMA cipher_store_pass` - Remove extraneous null check in `PRAGMA cipher_migrate` -## [3.3.0] - 2015-03-25 +## [3.3.0] - (March 2015 - [3.3.0 changes]) ### Added - Added FIPS API calls within the OpenSSL crypto provider - `PRAGMA cipher_default_page_size` - support for attaching non-default page sizes @@ -28,22 +284,22 @@ All notable changes to this project will be documented in this file. ### Changed - Merged upstream SQLite 3.8.8.3 -## [3.2.0] - 2014-09-30 +## [3.2.0] - (September 2014 - [3.2.0 changes]) ### Added - Added `PRAGMA cipher_store_pass` ### Changed - Merged upstream SQLite 3.8.6 -- Renmed README to README.md +- Renamed `README` to `README.md` -## [3.1.0] - 2014-04-23 +## [3.1.0] - (April 2014 - [3.1.0 changes]) ### Added - Added `PRAGMA cipher_profile` ### Changed - Merged upstream SQLite 3.8.4.3 -## [3.0.1] - 2013-12-06 +## [3.0.1] - (December 2013 - [3.0.1 changes]) ### Added - Added `PRAGMA cipher_add_random` to source external entropy @@ -51,13 +307,13 @@ All notable changes to this project will be documented in this file. - Fix `PRAGMA cipher_migrate` to handle passphrases longer than 64 characters & raw keys - Improvements to the libtomcrypt provider -## [3.0.0] - 2013-11-05 +## [3.0.0] - (November 2013 - [3.0.0 changes]) ### Added - Added `PRAGMA cipher_migrate` to migrate older database file formats ### Changed - Merged upstream SQLite 3.8.0.2 -- Remove usage of VirtualLock/Unlock on WinRT and Windows Phone +- Remove usage of `VirtualLock/Unlock` on WinRT and Windows Phone - Ignore HMAC read during Btree file copy - Fix lib naming for pkg-config - Use _v2 version of `sqlite3_key` and `sqlite3_rekey` @@ -66,29 +322,109 @@ All notable changes to this project will be documented in this file. ### Security - Change KDF iteration length from 4,000 to 64,000 -[unreleased]: https://github.com/sqlcipher/sqlcipher/compare/v3.4.0...prerelease -[3.4.0]: https://github.com/sqlcipher/sqlcipher/compare/v3.3.1...v3.4.0 -[3.3.1]: https://github.com/sqlcipher/sqlcipher/compare/v3.3.0...v3.3.1 -[3.3.0]: https://github.com/sqlcipher/sqlcipher/compare/v3.2.0...v3.3.0 -[3.2.0]: https://github.com/sqlcipher/sqlcipher/compare/v3.1.0...v3.2.0 -[3.1.0]: https://github.com/sqlcipher/sqlcipher/compare/v3.0.1...v3.1.0 -[3.0.1]: https://github.com/sqlcipher/sqlcipher/compare/v3.0.0...v3.0.1 -[3.0.0]: https://github.com/sqlcipher/sqlcipher/compare/v2.2.0...v3.0.0 -[2.2.0]: https://github.com/sqlcipher/sqlcipher/compare/v2.1.1...v2.2.0 -[2.1.1]: https://github.com/sqlcipher/sqlcipher/compare/v2.1.0...v2.1.1 -[2.1.0]: https://github.com/sqlcipher/sqlcipher/compare/v2.0.6...v2.1.0 -[2.0.6]: https://github.com/sqlcipher/sqlcipher/compare/v2.0.5...v2.0.6 -[2.0.5]: https://github.com/sqlcipher/sqlcipher/compare/v2.0.3...v2.0.5 -[2.0.3]: https://github.com/sqlcipher/sqlcipher/compare/v2.0.0...v2.0.3 -[2.0.0]: https://github.com/sqlcipher/sqlcipher/compare/v1.1.10...v2.0.0 -[1.1.10]: https://github.com/sqlcipher/sqlcipher/compare/v1.1.9...v1.1.10 -[1.1.9]: https://github.com/sqlcipher/sqlcipher/compare/v1.1.8...v1.1.9 -[1.1.8]: https://github.com/sqlcipher/sqlcipher/compare/v1.1.7...v1.1.8 -[1.1.7]: https://github.com/sqlcipher/sqlcipher/compare/v1.1.6...v1.1.7 -[1.1.6]: https://github.com/sqlcipher/sqlcipher/compare/v1.1.5...v1.1.6 -[1.1.5]: https://github.com/sqlcipher/sqlcipher/compare/v1.1.4...v1.1.5 -[1.1.4]: https://github.com/sqlcipher/sqlcipher/compare/v1.1.3...v1.1.4 -[1.1.3]: https://github.com/sqlcipher/sqlcipher/compare/v1.1.2...v1.1.3 -[1.1.2]: https://github.com/sqlcipher/sqlcipher/compare/v1.1.1...v1.1.1 -[1.1.1]: https://github.com/sqlcipher/sqlcipher/compare/v1.1.0...v1.1.1 -[1.1.0]: https://github.com/sqlcipher/sqlcipher/compare/617ed01...v1.1.0 +[4.13.0]: https://github.com/sqlcipher/sqlcipher/tree/v4.13.0 +[4.13.0 changes]: https://github.com/sqlcipher/sqlcipher/compare/v4.12.0...v4.13.0 +[4.12.0]: https://github.com/sqlcipher/sqlcipher/tree/v4.12.0 +[4.12.0 changes]: https://github.com/sqlcipher/sqlcipher/compare/v4.11.0...v4.12.0 +[4.11.0]: https://github.com/sqlcipher/sqlcipher/tree/v4.11.0 +[4.11.0 changes]: https://github.com/sqlcipher/sqlcipher/compare/v4.10.0...v4.11.0 +[4.10.0]: https://github.com/sqlcipher/sqlcipher/tree/v4.10.0 +[4.10.0 changes]: https://github.com/sqlcipher/sqlcipher/compare/v4.9.0...v4.10.0 +[4.9.0]: https://github.com/sqlcipher/sqlcipher/tree/v4.9.0 +[4.9.0 changes]: https://github.com/sqlcipher/sqlcipher/compare/v4.8.0...v4.9.0 +[4.8.0]: https://github.com/sqlcipher/sqlcipher/tree/v4.8.0 +[4.8.0 changes]: https://github.com/sqlcipher/sqlcipher/compare/v4.7.0...v4.8.0 +[4.7.0]: https://github.com/sqlcipher/sqlcipher/tree/v4.7.0 +[4.7.0 changes]: https://github.com/sqlcipher/sqlcipher/compare/v4.6.1...v4.7.0 +[4.6.1]: https://github.com/sqlcipher/sqlcipher/tree/v4.6.1 +[4.6.1 changes]: https://github.com/sqlcipher/sqlcipher/compare/v4.6.0...v4.6.1 +[4.6.0]: https://github.com/sqlcipher/sqlcipher/tree/v4.6.0 +[4.6.0 changes]: https://github.com/sqlcipher/sqlcipher/compare/v4.5.7...v4.6.0 +[4.5.7]: https://github.com/sqlcipher/sqlcipher/tree/v4.5.7 +[4.5.7 changes]: https://github.com/sqlcipher/sqlcipher/compare/v4.5.6...v4.5.7 +[4.5.6]: https://github.com/sqlcipher/sqlcipher/tree/v4.5.6 +[4.5.6 changes]: https://github.com/sqlcipher/sqlcipher/compare/v4.5.5...v4.5.6 +[4.5.5]: https://github.com/sqlcipher/sqlcipher/tree/v4.5.5 +[4.5.5 changes]: https://github.com/sqlcipher/sqlcipher/compare/v4.5.4...v4.5.5 +[4.5.4]: https://github.com/sqlcipher/sqlcipher/tree/v4.5.4 +[4.5.4 changes]: https://github.com/sqlcipher/sqlcipher/compare/v4.5.3...v4.5.4 +[4.5.3]: https://github.com/sqlcipher/sqlcipher/tree/v4.5.3 +[4.5.3 changes]: https://github.com/sqlcipher/sqlcipher/compare/v4.5.2...v4.5.3 +[4.5.2]: https://github.com/sqlcipher/sqlcipher/tree/v4.5.2 +[4.5.2 changes]: https://github.com/sqlcipher/sqlcipher/compare/v4.5.1...v4.5.2 +[4.5.1]: https://github.com/sqlcipher/sqlcipher/tree/v4.5.1 +[4.5.1 changes]: https://github.com/sqlcipher/sqlcipher/compare/v4.5.0...v4.5.1 +[4.5.0]: https://github.com/sqlcipher/sqlcipher/tree/v4.5.0 +[4.5.0 changes]: https://github.com/sqlcipher/sqlcipher/compare/v4.4.3...v4.5.0 +[4.4.3]: https://github.com/sqlcipher/sqlcipher/tree/v4.4.3 +[4.4.3 changes]: https://github.com/sqlcipher/sqlcipher/compare/v4.4.2...v4.4.3 +[4.4.2]: https://github.com/sqlcipher/sqlcipher/tree/v4.4.2 +[4.4.2 changes]: https://github.com/sqlcipher/sqlcipher/compare/v4.4.1...v4.4.2 +[4.4.1]: https://github.com/sqlcipher/sqlcipher/tree/v4.4.1 +[4.4.1 changes]: https://github.com/sqlcipher/sqlcipher/compare/v4.4.0...v4.4.1 +[4.4.0]: https://github.com/sqlcipher/sqlcipher/tree/v4.4.0 +[4.4.0 changes]: https://github.com/sqlcipher/sqlcipher/compare/v4.3.0...v4.4.0 +[4.3.0]: https://github.com/sqlcipher/sqlcipher/tree/v4.3.0 +[4.3.0 changes]: https://github.com/sqlcipher/sqlcipher/compare/v4.2.0...v4.3.0 +[4.2.0]: https://github.com/sqlcipher/sqlcipher/tree/v4.2.0 +[4.2.0 changes]: https://github.com/sqlcipher/sqlcipher/compare/v4.1.0...v4.2.0 +[4.1.0]: https://github.com/sqlcipher/sqlcipher/tree/v4.1.0 +[4.1.0 changes]: https://github.com/sqlcipher/sqlcipher/compare/v4.0.1...v4.1.0 +[4.0.1]: https://github.com/sqlcipher/sqlcipher/tree/v4.0.1 +[4.0.1 changes]: https://github.com/sqlcipher/sqlcipher/compare/v4.0.0...v4.0.1 +[4.0.0]: https://github.com/sqlcipher/sqlcipher/tree/v4.0.0 +[4.0.0 changes]: https://github.com/sqlcipher/sqlcipher/compare/v3.4.2...v4.0.0 +[3.4.2]: https://github.com/sqlcipher/sqlcipher/tree/v3.4.2 +[3.4.2 changes]: https://github.com/sqlcipher/sqlcipher/compare/v3.4.1...v3.4.2 +[3.4.1]: https://github.com/sqlcipher/sqlcipher/tree/v3.4.1 +[3.4.1 changes]: https://github.com/sqlcipher/sqlcipher/compare/v3.4.0...v3.4.1 +[3.4.0]: https://github.com/sqlcipher/sqlcipher/tree/v3.4.0 +[3.4.0 changes]: https://github.com/sqlcipher/sqlcipher/compare/v3.3.1...v3.4.0 +[3.3.1]: https://github.com/sqlcipher/sqlcipher/tree/v3.3.1 +[3.3.1 changes]: https://github.com/sqlcipher/sqlcipher/compare/v3.3.0...v3.3.1 +[3.3.0]: https://github.com/sqlcipher/sqlcipher/tree/v3.3.0 +[3.3.0 changes]: https://github.com/sqlcipher/sqlcipher/compare/v3.2.0...v3.3.0 +[3.2.0]: https://github.com/sqlcipher/sqlcipher/tree/v3.2.0 +[3.2.0 changes]: https://github.com/sqlcipher/sqlcipher/compare/v3.1.0...v3.2.0 +[3.1.0]: https://github.com/sqlcipher/sqlcipher/tree/v3.1.0 +[3.1.0 changes]: https://github.com/sqlcipher/sqlcipher/compare/v3.0.1...v3.1.0 +[3.0.1]: https://github.com/sqlcipher/sqlcipher/tree/v3.0.1 +[3.0.1 changes]: https://github.com/sqlcipher/sqlcipher/compare/v3.0.0...v3.0.1 +[3.0.0]: https://github.com/sqlcipher/sqlcipher/tree/v3.0.0 +[3.0.0 changes]: https://github.com/sqlcipher/sqlcipher/compare/v2.2.0...v3.0.0 +[2.2.0]: https://github.com/sqlcipher/sqlcipher/tree/v2.2.0 +[2.2.0 changes]: https://github.com/sqlcipher/sqlcipher/compare/v2.1.1...v2.2.0 +[2.1.1]: https://github.com/sqlcipher/sqlcipher/tree/v2.1.1 +[2.1.1 changes]: https://github.com/sqlcipher/sqlcipher/compare/v2.1.0...v2.1.1 +[2.1.0]: https://github.com/sqlcipher/sqlcipher/tree/v2.1.0 +[2.1.0 changes]: https://github.com/sqlcipher/sqlcipher/compare/v2.0.6...v2.1.0 +[2.0.6]: https://github.com/sqlcipher/sqlcipher/tree/v2.0.6 +[2.0.6 changes]: https://github.com/sqlcipher/sqlcipher/compare/v2.0.5...v2.0.6 +[2.0.5]: https://github.com/sqlcipher/sqlcipher/tree/v2.0.5 +[2.0.5 changes]: https://github.com/sqlcipher/sqlcipher/compare/v2.0.3...v2.0.5 +[2.0.3]: https://github.com/sqlcipher/sqlcipher/tree/v2.0.3 +[2.0.3 changes]: https://github.com/sqlcipher/sqlcipher/compare/v2.0.0...v2.0.3 +[2.0.0]: https://github.com/sqlcipher/sqlcipher/tree/v2.0.0 +[2.0.0 changes]: https://github.com/sqlcipher/sqlcipher/compare/v1.1.10...v2.0.0 +[1.1.10]: https://github.com/sqlcipher/sqlcipher/tree/v1.1.10 +[1.1.10 changes]: https://github.com/sqlcipher/sqlcipher/compare/v1.1.9...v1.1.10 +[1.1.9]: https://github.com/sqlcipher/sqlcipher/tree/v1.1.9 +[1.1.9 changes]: https://github.com/sqlcipher/sqlcipher/compare/v1.1.8...v1.1.9 +[1.1.8]: https://github.com/sqlcipher/sqlcipher/tree/v1.1.8 +[1.1.8 changes]: https://github.com/sqlcipher/sqlcipher/compare/v1.1.7...v1.1.8 +[1.1.7]: https://github.com/sqlcipher/sqlcipher/tree/v1.1.7 +[1.1.7 changes]: https://github.com/sqlcipher/sqlcipher/compare/v1.1.6...v1.1.7 +[1.1.6]: https://github.com/sqlcipher/sqlcipher/tree/v1.1.6 +[1.1.6 changes]: https://github.com/sqlcipher/sqlcipher/compare/v1.1.5...v1.1.6 +[1.1.5]: https://github.com/sqlcipher/sqlcipher/tree/v1.1.5 +[1.1.5 changes]: https://github.com/sqlcipher/sqlcipher/compare/v1.1.4...v1.1.5 +[1.1.4]: https://github.com/sqlcipher/sqlcipher/tree/v1.1.4 +[1.1.4 changes]: https://github.com/sqlcipher/sqlcipher/compare/v1.1.3...v1.1.4 +[1.1.3]: https://github.com/sqlcipher/sqlcipher/tree/v1.1.3 +[1.1.3 changes]: https://github.com/sqlcipher/sqlcipher/compare/v1.1.2...v1.1.3 +[1.1.2]: https://github.com/sqlcipher/sqlcipher/tree/v1.1.2 +[1.1.2 changes]: https://github.com/sqlcipher/sqlcipher/compare/v1.1.1...v1.1.1 +[1.1.1]: https://github.com/sqlcipher/sqlcipher/tree/v1.1.1 +[1.1.1 changes]: https://github.com/sqlcipher/sqlcipher/compare/v1.1.0...v1.1.1 +[1.1.0]: https://github.com/sqlcipher/sqlcipher/tree/v1.1.0 +[1.1.0 changes]: https://github.com/sqlcipher/sqlcipher/compare/617ed01...v1.1.0 diff --git a/LICENSE b/LICENSE.md similarity index 97% rename from LICENSE rename to LICENSE.md index bebe1e7eb1..3f71443161 100644 --- a/LICENSE +++ b/LICENSE.md @@ -1,4 +1,4 @@ -Copyright (c) 2008, ZETETIC LLC +Copyright (c) 2025, ZETETIC LLC All rights reserved. Redistribution and use in source and binary forms, with or without diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000000..3f71443161 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,24 @@ +Copyright (c) 2025, ZETETIC LLC +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of the ZETETIC LLC nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY +EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/Makefile.in b/Makefile.in index 31f039f475..a597a86a35 100644 --- a/Makefile.in +++ b/Makefile.in @@ -1,1292 +1,339 @@ -#!/usr/make +#!/usr/bin/make +# ^^^^ help out editors which guess this file's type. # # Makefile for SQLITE # -# This makefile is suppose to be configured automatically using the -# autoconf. But if that does not work for you, you can configure -# the makefile manually. Just set the parameters below to values that -# work well for your system. +# This makefile is intended to be configured automatically using the +# configure script. # -# If the configure script does not work out-of-the-box, you might -# be able to get it to work by giving it some hints. See the comment -# at the beginning of configure.in for additional information. +# The docs for many of its variables are in the primary static +# makefile, main.mk (which this one includes at runtime). # - -# The toplevel directory of the source tree. This is the directory -# that contains this "Makefile.in" and the "configure.in" script. +all: +######################################################################## +# Maintenance reminders: # -TOP = @abs_srcdir@ - - -# C Compiler and options for use in building executables that -# will run on the platform that is doing the build. +# - This makefile should remain as POSIX-make-compatible as possible: +# https://pubs.opengroup.org/onlinepubs/9799919799/utilities/make.html # -BCC = @BUILD_CC@ @BUILD_CFLAGS@ - -# TCC is the C Compile and options for use in building executables that -# will run on the target platform. (BCC and TCC are usually the -# same unless your are cross-compiling.) Separate CC and CFLAGS macros -# are provide so that these aspects of the build process can be changed -# on the "make" command-line. Ex: "make CC=clang CFLAGS=-fsanitize=undefined" +# - The naming convention of some vars, using periods instead of +# underscores, though unconventional, was selected for a couple of +# reasons: 1) Personal taste (for which there is no accounting). 2) +# It is thought to help defend against inadvertent injection of +# those vars via environment variables (because X.Y is not a legal +# environment variable name). "Feature or bug?" is debatable and +# this naming convention may be reverted if it causes any grief. # -CC = @CC@ -CFLAGS = @CPPFLAGS@ @CFLAGS@ -TCC = $(CC) $(CFLAGS) -I. -I${TOP}/src -I${TOP}/ext/rtree -I${TOP}/ext/fts3 - -# Define this for the autoconf-based build, so that the code knows it can -# include the generated config.h -# -TCC += -D_HAVE_SQLITE_CONFIG_H -DBUILD_sqlite -# Define -DNDEBUG to compile without debugging (i.e., for production usage) -# Omitting the define will cause extra debugging code to be inserted and -# includes extra comments when "EXPLAIN stmt" is used. # -TCC += @TARGET_DEBUG@ - -# Compiler options needed for programs that use the TCL library. +# The top-most directory of the source tree. This is the directory +# that contains this "Makefile.in" and the "configure" script. # -TCC += @TCL_INCLUDE_SPEC@ +TOP = @abs_top_srcdir@ -# The library that programs using TCL must link against. # -LIBTCL = @TCL_LIB_SPEC@ - -# Compiler options needed for programs that use the readline() library. +# Autotools-conventional vars which are used by package installation +# rules in main.mk. To get sane handling when a user overrides only +# a subset of these, we perform some acrobatics with these vars +# in the configure script: see [proj-remap-autoconf-dir-vars] for +# full details. # -READLINE_FLAGS = -DHAVE_READLINE=@TARGET_HAVE_READLINE@ @TARGET_READLINE_INC@ -READLINE_FLAGS += -DHAVE_EDITLINE=@TARGET_HAVE_EDITLINE@ - -# The library that programs using readline() must link against. +# For completeness's sake, the aforementioned conventional vars which +# are relevant to our installation rules are: # -LIBREADLINE = @TARGET_READLINE_LIBS@ - -# Should the database engine be compiled threadsafe +# datadir = $(prefix)/share +# mandir = $(datadir)/man +# includedir = $(prefix)/include +# exec_prefix = $(prefix) +# bindir = $(exec_prefix)/bin +# libdir = $(exec_prefix)/lib # -TCC += -DSQLITE_THREADSAFE=@SQLITE_THREADSAFE@ - -# Any target libraries which libsqlite must be linked against -# -TLIBS = @LIBS@ $(LIBS) +# Our builds do not require any of their relatives: +# +# sbindir = $(exec_prefix)/sbin +# sysconfdir = /etc +# sharedstatedir = $(prefix)/com +# localstatedir = /var +# runstatedir = /run +# infodir = $(datadir)/info +# libexecdir = $(exec_prefix)/libexec +# +prefix = @prefix@ +datadir = @datadir@ +mandir = @mandir@ +includedir = @includedir@ +exec_prefix = @exec_prefix@ +bindir = @bindir@ +libdir = @libdir@ -# Flags controlling use of the in memory btree implementation +INSTALL = @BIN_INSTALL@ +AR = @AR@ +AR.flags = cr +CC = @CC@ +B.cc = @CC_FOR_BUILD@ @BUILD_CFLAGS@ +T.cc = $(CC) +# +# $(CFLAGS) is problematic because it is frequently overridden when +# invoking make, which loses things like -fPIC. So... we avoid using +# it directly and instead add a level of indirection. We combine +# $(CFLAGS) and $(CPPFLAGS) here because that's the way the legacy +# build did it and many builds rely on that. See main.mk for more +# details. # -# SQLITE_TEMP_STORE is 0 to force temporary tables to be in a file, 1 to -# default to file, 2 to default to memory, and 3 to force temporary -# tables to always be in memory. +# Historical note: the pre-3.48 build only honored CPPFLAGS at +# configure-time, and expanded them into the generated Makefile. There +# are, in that build, no uses of CPPFLAGS in the configure-expanded +# Makefile. Ergo: if a client configures with CPPFLAGS=... and then +# explicitly passes CFLAGS=... to make, the CPPFLAGS will be +# lost. That behavior is retained in 3.48+. # -TEMP_STORE = -DSQLITE_TEMP_STORE=@TEMP_STORE@ +CFLAGS = @CFLAGS@ @CPPFLAGS@ +# +# $(LDFLAGS.configure) represents any LDFLAGS=... the client passes to +# configure. See main.mk. +# +LDFLAGS.configure = @LDFLAGS@ -# Enable/disable loadable extensions, and other optional features -# based on configuration. (-DSQLITE_OMIT*, -DSQLITE_ENABLE*). -# The same set of OMIT and ENABLE flags should be passed to the -# LEMON parser generator and the mkkeywordhash tool as well. -OPT_FEATURE_FLAGS = @OPT_FEATURE_FLAGS@ +# +# CFLAGS.core is documented in main.mk. +# +CFLAGS.core = @SH_CFLAGS@ +LDFLAGS.shlib = @SH_LDFLAGS@ +LDFLAGS.zlib = @LDFLAGS_ZLIB@ +LDFLAGS.math = @LDFLAGS_MATH@ +LDFLAGS.rpath = @LDFLAGS_RPATH@ +LDFLAGS.pthread = @LDFLAGS_PTHREAD@ +LDFLAGS.dlopen = @LDFLAGS_DLOPEN@ +LDFLAGS.readline = @LDFLAGS_READLINE@ +CFLAGS.readline = @CFLAGS_READLINE@ +LDFLAGS.icu = @LDFLAGS_ICU@ +LDFLAGS.rt = @LDFLAGS_RT@ +CFLAGS.icu = @CFLAGS_ICU@ +LDFLAGS.libsqlite3.soname = @LDFLAGS_LIBSQLITE3_SONAME@ +# soname: see https://sqlite.org/src/forumpost/5a3b44f510df8ded +LDFLAGS.libsqlite3.os-specific = \ + @LDFLAGS_MAC_CVERSION@ @LDFLAGS_MAC_INSTALL_NAME@ @LDFLAGS_OUT_IMPLIB@ +# os-specific: see +# - https://sqlite.org/forum/forumpost/9dfd5b8fd525a5d7 +# - https://sqlite.org/forum/forumpost/0c7fc097b2 +# - https://sqlite.org/forum/forumpost/5651662b8875ec0a -TCC += $(OPT_FEATURE_FLAGS) +libsqlite3.DLL.basename = @SQLITE_DLL_BASENAME@ +# DLL.basename: see https://sqlite.org/forum/forumpost/828fdfe904 +libsqlite3.out.implib = @SQLITE_OUT_IMPLIB@ +# libsqlite3.out.implib => the output filename part of LDFLAGS_OUT_IMPLIB. +ENABLE_LIB_SHARED = @ENABLE_LIB_SHARED@ +ENABLE_LIB_STATIC = @ENABLE_LIB_STATIC@ +HAVE_WASI_SDK = @HAVE_WASI_SDK@ +libsqlite3.DLL.install-rules = @SQLITE_DLL_INSTALL_RULES@ -# Add in any optional parameters specified on the make commane line -# ie. make "OPTS=-DSQLITE_ENABLE_FOO=1 -DSQLITE_OMIT_FOO=1". -TCC += $(OPTS) +# -fsanitize flags for the fuzzcheck-asap app +CFLAGS.fuzzcheck-asan.fsanitize = @CFLAGS_ASAN_FSANITIZE@ -# Version numbers and release number for the SQLite being compiled. # -VERSION = @VERSION@ -VERSION_NUMBER = @VERSION_NUMBER@ -RELEASE = @RELEASE@ - -# Filename extensions +# Intended to either be empty or be set to -g -DSQLITE_DEBUG=1. # -BEXE = @BUILD_EXEEXT@ -TEXE = @TARGET_EXEEXT@ +T.cc.TARGET_DEBUG = @TARGET_DEBUG@ -# The following variable is "1" if the configure script was able to locate -# the tclConfig.sh file. It is an empty string otherwise. When this -# variable is "1", the TCL extension library (libtclsqlite3.so) is built -# and installed. # -HAVE_TCL = @HAVE_TCL@ - -# This is the command to use for tclsh - normally just "tclsh", but we may -# know the specific version we want to use +# $(JIMSH) and $(CFLAGS.jimsh) are documented in main.mk. $(JIMSH) +# must start with a path component so that it can be invoked as a +# shell command. # -TCLSH_CMD = @TCLSH_CMD@ +CFLAGS.jimsh = @CFLAGS_JIMSH@ +JIMSH = ./jimsh$(T.exe) -# Where do we want to install the tcl plugin # -TCLLIBDIR = @TCLLIBDIR@ - -# The suffix used on shared libraries. Ex: ".dll", ".so", ".dylib" +# $(B.tclsh) is documented in main.mk. # -SHLIB_SUFFIX = @TCL_SHLIB_SUFFIX@ +B.tclsh = @BTCLSH@ +$(B.tclsh): -# If gcov support was enabled by the configure script, add the appropriate -# flags here. It's not always as easy as just having the user add the right -# CFLAGS / LDFLAGS, because libtool wants to use CFLAGS when linking, which -# causes build errors with -fprofile-arcs -ftest-coverage with some GCCs. -# Supposedly GCC does the right thing if you use --coverage, but in -# practice it still fails. See: # -# http://www.mail-archive.com/debian-gcc@lists.debian.org/msg26197.html +# $(OPT_FEATURE_FLAGS) is documented in main.mk. # -# for more info. +# The appending of $(OPTIONS) to $(OPT_FEATURE_FLAGS) is historical +# and somewhat confusing because there's another var, $(OPTS), which +# has a similar (but not identical) role. # -GCOV_CFLAGS1 = -DSQLITE_COVERAGE_TEST=1 -fprofile-arcs -ftest-coverage -GCOV_LDFLAGS1 = -lgcov -USE_GCOV = @USE_GCOV@ -LTCOMPILE_EXTRAS += $(GCOV_CFLAGS$(USE_GCOV)) -LTLINK_EXTRAS += $(GCOV_LDFLAGS$(USE_GCOV)) - -# BEGIN CRYPTO -CRYPTOLIBOBJ = \ - crypto.lo \ - crypto_impl.lo \ - crypto_openssl.lo \ - crypto_libtomcrypt.lo \ - crypto_cc.lo - -CRYPTOSRC = \ - $(TOP)/src/crypto.h \ - $(TOP)/src/sqlcipher.h \ - $(TOP)/src/crypto.c \ - $(TOP)/src/crypto_impl.c \ - $(TOP)/src/crypto_libtomcrypt.c \ - $(TOP)/src/crypto_openssl.c \ - $(TOP)/src/crypto_cc.c - -# END CRYPTO +OPT_FEATURE_FLAGS = @OPT_FEATURE_FLAGS@ $(OPTIONS) -# The directory into which to store package information for - -# Some standard variables and programs # -prefix = @prefix@ -exec_prefix = @exec_prefix@ -libdir = @libdir@ -pkgconfigdir = $(libdir)/pkgconfig -bindir = @bindir@ -includedir = @includedir@/sqlcipher -INSTALL = @INSTALL@ -LIBTOOL = ./libtool -ALLOWRELEASE = @ALLOWRELEASE@ - -# libtool compile/link/install -LTCOMPILE = $(LIBTOOL) --mode=compile --tag=CC $(TCC) $(LTCOMPILE_EXTRAS) -LTLINK = $(LIBTOOL) --mode=link $(TCC) $(LTCOMPILE_EXTRAS) @LDFLAGS@ $(LTLINK_EXTRAS) -LTINSTALL = $(LIBTOOL) --mode=install $(INSTALL) - -# You should not have to change anything below this line -############################################################################### - -USE_AMALGAMATION = @USE_AMALGAMATION@ - -# Object files for the SQLite library (non-amalgamation). -# -LIBOBJS0 = alter.lo analyze.lo attach.lo auth.lo \ - backup.lo bitvec.lo btmutex.lo btree.lo build.lo \ - callback.lo complete.lo ctime.lo date.lo dbstat.lo delete.lo \ - expr.lo fault.lo fkey.lo \ - fts3.lo fts3_aux.lo fts3_expr.lo fts3_hash.lo fts3_icu.lo \ - fts3_porter.lo fts3_snippet.lo fts3_tokenizer.lo fts3_tokenizer1.lo \ - fts3_tokenize_vtab.lo \ - fts3_unicode.lo fts3_unicode2.lo fts3_write.lo \ - fts5.lo \ - func.lo global.lo hash.lo \ - icu.lo insert.lo journal.lo json1.lo legacy.lo loadext.lo \ - main.lo malloc.lo mem0.lo mem1.lo mem2.lo mem3.lo mem5.lo \ - memjournal.lo \ - mutex.lo mutex_noop.lo mutex_unix.lo mutex_w32.lo \ - notify.lo opcodes.lo os.lo os_unix.lo os_win.lo \ - pager.lo parse.lo pcache.lo pcache1.lo pragma.lo prepare.lo printf.lo \ - random.lo resolve.lo rowset.lo rtree.lo select.lo sqlite3rbu.lo status.lo \ - table.lo threads.lo tokenize.lo treeview.lo trigger.lo \ - update.lo util.lo vacuum.lo \ - vdbe.lo vdbeapi.lo vdbeaux.lo vdbeblob.lo vdbemem.lo vdbesort.lo \ - vdbetrace.lo wal.lo walker.lo where.lo wherecode.lo whereexpr.lo \ - utf.lo vtab.lo $(CRYPTOLIBOBJ) - -# Object files for the amalgamation. +# Version (X.Y.Z) number for the SQLite being compiled. # -LIBOBJS1 = sqlite3.lo +PACKAGE_VERSION = @PACKAGE_VERSION@ -# Determine the real value of LIBOBJ based on the 'configure' script # -LIBOBJ = $(LIBOBJS$(USE_AMALGAMATION)) - - -# All of the source code files. -# -SRC = \ - $(CRYPTOSRC) \ - $(TOP)/src/alter.c \ - $(TOP)/src/analyze.c \ - $(TOP)/src/attach.c \ - $(TOP)/src/auth.c \ - $(TOP)/src/backup.c \ - $(TOP)/src/bitvec.c \ - $(TOP)/src/btmutex.c \ - $(TOP)/src/btree.c \ - $(TOP)/src/btree.h \ - $(TOP)/src/btreeInt.h \ - $(TOP)/src/build.c \ - $(TOP)/src/callback.c \ - $(TOP)/src/complete.c \ - $(TOP)/src/ctime.c \ - $(TOP)/src/date.c \ - $(TOP)/src/dbstat.c \ - $(TOP)/src/delete.c \ - $(TOP)/src/expr.c \ - $(TOP)/src/fault.c \ - $(TOP)/src/fkey.c \ - $(TOP)/src/func.c \ - $(TOP)/src/global.c \ - $(TOP)/src/hash.c \ - $(TOP)/src/hash.h \ - $(TOP)/src/hwtime.h \ - $(TOP)/src/insert.c \ - $(TOP)/src/journal.c \ - $(TOP)/src/legacy.c \ - $(TOP)/src/loadext.c \ - $(TOP)/src/main.c \ - $(TOP)/src/malloc.c \ - $(TOP)/src/mem0.c \ - $(TOP)/src/mem1.c \ - $(TOP)/src/mem2.c \ - $(TOP)/src/mem3.c \ - $(TOP)/src/mem5.c \ - $(TOP)/src/memjournal.c \ - $(TOP)/src/msvc.h \ - $(TOP)/src/mutex.c \ - $(TOP)/src/mutex.h \ - $(TOP)/src/mutex_noop.c \ - $(TOP)/src/mutex_unix.c \ - $(TOP)/src/mutex_w32.c \ - $(TOP)/src/notify.c \ - $(TOP)/src/os.c \ - $(TOP)/src/os.h \ - $(TOP)/src/os_common.h \ - $(TOP)/src/os_setup.h \ - $(TOP)/src/os_unix.c \ - $(TOP)/src/os_win.c \ - $(TOP)/src/os_win.h \ - $(TOP)/src/pager.c \ - $(TOP)/src/pager.h \ - $(TOP)/src/parse.y \ - $(TOP)/src/pcache.c \ - $(TOP)/src/pcache.h \ - $(TOP)/src/pcache1.c \ - $(TOP)/src/pragma.c \ - $(TOP)/src/pragma.h \ - $(TOP)/src/prepare.c \ - $(TOP)/src/printf.c \ - $(TOP)/src/random.c \ - $(TOP)/src/resolve.c \ - $(TOP)/src/rowset.c \ - $(TOP)/src/select.c \ - $(TOP)/src/status.c \ - $(TOP)/src/shell.c \ - $(TOP)/src/sqlite.h.in \ - $(TOP)/src/sqlite3ext.h \ - $(TOP)/src/sqliteInt.h \ - $(TOP)/src/sqliteLimit.h \ - $(TOP)/src/table.c \ - $(TOP)/src/tclsqlite.c \ - $(TOP)/src/threads.c \ - $(TOP)/src/tokenize.c \ - $(TOP)/src/treeview.c \ - $(TOP)/src/trigger.c \ - $(TOP)/src/utf.c \ - $(TOP)/src/update.c \ - $(TOP)/src/util.c \ - $(TOP)/src/vacuum.c \ - $(TOP)/src/vdbe.c \ - $(TOP)/src/vdbe.h \ - $(TOP)/src/vdbeapi.c \ - $(TOP)/src/vdbeaux.c \ - $(TOP)/src/vdbeblob.c \ - $(TOP)/src/vdbemem.c \ - $(TOP)/src/vdbesort.c \ - $(TOP)/src/vdbetrace.c \ - $(TOP)/src/vdbeInt.h \ - $(TOP)/src/vtab.c \ - $(TOP)/src/vxworks.h \ - $(TOP)/src/wal.c \ - $(TOP)/src/wal.h \ - $(TOP)/src/walker.c \ - $(TOP)/src/where.c \ - $(TOP)/src/wherecode.c \ - $(TOP)/src/whereexpr.c \ - $(TOP)/src/whereInt.h - -# Source code for extensions -# -SRC += \ - $(TOP)/ext/fts1/fts1.c \ - $(TOP)/ext/fts1/fts1.h \ - $(TOP)/ext/fts1/fts1_hash.c \ - $(TOP)/ext/fts1/fts1_hash.h \ - $(TOP)/ext/fts1/fts1_porter.c \ - $(TOP)/ext/fts1/fts1_tokenizer.h \ - $(TOP)/ext/fts1/fts1_tokenizer1.c -SRC += \ - $(TOP)/ext/fts2/fts2.c \ - $(TOP)/ext/fts2/fts2.h \ - $(TOP)/ext/fts2/fts2_hash.c \ - $(TOP)/ext/fts2/fts2_hash.h \ - $(TOP)/ext/fts2/fts2_icu.c \ - $(TOP)/ext/fts2/fts2_porter.c \ - $(TOP)/ext/fts2/fts2_tokenizer.h \ - $(TOP)/ext/fts2/fts2_tokenizer.c \ - $(TOP)/ext/fts2/fts2_tokenizer1.c -SRC += \ - $(TOP)/ext/fts3/fts3.c \ - $(TOP)/ext/fts3/fts3.h \ - $(TOP)/ext/fts3/fts3Int.h \ - $(TOP)/ext/fts3/fts3_aux.c \ - $(TOP)/ext/fts3/fts3_expr.c \ - $(TOP)/ext/fts3/fts3_hash.c \ - $(TOP)/ext/fts3/fts3_hash.h \ - $(TOP)/ext/fts3/fts3_icu.c \ - $(TOP)/ext/fts3/fts3_porter.c \ - $(TOP)/ext/fts3/fts3_snippet.c \ - $(TOP)/ext/fts3/fts3_tokenizer.h \ - $(TOP)/ext/fts3/fts3_tokenizer.c \ - $(TOP)/ext/fts3/fts3_tokenizer1.c \ - $(TOP)/ext/fts3/fts3_tokenize_vtab.c \ - $(TOP)/ext/fts3/fts3_unicode.c \ - $(TOP)/ext/fts3/fts3_unicode2.c \ - $(TOP)/ext/fts3/fts3_write.c -SRC += \ - $(TOP)/ext/icu/sqliteicu.h \ - $(TOP)/ext/icu/icu.c -SRC += \ - $(TOP)/ext/rtree/rtree.h \ - $(TOP)/ext/rtree/rtree.c -SRC += \ - $(TOP)/ext/rbu/sqlite3rbu.h \ - $(TOP)/ext/rbu/sqlite3rbu.c -SRC += \ - $(TOP)/ext/misc/json1.c - - - -# Generated source code files -# -SRC += \ - keywordhash.h \ - opcodes.c \ - opcodes.h \ - parse.c \ - parse.h \ - config.h \ - sqlite3.h - -# Source code to the test files. -# -TESTSRC = \ - $(TOP)/src/test1.c \ - $(TOP)/src/test2.c \ - $(TOP)/src/test3.c \ - $(TOP)/src/test4.c \ - $(TOP)/src/test5.c \ - $(TOP)/src/test6.c \ - $(TOP)/src/test7.c \ - $(TOP)/src/test8.c \ - $(TOP)/src/test9.c \ - $(TOP)/src/test_autoext.c \ - $(TOP)/src/test_async.c \ - $(TOP)/src/test_backup.c \ - $(TOP)/src/test_blob.c \ - $(TOP)/src/test_btree.c \ - $(TOP)/src/test_config.c \ - $(TOP)/src/test_demovfs.c \ - $(TOP)/src/test_devsym.c \ - $(TOP)/src/test_fs.c \ - $(TOP)/src/test_func.c \ - $(TOP)/src/test_hexio.c \ - $(TOP)/src/test_init.c \ - $(TOP)/src/test_intarray.c \ - $(TOP)/src/test_journal.c \ - $(TOP)/src/test_malloc.c \ - $(TOP)/src/test_multiplex.c \ - $(TOP)/src/test_mutex.c \ - $(TOP)/src/test_onefile.c \ - $(TOP)/src/test_osinst.c \ - $(TOP)/src/test_pcache.c \ - $(TOP)/src/test_quota.c \ - $(TOP)/src/test_rtree.c \ - $(TOP)/src/test_schema.c \ - $(TOP)/src/test_server.c \ - $(TOP)/src/test_superlock.c \ - $(TOP)/src/test_syscall.c \ - $(TOP)/src/test_tclvar.c \ - $(TOP)/src/test_thread.c \ - $(TOP)/src/test_vfs.c \ - $(TOP)/src/test_windirent.c \ - $(TOP)/src/test_wsd.c \ - $(TOP)/ext/fts3/fts3_term.c \ - $(TOP)/ext/fts3/fts3_test.c \ - $(TOP)/ext/rbu/test_rbu.c - -# Statically linked extensions -# -TESTSRC += \ - $(TOP)/ext/misc/amatch.c \ - $(TOP)/ext/misc/closure.c \ - $(TOP)/ext/misc/eval.c \ - $(TOP)/ext/misc/fileio.c \ - $(TOP)/ext/misc/fuzzer.c \ - $(TOP)/ext/fts5/fts5_tcl.c \ - $(TOP)/ext/fts5/fts5_test_mi.c \ - $(TOP)/ext/fts5/fts5_test_tok.c \ - $(TOP)/ext/misc/ieee754.c \ - $(TOP)/ext/misc/nextchar.c \ - $(TOP)/ext/misc/percentile.c \ - $(TOP)/ext/misc/regexp.c \ - $(TOP)/ext/misc/series.c \ - $(TOP)/ext/misc/spellfix.c \ - $(TOP)/ext/misc/totype.c \ - $(TOP)/ext/misc/wholenumber.c - -# Source code to the library files needed by the test fixture -# -TESTSRC2 = \ - $(TOP)/src/attach.c \ - $(TOP)/src/backup.c \ - $(TOP)/src/bitvec.c \ - $(TOP)/src/btree.c \ - $(TOP)/src/build.c \ - $(TOP)/src/ctime.c \ - $(TOP)/src/date.c \ - $(TOP)/src/dbstat.c \ - $(TOP)/src/expr.c \ - $(TOP)/src/func.c \ - $(TOP)/src/insert.c \ - $(TOP)/src/wal.c \ - $(TOP)/src/main.c \ - $(TOP)/src/mem5.c \ - $(TOP)/src/os.c \ - $(TOP)/src/os_unix.c \ - $(TOP)/src/os_win.c \ - $(TOP)/src/pager.c \ - $(TOP)/src/pragma.c \ - $(TOP)/src/prepare.c \ - $(TOP)/src/printf.c \ - $(TOP)/src/random.c \ - $(TOP)/src/pcache.c \ - $(TOP)/src/pcache1.c \ - $(TOP)/src/select.c \ - $(TOP)/src/tokenize.c \ - $(TOP)/src/utf.c \ - $(TOP)/src/util.c \ - $(TOP)/src/vdbeapi.c \ - $(TOP)/src/vdbeaux.c \ - $(TOP)/src/vdbe.c \ - $(TOP)/src/vdbemem.c \ - $(TOP)/src/vdbetrace.c \ - $(TOP)/src/where.c \ - $(TOP)/src/wherecode.c \ - $(TOP)/src/whereexpr.c \ - parse.c \ - $(TOP)/ext/fts3/fts3.c \ - $(TOP)/ext/fts3/fts3_aux.c \ - $(TOP)/ext/fts3/fts3_expr.c \ - $(TOP)/ext/fts3/fts3_term.c \ - $(TOP)/ext/fts3/fts3_tokenizer.c \ - $(TOP)/ext/fts3/fts3_write.c \ - $(TOP)/ext/async/sqlite3async.c - -# Header files used by all library source files. -# -HDR = \ - $(TOP)/src/btree.h \ - $(TOP)/src/btreeInt.h \ - $(TOP)/src/hash.h \ - $(TOP)/src/hwtime.h \ - keywordhash.h \ - $(TOP)/src/msvc.h \ - $(TOP)/src/mutex.h \ - opcodes.h \ - $(TOP)/src/os.h \ - $(TOP)/src/os_common.h \ - $(TOP)/src/os_setup.h \ - $(TOP)/src/os_win.h \ - $(TOP)/src/pager.h \ - $(TOP)/src/pcache.h \ - parse.h \ - $(TOP)/src/pragma.h \ - sqlite3.h \ - $(TOP)/src/sqlite3ext.h \ - $(TOP)/src/sqliteInt.h \ - $(TOP)/src/sqliteLimit.h \ - $(TOP)/src/vdbe.h \ - $(TOP)/src/vdbeInt.h \ - $(TOP)/src/vxworks.h \ - $(TOP)/src/whereInt.h \ - config.h - -# Header files used by extensions -# -EXTHDR += \ - $(TOP)/ext/fts1/fts1.h \ - $(TOP)/ext/fts1/fts1_hash.h \ - $(TOP)/ext/fts1/fts1_tokenizer.h -EXTHDR += \ - $(TOP)/ext/fts2/fts2.h \ - $(TOP)/ext/fts2/fts2_hash.h \ - $(TOP)/ext/fts2/fts2_tokenizer.h -EXTHDR += \ - $(TOP)/ext/fts3/fts3.h \ - $(TOP)/ext/fts3/fts3Int.h \ - $(TOP)/ext/fts3/fts3_hash.h \ - $(TOP)/ext/fts3/fts3_tokenizer.h -EXTHDR += \ - $(TOP)/ext/rtree/rtree.h -EXTHDR += \ - $(TOP)/ext/icu/sqliteicu.h -EXTHDR += \ - $(TOP)/ext/rtree/sqlite3rtree.h - -# executables needed for testing +# Filename extensions for binaries and libraries # -TESTPROGS = \ - testfixture$(TEXE) \ - sqlite3$(TEXE) \ - sqlite3_analyzer$(TEXE) \ - sqldiff$(TEXE) +B.exe = @BUILD_EXEEXT@ +T.exe = @TARGET_EXEEXT@ +B.dll = @BUILD_DLLEXT@ +T.dll = @TARGET_DLLEXT@ +B.lib = @BUILD_LIBEXT@ +T.lib = @TARGET_LIBEXT@ -# Databases containing fuzzer test cases # -FUZZDATA = \ - $(TOP)/test/fuzzdata1.db \ - $(TOP)/test/fuzzdata2.db \ - $(TOP)/test/fuzzdata3.db \ - $(TOP)/test/fuzzdata4.db - -# Standard options to testfixture +# $(HAVE_TCL) is 1 if the configure script was able to locate the +# tclConfig.sh file, else it is 0. When this variable is 1, the TCL +# extension library (libtclsqlite3.so) and related testing apps are +# built. # -TESTOPTS = --verbose=file --output=test-out.txt +HAVE_TCL = @HAVE_TCL@ -# Extra compiler options for various shell tools # -SHELL_OPT = -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_FTS4 -SHELL_OPT += -DSQLITE_ENABLE_EXPLAIN_COMMENTS -FUZZERSHELL_OPT = -DSQLITE_ENABLE_JSON1 -FUZZCHECK_OPT = -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_MEMSYS5 - -# This is the default Makefile target. The objects listed here -# are what get build when you type just "make" with no arguments. +# $(TCLSH_CMD) is the command to use for tclsh - normally just +# "tclsh", but we may know the specific version we want to use. This +# must point to a canonical TCL interpreter, not JimTCL. # -all: sqlite3.h libsqlcipher.la sqlcipher$(TEXE) $(HAVE_TCL:1=libtclsqlite3.la) - -Makefile: $(TOP)/Makefile.in - ./config.status - -sqlcipher.pc: $(TOP)/sqlcipher.pc.in - ./config.status - -libsqlcipher.la: $(LIBOBJ) - $(LTLINK) -o $@ $(LIBOBJ) $(TLIBS) \ - ${ALLOWRELEASE} -rpath "$(libdir)" -version-info "8:6:8" - -libtclsqlite3.la: tclsqlite.lo libsqlcipher.la - $(LTLINK) -o $@ tclsqlite.lo \ - libsqlcipher.la @TCL_STUB_LIB_SPEC@ $(TLIBS) \ - -rpath "$(TCLLIBDIR)" \ - -version-info "8:6:8" \ - -avoid-version - -sqlcipher$(TEXE): $(TOP)/src/shell.c libsqlcipher.la sqlite3.h - $(LTLINK) $(READLINE_FLAGS) $(SHELL_OPT) \ - -o $@ $(TOP)/src/shell.c libsqlcipher.la \ - $(LIBREADLINE) $(TLIBS) -rpath "$(libdir)" - -sqldiff$(TEXE): $(TOP)/tool/sqldiff.c sqlite3.c sqlite3.h - $(LTLINK) -o $@ $(TOP)/tool/sqldiff.c sqlite3.c $(TLIBS) - -srcck1$(BEXE): $(TOP)/tool/srcck1.c - $(BCC) -o srcck1$(BEXE) $(TOP)/tool/srcck1.c - -sourcetest: srcck1$(BEXE) sqlite3.c - ./srcck1 sqlite3.c - -fuzzershell$(TEXE): $(TOP)/tool/fuzzershell.c sqlite3.c sqlite3.h - $(LTLINK) -o $@ $(FUZZERSHELL_OPT) \ - $(TOP)/tool/fuzzershell.c sqlite3.c $(TLIBS) - -fuzzcheck$(TEXE): $(TOP)/test/fuzzcheck.c sqlite3.c sqlite3.h - $(LTLINK) -o $@ $(FUZZCHECK_OPT) $(TOP)/test/fuzzcheck.c sqlite3.c $(TLIBS) - -mptester$(TEXE): sqlite3.c $(TOP)/mptest/mptest.c - $(LTLINK) -o $@ -I. $(TOP)/mptest/mptest.c sqlite3.c \ - $(TLIBS) -rpath "$(libdir)" - -MPTEST1=./mptester$(TEXE) mptest.db $(TOP)/mptest/crash01.test --repeat 20 -MPTEST2=./mptester$(TEXE) mptest.db $(TOP)/mptest/multiwrite01.test --repeat 20 -mptest: mptester$(TEXE) - rm -f mptest.db - $(MPTEST1) --journalmode DELETE - $(MPTEST2) --journalmode WAL - $(MPTEST1) --journalmode WAL - $(MPTEST2) --journalmode PERSIST - $(MPTEST1) --journalmode PERSIST - $(MPTEST2) --journalmode TRUNCATE - $(MPTEST1) --journalmode TRUNCATE - $(MPTEST2) --journalmode DELETE - - -# This target creates a directory named "tsrc" and fills it with -# copies of all of the C source code and header files needed to -# build on the target system. Some of the C source code and header -# files are automatically generated. This target takes care of -# all that automatic generation. -# -.target_source: $(SRC) $(TOP)/tool/vdbe-compress.tcl fts5.c - rm -rf tsrc - mkdir tsrc - cp -f $(SRC) tsrc - rm tsrc/sqlite.h.in tsrc/parse.y - $(TCLSH_CMD) $(TOP)/tool/vdbe-compress.tcl $(OPTS) vdbe.new - mv vdbe.new tsrc/vdbe.c - cp fts5.c fts5.h tsrc - touch .target_source - -sqlite3.c: .target_source $(TOP)/tool/mksqlite3c.tcl - $(TCLSH_CMD) $(TOP)/tool/mksqlite3c.tcl - cp tsrc/shell.c tsrc/sqlite3ext.h . - -sqlite3ext.h: .target_source - cp tsrc/sqlite3ext.h . - -tclsqlite3.c: sqlite3.c - echo '#ifndef USE_SYSTEM_SQLITE' >tclsqlite3.c - cat sqlite3.c >>tclsqlite3.c - echo '#endif /* USE_SYSTEM_SQLITE */' >>tclsqlite3.c - cat $(TOP)/src/tclsqlite.c >>tclsqlite3.c - -sqlite3-all.c: sqlite3.c $(TOP)/tool/split-sqlite3c.tcl - $(TCLSH_CMD) $(TOP)/tool/split-sqlite3c.tcl +TCLSH_CMD = @TCLSH_CMD@ +TCL_CONFIG_SH = @TCL_CONFIG_SH@ -# Rule to build the amalgamation # -sqlite3.lo: sqlite3.c - $(LTCOMPILE) $(TEMP_STORE) -c sqlite3.c - -# Rules to build the LEMON compiler generator +# TCL config info from tclConfig.sh +# +# We have to inject this differently in main.mk to accommodate static +# makefiles, so we don't currently bother to export it here. This +# block is retained in case we decide that we do indeed need to export +# it at configure-time instead of calculate it at make-time. # -lemon$(BEXE): $(TOP)/tool/lemon.c $(TOP)/tool/lempar.c - $(BCC) -o $@ $(TOP)/tool/lemon.c - cp $(TOP)/tool/lempar.c . +#TCL_INCLUDE_SPEC = @TCL_INCLUDE_SPEC@ +#TCL_LIB_SPEC = @TCL_LIB_SPEC@ +#TCL_STUB_LIB_SPEC = @TCL_STUB_LIB_SPEC@ +#TCL_EXEC_PREFIX = @TCL_EXEC_PREFIX@ +#TCL_VERSION = @TCL_VERSION@ +TCL_MAJOR_VERSION = @TCL_MAJOR_VERSION@ +# ^^^ main.mk optionally uses this for determining the Tcl extension's +# DLL name. +TCL_EXT_DLL_BASENAME = @TCL_EXT_DLL_BASENAME@ +# ^^^ base name of the Tcl extension DLL. It varies by platform and +# Tcl version. -# Rules to build individual *.o files from generated *.c files. This -# applies to: # -# parse.o -# opcodes.o +# $(TCLLIBDIR) = where to install the tcl plugin. If this is empty, it +# is calculated at make-time by the targets which need it but we +# export it here so that it can be set at configure-time, so that +# clients are not required to pass it at make-time, or may set it in +# their environment to override it. # -parse.lo: parse.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c parse.c - -opcodes.lo: opcodes.c - $(LTCOMPILE) $(TEMP_STORE) -c opcodes.c - -# BEGIN CRYPTO -crypto.lo: $(TOP)/src/crypto.c $(HDR) - $(LTCOMPILE) -c $(TOP)/src/crypto.c -crypto_impl.lo: $(TOP)/src/crypto_impl.c $(HDR) - $(LTCOMPILE) -c $(TOP)/src/crypto_impl.c -crypto_openssl.lo: $(TOP)/src/crypto_openssl.c $(HDR) - $(LTCOMPILE) -c $(TOP)/src/crypto_openssl.c -crypto_libtomcrypt.lo: $(TOP)/src/crypto_libtomcrypt.c $(HDR) - $(LTCOMPILE) -c $(TOP)/src/crypto_libtomcrypt.c -crypto_cc.lo: $(TOP)/src/crypto_cc.c $(HDR) - $(LTCOMPILE) -c $(TOP)/src/crypto_cc.c -# END CRYPTO +TCLLIBDIR = @TCLLIBDIR@ -# Rules to build individual *.o files from files in the src directory. # -alter.lo: $(TOP)/src/alter.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/alter.c - -analyze.lo: $(TOP)/src/analyze.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/analyze.c - -attach.lo: $(TOP)/src/attach.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/attach.c - -auth.lo: $(TOP)/src/auth.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/auth.c - -backup.lo: $(TOP)/src/backup.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/backup.c - -bitvec.lo: $(TOP)/src/bitvec.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/bitvec.c - -btmutex.lo: $(TOP)/src/btmutex.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/btmutex.c - -btree.lo: $(TOP)/src/btree.c $(HDR) $(TOP)/src/pager.h - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/btree.c - -build.lo: $(TOP)/src/build.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/build.c - -callback.lo: $(TOP)/src/callback.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/callback.c - -complete.lo: $(TOP)/src/complete.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/complete.c - -ctime.lo: $(TOP)/src/ctime.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/ctime.c - -date.lo: $(TOP)/src/date.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/date.c - -dbstat.lo: $(TOP)/src/dbstat.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/dbstat.c - -delete.lo: $(TOP)/src/delete.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/delete.c - -expr.lo: $(TOP)/src/expr.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/expr.c - -fault.lo: $(TOP)/src/fault.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/fault.c - -fkey.lo: $(TOP)/src/fkey.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/fkey.c - -func.lo: $(TOP)/src/func.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/func.c - -global.lo: $(TOP)/src/global.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/global.c - -hash.lo: $(TOP)/src/hash.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/hash.c - -insert.lo: $(TOP)/src/insert.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/insert.c - -journal.lo: $(TOP)/src/journal.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/journal.c - -legacy.lo: $(TOP)/src/legacy.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/legacy.c - -loadext.lo: $(TOP)/src/loadext.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/loadext.c - -main.lo: $(TOP)/src/main.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/main.c - -malloc.lo: $(TOP)/src/malloc.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/malloc.c - -mem0.lo: $(TOP)/src/mem0.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/mem0.c - -mem1.lo: $(TOP)/src/mem1.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/mem1.c - -mem2.lo: $(TOP)/src/mem2.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/mem2.c - -mem3.lo: $(TOP)/src/mem3.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/mem3.c - -mem5.lo: $(TOP)/src/mem5.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/mem5.c - -memjournal.lo: $(TOP)/src/memjournal.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/memjournal.c - -mutex.lo: $(TOP)/src/mutex.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/mutex.c - -mutex_noop.lo: $(TOP)/src/mutex_noop.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/mutex_noop.c - -mutex_unix.lo: $(TOP)/src/mutex_unix.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/mutex_unix.c - -mutex_w32.lo: $(TOP)/src/mutex_w32.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/mutex_w32.c - -notify.lo: $(TOP)/src/notify.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/notify.c - -pager.lo: $(TOP)/src/pager.c $(HDR) $(TOP)/src/pager.h - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/pager.c - -pcache.lo: $(TOP)/src/pcache.c $(HDR) $(TOP)/src/pcache.h - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/pcache.c - -pcache1.lo: $(TOP)/src/pcache1.c $(HDR) $(TOP)/src/pcache.h - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/pcache1.c - -os.lo: $(TOP)/src/os.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/os.c - -os_unix.lo: $(TOP)/src/os_unix.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/os_unix.c - -os_win.lo: $(TOP)/src/os_win.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/os_win.c - -pragma.lo: $(TOP)/src/pragma.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/pragma.c - -prepare.lo: $(TOP)/src/prepare.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/prepare.c - -printf.lo: $(TOP)/src/printf.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/printf.c - -random.lo: $(TOP)/src/random.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/random.c - -resolve.lo: $(TOP)/src/resolve.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/resolve.c - -rowset.lo: $(TOP)/src/rowset.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/rowset.c - -select.lo: $(TOP)/src/select.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/select.c - -status.lo: $(TOP)/src/status.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/status.c - -table.lo: $(TOP)/src/table.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/table.c - -threads.lo: $(TOP)/src/threads.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/threads.c - -tokenize.lo: $(TOP)/src/tokenize.c keywordhash.h $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/tokenize.c - -treeview.lo: $(TOP)/src/treeview.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/treeview.c - -trigger.lo: $(TOP)/src/trigger.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/trigger.c - -update.lo: $(TOP)/src/update.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/update.c - -utf.lo: $(TOP)/src/utf.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/utf.c - -util.lo: $(TOP)/src/util.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/util.c - -vacuum.lo: $(TOP)/src/vacuum.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/vacuum.c - -vdbe.lo: $(TOP)/src/vdbe.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/vdbe.c - -vdbeapi.lo: $(TOP)/src/vdbeapi.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/vdbeapi.c - -vdbeaux.lo: $(TOP)/src/vdbeaux.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/vdbeaux.c - -vdbeblob.lo: $(TOP)/src/vdbeblob.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/vdbeblob.c - -vdbemem.lo: $(TOP)/src/vdbemem.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/vdbemem.c - -vdbesort.lo: $(TOP)/src/vdbesort.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/vdbesort.c - -vdbetrace.lo: $(TOP)/src/vdbetrace.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/vdbetrace.c - -vtab.lo: $(TOP)/src/vtab.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/vtab.c - -wal.lo: $(TOP)/src/wal.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/wal.c - -walker.lo: $(TOP)/src/walker.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/walker.c - -where.lo: $(TOP)/src/where.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/where.c - -wherecode.lo: $(TOP)/src/wherecode.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/wherecode.c - -whereexpr.lo: $(TOP)/src/whereexpr.c $(HDR) - $(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/whereexpr.c - -tclsqlite.lo: $(TOP)/src/tclsqlite.c $(HDR) - $(LTCOMPILE) -DUSE_TCL_STUBS=1 -c $(TOP)/src/tclsqlite.c - -tclsqlite-shell.lo: $(TOP)/src/tclsqlite.c $(HDR) - $(LTCOMPILE) -DTCLSH=1 -o $@ -c $(TOP)/src/tclsqlite.c - -tclsqlite-stubs.lo: $(TOP)/src/tclsqlite.c $(HDR) - $(LTCOMPILE) -DUSE_TCL_STUBS=1 -o $@ -c $(TOP)/src/tclsqlite.c - -tclsqlcipher$(TEXE): tclsqlite-shell.lo libsqlcipher.la - $(LTLINK) -o $@ tclsqlite-shell.lo \ - libsqlcipher.la $(LIBTCL) - -# Rules to build opcodes.c and opcodes.h +# Additional options when running tests using testrunner.tcl +# This is usually either blank or --status. # -opcodes.c: opcodes.h $(TOP)/tool/mkopcodec.tcl - $(TCLSH_CMD) $(TOP)/tool/mkopcodec.tcl opcodes.h >opcodes.c - -opcodes.h: parse.h $(TOP)/src/vdbe.c $(TOP)/tool/mkopcodeh.tcl - cat parse.h $(TOP)/src/vdbe.c | $(TCLSH_CMD) $(TOP)/tool/mkopcodeh.tcl >opcodes.h +TSTRNNR_OPTS = @TSTRNNR_OPTS@ -# Rules to build parse.c and parse.h - the outputs of lemon. # -parse.h: parse.c - -parse.c: $(TOP)/src/parse.y lemon$(BEXE) $(TOP)/tool/addopcodes.tcl - cp $(TOP)/src/parse.y . - rm -f parse.h - ./lemon$(BEXE) $(OPT_FEATURE_FLAGS) $(OPTS) parse.y - mv parse.h parse.h.temp - $(TCLSH_CMD) $(TOP)/tool/addopcodes.tcl parse.h.temp >parse.h - -sqlite3.h: $(TOP)/src/sqlite.h.in $(TOP)/manifest.uuid $(TOP)/VERSION - $(TCLSH_CMD) $(TOP)/tool/mksqlite3h.tcl $(TOP) >sqlite3.h - -keywordhash.h: $(TOP)/tool/mkkeywordhash.c - $(BCC) -o mkkeywordhash$(BEXE) $(OPT_FEATURE_FLAGS) $(OPTS) $(TOP)/tool/mkkeywordhash.c - ./mkkeywordhash$(BEXE) >keywordhash.h - - - -# Rules to build the extension objects. +# If gcov support was enabled by the configure script, add the appropriate +# flags here. It's not always as easy as just having the user add the right +# CFLAGS / LDFLAGS, because libtool wants to use CFLAGS when linking, which +# causes build errors with -fprofile-arcs -ftest-coverage with some GCCs. +# Supposedly GCC does the right thing if you use --coverage, but in +# practice it still fails. See: # -icu.lo: $(TOP)/ext/icu/icu.c $(HDR) $(EXTHDR) - $(LTCOMPILE) -DSQLITE_CORE -c $(TOP)/ext/icu/icu.c - -fts2.lo: $(TOP)/ext/fts2/fts2.c $(HDR) $(EXTHDR) - $(LTCOMPILE) -DSQLITE_CORE -c $(TOP)/ext/fts2/fts2.c - -fts2_hash.lo: $(TOP)/ext/fts2/fts2_hash.c $(HDR) $(EXTHDR) - $(LTCOMPILE) -DSQLITE_CORE -c $(TOP)/ext/fts2/fts2_hash.c - -fts2_icu.lo: $(TOP)/ext/fts2/fts2_icu.c $(HDR) $(EXTHDR) - $(LTCOMPILE) -DSQLITE_CORE -c $(TOP)/ext/fts2/fts2_icu.c - -fts2_porter.lo: $(TOP)/ext/fts2/fts2_porter.c $(HDR) $(EXTHDR) - $(LTCOMPILE) -DSQLITE_CORE -c $(TOP)/ext/fts2/fts2_porter.c - -fts2_tokenizer.lo: $(TOP)/ext/fts2/fts2_tokenizer.c $(HDR) $(EXTHDR) - $(LTCOMPILE) -DSQLITE_CORE -c $(TOP)/ext/fts2/fts2_tokenizer.c - -fts2_tokenizer1.lo: $(TOP)/ext/fts2/fts2_tokenizer1.c $(HDR) $(EXTHDR) - $(LTCOMPILE) -DSQLITE_CORE -c $(TOP)/ext/fts2/fts2_tokenizer1.c - -fts3.lo: $(TOP)/ext/fts3/fts3.c $(HDR) $(EXTHDR) - $(LTCOMPILE) -DSQLITE_CORE -c $(TOP)/ext/fts3/fts3.c - -fts3_aux.lo: $(TOP)/ext/fts3/fts3_aux.c $(HDR) $(EXTHDR) - $(LTCOMPILE) -DSQLITE_CORE -c $(TOP)/ext/fts3/fts3_aux.c - -fts3_expr.lo: $(TOP)/ext/fts3/fts3_expr.c $(HDR) $(EXTHDR) - $(LTCOMPILE) -DSQLITE_CORE -c $(TOP)/ext/fts3/fts3_expr.c - -fts3_hash.lo: $(TOP)/ext/fts3/fts3_hash.c $(HDR) $(EXTHDR) - $(LTCOMPILE) -DSQLITE_CORE -c $(TOP)/ext/fts3/fts3_hash.c - -fts3_icu.lo: $(TOP)/ext/fts3/fts3_icu.c $(HDR) $(EXTHDR) - $(LTCOMPILE) -DSQLITE_CORE -c $(TOP)/ext/fts3/fts3_icu.c - -fts3_porter.lo: $(TOP)/ext/fts3/fts3_porter.c $(HDR) $(EXTHDR) - $(LTCOMPILE) -DSQLITE_CORE -c $(TOP)/ext/fts3/fts3_porter.c - -fts3_snippet.lo: $(TOP)/ext/fts3/fts3_snippet.c $(HDR) $(EXTHDR) - $(LTCOMPILE) -DSQLITE_CORE -c $(TOP)/ext/fts3/fts3_snippet.c - -fts3_tokenizer.lo: $(TOP)/ext/fts3/fts3_tokenizer.c $(HDR) $(EXTHDR) - $(LTCOMPILE) -DSQLITE_CORE -c $(TOP)/ext/fts3/fts3_tokenizer.c - -fts3_tokenizer1.lo: $(TOP)/ext/fts3/fts3_tokenizer1.c $(HDR) $(EXTHDR) - $(LTCOMPILE) -DSQLITE_CORE -c $(TOP)/ext/fts3/fts3_tokenizer1.c - -fts3_tokenize_vtab.lo: $(TOP)/ext/fts3/fts3_tokenize_vtab.c $(HDR) $(EXTHDR) - $(LTCOMPILE) -DSQLITE_CORE -c $(TOP)/ext/fts3/fts3_tokenize_vtab.c - -fts3_unicode.lo: $(TOP)/ext/fts3/fts3_unicode.c $(HDR) $(EXTHDR) - $(LTCOMPILE) -DSQLITE_CORE -c $(TOP)/ext/fts3/fts3_unicode.c - -fts3_unicode2.lo: $(TOP)/ext/fts3/fts3_unicode2.c $(HDR) $(EXTHDR) - $(LTCOMPILE) -DSQLITE_CORE -c $(TOP)/ext/fts3/fts3_unicode2.c - -fts3_write.lo: $(TOP)/ext/fts3/fts3_write.c $(HDR) $(EXTHDR) - $(LTCOMPILE) -DSQLITE_CORE -c $(TOP)/ext/fts3/fts3_write.c - -rtree.lo: $(TOP)/ext/rtree/rtree.c $(HDR) $(EXTHDR) - $(LTCOMPILE) -DSQLITE_CORE -c $(TOP)/ext/rtree/rtree.c - -json1.lo: $(TOP)/ext/misc/json1.c - $(LTCOMPILE) -DSQLITE_CORE -c $(TOP)/ext/misc/json1.c - -# FTS5 things -# -FTS5_SRC = \ - $(TOP)/ext/fts5/fts5.h \ - $(TOP)/ext/fts5/fts5Int.h \ - $(TOP)/ext/fts5/fts5_aux.c \ - $(TOP)/ext/fts5/fts5_buffer.c \ - $(TOP)/ext/fts5/fts5_main.c \ - $(TOP)/ext/fts5/fts5_config.c \ - $(TOP)/ext/fts5/fts5_expr.c \ - $(TOP)/ext/fts5/fts5_hash.c \ - $(TOP)/ext/fts5/fts5_index.c \ - fts5parse.c fts5parse.h \ - $(TOP)/ext/fts5/fts5_storage.c \ - $(TOP)/ext/fts5/fts5_tokenize.c \ - $(TOP)/ext/fts5/fts5_unicode2.c \ - $(TOP)/ext/fts5/fts5_varint.c \ - $(TOP)/ext/fts5/fts5_vocab.c \ - -fts5parse.c: $(TOP)/ext/fts5/fts5parse.y lemon - cp $(TOP)/ext/fts5/fts5parse.y . - rm -f fts5parse.h - ./lemon $(OPTS) fts5parse.y - -fts5parse.h: fts5parse.c - -fts5.c: $(FTS5_SRC) - $(TCLSH_CMD) $(TOP)/ext/fts5/tool/mkfts5c.tcl - cp $(TOP)/ext/fts5/fts5.h . - -fts5.lo: fts5.c $(HDR) $(EXTHDR) - $(LTCOMPILE) -DSQLITE_CORE -c fts5.c - -sqlite3rbu.lo: $(TOP)/ext/rbu/sqlite3rbu.c $(HDR) $(EXTHDR) - $(LTCOMPILE) -DSQLITE_CORE -c $(TOP)/ext/rbu/sqlite3rbu.c - - -# Rules to build the 'testfixture' application. +# http://www.mail-archive.com/debian-gcc@lists.debian.org/msg26197.html # -# If using the amalgamation, use sqlite3.c directly to build the test -# fixture. Otherwise link against libsqlcipher.la. (This distinction is -# necessary because the test fixture requires non-API symbols which are -# hidden when the library is built via the amalgamation). +# for more info. # -TESTFIXTURE_FLAGS = -DTCLSH=1 -DSQLITE_TEST=1 -DSQLITE_CRASH_TEST=1 -TESTFIXTURE_FLAGS += -DSQLITE_SERVER=1 -DSQLITE_PRIVATE="" -DSQLITE_CORE -TESTFIXTURE_FLAGS += -DBUILD_sqlite - -TESTFIXTURE_SRC0 = $(TESTSRC2) libsqlcipher.la -TESTFIXTURE_SRC1 = sqlite3.c -TESTFIXTURE_SRC = $(TESTSRC) $(TOP)/src/tclsqlite.c -TESTFIXTURE_SRC += $(TESTFIXTURE_SRC$(USE_AMALGAMATION)) - -testfixture$(TEXE): $(TESTFIXTURE_SRC) - $(LTLINK) -DSQLITE_NO_SYNC=1 $(TEMP_STORE) $(TESTFIXTURE_FLAGS) \ - -o $@ $(TESTFIXTURE_SRC) $(LIBTCL) $(TLIBS) - -# A very detailed test running most or all test cases -fulltest: $(TESTPROGS) fuzztest - ./testfixture$(TEXE) $(TOP)/test/all.test $(TESTOPTS) - -# Really really long testing -soaktest: $(TESTPROGS) - ./testfixture$(TEXE) $(TOP)/test/all.test -soak=1 $(TESTOPTS) - -# Do extra testing but not everything. -fulltestonly: $(TESTPROGS) fuzztest - ./testfixture$(TEXE) $(TOP)/test/full.test - -# Fuzz testing -fuzztest: fuzzcheck$(TEXE) $(FUZZDATA) - ./fuzzcheck$(TEXE) $(FUZZDATA) - -fastfuzztest: fuzzcheck$(TEXE) $(FUZZDATA) - ./fuzzcheck$(TEXE) --limit-mem 100M $(FUZZDATA) - -valgrindfuzz: fuzzcheck$(TEXT) $(FUZZDATA) - valgrind ./fuzzcheck$(TEXE) --cell-size-check --limit-mem 10M --timeout 600 $(FUZZDATA) +CFLAGS.gcov1 = -DSQLITE_COVERAGE_TEST=1 -fprofile-arcs -ftest-coverage +LDFLAGS.gcov1 = -lgcov +USE_GCOV = @USE_GCOV@ +T.compile.gcov = $(CFLAGS.gcov$(USE_GCOV)) +T.link.gcov = $(LDFLAGS.gcov$(USE_GCOV)) -# Minimal testing that runs in less than 3 minutes # -quicktest: ./testfixture$(TEXE) - ./testfixture$(TEXE) $(TOP)/test/extraquick.test $(TESTOPTS) - -# This is the common case. Run many tests that do not take too long, -# including fuzzcheck, sqlite3_analyzer, and sqldiff tests. +# Vars with the AS_ prefix are specifically related to AutoSetup. # -test: $(TESTPROGS) sourcetest fastfuzztest - ./testfixture$(TEXE) $(TOP)/test/veryquick.test $(TESTOPTS) - -# Run a test using valgrind. This can take a really long time -# because valgrind is so much slower than a native machine. +# AS_AUTO_DEF is the main configure script. # -valgrindtest: $(TESTPROGS) valgrindfuzz - OMIT_MISUSE=1 valgrind -v ./testfixture$(TEXE) $(TOP)/test/permutations.test valgrind $(TESTOPTS) - -# A very fast test that checks basic sanity. The name comes from -# the 60s-era electronics testing: "Turn it on and see if smoke -# comes out." +AS_AUTO_DEF = $(TOP)/auto.def # -smoketest: $(TESTPROGS) fuzzcheck$(TEXE) - ./testfixture$(TEXE) $(TOP)/test/main.test $(TESTOPTS) - -sqlite3_analyzer.c: sqlite3.c $(TOP)/src/tclsqlite.c $(TOP)/tool/spaceanal.tcl - echo "#define TCLSH 2" > $@ - echo "#define SQLITE_ENABLE_DBSTAT_VTAB 1" >> $@ - cat sqlite3.c $(TOP)/src/tclsqlite.c >> $@ - echo "static const char *tclsh_main_loop(void){" >> $@ - echo "static const char *zMainloop = " >> $@ - $(TCLSH_CMD) $(TOP)/tool/tostr.tcl $(TOP)/tool/spaceanal.tcl >> $@ - echo "; return zMainloop; }" >> $@ - -sqlite3_analyzer$(TEXE): sqlite3_analyzer.c - $(LTLINK) sqlite3_analyzer.c -o $@ $(LIBTCL) $(TLIBS) - -showdb$(TEXE): $(TOP)/tool/showdb.c sqlite3.lo - $(LTLINK) -o $@ $(TOP)/tool/showdb.c sqlite3.lo $(TLIBS) - -showstat4$(TEXE): $(TOP)/tool/showstat4.c sqlite3.lo - $(LTLINK) -o $@ $(TOP)/tool/showstat4.c sqlite3.lo $(TLIBS) - -showjournal$(TEXE): $(TOP)/tool/showjournal.c sqlite3.lo - $(LTLINK) -o $@ $(TOP)/tool/showjournal.c sqlite3.lo $(TLIBS) - -showwal$(TEXE): $(TOP)/tool/showwal.c sqlite3.lo - $(LTLINK) -o $@ $(TOP)/tool/showwal.c sqlite3.lo $(TLIBS) - -rollback-test$(TEXE): $(TOP)/tool/rollback-test.c sqlite3.lo - $(LTLINK) -o $@ $(TOP)/tool/rollback-test.c sqlite3.lo $(TLIBS) - -LogEst$(TEXE): $(TOP)/tool/logest.c sqlite3.h - $(LTLINK) -I. -o $@ $(TOP)/tool/logest.c - -wordcount$(TEXE): $(TOP)/test/wordcount.c sqlite3.c - $(LTLINK) -o $@ $(TOP)/test/wordcount.c sqlite3.c $(TLIBS) - -speedtest1$(TEXE): $(TOP)/test/speedtest1.c sqlite3.lo - $(LTLINK) -o $@ $(TOP)/test/speedtest1.c sqlite3.lo $(TLIBS) - -rbu$(EXE): $(TOP)/ext/rbu/rbu.c $(TOP)/ext/rbu/sqlite3rbu.c sqlite3.lo - $(LTLINK) -I. -o $@ $(TOP)/ext/rbu/rbu.c sqlite3.lo $(TLIBS) - -loadfts$(EXE): $(TOP)/tool/loadfts.c libsqlite3.la - $(LTLINK) $(TOP)/tool/loadfts.c libsqlite3.la -o $@ $(TLIBS) - -# This target will fail if the SQLite amalgamation contains any exported -# symbols that do not begin with "sqlite3_". It is run as part of the -# releasetest.tcl script. +# Shell commands to re-run $(TOP)/configure with the same args it was +# invoked with to produce this makefile. # -checksymbols: sqlite3.lo - nm -g --defined-only sqlite3.o | grep -v " sqlite3_" ; test $$? -ne 0 - echo '0 errors out of 1 tests' +AS_AUTORECONFIG = @SQLITE_AUTORECONFIG@ +.PHONY: config reconfigure +config reconfigure: + $(AS_AUTORECONFIG) +USE_AMALGAMATION ?= @USE_AMALGAMATION@ +LINK_TOOLS_DYNAMICALLY ?= @LINK_TOOLS_DYNAMICALLY@ +AMALGAMATION_GEN_FLAGS ?= --linemacros=@AMALGAMATION_LINE_MACROS@ +EXTRA_SRC ?= @AMALGAMATION_EXTRA_SRC@ +STATIC_TCLSQLITE3 = @STATIC_TCLSQLITE3@ +STATIC_CLI_SHELL = @STATIC_CLI_SHELL@ -# Build the amalgamation-autoconf package. The amalamgation-tarball target builds -# a tarball named for the version number. Ex: sqlite-autoconf-3110000.tar.gz. -# The snapshot-tarball target builds a tarball named by the SHA1 hash # -amalgamation-tarball: sqlite3.c - TOP=$(TOP) sh $(TOP)/tool/mkautoconfamal.sh --normal - -snapshot-tarball: sqlite3.c - TOP=$(TOP) sh $(TOP)/tool/mkautoconfamal.sh --snapshot - -# The next two rules are used to support the "threadtest" target. Building -# threadtest runs a few thread-safety tests that are implemented in C. This -# target is invoked by the releasetest.tcl script. -# -THREADTEST3_SRC = $(TOP)/test/threadtest3.c \ - $(TOP)/test/tt3_checkpoint.c \ - $(TOP)/test/tt3_index.c \ - $(TOP)/test/tt3_vacuum.c \ - $(TOP)/test/tt3_stress.c \ - $(TOP)/test/tt3_lookaside1.c - -threadtest3$(TEXE): sqlite3.lo $(THREADTEST3_SRC) - $(LTLINK) $(TOP)/test/threadtest3.c $(TOP)/src/test_multiplex.c sqlite3.lo -o $@ $(TLIBS) - -threadtest: threadtest3$(TEXE) - ./threadtest3$(TEXE) - -releasetest: - $(TCLSH_CMD) $(TOP)/test/releasetest.tcl - -# Standard install and cleanup targets +# CFLAGS for sqlite3$(T.exe) # -lib_install: libsqlcipher.la - $(INSTALL) -d $(DESTDIR)$(libdir) - $(LTINSTALL) libsqlcipher.la $(DESTDIR)$(libdir) - -install: sqlcipher$(TEXE) lib_install sqlite3.h sqlcipher.pc ${HAVE_TCL:1=tcl_install} - $(INSTALL) -d $(DESTDIR)$(bindir) - $(LTINSTALL) sqlcipher$(TEXE) $(DESTDIR)$(bindir) - $(INSTALL) -d $(DESTDIR)$(includedir) - $(INSTALL) -m 0644 sqlite3.h $(DESTDIR)$(includedir) - $(INSTALL) -m 0644 $(TOP)/src/sqlite3ext.h $(DESTDIR)$(includedir) - $(INSTALL) -d $(DESTDIR)$(pkgconfigdir) - $(INSTALL) -m 0644 sqlcipher.pc $(DESTDIR)$(pkgconfigdir) +SHELL_OPT ?= @OPT_SHELL@ -pkgIndex.tcl: - echo 'package ifneeded sqlite3 $(RELEASE) [list load $(TCLLIBDIR)/libtclsqlite3$(SHLIB_SUFFIX) sqlite3]' > $@ -tcl_install: lib_install libtclsqlite3.la pkgIndex.tcl - $(INSTALL) -d $(DESTDIR)$(TCLLIBDIR) - $(LTINSTALL) libtclsqlite3.la $(DESTDIR)$(TCLLIBDIR) - rm -f $(DESTDIR)$(TCLLIBDIR)/libtclsqlite3.la $(DESTDIR)$(TCLLIBDIR)/libtclsqlite3.a - $(INSTALL) -m 0644 pkgIndex.tcl $(DESTDIR)$(TCLLIBDIR) +Makefile: $(TOP)/Makefile.in $(AS_AUTO_DEF) + $(AS_AUTORECONFIG) + @touch $@ -clean: - rm -f *.lo *.la *.o sqlcipher$(TEXE) libsqlcipher.la - rm -f sqlite3.h opcodes.* - rm -rf .libs .deps - rm -f lemon$(BEXE) lempar.c parse.* sqlite*.tar.gz - rm -f mkkeywordhash$(BEXE) keywordhash.h - rm -f *.da *.bb *.bbg gmon.out - rm -rf quota2a quota2b quota2c - rm -rf tsrc .target_source - rm -f tclsqlcipher$(TEXE) - rm -f testfixture$(TEXE) test.db - rm -f LogEst$(TEXE) fts3view$(TEXE) rollback-test$(TEXE) showdb$(TEXE) - rm -f showjournal$(TEXE) showstat4$(TEXE) showwal$(TEXE) speedtest1$(TEXE) - rm -f wordcount$(TEXE) - rm -f sqlite3.dll sqlite3.lib sqlite3.exp sqlite3.def - rm -f sqlite3.c - rm -f sqlite3rc.h - rm -f shell.c sqlite3ext.h - rm -f sqlite3_analyzer$(TEXE) sqlite3_analyzer.c - rm -f sqlite-*-output.vsix - rm -f mptester mptester.exe - rm -f rbu rbu.exe - rm -f srcck1 srcck1.exe - rm -f fuzzershell fuzzershell.exe - rm -f fuzzcheck fuzzcheck.exe - rm -f sqldiff sqldiff.exe - rm -f fts5.* fts5parse.* +sqlite3.pc: $(TOP)/sqlite3.pc.in $(AS_AUTO_DEF) + $(AS_AUTORECONFIG) + @touch $@ +install: install-pc # defined in main.mk -distclean: clean - rm -f config.h config.log config.status libtool Makefile sqlcipher.pc +sqlite_cfg.h: $(AS_AUTO_DEF) + $(AS_AUTORECONFIG) + @touch $@ # -# Windows section +# Fiddle app +# +# EMCC_WRAPPER must refer to the genuine emcc binary, or a +# call-compatible wrapper, e.g. $(TOP)/tool/emcc.sh. If it's empty, +# build components requiring Emscripten will not build. +# +# Achtung: though _this_ makefile is POSIX-make compatible, the fiddle +# build requires GNU make. # -dll: sqlite3.dll +EMCC_WRAPPER = @EMCC_WRAPPER@ +fiddle: sqlite3.c shell.c + @if [ x = "x$(EMCC_WRAPPER)" ]; then \ + echo "Emscripten SDK not found by configure. Cannot build fiddle." 1&>2; \ + exit 1; \ + fi + $(MAKE) -C ext/wasm fiddle emcc_opt=-Os -REAL_LIBOBJ = $(LIBOBJ:%.lo=.libs/%.o) +# +# Spell-checking for source comments +# The sources checked are either C sources or C source templates. +# Their comments are extracted and processed through aspell using +# a custom dictionary that contains scads of odd identifiers that +# find their way into the comments. +# +# Currently, this target is setup to be "made" in-tree only. +# The output is ephemeral. Redirect it to guide spelling fixups, +# either to correct spelling or add words to tool/custom.txt. +# +./custom.rws: ./tool/custom.txt + @echo 'Updating custom dictionary from tool/custom.txt' + aspell --lang=en create master ./custom.rws < ./tool/custom.txt +misspell: ./custom.rws has_tclsh84 +# $(JIMSH) does not work with spellsift.tcl + $(TCLSH_CMD) ./tool/spellsift.tcl ./src/*.c ./src/*.h ./src/*.in -$(REAL_LIBOBJ): $(LIBOBJ) +# +# clean/distclean are mostly defined in main.mk. In this makefile we +# perform cleanup known to be relevant to (only) the autosetup-driven +# build. +# +distclean-autosetup: + rm -f sqlite_cfg.h config.log config.status config.defines.* Makefile sqlite3.pc + rm -f $(TOP)/tool/emcc.sh + rm -f libsqlite3*$(T.dll) + rm -f jimsh0* +distclean: distclean-autosetup -sqlite3.def: $(REAL_LIBOBJ) - echo 'EXPORTS' >sqlite3.def - nm $(REAL_LIBOBJ) | grep ' T ' | grep ' _sqlite3_' \ - | sed 's/^.* _//' >>sqlite3.def +# +# tool/version-info: a utility for emitting sqlite3 version info +# in various forms. It's used by ext/wasm/. +# +version-info$(T.exe): $(TOP)/tool/version-info.c Makefile sqlite3.h + $(T.link) $(ST_OPT) -o $@ $(TOP)/tool/version-info.c -sqlite3.dll: $(REAL_LIBOBJ) sqlite3.def - $(TCC) -shared -o $@ sqlite3.def \ - -Wl,"--strip-all" $(REAL_LIBOBJ) +include $(TOP)/main.mk diff --git a/Makefile.linux-gcc b/Makefile.linux-gcc deleted file mode 100644 index 1491a4b02a..0000000000 --- a/Makefile.linux-gcc +++ /dev/null @@ -1,123 +0,0 @@ -#!/usr/make -# -# Makefile for SQLITE -# -# This is a template makefile for SQLite. Most people prefer to -# use the autoconf generated "configure" script to generate the -# makefile automatically. But that does not work for everybody -# and in every situation. If you are having problems with the -# "configure" script, you might want to try this makefile as an -# alternative. Create a copy of this file, edit the parameters -# below and type "make". -# - -#### The toplevel directory of the source tree. This is the directory -# that contains this "Makefile.in" and the "configure.in" script. -# -TOP = ../sqlite - -#### C Compiler and options for use in building executables that -# will run on the platform that is doing the build. -# -BCC = gcc -g -O2 -#BCC = /opt/ancic/bin/c89 -0 - -#### If the target operating system supports the "usleep()" system -# call, then define the HAVE_USLEEP macro for all C modules. -# -#USLEEP = -USLEEP = -DHAVE_USLEEP=1 - -#### If you want the SQLite library to be safe for use within a -# multi-threaded program, then define the following macro -# appropriately: -# -#THREADSAFE = -DTHREADSAFE=1 -THREADSAFE = -DTHREADSAFE=0 - -#### Specify any extra linker options needed to make the library -# thread safe -# -#THREADLIB = -lpthread -THREADLIB = - -#### Specify any extra libraries needed to access required functions. -# -#TLIBS = -lrt # fdatasync on Solaris 8 -TLIBS = - -#### Leave SQLITE_DEBUG undefined for maximum speed. Use SQLITE_DEBUG=1 -# to check for memory leaks. Use SQLITE_DEBUG=2 to print a log of all -# malloc()s and free()s in order to track down memory leaks. -# -# SQLite uses some expensive assert() statements in the inner loop. -# You can make the library go almost twice as fast if you compile -# with -DNDEBUG=1 -# -#OPTS = -DSQLITE_DEBUG=2 -#OPTS = -DSQLITE_DEBUG=1 -#OPTS = -OPTS = -DNDEBUG=1 -OPTS += -DHAVE_FDATASYNC=1 - -#### The suffix to add to executable files. ".exe" for windows. -# Nothing for unix. -# -#EXE = .exe -EXE = - -#### C Compile and options for use in building executables that -# will run on the target platform. This is usually the same -# as BCC, unless you are cross-compiling. -# -TCC = gcc -O6 -#TCC = gcc -g -O0 -Wall -#TCC = gcc -g -O0 -Wall -fprofile-arcs -ftest-coverage -#TCC = /opt/mingw/bin/i386-mingw32-gcc -O6 -#TCC = /opt/ansic/bin/c89 -O +z -Wl,-a,archive - -#### Tools used to build a static library. -# -AR = ar cr -#AR = /opt/mingw/bin/i386-mingw32-ar cr -RANLIB = ranlib -#RANLIB = /opt/mingw/bin/i386-mingw32-ranlib - -MKSHLIB = gcc -shared -SO = so -SHPREFIX = lib -# SO = dll -# SHPREFIX = - -#### Extra compiler options needed for programs that use the TCL library. -# -#TCL_FLAGS = -#TCL_FLAGS = -DSTATIC_BUILD=1 -TCL_FLAGS = -I/home/drh/tcltk/8.5linux -#TCL_FLAGS = -I/home/drh/tcltk/8.5win -DSTATIC_BUILD=1 -#TCL_FLAGS = -I/home/drh/tcltk/8.3hpux - -#### Linker options needed to link against the TCL library. -# -#LIBTCL = -ltcl -lm -ldl -LIBTCL = /home/drh/tcltk/8.5linux/libtcl8.5g.a -lm -ldl -#LIBTCL = /home/drh/tcltk/8.5win/libtcl85s.a -lmsvcrt -#LIBTCL = /home/drh/tcltk/8.3hpux/libtcl8.3.a -ldld -lm -lc - -#### Additional objects for SQLite library when TCL support is enabled. -#TCLOBJ = -TCLOBJ = tclsqlite.o - -#### Compiler options needed for programs that use the readline() library. -# -READLINE_FLAGS = -#READLINE_FLAGS = -DHAVE_READLINE=1 -I/usr/include/readline - -#### Linker options needed by programs using readline() must link against. -# -LIBREADLINE = -#LIBREADLINE = -static -lreadline -ltermcap - -# You should not have to change anything below this line -############################################################################### -include $(TOP)/main.mk diff --git a/Makefile.linux-generic b/Makefile.linux-generic new file mode 100644 index 0000000000..c7441fa517 --- /dev/null +++ b/Makefile.linux-generic @@ -0,0 +1,64 @@ +#!/usr/make +all: +# +# Makefile for SQLITE +# +# This is a template makefile for SQLite. Most people prefer to +# use the autoconf generated "configure" script to generate the +# makefile automatically. But that does not work for everybody +# and in every situation. If you are having problems with the +# "configure" script, you might want to try this makefile as an +# alternative. Create a copy of this file, edit the parameters +# below and type "make". +# +# Maintenance note: because this is the template for Linux systems, it +# is assumed that the platform has GNU make and this file takes +# advantage of that. +# +#### +# +# $(TOP) = The toplevel directory of the source tree. This is the +# directory that contains "Makefile.in" and "auto.def". +# +TOP ?= $(realpath $(dir $(lastword $(MAKEFILE_LIST)))) + +# +# $(CFLAGS) will be used when compiling the library and most +# utilities. It must normally contain -fPIC on Linux systems, +# but overriding CFLAGS is an easy way for users to inadvertently +# remove -fPIC from their builds, so we generally expect to see +# -fPIC in $(CFLAGS.core), which main.mk will integrate with +# the CFLAGS where needed. +# +CFLAGS = +CFLAGS.core = -fPIC + +# +# $(SHELL_OPT) contains CFLAGS for building the sqlite3 CLI shell. +# See main.mk for other potentially-relevant vars which may need +# tweaking, like $(LDFLAGS_READLINE). +# +SHELL_OPT += -DHAVE_READLINE=1 +SHELL_OPT += -DSQLITE_HAVE_ZLIB=1 +LDFLAGS.readline = -lreadline # may need -lcurses etc, depending on the system +CFLAGS.readline = # needs -I... if readline.h is in an unusual place. +LDFLAGS.zlib = -lz + +# +# Library's version number. +# +PACKAGE_VERSION ?= $(shell cat $(TOP)/VERSION 2>/dev/null) + +# sqlite_cfg.h is typically created by the configure script. It's +# commonly not needed but main.mk does not know that so we have to +# create a dummy if we don't already have one. +sqlite_cfg.h: + touch $@ +distclean-.: + rm -f sqlite_cfg.h + +# +# With the above in place, we can now import the rules make use of +# it... +# +include $(TOP)/main.mk diff --git a/Makefile.msc b/Makefile.msc index 796aec4b06..d3fe57de68 100644 --- a/Makefile.msc +++ b/Makefile.msc @@ -18,10 +18,40 @@ USE_AMALGAMATION = 1 !ENDIF # <> +# Optionally set EXTRA_SRC to a list of C files to append to +# the generated sqlite3.c. Any sqlite3 extensions added this +# way may require manual editing, as described in +# https://sqlite.org/forum/forumpost/903f721f3e7c0d25 +# +!IFNDEF EXTRA_SRC +EXTRA_SRC = +!ENDIF + # Set this non-0 to enable full warnings (-W4, etc) when compiling. # !IFNDEF USE_FULLWARN -USE_FULLWARN = 0 +USE_FULLWARN = 1 +!ENDIF + +# Set this non-0 to enable treating warnings as errors (-WX, etc) when +# compiling. +# +!IFNDEF USE_FATAL_WARN +USE_FATAL_WARN = 0 +!ENDIF + +# Set this non-0 to enable full runtime error checks (-RTC1, etc). This +# has no effect if (any) optimizations are enabled. +# +!IFNDEF USE_RUNTIME_CHECKS +USE_RUNTIME_CHECKS = 0 +!ENDIF + +# Set this non-0 to create a SQLite amalgamation file that excludes the +# various built-in extensions. +# +!IFNDEF MINIMAL_AMALGAMATION +MINIMAL_AMALGAMATION = 0 !ENDIF # Set this non-0 to use "stdcall" calling convention for the core library @@ -31,6 +61,21 @@ USE_FULLWARN = 0 USE_STDCALL = 0 !ENDIF +# Use the USE_SEH=0 option on the nmake command line to omit structured +# exception handling (SEH) support. SEH is on by default. +# +!IFNDEF USE_SEH +USE_SEH = 1 +!ENDIF + +# Use STATICALLY_LINK_TCL=1 to statically link against TCL +# +!IFNDEF STATICALLY_LINK_TCL +STATICALLY_LINK_TCL = 0 +!ELSEIF $(STATICALLY_LINK_TCL)!=0 +CCOPTS = $(CCOPTS) -DSTATIC_BUILD +!ENDIF + # Set this non-0 to have the shell executable link against the core dynamic # link library. # @@ -52,7 +97,7 @@ API_ARMOR = 0 !IFNDEF NO_WARN !IF $(USE_FULLWARN)!=0 NO_WARN = -wd4054 -wd4055 -wd4100 -wd4127 -wd4130 -wd4152 -wd4189 -wd4206 -NO_WARN = $(NO_WARN) -wd4210 -wd4232 -wd4305 -wd4306 -wd4702 -wd4706 +NO_WARN = $(NO_WARN) -wd4210 -wd4232 -wd4244 -wd4305 -wd4306 -wd4702 -wd4706 !ENDIF !ENDIF @@ -71,6 +116,29 @@ SPLIT_AMALGAMATION = 0 !ENDIF # <> +# Set this non-0 to have this makefile assume the Tcl shell executable +# (tclsh*.exe) is available in the PATH. By default, this is disabled +# for compatibility with older build environments. This setting only +# applies if TCLSH_CMD is not set manually. +# +!IFNDEF USE_TCLSH_IN_PATH +USE_TCLSH_IN_PATH = 0 +!ENDIF + +# Set this non-0 to use zlib, possibly compiling it from source code. +# +!IFNDEF USE_ZLIB +USE_ZLIB = 0 +!ENDIF + +# Set this non-0 to build zlib from source code. This is enabled by +# default and in that case it will be assumed that the ZLIBDIR macro +# points to the top-level source code directory for zlib. +# +!IFNDEF BUILD_ZLIB +BUILD_ZLIB = 1 +!ENDIF + # Set this non-0 to use the International Components for Unicode (ICU). # !IFNDEF USE_ICU @@ -174,6 +242,12 @@ WIN32HEAP = 0 OSTRACE = 0 !ENDIF +# enable address sanitizer using ASAN=1 on the command-line. +# +!IFNDEF ASAN +ASAN = 0 +!ENDIF + # Set this to one of the following values to enable various debugging # features. Each level includes the debugging options from the previous # levels. Currently, the recognized values for DEBUG are: @@ -190,6 +264,16 @@ OSTRACE = 0 DEBUG = 0 !ENDIF +# <> +# By default, use --linemacros=1 argument to the mksqlite3c.tcl tool, which +# is used to build the amalgamation. This can be turned off to ease debug +# of the amalgamation away from the source tree. +# +!IFNDEF NO_LINEMACROS +NO_LINEMACROS = 0 +!ENDIF +# <> + # Enable use of available compiler optimizations? Normally, this should be # non-zero. Setting this to zero, thus disabling all compiler optimizations, # can be useful for testing. @@ -198,6 +282,24 @@ DEBUG = 0 OPTIMIZATIONS = 2 !ENDIF +# Set this to non-0 to enable support for the session extension. +# +!IFNDEF SESSION +SESSION = 0 +!ENDIF + +# Set this to non-0 to enable support for the rbu extension. +# +!IFNDEF RBU +RBU = 0 +!ENDIF + +# Set this to non-0 to enable support for blocking locks. +# +!IFNDEF SETLK_TIMEOUT +SETLK_TIMEOUT = 0 +!ENDIF + # Set the source code file to be used by executables and libraries when # they need the amalgamation. # @@ -219,37 +321,130 @@ SQLITE3H = sqlite3.h # This is the name to use for the SQLite dynamic link library (DLL). # !IFNDEF SQLITE3DLL +!IF $(FOR_WIN10)!=0 +SQLITE3DLL = winsqlite3.dll +!ELSE SQLITE3DLL = sqlite3.dll !ENDIF +!ENDIF # This is the name to use for the SQLite import library (LIB). # !IFNDEF SQLITE3LIB +!IF $(FOR_WIN10)!=0 +SQLITE3LIB = winsqlite3.lib +!ELSE SQLITE3LIB = sqlite3.lib !ENDIF +!ENDIF # This is the name to use for the SQLite shell executable (EXE). # !IFNDEF SQLITE3EXE +!IF $(FOR_WIN10)!=0 +SQLITE3EXE = winsqlite3shell.exe +!ELSE SQLITE3EXE = sqlite3.exe !ENDIF +!ENDIF # This is the argument used to set the program database (PDB) file for the # SQLite shell executable (EXE). # !IFNDEF SQLITE3EXEPDB +!IF $(FOR_WIN10)!=0 +SQLITE3EXEPDB = +!ELSE SQLITE3EXEPDB = /pdb:sqlite3sh.pdb !ENDIF +!ENDIF + +# <> +# These are the names of the customized Tcl header files used by various parts +# of this makefile when the stdcall calling convention is in use. It is not +# used for any other purpose. +# +!IFNDEF SQLITETCLH +SQLITETCLH = sqlite_tcl.h +!ENDIF + +!IFNDEF SQLITETCLDECLSH +SQLITETCLDECLSH = sqlite_tclDecls.h +!ENDIF + +# This is the name to use for the dynamic link library (DLL) containing the +# Tcl bindings for SQLite. +# +!IFNDEF SQLITE3TCLDLL +SQLITE3TCLDLL = tclsqlite3.dll +!ENDIF + +# These are the additional targets that the targets that integrate with the +# Tcl library should depend on when compiling, etc. +# +!IFNDEF SQLITE_TCL_DEP +!IF $(USE_STDCALL)!=0 || $(FOR_WIN10)!=0 +SQLITE_TCL_DEP = $(SQLITETCLDECLSH) $(SQLITETCLH) +!ELSE +SQLITE_TCL_DEP = +!ENDIF +!ENDIF +# <> # These are the "standard" SQLite compilation options used when compiling for # the Windows platform. # !IFNDEF OPT_FEATURE_FLAGS +OPT_FEATURE_FLAGS = $(OPT_XTRA) +!IF $(MINIMAL_AMALGAMATION)==0 OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_FTS3=1 +OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_FTS5=1 OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_RTREE=1 +OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_GEOPOLY=1 +OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_STMTVTAB=1 +OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_DBPAGE_VTAB=1 +OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_DBSTAT_VTAB=1 +OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_BYTECODE_VTAB=1 +OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_CARRAY=1 +!ENDIF OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_COLUMN_METADATA=1 !ENDIF +# Additional feature-options above and beyond what are normally used can be +# be added using OPTIONS=.... on the command-line. These values are +# appended to the OPT_FEATURE_FLAGS variable. +# +!IFDEF OPTIONS +OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) $(OPTIONS) +!ENDIF + +# Should the session extension be enabled? If so, add compilation options +# to enable it. +# +!IF $(SESSION)!=0 +OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_SESSION=1 +OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_PREUPDATE_HOOK=1 +!ENDIF + +# Always enable math functions on Windows +OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_MATH_FUNCTIONS +OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_PERCENTILE + +# Should the rbu extension be enabled? If so, add compilation options +# to enable it. +# +!IF $(RBU)!=0 +OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_RBU=1 +!ENDIF + +# Should structured exception handling (SEH) be enabled for WAL mode in +# the core library? It is on by default. Only omit it if the +# USE_SEH=0 option is provided on the nmake command-line. +# +!IF $(USE_SEH)==0 +OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_OMIT_SEH=1 +!ENDIF + # These are the "extended" SQLite compilation options used when compiling for # the Windows 10 platform. # @@ -263,6 +458,10 @@ EXT_FEATURE_FLAGS = !ENDIF !ENDIF +!IF $(SETLK_TIMEOUT)!=0 +OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_SETLK_TIMEOUT +!ENDIF + ############################################################################### ############################### END OF OPTIONS ################################ ############################################################################### @@ -290,6 +489,14 @@ PROGRAMFILES_X86 = $(PROGRAMFILES_X86:\\=\) CC = cl.exe !ENDIF +# Check for the predefined command macro CSC. This should point to a working +# C Sharp compiler binary. If it is not defined, simply define it to the +# legacy default value 'csc.exe'. +# +!IFNDEF CSC +CSC = csc.exe +!ENDIF + # Check for the command macro LD. This should point to the linker binary for # the target platform. If it is not defined, simply define it to the legacy # default value 'link.exe'. @@ -379,9 +586,9 @@ UCRTLIBPATH = $(UCRTLIBPATH:\\=\) # will run on the platform that is doing the build. # !IF $(USE_FULLWARN)!=0 -BCC = $(NCC) -nologo -W4 $(CCOPTS) $(BCCOPTS) +BCC = $(NCC) -nologo -W4 -Fd$*.pdb $(CCOPTS) $(BCCOPTS) !ELSE -BCC = $(NCC) -nologo -W3 $(CCOPTS) $(BCCOPTS) +BCC = $(NCC) -nologo -W3 -Fd$*.pdb $(CCOPTS) $(BCCOPTS) !ENDIF # Check if assembly code listings should be generated for the source @@ -414,18 +621,15 @@ TCC = $(CC) -nologo -W4 -DINCLUDE_MSVC_H=1 $(CCOPTS) $(TCCOPTS) TCC = $(CC) -nologo -W3 $(CCOPTS) $(TCCOPTS) !ENDIF -TCC = $(TCC) -DSQLITE_OS_WIN=1 -I. -I$(TOP) -I$(TOP)\src -fp:precise -RCC = $(RC) -DSQLITE_OS_WIN=1 -I. -I$(TOP) -I$(TOP)\src $(RCOPTS) $(RCCOPTS) - -# Adjust the names of the primary targets for use with Windows 10. +# Check if warnings should be treated as errors when compiling. # -!IF $(FOR_WIN10)!=0 -SQLITE3DLL = winsqlite3.dll -SQLITE3LIB = winsqlite3.lib -SQLITE3EXE = winsqlite3shell.exe -SQLITE3EXEPDB = +!IF $(USE_FATAL_WARN)!=0 +TCC = $(TCC) -WX !ENDIF +TCC = $(TCC) -DSQLITE_OS_WIN=1 -I. -I$(TOP) -I$(TOP)\src -fp:precise +RCC = $(RC) -DSQLITE_OS_WIN=1 -I. -I$(TOP) -I$(TOP)\src $(RCOPTS) $(RCCOPTS) + # Check if we want to use the "stdcall" calling convention when compiling. # This is not supported by the compilers for non-x86 platforms. It should # also be noted here that building any target with these "stdcall" options @@ -435,20 +639,32 @@ SQLITE3EXEPDB = # !IF $(USE_STDCALL)!=0 || $(FOR_WIN10)!=0 !IF "$(PLATFORM)"=="x86" -CORE_CCONV_OPTS = -Gz -DSQLITE_CDECL=__cdecl -DSQLITE_STDCALL=__stdcall -SHELL_CCONV_OPTS = -Gz -DSQLITE_CDECL=__cdecl -DSQLITE_STDCALL=__stdcall +CORE_CCONV_OPTS = -Gz -guard:cf -DSQLITE_CDECL=__cdecl -DSQLITE_APICALL=__stdcall -DSQLITE_CALLBACK=__stdcall -DSQLITE_SYSAPI=__stdcall +SHELL_CCONV_OPTS = -Gz -guard:cf -DSQLITE_CDECL=__cdecl -DSQLITE_APICALL=__stdcall -DSQLITE_CALLBACK=__stdcall -DSQLITE_SYSAPI=__stdcall +# <> +TEST_CCONV_OPTS = -Gz -guard:cf -DSQLITE_CDECL=__cdecl -DSQLITE_APICALL=__stdcall -DSQLITE_CALLBACK=__stdcall -DSQLITE_SYSAPI=__stdcall -DINCLUDE_SQLITE_TCL_H=1 -DSQLITE_TCLAPI=__cdecl +# <> !ELSE !IFNDEF PLATFORM -CORE_CCONV_OPTS = -Gz -DSQLITE_CDECL=__cdecl -DSQLITE_STDCALL=__stdcall -SHELL_CCONV_OPTS = -Gz -DSQLITE_CDECL=__cdecl -DSQLITE_STDCALL=__stdcall +CORE_CCONV_OPTS = -Gz -guard:cf -DSQLITE_CDECL=__cdecl -DSQLITE_APICALL=__stdcall -DSQLITE_CALLBACK=__stdcall -DSQLITE_SYSAPI=__stdcall +SHELL_CCONV_OPTS = -Gz -guard:cf -DSQLITE_CDECL=__cdecl -DSQLITE_APICALL=__stdcall -DSQLITE_CALLBACK=__stdcall -DSQLITE_SYSAPI=__stdcall +# <> +TEST_CCONV_OPTS = -Gz -guard:cf -DSQLITE_CDECL=__cdecl -DSQLITE_APICALL=__stdcall -DSQLITE_CALLBACK=__stdcall -DSQLITE_SYSAPI=__stdcall -DINCLUDE_SQLITE_TCL_H=1 -DSQLITE_TCLAPI=__cdecl +# <> !ELSE CORE_CCONV_OPTS = SHELL_CCONV_OPTS = +# <> +TEST_CCONV_OPTS = +# <> !ENDIF !ENDIF !ELSE CORE_CCONV_OPTS = SHELL_CCONV_OPTS = +# <> +TEST_CCONV_OPTS = +# <> !ENDIF # These are additional compiler options used for the core library. @@ -465,20 +681,24 @@ CORE_COMPILE_OPTS = $(CORE_CCONV_OPTS) # when linking. # !IFNDEF CORE_LINK_DEP -!IF $(DYNAMIC_SHELL)!=0 || $(FOR_WIN10)!=0 +!IF $(DYNAMIC_SHELL)!=0 CORE_LINK_DEP = -!ELSE +!ELSEIF $(FOR_WIN10)==0 || "$(PLATFORM)"=="x86" CORE_LINK_DEP = sqlite3.def +!ELSE +CORE_LINK_DEP = !ENDIF !ENDIF # These are additional linker options used for the core library. # !IFNDEF CORE_LINK_OPTS -!IF $(DYNAMIC_SHELL)!=0 || $(FOR_WIN10)!=0 +!IF $(DYNAMIC_SHELL)!=0 CORE_LINK_OPTS = -!ELSE +!ELSEIF $(FOR_WIN10)==0 || "$(PLATFORM)"=="x86" CORE_LINK_OPTS = /DEF:sqlite3.def +!ELSE +CORE_LINK_OPTS = !ENDIF !ENDIF @@ -498,6 +718,10 @@ SHELL_COMPILE_OPTS = $(SHELL_CCONV_OPTS) !IFNDEF SHELL_CORE_SRC !IF $(DYNAMIC_SHELL)!=0 || $(FOR_WIN10)!=0 SHELL_CORE_SRC = +# <> +!ELSEIF $(USE_AMALGAMATION)==0 +SHELL_CORE_SRC = +# <> !ELSE SHELL_CORE_SRC = $(SQLITE3C) !ENDIF @@ -508,16 +732,33 @@ SHELL_CORE_SRC = $(SQLITE3C) !IFNDEF SHELL_CORE_DEP !IF $(DYNAMIC_SHELL)!=0 || $(FOR_WIN10)!=0 SHELL_CORE_DEP = $(SQLITE3DLL) +# <> +!ELSEIF $(USE_AMALGAMATION)==0 +SHELL_CORE_DEP = libsqlite3.lib +# <> !ELSE SHELL_CORE_DEP = !ENDIF !ENDIF +# <> +# If zlib support is enabled, add the dependencies for it. +# +!IF $(USE_ZLIB)!=0 && $(BUILD_ZLIB)!=0 +SHELL_CORE_DEP = zlib $(SHELL_CORE_DEP) +TESTFIXTURE_DEP = zlib $(TESTFIXTURE_DEP) +!ENDIF +# <> + # This is the core library that the shell executable should link with. # !IFNDEF SHELL_CORE_LIB !IF $(DYNAMIC_SHELL)!=0 || $(FOR_WIN10)!=0 SHELL_CORE_LIB = $(SQLITE3LIB) +# <> +!ELSEIF $(USE_AMALGAMATION)==0 +SHELL_CORE_LIB = libsqlite3.lib +# <> !ELSE SHELL_CORE_LIB = !ENDIF @@ -564,17 +805,21 @@ BCC = $(BCC) /d2guard4 -D_ARM_WINAPI_PARTITION_DESKTOP_SDK_AVAILABLE !IF $(DEBUG)>1 TCC = $(TCC) -MDd BCC = $(BCC) -MDd +ZLIBCFLAGS = -nologo -MDd -W3 -O2 -Oy- -Zi !ELSE TCC = $(TCC) -MD BCC = $(BCC) -MD +ZLIBCFLAGS = -nologo -MD -W3 -O2 -Oy- -Zi !ENDIF !ELSE !IF $(DEBUG)>1 TCC = $(TCC) -MTd BCC = $(BCC) -MTd +ZLIBCFLAGS = -nologo -MTd -W3 -O2 -Oy- -Zi !ELSE TCC = $(TCC) -MT BCC = $(BCC) -MT +ZLIBCFLAGS = -nologo -MT -W3 -O2 -Oy- -Zi !ENDIF !ENDIF @@ -588,6 +833,8 @@ TCC = $(TCC) -I$(TOP)\ext\fts3 RCC = $(RCC) -I$(TOP)\ext\fts3 TCC = $(TCC) -I$(TOP)\ext\rtree RCC = $(RCC) -I$(TOP)\ext\rtree +TCC = $(TCC) -I$(TOP)\ext\session +RCC = $(RCC) -I$(TOP)\ext\session !ENDIF # The mksqlite3c.tcl script accepts some options on the command @@ -595,11 +842,34 @@ RCC = $(RCC) -I$(TOP)\ext\rtree # options are necessary in order to allow debugging symbols to # work correctly with Visual Studio when using the amalgamation. # +!IFNDEF MKSQLITE3C_TOOL +!IF $(MINIMAL_AMALGAMATION)!=0 +MKSQLITE3C_TOOL = $(TOP)\tool\mksqlite3c-noext.tcl +!ELSE +MKSQLITE3C_TOOL = $(TOP)\tool\mksqlite3c.tcl +!ENDIF +!ENDIF + !IFNDEF MKSQLITE3C_ARGS -!IF $(DEBUG)>1 -MKSQLITE3C_ARGS = --linemacros +!IF $(DEBUG)>1 && $(NO_LINEMACROS)==0 +MKSQLITE3C_ARGS = --linemacros=1 !ELSE -MKSQLITE3C_ARGS = +MKSQLITE3C_ARGS = --linemacros=0 +!ENDIF +!IF $(USE_STDCALL)!=0 || $(FOR_WIN10)!=0 +MKSQLITE3C_ARGS = $(MKSQLITE3C_ARGS) --useapicall +!ENDIF +!ENDIF + +# The mksqlite3h.tcl script accepts some options on the command line. +# When compiling with stdcall support, some of these options are +# necessary. +# +!IFNDEF MKSQLITE3H_ARGS +!IF $(USE_STDCALL)!=0 || $(FOR_WIN10)!=0 +MKSQLITE3H_ARGS = --useapicall +!ELSE +MKSQLITE3H_ARGS = !ENDIF !ENDIF # <> @@ -620,8 +890,12 @@ RCC = $(RCC) -DSQLITE_ENABLE_API_ARMOR=1 !ENDIF !IF $(DEBUG)>2 -TCC = $(TCC) -DSQLITE_DEBUG=1 +TCC = $(TCC) -DSQLITE_DEBUG=1 -DSQLITE_USE_W32_FOR_CONSOLE_IO RCC = $(RCC) -DSQLITE_DEBUG=1 +!IF $(DYNAMIC_SHELL)==0 +TCC = $(TCC) -DSQLITE_ENABLE_WHERETRACE -DSQLITE_ENABLE_SELECTTRACE +RCC = $(RCC) -DSQLITE_ENABLE_WHERETRACE -DSQLITE_ENABLE_SELECTTRACE +!ENDIF !ENDIF !IF $(DEBUG)>4 || $(OSTRACE)!=0 @@ -667,6 +941,13 @@ RCC = $(RCC) -DSQLITE_WIN32_MALLOC_VALIDATE=1 !ENDIF !ENDIF + +# Address sanitizer if ASAN=1 +# +!IF $(ASAN)>0 +TCC = $(TCC) /fsanitize=address +!ENDIF + # <> # The locations of the Tcl header and library files. Also, the library that # non-stubs enabled programs using Tcl must link against. These variables @@ -674,24 +955,89 @@ RCC = $(RCC) -DSQLITE_WIN32_MALLOC_VALIDATE=1 # prior to running nmake in order to match the actual installed location and # version on this machine. # +!IF $(STATICALLY_LINK_TCL)!=0 +TCLSUFFIX = s +!ENDIF +!IFNDEF TCLSUFFIX +TCLSUFFIX = +!ENDIF + +!IFNDEF TCLDIR +TCLDIR = C:\Tcl +!ENDIF + +!IFNDEF TCLVERSION +!IF EXISTS("$(TCLDIR)\lib\tcl90$(TCLSUFFIX).lib") +TCLVERSION = 90 +!ELSEIF EXISTS("$(TCLDIR)\lib\tcl86$(TCLSUFFIX).lib") +TCLVERSION = 86 +!ELSEIF EXISTS("$(TCLDIR)\lib\tcl86t.lib") +TCLSUFFIX = t +TCLVERSION = 86 +!ELSE +TCLVERSION = 90 +!ENDIF +!ENDIF + !IFNDEF TCLINCDIR -TCLINCDIR = c:\tcl\include +TCLINCDIR = $(TCLDIR)\include !ENDIF !IFNDEF TCLLIBDIR -TCLLIBDIR = c:\tcl\lib +TCLLIBDIR = $(TCLDIR)\lib !ENDIF !IFNDEF LIBTCL -LIBTCL = tcl85.lib +LIBTCL = tcl$(TCLVERSION)$(TCLSUFFIX).lib +!ENDIF + +!IFNDEF TCLLIBS +!IF $(STATICALLY_LINK_TCL)!=0 +TCLLIBS = /NODEFAULTLIB:libucrt.lib netapi32.lib user32.lib ucrt.lib +!ELSE +TCLLIBS = +!ENDIF !ENDIF !IFNDEF LIBTCLSTUB -LIBTCLSTUB = tclstub85.lib +!IF EXISTS("$(TCLLIBDIR)\tclstub$(TCLVERSION)$(TCLSUFFIX).lib") +LIBTCLSTUB = tclstub$(TCLVERSION)$(TCLSUFFIX).lib +!ELSEIF EXISTS("$(TCLLIBDIR)\tclstub$(TCLSUFFIX).lib") +LIBTCLSTUB = tclstub$(TCLSUFFIX).lib +!ELSEIF EXISTS("$(TCLLIBDIR)\tclstub$(TCLVERSION).lib") +LIBTCLSTUB = tclstub$(TCLVERSION).lib +!ELSE +LIBTCLSTUB = tclstub.lib +!ENDIF !ENDIF !IFNDEF LIBTCLPATH -LIBTCLPATH = c:\tcl\bin +LIBTCLPATH = $(TCLDIR)\bin +!ENDIF + +# The locations of the zlib header and library files. These variables +# (ZLIBINCDIR, ZLIBLIBDIR, and ZLIBLIB) may be overridden via the environment +# prior to running nmake in order to match the actual installed (or source +# code) location on this machine. +# +!IFNDEF ZLIBDIR +ZLIBDIR = $(TOP)\compat\zlib +!ENDIF + +!IFNDEF ZLIBINCDIR +ZLIBINCDIR = $(ZLIBDIR) +!ENDIF + +!IFNDEF ZLIBLIBDIR +ZLIBLIBDIR = $(ZLIBDIR) +!ENDIF + +!IFNDEF ZLIBLIB +!IF $(DYNAMIC_SHELL)!=0 +ZLIBLIB = zdll.lib +!ELSE +ZLIBLIB = zlib.lib +!ENDIF !ENDIF # The locations of the ICU header and library files. These variables @@ -699,12 +1045,16 @@ LIBTCLPATH = c:\tcl\bin # prior to running nmake in order to match the actual installed location on # this machine. # +!IFNDEF ICUDIR +ICUDIR = $(TOP)\compat\icu +!ENDIF + !IFNDEF ICUINCDIR -ICUINCDIR = c:\icu\include +ICUINCDIR = $(ICUDIR)\include !ENDIF !IFNDEF ICULIBDIR -ICULIBDIR = c:\icu\lib +ICULIBDIR = $(ICUDIR)\lib !ENDIF !IFNDEF LIBICU @@ -717,7 +1067,31 @@ LIBICU = icuuc.lib icuin.lib # specific Tcl shell to use. # !IFNDEF TCLSH_CMD -TCLSH_CMD = tclsh85 +!IF EXISTS("$(TCLDIR)\bin\tclsh$(TCLVERSION).exe") +TCLSH_CMD = $(TCLDIR)\bin\tclsh$(TCLVERSION).exe +!ELSEIF EXISTS("$(TCLDIR)\bin\tclsh90.exe") +TCLSH_CMD = $(TCLDIR)\bin\tclsh90.exe +!ELSEIF EXISTS("$(TCLDIR)\bin\tclsh86.exe") +TCLSH_CMD = $(TCLDIR)\bin\tclsh86.exe +!ELSEIF EXISTS("$(TCLDIR)\bin\tclsh86t.exe") +TCLSH_CMD = $(TCLDIR)\bin\tclsh86t.exe +!ELSEIF EXISTS("$(TCLDIR)\bin\tclsh.exe") +TCLSH_CMD = $(TCLDIR)\bin\tclsh.exe +!ELSE +TCLSH_CMD = tclsh +!ENDIF +!ENDIF + +# A light-weight TCLSH replacement that can be used for code generation +# but which is not adequate for testing. This is "jimsh0" by default, +# with source code in the repository. To force the whole build to use +# the full, official tclsh, add WITHOUT_JIMSH=1 to the nmake command line. +# +!IFDEF WITHOUT_JIMSH +JIM_TCLSH = $(TCLSH_CMD) +!ENDIF +!IFNDEF JIM_TCLSH +JIM_TCLSH = jimsh0.exe !ENDIF # <> @@ -749,15 +1123,6 @@ RCC = $(RCC) -DSQLITE_THREAD_OVERRIDE_LOCK=-1 TLIBS = !ENDIF -# Flags controlling use of the in memory btree implementation -# -# SQLITE_TEMP_STORE is 0 to force temporary tables to be in a file, 1 to -# default to file, 2 to default to memory, and 3 to force temporary -# tables to always be in memory. -# -TCC = $(TCC) -DSQLITE_TEMP_STORE=1 -RCC = $(RCC) -DSQLITE_TEMP_STORE=1 - # Enable/disable loadable extensions, and other optional features # based on configuration. (-DSQLITE_OMIT*, -DSQLITE_ENABLE*). # The same set of OMIT and ENABLE flags should be passed to the @@ -800,6 +1165,10 @@ RCC = $(RCC) -D_DEBUG !IF $(DEBUG)>1 || $(OPTIMIZATIONS)==0 TCC = $(TCC) -Od BCC = $(BCC) -Od +!IF $(USE_RUNTIME_CHECKS)!=0 +TCC = $(TCC) -RTC1 +BCC = $(BCC) -RTC1 +!ENDIF !ELSEIF $(OPTIMIZATIONS)>=3 TCC = $(TCC) -Ox BCC = $(BCC) -Ox @@ -819,6 +1188,15 @@ BCC = $(BCC) -Zi !ENDIF # <> +# If zlib support is enabled, add the compiler options for it. +# +!IF $(USE_ZLIB)!=0 +TCC = $(TCC) -DSQLITE_HAVE_ZLIB=1 +RCC = $(RCC) -DSQLITE_HAVE_ZLIB=1 +TCC = $(TCC) -I$(ZLIBINCDIR) +RCC = $(RCC) -I$(ZLIBINCDIR) +!ENDIF + # If ICU support is enabled, add the compiler options for it. # !IF $(USE_ICU)!=0 @@ -834,7 +1212,7 @@ RCC = $(RCC) -I$(ICUINCDIR) # Command line prefixes for compiling code, compiling resources, # linking, etc. # -LTCOMPILE = $(TCC) -Fo$@ +LTCOMPILE = $(TCC) -Fo$@ -Fd$*.pdb LTRCOMPILE = $(RCC) -r LTLIB = lib.exe LTLINK = $(TCC) -Fe$@ @@ -842,7 +1220,7 @@ LTLINK = $(TCC) -Fe$@ # If requested, link to the RPCRT4 library. # !IF $(USE_RPCRT4_LIB)!=0 -LTLINK = $(LTLINK) rpcrt4.lib +LTLIBS = $(LTLIBS) rpcrt4.lib !ENDIF # If a platform was set, force the linker to target that. @@ -850,10 +1228,15 @@ LTLINK = $(LTLINK) rpcrt4.lib # set this for you. Otherwise, the linker will attempt # to deduce the binary type based on the object files. !IFDEF PLATFORM -LTLINKOPTS = /NOLOGO /MACHINE:$(PLATFORM) +LTLINKOPTS = $(LTLINKOPTS) /NOLOGO /MACHINE:$(PLATFORM) LTLIBOPTS = /NOLOGO /MACHINE:$(PLATFORM) +!ELSEIF "$(VISUALSTUDIOVERSION)"=="12.0" || \ + "$(VISUALSTUDIOVERSION)"=="14.0" || \ + "$(VISUALSTUDIOVERSION)"=="15.0" +LTLINKOPTS = /NOLOGO /MACHINE:x86 +LTLIBOPTS = /NOLOGO /MACHINE:x86 !ELSE -LTLINKOPTS = /NOLOGO +LTLINKOPTS = $(LTLINKOPTS) /NOLOGO LTLIBOPTS = /NOLOGO !ENDIF @@ -939,8 +1322,15 @@ LDFLAGS = $(LDOPTS) # Start with the Tcl related linker options. # !IF $(NO_TCL)==0 -LTLIBPATHS = /LIBPATH:$(TCLLIBDIR) -LTLIBS = $(LIBTCL) +TCLLIBPATHS = $(TCLLIBPATHS) /LIBPATH:$(TCLLIBDIR) +TCLLIBS = $(TCLLIBS) $(LIBTCL) +!ENDIF + +# If zlib support is enabled, add the linker options for it. +# +!IF $(USE_ZLIB)!=0 +LTLIBPATHS = $(LTLIBPATHS) /LIBPATH:$(ZLIBLIBDIR) +LTLIBS = $(LTLIBS) $(ZLIBLIB) !ENDIF # If ICU support is enabled, add the linker options for it. @@ -959,25 +1349,28 @@ LTLIBS = $(LTLIBS) $(LIBICU) # LIBOBJS0 = vdbe.lo parse.lo alter.lo analyze.lo attach.lo auth.lo \ backup.lo bitvec.lo btmutex.lo btree.lo build.lo \ - callback.lo complete.lo ctime.lo date.lo dbstat.lo delete.lo \ + callback.lo carray.lo complete.lo ctime.lo \ + date.lo dbpage.lo dbstat.lo delete.lo \ expr.lo fault.lo fkey.lo \ fts3.lo fts3_aux.lo fts3_expr.lo fts3_hash.lo fts3_icu.lo \ fts3_porter.lo fts3_snippet.lo fts3_tokenizer.lo fts3_tokenizer1.lo \ fts3_tokenize_vtab.lo fts3_unicode.lo fts3_unicode2.lo fts3_write.lo \ fts5.lo \ func.lo global.lo hash.lo \ - icu.lo insert.lo journal.lo legacy.lo loadext.lo \ + icu.lo insert.lo json.lo legacy.lo loadext.lo \ main.lo malloc.lo mem0.lo mem1.lo mem2.lo mem3.lo mem5.lo \ - memjournal.lo \ + memdb.lo memjournal.lo \ mutex.lo mutex_noop.lo mutex_unix.lo mutex_w32.lo \ - notify.lo opcodes.lo os.lo os_unix.lo os_win.lo \ + notify.lo opcodes.lo os.lo os_kv.lo os_unix.lo os_win.lo \ pager.lo pcache.lo pcache1.lo pragma.lo prepare.lo printf.lo \ - random.lo resolve.lo rowset.lo rtree.lo select.lo sqlite3rbu.lo status.lo \ + random.lo resolve.lo rowset.lo rtree.lo \ + sqlite3session.lo select.lo sqlite3rbu.lo status.lo stmt.lo \ table.lo threads.lo tokenize.lo treeview.lo trigger.lo \ - update.lo util.lo vacuum.lo \ + update.lo upsert.lo util.lo vacuum.lo \ vdbeapi.lo vdbeaux.lo vdbeblob.lo vdbemem.lo vdbesort.lo \ - vdbetrace.lo wal.lo walker.lo where.lo wherecode.lo whereexpr.lo \ - utf.lo vtab.lo + vdbetrace.lo vdbevtab.lo wal.lo walker.lo where.lo wherecode.lo \ + whereexpr.lo \ + window.lo utf.lo vtab.lo # <> # Object files for the amalgamation. @@ -1008,12 +1401,9 @@ LIBRESOBJS = # Core source code files, part 1. # SRC00 = \ - $(TOP)\src\crypto.c \ + $(TOP)\src\sqlcipher.c \ $(TOP)\src\crypto_cc.c \ - $(TOP)\src\crypto_impl.c \ - $(TOP)\src\crypto_libtomcrypt.c \ $(TOP)\src\crypto_openssl.c \ - $(TOP)\src\crypto.h \ $(TOP)\src\sqlcipher.h \ $(TOP)\src\alter.c \ $(TOP)\src\analyze.c \ @@ -1025,9 +1415,11 @@ SRC00 = \ $(TOP)\src\btree.c \ $(TOP)\src\build.c \ $(TOP)\src\callback.c \ + $(TOP)\src\carray.c \ $(TOP)\src\complete.c \ - $(TOP)\src\ctime.c \ + ctime.c \ $(TOP)\src\date.c \ + $(TOP)\src\dbpage.c \ $(TOP)\src\dbstat.c \ $(TOP)\src\delete.c \ $(TOP)\src\expr.c \ @@ -1037,7 +1429,7 @@ SRC00 = \ $(TOP)\src\global.c \ $(TOP)\src\hash.c \ $(TOP)\src\insert.c \ - $(TOP)\src\journal.c \ + $(TOP)\src\json.c \ $(TOP)\src\legacy.c \ $(TOP)\src\loadext.c \ $(TOP)\src\main.c \ @@ -1047,6 +1439,7 @@ SRC00 = \ $(TOP)\src\mem2.c \ $(TOP)\src\mem3.c \ $(TOP)\src\mem5.c \ + $(TOP)\src\memdb.c \ $(TOP)\src\memjournal.c \ $(TOP)\src\mutex.c \ $(TOP)\src\mutex_noop.c \ @@ -1054,6 +1447,7 @@ SRC00 = \ $(TOP)\src\mutex_w32.c \ $(TOP)\src\notify.c \ $(TOP)\src\os.c \ + $(TOP)\src\os_kv.c \ $(TOP)\src\os_unix.c \ $(TOP)\src\os_win.c @@ -1079,6 +1473,7 @@ SRC01 = \ $(TOP)\src\trigger.c \ $(TOP)\src\utf.c \ $(TOP)\src\update.c \ + $(TOP)\src\upsert.c \ $(TOP)\src\util.c \ $(TOP)\src\vacuum.c \ $(TOP)\src\vdbe.c \ @@ -1088,17 +1483,14 @@ SRC01 = \ $(TOP)\src\vdbemem.c \ $(TOP)\src\vdbesort.c \ $(TOP)\src\vdbetrace.c \ + $(TOP)\src\vdbevtab.c \ $(TOP)\src\vtab.c \ $(TOP)\src\wal.c \ $(TOP)\src\walker.c \ $(TOP)\src\where.c \ $(TOP)\src\wherecode.c \ - $(TOP)\src\whereexpr.c - -# Shell source code files. -# -SRC02 = \ - $(TOP)\src\shell.c + $(TOP)\src\whereexpr.c \ + $(TOP)\src\window.c # Core miscellaneous files. # @@ -1124,7 +1516,7 @@ SRC04 = \ SRC05 = \ $(TOP)\src\pager.h \ $(TOP)\src\pcache.h \ - $(TOP)\src\pragma.h \ + pragma.h \ $(TOP)\src\sqlite.h.in \ $(TOP)\src\sqlite3ext.h \ $(TOP)\src\sqliteInt.h \ @@ -1135,20 +1527,6 @@ SRC05 = \ $(TOP)\src\wal.h \ $(TOP)\src\whereInt.h -# Extension source code files, part 1. -# -SRC06 = \ - $(TOP)\ext\fts1\fts1.c \ - $(TOP)\ext\fts1\fts1_hash.c \ - $(TOP)\ext\fts1\fts1_porter.c \ - $(TOP)\ext\fts1\fts1_tokenizer1.c \ - $(TOP)\ext\fts2\fts2.c \ - $(TOP)\ext\fts2\fts2_hash.c \ - $(TOP)\ext\fts2\fts2_icu.c \ - $(TOP)\ext\fts2\fts2_porter.c \ - $(TOP)\ext\fts2\fts2_tokenizer.c \ - $(TOP)\ext\fts2\fts2_tokenizer1.c - # Extension source code files, part 2. # SRC07 = \ @@ -1167,18 +1545,9 @@ SRC07 = \ $(TOP)\ext\fts3\fts3_write.c \ $(TOP)\ext\icu\icu.c \ $(TOP)\ext\rtree\rtree.c \ + $(TOP)\ext\session\sqlite3session.c \ $(TOP)\ext\rbu\sqlite3rbu.c \ - $(TOP)\ext\misc\json1.c - -# Extension header files, part 1. -# -SRC08 = \ - $(TOP)\ext\fts1\fts1.h \ - $(TOP)\ext\fts1\fts1_hash.h \ - $(TOP)\ext\fts1\fts1_tokenizer.h \ - $(TOP)\ext\fts2\fts2.h \ - $(TOP)\ext\fts2\fts2_hash.h \ - $(TOP)\ext\fts2\fts2_tokenizer.h + $(TOP)\ext\misc\stmt.c # Extension header files, part 2. # @@ -1189,7 +1558,9 @@ SRC09 = \ $(TOP)\ext\fts3\fts3_tokenizer.h \ $(TOP)\ext\icu\sqliteicu.h \ $(TOP)\ext\rtree\rtree.h \ - $(TOP)\ext\rbu\sqlite3rbu.h + $(TOP)\ext\rtree\geopoly.c \ + $(TOP)\ext\rbu\sqlite3rbu.h \ + $(TOP)\ext\session\sqlite3session.h # Generated source code files # @@ -1203,11 +1574,22 @@ SRC11 = \ keywordhash.h \ opcodes.h \ parse.h \ + shell.c \ $(SQLITE3H) +# Generated Tcl header files +# +!IF $(USE_STDCALL)!=0 || $(FOR_WIN10)!=0 +SRC12 = \ + $(SQLITETCLH) \ + $(SQLITETCLDECLSH) +!ELSE +SRC12 = +!ENDIF + # All source code files. # -SRC = $(SRC00) $(SRC01) $(SRC02) $(SRC03) $(SRC04) $(SRC05) $(SRC06) $(SRC07) $(SRC08) $(SRC09) $(SRC10) $(SRC11) +SRC = $(SRC00) $(SRC01) $(SRC03) $(SRC04) $(SRC05) $(SRC07) $(SRC09) $(SRC10) $(SRC11) $(SRC12) # Source code to the test files. # @@ -1218,15 +1600,15 @@ TESTSRC = \ $(TOP)\src\test4.c \ $(TOP)\src\test5.c \ $(TOP)\src\test6.c \ - $(TOP)\src\test7.c \ $(TOP)\src\test8.c \ $(TOP)\src\test9.c \ $(TOP)\src\test_autoext.c \ - $(TOP)\src\test_async.c \ $(TOP)\src\test_backup.c \ + $(TOP)\src\test_bestindex.c \ $(TOP)\src\test_blob.c \ $(TOP)\src\test_btree.c \ $(TOP)\src\test_config.c \ + $(TOP)\src\test_delete.c \ $(TOP)\src\test_demovfs.c \ $(TOP)\src\test_devsym.c \ $(TOP)\src\test_fs.c \ @@ -1236,6 +1618,7 @@ TESTSRC = \ $(TOP)\src\test_intarray.c \ $(TOP)\src\test_journal.c \ $(TOP)\src\test_malloc.c \ + $(TOP)\src\test_md5.c \ $(TOP)\src\test_multiplex.c \ $(TOP)\src\test_mutex.c \ $(TOP)\src\test_onefile.c \ @@ -1244,47 +1627,76 @@ TESTSRC = \ $(TOP)\src\test_quota.c \ $(TOP)\src\test_rtree.c \ $(TOP)\src\test_schema.c \ - $(TOP)\src\test_server.c \ $(TOP)\src\test_superlock.c \ $(TOP)\src\test_syscall.c \ + $(TOP)\src\test_tclsh.c \ $(TOP)\src\test_tclvar.c \ $(TOP)\src\test_thread.c \ + $(TOP)\src\test_vdbecov.c \ $(TOP)\src\test_vfs.c \ - $(TOP)\src\test_windirent.c \ + $(TOP)\src\test_window.c \ $(TOP)\src\test_wsd.c \ $(TOP)\ext\fts3\fts3_term.c \ $(TOP)\ext\fts3\fts3_test.c \ - $(TOP)\ext\rbu\test_rbu.c + $(TOP)\ext\rbu\test_rbu.c \ + $(TOP)\ext\session\test_session.c # Statically linked extensions. # TESTEXT = \ + $(TOP)\ext\expert\sqlite3expert.c \ + $(TOP)\ext\expert\test_expert.c \ $(TOP)\ext\misc\amatch.c \ + $(TOP)\ext\misc\appendvfs.c \ + $(TOP)\ext\misc\basexx.c \ + $(TOP)\ext\misc\cksumvfs.c \ $(TOP)\ext\misc\closure.c \ + $(TOP)\ext\misc\csv.c \ + $(TOP)\ext\misc\decimal.c \ $(TOP)\ext\misc\eval.c \ + $(TOP)\ext\misc\explain.c \ $(TOP)\ext\misc\fileio.c \ $(TOP)\ext\misc\fuzzer.c \ $(TOP)\ext\fts5\fts5_tcl.c \ $(TOP)\ext\fts5\fts5_test_mi.c \ $(TOP)\ext\fts5\fts5_test_tok.c \ $(TOP)\ext\misc\ieee754.c \ + $(TOP)\ext\misc\mmapwarm.c \ $(TOP)\ext\misc\nextchar.c \ - $(TOP)\ext\misc\percentile.c \ + $(TOP)\ext\misc\normalize.c \ + $(TOP)\ext\misc\prefixes.c \ + $(TOP)\ext\misc\qpvtab.c \ + $(TOP)\ext\misc\randomjson.c \ $(TOP)\ext\misc\regexp.c \ + $(TOP)\ext\misc\remember.c \ $(TOP)\ext\misc\series.c \ $(TOP)\ext\misc\spellfix.c \ + $(TOP)\ext\misc\stmtrand.c \ $(TOP)\ext\misc\totype.c \ - $(TOP)\ext\misc\wholenumber.c + $(TOP)\ext\misc\unionvtab.c \ + $(TOP)\ext\misc\wholenumber.c \ + $(TOP)\ext\rtree\test_rtreedoc.c \ + $(TOP)\ext\recover\sqlite3recover.c \ + $(TOP)\ext\recover\test_recover.c \ + $(TOP)\ext\intck\test_intck.c \ + $(TOP)\ext\intck\sqlite3intck.c \ + $(TOP)\ext\recover\dbdata.c + +# If use of zlib is enabled, add the "zipfile.c" source file. +# +!IF $(USE_ZLIB)!=0 +TESTEXT = $(TESTEXT) $(TOP)\ext\misc\zipfile.c +!ENDIF # Source code to the library files needed by the test fixture +# (non-amalgamation) # TESTSRC2 = \ $(SRC00) \ $(SRC01) \ - $(SRC06) \ $(SRC07) \ $(SRC10) \ - $(TOP)\ext\async\sqlite3async.c + fts5.c # Header files used by all library source files. # @@ -1304,9 +1716,9 @@ HDR = \ $(TOP)\src\pager.h \ $(TOP)\src\pcache.h \ parse.h \ - $(TOP)\src\pragma.h \ + pragma.h \ $(SQLITE3H) \ - $(TOP)\src\sqlite3ext.h \ + sqlite3ext.h \ $(TOP)\src\sqliteInt.h \ $(TOP)\src\sqliteLimit.h \ $(TOP)\src\vdbe.h \ @@ -1316,25 +1728,20 @@ HDR = \ # Header files used by extensions # -EXTHDR = $(EXTHDR) \ - $(TOP)\ext\fts1\fts1.h \ - $(TOP)\ext\fts1\fts1_hash.h \ - $(TOP)\ext\fts1\fts1_tokenizer.h -EXTHDR = $(EXTHDR) \ - $(TOP)\ext\fts2\fts2.h \ - $(TOP)\ext\fts2\fts2_hash.h \ - $(TOP)\ext\fts2\fts2_tokenizer.h EXTHDR = $(EXTHDR) \ $(TOP)\ext\fts3\fts3.h \ $(TOP)\ext\fts3\fts3Int.h \ $(TOP)\ext\fts3\fts3_hash.h \ $(TOP)\ext\fts3\fts3_tokenizer.h EXTHDR = $(EXTHDR) \ - $(TOP)\ext\rtree\rtree.h + $(TOP)\ext\rtree\rtree.h \ + $(TOP)\ext\rtree\geopoly.c EXTHDR = $(EXTHDR) \ $(TOP)\ext\icu\sqliteicu.h EXTHDR = $(EXTHDR) \ $(TOP)\ext\rtree\sqlite3rtree.h +EXTHDR = $(EXTHDR) \ + $(TOP)\ext\session\sqlite3session.h # executables needed for testing # @@ -1342,7 +1749,10 @@ TESTPROGS = \ testfixture.exe \ $(SQLITE3EXE) \ sqlite3_analyzer.exe \ - sqldiff.exe + sqlite3_checker.exe \ + sqldiff.exe \ + dbhash.exe \ + sqltclsh.exe # Databases containing fuzzer test cases # @@ -1350,22 +1760,87 @@ FUZZDATA = \ $(TOP)\test\fuzzdata1.db \ $(TOP)\test\fuzzdata2.db \ $(TOP)\test\fuzzdata3.db \ - $(TOP)\test\fuzzdata4.db + $(TOP)\test\fuzzdata4.db \ + $(TOP)\test\fuzzdata5.db \ + $(TOP)\test\fuzzdata6.db \ + $(TOP)\test\fuzzdata7.db \ + $(TOP)\test\fuzzdata8.db # <> # Additional compiler options for the shell. These are only effective # when the shell is not being dynamically linked. # !IF $(DYNAMIC_SHELL)==0 && $(FOR_WIN10)==0 -SHELL_COMPILE_OPTS = $(SHELL_COMPILE_OPTS) -DSQLITE_SHELL_JSON1 -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_EXPLAIN_COMMENTS +SHELL_COMPILE_OPTS = $(SHELL_COMPILE_OPTS) -DSQLITE_DQS=0 +SHELL_COMPILE_OPTS = $(SHELL_COMPILE_OPTS) -DSQLITE_ENABLE_FTS4=1 +SHELL_COMPILE_OPTS = $(SHELL_COMPILE_OPTS) -DSQLITE_ENABLE_EXPLAIN_COMMENTS=1 +SHELL_COMPILE_OPTS = $(SHELL_COMPILE_OPTS) -DSQLITE_ENABLE_OFFSET_SQL_FUNC=1 +SHELL_COMPILE_OPTS = $(SHELL_COMPILE_OPTS) -DSQLITE_ENABLE_PERCENTILE=1 +SHELL_COMPILE_OPTS = $(SHELL_COMPILE_OPTS) -DSQLITE_ENABLE_UNKNOWN_SQL_FUNCTION=1 +SHELL_COMPILE_OPTS = $(SHELL_COMPILE_OPTS) -DSQLITE_ENABLE_STMT_SCANSTATUS=1 +SHELL_COMPILE_OPTS = $(SHELL_COMPILE_OPTS) -DSQLITE_STRICT_SUBTYPE=1 !ENDIF # <> # Extra compiler options for various test tools. # -MPTESTER_COMPILE_OPTS = -DSQLITE_SHELL_JSON1 -DSQLITE_ENABLE_FTS5 -FUZZERSHELL_COMPILE_OPTS = -DSQLITE_ENABLE_JSON1 -FUZZCHECK_COMPILE_OPTS = -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_MEMSYS5 +MPTESTER_COMPILE_OPTS = -DSQLITE_ENABLE_FTS5 +FUZZERSHELL_COMPILE_OPTS = +FUZZCHECK_OPTS = $(FUZZCHECK_OPTS) -I$(TOP)\test -I$(TOP)\ext\recover +FUZZCHECK_OPTS = $(FUZZCHECK_OPTS) -DSQLITE_ENABLE_MEMSYS5 +FUZZCHECK_OPTS = $(FUZZCHECK_OPTS) -DSQLITE_OSS_FUZZ +FUZZCHECK_OPTS = $(FUZZCHECK_OPTS) -DSQLITE_ENABLE_BYTECODE_VTAB +FUZZCHECK_OPTS = $(FUZZCHECK_OPTS) -DSQLITE_ENABLE_CARRAY +FUZZCHECK_OPTS = $(FUZZCHECK_OPTS) -DSQLITE_ENABLE_DBPAGE_VTAB +FUZZCHECK_OPTS = $(FUZZCHECK_OPTS) -DSQLITE_ENABLE_DBSTAT_VTAB +FUZZCHECK_OPTS = $(FUZZCHECK_OPTS) -DSQLITE_ENABLE_BYTECODE_VTAB +FUZZCHECK_OPTS = $(FUZZCHECK_OPTS) -DSQLITE_ENABLE_DESERIALIZE +FUZZCHECK_OPTS = $(FUZZCHECK_OPTS) -DSQLITE_ENABLE_EXPLAIN_COMMENTS +FUZZCHECK_OPTS = $(FUZZCHECK_OPTS) -DSQLITE_ENABLE_FTS3_PARENTHESIS +FUZZCHECK_OPTS = $(FUZZCHECK_OPTS) -DSQLITE_ENABLE_FTS4 +FUZZCHECK_OPTS = $(FUZZCHECK_OPTS) -DSQLITE_ENABLE_FTS5 +FUZZCHECK_OPTS = $(FUZZCHECK_OPTS) -DSQLITE_ENABLE_GEOPOLY +FUZZCHECK_OPTS = $(FUZZCHECK_OPTS) -DSQLITE_ENABLE_MATH_FUNCTIONS +FUZZCHECK_OPTS = $(FUZZCHECK_OPTS) -DSQLITE_ENABLE_MEMSYS5 +FUZZCHECK_OPTS = $(FUZZCHECK_OPTS) -DSQLITE_ENABLE_NORMALIZE +FUZZCHECK_OPTS = $(FUZZCHECK_OPTS) -DSQLITE_ENABLE_OFFSET_SQL_FUNC +FUZZCHECK_OPTS = $(FUZZCHECK_OPTS) -DSQLITE_ENABLE_PERCENTILE +FUZZCHECK_OPTS = $(FUZZCHECK_OPTS) -DSQLITE_ENABLE_PREUPDATE_HOOK +FUZZCHECK_OPTS = $(FUZZCHECK_OPTS) -DSQLITE_ENABLE_RTREE +FUZZCHECK_OPTS = $(FUZZCHECK_OPTS) -DSQLITE_ENABLE_SESSION +FUZZCHECK_OPTS = $(FUZZCHECK_OPTS) -DSQLITE_ENABLE_STMTVTAB +FUZZCHECK_OPTS = $(FUZZCHECK_OPTS) -DSQLITE_ENABLE_UNKNOWN_SQL_FUNCTION +FUZZCHECK_OPTS = $(FUZZCHECK_OPTS) -DSQLITE_ENABLE_STAT4 +FUZZCHECK_OPTS = $(FUZZCHECK_OPTS) -DSQLITE_ENABLE_STMT_SCANSTATUS +FUZZCHECK_OPTS = $(FUZZCHECK_OPTS) -DSQLITE_MAX_MEMORY=50000000 +FUZZCHECK_OPTS = $(FUZZCHECK_OPTS) -DSQLITE_MAX_MMAP_SIZE=0 +FUZZCHECK_OPTS = $(FUZZCHECK_OPTS) -DSQLITE_OMIT_LOAD_EXTENSION +FUZZCHECK_OPTS = $(FUZZCHECK_OPTS) -DSQLITE_PRINTF_PRECISION_LIMIT=1000 +FUZZCHECK_OPTS = $(FUZZCHECK_OPTS) -DSQLITE_PRIVATE="" +FUZZCHECK_OPTS = $(FUZZCHECK_OPTS) -DSQLITE_STRICT_SUBTYPE=1 +FUZZCHECK_OPTS = $(FUZZCHECK_OPTS) -DSQLITE_STATIC_RANDOMJSON + +FUZZCHECK_OPTS = $(FUZZCHECK_OPTS) -DSQLITE_MAX_MEMORY=50000000 +FUZZCHECK_OPTS = $(FUZZCHECK_OPTS) -DSQLITE_PRINTF_PRECISION_LIMIT=1000 +FUZZCHECK_OPTS = $(FUZZCHECK_OPTS) -DSQLITE_OMIT_LOAD_EXTENSION +FUZZCHECK_OPTS = $(FUZZCHECK_OPTS) -DSQLITE_ENABLE_FTS4 +FUZZCHECK_OPTS = $(FUZZCHECK_OPTS) -DSQLITE_ENABLE_FTS5 +FUZZCHECK_OPTS = $(FUZZCHECK_OPTS) -DSQLITE_ENABLE_RTREE +FUZZCHECK_OPTS = $(FUZZCHECK_OPTS) -DSQLITE_ENABLE_GEOPOLY +FUZZCHECK_OPTS = $(FUZZCHECK_OPTS) -DSQLITE_ENABLE_DBSTAT_VTAB +FUZZCHECK_OPTS = $(FUZZCHECK_OPTS) -DSQLITE_ENABLE_BYTECODE_VTAB +FUZZCHECK_SRC = $(FUZZCHECK_SRC) $(TOP)\test\fuzzcheck.c +FUZZCHECK_SRC = $(FUZZCHECK_SRC) $(TOP)\test\ossfuzz.c +FUZZCHECK_SRC = $(FUZZCHECK_SRC) $(TOP)\test\fuzzinvariants.c +FUZZCHECK_SRC = $(FUZZCHECK_SRC) $(TOP)\test\vt02.c +FUZZCHECK_SRC = $(FUZZCHECK_SRC) $(TOP)\ext\recover\dbdata.c +FUZZCHECK_SRC = $(FUZZCHECK_SRC) $(TOP)\ext\recover\sqlite3recover.c +FUZZCHECK_SRC = $(FUZZCHECK_SRC) $(TOP)\ext\misc\randomjson.c + +OSSSHELL_SRC = $(TOP)\test\ossshell.c $(TOP)\test\ossfuzz.c +DBFUZZ_COMPILE_OPTS = -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION +KV_COMPILE_OPTS = -DSQLITE_THREADSAFE=0 -DSQLITE_DIRECT_OVERFLOW_READ +ST_COMPILE_OPTS = -DSQLITE_THREADSAFE=0 # Standard options to testfixture. # @@ -1374,7 +1849,7 @@ TESTOPTS = --verbose=file --output=test-out.txt # Extra targets for the "all" target that require Tcl. # !IF $(NO_TCL)==0 -ALL_TCL_TARGETS = libtclsqlite3.lib +ALL_TCL_TARGETS = $(SQLITE3TCLDLL) !ELSE ALL_TCL_TARGETS = !ENDIF @@ -1383,54 +1858,135 @@ ALL_TCL_TARGETS = # This is the default Makefile target. The objects listed here # are what get build when you type just "make" with no arguments. # -all: dll libsqlite3.lib shell $(ALL_TCL_TARGETS) +core: dll libsqlite3.lib shell + +# Targets that require the Tcl library. +# +tcl: $(ALL_TCL_TARGETS) + +# This Makefile target builds all of the standard binaries. +# +all: core tcl # Dynamic link library section. # -dll: $(SQLITE3DLL) +dll: $(SQLITE3DLL) # Shell executable. # -shell: $(SQLITE3EXE) +shell: $(SQLITE3EXE) + +# jimsh0 - replacement for tclsh +# +jimsh0.exe: $(TOP)\autosetup\jimsh0.c + cl -DHAVE__FULLPATH=1 $(TOP)\autosetup\jimsh0.c +# <> libsqlite3.lib: $(LIBOBJ) $(LTLIB) $(LTLIBOPTS) /OUT:$@ $(LIBOBJ) $(TLIBS) -# <> libtclsqlite3.lib: tclsqlite.lo libsqlite3.lib - $(LTLIB) $(LTLIBOPTS) $(LTLIBPATHS) /OUT:$@ tclsqlite.lo libsqlite3.lib $(LIBTCLSTUB) $(TLIBS) + $(LTLIB) $(LTLIBOPTS) $(TCLLIBPATHS) $(LTLIBPATHS) /OUT:$@ tclsqlite.lo libsqlite3.lib $(LIBTCLSTUB) $(TLIBS) + +tclsqlite3.def: tclsqlite.lo + echo EXPORTS > tclsqlite3.def + dumpbin /all tclsqlite.lo \ + | $(TCLSH_CMD) $(TOP)\tool\replace.tcl include "^\s+/EXPORT:_?((?:Sqlite3|Tclsqlite3)_[^@]*)(?:@\d+)?$$" \1 \ + | sort >> tclsqlite3.def + +pkgIndex.tcl: $(TOP)\VERSION + for /F %%V in ('type "$(TOP)\VERSION"') do ( \ + echo package ifneeded sqlite3 @version@ [list load [file join $$dir $(SQLITE3TCLDLL)] Sqlite3] \ + | $(TCLSH_CMD) $(TOP)\tool\replace.tcl exact @version@ %%V > pkgIndex.tcl \ + ) + +$(SQLITE3TCLDLL): libtclsqlite3.lib $(LIBRESOBJS) tclsqlite3.def pkgIndex.tcl + $(LD) $(LDFLAGS) $(LTLINKOPTS) $(LTLIBPATHS) /DLL /DEF:tclsqlite3.def /OUT:$@ libtclsqlite3.lib $(LIBRESOBJS) $(LTLIBS) $(TLIBS) + +tclextension: $(SQLITE3TCLDLL) + +tclextension-install: $(SQLITE3TCLDLL) + $(TCLSH_CMD) $(TOP)\tool\buildtclext.tcl --install-only + +tclextension-uninstall: + $(TCLSH_CMD) $(TOP)\tool\buildtclext.tcl --uninstall + +tclextension-list: + @ $(TCLSH_CMD) $(TOP)\tool\buildtclext.tcl --info + +tclextension-verify: sqlite3.h + @ $(TCLSH_CMD) $(TOP)\tool\buildtclext.tcl --version-check + + # <> -$(SQLITE3DLL): $(LIBOBJ) $(LIBRESOBJS) $(CORE_LINK_DEP) +$(SQLITE3DLL): $(LIBOBJ) $(LIBRESOBJS) $(CORE_LINK_DEP) $(LD) $(LDFLAGS) $(LTLINKOPTS) $(LTLIBPATHS) /DLL $(CORE_LINK_OPTS) /OUT:$@ $(LIBOBJ) $(LIBRESOBJS) $(LTLIBS) $(TLIBS) -# <> -sqlite3.def: libsqlite3.lib +# <> +sqlite3.def: libsqlite3.lib $(JIM_TCLSH) echo EXPORTS > sqlite3.def dumpbin /all libsqlite3.lib \ - | $(TCLSH_CMD) $(TOP)\tool\replace.tcl include "^\s+1 _?(sqlite3_.*)$$" \1 \ + | $(JIM_TCLSH) $(TOP)\tool\replace.tcl include "^\s+1 _?(sqlite3(?:session|changeset|changegroup|rebaser|rbu)?_[^@]*)(?:@\d+)?$$" \1 \ | sort >> sqlite3.def -# <> +# <> -$(SQLITE3EXE): $(TOP)\src\shell.c $(SHELL_CORE_DEP) $(LIBRESOBJS) $(SHELL_CORE_SRC) $(SQLITE3H) - $(LTLINK) $(SHELL_COMPILE_OPTS) $(READLINE_FLAGS) $(TOP)\src\shell.c $(SHELL_CORE_SRC) \ +$(SQLITE3EXE): shell.c $(SHELL_CORE_DEP) $(LIBRESOBJS) $(SHELL_CORE_SRC) $(SQLITE3H) + $(LTLINK) $(SHELL_COMPILE_OPTS) $(READLINE_FLAGS) shell.c $(SHELL_CORE_SRC) \ /link $(SQLITE3EXEPDB) $(LDFLAGS) $(LTLINKOPTS) $(SHELL_LINK_OPTS) $(LTLIBPATHS) $(LIBRESOBJS) $(LIBREADLINE) $(LTLIBS) $(TLIBS) # <> -sqldiff.exe: $(TOP)\tool\sqldiff.c $(SQLITE3C) $(SQLITE3H) - $(LTLINK) $(NO_WARN) $(TOP)\tool\sqldiff.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS) +sqldiff.exe: $(TOP)\tool\sqldiff.c $(TOP)\ext\misc\sqlite3_stdio.h $(TOP)\ext\misc\sqlite3_stdio.c $(SQLITE3C) $(SQLITE3H) $(LIBRESOBJS) + $(LTLINK) $(NO_WARN) -I$(TOP)\ext\misc $(TOP)\tool\sqldiff.c $(TOP)\ext\misc\sqlite3_stdio.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS) $(LIBRESOBJS) + +dbhash.exe: $(TOP)\tool\dbhash.c $(SQLITE3C) $(SQLITE3H) + $(LTLINK) $(NO_WARN) $(TOP)\tool\dbhash.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS) + +RSYNC_SRC = \ + $(TOP)\tool\sqlite3_rsync.c \ + $(SQLITE3C) + +RSYNC_OPT = \ + -DSQLITE_ENABLE_DBPAGE_VTAB \ + -DSQLITE_THREADSAFE=0 \ + -DSQLITE_OMIT_LOAD_EXTENSION \ + -DSQLITE_OMIT_DEPRECATED + +sqlite3_rsync.exe: $(RSYNC_SRC) $(LIBRESOBJS) + $(LTLINK) $(RSYNC_OPT) $(NO_WARN) $(RSYNC_SRC) /link $(LDFLAGS) $(LTLINKOPTS) $(LIBRESOBJS) + +scrub.exe: $(TOP)\ext\misc\scrub.c $(SQLITE3C) $(SQLITE3H) + $(LTLINK) $(NO_WARN) -DSCRUB_STANDALONE=1 $(TOP)\ext\misc\scrub.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS) srcck1.exe: $(TOP)\tool\srcck1.c $(BCC) $(NO_WARN) -Fe$@ $(TOP)\tool\srcck1.c -sourcetest: srcck1.exe sqlite3.c - srcck1.exe sqlite3.c +sourcetest: srcck1.exe $(SQLITE3C) + srcck1.exe $(SQLITE3C) + +src-verify.exe: $(TOP)\tool\src-verify.c + $(LTLINK) $(NO_WARN) $(TOP)\tool\src-verify.c + +verify-source: src-verify.exe + src-verify.exe $(TOP) fuzzershell.exe: $(TOP)\tool\fuzzershell.c $(SQLITE3C) $(SQLITE3H) $(LTLINK) $(NO_WARN) $(FUZZERSHELL_COMPILE_OPTS) $(TOP)\tool\fuzzershell.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS) -fuzzcheck.exe: $(TOP)\test\fuzzcheck.c $(SQLITE3C) $(SQLITE3H) - $(LTLINK) $(NO_WARN) $(FUZZCHECK_COMPILE_OPTS) $(TOP)\test\fuzzcheck.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS) +dbfuzz.exe: $(TOP)\test\dbfuzz.c $(SQLITE3C) $(SQLITE3H) + $(LTLINK) $(NO_WARN) $(DBFUZZ_COMPILE_OPTS) $(TOP)\test\dbfuzz.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS) + +fuzzcheck.exe: $(FUZZCHECK_SRC) $(SQLITE3C) $(SQLITE3H) + $(LTLINK) /F 8388608 $(NO_WARN) $(FUZZCHECK_OPTS) $(FUZZCHECK_SRC) $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS) + +fuzzcheck-asan.exe: $(FUZZCHECK_SRC) $(SQLITE3C) $(SQLITE3H) + $(LTLINK) $(NO_WARN) /fsanitize=address $(FUZZCHECK_OPTS) $(FUZZCHECK_SRC) $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS) + +ossshell.exe: $(OSSSHELL_SRC) $(SQLITE3C) $(SQLITE3H) + $(LTLINK) $(NO_WARN) $(FUZZCHECK_OPTS) $(OSSSHELL_SRC) $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS) + +sessionfuzz.exe: zlib $(TOP)\test\sessionfuzz.c $(SQLITE3C) $(SQLITE3H) + $(LTLINK) $(NO_WARN) -I$(ZLIBINCDIR) $(TOP)\test\sessionfuzz.c /link $(LDFLAGS) $(LTLINKOPTS) /LIBPATH:$(ZLIBLIBDIR) $(ZLIBLIB) mptester.exe: $(TOP)\mptest\mptest.c $(SQLITE3C) $(SQLITE3H) $(LTLINK) $(NO_WARN) $(MPTESTER_COMPILE_OPTS) $(TOP)\mptest\mptest.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS) @@ -1455,34 +2011,22 @@ mptest: mptester.exe # files are automatically generated. This target takes care of # all that automatic generation. # -.target_source: $(SRC) $(TOP)\tool\vdbe-compress.tcl fts5.c +.target_source: $(SRC) $(TOP)\tool\vdbe-compress.tcl fts5.c $(SQLITE_TCL_DEP) $(JIM_TCLSH) -rmdir /Q/S tsrc 2>NUL -mkdir tsrc - for %i in ($(SRC00)) do copy /Y %i tsrc - for %i in ($(SRC01)) do copy /Y %i tsrc - for %i in ($(SRC02)) do copy /Y %i tsrc - for %i in ($(SRC03)) do copy /Y %i tsrc - for %i in ($(SRC04)) do copy /Y %i tsrc - for %i in ($(SRC05)) do copy /Y %i tsrc - for %i in ($(SRC06)) do copy /Y %i tsrc - for %i in ($(SRC07)) do copy /Y %i tsrc - for %i in ($(SRC08)) do copy /Y %i tsrc - for %i in ($(SRC09)) do copy /Y %i tsrc - for %i in ($(SRC10)) do copy /Y %i tsrc - for %i in ($(SRC11)) do copy /Y %i tsrc - copy /Y fts5.c tsrc - copy /Y fts5.h tsrc + $(JIM_TCLSH) $(TOP)\tool\cp.tcl $(SRC00) $(SRC01) $(SRC03) $(SRC04) $(SRC05) $(SRC07) $(SRC09) $(SRC10) $(SRC11) $(SRC12) fts5.c fts5.h tsrc + copy /B tsrc\fts5.c +,, + copy /B tsrc\fts5.h +,, del /Q tsrc\sqlite.h.in tsrc\parse.y 2>NUL - $(TCLSH_CMD) $(TOP)\tool\vdbe-compress.tcl $(OPTS) < tsrc\vdbe.c > vdbe.new + $(JIM_TCLSH) $(TOP)\tool\vdbe-compress.tcl $(OPTS) < tsrc\vdbe.c > vdbe.new move vdbe.new tsrc\vdbe.c echo > .target_source -sqlite3.c: .target_source sqlite3ext.h $(TOP)\tool\mksqlite3c.tcl - $(TCLSH_CMD) $(TOP)\tool\mksqlite3c.tcl $(MKSQLITE3C_ARGS) - copy tsrc\shell.c . +sqlite3.c: .target_source sqlite3ext.h sqlite3session.h $(MKSQLITE3C_TOOL) src-verify.exe $(JIM_TCLSH) + $(JIM_TCLSH) $(MKSQLITE3C_TOOL) $(MKSQLITE3C_ARGS) $(EXTRA_SRC) -sqlite3-all.c: sqlite3.c $(TOP)\tool\split-sqlite3c.tcl - $(TCLSH_CMD) $(TOP)\tool\split-sqlite3c.tcl +sqlite3-all.c: sqlite3.c $(TOP)\tool\split-sqlite3c.tcl $(JIM_TCLSH) + $(JIM_TCLSH) $(TOP)\tool\split-sqlite3c.tcl # <> # Rule to build the amalgamation @@ -1494,12 +2038,19 @@ sqlite3.lo: $(SQLITE3C) # Rules to build the LEMON compiler generator # lempar.c: $(TOP)\tool\lempar.c - copy $(TOP)\tool\lempar.c . + copy /Y $(TOP)\tool\lempar.c . + copy /B lempar.c +,, lemon.exe: $(TOP)\tool\lemon.c lempar.c $(BCC) $(NO_WARN) -Daccess=_access \ -Fe$@ $(TOP)\tool\lemon.c /link $(LDFLAGS) $(NLTLINKOPTS) $(NLTLIBPATHS) +# <> +# Rules to build the source-id generator tool +# +mksourceid.exe: $(TOP)\tool\mksourceid.c + $(BCC) $(NO_WARN) -Fe$@ $(TOP)\tool\mksourceid.c /link $(LDFLAGS) $(NLTLINKOPTS) $(NLTLIBPATHS) + # Rules to build individual *.lo files from generated *.c files. This # applies to: # @@ -1517,11 +2068,11 @@ opcodes.lo: opcodes.c # !IF $(USE_RC)!=0 # <> -$(LIBRESOBJS): $(TOP)\src\sqlite3.rc $(SQLITE3H) +$(LIBRESOBJS): $(TOP)\src\sqlite3.rc $(SQLITE3H) $(TOP)\VERSION $(JIM_TCLSH) echo #ifndef SQLITE_RESOURCE_VERSION > sqlite3rc.h for /F %%V in ('type "$(TOP)\VERSION"') do ( \ echo #define SQLITE_RESOURCE_VERSION %%V \ - | $(TCLSH_CMD) $(TOP)\tool\replace.tcl exact . ^, >> sqlite3rc.h \ + | $(JIM_TCLSH) $(TOP)\tool\replace.tcl exact . ^, >> sqlite3rc.h \ ) echo #endif >> sqlite3rc.h $(LTRCOMPILE) -fo $(LIBRESOBJS) $(TOP)\src\sqlite3.rc @@ -1561,16 +2112,25 @@ build.lo: $(TOP)\src\build.c $(HDR) callback.lo: $(TOP)\src\callback.c $(HDR) $(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\callback.c +carray.lo: $(TOP)\src\carray.c $(HDR) + $(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\carray.c + complete.lo: $(TOP)\src\complete.c $(HDR) $(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\complete.c -ctime.lo: $(TOP)\src\ctime.c $(HDR) - $(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\ctime.c +ctime.c: $(TOP)\tool\mkctimec.tcl $(JIM_TCLSH) + $(JIM_TCLSH) $(TOP)\tool\mkctimec.tcl + +ctime.lo: ctime.c $(HDR) + $(LTCOMPILE) $(CORE_COMPILE_OPTS) -c ctime.c date.lo: $(TOP)\src\date.c $(HDR) $(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\date.c -dbstat.lo: $(TOP)\src\date.c $(HDR) +dbpage.lo: $(TOP)\src\dbpage.c $(HDR) + $(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\dbpage.c + +dbstat.lo: $(TOP)\src\dbstat.c $(HDR) $(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\dbstat.c delete.lo: $(TOP)\src\delete.c $(HDR) @@ -1597,8 +2157,8 @@ hash.lo: $(TOP)\src\hash.c $(HDR) insert.lo: $(TOP)\src\insert.c $(HDR) $(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\insert.c -journal.lo: $(TOP)\src\journal.c $(HDR) - $(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\journal.c +json.lo: $(TOP)\src\json.c $(HDR) + $(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\json.c legacy.lo: $(TOP)\src\legacy.c $(HDR) $(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\legacy.c @@ -1627,6 +2187,9 @@ mem3.lo: $(TOP)\src\mem3.c $(HDR) mem5.lo: $(TOP)\src\mem5.c $(HDR) $(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\mem5.c +memdb.lo: $(TOP)\src\memdb.c $(HDR) + $(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\memdb.c + memjournal.lo: $(TOP)\src\memjournal.c $(HDR) $(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\memjournal.c @@ -1657,6 +2220,9 @@ pcache1.lo: $(TOP)\src\pcache1.c $(HDR) $(TOP)\src\pcache.h os.lo: $(TOP)\src\os.c $(HDR) $(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\os.c +os_kv.lo: $(TOP)\src\os_kv.c $(HDR) + $(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\os_kv.c + os_unix.lo: $(TOP)\src\os_unix.c $(HDR) $(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\os_unix.c @@ -1705,6 +2271,9 @@ trigger.lo: $(TOP)\src\trigger.c $(HDR) update.lo: $(TOP)\src\update.c $(HDR) $(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\update.c +upsert.lo: $(TOP)\src\upsert.c $(HDR) + $(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\upsert.c + utf.lo: $(TOP)\src\utf.c $(HDR) $(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\utf.c @@ -1735,6 +2304,9 @@ vdbesort.lo: $(TOP)\src\vdbesort.c $(HDR) vdbetrace.lo: $(TOP)\src\vdbetrace.c $(HDR) $(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\vdbetrace.c +vdbevtab.lo: $(TOP)\src\vdbevtab.c $(HDR) + $(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\vdbevtab.c + vtab.lo: $(TOP)\src\vtab.c $(HDR) $(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\vtab.c @@ -1753,39 +2325,55 @@ wherecode.lo: $(TOP)\src\wherecode.c $(HDR) whereexpr.lo: $(TOP)\src\whereexpr.c $(HDR) $(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\whereexpr.c -tclsqlite.lo: $(TOP)\src\tclsqlite.c $(HDR) +window.lo: $(TOP)\src\window.c $(HDR) + $(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\window.c + +tclsqlite.lo: $(TOP)\src\tclsqlite.c $(HDR) $(SQLITE_TCL_DEP) $(LTCOMPILE) $(NO_WARN) -DUSE_TCL_STUBS=1 -DBUILD_sqlite -I$(TCLINCDIR) -c $(TOP)\src\tclsqlite.c -tclsqlite-shell.lo: $(TOP)\src\tclsqlite.c $(HDR) - $(LTCOMPILE) $(NO_WARN) -DTCLSH=1 -DBUILD_sqlite -I$(TCLINCDIR) -c $(TOP)\src\tclsqlite.c +tclsqlite-shell.lo: $(TOP)\src\tclsqlite.c $(HDR) $(SQLITE_TCL_DEP) + $(LTCOMPILE) $(NO_WARN) -DTCLSH -DBUILD_sqlite -I$(TCLINCDIR) -c $(TOP)\src\tclsqlite.c tclsqlite3.exe: tclsqlite-shell.lo $(SQLITE3C) $(SQLITE3H) $(LIBRESOBJS) - $(LTLINK) $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS) $(LTLIBPATHS) /OUT:$@ tclsqlite-shell.lo $(LIBRESOBJS) $(LTLIBS) $(TLIBS) + $(LTLINK) $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS) $(TCLLIBPATHS) $(LTLIBPATHS) /OUT:$@ tclsqlite-shell.lo $(LIBRESOBJS) $(TCLLIBS) $(LTLIBS) $(TLIBS) # Rules to build opcodes.c and opcodes.h # -opcodes.c: opcodes.h $(TOP)\tool\mkopcodec.tcl - $(TCLSH_CMD) $(TOP)\tool\mkopcodec.tcl opcodes.h > opcodes.c +opcodes.c: opcodes.h $(TOP)\tool\mkopcodec.tcl $(JIM_TCLSH) + $(JIM_TCLSH) $(TOP)\tool\mkopcodec.tcl opcodes.h > opcodes.c -opcodes.h: parse.h $(TOP)\src\vdbe.c $(TOP)\tool\mkopcodeh.tcl - type parse.h $(TOP)\src\vdbe.c | $(TCLSH_CMD) $(TOP)\tool\mkopcodeh.tcl > opcodes.h +opcodes.h: parse.h $(TOP)\src\vdbe.c $(TOP)\tool\mkopcodeh.tcl $(JIM_TCLSH) + type parse.h $(TOP)\src\vdbe.c | $(JIM_TCLSH) $(TOP)\tool\mkopcodeh.tcl > opcodes.h # Rules to build parse.c and parse.h - the outputs of lemon. # parse.h: parse.c -parse.c: $(TOP)\src\parse.y lemon.exe $(TOP)\tool\addopcodes.tcl +parse.c: $(TOP)\src\parse.y lemon.exe del /Q parse.y parse.h parse.h.temp 2>NUL - copy $(TOP)\src\parse.y . - .\lemon.exe $(REQ_FEATURE_FLAGS) $(OPT_FEATURE_FLAGS) $(EXT_FEATURE_FLAGS) $(OPTS) parse.y - move parse.h parse.h.temp - $(TCLSH_CMD) $(TOP)\tool\addopcodes.tcl parse.h.temp > parse.h + copy /Y $(TOP)\src\parse.y . + copy /B parse.y +,, + .\lemon.exe $(REQ_FEATURE_FLAGS) $(OPT_FEATURE_FLAGS) $(EXT_FEATURE_FLAGS) $(OPTS) -S parse.y -$(SQLITE3H): $(TOP)\src\sqlite.h.in $(TOP)\manifest.uuid $(TOP)\VERSION - $(TCLSH_CMD) $(TOP)\tool\mksqlite3h.tcl $(TOP:\=/) > $(SQLITE3H) +pragma.h: $(TOP)\tool\mkpragmatab.tcl $(JIM_TCLSH) + $(JIM_TCLSH) $(TOP)\tool\mkpragmatab.tcl + +$(SQLITE3H): $(TOP)\src\sqlite.h.in $(TOP)\manifest mksourceid.exe $(TOP)\VERSION $(JIM_TCLSH) + $(JIM_TCLSH) $(TOP)\tool\mksqlite3h.tcl "$(TOP:\=/)" -o $(SQLITE3H) $(MKSQLITE3H_ARGS) + +sqlite3ext.h: .target_source +!IF $(USE_STDCALL)!=0 || $(FOR_WIN10)!=0 + type tsrc\sqlite3ext.h | $(TCLSH_CMD) $(TOP)\tool\replace.tcl regsub "\(\*\)" "(SQLITE_CALLBACK *)" \ + | $(TCLSH_CMD) $(TOP)\tool\replace.tcl regsub "\(\*" "(SQLITE_APICALL *" > sqlite3ext.h + copy /Y sqlite3ext.h tsrc\sqlite3ext.h +!ELSE + copy /Y tsrc\sqlite3ext.h sqlite3ext.h + copy /B sqlite3ext.h +,, +!ENDIF -sqlite3ext.h: .target_source - copy tsrc\sqlite3ext.h . +sqlite3session.h: $(TOP)\ext\session\sqlite3session.h + copy /Y $(TOP)\ext\session\sqlite3session.h . + copy /B sqlite3session.h +,, mkkeywordhash.exe: $(TOP)\tool\mkkeywordhash.c $(BCC) $(NO_WARN) -Fe$@ $(REQ_FEATURE_FLAGS) $(OPT_FEATURE_FLAGS) $(EXT_FEATURE_FLAGS) $(OPTS) \ @@ -1794,31 +2382,55 @@ mkkeywordhash.exe: $(TOP)\tool\mkkeywordhash.c keywordhash.h: $(TOP)\tool\mkkeywordhash.c mkkeywordhash.exe .\mkkeywordhash.exe > keywordhash.h - +# Source and header files that shell.c depends on +SHELL_DEP = \ + $(TOP)\src\shell.c.in \ + $(TOP)\ext\expert\sqlite3expert.c \ + $(TOP)\ext\expert\sqlite3expert.h \ + $(TOP)\ext\intck\sqlite3intck.c \ + $(TOP)\ext\intck\sqlite3intck.h \ + $(TOP)\ext\misc\appendvfs.c \ + $(TOP)\ext\misc\base64.c \ + $(TOP)\ext\misc\base85.c \ + $(TOP)\ext\misc\completion.c \ + $(TOP)\ext\misc\decimal.c \ + $(TOP)\ext\misc\fileio.c \ + $(TOP)\ext\misc\ieee754.c \ + $(TOP)\ext\misc\memtrace.c \ + $(TOP)\ext\misc\pcachetrace.c \ + $(TOP)\ext\misc\regexp.c \ + $(TOP)\ext\misc\series.c \ + $(TOP)\ext\misc\sha1.c \ + $(TOP)\ext\misc\shathree.c \ + $(TOP)\ext\misc\sqlar.c \ + $(TOP)\ext\misc\sqlite3_stdio.c \ + $(TOP)\ext\misc\sqlite3_stdio.h \ + $(TOP)\ext\misc\uint.c \ + $(TOP)\ext\misc\vfstrace.c \ + $(TOP)\ext\misc\windirent.h \ + $(TOP)\ext\misc\zipfile.c \ + $(TOP)\ext\recover\dbdata.c \ + $(TOP)\ext\recover\sqlite3recover.c \ + $(TOP)\ext\recover\sqlite3recover.h + +# If use of zlib is enabled, add the "zipfile.c" source file. +# +!IF $(USE_ZLIB)!=0 +SHELL_DEP = $(SHELL_DEP) $(TOP)\ext\misc\sqlar.c +SHELL_DEP = $(SHELL_DEP) $(TOP)\ext\misc\zipfile.c +!ENDIF + +shell.c: $(SHELL_DEP) $(TOP)\tool\mkshellc.tcl $(JIM_TCLSH) + $(JIM_TCLSH) $(TOP)\tool\mkshellc.tcl shell.c + +zlib: + pushd $(ZLIBDIR) && $(MAKE) /f win32\Makefile.msc clean $(ZLIBLIB) CFLAGS="$(ZLIBCFLAGS)" && popd # Rules to build the extension objects. # icu.lo: $(TOP)\ext\icu\icu.c $(HDR) $(EXTHDR) $(LTCOMPILE) $(CORE_COMPILE_OPTS) $(NO_WARN) -DSQLITE_CORE -c $(TOP)\ext\icu\icu.c -fts2.lo: $(TOP)\ext\fts2\fts2.c $(HDR) $(EXTHDR) - $(LTCOMPILE) $(CORE_COMPILE_OPTS) $(NO_WARN) -DSQLITE_CORE -c $(TOP)\ext\fts2\fts2.c - -fts2_hash.lo: $(TOP)\ext\fts2\fts2_hash.c $(HDR) $(EXTHDR) - $(LTCOMPILE) $(CORE_COMPILE_OPTS) $(NO_WARN) -DSQLITE_CORE -c $(TOP)\ext\fts2\fts2_hash.c - -fts2_icu.lo: $(TOP)\ext\fts2\fts2_icu.c $(HDR) $(EXTHDR) - $(LTCOMPILE) $(CORE_COMPILE_OPTS) $(NO_WARN) -DSQLITE_CORE -c $(TOP)\ext\fts2\fts2_icu.c - -fts2_porter.lo: $(TOP)\ext\fts2\fts2_porter.c $(HDR) $(EXTHDR) - $(LTCOMPILE) $(CORE_COMPILE_OPTS) $(NO_WARN) -DSQLITE_CORE -c $(TOP)\ext\fts2\fts2_porter.c - -fts2_tokenizer.lo: $(TOP)\ext\fts2\fts2_tokenizer.c $(HDR) $(EXTHDR) - $(LTCOMPILE) $(CORE_COMPILE_OPTS) $(NO_WARN) -DSQLITE_CORE -c $(TOP)\ext\fts2\fts2_tokenizer.c - -fts2_tokenizer1.lo: $(TOP)\ext\fts2\fts2_tokenizer1.c $(HDR) $(EXTHDR) - $(LTCOMPILE) $(CORE_COMPILE_OPTS) $(NO_WARN) -DSQLITE_CORE -c $(TOP)\ext\fts2\fts2_tokenizer1.c - fts3.lo: $(TOP)\ext\fts3\fts3.c $(HDR) $(EXTHDR) $(LTCOMPILE) $(CORE_COMPILE_OPTS) $(NO_WARN) -DSQLITE_CORE -c $(TOP)\ext\fts3\fts3.c @@ -1858,9 +2470,15 @@ fts3_unicode2.lo: $(TOP)\ext\fts3\fts3_unicode2.c $(HDR) $(EXTHDR) fts3_write.lo: $(TOP)\ext\fts3\fts3_write.c $(HDR) $(EXTHDR) $(LTCOMPILE) $(CORE_COMPILE_OPTS) $(NO_WARN) -DSQLITE_CORE -c $(TOP)\ext\fts3\fts3_write.c +stmt.lo: $(TOP)\ext\misc\stmt.c $(HDR) $(EXTHDR) + $(LTCOMPILE) $(CORE_COMPILE_OPTS) $(NO_WARN) -DSQLITE_CORE -c $(TOP)\ext\misc\stmt.c + rtree.lo: $(TOP)\ext\rtree\rtree.c $(HDR) $(EXTHDR) $(LTCOMPILE) $(CORE_COMPILE_OPTS) $(NO_WARN) -DSQLITE_CORE -c $(TOP)\ext\rtree\rtree.c +sqlite3session.lo: $(TOP)\ext\session\sqlite3session.c $(HDR) $(EXTHDR) + $(LTCOMPILE) $(CORE_COMPILE_OPTS) $(NO_WARN) -DSQLITE_CORE -c $(TOP)\ext\session\sqlite3session.c + # FTS5 things # FTS5_SRC = \ @@ -1881,15 +2499,17 @@ FTS5_SRC = \ $(TOP)\ext\fts5\fts5_vocab.c fts5parse.c: $(TOP)\ext\fts5\fts5parse.y lemon.exe - copy $(TOP)\ext\fts5\fts5parse.y . + copy /Y $(TOP)\ext\fts5\fts5parse.y . + copy /B fts5parse.y +,, del /Q fts5parse.h 2>NUL - .\lemon.exe $(REQ_FEATURE_FLAGS) $(OPT_FEATURE_FLAGS) $(EXT_FEATURE_FLAGS) $(OPTS) fts5parse.y + .\lemon.exe $(REQ_FEATURE_FLAGS) $(OPT_FEATURE_FLAGS) $(EXT_FEATURE_FLAGS) $(OPTS) -S fts5parse.y -fts5parse.h: fts5parse.c +fts5parse.h: fts5parse.c -fts5.c: $(FTS5_SRC) - $(TCLSH_CMD) $(TOP)\ext\fts5\tool\mkfts5c.tcl - copy $(TOP)\ext\fts5\fts5.h . +fts5.c: $(FTS5_SRC) $(JIM_TCLSH) + $(JIM_TCLSH) $(TOP)\ext\fts5\tool\mkfts5c.tcl + copy /Y $(TOP)\ext\fts5\fts5.h . + copy /B fts5.h +,, fts5.lo: fts5.c $(HDR) $(EXTHDR) $(LTCOMPILE) $(CORE_COMPILE_OPTS) $(NO_WARN) -DSQLITE_CORE -c fts5.c @@ -1910,9 +2530,20 @@ sqlite3rbu.lo: $(TOP)\ext\rbu\sqlite3rbu.c $(HDR) $(EXTHDR) # necessary because the test fixture requires non-API symbols which are # hidden when the library is built via the amalgamation). # -TESTFIXTURE_FLAGS = -DTCLSH=1 -DSQLITE_TEST=1 -DSQLITE_CRASH_TEST=1 +TESTFIXTURE_FLAGS = -DTCLSH_INIT_PROC=sqlite3TestInit -DSQLITE_TEST=1 -DSQLITE_CRASH_TEST=1 TESTFIXTURE_FLAGS = $(TESTFIXTURE_FLAGS) -DSQLITE_SERVER=1 -DSQLITE_PRIVATE="" TESTFIXTURE_FLAGS = $(TESTFIXTURE_FLAGS) -DSQLITE_CORE $(NO_WARN) +TESTFIXTURE_FLAGS = $(TESTFIXTURE_FLAGS) -DSQLITE_SERIES_CONSTRAINT_VERIFY=1 +TESTFIXTURE_FLAGS = $(TESTFIXTURE_FLAGS) -DSQLITE_DEFAULT_PAGE_SIZE=1024 +TESTFIXTURE_FLAGS = $(TESTFIXTURE_FLAGS) -DSQLITE_ENABLE_STMTVTAB=1 +TESTFIXTURE_FLAGS = $(TESTFIXTURE_FLAGS) -DSQLITE_ENABLE_DBPAGE_VTAB=1 +TESTFIXTURE_FLAGS = $(TESTFIXTURE_FLAGS) -DSQLITE_ENABLE_BYTECODE_VTAB=1 +TESTFIXTURE_FLAGS = $(TESTFIXTURE_FLAGS) -DSQLITE_ENABLE_CARRAY=1 +TESTFIXTURE_FLAGS = $(TESTFIXTURE_FLAGS) -DSQLITE_ENABLE_PERCENTILE=1 +TESTFIXTURE_FLAGS = $(TESTFIXTURE_FLAGS) -DSQLITE_CKSUMVFS_STATIC=1 +TESTFIXTURE_FLAGS = $(TESTFIXTURE_FLAGS) $(TEST_CCONV_OPTS) +TESTFIXTURE_FLAGS = $(TESTFIXTURE_FLAGS) -DSQLITE_STATIC_RANDOMJSON +TESTFIXTURE_FLAGS = $(TESTFIXTURE_FLAGS) -DSQLITE_STRICT_SUBTYPE=1 TESTFIXTURE_SRC0 = $(TESTEXT) $(TESTSRC2) TESTFIXTURE_SRC1 = $(TESTEXT) $(SQLITE3C) @@ -1922,17 +2553,54 @@ TESTFIXTURE_SRC = $(TESTSRC) $(TOP)\src\tclsqlite.c $(TESTFIXTURE_SRC0) TESTFIXTURE_SRC = $(TESTSRC) $(TOP)\src\tclsqlite.c $(TESTFIXTURE_SRC1) !ENDIF -testfixture.exe: $(TESTFIXTURE_SRC) $(SQLITE3H) $(LIBRESOBJS) $(HDR) +!IF $(USE_STDCALL)!=0 || $(FOR_WIN10)!=0 +sqlite_tclDecls.h: + echo #ifndef SQLITE_TCLAPI > $(SQLITETCLDECLSH) + echo # define SQLITE_TCLAPI >> $(SQLITETCLDECLSH) + echo #endif >> $(SQLITETCLDECLSH) + type "$(TCLINCDIR)\tclDecls.h" \ + | $(TCLSH_CMD) $(TOP)\tool\replace.tcl regsub "^(EXTERN(?: CONST\d+?)?\s+?[^\(]*?\s+?)Tcl_" "\1 SQLITE_TCLAPI Tcl_" \ + | $(TCLSH_CMD) $(TOP)\tool\replace.tcl regsub "^(EXTERN\s+?(?:void|VOID)\s+?)TclFreeObj" "\1 SQLITE_TCLAPI TclFreeObj" \ + | $(TCLSH_CMD) $(TOP)\tool\replace.tcl regsub "\(\*tcl_" "(SQLITE_TCLAPI *tcl_" \ + | $(TCLSH_CMD) $(TOP)\tool\replace.tcl regsub "\(\*tclFreeObj" "(SQLITE_TCLAPI *tclFreeObj" \ + | $(TCLSH_CMD) $(TOP)\tool\replace.tcl regsub "\(\*" "(SQLITE_TCLAPI *" >> $(SQLITETCLDECLSH) + +sqlite_tcl.h: + type "$(TCLINCDIR)\tcl.h" | $(TCLSH_CMD) $(TOP)\tool\replace.tcl exact tclDecls.h sqlite_tclDecls.h \ + | $(TCLSH_CMD) $(TOP)\tool\replace.tcl regsub "typedef (.*?)\(Tcl_" "typedef \1 (SQLITE_TCLAPI Tcl_" \ + | $(TCLSH_CMD) $(TOP)\tool\replace.tcl exact "void (*freeProc)" "void (SQLITE_TCLAPI *freeProc)" \ + | $(TCLSH_CMD) $(TOP)\tool\replace.tcl exact "Tcl_HashEntry *(*findProc)" "Tcl_HashEntry *(SQLITE_TCLAPI *findProc)" \ + | $(TCLSH_CMD) $(TOP)\tool\replace.tcl exact "Tcl_HashEntry *(*createProc)" "Tcl_HashEntry *(SQLITE_TCLAPI *createProc)" >> $(SQLITETCLH) +!ENDIF + +testfixture.exe: $(TESTFIXTURE_SRC) $(TESTFIXTURE_DEP) $(SQLITE3H) $(LIBRESOBJS) $(HDR) $(SQLITE_TCL_DEP) $(LTLINK) -DSQLITE_NO_SYNC=1 $(TESTFIXTURE_FLAGS) \ - -DBUILD_sqlite -I$(TCLINCDIR) \ + -DBUILD_sqlite -I$(TCLINCDIR) -I$(TOP)\ext\misc \ $(TESTFIXTURE_SRC) \ - /link $(LDFLAGS) $(LTLINKOPTS) $(LTLIBPATHS) $(LIBRESOBJS) $(LTLIBS) $(TLIBS) + /link $(LDFLAGS) $(LTLINKOPTS) $(TCLLIBPATHS) $(LTLIBPATHS) $(LIBRESOBJS) $(TCLLIBS) $(LTLIBS) $(TLIBS) + +# A small helper for manually running individual tests +tf.bat: testfixture.exe Makefile.msc + echo @set PATH=$(LIBTCLPATH);%PATH% > $@ + echo .\testfixture.exe %* >> $@ -extensiontest: testfixture.exe testloadext.dll +extensiontest: testfixture.exe testloadext.dll @set PATH=$(LIBTCLPATH);$(PATH) .\testfixture.exe $(TOP)\test\loadext.test $(TESTOPTS) -fulltest: $(TESTPROGS) fuzztest +tool-zip: testfixture.exe sqlite3.exe sqldiff.exe sqlite3_analyzer.exe sqlite3_rsync.exe sqlite3.dll $(TOP)\tool\mktoolzip.tcl + .\testfixture.exe $(TOP)\tool\mktoolzip.tcl + +snapshot-zip: testfixture.exe sqlite3.exe sqldiff.exe sqlite3_analyzer.exe sqlite3_rsync.exe sqlite3.dll $(TOP)\tool\mktoolzip.tcl + .\testfixture.exe $(TOP)\tool\mktoolzip.tcl --snapshot + +coretestprogs: testfixture.exe sqlite3.exe + +testprogs: $(TESTPROGS) srcck1.exe fuzzcheck.exe sessionfuzz.exe + +fulltest: alltest fuzztest + +alltest: $(TESTPROGS) @set PATH=$(LIBTCLPATH);$(PATH) .\testfixture.exe $(TOP)\test\all.test $(TESTOPTS) @@ -1951,8 +2619,12 @@ queryplantest: testfixture.exe shell fuzztest: fuzzcheck.exe .\fuzzcheck.exe $(FUZZDATA) -fastfuzztest: fuzzcheck.exe - .\fuzzcheck.exe --limit-mem 100M $(FUZZDATA) +# Legacy testing target for third-party integrators. The SQLite +# developers seldom use this target themselves. Instead +# they use "nmake /f Makefile.msc devtest" which runs tests on +# a standard set of options +# +test: $(TESTPROGS) sourcetest fuzztest tcltest # Minimal testing that runs in less than 3 minutes (on a fast machine) # @@ -1963,100 +2635,359 @@ quicktest: testfixture.exe sourcetest # This is the common case. Run many tests that do not take too long, # including fuzzcheck, sqlite3_analyzer, and sqldiff tests. # -test: $(TESTPROGS) sourcetest fastfuzztest + +# The veryquick.test TCL tests. +# +tcltest: testfixture.exe @set PATH=$(LIBTCLPATH);$(PATH) .\testfixture.exe $(TOP)\test\veryquick.test $(TESTOPTS) +# Runs all the same tests cases as the "tcltest" target but uses +# the testrunner.tcl script to run them in multiple cores +# concurrently. +testrunner: testfixture.exe + .\testfixture.exe $(TOP)\test\testrunner.tcl + +# This is the testing target preferred by the core SQLite developers. +# It runs tests under a standard configuration. The devs run +# "nmake /f Makefile.msc devtest" prior to each check-in, at a minimum. +# Probably other tests too, but at least this one. +# +devtest: srctree-check sourcetest + $(TCLSH_CMD) $(TOP)\test\testrunner.tcl mdevtest + +mdevtest: + $(TCLSH_CMD) $(TOP)\test\testrunner.tcl mdevtest + +# Validate that various generated files in the source tree +# are up-to-date. +# +srctree-check: $(TOP)\tool\srctree-check.tcl + $(TCLSH_CMD) $(TOP)\tool\srctree-check.tcl + +# Testing for a release +# +releasetest: verify-source + $(TCLSH_CMD) $(TOP)\test\testrunner.tcl release + +# xdevtest is like releasetest, except that it skips the +# dependency on verify-source so that xdevtest can be run from +# a modified source tree. +# +xdevtest: + $(TCLSH_CMD) $(TOP)\test\testrunner.tcl release + + smoketest: $(TESTPROGS) @set PATH=$(LIBTCLPATH);$(PATH) .\testfixture.exe $(TOP)\test\main.test $(TESTOPTS) -sqlite3_analyzer.c: $(SQLITE3C) $(SQLITE3H) $(TOP)\src\tclsqlite.c $(TOP)\tool\spaceanal.tcl - echo #define TCLSH 2 > $@ - echo #define SQLITE_ENABLE_DBSTAT_VTAB 1 >> $@ - copy $@ + $(SQLITE3C) + $(TOP)\src\tclsqlite.c $@ - echo static const char *tclsh_main_loop(void){ >> $@ - echo static const char *zMainloop = >> $@ - $(TCLSH_CMD) $(TOP)\tool\tostr.tcl $(TOP)\tool\spaceanal.tcl >> $@ - echo ; return zMainloop; } >> $@ +shelltest: $(TESTPROGS) + .\testfixture.exe $(TOP)\test\permutations.test shell + +sqlite3_analyzer.c: $(SQLITE3C) $(SQLITE3H) $(TOP)\src\tclsqlite.c $(TOP)\tool\spaceanal.tcl $(TOP)\tool\mkccode.tcl $(TOP)\tool\sqlite3_analyzer.c.in $(TOP)\ext\misc\sqlite3_stdio.h $(TOP)\ext\misc\sqlite3_stdio.c $(SQLITE_TCL_DEP) + $(TCLSH_CMD) $(TOP)\tool\mkccode.tcl -DINCLUDE_SQLITE3_C $(TOP)\tool\sqlite3_analyzer.c.in > $@ sqlite3_analyzer.exe: sqlite3_analyzer.c $(LIBRESOBJS) $(LTLINK) $(NO_WARN) -DBUILD_sqlite -I$(TCLINCDIR) sqlite3_analyzer.c \ - /link $(LDFLAGS) $(LTLINKOPTS) $(LTLIBPATHS) $(LIBRESOBJS) $(LTLIBS) $(TLIBS) + /link $(LDFLAGS) $(LTLINKOPTS) $(TCLLIBPATHS) $(LTLIBPATHS) $(LIBRESOBJS) $(TCLLIBS) $(LTLIBS) $(TLIBS) + +sqltclsh.c: sqlite3.c $(TOP)\src\tclsqlite.c $(TOP)\tool\sqltclsh.tcl $(TOP)\ext\misc\appendvfs.c $(TOP)\tool\mkccode.tcl $(TOP)\tool\sqltclsh.c.in + $(TCLSH_CMD) $(TOP)\tool\mkccode.tcl $(TOP)\tool\sqltclsh.c.in >sqltclsh.c + +sqltclsh.exe: sqltclsh.c $(SHELL_CORE_DEP) $(LIBRESOBJS) + $(LTLINK) $(NO_WARN) -DBUILD_sqlite -I$(TCLINCDIR) sqltclsh.c \ + /link $(LDFLAGS) $(LTLINKOPTS) $(TCLLIBPATHS) $(LTLIBPATHS) $(LIBRESOBJS) $(TCLLIBS) $(LTLIBS) $(TLIBS) + +sqlite3_expert.exe: $(SQLITE3C) $(TOP)\ext\expert\sqlite3expert.h $(TOP)\ext\expert\sqlite3expert.c $(TOP)\ext\expert\expert.c + $(LTLINK) $(NO_WARN) $(TOP)\ext\expert\sqlite3expert.c $(TOP)\ext\expert\expert.c $(SQLITE3C) $(TLIBS) + +CHECKER_DEPS =\ + $(TOP)\tool\mkccode.tcl \ + sqlite3.c \ + $(TOP)\src\tclsqlite.c \ + $(TOP)\ext\repair\sqlite3_checker.tcl \ + $(TOP)\ext\repair\checkindex.c \ + $(TOP)\ext\repair\checkfreelist.c \ + $(TOP)\ext\misc\btreeinfo.c \ + $(TOP)\ext\repair\sqlite3_checker.c.in + +sqlite3_checker.c: $(CHECKER_DEPS) + $(TCLSH_CMD) $(TOP)\tool\mkccode.tcl $(TOP)\ext\repair\sqlite3_checker.c.in > $@ + +sqlite3_checker.exe: sqlite3_checker.c $(LIBRESOBJS) + $(LTLINK) $(NO_WARN) -DBUILD_sqlite -I$(TCLINCDIR) sqlite3_checker.c \ + /link $(LDFLAGS) $(LTLINKOPTS) $(TCLLIBPATHS) $(LTLIBPATHS) $(LIBRESOBJS) $(TCLLIBS) $(LTLIBS) $(TLIBS) -testloadext.lo: $(TOP)\src\test_loadext.c +dbdump.exe: $(TOP)\ext\misc\dbdump.c $(SQLITE3C) $(SQLITE3H) $(LIBRESOBJS) + $(LTLINK) $(NO_WARN) -DDBDUMP_STANDALONE $(TOP)\ext\misc\dbdump.c $(SQLITE3C) \ + /link $(LDFLAGS) $(LTLINKOPTS) $(LTLIBPATHS) $(LIBRESOBJS) $(LTLIBS) + +testloadext.lo: $(TOP)\src\test_loadext.c $(SQLITE3H) $(LTCOMPILE) $(NO_WARN) -c $(TOP)\src\test_loadext.c -testloadext.dll: testloadext.lo +testloadext.dll: testloadext.lo $(LD) $(LDFLAGS) $(LTLINKOPTS) $(LTLIBPATHS) /DLL /OUT:$@ testloadext.lo +dbtotxt.exe: $(TOP)\tool\dbtotxt.c + $(LTLINK) $(NO_WARN) $(TOP)\tool\dbtotxt.c /link $(LDFLAGS) $(LTLINKOPTS) + showdb.exe: $(TOP)\tool\showdb.c $(SQLITE3C) $(SQLITE3H) - $(LTLINK) $(NO_WARN) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -Fe$@ \ + $(LTLINK) $(NO_WARN) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION \ $(TOP)\tool\showdb.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS) showstat4.exe: $(TOP)\tool\showstat4.c $(SQLITE3C) $(SQLITE3H) - $(LTLINK) $(NO_WARN) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -Fe$@ \ + $(LTLINK) $(NO_WARN) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION \ $(TOP)\tool\showstat4.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS) showjournal.exe: $(TOP)\tool\showjournal.c $(SQLITE3C) $(SQLITE3H) - $(LTLINK) $(NO_WARN) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -Fe$@ \ + $(LTLINK) $(NO_WARN) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION \ $(TOP)\tool\showjournal.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS) showwal.exe: $(TOP)\tool\showwal.c $(SQLITE3C) $(SQLITE3H) - $(LTLINK) $(NO_WARN) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -Fe$@ \ + $(LTLINK) $(NO_WARN) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION \ $(TOP)\tool\showwal.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS) +showshm.exe: $(TOP)\tool\showshm.c + $(LTLINK) $(NO_WARN) $(TOP)\tool\showshm.c /link $(LDFLAGS) $(LTLINKOPTS) + +index_usage.exe: $(TOP)\tool\index_usage.c $(SQLITE3C) $(SQLITE3H) + $(LTLINK) $(NO_WARN) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION \ + $(TOP)\tool\index_usage.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS) + +changeset.exe: $(TOP)\ext\session\changeset.c $(SQLITE3C) $(SQLITE3H) + $(LTLINK) $(NO_WARN) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION \ + -DSQLITE_ENABLE_SESSION=1 -DSQLITE_ENABLE_PREUPDATE_HOOK=1 \ + $(TOP)\ext\session\changeset.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS) + +changesetfuzz.exe: $(TOP)\ext\session\changesetfuzz.c $(SQLITE3C) $(SQLITE3H) + $(LTLINK) $(NO_WARN) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION \ + -DSQLITE_ENABLE_SESSION=1 -DSQLITE_ENABLE_PREUPDATE_HOOK=1 \ + $(TOP)\ext\session\changesetfuzz.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS) + fts3view.exe: $(TOP)\ext\fts3\tool\fts3view.c $(SQLITE3C) $(SQLITE3H) - $(LTLINK) $(NO_WARN) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -Fe$@ \ + $(LTLINK) $(NO_WARN) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION \ $(TOP)\ext\fts3\tool\fts3view.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS) rollback-test.exe: $(TOP)\tool\rollback-test.c $(SQLITE3C) $(SQLITE3H) - $(LTLINK) $(NO_WARN) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -Fe$@ \ + $(LTLINK) $(NO_WARN) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION \ $(TOP)\tool\rollback-test.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS) +atrc.exe: $(TOP)\test\atrc.c $(SQLITE3C) $(SQLITE3H) + $(LTLINK) $(NO_WARN) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION \ + $(TOP)\test\atrc.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS) + LogEst.exe: $(TOP)\tool\logest.c $(SQLITE3H) - $(LTLINK) $(NO_WARN) -Fe$@ $(TOP)\tool\LogEst.c /link $(LDFLAGS) $(LTLINKOPTS) + $(LTLINK) $(NO_WARN) $(TOP)\tool\LogEst.c /link $(LDFLAGS) $(LTLINKOPTS) wordcount.exe: $(TOP)\test\wordcount.c $(SQLITE3C) $(SQLITE3H) - $(LTLINK) $(NO_WARN) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -Fe$@ \ + $(LTLINK) $(NO_WARN) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION \ $(TOP)\test\wordcount.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS) speedtest1.exe: $(TOP)\test\speedtest1.c $(SQLITE3C) $(SQLITE3H) - $(LTLINK) $(NO_WARN) -DSQLITE_OMIT_LOAD_EXTENSION -Fe$@ \ + $(LTLINK) $(NO_WARN) $(ST_COMPILE_OPTS) -DSQLITE_OMIT_LOAD_EXTENSION \ $(TOP)\test\speedtest1.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS) -rbu.exe: $(TOP)\ext\rbu\rbu.c $(TOP)\ext\rbu\sqlite3rbu.c $(SQLITE3C) $(SQLITE3H) - $(LTLINK) $(NO_WARN) -DSQLITE_ENABLE_RBU -Fe$@ \ +kvtest.exe: $(TOP)\test\kvtest.c $(SQLITE3C) $(SQLITE3H) + $(LTLINK) $(NO_WARN) $(KV_COMPILE_OPTS) \ + $(TOP)\test\kvtest.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS) + +rbu.exe: $(TOP)\ext\rbu\rbu.c $(TOP)\ext\rbu\sqlite3rbu.c $(SQLITE3C) $(SQLITE3H) + $(LTLINK) $(NO_WARN) -DSQLITE_ENABLE_RBU \ $(TOP)\ext\rbu\rbu.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS) + +THREADTEST3_SRC = \ + $(TOP)\test\threadtest3.c \ + $(TOP)\test\tt3_checkpoint.c \ + $(TOP)\test\tt3_index.c \ + $(TOP)\test\tt3_vacuum.c \ + $(TOP)\test\tt3_stress.c \ + $(TOP)\test\tt3_lookaside1.c + +threadtest3.exe: $(THREADTEST3_SRC) $(TOP)\src\test_multiplex.c $(SQLITE3C) $(SQLITE3H) + $(LTLINK) $(NO_WARN) $(TOP)\test\threadtest3.c $(TOP)\src\test_multiplex.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS) + +# Display key variables that control which version of TCL is to be used. +# +tcl-env: + @echo TCLDIR = $(TCLDIR) + @echo TCLVERSION = $(TCLVERSION) + @echo TCLSUFFIX = $(TCLSUFFIX) + @echo LIBTCL = $(LIBTCL) + @echo LIBTCLSTUB = $(LIBTCLSTUB) + @echo TCLSH_CMD = $(TCLSH_CMD) + @echo JIM_TCLSH = $(JIM_TCLSH) + @echo VISUALSTUDIOVERSION = $(VISUALSTUDIOVERSION) + +env: + @echo ALL_TCL_TARGETS = $(ALL_TCL_TARGETS) + @echo API_ARMOR = $(API_ARMOR) + @echo ASAN = $(ASAN) + @echo BCC = $(BCC) + @echo BUILD_ZLIB = $(BUILD_ZLIB) + @echo CC = $(CC) + @echo CCOPTS = $(CCOPTS) + @echo CHECKER_DEPS = $(CHECKER_DEPS) + @echo CORE_CCONV_OPTS = $(CORE_CCONV_OPTS) + @echo CORE_COMPILE_OPTS = $(CORE_COMPILE_OPTS) + @echo CORE_LINK_DEP = $(CORE_LINK_DEP) + @echo CORE_LINK_OPTS = $(CORE_LINK_OPTS) + @echo CRTLIBPATH = $(CRTLIBPATH) + @echo CSC = $(CSC) + @echo DBFUZZ_COMPILE_OPTS = $(DBFUZZ_COMPILE_OPTS) + @echo DEBUG = $(DEBUG) + @echo DYNAMIC_SHELL = $(DYNAMIC_SHELL) + @echo EXT_FEATURE_FLAGS = $(EXT_FEATURE_FLAGS) + @echo EXTHDR = $(EXTHDR) + @echo EXTRA_SRC = $(EXTRA_SRC) + @echo FOR_UWP = $(FOR_UWP) + @echo FUZZCHECK_OPTS = $(FUZZCHECK_OPTS) + @echo FUZZCHECK_SRC = $(FUZZCHECK_SRC) + @echo FUZZDATA = $(FUZZDATA) + @echo FUZZERSHELL_COMPILE_OPTS = $(FUZZERSHELL_COMPILE_OPTS) + @echo HDR = $(HDR) + @echo ICUDIR = $(ICUDIR) + @echo ICUINCDIR = $(ICUINCDIR) + @echo ICULIBDIR = $(ICULIBDIR) + @echo JIM_TCLSH = $(JIM_TCLSH) + @echo KV_COMPILE_OPTS = $(KV_COMPILE_OPTS) + @echo LDFLAGS = $(LDFLAGS) + @echo LD = $(LD) + @echo LIBICU = $(LIBICU) + @echo LIBOBJ = $(LIBOBJ) + @echo LIBREADLINE = $(LIBREADLINE) + @echo LIBRESOBJS = $(LIBRESOBJS) + @echo LIBTCLPATH = $(LIBTCLPATH) + @echo LIBTCLSTUB = $(LIBTCLSTUB) + @echo LIBTCL = $(LIBTCL) + @echo LTCOMPILE = $(LTCOMPILE) + @echo LTLIB = $(LTLIB) + @echo LTLIBOPTS = $(LTLIBOPTS) + @echo LTLIBPATHS = $(LTLIBPATHS) + @echo LTLIBS = $(LTLIBS) + @echo LTLINK = $(LTLINK) + @echo LTLINKOPTS = $(LTLINKOPTS) + @echo LTRCOMPILE = $(LTRCOMPILE) + @echo MEMDEBUG = $(MEMDEBUG) + @echo MINIMAL_AMALGAMATION = $(MINIMAL_AMALGAMATION) + @echo MPTESTER_COMPILE_OPTS = $(MPTESTER_COMPILE_OPTS) + @echo NCC = $(NCC) + @echo NCRTLIBPATH = $(NCRTLIBPATH) + @echo NLTLIBPATHS = $(NLTLIBPATHS) + @echo NO_LINEMACROS = $(NO_LINEMACROS) + @echo NO_TCL = $(NO_TCL) + @echo NO_WARN = $(NO_WARN) + @echo NSDKLIBPATH = $(NSDKLIBPATH) + @echo NUCRTLIBPATH = $(NUCRTLIBPATH) + @echo OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) + @echo OPTIMIZATIONS = $(OPTIMIZATIONS) + @echo OSSSHELL_SRC = $(OSSSHELL_SRC) + @echo OSTRACE = $(OSTRACE) + @echo RBU = $(RBU) + @echo RCC = $(RCC) + @echo RC = $(RC) + @echo READLINE_FLAGS = $(READLINE_FLAGS) + @echo REQ_FEATURE_FLAGS = $(REQ_FEATURE_FLAGS) + @echo RSYNC_OPT = $(RSYNC_OPT) + @echo RSYNC_SRC = $(RSYNC_SRC) + @echo SESSION = $(SESSION) + @echo SETLK_TIMEOUT = $(SETLK_TIMEOUT) + @echo SHELL_CCONV_OPTS = $(SHELL_CCONV_OPTS) + @echo SHELL_COMPILE_OPTS = $(SHELL_COMPILE_OPTS) + @echo SHELL_CORE_DEP = $(SHELL_CORE_DEP) + @echo SHELL_CORE_LIB = $(SHELL_CORE_LIB) + @echo SHELL_CORE_SRC = $(SHELL_CORE_SRC) + @echo SHELL_DEP = $(SHELL_DEP) + @echo SHELL_LINK_OPTS = $(SHELL_LINK_OPTS) + @echo SPLIT_AMALGAMATION = $(SPLIT_AMALGAMATION) + @echo SQLITETCLDECLSH = $(SQLITETCLDECLSH) + @echo SQLITE_TCL_DEP = $(SQLITE_TCL_DEP) + @echo SQLITETCLH = $(SQLITETCLH) + @echo SRC = $(SRC) + @echo STATICALLY_LINK_TCL = $(STATICALLY_LINK_TCL) + @echo ST_COMPILE_OPTS = $(ST_COMPILE_OPTS) + @echo STORELIBPATH = $(STORELIBPATH) + @echo SYMBOLS = $(SYMBOLS) + @echo TCC = $(TCC) + @echo TCLDIR = $(TCLDIR) + @echo TCLINCDIR = $(TCLINCDIR) + @echo TCLLIBDIR = $(TCLLIBDIR) + @echo TCLLIBPATHS = $(TCLLIBPATHS) + @echo TCLLIBS = $(TCLLIBS) + @echo TCLSH_CMD = $(TCLSH_CMD) + @echo TCLSQLITEEX = $(TCLSQLITEEX) + @echo TCLSUFFIX = $(TCLSUFFIX) + @echo TCLVERSION = $(TCLVERSION) + @echo TEST_CCONV_OPTS = $(TEST_CCONV_OPTS) + @echo TESTEXT = $(TESTEXT) + @echo TESTFIXTURE_DEP = $(TESTFIXTURE_DEP) + @echo TESTFIXTURE_FLAGS = $(TESTFIXTURE_FLAGS) + @echo TESTFIXTURE_SRC = $(TESTFIXTURE_SRC) + @echo TESTOPTS = $(TESTOPTS) + @echo TESTPROGS = $(TESTPROGS) + @echo TESTSRC = $(TESTSRC) + @echo TLIBS = $(TLIBS) + @echo TOP = $(TOP) + @echo UCRTLIBPATH = $(UCRTLIBPATH) + @echo USE_AMALGAMATION = $(USE_AMALGAMATION) + @echo USE_CRT_DLL = $(USE_CRT_DLL) + @echo USE_FATAL_WARN = $(USE_FATAL_WARN) + @echo USE_FULLWARN = $(USE_FULLWARN) + @echo USE_ICU = $(USE_ICU) + @echo USE_LISTINGS = $(USE_LISTINGS) + @echo USE_NATIVE_LIBPATHS = $(USE_NATIVE_LIBPATHS) + @echo USE_RC = $(USE_RC) + @echo USE_RUNTIME_CHECKS = $(USE_RUNTIME_CHECKS) + @echo USE_SEH = $(USE_SEH) + @echo USE_STDCALL = $(USE_STDCALL) + @echo USE_ZLIB = $(USE_ZLIB) + @echo XCOMPILE = $(XCOMPILE) + @echo ZLIBDIR = $(ZLIBDIR) + @echo ZLIBINCDIR = $(ZLIBINCDIR) + @echo ZLIBLIBDIR = $(ZLIBLIBDIR) + @echo ZLIBLIB = $(ZLIBLIB) + +moreclean: clean + del /Q $(SQLITE3C) $(SQLITE3H) 2>NUL # <> clean: del /Q *.exp *.lo *.ilk *.lib *.obj *.ncb *.pdb *.sdf *.suo 2>NUL del /Q *.bsc *.cod *.da *.bb *.bbg *.vc gmon.out 2>NUL + del /Q sqlite3.def tclsqlite3.def ctime.c pragma.h 2>NUL + del /Q $(SQLITE3EXE) $(SQLITE3DLL) Replace.exe 2>NUL # <> - del /Q $(SQLITE3C) $(SQLITE3H) opcodes.c opcodes.h 2>NUL + del /Q $(SQLITE3TCLDLL) pkgIndex.tcl 2>NUL + del /Q opcodes.c opcodes.h 2>NUL del /Q lemon.* lempar.c parse.* 2>NUL - del /Q mkkeywordhash.* keywordhash.h 2>NUL + del /Q mksourceid.* mkkeywordhash.* keywordhash.h 2>NUL del /Q notasharedlib.* 2>NUL -rmdir /Q/S .deps 2>NUL -rmdir /Q/S .libs 2>NUL - -rmdir /Q/S quota2a 2>NUL - -rmdir /Q/S quota2b 2>NUL - -rmdir /Q/S quota2c 2>NUL -rmdir /Q/S tsrc 2>NUL del /Q .target_source 2>NUL - del /Q tclsqlite3.exe 2>NUL + del /Q tclsqlite3.exe $(SQLITETCLH) $(SQLITETCLDECLSH) 2>NUL + del /Q lsm.dll lsmtest.exe 2>NUL + del /Q atrc.exe changesetfuzz.exe dbtotxt.exe index_usage.exe 2>NUL del /Q testloadext.dll 2>NUL - del /Q testfixture.exe test.db 2>NUL - del /Q LogEst.exe fts3view.exe rollback-test.exe showdb.exe 2>NUL + del /Q testfixture.exe test.db tf.bat 2>NUL + del /Q /S testdir 2>/NUL + -rmdir /Q /S testdir 2>NUL + del /Q LogEst.exe fts3view.exe rollback-test.exe showdb.exe dbdump.exe 2>NUL + del /Q changeset.exe 2>NUL del /Q showjournal.exe showstat4.exe showwal.exe speedtest1.exe 2>NUL del /Q mptester.exe wordcount.exe rbu.exe srcck1.exe 2>NUL - del /Q $(SQLITE3EXE) $(SQLITE3DLL) sqlite3.def 2>NUL - del /Q sqlite3.c sqlite3-*.c 2>NUL + del /Q sqlite3.c sqlite3-*.c sqlite3.h 2>NUL del /Q sqlite3rc.h 2>NUL - del /Q shell.c sqlite3ext.h 2>NUL - del /Q sqlite3_analyzer.exe sqlite3_analyzer.c 2>NUL + del /Q shell.c sqlite3ext.h sqlite3session.h 2>NUL + del /Q sqlite3_analyzer.exe sqlite3_analyzer.c sqlite3_rsync.exe 2>NUL del /Q sqlite-*-output.vsix 2>NUL - del /Q fuzzershell.exe fuzzcheck.exe sqldiff.exe 2>NUL + del /Q fuzzershell.exe fuzzcheck.exe sqldiff.exe dbhash.exe 2>NUL + del /Q sqltclsh.* 2>NUL + del /Q dbfuzz.exe sessionfuzz.exe threadtest3.exe 2>NUL + del /Q kvtest.exe ossshell.exe scrub.exe 2>NUL + del /Q showshm.exe sqlite3_checker.* sqlite3_expert.exe 2>NUL del /Q fts5.* fts5parse.* 2>NUL + del /q src-verify.exe 2>NUL + del /q jimsh.exe jimsh0.exe 2>NUL # <> diff --git a/README.md b/README.md index a3abad40f4..b18f572325 100644 --- a/README.md +++ b/README.md @@ -1,98 +1,115 @@ ## SQLCipher -SQLCipher is an SQLite extension that provides transparent 256-bit AES encryption of -database files. Pages are encrypted before being written to disk and are decrypted -when read back. Due to the small footprint and great performance it’s ideal for -protecting embedded application databases and is well suited for mobile development. +SQLCipher is a standalone fork of the [SQLite](https://www.sqlite.org/) database library that adds 256 bit AES encryption of database files and other security features like: -The official SQLCipher software site is http://sqlcipher.net +- on-the-fly encryption +- tamper detection +- memory sanitization +- strong key derivation -SQLCipher was initially developed by Stephen Lombardo at Zetetic LLC -(sjlombardo@zetetic.net) as the encrypted database layer for Strip, -an iPhone data vault and password manager (http://getstrip.com). +SQLCipher is based on SQLite and stable upstream release features are periodically integrated. While SQLCipher is maintained as a separate version of the source tree, the project minimizes alterations to core SQLite code whenever possible. + +SQLCipher is maintained by Zetetic, LLC, and additional information and documentation is available on the official [SQLCipher site](https://www.zetetic.net/sqlcipher/). ## Features - Fast performance with as little as 5-15% overhead for encryption on many operations - 100% of data in the database file is encrypted -- Good security practices (CBC mode, key derivation) +- Good security practices (CBC mode, HMAC, key derivation) - Zero-configuration and application level cryptography -- Algorithms provided by the peer reviewed OpenSSL crypto library. -- Configurable crypto providers +- Support for multiple cryptographic providers -## Contributions +## Compatibility -We welcome contributions, to contribute to SQLCipher, a [contributor agreement](https://www.zetetic.net/contributions/) needs to be submitted. All submissions should be based on the `prerelease` branch. +SQLCipher maintains database format compatibility within the same major version number so an application on any platform can open databases created by any other application provided the major version of SQLCipher is the same between them. However, major version updates (e.g. from 3.x to 4.x) often include changes to default settings. This means that newer major versions of SQLCipher will not open databases created by older versions without using special settings. For example, SQLCipher 4 introduces many new performance and security enhancements. The new default algorithms, increased KDF iterations, and larger page size mean that SQLCipher 4 will not open databases created by SQLCipher 1.x, 2.x, or 3.x by default. Instead, an application would either need to migrate the older databases to use the new format or enable a special backwards-compatibility mode. The available options are described in SQLCipher's [upgrade documentation](https://discuss.zetetic.net/t/upgrading-to-sqlcipher-4/3283). -If you are reading this on a Git mirror someplace, you are doing it wrong. -The [official repository](https://www.sqlite.org/src/) is better. Go there -now. +SQLCipher is also compatible with standard SQLite databases. When a key is not provided, SQLCipher will behave just like the standard SQLite library. It is also possible to convert from a plaintext database (standard SQLite) to an encrypted SQLCipher database using [ATTACH and the sqlcipher_export() convenience function](https://discuss.zetetic.net/t/how-to-encrypt-a-plaintext-sqlite-database-to-use-sqlcipher-and-avoid-file-is-encrypted-or-is-not-a-database-errors/868). + +## Contributions + +The SQLCipher team welcomes contributions to the core library. All contributions including pull requests and patches should be based on the `prerelease` branch, and must be accompanied by a [contributor agreement](https://www.zetetic.net/contributions/). We strongly encourage [discussion](https://discuss.zetetic.net/c/sqlcipher) of the proposed change prior to development and submission. ## Compiling -Building SQLCipher is almost the same as compiling a regular version of -SQLite with two small exceptions: +Building SQLCipher is similar to compiling a regular version of SQLite from source, with a few small exceptions. You must: - 1. You *must* define SQLITE_HAS_CODEC and SQLITE_TEMP_STORE=2 when building sqlcipher. - 2. You need to link against a OpenSSL's libcrypto + 1. define `SQLITE_HAS_CODEC` + 2. define `SQLITE_TEMP_STORE=2` or `SQLITE_TEMP_STORE=3` (or use `configure`'s --with-tempstore=yes option) + 3. define `SQLITE_EXTRA_INIT=sqlcipher_extra_init` and `SQLITE_EXTRA_SHUTDOWN=sqlcipher_extra_shutdown` + 4. define `SQLITE_THREADSAFE` to `1` or `2` (enabled automatically by `configure`) + 2. compile and link with a supported cryptographic provider (OpenSSL, LibTomCrypt, CommonCrypto/Security.framework, or NSS) -Example Static linking (replace /opt/local/lib with the path to libcrypto.a). Note in this -example, --enable-tempstore=yes is setting SQLITE_TEMP_STORE=2 for the build. +The following examples demonstrate use of OpenSSL, which is a readily available provider on most Unix-like systems. Note that, in this example, `--with-tempstore=yes` is setting `SQLITE_TEMP_STORE=2` for the build, and `SQLITE_THREADSAFE` has a default value of `1`. + +``` +$ ./configure --with-tempstore=yes CFLAGS="-DSQLITE_HAS_CODEC -DSQLITE_EXTRA_INIT=sqlcipher_extra_init -DSQLITE_EXTRA_SHUTDOWN=sqlcipher_extra_shutdown" \ + LDFLAGS="-lcrypto" +$ make +``` + +## Testing - $ ./configure --enable-tempstore=yes CFLAGS="-DSQLITE_HAS_CODEC" \ - LDFLAGS="/opt/local/lib/libcrypto.a" - $ make +The full SQLite test suite will not complete successfully when using SQLCipher. In some cases encryption interferes with low-level tests that require access to database file data or features which are unsupported by SQLCipher. Those tests that are intended to support encryption are intended for non-SQLCipher implementations. In addition, because SQLite tests are not always isolated, if one test fails it can trigger a domino effect with other failures in later steps. -Example Dynamic linking +As a result, the SQLCipher package includes it's own independent tests that exercise and verify the core functionality of the SQLCipher extensions. This test suite is intended to provide an abbreviated verification of SQLCipher's internal logic; it does not perform an exhaustive test of the SQLite database system as a whole or verify functionality on specific platforms. Because SQLCipher is based on stable upstream builds of SQLite, it is considered a basic assumption that the core SQLite library code is operating properly (the SQLite core is almost untouched in SQLCipher). Thus, the additional SQLCipher-specific test provide the requisite verification that the library is operating as expected with SQLCipher's security features enabled. - $ ./configure --enable-tempstore=yes CFLAGS="-DSQLITE_HAS_CODEC" \ - LDFLAGS="-lcrypto" - $ make +To run SQLCipher specific tests, configure as described here and run the following to execute the tests and receive a report of the results: + +``` +$ ./configure --with-tempstore=yes --enable-fts5 CFLAGS="-DSQLITE_HAS_CODEC -DSQLITE_EXTRA_INIT=sqlcipher_extra_init -DSQLITE_EXTRA_SHUTDOWN=sqlcipher_extra_shutdown -DSQLCIPHER_TEST" \ + LDFLAGS="-lcrypto" +$ make testfixture +$ ./testfixture test/sqlcipher.test +``` ## Encrypting a database To specify an encryption passphrase for the database via the SQL interface you -use a pragma. The passphrase you enter is passed through PBKDF2 key derivation to +use a PRAGMA. The passphrase you enter is passed through PBKDF2 key derivation to obtain the encryption key for the database PRAGMA key = 'passphrase'; Alternately, you can specify an exact byte sequence using a blob literal. If you -use this method it is your responsibility to ensure that the data you provide a +use this method it is your responsibility to ensure that the data you provide is a 64 character hex string, which will be converted directly to 32 bytes (256 bits) of key data without key derivation. PRAGMA key = "x'2DD29CA851E7B56E4697B0E1F08507293D761A05CE4D1B628663F411A8086D99'"; -To encrypt a database programatically you can use the sqlite3_key function. -The data provided in pKey is converted to an encryption key according to the -same rules as PRAGMA key. +To encrypt a database programmatically you can use the `sqlite3_key` function. +The data provided in `pKey` is converted to an encryption key according to the +same rules as `PRAGMA key`. int sqlite3_key(sqlite3 *db, const void *pKey, int nKey); -PRAGMA key or sqlite3_key should be called as the first operation when a database is open. +`PRAGMA key` or `sqlite3_key` should be called as the first operation when a database is open. ## Changing a database key -To change the encryption passphrase for an existing database you may use the rekey pragma +To change the encryption passphrase for an existing database you may use the rekey PRAGMA after you've supplied the correct database password; PRAGMA key = 'passphrase'; -- start with the existing database passphrase PRAGMA rekey = 'new-passphrase'; -- rekey will reencrypt with the new passphrase -The hexrekey pragma may be used to rekey to a specific binary value +The hex rekey pragma may be used to rekey to a specific binary value PRAGMA rekey = "x'2DD29CA851E7B56E4697B0E1F08507293D761A05CE4D1B628663F411A8086D99'"; -This can be accomplished programtically by using sqlite3_rekey; +This can be accomplished programmatically by using sqlite3_rekey; sqlite3_rekey(sqlite3 *db, const void *pKey, int nKey) ## Support -The primary avenue for support and discussions is the SQLCipher users mailing list: +The primary source for complete documentation (design, API, platforms, usage) is the SQLCipher website: + +https://www.zetetic.net/sqlcipher/documentation + +The primary avenue for support and discussions is the SQLCipher discuss site: -http://groups.google.com/group/sqlcipher +https://discuss.zetetic.net/c/sqlcipher Issues or support questions on using SQLCipher should be entered into the GitHub Issue tracker: @@ -105,9 +122,9 @@ posts about SQLCipher as we do not monitor them frequently. If you are using SQLCipher in your own software please let us know at support@zetetic.net! -## License +## Community Edition Open Source License -Copyright (c) 2008, ZETETIC LLC +Copyright (c) 2025, ZETETIC LLC All rights reserved. Redistribution and use in source and binary forms, with or without @@ -132,13 +149,102 @@ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -## End SQLCipher +# Begin SQLite README.md + +

SQLite Source Repository

+ +This repository contains the complete source code for the +[SQLite database engine](https://sqlite.org/), including +many tests. Additional tests and most documentation +are managed separately. + +See the [on-line documentation](https://sqlite.org/) for more information +about what SQLite is and how it works from a user's perspective. This +README file is about the source code that goes into building SQLite, +not about how SQLite is used. + +## Version Control + +SQLite sources are managed using +[Fossil](https://fossil-scm.org/), a distributed version control system +that was specifically designed and written to support SQLite development. +The [Fossil repository](https://sqlite.org/src/timeline) contains the urtext. + +If you are reading this on GitHub or some other Git repository or service, +then you are looking at a mirror. The names of check-ins and +other artifacts in a Git mirror are different from the official +names for those objects. The official names for check-ins are +found in a footer on the check-in comment for authorized mirrors. +The official check-in name can also be seen in the `manifest.uuid` file +in the root of the tree. Always use the official name, not the +Git-name, when communicating about an SQLite check-in. + +If you pulled your SQLite source code from a secondary source and want to +verify its integrity, there are hints on how to do that in the +[Verifying Code Authenticity](#vauth) section below. + +## Contacting The SQLite Developers + +The preferred way to ask questions or make comments about SQLite or to +report bugs against SQLite is to visit the +[SQLite Forum](https://sqlite.org/forum) at . +Anonymous postings are permitted. + +If you think you have found a bug that has security implications and +you do not want to report it on the public forum, you can send a private +email to drh at sqlite dot org. + +## Public Domain + +The SQLite source code is in the public domain. See + for details. + +Because SQLite is in the public domain, we do not normally accept pull +requests, because if we did take a pull request, the changes in that +pull request might carry a copyright and the SQLite source code would +then no longer be fully in the public domain. + +## Obtaining The SQLite Source Code + +Source code tarballs or ZIP archives are available at: + + * [Latest trunk check-in](https://sqlite.org/src/rchvdwnld/trunk). -This directory contains source code to + * [Latest release](https://sqlite.org/src/rchvdwnld/release) - SQLite: An Embeddable SQL Database Engine + * For other check-ins, browse the + [project timeline](https://sqlite.org/src/timeline?y=ci) and + click on the check-in hash of the check-in you want to download. + On the resulting "info" page, click one of the options to the + right of the "**Downloads:**" label in the "**Overview**" section + near the top. -To compile the project, first create a directory in which to place +To access sources directly using [Fossil](https://fossil-scm.org/home), +first install Fossil version 2.0 or later. +Source tarballs and precompiled binaries for Fossil are available at +. Fossil is +a stand-alone program. To install, simply download or build the single +executable file and put that file someplace on your $PATH or %PATH%. +Then run commands like this: + + mkdir -p ~/sqlite + cd ~/sqlite + fossil open https://sqlite.org/src + +The initial "fossil open" command will take two or three minutes. Afterwards, +you can do fast, bandwidth-efficient updates to the whatever versions +of SQLite you like. Some examples: + + fossil update trunk ;# latest trunk check-in + fossil update release ;# latest official release + fossil update trunk:2024-01-01 ;# First trunk check-in after 2024-01-01 + fossil update version-3.39.0 ;# Version 3.39.0 + +Or type "fossil ui" to get a web-based user interface. + +## Compiling for Unix-like systems + +First create a directory in which to place the build products. It is recommended, but not required, that the build directory be separate from the source directory. Cd into the build directory and then from the build directory run the configure @@ -146,12 +252,42 @@ script found at the root of the source tree. Then run "make". For example: - tar xzf sqlite.tar.gz ;# Unpack the source tree into "sqlite" - mkdir bld ;# Build will occur in a sibling directory - cd bld ;# Change to the build directory - ../sqlite/configure ;# Run the configure script - make ;# Run the makefile. - make install ;# (Optional) Install the build products + apt install gcc make tcl-dev ;# Make sure you have all the necessary build tools + tar xzf sqlite.tar.gz ;# Unpack the source tree into "sqlite" + mkdir bld ;# Build will occur in a sibling directory + cd bld ;# Change to the build directory + ../sqlite/configure ;# Run the configure script + make sqlite3 ;# Builds the "sqlite3" command-line tool + make sqlite3.c ;# Build the "amalgamation" source file + make sqldiff ;# Builds the "sqldiff" command-line tool + # Makefile targets below this point require tcl-dev + make tclextension-install ;# Build and install the SQLite TCL extension + make devtest ;# Run development tests + make releasetest ;# Run full release tests + make sqlite3_analyzer ;# Builds the "sqlite3_analyzer" tool + +See the makefile for additional targets. For debugging builds, the +core developers typically run "configure" with options like this: + + ../sqlite/configure --enable-all --enable-debug CFLAGS='-O0 -g' + +For release builds, the core developers usually do: + + ../sqlite/configure --enable-all + +Almost all makefile targets require a "tclsh" TCL interpreter version 8.6 or +later. The "tclextension-install" target and the test targets that follow +all require TCL development libraries too. ("apt install tcl-dev"). It is +helpful, but is not required, to install the SQLite TCL extension (the +"tclextension-install" target) prior to running tests. The "releasetest" +target has additional requirements, such as "valgrind". + +On "make" command-lines, one can add "OPTIONS=..." to specify additional +compile-time options over and above those set by ./configure. For example, +to compile with the SQLITE_OMIT_DEPRECATED compile-time option, one could say: + + ./configure --enable-all + make OPTIONS=-DSQLITE_OMIT_DEPRECATED sqlite3 The configure script uses autoconf 2.61 and libtool. If the configure script does not work out for you, there is a generic makefile named @@ -159,64 +295,86 @@ script does not work out for you, there is a generic makefile named can copy and edit to suit your needs. Comments on the generic makefile show what changes are needed. -The linux binaries on the website are created using the generic makefile, -not the configure script. The windows binaries on the website are created -using MinGW32 configured as a cross-compiler running under Linux. For -details, see the ./publish.sh script at the top-level of the source tree. -The developers do not use teh configure script. - -SQLite does not require TCL to run, but a TCL installation is required -by the makefiles. SQLite contains a lot of generated code and TCL is -used to do much of that code generation. The makefile also requires -AWK. - -Contacts: - - http://www.sqlite.org/ -======= - mkdir bld - cd bld - nmake /f Makefile.msc TOP=..\sqlite - nmake /f Makefile.msc sqlite3.c TOP=..\sqlite - nmake /f Makefile.msc sqlite3.dll TOP=..\sqlite - nmake /f Makefile.msc sqlite3.exe TOP=..\sqlite - nmake /f Makefile.msc test TOP=..\sqlite - -There are several build options that can be set via the NMAKE command -line. For example, to build for WinRT, simply add "FOR_WINRT=1" argument -to the "sqlite3.dll" command line above. When debugging into the SQLite -code, adding the "DEBUG=1" argument to one of the above command lines is -recommended. - -SQLite does not require [Tcl](http://www.tcl.tk/) to run, but a Tcl installation -is required by the makefiles (including those for MSVC). SQLite contains -a lot of generated code and Tcl is used to do much of that code generation. -The makefiles also require AWK. - -## Source Code Tour - -Most of the core source files are in the **src/** subdirectory. But -src/ also contains files used to build the "testfixture" test harness; -those file all begin with "test". And src/ contains the "shell.c" file -which is the main program for the "sqlite3.exe" command-line shell and -the "tclsqlite.c" file which implements the bindings to SQLite from the -Tcl programming language. (Historical note: SQLite began as a Tcl -extension and only later escaped to the wild as an independent library.) - -Test scripts and programs are found in the **test/** subdirectory. -There are other test suites for SQLite (see -[How SQLite Is Tested](http://www.sqlite.org/testing.html)) -but those other test suites are -in separate source repositories. - -The **ext/** subdirectory contains code for extensions. The -Full-text search engine is in **ext/fts3**. The R-Tree engine is in -**ext/rtree**. The **ext/misc** subdirectory contains a number of -smaller, single-file extensions, such as a REGEXP operator. - -The **tool/** subdirectory contains various scripts and programs used -for building generated source code files or for testing or for generating -accessory programs such as "sqlite3_analyzer(.exe)". +## Compiling for Windows Using MSVC + +On Windows, everything can be compiled with MSVC. +You will also need a working installation of TCL if you want to run tests. +TCL is not required if you just want to build SQLite itself. +See the [compile-for-windows.md](doc/compile-for-windows.md) document for +additional information about how to install MSVC and TCL and configure your +build environment. + +If you want to run tests, you need to let SQLite know the location of your +TCL library, using a command like this: + + set TCLDIR=c:\Tcl + +SQLite uses "tclsh.exe" as part of the build process, and so that +program will need to be somewhere on your %PATH%. SQLite itself +does not contain any TCL code, but it does use TCL to run tests. +You may need to install TCL development +libraries in order to successfully complete some makefile targets. +It is helpful, but is not required, to install the SQLite TCL extension +(the "tclextension-install" target) prior to running tests. + +Build using Makefile.msc. Example: + + nmake /f Makefile.msc sqlite3.exe + nmake /f Makefile.msc sqlite3.c + nmake /f Makefile.msc sqldiff.exe + # Makefile targets below this point require TCL development libraries + nmake /f Makefile.msc tclextension-install + nmake /f Makefile.msc devtest + nmake /f Makefile.msc releasetest + nmake /f Makefile.msc sqlite3_analyzer.exe + +There are many other makefile targets. See comments in Makefile.msc for +details. + +As with the unix Makefile, the OPTIONS=... argument can be passed on the nmake +command-line to enable new compile-time options. For example: + + nmake /f Makefile.msc OPTIONS=-DSQLITE_OMIT_DEPRECATED sqlite3.exe + +## Source Tree Map + + * **src/** - This directory contains the primary source code for the + SQLite core. For historical reasons, C-code used for testing is + also found here. Source files intended for testing begin with "`test`". + The `tclsqlite3.c` and `tclsqlite3.h` files are the TCL interface + for SQLite and are also not part of the core. + + * **test/** - This directory and its subdirectories contains code used + for testing. Files that end in "`.test`" are TCL scripts that run + tests using an augmented TCL interpreter named "testfixture". Use + a command like "`make testfixture`" (unix) or + "`nmake /f Makefile.msc testfixture.exe`" (windows) to build that + augmented TCL interpreter, then run individual tests using commands like + "`testfixture test/main.test`". This test/ subdirectory also contains + additional C code modules and scripts for other kinds of testing. + + * **tool/** - This directory contains programs and scripts used to + build some of the machine-generated code that goes into the SQLite + core, as well as to build and run tests and perform diagnostics. + The source code to [the Lemon parser generator](./doc/lemon.html) is + found here. There are also TCL scripts used to build and/or transform + source code files. For example, the tool/mksqlite3h.tcl script reads + the src/sqlite.h.in file and uses it as a template to construct + the deliverable "sqlite3.h" file that defines the SQLite interface. + + * **ext/** - Various extensions to SQLite are found under this + directory. For example, the FTS5 subsystem is in "ext/fts5/". + Some of these extensions (ex: FTS3/4, FTS5, RTREE) might get built + into the SQLite amalgamation, but not all of them. The + "ext/misc/" subdirectory contains an assortment of one-file extensions, + many of which are omitted from the SQLite core, but which are included + in the [SQLite CLI](https://sqlite.org/cli.html). + + * **doc/** - Some documentation files about SQLite internals are found + here. Note, however, that the primary documentation designed for + application developers and users of SQLite is in a completely separate + repository. Note also that the primary API documentation is derived + from specially constructed comments in the src/sqlite.h.in file. ### Generated Source Code Files @@ -230,8 +388,8 @@ manually-edited files and automatically-generated files. The SQLite interface is defined by the **sqlite3.h** header file, which is generated from src/sqlite.h.in, ./manifest.uuid, and ./VERSION. The -[Tcl script](http://www.tcl.tk) at tool/mksqlite3h.tcl does the conversion. -The manifest.uuid file contains the SHA1 hash of the particular check-in +[Tcl script](https://www.tcl.tk) at tool/mksqlite3h.tcl does the conversion. +The manifest.uuid file contains the SHA3 hash of the particular check-in and is used to generate the SQLITE\_SOURCE\_ID macro. The VERSION file contains the current SQLite version number. The sqlite3.h header is really just a copy of src/sqlite.h.in with the source-id and version number inserted @@ -239,23 +397,19 @@ at just the right spots. Note that comment text in the sqlite3.h file is used to generate much of the SQLite API documentation. The Tcl scripts used to generate that documentation are in a separate source repository. -The SQL language parser is **parse.c** which is generate from a grammar in +The SQL language parser is **parse.c** which is generated from a grammar in the src/parse.y file. The conversion of "parse.y" into "parse.c" is done by the [lemon](./doc/lemon.html) LALR(1) parser generator. The source code -for lemon is at tool/lemon.c. Lemon uses a -template for generating its parser. A generic template is in tool/lempar.c, -but SQLite uses a slightly modified template found in src/lempar.c. - +for lemon is at tool/lemon.c. Lemon uses the tool/lempar.c file as a +template for generating its parser. Lemon also generates the **parse.h** header file, at the same time it -generates parse.c. But the parse.h header file is -modified further (to add additional symbols) using the ./addopcodes.awk -AWK script. +generates parse.c. The **opcodes.h** header file contains macros that define the numbers corresponding to opcodes in the "VDBE" virtual machine. The opcodes.h -file is generated by the scanning the src/vdbe.c source file. The -AWK script at ./mkopcodeh.awk does this scan and generates opcodes.h. -A second AWK script, ./mkopcodec.awk, then scans opcodes.h to generate +file is generated by scanning the src/vdbe.c source file. The +Tcl script at ./mkopcodeh.tcl does this scan and generates opcodes.h. +A second Tcl script, ./mkopcodec.tcl, then scans opcodes.h to generate the **opcodes.c** source file, which contains a reverse mapping from opcode-number to opcode-name that is used for EXPLAIN output. @@ -264,6 +418,13 @@ that maps SQL language keywords (ex: "CREATE", "SELECT", "INDEX", etc.) into the numeric codes used by the parse.c parser. The keywordhash.h file is generated by a C-language program at tool mkkeywordhash.c. +The **pragma.h** header file contains various definitions used to parse +and implement the PRAGMA statements. The header is generated by a +script **tool/mkpragmatab.tcl**. If you want to add a new PRAGMA, edit +the **tool/mkpragmatab.tcl** file to insert the information needed by the +parser for your new PRAGMA, then run the script to regenerate the +**pragma.h** header file. + ### The Amalgamation All of the individual C source code and header files (both manually-edited @@ -281,12 +442,12 @@ subdirectory (using the equivalent of "make target_source") then the tool/mksqlite3c.tcl script is run to copy them all together in just the right order while resolving internal "#include" references. -The amalgamation source file is more than 100K lines long. Some symbolic +The amalgamation source file is more than 200K lines long. Some symbolic debuggers (most notably MSVC) are unable to deal with files longer than 64K lines. To work around this, a separate Tcl script, tool/split-sqlite3c.tcl, can be run on the amalgamation to break it up into a single small C file -called **sqlite3-all.c** that does #include on about five other files -named **sqlite3-1.c**, **sqlite3-2.c**, ..., **sqlite3-5.c**. In this way, +called **sqlite3-all.c** that does #include on about seven other files +named **sqlite3-1.c**, **sqlite3-2.c**, ..., **sqlite3-7.c**. In this way, all of the source code is contained within a single translation unit so that the compiler can do extra cross-procedure optimization, but no individual source file exceeds 32K lines in length. @@ -294,31 +455,39 @@ individual source file exceeds 32K lines in length. ## How It All Fits Together SQLite is modular in design. -See the [architectural description](http://www.sqlite.org/arch.html) +See the [architectural description](https://sqlite.org/arch.html) for details. Other documents that are useful in -(helping to understand how SQLite works include the -[file format](http://www.sqlite.org/fileformat2.html) description, -the [virtual machine](http://www.sqlite.org/vdbe.html) that runs +helping to understand how SQLite works include the +[file format](https://sqlite.org/fileformat2.html) description, +the [virtual machine](https://sqlite.org/opcode.html) that runs prepared statements, the description of -[how transactions work](http://www.sqlite.org/atomiccommit.html), and -the [overview of the query planner](http://www.sqlite.org/optoverview.html). +[how transactions work](https://sqlite.org/atomiccommit.html), and +the [overview of the query planner](https://sqlite.org/optoverview.html). -Unfortunately, years of effort have gone into optimizating SQLite, both +Decades of effort have gone into optimizing SQLite, both for small size and high performance. And optimizations tend to result in -complex code. So there is a lot of complexity in the SQLite implementation. +complex code. So there is a lot of complexity in the current SQLite +implementation. It will not be the easiest library in the world to hack. -Key files: +### Key source code files * **sqlite.h.in** - This file defines the public interface to the SQLite library. Readers will need to be familiar with this interface before - trying to understand how the library works internally. + trying to understand how the library works internally. This file is + really a template that is transformed into the "sqlite3.h" deliverable + using a script invoked by the makefile. * **sqliteInt.h** - this header file defines many of the data objects - used internally by SQLite. + used internally by SQLite. In addition to "sqliteInt.h", some + subsystems inside of sQLite have their own header files. These internal + interfaces are not for use by applications. They can and do change + from one release of SQLite to the next. - * **parse.y** - This file describes the LALR(1) grammer that SQLite uses + * **parse.y** - This file describes the LALR(1) grammar that SQLite uses to parse SQL statements, and the actions that are taken at each step - in the parsing process. + in the parsing process. The file is processed by the + [Lemon Parser Generator](./doc/lemon.html) to produce the actual C code + used for parsing. * **vdbe.c** - This file implements the virtual machine that runs prepared statements. There are various helper files whose names @@ -326,36 +495,85 @@ Key files: which defines internal data objects. The rest of SQLite interacts with the VDBE through an interface defined by vdbe.h. - * **where.c** - This file analyzes the WHERE clause and generates + * **where.c** - This file (together with its helper files named + by "where*.c") analyzes the WHERE clause and generates virtual machine code to run queries efficiently. This file is sometimes called the "query optimizer". It has its own private header file, whereInt.h, that defines data objects used internally. * **btree.c** - This file contains the implementation of the B-Tree - storage engine used by SQLite. + storage engine used by SQLite. The interface to the rest of the system + is defined by "btree.h". The "btreeInt.h" header defines objects + used internally by btree.c and not published to the rest of the system. * **pager.c** - This file contains the "pager" implementation, the - module that implements transactions. + module that implements transactions. The "pager.h" header file + defines the interface between pager.c and the rest of the system. * **os_unix.c** and **os_win.c** - These two files implement the interface between SQLite and the underlying operating system using the run-time pluggable VFS interface. - * **shell.c** - This file is not part of the core SQLite library. This + * **shell.c.in** - This file is not part of the core SQLite library. This is the file that, when linked against sqlite3.a, generates the - "sqlite3.exe" command-line shell. + "sqlite3.exe" command-line shell. The "shell.c.in" file is transformed + into "shell.c" as part of the build process. * **tclsqlite.c** - This file implements the Tcl bindings for SQLite. It is not part of the core SQLite library. But as most of the tests in this repository are written in Tcl, the Tcl language bindings are important. -There are many other source files. Each has a suscinct header comment that + * **test\*.c** - Files in the src/ folder that begin with "test" go into + building the "testfixture.exe" program. The testfixture.exe program is + an enhanced Tcl shell. The testfixture.exe program runs scripts in the + test/ folder to validate the core SQLite code. The testfixture program + (and some other test programs too) is built and run when you type + "make test". + + * **VERSION**, **manifest**, and **manifest.uuid** - These files define + the current SQLite version number. The "VERSION" file is human generated, + but the "manifest" and "manifest.uuid" files are automatically generated + by the [Fossil version control system](https://fossil-scm.org/). + +There are many other source files. Each has a succinct header comment that describes its purpose and role within the larger system. + +## Verifying Code Authenticity + +The `manifest` file at the root directory of the source tree +contains either a SHA3-256 hash or a SHA1 hash +for every source file in the repository. +The name of the version of the entire source tree is just the +SHA3-256 hash of the `manifest` file itself, possibly with the +last line of that file omitted if the last line begins with +"`# Remove this line`". +The `manifest.uuid` file should contain the SHA3-256 hash of the +`manifest` file. If all of the above hash comparisons are correct, then +you can be confident that your source tree is authentic and unadulterated. +Details on the format for the `manifest` files are available +[on the Fossil website](https://fossil-scm.org/home/doc/trunk/www/fileformat.wiki#manifest). + +The process of checking source code authenticity is automated by the +makefile: + +> make verify-source + +Or on windows: + +> nmake /f Makefile.msc verify-source + +Using the makefile to verify source integrity is good for detecting +accidental changes to the source tree, but malicious changes could be +hidden by also modifying the makefiles. ## Contacts -The main SQLite webpage is [http://www.sqlite.org/](http://www.sqlite.org/) -with geographically distributed backup servers at -[http://www2.sqlite.org/](http://www2.sqlite.org) and -[http://www3.sqlite.org/](http://www3.sqlite.org). +The main SQLite website is [https://sqlite.org/](https://sqlite.org/) +with geographically distributed backups at +[https://www2.sqlite.org/](https://www2.sqlite.org) and +[https://www3.sqlite.org/](https://www3.sqlite.org). + +Contact the SQLite developers through the +[SQLite Forum](https://sqlite.org/forum/). In an emergency, you +can send private email to the lead developer at drh at sqlite dot org. diff --git a/SQLITE_LICENSE.md b/SQLITE_LICENSE.md new file mode 100644 index 0000000000..4029dc9e7f --- /dev/null +++ b/SQLITE_LICENSE.md @@ -0,0 +1,79 @@ +The SQLite source code, including all of the files in the directories +listed in the bullets below are +[Public Domain](https://sqlite.org/copyright.html). +The authors have submitted written affidavits releasing their work to +the public for any use. Every byte of the public-domain code can be +traced back to the original authors. The files of this repository +that are public domain include the following: + + * All of the primary SQLite source code files found in the + [src/ directory](https://sqlite.org/src/tree/src?type=tree&expand) + * All of the test cases and testing code in the + [test/ directory](https://sqlite.org/src/tree/test?type=tree&expand) + * All of the SQLite extension source code and test cases in the + [ext/ directory](https://sqlite.org/src/tree/ext?type=tree&expand) + * All code that ends up in the "sqlite3.c" and "sqlite3.h" build products + that actually implement the SQLite RDBMS. + * All of the code used to compile the + [command-line interface](https://sqlite.org/cli.html) + * All of the code used to build various utility programs such as + "sqldiff", "sqlite3_rsync", and "sqlite3_analyzer". + + +The public domain source files usually contain a header comment +similar to the following to make it clear that the software is +public domain. + +> The author disclaims copyright to this source code. In place of +> a legal notice, here is a blessing: +> +> * May you do good and not evil. +> * May you find forgiveness for yourself and forgive others. +> * May you share freely, never taking more than you give. + +Almost every file you find in this source repository will be +public domain. But there are a small number of exceptions: + +Non-Public-Domain Code Included With This Source Repository AS A Convenience +---------------------------------------------------------------------------- + +This repository contains a (relatively) small amount of non-public-domain +code used to help implement the configuration and build logic. In other +words, there are some non-public-domain files used to implement: + +> ./configure && make + +In all cases, the non-public-domain files included with this +repository have generous BSD-style licenses. So anyone is free to +use any of the code in this source repository for any purpose, though +attribution may be required to reuse or republish the configure and +build scripts. None of the non-public-domain code ever actually reaches +the build products, such as "sqlite3.c", however, so no attribution is +required to use SQLite itself. The non-public-domain code consists of +scripts used to help compile SQLite. The non-public-domain code is +technically not part of SQLite. The non-public-domain code is +included in this repository as a convenience to developers, so that those +who want to build SQLite do not need to go download a bunch of +third-party build scripts in order to compile SQLite. + +Non-public-domain code included in this respository includes: + + * The ["autosetup"](http://msteveb.github.io/autosetup/) configuration + system that is contained (mostly) in the autosetup/ directory, but also + includes the "./configure" script at the top-level of this archive. + Autosetup has a separate BSD-style license. See the + [autosetup/LICENSE](http://msteveb.github.io/autosetup/license/) + for details. + + * There are BSD-style licenses on some of the configuration + software found in the legacy autoconf/ directory and its + subdirectories. + +The following unix shell command can be run from the top-level +of this source repository in order to remove all non-public-domain +code: + +> rm -rf configure autosetup autoconf + +If you unpack this source repository and then run the command above, what +is left will be 100% public domain. diff --git a/VERSION b/VERSION index afad818663..0306a564de 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -3.11.0 +3.51.2 diff --git a/aclocal.m4 b/aclocal.m4 deleted file mode 100644 index c4c8a2bc00..0000000000 --- a/aclocal.m4 +++ /dev/null @@ -1,9043 +0,0 @@ -# generated automatically by aclocal 1.15 -*- Autoconf -*- - -# Copyright (C) 1996-2014 Free Software Foundation, Inc. - -# This file is free software; the Free Software Foundation -# gives unlimited permission to copy and/or distribute it, -# with or without modifications, as long as this notice is preserved. - -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY, to the extent permitted by law; without -# even the implied warranty of MERCHANTABILITY or FITNESS FOR A -# PARTICULAR PURPOSE. - -m4_ifndef([AC_CONFIG_MACRO_DIRS], [m4_defun([_AM_CONFIG_MACRO_DIRS], [])m4_defun([AC_CONFIG_MACRO_DIRS], [_AM_CONFIG_MACRO_DIRS($@)])]) -# libtool.m4 - Configure libtool for the host system. -*-Autoconf-*- -# -# Copyright (C) 1996-2001, 2003-2015 Free Software Foundation, Inc. -# Written by Gordon Matzigkeit, 1996 -# -# This file is free software; the Free Software Foundation gives -# unlimited permission to copy and/or distribute it, with or without -# modifications, as long as this notice is preserved. - -m4_define([_LT_COPYING], [dnl -# Copyright (C) 2014 Free Software Foundation, Inc. -# This is free software; see the source for copying conditions. There is NO -# warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - -# GNU Libtool is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of of the License, or -# (at your option) any later version. -# -# As a special exception to the GNU General Public License, if you -# distribute this file as part of a program or library that is built -# using GNU Libtool, you may include this file under the same -# distribution terms that you use for the rest of that program. -# -# GNU Libtool is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . -]) - -# serial 58 LT_INIT - - -# LT_PREREQ(VERSION) -# ------------------ -# Complain and exit if this libtool version is less that VERSION. -m4_defun([LT_PREREQ], -[m4_if(m4_version_compare(m4_defn([LT_PACKAGE_VERSION]), [$1]), -1, - [m4_default([$3], - [m4_fatal([Libtool version $1 or higher is required], - 63)])], - [$2])]) - - -# _LT_CHECK_BUILDDIR -# ------------------ -# Complain if the absolute build directory name contains unusual characters -m4_defun([_LT_CHECK_BUILDDIR], -[case `pwd` in - *\ * | *\ *) - AC_MSG_WARN([Libtool does not cope well with whitespace in `pwd`]) ;; -esac -]) - - -# LT_INIT([OPTIONS]) -# ------------------ -AC_DEFUN([LT_INIT], -[AC_PREREQ([2.62])dnl We use AC_PATH_PROGS_FEATURE_CHECK -AC_REQUIRE([AC_CONFIG_AUX_DIR_DEFAULT])dnl -AC_BEFORE([$0], [LT_LANG])dnl -AC_BEFORE([$0], [LT_OUTPUT])dnl -AC_BEFORE([$0], [LTDL_INIT])dnl -m4_require([_LT_CHECK_BUILDDIR])dnl - -dnl Autoconf doesn't catch unexpanded LT_ macros by default: -m4_pattern_forbid([^_?LT_[A-Z_]+$])dnl -m4_pattern_allow([^(_LT_EOF|LT_DLGLOBAL|LT_DLLAZY_OR_NOW|LT_MULTI_MODULE)$])dnl -dnl aclocal doesn't pull ltoptions.m4, ltsugar.m4, or ltversion.m4 -dnl unless we require an AC_DEFUNed macro: -AC_REQUIRE([LTOPTIONS_VERSION])dnl -AC_REQUIRE([LTSUGAR_VERSION])dnl -AC_REQUIRE([LTVERSION_VERSION])dnl -AC_REQUIRE([LTOBSOLETE_VERSION])dnl -m4_require([_LT_PROG_LTMAIN])dnl - -_LT_SHELL_INIT([SHELL=${CONFIG_SHELL-/bin/sh}]) - -dnl Parse OPTIONS -_LT_SET_OPTIONS([$0], [$1]) - -# This can be used to rebuild libtool when needed -LIBTOOL_DEPS=$ltmain - -# Always use our own libtool. -LIBTOOL='$(SHELL) $(top_builddir)/libtool' -AC_SUBST(LIBTOOL)dnl - -_LT_SETUP - -# Only expand once: -m4_define([LT_INIT]) -])# LT_INIT - -# Old names: -AU_ALIAS([AC_PROG_LIBTOOL], [LT_INIT]) -AU_ALIAS([AM_PROG_LIBTOOL], [LT_INIT]) -dnl aclocal-1.4 backwards compatibility: -dnl AC_DEFUN([AC_PROG_LIBTOOL], []) -dnl AC_DEFUN([AM_PROG_LIBTOOL], []) - - -# _LT_PREPARE_CC_BASENAME -# ----------------------- -m4_defun([_LT_PREPARE_CC_BASENAME], [ -# Calculate cc_basename. Skip known compiler wrappers and cross-prefix. -func_cc_basename () -{ - for cc_temp in @S|@*""; do - case $cc_temp in - compile | *[[\\/]]compile | ccache | *[[\\/]]ccache ) ;; - distcc | *[[\\/]]distcc | purify | *[[\\/]]purify ) ;; - \-*) ;; - *) break;; - esac - done - func_cc_basename_result=`$ECHO "$cc_temp" | $SED "s%.*/%%; s%^$host_alias-%%"` -} -])# _LT_PREPARE_CC_BASENAME - - -# _LT_CC_BASENAME(CC) -# ------------------- -# It would be clearer to call AC_REQUIREs from _LT_PREPARE_CC_BASENAME, -# but that macro is also expanded into generated libtool script, which -# arranges for $SED and $ECHO to be set by different means. -m4_defun([_LT_CC_BASENAME], -[m4_require([_LT_PREPARE_CC_BASENAME])dnl -AC_REQUIRE([_LT_DECL_SED])dnl -AC_REQUIRE([_LT_PROG_ECHO_BACKSLASH])dnl -func_cc_basename $1 -cc_basename=$func_cc_basename_result -]) - - -# _LT_FILEUTILS_DEFAULTS -# ---------------------- -# It is okay to use these file commands and assume they have been set -# sensibly after 'm4_require([_LT_FILEUTILS_DEFAULTS])'. -m4_defun([_LT_FILEUTILS_DEFAULTS], -[: ${CP="cp -f"} -: ${MV="mv -f"} -: ${RM="rm -f"} -])# _LT_FILEUTILS_DEFAULTS - - -# _LT_SETUP -# --------- -m4_defun([_LT_SETUP], -[AC_REQUIRE([AC_CANONICAL_HOST])dnl -AC_REQUIRE([AC_CANONICAL_BUILD])dnl -AC_REQUIRE([_LT_PREPARE_SED_QUOTE_VARS])dnl -AC_REQUIRE([_LT_PROG_ECHO_BACKSLASH])dnl - -_LT_DECL([], [PATH_SEPARATOR], [1], [The PATH separator for the build system])dnl -dnl -_LT_DECL([], [host_alias], [0], [The host system])dnl -_LT_DECL([], [host], [0])dnl -_LT_DECL([], [host_os], [0])dnl -dnl -_LT_DECL([], [build_alias], [0], [The build system])dnl -_LT_DECL([], [build], [0])dnl -_LT_DECL([], [build_os], [0])dnl -dnl -AC_REQUIRE([AC_PROG_CC])dnl -AC_REQUIRE([LT_PATH_LD])dnl -AC_REQUIRE([LT_PATH_NM])dnl -dnl -AC_REQUIRE([AC_PROG_LN_S])dnl -test -z "$LN_S" && LN_S="ln -s" -_LT_DECL([], [LN_S], [1], [Whether we need soft or hard links])dnl -dnl -AC_REQUIRE([LT_CMD_MAX_LEN])dnl -_LT_DECL([objext], [ac_objext], [0], [Object file suffix (normally "o")])dnl -_LT_DECL([], [exeext], [0], [Executable file suffix (normally "")])dnl -dnl -m4_require([_LT_FILEUTILS_DEFAULTS])dnl -m4_require([_LT_CHECK_SHELL_FEATURES])dnl -m4_require([_LT_PATH_CONVERSION_FUNCTIONS])dnl -m4_require([_LT_CMD_RELOAD])dnl -m4_require([_LT_CHECK_MAGIC_METHOD])dnl -m4_require([_LT_CHECK_SHAREDLIB_FROM_LINKLIB])dnl -m4_require([_LT_CMD_OLD_ARCHIVE])dnl -m4_require([_LT_CMD_GLOBAL_SYMBOLS])dnl -m4_require([_LT_WITH_SYSROOT])dnl -m4_require([_LT_CMD_TRUNCATE])dnl - -_LT_CONFIG_LIBTOOL_INIT([ -# See if we are running on zsh, and set the options that allow our -# commands through without removal of \ escapes INIT. -if test -n "\${ZSH_VERSION+set}"; then - setopt NO_GLOB_SUBST -fi -]) -if test -n "${ZSH_VERSION+set}"; then - setopt NO_GLOB_SUBST -fi - -_LT_CHECK_OBJDIR - -m4_require([_LT_TAG_COMPILER])dnl - -case $host_os in -aix3*) - # AIX sometimes has problems with the GCC collect2 program. For some - # reason, if we set the COLLECT_NAMES environment variable, the problems - # vanish in a puff of smoke. - if test set != "${COLLECT_NAMES+set}"; then - COLLECT_NAMES= - export COLLECT_NAMES - fi - ;; -esac - -# Global variables: -ofile=libtool -can_build_shared=yes - -# All known linkers require a '.a' archive for static linking (except MSVC, -# which needs '.lib'). -libext=a - -with_gnu_ld=$lt_cv_prog_gnu_ld - -old_CC=$CC -old_CFLAGS=$CFLAGS - -# Set sane defaults for various variables -test -z "$CC" && CC=cc -test -z "$LTCC" && LTCC=$CC -test -z "$LTCFLAGS" && LTCFLAGS=$CFLAGS -test -z "$LD" && LD=ld -test -z "$ac_objext" && ac_objext=o - -_LT_CC_BASENAME([$compiler]) - -# Only perform the check for file, if the check method requires it -test -z "$MAGIC_CMD" && MAGIC_CMD=file -case $deplibs_check_method in -file_magic*) - if test "$file_magic_cmd" = '$MAGIC_CMD'; then - _LT_PATH_MAGIC - fi - ;; -esac - -# Use C for the default configuration in the libtool script -LT_SUPPORTED_TAG([CC]) -_LT_LANG_C_CONFIG -_LT_LANG_DEFAULT_CONFIG -_LT_CONFIG_COMMANDS -])# _LT_SETUP - - -# _LT_PREPARE_SED_QUOTE_VARS -# -------------------------- -# Define a few sed substitution that help us do robust quoting. -m4_defun([_LT_PREPARE_SED_QUOTE_VARS], -[# Backslashify metacharacters that are still active within -# double-quoted strings. -sed_quote_subst='s/\([["`$\\]]\)/\\\1/g' - -# Same as above, but do not quote variable references. -double_quote_subst='s/\([["`\\]]\)/\\\1/g' - -# Sed substitution to delay expansion of an escaped shell variable in a -# double_quote_subst'ed string. -delay_variable_subst='s/\\\\\\\\\\\$/\\\\\\$/g' - -# Sed substitution to delay expansion of an escaped single quote. -delay_single_quote_subst='s/'\''/'\'\\\\\\\'\''/g' - -# Sed substitution to avoid accidental globbing in evaled expressions -no_glob_subst='s/\*/\\\*/g' -]) - -# _LT_PROG_LTMAIN -# --------------- -# Note that this code is called both from 'configure', and 'config.status' -# now that we use AC_CONFIG_COMMANDS to generate libtool. Notably, -# 'config.status' has no value for ac_aux_dir unless we are using Automake, -# so we pass a copy along to make sure it has a sensible value anyway. -m4_defun([_LT_PROG_LTMAIN], -[m4_ifdef([AC_REQUIRE_AUX_FILE], [AC_REQUIRE_AUX_FILE([ltmain.sh])])dnl -_LT_CONFIG_LIBTOOL_INIT([ac_aux_dir='$ac_aux_dir']) -ltmain=$ac_aux_dir/ltmain.sh -])# _LT_PROG_LTMAIN - - - -# So that we can recreate a full libtool script including additional -# tags, we accumulate the chunks of code to send to AC_CONFIG_COMMANDS -# in macros and then make a single call at the end using the 'libtool' -# label. - - -# _LT_CONFIG_LIBTOOL_INIT([INIT-COMMANDS]) -# ---------------------------------------- -# Register INIT-COMMANDS to be passed to AC_CONFIG_COMMANDS later. -m4_define([_LT_CONFIG_LIBTOOL_INIT], -[m4_ifval([$1], - [m4_append([_LT_OUTPUT_LIBTOOL_INIT], - [$1 -])])]) - -# Initialize. -m4_define([_LT_OUTPUT_LIBTOOL_INIT]) - - -# _LT_CONFIG_LIBTOOL([COMMANDS]) -# ------------------------------ -# Register COMMANDS to be passed to AC_CONFIG_COMMANDS later. -m4_define([_LT_CONFIG_LIBTOOL], -[m4_ifval([$1], - [m4_append([_LT_OUTPUT_LIBTOOL_COMMANDS], - [$1 -])])]) - -# Initialize. -m4_define([_LT_OUTPUT_LIBTOOL_COMMANDS]) - - -# _LT_CONFIG_SAVE_COMMANDS([COMMANDS], [INIT_COMMANDS]) -# ----------------------------------------------------- -m4_defun([_LT_CONFIG_SAVE_COMMANDS], -[_LT_CONFIG_LIBTOOL([$1]) -_LT_CONFIG_LIBTOOL_INIT([$2]) -]) - - -# _LT_FORMAT_COMMENT([COMMENT]) -# ----------------------------- -# Add leading comment marks to the start of each line, and a trailing -# full-stop to the whole comment if one is not present already. -m4_define([_LT_FORMAT_COMMENT], -[m4_ifval([$1], [ -m4_bpatsubst([m4_bpatsubst([$1], [^ *], [# ])], - [['`$\]], [\\\&])]m4_bmatch([$1], [[!?.]$], [], [.]) -)]) - - - - - -# _LT_DECL([CONFIGNAME], VARNAME, VALUE, [DESCRIPTION], [IS-TAGGED?]) -# ------------------------------------------------------------------- -# CONFIGNAME is the name given to the value in the libtool script. -# VARNAME is the (base) name used in the configure script. -# VALUE may be 0, 1 or 2 for a computed quote escaped value based on -# VARNAME. Any other value will be used directly. -m4_define([_LT_DECL], -[lt_if_append_uniq([lt_decl_varnames], [$2], [, ], - [lt_dict_add_subkey([lt_decl_dict], [$2], [libtool_name], - [m4_ifval([$1], [$1], [$2])]) - lt_dict_add_subkey([lt_decl_dict], [$2], [value], [$3]) - m4_ifval([$4], - [lt_dict_add_subkey([lt_decl_dict], [$2], [description], [$4])]) - lt_dict_add_subkey([lt_decl_dict], [$2], - [tagged?], [m4_ifval([$5], [yes], [no])])]) -]) - - -# _LT_TAGDECL([CONFIGNAME], VARNAME, VALUE, [DESCRIPTION]) -# -------------------------------------------------------- -m4_define([_LT_TAGDECL], [_LT_DECL([$1], [$2], [$3], [$4], [yes])]) - - -# lt_decl_tag_varnames([SEPARATOR], [VARNAME1...]) -# ------------------------------------------------ -m4_define([lt_decl_tag_varnames], -[_lt_decl_filter([tagged?], [yes], $@)]) - - -# _lt_decl_filter(SUBKEY, VALUE, [SEPARATOR], [VARNAME1..]) -# --------------------------------------------------------- -m4_define([_lt_decl_filter], -[m4_case([$#], - [0], [m4_fatal([$0: too few arguments: $#])], - [1], [m4_fatal([$0: too few arguments: $#: $1])], - [2], [lt_dict_filter([lt_decl_dict], [$1], [$2], [], lt_decl_varnames)], - [3], [lt_dict_filter([lt_decl_dict], [$1], [$2], [$3], lt_decl_varnames)], - [lt_dict_filter([lt_decl_dict], $@)])[]dnl -]) - - -# lt_decl_quote_varnames([SEPARATOR], [VARNAME1...]) -# -------------------------------------------------- -m4_define([lt_decl_quote_varnames], -[_lt_decl_filter([value], [1], $@)]) - - -# lt_decl_dquote_varnames([SEPARATOR], [VARNAME1...]) -# --------------------------------------------------- -m4_define([lt_decl_dquote_varnames], -[_lt_decl_filter([value], [2], $@)]) - - -# lt_decl_varnames_tagged([SEPARATOR], [VARNAME1...]) -# --------------------------------------------------- -m4_define([lt_decl_varnames_tagged], -[m4_assert([$# <= 2])dnl -_$0(m4_quote(m4_default([$1], [[, ]])), - m4_ifval([$2], [[$2]], [m4_dquote(lt_decl_tag_varnames)]), - m4_split(m4_normalize(m4_quote(_LT_TAGS)), [ ]))]) -m4_define([_lt_decl_varnames_tagged], -[m4_ifval([$3], [lt_combine([$1], [$2], [_], $3)])]) - - -# lt_decl_all_varnames([SEPARATOR], [VARNAME1...]) -# ------------------------------------------------ -m4_define([lt_decl_all_varnames], -[_$0(m4_quote(m4_default([$1], [[, ]])), - m4_if([$2], [], - m4_quote(lt_decl_varnames), - m4_quote(m4_shift($@))))[]dnl -]) -m4_define([_lt_decl_all_varnames], -[lt_join($@, lt_decl_varnames_tagged([$1], - lt_decl_tag_varnames([[, ]], m4_shift($@))))dnl -]) - - -# _LT_CONFIG_STATUS_DECLARE([VARNAME]) -# ------------------------------------ -# Quote a variable value, and forward it to 'config.status' so that its -# declaration there will have the same value as in 'configure'. VARNAME -# must have a single quote delimited value for this to work. -m4_define([_LT_CONFIG_STATUS_DECLARE], -[$1='`$ECHO "$][$1" | $SED "$delay_single_quote_subst"`']) - - -# _LT_CONFIG_STATUS_DECLARATIONS -# ------------------------------ -# We delimit libtool config variables with single quotes, so when -# we write them to config.status, we have to be sure to quote all -# embedded single quotes properly. In configure, this macro expands -# each variable declared with _LT_DECL (and _LT_TAGDECL) into: -# -# ='`$ECHO "$" | $SED "$delay_single_quote_subst"`' -m4_defun([_LT_CONFIG_STATUS_DECLARATIONS], -[m4_foreach([_lt_var], m4_quote(lt_decl_all_varnames), - [m4_n([_LT_CONFIG_STATUS_DECLARE(_lt_var)])])]) - - -# _LT_LIBTOOL_TAGS -# ---------------- -# Output comment and list of tags supported by the script -m4_defun([_LT_LIBTOOL_TAGS], -[_LT_FORMAT_COMMENT([The names of the tagged configurations supported by this script])dnl -available_tags='_LT_TAGS'dnl -]) - - -# _LT_LIBTOOL_DECLARE(VARNAME, [TAG]) -# ----------------------------------- -# Extract the dictionary values for VARNAME (optionally with TAG) and -# expand to a commented shell variable setting: -# -# # Some comment about what VAR is for. -# visible_name=$lt_internal_name -m4_define([_LT_LIBTOOL_DECLARE], -[_LT_FORMAT_COMMENT(m4_quote(lt_dict_fetch([lt_decl_dict], [$1], - [description])))[]dnl -m4_pushdef([_libtool_name], - m4_quote(lt_dict_fetch([lt_decl_dict], [$1], [libtool_name])))[]dnl -m4_case(m4_quote(lt_dict_fetch([lt_decl_dict], [$1], [value])), - [0], [_libtool_name=[$]$1], - [1], [_libtool_name=$lt_[]$1], - [2], [_libtool_name=$lt_[]$1], - [_libtool_name=lt_dict_fetch([lt_decl_dict], [$1], [value])])[]dnl -m4_ifval([$2], [_$2])[]m4_popdef([_libtool_name])[]dnl -]) - - -# _LT_LIBTOOL_CONFIG_VARS -# ----------------------- -# Produce commented declarations of non-tagged libtool config variables -# suitable for insertion in the LIBTOOL CONFIG section of the 'libtool' -# script. Tagged libtool config variables (even for the LIBTOOL CONFIG -# section) are produced by _LT_LIBTOOL_TAG_VARS. -m4_defun([_LT_LIBTOOL_CONFIG_VARS], -[m4_foreach([_lt_var], - m4_quote(_lt_decl_filter([tagged?], [no], [], lt_decl_varnames)), - [m4_n([_LT_LIBTOOL_DECLARE(_lt_var)])])]) - - -# _LT_LIBTOOL_TAG_VARS(TAG) -# ------------------------- -m4_define([_LT_LIBTOOL_TAG_VARS], -[m4_foreach([_lt_var], m4_quote(lt_decl_tag_varnames), - [m4_n([_LT_LIBTOOL_DECLARE(_lt_var, [$1])])])]) - - -# _LT_TAGVAR(VARNAME, [TAGNAME]) -# ------------------------------ -m4_define([_LT_TAGVAR], [m4_ifval([$2], [$1_$2], [$1])]) - - -# _LT_CONFIG_COMMANDS -# ------------------- -# Send accumulated output to $CONFIG_STATUS. Thanks to the lists of -# variables for single and double quote escaping we saved from calls -# to _LT_DECL, we can put quote escaped variables declarations -# into 'config.status', and then the shell code to quote escape them in -# for loops in 'config.status'. Finally, any additional code accumulated -# from calls to _LT_CONFIG_LIBTOOL_INIT is expanded. -m4_defun([_LT_CONFIG_COMMANDS], -[AC_PROVIDE_IFELSE([LT_OUTPUT], - dnl If the libtool generation code has been placed in $CONFIG_LT, - dnl instead of duplicating it all over again into config.status, - dnl then we will have config.status run $CONFIG_LT later, so it - dnl needs to know what name is stored there: - [AC_CONFIG_COMMANDS([libtool], - [$SHELL $CONFIG_LT || AS_EXIT(1)], [CONFIG_LT='$CONFIG_LT'])], - dnl If the libtool generation code is destined for config.status, - dnl expand the accumulated commands and init code now: - [AC_CONFIG_COMMANDS([libtool], - [_LT_OUTPUT_LIBTOOL_COMMANDS], [_LT_OUTPUT_LIBTOOL_COMMANDS_INIT])]) -])#_LT_CONFIG_COMMANDS - - -# Initialize. -m4_define([_LT_OUTPUT_LIBTOOL_COMMANDS_INIT], -[ - -# The HP-UX ksh and POSIX shell print the target directory to stdout -# if CDPATH is set. -(unset CDPATH) >/dev/null 2>&1 && unset CDPATH - -sed_quote_subst='$sed_quote_subst' -double_quote_subst='$double_quote_subst' -delay_variable_subst='$delay_variable_subst' -_LT_CONFIG_STATUS_DECLARATIONS -LTCC='$LTCC' -LTCFLAGS='$LTCFLAGS' -compiler='$compiler_DEFAULT' - -# A function that is used when there is no print builtin or printf. -func_fallback_echo () -{ - eval 'cat <<_LTECHO_EOF -\$[]1 -_LTECHO_EOF' -} - -# Quote evaled strings. -for var in lt_decl_all_varnames([[ \ -]], lt_decl_quote_varnames); do - case \`eval \\\\\$ECHO \\\\""\\\\\$\$var"\\\\"\` in - *[[\\\\\\\`\\"\\\$]]*) - eval "lt_\$var=\\\\\\"\\\`\\\$ECHO \\"\\\$\$var\\" | \\\$SED \\"\\\$sed_quote_subst\\"\\\`\\\\\\"" ## exclude from sc_prohibit_nested_quotes - ;; - *) - eval "lt_\$var=\\\\\\"\\\$\$var\\\\\\"" - ;; - esac -done - -# Double-quote double-evaled strings. -for var in lt_decl_all_varnames([[ \ -]], lt_decl_dquote_varnames); do - case \`eval \\\\\$ECHO \\\\""\\\\\$\$var"\\\\"\` in - *[[\\\\\\\`\\"\\\$]]*) - eval "lt_\$var=\\\\\\"\\\`\\\$ECHO \\"\\\$\$var\\" | \\\$SED -e \\"\\\$double_quote_subst\\" -e \\"\\\$sed_quote_subst\\" -e \\"\\\$delay_variable_subst\\"\\\`\\\\\\"" ## exclude from sc_prohibit_nested_quotes - ;; - *) - eval "lt_\$var=\\\\\\"\\\$\$var\\\\\\"" - ;; - esac -done - -_LT_OUTPUT_LIBTOOL_INIT -]) - -# _LT_GENERATED_FILE_INIT(FILE, [COMMENT]) -# ------------------------------------ -# Generate a child script FILE with all initialization necessary to -# reuse the environment learned by the parent script, and make the -# file executable. If COMMENT is supplied, it is inserted after the -# '#!' sequence but before initialization text begins. After this -# macro, additional text can be appended to FILE to form the body of -# the child script. The macro ends with non-zero status if the -# file could not be fully written (such as if the disk is full). -m4_ifdef([AS_INIT_GENERATED], -[m4_defun([_LT_GENERATED_FILE_INIT],[AS_INIT_GENERATED($@)])], -[m4_defun([_LT_GENERATED_FILE_INIT], -[m4_require([AS_PREPARE])]dnl -[m4_pushdef([AS_MESSAGE_LOG_FD])]dnl -[lt_write_fail=0 -cat >$1 <<_ASEOF || lt_write_fail=1 -#! $SHELL -# Generated by $as_me. -$2 -SHELL=\${CONFIG_SHELL-$SHELL} -export SHELL -_ASEOF -cat >>$1 <<\_ASEOF || lt_write_fail=1 -AS_SHELL_SANITIZE -_AS_PREPARE -exec AS_MESSAGE_FD>&1 -_ASEOF -test 0 = "$lt_write_fail" && chmod +x $1[]dnl -m4_popdef([AS_MESSAGE_LOG_FD])])])# _LT_GENERATED_FILE_INIT - -# LT_OUTPUT -# --------- -# This macro allows early generation of the libtool script (before -# AC_OUTPUT is called), incase it is used in configure for compilation -# tests. -AC_DEFUN([LT_OUTPUT], -[: ${CONFIG_LT=./config.lt} -AC_MSG_NOTICE([creating $CONFIG_LT]) -_LT_GENERATED_FILE_INIT(["$CONFIG_LT"], -[# Run this file to recreate a libtool stub with the current configuration.]) - -cat >>"$CONFIG_LT" <<\_LTEOF -lt_cl_silent=false -exec AS_MESSAGE_LOG_FD>>config.log -{ - echo - AS_BOX([Running $as_me.]) -} >&AS_MESSAGE_LOG_FD - -lt_cl_help="\ -'$as_me' creates a local libtool stub from the current configuration, -for use in further configure time tests before the real libtool is -generated. - -Usage: $[0] [[OPTIONS]] - - -h, --help print this help, then exit - -V, --version print version number, then exit - -q, --quiet do not print progress messages - -d, --debug don't remove temporary files - -Report bugs to ." - -lt_cl_version="\ -m4_ifset([AC_PACKAGE_NAME], [AC_PACKAGE_NAME ])config.lt[]dnl -m4_ifset([AC_PACKAGE_VERSION], [ AC_PACKAGE_VERSION]) -configured by $[0], generated by m4_PACKAGE_STRING. - -Copyright (C) 2011 Free Software Foundation, Inc. -This config.lt script is free software; the Free Software Foundation -gives unlimited permision to copy, distribute and modify it." - -while test 0 != $[#] -do - case $[1] in - --version | --v* | -V ) - echo "$lt_cl_version"; exit 0 ;; - --help | --h* | -h ) - echo "$lt_cl_help"; exit 0 ;; - --debug | --d* | -d ) - debug=: ;; - --quiet | --q* | --silent | --s* | -q ) - lt_cl_silent=: ;; - - -*) AC_MSG_ERROR([unrecognized option: $[1] -Try '$[0] --help' for more information.]) ;; - - *) AC_MSG_ERROR([unrecognized argument: $[1] -Try '$[0] --help' for more information.]) ;; - esac - shift -done - -if $lt_cl_silent; then - exec AS_MESSAGE_FD>/dev/null -fi -_LTEOF - -cat >>"$CONFIG_LT" <<_LTEOF -_LT_OUTPUT_LIBTOOL_COMMANDS_INIT -_LTEOF - -cat >>"$CONFIG_LT" <<\_LTEOF -AC_MSG_NOTICE([creating $ofile]) -_LT_OUTPUT_LIBTOOL_COMMANDS -AS_EXIT(0) -_LTEOF -chmod +x "$CONFIG_LT" - -# configure is writing to config.log, but config.lt does its own redirection, -# appending to config.log, which fails on DOS, as config.log is still kept -# open by configure. Here we exec the FD to /dev/null, effectively closing -# config.log, so it can be properly (re)opened and appended to by config.lt. -lt_cl_success=: -test yes = "$silent" && - lt_config_lt_args="$lt_config_lt_args --quiet" -exec AS_MESSAGE_LOG_FD>/dev/null -$SHELL "$CONFIG_LT" $lt_config_lt_args || lt_cl_success=false -exec AS_MESSAGE_LOG_FD>>config.log -$lt_cl_success || AS_EXIT(1) -])# LT_OUTPUT - - -# _LT_CONFIG(TAG) -# --------------- -# If TAG is the built-in tag, create an initial libtool script with a -# default configuration from the untagged config vars. Otherwise add code -# to config.status for appending the configuration named by TAG from the -# matching tagged config vars. -m4_defun([_LT_CONFIG], -[m4_require([_LT_FILEUTILS_DEFAULTS])dnl -_LT_CONFIG_SAVE_COMMANDS([ - m4_define([_LT_TAG], m4_if([$1], [], [C], [$1]))dnl - m4_if(_LT_TAG, [C], [ - # See if we are running on zsh, and set the options that allow our - # commands through without removal of \ escapes. - if test -n "${ZSH_VERSION+set}"; then - setopt NO_GLOB_SUBST - fi - - cfgfile=${ofile}T - trap "$RM \"$cfgfile\"; exit 1" 1 2 15 - $RM "$cfgfile" - - cat <<_LT_EOF >> "$cfgfile" -#! $SHELL -# Generated automatically by $as_me ($PACKAGE) $VERSION -# Libtool was configured on host `(hostname || uname -n) 2>/dev/null | sed 1q`: -# NOTE: Changes made to this file will be lost: look at ltmain.sh. - -# Provide generalized library-building support services. -# Written by Gordon Matzigkeit, 1996 - -_LT_COPYING -_LT_LIBTOOL_TAGS - -# Configured defaults for sys_lib_dlsearch_path munging. -: \${LT_SYS_LIBRARY_PATH="$configure_time_lt_sys_library_path"} - -# ### BEGIN LIBTOOL CONFIG -_LT_LIBTOOL_CONFIG_VARS -_LT_LIBTOOL_TAG_VARS -# ### END LIBTOOL CONFIG - -_LT_EOF - - cat <<'_LT_EOF' >> "$cfgfile" - -# ### BEGIN FUNCTIONS SHARED WITH CONFIGURE - -_LT_PREPARE_MUNGE_PATH_LIST -_LT_PREPARE_CC_BASENAME - -# ### END FUNCTIONS SHARED WITH CONFIGURE - -_LT_EOF - - case $host_os in - aix3*) - cat <<\_LT_EOF >> "$cfgfile" -# AIX sometimes has problems with the GCC collect2 program. For some -# reason, if we set the COLLECT_NAMES environment variable, the problems -# vanish in a puff of smoke. -if test set != "${COLLECT_NAMES+set}"; then - COLLECT_NAMES= - export COLLECT_NAMES -fi -_LT_EOF - ;; - esac - - _LT_PROG_LTMAIN - - # We use sed instead of cat because bash on DJGPP gets confused if - # if finds mixed CR/LF and LF-only lines. Since sed operates in - # text mode, it properly converts lines to CR/LF. This bash problem - # is reportedly fixed, but why not run on old versions too? - sed '$q' "$ltmain" >> "$cfgfile" \ - || (rm -f "$cfgfile"; exit 1) - - mv -f "$cfgfile" "$ofile" || - (rm -f "$ofile" && cp "$cfgfile" "$ofile" && rm -f "$cfgfile") - chmod +x "$ofile" -], -[cat <<_LT_EOF >> "$ofile" - -dnl Unfortunately we have to use $1 here, since _LT_TAG is not expanded -dnl in a comment (ie after a #). -# ### BEGIN LIBTOOL TAG CONFIG: $1 -_LT_LIBTOOL_TAG_VARS(_LT_TAG) -# ### END LIBTOOL TAG CONFIG: $1 -_LT_EOF -])dnl /m4_if -], -[m4_if([$1], [], [ - PACKAGE='$PACKAGE' - VERSION='$VERSION' - RM='$RM' - ofile='$ofile'], []) -])dnl /_LT_CONFIG_SAVE_COMMANDS -])# _LT_CONFIG - - -# LT_SUPPORTED_TAG(TAG) -# --------------------- -# Trace this macro to discover what tags are supported by the libtool -# --tag option, using: -# autoconf --trace 'LT_SUPPORTED_TAG:$1' -AC_DEFUN([LT_SUPPORTED_TAG], []) - - -# C support is built-in for now -m4_define([_LT_LANG_C_enabled], []) -m4_define([_LT_TAGS], []) - - -# LT_LANG(LANG) -# ------------- -# Enable libtool support for the given language if not already enabled. -AC_DEFUN([LT_LANG], -[AC_BEFORE([$0], [LT_OUTPUT])dnl -m4_case([$1], - [C], [_LT_LANG(C)], - [C++], [_LT_LANG(CXX)], - [Go], [_LT_LANG(GO)], - [Java], [_LT_LANG(GCJ)], - [Fortran 77], [_LT_LANG(F77)], - [Fortran], [_LT_LANG(FC)], - [Windows Resource], [_LT_LANG(RC)], - [m4_ifdef([_LT_LANG_]$1[_CONFIG], - [_LT_LANG($1)], - [m4_fatal([$0: unsupported language: "$1"])])])dnl -])# LT_LANG - - -# _LT_LANG(LANGNAME) -# ------------------ -m4_defun([_LT_LANG], -[m4_ifdef([_LT_LANG_]$1[_enabled], [], - [LT_SUPPORTED_TAG([$1])dnl - m4_append([_LT_TAGS], [$1 ])dnl - m4_define([_LT_LANG_]$1[_enabled], [])dnl - _LT_LANG_$1_CONFIG($1)])dnl -])# _LT_LANG - - -m4_ifndef([AC_PROG_GO], [ -# NOTE: This macro has been submitted for inclusion into # -# GNU Autoconf as AC_PROG_GO. When it is available in # -# a released version of Autoconf we should remove this # -# macro and use it instead. # -m4_defun([AC_PROG_GO], -[AC_LANG_PUSH(Go)dnl -AC_ARG_VAR([GOC], [Go compiler command])dnl -AC_ARG_VAR([GOFLAGS], [Go compiler flags])dnl -_AC_ARG_VAR_LDFLAGS()dnl -AC_CHECK_TOOL(GOC, gccgo) -if test -z "$GOC"; then - if test -n "$ac_tool_prefix"; then - AC_CHECK_PROG(GOC, [${ac_tool_prefix}gccgo], [${ac_tool_prefix}gccgo]) - fi -fi -if test -z "$GOC"; then - AC_CHECK_PROG(GOC, gccgo, gccgo, false) -fi -])#m4_defun -])#m4_ifndef - - -# _LT_LANG_DEFAULT_CONFIG -# ----------------------- -m4_defun([_LT_LANG_DEFAULT_CONFIG], -[AC_PROVIDE_IFELSE([AC_PROG_CXX], - [LT_LANG(CXX)], - [m4_define([AC_PROG_CXX], defn([AC_PROG_CXX])[LT_LANG(CXX)])]) - -AC_PROVIDE_IFELSE([AC_PROG_F77], - [LT_LANG(F77)], - [m4_define([AC_PROG_F77], defn([AC_PROG_F77])[LT_LANG(F77)])]) - -AC_PROVIDE_IFELSE([AC_PROG_FC], - [LT_LANG(FC)], - [m4_define([AC_PROG_FC], defn([AC_PROG_FC])[LT_LANG(FC)])]) - -dnl The call to [A][M_PROG_GCJ] is quoted like that to stop aclocal -dnl pulling things in needlessly. -AC_PROVIDE_IFELSE([AC_PROG_GCJ], - [LT_LANG(GCJ)], - [AC_PROVIDE_IFELSE([A][M_PROG_GCJ], - [LT_LANG(GCJ)], - [AC_PROVIDE_IFELSE([LT_PROG_GCJ], - [LT_LANG(GCJ)], - [m4_ifdef([AC_PROG_GCJ], - [m4_define([AC_PROG_GCJ], defn([AC_PROG_GCJ])[LT_LANG(GCJ)])]) - m4_ifdef([A][M_PROG_GCJ], - [m4_define([A][M_PROG_GCJ], defn([A][M_PROG_GCJ])[LT_LANG(GCJ)])]) - m4_ifdef([LT_PROG_GCJ], - [m4_define([LT_PROG_GCJ], defn([LT_PROG_GCJ])[LT_LANG(GCJ)])])])])]) - -AC_PROVIDE_IFELSE([AC_PROG_GO], - [LT_LANG(GO)], - [m4_define([AC_PROG_GO], defn([AC_PROG_GO])[LT_LANG(GO)])]) - -AC_PROVIDE_IFELSE([LT_PROG_RC], - [LT_LANG(RC)], - [m4_define([LT_PROG_RC], defn([LT_PROG_RC])[LT_LANG(RC)])]) -])# _LT_LANG_DEFAULT_CONFIG - -# Obsolete macros: -AU_DEFUN([AC_LIBTOOL_CXX], [LT_LANG(C++)]) -AU_DEFUN([AC_LIBTOOL_F77], [LT_LANG(Fortran 77)]) -AU_DEFUN([AC_LIBTOOL_FC], [LT_LANG(Fortran)]) -AU_DEFUN([AC_LIBTOOL_GCJ], [LT_LANG(Java)]) -AU_DEFUN([AC_LIBTOOL_RC], [LT_LANG(Windows Resource)]) -dnl aclocal-1.4 backwards compatibility: -dnl AC_DEFUN([AC_LIBTOOL_CXX], []) -dnl AC_DEFUN([AC_LIBTOOL_F77], []) -dnl AC_DEFUN([AC_LIBTOOL_FC], []) -dnl AC_DEFUN([AC_LIBTOOL_GCJ], []) -dnl AC_DEFUN([AC_LIBTOOL_RC], []) - - -# _LT_TAG_COMPILER -# ---------------- -m4_defun([_LT_TAG_COMPILER], -[AC_REQUIRE([AC_PROG_CC])dnl - -_LT_DECL([LTCC], [CC], [1], [A C compiler])dnl -_LT_DECL([LTCFLAGS], [CFLAGS], [1], [LTCC compiler flags])dnl -_LT_TAGDECL([CC], [compiler], [1], [A language specific compiler])dnl -_LT_TAGDECL([with_gcc], [GCC], [0], [Is the compiler the GNU compiler?])dnl - -# If no C compiler was specified, use CC. -LTCC=${LTCC-"$CC"} - -# If no C compiler flags were specified, use CFLAGS. -LTCFLAGS=${LTCFLAGS-"$CFLAGS"} - -# Allow CC to be a program name with arguments. -compiler=$CC -])# _LT_TAG_COMPILER - - -# _LT_COMPILER_BOILERPLATE -# ------------------------ -# Check for compiler boilerplate output or warnings with -# the simple compiler test code. -m4_defun([_LT_COMPILER_BOILERPLATE], -[m4_require([_LT_DECL_SED])dnl -ac_outfile=conftest.$ac_objext -echo "$lt_simple_compile_test_code" >conftest.$ac_ext -eval "$ac_compile" 2>&1 >/dev/null | $SED '/^$/d; /^ *+/d' >conftest.err -_lt_compiler_boilerplate=`cat conftest.err` -$RM conftest* -])# _LT_COMPILER_BOILERPLATE - - -# _LT_LINKER_BOILERPLATE -# ---------------------- -# Check for linker boilerplate output or warnings with -# the simple link test code. -m4_defun([_LT_LINKER_BOILERPLATE], -[m4_require([_LT_DECL_SED])dnl -ac_outfile=conftest.$ac_objext -echo "$lt_simple_link_test_code" >conftest.$ac_ext -eval "$ac_link" 2>&1 >/dev/null | $SED '/^$/d; /^ *+/d' >conftest.err -_lt_linker_boilerplate=`cat conftest.err` -$RM -r conftest* -])# _LT_LINKER_BOILERPLATE - -# _LT_REQUIRED_DARWIN_CHECKS -# ------------------------- -m4_defun_once([_LT_REQUIRED_DARWIN_CHECKS],[ - case $host_os in - rhapsody* | darwin*) - AC_CHECK_TOOL([DSYMUTIL], [dsymutil], [:]) - AC_CHECK_TOOL([NMEDIT], [nmedit], [:]) - AC_CHECK_TOOL([LIPO], [lipo], [:]) - AC_CHECK_TOOL([OTOOL], [otool], [:]) - AC_CHECK_TOOL([OTOOL64], [otool64], [:]) - _LT_DECL([], [DSYMUTIL], [1], - [Tool to manipulate archived DWARF debug symbol files on Mac OS X]) - _LT_DECL([], [NMEDIT], [1], - [Tool to change global to local symbols on Mac OS X]) - _LT_DECL([], [LIPO], [1], - [Tool to manipulate fat objects and archives on Mac OS X]) - _LT_DECL([], [OTOOL], [1], - [ldd/readelf like tool for Mach-O binaries on Mac OS X]) - _LT_DECL([], [OTOOL64], [1], - [ldd/readelf like tool for 64 bit Mach-O binaries on Mac OS X 10.4]) - - AC_CACHE_CHECK([for -single_module linker flag],[lt_cv_apple_cc_single_mod], - [lt_cv_apple_cc_single_mod=no - if test -z "$LT_MULTI_MODULE"; then - # By default we will add the -single_module flag. You can override - # by either setting the environment variable LT_MULTI_MODULE - # non-empty at configure time, or by adding -multi_module to the - # link flags. - rm -rf libconftest.dylib* - echo "int foo(void){return 1;}" > conftest.c - echo "$LTCC $LTCFLAGS $LDFLAGS -o libconftest.dylib \ --dynamiclib -Wl,-single_module conftest.c" >&AS_MESSAGE_LOG_FD - $LTCC $LTCFLAGS $LDFLAGS -o libconftest.dylib \ - -dynamiclib -Wl,-single_module conftest.c 2>conftest.err - _lt_result=$? - # If there is a non-empty error log, and "single_module" - # appears in it, assume the flag caused a linker warning - if test -s conftest.err && $GREP single_module conftest.err; then - cat conftest.err >&AS_MESSAGE_LOG_FD - # Otherwise, if the output was created with a 0 exit code from - # the compiler, it worked. - elif test -f libconftest.dylib && test 0 = "$_lt_result"; then - lt_cv_apple_cc_single_mod=yes - else - cat conftest.err >&AS_MESSAGE_LOG_FD - fi - rm -rf libconftest.dylib* - rm -f conftest.* - fi]) - - AC_CACHE_CHECK([for -exported_symbols_list linker flag], - [lt_cv_ld_exported_symbols_list], - [lt_cv_ld_exported_symbols_list=no - save_LDFLAGS=$LDFLAGS - echo "_main" > conftest.sym - LDFLAGS="$LDFLAGS -Wl,-exported_symbols_list,conftest.sym" - AC_LINK_IFELSE([AC_LANG_PROGRAM([],[])], - [lt_cv_ld_exported_symbols_list=yes], - [lt_cv_ld_exported_symbols_list=no]) - LDFLAGS=$save_LDFLAGS - ]) - - AC_CACHE_CHECK([for -force_load linker flag],[lt_cv_ld_force_load], - [lt_cv_ld_force_load=no - cat > conftest.c << _LT_EOF -int forced_loaded() { return 2;} -_LT_EOF - echo "$LTCC $LTCFLAGS -c -o conftest.o conftest.c" >&AS_MESSAGE_LOG_FD - $LTCC $LTCFLAGS -c -o conftest.o conftest.c 2>&AS_MESSAGE_LOG_FD - echo "$AR cru libconftest.a conftest.o" >&AS_MESSAGE_LOG_FD - $AR cru libconftest.a conftest.o 2>&AS_MESSAGE_LOG_FD - echo "$RANLIB libconftest.a" >&AS_MESSAGE_LOG_FD - $RANLIB libconftest.a 2>&AS_MESSAGE_LOG_FD - cat > conftest.c << _LT_EOF -int main() { return 0;} -_LT_EOF - echo "$LTCC $LTCFLAGS $LDFLAGS -o conftest conftest.c -Wl,-force_load,./libconftest.a" >&AS_MESSAGE_LOG_FD - $LTCC $LTCFLAGS $LDFLAGS -o conftest conftest.c -Wl,-force_load,./libconftest.a 2>conftest.err - _lt_result=$? - if test -s conftest.err && $GREP force_load conftest.err; then - cat conftest.err >&AS_MESSAGE_LOG_FD - elif test -f conftest && test 0 = "$_lt_result" && $GREP forced_load conftest >/dev/null 2>&1; then - lt_cv_ld_force_load=yes - else - cat conftest.err >&AS_MESSAGE_LOG_FD - fi - rm -f conftest.err libconftest.a conftest conftest.c - rm -rf conftest.dSYM - ]) - case $host_os in - rhapsody* | darwin1.[[012]]) - _lt_dar_allow_undefined='$wl-undefined ${wl}suppress' ;; - darwin1.*) - _lt_dar_allow_undefined='$wl-flat_namespace $wl-undefined ${wl}suppress' ;; - darwin*) # darwin 5.x on - # if running on 10.5 or later, the deployment target defaults - # to the OS version, if on x86, and 10.4, the deployment - # target defaults to 10.4. Don't you love it? - case ${MACOSX_DEPLOYMENT_TARGET-10.0},$host in - 10.0,*86*-darwin8*|10.0,*-darwin[[91]]*) - _lt_dar_allow_undefined='$wl-undefined ${wl}dynamic_lookup' ;; - 10.[[012]][[,.]]*) - _lt_dar_allow_undefined='$wl-flat_namespace $wl-undefined ${wl}suppress' ;; - 10.*) - _lt_dar_allow_undefined='$wl-undefined ${wl}dynamic_lookup' ;; - esac - ;; - esac - if test yes = "$lt_cv_apple_cc_single_mod"; then - _lt_dar_single_mod='$single_module' - fi - if test yes = "$lt_cv_ld_exported_symbols_list"; then - _lt_dar_export_syms=' $wl-exported_symbols_list,$output_objdir/$libname-symbols.expsym' - else - _lt_dar_export_syms='~$NMEDIT -s $output_objdir/$libname-symbols.expsym $lib' - fi - if test : != "$DSYMUTIL" && test no = "$lt_cv_ld_force_load"; then - _lt_dsymutil='~$DSYMUTIL $lib || :' - else - _lt_dsymutil= - fi - ;; - esac -]) - - -# _LT_DARWIN_LINKER_FEATURES([TAG]) -# --------------------------------- -# Checks for linker and compiler features on darwin -m4_defun([_LT_DARWIN_LINKER_FEATURES], -[ - m4_require([_LT_REQUIRED_DARWIN_CHECKS]) - _LT_TAGVAR(archive_cmds_need_lc, $1)=no - _LT_TAGVAR(hardcode_direct, $1)=no - _LT_TAGVAR(hardcode_automatic, $1)=yes - _LT_TAGVAR(hardcode_shlibpath_var, $1)=unsupported - if test yes = "$lt_cv_ld_force_load"; then - _LT_TAGVAR(whole_archive_flag_spec, $1)='`for conv in $convenience\"\"; do test -n \"$conv\" && new_convenience=\"$new_convenience $wl-force_load,$conv\"; done; func_echo_all \"$new_convenience\"`' - m4_case([$1], [F77], [_LT_TAGVAR(compiler_needs_object, $1)=yes], - [FC], [_LT_TAGVAR(compiler_needs_object, $1)=yes]) - else - _LT_TAGVAR(whole_archive_flag_spec, $1)='' - fi - _LT_TAGVAR(link_all_deplibs, $1)=yes - _LT_TAGVAR(allow_undefined_flag, $1)=$_lt_dar_allow_undefined - case $cc_basename in - ifort*|nagfor*) _lt_dar_can_shared=yes ;; - *) _lt_dar_can_shared=$GCC ;; - esac - if test yes = "$_lt_dar_can_shared"; then - output_verbose_link_cmd=func_echo_all - _LT_TAGVAR(archive_cmds, $1)="\$CC -dynamiclib \$allow_undefined_flag -o \$lib \$libobjs \$deplibs \$compiler_flags -install_name \$rpath/\$soname \$verstring $_lt_dar_single_mod$_lt_dsymutil" - _LT_TAGVAR(module_cmds, $1)="\$CC \$allow_undefined_flag -o \$lib -bundle \$libobjs \$deplibs \$compiler_flags$_lt_dsymutil" - _LT_TAGVAR(archive_expsym_cmds, $1)="sed 's|^|_|' < \$export_symbols > \$output_objdir/\$libname-symbols.expsym~\$CC -dynamiclib \$allow_undefined_flag -o \$lib \$libobjs \$deplibs \$compiler_flags -install_name \$rpath/\$soname \$verstring $_lt_dar_single_mod$_lt_dar_export_syms$_lt_dsymutil" - _LT_TAGVAR(module_expsym_cmds, $1)="sed -e 's|^|_|' < \$export_symbols > \$output_objdir/\$libname-symbols.expsym~\$CC \$allow_undefined_flag -o \$lib -bundle \$libobjs \$deplibs \$compiler_flags$_lt_dar_export_syms$_lt_dsymutil" - m4_if([$1], [CXX], -[ if test yes != "$lt_cv_apple_cc_single_mod"; then - _LT_TAGVAR(archive_cmds, $1)="\$CC -r -keep_private_externs -nostdlib -o \$lib-master.o \$libobjs~\$CC -dynamiclib \$allow_undefined_flag -o \$lib \$lib-master.o \$deplibs \$compiler_flags -install_name \$rpath/\$soname \$verstring$_lt_dsymutil" - _LT_TAGVAR(archive_expsym_cmds, $1)="sed 's|^|_|' < \$export_symbols > \$output_objdir/\$libname-symbols.expsym~\$CC -r -keep_private_externs -nostdlib -o \$lib-master.o \$libobjs~\$CC -dynamiclib \$allow_undefined_flag -o \$lib \$lib-master.o \$deplibs \$compiler_flags -install_name \$rpath/\$soname \$verstring$_lt_dar_export_syms$_lt_dsymutil" - fi -],[]) - else - _LT_TAGVAR(ld_shlibs, $1)=no - fi -]) - -# _LT_SYS_MODULE_PATH_AIX([TAGNAME]) -# ---------------------------------- -# Links a minimal program and checks the executable -# for the system default hardcoded library path. In most cases, -# this is /usr/lib:/lib, but when the MPI compilers are used -# the location of the communication and MPI libs are included too. -# If we don't find anything, use the default library path according -# to the aix ld manual. -# Store the results from the different compilers for each TAGNAME. -# Allow to override them for all tags through lt_cv_aix_libpath. -m4_defun([_LT_SYS_MODULE_PATH_AIX], -[m4_require([_LT_DECL_SED])dnl -if test set = "${lt_cv_aix_libpath+set}"; then - aix_libpath=$lt_cv_aix_libpath -else - AC_CACHE_VAL([_LT_TAGVAR([lt_cv_aix_libpath_], [$1])], - [AC_LINK_IFELSE([AC_LANG_PROGRAM],[ - lt_aix_libpath_sed='[ - /Import File Strings/,/^$/ { - /^0/ { - s/^0 *\([^ ]*\) *$/\1/ - p - } - }]' - _LT_TAGVAR([lt_cv_aix_libpath_], [$1])=`dump -H conftest$ac_exeext 2>/dev/null | $SED -n -e "$lt_aix_libpath_sed"` - # Check for a 64-bit object if we didn't find anything. - if test -z "$_LT_TAGVAR([lt_cv_aix_libpath_], [$1])"; then - _LT_TAGVAR([lt_cv_aix_libpath_], [$1])=`dump -HX64 conftest$ac_exeext 2>/dev/null | $SED -n -e "$lt_aix_libpath_sed"` - fi],[]) - if test -z "$_LT_TAGVAR([lt_cv_aix_libpath_], [$1])"; then - _LT_TAGVAR([lt_cv_aix_libpath_], [$1])=/usr/lib:/lib - fi - ]) - aix_libpath=$_LT_TAGVAR([lt_cv_aix_libpath_], [$1]) -fi -])# _LT_SYS_MODULE_PATH_AIX - - -# _LT_SHELL_INIT(ARG) -# ------------------- -m4_define([_LT_SHELL_INIT], -[m4_divert_text([M4SH-INIT], [$1 -])])# _LT_SHELL_INIT - - - -# _LT_PROG_ECHO_BACKSLASH -# ----------------------- -# Find how we can fake an echo command that does not interpret backslash. -# In particular, with Autoconf 2.60 or later we add some code to the start -# of the generated configure script that will find a shell with a builtin -# printf (that we can use as an echo command). -m4_defun([_LT_PROG_ECHO_BACKSLASH], -[ECHO='\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\' -ECHO=$ECHO$ECHO$ECHO$ECHO$ECHO -ECHO=$ECHO$ECHO$ECHO$ECHO$ECHO$ECHO - -AC_MSG_CHECKING([how to print strings]) -# Test print first, because it will be a builtin if present. -if test "X`( print -r -- -n ) 2>/dev/null`" = X-n && \ - test "X`print -r -- $ECHO 2>/dev/null`" = "X$ECHO"; then - ECHO='print -r --' -elif test "X`printf %s $ECHO 2>/dev/null`" = "X$ECHO"; then - ECHO='printf %s\n' -else - # Use this function as a fallback that always works. - func_fallback_echo () - { - eval 'cat <<_LTECHO_EOF -$[]1 -_LTECHO_EOF' - } - ECHO='func_fallback_echo' -fi - -# func_echo_all arg... -# Invoke $ECHO with all args, space-separated. -func_echo_all () -{ - $ECHO "$*" -} - -case $ECHO in - printf*) AC_MSG_RESULT([printf]) ;; - print*) AC_MSG_RESULT([print -r]) ;; - *) AC_MSG_RESULT([cat]) ;; -esac - -m4_ifdef([_AS_DETECT_SUGGESTED], -[_AS_DETECT_SUGGESTED([ - test -n "${ZSH_VERSION+set}${BASH_VERSION+set}" || ( - ECHO='\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\' - ECHO=$ECHO$ECHO$ECHO$ECHO$ECHO - ECHO=$ECHO$ECHO$ECHO$ECHO$ECHO$ECHO - PATH=/empty FPATH=/empty; export PATH FPATH - test "X`printf %s $ECHO`" = "X$ECHO" \ - || test "X`print -r -- $ECHO`" = "X$ECHO" )])]) - -_LT_DECL([], [SHELL], [1], [Shell to use when invoking shell scripts]) -_LT_DECL([], [ECHO], [1], [An echo program that protects backslashes]) -])# _LT_PROG_ECHO_BACKSLASH - - -# _LT_WITH_SYSROOT -# ---------------- -AC_DEFUN([_LT_WITH_SYSROOT], -[AC_MSG_CHECKING([for sysroot]) -AC_ARG_WITH([sysroot], -[AS_HELP_STRING([--with-sysroot@<:@=DIR@:>@], - [Search for dependent libraries within DIR (or the compiler's sysroot - if not specified).])], -[], [with_sysroot=no]) - -dnl lt_sysroot will always be passed unquoted. We quote it here -dnl in case the user passed a directory name. -lt_sysroot= -case $with_sysroot in #( - yes) - if test yes = "$GCC"; then - lt_sysroot=`$CC --print-sysroot 2>/dev/null` - fi - ;; #( - /*) - lt_sysroot=`echo "$with_sysroot" | sed -e "$sed_quote_subst"` - ;; #( - no|'') - ;; #( - *) - AC_MSG_RESULT([$with_sysroot]) - AC_MSG_ERROR([The sysroot must be an absolute path.]) - ;; -esac - - AC_MSG_RESULT([${lt_sysroot:-no}]) -_LT_DECL([], [lt_sysroot], [0], [The root where to search for ]dnl -[dependent libraries, and where our libraries should be installed.])]) - -# _LT_ENABLE_LOCK -# --------------- -m4_defun([_LT_ENABLE_LOCK], -[AC_ARG_ENABLE([libtool-lock], - [AS_HELP_STRING([--disable-libtool-lock], - [avoid locking (might break parallel builds)])]) -test no = "$enable_libtool_lock" || enable_libtool_lock=yes - -# Some flags need to be propagated to the compiler or linker for good -# libtool support. -case $host in -ia64-*-hpux*) - # Find out what ABI is being produced by ac_compile, and set mode - # options accordingly. - echo 'int i;' > conftest.$ac_ext - if AC_TRY_EVAL(ac_compile); then - case `/usr/bin/file conftest.$ac_objext` in - *ELF-32*) - HPUX_IA64_MODE=32 - ;; - *ELF-64*) - HPUX_IA64_MODE=64 - ;; - esac - fi - rm -rf conftest* - ;; -*-*-irix6*) - # Find out what ABI is being produced by ac_compile, and set linker - # options accordingly. - echo '[#]line '$LINENO' "configure"' > conftest.$ac_ext - if AC_TRY_EVAL(ac_compile); then - if test yes = "$lt_cv_prog_gnu_ld"; then - case `/usr/bin/file conftest.$ac_objext` in - *32-bit*) - LD="${LD-ld} -melf32bsmip" - ;; - *N32*) - LD="${LD-ld} -melf32bmipn32" - ;; - *64-bit*) - LD="${LD-ld} -melf64bmip" - ;; - esac - else - case `/usr/bin/file conftest.$ac_objext` in - *32-bit*) - LD="${LD-ld} -32" - ;; - *N32*) - LD="${LD-ld} -n32" - ;; - *64-bit*) - LD="${LD-ld} -64" - ;; - esac - fi - fi - rm -rf conftest* - ;; - -mips64*-*linux*) - # Find out what ABI is being produced by ac_compile, and set linker - # options accordingly. - echo '[#]line '$LINENO' "configure"' > conftest.$ac_ext - if AC_TRY_EVAL(ac_compile); then - emul=elf - case `/usr/bin/file conftest.$ac_objext` in - *32-bit*) - emul="${emul}32" - ;; - *64-bit*) - emul="${emul}64" - ;; - esac - case `/usr/bin/file conftest.$ac_objext` in - *MSB*) - emul="${emul}btsmip" - ;; - *LSB*) - emul="${emul}ltsmip" - ;; - esac - case `/usr/bin/file conftest.$ac_objext` in - *N32*) - emul="${emul}n32" - ;; - esac - LD="${LD-ld} -m $emul" - fi - rm -rf conftest* - ;; - -x86_64-*kfreebsd*-gnu|x86_64-*linux*|powerpc*-*linux*| \ -s390*-*linux*|s390*-*tpf*|sparc*-*linux*) - # Find out what ABI is being produced by ac_compile, and set linker - # options accordingly. Note that the listed cases only cover the - # situations where additional linker options are needed (such as when - # doing 32-bit compilation for a host where ld defaults to 64-bit, or - # vice versa); the common cases where no linker options are needed do - # not appear in the list. - echo 'int i;' > conftest.$ac_ext - if AC_TRY_EVAL(ac_compile); then - case `/usr/bin/file conftest.o` in - *32-bit*) - case $host in - x86_64-*kfreebsd*-gnu) - LD="${LD-ld} -m elf_i386_fbsd" - ;; - x86_64-*linux*) - case `/usr/bin/file conftest.o` in - *x86-64*) - LD="${LD-ld} -m elf32_x86_64" - ;; - *) - LD="${LD-ld} -m elf_i386" - ;; - esac - ;; - powerpc64le-*linux*) - LD="${LD-ld} -m elf32lppclinux" - ;; - powerpc64-*linux*) - LD="${LD-ld} -m elf32ppclinux" - ;; - s390x-*linux*) - LD="${LD-ld} -m elf_s390" - ;; - sparc64-*linux*) - LD="${LD-ld} -m elf32_sparc" - ;; - esac - ;; - *64-bit*) - case $host in - x86_64-*kfreebsd*-gnu) - LD="${LD-ld} -m elf_x86_64_fbsd" - ;; - x86_64-*linux*) - LD="${LD-ld} -m elf_x86_64" - ;; - powerpcle-*linux*) - LD="${LD-ld} -m elf64lppc" - ;; - powerpc-*linux*) - LD="${LD-ld} -m elf64ppc" - ;; - s390*-*linux*|s390*-*tpf*) - LD="${LD-ld} -m elf64_s390" - ;; - sparc*-*linux*) - LD="${LD-ld} -m elf64_sparc" - ;; - esac - ;; - esac - fi - rm -rf conftest* - ;; - -*-*-sco3.2v5*) - # On SCO OpenServer 5, we need -belf to get full-featured binaries. - SAVE_CFLAGS=$CFLAGS - CFLAGS="$CFLAGS -belf" - AC_CACHE_CHECK([whether the C compiler needs -belf], lt_cv_cc_needs_belf, - [AC_LANG_PUSH(C) - AC_LINK_IFELSE([AC_LANG_PROGRAM([[]],[[]])],[lt_cv_cc_needs_belf=yes],[lt_cv_cc_needs_belf=no]) - AC_LANG_POP]) - if test yes != "$lt_cv_cc_needs_belf"; then - # this is probably gcc 2.8.0, egcs 1.0 or newer; no need for -belf - CFLAGS=$SAVE_CFLAGS - fi - ;; -*-*solaris*) - # Find out what ABI is being produced by ac_compile, and set linker - # options accordingly. - echo 'int i;' > conftest.$ac_ext - if AC_TRY_EVAL(ac_compile); then - case `/usr/bin/file conftest.o` in - *64-bit*) - case $lt_cv_prog_gnu_ld in - yes*) - case $host in - i?86-*-solaris*|x86_64-*-solaris*) - LD="${LD-ld} -m elf_x86_64" - ;; - sparc*-*-solaris*) - LD="${LD-ld} -m elf64_sparc" - ;; - esac - # GNU ld 2.21 introduced _sol2 emulations. Use them if available. - if ${LD-ld} -V | grep _sol2 >/dev/null 2>&1; then - LD=${LD-ld}_sol2 - fi - ;; - *) - if ${LD-ld} -64 -r -o conftest2.o conftest.o >/dev/null 2>&1; then - LD="${LD-ld} -64" - fi - ;; - esac - ;; - esac - fi - rm -rf conftest* - ;; -esac - -need_locks=$enable_libtool_lock -])# _LT_ENABLE_LOCK - - -# _LT_PROG_AR -# ----------- -m4_defun([_LT_PROG_AR], -[AC_CHECK_TOOLS(AR, [ar], false) -: ${AR=ar} -: ${AR_FLAGS=cru} -_LT_DECL([], [AR], [1], [The archiver]) -_LT_DECL([], [AR_FLAGS], [1], [Flags to create an archive]) - -AC_CACHE_CHECK([for archiver @FILE support], [lt_cv_ar_at_file], - [lt_cv_ar_at_file=no - AC_COMPILE_IFELSE([AC_LANG_PROGRAM], - [echo conftest.$ac_objext > conftest.lst - lt_ar_try='$AR $AR_FLAGS libconftest.a @conftest.lst >&AS_MESSAGE_LOG_FD' - AC_TRY_EVAL([lt_ar_try]) - if test 0 -eq "$ac_status"; then - # Ensure the archiver fails upon bogus file names. - rm -f conftest.$ac_objext libconftest.a - AC_TRY_EVAL([lt_ar_try]) - if test 0 -ne "$ac_status"; then - lt_cv_ar_at_file=@ - fi - fi - rm -f conftest.* libconftest.a - ]) - ]) - -if test no = "$lt_cv_ar_at_file"; then - archiver_list_spec= -else - archiver_list_spec=$lt_cv_ar_at_file -fi -_LT_DECL([], [archiver_list_spec], [1], - [How to feed a file listing to the archiver]) -])# _LT_PROG_AR - - -# _LT_CMD_OLD_ARCHIVE -# ------------------- -m4_defun([_LT_CMD_OLD_ARCHIVE], -[_LT_PROG_AR - -AC_CHECK_TOOL(STRIP, strip, :) -test -z "$STRIP" && STRIP=: -_LT_DECL([], [STRIP], [1], [A symbol stripping program]) - -AC_CHECK_TOOL(RANLIB, ranlib, :) -test -z "$RANLIB" && RANLIB=: -_LT_DECL([], [RANLIB], [1], - [Commands used to install an old-style archive]) - -# Determine commands to create old-style static archives. -old_archive_cmds='$AR $AR_FLAGS $oldlib$oldobjs' -old_postinstall_cmds='chmod 644 $oldlib' -old_postuninstall_cmds= - -if test -n "$RANLIB"; then - case $host_os in - bitrig* | openbsd*) - old_postinstall_cmds="$old_postinstall_cmds~\$RANLIB -t \$tool_oldlib" - ;; - *) - old_postinstall_cmds="$old_postinstall_cmds~\$RANLIB \$tool_oldlib" - ;; - esac - old_archive_cmds="$old_archive_cmds~\$RANLIB \$tool_oldlib" -fi - -case $host_os in - darwin*) - lock_old_archive_extraction=yes ;; - *) - lock_old_archive_extraction=no ;; -esac -_LT_DECL([], [old_postinstall_cmds], [2]) -_LT_DECL([], [old_postuninstall_cmds], [2]) -_LT_TAGDECL([], [old_archive_cmds], [2], - [Commands used to build an old-style archive]) -_LT_DECL([], [lock_old_archive_extraction], [0], - [Whether to use a lock for old archive extraction]) -])# _LT_CMD_OLD_ARCHIVE - - -# _LT_COMPILER_OPTION(MESSAGE, VARIABLE-NAME, FLAGS, -# [OUTPUT-FILE], [ACTION-SUCCESS], [ACTION-FAILURE]) -# ---------------------------------------------------------------- -# Check whether the given compiler option works -AC_DEFUN([_LT_COMPILER_OPTION], -[m4_require([_LT_FILEUTILS_DEFAULTS])dnl -m4_require([_LT_DECL_SED])dnl -AC_CACHE_CHECK([$1], [$2], - [$2=no - m4_if([$4], , [ac_outfile=conftest.$ac_objext], [ac_outfile=$4]) - echo "$lt_simple_compile_test_code" > conftest.$ac_ext - lt_compiler_flag="$3" ## exclude from sc_useless_quotes_in_assignment - # Insert the option either (1) after the last *FLAGS variable, or - # (2) before a word containing "conftest.", or (3) at the end. - # Note that $ac_compile itself does not contain backslashes and begins - # with a dollar sign (not a hyphen), so the echo should work correctly. - # The option is referenced via a variable to avoid confusing sed. - lt_compile=`echo "$ac_compile" | $SED \ - -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \ - -e 's: [[^ ]]*conftest\.: $lt_compiler_flag&:; t' \ - -e 's:$: $lt_compiler_flag:'` - (eval echo "\"\$as_me:$LINENO: $lt_compile\"" >&AS_MESSAGE_LOG_FD) - (eval "$lt_compile" 2>conftest.err) - ac_status=$? - cat conftest.err >&AS_MESSAGE_LOG_FD - echo "$as_me:$LINENO: \$? = $ac_status" >&AS_MESSAGE_LOG_FD - if (exit $ac_status) && test -s "$ac_outfile"; then - # The compiler can only warn and ignore the option if not recognized - # So say no if there are warnings other than the usual output. - $ECHO "$_lt_compiler_boilerplate" | $SED '/^$/d' >conftest.exp - $SED '/^$/d; /^ *+/d' conftest.err >conftest.er2 - if test ! -s conftest.er2 || diff conftest.exp conftest.er2 >/dev/null; then - $2=yes - fi - fi - $RM conftest* -]) - -if test yes = "[$]$2"; then - m4_if([$5], , :, [$5]) -else - m4_if([$6], , :, [$6]) -fi -])# _LT_COMPILER_OPTION - -# Old name: -AU_ALIAS([AC_LIBTOOL_COMPILER_OPTION], [_LT_COMPILER_OPTION]) -dnl aclocal-1.4 backwards compatibility: -dnl AC_DEFUN([AC_LIBTOOL_COMPILER_OPTION], []) - - -# _LT_LINKER_OPTION(MESSAGE, VARIABLE-NAME, FLAGS, -# [ACTION-SUCCESS], [ACTION-FAILURE]) -# ---------------------------------------------------- -# Check whether the given linker option works -AC_DEFUN([_LT_LINKER_OPTION], -[m4_require([_LT_FILEUTILS_DEFAULTS])dnl -m4_require([_LT_DECL_SED])dnl -AC_CACHE_CHECK([$1], [$2], - [$2=no - save_LDFLAGS=$LDFLAGS - LDFLAGS="$LDFLAGS $3" - echo "$lt_simple_link_test_code" > conftest.$ac_ext - if (eval $ac_link 2>conftest.err) && test -s conftest$ac_exeext; then - # The linker can only warn and ignore the option if not recognized - # So say no if there are warnings - if test -s conftest.err; then - # Append any errors to the config.log. - cat conftest.err 1>&AS_MESSAGE_LOG_FD - $ECHO "$_lt_linker_boilerplate" | $SED '/^$/d' > conftest.exp - $SED '/^$/d; /^ *+/d' conftest.err >conftest.er2 - if diff conftest.exp conftest.er2 >/dev/null; then - $2=yes - fi - else - $2=yes - fi - fi - $RM -r conftest* - LDFLAGS=$save_LDFLAGS -]) - -if test yes = "[$]$2"; then - m4_if([$4], , :, [$4]) -else - m4_if([$5], , :, [$5]) -fi -])# _LT_LINKER_OPTION - -# Old name: -AU_ALIAS([AC_LIBTOOL_LINKER_OPTION], [_LT_LINKER_OPTION]) -dnl aclocal-1.4 backwards compatibility: -dnl AC_DEFUN([AC_LIBTOOL_LINKER_OPTION], []) - - -# LT_CMD_MAX_LEN -#--------------- -AC_DEFUN([LT_CMD_MAX_LEN], -[AC_REQUIRE([AC_CANONICAL_HOST])dnl -# find the maximum length of command line arguments -AC_MSG_CHECKING([the maximum length of command line arguments]) -AC_CACHE_VAL([lt_cv_sys_max_cmd_len], [dnl - i=0 - teststring=ABCD - - case $build_os in - msdosdjgpp*) - # On DJGPP, this test can blow up pretty badly due to problems in libc - # (any single argument exceeding 2000 bytes causes a buffer overrun - # during glob expansion). Even if it were fixed, the result of this - # check would be larger than it should be. - lt_cv_sys_max_cmd_len=12288; # 12K is about right - ;; - - gnu*) - # Under GNU Hurd, this test is not required because there is - # no limit to the length of command line arguments. - # Libtool will interpret -1 as no limit whatsoever - lt_cv_sys_max_cmd_len=-1; - ;; - - cygwin* | mingw* | cegcc*) - # On Win9x/ME, this test blows up -- it succeeds, but takes - # about 5 minutes as the teststring grows exponentially. - # Worse, since 9x/ME are not pre-emptively multitasking, - # you end up with a "frozen" computer, even though with patience - # the test eventually succeeds (with a max line length of 256k). - # Instead, let's just punt: use the minimum linelength reported by - # all of the supported platforms: 8192 (on NT/2K/XP). - lt_cv_sys_max_cmd_len=8192; - ;; - - mint*) - # On MiNT this can take a long time and run out of memory. - lt_cv_sys_max_cmd_len=8192; - ;; - - amigaos*) - # On AmigaOS with pdksh, this test takes hours, literally. - # So we just punt and use a minimum line length of 8192. - lt_cv_sys_max_cmd_len=8192; - ;; - - bitrig* | darwin* | dragonfly* | freebsd* | netbsd* | openbsd*) - # This has been around since 386BSD, at least. Likely further. - if test -x /sbin/sysctl; then - lt_cv_sys_max_cmd_len=`/sbin/sysctl -n kern.argmax` - elif test -x /usr/sbin/sysctl; then - lt_cv_sys_max_cmd_len=`/usr/sbin/sysctl -n kern.argmax` - else - lt_cv_sys_max_cmd_len=65536 # usable default for all BSDs - fi - # And add a safety zone - lt_cv_sys_max_cmd_len=`expr $lt_cv_sys_max_cmd_len \/ 4` - lt_cv_sys_max_cmd_len=`expr $lt_cv_sys_max_cmd_len \* 3` - ;; - - interix*) - # We know the value 262144 and hardcode it with a safety zone (like BSD) - lt_cv_sys_max_cmd_len=196608 - ;; - - os2*) - # The test takes a long time on OS/2. - lt_cv_sys_max_cmd_len=8192 - ;; - - osf*) - # Dr. Hans Ekkehard Plesser reports seeing a kernel panic running configure - # due to this test when exec_disable_arg_limit is 1 on Tru64. It is not - # nice to cause kernel panics so lets avoid the loop below. - # First set a reasonable default. - lt_cv_sys_max_cmd_len=16384 - # - if test -x /sbin/sysconfig; then - case `/sbin/sysconfig -q proc exec_disable_arg_limit` in - *1*) lt_cv_sys_max_cmd_len=-1 ;; - esac - fi - ;; - sco3.2v5*) - lt_cv_sys_max_cmd_len=102400 - ;; - sysv5* | sco5v6* | sysv4.2uw2*) - kargmax=`grep ARG_MAX /etc/conf/cf.d/stune 2>/dev/null` - if test -n "$kargmax"; then - lt_cv_sys_max_cmd_len=`echo $kargmax | sed 's/.*[[ ]]//'` - else - lt_cv_sys_max_cmd_len=32768 - fi - ;; - *) - lt_cv_sys_max_cmd_len=`(getconf ARG_MAX) 2> /dev/null` - if test -n "$lt_cv_sys_max_cmd_len" && \ - test undefined != "$lt_cv_sys_max_cmd_len"; then - lt_cv_sys_max_cmd_len=`expr $lt_cv_sys_max_cmd_len \/ 4` - lt_cv_sys_max_cmd_len=`expr $lt_cv_sys_max_cmd_len \* 3` - else - # Make teststring a little bigger before we do anything with it. - # a 1K string should be a reasonable start. - for i in 1 2 3 4 5 6 7 8; do - teststring=$teststring$teststring - done - SHELL=${SHELL-${CONFIG_SHELL-/bin/sh}} - # If test is not a shell built-in, we'll probably end up computing a - # maximum length that is only half of the actual maximum length, but - # we can't tell. - while { test X`env echo "$teststring$teststring" 2>/dev/null` \ - = "X$teststring$teststring"; } >/dev/null 2>&1 && - test 17 != "$i" # 1/2 MB should be enough - do - i=`expr $i + 1` - teststring=$teststring$teststring - done - # Only check the string length outside the loop. - lt_cv_sys_max_cmd_len=`expr "X$teststring" : ".*" 2>&1` - teststring= - # Add a significant safety factor because C++ compilers can tack on - # massive amounts of additional arguments before passing them to the - # linker. It appears as though 1/2 is a usable value. - lt_cv_sys_max_cmd_len=`expr $lt_cv_sys_max_cmd_len \/ 2` - fi - ;; - esac -]) -if test -n "$lt_cv_sys_max_cmd_len"; then - AC_MSG_RESULT($lt_cv_sys_max_cmd_len) -else - AC_MSG_RESULT(none) -fi -max_cmd_len=$lt_cv_sys_max_cmd_len -_LT_DECL([], [max_cmd_len], [0], - [What is the maximum length of a command?]) -])# LT_CMD_MAX_LEN - -# Old name: -AU_ALIAS([AC_LIBTOOL_SYS_MAX_CMD_LEN], [LT_CMD_MAX_LEN]) -dnl aclocal-1.4 backwards compatibility: -dnl AC_DEFUN([AC_LIBTOOL_SYS_MAX_CMD_LEN], []) - - -# _LT_HEADER_DLFCN -# ---------------- -m4_defun([_LT_HEADER_DLFCN], -[AC_CHECK_HEADERS([dlfcn.h], [], [], [AC_INCLUDES_DEFAULT])dnl -])# _LT_HEADER_DLFCN - - -# _LT_TRY_DLOPEN_SELF (ACTION-IF-TRUE, ACTION-IF-TRUE-W-USCORE, -# ACTION-IF-FALSE, ACTION-IF-CROSS-COMPILING) -# ---------------------------------------------------------------- -m4_defun([_LT_TRY_DLOPEN_SELF], -[m4_require([_LT_HEADER_DLFCN])dnl -if test yes = "$cross_compiling"; then : - [$4] -else - lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2 - lt_status=$lt_dlunknown - cat > conftest.$ac_ext <<_LT_EOF -[#line $LINENO "configure" -#include "confdefs.h" - -#if HAVE_DLFCN_H -#include -#endif - -#include - -#ifdef RTLD_GLOBAL -# define LT_DLGLOBAL RTLD_GLOBAL -#else -# ifdef DL_GLOBAL -# define LT_DLGLOBAL DL_GLOBAL -# else -# define LT_DLGLOBAL 0 -# endif -#endif - -/* We may have to define LT_DLLAZY_OR_NOW in the command line if we - find out it does not work in some platform. */ -#ifndef LT_DLLAZY_OR_NOW -# ifdef RTLD_LAZY -# define LT_DLLAZY_OR_NOW RTLD_LAZY -# else -# ifdef DL_LAZY -# define LT_DLLAZY_OR_NOW DL_LAZY -# else -# ifdef RTLD_NOW -# define LT_DLLAZY_OR_NOW RTLD_NOW -# else -# ifdef DL_NOW -# define LT_DLLAZY_OR_NOW DL_NOW -# else -# define LT_DLLAZY_OR_NOW 0 -# endif -# endif -# endif -# endif -#endif - -/* When -fvisibility=hidden is used, assume the code has been annotated - correspondingly for the symbols needed. */ -#if defined __GNUC__ && (((__GNUC__ == 3) && (__GNUC_MINOR__ >= 3)) || (__GNUC__ > 3)) -int fnord () __attribute__((visibility("default"))); -#endif - -int fnord () { return 42; } -int main () -{ - void *self = dlopen (0, LT_DLGLOBAL|LT_DLLAZY_OR_NOW); - int status = $lt_dlunknown; - - if (self) - { - if (dlsym (self,"fnord")) status = $lt_dlno_uscore; - else - { - if (dlsym( self,"_fnord")) status = $lt_dlneed_uscore; - else puts (dlerror ()); - } - /* dlclose (self); */ - } - else - puts (dlerror ()); - - return status; -}] -_LT_EOF - if AC_TRY_EVAL(ac_link) && test -s "conftest$ac_exeext" 2>/dev/null; then - (./conftest; exit; ) >&AS_MESSAGE_LOG_FD 2>/dev/null - lt_status=$? - case x$lt_status in - x$lt_dlno_uscore) $1 ;; - x$lt_dlneed_uscore) $2 ;; - x$lt_dlunknown|x*) $3 ;; - esac - else : - # compilation failed - $3 - fi -fi -rm -fr conftest* -])# _LT_TRY_DLOPEN_SELF - - -# LT_SYS_DLOPEN_SELF -# ------------------ -AC_DEFUN([LT_SYS_DLOPEN_SELF], -[m4_require([_LT_HEADER_DLFCN])dnl -if test yes != "$enable_dlopen"; then - enable_dlopen=unknown - enable_dlopen_self=unknown - enable_dlopen_self_static=unknown -else - lt_cv_dlopen=no - lt_cv_dlopen_libs= - - case $host_os in - beos*) - lt_cv_dlopen=load_add_on - lt_cv_dlopen_libs= - lt_cv_dlopen_self=yes - ;; - - mingw* | pw32* | cegcc*) - lt_cv_dlopen=LoadLibrary - lt_cv_dlopen_libs= - ;; - - cygwin*) - lt_cv_dlopen=dlopen - lt_cv_dlopen_libs= - ;; - - darwin*) - # if libdl is installed we need to link against it - AC_CHECK_LIB([dl], [dlopen], - [lt_cv_dlopen=dlopen lt_cv_dlopen_libs=-ldl],[ - lt_cv_dlopen=dyld - lt_cv_dlopen_libs= - lt_cv_dlopen_self=yes - ]) - ;; - - tpf*) - # Don't try to run any link tests for TPF. We know it's impossible - # because TPF is a cross-compiler, and we know how we open DSOs. - lt_cv_dlopen=dlopen - lt_cv_dlopen_libs= - lt_cv_dlopen_self=no - ;; - - *) - AC_CHECK_FUNC([shl_load], - [lt_cv_dlopen=shl_load], - [AC_CHECK_LIB([dld], [shl_load], - [lt_cv_dlopen=shl_load lt_cv_dlopen_libs=-ldld], - [AC_CHECK_FUNC([dlopen], - [lt_cv_dlopen=dlopen], - [AC_CHECK_LIB([dl], [dlopen], - [lt_cv_dlopen=dlopen lt_cv_dlopen_libs=-ldl], - [AC_CHECK_LIB([svld], [dlopen], - [lt_cv_dlopen=dlopen lt_cv_dlopen_libs=-lsvld], - [AC_CHECK_LIB([dld], [dld_link], - [lt_cv_dlopen=dld_link lt_cv_dlopen_libs=-ldld]) - ]) - ]) - ]) - ]) - ]) - ;; - esac - - if test no = "$lt_cv_dlopen"; then - enable_dlopen=no - else - enable_dlopen=yes - fi - - case $lt_cv_dlopen in - dlopen) - save_CPPFLAGS=$CPPFLAGS - test yes = "$ac_cv_header_dlfcn_h" && CPPFLAGS="$CPPFLAGS -DHAVE_DLFCN_H" - - save_LDFLAGS=$LDFLAGS - wl=$lt_prog_compiler_wl eval LDFLAGS=\"\$LDFLAGS $export_dynamic_flag_spec\" - - save_LIBS=$LIBS - LIBS="$lt_cv_dlopen_libs $LIBS" - - AC_CACHE_CHECK([whether a program can dlopen itself], - lt_cv_dlopen_self, [dnl - _LT_TRY_DLOPEN_SELF( - lt_cv_dlopen_self=yes, lt_cv_dlopen_self=yes, - lt_cv_dlopen_self=no, lt_cv_dlopen_self=cross) - ]) - - if test yes = "$lt_cv_dlopen_self"; then - wl=$lt_prog_compiler_wl eval LDFLAGS=\"\$LDFLAGS $lt_prog_compiler_static\" - AC_CACHE_CHECK([whether a statically linked program can dlopen itself], - lt_cv_dlopen_self_static, [dnl - _LT_TRY_DLOPEN_SELF( - lt_cv_dlopen_self_static=yes, lt_cv_dlopen_self_static=yes, - lt_cv_dlopen_self_static=no, lt_cv_dlopen_self_static=cross) - ]) - fi - - CPPFLAGS=$save_CPPFLAGS - LDFLAGS=$save_LDFLAGS - LIBS=$save_LIBS - ;; - esac - - case $lt_cv_dlopen_self in - yes|no) enable_dlopen_self=$lt_cv_dlopen_self ;; - *) enable_dlopen_self=unknown ;; - esac - - case $lt_cv_dlopen_self_static in - yes|no) enable_dlopen_self_static=$lt_cv_dlopen_self_static ;; - *) enable_dlopen_self_static=unknown ;; - esac -fi -_LT_DECL([dlopen_support], [enable_dlopen], [0], - [Whether dlopen is supported]) -_LT_DECL([dlopen_self], [enable_dlopen_self], [0], - [Whether dlopen of programs is supported]) -_LT_DECL([dlopen_self_static], [enable_dlopen_self_static], [0], - [Whether dlopen of statically linked programs is supported]) -])# LT_SYS_DLOPEN_SELF - -# Old name: -AU_ALIAS([AC_LIBTOOL_DLOPEN_SELF], [LT_SYS_DLOPEN_SELF]) -dnl aclocal-1.4 backwards compatibility: -dnl AC_DEFUN([AC_LIBTOOL_DLOPEN_SELF], []) - - -# _LT_COMPILER_C_O([TAGNAME]) -# --------------------------- -# Check to see if options -c and -o are simultaneously supported by compiler. -# This macro does not hard code the compiler like AC_PROG_CC_C_O. -m4_defun([_LT_COMPILER_C_O], -[m4_require([_LT_DECL_SED])dnl -m4_require([_LT_FILEUTILS_DEFAULTS])dnl -m4_require([_LT_TAG_COMPILER])dnl -AC_CACHE_CHECK([if $compiler supports -c -o file.$ac_objext], - [_LT_TAGVAR(lt_cv_prog_compiler_c_o, $1)], - [_LT_TAGVAR(lt_cv_prog_compiler_c_o, $1)=no - $RM -r conftest 2>/dev/null - mkdir conftest - cd conftest - mkdir out - echo "$lt_simple_compile_test_code" > conftest.$ac_ext - - lt_compiler_flag="-o out/conftest2.$ac_objext" - # Insert the option either (1) after the last *FLAGS variable, or - # (2) before a word containing "conftest.", or (3) at the end. - # Note that $ac_compile itself does not contain backslashes and begins - # with a dollar sign (not a hyphen), so the echo should work correctly. - lt_compile=`echo "$ac_compile" | $SED \ - -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \ - -e 's: [[^ ]]*conftest\.: $lt_compiler_flag&:; t' \ - -e 's:$: $lt_compiler_flag:'` - (eval echo "\"\$as_me:$LINENO: $lt_compile\"" >&AS_MESSAGE_LOG_FD) - (eval "$lt_compile" 2>out/conftest.err) - ac_status=$? - cat out/conftest.err >&AS_MESSAGE_LOG_FD - echo "$as_me:$LINENO: \$? = $ac_status" >&AS_MESSAGE_LOG_FD - if (exit $ac_status) && test -s out/conftest2.$ac_objext - then - # The compiler can only warn and ignore the option if not recognized - # So say no if there are warnings - $ECHO "$_lt_compiler_boilerplate" | $SED '/^$/d' > out/conftest.exp - $SED '/^$/d; /^ *+/d' out/conftest.err >out/conftest.er2 - if test ! -s out/conftest.er2 || diff out/conftest.exp out/conftest.er2 >/dev/null; then - _LT_TAGVAR(lt_cv_prog_compiler_c_o, $1)=yes - fi - fi - chmod u+w . 2>&AS_MESSAGE_LOG_FD - $RM conftest* - # SGI C++ compiler will create directory out/ii_files/ for - # template instantiation - test -d out/ii_files && $RM out/ii_files/* && rmdir out/ii_files - $RM out/* && rmdir out - cd .. - $RM -r conftest - $RM conftest* -]) -_LT_TAGDECL([compiler_c_o], [lt_cv_prog_compiler_c_o], [1], - [Does compiler simultaneously support -c and -o options?]) -])# _LT_COMPILER_C_O - - -# _LT_COMPILER_FILE_LOCKS([TAGNAME]) -# ---------------------------------- -# Check to see if we can do hard links to lock some files if needed -m4_defun([_LT_COMPILER_FILE_LOCKS], -[m4_require([_LT_ENABLE_LOCK])dnl -m4_require([_LT_FILEUTILS_DEFAULTS])dnl -_LT_COMPILER_C_O([$1]) - -hard_links=nottested -if test no = "$_LT_TAGVAR(lt_cv_prog_compiler_c_o, $1)" && test no != "$need_locks"; then - # do not overwrite the value of need_locks provided by the user - AC_MSG_CHECKING([if we can lock with hard links]) - hard_links=yes - $RM conftest* - ln conftest.a conftest.b 2>/dev/null && hard_links=no - touch conftest.a - ln conftest.a conftest.b 2>&5 || hard_links=no - ln conftest.a conftest.b 2>/dev/null && hard_links=no - AC_MSG_RESULT([$hard_links]) - if test no = "$hard_links"; then - AC_MSG_WARN(['$CC' does not support '-c -o', so 'make -j' may be unsafe]) - need_locks=warn - fi -else - need_locks=no -fi -_LT_DECL([], [need_locks], [1], [Must we lock files when doing compilation?]) -])# _LT_COMPILER_FILE_LOCKS - - -# _LT_CHECK_OBJDIR -# ---------------- -m4_defun([_LT_CHECK_OBJDIR], -[AC_CACHE_CHECK([for objdir], [lt_cv_objdir], -[rm -f .libs 2>/dev/null -mkdir .libs 2>/dev/null -if test -d .libs; then - lt_cv_objdir=.libs -else - # MS-DOS does not allow filenames that begin with a dot. - lt_cv_objdir=_libs -fi -rmdir .libs 2>/dev/null]) -objdir=$lt_cv_objdir -_LT_DECL([], [objdir], [0], - [The name of the directory that contains temporary libtool files])dnl -m4_pattern_allow([LT_OBJDIR])dnl -AC_DEFINE_UNQUOTED([LT_OBJDIR], "$lt_cv_objdir/", - [Define to the sub-directory where libtool stores uninstalled libraries.]) -])# _LT_CHECK_OBJDIR - - -# _LT_LINKER_HARDCODE_LIBPATH([TAGNAME]) -# -------------------------------------- -# Check hardcoding attributes. -m4_defun([_LT_LINKER_HARDCODE_LIBPATH], -[AC_MSG_CHECKING([how to hardcode library paths into programs]) -_LT_TAGVAR(hardcode_action, $1)= -if test -n "$_LT_TAGVAR(hardcode_libdir_flag_spec, $1)" || - test -n "$_LT_TAGVAR(runpath_var, $1)" || - test yes = "$_LT_TAGVAR(hardcode_automatic, $1)"; then - - # We can hardcode non-existent directories. - if test no != "$_LT_TAGVAR(hardcode_direct, $1)" && - # If the only mechanism to avoid hardcoding is shlibpath_var, we - # have to relink, otherwise we might link with an installed library - # when we should be linking with a yet-to-be-installed one - ## test no != "$_LT_TAGVAR(hardcode_shlibpath_var, $1)" && - test no != "$_LT_TAGVAR(hardcode_minus_L, $1)"; then - # Linking always hardcodes the temporary library directory. - _LT_TAGVAR(hardcode_action, $1)=relink - else - # We can link without hardcoding, and we can hardcode nonexisting dirs. - _LT_TAGVAR(hardcode_action, $1)=immediate - fi -else - # We cannot hardcode anything, or else we can only hardcode existing - # directories. - _LT_TAGVAR(hardcode_action, $1)=unsupported -fi -AC_MSG_RESULT([$_LT_TAGVAR(hardcode_action, $1)]) - -if test relink = "$_LT_TAGVAR(hardcode_action, $1)" || - test yes = "$_LT_TAGVAR(inherit_rpath, $1)"; then - # Fast installation is not supported - enable_fast_install=no -elif test yes = "$shlibpath_overrides_runpath" || - test no = "$enable_shared"; then - # Fast installation is not necessary - enable_fast_install=needless -fi -_LT_TAGDECL([], [hardcode_action], [0], - [How to hardcode a shared library path into an executable]) -])# _LT_LINKER_HARDCODE_LIBPATH - - -# _LT_CMD_STRIPLIB -# ---------------- -m4_defun([_LT_CMD_STRIPLIB], -[m4_require([_LT_DECL_EGREP]) -striplib= -old_striplib= -AC_MSG_CHECKING([whether stripping libraries is possible]) -if test -n "$STRIP" && $STRIP -V 2>&1 | $GREP "GNU strip" >/dev/null; then - test -z "$old_striplib" && old_striplib="$STRIP --strip-debug" - test -z "$striplib" && striplib="$STRIP --strip-unneeded" - AC_MSG_RESULT([yes]) -else -# FIXME - insert some real tests, host_os isn't really good enough - case $host_os in - darwin*) - if test -n "$STRIP"; then - striplib="$STRIP -x" - old_striplib="$STRIP -S" - AC_MSG_RESULT([yes]) - else - AC_MSG_RESULT([no]) - fi - ;; - *) - AC_MSG_RESULT([no]) - ;; - esac -fi -_LT_DECL([], [old_striplib], [1], [Commands to strip libraries]) -_LT_DECL([], [striplib], [1]) -])# _LT_CMD_STRIPLIB - - -# _LT_PREPARE_MUNGE_PATH_LIST -# --------------------------- -# Make sure func_munge_path_list() is defined correctly. -m4_defun([_LT_PREPARE_MUNGE_PATH_LIST], -[[# func_munge_path_list VARIABLE PATH -# ----------------------------------- -# VARIABLE is name of variable containing _space_ separated list of -# directories to be munged by the contents of PATH, which is string -# having a format: -# "DIR[:DIR]:" -# string "DIR[ DIR]" will be prepended to VARIABLE -# ":DIR[:DIR]" -# string "DIR[ DIR]" will be appended to VARIABLE -# "DIRP[:DIRP]::[DIRA:]DIRA" -# string "DIRP[ DIRP]" will be prepended to VARIABLE and string -# "DIRA[ DIRA]" will be appended to VARIABLE -# "DIR[:DIR]" -# VARIABLE will be replaced by "DIR[ DIR]" -func_munge_path_list () -{ - case x@S|@2 in - x) - ;; - *:) - eval @S|@1=\"`$ECHO @S|@2 | $SED 's/:/ /g'` \@S|@@S|@1\" - ;; - x:*) - eval @S|@1=\"\@S|@@S|@1 `$ECHO @S|@2 | $SED 's/:/ /g'`\" - ;; - *::*) - eval @S|@1=\"\@S|@@S|@1\ `$ECHO @S|@2 | $SED -e 's/.*:://' -e 's/:/ /g'`\" - eval @S|@1=\"`$ECHO @S|@2 | $SED -e 's/::.*//' -e 's/:/ /g'`\ \@S|@@S|@1\" - ;; - *) - eval @S|@1=\"`$ECHO @S|@2 | $SED 's/:/ /g'`\" - ;; - esac -} -]])# _LT_PREPARE_PATH_LIST - - -# _LT_SYS_DYNAMIC_LINKER([TAG]) -# ----------------------------- -# PORTME Fill in your ld.so characteristics -m4_defun([_LT_SYS_DYNAMIC_LINKER], -[AC_REQUIRE([AC_CANONICAL_HOST])dnl -m4_require([_LT_DECL_EGREP])dnl -m4_require([_LT_FILEUTILS_DEFAULTS])dnl -m4_require([_LT_DECL_OBJDUMP])dnl -m4_require([_LT_DECL_SED])dnl -m4_require([_LT_CHECK_SHELL_FEATURES])dnl -m4_require([_LT_PREPARE_MUNGE_PATH_LIST])dnl -AC_MSG_CHECKING([dynamic linker characteristics]) -m4_if([$1], - [], [ -if test yes = "$GCC"; then - case $host_os in - darwin*) lt_awk_arg='/^libraries:/,/LR/' ;; - *) lt_awk_arg='/^libraries:/' ;; - esac - case $host_os in - mingw* | cegcc*) lt_sed_strip_eq='s|=\([[A-Za-z]]:\)|\1|g' ;; - *) lt_sed_strip_eq='s|=/|/|g' ;; - esac - lt_search_path_spec=`$CC -print-search-dirs | awk $lt_awk_arg | $SED -e "s/^libraries://" -e $lt_sed_strip_eq` - case $lt_search_path_spec in - *\;*) - # if the path contains ";" then we assume it to be the separator - # otherwise default to the standard path separator (i.e. ":") - it is - # assumed that no part of a normal pathname contains ";" but that should - # okay in the real world where ";" in dirpaths is itself problematic. - lt_search_path_spec=`$ECHO "$lt_search_path_spec" | $SED 's/;/ /g'` - ;; - *) - lt_search_path_spec=`$ECHO "$lt_search_path_spec" | $SED "s/$PATH_SEPARATOR/ /g"` - ;; - esac - # Ok, now we have the path, separated by spaces, we can step through it - # and add multilib dir if necessary... - lt_tmp_lt_search_path_spec= - lt_multi_os_dir=/`$CC $CPPFLAGS $CFLAGS $LDFLAGS -print-multi-os-directory 2>/dev/null` - # ...but if some path component already ends with the multilib dir we assume - # that all is fine and trust -print-search-dirs as is (GCC 4.2? or newer). - case "$lt_multi_os_dir; $lt_search_path_spec " in - "/; "* | "/.; "* | "/./; "* | *"$lt_multi_os_dir "* | *"$lt_multi_os_dir/ "*) - lt_multi_os_dir= - ;; - esac - for lt_sys_path in $lt_search_path_spec; do - if test -d "$lt_sys_path$lt_multi_os_dir"; then - lt_tmp_lt_search_path_spec="$lt_tmp_lt_search_path_spec $lt_sys_path$lt_multi_os_dir" - elif test -n "$lt_multi_os_dir"; then - test -d "$lt_sys_path" && \ - lt_tmp_lt_search_path_spec="$lt_tmp_lt_search_path_spec $lt_sys_path" - fi - done - lt_search_path_spec=`$ECHO "$lt_tmp_lt_search_path_spec" | awk ' -BEGIN {RS = " "; FS = "/|\n";} { - lt_foo = ""; - lt_count = 0; - for (lt_i = NF; lt_i > 0; lt_i--) { - if ($lt_i != "" && $lt_i != ".") { - if ($lt_i == "..") { - lt_count++; - } else { - if (lt_count == 0) { - lt_foo = "/" $lt_i lt_foo; - } else { - lt_count--; - } - } - } - } - if (lt_foo != "") { lt_freq[[lt_foo]]++; } - if (lt_freq[[lt_foo]] == 1) { print lt_foo; } -}'` - # AWK program above erroneously prepends '/' to C:/dos/paths - # for these hosts. - case $host_os in - mingw* | cegcc*) lt_search_path_spec=`$ECHO "$lt_search_path_spec" |\ - $SED 's|/\([[A-Za-z]]:\)|\1|g'` ;; - esac - sys_lib_search_path_spec=`$ECHO "$lt_search_path_spec" | $lt_NL2SP` -else - sys_lib_search_path_spec="/lib /usr/lib /usr/local/lib" -fi]) -library_names_spec= -libname_spec='lib$name' -soname_spec= -shrext_cmds=.so -postinstall_cmds= -postuninstall_cmds= -finish_cmds= -finish_eval= -shlibpath_var= -shlibpath_overrides_runpath=unknown -version_type=none -dynamic_linker="$host_os ld.so" -sys_lib_dlsearch_path_spec="/lib /usr/lib" -need_lib_prefix=unknown -hardcode_into_libs=no - -# when you set need_version to no, make sure it does not cause -set_version -# flags to be left without arguments -need_version=unknown - -AC_ARG_VAR([LT_SYS_LIBRARY_PATH], -[User-defined run-time library search path.]) - -case $host_os in -aix3*) - version_type=linux # correct to gnu/linux during the next big refactor - library_names_spec='$libname$release$shared_ext$versuffix $libname.a' - shlibpath_var=LIBPATH - - # AIX 3 has no versioning support, so we append a major version to the name. - soname_spec='$libname$release$shared_ext$major' - ;; - -aix[[4-9]]*) - version_type=linux # correct to gnu/linux during the next big refactor - need_lib_prefix=no - need_version=no - hardcode_into_libs=yes - if test ia64 = "$host_cpu"; then - # AIX 5 supports IA64 - library_names_spec='$libname$release$shared_ext$major $libname$release$shared_ext$versuffix $libname$shared_ext' - shlibpath_var=LD_LIBRARY_PATH - else - # With GCC up to 2.95.x, collect2 would create an import file - # for dependence libraries. The import file would start with - # the line '#! .'. This would cause the generated library to - # depend on '.', always an invalid library. This was fixed in - # development snapshots of GCC prior to 3.0. - case $host_os in - aix4 | aix4.[[01]] | aix4.[[01]].*) - if { echo '#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 97)' - echo ' yes ' - echo '#endif'; } | $CC -E - | $GREP yes > /dev/null; then - : - else - can_build_shared=no - fi - ;; - esac - # Using Import Files as archive members, it is possible to support - # filename-based versioning of shared library archives on AIX. While - # this would work for both with and without runtime linking, it will - # prevent static linking of such archives. So we do filename-based - # shared library versioning with .so extension only, which is used - # when both runtime linking and shared linking is enabled. - # Unfortunately, runtime linking may impact performance, so we do - # not want this to be the default eventually. Also, we use the - # versioned .so libs for executables only if there is the -brtl - # linker flag in LDFLAGS as well, or --with-aix-soname=svr4 only. - # To allow for filename-based versioning support, we need to create - # libNAME.so.V as an archive file, containing: - # *) an Import File, referring to the versioned filename of the - # archive as well as the shared archive member, telling the - # bitwidth (32 or 64) of that shared object, and providing the - # list of exported symbols of that shared object, eventually - # decorated with the 'weak' keyword - # *) the shared object with the F_LOADONLY flag set, to really avoid - # it being seen by the linker. - # At run time we better use the real file rather than another symlink, - # but for link time we create the symlink libNAME.so -> libNAME.so.V - - case $with_aix_soname,$aix_use_runtimelinking in - # AIX (on Power*) has no versioning support, so currently we cannot hardcode correct - # soname into executable. Probably we can add versioning support to - # collect2, so additional links can be useful in future. - aix,yes) # traditional libtool - dynamic_linker='AIX unversionable lib.so' - # If using run time linking (on AIX 4.2 or later) use lib.so - # instead of lib.a to let people know that these are not - # typical AIX shared libraries. - library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' - ;; - aix,no) # traditional AIX only - dynamic_linker='AIX lib.a[(]lib.so.V[)]' - # We preserve .a as extension for shared libraries through AIX4.2 - # and later when we are not doing run time linking. - library_names_spec='$libname$release.a $libname.a' - soname_spec='$libname$release$shared_ext$major' - ;; - svr4,*) # full svr4 only - dynamic_linker="AIX lib.so.V[(]$shared_archive_member_spec.o[)]" - library_names_spec='$libname$release$shared_ext$major $libname$shared_ext' - # We do not specify a path in Import Files, so LIBPATH fires. - shlibpath_overrides_runpath=yes - ;; - *,yes) # both, prefer svr4 - dynamic_linker="AIX lib.so.V[(]$shared_archive_member_spec.o[)], lib.a[(]lib.so.V[)]" - library_names_spec='$libname$release$shared_ext$major $libname$shared_ext' - # unpreferred sharedlib libNAME.a needs extra handling - postinstall_cmds='test -n "$linkname" || linkname="$realname"~func_stripname "" ".so" "$linkname"~$install_shared_prog "$dir/$func_stripname_result.$libext" "$destdir/$func_stripname_result.$libext"~test -z "$tstripme" || test -z "$striplib" || $striplib "$destdir/$func_stripname_result.$libext"' - postuninstall_cmds='for n in $library_names $old_library; do :; done~func_stripname "" ".so" "$n"~test "$func_stripname_result" = "$n" || func_append rmfiles " $odir/$func_stripname_result.$libext"' - # We do not specify a path in Import Files, so LIBPATH fires. - shlibpath_overrides_runpath=yes - ;; - *,no) # both, prefer aix - dynamic_linker="AIX lib.a[(]lib.so.V[)], lib.so.V[(]$shared_archive_member_spec.o[)]" - library_names_spec='$libname$release.a $libname.a' - soname_spec='$libname$release$shared_ext$major' - # unpreferred sharedlib libNAME.so.V and symlink libNAME.so need extra handling - postinstall_cmds='test -z "$dlname" || $install_shared_prog $dir/$dlname $destdir/$dlname~test -z "$tstripme" || test -z "$striplib" || $striplib $destdir/$dlname~test -n "$linkname" || linkname=$realname~func_stripname "" ".a" "$linkname"~(cd "$destdir" && $LN_S -f $dlname $func_stripname_result.so)' - postuninstall_cmds='test -z "$dlname" || func_append rmfiles " $odir/$dlname"~for n in $old_library $library_names; do :; done~func_stripname "" ".a" "$n"~func_append rmfiles " $odir/$func_stripname_result.so"' - ;; - esac - shlibpath_var=LIBPATH - fi - ;; - -amigaos*) - case $host_cpu in - powerpc) - # Since July 2007 AmigaOS4 officially supports .so libraries. - # When compiling the executable, add -use-dynld -Lsobjs: to the compileline. - library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' - ;; - m68k) - library_names_spec='$libname.ixlibrary $libname.a' - # Create ${libname}_ixlibrary.a entries in /sys/libs. - finish_eval='for lib in `ls $libdir/*.ixlibrary 2>/dev/null`; do libname=`func_echo_all "$lib" | $SED '\''s%^.*/\([[^/]]*\)\.ixlibrary$%\1%'\''`; $RM /sys/libs/${libname}_ixlibrary.a; $show "cd /sys/libs && $LN_S $lib ${libname}_ixlibrary.a"; cd /sys/libs && $LN_S $lib ${libname}_ixlibrary.a || exit 1; done' - ;; - esac - ;; - -beos*) - library_names_spec='$libname$shared_ext' - dynamic_linker="$host_os ld.so" - shlibpath_var=LIBRARY_PATH - ;; - -bsdi[[45]]*) - version_type=linux # correct to gnu/linux during the next big refactor - need_version=no - library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' - soname_spec='$libname$release$shared_ext$major' - finish_cmds='PATH="\$PATH:/sbin" ldconfig $libdir' - shlibpath_var=LD_LIBRARY_PATH - sys_lib_search_path_spec="/shlib /usr/lib /usr/X11/lib /usr/contrib/lib /lib /usr/local/lib" - sys_lib_dlsearch_path_spec="/shlib /usr/lib /usr/local/lib" - # the default ld.so.conf also contains /usr/contrib/lib and - # /usr/X11R6/lib (/usr/X11 is a link to /usr/X11R6), but let us allow - # libtool to hard-code these into programs - ;; - -cygwin* | mingw* | pw32* | cegcc*) - version_type=windows - shrext_cmds=.dll - need_version=no - need_lib_prefix=no - - case $GCC,$cc_basename in - yes,*) - # gcc - library_names_spec='$libname.dll.a' - # DLL is installed to $(libdir)/../bin by postinstall_cmds - postinstall_cmds='base_file=`basename \$file`~ - dlpath=`$SHELL 2>&1 -c '\''. $dir/'\''\$base_file'\''i; echo \$dlname'\''`~ - dldir=$destdir/`dirname \$dlpath`~ - test -d \$dldir || mkdir -p \$dldir~ - $install_prog $dir/$dlname \$dldir/$dlname~ - chmod a+x \$dldir/$dlname~ - if test -n '\''$stripme'\'' && test -n '\''$striplib'\''; then - eval '\''$striplib \$dldir/$dlname'\'' || exit \$?; - fi' - postuninstall_cmds='dldll=`$SHELL 2>&1 -c '\''. $file; echo \$dlname'\''`~ - dlpath=$dir/\$dldll~ - $RM \$dlpath' - shlibpath_overrides_runpath=yes - - case $host_os in - cygwin*) - # Cygwin DLLs use 'cyg' prefix rather than 'lib' - soname_spec='`echo $libname | sed -e 's/^lib/cyg/'``echo $release | $SED -e 's/[[.]]/-/g'`$versuffix$shared_ext' -m4_if([$1], [],[ - sys_lib_search_path_spec="$sys_lib_search_path_spec /usr/lib/w32api"]) - ;; - mingw* | cegcc*) - # MinGW DLLs use traditional 'lib' prefix - soname_spec='$libname`echo $release | $SED -e 's/[[.]]/-/g'`$versuffix$shared_ext' - ;; - pw32*) - # pw32 DLLs use 'pw' prefix rather than 'lib' - library_names_spec='`echo $libname | sed -e 's/^lib/pw/'``echo $release | $SED -e 's/[[.]]/-/g'`$versuffix$shared_ext' - ;; - esac - dynamic_linker='Win32 ld.exe' - ;; - - *,cl*) - # Native MSVC - libname_spec='$name' - soname_spec='$libname`echo $release | $SED -e 's/[[.]]/-/g'`$versuffix$shared_ext' - library_names_spec='$libname.dll.lib' - - case $build_os in - mingw*) - sys_lib_search_path_spec= - lt_save_ifs=$IFS - IFS=';' - for lt_path in $LIB - do - IFS=$lt_save_ifs - # Let DOS variable expansion print the short 8.3 style file name. - lt_path=`cd "$lt_path" 2>/dev/null && cmd //C "for %i in (".") do @echo %~si"` - sys_lib_search_path_spec="$sys_lib_search_path_spec $lt_path" - done - IFS=$lt_save_ifs - # Convert to MSYS style. - sys_lib_search_path_spec=`$ECHO "$sys_lib_search_path_spec" | sed -e 's|\\\\|/|g' -e 's| \\([[a-zA-Z]]\\):| /\\1|g' -e 's|^ ||'` - ;; - cygwin*) - # Convert to unix form, then to dos form, then back to unix form - # but this time dos style (no spaces!) so that the unix form looks - # like /cygdrive/c/PROGRA~1:/cygdr... - sys_lib_search_path_spec=`cygpath --path --unix "$LIB"` - sys_lib_search_path_spec=`cygpath --path --dos "$sys_lib_search_path_spec" 2>/dev/null` - sys_lib_search_path_spec=`cygpath --path --unix "$sys_lib_search_path_spec" | $SED -e "s/$PATH_SEPARATOR/ /g"` - ;; - *) - sys_lib_search_path_spec=$LIB - if $ECHO "$sys_lib_search_path_spec" | [$GREP ';[c-zC-Z]:/' >/dev/null]; then - # It is most probably a Windows format PATH. - sys_lib_search_path_spec=`$ECHO "$sys_lib_search_path_spec" | $SED -e 's/;/ /g'` - else - sys_lib_search_path_spec=`$ECHO "$sys_lib_search_path_spec" | $SED -e "s/$PATH_SEPARATOR/ /g"` - fi - # FIXME: find the short name or the path components, as spaces are - # common. (e.g. "Program Files" -> "PROGRA~1") - ;; - esac - - # DLL is installed to $(libdir)/../bin by postinstall_cmds - postinstall_cmds='base_file=`basename \$file`~ - dlpath=`$SHELL 2>&1 -c '\''. $dir/'\''\$base_file'\''i; echo \$dlname'\''`~ - dldir=$destdir/`dirname \$dlpath`~ - test -d \$dldir || mkdir -p \$dldir~ - $install_prog $dir/$dlname \$dldir/$dlname' - postuninstall_cmds='dldll=`$SHELL 2>&1 -c '\''. $file; echo \$dlname'\''`~ - dlpath=$dir/\$dldll~ - $RM \$dlpath' - shlibpath_overrides_runpath=yes - dynamic_linker='Win32 link.exe' - ;; - - *) - # Assume MSVC wrapper - library_names_spec='$libname`echo $release | $SED -e 's/[[.]]/-/g'`$versuffix$shared_ext $libname.lib' - dynamic_linker='Win32 ld.exe' - ;; - esac - # FIXME: first we should search . and the directory the executable is in - shlibpath_var=PATH - ;; - -darwin* | rhapsody*) - dynamic_linker="$host_os dyld" - version_type=darwin - need_lib_prefix=no - need_version=no - library_names_spec='$libname$release$major$shared_ext $libname$shared_ext' - soname_spec='$libname$release$major$shared_ext' - shlibpath_overrides_runpath=yes - shlibpath_var=DYLD_LIBRARY_PATH - shrext_cmds='`test .$module = .yes && echo .so || echo .dylib`' -m4_if([$1], [],[ - sys_lib_search_path_spec="$sys_lib_search_path_spec /usr/local/lib"]) - sys_lib_dlsearch_path_spec='/usr/local/lib /lib /usr/lib' - ;; - -dgux*) - version_type=linux # correct to gnu/linux during the next big refactor - need_lib_prefix=no - need_version=no - library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' - soname_spec='$libname$release$shared_ext$major' - shlibpath_var=LD_LIBRARY_PATH - ;; - -freebsd* | dragonfly*) - # DragonFly does not have aout. When/if they implement a new - # versioning mechanism, adjust this. - if test -x /usr/bin/objformat; then - objformat=`/usr/bin/objformat` - else - case $host_os in - freebsd[[23]].*) objformat=aout ;; - *) objformat=elf ;; - esac - fi - version_type=freebsd-$objformat - case $version_type in - freebsd-elf*) - library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' - soname_spec='$libname$release$shared_ext$major' - need_version=no - need_lib_prefix=no - ;; - freebsd-*) - library_names_spec='$libname$release$shared_ext$versuffix $libname$shared_ext$versuffix' - need_version=yes - ;; - esac - shlibpath_var=LD_LIBRARY_PATH - case $host_os in - freebsd2.*) - shlibpath_overrides_runpath=yes - ;; - freebsd3.[[01]]* | freebsdelf3.[[01]]*) - shlibpath_overrides_runpath=yes - hardcode_into_libs=yes - ;; - freebsd3.[[2-9]]* | freebsdelf3.[[2-9]]* | \ - freebsd4.[[0-5]] | freebsdelf4.[[0-5]] | freebsd4.1.1 | freebsdelf4.1.1) - shlibpath_overrides_runpath=no - hardcode_into_libs=yes - ;; - *) # from 4.6 on, and DragonFly - shlibpath_overrides_runpath=yes - hardcode_into_libs=yes - ;; - esac - ;; - -haiku*) - version_type=linux # correct to gnu/linux during the next big refactor - need_lib_prefix=no - need_version=no - dynamic_linker="$host_os runtime_loader" - library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' - soname_spec='$libname$release$shared_ext$major' - shlibpath_var=LIBRARY_PATH - shlibpath_overrides_runpath=no - sys_lib_dlsearch_path_spec='/boot/home/config/lib /boot/common/lib /boot/system/lib' - hardcode_into_libs=yes - ;; - -hpux9* | hpux10* | hpux11*) - # Give a soname corresponding to the major version so that dld.sl refuses to - # link against other versions. - version_type=sunos - need_lib_prefix=no - need_version=no - case $host_cpu in - ia64*) - shrext_cmds='.so' - hardcode_into_libs=yes - dynamic_linker="$host_os dld.so" - shlibpath_var=LD_LIBRARY_PATH - shlibpath_overrides_runpath=yes # Unless +noenvvar is specified. - library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' - soname_spec='$libname$release$shared_ext$major' - if test 32 = "$HPUX_IA64_MODE"; then - sys_lib_search_path_spec="/usr/lib/hpux32 /usr/local/lib/hpux32 /usr/local/lib" - sys_lib_dlsearch_path_spec=/usr/lib/hpux32 - else - sys_lib_search_path_spec="/usr/lib/hpux64 /usr/local/lib/hpux64" - sys_lib_dlsearch_path_spec=/usr/lib/hpux64 - fi - ;; - hppa*64*) - shrext_cmds='.sl' - hardcode_into_libs=yes - dynamic_linker="$host_os dld.sl" - shlibpath_var=LD_LIBRARY_PATH # How should we handle SHLIB_PATH - shlibpath_overrides_runpath=yes # Unless +noenvvar is specified. - library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' - soname_spec='$libname$release$shared_ext$major' - sys_lib_search_path_spec="/usr/lib/pa20_64 /usr/ccs/lib/pa20_64" - sys_lib_dlsearch_path_spec=$sys_lib_search_path_spec - ;; - *) - shrext_cmds='.sl' - dynamic_linker="$host_os dld.sl" - shlibpath_var=SHLIB_PATH - shlibpath_overrides_runpath=no # +s is required to enable SHLIB_PATH - library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' - soname_spec='$libname$release$shared_ext$major' - ;; - esac - # HP-UX runs *really* slowly unless shared libraries are mode 555, ... - postinstall_cmds='chmod 555 $lib' - # or fails outright, so override atomically: - install_override_mode=555 - ;; - -interix[[3-9]]*) - version_type=linux # correct to gnu/linux during the next big refactor - need_lib_prefix=no - need_version=no - library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' - soname_spec='$libname$release$shared_ext$major' - dynamic_linker='Interix 3.x ld.so.1 (PE, like ELF)' - shlibpath_var=LD_LIBRARY_PATH - shlibpath_overrides_runpath=no - hardcode_into_libs=yes - ;; - -irix5* | irix6* | nonstopux*) - case $host_os in - nonstopux*) version_type=nonstopux ;; - *) - if test yes = "$lt_cv_prog_gnu_ld"; then - version_type=linux # correct to gnu/linux during the next big refactor - else - version_type=irix - fi ;; - esac - need_lib_prefix=no - need_version=no - soname_spec='$libname$release$shared_ext$major' - library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$release$shared_ext $libname$shared_ext' - case $host_os in - irix5* | nonstopux*) - libsuff= shlibsuff= - ;; - *) - case $LD in # libtool.m4 will add one of these switches to LD - *-32|*"-32 "|*-melf32bsmip|*"-melf32bsmip ") - libsuff= shlibsuff= libmagic=32-bit;; - *-n32|*"-n32 "|*-melf32bmipn32|*"-melf32bmipn32 ") - libsuff=32 shlibsuff=N32 libmagic=N32;; - *-64|*"-64 "|*-melf64bmip|*"-melf64bmip ") - libsuff=64 shlibsuff=64 libmagic=64-bit;; - *) libsuff= shlibsuff= libmagic=never-match;; - esac - ;; - esac - shlibpath_var=LD_LIBRARY${shlibsuff}_PATH - shlibpath_overrides_runpath=no - sys_lib_search_path_spec="/usr/lib$libsuff /lib$libsuff /usr/local/lib$libsuff" - sys_lib_dlsearch_path_spec="/usr/lib$libsuff /lib$libsuff" - hardcode_into_libs=yes - ;; - -# No shared lib support for Linux oldld, aout, or coff. -linux*oldld* | linux*aout* | linux*coff*) - dynamic_linker=no - ;; - -linux*android*) - version_type=none # Android doesn't support versioned libraries. - need_lib_prefix=no - need_version=no - library_names_spec='$libname$release$shared_ext' - soname_spec='$libname$release$shared_ext' - finish_cmds= - shlibpath_var=LD_LIBRARY_PATH - shlibpath_overrides_runpath=yes - - # This implies no fast_install, which is unacceptable. - # Some rework will be needed to allow for fast_install - # before this can be enabled. - hardcode_into_libs=yes - - dynamic_linker='Android linker' - # Don't embed -rpath directories since the linker doesn't support them. - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-L$libdir' - ;; - -# This must be glibc/ELF. -linux* | k*bsd*-gnu | kopensolaris*-gnu | gnu*) - version_type=linux # correct to gnu/linux during the next big refactor - need_lib_prefix=no - need_version=no - library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' - soname_spec='$libname$release$shared_ext$major' - finish_cmds='PATH="\$PATH:/sbin" ldconfig -n $libdir' - shlibpath_var=LD_LIBRARY_PATH - shlibpath_overrides_runpath=no - - # Some binutils ld are patched to set DT_RUNPATH - AC_CACHE_VAL([lt_cv_shlibpath_overrides_runpath], - [lt_cv_shlibpath_overrides_runpath=no - save_LDFLAGS=$LDFLAGS - save_libdir=$libdir - eval "libdir=/foo; wl=\"$_LT_TAGVAR(lt_prog_compiler_wl, $1)\"; \ - LDFLAGS=\"\$LDFLAGS $_LT_TAGVAR(hardcode_libdir_flag_spec, $1)\"" - AC_LINK_IFELSE([AC_LANG_PROGRAM([],[])], - [AS_IF([ ($OBJDUMP -p conftest$ac_exeext) 2>/dev/null | grep "RUNPATH.*$libdir" >/dev/null], - [lt_cv_shlibpath_overrides_runpath=yes])]) - LDFLAGS=$save_LDFLAGS - libdir=$save_libdir - ]) - shlibpath_overrides_runpath=$lt_cv_shlibpath_overrides_runpath - - # This implies no fast_install, which is unacceptable. - # Some rework will be needed to allow for fast_install - # before this can be enabled. - hardcode_into_libs=yes - - # Ideally, we could use ldconfig to report *all* directores which are - # searched for libraries, however this is still not possible. Aside from not - # being certain /sbin/ldconfig is available, command - # 'ldconfig -N -X -v | grep ^/' on 64bit Fedora does not report /usr/lib64, - # even though it is searched at run-time. Try to do the best guess by - # appending ld.so.conf contents (and includes) to the search path. - if test -f /etc/ld.so.conf; then - lt_ld_extra=`awk '/^include / { system(sprintf("cd /etc; cat %s 2>/dev/null", \[$]2)); skip = 1; } { if (!skip) print \[$]0; skip = 0; }' < /etc/ld.so.conf | $SED -e 's/#.*//;/^[ ]*hwcap[ ]/d;s/[:, ]/ /g;s/=[^=]*$//;s/=[^= ]* / /g;s/"//g;/^$/d' | tr '\n' ' '` - sys_lib_dlsearch_path_spec="/lib /usr/lib $lt_ld_extra" - fi - - # We used to test for /lib/ld.so.1 and disable shared libraries on - # powerpc, because MkLinux only supported shared libraries with the - # GNU dynamic linker. Since this was broken with cross compilers, - # most powerpc-linux boxes support dynamic linking these days and - # people can always --disable-shared, the test was removed, and we - # assume the GNU/Linux dynamic linker is in use. - dynamic_linker='GNU/Linux ld.so' - ;; - -netbsd*) - version_type=sunos - need_lib_prefix=no - need_version=no - if echo __ELF__ | $CC -E - | $GREP __ELF__ >/dev/null; then - library_names_spec='$libname$release$shared_ext$versuffix $libname$shared_ext$versuffix' - finish_cmds='PATH="\$PATH:/sbin" ldconfig -m $libdir' - dynamic_linker='NetBSD (a.out) ld.so' - else - library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' - soname_spec='$libname$release$shared_ext$major' - dynamic_linker='NetBSD ld.elf_so' - fi - shlibpath_var=LD_LIBRARY_PATH - shlibpath_overrides_runpath=yes - hardcode_into_libs=yes - ;; - -newsos6) - version_type=linux # correct to gnu/linux during the next big refactor - library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' - shlibpath_var=LD_LIBRARY_PATH - shlibpath_overrides_runpath=yes - ;; - -*nto* | *qnx*) - version_type=qnx - need_lib_prefix=no - need_version=no - library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' - soname_spec='$libname$release$shared_ext$major' - shlibpath_var=LD_LIBRARY_PATH - shlibpath_overrides_runpath=no - hardcode_into_libs=yes - dynamic_linker='ldqnx.so' - ;; - -openbsd* | bitrig*) - version_type=sunos - sys_lib_dlsearch_path_spec=/usr/lib - need_lib_prefix=no - if test -z "`echo __ELF__ | $CC -E - | $GREP __ELF__`"; then - need_version=no - else - need_version=yes - fi - library_names_spec='$libname$release$shared_ext$versuffix $libname$shared_ext$versuffix' - finish_cmds='PATH="\$PATH:/sbin" ldconfig -m $libdir' - shlibpath_var=LD_LIBRARY_PATH - shlibpath_overrides_runpath=yes - ;; - -os2*) - libname_spec='$name' - version_type=windows - shrext_cmds=.dll - need_version=no - need_lib_prefix=no - # OS/2 can only load a DLL with a base name of 8 characters or less. - soname_spec='`test -n "$os2dllname" && libname="$os2dllname"; - v=$($ECHO $release$versuffix | tr -d .-); - n=$($ECHO $libname | cut -b -$((8 - ${#v})) | tr . _); - $ECHO $n$v`$shared_ext' - library_names_spec='${libname}_dll.$libext' - dynamic_linker='OS/2 ld.exe' - shlibpath_var=BEGINLIBPATH - sys_lib_search_path_spec="/lib /usr/lib /usr/local/lib" - sys_lib_dlsearch_path_spec=$sys_lib_search_path_spec - postinstall_cmds='base_file=`basename \$file`~ - dlpath=`$SHELL 2>&1 -c '\''. $dir/'\''\$base_file'\''i; $ECHO \$dlname'\''`~ - dldir=$destdir/`dirname \$dlpath`~ - test -d \$dldir || mkdir -p \$dldir~ - $install_prog $dir/$dlname \$dldir/$dlname~ - chmod a+x \$dldir/$dlname~ - if test -n '\''$stripme'\'' && test -n '\''$striplib'\''; then - eval '\''$striplib \$dldir/$dlname'\'' || exit \$?; - fi' - postuninstall_cmds='dldll=`$SHELL 2>&1 -c '\''. $file; $ECHO \$dlname'\''`~ - dlpath=$dir/\$dldll~ - $RM \$dlpath' - ;; - -osf3* | osf4* | osf5*) - version_type=osf - need_lib_prefix=no - need_version=no - soname_spec='$libname$release$shared_ext$major' - library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' - shlibpath_var=LD_LIBRARY_PATH - sys_lib_search_path_spec="/usr/shlib /usr/ccs/lib /usr/lib/cmplrs/cc /usr/lib /usr/local/lib /var/shlib" - sys_lib_dlsearch_path_spec=$sys_lib_search_path_spec - ;; - -rdos*) - dynamic_linker=no - ;; - -solaris*) - version_type=linux # correct to gnu/linux during the next big refactor - need_lib_prefix=no - need_version=no - library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' - soname_spec='$libname$release$shared_ext$major' - shlibpath_var=LD_LIBRARY_PATH - shlibpath_overrides_runpath=yes - hardcode_into_libs=yes - # ldd complains unless libraries are executable - postinstall_cmds='chmod +x $lib' - ;; - -sunos4*) - version_type=sunos - library_names_spec='$libname$release$shared_ext$versuffix $libname$shared_ext$versuffix' - finish_cmds='PATH="\$PATH:/usr/etc" ldconfig $libdir' - shlibpath_var=LD_LIBRARY_PATH - shlibpath_overrides_runpath=yes - if test yes = "$with_gnu_ld"; then - need_lib_prefix=no - fi - need_version=yes - ;; - -sysv4 | sysv4.3*) - version_type=linux # correct to gnu/linux during the next big refactor - library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' - soname_spec='$libname$release$shared_ext$major' - shlibpath_var=LD_LIBRARY_PATH - case $host_vendor in - sni) - shlibpath_overrides_runpath=no - need_lib_prefix=no - runpath_var=LD_RUN_PATH - ;; - siemens) - need_lib_prefix=no - ;; - motorola) - need_lib_prefix=no - need_version=no - shlibpath_overrides_runpath=no - sys_lib_search_path_spec='/lib /usr/lib /usr/ccs/lib' - ;; - esac - ;; - -sysv4*MP*) - if test -d /usr/nec; then - version_type=linux # correct to gnu/linux during the next big refactor - library_names_spec='$libname$shared_ext.$versuffix $libname$shared_ext.$major $libname$shared_ext' - soname_spec='$libname$shared_ext.$major' - shlibpath_var=LD_LIBRARY_PATH - fi - ;; - -sysv5* | sco3.2v5* | sco5v6* | unixware* | OpenUNIX* | sysv4*uw2*) - version_type=sco - need_lib_prefix=no - need_version=no - library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext $libname$shared_ext' - soname_spec='$libname$release$shared_ext$major' - shlibpath_var=LD_LIBRARY_PATH - shlibpath_overrides_runpath=yes - hardcode_into_libs=yes - if test yes = "$with_gnu_ld"; then - sys_lib_search_path_spec='/usr/local/lib /usr/gnu/lib /usr/ccs/lib /usr/lib /lib' - else - sys_lib_search_path_spec='/usr/ccs/lib /usr/lib' - case $host_os in - sco3.2v5*) - sys_lib_search_path_spec="$sys_lib_search_path_spec /lib" - ;; - esac - fi - sys_lib_dlsearch_path_spec='/usr/lib' - ;; - -tpf*) - # TPF is a cross-target only. Preferred cross-host = GNU/Linux. - version_type=linux # correct to gnu/linux during the next big refactor - need_lib_prefix=no - need_version=no - library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' - shlibpath_var=LD_LIBRARY_PATH - shlibpath_overrides_runpath=no - hardcode_into_libs=yes - ;; - -uts4*) - version_type=linux # correct to gnu/linux during the next big refactor - library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' - soname_spec='$libname$release$shared_ext$major' - shlibpath_var=LD_LIBRARY_PATH - ;; - -*) - dynamic_linker=no - ;; -esac -AC_MSG_RESULT([$dynamic_linker]) -test no = "$dynamic_linker" && can_build_shared=no - -variables_saved_for_relink="PATH $shlibpath_var $runpath_var" -if test yes = "$GCC"; then - variables_saved_for_relink="$variables_saved_for_relink GCC_EXEC_PREFIX COMPILER_PATH LIBRARY_PATH" -fi - -if test set = "${lt_cv_sys_lib_search_path_spec+set}"; then - sys_lib_search_path_spec=$lt_cv_sys_lib_search_path_spec -fi - -if test set = "${lt_cv_sys_lib_dlsearch_path_spec+set}"; then - sys_lib_dlsearch_path_spec=$lt_cv_sys_lib_dlsearch_path_spec -fi - -# remember unaugmented sys_lib_dlsearch_path content for libtool script decls... -configure_time_dlsearch_path=$sys_lib_dlsearch_path_spec - -# ... but it needs LT_SYS_LIBRARY_PATH munging for other configure-time code -func_munge_path_list sys_lib_dlsearch_path_spec "$LT_SYS_LIBRARY_PATH" - -# to be used as default LT_SYS_LIBRARY_PATH value in generated libtool -configure_time_lt_sys_library_path=$LT_SYS_LIBRARY_PATH - -_LT_DECL([], [variables_saved_for_relink], [1], - [Variables whose values should be saved in libtool wrapper scripts and - restored at link time]) -_LT_DECL([], [need_lib_prefix], [0], - [Do we need the "lib" prefix for modules?]) -_LT_DECL([], [need_version], [0], [Do we need a version for libraries?]) -_LT_DECL([], [version_type], [0], [Library versioning type]) -_LT_DECL([], [runpath_var], [0], [Shared library runtime path variable]) -_LT_DECL([], [shlibpath_var], [0],[Shared library path variable]) -_LT_DECL([], [shlibpath_overrides_runpath], [0], - [Is shlibpath searched before the hard-coded library search path?]) -_LT_DECL([], [libname_spec], [1], [Format of library name prefix]) -_LT_DECL([], [library_names_spec], [1], - [[List of archive names. First name is the real one, the rest are links. - The last name is the one that the linker finds with -lNAME]]) -_LT_DECL([], [soname_spec], [1], - [[The coded name of the library, if different from the real name]]) -_LT_DECL([], [install_override_mode], [1], - [Permission mode override for installation of shared libraries]) -_LT_DECL([], [postinstall_cmds], [2], - [Command to use after installation of a shared archive]) -_LT_DECL([], [postuninstall_cmds], [2], - [Command to use after uninstallation of a shared archive]) -_LT_DECL([], [finish_cmds], [2], - [Commands used to finish a libtool library installation in a directory]) -_LT_DECL([], [finish_eval], [1], - [[As "finish_cmds", except a single script fragment to be evaled but - not shown]]) -_LT_DECL([], [hardcode_into_libs], [0], - [Whether we should hardcode library paths into libraries]) -_LT_DECL([], [sys_lib_search_path_spec], [2], - [Compile-time system search path for libraries]) -_LT_DECL([sys_lib_dlsearch_path_spec], [configure_time_dlsearch_path], [2], - [Detected run-time system search path for libraries]) -_LT_DECL([], [configure_time_lt_sys_library_path], [2], - [Explicit LT_SYS_LIBRARY_PATH set during ./configure time]) -])# _LT_SYS_DYNAMIC_LINKER - - -# _LT_PATH_TOOL_PREFIX(TOOL) -# -------------------------- -# find a file program that can recognize shared library -AC_DEFUN([_LT_PATH_TOOL_PREFIX], -[m4_require([_LT_DECL_EGREP])dnl -AC_MSG_CHECKING([for $1]) -AC_CACHE_VAL(lt_cv_path_MAGIC_CMD, -[case $MAGIC_CMD in -[[\\/*] | ?:[\\/]*]) - lt_cv_path_MAGIC_CMD=$MAGIC_CMD # Let the user override the test with a path. - ;; -*) - lt_save_MAGIC_CMD=$MAGIC_CMD - lt_save_ifs=$IFS; IFS=$PATH_SEPARATOR -dnl $ac_dummy forces splitting on constant user-supplied paths. -dnl POSIX.2 word splitting is done only on the output of word expansions, -dnl not every word. This closes a longstanding sh security hole. - ac_dummy="m4_if([$2], , $PATH, [$2])" - for ac_dir in $ac_dummy; do - IFS=$lt_save_ifs - test -z "$ac_dir" && ac_dir=. - if test -f "$ac_dir/$1"; then - lt_cv_path_MAGIC_CMD=$ac_dir/"$1" - if test -n "$file_magic_test_file"; then - case $deplibs_check_method in - "file_magic "*) - file_magic_regex=`expr "$deplibs_check_method" : "file_magic \(.*\)"` - MAGIC_CMD=$lt_cv_path_MAGIC_CMD - if eval $file_magic_cmd \$file_magic_test_file 2> /dev/null | - $EGREP "$file_magic_regex" > /dev/null; then - : - else - cat <<_LT_EOF 1>&2 - -*** Warning: the command libtool uses to detect shared libraries, -*** $file_magic_cmd, produces output that libtool cannot recognize. -*** The result is that libtool may fail to recognize shared libraries -*** as such. This will affect the creation of libtool libraries that -*** depend on shared libraries, but programs linked with such libtool -*** libraries will work regardless of this problem. Nevertheless, you -*** may want to report the problem to your system manager and/or to -*** bug-libtool@gnu.org - -_LT_EOF - fi ;; - esac - fi - break - fi - done - IFS=$lt_save_ifs - MAGIC_CMD=$lt_save_MAGIC_CMD - ;; -esac]) -MAGIC_CMD=$lt_cv_path_MAGIC_CMD -if test -n "$MAGIC_CMD"; then - AC_MSG_RESULT($MAGIC_CMD) -else - AC_MSG_RESULT(no) -fi -_LT_DECL([], [MAGIC_CMD], [0], - [Used to examine libraries when file_magic_cmd begins with "file"])dnl -])# _LT_PATH_TOOL_PREFIX - -# Old name: -AU_ALIAS([AC_PATH_TOOL_PREFIX], [_LT_PATH_TOOL_PREFIX]) -dnl aclocal-1.4 backwards compatibility: -dnl AC_DEFUN([AC_PATH_TOOL_PREFIX], []) - - -# _LT_PATH_MAGIC -# -------------- -# find a file program that can recognize a shared library -m4_defun([_LT_PATH_MAGIC], -[_LT_PATH_TOOL_PREFIX(${ac_tool_prefix}file, /usr/bin$PATH_SEPARATOR$PATH) -if test -z "$lt_cv_path_MAGIC_CMD"; then - if test -n "$ac_tool_prefix"; then - _LT_PATH_TOOL_PREFIX(file, /usr/bin$PATH_SEPARATOR$PATH) - else - MAGIC_CMD=: - fi -fi -])# _LT_PATH_MAGIC - - -# LT_PATH_LD -# ---------- -# find the pathname to the GNU or non-GNU linker -AC_DEFUN([LT_PATH_LD], -[AC_REQUIRE([AC_PROG_CC])dnl -AC_REQUIRE([AC_CANONICAL_HOST])dnl -AC_REQUIRE([AC_CANONICAL_BUILD])dnl -m4_require([_LT_DECL_SED])dnl -m4_require([_LT_DECL_EGREP])dnl -m4_require([_LT_PROG_ECHO_BACKSLASH])dnl - -AC_ARG_WITH([gnu-ld], - [AS_HELP_STRING([--with-gnu-ld], - [assume the C compiler uses GNU ld @<:@default=no@:>@])], - [test no = "$withval" || with_gnu_ld=yes], - [with_gnu_ld=no])dnl - -ac_prog=ld -if test yes = "$GCC"; then - # Check if gcc -print-prog-name=ld gives a path. - AC_MSG_CHECKING([for ld used by $CC]) - case $host in - *-*-mingw*) - # gcc leaves a trailing carriage return, which upsets mingw - ac_prog=`($CC -print-prog-name=ld) 2>&5 | tr -d '\015'` ;; - *) - ac_prog=`($CC -print-prog-name=ld) 2>&5` ;; - esac - case $ac_prog in - # Accept absolute paths. - [[\\/]]* | ?:[[\\/]]*) - re_direlt='/[[^/]][[^/]]*/\.\./' - # Canonicalize the pathname of ld - ac_prog=`$ECHO "$ac_prog"| $SED 's%\\\\%/%g'` - while $ECHO "$ac_prog" | $GREP "$re_direlt" > /dev/null 2>&1; do - ac_prog=`$ECHO $ac_prog| $SED "s%$re_direlt%/%"` - done - test -z "$LD" && LD=$ac_prog - ;; - "") - # If it fails, then pretend we aren't using GCC. - ac_prog=ld - ;; - *) - # If it is relative, then search for the first ld in PATH. - with_gnu_ld=unknown - ;; - esac -elif test yes = "$with_gnu_ld"; then - AC_MSG_CHECKING([for GNU ld]) -else - AC_MSG_CHECKING([for non-GNU ld]) -fi -AC_CACHE_VAL(lt_cv_path_LD, -[if test -z "$LD"; then - lt_save_ifs=$IFS; IFS=$PATH_SEPARATOR - for ac_dir in $PATH; do - IFS=$lt_save_ifs - test -z "$ac_dir" && ac_dir=. - if test -f "$ac_dir/$ac_prog" || test -f "$ac_dir/$ac_prog$ac_exeext"; then - lt_cv_path_LD=$ac_dir/$ac_prog - # Check to see if the program is GNU ld. I'd rather use --version, - # but apparently some variants of GNU ld only accept -v. - # Break only if it was the GNU/non-GNU ld that we prefer. - case `"$lt_cv_path_LD" -v 2>&1 &1 conftest.i -cat conftest.i conftest.i >conftest2.i -: ${lt_DD:=$DD} -AC_PATH_PROGS_FEATURE_CHECK([lt_DD], [dd], -[if "$ac_path_lt_DD" bs=32 count=1 conftest.out 2>/dev/null; then - cmp -s conftest.i conftest.out \ - && ac_cv_path_lt_DD="$ac_path_lt_DD" ac_path_lt_DD_found=: -fi]) -rm -f conftest.i conftest2.i conftest.out]) -])# _LT_PATH_DD - - -# _LT_CMD_TRUNCATE -# ---------------- -# find command to truncate a binary pipe -m4_defun([_LT_CMD_TRUNCATE], -[m4_require([_LT_PATH_DD]) -AC_CACHE_CHECK([how to truncate binary pipes], [lt_cv_truncate_bin], -[printf 0123456789abcdef0123456789abcdef >conftest.i -cat conftest.i conftest.i >conftest2.i -lt_cv_truncate_bin= -if "$ac_cv_path_lt_DD" bs=32 count=1 conftest.out 2>/dev/null; then - cmp -s conftest.i conftest.out \ - && lt_cv_truncate_bin="$ac_cv_path_lt_DD bs=4096 count=1" -fi -rm -f conftest.i conftest2.i conftest.out -test -z "$lt_cv_truncate_bin" && lt_cv_truncate_bin="$SED -e 4q"]) -_LT_DECL([lt_truncate_bin], [lt_cv_truncate_bin], [1], - [Command to truncate a binary pipe]) -])# _LT_CMD_TRUNCATE - - -# _LT_CHECK_MAGIC_METHOD -# ---------------------- -# how to check for library dependencies -# -- PORTME fill in with the dynamic library characteristics -m4_defun([_LT_CHECK_MAGIC_METHOD], -[m4_require([_LT_DECL_EGREP]) -m4_require([_LT_DECL_OBJDUMP]) -AC_CACHE_CHECK([how to recognize dependent libraries], -lt_cv_deplibs_check_method, -[lt_cv_file_magic_cmd='$MAGIC_CMD' -lt_cv_file_magic_test_file= -lt_cv_deplibs_check_method='unknown' -# Need to set the preceding variable on all platforms that support -# interlibrary dependencies. -# 'none' -- dependencies not supported. -# 'unknown' -- same as none, but documents that we really don't know. -# 'pass_all' -- all dependencies passed with no checks. -# 'test_compile' -- check by making test program. -# 'file_magic [[regex]]' -- check by looking for files in library path -# that responds to the $file_magic_cmd with a given extended regex. -# If you have 'file' or equivalent on your system and you're not sure -# whether 'pass_all' will *always* work, you probably want this one. - -case $host_os in -aix[[4-9]]*) - lt_cv_deplibs_check_method=pass_all - ;; - -beos*) - lt_cv_deplibs_check_method=pass_all - ;; - -bsdi[[45]]*) - lt_cv_deplibs_check_method='file_magic ELF [[0-9]][[0-9]]*-bit [[ML]]SB (shared object|dynamic lib)' - lt_cv_file_magic_cmd='/usr/bin/file -L' - lt_cv_file_magic_test_file=/shlib/libc.so - ;; - -cygwin*) - # func_win32_libid is a shell function defined in ltmain.sh - lt_cv_deplibs_check_method='file_magic ^x86 archive import|^x86 DLL' - lt_cv_file_magic_cmd='func_win32_libid' - ;; - -mingw* | pw32*) - # Base MSYS/MinGW do not provide the 'file' command needed by - # func_win32_libid shell function, so use a weaker test based on 'objdump', - # unless we find 'file', for example because we are cross-compiling. - if ( file / ) >/dev/null 2>&1; then - lt_cv_deplibs_check_method='file_magic ^x86 archive import|^x86 DLL' - lt_cv_file_magic_cmd='func_win32_libid' - else - # Keep this pattern in sync with the one in func_win32_libid. - lt_cv_deplibs_check_method='file_magic file format (pei*-i386(.*architecture: i386)?|pe-arm-wince|pe-x86-64)' - lt_cv_file_magic_cmd='$OBJDUMP -f' - fi - ;; - -cegcc*) - # use the weaker test based on 'objdump'. See mingw*. - lt_cv_deplibs_check_method='file_magic file format pe-arm-.*little(.*architecture: arm)?' - lt_cv_file_magic_cmd='$OBJDUMP -f' - ;; - -darwin* | rhapsody*) - lt_cv_deplibs_check_method=pass_all - ;; - -freebsd* | dragonfly*) - if echo __ELF__ | $CC -E - | $GREP __ELF__ > /dev/null; then - case $host_cpu in - i*86 ) - # Not sure whether the presence of OpenBSD here was a mistake. - # Let's accept both of them until this is cleared up. - lt_cv_deplibs_check_method='file_magic (FreeBSD|OpenBSD|DragonFly)/i[[3-9]]86 (compact )?demand paged shared library' - lt_cv_file_magic_cmd=/usr/bin/file - lt_cv_file_magic_test_file=`echo /usr/lib/libc.so.*` - ;; - esac - else - lt_cv_deplibs_check_method=pass_all - fi - ;; - -haiku*) - lt_cv_deplibs_check_method=pass_all - ;; - -hpux10.20* | hpux11*) - lt_cv_file_magic_cmd=/usr/bin/file - case $host_cpu in - ia64*) - lt_cv_deplibs_check_method='file_magic (s[[0-9]][[0-9]][[0-9]]|ELF-[[0-9]][[0-9]]) shared object file - IA64' - lt_cv_file_magic_test_file=/usr/lib/hpux32/libc.so - ;; - hppa*64*) - [lt_cv_deplibs_check_method='file_magic (s[0-9][0-9][0-9]|ELF[ -][0-9][0-9])(-bit)?( [LM]SB)? shared object( file)?[, -]* PA-RISC [0-9]\.[0-9]'] - lt_cv_file_magic_test_file=/usr/lib/pa20_64/libc.sl - ;; - *) - lt_cv_deplibs_check_method='file_magic (s[[0-9]][[0-9]][[0-9]]|PA-RISC[[0-9]]\.[[0-9]]) shared library' - lt_cv_file_magic_test_file=/usr/lib/libc.sl - ;; - esac - ;; - -interix[[3-9]]*) - # PIC code is broken on Interix 3.x, that's why |\.a not |_pic\.a here - lt_cv_deplibs_check_method='match_pattern /lib[[^/]]+(\.so|\.a)$' - ;; - -irix5* | irix6* | nonstopux*) - case $LD in - *-32|*"-32 ") libmagic=32-bit;; - *-n32|*"-n32 ") libmagic=N32;; - *-64|*"-64 ") libmagic=64-bit;; - *) libmagic=never-match;; - esac - lt_cv_deplibs_check_method=pass_all - ;; - -# This must be glibc/ELF. -linux* | k*bsd*-gnu | kopensolaris*-gnu | gnu*) - lt_cv_deplibs_check_method=pass_all - ;; - -netbsd*) - if echo __ELF__ | $CC -E - | $GREP __ELF__ > /dev/null; then - lt_cv_deplibs_check_method='match_pattern /lib[[^/]]+(\.so\.[[0-9]]+\.[[0-9]]+|_pic\.a)$' - else - lt_cv_deplibs_check_method='match_pattern /lib[[^/]]+(\.so|_pic\.a)$' - fi - ;; - -newos6*) - lt_cv_deplibs_check_method='file_magic ELF [[0-9]][[0-9]]*-bit [[ML]]SB (executable|dynamic lib)' - lt_cv_file_magic_cmd=/usr/bin/file - lt_cv_file_magic_test_file=/usr/lib/libnls.so - ;; - -*nto* | *qnx*) - lt_cv_deplibs_check_method=pass_all - ;; - -openbsd* | bitrig*) - if test -z "`echo __ELF__ | $CC -E - | $GREP __ELF__`"; then - lt_cv_deplibs_check_method='match_pattern /lib[[^/]]+(\.so\.[[0-9]]+\.[[0-9]]+|\.so|_pic\.a)$' - else - lt_cv_deplibs_check_method='match_pattern /lib[[^/]]+(\.so\.[[0-9]]+\.[[0-9]]+|_pic\.a)$' - fi - ;; - -osf3* | osf4* | osf5*) - lt_cv_deplibs_check_method=pass_all - ;; - -rdos*) - lt_cv_deplibs_check_method=pass_all - ;; - -solaris*) - lt_cv_deplibs_check_method=pass_all - ;; - -sysv5* | sco3.2v5* | sco5v6* | unixware* | OpenUNIX* | sysv4*uw2*) - lt_cv_deplibs_check_method=pass_all - ;; - -sysv4 | sysv4.3*) - case $host_vendor in - motorola) - lt_cv_deplibs_check_method='file_magic ELF [[0-9]][[0-9]]*-bit [[ML]]SB (shared object|dynamic lib) M[[0-9]][[0-9]]* Version [[0-9]]' - lt_cv_file_magic_test_file=`echo /usr/lib/libc.so*` - ;; - ncr) - lt_cv_deplibs_check_method=pass_all - ;; - sequent) - lt_cv_file_magic_cmd='/bin/file' - lt_cv_deplibs_check_method='file_magic ELF [[0-9]][[0-9]]*-bit [[LM]]SB (shared object|dynamic lib )' - ;; - sni) - lt_cv_file_magic_cmd='/bin/file' - lt_cv_deplibs_check_method="file_magic ELF [[0-9]][[0-9]]*-bit [[LM]]SB dynamic lib" - lt_cv_file_magic_test_file=/lib/libc.so - ;; - siemens) - lt_cv_deplibs_check_method=pass_all - ;; - pc) - lt_cv_deplibs_check_method=pass_all - ;; - esac - ;; - -tpf*) - lt_cv_deplibs_check_method=pass_all - ;; -os2*) - lt_cv_deplibs_check_method=pass_all - ;; -esac -]) - -file_magic_glob= -want_nocaseglob=no -if test "$build" = "$host"; then - case $host_os in - mingw* | pw32*) - if ( shopt | grep nocaseglob ) >/dev/null 2>&1; then - want_nocaseglob=yes - else - file_magic_glob=`echo aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ | $SED -e "s/\(..\)/s\/[[\1]]\/[[\1]]\/g;/g"` - fi - ;; - esac -fi - -file_magic_cmd=$lt_cv_file_magic_cmd -deplibs_check_method=$lt_cv_deplibs_check_method -test -z "$deplibs_check_method" && deplibs_check_method=unknown - -_LT_DECL([], [deplibs_check_method], [1], - [Method to check whether dependent libraries are shared objects]) -_LT_DECL([], [file_magic_cmd], [1], - [Command to use when deplibs_check_method = "file_magic"]) -_LT_DECL([], [file_magic_glob], [1], - [How to find potential files when deplibs_check_method = "file_magic"]) -_LT_DECL([], [want_nocaseglob], [1], - [Find potential files using nocaseglob when deplibs_check_method = "file_magic"]) -])# _LT_CHECK_MAGIC_METHOD - - -# LT_PATH_NM -# ---------- -# find the pathname to a BSD- or MS-compatible name lister -AC_DEFUN([LT_PATH_NM], -[AC_REQUIRE([AC_PROG_CC])dnl -AC_CACHE_CHECK([for BSD- or MS-compatible name lister (nm)], lt_cv_path_NM, -[if test -n "$NM"; then - # Let the user override the test. - lt_cv_path_NM=$NM -else - lt_nm_to_check=${ac_tool_prefix}nm - if test -n "$ac_tool_prefix" && test "$build" = "$host"; then - lt_nm_to_check="$lt_nm_to_check nm" - fi - for lt_tmp_nm in $lt_nm_to_check; do - lt_save_ifs=$IFS; IFS=$PATH_SEPARATOR - for ac_dir in $PATH /usr/ccs/bin/elf /usr/ccs/bin /usr/ucb /bin; do - IFS=$lt_save_ifs - test -z "$ac_dir" && ac_dir=. - tmp_nm=$ac_dir/$lt_tmp_nm - if test -f "$tmp_nm" || test -f "$tmp_nm$ac_exeext"; then - # Check to see if the nm accepts a BSD-compat flag. - # Adding the 'sed 1q' prevents false positives on HP-UX, which says: - # nm: unknown option "B" ignored - # Tru64's nm complains that /dev/null is an invalid object file - # MSYS converts /dev/null to NUL, MinGW nm treats NUL as empty - case $build_os in - mingw*) lt_bad_file=conftest.nm/nofile ;; - *) lt_bad_file=/dev/null ;; - esac - case `"$tmp_nm" -B $lt_bad_file 2>&1 | sed '1q'` in - *$lt_bad_file* | *'Invalid file or object type'*) - lt_cv_path_NM="$tmp_nm -B" - break 2 - ;; - *) - case `"$tmp_nm" -p /dev/null 2>&1 | sed '1q'` in - */dev/null*) - lt_cv_path_NM="$tmp_nm -p" - break 2 - ;; - *) - lt_cv_path_NM=${lt_cv_path_NM="$tmp_nm"} # keep the first match, but - continue # so that we can try to find one that supports BSD flags - ;; - esac - ;; - esac - fi - done - IFS=$lt_save_ifs - done - : ${lt_cv_path_NM=no} -fi]) -if test no != "$lt_cv_path_NM"; then - NM=$lt_cv_path_NM -else - # Didn't find any BSD compatible name lister, look for dumpbin. - if test -n "$DUMPBIN"; then : - # Let the user override the test. - else - AC_CHECK_TOOLS(DUMPBIN, [dumpbin "link -dump"], :) - case `$DUMPBIN -symbols -headers /dev/null 2>&1 | sed '1q'` in - *COFF*) - DUMPBIN="$DUMPBIN -symbols -headers" - ;; - *) - DUMPBIN=: - ;; - esac - fi - AC_SUBST([DUMPBIN]) - if test : != "$DUMPBIN"; then - NM=$DUMPBIN - fi -fi -test -z "$NM" && NM=nm -AC_SUBST([NM]) -_LT_DECL([], [NM], [1], [A BSD- or MS-compatible name lister])dnl - -AC_CACHE_CHECK([the name lister ($NM) interface], [lt_cv_nm_interface], - [lt_cv_nm_interface="BSD nm" - echo "int some_variable = 0;" > conftest.$ac_ext - (eval echo "\"\$as_me:$LINENO: $ac_compile\"" >&AS_MESSAGE_LOG_FD) - (eval "$ac_compile" 2>conftest.err) - cat conftest.err >&AS_MESSAGE_LOG_FD - (eval echo "\"\$as_me:$LINENO: $NM \\\"conftest.$ac_objext\\\"\"" >&AS_MESSAGE_LOG_FD) - (eval "$NM \"conftest.$ac_objext\"" 2>conftest.err > conftest.out) - cat conftest.err >&AS_MESSAGE_LOG_FD - (eval echo "\"\$as_me:$LINENO: output\"" >&AS_MESSAGE_LOG_FD) - cat conftest.out >&AS_MESSAGE_LOG_FD - if $GREP 'External.*some_variable' conftest.out > /dev/null; then - lt_cv_nm_interface="MS dumpbin" - fi - rm -f conftest*]) -])# LT_PATH_NM - -# Old names: -AU_ALIAS([AM_PROG_NM], [LT_PATH_NM]) -AU_ALIAS([AC_PROG_NM], [LT_PATH_NM]) -dnl aclocal-1.4 backwards compatibility: -dnl AC_DEFUN([AM_PROG_NM], []) -dnl AC_DEFUN([AC_PROG_NM], []) - -# _LT_CHECK_SHAREDLIB_FROM_LINKLIB -# -------------------------------- -# how to determine the name of the shared library -# associated with a specific link library. -# -- PORTME fill in with the dynamic library characteristics -m4_defun([_LT_CHECK_SHAREDLIB_FROM_LINKLIB], -[m4_require([_LT_DECL_EGREP]) -m4_require([_LT_DECL_OBJDUMP]) -m4_require([_LT_DECL_DLLTOOL]) -AC_CACHE_CHECK([how to associate runtime and link libraries], -lt_cv_sharedlib_from_linklib_cmd, -[lt_cv_sharedlib_from_linklib_cmd='unknown' - -case $host_os in -cygwin* | mingw* | pw32* | cegcc*) - # two different shell functions defined in ltmain.sh; - # decide which one to use based on capabilities of $DLLTOOL - case `$DLLTOOL --help 2>&1` in - *--identify-strict*) - lt_cv_sharedlib_from_linklib_cmd=func_cygming_dll_for_implib - ;; - *) - lt_cv_sharedlib_from_linklib_cmd=func_cygming_dll_for_implib_fallback - ;; - esac - ;; -*) - # fallback: assume linklib IS sharedlib - lt_cv_sharedlib_from_linklib_cmd=$ECHO - ;; -esac -]) -sharedlib_from_linklib_cmd=$lt_cv_sharedlib_from_linklib_cmd -test -z "$sharedlib_from_linklib_cmd" && sharedlib_from_linklib_cmd=$ECHO - -_LT_DECL([], [sharedlib_from_linklib_cmd], [1], - [Command to associate shared and link libraries]) -])# _LT_CHECK_SHAREDLIB_FROM_LINKLIB - - -# _LT_PATH_MANIFEST_TOOL -# ---------------------- -# locate the manifest tool -m4_defun([_LT_PATH_MANIFEST_TOOL], -[AC_CHECK_TOOL(MANIFEST_TOOL, mt, :) -test -z "$MANIFEST_TOOL" && MANIFEST_TOOL=mt -AC_CACHE_CHECK([if $MANIFEST_TOOL is a manifest tool], [lt_cv_path_mainfest_tool], - [lt_cv_path_mainfest_tool=no - echo "$as_me:$LINENO: $MANIFEST_TOOL '-?'" >&AS_MESSAGE_LOG_FD - $MANIFEST_TOOL '-?' 2>conftest.err > conftest.out - cat conftest.err >&AS_MESSAGE_LOG_FD - if $GREP 'Manifest Tool' conftest.out > /dev/null; then - lt_cv_path_mainfest_tool=yes - fi - rm -f conftest*]) -if test yes != "$lt_cv_path_mainfest_tool"; then - MANIFEST_TOOL=: -fi -_LT_DECL([], [MANIFEST_TOOL], [1], [Manifest tool])dnl -])# _LT_PATH_MANIFEST_TOOL - - -# _LT_DLL_DEF_P([FILE]) -# --------------------- -# True iff FILE is a Windows DLL '.def' file. -# Keep in sync with func_dll_def_p in the libtool script -AC_DEFUN([_LT_DLL_DEF_P], -[dnl - test DEF = "`$SED -n dnl - -e '\''s/^[[ ]]*//'\'' dnl Strip leading whitespace - -e '\''/^\(;.*\)*$/d'\'' dnl Delete empty lines and comments - -e '\''s/^\(EXPORTS\|LIBRARY\)\([[ ]].*\)*$/DEF/p'\'' dnl - -e q dnl Only consider the first "real" line - $1`" dnl -])# _LT_DLL_DEF_P - - -# LT_LIB_M -# -------- -# check for math library -AC_DEFUN([LT_LIB_M], -[AC_REQUIRE([AC_CANONICAL_HOST])dnl -LIBM= -case $host in -*-*-beos* | *-*-cegcc* | *-*-cygwin* | *-*-haiku* | *-*-pw32* | *-*-darwin*) - # These system don't have libm, or don't need it - ;; -*-ncr-sysv4.3*) - AC_CHECK_LIB(mw, _mwvalidcheckl, LIBM=-lmw) - AC_CHECK_LIB(m, cos, LIBM="$LIBM -lm") - ;; -*) - AC_CHECK_LIB(m, cos, LIBM=-lm) - ;; -esac -AC_SUBST([LIBM]) -])# LT_LIB_M - -# Old name: -AU_ALIAS([AC_CHECK_LIBM], [LT_LIB_M]) -dnl aclocal-1.4 backwards compatibility: -dnl AC_DEFUN([AC_CHECK_LIBM], []) - - -# _LT_COMPILER_NO_RTTI([TAGNAME]) -# ------------------------------- -m4_defun([_LT_COMPILER_NO_RTTI], -[m4_require([_LT_TAG_COMPILER])dnl - -_LT_TAGVAR(lt_prog_compiler_no_builtin_flag, $1)= - -if test yes = "$GCC"; then - case $cc_basename in - nvcc*) - _LT_TAGVAR(lt_prog_compiler_no_builtin_flag, $1)=' -Xcompiler -fno-builtin' ;; - *) - _LT_TAGVAR(lt_prog_compiler_no_builtin_flag, $1)=' -fno-builtin' ;; - esac - - _LT_COMPILER_OPTION([if $compiler supports -fno-rtti -fno-exceptions], - lt_cv_prog_compiler_rtti_exceptions, - [-fno-rtti -fno-exceptions], [], - [_LT_TAGVAR(lt_prog_compiler_no_builtin_flag, $1)="$_LT_TAGVAR(lt_prog_compiler_no_builtin_flag, $1) -fno-rtti -fno-exceptions"]) -fi -_LT_TAGDECL([no_builtin_flag], [lt_prog_compiler_no_builtin_flag], [1], - [Compiler flag to turn off builtin functions]) -])# _LT_COMPILER_NO_RTTI - - -# _LT_CMD_GLOBAL_SYMBOLS -# ---------------------- -m4_defun([_LT_CMD_GLOBAL_SYMBOLS], -[AC_REQUIRE([AC_CANONICAL_HOST])dnl -AC_REQUIRE([AC_PROG_CC])dnl -AC_REQUIRE([AC_PROG_AWK])dnl -AC_REQUIRE([LT_PATH_NM])dnl -AC_REQUIRE([LT_PATH_LD])dnl -m4_require([_LT_DECL_SED])dnl -m4_require([_LT_DECL_EGREP])dnl -m4_require([_LT_TAG_COMPILER])dnl - -# Check for command to grab the raw symbol name followed by C symbol from nm. -AC_MSG_CHECKING([command to parse $NM output from $compiler object]) -AC_CACHE_VAL([lt_cv_sys_global_symbol_pipe], -[ -# These are sane defaults that work on at least a few old systems. -# [They come from Ultrix. What could be older than Ultrix?!! ;)] - -# Character class describing NM global symbol codes. -symcode='[[BCDEGRST]]' - -# Regexp to match symbols that can be accessed directly from C. -sympat='\([[_A-Za-z]][[_A-Za-z0-9]]*\)' - -# Define system-specific variables. -case $host_os in -aix*) - symcode='[[BCDT]]' - ;; -cygwin* | mingw* | pw32* | cegcc*) - symcode='[[ABCDGISTW]]' - ;; -hpux*) - if test ia64 = "$host_cpu"; then - symcode='[[ABCDEGRST]]' - fi - ;; -irix* | nonstopux*) - symcode='[[BCDEGRST]]' - ;; -osf*) - symcode='[[BCDEGQRST]]' - ;; -solaris*) - symcode='[[BDRT]]' - ;; -sco3.2v5*) - symcode='[[DT]]' - ;; -sysv4.2uw2*) - symcode='[[DT]]' - ;; -sysv5* | sco5v6* | unixware* | OpenUNIX*) - symcode='[[ABDT]]' - ;; -sysv4) - symcode='[[DFNSTU]]' - ;; -esac - -# If we're using GNU nm, then use its standard symbol codes. -case `$NM -V 2>&1` in -*GNU* | *'with BFD'*) - symcode='[[ABCDGIRSTW]]' ;; -esac - -if test "$lt_cv_nm_interface" = "MS dumpbin"; then - # Gets list of data symbols to import. - lt_cv_sys_global_symbol_to_import="sed -n -e 's/^I .* \(.*\)$/\1/p'" - # Adjust the below global symbol transforms to fixup imported variables. - lt_cdecl_hook=" -e 's/^I .* \(.*\)$/extern __declspec(dllimport) char \1;/p'" - lt_c_name_hook=" -e 's/^I .* \(.*\)$/ {\"\1\", (void *) 0},/p'" - lt_c_name_lib_hook="\ - -e 's/^I .* \(lib.*\)$/ {\"\1\", (void *) 0},/p'\ - -e 's/^I .* \(.*\)$/ {\"lib\1\", (void *) 0},/p'" -else - # Disable hooks by default. - lt_cv_sys_global_symbol_to_import= - lt_cdecl_hook= - lt_c_name_hook= - lt_c_name_lib_hook= -fi - -# Transform an extracted symbol line into a proper C declaration. -# Some systems (esp. on ia64) link data and code symbols differently, -# so use this general approach. -lt_cv_sys_global_symbol_to_cdecl="sed -n"\ -$lt_cdecl_hook\ -" -e 's/^T .* \(.*\)$/extern int \1();/p'"\ -" -e 's/^$symcode$symcode* .* \(.*\)$/extern char \1;/p'" - -# Transform an extracted symbol line into symbol name and symbol address -lt_cv_sys_global_symbol_to_c_name_address="sed -n"\ -$lt_c_name_hook\ -" -e 's/^: \(.*\) .*$/ {\"\1\", (void *) 0},/p'"\ -" -e 's/^$symcode$symcode* .* \(.*\)$/ {\"\1\", (void *) \&\1},/p'" - -# Transform an extracted symbol line into symbol name with lib prefix and -# symbol address. -lt_cv_sys_global_symbol_to_c_name_address_lib_prefix="sed -n"\ -$lt_c_name_lib_hook\ -" -e 's/^: \(.*\) .*$/ {\"\1\", (void *) 0},/p'"\ -" -e 's/^$symcode$symcode* .* \(lib.*\)$/ {\"\1\", (void *) \&\1},/p'"\ -" -e 's/^$symcode$symcode* .* \(.*\)$/ {\"lib\1\", (void *) \&\1},/p'" - -# Handle CRLF in mingw tool chain -opt_cr= -case $build_os in -mingw*) - opt_cr=`$ECHO 'x\{0,1\}' | tr x '\015'` # option cr in regexp - ;; -esac - -# Try without a prefix underscore, then with it. -for ac_symprfx in "" "_"; do - - # Transform symcode, sympat, and symprfx into a raw symbol and a C symbol. - symxfrm="\\1 $ac_symprfx\\2 \\2" - - # Write the raw and C identifiers. - if test "$lt_cv_nm_interface" = "MS dumpbin"; then - # Fake it for dumpbin and say T for any non-static function, - # D for any global variable and I for any imported variable. - # Also find C++ and __fastcall symbols from MSVC++, - # which start with @ or ?. - lt_cv_sys_global_symbol_pipe="$AWK ['"\ -" {last_section=section; section=\$ 3};"\ -" /^COFF SYMBOL TABLE/{for(i in hide) delete hide[i]};"\ -" /Section length .*#relocs.*(pick any)/{hide[last_section]=1};"\ -" /^ *Symbol name *: /{split(\$ 0,sn,\":\"); si=substr(sn[2],2)};"\ -" /^ *Type *: code/{print \"T\",si,substr(si,length(prfx))};"\ -" /^ *Type *: data/{print \"I\",si,substr(si,length(prfx))};"\ -" \$ 0!~/External *\|/{next};"\ -" / 0+ UNDEF /{next}; / UNDEF \([^|]\)*()/{next};"\ -" {if(hide[section]) next};"\ -" {f=\"D\"}; \$ 0~/\(\).*\|/{f=\"T\"};"\ -" {split(\$ 0,a,/\||\r/); split(a[2],s)};"\ -" s[1]~/^[@?]/{print f,s[1],s[1]; next};"\ -" s[1]~prfx {split(s[1],t,\"@\"); print f,t[1],substr(t[1],length(prfx))}"\ -" ' prfx=^$ac_symprfx]" - else - lt_cv_sys_global_symbol_pipe="sed -n -e 's/^.*[[ ]]\($symcode$symcode*\)[[ ]][[ ]]*$ac_symprfx$sympat$opt_cr$/$symxfrm/p'" - fi - lt_cv_sys_global_symbol_pipe="$lt_cv_sys_global_symbol_pipe | sed '/ __gnu_lto/d'" - - # Check to see that the pipe works correctly. - pipe_works=no - - rm -f conftest* - cat > conftest.$ac_ext <<_LT_EOF -#ifdef __cplusplus -extern "C" { -#endif -char nm_test_var; -void nm_test_func(void); -void nm_test_func(void){} -#ifdef __cplusplus -} -#endif -int main(){nm_test_var='a';nm_test_func();return(0);} -_LT_EOF - - if AC_TRY_EVAL(ac_compile); then - # Now try to grab the symbols. - nlist=conftest.nm - if AC_TRY_EVAL(NM conftest.$ac_objext \| "$lt_cv_sys_global_symbol_pipe" \> $nlist) && test -s "$nlist"; then - # Try sorting and uniquifying the output. - if sort "$nlist" | uniq > "$nlist"T; then - mv -f "$nlist"T "$nlist" - else - rm -f "$nlist"T - fi - - # Make sure that we snagged all the symbols we need. - if $GREP ' nm_test_var$' "$nlist" >/dev/null; then - if $GREP ' nm_test_func$' "$nlist" >/dev/null; then - cat <<_LT_EOF > conftest.$ac_ext -/* Keep this code in sync between libtool.m4, ltmain, lt_system.h, and tests. */ -#if defined _WIN32 || defined __CYGWIN__ || defined _WIN32_WCE -/* DATA imports from DLLs on WIN32 can't be const, because runtime - relocations are performed -- see ld's documentation on pseudo-relocs. */ -# define LT@&t@_DLSYM_CONST -#elif defined __osf__ -/* This system does not cope well with relocations in const data. */ -# define LT@&t@_DLSYM_CONST -#else -# define LT@&t@_DLSYM_CONST const -#endif - -#ifdef __cplusplus -extern "C" { -#endif - -_LT_EOF - # Now generate the symbol file. - eval "$lt_cv_sys_global_symbol_to_cdecl"' < "$nlist" | $GREP -v main >> conftest.$ac_ext' - - cat <<_LT_EOF >> conftest.$ac_ext - -/* The mapping between symbol names and symbols. */ -LT@&t@_DLSYM_CONST struct { - const char *name; - void *address; -} -lt__PROGRAM__LTX_preloaded_symbols[[]] = -{ - { "@PROGRAM@", (void *) 0 }, -_LT_EOF - $SED "s/^$symcode$symcode* .* \(.*\)$/ {\"\1\", (void *) \&\1},/" < "$nlist" | $GREP -v main >> conftest.$ac_ext - cat <<\_LT_EOF >> conftest.$ac_ext - {0, (void *) 0} -}; - -/* This works around a problem in FreeBSD linker */ -#ifdef FREEBSD_WORKAROUND -static const void *lt_preloaded_setup() { - return lt__PROGRAM__LTX_preloaded_symbols; -} -#endif - -#ifdef __cplusplus -} -#endif -_LT_EOF - # Now try linking the two files. - mv conftest.$ac_objext conftstm.$ac_objext - lt_globsym_save_LIBS=$LIBS - lt_globsym_save_CFLAGS=$CFLAGS - LIBS=conftstm.$ac_objext - CFLAGS="$CFLAGS$_LT_TAGVAR(lt_prog_compiler_no_builtin_flag, $1)" - if AC_TRY_EVAL(ac_link) && test -s conftest$ac_exeext; then - pipe_works=yes - fi - LIBS=$lt_globsym_save_LIBS - CFLAGS=$lt_globsym_save_CFLAGS - else - echo "cannot find nm_test_func in $nlist" >&AS_MESSAGE_LOG_FD - fi - else - echo "cannot find nm_test_var in $nlist" >&AS_MESSAGE_LOG_FD - fi - else - echo "cannot run $lt_cv_sys_global_symbol_pipe" >&AS_MESSAGE_LOG_FD - fi - else - echo "$progname: failed program was:" >&AS_MESSAGE_LOG_FD - cat conftest.$ac_ext >&5 - fi - rm -rf conftest* conftst* - - # Do not use the global_symbol_pipe unless it works. - if test yes = "$pipe_works"; then - break - else - lt_cv_sys_global_symbol_pipe= - fi -done -]) -if test -z "$lt_cv_sys_global_symbol_pipe"; then - lt_cv_sys_global_symbol_to_cdecl= -fi -if test -z "$lt_cv_sys_global_symbol_pipe$lt_cv_sys_global_symbol_to_cdecl"; then - AC_MSG_RESULT(failed) -else - AC_MSG_RESULT(ok) -fi - -# Response file support. -if test "$lt_cv_nm_interface" = "MS dumpbin"; then - nm_file_list_spec='@' -elif $NM --help 2>/dev/null | grep '[[@]]FILE' >/dev/null; then - nm_file_list_spec='@' -fi - -_LT_DECL([global_symbol_pipe], [lt_cv_sys_global_symbol_pipe], [1], - [Take the output of nm and produce a listing of raw symbols and C names]) -_LT_DECL([global_symbol_to_cdecl], [lt_cv_sys_global_symbol_to_cdecl], [1], - [Transform the output of nm in a proper C declaration]) -_LT_DECL([global_symbol_to_import], [lt_cv_sys_global_symbol_to_import], [1], - [Transform the output of nm into a list of symbols to manually relocate]) -_LT_DECL([global_symbol_to_c_name_address], - [lt_cv_sys_global_symbol_to_c_name_address], [1], - [Transform the output of nm in a C name address pair]) -_LT_DECL([global_symbol_to_c_name_address_lib_prefix], - [lt_cv_sys_global_symbol_to_c_name_address_lib_prefix], [1], - [Transform the output of nm in a C name address pair when lib prefix is needed]) -_LT_DECL([nm_interface], [lt_cv_nm_interface], [1], - [The name lister interface]) -_LT_DECL([], [nm_file_list_spec], [1], - [Specify filename containing input files for $NM]) -]) # _LT_CMD_GLOBAL_SYMBOLS - - -# _LT_COMPILER_PIC([TAGNAME]) -# --------------------------- -m4_defun([_LT_COMPILER_PIC], -[m4_require([_LT_TAG_COMPILER])dnl -_LT_TAGVAR(lt_prog_compiler_wl, $1)= -_LT_TAGVAR(lt_prog_compiler_pic, $1)= -_LT_TAGVAR(lt_prog_compiler_static, $1)= - -m4_if([$1], [CXX], [ - # C++ specific cases for pic, static, wl, etc. - if test yes = "$GXX"; then - _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' - _LT_TAGVAR(lt_prog_compiler_static, $1)='-static' - - case $host_os in - aix*) - # All AIX code is PIC. - if test ia64 = "$host_cpu"; then - # AIX 5 now supports IA64 processor - _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic' - fi - _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC' - ;; - - amigaos*) - case $host_cpu in - powerpc) - # see comment about AmigaOS4 .so support - _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC' - ;; - m68k) - # FIXME: we need at least 68020 code to build shared libraries, but - # adding the '-m68020' flag to GCC prevents building anything better, - # like '-m68040'. - _LT_TAGVAR(lt_prog_compiler_pic, $1)='-m68020 -resident32 -malways-restore-a4' - ;; - esac - ;; - - beos* | irix5* | irix6* | nonstopux* | osf3* | osf4* | osf5*) - # PIC is the default for these OSes. - ;; - mingw* | cygwin* | os2* | pw32* | cegcc*) - # This hack is so that the source file can tell whether it is being - # built for inclusion in a dll (and should export symbols for example). - # Although the cygwin gcc ignores -fPIC, still need this for old-style - # (--disable-auto-import) libraries - m4_if([$1], [GCJ], [], - [_LT_TAGVAR(lt_prog_compiler_pic, $1)='-DDLL_EXPORT']) - case $host_os in - os2*) - _LT_TAGVAR(lt_prog_compiler_static, $1)='$wl-static' - ;; - esac - ;; - darwin* | rhapsody*) - # PIC is the default on this platform - # Common symbols not allowed in MH_DYLIB files - _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fno-common' - ;; - *djgpp*) - # DJGPP does not support shared libraries at all - _LT_TAGVAR(lt_prog_compiler_pic, $1)= - ;; - haiku*) - # PIC is the default for Haiku. - # The "-static" flag exists, but is broken. - _LT_TAGVAR(lt_prog_compiler_static, $1)= - ;; - interix[[3-9]]*) - # Interix 3.x gcc -fpic/-fPIC options generate broken code. - # Instead, we relocate shared libraries at runtime. - ;; - sysv4*MP*) - if test -d /usr/nec; then - _LT_TAGVAR(lt_prog_compiler_pic, $1)=-Kconform_pic - fi - ;; - hpux*) - # PIC is the default for 64-bit PA HP-UX, but not for 32-bit - # PA HP-UX. On IA64 HP-UX, PIC is the default but the pic flag - # sets the default TLS model and affects inlining. - case $host_cpu in - hppa*64*) - ;; - *) - _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC' - ;; - esac - ;; - *qnx* | *nto*) - # QNX uses GNU C++, but need to define -shared option too, otherwise - # it will coredump. - _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC -shared' - ;; - *) - _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC' - ;; - esac - else - case $host_os in - aix[[4-9]]*) - # All AIX code is PIC. - if test ia64 = "$host_cpu"; then - # AIX 5 now supports IA64 processor - _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic' - else - _LT_TAGVAR(lt_prog_compiler_static, $1)='-bnso -bI:/lib/syscalls.exp' - fi - ;; - chorus*) - case $cc_basename in - cxch68*) - # Green Hills C++ Compiler - # _LT_TAGVAR(lt_prog_compiler_static, $1)="--no_auto_instantiation -u __main -u __premain -u _abort -r $COOL_DIR/lib/libOrb.a $MVME_DIR/lib/CC/libC.a $MVME_DIR/lib/classix/libcx.s.a" - ;; - esac - ;; - mingw* | cygwin* | os2* | pw32* | cegcc*) - # This hack is so that the source file can tell whether it is being - # built for inclusion in a dll (and should export symbols for example). - m4_if([$1], [GCJ], [], - [_LT_TAGVAR(lt_prog_compiler_pic, $1)='-DDLL_EXPORT']) - ;; - dgux*) - case $cc_basename in - ec++*) - _LT_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC' - ;; - ghcx*) - # Green Hills C++ Compiler - _LT_TAGVAR(lt_prog_compiler_pic, $1)='-pic' - ;; - *) - ;; - esac - ;; - freebsd* | dragonfly*) - # FreeBSD uses GNU C++ - ;; - hpux9* | hpux10* | hpux11*) - case $cc_basename in - CC*) - _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' - _LT_TAGVAR(lt_prog_compiler_static, $1)='$wl-a ${wl}archive' - if test ia64 != "$host_cpu"; then - _LT_TAGVAR(lt_prog_compiler_pic, $1)='+Z' - fi - ;; - aCC*) - _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' - _LT_TAGVAR(lt_prog_compiler_static, $1)='$wl-a ${wl}archive' - case $host_cpu in - hppa*64*|ia64*) - # +Z the default - ;; - *) - _LT_TAGVAR(lt_prog_compiler_pic, $1)='+Z' - ;; - esac - ;; - *) - ;; - esac - ;; - interix*) - # This is c89, which is MS Visual C++ (no shared libs) - # Anyone wants to do a port? - ;; - irix5* | irix6* | nonstopux*) - case $cc_basename in - CC*) - _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' - _LT_TAGVAR(lt_prog_compiler_static, $1)='-non_shared' - # CC pic flag -KPIC is the default. - ;; - *) - ;; - esac - ;; - linux* | k*bsd*-gnu | kopensolaris*-gnu | gnu*) - case $cc_basename in - KCC*) - # KAI C++ Compiler - _LT_TAGVAR(lt_prog_compiler_wl, $1)='--backend -Wl,' - _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC' - ;; - ecpc* ) - # old Intel C++ for x86_64, which still supported -KPIC. - _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' - _LT_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC' - _LT_TAGVAR(lt_prog_compiler_static, $1)='-static' - ;; - icpc* ) - # Intel C++, used to be incompatible with GCC. - # ICC 10 doesn't accept -KPIC any more. - _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' - _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC' - _LT_TAGVAR(lt_prog_compiler_static, $1)='-static' - ;; - pgCC* | pgcpp*) - # Portland Group C++ compiler - _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' - _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fpic' - _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic' - ;; - cxx*) - # Compaq C++ - # Make sure the PIC flag is empty. It appears that all Alpha - # Linux and Compaq Tru64 Unix objects are PIC. - _LT_TAGVAR(lt_prog_compiler_pic, $1)= - _LT_TAGVAR(lt_prog_compiler_static, $1)='-non_shared' - ;; - xlc* | xlC* | bgxl[[cC]]* | mpixl[[cC]]*) - # IBM XL 8.0, 9.0 on PPC and BlueGene - _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' - _LT_TAGVAR(lt_prog_compiler_pic, $1)='-qpic' - _LT_TAGVAR(lt_prog_compiler_static, $1)='-qstaticlink' - ;; - *) - case `$CC -V 2>&1 | sed 5q` in - *Sun\ C*) - # Sun C++ 5.9 - _LT_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC' - _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic' - _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Qoption ld ' - ;; - esac - ;; - esac - ;; - lynxos*) - ;; - m88k*) - ;; - mvs*) - case $cc_basename in - cxx*) - _LT_TAGVAR(lt_prog_compiler_pic, $1)='-W c,exportall' - ;; - *) - ;; - esac - ;; - netbsd*) - ;; - *qnx* | *nto*) - # QNX uses GNU C++, but need to define -shared option too, otherwise - # it will coredump. - _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC -shared' - ;; - osf3* | osf4* | osf5*) - case $cc_basename in - KCC*) - _LT_TAGVAR(lt_prog_compiler_wl, $1)='--backend -Wl,' - ;; - RCC*) - # Rational C++ 2.4.1 - _LT_TAGVAR(lt_prog_compiler_pic, $1)='-pic' - ;; - cxx*) - # Digital/Compaq C++ - _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' - # Make sure the PIC flag is empty. It appears that all Alpha - # Linux and Compaq Tru64 Unix objects are PIC. - _LT_TAGVAR(lt_prog_compiler_pic, $1)= - _LT_TAGVAR(lt_prog_compiler_static, $1)='-non_shared' - ;; - *) - ;; - esac - ;; - psos*) - ;; - solaris*) - case $cc_basename in - CC* | sunCC*) - # Sun C++ 4.2, 5.x and Centerline C++ - _LT_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC' - _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic' - _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Qoption ld ' - ;; - gcx*) - # Green Hills C++ Compiler - _LT_TAGVAR(lt_prog_compiler_pic, $1)='-PIC' - ;; - *) - ;; - esac - ;; - sunos4*) - case $cc_basename in - CC*) - # Sun C++ 4.x - _LT_TAGVAR(lt_prog_compiler_pic, $1)='-pic' - _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic' - ;; - lcc*) - # Lucid - _LT_TAGVAR(lt_prog_compiler_pic, $1)='-pic' - ;; - *) - ;; - esac - ;; - sysv5* | unixware* | sco3.2v5* | sco5v6* | OpenUNIX*) - case $cc_basename in - CC*) - _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' - _LT_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC' - _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic' - ;; - esac - ;; - tandem*) - case $cc_basename in - NCC*) - # NonStop-UX NCC 3.20 - _LT_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC' - ;; - *) - ;; - esac - ;; - vxworks*) - ;; - *) - _LT_TAGVAR(lt_prog_compiler_can_build_shared, $1)=no - ;; - esac - fi -], -[ - if test yes = "$GCC"; then - _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' - _LT_TAGVAR(lt_prog_compiler_static, $1)='-static' - - case $host_os in - aix*) - # All AIX code is PIC. - if test ia64 = "$host_cpu"; then - # AIX 5 now supports IA64 processor - _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic' - fi - _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC' - ;; - - amigaos*) - case $host_cpu in - powerpc) - # see comment about AmigaOS4 .so support - _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC' - ;; - m68k) - # FIXME: we need at least 68020 code to build shared libraries, but - # adding the '-m68020' flag to GCC prevents building anything better, - # like '-m68040'. - _LT_TAGVAR(lt_prog_compiler_pic, $1)='-m68020 -resident32 -malways-restore-a4' - ;; - esac - ;; - - beos* | irix5* | irix6* | nonstopux* | osf3* | osf4* | osf5*) - # PIC is the default for these OSes. - ;; - - mingw* | cygwin* | pw32* | os2* | cegcc*) - # This hack is so that the source file can tell whether it is being - # built for inclusion in a dll (and should export symbols for example). - # Although the cygwin gcc ignores -fPIC, still need this for old-style - # (--disable-auto-import) libraries - m4_if([$1], [GCJ], [], - [_LT_TAGVAR(lt_prog_compiler_pic, $1)='-DDLL_EXPORT']) - case $host_os in - os2*) - _LT_TAGVAR(lt_prog_compiler_static, $1)='$wl-static' - ;; - esac - ;; - - darwin* | rhapsody*) - # PIC is the default on this platform - # Common symbols not allowed in MH_DYLIB files - _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fno-common' - ;; - - haiku*) - # PIC is the default for Haiku. - # The "-static" flag exists, but is broken. - _LT_TAGVAR(lt_prog_compiler_static, $1)= - ;; - - hpux*) - # PIC is the default for 64-bit PA HP-UX, but not for 32-bit - # PA HP-UX. On IA64 HP-UX, PIC is the default but the pic flag - # sets the default TLS model and affects inlining. - case $host_cpu in - hppa*64*) - # +Z the default - ;; - *) - _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC' - ;; - esac - ;; - - interix[[3-9]]*) - # Interix 3.x gcc -fpic/-fPIC options generate broken code. - # Instead, we relocate shared libraries at runtime. - ;; - - msdosdjgpp*) - # Just because we use GCC doesn't mean we suddenly get shared libraries - # on systems that don't support them. - _LT_TAGVAR(lt_prog_compiler_can_build_shared, $1)=no - enable_shared=no - ;; - - *nto* | *qnx*) - # QNX uses GNU C++, but need to define -shared option too, otherwise - # it will coredump. - _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC -shared' - ;; - - sysv4*MP*) - if test -d /usr/nec; then - _LT_TAGVAR(lt_prog_compiler_pic, $1)=-Kconform_pic - fi - ;; - - *) - _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC' - ;; - esac - - case $cc_basename in - nvcc*) # Cuda Compiler Driver 2.2 - _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Xlinker ' - if test -n "$_LT_TAGVAR(lt_prog_compiler_pic, $1)"; then - _LT_TAGVAR(lt_prog_compiler_pic, $1)="-Xcompiler $_LT_TAGVAR(lt_prog_compiler_pic, $1)" - fi - ;; - esac - else - # PORTME Check for flag to pass linker flags through the system compiler. - case $host_os in - aix*) - _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' - if test ia64 = "$host_cpu"; then - # AIX 5 now supports IA64 processor - _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic' - else - _LT_TAGVAR(lt_prog_compiler_static, $1)='-bnso -bI:/lib/syscalls.exp' - fi - ;; - - darwin* | rhapsody*) - # PIC is the default on this platform - # Common symbols not allowed in MH_DYLIB files - _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fno-common' - case $cc_basename in - nagfor*) - # NAG Fortran compiler - _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,-Wl,,' - _LT_TAGVAR(lt_prog_compiler_pic, $1)='-PIC' - _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic' - ;; - esac - ;; - - mingw* | cygwin* | pw32* | os2* | cegcc*) - # This hack is so that the source file can tell whether it is being - # built for inclusion in a dll (and should export symbols for example). - m4_if([$1], [GCJ], [], - [_LT_TAGVAR(lt_prog_compiler_pic, $1)='-DDLL_EXPORT']) - case $host_os in - os2*) - _LT_TAGVAR(lt_prog_compiler_static, $1)='$wl-static' - ;; - esac - ;; - - hpux9* | hpux10* | hpux11*) - _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' - # PIC is the default for IA64 HP-UX and 64-bit HP-UX, but - # not for PA HP-UX. - case $host_cpu in - hppa*64*|ia64*) - # +Z the default - ;; - *) - _LT_TAGVAR(lt_prog_compiler_pic, $1)='+Z' - ;; - esac - # Is there a better lt_prog_compiler_static that works with the bundled CC? - _LT_TAGVAR(lt_prog_compiler_static, $1)='$wl-a ${wl}archive' - ;; - - irix5* | irix6* | nonstopux*) - _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' - # PIC (with -KPIC) is the default. - _LT_TAGVAR(lt_prog_compiler_static, $1)='-non_shared' - ;; - - linux* | k*bsd*-gnu | kopensolaris*-gnu | gnu*) - case $cc_basename in - # old Intel for x86_64, which still supported -KPIC. - ecc*) - _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' - _LT_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC' - _LT_TAGVAR(lt_prog_compiler_static, $1)='-static' - ;; - # icc used to be incompatible with GCC. - # ICC 10 doesn't accept -KPIC any more. - icc* | ifort*) - _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' - _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC' - _LT_TAGVAR(lt_prog_compiler_static, $1)='-static' - ;; - # Lahey Fortran 8.1. - lf95*) - _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' - _LT_TAGVAR(lt_prog_compiler_pic, $1)='--shared' - _LT_TAGVAR(lt_prog_compiler_static, $1)='--static' - ;; - nagfor*) - # NAG Fortran compiler - _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,-Wl,,' - _LT_TAGVAR(lt_prog_compiler_pic, $1)='-PIC' - _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic' - ;; - tcc*) - # Fabrice Bellard et al's Tiny C Compiler - _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' - _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC' - _LT_TAGVAR(lt_prog_compiler_static, $1)='-static' - ;; - pgcc* | pgf77* | pgf90* | pgf95* | pgfortran*) - # Portland Group compilers (*not* the Pentium gcc compiler, - # which looks to be a dead project) - _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' - _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fpic' - _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic' - ;; - ccc*) - _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' - # All Alpha code is PIC. - _LT_TAGVAR(lt_prog_compiler_static, $1)='-non_shared' - ;; - xl* | bgxl* | bgf* | mpixl*) - # IBM XL C 8.0/Fortran 10.1, 11.1 on PPC and BlueGene - _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' - _LT_TAGVAR(lt_prog_compiler_pic, $1)='-qpic' - _LT_TAGVAR(lt_prog_compiler_static, $1)='-qstaticlink' - ;; - *) - case `$CC -V 2>&1 | sed 5q` in - *Sun\ Ceres\ Fortran* | *Sun*Fortran*\ [[1-7]].* | *Sun*Fortran*\ 8.[[0-3]]*) - # Sun Fortran 8.3 passes all unrecognized flags to the linker - _LT_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC' - _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic' - _LT_TAGVAR(lt_prog_compiler_wl, $1)='' - ;; - *Sun\ F* | *Sun*Fortran*) - _LT_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC' - _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic' - _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Qoption ld ' - ;; - *Sun\ C*) - # Sun C 5.9 - _LT_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC' - _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic' - _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' - ;; - *Intel*\ [[CF]]*Compiler*) - _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' - _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC' - _LT_TAGVAR(lt_prog_compiler_static, $1)='-static' - ;; - *Portland\ Group*) - _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' - _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fpic' - _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic' - ;; - esac - ;; - esac - ;; - - newsos6) - _LT_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC' - _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic' - ;; - - *nto* | *qnx*) - # QNX uses GNU C++, but need to define -shared option too, otherwise - # it will coredump. - _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC -shared' - ;; - - osf3* | osf4* | osf5*) - _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' - # All OSF/1 code is PIC. - _LT_TAGVAR(lt_prog_compiler_static, $1)='-non_shared' - ;; - - rdos*) - _LT_TAGVAR(lt_prog_compiler_static, $1)='-non_shared' - ;; - - solaris*) - _LT_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC' - _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic' - case $cc_basename in - f77* | f90* | f95* | sunf77* | sunf90* | sunf95*) - _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Qoption ld ';; - *) - _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,';; - esac - ;; - - sunos4*) - _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Qoption ld ' - _LT_TAGVAR(lt_prog_compiler_pic, $1)='-PIC' - _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic' - ;; - - sysv4 | sysv4.2uw2* | sysv4.3*) - _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' - _LT_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC' - _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic' - ;; - - sysv4*MP*) - if test -d /usr/nec; then - _LT_TAGVAR(lt_prog_compiler_pic, $1)='-Kconform_pic' - _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic' - fi - ;; - - sysv5* | unixware* | sco3.2v5* | sco5v6* | OpenUNIX*) - _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' - _LT_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC' - _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic' - ;; - - unicos*) - _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,' - _LT_TAGVAR(lt_prog_compiler_can_build_shared, $1)=no - ;; - - uts4*) - _LT_TAGVAR(lt_prog_compiler_pic, $1)='-pic' - _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic' - ;; - - *) - _LT_TAGVAR(lt_prog_compiler_can_build_shared, $1)=no - ;; - esac - fi -]) -case $host_os in - # For platforms that do not support PIC, -DPIC is meaningless: - *djgpp*) - _LT_TAGVAR(lt_prog_compiler_pic, $1)= - ;; - *) - _LT_TAGVAR(lt_prog_compiler_pic, $1)="$_LT_TAGVAR(lt_prog_compiler_pic, $1)@&t@m4_if([$1],[],[ -DPIC],[m4_if([$1],[CXX],[ -DPIC],[])])" - ;; -esac - -AC_CACHE_CHECK([for $compiler option to produce PIC], - [_LT_TAGVAR(lt_cv_prog_compiler_pic, $1)], - [_LT_TAGVAR(lt_cv_prog_compiler_pic, $1)=$_LT_TAGVAR(lt_prog_compiler_pic, $1)]) -_LT_TAGVAR(lt_prog_compiler_pic, $1)=$_LT_TAGVAR(lt_cv_prog_compiler_pic, $1) - -# -# Check to make sure the PIC flag actually works. -# -if test -n "$_LT_TAGVAR(lt_prog_compiler_pic, $1)"; then - _LT_COMPILER_OPTION([if $compiler PIC flag $_LT_TAGVAR(lt_prog_compiler_pic, $1) works], - [_LT_TAGVAR(lt_cv_prog_compiler_pic_works, $1)], - [$_LT_TAGVAR(lt_prog_compiler_pic, $1)@&t@m4_if([$1],[],[ -DPIC],[m4_if([$1],[CXX],[ -DPIC],[])])], [], - [case $_LT_TAGVAR(lt_prog_compiler_pic, $1) in - "" | " "*) ;; - *) _LT_TAGVAR(lt_prog_compiler_pic, $1)=" $_LT_TAGVAR(lt_prog_compiler_pic, $1)" ;; - esac], - [_LT_TAGVAR(lt_prog_compiler_pic, $1)= - _LT_TAGVAR(lt_prog_compiler_can_build_shared, $1)=no]) -fi -_LT_TAGDECL([pic_flag], [lt_prog_compiler_pic], [1], - [Additional compiler flags for building library objects]) - -_LT_TAGDECL([wl], [lt_prog_compiler_wl], [1], - [How to pass a linker flag through the compiler]) -# -# Check to make sure the static flag actually works. -# -wl=$_LT_TAGVAR(lt_prog_compiler_wl, $1) eval lt_tmp_static_flag=\"$_LT_TAGVAR(lt_prog_compiler_static, $1)\" -_LT_LINKER_OPTION([if $compiler static flag $lt_tmp_static_flag works], - _LT_TAGVAR(lt_cv_prog_compiler_static_works, $1), - $lt_tmp_static_flag, - [], - [_LT_TAGVAR(lt_prog_compiler_static, $1)=]) -_LT_TAGDECL([link_static_flag], [lt_prog_compiler_static], [1], - [Compiler flag to prevent dynamic linking]) -])# _LT_COMPILER_PIC - - -# _LT_LINKER_SHLIBS([TAGNAME]) -# ---------------------------- -# See if the linker supports building shared libraries. -m4_defun([_LT_LINKER_SHLIBS], -[AC_REQUIRE([LT_PATH_LD])dnl -AC_REQUIRE([LT_PATH_NM])dnl -m4_require([_LT_PATH_MANIFEST_TOOL])dnl -m4_require([_LT_FILEUTILS_DEFAULTS])dnl -m4_require([_LT_DECL_EGREP])dnl -m4_require([_LT_DECL_SED])dnl -m4_require([_LT_CMD_GLOBAL_SYMBOLS])dnl -m4_require([_LT_TAG_COMPILER])dnl -AC_MSG_CHECKING([whether the $compiler linker ($LD) supports shared libraries]) -m4_if([$1], [CXX], [ - _LT_TAGVAR(export_symbols_cmds, $1)='$NM $libobjs $convenience | $global_symbol_pipe | $SED '\''s/.* //'\'' | sort | uniq > $export_symbols' - _LT_TAGVAR(exclude_expsyms, $1)=['_GLOBAL_OFFSET_TABLE_|_GLOBAL__F[ID]_.*'] - case $host_os in - aix[[4-9]]*) - # If we're using GNU nm, then we don't want the "-C" option. - # -C means demangle to GNU nm, but means don't demangle to AIX nm. - # Without the "-l" option, or with the "-B" option, AIX nm treats - # weak defined symbols like other global defined symbols, whereas - # GNU nm marks them as "W". - # While the 'weak' keyword is ignored in the Export File, we need - # it in the Import File for the 'aix-soname' feature, so we have - # to replace the "-B" option with "-P" for AIX nm. - if $NM -V 2>&1 | $GREP 'GNU' > /dev/null; then - _LT_TAGVAR(export_symbols_cmds, $1)='$NM -Bpg $libobjs $convenience | awk '\''{ if (((\$ 2 == "T") || (\$ 2 == "D") || (\$ 2 == "B") || (\$ 2 == "W")) && ([substr](\$ 3,1,1) != ".")) { if (\$ 2 == "W") { print \$ 3 " weak" } else { print \$ 3 } } }'\'' | sort -u > $export_symbols' - else - _LT_TAGVAR(export_symbols_cmds, $1)='`func_echo_all $NM | $SED -e '\''s/B\([[^B]]*\)$/P\1/'\''` -PCpgl $libobjs $convenience | awk '\''{ if (((\$ 2 == "T") || (\$ 2 == "D") || (\$ 2 == "B") || (\$ 2 == "W") || (\$ 2 == "V") || (\$ 2 == "Z")) && ([substr](\$ 1,1,1) != ".")) { if ((\$ 2 == "W") || (\$ 2 == "V") || (\$ 2 == "Z")) { print \$ 1 " weak" } else { print \$ 1 } } }'\'' | sort -u > $export_symbols' - fi - ;; - pw32*) - _LT_TAGVAR(export_symbols_cmds, $1)=$ltdll_cmds - ;; - cygwin* | mingw* | cegcc*) - case $cc_basename in - cl*) - _LT_TAGVAR(exclude_expsyms, $1)='_NULL_IMPORT_DESCRIPTOR|_IMPORT_DESCRIPTOR_.*' - ;; - *) - _LT_TAGVAR(export_symbols_cmds, $1)='$NM $libobjs $convenience | $global_symbol_pipe | $SED -e '\''/^[[BCDGRS]][[ ]]/s/.*[[ ]]\([[^ ]]*\)/\1 DATA/;s/^.*[[ ]]__nm__\([[^ ]]*\)[[ ]][[^ ]]*/\1 DATA/;/^I[[ ]]/d;/^[[AITW]][[ ]]/s/.* //'\'' | sort | uniq > $export_symbols' - _LT_TAGVAR(exclude_expsyms, $1)=['[_]+GLOBAL_OFFSET_TABLE_|[_]+GLOBAL__[FID]_.*|[_]+head_[A-Za-z0-9_]+_dll|[A-Za-z0-9_]+_dll_iname'] - ;; - esac - ;; - *) - _LT_TAGVAR(export_symbols_cmds, $1)='$NM $libobjs $convenience | $global_symbol_pipe | $SED '\''s/.* //'\'' | sort | uniq > $export_symbols' - ;; - esac -], [ - runpath_var= - _LT_TAGVAR(allow_undefined_flag, $1)= - _LT_TAGVAR(always_export_symbols, $1)=no - _LT_TAGVAR(archive_cmds, $1)= - _LT_TAGVAR(archive_expsym_cmds, $1)= - _LT_TAGVAR(compiler_needs_object, $1)=no - _LT_TAGVAR(enable_shared_with_static_runtimes, $1)=no - _LT_TAGVAR(export_dynamic_flag_spec, $1)= - _LT_TAGVAR(export_symbols_cmds, $1)='$NM $libobjs $convenience | $global_symbol_pipe | $SED '\''s/.* //'\'' | sort | uniq > $export_symbols' - _LT_TAGVAR(hardcode_automatic, $1)=no - _LT_TAGVAR(hardcode_direct, $1)=no - _LT_TAGVAR(hardcode_direct_absolute, $1)=no - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)= - _LT_TAGVAR(hardcode_libdir_separator, $1)= - _LT_TAGVAR(hardcode_minus_L, $1)=no - _LT_TAGVAR(hardcode_shlibpath_var, $1)=unsupported - _LT_TAGVAR(inherit_rpath, $1)=no - _LT_TAGVAR(link_all_deplibs, $1)=unknown - _LT_TAGVAR(module_cmds, $1)= - _LT_TAGVAR(module_expsym_cmds, $1)= - _LT_TAGVAR(old_archive_from_new_cmds, $1)= - _LT_TAGVAR(old_archive_from_expsyms_cmds, $1)= - _LT_TAGVAR(thread_safe_flag_spec, $1)= - _LT_TAGVAR(whole_archive_flag_spec, $1)= - # include_expsyms should be a list of space-separated symbols to be *always* - # included in the symbol list - _LT_TAGVAR(include_expsyms, $1)= - # exclude_expsyms can be an extended regexp of symbols to exclude - # it will be wrapped by ' (' and ')$', so one must not match beginning or - # end of line. Example: 'a|bc|.*d.*' will exclude the symbols 'a' and 'bc', - # as well as any symbol that contains 'd'. - _LT_TAGVAR(exclude_expsyms, $1)=['_GLOBAL_OFFSET_TABLE_|_GLOBAL__F[ID]_.*'] - # Although _GLOBAL_OFFSET_TABLE_ is a valid symbol C name, most a.out - # platforms (ab)use it in PIC code, but their linkers get confused if - # the symbol is explicitly referenced. Since portable code cannot - # rely on this symbol name, it's probably fine to never include it in - # preloaded symbol tables. - # Exclude shared library initialization/finalization symbols. -dnl Note also adjust exclude_expsyms for C++ above. - extract_expsyms_cmds= - - case $host_os in - cygwin* | mingw* | pw32* | cegcc*) - # FIXME: the MSVC++ port hasn't been tested in a loooong time - # When not using gcc, we currently assume that we are using - # Microsoft Visual C++. - if test yes != "$GCC"; then - with_gnu_ld=no - fi - ;; - interix*) - # we just hope/assume this is gcc and not c89 (= MSVC++) - with_gnu_ld=yes - ;; - openbsd* | bitrig*) - with_gnu_ld=no - ;; - esac - - _LT_TAGVAR(ld_shlibs, $1)=yes - - # On some targets, GNU ld is compatible enough with the native linker - # that we're better off using the native interface for both. - lt_use_gnu_ld_interface=no - if test yes = "$with_gnu_ld"; then - case $host_os in - aix*) - # The AIX port of GNU ld has always aspired to compatibility - # with the native linker. However, as the warning in the GNU ld - # block says, versions before 2.19.5* couldn't really create working - # shared libraries, regardless of the interface used. - case `$LD -v 2>&1` in - *\ \(GNU\ Binutils\)\ 2.19.5*) ;; - *\ \(GNU\ Binutils\)\ 2.[[2-9]]*) ;; - *\ \(GNU\ Binutils\)\ [[3-9]]*) ;; - *) - lt_use_gnu_ld_interface=yes - ;; - esac - ;; - *) - lt_use_gnu_ld_interface=yes - ;; - esac - fi - - if test yes = "$lt_use_gnu_ld_interface"; then - # If archive_cmds runs LD, not CC, wlarc should be empty - wlarc='$wl' - - # Set some defaults for GNU ld with shared library support. These - # are reset later if shared libraries are not supported. Putting them - # here allows them to be overridden if necessary. - runpath_var=LD_RUN_PATH - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath $wl$libdir' - _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl--export-dynamic' - # ancient GNU ld didn't support --whole-archive et. al. - if $LD --help 2>&1 | $GREP 'no-whole-archive' > /dev/null; then - _LT_TAGVAR(whole_archive_flag_spec, $1)=$wlarc'--whole-archive$convenience '$wlarc'--no-whole-archive' - else - _LT_TAGVAR(whole_archive_flag_spec, $1)= - fi - supports_anon_versioning=no - case `$LD -v | $SED -e 's/([^)]\+)\s\+//' 2>&1` in - *GNU\ gold*) supports_anon_versioning=yes ;; - *\ [[01]].* | *\ 2.[[0-9]].* | *\ 2.10.*) ;; # catch versions < 2.11 - *\ 2.11.93.0.2\ *) supports_anon_versioning=yes ;; # RH7.3 ... - *\ 2.11.92.0.12\ *) supports_anon_versioning=yes ;; # Mandrake 8.2 ... - *\ 2.11.*) ;; # other 2.11 versions - *) supports_anon_versioning=yes ;; - esac - - # See if GNU ld supports shared libraries. - case $host_os in - aix[[3-9]]*) - # On AIX/PPC, the GNU linker is very broken - if test ia64 != "$host_cpu"; then - _LT_TAGVAR(ld_shlibs, $1)=no - cat <<_LT_EOF 1>&2 - -*** Warning: the GNU linker, at least up to release 2.19, is reported -*** to be unable to reliably create shared libraries on AIX. -*** Therefore, libtool is disabling shared libraries support. If you -*** really care for shared libraries, you may want to install binutils -*** 2.20 or above, or modify your PATH so that a non-GNU linker is found. -*** You will then need to restart the configuration process. - -_LT_EOF - fi - ;; - - amigaos*) - case $host_cpu in - powerpc) - # see comment about AmigaOS4 .so support - _LT_TAGVAR(archive_cmds, $1)='$CC -shared $libobjs $deplibs $compiler_flags $wl-soname $wl$soname -o $lib' - _LT_TAGVAR(archive_expsym_cmds, $1)='' - ;; - m68k) - _LT_TAGVAR(archive_cmds, $1)='$RM $output_objdir/a2ixlibrary.data~$ECHO "#define NAME $libname" > $output_objdir/a2ixlibrary.data~$ECHO "#define LIBRARY_ID 1" >> $output_objdir/a2ixlibrary.data~$ECHO "#define VERSION $major" >> $output_objdir/a2ixlibrary.data~$ECHO "#define REVISION $revision" >> $output_objdir/a2ixlibrary.data~$AR $AR_FLAGS $lib $libobjs~$RANLIB $lib~(cd $output_objdir && a2ixlibrary -32)' - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-L$libdir' - _LT_TAGVAR(hardcode_minus_L, $1)=yes - ;; - esac - ;; - - beos*) - if $LD --help 2>&1 | $GREP ': supported targets:.* elf' > /dev/null; then - _LT_TAGVAR(allow_undefined_flag, $1)=unsupported - # Joseph Beckenbach says some releases of gcc - # support --undefined. This deserves some investigation. FIXME - _LT_TAGVAR(archive_cmds, $1)='$CC -nostart $libobjs $deplibs $compiler_flags $wl-soname $wl$soname -o $lib' - else - _LT_TAGVAR(ld_shlibs, $1)=no - fi - ;; - - cygwin* | mingw* | pw32* | cegcc*) - # _LT_TAGVAR(hardcode_libdir_flag_spec, $1) is actually meaningless, - # as there is no search path for DLLs. - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-L$libdir' - _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl--export-all-symbols' - _LT_TAGVAR(allow_undefined_flag, $1)=unsupported - _LT_TAGVAR(always_export_symbols, $1)=no - _LT_TAGVAR(enable_shared_with_static_runtimes, $1)=yes - _LT_TAGVAR(export_symbols_cmds, $1)='$NM $libobjs $convenience | $global_symbol_pipe | $SED -e '\''/^[[BCDGRS]][[ ]]/s/.*[[ ]]\([[^ ]]*\)/\1 DATA/;s/^.*[[ ]]__nm__\([[^ ]]*\)[[ ]][[^ ]]*/\1 DATA/;/^I[[ ]]/d;/^[[AITW]][[ ]]/s/.* //'\'' | sort | uniq > $export_symbols' - _LT_TAGVAR(exclude_expsyms, $1)=['[_]+GLOBAL_OFFSET_TABLE_|[_]+GLOBAL__[FID]_.*|[_]+head_[A-Za-z0-9_]+_dll|[A-Za-z0-9_]+_dll_iname'] - - if $LD --help 2>&1 | $GREP 'auto-import' > /dev/null; then - _LT_TAGVAR(archive_cmds, $1)='$CC -shared $libobjs $deplibs $compiler_flags -o $output_objdir/$soname $wl--enable-auto-image-base -Xlinker --out-implib -Xlinker $lib' - # If the export-symbols file already is a .def file, use it as - # is; otherwise, prepend EXPORTS... - _LT_TAGVAR(archive_expsym_cmds, $1)='if _LT_DLL_DEF_P([$export_symbols]); then - cp $export_symbols $output_objdir/$soname.def; - else - echo EXPORTS > $output_objdir/$soname.def; - cat $export_symbols >> $output_objdir/$soname.def; - fi~ - $CC -shared $output_objdir/$soname.def $libobjs $deplibs $compiler_flags -o $output_objdir/$soname $wl--enable-auto-image-base -Xlinker --out-implib -Xlinker $lib' - else - _LT_TAGVAR(ld_shlibs, $1)=no - fi - ;; - - haiku*) - _LT_TAGVAR(archive_cmds, $1)='$CC -shared $libobjs $deplibs $compiler_flags $wl-soname $wl$soname -o $lib' - _LT_TAGVAR(link_all_deplibs, $1)=yes - ;; - - os2*) - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-L$libdir' - _LT_TAGVAR(hardcode_minus_L, $1)=yes - _LT_TAGVAR(allow_undefined_flag, $1)=unsupported - shrext_cmds=.dll - _LT_TAGVAR(archive_cmds, $1)='$ECHO "LIBRARY ${soname%$shared_ext} INITINSTANCE TERMINSTANCE" > $output_objdir/$libname.def~ - $ECHO "DESCRIPTION \"$libname\"" >> $output_objdir/$libname.def~ - $ECHO "DATA MULTIPLE NONSHARED" >> $output_objdir/$libname.def~ - $ECHO EXPORTS >> $output_objdir/$libname.def~ - emxexp $libobjs | $SED /"_DLL_InitTerm"/d >> $output_objdir/$libname.def~ - $CC -Zdll -Zcrtdll -o $output_objdir/$soname $libobjs $deplibs $compiler_flags $output_objdir/$libname.def~ - emximp -o $lib $output_objdir/$libname.def' - _LT_TAGVAR(archive_expsym_cmds, $1)='$ECHO "LIBRARY ${soname%$shared_ext} INITINSTANCE TERMINSTANCE" > $output_objdir/$libname.def~ - $ECHO "DESCRIPTION \"$libname\"" >> $output_objdir/$libname.def~ - $ECHO "DATA MULTIPLE NONSHARED" >> $output_objdir/$libname.def~ - $ECHO EXPORTS >> $output_objdir/$libname.def~ - prefix_cmds="$SED"~ - if test EXPORTS = "`$SED 1q $export_symbols`"; then - prefix_cmds="$prefix_cmds -e 1d"; - fi~ - prefix_cmds="$prefix_cmds -e \"s/^\(.*\)$/_\1/g\""~ - cat $export_symbols | $prefix_cmds >> $output_objdir/$libname.def~ - $CC -Zdll -Zcrtdll -o $output_objdir/$soname $libobjs $deplibs $compiler_flags $output_objdir/$libname.def~ - emximp -o $lib $output_objdir/$libname.def' - _LT_TAGVAR(old_archive_From_new_cmds, $1)='emximp -o $output_objdir/${libname}_dll.a $output_objdir/$libname.def' - _LT_TAGVAR(enable_shared_with_static_runtimes, $1)=yes - ;; - - interix[[3-9]]*) - _LT_TAGVAR(hardcode_direct, $1)=no - _LT_TAGVAR(hardcode_shlibpath_var, $1)=no - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath,$libdir' - _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl-E' - # Hack: On Interix 3.x, we cannot compile PIC because of a broken gcc. - # Instead, shared libraries are loaded at an image base (0x10000000 by - # default) and relocated if they conflict, which is a slow very memory - # consuming and fragmenting process. To avoid this, we pick a random, - # 256 KiB-aligned image base between 0x50000000 and 0x6FFC0000 at link - # time. Moving up from 0x10000000 also allows more sbrk(2) space. - _LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag $libobjs $deplibs $compiler_flags $wl-h,$soname $wl--image-base,`expr ${RANDOM-$$} % 4096 / 2 \* 262144 + 1342177280` -o $lib' - _LT_TAGVAR(archive_expsym_cmds, $1)='sed "s|^|_|" $export_symbols >$output_objdir/$soname.expsym~$CC -shared $pic_flag $libobjs $deplibs $compiler_flags $wl-h,$soname $wl--retain-symbols-file,$output_objdir/$soname.expsym $wl--image-base,`expr ${RANDOM-$$} % 4096 / 2 \* 262144 + 1342177280` -o $lib' - ;; - - gnu* | linux* | tpf* | k*bsd*-gnu | kopensolaris*-gnu) - tmp_diet=no - if test linux-dietlibc = "$host_os"; then - case $cc_basename in - diet\ *) tmp_diet=yes;; # linux-dietlibc with static linking (!diet-dyn) - esac - fi - if $LD --help 2>&1 | $EGREP ': supported targets:.* elf' > /dev/null \ - && test no = "$tmp_diet" - then - tmp_addflag=' $pic_flag' - tmp_sharedflag='-shared' - case $cc_basename,$host_cpu in - pgcc*) # Portland Group C compiler - _LT_TAGVAR(whole_archive_flag_spec, $1)='$wl--whole-archive`for conv in $convenience\"\"; do test -n \"$conv\" && new_convenience=\"$new_convenience,$conv\"; done; func_echo_all \"$new_convenience\"` $wl--no-whole-archive' - tmp_addflag=' $pic_flag' - ;; - pgf77* | pgf90* | pgf95* | pgfortran*) - # Portland Group f77 and f90 compilers - _LT_TAGVAR(whole_archive_flag_spec, $1)='$wl--whole-archive`for conv in $convenience\"\"; do test -n \"$conv\" && new_convenience=\"$new_convenience,$conv\"; done; func_echo_all \"$new_convenience\"` $wl--no-whole-archive' - tmp_addflag=' $pic_flag -Mnomain' ;; - ecc*,ia64* | icc*,ia64*) # Intel C compiler on ia64 - tmp_addflag=' -i_dynamic' ;; - efc*,ia64* | ifort*,ia64*) # Intel Fortran compiler on ia64 - tmp_addflag=' -i_dynamic -nofor_main' ;; - ifc* | ifort*) # Intel Fortran compiler - tmp_addflag=' -nofor_main' ;; - lf95*) # Lahey Fortran 8.1 - _LT_TAGVAR(whole_archive_flag_spec, $1)= - tmp_sharedflag='--shared' ;; - nagfor*) # NAGFOR 5.3 - tmp_sharedflag='-Wl,-shared' ;; - xl[[cC]]* | bgxl[[cC]]* | mpixl[[cC]]*) # IBM XL C 8.0 on PPC (deal with xlf below) - tmp_sharedflag='-qmkshrobj' - tmp_addflag= ;; - nvcc*) # Cuda Compiler Driver 2.2 - _LT_TAGVAR(whole_archive_flag_spec, $1)='$wl--whole-archive`for conv in $convenience\"\"; do test -n \"$conv\" && new_convenience=\"$new_convenience,$conv\"; done; func_echo_all \"$new_convenience\"` $wl--no-whole-archive' - _LT_TAGVAR(compiler_needs_object, $1)=yes - ;; - esac - case `$CC -V 2>&1 | sed 5q` in - *Sun\ C*) # Sun C 5.9 - _LT_TAGVAR(whole_archive_flag_spec, $1)='$wl--whole-archive`new_convenience=; for conv in $convenience\"\"; do test -z \"$conv\" || new_convenience=\"$new_convenience,$conv\"; done; func_echo_all \"$new_convenience\"` $wl--no-whole-archive' - _LT_TAGVAR(compiler_needs_object, $1)=yes - tmp_sharedflag='-G' ;; - *Sun\ F*) # Sun Fortran 8.3 - tmp_sharedflag='-G' ;; - esac - _LT_TAGVAR(archive_cmds, $1)='$CC '"$tmp_sharedflag""$tmp_addflag"' $libobjs $deplibs $compiler_flags $wl-soname $wl$soname -o $lib' - - if test yes = "$supports_anon_versioning"; then - _LT_TAGVAR(archive_expsym_cmds, $1)='echo "{ global:" > $output_objdir/$libname.ver~ - cat $export_symbols | sed -e "s/\(.*\)/\1;/" >> $output_objdir/$libname.ver~ - echo "local: *; };" >> $output_objdir/$libname.ver~ - $CC '"$tmp_sharedflag""$tmp_addflag"' $libobjs $deplibs $compiler_flags $wl-soname $wl$soname $wl-version-script $wl$output_objdir/$libname.ver -o $lib' - fi - - case $cc_basename in - tcc*) - _LT_TAGVAR(export_dynamic_flag_spec, $1)='-rdynamic' - ;; - xlf* | bgf* | bgxlf* | mpixlf*) - # IBM XL Fortran 10.1 on PPC cannot create shared libs itself - _LT_TAGVAR(whole_archive_flag_spec, $1)='--whole-archive$convenience --no-whole-archive' - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath $wl$libdir' - _LT_TAGVAR(archive_cmds, $1)='$LD -shared $libobjs $deplibs $linker_flags -soname $soname -o $lib' - if test yes = "$supports_anon_versioning"; then - _LT_TAGVAR(archive_expsym_cmds, $1)='echo "{ global:" > $output_objdir/$libname.ver~ - cat $export_symbols | sed -e "s/\(.*\)/\1;/" >> $output_objdir/$libname.ver~ - echo "local: *; };" >> $output_objdir/$libname.ver~ - $LD -shared $libobjs $deplibs $linker_flags -soname $soname -version-script $output_objdir/$libname.ver -o $lib' - fi - ;; - esac - else - _LT_TAGVAR(ld_shlibs, $1)=no - fi - ;; - - netbsd*) - if echo __ELF__ | $CC -E - | $GREP __ELF__ >/dev/null; then - _LT_TAGVAR(archive_cmds, $1)='$LD -Bshareable $libobjs $deplibs $linker_flags -o $lib' - wlarc= - else - _LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag $libobjs $deplibs $compiler_flags $wl-soname $wl$soname -o $lib' - _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $pic_flag $libobjs $deplibs $compiler_flags $wl-soname $wl$soname $wl-retain-symbols-file $wl$export_symbols -o $lib' - fi - ;; - - solaris*) - if $LD -v 2>&1 | $GREP 'BFD 2\.8' > /dev/null; then - _LT_TAGVAR(ld_shlibs, $1)=no - cat <<_LT_EOF 1>&2 - -*** Warning: The releases 2.8.* of the GNU linker cannot reliably -*** create shared libraries on Solaris systems. Therefore, libtool -*** is disabling shared libraries support. We urge you to upgrade GNU -*** binutils to release 2.9.1 or newer. Another option is to modify -*** your PATH or compiler configuration so that the native linker is -*** used, and then restart. - -_LT_EOF - elif $LD --help 2>&1 | $GREP ': supported targets:.* elf' > /dev/null; then - _LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag $libobjs $deplibs $compiler_flags $wl-soname $wl$soname -o $lib' - _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $pic_flag $libobjs $deplibs $compiler_flags $wl-soname $wl$soname $wl-retain-symbols-file $wl$export_symbols -o $lib' - else - _LT_TAGVAR(ld_shlibs, $1)=no - fi - ;; - - sysv5* | sco3.2v5* | sco5v6* | unixware* | OpenUNIX*) - case `$LD -v 2>&1` in - *\ [[01]].* | *\ 2.[[0-9]].* | *\ 2.1[[0-5]].*) - _LT_TAGVAR(ld_shlibs, $1)=no - cat <<_LT_EOF 1>&2 - -*** Warning: Releases of the GNU linker prior to 2.16.91.0.3 cannot -*** reliably create shared libraries on SCO systems. Therefore, libtool -*** is disabling shared libraries support. We urge you to upgrade GNU -*** binutils to release 2.16.91.0.3 or newer. Another option is to modify -*** your PATH or compiler configuration so that the native linker is -*** used, and then restart. - -_LT_EOF - ;; - *) - # For security reasons, it is highly recommended that you always - # use absolute paths for naming shared libraries, and exclude the - # DT_RUNPATH tag from executables and libraries. But doing so - # requires that you compile everything twice, which is a pain. - if $LD --help 2>&1 | $GREP ': supported targets:.* elf' > /dev/null; then - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath $wl$libdir' - _LT_TAGVAR(archive_cmds, $1)='$CC -shared $libobjs $deplibs $compiler_flags $wl-soname $wl$soname -o $lib' - _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $libobjs $deplibs $compiler_flags $wl-soname $wl$soname $wl-retain-symbols-file $wl$export_symbols -o $lib' - else - _LT_TAGVAR(ld_shlibs, $1)=no - fi - ;; - esac - ;; - - sunos4*) - _LT_TAGVAR(archive_cmds, $1)='$LD -assert pure-text -Bshareable -o $lib $libobjs $deplibs $linker_flags' - wlarc= - _LT_TAGVAR(hardcode_direct, $1)=yes - _LT_TAGVAR(hardcode_shlibpath_var, $1)=no - ;; - - *) - if $LD --help 2>&1 | $GREP ': supported targets:.* elf' > /dev/null; then - _LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag $libobjs $deplibs $compiler_flags $wl-soname $wl$soname -o $lib' - _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $pic_flag $libobjs $deplibs $compiler_flags $wl-soname $wl$soname $wl-retain-symbols-file $wl$export_symbols -o $lib' - else - _LT_TAGVAR(ld_shlibs, $1)=no - fi - ;; - esac - - if test no = "$_LT_TAGVAR(ld_shlibs, $1)"; then - runpath_var= - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)= - _LT_TAGVAR(export_dynamic_flag_spec, $1)= - _LT_TAGVAR(whole_archive_flag_spec, $1)= - fi - else - # PORTME fill in a description of your system's linker (not GNU ld) - case $host_os in - aix3*) - _LT_TAGVAR(allow_undefined_flag, $1)=unsupported - _LT_TAGVAR(always_export_symbols, $1)=yes - _LT_TAGVAR(archive_expsym_cmds, $1)='$LD -o $output_objdir/$soname $libobjs $deplibs $linker_flags -bE:$export_symbols -T512 -H512 -bM:SRE~$AR $AR_FLAGS $lib $output_objdir/$soname' - # Note: this linker hardcodes the directories in LIBPATH if there - # are no directories specified by -L. - _LT_TAGVAR(hardcode_minus_L, $1)=yes - if test yes = "$GCC" && test -z "$lt_prog_compiler_static"; then - # Neither direct hardcoding nor static linking is supported with a - # broken collect2. - _LT_TAGVAR(hardcode_direct, $1)=unsupported - fi - ;; - - aix[[4-9]]*) - if test ia64 = "$host_cpu"; then - # On IA64, the linker does run time linking by default, so we don't - # have to do anything special. - aix_use_runtimelinking=no - exp_sym_flag='-Bexport' - no_entry_flag= - else - # If we're using GNU nm, then we don't want the "-C" option. - # -C means demangle to GNU nm, but means don't demangle to AIX nm. - # Without the "-l" option, or with the "-B" option, AIX nm treats - # weak defined symbols like other global defined symbols, whereas - # GNU nm marks them as "W". - # While the 'weak' keyword is ignored in the Export File, we need - # it in the Import File for the 'aix-soname' feature, so we have - # to replace the "-B" option with "-P" for AIX nm. - if $NM -V 2>&1 | $GREP 'GNU' > /dev/null; then - _LT_TAGVAR(export_symbols_cmds, $1)='$NM -Bpg $libobjs $convenience | awk '\''{ if (((\$ 2 == "T") || (\$ 2 == "D") || (\$ 2 == "B") || (\$ 2 == "W")) && ([substr](\$ 3,1,1) != ".")) { if (\$ 2 == "W") { print \$ 3 " weak" } else { print \$ 3 } } }'\'' | sort -u > $export_symbols' - else - _LT_TAGVAR(export_symbols_cmds, $1)='`func_echo_all $NM | $SED -e '\''s/B\([[^B]]*\)$/P\1/'\''` -PCpgl $libobjs $convenience | awk '\''{ if (((\$ 2 == "T") || (\$ 2 == "D") || (\$ 2 == "B") || (\$ 2 == "W") || (\$ 2 == "V") || (\$ 2 == "Z")) && ([substr](\$ 1,1,1) != ".")) { if ((\$ 2 == "W") || (\$ 2 == "V") || (\$ 2 == "Z")) { print \$ 1 " weak" } else { print \$ 1 } } }'\'' | sort -u > $export_symbols' - fi - aix_use_runtimelinking=no - - # Test if we are trying to use run time linking or normal - # AIX style linking. If -brtl is somewhere in LDFLAGS, we - # have runtime linking enabled, and use it for executables. - # For shared libraries, we enable/disable runtime linking - # depending on the kind of the shared library created - - # when "with_aix_soname,aix_use_runtimelinking" is: - # "aix,no" lib.a(lib.so.V) shared, rtl:no, for executables - # "aix,yes" lib.so shared, rtl:yes, for executables - # lib.a static archive - # "both,no" lib.so.V(shr.o) shared, rtl:yes - # lib.a(lib.so.V) shared, rtl:no, for executables - # "both,yes" lib.so.V(shr.o) shared, rtl:yes, for executables - # lib.a(lib.so.V) shared, rtl:no - # "svr4,*" lib.so.V(shr.o) shared, rtl:yes, for executables - # lib.a static archive - case $host_os in aix4.[[23]]|aix4.[[23]].*|aix[[5-9]]*) - for ld_flag in $LDFLAGS; do - if (test x-brtl = "x$ld_flag" || test x-Wl,-brtl = "x$ld_flag"); then - aix_use_runtimelinking=yes - break - fi - done - if test svr4,no = "$with_aix_soname,$aix_use_runtimelinking"; then - # With aix-soname=svr4, we create the lib.so.V shared archives only, - # so we don't have lib.a shared libs to link our executables. - # We have to force runtime linking in this case. - aix_use_runtimelinking=yes - LDFLAGS="$LDFLAGS -Wl,-brtl" - fi - ;; - esac - - exp_sym_flag='-bexport' - no_entry_flag='-bnoentry' - fi - - # When large executables or shared objects are built, AIX ld can - # have problems creating the table of contents. If linking a library - # or program results in "error TOC overflow" add -mminimal-toc to - # CXXFLAGS/CFLAGS for g++/gcc. In the cases where that is not - # enough to fix the problem, add -Wl,-bbigtoc to LDFLAGS. - - _LT_TAGVAR(archive_cmds, $1)='' - _LT_TAGVAR(hardcode_direct, $1)=yes - _LT_TAGVAR(hardcode_direct_absolute, $1)=yes - _LT_TAGVAR(hardcode_libdir_separator, $1)=':' - _LT_TAGVAR(link_all_deplibs, $1)=yes - _LT_TAGVAR(file_list_spec, $1)='$wl-f,' - case $with_aix_soname,$aix_use_runtimelinking in - aix,*) ;; # traditional, no import file - svr4,* | *,yes) # use import file - # The Import File defines what to hardcode. - _LT_TAGVAR(hardcode_direct, $1)=no - _LT_TAGVAR(hardcode_direct_absolute, $1)=no - ;; - esac - - if test yes = "$GCC"; then - case $host_os in aix4.[[012]]|aix4.[[012]].*) - # We only want to do this on AIX 4.2 and lower, the check - # below for broken collect2 doesn't work under 4.3+ - collect2name=`$CC -print-prog-name=collect2` - if test -f "$collect2name" && - strings "$collect2name" | $GREP resolve_lib_name >/dev/null - then - # We have reworked collect2 - : - else - # We have old collect2 - _LT_TAGVAR(hardcode_direct, $1)=unsupported - # It fails to find uninstalled libraries when the uninstalled - # path is not listed in the libpath. Setting hardcode_minus_L - # to unsupported forces relinking - _LT_TAGVAR(hardcode_minus_L, $1)=yes - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-L$libdir' - _LT_TAGVAR(hardcode_libdir_separator, $1)= - fi - ;; - esac - shared_flag='-shared' - if test yes = "$aix_use_runtimelinking"; then - shared_flag="$shared_flag "'$wl-G' - fi - # Need to ensure runtime linking is disabled for the traditional - # shared library, or the linker may eventually find shared libraries - # /with/ Import File - we do not want to mix them. - shared_flag_aix='-shared' - shared_flag_svr4='-shared $wl-G' - else - # not using gcc - if test ia64 = "$host_cpu"; then - # VisualAge C++, Version 5.5 for AIX 5L for IA-64, Beta 3 Release - # chokes on -Wl,-G. The following line is correct: - shared_flag='-G' - else - if test yes = "$aix_use_runtimelinking"; then - shared_flag='$wl-G' - else - shared_flag='$wl-bM:SRE' - fi - shared_flag_aix='$wl-bM:SRE' - shared_flag_svr4='$wl-G' - fi - fi - - _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl-bexpall' - # It seems that -bexpall does not export symbols beginning with - # underscore (_), so it is better to generate a list of symbols to export. - _LT_TAGVAR(always_export_symbols, $1)=yes - if test aix,yes = "$with_aix_soname,$aix_use_runtimelinking"; then - # Warning - without using the other runtime loading flags (-brtl), - # -berok will link without error, but may produce a broken library. - _LT_TAGVAR(allow_undefined_flag, $1)='-berok' - # Determine the default libpath from the value encoded in an - # empty executable. - _LT_SYS_MODULE_PATH_AIX([$1]) - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-blibpath:$libdir:'"$aix_libpath" - _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -o $output_objdir/$soname $libobjs $deplibs $wl'$no_entry_flag' $compiler_flags `if test -n "$allow_undefined_flag"; then func_echo_all "$wl$allow_undefined_flag"; else :; fi` $wl'$exp_sym_flag:\$export_symbols' '$shared_flag - else - if test ia64 = "$host_cpu"; then - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-R $libdir:/usr/lib:/lib' - _LT_TAGVAR(allow_undefined_flag, $1)="-z nodefs" - _LT_TAGVAR(archive_expsym_cmds, $1)="\$CC $shared_flag"' -o $output_objdir/$soname $libobjs $deplibs '"\$wl$no_entry_flag"' $compiler_flags $wl$allow_undefined_flag '"\$wl$exp_sym_flag:\$export_symbols" - else - # Determine the default libpath from the value encoded in an - # empty executable. - _LT_SYS_MODULE_PATH_AIX([$1]) - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-blibpath:$libdir:'"$aix_libpath" - # Warning - without using the other run time loading flags, - # -berok will link without error, but may produce a broken library. - _LT_TAGVAR(no_undefined_flag, $1)=' $wl-bernotok' - _LT_TAGVAR(allow_undefined_flag, $1)=' $wl-berok' - if test yes = "$with_gnu_ld"; then - # We only use this code for GNU lds that support --whole-archive. - _LT_TAGVAR(whole_archive_flag_spec, $1)='$wl--whole-archive$convenience $wl--no-whole-archive' - else - # Exported symbols can be pulled into shared objects from archives - _LT_TAGVAR(whole_archive_flag_spec, $1)='$convenience' - fi - _LT_TAGVAR(archive_cmds_need_lc, $1)=yes - _LT_TAGVAR(archive_expsym_cmds, $1)='$RM -r $output_objdir/$realname.d~$MKDIR $output_objdir/$realname.d' - # -brtl affects multiple linker settings, -berok does not and is overridden later - compiler_flags_filtered='`func_echo_all "$compiler_flags " | $SED -e "s%-brtl\\([[, ]]\\)%-berok\\1%g"`' - if test svr4 != "$with_aix_soname"; then - # This is similar to how AIX traditionally builds its shared libraries. - _LT_TAGVAR(archive_expsym_cmds, $1)="$_LT_TAGVAR(archive_expsym_cmds, $1)"'~$CC '$shared_flag_aix' -o $output_objdir/$realname.d/$soname $libobjs $deplibs $wl-bnoentry '$compiler_flags_filtered'$wl-bE:$export_symbols$allow_undefined_flag~$AR $AR_FLAGS $output_objdir/$libname$release.a $output_objdir/$realname.d/$soname' - fi - if test aix != "$with_aix_soname"; then - _LT_TAGVAR(archive_expsym_cmds, $1)="$_LT_TAGVAR(archive_expsym_cmds, $1)"'~$CC '$shared_flag_svr4' -o $output_objdir/$realname.d/$shared_archive_member_spec.o $libobjs $deplibs $wl-bnoentry '$compiler_flags_filtered'$wl-bE:$export_symbols$allow_undefined_flag~$STRIP -e $output_objdir/$realname.d/$shared_archive_member_spec.o~( func_echo_all "#! $soname($shared_archive_member_spec.o)"; if test shr_64 = "$shared_archive_member_spec"; then func_echo_all "# 64"; else func_echo_all "# 32"; fi; cat $export_symbols ) > $output_objdir/$realname.d/$shared_archive_member_spec.imp~$AR $AR_FLAGS $output_objdir/$soname $output_objdir/$realname.d/$shared_archive_member_spec.o $output_objdir/$realname.d/$shared_archive_member_spec.imp' - else - # used by -dlpreopen to get the symbols - _LT_TAGVAR(archive_expsym_cmds, $1)="$_LT_TAGVAR(archive_expsym_cmds, $1)"'~$MV $output_objdir/$realname.d/$soname $output_objdir' - fi - _LT_TAGVAR(archive_expsym_cmds, $1)="$_LT_TAGVAR(archive_expsym_cmds, $1)"'~$RM -r $output_objdir/$realname.d' - fi - fi - ;; - - amigaos*) - case $host_cpu in - powerpc) - # see comment about AmigaOS4 .so support - _LT_TAGVAR(archive_cmds, $1)='$CC -shared $libobjs $deplibs $compiler_flags $wl-soname $wl$soname -o $lib' - _LT_TAGVAR(archive_expsym_cmds, $1)='' - ;; - m68k) - _LT_TAGVAR(archive_cmds, $1)='$RM $output_objdir/a2ixlibrary.data~$ECHO "#define NAME $libname" > $output_objdir/a2ixlibrary.data~$ECHO "#define LIBRARY_ID 1" >> $output_objdir/a2ixlibrary.data~$ECHO "#define VERSION $major" >> $output_objdir/a2ixlibrary.data~$ECHO "#define REVISION $revision" >> $output_objdir/a2ixlibrary.data~$AR $AR_FLAGS $lib $libobjs~$RANLIB $lib~(cd $output_objdir && a2ixlibrary -32)' - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-L$libdir' - _LT_TAGVAR(hardcode_minus_L, $1)=yes - ;; - esac - ;; - - bsdi[[45]]*) - _LT_TAGVAR(export_dynamic_flag_spec, $1)=-rdynamic - ;; - - cygwin* | mingw* | pw32* | cegcc*) - # When not using gcc, we currently assume that we are using - # Microsoft Visual C++. - # hardcode_libdir_flag_spec is actually meaningless, as there is - # no search path for DLLs. - case $cc_basename in - cl*) - # Native MSVC - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)=' ' - _LT_TAGVAR(allow_undefined_flag, $1)=unsupported - _LT_TAGVAR(always_export_symbols, $1)=yes - _LT_TAGVAR(file_list_spec, $1)='@' - # Tell ltmain to make .lib files, not .a files. - libext=lib - # Tell ltmain to make .dll files, not .so files. - shrext_cmds=.dll - # FIXME: Setting linknames here is a bad hack. - _LT_TAGVAR(archive_cmds, $1)='$CC -o $output_objdir/$soname $libobjs $compiler_flags $deplibs -Wl,-DLL,-IMPLIB:"$tool_output_objdir$libname.dll.lib"~linknames=' - _LT_TAGVAR(archive_expsym_cmds, $1)='if _LT_DLL_DEF_P([$export_symbols]); then - cp "$export_symbols" "$output_objdir/$soname.def"; - echo "$tool_output_objdir$soname.def" > "$output_objdir/$soname.exp"; - else - $SED -e '\''s/^/-link -EXPORT:/'\'' < $export_symbols > $output_objdir/$soname.exp; - fi~ - $CC -o $tool_output_objdir$soname $libobjs $compiler_flags $deplibs "@$tool_output_objdir$soname.exp" -Wl,-DLL,-IMPLIB:"$tool_output_objdir$libname.dll.lib"~ - linknames=' - # The linker will not automatically build a static lib if we build a DLL. - # _LT_TAGVAR(old_archive_from_new_cmds, $1)='true' - _LT_TAGVAR(enable_shared_with_static_runtimes, $1)=yes - _LT_TAGVAR(exclude_expsyms, $1)='_NULL_IMPORT_DESCRIPTOR|_IMPORT_DESCRIPTOR_.*' - _LT_TAGVAR(export_symbols_cmds, $1)='$NM $libobjs $convenience | $global_symbol_pipe | $SED -e '\''/^[[BCDGRS]][[ ]]/s/.*[[ ]]\([[^ ]]*\)/\1,DATA/'\'' | $SED -e '\''/^[[AITW]][[ ]]/s/.*[[ ]]//'\'' | sort | uniq > $export_symbols' - # Don't use ranlib - _LT_TAGVAR(old_postinstall_cmds, $1)='chmod 644 $oldlib' - _LT_TAGVAR(postlink_cmds, $1)='lt_outputfile="@OUTPUT@"~ - lt_tool_outputfile="@TOOL_OUTPUT@"~ - case $lt_outputfile in - *.exe|*.EXE) ;; - *) - lt_outputfile=$lt_outputfile.exe - lt_tool_outputfile=$lt_tool_outputfile.exe - ;; - esac~ - if test : != "$MANIFEST_TOOL" && test -f "$lt_outputfile.manifest"; then - $MANIFEST_TOOL -manifest "$lt_tool_outputfile.manifest" -outputresource:"$lt_tool_outputfile" || exit 1; - $RM "$lt_outputfile.manifest"; - fi' - ;; - *) - # Assume MSVC wrapper - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)=' ' - _LT_TAGVAR(allow_undefined_flag, $1)=unsupported - # Tell ltmain to make .lib files, not .a files. - libext=lib - # Tell ltmain to make .dll files, not .so files. - shrext_cmds=.dll - # FIXME: Setting linknames here is a bad hack. - _LT_TAGVAR(archive_cmds, $1)='$CC -o $lib $libobjs $compiler_flags `func_echo_all "$deplibs" | $SED '\''s/ -lc$//'\''` -link -dll~linknames=' - # The linker will automatically build a .lib file if we build a DLL. - _LT_TAGVAR(old_archive_from_new_cmds, $1)='true' - # FIXME: Should let the user specify the lib program. - _LT_TAGVAR(old_archive_cmds, $1)='lib -OUT:$oldlib$oldobjs$old_deplibs' - _LT_TAGVAR(enable_shared_with_static_runtimes, $1)=yes - ;; - esac - ;; - - darwin* | rhapsody*) - _LT_DARWIN_LINKER_FEATURES($1) - ;; - - dgux*) - _LT_TAGVAR(archive_cmds, $1)='$LD -G -h $soname -o $lib $libobjs $deplibs $linker_flags' - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-L$libdir' - _LT_TAGVAR(hardcode_shlibpath_var, $1)=no - ;; - - # FreeBSD 2.2.[012] allows us to include c++rt0.o to get C++ constructor - # support. Future versions do this automatically, but an explicit c++rt0.o - # does not break anything, and helps significantly (at the cost of a little - # extra space). - freebsd2.2*) - _LT_TAGVAR(archive_cmds, $1)='$LD -Bshareable -o $lib $libobjs $deplibs $linker_flags /usr/lib/c++rt0.o' - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-R$libdir' - _LT_TAGVAR(hardcode_direct, $1)=yes - _LT_TAGVAR(hardcode_shlibpath_var, $1)=no - ;; - - # Unfortunately, older versions of FreeBSD 2 do not have this feature. - freebsd2.*) - _LT_TAGVAR(archive_cmds, $1)='$LD -Bshareable -o $lib $libobjs $deplibs $linker_flags' - _LT_TAGVAR(hardcode_direct, $1)=yes - _LT_TAGVAR(hardcode_minus_L, $1)=yes - _LT_TAGVAR(hardcode_shlibpath_var, $1)=no - ;; - - # FreeBSD 3 and greater uses gcc -shared to do shared libraries. - freebsd* | dragonfly*) - _LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag -o $lib $libobjs $deplibs $compiler_flags' - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-R$libdir' - _LT_TAGVAR(hardcode_direct, $1)=yes - _LT_TAGVAR(hardcode_shlibpath_var, $1)=no - ;; - - hpux9*) - if test yes = "$GCC"; then - _LT_TAGVAR(archive_cmds, $1)='$RM $output_objdir/$soname~$CC -shared $pic_flag $wl+b $wl$install_libdir -o $output_objdir/$soname $libobjs $deplibs $compiler_flags~test "x$output_objdir/$soname" = "x$lib" || mv $output_objdir/$soname $lib' - else - _LT_TAGVAR(archive_cmds, $1)='$RM $output_objdir/$soname~$LD -b +b $install_libdir -o $output_objdir/$soname $libobjs $deplibs $linker_flags~test "x$output_objdir/$soname" = "x$lib" || mv $output_objdir/$soname $lib' - fi - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl+b $wl$libdir' - _LT_TAGVAR(hardcode_libdir_separator, $1)=: - _LT_TAGVAR(hardcode_direct, $1)=yes - - # hardcode_minus_L: Not really in the search PATH, - # but as the default location of the library. - _LT_TAGVAR(hardcode_minus_L, $1)=yes - _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl-E' - ;; - - hpux10*) - if test yes,no = "$GCC,$with_gnu_ld"; then - _LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag $wl+h $wl$soname $wl+b $wl$install_libdir -o $lib $libobjs $deplibs $compiler_flags' - else - _LT_TAGVAR(archive_cmds, $1)='$LD -b +h $soname +b $install_libdir -o $lib $libobjs $deplibs $linker_flags' - fi - if test no = "$with_gnu_ld"; then - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl+b $wl$libdir' - _LT_TAGVAR(hardcode_libdir_separator, $1)=: - _LT_TAGVAR(hardcode_direct, $1)=yes - _LT_TAGVAR(hardcode_direct_absolute, $1)=yes - _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl-E' - # hardcode_minus_L: Not really in the search PATH, - # but as the default location of the library. - _LT_TAGVAR(hardcode_minus_L, $1)=yes - fi - ;; - - hpux11*) - if test yes,no = "$GCC,$with_gnu_ld"; then - case $host_cpu in - hppa*64*) - _LT_TAGVAR(archive_cmds, $1)='$CC -shared $wl+h $wl$soname -o $lib $libobjs $deplibs $compiler_flags' - ;; - ia64*) - _LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag $wl+h $wl$soname $wl+nodefaultrpath -o $lib $libobjs $deplibs $compiler_flags' - ;; - *) - _LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag $wl+h $wl$soname $wl+b $wl$install_libdir -o $lib $libobjs $deplibs $compiler_flags' - ;; - esac - else - case $host_cpu in - hppa*64*) - _LT_TAGVAR(archive_cmds, $1)='$CC -b $wl+h $wl$soname -o $lib $libobjs $deplibs $compiler_flags' - ;; - ia64*) - _LT_TAGVAR(archive_cmds, $1)='$CC -b $wl+h $wl$soname $wl+nodefaultrpath -o $lib $libobjs $deplibs $compiler_flags' - ;; - *) - m4_if($1, [], [ - # Older versions of the 11.00 compiler do not understand -b yet - # (HP92453-01 A.11.01.20 doesn't, HP92453-01 B.11.X.35175-35176.GP does) - _LT_LINKER_OPTION([if $CC understands -b], - _LT_TAGVAR(lt_cv_prog_compiler__b, $1), [-b], - [_LT_TAGVAR(archive_cmds, $1)='$CC -b $wl+h $wl$soname $wl+b $wl$install_libdir -o $lib $libobjs $deplibs $compiler_flags'], - [_LT_TAGVAR(archive_cmds, $1)='$LD -b +h $soname +b $install_libdir -o $lib $libobjs $deplibs $linker_flags'])], - [_LT_TAGVAR(archive_cmds, $1)='$CC -b $wl+h $wl$soname $wl+b $wl$install_libdir -o $lib $libobjs $deplibs $compiler_flags']) - ;; - esac - fi - if test no = "$with_gnu_ld"; then - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl+b $wl$libdir' - _LT_TAGVAR(hardcode_libdir_separator, $1)=: - - case $host_cpu in - hppa*64*|ia64*) - _LT_TAGVAR(hardcode_direct, $1)=no - _LT_TAGVAR(hardcode_shlibpath_var, $1)=no - ;; - *) - _LT_TAGVAR(hardcode_direct, $1)=yes - _LT_TAGVAR(hardcode_direct_absolute, $1)=yes - _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl-E' - - # hardcode_minus_L: Not really in the search PATH, - # but as the default location of the library. - _LT_TAGVAR(hardcode_minus_L, $1)=yes - ;; - esac - fi - ;; - - irix5* | irix6* | nonstopux*) - if test yes = "$GCC"; then - _LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag $libobjs $deplibs $compiler_flags $wl-soname $wl$soname `test -n "$verstring" && func_echo_all "$wl-set_version $wl$verstring"` $wl-update_registry $wl$output_objdir/so_locations -o $lib' - # Try to use the -exported_symbol ld option, if it does not - # work, assume that -exports_file does not work either and - # implicitly export all symbols. - # This should be the same for all languages, so no per-tag cache variable. - AC_CACHE_CHECK([whether the $host_os linker accepts -exported_symbol], - [lt_cv_irix_exported_symbol], - [save_LDFLAGS=$LDFLAGS - LDFLAGS="$LDFLAGS -shared $wl-exported_symbol ${wl}foo $wl-update_registry $wl/dev/null" - AC_LINK_IFELSE( - [AC_LANG_SOURCE( - [AC_LANG_CASE([C], [[int foo (void) { return 0; }]], - [C++], [[int foo (void) { return 0; }]], - [Fortran 77], [[ - subroutine foo - end]], - [Fortran], [[ - subroutine foo - end]])])], - [lt_cv_irix_exported_symbol=yes], - [lt_cv_irix_exported_symbol=no]) - LDFLAGS=$save_LDFLAGS]) - if test yes = "$lt_cv_irix_exported_symbol"; then - _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $pic_flag $libobjs $deplibs $compiler_flags $wl-soname $wl$soname `test -n "$verstring" && func_echo_all "$wl-set_version $wl$verstring"` $wl-update_registry $wl$output_objdir/so_locations $wl-exports_file $wl$export_symbols -o $lib' - fi - else - _LT_TAGVAR(archive_cmds, $1)='$CC -shared $libobjs $deplibs $compiler_flags -soname $soname `test -n "$verstring" && func_echo_all "-set_version $verstring"` -update_registry $output_objdir/so_locations -o $lib' - _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $libobjs $deplibs $compiler_flags -soname $soname `test -n "$verstring" && func_echo_all "-set_version $verstring"` -update_registry $output_objdir/so_locations -exports_file $export_symbols -o $lib' - fi - _LT_TAGVAR(archive_cmds_need_lc, $1)='no' - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath $wl$libdir' - _LT_TAGVAR(hardcode_libdir_separator, $1)=: - _LT_TAGVAR(inherit_rpath, $1)=yes - _LT_TAGVAR(link_all_deplibs, $1)=yes - ;; - - linux*) - case $cc_basename in - tcc*) - # Fabrice Bellard et al's Tiny C Compiler - _LT_TAGVAR(ld_shlibs, $1)=yes - _LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag -o $lib $libobjs $deplibs $compiler_flags' - ;; - esac - ;; - - netbsd*) - if echo __ELF__ | $CC -E - | $GREP __ELF__ >/dev/null; then - _LT_TAGVAR(archive_cmds, $1)='$LD -Bshareable -o $lib $libobjs $deplibs $linker_flags' # a.out - else - _LT_TAGVAR(archive_cmds, $1)='$LD -shared -o $lib $libobjs $deplibs $linker_flags' # ELF - fi - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-R$libdir' - _LT_TAGVAR(hardcode_direct, $1)=yes - _LT_TAGVAR(hardcode_shlibpath_var, $1)=no - ;; - - newsos6) - _LT_TAGVAR(archive_cmds, $1)='$LD -G -h $soname -o $lib $libobjs $deplibs $linker_flags' - _LT_TAGVAR(hardcode_direct, $1)=yes - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath $wl$libdir' - _LT_TAGVAR(hardcode_libdir_separator, $1)=: - _LT_TAGVAR(hardcode_shlibpath_var, $1)=no - ;; - - *nto* | *qnx*) - ;; - - openbsd* | bitrig*) - if test -f /usr/libexec/ld.so; then - _LT_TAGVAR(hardcode_direct, $1)=yes - _LT_TAGVAR(hardcode_shlibpath_var, $1)=no - _LT_TAGVAR(hardcode_direct_absolute, $1)=yes - if test -z "`echo __ELF__ | $CC -E - | $GREP __ELF__`"; then - _LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag -o $lib $libobjs $deplibs $compiler_flags' - _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $pic_flag -o $lib $libobjs $deplibs $compiler_flags $wl-retain-symbols-file,$export_symbols' - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath,$libdir' - _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl-E' - else - _LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag -o $lib $libobjs $deplibs $compiler_flags' - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath,$libdir' - fi - else - _LT_TAGVAR(ld_shlibs, $1)=no - fi - ;; - - os2*) - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-L$libdir' - _LT_TAGVAR(hardcode_minus_L, $1)=yes - _LT_TAGVAR(allow_undefined_flag, $1)=unsupported - shrext_cmds=.dll - _LT_TAGVAR(archive_cmds, $1)='$ECHO "LIBRARY ${soname%$shared_ext} INITINSTANCE TERMINSTANCE" > $output_objdir/$libname.def~ - $ECHO "DESCRIPTION \"$libname\"" >> $output_objdir/$libname.def~ - $ECHO "DATA MULTIPLE NONSHARED" >> $output_objdir/$libname.def~ - $ECHO EXPORTS >> $output_objdir/$libname.def~ - emxexp $libobjs | $SED /"_DLL_InitTerm"/d >> $output_objdir/$libname.def~ - $CC -Zdll -Zcrtdll -o $output_objdir/$soname $libobjs $deplibs $compiler_flags $output_objdir/$libname.def~ - emximp -o $lib $output_objdir/$libname.def' - _LT_TAGVAR(archive_expsym_cmds, $1)='$ECHO "LIBRARY ${soname%$shared_ext} INITINSTANCE TERMINSTANCE" > $output_objdir/$libname.def~ - $ECHO "DESCRIPTION \"$libname\"" >> $output_objdir/$libname.def~ - $ECHO "DATA MULTIPLE NONSHARED" >> $output_objdir/$libname.def~ - $ECHO EXPORTS >> $output_objdir/$libname.def~ - prefix_cmds="$SED"~ - if test EXPORTS = "`$SED 1q $export_symbols`"; then - prefix_cmds="$prefix_cmds -e 1d"; - fi~ - prefix_cmds="$prefix_cmds -e \"s/^\(.*\)$/_\1/g\""~ - cat $export_symbols | $prefix_cmds >> $output_objdir/$libname.def~ - $CC -Zdll -Zcrtdll -o $output_objdir/$soname $libobjs $deplibs $compiler_flags $output_objdir/$libname.def~ - emximp -o $lib $output_objdir/$libname.def' - _LT_TAGVAR(old_archive_From_new_cmds, $1)='emximp -o $output_objdir/${libname}_dll.a $output_objdir/$libname.def' - _LT_TAGVAR(enable_shared_with_static_runtimes, $1)=yes - ;; - - osf3*) - if test yes = "$GCC"; then - _LT_TAGVAR(allow_undefined_flag, $1)=' $wl-expect_unresolved $wl\*' - _LT_TAGVAR(archive_cmds, $1)='$CC -shared$allow_undefined_flag $libobjs $deplibs $compiler_flags $wl-soname $wl$soname `test -n "$verstring" && func_echo_all "$wl-set_version $wl$verstring"` $wl-update_registry $wl$output_objdir/so_locations -o $lib' - else - _LT_TAGVAR(allow_undefined_flag, $1)=' -expect_unresolved \*' - _LT_TAGVAR(archive_cmds, $1)='$CC -shared$allow_undefined_flag $libobjs $deplibs $compiler_flags -soname $soname `test -n "$verstring" && func_echo_all "-set_version $verstring"` -update_registry $output_objdir/so_locations -o $lib' - fi - _LT_TAGVAR(archive_cmds_need_lc, $1)='no' - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath $wl$libdir' - _LT_TAGVAR(hardcode_libdir_separator, $1)=: - ;; - - osf4* | osf5*) # as osf3* with the addition of -msym flag - if test yes = "$GCC"; then - _LT_TAGVAR(allow_undefined_flag, $1)=' $wl-expect_unresolved $wl\*' - _LT_TAGVAR(archive_cmds, $1)='$CC -shared$allow_undefined_flag $pic_flag $libobjs $deplibs $compiler_flags $wl-msym $wl-soname $wl$soname `test -n "$verstring" && func_echo_all "$wl-set_version $wl$verstring"` $wl-update_registry $wl$output_objdir/so_locations -o $lib' - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath $wl$libdir' - else - _LT_TAGVAR(allow_undefined_flag, $1)=' -expect_unresolved \*' - _LT_TAGVAR(archive_cmds, $1)='$CC -shared$allow_undefined_flag $libobjs $deplibs $compiler_flags -msym -soname $soname `test -n "$verstring" && func_echo_all "-set_version $verstring"` -update_registry $output_objdir/so_locations -o $lib' - _LT_TAGVAR(archive_expsym_cmds, $1)='for i in `cat $export_symbols`; do printf "%s %s\\n" -exported_symbol "\$i" >> $lib.exp; done; printf "%s\\n" "-hidden">> $lib.exp~ - $CC -shared$allow_undefined_flag $wl-input $wl$lib.exp $compiler_flags $libobjs $deplibs -soname $soname `test -n "$verstring" && $ECHO "-set_version $verstring"` -update_registry $output_objdir/so_locations -o $lib~$RM $lib.exp' - - # Both c and cxx compiler support -rpath directly - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-rpath $libdir' - fi - _LT_TAGVAR(archive_cmds_need_lc, $1)='no' - _LT_TAGVAR(hardcode_libdir_separator, $1)=: - ;; - - solaris*) - _LT_TAGVAR(no_undefined_flag, $1)=' -z defs' - if test yes = "$GCC"; then - wlarc='$wl' - _LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag $wl-z ${wl}text $wl-h $wl$soname -o $lib $libobjs $deplibs $compiler_flags' - _LT_TAGVAR(archive_expsym_cmds, $1)='echo "{ global:" > $lib.exp~cat $export_symbols | $SED -e "s/\(.*\)/\1;/" >> $lib.exp~echo "local: *; };" >> $lib.exp~ - $CC -shared $pic_flag $wl-z ${wl}text $wl-M $wl$lib.exp $wl-h $wl$soname -o $lib $libobjs $deplibs $compiler_flags~$RM $lib.exp' - else - case `$CC -V 2>&1` in - *"Compilers 5.0"*) - wlarc='' - _LT_TAGVAR(archive_cmds, $1)='$LD -G$allow_undefined_flag -h $soname -o $lib $libobjs $deplibs $linker_flags' - _LT_TAGVAR(archive_expsym_cmds, $1)='echo "{ global:" > $lib.exp~cat $export_symbols | $SED -e "s/\(.*\)/\1;/" >> $lib.exp~echo "local: *; };" >> $lib.exp~ - $LD -G$allow_undefined_flag -M $lib.exp -h $soname -o $lib $libobjs $deplibs $linker_flags~$RM $lib.exp' - ;; - *) - wlarc='$wl' - _LT_TAGVAR(archive_cmds, $1)='$CC -G$allow_undefined_flag -h $soname -o $lib $libobjs $deplibs $compiler_flags' - _LT_TAGVAR(archive_expsym_cmds, $1)='echo "{ global:" > $lib.exp~cat $export_symbols | $SED -e "s/\(.*\)/\1;/" >> $lib.exp~echo "local: *; };" >> $lib.exp~ - $CC -G$allow_undefined_flag -M $lib.exp -h $soname -o $lib $libobjs $deplibs $compiler_flags~$RM $lib.exp' - ;; - esac - fi - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-R$libdir' - _LT_TAGVAR(hardcode_shlibpath_var, $1)=no - case $host_os in - solaris2.[[0-5]] | solaris2.[[0-5]].*) ;; - *) - # The compiler driver will combine and reorder linker options, - # but understands '-z linker_flag'. GCC discards it without '$wl', - # but is careful enough not to reorder. - # Supported since Solaris 2.6 (maybe 2.5.1?) - if test yes = "$GCC"; then - _LT_TAGVAR(whole_archive_flag_spec, $1)='$wl-z ${wl}allextract$convenience $wl-z ${wl}defaultextract' - else - _LT_TAGVAR(whole_archive_flag_spec, $1)='-z allextract$convenience -z defaultextract' - fi - ;; - esac - _LT_TAGVAR(link_all_deplibs, $1)=yes - ;; - - sunos4*) - if test sequent = "$host_vendor"; then - # Use $CC to link under sequent, because it throws in some extra .o - # files that make .init and .fini sections work. - _LT_TAGVAR(archive_cmds, $1)='$CC -G $wl-h $soname -o $lib $libobjs $deplibs $compiler_flags' - else - _LT_TAGVAR(archive_cmds, $1)='$LD -assert pure-text -Bstatic -o $lib $libobjs $deplibs $linker_flags' - fi - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-L$libdir' - _LT_TAGVAR(hardcode_direct, $1)=yes - _LT_TAGVAR(hardcode_minus_L, $1)=yes - _LT_TAGVAR(hardcode_shlibpath_var, $1)=no - ;; - - sysv4) - case $host_vendor in - sni) - _LT_TAGVAR(archive_cmds, $1)='$LD -G -h $soname -o $lib $libobjs $deplibs $linker_flags' - _LT_TAGVAR(hardcode_direct, $1)=yes # is this really true??? - ;; - siemens) - ## LD is ld it makes a PLAMLIB - ## CC just makes a GrossModule. - _LT_TAGVAR(archive_cmds, $1)='$LD -G -o $lib $libobjs $deplibs $linker_flags' - _LT_TAGVAR(reload_cmds, $1)='$CC -r -o $output$reload_objs' - _LT_TAGVAR(hardcode_direct, $1)=no - ;; - motorola) - _LT_TAGVAR(archive_cmds, $1)='$LD -G -h $soname -o $lib $libobjs $deplibs $linker_flags' - _LT_TAGVAR(hardcode_direct, $1)=no #Motorola manual says yes, but my tests say they lie - ;; - esac - runpath_var='LD_RUN_PATH' - _LT_TAGVAR(hardcode_shlibpath_var, $1)=no - ;; - - sysv4.3*) - _LT_TAGVAR(archive_cmds, $1)='$LD -G -h $soname -o $lib $libobjs $deplibs $linker_flags' - _LT_TAGVAR(hardcode_shlibpath_var, $1)=no - _LT_TAGVAR(export_dynamic_flag_spec, $1)='-Bexport' - ;; - - sysv4*MP*) - if test -d /usr/nec; then - _LT_TAGVAR(archive_cmds, $1)='$LD -G -h $soname -o $lib $libobjs $deplibs $linker_flags' - _LT_TAGVAR(hardcode_shlibpath_var, $1)=no - runpath_var=LD_RUN_PATH - hardcode_runpath_var=yes - _LT_TAGVAR(ld_shlibs, $1)=yes - fi - ;; - - sysv4*uw2* | sysv5OpenUNIX* | sysv5UnixWare7.[[01]].[[10]]* | unixware7* | sco3.2v5.0.[[024]]*) - _LT_TAGVAR(no_undefined_flag, $1)='$wl-z,text' - _LT_TAGVAR(archive_cmds_need_lc, $1)=no - _LT_TAGVAR(hardcode_shlibpath_var, $1)=no - runpath_var='LD_RUN_PATH' - - if test yes = "$GCC"; then - _LT_TAGVAR(archive_cmds, $1)='$CC -shared $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags' - _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $wl-Bexport:$export_symbols $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags' - else - _LT_TAGVAR(archive_cmds, $1)='$CC -G $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags' - _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -G $wl-Bexport:$export_symbols $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags' - fi - ;; - - sysv5* | sco3.2v5* | sco5v6*) - # Note: We CANNOT use -z defs as we might desire, because we do not - # link with -lc, and that would cause any symbols used from libc to - # always be unresolved, which means just about no library would - # ever link correctly. If we're not using GNU ld we use -z text - # though, which does catch some bad symbols but isn't as heavy-handed - # as -z defs. - _LT_TAGVAR(no_undefined_flag, $1)='$wl-z,text' - _LT_TAGVAR(allow_undefined_flag, $1)='$wl-z,nodefs' - _LT_TAGVAR(archive_cmds_need_lc, $1)=no - _LT_TAGVAR(hardcode_shlibpath_var, $1)=no - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-R,$libdir' - _LT_TAGVAR(hardcode_libdir_separator, $1)=':' - _LT_TAGVAR(link_all_deplibs, $1)=yes - _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl-Bexport' - runpath_var='LD_RUN_PATH' - - if test yes = "$GCC"; then - _LT_TAGVAR(archive_cmds, $1)='$CC -shared $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags' - _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $wl-Bexport:$export_symbols $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags' - else - _LT_TAGVAR(archive_cmds, $1)='$CC -G $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags' - _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -G $wl-Bexport:$export_symbols $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags' - fi - ;; - - uts4*) - _LT_TAGVAR(archive_cmds, $1)='$LD -G -h $soname -o $lib $libobjs $deplibs $linker_flags' - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-L$libdir' - _LT_TAGVAR(hardcode_shlibpath_var, $1)=no - ;; - - *) - _LT_TAGVAR(ld_shlibs, $1)=no - ;; - esac - - if test sni = "$host_vendor"; then - case $host in - sysv4 | sysv4.2uw2* | sysv4.3* | sysv5*) - _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl-Blargedynsym' - ;; - esac - fi - fi -]) -AC_MSG_RESULT([$_LT_TAGVAR(ld_shlibs, $1)]) -test no = "$_LT_TAGVAR(ld_shlibs, $1)" && can_build_shared=no - -_LT_TAGVAR(with_gnu_ld, $1)=$with_gnu_ld - -_LT_DECL([], [libext], [0], [Old archive suffix (normally "a")])dnl -_LT_DECL([], [shrext_cmds], [1], [Shared library suffix (normally ".so")])dnl -_LT_DECL([], [extract_expsyms_cmds], [2], - [The commands to extract the exported symbol list from a shared archive]) - -# -# Do we need to explicitly link libc? -# -case "x$_LT_TAGVAR(archive_cmds_need_lc, $1)" in -x|xyes) - # Assume -lc should be added - _LT_TAGVAR(archive_cmds_need_lc, $1)=yes - - if test yes,yes = "$GCC,$enable_shared"; then - case $_LT_TAGVAR(archive_cmds, $1) in - *'~'*) - # FIXME: we may have to deal with multi-command sequences. - ;; - '$CC '*) - # Test whether the compiler implicitly links with -lc since on some - # systems, -lgcc has to come before -lc. If gcc already passes -lc - # to ld, don't add -lc before -lgcc. - AC_CACHE_CHECK([whether -lc should be explicitly linked in], - [lt_cv_]_LT_TAGVAR(archive_cmds_need_lc, $1), - [$RM conftest* - echo "$lt_simple_compile_test_code" > conftest.$ac_ext - - if AC_TRY_EVAL(ac_compile) 2>conftest.err; then - soname=conftest - lib=conftest - libobjs=conftest.$ac_objext - deplibs= - wl=$_LT_TAGVAR(lt_prog_compiler_wl, $1) - pic_flag=$_LT_TAGVAR(lt_prog_compiler_pic, $1) - compiler_flags=-v - linker_flags=-v - verstring= - output_objdir=. - libname=conftest - lt_save_allow_undefined_flag=$_LT_TAGVAR(allow_undefined_flag, $1) - _LT_TAGVAR(allow_undefined_flag, $1)= - if AC_TRY_EVAL(_LT_TAGVAR(archive_cmds, $1) 2\>\&1 \| $GREP \" -lc \" \>/dev/null 2\>\&1) - then - lt_cv_[]_LT_TAGVAR(archive_cmds_need_lc, $1)=no - else - lt_cv_[]_LT_TAGVAR(archive_cmds_need_lc, $1)=yes - fi - _LT_TAGVAR(allow_undefined_flag, $1)=$lt_save_allow_undefined_flag - else - cat conftest.err 1>&5 - fi - $RM conftest* - ]) - _LT_TAGVAR(archive_cmds_need_lc, $1)=$lt_cv_[]_LT_TAGVAR(archive_cmds_need_lc, $1) - ;; - esac - fi - ;; -esac - -_LT_TAGDECL([build_libtool_need_lc], [archive_cmds_need_lc], [0], - [Whether or not to add -lc for building shared libraries]) -_LT_TAGDECL([allow_libtool_libs_with_static_runtimes], - [enable_shared_with_static_runtimes], [0], - [Whether or not to disallow shared libs when runtime libs are static]) -_LT_TAGDECL([], [export_dynamic_flag_spec], [1], - [Compiler flag to allow reflexive dlopens]) -_LT_TAGDECL([], [whole_archive_flag_spec], [1], - [Compiler flag to generate shared objects directly from archives]) -_LT_TAGDECL([], [compiler_needs_object], [1], - [Whether the compiler copes with passing no objects directly]) -_LT_TAGDECL([], [old_archive_from_new_cmds], [2], - [Create an old-style archive from a shared archive]) -_LT_TAGDECL([], [old_archive_from_expsyms_cmds], [2], - [Create a temporary old-style archive to link instead of a shared archive]) -_LT_TAGDECL([], [archive_cmds], [2], [Commands used to build a shared archive]) -_LT_TAGDECL([], [archive_expsym_cmds], [2]) -_LT_TAGDECL([], [module_cmds], [2], - [Commands used to build a loadable module if different from building - a shared archive.]) -_LT_TAGDECL([], [module_expsym_cmds], [2]) -_LT_TAGDECL([], [with_gnu_ld], [1], - [Whether we are building with GNU ld or not]) -_LT_TAGDECL([], [allow_undefined_flag], [1], - [Flag that allows shared libraries with undefined symbols to be built]) -_LT_TAGDECL([], [no_undefined_flag], [1], - [Flag that enforces no undefined symbols]) -_LT_TAGDECL([], [hardcode_libdir_flag_spec], [1], - [Flag to hardcode $libdir into a binary during linking. - This must work even if $libdir does not exist]) -_LT_TAGDECL([], [hardcode_libdir_separator], [1], - [Whether we need a single "-rpath" flag with a separated argument]) -_LT_TAGDECL([], [hardcode_direct], [0], - [Set to "yes" if using DIR/libNAME$shared_ext during linking hardcodes - DIR into the resulting binary]) -_LT_TAGDECL([], [hardcode_direct_absolute], [0], - [Set to "yes" if using DIR/libNAME$shared_ext during linking hardcodes - DIR into the resulting binary and the resulting library dependency is - "absolute", i.e impossible to change by setting $shlibpath_var if the - library is relocated]) -_LT_TAGDECL([], [hardcode_minus_L], [0], - [Set to "yes" if using the -LDIR flag during linking hardcodes DIR - into the resulting binary]) -_LT_TAGDECL([], [hardcode_shlibpath_var], [0], - [Set to "yes" if using SHLIBPATH_VAR=DIR during linking hardcodes DIR - into the resulting binary]) -_LT_TAGDECL([], [hardcode_automatic], [0], - [Set to "yes" if building a shared library automatically hardcodes DIR - into the library and all subsequent libraries and executables linked - against it]) -_LT_TAGDECL([], [inherit_rpath], [0], - [Set to yes if linker adds runtime paths of dependent libraries - to runtime path list]) -_LT_TAGDECL([], [link_all_deplibs], [0], - [Whether libtool must link a program against all its dependency libraries]) -_LT_TAGDECL([], [always_export_symbols], [0], - [Set to "yes" if exported symbols are required]) -_LT_TAGDECL([], [export_symbols_cmds], [2], - [The commands to list exported symbols]) -_LT_TAGDECL([], [exclude_expsyms], [1], - [Symbols that should not be listed in the preloaded symbols]) -_LT_TAGDECL([], [include_expsyms], [1], - [Symbols that must always be exported]) -_LT_TAGDECL([], [prelink_cmds], [2], - [Commands necessary for linking programs (against libraries) with templates]) -_LT_TAGDECL([], [postlink_cmds], [2], - [Commands necessary for finishing linking programs]) -_LT_TAGDECL([], [file_list_spec], [1], - [Specify filename containing input files]) -dnl FIXME: Not yet implemented -dnl _LT_TAGDECL([], [thread_safe_flag_spec], [1], -dnl [Compiler flag to generate thread safe objects]) -])# _LT_LINKER_SHLIBS - - -# _LT_LANG_C_CONFIG([TAG]) -# ------------------------ -# Ensure that the configuration variables for a C compiler are suitably -# defined. These variables are subsequently used by _LT_CONFIG to write -# the compiler configuration to 'libtool'. -m4_defun([_LT_LANG_C_CONFIG], -[m4_require([_LT_DECL_EGREP])dnl -lt_save_CC=$CC -AC_LANG_PUSH(C) - -# Source file extension for C test sources. -ac_ext=c - -# Object file extension for compiled C test sources. -objext=o -_LT_TAGVAR(objext, $1)=$objext - -# Code to be used in simple compile tests -lt_simple_compile_test_code="int some_variable = 0;" - -# Code to be used in simple link tests -lt_simple_link_test_code='int main(){return(0);}' - -_LT_TAG_COMPILER -# Save the default compiler, since it gets overwritten when the other -# tags are being tested, and _LT_TAGVAR(compiler, []) is a NOP. -compiler_DEFAULT=$CC - -# save warnings/boilerplate of simple test code -_LT_COMPILER_BOILERPLATE -_LT_LINKER_BOILERPLATE - -if test -n "$compiler"; then - _LT_COMPILER_NO_RTTI($1) - _LT_COMPILER_PIC($1) - _LT_COMPILER_C_O($1) - _LT_COMPILER_FILE_LOCKS($1) - _LT_LINKER_SHLIBS($1) - _LT_SYS_DYNAMIC_LINKER($1) - _LT_LINKER_HARDCODE_LIBPATH($1) - LT_SYS_DLOPEN_SELF - _LT_CMD_STRIPLIB - - # Report what library types will actually be built - AC_MSG_CHECKING([if libtool supports shared libraries]) - AC_MSG_RESULT([$can_build_shared]) - - AC_MSG_CHECKING([whether to build shared libraries]) - test no = "$can_build_shared" && enable_shared=no - - # On AIX, shared libraries and static libraries use the same namespace, and - # are all built from PIC. - case $host_os in - aix3*) - test yes = "$enable_shared" && enable_static=no - if test -n "$RANLIB"; then - archive_cmds="$archive_cmds~\$RANLIB \$lib" - postinstall_cmds='$RANLIB $lib' - fi - ;; - - aix[[4-9]]*) - if test ia64 != "$host_cpu"; then - case $enable_shared,$with_aix_soname,$aix_use_runtimelinking in - yes,aix,yes) ;; # shared object as lib.so file only - yes,svr4,*) ;; # shared object as lib.so archive member only - yes,*) enable_static=no ;; # shared object in lib.a archive as well - esac - fi - ;; - esac - AC_MSG_RESULT([$enable_shared]) - - AC_MSG_CHECKING([whether to build static libraries]) - # Make sure either enable_shared or enable_static is yes. - test yes = "$enable_shared" || enable_static=yes - AC_MSG_RESULT([$enable_static]) - - _LT_CONFIG($1) -fi -AC_LANG_POP -CC=$lt_save_CC -])# _LT_LANG_C_CONFIG - - -# _LT_LANG_CXX_CONFIG([TAG]) -# -------------------------- -# Ensure that the configuration variables for a C++ compiler are suitably -# defined. These variables are subsequently used by _LT_CONFIG to write -# the compiler configuration to 'libtool'. -m4_defun([_LT_LANG_CXX_CONFIG], -[m4_require([_LT_FILEUTILS_DEFAULTS])dnl -m4_require([_LT_DECL_EGREP])dnl -m4_require([_LT_PATH_MANIFEST_TOOL])dnl -if test -n "$CXX" && ( test no != "$CXX" && - ( (test g++ = "$CXX" && `g++ -v >/dev/null 2>&1` ) || - (test g++ != "$CXX"))); then - AC_PROG_CXXCPP -else - _lt_caught_CXX_error=yes -fi - -AC_LANG_PUSH(C++) -_LT_TAGVAR(archive_cmds_need_lc, $1)=no -_LT_TAGVAR(allow_undefined_flag, $1)= -_LT_TAGVAR(always_export_symbols, $1)=no -_LT_TAGVAR(archive_expsym_cmds, $1)= -_LT_TAGVAR(compiler_needs_object, $1)=no -_LT_TAGVAR(export_dynamic_flag_spec, $1)= -_LT_TAGVAR(hardcode_direct, $1)=no -_LT_TAGVAR(hardcode_direct_absolute, $1)=no -_LT_TAGVAR(hardcode_libdir_flag_spec, $1)= -_LT_TAGVAR(hardcode_libdir_separator, $1)= -_LT_TAGVAR(hardcode_minus_L, $1)=no -_LT_TAGVAR(hardcode_shlibpath_var, $1)=unsupported -_LT_TAGVAR(hardcode_automatic, $1)=no -_LT_TAGVAR(inherit_rpath, $1)=no -_LT_TAGVAR(module_cmds, $1)= -_LT_TAGVAR(module_expsym_cmds, $1)= -_LT_TAGVAR(link_all_deplibs, $1)=unknown -_LT_TAGVAR(old_archive_cmds, $1)=$old_archive_cmds -_LT_TAGVAR(reload_flag, $1)=$reload_flag -_LT_TAGVAR(reload_cmds, $1)=$reload_cmds -_LT_TAGVAR(no_undefined_flag, $1)= -_LT_TAGVAR(whole_archive_flag_spec, $1)= -_LT_TAGVAR(enable_shared_with_static_runtimes, $1)=no - -# Source file extension for C++ test sources. -ac_ext=cpp - -# Object file extension for compiled C++ test sources. -objext=o -_LT_TAGVAR(objext, $1)=$objext - -# No sense in running all these tests if we already determined that -# the CXX compiler isn't working. Some variables (like enable_shared) -# are currently assumed to apply to all compilers on this platform, -# and will be corrupted by setting them based on a non-working compiler. -if test yes != "$_lt_caught_CXX_error"; then - # Code to be used in simple compile tests - lt_simple_compile_test_code="int some_variable = 0;" - - # Code to be used in simple link tests - lt_simple_link_test_code='int main(int, char *[[]]) { return(0); }' - - # ltmain only uses $CC for tagged configurations so make sure $CC is set. - _LT_TAG_COMPILER - - # save warnings/boilerplate of simple test code - _LT_COMPILER_BOILERPLATE - _LT_LINKER_BOILERPLATE - - # Allow CC to be a program name with arguments. - lt_save_CC=$CC - lt_save_CFLAGS=$CFLAGS - lt_save_LD=$LD - lt_save_GCC=$GCC - GCC=$GXX - lt_save_with_gnu_ld=$with_gnu_ld - lt_save_path_LD=$lt_cv_path_LD - if test -n "${lt_cv_prog_gnu_ldcxx+set}"; then - lt_cv_prog_gnu_ld=$lt_cv_prog_gnu_ldcxx - else - $as_unset lt_cv_prog_gnu_ld - fi - if test -n "${lt_cv_path_LDCXX+set}"; then - lt_cv_path_LD=$lt_cv_path_LDCXX - else - $as_unset lt_cv_path_LD - fi - test -z "${LDCXX+set}" || LD=$LDCXX - CC=${CXX-"c++"} - CFLAGS=$CXXFLAGS - compiler=$CC - _LT_TAGVAR(compiler, $1)=$CC - _LT_CC_BASENAME([$compiler]) - - if test -n "$compiler"; then - # We don't want -fno-exception when compiling C++ code, so set the - # no_builtin_flag separately - if test yes = "$GXX"; then - _LT_TAGVAR(lt_prog_compiler_no_builtin_flag, $1)=' -fno-builtin' - else - _LT_TAGVAR(lt_prog_compiler_no_builtin_flag, $1)= - fi - - if test yes = "$GXX"; then - # Set up default GNU C++ configuration - - LT_PATH_LD - - # Check if GNU C++ uses GNU ld as the underlying linker, since the - # archiving commands below assume that GNU ld is being used. - if test yes = "$with_gnu_ld"; then - _LT_TAGVAR(archive_cmds, $1)='$CC $pic_flag -shared -nostdlib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-soname $wl$soname -o $lib' - _LT_TAGVAR(archive_expsym_cmds, $1)='$CC $pic_flag -shared -nostdlib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-soname $wl$soname $wl-retain-symbols-file $wl$export_symbols -o $lib' - - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath $wl$libdir' - _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl--export-dynamic' - - # If archive_cmds runs LD, not CC, wlarc should be empty - # XXX I think wlarc can be eliminated in ltcf-cxx, but I need to - # investigate it a little bit more. (MM) - wlarc='$wl' - - # ancient GNU ld didn't support --whole-archive et. al. - if eval "`$CC -print-prog-name=ld` --help 2>&1" | - $GREP 'no-whole-archive' > /dev/null; then - _LT_TAGVAR(whole_archive_flag_spec, $1)=$wlarc'--whole-archive$convenience '$wlarc'--no-whole-archive' - else - _LT_TAGVAR(whole_archive_flag_spec, $1)= - fi - else - with_gnu_ld=no - wlarc= - - # A generic and very simple default shared library creation - # command for GNU C++ for the case where it uses the native - # linker, instead of GNU ld. If possible, this setting should - # overridden to take advantage of the native linker features on - # the platform it is being used on. - _LT_TAGVAR(archive_cmds, $1)='$CC -shared -nostdlib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags -o $lib' - fi - - # Commands to make compiler produce verbose output that lists - # what "hidden" libraries, object files and flags are used when - # linking a shared library. - output_verbose_link_cmd='$CC -shared $CFLAGS -v conftest.$objext 2>&1 | $GREP -v "^Configured with:" | $GREP "\-L"' - - else - GXX=no - with_gnu_ld=no - wlarc= - fi - - # PORTME: fill in a description of your system's C++ link characteristics - AC_MSG_CHECKING([whether the $compiler linker ($LD) supports shared libraries]) - _LT_TAGVAR(ld_shlibs, $1)=yes - case $host_os in - aix3*) - # FIXME: insert proper C++ library support - _LT_TAGVAR(ld_shlibs, $1)=no - ;; - aix[[4-9]]*) - if test ia64 = "$host_cpu"; then - # On IA64, the linker does run time linking by default, so we don't - # have to do anything special. - aix_use_runtimelinking=no - exp_sym_flag='-Bexport' - no_entry_flag= - else - aix_use_runtimelinking=no - - # Test if we are trying to use run time linking or normal - # AIX style linking. If -brtl is somewhere in LDFLAGS, we - # have runtime linking enabled, and use it for executables. - # For shared libraries, we enable/disable runtime linking - # depending on the kind of the shared library created - - # when "with_aix_soname,aix_use_runtimelinking" is: - # "aix,no" lib.a(lib.so.V) shared, rtl:no, for executables - # "aix,yes" lib.so shared, rtl:yes, for executables - # lib.a static archive - # "both,no" lib.so.V(shr.o) shared, rtl:yes - # lib.a(lib.so.V) shared, rtl:no, for executables - # "both,yes" lib.so.V(shr.o) shared, rtl:yes, for executables - # lib.a(lib.so.V) shared, rtl:no - # "svr4,*" lib.so.V(shr.o) shared, rtl:yes, for executables - # lib.a static archive - case $host_os in aix4.[[23]]|aix4.[[23]].*|aix[[5-9]]*) - for ld_flag in $LDFLAGS; do - case $ld_flag in - *-brtl*) - aix_use_runtimelinking=yes - break - ;; - esac - done - if test svr4,no = "$with_aix_soname,$aix_use_runtimelinking"; then - # With aix-soname=svr4, we create the lib.so.V shared archives only, - # so we don't have lib.a shared libs to link our executables. - # We have to force runtime linking in this case. - aix_use_runtimelinking=yes - LDFLAGS="$LDFLAGS -Wl,-brtl" - fi - ;; - esac - - exp_sym_flag='-bexport' - no_entry_flag='-bnoentry' - fi - - # When large executables or shared objects are built, AIX ld can - # have problems creating the table of contents. If linking a library - # or program results in "error TOC overflow" add -mminimal-toc to - # CXXFLAGS/CFLAGS for g++/gcc. In the cases where that is not - # enough to fix the problem, add -Wl,-bbigtoc to LDFLAGS. - - _LT_TAGVAR(archive_cmds, $1)='' - _LT_TAGVAR(hardcode_direct, $1)=yes - _LT_TAGVAR(hardcode_direct_absolute, $1)=yes - _LT_TAGVAR(hardcode_libdir_separator, $1)=':' - _LT_TAGVAR(link_all_deplibs, $1)=yes - _LT_TAGVAR(file_list_spec, $1)='$wl-f,' - case $with_aix_soname,$aix_use_runtimelinking in - aix,*) ;; # no import file - svr4,* | *,yes) # use import file - # The Import File defines what to hardcode. - _LT_TAGVAR(hardcode_direct, $1)=no - _LT_TAGVAR(hardcode_direct_absolute, $1)=no - ;; - esac - - if test yes = "$GXX"; then - case $host_os in aix4.[[012]]|aix4.[[012]].*) - # We only want to do this on AIX 4.2 and lower, the check - # below for broken collect2 doesn't work under 4.3+ - collect2name=`$CC -print-prog-name=collect2` - if test -f "$collect2name" && - strings "$collect2name" | $GREP resolve_lib_name >/dev/null - then - # We have reworked collect2 - : - else - # We have old collect2 - _LT_TAGVAR(hardcode_direct, $1)=unsupported - # It fails to find uninstalled libraries when the uninstalled - # path is not listed in the libpath. Setting hardcode_minus_L - # to unsupported forces relinking - _LT_TAGVAR(hardcode_minus_L, $1)=yes - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-L$libdir' - _LT_TAGVAR(hardcode_libdir_separator, $1)= - fi - esac - shared_flag='-shared' - if test yes = "$aix_use_runtimelinking"; then - shared_flag=$shared_flag' $wl-G' - fi - # Need to ensure runtime linking is disabled for the traditional - # shared library, or the linker may eventually find shared libraries - # /with/ Import File - we do not want to mix them. - shared_flag_aix='-shared' - shared_flag_svr4='-shared $wl-G' - else - # not using gcc - if test ia64 = "$host_cpu"; then - # VisualAge C++, Version 5.5 for AIX 5L for IA-64, Beta 3 Release - # chokes on -Wl,-G. The following line is correct: - shared_flag='-G' - else - if test yes = "$aix_use_runtimelinking"; then - shared_flag='$wl-G' - else - shared_flag='$wl-bM:SRE' - fi - shared_flag_aix='$wl-bM:SRE' - shared_flag_svr4='$wl-G' - fi - fi - - _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl-bexpall' - # It seems that -bexpall does not export symbols beginning with - # underscore (_), so it is better to generate a list of symbols to - # export. - _LT_TAGVAR(always_export_symbols, $1)=yes - if test aix,yes = "$with_aix_soname,$aix_use_runtimelinking"; then - # Warning - without using the other runtime loading flags (-brtl), - # -berok will link without error, but may produce a broken library. - # The "-G" linker flag allows undefined symbols. - _LT_TAGVAR(no_undefined_flag, $1)='-bernotok' - # Determine the default libpath from the value encoded in an empty - # executable. - _LT_SYS_MODULE_PATH_AIX([$1]) - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-blibpath:$libdir:'"$aix_libpath" - - _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -o $output_objdir/$soname $libobjs $deplibs $wl'$no_entry_flag' $compiler_flags `if test -n "$allow_undefined_flag"; then func_echo_all "$wl$allow_undefined_flag"; else :; fi` $wl'$exp_sym_flag:\$export_symbols' '$shared_flag - else - if test ia64 = "$host_cpu"; then - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-R $libdir:/usr/lib:/lib' - _LT_TAGVAR(allow_undefined_flag, $1)="-z nodefs" - _LT_TAGVAR(archive_expsym_cmds, $1)="\$CC $shared_flag"' -o $output_objdir/$soname $libobjs $deplibs '"\$wl$no_entry_flag"' $compiler_flags $wl$allow_undefined_flag '"\$wl$exp_sym_flag:\$export_symbols" - else - # Determine the default libpath from the value encoded in an - # empty executable. - _LT_SYS_MODULE_PATH_AIX([$1]) - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-blibpath:$libdir:'"$aix_libpath" - # Warning - without using the other run time loading flags, - # -berok will link without error, but may produce a broken library. - _LT_TAGVAR(no_undefined_flag, $1)=' $wl-bernotok' - _LT_TAGVAR(allow_undefined_flag, $1)=' $wl-berok' - if test yes = "$with_gnu_ld"; then - # We only use this code for GNU lds that support --whole-archive. - _LT_TAGVAR(whole_archive_flag_spec, $1)='$wl--whole-archive$convenience $wl--no-whole-archive' - else - # Exported symbols can be pulled into shared objects from archives - _LT_TAGVAR(whole_archive_flag_spec, $1)='$convenience' - fi - _LT_TAGVAR(archive_cmds_need_lc, $1)=yes - _LT_TAGVAR(archive_expsym_cmds, $1)='$RM -r $output_objdir/$realname.d~$MKDIR $output_objdir/$realname.d' - # -brtl affects multiple linker settings, -berok does not and is overridden later - compiler_flags_filtered='`func_echo_all "$compiler_flags " | $SED -e "s%-brtl\\([[, ]]\\)%-berok\\1%g"`' - if test svr4 != "$with_aix_soname"; then - # This is similar to how AIX traditionally builds its shared - # libraries. Need -bnortl late, we may have -brtl in LDFLAGS. - _LT_TAGVAR(archive_expsym_cmds, $1)="$_LT_TAGVAR(archive_expsym_cmds, $1)"'~$CC '$shared_flag_aix' -o $output_objdir/$realname.d/$soname $libobjs $deplibs $wl-bnoentry '$compiler_flags_filtered'$wl-bE:$export_symbols$allow_undefined_flag~$AR $AR_FLAGS $output_objdir/$libname$release.a $output_objdir/$realname.d/$soname' - fi - if test aix != "$with_aix_soname"; then - _LT_TAGVAR(archive_expsym_cmds, $1)="$_LT_TAGVAR(archive_expsym_cmds, $1)"'~$CC '$shared_flag_svr4' -o $output_objdir/$realname.d/$shared_archive_member_spec.o $libobjs $deplibs $wl-bnoentry '$compiler_flags_filtered'$wl-bE:$export_symbols$allow_undefined_flag~$STRIP -e $output_objdir/$realname.d/$shared_archive_member_spec.o~( func_echo_all "#! $soname($shared_archive_member_spec.o)"; if test shr_64 = "$shared_archive_member_spec"; then func_echo_all "# 64"; else func_echo_all "# 32"; fi; cat $export_symbols ) > $output_objdir/$realname.d/$shared_archive_member_spec.imp~$AR $AR_FLAGS $output_objdir/$soname $output_objdir/$realname.d/$shared_archive_member_spec.o $output_objdir/$realname.d/$shared_archive_member_spec.imp' - else - # used by -dlpreopen to get the symbols - _LT_TAGVAR(archive_expsym_cmds, $1)="$_LT_TAGVAR(archive_expsym_cmds, $1)"'~$MV $output_objdir/$realname.d/$soname $output_objdir' - fi - _LT_TAGVAR(archive_expsym_cmds, $1)="$_LT_TAGVAR(archive_expsym_cmds, $1)"'~$RM -r $output_objdir/$realname.d' - fi - fi - ;; - - beos*) - if $LD --help 2>&1 | $GREP ': supported targets:.* elf' > /dev/null; then - _LT_TAGVAR(allow_undefined_flag, $1)=unsupported - # Joseph Beckenbach says some releases of gcc - # support --undefined. This deserves some investigation. FIXME - _LT_TAGVAR(archive_cmds, $1)='$CC -nostart $libobjs $deplibs $compiler_flags $wl-soname $wl$soname -o $lib' - else - _LT_TAGVAR(ld_shlibs, $1)=no - fi - ;; - - chorus*) - case $cc_basename in - *) - # FIXME: insert proper C++ library support - _LT_TAGVAR(ld_shlibs, $1)=no - ;; - esac - ;; - - cygwin* | mingw* | pw32* | cegcc*) - case $GXX,$cc_basename in - ,cl* | no,cl*) - # Native MSVC - # hardcode_libdir_flag_spec is actually meaningless, as there is - # no search path for DLLs. - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)=' ' - _LT_TAGVAR(allow_undefined_flag, $1)=unsupported - _LT_TAGVAR(always_export_symbols, $1)=yes - _LT_TAGVAR(file_list_spec, $1)='@' - # Tell ltmain to make .lib files, not .a files. - libext=lib - # Tell ltmain to make .dll files, not .so files. - shrext_cmds=.dll - # FIXME: Setting linknames here is a bad hack. - _LT_TAGVAR(archive_cmds, $1)='$CC -o $output_objdir/$soname $libobjs $compiler_flags $deplibs -Wl,-DLL,-IMPLIB:"$tool_output_objdir$libname.dll.lib"~linknames=' - _LT_TAGVAR(archive_expsym_cmds, $1)='if _LT_DLL_DEF_P([$export_symbols]); then - cp "$export_symbols" "$output_objdir/$soname.def"; - echo "$tool_output_objdir$soname.def" > "$output_objdir/$soname.exp"; - else - $SED -e '\''s/^/-link -EXPORT:/'\'' < $export_symbols > $output_objdir/$soname.exp; - fi~ - $CC -o $tool_output_objdir$soname $libobjs $compiler_flags $deplibs "@$tool_output_objdir$soname.exp" -Wl,-DLL,-IMPLIB:"$tool_output_objdir$libname.dll.lib"~ - linknames=' - # The linker will not automatically build a static lib if we build a DLL. - # _LT_TAGVAR(old_archive_from_new_cmds, $1)='true' - _LT_TAGVAR(enable_shared_with_static_runtimes, $1)=yes - # Don't use ranlib - _LT_TAGVAR(old_postinstall_cmds, $1)='chmod 644 $oldlib' - _LT_TAGVAR(postlink_cmds, $1)='lt_outputfile="@OUTPUT@"~ - lt_tool_outputfile="@TOOL_OUTPUT@"~ - case $lt_outputfile in - *.exe|*.EXE) ;; - *) - lt_outputfile=$lt_outputfile.exe - lt_tool_outputfile=$lt_tool_outputfile.exe - ;; - esac~ - func_to_tool_file "$lt_outputfile"~ - if test : != "$MANIFEST_TOOL" && test -f "$lt_outputfile.manifest"; then - $MANIFEST_TOOL -manifest "$lt_tool_outputfile.manifest" -outputresource:"$lt_tool_outputfile" || exit 1; - $RM "$lt_outputfile.manifest"; - fi' - ;; - *) - # g++ - # _LT_TAGVAR(hardcode_libdir_flag_spec, $1) is actually meaningless, - # as there is no search path for DLLs. - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-L$libdir' - _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl--export-all-symbols' - _LT_TAGVAR(allow_undefined_flag, $1)=unsupported - _LT_TAGVAR(always_export_symbols, $1)=no - _LT_TAGVAR(enable_shared_with_static_runtimes, $1)=yes - - if $LD --help 2>&1 | $GREP 'auto-import' > /dev/null; then - _LT_TAGVAR(archive_cmds, $1)='$CC -shared -nostdlib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags -o $output_objdir/$soname $wl--enable-auto-image-base -Xlinker --out-implib -Xlinker $lib' - # If the export-symbols file already is a .def file, use it as - # is; otherwise, prepend EXPORTS... - _LT_TAGVAR(archive_expsym_cmds, $1)='if _LT_DLL_DEF_P([$export_symbols]); then - cp $export_symbols $output_objdir/$soname.def; - else - echo EXPORTS > $output_objdir/$soname.def; - cat $export_symbols >> $output_objdir/$soname.def; - fi~ - $CC -shared -nostdlib $output_objdir/$soname.def $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags -o $output_objdir/$soname $wl--enable-auto-image-base -Xlinker --out-implib -Xlinker $lib' - else - _LT_TAGVAR(ld_shlibs, $1)=no - fi - ;; - esac - ;; - darwin* | rhapsody*) - _LT_DARWIN_LINKER_FEATURES($1) - ;; - - os2*) - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-L$libdir' - _LT_TAGVAR(hardcode_minus_L, $1)=yes - _LT_TAGVAR(allow_undefined_flag, $1)=unsupported - shrext_cmds=.dll - _LT_TAGVAR(archive_cmds, $1)='$ECHO "LIBRARY ${soname%$shared_ext} INITINSTANCE TERMINSTANCE" > $output_objdir/$libname.def~ - $ECHO "DESCRIPTION \"$libname\"" >> $output_objdir/$libname.def~ - $ECHO "DATA MULTIPLE NONSHARED" >> $output_objdir/$libname.def~ - $ECHO EXPORTS >> $output_objdir/$libname.def~ - emxexp $libobjs | $SED /"_DLL_InitTerm"/d >> $output_objdir/$libname.def~ - $CC -Zdll -Zcrtdll -o $output_objdir/$soname $libobjs $deplibs $compiler_flags $output_objdir/$libname.def~ - emximp -o $lib $output_objdir/$libname.def' - _LT_TAGVAR(archive_expsym_cmds, $1)='$ECHO "LIBRARY ${soname%$shared_ext} INITINSTANCE TERMINSTANCE" > $output_objdir/$libname.def~ - $ECHO "DESCRIPTION \"$libname\"" >> $output_objdir/$libname.def~ - $ECHO "DATA MULTIPLE NONSHARED" >> $output_objdir/$libname.def~ - $ECHO EXPORTS >> $output_objdir/$libname.def~ - prefix_cmds="$SED"~ - if test EXPORTS = "`$SED 1q $export_symbols`"; then - prefix_cmds="$prefix_cmds -e 1d"; - fi~ - prefix_cmds="$prefix_cmds -e \"s/^\(.*\)$/_\1/g\""~ - cat $export_symbols | $prefix_cmds >> $output_objdir/$libname.def~ - $CC -Zdll -Zcrtdll -o $output_objdir/$soname $libobjs $deplibs $compiler_flags $output_objdir/$libname.def~ - emximp -o $lib $output_objdir/$libname.def' - _LT_TAGVAR(old_archive_From_new_cmds, $1)='emximp -o $output_objdir/${libname}_dll.a $output_objdir/$libname.def' - _LT_TAGVAR(enable_shared_with_static_runtimes, $1)=yes - ;; - - dgux*) - case $cc_basename in - ec++*) - # FIXME: insert proper C++ library support - _LT_TAGVAR(ld_shlibs, $1)=no - ;; - ghcx*) - # Green Hills C++ Compiler - # FIXME: insert proper C++ library support - _LT_TAGVAR(ld_shlibs, $1)=no - ;; - *) - # FIXME: insert proper C++ library support - _LT_TAGVAR(ld_shlibs, $1)=no - ;; - esac - ;; - - freebsd2.*) - # C++ shared libraries reported to be fairly broken before - # switch to ELF - _LT_TAGVAR(ld_shlibs, $1)=no - ;; - - freebsd-elf*) - _LT_TAGVAR(archive_cmds_need_lc, $1)=no - ;; - - freebsd* | dragonfly*) - # FreeBSD 3 and later use GNU C++ and GNU ld with standard ELF - # conventions - _LT_TAGVAR(ld_shlibs, $1)=yes - ;; - - haiku*) - _LT_TAGVAR(archive_cmds, $1)='$CC -shared $libobjs $deplibs $compiler_flags $wl-soname $wl$soname -o $lib' - _LT_TAGVAR(link_all_deplibs, $1)=yes - ;; - - hpux9*) - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl+b $wl$libdir' - _LT_TAGVAR(hardcode_libdir_separator, $1)=: - _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl-E' - _LT_TAGVAR(hardcode_direct, $1)=yes - _LT_TAGVAR(hardcode_minus_L, $1)=yes # Not in the search PATH, - # but as the default - # location of the library. - - case $cc_basename in - CC*) - # FIXME: insert proper C++ library support - _LT_TAGVAR(ld_shlibs, $1)=no - ;; - aCC*) - _LT_TAGVAR(archive_cmds, $1)='$RM $output_objdir/$soname~$CC -b $wl+b $wl$install_libdir -o $output_objdir/$soname $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags~test "x$output_objdir/$soname" = "x$lib" || mv $output_objdir/$soname $lib' - # Commands to make compiler produce verbose output that lists - # what "hidden" libraries, object files and flags are used when - # linking a shared library. - # - # There doesn't appear to be a way to prevent this compiler from - # explicitly linking system object files so we need to strip them - # from the output so that they don't get included in the library - # dependencies. - output_verbose_link_cmd='templist=`($CC -b $CFLAGS -v conftest.$objext 2>&1) | $EGREP "\-L"`; list= ; for z in $templist; do case $z in conftest.$objext) list="$list $z";; *.$objext);; *) list="$list $z";;esac; done; func_echo_all "$list"' - ;; - *) - if test yes = "$GXX"; then - _LT_TAGVAR(archive_cmds, $1)='$RM $output_objdir/$soname~$CC -shared -nostdlib $pic_flag $wl+b $wl$install_libdir -o $output_objdir/$soname $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags~test "x$output_objdir/$soname" = "x$lib" || mv $output_objdir/$soname $lib' - else - # FIXME: insert proper C++ library support - _LT_TAGVAR(ld_shlibs, $1)=no - fi - ;; - esac - ;; - - hpux10*|hpux11*) - if test no = "$with_gnu_ld"; then - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl+b $wl$libdir' - _LT_TAGVAR(hardcode_libdir_separator, $1)=: - - case $host_cpu in - hppa*64*|ia64*) - ;; - *) - _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl-E' - ;; - esac - fi - case $host_cpu in - hppa*64*|ia64*) - _LT_TAGVAR(hardcode_direct, $1)=no - _LT_TAGVAR(hardcode_shlibpath_var, $1)=no - ;; - *) - _LT_TAGVAR(hardcode_direct, $1)=yes - _LT_TAGVAR(hardcode_direct_absolute, $1)=yes - _LT_TAGVAR(hardcode_minus_L, $1)=yes # Not in the search PATH, - # but as the default - # location of the library. - ;; - esac - - case $cc_basename in - CC*) - # FIXME: insert proper C++ library support - _LT_TAGVAR(ld_shlibs, $1)=no - ;; - aCC*) - case $host_cpu in - hppa*64*) - _LT_TAGVAR(archive_cmds, $1)='$CC -b $wl+h $wl$soname -o $lib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags' - ;; - ia64*) - _LT_TAGVAR(archive_cmds, $1)='$CC -b $wl+h $wl$soname $wl+nodefaultrpath -o $lib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags' - ;; - *) - _LT_TAGVAR(archive_cmds, $1)='$CC -b $wl+h $wl$soname $wl+b $wl$install_libdir -o $lib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags' - ;; - esac - # Commands to make compiler produce verbose output that lists - # what "hidden" libraries, object files and flags are used when - # linking a shared library. - # - # There doesn't appear to be a way to prevent this compiler from - # explicitly linking system object files so we need to strip them - # from the output so that they don't get included in the library - # dependencies. - output_verbose_link_cmd='templist=`($CC -b $CFLAGS -v conftest.$objext 2>&1) | $GREP "\-L"`; list= ; for z in $templist; do case $z in conftest.$objext) list="$list $z";; *.$objext);; *) list="$list $z";;esac; done; func_echo_all "$list"' - ;; - *) - if test yes = "$GXX"; then - if test no = "$with_gnu_ld"; then - case $host_cpu in - hppa*64*) - _LT_TAGVAR(archive_cmds, $1)='$CC -shared -nostdlib -fPIC $wl+h $wl$soname -o $lib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags' - ;; - ia64*) - _LT_TAGVAR(archive_cmds, $1)='$CC -shared -nostdlib $pic_flag $wl+h $wl$soname $wl+nodefaultrpath -o $lib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags' - ;; - *) - _LT_TAGVAR(archive_cmds, $1)='$CC -shared -nostdlib $pic_flag $wl+h $wl$soname $wl+b $wl$install_libdir -o $lib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags' - ;; - esac - fi - else - # FIXME: insert proper C++ library support - _LT_TAGVAR(ld_shlibs, $1)=no - fi - ;; - esac - ;; - - interix[[3-9]]*) - _LT_TAGVAR(hardcode_direct, $1)=no - _LT_TAGVAR(hardcode_shlibpath_var, $1)=no - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath,$libdir' - _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl-E' - # Hack: On Interix 3.x, we cannot compile PIC because of a broken gcc. - # Instead, shared libraries are loaded at an image base (0x10000000 by - # default) and relocated if they conflict, which is a slow very memory - # consuming and fragmenting process. To avoid this, we pick a random, - # 256 KiB-aligned image base between 0x50000000 and 0x6FFC0000 at link - # time. Moving up from 0x10000000 also allows more sbrk(2) space. - _LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag $libobjs $deplibs $compiler_flags $wl-h,$soname $wl--image-base,`expr ${RANDOM-$$} % 4096 / 2 \* 262144 + 1342177280` -o $lib' - _LT_TAGVAR(archive_expsym_cmds, $1)='sed "s|^|_|" $export_symbols >$output_objdir/$soname.expsym~$CC -shared $pic_flag $libobjs $deplibs $compiler_flags $wl-h,$soname $wl--retain-symbols-file,$output_objdir/$soname.expsym $wl--image-base,`expr ${RANDOM-$$} % 4096 / 2 \* 262144 + 1342177280` -o $lib' - ;; - irix5* | irix6*) - case $cc_basename in - CC*) - # SGI C++ - _LT_TAGVAR(archive_cmds, $1)='$CC -shared -all -multigot $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags -soname $soname `test -n "$verstring" && func_echo_all "-set_version $verstring"` -update_registry $output_objdir/so_locations -o $lib' - - # Archives containing C++ object files must be created using - # "CC -ar", where "CC" is the IRIX C++ compiler. This is - # necessary to make sure instantiated templates are included - # in the archive. - _LT_TAGVAR(old_archive_cmds, $1)='$CC -ar -WR,-u -o $oldlib $oldobjs' - ;; - *) - if test yes = "$GXX"; then - if test no = "$with_gnu_ld"; then - _LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag -nostdlib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-soname $wl$soname `test -n "$verstring" && func_echo_all "$wl-set_version $wl$verstring"` $wl-update_registry $wl$output_objdir/so_locations -o $lib' - else - _LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag -nostdlib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-soname $wl$soname `test -n "$verstring" && func_echo_all "$wl-set_version $wl$verstring"` -o $lib' - fi - fi - _LT_TAGVAR(link_all_deplibs, $1)=yes - ;; - esac - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath $wl$libdir' - _LT_TAGVAR(hardcode_libdir_separator, $1)=: - _LT_TAGVAR(inherit_rpath, $1)=yes - ;; - - linux* | k*bsd*-gnu | kopensolaris*-gnu | gnu*) - case $cc_basename in - KCC*) - # Kuck and Associates, Inc. (KAI) C++ Compiler - - # KCC will only create a shared library if the output file - # ends with ".so" (or ".sl" for HP-UX), so rename the library - # to its proper name (with version) after linking. - _LT_TAGVAR(archive_cmds, $1)='tempext=`echo $shared_ext | $SED -e '\''s/\([[^()0-9A-Za-z{}]]\)/\\\\\1/g'\''`; templib=`echo $lib | $SED -e "s/\$tempext\..*/.so/"`; $CC $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags --soname $soname -o \$templib; mv \$templib $lib' - _LT_TAGVAR(archive_expsym_cmds, $1)='tempext=`echo $shared_ext | $SED -e '\''s/\([[^()0-9A-Za-z{}]]\)/\\\\\1/g'\''`; templib=`echo $lib | $SED -e "s/\$tempext\..*/.so/"`; $CC $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags --soname $soname -o \$templib $wl-retain-symbols-file,$export_symbols; mv \$templib $lib' - # Commands to make compiler produce verbose output that lists - # what "hidden" libraries, object files and flags are used when - # linking a shared library. - # - # There doesn't appear to be a way to prevent this compiler from - # explicitly linking system object files so we need to strip them - # from the output so that they don't get included in the library - # dependencies. - output_verbose_link_cmd='templist=`$CC $CFLAGS -v conftest.$objext -o libconftest$shared_ext 2>&1 | $GREP "ld"`; rm -f libconftest$shared_ext; list= ; for z in $templist; do case $z in conftest.$objext) list="$list $z";; *.$objext);; *) list="$list $z";;esac; done; func_echo_all "$list"' - - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath,$libdir' - _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl--export-dynamic' - - # Archives containing C++ object files must be created using - # "CC -Bstatic", where "CC" is the KAI C++ compiler. - _LT_TAGVAR(old_archive_cmds, $1)='$CC -Bstatic -o $oldlib $oldobjs' - ;; - icpc* | ecpc* ) - # Intel C++ - with_gnu_ld=yes - # version 8.0 and above of icpc choke on multiply defined symbols - # if we add $predep_objects and $postdep_objects, however 7.1 and - # earlier do not add the objects themselves. - case `$CC -V 2>&1` in - *"Version 7."*) - _LT_TAGVAR(archive_cmds, $1)='$CC -shared $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-soname $wl$soname -o $lib' - _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-soname $wl$soname $wl-retain-symbols-file $wl$export_symbols -o $lib' - ;; - *) # Version 8.0 or newer - tmp_idyn= - case $host_cpu in - ia64*) tmp_idyn=' -i_dynamic';; - esac - _LT_TAGVAR(archive_cmds, $1)='$CC -shared'"$tmp_idyn"' $libobjs $deplibs $compiler_flags $wl-soname $wl$soname -o $lib' - _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -shared'"$tmp_idyn"' $libobjs $deplibs $compiler_flags $wl-soname $wl$soname $wl-retain-symbols-file $wl$export_symbols -o $lib' - ;; - esac - _LT_TAGVAR(archive_cmds_need_lc, $1)=no - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath,$libdir' - _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl--export-dynamic' - _LT_TAGVAR(whole_archive_flag_spec, $1)='$wl--whole-archive$convenience $wl--no-whole-archive' - ;; - pgCC* | pgcpp*) - # Portland Group C++ compiler - case `$CC -V` in - *pgCC\ [[1-5]].* | *pgcpp\ [[1-5]].*) - _LT_TAGVAR(prelink_cmds, $1)='tpldir=Template.dir~ - rm -rf $tpldir~ - $CC --prelink_objects --instantiation_dir $tpldir $objs $libobjs $compile_deplibs~ - compile_command="$compile_command `find $tpldir -name \*.o | sort | $NL2SP`"' - _LT_TAGVAR(old_archive_cmds, $1)='tpldir=Template.dir~ - rm -rf $tpldir~ - $CC --prelink_objects --instantiation_dir $tpldir $oldobjs$old_deplibs~ - $AR $AR_FLAGS $oldlib$oldobjs$old_deplibs `find $tpldir -name \*.o | sort | $NL2SP`~ - $RANLIB $oldlib' - _LT_TAGVAR(archive_cmds, $1)='tpldir=Template.dir~ - rm -rf $tpldir~ - $CC --prelink_objects --instantiation_dir $tpldir $predep_objects $libobjs $deplibs $convenience $postdep_objects~ - $CC -shared $pic_flag $predep_objects $libobjs $deplibs `find $tpldir -name \*.o | sort | $NL2SP` $postdep_objects $compiler_flags $wl-soname $wl$soname -o $lib' - _LT_TAGVAR(archive_expsym_cmds, $1)='tpldir=Template.dir~ - rm -rf $tpldir~ - $CC --prelink_objects --instantiation_dir $tpldir $predep_objects $libobjs $deplibs $convenience $postdep_objects~ - $CC -shared $pic_flag $predep_objects $libobjs $deplibs `find $tpldir -name \*.o | sort | $NL2SP` $postdep_objects $compiler_flags $wl-soname $wl$soname $wl-retain-symbols-file $wl$export_symbols -o $lib' - ;; - *) # Version 6 and above use weak symbols - _LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-soname $wl$soname -o $lib' - _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $pic_flag $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-soname $wl$soname $wl-retain-symbols-file $wl$export_symbols -o $lib' - ;; - esac - - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl--rpath $wl$libdir' - _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl--export-dynamic' - _LT_TAGVAR(whole_archive_flag_spec, $1)='$wl--whole-archive`for conv in $convenience\"\"; do test -n \"$conv\" && new_convenience=\"$new_convenience,$conv\"; done; func_echo_all \"$new_convenience\"` $wl--no-whole-archive' - ;; - cxx*) - # Compaq C++ - _LT_TAGVAR(archive_cmds, $1)='$CC -shared $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-soname $wl$soname -o $lib' - _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-soname $wl$soname -o $lib $wl-retain-symbols-file $wl$export_symbols' - - runpath_var=LD_RUN_PATH - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-rpath $libdir' - _LT_TAGVAR(hardcode_libdir_separator, $1)=: - - # Commands to make compiler produce verbose output that lists - # what "hidden" libraries, object files and flags are used when - # linking a shared library. - # - # There doesn't appear to be a way to prevent this compiler from - # explicitly linking system object files so we need to strip them - # from the output so that they don't get included in the library - # dependencies. - output_verbose_link_cmd='templist=`$CC -shared $CFLAGS -v conftest.$objext 2>&1 | $GREP "ld"`; templist=`func_echo_all "$templist" | $SED "s/\(^.*ld.*\)\( .*ld .*$\)/\1/"`; list= ; for z in $templist; do case $z in conftest.$objext) list="$list $z";; *.$objext);; *) list="$list $z";;esac; done; func_echo_all "X$list" | $Xsed' - ;; - xl* | mpixl* | bgxl*) - # IBM XL 8.0 on PPC, with GNU ld - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath $wl$libdir' - _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl--export-dynamic' - _LT_TAGVAR(archive_cmds, $1)='$CC -qmkshrobj $libobjs $deplibs $compiler_flags $wl-soname $wl$soname -o $lib' - if test yes = "$supports_anon_versioning"; then - _LT_TAGVAR(archive_expsym_cmds, $1)='echo "{ global:" > $output_objdir/$libname.ver~ - cat $export_symbols | sed -e "s/\(.*\)/\1;/" >> $output_objdir/$libname.ver~ - echo "local: *; };" >> $output_objdir/$libname.ver~ - $CC -qmkshrobj $libobjs $deplibs $compiler_flags $wl-soname $wl$soname $wl-version-script $wl$output_objdir/$libname.ver -o $lib' - fi - ;; - *) - case `$CC -V 2>&1 | sed 5q` in - *Sun\ C*) - # Sun C++ 5.9 - _LT_TAGVAR(no_undefined_flag, $1)=' -zdefs' - _LT_TAGVAR(archive_cmds, $1)='$CC -G$allow_undefined_flag -h$soname -o $lib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags' - _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -G$allow_undefined_flag -h$soname -o $lib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-retain-symbols-file $wl$export_symbols' - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-R$libdir' - _LT_TAGVAR(whole_archive_flag_spec, $1)='$wl--whole-archive`new_convenience=; for conv in $convenience\"\"; do test -z \"$conv\" || new_convenience=\"$new_convenience,$conv\"; done; func_echo_all \"$new_convenience\"` $wl--no-whole-archive' - _LT_TAGVAR(compiler_needs_object, $1)=yes - - # Not sure whether something based on - # $CC $CFLAGS -v conftest.$objext -o libconftest$shared_ext 2>&1 - # would be better. - output_verbose_link_cmd='func_echo_all' - - # Archives containing C++ object files must be created using - # "CC -xar", where "CC" is the Sun C++ compiler. This is - # necessary to make sure instantiated templates are included - # in the archive. - _LT_TAGVAR(old_archive_cmds, $1)='$CC -xar -o $oldlib $oldobjs' - ;; - esac - ;; - esac - ;; - - lynxos*) - # FIXME: insert proper C++ library support - _LT_TAGVAR(ld_shlibs, $1)=no - ;; - - m88k*) - # FIXME: insert proper C++ library support - _LT_TAGVAR(ld_shlibs, $1)=no - ;; - - mvs*) - case $cc_basename in - cxx*) - # FIXME: insert proper C++ library support - _LT_TAGVAR(ld_shlibs, $1)=no - ;; - *) - # FIXME: insert proper C++ library support - _LT_TAGVAR(ld_shlibs, $1)=no - ;; - esac - ;; - - netbsd*) - if echo __ELF__ | $CC -E - | $GREP __ELF__ >/dev/null; then - _LT_TAGVAR(archive_cmds, $1)='$LD -Bshareable -o $lib $predep_objects $libobjs $deplibs $postdep_objects $linker_flags' - wlarc= - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-R$libdir' - _LT_TAGVAR(hardcode_direct, $1)=yes - _LT_TAGVAR(hardcode_shlibpath_var, $1)=no - fi - # Workaround some broken pre-1.5 toolchains - output_verbose_link_cmd='$CC -shared $CFLAGS -v conftest.$objext 2>&1 | $GREP conftest.$objext | $SED -e "s:-lgcc -lc -lgcc::"' - ;; - - *nto* | *qnx*) - _LT_TAGVAR(ld_shlibs, $1)=yes - ;; - - openbsd* | bitrig*) - if test -f /usr/libexec/ld.so; then - _LT_TAGVAR(hardcode_direct, $1)=yes - _LT_TAGVAR(hardcode_shlibpath_var, $1)=no - _LT_TAGVAR(hardcode_direct_absolute, $1)=yes - _LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags -o $lib' - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath,$libdir' - if test -z "`echo __ELF__ | $CC -E - | grep __ELF__`"; then - _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $pic_flag $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-retain-symbols-file,$export_symbols -o $lib' - _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl-E' - _LT_TAGVAR(whole_archive_flag_spec, $1)=$wlarc'--whole-archive$convenience '$wlarc'--no-whole-archive' - fi - output_verbose_link_cmd=func_echo_all - else - _LT_TAGVAR(ld_shlibs, $1)=no - fi - ;; - - osf3* | osf4* | osf5*) - case $cc_basename in - KCC*) - # Kuck and Associates, Inc. (KAI) C++ Compiler - - # KCC will only create a shared library if the output file - # ends with ".so" (or ".sl" for HP-UX), so rename the library - # to its proper name (with version) after linking. - _LT_TAGVAR(archive_cmds, $1)='tempext=`echo $shared_ext | $SED -e '\''s/\([[^()0-9A-Za-z{}]]\)/\\\\\1/g'\''`; templib=`echo "$lib" | $SED -e "s/\$tempext\..*/.so/"`; $CC $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags --soname $soname -o \$templib; mv \$templib $lib' - - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath,$libdir' - _LT_TAGVAR(hardcode_libdir_separator, $1)=: - - # Archives containing C++ object files must be created using - # the KAI C++ compiler. - case $host in - osf3*) _LT_TAGVAR(old_archive_cmds, $1)='$CC -Bstatic -o $oldlib $oldobjs' ;; - *) _LT_TAGVAR(old_archive_cmds, $1)='$CC -o $oldlib $oldobjs' ;; - esac - ;; - RCC*) - # Rational C++ 2.4.1 - # FIXME: insert proper C++ library support - _LT_TAGVAR(ld_shlibs, $1)=no - ;; - cxx*) - case $host in - osf3*) - _LT_TAGVAR(allow_undefined_flag, $1)=' $wl-expect_unresolved $wl\*' - _LT_TAGVAR(archive_cmds, $1)='$CC -shared$allow_undefined_flag $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-soname $soname `test -n "$verstring" && func_echo_all "$wl-set_version $verstring"` -update_registry $output_objdir/so_locations -o $lib' - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath $wl$libdir' - ;; - *) - _LT_TAGVAR(allow_undefined_flag, $1)=' -expect_unresolved \*' - _LT_TAGVAR(archive_cmds, $1)='$CC -shared$allow_undefined_flag $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags -msym -soname $soname `test -n "$verstring" && func_echo_all "-set_version $verstring"` -update_registry $output_objdir/so_locations -o $lib' - _LT_TAGVAR(archive_expsym_cmds, $1)='for i in `cat $export_symbols`; do printf "%s %s\\n" -exported_symbol "\$i" >> $lib.exp; done~ - echo "-hidden">> $lib.exp~ - $CC -shared$allow_undefined_flag $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags -msym -soname $soname $wl-input $wl$lib.exp `test -n "$verstring" && $ECHO "-set_version $verstring"` -update_registry $output_objdir/so_locations -o $lib~ - $RM $lib.exp' - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-rpath $libdir' - ;; - esac - - _LT_TAGVAR(hardcode_libdir_separator, $1)=: - - # Commands to make compiler produce verbose output that lists - # what "hidden" libraries, object files and flags are used when - # linking a shared library. - # - # There doesn't appear to be a way to prevent this compiler from - # explicitly linking system object files so we need to strip them - # from the output so that they don't get included in the library - # dependencies. - output_verbose_link_cmd='templist=`$CC -shared $CFLAGS -v conftest.$objext 2>&1 | $GREP "ld" | $GREP -v "ld:"`; templist=`func_echo_all "$templist" | $SED "s/\(^.*ld.*\)\( .*ld.*$\)/\1/"`; list= ; for z in $templist; do case $z in conftest.$objext) list="$list $z";; *.$objext);; *) list="$list $z";;esac; done; func_echo_all "$list"' - ;; - *) - if test yes,no = "$GXX,$with_gnu_ld"; then - _LT_TAGVAR(allow_undefined_flag, $1)=' $wl-expect_unresolved $wl\*' - case $host in - osf3*) - _LT_TAGVAR(archive_cmds, $1)='$CC -shared -nostdlib $allow_undefined_flag $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-soname $wl$soname `test -n "$verstring" && func_echo_all "$wl-set_version $wl$verstring"` $wl-update_registry $wl$output_objdir/so_locations -o $lib' - ;; - *) - _LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag -nostdlib $allow_undefined_flag $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-msym $wl-soname $wl$soname `test -n "$verstring" && func_echo_all "$wl-set_version $wl$verstring"` $wl-update_registry $wl$output_objdir/so_locations -o $lib' - ;; - esac - - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath $wl$libdir' - _LT_TAGVAR(hardcode_libdir_separator, $1)=: - - # Commands to make compiler produce verbose output that lists - # what "hidden" libraries, object files and flags are used when - # linking a shared library. - output_verbose_link_cmd='$CC -shared $CFLAGS -v conftest.$objext 2>&1 | $GREP -v "^Configured with:" | $GREP "\-L"' - - else - # FIXME: insert proper C++ library support - _LT_TAGVAR(ld_shlibs, $1)=no - fi - ;; - esac - ;; - - psos*) - # FIXME: insert proper C++ library support - _LT_TAGVAR(ld_shlibs, $1)=no - ;; - - sunos4*) - case $cc_basename in - CC*) - # Sun C++ 4.x - # FIXME: insert proper C++ library support - _LT_TAGVAR(ld_shlibs, $1)=no - ;; - lcc*) - # Lucid - # FIXME: insert proper C++ library support - _LT_TAGVAR(ld_shlibs, $1)=no - ;; - *) - # FIXME: insert proper C++ library support - _LT_TAGVAR(ld_shlibs, $1)=no - ;; - esac - ;; - - solaris*) - case $cc_basename in - CC* | sunCC*) - # Sun C++ 4.2, 5.x and Centerline C++ - _LT_TAGVAR(archive_cmds_need_lc,$1)=yes - _LT_TAGVAR(no_undefined_flag, $1)=' -zdefs' - _LT_TAGVAR(archive_cmds, $1)='$CC -G$allow_undefined_flag -h$soname -o $lib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags' - _LT_TAGVAR(archive_expsym_cmds, $1)='echo "{ global:" > $lib.exp~cat $export_symbols | $SED -e "s/\(.*\)/\1;/" >> $lib.exp~echo "local: *; };" >> $lib.exp~ - $CC -G$allow_undefined_flag $wl-M $wl$lib.exp -h$soname -o $lib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags~$RM $lib.exp' - - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-R$libdir' - _LT_TAGVAR(hardcode_shlibpath_var, $1)=no - case $host_os in - solaris2.[[0-5]] | solaris2.[[0-5]].*) ;; - *) - # The compiler driver will combine and reorder linker options, - # but understands '-z linker_flag'. - # Supported since Solaris 2.6 (maybe 2.5.1?) - _LT_TAGVAR(whole_archive_flag_spec, $1)='-z allextract$convenience -z defaultextract' - ;; - esac - _LT_TAGVAR(link_all_deplibs, $1)=yes - - output_verbose_link_cmd='func_echo_all' - - # Archives containing C++ object files must be created using - # "CC -xar", where "CC" is the Sun C++ compiler. This is - # necessary to make sure instantiated templates are included - # in the archive. - _LT_TAGVAR(old_archive_cmds, $1)='$CC -xar -o $oldlib $oldobjs' - ;; - gcx*) - # Green Hills C++ Compiler - _LT_TAGVAR(archive_cmds, $1)='$CC -shared $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-h $wl$soname -o $lib' - - # The C++ compiler must be used to create the archive. - _LT_TAGVAR(old_archive_cmds, $1)='$CC $LDFLAGS -archive -o $oldlib $oldobjs' - ;; - *) - # GNU C++ compiler with Solaris linker - if test yes,no = "$GXX,$with_gnu_ld"; then - _LT_TAGVAR(no_undefined_flag, $1)=' $wl-z ${wl}defs' - if $CC --version | $GREP -v '^2\.7' > /dev/null; then - _LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag -nostdlib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-h $wl$soname -o $lib' - _LT_TAGVAR(archive_expsym_cmds, $1)='echo "{ global:" > $lib.exp~cat $export_symbols | $SED -e "s/\(.*\)/\1;/" >> $lib.exp~echo "local: *; };" >> $lib.exp~ - $CC -shared $pic_flag -nostdlib $wl-M $wl$lib.exp $wl-h $wl$soname -o $lib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags~$RM $lib.exp' - - # Commands to make compiler produce verbose output that lists - # what "hidden" libraries, object files and flags are used when - # linking a shared library. - output_verbose_link_cmd='$CC -shared $CFLAGS -v conftest.$objext 2>&1 | $GREP -v "^Configured with:" | $GREP "\-L"' - else - # g++ 2.7 appears to require '-G' NOT '-shared' on this - # platform. - _LT_TAGVAR(archive_cmds, $1)='$CC -G -nostdlib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-h $wl$soname -o $lib' - _LT_TAGVAR(archive_expsym_cmds, $1)='echo "{ global:" > $lib.exp~cat $export_symbols | $SED -e "s/\(.*\)/\1;/" >> $lib.exp~echo "local: *; };" >> $lib.exp~ - $CC -G -nostdlib $wl-M $wl$lib.exp $wl-h $wl$soname -o $lib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags~$RM $lib.exp' - - # Commands to make compiler produce verbose output that lists - # what "hidden" libraries, object files and flags are used when - # linking a shared library. - output_verbose_link_cmd='$CC -G $CFLAGS -v conftest.$objext 2>&1 | $GREP -v "^Configured with:" | $GREP "\-L"' - fi - - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-R $wl$libdir' - case $host_os in - solaris2.[[0-5]] | solaris2.[[0-5]].*) ;; - *) - _LT_TAGVAR(whole_archive_flag_spec, $1)='$wl-z ${wl}allextract$convenience $wl-z ${wl}defaultextract' - ;; - esac - fi - ;; - esac - ;; - - sysv4*uw2* | sysv5OpenUNIX* | sysv5UnixWare7.[[01]].[[10]]* | unixware7* | sco3.2v5.0.[[024]]*) - _LT_TAGVAR(no_undefined_flag, $1)='$wl-z,text' - _LT_TAGVAR(archive_cmds_need_lc, $1)=no - _LT_TAGVAR(hardcode_shlibpath_var, $1)=no - runpath_var='LD_RUN_PATH' - - case $cc_basename in - CC*) - _LT_TAGVAR(archive_cmds, $1)='$CC -G $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags' - _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -G $wl-Bexport:$export_symbols $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags' - ;; - *) - _LT_TAGVAR(archive_cmds, $1)='$CC -shared $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags' - _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $wl-Bexport:$export_symbols $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags' - ;; - esac - ;; - - sysv5* | sco3.2v5* | sco5v6*) - # Note: We CANNOT use -z defs as we might desire, because we do not - # link with -lc, and that would cause any symbols used from libc to - # always be unresolved, which means just about no library would - # ever link correctly. If we're not using GNU ld we use -z text - # though, which does catch some bad symbols but isn't as heavy-handed - # as -z defs. - _LT_TAGVAR(no_undefined_flag, $1)='$wl-z,text' - _LT_TAGVAR(allow_undefined_flag, $1)='$wl-z,nodefs' - _LT_TAGVAR(archive_cmds_need_lc, $1)=no - _LT_TAGVAR(hardcode_shlibpath_var, $1)=no - _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-R,$libdir' - _LT_TAGVAR(hardcode_libdir_separator, $1)=':' - _LT_TAGVAR(link_all_deplibs, $1)=yes - _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl-Bexport' - runpath_var='LD_RUN_PATH' - - case $cc_basename in - CC*) - _LT_TAGVAR(archive_cmds, $1)='$CC -G $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags' - _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -G $wl-Bexport:$export_symbols $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags' - _LT_TAGVAR(old_archive_cmds, $1)='$CC -Tprelink_objects $oldobjs~ - '"$_LT_TAGVAR(old_archive_cmds, $1)" - _LT_TAGVAR(reload_cmds, $1)='$CC -Tprelink_objects $reload_objs~ - '"$_LT_TAGVAR(reload_cmds, $1)" - ;; - *) - _LT_TAGVAR(archive_cmds, $1)='$CC -shared $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags' - _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $wl-Bexport:$export_symbols $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags' - ;; - esac - ;; - - tandem*) - case $cc_basename in - NCC*) - # NonStop-UX NCC 3.20 - # FIXME: insert proper C++ library support - _LT_TAGVAR(ld_shlibs, $1)=no - ;; - *) - # FIXME: insert proper C++ library support - _LT_TAGVAR(ld_shlibs, $1)=no - ;; - esac - ;; - - vxworks*) - # FIXME: insert proper C++ library support - _LT_TAGVAR(ld_shlibs, $1)=no - ;; - - *) - # FIXME: insert proper C++ library support - _LT_TAGVAR(ld_shlibs, $1)=no - ;; - esac - - AC_MSG_RESULT([$_LT_TAGVAR(ld_shlibs, $1)]) - test no = "$_LT_TAGVAR(ld_shlibs, $1)" && can_build_shared=no - - _LT_TAGVAR(GCC, $1)=$GXX - _LT_TAGVAR(LD, $1)=$LD - - ## CAVEAT EMPTOR: - ## There is no encapsulation within the following macros, do not change - ## the running order or otherwise move them around unless you know exactly - ## what you are doing... - _LT_SYS_HIDDEN_LIBDEPS($1) - _LT_COMPILER_PIC($1) - _LT_COMPILER_C_O($1) - _LT_COMPILER_FILE_LOCKS($1) - _LT_LINKER_SHLIBS($1) - _LT_SYS_DYNAMIC_LINKER($1) - _LT_LINKER_HARDCODE_LIBPATH($1) - - _LT_CONFIG($1) - fi # test -n "$compiler" - - CC=$lt_save_CC - CFLAGS=$lt_save_CFLAGS - LDCXX=$LD - LD=$lt_save_LD - GCC=$lt_save_GCC - with_gnu_ld=$lt_save_with_gnu_ld - lt_cv_path_LDCXX=$lt_cv_path_LD - lt_cv_path_LD=$lt_save_path_LD - lt_cv_prog_gnu_ldcxx=$lt_cv_prog_gnu_ld - lt_cv_prog_gnu_ld=$lt_save_with_gnu_ld -fi # test yes != "$_lt_caught_CXX_error" - -AC_LANG_POP -])# _LT_LANG_CXX_CONFIG - - -# _LT_FUNC_STRIPNAME_CNF -# ---------------------- -# func_stripname_cnf prefix suffix name -# strip PREFIX and SUFFIX off of NAME. -# PREFIX and SUFFIX must not contain globbing or regex special -# characters, hashes, percent signs, but SUFFIX may contain a leading -# dot (in which case that matches only a dot). -# -# This function is identical to the (non-XSI) version of func_stripname, -# except this one can be used by m4 code that may be executed by configure, -# rather than the libtool script. -m4_defun([_LT_FUNC_STRIPNAME_CNF],[dnl -AC_REQUIRE([_LT_DECL_SED]) -AC_REQUIRE([_LT_PROG_ECHO_BACKSLASH]) -func_stripname_cnf () -{ - case @S|@2 in - .*) func_stripname_result=`$ECHO "@S|@3" | $SED "s%^@S|@1%%; s%\\\\@S|@2\$%%"`;; - *) func_stripname_result=`$ECHO "@S|@3" | $SED "s%^@S|@1%%; s%@S|@2\$%%"`;; - esac -} # func_stripname_cnf -])# _LT_FUNC_STRIPNAME_CNF - - -# _LT_SYS_HIDDEN_LIBDEPS([TAGNAME]) -# --------------------------------- -# Figure out "hidden" library dependencies from verbose -# compiler output when linking a shared library. -# Parse the compiler output and extract the necessary -# objects, libraries and library flags. -m4_defun([_LT_SYS_HIDDEN_LIBDEPS], -[m4_require([_LT_FILEUTILS_DEFAULTS])dnl -AC_REQUIRE([_LT_FUNC_STRIPNAME_CNF])dnl -# Dependencies to place before and after the object being linked: -_LT_TAGVAR(predep_objects, $1)= -_LT_TAGVAR(postdep_objects, $1)= -_LT_TAGVAR(predeps, $1)= -_LT_TAGVAR(postdeps, $1)= -_LT_TAGVAR(compiler_lib_search_path, $1)= - -dnl we can't use the lt_simple_compile_test_code here, -dnl because it contains code intended for an executable, -dnl not a library. It's possible we should let each -dnl tag define a new lt_????_link_test_code variable, -dnl but it's only used here... -m4_if([$1], [], [cat > conftest.$ac_ext <<_LT_EOF -int a; -void foo (void) { a = 0; } -_LT_EOF -], [$1], [CXX], [cat > conftest.$ac_ext <<_LT_EOF -class Foo -{ -public: - Foo (void) { a = 0; } -private: - int a; -}; -_LT_EOF -], [$1], [F77], [cat > conftest.$ac_ext <<_LT_EOF - subroutine foo - implicit none - integer*4 a - a=0 - return - end -_LT_EOF -], [$1], [FC], [cat > conftest.$ac_ext <<_LT_EOF - subroutine foo - implicit none - integer a - a=0 - return - end -_LT_EOF -], [$1], [GCJ], [cat > conftest.$ac_ext <<_LT_EOF -public class foo { - private int a; - public void bar (void) { - a = 0; - } -}; -_LT_EOF -], [$1], [GO], [cat > conftest.$ac_ext <<_LT_EOF -package foo -func foo() { -} -_LT_EOF -]) - -_lt_libdeps_save_CFLAGS=$CFLAGS -case "$CC $CFLAGS " in #( -*\ -flto*\ *) CFLAGS="$CFLAGS -fno-lto" ;; -*\ -fwhopr*\ *) CFLAGS="$CFLAGS -fno-whopr" ;; -*\ -fuse-linker-plugin*\ *) CFLAGS="$CFLAGS -fno-use-linker-plugin" ;; -esac - -dnl Parse the compiler output and extract the necessary -dnl objects, libraries and library flags. -if AC_TRY_EVAL(ac_compile); then - # Parse the compiler output and extract the necessary - # objects, libraries and library flags. - - # Sentinel used to keep track of whether or not we are before - # the conftest object file. - pre_test_object_deps_done=no - - for p in `eval "$output_verbose_link_cmd"`; do - case $prev$p in - - -L* | -R* | -l*) - # Some compilers place space between "-{L,R}" and the path. - # Remove the space. - if test x-L = "$p" || - test x-R = "$p"; then - prev=$p - continue - fi - - # Expand the sysroot to ease extracting the directories later. - if test -z "$prev"; then - case $p in - -L*) func_stripname_cnf '-L' '' "$p"; prev=-L; p=$func_stripname_result ;; - -R*) func_stripname_cnf '-R' '' "$p"; prev=-R; p=$func_stripname_result ;; - -l*) func_stripname_cnf '-l' '' "$p"; prev=-l; p=$func_stripname_result ;; - esac - fi - case $p in - =*) func_stripname_cnf '=' '' "$p"; p=$lt_sysroot$func_stripname_result ;; - esac - if test no = "$pre_test_object_deps_done"; then - case $prev in - -L | -R) - # Internal compiler library paths should come after those - # provided the user. The postdeps already come after the - # user supplied libs so there is no need to process them. - if test -z "$_LT_TAGVAR(compiler_lib_search_path, $1)"; then - _LT_TAGVAR(compiler_lib_search_path, $1)=$prev$p - else - _LT_TAGVAR(compiler_lib_search_path, $1)="${_LT_TAGVAR(compiler_lib_search_path, $1)} $prev$p" - fi - ;; - # The "-l" case would never come before the object being - # linked, so don't bother handling this case. - esac - else - if test -z "$_LT_TAGVAR(postdeps, $1)"; then - _LT_TAGVAR(postdeps, $1)=$prev$p - else - _LT_TAGVAR(postdeps, $1)="${_LT_TAGVAR(postdeps, $1)} $prev$p" - fi - fi - prev= - ;; - - *.lto.$objext) ;; # Ignore GCC LTO objects - *.$objext) - # This assumes that the test object file only shows up - # once in the compiler output. - if test "$p" = "conftest.$objext"; then - pre_test_object_deps_done=yes - continue - fi - - if test no = "$pre_test_object_deps_done"; then - if test -z "$_LT_TAGVAR(predep_objects, $1)"; then - _LT_TAGVAR(predep_objects, $1)=$p - else - _LT_TAGVAR(predep_objects, $1)="$_LT_TAGVAR(predep_objects, $1) $p" - fi - else - if test -z "$_LT_TAGVAR(postdep_objects, $1)"; then - _LT_TAGVAR(postdep_objects, $1)=$p - else - _LT_TAGVAR(postdep_objects, $1)="$_LT_TAGVAR(postdep_objects, $1) $p" - fi - fi - ;; - - *) ;; # Ignore the rest. - - esac - done - - # Clean up. - rm -f a.out a.exe -else - echo "libtool.m4: error: problem compiling $1 test program" -fi - -$RM -f confest.$objext -CFLAGS=$_lt_libdeps_save_CFLAGS - -# PORTME: override above test on systems where it is broken -m4_if([$1], [CXX], -[case $host_os in -interix[[3-9]]*) - # Interix 3.5 installs completely hosed .la files for C++, so rather than - # hack all around it, let's just trust "g++" to DTRT. - _LT_TAGVAR(predep_objects,$1)= - _LT_TAGVAR(postdep_objects,$1)= - _LT_TAGVAR(postdeps,$1)= - ;; -esac -]) - -case " $_LT_TAGVAR(postdeps, $1) " in -*" -lc "*) _LT_TAGVAR(archive_cmds_need_lc, $1)=no ;; -esac - _LT_TAGVAR(compiler_lib_search_dirs, $1)= -if test -n "${_LT_TAGVAR(compiler_lib_search_path, $1)}"; then - _LT_TAGVAR(compiler_lib_search_dirs, $1)=`echo " ${_LT_TAGVAR(compiler_lib_search_path, $1)}" | $SED -e 's! -L! !g' -e 's!^ !!'` -fi -_LT_TAGDECL([], [compiler_lib_search_dirs], [1], - [The directories searched by this compiler when creating a shared library]) -_LT_TAGDECL([], [predep_objects], [1], - [Dependencies to place before and after the objects being linked to - create a shared library]) -_LT_TAGDECL([], [postdep_objects], [1]) -_LT_TAGDECL([], [predeps], [1]) -_LT_TAGDECL([], [postdeps], [1]) -_LT_TAGDECL([], [compiler_lib_search_path], [1], - [The library search path used internally by the compiler when linking - a shared library]) -])# _LT_SYS_HIDDEN_LIBDEPS - - -# _LT_LANG_F77_CONFIG([TAG]) -# -------------------------- -# Ensure that the configuration variables for a Fortran 77 compiler are -# suitably defined. These variables are subsequently used by _LT_CONFIG -# to write the compiler configuration to 'libtool'. -m4_defun([_LT_LANG_F77_CONFIG], -[AC_LANG_PUSH(Fortran 77) -if test -z "$F77" || test no = "$F77"; then - _lt_disable_F77=yes -fi - -_LT_TAGVAR(archive_cmds_need_lc, $1)=no -_LT_TAGVAR(allow_undefined_flag, $1)= -_LT_TAGVAR(always_export_symbols, $1)=no -_LT_TAGVAR(archive_expsym_cmds, $1)= -_LT_TAGVAR(export_dynamic_flag_spec, $1)= -_LT_TAGVAR(hardcode_direct, $1)=no -_LT_TAGVAR(hardcode_direct_absolute, $1)=no -_LT_TAGVAR(hardcode_libdir_flag_spec, $1)= -_LT_TAGVAR(hardcode_libdir_separator, $1)= -_LT_TAGVAR(hardcode_minus_L, $1)=no -_LT_TAGVAR(hardcode_automatic, $1)=no -_LT_TAGVAR(inherit_rpath, $1)=no -_LT_TAGVAR(module_cmds, $1)= -_LT_TAGVAR(module_expsym_cmds, $1)= -_LT_TAGVAR(link_all_deplibs, $1)=unknown -_LT_TAGVAR(old_archive_cmds, $1)=$old_archive_cmds -_LT_TAGVAR(reload_flag, $1)=$reload_flag -_LT_TAGVAR(reload_cmds, $1)=$reload_cmds -_LT_TAGVAR(no_undefined_flag, $1)= -_LT_TAGVAR(whole_archive_flag_spec, $1)= -_LT_TAGVAR(enable_shared_with_static_runtimes, $1)=no - -# Source file extension for f77 test sources. -ac_ext=f - -# Object file extension for compiled f77 test sources. -objext=o -_LT_TAGVAR(objext, $1)=$objext - -# No sense in running all these tests if we already determined that -# the F77 compiler isn't working. Some variables (like enable_shared) -# are currently assumed to apply to all compilers on this platform, -# and will be corrupted by setting them based on a non-working compiler. -if test yes != "$_lt_disable_F77"; then - # Code to be used in simple compile tests - lt_simple_compile_test_code="\ - subroutine t - return - end -" - - # Code to be used in simple link tests - lt_simple_link_test_code="\ - program t - end -" - - # ltmain only uses $CC for tagged configurations so make sure $CC is set. - _LT_TAG_COMPILER - - # save warnings/boilerplate of simple test code - _LT_COMPILER_BOILERPLATE - _LT_LINKER_BOILERPLATE - - # Allow CC to be a program name with arguments. - lt_save_CC=$CC - lt_save_GCC=$GCC - lt_save_CFLAGS=$CFLAGS - CC=${F77-"f77"} - CFLAGS=$FFLAGS - compiler=$CC - _LT_TAGVAR(compiler, $1)=$CC - _LT_CC_BASENAME([$compiler]) - GCC=$G77 - if test -n "$compiler"; then - AC_MSG_CHECKING([if libtool supports shared libraries]) - AC_MSG_RESULT([$can_build_shared]) - - AC_MSG_CHECKING([whether to build shared libraries]) - test no = "$can_build_shared" && enable_shared=no - - # On AIX, shared libraries and static libraries use the same namespace, and - # are all built from PIC. - case $host_os in - aix3*) - test yes = "$enable_shared" && enable_static=no - if test -n "$RANLIB"; then - archive_cmds="$archive_cmds~\$RANLIB \$lib" - postinstall_cmds='$RANLIB $lib' - fi - ;; - aix[[4-9]]*) - if test ia64 != "$host_cpu"; then - case $enable_shared,$with_aix_soname,$aix_use_runtimelinking in - yes,aix,yes) ;; # shared object as lib.so file only - yes,svr4,*) ;; # shared object as lib.so archive member only - yes,*) enable_static=no ;; # shared object in lib.a archive as well - esac - fi - ;; - esac - AC_MSG_RESULT([$enable_shared]) - - AC_MSG_CHECKING([whether to build static libraries]) - # Make sure either enable_shared or enable_static is yes. - test yes = "$enable_shared" || enable_static=yes - AC_MSG_RESULT([$enable_static]) - - _LT_TAGVAR(GCC, $1)=$G77 - _LT_TAGVAR(LD, $1)=$LD - - ## CAVEAT EMPTOR: - ## There is no encapsulation within the following macros, do not change - ## the running order or otherwise move them around unless you know exactly - ## what you are doing... - _LT_COMPILER_PIC($1) - _LT_COMPILER_C_O($1) - _LT_COMPILER_FILE_LOCKS($1) - _LT_LINKER_SHLIBS($1) - _LT_SYS_DYNAMIC_LINKER($1) - _LT_LINKER_HARDCODE_LIBPATH($1) - - _LT_CONFIG($1) - fi # test -n "$compiler" - - GCC=$lt_save_GCC - CC=$lt_save_CC - CFLAGS=$lt_save_CFLAGS -fi # test yes != "$_lt_disable_F77" - -AC_LANG_POP -])# _LT_LANG_F77_CONFIG - - -# _LT_LANG_FC_CONFIG([TAG]) -# ------------------------- -# Ensure that the configuration variables for a Fortran compiler are -# suitably defined. These variables are subsequently used by _LT_CONFIG -# to write the compiler configuration to 'libtool'. -m4_defun([_LT_LANG_FC_CONFIG], -[AC_LANG_PUSH(Fortran) - -if test -z "$FC" || test no = "$FC"; then - _lt_disable_FC=yes -fi - -_LT_TAGVAR(archive_cmds_need_lc, $1)=no -_LT_TAGVAR(allow_undefined_flag, $1)= -_LT_TAGVAR(always_export_symbols, $1)=no -_LT_TAGVAR(archive_expsym_cmds, $1)= -_LT_TAGVAR(export_dynamic_flag_spec, $1)= -_LT_TAGVAR(hardcode_direct, $1)=no -_LT_TAGVAR(hardcode_direct_absolute, $1)=no -_LT_TAGVAR(hardcode_libdir_flag_spec, $1)= -_LT_TAGVAR(hardcode_libdir_separator, $1)= -_LT_TAGVAR(hardcode_minus_L, $1)=no -_LT_TAGVAR(hardcode_automatic, $1)=no -_LT_TAGVAR(inherit_rpath, $1)=no -_LT_TAGVAR(module_cmds, $1)= -_LT_TAGVAR(module_expsym_cmds, $1)= -_LT_TAGVAR(link_all_deplibs, $1)=unknown -_LT_TAGVAR(old_archive_cmds, $1)=$old_archive_cmds -_LT_TAGVAR(reload_flag, $1)=$reload_flag -_LT_TAGVAR(reload_cmds, $1)=$reload_cmds -_LT_TAGVAR(no_undefined_flag, $1)= -_LT_TAGVAR(whole_archive_flag_spec, $1)= -_LT_TAGVAR(enable_shared_with_static_runtimes, $1)=no - -# Source file extension for fc test sources. -ac_ext=${ac_fc_srcext-f} - -# Object file extension for compiled fc test sources. -objext=o -_LT_TAGVAR(objext, $1)=$objext - -# No sense in running all these tests if we already determined that -# the FC compiler isn't working. Some variables (like enable_shared) -# are currently assumed to apply to all compilers on this platform, -# and will be corrupted by setting them based on a non-working compiler. -if test yes != "$_lt_disable_FC"; then - # Code to be used in simple compile tests - lt_simple_compile_test_code="\ - subroutine t - return - end -" - - # Code to be used in simple link tests - lt_simple_link_test_code="\ - program t - end -" - - # ltmain only uses $CC for tagged configurations so make sure $CC is set. - _LT_TAG_COMPILER - - # save warnings/boilerplate of simple test code - _LT_COMPILER_BOILERPLATE - _LT_LINKER_BOILERPLATE - - # Allow CC to be a program name with arguments. - lt_save_CC=$CC - lt_save_GCC=$GCC - lt_save_CFLAGS=$CFLAGS - CC=${FC-"f95"} - CFLAGS=$FCFLAGS - compiler=$CC - GCC=$ac_cv_fc_compiler_gnu - - _LT_TAGVAR(compiler, $1)=$CC - _LT_CC_BASENAME([$compiler]) - - if test -n "$compiler"; then - AC_MSG_CHECKING([if libtool supports shared libraries]) - AC_MSG_RESULT([$can_build_shared]) - - AC_MSG_CHECKING([whether to build shared libraries]) - test no = "$can_build_shared" && enable_shared=no - - # On AIX, shared libraries and static libraries use the same namespace, and - # are all built from PIC. - case $host_os in - aix3*) - test yes = "$enable_shared" && enable_static=no - if test -n "$RANLIB"; then - archive_cmds="$archive_cmds~\$RANLIB \$lib" - postinstall_cmds='$RANLIB $lib' - fi - ;; - aix[[4-9]]*) - if test ia64 != "$host_cpu"; then - case $enable_shared,$with_aix_soname,$aix_use_runtimelinking in - yes,aix,yes) ;; # shared object as lib.so file only - yes,svr4,*) ;; # shared object as lib.so archive member only - yes,*) enable_static=no ;; # shared object in lib.a archive as well - esac - fi - ;; - esac - AC_MSG_RESULT([$enable_shared]) - - AC_MSG_CHECKING([whether to build static libraries]) - # Make sure either enable_shared or enable_static is yes. - test yes = "$enable_shared" || enable_static=yes - AC_MSG_RESULT([$enable_static]) - - _LT_TAGVAR(GCC, $1)=$ac_cv_fc_compiler_gnu - _LT_TAGVAR(LD, $1)=$LD - - ## CAVEAT EMPTOR: - ## There is no encapsulation within the following macros, do not change - ## the running order or otherwise move them around unless you know exactly - ## what you are doing... - _LT_SYS_HIDDEN_LIBDEPS($1) - _LT_COMPILER_PIC($1) - _LT_COMPILER_C_O($1) - _LT_COMPILER_FILE_LOCKS($1) - _LT_LINKER_SHLIBS($1) - _LT_SYS_DYNAMIC_LINKER($1) - _LT_LINKER_HARDCODE_LIBPATH($1) - - _LT_CONFIG($1) - fi # test -n "$compiler" - - GCC=$lt_save_GCC - CC=$lt_save_CC - CFLAGS=$lt_save_CFLAGS -fi # test yes != "$_lt_disable_FC" - -AC_LANG_POP -])# _LT_LANG_FC_CONFIG - - -# _LT_LANG_GCJ_CONFIG([TAG]) -# -------------------------- -# Ensure that the configuration variables for the GNU Java Compiler compiler -# are suitably defined. These variables are subsequently used by _LT_CONFIG -# to write the compiler configuration to 'libtool'. -m4_defun([_LT_LANG_GCJ_CONFIG], -[AC_REQUIRE([LT_PROG_GCJ])dnl -AC_LANG_SAVE - -# Source file extension for Java test sources. -ac_ext=java - -# Object file extension for compiled Java test sources. -objext=o -_LT_TAGVAR(objext, $1)=$objext - -# Code to be used in simple compile tests -lt_simple_compile_test_code="class foo {}" - -# Code to be used in simple link tests -lt_simple_link_test_code='public class conftest { public static void main(String[[]] argv) {}; }' - -# ltmain only uses $CC for tagged configurations so make sure $CC is set. -_LT_TAG_COMPILER - -# save warnings/boilerplate of simple test code -_LT_COMPILER_BOILERPLATE -_LT_LINKER_BOILERPLATE - -# Allow CC to be a program name with arguments. -lt_save_CC=$CC -lt_save_CFLAGS=$CFLAGS -lt_save_GCC=$GCC -GCC=yes -CC=${GCJ-"gcj"} -CFLAGS=$GCJFLAGS -compiler=$CC -_LT_TAGVAR(compiler, $1)=$CC -_LT_TAGVAR(LD, $1)=$LD -_LT_CC_BASENAME([$compiler]) - -# GCJ did not exist at the time GCC didn't implicitly link libc in. -_LT_TAGVAR(archive_cmds_need_lc, $1)=no - -_LT_TAGVAR(old_archive_cmds, $1)=$old_archive_cmds -_LT_TAGVAR(reload_flag, $1)=$reload_flag -_LT_TAGVAR(reload_cmds, $1)=$reload_cmds - -if test -n "$compiler"; then - _LT_COMPILER_NO_RTTI($1) - _LT_COMPILER_PIC($1) - _LT_COMPILER_C_O($1) - _LT_COMPILER_FILE_LOCKS($1) - _LT_LINKER_SHLIBS($1) - _LT_LINKER_HARDCODE_LIBPATH($1) - - _LT_CONFIG($1) -fi - -AC_LANG_RESTORE - -GCC=$lt_save_GCC -CC=$lt_save_CC -CFLAGS=$lt_save_CFLAGS -])# _LT_LANG_GCJ_CONFIG - - -# _LT_LANG_GO_CONFIG([TAG]) -# -------------------------- -# Ensure that the configuration variables for the GNU Go compiler -# are suitably defined. These variables are subsequently used by _LT_CONFIG -# to write the compiler configuration to 'libtool'. -m4_defun([_LT_LANG_GO_CONFIG], -[AC_REQUIRE([LT_PROG_GO])dnl -AC_LANG_SAVE - -# Source file extension for Go test sources. -ac_ext=go - -# Object file extension for compiled Go test sources. -objext=o -_LT_TAGVAR(objext, $1)=$objext - -# Code to be used in simple compile tests -lt_simple_compile_test_code="package main; func main() { }" - -# Code to be used in simple link tests -lt_simple_link_test_code='package main; func main() { }' - -# ltmain only uses $CC for tagged configurations so make sure $CC is set. -_LT_TAG_COMPILER - -# save warnings/boilerplate of simple test code -_LT_COMPILER_BOILERPLATE -_LT_LINKER_BOILERPLATE - -# Allow CC to be a program name with arguments. -lt_save_CC=$CC -lt_save_CFLAGS=$CFLAGS -lt_save_GCC=$GCC -GCC=yes -CC=${GOC-"gccgo"} -CFLAGS=$GOFLAGS -compiler=$CC -_LT_TAGVAR(compiler, $1)=$CC -_LT_TAGVAR(LD, $1)=$LD -_LT_CC_BASENAME([$compiler]) - -# Go did not exist at the time GCC didn't implicitly link libc in. -_LT_TAGVAR(archive_cmds_need_lc, $1)=no - -_LT_TAGVAR(old_archive_cmds, $1)=$old_archive_cmds -_LT_TAGVAR(reload_flag, $1)=$reload_flag -_LT_TAGVAR(reload_cmds, $1)=$reload_cmds - -if test -n "$compiler"; then - _LT_COMPILER_NO_RTTI($1) - _LT_COMPILER_PIC($1) - _LT_COMPILER_C_O($1) - _LT_COMPILER_FILE_LOCKS($1) - _LT_LINKER_SHLIBS($1) - _LT_LINKER_HARDCODE_LIBPATH($1) - - _LT_CONFIG($1) -fi - -AC_LANG_RESTORE - -GCC=$lt_save_GCC -CC=$lt_save_CC -CFLAGS=$lt_save_CFLAGS -])# _LT_LANG_GO_CONFIG - - -# _LT_LANG_RC_CONFIG([TAG]) -# ------------------------- -# Ensure that the configuration variables for the Windows resource compiler -# are suitably defined. These variables are subsequently used by _LT_CONFIG -# to write the compiler configuration to 'libtool'. -m4_defun([_LT_LANG_RC_CONFIG], -[AC_REQUIRE([LT_PROG_RC])dnl -AC_LANG_SAVE - -# Source file extension for RC test sources. -ac_ext=rc - -# Object file extension for compiled RC test sources. -objext=o -_LT_TAGVAR(objext, $1)=$objext - -# Code to be used in simple compile tests -lt_simple_compile_test_code='sample MENU { MENUITEM "&Soup", 100, CHECKED }' - -# Code to be used in simple link tests -lt_simple_link_test_code=$lt_simple_compile_test_code - -# ltmain only uses $CC for tagged configurations so make sure $CC is set. -_LT_TAG_COMPILER - -# save warnings/boilerplate of simple test code -_LT_COMPILER_BOILERPLATE -_LT_LINKER_BOILERPLATE - -# Allow CC to be a program name with arguments. -lt_save_CC=$CC -lt_save_CFLAGS=$CFLAGS -lt_save_GCC=$GCC -GCC= -CC=${RC-"windres"} -CFLAGS= -compiler=$CC -_LT_TAGVAR(compiler, $1)=$CC -_LT_CC_BASENAME([$compiler]) -_LT_TAGVAR(lt_cv_prog_compiler_c_o, $1)=yes - -if test -n "$compiler"; then - : - _LT_CONFIG($1) -fi - -GCC=$lt_save_GCC -AC_LANG_RESTORE -CC=$lt_save_CC -CFLAGS=$lt_save_CFLAGS -])# _LT_LANG_RC_CONFIG - - -# LT_PROG_GCJ -# ----------- -AC_DEFUN([LT_PROG_GCJ], -[m4_ifdef([AC_PROG_GCJ], [AC_PROG_GCJ], - [m4_ifdef([A][M_PROG_GCJ], [A][M_PROG_GCJ], - [AC_CHECK_TOOL(GCJ, gcj,) - test set = "${GCJFLAGS+set}" || GCJFLAGS="-g -O2" - AC_SUBST(GCJFLAGS)])])[]dnl -]) - -# Old name: -AU_ALIAS([LT_AC_PROG_GCJ], [LT_PROG_GCJ]) -dnl aclocal-1.4 backwards compatibility: -dnl AC_DEFUN([LT_AC_PROG_GCJ], []) - - -# LT_PROG_GO -# ---------- -AC_DEFUN([LT_PROG_GO], -[AC_CHECK_TOOL(GOC, gccgo,) -]) - - -# LT_PROG_RC -# ---------- -AC_DEFUN([LT_PROG_RC], -[AC_CHECK_TOOL(RC, windres,) -]) - -# Old name: -AU_ALIAS([LT_AC_PROG_RC], [LT_PROG_RC]) -dnl aclocal-1.4 backwards compatibility: -dnl AC_DEFUN([LT_AC_PROG_RC], []) - - -# _LT_DECL_EGREP -# -------------- -# If we don't have a new enough Autoconf to choose the best grep -# available, choose the one first in the user's PATH. -m4_defun([_LT_DECL_EGREP], -[AC_REQUIRE([AC_PROG_EGREP])dnl -AC_REQUIRE([AC_PROG_FGREP])dnl -test -z "$GREP" && GREP=grep -_LT_DECL([], [GREP], [1], [A grep program that handles long lines]) -_LT_DECL([], [EGREP], [1], [An ERE matcher]) -_LT_DECL([], [FGREP], [1], [A literal string matcher]) -dnl Non-bleeding-edge autoconf doesn't subst GREP, so do it here too -AC_SUBST([GREP]) -]) - - -# _LT_DECL_OBJDUMP -# -------------- -# If we don't have a new enough Autoconf to choose the best objdump -# available, choose the one first in the user's PATH. -m4_defun([_LT_DECL_OBJDUMP], -[AC_CHECK_TOOL(OBJDUMP, objdump, false) -test -z "$OBJDUMP" && OBJDUMP=objdump -_LT_DECL([], [OBJDUMP], [1], [An object symbol dumper]) -AC_SUBST([OBJDUMP]) -]) - -# _LT_DECL_DLLTOOL -# ---------------- -# Ensure DLLTOOL variable is set. -m4_defun([_LT_DECL_DLLTOOL], -[AC_CHECK_TOOL(DLLTOOL, dlltool, false) -test -z "$DLLTOOL" && DLLTOOL=dlltool -_LT_DECL([], [DLLTOOL], [1], [DLL creation program]) -AC_SUBST([DLLTOOL]) -]) - -# _LT_DECL_SED -# ------------ -# Check for a fully-functional sed program, that truncates -# as few characters as possible. Prefer GNU sed if found. -m4_defun([_LT_DECL_SED], -[AC_PROG_SED -test -z "$SED" && SED=sed -Xsed="$SED -e 1s/^X//" -_LT_DECL([], [SED], [1], [A sed program that does not truncate output]) -_LT_DECL([], [Xsed], ["\$SED -e 1s/^X//"], - [Sed that helps us avoid accidentally triggering echo(1) options like -n]) -])# _LT_DECL_SED - -m4_ifndef([AC_PROG_SED], [ -# NOTE: This macro has been submitted for inclusion into # -# GNU Autoconf as AC_PROG_SED. When it is available in # -# a released version of Autoconf we should remove this # -# macro and use it instead. # - -m4_defun([AC_PROG_SED], -[AC_MSG_CHECKING([for a sed that does not truncate output]) -AC_CACHE_VAL(lt_cv_path_SED, -[# Loop through the user's path and test for sed and gsed. -# Then use that list of sed's as ones to test for truncation. -as_save_IFS=$IFS; IFS=$PATH_SEPARATOR -for as_dir in $PATH -do - IFS=$as_save_IFS - test -z "$as_dir" && as_dir=. - for lt_ac_prog in sed gsed; do - for ac_exec_ext in '' $ac_executable_extensions; do - if $as_executable_p "$as_dir/$lt_ac_prog$ac_exec_ext"; then - lt_ac_sed_list="$lt_ac_sed_list $as_dir/$lt_ac_prog$ac_exec_ext" - fi - done - done -done -IFS=$as_save_IFS -lt_ac_max=0 -lt_ac_count=0 -# Add /usr/xpg4/bin/sed as it is typically found on Solaris -# along with /bin/sed that truncates output. -for lt_ac_sed in $lt_ac_sed_list /usr/xpg4/bin/sed; do - test ! -f "$lt_ac_sed" && continue - cat /dev/null > conftest.in - lt_ac_count=0 - echo $ECHO_N "0123456789$ECHO_C" >conftest.in - # Check for GNU sed and select it if it is found. - if "$lt_ac_sed" --version 2>&1 < /dev/null | grep 'GNU' > /dev/null; then - lt_cv_path_SED=$lt_ac_sed - break - fi - while true; do - cat conftest.in conftest.in >conftest.tmp - mv conftest.tmp conftest.in - cp conftest.in conftest.nl - echo >>conftest.nl - $lt_ac_sed -e 's/a$//' < conftest.nl >conftest.out || break - cmp -s conftest.out conftest.nl || break - # 10000 chars as input seems more than enough - test 10 -lt "$lt_ac_count" && break - lt_ac_count=`expr $lt_ac_count + 1` - if test "$lt_ac_count" -gt "$lt_ac_max"; then - lt_ac_max=$lt_ac_count - lt_cv_path_SED=$lt_ac_sed - fi - done -done -]) -SED=$lt_cv_path_SED -AC_SUBST([SED]) -AC_MSG_RESULT([$SED]) -])#AC_PROG_SED -])#m4_ifndef - -# Old name: -AU_ALIAS([LT_AC_PROG_SED], [AC_PROG_SED]) -dnl aclocal-1.4 backwards compatibility: -dnl AC_DEFUN([LT_AC_PROG_SED], []) - - -# _LT_CHECK_SHELL_FEATURES -# ------------------------ -# Find out whether the shell is Bourne or XSI compatible, -# or has some other useful features. -m4_defun([_LT_CHECK_SHELL_FEATURES], -[if ( (MAIL=60; unset MAIL) || exit) >/dev/null 2>&1; then - lt_unset=unset -else - lt_unset=false -fi -_LT_DECL([], [lt_unset], [0], [whether the shell understands "unset"])dnl - -# test EBCDIC or ASCII -case `echo X|tr X '\101'` in - A) # ASCII based system - # \n is not interpreted correctly by Solaris 8 /usr/ucb/tr - lt_SP2NL='tr \040 \012' - lt_NL2SP='tr \015\012 \040\040' - ;; - *) # EBCDIC based system - lt_SP2NL='tr \100 \n' - lt_NL2SP='tr \r\n \100\100' - ;; -esac -_LT_DECL([SP2NL], [lt_SP2NL], [1], [turn spaces into newlines])dnl -_LT_DECL([NL2SP], [lt_NL2SP], [1], [turn newlines into spaces])dnl -])# _LT_CHECK_SHELL_FEATURES - - -# _LT_PATH_CONVERSION_FUNCTIONS -# ----------------------------- -# Determine what file name conversion functions should be used by -# func_to_host_file (and, implicitly, by func_to_host_path). These are needed -# for certain cross-compile configurations and native mingw. -m4_defun([_LT_PATH_CONVERSION_FUNCTIONS], -[AC_REQUIRE([AC_CANONICAL_HOST])dnl -AC_REQUIRE([AC_CANONICAL_BUILD])dnl -AC_MSG_CHECKING([how to convert $build file names to $host format]) -AC_CACHE_VAL(lt_cv_to_host_file_cmd, -[case $host in - *-*-mingw* ) - case $build in - *-*-mingw* ) # actually msys - lt_cv_to_host_file_cmd=func_convert_file_msys_to_w32 - ;; - *-*-cygwin* ) - lt_cv_to_host_file_cmd=func_convert_file_cygwin_to_w32 - ;; - * ) # otherwise, assume *nix - lt_cv_to_host_file_cmd=func_convert_file_nix_to_w32 - ;; - esac - ;; - *-*-cygwin* ) - case $build in - *-*-mingw* ) # actually msys - lt_cv_to_host_file_cmd=func_convert_file_msys_to_cygwin - ;; - *-*-cygwin* ) - lt_cv_to_host_file_cmd=func_convert_file_noop - ;; - * ) # otherwise, assume *nix - lt_cv_to_host_file_cmd=func_convert_file_nix_to_cygwin - ;; - esac - ;; - * ) # unhandled hosts (and "normal" native builds) - lt_cv_to_host_file_cmd=func_convert_file_noop - ;; -esac -]) -to_host_file_cmd=$lt_cv_to_host_file_cmd -AC_MSG_RESULT([$lt_cv_to_host_file_cmd]) -_LT_DECL([to_host_file_cmd], [lt_cv_to_host_file_cmd], - [0], [convert $build file names to $host format])dnl - -AC_MSG_CHECKING([how to convert $build file names to toolchain format]) -AC_CACHE_VAL(lt_cv_to_tool_file_cmd, -[#assume ordinary cross tools, or native build. -lt_cv_to_tool_file_cmd=func_convert_file_noop -case $host in - *-*-mingw* ) - case $build in - *-*-mingw* ) # actually msys - lt_cv_to_tool_file_cmd=func_convert_file_msys_to_w32 - ;; - esac - ;; -esac -]) -to_tool_file_cmd=$lt_cv_to_tool_file_cmd -AC_MSG_RESULT([$lt_cv_to_tool_file_cmd]) -_LT_DECL([to_tool_file_cmd], [lt_cv_to_tool_file_cmd], - [0], [convert $build files to toolchain format])dnl -])# _LT_PATH_CONVERSION_FUNCTIONS - -# Helper functions for option handling. -*- Autoconf -*- -# -# Copyright (C) 2004-2005, 2007-2009, 2011-2015 Free Software -# Foundation, Inc. -# Written by Gary V. Vaughan, 2004 -# -# This file is free software; the Free Software Foundation gives -# unlimited permission to copy and/or distribute it, with or without -# modifications, as long as this notice is preserved. - -# serial 8 ltoptions.m4 - -# This is to help aclocal find these macros, as it can't see m4_define. -AC_DEFUN([LTOPTIONS_VERSION], [m4_if([1])]) - - -# _LT_MANGLE_OPTION(MACRO-NAME, OPTION-NAME) -# ------------------------------------------ -m4_define([_LT_MANGLE_OPTION], -[[_LT_OPTION_]m4_bpatsubst($1__$2, [[^a-zA-Z0-9_]], [_])]) - - -# _LT_SET_OPTION(MACRO-NAME, OPTION-NAME) -# --------------------------------------- -# Set option OPTION-NAME for macro MACRO-NAME, and if there is a -# matching handler defined, dispatch to it. Other OPTION-NAMEs are -# saved as a flag. -m4_define([_LT_SET_OPTION], -[m4_define(_LT_MANGLE_OPTION([$1], [$2]))dnl -m4_ifdef(_LT_MANGLE_DEFUN([$1], [$2]), - _LT_MANGLE_DEFUN([$1], [$2]), - [m4_warning([Unknown $1 option '$2'])])[]dnl -]) - - -# _LT_IF_OPTION(MACRO-NAME, OPTION-NAME, IF-SET, [IF-NOT-SET]) -# ------------------------------------------------------------ -# Execute IF-SET if OPTION is set, IF-NOT-SET otherwise. -m4_define([_LT_IF_OPTION], -[m4_ifdef(_LT_MANGLE_OPTION([$1], [$2]), [$3], [$4])]) - - -# _LT_UNLESS_OPTIONS(MACRO-NAME, OPTION-LIST, IF-NOT-SET) -# ------------------------------------------------------- -# Execute IF-NOT-SET unless all options in OPTION-LIST for MACRO-NAME -# are set. -m4_define([_LT_UNLESS_OPTIONS], -[m4_foreach([_LT_Option], m4_split(m4_normalize([$2])), - [m4_ifdef(_LT_MANGLE_OPTION([$1], _LT_Option), - [m4_define([$0_found])])])[]dnl -m4_ifdef([$0_found], [m4_undefine([$0_found])], [$3 -])[]dnl -]) - - -# _LT_SET_OPTIONS(MACRO-NAME, OPTION-LIST) -# ---------------------------------------- -# OPTION-LIST is a space-separated list of Libtool options associated -# with MACRO-NAME. If any OPTION has a matching handler declared with -# LT_OPTION_DEFINE, dispatch to that macro; otherwise complain about -# the unknown option and exit. -m4_defun([_LT_SET_OPTIONS], -[# Set options -m4_foreach([_LT_Option], m4_split(m4_normalize([$2])), - [_LT_SET_OPTION([$1], _LT_Option)]) - -m4_if([$1],[LT_INIT],[ - dnl - dnl Simply set some default values (i.e off) if boolean options were not - dnl specified: - _LT_UNLESS_OPTIONS([LT_INIT], [dlopen], [enable_dlopen=no - ]) - _LT_UNLESS_OPTIONS([LT_INIT], [win32-dll], [enable_win32_dll=no - ]) - dnl - dnl If no reference was made to various pairs of opposing options, then - dnl we run the default mode handler for the pair. For example, if neither - dnl 'shared' nor 'disable-shared' was passed, we enable building of shared - dnl archives by default: - _LT_UNLESS_OPTIONS([LT_INIT], [shared disable-shared], [_LT_ENABLE_SHARED]) - _LT_UNLESS_OPTIONS([LT_INIT], [static disable-static], [_LT_ENABLE_STATIC]) - _LT_UNLESS_OPTIONS([LT_INIT], [pic-only no-pic], [_LT_WITH_PIC]) - _LT_UNLESS_OPTIONS([LT_INIT], [fast-install disable-fast-install], - [_LT_ENABLE_FAST_INSTALL]) - _LT_UNLESS_OPTIONS([LT_INIT], [aix-soname=aix aix-soname=both aix-soname=svr4], - [_LT_WITH_AIX_SONAME([aix])]) - ]) -])# _LT_SET_OPTIONS - - - -# _LT_MANGLE_DEFUN(MACRO-NAME, OPTION-NAME) -# ----------------------------------------- -m4_define([_LT_MANGLE_DEFUN], -[[_LT_OPTION_DEFUN_]m4_bpatsubst(m4_toupper([$1__$2]), [[^A-Z0-9_]], [_])]) - - -# LT_OPTION_DEFINE(MACRO-NAME, OPTION-NAME, CODE) -# ----------------------------------------------- -m4_define([LT_OPTION_DEFINE], -[m4_define(_LT_MANGLE_DEFUN([$1], [$2]), [$3])[]dnl -])# LT_OPTION_DEFINE - - -# dlopen -# ------ -LT_OPTION_DEFINE([LT_INIT], [dlopen], [enable_dlopen=yes -]) - -AU_DEFUN([AC_LIBTOOL_DLOPEN], -[_LT_SET_OPTION([LT_INIT], [dlopen]) -AC_DIAGNOSE([obsolete], -[$0: Remove this warning and the call to _LT_SET_OPTION when you -put the 'dlopen' option into LT_INIT's first parameter.]) -]) - -dnl aclocal-1.4 backwards compatibility: -dnl AC_DEFUN([AC_LIBTOOL_DLOPEN], []) - - -# win32-dll -# --------- -# Declare package support for building win32 dll's. -LT_OPTION_DEFINE([LT_INIT], [win32-dll], -[enable_win32_dll=yes - -case $host in -*-*-cygwin* | *-*-mingw* | *-*-pw32* | *-*-cegcc*) - AC_CHECK_TOOL(AS, as, false) - AC_CHECK_TOOL(DLLTOOL, dlltool, false) - AC_CHECK_TOOL(OBJDUMP, objdump, false) - ;; -esac - -test -z "$AS" && AS=as -_LT_DECL([], [AS], [1], [Assembler program])dnl - -test -z "$DLLTOOL" && DLLTOOL=dlltool -_LT_DECL([], [DLLTOOL], [1], [DLL creation program])dnl - -test -z "$OBJDUMP" && OBJDUMP=objdump -_LT_DECL([], [OBJDUMP], [1], [Object dumper program])dnl -])# win32-dll - -AU_DEFUN([AC_LIBTOOL_WIN32_DLL], -[AC_REQUIRE([AC_CANONICAL_HOST])dnl -_LT_SET_OPTION([LT_INIT], [win32-dll]) -AC_DIAGNOSE([obsolete], -[$0: Remove this warning and the call to _LT_SET_OPTION when you -put the 'win32-dll' option into LT_INIT's first parameter.]) -]) - -dnl aclocal-1.4 backwards compatibility: -dnl AC_DEFUN([AC_LIBTOOL_WIN32_DLL], []) - - -# _LT_ENABLE_SHARED([DEFAULT]) -# ---------------------------- -# implement the --enable-shared flag, and supports the 'shared' and -# 'disable-shared' LT_INIT options. -# DEFAULT is either 'yes' or 'no'. If omitted, it defaults to 'yes'. -m4_define([_LT_ENABLE_SHARED], -[m4_define([_LT_ENABLE_SHARED_DEFAULT], [m4_if($1, no, no, yes)])dnl -AC_ARG_ENABLE([shared], - [AS_HELP_STRING([--enable-shared@<:@=PKGS@:>@], - [build shared libraries @<:@default=]_LT_ENABLE_SHARED_DEFAULT[@:>@])], - [p=${PACKAGE-default} - case $enableval in - yes) enable_shared=yes ;; - no) enable_shared=no ;; - *) - enable_shared=no - # Look at the argument we got. We use all the common list separators. - lt_save_ifs=$IFS; IFS=$IFS$PATH_SEPARATOR, - for pkg in $enableval; do - IFS=$lt_save_ifs - if test "X$pkg" = "X$p"; then - enable_shared=yes - fi - done - IFS=$lt_save_ifs - ;; - esac], - [enable_shared=]_LT_ENABLE_SHARED_DEFAULT) - - _LT_DECL([build_libtool_libs], [enable_shared], [0], - [Whether or not to build shared libraries]) -])# _LT_ENABLE_SHARED - -LT_OPTION_DEFINE([LT_INIT], [shared], [_LT_ENABLE_SHARED([yes])]) -LT_OPTION_DEFINE([LT_INIT], [disable-shared], [_LT_ENABLE_SHARED([no])]) - -# Old names: -AC_DEFUN([AC_ENABLE_SHARED], -[_LT_SET_OPTION([LT_INIT], m4_if([$1], [no], [disable-])[shared]) -]) - -AC_DEFUN([AC_DISABLE_SHARED], -[_LT_SET_OPTION([LT_INIT], [disable-shared]) -]) - -AU_DEFUN([AM_ENABLE_SHARED], [AC_ENABLE_SHARED($@)]) -AU_DEFUN([AM_DISABLE_SHARED], [AC_DISABLE_SHARED($@)]) - -dnl aclocal-1.4 backwards compatibility: -dnl AC_DEFUN([AM_ENABLE_SHARED], []) -dnl AC_DEFUN([AM_DISABLE_SHARED], []) - - - -# _LT_ENABLE_STATIC([DEFAULT]) -# ---------------------------- -# implement the --enable-static flag, and support the 'static' and -# 'disable-static' LT_INIT options. -# DEFAULT is either 'yes' or 'no'. If omitted, it defaults to 'yes'. -m4_define([_LT_ENABLE_STATIC], -[m4_define([_LT_ENABLE_STATIC_DEFAULT], [m4_if($1, no, no, yes)])dnl -AC_ARG_ENABLE([static], - [AS_HELP_STRING([--enable-static@<:@=PKGS@:>@], - [build static libraries @<:@default=]_LT_ENABLE_STATIC_DEFAULT[@:>@])], - [p=${PACKAGE-default} - case $enableval in - yes) enable_static=yes ;; - no) enable_static=no ;; - *) - enable_static=no - # Look at the argument we got. We use all the common list separators. - lt_save_ifs=$IFS; IFS=$IFS$PATH_SEPARATOR, - for pkg in $enableval; do - IFS=$lt_save_ifs - if test "X$pkg" = "X$p"; then - enable_static=yes - fi - done - IFS=$lt_save_ifs - ;; - esac], - [enable_static=]_LT_ENABLE_STATIC_DEFAULT) - - _LT_DECL([build_old_libs], [enable_static], [0], - [Whether or not to build static libraries]) -])# _LT_ENABLE_STATIC - -LT_OPTION_DEFINE([LT_INIT], [static], [_LT_ENABLE_STATIC([yes])]) -LT_OPTION_DEFINE([LT_INIT], [disable-static], [_LT_ENABLE_STATIC([no])]) - -# Old names: -AC_DEFUN([AC_ENABLE_STATIC], -[_LT_SET_OPTION([LT_INIT], m4_if([$1], [no], [disable-])[static]) -]) - -AC_DEFUN([AC_DISABLE_STATIC], -[_LT_SET_OPTION([LT_INIT], [disable-static]) -]) - -AU_DEFUN([AM_ENABLE_STATIC], [AC_ENABLE_STATIC($@)]) -AU_DEFUN([AM_DISABLE_STATIC], [AC_DISABLE_STATIC($@)]) - -dnl aclocal-1.4 backwards compatibility: -dnl AC_DEFUN([AM_ENABLE_STATIC], []) -dnl AC_DEFUN([AM_DISABLE_STATIC], []) - - - -# _LT_ENABLE_FAST_INSTALL([DEFAULT]) -# ---------------------------------- -# implement the --enable-fast-install flag, and support the 'fast-install' -# and 'disable-fast-install' LT_INIT options. -# DEFAULT is either 'yes' or 'no'. If omitted, it defaults to 'yes'. -m4_define([_LT_ENABLE_FAST_INSTALL], -[m4_define([_LT_ENABLE_FAST_INSTALL_DEFAULT], [m4_if($1, no, no, yes)])dnl -AC_ARG_ENABLE([fast-install], - [AS_HELP_STRING([--enable-fast-install@<:@=PKGS@:>@], - [optimize for fast installation @<:@default=]_LT_ENABLE_FAST_INSTALL_DEFAULT[@:>@])], - [p=${PACKAGE-default} - case $enableval in - yes) enable_fast_install=yes ;; - no) enable_fast_install=no ;; - *) - enable_fast_install=no - # Look at the argument we got. We use all the common list separators. - lt_save_ifs=$IFS; IFS=$IFS$PATH_SEPARATOR, - for pkg in $enableval; do - IFS=$lt_save_ifs - if test "X$pkg" = "X$p"; then - enable_fast_install=yes - fi - done - IFS=$lt_save_ifs - ;; - esac], - [enable_fast_install=]_LT_ENABLE_FAST_INSTALL_DEFAULT) - -_LT_DECL([fast_install], [enable_fast_install], [0], - [Whether or not to optimize for fast installation])dnl -])# _LT_ENABLE_FAST_INSTALL - -LT_OPTION_DEFINE([LT_INIT], [fast-install], [_LT_ENABLE_FAST_INSTALL([yes])]) -LT_OPTION_DEFINE([LT_INIT], [disable-fast-install], [_LT_ENABLE_FAST_INSTALL([no])]) - -# Old names: -AU_DEFUN([AC_ENABLE_FAST_INSTALL], -[_LT_SET_OPTION([LT_INIT], m4_if([$1], [no], [disable-])[fast-install]) -AC_DIAGNOSE([obsolete], -[$0: Remove this warning and the call to _LT_SET_OPTION when you put -the 'fast-install' option into LT_INIT's first parameter.]) -]) - -AU_DEFUN([AC_DISABLE_FAST_INSTALL], -[_LT_SET_OPTION([LT_INIT], [disable-fast-install]) -AC_DIAGNOSE([obsolete], -[$0: Remove this warning and the call to _LT_SET_OPTION when you put -the 'disable-fast-install' option into LT_INIT's first parameter.]) -]) - -dnl aclocal-1.4 backwards compatibility: -dnl AC_DEFUN([AC_ENABLE_FAST_INSTALL], []) -dnl AC_DEFUN([AM_DISABLE_FAST_INSTALL], []) - - -# _LT_WITH_AIX_SONAME([DEFAULT]) -# ---------------------------------- -# implement the --with-aix-soname flag, and support the `aix-soname=aix' -# and `aix-soname=both' and `aix-soname=svr4' LT_INIT options. DEFAULT -# is either `aix', `both' or `svr4'. If omitted, it defaults to `aix'. -m4_define([_LT_WITH_AIX_SONAME], -[m4_define([_LT_WITH_AIX_SONAME_DEFAULT], [m4_if($1, svr4, svr4, m4_if($1, both, both, aix))])dnl -shared_archive_member_spec= -case $host,$enable_shared in -power*-*-aix[[5-9]]*,yes) - AC_MSG_CHECKING([which variant of shared library versioning to provide]) - AC_ARG_WITH([aix-soname], - [AS_HELP_STRING([--with-aix-soname=aix|svr4|both], - [shared library versioning (aka "SONAME") variant to provide on AIX, @<:@default=]_LT_WITH_AIX_SONAME_DEFAULT[@:>@.])], - [case $withval in - aix|svr4|both) - ;; - *) - AC_MSG_ERROR([Unknown argument to --with-aix-soname]) - ;; - esac - lt_cv_with_aix_soname=$with_aix_soname], - [AC_CACHE_VAL([lt_cv_with_aix_soname], - [lt_cv_with_aix_soname=]_LT_WITH_AIX_SONAME_DEFAULT) - with_aix_soname=$lt_cv_with_aix_soname]) - AC_MSG_RESULT([$with_aix_soname]) - if test aix != "$with_aix_soname"; then - # For the AIX way of multilib, we name the shared archive member - # based on the bitwidth used, traditionally 'shr.o' or 'shr_64.o', - # and 'shr.imp' or 'shr_64.imp', respectively, for the Import File. - # Even when GNU compilers ignore OBJECT_MODE but need '-maix64' flag, - # the AIX toolchain works better with OBJECT_MODE set (default 32). - if test 64 = "${OBJECT_MODE-32}"; then - shared_archive_member_spec=shr_64 - else - shared_archive_member_spec=shr - fi - fi - ;; -*) - with_aix_soname=aix - ;; -esac - -_LT_DECL([], [shared_archive_member_spec], [0], - [Shared archive member basename, for filename based shared library versioning on AIX])dnl -])# _LT_WITH_AIX_SONAME - -LT_OPTION_DEFINE([LT_INIT], [aix-soname=aix], [_LT_WITH_AIX_SONAME([aix])]) -LT_OPTION_DEFINE([LT_INIT], [aix-soname=both], [_LT_WITH_AIX_SONAME([both])]) -LT_OPTION_DEFINE([LT_INIT], [aix-soname=svr4], [_LT_WITH_AIX_SONAME([svr4])]) - - -# _LT_WITH_PIC([MODE]) -# -------------------- -# implement the --with-pic flag, and support the 'pic-only' and 'no-pic' -# LT_INIT options. -# MODE is either 'yes' or 'no'. If omitted, it defaults to 'both'. -m4_define([_LT_WITH_PIC], -[AC_ARG_WITH([pic], - [AS_HELP_STRING([--with-pic@<:@=PKGS@:>@], - [try to use only PIC/non-PIC objects @<:@default=use both@:>@])], - [lt_p=${PACKAGE-default} - case $withval in - yes|no) pic_mode=$withval ;; - *) - pic_mode=default - # Look at the argument we got. We use all the common list separators. - lt_save_ifs=$IFS; IFS=$IFS$PATH_SEPARATOR, - for lt_pkg in $withval; do - IFS=$lt_save_ifs - if test "X$lt_pkg" = "X$lt_p"; then - pic_mode=yes - fi - done - IFS=$lt_save_ifs - ;; - esac], - [pic_mode=m4_default([$1], [default])]) - -_LT_DECL([], [pic_mode], [0], [What type of objects to build])dnl -])# _LT_WITH_PIC - -LT_OPTION_DEFINE([LT_INIT], [pic-only], [_LT_WITH_PIC([yes])]) -LT_OPTION_DEFINE([LT_INIT], [no-pic], [_LT_WITH_PIC([no])]) - -# Old name: -AU_DEFUN([AC_LIBTOOL_PICMODE], -[_LT_SET_OPTION([LT_INIT], [pic-only]) -AC_DIAGNOSE([obsolete], -[$0: Remove this warning and the call to _LT_SET_OPTION when you -put the 'pic-only' option into LT_INIT's first parameter.]) -]) - -dnl aclocal-1.4 backwards compatibility: -dnl AC_DEFUN([AC_LIBTOOL_PICMODE], []) - - -m4_define([_LTDL_MODE], []) -LT_OPTION_DEFINE([LTDL_INIT], [nonrecursive], - [m4_define([_LTDL_MODE], [nonrecursive])]) -LT_OPTION_DEFINE([LTDL_INIT], [recursive], - [m4_define([_LTDL_MODE], [recursive])]) -LT_OPTION_DEFINE([LTDL_INIT], [subproject], - [m4_define([_LTDL_MODE], [subproject])]) - -m4_define([_LTDL_TYPE], []) -LT_OPTION_DEFINE([LTDL_INIT], [installable], - [m4_define([_LTDL_TYPE], [installable])]) -LT_OPTION_DEFINE([LTDL_INIT], [convenience], - [m4_define([_LTDL_TYPE], [convenience])]) - -# ltsugar.m4 -- libtool m4 base layer. -*-Autoconf-*- -# -# Copyright (C) 2004-2005, 2007-2008, 2011-2015 Free Software -# Foundation, Inc. -# Written by Gary V. Vaughan, 2004 -# -# This file is free software; the Free Software Foundation gives -# unlimited permission to copy and/or distribute it, with or without -# modifications, as long as this notice is preserved. - -# serial 6 ltsugar.m4 - -# This is to help aclocal find these macros, as it can't see m4_define. -AC_DEFUN([LTSUGAR_VERSION], [m4_if([0.1])]) - - -# lt_join(SEP, ARG1, [ARG2...]) -# ----------------------------- -# Produce ARG1SEPARG2...SEPARGn, omitting [] arguments and their -# associated separator. -# Needed until we can rely on m4_join from Autoconf 2.62, since all earlier -# versions in m4sugar had bugs. -m4_define([lt_join], -[m4_if([$#], [1], [], - [$#], [2], [[$2]], - [m4_if([$2], [], [], [[$2]_])$0([$1], m4_shift(m4_shift($@)))])]) -m4_define([_lt_join], -[m4_if([$#$2], [2], [], - [m4_if([$2], [], [], [[$1$2]])$0([$1], m4_shift(m4_shift($@)))])]) - - -# lt_car(LIST) -# lt_cdr(LIST) -# ------------ -# Manipulate m4 lists. -# These macros are necessary as long as will still need to support -# Autoconf-2.59, which quotes differently. -m4_define([lt_car], [[$1]]) -m4_define([lt_cdr], -[m4_if([$#], 0, [m4_fatal([$0: cannot be called without arguments])], - [$#], 1, [], - [m4_dquote(m4_shift($@))])]) -m4_define([lt_unquote], $1) - - -# lt_append(MACRO-NAME, STRING, [SEPARATOR]) -# ------------------------------------------ -# Redefine MACRO-NAME to hold its former content plus 'SEPARATOR''STRING'. -# Note that neither SEPARATOR nor STRING are expanded; they are appended -# to MACRO-NAME as is (leaving the expansion for when MACRO-NAME is invoked). -# No SEPARATOR is output if MACRO-NAME was previously undefined (different -# than defined and empty). -# -# This macro is needed until we can rely on Autoconf 2.62, since earlier -# versions of m4sugar mistakenly expanded SEPARATOR but not STRING. -m4_define([lt_append], -[m4_define([$1], - m4_ifdef([$1], [m4_defn([$1])[$3]])[$2])]) - - - -# lt_combine(SEP, PREFIX-LIST, INFIX, SUFFIX1, [SUFFIX2...]) -# ---------------------------------------------------------- -# Produce a SEP delimited list of all paired combinations of elements of -# PREFIX-LIST with SUFFIX1 through SUFFIXn. Each element of the list -# has the form PREFIXmINFIXSUFFIXn. -# Needed until we can rely on m4_combine added in Autoconf 2.62. -m4_define([lt_combine], -[m4_if(m4_eval([$# > 3]), [1], - [m4_pushdef([_Lt_sep], [m4_define([_Lt_sep], m4_defn([lt_car]))])]]dnl -[[m4_foreach([_Lt_prefix], [$2], - [m4_foreach([_Lt_suffix], - ]m4_dquote(m4_dquote(m4_shift(m4_shift(m4_shift($@)))))[, - [_Lt_sep([$1])[]m4_defn([_Lt_prefix])[$3]m4_defn([_Lt_suffix])])])])]) - - -# lt_if_append_uniq(MACRO-NAME, VARNAME, [SEPARATOR], [UNIQ], [NOT-UNIQ]) -# ----------------------------------------------------------------------- -# Iff MACRO-NAME does not yet contain VARNAME, then append it (delimited -# by SEPARATOR if supplied) and expand UNIQ, else NOT-UNIQ. -m4_define([lt_if_append_uniq], -[m4_ifdef([$1], - [m4_if(m4_index([$3]m4_defn([$1])[$3], [$3$2$3]), [-1], - [lt_append([$1], [$2], [$3])$4], - [$5])], - [lt_append([$1], [$2], [$3])$4])]) - - -# lt_dict_add(DICT, KEY, VALUE) -# ----------------------------- -m4_define([lt_dict_add], -[m4_define([$1($2)], [$3])]) - - -# lt_dict_add_subkey(DICT, KEY, SUBKEY, VALUE) -# -------------------------------------------- -m4_define([lt_dict_add_subkey], -[m4_define([$1($2:$3)], [$4])]) - - -# lt_dict_fetch(DICT, KEY, [SUBKEY]) -# ---------------------------------- -m4_define([lt_dict_fetch], -[m4_ifval([$3], - m4_ifdef([$1($2:$3)], [m4_defn([$1($2:$3)])]), - m4_ifdef([$1($2)], [m4_defn([$1($2)])]))]) - - -# lt_if_dict_fetch(DICT, KEY, [SUBKEY], VALUE, IF-TRUE, [IF-FALSE]) -# ----------------------------------------------------------------- -m4_define([lt_if_dict_fetch], -[m4_if(lt_dict_fetch([$1], [$2], [$3]), [$4], - [$5], - [$6])]) - - -# lt_dict_filter(DICT, [SUBKEY], VALUE, [SEPARATOR], KEY, [...]) -# -------------------------------------------------------------- -m4_define([lt_dict_filter], -[m4_if([$5], [], [], - [lt_join(m4_quote(m4_default([$4], [[, ]])), - lt_unquote(m4_split(m4_normalize(m4_foreach(_Lt_key, lt_car([m4_shiftn(4, $@)]), - [lt_if_dict_fetch([$1], _Lt_key, [$2], [$3], [_Lt_key ])])))))])[]dnl -]) - -# ltversion.m4 -- version numbers -*- Autoconf -*- -# -# Copyright (C) 2004, 2011-2015 Free Software Foundation, Inc. -# Written by Scott James Remnant, 2004 -# -# This file is free software; the Free Software Foundation gives -# unlimited permission to copy and/or distribute it, with or without -# modifications, as long as this notice is preserved. - -# @configure_input@ - -# serial 4179 ltversion.m4 -# This file is part of GNU Libtool - -m4_define([LT_PACKAGE_VERSION], [2.4.6]) -m4_define([LT_PACKAGE_REVISION], [2.4.6]) - -AC_DEFUN([LTVERSION_VERSION], -[macro_version='2.4.6' -macro_revision='2.4.6' -_LT_DECL(, macro_version, 0, [Which release of libtool.m4 was used?]) -_LT_DECL(, macro_revision, 0) -]) - -# lt~obsolete.m4 -- aclocal satisfying obsolete definitions. -*-Autoconf-*- -# -# Copyright (C) 2004-2005, 2007, 2009, 2011-2015 Free Software -# Foundation, Inc. -# Written by Scott James Remnant, 2004. -# -# This file is free software; the Free Software Foundation gives -# unlimited permission to copy and/or distribute it, with or without -# modifications, as long as this notice is preserved. - -# serial 5 lt~obsolete.m4 - -# These exist entirely to fool aclocal when bootstrapping libtool. -# -# In the past libtool.m4 has provided macros via AC_DEFUN (or AU_DEFUN), -# which have later been changed to m4_define as they aren't part of the -# exported API, or moved to Autoconf or Automake where they belong. -# -# The trouble is, aclocal is a bit thick. It'll see the old AC_DEFUN -# in /usr/share/aclocal/libtool.m4 and remember it, then when it sees us -# using a macro with the same name in our local m4/libtool.m4 it'll -# pull the old libtool.m4 in (it doesn't see our shiny new m4_define -# and doesn't know about Autoconf macros at all.) -# -# So we provide this file, which has a silly filename so it's always -# included after everything else. This provides aclocal with the -# AC_DEFUNs it wants, but when m4 processes it, it doesn't do anything -# because those macros already exist, or will be overwritten later. -# We use AC_DEFUN over AU_DEFUN for compatibility with aclocal-1.6. -# -# Anytime we withdraw an AC_DEFUN or AU_DEFUN, remember to add it here. -# Yes, that means every name once taken will need to remain here until -# we give up compatibility with versions before 1.7, at which point -# we need to keep only those names which we still refer to. - -# This is to help aclocal find these macros, as it can't see m4_define. -AC_DEFUN([LTOBSOLETE_VERSION], [m4_if([1])]) - -m4_ifndef([AC_LIBTOOL_LINKER_OPTION], [AC_DEFUN([AC_LIBTOOL_LINKER_OPTION])]) -m4_ifndef([AC_PROG_EGREP], [AC_DEFUN([AC_PROG_EGREP])]) -m4_ifndef([_LT_AC_PROG_ECHO_BACKSLASH], [AC_DEFUN([_LT_AC_PROG_ECHO_BACKSLASH])]) -m4_ifndef([_LT_AC_SHELL_INIT], [AC_DEFUN([_LT_AC_SHELL_INIT])]) -m4_ifndef([_LT_AC_SYS_LIBPATH_AIX], [AC_DEFUN([_LT_AC_SYS_LIBPATH_AIX])]) -m4_ifndef([_LT_PROG_LTMAIN], [AC_DEFUN([_LT_PROG_LTMAIN])]) -m4_ifndef([_LT_AC_TAGVAR], [AC_DEFUN([_LT_AC_TAGVAR])]) -m4_ifndef([AC_LTDL_ENABLE_INSTALL], [AC_DEFUN([AC_LTDL_ENABLE_INSTALL])]) -m4_ifndef([AC_LTDL_PREOPEN], [AC_DEFUN([AC_LTDL_PREOPEN])]) -m4_ifndef([_LT_AC_SYS_COMPILER], [AC_DEFUN([_LT_AC_SYS_COMPILER])]) -m4_ifndef([_LT_AC_LOCK], [AC_DEFUN([_LT_AC_LOCK])]) -m4_ifndef([AC_LIBTOOL_SYS_OLD_ARCHIVE], [AC_DEFUN([AC_LIBTOOL_SYS_OLD_ARCHIVE])]) -m4_ifndef([_LT_AC_TRY_DLOPEN_SELF], [AC_DEFUN([_LT_AC_TRY_DLOPEN_SELF])]) -m4_ifndef([AC_LIBTOOL_PROG_CC_C_O], [AC_DEFUN([AC_LIBTOOL_PROG_CC_C_O])]) -m4_ifndef([AC_LIBTOOL_SYS_HARD_LINK_LOCKS], [AC_DEFUN([AC_LIBTOOL_SYS_HARD_LINK_LOCKS])]) -m4_ifndef([AC_LIBTOOL_OBJDIR], [AC_DEFUN([AC_LIBTOOL_OBJDIR])]) -m4_ifndef([AC_LTDL_OBJDIR], [AC_DEFUN([AC_LTDL_OBJDIR])]) -m4_ifndef([AC_LIBTOOL_PROG_LD_HARDCODE_LIBPATH], [AC_DEFUN([AC_LIBTOOL_PROG_LD_HARDCODE_LIBPATH])]) -m4_ifndef([AC_LIBTOOL_SYS_LIB_STRIP], [AC_DEFUN([AC_LIBTOOL_SYS_LIB_STRIP])]) -m4_ifndef([AC_PATH_MAGIC], [AC_DEFUN([AC_PATH_MAGIC])]) -m4_ifndef([AC_PROG_LD_GNU], [AC_DEFUN([AC_PROG_LD_GNU])]) -m4_ifndef([AC_PROG_LD_RELOAD_FLAG], [AC_DEFUN([AC_PROG_LD_RELOAD_FLAG])]) -m4_ifndef([AC_DEPLIBS_CHECK_METHOD], [AC_DEFUN([AC_DEPLIBS_CHECK_METHOD])]) -m4_ifndef([AC_LIBTOOL_PROG_COMPILER_NO_RTTI], [AC_DEFUN([AC_LIBTOOL_PROG_COMPILER_NO_RTTI])]) -m4_ifndef([AC_LIBTOOL_SYS_GLOBAL_SYMBOL_PIPE], [AC_DEFUN([AC_LIBTOOL_SYS_GLOBAL_SYMBOL_PIPE])]) -m4_ifndef([AC_LIBTOOL_PROG_COMPILER_PIC], [AC_DEFUN([AC_LIBTOOL_PROG_COMPILER_PIC])]) -m4_ifndef([AC_LIBTOOL_PROG_LD_SHLIBS], [AC_DEFUN([AC_LIBTOOL_PROG_LD_SHLIBS])]) -m4_ifndef([AC_LIBTOOL_POSTDEP_PREDEP], [AC_DEFUN([AC_LIBTOOL_POSTDEP_PREDEP])]) -m4_ifndef([LT_AC_PROG_EGREP], [AC_DEFUN([LT_AC_PROG_EGREP])]) -m4_ifndef([LT_AC_PROG_SED], [AC_DEFUN([LT_AC_PROG_SED])]) -m4_ifndef([_LT_CC_BASENAME], [AC_DEFUN([_LT_CC_BASENAME])]) -m4_ifndef([_LT_COMPILER_BOILERPLATE], [AC_DEFUN([_LT_COMPILER_BOILERPLATE])]) -m4_ifndef([_LT_LINKER_BOILERPLATE], [AC_DEFUN([_LT_LINKER_BOILERPLATE])]) -m4_ifndef([_AC_PROG_LIBTOOL], [AC_DEFUN([_AC_PROG_LIBTOOL])]) -m4_ifndef([AC_LIBTOOL_SETUP], [AC_DEFUN([AC_LIBTOOL_SETUP])]) -m4_ifndef([_LT_AC_CHECK_DLFCN], [AC_DEFUN([_LT_AC_CHECK_DLFCN])]) -m4_ifndef([AC_LIBTOOL_SYS_DYNAMIC_LINKER], [AC_DEFUN([AC_LIBTOOL_SYS_DYNAMIC_LINKER])]) -m4_ifndef([_LT_AC_TAGCONFIG], [AC_DEFUN([_LT_AC_TAGCONFIG])]) -m4_ifndef([AC_DISABLE_FAST_INSTALL], [AC_DEFUN([AC_DISABLE_FAST_INSTALL])]) -m4_ifndef([_LT_AC_LANG_CXX], [AC_DEFUN([_LT_AC_LANG_CXX])]) -m4_ifndef([_LT_AC_LANG_F77], [AC_DEFUN([_LT_AC_LANG_F77])]) -m4_ifndef([_LT_AC_LANG_GCJ], [AC_DEFUN([_LT_AC_LANG_GCJ])]) -m4_ifndef([AC_LIBTOOL_LANG_C_CONFIG], [AC_DEFUN([AC_LIBTOOL_LANG_C_CONFIG])]) -m4_ifndef([_LT_AC_LANG_C_CONFIG], [AC_DEFUN([_LT_AC_LANG_C_CONFIG])]) -m4_ifndef([AC_LIBTOOL_LANG_CXX_CONFIG], [AC_DEFUN([AC_LIBTOOL_LANG_CXX_CONFIG])]) -m4_ifndef([_LT_AC_LANG_CXX_CONFIG], [AC_DEFUN([_LT_AC_LANG_CXX_CONFIG])]) -m4_ifndef([AC_LIBTOOL_LANG_F77_CONFIG], [AC_DEFUN([AC_LIBTOOL_LANG_F77_CONFIG])]) -m4_ifndef([_LT_AC_LANG_F77_CONFIG], [AC_DEFUN([_LT_AC_LANG_F77_CONFIG])]) -m4_ifndef([AC_LIBTOOL_LANG_GCJ_CONFIG], [AC_DEFUN([AC_LIBTOOL_LANG_GCJ_CONFIG])]) -m4_ifndef([_LT_AC_LANG_GCJ_CONFIG], [AC_DEFUN([_LT_AC_LANG_GCJ_CONFIG])]) -m4_ifndef([AC_LIBTOOL_LANG_RC_CONFIG], [AC_DEFUN([AC_LIBTOOL_LANG_RC_CONFIG])]) -m4_ifndef([_LT_AC_LANG_RC_CONFIG], [AC_DEFUN([_LT_AC_LANG_RC_CONFIG])]) -m4_ifndef([AC_LIBTOOL_CONFIG], [AC_DEFUN([AC_LIBTOOL_CONFIG])]) -m4_ifndef([_LT_AC_FILE_LTDLL_C], [AC_DEFUN([_LT_AC_FILE_LTDLL_C])]) -m4_ifndef([_LT_REQUIRED_DARWIN_CHECKS], [AC_DEFUN([_LT_REQUIRED_DARWIN_CHECKS])]) -m4_ifndef([_LT_AC_PROG_CXXCPP], [AC_DEFUN([_LT_AC_PROG_CXXCPP])]) -m4_ifndef([_LT_PREPARE_SED_QUOTE_VARS], [AC_DEFUN([_LT_PREPARE_SED_QUOTE_VARS])]) -m4_ifndef([_LT_PROG_ECHO_BACKSLASH], [AC_DEFUN([_LT_PROG_ECHO_BACKSLASH])]) -m4_ifndef([_LT_PROG_F77], [AC_DEFUN([_LT_PROG_F77])]) -m4_ifndef([_LT_PROG_FC], [AC_DEFUN([_LT_PROG_FC])]) -m4_ifndef([_LT_PROG_CXX], [AC_DEFUN([_LT_PROG_CXX])]) - diff --git a/art/icon-243x273.gif b/art/icon-243x273.gif new file mode 100644 index 0000000000..e1cdfd0b51 Binary files /dev/null and b/art/icon-243x273.gif differ diff --git a/art/icon-80x90.gif b/art/icon-80x90.gif new file mode 100644 index 0000000000..ebb2390005 Binary files /dev/null and b/art/icon-80x90.gif differ diff --git a/art/sqlite370.svg b/art/sqlite370.svg new file mode 100644 index 0000000000..9a050b593d --- /dev/null +++ b/art/sqlite370.svg @@ -0,0 +1,104 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/auto.def b/auto.def new file mode 100644 index 0000000000..214ef22304 --- /dev/null +++ b/auto.def @@ -0,0 +1,69 @@ +#!/do/not/tclsh +# ^^^ help out editors which guess this file's content type. +# +# This is the main autosetup-compatible configure script for the +# SQLite project. +# +# This script and all of its dependencies must be kept compatible with +# JimTCL, a copy of which is included in this source tree as +# ./autosetup/jimsh0.c. The number of incompatibilities between +# canonical TCL and JimTCL is very low and alternative formulations of +# incompatible constructs have, so far, been easy to find. +# +# JimTCL: https://jim.tcl.tk +# +# Code-diver notes: APIs names starting with "sqlite-" are specific to +# this project and can be found in autosetup/sqlite-config.tcl. Names +# starting with "proj-" are project-agnostic and found in +# autosetup/proj.tcl. +# +use sqlite-config +sqlite-configure canonical { + proj-if-opt-truthy dev { + # --enable-dev needs to come early so that the downstream tests + # which check for the following flags use their updated state. + proj-opt-set all 1 + proj-opt-set debug 1 + proj-opt-set amalgamation 0 + define CFLAGS [get-env CFLAGS {-O0 -g}] + # -------------^^^^^^^ intentionally using [get-env] instead of + # [proj-get-env] here because [sqlite-setup-default-cflags] uses + # [proj-get-env] and we want this to supercede that. + sqlite-munge-cflags; # straighten out -DSQLITE_ENABLE/OMIT flags + } + sqlite-handle-debug ;# must come after --dev flag check + sqlite-check-common-bins ;# must come before [sqlite-handle-wasi-sdk] + sqlite-handle-wasi-sdk ;# must run relatively early, as it changes the environment + sqlite-check-common-system-deps + + proj-define-for-opt amalgamation USE_AMALGAMATION "Use amalgamation for builds?" + + proj-define-for-opt gcov USE_GCOV "Use gcov?" + + proj-define-for-opt test-status TSTRNNR_OPTS \ + "test-runner flags:" {--status} {} + + proj-define-for-opt linemacros AMALGAMATION_LINE_MACROS \ + "Use #line macros in the amalgamation:" + + define AMALGAMATION_EXTRA_SRC \ + [join [opt-val amalgamation-extra-src ""] " "] + + define LINK_TOOLS_DYNAMICALLY [proj-opt-was-provided dynlink-tools] + + if {[set fsan [join [opt-val asan-fsanitize] ","]] in {auto ""}} { + set fsan address,bounds-strict + } + define CFLAGS_ASAN_FSANITIZE [proj-check-fsanitize [split $fsan ", "]] + + sqlite-handle-tcl + sqlite-handle-emsdk + + proj-if-opt-truthy static-shells { + proj-opt-set static-tclsqlite3 1 + proj-opt-set static-cli-shell 1 + } + proj-define-for-opt static-tclsqlite3 STATIC_TCLSQLITE3 "Statically link tclsqlite3?" + proj-define-for-opt static-cli-shell STATIC_CLI_SHELL "Statically link CLI shell?" + +}; # sqlite-configure diff --git a/autoconf/INSTALL b/autoconf/INSTALL deleted file mode 100644 index a1e89e18ad..0000000000 --- a/autoconf/INSTALL +++ /dev/null @@ -1,370 +0,0 @@ -Installation Instructions -************************* - -Copyright (C) 1994-1996, 1999-2002, 2004-2011 Free Software Foundation, -Inc. - - Copying and distribution of this file, with or without modification, -are permitted in any medium without royalty provided the copyright -notice and this notice are preserved. This file is offered as-is, -without warranty of any kind. - -Basic Installation -================== - - Briefly, the shell commands `./configure; make; make install' should -configure, build, and install this package. The following -more-detailed instructions are generic; see the `README' file for -instructions specific to this package. Some packages provide this -`INSTALL' file but do not implement all of the features documented -below. The lack of an optional feature in a given package is not -necessarily a bug. More recommendations for GNU packages can be found -in *note Makefile Conventions: (standards)Makefile Conventions. - - The `configure' shell script attempts to guess correct values for -various system-dependent variables used during compilation. It uses -those values to create a `Makefile' in each directory of the package. -It may also create one or more `.h' files containing system-dependent -definitions. Finally, it creates a shell script `config.status' that -you can run in the future to recreate the current configuration, and a -file `config.log' containing compiler output (useful mainly for -debugging `configure'). - - It can also use an optional file (typically called `config.cache' -and enabled with `--cache-file=config.cache' or simply `-C') that saves -the results of its tests to speed up reconfiguring. Caching is -disabled by default to prevent problems with accidental use of stale -cache files. - - If you need to do unusual things to compile the package, please try -to figure out how `configure' could check whether to do them, and mail -diffs or instructions to the address given in the `README' so they can -be considered for the next release. If you are using the cache, and at -some point `config.cache' contains results you don't want to keep, you -may remove or edit it. - - The file `configure.ac' (or `configure.in') is used to create -`configure' by a program called `autoconf'. You need `configure.ac' if -you want to change it or regenerate `configure' using a newer version -of `autoconf'. - - The simplest way to compile this package is: - - 1. `cd' to the directory containing the package's source code and type - `./configure' to configure the package for your system. - - Running `configure' might take a while. While running, it prints - some messages telling which features it is checking for. - - 2. Type `make' to compile the package. - - 3. Optionally, type `make check' to run any self-tests that come with - the package, generally using the just-built uninstalled binaries. - - 4. Type `make install' to install the programs and any data files and - documentation. When installing into a prefix owned by root, it is - recommended that the package be configured and built as a regular - user, and only the `make install' phase executed with root - privileges. - - 5. Optionally, type `make installcheck' to repeat any self-tests, but - this time using the binaries in their final installed location. - This target does not install anything. Running this target as a - regular user, particularly if the prior `make install' required - root privileges, verifies that the installation completed - correctly. - - 6. You can remove the program binaries and object files from the - source code directory by typing `make clean'. To also remove the - files that `configure' created (so you can compile the package for - a different kind of computer), type `make distclean'. There is - also a `make maintainer-clean' target, but that is intended mainly - for the package's developers. If you use it, you may have to get - all sorts of other programs in order to regenerate files that came - with the distribution. - - 7. Often, you can also type `make uninstall' to remove the installed - files again. In practice, not all packages have tested that - uninstallation works correctly, even though it is required by the - GNU Coding Standards. - - 8. Some packages, particularly those that use Automake, provide `make - distcheck', which can by used by developers to test that all other - targets like `make install' and `make uninstall' work correctly. - This target is generally not run by end users. - -Compilers and Options -===================== - - Some systems require unusual options for compilation or linking that -the `configure' script does not know about. Run `./configure --help' -for details on some of the pertinent environment variables. - - You can give `configure' initial values for configuration parameters -by setting variables in the command line or in the environment. Here -is an example: - - ./configure CC=c99 CFLAGS=-g LIBS=-lposix - - *Note Defining Variables::, for more details. - -Compiling For Multiple Architectures -==================================== - - You can compile the package for more than one kind of computer at the -same time, by placing the object files for each architecture in their -own directory. To do this, you can use GNU `make'. `cd' to the -directory where you want the object files and executables to go and run -the `configure' script. `configure' automatically checks for the -source code in the directory that `configure' is in and in `..'. This -is known as a "VPATH" build. - - With a non-GNU `make', it is safer to compile the package for one -architecture at a time in the source code directory. After you have -installed the package for one architecture, use `make distclean' before -reconfiguring for another architecture. - - On MacOS X 10.5 and later systems, you can create libraries and -executables that work on multiple system types--known as "fat" or -"universal" binaries--by specifying multiple `-arch' options to the -compiler but only a single `-arch' option to the preprocessor. Like -this: - - ./configure CC="gcc -arch i386 -arch x86_64 -arch ppc -arch ppc64" \ - CXX="g++ -arch i386 -arch x86_64 -arch ppc -arch ppc64" \ - CPP="gcc -E" CXXCPP="g++ -E" - - This is not guaranteed to produce working output in all cases, you -may have to build one architecture at a time and combine the results -using the `lipo' tool if you have problems. - -Installation Names -================== - - By default, `make install' installs the package's commands under -`/usr/local/bin', include files under `/usr/local/include', etc. You -can specify an installation prefix other than `/usr/local' by giving -`configure' the option `--prefix=PREFIX', where PREFIX must be an -absolute file name. - - You can specify separate installation prefixes for -architecture-specific files and architecture-independent files. If you -pass the option `--exec-prefix=PREFIX' to `configure', the package uses -PREFIX as the prefix for installing programs and libraries. -Documentation and other data files still use the regular prefix. - - In addition, if you use an unusual directory layout you can give -options like `--bindir=DIR' to specify different values for particular -kinds of files. Run `configure --help' for a list of the directories -you can set and what kinds of files go in them. In general, the -default for these options is expressed in terms of `${prefix}', so that -specifying just `--prefix' will affect all of the other directory -specifications that were not explicitly provided. - - The most portable way to affect installation locations is to pass the -correct locations to `configure'; however, many packages provide one or -both of the following shortcuts of passing variable assignments to the -`make install' command line to change installation locations without -having to reconfigure or recompile. - - The first method involves providing an override variable for each -affected directory. For example, `make install -prefix=/alternate/directory' will choose an alternate location for all -directory configuration variables that were expressed in terms of -`${prefix}'. Any directories that were specified during `configure', -but not in terms of `${prefix}', must each be overridden at install -time for the entire installation to be relocated. The approach of -makefile variable overrides for each directory variable is required by -the GNU Coding Standards, and ideally causes no recompilation. -However, some platforms have known limitations with the semantics of -shared libraries that end up requiring recompilation when using this -method, particularly noticeable in packages that use GNU Libtool. - - The second method involves providing the `DESTDIR' variable. For -example, `make install DESTDIR=/alternate/directory' will prepend -`/alternate/directory' before all installation names. The approach of -`DESTDIR' overrides is not required by the GNU Coding Standards, and -does not work on platforms that have drive letters. On the other hand, -it does better at avoiding recompilation issues, and works well even -when some directory options were not specified in terms of `${prefix}' -at `configure' time. - -Optional Features -================= - - If the package supports it, you can cause programs to be installed -with an extra prefix or suffix on their names by giving `configure' the -option `--program-prefix=PREFIX' or `--program-suffix=SUFFIX'. - - Some packages pay attention to `--enable-FEATURE' options to -`configure', where FEATURE indicates an optional part of the package. -They may also pay attention to `--with-PACKAGE' options, where PACKAGE -is something like `gnu-as' or `x' (for the X Window System). The -`README' should mention any `--enable-' and `--with-' options that the -package recognizes. - - For packages that use the X Window System, `configure' can usually -find the X include and library files automatically, but if it doesn't, -you can use the `configure' options `--x-includes=DIR' and -`--x-libraries=DIR' to specify their locations. - - Some packages offer the ability to configure how verbose the -execution of `make' will be. For these packages, running `./configure ---enable-silent-rules' sets the default to minimal output, which can be -overridden with `make V=1'; while running `./configure ---disable-silent-rules' sets the default to verbose, which can be -overridden with `make V=0'. - -Particular systems -================== - - On HP-UX, the default C compiler is not ANSI C compatible. If GNU -CC is not installed, it is recommended to use the following options in -order to use an ANSI C compiler: - - ./configure CC="cc -Ae -D_XOPEN_SOURCE=500" - -and if that doesn't work, install pre-built binaries of GCC for HP-UX. - - HP-UX `make' updates targets which have the same time stamps as -their prerequisites, which makes it generally unusable when shipped -generated files such as `configure' are involved. Use GNU `make' -instead. - - On OSF/1 a.k.a. Tru64, some versions of the default C compiler cannot -parse its `' header file. The option `-nodtk' can be used as -a workaround. If GNU CC is not installed, it is therefore recommended -to try - - ./configure CC="cc" - -and if that doesn't work, try - - ./configure CC="cc -nodtk" - - On Solaris, don't put `/usr/ucb' early in your `PATH'. This -directory contains several dysfunctional programs; working variants of -these programs are available in `/usr/bin'. So, if you need `/usr/ucb' -in your `PATH', put it _after_ `/usr/bin'. - - On Haiku, software installed for all users goes in `/boot/common', -not `/usr/local'. It is recommended to use the following options: - - ./configure --prefix=/boot/common - -Specifying the System Type -========================== - - There may be some features `configure' cannot figure out -automatically, but needs to determine by the type of machine the package -will run on. Usually, assuming the package is built to be run on the -_same_ architectures, `configure' can figure that out, but if it prints -a message saying it cannot guess the machine type, give it the -`--build=TYPE' option. TYPE can either be a short name for the system -type, such as `sun4', or a canonical name which has the form: - - CPU-COMPANY-SYSTEM - -where SYSTEM can have one of these forms: - - OS - KERNEL-OS - - See the file `config.sub' for the possible values of each field. If -`config.sub' isn't included in this package, then this package doesn't -need to know the machine type. - - If you are _building_ compiler tools for cross-compiling, you should -use the option `--target=TYPE' to select the type of system they will -produce code for. - - If you want to _use_ a cross compiler, that generates code for a -platform different from the build platform, you should specify the -"host" platform (i.e., that on which the generated programs will -eventually be run) with `--host=TYPE'. - -Sharing Defaults -================ - - If you want to set default values for `configure' scripts to share, -you can create a site shell script called `config.site' that gives -default values for variables like `CC', `cache_file', and `prefix'. -`configure' looks for `PREFIX/share/config.site' if it exists, then -`PREFIX/etc/config.site' if it exists. Or, you can set the -`CONFIG_SITE' environment variable to the location of the site script. -A warning: not all `configure' scripts look for a site script. - -Defining Variables -================== - - Variables not defined in a site shell script can be set in the -environment passed to `configure'. However, some packages may run -configure again during the build, and the customized values of these -variables may be lost. In order to avoid this problem, you should set -them in the `configure' command line, using `VAR=value'. For example: - - ./configure CC=/usr/local2/bin/gcc - -causes the specified `gcc' to be used as the C compiler (unless it is -overridden in the site shell script). - -Unfortunately, this technique does not work for `CONFIG_SHELL' due to -an Autoconf bug. Until the bug is fixed you can use this workaround: - - CONFIG_SHELL=/bin/bash /bin/bash ./configure CONFIG_SHELL=/bin/bash - -`configure' Invocation -====================== - - `configure' recognizes the following options to control how it -operates. - -`--help' -`-h' - Print a summary of all of the options to `configure', and exit. - -`--help=short' -`--help=recursive' - Print a summary of the options unique to this package's - `configure', and exit. The `short' variant lists options used - only in the top level, while the `recursive' variant lists options - also present in any nested packages. - -`--version' -`-V' - Print the version of Autoconf used to generate the `configure' - script, and exit. - -`--cache-file=FILE' - Enable the cache: use and save the results of the tests in FILE, - traditionally `config.cache'. FILE defaults to `/dev/null' to - disable caching. - -`--config-cache' -`-C' - Alias for `--cache-file=config.cache'. - -`--quiet' -`--silent' -`-q' - Do not print messages saying which checks are being made. To - suppress all normal output, redirect it to `/dev/null' (any error - messages will still be shown). - -`--srcdir=DIR' - Look for the package's source code in directory DIR. Usually - `configure' can determine that directory automatically. - -`--prefix=DIR' - Use DIR as the installation prefix. *note Installation Names:: - for more details, including other options available for fine-tuning - the installation locations. - -`--no-create' -`-n' - Run the configure checks, but stop before creating any output - files. - -`configure' also accepts some other, not widely useful, options. Run -`configure --help' for more details. - diff --git a/autoconf/Makefile.am b/autoconf/Makefile.am deleted file mode 100644 index 0e09cfcd95..0000000000 --- a/autoconf/Makefile.am +++ /dev/null @@ -1,20 +0,0 @@ - -AM_CFLAGS = @THREADSAFE_FLAGS@ @DYNAMIC_EXTENSION_FLAGS@ @FTS5_FLAGS@ @JSON1_FLAGS@ -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_RTREE - -lib_LTLIBRARIES = libsqlite3.la -libsqlite3_la_SOURCES = sqlite3.c -libsqlite3_la_LDFLAGS = -no-undefined -version-info 8:6:8 - -bin_PROGRAMS = sqlite3 -sqlite3_SOURCES = shell.c sqlite3.c sqlite3.h -sqlite3_LDADD = @READLINE_LIBS@ -sqlite3_DEPENDENCIES = @EXTRA_SHELL_OBJ@ -sqlite3_CFLAGS = $(AM_CFLAGS) -DSQLITE_ENABLE_EXPLAIN_COMMENTS - -include_HEADERS = sqlite3.h sqlite3ext.h - -EXTRA_DIST = sqlite3.1 tea Makefile.msc sqlite3.rc README.txt -pkgconfigdir = ${libdir}/pkgconfig -pkgconfig_DATA = sqlite3.pc - -man_MANS = sqlite3.1 diff --git a/autoconf/Makefile.fallback b/autoconf/Makefile.fallback new file mode 100644 index 0000000000..9355b147a8 --- /dev/null +++ b/autoconf/Makefile.fallback @@ -0,0 +1,19 @@ +#!/usr/bin/make +# +# If the configure script does not work, then this Makefile is available +# as a backup. Manually configure the variables below. +# +# Note: This makefile works out-of-the-box on MacOS 10.2 (Jaguar) +# +CC = gcc +CFLAGS = -O0 -I. +LIBS = -lz +COPTS += -D_BSD_SOURCE +COPTS += -DSQLITE_ENABLE_LOCKING_STYLE=0 +COPTS += -DSQLITE_THREADSAFE=0 +COPTS += -DSQLITE_OMIT_LOAD_EXTENSION +COPTS += -DSQLITE_WITHOUT_ZONEMALLOC +COPTS += -DSQLITE_ENABLE_RTREE + +sqlite3: shell.c sqlite3.c + $(CC) $(CFLAGS) $(COPTS) -o sqlite3 shell.c sqlite3.c $(LIBS) diff --git a/autoconf/Makefile.in b/autoconf/Makefile.in new file mode 100644 index 0000000000..c938ffe1bf --- /dev/null +++ b/autoconf/Makefile.in @@ -0,0 +1,314 @@ +######################################################################## +# This is a main makefile for the "autoconf" bundle of SQLite. This is +# a trimmed-down version of the canonical makefile, devoid of most +# documentation. For the full docs, see /main.mk in the canonical +# source tree. +# +# Maintenance reminders: +# +# - To keep this working with an out-of-tree build, be sure to prefix +# input file names with $(TOP)/ where appropriate (which is most +# places). +# +# - The original/canonical recipes can be found in /main.mk in the +# canonical source tree. +all: + +TOP = @abs_top_srcdir@ + +PACKAGE_VERSION = @PACKAGE_VERSION@ + +# +# Filename extensions for binaries and libraries +# +B.exe = @BUILD_EXEEXT@ +T.exe = @TARGET_EXEEXT@ +B.dll = @BUILD_DLLEXT@ +T.dll = @TARGET_DLLEXT@ +B.lib = @BUILD_LIBEXT@ +T.lib = @TARGET_LIBEXT@ + +# +# Autotools-compatibility dirs +# +prefix = @prefix@ +datadir = @datadir@ +mandir = @mandir@ +includedir = @includedir@ +exec_prefix = @exec_prefix@ +bindir = @bindir@ +libdir = @libdir@ + +# +# Required binaries +# +INSTALL = @BIN_INSTALL@ +AR = @AR@ +AR.flags = cr +CC = @CC@ + + +ENABLE_LIB_SHARED = @ENABLE_LIB_SHARED@ +ENABLE_LIB_STATIC = @ENABLE_LIB_STATIC@ +HAVE_WASI_SDK = @HAVE_WASI_SDK@ + +CFLAGS = @CFLAGS@ @CPPFLAGS@ +# +# $(LDFLAGS.configure) represents any LDFLAGS=... the client passes to +# configure. See main.mk. +# +LDFLAGS.configure = @LDFLAGS@ + +CFLAGS.core = @SH_CFLAGS@ +LDFLAGS.shlib = @SH_LDFLAGS@ +LDFLAGS.zlib = @LDFLAGS_ZLIB@ +LDFLAGS.math = @LDFLAGS_MATH@ +LDFLAGS.rpath = @LDFLAGS_RPATH@ +LDFLAGS.pthread = @LDFLAGS_PTHREAD@ +LDFLAGS.dlopen = @LDFLAGS_DLOPEN@ +LDFLAGS.readline = @LDFLAGS_READLINE@ +CFLAGS.readline = @CFLAGS_READLINE@ +LDFLAGS.rt = @LDFLAGS_RT@ +LDFLAGS.icu = @LDFLAGS_ICU@ +CFLAGS.icu = @CFLAGS_ICU@ + +# INSTALL reminder: we specifically do not strip binaries, +# as discussed in https://sqlite.org/forum/forumpost/9a67df63eda9925c. +INSTALL.noexec = $(INSTALL) -m 0644 + +install-dir.bin = $(DESTDIR)$(bindir) +install-dir.lib = $(DESTDIR)$(libdir) +install-dir.include = $(DESTDIR)$(includedir) +install-dir.pkgconfig = $(DESTDIR)$(libdir)/pkgconfig +install-dir.man1 = $(DESTDIR)$(mandir)/man1 +install-dir.all = $(install-dir.bin) $(install-dir.include) \ + $(install-dir.lib) $(install-dir.man1) \ + $(install-dir.pkgconfig) +$(install-dir.all): + @if [ ! -d "$@" ]; then set -x; $(INSTALL) -d "$@"; fi +# ^^^^ on some platforms, install -d fails if the target already exists. + + +# +# Vars with the AS_ prefix are specifically related to AutoSetup. +# +# AS_AUTO_DEF is the main configure script. +# +AS_AUTO_DEF = $(TOP)/auto.def + +# +# Shell commands to re-run $(TOP)/configure with the same args it was +# invoked with to produce this makefile. +# +AS_AUTORECONFIG = @SQLITE_AUTORECONFIG@ +Makefile: $(TOP)/Makefile.in $(AS_AUTO_DEF) + $(AS_AUTORECONFIG) + @touch $@ + +sqlite3.pc: $(TOP)/sqlite3.pc.in $(AS_AUTO_DEF) + $(AS_AUTORECONFIG) + @touch $@ + +sqlite_cfg.h: $(AS_AUTO_DEF) + $(AS_AUTORECONFIG) + @touch $@ + +# +# CFLAGS for sqlite3$(T.exe) +# +SHELL_OPT ?= @OPT_SHELL@ +SHELL_OPT += -DSQLITE_DQS=0 +SHELL_OPT += -DSQLITE_ENABLE_FTS4 +#SHELL_OPT += -DSQLITE_ENABLE_FTS5 +SHELL_OPT += -DSQLITE_ENABLE_RTREE +SHELL_OPT += -DSQLITE_ENABLE_EXPLAIN_COMMENTS +SHELL_OPT += -DSQLITE_ENABLE_UNKNOWN_SQL_FUNCTION +SHELL_OPT += -DSQLITE_ENABLE_STMTVTAB +SHELL_OPT += -DSQLITE_ENABLE_DBPAGE_VTAB +SHELL_OPT += -DSQLITE_ENABLE_DBSTAT_VTAB +SHELL_OPT += -DSQLITE_ENABLE_BYTECODE_VTAB +SHELL_OPT += -DSQLITE_ENABLE_OFFSET_SQL_FUNC +SHELL_OPT += -DSQLITE_ENABLE_PERCENTILE +SHELL_OPT += -DSQLITE_STRICT_SUBTYPE=1 + +# +# Library-level feature flags +# +OPT_FEATURE_FLAGS = @OPT_FEATURE_FLAGS@ + +LDFLAGS.libsqlite3.soname = @LDFLAGS_LIBSQLITE3_SONAME@ +# soname: see https://sqlite.org/src/forumpost/5a3b44f510df8ded +LDFLAGS.libsqlite3.os-specific = \ + @LDFLAGS_MAC_CVERSION@ @LDFLAGS_MAC_INSTALL_NAME@ @LDFLAGS_OUT_IMPLIB@ + +LDFLAGS.libsqlite3 = \ + $(LDFLAGS.rpath) $(LDFLAGS.pthread) \ + $(LDFLAGS.math) $(LDFLAGS.dlopen) \ + $(LDFLAGS.zlib) $(LDFLAGS.icu) \ + $(LDFLAGS.rt) $(LDFLAGS.configure) +CFLAGS.libsqlite3 = -I. $(CFLAGS.core) $(CFLAGS.icu) $(OPT_FEATURE_FLAGS) + +sqlite3.o: $(TOP)/sqlite3.h $(TOP)/sqlite3.c + $(CC) -c $(TOP)/sqlite3.c -o $@ $(CFLAGS) $(CFLAGS.libsqlite3) + +libsqlite3.LIB = libsqlite3$(T.lib) +libsqlite3.DLL.basename = @SQLITE_DLL_BASENAME@ +libsqlite3.out.implib = @SQLITE_OUT_IMPLIB@ +libsqlite3.DLL = $(libsqlite3.DLL.basename)$(T.dll) +libsqlite3.DLL.install-rules = @SQLITE_DLL_INSTALL_RULES@ + +$(libsqlite3.DLL): sqlite3.o + $(CC) -o $@ sqlite3.o $(LDFLAGS.shlib) \ + $(LDFLAGS) $(LDFLAGS.libsqlite3) \ + $(LDFLAGS.libsqlite3.os-specific) $(LDFLAGS.libsqlite3.soname) +$(libsqlite3.DLL)-1: $(libsqlite3.DLL) +$(libsqlite3.DLL)-0: +all: $(libsqlite3.DLL)-$(ENABLE_LIB_SHARED) + +$(libsqlite3.LIB): sqlite3.o + $(AR) $(AR.flags) $@ sqlite3.o +$(libsqlite3.LIB)-1: $(libsqlite3.LIB) +$(libsqlite3.LIB)-0: +all: $(libsqlite3.LIB)-$(ENABLE_LIB_STATIC) + +# +# Maintenance reminder: the install-dll-... rules must be kept in sync +# with the main copies rom /main.mk. +# +install-dll-out-implib: $(install-dir.lib) $(libsqlite3.DLL) + if [ x != "x$(libsqlite3.out.implib)" ] && [ -f "$(libsqlite3.out.implib)" ]; then \ + $(INSTALL) $(libsqlite3.out.implib) "$(install-dir.lib)"; \ + fi + +install-dll-unix-generic: install-dll-out-implib + $(INSTALL) $(libsqlite3.DLL) "$(install-dir.lib)" + @echo "Setting up $(libsqlite3.DLL) version symlinks..."; \ + cd "$(install-dir.lib)" || exit $$?; \ + rm -f $(libsqlite3.DLL).0 $(libsqlite3.DLL).$(PACKAGE_VERSION) || exit $$?; \ + mv $(libsqlite3.DLL) $(libsqlite3.DLL).$(PACKAGE_VERSION) || exit $$?; \ + ln -s $(libsqlite3.DLL).$(PACKAGE_VERSION) $(libsqlite3.DLL) || exit $$?; \ + ln -s $(libsqlite3.DLL).$(PACKAGE_VERSION) $(libsqlite3.DLL).0 || exit $$?; \ + ls -la $(libsqlite3.DLL) $(libsqlite3.DLL).[a03]*; \ + if [ -e $(libsqlite3.DLL).0.8.6 ]; then \ + echo "ACHTUNG: legacy libtool-compatible install found. Re-linking it..."; \ + rm -f libsqlite3.la $(libsqlite3.DLL).0.8.6 || exit $$?; \ + ln -s $(libsqlite3.DLL).$(PACKAGE_VERSION) $(libsqlite3.DLL).0.8.6 || exit $$?; \ + ls -la $(libsqlite3.DLL).0.8.6; \ + elif [ x1 = "x$(INSTALL_SO_086_LINK)" ]; then \ + echo "ACHTUNG: installing legacy libtool-style links because INSTALL_SO_086_LINK=1"; \ + rm -f libsqlite3.la $(libsqlite3.DLL).0.8.6 || exit $$?; \ + ln -s $(libsqlite3.DLL).$(PACKAGE_VERSION) $(libsqlite3.DLL).0.8.6 || exit $$?; \ + ls -la $(libsqlite3.DLL).0.8.6; \ + fi + +install-dll-msys: install-dll-out-implib $(install-dir.bin) + $(INSTALL) $(libsqlite3.DLL) "$(install-dir.bin)" +# ----------------------------------------------^^^ yes, bin +# Each of {msys,mingw,cygwin} uses a different name for the DLL, but +# that is already accounted for via $(libsqlite3.DLL). +install-dll-mingw: install-dll-msys +install-dll-cygwin: install-dll-msys + +install-dll-darwin: $(install-dir.lib) $(libsqlite3.DLL) + $(INSTALL) $(libsqlite3.DLL) "$(install-dir.lib)" + @echo "Setting up $(libsqlite3.DLL) version symlinks..."; \ + cd "$(install-dir.lib)" || exit $$?; \ + rm -f libsqlite3.0$(T.dll) libsqlite3.$(PACKAGE_VERSION)$(T.dll) || exit $$?; \ + dllname=libsqlite3.$(PACKAGE_VERSION)$(T.dll); \ + mv $(libsqlite3.DLL) $$dllname || exit $$?; \ + ln -s $$dllname $(libsqlite3.DLL) || exit $$?; \ + ln -s $$dllname libsqlite3.0$(T.dll) || exit $$?; \ + ls -la $$dllname $(libsqlite3.DLL) libsqlite3.0$(T.dll) + +install-dll-1: install-dll-$(libsqlite3.DLL.install-rules) +install-dll-0 install-dll-: +install-dll: install-dll-$(ENABLE_LIB_SHARED) +install: install-dll + +install-lib-1: $(install-dir.lib) $(libsqlite3.LIB) + $(INSTALL.noexec) $(libsqlite3.LIB) "$(install-dir.lib)" +install-lib-0 install-lib-: +install-lib: install-lib-$(ENABLE_LIB_STATIC) +install: install-lib + +# +# Flags to link the shell app either directly against sqlite3.c +# (ENABLE_STATIC_SHELL==1) or libsqlite3.so (ENABLE_STATIC_SHELL==0). +# +# Maintenance reminder: placement of $(LDFLAGS) is more relevant for +# some platforms than others: +# https://sqlite.org/forum/forumpost/d80ecdaddd +ENABLE_STATIC_SHELL = @ENABLE_STATIC_SHELL@ +sqlite3-shell-link-flags.1 = $(TOP)/sqlite3.c $(LDFLAGS) $(LDFLAGS.libsqlite3) +sqlite3-shell-link-flags.0 = $(LDFLAGS) -L. -lsqlite3 $(LDFLAGS.zlib) $(LDFLAGS.math) +sqlite3-shell-deps.1 = $(TOP)/sqlite3.c +sqlite3-shell-deps.0 = $(libsqlite3.DLL) +# +# STATIC_CLI_SHELL = 1 to statically link sqlite3$(T.exe), else +# 0. Requires static versions of all requisite libraries. Primarily +# intended for use with static-friendly environments like Alpine +# Linux. +# +STATIC_CLI_SHELL = @STATIC_CLI_SHELL@ +# +# sqlite3-shell-static.flags.N = N is $(STATIC_CLI_SHELL) +# +sqlite3-shell-static.flags.1 = -static +sqlite3-shell-static.flags.0 = +sqlite3$(T.exe): $(TOP)/shell.c $(sqlite3-shell-deps.$(ENABLE_STATIC_SHELL)) + $(CC) -o $@ \ + $(TOP)/shell.c $(sqlite3-shell-link-flags.$(ENABLE_STATIC_SHELL)) \ + $(sqlite3-shell-static.flags.$(STATIC_CLI_SHELL)) \ + -I. $(OPT_FEATURE_FLAGS) $(SHELL_OPT) \ + $(CFLAGS) $(CFLAGS.readline) $(CFLAGS.icu) \ + $(LDFLAGS.readline) + +sqlite3$(T.exe)-1: +sqlite3$(T.exe)-0: sqlite3$(T.exe) +all: sqlite3$(T.exe)-$(HAVE_WASI_SDK) + +install-shell-0: sqlite3$(T.exe) $(install-dir.bin) + $(INSTALL) sqlite3$(T.exe) "$(install-dir.bin)" +install-shell-1: +install: install-shell-$(HAVE_WASI_SDK) + +install-headers: $(TOP)/sqlite3.h $(install-dir.include) + $(INSTALL.noexec) $(TOP)/sqlite3.h $(TOP)/sqlite3ext.h "$(install-dir.include)" +install: install-headers + +install-pc: sqlite3.pc $(install-dir.pkgconfig) + $(INSTALL.noexec) sqlite3.pc "$(install-dir.pkgconfig)" +install: install-pc + +install-man1: $(TOP)/sqlite3.1 $(install-dir.man1) + $(INSTALL.noexec) $(TOP)/sqlite3.1 "$(install-dir.man1)" +install: install-man1 + +clean: + rm -f *.o sqlite3$(T.exe) + rm -f $(libsqlite3.LIB) $(libsqlite3.DLL) libsqlite3$(T.dll).a + +distclean: clean + rm -f jimsh0$(T.exe) config.* sqlite3.pc sqlite_cfg.h Makefile + +DIST_FILES := \ + README.txt VERSION \ + auto.def autosetup configure tea \ + sqlite3.h sqlite3.c shell.c sqlite3ext.h \ + Makefile.in Makefile.msc Makefile.fallback \ + sqlite3.rc sqlite3rc.h Replace.cs \ + sqlite3.pc.in sqlite3.1 + +# +# Maintenance note: dist_name must be sqlite-$(PACKAGE_VERSION) so +# that tool/mkautoconfamal.sh knows how to find it. +# +dist_name = sqlite-$(PACKAGE_VERSION) +dist_tarball = $(dist_name).tar.gz +dist: + rm -fr $(dist_name) + mkdir -p $(dist_name) + cp -rp $(DIST_FILES) $(dist_name)/. + tar czf $(dist_tarball) $(dist_name) + rm -fr $(dist_name) + ls -l $(dist_tarball) diff --git a/autoconf/Makefile.msc b/autoconf/Makefile.msc index 0be42b4e5f..365e1f0fb5 100644 --- a/autoconf/Makefile.msc +++ b/autoconf/Makefile.msc @@ -18,10 +18,40 @@ TOP = . +# Optionally set EXTRA_SRC to a list of C files to append to +# the generated sqlite3.c. Any sqlite3 extensions added this +# way may require manual editing, as described in +# https://sqlite.org/forum/forumpost/903f721f3e7c0d25 +# +!IFNDEF EXTRA_SRC +EXTRA_SRC = +!ENDIF + # Set this non-0 to enable full warnings (-W4, etc) when compiling. # !IFNDEF USE_FULLWARN -USE_FULLWARN = 0 +USE_FULLWARN = 1 +!ENDIF + +# Set this non-0 to enable treating warnings as errors (-WX, etc) when +# compiling. +# +!IFNDEF USE_FATAL_WARN +USE_FATAL_WARN = 0 +!ENDIF + +# Set this non-0 to enable full runtime error checks (-RTC1, etc). This +# has no effect if (any) optimizations are enabled. +# +!IFNDEF USE_RUNTIME_CHECKS +USE_RUNTIME_CHECKS = 0 +!ENDIF + +# Set this non-0 to create a SQLite amalgamation file that excludes the +# various built-in extensions. +# +!IFNDEF MINIMAL_AMALGAMATION +MINIMAL_AMALGAMATION = 0 !ENDIF # Set this non-0 to use "stdcall" calling convention for the core library @@ -31,6 +61,21 @@ USE_FULLWARN = 0 USE_STDCALL = 0 !ENDIF +# Use the USE_SEH=0 option on the nmake command line to omit structured +# exception handling (SEH) support. SEH is on by default. +# +!IFNDEF USE_SEH +USE_SEH = 1 +!ENDIF + +# Use STATICALLY_LINK_TCL=1 to statically link against TCL +# +!IFNDEF STATICALLY_LINK_TCL +STATICALLY_LINK_TCL = 0 +!ELSEIF $(STATICALLY_LINK_TCL)!=0 +CCOPTS = $(CCOPTS) -DSTATIC_BUILD +!ENDIF + # Set this non-0 to have the shell executable link against the core dynamic # link library. # @@ -52,7 +97,7 @@ API_ARMOR = 0 !IFNDEF NO_WARN !IF $(USE_FULLWARN)!=0 NO_WARN = -wd4054 -wd4055 -wd4100 -wd4127 -wd4130 -wd4152 -wd4189 -wd4206 -NO_WARN = $(NO_WARN) -wd4210 -wd4232 -wd4305 -wd4306 -wd4702 -wd4706 +NO_WARN = $(NO_WARN) -wd4210 -wd4232 -wd4244 -wd4305 -wd4306 -wd4702 -wd4706 !ENDIF !ENDIF @@ -159,6 +204,12 @@ WIN32HEAP = 0 OSTRACE = 0 !ENDIF +# enable address sanitizer using ASAN=1 on the command-line. +# +!IFNDEF ASAN +ASAN = 0 +!ENDIF + # Set this to one of the following values to enable various debugging # features. Each level includes the debugging options from the previous # levels. Currently, the recognized values for DEBUG are: @@ -175,6 +226,7 @@ OSTRACE = 0 DEBUG = 0 !ENDIF + # Enable use of available compiler optimizations? Normally, this should be # non-zero. Setting this to zero, thus disabling all compiler optimizations, # can be useful for testing. @@ -183,6 +235,24 @@ DEBUG = 0 OPTIMIZATIONS = 2 !ENDIF +# Set this to non-0 to enable support for the session extension. +# +!IFNDEF SESSION +SESSION = 0 +!ENDIF + +# Set this to non-0 to enable support for the rbu extension. +# +!IFNDEF RBU +RBU = 0 +!ENDIF + +# Set this to non-0 to enable support for blocking locks. +# +!IFNDEF SETLK_TIMEOUT +SETLK_TIMEOUT = 0 +!ENDIF + # Set the source code file to be used by executables and libraries when # they need the amalgamation. # @@ -204,37 +274,99 @@ SQLITE3H = sqlite3.h # This is the name to use for the SQLite dynamic link library (DLL). # !IFNDEF SQLITE3DLL +!IF $(FOR_WIN10)!=0 +SQLITE3DLL = winsqlite3.dll +!ELSE SQLITE3DLL = sqlite3.dll !ENDIF +!ENDIF # This is the name to use for the SQLite import library (LIB). # !IFNDEF SQLITE3LIB +!IF $(FOR_WIN10)!=0 +SQLITE3LIB = winsqlite3.lib +!ELSE SQLITE3LIB = sqlite3.lib !ENDIF +!ENDIF # This is the name to use for the SQLite shell executable (EXE). # !IFNDEF SQLITE3EXE +!IF $(FOR_WIN10)!=0 +SQLITE3EXE = winsqlite3shell.exe +!ELSE SQLITE3EXE = sqlite3.exe !ENDIF +!ENDIF # This is the argument used to set the program database (PDB) file for the # SQLite shell executable (EXE). # !IFNDEF SQLITE3EXEPDB +!IF $(FOR_WIN10)!=0 +SQLITE3EXEPDB = +!ELSE SQLITE3EXEPDB = /pdb:sqlite3sh.pdb !ENDIF +!ENDIF + # These are the "standard" SQLite compilation options used when compiling for # the Windows platform. # !IFNDEF OPT_FEATURE_FLAGS +OPT_FEATURE_FLAGS = $(OPT_XTRA) +!IF $(MINIMAL_AMALGAMATION)==0 OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_FTS3=1 +OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_FTS5=1 OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_RTREE=1 +OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_GEOPOLY=1 +OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_STMTVTAB=1 +OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_DBPAGE_VTAB=1 +OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_DBSTAT_VTAB=1 +OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_BYTECODE_VTAB=1 +OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_CARRAY=1 +!ENDIF OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_COLUMN_METADATA=1 !ENDIF +# Additional feature-options above and beyond what are normally used can be +# be added using OPTIONS=.... on the command-line. These values are +# appended to the OPT_FEATURE_FLAGS variable. +# +!IFDEF OPTIONS +OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) $(OPTIONS) +!ENDIF + +# Should the session extension be enabled? If so, add compilation options +# to enable it. +# +!IF $(SESSION)!=0 +OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_SESSION=1 +OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_PREUPDATE_HOOK=1 +!ENDIF + +# Always enable math functions on Windows +OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_MATH_FUNCTIONS +OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_PERCENTILE + +# Should the rbu extension be enabled? If so, add compilation options +# to enable it. +# +!IF $(RBU)!=0 +OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_RBU=1 +!ENDIF + +# Should structured exception handling (SEH) be enabled for WAL mode in +# the core library? It is on by default. Only omit it if the +# USE_SEH=0 option is provided on the nmake command-line. +# +!IF $(USE_SEH)==0 +OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_OMIT_SEH=1 +!ENDIF + # These are the "extended" SQLite compilation options used when compiling for # the Windows 10 platform. # @@ -248,6 +380,10 @@ EXT_FEATURE_FLAGS = !ENDIF !ENDIF +!IF $(SETLK_TIMEOUT)!=0 +OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_SETLK_TIMEOUT +!ENDIF + ############################################################################### ############################### END OF OPTIONS ################################ ############################################################################### @@ -275,6 +411,14 @@ PROGRAMFILES_X86 = $(PROGRAMFILES_X86:\\=\) CC = cl.exe !ENDIF +# Check for the predefined command macro CSC. This should point to a working +# C Sharp compiler binary. If it is not defined, simply define it to the +# legacy default value 'csc.exe'. +# +!IFNDEF CSC +CSC = csc.exe +!ENDIF + # Check for the command macro LD. This should point to the linker binary for # the target platform. If it is not defined, simply define it to the legacy # default value 'link.exe'. @@ -364,9 +508,9 @@ UCRTLIBPATH = $(UCRTLIBPATH:\\=\) # will run on the platform that is doing the build. # !IF $(USE_FULLWARN)!=0 -BCC = $(NCC) -nologo -W4 $(CCOPTS) $(BCCOPTS) +BCC = $(NCC) -nologo -W4 -Fd$*.pdb $(CCOPTS) $(BCCOPTS) !ELSE -BCC = $(NCC) -nologo -W3 $(CCOPTS) $(BCCOPTS) +BCC = $(NCC) -nologo -W3 -Fd$*.pdb $(CCOPTS) $(BCCOPTS) !ENDIF # Check if assembly code listings should be generated for the source @@ -399,18 +543,15 @@ TCC = $(CC) -nologo -W4 -DINCLUDE_MSVC_H=1 $(CCOPTS) $(TCCOPTS) TCC = $(CC) -nologo -W3 $(CCOPTS) $(TCCOPTS) !ENDIF -TCC = $(TCC) -DSQLITE_OS_WIN=1 -I. -I$(TOP) -fp:precise -RCC = $(RC) -DSQLITE_OS_WIN=1 -I. -I$(TOP) $(RCOPTS) $(RCCOPTS) - -# Adjust the names of the primary targets for use with Windows 10. +# Check if warnings should be treated as errors when compiling. # -!IF $(FOR_WIN10)!=0 -SQLITE3DLL = winsqlite3.dll -SQLITE3LIB = winsqlite3.lib -SQLITE3EXE = winsqlite3shell.exe -SQLITE3EXEPDB = +!IF $(USE_FATAL_WARN)!=0 +TCC = $(TCC) -WX !ENDIF +TCC = $(TCC) -DSQLITE_OS_WIN=1 -I. -I$(TOP) -fp:precise +RCC = $(RC) -DSQLITE_OS_WIN=1 -I. -I$(TOP) $(RCOPTS) $(RCCOPTS) + # Check if we want to use the "stdcall" calling convention when compiling. # This is not supported by the compilers for non-x86 platforms. It should # also be noted here that building any target with these "stdcall" options @@ -420,12 +561,12 @@ SQLITE3EXEPDB = # !IF $(USE_STDCALL)!=0 || $(FOR_WIN10)!=0 !IF "$(PLATFORM)"=="x86" -CORE_CCONV_OPTS = -Gz -DSQLITE_CDECL=__cdecl -DSQLITE_STDCALL=__stdcall -SHELL_CCONV_OPTS = -Gz -DSQLITE_CDECL=__cdecl -DSQLITE_STDCALL=__stdcall +CORE_CCONV_OPTS = -Gz -guard:cf -DSQLITE_CDECL=__cdecl -DSQLITE_APICALL=__stdcall -DSQLITE_CALLBACK=__stdcall -DSQLITE_SYSAPI=__stdcall +SHELL_CCONV_OPTS = -Gz -guard:cf -DSQLITE_CDECL=__cdecl -DSQLITE_APICALL=__stdcall -DSQLITE_CALLBACK=__stdcall -DSQLITE_SYSAPI=__stdcall !ELSE !IFNDEF PLATFORM -CORE_CCONV_OPTS = -Gz -DSQLITE_CDECL=__cdecl -DSQLITE_STDCALL=__stdcall -SHELL_CCONV_OPTS = -Gz -DSQLITE_CDECL=__cdecl -DSQLITE_STDCALL=__stdcall +CORE_CCONV_OPTS = -Gz -guard:cf -DSQLITE_CDECL=__cdecl -DSQLITE_APICALL=__stdcall -DSQLITE_CALLBACK=__stdcall -DSQLITE_SYSAPI=__stdcall +SHELL_CCONV_OPTS = -Gz -guard:cf -DSQLITE_CDECL=__cdecl -DSQLITE_APICALL=__stdcall -DSQLITE_CALLBACK=__stdcall -DSQLITE_SYSAPI=__stdcall !ELSE CORE_CCONV_OPTS = SHELL_CCONV_OPTS = @@ -450,8 +591,10 @@ CORE_COMPILE_OPTS = $(CORE_CCONV_OPTS) # when linking. # !IFNDEF CORE_LINK_DEP -!IF $(DYNAMIC_SHELL)!=0 || $(FOR_WIN10)!=0 +!IF $(DYNAMIC_SHELL)!=0 CORE_LINK_DEP = +!ELSEIF $(FOR_WIN10)==0 || "$(PLATFORM)"=="x86" +CORE_LINK_DEP = sqlite3.def !ELSE CORE_LINK_DEP = !ENDIF @@ -460,8 +603,10 @@ CORE_LINK_DEP = # These are additional linker options used for the core library. # !IFNDEF CORE_LINK_OPTS -!IF $(DYNAMIC_SHELL)!=0 || $(FOR_WIN10)!=0 +!IF $(DYNAMIC_SHELL)!=0 CORE_LINK_OPTS = +!ELSEIF $(FOR_WIN10)==0 || "$(PLATFORM)"=="x86" +CORE_LINK_OPTS = /DEF:sqlite3.def !ELSE CORE_LINK_OPTS = !ENDIF @@ -498,6 +643,7 @@ SHELL_CORE_DEP = !ENDIF !ENDIF + # This is the core library that the shell executable should link with. # !IFNDEF SHELL_CORE_LIB @@ -549,17 +695,21 @@ BCC = $(BCC) /d2guard4 -D_ARM_WINAPI_PARTITION_DESKTOP_SDK_AVAILABLE !IF $(DEBUG)>1 TCC = $(TCC) -MDd BCC = $(BCC) -MDd +ZLIBCFLAGS = -nologo -MDd -W3 -O2 -Oy- -Zi !ELSE TCC = $(TCC) -MD BCC = $(BCC) -MD +ZLIBCFLAGS = -nologo -MD -W3 -O2 -Oy- -Zi !ENDIF !ELSE !IF $(DEBUG)>1 TCC = $(TCC) -MTd BCC = $(BCC) -MTd +ZLIBCFLAGS = -nologo -MTd -W3 -O2 -Oy- -Zi !ELSE TCC = $(TCC) -MT BCC = $(BCC) -MT +ZLIBCFLAGS = -nologo -MT -W3 -O2 -Oy- -Zi !ENDIF !ENDIF @@ -580,8 +730,12 @@ RCC = $(RCC) -DSQLITE_ENABLE_API_ARMOR=1 !ENDIF !IF $(DEBUG)>2 -TCC = $(TCC) -DSQLITE_DEBUG=1 +TCC = $(TCC) -DSQLITE_DEBUG=1 -DSQLITE_USE_W32_FOR_CONSOLE_IO RCC = $(RCC) -DSQLITE_DEBUG=1 +!IF $(DYNAMIC_SHELL)==0 +TCC = $(TCC) -DSQLITE_ENABLE_WHERETRACE -DSQLITE_ENABLE_SELECTTRACE +RCC = $(RCC) -DSQLITE_ENABLE_WHERETRACE -DSQLITE_ENABLE_SELECTTRACE +!ENDIF !ENDIF !IF $(DEBUG)>4 || $(OSTRACE)!=0 @@ -628,6 +782,13 @@ RCC = $(RCC) -DSQLITE_WIN32_MALLOC_VALIDATE=1 !ENDIF +# Address sanitizer if ASAN=1 +# +!IF $(ASAN)>0 +TCC = $(TCC) /fsanitize=address +!ENDIF + + # Compiler options needed for programs that use the readline() library. # !IFNDEF READLINE_FLAGS @@ -656,15 +817,6 @@ RCC = $(RCC) -DSQLITE_THREAD_OVERRIDE_LOCK=-1 TLIBS = !ENDIF -# Flags controlling use of the in memory btree implementation -# -# SQLITE_TEMP_STORE is 0 to force temporary tables to be in a file, 1 to -# default to file, 2 to default to memory, and 3 to force temporary -# tables to always be in memory. -# -TCC = $(TCC) -DSQLITE_TEMP_STORE=1 -RCC = $(RCC) -DSQLITE_TEMP_STORE=1 - # Enable/disable loadable extensions, and other optional features # based on configuration. (-DSQLITE_OMIT*, -DSQLITE_ENABLE*). # The same set of OMIT and ENABLE flags should be passed to the @@ -707,6 +859,10 @@ RCC = $(RCC) -D_DEBUG !IF $(DEBUG)>1 || $(OPTIMIZATIONS)==0 TCC = $(TCC) -Od BCC = $(BCC) -Od +!IF $(USE_RUNTIME_CHECKS)!=0 +TCC = $(TCC) -RTC1 +BCC = $(BCC) -RTC1 +!ENDIF !ELSEIF $(OPTIMIZATIONS)>=3 TCC = $(TCC) -Ox BCC = $(BCC) -Ox @@ -729,7 +885,7 @@ BCC = $(BCC) -Zi # Command line prefixes for compiling code, compiling resources, # linking, etc. # -LTCOMPILE = $(TCC) -Fo$@ +LTCOMPILE = $(TCC) -Fo$@ -Fd$*.pdb LTRCOMPILE = $(RCC) -r LTLIB = lib.exe LTLINK = $(TCC) -Fe$@ @@ -737,7 +893,7 @@ LTLINK = $(TCC) -Fe$@ # If requested, link to the RPCRT4 library. # !IF $(USE_RPCRT4_LIB)!=0 -LTLINK = $(LTLINK) rpcrt4.lib +LTLIBS = $(LTLIBS) rpcrt4.lib !ENDIF # If a platform was set, force the linker to target that. @@ -747,6 +903,11 @@ LTLINK = $(LTLINK) rpcrt4.lib !IFDEF PLATFORM LTLINKOPTS = /NOLOGO /MACHINE:$(PLATFORM) LTLIBOPTS = /NOLOGO /MACHINE:$(PLATFORM) +!ELSEIF "$(VISUALSTUDIOVERSION)"=="12.0" || \ + "$(VISUALSTUDIOVERSION)"=="14.0" || \ + "$(VISUALSTUDIOVERSION)"=="15.0" +LTLINKOPTS = /NOLOGO /MACHINE:x86 +LTLIBOPTS = /NOLOGO /MACHINE:x86 !ELSE LTLINKOPTS = /NOLOGO LTLIBOPTS = /NOLOGO @@ -856,33 +1017,58 @@ LIBRESOBJS = # when the shell is not being dynamically linked. # !IF $(DYNAMIC_SHELL)==0 && $(FOR_WIN10)==0 -SHELL_COMPILE_OPTS = $(SHELL_COMPILE_OPTS) -DSQLITE_SHELL_JSON1 -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_EXPLAIN_COMMENTS +SHELL_COMPILE_OPTS = $(SHELL_COMPILE_OPTS) -DSQLITE_DQS=0 +SHELL_COMPILE_OPTS = $(SHELL_COMPILE_OPTS) -DSQLITE_ENABLE_FTS4=1 +SHELL_COMPILE_OPTS = $(SHELL_COMPILE_OPTS) -DSQLITE_ENABLE_EXPLAIN_COMMENTS=1 +SHELL_COMPILE_OPTS = $(SHELL_COMPILE_OPTS) -DSQLITE_ENABLE_OFFSET_SQL_FUNC=1 +SHELL_COMPILE_OPTS = $(SHELL_COMPILE_OPTS) -DSQLITE_ENABLE_PERCENTILE=1 +SHELL_COMPILE_OPTS = $(SHELL_COMPILE_OPTS) -DSQLITE_ENABLE_UNKNOWN_SQL_FUNCTION=1 +SHELL_COMPILE_OPTS = $(SHELL_COMPILE_OPTS) -DSQLITE_ENABLE_STMT_SCANSTATUS=1 +SHELL_COMPILE_OPTS = $(SHELL_COMPILE_OPTS) -DSQLITE_STRICT_SUBTYPE=1 !ENDIF # This is the default Makefile target. The objects listed here # are what get build when you type just "make" with no arguments. # -all: dll libsqlite3.lib shell +core: dll shell + +# Targets that require the Tcl library. +# +tcl: $(ALL_TCL_TARGETS) + +# This Makefile target builds all of the standard binaries. +# +all: core tcl # Dynamic link library section. # -dll: $(SQLITE3DLL) +dll: $(SQLITE3DLL) # Shell executable. # -shell: $(SQLITE3EXE) +shell: $(SQLITE3EXE) -libsqlite3.lib: $(LIBOBJ) - $(LTLIB) $(LTLIBOPTS) /OUT:$@ $(LIBOBJ) $(TLIBS) +# jimsh0 - replacement for tclsh +# +jimsh0.exe: $(TOP)\autosetup\jimsh0.c + cl -DHAVE__FULLPATH=1 $(TOP)\autosetup\jimsh0.c -$(SQLITE3DLL): $(LIBOBJ) $(LIBRESOBJS) $(CORE_LINK_DEP) +$(SQLITE3DLL): $(LIBOBJ) $(LIBRESOBJS) $(CORE_LINK_DEP) $(LD) $(LDFLAGS) $(LTLINKOPTS) $(LTLIBPATHS) /DLL $(CORE_LINK_OPTS) /OUT:$@ $(LIBOBJ) $(LIBRESOBJS) $(LTLIBS) $(TLIBS) +Replace.exe: + $(CSC) /target:exe $(TOP)\Replace.cs + +sqlite3.def: Replace.exe $(LIBOBJ) + echo EXPORTS > sqlite3.def + dumpbin /all $(LIBOBJ) \ + | .\Replace.exe "^\s+/EXPORT:_?(sqlite3(?:session|changeset|changegroup|rebaser|rbu)?_[^@,]*)(?:@\d+|,DATA)?$$" $$1 true \ + | sort >> sqlite3.def -$(SQLITE3EXE): $(TOP)\shell.c $(SHELL_CORE_DEP) $(LIBRESOBJS) $(SHELL_CORE_SRC) $(SQLITE3H) - $(LTLINK) $(SHELL_COMPILE_OPTS) $(READLINE_FLAGS) $(TOP)\shell.c $(SHELL_CORE_SRC) \ +$(SQLITE3EXE): shell.c $(SHELL_CORE_DEP) $(LIBRESOBJS) $(SHELL_CORE_SRC) $(SQLITE3H) + $(LTLINK) $(SHELL_COMPILE_OPTS) $(READLINE_FLAGS) shell.c $(SHELL_CORE_SRC) \ /link $(SQLITE3EXEPDB) $(LDFLAGS) $(LTLINKOPTS) $(SHELL_LINK_OPTS) $(LTLIBPATHS) $(LIBRESOBJS) $(LIBREADLINE) $(LTLIBS) $(TLIBS) @@ -897,7 +1083,7 @@ sqlite3.lo: $(SQLITE3C) !IF $(USE_RC)!=0 _HASHCHAR=^# !IF ![echo !IFNDEF VERSION > rcver.vc] && \ - ![for /F "delims=" %V in ('type "$(SQLITE3H)" ^| find "$(_HASHCHAR)define SQLITE_VERSION "') do (echo VERSION = ^^%V >> rcver.vc)] && \ + ![for /F "delims=" %V in ('type "$(SQLITE3H)" ^| "%SystemRoot%\System32\find.exe" "$(_HASHCHAR)define SQLITE_VERSION "') do (echo VERSION = ^^%V >> rcver.vc)] && \ ![echo !ENDIF >> rcver.vc] !INCLUDE rcver.vc !ENDIF @@ -919,3 +1105,5 @@ $(LIBRESOBJS): $(TOP)\sqlite3.rc rcver.vc $(SQLITE3H) clean: del /Q *.exp *.lo *.ilk *.lib *.obj *.ncb *.pdb *.sdf *.suo 2>NUL del /Q *.bsc *.cod *.da *.bb *.bbg *.vc gmon.out 2>NUL + del /Q sqlite3.def tclsqlite3.def ctime.c pragma.h 2>NUL + del /Q $(SQLITE3EXE) $(SQLITE3DLL) Replace.exe 2>NUL diff --git a/autoconf/README.first b/autoconf/README.first index 5c2ea0a70f..75c4a76d61 100644 --- a/autoconf/README.first +++ b/autoconf/README.first @@ -1,11 +1,12 @@ -This directory contains components use to build an autoconf-ready package -of the SQLite amalgamation: sqlite-autoconf-30XXXXXX.tar.gz +This directory contains components used to build an autoconf-like +package of the SQLite amalgamation: sqlite-autoconf-30XXXXXX.tar.gz -To build the autoconf amalgamation, run from the top-level: +To build the autoconf amalgamation, run from the top of the canonical +source tree: ./configure make amalgamation-tarball -The amalgamation-tarball target (also available in "main.mk") runs the -script tool/mkautoconfamal.sh which does the work. Refer to that script -for details. +The amalgamation-tarball target (available in "main.mk") runs the +script tool/mkautoconfamal.sh which does the work. Refer to that +script for details. diff --git a/autoconf/README.txt b/autoconf/README.txt index 2d6db4f07f..ca0ed20fd4 100644 --- a/autoconf/README.txt +++ b/autoconf/README.txt @@ -4,13 +4,44 @@ This package contains: * the sqlite3.h and sqlite3ext.h header files that define the C-language interface to the sqlite3.c library file * the shell.c file used to build the sqlite3 command-line shell program - * autoconf/automake installation infrastucture for building on POSIX + * autoconf-like installation infrastucture for building on POSIX compliant systems - * a Makefile.msc and sqlite3.rc for building with Microsoft Visual C++ on - Windows + * a Makefile.msc, sqlite3.rc, and Replace.cs for building with Microsoft + Visual C++ on Windows -SUMMARY OF HOW TO BUILD -======================= +WHY USE THIS PACKAGE? +===================== + +The canonical make system for SQLite requires TCL as part of the build +process. Various TCL scripts are used to generate parts of the code and +TCL is used to run tests. But some people would prefer to build SQLite +using only generic tools and without having to install TCL. The purpose +of this package is to provide that capability. + +This package contains a pre-build SQLite amalgamation file "sqlite3.c" +(and its associated header file "sqlite3.h"). Because the +amalgamation has been pre-built, no TCL is required for the code +generate (the configure script itself is written in TCL but it can use +the embedded copy of JimTCL). + +REASONS TO USE THE CANONICAL BUILD SYSTEM RATHER THAN THIS PACKAGE +================================================================== + + * the canonical build system allows you to run tests to verify that + the build worked + * the canonical build system supports more compile-time options + * the canonical build system works for any arbitrary check-in to + the SQLite source tree + +Step-by-step instructions on how to build using the canonical make +system for SQLite can be found at: + + https://sqlite.org/src/doc/trunk/doc/compile-for-unix.md + https://sqlite.org/src/doc/trunk/doc/compile-for-windows.md + + +SUMMARY OF HOW TO BUILD USING THIS PACKAGE +========================================== Unix: ./configure; make Windows: nmake /f Makefile.msc @@ -18,14 +49,12 @@ SUMMARY OF HOW TO BUILD BUILDING ON POSIX ================= -The generic installation instructions for autoconf/automake are found -in the INSTALL file. - -The following SQLite specific boolean options are supported: +The configure script follows common conventions, making it easy +to use for anyone who has configured a software tree before. +It supports a number of build-time flags, the full list of which +can be seen by running: - --enable-readline use readline in shell tool [default=yes] - --enable-threadsafe build a thread-safe library [default=yes] - --enable-dynamic-extensions support loadable extensions [default=yes] + ./configure --help The default value for the CFLAGS variable (options passed to the C compiler) includes debugging symbols in the build, resulting in larger @@ -36,10 +65,11 @@ line like this: to produce a smaller installation footprint. -Other SQLite compilation parameters can also be set using CFLAGS. For +Many SQLite compilation parameters can be defined by passing flags +to the configure script. Others may be passed on in the CFLAGS. For example: - $ CFLAGS="-Os -DSQLITE_THREADSAFE=0" ./configure + $ CFLAGS="-Os -DSQLITE_OMIT_DEPRECATED" ./configure BUILDING WITH MICROSOFT VISUAL C++ @@ -53,48 +83,6 @@ Using Microsoft Visual C++ 2005 (or later) is recommended. Several Windows platform variants may be built by adding additional macros to the NMAKE command line. -Building for WinRT 8.0 ----------------------- - - FOR_WINRT=1 - -Using Microsoft Visual C++ 2012 (or later) is required. When using the -above, something like the following macro will need to be added to the -NMAKE command line as well: - - "NSDKLIBPATH=%WindowsSdkDir%\..\8.0\lib\win8\um\x86" - -Building for WinRT 8.1 ----------------------- - - FOR_WINRT=1 - -Using Microsoft Visual C++ 2013 (or later) is required. When using the -above, something like the following macro will need to be added to the -NMAKE command line as well: - - "NSDKLIBPATH=%WindowsSdkDir%\..\8.1\lib\winv6.3\um\x86" - -Building for UWP 10.0 ---------------------- - - FOR_WINRT=1 FOR_UWP=1 - -Using Microsoft Visual C++ 2015 (or later) is required. When using the -above, something like the following macros will need to be added to the -NMAKE command line as well: - - "NSDKLIBPATH=%WindowsSdkDir%\..\10\lib\10.0.10586.0\um\x86" - "PSDKLIBPATH=%WindowsSdkDir%\..\10\lib\10.0.10586.0\um\x86" - "NUCRTLIBPATH=%UniversalCRTSdkDir%\..\10\lib\10.0.10586.0\ucrt\x86" - -Building for the Windows 10 SDK -------------------------------- - - FOR_WIN10=1 - -Using Microsoft Visual C++ 2015 (or later) is required. When using the -above, no other macros should be needed on the NMAKE command line. Other preprocessor defines -------------------------- @@ -102,10 +90,10 @@ Other preprocessor defines Additionally, preprocessor defines may be specified by using the OPTS macro on the NMAKE command line. However, not all possible preprocessor defines may be specified in this manner as some require the amalgamation to be built -with them enabled (see http://www.sqlite.org/compile.html). For example, the +with them enabled (see http://sqlite.org/compile.html). For example, the following will work: - "OPTS=-DSQLITE_ENABLE_STAT4=1 -DSQLITE_ENABLE_JSON1=1" + "OPTS=-DSQLITE_ENABLE_STAT4=1 -DSQLITE_OMIT_JSON=1" However, the following will not compile unless the amalgamation was built with it enabled: diff --git a/autoconf/auto.def b/autoconf/auto.def new file mode 100644 index 0000000000..c61d81e506 --- /dev/null +++ b/autoconf/auto.def @@ -0,0 +1,25 @@ +#!/do/not/tclsh +# ^^^ help out editors which guess this file's content type. +# +# This is the main autosetup-compatible configure script for the +# "autoconf" bundle of the SQLite project. +use sqlite-config +sqlite-configure autoconf { + sqlite-handle-debug + sqlite-check-common-bins ;# must come before [sqlite-handle-wasi-sdk] + sqlite-handle-wasi-sdk ;# must run relatively early, as it changes the environment + sqlite-check-common-system-deps + proj-define-for-opt static-shell ENABLE_STATIC_SHELL \ + "Link library statically into the CLI shell?" + proj-define-for-opt static-cli-shell STATIC_CLI_SHELL "Statically link CLI shell?" + if {![opt-bool static-shell] && [opt-bool static-cli-shell]} { + proj-fatal "--disable-static-shell and --static-cli-shell are mutualy exclusive" + } + if {![opt-bool shared] && ![opt-bool static-shell]} { + proj-opt-set shared 1 + proj-indented-notice { + NOTICE: ignoring --disable-shared because --disable-static-shell + was specified. + } + } +} diff --git a/autoconf/configure.ac b/autoconf/configure.ac deleted file mode 100644 index 9492530680..0000000000 --- a/autoconf/configure.ac +++ /dev/null @@ -1,167 +0,0 @@ - -#----------------------------------------------------------------------- -# Supports the following non-standard switches. -# -# --enable-threadsafe -# --enable-readline -# --enable-editline -# --enable-static-shell -# --enable-dynamic-extensions -# - -AC_PREREQ(2.61) -AC_INIT(sqlite, --SQLITE-VERSION--, http://www.sqlite.org) -AC_CONFIG_SRCDIR([sqlite3.c]) - -# Use automake. -AM_INIT_AUTOMAKE([foreign]) - -AC_SYS_LARGEFILE - -# Check for required programs. -AC_PROG_CC -AC_PROG_LIBTOOL -AC_PROG_MKDIR_P - -# Check for library functions that SQLite can optionally use. -AC_CHECK_FUNCS([fdatasync usleep fullfsync localtime_r gmtime_r]) -AC_FUNC_STRERROR_R - -AC_CONFIG_FILES([Makefile sqlite3.pc]) -AC_SUBST(BUILD_CFLAGS) - -#----------------------------------------------------------------------- -# --enable-editline -# --enable-readline -# -AC_ARG_ENABLE(editline, [AS_HELP_STRING( - [--enable-editline], - [use BSD libedit])], - [], [enable_editline=yes]) -AC_ARG_ENABLE(readline, [AS_HELP_STRING( - [--enable-readline], - [use readline])], - [], [enable_readline=no]) -if test x"$enable_editline" != xno ; then - sLIBS=$LIBS - LIBS="" - AC_SEARCH_LIBS([readline],[edit],[enable_readline=no],[enable_editline=no]) - READLINE_LIBS=$LIBS - if test x"$LIBS" != "x"; then - AC_DEFINE([HAVE_EDITLINE],1,Define to use BSD editline) - else - unset ac_cv_search_readline - fi - LIBS=$sLIBS -fi -if test x"$enable_readline" != xno ; then - sLIBS=$LIBS - LIBS="" - AC_SEARCH_LIBS(tgetent, curses ncurses ncursesw, [], []) - AC_SEARCH_LIBS(readline, readline, [], [enable_readline=no]) - AC_CHECK_FUNCS(readline, [], []) - READLINE_LIBS=$LIBS - LIBS=$sLIBS -fi -AC_SUBST(READLINE_LIBS) -#----------------------------------------------------------------------- - -#----------------------------------------------------------------------- -# --enable-threadsafe -# -AC_ARG_ENABLE(threadsafe, [AS_HELP_STRING( - [--enable-threadsafe], [build a thread-safe library [default=yes]])], - [], [enable_threadsafe=yes]) -THREADSAFE_FLAGS=-DSQLITE_THREADSAFE=0 -if test x"$enable_threadsafe" != "xno"; then - THREADSAFE_FLAGS="-D_REENTRANT=1 -DSQLITE_THREADSAFE=1" - AC_SEARCH_LIBS(pthread_create, pthread) - AC_SEARCH_LIBS(pthread_mutexattr_init, pthread) -fi -AC_SUBST(THREADSAFE_FLAGS) -#----------------------------------------------------------------------- - -#----------------------------------------------------------------------- -# --enable-dynamic-extensions -# -AC_ARG_ENABLE(dynamic-extensions, [AS_HELP_STRING( - [--enable-dynamic-extensions], [support loadable extensions [default=yes]])], - [], [enable_dynamic_extensions=yes]) -if test x"$enable_dynamic_extensions" != "xno"; then - AC_SEARCH_LIBS(dlopen, dl) -else - DYNAMIC_EXTENSION_FLAGS=-DSQLITE_OMIT_LOAD_EXTENSION=1 -fi -AC_MSG_CHECKING([for whether to support dynamic extensions]) -AC_MSG_RESULT($enable_dynamic_extensions) -AC_SUBST(DYNAMIC_EXTENSION_FLAGS) -#----------------------------------------------------------------------- - -#----------------------------------------------------------------------- -# --enable-fts5 -# -AC_ARG_ENABLE(fts5, [AS_HELP_STRING( - [--enable-fts5], [include fts5 support [default=no]])], - [], [enable_fts5=no]) -if test x"$enable_fts5" == "xyes"; then - AC_SEARCH_LIBS(log, m) - FTS5_FLAGS=-DSQLITE_ENABLE_FTS5 -fi -AC_SUBST(FTS5_FLAGS) -#----------------------------------------------------------------------- - -#----------------------------------------------------------------------- -# --enable-json1 -# -AC_ARG_ENABLE(json1, [AS_HELP_STRING( - [--enable-json1], [include json1 support [default=no]])], - [], [enable_json1=no]) -if test x"$enable_json1" == "xyes"; then - JSON1_FLAGS=-DSQLITE_ENABLE_JSON1 -fi -AC_SUBST(JSON1_FLAGS) -#----------------------------------------------------------------------- - -#----------------------------------------------------------------------- -# --enable-static-shell -# -AC_ARG_ENABLE(static-shell, [AS_HELP_STRING( - [--enable-static-shell], - [statically link libsqlite3 into shell tool [default=yes]])], - [], [enable_static_shell=yes]) -if test x"$enable_static_shell" == "xyes"; then - EXTRA_SHELL_OBJ=sqlite3.$OBJEXT -else - EXTRA_SHELL_OBJ=libsqlite3.la -fi -AC_SUBST(EXTRA_SHELL_OBJ) -#----------------------------------------------------------------------- - -AC_CHECK_FUNCS(posix_fallocate) - -#----------------------------------------------------------------------- -# UPDATE: Maybe it's better if users just set CFLAGS before invoking -# configure. This option doesn't really add much... -# -# --enable-tempstore -# -# AC_ARG_ENABLE(tempstore, [AS_HELP_STRING( -# [--enable-tempstore], -# [in-memory temporary tables (never, no, yes, always) [default=no]])], -# [], [enable_tempstore=no]) -# AC_MSG_CHECKING([for whether or not to store temp tables in-memory]) -# case "$enable_tempstore" in -# never ) TEMP_STORE=0 ;; -# no ) TEMP_STORE=1 ;; -# always ) TEMP_STORE=3 ;; -# yes ) TEMP_STORE=3 ;; -# * ) -# TEMP_STORE=1 -# enable_tempstore=yes -# ;; -# esac -# AC_MSG_RESULT($enable_tempstore) -# AC_SUBST(TEMP_STORE) -#----------------------------------------------------------------------- - -AC_OUTPUT diff --git a/autoconf/tea/Makefile.in b/autoconf/tea/Makefile.in index 3e481dadfe..04c8f87f55 100644 --- a/autoconf/tea/Makefile.in +++ b/autoconf/tea/Makefile.in @@ -1,440 +1,559 @@ -# Makefile.in -- +all: # -# This file is a Makefile for Sample TEA Extension. If it has the name -# "Makefile.in" then it is a template for a Makefile; to generate the -# actual Makefile, run "./configure", which is a configuration script -# generated by the "autoconf" program (constructs like "@foo@" will get -# replaced in the actual Makefile. -# -# Copyright (c) 1999 Scriptics Corporation. -# Copyright (c) 2002-2005 ActiveState Corporation. -# -# See the file "license.terms" for information on usage and redistribution -# of this file, and for a DISCLAIMER OF ALL WARRANTIES. -# -# RCS: @(#) $Id: Makefile.in,v 1.59 2005/07/26 19:17:02 mdejong Exp $ - -#======================================================================== -# Add additional lines to handle any additional AC_SUBST cases that -# have been added in a customized configure script. -#======================================================================== - -#SAMPLE_NEW_VAR = @SAMPLE_NEW_VAR@ - -#======================================================================== -# Nothing of the variables below this line should need to be changed. -# Please check the TARGETS section below to make sure the make targets -# are correct. -#======================================================================== - -#======================================================================== -# The names of the source files is defined in the configure script. -# The object files are used for linking into the final library. -# This will be used when a dist target is added to the Makefile. -# It is not important to specify the directory, as long as it is the -# $(srcdir) or in the generic, win or unix subdirectory. -#======================================================================== - -PKG_SOURCES = @PKG_SOURCES@ -PKG_OBJECTS = @PKG_OBJECTS@ - -PKG_STUB_SOURCES = @PKG_STUB_SOURCES@ -PKG_STUB_OBJECTS = @PKG_STUB_OBJECTS@ - -#======================================================================== -# PKG_TCL_SOURCES identifies Tcl runtime files that are associated with -# this package that need to be installed, if any. -#======================================================================== - -PKG_TCL_SOURCES = @PKG_TCL_SOURCES@ - -#======================================================================== -# This is a list of public header files to be installed, if any. -#======================================================================== - -PKG_HEADERS = @PKG_HEADERS@ - -#======================================================================== -# "PKG_LIB_FILE" refers to the library (dynamic or static as per -# configuration options) composed of the named objects. -#======================================================================== - -PKG_LIB_FILE = @PKG_LIB_FILE@ -PKG_STUB_LIB_FILE = @PKG_STUB_LIB_FILE@ - -lib_BINARIES = $(PKG_LIB_FILE) -BINARIES = $(lib_BINARIES) - -SHELL = @SHELL@ - -srcdir = @srcdir@ -prefix = @prefix@ -exec_prefix = @exec_prefix@ - -bindir = @bindir@ -libdir = @libdir@ -datarootdir = @datarootdir@ -datadir = @datadir@ -mandir = @mandir@ -includedir = @includedir@ - -DESTDIR = - -PKG_DIR = $(PACKAGE_NAME)$(PACKAGE_VERSION) -pkgdatadir = $(datadir)/$(PKG_DIR) -pkglibdir = $(libdir)/$(PKG_DIR) -pkgincludedir = $(includedir)/$(PKG_DIR) - -top_builddir = . - -INSTALL = @INSTALL@ -INSTALL_PROGRAM = @INSTALL_PROGRAM@ -INSTALL_DATA = @INSTALL_DATA@ -INSTALL_SCRIPT = @INSTALL_SCRIPT@ - -PACKAGE_NAME = @PACKAGE_NAME@ -PACKAGE_VERSION = @PACKAGE_VERSION@ -CC = @CC@ -CFLAGS_DEFAULT = @CFLAGS_DEFAULT@ -CFLAGS_WARNING = @CFLAGS_WARNING@ -CLEANFILES = @CLEANFILES@ -EXEEXT = @EXEEXT@ -LDFLAGS_DEFAULT = @LDFLAGS_DEFAULT@ -MAKE_LIB = @MAKE_LIB@ -MAKE_SHARED_LIB = @MAKE_SHARED_LIB@ -MAKE_STATIC_LIB = @MAKE_STATIC_LIB@ -MAKE_STUB_LIB = @MAKE_STUB_LIB@ -OBJEXT = @OBJEXT@ -RANLIB = @RANLIB@ -RANLIB_STUB = @RANLIB_STUB@ -SHLIB_CFLAGS = @SHLIB_CFLAGS@ -SHLIB_LD = @SHLIB_LD@ -SHLIB_LD_LIBS = @SHLIB_LD_LIBS@ -STLIB_LD = @STLIB_LD@ -#TCL_DEFS = @TCL_DEFS@ -TCL_BIN_DIR = @TCL_BIN_DIR@ -TCL_SRC_DIR = @TCL_SRC_DIR@ -#TK_BIN_DIR = @TK_BIN_DIR@ -#TK_SRC_DIR = @TK_SRC_DIR@ - -# This is no longer necessary even for packages that use private Tcl headers -#TCL_TOP_DIR_NATIVE = @TCL_TOP_DIR_NATIVE@ -# Not used, but retained for reference of what libs Tcl required -#TCL_LIBS = @TCL_LIBS@ - -#======================================================================== -# TCLLIBPATH seeds the auto_path in Tcl's init.tcl so we can test our -# package without installing. The other environment variables allow us -# to test against an uninstalled Tcl. Add special env vars that you -# require for testing here (like TCLX_LIBRARY). -#======================================================================== - -EXTRA_PATH = $(top_builddir):$(TCL_BIN_DIR) -#EXTRA_PATH = $(top_builddir):$(TCL_BIN_DIR):$(TK_BIN_DIR) -TCLLIBPATH = $(top_builddir) -TCLSH_ENV = TCL_LIBRARY=`@CYGPATH@ $(TCL_SRC_DIR)/library` \ - @LD_LIBRARY_PATH_VAR@="$(EXTRA_PATH):$(@LD_LIBRARY_PATH_VAR@)" \ - PATH="$(EXTRA_PATH):$(PATH)" \ - TCLLIBPATH="$(TCLLIBPATH)" -# TK_LIBRARY=`@CYGPATH@ $(TK_SRC_DIR)/library` - -TCLSH_PROG = @TCLSH_PROG@ -TCLSH = $(TCLSH_ENV) $(TCLSH_PROG) - -#WISH_PROG = @WISH_PROG@ -#WISH = $(TCLSH_ENV) $(WISH_PROG) - - -SHARED_BUILD = @SHARED_BUILD@ - -INCLUDES = @PKG_INCLUDES@ @TCL_INCLUDES@ -I$(srcdir)/.. -#INCLUDES = @PKG_INCLUDES@ @TCL_INCLUDES@ @TK_INCLUDES@ @TK_XINCLUDES@ - -PKG_CFLAGS = @PKG_CFLAGS@ - -# TCL_DEFS is not strictly need here, but if you remove it, then you -# must make sure that configure.in checks for the necessary components -# that your library may use. TCL_DEFS can actually be a problem if -# you do not compile with a similar machine setup as the Tcl core was -# compiled with. -#DEFS = $(TCL_DEFS) @DEFS@ $(PKG_CFLAGS) -DEFS = @DEFS@ $(PKG_CFLAGS) - -CONFIG_CLEAN_FILES = Makefile pkgIndex.tcl - -CPPFLAGS = @CPPFLAGS@ -LIBS = @PKG_LIBS@ @LIBS@ -AR = @AR@ -CFLAGS = @CFLAGS@ -COMPILE = $(CC) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) - -#======================================================================== -# Start of user-definable TARGETS section -#======================================================================== - -#======================================================================== -# TEA TARGETS. Please note that the "libraries:" target refers to platform -# independent files, and the "binaries:" target inclues executable programs and -# platform-dependent libraries. Modify these targets so that they install -# the various pieces of your package. The make and install rules -# for the BINARIES that you specified above have already been done. -#======================================================================== - -all: binaries libraries doc - -#======================================================================== -# The binaries target builds executable programs, Windows .dll's, unix -# shared/static libraries, and any other platform-dependent files. -# The list of targets to build for "binaries:" is specified at the top -# of the Makefile, in the "BINARIES" variable. -#======================================================================== - -binaries: $(BINARIES) - -libraries: - - -#======================================================================== -# Your doc target should differentiate from doc builds (by the developer) -# and doc installs (see install-doc), which just install the docs on the -# end user machine when building from source. -#======================================================================== - -doc: - @echo "If you have documentation to create, place the commands to" - @echo "build the docs in the 'doc:' target. For example:" - @echo " xml2nroff sample.xml > sample.n" - @echo " xml2html sample.xml > sample.html" - -install: all install-binaries install-libraries install-doc - -install-binaries: binaries install-lib-binaries install-bin-binaries - -#======================================================================== -# This rule installs platform-independent files, such as header files. -# The list=...; for p in $$list handles the empty list case x-platform. -#======================================================================== - -install-libraries: libraries - @mkdir -p $(DESTDIR)$(includedir) - @echo "Installing header files in $(DESTDIR)$(includedir)" - @list='$(PKG_HEADERS)'; for i in $$list; do \ - echo "Installing $(srcdir)/$$i" ; \ - $(INSTALL_DATA) $(srcdir)/$$i $(DESTDIR)$(includedir) ; \ - done; - -#======================================================================== -# Install documentation. Unix manpages should go in the $(mandir) -# directory. -#======================================================================== - -install-doc: doc - @mkdir -p $(DESTDIR)$(mandir)/mann - @echo "Installing documentation in $(DESTDIR)$(mandir)" - @list='$(srcdir)/doc/*.n'; for i in $$list; do \ - echo "Installing $$i"; \ - rm -f $(DESTDIR)$(mandir)/mann/`basename $$i`; \ - $(INSTALL_DATA) $$i $(DESTDIR)$(mandir)/mann ; \ - done - -test: binaries libraries - @echo "SQLite TEA distribution does not include tests" - -shell: binaries libraries - @$(TCLSH) $(SCRIPT) - -gdb: - $(TCLSH_ENV) gdb $(TCLSH_PROG) $(SCRIPT) - -depend: - -#======================================================================== -# $(PKG_LIB_FILE) should be listed as part of the BINARIES variable -# mentioned above. That will ensure that this target is built when you -# run "make binaries". -# -# The $(PKG_OBJECTS) objects are created and linked into the final -# library. In most cases these object files will correspond to the -# source files above. -#======================================================================== - -$(PKG_LIB_FILE): $(PKG_OBJECTS) - -rm -f $(PKG_LIB_FILE) - ${MAKE_LIB} - $(RANLIB) $(PKG_LIB_FILE) - -$(PKG_STUB_LIB_FILE): $(PKG_STUB_OBJECTS) - -rm -f $(PKG_STUB_LIB_FILE) - ${MAKE_STUB_LIB} - $(RANLIB_STUB) $(PKG_STUB_LIB_FILE) - -#======================================================================== -# We need to enumerate the list of .c to .o lines here. -# -# In the following lines, $(srcdir) refers to the toplevel directory -# containing your extension. If your sources are in a subdirectory, -# you will have to modify the paths to reflect this: -# -# sample.$(OBJEXT): $(srcdir)/generic/sample.c -# $(COMPILE) -c `@CYGPATH@ $(srcdir)/generic/sample.c` -o $@ -# -# Setting the VPATH variable to a list of paths will cause the makefile -# to look into these paths when resolving .c to .obj dependencies. -# As necessary, add $(srcdir):$(srcdir)/compat:.... -#======================================================================== - -VPATH = $(srcdir):$(srcdir)/generic:$(srcdir)/unix:$(srcdir)/win - -.c.@OBJEXT@: - $(COMPILE) -c `@CYGPATH@ $<` -o $@ - -#======================================================================== -# Distribution creation -# You may need to tweak this target to make it work correctly. -#======================================================================== - -#COMPRESS = tar cvf $(PKG_DIR).tar $(PKG_DIR); compress $(PKG_DIR).tar -COMPRESS = gtar zcvf $(PKG_DIR).tar.gz $(PKG_DIR) -DIST_ROOT = /tmp/dist -DIST_DIR = $(DIST_ROOT)/$(PKG_DIR) - -dist-clean: - rm -rf $(DIST_DIR) $(DIST_ROOT)/$(PKG_DIR).tar.* - -dist: dist-clean - mkdir -p $(DIST_DIR) - cp -p $(srcdir)/README* $(srcdir)/license* \ - $(srcdir)/aclocal.m4 $(srcdir)/configure $(srcdir)/*.in \ - $(DIST_DIR)/ - chmod 664 $(DIST_DIR)/Makefile.in $(DIST_DIR)/aclocal.m4 - chmod 775 $(DIST_DIR)/configure $(DIST_DIR)/configure.in - - for i in $(srcdir)/*.[ch]; do \ - if [ -f $$i ]; then \ - cp -p $$i $(DIST_DIR)/ ; \ - fi; \ - done; - - mkdir $(DIST_DIR)/tclconfig - cp $(srcdir)/tclconfig/install-sh $(srcdir)/tclconfig/tcl.m4 \ - $(DIST_DIR)/tclconfig/ - chmod 664 $(DIST_DIR)/tclconfig/tcl.m4 - chmod +x $(DIST_DIR)/tclconfig/install-sh - - list='demos doc generic library mac tests unix win'; \ - for p in $$list; do \ - if test -d $(srcdir)/$$p ; then \ - mkdir $(DIST_DIR)/$$p; \ - cp -p $(srcdir)/$$p/*.* $(DIST_DIR)/$$p/; \ - fi; \ - done - - (cd $(DIST_ROOT); $(COMPRESS);) - -#======================================================================== -# End of user-definable section -#======================================================================== - -#======================================================================== -# Don't modify the file to clean here. Instead, set the "CLEANFILES" -# variable in configure.in -#======================================================================== - -clean: - -test -z "$(BINARIES)" || rm -f $(BINARIES) - -rm -f *.$(OBJEXT) core *.core - -test -z "$(CLEANFILES)" || rm -f $(CLEANFILES) - -distclean: clean - -rm -f *.tab.c - -rm -f $(CONFIG_CLEAN_FILES) - -rm -f config.h config.cache config.log config.status - -#======================================================================== -# Install binary object libraries. On Windows this includes both .dll and -# .lib files. Because the .lib files are not explicitly listed anywhere, -# we need to deduce their existence from the .dll file of the same name. -# Library files go into the lib directory. -# In addition, this will generate the pkgIndex.tcl -# file in the install location (assuming it can find a usable tclsh shell) -# -# You should not have to modify this target. -#======================================================================== - -install-lib-binaries: binaries - @mkdir -p $(DESTDIR)$(pkglibdir) - @list='$(lib_BINARIES)'; for p in $$list; do \ - if test -f $$p; then \ - echo " $(INSTALL_PROGRAM) $$p $(DESTDIR)$(pkglibdir)/$$p"; \ - $(INSTALL_PROGRAM) $$p $(DESTDIR)$(pkglibdir)/$$p; \ - stub=`echo $$p|sed -e "s/.*\(stub\).*/\1/"`; \ - if test "x$$stub" = "xstub"; then \ - echo " $(RANLIB_STUB) $(DESTDIR)$(pkglibdir)/$$p"; \ - $(RANLIB_STUB) $(DESTDIR)$(pkglibdir)/$$p; \ - else \ - echo " $(RANLIB) $(DESTDIR)$(pkglibdir)/$$p"; \ - $(RANLIB) $(DESTDIR)$(pkglibdir)/$$p; \ - fi; \ - ext=`echo $$p|sed -e "s/.*\.//"`; \ - if test "x$$ext" = "xdll"; then \ - lib=`basename $$p|sed -e 's/.[^.]*$$//'`.lib; \ - if test -f $$lib; then \ - echo " $(INSTALL_DATA) $$lib $(DESTDIR)$(pkglibdir)/$$lib"; \ - $(INSTALL_DATA) $$lib $(DESTDIR)$(pkglibdir)/$$lib; \ - fi; \ - fi; \ - fi; \ - done - @list='$(PKG_TCL_SOURCES)'; for p in $$list; do \ - if test -f $(srcdir)/$$p; then \ - destp=`basename $$p`; \ - echo " Install $$destp $(DESTDIR)$(pkglibdir)/$$destp"; \ - $(INSTALL_DATA) $(srcdir)/$$p $(DESTDIR)$(pkglibdir)/$$destp; \ - fi; \ - done - @if test "x$(SHARED_BUILD)" = "x1"; then \ - echo " Install pkgIndex.tcl $(DESTDIR)$(pkglibdir)"; \ - $(INSTALL_DATA) pkgIndex.tcl $(DESTDIR)$(pkglibdir); \ +# Unless this file is named Makefile.in, you are probably looking +# at an automatically generated/filtered copy and should probably not +# edit it. +# +# This makefile is part of the teaish framework, a tool for building +# Tcl extensions, conceptually related to TEA/tclconfig but using the +# Autosetup configuration system instead of the GNU Autotools. +# +# A copy of this makefile gets processed for each extension separately +# and populated with info about how to build, test, and install the +# extension. +# +# Maintenance reminder: this file needs to stay portable with POSIX +# Make, not just GNU Make. Yes, that's unfortunate because it makes +# some things impossible (like skipping over swathes of rules when +# 'make distclean' is invoked). +# + +CC = @CC@ +INSTALL = @BIN_INSTALL@ +INSTALL.noexec = $(INSTALL) -m 0644 + +# +# Var name prefixes: +# +# teaish. => teaish core +# tx. => teaish extension +# +# Vars with a "tx." or "teaish." prefix are all "public" for purposes +# of the extension makefile, but the extension must not any "teaish." +# vars and must only modify "tx." vars where that allowance is +# specifically noted. +# +# Vars with a "teaish__" prefix are "private" and must not be used by +# the extension makefile. They may change semantics or be removed in +# any given teaish build. +# +tx.name = @TEAISH_NAME@ +tx.version = @TEAISH_VERSION@ +tx.name.pkg = @TEAISH_PKGNAME@ +tx.libdir = @TEAISH_LIBDIR_NAME@ +tx.loadPrefix = @TEAISH_LOAD_PREFIX@ +tx.tcl = @TEAISH_TCL@ +tx.makefile = @TEAISH_MAKEFILE@ +tx.makefile.in = @TEAISH_MAKEFILE_IN@ +tx.dll8.basename = @TEAISH_DLL8_BASENAME@ +tx.dll9.basename = @TEAISH_DLL9_BASENAME@ +tx.dll8 = @TEAISH_DLL8@ +tx.dll9 = @TEAISH_DLL9@ +tx.dll = $(tx.dll$(TCL_MAJOR_VERSION)) +tx.dir = @TEAISH_EXT_DIR@ +@if TEAISH_TM_TCL +# Input filename for tcl::tm-style module +tx.tm = @TEAISH_TM_TCL@ +# Target filename for tcl::tm-style installation +tx.tm.tgt = $(tx.name.pkg)-$(tx.version).tm +@endif + +@if TEAISH_DIST_NAME +tx.name.dist = @TEAISH_DIST_NAME@ +@else +tx.name.dist = $(teaish.name) +@endif + +teaish.dir = @abs_top_srcdir@ +#teaish.dir.autosetup = @TEAISH_AUTOSETUP_DIR@ +teaish.makefile = Makefile +teaish.makefile.in = $(teaish.dir)/Makefile.in +teaish__auto.def = $(teaish.dir)/auto.def + +# +# Autotools-conventional vars. We don't actually use these in this +# makefile but some may be referenced by vars imported via +# tclConfig.sh. They are part of the public API and may be reliably +# depended on from teaish.make.in. +# +bindir = @bindir@ +datadir = @datadir@ +exec_prefix = @exec_prefix@ +includedir = @includedir@ +infodir = @infodir@ +libdir = @libdir@ +libexecdir = @libexecdir@ +localstatedir = @localstatedir@ +mandir = @mandir@ +prefix = @prefix@ +runstatedir = @runstatedir@ +sbindir = @sbindir@ +sharedstatedir = @sharedstatedir@ +sysconfdir = @sysconfdir@ + + +# +# Vars derived (mostly) from tclConfig.sh. These may be reliably +# used from the extension makefile. +# +TCLSH = @TCLSH_CMD@ +TCL_CONFIG_SH = @TCL_CONFIG_SH@ +TCL_EXEC_PREFIX = @TCL_EXEC_PREFIX@ +TCL_INCLUDE_SPEC = @TCL_INCLUDE_SPEC@ +TCL_LIBS = @TCL_LIBS@ +TCL_LIB_SPEC = @TCL_LIB_SPEC@ +TCL_MAJOR_VERSION = @TCL_MAJOR_VERSION@ +TCL_MINOR_VERSION = @TCL_MINOR_VERSION@ +TCL_PATCH_LEVEL = @TCL_PATCH_LEVEL@ +TCL_PREFIX = @TCL_PREFIX@ +TCL_SHLIB_SUFFIX = @TCL_SHLIB_SUFFIX@ +TCL_STUB_LIB_SPEC = @TCL_STUB_LIB_SPEC@ +TCL_VERSION = @TCL_VERSION@ +TCLLIBDIR = @TCLLIBDIR@ + +# +# CFLAGS.configure = CFLAGS as known at configure-time. +# +# This ordering is deliberate: flags populated via tcl's +# [teaish-cflags-add] should preceed CFLAGS and CPPFLAGS (which +# typically come from the ./configure command-line invocation). +# +CFLAGS.configure = @SH_CFLAGS@ @TEAISH_CFLAGS@ @CFLAGS@ @CPPFLAGS@ $(TCL_INCLUDE_SPEC) +CFLAGS.configure += -DUSE_TCL_STUBS=@TEAISH_USE_STUBS@ + +# +# LDFLAGS.configure = LDFLAGS as known at configure-time. +# +# This ordering is deliberate: flags populated via tcl's +# [teaish-ldflags-add] should precede LDFLAGS (which typically +# comes from the ./configure command-line invocation). +# +LDFLAGS.configure = @TEAISH_LDFLAGS@ @LDFLAGS@ + +# +# Linker flags for linkhing a shared library. +# +LDFLAGS.shlib = @SH_LDFLAGS@ + +# +# The following tx.XYZ vars may be populated/modified by teaish.tcl +# and/or teaish.make. +# + +# +# tx.src is the list of source or object files to include in the +# (single) compiler/linker invocation. This will initially contain any +# sources passed to [teaish-src-add], but may also be appended to by +# teaish.make. +# +tx.src = @TEAISH_EXT_SRC@ + +# +# tx.CFLAGS is typically set by teaish.make, whereas TEAISH_CFLAGS +# gets set up via the configure script. +# +tx.CFLAGS = +tx.CPPFLAGS = + +# +# tx.LDFLAGS is typically set by teaish.make, whereas TEAISH_LDFLAGS +# gets set up via the configure script. +# +tx.LDFLAGS = + +# +# The list of 'dist' files may be appended to from teaish.make.in. +# It can also be set up from teaish.tcl using [teaish-dist-add] +# and/or [teaish-src-add -dist ...]. +# +tx.dist.files = @TEAISH_DIST_FILES@ + +# +# The base name for a distribution tar/zip file. +# +tx.dist.basename = $(tx.name.dist)-$(tx.version) + +# List of deps which may trigger an auto-reconfigure. +# +teaish__autogen.deps = \ + $(tx.makefile.in) $(teaish.makefile.in) \ + $(tx.tcl) \ + @TEAISH_PKGINDEX_TCL_IN@ @TEAISH_TM_TCL_IN@ \ + @AUTODEPS@ + +@if TEAISH_MAKEFILE_IN +$(tx.makefile): $(tx.makefile.in) +@endif + +teaish.autoreconfig = \ + @TEAISH_AUTORECONFIG@ + +# +# Problem: when more than one target can invoke TEAISH_AUTORECONFIG, +# we can get parallel reconfigures running. Thus, targets which +# may require reconfigure should depend on... +# +config.log: $(teaish__autogen.deps) + $(teaish.autoreconfig) +# ^^^ We would love to skip this when running [dist]clean, but there's +# no POSIX Make-portable way to do that. GNU Make can. +.PHONY: reconfigure +reconfigure: + $(teaish.autoreconfig) + +$(teaish.makefile): $(teaish__auto.def) $(teaish.makefile.in) \ + @AUTODEPS@ + +@if TEAISH_TESTER_TCL_IN +@TEAISH_TESTER_TCL_IN@: $(teaish__autogen.deps) +config.log: @TEAISH_TESTER_TCL_IN@ +@TEAISH_TESTER_TCL@: @TEAISH_TESTER_TCL_IN@ +@endif +@if TEAISH_TEST_TCL_IN +@TEAISH_TEST_TCL_IN@: $(teaish__autogen.deps) +config.log: @TEAISH_TEST_TCL_IN@ +@TEAISH_TEST_TCL@: @TEAISH_TEST_TCL_IN@ +@endif + +# +# CC variant for compiling Tcl-using sources. +# +CC.tcl = \ + $(CC) -o $@ $(CFLAGS.configure) $(CFLAGS) $(tx.CFLAGS) $(tx.CPPFLAGS) + +# +# CC variant for linking $(tx.src) into an extension DLL. Note that +# $(tx.src) must come before $(LDFLAGS...) for linking to third-party +# libs to work. +# +CC.dll = \ + $(CC.tcl) $(tx.src) $(LDFLAGS.shlib) \ + $(tx.LDFLAGS) $(LDFLAGS.configure) $(LDFLAGS) $(TCL_STUB_LIB_SPEC) + +@if TEAISH_ENABLE_DLL +# +# The rest of this makefile exists solely to support this brief +# target: the extension shared lib. +# +$(tx.dll): $(tx.src) config.log + @if [ "x" = "x$(tx.src)" ]; then \ + echo "Makefile var tx.src (source/object files) is empty" 1>&2; \ + exit 1; \ + fi + $(CC.dll) + +all: $(tx.dll) +@endif # TEAISH_ENABLE_DLL + +tclsh: $(teaish.makefile) config.log + @{ echo "#!/bin/sh"; echo 'exec $(TCLSH) "$$@"'; } > $@ + @chmod +x $@ + @echo "Created $@" + +# +# Run the generated test script. +# +.PHONY: test-pre test-prepre test-core test test-post test-extension +test-extension: # this name is reserved for use by teaish.make[.in] +@if TEAISH_ENABLE_DLL +test-prepre: $(tx.dll) +@endif +@if TEAISH_TESTER_TCL +teaish.tester.tcl = @TEAISH_TESTER_TCL@ +test-core.args = $(teaish.tester.tcl) +@if TEAISH_ENABLE_DLL +test-core.args += '$(tx.dll)' '$(tx.loadPrefix)' +@else +test-core.args += '' '' +@endif +test-core.args += @TEAISH_TESTUTIL_TCL@ +# Clients may pass additional args via test.args=... +# and ::argv will be rewritten before the test script loads, to +# remove $(test-core.args) +test.args ?= +test-core: test-pre + $(TCLSH) $(test-core.args) $(test.args) +test-gdb: $(teaish.tester.tcl) + gdb --args $(TCLSH) $(test-core.args) $(test.args) +test-vg.flags ?= --leak-check=full -v --show-reachable=yes --track-origins=yes +test-vg: $(teaish.tester.tcl) + valgrind $(test-vg.flags) $(TCLSH) $(test-core.args) $(test.args) +@else # !TEAISH_TESTER_TCL +test-prepre: +@endif # TEAISH_TESTER_TCL +test-pre: test-prepre +test-core: test-pre +test-post: test-core +test: test-post + +# +# Cleanup rules... +# +#.PHONY: clean-pre clean-core clean-post clean-extension +# +clean-pre: +clean-core: clean-pre + rm -f $(tx.dll8) $(tx.dll9) tclsh +clean-post: clean-core +clean: clean-post + +.PHONY: distclean-pre distclean-core distclean-post clean-extension +distclean-pre: clean +distclean-core: distclean-pre + rm -f Makefile + rm -f config.log config.defines.txt +@if TEAISH_MAKEFILE_IN +@if TEAISH_MAKEFILE + rm -f @TEAISH_MAKEFILE@ +@endif +@endif +@if TEAISH_TESTER_TCL_IN + rm -f $(teaish.tester.tcl) +@endif +@if TEAISH_PKGINDEX_TCL_IN + rm -f @TEAISH_PKGINDEX_TCL@ +@endif +@if TEAISH_PKGINIT_TCL_IN + rm -f @TEAISH_PKGINIT_TCL@ +@endif +@if TEAISH_TEST_TCL_IN + rm -f @TEAISH_TEST_TCL@ +@endif +distclean-post: distclean-core +distclean: distclean-post +# +# The (dist)clean-extension targets are reserved for use by +# client-side teaish.make. +# +# Client code which wants to clean up extra stuff should do so by +# adding their cleanup target (e.g. clean-extension) as a dependency +# to the 'clean' target, like so: +# +# clean: distclean-extension +# distclean: distclean-extension +# +distclean-extension: +clean-extension: + +# +# Installation rules... +# +@if TEAISH_ENABLE_INSTALL +.PHONY: install-pre install-core install-post install-test install-prepre install-extension +install-extension: # this name is reserved for use by teaish.make + +@if TEAISH_ENABLE_DLL +install-prepre: $(tx.dll) +@else +install-prepre: +@endif + +@if TEAISH_TM_TCL +install-core.tmdir = $(DESTDIR)@TEAISH_TCL_TM_DIR@ +@endif + +install-pre: install-prepre +install-core: install-pre + @if [ ! -d "$(DESTDIR)$(TCLLIBDIR)" ]; then \ + set -x; $(INSTALL) -d "$(DESTDIR)$(TCLLIBDIR)"; \ + fi +# ^^^^ on some platforms, install -d fails if the target already exists. +@if TEAISH_ENABLE_DLL + $(INSTALL) $(tx.dll) "$(DESTDIR)$(TCLLIBDIR)" +@endif +@if TEAISH_PKGINDEX_TCL + $(INSTALL.noexec) "@TEAISH_PKGINDEX_TCL@" "$(DESTDIR)$(TCLLIBDIR)" +@endif +@if TEAISH_PKGINIT_TCL + $(INSTALL.noexec) "@TEAISH_PKGINIT_TCL@" "$(DESTDIR)$(TCLLIBDIR)" +@endif +@if TEAISH_TM_TCL + @if [ ! -d "$(install-core.tmdir)" ]; then \ + set -x; $(INSTALL) -d "$(install-core.tmdir)"; \ + fi + $(INSTALL.noexec) "@TEAISH_TM_TCL@" "$(install-core.tmdir)/$(tx.tm.tgt)" +@endif +install-test: install-core + @echo "Post-install test of [package require $(tx.name.pkg) $(tx.version)]..."; \ + set xtra=""; \ + if [ x != "x$(DESTDIR)" ]; then \ + xtra='set ::auto_path [linsert $$::auto_path 0 [file normalize $(DESTDIR)$(TCLLIBDIR)/..]];'; \ + fi; \ + if echo \ + 'set c 0; ' $$xtra \ + '@TEAISH_POSTINST_PREREQUIRE@' \ + 'if {[catch {package require $(tx.name.pkg) $(tx.version)} xc]} {incr c};' \ + 'if {$$c && "" ne $$xc} {puts $$xc; puts "auto_path=$$::auto_path"};' \ + 'exit $$c' \ + | $(TCLSH) ; then \ + echo "passed"; \ + else \ + echo "FAILED"; \ + exit 1; \ fi +install-post: install-test +install: install-post + +# +# Uninstall rules... +# +.PHONY: uninstall uninstall-pre uninstall-core uninstall-post uninstall-extension +uninstall-extension: # this name is reserved for use by teaish.make +uninstall-pre: +uninstall-core: uninstall-pre +@if TEAISH_ENABLE_DLL + rm -fr "$(DESTDIR)$(TCLLIBDIR)" +@endif +@if TEAISH_TM_TCL + rm -f "$(DESTDIR)$(install-core.tmdir)/$(tx.tm.tgt)" +@endif + +uninstall-post: uninstall-core + @echo "Uninstalled Tcl extension $(tx.name) $(tx.version)" +uninstall: uninstall-post +@endif # TEAISH_ENABLE_INSTALL + +@if TEAISH_MAKEFILE_IN +Makefile: $(tx.makefile.in) +config.log: $(teaish.makefile.in) +@endif + +# +# Package archive generation ("dist") rules... +# +@if TEAISH_ENABLE_DIST +@if BIN_TAR +@if BIN_ZIP + +# When installing teaish as part of "make dist", we need to run +# configure with similar flags to what we last configured with but we +# must not pass on any extension-specific flags, as those won't be +# recognized when running in --teaish-install mode, causing +# the sub-configure to fail. +dist.flags = --with-tclsh=$(TCLSH) +dist.reconfig = $(teaish.dir)/configure $(tx.dist.reconfig-flags) $(dist.flags) + +# Temp dir for dist.zip. Must be different than dist.tgz or else +# parallel builds may hose the dist. +teaish__dist.tmp.zip = teaish__dist_zip +# +# Make a distribution zip file... +# +dist.zip = $(tx.dist.basename).zip +.PHONY: dist.zip dist.zip-core dist.zip-post +#dist.zip-pre: +# We apparently can't add a pre-hook here, else "make dist" rebuilds +# the archive each time it's run. +$(dist.zip): $(tx.dist.files) + @rm -fr $(teaish__dist.tmp.zip) + @mkdir -p $(teaish__dist.tmp.zip)/$(tx.dist.basename) + @tar cf $(teaish__dist.tmp.zip)/tmp.tar $(tx.dist.files) + @tar xf $(teaish__dist.tmp.zip)/tmp.tar -C $(teaish__dist.tmp.zip)/$(tx.dist.basename) +@if TEAISH_DIST_FULL + @$(dist.reconfig) \ + --teaish-install=$(teaish__dist.tmp.zip)/$(tx.dist.basename) \ + --t-e-d=$(teaish__dist.tmp.zip)/$(tx.dist.basename) >/dev/null +@endif + @rm -f $(tx.dist.basename)/tmp.tar $(dist.zip) + @cd $(teaish__dist.tmp.zip) && zip -q -r ../$(dist.zip) $(tx.dist.basename) + @rm -fr $(teaish__dist.tmp.zip) + @ls -la $(dist.zip) +dist.zip-core: $(dist.zip) +dist.zip-post: dist.zip-core +dist.zip: dist.zip-post +dist: dist.zip +undist-zip: + rm -f $(dist.zip) +undist: undist-zip +@endif #BIN_ZIP + +# +# Make a distribution tarball... +# +teaish__dist.tmp.tgz = teaish__dist_tgz +dist.tgz = $(tx.dist.basename).tar.gz +.PHONY: dist.tgz dist.tgz-core dist.tgz-post +# dist.tgz-pre: +# see notes in dist.zip +$(dist.tgz): $(tx.dist.files) + @rm -fr $(teaish__dist.tmp.tgz) + @mkdir -p $(teaish__dist.tmp.tgz)/$(tx.dist.basename) + @tar cf $(teaish__dist.tmp.tgz)/tmp.tar $(tx.dist.files) + @tar xf $(teaish__dist.tmp.tgz)/tmp.tar -C $(teaish__dist.tmp.tgz)/$(tx.dist.basename) +@if TEAISH_DIST_FULL + @rm -f $(teaish__dist.tmp.tgz)/$(tx.dist.basename)/pkgIndex.tcl.in; # kludge + @$(dist.reconfig) \ + --teaish-install=$(teaish__dist.tmp.tgz)/$(tx.dist.basename) \ + --t-e-d=$(teaish__dist.tmp.zip)/$(tx.dist.basename) >/dev/null +@endif + @rm -f $(tx.dist.basename)/tmp.tar $(dist.tgz) + @cd $(teaish__dist.tmp.tgz) && tar czf ../$(dist.tgz) $(tx.dist.basename) + @rm -fr $(teaish__dist.tmp.tgz) + @ls -la $(dist.tgz) +dist.tgz-core: $(dist.tgz) +dist.tgz-post: dist.tgz-core +dist.tgz: dist.tgz-post +dist: dist.tgz +undist-tgz: + rm -f $(dist.tgz) +undist: undist-tgz +@else #!BIN_TAR +dist: + @echo "The dist rules require tar, which configure did not find." 1>&2; exit 1 +@endif #BIN_TAR +@else #!TEAISH_ENABLE_DIST +undist: +dist: +@if TEAISH_OUT_OF_EXT_TREE + @echo "'dist' can only be used from an extension's home dir" 1>&2; \ + echo "In this case: @TEAISH_EXT_DIR@" 1>&2; exit 1 +@endif +@endif #TEAISH_ENABLE_DIST + +Makefile: @TEAISH_TCL@ + +@if TEAISH_MAKEFILE_CODE +# +# TEAISH_MAKEFILE_CODE may contain literal makefile code, which +# gets pasted verbatim here. Either [define TEAISH_MAKEFILE_CODE +# ...] or use [teaish-make-add] to incrementally build up this +# content. +# +# +@TEAISH_MAKEFILE_CODE@ +# +@endif -#======================================================================== -# Install binary executables (e.g. .exe files and dependent .dll files) -# This is for files that must go in the bin directory (located next to -# wish and tclsh), like dependent .dll files on Windows. -# -# You should not have to modify this target, except to define bin_BINARIES -# above if necessary. -#======================================================================== - -install-bin-binaries: binaries - @mkdir -p $(DESTDIR)$(bindir) - @list='$(bin_BINARIES)'; for p in $$list; do \ - if test -f $$p; then \ - echo " $(INSTALL_PROGRAM) $$p $(DESTDIR)$(bindir)/$$p"; \ - $(INSTALL_PROGRAM) $$p $(DESTDIR)$(bindir)/$$p; \ - fi; \ - done - -.SUFFIXES: .c .$(OBJEXT) - -Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status - cd $(top_builddir) \ - && CONFIG_FILES=$@ CONFIG_HEADERS= $(SHELL) ./config.status - -uninstall-binaries: - list='$(lib_BINARIES)'; for p in $$list; do \ - rm -f $(DESTDIR)$(pkglibdir)/$$p; \ - done - list='$(PKG_TCL_SOURCES)'; for p in $$list; do \ - p=`basename $$p`; \ - rm -f $(DESTDIR)$(pkglibdir)/$$p; \ - done - list='$(bin_BINARIES)'; for p in $$list; do \ - rm -f $(DESTDIR)$(bindir)/$$p; \ - done - -.PHONY: all binaries clean depend distclean doc install libraries test - -# Tell versions [3.59,3.63) of GNU make to not export all variables. -# Otherwise a system limit (for SysV at least) may be exceeded. -.NOEXPORT: +@if TEAISH_MAKEFILE +# +# TEAISH_MAKEFILE[_IN] defines any extension-specific state this file +# needs. +# +# It must set the following vars if they're not already accounted for +# via teaish.tcl. +# +# - tx.src = list of the extension's source files, being sure to +# prefix each with $(tx.dir) (if it's in the same dir as the +# extension) so that out-of-tree builds can find them. Optionally, +# [define] TEAISH_EXT_SRC or pass them to [teaish-src-add]. +# +# It may optionally set the following vars: +# +# - tx.CFLAGS = CFLAGS/CPPFLAGS. Optionally, [define] TEAISH_CFLAGS +# or pass them to [teaish-cflags-add]. +# +# - tx.LDFLAGS = LDFLAGS. Optionally, [define] TEAISH_LDFLAGS or +# pass them to [teaish-ldflags-add]. +# +# It may optionally hook into various targets as documented in +# /doc/extensions.md in the canonical teaish source tree. +# +# Interestingly, we don't have to pre-filter teaish.makefile.in - we +# can just @include it here. That skips its teaish-specific validation +# though. Hmm. +# +# +Makefile: @TEAISH_MAKEFILE@ +@include @TEAISH_MAKEFILE@ +# +@endif diff --git a/autoconf/tea/README b/autoconf/tea/README deleted file mode 100644 index 99dc8b8f03..0000000000 --- a/autoconf/tea/README +++ /dev/null @@ -1,36 +0,0 @@ -This is the SQLite extension for Tcl using the Tcl Extension -Architecture (TEA). For additional information on SQLite see - - http://www.sqlite.org/ - - -UNIX BUILD -========== - -Building under most UNIX systems is easy, just run the configure script -and then run make. For more information about the build process, see -the tcl/unix/README file in the Tcl src dist. The following minimal -example will install the extension in the /opt/tcl directory. - - $ cd sqlite-*-tea - $ ./configure --prefix=/opt/tcl - $ make - $ make install - -WINDOWS BUILD -============= - -The recommended method to build extensions under windows is to use the -Msys + Mingw build process. This provides a Unix-style build while -generating native Windows binaries. Using the Msys + Mingw build tools -means that you can use the same configure script as per the Unix build -to create a Makefile. See the tcl/win/README file for the URL of -the Msys + Mingw download. - -If you have VC++ then you may wish to use the files in the win -subdirectory and build the extension using just VC++. These files have -been designed to be as generic as possible but will require some -additional maintenance by the project developer to synchronise with -the TEA configure.in and Makefile.in files. Instructions for using the -VC++ makefile are written in the first part of the Makefile.vc -file. diff --git a/autoconf/tea/README.txt b/autoconf/tea/README.txt new file mode 100644 index 0000000000..122b08d32d --- /dev/null +++ b/autoconf/tea/README.txt @@ -0,0 +1,94 @@ +This is the SQLite extension for Tcl using something akin to +the Tcl Extension Architecture (TEA). To build it: + + ./configure ...flags... + +e.g.: + + ./configure --with-tcl=/path/to/tcl/install/root + +or: + + ./configure --with-tclsh=/path/to/tcl/install/root + +Run ./configure --help for the full list of flags. + +The configuration process will fail if tclConfig.sh cannot be found. + +The makefile will only honor CFLAGS and CPPFLAGS passed to the +configure script, not those directly passed to the makefile. + +Then: + + make test install + +----------------------- THE PREFERRED WAY --------------------------- + +The preferred way to build the TCL extension for SQLite is to use the +canonical source code tarball. For Unix: + + ./configure --with-tclsh=$(TCLSH) + make tclextension-install + +For Windows: + + nmake /f Makefile.msc tclextension-install TCLSH_CMD=$(TCLSH) + +In both of the above, replace $(TCLSH) with the full pathname of +of the tclsh that you want the SQLite extension to work with. See +step-by-step instructions at the links below for more information: + + https://sqlite.org/src/doc/trunk/doc/compile-for-unix.md + https://sqlite.org/src/doc/trunk/doc/compile-for-windows.md + +And info about the extension's Tcl interface can be found at: + + https://sqlite.org/tclsqlite.html + +The whole point of the amalgamation-autoconf tarball (in which this +README.txt file is embedded) is to provide a means of compiling SQLite +that does not require first installing TCL and/or "tclsh". The +canonical Makefile in the SQLite source tree provides more +capabilities (such as the the ability to run test cases to ensure that +the build worked) and is better maintained. The only downside of the +canonical Makefile is that it requires a TCL installation. But if you +are wanting to build the TCL extension for SQLite, then presumably you +already have a TCL installation. So why not just use the more-capable +and better-maintained canoncal Makefile? + +As of version 3.50.0, this build process uses "teaish": + + https://fossil.wanderinghorse.net/r/teaish + +which is conceptually derived from the pre-3.50 toolchain, TEA: + + http://core.tcl-lang.org/tclconfig + http://core.tcl-lang.org/sampleextension + +It to works for us. It might also work for you. But we cannot +promise that. + +If you want to use this TEA builder and it works for you, that's fine. +But if you have trouble, the first thing you should do is go back +to using the canonical Makefile in the SQLite source tree. + +------------------------------------------------------------------ + + +UNIX BUILD +========== + +Building under most UNIX systems is easy, just run the configure +script and then run make. For example: + + $ cd sqlite-*-tea + $ ./configure --with-tcl=/path/to/tcl/install/root + $ make test + $ make install + +WINDOWS BUILD +============= + +On Windows this build is known to work on Cygwin and some Msys2 +environments. We do not currently support Microsoft makefiles for +native Windows builds. diff --git a/autoconf/tea/_teaish.tester.tcl.in b/autoconf/tea/_teaish.tester.tcl.in new file mode 100644 index 0000000000..e04d8e63e7 --- /dev/null +++ b/autoconf/tea/_teaish.tester.tcl.in @@ -0,0 +1,50 @@ +# -*- tcl -*- +# +# Unless this file is named _teaish.tester.tcl.in, you are probably +# looking at an automatically generated/filtered copy and should +# probably not edit it. +# +# This is the wrapper script invoked by teaish's "make test" recipe. +# It gets passed 3 args: +# +# $1 = the DLL name, or "" if the extension has no DLL +# +# $2 = the "load prefix" for Tcl's [load] or empty if $1 is empty +# +# $3 = the /path/to/teaish/tester.tcl (test utility code) +# +@if TEAISH_VSATISFIES_CODE +@TEAISH_VSATISFIES_CODE@ +@endif +if {[llength [lindex $::argv 0]] > 0} { + load [file normalize [lindex $::argv 0]] [lindex $::argv 1]; + # ----^^^^^^^ needed on Haiku when argv 0 is just a filename, else + # load cannot find the file. +} +set ::argv [lassign $argv - -] +source -encoding utf-8 [lindex $::argv 0]; # teaish/tester.tcl +@if TEAISH_PKGINIT_TCL +apply {{file} { + set dir [file dirname $::argv0] + source -encoding utf-8 $file +}} [join {@TEAISH_PKGINIT_TCL@}] +@endif +@if TEAISH_TM_TCL +apply {{file} { + set dir [file dirname $::argv0] + source -encoding utf-8 $file +}} [join {@TEAISH_TM_TCL@}] +@endif +@if TEAISH_TEST_TCL +apply {{file} { + # Populate state for [tester.tcl::teaish-build-flag*] + array set ::teaish__BuildFlags @TEAISH__DEFINES_MAP@ + set dir [file normalize [file dirname $file]] + #test-fail "Just testing" + source -encoding utf-8 $file +}} [join {@TEAISH_TEST_TCL@}] +@else # TEAISH_TEST_TCL +# No $TEAISH_TEST_TCL provided, so here's a default test which simply +# loads the extension. +puts {Extension @TEAISH_NAME@ @TEAISH_VERSION@ successfully loaded from @TEAISH_TESTER_TCL@} +@endif diff --git a/autoconf/tea/aclocal.m4 b/autoconf/tea/aclocal.m4 deleted file mode 100644 index 0b057391d2..0000000000 --- a/autoconf/tea/aclocal.m4 +++ /dev/null @@ -1,9 +0,0 @@ -# -# Include the TEA standard macro set -# - -builtin(include,tclconfig/tcl.m4) - -# -# Add here whatever m4 macros you want to define for your package -# diff --git a/autoconf/tea/auto.def b/autoconf/tea/auto.def new file mode 100644 index 0000000000..7170b3d1fe --- /dev/null +++ b/autoconf/tea/auto.def @@ -0,0 +1,8 @@ +#/do/not/tclsh +# ^^^ help out editors which guess this file's content type. +# +# Main configure script entry point for the TEA(ish) framework. All +# extension-specific customization goes in teaish.tcl.in or +# teaish.tcl. +use teaish/core +teaish-configure-core diff --git a/autoconf/tea/configure b/autoconf/tea/configure new file mode 100755 index 0000000000..01b3abcc2f --- /dev/null +++ b/autoconf/tea/configure @@ -0,0 +1,20 @@ +#!/bin/sh +# Look for and run autosetup... +dir0="`dirname "$0"`" +dirA="$dir0" +if [ -d $dirA/autosetup ]; then + # A local copy of autosetup + dirA=$dirA/autosetup +elif [ -d $dirA/../autosetup ]; then + # SQLite "autoconf" bundle + dirA=$dirA/../autosetup +elif [ -d $dirA/../../autosetup ]; then + # SQLite canonical source tree + dirA=$dirA/../../autosetup +else + echo "$0: Cannot find autosetup" 1>&2 + exit 1 +fi +WRAPPER="$0"; export WRAPPER; exec "`"$dirA/autosetup-find-tclsh"`" \ + "$dirA/autosetup" --teaish-extension-dir="$dir0" \ + "$@" diff --git a/autoconf/tea/configure.ac b/autoconf/tea/configure.ac deleted file mode 100644 index 8df0af6195..0000000000 --- a/autoconf/tea/configure.ac +++ /dev/null @@ -1,202 +0,0 @@ -#!/bin/bash -norc -dnl This file is an input file used by the GNU "autoconf" program to -dnl generate the file "configure", which is run during Tcl installation -dnl to configure the system for the local environment. -# -# RCS: @(#) $Id: configure.in,v 1.43 2005/07/26 19:17:05 mdejong Exp $ - -#----------------------------------------------------------------------- -# Sample configure.in for Tcl Extensions. The only places you should -# need to modify this file are marked by the string __CHANGE__ -#----------------------------------------------------------------------- - -#----------------------------------------------------------------------- -# __CHANGE__ -# Set your package name and version numbers here. -# -# This initializes the environment with PACKAGE_NAME and PACKAGE_VERSION -# set as provided. These will also be added as -D defs in your Makefile -# so you can encode the package version directly into the source files. -#----------------------------------------------------------------------- - -AC_INIT([sqlite], [3.7.4]) - -#-------------------------------------------------------------------- -# Call TEA_INIT as the first TEA_ macro to set up initial vars. -# This will define a ${TEA_PLATFORM} variable == "unix" or "windows" -# as well as PKG_LIB_FILE and PKG_STUB_LIB_FILE. -#-------------------------------------------------------------------- - -TEA_INIT([3.9]) - -AC_CONFIG_AUX_DIR(tclconfig) - -#-------------------------------------------------------------------- -# Load the tclConfig.sh file -#-------------------------------------------------------------------- - -TEA_PATH_TCLCONFIG -TEA_LOAD_TCLCONFIG - -#-------------------------------------------------------------------- -# Load the tkConfig.sh file if necessary (Tk extension) -#-------------------------------------------------------------------- - -#TEA_PATH_TKCONFIG -#TEA_LOAD_TKCONFIG - -#----------------------------------------------------------------------- -# Handle the --prefix=... option by defaulting to what Tcl gave. -# Must be called after TEA_LOAD_TCLCONFIG and before TEA_SETUP_COMPILER. -#----------------------------------------------------------------------- - -TEA_PREFIX - -#----------------------------------------------------------------------- -# Standard compiler checks. -# This sets up CC by using the CC env var, or looks for gcc otherwise. -# This also calls AC_PROG_CC, AC_PROG_INSTALL and a few others to create -# the basic setup necessary to compile executables. -#----------------------------------------------------------------------- - -TEA_SETUP_COMPILER - -#----------------------------------------------------------------------- -# __CHANGE__ -# Specify the C source files to compile in TEA_ADD_SOURCES, -# public headers that need to be installed in TEA_ADD_HEADERS, -# stub library C source files to compile in TEA_ADD_STUB_SOURCES, -# and runtime Tcl library files in TEA_ADD_TCL_SOURCES. -# This defines PKG(_STUB)_SOURCES, PKG(_STUB)_OBJECTS, PKG_HEADERS -# and PKG_TCL_SOURCES. -#----------------------------------------------------------------------- - -TEA_ADD_SOURCES([tclsqlite3.c]) -TEA_ADD_HEADERS([]) -TEA_ADD_INCLUDES([-I\"`\${CYGPATH} \${srcdir}/generic`\"]) -TEA_ADD_LIBS([]) -TEA_ADD_CFLAGS([-DSQLITE_ENABLE_FTS3=1]) -TEA_ADD_CFLAGS([-DSQLITE_3_SUFFIX_ONLY=1]) -TEA_ADD_CFLAGS([-DSQLITE_ENABLE_RTREE=1]) -TEA_ADD_CFLAGS([-DSQLITE_OMIT_DEPRECATED=1]) -TEA_ADD_STUB_SOURCES([]) -TEA_ADD_TCL_SOURCES([]) - -#-------------------------------------------------------------------- -# The --with-system-sqlite causes the TCL bindings to SQLite to use -# the system shared library for SQLite rather than statically linking -# against its own private copy. This is dangerous and leads to -# undersirable dependences and is not recommended. -# Patchs from rmax. -#-------------------------------------------------------------------- -AC_ARG_WITH([system-sqlite], - [AC_HELP_STRING([--with-system-sqlite], - [use a system-supplied libsqlite3 instead of the bundled one])], - [], [with_system_sqlite=no]) -if test x$with_system_sqlite != xno; then - AC_CHECK_HEADER([sqlite3.h], - [AC_CHECK_LIB([sqlite3],[sqlite3_initialize], - [AC_DEFINE(USE_SYSTEM_SQLITE) - LIBS="$LIBS -lsqlite3"])]) -fi - -#-------------------------------------------------------------------- -# __CHANGE__ -# Choose which headers you need. Extension authors should try very -# hard to only rely on the Tcl public header files. Internal headers -# contain private data structures and are subject to change without -# notice. -# This MUST be called after TEA_LOAD_TCLCONFIG / TEA_LOAD_TKCONFIG -#-------------------------------------------------------------------- - -TEA_PUBLIC_TCL_HEADERS -#TEA_PRIVATE_TCL_HEADERS - -#TEA_PUBLIC_TK_HEADERS -#TEA_PRIVATE_TK_HEADERS -#TEA_PATH_X - -#-------------------------------------------------------------------- -# Check whether --enable-threads or --disable-threads was given. -# This auto-enables if Tcl was compiled threaded. -#-------------------------------------------------------------------- - -TEA_ENABLE_THREADS -if test "${TCL_THREADS}" = "1" ; then - AC_DEFINE(SQLITE_THREADSAFE, 1, [Trigger sqlite threadsafe build]) - # Not automatically added by Tcl because its assumed Tcl links to them, - # but it may not if it isn't really a threaded build. - TEA_ADD_LIBS([$THREADS_LIBS]) -else - AC_DEFINE(SQLITE_THREADSAFE, 0, [Trigger sqlite non-threadsafe build]) -fi - -#-------------------------------------------------------------------- -# The statement below defines a collection of symbols related to -# building as a shared library instead of a static library. -#-------------------------------------------------------------------- - -TEA_ENABLE_SHARED - -#-------------------------------------------------------------------- -# This macro figures out what flags to use with the compiler/linker -# when building shared/static debug/optimized objects. This information -# can be taken from the tclConfig.sh file, but this figures it all out. -#-------------------------------------------------------------------- - -TEA_CONFIG_CFLAGS - -#-------------------------------------------------------------------- -# Set the default compiler switches based on the --enable-symbols option. -#-------------------------------------------------------------------- - -TEA_ENABLE_SYMBOLS - -#-------------------------------------------------------------------- -# Everyone should be linking against the Tcl stub library. If you -# can't for some reason, remove this definition. If you aren't using -# stubs, you also need to modify the SHLIB_LD_LIBS setting below to -# link against the non-stubbed Tcl library. Add Tk too if necessary. -#-------------------------------------------------------------------- - -AC_DEFINE(USE_TCL_STUBS, 1, [Use Tcl stubs]) -#AC_DEFINE(USE_TK_STUBS, 1, [Use Tk stubs]) - - -#-------------------------------------------------------------------- -# Redefine fdatasync as fsync on systems that lack fdatasync -#-------------------------------------------------------------------- -# -#AC_CHECK_FUNC(fdatasync, , AC_DEFINE(fdatasync, fsync)) -# Check for library functions that SQLite can optionally use. -AC_CHECK_FUNCS([fdatasync usleep fullfsync localtime_r gmtime_r]) - -AC_FUNC_STRERROR_R - - -#-------------------------------------------------------------------- -# This macro generates a line to use when building a library. It -# depends on values set by the TEA_ENABLE_SHARED, TEA_ENABLE_SYMBOLS, -# and TEA_LOAD_TCLCONFIG macros above. -#-------------------------------------------------------------------- - -TEA_MAKE_LIB - -#-------------------------------------------------------------------- -# Determine the name of the tclsh and/or wish executables in the -# Tcl and Tk build directories or the location they were installed -# into. These paths are used to support running test cases only, -# the Makefile should not be making use of these paths to generate -# a pkgIndex.tcl file or anything else at extension build time. -#-------------------------------------------------------------------- - -TEA_PROG_TCLSH -#TEA_PROG_WISH - -#-------------------------------------------------------------------- -# Finally, substitute all of the various values into the Makefile. -# You may alternatively have a special pkgIndex.tcl.in or other files -# which require substituting th AC variables in. Include these here. -#-------------------------------------------------------------------- - -AC_OUTPUT([Makefile pkgIndex.tcl]) diff --git a/autoconf/tea/doc/sqlite3.n b/autoconf/tea/doc/sqlite3.n deleted file mode 100644 index 13913e5583..0000000000 --- a/autoconf/tea/doc/sqlite3.n +++ /dev/null @@ -1,15 +0,0 @@ -.TH sqlite3 n 4.1 "Tcl-Extensions" -.HS sqlite3 tcl -.BS -.SH NAME -sqlite3 \- an interface to the SQLite3 database engine -.SH SYNOPSIS -\fBsqlite3\fI command_name ?filename?\fR -.br -.SH DESCRIPTION -SQLite3 is a self-contains, zero-configuration, transactional SQL database -engine. This extension provides an easy to use interface for accessing -SQLite database files from Tcl. -.PP -For full documentation see \fIhttp://www.sqlite.org/\fR and -in particular \fIhttp://www.sqlite.org/tclsqlite.html\fR. diff --git a/autoconf/tea/pkgIndex.tcl.in b/autoconf/tea/pkgIndex.tcl.in index bc585f73b3..c93fcc6854 100644 --- a/autoconf/tea/pkgIndex.tcl.in +++ b/autoconf/tea/pkgIndex.tcl.in @@ -1,7 +1,40 @@ -# +# -*- tcl -*- # Tcl package index file # -# Note sqlite*3* init specifically +# Unless this file is named pkgIndex.tcl.in, you are probably looking +# at an automatically generated/filtered copy and should probably not +# edit it. # -package ifneeded sqlite3 @PACKAGE_VERSION@ \ - [list load [file join $dir @PKG_LIB_FILE@] Sqlite3] +# Adapted from https://core.tcl-lang.org/tcltls +@if TEAISH_VSATISFIES_CODE +@TEAISH_VSATISFIES_CODE@ +@endif +if {[package vsatisfies [package provide Tcl] 9.0-]} { + package ifneeded {@TEAISH_PKGNAME@} {@TEAISH_VERSION@} [list apply {{dir} { +@if TEAISH_ENABLE_DLL + load [file join $dir {@TEAISH_DLL9@}] @TEAISH_LOAD_PREFIX@ +@endif +@if TEAISH_PKGINIT_TCL_TAIL + set initScript [file join $dir {@TEAISH_PKGINIT_TCL_TAIL@}] + if {[file exists $initScript]} { + source -encoding utf-8 $initScript + } +@endif + }} $dir] +} else { + package ifneeded {@TEAISH_PKGNAME@} {@TEAISH_VERSION@} [list apply {{dir} { +@if TEAISH_ENABLE_DLL + if {[string tolower [file extension {@TEAISH_DLL8@}]] in [list .dll .dylib .so]} { + load [file join $dir {@TEAISH_DLL8@}] @TEAISH_LOAD_PREFIX@ + } else { + load {} @TEAISH_LOAD_PREFIX@ + } +@endif +@if TEAISH_PKGINIT_TCL_TAIL + set initScript [file join $dir {@TEAISH_PKGINIT_TCL_TAIL@}] + if {[file exists $initScript]} { + source -encoding utf-8 $initScript + } +@endif + }} $dir] +} diff --git a/autoconf/tea/tclconfig/install-sh b/autoconf/tea/tclconfig/install-sh deleted file mode 100644 index 7c34c3f926..0000000000 --- a/autoconf/tea/tclconfig/install-sh +++ /dev/null @@ -1,528 +0,0 @@ -#!/bin/sh -# install - install a program, script, or datafile - -scriptversion=2011-04-20.01; # UTC - -# This originates from X11R5 (mit/util/scripts/install.sh), which was -# later released in X11R6 (xc/config/util/install.sh) with the -# following copyright and license. -# -# Copyright (C) 1994 X Consortium -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to -# deal in the Software without restriction, including without limitation the -# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or -# sell copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in -# all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN -# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC- -# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -# -# Except as contained in this notice, the name of the X Consortium shall not -# be used in advertising or otherwise to promote the sale, use or other deal- -# ings in this Software without prior written authorization from the X Consor- -# tium. -# -# -# FSF changes to this file are in the public domain. -# -# Calling this script install-sh is preferred over install.sh, to prevent -# `make' implicit rules from creating a file called install from it -# when there is no Makefile. -# -# This script is compatible with the BSD install script, but was written -# from scratch. - -nl=' -' -IFS=" "" $nl" - -# set DOITPROG to echo to test this script - -# Don't use :- since 4.3BSD and earlier shells don't like it. -doit=${DOITPROG-} -if test -z "$doit"; then - doit_exec=exec -else - doit_exec=$doit -fi - -# Put in absolute file names if you don't have them in your path; -# or use environment vars. - -chgrpprog=${CHGRPPROG-chgrp} -chmodprog=${CHMODPROG-chmod} -chownprog=${CHOWNPROG-chown} -cmpprog=${CMPPROG-cmp} -cpprog=${CPPROG-cp} -mkdirprog=${MKDIRPROG-mkdir} -mvprog=${MVPROG-mv} -rmprog=${RMPROG-rm} -stripprog=${STRIPPROG-strip} - -posix_glob='?' -initialize_posix_glob=' - test "$posix_glob" != "?" || { - if (set -f) 2>/dev/null; then - posix_glob= - else - posix_glob=: - fi - } -' - -posix_mkdir= - -# Desired mode of installed file. -mode=0755 - -chgrpcmd= -chmodcmd=$chmodprog -chowncmd= -mvcmd=$mvprog -rmcmd="$rmprog -f" -stripcmd= - -src= -dst= -dir_arg= -dst_arg= - -copy_on_change=false -no_target_directory= - -usage="\ -Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE - or: $0 [OPTION]... SRCFILES... DIRECTORY - or: $0 [OPTION]... -t DIRECTORY SRCFILES... - or: $0 [OPTION]... -d DIRECTORIES... - -In the 1st form, copy SRCFILE to DSTFILE. -In the 2nd and 3rd, copy all SRCFILES to DIRECTORY. -In the 4th, create DIRECTORIES. - -Options: - --help display this help and exit. - --version display version info and exit. - - -c (ignored) - -C install only if different (preserve the last data modification time) - -d create directories instead of installing files. - -g GROUP $chgrpprog installed files to GROUP. - -m MODE $chmodprog installed files to MODE. - -o USER $chownprog installed files to USER. - -s $stripprog installed files. - -S $stripprog installed files. - -t DIRECTORY install into DIRECTORY. - -T report an error if DSTFILE is a directory. - -Environment variables override the default commands: - CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG - RMPROG STRIPPROG -" - -while test $# -ne 0; do - case $1 in - -c) ;; - - -C) copy_on_change=true;; - - -d) dir_arg=true;; - - -g) chgrpcmd="$chgrpprog $2" - shift;; - - --help) echo "$usage"; exit $?;; - - -m) mode=$2 - case $mode in - *' '* | *' '* | *' -'* | *'*'* | *'?'* | *'['*) - echo "$0: invalid mode: $mode" >&2 - exit 1;; - esac - shift;; - - -o) chowncmd="$chownprog $2" - shift;; - - -s) stripcmd=$stripprog;; - - -S) stripcmd="$stripprog $2" - shift;; - - -t) dst_arg=$2 - shift;; - - -T) no_target_directory=true;; - - --version) echo "$0 $scriptversion"; exit $?;; - - --) shift - break;; - - -*) echo "$0: invalid option: $1" >&2 - exit 1;; - - *) break;; - esac - shift -done - -if test $# -ne 0 && test -z "$dir_arg$dst_arg"; then - # When -d is used, all remaining arguments are directories to create. - # When -t is used, the destination is already specified. - # Otherwise, the last argument is the destination. Remove it from $@. - for arg - do - if test -n "$dst_arg"; then - # $@ is not empty: it contains at least $arg. - set fnord "$@" "$dst_arg" - shift # fnord - fi - shift # arg - dst_arg=$arg - done -fi - -if test $# -eq 0; then - if test -z "$dir_arg"; then - echo "$0: no input file specified." >&2 - exit 1 - fi - # It's OK to call `install-sh -d' without argument. - # This can happen when creating conditional directories. - exit 0 -fi - -if test -z "$dir_arg"; then - do_exit='(exit $ret); exit $ret' - trap "ret=129; $do_exit" 1 - trap "ret=130; $do_exit" 2 - trap "ret=141; $do_exit" 13 - trap "ret=143; $do_exit" 15 - - # Set umask so as not to create temps with too-generous modes. - # However, 'strip' requires both read and write access to temps. - case $mode in - # Optimize common cases. - *644) cp_umask=133;; - *755) cp_umask=22;; - - *[0-7]) - if test -z "$stripcmd"; then - u_plus_rw= - else - u_plus_rw='% 200' - fi - cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;; - *) - if test -z "$stripcmd"; then - u_plus_rw= - else - u_plus_rw=,u+rw - fi - cp_umask=$mode$u_plus_rw;; - esac -fi - -for src -do - # Protect names starting with `-'. - case $src in - -*) src=./$src;; - esac - - if test -n "$dir_arg"; then - dst=$src - dstdir=$dst - test -d "$dstdir" - dstdir_status=$? - else - - # Waiting for this to be detected by the "$cpprog $src $dsttmp" command - # might cause directories to be created, which would be especially bad - # if $src (and thus $dsttmp) contains '*'. - if test ! -f "$src" && test ! -d "$src"; then - echo "$0: $src does not exist." >&2 - exit 1 - fi - - if test -z "$dst_arg"; then - echo "$0: no destination specified." >&2 - exit 1 - fi - - dst=$dst_arg - # Protect names starting with `-'. - case $dst in - -*) dst=./$dst;; - esac - - # If destination is a directory, append the input filename; won't work - # if double slashes aren't ignored. - if test -d "$dst"; then - if test -n "$no_target_directory"; then - echo "$0: $dst_arg: Is a directory" >&2 - exit 1 - fi - dstdir=$dst - dst=$dstdir/`basename "$src"` - dstdir_status=0 - else - # Prefer dirname, but fall back on a substitute if dirname fails. - dstdir=` - (dirname "$dst") 2>/dev/null || - expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \ - X"$dst" : 'X\(//\)[^/]' \| \ - X"$dst" : 'X\(//\)$' \| \ - X"$dst" : 'X\(/\)' \| . 2>/dev/null || - echo X"$dst" | - sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{ - s//\1/ - q - } - /^X\(\/\/\)[^/].*/{ - s//\1/ - q - } - /^X\(\/\/\)$/{ - s//\1/ - q - } - /^X\(\/\).*/{ - s//\1/ - q - } - s/.*/./; q' - ` - - test -d "$dstdir" - dstdir_status=$? - fi - fi - - obsolete_mkdir_used=false - - if test $dstdir_status != 0; then - case $posix_mkdir in - '') - # Create intermediate dirs using mode 755 as modified by the umask. - # This is like FreeBSD 'install' as of 1997-10-28. - umask=`umask` - case $stripcmd.$umask in - # Optimize common cases. - *[2367][2367]) mkdir_umask=$umask;; - .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;; - - *[0-7]) - mkdir_umask=`expr $umask + 22 \ - - $umask % 100 % 40 + $umask % 20 \ - - $umask % 10 % 4 + $umask % 2 - `;; - *) mkdir_umask=$umask,go-w;; - esac - - # With -d, create the new directory with the user-specified mode. - # Otherwise, rely on $mkdir_umask. - if test -n "$dir_arg"; then - mkdir_mode=-m$mode - else - mkdir_mode= - fi - - posix_mkdir=false - case $umask in - *[123567][0-7][0-7]) - # POSIX mkdir -p sets u+wx bits regardless of umask, which - # is incompatible with FreeBSD 'install' when (umask & 300) != 0. - ;; - *) - tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$ - trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0 - - if (umask $mkdir_umask && - exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1 - then - if test -z "$dir_arg" || { - # Check for POSIX incompatibilities with -m. - # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or - # other-writeable bit of parent directory when it shouldn't. - # FreeBSD 6.1 mkdir -m -p sets mode of existing directory. - ls_ld_tmpdir=`ls -ld "$tmpdir"` - case $ls_ld_tmpdir in - d????-?r-*) different_mode=700;; - d????-?--*) different_mode=755;; - *) false;; - esac && - $mkdirprog -m$different_mode -p -- "$tmpdir" && { - ls_ld_tmpdir_1=`ls -ld "$tmpdir"` - test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1" - } - } - then posix_mkdir=: - fi - rmdir "$tmpdir/d" "$tmpdir" - else - # Remove any dirs left behind by ancient mkdir implementations. - rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null - fi - trap '' 0;; - esac;; - esac - - if - $posix_mkdir && ( - umask $mkdir_umask && - $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir" - ) - then : - else - - # The umask is ridiculous, or mkdir does not conform to POSIX, - # or it failed possibly due to a race condition. Create the - # directory the slow way, step by step, checking for races as we go. - - case $dstdir in - /*) prefix='/';; - -*) prefix='./';; - *) prefix='';; - esac - - eval "$initialize_posix_glob" - - oIFS=$IFS - IFS=/ - $posix_glob set -f - set fnord $dstdir - shift - $posix_glob set +f - IFS=$oIFS - - prefixes= - - for d - do - test -z "$d" && continue - - prefix=$prefix$d - if test -d "$prefix"; then - prefixes= - else - if $posix_mkdir; then - (umask=$mkdir_umask && - $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break - # Don't fail if two instances are running concurrently. - test -d "$prefix" || exit 1 - else - case $prefix in - *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;; - *) qprefix=$prefix;; - esac - prefixes="$prefixes '$qprefix'" - fi - fi - prefix=$prefix/ - done - - if test -n "$prefixes"; then - # Don't fail if two instances are running concurrently. - (umask $mkdir_umask && - eval "\$doit_exec \$mkdirprog $prefixes") || - test -d "$dstdir" || exit 1 - obsolete_mkdir_used=true - fi - fi - fi - - if test -n "$dir_arg"; then - { test -z "$chowncmd" || $doit $chowncmd "$dst"; } && - { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } && - { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false || - test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1 - else - - # Make a couple of temp file names in the proper directory. - dsttmp=$dstdir/_inst.$$_ - rmtmp=$dstdir/_rm.$$_ - - # Trap to clean up those temp files at exit. - trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0 - - # Copy the file name to the temp name. - (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") && - - # and set any options; do chmod last to preserve setuid bits. - # - # If any of these fail, we abort the whole thing. If we want to - # ignore errors from any of these, just make sure not to ignore - # errors from the above "$doit $cpprog $src $dsttmp" command. - # - { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } && - { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } && - { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } && - { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } && - - # If -C, don't bother to copy if it wouldn't change the file. - if $copy_on_change && - old=`LC_ALL=C ls -dlL "$dst" 2>/dev/null` && - new=`LC_ALL=C ls -dlL "$dsttmp" 2>/dev/null` && - - eval "$initialize_posix_glob" && - $posix_glob set -f && - set X $old && old=:$2:$4:$5:$6 && - set X $new && new=:$2:$4:$5:$6 && - $posix_glob set +f && - - test "$old" = "$new" && - $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1 - then - rm -f "$dsttmp" - else - # Rename the file to the real destination. - $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null || - - # The rename failed, perhaps because mv can't rename something else - # to itself, or perhaps because mv is so ancient that it does not - # support -f. - { - # Now remove or move aside any old file at destination location. - # We try this two ways since rm can't unlink itself on some - # systems and the destination file might be busy for other - # reasons. In this case, the final cleanup might fail but the new - # file should still install successfully. - { - test ! -f "$dst" || - $doit $rmcmd -f "$dst" 2>/dev/null || - { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null && - { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; } - } || - { echo "$0: cannot unlink or rename $dst" >&2 - (exit 1); exit 1 - } - } && - - # Now rename the file to the real destination. - $doit $mvcmd "$dsttmp" "$dst" - } - fi || exit 1 - - trap '' 0 - fi -done - -# Local variables: -# eval: (add-hook 'write-file-hooks 'time-stamp) -# time-stamp-start: "scriptversion=" -# time-stamp-format: "%:y-%02m-%02d.%02H" -# time-stamp-time-zone: "UTC" -# time-stamp-end: "; # UTC" -# End: diff --git a/autoconf/tea/tclconfig/tcl.m4 b/autoconf/tea/tclconfig/tcl.m4 deleted file mode 100644 index 4b4bd1e888..0000000000 --- a/autoconf/tea/tclconfig/tcl.m4 +++ /dev/null @@ -1,4168 +0,0 @@ -# tcl.m4 -- -# -# This file provides a set of autoconf macros to help TEA-enable -# a Tcl extension. -# -# Copyright (c) 1999-2000 Ajuba Solutions. -# Copyright (c) 2002-2005 ActiveState Corporation. -# -# See the file "license.terms" for information on usage and redistribution -# of this file, and for a DISCLAIMER OF ALL WARRANTIES. - -AC_PREREQ(2.57) - -dnl TEA extensions pass us the version of TEA they think they -dnl are compatible with (must be set in TEA_INIT below) -dnl TEA_VERSION="3.9" - -# Possible values for key variables defined: -# -# TEA_WINDOWINGSYSTEM - win32 aqua x11 (mirrors 'tk windowingsystem') -# TEA_PLATFORM - windows unix -# - -#------------------------------------------------------------------------ -# TEA_PATH_TCLCONFIG -- -# -# Locate the tclConfig.sh file and perform a sanity check on -# the Tcl compile flags -# -# Arguments: -# none -# -# Results: -# -# Adds the following arguments to configure: -# --with-tcl=... -# -# Defines the following vars: -# TCL_BIN_DIR Full path to the directory containing -# the tclConfig.sh file -#------------------------------------------------------------------------ - -AC_DEFUN([TEA_PATH_TCLCONFIG], [ - dnl TEA specific: Make sure we are initialized - AC_REQUIRE([TEA_INIT]) - # - # Ok, lets find the tcl configuration - # First, look for one uninstalled. - # the alternative search directory is invoked by --with-tcl - # - - if test x"${no_tcl}" = x ; then - # we reset no_tcl in case something fails here - no_tcl=true - AC_ARG_WITH(tcl, - AC_HELP_STRING([--with-tcl], - [directory containing tcl configuration (tclConfig.sh)]), - with_tclconfig="${withval}") - AC_MSG_CHECKING([for Tcl configuration]) - AC_CACHE_VAL(ac_cv_c_tclconfig,[ - - # First check to see if --with-tcl was specified. - if test x"${with_tclconfig}" != x ; then - case "${with_tclconfig}" in - */tclConfig.sh ) - if test -f "${with_tclconfig}"; then - AC_MSG_WARN([--with-tcl argument should refer to directory containing tclConfig.sh, not to tclConfig.sh itself]) - with_tclconfig="`echo "${with_tclconfig}" | sed 's!/tclConfig\.sh$!!'`" - fi ;; - esac - if test -f "${with_tclconfig}/tclConfig.sh" ; then - ac_cv_c_tclconfig="`(cd "${with_tclconfig}"; pwd)`" - else - AC_MSG_ERROR([${with_tclconfig} directory doesn't contain tclConfig.sh]) - fi - fi - - # then check for a private Tcl installation - if test x"${ac_cv_c_tclconfig}" = x ; then - for i in \ - ../tcl \ - `ls -dr ../tcl[[8-9]].[[0-9]].[[0-9]]* 2>/dev/null` \ - `ls -dr ../tcl[[8-9]].[[0-9]] 2>/dev/null` \ - `ls -dr ../tcl[[8-9]].[[0-9]]* 2>/dev/null` \ - ../../tcl \ - `ls -dr ../../tcl[[8-9]].[[0-9]].[[0-9]]* 2>/dev/null` \ - `ls -dr ../../tcl[[8-9]].[[0-9]] 2>/dev/null` \ - `ls -dr ../../tcl[[8-9]].[[0-9]]* 2>/dev/null` \ - ../../../tcl \ - `ls -dr ../../../tcl[[8-9]].[[0-9]].[[0-9]]* 2>/dev/null` \ - `ls -dr ../../../tcl[[8-9]].[[0-9]] 2>/dev/null` \ - `ls -dr ../../../tcl[[8-9]].[[0-9]]* 2>/dev/null` ; do - if test "${TEA_PLATFORM}" = "windows" \ - -a -f "$i/win/tclConfig.sh" ; then - ac_cv_c_tclconfig="`(cd $i/win; pwd)`" - break - fi - if test -f "$i/unix/tclConfig.sh" ; then - ac_cv_c_tclconfig="`(cd $i/unix; pwd)`" - break - fi - done - fi - - # on Darwin, check in Framework installation locations - if test "`uname -s`" = "Darwin" -a x"${ac_cv_c_tclconfig}" = x ; then - for i in `ls -d ~/Library/Frameworks 2>/dev/null` \ - `ls -d /Library/Frameworks 2>/dev/null` \ - `ls -d /Network/Library/Frameworks 2>/dev/null` \ - `ls -d /System/Library/Frameworks 2>/dev/null` \ - ; do - if test -f "$i/Tcl.framework/tclConfig.sh" ; then - ac_cv_c_tclconfig="`(cd $i/Tcl.framework; pwd)`" - break - fi - done - fi - - # TEA specific: on Windows, check in common installation locations - if test "${TEA_PLATFORM}" = "windows" \ - -a x"${ac_cv_c_tclconfig}" = x ; then - for i in `ls -d C:/Tcl/lib 2>/dev/null` \ - `ls -d C:/Progra~1/Tcl/lib 2>/dev/null` \ - ; do - if test -f "$i/tclConfig.sh" ; then - ac_cv_c_tclconfig="`(cd $i; pwd)`" - break - fi - done - fi - - # check in a few common install locations - if test x"${ac_cv_c_tclconfig}" = x ; then - for i in `ls -d ${libdir} 2>/dev/null` \ - `ls -d ${exec_prefix}/lib 2>/dev/null` \ - `ls -d ${prefix}/lib 2>/dev/null` \ - `ls -d /usr/local/lib 2>/dev/null` \ - `ls -d /usr/contrib/lib 2>/dev/null` \ - `ls -d /usr/lib 2>/dev/null` \ - `ls -d /usr/lib64 2>/dev/null` \ - `ls -d /usr/lib/tcl8.6 2>/dev/null` \ - `ls -d /usr/lib/tcl8.5 2>/dev/null` \ - ; do - if test -f "$i/tclConfig.sh" ; then - ac_cv_c_tclconfig="`(cd $i; pwd)`" - break - fi - done - fi - - # check in a few other private locations - if test x"${ac_cv_c_tclconfig}" = x ; then - for i in \ - ${srcdir}/../tcl \ - `ls -dr ${srcdir}/../tcl[[8-9]].[[0-9]].[[0-9]]* 2>/dev/null` \ - `ls -dr ${srcdir}/../tcl[[8-9]].[[0-9]] 2>/dev/null` \ - `ls -dr ${srcdir}/../tcl[[8-9]].[[0-9]]* 2>/dev/null` ; do - if test "${TEA_PLATFORM}" = "windows" \ - -a -f "$i/win/tclConfig.sh" ; then - ac_cv_c_tclconfig="`(cd $i/win; pwd)`" - break - fi - if test -f "$i/unix/tclConfig.sh" ; then - ac_cv_c_tclconfig="`(cd $i/unix; pwd)`" - break - fi - done - fi - ]) - - if test x"${ac_cv_c_tclconfig}" = x ; then - TCL_BIN_DIR="# no Tcl configs found" - AC_MSG_ERROR([Can't find Tcl configuration definitions. Use --with-tcl to specify a directory containing tclConfig.sh]) - else - no_tcl= - TCL_BIN_DIR="${ac_cv_c_tclconfig}" - AC_MSG_RESULT([found ${TCL_BIN_DIR}/tclConfig.sh]) - fi - fi -]) - -#------------------------------------------------------------------------ -# TEA_PATH_TKCONFIG -- -# -# Locate the tkConfig.sh file -# -# Arguments: -# none -# -# Results: -# -# Adds the following arguments to configure: -# --with-tk=... -# -# Defines the following vars: -# TK_BIN_DIR Full path to the directory containing -# the tkConfig.sh file -#------------------------------------------------------------------------ - -AC_DEFUN([TEA_PATH_TKCONFIG], [ - # - # Ok, lets find the tk configuration - # First, look for one uninstalled. - # the alternative search directory is invoked by --with-tk - # - - if test x"${no_tk}" = x ; then - # we reset no_tk in case something fails here - no_tk=true - AC_ARG_WITH(tk, - AC_HELP_STRING([--with-tk], - [directory containing tk configuration (tkConfig.sh)]), - with_tkconfig="${withval}") - AC_MSG_CHECKING([for Tk configuration]) - AC_CACHE_VAL(ac_cv_c_tkconfig,[ - - # First check to see if --with-tkconfig was specified. - if test x"${with_tkconfig}" != x ; then - case "${with_tkconfig}" in - */tkConfig.sh ) - if test -f "${with_tkconfig}"; then - AC_MSG_WARN([--with-tk argument should refer to directory containing tkConfig.sh, not to tkConfig.sh itself]) - with_tkconfig="`echo "${with_tkconfig}" | sed 's!/tkConfig\.sh$!!'`" - fi ;; - esac - if test -f "${with_tkconfig}/tkConfig.sh" ; then - ac_cv_c_tkconfig="`(cd "${with_tkconfig}"; pwd)`" - else - AC_MSG_ERROR([${with_tkconfig} directory doesn't contain tkConfig.sh]) - fi - fi - - # then check for a private Tk library - if test x"${ac_cv_c_tkconfig}" = x ; then - for i in \ - ../tk \ - `ls -dr ../tk[[8-9]].[[0-9]].[[0-9]]* 2>/dev/null` \ - `ls -dr ../tk[[8-9]].[[0-9]] 2>/dev/null` \ - `ls -dr ../tk[[8-9]].[[0-9]]* 2>/dev/null` \ - ../../tk \ - `ls -dr ../../tk[[8-9]].[[0-9]].[[0-9]]* 2>/dev/null` \ - `ls -dr ../../tk[[8-9]].[[0-9]] 2>/dev/null` \ - `ls -dr ../../tk[[8-9]].[[0-9]]* 2>/dev/null` \ - ../../../tk \ - `ls -dr ../../../tk[[8-9]].[[0-9]].[[0-9]]* 2>/dev/null` \ - `ls -dr ../../../tk[[8-9]].[[0-9]] 2>/dev/null` \ - `ls -dr ../../../tk[[8-9]].[[0-9]]* 2>/dev/null` ; do - if test "${TEA_PLATFORM}" = "windows" \ - -a -f "$i/win/tkConfig.sh" ; then - ac_cv_c_tkconfig="`(cd $i/win; pwd)`" - break - fi - if test -f "$i/unix/tkConfig.sh" ; then - ac_cv_c_tkconfig="`(cd $i/unix; pwd)`" - break - fi - done - fi - - # on Darwin, check in Framework installation locations - if test "`uname -s`" = "Darwin" -a x"${ac_cv_c_tkconfig}" = x ; then - for i in `ls -d ~/Library/Frameworks 2>/dev/null` \ - `ls -d /Library/Frameworks 2>/dev/null` \ - `ls -d /Network/Library/Frameworks 2>/dev/null` \ - `ls -d /System/Library/Frameworks 2>/dev/null` \ - ; do - if test -f "$i/Tk.framework/tkConfig.sh" ; then - ac_cv_c_tkconfig="`(cd $i/Tk.framework; pwd)`" - break - fi - done - fi - - # check in a few common install locations - if test x"${ac_cv_c_tkconfig}" = x ; then - for i in `ls -d ${libdir} 2>/dev/null` \ - `ls -d ${exec_prefix}/lib 2>/dev/null` \ - `ls -d ${prefix}/lib 2>/dev/null` \ - `ls -d /usr/local/lib 2>/dev/null` \ - `ls -d /usr/contrib/lib 2>/dev/null` \ - `ls -d /usr/lib 2>/dev/null` \ - `ls -d /usr/lib64 2>/dev/null` \ - ; do - if test -f "$i/tkConfig.sh" ; then - ac_cv_c_tkconfig="`(cd $i; pwd)`" - break - fi - done - fi - - # TEA specific: on Windows, check in common installation locations - if test "${TEA_PLATFORM}" = "windows" \ - -a x"${ac_cv_c_tkconfig}" = x ; then - for i in `ls -d C:/Tcl/lib 2>/dev/null` \ - `ls -d C:/Progra~1/Tcl/lib 2>/dev/null` \ - ; do - if test -f "$i/tkConfig.sh" ; then - ac_cv_c_tkconfig="`(cd $i; pwd)`" - break - fi - done - fi - - # check in a few other private locations - if test x"${ac_cv_c_tkconfig}" = x ; then - for i in \ - ${srcdir}/../tk \ - `ls -dr ${srcdir}/../tk[[8-9]].[[0-9]].[[0-9]]* 2>/dev/null` \ - `ls -dr ${srcdir}/../tk[[8-9]].[[0-9]] 2>/dev/null` \ - `ls -dr ${srcdir}/../tk[[8-9]].[[0-9]]* 2>/dev/null` ; do - if test "${TEA_PLATFORM}" = "windows" \ - -a -f "$i/win/tkConfig.sh" ; then - ac_cv_c_tkconfig="`(cd $i/win; pwd)`" - break - fi - if test -f "$i/unix/tkConfig.sh" ; then - ac_cv_c_tkconfig="`(cd $i/unix; pwd)`" - break - fi - done - fi - ]) - - if test x"${ac_cv_c_tkconfig}" = x ; then - TK_BIN_DIR="# no Tk configs found" - AC_MSG_ERROR([Can't find Tk configuration definitions. Use --with-tk to specify a directory containing tkConfig.sh]) - else - no_tk= - TK_BIN_DIR="${ac_cv_c_tkconfig}" - AC_MSG_RESULT([found ${TK_BIN_DIR}/tkConfig.sh]) - fi - fi -]) - -#------------------------------------------------------------------------ -# TEA_LOAD_TCLCONFIG -- -# -# Load the tclConfig.sh file -# -# Arguments: -# -# Requires the following vars to be set: -# TCL_BIN_DIR -# -# Results: -# -# Substitutes the following vars: -# TCL_BIN_DIR -# TCL_SRC_DIR -# TCL_LIB_FILE -#------------------------------------------------------------------------ - -AC_DEFUN([TEA_LOAD_TCLCONFIG], [ - AC_MSG_CHECKING([for existence of ${TCL_BIN_DIR}/tclConfig.sh]) - - if test -f "${TCL_BIN_DIR}/tclConfig.sh" ; then - AC_MSG_RESULT([loading]) - . "${TCL_BIN_DIR}/tclConfig.sh" - else - AC_MSG_RESULT([could not find ${TCL_BIN_DIR}/tclConfig.sh]) - fi - - # eval is required to do the TCL_DBGX substitution - eval "TCL_LIB_FILE=\"${TCL_LIB_FILE}\"" - eval "TCL_STUB_LIB_FILE=\"${TCL_STUB_LIB_FILE}\"" - - # If the TCL_BIN_DIR is the build directory (not the install directory), - # then set the common variable name to the value of the build variables. - # For example, the variable TCL_LIB_SPEC will be set to the value - # of TCL_BUILD_LIB_SPEC. An extension should make use of TCL_LIB_SPEC - # instead of TCL_BUILD_LIB_SPEC since it will work with both an - # installed and uninstalled version of Tcl. - if test -f "${TCL_BIN_DIR}/Makefile" ; then - TCL_LIB_SPEC="${TCL_BUILD_LIB_SPEC}" - TCL_STUB_LIB_SPEC="${TCL_BUILD_STUB_LIB_SPEC}" - TCL_STUB_LIB_PATH="${TCL_BUILD_STUB_LIB_PATH}" - elif test "`uname -s`" = "Darwin"; then - # If Tcl was built as a framework, attempt to use the libraries - # from the framework at the given location so that linking works - # against Tcl.framework installed in an arbitrary location. - case ${TCL_DEFS} in - *TCL_FRAMEWORK*) - if test -f "${TCL_BIN_DIR}/${TCL_LIB_FILE}"; then - for i in "`cd "${TCL_BIN_DIR}"; pwd`" \ - "`cd "${TCL_BIN_DIR}"/../..; pwd`"; do - if test "`basename "$i"`" = "${TCL_LIB_FILE}.framework"; then - TCL_LIB_SPEC="-F`dirname "$i" | sed -e 's/ /\\\\ /g'` -framework ${TCL_LIB_FILE}" - break - fi - done - fi - if test -f "${TCL_BIN_DIR}/${TCL_STUB_LIB_FILE}"; then - TCL_STUB_LIB_SPEC="-L`echo "${TCL_BIN_DIR}" | sed -e 's/ /\\\\ /g'` ${TCL_STUB_LIB_FLAG}" - TCL_STUB_LIB_PATH="${TCL_BIN_DIR}/${TCL_STUB_LIB_FILE}" - fi - ;; - esac - fi - - # eval is required to do the TCL_DBGX substitution - eval "TCL_LIB_FLAG=\"${TCL_LIB_FLAG}\"" - eval "TCL_LIB_SPEC=\"${TCL_LIB_SPEC}\"" - eval "TCL_STUB_LIB_FLAG=\"${TCL_STUB_LIB_FLAG}\"" - eval "TCL_STUB_LIB_SPEC=\"${TCL_STUB_LIB_SPEC}\"" - - AC_SUBST(TCL_VERSION) - AC_SUBST(TCL_PATCH_LEVEL) - AC_SUBST(TCL_BIN_DIR) - AC_SUBST(TCL_SRC_DIR) - - AC_SUBST(TCL_LIB_FILE) - AC_SUBST(TCL_LIB_FLAG) - AC_SUBST(TCL_LIB_SPEC) - - AC_SUBST(TCL_STUB_LIB_FILE) - AC_SUBST(TCL_STUB_LIB_FLAG) - AC_SUBST(TCL_STUB_LIB_SPEC) - - AC_MSG_CHECKING([platform]) - hold_cc=$CC; CC="$TCL_CC" - AC_TRY_COMPILE(,[ - #ifdef _WIN32 - #error win32 - #endif - ], TEA_PLATFORM="unix", - TEA_PLATFORM="windows" - ) - CC=$hold_cc - AC_MSG_RESULT($TEA_PLATFORM) - - # The BUILD_$pkg is to define the correct extern storage class - # handling when making this package - AC_DEFINE_UNQUOTED(BUILD_${PACKAGE_NAME}, [], - [Building extension source?]) - # Do this here as we have fully defined TEA_PLATFORM now - if test "${TEA_PLATFORM}" = "windows" ; then - EXEEXT=".exe" - CLEANFILES="$CLEANFILES *.lib *.dll *.pdb *.exp" - fi - - # TEA specific: - AC_SUBST(CLEANFILES) - AC_SUBST(TCL_LIBS) - AC_SUBST(TCL_DEFS) - AC_SUBST(TCL_EXTRA_CFLAGS) - AC_SUBST(TCL_LD_FLAGS) - AC_SUBST(TCL_SHLIB_LD_LIBS) -]) - -#------------------------------------------------------------------------ -# TEA_LOAD_TKCONFIG -- -# -# Load the tkConfig.sh file -# -# Arguments: -# -# Requires the following vars to be set: -# TK_BIN_DIR -# -# Results: -# -# Sets the following vars that should be in tkConfig.sh: -# TK_BIN_DIR -#------------------------------------------------------------------------ - -AC_DEFUN([TEA_LOAD_TKCONFIG], [ - AC_MSG_CHECKING([for existence of ${TK_BIN_DIR}/tkConfig.sh]) - - if test -f "${TK_BIN_DIR}/tkConfig.sh" ; then - AC_MSG_RESULT([loading]) - . "${TK_BIN_DIR}/tkConfig.sh" - else - AC_MSG_RESULT([could not find ${TK_BIN_DIR}/tkConfig.sh]) - fi - - # eval is required to do the TK_DBGX substitution - eval "TK_LIB_FILE=\"${TK_LIB_FILE}\"" - eval "TK_STUB_LIB_FILE=\"${TK_STUB_LIB_FILE}\"" - - # If the TK_BIN_DIR is the build directory (not the install directory), - # then set the common variable name to the value of the build variables. - # For example, the variable TK_LIB_SPEC will be set to the value - # of TK_BUILD_LIB_SPEC. An extension should make use of TK_LIB_SPEC - # instead of TK_BUILD_LIB_SPEC since it will work with both an - # installed and uninstalled version of Tcl. - if test -f "${TK_BIN_DIR}/Makefile" ; then - TK_LIB_SPEC="${TK_BUILD_LIB_SPEC}" - TK_STUB_LIB_SPEC="${TK_BUILD_STUB_LIB_SPEC}" - TK_STUB_LIB_PATH="${TK_BUILD_STUB_LIB_PATH}" - elif test "`uname -s`" = "Darwin"; then - # If Tk was built as a framework, attempt to use the libraries - # from the framework at the given location so that linking works - # against Tk.framework installed in an arbitrary location. - case ${TK_DEFS} in - *TK_FRAMEWORK*) - if test -f "${TK_BIN_DIR}/${TK_LIB_FILE}"; then - for i in "`cd "${TK_BIN_DIR}"; pwd`" \ - "`cd "${TK_BIN_DIR}"/../..; pwd`"; do - if test "`basename "$i"`" = "${TK_LIB_FILE}.framework"; then - TK_LIB_SPEC="-F`dirname "$i" | sed -e 's/ /\\\\ /g'` -framework ${TK_LIB_FILE}" - break - fi - done - fi - if test -f "${TK_BIN_DIR}/${TK_STUB_LIB_FILE}"; then - TK_STUB_LIB_SPEC="-L` echo "${TK_BIN_DIR}" | sed -e 's/ /\\\\ /g'` ${TK_STUB_LIB_FLAG}" - TK_STUB_LIB_PATH="${TK_BIN_DIR}/${TK_STUB_LIB_FILE}" - fi - ;; - esac - fi - - # eval is required to do the TK_DBGX substitution - eval "TK_LIB_FLAG=\"${TK_LIB_FLAG}\"" - eval "TK_LIB_SPEC=\"${TK_LIB_SPEC}\"" - eval "TK_STUB_LIB_FLAG=\"${TK_STUB_LIB_FLAG}\"" - eval "TK_STUB_LIB_SPEC=\"${TK_STUB_LIB_SPEC}\"" - - # TEA specific: Ensure windowingsystem is defined - if test "${TEA_PLATFORM}" = "unix" ; then - case ${TK_DEFS} in - *MAC_OSX_TK*) - AC_DEFINE(MAC_OSX_TK, 1, [Are we building against Mac OS X TkAqua?]) - TEA_WINDOWINGSYSTEM="aqua" - ;; - *) - TEA_WINDOWINGSYSTEM="x11" - ;; - esac - elif test "${TEA_PLATFORM}" = "windows" ; then - TEA_WINDOWINGSYSTEM="win32" - fi - - AC_SUBST(TK_VERSION) - AC_SUBST(TK_BIN_DIR) - AC_SUBST(TK_SRC_DIR) - - AC_SUBST(TK_LIB_FILE) - AC_SUBST(TK_LIB_FLAG) - AC_SUBST(TK_LIB_SPEC) - - AC_SUBST(TK_STUB_LIB_FILE) - AC_SUBST(TK_STUB_LIB_FLAG) - AC_SUBST(TK_STUB_LIB_SPEC) - - # TEA specific: - AC_SUBST(TK_LIBS) - AC_SUBST(TK_XINCLUDES) -]) - -#------------------------------------------------------------------------ -# TEA_PROG_TCLSH -# Determine the fully qualified path name of the tclsh executable -# in the Tcl build directory or the tclsh installed in a bin -# directory. This macro will correctly determine the name -# of the tclsh executable even if tclsh has not yet been -# built in the build directory. The tclsh found is always -# associated with a tclConfig.sh file. This tclsh should be used -# only for running extension test cases. It should never be -# or generation of files (like pkgIndex.tcl) at build time. -# -# Arguments: -# none -# -# Results: -# Substitutes the following vars: -# TCLSH_PROG -#------------------------------------------------------------------------ - -AC_DEFUN([TEA_PROG_TCLSH], [ - AC_MSG_CHECKING([for tclsh]) - if test -f "${TCL_BIN_DIR}/Makefile" ; then - # tclConfig.sh is in Tcl build directory - if test "${TEA_PLATFORM}" = "windows"; then - TCLSH_PROG="${TCL_BIN_DIR}/tclsh${TCL_MAJOR_VERSION}${TCL_MINOR_VERSION}${TCL_DBGX}${EXEEXT}" - else - TCLSH_PROG="${TCL_BIN_DIR}/tclsh" - fi - else - # tclConfig.sh is in install location - if test "${TEA_PLATFORM}" = "windows"; then - TCLSH_PROG="tclsh${TCL_MAJOR_VERSION}${TCL_MINOR_VERSION}${TCL_DBGX}${EXEEXT}" - else - TCLSH_PROG="tclsh${TCL_MAJOR_VERSION}.${TCL_MINOR_VERSION}${TCL_DBGX}" - fi - list="`ls -d ${TCL_BIN_DIR}/../bin 2>/dev/null` \ - `ls -d ${TCL_BIN_DIR}/.. 2>/dev/null` \ - `ls -d ${TCL_PREFIX}/bin 2>/dev/null`" - for i in $list ; do - if test -f "$i/${TCLSH_PROG}" ; then - REAL_TCL_BIN_DIR="`cd "$i"; pwd`/" - break - fi - done - TCLSH_PROG="${REAL_TCL_BIN_DIR}${TCLSH_PROG}" - fi - AC_MSG_RESULT([${TCLSH_PROG}]) - AC_SUBST(TCLSH_PROG) -]) - -#------------------------------------------------------------------------ -# TEA_PROG_WISH -# Determine the fully qualified path name of the wish executable -# in the Tk build directory or the wish installed in a bin -# directory. This macro will correctly determine the name -# of the wish executable even if wish has not yet been -# built in the build directory. The wish found is always -# associated with a tkConfig.sh file. This wish should be used -# only for running extension test cases. It should never be -# or generation of files (like pkgIndex.tcl) at build time. -# -# Arguments: -# none -# -# Results: -# Substitutes the following vars: -# WISH_PROG -#------------------------------------------------------------------------ - -AC_DEFUN([TEA_PROG_WISH], [ - AC_MSG_CHECKING([for wish]) - if test -f "${TK_BIN_DIR}/Makefile" ; then - # tkConfig.sh is in Tk build directory - if test "${TEA_PLATFORM}" = "windows"; then - WISH_PROG="${TK_BIN_DIR}/wish${TK_MAJOR_VERSION}${TK_MINOR_VERSION}${TK_DBGX}${EXEEXT}" - else - WISH_PROG="${TK_BIN_DIR}/wish" - fi - else - # tkConfig.sh is in install location - if test "${TEA_PLATFORM}" = "windows"; then - WISH_PROG="wish${TK_MAJOR_VERSION}${TK_MINOR_VERSION}${TK_DBGX}${EXEEXT}" - else - WISH_PROG="wish${TK_MAJOR_VERSION}.${TK_MINOR_VERSION}${TK_DBGX}" - fi - list="`ls -d ${TK_BIN_DIR}/../bin 2>/dev/null` \ - `ls -d ${TK_BIN_DIR}/.. 2>/dev/null` \ - `ls -d ${TK_PREFIX}/bin 2>/dev/null`" - for i in $list ; do - if test -f "$i/${WISH_PROG}" ; then - REAL_TK_BIN_DIR="`cd "$i"; pwd`/" - break - fi - done - WISH_PROG="${REAL_TK_BIN_DIR}${WISH_PROG}" - fi - AC_MSG_RESULT([${WISH_PROG}]) - AC_SUBST(WISH_PROG) -]) - -#------------------------------------------------------------------------ -# TEA_ENABLE_SHARED -- -# -# Allows the building of shared libraries -# -# Arguments: -# none -# -# Results: -# -# Adds the following arguments to configure: -# --enable-shared=yes|no -# -# Defines the following vars: -# STATIC_BUILD Used for building import/export libraries -# on Windows. -# -# Sets the following vars: -# SHARED_BUILD Value of 1 or 0 -#------------------------------------------------------------------------ - -AC_DEFUN([TEA_ENABLE_SHARED], [ - AC_MSG_CHECKING([how to build libraries]) - AC_ARG_ENABLE(shared, - AC_HELP_STRING([--enable-shared], - [build and link with shared libraries (default: on)]), - [tcl_ok=$enableval], [tcl_ok=yes]) - - if test "${enable_shared+set}" = set; then - enableval="$enable_shared" - tcl_ok=$enableval - else - tcl_ok=yes - fi - - if test "$tcl_ok" = "yes" ; then - AC_MSG_RESULT([shared]) - SHARED_BUILD=1 - else - AC_MSG_RESULT([static]) - SHARED_BUILD=0 - AC_DEFINE(STATIC_BUILD, 1, [Is this a static build?]) - fi - AC_SUBST(SHARED_BUILD) -]) - -#------------------------------------------------------------------------ -# TEA_ENABLE_THREADS -- -# -# Specify if thread support should be enabled. If "yes" is specified -# as an arg (optional), threads are enabled by default, "no" means -# threads are disabled. "yes" is the default. -# -# TCL_THREADS is checked so that if you are compiling an extension -# against a threaded core, your extension must be compiled threaded -# as well. -# -# Note that it is legal to have a thread enabled extension run in a -# threaded or non-threaded Tcl core, but a non-threaded extension may -# only run in a non-threaded Tcl core. -# -# Arguments: -# none -# -# Results: -# -# Adds the following arguments to configure: -# --enable-threads -# -# Sets the following vars: -# THREADS_LIBS Thread library(s) -# -# Defines the following vars: -# TCL_THREADS -# _REENTRANT -# _THREAD_SAFE -#------------------------------------------------------------------------ - -AC_DEFUN([TEA_ENABLE_THREADS], [ - AC_ARG_ENABLE(threads, - AC_HELP_STRING([--enable-threads], - [build with threads]), - [tcl_ok=$enableval], [tcl_ok=yes]) - - if test "${enable_threads+set}" = set; then - enableval="$enable_threads" - tcl_ok=$enableval - else - tcl_ok=yes - fi - - if test "$tcl_ok" = "yes" -o "${TCL_THREADS}" = 1; then - TCL_THREADS=1 - - if test "${TEA_PLATFORM}" != "windows" ; then - # We are always OK on Windows, so check what this platform wants: - - # USE_THREAD_ALLOC tells us to try the special thread-based - # allocator that significantly reduces lock contention - AC_DEFINE(USE_THREAD_ALLOC, 1, - [Do we want to use the threaded memory allocator?]) - AC_DEFINE(_REENTRANT, 1, [Do we want the reentrant OS API?]) - if test "`uname -s`" = "SunOS" ; then - AC_DEFINE(_POSIX_PTHREAD_SEMANTICS, 1, - [Do we really want to follow the standard? Yes we do!]) - fi - AC_DEFINE(_THREAD_SAFE, 1, [Do we want the thread-safe OS API?]) - AC_CHECK_LIB(pthread,pthread_mutex_init,tcl_ok=yes,tcl_ok=no) - if test "$tcl_ok" = "no"; then - # Check a little harder for __pthread_mutex_init in the same - # library, as some systems hide it there until pthread.h is - # defined. We could alternatively do an AC_TRY_COMPILE with - # pthread.h, but that will work with libpthread really doesn't - # exist, like AIX 4.2. [Bug: 4359] - AC_CHECK_LIB(pthread, __pthread_mutex_init, - tcl_ok=yes, tcl_ok=no) - fi - - if test "$tcl_ok" = "yes"; then - # The space is needed - THREADS_LIBS=" -lpthread" - else - AC_CHECK_LIB(pthreads, pthread_mutex_init, - tcl_ok=yes, tcl_ok=no) - if test "$tcl_ok" = "yes"; then - # The space is needed - THREADS_LIBS=" -lpthreads" - else - AC_CHECK_LIB(c, pthread_mutex_init, - tcl_ok=yes, tcl_ok=no) - if test "$tcl_ok" = "no"; then - AC_CHECK_LIB(c_r, pthread_mutex_init, - tcl_ok=yes, tcl_ok=no) - if test "$tcl_ok" = "yes"; then - # The space is needed - THREADS_LIBS=" -pthread" - else - TCL_THREADS=0 - AC_MSG_WARN([Do not know how to find pthread lib on your system - thread support disabled]) - fi - fi - fi - fi - fi - else - TCL_THREADS=0 - fi - # Do checking message here to not mess up interleaved configure output - AC_MSG_CHECKING([for building with threads]) - if test "${TCL_THREADS}" = 1; then - AC_DEFINE(TCL_THREADS, 1, [Are we building with threads enabled?]) - AC_MSG_RESULT([yes (default)]) - else - AC_MSG_RESULT([no]) - fi - # TCL_THREADS sanity checking. See if our request for building with - # threads is the same as the way Tcl was built. If not, warn the user. - case ${TCL_DEFS} in - *THREADS=1*) - if test "${TCL_THREADS}" = "0"; then - AC_MSG_WARN([ - Building ${PACKAGE_NAME} without threads enabled, but building against Tcl - that IS thread-enabled. It is recommended to use --enable-threads.]) - fi - ;; - *) - if test "${TCL_THREADS}" = "1"; then - AC_MSG_WARN([ - --enable-threads requested, but building against a Tcl that is NOT - thread-enabled. This is an OK configuration that will also run in - a thread-enabled core.]) - fi - ;; - esac - AC_SUBST(TCL_THREADS) -]) - -#------------------------------------------------------------------------ -# TEA_ENABLE_SYMBOLS -- -# -# Specify if debugging symbols should be used. -# Memory (TCL_MEM_DEBUG) debugging can also be enabled. -# -# Arguments: -# none -# -# TEA varies from core Tcl in that C|LDFLAGS_DEFAULT receives -# the value of C|LDFLAGS_OPTIMIZE|DEBUG already substituted. -# Requires the following vars to be set in the Makefile: -# CFLAGS_DEFAULT -# LDFLAGS_DEFAULT -# -# Results: -# -# Adds the following arguments to configure: -# --enable-symbols -# -# Defines the following vars: -# CFLAGS_DEFAULT Sets to $(CFLAGS_DEBUG) if true -# Sets to "$(CFLAGS_OPTIMIZE) -DNDEBUG" if false -# LDFLAGS_DEFAULT Sets to $(LDFLAGS_DEBUG) if true -# Sets to $(LDFLAGS_OPTIMIZE) if false -# DBGX Formerly used as debug library extension; -# always blank now. -#------------------------------------------------------------------------ - -AC_DEFUN([TEA_ENABLE_SYMBOLS], [ - dnl TEA specific: Make sure we are initialized - AC_REQUIRE([TEA_CONFIG_CFLAGS]) - AC_MSG_CHECKING([for build with symbols]) - AC_ARG_ENABLE(symbols, - AC_HELP_STRING([--enable-symbols], - [build with debugging symbols (default: off)]), - [tcl_ok=$enableval], [tcl_ok=no]) - DBGX="" - if test "$tcl_ok" = "no"; then - CFLAGS_DEFAULT="${CFLAGS_OPTIMIZE} -DNDEBUG" - LDFLAGS_DEFAULT="${LDFLAGS_OPTIMIZE}" - AC_MSG_RESULT([no]) - else - CFLAGS_DEFAULT="${CFLAGS_DEBUG}" - LDFLAGS_DEFAULT="${LDFLAGS_DEBUG}" - if test "$tcl_ok" = "yes"; then - AC_MSG_RESULT([yes (standard debugging)]) - fi - fi - # TEA specific: - if test "${TEA_PLATFORM}" != "windows" ; then - LDFLAGS_DEFAULT="${LDFLAGS}" - fi - AC_SUBST(CFLAGS_DEFAULT) - AC_SUBST(LDFLAGS_DEFAULT) - AC_SUBST(TCL_DBGX) - - if test "$tcl_ok" = "mem" -o "$tcl_ok" = "all"; then - AC_DEFINE(TCL_MEM_DEBUG, 1, [Is memory debugging enabled?]) - fi - - if test "$tcl_ok" != "yes" -a "$tcl_ok" != "no"; then - if test "$tcl_ok" = "all"; then - AC_MSG_RESULT([enabled symbols mem debugging]) - else - AC_MSG_RESULT([enabled $tcl_ok debugging]) - fi - fi -]) - -#------------------------------------------------------------------------ -# TEA_ENABLE_LANGINFO -- -# -# Allows use of modern nl_langinfo check for better l10n. -# This is only relevant for Unix. -# -# Arguments: -# none -# -# Results: -# -# Adds the following arguments to configure: -# --enable-langinfo=yes|no (default is yes) -# -# Defines the following vars: -# HAVE_LANGINFO Triggers use of nl_langinfo if defined. -#------------------------------------------------------------------------ - -AC_DEFUN([TEA_ENABLE_LANGINFO], [ - AC_ARG_ENABLE(langinfo, - AC_HELP_STRING([--enable-langinfo], - [use nl_langinfo if possible to determine encoding at startup, otherwise use old heuristic (default: on)]), - [langinfo_ok=$enableval], [langinfo_ok=yes]) - - HAVE_LANGINFO=0 - if test "$langinfo_ok" = "yes"; then - AC_CHECK_HEADER(langinfo.h,[langinfo_ok=yes],[langinfo_ok=no]) - fi - AC_MSG_CHECKING([whether to use nl_langinfo]) - if test "$langinfo_ok" = "yes"; then - AC_CACHE_VAL(tcl_cv_langinfo_h, [ - AC_TRY_COMPILE([#include ], [nl_langinfo(CODESET);], - [tcl_cv_langinfo_h=yes],[tcl_cv_langinfo_h=no])]) - AC_MSG_RESULT([$tcl_cv_langinfo_h]) - if test $tcl_cv_langinfo_h = yes; then - AC_DEFINE(HAVE_LANGINFO, 1, [Do we have nl_langinfo()?]) - fi - else - AC_MSG_RESULT([$langinfo_ok]) - fi -]) - -#-------------------------------------------------------------------- -# TEA_CONFIG_SYSTEM -# -# Determine what the system is (some things cannot be easily checked -# on a feature-driven basis, alas). This can usually be done via the -# "uname" command. -# -# Arguments: -# none -# -# Results: -# Defines the following var: -# -# system - System/platform/version identification code. -#-------------------------------------------------------------------- - -AC_DEFUN([TEA_CONFIG_SYSTEM], [ - AC_CACHE_CHECK([system version], tcl_cv_sys_version, [ - # TEA specific: - if test "${TEA_PLATFORM}" = "windows" ; then - tcl_cv_sys_version=windows - else - tcl_cv_sys_version=`uname -s`-`uname -r` - if test "$?" -ne 0 ; then - AC_MSG_WARN([can't find uname command]) - tcl_cv_sys_version=unknown - else - if test "`uname -s`" = "AIX" ; then - tcl_cv_sys_version=AIX-`uname -v`.`uname -r` - fi - fi - fi - ]) - system=$tcl_cv_sys_version -]) - -#-------------------------------------------------------------------- -# TEA_CONFIG_CFLAGS -# -# Try to determine the proper flags to pass to the compiler -# for building shared libraries and other such nonsense. -# -# Arguments: -# none -# -# Results: -# -# Defines and substitutes the following vars: -# -# DL_OBJS, DL_LIBS - removed for TEA, only needed by core. -# LDFLAGS - Flags to pass to the compiler when linking object -# files into an executable application binary such -# as tclsh. -# LD_SEARCH_FLAGS-Flags to pass to ld, such as "-R /usr/local/tcl/lib", -# that tell the run-time dynamic linker where to look -# for shared libraries such as libtcl.so. Depends on -# the variable LIB_RUNTIME_DIR in the Makefile. Could -# be the same as CC_SEARCH_FLAGS if ${CC} is used to link. -# CC_SEARCH_FLAGS-Flags to pass to ${CC}, such as "-Wl,-rpath,/usr/local/tcl/lib", -# that tell the run-time dynamic linker where to look -# for shared libraries such as libtcl.so. Depends on -# the variable LIB_RUNTIME_DIR in the Makefile. -# SHLIB_CFLAGS - Flags to pass to cc when compiling the components -# of a shared library (may request position-independent -# code, among other things). -# SHLIB_LD - Base command to use for combining object files -# into a shared library. -# SHLIB_LD_LIBS - Dependent libraries for the linker to scan when -# creating shared libraries. This symbol typically -# goes at the end of the "ld" commands that build -# shared libraries. The value of the symbol defaults to -# "${LIBS}" if all of the dependent libraries should -# be specified when creating a shared library. If -# dependent libraries should not be specified (as on -# SunOS 4.x, where they cause the link to fail, or in -# general if Tcl and Tk aren't themselves shared -# libraries), then this symbol has an empty string -# as its value. -# SHLIB_SUFFIX - Suffix to use for the names of dynamically loadable -# extensions. An empty string means we don't know how -# to use shared libraries on this platform. -# LIB_SUFFIX - Specifies everything that comes after the "libfoo" -# in a static or shared library name, using the $PACKAGE_VERSION variable -# to put the version in the right place. This is used -# by platforms that need non-standard library names. -# Examples: ${PACKAGE_VERSION}.so.1.1 on NetBSD, since it needs -# to have a version after the .so, and ${PACKAGE_VERSION}.a -# on AIX, since a shared library needs to have -# a .a extension whereas shared objects for loadable -# extensions have a .so extension. Defaults to -# ${PACKAGE_VERSION}${SHLIB_SUFFIX}. -# CFLAGS_DEBUG - -# Flags used when running the compiler in debug mode -# CFLAGS_OPTIMIZE - -# Flags used when running the compiler in optimize mode -# CFLAGS - Additional CFLAGS added as necessary (usually 64-bit) -#-------------------------------------------------------------------- - -AC_DEFUN([TEA_CONFIG_CFLAGS], [ - dnl TEA specific: Make sure we are initialized - AC_REQUIRE([TEA_INIT]) - - # Step 0.a: Enable 64 bit support? - - AC_MSG_CHECKING([if 64bit support is requested]) - AC_ARG_ENABLE(64bit, - AC_HELP_STRING([--enable-64bit], - [enable 64bit support (default: off)]), - [do64bit=$enableval], [do64bit=no]) - AC_MSG_RESULT([$do64bit]) - - # Step 0.b: Enable Solaris 64 bit VIS support? - - AC_MSG_CHECKING([if 64bit Sparc VIS support is requested]) - AC_ARG_ENABLE(64bit-vis, - AC_HELP_STRING([--enable-64bit-vis], - [enable 64bit Sparc VIS support (default: off)]), - [do64bitVIS=$enableval], [do64bitVIS=no]) - AC_MSG_RESULT([$do64bitVIS]) - # Force 64bit on with VIS - AS_IF([test "$do64bitVIS" = "yes"], [do64bit=yes]) - - # Step 0.c: Check if visibility support is available. Do this here so - # that platform specific alternatives can be used below if this fails. - - AC_CACHE_CHECK([if compiler supports visibility "hidden"], - tcl_cv_cc_visibility_hidden, [ - hold_cflags=$CFLAGS; CFLAGS="$CFLAGS -Werror" - AC_TRY_LINK([ - extern __attribute__((__visibility__("hidden"))) void f(void); - void f(void) {}], [f();], tcl_cv_cc_visibility_hidden=yes, - tcl_cv_cc_visibility_hidden=no) - CFLAGS=$hold_cflags]) - AS_IF([test $tcl_cv_cc_visibility_hidden = yes], [ - AC_DEFINE(MODULE_SCOPE, - [extern __attribute__((__visibility__("hidden")))], - [Compiler support for module scope symbols]) - AC_DEFINE(HAVE_HIDDEN, [1], [Compiler support for module scope symbols]) - ]) - - # Step 0.d: Disable -rpath support? - - AC_MSG_CHECKING([if rpath support is requested]) - AC_ARG_ENABLE(rpath, - AC_HELP_STRING([--disable-rpath], - [disable rpath support (default: on)]), - [doRpath=$enableval], [doRpath=yes]) - AC_MSG_RESULT([$doRpath]) - - # TEA specific: Cross-compiling options for Windows/CE builds? - - AS_IF([test "${TEA_PLATFORM}" = windows], [ - AC_MSG_CHECKING([if Windows/CE build is requested]) - AC_ARG_ENABLE(wince, - AC_HELP_STRING([--enable-wince], - [enable Win/CE support (where applicable)]), - [doWince=$enableval], [doWince=no]) - AC_MSG_RESULT([$doWince]) - ]) - - # Set the variable "system" to hold the name and version number - # for the system. - - TEA_CONFIG_SYSTEM - - # Require ranlib early so we can override it in special cases below. - - AC_REQUIRE([AC_PROG_RANLIB]) - - # Set configuration options based on system name and version. - # This is similar to Tcl's unix/tcl.m4 except that we've added a - # "windows" case and removed some core-only vars. - - do64bit_ok=no - # default to '{$LIBS}' and set to "" on per-platform necessary basis - SHLIB_LD_LIBS='${LIBS}' - # When ld needs options to work in 64-bit mode, put them in - # LDFLAGS_ARCH so they eventually end up in LDFLAGS even if [load] - # is disabled by the user. [Bug 1016796] - LDFLAGS_ARCH="" - UNSHARED_LIB_SUFFIX="" - # TEA specific: use PACKAGE_VERSION instead of VERSION - TCL_TRIM_DOTS='`echo ${PACKAGE_VERSION} | tr -d .`' - ECHO_VERSION='`echo ${PACKAGE_VERSION}`' - TCL_LIB_VERSIONS_OK=ok - CFLAGS_DEBUG=-g - AS_IF([test "$GCC" = yes], [ - CFLAGS_OPTIMIZE=-O2 - CFLAGS_WARNING="-Wall" - ], [ - CFLAGS_OPTIMIZE=-O - CFLAGS_WARNING="" - ]) - AC_CHECK_TOOL(AR, ar) - STLIB_LD='${AR} cr' - LD_LIBRARY_PATH_VAR="LD_LIBRARY_PATH" - AS_IF([test "x$SHLIB_VERSION" = x],[SHLIB_VERSION="1.0"]) - case $system in - # TEA specific: - windows) - # This is a 2-stage check to make sure we have the 64-bit SDK - # We have to know where the SDK is installed. - # This magic is based on MS Platform SDK for Win2003 SP1 - hobbs - # MACHINE is IX86 for LINK, but this is used by the manifest, - # which requires x86|amd64|ia64. - MACHINE="X86" - if test "$do64bit" != "no" ; then - if test "x${MSSDK}x" = "xx" ; then - MSSDK="C:/Progra~1/Microsoft Platform SDK" - fi - MSSDK=`echo "$MSSDK" | sed -e 's!\\\!/!g'` - PATH64="" - case "$do64bit" in - amd64|x64|yes) - MACHINE="AMD64" ; # default to AMD64 64-bit build - PATH64="${MSSDK}/Bin/Win64/x86/AMD64" - ;; - ia64) - MACHINE="IA64" - PATH64="${MSSDK}/Bin/Win64" - ;; - esac - if test "$GCC" != "yes" -a ! -d "${PATH64}" ; then - AC_MSG_WARN([Could not find 64-bit $MACHINE SDK to enable 64bit mode]) - AC_MSG_WARN([Ensure latest Platform SDK is installed]) - do64bit="no" - else - AC_MSG_RESULT([ Using 64-bit $MACHINE mode]) - do64bit_ok="yes" - fi - fi - - if test "$doWince" != "no" ; then - if test "$do64bit" != "no" ; then - AC_MSG_ERROR([Windows/CE and 64-bit builds incompatible]) - fi - if test "$GCC" = "yes" ; then - AC_MSG_ERROR([Windows/CE and GCC builds incompatible]) - fi - TEA_PATH_CELIB - # Set defaults for common evc4/PPC2003 setup - # Currently Tcl requires 300+, possibly 420+ for sockets - CEVERSION=420; # could be 211 300 301 400 420 ... - TARGETCPU=ARMV4; # could be ARMV4 ARM MIPS SH3 X86 ... - ARCH=ARM; # could be ARM MIPS X86EM ... - PLATFORM="Pocket PC 2003"; # or "Pocket PC 2002" - if test "$doWince" != "yes"; then - # If !yes then the user specified something - # Reset ARCH to allow user to skip specifying it - ARCH= - eval `echo $doWince | awk -F, '{ \ - if (length([$]1)) { printf "CEVERSION=\"%s\"\n", [$]1; \ - if ([$]1 < 400) { printf "PLATFORM=\"Pocket PC 2002\"\n" } }; \ - if (length([$]2)) { printf "TARGETCPU=\"%s\"\n", toupper([$]2) }; \ - if (length([$]3)) { printf "ARCH=\"%s\"\n", toupper([$]3) }; \ - if (length([$]4)) { printf "PLATFORM=\"%s\"\n", [$]4 }; \ - }'` - if test "x${ARCH}" = "x" ; then - ARCH=$TARGETCPU; - fi - fi - OSVERSION=WCE$CEVERSION; - if test "x${WCEROOT}" = "x" ; then - WCEROOT="C:/Program Files/Microsoft eMbedded C++ 4.0" - if test ! -d "${WCEROOT}" ; then - WCEROOT="C:/Program Files/Microsoft eMbedded Tools" - fi - fi - if test "x${SDKROOT}" = "x" ; then - SDKROOT="C:/Program Files/Windows CE Tools" - if test ! -d "${SDKROOT}" ; then - SDKROOT="C:/Windows CE Tools" - fi - fi - WCEROOT=`echo "$WCEROOT" | sed -e 's!\\\!/!g'` - SDKROOT=`echo "$SDKROOT" | sed -e 's!\\\!/!g'` - if test ! -d "${SDKROOT}/${OSVERSION}/${PLATFORM}/Lib/${TARGETCPU}" \ - -o ! -d "${WCEROOT}/EVC/${OSVERSION}/bin"; then - AC_MSG_ERROR([could not find PocketPC SDK or target compiler to enable WinCE mode [$CEVERSION,$TARGETCPU,$ARCH,$PLATFORM]]) - doWince="no" - else - # We could PATH_NOSPACE these, but that's not important, - # as long as we quote them when used. - CEINCLUDE="${SDKROOT}/${OSVERSION}/${PLATFORM}/include" - if test -d "${CEINCLUDE}/${TARGETCPU}" ; then - CEINCLUDE="${CEINCLUDE}/${TARGETCPU}" - fi - CELIBPATH="${SDKROOT}/${OSVERSION}/${PLATFORM}/Lib/${TARGETCPU}" - fi - fi - - if test "$GCC" != "yes" ; then - if test "${SHARED_BUILD}" = "0" ; then - runtime=-MT - else - runtime=-MD - fi - - if test "$do64bit" != "no" ; then - # All this magic is necessary for the Win64 SDK RC1 - hobbs - CC="\"${PATH64}/cl.exe\"" - CFLAGS="${CFLAGS} -I\"${MSSDK}/Include\" -I\"${MSSDK}/Include/crt\" -I\"${MSSDK}/Include/crt/sys\"" - RC="\"${MSSDK}/bin/rc.exe\"" - lflags="-nologo -MACHINE:${MACHINE} -LIBPATH:\"${MSSDK}/Lib/${MACHINE}\"" - LINKBIN="\"${PATH64}/link.exe\"" - CFLAGS_DEBUG="-nologo -Zi -Od -W3 ${runtime}d" - CFLAGS_OPTIMIZE="-nologo -O2 -W2 ${runtime}" - # Avoid 'unresolved external symbol __security_cookie' - # errors, c.f. http://support.microsoft.com/?id=894573 - TEA_ADD_LIBS([bufferoverflowU.lib]) - elif test "$doWince" != "no" ; then - CEBINROOT="${WCEROOT}/EVC/${OSVERSION}/bin" - if test "${TARGETCPU}" = "X86"; then - CC="\"${CEBINROOT}/cl.exe\"" - else - CC="\"${CEBINROOT}/cl${ARCH}.exe\"" - fi - CFLAGS="$CFLAGS -I\"${CELIB_DIR}/inc\" -I\"${CEINCLUDE}\"" - RC="\"${WCEROOT}/Common/EVC/bin/rc.exe\"" - arch=`echo ${ARCH} | awk '{print tolower([$]0)}'` - defs="${ARCH} _${ARCH}_ ${arch} PALM_SIZE _MT _WINDOWS" - if test "${SHARED_BUILD}" = "1" ; then - # Static CE builds require static celib as well - defs="${defs} _DLL" - fi - for i in $defs ; do - AC_DEFINE_UNQUOTED($i, 1, [WinCE def ]$i) - done - AC_DEFINE_UNQUOTED(_WIN32_WCE, $CEVERSION, [_WIN32_WCE version]) - AC_DEFINE_UNQUOTED(UNDER_CE, $CEVERSION, [UNDER_CE version]) - CFLAGS_DEBUG="-nologo -Zi -Od" - CFLAGS_OPTIMIZE="-nologo -Ox" - lversion=`echo ${CEVERSION} | sed -e 's/\(.\)\(..\)/\1\.\2/'` - lflags="-MACHINE:${ARCH} -LIBPATH:\"${CELIBPATH}\" -subsystem:windowsce,${lversion} -nologo" - LINKBIN="\"${CEBINROOT}/link.exe\"" - AC_SUBST(CELIB_DIR) - else - RC="rc" - lflags="-nologo" - LINKBIN="link" - CFLAGS_DEBUG="-nologo -Z7 -Od -W3 -WX ${runtime}d" - CFLAGS_OPTIMIZE="-nologo -O2 -W2 ${runtime}" - fi - fi - - if test "$GCC" = "yes"; then - # mingw gcc mode - AC_CHECK_TOOL(RC, windres) - CFLAGS_DEBUG="-g" - CFLAGS_OPTIMIZE="-O2 -fomit-frame-pointer" - SHLIB_LD='${CC} -shared' - UNSHARED_LIB_SUFFIX='${TCL_TRIM_DOTS}.a' - LDFLAGS_CONSOLE="-wl,--subsystem,console ${lflags}" - LDFLAGS_WINDOW="-wl,--subsystem,windows ${lflags}" - - AC_CACHE_CHECK(for cross-compile version of gcc, - ac_cv_cross, - AC_TRY_COMPILE([ - #ifdef _WIN32 - #error cross-compiler - #endif - ], [], - ac_cv_cross=yes, - ac_cv_cross=no) - ) - if test "$ac_cv_cross" = "yes"; then - case "$do64bit" in - amd64|x64|yes) - CC="x86_64-w64-mingw32-gcc" - LD="x86_64-w64-mingw32-ld" - AR="x86_64-w64-mingw32-ar" - RANLIB="x86_64-w64-mingw32-ranlib" - RC="x86_64-w64-mingw32-windres" - ;; - *) - CC="i686-w64-mingw32-gcc" - LD="i686-w64-mingw32-ld" - AR="i686-w64-mingw32-ar" - RANLIB="i686-w64-mingw32-ranlib" - RC="i686-w64-mingw32-windres" - ;; - esac - fi - - else - SHLIB_LD="${LINKBIN} -dll ${lflags}" - # link -lib only works when -lib is the first arg - STLIB_LD="${LINKBIN} -lib ${lflags}" - UNSHARED_LIB_SUFFIX='${TCL_TRIM_DOTS}.lib' - PATHTYPE=-w - # For information on what debugtype is most useful, see: - # http://msdn.microsoft.com/library/en-us/dnvc60/html/gendepdebug.asp - # and also - # http://msdn2.microsoft.com/en-us/library/y0zzbyt4%28VS.80%29.aspx - # This essentially turns it all on. - LDFLAGS_DEBUG="-debug -debugtype:cv" - LDFLAGS_OPTIMIZE="-release" - if test "$doWince" != "no" ; then - LDFLAGS_CONSOLE="-link ${lflags}" - LDFLAGS_WINDOW=${LDFLAGS_CONSOLE} - else - LDFLAGS_CONSOLE="-link -subsystem:console ${lflags}" - LDFLAGS_WINDOW="-link -subsystem:windows ${lflags}" - fi - fi - - SHLIB_SUFFIX=".dll" - SHARED_LIB_SUFFIX='${TCL_TRIM_DOTS}.dll' - - TCL_LIB_VERSIONS_OK=nodots - ;; - AIX-*) - AS_IF([test "${TCL_THREADS}" = "1" -a "$GCC" != "yes"], [ - # AIX requires the _r compiler when gcc isn't being used - case "${CC}" in - *_r|*_r\ *) - # ok ... - ;; - *) - # Make sure only first arg gets _r - CC=`echo "$CC" | sed -e 's/^\([[^ ]]*\)/\1_r/'` - ;; - esac - AC_MSG_RESULT([Using $CC for compiling with threads]) - ]) - LIBS="$LIBS -lc" - SHLIB_CFLAGS="" - SHLIB_SUFFIX=".so" - - LD_LIBRARY_PATH_VAR="LIBPATH" - - # Check to enable 64-bit flags for compiler/linker - AS_IF([test "$do64bit" = yes], [ - AS_IF([test "$GCC" = yes], [ - AC_MSG_WARN([64bit mode not supported with GCC on $system]) - ], [ - do64bit_ok=yes - CFLAGS="$CFLAGS -q64" - LDFLAGS_ARCH="-q64" - RANLIB="${RANLIB} -X64" - AR="${AR} -X64" - SHLIB_LD_FLAGS="-b64" - ]) - ]) - - AS_IF([test "`uname -m`" = ia64], [ - # AIX-5 uses ELF style dynamic libraries on IA-64, but not PPC - SHLIB_LD="/usr/ccs/bin/ld -G -z text" - AS_IF([test "$GCC" = yes], [ - CC_SEARCH_FLAGS='-Wl,-R,${LIB_RUNTIME_DIR}' - ], [ - CC_SEARCH_FLAGS='-R${LIB_RUNTIME_DIR}' - ]) - LD_SEARCH_FLAGS='-R ${LIB_RUNTIME_DIR}' - ], [ - AS_IF([test "$GCC" = yes], [ - SHLIB_LD='${CC} -shared -Wl,-bexpall' - ], [ - SHLIB_LD="/bin/ld -bhalt:4 -bM:SRE -bexpall -H512 -T512 -bnoentry" - LDFLAGS="$LDFLAGS -brtl" - ]) - SHLIB_LD="${SHLIB_LD} ${SHLIB_LD_FLAGS}" - CC_SEARCH_FLAGS='-L${LIB_RUNTIME_DIR}' - LD_SEARCH_FLAGS=${CC_SEARCH_FLAGS} - ]) - ;; - BeOS*) - SHLIB_CFLAGS="-fPIC" - SHLIB_LD='${CC} -nostart' - SHLIB_SUFFIX=".so" - - #----------------------------------------------------------- - # Check for inet_ntoa in -lbind, for BeOS (which also needs - # -lsocket, even if the network functions are in -lnet which - # is always linked to, for compatibility. - #----------------------------------------------------------- - AC_CHECK_LIB(bind, inet_ntoa, [LIBS="$LIBS -lbind -lsocket"]) - ;; - BSD/OS-4.*) - SHLIB_CFLAGS="-export-dynamic -fPIC" - SHLIB_LD='${CC} -shared' - SHLIB_SUFFIX=".so" - LDFLAGS="$LDFLAGS -export-dynamic" - CC_SEARCH_FLAGS="" - LD_SEARCH_FLAGS="" - ;; - CYGWIN_*) - SHLIB_CFLAGS="" - SHLIB_LD='${CC} -shared' - SHLIB_SUFFIX=".dll" - EXEEXT=".exe" - do64bit_ok=yes - CC_SEARCH_FLAGS="" - LD_SEARCH_FLAGS="" - ;; - Haiku*) - LDFLAGS="$LDFLAGS -Wl,--export-dynamic" - SHLIB_CFLAGS="-fPIC" - SHLIB_SUFFIX=".so" - SHLIB_LD='${CC} -shared ${CFLAGS} ${LDFLAGS}' - AC_CHECK_LIB(network, inet_ntoa, [LIBS="$LIBS -lnetwork"]) - ;; - HP-UX-*.11.*) - # Use updated header definitions where possible - AC_DEFINE(_XOPEN_SOURCE_EXTENDED, 1, [Do we want to use the XOPEN network library?]) - # TEA specific: Needed by Tcl, but not most extensions - #AC_DEFINE(_XOPEN_SOURCE, 1, [Do we want to use the XOPEN network library?]) - #LIBS="$LIBS -lxnet" # Use the XOPEN network library - - AS_IF([test "`uname -m`" = ia64], [ - SHLIB_SUFFIX=".so" - # Use newer C++ library for C++ extensions - #if test "$GCC" != "yes" ; then - # CPPFLAGS="-AA" - #fi - ], [ - SHLIB_SUFFIX=".sl" - ]) - AC_CHECK_LIB(dld, shl_load, tcl_ok=yes, tcl_ok=no) - AS_IF([test "$tcl_ok" = yes], [ - LDFLAGS="$LDFLAGS -Wl,-E" - CC_SEARCH_FLAGS='-Wl,+s,+b,${LIB_RUNTIME_DIR}:.' - LD_SEARCH_FLAGS='+s +b ${LIB_RUNTIME_DIR}:.' - LD_LIBRARY_PATH_VAR="SHLIB_PATH" - ]) - AS_IF([test "$GCC" = yes], [ - SHLIB_LD='${CC} -shared' - LD_SEARCH_FLAGS=${CC_SEARCH_FLAGS} - ], [ - CFLAGS="$CFLAGS -z" - # Users may want PA-RISC 1.1/2.0 portable code - needs HP cc - #CFLAGS="$CFLAGS +DAportable" - SHLIB_CFLAGS="+z" - SHLIB_LD="ld -b" - ]) - - # Check to enable 64-bit flags for compiler/linker - AS_IF([test "$do64bit" = "yes"], [ - AS_IF([test "$GCC" = yes], [ - case `${CC} -dumpmachine` in - hppa64*) - # 64-bit gcc in use. Fix flags for GNU ld. - do64bit_ok=yes - SHLIB_LD='${CC} -shared' - AS_IF([test $doRpath = yes], [ - CC_SEARCH_FLAGS='-Wl,-rpath,${LIB_RUNTIME_DIR}']) - LD_SEARCH_FLAGS=${CC_SEARCH_FLAGS} - ;; - *) - AC_MSG_WARN([64bit mode not supported with GCC on $system]) - ;; - esac - ], [ - do64bit_ok=yes - CFLAGS="$CFLAGS +DD64" - LDFLAGS_ARCH="+DD64" - ]) - ]) ;; - IRIX-6.*) - SHLIB_CFLAGS="" - SHLIB_LD="ld -n32 -shared -rdata_shared" - SHLIB_SUFFIX=".so" - AS_IF([test $doRpath = yes], [ - CC_SEARCH_FLAGS='-Wl,-rpath,${LIB_RUNTIME_DIR}' - LD_SEARCH_FLAGS='-rpath ${LIB_RUNTIME_DIR}']) - AS_IF([test "$GCC" = yes], [ - CFLAGS="$CFLAGS -mabi=n32" - LDFLAGS="$LDFLAGS -mabi=n32" - ], [ - case $system in - IRIX-6.3) - # Use to build 6.2 compatible binaries on 6.3. - CFLAGS="$CFLAGS -n32 -D_OLD_TERMIOS" - ;; - *) - CFLAGS="$CFLAGS -n32" - ;; - esac - LDFLAGS="$LDFLAGS -n32" - ]) - ;; - IRIX64-6.*) - SHLIB_CFLAGS="" - SHLIB_LD="ld -n32 -shared -rdata_shared" - SHLIB_SUFFIX=".so" - AS_IF([test $doRpath = yes], [ - CC_SEARCH_FLAGS='-Wl,-rpath,${LIB_RUNTIME_DIR}' - LD_SEARCH_FLAGS='-rpath ${LIB_RUNTIME_DIR}']) - - # Check to enable 64-bit flags for compiler/linker - - AS_IF([test "$do64bit" = yes], [ - AS_IF([test "$GCC" = yes], [ - AC_MSG_WARN([64bit mode not supported by gcc]) - ], [ - do64bit_ok=yes - SHLIB_LD="ld -64 -shared -rdata_shared" - CFLAGS="$CFLAGS -64" - LDFLAGS_ARCH="-64" - ]) - ]) - ;; - Linux*|GNU*|NetBSD-Debian) - SHLIB_CFLAGS="-fPIC" - SHLIB_SUFFIX=".so" - - # TEA specific: - CFLAGS_OPTIMIZE="-O2 -fomit-frame-pointer" - - # TEA specific: use LDFLAGS_DEFAULT instead of LDFLAGS - SHLIB_LD='${CC} -shared ${CFLAGS} ${LDFLAGS_DEFAULT}' - LDFLAGS="$LDFLAGS -Wl,--export-dynamic" - AS_IF([test $doRpath = yes], [ - CC_SEARCH_FLAGS='-Wl,-rpath,${LIB_RUNTIME_DIR}']) - LD_SEARCH_FLAGS=${CC_SEARCH_FLAGS} - AS_IF([test "`uname -m`" = "alpha"], [CFLAGS="$CFLAGS -mieee"]) - AS_IF([test $do64bit = yes], [ - AC_CACHE_CHECK([if compiler accepts -m64 flag], tcl_cv_cc_m64, [ - hold_cflags=$CFLAGS - CFLAGS="$CFLAGS -m64" - AC_TRY_LINK(,, tcl_cv_cc_m64=yes, tcl_cv_cc_m64=no) - CFLAGS=$hold_cflags]) - AS_IF([test $tcl_cv_cc_m64 = yes], [ - CFLAGS="$CFLAGS -m64" - do64bit_ok=yes - ]) - ]) - - # The combo of gcc + glibc has a bug related to inlining of - # functions like strtod(). The -fno-builtin flag should address - # this problem but it does not work. The -fno-inline flag is kind - # of overkill but it works. Disable inlining only when one of the - # files in compat/*.c is being linked in. - - AS_IF([test x"${USE_COMPAT}" != x],[CFLAGS="$CFLAGS -fno-inline"]) - ;; - Lynx*) - SHLIB_CFLAGS="-fPIC" - SHLIB_SUFFIX=".so" - CFLAGS_OPTIMIZE=-02 - SHLIB_LD='${CC} -shared' - LD_FLAGS="-Wl,--export-dynamic" - AS_IF([test $doRpath = yes], [ - CC_SEARCH_FLAGS='-Wl,-rpath,${LIB_RUNTIME_DIR}' - LD_SEARCH_FLAGS='-Wl,-rpath,${LIB_RUNTIME_DIR}']) - ;; - OpenBSD-*) - arch=`arch -s` - case "$arch" in - vax) - SHLIB_SUFFIX="" - SHARED_LIB_SUFFIX="" - LDFLAGS="" - ;; - *) - SHLIB_CFLAGS="-fPIC" - SHLIB_LD='${CC} -shared ${SHLIB_CFLAGS}' - SHLIB_SUFFIX=".so" - AS_IF([test $doRpath = yes], [ - CC_SEARCH_FLAGS='-Wl,-rpath,${LIB_RUNTIME_DIR}']) - LD_SEARCH_FLAGS=${CC_SEARCH_FLAGS} - SHARED_LIB_SUFFIX='${TCL_TRIM_DOTS}.so.${SHLIB_VERSION}' - LDFLAGS="-Wl,-export-dynamic" - ;; - esac - case "$arch" in - vax) - CFLAGS_OPTIMIZE="-O1" - ;; - *) - CFLAGS_OPTIMIZE="-O2" - ;; - esac - AS_IF([test "${TCL_THREADS}" = "1"], [ - # On OpenBSD: Compile with -pthread - # Don't link with -lpthread - LIBS=`echo $LIBS | sed s/-lpthread//` - CFLAGS="$CFLAGS -pthread" - ]) - # OpenBSD doesn't do version numbers with dots. - UNSHARED_LIB_SUFFIX='${TCL_TRIM_DOTS}.a' - TCL_LIB_VERSIONS_OK=nodots - ;; - NetBSD-*) - # NetBSD has ELF and can use 'cc -shared' to build shared libs - SHLIB_CFLAGS="-fPIC" - SHLIB_LD='${CC} -shared ${SHLIB_CFLAGS}' - SHLIB_SUFFIX=".so" - LDFLAGS="$LDFLAGS -export-dynamic" - AS_IF([test $doRpath = yes], [ - CC_SEARCH_FLAGS='-Wl,-rpath,${LIB_RUNTIME_DIR}']) - LD_SEARCH_FLAGS=${CC_SEARCH_FLAGS} - AS_IF([test "${TCL_THREADS}" = "1"], [ - # The -pthread needs to go in the CFLAGS, not LIBS - LIBS=`echo $LIBS | sed s/-pthread//` - CFLAGS="$CFLAGS -pthread" - LDFLAGS="$LDFLAGS -pthread" - ]) - ;; - FreeBSD-*) - # This configuration from FreeBSD Ports. - SHLIB_CFLAGS="-fPIC" - SHLIB_LD="${CC} -shared" - TCL_SHLIB_LD_EXTRAS="-Wl,-soname=\$[@]" - TK_SHLIB_LD_EXTRAS="-Wl,-soname,\$[@]" - SHLIB_SUFFIX=".so" - LDFLAGS="" - AS_IF([test $doRpath = yes], [ - CC_SEARCH_FLAGS='-Wl,-rpath,${LIB_RUNTIME_DIR}' - LD_SEARCH_FLAGS='-Wl,-rpath,${LIB_RUNTIME_DIR}']) - AS_IF([test "${TCL_THREADS}" = "1"], [ - # The -pthread needs to go in the LDFLAGS, not LIBS - LIBS=`echo $LIBS | sed s/-pthread//` - CFLAGS="$CFLAGS $PTHREAD_CFLAGS" - LDFLAGS="$LDFLAGS $PTHREAD_LIBS"]) - case $system in - FreeBSD-3.*) - # Version numbers are dot-stripped by system policy. - TCL_TRIM_DOTS=`echo ${VERSION} | tr -d .` - UNSHARED_LIB_SUFFIX='${TCL_TRIM_DOTS}.a' - SHARED_LIB_SUFFIX='${TCL_TRIM_DOTS}.so' - TCL_LIB_VERSIONS_OK=nodots - ;; - esac - ;; - Darwin-*) - CFLAGS_OPTIMIZE="-Os" - SHLIB_CFLAGS="-fno-common" - # To avoid discrepancies between what headers configure sees during - # preprocessing tests and compiling tests, move any -isysroot and - # -mmacosx-version-min flags from CFLAGS to CPPFLAGS: - CPPFLAGS="${CPPFLAGS} `echo " ${CFLAGS}" | \ - awk 'BEGIN {FS=" +-";ORS=" "}; {for (i=2;i<=NF;i++) \ - if ([$]i~/^(isysroot|mmacosx-version-min)/) print "-"[$]i}'`" - CFLAGS="`echo " ${CFLAGS}" | \ - awk 'BEGIN {FS=" +-";ORS=" "}; {for (i=2;i<=NF;i++) \ - if (!([$]i~/^(isysroot|mmacosx-version-min)/)) print "-"[$]i}'`" - AS_IF([test $do64bit = yes], [ - case `arch` in - ppc) - AC_CACHE_CHECK([if compiler accepts -arch ppc64 flag], - tcl_cv_cc_arch_ppc64, [ - hold_cflags=$CFLAGS - CFLAGS="$CFLAGS -arch ppc64 -mpowerpc64 -mcpu=G5" - AC_TRY_LINK(,, tcl_cv_cc_arch_ppc64=yes, - tcl_cv_cc_arch_ppc64=no) - CFLAGS=$hold_cflags]) - AS_IF([test $tcl_cv_cc_arch_ppc64 = yes], [ - CFLAGS="$CFLAGS -arch ppc64 -mpowerpc64 -mcpu=G5" - do64bit_ok=yes - ]);; - i386) - AC_CACHE_CHECK([if compiler accepts -arch x86_64 flag], - tcl_cv_cc_arch_x86_64, [ - hold_cflags=$CFLAGS - CFLAGS="$CFLAGS -arch x86_64" - AC_TRY_LINK(,, tcl_cv_cc_arch_x86_64=yes, - tcl_cv_cc_arch_x86_64=no) - CFLAGS=$hold_cflags]) - AS_IF([test $tcl_cv_cc_arch_x86_64 = yes], [ - CFLAGS="$CFLAGS -arch x86_64" - do64bit_ok=yes - ]);; - *) - AC_MSG_WARN([Don't know how enable 64-bit on architecture `arch`]);; - esac - ], [ - # Check for combined 32-bit and 64-bit fat build - AS_IF([echo "$CFLAGS " |grep -E -q -- '-arch (ppc64|x86_64) ' \ - && echo "$CFLAGS " |grep -E -q -- '-arch (ppc|i386) '], [ - fat_32_64=yes]) - ]) - # TEA specific: use LDFLAGS_DEFAULT instead of LDFLAGS - SHLIB_LD='${CC} -dynamiclib ${CFLAGS} ${LDFLAGS_DEFAULT}' - AC_CACHE_CHECK([if ld accepts -single_module flag], tcl_cv_ld_single_module, [ - hold_ldflags=$LDFLAGS - LDFLAGS="$LDFLAGS -dynamiclib -Wl,-single_module" - AC_TRY_LINK(, [int i;], tcl_cv_ld_single_module=yes, tcl_cv_ld_single_module=no) - LDFLAGS=$hold_ldflags]) - AS_IF([test $tcl_cv_ld_single_module = yes], [ - SHLIB_LD="${SHLIB_LD} -Wl,-single_module" - ]) - # TEA specific: link shlib with current and compatibility version flags - vers=`echo ${PACKAGE_VERSION} | sed -e 's/^\([[0-9]]\{1,5\}\)\(\(\.[[0-9]]\{1,3\}\)\{0,2\}\).*$/\1\2/p' -e d` - SHLIB_LD="${SHLIB_LD} -current_version ${vers:-0} -compatibility_version ${vers:-0}" - SHLIB_SUFFIX=".dylib" - # Don't use -prebind when building for Mac OS X 10.4 or later only: - AS_IF([test "`echo "${MACOSX_DEPLOYMENT_TARGET}" | awk -F '10\\.' '{print int([$]2)}'`" -lt 4 -a \ - "`echo "${CPPFLAGS}" | awk -F '-mmacosx-version-min=10\\.' '{print int([$]2)}'`" -lt 4], [ - LDFLAGS="$LDFLAGS -prebind"]) - LDFLAGS="$LDFLAGS -headerpad_max_install_names" - AC_CACHE_CHECK([if ld accepts -search_paths_first flag], - tcl_cv_ld_search_paths_first, [ - hold_ldflags=$LDFLAGS - LDFLAGS="$LDFLAGS -Wl,-search_paths_first" - AC_TRY_LINK(, [int i;], tcl_cv_ld_search_paths_first=yes, - tcl_cv_ld_search_paths_first=no) - LDFLAGS=$hold_ldflags]) - AS_IF([test $tcl_cv_ld_search_paths_first = yes], [ - LDFLAGS="$LDFLAGS -Wl,-search_paths_first" - ]) - AS_IF([test "$tcl_cv_cc_visibility_hidden" != yes], [ - AC_DEFINE(MODULE_SCOPE, [__private_extern__], - [Compiler support for module scope symbols]) - tcl_cv_cc_visibility_hidden=yes - ]) - CC_SEARCH_FLAGS="" - LD_SEARCH_FLAGS="" - LD_LIBRARY_PATH_VAR="DYLD_LIBRARY_PATH" - # TEA specific: for combined 32 & 64 bit fat builds of Tk - # extensions, verify that 64-bit build is possible. - AS_IF([test "$fat_32_64" = yes && test -n "${TK_BIN_DIR}"], [ - AS_IF([test "${TEA_WINDOWINGSYSTEM}" = x11], [ - AC_CACHE_CHECK([for 64-bit X11], tcl_cv_lib_x11_64, [ - for v in CFLAGS CPPFLAGS LDFLAGS; do - eval 'hold_'$v'="$'$v'";'$v'="`echo "$'$v' "|sed -e "s/-arch ppc / /g" -e "s/-arch i386 / /g"`"' - done - CPPFLAGS="$CPPFLAGS -I/usr/X11R6/include" - LDFLAGS="$LDFLAGS -L/usr/X11R6/lib -lX11" - AC_TRY_LINK([#include ], [XrmInitialize();], - tcl_cv_lib_x11_64=yes, tcl_cv_lib_x11_64=no) - for v in CFLAGS CPPFLAGS LDFLAGS; do - eval $v'="$hold_'$v'"' - done]) - ]) - AS_IF([test "${TEA_WINDOWINGSYSTEM}" = aqua], [ - AC_CACHE_CHECK([for 64-bit Tk], tcl_cv_lib_tk_64, [ - for v in CFLAGS CPPFLAGS LDFLAGS; do - eval 'hold_'$v'="$'$v'";'$v'="`echo "$'$v' "|sed -e "s/-arch ppc / /g" -e "s/-arch i386 / /g"`"' - done - CPPFLAGS="$CPPFLAGS -DUSE_TCL_STUBS=1 -DUSE_TK_STUBS=1 ${TCL_INCLUDES} ${TK_INCLUDES}" - LDFLAGS="$LDFLAGS ${TCL_STUB_LIB_SPEC} ${TK_STUB_LIB_SPEC}" - AC_TRY_LINK([#include ], [Tk_InitStubs(NULL, "", 0);], - tcl_cv_lib_tk_64=yes, tcl_cv_lib_tk_64=no) - for v in CFLAGS CPPFLAGS LDFLAGS; do - eval $v'="$hold_'$v'"' - done]) - ]) - # remove 64-bit arch flags from CFLAGS et al. if configuration - # does not support 64-bit. - AS_IF([test "$tcl_cv_lib_tk_64" = no -o "$tcl_cv_lib_x11_64" = no], [ - AC_MSG_NOTICE([Removing 64-bit architectures from compiler & linker flags]) - for v in CFLAGS CPPFLAGS LDFLAGS; do - eval $v'="`echo "$'$v' "|sed -e "s/-arch ppc64 / /g" -e "s/-arch x86_64 / /g"`"' - done]) - ]) - ;; - OS/390-*) - CFLAGS_OPTIMIZE="" # Optimizer is buggy - AC_DEFINE(_OE_SOCKETS, 1, # needed in sys/socket.h - [Should OS/390 do the right thing with sockets?]) - ;; - OSF1-V*) - # Digital OSF/1 - SHLIB_CFLAGS="" - AS_IF([test "$SHARED_BUILD" = 1], [ - SHLIB_LD='ld -shared -expect_unresolved "*"' - ], [ - SHLIB_LD='ld -non_shared -expect_unresolved "*"' - ]) - SHLIB_SUFFIX=".so" - AS_IF([test $doRpath = yes], [ - CC_SEARCH_FLAGS='-Wl,-rpath,${LIB_RUNTIME_DIR}' - LD_SEARCH_FLAGS='-rpath ${LIB_RUNTIME_DIR}']) - AS_IF([test "$GCC" = yes], [CFLAGS="$CFLAGS -mieee"], [ - CFLAGS="$CFLAGS -DHAVE_TZSET -std1 -ieee"]) - # see pthread_intro(3) for pthread support on osf1, k.furukawa - AS_IF([test "${TCL_THREADS}" = 1], [ - CFLAGS="$CFLAGS -DHAVE_PTHREAD_ATTR_SETSTACKSIZE" - CFLAGS="$CFLAGS -DTCL_THREAD_STACK_MIN=PTHREAD_STACK_MIN*64" - LIBS=`echo $LIBS | sed s/-lpthreads//` - AS_IF([test "$GCC" = yes], [ - LIBS="$LIBS -lpthread -lmach -lexc" - ], [ - CFLAGS="$CFLAGS -pthread" - LDFLAGS="$LDFLAGS -pthread" - ]) - ]) - ;; - QNX-6*) - # QNX RTP - # This may work for all QNX, but it was only reported for v6. - SHLIB_CFLAGS="-fPIC" - SHLIB_LD="ld -Bshareable -x" - SHLIB_LD_LIBS="" - SHLIB_SUFFIX=".so" - CC_SEARCH_FLAGS="" - LD_SEARCH_FLAGS="" - ;; - SCO_SV-3.2*) - AS_IF([test "$GCC" = yes], [ - SHLIB_CFLAGS="-fPIC -melf" - LDFLAGS="$LDFLAGS -melf -Wl,-Bexport" - ], [ - SHLIB_CFLAGS="-Kpic -belf" - LDFLAGS="$LDFLAGS -belf -Wl,-Bexport" - ]) - SHLIB_LD="ld -G" - SHLIB_LD_LIBS="" - SHLIB_SUFFIX=".so" - CC_SEARCH_FLAGS="" - LD_SEARCH_FLAGS="" - ;; - SunOS-5.[[0-6]]) - # Careful to not let 5.10+ fall into this case - - # Note: If _REENTRANT isn't defined, then Solaris - # won't define thread-safe library routines. - - AC_DEFINE(_REENTRANT, 1, [Do we want the reentrant OS API?]) - AC_DEFINE(_POSIX_PTHREAD_SEMANTICS, 1, - [Do we really want to follow the standard? Yes we do!]) - - SHLIB_CFLAGS="-KPIC" - SHLIB_SUFFIX=".so" - AS_IF([test "$GCC" = yes], [ - SHLIB_LD='${CC} -shared' - CC_SEARCH_FLAGS='-Wl,-R,${LIB_RUNTIME_DIR}' - LD_SEARCH_FLAGS=${CC_SEARCH_FLAGS} - ], [ - SHLIB_LD="/usr/ccs/bin/ld -G -z text" - CC_SEARCH_FLAGS='-R ${LIB_RUNTIME_DIR}' - LD_SEARCH_FLAGS=${CC_SEARCH_FLAGS} - ]) - ;; - SunOS-5*) - # Note: If _REENTRANT isn't defined, then Solaris - # won't define thread-safe library routines. - - AC_DEFINE(_REENTRANT, 1, [Do we want the reentrant OS API?]) - AC_DEFINE(_POSIX_PTHREAD_SEMANTICS, 1, - [Do we really want to follow the standard? Yes we do!]) - - SHLIB_CFLAGS="-KPIC" - - # Check to enable 64-bit flags for compiler/linker - AS_IF([test "$do64bit" = yes], [ - arch=`isainfo` - AS_IF([test "$arch" = "sparcv9 sparc"], [ - AS_IF([test "$GCC" = yes], [ - AS_IF([test "`${CC} -dumpversion | awk -F. '{print [$]1}'`" -lt 3], [ - AC_MSG_WARN([64bit mode not supported with GCC < 3.2 on $system]) - ], [ - do64bit_ok=yes - CFLAGS="$CFLAGS -m64 -mcpu=v9" - LDFLAGS="$LDFLAGS -m64 -mcpu=v9" - SHLIB_CFLAGS="-fPIC" - ]) - ], [ - do64bit_ok=yes - AS_IF([test "$do64bitVIS" = yes], [ - CFLAGS="$CFLAGS -xarch=v9a" - LDFLAGS_ARCH="-xarch=v9a" - ], [ - CFLAGS="$CFLAGS -xarch=v9" - LDFLAGS_ARCH="-xarch=v9" - ]) - # Solaris 64 uses this as well - #LD_LIBRARY_PATH_VAR="LD_LIBRARY_PATH_64" - ]) - ], [AS_IF([test "$arch" = "amd64 i386"], [ - AS_IF([test "$GCC" = yes], [ - case $system in - SunOS-5.1[[1-9]]*|SunOS-5.[[2-9]][[0-9]]*) - do64bit_ok=yes - CFLAGS="$CFLAGS -m64" - LDFLAGS="$LDFLAGS -m64";; - *) - AC_MSG_WARN([64bit mode not supported with GCC on $system]);; - esac - ], [ - do64bit_ok=yes - case $system in - SunOS-5.1[[1-9]]*|SunOS-5.[[2-9]][[0-9]]*) - CFLAGS="$CFLAGS -m64" - LDFLAGS="$LDFLAGS -m64";; - *) - CFLAGS="$CFLAGS -xarch=amd64" - LDFLAGS="$LDFLAGS -xarch=amd64";; - esac - ]) - ], [AC_MSG_WARN([64bit mode not supported for $arch])])]) - ]) - - SHLIB_SUFFIX=".so" - AS_IF([test "$GCC" = yes], [ - SHLIB_LD='${CC} -shared' - CC_SEARCH_FLAGS='-Wl,-R,${LIB_RUNTIME_DIR}' - LD_SEARCH_FLAGS=${CC_SEARCH_FLAGS} - AS_IF([test "$do64bit_ok" = yes], [ - AS_IF([test "$arch" = "sparcv9 sparc"], [ - # We need to specify -static-libgcc or we need to - # add the path to the sparv9 libgcc. - # JH: static-libgcc is necessary for core Tcl, but may - # not be necessary for extensions. - SHLIB_LD="$SHLIB_LD -m64 -mcpu=v9 -static-libgcc" - # for finding sparcv9 libgcc, get the regular libgcc - # path, remove so name and append 'sparcv9' - #v9gcclibdir="`gcc -print-file-name=libgcc_s.so` | ..." - #CC_SEARCH_FLAGS="${CC_SEARCH_FLAGS},-R,$v9gcclibdir" - ], [AS_IF([test "$arch" = "amd64 i386"], [ - # JH: static-libgcc is necessary for core Tcl, but may - # not be necessary for extensions. - SHLIB_LD="$SHLIB_LD -m64 -static-libgcc" - ])]) - ]) - ], [ - case $system in - SunOS-5.[[1-9]][[0-9]]*) - # TEA specific: use LDFLAGS_DEFAULT instead of LDFLAGS - SHLIB_LD='${CC} -G -z text ${LDFLAGS_DEFAULT}';; - *) - SHLIB_LD='/usr/ccs/bin/ld -G -z text';; - esac - CC_SEARCH_FLAGS='-Wl,-R,${LIB_RUNTIME_DIR}' - LD_SEARCH_FLAGS='-R ${LIB_RUNTIME_DIR}' - ]) - ;; - UNIX_SV* | UnixWare-5*) - SHLIB_CFLAGS="-KPIC" - SHLIB_LD='${CC} -G' - SHLIB_LD_LIBS="" - SHLIB_SUFFIX=".so" - # Some UNIX_SV* systems (unixware 1.1.2 for example) have linkers - # that don't grok the -Bexport option. Test that it does. - AC_CACHE_CHECK([for ld accepts -Bexport flag], tcl_cv_ld_Bexport, [ - hold_ldflags=$LDFLAGS - LDFLAGS="$LDFLAGS -Wl,-Bexport" - AC_TRY_LINK(, [int i;], tcl_cv_ld_Bexport=yes, tcl_cv_ld_Bexport=no) - LDFLAGS=$hold_ldflags]) - AS_IF([test $tcl_cv_ld_Bexport = yes], [ - LDFLAGS="$LDFLAGS -Wl,-Bexport" - ]) - CC_SEARCH_FLAGS="" - LD_SEARCH_FLAGS="" - ;; - esac - - AS_IF([test "$do64bit" = yes -a "$do64bit_ok" = no], [ - AC_MSG_WARN([64bit support being disabled -- don't know magic for this platform]) - ]) - -dnl # Add any CPPFLAGS set in the environment to our CFLAGS, but delay doing so -dnl # until the end of configure, as configure's compile and link tests use -dnl # both CPPFLAGS and CFLAGS (unlike our compile and link) but configure's -dnl # preprocessing tests use only CPPFLAGS. - AC_CONFIG_COMMANDS_PRE([CFLAGS="${CFLAGS} ${CPPFLAGS}"; CPPFLAGS=""]) - - # Add in the arch flags late to ensure it wasn't removed. - # Not necessary in TEA, but this is aligned with core - LDFLAGS="$LDFLAGS $LDFLAGS_ARCH" - - # If we're running gcc, then change the C flags for compiling shared - # libraries to the right flags for gcc, instead of those for the - # standard manufacturer compiler. - - AS_IF([test "$GCC" = yes], [ - case $system in - AIX-*) ;; - BSD/OS*) ;; - CYGWIN_*|MINGW32_*) ;; - IRIX*) ;; - NetBSD-*|FreeBSD-*|OpenBSD-*) ;; - Darwin-*) ;; - SCO_SV-3.2*) ;; - windows) ;; - *) SHLIB_CFLAGS="-fPIC" ;; - esac]) - - AS_IF([test "$tcl_cv_cc_visibility_hidden" != yes], [ - AC_DEFINE(MODULE_SCOPE, [extern], - [No Compiler support for module scope symbols]) - ]) - - AS_IF([test "$SHARED_LIB_SUFFIX" = ""], [ - # TEA specific: use PACKAGE_VERSION instead of VERSION - SHARED_LIB_SUFFIX='${PACKAGE_VERSION}${SHLIB_SUFFIX}']) - AS_IF([test "$UNSHARED_LIB_SUFFIX" = ""], [ - # TEA specific: use PACKAGE_VERSION instead of VERSION - UNSHARED_LIB_SUFFIX='${PACKAGE_VERSION}.a']) - - if test "${GCC}" = "yes" -a ${SHLIB_SUFFIX} = ".dll"; then - AC_CACHE_CHECK(for SEH support in compiler, - tcl_cv_seh, - AC_TRY_RUN([ -#define WIN32_LEAN_AND_MEAN -#include -#undef WIN32_LEAN_AND_MEAN - - int main(int argc, char** argv) { - int a, b = 0; - __try { - a = 666 / b; - } - __except (EXCEPTION_EXECUTE_HANDLER) { - return 0; - } - return 1; - } - ], - tcl_cv_seh=yes, - tcl_cv_seh=no, - tcl_cv_seh=no) - ) - if test "$tcl_cv_seh" = "no" ; then - AC_DEFINE(HAVE_NO_SEH, 1, - [Defined when mingw does not support SEH]) - fi - - # - # Check to see if the excpt.h include file provided contains the - # definition for EXCEPTION_DISPOSITION; if not, which is the case - # with Cygwin's version as of 2002-04-10, define it to be int, - # sufficient for getting the current code to work. - # - AC_CACHE_CHECK(for EXCEPTION_DISPOSITION support in include files, - tcl_cv_eh_disposition, - AC_TRY_COMPILE([ -# define WIN32_LEAN_AND_MEAN -# include -# undef WIN32_LEAN_AND_MEAN - ],[ - EXCEPTION_DISPOSITION x; - ], - tcl_cv_eh_disposition=yes, - tcl_cv_eh_disposition=no) - ) - if test "$tcl_cv_eh_disposition" = "no" ; then - AC_DEFINE(EXCEPTION_DISPOSITION, int, - [Defined when cygwin/mingw does not support EXCEPTION DISPOSITION]) - fi - - # Check to see if winnt.h defines CHAR, SHORT, and LONG - # even if VOID has already been #defined. The win32api - # used by mingw and cygwin is known to do this. - - AC_CACHE_CHECK(for winnt.h that ignores VOID define, - tcl_cv_winnt_ignore_void, - AC_TRY_COMPILE([ -#define VOID void -#define WIN32_LEAN_AND_MEAN -#include -#undef WIN32_LEAN_AND_MEAN - ], [ - CHAR c; - SHORT s; - LONG l; - ], - tcl_cv_winnt_ignore_void=yes, - tcl_cv_winnt_ignore_void=no) - ) - if test "$tcl_cv_winnt_ignore_void" = "yes" ; then - AC_DEFINE(HAVE_WINNT_IGNORE_VOID, 1, - [Defined when cygwin/mingw ignores VOID define in winnt.h]) - fi - fi - - # See if the compiler supports casting to a union type. - # This is used to stop gcc from printing a compiler - # warning when initializing a union member. - - AC_CACHE_CHECK(for cast to union support, - tcl_cv_cast_to_union, - AC_TRY_COMPILE([], - [ - union foo { int i; double d; }; - union foo f = (union foo) (int) 0; - ], - tcl_cv_cast_to_union=yes, - tcl_cv_cast_to_union=no) - ) - if test "$tcl_cv_cast_to_union" = "yes"; then - AC_DEFINE(HAVE_CAST_TO_UNION, 1, - [Defined when compiler supports casting to union type.]) - fi - - AC_SUBST(CFLAGS_DEBUG) - AC_SUBST(CFLAGS_OPTIMIZE) - AC_SUBST(CFLAGS_WARNING) - - AC_SUBST(STLIB_LD) - AC_SUBST(SHLIB_LD) - - AC_SUBST(SHLIB_LD_LIBS) - AC_SUBST(SHLIB_CFLAGS) - - AC_SUBST(LD_LIBRARY_PATH_VAR) - - # These must be called after we do the basic CFLAGS checks and - # verify any possible 64-bit or similar switches are necessary - TEA_TCL_EARLY_FLAGS - TEA_TCL_64BIT_FLAGS -]) - -#-------------------------------------------------------------------- -# TEA_SERIAL_PORT -# -# Determine which interface to use to talk to the serial port. -# Note that #include lines must begin in leftmost column for -# some compilers to recognize them as preprocessor directives, -# and some build environments have stdin not pointing at a -# pseudo-terminal (usually /dev/null instead.) -# -# Arguments: -# none -# -# Results: -# -# Defines only one of the following vars: -# HAVE_SYS_MODEM_H -# USE_TERMIOS -# USE_TERMIO -# USE_SGTTY -#-------------------------------------------------------------------- - -AC_DEFUN([TEA_SERIAL_PORT], [ - AC_CHECK_HEADERS(sys/modem.h) - AC_CACHE_CHECK([termios vs. termio vs. sgtty], tcl_cv_api_serial, [ - AC_TRY_RUN([ -#include - -int main() { - struct termios t; - if (tcgetattr(0, &t) == 0) { - cfsetospeed(&t, 0); - t.c_cflag |= PARENB | PARODD | CSIZE | CSTOPB; - return 0; - } - return 1; -}], tcl_cv_api_serial=termios, tcl_cv_api_serial=no, tcl_cv_api_serial=no) - if test $tcl_cv_api_serial = no ; then - AC_TRY_RUN([ -#include - -int main() { - struct termio t; - if (ioctl(0, TCGETA, &t) == 0) { - t.c_cflag |= CBAUD | PARENB | PARODD | CSIZE | CSTOPB; - return 0; - } - return 1; -}], tcl_cv_api_serial=termio, tcl_cv_api_serial=no, tcl_cv_api_serial=no) - fi - if test $tcl_cv_api_serial = no ; then - AC_TRY_RUN([ -#include - -int main() { - struct sgttyb t; - if (ioctl(0, TIOCGETP, &t) == 0) { - t.sg_ospeed = 0; - t.sg_flags |= ODDP | EVENP | RAW; - return 0; - } - return 1; -}], tcl_cv_api_serial=sgtty, tcl_cv_api_serial=no, tcl_cv_api_serial=no) - fi - if test $tcl_cv_api_serial = no ; then - AC_TRY_RUN([ -#include -#include - -int main() { - struct termios t; - if (tcgetattr(0, &t) == 0 - || errno == ENOTTY || errno == ENXIO || errno == EINVAL) { - cfsetospeed(&t, 0); - t.c_cflag |= PARENB | PARODD | CSIZE | CSTOPB; - return 0; - } - return 1; -}], tcl_cv_api_serial=termios, tcl_cv_api_serial=no, tcl_cv_api_serial=no) - fi - if test $tcl_cv_api_serial = no; then - AC_TRY_RUN([ -#include -#include - -int main() { - struct termio t; - if (ioctl(0, TCGETA, &t) == 0 - || errno == ENOTTY || errno == ENXIO || errno == EINVAL) { - t.c_cflag |= CBAUD | PARENB | PARODD | CSIZE | CSTOPB; - return 0; - } - return 1; - }], tcl_cv_api_serial=termio, tcl_cv_api_serial=no, tcl_cv_api_serial=no) - fi - if test $tcl_cv_api_serial = no; then - AC_TRY_RUN([ -#include -#include - -int main() { - struct sgttyb t; - if (ioctl(0, TIOCGETP, &t) == 0 - || errno == ENOTTY || errno == ENXIO || errno == EINVAL) { - t.sg_ospeed = 0; - t.sg_flags |= ODDP | EVENP | RAW; - return 0; - } - return 1; -}], tcl_cv_api_serial=sgtty, tcl_cv_api_serial=none, tcl_cv_api_serial=none) - fi]) - case $tcl_cv_api_serial in - termios) AC_DEFINE(USE_TERMIOS, 1, [Use the termios API for serial lines]);; - termio) AC_DEFINE(USE_TERMIO, 1, [Use the termio API for serial lines]);; - sgtty) AC_DEFINE(USE_SGTTY, 1, [Use the sgtty API for serial lines]);; - esac -]) - -#-------------------------------------------------------------------- -# TEA_MISSING_POSIX_HEADERS -# -# Supply substitutes for missing POSIX header files. Special -# notes: -# - stdlib.h doesn't define strtol, strtoul, or -# strtod in some versions of SunOS -# - some versions of string.h don't declare procedures such -# as strstr -# -# Arguments: -# none -# -# Results: -# -# Defines some of the following vars: -# NO_DIRENT_H -# NO_ERRNO_H -# NO_VALUES_H -# HAVE_LIMITS_H or NO_LIMITS_H -# NO_STDLIB_H -# NO_STRING_H -# NO_SYS_WAIT_H -# NO_DLFCN_H -# HAVE_SYS_PARAM_H -# -# HAVE_STRING_H ? -# -# tkUnixPort.h checks for HAVE_LIMITS_H, so do both HAVE and -# CHECK on limits.h -#-------------------------------------------------------------------- - -AC_DEFUN([TEA_MISSING_POSIX_HEADERS], [ - AC_CACHE_CHECK([dirent.h], tcl_cv_dirent_h, [ - AC_TRY_LINK([#include -#include ], [ -#ifndef _POSIX_SOURCE -# ifdef __Lynx__ - /* - * Generate compilation error to make the test fail: Lynx headers - * are only valid if really in the POSIX environment. - */ - - missing_procedure(); -# endif -#endif -DIR *d; -struct dirent *entryPtr; -char *p; -d = opendir("foobar"); -entryPtr = readdir(d); -p = entryPtr->d_name; -closedir(d); -], tcl_cv_dirent_h=yes, tcl_cv_dirent_h=no)]) - - if test $tcl_cv_dirent_h = no; then - AC_DEFINE(NO_DIRENT_H, 1, [Do we have ?]) - fi - - # TEA specific: - AC_CHECK_HEADER(errno.h, , [AC_DEFINE(NO_ERRNO_H, 1, [Do we have ?])]) - AC_CHECK_HEADER(float.h, , [AC_DEFINE(NO_FLOAT_H, 1, [Do we have ?])]) - AC_CHECK_HEADER(values.h, , [AC_DEFINE(NO_VALUES_H, 1, [Do we have ?])]) - AC_CHECK_HEADER(limits.h, - [AC_DEFINE(HAVE_LIMITS_H, 1, [Do we have ?])], - [AC_DEFINE(NO_LIMITS_H, 1, [Do we have ?])]) - AC_CHECK_HEADER(stdlib.h, tcl_ok=1, tcl_ok=0) - AC_EGREP_HEADER(strtol, stdlib.h, , tcl_ok=0) - AC_EGREP_HEADER(strtoul, stdlib.h, , tcl_ok=0) - AC_EGREP_HEADER(strtod, stdlib.h, , tcl_ok=0) - if test $tcl_ok = 0; then - AC_DEFINE(NO_STDLIB_H, 1, [Do we have ?]) - fi - AC_CHECK_HEADER(string.h, tcl_ok=1, tcl_ok=0) - AC_EGREP_HEADER(strstr, string.h, , tcl_ok=0) - AC_EGREP_HEADER(strerror, string.h, , tcl_ok=0) - - # See also memmove check below for a place where NO_STRING_H can be - # set and why. - - if test $tcl_ok = 0; then - AC_DEFINE(NO_STRING_H, 1, [Do we have ?]) - fi - - AC_CHECK_HEADER(sys/wait.h, , [AC_DEFINE(NO_SYS_WAIT_H, 1, [Do we have ?])]) - AC_CHECK_HEADER(dlfcn.h, , [AC_DEFINE(NO_DLFCN_H, 1, [Do we have ?])]) - - # OS/390 lacks sys/param.h (and doesn't need it, by chance). - AC_HAVE_HEADERS(sys/param.h) -]) - -#-------------------------------------------------------------------- -# TEA_PATH_X -# -# Locate the X11 header files and the X11 library archive. Try -# the ac_path_x macro first, but if it doesn't find the X stuff -# (e.g. because there's no xmkmf program) then check through -# a list of possible directories. Under some conditions the -# autoconf macro will return an include directory that contains -# no include files, so double-check its result just to be safe. -# -# This should be called after TEA_CONFIG_CFLAGS as setting the -# LIBS line can confuse some configure macro magic. -# -# Arguments: -# none -# -# Results: -# -# Sets the following vars: -# XINCLUDES -# XLIBSW -# PKG_LIBS (appends to) -#-------------------------------------------------------------------- - -AC_DEFUN([TEA_PATH_X], [ - if test "${TEA_WINDOWINGSYSTEM}" = "x11" ; then - TEA_PATH_UNIX_X - fi -]) - -AC_DEFUN([TEA_PATH_UNIX_X], [ - AC_PATH_X - not_really_there="" - if test "$no_x" = ""; then - if test "$x_includes" = ""; then - AC_TRY_CPP([#include ], , not_really_there="yes") - else - if test ! -r $x_includes/X11/Xlib.h; then - not_really_there="yes" - fi - fi - fi - if test "$no_x" = "yes" -o "$not_really_there" = "yes"; then - AC_MSG_CHECKING([for X11 header files]) - found_xincludes="no" - AC_TRY_CPP([#include ], found_xincludes="yes", found_xincludes="no") - if test "$found_xincludes" = "no"; then - dirs="/usr/unsupported/include /usr/local/include /usr/X386/include /usr/X11R6/include /usr/X11R5/include /usr/include/X11R5 /usr/include/X11R4 /usr/openwin/include /usr/X11/include /usr/sww/include" - for i in $dirs ; do - if test -r $i/X11/Xlib.h; then - AC_MSG_RESULT([$i]) - XINCLUDES=" -I$i" - found_xincludes="yes" - break - fi - done - fi - else - if test "$x_includes" != ""; then - XINCLUDES="-I$x_includes" - found_xincludes="yes" - fi - fi - if test "$found_xincludes" = "no"; then - AC_MSG_RESULT([couldn't find any!]) - fi - - if test "$no_x" = yes; then - AC_MSG_CHECKING([for X11 libraries]) - XLIBSW=nope - dirs="/usr/unsupported/lib /usr/local/lib /usr/X386/lib /usr/X11R6/lib /usr/X11R5/lib /usr/lib/X11R5 /usr/lib/X11R4 /usr/openwin/lib /usr/X11/lib /usr/sww/X11/lib" - for i in $dirs ; do - if test -r $i/libX11.a -o -r $i/libX11.so -o -r $i/libX11.sl -o -r $i/libX11.dylib; then - AC_MSG_RESULT([$i]) - XLIBSW="-L$i -lX11" - x_libraries="$i" - break - fi - done - else - if test "$x_libraries" = ""; then - XLIBSW=-lX11 - else - XLIBSW="-L$x_libraries -lX11" - fi - fi - if test "$XLIBSW" = nope ; then - AC_CHECK_LIB(Xwindow, XCreateWindow, XLIBSW=-lXwindow) - fi - if test "$XLIBSW" = nope ; then - AC_MSG_RESULT([could not find any! Using -lX11.]) - XLIBSW=-lX11 - fi - # TEA specific: - if test x"${XLIBSW}" != x ; then - PKG_LIBS="${PKG_LIBS} ${XLIBSW}" - fi -]) - -#-------------------------------------------------------------------- -# TEA_BLOCKING_STYLE -# -# The statements below check for systems where POSIX-style -# non-blocking I/O (O_NONBLOCK) doesn't work or is unimplemented. -# On these systems (mostly older ones), use the old BSD-style -# FIONBIO approach instead. -# -# Arguments: -# none -# -# Results: -# -# Defines some of the following vars: -# HAVE_SYS_IOCTL_H -# HAVE_SYS_FILIO_H -# USE_FIONBIO -# O_NONBLOCK -#-------------------------------------------------------------------- - -AC_DEFUN([TEA_BLOCKING_STYLE], [ - AC_CHECK_HEADERS(sys/ioctl.h) - AC_CHECK_HEADERS(sys/filio.h) - TEA_CONFIG_SYSTEM - AC_MSG_CHECKING([FIONBIO vs. O_NONBLOCK for nonblocking I/O]) - case $system in - OSF*) - AC_DEFINE(USE_FIONBIO, 1, [Should we use FIONBIO?]) - AC_MSG_RESULT([FIONBIO]) - ;; - *) - AC_MSG_RESULT([O_NONBLOCK]) - ;; - esac -]) - -#-------------------------------------------------------------------- -# TEA_TIME_HANDLER -# -# Checks how the system deals with time.h, what time structures -# are used on the system, and what fields the structures have. -# -# Arguments: -# none -# -# Results: -# -# Defines some of the following vars: -# USE_DELTA_FOR_TZ -# HAVE_TM_GMTOFF -# HAVE_TM_TZADJ -# HAVE_TIMEZONE_VAR -#-------------------------------------------------------------------- - -AC_DEFUN([TEA_TIME_HANDLER], [ - AC_CHECK_HEADERS(sys/time.h) - AC_HEADER_TIME - AC_STRUCT_TIMEZONE - - AC_CHECK_FUNCS(gmtime_r localtime_r) - - AC_CACHE_CHECK([tm_tzadj in struct tm], tcl_cv_member_tm_tzadj, [ - AC_TRY_COMPILE([#include ], [struct tm tm; tm.tm_tzadj;], - tcl_cv_member_tm_tzadj=yes, tcl_cv_member_tm_tzadj=no)]) - if test $tcl_cv_member_tm_tzadj = yes ; then - AC_DEFINE(HAVE_TM_TZADJ, 1, [Should we use the tm_tzadj field of struct tm?]) - fi - - AC_CACHE_CHECK([tm_gmtoff in struct tm], tcl_cv_member_tm_gmtoff, [ - AC_TRY_COMPILE([#include ], [struct tm tm; tm.tm_gmtoff;], - tcl_cv_member_tm_gmtoff=yes, tcl_cv_member_tm_gmtoff=no)]) - if test $tcl_cv_member_tm_gmtoff = yes ; then - AC_DEFINE(HAVE_TM_GMTOFF, 1, [Should we use the tm_gmtoff field of struct tm?]) - fi - - # - # Its important to include time.h in this check, as some systems - # (like convex) have timezone functions, etc. - # - AC_CACHE_CHECK([long timezone variable], tcl_cv_timezone_long, [ - AC_TRY_COMPILE([#include ], - [extern long timezone; - timezone += 1; - exit (0);], - tcl_cv_timezone_long=yes, tcl_cv_timezone_long=no)]) - if test $tcl_cv_timezone_long = yes ; then - AC_DEFINE(HAVE_TIMEZONE_VAR, 1, [Should we use the global timezone variable?]) - else - # - # On some systems (eg IRIX 6.2), timezone is a time_t and not a long. - # - AC_CACHE_CHECK([time_t timezone variable], tcl_cv_timezone_time, [ - AC_TRY_COMPILE([#include ], - [extern time_t timezone; - timezone += 1; - exit (0);], - tcl_cv_timezone_time=yes, tcl_cv_timezone_time=no)]) - if test $tcl_cv_timezone_time = yes ; then - AC_DEFINE(HAVE_TIMEZONE_VAR, 1, [Should we use the global timezone variable?]) - fi - fi -]) - -#-------------------------------------------------------------------- -# TEA_BUGGY_STRTOD -# -# Under Solaris 2.4, strtod returns the wrong value for the -# terminating character under some conditions. Check for this -# and if the problem exists use a substitute procedure -# "fixstrtod" (provided by Tcl) that corrects the error. -# Also, on Compaq's Tru64 Unix 5.0, -# strtod(" ") returns 0.0 instead of a failure to convert. -# -# Arguments: -# none -# -# Results: -# -# Might defines some of the following vars: -# strtod (=fixstrtod) -#-------------------------------------------------------------------- - -AC_DEFUN([TEA_BUGGY_STRTOD], [ - AC_CHECK_FUNC(strtod, tcl_strtod=1, tcl_strtod=0) - if test "$tcl_strtod" = 1; then - AC_CACHE_CHECK([for Solaris2.4/Tru64 strtod bugs], tcl_cv_strtod_buggy,[ - AC_TRY_RUN([ - extern double strtod(); - int main() { - char *infString="Inf", *nanString="NaN", *spaceString=" "; - char *term; - double value; - value = strtod(infString, &term); - if ((term != infString) && (term[-1] == 0)) { - exit(1); - } - value = strtod(nanString, &term); - if ((term != nanString) && (term[-1] == 0)) { - exit(1); - } - value = strtod(spaceString, &term); - if (term == (spaceString+1)) { - exit(1); - } - exit(0); - }], tcl_cv_strtod_buggy=ok, tcl_cv_strtod_buggy=buggy, - tcl_cv_strtod_buggy=buggy)]) - if test "$tcl_cv_strtod_buggy" = buggy; then - AC_LIBOBJ([fixstrtod]) - USE_COMPAT=1 - AC_DEFINE(strtod, fixstrtod, [Do we want to use the strtod() in compat?]) - fi - fi -]) - -#-------------------------------------------------------------------- -# TEA_TCL_LINK_LIBS -# -# Search for the libraries needed to link the Tcl shell. -# Things like the math library (-lm) and socket stuff (-lsocket vs. -# -lnsl) are dealt with here. -# -# Arguments: -# Requires the following vars to be set in the Makefile: -# DL_LIBS (not in TEA, only needed in core) -# LIBS -# MATH_LIBS -# -# Results: -# -# Substitutes the following vars: -# TCL_LIBS -# MATH_LIBS -# -# Might append to the following vars: -# LIBS -# -# Might define the following vars: -# HAVE_NET_ERRNO_H -#-------------------------------------------------------------------- - -AC_DEFUN([TEA_TCL_LINK_LIBS], [ - #-------------------------------------------------------------------- - # On a few very rare systems, all of the libm.a stuff is - # already in libc.a. Set compiler flags accordingly. - # Also, Linux requires the "ieee" library for math to work - # right (and it must appear before "-lm"). - #-------------------------------------------------------------------- - - AC_CHECK_FUNC(sin, MATH_LIBS="", MATH_LIBS="-lm") - AC_CHECK_LIB(ieee, main, [MATH_LIBS="-lieee $MATH_LIBS"]) - - #-------------------------------------------------------------------- - # Interactive UNIX requires -linet instead of -lsocket, plus it - # needs net/errno.h to define the socket-related error codes. - #-------------------------------------------------------------------- - - AC_CHECK_LIB(inet, main, [LIBS="$LIBS -linet"]) - AC_CHECK_HEADER(net/errno.h, [ - AC_DEFINE(HAVE_NET_ERRNO_H, 1, [Do we have ?])]) - - #-------------------------------------------------------------------- - # Check for the existence of the -lsocket and -lnsl libraries. - # The order here is important, so that they end up in the right - # order in the command line generated by make. Here are some - # special considerations: - # 1. Use "connect" and "accept" to check for -lsocket, and - # "gethostbyname" to check for -lnsl. - # 2. Use each function name only once: can't redo a check because - # autoconf caches the results of the last check and won't redo it. - # 3. Use -lnsl and -lsocket only if they supply procedures that - # aren't already present in the normal libraries. This is because - # IRIX 5.2 has libraries, but they aren't needed and they're - # bogus: they goof up name resolution if used. - # 4. On some SVR4 systems, can't use -lsocket without -lnsl too. - # To get around this problem, check for both libraries together - # if -lsocket doesn't work by itself. - #-------------------------------------------------------------------- - - tcl_checkBoth=0 - AC_CHECK_FUNC(connect, tcl_checkSocket=0, tcl_checkSocket=1) - if test "$tcl_checkSocket" = 1; then - AC_CHECK_FUNC(setsockopt, , [AC_CHECK_LIB(socket, setsockopt, - LIBS="$LIBS -lsocket", tcl_checkBoth=1)]) - fi - if test "$tcl_checkBoth" = 1; then - tk_oldLibs=$LIBS - LIBS="$LIBS -lsocket -lnsl" - AC_CHECK_FUNC(accept, tcl_checkNsl=0, [LIBS=$tk_oldLibs]) - fi - AC_CHECK_FUNC(gethostbyname, , [AC_CHECK_LIB(nsl, gethostbyname, - [LIBS="$LIBS -lnsl"])]) - - # TEA specific: Don't perform the eval of the libraries here because - # DL_LIBS won't be set until we call TEA_CONFIG_CFLAGS - - TCL_LIBS='${DL_LIBS} ${LIBS} ${MATH_LIBS}' - AC_SUBST(TCL_LIBS) - AC_SUBST(MATH_LIBS) -]) - -#-------------------------------------------------------------------- -# TEA_TCL_EARLY_FLAGS -# -# Check for what flags are needed to be passed so the correct OS -# features are available. -# -# Arguments: -# None -# -# Results: -# -# Might define the following vars: -# _ISOC99_SOURCE -# _LARGEFILE64_SOURCE -# _LARGEFILE_SOURCE64 -#-------------------------------------------------------------------- - -AC_DEFUN([TEA_TCL_EARLY_FLAG],[ - AC_CACHE_VAL([tcl_cv_flag_]translit($1,[A-Z],[a-z]), - AC_TRY_COMPILE([$2], $3, [tcl_cv_flag_]translit($1,[A-Z],[a-z])=no, - AC_TRY_COMPILE([[#define ]$1[ 1 -]$2], $3, - [tcl_cv_flag_]translit($1,[A-Z],[a-z])=yes, - [tcl_cv_flag_]translit($1,[A-Z],[a-z])=no))) - if test ["x${tcl_cv_flag_]translit($1,[A-Z],[a-z])[}" = "xyes"] ; then - AC_DEFINE($1, 1, [Add the ]$1[ flag when building]) - tcl_flags="$tcl_flags $1" - fi -]) - -AC_DEFUN([TEA_TCL_EARLY_FLAGS],[ - AC_MSG_CHECKING([for required early compiler flags]) - tcl_flags="" - TEA_TCL_EARLY_FLAG(_ISOC99_SOURCE,[#include ], - [char *p = (char *)strtoll; char *q = (char *)strtoull;]) - TEA_TCL_EARLY_FLAG(_LARGEFILE64_SOURCE,[#include ], - [struct stat64 buf; int i = stat64("/", &buf);]) - TEA_TCL_EARLY_FLAG(_LARGEFILE_SOURCE64,[#include ], - [char *p = (char *)open64;]) - if test "x${tcl_flags}" = "x" ; then - AC_MSG_RESULT([none]) - else - AC_MSG_RESULT([${tcl_flags}]) - fi -]) - -#-------------------------------------------------------------------- -# TEA_TCL_64BIT_FLAGS -# -# Check for what is defined in the way of 64-bit features. -# -# Arguments: -# None -# -# Results: -# -# Might define the following vars: -# TCL_WIDE_INT_IS_LONG -# TCL_WIDE_INT_TYPE -# HAVE_STRUCT_DIRENT64 -# HAVE_STRUCT_STAT64 -# HAVE_TYPE_OFF64_T -#-------------------------------------------------------------------- - -AC_DEFUN([TEA_TCL_64BIT_FLAGS], [ - AC_MSG_CHECKING([for 64-bit integer type]) - AC_CACHE_VAL(tcl_cv_type_64bit,[ - tcl_cv_type_64bit=none - # See if the compiler knows natively about __int64 - AC_TRY_COMPILE(,[__int64 value = (__int64) 0;], - tcl_type_64bit=__int64, tcl_type_64bit="long long") - # See if we should use long anyway Note that we substitute in the - # type that is our current guess for a 64-bit type inside this check - # program, so it should be modified only carefully... - AC_TRY_COMPILE(,[switch (0) { - case 1: case (sizeof(]${tcl_type_64bit}[)==sizeof(long)): ; - }],tcl_cv_type_64bit=${tcl_type_64bit})]) - if test "${tcl_cv_type_64bit}" = none ; then - AC_DEFINE(TCL_WIDE_INT_IS_LONG, 1, [Are wide integers to be implemented with C 'long's?]) - AC_MSG_RESULT([using long]) - elif test "${tcl_cv_type_64bit}" = "__int64" \ - -a "${TEA_PLATFORM}" = "windows" ; then - # TEA specific: We actually want to use the default tcl.h checks in - # this case to handle both TCL_WIDE_INT_TYPE and TCL_LL_MODIFIER* - AC_MSG_RESULT([using Tcl header defaults]) - else - AC_DEFINE_UNQUOTED(TCL_WIDE_INT_TYPE,${tcl_cv_type_64bit}, - [What type should be used to define wide integers?]) - AC_MSG_RESULT([${tcl_cv_type_64bit}]) - - # Now check for auxiliary declarations - AC_CACHE_CHECK([for struct dirent64], tcl_cv_struct_dirent64,[ - AC_TRY_COMPILE([#include -#include ],[struct dirent64 p;], - tcl_cv_struct_dirent64=yes,tcl_cv_struct_dirent64=no)]) - if test "x${tcl_cv_struct_dirent64}" = "xyes" ; then - AC_DEFINE(HAVE_STRUCT_DIRENT64, 1, [Is 'struct dirent64' in ?]) - fi - - AC_CACHE_CHECK([for struct stat64], tcl_cv_struct_stat64,[ - AC_TRY_COMPILE([#include ],[struct stat64 p; -], - tcl_cv_struct_stat64=yes,tcl_cv_struct_stat64=no)]) - if test "x${tcl_cv_struct_stat64}" = "xyes" ; then - AC_DEFINE(HAVE_STRUCT_STAT64, 1, [Is 'struct stat64' in ?]) - fi - - AC_CHECK_FUNCS(open64 lseek64) - AC_MSG_CHECKING([for off64_t]) - AC_CACHE_VAL(tcl_cv_type_off64_t,[ - AC_TRY_COMPILE([#include ],[off64_t offset; -], - tcl_cv_type_off64_t=yes,tcl_cv_type_off64_t=no)]) - dnl Define HAVE_TYPE_OFF64_T only when the off64_t type and the - dnl functions lseek64 and open64 are defined. - if test "x${tcl_cv_type_off64_t}" = "xyes" && \ - test "x${ac_cv_func_lseek64}" = "xyes" && \ - test "x${ac_cv_func_open64}" = "xyes" ; then - AC_DEFINE(HAVE_TYPE_OFF64_T, 1, [Is off64_t in ?]) - AC_MSG_RESULT([yes]) - else - AC_MSG_RESULT([no]) - fi - fi -]) - -## -## Here ends the standard Tcl configuration bits and starts the -## TEA specific functions -## - -#------------------------------------------------------------------------ -# TEA_INIT -- -# -# Init various Tcl Extension Architecture (TEA) variables. -# This should be the first called TEA_* macro. -# -# Arguments: -# none -# -# Results: -# -# Defines and substs the following vars: -# CYGPATH -# EXEEXT -# Defines only: -# TEA_VERSION -# TEA_INITED -# TEA_PLATFORM (windows or unix) -# -# "cygpath" is used on windows to generate native path names for include -# files. These variables should only be used with the compiler and linker -# since they generate native path names. -# -# EXEEXT -# Select the executable extension based on the host type. This -# is a lightweight replacement for AC_EXEEXT that doesn't require -# a compiler. -#------------------------------------------------------------------------ - -AC_DEFUN([TEA_INIT], [ - # TEA extensions pass this us the version of TEA they think they - # are compatible with. - TEA_VERSION="3.9" - - AC_MSG_CHECKING([for correct TEA configuration]) - if test x"${PACKAGE_NAME}" = x ; then - AC_MSG_ERROR([ -The PACKAGE_NAME variable must be defined by your TEA configure.in]) - fi - if test x"$1" = x ; then - AC_MSG_ERROR([ -TEA version not specified.]) - elif test "$1" != "${TEA_VERSION}" ; then - AC_MSG_RESULT([warning: requested TEA version "$1", have "${TEA_VERSION}"]) - else - AC_MSG_RESULT([ok (TEA ${TEA_VERSION})]) - fi - - # If the user did not set CFLAGS, set it now to keep macros - # like AC_PROG_CC and AC_TRY_COMPILE from adding "-g -O2". - if test "${CFLAGS+set}" != "set" ; then - CFLAGS="" - fi - - case "`uname -s`" in - *win32*|*WIN32*|*MINGW32_*) - AC_CHECK_PROG(CYGPATH, cygpath, cygpath -w, echo) - EXEEXT=".exe" - TEA_PLATFORM="windows" - ;; - *CYGWIN_*) - CYGPATH=echo - EXEEXT=".exe" - # TEA_PLATFORM is determined later in LOAD_TCLCONFIG - ;; - *) - CYGPATH=echo - # Maybe we are cross-compiling.... - case ${host_alias} in - *mingw32*) - EXEEXT=".exe" - TEA_PLATFORM="windows" - ;; - *) - EXEEXT="" - TEA_PLATFORM="unix" - ;; - esac - ;; - esac - - # Check if exec_prefix is set. If not use fall back to prefix. - # Note when adjusted, so that TEA_PREFIX can correct for this. - # This is needed for recursive configures, since autoconf propagates - # $prefix, but not $exec_prefix (doh!). - if test x$exec_prefix = xNONE ; then - exec_prefix_default=yes - exec_prefix=$prefix - fi - - AC_MSG_NOTICE([configuring ${PACKAGE_NAME} ${PACKAGE_VERSION}]) - - AC_SUBST(EXEEXT) - AC_SUBST(CYGPATH) - - # This package name must be replaced statically for AC_SUBST to work - AC_SUBST(PKG_LIB_FILE) - # Substitute STUB_LIB_FILE in case package creates a stub library too. - AC_SUBST(PKG_STUB_LIB_FILE) - - # We AC_SUBST these here to ensure they are subst'ed, - # in case the user doesn't call TEA_ADD_... - AC_SUBST(PKG_STUB_SOURCES) - AC_SUBST(PKG_STUB_OBJECTS) - AC_SUBST(PKG_TCL_SOURCES) - AC_SUBST(PKG_HEADERS) - AC_SUBST(PKG_INCLUDES) - AC_SUBST(PKG_LIBS) - AC_SUBST(PKG_CFLAGS) -]) - -#------------------------------------------------------------------------ -# TEA_ADD_SOURCES -- -# -# Specify one or more source files. Users should check for -# the right platform before adding to their list. -# It is not important to specify the directory, as long as it is -# in the generic, win or unix subdirectory of $(srcdir). -# -# Arguments: -# one or more file names -# -# Results: -# -# Defines and substs the following vars: -# PKG_SOURCES -# PKG_OBJECTS -#------------------------------------------------------------------------ -AC_DEFUN([TEA_ADD_SOURCES], [ - vars="$@" - for i in $vars; do - case $i in - [\$]*) - # allow $-var names - PKG_SOURCES="$PKG_SOURCES $i" - PKG_OBJECTS="$PKG_OBJECTS $i" - ;; - *) - # check for existence - allows for generic/win/unix VPATH - # To add more dirs here (like 'src'), you have to update VPATH - # in Makefile.in as well - if test ! -f "${srcdir}/$i" -a ! -f "${srcdir}/generic/$i" \ - -a ! -f "${srcdir}/win/$i" -a ! -f "${srcdir}/unix/$i" \ - -a ! -f "${srcdir}/macosx/$i" \ - ; then - AC_MSG_ERROR([could not find source file '$i']) - fi - PKG_SOURCES="$PKG_SOURCES $i" - # this assumes it is in a VPATH dir - i=`basename $i` - # handle user calling this before or after TEA_SETUP_COMPILER - if test x"${OBJEXT}" != x ; then - j="`echo $i | sed -e 's/\.[[^.]]*$//'`.${OBJEXT}" - else - j="`echo $i | sed -e 's/\.[[^.]]*$//'`.\${OBJEXT}" - fi - PKG_OBJECTS="$PKG_OBJECTS $j" - ;; - esac - done - AC_SUBST(PKG_SOURCES) - AC_SUBST(PKG_OBJECTS) -]) - -#------------------------------------------------------------------------ -# TEA_ADD_STUB_SOURCES -- -# -# Specify one or more source files. Users should check for -# the right platform before adding to their list. -# It is not important to specify the directory, as long as it is -# in the generic, win or unix subdirectory of $(srcdir). -# -# Arguments: -# one or more file names -# -# Results: -# -# Defines and substs the following vars: -# PKG_STUB_SOURCES -# PKG_STUB_OBJECTS -#------------------------------------------------------------------------ -AC_DEFUN([TEA_ADD_STUB_SOURCES], [ - vars="$@" - for i in $vars; do - # check for existence - allows for generic/win/unix VPATH - if test ! -f "${srcdir}/$i" -a ! -f "${srcdir}/generic/$i" \ - -a ! -f "${srcdir}/win/$i" -a ! -f "${srcdir}/unix/$i" \ - -a ! -f "${srcdir}/macosx/$i" \ - ; then - AC_MSG_ERROR([could not find stub source file '$i']) - fi - PKG_STUB_SOURCES="$PKG_STUB_SOURCES $i" - # this assumes it is in a VPATH dir - i=`basename $i` - # handle user calling this before or after TEA_SETUP_COMPILER - if test x"${OBJEXT}" != x ; then - j="`echo $i | sed -e 's/\.[[^.]]*$//'`.${OBJEXT}" - else - j="`echo $i | sed -e 's/\.[[^.]]*$//'`.\${OBJEXT}" - fi - PKG_STUB_OBJECTS="$PKG_STUB_OBJECTS $j" - done - AC_SUBST(PKG_STUB_SOURCES) - AC_SUBST(PKG_STUB_OBJECTS) -]) - -#------------------------------------------------------------------------ -# TEA_ADD_TCL_SOURCES -- -# -# Specify one or more Tcl source files. These should be platform -# independent runtime files. -# -# Arguments: -# one or more file names -# -# Results: -# -# Defines and substs the following vars: -# PKG_TCL_SOURCES -#------------------------------------------------------------------------ -AC_DEFUN([TEA_ADD_TCL_SOURCES], [ - vars="$@" - for i in $vars; do - # check for existence, be strict because it is installed - if test ! -f "${srcdir}/$i" ; then - AC_MSG_ERROR([could not find tcl source file '${srcdir}/$i']) - fi - PKG_TCL_SOURCES="$PKG_TCL_SOURCES $i" - done - AC_SUBST(PKG_TCL_SOURCES) -]) - -#------------------------------------------------------------------------ -# TEA_ADD_HEADERS -- -# -# Specify one or more source headers. Users should check for -# the right platform before adding to their list. -# -# Arguments: -# one or more file names -# -# Results: -# -# Defines and substs the following vars: -# PKG_HEADERS -#------------------------------------------------------------------------ -AC_DEFUN([TEA_ADD_HEADERS], [ - vars="$@" - for i in $vars; do - # check for existence, be strict because it is installed - if test ! -f "${srcdir}/$i" ; then - AC_MSG_ERROR([could not find header file '${srcdir}/$i']) - fi - PKG_HEADERS="$PKG_HEADERS $i" - done - AC_SUBST(PKG_HEADERS) -]) - -#------------------------------------------------------------------------ -# TEA_ADD_INCLUDES -- -# -# Specify one or more include dirs. Users should check for -# the right platform before adding to their list. -# -# Arguments: -# one or more file names -# -# Results: -# -# Defines and substs the following vars: -# PKG_INCLUDES -#------------------------------------------------------------------------ -AC_DEFUN([TEA_ADD_INCLUDES], [ - vars="$@" - for i in $vars; do - PKG_INCLUDES="$PKG_INCLUDES $i" - done - AC_SUBST(PKG_INCLUDES) -]) - -#------------------------------------------------------------------------ -# TEA_ADD_LIBS -- -# -# Specify one or more libraries. Users should check for -# the right platform before adding to their list. For Windows, -# libraries provided in "foo.lib" format will be converted to -# "-lfoo" when using GCC (mingw). -# -# Arguments: -# one or more file names -# -# Results: -# -# Defines and substs the following vars: -# PKG_LIBS -#------------------------------------------------------------------------ -AC_DEFUN([TEA_ADD_LIBS], [ - vars="$@" - for i in $vars; do - if test "${TEA_PLATFORM}" = "windows" -a "$GCC" = "yes" ; then - # Convert foo.lib to -lfoo for GCC. No-op if not *.lib - i=`echo "$i" | sed -e 's/^\([[^-]].*\)\.lib[$]/-l\1/i'` - fi - PKG_LIBS="$PKG_LIBS $i" - done - AC_SUBST(PKG_LIBS) -]) - -#------------------------------------------------------------------------ -# TEA_ADD_CFLAGS -- -# -# Specify one or more CFLAGS. Users should check for -# the right platform before adding to their list. -# -# Arguments: -# one or more file names -# -# Results: -# -# Defines and substs the following vars: -# PKG_CFLAGS -#------------------------------------------------------------------------ -AC_DEFUN([TEA_ADD_CFLAGS], [ - PKG_CFLAGS="$PKG_CFLAGS $@" - AC_SUBST(PKG_CFLAGS) -]) - -#------------------------------------------------------------------------ -# TEA_ADD_CLEANFILES -- -# -# Specify one or more CLEANFILES. -# -# Arguments: -# one or more file names to clean target -# -# Results: -# -# Appends to CLEANFILES, already defined for subst in LOAD_TCLCONFIG -#------------------------------------------------------------------------ -AC_DEFUN([TEA_ADD_CLEANFILES], [ - CLEANFILES="$CLEANFILES $@" -]) - -#------------------------------------------------------------------------ -# TEA_PREFIX -- -# -# Handle the --prefix=... option by defaulting to what Tcl gave -# -# Arguments: -# none -# -# Results: -# -# If --prefix or --exec-prefix was not specified, $prefix and -# $exec_prefix will be set to the values given to Tcl when it was -# configured. -#------------------------------------------------------------------------ -AC_DEFUN([TEA_PREFIX], [ - if test "${prefix}" = "NONE"; then - prefix_default=yes - if test x"${TCL_PREFIX}" != x; then - AC_MSG_NOTICE([--prefix defaulting to TCL_PREFIX ${TCL_PREFIX}]) - prefix=${TCL_PREFIX} - else - AC_MSG_NOTICE([--prefix defaulting to /usr/local]) - prefix=/usr/local - fi - fi - if test "${exec_prefix}" = "NONE" -a x"${prefix_default}" = x"yes" \ - -o x"${exec_prefix_default}" = x"yes" ; then - if test x"${TCL_EXEC_PREFIX}" != x; then - AC_MSG_NOTICE([--exec-prefix defaulting to TCL_EXEC_PREFIX ${TCL_EXEC_PREFIX}]) - exec_prefix=${TCL_EXEC_PREFIX} - else - AC_MSG_NOTICE([--exec-prefix defaulting to ${prefix}]) - exec_prefix=$prefix - fi - fi -]) - -#------------------------------------------------------------------------ -# TEA_SETUP_COMPILER_CC -- -# -# Do compiler checks the way we want. This is just a replacement -# for AC_PROG_CC in TEA configure.in files to make them cleaner. -# -# Arguments: -# none -# -# Results: -# -# Sets up CC var and other standard bits we need to make executables. -#------------------------------------------------------------------------ -AC_DEFUN([TEA_SETUP_COMPILER_CC], [ - # Don't put any macros that use the compiler (e.g. AC_TRY_COMPILE) - # in this macro, they need to go into TEA_SETUP_COMPILER instead. - - AC_PROG_CC - AC_PROG_CPP - - INSTALL="\$(SHELL) \$(srcdir)/tclconfig/install-sh -c" - AC_SUBST(INSTALL) - INSTALL_DATA="\${INSTALL} -m 644" - AC_SUBST(INSTALL_DATA) - INSTALL_PROGRAM="\${INSTALL}" - AC_SUBST(INSTALL_PROGRAM) - INSTALL_SCRIPT="\${INSTALL}" - AC_SUBST(INSTALL_SCRIPT) - - #-------------------------------------------------------------------- - # Checks to see if the make program sets the $MAKE variable. - #-------------------------------------------------------------------- - - AC_PROG_MAKE_SET - - #-------------------------------------------------------------------- - # Find ranlib - #-------------------------------------------------------------------- - - AC_CHECK_TOOL(RANLIB, ranlib) - - #-------------------------------------------------------------------- - # Determines the correct binary file extension (.o, .obj, .exe etc.) - #-------------------------------------------------------------------- - - AC_OBJEXT - AC_EXEEXT -]) - -#------------------------------------------------------------------------ -# TEA_SETUP_COMPILER -- -# -# Do compiler checks that use the compiler. This must go after -# TEA_SETUP_COMPILER_CC, which does the actual compiler check. -# -# Arguments: -# none -# -# Results: -# -# Sets up CC var and other standard bits we need to make executables. -#------------------------------------------------------------------------ -AC_DEFUN([TEA_SETUP_COMPILER], [ - # Any macros that use the compiler (e.g. AC_TRY_COMPILE) have to go here. - AC_REQUIRE([TEA_SETUP_COMPILER_CC]) - - #------------------------------------------------------------------------ - # If we're using GCC, see if the compiler understands -pipe. If so, use it. - # It makes compiling go faster. (This is only a performance feature.) - #------------------------------------------------------------------------ - - if test -z "$no_pipe" -a -n "$GCC"; then - AC_CACHE_CHECK([if the compiler understands -pipe], - tcl_cv_cc_pipe, [ - hold_cflags=$CFLAGS; CFLAGS="$CFLAGS -pipe" - AC_TRY_COMPILE(,, tcl_cv_cc_pipe=yes, tcl_cv_cc_pipe=no) - CFLAGS=$hold_cflags]) - if test $tcl_cv_cc_pipe = yes; then - CFLAGS="$CFLAGS -pipe" - fi - fi - - #-------------------------------------------------------------------- - # Common compiler flag setup - #-------------------------------------------------------------------- - - AC_C_BIGENDIAN - if test "${TEA_PLATFORM}" = "unix" ; then - TEA_TCL_LINK_LIBS - TEA_MISSING_POSIX_HEADERS - # Let the user call this, because if it triggers, they will - # need a compat/strtod.c that is correct. Users can also - # use Tcl_GetDouble(FromObj) instead. - #TEA_BUGGY_STRTOD - fi -]) - -#------------------------------------------------------------------------ -# TEA_MAKE_LIB -- -# -# Generate a line that can be used to build a shared/unshared library -# in a platform independent manner. -# -# Arguments: -# none -# -# Requires: -# -# Results: -# -# Defines the following vars: -# CFLAGS - Done late here to note disturb other AC macros -# MAKE_LIB - Command to execute to build the Tcl library; -# differs depending on whether or not Tcl is being -# compiled as a shared library. -# MAKE_SHARED_LIB Makefile rule for building a shared library -# MAKE_STATIC_LIB Makefile rule for building a static library -# MAKE_STUB_LIB Makefile rule for building a stub library -# VC_MANIFEST_EMBED_DLL Makefile rule for embedded VC manifest in DLL -# VC_MANIFEST_EMBED_EXE Makefile rule for embedded VC manifest in EXE -#------------------------------------------------------------------------ - -AC_DEFUN([TEA_MAKE_LIB], [ - if test "${TEA_PLATFORM}" = "windows" -a "$GCC" != "yes"; then - MAKE_STATIC_LIB="\${STLIB_LD} -out:\[$]@ \$(PKG_OBJECTS)" - MAKE_SHARED_LIB="\${SHLIB_LD} \${SHLIB_LD_LIBS} \${LDFLAGS_DEFAULT} -out:\[$]@ \$(PKG_OBJECTS)" - AC_EGREP_CPP([manifest needed], [ -#if defined(_MSC_VER) && _MSC_VER >= 1400 -print("manifest needed") -#endif - ], [ - # Could do a CHECK_PROG for mt, but should always be with MSVC8+ - VC_MANIFEST_EMBED_DLL="if test -f \[$]@.manifest ; then mt.exe -nologo -manifest \[$]@.manifest -outputresource:\[$]@\;2 ; fi" - VC_MANIFEST_EMBED_EXE="if test -f \[$]@.manifest ; then mt.exe -nologo -manifest \[$]@.manifest -outputresource:\[$]@\;1 ; fi" - MAKE_SHARED_LIB="${MAKE_SHARED_LIB} ; ${VC_MANIFEST_EMBED_DLL}" - TEA_ADD_CLEANFILES([*.manifest]) - ]) - MAKE_STUB_LIB="\${STLIB_LD} -nodefaultlib -out:\[$]@ \$(PKG_STUB_OBJECTS)" - else - MAKE_STATIC_LIB="\${STLIB_LD} \[$]@ \$(PKG_OBJECTS)" - MAKE_SHARED_LIB="\${SHLIB_LD} -o \[$]@ \$(PKG_OBJECTS) \${SHLIB_LD_LIBS}" - MAKE_STUB_LIB="\${STLIB_LD} \[$]@ \$(PKG_STUB_OBJECTS)" - fi - - if test "${SHARED_BUILD}" = "1" ; then - MAKE_LIB="${MAKE_SHARED_LIB} " - else - MAKE_LIB="${MAKE_STATIC_LIB} " - fi - - #-------------------------------------------------------------------- - # Shared libraries and static libraries have different names. - # Use the double eval to make sure any variables in the suffix is - # substituted. (@@@ Might not be necessary anymore) - #-------------------------------------------------------------------- - - if test "${TEA_PLATFORM}" = "windows" ; then - if test "${SHARED_BUILD}" = "1" ; then - # We force the unresolved linking of symbols that are really in - # the private libraries of Tcl and Tk. - if test x"${TK_BIN_DIR}" != x ; then - SHLIB_LD_LIBS="${SHLIB_LD_LIBS} \"`${CYGPATH} ${TK_BIN_DIR}/${TK_STUB_LIB_FILE}`\"" - fi - SHLIB_LD_LIBS="${SHLIB_LD_LIBS} \"`${CYGPATH} ${TCL_BIN_DIR}/${TCL_STUB_LIB_FILE}`\"" - if test "$GCC" = "yes"; then - SHLIB_LD_LIBS="${SHLIB_LD_LIBS} -static-libgcc" - fi - eval eval "PKG_LIB_FILE=${PACKAGE_NAME}${SHARED_LIB_SUFFIX}" - else - eval eval "PKG_LIB_FILE=${PACKAGE_NAME}${UNSHARED_LIB_SUFFIX}" - if test "$GCC" = "yes"; then - PKG_LIB_FILE=lib${PKG_LIB_FILE} - fi - fi - # Some packages build their own stubs libraries - eval eval "PKG_STUB_LIB_FILE=${PACKAGE_NAME}stub${UNSHARED_LIB_SUFFIX}" - if test "$GCC" = "yes"; then - PKG_STUB_LIB_FILE=lib${PKG_STUB_LIB_FILE} - fi - # These aren't needed on Windows (either MSVC or gcc) - RANLIB=: - RANLIB_STUB=: - else - RANLIB_STUB="${RANLIB}" - if test "${SHARED_BUILD}" = "1" ; then - SHLIB_LD_LIBS="${SHLIB_LD_LIBS} ${TCL_STUB_LIB_SPEC}" - if test x"${TK_BIN_DIR}" != x ; then - SHLIB_LD_LIBS="${SHLIB_LD_LIBS} ${TK_STUB_LIB_SPEC}" - fi - eval eval "PKG_LIB_FILE=lib${PACKAGE_NAME}${SHARED_LIB_SUFFIX}" - RANLIB=: - else - eval eval "PKG_LIB_FILE=lib${PACKAGE_NAME}${UNSHARED_LIB_SUFFIX}" - fi - # Some packages build their own stubs libraries - eval eval "PKG_STUB_LIB_FILE=lib${PACKAGE_NAME}stub${UNSHARED_LIB_SUFFIX}" - fi - - # These are escaped so that only CFLAGS is picked up at configure time. - # The other values will be substituted at make time. - CFLAGS="${CFLAGS} \${CFLAGS_DEFAULT} \${CFLAGS_WARNING}" - if test "${SHARED_BUILD}" = "1" ; then - CFLAGS="${CFLAGS} \${SHLIB_CFLAGS}" - fi - - AC_SUBST(MAKE_LIB) - AC_SUBST(MAKE_SHARED_LIB) - AC_SUBST(MAKE_STATIC_LIB) - AC_SUBST(MAKE_STUB_LIB) - AC_SUBST(RANLIB_STUB) - AC_SUBST(VC_MANIFEST_EMBED_DLL) - AC_SUBST(VC_MANIFEST_EMBED_EXE) -]) - -#------------------------------------------------------------------------ -# TEA_LIB_SPEC -- -# -# Compute the name of an existing object library located in libdir -# from the given base name and produce the appropriate linker flags. -# -# Arguments: -# basename The base name of the library without version -# numbers, extensions, or "lib" prefixes. -# extra_dir Extra directory in which to search for the -# library. This location is used first, then -# $prefix/$exec-prefix, then some defaults. -# -# Requires: -# TEA_INIT and TEA_PREFIX must be called first. -# -# Results: -# -# Defines the following vars: -# ${basename}_LIB_NAME The computed library name. -# ${basename}_LIB_SPEC The computed linker flags. -#------------------------------------------------------------------------ - -AC_DEFUN([TEA_LIB_SPEC], [ - AC_MSG_CHECKING([for $1 library]) - - # Look in exec-prefix for the library (defined by TEA_PREFIX). - - tea_lib_name_dir="${exec_prefix}/lib" - - # Or in a user-specified location. - - if test x"$2" != x ; then - tea_extra_lib_dir=$2 - else - tea_extra_lib_dir=NONE - fi - - for i in \ - `ls -dr ${tea_extra_lib_dir}/$1[[0-9]]*.lib 2>/dev/null ` \ - `ls -dr ${tea_extra_lib_dir}/lib$1[[0-9]]* 2>/dev/null ` \ - `ls -dr ${tea_lib_name_dir}/$1[[0-9]]*.lib 2>/dev/null ` \ - `ls -dr ${tea_lib_name_dir}/lib$1[[0-9]]* 2>/dev/null ` \ - `ls -dr /usr/lib/$1[[0-9]]*.lib 2>/dev/null ` \ - `ls -dr /usr/lib/lib$1[[0-9]]* 2>/dev/null ` \ - `ls -dr /usr/lib64/$1[[0-9]]*.lib 2>/dev/null ` \ - `ls -dr /usr/lib64/lib$1[[0-9]]* 2>/dev/null ` \ - `ls -dr /usr/local/lib/$1[[0-9]]*.lib 2>/dev/null ` \ - `ls -dr /usr/local/lib/lib$1[[0-9]]* 2>/dev/null ` ; do - if test -f "$i" ; then - tea_lib_name_dir=`dirname $i` - $1_LIB_NAME=`basename $i` - $1_LIB_PATH_NAME=$i - break - fi - done - - if test "${TEA_PLATFORM}" = "windows"; then - $1_LIB_SPEC=\"`${CYGPATH} ${$1_LIB_PATH_NAME} 2>/dev/null`\" - else - # Strip off the leading "lib" and trailing ".a" or ".so" - - tea_lib_name_lib=`echo ${$1_LIB_NAME}|sed -e 's/^lib//' -e 's/\.[[^.]]*$//' -e 's/\.so.*//'` - $1_LIB_SPEC="-L${tea_lib_name_dir} -l${tea_lib_name_lib}" - fi - - if test "x${$1_LIB_NAME}" = x ; then - AC_MSG_ERROR([not found]) - else - AC_MSG_RESULT([${$1_LIB_SPEC}]) - fi -]) - -#------------------------------------------------------------------------ -# TEA_PRIVATE_TCL_HEADERS -- -# -# Locate the private Tcl include files -# -# Arguments: -# -# Requires: -# TCL_SRC_DIR Assumes that TEA_LOAD_TCLCONFIG has -# already been called. -# -# Results: -# -# Substitutes the following vars: -# TCL_TOP_DIR_NATIVE -# TCL_INCLUDES -#------------------------------------------------------------------------ - -AC_DEFUN([TEA_PRIVATE_TCL_HEADERS], [ - # Allow for --with-tclinclude to take effect and define ${ac_cv_c_tclh} - AC_REQUIRE([TEA_PUBLIC_TCL_HEADERS]) - AC_MSG_CHECKING([for Tcl private include files]) - - TCL_SRC_DIR_NATIVE=`${CYGPATH} ${TCL_SRC_DIR}` - TCL_TOP_DIR_NATIVE=\"${TCL_SRC_DIR_NATIVE}\" - - # Check to see if tclPort.h isn't already with the public headers - # Don't look for tclInt.h because that resides with tcl.h in the core - # sources, but the Port headers are in a different directory - if test "${TEA_PLATFORM}" = "windows" -a \ - -f "${ac_cv_c_tclh}/tclWinPort.h"; then - result="private headers found with public headers" - elif test "${TEA_PLATFORM}" = "unix" -a \ - -f "${ac_cv_c_tclh}/tclUnixPort.h"; then - result="private headers found with public headers" - else - TCL_GENERIC_DIR_NATIVE=\"${TCL_SRC_DIR_NATIVE}/generic\" - if test "${TEA_PLATFORM}" = "windows"; then - TCL_PLATFORM_DIR_NATIVE=\"${TCL_SRC_DIR_NATIVE}/win\" - else - TCL_PLATFORM_DIR_NATIVE=\"${TCL_SRC_DIR_NATIVE}/unix\" - fi - # Overwrite the previous TCL_INCLUDES as this should capture both - # public and private headers in the same set. - # We want to ensure these are substituted so as not to require - # any *_NATIVE vars be defined in the Makefile - TCL_INCLUDES="-I${TCL_GENERIC_DIR_NATIVE} -I${TCL_PLATFORM_DIR_NATIVE}" - if test "`uname -s`" = "Darwin"; then - # If Tcl was built as a framework, attempt to use - # the framework's Headers and PrivateHeaders directories - case ${TCL_DEFS} in - *TCL_FRAMEWORK*) - if test -d "${TCL_BIN_DIR}/Headers" -a \ - -d "${TCL_BIN_DIR}/PrivateHeaders"; then - TCL_INCLUDES="-I\"${TCL_BIN_DIR}/Headers\" -I\"${TCL_BIN_DIR}/PrivateHeaders\" ${TCL_INCLUDES}" - else - TCL_INCLUDES="${TCL_INCLUDES} ${TCL_INCLUDE_SPEC} `echo "${TCL_INCLUDE_SPEC}" | sed -e 's/Headers/PrivateHeaders/'`" - fi - ;; - esac - result="Using ${TCL_INCLUDES}" - else - if test ! -f "${TCL_SRC_DIR}/generic/tclInt.h" ; then - AC_MSG_ERROR([Cannot find private header tclInt.h in ${TCL_SRC_DIR}]) - fi - result="Using srcdir found in tclConfig.sh: ${TCL_SRC_DIR}" - fi - fi - - AC_SUBST(TCL_TOP_DIR_NATIVE) - - AC_SUBST(TCL_INCLUDES) - AC_MSG_RESULT([${result}]) -]) - -#------------------------------------------------------------------------ -# TEA_PUBLIC_TCL_HEADERS -- -# -# Locate the installed public Tcl header files -# -# Arguments: -# None. -# -# Requires: -# CYGPATH must be set -# -# Results: -# -# Adds a --with-tclinclude switch to configure. -# Result is cached. -# -# Substitutes the following vars: -# TCL_INCLUDES -#------------------------------------------------------------------------ - -AC_DEFUN([TEA_PUBLIC_TCL_HEADERS], [ - AC_MSG_CHECKING([for Tcl public headers]) - - AC_ARG_WITH(tclinclude, [ --with-tclinclude directory containing the public Tcl header files], with_tclinclude=${withval}) - - AC_CACHE_VAL(ac_cv_c_tclh, [ - # Use the value from --with-tclinclude, if it was given - - if test x"${with_tclinclude}" != x ; then - if test -f "${with_tclinclude}/tcl.h" ; then - ac_cv_c_tclh=${with_tclinclude} - else - AC_MSG_ERROR([${with_tclinclude} directory does not contain tcl.h]) - fi - else - list="" - if test "`uname -s`" = "Darwin"; then - # If Tcl was built as a framework, attempt to use - # the framework's Headers directory - case ${TCL_DEFS} in - *TCL_FRAMEWORK*) - list="`ls -d ${TCL_BIN_DIR}/Headers 2>/dev/null`" - ;; - esac - fi - - # Look in the source dir only if Tcl is not installed, - # and in that situation, look there before installed locations. - if test -f "${TCL_BIN_DIR}/Makefile" ; then - list="$list `ls -d ${TCL_SRC_DIR}/generic 2>/dev/null`" - fi - - # Check order: pkg --prefix location, Tcl's --prefix location, - # relative to directory of tclConfig.sh. - - eval "temp_includedir=${includedir}" - list="$list \ - `ls -d ${temp_includedir} 2>/dev/null` \ - `ls -d ${TCL_PREFIX}/include 2>/dev/null` \ - `ls -d ${TCL_BIN_DIR}/../include 2>/dev/null`" - if test "${TEA_PLATFORM}" != "windows" -o "$GCC" = "yes"; then - list="$list /usr/local/include /usr/include" - if test x"${TCL_INCLUDE_SPEC}" != x ; then - d=`echo "${TCL_INCLUDE_SPEC}" | sed -e 's/^-I//'` - list="$list `ls -d ${d} 2>/dev/null`" - fi - fi - for i in $list ; do - if test -f "$i/tcl.h" ; then - ac_cv_c_tclh=$i - break - fi - done - fi - ]) - - # Print a message based on how we determined the include path - - if test x"${ac_cv_c_tclh}" = x ; then - AC_MSG_ERROR([tcl.h not found. Please specify its location with --with-tclinclude]) - else - AC_MSG_RESULT([${ac_cv_c_tclh}]) - fi - - # Convert to a native path and substitute into the output files. - - INCLUDE_DIR_NATIVE=`${CYGPATH} ${ac_cv_c_tclh}` - - TCL_INCLUDES=-I\"${INCLUDE_DIR_NATIVE}\" - - AC_SUBST(TCL_INCLUDES) -]) - -#------------------------------------------------------------------------ -# TEA_PRIVATE_TK_HEADERS -- -# -# Locate the private Tk include files -# -# Arguments: -# -# Requires: -# TK_SRC_DIR Assumes that TEA_LOAD_TKCONFIG has -# already been called. -# -# Results: -# -# Substitutes the following vars: -# TK_INCLUDES -#------------------------------------------------------------------------ - -AC_DEFUN([TEA_PRIVATE_TK_HEADERS], [ - # Allow for --with-tkinclude to take effect and define ${ac_cv_c_tkh} - AC_REQUIRE([TEA_PUBLIC_TK_HEADERS]) - AC_MSG_CHECKING([for Tk private include files]) - - TK_SRC_DIR_NATIVE=`${CYGPATH} ${TK_SRC_DIR}` - TK_TOP_DIR_NATIVE=\"${TK_SRC_DIR_NATIVE}\" - - # Check to see if tkPort.h isn't already with the public headers - # Don't look for tkInt.h because that resides with tk.h in the core - # sources, but the Port headers are in a different directory - if test "${TEA_PLATFORM}" = "windows" -a \ - -f "${ac_cv_c_tkh}/tkWinPort.h"; then - result="private headers found with public headers" - elif test "${TEA_PLATFORM}" = "unix" -a \ - -f "${ac_cv_c_tkh}/tkUnixPort.h"; then - result="private headers found with public headers" - else - TK_GENERIC_DIR_NATIVE=\"${TK_SRC_DIR_NATIVE}/generic\" - TK_XLIB_DIR_NATIVE=\"${TK_SRC_DIR_NATIVE}/xlib\" - if test "${TEA_PLATFORM}" = "windows"; then - TK_PLATFORM_DIR_NATIVE=\"${TK_SRC_DIR_NATIVE}/win\" - else - TK_PLATFORM_DIR_NATIVE=\"${TK_SRC_DIR_NATIVE}/unix\" - fi - # Overwrite the previous TK_INCLUDES as this should capture both - # public and private headers in the same set. - # We want to ensure these are substituted so as not to require - # any *_NATIVE vars be defined in the Makefile - TK_INCLUDES="-I${TK_GENERIC_DIR_NATIVE} -I${TK_PLATFORM_DIR_NATIVE}" - # Detect and add ttk subdir - if test -d "${TK_SRC_DIR}/generic/ttk"; then - TK_INCLUDES="${TK_INCLUDES} -I\"${TK_SRC_DIR_NATIVE}/generic/ttk\"" - fi - if test "${TEA_WINDOWINGSYSTEM}" != "x11"; then - TK_INCLUDES="${TK_INCLUDES} -I\"${TK_XLIB_DIR_NATIVE}\"" - fi - if test "${TEA_WINDOWINGSYSTEM}" = "aqua"; then - TK_INCLUDES="${TK_INCLUDES} -I\"${TK_SRC_DIR_NATIVE}/macosx\"" - fi - if test "`uname -s`" = "Darwin"; then - # If Tk was built as a framework, attempt to use - # the framework's Headers and PrivateHeaders directories - case ${TK_DEFS} in - *TK_FRAMEWORK*) - if test -d "${TK_BIN_DIR}/Headers" -a \ - -d "${TK_BIN_DIR}/PrivateHeaders"; then - TK_INCLUDES="-I\"${TK_BIN_DIR}/Headers\" -I\"${TK_BIN_DIR}/PrivateHeaders\" ${TK_INCLUDES}" - else - TK_INCLUDES="${TK_INCLUDES} ${TK_INCLUDE_SPEC} `echo "${TK_INCLUDE_SPEC}" | sed -e 's/Headers/PrivateHeaders/'`" - fi - ;; - esac - result="Using ${TK_INCLUDES}" - else - if test ! -f "${TK_SRC_DIR}/generic/tkInt.h" ; then - AC_MSG_ERROR([Cannot find private header tkInt.h in ${TK_SRC_DIR}]) - fi - result="Using srcdir found in tkConfig.sh: ${TK_SRC_DIR}" - fi - fi - - AC_SUBST(TK_TOP_DIR_NATIVE) - AC_SUBST(TK_XLIB_DIR_NATIVE) - - AC_SUBST(TK_INCLUDES) - AC_MSG_RESULT([${result}]) -]) - -#------------------------------------------------------------------------ -# TEA_PUBLIC_TK_HEADERS -- -# -# Locate the installed public Tk header files -# -# Arguments: -# None. -# -# Requires: -# CYGPATH must be set -# -# Results: -# -# Adds a --with-tkinclude switch to configure. -# Result is cached. -# -# Substitutes the following vars: -# TK_INCLUDES -#------------------------------------------------------------------------ - -AC_DEFUN([TEA_PUBLIC_TK_HEADERS], [ - AC_MSG_CHECKING([for Tk public headers]) - - AC_ARG_WITH(tkinclude, [ --with-tkinclude directory containing the public Tk header files], with_tkinclude=${withval}) - - AC_CACHE_VAL(ac_cv_c_tkh, [ - # Use the value from --with-tkinclude, if it was given - - if test x"${with_tkinclude}" != x ; then - if test -f "${with_tkinclude}/tk.h" ; then - ac_cv_c_tkh=${with_tkinclude} - else - AC_MSG_ERROR([${with_tkinclude} directory does not contain tk.h]) - fi - else - list="" - if test "`uname -s`" = "Darwin"; then - # If Tk was built as a framework, attempt to use - # the framework's Headers directory. - case ${TK_DEFS} in - *TK_FRAMEWORK*) - list="`ls -d ${TK_BIN_DIR}/Headers 2>/dev/null`" - ;; - esac - fi - - # Look in the source dir only if Tk is not installed, - # and in that situation, look there before installed locations. - if test -f "${TK_BIN_DIR}/Makefile" ; then - list="$list `ls -d ${TK_SRC_DIR}/generic 2>/dev/null`" - fi - - # Check order: pkg --prefix location, Tk's --prefix location, - # relative to directory of tkConfig.sh, Tcl's --prefix location, - # relative to directory of tclConfig.sh. - - eval "temp_includedir=${includedir}" - list="$list \ - `ls -d ${temp_includedir} 2>/dev/null` \ - `ls -d ${TK_PREFIX}/include 2>/dev/null` \ - `ls -d ${TK_BIN_DIR}/../include 2>/dev/null` \ - `ls -d ${TCL_PREFIX}/include 2>/dev/null` \ - `ls -d ${TCL_BIN_DIR}/../include 2>/dev/null`" - if test "${TEA_PLATFORM}" != "windows" -o "$GCC" = "yes"; then - list="$list /usr/local/include /usr/include" - if test x"${TK_INCLUDE_SPEC}" != x ; then - d=`echo "${TK_INCLUDE_SPEC}" | sed -e 's/^-I//'` - list="$list `ls -d ${d} 2>/dev/null`" - fi - fi - for i in $list ; do - if test -f "$i/tk.h" ; then - ac_cv_c_tkh=$i - break - fi - done - fi - ]) - - # Print a message based on how we determined the include path - - if test x"${ac_cv_c_tkh}" = x ; then - AC_MSG_ERROR([tk.h not found. Please specify its location with --with-tkinclude]) - else - AC_MSG_RESULT([${ac_cv_c_tkh}]) - fi - - # Convert to a native path and substitute into the output files. - - INCLUDE_DIR_NATIVE=`${CYGPATH} ${ac_cv_c_tkh}` - - TK_INCLUDES=-I\"${INCLUDE_DIR_NATIVE}\" - - AC_SUBST(TK_INCLUDES) - - if test "${TEA_WINDOWINGSYSTEM}" != "x11"; then - # On Windows and Aqua, we need the X compat headers - AC_MSG_CHECKING([for X11 header files]) - if test ! -r "${INCLUDE_DIR_NATIVE}/X11/Xlib.h"; then - INCLUDE_DIR_NATIVE="`${CYGPATH} ${TK_SRC_DIR}/xlib`" - TK_XINCLUDES=-I\"${INCLUDE_DIR_NATIVE}\" - AC_SUBST(TK_XINCLUDES) - fi - AC_MSG_RESULT([${INCLUDE_DIR_NATIVE}]) - fi -]) - -#------------------------------------------------------------------------ -# TEA_PATH_CONFIG -- -# -# Locate the ${1}Config.sh file and perform a sanity check on -# the ${1} compile flags. These are used by packages like -# [incr Tk] that load *Config.sh files from more than Tcl and Tk. -# -# Arguments: -# none -# -# Results: -# -# Adds the following arguments to configure: -# --with-$1=... -# -# Defines the following vars: -# $1_BIN_DIR Full path to the directory containing -# the $1Config.sh file -#------------------------------------------------------------------------ - -AC_DEFUN([TEA_PATH_CONFIG], [ - # - # Ok, lets find the $1 configuration - # First, look for one uninstalled. - # the alternative search directory is invoked by --with-$1 - # - - if test x"${no_$1}" = x ; then - # we reset no_$1 in case something fails here - no_$1=true - AC_ARG_WITH($1, [ --with-$1 directory containing $1 configuration ($1Config.sh)], with_$1config=${withval}) - AC_MSG_CHECKING([for $1 configuration]) - AC_CACHE_VAL(ac_cv_c_$1config,[ - - # First check to see if --with-$1 was specified. - if test x"${with_$1config}" != x ; then - case ${with_$1config} in - */$1Config.sh ) - if test -f ${with_$1config}; then - AC_MSG_WARN([--with-$1 argument should refer to directory containing $1Config.sh, not to $1Config.sh itself]) - with_$1config=`echo ${with_$1config} | sed 's!/$1Config\.sh$!!'` - fi;; - esac - if test -f "${with_$1config}/$1Config.sh" ; then - ac_cv_c_$1config=`(cd ${with_$1config}; pwd)` - else - AC_MSG_ERROR([${with_$1config} directory doesn't contain $1Config.sh]) - fi - fi - - # then check for a private $1 installation - if test x"${ac_cv_c_$1config}" = x ; then - for i in \ - ../$1 \ - `ls -dr ../$1*[[0-9]].[[0-9]]*.[[0-9]]* 2>/dev/null` \ - `ls -dr ../$1*[[0-9]].[[0-9]][[0-9]] 2>/dev/null` \ - `ls -dr ../$1*[[0-9]].[[0-9]] 2>/dev/null` \ - `ls -dr ../$1*[[0-9]].[[0-9]]* 2>/dev/null` \ - ../../$1 \ - `ls -dr ../../$1*[[0-9]].[[0-9]]*.[[0-9]]* 2>/dev/null` \ - `ls -dr ../../$1*[[0-9]].[[0-9]][[0-9]] 2>/dev/null` \ - `ls -dr ../../$1*[[0-9]].[[0-9]] 2>/dev/null` \ - `ls -dr ../../$1*[[0-9]].[[0-9]]* 2>/dev/null` \ - ../../../$1 \ - `ls -dr ../../../$1*[[0-9]].[[0-9]]*.[[0-9]]* 2>/dev/null` \ - `ls -dr ../../../$1*[[0-9]].[[0-9]][[0-9]] 2>/dev/null` \ - `ls -dr ../../../$1*[[0-9]].[[0-9]] 2>/dev/null` \ - `ls -dr ../../../$1*[[0-9]].[[0-9]]* 2>/dev/null` \ - ${srcdir}/../$1 \ - `ls -dr ${srcdir}/../$1*[[0-9]].[[0-9]]*.[[0-9]]* 2>/dev/null` \ - `ls -dr ${srcdir}/../$1*[[0-9]].[[0-9]][[0-9]] 2>/dev/null` \ - `ls -dr ${srcdir}/../$1*[[0-9]].[[0-9]] 2>/dev/null` \ - `ls -dr ${srcdir}/../$1*[[0-9]].[[0-9]]* 2>/dev/null` \ - ; do - if test -f "$i/$1Config.sh" ; then - ac_cv_c_$1config=`(cd $i; pwd)` - break - fi - if test -f "$i/unix/$1Config.sh" ; then - ac_cv_c_$1config=`(cd $i/unix; pwd)` - break - fi - done - fi - - # check in a few common install locations - if test x"${ac_cv_c_$1config}" = x ; then - for i in `ls -d ${libdir} 2>/dev/null` \ - `ls -d ${exec_prefix}/lib 2>/dev/null` \ - `ls -d ${prefix}/lib 2>/dev/null` \ - `ls -d /usr/local/lib 2>/dev/null` \ - `ls -d /usr/contrib/lib 2>/dev/null` \ - `ls -d /usr/lib 2>/dev/null` \ - `ls -d /usr/lib64 2>/dev/null` \ - ; do - if test -f "$i/$1Config.sh" ; then - ac_cv_c_$1config=`(cd $i; pwd)` - break - fi - done - fi - ]) - - if test x"${ac_cv_c_$1config}" = x ; then - $1_BIN_DIR="# no $1 configs found" - AC_MSG_WARN([Cannot find $1 configuration definitions]) - exit 0 - else - no_$1= - $1_BIN_DIR=${ac_cv_c_$1config} - AC_MSG_RESULT([found $$1_BIN_DIR/$1Config.sh]) - fi - fi -]) - -#------------------------------------------------------------------------ -# TEA_LOAD_CONFIG -- -# -# Load the $1Config.sh file -# -# Arguments: -# -# Requires the following vars to be set: -# $1_BIN_DIR -# -# Results: -# -# Substitutes the following vars: -# $1_SRC_DIR -# $1_LIB_FILE -# $1_LIB_SPEC -#------------------------------------------------------------------------ - -AC_DEFUN([TEA_LOAD_CONFIG], [ - AC_MSG_CHECKING([for existence of ${$1_BIN_DIR}/$1Config.sh]) - - if test -f "${$1_BIN_DIR}/$1Config.sh" ; then - AC_MSG_RESULT([loading]) - . "${$1_BIN_DIR}/$1Config.sh" - else - AC_MSG_RESULT([file not found]) - fi - - # - # If the $1_BIN_DIR is the build directory (not the install directory), - # then set the common variable name to the value of the build variables. - # For example, the variable $1_LIB_SPEC will be set to the value - # of $1_BUILD_LIB_SPEC. An extension should make use of $1_LIB_SPEC - # instead of $1_BUILD_LIB_SPEC since it will work with both an - # installed and uninstalled version of Tcl. - # - - if test -f "${$1_BIN_DIR}/Makefile" ; then - AC_MSG_WARN([Found Makefile - using build library specs for $1]) - $1_LIB_SPEC=${$1_BUILD_LIB_SPEC} - $1_STUB_LIB_SPEC=${$1_BUILD_STUB_LIB_SPEC} - $1_STUB_LIB_PATH=${$1_BUILD_STUB_LIB_PATH} - $1_INCLUDE_SPEC=${$1_BUILD_INCLUDE_SPEC} - $1_LIBRARY_PATH=${$1_LIBRARY_PATH} - fi - - AC_SUBST($1_VERSION) - AC_SUBST($1_BIN_DIR) - AC_SUBST($1_SRC_DIR) - - AC_SUBST($1_LIB_FILE) - AC_SUBST($1_LIB_SPEC) - - AC_SUBST($1_STUB_LIB_FILE) - AC_SUBST($1_STUB_LIB_SPEC) - AC_SUBST($1_STUB_LIB_PATH) - - # Allow the caller to prevent this auto-check by specifying any 2nd arg - AS_IF([test "x$2" = x], [ - # Check both upper and lower-case variants - # If a dev wanted non-stubs libs, this function could take an option - # to not use _STUB in the paths below - AS_IF([test "x${$1_STUB_LIB_SPEC}" = x], - [TEA_LOAD_CONFIG_LIB(translit($1,[a-z],[A-Z])_STUB)], - [TEA_LOAD_CONFIG_LIB($1_STUB)]) - ]) -]) - -#------------------------------------------------------------------------ -# TEA_LOAD_CONFIG_LIB -- -# -# Helper function to load correct library from another extension's -# ${PACKAGE}Config.sh. -# -# Results: -# Adds to LIBS the appropriate extension library -#------------------------------------------------------------------------ -AC_DEFUN([TEA_LOAD_CONFIG_LIB], [ - AC_MSG_CHECKING([For $1 library for LIBS]) - # This simplifies the use of stub libraries by automatically adding - # the stub lib to your path. Normally this would add to SHLIB_LD_LIBS, - # but this is called before CONFIG_CFLAGS. More importantly, this adds - # to PKG_LIBS, which becomes LIBS, and that is only used by SHLIB_LD. - if test "x${$1_LIB_SPEC}" != "x" ; then - if test "${TEA_PLATFORM}" = "windows" -a "$GCC" != "yes" ; then - TEA_ADD_LIBS([\"`${CYGPATH} ${$1_LIB_PATH}`\"]) - AC_MSG_RESULT([using $1_LIB_PATH ${$1_LIB_PATH}]) - else - TEA_ADD_LIBS([${$1_LIB_SPEC}]) - AC_MSG_RESULT([using $1_LIB_SPEC ${$1_LIB_SPEC}]) - fi - else - AC_MSG_RESULT([file not found]) - fi -]) - -#------------------------------------------------------------------------ -# TEA_EXPORT_CONFIG -- -# -# Define the data to insert into the ${PACKAGE}Config.sh file -# -# Arguments: -# -# Requires the following vars to be set: -# $1 -# -# Results: -# Substitutes the following vars: -#------------------------------------------------------------------------ - -AC_DEFUN([TEA_EXPORT_CONFIG], [ - #-------------------------------------------------------------------- - # These are for $1Config.sh - #-------------------------------------------------------------------- - - # pkglibdir must be a fully qualified path and (not ${exec_prefix}/lib) - eval pkglibdir="[$]{libdir}/$1${PACKAGE_VERSION}" - if test "${TCL_LIB_VERSIONS_OK}" = "ok"; then - eval $1_LIB_FLAG="-l$1${PACKAGE_VERSION}${DBGX}" - eval $1_STUB_LIB_FLAG="-l$1stub${PACKAGE_VERSION}${DBGX}" - else - eval $1_LIB_FLAG="-l$1`echo ${PACKAGE_VERSION} | tr -d .`${DBGX}" - eval $1_STUB_LIB_FLAG="-l$1stub`echo ${PACKAGE_VERSION} | tr -d .`${DBGX}" - fi - $1_BUILD_LIB_SPEC="-L`pwd` ${$1_LIB_FLAG}" - $1_LIB_SPEC="-L${pkglibdir} ${$1_LIB_FLAG}" - $1_BUILD_STUB_LIB_SPEC="-L`pwd` [$]{$1_STUB_LIB_FLAG}" - $1_STUB_LIB_SPEC="-L${pkglibdir} [$]{$1_STUB_LIB_FLAG}" - $1_BUILD_STUB_LIB_PATH="`pwd`/[$]{PKG_STUB_LIB_FILE}" - $1_STUB_LIB_PATH="${pkglibdir}/[$]{PKG_STUB_LIB_FILE}" - - AC_SUBST($1_BUILD_LIB_SPEC) - AC_SUBST($1_LIB_SPEC) - AC_SUBST($1_BUILD_STUB_LIB_SPEC) - AC_SUBST($1_STUB_LIB_SPEC) - AC_SUBST($1_BUILD_STUB_LIB_PATH) - AC_SUBST($1_STUB_LIB_PATH) - - AC_SUBST(MAJOR_VERSION) - AC_SUBST(MINOR_VERSION) - AC_SUBST(PATCHLEVEL) -]) - - -#------------------------------------------------------------------------ -# TEA_PATH_CELIB -- -# -# Locate Keuchel's celib emulation layer for targeting Win/CE -# -# Arguments: -# none -# -# Results: -# -# Adds the following arguments to configure: -# --with-celib=... -# -# Defines the following vars: -# CELIB_DIR Full path to the directory containing -# the include and platform lib files -#------------------------------------------------------------------------ - -AC_DEFUN([TEA_PATH_CELIB], [ - # First, look for one uninstalled. - # the alternative search directory is invoked by --with-celib - - if test x"${no_celib}" = x ; then - # we reset no_celib in case something fails here - no_celib=true - AC_ARG_WITH(celib,[ --with-celib=DIR use Windows/CE support library from DIR], with_celibconfig=${withval}) - AC_MSG_CHECKING([for Windows/CE celib directory]) - AC_CACHE_VAL(ac_cv_c_celibconfig,[ - # First check to see if --with-celibconfig was specified. - if test x"${with_celibconfig}" != x ; then - if test -d "${with_celibconfig}/inc" ; then - ac_cv_c_celibconfig=`(cd ${with_celibconfig}; pwd)` - else - AC_MSG_ERROR([${with_celibconfig} directory doesn't contain inc directory]) - fi - fi - - # then check for a celib library - if test x"${ac_cv_c_celibconfig}" = x ; then - for i in \ - ../celib-palm-3.0 \ - ../celib \ - ../../celib-palm-3.0 \ - ../../celib \ - `ls -dr ../celib-*3.[[0-9]]* 2>/dev/null` \ - ${srcdir}/../celib-palm-3.0 \ - ${srcdir}/../celib \ - `ls -dr ${srcdir}/../celib-*3.[[0-9]]* 2>/dev/null` \ - ; do - if test -d "$i/inc" ; then - ac_cv_c_celibconfig=`(cd $i; pwd)` - break - fi - done - fi - ]) - if test x"${ac_cv_c_celibconfig}" = x ; then - AC_MSG_ERROR([Cannot find celib support library directory]) - else - no_celib= - CELIB_DIR=${ac_cv_c_celibconfig} - CELIB_DIR=`echo "$CELIB_DIR" | sed -e 's!\\\!/!g'` - AC_MSG_RESULT([found $CELIB_DIR]) - fi - fi -]) -# Local Variables: -# mode: autoconf -# End: diff --git a/autoconf/tea/teaish.tcl b/autoconf/tea/teaish.tcl new file mode 100644 index 0000000000..47e0ea7013 --- /dev/null +++ b/autoconf/tea/teaish.tcl @@ -0,0 +1,569 @@ +# Teaish configure script for the SQLite Tcl extension + +# +# State for disparate config-time pieces. +# +array set sqlite__Config [proj-strip-hash-comments { + # + # The list of feature --flags which the --all flag implies. This + # requires special handling in a few places. + # + all-flag-enables {fts3 fts4 fts5 rtree geopoly} + + # >0 if building in the canonical tree. -1=undetermined + is-canonical -1 +}] + +# +# Set up the package info for teaish... +# +apply {{dir} { + # Figure out the version number... + set version "" + if {[file exists $dir/../VERSION]} { + # The canonical SQLite TEA(ish) build + set version [proj-file-content -trim $dir/../VERSION] + set ::sqlite__Config(is-canonical) 1 + set distname sqlite-tcl + } elseif {[file exists $dir/generic/tclsqlite3.c]} { + # The copy from the teaish tree, used as a dev/test bed before + # updating SQLite's tree. + set ::sqlite__Config(is-canonical) 0 + set fd [open $dir/generic/tclsqlite3.c rb] + while {[gets $fd line] >=0} { + if {[regexp {^#define[ ]+SQLITE_VERSION[ ]+"(3.+)"} \ + $line - version]} { + set distname sqlite-teaish + break + } + } + close $fd + } + + if {"" eq $version} { + proj-fatal "Cannot determine the SQLite version number" + } + + proj-assert {$::sqlite__Config(is-canonical) > -1} + proj-assert {[string match 3.*.* $version]} \ + "Unexpected SQLite version: $version" + + set pragmas {} + if {$::sqlite__Config(is-canonical)} { + # Disable "make dist" in the canonical tree. That tree is + # generated from several pieces and creating/testing working + # "dist" rules for that sub-build currently feels unnecessary. The + # copy in the teaish tree, though, should be able to "make dist". + lappend pragmas no-dist + } else { + lappend pragmas full-dist + } + + teaish-pkginfo-set -vars { + -name sqlite + -name.pkg sqlite3 + -version $version + -name.dist $distname + -libDir sqlite$version + -pragmas $pragmas + -src generic/tclsqlite3.c + } + # We should also have: + # -vsatisfies 8.6- + # But at least one platform is failing this vsatisfies check + # for no apparent reason: + # https://sqlite.org/forum/forumpost/fde857fb8101a4be +}} [teaish-get -dir] + + +# +# Must return either an empty string or a list in the form accepted by +# autosetup's [options] function. +# +proc teaish-options {} { + # These flags and defaults mostly derive from the historical TEA + # build. Some, like ICU, are taken from the canonical SQLite tree. + return [subst -nocommands -nobackslashes { + with-system-sqlite=0 + => {Use the system-level SQLite instead of the copy in this tree. + Also requires use of --override-sqlite-version so that the build + knows what version number to associate with the system-level SQLite.} + override-sqlite-version:VERSION + => {For use with --with-system-sqlite to set the version number.} + threadsafe=1 => {Disable mutexing} + with-tempstore:=no => {Use an in-RAM database for temporary tables: never,no,yes,always} + load-extension=0 => {Enable loading of external extensions} + math=1 => {Disable math functions} + json=1 => {Disable JSON functions} + fts3 => {Enable the FTS3 extension} + fts4 => {Enable the FTS4 extension} + fts5 => {Enable the FTS5 extension} + update-limit => {Enable the UPDATE/DELETE LIMIT clause} + geopoly => {Enable the GEOPOLY extension} + rtree => {Enable the RTREE extension} + session => {Enable the SESSION extension} + all=1 => {Disable $::sqlite__Config(all-flag-enables)} + with-icu-ldflags:LDFLAGS + => {Enable SQLITE_ENABLE_ICU and add the given linker flags for the + ICU libraries. e.g. on Ubuntu systems, try '-licui18n -licuuc -licudata'.} + with-icu-cflags:CFLAGS + => {Apply extra CFLAGS/CPPFLAGS necessary for building with ICU. + e.g. -I/usr/local/include} + with-icu-config:=auto + => {Enable SQLITE_ENABLE_ICU. Value must be one of: auto, pkg-config, + /path/to/icu-config} + icu-collations=0 + => {Enable SQLITE_ENABLE_ICU_COLLATIONS. Requires --with-icu-ldflags=... + or --with-icu-config} + }] +} + +# +# Gets called by tea-configure-core. Must perform any configuration +# work needed for this extension. +# +proc teaish-configure {} { + use teaish/feature + + if {[proj-opt-was-provided override-sqlite-version]} { + teaish-pkginfo-set -version [opt-val override-sqlite-version] + proj-warn "overriding sqlite version number:" [teaish-pkginfo-get -version] + } elseif {[proj-opt-was-provided with-system-sqlite] + && [opt-val with-system-sqlite] ne "0"} { + proj-fatal "when using --with-system-sqlite also use" \ + "--override-sqlite-version to specify a library version number." + } + + define CFLAGS [proj-get-env CFLAGS {-O2}] + sqlite-munge-cflags + + # + # Add feature flags from legacy configure.ac which are not covered by + # --flags. + # + sqlite-add-feature-flag { + -DSQLITE_3_SUFFIX_ONLY=1 + -DSQLITE_ENABLE_DESERIALIZE=1 + -DSQLITE_ENABLE_DBPAGE_VTAB=1 + -DSQLITE_ENABLE_BYTECODE_VTAB=1 + -DSQLITE_ENABLE_DBSTAT_VTAB=1 + } + + if {[opt-bool with-system-sqlite]} { + msg-result "Using system-level sqlite3." + teaish-cflags-add -DUSE_SYSTEM_SQLITE + teaish-ldflags-add -lsqlite3 + } elseif {$::sqlite__Config(is-canonical)} { + teaish-cflags-add -I[teaish-get -dir]/.. + } + + teaish-check-librt + teaish-check-libz + sqlite-handle-threadsafe + sqlite-handle-tempstore + sqlite-handle-load-extension + sqlite-handle-math + sqlite-handle-icu + + sqlite-handle-common-feature-flags; # must be late in the process +}; # teaish-configure + +define OPT_FEATURE_FLAGS {} ; # -DSQLITE_OMIT/ENABLE flags. +# +# Adds $args, if not empty, to OPT_FEATURE_FLAGS. This is intended only for holding +# -DSQLITE_ENABLE/OMIT/... flags, but that is not enforced here. +proc sqlite-add-feature-flag {args} { + if {"" ne $args} { + define-append OPT_FEATURE_FLAGS {*}$args + } +} + +# +# Check for log(3) in libm and die with an error if it is not +# found. $featureName should be the feature name which requires that +# function (it's used only in error messages). defines LDFLAGS_MATH to +# the required linker flags (which may be empty even if the math APIs +# are found, depending on the OS). +proc sqlite-affirm-have-math {featureName} { + if {"" eq [get-define LDFLAGS_MATH ""]} { + if {![msg-quiet proj-check-function-in-lib log m]} { + user-error "Missing math APIs for $featureName" + } + set lfl [get-define lib_log ""] + undefine lib_log + if {"" ne $lfl} { + user-notice "Forcing requirement of $lfl for $featureName" + } + define LDFLAGS_MATH $lfl + teaish-ldflags-prepend $lfl + } +} + +# +# Handle various SQLITE_ENABLE/OMIT_... feature flags. +proc sqlite-handle-common-feature-flags {} { + msg-result "Feature flags..." + if {![opt-bool all]} { + # Special handling for --disable-all + foreach flag $::sqlite__Config(all-flag-enables) { + if {![proj-opt-was-provided $flag]} { + proj-opt-set $flag 0 + } + } + } + foreach {boolFlag featureFlag ifSetEvalThis} [proj-strip-hash-comments { + all {} { + # The 'all' option must be first in this list. This impl makes + # an effort to only apply flags which the user did not already + # apply, so that combinations like (--all --disable-geopoly) + # will indeed disable geopoly. There are corner cases where + # flags which depend on each other will behave in non-intuitive + # ways: + # + # --all --disable-rtree + # + # Will NOT disable geopoly, though geopoly depends on rtree. + # The --geopoly flag, though, will automatically re-enable + # --rtree, so --disable-rtree won't actually disable anything in + # that case. + foreach k $::sqlite__Config(all-flag-enables) { + if {![proj-opt-was-provided $k]} { + proj-opt-set $k 1 + } + } + } + fts3 -DSQLITE_ENABLE_FTS3 {sqlite-affirm-have-math fts3} + fts4 -DSQLITE_ENABLE_FTS4 {sqlite-affirm-have-math fts4} + fts5 -DSQLITE_ENABLE_FTS5 {sqlite-affirm-have-math fts5} + geopoly -DSQLITE_ENABLE_GEOPOLY {proj-opt-set rtree} + rtree -DSQLITE_ENABLE_RTREE {} + session {-DSQLITE_ENABLE_SESSION -DSQLITE_ENABLE_PREUPDATE_HOOK} {} + update-limit -DSQLITE_ENABLE_UPDATE_DELETE_LIMIT {} + scanstatus -DSQLITE_ENABLE_STMT_SCANSTATUS {} + }] { + if {$boolFlag ni $::autosetup(options)} { + # Skip flags which are in the canonical build but not + # the autoconf bundle. + continue + } + proj-if-opt-truthy $boolFlag { + sqlite-add-feature-flag $featureFlag + if {0 != [eval $ifSetEvalThis] && "all" ne $boolFlag} { + msg-result " + $boolFlag" + } + } { + if {"all" ne $boolFlag} { + msg-result " - $boolFlag" + } + } + } + # + # Invert the above loop's logic for some SQLITE_OMIT_... cases. If + # config option $boolFlag is false, [sqlite-add-feature-flag + # $featureFlag], where $featureFlag is intended to be + # -DSQLITE_OMIT_... + foreach {boolFlag featureFlag} { + json -DSQLITE_OMIT_JSON + } { + if {[proj-opt-truthy $boolFlag]} { + msg-result " + $boolFlag" + } else { + sqlite-add-feature-flag $featureFlag + msg-result " - $boolFlag" + } + } + + ## + # Remove duplicates from the final feature flag sets and show them + # to the user. + set oFF [get-define OPT_FEATURE_FLAGS] + if {"" ne $oFF} { + define OPT_FEATURE_FLAGS [lsort -unique $oFF] + msg-result "Library feature flags: [get-define OPT_FEATURE_FLAGS]" + } + if {[lsearch [get-define TARGET_DEBUG ""] -DSQLITE_DEBUG=1] > -1} { + msg-result "Note: this is a debug build, so performance will suffer." + } + teaish-cflags-add -define OPT_FEATURE_FLAGS +}; # sqlite-handle-common-feature-flags + +# +# If --enable-threadsafe is set, this adds -DSQLITE_THREADSAFE=1 to +# OPT_FEATURE_FLAGS and sets LDFLAGS_PTHREAD to the linker flags +# needed for linking pthread (possibly an empty string). If +# --enable-threadsafe is not set, adds -DSQLITE_THREADSAFE=0 to +# OPT_FEATURE_FLAGS and sets LDFLAGS_PTHREAD to an empty string. +# +# It prepends the flags to the global LDFLAGS. +proc sqlite-handle-threadsafe {} { + msg-checking "Support threadsafe operation? " + define LDFLAGS_PTHREAD "" + set enable 0 + if {[proj-opt-was-provided threadsafe]} { + proj-if-opt-truthy threadsafe { + if {[proj-check-function-in-lib pthread_create pthread] + && [proj-check-function-in-lib pthread_mutexattr_init pthread]} { + incr enable + set ldf [get-define lib_pthread_create] + define LDFLAGS_PTHREAD $ldf + teaish-ldflags-prepend $ldf + undefine lib_pthread_create + undefine lib_pthread_mutexattr_init + } else { + user-error "Missing required pthread libraries. Use --disable-threadsafe to disable this check." + } + # Recall that LDFLAGS_PTHREAD might be empty even if pthreads if + # found because it's in -lc on some platforms. + } { + msg-result "Disabled using --disable-threadsafe" + } + } else { + # + # If user does not specify --[disable-]threadsafe then select a + # default based on whether it looks like Tcl has threading + # support. + # + catch { + scan [exec echo {puts [tcl::pkgconfig get threaded]} | [get-define TCLSH_CMD]] \ + %d enable + } + if {$enable} { + set flagName "--threadsafe" + set lblAbled "enabled" + msg-result yes + } else { + set flagName "--disable-threadsafe" + set lblAbled "disabled" + msg-result no + } + msg-result "Defaulting to ${flagName} because Tcl has threading ${lblAbled}." + # ^^^ We (probably) don't need to link against -lpthread in the + # is-enabled case. We might in the case of static linking. Unsure. + } + sqlite-add-feature-flag -DSQLITE_THREADSAFE=${enable} + return $enable +} + +# +# Handles the --enable-load-extension flag. Returns 1 if the support +# is enabled, else 0. If support for that feature is not found, a +# fatal error is triggered if --enable-load-extension is explicitly +# provided, else a loud warning is instead emitted. If +# --disable-load-extension is used, no check is performed. +# +# Makes the following environment changes: +# +# - defines LDFLAGS_DLOPEN to any linker flags needed for this +# feature. It may legally be empty on some systems where dlopen() +# is in libc. +# +# - If the feature is not available, adds +# -DSQLITE_OMIT_LOAD_EXTENSION=1 to the feature flags list. +proc sqlite-handle-load-extension {} { + define LDFLAGS_DLOPEN "" + set found 0 + proj-if-opt-truthy load-extension { + set found [proj-check-function-in-lib dlopen dl] + if {$found} { + set ldf [get-define lib_dlopen] + define LDFLAGS_DLOPEN $ldf + teaish-ldflags-prepend $ldf + undefine lib_dlopen + } else { + if {[proj-opt-was-provided load-extension]} { + # Explicit --enable-load-extension: fail if not found + proj-indented-notice -error { + --enable-load-extension was provided but dlopen() + not found. Use --disable-load-extension to bypass this + check. + } + } else { + # It was implicitly enabled: warn if not found + proj-indented-notice { + WARNING: dlopen() not found, so loadable module support will + be disabled. Use --disable-load-extension to bypass this + check. + } + } + } + } + if {$found} { + msg-result "Loadable extension support enabled." + } else { + msg-result "Disabling loadable extension support. Use --enable-load-extension to enable them." + sqlite-add-feature-flag -DSQLITE_OMIT_LOAD_EXTENSION=1 + } + return $found +} + +# +# ICU - International Components for Unicode +# +# Handles these flags: +# +# --with-icu-ldflags=LDFLAGS +# --with-icu-cflags=CFLAGS +# --with-icu-config[=auto | pkg-config | /path/to/icu-config] +# --enable-icu-collations +# +# --with-icu-config values: +# +# - auto: use the first one of (pkg-config, icu-config) found on the +# system. +# - pkg-config: use only pkg-config to determine flags +# - /path/to/icu-config: use that to determine flags +# +# If --with-icu-config is used as neither pkg-config nor icu-config +# are found, fail fatally. +# +# If both --with-icu-ldflags and --with-icu-config are provided, they +# are cumulative. If neither are provided, icu-collations is not +# honored and a warning is emitted if it is provided. +# +# Design note: though we could automatically enable ICU if the +# icu-config binary or (pkg-config icu-io) are found, we specifically +# do not. ICU is always an opt-in feature. +proc sqlite-handle-icu {} { + define LDFLAGS_LIBICU [join [opt-val with-icu-ldflags ""]] + define CFLAGS_LIBICU [join [opt-val with-icu-cflags ""]] + if {[proj-opt-was-provided with-icu-config]} { + msg-result "Checking for ICU support..." + set icuConfigBin [opt-val with-icu-config] + set tryIcuConfigBin 1; # set to 0 if we end up using pkg-config + if {$icuConfigBin in {auto pkg-config}} { + uplevel 3 { use pkg-config } + if {[pkg-config-init 0] && [pkg-config icu-io]} { + # Maintenance reminder: historical docs say to use both of + # (icu-io, icu-uc). icu-uc lacks a required lib and icu-io has + # all of them on tested OSes. + set tryIcuConfigBin 0 + define LDFLAGS_LIBICU [get-define PKG_ICU_IO_LDFLAGS] + define-append LDFLAGS_LIBICU [get-define PKG_ICU_IO_LIBS] + define CFLAGS_LIBICU [get-define PKG_ICU_IO_CFLAGS] + } elseif {"pkg-config" eq $icuConfigBin} { + proj-fatal "pkg-config cannot find package icu-io" + } else { + proj-assert {"auto" eq $icuConfigBin} + } + } + if {$tryIcuConfigBin} { + if {"auto" eq $icuConfigBin} { + set icuConfigBin [proj-first-bin-of \ + /usr/local/bin/icu-config \ + /usr/bin/icu-config] + if {"" eq $icuConfigBin} { + proj-indented-notice -error { + --with-icu-config=auto cannot find (pkg-config icu-io) or icu-config binary. + On Ubuntu-like systems try: + --with-icu-ldflags='-licui18n -licuuc -licudata' + } + } + } + if {[file-isexec $icuConfigBin]} { + set x [exec $icuConfigBin --ldflags] + if {"" eq $x} { + proj-indented-notice -error \ + [subst { + $icuConfigBin --ldflags returned no data. + On Ubuntu-like systems try: + --with-icu-ldflags='-licui18n -licuuc -licudata' + }] + } + define-append LDFLAGS_LIBICU $x + set x [exec $icuConfigBin --cppflags] + define-append CFLAGS_LIBICU $x + } else { + proj-fatal "--with-icu-config=$icuConfigBin does not refer to an executable" + } + } + } + set ldflags [define LDFLAGS_LIBICU [string trim [get-define LDFLAGS_LIBICU]]] + set cflags [define CFLAGS_LIBICU [string trim [get-define CFLAGS_LIBICU]]] + if {"" ne $ldflags} { + sqlite-add-feature-flag -DSQLITE_ENABLE_ICU + msg-result "Enabling ICU support with flags: $ldflags $cflags" + if {[opt-bool icu-collations]} { + msg-result "Enabling ICU collations." + sqlite-add-feature-flag -DSQLITE_ENABLE_ICU_COLLATIONS + } + teaish-ldflags-prepend $ldflags + teaish-cflags-add $cflags + } elseif {[opt-bool icu-collations]} { + proj-warn "ignoring --enable-icu-collations because neither --with-icu-ldflags nor --with-icu-config provided any linker flags" + } else { + msg-result "ICU support is disabled." + } +}; # sqlite-handle-icu + + +# +# Handles the --with-tempstore flag. +# +# The test fixture likes to set SQLITE_TEMP_STORE on its own, so do +# not set that feature flag unless it was explicitly provided to the +# configure script. +proc sqlite-handle-tempstore {} { + if {[proj-opt-was-provided with-tempstore]} { + set ts [opt-val with-tempstore no] + set tsn 1 + msg-checking "Use an in-RAM database for temporary tables? " + switch -exact -- $ts { + never { set tsn 0 } + no { set tsn 1 } + yes { set tsn 2 } + always { set tsn 3 } + default { + user-error "Invalid --with-tempstore value '$ts'. Use one of: never, no, yes, always" + } + } + msg-result $ts + sqlite-add-feature-flag -DSQLITE_TEMP_STORE=$tsn + } +} + +# +# Handles the --enable-math flag. +proc sqlite-handle-math {} { + proj-if-opt-truthy math { + if {![proj-check-function-in-lib ceil m]} { + user-error "Cannot find libm functions. Use --disable-math to bypass this." + } + set lfl [get-define lib_ceil] + undefine lib_ceil + define LDFLAGS_MATH $lfl + teaish-ldflags-prepend $lfl + sqlite-add-feature-flag -DSQLITE_ENABLE_MATH_FUNCTIONS + msg-result "Enabling math SQL functions" + } { + define LDFLAGS_MATH "" + msg-result "Disabling math SQL functions" + } +} + +# +# Move -DSQLITE_OMIT... and -DSQLITE_ENABLE... flags from CFLAGS and +# CPPFLAGS to OPT_FEATURE_FLAGS and remove them from BUILD_CFLAGS. +proc sqlite-munge-cflags {} { + # Move CFLAGS and CPPFLAGS entries matching -DSQLITE_OMIT* and + # -DSQLITE_ENABLE* to OPT_FEATURE_FLAGS. This behavior is derived + # from the pre-3.48 build. + # + # If any configure flags for features are in conflict with + # CFLAGS/CPPFLAGS-specified feature flags, all bets are off. There + # are no guarantees about which one will take precedence. + foreach flagDef {CFLAGS CPPFLAGS} { + set tmp "" + foreach cf [get-define $flagDef ""] { + switch -glob -- $cf { + -DSQLITE_OMIT* - + -DSQLITE_ENABLE* { + sqlite-add-feature-flag $cf + } + default { + lappend tmp $cf + } + } + } + define $flagDef $tmp + } +} diff --git a/autoconf/tea/teaish.test.tcl b/autoconf/tea/teaish.test.tcl new file mode 100644 index 0000000000..b63c9426e3 --- /dev/null +++ b/autoconf/tea/teaish.test.tcl @@ -0,0 +1,14 @@ +test-expect 1.0-open { + sqlite3 db :memory: +} {} + +test-assert 1.1-version-3.x { + [string match 3.* [db eval {select sqlite_version()}]] +} + +test-expect 1.2-select { + db eval {select 'hi, world',1,2,3} +} {{hi, world} 1 2 3} + + +test-expect 99.0-db-close {db close} {} diff --git a/autoconf/tea/win/makefile.vc b/autoconf/tea/win/makefile.vc deleted file mode 100644 index a5e4627707..0000000000 --- a/autoconf/tea/win/makefile.vc +++ /dev/null @@ -1,414 +0,0 @@ -# makefile.vc -- -*- Makefile -*- -# -# Microsoft Visual C++ makefile for use with nmake.exe v1.62+ (VC++ 5.0+) -# -# This makefile is based upon the Tcl 8.4 Makefile.vc and modified to -# make it suitable as a general package makefile. Look for the word EDIT -# which marks sections that may need modification. As a minumum you will -# need to change the PROJECT, DOTVERSION and DLLOBJS variables to values -# relevant to your package. -# -# See the file "license.terms" for information on usage and redistribution -# of this file, and for a DISCLAIMER OF ALL WARRANTIES. -# -# Copyright (c) 1995-1996 Sun Microsystems, Inc. -# Copyright (c) 1998-2000 Ajuba Solutions. -# Copyright (c) 2001 ActiveState Corporation. -# Copyright (c) 2001-2002 David Gravereaux. -# Copyright (c) 2003 Pat Thoyts -# -#------------------------------------------------------------------------- -# RCS: @(#)$Id: makefile.vc,v 1.4 2004/07/26 08:22:05 patthoyts Exp $ -#------------------------------------------------------------------------- - -!if !defined(MSDEVDIR) && !defined(MSVCDIR) && !defined(VCINSTALLDIR) && !defined(MSSDK) && !defined(WINDOWSSDKDIR) -MSG = ^ -You will need to run vcvars32.bat from Developer Studio, first, to setup^ -the environment. Jump to this line to read the new instructions. -!error $(MSG) -!endif - -#------------------------------------------------------------------------------ -# HOW TO USE this makefile: -# -# 1) It is now necessary to have %MSVCDir% set in the environment. This is -# used as a check to see if vcvars32.bat had been run prior to running -# nmake or during the installation of Microsoft Visual C++, MSVCDir had -# been set globally and the PATH adjusted. Either way is valid. -# -# You'll need to run vcvars32.bat contained in the MsDev's vc(98)/bin -# directory to setup the proper environment, if needed, for your current -# setup. This is a needed bootstrap requirement and allows the swapping of -# different environments to be easier. -# -# 2) To use the Platform SDK (not expressly needed), run setenv.bat after -# vcvars32.bat according to the instructions for it. This can also turn on -# the 64-bit compiler, if your SDK has it. -# -# 3) Targets are: -# all -- Builds everything. -# -- Builds the project (eg: nmake sample) -# test -- Builds and runs the test suite. -# install -- Installs the built binaries and libraries to $(INSTALLDIR) -# in an appropriate subdirectory. -# clean/realclean/distclean -- varying levels of cleaning. -# -# 4) Macros usable on the commandline: -# INSTALLDIR= -# Sets where to install Tcl from the built binaries. -# C:\Progra~1\Tcl is assumed when not specified. -# -# OPTS=static,msvcrt,staticpkg,threads,symbols,profile,loimpact,none -# Sets special options for the core. The default is for none. -# Any combination of the above may be used (comma separated). -# 'none' will over-ride everything to nothing. -# -# static = Builds a static library of the core instead of a -# dll. The shell will be static (and large), as well. -# msvcrt = Effects the static option only to switch it from -# using libcmt(d) as the C runtime [by default] to -# msvcrt(d). This is useful for static embedding -# support. -# staticpkg = Effects the static option only to switch -# tclshXX.exe to have the dde and reg extension linked -# inside it. -# threads = Turns on full multithreading support. -# thrdalloc = Use the thread allocator (shared global free pool). -# symbols = Adds symbols for step debugging. -# profile = Adds profiling hooks. Map file is assumed. -# loimpact = Adds a flag for how NT treats the heap to keep memory -# in use, low. This is said to impact alloc performance. -# -# STATS=memdbg,compdbg,none -# Sets optional memory and bytecode compiler debugging code added -# to the core. The default is for none. Any combination of the -# above may be used (comma separated). 'none' will over-ride -# everything to nothing. -# -# memdbg = Enables the debugging memory allocator. -# compdbg = Enables byte compilation logging. -# -# MACHINE=(IX86|IA64|ALPHA) -# Set the machine type used for the compiler, linker, and -# resource compiler. This hook is needed to tell the tools -# when alternate platforms are requested. IX86 is the default -# when not specified. -# -# TMP_DIR= -# OUT_DIR= -# Hooks to allow the intermediate and output directories to be -# changed. $(OUT_DIR) is assumed to be -# $(BINROOT)\(Release|Debug) based on if symbols are requested. -# $(TMP_DIR) will de $(OUT_DIR)\ by default. -# -# TESTPAT= -# Reads the tests requested to be run from this file. -# -# CFG_ENCODING=encoding -# name of encoding for configuration information. Defaults -# to cp1252 -# -# 5) Examples: -# -# Basic syntax of calling nmake looks like this: -# nmake [-nologo] -f makefile.vc [target|macrodef [target|macrodef] [...]] -# -# Standard (no frills) -# c:\tcl_src\win\>c:\progra~1\micros~1\vc98\bin\vcvars32.bat -# Setting environment for using Microsoft Visual C++ tools. -# c:\tcl_src\win\>nmake -f makefile.vc all -# c:\tcl_src\win\>nmake -f makefile.vc install INSTALLDIR=c:\progra~1\tcl -# -# Building for Win64 -# c:\tcl_src\win\>c:\progra~1\micros~1\vc98\bin\vcvars32.bat -# Setting environment for using Microsoft Visual C++ tools. -# c:\tcl_src\win\>c:\progra~1\platfo~1\setenv.bat /pre64 /RETAIL -# Targeting Windows pre64 RETAIL -# c:\tcl_src\win\>nmake -f makefile.vc MACHINE=IA64 -# -#------------------------------------------------------------------------------ -#============================================================================== -############################################################################### -#------------------------------------------------------------------------------ - -!if !exist("makefile.vc") -MSG = ^ -You must run this makefile only from the directory it is in.^ -Please `cd` to its location first. -!error $(MSG) -!endif - -#------------------------------------------------------------------------- -# Project specific information (EDIT) -# -# You should edit this with the name and version of your project. This -# information is used to generate the name of the package library and -# it's install location. -# -# For example, the sample extension is going to build sample04.dll and -# would install it into $(INSTALLDIR)\lib\sample04 -# -# You need to specify the object files that need to be linked into your -# binary here. -# -#------------------------------------------------------------------------- - -PROJECT = sqlite3 -!include "rules.vc" - -# nmakehelp -V will search the file for tag, skips until a -# number and returns all character until a character not in [0-9.ab] -# is read. - -!if [echo REM = This file is generated from Makefile.vc > versions.vc] -!endif -# get project version from row "AC_INIT([sqlite], [3.7.14])" -!if [echo DOTVERSION = \>> versions.vc] \ - && [nmakehlp -V ..\configure.in AC_INIT >> versions.vc] -!endif -!include "versions.vc" - -VERSION = $(DOTVERSION:.=) -STUBPREFIX = $(PROJECT)stub - -DLLOBJS = \ - $(TMP_DIR)\tclsqlite3.obj - -#------------------------------------------------------------------------- -# Target names and paths ( shouldn't need changing ) -#------------------------------------------------------------------------- - -BINROOT = . -ROOT = .. - -PRJIMPLIB = $(OUT_DIR)\$(PROJECT)$(VERSION)$(SUFX).lib -PRJLIBNAME = $(PROJECT)$(VERSION)$(SUFX).$(EXT) -PRJLIB = $(OUT_DIR)\$(PRJLIBNAME) - -PRJSTUBLIBNAME = $(STUBPREFIX)$(VERSION).lib -PRJSTUBLIB = $(OUT_DIR)\$(PRJSTUBLIBNAME) - -### Make sure we use backslash only. -PRJ_INSTALL_DIR = $(_INSTALLDIR)\$(PROJECT)$(DOTVERSION) -LIB_INSTALL_DIR = $(PRJ_INSTALL_DIR) -BIN_INSTALL_DIR = $(PRJ_INSTALL_DIR) -DOC_INSTALL_DIR = $(PRJ_INSTALL_DIR) -SCRIPT_INSTALL_DIR = $(PRJ_INSTALL_DIR) -INCLUDE_INSTALL_DIR = $(_TCLDIR)\include - -### The following paths CANNOT have spaces in them. -GENERICDIR = $(ROOT)\generic -WINDIR = $(ROOT)\win -LIBDIR = $(ROOT)\library -DOCDIR = $(ROOT)\doc -TOOLSDIR = $(ROOT)\tools -COMPATDIR = $(ROOT)\compat - -#--------------------------------------------------------------------- -# Compile flags -#--------------------------------------------------------------------- - -!if !$(DEBUG) -!if $(OPTIMIZING) -### This cranks the optimization level to maximize speed -cdebug = -O2 -Op -Gs -!else -cdebug = -!endif -!else if "$(MACHINE)" == "IA64" -### Warnings are too many, can't support warnings into errors. -cdebug = -Z7 -Od -GZ -!else -cdebug = -Z7 -WX -Od -GZ -!endif - -### Declarations common to all compiler options -cflags = -nologo -c -W3 -YX -Fp$(TMP_DIR)^\ - -!if $(MSVCRT) -!if $(DEBUG) -crt = -MDd -!else -crt = -MD -!endif -!else -!if $(DEBUG) -crt = -MTd -!else -crt = -MT -!endif -!endif - -INCLUDES = $(TCL_INCLUDES) -I"$(WINDIR)" -I"$(GENERICDIR)" \ - -I"$(ROOT)\.." -BASE_CLFAGS = $(cflags) $(cdebug) $(crt) $(INCLUDES) \ - -DSQLITE_3_SUFFIX_ONLY=1 -DSQLITE_ENABLE_RTREE=1 \ - -DSQLITE_ENABLE_FTS3=1 -DSQLITE_OMIT_DEPRECATED=1 -CON_CFLAGS = $(cflags) $(cdebug) $(crt) -DCONSOLE -DSQLITE_ENABLE_FTS3=1 -TCL_CFLAGS = -DBUILD_sqlite -DUSE_TCL_STUBS \ - -DPACKAGE_VERSION="\"$(DOTVERSION)\"" $(BASE_CLFAGS) \ - $(OPTDEFINES) - -#--------------------------------------------------------------------- -# Link flags -#--------------------------------------------------------------------- - -!if $(DEBUG) -ldebug = -debug:full -debugtype:cv -!else -ldebug = -release -opt:ref -opt:icf,3 -!endif - -### Declarations common to all linker options -lflags = -nologo -machine:$(MACHINE) $(ldebug) - -!if $(PROFILE) -lflags = $(lflags) -profile -!endif - -!if $(ALIGN98_HACK) && !$(STATIC_BUILD) -### Align sections for PE size savings. -lflags = $(lflags) -opt:nowin98 -!else if !$(ALIGN98_HACK) && $(STATIC_BUILD) -### Align sections for speed in loading by choosing the virtual page size. -lflags = $(lflags) -align:4096 -!endif - -!if $(LOIMPACT) -lflags = $(lflags) -ws:aggressive -!endif - -dlllflags = $(lflags) -dll -conlflags = $(lflags) -subsystem:console -guilflags = $(lflags) -subsystem:windows -baselibs = $(TCLSTUBLIB) - -#--------------------------------------------------------------------- -# TclTest flags -#--------------------------------------------------------------------- - -!IF "$(TESTPAT)" != "" -TESTFLAGS = $(TESTFLAGS) -file $(TESTPAT) -!ENDIF - -#--------------------------------------------------------------------- -# Project specific targets (EDIT) -#--------------------------------------------------------------------- - -all: setup $(PROJECT) -$(PROJECT): setup $(PRJLIB) -install: install-binaries install-libraries install-docs - -# Tests need to ensure we load the right dll file we -# have to handle the output differently on Win9x. -# -!if "$(OS)" == "Windows_NT" || "$(MSVCDIR)" == "IDE" -test: setup $(PROJECT) - set TCL_LIBRARY=$(ROOT)/library - $(TCLSH) << -load $(PRJLIB:\=/) -cd "$(ROOT)/tests" -set argv "$(TESTFLAGS)" -source all.tcl -<< -!else -test: setup $(PROJECT) - echo Please wait while the test results are collected - set TCL_LIBRARY=$(ROOT)/library - $(TCLSH) << >tests.log -load $(PRJLIB:\=/) -cd "$(ROOT)/tests" -set argv "$(TESTFLAGS)" -source all.tcl -<< - type tests.log | more -!endif - -setup: - @if not exist $(OUT_DIR)\nul mkdir $(OUT_DIR) - @if not exist $(TMP_DIR)\nul mkdir $(TMP_DIR) - -$(PRJLIB): $(DLLOBJS) - $(link32) $(dlllflags) -out:$@ $(baselibs) @<< -$** -<< - -@del $*.exp - -$(PRJSTUBLIB): $(PRJSTUBOBJS) - $(lib32) -nologo -out:$@ $(PRJSTUBOBJS) - -#--------------------------------------------------------------------- -# Implicit rules -#--------------------------------------------------------------------- - -{$(WINDIR)}.c{$(TMP_DIR)}.obj:: - $(cc32) $(TCL_CFLAGS) -DBUILD_$(PROJECT) -Fo$(TMP_DIR)\ @<< -$< -<< - -{$(GENERICDIR)}.c{$(TMP_DIR)}.obj:: - $(cc32) $(TCL_CFLAGS) -DBUILD_$(PROJECT) -Fo$(TMP_DIR)\ @<< -$< -<< - -{$(COMPATDIR)}.c{$(TMP_DIR)}.obj:: - $(cc32) $(TCL_CFLAGS) -DBUILD_$(PROJECT) -Fo$(TMP_DIR)\ @<< -$< -<< - -{$(WINDIR)}.rc{$(TMP_DIR)}.res: - $(rc32) -fo $@ -r -i "$(GENERICDIR)" -D__WIN32__ \ -!if $(DEBUG) - -d DEBUG \ -!endif -!if $(TCL_THREADS) - -d TCL_THREADS \ -!endif -!if $(STATIC_BUILD) - -d STATIC_BUILD \ -!endif - $< - -.SUFFIXES: -.SUFFIXES:.c .rc - -#--------------------------------------------------------------------- -# Installation. (EDIT) -# -# You may need to modify this section to reflect the final distribution -# of your files and possibly to generate documentation. -# -#--------------------------------------------------------------------- - -install-binaries: - @echo Installing binaries to '$(SCRIPT_INSTALL_DIR)' - @if not exist "$(SCRIPT_INSTALL_DIR)" mkdir "$(SCRIPT_INSTALL_DIR)" - @$(CPY) $(PRJLIB) "$(SCRIPT_INSTALL_DIR)" >NUL - -install-libraries: - @echo Installing libraries to '$(SCRIPT_INSTALL_DIR)' - @if exist $(LIBDIR) $(CPY) $(LIBDIR)\*.tcl "$(SCRIPT_INSTALL_DIR)" - @echo Installing package index in '$(SCRIPT_INSTALL_DIR)' - @type << >"$(SCRIPT_INSTALL_DIR)\pkgIndex.tcl" -package ifneeded $(PROJECT) $(DOTVERSION) \ - [list load [file join $$dir $(PRJLIBNAME)] sqlite3] -<< - -install-docs: - @echo Installing documentation files to '$(DOC_INSTALL_DIR)' - @if exist $(DOCDIR) $(CPY) $(DOCDIR)\*.n "$(DOC_INSTALL_DIR)" - -#--------------------------------------------------------------------- -# Clean up -#--------------------------------------------------------------------- - -clean: - @if exist $(TMP_DIR)\nul $(RMDIR) $(TMP_DIR) - @if exist $(WINDIR)\version.vc del $(WINDIR)\version.vc - -realclean: clean - @if exist $(OUT_DIR)\nul $(RMDIR) $(OUT_DIR) - -distclean: realclean - @if exist $(WINDIR)\nmakehlp.exe del $(WINDIR)\nmakehlp.exe - @if exist $(WINDIR)\nmakehlp.obj del $(WINDIR)\nmakehlp.obj diff --git a/autoconf/tea/win/nmakehlp.c b/autoconf/tea/win/nmakehlp.c deleted file mode 100644 index e00f1b4996..0000000000 --- a/autoconf/tea/win/nmakehlp.c +++ /dev/null @@ -1,694 +0,0 @@ -/* - * ---------------------------------------------------------------------------- - * nmakehlp.c -- - * - * This is used to fix limitations within nmake and the environment. - * - * Copyright (c) 2002 by David Gravereaux. - * Copyright (c) 2006 by Pat Thoyts - * - * See the file "license.terms" for information on usage and redistribution of - * this file, and for a DISCLAIMER OF ALL WARRANTIES. - * ---------------------------------------------------------------------------- - */ - -#define _CRT_SECURE_NO_DEPRECATE -#include -#define NO_SHLWAPI_GDI -#define NO_SHLWAPI_STREAM -#define NO_SHLWAPI_REG -#include -#pragma comment (lib, "user32.lib") -#pragma comment (lib, "kernel32.lib") -#pragma comment (lib, "shlwapi.lib") -#include -#include - -/* - * This library is required for x64 builds with _some_ versions of MSVC - */ -#if defined(_M_IA64) || defined(_M_AMD64) -#if _MSC_VER >= 1400 && _MSC_VER < 1500 -#pragma comment(lib, "bufferoverflowU") -#endif -#endif - -/* ISO hack for dumb VC++ */ -#ifdef _MSC_VER -#define snprintf _snprintf -#endif - - - -/* protos */ - -static int CheckForCompilerFeature(const char *option); -static int CheckForLinkerFeature(const char *option); -static int IsIn(const char *string, const char *substring); -static int SubstituteFile(const char *substs, const char *filename); -static int QualifyPath(const char *path); -static const char *GetVersionFromFile(const char *filename, const char *match); -static DWORD WINAPI ReadFromPipe(LPVOID args); - -/* globals */ - -#define CHUNK 25 -#define STATICBUFFERSIZE 1000 -typedef struct { - HANDLE pipe; - char buffer[STATICBUFFERSIZE]; -} pipeinfo; - -pipeinfo Out = {INVALID_HANDLE_VALUE, '\0'}; -pipeinfo Err = {INVALID_HANDLE_VALUE, '\0'}; - -/* - * exitcodes: 0 == no, 1 == yes, 2 == error - */ - -int -main( - int argc, - char *argv[]) -{ - char msg[300]; - DWORD dwWritten; - int chars; - - /* - * Make sure children (cl.exe and link.exe) are kept quiet. - */ - - SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX); - - /* - * Make sure the compiler and linker aren't effected by the outside world. - */ - - SetEnvironmentVariable("CL", ""); - SetEnvironmentVariable("LINK", ""); - - if (argc > 1 && *argv[1] == '-') { - switch (*(argv[1]+1)) { - case 'c': - if (argc != 3) { - chars = snprintf(msg, sizeof(msg) - 1, - "usage: %s -c \n" - "Tests for whether cl.exe supports an option\n" - "exitcodes: 0 == no, 1 == yes, 2 == error\n", argv[0]); - WriteFile(GetStdHandle(STD_ERROR_HANDLE), msg, chars, - &dwWritten, NULL); - return 2; - } - return CheckForCompilerFeature(argv[2]); - case 'l': - if (argc != 3) { - chars = snprintf(msg, sizeof(msg) - 1, - "usage: %s -l \n" - "Tests for whether link.exe supports an option\n" - "exitcodes: 0 == no, 1 == yes, 2 == error\n", argv[0]); - WriteFile(GetStdHandle(STD_ERROR_HANDLE), msg, chars, - &dwWritten, NULL); - return 2; - } - return CheckForLinkerFeature(argv[2]); - case 'f': - if (argc == 2) { - chars = snprintf(msg, sizeof(msg) - 1, - "usage: %s -f \n" - "Find a substring within another\n" - "exitcodes: 0 == no, 1 == yes, 2 == error\n", argv[0]); - WriteFile(GetStdHandle(STD_ERROR_HANDLE), msg, chars, - &dwWritten, NULL); - return 2; - } else if (argc == 3) { - /* - * If the string is blank, there is no match. - */ - - return 0; - } else { - return IsIn(argv[2], argv[3]); - } - case 's': - if (argc == 2) { - chars = snprintf(msg, sizeof(msg) - 1, - "usage: %s -s \n" - "Perform a set of string map type substutitions on a file\n" - "exitcodes: 0\n", - argv[0]); - WriteFile(GetStdHandle(STD_ERROR_HANDLE), msg, chars, - &dwWritten, NULL); - return 2; - } - return SubstituteFile(argv[2], argv[3]); - case 'V': - if (argc != 4) { - chars = snprintf(msg, sizeof(msg) - 1, - "usage: %s -V filename matchstring\n" - "Extract a version from a file:\n" - "eg: pkgIndex.tcl \"package ifneeded http\"", - argv[0]); - WriteFile(GetStdHandle(STD_ERROR_HANDLE), msg, chars, - &dwWritten, NULL); - return 0; - } - printf("%s\n", GetVersionFromFile(argv[2], argv[3])); - return 0; - case 'Q': - if (argc != 3) { - chars = snprintf(msg, sizeof(msg) - 1, - "usage: %s -Q path\n" - "Emit the fully qualified path\n" - "exitcodes: 0 == no, 1 == yes, 2 == error\n", argv[0]); - WriteFile(GetStdHandle(STD_ERROR_HANDLE), msg, chars, - &dwWritten, NULL); - return 2; - } - return QualifyPath(argv[2]); - } - } - chars = snprintf(msg, sizeof(msg) - 1, - "usage: %s -c|-f|-l|-Q|-s|-V ...\n" - "This is a little helper app to equalize shell differences between WinNT and\n" - "Win9x and get nmake.exe to accomplish its job.\n", - argv[0]); - WriteFile(GetStdHandle(STD_ERROR_HANDLE), msg, chars, &dwWritten, NULL); - return 2; -} - -static int -CheckForCompilerFeature( - const char *option) -{ - STARTUPINFO si; - PROCESS_INFORMATION pi; - SECURITY_ATTRIBUTES sa; - DWORD threadID; - char msg[300]; - BOOL ok; - HANDLE hProcess, h, pipeThreads[2]; - char cmdline[100]; - - hProcess = GetCurrentProcess(); - - ZeroMemory(&pi, sizeof(PROCESS_INFORMATION)); - ZeroMemory(&si, sizeof(STARTUPINFO)); - si.cb = sizeof(STARTUPINFO); - si.dwFlags = STARTF_USESTDHANDLES; - si.hStdInput = INVALID_HANDLE_VALUE; - - ZeroMemory(&sa, sizeof(SECURITY_ATTRIBUTES)); - sa.nLength = sizeof(SECURITY_ATTRIBUTES); - sa.lpSecurityDescriptor = NULL; - sa.bInheritHandle = FALSE; - - /* - * Create a non-inheritible pipe. - */ - - CreatePipe(&Out.pipe, &h, &sa, 0); - - /* - * Dupe the write side, make it inheritible, and close the original. - */ - - DuplicateHandle(hProcess, h, hProcess, &si.hStdOutput, 0, TRUE, - DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE); - - /* - * Same as above, but for the error side. - */ - - CreatePipe(&Err.pipe, &h, &sa, 0); - DuplicateHandle(hProcess, h, hProcess, &si.hStdError, 0, TRUE, - DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE); - - /* - * Base command line. - */ - - lstrcpy(cmdline, "cl.exe -nologo -c -TC -Zs -X -Fp.\\_junk.pch "); - - /* - * Append our option for testing - */ - - lstrcat(cmdline, option); - - /* - * Filename to compile, which exists, but is nothing and empty. - */ - - lstrcat(cmdline, " .\\nul"); - - ok = CreateProcess( - NULL, /* Module name. */ - cmdline, /* Command line. */ - NULL, /* Process handle not inheritable. */ - NULL, /* Thread handle not inheritable. */ - TRUE, /* yes, inherit handles. */ - DETACHED_PROCESS, /* No console for you. */ - NULL, /* Use parent's environment block. */ - NULL, /* Use parent's starting directory. */ - &si, /* Pointer to STARTUPINFO structure. */ - &pi); /* Pointer to PROCESS_INFORMATION structure. */ - - if (!ok) { - DWORD err = GetLastError(); - int chars = snprintf(msg, sizeof(msg) - 1, - "Tried to launch: \"%s\", but got error [%u]: ", cmdline, err); - - FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS| - FORMAT_MESSAGE_MAX_WIDTH_MASK, 0L, err, 0, (LPVOID)&msg[chars], - (300-chars), 0); - WriteFile(GetStdHandle(STD_ERROR_HANDLE), msg, lstrlen(msg), &err,NULL); - return 2; - } - - /* - * Close our references to the write handles that have now been inherited. - */ - - CloseHandle(si.hStdOutput); - CloseHandle(si.hStdError); - - WaitForInputIdle(pi.hProcess, 5000); - CloseHandle(pi.hThread); - - /* - * Start the pipe reader threads. - */ - - pipeThreads[0] = CreateThread(NULL, 0, ReadFromPipe, &Out, 0, &threadID); - pipeThreads[1] = CreateThread(NULL, 0, ReadFromPipe, &Err, 0, &threadID); - - /* - * Block waiting for the process to end. - */ - - WaitForSingleObject(pi.hProcess, INFINITE); - CloseHandle(pi.hProcess); - - /* - * Wait for our pipe to get done reading, should it be a little slow. - */ - - WaitForMultipleObjects(2, pipeThreads, TRUE, 500); - CloseHandle(pipeThreads[0]); - CloseHandle(pipeThreads[1]); - - /* - * Look for the commandline warning code in both streams. - * - in MSVC 6 & 7 we get D4002, in MSVC 8 we get D9002. - */ - - return !(strstr(Out.buffer, "D4002") != NULL - || strstr(Err.buffer, "D4002") != NULL - || strstr(Out.buffer, "D9002") != NULL - || strstr(Err.buffer, "D9002") != NULL - || strstr(Out.buffer, "D2021") != NULL - || strstr(Err.buffer, "D2021") != NULL); -} - -static int -CheckForLinkerFeature( - const char *option) -{ - STARTUPINFO si; - PROCESS_INFORMATION pi; - SECURITY_ATTRIBUTES sa; - DWORD threadID; - char msg[300]; - BOOL ok; - HANDLE hProcess, h, pipeThreads[2]; - char cmdline[100]; - - hProcess = GetCurrentProcess(); - - ZeroMemory(&pi, sizeof(PROCESS_INFORMATION)); - ZeroMemory(&si, sizeof(STARTUPINFO)); - si.cb = sizeof(STARTUPINFO); - si.dwFlags = STARTF_USESTDHANDLES; - si.hStdInput = INVALID_HANDLE_VALUE; - - ZeroMemory(&sa, sizeof(SECURITY_ATTRIBUTES)); - sa.nLength = sizeof(SECURITY_ATTRIBUTES); - sa.lpSecurityDescriptor = NULL; - sa.bInheritHandle = TRUE; - - /* - * Create a non-inheritible pipe. - */ - - CreatePipe(&Out.pipe, &h, &sa, 0); - - /* - * Dupe the write side, make it inheritible, and close the original. - */ - - DuplicateHandle(hProcess, h, hProcess, &si.hStdOutput, 0, TRUE, - DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE); - - /* - * Same as above, but for the error side. - */ - - CreatePipe(&Err.pipe, &h, &sa, 0); - DuplicateHandle(hProcess, h, hProcess, &si.hStdError, 0, TRUE, - DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE); - - /* - * Base command line. - */ - - lstrcpy(cmdline, "link.exe -nologo "); - - /* - * Append our option for testing. - */ - - lstrcat(cmdline, option); - - ok = CreateProcess( - NULL, /* Module name. */ - cmdline, /* Command line. */ - NULL, /* Process handle not inheritable. */ - NULL, /* Thread handle not inheritable. */ - TRUE, /* yes, inherit handles. */ - DETACHED_PROCESS, /* No console for you. */ - NULL, /* Use parent's environment block. */ - NULL, /* Use parent's starting directory. */ - &si, /* Pointer to STARTUPINFO structure. */ - &pi); /* Pointer to PROCESS_INFORMATION structure. */ - - if (!ok) { - DWORD err = GetLastError(); - int chars = snprintf(msg, sizeof(msg) - 1, - "Tried to launch: \"%s\", but got error [%u]: ", cmdline, err); - - FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS| - FORMAT_MESSAGE_MAX_WIDTH_MASK, 0L, err, 0, (LPVOID)&msg[chars], - (300-chars), 0); - WriteFile(GetStdHandle(STD_ERROR_HANDLE), msg, lstrlen(msg), &err,NULL); - return 2; - } - - /* - * Close our references to the write handles that have now been inherited. - */ - - CloseHandle(si.hStdOutput); - CloseHandle(si.hStdError); - - WaitForInputIdle(pi.hProcess, 5000); - CloseHandle(pi.hThread); - - /* - * Start the pipe reader threads. - */ - - pipeThreads[0] = CreateThread(NULL, 0, ReadFromPipe, &Out, 0, &threadID); - pipeThreads[1] = CreateThread(NULL, 0, ReadFromPipe, &Err, 0, &threadID); - - /* - * Block waiting for the process to end. - */ - - WaitForSingleObject(pi.hProcess, INFINITE); - CloseHandle(pi.hProcess); - - /* - * Wait for our pipe to get done reading, should it be a little slow. - */ - - WaitForMultipleObjects(2, pipeThreads, TRUE, 500); - CloseHandle(pipeThreads[0]); - CloseHandle(pipeThreads[1]); - - /* - * Look for the commandline warning code in the stderr stream. - */ - - return !(strstr(Out.buffer, "LNK1117") != NULL || - strstr(Err.buffer, "LNK1117") != NULL || - strstr(Out.buffer, "LNK4044") != NULL || - strstr(Err.buffer, "LNK4044") != NULL); -} - -static DWORD WINAPI -ReadFromPipe( - LPVOID args) -{ - pipeinfo *pi = (pipeinfo *) args; - char *lastBuf = pi->buffer; - DWORD dwRead; - BOOL ok; - - again: - if (lastBuf - pi->buffer + CHUNK > STATICBUFFERSIZE) { - CloseHandle(pi->pipe); - return (DWORD)-1; - } - ok = ReadFile(pi->pipe, lastBuf, CHUNK, &dwRead, 0L); - if (!ok || dwRead == 0) { - CloseHandle(pi->pipe); - return 0; - } - lastBuf += dwRead; - goto again; - - return 0; /* makes the compiler happy */ -} - -static int -IsIn( - const char *string, - const char *substring) -{ - return (strstr(string, substring) != NULL); -} - -/* - * GetVersionFromFile -- - * Looks for a match string in a file and then returns the version - * following the match where a version is anything acceptable to - * package provide or package ifneeded. - */ - -static const char * -GetVersionFromFile( - const char *filename, - const char *match) -{ - size_t cbBuffer = 100; - static char szBuffer[100]; - char *szResult = NULL; - FILE *fp = fopen(filename, "rt"); - - if (fp != NULL) { - /* - * Read data until we see our match string. - */ - - while (fgets(szBuffer, cbBuffer, fp) != NULL) { - LPSTR p, q; - - p = strstr(szBuffer, match); - if (p != NULL) { - /* - * Skip to first digit. - */ - - while (*p && !isdigit(*p)) { - ++p; - } - - /* - * Find ending whitespace. - */ - - q = p; - while (*q && (isalnum(*q) || *q == '.')) { - ++q; - } - - memcpy(szBuffer, p, q - p); - szBuffer[q-p] = 0; - szResult = szBuffer; - break; - } - } - fclose(fp); - } - return szResult; -} - -/* - * List helpers for the SubstituteFile function - */ - -typedef struct list_item_t { - struct list_item_t *nextPtr; - char * key; - char * value; -} list_item_t; - -/* insert a list item into the list (list may be null) */ -static list_item_t * -list_insert(list_item_t **listPtrPtr, const char *key, const char *value) -{ - list_item_t *itemPtr = malloc(sizeof(list_item_t)); - if (itemPtr) { - itemPtr->key = strdup(key); - itemPtr->value = strdup(value); - itemPtr->nextPtr = NULL; - - while(*listPtrPtr) { - listPtrPtr = &(*listPtrPtr)->nextPtr; - } - *listPtrPtr = itemPtr; - } - return itemPtr; -} - -static void -list_free(list_item_t **listPtrPtr) -{ - list_item_t *tmpPtr, *listPtr = *listPtrPtr; - while (listPtr) { - tmpPtr = listPtr; - listPtr = listPtr->nextPtr; - free(tmpPtr->key); - free(tmpPtr->value); - free(tmpPtr); - } -} - -/* - * SubstituteFile -- - * As windows doesn't provide anything useful like sed and it's unreliable - * to use the tclsh you are building against (consider x-platform builds - - * eg compiling AMD64 target from IX86) we provide a simple substitution - * option here to handle autoconf style substitutions. - * The substitution file is whitespace and line delimited. The file should - * consist of lines matching the regular expression: - * \s*\S+\s+\S*$ - * - * Usage is something like: - * nmakehlp -S << $** > $@ - * @PACKAGE_NAME@ $(PACKAGE_NAME) - * @PACKAGE_VERSION@ $(PACKAGE_VERSION) - * << - */ - -static int -SubstituteFile( - const char *substitutions, - const char *filename) -{ - size_t cbBuffer = 1024; - static char szBuffer[1024], szCopy[1024]; - char *szResult = NULL; - list_item_t *substPtr = NULL; - FILE *fp, *sp; - - fp = fopen(filename, "rt"); - if (fp != NULL) { - - /* - * Build a list of substutitions from the first filename - */ - - sp = fopen(substitutions, "rt"); - if (sp != NULL) { - while (fgets(szBuffer, cbBuffer, sp) != NULL) { - unsigned char *ks, *ke, *vs, *ve; - ks = (unsigned char*)szBuffer; - while (ks && *ks && isspace(*ks)) ++ks; - ke = ks; - while (ke && *ke && !isspace(*ke)) ++ke; - vs = ke; - while (vs && *vs && isspace(*vs)) ++vs; - ve = vs; - while (ve && *ve && !(*ve == '\r' || *ve == '\n')) ++ve; - *ke = 0, *ve = 0; - list_insert(&substPtr, (char*)ks, (char*)vs); - } - fclose(sp); - } - - /* debug: dump the list */ -#ifdef _DEBUG - { - int n = 0; - list_item_t *p = NULL; - for (p = substPtr; p != NULL; p = p->nextPtr, ++n) { - fprintf(stderr, "% 3d '%s' => '%s'\n", n, p->key, p->value); - } - } -#endif - - /* - * Run the substitutions over each line of the input - */ - - while (fgets(szBuffer, cbBuffer, fp) != NULL) { - list_item_t *p = NULL; - for (p = substPtr; p != NULL; p = p->nextPtr) { - char *m = strstr(szBuffer, p->key); - if (m) { - char *cp, *op, *sp; - cp = szCopy; - op = szBuffer; - while (op != m) *cp++ = *op++; - sp = p->value; - while (sp && *sp) *cp++ = *sp++; - op += strlen(p->key); - while (*op) *cp++ = *op++; - *cp = 0; - memcpy(szBuffer, szCopy, sizeof(szCopy)); - } - } - printf(szBuffer); - } - - list_free(&substPtr); - } - fclose(fp); - return 0; -} - -/* - * QualifyPath -- - * - * This composes the current working directory with a provided path - * and returns the fully qualified and normalized path. - * Mostly needed to setup paths for testing. - */ - -static int -QualifyPath( - const char *szPath) -{ - char szCwd[MAX_PATH + 1]; - char szTmp[MAX_PATH + 1]; - char *p; - GetCurrentDirectory(MAX_PATH, szCwd); - while ((p = strchr(szPath, '/')) && *p) - *p = '\\'; - PathCombine(szTmp, szCwd, szPath); - PathCanonicalize(szCwd, szTmp); - printf("%s\n", szCwd); - return 0; -} - -/* - * Local variables: - * mode: c - * c-basic-offset: 4 - * fill-column: 78 - * indent-tabs-mode: t - * tab-width: 8 - * End: - */ diff --git a/autoconf/tea/win/rules.vc b/autoconf/tea/win/rules.vc deleted file mode 100644 index 99471053c8..0000000000 --- a/autoconf/tea/win/rules.vc +++ /dev/null @@ -1,711 +0,0 @@ -#------------------------------------------------------------------------------ -# rules.vc -- -# -# Microsoft Visual C++ makefile include for decoding the commandline -# macros. This file does not need editing to build Tcl. -# -# See the file "license.terms" for information on usage and redistribution -# of this file, and for a DISCLAIMER OF ALL WARRANTIES. -# -# Copyright (c) 2001-2003 David Gravereaux. -# Copyright (c) 2003-2008 Patrick Thoyts -#------------------------------------------------------------------------------ - -!ifndef _RULES_VC -_RULES_VC = 1 - -cc32 = $(CC) # built-in default. -link32 = link -lib32 = lib -rc32 = $(RC) # built-in default. - -!ifndef INSTALLDIR -### Assume the normal default. -_INSTALLDIR = C:\Program Files\Tcl -!else -### Fix the path separators. -_INSTALLDIR = $(INSTALLDIR:/=\) -!endif - -#---------------------------------------------------------- -# Set the proper copy method to avoid overwrite questions -# to the user when copying files and selecting the right -# "delete all" method. -#---------------------------------------------------------- - -!if "$(OS)" == "Windows_NT" -RMDIR = rmdir /S /Q -ERRNULL = 2>NUL -!if ![ver | find "4.0" > nul] -CPY = echo y | xcopy /i >NUL -COPY = copy >NUL -!else -CPY = xcopy /i /y >NUL -COPY = copy /y >NUL -!endif -!else # "$(OS)" != "Windows_NT" -CPY = xcopy /i >_JUNK.OUT # On Win98 NUL does not work here. -COPY = copy >_JUNK.OUT # On Win98 NUL does not work here. -RMDIR = deltree /Y -NULL = \NUL # Used in testing directory existence -ERRNULL = >NUL # Win9x shell cannot redirect stderr -!endif -MKDIR = mkdir - -#------------------------------------------------------------------------------ -# Determine the host and target architectures and compiler version. -#------------------------------------------------------------------------------ - -_HASH=^# -_VC_MANIFEST_EMBED_EXE= -_VC_MANIFEST_EMBED_DLL= -VCVER=0 -!if ![echo VCVERSION=_MSC_VER > vercl.x] \ - && ![echo $(_HASH)if defined(_M_IX86) >> vercl.x] \ - && ![echo ARCH=IX86 >> vercl.x] \ - && ![echo $(_HASH)elif defined(_M_AMD64) >> vercl.x] \ - && ![echo ARCH=AMD64 >> vercl.x] \ - && ![echo $(_HASH)endif >> vercl.x] \ - && ![cl -nologo -TC -P vercl.x $(ERRNULL)] -!include vercl.i -!if ![echo VCVER= ^\> vercl.vc] \ - && ![set /a $(VCVERSION) / 100 - 6 >> vercl.vc] -!include vercl.vc -!endif -!endif -!if ![del $(ERRNUL) /q/f vercl.x vercl.i vercl.vc] -!endif - -!if ![reg query HKLM\Hardware\Description\System\CentralProcessor\0 /v Identifier | findstr /i x86] -NATIVE_ARCH=IX86 -!else -NATIVE_ARCH=AMD64 -!endif - -# Since MSVC8 we must deal with manifest resources. -!if $(VCVERSION) >= 1400 -_VC_MANIFEST_EMBED_EXE=if exist $@.manifest mt -nologo -manifest $@.manifest -outputresource:$@;1 -_VC_MANIFEST_EMBED_DLL=if exist $@.manifest mt -nologo -manifest $@.manifest -outputresource:$@;2 -!endif - -!ifndef MACHINE -MACHINE=$(ARCH) -!endif - -!ifndef CFG_ENCODING -CFG_ENCODING = \"cp1252\" -!endif - -!message =============================================================================== - -#---------------------------------------------------------- -# build the helper app we need to overcome nmake's limiting -# environment. -#---------------------------------------------------------- - -!if !exist(nmakehlp.exe) -!if [$(cc32) -nologo nmakehlp.c -link -subsystem:console > nul] -!endif -!endif - -#---------------------------------------------------------- -# Test for compiler features -#---------------------------------------------------------- - -### test for optimizations -!if [nmakehlp -c -Ot] -!message *** Compiler has 'Optimizations' -OPTIMIZING = 1 -!else -!message *** Compiler does not have 'Optimizations' -OPTIMIZING = 0 -!endif - -OPTIMIZATIONS = - -!if [nmakehlp -c -Ot] -OPTIMIZATIONS = $(OPTIMIZATIONS) -Ot -!endif - -!if [nmakehlp -c -Oi] -OPTIMIZATIONS = $(OPTIMIZATIONS) -Oi -!endif - -!if [nmakehlp -c -Op] -OPTIMIZATIONS = $(OPTIMIZATIONS) -Op -!endif - -!if [nmakehlp -c -fp:strict] -OPTIMIZATIONS = $(OPTIMIZATIONS) -fp:strict -!endif - -!if [nmakehlp -c -Gs] -OPTIMIZATIONS = $(OPTIMIZATIONS) -Gs -!endif - -!if [nmakehlp -c -GS] -OPTIMIZATIONS = $(OPTIMIZATIONS) -GS -!endif - -!if [nmakehlp -c -GL] -OPTIMIZATIONS = $(OPTIMIZATIONS) -GL -!endif - -DEBUGFLAGS = - -!if [nmakehlp -c -RTC1] -DEBUGFLAGS = $(DEBUGFLAGS) -RTC1 -!elseif [nmakehlp -c -GZ] -DEBUGFLAGS = $(DEBUGFLAGS) -GZ -!endif - -COMPILERFLAGS =-W3 -DUNICODE -D_UNICODE - -# In v13 -GL and -YX are incompatible. -!if [nmakehlp -c -YX] -!if ![nmakehlp -c -GL] -OPTIMIZATIONS = $(OPTIMIZATIONS) -YX -!endif -!endif - -!if "$(MACHINE)" == "IX86" -### test for pentium errata -!if [nmakehlp -c -QI0f] -!message *** Compiler has 'Pentium 0x0f fix' -COMPILERFLAGS = $(COMPILERFLAGS) -QI0f -!else -!message *** Compiler does not have 'Pentium 0x0f fix' -!endif -!endif - -!if "$(MACHINE)" == "IA64" -### test for Itanium errata -!if [nmakehlp -c -QIA64_Bx] -!message *** Compiler has 'B-stepping errata workarounds' -COMPILERFLAGS = $(COMPILERFLAGS) -QIA64_Bx -!else -!message *** Compiler does not have 'B-stepping errata workarounds' -!endif -!endif - -!if "$(MACHINE)" == "IX86" -### test for -align:4096, when align:512 will do. -!if [nmakehlp -l -opt:nowin98] -!message *** Linker has 'Win98 alignment problem' -ALIGN98_HACK = 1 -!else -!message *** Linker does not have 'Win98 alignment problem' -ALIGN98_HACK = 0 -!endif -!else -ALIGN98_HACK = 0 -!endif - -LINKERFLAGS = - -!if [nmakehlp -l -ltcg] -LINKERFLAGS =-ltcg -!endif - -#---------------------------------------------------------- -# Decode the options requested. -#---------------------------------------------------------- - -!if "$(OPTS)" == "" || [nmakehlp -f "$(OPTS)" "none"] -STATIC_BUILD = 0 -TCL_THREADS = 1 -DEBUG = 0 -SYMBOLS = 0 -PROFILE = 0 -PGO = 0 -MSVCRT = 0 -LOIMPACT = 0 -TCL_USE_STATIC_PACKAGES = 0 -USE_THREAD_ALLOC = 1 -UNCHECKED = 0 -!else -!if [nmakehlp -f $(OPTS) "static"] -!message *** Doing static -STATIC_BUILD = 1 -!else -STATIC_BUILD = 0 -!endif -!if [nmakehlp -f $(OPTS) "msvcrt"] -!message *** Doing msvcrt -MSVCRT = 1 -!else -MSVCRT = 0 -!endif -!if [nmakehlp -f $(OPTS) "staticpkg"] -!message *** Doing staticpkg -TCL_USE_STATIC_PACKAGES = 1 -!else -TCL_USE_STATIC_PACKAGES = 0 -!endif -!if [nmakehlp -f $(OPTS) "nothreads"] -!message *** Compile explicitly for non-threaded tcl -TCL_THREADS = 0 -!else -TCL_THREADS = 1 -USE_THREAD_ALLOC= 1 -!endif -!if [nmakehlp -f $(OPTS) "symbols"] -!message *** Doing symbols -DEBUG = 1 -!else -DEBUG = 0 -!endif -!if [nmakehlp -f $(OPTS) "pdbs"] -!message *** Doing pdbs -SYMBOLS = 1 -!else -SYMBOLS = 0 -!endif -!if [nmakehlp -f $(OPTS) "profile"] -!message *** Doing profile -PROFILE = 1 -!else -PROFILE = 0 -!endif -!if [nmakehlp -f $(OPTS) "pgi"] -!message *** Doing profile guided optimization instrumentation -PGO = 1 -!elseif [nmakehlp -f $(OPTS) "pgo"] -!message *** Doing profile guided optimization -PGO = 2 -!else -PGO = 0 -!endif -!if [nmakehlp -f $(OPTS) "loimpact"] -!message *** Doing loimpact -LOIMPACT = 1 -!else -LOIMPACT = 0 -!endif -!if [nmakehlp -f $(OPTS) "thrdalloc"] -!message *** Doing thrdalloc -USE_THREAD_ALLOC = 1 -!endif -!if [nmakehlp -f $(OPTS) "tclalloc"] -!message *** Doing tclalloc -USE_THREAD_ALLOC = 0 -!endif -!if [nmakehlp -f $(OPTS) "unchecked"] -!message *** Doing unchecked -UNCHECKED = 1 -!else -UNCHECKED = 0 -!endif -!endif - - -!if !$(STATIC_BUILD) -# Make sure we don't build overly fat DLLs. -MSVCRT = 1 -# We shouldn't statically put the extensions inside the shell when dynamic. -TCL_USE_STATIC_PACKAGES = 0 -!endif - - -#---------------------------------------------------------- -# Figure-out how to name our intermediate and output directories. -# We wouldn't want different builds to use the same .obj files -# by accident. -#---------------------------------------------------------- - -#---------------------------------------- -# Naming convention: -# t = full thread support. -# s = static library (as opposed to an -# import library) -# g = linked to the debug enabled C -# run-time. -# x = special static build when it -# links to the dynamic C run-time. -#---------------------------------------- -SUFX = tsgx - -!if $(DEBUG) -BUILDDIRTOP = Debug -!else -BUILDDIRTOP = Release -!endif - -!if "$(MACHINE)" != "IX86" -BUILDDIRTOP =$(BUILDDIRTOP)_$(MACHINE) -!endif -!if $(VCVER) > 6 -BUILDDIRTOP =$(BUILDDIRTOP)_VC$(VCVER) -!endif - -!if !$(DEBUG) || $(DEBUG) && $(UNCHECKED) -SUFX = $(SUFX:g=) -!endif - -TMP_DIRFULL = .\$(BUILDDIRTOP)\$(PROJECT)_ThreadedDynamicStaticX - -!if !$(STATIC_BUILD) -TMP_DIRFULL = $(TMP_DIRFULL:Static=) -SUFX = $(SUFX:s=) -EXT = dll -!if $(MSVCRT) -TMP_DIRFULL = $(TMP_DIRFULL:X=) -SUFX = $(SUFX:x=) -!endif -!else -TMP_DIRFULL = $(TMP_DIRFULL:Dynamic=) -EXT = lib -!if !$(MSVCRT) -TMP_DIRFULL = $(TMP_DIRFULL:X=) -SUFX = $(SUFX:x=) -!endif -!endif - -!if !$(TCL_THREADS) -TMP_DIRFULL = $(TMP_DIRFULL:Threaded=) -SUFX = $(SUFX:t=) -!endif - -!ifndef TMP_DIR -TMP_DIR = $(TMP_DIRFULL) -!ifndef OUT_DIR -OUT_DIR = .\$(BUILDDIRTOP) -!endif -!else -!ifndef OUT_DIR -OUT_DIR = $(TMP_DIR) -!endif -!endif - - -#---------------------------------------------------------- -# Decode the statistics requested. -#---------------------------------------------------------- - -!if "$(STATS)" == "" || [nmakehlp -f "$(STATS)" "none"] -TCL_MEM_DEBUG = 0 -TCL_COMPILE_DEBUG = 0 -!else -!if [nmakehlp -f $(STATS) "memdbg"] -!message *** Doing memdbg -TCL_MEM_DEBUG = 1 -!else -TCL_MEM_DEBUG = 0 -!endif -!if [nmakehlp -f $(STATS) "compdbg"] -!message *** Doing compdbg -TCL_COMPILE_DEBUG = 1 -!else -TCL_COMPILE_DEBUG = 0 -!endif -!endif - - -#---------------------------------------------------------- -# Decode the checks requested. -#---------------------------------------------------------- - -!if "$(CHECKS)" == "" || [nmakehlp -f "$(CHECKS)" "none"] -TCL_NO_DEPRECATED = 0 -WARNINGS = -W3 -!else -!if [nmakehlp -f $(CHECKS) "nodep"] -!message *** Doing nodep check -TCL_NO_DEPRECATED = 1 -!else -TCL_NO_DEPRECATED = 0 -!endif -!if [nmakehlp -f $(CHECKS) "fullwarn"] -!message *** Doing full warnings check -WARNINGS = -W4 -!if [nmakehlp -l -warn:3] -LINKERFLAGS = $(LINKERFLAGS) -warn:3 -!endif -!else -WARNINGS = -W3 -!endif -!if [nmakehlp -f $(CHECKS) "64bit"] && [nmakehlp -c -Wp64] -!message *** Doing 64bit portability warnings -WARNINGS = $(WARNINGS) -Wp64 -!endif -!endif - -!if $(PGO) > 1 -!if [nmakehlp -l -ltcg:pgoptimize] -LINKERFLAGS = $(LINKERFLAGS:-ltcg=) -ltcg:pgoptimize -!else -MSG=^ -This compiler does not support profile guided optimization. -!error $(MSG) -!endif -!elseif $(PGO) > 0 -!if [nmakehlp -l -ltcg:pginstrument] -LINKERFLAGS = $(LINKERFLAGS:-ltcg=) -ltcg:pginstrument -!else -MSG=^ -This compiler does not support profile guided optimization. -!error $(MSG) -!endif -!endif - -#---------------------------------------------------------- -# Set our defines now armed with our options. -#---------------------------------------------------------- - -OPTDEFINES = -DTCL_CFGVAL_ENCODING=$(CFG_ENCODING) -DSTDC_HEADERS - -!if $(TCL_MEM_DEBUG) -OPTDEFINES = $(OPTDEFINES) -DTCL_MEM_DEBUG -!endif -!if $(TCL_COMPILE_DEBUG) -OPTDEFINES = $(OPTDEFINES) -DTCL_COMPILE_DEBUG -DTCL_COMPILE_STATS -!endif -!if $(TCL_THREADS) -OPTDEFINES = $(OPTDEFINES) -DTCL_THREADS=1 -!if $(USE_THREAD_ALLOC) -OPTDEFINES = $(OPTDEFINES) -DUSE_THREAD_ALLOC=1 -!endif -!endif -!if $(STATIC_BUILD) -OPTDEFINES = $(OPTDEFINES) -DSTATIC_BUILD -!endif -!if $(TCL_NO_DEPRECATED) -OPTDEFINES = $(OPTDEFINES) -DTCL_NO_DEPRECATED -!endif - -!if !$(DEBUG) -OPTDEFINES = $(OPTDEFINES) -DNDEBUG -!if $(OPTIMIZING) -OPTDEFINES = $(OPTDEFINES) -DTCL_CFG_OPTIMIZED -!endif -!endif -!if $(PROFILE) -OPTDEFINES = $(OPTDEFINES) -DTCL_CFG_PROFILED -!endif -!if "$(MACHINE)" == "IA64" || "$(MACHINE)" == "AMD64" -OPTDEFINES = $(OPTDEFINES) -DTCL_CFG_DO64BIT -!endif -!if $(VCVERSION) < 1300 -OPTDEFINES = $(OPTDEFINES) -DNO_STRTOI64 -!endif - -#---------------------------------------------------------- -# Locate the Tcl headers to build against -#---------------------------------------------------------- - -!if "$(PROJECT)" == "tcl" - -_TCL_H = ..\generic\tcl.h - -!else - -# If INSTALLDIR set to tcl root dir then reset to the lib dir. -!if exist("$(_INSTALLDIR)\include\tcl.h") -_INSTALLDIR=$(_INSTALLDIR)\lib -!endif - -!if !defined(TCLDIR) -!if exist("$(_INSTALLDIR)\..\include\tcl.h") -TCLINSTALL = 1 -_TCLDIR = $(_INSTALLDIR)\.. -_TCL_H = $(_INSTALLDIR)\..\include\tcl.h -TCLDIR = $(_INSTALLDIR)\.. -!else -MSG=^ -Failed to find tcl.h. Set the TCLDIR macro. -!error $(MSG) -!endif -!else -_TCLDIR = $(TCLDIR:/=\) -!if exist("$(_TCLDIR)\include\tcl.h") -TCLINSTALL = 1 -_TCL_H = $(_TCLDIR)\include\tcl.h -!elseif exist("$(_TCLDIR)\generic\tcl.h") -TCLINSTALL = 0 -_TCL_H = $(_TCLDIR)\generic\tcl.h -!else -MSG =^ -Failed to find tcl.h. The TCLDIR macro does not appear correct. -!error $(MSG) -!endif -!endif -!endif - -#-------------------------------------------------------------- -# Extract various version numbers from tcl headers -# The generated file is then included in the makefile. -#-------------------------------------------------------------- - -!if [echo REM = This file is generated from rules.vc > versions.vc] -!endif -!if [echo TCL_MAJOR_VERSION = \>> versions.vc] \ - && [nmakehlp -V "$(_TCL_H)" TCL_MAJOR_VERSION >> versions.vc] -!endif -!if [echo TCL_MINOR_VERSION = \>> versions.vc] \ - && [nmakehlp -V "$(_TCL_H)" TCL_MINOR_VERSION >> versions.vc] -!endif -!if [echo TCL_PATCH_LEVEL = \>> versions.vc] \ - && [nmakehlp -V "$(_TCL_H)" TCL_PATCH_LEVEL >> versions.vc] -!endif - -# If building the tcl core then we need additional package versions -!if "$(PROJECT)" == "tcl" -!if [echo PKG_HTTP_VER = \>> versions.vc] \ - && [nmakehlp -V ..\library\http\pkgIndex.tcl http >> versions.vc] -!endif -!if [echo PKG_TCLTEST_VER = \>> versions.vc] \ - && [nmakehlp -V ..\library\tcltest\pkgIndex.tcl tcltest >> versions.vc] -!endif -!if [echo PKG_MSGCAT_VER = \>> versions.vc] \ - && [nmakehlp -V ..\library\msgcat\pkgIndex.tcl msgcat >> versions.vc] -!endif -!if [echo PKG_PLATFORM_VER = \>> versions.vc] \ - && [nmakehlp -V ..\library\platform\pkgIndex.tcl "platform " >> versions.vc] -!endif -!if [echo PKG_SHELL_VER = \>> versions.vc] \ - && [nmakehlp -V ..\library\platform\pkgIndex.tcl "platform::shell" >> versions.vc] -!endif -!if [echo PKG_DDE_VER = \>> versions.vc] \ - && [nmakehlp -V ..\library\dde\pkgIndex.tcl "dde " >> versions.vc] -!endif -!if [echo PKG_REG_VER =\>> versions.vc] \ - && [nmakehlp -V ..\library\reg\pkgIndex.tcl registry >> versions.vc] -!endif -!endif - -!include versions.vc - -#-------------------------------------------------------------- -# Setup tcl version dependent stuff headers -#-------------------------------------------------------------- - -!if "$(PROJECT)" != "tcl" - -TCL_VERSION = $(TCL_MAJOR_VERSION)$(TCL_MINOR_VERSION) - -!if $(TCL_VERSION) < 81 -TCL_DOES_STUBS = 0 -!else -TCL_DOES_STUBS = 1 -!endif - -!if $(TCLINSTALL) -TCLSH = "$(_TCLDIR)\bin\tclsh$(TCL_VERSION)$(SUFX).exe" -!if !exist($(TCLSH)) && $(TCL_THREADS) -TCLSH = "$(_TCLDIR)\bin\tclsh$(TCL_VERSION)t$(SUFX).exe" -!endif -TCLSTUBLIB = "$(_TCLDIR)\lib\tclstub$(TCL_VERSION).lib" -TCLIMPLIB = "$(_TCLDIR)\lib\tcl$(TCL_VERSION)$(SUFX).lib" -TCL_LIBRARY = $(_TCLDIR)\lib -TCLREGLIB = "$(_TCLDIR)\lib\tclreg13$(SUFX:t=).lib" -TCLDDELIB = "$(_TCLDIR)\lib\tcldde14$(SUFX:t=).lib" -COFFBASE = \must\have\tcl\sources\to\build\this\target -TCLTOOLSDIR = \must\have\tcl\sources\to\build\this\target -TCL_INCLUDES = -I"$(_TCLDIR)\include" -!else -TCLSH = "$(_TCLDIR)\win\$(BUILDDIRTOP)\tclsh$(TCL_VERSION)$(SUFX).exe" -!if !exist($(TCLSH)) && $(TCL_THREADS) -TCLSH = "$(_TCLDIR)\win\$(BUILDDIRTOP)\tclsh$(TCL_VERSION)t$(SUFX).exe" -!endif -TCLSTUBLIB = "$(_TCLDIR)\win\$(BUILDDIRTOP)\tclstub$(TCL_VERSION).lib" -TCLIMPLIB = "$(_TCLDIR)\win\$(BUILDDIRTOP)\tcl$(TCL_VERSION)$(SUFX).lib" -TCL_LIBRARY = $(_TCLDIR)\library -TCLREGLIB = "$(_TCLDIR)\win\$(BUILDDIRTOP)\tclreg13$(SUFX:t=).lib" -TCLDDELIB = "$(_TCLDIR)\win\$(BUILDDIRTOP)\tcldde14$(SUFX:t=).lib" -COFFBASE = "$(_TCLDIR)\win\coffbase.txt" -TCLTOOLSDIR = $(_TCLDIR)\tools -TCL_INCLUDES = -I"$(_TCLDIR)\generic" -I"$(_TCLDIR)\win" -!endif - -!endif - -#------------------------------------------------------------------------- -# Locate the Tk headers to build against -#------------------------------------------------------------------------- - -!if "$(PROJECT)" == "tk" -_TK_H = ..\generic\tk.h -_INSTALLDIR = $(_INSTALLDIR)\.. -!endif - -!ifdef PROJECT_REQUIRES_TK -!if !defined(TKDIR) -!if exist("$(_INSTALLDIR)\..\include\tk.h") -TKINSTALL = 1 -_TKDIR = $(_INSTALLDIR)\.. -_TK_H = $(_TKDIR)\include\tk.h -TKDIR = $(_TKDIR) -!elseif exist("$(_TCLDIR)\include\tk.h") -TKINSTALL = 1 -_TKDIR = $(_TCLDIR) -_TK_H = $(_TKDIR)\include\tk.h -TKDIR = $(_TKDIR) -!endif -!else -_TKDIR = $(TKDIR:/=\) -!if exist("$(_TKDIR)\include\tk.h") -TKINSTALL = 1 -_TK_H = $(_TKDIR)\include\tk.h -!elseif exist("$(_TKDIR)\generic\tk.h") -TKINSTALL = 0 -_TK_H = $(_TKDIR)\generic\tk.h -!else -MSG =^ -Failed to find tk.h. The TKDIR macro does not appear correct. -!error $(MSG) -!endif -!endif -!endif - -#------------------------------------------------------------------------- -# Extract Tk version numbers -#------------------------------------------------------------------------- - -!if defined(PROJECT_REQUIRES_TK) || "$(PROJECT)" == "tk" - -!if [echo TK_MAJOR_VERSION = \>> versions.vc] \ - && [nmakehlp -V $(_TK_H) TK_MAJOR_VERSION >> versions.vc] -!endif -!if [echo TK_MINOR_VERSION = \>> versions.vc] \ - && [nmakehlp -V $(_TK_H) TK_MINOR_VERSION >> versions.vc] -!endif -!if [echo TK_PATCH_LEVEL = \>> versions.vc] \ - && [nmakehlp -V $(_TK_H) TK_PATCH_LEVEL >> versions.vc] -!endif - -!include versions.vc - -TK_DOTVERSION = $(TK_MAJOR_VERSION).$(TK_MINOR_VERSION) -TK_VERSION = $(TK_MAJOR_VERSION)$(TK_MINOR_VERSION) - -!if "$(PROJECT)" != "tk" -!if $(TKINSTALL) -WISH = "$(_TKDIR)\bin\wish$(TK_VERSION)$(SUFX).exe" -TKSTUBLIB = "$(_TKDIR)\lib\tkstub$(TK_VERSION).lib" -TKIMPLIB = "$(_TKDIR)\lib\tk$(TK_VERSION)$(SUFX).lib" -TK_INCLUDES = -I"$(_TKDIR)\include" -!else -WISH = "$(_TKDIR)\win\$(BUILDDIRTOP)\wish$(TCL_VERSION)$(SUFX).exe" -TKSTUBLIB = "$(_TKDIR)\win\$(BUILDDIRTOP)\tkstub$(TCL_VERSION).lib" -TKIMPLIB = "$(_TKDIR)\win\$(BUILDDIRTOP)\tk$(TCL_VERSION)$(SUFX).lib" -TK_INCLUDES = -I"$(_TKDIR)\generic" -I"$(_TKDIR)\win" -I"$(_TKDIR)\xlib" -!endif -!endif - -!endif - -#---------------------------------------------------------- -# Display stats being used. -#---------------------------------------------------------- - -!message *** Intermediate directory will be '$(TMP_DIR)' -!message *** Output directory will be '$(OUT_DIR)' -!message *** Suffix for binaries will be '$(SUFX)' -!message *** Optional defines are '$(OPTDEFINES)' -!message *** Compiler version $(VCVER). Target machine is $(MACHINE) -!message *** Host architecture is $(NATIVE_ARCH) -!message *** Compiler options '$(COMPILERFLAGS) $(OPTIMIZATIONS) $(DEBUGFLAGS) $(WARNINGS)' -!message *** Link options '$(LINKERFLAGS)' - -!endif - diff --git a/autosetup/LICENSE b/autosetup/LICENSE new file mode 100644 index 0000000000..4fe636c9d9 --- /dev/null +++ b/autosetup/LICENSE @@ -0,0 +1,35 @@ +Unless explicitly stated, all files which form part of autosetup +are released under the following license: + +--------------------------------------------------------------------- +autosetup - A build environment "autoconfigurator" + +Copyright (c) 2010-2011, WorkWare Systems + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials + provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE WORKWARE SYSTEMS ``AS IS'' AND ANY +EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A +PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WORKWARE +SYSTEMS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, +INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +The views and conclusions contained in the software and documentation +are those of the authors and should not be interpreted as representing +official policies, either expressed or implied, of WorkWare Systems. diff --git a/autosetup/README.autosetup b/autosetup/README.autosetup new file mode 100644 index 0000000000..3952980480 --- /dev/null +++ b/autosetup/README.autosetup @@ -0,0 +1,11 @@ +README.autosetup created by autosetup v0.7.2 + +This is the autosetup directory for a local install of autosetup. +It contains autosetup, support files and loadable modules. + +*.tcl files in this directory are optional modules which +can be loaded with the 'use' directive. + +*.auto files in this directory are auto-loaded. + +For more information, see https://msteveb.github.io/autosetup/ diff --git a/autosetup/README.md b/autosetup/README.md new file mode 100644 index 0000000000..c8da5c643a --- /dev/null +++ b/autosetup/README.md @@ -0,0 +1,454 @@ +Maintaining Autosetup in the SQLite Tree +======================================================================== + +This document provides some tips and reminders for the SQLite +developers regarding using and maintaining the [Autosetup][]-based +build infrastructure. It is not an [Autosetup][] reference. + +**Table of Contents**: + +- [Autosetup API Reference](#apiref) +- [API Tips](#apitips) +- [Ensuring TCL Compatibility](#tclcompat) +- [Design Conventions](#conventions) + - Symbolic Names of Feature Flags + - Do Not Update Global Shared State +- [Updating Autosetup](#updating) + - ***[Patching Autosetup for Project-local changes](#patching)*** +- [Branch-specific Customization](#branch-customization) + + +------------------------------------------------------------------------ + + +Autosetup API Reference +======================================================================== + +The Autosetup API is quite extensive and can be read either in +the [files in the `autosetup` dir](/dir/autosetup) or using: + +> +``` +$ ./configure --reference | less +``` + +That will include any docs from any TCL files in the `./autosetup` dir +which contain certain (simple) markup defined by autosetup. + +This project's own configuration-related TCL code is spread across the +following files: + +- [proj.tcl][]: project-agnostic utility code for autosetup-driven + projects. This file is designed to be shared between this project, + other projects managed under the SQLite/Hwaci umbrella + (e.g. Fossil), and personal projects of SQLite's developers. It is + essentially an amalgamation of a decade's worth of autosetup-related + utility code. +- [sqlite-config.tcl][]: utility code which is too project-specific + for `proj.tcl`. We split this out of `auto.def` so that it can be + used by both `auto.def` and... +- [auto.def][]: the primary driver for the `./configure` process. + When we talk about "the configure script," we're technically + referring to this file, though it actually contains very little + of the TCL code. +- [autoconf/auto.def][]: the main driver script for the "autoconf" + bundle's configure script. It is essentially a slightly trimmed-down + version of the main `auto.def` file. The `autoconf` dir was ported + from the Autotools to Autosetup in the 3.49.0 dev cycle but retains + the "autoconf" name to minimize downstream disruption. + + + +Autosetup API Tips +======================================================================== + +This section briefly covers only APIs which are frequently useful in +day-to-day maintenance and might not be immediately recognized as such +from a casual perusal of the relevant TCL files. The complete docs of +those with `proj-` prefix can be found in [proj.tcl][] and those with +an `sqlite-` prefix are in [sqlite-config.tcl][]. The others are part +of Autosetup's core packages and are scattered around [the TCL files +in ./autosetup](/dir/autosetup). + +In (mostly) alphabetical order: + +- **`file-isexec filename`**\ + Should be used in place of `[file executable]`, as it will also + check for `${filename}.exe` on Windows platforms. However, on such + platforms it also assumes that _any_ existing file is executable. + +- **`get-env VAR ?default?`**\ + Will fetch an "environment variable" from the first of either: (1) a + KEY=VALUE passed to the configure script or (2) the system's + environment variables. Not to be confused with `getenv`, which only + does the latter and is rarely, if ever, useful in this tree. + - **`proj-get-env VAR ?default?`**\ + Works like `get-env` but will, if that function finds no match, + look for a file named `./.env-$VAR` and, if found, return its + trimmed contents. This can be used, e.g., to set a developer's + local preferences for the default `CFLAGS`.\ + Tip: adding `-O0` to `.env-CFLAGS` reduces rebuild times + considerably at the cost of performance in `make devtest` and the + like. + +- **`proj-fatal msg`**\ + Emits `$msg` to stderr and exits with non-zero. Its differences from + autosetup's `user-error` are purely cosmetic. + +- **`proj-if-opt-truthy flag thenScript ?elseScript?`**\ + Evals `thenScript` if the given `--flag` is truthy, else it + evals the optional `elseScript`. + +- **`proj-indented-notice ?-error? ?-notice? msg`**\ + Breaks its `msg` argument into lines, trims them, and emits them + with consistent indentation. Exactly how it emits depends on the + flags passed to it (or not), as covered in its docs. This will stick + out starkly from normal output and is intended to be used only for + important notices. + +- **`proj-opt-truthy flag`**\ + Returns 1 if `--flag`'s value is "truthy," i.e. one of (1, on, + enabled, yes, true). + +- **`proj-opt-was-provided FLAG`**\ + Returns 1 if `--FLAG` was explicitly provided to configure, + else 0. This distinction can be used to determine, e.g., whether + `--with-readline` was provided or whether we're searching for + readline by default. In the former case, failure to find it should + be treated as fatal, where in the latter case it's not.\ + Unlike most functions which deal with `--flags`, this one does not + validate that `$FLAG` is a registered flag so will not fail fatally + if `$FLAG` is not registered as an Autosetup option. + +- **`proj-val-truthy value`**\ + Returns 1 if `$value` is "truthy," See `proj-opt-truthy` for the definition + of "truthy." + +- **`proj-warn msg`**\ + Emits `$msg` to stderr. Closely-related is autosetup's `user-notice` + (described below). + +- **`sqlite-add-feature-flag ?-shell? FLAG...`**\ + Adds the given feature flag to the CFLAGS which are specific to + building libsqlite3. It's intended to be passed one or more + `-DSQLITE_ENABLE_...`, or similar, flags. If the `-shell` flag is + used then it also passes its arguments to + `sqlite-add-shell-opt`. This is a no-op if `FLAG` is not provided or + is empty. + +- **`sqlite-add-shell-opt FLAG...`**\ + The shell-specific counterpart of `sqlite-add-feature-flag` which + only adds the given flag(s) to the CLI-shell-specific CFLAGS. + +- **`sqlite-configure BUILD-NAME {script}`**\ + This is where all configure `--flags` are defined for all known + build modes ("canonical" or "autoconf"). After processing all flags, + this function runs `$script`, which contains the build-mode-specific + configuration bits, and then runs any finalization bits which are + common to all build modes. The `auto.def` files are intended to contain + exactly two commands: + `use sqlite-config; sqlite-configure BUILD-NAME {script}` + +- **`user-notice msg`**\ + Queues `$msg` to be sent to stderr, but does not emit it until + either `show-notices` is called or the next time autosetup would + output something (it internally calls `show-notices`). This can be + used to generate warnings between a "checking for..." message and + its resulting "yes/no/whatever" message in such a way as to not + spoil the layout of such messages. + + + +Ensuring TCL Compatibility +======================================================================== + +One of the significant benefits of using Autosetup is that (A) this +project uses many TCL scripts in the build process and (B) Autosetup +comes with a TCL interpreter named [JimTCL][]. + +It is important that any TCL files used by the configure process and +makefiles remain compatible with both [JimTCL][] and the canonical +TCL. Though JimTCL has outstanding compatibility with canonical TCL, +it does have a few corners with incompatibilities, e.g. regular +expressions. If a script runs in JimTCL without using any +JimTCL-specific features, then it's a certainty that it will run in +canonical TCL as well. The opposite, however, is not _always_ the +case. + +When [`./configure`](/file/configure) is run, it goes through a +bootstrapping process to find a suitable TCL with which to run the +autosetup framework. The first step involves [finding or building a +TCL shell](/file/autosetup/autosetup-find-tclsh). That will first +search for an available `tclsh` (under several common names, +e.g. `tclsh8.6`) before falling back to compiling the copy of +`jimsh0.c` included in the source tree. i.e. it will prefer to use a +system-installed TCL for running the configure script. Once it finds +(or builds) a TCL shell, it then runs [a sanity test to ensure that +the shell is suitable](/file/autosetup/autosetup-test-tclsh) before +using it to run the main autosetup app. + +There are two simple ways to ensure that running of the configure +process uses JimTCL instead of the canonical `tclsh`, and either +approach provides equally high assurances about configure script +compatibility across TCL implementations: + +1. Build on a system with no `tclsh` installed in the `$PATH`. In that + case, the configure process will fall back to building the in-tree + copy of JimTCL. + +2. Manually build `./jimsh0` in the top of the checkout with:\ + `cc -o jimsh0 autosetup/jimsh0.c`\ + With that in place, the configure script will prefer to use that + before looking for a system-level `tclsh`. Be aware, though, that + `make distclean` will remove that file. + +**Note that `./jimsh0` is distinctly different from the `./jimsh`** +which gets built for code-generation purposes. The latter requires +non-default build flags to enable features which are +platform-dependent, most notably to make its `[file normalize]` work. +This means, for example, that the configure script and its utility +APIs must not use `[file normalize]`, but autosetup provides a +TCL-only implementation of `[file-normalize]` (note the dash) for +portable use in the configure script. Contrariwise, code-generation +scripts invoked via `make` may use `[file normalize]`, as they'll use +`./jimsh` or `tclsh` instead of `./jimsh0`. + + +Known TCL Incompatibilities +------------------------------------------------------------------------ + +A summary of known incompatibilities in JimTCL + +- **CRNL line endings**: prior to 2025-02-05 `fconfigure -translation ...` + was a no-op in JimTCL, and it emits CRNL line endings by default on + Windows. Since then, it supports `-translation binary`, which is + close enough to `-translation lf` for our purposes. When working + with files using the `open` command, it is important to use mode + `"rb"` or `"wb"`, as appropriate, so that the output does not get + CRNL-mangled on Windows. + +- **`file copy`** does not support multiple source files. See + [](/info/61f18c96183867fe) for a workaround. + +- **Regular expressions**: + + - Patterns treat `\nnn` octal values as back-references (which it + does not support). Those can be reformulated as demonstrated in + [](/info/aeac23359bb681c0). + + - `regsub` does not support the `\y` flag. A workaround is demonstrated + in [](/info/c2e5dd791cce3ec4). + + + +Design Conventions +======================================================================== + +This section describes the motivations for the most glaring of the +build's design decisions, in particular how they deviate from +historical, or even widely-conventional, practices. + +Symbolic Names of Feature Flags +------------------------------------------------------------------------ + +Historically, the project's makefile has exclusively used +`UPPER_UNDERSCORE` form for makefile variables. This build, however, +primarily uses `X.y` format, where `X` is often a category label, +e.g. `CFLAGS`, and `y` is the specific instance of that category, +e.g. `CFLAGS.readline`. + +When the configure script exports flags for consumption by filtered +files, e.g. [Makefile.in][] and the generated +`sqlite_cfg.h`, it does so in the more conventional `X_Y` form because +those flags get exported as as C `#define`s to `sqlite_cfg.h`, where +dots are not permitted. + +The `X.y` convention is used in the makefiles primarily because the +person who did the initial port finds that considerably easier on the +eyes and fingers. In practice, the `X_Y` form of such exports is used +exactly once in [Makefile.in][], where it's translated from `@X_Y@` +into into `X.y` form for consumption by [Makefile.in][] and +[main.mk][]. For example: + +> +``` +LDFLAGS.shobj = @SHOBJ_LDFLAGS@ +LDFLAGS.zlib = @LDFLAGS_ZLIB@ +LDFLAGS.math = @LDFLAGS_MATH@ +``` + +(That first one is defined by autosetup, and thus applies "LDFLAGS" as +the suffix rather than the prefix. Which is more legible is a matter +of taste, for which there is no accounting.) + + +Do Not Update Global Shared State +------------------------------------------------------------------------ + +In both the legacy Autotools-driven build and common Autosetup usage, +feature tests performed by the configure script may amend global flags +such as `LIBS`, `LDFLAGS`, and `CFLAGS`[^as-cflags]. That's +appropriate for a makefile which builds a single deliverable, but less +so for makefiles which produce multiple deliverables. Drawbacks of +that approach include: + +- It's unlikely that every single deliverable will require the same + core set of those flags. +- It can be difficult to determine the origin of any given change to + that global state because those changes are hidden behind voodoo + performed outside the immediate visibility of the configure script's + maintainer. +- It can force the maintainers of the configure script to place tests + in a specific order so that the resulting flags get applied at + the correct time and/or in the correct order.\ + (A real-life example: before the approach described below was taken + to collecting build-time flags, the test for `-rpath` had to come + _after_ the test for zlib because the results of the `-rpath` test + implicitly modified global state which broke the zlib feature + test. Because the feature tests no longer (intentionally) modify + shared global state, that is not an issue.) + +In this build, cases where feature tests modify global state in such a +way that it may impact later feature tests are either (A) very +intentionally defined to do so (e.g. the `--with-wasi-sdk` flag has +invasive side-effects) or (B) are oversights (i.e. bugs). + +This tree's [configure script][auto.def], [utility APIs][proj.tcl], +[Makefile.in][], and [main.mk][] therefore strive to separate the +results of any given feature test into its own well-defined +variables. For example: + +- The linker flags for zlib are exported from the configure script as + `LDFLAGS_ZLIB`, which [Makefile.in][] and [main.mk][] then expose as + `LDFLAGS.zlib`. +- `CFLAGS_READLINE` (a.k.a. `CFLAGS.readline`) contains the `CFLAGS` + needed for including `libreadline`, `libedit`, or `linenoise`, and + `LDFLAGS_READLINE` (a.k.a. `LDFLAGS.readline`) is its link-time + counterpart. + +It is then up to the Makefile to apply and order the flags however is +appropriate. + +At the end of the configure script, the global `CFLAGS` _ideally_ +holds only flags which are either relevant to all targets or, failing +that, will have no unintended side-effects on any targets. That said: +clients frequently pass custom `CFLAGS` to `./configure` or `make` to +set library-level feature toggles, e.g. `-DSQLITE_OMIT_FOO`, in which +case there is no practical way to avoid "polluting" the builds of +arbitrary makefile targets with those. _C'est la vie._ + + +[^as-cflags]: But see this article for a detailed discussion of how + autosetup currently deals specifically with CFLAGS: + + + + +Updating Autosetup +======================================================================== + +Updating autosetup is, more often than not, painless. It requires having +a checked-out copy of [the autosetup git repository][autosetup-git]: + +> +``` +$ git clone https://github.com/msteveb/autosetup +$ cd autosetup +# Or, if it's already checked out: +$ git pull +``` + +Then, from the top-most directory of an SQLite checkout: + +> +``` +$ /path/to/autosetup-checkout/autosetup --install . +$ fossil status # show the modified files +``` + +Unless the upgrade made any incompatible changes (which is exceedingly +rare), that's all there is to it. After that's done, **apply a patch +for the change described in the following section**, test the +configure process, and check it in. + + +Patching Autosetup for Project-local Changes +------------------------------------------------------------------------ + +Autosetup reserves the flag name **`--debug`** for its own purposes, +and its own special handling of `--enable-...` flags makes `--debug` +an alias for `--enable-debug`. As this project has a long history of +using `--enable-debug`, we patch autosetup to use the name +`--autosetup-debug` in place of `--debug`. That requires (as of this +writing) four small edits in +[/autosetup/autosetup](/file/autosetup/autosetup), as demonstrated in +[check-in 3296c8d3](/info/3296c8d3). + +If autosetup is upgraded and this patch is _not_ applied the invoking +`./configure` will fail loudly because of the declaration of the +`debug` flag in `auto.def` - duplicated flags are not permitted. + + +Branch-specific Customization +======================================================================== + +Certain vendor-specific branches require slight configure script +customization. Rather than editing `sqlite-config.tcl` for this, +which frequently leads to merge conflicts, the following approach +is recommended: + +In the vendor-specific branch, create a file named +`autosetup/sqlite-custom.tcl`. + +That file should contain the following content... + +If flag customization is required, add: + +> +``` +proc sqlite-custom-flags {} { + # If any existing --flags require different default values + # then call: + options-defaults { + flag-name new-default-value + ... + } + # ^^^ That will replace the default value but will not update + # the --help text, which may lead to some confusion: + # https://github.com/msteveb/autosetup/issues/77 + + return { + {*} { + new-flag-name => {Help text} + ... + } + }; #see below +} +``` + +That function must return either an empty string or a list in the form +used internally by [sqlite-config.tcl][]'s `sqlite-configure`. + +Next, define: + +> +``` +proc sqlite-custom-handle-flags {} { + ... do any custom flag handling here ... +} +``` + +That function, if defined, will be called relatively late in the +configure process, before any filtered files are generated but after +all other significant processing. + + +[Autosetup]: https://msteveb.github.io/autosetup/ +[auto.def]: /file/auto.def +[autoconf/auto.def]: /file/autoconf/auto.def +[autosetup-git]: https://github.com/msteveb/autosetup +[proj.tcl]: /file/autosetup/proj.tcl +[sqlite-config.tcl]: /file/autosetup/sqlite-config.tcl +[Makefile.in]: /file/Makefile.in +[main.mk]: /file/main.mk +[JimTCL]: https://jim.tcl.tk diff --git a/autosetup/autosetup b/autosetup/autosetup new file mode 100755 index 0000000000..c3a31bec58 --- /dev/null +++ b/autosetup/autosetup @@ -0,0 +1,2544 @@ +#!/bin/sh +# Copyright (c) 2006-2011 WorkWare Systems http://www.workware.net.au/ +# All rights reserved +# vim:se syntax=tcl: +# \ +dir=`dirname "$0"`; exec "`$dir/autosetup-find-tclsh`" "$0" "$@" + +# Note that the version has a trailing + on unreleased versions +set autosetup(version) 0.7.2 + +# Can be set to 1 to debug early-init problems +set autosetup(debug) [expr {"--autosetup-debug" in $argv}] + +################################################################## +# +# Main flow of control, option handling +# +proc main {argv} { + global autosetup define + + # There are 3 potential directories involved: + # 1. The directory containing autosetup (this script) + # 2. The directory containing auto.def + # 3. The current directory + + # From this we need to determine: + # a. The path to this script (and related support files) + # b. The path to auto.def + # c. The build directory, where output files are created + + # This is also complicated by the fact that autosetup may + # have been run via the configure wrapper ([getenv WRAPPER] is set) + + # Here are the rules. + # a. This script is $::argv0 + # => dir, prog, exe, libdir + # b. auto.def is in the directory containing the configure wrapper, + # otherwise it is in the current directory. + # => srcdir, autodef + # c. The build directory is the current directory + # => builddir, [pwd] + + # 'misc' is needed before we can do anything, so set a temporary libdir + # in case this is the development version + set autosetup(libdir) [file dirname $::argv0]/lib + use misc + + # (a) + set autosetup(dir) [realdir [file dirname [realpath $::argv0]]] + set autosetup(prog) [file join $autosetup(dir) [file tail $::argv0]] + set autosetup(exe) [getenv WRAPPER $autosetup(prog)] + if {$autosetup(installed)} { + set autosetup(libdir) $autosetup(dir) + } else { + set autosetup(libdir) [file join $autosetup(dir) lib] + } + autosetup_add_dep $autosetup(prog) + + # (b) + if {[getenv WRAPPER ""] eq ""} { + # Invoked directly + set autosetup(srcdir) [pwd] + } else { + # Invoked via the configure wrapper + set autosetup(srcdir) [file-normalize [file dirname $autosetup(exe)]] + } + set autosetup(autodef) [relative-path $autosetup(srcdir)/auto.def] + + # (c) + set autosetup(builddir) [pwd] + + set autosetup(argv) $argv + set autosetup(cmdline) {} + # options is a list of known options + set autosetup(options) {} + # optset is a dictionary of option values set by the user based on getopt + set autosetup(optset) {} + # optdefault is a dictionary of default values + set autosetup(optdefault) {} + # options-defaults is a dictionary of overrides for default values for options + set autosetup(options-defaults) {} + set autosetup(optionhelp) {} + set autosetup(showhelp) 0 + + use util + + # Parse options + use getopt + + # At the is point we don't know what is a valid option + # We simply parse anything that looks like an option + set autosetup(getopt) [getopt argv] + + #"=Core Options:" + options-add { + help:=all => "display help and options. Optional: module name, such as --help=system" + licence license => "display the autosetup license" + version => "display the version of autosetup" + ref:=text manual:=text + reference:=text => "display the autosetup command reference. 'text', 'wiki', 'asciidoc' or 'markdown'" + autosetup-debug => "display debugging output as autosetup runs" + install:=. => "install autosetup to the current or given directory" + } + if {$autosetup(installed)} { + # hidden options so we can produce a nice error + options-add { + sysinstall:path + } + } else { + options-add { + sysinstall:path => "install standalone autosetup to the given directory (e.g.: /usr/local)" + } + } + options-add { + force init:=help => "create initial auto.def, etc. Use --init=help for known types" + # Undocumented options + option-checking=1 + nopager + quiet + timing + conf: + } + + if {[opt-bool version]} { + puts $autosetup(version) + exit 0 + } + + # autosetup --conf=alternate-auto.def + if {[opt-str conf o]} { + set autosetup(autodef) $o + } + + # Debugging output (set this early) + incr autosetup(debug) [opt-bool autosetup-debug] + incr autosetup(force) [opt-bool force] + incr autosetup(msg-quiet) [opt-bool quiet] + incr autosetup(msg-timing) [opt-bool timing] + + # If the local module exists, source it now to allow for + # project-local customisations + if {[file exists $autosetup(libdir)/local.tcl]} { + use local + } + + # Now any auto-load modules + autosetup_load_auto_modules + + if {[opt-str help o]} { + incr autosetup(showhelp) + use help + autosetup_help $o + } + + if {[opt-bool licence license]} { + use help + autosetup_show_license + exit 0 + } + + if {[opt-str {manual ref reference} o]} { + use help + autosetup_reference $o + } + + # Allow combining --install and --init + set earlyexit 0 + if {[opt-str install o]} { + use install + autosetup_install $o + incr earlyexit + } + + if {[opt-str init o]} { + use init + autosetup_init $o + incr earlyexit + } + + if {$earlyexit} { + exit 0 + } + if {[opt-str sysinstall o]} { + use install + autosetup_install $o 1 + exit 0 + } + + if {![file exists $autosetup(autodef)]} { + # Check for invalid option first + options {} + user-error "No auto.def found in \"$autosetup(srcdir)\" (use [file tail $::autosetup(exe)] --init to create one)" + } + + # Parse extra arguments into autosetup(cmdline) + foreach arg $argv { + if {[regexp {([^=]*)=(.*)} $arg -> n v]} { + dict set autosetup(cmdline) $n $v + define $n $v + } else { + user-error "Unexpected parameter: $arg" + } + } + + autosetup_add_dep $autosetup(autodef) + + # Add $argv to CONFIGURE_OPTS + define-append-argv CONFIGURE_OPTS {*}$autosetup(argv) + # Set up AUTOREMAKE to reconfigure with the same args + define-append-argv AUTOREMAKE {*}$autosetup(exe) {*}$autosetup(argv) + + # Log how we were invoked + configlog "Invoked as: [getenv WRAPPER $::argv0] [quote-argv $autosetup(argv)]" + configlog "Tclsh: [info nameofexecutable]" + + # Load auto.def as module "auto.def" + autosetup_load_module auto.def source $autosetup(autodef) + + # Could warn here if options {} was not specified + + show-notices + + if {$autosetup(debug)} { + msg-result "Writing all defines to config.log" + configlog "================ defines ======================" + foreach n [lsort [array names define]] { + configlog "define $n $define($n)" + } + } + + exit 0 +} + +# @section Option Handling + +# @opt-bool ?-nodefault? option ... +# +# Check each of the named, boolean options and if any have been explicitly enabled +# or disabled by the user, return 1 or 0 accordingly. +# +# If the option was specified more than once, the last value wins. +# e.g. With '--enable-foo --disable-foo', '[opt-bool foo]' will return 0 +# +# If no value was specified by the user, returns the default value for the +# first option. If '-nodefault' is given, this behaviour changes and +# -1 is returned instead. +# +proc opt-bool {args} { + set nodefault 0 + if {[lindex $args 0] eq "-nodefault"} { + set nodefault 1 + set args [lrange $args 1 end] + } + option-check-names {*}$args + + foreach opt $args { + if {[dict exists $::autosetup(optset) $opt]} { + return [dict get $::autosetup(optset) $opt] + } + } + + if {$nodefault} { + return -1 + } + # Default value is the default for the first option + return [dict get $::autosetup(optdefault) [lindex $args 0]] +} + +# @opt-val optionlist ?default=""? +# +# Returns a list containing all the values given for the non-boolean options in '$optionlist'. +# There will be one entry in the list for each option given by the user, including if the +# same option was used multiple times. +# +# If no options were set, '$default' is returned (exactly, not as a list). +# +# Note: For most use cases, 'opt-str' should be preferred. +# +proc opt-val {names {default ""}} { + option-check-names {*}$names + + foreach opt $names { + if {[dict exists $::autosetup(optset) $opt]} { + lappend result {*}[dict get $::autosetup(optset) $opt] + } + } + if {[info exists result]} { + return $result + } + return $default +} + +# @opt-str optionlist varname ?default? +# +# Sets '$varname' in the callers scope to the value for one of the given options. +# +# For the list of options given in '$optionlist', if any value is set for any option, +# the option value is taken to be the *last* value of the last option (in the order given). +# +# If no option was given, and a default was specified with 'options-defaults', +# that value is used. +# +# If no 'options-defaults' value was given and '$default' was given, it is used. +# +# If none of the above provided a value, no value is set. +# +# The return value depends on whether '$default' was specified. +# If it was, the option value is returned. +# If it was not, 1 is returns if a value was set, or 0 if not. +# +# Typical usage is as follows: +# +## if {[opt-str {myopt altname} o]} { +## do something with $o +## } +# +# Or: +## define myname [opt-str {myopt altname} o "/usr/local"] +# +proc opt-str {names varname args} { + global autosetup + + option-check-names {*}$names + upvar $varname value + + if {[llength $args]} { + # A default was given, so always return the string value of the option + set default [lindex $args 0] + set retopt 1 + } else { + # No default, so return 0 or 1 to indicate if a value was found + set retopt 0 + } + + foreach opt $names { + if {[dict exists $::autosetup(optset) $opt]} { + set result [lindex [dict get $::autosetup(optset) $opt] end] + } + } + + if {![info exists result]} { + # No user-specified value. Has options-defaults been set? + foreach opt $names { + if {[dict exists $::autosetup(optdefault) $opt]} { + set result [dict get $autosetup(optdefault) $opt] + } + } + } + + if {[info exists result]} { + set value $result + if {$retopt} { + return $value + } + return 1 + } + + if {$retopt} { + set value $default + return $value + } + + return 0 +} + +proc option-check-names {args} { + foreach o $args { + if {$o ni $::autosetup(options)} { + autosetup-error "Request for undeclared option --$o" + } + } +} + +# Parse the option definition in $opts and update +# ::autosetup(setoptions) and ::autosetup(optionhelp) appropriately +# +proc options-add {opts} { + global autosetup + + # First weed out comment lines + set realopts {} + foreach line [split $opts \n] { + if {![string match "#*" [string trimleft $line]]} { + append realopts $line \n + } + } + set opts $realopts + + for {set i 0} {$i < [llength $opts]} {incr i} { + set opt [lindex $opts $i] + if {[string match =* $opt]} { + # This is a special heading + lappend autosetup(optionhelp) [list $opt $autosetup(module)] + continue + } + unset -nocomplain defaultvalue equal value + + #puts "i=$i, opt=$opt" + regexp {^([^:=]*)(:)?(=)?(.*)$} $opt -> name colon equal value + if {$name in $autosetup(options)} { + autosetup-error "Option $name already specified" + } + + #puts "$opt => $name $colon $equal $value" + + # Find the corresponding value in the user options + # and set the default if necessary + if {[string match "-*" $opt]} { + # We no longer support documentation-only options, like "-C " + autosetup-error "Option $opt is not supported" + } elseif {$colon eq ""} { + # Boolean option + lappend autosetup(options) $name + + # Check for override + if {[dict exists $autosetup(options-defaults) $name]} { + # A default was specified with options-defaults, so use it + set value [dict get $autosetup(options-defaults) $name] + } + + if {$value eq "1"} { + set opthelp "--disable-$name" + } else { + set opthelp "--$name" + } + + # Set the default + if {$value eq ""} { + set value 0 + } + set defaultvalue $value + dict set autosetup(optdefault) $name $defaultvalue + + if {[dict exists $autosetup(getopt) $name]} { + # The option was specified by the user. Look at the last value. + lassign [lindex [dict get $autosetup(getopt) $name] end] type setvalue + if {$type eq "str"} { + # Can we convert the value to a boolean? + if {$setvalue in {1 enabled yes}} { + set setvalue 1 + } elseif {$setvalue in {0 disabled no}} { + set setvalue 0 + } else { + user-error "Boolean option $name given as --$name=$setvalue" + } + } + dict set autosetup(optset) $name $setvalue + #puts "Found boolean option --$name=$setvalue" + } + } else { + # String option. + lappend autosetup(options) $name + + if {$equal ne "="} { + # Was the option given as "name:value=default"? + # If so, set $value to the display name and $defaultvalue to the default + # (This is the preferred way to set a default value for a string option) + if {[regexp {^([^=]+)=(.*)$} $value -> value defaultvalue]} { + dict set autosetup(optdefault) $name $defaultvalue + } + } + + # Maybe override the default value + if {[dict exists $autosetup(options-defaults) $name]} { + # A default was specified with options-defaults, so use it + set defaultvalue [dict get $autosetup(options-defaults) $name] + dict set autosetup(optdefault) $name $defaultvalue + } elseif {![info exists defaultvalue]} { + # No default value was given by value=default or options-defaults + # so use the value as the default when the plain option with no + # value is given (.e.g. just --opt instead of --opt=value) + set defaultvalue $value + } + + if {$equal eq "="} { + # String option with optional value + set opthelp "--$name?=$value?" + } else { + # String option with required value + set opthelp "--$name=$value" + } + + # Get the values specified by the user + if {[dict exists $autosetup(getopt) $name]} { + set listvalue {} + + foreach pair [dict get $autosetup(getopt) $name] { + lassign $pair type setvalue + if {$type eq "bool" && $setvalue} { + if {$equal ne "="} { + user-error "Option --$name requires a value" + } + # If given as a boolean, use the default value + set setvalue $defaultvalue + } + lappend listvalue $setvalue + } + + #puts "Found string option --$name=$listvalue" + dict set autosetup(optset) $name $listvalue + } + } + + # Now create the help for this option if appropriate + if {[lindex $opts $i+1] eq "=>"} { + set desc [lindex $opts $i+2] + if {[info exists defaultvalue]} { + set desc [string map [list @default@ $defaultvalue] $desc] + } + # A multi-line description + lappend autosetup(optionhelp) [list $opthelp $autosetup(module) $desc] + incr i 2 + } + } +} + +# @module-options optionlist +# +# Deprecated. Simply use 'options' from within a module. +proc module-options {opts} { + options $opts +} + +proc max {a b} { + expr {$a > $b ? $a : $b} +} + +proc options-wrap-desc {text length firstprefix nextprefix initial} { + set len $initial + set space $firstprefix + foreach word [split $text] { + set word [string trim $word] + if {$word == ""} { + continue + } + if {$len && [string length $space$word] + $len >= $length} { + puts "" + set len 0 + set space $nextprefix + } + incr len [string length $space$word] + puts -nonewline $space$word + set space " " + } + if {$len} { + puts "" + } +} + +# Display options (from $autosetup(optionhelp)) for modules that match +# glob pattern $what +proc options-show {what} { + set local 0 + # Determine the max option width + set max 0 + foreach help $::autosetup(optionhelp) { + lassign $help opt module desc + if {![string match $what $module]} { + continue + } + if {[string match =* $opt] || [string match \n* $desc]} { + continue + } + set max [max $max [string length $opt]] + } + set indent [string repeat " " [expr {$max+4}]] + set cols [getenv COLUMNS 80] + catch { + lassign [exec stty size] _ sttycols + if {[string is integer -strict $sttycols]} { + set cols $sttycols + } + } + incr cols -1 + # Now output + foreach help $::autosetup(optionhelp) { + lassign $help opt module desc + if {![string match $what $module]} { + continue + } + if {$local == 0 && $module eq "auto.def"} { + puts "Local Options:" + incr local + } + if {[string match =* $opt]} { + # Output a special heading line" + puts [string range $opt 1 end] + continue + } + puts -nonewline " [format %-${max}s $opt]" + if {[string match \n* $desc]} { + # Output a pre-formatted help description as-is + puts $desc + } else { + options-wrap-desc [string trim $desc] $cols " " $indent [expr {$max+2}] + } + } +} + +# @options optionspec +# +# Specifies configuration-time options which may be selected by the user +# and checked with 'opt-str' and 'opt-bool'. '$optionspec' contains a series +# of options specifications separated by newlines, as follows: +# +# A boolean option is of the form: +# +## name[=0|1] => "Description of this boolean option" +# +# The default is 'name=0', meaning that the option is disabled by default. +# If 'name=1' is used to make the option enabled by default, the description should reflect +# that with text like "Disable support for ...". +# +# An argument option (one which takes a parameter) is of one of the following forms: +# +## name:value => "Description of this option" +## name:value=default => "Description of this option with a default value" +## name:=value => "Description of this option with an optional value" +# +# If the 'name:value' form is used, the value must be provided with the option (as '--name=myvalue'). +# If the 'name:value=default' form is used, the option has the given default value even if not +# specified by the user. +# If the 'name:=value' form is used, the value is optional and the given value is used +# if it is not provided. +# +# The description may contain '@default@', in which case it will be replaced with the default +# value for the option (taking into account defaults specified with 'options-defaults'. +# +# Undocumented options are also supported by omitting the '=> description'. +# These options are not displayed with '--help' and can be useful for internal options or as aliases. +# +# For example, '--disable-lfs' is an alias for '--disable=largefile': +# +## lfs=1 largefile=1 => "Disable large file support" +# +proc options {optlist} { + global autosetup + + options-add $optlist + + if {$autosetup(showhelp)} { + # If --help, stop now to show help + return -code break + } + + if {$autosetup(module) eq "auto.def"} { + # Check for invalid options + if {[opt-bool option-checking]} { + foreach o [dict keys $::autosetup(getopt)] { + if {$o ni $::autosetup(options)} { + user-error "Unknown option --$o" + } + } + } + } +} + +# @options-defaults dictionary +# +# Specifies a dictionary of options and a new default value for each of those options. +# Use before any 'use' statements in 'auto.def' to change the defaults for +# subsequently included modules. +proc options-defaults {dict} { + foreach {n v} $dict { + dict set ::autosetup(options-defaults) $n $v + } +} + +proc config_guess {} { + if {[file-isexec $::autosetup(dir)/autosetup-config.guess]} { + if {[catch {exec-with-stderr sh $::autosetup(dir)/autosetup-config.guess} alias]} { + user-error $alias + } + return $alias + } else { + configlog "No autosetup-config.guess, so using uname" + string tolower [exec uname -p]-unknown-[exec uname -s][exec uname -r] + } +} + +proc config_sub {alias} { + if {[file-isexec $::autosetup(dir)/autosetup-config.sub]} { + if {[catch {exec-with-stderr sh $::autosetup(dir)/autosetup-config.sub $alias} alias]} { + user-error $alias + } + } + return $alias +} + +# @section Variable Definitions (defines) + +# @define name ?value=1? +# +# Defines the named variable to the given value. +# These (name, value) pairs represent the results of the configuration check +# and are available to be subsequently checked, modified and substituted. +# +proc define {name {value 1}} { + set ::define($name) $value + #dputs "$name <= $value" +} + +# @define-push {name ...} script +# +# Save the values of the given defines, evaluation the script, then restore. +# For example, to avoid updating AS_FLAGS and AS_CXXFLAGS: +## define-push {AS_CFLAGS AS_CXXFLAGS} { +## cc-check-flags -Wno-error +## } +proc define-push {names script} { + array set unset {} + foreach name $names { + if {[is-defined $name]} { + set save($name) [get-define $name] + } else { + set unset($name) 1 + } + } + uplevel 1 $script + array set ::define [array get save] + foreach name [array names unset] { + unset -nocomplain ::define($name) + } +} + +# @undefine name +# +# Undefine the named variable. +# +proc undefine {name} { + unset -nocomplain ::define($name) + #dputs "$name <= " +} + +# @define-append name value ... +# +# Appends the given value(s) to the given "defined" variable. +# If the variable is not defined or empty, it is set to '$value'. +# Otherwise the value is appended, separated by a space. +# Any extra values are similarly appended. +# +# Note that define-append is not designed to add values containing spaces. +# If values may contain spaces, consider define-append-argv instead. +# +proc define-append {name args} { + if {[get-define $name ""] ne ""} { + foreach arg $args { + if {$arg eq ""} { + continue + } + append ::define($name) " " $arg + } + } else { + set ::define($name) [join $args] + } + #dputs "$name += [join $args] => $::define($name)" +} + +# @define-append-argv name value ... +# +# Similar to define-append except designed to construct shell command +# lines, including correct handling of parameters with spaces. +# +# Each non-empty value is quoted if necessary and then appended to the given variable +# if it does not already exist. +# +proc define-append-argv {name args} { + set seen {} + set new {} + foreach val [list {*}[get-define $name ""] {*}$args] { + if {$val ne {} && ![dict exists $seen $val]} { + lappend new [quote-if-needed $val] + dict set seen $val 1 + } + } + set ::define($name) [join $new " "] + #dputs "$name += [join $args] => $::define($name)" +} + +# @get-define name ?default=0? +# +# Returns the current value of the "defined" variable, or '$default' +# if not set. +# +proc get-define {name {default 0}} { + if {[info exists ::define($name)]} { + #dputs "$name => $::define($name)" + return $::define($name) + } + #dputs "$name => $default" + return $default +} + +# @is-defined name +# +# Returns 1 if the given variable is defined. +# +proc is-defined {name} { + info exists ::define($name) +} + +# @is-define-set name +# +# Returns 1 if the given variable is defined and is set +# to a value other than "" or 0 +# +proc is-define-set {name} { + if {[get-define $name] in {0 ""}} { + return 0 + } + return 1 +} + +# @all-defines +# +# Returns a dictionary (name, value list) of all defined variables. +# +# This is suitable for use with 'dict', 'array set' or 'foreach' +# and allows for arbitrary processing of the defined variables. +# +proc all-defines {} { + array get ::define +} + +# @section Environment/Helpers + +# @get-env name default +# +# If '$name' was specified on the command line, return it. +# Otherwise if '$name' was set in the environment, return it. +# Otherwise return '$default'. +# +proc get-env {name default} { + if {[dict exists $::autosetup(cmdline) $name]} { + return [dict get $::autosetup(cmdline) $name] + } + getenv $name $default +} + +# @env-is-set name +# +# Returns 1 if '$name' was specified on the command line or in the environment. +# Note that an empty environment variable is not considered to be set. +# +proc env-is-set {name} { + if {[dict exists $::autosetup(cmdline) $name]} { + return 1 + } + if {[getenv $name ""] ne ""} { + return 1 + } + return 0 +} + +# @readfile filename ?default=""? +# +# Return the contents of the file, without the trailing newline. +# If the file doesn't exist or can't be read, returns '$default'. +# +proc readfile {filename {default_value ""}} { + set result $default_value + catch { + set f [open $filename] + set result [read -nonewline $f] + close $f + } + return $result +} + +# @writefile filename value +# +# Creates the given file containing '$value'. +# Does not add an extra newline. +# +proc writefile {filename value} { + set f [open $filename w] + puts -nonewline $f $value + close $f +} + +proc quote-if-needed {str} { + if {[string match {*[\" ]*} $str]} { + return \"[string map [list \" \\" \\ \\\\] $str]\" + } + return $str +} + +proc quote-argv {argv} { + set args {} + foreach arg $argv { + lappend args [quote-if-needed $arg] + } + join $args +} + +# @list-non-empty list +# +# Returns a copy of the given list with empty elements removed +proc list-non-empty {list} { + set result {} + foreach p $list { + if {$p ne ""} { + lappend result $p + } + } + return $result +} + +# @section Paths, Searching + +# @find-executable-path name +# +# Searches the path for an executable with the given name. +# Note that the name may include some parameters, e.g. 'cc -mbig-endian', +# in which case the parameters are ignored. +# Returns the full path to the executable if found, or "" if not found. +# +proc find-executable-path {name} { + # Ignore any parameters + set name [lindex $name 0] + # The empty string is never a valid executable + if {$name ne ""} { + foreach p [split-path] { + dputs "Looking for $name in $p" + set exec [file join $p $name] + if {[file-isexec $exec]} { + dputs "Found $name -> $exec" + return $exec + } + } + } + return {} +} + +# @find-executable name +# +# Searches the path for an executable with the given name. +# Note that the name may include some parameters, e.g. 'cc -mbig-endian', +# in which case the parameters are ignored. +# Returns 1 if found, or 0 if not. +# +proc find-executable {name} { + if {[find-executable-path $name] eq {}} { + return 0 + } + return 1 +} + +# @find-an-executable ?-required? name ... +# +# Given a list of possible executable names, +# searches for one of these on the path. +# +# Returns the name found, or "" if none found. +# If the first parameter is '-required', an error is generated +# if no executable is found. +# +proc find-an-executable {args} { + set required 0 + if {[lindex $args 0] eq "-required"} { + set args [lrange $args 1 end] + incr required + } + foreach name $args { + if {[find-executable $name]} { + return $name + } + } + if {$required} { + if {[llength $args] == 1} { + user-error "failed to find: [join $args]" + } else { + user-error "failed to find one of: [join $args]" + } + } + return "" +} + +# @section Logging, Messages and Errors + +# @configlog msg +# +# Writes the given message to the configuration log, 'config.log'. +# +proc configlog {msg} { + if {![info exists ::autosetup(logfh)]} { + set ::autosetup(logfh) [open config.log w] + } + puts $::autosetup(logfh) $msg +} + +# @msg-checking msg +# +# Writes the message with no newline to stdout. +# +proc msg-checking {msg} { + if {$::autosetup(msg-quiet) == 0} { + maybe-show-timestamp + puts -nonewline $msg + set ::autosetup(msg-checking) 1 + } +} + +# @msg-result msg +# +# Writes the message to stdout. +# +proc msg-result {msg} { + if {$::autosetup(msg-quiet) == 0} { + maybe-show-timestamp + puts $msg + set ::autosetup(msg-checking) 0 + show-notices + } +} + +# @msg-quiet command ... +# +# 'msg-quiet' evaluates it's arguments as a command with output +# from 'msg-checking' and 'msg-result' suppressed. +# +# This is useful if a check needs to run a subcheck which isn't +# of interest to the user. +proc msg-quiet {args} { + incr ::autosetup(msg-quiet) + set rc [uplevel 1 $args] + incr ::autosetup(msg-quiet) -1 + return $rc +} + +# Will be overridden by 'use misc' +proc error-stacktrace {msg} { + return $msg +} + +proc error-location {msg} { + return $msg +} + +################################################################## +# +# Debugging output +# +proc dputs {msg} { + if {$::autosetup(debug)} { + puts $msg + } +} + +################################################################## +# +# User and system warnings and errors +# +# Usage errors such as wrong command line options + +# @user-error msg +# +# Indicate incorrect usage to the user, including if required components +# or features are not found. +# 'autosetup' exits with a non-zero return code. +# +proc user-error {msg} { + show-notices + puts stderr "Error: $msg" + puts stderr "Try: '[file tail $::autosetup(exe)] --help' for options" + exit 1 +} + +# @user-notice msg +# +# Output the given message to stderr. +# +proc user-notice {msg} { + lappend ::autosetup(notices) $msg +} + +# Incorrect usage in the auto.def file. Identify the location. +proc autosetup-error {msg} { + autosetup-full-error [error-location $msg] +} + +# Like autosetup-error, except $msg is the full error message. +proc autosetup-full-error {msg} { + show-notices + puts stderr $msg + exit 1 +} + +proc show-notices {} { + if {$::autosetup(msg-checking)} { + puts "" + set ::autosetup(msg-checking) 0 + } + flush stdout + if {[info exists ::autosetup(notices)]} { + puts stderr [join $::autosetup(notices) \n] + unset ::autosetup(notices) + } +} + +proc maybe-show-timestamp {} { + if {$::autosetup(msg-timing) && $::autosetup(msg-checking) == 0} { + puts -nonewline [format {[%6.2f] } [expr {([clock millis] - $::autosetup(start)) % 10000 / 1000.0}]] + } +} + +# @autosetup-require-version required +# +# Checks the current version of 'autosetup' against '$required'. +# A fatal error is generated if the current version is less than that required. +# +proc autosetup-require-version {required} { + if {[compare-versions $::autosetup(version) $required] < 0} { + user-error "autosetup version $required is required, but this is $::autosetup(version)" + } +} + +proc autosetup_version {} { + return "autosetup v$::autosetup(version)" +} + +################################################################## +# +# Directory/path handling +# + +proc realdir {dir} { + set oldpwd [pwd] + cd $dir + set pwd [pwd] + cd $oldpwd + return $pwd +} + +# Follow symlinks until we get to something which is not a symlink +proc realpath {path} { + while {1} { + if {[catch { + set path [file readlink $path] + }]} { + # Not a link + break + } + } + return $path +} + +# Convert absolute path, $path into a path relative +# to the given directory (or the current dir, if not given). +# +proc relative-path {path {pwd {}}} { + set diff 0 + set same 0 + set newf {} + set prefix {} + set path [file-normalize $path] + if {$pwd eq ""} { + set pwd [pwd] + } else { + set pwd [file-normalize $pwd] + } + + if {$path eq $pwd} { + return . + } + + # Try to make the filename relative to the current dir + foreach p [split $pwd /] f [split $path /] { + if {$p ne $f} { + incr diff + } elseif {!$diff} { + incr same + } + if {$diff} { + if {$p ne ""} { + # Add .. for sibling or parent dir + lappend prefix .. + } + if {$f ne ""} { + lappend newf $f + } + } + } + if {$same == 1 || [llength $prefix] > 3} { + return $path + } + + file join [join $prefix /] [join $newf /] +} + +# Add filename as a dependency to rerun autosetup +# The name will be normalised (converted to a full path) +# +proc autosetup_add_dep {filename} { + lappend ::autosetup(deps) [file-normalize $filename] +} + +# @section Modules Support + +################################################################## +# +# Library module support +# + +# @use module ... +# +# Load the given library modules. +# e.g. 'use cc cc-shared' +# +# Note that module 'X' is implemented in either 'autosetup/X.tcl' +# or 'autosetup/X/init.tcl' +# +# The latter form is useful for a complex module which requires additional +# support file. In this form, '$::usedir' is set to the module directory +# when it is loaded. +# +proc use {args} { + global autosetup libmodule modsource + + set dirs [list $autosetup(libdir)] + if {[info exists autosetup(srcdir)]} { + lappend dirs $autosetup(srcdir)/autosetup + } + foreach m $args { + if {[info exists libmodule($m)]} { + continue + } + set libmodule($m) 1 + + if {[info exists modsource(${m}.tcl)]} { + autosetup_load_module $m eval $modsource(${m}.tcl) + } else { + set locs [list ${m}.tcl ${m}/init.tcl] + set found 0 + foreach dir $dirs { + foreach loc $locs { + set source $dir/$loc + if {[file exists $source]} { + incr found + break + } + } + if {$found} { + break + } + } + if {$found} { + # For the convenience of the "use" source, point to the directory + # it is being loaded from + set ::usedir [file dirname $source] + autosetup_load_module $m source $source + autosetup_add_dep $source + } else { + autosetup-error "use: No such module: $m" + } + } + } +} + +proc autosetup_load_auto_modules {} { + global autosetup modsource + # First load any embedded auto modules + foreach mod [array names modsource *.auto] { + autosetup_load_module $mod eval $modsource($mod) + } + # Now any external auto modules + foreach file [glob -nocomplain $autosetup(libdir)/*.auto $autosetup(libdir)/*/*.auto] { + autosetup_load_module [file tail $file] source $file + } +} + +# Load module source in the global scope by executing the given command +proc autosetup_load_module {module args} { + global autosetup + set prev $autosetup(module) + set autosetup(module) $module + + if {[catch [list uplevel #0 $args] msg opts] ni {0 2 3}} { + autosetup-full-error [error-dump $msg $opts $::autosetup(debug)] + } + set autosetup(module) $prev +} + +# Initial settings +set autosetup(exe) $::argv0 +set autosetup(istcl) 1 +set autosetup(start) [clock millis] +set autosetup(installed) 0 +set autosetup(sysinstall) 0 +set autosetup(msg-checking) 0 +set autosetup(msg-quiet) 0 +set autosetup(inittypes) {} +set autosetup(module) autosetup + +# Embedded modules are inserted below here +set autosetup(installed) 1 +set autosetup(sysinstall) 0 +# ----- @module asciidoc-formatting.tcl ----- + +set modsource(asciidoc-formatting.tcl) { +# Copyright (c) 2010 WorkWare Systems http://www.workware.net.au/ +# All rights reserved + +# Module which provides text formatting +# asciidoc format + +use formatting + +proc para {text} { + regsub -all "\[ \t\n\]+" [string trim $text] " " +} +proc title {text} { + underline [para $text] = + nl +} +proc p {text} { + puts [para $text] + nl +} +proc code {text} { + foreach line [parse_code_block $text] { + puts " $line" + } + nl +} +proc codelines {lines} { + foreach line $lines { + puts " $line" + } + nl +} +proc nl {} { + puts "" +} +proc underline {text char} { + regexp "^(\[ \t\]*)(.*)" $text -> indent words + puts $text + puts $indent[string repeat $char [string length $words]] +} +proc section {text} { + underline "[para $text]" - + nl +} +proc subsection {text} { + underline "$text" ~ + nl +} +proc bullet {text} { + puts "* [para $text]" +} +proc indent {text} { + puts " :: " + puts [para $text] +} +proc defn {first args} { + set sep "" + if {$first ne ""} { + puts "${first}::" + } else { + puts " :: " + } + set defn [string trim [join $args \n]] + regsub -all "\n\n" $defn "\n ::\n" defn + puts $defn +} +} + +# ----- @module formatting.tcl ----- + +set modsource(formatting.tcl) { +# Copyright (c) 2010 WorkWare Systems http://www.workware.net.au/ +# All rights reserved + +# Module which provides common text formatting + +# This is designed for documentation which looks like: +# code {...} +# or +# code { +# ... +# ... +# } +# In the second case, we need to work out the indenting +# and strip it from all lines but preserve the remaining indenting. +# Note that all lines need to be indented with the same initial +# spaces/tabs. +# +# Returns a list of lines with the indenting removed. +# +proc parse_code_block {text} { + # If the text begins with newline, take the following text, + # otherwise just return the original + if {![regexp "^\n(.*)" $text -> text]} { + return [list [string trim $text]] + } + + # And trip spaces off the end + set text [string trimright $text] + + set min 100 + # Examine each line to determine the minimum indent + foreach line [split $text \n] { + if {$line eq ""} { + # Ignore empty lines for the indent calculation + continue + } + regexp "^(\[ \t\]*)" $line -> indent + set len [string length $indent] + if {$len < $min} { + set min $len + } + } + + # Now make a list of lines with this indent removed + set lines {} + foreach line [split $text \n] { + lappend lines [string range $line $min end] + } + + # Return the result + return $lines +} +} + +# ----- @module getopt.tcl ----- + +set modsource(getopt.tcl) { +# Copyright (c) 2006 WorkWare Systems http://www.workware.net.au/ +# All rights reserved + +# Simple getopt module + +# Parse everything out of the argv list which looks like an option +# Everything which doesn't look like an option, or is after --, is left unchanged +# Understands --enable-xxx as a synonym for --xxx to enable the boolean option xxx. +# Understands --disable-xxx to disable the boolean option xxx. +# +# The returned value is a dictionary keyed by option name +# Each value is a list of {type value} ... where type is "bool" or "str". +# The value for a boolean option is 0 or 1. The value of a string option is the value given. +proc getopt {argvname} { + upvar $argvname argv + set nargv {} + + set opts {} + + for {set i 0} {$i < [llength $argv]} {incr i} { + set arg [lindex $argv $i] + + #dputs arg=$arg + + if {$arg eq "--"} { + # End of options + incr i + lappend nargv {*}[lrange $argv $i end] + break + } + + if {[regexp {^--([^=][^=]+)=(.*)$} $arg -> name value]} { + # --name=value + dict lappend opts $name [list str $value] + } elseif {[regexp {^--(enable-|disable-)?([^=]*)$} $arg -> prefix name]} { + if {$prefix in {enable- ""}} { + set value 1 + } else { + set value 0 + } + dict lappend opts $name [list bool $value] + } else { + lappend nargv $arg + } + } + + #puts "getopt: argv=[join $argv] => [join $nargv]" + #array set getopt $opts + #parray getopt + + set argv $nargv + + return $opts +} +} + +# ----- @module help.tcl ----- + +set modsource(help.tcl) { +# Copyright (c) 2010 WorkWare Systems http://workware.net.au/ +# All rights reserved + +# Module which provides usage, help and the command reference + +proc autosetup_help {what} { + use_pager + + puts "Usage: [file tail $::autosetup(exe)] \[options\] \[settings\]\n" + puts "This is [autosetup_version], a build environment \"autoconfigurator\"" + puts "See the documentation online at https://msteveb.github.io/autosetup/\n" + + if {$what in {all local}} { + # Need to load auto.def now + if {[file exists $::autosetup(autodef)]} { + # Load auto.def as module "auto.def" + autosetup_load_module auto.def source $::autosetup(autodef) + } + if {$what eq "all"} { + set what * + } else { + set what auto.def + } + } else { + use $what + puts "Options for module $what:" + } + options-show $what + exit 0 +} + +proc autosetup_show_license {} { + global modsource autosetup + use_pager + + if {[info exists modsource(LICENSE)]} { + puts $modsource(LICENSE) + return + } + foreach dir [list $autosetup(libdir) $autosetup(srcdir)] { + set path [file join $dir LICENSE] + if {[file exists $path]} { + puts [readfile $path] + return + } + } + puts "LICENSE not found" +} + +# If not already paged and stdout is a tty, pipe the output through the pager +# This is done by reinvoking autosetup with --nopager added +proc use_pager {} { + if {![opt-bool nopager] && [getenv PAGER ""] ne "" && [isatty? stdin] && [isatty? stdout]} { + if {[catch { + exec [info nameofexecutable] $::argv0 --nopager {*}$::argv |& {*}[getenv PAGER] >@stdout <@stdin 2>@stderr + } msg opts] == 1} { + if {[dict get $opts -errorcode] eq "NONE"} { + # an internal/exec error + puts stderr $msg + exit 1 + } + } + exit 0 + } +} + +# Outputs the autosetup references in one of several formats +proc autosetup_reference {{type text}} { + + use_pager + + switch -glob -- $type { + wiki {use wiki-formatting} + ascii* {use asciidoc-formatting} + md - markdown {use markdown-formatting} + default {use text-formatting} + } + + title "[autosetup_version] -- Command Reference" + + section {Introduction} + + p { + See https://msteveb.github.io/autosetup/ for the online documentation for 'autosetup'. + This documentation can also be accessed locally with `autosetup --ref`. + } + + p { + 'autosetup' provides a number of built-in commands which + are documented below. These may be used from 'auto.def' to test + for features, define variables, create files from templates and + other similar actions. + } + + automf_command_reference + + exit 0 +} + +proc autosetup_output_block {type lines} { + if {[llength $lines]} { + switch $type { + section { + section $lines + } + subsection { + subsection $lines + } + code { + codelines $lines + } + p { + p [join $lines] + } + list { + foreach line $lines { + bullet $line + } + nl + } + } + } +} + +# Generate a command reference from inline documentation +proc automf_command_reference {} { + lappend files $::autosetup(prog) + lappend files {*}[lsort [glob -nocomplain $::autosetup(libdir)/{*/*.tcl,*.tcl}]] + + # We want to process all non-module files before module files + # and then modules in alphabetical order. + # So examine all files and extract docs into doc($modulename) and doc(_core_) + # + # Each entry is a list of {type data} where $type is one of: section, subsection, code, list, p + # and $data is a string for section, subsection or a list of text lines for other types. + + # XXX: Should commands be in alphabetical order too? Currently they are in file order. + + set doc(_core_) {} + lappend doc(_core_) [list section "Core Commands"] + + foreach file $files { + set modulename [file rootname [file tail $file]] + set current _core_ + set f [open $file] + while {![eof $f]} { + set line [gets $f] + + if {[regexp {^#.*@section (.*)$} $line -> section]} { + lappend doc($current) [list section $section] + continue + } + + # Find embedded module names + if {[regexp {^#.*@module ([^ ]*)} $line -> modulename]} { + continue + } + + # Find lines starting with "# @*" and continuing through the remaining comment lines + if {![regexp {^# @(.*)} $line -> cmd]} { + continue + } + + # Synopsis or command? + if {$cmd eq "synopsis:"} { + set current $modulename + lappend doc($current) [list section "Module: $modulename"] + } else { + lappend doc($current) [list subsection $cmd] + } + + set lines {} + set type p + + # Now the description + while {![eof $f]} { + set line [gets $f] + + if {![regexp {^#(#)? ?(.*)} $line -> hash cmd]} { + break + } + if {$hash eq "#"} { + set t code + } elseif {[regexp {^- (.*)} $cmd -> cmd]} { + set t list + } else { + set t p + } + + #puts "hash=$hash, oldhash=$oldhash, lines=[llength $lines], cmd=$cmd" + + if {$t ne $type || $cmd eq ""} { + # Finish the current block + lappend doc($current) [list $type $lines] + set lines {} + set type $t + } + if {$cmd ne ""} { + lappend lines $cmd + } + } + + lappend doc($current) [list $type $lines] + } + close $f + } + + # Now format and output the results + + # _core_ will sort first + foreach module [lsort [array names doc]] { + foreach item $doc($module) { + autosetup_output_block {*}$item + } + } +} +} + +# ----- @module init.tcl ----- + +set modsource(init.tcl) { +# Copyright (c) 2010 WorkWare Systems http://www.workware.net.au/ +# All rights reserved + +# Module to help create auto.def and configure + +proc autosetup_init {type} { + set help 0 + if {$type in {? help}} { + incr help + } elseif {![dict exists $::autosetup(inittypes) $type]} { + puts "Unknown type, --init=$type" + incr help + } + if {$help} { + puts "Use one of the following types (e.g. --init=make)\n" + foreach type [lsort [dict keys $::autosetup(inittypes)]] { + lassign [dict get $::autosetup(inittypes) $type] desc + # XXX: Use the options-show code to wrap the description + puts [format "%-10s %s" $type $desc] + } + return + } + lassign [dict get $::autosetup(inittypes) $type] desc script + + puts "Initialising $type: $desc\n" + + # All initialisations happens in the top level srcdir + cd $::autosetup(srcdir) + + uplevel #0 $script +} + +proc autosetup_add_init_type {type desc script} { + dict set ::autosetup(inittypes) $type [list $desc $script] +} + +# This is for in creating build-system init scripts +# +# If the file doesn't exist, create it containing $contents +# If the file does exist, only overwrite if --force is specified. +# +proc autosetup_check_create {filename contents} { + if {[file exists $filename]} { + if {!$::autosetup(force)} { + puts "I see $filename already exists." + return + } else { + puts "I will overwrite the existing $filename because you used --force." + } + } else { + puts "I don't see $filename, so I will create it." + } + writefile $filename $contents +} +} + +# ----- @module install.tcl ----- + +set modsource(install.tcl) { +# Copyright (c) 2006-2010 WorkWare Systems http://www.workware.net.au/ +# All rights reserved + +# Module which can install autosetup + +# autosetup(installed)=1 means that autosetup is not running from source +# autosetup(sysinstall)=1 means that autosetup is running from a sysinstall version +# shared=1 means that we are trying to do a sysinstall. This is only possible from the development source. + +proc autosetup_install {dir {shared 0}} { + global autosetup + if {$shared} { + if {$autosetup(installed) || $autosetup(sysinstall)} { + user-error "Can only --sysinstall from development sources" + } + } elseif {$autosetup(installed) && !$autosetup(sysinstall)} { + user-error "Can't --install from project install" + } + + if {$autosetup(sysinstall)} { + # This is the sysinstall version, so install just uses references + cd $dir + + puts "[autosetup_version] creating configure to use system-installed autosetup" + autosetup_create_configure 1 + puts "Creating autosetup/README.autosetup" + file mkdir autosetup + autosetup_install_readme autosetup/README.autosetup 1 + return + } + + if {[catch { + if {$shared} { + set target $dir/bin/autosetup + set installedas $target + } else { + if {$dir eq "."} { + set installedas autosetup + } else { + set installedas $dir/autosetup + } + cd $dir + file mkdir autosetup + set target autosetup/autosetup + } + set targetdir [file dirname $target] + file mkdir $targetdir + + set f [open $target w] + + set publicmodules {} + + # First the main script, but only up until "CUT HERE" + set in [open $autosetup(dir)/autosetup] + while {[gets $in buf] >= 0} { + if {$buf ne "##-- CUT HERE --##"} { + puts $f $buf + continue + } + + # Insert the static modules here + # i.e. those which don't contain @synopsis: + # All modules are inserted if $shared is set + puts $f "set autosetup(installed) 1" + puts $f "set autosetup(sysinstall) $shared" + foreach file [lsort [glob $autosetup(libdir)/*.{tcl,auto}]] { + set modname [file tail $file] + set ext [file ext $modname] + set buf [readfile $file] + if {!$shared} { + if {$ext eq ".auto" || [string match "*\n# @synopsis:*" $buf]} { + lappend publicmodules $file + continue + } + } + dputs "install: importing lib/[file tail $file]" + puts $f "# ----- @module $modname -----" + puts $f "\nset modsource($modname) \{" + puts $f $buf + puts $f "\}\n" + } + if {$shared} { + foreach {srcname destname} [list $autosetup(libdir)/README.autosetup-lib README.autosetup \ + $autosetup(srcdir)/LICENSE LICENSE] { + dputs "install: importing $srcname as $destname" + puts $f "\nset modsource($destname) \\\n[list [readfile $srcname]\n]\n" + } + } + } + close $in + close $f + catch {exec chmod 755 $target} + + set installfiles {autosetup-config.guess autosetup-config.sub autosetup-test-tclsh} + set removefiles {} + + if {!$shared} { + autosetup_install_readme $targetdir/README.autosetup 0 + + # Install public modules + foreach file $publicmodules { + set tail [file tail $file] + autosetup_install_file $file $targetdir/$tail + } + lappend installfiles jimsh0.c autosetup-find-tclsh LICENSE + lappend removefiles config.guess config.sub test-tclsh find-tclsh + } else { + lappend installfiles {sys-find-tclsh autosetup-find-tclsh} + } + + # Install support files + foreach fileinfo $installfiles { + if {[llength $fileinfo] == 2} { + lassign $fileinfo source dest + } else { + lassign $fileinfo source + set dest $source + } + autosetup_install_file $autosetup(dir)/$source $targetdir/$dest + } + + # Remove obsolete files + foreach file $removefiles { + if {[file exists $targetdir/$file]} { + file delete $targetdir/$file + } + } + } error]} { + user-error "Failed to install autosetup: $error" + } + if {$shared} { + set type "system" + } else { + set type "local" + } + puts "Installed $type [autosetup_version] to $installedas" + + if {!$shared} { + # Now create 'configure' if necessary + autosetup_create_configure 0 + } +} + +proc autosetup_create_configure {shared} { + if {[file exists configure]} { + if {!$::autosetup(force)} { + # Could this be an autosetup configure? + if {![string match "*\nWRAPPER=*" [readfile configure]]} { + puts "I see configure, but not created by autosetup, so I won't overwrite it." + puts "Remove it or use --force to overwrite." + return + } + } else { + puts "I will overwrite the existing configure because you used --force." + } + } else { + puts "I don't see configure, so I will create it." + } + if {$shared} { + writefile configure \ +{#!/bin/sh +WRAPPER="$0"; export WRAPPER; "autosetup" "$@" +} + } else { + writefile configure \ +{#!/bin/sh +dir="`dirname "$0"`/autosetup" +#@@INITCHECK@@# +WRAPPER="$0"; export WRAPPER; exec "`"$dir/autosetup-find-tclsh"`" "$dir/autosetup" "$@" +} + } + catch {exec chmod 755 configure} +} + +# Append the contents of $file to filehandle $f +proc autosetup_install_append {f file} { + dputs "install: include $file" + set in [open $file] + puts $f [read $in] + close $in +} + +proc autosetup_install_file {source target} { + dputs "install: $source => $target" + if {![file exists $source]} { + error "Missing installation file '$source'" + } + writefile $target [readfile $source]\n + # If possible, copy the file mode + file stat $source stat + set mode [format %o [expr {$stat(mode) & 0x1ff}]] + catch {exec chmod $mode $target} +} + +proc autosetup_install_readme {target sysinstall} { + set readme "README.autosetup created by [autosetup_version]\n\n" + if {$sysinstall} { + append readme \ +{This is the autosetup directory for a system install of autosetup. +Loadable modules can be added here. +} + } else { + append readme \ +{This is the autosetup directory for a local install of autosetup. +It contains autosetup, support files and loadable modules. +} +} + + append readme { +*.tcl files in this directory are optional modules which +can be loaded with the 'use' directive. + +*.auto files in this directory are auto-loaded. + +For more information, see https://msteveb.github.io/autosetup/ +} + dputs "install: autosetup/README.autosetup" + writefile $target $readme +} +} + +# ----- @module markdown-formatting.tcl ----- + +set modsource(markdown-formatting.tcl) { +# Copyright (c) 2010 WorkWare Systems http://www.workware.net.au/ +# All rights reserved + +# Module which provides text formatting +# markdown format (kramdown syntax) + +use formatting + +proc para {text} { + regsub -all "\[ \t\n\]+" [string trim $text] " " text + regsub -all {([^a-zA-Z])'([^']*)'} $text {\1**`\2`**} text + regsub -all {^'([^']*)'} $text {**`\1`**} text + regsub -all {(http[^ \t\n]*)} $text {[\1](\1)} text + return $text +} +proc title {text} { + underline [para $text] = + nl +} +proc p {text} { + puts [para $text] + nl +} +proc codelines {lines} { + puts "~~~~~~~~~~~~" + foreach line $lines { + puts $line + } + puts "~~~~~~~~~~~~" + nl +} +proc code {text} { + puts "~~~~~~~~~~~~" + foreach line [parse_code_block $text] { + puts $line + } + puts "~~~~~~~~~~~~" + nl +} +proc nl {} { + puts "" +} +proc underline {text char} { + regexp "^(\[ \t\]*)(.*)" $text -> indent words + puts $text + puts $indent[string repeat $char [string length $words]] +} +proc section {text} { + underline "[para $text]" - + nl +} +proc subsection {text} { + puts "### `$text`" + nl +} +proc bullet {text} { + puts "* [para $text]" +} +proc defn {first args} { + puts "^" + set defn [string trim [join $args \n]] + if {$first ne ""} { + puts "**${first}**" + puts -nonewline ": " + regsub -all "\n\n" $defn "\n: " defn + } + puts "$defn" +} +} + +# ----- @module misc.tcl ----- + +set modsource(misc.tcl) { +# Copyright (c) 2007-2010 WorkWare Systems http://www.workware.net.au/ +# All rights reserved + +# Module containing misc procs useful to modules +# Largely for platform compatibility + +set autosetup(istcl) [info exists ::tcl_library] +set autosetup(iswin) [string equal windows $tcl_platform(platform)] + +if {$autosetup(iswin)} { + # mingw/windows separates $PATH with semicolons + # and doesn't have an executable bit + proc split-path {} { + split [getenv PATH .] {;} + } + proc file-isexec {exec} { + # Basic test for windows. We ignore .bat + if {[file isfile $exec] || [file isfile $exec.exe]} { + return 1 + } + return 0 + } +} else { + # unix separates $PATH with colons and has and executable bit + proc split-path {} { + split [getenv PATH .] : + } + # Check for an executable file + proc file-isexec {exec} { + if {[file executable $exec] && [file isfile $exec]} { + return 1 + } + return 0 + } +} + +# Assume that exec can return stdout and stderr +proc exec-with-stderr {args} { + exec {*}$args 2>@1 +} + +if {$autosetup(istcl)} { + # Tcl doesn't have the env command + proc getenv {name args} { + if {[info exists ::env($name)]} { + return $::env($name) + } + if {[llength $args]} { + return [lindex $args 0] + } + return -code error "environment variable \"$name\" does not exist" + } + proc isatty? {channel} { + dict exists [fconfigure $channel] -xchar + } + # Jim-compatible stacktrace using info frame + proc stacktrace {} { + set stacktrace {} + # 2 to skip the current frame + for {set i 2} {$i < [info frame]} {incr i} { + set frame [info frame -$i] + if {[dict exists $frame file]} { + # We don't need proc, so use "" + lappend stacktrace "" [dict get $frame file] [dict get $frame line] "" + } + } + return $stacktrace + } +} else { + if {$autosetup(iswin)} { + # On Windows, backslash convert all environment variables + # (Assume that Tcl does this for us) + proc getenv {name args} { + string map {\\ /} [env $name {*}$args] + } + } else { + # Jim on unix is simple + alias getenv env + } + proc isatty? {channel} { + set tty 0 + catch { + # isatty is a recent addition to Jim Tcl + set tty [$channel isatty] + } + return $tty + } +} + +# In case 'file normalize' doesn't exist +# +proc file-normalize {path} { + if {[catch {file normalize $path} result]} { + if {$path eq ""} { + return "" + } + set oldpwd [pwd] + if {[file isdir $path]} { + cd $path + set result [pwd] + } else { + cd [file dirname $path] + set result [file join [pwd] [file tail $path]] + } + cd $oldpwd + } + return $result +} + +# If everything is working properly, the only errors which occur +# should be generated in user code (e.g. auto.def). +# By default, we only want to show the error location in user code. +# We use [info frame] to achieve this, but it works differently on Tcl and Jim. +# +# This is designed to be called for incorrect usage in auto.def, via autosetup-error +# +proc error-location {msg} { + if {$::autosetup(debug)} { + return -code error $msg + } + set vars {p f l cmd} + if {!$::autosetup(istcl) && ![dict exists $::tcl_platform stackFormat]} { + # Older versions of Jim had a 3 element stacktrace + set vars {p f l} + } + foreach $vars [stacktrace] { + if {[string match *.def $f]} { + return "[relative-path $f]:$l: Error: $msg" + } + #puts "Skipping $f:$l" + } + return $msg +} + +# If everything is working properly, the only errors which occur +# should be generated in user code (e.g. auto.def). +# By default, we only want to show the error location in user code. +# We use [info frame] to achieve this, but it works differently on Tcl and Jim. +# +# This is designed to be called for incorrect usage in auto.def, via autosetup-error +# +proc error-stacktrace {msg} { + if {$::autosetup(debug)} { + return -code error $msg + } + # Search back through the stack trace for the first error in a .def file + for {set i 1} {$i < [info level]} {incr i} { + if {$::autosetup(istcl)} { + array set info [info frame -$i] + } else { + lassign [info frame -$i] info(caller) info(file) info(line) + } + if {[string match *.def $info(file)]} { + return "[relative-path $info(file)]:$info(line): Error: $msg" + } + #puts "Skipping $info(file):$info(line)" + } + return $msg +} + +# Given the return from [catch {...} msg opts], returns an appropriate +# error message. A nice one for Jim and a less-nice one for Tcl. +# If 'fulltrace' is set, a full stack trace is provided. +# Otherwise a simple message is provided. +# +# This is designed for developer errors, e.g. in module code or auto.def code +# +# +proc error-dump {msg opts fulltrace} { + if {$::autosetup(istcl)} { + if {$fulltrace} { + return "Error: [dict get $opts -errorinfo]" + } else { + return "Error: $msg" + } + } else { + lassign $opts(-errorinfo) p f l + if {$f ne ""} { + set result "$f:$l: Error: " + } + append result "$msg\n" + if {$fulltrace} { + append result [stackdump $opts(-errorinfo)] + } + + # Remove the trailing newline + string trim $result + } +} +} + +# ----- @module text-formatting.tcl ----- + +set modsource(text-formatting.tcl) { +# Copyright (c) 2010 WorkWare Systems http://www.workware.net.au/ +# All rights reserved + +# Module which provides text formatting + +use formatting + +proc wordwrap {text length {firstprefix ""} {nextprefix ""}} { + set len 0 + set space $firstprefix + + foreach word [split $text] { + set word [string trim $word] + if {$word eq ""} { + continue + } + if {[info exists partial]} { + append partial " " $word + if {[string first $quote $word] < 0} { + # Haven't found end of quoted word + continue + } + # Finished quoted word + set word $partial + unset partial + unset quote + } else { + set quote [string index $word 0] + if {$quote in {' *}} { + if {[string first $quote $word 1] < 0} { + # Haven't found end of quoted word + # Not a whole word. + set first [string index $word 0] + # Start of quoted word + set partial $word + continue + } + } + } + + if {$len && [string length $space$word] + $len >= $length} { + puts "" + set len 0 + set space $nextprefix + } + incr len [string length $space$word] + + # Use man-page conventions for highlighting 'quoted' and *quoted* + # single words. + # Use x^Hx for *bold* and _^Hx for 'underline'. + # + # less and more will both understand this. + # Pipe through 'col -b' to remove them. + if {[regexp {^'(.*)'(.*)} $word -> quoted after]} { + set quoted [string map {~ " "} $quoted] + regsub -all . $quoted "&\b&" quoted + set word $quoted$after + } elseif {[regexp {^[*](.*)[*](.*)} $word -> quoted after]} { + set quoted [string map {~ " "} $quoted] + regsub -all . $quoted "_\b&" quoted + set word $quoted$after + } + puts -nonewline $space$word + set space " " + } + if {[info exists partial]} { + # Missing end of quote + puts -nonewline $space$partial + } + if {$len} { + puts "" + } +} +proc title {text} { + underline [string trim $text] = + nl +} +proc p {text} { + wordwrap $text 80 + nl +} +proc codelines {lines} { + foreach line $lines { + puts " $line" + } + nl +} +proc nl {} { + puts "" +} +proc underline {text char} { + regexp "^(\[ \t\]*)(.*)" $text -> indent words + puts $text + puts $indent[string repeat $char [string length $words]] +} +proc section {text} { + underline "[string trim $text]" - + nl +} +proc subsection {text} { + underline "$text" ~ + nl +} +proc bullet {text} { + wordwrap $text 76 " * " " " +} +proc indent {text} { + wordwrap $text 76 " " " " +} +proc defn {first args} { + if {$first ne ""} { + underline " $first" ~ + } + foreach p $args { + if {$p ne ""} { + indent $p + } + } +} +} + +# ----- @module util.tcl ----- + +set modsource(util.tcl) { +# Copyright (c) 2012 WorkWare Systems http://www.workware.net.au/ +# All rights reserved + +# Module which contains miscellaneous utility functions + +# @section Utilities + +# @compare-versions version1 version2 +# +# Versions are of the form 'a.b.c' (may be any number of numeric components) +# +# Compares the two versions and returns: +## -1 if v1 < v2 +## 0 if v1 == v2 +## 1 if v1 > v2 +# +# If one version has fewer components than the other, 0 is substituted to the right. e.g. +## 0.2 < 0.3 +## 0.2.5 > 0.2 +## 1.1 == 1.1.0 +# +proc compare-versions {v1 v2} { + foreach c1 [split $v1 .] c2 [split $v2 .] { + if {$c1 eq ""} { + set c1 0 + } + if {$c2 eq ""} { + set c2 0 + } + if {$c1 < $c2} { + return -1 + } + if {$c1 > $c2} { + return 1 + } + } + return 0 +} + +# @suffix suf list +# +# Takes a list and returns a new list with '$suf' appended +# to each element +# +## suffix .c {a b c} => {a.c b.c c.c} +# +proc suffix {suf list} { + set result {} + foreach p $list { + lappend result $p$suf + } + return $result +} + +# @prefix pre list +# +# Takes a list and returns a new list with '$pre' prepended +# to each element +# +## prefix jim- {a.c b.c} => {jim-a.c jim-b.c} +# +proc prefix {pre list} { + set result {} + foreach p $list { + lappend result $pre$p + } + return $result +} + +# @lpop list +# +# Removes the last entry from the given list and returns it. +proc lpop {listname} { + upvar $listname list + set val [lindex $list end] + set list [lrange $list 0 end-1] + return $val +} +} + +# ----- @module wiki-formatting.tcl ----- + +set modsource(wiki-formatting.tcl) { +# Copyright (c) 2010 WorkWare Systems http://www.workware.net.au/ +# All rights reserved + +# Module which provides text formatting +# wiki.tcl.tk format output + +use formatting + +proc joinlines {text} { + set lines {} + foreach l [split [string trim $text] \n] { + lappend lines [string trim $l] + } + join $lines +} +proc p {text} { + puts [joinlines $text] + puts "" +} +proc title {text} { + puts "*** [joinlines $text] ***" + puts "" +} +proc codelines {lines} { + puts "======" + foreach line $lines { + puts " $line" + } + puts "======" +} +proc code {text} { + puts "======" + foreach line [parse_code_block $text] { + puts " $line" + } + puts "======" +} +proc nl {} { +} +proc section {text} { + puts "'''$text'''" + puts "" +} +proc subsection {text} { + puts "''$text''" + puts "" +} +proc bullet {text} { + puts " * [joinlines $text]" +} +proc indent {text} { + puts " : [joinlines $text]" +} +proc defn {first args} { + if {$first ne ""} { + indent '''$first''' + } + + foreach p $args { + p $p + } +} +} + + +################################################################## +# +# Entry/Exit +# +if {$autosetup(debug)} { + main $argv +} +if {[catch {main $argv} msg opts] == 1} { + show-notices + autosetup-full-error [error-dump $msg $opts $autosetup(debug)] + if {!$autosetup(debug)} { + puts stderr "Try: '[file tail $autosetup(exe)] --debug' for a full stack trace" + } + exit 1 +} diff --git a/autosetup/autosetup-config.guess b/autosetup/autosetup-config.guess new file mode 100755 index 0000000000..48a684601b --- /dev/null +++ b/autosetup/autosetup-config.guess @@ -0,0 +1,1815 @@ +#! /bin/sh +# Attempt to guess a canonical system name. +# Copyright 1992-2024 Free Software Foundation, Inc. + +# shellcheck disable=SC2006,SC2268 # see below for rationale + +timestamp='2024-07-27' + +# This file is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, see . +# +# As a special exception to the GNU General Public License, if you +# distribute this file as part of a program that contains a +# configuration script generated by Autoconf, you may include it under +# the same distribution terms that you use for the rest of that +# program. This Exception is an additional permission under section 7 +# of the GNU General Public License, version 3 ("GPLv3"). +# +# Originally written by Per Bothner; maintained since 2000 by Ben Elliston. +# +# You can get the latest version of this script from: +# https://git.savannah.gnu.org/cgit/config.git/plain/config.guess +# +# Please send patches to . + + +# The "shellcheck disable" line above the timestamp inhibits complaints +# about features and limitations of the classic Bourne shell that were +# superseded or lifted in POSIX. However, this script identifies a wide +# variety of pre-POSIX systems that do not have POSIX shells at all, and +# even some reasonably current systems (Solaris 10 as case-in-point) still +# have a pre-POSIX /bin/sh. + + +me=`echo "$0" | sed -e 's,.*/,,'` + +usage="\ +Usage: $0 [OPTION] + +Output the configuration name of the system '$me' is run on. + +Options: + -h, --help print this help, then exit + -t, --time-stamp print date of last modification, then exit + -v, --version print version number, then exit + +Report bugs and patches to ." + +version="\ +GNU config.guess ($timestamp) + +Originally written by Per Bothner. +Copyright 1992-2024 Free Software Foundation, Inc. + +This is free software; see the source for copying conditions. There is NO +warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." + +help=" +Try '$me --help' for more information." + +# Parse command line +while test $# -gt 0 ; do + case $1 in + --time-stamp | --time* | -t ) + echo "$timestamp" ; exit ;; + --version | -v ) + echo "$version" ; exit ;; + --help | --h* | -h ) + echo "$usage"; exit ;; + -- ) # Stop option processing + shift; break ;; + - ) # Use stdin as input. + break ;; + -* ) + echo "$me: invalid option $1$help" >&2 + exit 1 ;; + * ) + break ;; + esac +done + +if test $# != 0; then + echo "$me: too many arguments$help" >&2 + exit 1 +fi + +# Just in case it came from the environment. +GUESS= + +# CC_FOR_BUILD -- compiler used by this script. Note that the use of a +# compiler to aid in system detection is discouraged as it requires +# temporary files to be created and, as you can see below, it is a +# headache to deal with in a portable fashion. + +# Historically, 'CC_FOR_BUILD' used to be named 'HOST_CC'. We still +# use 'HOST_CC' if defined, but it is deprecated. + +# Portable tmp directory creation inspired by the Autoconf team. + +tmp= +# shellcheck disable=SC2172 +trap 'test -z "$tmp" || rm -fr "$tmp"' 0 1 2 13 15 + +set_cc_for_build() { + # prevent multiple calls if $tmp is already set + test "$tmp" && return 0 + : "${TMPDIR=/tmp}" + # shellcheck disable=SC2039,SC3028 + { tmp=`(umask 077 && mktemp -d "$TMPDIR/cgXXXXXX") 2>/dev/null` && test -n "$tmp" && test -d "$tmp" ; } || + { test -n "$RANDOM" && tmp=$TMPDIR/cg$$-$RANDOM && (umask 077 && mkdir "$tmp" 2>/dev/null) ; } || + { tmp=$TMPDIR/cg-$$ && (umask 077 && mkdir "$tmp" 2>/dev/null) && echo "Warning: creating insecure temp directory" >&2 ; } || + { echo "$me: cannot create a temporary directory in $TMPDIR" >&2 ; exit 1 ; } + dummy=$tmp/dummy + case ${CC_FOR_BUILD-},${HOST_CC-},${CC-} in + ,,) echo "int x;" > "$dummy.c" + for driver in cc gcc c17 c99 c89 ; do + if ($driver -c -o "$dummy.o" "$dummy.c") >/dev/null 2>&1 ; then + CC_FOR_BUILD=$driver + break + fi + done + if test x"$CC_FOR_BUILD" = x ; then + CC_FOR_BUILD=no_compiler_found + fi + ;; + ,,*) CC_FOR_BUILD=$CC ;; + ,*,*) CC_FOR_BUILD=$HOST_CC ;; + esac +} + +# This is needed to find uname on a Pyramid OSx when run in the BSD universe. +# (ghazi@noc.rutgers.edu 1994-08-24) +if test -f /.attbin/uname ; then + PATH=$PATH:/.attbin ; export PATH +fi + +UNAME_MACHINE=`(uname -m) 2>/dev/null` || UNAME_MACHINE=unknown +UNAME_RELEASE=`(uname -r) 2>/dev/null` || UNAME_RELEASE=unknown +UNAME_SYSTEM=`(uname -s) 2>/dev/null` || UNAME_SYSTEM=unknown +UNAME_VERSION=`(uname -v) 2>/dev/null` || UNAME_VERSION=unknown + +case $UNAME_SYSTEM in +Linux|GNU|GNU/*) + LIBC=unknown + + set_cc_for_build + cat <<-EOF > "$dummy.c" + #if defined(__ANDROID__) + LIBC=android + #else + #include + #if defined(__UCLIBC__) + LIBC=uclibc + #elif defined(__dietlibc__) + LIBC=dietlibc + #elif defined(__GLIBC__) + LIBC=gnu + #elif defined(__LLVM_LIBC__) + LIBC=llvm + #else + #include + /* First heuristic to detect musl libc. */ + #ifdef __DEFINED_va_list + LIBC=musl + #endif + #endif + #endif + EOF + cc_set_libc=`$CC_FOR_BUILD -E "$dummy.c" 2>/dev/null | grep '^LIBC' | sed 's, ,,g'` + eval "$cc_set_libc" + + # Second heuristic to detect musl libc. + if [ "$LIBC" = unknown ] && + command -v ldd >/dev/null && + ldd --version 2>&1 | grep -q ^musl; then + LIBC=musl + fi + + # If the system lacks a compiler, then just pick glibc. + # We could probably try harder. + if [ "$LIBC" = unknown ]; then + LIBC=gnu + fi + ;; +esac + +# Note: order is significant - the case branches are not exclusive. + +case $UNAME_MACHINE:$UNAME_SYSTEM:$UNAME_RELEASE:$UNAME_VERSION in + *:NetBSD:*:*) + # NetBSD (nbsd) targets should (where applicable) match one or + # more of the tuples: *-*-netbsdelf*, *-*-netbsdaout*, + # *-*-netbsdecoff* and *-*-netbsd*. For targets that recently + # switched to ELF, *-*-netbsd* would select the old + # object file format. This provides both forward + # compatibility and a consistent mechanism for selecting the + # object file format. + # + # Note: NetBSD doesn't particularly care about the vendor + # portion of the name. We always set it to "unknown". + UNAME_MACHINE_ARCH=`(uname -p 2>/dev/null || \ + /sbin/sysctl -n hw.machine_arch 2>/dev/null || \ + /usr/sbin/sysctl -n hw.machine_arch 2>/dev/null || \ + echo unknown)` + case $UNAME_MACHINE_ARCH in + aarch64eb) machine=aarch64_be-unknown ;; + armeb) machine=armeb-unknown ;; + arm*) machine=arm-unknown ;; + sh3el) machine=shl-unknown ;; + sh3eb) machine=sh-unknown ;; + sh5el) machine=sh5le-unknown ;; + earmv*) + arch=`echo "$UNAME_MACHINE_ARCH" | sed -e 's,^e\(armv[0-9]\).*$,\1,'` + endian=`echo "$UNAME_MACHINE_ARCH" | sed -ne 's,^.*\(eb\)$,\1,p'` + machine=${arch}${endian}-unknown + ;; + *) machine=$UNAME_MACHINE_ARCH-unknown ;; + esac + # The Operating System including object format, if it has switched + # to ELF recently (or will in the future) and ABI. + case $UNAME_MACHINE_ARCH in + earm*) + os=netbsdelf + ;; + arm*|i386|m68k|ns32k|sh3*|sparc|vax) + set_cc_for_build + if echo __ELF__ | $CC_FOR_BUILD -E - 2>/dev/null \ + | grep -q __ELF__ + then + # Once all utilities can be ECOFF (netbsdecoff) or a.out (netbsdaout). + # Return netbsd for either. FIX? + os=netbsd + else + os=netbsdelf + fi + ;; + *) + os=netbsd + ;; + esac + # Determine ABI tags. + case $UNAME_MACHINE_ARCH in + earm*) + expr='s/^earmv[0-9]/-eabi/;s/eb$//' + abi=`echo "$UNAME_MACHINE_ARCH" | sed -e "$expr"` + ;; + esac + # The OS release + # Debian GNU/NetBSD machines have a different userland, and + # thus, need a distinct triplet. However, they do not need + # kernel version information, so it can be replaced with a + # suitable tag, in the style of linux-gnu. + case $UNAME_VERSION in + Debian*) + release='-gnu' + ;; + *) + release=`echo "$UNAME_RELEASE" | sed -e 's/[-_].*//' | cut -d. -f1,2` + ;; + esac + # Since CPU_TYPE-MANUFACTURER-KERNEL-OPERATING_SYSTEM: + # contains redundant information, the shorter form: + # CPU_TYPE-MANUFACTURER-OPERATING_SYSTEM is used. + GUESS=$machine-${os}${release}${abi-} + ;; + *:Bitrig:*:*) + UNAME_MACHINE_ARCH=`arch | sed 's/Bitrig.//'` + GUESS=$UNAME_MACHINE_ARCH-unknown-bitrig$UNAME_RELEASE + ;; + *:OpenBSD:*:*) + UNAME_MACHINE_ARCH=`arch | sed 's/OpenBSD.//'` + GUESS=$UNAME_MACHINE_ARCH-unknown-openbsd$UNAME_RELEASE + ;; + *:SecBSD:*:*) + UNAME_MACHINE_ARCH=`arch | sed 's/SecBSD.//'` + GUESS=$UNAME_MACHINE_ARCH-unknown-secbsd$UNAME_RELEASE + ;; + *:LibertyBSD:*:*) + UNAME_MACHINE_ARCH=`arch | sed 's/^.*BSD\.//'` + GUESS=$UNAME_MACHINE_ARCH-unknown-libertybsd$UNAME_RELEASE + ;; + *:MidnightBSD:*:*) + GUESS=$UNAME_MACHINE-unknown-midnightbsd$UNAME_RELEASE + ;; + *:ekkoBSD:*:*) + GUESS=$UNAME_MACHINE-unknown-ekkobsd$UNAME_RELEASE + ;; + *:SolidBSD:*:*) + GUESS=$UNAME_MACHINE-unknown-solidbsd$UNAME_RELEASE + ;; + *:OS108:*:*) + GUESS=$UNAME_MACHINE-unknown-os108_$UNAME_RELEASE + ;; + macppc:MirBSD:*:*) + GUESS=powerpc-unknown-mirbsd$UNAME_RELEASE + ;; + *:MirBSD:*:*) + GUESS=$UNAME_MACHINE-unknown-mirbsd$UNAME_RELEASE + ;; + *:Sortix:*:*) + GUESS=$UNAME_MACHINE-unknown-sortix + ;; + *:Twizzler:*:*) + GUESS=$UNAME_MACHINE-unknown-twizzler + ;; + *:Redox:*:*) + GUESS=$UNAME_MACHINE-unknown-redox + ;; + mips:OSF1:*.*) + GUESS=mips-dec-osf1 + ;; + alpha:OSF1:*:*) + # Reset EXIT trap before exiting to avoid spurious non-zero exit code. + trap '' 0 + case $UNAME_RELEASE in + *4.0) + UNAME_RELEASE=`/usr/sbin/sizer -v | awk '{print $3}'` + ;; + *5.*) + UNAME_RELEASE=`/usr/sbin/sizer -v | awk '{print $4}'` + ;; + esac + # According to Compaq, /usr/sbin/psrinfo has been available on + # OSF/1 and Tru64 systems produced since 1995. I hope that + # covers most systems running today. This code pipes the CPU + # types through head -n 1, so we only detect the type of CPU 0. + ALPHA_CPU_TYPE=`/usr/sbin/psrinfo -v | sed -n -e 's/^ The alpha \(.*\) processor.*$/\1/p' | head -n 1` + case $ALPHA_CPU_TYPE in + "EV4 (21064)") + UNAME_MACHINE=alpha ;; + "EV4.5 (21064)") + UNAME_MACHINE=alpha ;; + "LCA4 (21066/21068)") + UNAME_MACHINE=alpha ;; + "EV5 (21164)") + UNAME_MACHINE=alphaev5 ;; + "EV5.6 (21164A)") + UNAME_MACHINE=alphaev56 ;; + "EV5.6 (21164PC)") + UNAME_MACHINE=alphapca56 ;; + "EV5.7 (21164PC)") + UNAME_MACHINE=alphapca57 ;; + "EV6 (21264)") + UNAME_MACHINE=alphaev6 ;; + "EV6.7 (21264A)") + UNAME_MACHINE=alphaev67 ;; + "EV6.8CB (21264C)") + UNAME_MACHINE=alphaev68 ;; + "EV6.8AL (21264B)") + UNAME_MACHINE=alphaev68 ;; + "EV6.8CX (21264D)") + UNAME_MACHINE=alphaev68 ;; + "EV6.9A (21264/EV69A)") + UNAME_MACHINE=alphaev69 ;; + "EV7 (21364)") + UNAME_MACHINE=alphaev7 ;; + "EV7.9 (21364A)") + UNAME_MACHINE=alphaev79 ;; + esac + # A Pn.n version is a patched version. + # A Vn.n version is a released version. + # A Tn.n version is a released field test version. + # A Xn.n version is an unreleased experimental baselevel. + # 1.2 uses "1.2" for uname -r. + OSF_REL=`echo "$UNAME_RELEASE" | sed -e 's/^[PVTX]//' | tr ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz` + GUESS=$UNAME_MACHINE-dec-osf$OSF_REL + ;; + Amiga*:UNIX_System_V:4.0:*) + GUESS=m68k-unknown-sysv4 + ;; + *:[Aa]miga[Oo][Ss]:*:*) + GUESS=$UNAME_MACHINE-unknown-amigaos + ;; + *:[Mm]orph[Oo][Ss]:*:*) + GUESS=$UNAME_MACHINE-unknown-morphos + ;; + *:OS/390:*:*) + GUESS=i370-ibm-openedition + ;; + *:z/VM:*:*) + GUESS=s390-ibm-zvmoe + ;; + *:OS400:*:*) + GUESS=powerpc-ibm-os400 + ;; + arm:RISC*:1.[012]*:*|arm:riscix:1.[012]*:*) + GUESS=arm-acorn-riscix$UNAME_RELEASE + ;; + arm*:riscos:*:*|arm*:RISCOS:*:*) + GUESS=arm-unknown-riscos + ;; + SR2?01:HI-UX/MPP:*:* | SR8000:HI-UX/MPP:*:*) + GUESS=hppa1.1-hitachi-hiuxmpp + ;; + Pyramid*:OSx*:*:* | MIS*:OSx*:*:* | MIS*:SMP_DC-OSx*:*:*) + # akee@wpdis03.wpafb.af.mil (Earle F. Ake) contributed MIS and NILE. + case `(/bin/universe) 2>/dev/null` in + att) GUESS=pyramid-pyramid-sysv3 ;; + *) GUESS=pyramid-pyramid-bsd ;; + esac + ;; + NILE*:*:*:dcosx) + GUESS=pyramid-pyramid-svr4 + ;; + DRS?6000:unix:4.0:6*) + GUESS=sparc-icl-nx6 + ;; + DRS?6000:UNIX_SV:4.2*:7* | DRS?6000:isis:4.2*:7*) + case `/usr/bin/uname -p` in + sparc) GUESS=sparc-icl-nx7 ;; + esac + ;; + s390x:SunOS:*:*) + SUN_REL=`echo "$UNAME_RELEASE" | sed -e 's/[^.]*//'` + GUESS=$UNAME_MACHINE-ibm-solaris2$SUN_REL + ;; + sun4H:SunOS:5.*:*) + SUN_REL=`echo "$UNAME_RELEASE" | sed -e 's/[^.]*//'` + GUESS=sparc-hal-solaris2$SUN_REL + ;; + sun4*:SunOS:5.*:* | tadpole*:SunOS:5.*:*) + SUN_REL=`echo "$UNAME_RELEASE" | sed -e 's/[^.]*//'` + GUESS=sparc-sun-solaris2$SUN_REL + ;; + i86pc:AuroraUX:5.*:* | i86xen:AuroraUX:5.*:*) + GUESS=i386-pc-auroraux$UNAME_RELEASE + ;; + i86pc:SunOS:5.*:* | i86xen:SunOS:5.*:*) + set_cc_for_build + SUN_ARCH=i386 + # If there is a compiler, see if it is configured for 64-bit objects. + # Note that the Sun cc does not turn __LP64__ into 1 like gcc does. + # This test works for both compilers. + if test "$CC_FOR_BUILD" != no_compiler_found; then + if (echo '#ifdef __amd64'; echo IS_64BIT_ARCH; echo '#endif') | \ + (CCOPTS="" $CC_FOR_BUILD -m64 -E - 2>/dev/null) | \ + grep IS_64BIT_ARCH >/dev/null + then + SUN_ARCH=x86_64 + fi + fi + SUN_REL=`echo "$UNAME_RELEASE" | sed -e 's/[^.]*//'` + GUESS=$SUN_ARCH-pc-solaris2$SUN_REL + ;; + sun4*:SunOS:6*:*) + # According to config.sub, this is the proper way to canonicalize + # SunOS6. Hard to guess exactly what SunOS6 will be like, but + # it's likely to be more like Solaris than SunOS4. + SUN_REL=`echo "$UNAME_RELEASE" | sed -e 's/[^.]*//'` + GUESS=sparc-sun-solaris3$SUN_REL + ;; + sun4*:SunOS:*:*) + case `/usr/bin/arch -k` in + Series*|S4*) + UNAME_RELEASE=`uname -v` + ;; + esac + # Japanese Language versions have a version number like '4.1.3-JL'. + SUN_REL=`echo "$UNAME_RELEASE" | sed -e 's/-/_/'` + GUESS=sparc-sun-sunos$SUN_REL + ;; + sun3*:SunOS:*:*) + GUESS=m68k-sun-sunos$UNAME_RELEASE + ;; + sun*:*:4.2BSD:*) + UNAME_RELEASE=`(sed 1q /etc/motd | awk '{print substr($5,1,3)}') 2>/dev/null` + test "x$UNAME_RELEASE" = x && UNAME_RELEASE=3 + case `/bin/arch` in + sun3) + GUESS=m68k-sun-sunos$UNAME_RELEASE + ;; + sun4) + GUESS=sparc-sun-sunos$UNAME_RELEASE + ;; + esac + ;; + aushp:SunOS:*:*) + GUESS=sparc-auspex-sunos$UNAME_RELEASE + ;; + # The situation for MiNT is a little confusing. The machine name + # can be virtually everything (everything which is not + # "atarist" or "atariste" at least should have a processor + # > m68000). The system name ranges from "MiNT" over "FreeMiNT" + # to the lowercase version "mint" (or "freemint"). Finally + # the system name "TOS" denotes a system which is actually not + # MiNT. But MiNT is downward compatible to TOS, so this should + # be no problem. + atarist[e]:*MiNT:*:* | atarist[e]:*mint:*:* | atarist[e]:*TOS:*:*) + GUESS=m68k-atari-mint$UNAME_RELEASE + ;; + atari*:*MiNT:*:* | atari*:*mint:*:* | atarist[e]:*TOS:*:*) + GUESS=m68k-atari-mint$UNAME_RELEASE + ;; + *falcon*:*MiNT:*:* | *falcon*:*mint:*:* | *falcon*:*TOS:*:*) + GUESS=m68k-atari-mint$UNAME_RELEASE + ;; + milan*:*MiNT:*:* | milan*:*mint:*:* | *milan*:*TOS:*:*) + GUESS=m68k-milan-mint$UNAME_RELEASE + ;; + hades*:*MiNT:*:* | hades*:*mint:*:* | *hades*:*TOS:*:*) + GUESS=m68k-hades-mint$UNAME_RELEASE + ;; + *:*MiNT:*:* | *:*mint:*:* | *:*TOS:*:*) + GUESS=m68k-unknown-mint$UNAME_RELEASE + ;; + m68k:machten:*:*) + GUESS=m68k-apple-machten$UNAME_RELEASE + ;; + powerpc:machten:*:*) + GUESS=powerpc-apple-machten$UNAME_RELEASE + ;; + RISC*:Mach:*:*) + GUESS=mips-dec-mach_bsd4.3 + ;; + RISC*:ULTRIX:*:*) + GUESS=mips-dec-ultrix$UNAME_RELEASE + ;; + VAX*:ULTRIX*:*:*) + GUESS=vax-dec-ultrix$UNAME_RELEASE + ;; + 2020:CLIX:*:* | 2430:CLIX:*:*) + GUESS=clipper-intergraph-clix$UNAME_RELEASE + ;; + mips:*:*:UMIPS | mips:*:*:RISCos) + set_cc_for_build + sed 's/^ //' << EOF > "$dummy.c" +#ifdef __cplusplus +#include /* for printf() prototype */ + int main (int argc, char *argv[]) { +#else + int main (argc, argv) int argc; char *argv[]; { +#endif + #if defined (host_mips) && defined (MIPSEB) + #if defined (SYSTYPE_SYSV) + printf ("mips-mips-riscos%ssysv\\n", argv[1]); exit (0); + #endif + #if defined (SYSTYPE_SVR4) + printf ("mips-mips-riscos%ssvr4\\n", argv[1]); exit (0); + #endif + #if defined (SYSTYPE_BSD43) || defined(SYSTYPE_BSD) + printf ("mips-mips-riscos%sbsd\\n", argv[1]); exit (0); + #endif + #endif + exit (-1); + } +EOF + $CC_FOR_BUILD -o "$dummy" "$dummy.c" && + dummyarg=`echo "$UNAME_RELEASE" | sed -n 's/\([0-9]*\).*/\1/p'` && + SYSTEM_NAME=`"$dummy" "$dummyarg"` && + { echo "$SYSTEM_NAME"; exit; } + GUESS=mips-mips-riscos$UNAME_RELEASE + ;; + Motorola:PowerMAX_OS:*:*) + GUESS=powerpc-motorola-powermax + ;; + Motorola:*:4.3:PL8-*) + GUESS=powerpc-harris-powermax + ;; + Night_Hawk:*:*:PowerMAX_OS | Synergy:PowerMAX_OS:*:*) + GUESS=powerpc-harris-powermax + ;; + Night_Hawk:Power_UNIX:*:*) + GUESS=powerpc-harris-powerunix + ;; + m88k:CX/UX:7*:*) + GUESS=m88k-harris-cxux7 + ;; + m88k:*:4*:R4*) + GUESS=m88k-motorola-sysv4 + ;; + m88k:*:3*:R3*) + GUESS=m88k-motorola-sysv3 + ;; + AViiON:dgux:*:*) + # DG/UX returns AViiON for all architectures + UNAME_PROCESSOR=`/usr/bin/uname -p` + if test "$UNAME_PROCESSOR" = mc88100 || test "$UNAME_PROCESSOR" = mc88110 + then + if test "$TARGET_BINARY_INTERFACE"x = m88kdguxelfx || \ + test "$TARGET_BINARY_INTERFACE"x = x + then + GUESS=m88k-dg-dgux$UNAME_RELEASE + else + GUESS=m88k-dg-dguxbcs$UNAME_RELEASE + fi + else + GUESS=i586-dg-dgux$UNAME_RELEASE + fi + ;; + M88*:DolphinOS:*:*) # DolphinOS (SVR3) + GUESS=m88k-dolphin-sysv3 + ;; + M88*:*:R3*:*) + # Delta 88k system running SVR3 + GUESS=m88k-motorola-sysv3 + ;; + XD88*:*:*:*) # Tektronix XD88 system running UTekV (SVR3) + GUESS=m88k-tektronix-sysv3 + ;; + Tek43[0-9][0-9]:UTek:*:*) # Tektronix 4300 system running UTek (BSD) + GUESS=m68k-tektronix-bsd + ;; + *:IRIX*:*:*) + IRIX_REL=`echo "$UNAME_RELEASE" | sed -e 's/-/_/g'` + GUESS=mips-sgi-irix$IRIX_REL + ;; + ????????:AIX?:[12].1:2) # AIX 2.2.1 or AIX 2.1.1 is RT/PC AIX. + GUESS=romp-ibm-aix # uname -m gives an 8 hex-code CPU id + ;; # Note that: echo "'`uname -s`'" gives 'AIX ' + i*86:AIX:*:*) + GUESS=i386-ibm-aix + ;; + ia64:AIX:*:*) + if test -x /usr/bin/oslevel ; then + IBM_REV=`/usr/bin/oslevel` + else + IBM_REV=$UNAME_VERSION.$UNAME_RELEASE + fi + GUESS=$UNAME_MACHINE-ibm-aix$IBM_REV + ;; + *:AIX:2:3) + if grep bos325 /usr/include/stdio.h >/dev/null 2>&1; then + set_cc_for_build + sed 's/^ //' << EOF > "$dummy.c" + #include + + int + main () + { + if (!__power_pc()) + exit(1); + puts("powerpc-ibm-aix3.2.5"); + exit(0); + } +EOF + if $CC_FOR_BUILD -o "$dummy" "$dummy.c" && SYSTEM_NAME=`"$dummy"` + then + GUESS=$SYSTEM_NAME + else + GUESS=rs6000-ibm-aix3.2.5 + fi + elif grep bos324 /usr/include/stdio.h >/dev/null 2>&1; then + GUESS=rs6000-ibm-aix3.2.4 + else + GUESS=rs6000-ibm-aix3.2 + fi + ;; + *:AIX:*:[4567]) + IBM_CPU_ID=`/usr/sbin/lsdev -C -c processor -S available | sed 1q | awk '{ print $1 }'` + if /usr/sbin/lsattr -El "$IBM_CPU_ID" | grep ' POWER' >/dev/null 2>&1; then + IBM_ARCH=rs6000 + else + IBM_ARCH=powerpc + fi + if test -x /usr/bin/lslpp ; then + IBM_REV=`/usr/bin/lslpp -Lqc bos.rte.libc | \ + awk -F: '{ print $3 }' | sed s/[0-9]*$/0/` + else + IBM_REV=$UNAME_VERSION.$UNAME_RELEASE + fi + GUESS=$IBM_ARCH-ibm-aix$IBM_REV + ;; + *:AIX:*:*) + GUESS=rs6000-ibm-aix + ;; + ibmrt:4.4BSD:*|romp-ibm:4.4BSD:*) + GUESS=romp-ibm-bsd4.4 + ;; + ibmrt:*BSD:*|romp-ibm:BSD:*) # covers RT/PC BSD and + GUESS=romp-ibm-bsd$UNAME_RELEASE # 4.3 with uname added to + ;; # report: romp-ibm BSD 4.3 + *:BOSX:*:*) + GUESS=rs6000-bull-bosx + ;; + DPX/2?00:B.O.S.:*:*) + GUESS=m68k-bull-sysv3 + ;; + 9000/[34]??:4.3bsd:1.*:*) + GUESS=m68k-hp-bsd + ;; + hp300:4.4BSD:*:* | 9000/[34]??:4.3bsd:2.*:*) + GUESS=m68k-hp-bsd4.4 + ;; + 9000/[34678]??:HP-UX:*:*) + HPUX_REV=`echo "$UNAME_RELEASE" | sed -e 's/[^.]*.[0B]*//'` + case $UNAME_MACHINE in + 9000/31?) HP_ARCH=m68000 ;; + 9000/[34]??) HP_ARCH=m68k ;; + 9000/[678][0-9][0-9]) + if test -x /usr/bin/getconf; then + sc_cpu_version=`/usr/bin/getconf SC_CPU_VERSION 2>/dev/null` + sc_kernel_bits=`/usr/bin/getconf SC_KERNEL_BITS 2>/dev/null` + case $sc_cpu_version in + 523) HP_ARCH=hppa1.0 ;; # CPU_PA_RISC1_0 + 528) HP_ARCH=hppa1.1 ;; # CPU_PA_RISC1_1 + 532) # CPU_PA_RISC2_0 + case $sc_kernel_bits in + 32) HP_ARCH=hppa2.0n ;; + 64) HP_ARCH=hppa2.0w ;; + '') HP_ARCH=hppa2.0 ;; # HP-UX 10.20 + esac ;; + esac + fi + if test "$HP_ARCH" = ""; then + set_cc_for_build + sed 's/^ //' << EOF > "$dummy.c" + + #define _HPUX_SOURCE + #include + #include + + int + main () + { + #if defined(_SC_KERNEL_BITS) + long bits = sysconf(_SC_KERNEL_BITS); + #endif + long cpu = sysconf (_SC_CPU_VERSION); + + switch (cpu) + { + case CPU_PA_RISC1_0: puts ("hppa1.0"); break; + case CPU_PA_RISC1_1: puts ("hppa1.1"); break; + case CPU_PA_RISC2_0: + #if defined(_SC_KERNEL_BITS) + switch (bits) + { + case 64: puts ("hppa2.0w"); break; + case 32: puts ("hppa2.0n"); break; + default: puts ("hppa2.0"); break; + } break; + #else /* !defined(_SC_KERNEL_BITS) */ + puts ("hppa2.0"); break; + #endif + default: puts ("hppa1.0"); break; + } + exit (0); + } +EOF + (CCOPTS="" $CC_FOR_BUILD -o "$dummy" "$dummy.c" 2>/dev/null) && HP_ARCH=`"$dummy"` + test -z "$HP_ARCH" && HP_ARCH=hppa + fi ;; + esac + if test "$HP_ARCH" = hppa2.0w + then + set_cc_for_build + + # hppa2.0w-hp-hpux* has a 64-bit kernel and a compiler generating + # 32-bit code. hppa64-hp-hpux* has the same kernel and a compiler + # generating 64-bit code. GNU and HP use different nomenclature: + # + # $ CC_FOR_BUILD=cc ./config.guess + # => hppa2.0w-hp-hpux11.23 + # $ CC_FOR_BUILD="cc +DA2.0w" ./config.guess + # => hppa64-hp-hpux11.23 + + if echo __LP64__ | (CCOPTS="" $CC_FOR_BUILD -E - 2>/dev/null) | + grep -q __LP64__ + then + HP_ARCH=hppa2.0w + else + HP_ARCH=hppa64 + fi + fi + GUESS=$HP_ARCH-hp-hpux$HPUX_REV + ;; + ia64:HP-UX:*:*) + HPUX_REV=`echo "$UNAME_RELEASE" | sed -e 's/[^.]*.[0B]*//'` + GUESS=ia64-hp-hpux$HPUX_REV + ;; + 3050*:HI-UX:*:*) + set_cc_for_build + sed 's/^ //' << EOF > "$dummy.c" + #include + int + main () + { + long cpu = sysconf (_SC_CPU_VERSION); + /* The order matters, because CPU_IS_HP_MC68K erroneously returns + true for CPU_PA_RISC1_0. CPU_IS_PA_RISC returns correct + results, however. */ + if (CPU_IS_PA_RISC (cpu)) + { + switch (cpu) + { + case CPU_PA_RISC1_0: puts ("hppa1.0-hitachi-hiuxwe2"); break; + case CPU_PA_RISC1_1: puts ("hppa1.1-hitachi-hiuxwe2"); break; + case CPU_PA_RISC2_0: puts ("hppa2.0-hitachi-hiuxwe2"); break; + default: puts ("hppa-hitachi-hiuxwe2"); break; + } + } + else if (CPU_IS_HP_MC68K (cpu)) + puts ("m68k-hitachi-hiuxwe2"); + else puts ("unknown-hitachi-hiuxwe2"); + exit (0); + } +EOF + $CC_FOR_BUILD -o "$dummy" "$dummy.c" && SYSTEM_NAME=`"$dummy"` && + { echo "$SYSTEM_NAME"; exit; } + GUESS=unknown-hitachi-hiuxwe2 + ;; + 9000/7??:4.3bsd:*:* | 9000/8?[79]:4.3bsd:*:*) + GUESS=hppa1.1-hp-bsd + ;; + 9000/8??:4.3bsd:*:*) + GUESS=hppa1.0-hp-bsd + ;; + *9??*:MPE/iX:*:* | *3000*:MPE/iX:*:*) + GUESS=hppa1.0-hp-mpeix + ;; + hp7??:OSF1:*:* | hp8?[79]:OSF1:*:*) + GUESS=hppa1.1-hp-osf + ;; + hp8??:OSF1:*:*) + GUESS=hppa1.0-hp-osf + ;; + i*86:OSF1:*:*) + if test -x /usr/sbin/sysversion ; then + GUESS=$UNAME_MACHINE-unknown-osf1mk + else + GUESS=$UNAME_MACHINE-unknown-osf1 + fi + ;; + parisc*:Lites*:*:*) + GUESS=hppa1.1-hp-lites + ;; + C1*:ConvexOS:*:* | convex:ConvexOS:C1*:*) + GUESS=c1-convex-bsd + ;; + C2*:ConvexOS:*:* | convex:ConvexOS:C2*:*) + if getsysinfo -f scalar_acc + then echo c32-convex-bsd + else echo c2-convex-bsd + fi + exit ;; + C34*:ConvexOS:*:* | convex:ConvexOS:C34*:*) + GUESS=c34-convex-bsd + ;; + C38*:ConvexOS:*:* | convex:ConvexOS:C38*:*) + GUESS=c38-convex-bsd + ;; + C4*:ConvexOS:*:* | convex:ConvexOS:C4*:*) + GUESS=c4-convex-bsd + ;; + CRAY*Y-MP:*:*:*) + CRAY_REL=`echo "$UNAME_RELEASE" | sed -e 's/\.[^.]*$/.X/'` + GUESS=ymp-cray-unicos$CRAY_REL + ;; + CRAY*[A-Z]90:*:*:*) + echo "$UNAME_MACHINE"-cray-unicos"$UNAME_RELEASE" \ + | sed -e 's/CRAY.*\([A-Z]90\)/\1/' \ + -e y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/ \ + -e 's/\.[^.]*$/.X/' + exit ;; + CRAY*TS:*:*:*) + CRAY_REL=`echo "$UNAME_RELEASE" | sed -e 's/\.[^.]*$/.X/'` + GUESS=t90-cray-unicos$CRAY_REL + ;; + CRAY*T3E:*:*:*) + CRAY_REL=`echo "$UNAME_RELEASE" | sed -e 's/\.[^.]*$/.X/'` + GUESS=alphaev5-cray-unicosmk$CRAY_REL + ;; + CRAY*SV1:*:*:*) + CRAY_REL=`echo "$UNAME_RELEASE" | sed -e 's/\.[^.]*$/.X/'` + GUESS=sv1-cray-unicos$CRAY_REL + ;; + *:UNICOS/mp:*:*) + CRAY_REL=`echo "$UNAME_RELEASE" | sed -e 's/\.[^.]*$/.X/'` + GUESS=craynv-cray-unicosmp$CRAY_REL + ;; + F30[01]:UNIX_System_V:*:* | F700:UNIX_System_V:*:*) + FUJITSU_PROC=`uname -m | tr ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz` + FUJITSU_SYS=`uname -p | tr ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz | sed -e 's/\///'` + FUJITSU_REL=`echo "$UNAME_RELEASE" | sed -e 's/ /_/'` + GUESS=${FUJITSU_PROC}-fujitsu-${FUJITSU_SYS}${FUJITSU_REL} + ;; + 5000:UNIX_System_V:4.*:*) + FUJITSU_SYS=`uname -p | tr ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz | sed -e 's/\///'` + FUJITSU_REL=`echo "$UNAME_RELEASE" | tr ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz | sed -e 's/ /_/'` + GUESS=sparc-fujitsu-${FUJITSU_SYS}${FUJITSU_REL} + ;; + i*86:BSD/386:*:* | i*86:BSD/OS:*:* | *:Ascend\ Embedded/OS:*:*) + GUESS=$UNAME_MACHINE-pc-bsdi$UNAME_RELEASE + ;; + sparc*:BSD/OS:*:*) + GUESS=sparc-unknown-bsdi$UNAME_RELEASE + ;; + *:BSD/OS:*:*) + GUESS=$UNAME_MACHINE-unknown-bsdi$UNAME_RELEASE + ;; + arm:FreeBSD:*:*) + UNAME_PROCESSOR=`uname -p` + set_cc_for_build + if echo __ARM_PCS_VFP | $CC_FOR_BUILD -E - 2>/dev/null \ + | grep -q __ARM_PCS_VFP + then + FREEBSD_REL=`echo "$UNAME_RELEASE" | sed -e 's/[-(].*//'` + GUESS=$UNAME_PROCESSOR-unknown-freebsd$FREEBSD_REL-gnueabi + else + FREEBSD_REL=`echo "$UNAME_RELEASE" | sed -e 's/[-(].*//'` + GUESS=$UNAME_PROCESSOR-unknown-freebsd$FREEBSD_REL-gnueabihf + fi + ;; + *:FreeBSD:*:*) + UNAME_PROCESSOR=`uname -p` + case $UNAME_PROCESSOR in + amd64) + UNAME_PROCESSOR=x86_64 ;; + i386) + UNAME_PROCESSOR=i586 ;; + esac + FREEBSD_REL=`echo "$UNAME_RELEASE" | sed -e 's/[-(].*//'` + GUESS=$UNAME_PROCESSOR-unknown-freebsd$FREEBSD_REL + ;; + i*:CYGWIN*:*) + GUESS=$UNAME_MACHINE-pc-cygwin + ;; + *:MINGW64*:*) + GUESS=$UNAME_MACHINE-pc-mingw64 + ;; + *:MINGW*:*) + GUESS=$UNAME_MACHINE-pc-mingw32 + ;; + *:MSYS*:*) + GUESS=$UNAME_MACHINE-pc-msys + ;; + i*:PW*:*) + GUESS=$UNAME_MACHINE-pc-pw32 + ;; + *:SerenityOS:*:*) + GUESS=$UNAME_MACHINE-pc-serenity + ;; + *:Interix*:*) + case $UNAME_MACHINE in + x86) + GUESS=i586-pc-interix$UNAME_RELEASE + ;; + authenticamd | genuineintel | EM64T) + GUESS=x86_64-unknown-interix$UNAME_RELEASE + ;; + IA64) + GUESS=ia64-unknown-interix$UNAME_RELEASE + ;; + esac ;; + i*:UWIN*:*) + GUESS=$UNAME_MACHINE-pc-uwin + ;; + amd64:CYGWIN*:*:* | x86_64:CYGWIN*:*:*) + GUESS=x86_64-pc-cygwin + ;; + prep*:SunOS:5.*:*) + SUN_REL=`echo "$UNAME_RELEASE" | sed -e 's/[^.]*//'` + GUESS=powerpcle-unknown-solaris2$SUN_REL + ;; + *:GNU:*:*) + # the GNU system + GNU_ARCH=`echo "$UNAME_MACHINE" | sed -e 's,[-/].*$,,'` + GNU_REL=`echo "$UNAME_RELEASE" | sed -e 's,/.*$,,'` + GUESS=$GNU_ARCH-unknown-$LIBC$GNU_REL + ;; + *:GNU/*:*:*) + # other systems with GNU libc and userland + GNU_SYS=`echo "$UNAME_SYSTEM" | sed 's,^[^/]*/,,' | tr "[:upper:]" "[:lower:]"` + GNU_REL=`echo "$UNAME_RELEASE" | sed -e 's/[-(].*//'` + GUESS=$UNAME_MACHINE-unknown-$GNU_SYS$GNU_REL-$LIBC + ;; + x86_64:[Mm]anagarm:*:*|i?86:[Mm]anagarm:*:*) + GUESS="$UNAME_MACHINE-pc-managarm-mlibc" + ;; + *:[Mm]anagarm:*:*) + GUESS="$UNAME_MACHINE-unknown-managarm-mlibc" + ;; + *:Minix:*:*) + GUESS=$UNAME_MACHINE-unknown-minix + ;; + aarch64:Linux:*:*) + set_cc_for_build + CPU=$UNAME_MACHINE + LIBCABI=$LIBC + if test "$CC_FOR_BUILD" != no_compiler_found; then + ABI=64 + sed 's/^ //' << EOF > "$dummy.c" + #ifdef __ARM_EABI__ + #ifdef __ARM_PCS_VFP + ABI=eabihf + #else + ABI=eabi + #endif + #endif +EOF + cc_set_abi=`$CC_FOR_BUILD -E "$dummy.c" 2>/dev/null | grep '^ABI' | sed 's, ,,g'` + eval "$cc_set_abi" + case $ABI in + eabi | eabihf) CPU=armv8l; LIBCABI=$LIBC$ABI ;; + esac + fi + GUESS=$CPU-unknown-linux-$LIBCABI + ;; + aarch64_be:Linux:*:*) + UNAME_MACHINE=aarch64_be + GUESS=$UNAME_MACHINE-unknown-linux-$LIBC + ;; + alpha:Linux:*:*) + case `sed -n '/^cpu model/s/^.*: \(.*\)/\1/p' /proc/cpuinfo 2>/dev/null` in + EV5) UNAME_MACHINE=alphaev5 ;; + EV56) UNAME_MACHINE=alphaev56 ;; + PCA56) UNAME_MACHINE=alphapca56 ;; + PCA57) UNAME_MACHINE=alphapca56 ;; + EV6) UNAME_MACHINE=alphaev6 ;; + EV67) UNAME_MACHINE=alphaev67 ;; + EV68*) UNAME_MACHINE=alphaev68 ;; + esac + objdump --private-headers /bin/sh | grep -q ld.so.1 + if test "$?" = 0 ; then LIBC=gnulibc1 ; fi + GUESS=$UNAME_MACHINE-unknown-linux-$LIBC + ;; + arc:Linux:*:* | arceb:Linux:*:* | arc32:Linux:*:* | arc64:Linux:*:*) + GUESS=$UNAME_MACHINE-unknown-linux-$LIBC + ;; + arm*:Linux:*:*) + set_cc_for_build + if echo __ARM_EABI__ | $CC_FOR_BUILD -E - 2>/dev/null \ + | grep -q __ARM_EABI__ + then + GUESS=$UNAME_MACHINE-unknown-linux-$LIBC + else + if echo __ARM_PCS_VFP | $CC_FOR_BUILD -E - 2>/dev/null \ + | grep -q __ARM_PCS_VFP + then + GUESS=$UNAME_MACHINE-unknown-linux-${LIBC}eabi + else + GUESS=$UNAME_MACHINE-unknown-linux-${LIBC}eabihf + fi + fi + ;; + avr32*:Linux:*:*) + GUESS=$UNAME_MACHINE-unknown-linux-$LIBC + ;; + cris:Linux:*:*) + GUESS=$UNAME_MACHINE-axis-linux-$LIBC + ;; + crisv32:Linux:*:*) + GUESS=$UNAME_MACHINE-axis-linux-$LIBC + ;; + e2k:Linux:*:*) + GUESS=$UNAME_MACHINE-unknown-linux-$LIBC + ;; + frv:Linux:*:*) + GUESS=$UNAME_MACHINE-unknown-linux-$LIBC + ;; + hexagon:Linux:*:*) + GUESS=$UNAME_MACHINE-unknown-linux-$LIBC + ;; + i*86:Linux:*:*) + GUESS=$UNAME_MACHINE-pc-linux-$LIBC + ;; + ia64:Linux:*:*) + GUESS=$UNAME_MACHINE-unknown-linux-$LIBC + ;; + k1om:Linux:*:*) + GUESS=$UNAME_MACHINE-unknown-linux-$LIBC + ;; + kvx:Linux:*:*) + GUESS=$UNAME_MACHINE-unknown-linux-$LIBC + ;; + kvx:cos:*:*) + GUESS=$UNAME_MACHINE-unknown-cos + ;; + kvx:mbr:*:*) + GUESS=$UNAME_MACHINE-unknown-mbr + ;; + loongarch32:Linux:*:* | loongarch64:Linux:*:*) + GUESS=$UNAME_MACHINE-unknown-linux-$LIBC + ;; + m32r*:Linux:*:*) + GUESS=$UNAME_MACHINE-unknown-linux-$LIBC + ;; + m68*:Linux:*:*) + GUESS=$UNAME_MACHINE-unknown-linux-$LIBC + ;; + mips:Linux:*:* | mips64:Linux:*:*) + set_cc_for_build + IS_GLIBC=0 + test x"${LIBC}" = xgnu && IS_GLIBC=1 + sed 's/^ //' << EOF > "$dummy.c" + #undef CPU + #undef mips + #undef mipsel + #undef mips64 + #undef mips64el + #if ${IS_GLIBC} && defined(_ABI64) + LIBCABI=gnuabi64 + #else + #if ${IS_GLIBC} && defined(_ABIN32) + LIBCABI=gnuabin32 + #else + LIBCABI=${LIBC} + #endif + #endif + + #if ${IS_GLIBC} && defined(__mips64) && defined(__mips_isa_rev) && __mips_isa_rev>=6 + CPU=mipsisa64r6 + #else + #if ${IS_GLIBC} && !defined(__mips64) && defined(__mips_isa_rev) && __mips_isa_rev>=6 + CPU=mipsisa32r6 + #else + #if defined(__mips64) + CPU=mips64 + #else + CPU=mips + #endif + #endif + #endif + + #if defined(__MIPSEL__) || defined(__MIPSEL) || defined(_MIPSEL) || defined(MIPSEL) + MIPS_ENDIAN=el + #else + #if defined(__MIPSEB__) || defined(__MIPSEB) || defined(_MIPSEB) || defined(MIPSEB) + MIPS_ENDIAN= + #else + MIPS_ENDIAN= + #endif + #endif +EOF + cc_set_vars=`$CC_FOR_BUILD -E "$dummy.c" 2>/dev/null | grep '^CPU\|^MIPS_ENDIAN\|^LIBCABI'` + eval "$cc_set_vars" + test "x$CPU" != x && { echo "$CPU${MIPS_ENDIAN}-unknown-linux-$LIBCABI"; exit; } + ;; + mips64el:Linux:*:*) + GUESS=$UNAME_MACHINE-unknown-linux-$LIBC + ;; + openrisc*:Linux:*:*) + GUESS=or1k-unknown-linux-$LIBC + ;; + or32:Linux:*:* | or1k*:Linux:*:*) + GUESS=$UNAME_MACHINE-unknown-linux-$LIBC + ;; + padre:Linux:*:*) + GUESS=sparc-unknown-linux-$LIBC + ;; + parisc64:Linux:*:* | hppa64:Linux:*:*) + GUESS=hppa64-unknown-linux-$LIBC + ;; + parisc:Linux:*:* | hppa:Linux:*:*) + # Look for CPU level + case `grep '^cpu[^a-z]*:' /proc/cpuinfo 2>/dev/null | cut -d' ' -f2` in + PA7*) GUESS=hppa1.1-unknown-linux-$LIBC ;; + PA8*) GUESS=hppa2.0-unknown-linux-$LIBC ;; + *) GUESS=hppa-unknown-linux-$LIBC ;; + esac + ;; + ppc64:Linux:*:*) + GUESS=powerpc64-unknown-linux-$LIBC + ;; + ppc:Linux:*:*) + GUESS=powerpc-unknown-linux-$LIBC + ;; + ppc64le:Linux:*:*) + GUESS=powerpc64le-unknown-linux-$LIBC + ;; + ppcle:Linux:*:*) + GUESS=powerpcle-unknown-linux-$LIBC + ;; + riscv32:Linux:*:* | riscv32be:Linux:*:* | riscv64:Linux:*:* | riscv64be:Linux:*:*) + GUESS=$UNAME_MACHINE-unknown-linux-$LIBC + ;; + s390:Linux:*:* | s390x:Linux:*:*) + GUESS=$UNAME_MACHINE-ibm-linux-$LIBC + ;; + sh64*:Linux:*:*) + GUESS=$UNAME_MACHINE-unknown-linux-$LIBC + ;; + sh*:Linux:*:*) + GUESS=$UNAME_MACHINE-unknown-linux-$LIBC + ;; + sparc:Linux:*:* | sparc64:Linux:*:*) + GUESS=$UNAME_MACHINE-unknown-linux-$LIBC + ;; + tile*:Linux:*:*) + GUESS=$UNAME_MACHINE-unknown-linux-$LIBC + ;; + vax:Linux:*:*) + GUESS=$UNAME_MACHINE-dec-linux-$LIBC + ;; + x86_64:Linux:*:*) + set_cc_for_build + CPU=$UNAME_MACHINE + LIBCABI=$LIBC + if test "$CC_FOR_BUILD" != no_compiler_found; then + ABI=64 + sed 's/^ //' << EOF > "$dummy.c" + #ifdef __i386__ + ABI=x86 + #else + #ifdef __ILP32__ + ABI=x32 + #endif + #endif +EOF + cc_set_abi=`$CC_FOR_BUILD -E "$dummy.c" 2>/dev/null | grep '^ABI' | sed 's, ,,g'` + eval "$cc_set_abi" + case $ABI in + x86) CPU=i686 ;; + x32) LIBCABI=${LIBC}x32 ;; + esac + fi + GUESS=$CPU-pc-linux-$LIBCABI + ;; + xtensa*:Linux:*:*) + GUESS=$UNAME_MACHINE-unknown-linux-$LIBC + ;; + i*86:DYNIX/ptx:4*:*) + # ptx 4.0 does uname -s correctly, with DYNIX/ptx in there. + # earlier versions are messed up and put the nodename in both + # sysname and nodename. + GUESS=i386-sequent-sysv4 + ;; + i*86:UNIX_SV:4.2MP:2.*) + # Unixware is an offshoot of SVR4, but it has its own version + # number series starting with 2... + # I am not positive that other SVR4 systems won't match this, + # I just have to hope. -- rms. + # Use sysv4.2uw... so that sysv4* matches it. + GUESS=$UNAME_MACHINE-pc-sysv4.2uw$UNAME_VERSION + ;; + i*86:OS/2:*:*) + # If we were able to find 'uname', then EMX Unix compatibility + # is probably installed. + GUESS=$UNAME_MACHINE-pc-os2-emx + ;; + i*86:XTS-300:*:STOP) + GUESS=$UNAME_MACHINE-unknown-stop + ;; + i*86:atheos:*:*) + GUESS=$UNAME_MACHINE-unknown-atheos + ;; + i*86:syllable:*:*) + GUESS=$UNAME_MACHINE-pc-syllable + ;; + i*86:LynxOS:2.*:* | i*86:LynxOS:3.[01]*:* | i*86:LynxOS:4.[02]*:*) + GUESS=i386-unknown-lynxos$UNAME_RELEASE + ;; + i*86:*DOS:*:*) + GUESS=$UNAME_MACHINE-pc-msdosdjgpp + ;; + i*86:*:4.*:*) + UNAME_REL=`echo "$UNAME_RELEASE" | sed 's/\/MP$//'` + if grep Novell /usr/include/link.h >/dev/null 2>/dev/null; then + GUESS=$UNAME_MACHINE-univel-sysv$UNAME_REL + else + GUESS=$UNAME_MACHINE-pc-sysv$UNAME_REL + fi + ;; + i*86:*:5:[678]*) + # UnixWare 7.x, OpenUNIX and OpenServer 6. + case `/bin/uname -X | grep "^Machine"` in + *486*) UNAME_MACHINE=i486 ;; + *Pentium) UNAME_MACHINE=i586 ;; + *Pent*|*Celeron) UNAME_MACHINE=i686 ;; + esac + GUESS=$UNAME_MACHINE-unknown-sysv${UNAME_RELEASE}${UNAME_SYSTEM}${UNAME_VERSION} + ;; + i*86:*:3.2:*) + if test -f /usr/options/cb.name; then + UNAME_REL=`sed -n 's/.*Version //p' /dev/null >/dev/null ; then + UNAME_REL=`(/bin/uname -X|grep Release|sed -e 's/.*= //')` + (/bin/uname -X|grep i80486 >/dev/null) && UNAME_MACHINE=i486 + (/bin/uname -X|grep '^Machine.*Pentium' >/dev/null) \ + && UNAME_MACHINE=i586 + (/bin/uname -X|grep '^Machine.*Pent *II' >/dev/null) \ + && UNAME_MACHINE=i686 + (/bin/uname -X|grep '^Machine.*Pentium Pro' >/dev/null) \ + && UNAME_MACHINE=i686 + GUESS=$UNAME_MACHINE-pc-sco$UNAME_REL + else + GUESS=$UNAME_MACHINE-pc-sysv32 + fi + ;; + pc:*:*:*) + # Left here for compatibility: + # uname -m prints for DJGPP always 'pc', but it prints nothing about + # the processor, so we play safe by assuming i586. + # Note: whatever this is, it MUST be the same as what config.sub + # prints for the "djgpp" host, or else GDB configure will decide that + # this is a cross-build. + GUESS=i586-pc-msdosdjgpp + ;; + Intel:Mach:3*:*) + GUESS=i386-pc-mach3 + ;; + paragon:*:*:*) + GUESS=i860-intel-osf1 + ;; + i860:*:4.*:*) # i860-SVR4 + if grep Stardent /usr/include/sys/uadmin.h >/dev/null 2>&1 ; then + GUESS=i860-stardent-sysv$UNAME_RELEASE # Stardent Vistra i860-SVR4 + else # Add other i860-SVR4 vendors below as they are discovered. + GUESS=i860-unknown-sysv$UNAME_RELEASE # Unknown i860-SVR4 + fi + ;; + mini*:CTIX:SYS*5:*) + # "miniframe" + GUESS=m68010-convergent-sysv + ;; + mc68k:UNIX:SYSTEM5:3.51m) + GUESS=m68k-convergent-sysv + ;; + M680?0:D-NIX:5.3:*) + GUESS=m68k-diab-dnix + ;; + M68*:*:R3V[5678]*:*) + test -r /sysV68 && { echo 'm68k-motorola-sysv'; exit; } ;; + 3[345]??:*:4.0:3.0 | 3[34]??A:*:4.0:3.0 | 3[34]??,*:*:4.0:3.0 | 3[34]??/*:*:4.0:3.0 | 4400:*:4.0:3.0 | 4850:*:4.0:3.0 | SKA40:*:4.0:3.0 | SDS2:*:4.0:3.0 | SHG2:*:4.0:3.0 | S7501*:*:4.0:3.0) + OS_REL='' + test -r /etc/.relid \ + && OS_REL=.`sed -n 's/[^ ]* [^ ]* \([0-9][0-9]\).*/\1/p' < /etc/.relid` + /bin/uname -p 2>/dev/null | grep 86 >/dev/null \ + && { echo i486-ncr-sysv4.3"$OS_REL"; exit; } + /bin/uname -p 2>/dev/null | /bin/grep entium >/dev/null \ + && { echo i586-ncr-sysv4.3"$OS_REL"; exit; } ;; + 3[34]??:*:4.0:* | 3[34]??,*:*:4.0:*) + /bin/uname -p 2>/dev/null | grep 86 >/dev/null \ + && { echo i486-ncr-sysv4; exit; } ;; + NCR*:*:4.2:* | MPRAS*:*:4.2:*) + OS_REL='.3' + test -r /etc/.relid \ + && OS_REL=.`sed -n 's/[^ ]* [^ ]* \([0-9][0-9]\).*/\1/p' < /etc/.relid` + /bin/uname -p 2>/dev/null | grep 86 >/dev/null \ + && { echo i486-ncr-sysv4.3"$OS_REL"; exit; } + /bin/uname -p 2>/dev/null | /bin/grep entium >/dev/null \ + && { echo i586-ncr-sysv4.3"$OS_REL"; exit; } + /bin/uname -p 2>/dev/null | /bin/grep pteron >/dev/null \ + && { echo i586-ncr-sysv4.3"$OS_REL"; exit; } ;; + m68*:LynxOS:2.*:* | m68*:LynxOS:3.0*:*) + GUESS=m68k-unknown-lynxos$UNAME_RELEASE + ;; + mc68030:UNIX_System_V:4.*:*) + GUESS=m68k-atari-sysv4 + ;; + TSUNAMI:LynxOS:2.*:*) + GUESS=sparc-unknown-lynxos$UNAME_RELEASE + ;; + rs6000:LynxOS:2.*:*) + GUESS=rs6000-unknown-lynxos$UNAME_RELEASE + ;; + PowerPC:LynxOS:2.*:* | PowerPC:LynxOS:3.[01]*:* | PowerPC:LynxOS:4.[02]*:*) + GUESS=powerpc-unknown-lynxos$UNAME_RELEASE + ;; + SM[BE]S:UNIX_SV:*:*) + GUESS=mips-dde-sysv$UNAME_RELEASE + ;; + RM*:ReliantUNIX-*:*:*) + GUESS=mips-sni-sysv4 + ;; + RM*:SINIX-*:*:*) + GUESS=mips-sni-sysv4 + ;; + *:SINIX-*:*:*) + if uname -p 2>/dev/null >/dev/null ; then + UNAME_MACHINE=`(uname -p) 2>/dev/null` + GUESS=$UNAME_MACHINE-sni-sysv4 + else + GUESS=ns32k-sni-sysv + fi + ;; + PENTIUM:*:4.0*:*) # Unisys 'ClearPath HMP IX 4000' SVR4/MP effort + # says + GUESS=i586-unisys-sysv4 + ;; + *:UNIX_System_V:4*:FTX*) + # From Gerald Hewes . + # How about differentiating between stratus architectures? -djm + GUESS=hppa1.1-stratus-sysv4 + ;; + *:*:*:FTX*) + # From seanf@swdc.stratus.com. + GUESS=i860-stratus-sysv4 + ;; + i*86:VOS:*:*) + # From Paul.Green@stratus.com. + GUESS=$UNAME_MACHINE-stratus-vos + ;; + *:VOS:*:*) + # From Paul.Green@stratus.com. + GUESS=hppa1.1-stratus-vos + ;; + mc68*:A/UX:*:*) + GUESS=m68k-apple-aux$UNAME_RELEASE + ;; + news*:NEWS-OS:6*:*) + GUESS=mips-sony-newsos6 + ;; + R[34]000:*System_V*:*:* | R4000:UNIX_SYSV:*:* | R*000:UNIX_SV:*:*) + if test -d /usr/nec; then + GUESS=mips-nec-sysv$UNAME_RELEASE + else + GUESS=mips-unknown-sysv$UNAME_RELEASE + fi + ;; + BeBox:BeOS:*:*) # BeOS running on hardware made by Be, PPC only. + GUESS=powerpc-be-beos + ;; + BeMac:BeOS:*:*) # BeOS running on Mac or Mac clone, PPC only. + GUESS=powerpc-apple-beos + ;; + BePC:BeOS:*:*) # BeOS running on Intel PC compatible. + GUESS=i586-pc-beos + ;; + BePC:Haiku:*:*) # Haiku running on Intel PC compatible. + GUESS=i586-pc-haiku + ;; + ppc:Haiku:*:*) # Haiku running on Apple PowerPC + GUESS=powerpc-apple-haiku + ;; + *:Haiku:*:*) # Haiku modern gcc (not bound by BeOS compat) + GUESS=$UNAME_MACHINE-unknown-haiku + ;; + SX-4:SUPER-UX:*:*) + GUESS=sx4-nec-superux$UNAME_RELEASE + ;; + SX-5:SUPER-UX:*:*) + GUESS=sx5-nec-superux$UNAME_RELEASE + ;; + SX-6:SUPER-UX:*:*) + GUESS=sx6-nec-superux$UNAME_RELEASE + ;; + SX-7:SUPER-UX:*:*) + GUESS=sx7-nec-superux$UNAME_RELEASE + ;; + SX-8:SUPER-UX:*:*) + GUESS=sx8-nec-superux$UNAME_RELEASE + ;; + SX-8R:SUPER-UX:*:*) + GUESS=sx8r-nec-superux$UNAME_RELEASE + ;; + SX-ACE:SUPER-UX:*:*) + GUESS=sxace-nec-superux$UNAME_RELEASE + ;; + Power*:Rhapsody:*:*) + GUESS=powerpc-apple-rhapsody$UNAME_RELEASE + ;; + *:Rhapsody:*:*) + GUESS=$UNAME_MACHINE-apple-rhapsody$UNAME_RELEASE + ;; + arm64:Darwin:*:*) + GUESS=aarch64-apple-darwin$UNAME_RELEASE + ;; + *:Darwin:*:*) + UNAME_PROCESSOR=`uname -p` + case $UNAME_PROCESSOR in + unknown) UNAME_PROCESSOR=powerpc ;; + esac + if command -v xcode-select > /dev/null 2> /dev/null && \ + ! xcode-select --print-path > /dev/null 2> /dev/null ; then + # Avoid executing cc if there is no toolchain installed as + # cc will be a stub that puts up a graphical alert + # prompting the user to install developer tools. + CC_FOR_BUILD=no_compiler_found + else + set_cc_for_build + fi + if test "$CC_FOR_BUILD" != no_compiler_found; then + if (echo '#ifdef __LP64__'; echo IS_64BIT_ARCH; echo '#endif') | \ + (CCOPTS="" $CC_FOR_BUILD -E - 2>/dev/null) | \ + grep IS_64BIT_ARCH >/dev/null + then + case $UNAME_PROCESSOR in + i386) UNAME_PROCESSOR=x86_64 ;; + powerpc) UNAME_PROCESSOR=powerpc64 ;; + esac + fi + # On 10.4-10.6 one might compile for PowerPC via gcc -arch ppc + if (echo '#ifdef __POWERPC__'; echo IS_PPC; echo '#endif') | \ + (CCOPTS="" $CC_FOR_BUILD -E - 2>/dev/null) | \ + grep IS_PPC >/dev/null + then + UNAME_PROCESSOR=powerpc + fi + elif test "$UNAME_PROCESSOR" = i386 ; then + # uname -m returns i386 or x86_64 + UNAME_PROCESSOR=$UNAME_MACHINE + fi + GUESS=$UNAME_PROCESSOR-apple-darwin$UNAME_RELEASE + ;; + *:procnto*:*:* | *:QNX:[0123456789]*:*) + UNAME_PROCESSOR=`uname -p` + if test "$UNAME_PROCESSOR" = x86; then + UNAME_PROCESSOR=i386 + UNAME_MACHINE=pc + fi + GUESS=$UNAME_PROCESSOR-$UNAME_MACHINE-nto-qnx$UNAME_RELEASE + ;; + *:QNX:*:4*) + GUESS=i386-pc-qnx + ;; + NEO-*:NONSTOP_KERNEL:*:*) + GUESS=neo-tandem-nsk$UNAME_RELEASE + ;; + NSE-*:NONSTOP_KERNEL:*:*) + GUESS=nse-tandem-nsk$UNAME_RELEASE + ;; + NSR-*:NONSTOP_KERNEL:*:*) + GUESS=nsr-tandem-nsk$UNAME_RELEASE + ;; + NSV-*:NONSTOP_KERNEL:*:*) + GUESS=nsv-tandem-nsk$UNAME_RELEASE + ;; + NSX-*:NONSTOP_KERNEL:*:*) + GUESS=nsx-tandem-nsk$UNAME_RELEASE + ;; + *:NonStop-UX:*:*) + GUESS=mips-compaq-nonstopux + ;; + BS2000:POSIX*:*:*) + GUESS=bs2000-siemens-sysv + ;; + DS/*:UNIX_System_V:*:*) + GUESS=$UNAME_MACHINE-$UNAME_SYSTEM-$UNAME_RELEASE + ;; + *:Plan9:*:*) + # "uname -m" is not consistent, so use $cputype instead. 386 + # is converted to i386 for consistency with other x86 + # operating systems. + if test "${cputype-}" = 386; then + UNAME_MACHINE=i386 + elif test "x${cputype-}" != x; then + UNAME_MACHINE=$cputype + fi + GUESS=$UNAME_MACHINE-unknown-plan9 + ;; + *:TOPS-10:*:*) + GUESS=pdp10-unknown-tops10 + ;; + *:TENEX:*:*) + GUESS=pdp10-unknown-tenex + ;; + KS10:TOPS-20:*:* | KL10:TOPS-20:*:* | TYPE4:TOPS-20:*:*) + GUESS=pdp10-dec-tops20 + ;; + XKL-1:TOPS-20:*:* | TYPE5:TOPS-20:*:*) + GUESS=pdp10-xkl-tops20 + ;; + *:TOPS-20:*:*) + GUESS=pdp10-unknown-tops20 + ;; + *:ITS:*:*) + GUESS=pdp10-unknown-its + ;; + SEI:*:*:SEIUX) + GUESS=mips-sei-seiux$UNAME_RELEASE + ;; + *:DragonFly:*:*) + DRAGONFLY_REL=`echo "$UNAME_RELEASE" | sed -e 's/[-(].*//'` + GUESS=$UNAME_MACHINE-unknown-dragonfly$DRAGONFLY_REL + ;; + *:*VMS:*:*) + UNAME_MACHINE=`(uname -p) 2>/dev/null` + case $UNAME_MACHINE in + A*) GUESS=alpha-dec-vms ;; + I*) GUESS=ia64-dec-vms ;; + V*) GUESS=vax-dec-vms ;; + esac ;; + *:XENIX:*:SysV) + GUESS=i386-pc-xenix + ;; + i*86:skyos:*:*) + SKYOS_REL=`echo "$UNAME_RELEASE" | sed -e 's/ .*$//'` + GUESS=$UNAME_MACHINE-pc-skyos$SKYOS_REL + ;; + i*86:rdos:*:*) + GUESS=$UNAME_MACHINE-pc-rdos + ;; + i*86:Fiwix:*:*) + GUESS=$UNAME_MACHINE-pc-fiwix + ;; + *:AROS:*:*) + GUESS=$UNAME_MACHINE-unknown-aros + ;; + x86_64:VMkernel:*:*) + GUESS=$UNAME_MACHINE-unknown-esx + ;; + amd64:Isilon\ OneFS:*:*) + GUESS=x86_64-unknown-onefs + ;; + *:Unleashed:*:*) + GUESS=$UNAME_MACHINE-unknown-unleashed$UNAME_RELEASE + ;; + *:Ironclad:*:*) + GUESS=$UNAME_MACHINE-unknown-ironclad + ;; +esac + +# Do we have a guess based on uname results? +if test "x$GUESS" != x; then + echo "$GUESS" + exit +fi + +# No uname command or uname output not recognized. +set_cc_for_build +cat > "$dummy.c" < +#include +#endif +#if defined(ultrix) || defined(_ultrix) || defined(__ultrix) || defined(__ultrix__) +#if defined (vax) || defined (__vax) || defined (__vax__) || defined(mips) || defined(__mips) || defined(__mips__) || defined(MIPS) || defined(__MIPS__) +#include +#if defined(_SIZE_T_) || defined(SIGLOST) +#include +#endif +#endif +#endif +int +main () +{ +#if defined (sony) +#if defined (MIPSEB) + /* BFD wants "bsd" instead of "newsos". Perhaps BFD should be changed, + I don't know.... */ + printf ("mips-sony-bsd\n"); exit (0); +#else +#include + printf ("m68k-sony-newsos%s\n", +#ifdef NEWSOS4 + "4" +#else + "" +#endif + ); exit (0); +#endif +#endif + +#if defined (NeXT) +#if !defined (__ARCHITECTURE__) +#define __ARCHITECTURE__ "m68k" +#endif + int version; + version=`(hostinfo | sed -n 's/.*NeXT Mach \([0-9]*\).*/\1/p') 2>/dev/null`; + if (version < 4) + printf ("%s-next-nextstep%d\n", __ARCHITECTURE__, version); + else + printf ("%s-next-openstep%d\n", __ARCHITECTURE__, version); + exit (0); +#endif + +#if defined (MULTIMAX) || defined (n16) +#if defined (UMAXV) + printf ("ns32k-encore-sysv\n"); exit (0); +#else +#if defined (CMU) + printf ("ns32k-encore-mach\n"); exit (0); +#else + printf ("ns32k-encore-bsd\n"); exit (0); +#endif +#endif +#endif + +#if defined (__386BSD__) + printf ("i386-pc-bsd\n"); exit (0); +#endif + +#if defined (sequent) +#if defined (i386) + printf ("i386-sequent-dynix\n"); exit (0); +#endif +#if defined (ns32000) + printf ("ns32k-sequent-dynix\n"); exit (0); +#endif +#endif + +#if defined (_SEQUENT_) + struct utsname un; + + uname(&un); + if (strncmp(un.version, "V2", 2) == 0) { + printf ("i386-sequent-ptx2\n"); exit (0); + } + if (strncmp(un.version, "V1", 2) == 0) { /* XXX is V1 correct? */ + printf ("i386-sequent-ptx1\n"); exit (0); + } + printf ("i386-sequent-ptx\n"); exit (0); +#endif + +#if defined (vax) +#if !defined (ultrix) +#include +#if defined (BSD) +#if BSD == 43 + printf ("vax-dec-bsd4.3\n"); exit (0); +#else +#if BSD == 199006 + printf ("vax-dec-bsd4.3reno\n"); exit (0); +#else + printf ("vax-dec-bsd\n"); exit (0); +#endif +#endif +#else + printf ("vax-dec-bsd\n"); exit (0); +#endif +#else +#if defined(_SIZE_T_) || defined(SIGLOST) + struct utsname un; + uname (&un); + printf ("vax-dec-ultrix%s\n", un.release); exit (0); +#else + printf ("vax-dec-ultrix\n"); exit (0); +#endif +#endif +#endif +#if defined(ultrix) || defined(_ultrix) || defined(__ultrix) || defined(__ultrix__) +#if defined(mips) || defined(__mips) || defined(__mips__) || defined(MIPS) || defined(__MIPS__) +#if defined(_SIZE_T_) || defined(SIGLOST) + struct utsname *un; + uname (&un); + printf ("mips-dec-ultrix%s\n", un.release); exit (0); +#else + printf ("mips-dec-ultrix\n"); exit (0); +#endif +#endif +#endif + +#if defined (alliant) && defined (i860) + printf ("i860-alliant-bsd\n"); exit (0); +#endif + + exit (1); +} +EOF + +$CC_FOR_BUILD -o "$dummy" "$dummy.c" 2>/dev/null && SYSTEM_NAME=`"$dummy"` && + { echo "$SYSTEM_NAME"; exit; } + +# Apollos put the system type in the environment. +test -d /usr/apollo && { echo "$ISP-apollo-$SYSTYPE"; exit; } + +echo "$0: unable to guess system type" >&2 + +case $UNAME_MACHINE:$UNAME_SYSTEM in + mips:Linux | mips64:Linux) + # If we got here on MIPS GNU/Linux, output extra information. + cat >&2 <&2 <&2 </dev/null || echo unknown` +uname -r = `(uname -r) 2>/dev/null || echo unknown` +uname -s = `(uname -s) 2>/dev/null || echo unknown` +uname -v = `(uname -v) 2>/dev/null || echo unknown` + +/usr/bin/uname -p = `(/usr/bin/uname -p) 2>/dev/null` +/bin/uname -X = `(/bin/uname -X) 2>/dev/null` + +hostinfo = `(hostinfo) 2>/dev/null` +/bin/universe = `(/bin/universe) 2>/dev/null` +/usr/bin/arch -k = `(/usr/bin/arch -k) 2>/dev/null` +/bin/arch = `(/bin/arch) 2>/dev/null` +/usr/bin/oslevel = `(/usr/bin/oslevel) 2>/dev/null` +/usr/convex/getsysinfo = `(/usr/convex/getsysinfo) 2>/dev/null` + +UNAME_MACHINE = "$UNAME_MACHINE" +UNAME_RELEASE = "$UNAME_RELEASE" +UNAME_SYSTEM = "$UNAME_SYSTEM" +UNAME_VERSION = "$UNAME_VERSION" +EOF +fi + +exit 1 + +# Local variables: +# eval: (add-hook 'before-save-hook 'time-stamp) +# time-stamp-start: "timestamp='" +# time-stamp-format: "%:y-%02m-%02d" +# time-stamp-end: "'" +# End: diff --git a/autosetup/autosetup-config.sub b/autosetup/autosetup-config.sub new file mode 100755 index 0000000000..4aaae46f6f --- /dev/null +++ b/autosetup/autosetup-config.sub @@ -0,0 +1,2354 @@ +#! /bin/sh +# Configuration validation subroutine script. +# Copyright 1992-2024 Free Software Foundation, Inc. + +# shellcheck disable=SC2006,SC2268,SC2162 # see below for rationale + +timestamp='2024-05-27' + +# This file is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, see . +# +# As a special exception to the GNU General Public License, if you +# distribute this file as part of a program that contains a +# configuration script generated by Autoconf, you may include it under +# the same distribution terms that you use for the rest of that +# program. This Exception is an additional permission under section 7 +# of the GNU General Public License, version 3 ("GPLv3"). + + +# Please send patches to . +# +# Configuration subroutine to validate and canonicalize a configuration type. +# Supply the specified configuration type as an argument. +# If it is invalid, we print an error message on stderr and exit with code 1. +# Otherwise, we print the canonical config type on stdout and succeed. + +# You can get the latest version of this script from: +# https://git.savannah.gnu.org/cgit/config.git/plain/config.sub + +# This file is supposed to be the same for all GNU packages +# and recognize all the CPU types, system types and aliases +# that are meaningful with *any* GNU software. +# Each package is responsible for reporting which valid configurations +# it does not support. The user should be able to distinguish +# a failure to support a valid configuration from a meaningless +# configuration. + +# The goal of this file is to map all the various variations of a given +# machine specification into a single specification in the form: +# CPU_TYPE-MANUFACTURER-OPERATING_SYSTEM +# or in some cases, the newer four-part form: +# CPU_TYPE-MANUFACTURER-KERNEL-OPERATING_SYSTEM +# It is wrong to echo any other type of specification. + +# The "shellcheck disable" line above the timestamp inhibits complaints +# about features and limitations of the classic Bourne shell that were +# superseded or lifted in POSIX. However, this script identifies a wide +# variety of pre-POSIX systems that do not have POSIX shells at all, and +# even some reasonably current systems (Solaris 10 as case-in-point) still +# have a pre-POSIX /bin/sh. + +me=`echo "$0" | sed -e 's,.*/,,'` + +usage="\ +Usage: $0 [OPTION] CPU-MFR-OPSYS or ALIAS + +Canonicalize a configuration name. + +Options: + -h, --help print this help, then exit + -t, --time-stamp print date of last modification, then exit + -v, --version print version number, then exit + +Report bugs and patches to ." + +version="\ +GNU config.sub ($timestamp) + +Copyright 1992-2024 Free Software Foundation, Inc. + +This is free software; see the source for copying conditions. There is NO +warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." + +help=" +Try '$me --help' for more information." + +# Parse command line +while test $# -gt 0 ; do + case $1 in + --time-stamp | --time* | -t ) + echo "$timestamp" ; exit ;; + --version | -v ) + echo "$version" ; exit ;; + --help | --h* | -h ) + echo "$usage"; exit ;; + -- ) # Stop option processing + shift; break ;; + - ) # Use stdin as input. + break ;; + -* ) + echo "$me: invalid option $1$help" >&2 + exit 1 ;; + + *local*) + # First pass through any local machine types. + echo "$1" + exit ;; + + * ) + break ;; + esac +done + +case $# in + 0) echo "$me: missing argument$help" >&2 + exit 1;; + 1) ;; + *) echo "$me: too many arguments$help" >&2 + exit 1;; +esac + +# Split fields of configuration type +saved_IFS=$IFS +IFS="-" read field1 field2 field3 field4 <&2 + exit 1 + ;; + *-*-*-*) + basic_machine=$field1-$field2 + basic_os=$field3-$field4 + ;; + *-*-*) + # Ambiguous whether COMPANY is present, or skipped and KERNEL-OS is two + # parts + maybe_os=$field2-$field3 + case $maybe_os in + cloudabi*-eabi* \ + | kfreebsd*-gnu* \ + | knetbsd*-gnu* \ + | kopensolaris*-gnu* \ + | linux-* \ + | managarm-* \ + | netbsd*-eabi* \ + | netbsd*-gnu* \ + | nto-qnx* \ + | os2-emx* \ + | rtmk-nova* \ + | storm-chaos* \ + | uclinux-gnu* \ + | uclinux-uclibc* \ + | windows-* ) + basic_machine=$field1 + basic_os=$maybe_os + ;; + android-linux) + basic_machine=$field1-unknown + basic_os=linux-android + ;; + *) + basic_machine=$field1-$field2 + basic_os=$field3 + ;; + esac + ;; + *-*) + case $field1-$field2 in + # Shorthands that happen to contain a single dash + convex-c[12] | convex-c3[248]) + basic_machine=$field2-convex + basic_os= + ;; + decstation-3100) + basic_machine=mips-dec + basic_os= + ;; + *-*) + # Second component is usually, but not always the OS + case $field2 in + # Do not treat sunos as a manufacturer + sun*os*) + basic_machine=$field1 + basic_os=$field2 + ;; + # Manufacturers + 3100* \ + | 32* \ + | 3300* \ + | 3600* \ + | 7300* \ + | acorn \ + | altos* \ + | apollo \ + | apple \ + | atari \ + | att* \ + | axis \ + | be \ + | bull \ + | cbm \ + | ccur \ + | cisco \ + | commodore \ + | convergent* \ + | convex* \ + | cray \ + | crds \ + | dec* \ + | delta* \ + | dg \ + | digital \ + | dolphin \ + | encore* \ + | gould \ + | harris \ + | highlevel \ + | hitachi* \ + | hp \ + | ibm* \ + | intergraph \ + | isi* \ + | knuth \ + | masscomp \ + | microblaze* \ + | mips* \ + | motorola* \ + | ncr* \ + | news \ + | next \ + | ns \ + | oki \ + | omron* \ + | pc533* \ + | rebel \ + | rom68k \ + | rombug \ + | semi \ + | sequent* \ + | siemens \ + | sgi* \ + | siemens \ + | sim \ + | sni \ + | sony* \ + | stratus \ + | sun \ + | sun[234]* \ + | tektronix \ + | tti* \ + | ultra \ + | unicom* \ + | wec \ + | winbond \ + | wrs) + basic_machine=$field1-$field2 + basic_os= + ;; + zephyr*) + basic_machine=$field1-unknown + basic_os=$field2 + ;; + *) + basic_machine=$field1 + basic_os=$field2 + ;; + esac + ;; + esac + ;; + *) + # Convert single-component short-hands not valid as part of + # multi-component configurations. + case $field1 in + 386bsd) + basic_machine=i386-pc + basic_os=bsd + ;; + a29khif) + basic_machine=a29k-amd + basic_os=udi + ;; + adobe68k) + basic_machine=m68010-adobe + basic_os=scout + ;; + alliant) + basic_machine=fx80-alliant + basic_os= + ;; + altos | altos3068) + basic_machine=m68k-altos + basic_os= + ;; + am29k) + basic_machine=a29k-none + basic_os=bsd + ;; + amdahl) + basic_machine=580-amdahl + basic_os=sysv + ;; + amiga) + basic_machine=m68k-unknown + basic_os= + ;; + amigaos | amigados) + basic_machine=m68k-unknown + basic_os=amigaos + ;; + amigaunix | amix) + basic_machine=m68k-unknown + basic_os=sysv4 + ;; + apollo68) + basic_machine=m68k-apollo + basic_os=sysv + ;; + apollo68bsd) + basic_machine=m68k-apollo + basic_os=bsd + ;; + aros) + basic_machine=i386-pc + basic_os=aros + ;; + aux) + basic_machine=m68k-apple + basic_os=aux + ;; + balance) + basic_machine=ns32k-sequent + basic_os=dynix + ;; + blackfin) + basic_machine=bfin-unknown + basic_os=linux + ;; + cegcc) + basic_machine=arm-unknown + basic_os=cegcc + ;; + cray) + basic_machine=j90-cray + basic_os=unicos + ;; + crds | unos) + basic_machine=m68k-crds + basic_os= + ;; + da30) + basic_machine=m68k-da30 + basic_os= + ;; + decstation | pmax | pmin | dec3100 | decstatn) + basic_machine=mips-dec + basic_os= + ;; + delta88) + basic_machine=m88k-motorola + basic_os=sysv3 + ;; + dicos) + basic_machine=i686-pc + basic_os=dicos + ;; + djgpp) + basic_machine=i586-pc + basic_os=msdosdjgpp + ;; + ebmon29k) + basic_machine=a29k-amd + basic_os=ebmon + ;; + es1800 | OSE68k | ose68k | ose | OSE) + basic_machine=m68k-ericsson + basic_os=ose + ;; + gmicro) + basic_machine=tron-gmicro + basic_os=sysv + ;; + go32) + basic_machine=i386-pc + basic_os=go32 + ;; + h8300hms) + basic_machine=h8300-hitachi + basic_os=hms + ;; + h8300xray) + basic_machine=h8300-hitachi + basic_os=xray + ;; + h8500hms) + basic_machine=h8500-hitachi + basic_os=hms + ;; + harris) + basic_machine=m88k-harris + basic_os=sysv3 + ;; + hp300 | hp300hpux) + basic_machine=m68k-hp + basic_os=hpux + ;; + hp300bsd) + basic_machine=m68k-hp + basic_os=bsd + ;; + hppaosf) + basic_machine=hppa1.1-hp + basic_os=osf + ;; + hppro) + basic_machine=hppa1.1-hp + basic_os=proelf + ;; + i386mach) + basic_machine=i386-mach + basic_os=mach + ;; + isi68 | isi) + basic_machine=m68k-isi + basic_os=sysv + ;; + m68knommu) + basic_machine=m68k-unknown + basic_os=linux + ;; + magnum | m3230) + basic_machine=mips-mips + basic_os=sysv + ;; + merlin) + basic_machine=ns32k-utek + basic_os=sysv + ;; + mingw64) + basic_machine=x86_64-pc + basic_os=mingw64 + ;; + mingw32) + basic_machine=i686-pc + basic_os=mingw32 + ;; + mingw32ce) + basic_machine=arm-unknown + basic_os=mingw32ce + ;; + monitor) + basic_machine=m68k-rom68k + basic_os=coff + ;; + morphos) + basic_machine=powerpc-unknown + basic_os=morphos + ;; + moxiebox) + basic_machine=moxie-unknown + basic_os=moxiebox + ;; + msdos) + basic_machine=i386-pc + basic_os=msdos + ;; + msys) + basic_machine=i686-pc + basic_os=msys + ;; + mvs) + basic_machine=i370-ibm + basic_os=mvs + ;; + nacl) + basic_machine=le32-unknown + basic_os=nacl + ;; + ncr3000) + basic_machine=i486-ncr + basic_os=sysv4 + ;; + netbsd386) + basic_machine=i386-pc + basic_os=netbsd + ;; + netwinder) + basic_machine=armv4l-rebel + basic_os=linux + ;; + news | news700 | news800 | news900) + basic_machine=m68k-sony + basic_os=newsos + ;; + news1000) + basic_machine=m68030-sony + basic_os=newsos + ;; + necv70) + basic_machine=v70-nec + basic_os=sysv + ;; + nh3000) + basic_machine=m68k-harris + basic_os=cxux + ;; + nh[45]000) + basic_machine=m88k-harris + basic_os=cxux + ;; + nindy960) + basic_machine=i960-intel + basic_os=nindy + ;; + mon960) + basic_machine=i960-intel + basic_os=mon960 + ;; + nonstopux) + basic_machine=mips-compaq + basic_os=nonstopux + ;; + os400) + basic_machine=powerpc-ibm + basic_os=os400 + ;; + OSE68000 | ose68000) + basic_machine=m68000-ericsson + basic_os=ose + ;; + os68k) + basic_machine=m68k-none + basic_os=os68k + ;; + paragon) + basic_machine=i860-intel + basic_os=osf + ;; + parisc) + basic_machine=hppa-unknown + basic_os=linux + ;; + psp) + basic_machine=mipsallegrexel-sony + basic_os=psp + ;; + pw32) + basic_machine=i586-unknown + basic_os=pw32 + ;; + rdos | rdos64) + basic_machine=x86_64-pc + basic_os=rdos + ;; + rdos32) + basic_machine=i386-pc + basic_os=rdos + ;; + rom68k) + basic_machine=m68k-rom68k + basic_os=coff + ;; + sa29200) + basic_machine=a29k-amd + basic_os=udi + ;; + sei) + basic_machine=mips-sei + basic_os=seiux + ;; + sequent) + basic_machine=i386-sequent + basic_os= + ;; + sps7) + basic_machine=m68k-bull + basic_os=sysv2 + ;; + st2000) + basic_machine=m68k-tandem + basic_os= + ;; + stratus) + basic_machine=i860-stratus + basic_os=sysv4 + ;; + sun2) + basic_machine=m68000-sun + basic_os= + ;; + sun2os3) + basic_machine=m68000-sun + basic_os=sunos3 + ;; + sun2os4) + basic_machine=m68000-sun + basic_os=sunos4 + ;; + sun3) + basic_machine=m68k-sun + basic_os= + ;; + sun3os3) + basic_machine=m68k-sun + basic_os=sunos3 + ;; + sun3os4) + basic_machine=m68k-sun + basic_os=sunos4 + ;; + sun4) + basic_machine=sparc-sun + basic_os= + ;; + sun4os3) + basic_machine=sparc-sun + basic_os=sunos3 + ;; + sun4os4) + basic_machine=sparc-sun + basic_os=sunos4 + ;; + sun4sol2) + basic_machine=sparc-sun + basic_os=solaris2 + ;; + sun386 | sun386i | roadrunner) + basic_machine=i386-sun + basic_os= + ;; + sv1) + basic_machine=sv1-cray + basic_os=unicos + ;; + symmetry) + basic_machine=i386-sequent + basic_os=dynix + ;; + t3e) + basic_machine=alphaev5-cray + basic_os=unicos + ;; + t90) + basic_machine=t90-cray + basic_os=unicos + ;; + toad1) + basic_machine=pdp10-xkl + basic_os=tops20 + ;; + tpf) + basic_machine=s390x-ibm + basic_os=tpf + ;; + udi29k) + basic_machine=a29k-amd + basic_os=udi + ;; + ultra3) + basic_machine=a29k-nyu + basic_os=sym1 + ;; + v810 | necv810) + basic_machine=v810-nec + basic_os=none + ;; + vaxv) + basic_machine=vax-dec + basic_os=sysv + ;; + vms) + basic_machine=vax-dec + basic_os=vms + ;; + vsta) + basic_machine=i386-pc + basic_os=vsta + ;; + vxworks960) + basic_machine=i960-wrs + basic_os=vxworks + ;; + vxworks68) + basic_machine=m68k-wrs + basic_os=vxworks + ;; + vxworks29k) + basic_machine=a29k-wrs + basic_os=vxworks + ;; + xbox) + basic_machine=i686-pc + basic_os=mingw32 + ;; + ymp) + basic_machine=ymp-cray + basic_os=unicos + ;; + *) + basic_machine=$1 + basic_os= + ;; + esac + ;; +esac + +# Decode 1-component or ad-hoc basic machines +case $basic_machine in + # Here we handle the default manufacturer of certain CPU types. It is in + # some cases the only manufacturer, in others, it is the most popular. + w89k) + cpu=hppa1.1 + vendor=winbond + ;; + op50n) + cpu=hppa1.1 + vendor=oki + ;; + op60c) + cpu=hppa1.1 + vendor=oki + ;; + ibm*) + cpu=i370 + vendor=ibm + ;; + orion105) + cpu=clipper + vendor=highlevel + ;; + mac | mpw | mac-mpw) + cpu=m68k + vendor=apple + ;; + pmac | pmac-mpw) + cpu=powerpc + vendor=apple + ;; + + # Recognize the various machine names and aliases which stand + # for a CPU type and a company and sometimes even an OS. + 3b1 | 7300 | 7300-att | att-7300 | pc7300 | safari | unixpc) + cpu=m68000 + vendor=att + ;; + 3b*) + cpu=we32k + vendor=att + ;; + bluegene*) + cpu=powerpc + vendor=ibm + basic_os=cnk + ;; + decsystem10* | dec10*) + cpu=pdp10 + vendor=dec + basic_os=tops10 + ;; + decsystem20* | dec20*) + cpu=pdp10 + vendor=dec + basic_os=tops20 + ;; + delta | 3300 | delta-motorola | 3300-motorola | motorola-delta | motorola-3300) + cpu=m68k + vendor=motorola + ;; + # This used to be dpx2*, but that gets the RS6000-based + # DPX/20 and the x86-based DPX/2-100 wrong. See + # https://oldskool.silicium.org/stations/bull_dpx20.htm + # https://www.feb-patrimoine.com/english/bull_dpx2.htm + # https://www.feb-patrimoine.com/english/unix_and_bull.htm + dpx2 | dpx2[23]00 | dpx2[23]xx) + cpu=m68k + vendor=bull + ;; + dpx2100 | dpx21xx) + cpu=i386 + vendor=bull + ;; + dpx20) + cpu=rs6000 + vendor=bull + ;; + encore | umax | mmax) + cpu=ns32k + vendor=encore + ;; + elxsi) + cpu=elxsi + vendor=elxsi + basic_os=${basic_os:-bsd} + ;; + fx2800) + cpu=i860 + vendor=alliant + ;; + genix) + cpu=ns32k + vendor=ns + ;; + h3050r* | hiux*) + cpu=hppa1.1 + vendor=hitachi + basic_os=hiuxwe2 + ;; + hp3k9[0-9][0-9] | hp9[0-9][0-9]) + cpu=hppa1.0 + vendor=hp + ;; + hp9k2[0-9][0-9] | hp9k31[0-9]) + cpu=m68000 + vendor=hp + ;; + hp9k3[2-9][0-9]) + cpu=m68k + vendor=hp + ;; + hp9k6[0-9][0-9] | hp6[0-9][0-9]) + cpu=hppa1.0 + vendor=hp + ;; + hp9k7[0-79][0-9] | hp7[0-79][0-9]) + cpu=hppa1.1 + vendor=hp + ;; + hp9k78[0-9] | hp78[0-9]) + # FIXME: really hppa2.0-hp + cpu=hppa1.1 + vendor=hp + ;; + hp9k8[67]1 | hp8[67]1 | hp9k80[24] | hp80[24] | hp9k8[78]9 | hp8[78]9 | hp9k893 | hp893) + # FIXME: really hppa2.0-hp + cpu=hppa1.1 + vendor=hp + ;; + hp9k8[0-9][13679] | hp8[0-9][13679]) + cpu=hppa1.1 + vendor=hp + ;; + hp9k8[0-9][0-9] | hp8[0-9][0-9]) + cpu=hppa1.0 + vendor=hp + ;; + i*86v32) + cpu=`echo "$1" | sed -e 's/86.*/86/'` + vendor=pc + basic_os=sysv32 + ;; + i*86v4*) + cpu=`echo "$1" | sed -e 's/86.*/86/'` + vendor=pc + basic_os=sysv4 + ;; + i*86v) + cpu=`echo "$1" | sed -e 's/86.*/86/'` + vendor=pc + basic_os=sysv + ;; + i*86sol2) + cpu=`echo "$1" | sed -e 's/86.*/86/'` + vendor=pc + basic_os=solaris2 + ;; + j90 | j90-cray) + cpu=j90 + vendor=cray + basic_os=${basic_os:-unicos} + ;; + iris | iris4d) + cpu=mips + vendor=sgi + case $basic_os in + irix*) + ;; + *) + basic_os=irix4 + ;; + esac + ;; + miniframe) + cpu=m68000 + vendor=convergent + ;; + *mint | mint[0-9]* | *MiNT | *MiNT[0-9]*) + cpu=m68k + vendor=atari + basic_os=mint + ;; + news-3600 | risc-news) + cpu=mips + vendor=sony + basic_os=newsos + ;; + next | m*-next) + cpu=m68k + vendor=next + ;; + np1) + cpu=np1 + vendor=gould + ;; + op50n-* | op60c-*) + cpu=hppa1.1 + vendor=oki + basic_os=proelf + ;; + pa-hitachi) + cpu=hppa1.1 + vendor=hitachi + basic_os=hiuxwe2 + ;; + pbd) + cpu=sparc + vendor=tti + ;; + pbb) + cpu=m68k + vendor=tti + ;; + pc532) + cpu=ns32k + vendor=pc532 + ;; + pn) + cpu=pn + vendor=gould + ;; + power) + cpu=power + vendor=ibm + ;; + ps2) + cpu=i386 + vendor=ibm + ;; + rm[46]00) + cpu=mips + vendor=siemens + ;; + rtpc | rtpc-*) + cpu=romp + vendor=ibm + ;; + sde) + cpu=mipsisa32 + vendor=sde + basic_os=${basic_os:-elf} + ;; + simso-wrs) + cpu=sparclite + vendor=wrs + basic_os=vxworks + ;; + tower | tower-32) + cpu=m68k + vendor=ncr + ;; + vpp*|vx|vx-*) + cpu=f301 + vendor=fujitsu + ;; + w65) + cpu=w65 + vendor=wdc + ;; + w89k-*) + cpu=hppa1.1 + vendor=winbond + basic_os=proelf + ;; + none) + cpu=none + vendor=none + ;; + leon|leon[3-9]) + cpu=sparc + vendor=$basic_machine + ;; + leon-*|leon[3-9]-*) + cpu=sparc + vendor=`echo "$basic_machine" | sed 's/-.*//'` + ;; + + *-*) + saved_IFS=$IFS + IFS="-" read cpu vendor <&2 + exit 1 + ;; + esac + ;; +esac + +# Here we canonicalize certain aliases for manufacturers. +case $vendor in + digital*) + vendor=dec + ;; + commodore*) + vendor=cbm + ;; + *) + ;; +esac + +# Decode manufacturer-specific aliases for certain operating systems. + +if test x"$basic_os" != x +then + +# First recognize some ad-hoc cases, or perhaps split kernel-os, or else just +# set os. +obj= +case $basic_os in + gnu/linux*) + kernel=linux + os=`echo "$basic_os" | sed -e 's|gnu/linux|gnu|'` + ;; + os2-emx) + kernel=os2 + os=`echo "$basic_os" | sed -e 's|os2-emx|emx|'` + ;; + nto-qnx*) + kernel=nto + os=`echo "$basic_os" | sed -e 's|nto-qnx|qnx|'` + ;; + *-*) + saved_IFS=$IFS + IFS="-" read kernel os <&2 + fi + ;; + *) + echo "Invalid configuration '$1': OS '$os' not recognized" 1>&2 + exit 1 + ;; +esac + +case $obj in + aout* | coff* | elf* | pe*) + ;; + '') + # empty is fine + ;; + *) + echo "Invalid configuration '$1': Machine code format '$obj' not recognized" 1>&2 + exit 1 + ;; +esac + +# Here we handle the constraint that a (synthetic) cpu and os are +# valid only in combination with each other and nowhere else. +case $cpu-$os in + # The "javascript-unknown-ghcjs" triple is used by GHC; we + # accept it here in order to tolerate that, but reject any + # variations. + javascript-ghcjs) + ;; + javascript-* | *-ghcjs) + echo "Invalid configuration '$1': cpu '$cpu' is not valid with os '$os$obj'" 1>&2 + exit 1 + ;; +esac + +# As a final step for OS-related things, validate the OS-kernel combination +# (given a valid OS), if there is a kernel. +case $kernel-$os-$obj in + linux-gnu*- | linux-android*- | linux-dietlibc*- | linux-llvm*- \ + | linux-mlibc*- | linux-musl*- | linux-newlib*- \ + | linux-relibc*- | linux-uclibc*- | linux-ohos*- ) + ;; + uclinux-uclibc*- | uclinux-gnu*- ) + ;; + managarm-mlibc*- | managarm-kernel*- ) + ;; + windows*-msvc*-) + ;; + -dietlibc*- | -llvm*- | -mlibc*- | -musl*- | -newlib*- | -relibc*- \ + | -uclibc*- ) + # These are just libc implementations, not actual OSes, and thus + # require a kernel. + echo "Invalid configuration '$1': libc '$os' needs explicit kernel." 1>&2 + exit 1 + ;; + -kernel*- ) + echo "Invalid configuration '$1': '$os' needs explicit kernel." 1>&2 + exit 1 + ;; + *-kernel*- ) + echo "Invalid configuration '$1': '$kernel' does not support '$os'." 1>&2 + exit 1 + ;; + *-msvc*- ) + echo "Invalid configuration '$1': '$os' needs 'windows'." 1>&2 + exit 1 + ;; + kfreebsd*-gnu*- | knetbsd*-gnu*- | netbsd*-gnu*- | kopensolaris*-gnu*-) + ;; + vxworks-simlinux- | vxworks-simwindows- | vxworks-spe-) + ;; + nto-qnx*-) + ;; + os2-emx-) + ;; + rtmk-nova-) + ;; + *-eabi*- | *-gnueabi*-) + ;; + none--*) + # None (no kernel, i.e. freestanding / bare metal), + # can be paired with an machine code file format + ;; + -*-) + # Blank kernel with real OS is always fine. + ;; + --*) + # Blank kernel and OS with real machine code file format is always fine. + ;; + *-*-*) + echo "Invalid configuration '$1': Kernel '$kernel' not known to work with OS '$os'." 1>&2 + exit 1 + ;; +esac + +# Here we handle the case where we know the os, and the CPU type, but not the +# manufacturer. We pick the logical manufacturer. +case $vendor in + unknown) + case $cpu-$os in + *-riscix*) + vendor=acorn + ;; + *-sunos* | *-solaris*) + vendor=sun + ;; + *-cnk* | *-aix*) + vendor=ibm + ;; + *-beos*) + vendor=be + ;; + *-hpux*) + vendor=hp + ;; + *-mpeix*) + vendor=hp + ;; + *-hiux*) + vendor=hitachi + ;; + *-unos*) + vendor=crds + ;; + *-dgux*) + vendor=dg + ;; + *-luna*) + vendor=omron + ;; + *-genix*) + vendor=ns + ;; + *-clix*) + vendor=intergraph + ;; + *-mvs* | *-opened*) + vendor=ibm + ;; + *-os400*) + vendor=ibm + ;; + s390-* | s390x-*) + vendor=ibm + ;; + *-ptx*) + vendor=sequent + ;; + *-tpf*) + vendor=ibm + ;; + *-vxsim* | *-vxworks* | *-windiss*) + vendor=wrs + ;; + *-aux*) + vendor=apple + ;; + *-hms*) + vendor=hitachi + ;; + *-mpw* | *-macos*) + vendor=apple + ;; + *-*mint | *-mint[0-9]* | *-*MiNT | *-MiNT[0-9]*) + vendor=atari + ;; + *-vos*) + vendor=stratus + ;; + esac + ;; +esac + +echo "$cpu-$vendor${kernel:+-$kernel}${os:+-$os}${obj:+-$obj}" +exit + +# Local variables: +# eval: (add-hook 'before-save-hook 'time-stamp) +# time-stamp-start: "timestamp='" +# time-stamp-format: "%:y-%02m-%02d" +# time-stamp-end: "'" +# End: diff --git a/autosetup/autosetup-find-tclsh b/autosetup/autosetup-find-tclsh new file mode 100755 index 0000000000..9f6d6e9402 --- /dev/null +++ b/autosetup/autosetup-find-tclsh @@ -0,0 +1,16 @@ +#!/bin/sh +# Looks for a suitable tclsh or jimsh in the PATH +# If not found, builds a bootstrap jimsh in current dir from source +# Prefer $autosetup_tclsh if is set in the environment (unless ./jimsh0 works) +# If an argument is given, use that as the test instead of autosetup-test-tclsh +d="`dirname "$0"`" +for tclsh in ./jimsh0 $autosetup_tclsh jimsh tclsh tclsh8.5 tclsh8.6 tclsh8.7; do + { $tclsh "$d/${1-autosetup-test-tclsh}"; } 2>/dev/null && exit 0 +done +echo 1>&2 "No installed jimsh or tclsh, building local bootstrap jimsh0" +for cc in ${CC_FOR_BUILD:-cc} gcc; do + { $cc -o jimsh0 "$d/jimsh0.c"; } 2>/dev/null >/dev/null || continue + ./jimsh0 "$d/${1-autosetup-test-tclsh}" && exit 0 +done +echo 1>&2 "No working C compiler found. Tried ${CC_FOR_BUILD:-cc} and gcc." +echo false diff --git a/autosetup/autosetup-test-tclsh b/autosetup/autosetup-test-tclsh new file mode 100644 index 0000000000..75126d2444 --- /dev/null +++ b/autosetup/autosetup-test-tclsh @@ -0,0 +1,20 @@ +# A small Tcl script to verify that the chosen +# interpreter works. Sometimes we might e.g. pick up +# an interpreter for a different arch. +# Outputs the full path to the interpreter + +if {[catch {info version} version] == 0} { + # This is Jim Tcl + if {$version >= 0.72} { + # Ensure that regexp works + regexp (a.*?) a + puts [info nameofexecutable] + exit 0 + } +} elseif {[catch {info tclversion} version] == 0} { + if {$version >= 8.5 && ![string match 8.5a* [info patchlevel]]} { + puts [info nameofexecutable] + exit 0 + } +} +exit 1 diff --git a/autosetup/cc-db.tcl b/autosetup/cc-db.tcl new file mode 100644 index 0000000000..12f1aed2c9 --- /dev/null +++ b/autosetup/cc-db.tcl @@ -0,0 +1,15 @@ +# Copyright (c) 2011 WorkWare Systems http://www.workware.net.au/ +# All rights reserved + +# @synopsis: +# +# The 'cc-db' module provides a knowledge-base of system idiosyncrasies. +# In general, this module can always be included. + +use cc + +options {} + +# openbsd needs sys/types.h to detect some system headers +cc-include-needs sys/socket.h sys/types.h +cc-include-needs netinet/in.h sys/types.h diff --git a/autosetup/cc-lib.tcl b/autosetup/cc-lib.tcl new file mode 100644 index 0000000000..01a0fb3877 --- /dev/null +++ b/autosetup/cc-lib.tcl @@ -0,0 +1,187 @@ +# Copyright (c) 2011 WorkWare Systems http://www.workware.net.au/ +# All rights reserved + +# @synopsis: +# +# Provides a library of common tests on top of the 'cc' module. + +use cc + +# @cc-check-lfs +# +# The equivalent of the 'AC_SYS_LARGEFILE' macro. +# +# defines 'HAVE_LFS' if LFS is available, +# and defines '_FILE_OFFSET_BITS=64' if necessary +# +# Returns 1 if 'LFS' is available or 0 otherwise +# +proc cc-check-lfs {} { + cc-check-includes sys/types.h + msg-checking "Checking if -D_FILE_OFFSET_BITS=64 is needed..." + set lfs 1 + if {[msg-quiet cc-with {-includes sys/types.h} {cc-check-sizeof off_t}] == 8} { + msg-result no + } elseif {[msg-quiet cc-with {-includes sys/types.h -cflags -D_FILE_OFFSET_BITS=64} {cc-check-sizeof off_t}] == 8} { + define _FILE_OFFSET_BITS 64 + msg-result yes + } else { + set lfs 0 + msg-result none + } + define-feature lfs $lfs + return $lfs +} + +# @cc-check-endian +# +# The equivalent of the 'AC_C_BIGENDIAN' macro. +# +# defines 'HAVE_BIG_ENDIAN' if endian is known to be big, +# or 'HAVE_LITTLE_ENDIAN' if endian is known to be little. +# +# Returns 1 if determined, or 0 if not. +# +proc cc-check-endian {} { + cc-check-includes sys/types.h sys/param.h + set rc 0 + msg-checking "Checking endian..." + cc-with {-includes {sys/types.h sys/param.h}} { + if {[cctest -code { + #if !defined(BIG_ENDIAN) || !defined(BYTE_ORDER) + #error unknown + #elif BYTE_ORDER != BIG_ENDIAN + #error little + #endif + }]} { + define-feature big-endian + msg-result "big" + set rc 1 + } elseif {[cctest -code { + #if !defined(LITTLE_ENDIAN) || !defined(BYTE_ORDER) + #error unknown + #elif BYTE_ORDER != LITTLE_ENDIAN + #error big + #endif + }]} { + define-feature little-endian + msg-result "little" + set rc 1 + } else { + msg-result "unknown" + } + } + return $rc +} + +# @cc-check-flags flag ?...? +# +# Checks whether the given C/C++ compiler flags can be used. Defines feature +# names prefixed with 'HAVE_CFLAG' and 'HAVE_CXXFLAG' respectively, and +# appends working flags to '-cflags' and 'AS_CFLAGS' or 'AS_CXXFLAGS'. +proc cc-check-flags {args} { + set result 1 + array set opts [cc-get-settings] + switch -exact -- $opts(-lang) { + c++ { + set lang C++ + set prefix CXXFLAG + } + c { + set lang C + set prefix CFLAG + } + default { + autosetup-error "cc-check-flags failed with unknown language: $opts(-lang)" + } + } + foreach flag $args { + msg-checking "Checking whether the $lang compiler accepts $flag..." + if {[cctest -cflags $flag]} { + msg-result yes + define-feature $prefix$flag + cc-with [list -cflags [list $flag]] + define-append AS_${prefix}S $flag + } else { + msg-result no + set result 0 + } + } + return $result +} + +# @cc-check-standards ver ?...? +# +# Checks whether the C/C++ compiler accepts one of the specified '-std=$ver' +# options, and appends the first working one to '-cflags' and 'AS_CFLAGS' or +# 'AS_CXXFLAGS'. +proc cc-check-standards {args} { + array set opts [cc-get-settings] + foreach std $args { + if {[cc-check-flags -std=$std]} { + return $std + } + } + return "" +} + +# Checks whether $keyword is usable as alignof +proc cctest_alignof {keyword} { + msg-checking "Checking for $keyword..." + if {[cctest -code "int x = ${keyword}(char), y = ${keyword}('x');"]} then { + msg-result ok + define-feature $keyword + } else { + msg-result "not found" + } +} + +# @cc-check-c11 +# +# Checks for several C11/C++11 extensions and their alternatives. Currently +# checks for '_Static_assert', '_Alignof', '__alignof__', '__alignof'. +proc cc-check-c11 {} { + msg-checking "Checking for _Static_assert..." + if {[cctest -code { + _Static_assert(1, "static assertions are available"); + }]} then { + msg-result ok + define-feature _Static_assert + } else { + msg-result "not found" + } + + cctest_alignof _Alignof + cctest_alignof __alignof__ + cctest_alignof __alignof +} + +# @cc-check-alloca +# +# The equivalent of the 'AC_FUNC_ALLOCA' macro. +# +# Checks for the existence of 'alloca' +# defines 'HAVE_ALLOCA' and returns 1 if it exists. +proc cc-check-alloca {} { + cc-check-some-feature alloca { + cctest -includes alloca.h -code { alloca (2 * sizeof (int)); } + } +} + +# @cc-signal-return-type +# +# The equivalent of the 'AC_TYPE_SIGNAL' macro. +# +# defines 'RETSIGTYPE' to 'int' or 'void'. +proc cc-signal-return-type {} { + msg-checking "Checking return type of signal handlers..." + cc-with {-includes {sys/types.h signal.h}} { + if {[cctest -code {return *(signal (0, 0)) (0) == 1;}]} { + set type int + } else { + set type void + } + define RETSIGTYPE $type + msg-result $type + } +} diff --git a/autosetup/cc-shared.tcl b/autosetup/cc-shared.tcl new file mode 100644 index 0000000000..1fa200eec1 --- /dev/null +++ b/autosetup/cc-shared.tcl @@ -0,0 +1,115 @@ +# Copyright (c) 2010 WorkWare Systems http://www.workware.net.au/ +# All rights reserved + +# @synopsis: +# +# The 'cc-shared' module provides support for shared libraries and shared objects. +# It defines the following variables: +# +## SH_CFLAGS Flags to use compiling sources destined for a shared library +## SH_LDFLAGS Flags to use linking (creating) a shared library +## SH_SOPREFIX Prefix to use to set the soname when creating a shared library +## SH_SOFULLPATH Set to 1 if the shared library soname should include the full install path +## SH_SOEXT Extension for shared libs +## SH_SOEXTVER Format for versioned shared libs - %s = version +## SHOBJ_CFLAGS Flags to use compiling sources destined for a shared object +## SHOBJ_LDFLAGS Flags to use linking a shared object, undefined symbols allowed +## SHOBJ_LDFLAGS_R - as above, but all symbols must be resolved +## SH_LINKRPATH Format for setting the rpath when linking an executable, %s = path +## SH_LINKFLAGS Flags to use linking an executable which will load shared objects +## LD_LIBRARY_PATH Environment variable which specifies path to shared libraries +## STRIPLIBFLAGS Arguments to strip a dynamic library + +options {} + +# Defaults: gcc on unix +define SHOBJ_CFLAGS -fPIC +define SHOBJ_LDFLAGS -shared +define SH_CFLAGS -fPIC +define SH_LDFLAGS -shared +define SH_LINKFLAGS -rdynamic +define SH_LINKRPATH "-Wl,-rpath -Wl,%s" +define SH_SOEXT .so +define SH_SOEXTVER .so.%s +define SH_SOPREFIX -Wl,-soname, +define LD_LIBRARY_PATH LD_LIBRARY_PATH +define STRIPLIBFLAGS --strip-unneeded + +# Note: This is a helpful reference for identifying the toolchain +# http://sourceforge.net/apps/mediawiki/predef/index.php?title=Compilers + +switch -glob -- [get-define host] { + *-*-darwin* { + define SHOBJ_CFLAGS "-dynamic -fno-common" + define SHOBJ_LDFLAGS "-bundle -undefined dynamic_lookup" + define SHOBJ_LDFLAGS_R -bundle + define SH_CFLAGS -dynamic + define SH_LDFLAGS -dynamiclib + define SH_LINKFLAGS "" + define SH_SOEXT .dylib + define SH_SOEXTVER .%s.dylib + define SH_SOPREFIX -Wl,-install_name, + define SH_SOFULLPATH + define LD_LIBRARY_PATH DYLD_LIBRARY_PATH + define STRIPLIBFLAGS -x + } + *-*-ming* - *-*-cygwin - *-*-msys { + define SHOBJ_CFLAGS "" + define SHOBJ_LDFLAGS -shared + define SH_CFLAGS "" + define SH_LDFLAGS -shared + define SH_LINKRPATH "" + define SH_LINKFLAGS "" + define SH_SOEXT .dll + define SH_SOEXTVER .dll + define SH_SOPREFIX "" + define LD_LIBRARY_PATH PATH + } + sparc* { + if {[msg-quiet cc-check-decls __SUNPRO_C]} { + msg-result "Found sun stdio compiler" + # sun stdio compiler + # XXX: These haven't been fully tested. + define SHOBJ_CFLAGS -KPIC + define SHOBJ_LDFLAGS "-G" + define SH_CFLAGS -KPIC + define SH_LINKFLAGS -Wl,-export-dynamic + define SH_SOPREFIX -Wl,-h, + } + } + *-*-solaris* { + if {[msg-quiet cc-check-decls __SUNPRO_C]} { + msg-result "Found sun stdio compiler" + # sun stdio compiler + # XXX: These haven't been fully tested. + define SHOBJ_CFLAGS -KPIC + define SHOBJ_LDFLAGS "-G" + define SH_CFLAGS -KPIC + define SH_LINKFLAGS -Wl,-export-dynamic + define SH_SOPREFIX -Wl,-h, + } + } + *-*-hpux* { + define SHOBJ_CFLAGS +z + define SHOBJ_LDFLAGS -b + define SH_CFLAGS +z + define SH_LDFLAGS -b + define SH_LINKFLAGS -Wl,+s + define SH_LINKRPATH "-Wl,+b -Wl,%s" + define SH_SOPREFIX -Wl,+h, + define STRIPLIBFLAGS -Wl,-s + } + *-*-haiku { + define SHOBJ_CFLAGS "" + define SHOBJ_LDFLAGS -shared + define SH_CFLAGS "" + define SH_LDFLAGS -shared + define SH_LINKFLAGS "" + define SH_SOPREFIX "" + define LD_LIBRARY_PATH LIBRARY_PATH + } +} + +if {![is-defined SHOBJ_LDFLAGS_R]} { + define SHOBJ_LDFLAGS_R [get-define SHOBJ_LDFLAGS] +} diff --git a/autosetup/cc.tcl b/autosetup/cc.tcl new file mode 100644 index 0000000000..05c1b1cf40 --- /dev/null +++ b/autosetup/cc.tcl @@ -0,0 +1,758 @@ +# Copyright (c) 2010 WorkWare Systems http://www.workware.net.au/ +# All rights reserved + +# @synopsis: +# +# The 'cc' module supports checking various 'features' of the C or C++ +# compiler/linker environment. Common commands are 'cc-check-includes', +# 'cc-check-types', 'cc-check-functions', 'cc-with' and 'make-config-header' +# +# The following environment variables are used if set: +# +## CC - C compiler +## CXX - C++ compiler +## CPP - C preprocessor +## CCACHE - Set to "none" to disable automatic use of ccache +## CPPFLAGS - Additional C preprocessor compiler flags (C and C++), before CFLAGS, CXXFLAGS +## CFLAGS - Additional C compiler flags +## CXXFLAGS - Additional C++ compiler flags +## LDFLAGS - Additional compiler flags during linking +## LINKFLAGS - ?How is this different from LDFLAGS? +## LIBS - Additional libraries to use (for all tests) +## CROSS - Tool prefix for cross compilation +# +# The following variables are defined from the corresponding +# environment variables if set. +# +## CC_FOR_BUILD +## LD + +use system + +options {} + +# Checks for the existence of the given function by linking +# +proc cctest_function {function} { + cctest -link 1 -declare "extern void $function\(void);" -code "$function\();" +} + +# Checks for the existence of the given type by compiling +proc cctest_type {type} { + cctest -code "$type _x;" +} + +# Checks for the existence of the given type/structure member. +# e.g. "struct stat.st_mtime" +proc cctest_member {struct_member} { + # split at the first dot + regexp {^([^.]+)[.](.*)$} $struct_member -> struct member + cctest -code "static $struct _s; return sizeof(_s.$member);" +} + +# Checks for the existence of the given define by compiling +# +proc cctest_define {name} { + cctest -code "#ifndef $name\n#error not defined\n#endif" +} + +# Checks for the existence of the given name either as +# a macro (#define) or an rvalue (such as an enum) +# +proc cctest_decl {name} { + cctest -code "#ifndef $name\n(void)$name;\n#endif" +} + +# @cc-check-sizeof type ... +# +# Checks the size of the given types (between 1 and 32, inclusive). +# Defines a variable with the size determined, or 'unknown' otherwise. +# e.g. for type 'long long', defines 'SIZEOF_LONG_LONG'. +# Returns the size of the last type. +# +proc cc-check-sizeof {args} { + foreach type $args { + msg-checking "Checking for sizeof $type..." + set size unknown + # Try the most common sizes first + foreach i {4 8 1 2 16 32} { + if {[cctest -code "static int _x\[sizeof($type) == $i ? 1 : -1\] = { 1 };"]} { + set size $i + break + } + } + msg-result $size + set define [feature-define-name $type SIZEOF_] + define $define $size + } + # Return the last result + get-define $define +} + +# Checks for each feature in $list by using the given script. +# +# When the script is evaluated, $each is set to the feature +# being checked, and $extra is set to any additional cctest args. +# +# Returns 1 if all features were found, or 0 otherwise. +proc cc-check-some-feature {list script} { + set ret 1 + foreach each $list { + if {![check-feature $each $script]} { + set ret 0 + } + } + return $ret +} + +# @cc-check-includes includes ... +# +# Checks that the given include files can be used. +proc cc-check-includes {args} { + cc-check-some-feature $args { + set with {} + if {[dict exists $::autosetup(cc-include-deps) $each]} { + set deps [dict keys [dict get $::autosetup(cc-include-deps) $each]] + msg-quiet cc-check-includes {*}$deps + foreach i $deps { + if {[have-feature $i]} { + lappend with $i + } + } + } + if {[llength $with]} { + cc-with [list -includes $with] { + cctest -includes $each + } + } else { + cctest -includes $each + } + } +} + +# @cc-include-needs include required ... +# +# Ensures that when checking for '$include', a check is first +# made for each '$required' file, and if found, it is included with '#include'. +proc cc-include-needs {file args} { + foreach depfile $args { + dict set ::autosetup(cc-include-deps) $file $depfile 1 + } +} + +# @cc-check-types type ... +# +# Checks that the types exist. +proc cc-check-types {args} { + cc-check-some-feature $args { + cctest_type $each + } +} + +# @cc-check-defines define ... +# +# Checks that the given preprocessor symbols are defined. +proc cc-check-defines {args} { + cc-check-some-feature $args { + cctest_define $each + } +} + +# @cc-check-decls name ... +# +# Checks that each given name is either a preprocessor symbol or rvalue +# such as an enum. Note that the define used is 'HAVE_DECL_xxx' +# rather than 'HAVE_xxx'. +proc cc-check-decls {args} { + set ret 1 + foreach name $args { + msg-checking "Checking for $name..." + set r [cctest_decl $name] + define-feature "decl $name" $r + if {$r} { + msg-result "ok" + } else { + msg-result "not found" + set ret 0 + } + } + return $ret +} + +# @cc-check-functions function ... +# +# Checks that the given functions exist (can be linked). +proc cc-check-functions {args} { + cc-check-some-feature $args { + cctest_function $each + } +} + +# @cc-check-members type.member ... +# +# Checks that the given type/structure members exist. +# A structure member is of the form 'struct stat.st_mtime'. +proc cc-check-members {args} { + cc-check-some-feature $args { + cctest_member $each + } +} + +# @cc-check-function-in-lib function libs ?otherlibs? +# +# Checks that the given function can be found in one of the libs. +# +# First checks for no library required, then checks each of the libraries +# in turn. +# +# If the function is found, the feature is defined and 'lib_$function' is defined +# to '-l$lib' where the function was found, or "" if no library required. +# In addition, '-l$lib' is prepended to the 'LIBS' define. +# +# If additional libraries may be needed for linking, they should be specified +# with '$extralibs' as '-lotherlib1 -lotherlib2'. +# These libraries are not automatically added to 'LIBS'. +# +# Returns 1 if found or 0 if not. +# +proc cc-check-function-in-lib {function libs {otherlibs {}}} { + msg-checking "Checking libs for $function..." + set found 0 + cc-with [list -libs $otherlibs] { + if {[cctest_function $function]} { + msg-result "none needed" + define lib_$function "" + incr found + } else { + foreach lib $libs { + cc-with [list -libs -l$lib] { + if {[cctest_function $function]} { + msg-result -l$lib + define lib_$function -l$lib + # prepend to LIBS + define LIBS "-l$lib [get-define LIBS]" + incr found + break + } + } + } + } + } + define-feature $function $found + if {!$found} { + msg-result "no" + } + return $found +} + +# @cc-check-tools tool ... +# +# Checks for existence of the given compiler tools, taking +# into account any cross compilation prefix. +# +# For example, when checking for 'ar', first 'AR' is checked on the command +# line and then in the environment. If not found, '${host}-ar' or +# simply 'ar' is assumed depending upon whether cross compiling. +# The path is searched for this executable, and if found 'AR' is defined +# to the executable name. +# Note that even when cross compiling, the simple 'ar' is used as a fallback, +# but a warning is generated. This is necessary for some toolchains. +# +# It is an error if the executable is not found. +# +proc cc-check-tools {args} { + foreach tool $args { + set TOOL [string toupper $tool] + set exe [get-env $TOOL [get-define cross]$tool] + if {[find-executable $exe]} { + define $TOOL $exe + continue + } + if {[find-executable $tool]} { + msg-result "Warning: Failed to find $exe, falling back to $tool which may be incorrect" + define $TOOL $tool + continue + } + user-error "Failed to find $exe" + } +} + +# @cc-check-progs prog ... +# +# Checks for existence of the given executables on the path. +# +# For example, when checking for 'grep', the path is searched for +# the executable, 'grep', and if found 'GREP' is defined as 'grep'. +# +# If the executable is not found, the variable is defined as 'false'. +# Returns 1 if all programs were found, or 0 otherwise. +# +proc cc-check-progs {args} { + set failed 0 + foreach prog $args { + set PROG [string toupper $prog] + msg-checking "Checking for $prog..." + if {![find-executable $prog]} { + msg-result no + define $PROG false + incr failed + } else { + msg-result ok + define $PROG $prog + } + } + expr {!$failed} +} + +# @cc-path-progs prog ... +# +# Like cc-check-progs, but sets the define to the full path rather +# than just the program name. +# +proc cc-path-progs {args} { + set failed 0 + foreach prog $args { + set PROG [string toupper $prog] + msg-checking "Checking for $prog..." + set path [find-executable-path $prog] + if {$path eq ""} { + msg-result no + define $PROG false + incr failed + } else { + msg-result $path + define $PROG $path + } + } + expr {!$failed} +} + +# Adds the given settings to $::autosetup(ccsettings) and +# returns the old settings. +# +proc cc-add-settings {settings} { + if {[llength $settings] % 2} { + autosetup-error "settings list is missing a value: $settings" + } + + set prev [cc-get-settings] + # workaround a bug in some versions of jimsh by forcing + # conversion of $prev to a list + llength $prev + + array set new $prev + + foreach {name value} $settings { + switch -exact -- $name { + -cflags - -includes { + # These are given as lists + lappend new($name) {*}[list-non-empty $value] + } + -declare { + lappend new($name) $value + } + -libs { + # Note that new libraries are added before previous libraries + set new($name) [list {*}[list-non-empty $value] {*}$new($name)] + } + -link - -lang - -nooutput { + set new($name) $value + } + -source - -sourcefile - -code { + # XXX: These probably are only valid directly from cctest + set new($name) $value + } + default { + autosetup-error "unknown cctest setting: $name" + } + } + } + + cc-store-settings [array get new] + + return $prev +} + +proc cc-store-settings {new} { + set ::autosetup(ccsettings) $new +} + +proc cc-get-settings {} { + return $::autosetup(ccsettings) +} + +# Similar to cc-add-settings, but each given setting +# simply replaces the existing value. +# +# Returns the previous settings +proc cc-update-settings {args} { + set prev [cc-get-settings] + cc-store-settings [dict merge $prev $args] + return $prev +} + +# @cc-with settings ?{ script }? +# +# Sets the given 'cctest' settings and then runs the tests in '$script'. +# Note that settings such as '-lang' replace the current setting, while +# those such as '-includes' are appended to the existing setting. +# +# If no script is given, the settings become the default for the remainder +# of the 'auto.def' file. +# +## cc-with {-lang c++} { +## # This will check with the C++ compiler +## cc-check-types bool +## cc-with {-includes signal.h} { +## # This will check with the C++ compiler, signal.h and any existing includes. +## ... +## } +## # back to just the C++ compiler +## } +# +# The '-libs' setting is special in that newer values are added *before* earlier ones. +# +## cc-with {-libs {-lc -lm}} { +## cc-with {-libs -ldl} { +## cctest -libs -lsocket ... +## # libs will be in this order: -lsocket -ldl -lc -lm +## } +## } +# +# If you wish to invoke something like cc-check-flags but not have -cflags updated, +# use the following idiom: +# +## cc-with {} { +## cc-check-flags ... +## } +proc cc-with {settings args} { + if {[llength $args] == 0} { + cc-add-settings $settings + } elseif {[llength $args] > 1} { + autosetup-error "usage: cc-with settings ?script?" + } else { + set save [cc-add-settings $settings] + set rc [catch {uplevel 1 [lindex $args 0]} result info] + cc-store-settings $save + if {$rc != 0} { + return -code [dict get $info -code] $result + } + return $result + } +} + +# @cctest ?settings? +# +# Low level C/C++ compiler checker. Compiles and or links a small C program +# according to the arguments and returns 1 if OK, or 0 if not. +# +# Supported settings are: +# +## -cflags cflags A list of flags to pass to the compiler +## -includes list A list of includes, e.g. {stdlib.h stdio.h} +## -declare code Code to declare before main() +## -link 1 Don't just compile, link too +## -lang c|c++ Use the C (default) or C++ compiler +## -libs liblist List of libraries to link, e.g. {-ldl -lm} +## -code code Code to compile in the body of main() +## -source code Compile a complete program. Ignore -includes, -declare and -code +## -sourcefile file Shorthand for -source [readfile [get-define srcdir]/$file] +## -nooutput 1 Treat any compiler output (e.g. a warning) as an error +# +# Unless '-source' or '-sourcefile' is specified, the C program looks like: +# +## #include /* same for remaining includes in the list */ +## declare-code /* any code in -declare, verbatim */ +## int main(void) { +## code /* any code in -code, verbatim */ +## return 0; +## } +# +# And the command line looks like: +# +## CC -cflags CFLAGS CPPFLAGS conftest.c -o conftest.o +## CXX -cflags CXXFLAGS CPPFLAGS conftest.cpp -o conftest.o +# +# And if linking: +# +## CC LDFLAGS -cflags CFLAGS conftest.c -o conftest -libs LIBS +## CXX LDFLAGS -cflags CXXFLAGS conftest.c -o conftest -libs LIBS +# +# Any failures are recorded in 'config.log' +# +proc cctest {args} { + set tmp conftest__ + + # Easiest way to merge in the settings + cc-with $args { + array set opts [cc-get-settings] + } + + if {[info exists opts(-sourcefile)]} { + set opts(-source) [readfile [get-define srcdir]/$opts(-sourcefile) "#error can't find $opts(-sourcefile)"] + } + if {[info exists opts(-source)]} { + set lines $opts(-source) + } else { + foreach i $opts(-includes) { + if {$opts(-code) ne "" && ![feature-checked $i]} { + # Compiling real code with an unchecked header file + # Quickly (and silently) check for it now + + # Remove all -includes from settings before checking + set saveopts [cc-update-settings -includes {}] + msg-quiet cc-check-includes $i + cc-store-settings $saveopts + } + if {$opts(-code) eq "" || [have-feature $i]} { + lappend source "#include <$i>" + } + } + lappend source {*}$opts(-declare) + lappend source "int main(void) {" + lappend source $opts(-code) + lappend source "return 0;" + lappend source "}" + + set lines [join $source \n] + } + + # Build the command line + set cmdline {} + lappend cmdline {*}[get-define CCACHE] + switch -exact -- $opts(-lang) { + c++ { + set src conftest__.cpp + lappend cmdline {*}[get-define CXX] + set cflags [get-define CXXFLAGS] + } + c { + set src conftest__.c + lappend cmdline {*}[get-define CC] + set cflags [get-define CFLAGS] + } + default { + autosetup-error "cctest called with unknown language: $opts(-lang)" + } + } + + if {$opts(-link)} { + lappend cmdline {*}[get-define LDFLAGS] + } else { + lappend cflags {*}[get-define CPPFLAGS] + set tmp conftest__.o + lappend cmdline -c + } + lappend cmdline {*}$opts(-cflags) {*}[get-define cc-default-debug ""] {*}$cflags + lappend cmdline $src -o $tmp + if {$opts(-link)} { + lappend cmdline {*}$opts(-libs) {*}[get-define LIBS] + } + + # At this point we have the complete command line and the + # complete source to be compiled. Get the result from cache if + # we can + if {[info exists ::cc_cache($cmdline,$lines)]} { + msg-checking "(cached) " + set ok $::cc_cache($cmdline,$lines) + if {$::autosetup(debug)} { + configlog "From cache (ok=$ok): [join $cmdline]" + configlog "============" + configlog $lines + configlog "============" + } + return $ok + } + + writefile $src $lines\n + + set ok 1 + set err [catch {exec-with-stderr {*}$cmdline} result errinfo] + if {$err || ($opts(-nooutput) && [string length $result])} { + configlog "Failed: [join $cmdline]" + configlog $result + configlog "============" + configlog "The failed code was:" + configlog $lines + configlog "============" + set ok 0 + } elseif {$::autosetup(debug)} { + configlog "Compiled OK: [join $cmdline]" + configlog "============" + configlog $lines + configlog "============" + } + file delete $src + file delete $tmp + + # cache it + set ::cc_cache($cmdline,$lines) $ok + + return $ok +} + +# @make-autoconf-h outfile ?auto-patterns=HAVE_*? ?bare-patterns=SIZEOF_*? +# +# Deprecated - see 'make-config-header' +proc make-autoconf-h {file {autopatterns {HAVE_*}} {barepatterns {SIZEOF_* HAVE_DECL_*}}} { + user-notice "*** make-autoconf-h is deprecated -- use make-config-header instead" + make-config-header $file -auto $autopatterns -bare $barepatterns +} + +# @make-config-header outfile ?-auto patternlist? ?-bare patternlist? ?-none patternlist? ?-str patternlist? ... +# +# Examines all defined variables which match the given patterns +# and writes an include file, '$file', which defines each of these. +# Variables which match '-auto' are output as follows: +# - defines which have the value '0' are ignored. +# - defines which have integer values are defined as the integer value. +# - any other value is defined as a string, e.g. '"value"' +# Variables which match '-bare' are defined as-is. +# Variables which match '-str' are defined as a string, e.g. '"value"' +# Variables which match '-none' are omitted. +# +# Note that order is important. The first pattern that matches is selected. +# Default behaviour is: +# +## -bare {SIZEOF_* HAVE_DECL_*} -auto HAVE_* -none * +# +# If the file would be unchanged, it is not written. +proc make-config-header {file args} { + set guard _[string toupper [regsub -all {[^a-zA-Z0-9]} [file tail $file] _]] + file mkdir [file dirname $file] + set lines {} + lappend lines "#ifndef $guard" + lappend lines "#define $guard" + + # Add some defaults + lappend args -bare {SIZEOF_* HAVE_DECL_*} -auto HAVE_* + + foreach n [lsort [dict keys [all-defines]]] { + set value [get-define $n] + set type [calc-define-output-type $n $args] + switch -exact -- $type { + -bare { + # Just output the value unchanged + } + -none { + continue + } + -str { + set value \"[string map [list \\ \\\\ \" \\\"] $value]\" + } + -auto { + # Automatically determine the type + if {$value eq "0"} { + lappend lines "/* #undef $n */" + continue + } + if {![string is integer -strict $value]} { + set value \"[string map [list \\ \\\\ \" \\\"] $value]\" + } + } + "" { + continue + } + default { + autosetup-error "Unknown type in make-config-header: $type" + } + } + lappend lines "#define $n $value" + } + lappend lines "#endif" + set buf [join $lines \n] + write-if-changed $file $buf { + msg-result "Created $file" + } +} + +proc calc-define-output-type {name spec} { + foreach {type patterns} $spec { + foreach pattern $patterns { + if {[string match $pattern $name]} { + return $type + } + } + } + return "" +} + +proc cc-init {} { + global autosetup + + # Initialise some values from the environment or commandline or default settings + foreach i {LDFLAGS LIBS CPPFLAGS LINKFLAGS CFLAGS} { + lassign $i var default + define $var [get-env $var $default] + } + + if {[env-is-set CC]} { + # Set by the user, so don't try anything else + set try [list [get-env CC ""]] + } else { + # Try some reasonable options + set try [list [get-define cross]cc [get-define cross]gcc] + } + define CC [find-an-executable {*}$try] + if {[get-define CC] eq ""} { + user-error "Could not find a C compiler. Tried: [join $try ", "]" + } + + define CPP [get-env CPP "[get-define CC] -E"] + + # XXX: Could avoid looking for a C++ compiler until requested + # If CXX isn't found, it is set to the empty string. + if {[env-is-set CXX]} { + define CXX [find-an-executable -required [get-env CXX ""]] + } else { + define CXX [find-an-executable [get-define cross]c++ [get-define cross]g++] + } + + # CXXFLAGS default to CFLAGS if not specified + define CXXFLAGS [get-env CXXFLAGS [get-define CFLAGS]] + + # May need a CC_FOR_BUILD, so look for one + define CC_FOR_BUILD [find-an-executable [get-env CC_FOR_BUILD ""] cc gcc false] + + # These start empty and never come from the user or environment + define AS_CFLAGS "" + define AS_CPPFLAGS "" + define AS_CXXFLAGS "" + + define CCACHE [find-an-executable [get-env CCACHE ccache]] + + # If any of these are set in the environment, propagate them to the AUTOREMAKE commandline + foreach i {CC CXX CCACHE CPP CFLAGS CXXFLAGS CXXFLAGS LDFLAGS LIBS CROSS CPPFLAGS LINKFLAGS CC_FOR_BUILD LD} { + if {[env-is-set $i]} { + # Note: If the variable is set on the command line, get-env will return that value + # so the command line will continue to override the environment + define-append-argv AUTOREMAKE $i=[get-env $i ""] + } + } + + # Initial cctest settings + cc-store-settings {-cflags {} -includes {} -declare {} -link 0 -lang c -libs {} -code {} -nooutput 0} + set autosetup(cc-include-deps) {} + + msg-result "C compiler...[get-define CCACHE] [get-define CC] [get-define CFLAGS] [get-define CPPFLAGS]" + if {[get-define CXX] ne "false"} { + msg-result "C++ compiler...[get-define CCACHE] [get-define CXX] [get-define CXXFLAGS] [get-define CPPFLAGS]" + } + msg-result "Build C compiler...[get-define CC_FOR_BUILD]" + + # On Darwin, we prefer to use -g0 to avoid creating .dSYM directories + # but some compilers may not support it, so test here. + switch -glob -- [get-define host] { + *-*-darwin* { + if {[cctest -cflags {-g0}]} { + define cc-default-debug -g0 + } + } + } + + if {![cc-check-includes stdlib.h]} { + user-error "Compiler does not work. See config.log" + } +} + +cc-init diff --git a/autosetup/find_tclconfig.tcl b/autosetup/find_tclconfig.tcl new file mode 100644 index 0000000000..c3d3df8ec3 --- /dev/null +++ b/autosetup/find_tclconfig.tcl @@ -0,0 +1,24 @@ +# +# Run this TCL script to find and print the pathname for the tclConfig.sh +# file. Used by ../configure +# +if {[catch { + set libdir [tcl::pkgconfig get libdir,install] +}]} { + puts stderr "tclsh too old: does not support tcl::pkgconfig" + exit 1 +} +if {![file exists $libdir]} { + puts stderr "tclsh reported library directory \"$libdir\" does not exist" + exit 1 +} +if {![file exists $libdir/tclConfig.sh]} { + set n1 $libdir/tcl$::tcl_version + if {[file exists $n1/tclConfig.sh]} { + set libdir $n1 + } else { + puts stderr "cannot find tclConfig.sh in either $libdir or $n1" + exit 1 + } +} +puts $libdir diff --git a/autosetup/jimsh0.c b/autosetup/jimsh0.c new file mode 100644 index 0000000000..1a6453d0c8 --- /dev/null +++ b/autosetup/jimsh0.c @@ -0,0 +1,24506 @@ +/* This is single source file, bootstrap version of Jim Tcl. See http://jim.tcl.tk/ */ +#define JIM_COMPAT +#define JIM_ANSIC +#define JIM_REGEXP +#define HAVE_NO_AUTOCONF +#define JIM_TINY +#define _JIMAUTOCONF_H +#define TCL_LIBRARY "." +#define jim_ext_bootstrap +#define jim_ext_aio +#define jim_ext_readdir +#define jim_ext_regexp +#define jim_ext_file +#define jim_ext_glob +#define jim_ext_exec +#define jim_ext_clock +#define jim_ext_array +#define jim_ext_stdlib +#define jim_ext_tclcompat +#if defined(_MSC_VER) +#define TCL_PLATFORM_OS "windows" +#define TCL_PLATFORM_PLATFORM "windows" +#define TCL_PLATFORM_PATH_SEPARATOR ";" +#define HAVE_MKDIR_ONE_ARG +#define HAVE_SYSTEM +#elif defined(__MINGW32__) +#define TCL_PLATFORM_OS "mingw" +#define TCL_PLATFORM_PLATFORM "windows" +#define TCL_PLATFORM_PATH_SEPARATOR ";" +#define HAVE_MKDIR_ONE_ARG +#define HAVE_SYSTEM +#define HAVE_SYS_TIME_H +#define HAVE_DIRENT_H +#define HAVE_UNISTD_H +#define HAVE_UMASK +#include +#ifndef S_IRWXG +#define S_IRWXG 0 +#endif +#ifndef S_IRWXO +#define S_IRWXO 0 +#endif +#else +#define TCL_PLATFORM_OS "unknown" +#define TCL_PLATFORM_PLATFORM "unix" +#define TCL_PLATFORM_PATH_SEPARATOR ":" +#ifdef _MINIX +#define vfork fork +#define _POSIX_SOURCE +#else +#define _GNU_SOURCE +#endif +#define HAVE_FORK +#define HAVE_WAITPID +#define HAVE_ISATTY +#define HAVE_MKSTEMP +#define HAVE_LINK +#define HAVE_SYS_TIME_H +#define HAVE_DIRENT_H +#define HAVE_UNISTD_H +#define HAVE_UMASK +#define HAVE_PIPE +#define _FILE_OFFSET_BITS 64 +#endif +#define JIM_VERSION 84 +#ifndef JIM_WIN32COMPAT_H +#define JIM_WIN32COMPAT_H + + + +#ifdef __cplusplus +extern "C" { +#endif + + +#if defined(_WIN32) || defined(WIN32) + +#define HAVE_DLOPEN +void *dlopen(const char *path, int mode); +int dlclose(void *handle); +void *dlsym(void *handle, const char *symbol); +char *dlerror(void); + + +#if defined(__MINGW32__) + #define JIM_SPRINTF_DOUBLE_NEEDS_FIX +#endif + +#ifdef _MSC_VER + + +#if _MSC_VER >= 1000 + #pragma warning(disable:4146) +#endif + +#include +#define jim_wide _int64 +#ifndef HAVE_LONG_LONG +#define HAVE_LONG_LONG +#endif +#ifndef LLONG_MAX + #define LLONG_MAX 9223372036854775807I64 +#endif +#ifndef LLONG_MIN + #define LLONG_MIN (-LLONG_MAX - 1I64) +#endif +#define JIM_WIDE_MIN LLONG_MIN +#define JIM_WIDE_MAX LLONG_MAX +#define JIM_WIDE_MODIFIER "I64d" +#define strcasecmp _stricmp +#define strtoull _strtoui64 + +#include + +#include +int gettimeofday(struct timeval *tv, void *unused); + +#define HAVE_OPENDIR +struct dirent { + char *d_name; +}; + +typedef struct DIR { + long handle; + struct _finddata_t info; + struct dirent result; + char *name; +} DIR; + +DIR *opendir(const char *name); +int closedir(DIR *dir); +struct dirent *readdir(DIR *dir); + +#endif + +#endif + +#ifdef __cplusplus +} +#endif + +#endif +#ifndef UTF8_UTIL_H +#define UTF8_UTIL_H + +#ifdef __cplusplus +extern "C" { +#endif + + + +#define MAX_UTF8_LEN 4 + +int utf8_fromunicode(char *p, unsigned uc); + +#ifndef JIM_UTF8 +#include + + +#define utf8_strlen(S, B) ((B) < 0 ? (int)strlen(S) : (B)) +#define utf8_strwidth(S, B) utf8_strlen((S), (B)) +#define utf8_tounicode(S, CP) (*(CP) = (unsigned char)*(S), 1) +#define utf8_getchars(CP, C) (*(CP) = (C), 1) +#define utf8_upper(C) toupper(C) +#define utf8_title(C) toupper(C) +#define utf8_lower(C) tolower(C) +#define utf8_index(C, I) (I) +#define utf8_charlen(C) 1 +#define utf8_prev_len(S, L) 1 +#define utf8_width(C) 1 + +#else + +#endif + +#ifdef __cplusplus +} +#endif + +#endif + +#ifndef __JIM__H +#define __JIM__H + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#include +#include + + +#ifndef HAVE_NO_AUTOCONF +#endif + + + +#ifndef jim_wide +# ifdef HAVE_LONG_LONG +# define jim_wide long long +# ifndef LLONG_MAX +# define LLONG_MAX 9223372036854775807LL +# endif +# ifndef LLONG_MIN +# define LLONG_MIN (-LLONG_MAX - 1LL) +# endif +# define JIM_WIDE_MIN LLONG_MIN +# define JIM_WIDE_MAX LLONG_MAX +# else +# define jim_wide long +# define JIM_WIDE_MIN LONG_MIN +# define JIM_WIDE_MAX LONG_MAX +# endif + + +# ifdef HAVE_LONG_LONG +# define JIM_WIDE_MODIFIER "lld" +# else +# define JIM_WIDE_MODIFIER "ld" +# define strtoull strtoul +# endif +#endif + +#define UCHAR(c) ((unsigned char)(c)) + + + +#define JIM_ABI_VERSION 101 + +#define JIM_OK 0 +#define JIM_ERR 1 +#define JIM_RETURN 2 +#define JIM_BREAK 3 +#define JIM_CONTINUE 4 +#define JIM_SIGNAL 5 +#define JIM_EXIT 6 + +#define JIM_EVAL 7 + +#define JIM_MAX_CALLFRAME_DEPTH 1000 +#define JIM_MAX_EVAL_DEPTH 2000 + + +#define JIM_PRIV_FLAG_SHIFT 20 + +#define JIM_NONE 0 +#define JIM_ERRMSG 1 +#define JIM_ENUM_ABBREV 2 +#define JIM_UNSHARED 4 +#define JIM_MUSTEXIST 8 +#define JIM_NORESULT 16 + + +#define JIM_SUBST_NOVAR 1 +#define JIM_SUBST_NOCMD 2 +#define JIM_SUBST_NOESC 4 +#define JIM_SUBST_FLAG 128 + + +#define JIM_CASESENS 0 +#define JIM_NOCASE 1 +#define JIM_OPT_END 2 + + +#define JIM_PATH_LEN 1024 + + +#define JIM_NOTUSED(V) ((void) V) + +#define JIM_LIBPATH "auto_path" +#define JIM_INTERACTIVE "tcl_interactive" + + +typedef struct Jim_Stack { + int len; + int maxlen; + void **vector; +} Jim_Stack; + + +typedef struct Jim_HashEntry { + void *key; + union { + void *val; + int intval; + } u; + struct Jim_HashEntry *next; +} Jim_HashEntry; + +typedef struct Jim_HashTableType { + unsigned int (*hashFunction)(const void *key); + void *(*keyDup)(void *privdata, const void *key); + void *(*valDup)(void *privdata, const void *obj); + int (*keyCompare)(void *privdata, const void *key1, const void *key2); + void (*keyDestructor)(void *privdata, void *key); + void (*valDestructor)(void *privdata, void *obj); +} Jim_HashTableType; + +typedef struct Jim_HashTable { + Jim_HashEntry **table; + const Jim_HashTableType *type; + void *privdata; + unsigned int size; + unsigned int sizemask; + unsigned int used; + unsigned int collisions; + unsigned int uniq; +} Jim_HashTable; + +typedef struct Jim_HashTableIterator { + Jim_HashTable *ht; + Jim_HashEntry *entry, *nextEntry; + int index; +} Jim_HashTableIterator; + + +#define JIM_HT_INITIAL_SIZE 16 + + +#define Jim_FreeEntryVal(ht, entry) \ + if ((ht)->type->valDestructor) \ + (ht)->type->valDestructor((ht)->privdata, (entry)->u.val) + +#define Jim_SetHashVal(ht, entry, _val_) do { \ + if ((ht)->type->valDup) \ + (entry)->u.val = (ht)->type->valDup((ht)->privdata, (_val_)); \ + else \ + (entry)->u.val = (_val_); \ +} while(0) + +#define Jim_SetHashIntVal(ht, entry, _val_) (entry)->u.intval = (_val_) + +#define Jim_FreeEntryKey(ht, entry) \ + if ((ht)->type->keyDestructor) \ + (ht)->type->keyDestructor((ht)->privdata, (entry)->key) + +#define Jim_SetHashKey(ht, entry, _key_) do { \ + if ((ht)->type->keyDup) \ + (entry)->key = (ht)->type->keyDup((ht)->privdata, (_key_)); \ + else \ + (entry)->key = (void *)(_key_); \ +} while(0) + +#define Jim_CompareHashKeys(ht, key1, key2) \ + (((ht)->type->keyCompare) ? \ + (ht)->type->keyCompare((ht)->privdata, (key1), (key2)) : \ + (key1) == (key2)) + +#define Jim_HashKey(ht, key) ((ht)->type->hashFunction(key) + (ht)->uniq) + +#define Jim_GetHashEntryKey(he) ((he)->key) +#define Jim_GetHashEntryVal(he) ((he)->u.val) +#define Jim_GetHashEntryIntVal(he) ((he)->u.intval) +#define Jim_GetHashTableCollisions(ht) ((ht)->collisions) +#define Jim_GetHashTableSize(ht) ((ht)->size) +#define Jim_GetHashTableUsed(ht) ((ht)->used) + + +typedef struct Jim_Obj { + char *bytes; + const struct Jim_ObjType *typePtr; + int refCount; + int length; + + union { + + jim_wide wideValue; + + int intValue; + + double doubleValue; + + void *ptr; + + struct { + void *ptr1; + void *ptr2; + } twoPtrValue; + + struct { + void *ptr; + int int1; + int int2; + } ptrIntValue; + + struct { + struct Jim_VarVal *vv; + unsigned long callFrameId; + int global; + } varValue; + + struct { + struct Jim_Obj *nsObj; + struct Jim_Cmd *cmdPtr; + unsigned long procEpoch; + } cmdValue; + + struct { + struct Jim_Obj **ele; + int len; + int maxLen; + } listValue; + + struct Jim_Dict *dictValue; + + struct { + int maxLength; + int charLength; + } strValue; + + struct { + unsigned long id; + struct Jim_Reference *refPtr; + } refValue; + + struct { + struct Jim_Obj *fileNameObj; + int lineNumber; + } sourceValue; + + struct { + struct Jim_Obj *varNameObjPtr; + struct Jim_Obj *indexObjPtr; + } dictSubstValue; + struct { + int line; + int argc; + } scriptLineValue; + } internalRep; + struct Jim_Obj *prevObjPtr; + struct Jim_Obj *nextObjPtr; +} Jim_Obj; + + +#define Jim_IncrRefCount(objPtr) \ + ++(objPtr)->refCount +#define Jim_DecrRefCount(interp, objPtr) \ + if (--(objPtr)->refCount <= 0) Jim_FreeObj(interp, objPtr) +#define Jim_IsShared(objPtr) \ + ((objPtr)->refCount > 1) + +#define Jim_FreeNewObj Jim_FreeObj + + +#define Jim_FreeIntRep(i,o) \ + if ((o)->typePtr && (o)->typePtr->freeIntRepProc) \ + (o)->typePtr->freeIntRepProc(i, o) + + +#define Jim_GetIntRepPtr(o) (o)->internalRep.ptr + + +#define Jim_SetIntRepPtr(o, p) \ + (o)->internalRep.ptr = (p) + + +struct Jim_Interp; + +typedef void (Jim_FreeInternalRepProc)(struct Jim_Interp *interp, + struct Jim_Obj *objPtr); +typedef void (Jim_DupInternalRepProc)(struct Jim_Interp *interp, + struct Jim_Obj *srcPtr, Jim_Obj *dupPtr); +typedef void (Jim_UpdateStringProc)(struct Jim_Obj *objPtr); + +typedef struct Jim_ObjType { + const char *name; + Jim_FreeInternalRepProc *freeIntRepProc; + Jim_DupInternalRepProc *dupIntRepProc; + Jim_UpdateStringProc *updateStringProc; + int flags; +} Jim_ObjType; + + +#define JIM_TYPE_NONE 0 +#define JIM_TYPE_REFERENCES 1 + + + +typedef struct Jim_CallFrame { + unsigned long id; + int level; + struct Jim_HashTable vars; + struct Jim_HashTable *staticVars; + struct Jim_CallFrame *parent; + Jim_Obj *const *argv; + int argc; + Jim_Obj *procArgsObjPtr; + Jim_Obj *procBodyObjPtr; + struct Jim_CallFrame *next; + Jim_Obj *nsObj; + Jim_Obj *unused_fileNameObj; + int unused_line; + Jim_Stack *localCommands; + struct Jim_Obj *tailcallObj; + struct Jim_Cmd *tailcallCmd; +} Jim_CallFrame; + + +typedef struct Jim_EvalFrame { + Jim_CallFrame *framePtr; + int level; + int procLevel; + struct Jim_Cmd *cmd; + struct Jim_EvalFrame *parent; + Jim_Obj *const *argv; + int argc; + Jim_Obj *scriptObj; +} Jim_EvalFrame; + +typedef struct Jim_VarVal { + Jim_Obj *objPtr; + struct Jim_CallFrame *linkFramePtr; + int refCount; +} Jim_VarVal; + + +typedef int Jim_CmdProc(struct Jim_Interp *interp, int argc, + Jim_Obj *const *argv); +typedef void Jim_DelCmdProc(struct Jim_Interp *interp, void *privData); + +typedef struct Jim_Dict { + struct JimDictHashEntry { + int offset; + unsigned hash; + } *ht; + unsigned int size; + unsigned int sizemask; + unsigned int uniq; + Jim_Obj **table; + int len; + int maxLen; + unsigned int dummy; +} Jim_Dict; + +typedef struct Jim_Cmd { + int inUse; + int isproc; + struct Jim_Cmd *prevCmd; + Jim_Obj *cmdNameObj; + union { + struct { + + Jim_CmdProc *cmdProc; + Jim_DelCmdProc *delProc; + void *privData; + } native; + struct { + + Jim_Obj *argListObjPtr; + Jim_Obj *bodyObjPtr; + Jim_HashTable *staticVars; + int argListLen; + int reqArity; + int optArity; + int argsPos; + int upcall; + struct Jim_ProcArg { + Jim_Obj *nameObjPtr; + Jim_Obj *defaultObjPtr; + } *arglist; + Jim_Obj *nsObj; + } proc; + } u; +} Jim_Cmd; + + +typedef struct Jim_PrngState { + unsigned char sbox[256]; + unsigned int i, j; +} Jim_PrngState; + +typedef struct Jim_Interp { + Jim_Obj *result; + int unused_errorLine; + Jim_Obj *currentFilenameObj; + int break_level; + int maxCallFrameDepth; + int maxEvalDepth; + int evalDepth; + int returnCode; + int returnLevel; + int exitCode; + long id; + int signal_level; + jim_wide sigmask; + int (*signal_set_result)(struct Jim_Interp *interp, jim_wide sigmask); + Jim_CallFrame *framePtr; + Jim_CallFrame *topFramePtr; + struct Jim_HashTable commands; + unsigned long procEpoch; /* Incremented every time the result + of procedures names lookup caching + may no longer be valid. */ + unsigned long callFrameEpoch; /* Incremented every time a new + callframe is created. This id is used for the + 'ID' field contained in the Jim_CallFrame + structure. */ + int local; + int quitting; + int safeexpr; + Jim_Obj *liveList; + Jim_Obj *freeList; + Jim_Obj *unused_currentScriptObj; + Jim_EvalFrame topEvalFrame; + Jim_EvalFrame *evalFrame; + int procLevel; + Jim_Obj * const *unused_argv; + Jim_Obj *nullScriptObj; + Jim_Obj *emptyObj; + Jim_Obj *trueObj; + Jim_Obj *falseObj; + unsigned long referenceNextId; + struct Jim_HashTable references; + unsigned long lastCollectId; /* reference max Id of the last GC + execution. It's set to ~0 while the collection + is running as sentinel to avoid to recursive + calls via the [collect] command inside + finalizers. */ + jim_wide lastCollectTime; + Jim_Obj *stackTrace; + Jim_Obj *errorProc; + Jim_Obj *unknown; + Jim_Obj *defer; + Jim_Obj *traceCmdObj; + int unknown_called; + int errorFlag; + void *cmdPrivData; /* Used to pass the private data pointer to + a command. It is set to what the user specified + via Jim_CreateCommand(). */ + + Jim_Cmd *oldCmdCache; + int oldCmdCacheSize; + struct Jim_CallFrame *freeFramesList; + struct Jim_HashTable assocData; + Jim_PrngState *prngState; + struct Jim_HashTable packages; + Jim_Stack *loadHandles; +} Jim_Interp; + +#define Jim_SetResultString(i,s,l) Jim_SetResult(i, Jim_NewStringObj(i,s,l)) +#define Jim_SetResultInt(i,intval) Jim_SetResult(i, Jim_NewIntObj(i,intval)) + +#define Jim_SetResultBool(i,b) Jim_SetResultInt(i, b) +#define Jim_SetEmptyResult(i) Jim_SetResult(i, (i)->emptyObj) +#define Jim_GetResult(i) ((i)->result) +#define Jim_CmdPrivData(i) ((i)->cmdPrivData) + +#define Jim_SetResult(i,o) do { \ + Jim_Obj *_resultObjPtr_ = (o); \ + Jim_IncrRefCount(_resultObjPtr_); \ + Jim_DecrRefCount(i,(i)->result); \ + (i)->result = _resultObjPtr_; \ +} while(0) + + +#define Jim_GetId(i) (++(i)->id) + + +#define JIM_REFERENCE_TAGLEN 7 /* The tag is fixed-length, because the reference + string representation must be fixed length. */ +typedef struct Jim_Reference { + Jim_Obj *objPtr; + Jim_Obj *finalizerCmdNamePtr; + char tag[JIM_REFERENCE_TAGLEN+1]; +} Jim_Reference; + + +#define Jim_NewEmptyStringObj(i) Jim_NewStringObj(i, "", 0) +#define Jim_FreeHashTableIterator(iter) Jim_Free(iter) + +#define JIM_EXPORT extern + + + +JIM_EXPORT void *(*Jim_Allocator)(void *ptr, size_t size); + +#define Jim_Free(P) Jim_Allocator((P), 0) +#define Jim_Realloc(P, S) Jim_Allocator((P), (S)) +#define Jim_Alloc(S) Jim_Allocator(NULL, (S)) +JIM_EXPORT char * Jim_StrDup (const char *s); +JIM_EXPORT char *Jim_StrDupLen(const char *s, int l); + + +JIM_EXPORT char **Jim_GetEnviron(void); +JIM_EXPORT void Jim_SetEnviron(char **env); +JIM_EXPORT int Jim_MakeTempFile(Jim_Interp *interp, const char *filename_template, int unlink_file); +#ifndef CLOCK_REALTIME +# define CLOCK_REALTIME 0 +#endif +#ifndef CLOCK_MONOTONIC +# define CLOCK_MONOTONIC 1 +#endif +#ifndef CLOCK_MONOTONIC_RAW +# define CLOCK_MONOTONIC_RAW CLOCK_MONOTONIC +#endif +JIM_EXPORT jim_wide Jim_GetTimeUsec(unsigned type); + + +JIM_EXPORT int Jim_Eval(Jim_Interp *interp, const char *script); + + +JIM_EXPORT int Jim_EvalSource(Jim_Interp *interp, const char *filename, int lineno, const char *script); + +#define Jim_Eval_Named(I, S, F, L) Jim_EvalSource((I), (F), (L), (S)) + +JIM_EXPORT int Jim_EvalGlobal(Jim_Interp *interp, const char *script); +JIM_EXPORT int Jim_EvalFile(Jim_Interp *interp, const char *filename); +JIM_EXPORT int Jim_EvalFileGlobal(Jim_Interp *interp, const char *filename); +JIM_EXPORT int Jim_EvalObj (Jim_Interp *interp, Jim_Obj *scriptObjPtr); +JIM_EXPORT int Jim_EvalObjVector (Jim_Interp *interp, int objc, + Jim_Obj *const *objv); +JIM_EXPORT int Jim_EvalObjList(Jim_Interp *interp, Jim_Obj *listObj); +JIM_EXPORT int Jim_EvalObjPrefix(Jim_Interp *interp, Jim_Obj *prefix, + int objc, Jim_Obj *const *objv); +#define Jim_EvalPrefix(i, p, oc, ov) Jim_EvalObjPrefix((i), Jim_NewStringObj((i), (p), -1), (oc), (ov)) +JIM_EXPORT int Jim_EvalNamespace(Jim_Interp *interp, Jim_Obj *scriptObj, Jim_Obj *nsObj); +JIM_EXPORT int Jim_SubstObj (Jim_Interp *interp, Jim_Obj *substObjPtr, + Jim_Obj **resObjPtrPtr, int flags); + + +JIM_EXPORT Jim_Obj *Jim_GetSourceInfo(Jim_Interp *interp, Jim_Obj *objPtr, + int *lineptr); + +JIM_EXPORT void Jim_SetSourceInfo(Jim_Interp *interp, Jim_Obj *objPtr, + Jim_Obj *fileNameObj, int lineNumber); + + + +JIM_EXPORT void Jim_InitStack(Jim_Stack *stack); +JIM_EXPORT void Jim_FreeStack(Jim_Stack *stack); +JIM_EXPORT int Jim_StackLen(Jim_Stack *stack); +JIM_EXPORT void Jim_StackPush(Jim_Stack *stack, void *element); +JIM_EXPORT void * Jim_StackPop(Jim_Stack *stack); +JIM_EXPORT void * Jim_StackPeek(Jim_Stack *stack); +JIM_EXPORT void Jim_FreeStackElements(Jim_Stack *stack, void (*freeFunc)(void *ptr)); + + +JIM_EXPORT int Jim_InitHashTable (Jim_HashTable *ht, + const Jim_HashTableType *type, void *privdata); +JIM_EXPORT void Jim_ExpandHashTable (Jim_HashTable *ht, + unsigned int size); +JIM_EXPORT int Jim_AddHashEntry (Jim_HashTable *ht, const void *key, + void *val); +JIM_EXPORT int Jim_ReplaceHashEntry (Jim_HashTable *ht, + const void *key, void *val); +JIM_EXPORT int Jim_DeleteHashEntry (Jim_HashTable *ht, + const void *key); +JIM_EXPORT int Jim_FreeHashTable (Jim_HashTable *ht); +JIM_EXPORT Jim_HashEntry * Jim_FindHashEntry (Jim_HashTable *ht, + const void *key); +JIM_EXPORT Jim_HashTableIterator *Jim_GetHashTableIterator + (Jim_HashTable *ht); +JIM_EXPORT Jim_HashEntry * Jim_NextHashEntry + (Jim_HashTableIterator *iter); + + +JIM_EXPORT Jim_Obj * Jim_NewObj (Jim_Interp *interp); +JIM_EXPORT void Jim_FreeObj (Jim_Interp *interp, Jim_Obj *objPtr); +JIM_EXPORT void Jim_InvalidateStringRep (Jim_Obj *objPtr); +JIM_EXPORT Jim_Obj * Jim_DuplicateObj (Jim_Interp *interp, + Jim_Obj *objPtr); +JIM_EXPORT const char * Jim_GetString(Jim_Obj *objPtr, + int *lenPtr); +JIM_EXPORT const char *Jim_String(Jim_Obj *objPtr); +JIM_EXPORT int Jim_Length(Jim_Obj *objPtr); + + +JIM_EXPORT Jim_Obj * Jim_NewStringObj (Jim_Interp *interp, + const char *s, int len); +JIM_EXPORT Jim_Obj *Jim_NewStringObjUtf8(Jim_Interp *interp, + const char *s, int charlen); +JIM_EXPORT Jim_Obj * Jim_NewStringObjNoAlloc (Jim_Interp *interp, + char *s, int len); +JIM_EXPORT void Jim_AppendString (Jim_Interp *interp, Jim_Obj *objPtr, + const char *str, int len); +JIM_EXPORT void Jim_AppendObj (Jim_Interp *interp, Jim_Obj *objPtr, + Jim_Obj *appendObjPtr); +JIM_EXPORT void Jim_AppendStrings (Jim_Interp *interp, + Jim_Obj *objPtr, ...); +JIM_EXPORT int Jim_StringEqObj(Jim_Obj *aObjPtr, Jim_Obj *bObjPtr); +JIM_EXPORT int Jim_StringMatchObj (Jim_Interp *interp, Jim_Obj *patternObjPtr, + Jim_Obj *objPtr, int nocase); +JIM_EXPORT Jim_Obj * Jim_StringRangeObj (Jim_Interp *interp, + Jim_Obj *strObjPtr, Jim_Obj *firstObjPtr, + Jim_Obj *lastObjPtr); +JIM_EXPORT Jim_Obj * Jim_FormatString (Jim_Interp *interp, + Jim_Obj *fmtObjPtr, int objc, Jim_Obj *const *objv); +JIM_EXPORT Jim_Obj * Jim_ScanString (Jim_Interp *interp, Jim_Obj *strObjPtr, + Jim_Obj *fmtObjPtr, int flags); +JIM_EXPORT int Jim_CompareStringImmediate (Jim_Interp *interp, + Jim_Obj *objPtr, const char *str); +JIM_EXPORT int Jim_StringCompareObj(Jim_Interp *interp, Jim_Obj *firstObjPtr, + Jim_Obj *secondObjPtr, int nocase); +JIM_EXPORT int Jim_Utf8Length(Jim_Interp *interp, Jim_Obj *objPtr); + + +JIM_EXPORT Jim_Obj * Jim_NewReference (Jim_Interp *interp, + Jim_Obj *objPtr, Jim_Obj *tagPtr, Jim_Obj *cmdNamePtr); +JIM_EXPORT Jim_Reference * Jim_GetReference (Jim_Interp *interp, + Jim_Obj *objPtr); +JIM_EXPORT int Jim_SetFinalizer (Jim_Interp *interp, Jim_Obj *objPtr, Jim_Obj *cmdNamePtr); +JIM_EXPORT int Jim_GetFinalizer (Jim_Interp *interp, Jim_Obj *objPtr, Jim_Obj **cmdNamePtrPtr); + + +JIM_EXPORT Jim_Interp * Jim_CreateInterp (void); +JIM_EXPORT void Jim_FreeInterp (Jim_Interp *i); +JIM_EXPORT int Jim_GetExitCode (Jim_Interp *interp); +JIM_EXPORT const char *Jim_ReturnCode(int code); +JIM_EXPORT void Jim_SetResultFormatted(Jim_Interp *interp, const char *format, ...); + + +JIM_EXPORT void Jim_RegisterCoreCommands (Jim_Interp *interp); +JIM_EXPORT int Jim_CreateCommand (Jim_Interp *interp, + const char *cmdName, Jim_CmdProc *cmdProc, void *privData, + Jim_DelCmdProc *delProc); +JIM_EXPORT int Jim_DeleteCommand (Jim_Interp *interp, + Jim_Obj *cmdNameObj); +JIM_EXPORT int Jim_RenameCommand (Jim_Interp *interp, + Jim_Obj *oldNameObj, Jim_Obj *newNameObj); +JIM_EXPORT Jim_Cmd * Jim_GetCommand (Jim_Interp *interp, + Jim_Obj *objPtr, int flags); +JIM_EXPORT int Jim_SetVariable (Jim_Interp *interp, + Jim_Obj *nameObjPtr, Jim_Obj *valObjPtr); +JIM_EXPORT int Jim_SetVariableStr (Jim_Interp *interp, + const char *name, Jim_Obj *objPtr); +JIM_EXPORT int Jim_SetGlobalVariableStr (Jim_Interp *interp, + const char *name, Jim_Obj *objPtr); +JIM_EXPORT int Jim_SetVariableStrWithStr (Jim_Interp *interp, + const char *name, const char *val); +JIM_EXPORT int Jim_SetVariableLink (Jim_Interp *interp, + Jim_Obj *nameObjPtr, Jim_Obj *targetNameObjPtr, + Jim_CallFrame *targetCallFrame); +JIM_EXPORT Jim_Obj * Jim_MakeGlobalNamespaceName(Jim_Interp *interp, + Jim_Obj *nameObjPtr); +JIM_EXPORT Jim_Obj * Jim_GetVariable (Jim_Interp *interp, + Jim_Obj *nameObjPtr, int flags); +JIM_EXPORT Jim_Obj * Jim_GetGlobalVariable (Jim_Interp *interp, + Jim_Obj *nameObjPtr, int flags); +JIM_EXPORT Jim_Obj * Jim_GetVariableStr (Jim_Interp *interp, + const char *name, int flags); +JIM_EXPORT Jim_Obj * Jim_GetGlobalVariableStr (Jim_Interp *interp, + const char *name, int flags); +JIM_EXPORT int Jim_UnsetVariable (Jim_Interp *interp, + Jim_Obj *nameObjPtr, int flags); + + +JIM_EXPORT Jim_CallFrame *Jim_GetCallFrameByLevel(Jim_Interp *interp, + Jim_Obj *levelObjPtr); + + +JIM_EXPORT int Jim_Collect (Jim_Interp *interp); +JIM_EXPORT void Jim_CollectIfNeeded (Jim_Interp *interp); + + +JIM_EXPORT int Jim_GetIndex (Jim_Interp *interp, Jim_Obj *objPtr, + int *indexPtr); + + +JIM_EXPORT Jim_Obj * Jim_NewListObj (Jim_Interp *interp, + Jim_Obj *const *elements, int len); +JIM_EXPORT void Jim_ListInsertElements (Jim_Interp *interp, + Jim_Obj *listPtr, int listindex, int objc, Jim_Obj *const *objVec); +JIM_EXPORT void Jim_ListAppendElement (Jim_Interp *interp, + Jim_Obj *listPtr, Jim_Obj *objPtr); +JIM_EXPORT void Jim_ListAppendList (Jim_Interp *interp, + Jim_Obj *listPtr, Jim_Obj *appendListPtr); +JIM_EXPORT int Jim_ListLength (Jim_Interp *interp, Jim_Obj *objPtr); +JIM_EXPORT int Jim_ListIndex (Jim_Interp *interp, Jim_Obj *listPrt, + int listindex, Jim_Obj **objPtrPtr, int seterr); +JIM_EXPORT Jim_Obj *Jim_ListGetIndex(Jim_Interp *interp, Jim_Obj *listPtr, int idx); +JIM_EXPORT int Jim_SetListIndex (Jim_Interp *interp, + Jim_Obj *varNamePtr, Jim_Obj *const *indexv, int indexc, + Jim_Obj *newObjPtr); +JIM_EXPORT Jim_Obj * Jim_ConcatObj (Jim_Interp *interp, int objc, + Jim_Obj *const *objv); +JIM_EXPORT Jim_Obj *Jim_ListJoin(Jim_Interp *interp, + Jim_Obj *listObjPtr, const char *joinStr, int joinStrLen); + + +JIM_EXPORT Jim_Obj * Jim_NewDictObj (Jim_Interp *interp, + Jim_Obj *const *elements, int len); +JIM_EXPORT int Jim_DictKey (Jim_Interp *interp, Jim_Obj *dictPtr, + Jim_Obj *keyPtr, Jim_Obj **objPtrPtr, int flags); +JIM_EXPORT int Jim_DictKeysVector (Jim_Interp *interp, + Jim_Obj *dictPtr, Jim_Obj *const *keyv, int keyc, + Jim_Obj **objPtrPtr, int flags); +JIM_EXPORT int Jim_SetDictKeysVector (Jim_Interp *interp, + Jim_Obj *varNamePtr, Jim_Obj *const *keyv, int keyc, + Jim_Obj *newObjPtr, int flags); +JIM_EXPORT Jim_Obj **Jim_DictPairs(Jim_Interp *interp, + Jim_Obj *dictPtr, int *len); +JIM_EXPORT int Jim_DictAddElement(Jim_Interp *interp, Jim_Obj *objPtr, + Jim_Obj *keyObjPtr, Jim_Obj *valueObjPtr); + +#define JIM_DICTMATCH_KEYS 0x0001 +#define JIM_DICTMATCH_VALUES 0x002 + +JIM_EXPORT int Jim_DictMatchTypes(Jim_Interp *interp, Jim_Obj *objPtr, Jim_Obj *patternObj, int match_type, int return_types); +JIM_EXPORT int Jim_DictSize(Jim_Interp *interp, Jim_Obj *objPtr); +JIM_EXPORT int Jim_DictInfo(Jim_Interp *interp, Jim_Obj *objPtr); +JIM_EXPORT Jim_Obj *Jim_DictMerge(Jim_Interp *interp, int objc, Jim_Obj *const *objv); + + +JIM_EXPORT int Jim_GetReturnCode (Jim_Interp *interp, Jim_Obj *objPtr, + int *intPtr); + + +JIM_EXPORT int Jim_EvalExpression (Jim_Interp *interp, + Jim_Obj *exprObjPtr); +JIM_EXPORT int Jim_GetBoolFromExpr (Jim_Interp *interp, + Jim_Obj *exprObjPtr, int *boolPtr); + + +JIM_EXPORT int Jim_GetBoolean(Jim_Interp *interp, Jim_Obj *objPtr, + int *booleanPtr); + + +JIM_EXPORT int Jim_GetWide (Jim_Interp *interp, Jim_Obj *objPtr, + jim_wide *widePtr); +JIM_EXPORT int Jim_GetWideExpr(Jim_Interp *interp, Jim_Obj *objPtr, + jim_wide *widePtr); +JIM_EXPORT int Jim_GetLong (Jim_Interp *interp, Jim_Obj *objPtr, + long *longPtr); +#define Jim_NewWideObj Jim_NewIntObj +JIM_EXPORT Jim_Obj * Jim_NewIntObj (Jim_Interp *interp, + jim_wide wideValue); + + +JIM_EXPORT int Jim_GetDouble(Jim_Interp *interp, Jim_Obj *objPtr, + double *doublePtr); +JIM_EXPORT void Jim_SetDouble(Jim_Interp *interp, Jim_Obj *objPtr, + double doubleValue); +JIM_EXPORT Jim_Obj * Jim_NewDoubleObj(Jim_Interp *interp, double doubleValue); + + +JIM_EXPORT void Jim_WrongNumArgs (Jim_Interp *interp, int argc, + Jim_Obj *const *argv, const char *msg); +JIM_EXPORT int Jim_GetEnum (Jim_Interp *interp, Jim_Obj *objPtr, + const char * const *tablePtr, int *indexPtr, const char *name, int flags); +JIM_EXPORT int Jim_CheckShowCommands(Jim_Interp *interp, Jim_Obj *objPtr, + const char *const *tablePtr); +JIM_EXPORT int Jim_ScriptIsComplete(Jim_Interp *interp, + Jim_Obj *scriptObj, char *stateCharPtr); + +JIM_EXPORT int Jim_FindByName(const char *name, const char * const array[], size_t len); + + +typedef void (Jim_InterpDeleteProc)(Jim_Interp *interp, void *data); +JIM_EXPORT void * Jim_GetAssocData(Jim_Interp *interp, const char *key); +JIM_EXPORT int Jim_SetAssocData(Jim_Interp *interp, const char *key, + Jim_InterpDeleteProc *delProc, void *data); +JIM_EXPORT int Jim_DeleteAssocData(Jim_Interp *interp, const char *key); +JIM_EXPORT int Jim_CheckAbiVersion(Jim_Interp *interp, int abi_version); + + + + +JIM_EXPORT int Jim_PackageProvide (Jim_Interp *interp, + const char *name, const char *ver, int flags); +JIM_EXPORT int Jim_PackageRequire (Jim_Interp *interp, + const char *name, int flags); +#define Jim_PackageProvideCheck(INTERP, NAME) \ + if (Jim_CheckAbiVersion(INTERP, JIM_ABI_VERSION) == JIM_ERR || Jim_PackageProvide(INTERP, NAME, "1.0", JIM_ERRMSG)) \ + return JIM_ERR + + +JIM_EXPORT void Jim_MakeErrorMessage (Jim_Interp *interp); + + +JIM_EXPORT int Jim_InteractivePrompt (Jim_Interp *interp); +JIM_EXPORT void Jim_HistoryLoad(const char *filename); +JIM_EXPORT void Jim_HistorySave(const char *filename); +JIM_EXPORT char *Jim_HistoryGetline(Jim_Interp *interp, const char *prompt); +JIM_EXPORT void Jim_HistorySetCompletion(Jim_Interp *interp, Jim_Obj *completionCommandObj); +JIM_EXPORT void Jim_HistorySetHints(Jim_Interp *interp, Jim_Obj *hintsCommandObj); +JIM_EXPORT void Jim_HistoryAdd(const char *line); +JIM_EXPORT void Jim_HistoryShow(void); +JIM_EXPORT void Jim_HistorySetMaxLen(int length); +JIM_EXPORT int Jim_HistoryGetMaxLen(void); + + +JIM_EXPORT int Jim_InitStaticExtensions(Jim_Interp *interp); +JIM_EXPORT int Jim_StringToWide(const char *str, jim_wide *widePtr, int base); +JIM_EXPORT int Jim_IsBigEndian(void); + +#define Jim_CheckSignal(i) ((i)->signal_level && (i)->sigmask) +JIM_EXPORT void Jim_SignalSetIgnored(jim_wide mask); + + +JIM_EXPORT int Jim_LoadLibrary(Jim_Interp *interp, const char *pathName); +JIM_EXPORT void Jim_FreeLoadHandles(Jim_Interp *interp); + + +JIM_EXPORT int Jim_AioFilehandle(Jim_Interp *interp, Jim_Obj *command); + + +JIM_EXPORT int Jim_IsDict(Jim_Obj *objPtr); +JIM_EXPORT int Jim_IsList(Jim_Obj *objPtr); + +#ifdef __cplusplus +} +#endif + +#endif + +#ifndef JIM_SUBCMD_H +#define JIM_SUBCMD_H + + +#ifdef __cplusplus +extern "C" { +#endif + + +#define JIM_MODFLAG_HIDDEN 0x0001 +#define JIM_MODFLAG_FULLARGV 0x0002 + + + +typedef int jim_subcmd_function(Jim_Interp *interp, int argc, Jim_Obj *const *argv); + +typedef struct { + const char *cmd; + const char *args; + jim_subcmd_function *function; + short minargs; + short maxargs; + unsigned short flags; +} jim_subcmd_type; + +#define JIM_DEF_SUBCMD(name, args, minargs, maxargs) { name, args, NULL, minargs, maxargs } +#define JIM_DEF_SUBCMD_HIDDEN(name, args, minargs, maxargs) { name, args, NULL, minargs, maxargs, JIM_MODFLAG_HIDDEN } + +const jim_subcmd_type * +Jim_ParseSubCmd(Jim_Interp *interp, const jim_subcmd_type *command_table, int argc, Jim_Obj *const *argv); + +int Jim_SubCmdProc(Jim_Interp *interp, int argc, Jim_Obj *const *argv); + +int Jim_CallSubCmd(Jim_Interp *interp, const jim_subcmd_type *ct, int argc, Jim_Obj *const *argv); + +void Jim_SubCmdArgError(Jim_Interp *interp, const jim_subcmd_type *ct, Jim_Obj *subcmd); + +#ifdef __cplusplus +} +#endif + +#endif +#ifndef JIMREGEXP_H +#define JIMREGEXP_H + + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +typedef struct { + int rm_so; + int rm_eo; +} regmatch_t; + + +typedef struct regexp { + + int re_nsub; + + + int cflags; + int err; + int regstart; + int reganch; + int regmust; + int regmlen; + int *program; + + + const char *regparse; + int p; + int proglen; + + + int eflags; + const char *start; + const char *reginput; + const char *regbol; + + + regmatch_t *pmatch; + int nmatch; +} regexp; + +typedef regexp regex_t; + +#define REG_EXTENDED 0 +#define REG_NEWLINE 1 +#define REG_ICASE 2 + +#define REG_NOTBOL 16 + +enum { + REG_NOERROR, + REG_NOMATCH, + REG_BADPAT, + REG_ERR_NULL_ARGUMENT, + REG_ERR_UNKNOWN, + REG_ERR_TOO_BIG, + REG_ERR_NOMEM, + REG_ERR_TOO_MANY_PAREN, + REG_ERR_UNMATCHED_PAREN, + REG_ERR_UNMATCHED_BRACES, + REG_ERR_BAD_COUNT, + REG_ERR_JUNK_ON_END, + REG_ERR_OPERAND_COULD_BE_EMPTY, + REG_ERR_NESTED_COUNT, + REG_ERR_INTERNAL, + REG_ERR_COUNT_FOLLOWS_NOTHING, + REG_ERR_INVALID_ESCAPE, + REG_ERR_CORRUPTED, + REG_ERR_NULL_CHAR, + REG_ERR_UNMATCHED_BRACKET, + REG_ERR_NUM +}; + +int jim_regcomp(regex_t *preg, const char *regex, int cflags); +int jim_regexec(regex_t *preg, const char *string, size_t nmatch, regmatch_t pmatch[], int eflags); +size_t jim_regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size); +void jim_regfree(regex_t *preg); + +#ifdef __cplusplus +} +#endif + +#endif +#ifndef JIM_SIGNAL_H +#define JIM_SIGNAL_H + +#ifdef __cplusplus +extern "C" { +#endif + +const char *Jim_SignalId(int sig); + +#ifdef __cplusplus +} +#endif + +#endif +#ifndef JIMIOCOMPAT_H +#define JIMIOCOMPAT_H + + +#include +#include +#include + + +void Jim_SetResultErrno(Jim_Interp *interp, const char *msg); + +int Jim_OpenForWrite(const char *filename, int append); + +int Jim_OpenForRead(const char *filename); + +#if defined(__MINGW32__) || defined(_WIN32) + #ifndef STRICT + #define STRICT + #endif + #define WIN32_LEAN_AND_MEAN + #include + #include + #include + #include + + typedef HANDLE phandle_t; + #define JIM_BAD_PHANDLE INVALID_HANDLE_VALUE + + + #define WIFEXITED(STATUS) (((STATUS) & 0xff00) == 0) + #define WEXITSTATUS(STATUS) ((STATUS) & 0x00ff) + #define WIFSIGNALED(STATUS) (((STATUS) & 0xff00) != 0) + #define WTERMSIG(STATUS) (((STATUS) >> 8) & 0xff) + #define WNOHANG 1 + + int Jim_Errno(void); + + long waitpid(phandle_t phandle, int *status, int nohang); + + phandle_t JimWaitPid(long processid, int *status, int nohang); + + long JimProcessPid(phandle_t phandle); + + #define HAVE_PIPE + #define pipe(P) _pipe((P), 0, O_NOINHERIT) + + typedef struct __stat64 jim_stat_t; + #define Jim_Stat _stat64 + #define Jim_FileStat _fstat64 + #define Jim_Lseek _lseeki64 + #define O_TEXT _O_TEXT + #define O_BINARY _O_BINARY + #define Jim_SetMode _setmode + #ifndef STDIN_FILENO + #define STDIN_FILENO 0 + #endif + +#else + #if defined(HAVE_STAT64) + typedef struct stat64 jim_stat_t; + #define Jim_Stat stat64 + #if defined(HAVE_FSTAT64) + #define Jim_FileStat fstat64 + #endif + #if defined(HAVE_LSTAT64) + #define Jim_LinkStat lstat64 + #endif + #else + typedef struct stat jim_stat_t; + #define Jim_Stat stat + #if defined(HAVE_FSTAT) + #define Jim_FileStat fstat + #endif + #if defined(HAVE_LSTAT) + #define Jim_LinkStat lstat + #endif + #endif + #if defined(HAVE_LSEEK64) + #define Jim_Lseek lseek64 + #else + #define Jim_Lseek lseek + #endif + + #if defined(HAVE_UNISTD_H) + #include + #include + #include + + typedef int phandle_t; + #define Jim_Errno() errno + #define JIM_BAD_PHANDLE -1 + #define JimProcessPid(PIDTYPE) (PIDTYPE) + #define JimWaitPid waitpid + + #ifndef HAVE_EXECVPE + #define execvpe(ARG0, ARGV, ENV) execvp(ARG0, ARGV) + #endif + #endif + + #ifndef O_TEXT + #define O_TEXT 0 + #endif + +#endif + +# ifndef MAXPATHLEN +# ifdef PATH_MAX +# define MAXPATHLEN PATH_MAX +# else +# define MAXPATHLEN JIM_PATH_LEN +# endif +# endif + + +int Jim_FileStoreStatData(Jim_Interp *interp, Jim_Obj *varName, const jim_stat_t *sb); + +#endif +int Jim_bootstrapInit(Jim_Interp *interp) +{ + if (Jim_PackageProvide(interp, "bootstrap", "1.0", JIM_ERRMSG)) + return JIM_ERR; + + return Jim_EvalSource(interp, "bootstrap.tcl", 1, +"\n" +"proc package {cmd args} {\n" +" if {$cmd eq \"require\"} {\n" +" foreach path $::auto_path {\n" +" lassign $args pkg\n" +" set pkgpath $path/$pkg.tcl\n" +" if {$path eq \".\"} {\n" +" set pkgpath $pkg.tcl\n" +" }\n" +" if {[file exists $pkgpath]} {\n" +" tailcall uplevel #0 [list source $pkgpath]\n" +" }\n" +" }\n" +" }\n" +"}\n" +"set tcl_platform(bootstrap) 1\n" +); +} +int Jim_initjimshInit(Jim_Interp *interp) +{ + if (Jim_PackageProvide(interp, "initjimsh", "1.0", JIM_ERRMSG)) + return JIM_ERR; + + return Jim_EvalSource(interp, "initjimsh.tcl", 1, +"\n" +"\n" +"\n" +"proc _jimsh_init {} {\n" +" rename _jimsh_init {}\n" +" global jim::exe jim::argv0 tcl_interactive auto_path tcl_platform\n" +"\n" +"\n" +" if {[exists jim::argv0]} {\n" +" if {[string match \"*/*\" $jim::argv0]} {\n" +" set jim::exe [file join [pwd] $jim::argv0]\n" +" } else {\n" +" set jim::argv0 [file tail $jim::argv0]\n" +" set path [split [env PATH \"\"] $tcl_platform(pathSeparator)]\n" +" if {$tcl_platform(platform) eq \"windows\"} {\n" +"\n" +" set path [lmap p [list \"\" {*}$path] { string map {\\\\ /} $p }]\n" +" }\n" +" foreach p $path {\n" +" set exec [file join [pwd] $p $jim::argv0]\n" +" if {[file executable $exec]} {\n" +" set jim::exe $exec\n" +" break\n" +" }\n" +" }\n" +" }\n" +" }\n" +"\n" +"\n" +" lappend p {*}[split [env JIMLIB {}] $tcl_platform(pathSeparator)]\n" +" if {[exists jim::exe]} {\n" +" lappend p [file dirname $jim::exe]\n" +" }\n" +" lappend p {*}$auto_path\n" +" set auto_path $p\n" +"\n" +" if {$tcl_interactive && [env HOME {}] ne \"\"} {\n" +" foreach src {.jimrc jimrc.tcl} {\n" +" if {[file exists [env HOME]/$src]} {\n" +" uplevel #0 source [env HOME]/$src\n" +" break\n" +" }\n" +" }\n" +" }\n" +" return \"\"\n" +"}\n" +"\n" +"if {$tcl_platform(platform) eq \"windows\"} {\n" +" set jim::argv0 [string map {\\\\ /} $jim::argv0]\n" +"}\n" +"\n" +"\n" +"set tcl::autocomplete_commands {array clock debug dict file history info namespace package signal socket string tcl::prefix zlib}\n" +"\n" +"\n" +"\n" +"proc tcl::autocomplete {prefix} {\n" +" if {[set space [string first \" \" $prefix]] != -1} {\n" +" set cmd [string range $prefix 0 $space-1]\n" +" if {$cmd in $::tcl::autocomplete_commands || [info channel $cmd] ne \"\"} {\n" +" set arg [string range $prefix $space+1 end]\n" +"\n" +" return [lmap p [$cmd -commands] {\n" +" if {![string match \"${arg}*\" $p]} continue\n" +" function \"$cmd $p\"\n" +" }]\n" +" }\n" +" }\n" +"\n" +" if {[string match \"source *\" $prefix]} {\n" +" set path [string range $prefix 7 end]\n" +" return [lmap p [glob -nocomplain \"${path}*\"] {\n" +" function \"source $p\"\n" +" }]\n" +" }\n" +"\n" +" return [lmap p [lsort [info commands $prefix*]] {\n" +" if {[string match \"* *\" $p]} {\n" +" continue\n" +" }\n" +" function $p\n" +" }]\n" +"}\n" +"\n" +"\n" +"set tcl::stdhint_commands {array clock debug dict file history info namespace package signal string zlib}\n" +"\n" +"set tcl::stdhint_cols {\n" +" none {0}\n" +" black {30}\n" +" red {31}\n" +" green {32}\n" +" yellow {33}\n" +" blue {34}\n" +" purple {35}\n" +" cyan {36}\n" +" normal {37}\n" +" grey {30 1}\n" +" gray {30 1}\n" +" lred {31 1}\n" +" lgreen {32 1}\n" +" lyellow {33 1}\n" +" lblue {34 1}\n" +" lpurple {35 1}\n" +" lcyan {36 1}\n" +" white {37 1}\n" +"}\n" +"\n" +"\n" +"set tcl::stdhint_col $tcl::stdhint_cols(lcyan)\n" +"\n" +"\n" +"proc tcl::stdhint {string} {\n" +" set result \"\"\n" +" if {[llength $string] >= 2} {\n" +" lassign $string cmd arg\n" +" if {$cmd in $::tcl::stdhint_commands || [info channel $cmd] ne \"\"} {\n" +" catch {\n" +" set help [$cmd -help $arg]\n" +" if {[string match \"Usage: $cmd *\" $help]} {\n" +" set n [llength $string]\n" +" set subcmd [lindex $help $n]\n" +" incr n\n" +" set hint [join [lrange $help $n end]]\n" +" set prefix \"\"\n" +" if {![string match \"* \" $string]} {\n" +" if {$n == 3 && $subcmd ne $arg} {\n" +"\n" +" set prefix \"[string range $subcmd [string length $arg] end] \"\n" +" } else {\n" +" set prefix \" \"\n" +" }\n" +" }\n" +" set result [list $prefix$hint {*}$::tcl::stdhint_col]\n" +" }\n" +" }\n" +" }\n" +" }\n" +" return $result\n" +"}\n" +"\n" +"_jimsh_init\n" +); +} +int Jim_globInit(Jim_Interp *interp) +{ + if (Jim_PackageProvide(interp, "glob", "1.0", JIM_ERRMSG)) + return JIM_ERR; + + return Jim_EvalSource(interp, "glob.tcl", 1, +"\n" +"\n" +"\n" +"\n" +"\n" +"\n" +"\n" +"package require readdir\n" +"\n" +"\n" +"proc glob.globdir {dir pattern} {\n" +" if {[file exists $dir/$pattern]} {\n" +"\n" +" return [list $pattern]\n" +" }\n" +"\n" +" set result {}\n" +" set files [readdir $dir]\n" +" lappend files . ..\n" +"\n" +" foreach name $files {\n" +" if {[string match $pattern $name]} {\n" +"\n" +" if {[string index $name 0] eq \".\" && [string index $pattern 0] ne \".\"} {\n" +" continue\n" +" }\n" +" lappend result $name\n" +" }\n" +" }\n" +"\n" +" return $result\n" +"}\n" +"\n" +"\n" +"\n" +"\n" +"proc glob.explode {pattern} {\n" +" set oldexp {}\n" +" set newexp {\"\"}\n" +"\n" +" while 1 {\n" +" set oldexp $newexp\n" +" set newexp {}\n" +" set ob [string first \\{ $pattern]\n" +" set cb [string first \\} $pattern]\n" +"\n" +" if {$ob < $cb && $ob != -1} {\n" +" set mid [string range $pattern 0 $ob-1]\n" +" set subexp [lassign [glob.explode [string range $pattern $ob+1 end]] pattern]\n" +" if {$pattern eq \"\"} {\n" +" error \"unmatched open brace in glob pattern\"\n" +" }\n" +" set pattern [string range $pattern 1 end]\n" +"\n" +" foreach subs $subexp {\n" +" foreach sub [split $subs ,] {\n" +" foreach old $oldexp {\n" +" lappend newexp $old$mid$sub\n" +" }\n" +" }\n" +" }\n" +" } elseif {$cb != -1} {\n" +" set suf [string range $pattern 0 $cb-1]\n" +" set rest [string range $pattern $cb end]\n" +" break\n" +" } else {\n" +" set suf $pattern\n" +" set rest \"\"\n" +" break\n" +" }\n" +" }\n" +"\n" +" foreach old $oldexp {\n" +" lappend newexp $old$suf\n" +" }\n" +" list $rest {*}$newexp\n" +"}\n" +"\n" +"\n" +"\n" +"proc glob.glob {base pattern} {\n" +" set dir [file dirname $pattern]\n" +" if {$pattern eq $dir || $pattern eq \"\"} {\n" +" return [list [file join $base $dir] $pattern]\n" +" } elseif {$pattern eq [file tail $pattern]} {\n" +" set dir \"\"\n" +" }\n" +"\n" +"\n" +" set dirlist [glob.glob $base $dir]\n" +" set pattern [file tail $pattern]\n" +"\n" +"\n" +" set result {}\n" +" foreach {realdir dir} $dirlist {\n" +" if {![file isdir $realdir]} {\n" +" continue\n" +" }\n" +" if {[string index $dir end] ne \"/\" && $dir ne \"\"} {\n" +" append dir /\n" +" }\n" +" foreach name [glob.globdir $realdir $pattern] {\n" +" lappend result [file join $realdir $name] $dir$name\n" +" }\n" +" }\n" +" return $result\n" +"}\n" +"\n" +"\n" +"\n" +"\n" +"\n" +"\n" +"\n" +"\n" +"\n" +"\n" +"\n" +"\n" +"proc glob {args} {\n" +" set nocomplain 0\n" +" set base \"\"\n" +" set tails 0\n" +"\n" +" set n 0\n" +" foreach arg $args {\n" +" if {[info exists param]} {\n" +" set $param $arg\n" +" unset param\n" +" incr n\n" +" continue\n" +" }\n" +" switch -glob -- $arg {\n" +" -d* {\n" +" set switch $arg\n" +" set param base\n" +" }\n" +" -n* {\n" +" set nocomplain 1\n" +" }\n" +" -ta* {\n" +" set tails 1\n" +" }\n" +" -- {\n" +" incr n\n" +" break\n" +" }\n" +" -* {\n" +" return -code error \"bad option \\\"$arg\\\": must be -directory, -nocomplain, -tails, or --\"\n" +" }\n" +" * {\n" +" break\n" +" }\n" +" }\n" +" incr n\n" +" }\n" +" if {[info exists param]} {\n" +" return -code error \"missing argument to \\\"$switch\\\"\"\n" +" }\n" +" if {[llength $args] <= $n} {\n" +" return -code error \"wrong # args: should be \\\"glob ?options? pattern ?pattern ...?\\\"\"\n" +" }\n" +"\n" +" set args [lrange $args $n end]\n" +"\n" +" set result {}\n" +" foreach pattern $args {\n" +" set escpattern [string map {\n" +" \\\\\\\\ \\x01 \\\\\\{ \\x02 \\\\\\} \\x03 \\\\, \\x04\n" +" } $pattern]\n" +" set patexps [lassign [glob.explode $escpattern] rest]\n" +" if {$rest ne \"\"} {\n" +" return -code error \"unmatched close brace in glob pattern\"\n" +" }\n" +" foreach patexp $patexps {\n" +" set patexp [string map {\n" +" \\x01 \\\\\\\\ \\x02 \\{ \\x03 \\} \\x04 ,\n" +" } $patexp]\n" +" foreach {realname name} [glob.glob $base $patexp] {\n" +" incr n\n" +" if {$tails} {\n" +" lappend result $name\n" +" } else {\n" +" lappend result [file join $base $name]\n" +" }\n" +" }\n" +" }\n" +" }\n" +"\n" +" if {!$nocomplain && [llength $result] == 0} {\n" +" set s $(([llength $args] > 1) ? \"s\" : \"\")\n" +" return -code error \"no files matched glob pattern$s \\\"[join $args]\\\"\"\n" +" }\n" +"\n" +" return $result\n" +"}\n" +); +} +int Jim_stdlibInit(Jim_Interp *interp) +{ + if (Jim_PackageProvide(interp, "stdlib", "1.0", JIM_ERRMSG)) + return JIM_ERR; + + return Jim_EvalSource(interp, "stdlib.tcl", 1, +"\n" +"\n" +"if {![exists -command ref]} {\n" +"\n" +" proc ref {args} {{count 0}} {\n" +" format %08x [incr count]\n" +" }\n" +"}\n" +"\n" +"\n" +"proc lambda {arglist args} {\n" +" tailcall proc [ref {} function lambda.finalizer] $arglist {*}$args\n" +"}\n" +"\n" +"proc lambda.finalizer {name val} {\n" +" rename $name {}\n" +"}\n" +"\n" +"\n" +"proc curry {args} {\n" +" alias [ref {} function lambda.finalizer] {*}$args\n" +"}\n" +"\n" +"\n" +"\n" +"\n" +"\n" +"\n" +"\n" +"\n" +"\n" +"proc function {value} {\n" +" return $value\n" +"}\n" +"\n" +"\n" +"proc stackdump {stacktrace} {\n" +" set lines {}\n" +" lappend lines \"Traceback (most recent call last):\"\n" +" foreach {cmd l f p} [lreverse $stacktrace] {\n" +" set line {}\n" +" if {$f ne \"\"} {\n" +" append line \" File \\\"$f\\\", line $l\"\n" +" }\n" +" if {$p ne \"\"} {\n" +" append line \", in $p\"\n" +" }\n" +" if {$line ne \"\"} {\n" +" lappend lines $line\n" +" if {$cmd ne \"\"} {\n" +" set nl [string first \\n $cmd 1]\n" +" if {$nl >= 0} {\n" +" set cmd [string range $cmd 0 $nl-1]...\n" +" }\n" +" lappend lines \" $cmd\"\n" +" }\n" +" }\n" +" }\n" +" if {[llength $lines] > 1} {\n" +" return [join $lines \\n]\n" +" }\n" +"}\n" +"\n" +"\n" +"\n" +"proc defer {script} {\n" +" upvar jim::defer v\n" +" lappend v $script\n" +"}\n" +"\n" +"\n" +"\n" +"proc errorInfo {msg {stacktrace \"\"}} {\n" +" if {$stacktrace eq \"\"} {\n" +"\n" +" set stacktrace [info stacktrace]\n" +" }\n" +" lassign $stacktrace p f l cmd\n" +" if {$f ne \"\"} {\n" +" set result \"$f:$l: Error: \"\n" +" }\n" +" append result \"$msg\\n\"\n" +" append result [stackdump $stacktrace]\n" +"\n" +"\n" +" string trim $result\n" +"}\n" +"\n" +"\n" +"\n" +"proc {info nameofexecutable} {} {\n" +" if {[exists ::jim::exe]} {\n" +" return $::jim::exe\n" +" }\n" +"}\n" +"\n" +"\n" +"proc {dict update} {&varName args script} {\n" +" set keys {}\n" +" foreach {n v} $args {\n" +" upvar $v var_$v\n" +" if {[dict exists $varName $n]} {\n" +" set var_$v [dict get $varName $n]\n" +" }\n" +" }\n" +" catch {uplevel 1 $script} msg opts\n" +" if {[info exists varName]} {\n" +" foreach {n v} $args {\n" +" if {[info exists var_$v]} {\n" +" dict set varName $n [set var_$v]\n" +" } else {\n" +" dict unset varName $n\n" +" }\n" +" }\n" +" }\n" +" return {*}$opts $msg\n" +"}\n" +"\n" +"proc {dict replace} {dictionary {args {key value}}} {\n" +" if {[llength ${key value}] % 2} {\n" +" tailcall {dict replace}\n" +" }\n" +" tailcall dict merge $dictionary ${key value}\n" +"}\n" +"\n" +"\n" +"proc {dict lappend} {varName key {args value}} {\n" +" upvar $varName dict\n" +" if {[exists dict] && [dict exists $dict $key]} {\n" +" set list [dict get $dict $key]\n" +" }\n" +" lappend list {*}$value\n" +" dict set dict $key $list\n" +"}\n" +"\n" +"\n" +"proc {dict append} {varName key {args value}} {\n" +" upvar $varName dict\n" +" if {[exists dict] && [dict exists $dict $key]} {\n" +" set str [dict get $dict $key]\n" +" }\n" +" append str {*}$value\n" +" dict set dict $key $str\n" +"}\n" +"\n" +"\n" +"proc {dict incr} {varName key {increment 1}} {\n" +" upvar $varName dict\n" +" if {[exists dict] && [dict exists $dict $key]} {\n" +" set value [dict get $dict $key]\n" +" }\n" +" incr value $increment\n" +" dict set dict $key $value\n" +"}\n" +"\n" +"\n" +"proc {dict remove} {dictionary {args key}} {\n" +" foreach k $key {\n" +" dict unset dictionary $k\n" +" }\n" +" return $dictionary\n" +"}\n" +"\n" +"\n" +"proc {dict for} {vars dictionary script} {\n" +" if {[llength $vars] != 2} {\n" +" return -code error \"must have exactly two variable names\"\n" +" }\n" +" dict size $dictionary\n" +" tailcall foreach $vars $dictionary $script\n" +"}\n" +); +} +int Jim_tclcompatInit(Jim_Interp *interp) +{ + if (Jim_PackageProvide(interp, "tclcompat", "1.0", JIM_ERRMSG)) + return JIM_ERR; + + return Jim_EvalSource(interp, "tclcompat.tcl", 1, +"\n" +"\n" +"\n" +"\n" +"\n" +"\n" +"\n" +"\n" +"set env [env]\n" +"\n" +"\n" +"if {[exists -command stdout]} {\n" +"\n" +" foreach p {gets flush close eof seek tell} {\n" +" proc $p {chan args} {p} {\n" +" tailcall $chan $p {*}$args\n" +" }\n" +" }\n" +" unset p\n" +"\n" +"\n" +"\n" +" proc puts {{-nonewline {}} {chan stdout} msg} {\n" +" if {${-nonewline} ni {-nonewline {}}} {\n" +" tailcall ${-nonewline} puts $msg\n" +" }\n" +" tailcall $chan puts {*}${-nonewline} $msg\n" +" }\n" +"\n" +"\n" +"\n" +"\n" +"\n" +" proc read {{-nonewline {}} chan} {\n" +" if {${-nonewline} ni {-nonewline {}}} {\n" +" tailcall ${-nonewline} read {*}${chan}\n" +" }\n" +" tailcall $chan read {*}${-nonewline}\n" +" }\n" +"\n" +" proc fconfigure {f args} {\n" +" foreach {n v} $args {\n" +" switch -glob -- $n {\n" +" -bl* {\n" +" $f ndelay $(!$v)\n" +" }\n" +" -bu* {\n" +" $f buffering $v\n" +" }\n" +" -tr* {\n" +" $f translation $v\n" +" }\n" +" default {\n" +" return -code error \"fconfigure: unknown option $n\"\n" +" }\n" +" }\n" +" }\n" +" }\n" +"}\n" +"\n" +"\n" +"proc fileevent {args} {\n" +" tailcall {*}$args\n" +"}\n" +"\n" +"\n" +"\n" +"proc parray {arrayname {pattern *} {puts puts}} {\n" +" upvar $arrayname a\n" +"\n" +" set max 0\n" +" foreach name [array names a $pattern]] {\n" +" if {[string length $name] > $max} {\n" +" set max [string length $name]\n" +" }\n" +" }\n" +" incr max [string length $arrayname]\n" +" incr max 2\n" +" foreach name [lsort [array names a $pattern]] {\n" +" $puts [format \"%-${max}s = %s\" $arrayname\\($name\\) $a($name)]\n" +" }\n" +"}\n" +"\n" +"\n" +"proc {file copy} {{force {}} source target} {\n" +" try {\n" +" if {$force ni {{} -force}} {\n" +" error \"bad option \\\"$force\\\": should be -force\"\n" +" }\n" +"\n" +" set in [open $source rb]\n" +"\n" +" if {[file exists $target]} {\n" +" if {$force eq \"\"} {\n" +" error \"error copying \\\"$source\\\" to \\\"$target\\\": file already exists\"\n" +" }\n" +"\n" +" if {$source eq $target} {\n" +" return\n" +" }\n" +"\n" +"\n" +" file stat $source ss\n" +" file stat $target ts\n" +" if {$ss(dev) == $ts(dev) && $ss(ino) == $ts(ino) && $ss(ino)} {\n" +" return\n" +" }\n" +" }\n" +" set out [open $target wb]\n" +" $in copyto $out\n" +" $out close\n" +" } on error {msg opts} {\n" +" incr opts(-level)\n" +" return {*}$opts $msg\n" +" } finally {\n" +" catch {$in close}\n" +" }\n" +"}\n" +"\n" +"\n" +"\n" +"proc popen {cmd {mode r}} {\n" +" lassign [pipe] r w\n" +" try {\n" +" if {[string match \"w*\" $mode]} {\n" +" lappend cmd <@$r &\n" +" set pids [exec {*}$cmd]\n" +" $r close\n" +" set f $w\n" +" } else {\n" +" lappend cmd >@$w &\n" +" set pids [exec {*}$cmd]\n" +" $w close\n" +" set f $r\n" +" }\n" +" lambda {cmd args} {f pids} {\n" +" if {$cmd eq \"pid\"} {\n" +" return $pids\n" +" }\n" +" if {$cmd eq \"close\"} {\n" +" $f close\n" +"\n" +" set retopts {}\n" +" foreach p $pids {\n" +" lassign [wait $p] status - rc\n" +" if {$status eq \"CHILDSTATUS\"} {\n" +" if {$rc == 0} {\n" +" continue\n" +" }\n" +" set msg \"child process exited abnormally\"\n" +" } else {\n" +" set msg \"child killed: received signal\"\n" +" }\n" +" set retopts [list -code error -errorcode [list $status $p $rc] $msg]\n" +" }\n" +" return {*}$retopts\n" +" }\n" +" tailcall $f $cmd {*}$args\n" +" }\n" +" } on error {error opts} {\n" +" $r close\n" +" $w close\n" +" error $error\n" +" }\n" +"}\n" +"\n" +"\n" +"local proc pid {{channelId {}}} {\n" +" if {$channelId eq \"\"} {\n" +" tailcall upcall pid\n" +" }\n" +" if {[catch {$channelId tell}]} {\n" +" return -code error \"can not find channel named \\\"$channelId\\\"\"\n" +" }\n" +" if {[catch {$channelId pid} pids]} {\n" +" return \"\"\n" +" }\n" +" return $pids\n" +"}\n" +"\n" +"\n" +"\n" +"proc throw {code {msg \"\"}} {\n" +" return -code $code $msg\n" +"}\n" +"\n" +"\n" +"proc {file delete force} {path} {\n" +" foreach e [readdir $path] {\n" +" file delete -force $path/$e\n" +" }\n" +" file delete $path\n" +"}\n" +); +} + + +#include +#include +#include +#include +#include +#ifdef HAVE_UNISTD_H +#include +#include +#endif +#ifdef HAVE_UTIL_H +#include +#endif +#ifdef HAVE_PTY_H +#include +#endif + + +#if defined(HAVE_SYS_SOCKET_H) && defined(HAVE_SELECT) && defined(HAVE_NETINET_IN_H) && defined(HAVE_NETDB_H) && defined(HAVE_ARPA_INET_H) +#include +#include +#include +#include +#include +#ifdef HAVE_SYS_UN_H +#include +#endif +#define HAVE_SOCKETS +#elif defined (__MINGW32__) + +#endif + +#if defined(JIM_SSL) +#include +#include +#endif + +#ifdef HAVE_TERMIOS_H +#endif + + +#define AIO_CMD_LEN 32 +#define AIO_DEFAULT_RBUF_LEN 256 +#define AIO_DEFAULT_WBUF_LIMIT (64 * 1024) + +#define AIO_KEEPOPEN 1 +#define AIO_NODELETE 2 +#define AIO_EOF 4 +#define AIO_WBUF_NONE 8 +#define AIO_NONBLOCK 16 + +#define AIO_ONEREAD 32 + +enum wbuftype { + WBUF_OPT_NONE, + WBUF_OPT_LINE, + WBUF_OPT_FULL, +}; + +#if defined(JIM_IPV6) +#define IPV6 1 +#else +#define IPV6 0 +#ifndef PF_INET6 +#define PF_INET6 0 +#endif +#endif +#if defined(HAVE_SYS_UN_H) && defined(PF_UNIX) +#define UNIX_SOCKETS 1 +#else +#define UNIX_SOCKETS 0 +#endif + + + + +static int JimReadableTimeout(int fd, long ms) +{ +#ifdef HAVE_SELECT + int retval; + struct timeval tv; + fd_set rfds; + + FD_ZERO(&rfds); + + FD_SET(fd, &rfds); + tv.tv_sec = ms / 1000; + tv.tv_usec = (ms % 1000) * 1000; + + retval = select(fd + 1, &rfds, NULL, NULL, ms == 0 ? NULL : &tv); + + if (retval > 0) { + return JIM_OK; + } + return JIM_ERR; +#else + return JIM_OK; +#endif +} + + +struct AioFile; + +typedef struct { + int (*writer)(struct AioFile *af, const char *buf, int len); + int (*reader)(struct AioFile *af, char *buf, int len, int pending); + int (*error)(const struct AioFile *af); + const char *(*strerror)(struct AioFile *af); + int (*verify)(struct AioFile *af); +} JimAioFopsType; + +typedef struct AioFile +{ + Jim_Obj *filename; + int wbuft; + int flags; + long timeout; + int fd; + int addr_family; + void *ssl; + const JimAioFopsType *fops; + Jim_Obj *readbuf; + Jim_Obj *writebuf; + char *rbuf; + size_t rbuf_len; + size_t wbuf_limit; +} AioFile; + +static void aio_consume(Jim_Obj *objPtr, int n); + +static int stdio_writer(struct AioFile *af, const char *buf, int len) +{ + int ret = write(af->fd, buf, len); + if (ret < 0 && errno == EPIPE) { + aio_consume(af->writebuf, Jim_Length(af->writebuf)); + } + return ret; +} + +static int stdio_reader(struct AioFile *af, char *buf, int len, int nb) +{ + if (nb || af->timeout == 0 || JimReadableTimeout(af->fd, af->timeout) == JIM_OK) { + + int ret; + + errno = 0; + ret = read(af->fd, buf, len); + if (ret <= 0 && errno != EAGAIN && errno != EINTR) { + af->flags |= AIO_EOF; + } + return ret; + } + errno = ETIMEDOUT; + return -1; +} + +static int stdio_error(const AioFile *af) +{ + if (af->flags & AIO_EOF) { + return JIM_OK; + } + + switch (errno) { + case EAGAIN: + case EINTR: + case ETIMEDOUT: +#ifdef ECONNRESET + case ECONNRESET: +#endif +#ifdef ECONNABORTED + case ECONNABORTED: +#endif + return JIM_OK; + default: + return JIM_ERR; + } +} + +static const char *stdio_strerror(struct AioFile *af) +{ + return strerror(errno); +} + +static const JimAioFopsType stdio_fops = { + stdio_writer, + stdio_reader, + stdio_error, + stdio_strerror, + NULL, +}; + + +static void aio_set_nonblocking(AioFile *af, int nb) +{ +#ifdef O_NDELAY + int old = !!(af->flags & AIO_NONBLOCK); + if (old != nb) { + int fmode = fcntl(af->fd, F_GETFL); + if (nb) { + fmode |= O_NDELAY; + af->flags |= AIO_NONBLOCK; + } + else { + fmode &= ~O_NDELAY; + af->flags &= ~AIO_NONBLOCK; + } + (void)fcntl(af->fd, F_SETFL, fmode); + } +#endif +} + +static int aio_start_nonblocking(AioFile *af) +{ + int old = !!(af->flags & AIO_NONBLOCK); + if (af->timeout) { + aio_set_nonblocking(af, 1); + } + return old; +} + +static int JimAioSubCmdProc(Jim_Interp *interp, int argc, Jim_Obj *const *argv); +static AioFile *JimMakeChannel(Jim_Interp *interp, int fd, Jim_Obj *filename, + const char *hdlfmt, int family, int flags); + + +static const char *JimAioErrorString(AioFile *af) +{ + if (af && af->fops) + return af->fops->strerror(af); + + return strerror(errno); +} + +static void JimAioSetError(Jim_Interp *interp, Jim_Obj *name) +{ + AioFile *af = Jim_CmdPrivData(interp); + + if (name) { + Jim_SetResultFormatted(interp, "%#s: %s", name, JimAioErrorString(af)); + } + else { + Jim_SetResultString(interp, JimAioErrorString(af), -1); + } +} + +static int aio_eof(AioFile *af) +{ + return af->flags & AIO_EOF; +} + +static int JimCheckStreamError(Jim_Interp *interp, AioFile *af) +{ + int ret = 0; + if (!aio_eof(af)) { + ret = af->fops->error(af); + if (ret) { + JimAioSetError(interp, af->filename); + } + } + return ret; +} + +static void aio_consume(Jim_Obj *objPtr, int n) +{ + assert(objPtr->bytes); + assert(n <= objPtr->length); + + + memmove(objPtr->bytes, objPtr->bytes + n, objPtr->length - n + 1); + objPtr->length -= n; +} + + +static int aio_flush(Jim_Interp *interp, AioFile *af); + +#ifdef jim_ext_eventloop +static int aio_autoflush(Jim_Interp *interp, void *clientData, int mask) +{ + AioFile *af = clientData; + + aio_flush(interp, af); + if (Jim_Length(af->writebuf) == 0) { + + return -1; + } + return 0; +} +#endif + + +static int aio_flush(Jim_Interp *interp, AioFile *af) +{ + int len; + const char *pt = Jim_GetString(af->writebuf, &len); + if (len) { + int ret = af->fops->writer(af, pt, len); + if (ret > 0) { + + aio_consume(af->writebuf, ret); + } + if (ret < 0) { + return JimCheckStreamError(interp, af); + } + if (Jim_Length(af->writebuf)) { +#ifdef jim_ext_eventloop + void *handler = Jim_FindFileHandler(interp, af->fd, JIM_EVENT_WRITABLE); + if (handler == NULL) { + Jim_CreateFileHandler(interp, af->fd, JIM_EVENT_WRITABLE, aio_autoflush, af, NULL); + return JIM_OK; + } + else if (handler == af) { + + return JIM_OK; + } +#endif + + Jim_SetResultString(interp, "send buffer is full", -1); + return JIM_ERR; + } + } + return JIM_OK; +} + +static int aio_read_len(Jim_Interp *interp, AioFile *af, unsigned flags, int neededLen) +{ + if (!af->readbuf) { + af->readbuf = Jim_NewStringObj(interp, NULL, 0); + } + + if (neededLen >= 0) { + neededLen -= Jim_Length(af->readbuf); + if (neededLen <= 0) { + return JIM_OK; + } + } + + while (neededLen && !aio_eof(af)) { + int retval; + int readlen; + + if (neededLen == -1) { + readlen = af->rbuf_len; + } + else { + readlen = (neededLen > af->rbuf_len ? af->rbuf_len : neededLen); + } + + if (!af->rbuf) { + af->rbuf = Jim_Alloc(af->rbuf_len); + } + retval = af->fops->reader(af, af->rbuf, readlen, flags & AIO_NONBLOCK); + if (retval > 0) { + if (retval) { + Jim_AppendString(interp, af->readbuf, af->rbuf, retval); + } + if (neededLen != -1) { + neededLen -= retval; + } + if (flags & AIO_ONEREAD) { + return JIM_OK; + } + continue; + } + if ((flags & AIO_ONEREAD) || JimCheckStreamError(interp, af)) { + return JIM_ERR; + } + break; + } + + return JIM_OK; +} + +static Jim_Obj *aio_read_consume(Jim_Interp *interp, AioFile *af, int neededLen) +{ + Jim_Obj *objPtr = NULL; + + if (neededLen < 0 || af->readbuf == NULL || Jim_Length(af->readbuf) <= neededLen) { + objPtr = af->readbuf; + af->readbuf = NULL; + } + else if (af->readbuf) { + + int len; + const char *pt = Jim_GetString(af->readbuf, &len); + + objPtr = Jim_NewStringObj(interp, pt, neededLen); + aio_consume(af->readbuf, neededLen); + } + + return objPtr; +} + +static void JimAioDelProc(Jim_Interp *interp, void *privData) +{ + AioFile *af = privData; + + JIM_NOTUSED(interp); + + + aio_flush(interp, af); + Jim_DecrRefCount(interp, af->writebuf); + +#if UNIX_SOCKETS + if (af->addr_family == PF_UNIX && (af->flags & AIO_NODELETE) == 0) { + + Jim_Obj *filenameObj = aio_sockname(interp, af->fd); + if (filenameObj) { + if (Jim_Length(filenameObj)) { + remove(Jim_String(filenameObj)); + } + Jim_FreeNewObj(interp, filenameObj); + } + } +#endif + + Jim_DecrRefCount(interp, af->filename); + +#ifdef jim_ext_eventloop + + Jim_DeleteFileHandler(interp, af->fd, JIM_EVENT_READABLE | JIM_EVENT_WRITABLE | JIM_EVENT_EXCEPTION); +#endif + +#if defined(JIM_SSL) + if (af->ssl != NULL) { + SSL_free(af->ssl); + } +#endif + if (!(af->flags & AIO_KEEPOPEN)) { + close(af->fd); + } + if (af->readbuf) { + Jim_FreeNewObj(interp, af->readbuf); + } + + Jim_Free(af->rbuf); + Jim_Free(af); +} + +static int aio_cmd_read(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + AioFile *af = Jim_CmdPrivData(interp); + int nonewline = 0; + jim_wide neededLen = -1; + static const char * const options[] = { "-pending", "-nonewline", NULL }; + enum { OPT_PENDING, OPT_NONEWLINE }; + int option; + int nb; + Jim_Obj *objPtr; + + if (argc) { + if (*Jim_String(argv[0]) == '-') { + if (Jim_GetEnum(interp, argv[0], options, &option, NULL, JIM_ERRMSG) != JIM_OK) { + return JIM_ERR; + } + switch (option) { + case OPT_PENDING: + + break; + case OPT_NONEWLINE: + nonewline++; + break; + } + } + else { + if (Jim_GetWide(interp, argv[0], &neededLen) != JIM_OK) + return JIM_ERR; + if (neededLen < 0) { + Jim_SetResultString(interp, "invalid parameter: negative len", -1); + return JIM_ERR; + } + } + argc--; + argv++; + } + if (argc) { + return -1; + } + + + nb = aio_start_nonblocking(af); + + if (aio_read_len(interp, af, nb ? AIO_NONBLOCK : 0, neededLen) != JIM_OK) { + aio_set_nonblocking(af, nb); + return JIM_ERR; + } + objPtr = aio_read_consume(interp, af, neededLen); + + aio_set_nonblocking(af, nb); + + if (objPtr) { + if (nonewline) { + int len; + const char *s = Jim_GetString(objPtr, &len); + + if (len > 0 && s[len - 1] == '\n') { + objPtr->length--; + objPtr->bytes[objPtr->length] = '\0'; + } + } + Jim_SetResult(interp, objPtr); + } + else { + Jim_SetEmptyResult(interp); + } + return JIM_OK; +} + +int Jim_AioFilehandle(Jim_Interp *interp, Jim_Obj *command) +{ + Jim_Cmd *cmdPtr = Jim_GetCommand(interp, command, JIM_ERRMSG); + + + if (cmdPtr && !cmdPtr->isproc && cmdPtr->u.native.cmdProc == JimAioSubCmdProc) { + return ((AioFile *) cmdPtr->u.native.privData)->fd; + } + Jim_SetResultFormatted(interp, "Not a filehandle: \"%#s\"", command); + return -1; +} + +static int aio_cmd_getfd(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + AioFile *af = Jim_CmdPrivData(interp); + + + aio_flush(interp, af); + + Jim_SetResultInt(interp, af->fd); + + return JIM_OK; +} + +static int aio_cmd_copy(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + AioFile *af = Jim_CmdPrivData(interp); + jim_wide count = 0; + jim_wide maxlen = JIM_WIDE_MAX; + int ok = 1; + Jim_Obj *objv[4]; + + if (argc == 2) { + if (Jim_GetWide(interp, argv[1], &maxlen) != JIM_OK) { + return JIM_ERR; + } + } + + objv[0] = argv[0]; + objv[1] = Jim_NewStringObj(interp, "flush", -1); + if (Jim_EvalObjVector(interp, 2, objv) != JIM_OK) { + Jim_SetResultFormatted(interp, "Not a filehandle: \"%#s\"", argv[0]); + return JIM_ERR; + } + + + objv[0] = argv[0]; + objv[1] = Jim_NewStringObj(interp, "puts", -1); + objv[2] = Jim_NewStringObj(interp, "-nonewline", -1); + Jim_IncrRefCount(objv[1]); + Jim_IncrRefCount(objv[2]); + + while (count < maxlen) { + jim_wide len = maxlen - count; + if (len > af->rbuf_len) { + len = af->rbuf_len; + } + if (aio_read_len(interp, af, 0, len) != JIM_OK) { + ok = 0; + break; + } + objv[3] = aio_read_consume(interp, af, len); + count += Jim_Length(objv[3]); + if (Jim_EvalObjVector(interp, 4, objv) != JIM_OK) { + ok = 0; + break; + } + if (aio_eof(af)) { + break; + } + if (count >= 16384 && af->rbuf_len < 65536) { + + af->rbuf_len = 65536; + af->rbuf = Jim_Realloc(af->rbuf, af->rbuf_len); + } + } + + Jim_DecrRefCount(interp, objv[1]); + Jim_DecrRefCount(interp, objv[2]); + + if (!ok) { + return JIM_ERR; + } + + Jim_SetResultInt(interp, count); + + return JIM_OK; +} + +static int aio_cmd_gets(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + AioFile *af = Jim_CmdPrivData(interp); + Jim_Obj *objPtr = NULL; + int len; + int nb; + unsigned flags = AIO_ONEREAD; + char *nl = NULL; + int offset = 0; + + errno = 0; + + + nb = aio_start_nonblocking(af); + if (nb) { + flags |= AIO_NONBLOCK; + } + + while (!aio_eof(af)) { + if (af->readbuf) { + const char *pt = Jim_GetString(af->readbuf, &len); + nl = memchr(pt + offset, '\n', len - offset); + if (nl) { + + objPtr = Jim_NewStringObj(interp, pt, nl - pt); + + aio_consume(af->readbuf, nl - pt + 1); + break; + } + offset = len; + } + + + if (aio_read_len(interp, af, flags, -1) != JIM_OK) { + break; + } + } + + aio_set_nonblocking(af, nb); + + if (!nl && aio_eof(af) && af->readbuf) { + + objPtr = af->readbuf; + af->readbuf = NULL; + } + else if (!objPtr) { + objPtr = Jim_NewStringObj(interp, NULL, 0); + } + + if (argc) { + if (Jim_SetVariable(interp, argv[0], objPtr) != JIM_OK) { + Jim_FreeNewObj(interp, objPtr); + return JIM_ERR; + } + + len = Jim_Length(objPtr); + + if (!nl && len == 0) { + + len = -1; + } + Jim_SetResultInt(interp, len); + } + else { + Jim_SetResult(interp, objPtr); + } + return JIM_OK; +} + +static int aio_cmd_puts(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + AioFile *af = Jim_CmdPrivData(interp); + int wlen; + const char *wdata; + Jim_Obj *strObj; + int wnow = 0; + int nl = 1; + + if (argc == 2) { + if (!Jim_CompareStringImmediate(interp, argv[0], "-nonewline")) { + return -1; + } + strObj = argv[1]; + nl = 0; + } + else { + strObj = argv[0]; + } + +#ifdef JIM_MAINTAINER + if (Jim_IsShared(af->writebuf)) { + Jim_DecrRefCount(interp, af->writebuf); + af->writebuf = Jim_DuplicateObj(interp, af->writebuf); + Jim_IncrRefCount(af->writebuf); + } +#endif + Jim_AppendObj(interp, af->writebuf, strObj); + if (nl) { + Jim_AppendString(interp, af->writebuf, "\n", 1); + } + + + wdata = Jim_GetString(af->writebuf, &wlen); + switch (af->wbuft) { + case WBUF_OPT_NONE: + + wnow = 1; + break; + + case WBUF_OPT_LINE: + + if (nl || memchr(wdata, '\n', wlen) != NULL) { + wnow = 1; + } + break; + + case WBUF_OPT_FULL: + if (wlen >= af->wbuf_limit) { + wnow = 1; + } + break; + } + + if (wnow) { + return aio_flush(interp, af); + } + return JIM_OK; +} + +static int aio_cmd_isatty(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ +#ifdef HAVE_ISATTY + AioFile *af = Jim_CmdPrivData(interp); + Jim_SetResultInt(interp, isatty(af->fd)); +#else + Jim_SetResultInt(interp, 0); +#endif + + return JIM_OK; +} + + +static int aio_cmd_flush(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + AioFile *af = Jim_CmdPrivData(interp); + return aio_flush(interp, af); +} + +static int aio_cmd_eof(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + AioFile *af = Jim_CmdPrivData(interp); + + Jim_SetResultInt(interp, !!aio_eof(af)); + return JIM_OK; +} + +static int aio_cmd_close(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + AioFile *af = Jim_CmdPrivData(interp); + if (argc == 3) { + int option = -1; +#if defined(HAVE_SOCKETS) + static const char * const options[] = { "r", "w", "-nodelete", NULL }; + enum { OPT_R, OPT_W, OPT_NODELETE }; + + if (Jim_GetEnum(interp, argv[2], options, &option, NULL, JIM_ERRMSG) != JIM_OK) { + return JIM_ERR; + } +#endif + switch (option) { +#if defined(HAVE_SHUTDOWN) + case OPT_R: + case OPT_W: + if (shutdown(af->fd, option == OPT_R ? SHUT_RD : SHUT_WR) == 0) { + return JIM_OK; + } + JimAioSetError(interp, NULL); + return JIM_ERR; +#endif +#if UNIX_SOCKETS + case OPT_NODELETE: + if (af->addr_family == PF_UNIX) { + af->flags |= AIO_NODELETE; + break; + } + +#endif + default: + Jim_SetResultString(interp, "not supported", -1); + return JIM_ERR; + } + } + + + af->flags &= ~AIO_KEEPOPEN; + + return Jim_DeleteCommand(interp, argv[0]); +} + +static int aio_cmd_seek(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + AioFile *af = Jim_CmdPrivData(interp); + int orig = SEEK_SET; + jim_wide offset; + + if (argc == 2) { + if (Jim_CompareStringImmediate(interp, argv[1], "start")) + orig = SEEK_SET; + else if (Jim_CompareStringImmediate(interp, argv[1], "current")) + orig = SEEK_CUR; + else if (Jim_CompareStringImmediate(interp, argv[1], "end")) + orig = SEEK_END; + else { + return -1; + } + } + if (Jim_GetWide(interp, argv[0], &offset) != JIM_OK) { + return JIM_ERR; + } + if (orig != SEEK_CUR || offset != 0) { + + aio_flush(interp, af); + } + if (Jim_Lseek(af->fd, offset, orig) == -1) { + JimAioSetError(interp, af->filename); + return JIM_ERR; + } + if (af->readbuf) { + Jim_FreeNewObj(interp, af->readbuf); + af->readbuf = NULL; + } + af->flags &= ~AIO_EOF; + return JIM_OK; +} + +static int aio_cmd_tell(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + AioFile *af = Jim_CmdPrivData(interp); + + Jim_SetResultInt(interp, Jim_Lseek(af->fd, 0, SEEK_CUR)); + return JIM_OK; +} + +static int aio_cmd_filename(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + AioFile *af = Jim_CmdPrivData(interp); + + Jim_SetResult(interp, af->filename); + return JIM_OK; +} + +#ifdef O_NDELAY +static int aio_cmd_ndelay(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + AioFile *af = Jim_CmdPrivData(interp); + + if (argc) { + long nb; + + if (Jim_GetLong(interp, argv[0], &nb) != JIM_OK) { + return JIM_ERR; + } + aio_set_nonblocking(af, nb); + } + Jim_SetResultInt(interp, (af->flags & AIO_NONBLOCK) ? 1 : 0); + return JIM_OK; +} +#endif + + +#ifdef HAVE_FSYNC +static int aio_cmd_sync(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + AioFile *af = Jim_CmdPrivData(interp); + + if (aio_flush(interp, af) != JIM_OK) { + return JIM_ERR; + } + fsync(af->fd); + return JIM_OK; +} +#endif + +static int aio_cmd_buffering(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + AioFile *af = Jim_CmdPrivData(interp); + Jim_Obj *resultObj; + + static const char * const options[] = { + "none", + "line", + "full", + NULL + }; + + if (argc) { + if (Jim_GetEnum(interp, argv[0], options, &af->wbuft, NULL, JIM_ERRMSG) != JIM_OK) { + return JIM_ERR; + } + + if (af->wbuft == WBUF_OPT_FULL && argc == 2) { + long l; + if (Jim_GetLong(interp, argv[1], &l) != JIM_OK || l <= 0) { + return JIM_ERR; + } + af->wbuf_limit = l; + } + + if (af->wbuft == WBUF_OPT_NONE) { + if (aio_flush(interp, af) != JIM_OK) { + return JIM_ERR; + } + } + + } + + resultObj = Jim_NewListObj(interp, NULL, 0); + Jim_ListAppendElement(interp, resultObj, Jim_NewStringObj(interp, options[af->wbuft], -1)); + if (af->wbuft == WBUF_OPT_FULL) { + Jim_ListAppendElement(interp, resultObj, Jim_NewIntObj(interp, af->wbuf_limit)); + } + Jim_SetResult(interp, resultObj); + + return JIM_OK; +} + +static int aio_cmd_translation(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + enum {OPT_BINARY, OPT_TEXT}; + static const char * const options[] = { + "binary", + "text", + NULL + }; + int opt; + + if (Jim_GetEnum(interp, argv[0], options, &opt, NULL, JIM_ERRMSG) != JIM_OK) { + return JIM_ERR; + } +#if defined(Jim_SetMode) + else { + AioFile *af = Jim_CmdPrivData(interp); + Jim_SetMode(af->fd, opt == OPT_BINARY ? O_BINARY : O_TEXT); + } +#endif + return JIM_OK; +} + +static int aio_cmd_readsize(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + AioFile *af = Jim_CmdPrivData(interp); + + if (argc) { + long l; + if (Jim_GetLong(interp, argv[0], &l) != JIM_OK || l <= 0) { + return JIM_ERR; + } + af->rbuf_len = l; + if (af->rbuf) { + af->rbuf = Jim_Realloc(af->rbuf, af->rbuf_len); + } + } + Jim_SetResultInt(interp, af->rbuf_len); + + return JIM_OK; +} + +#ifdef jim_ext_eventloop +static int aio_cmd_timeout(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ +#ifdef HAVE_SELECT + AioFile *af = Jim_CmdPrivData(interp); + if (argc == 1) { + if (Jim_GetLong(interp, argv[0], &af->timeout) != JIM_OK) { + return JIM_ERR; + } + } + Jim_SetResultInt(interp, af->timeout); + return JIM_OK; +#else + Jim_SetResultString(interp, "timeout not supported", -1); + return JIM_ERR; +#endif +} + +static int aio_eventinfo(Jim_Interp *interp, AioFile * af, unsigned mask, + int argc, Jim_Obj * const *argv) +{ + if (argc == 0) { + + Jim_Obj *objPtr = Jim_FindFileHandler(interp, af->fd, mask); + if (objPtr) { + Jim_SetResult(interp, objPtr); + } + return JIM_OK; + } + + + Jim_DeleteFileHandler(interp, af->fd, mask); + + + if (Jim_Length(argv[0])) { + Jim_CreateScriptFileHandler(interp, af->fd, mask, argv[0]); + } + + return JIM_OK; +} + +static int aio_cmd_readable(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + AioFile *af = Jim_CmdPrivData(interp); + + return aio_eventinfo(interp, af, JIM_EVENT_READABLE, argc, argv); +} + +static int aio_cmd_writable(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + AioFile *af = Jim_CmdPrivData(interp); + + return aio_eventinfo(interp, af, JIM_EVENT_WRITABLE, argc, argv); +} + +static int aio_cmd_onexception(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + AioFile *af = Jim_CmdPrivData(interp); + + return aio_eventinfo(interp, af, JIM_EVENT_EXCEPTION, argc, argv); +} +#endif + +#if defined(jim_ext_file) && defined(Jim_FileStat) +static int aio_cmd_stat(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + jim_stat_t sb; + AioFile *af = Jim_CmdPrivData(interp); + + if (Jim_FileStat(af->fd, &sb) == -1) { + JimAioSetError(interp, NULL); + return JIM_ERR; + } + return Jim_FileStoreStatData(interp, argc == 0 ? NULL : argv[0], &sb); +} +#endif + + + + +static const jim_subcmd_type aio_command_table[] = { + { "read", + "?-nonewline|len?", + aio_cmd_read, + 0, + 2, + + }, + { "copyto", + "handle ?size?", + aio_cmd_copy, + 1, + 2, + + }, + { "getfd", + NULL, + aio_cmd_getfd, + 0, + 0, + + }, + { "gets", + "?var?", + aio_cmd_gets, + 0, + 1, + + }, + { "puts", + "?-nonewline? str", + aio_cmd_puts, + 1, + 2, + + }, + { "isatty", + NULL, + aio_cmd_isatty, + 0, + 0, + + }, + { "flush", + NULL, + aio_cmd_flush, + 0, + 0, + + }, + { "eof", + NULL, + aio_cmd_eof, + 0, + 0, + + }, + { "close", + "?r(ead)|w(rite)?", + aio_cmd_close, + 0, + 1, + JIM_MODFLAG_FULLARGV, + + }, + { "seek", + "offset ?start|current|end", + aio_cmd_seek, + 1, + 2, + + }, + { "tell", + NULL, + aio_cmd_tell, + 0, + 0, + + }, + { "filename", + NULL, + aio_cmd_filename, + 0, + 0, + + }, +#ifdef O_NDELAY + { "ndelay", + "?0|1?", + aio_cmd_ndelay, + 0, + 1, + + }, +#endif +#ifdef HAVE_FSYNC + { "sync", + NULL, + aio_cmd_sync, + 0, + 0, + + }, +#endif + { "buffering", + "?none|line|full? ?size?", + aio_cmd_buffering, + 0, + 2, + + }, + { "translation", + "binary|text", + aio_cmd_translation, + 1, + 1, + + }, + { "readsize", + "?size?", + aio_cmd_readsize, + 0, + 1, + + }, +#if defined(jim_ext_file) && defined(Jim_FileStat) + { "stat", + "?var?", + aio_cmd_stat, + 0, + 1, + + }, +#endif +#ifdef jim_ext_eventloop + { "readable", + "?readable-script?", + aio_cmd_readable, + 0, + 1, + + }, + { "writable", + "?writable-script?", + aio_cmd_writable, + 0, + 1, + + }, + { "onexception", + "?exception-script?", + aio_cmd_onexception, + 0, + 1, + + }, + { "timeout", + "?ms?", + aio_cmd_timeout, + 0, + 1, + + }, +#endif + { NULL } +}; + +static int JimAioSubCmdProc(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + return Jim_CallSubCmd(interp, Jim_ParseSubCmd(interp, aio_command_table, argc, argv), argc, argv); +} + +static int parse_posix_open_mode(Jim_Interp *interp, Jim_Obj *modeObj) +{ + int i; + int flags = 0; + #ifndef O_NOCTTY + + #define O_NOCTTY 0 + #endif + static const char * const modetypes[] = { + "RDONLY", "WRONLY", "RDWR", "APPEND", "BINARY", "CREAT", "EXCL", "NOCTTY", "TRUNC", NULL + }; + static const int modeflags[] = { + O_RDONLY, O_WRONLY, O_RDWR, O_APPEND, 0, O_CREAT, O_EXCL, O_NOCTTY, O_TRUNC, + }; + + for (i = 0; i < Jim_ListLength(interp, modeObj); i++) { + int opt; + Jim_Obj *objPtr = Jim_ListGetIndex(interp, modeObj, i); + if (Jim_GetEnum(interp, objPtr, modetypes, &opt, "access mode", JIM_ERRMSG) != JIM_OK) { + return -1; + } + flags |= modeflags[opt]; + } + return flags; +} + +static int parse_open_mode(Jim_Interp *interp, Jim_Obj *filenameObj, Jim_Obj *modeObj) +{ + + int flags; + const char *mode = Jim_String(modeObj); + if (*mode == 'R' || *mode == 'W') { + return parse_posix_open_mode(interp, modeObj); + } + if (*mode == 'r') { + flags = O_RDONLY; + } + else if (*mode == 'w') { + flags = O_WRONLY | O_CREAT | O_TRUNC; + } + else if (*mode == 'a') { + flags = O_WRONLY | O_CREAT | O_APPEND; + } + else { + Jim_SetResultFormatted(interp, "%s: invalid open mode '%s'", Jim_String(filenameObj), mode); + return -1; + } + mode++; + + if (*mode == 'b') { +#ifdef O_BINARY + flags |= O_BINARY; +#endif + mode++; + } + + if (*mode == 't') { +#ifdef O_TEXT + flags |= O_TEXT; +#endif + mode++; + } + + if (*mode == '+') { + mode++; + + flags &= ~(O_RDONLY | O_WRONLY); + flags |= O_RDWR; + } + + if (*mode == 'x') { + mode++; +#ifdef O_EXCL + flags |= O_EXCL; +#endif + } + + if (*mode == 'F') { + mode++; +#ifdef O_LARGEFILE + flags |= O_LARGEFILE; +#endif + } + + if (*mode == 'e') { + + mode++; + } + return flags; +} + +static int JimAioOpenCommand(Jim_Interp *interp, int argc, + Jim_Obj *const *argv) +{ + int openflags; + const char *filename; + int fd = -1; + int n = 0; + int flags = 0; + + if (argc > 2 && Jim_CompareStringImmediate(interp, argv[2], "-noclose")) { + flags = AIO_KEEPOPEN; + n++; + } + if (argc < 2 || argc > 3 + n) { + Jim_WrongNumArgs(interp, 1, argv, "filename ?-noclose? ?mode?"); + return JIM_ERR; + } + + filename = Jim_String(argv[1]); + +#ifdef jim_ext_tclcompat + { + + + if (*filename == '|') { + Jim_Obj *evalObj[3]; + int i = 0; + + evalObj[i++] = Jim_NewStringObj(interp, "::popen", -1); + evalObj[i++] = Jim_NewStringObj(interp, filename + 1, -1); + if (argc == 3 + n) { + evalObj[i++] = argv[2 + n]; + } + + return Jim_EvalObjVector(interp, i, evalObj); + } + } +#endif + if (argc == 3 + n) { + openflags = parse_open_mode(interp, argv[1], argv[2 + n]); + if (openflags == -1) { + return JIM_ERR; + } + } + else { + openflags = O_RDONLY; + } + fd = open(filename, openflags, 0666); + if (fd < 0) { + JimAioSetError(interp, argv[1]); + return JIM_ERR; + } + + return JimMakeChannel(interp, fd, argv[1], "aio.handle%ld", 0, flags) ? JIM_OK : JIM_ERR; +} + + +static AioFile *JimMakeChannel(Jim_Interp *interp, int fd, Jim_Obj *filename, + const char *hdlfmt, int family, int flags) +{ + AioFile *af; + char buf[AIO_CMD_LEN]; + Jim_Obj *cmdname; + + snprintf(buf, sizeof(buf), hdlfmt, Jim_GetId(interp)); + cmdname = Jim_NewStringObj(interp, buf, -1); + if (!filename) { + filename = cmdname; + } + Jim_IncrRefCount(filename); + + + af = Jim_Alloc(sizeof(*af)); + memset(af, 0, sizeof(*af)); + af->filename = filename; + af->fd = fd; + af->addr_family = family; + af->fops = &stdio_fops; + af->ssl = NULL; + if (flags & AIO_WBUF_NONE) { + af->wbuft = WBUF_OPT_NONE; + } + else { +#ifdef HAVE_ISATTY + af->wbuft = isatty(af->fd) ? WBUF_OPT_LINE : WBUF_OPT_FULL; +#else + af->wbuft = WBUF_OPT_FULL; +#endif + } + +#ifdef FD_CLOEXEC + if ((flags & AIO_KEEPOPEN) == 0) { + (void)fcntl(af->fd, F_SETFD, FD_CLOEXEC); + } +#endif + aio_set_nonblocking(af, !!(flags & AIO_NONBLOCK)); + + af->flags |= flags; + + af->writebuf = Jim_NewStringObj(interp, NULL, 0); + Jim_IncrRefCount(af->writebuf); + af->wbuf_limit = AIO_DEFAULT_WBUF_LIMIT; + af->rbuf_len = AIO_DEFAULT_RBUF_LEN; + + + Jim_CreateCommand(interp, buf, JimAioSubCmdProc, af, JimAioDelProc); + + Jim_SetResult(interp, Jim_MakeGlobalNamespaceName(interp, cmdname)); + + return af; +} + +#if defined(HAVE_PIPE) || (defined(HAVE_SOCKETPAIR) && UNIX_SOCKETS) || defined(HAVE_OPENPTY) +static int JimMakeChannelPair(Jim_Interp *interp, int p[2], Jim_Obj *filename, + const char *hdlfmt, int family, int flags) +{ + if (JimMakeChannel(interp, p[0], filename, hdlfmt, family, flags)) { + Jim_Obj *objPtr = Jim_NewListObj(interp, NULL, 0); + Jim_ListAppendElement(interp, objPtr, Jim_GetResult(interp)); + if (JimMakeChannel(interp, p[1], filename, hdlfmt, family, flags)) { + Jim_ListAppendElement(interp, objPtr, Jim_GetResult(interp)); + Jim_SetResult(interp, objPtr); + return JIM_OK; + } + } + + + close(p[0]); + close(p[1]); + JimAioSetError(interp, NULL); + return JIM_ERR; +} +#endif + +#ifdef HAVE_PIPE +static int JimCreatePipe(Jim_Interp *interp, Jim_Obj *filenameObj, int flags) +{ + int p[2]; + + if (pipe(p) != 0) { + JimAioSetError(interp, NULL); + return JIM_ERR; + } + + return JimMakeChannelPair(interp, p, filenameObj, "aio.pipe%ld", 0, flags); +} + + +static int JimAioPipeCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + if (argc != 1) { + Jim_WrongNumArgs(interp, 1, argv, ""); + return JIM_ERR; + } + return JimCreatePipe(interp, argv[0], 0); +} +#endif + +#ifdef HAVE_OPENPTY +static int JimAioOpenPtyCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + int p[2]; + char path[MAXPATHLEN]; + + if (argc != 1) { + Jim_WrongNumArgs(interp, 1, argv, ""); + return JIM_ERR; + } + + if (openpty(&p[0], &p[1], path, NULL, NULL) != 0) { + JimAioSetError(interp, NULL); + return JIM_ERR; + } + + + return JimMakeChannelPair(interp, p, Jim_NewStringObj(interp, path, -1), "aio.pty%ld", 0, 0); + return JimMakeChannelPair(interp, p, Jim_NewStringObj(interp, path, -1), "aio.pty%ld", 0, 0); +} +#endif + + + +int Jim_aioInit(Jim_Interp *interp) +{ + if (Jim_PackageProvide(interp, "aio", "1.0", JIM_ERRMSG)) + return JIM_ERR; + +#if defined(JIM_SSL) + Jim_CreateCommand(interp, "load_ssl_certs", JimAioLoadSSLCertsCommand, NULL, NULL); +#endif + + Jim_CreateCommand(interp, "open", JimAioOpenCommand, NULL, NULL); +#ifdef HAVE_SOCKETS + Jim_CreateCommand(interp, "socket", JimAioSockCommand, NULL, NULL); +#endif +#ifdef HAVE_PIPE + Jim_CreateCommand(interp, "pipe", JimAioPipeCommand, NULL, NULL); +#endif + + + JimMakeChannel(interp, fileno(stdin), NULL, "stdin", 0, AIO_KEEPOPEN); + JimMakeChannel(interp, fileno(stdout), NULL, "stdout", 0, AIO_KEEPOPEN); + JimMakeChannel(interp, fileno(stderr), NULL, "stderr", 0, AIO_KEEPOPEN | AIO_WBUF_NONE); + + return JIM_OK; +} + +#include +#include +#include + + +#ifdef HAVE_DIRENT_H +#include +#endif + +int Jim_ReaddirCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + const char *dirPath; + DIR *dirPtr; + struct dirent *entryPtr; + int nocomplain = 0; + + if (argc == 3 && Jim_CompareStringImmediate(interp, argv[1], "-nocomplain")) { + nocomplain = 1; + } + if (argc != 2 && !nocomplain) { + Jim_WrongNumArgs(interp, 1, argv, "?-nocomplain? dirPath"); + return JIM_ERR; + } + + dirPath = Jim_String(argv[1 + nocomplain]); + + dirPtr = opendir(dirPath); + if (dirPtr == NULL) { + if (nocomplain) { + return JIM_OK; + } + Jim_SetResultString(interp, strerror(errno), -1); + return JIM_ERR; + } + else { + Jim_Obj *listObj = Jim_NewListObj(interp, NULL, 0); + + while ((entryPtr = readdir(dirPtr)) != NULL) { + if (entryPtr->d_name[0] == '.') { + if (entryPtr->d_name[1] == '\0') { + continue; + } + if ((entryPtr->d_name[1] == '.') && (entryPtr->d_name[2] == '\0')) + continue; + } + Jim_ListAppendElement(interp, listObj, Jim_NewStringObj(interp, entryPtr->d_name, -1)); + } + closedir(dirPtr); + + Jim_SetResult(interp, listObj); + + return JIM_OK; + } +} + +int Jim_readdirInit(Jim_Interp *interp) +{ + Jim_PackageProvideCheck(interp, "readdir"); + Jim_CreateCommand(interp, "readdir", Jim_ReaddirCmd, NULL, NULL); + return JIM_OK; +} + +#include +#include + +#if defined(JIM_REGEXP) +#else + #include + #define jim_regcomp regcomp + #define jim_regexec regexec + #define jim_regerror regerror + #define jim_regfree regfree +#endif + +static void FreeRegexpInternalRep(Jim_Interp *interp, Jim_Obj *objPtr) +{ + jim_regfree(objPtr->internalRep.ptrIntValue.ptr); + Jim_Free(objPtr->internalRep.ptrIntValue.ptr); +} + +static const Jim_ObjType regexpObjType = { + "regexp", + FreeRegexpInternalRep, + NULL, + NULL, + JIM_TYPE_NONE +}; + +static regex_t *SetRegexpFromAny(Jim_Interp *interp, Jim_Obj *objPtr, unsigned flags) +{ + regex_t *compre; + const char *pattern; + int ret; + + + if (objPtr->typePtr == ®expObjType && + objPtr->internalRep.ptrIntValue.ptr && objPtr->internalRep.ptrIntValue.int1 == flags) { + + return objPtr->internalRep.ptrIntValue.ptr; + } + + + + + pattern = Jim_String(objPtr); + compre = Jim_Alloc(sizeof(regex_t)); + + if ((ret = jim_regcomp(compre, pattern, REG_EXTENDED | flags)) != 0) { + char buf[100]; + + jim_regerror(ret, compre, buf, sizeof(buf)); + Jim_SetResultFormatted(interp, "couldn't compile regular expression pattern: %s", buf); + jim_regfree(compre); + Jim_Free(compre); + return NULL; + } + + Jim_FreeIntRep(interp, objPtr); + + objPtr->typePtr = ®expObjType; + objPtr->internalRep.ptrIntValue.int1 = flags; + objPtr->internalRep.ptrIntValue.ptr = compre; + + return compre; +} + +int Jim_RegexpCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + int opt_indices = 0; + int opt_all = 0; + int opt_inline = 0; + regex_t *regex; + int match, i, j; + int offset = 0; + regmatch_t *pmatch = NULL; + int source_len; + int result = JIM_OK; + const char *pattern; + const char *source_str; + int num_matches = 0; + int num_vars; + Jim_Obj *resultListObj = NULL; + int regcomp_flags = 0; + int eflags = 0; + int option; + enum { + OPT_INDICES, OPT_NOCASE, OPT_LINE, OPT_ALL, OPT_INLINE, OPT_START, OPT_END + }; + static const char * const options[] = { + "-indices", "-nocase", "-line", "-all", "-inline", "-start", "--", NULL + }; + + if (argc < 3) { + wrongNumArgs: + Jim_WrongNumArgs(interp, 1, argv, + "?-switch ...? exp string ?matchVar? ?subMatchVar ...?"); + return JIM_ERR; + } + + for (i = 1; i < argc; i++) { + const char *opt = Jim_String(argv[i]); + + if (*opt != '-') { + break; + } + if (Jim_GetEnum(interp, argv[i], options, &option, "switch", JIM_ERRMSG | JIM_ENUM_ABBREV) != JIM_OK) { + return JIM_ERR; + } + if (option == OPT_END) { + i++; + break; + } + switch (option) { + case OPT_INDICES: + opt_indices = 1; + break; + + case OPT_NOCASE: + regcomp_flags |= REG_ICASE; + break; + + case OPT_LINE: + regcomp_flags |= REG_NEWLINE; + break; + + case OPT_ALL: + opt_all = 1; + break; + + case OPT_INLINE: + opt_inline = 1; + break; + + case OPT_START: + if (++i == argc) { + goto wrongNumArgs; + } + if (Jim_GetIndex(interp, argv[i], &offset) != JIM_OK) { + return JIM_ERR; + } + break; + } + } + if (argc - i < 2) { + goto wrongNumArgs; + } + + regex = SetRegexpFromAny(interp, argv[i], regcomp_flags); + if (!regex) { + return JIM_ERR; + } + + pattern = Jim_String(argv[i]); + source_str = Jim_GetString(argv[i + 1], &source_len); + + num_vars = argc - i - 2; + + if (opt_inline) { + if (num_vars) { + Jim_SetResultString(interp, "regexp match variables not allowed when using -inline", + -1); + result = JIM_ERR; + goto done; + } + num_vars = regex->re_nsub + 1; + } + + pmatch = Jim_Alloc((num_vars + 1) * sizeof(*pmatch)); + + if (offset) { + if (offset < 0) { + offset += source_len + 1; + } + if (offset > source_len) { + source_str += source_len; + } + else if (offset > 0) { + source_str += utf8_index(source_str, offset); + } + eflags |= REG_NOTBOL; + } + + if (opt_inline) { + resultListObj = Jim_NewListObj(interp, NULL, 0); + } + + next_match: + match = jim_regexec(regex, source_str, num_vars + 1, pmatch, eflags); + if (match >= REG_BADPAT) { + char buf[100]; + + jim_regerror(match, regex, buf, sizeof(buf)); + Jim_SetResultFormatted(interp, "error while matching pattern: %s", buf); + result = JIM_ERR; + goto done; + } + + if (match == REG_NOMATCH) { + goto done; + } + + num_matches++; + + if (opt_all && !opt_inline) { + + goto try_next_match; + } + + + j = 0; + for (i += 2; opt_inline ? j < num_vars : i < argc; i++, j++) { + Jim_Obj *resultObj; + + if (opt_indices) { + resultObj = Jim_NewListObj(interp, NULL, 0); + } + else { + resultObj = Jim_NewStringObj(interp, "", 0); + } + + if (pmatch[j].rm_so == -1) { + if (opt_indices) { + Jim_ListAppendElement(interp, resultObj, Jim_NewIntObj(interp, -1)); + Jim_ListAppendElement(interp, resultObj, Jim_NewIntObj(interp, -1)); + } + } + else { + if (opt_indices) { + + int so = utf8_strlen(source_str, pmatch[j].rm_so); + int eo = utf8_strlen(source_str, pmatch[j].rm_eo); + Jim_ListAppendElement(interp, resultObj, Jim_NewIntObj(interp, offset + so)); + Jim_ListAppendElement(interp, resultObj, Jim_NewIntObj(interp, offset + eo - 1)); + } + else { + Jim_AppendString(interp, resultObj, source_str + pmatch[j].rm_so, pmatch[j].rm_eo - pmatch[j].rm_so); + } + } + + if (opt_inline) { + Jim_ListAppendElement(interp, resultListObj, resultObj); + } + else { + + result = Jim_SetVariable(interp, argv[i], resultObj); + + if (result != JIM_OK) { + Jim_FreeObj(interp, resultObj); + break; + } + } + } + + try_next_match: + if (opt_all && (pattern[0] != '^' || (regcomp_flags & REG_NEWLINE)) && *source_str) { + if (pmatch[0].rm_eo) { + offset += utf8_strlen(source_str, pmatch[0].rm_eo); + source_str += pmatch[0].rm_eo; + } + else { + source_str++; + offset++; + } + if (*source_str) { + eflags = REG_NOTBOL; + goto next_match; + } + } + + done: + if (result == JIM_OK) { + if (opt_inline) { + Jim_SetResult(interp, resultListObj); + } + else { + Jim_SetResultInt(interp, num_matches); + } + } + + Jim_Free(pmatch); + return result; +} + +#define MAX_SUB_MATCHES 50 + +int Jim_RegsubCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + int regcomp_flags = 0; + int regexec_flags = 0; + int opt_all = 0; + int opt_command = 0; + int offset = 0; + regex_t *regex; + const char *p; + int result = JIM_OK; + regmatch_t pmatch[MAX_SUB_MATCHES + 1]; + int num_matches = 0; + + int i, j, n; + Jim_Obj *varname; + Jim_Obj *resultObj; + Jim_Obj *cmd_prefix = NULL; + Jim_Obj *regcomp_obj = NULL; + const char *source_str; + int source_len; + const char *replace_str = NULL; + int replace_len; + const char *pattern; + int option; + enum { + OPT_NOCASE, OPT_LINE, OPT_ALL, OPT_START, OPT_COMMAND, OPT_END + }; + static const char * const options[] = { + "-nocase", "-line", "-all", "-start", "-command", "--", NULL + }; + + if (argc < 4) { + wrongNumArgs: + Jim_WrongNumArgs(interp, 1, argv, + "?-switch ...? exp string subSpec ?varName?"); + return JIM_ERR; + } + + for (i = 1; i < argc; i++) { + const char *opt = Jim_String(argv[i]); + + if (*opt != '-') { + break; + } + if (Jim_GetEnum(interp, argv[i], options, &option, "switch", JIM_ERRMSG | JIM_ENUM_ABBREV) != JIM_OK) { + return JIM_ERR; + } + if (option == OPT_END) { + i++; + break; + } + switch (option) { + case OPT_NOCASE: + regcomp_flags |= REG_ICASE; + break; + + case OPT_LINE: + regcomp_flags |= REG_NEWLINE; + break; + + case OPT_ALL: + opt_all = 1; + break; + + case OPT_START: + if (++i == argc) { + goto wrongNumArgs; + } + if (Jim_GetIndex(interp, argv[i], &offset) != JIM_OK) { + return JIM_ERR; + } + break; + + case OPT_COMMAND: + opt_command = 1; + break; + } + } + if (argc - i != 3 && argc - i != 4) { + goto wrongNumArgs; + } + + + regcomp_obj = Jim_DuplicateObj(interp, argv[i]); + Jim_IncrRefCount(regcomp_obj); + regex = SetRegexpFromAny(interp, regcomp_obj, regcomp_flags); + if (!regex) { + Jim_DecrRefCount(interp, regcomp_obj); + return JIM_ERR; + } + pattern = Jim_String(argv[i]); + + source_str = Jim_GetString(argv[i + 1], &source_len); + if (opt_command) { + cmd_prefix = argv[i + 2]; + if (Jim_ListLength(interp, cmd_prefix) == 0) { + Jim_SetResultString(interp, "command prefix must be a list of at least one element", -1); + Jim_DecrRefCount(interp, regcomp_obj); + return JIM_ERR; + } + Jim_IncrRefCount(cmd_prefix); + } + else { + replace_str = Jim_GetString(argv[i + 2], &replace_len); + } + varname = argv[i + 3]; + + + resultObj = Jim_NewStringObj(interp, "", 0); + + if (offset) { + if (offset < 0) { + offset += source_len + 1; + } + if (offset > source_len) { + offset = source_len; + } + else if (offset < 0) { + offset = 0; + } + } + + offset = utf8_index(source_str, offset); + + + Jim_AppendString(interp, resultObj, source_str, offset); + + + n = source_len - offset; + p = source_str + offset; + do { + int match = jim_regexec(regex, p, MAX_SUB_MATCHES, pmatch, regexec_flags); + + if (match >= REG_BADPAT) { + char buf[100]; + + jim_regerror(match, regex, buf, sizeof(buf)); + Jim_SetResultFormatted(interp, "error while matching pattern: %s", buf); + return JIM_ERR; + } + if (match == REG_NOMATCH) { + break; + } + + num_matches++; + + Jim_AppendString(interp, resultObj, p, pmatch[0].rm_so); + + if (opt_command) { + + Jim_Obj *cmdListObj = Jim_DuplicateObj(interp, cmd_prefix); + for (j = 0; j < MAX_SUB_MATCHES; j++) { + if (pmatch[j].rm_so == -1) { + break; + } + else { + Jim_Obj *srcObj = Jim_NewStringObj(interp, p + pmatch[j].rm_so, pmatch[j].rm_eo - pmatch[j].rm_so); + Jim_ListAppendElement(interp, cmdListObj, srcObj); + } + } + Jim_IncrRefCount(cmdListObj); + + result = Jim_EvalObj(interp, cmdListObj); + Jim_DecrRefCount(interp, cmdListObj); + if (result != JIM_OK) { + goto cmd_error; + } + Jim_AppendString(interp, resultObj, Jim_String(Jim_GetResult(interp)), -1); + } + else { + + for (j = 0; j < replace_len; j++) { + int idx; + int c = replace_str[j]; + + if (c == '&') { + idx = 0; + } + else if (c == '\\' && j < replace_len) { + c = replace_str[++j]; + if ((c >= '0') && (c <= '9')) { + idx = c - '0'; + } + else if ((c == '\\') || (c == '&')) { + Jim_AppendString(interp, resultObj, replace_str + j, 1); + continue; + } + else { + Jim_AppendString(interp, resultObj, replace_str + j - 1, (j == replace_len) ? 1 : 2); + continue; + } + } + else { + Jim_AppendString(interp, resultObj, replace_str + j, 1); + continue; + } + if ((idx < MAX_SUB_MATCHES) && pmatch[idx].rm_so != -1 && pmatch[idx].rm_eo != -1) { + Jim_AppendString(interp, resultObj, p + pmatch[idx].rm_so, + pmatch[idx].rm_eo - pmatch[idx].rm_so); + } + } + } + + p += pmatch[0].rm_eo; + n -= pmatch[0].rm_eo; + + + if (!opt_all || n == 0) { + break; + } + + + if ((regcomp_flags & REG_NEWLINE) == 0 && pattern[0] == '^') { + break; + } + + + if (pattern[0] == '\0' && n) { + + Jim_AppendString(interp, resultObj, p, 1); + p++; + n--; + } + + if (pmatch[0].rm_eo == pmatch[0].rm_so) { + + regexec_flags = REG_NOTBOL; + } + else { + regexec_flags = 0; + } + + } while (n); + + Jim_AppendString(interp, resultObj, p, -1); + +cmd_error: + if (result == JIM_OK) { + + if (argc - i == 4) { + result = Jim_SetVariable(interp, varname, resultObj); + + if (result == JIM_OK) { + Jim_SetResultInt(interp, num_matches); + } + else { + Jim_FreeObj(interp, resultObj); + } + } + else { + Jim_SetResult(interp, resultObj); + result = JIM_OK; + } + } + else { + Jim_FreeObj(interp, resultObj); + } + + if (opt_command) { + Jim_DecrRefCount(interp, cmd_prefix); + } + + Jim_DecrRefCount(interp, regcomp_obj); + + return result; +} + +int Jim_regexpInit(Jim_Interp *interp) +{ + Jim_PackageProvideCheck(interp, "regexp"); + Jim_CreateCommand(interp, "regexp", Jim_RegexpCmd, NULL, NULL); + Jim_CreateCommand(interp, "regsub", Jim_RegsubCmd, NULL, NULL); + return JIM_OK; +} + +#include +#include +#include +#include +#include + + +#ifdef HAVE_UTIMES +#include +#endif +#ifdef HAVE_UNISTD_H +#include +#elif defined(_MSC_VER) +#include +#define F_OK 0 +#define W_OK 2 +#define R_OK 4 +#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG) +#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) +#endif + +#if defined(__MINGW32__) || defined(__MSYS__) || defined(_MSC_VER) +#define ISWINDOWS 1 + +#undef HAVE_SYMLINK +#else +#define ISWINDOWS 0 +#endif + + +#if defined(HAVE_STRUCT_STAT_ST_MTIMESPEC) + #define STAT_MTIME_US(STAT) ((STAT).st_mtimespec.tv_sec * 1000000ll + (STAT).st_mtimespec.tv_nsec / 1000) +#elif defined(HAVE_STRUCT_STAT_ST_MTIM) + #define STAT_MTIME_US(STAT) ((STAT).st_mtim.tv_sec * 1000000ll + (STAT).st_mtim.tv_nsec / 1000) +#endif + + +static void JimFixPath(char *path) +{ + if (ISWINDOWS) { + + char *p = path; + while ((p = strchr(p, '\\')) != NULL) { + *p++ = '/'; + } + } +} + + +static const char *JimGetFileType(int mode) +{ + if (S_ISREG(mode)) { + return "file"; + } + else if (S_ISDIR(mode)) { + return "directory"; + } +#ifdef S_ISCHR + else if (S_ISCHR(mode)) { + return "characterSpecial"; + } +#endif +#ifdef S_ISBLK + else if (S_ISBLK(mode)) { + return "blockSpecial"; + } +#endif +#ifdef S_ISFIFO + else if (S_ISFIFO(mode)) { + return "fifo"; + } +#endif +#ifdef S_ISLNK + else if (S_ISLNK(mode)) { + return "link"; + } +#endif +#ifdef S_ISSOCK + else if (S_ISSOCK(mode)) { + return "socket"; + } +#endif + return "unknown"; +} + +static void AppendStatElement(Jim_Interp *interp, Jim_Obj *listObj, const char *key, jim_wide value) +{ + Jim_ListAppendElement(interp, listObj, Jim_NewStringObj(interp, key, -1)); + Jim_ListAppendElement(interp, listObj, Jim_NewIntObj(interp, value)); +} + +int Jim_FileStoreStatData(Jim_Interp *interp, Jim_Obj *varName, const jim_stat_t *sb) +{ + + Jim_Obj *listObj = Jim_NewListObj(interp, NULL, 0); + + AppendStatElement(interp, listObj, "dev", sb->st_dev); + AppendStatElement(interp, listObj, "ino", sb->st_ino); + AppendStatElement(interp, listObj, "mode", sb->st_mode); + AppendStatElement(interp, listObj, "nlink", sb->st_nlink); + AppendStatElement(interp, listObj, "uid", sb->st_uid); + AppendStatElement(interp, listObj, "gid", sb->st_gid); + AppendStatElement(interp, listObj, "size", sb->st_size); + AppendStatElement(interp, listObj, "atime", sb->st_atime); + AppendStatElement(interp, listObj, "mtime", sb->st_mtime); + AppendStatElement(interp, listObj, "ctime", sb->st_ctime); +#ifdef STAT_MTIME_US + AppendStatElement(interp, listObj, "mtimeus", STAT_MTIME_US(*sb)); +#endif + Jim_ListAppendElement(interp, listObj, Jim_NewStringObj(interp, "type", -1)); + Jim_ListAppendElement(interp, listObj, Jim_NewStringObj(interp, JimGetFileType((int)sb->st_mode), -1)); + + + if (varName) { + Jim_Obj *objPtr; + objPtr = Jim_GetVariable(interp, varName, JIM_NONE); + + if (objPtr) { + Jim_Obj *objv[2]; + + objv[0] = objPtr; + objv[1] = listObj; + + objPtr = Jim_DictMerge(interp, 2, objv); + if (objPtr == NULL) { + + Jim_SetResultFormatted(interp, "can't set \"%#s(dev)\": variable isn't array", varName); + Jim_FreeNewObj(interp, listObj); + return JIM_ERR; + } + + Jim_InvalidateStringRep(objPtr); + + Jim_FreeNewObj(interp, listObj); + listObj = objPtr; + } + Jim_SetVariable(interp, varName, listObj); + } + + + Jim_SetResult(interp, listObj); + + return JIM_OK; +} + +static int JimPathLenNoTrailingSlashes(const char *path, int len) +{ + int i; + for (i = len; i > 1 && path[i - 1] == '/'; i--) { + + if (ISWINDOWS && path[i - 2] == ':') { + + break; + } + } + return i; +} + +static Jim_Obj *JimStripTrailingSlashes(Jim_Interp *interp, Jim_Obj *objPtr) +{ + int len = Jim_Length(objPtr); + const char *path = Jim_String(objPtr); + int i = JimPathLenNoTrailingSlashes(path, len); + if (i != len) { + objPtr = Jim_NewStringObj(interp, path, i); + } + Jim_IncrRefCount(objPtr); + return objPtr; +} + +static int file_cmd_dirname(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + Jim_Obj *objPtr = JimStripTrailingSlashes(interp, argv[0]); + const char *path = Jim_String(objPtr); + const char *p = strrchr(path, '/'); + + if (!p) { + Jim_SetResultString(interp, ".", -1); + } + else if (p[1] == 0) { + + Jim_SetResult(interp, objPtr); + } + else if (p == path) { + Jim_SetResultString(interp, "/", -1); + } + else if (ISWINDOWS && p[-1] == ':') { + + Jim_SetResultString(interp, path, p - path + 1); + } + else { + + int len = JimPathLenNoTrailingSlashes(path, p - path); + Jim_SetResultString(interp, path, len); + } + Jim_DecrRefCount(interp, objPtr); + return JIM_OK; +} + +static int file_cmd_split(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + Jim_Obj *listObj = Jim_NewListObj(interp, NULL, 0); + const char *path = Jim_String(argv[0]); + + if (*path == '/') { + Jim_ListAppendElement(interp, listObj, Jim_NewStringObj(interp, "/", 1)); + } + + while (1) { + + while (*path == '/') { + path++; + } + if (*path) { + const char *pt = strchr(path, '/'); + if (pt) { + Jim_ListAppendElement(interp, listObj, Jim_NewStringObj(interp, path, pt - path)); + path = pt; + continue; + } + Jim_ListAppendElement(interp, listObj, Jim_NewStringObj(interp, path, -1)); + } + break; + } + Jim_SetResult(interp, listObj); + return JIM_OK; +} + +static int file_cmd_rootname(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + const char *path = Jim_String(argv[0]); + const char *lastSlash = strrchr(path, '/'); + const char *p = strrchr(path, '.'); + + if (p == NULL || (lastSlash != NULL && lastSlash > p)) { + Jim_SetResult(interp, argv[0]); + } + else { + Jim_SetResultString(interp, path, p - path); + } + return JIM_OK; +} + +static int file_cmd_extension(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + Jim_Obj *objPtr = JimStripTrailingSlashes(interp, argv[0]); + const char *path = Jim_String(objPtr); + const char *lastSlash = strrchr(path, '/'); + const char *p = strrchr(path, '.'); + + if (p == NULL || (lastSlash != NULL && lastSlash >= p)) { + p = ""; + } + Jim_SetResultString(interp, p, -1); + Jim_DecrRefCount(interp, objPtr); + return JIM_OK; +} + +static int file_cmd_tail(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + Jim_Obj *objPtr = JimStripTrailingSlashes(interp, argv[0]); + const char *path = Jim_String(objPtr); + const char *lastSlash = strrchr(path, '/'); + + if (lastSlash) { + Jim_SetResultString(interp, lastSlash + 1, -1); + } + else { + Jim_SetResult(interp, objPtr); + } + Jim_DecrRefCount(interp, objPtr); + return JIM_OK; +} + +#ifndef HAVE_RESTRICT +#define restrict +#endif + +static char *JimRealPath(const char *restrict path, char *restrict resolved_path, size_t len) +{ +#if defined(HAVE__FULLPATH) + return _fullpath(resolved_path, path, len); +#elif defined(HAVE_REALPATH) + return realpath(path, resolved_path); +#else + return NULL; +#endif +} + +static int file_cmd_normalize(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + const char *path = Jim_String(argv[0]); + char *newname = Jim_Alloc(MAXPATHLEN); + + if (JimRealPath(path, newname, MAXPATHLEN)) { + JimFixPath(newname); + Jim_SetResult(interp, Jim_NewStringObjNoAlloc(interp, newname, -1)); + return JIM_OK; + } + Jim_Free(newname); + Jim_SetResultFormatted(interp, "can't normalize \"%#s\": %s", argv[0], strerror(errno)); + return JIM_ERR; +} + +static int file_cmd_join(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + int i; + char *newname = Jim_Alloc(MAXPATHLEN + 1); + char *last = newname; + + *newname = 0; + + + for (i = 0; i < argc; i++) { + int len; + const char *part = Jim_GetString(argv[i], &len); + + if (*part == '/') { + + last = newname; + } + else if (ISWINDOWS && strchr(part, ':')) { + + last = newname; + } + else if (part[0] == '.') { + if (part[1] == '/') { + part += 2; + len -= 2; + } + else if (part[1] == 0 && last != newname) { + + continue; + } + } + + + if (last != newname && last[-1] != '/') { + *last++ = '/'; + } + + if (len) { + if (last + len - newname >= MAXPATHLEN) { + Jim_Free(newname); + Jim_SetResultString(interp, "Path too long", -1); + return JIM_ERR; + } + memcpy(last, part, len); + last += len; + } + + + if (last > newname + 1 && last[-1] == '/') { + + if (!ISWINDOWS || !(last > newname + 2 && last[-2] == ':')) { + *--last = 0; + } + } + } + + *last = 0; + + + + Jim_SetResult(interp, Jim_NewStringObjNoAlloc(interp, newname, last - newname)); + + return JIM_OK; +} + +static int file_access(Jim_Interp *interp, Jim_Obj *filename, int mode) +{ + Jim_SetResultBool(interp, access(Jim_String(filename), mode) != -1); + + return JIM_OK; +} + +static int file_cmd_readable(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + return file_access(interp, argv[0], R_OK); +} + +static int file_cmd_writable(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + return file_access(interp, argv[0], W_OK); +} + +static int file_cmd_executable(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ +#ifdef X_OK + return file_access(interp, argv[0], X_OK); +#else + + Jim_SetResultBool(interp, 1); + return JIM_OK; +#endif +} + +static int file_cmd_exists(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + return file_access(interp, argv[0], F_OK); +} + +static int file_cmd_delete(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + int force = Jim_CompareStringImmediate(interp, argv[0], "-force"); + + if (force || Jim_CompareStringImmediate(interp, argv[0], "--")) { + argc--; + argv++; + } + + while (argc--) { + const char *path = Jim_String(argv[0]); + + if (unlink(path) == -1 && errno != ENOENT) { + if (rmdir(path) == -1) { + + if (!force || Jim_EvalPrefix(interp, "file delete force", 1, argv) != JIM_OK) { + Jim_SetResultFormatted(interp, "couldn't delete file \"%s\": %s", path, + strerror(errno)); + return JIM_ERR; + } + } + } + argv++; + } + return JIM_OK; +} + +#ifdef HAVE_MKDIR_ONE_ARG +#define MKDIR_DEFAULT(PATHNAME) mkdir(PATHNAME) +#else +#define MKDIR_DEFAULT(PATHNAME) mkdir(PATHNAME, 0755) +#endif + +static int mkdir_all(char *path) +{ + int ok = 1; + + + goto first; + + while (ok--) { + + { + char *slash = strrchr(path, '/'); + + if (slash && slash != path) { + *slash = 0; + if (mkdir_all(path) != 0) { + return -1; + } + *slash = '/'; + } + } + first: + if (MKDIR_DEFAULT(path) == 0) { + return 0; + } + if (errno == ENOENT) { + + continue; + } + + if (errno == EEXIST) { + jim_stat_t sb; + + if (Jim_Stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) { + return 0; + } + + errno = EEXIST; + } + + break; + } + return -1; +} + +static int file_cmd_mkdir(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + while (argc--) { + char *path = Jim_StrDup(Jim_String(argv[0])); + int rc = mkdir_all(path); + + Jim_Free(path); + if (rc != 0) { + Jim_SetResultFormatted(interp, "can't create directory \"%#s\": %s", argv[0], + strerror(errno)); + return JIM_ERR; + } + argv++; + } + return JIM_OK; +} + +static int file_cmd_tempfile(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + int fd = Jim_MakeTempFile(interp, (argc >= 1) ? Jim_String(argv[0]) : NULL, 0); + + if (fd < 0) { + return JIM_ERR; + } + close(fd); + + return JIM_OK; +} + +static int file_cmd_rename(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + const char *source; + const char *dest; + int force = 0; + + if (argc == 3) { + if (!Jim_CompareStringImmediate(interp, argv[0], "-force")) { + return -1; + } + force++; + argv++; + argc--; + } + + source = Jim_String(argv[0]); + dest = Jim_String(argv[1]); + + if (!force && access(dest, F_OK) == 0) { + Jim_SetResultFormatted(interp, "error renaming \"%#s\" to \"%#s\": target exists", argv[0], + argv[1]); + return JIM_ERR; + } +#if ISWINDOWS + if (access(dest, F_OK) == 0) { + + remove(dest); + } +#endif + if (rename(source, dest) != 0) { + Jim_SetResultFormatted(interp, "error renaming \"%#s\" to \"%#s\": %s", argv[0], argv[1], + strerror(errno)); + return JIM_ERR; + } + + return JIM_OK; +} + +#if defined(HAVE_LINK) && defined(HAVE_SYMLINK) +static int file_cmd_link(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + int ret; + const char *source; + const char *dest; + static const char * const options[] = { "-hard", "-symbolic", NULL }; + enum { OPT_HARD, OPT_SYMBOLIC, }; + int option = OPT_HARD; + + if (argc == 3) { + if (Jim_GetEnum(interp, argv[0], options, &option, NULL, JIM_ENUM_ABBREV | JIM_ERRMSG) != JIM_OK) { + return JIM_ERR; + } + argv++; + argc--; + } + + dest = Jim_String(argv[0]); + source = Jim_String(argv[1]); + + if (option == OPT_HARD) { + ret = link(source, dest); + } + else { + ret = symlink(source, dest); + } + + if (ret != 0) { + Jim_SetResultFormatted(interp, "error linking \"%#s\" to \"%#s\": %s", argv[0], argv[1], + strerror(errno)); + return JIM_ERR; + } + + return JIM_OK; +} +#endif + +static int file_stat(Jim_Interp *interp, Jim_Obj *filename, jim_stat_t *sb) +{ + const char *path = Jim_String(filename); + + if (Jim_Stat(path, sb) == -1) { + Jim_SetResultFormatted(interp, "could not read \"%#s\": %s", filename, strerror(errno)); + return JIM_ERR; + } + return JIM_OK; +} + +#ifdef Jim_LinkStat +static int file_lstat(Jim_Interp *interp, Jim_Obj *filename, jim_stat_t *sb) +{ + const char *path = Jim_String(filename); + + if (Jim_LinkStat(path, sb) == -1) { + Jim_SetResultFormatted(interp, "could not read \"%#s\": %s", filename, strerror(errno)); + return JIM_ERR; + } + return JIM_OK; +} +#else +#define file_lstat file_stat +#endif + +static int file_cmd_atime(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + jim_stat_t sb; + + if (file_stat(interp, argv[0], &sb) != JIM_OK) { + return JIM_ERR; + } + Jim_SetResultInt(interp, sb.st_atime); + return JIM_OK; +} + +static int JimSetFileTimes(Jim_Interp *interp, const char *filename, jim_wide us) +{ +#ifdef HAVE_UTIMES + struct timeval times[2]; + + times[1].tv_sec = times[0].tv_sec = us / 1000000; + times[1].tv_usec = times[0].tv_usec = us % 1000000; + + if (utimes(filename, times) != 0) { + Jim_SetResultFormatted(interp, "can't set time on \"%s\": %s", filename, strerror(errno)); + return JIM_ERR; + } + return JIM_OK; +#else + Jim_SetResultString(interp, "Not implemented", -1); + return JIM_ERR; +#endif +} + +static int file_cmd_mtime(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + jim_stat_t sb; + + if (argc == 2) { + jim_wide secs; + if (Jim_GetWide(interp, argv[1], &secs) != JIM_OK) { + return JIM_ERR; + } + return JimSetFileTimes(interp, Jim_String(argv[0]), secs * 1000000); + } + if (file_stat(interp, argv[0], &sb) != JIM_OK) { + return JIM_ERR; + } + Jim_SetResultInt(interp, sb.st_mtime); + return JIM_OK; +} + +#ifdef STAT_MTIME_US +static int file_cmd_mtimeus(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + jim_stat_t sb; + + if (argc == 2) { + jim_wide us; + if (Jim_GetWide(interp, argv[1], &us) != JIM_OK) { + return JIM_ERR; + } + return JimSetFileTimes(interp, Jim_String(argv[0]), us); + } + if (file_stat(interp, argv[0], &sb) != JIM_OK) { + return JIM_ERR; + } + Jim_SetResultInt(interp, STAT_MTIME_US(sb)); + return JIM_OK; +} +#endif + +static int file_cmd_copy(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + return Jim_EvalPrefix(interp, "file copy", argc, argv); +} + +static int file_cmd_size(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + jim_stat_t sb; + + if (file_stat(interp, argv[0], &sb) != JIM_OK) { + return JIM_ERR; + } + Jim_SetResultInt(interp, sb.st_size); + return JIM_OK; +} + +static int file_cmd_isdirectory(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + jim_stat_t sb; + int ret = 0; + + if (file_stat(interp, argv[0], &sb) == JIM_OK) { + ret = S_ISDIR(sb.st_mode); + } + Jim_SetResultInt(interp, ret); + return JIM_OK; +} + +static int file_cmd_isfile(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + jim_stat_t sb; + int ret = 0; + + if (file_stat(interp, argv[0], &sb) == JIM_OK) { + ret = S_ISREG(sb.st_mode); + } + Jim_SetResultInt(interp, ret); + return JIM_OK; +} + +#ifdef HAVE_GETEUID +static int file_cmd_owned(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + jim_stat_t sb; + int ret = 0; + + if (file_stat(interp, argv[0], &sb) == JIM_OK) { + ret = (geteuid() == sb.st_uid); + } + Jim_SetResultInt(interp, ret); + return JIM_OK; +} +#endif + +#if defined(HAVE_READLINK) +static int file_cmd_readlink(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + const char *path = Jim_String(argv[0]); + char *linkValue = Jim_Alloc(MAXPATHLEN + 1); + + int linkLength = readlink(path, linkValue, MAXPATHLEN); + + if (linkLength == -1) { + Jim_Free(linkValue); + Jim_SetResultFormatted(interp, "could not read link \"%#s\": %s", argv[0], strerror(errno)); + return JIM_ERR; + } + linkValue[linkLength] = 0; + Jim_SetResult(interp, Jim_NewStringObjNoAlloc(interp, linkValue, linkLength)); + return JIM_OK; +} +#endif + +static int file_cmd_type(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + jim_stat_t sb; + + if (file_lstat(interp, argv[0], &sb) != JIM_OK) { + return JIM_ERR; + } + Jim_SetResultString(interp, JimGetFileType((int)sb.st_mode), -1); + return JIM_OK; +} + +#ifdef Jim_LinkStat +static int file_cmd_lstat(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + jim_stat_t sb; + + if (file_lstat(interp, argv[0], &sb) != JIM_OK) { + return JIM_ERR; + } + return Jim_FileStoreStatData(interp, argc == 2 ? argv[1] : NULL, &sb); +} +#else +#define file_cmd_lstat file_cmd_stat +#endif + +static int file_cmd_stat(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + jim_stat_t sb; + + if (file_stat(interp, argv[0], &sb) != JIM_OK) { + return JIM_ERR; + } + return Jim_FileStoreStatData(interp, argc == 2 ? argv[1] : NULL, &sb); +} + +static const jim_subcmd_type file_command_table[] = { + { "atime", + "name", + file_cmd_atime, + 1, + 1, + + }, + { "mtime", + "name ?time?", + file_cmd_mtime, + 1, + 2, + + }, +#ifdef STAT_MTIME_US + { "mtimeus", + "name ?time?", + file_cmd_mtimeus, + 1, + 2, + + }, +#endif + { "copy", + "?-force? source dest", + file_cmd_copy, + 2, + 3, + + }, + { "dirname", + "name", + file_cmd_dirname, + 1, + 1, + + }, + { "rootname", + "name", + file_cmd_rootname, + 1, + 1, + + }, + { "extension", + "name", + file_cmd_extension, + 1, + 1, + + }, + { "tail", + "name", + file_cmd_tail, + 1, + 1, + + }, + { "split", + "name", + file_cmd_split, + 1, + 1, + + }, + { "normalize", + "name", + file_cmd_normalize, + 1, + 1, + + }, + { "join", + "name ?name ...?", + file_cmd_join, + 1, + -1, + + }, + { "readable", + "name", + file_cmd_readable, + 1, + 1, + + }, + { "writable", + "name", + file_cmd_writable, + 1, + 1, + + }, + { "executable", + "name", + file_cmd_executable, + 1, + 1, + + }, + { "exists", + "name", + file_cmd_exists, + 1, + 1, + + }, + { "delete", + "?-force|--? name ...", + file_cmd_delete, + 1, + -1, + + }, + { "mkdir", + "dir ...", + file_cmd_mkdir, + 1, + -1, + + }, + { "tempfile", + "?template?", + file_cmd_tempfile, + 0, + 1, + + }, + { "rename", + "?-force? source dest", + file_cmd_rename, + 2, + 3, + + }, +#if defined(HAVE_LINK) && defined(HAVE_SYMLINK) + { "link", + "?-symbolic|-hard? newname target", + file_cmd_link, + 2, + 3, + + }, +#endif +#if defined(HAVE_READLINK) + { "readlink", + "name", + file_cmd_readlink, + 1, + 1, + + }, +#endif + { "size", + "name", + file_cmd_size, + 1, + 1, + + }, + { "stat", + "name ?var?", + file_cmd_stat, + 1, + 2, + + }, + { "lstat", + "name ?var?", + file_cmd_lstat, + 1, + 2, + + }, + { "type", + "name", + file_cmd_type, + 1, + 1, + + }, +#ifdef HAVE_GETEUID + { "owned", + "name", + file_cmd_owned, + 1, + 1, + + }, +#endif + { "isdirectory", + "name", + file_cmd_isdirectory, + 1, + 1, + + }, + { "isfile", + "name", + file_cmd_isfile, + 1, + 1, + + }, + { + NULL + } +}; + +static int Jim_CdCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + const char *path; + + if (argc != 2) { + Jim_WrongNumArgs(interp, 1, argv, "dirname"); + return JIM_ERR; + } + + path = Jim_String(argv[1]); + + if (chdir(path) != 0) { + Jim_SetResultFormatted(interp, "couldn't change working directory to \"%s\": %s", path, + strerror(errno)); + return JIM_ERR; + } + return JIM_OK; +} + +static int Jim_PwdCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + char *cwd = Jim_Alloc(MAXPATHLEN); + + if (getcwd(cwd, MAXPATHLEN) == NULL) { + Jim_SetResultString(interp, "Failed to get pwd", -1); + Jim_Free(cwd); + return JIM_ERR; + } + JimFixPath(cwd); + Jim_SetResultString(interp, cwd, -1); + + Jim_Free(cwd); + return JIM_OK; +} + +int Jim_fileInit(Jim_Interp *interp) +{ + Jim_PackageProvideCheck(interp, "file"); + Jim_CreateCommand(interp, "file", Jim_SubCmdProc, (void *)file_command_table, NULL); + Jim_CreateCommand(interp, "pwd", Jim_PwdCmd, NULL, NULL); + Jim_CreateCommand(interp, "cd", Jim_CdCmd, NULL, NULL); + return JIM_OK; +} + +#include +#include + + +#if (!(defined(HAVE_VFORK) || defined(HAVE_FORK)) || !defined(HAVE_WAITPID)) && !defined(__MINGW32__) +static int Jim_ExecCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + Jim_Obj *cmdlineObj = Jim_NewEmptyStringObj(interp); + int i, j; + int rc; + + + for (i = 1; i < argc; i++) { + int len; + const char *arg = Jim_GetString(argv[i], &len); + + if (i > 1) { + Jim_AppendString(interp, cmdlineObj, " ", 1); + } + if (strpbrk(arg, "\\\" ") == NULL) { + + Jim_AppendString(interp, cmdlineObj, arg, len); + continue; + } + + Jim_AppendString(interp, cmdlineObj, "\"", 1); + for (j = 0; j < len; j++) { + if (arg[j] == '\\' || arg[j] == '"') { + Jim_AppendString(interp, cmdlineObj, "\\", 1); + } + Jim_AppendString(interp, cmdlineObj, &arg[j], 1); + } + Jim_AppendString(interp, cmdlineObj, "\"", 1); + } + rc = system(Jim_String(cmdlineObj)); + + Jim_FreeNewObj(interp, cmdlineObj); + + if (rc) { + Jim_Obj *errorCode = Jim_NewListObj(interp, NULL, 0); + Jim_ListAppendElement(interp, errorCode, Jim_NewStringObj(interp, "CHILDSTATUS", -1)); + Jim_ListAppendElement(interp, errorCode, Jim_NewIntObj(interp, 0)); + Jim_ListAppendElement(interp, errorCode, Jim_NewIntObj(interp, rc)); + Jim_SetGlobalVariableStr(interp, "errorCode", errorCode); + return JIM_ERR; + } + + return JIM_OK; +} + +int Jim_execInit(Jim_Interp *interp) +{ + Jim_PackageProvideCheck(interp, "exec"); + Jim_CreateCommand(interp, "exec", Jim_ExecCmd, NULL, NULL); + return JIM_OK; +} +#else + + +#include +#include +#include + +struct WaitInfoTable; + +static char **JimOriginalEnviron(void); +static char **JimSaveEnv(char **env); +static void JimRestoreEnv(char **env); +static int JimCreatePipeline(Jim_Interp *interp, int argc, Jim_Obj *const *argv, + phandle_t **pidArrayPtr, int *inPipePtr, int *outPipePtr, int *errFilePtr); +static void JimDetachPids(struct WaitInfoTable *table, int numPids, const phandle_t *pidPtr); +static int JimCleanupChildren(Jim_Interp *interp, int numPids, phandle_t *pidPtr, Jim_Obj *errStrObj); +static int Jim_WaitCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv); + +#if defined(__MINGW32__) +static phandle_t JimStartWinProcess(Jim_Interp *interp, char **argv, char **env, int inputId, int outputId, int errorId); +#endif + +static void Jim_RemoveTrailingNewline(Jim_Obj *objPtr) +{ + int len; + const char *s = Jim_GetString(objPtr, &len); + + if (len > 0 && s[len - 1] == '\n') { + objPtr->length--; + objPtr->bytes[objPtr->length] = '\0'; + } +} + +static int JimAppendStreamToString(Jim_Interp *interp, int fd, Jim_Obj *strObj) +{ + char buf[256]; + int ret = 0; + + while (1) { + int retval = read(fd, buf, sizeof(buf)); + if (retval > 0) { + ret = 1; + Jim_AppendString(interp, strObj, buf, retval); + } + if (retval <= 0) { + break; + } + } + close(fd); + return ret; +} + +static char **JimBuildEnv(Jim_Interp *interp) +{ + int i; + int size; + int num; + int n; + char **envptr; + char *envdata; + + Jim_Obj *objPtr = Jim_GetGlobalVariableStr(interp, "env", JIM_NONE); + + if (!objPtr) { + return JimOriginalEnviron(); + } + + + + num = Jim_ListLength(interp, objPtr); + if (num % 2) { + + num--; + } + size = Jim_Length(objPtr) + 2; + + envptr = Jim_Alloc(sizeof(*envptr) * (num / 2 + 1) + size); + envdata = (char *)&envptr[num / 2 + 1]; + + n = 0; + for (i = 0; i < num; i += 2) { + const char *s1, *s2; + Jim_Obj *elemObj; + + Jim_ListIndex(interp, objPtr, i, &elemObj, JIM_NONE); + s1 = Jim_String(elemObj); + Jim_ListIndex(interp, objPtr, i + 1, &elemObj, JIM_NONE); + s2 = Jim_String(elemObj); + + envptr[n] = envdata; + envdata += sprintf(envdata, "%s=%s", s1, s2); + envdata++; + n++; + } + envptr[n] = NULL; + *envdata = 0; + + return envptr; +} + +static void JimFreeEnv(char **env, char **original_environ) +{ + if (env != original_environ) { + Jim_Free(env); + } +} + +static Jim_Obj *JimMakeErrorCode(Jim_Interp *interp, long pid, int waitStatus, Jim_Obj *errStrObj) +{ + Jim_Obj *errorCode = Jim_NewListObj(interp, NULL, 0); + + if (pid <= 0) { + Jim_ListAppendElement(interp, errorCode, Jim_NewStringObj(interp, "NONE", -1)); + Jim_ListAppendElement(interp, errorCode, Jim_NewIntObj(interp, pid)); + Jim_ListAppendElement(interp, errorCode, Jim_NewIntObj(interp, -1)); + } + else if (WIFEXITED(waitStatus)) { + Jim_ListAppendElement(interp, errorCode, Jim_NewStringObj(interp, "CHILDSTATUS", -1)); + Jim_ListAppendElement(interp, errorCode, Jim_NewIntObj(interp, pid)); + Jim_ListAppendElement(interp, errorCode, Jim_NewIntObj(interp, WEXITSTATUS(waitStatus))); + } + else { + const char *type; + const char *action; + const char *signame; + + if (WIFSIGNALED(waitStatus)) { + type = "CHILDKILLED"; + action = "killed"; + signame = Jim_SignalId(WTERMSIG(waitStatus)); + } + else { + type = "CHILDSUSP"; + action = "suspended"; + signame = "none"; + } + + Jim_ListAppendElement(interp, errorCode, Jim_NewStringObj(interp, type, -1)); + + if (errStrObj) { + Jim_AppendStrings(interp, errStrObj, "child ", action, " by signal ", Jim_SignalId(WTERMSIG(waitStatus)), "\n", NULL); + } + + Jim_ListAppendElement(interp, errorCode, Jim_NewIntObj(interp, pid)); + Jim_ListAppendElement(interp, errorCode, Jim_NewStringObj(interp, signame, -1)); + } + return errorCode; +} + +static int JimCheckWaitStatus(Jim_Interp *interp, long pid, int waitStatus, Jim_Obj *errStrObj) +{ + if (WIFEXITED(waitStatus) && WEXITSTATUS(waitStatus) == 0) { + return JIM_OK; + } + Jim_SetGlobalVariableStr(interp, "errorCode", JimMakeErrorCode(interp, pid, waitStatus, errStrObj)); + + return JIM_ERR; +} + + +struct WaitInfo +{ + phandle_t phandle; + int status; + int flags; +}; + + +struct WaitInfoTable { + struct WaitInfo *info; + int size; + int used; + int refcount; +}; + + +#define WI_DETACHED 2 + +#define WAIT_TABLE_GROW_BY 4 + +static void JimFreeWaitInfoTable(struct Jim_Interp *interp, void *privData) +{ + struct WaitInfoTable *table = privData; + + if (--table->refcount == 0) { + Jim_Free(table->info); + Jim_Free(table); + } +} + +static struct WaitInfoTable *JimAllocWaitInfoTable(void) +{ + struct WaitInfoTable *table = Jim_Alloc(sizeof(*table)); + table->info = NULL; + table->size = table->used = 0; + table->refcount = 1; + + return table; +} + +static int JimWaitRemove(struct WaitInfoTable *table, phandle_t phandle) +{ + int i; + + + for (i = 0; i < table->used; i++) { + if (phandle == table->info[i].phandle) { + if (i != table->used - 1) { + table->info[i] = table->info[table->used - 1]; + } + table->used--; + return 0; + } + } + return -1; +} + +static int Jim_ExecCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + int outputId; + int errorId; + phandle_t *pidPtr; + int numPids, result; + int child_siginfo = 1; + Jim_Obj *childErrObj; + Jim_Obj *errStrObj; + struct WaitInfoTable *table = Jim_CmdPrivData(interp); + + if (argc > 1 && Jim_CompareStringImmediate(interp, argv[argc - 1], "&")) { + Jim_Obj *listObj; + int i; + + argc--; + numPids = JimCreatePipeline(interp, argc - 1, argv + 1, &pidPtr, NULL, NULL, NULL); + if (numPids < 0) { + return JIM_ERR; + } + + listObj = Jim_NewListObj(interp, NULL, 0); + for (i = 0; i < numPids; i++) { + Jim_ListAppendElement(interp, listObj, Jim_NewIntObj(interp, JimProcessPid(pidPtr[i]))); + } + Jim_SetResult(interp, listObj); + JimDetachPids(table, numPids, pidPtr); + Jim_Free(pidPtr); + return JIM_OK; + } + + numPids = + JimCreatePipeline(interp, argc - 1, argv + 1, &pidPtr, NULL, &outputId, &errorId); + + if (numPids < 0) { + return JIM_ERR; + } + + result = JIM_OK; + + errStrObj = Jim_NewStringObj(interp, "", 0); + + + if (outputId != -1) { + if (JimAppendStreamToString(interp, outputId, errStrObj) < 0) { + result = JIM_ERR; + Jim_SetResultErrno(interp, "error reading from output pipe"); + } + } + + + childErrObj = Jim_NewStringObj(interp, "", 0); + Jim_IncrRefCount(childErrObj); + + if (JimCleanupChildren(interp, numPids, pidPtr, childErrObj) != JIM_OK) { + result = JIM_ERR; + } + + if (errorId != -1) { + int ret; + Jim_Lseek(errorId, 0, SEEK_SET); + ret = JimAppendStreamToString(interp, errorId, errStrObj); + if (ret < 0) { + Jim_SetResultErrno(interp, "error reading from error pipe"); + result = JIM_ERR; + } + else if (ret > 0) { + + child_siginfo = 0; + } + } + + if (child_siginfo) { + + Jim_AppendObj(interp, errStrObj, childErrObj); + } + Jim_DecrRefCount(interp, childErrObj); + + + Jim_RemoveTrailingNewline(errStrObj); + + + Jim_SetResult(interp, errStrObj); + + return result; +} + +static long JimWaitForProcess(struct WaitInfoTable *table, phandle_t phandle, int *statusPtr) +{ + if (JimWaitRemove(table, phandle) == 0) { + + return waitpid(phandle, statusPtr, 0); + } + + + return -1; +} + +static void JimDetachPids(struct WaitInfoTable *table, int numPids, const phandle_t *pidPtr) +{ + int j; + + for (j = 0; j < numPids; j++) { + + int i; + for (i = 0; i < table->used; i++) { + if (pidPtr[j] == table->info[i].phandle) { + table->info[i].flags |= WI_DETACHED; + break; + } + } + } +} + +static int JimGetChannelFd(Jim_Interp *interp, const char *name) +{ + Jim_Obj *objv[2]; + + objv[0] = Jim_NewStringObj(interp, name, -1); + objv[1] = Jim_NewStringObj(interp, "getfd", -1); + + if (Jim_EvalObjVector(interp, 2, objv) == JIM_OK) { + jim_wide fd; + if (Jim_GetWide(interp, Jim_GetResult(interp), &fd) == JIM_OK) { + return fd; + } + } + return -1; +} + +static void JimReapDetachedPids(struct WaitInfoTable *table) +{ + struct WaitInfo *waitPtr; + int count; + int dest; + + if (!table) { + return; + } + + waitPtr = table->info; + dest = 0; + for (count = table->used; count > 0; waitPtr++, count--) { + if (waitPtr->flags & WI_DETACHED) { + int status; + long pid = waitpid(waitPtr->phandle, &status, WNOHANG); + if (pid > 0) { + + table->used--; + continue; + } + } + if (waitPtr != &table->info[dest]) { + table->info[dest] = *waitPtr; + } + dest++; + } +} + +static int Jim_WaitCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + struct WaitInfoTable *table = Jim_CmdPrivData(interp); + int nohang = 0; + long pid; + phandle_t phandle; + int status; + Jim_Obj *errCodeObj; + + + if (argc == 1) { + JimReapDetachedPids(table); + return JIM_OK; + } + + if (argc > 1 && Jim_CompareStringImmediate(interp, argv[1], "-nohang")) { + nohang = 1; + } + if (argc != nohang + 2) { + Jim_WrongNumArgs(interp, 1, argv, "?-nohang? ?pid?"); + return JIM_ERR; + } + if (Jim_GetLong(interp, argv[nohang + 1], &pid) != JIM_OK) { + return JIM_ERR; + } + + + phandle = JimWaitPid(pid, &status, nohang ? WNOHANG : 0); + if (phandle == JIM_BAD_PHANDLE) { + pid = -1; + } +#ifndef __MINGW32__ + else if (pid < 0) { + pid = phandle; + } +#endif + + errCodeObj = JimMakeErrorCode(interp, pid, status, NULL); + + if (phandle != JIM_BAD_PHANDLE && (WIFEXITED(status) || WIFSIGNALED(status))) { + + JimWaitRemove(table, phandle); + } + Jim_SetResult(interp, errCodeObj); + return JIM_OK; +} + +static int Jim_PidCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + if (argc != 1) { + Jim_WrongNumArgs(interp, 1, argv, ""); + return JIM_ERR; + } + + Jim_SetResultInt(interp, (jim_wide)getpid()); + return JIM_OK; +} + +static int +JimCreatePipeline(Jim_Interp *interp, int argc, Jim_Obj *const *argv, phandle_t **pidArrayPtr, + int *inPipePtr, int *outPipePtr, int *errFilePtr) +{ + phandle_t *pidPtr = NULL; /* Points to alloc-ed array holding all + * the pids of child processes. */ + int numPids = 0; /* Actual number of processes that exist + * at *pidPtr right now. */ + int cmdCount; /* Count of number of distinct commands + * found in argc/argv. */ + const char *input = NULL; /* Describes input for pipeline, depending + * on "inputFile". NULL means take input + * from stdin/pipe. */ + int input_len = 0; + +#define FILE_NAME 0 +#define FILE_APPEND 1 +#define FILE_HANDLE 2 +#define FILE_TEXT 3 + + int inputFile = FILE_NAME; /* 1 means input is name of input file. + * 2 means input is filehandle name. + * 0 means input holds actual + * text to be input to command. */ + + int outputFile = FILE_NAME; /* 0 means output is the name of output file. + * 1 means output is the name of output file, and append. + * 2 means output is filehandle name. + * All this is ignored if output is NULL + */ + int errorFile = FILE_NAME; /* 0 means error is the name of error file. + * 1 means error is the name of error file, and append. + * 2 means error is filehandle name. + * All this is ignored if error is NULL + */ + const char *output = NULL; /* Holds name of output file to pipe to, + * or NULL if output goes to stdout/pipe. */ + const char *error = NULL; /* Holds name of stderr file to pipe to, + * or NULL if stderr goes to stderr/pipe. */ + int inputId = -1; + int outputId = -1; + int errorId = -1; + int lastOutputId = -1; + int pipeIds[2]; + int firstArg, lastArg; /* Indexes of first and last arguments in + * current command. */ + int lastBar; + int i; + phandle_t phandle; + char **save_environ; +#if defined(HAVE_EXECVPE) && !defined(__MINGW32__) + char **child_environ; +#endif + struct WaitInfoTable *table = Jim_CmdPrivData(interp); + + + char **arg_array = Jim_Alloc(sizeof(*arg_array) * (argc + 1)); + int arg_count = 0; + + if (inPipePtr != NULL) { + *inPipePtr = -1; + } + if (outPipePtr != NULL) { + *outPipePtr = -1; + } + if (errFilePtr != NULL) { + *errFilePtr = -1; + } + pipeIds[0] = pipeIds[1] = -1; + + cmdCount = 1; + lastBar = -1; + for (i = 0; i < argc; i++) { + const char *arg = Jim_String(argv[i]); + + if (arg[0] == '<') { + inputFile = FILE_NAME; + input = arg + 1; + if (*input == '<') { + inputFile = FILE_TEXT; + input_len = Jim_Length(argv[i]) - 2; + input++; + } + else if (*input == '@') { + inputFile = FILE_HANDLE; + input++; + } + + if (!*input && ++i < argc) { + input = Jim_GetString(argv[i], &input_len); + } + } + else if (arg[0] == '>') { + int dup_error = 0; + + outputFile = FILE_NAME; + + output = arg + 1; + if (*output == '>') { + outputFile = FILE_APPEND; + output++; + } + if (*output == '&') { + + output++; + dup_error = 1; + } + if (*output == '@') { + outputFile = FILE_HANDLE; + output++; + } + if (!*output && ++i < argc) { + output = Jim_String(argv[i]); + } + if (dup_error) { + errorFile = outputFile; + error = output; + } + } + else if (arg[0] == '2' && arg[1] == '>') { + error = arg + 2; + errorFile = FILE_NAME; + + if (*error == '@') { + errorFile = FILE_HANDLE; + error++; + } + else if (*error == '>') { + errorFile = FILE_APPEND; + error++; + } + if (!*error && ++i < argc) { + error = Jim_String(argv[i]); + } + } + else { + if (strcmp(arg, "|") == 0 || strcmp(arg, "|&") == 0) { + if (i == lastBar + 1 || i == argc - 1) { + Jim_SetResultString(interp, "illegal use of | or |& in command", -1); + goto badargs; + } + lastBar = i; + cmdCount++; + } + + arg_array[arg_count++] = (char *)arg; + continue; + } + + if (i >= argc) { + Jim_SetResultFormatted(interp, "can't specify \"%s\" as last word in command", arg); + goto badargs; + } + } + + if (arg_count == 0) { + Jim_SetResultString(interp, "didn't specify command to execute", -1); +badargs: + Jim_Free(arg_array); + return -1; + } + + + save_environ = JimSaveEnv(JimBuildEnv(interp)); + + if (input != NULL) { + if (inputFile == FILE_TEXT) { + inputId = Jim_MakeTempFile(interp, NULL, 1); + if (inputId == -1) { + goto error; + } + if (write(inputId, input, input_len) != input_len) { + Jim_SetResultErrno(interp, "couldn't write temp file"); + close(inputId); + goto error; + } + Jim_Lseek(inputId, 0L, SEEK_SET); + } + else if (inputFile == FILE_HANDLE) { + int fd = JimGetChannelFd(interp, input); + + if (fd < 0) { + goto error; + } + inputId = dup(fd); + } + else { + inputId = Jim_OpenForRead(input); + if (inputId == -1) { + Jim_SetResultFormatted(interp, "couldn't read file \"%s\": %s", input, strerror(Jim_Errno())); + goto error; + } + } + } + else if (inPipePtr != NULL) { + if (pipe(pipeIds) != 0) { + Jim_SetResultErrno(interp, "couldn't create input pipe for command"); + goto error; + } + inputId = pipeIds[0]; + *inPipePtr = pipeIds[1]; + pipeIds[0] = pipeIds[1] = -1; + } + + if (output != NULL) { + if (outputFile == FILE_HANDLE) { + int fd = JimGetChannelFd(interp, output); + if (fd < 0) { + goto error; + } + lastOutputId = dup(fd); + } + else { + lastOutputId = Jim_OpenForWrite(output, outputFile == FILE_APPEND); + if (lastOutputId == -1) { + Jim_SetResultFormatted(interp, "couldn't write file \"%s\": %s", output, strerror(Jim_Errno())); + goto error; + } + } + } + else if (outPipePtr != NULL) { + if (pipe(pipeIds) != 0) { + Jim_SetResultErrno(interp, "couldn't create output pipe"); + goto error; + } + lastOutputId = pipeIds[1]; + *outPipePtr = pipeIds[0]; + pipeIds[0] = pipeIds[1] = -1; + } + + if (error != NULL) { + if (errorFile == FILE_HANDLE) { + if (strcmp(error, "1") == 0) { + + if (lastOutputId != -1) { + errorId = dup(lastOutputId); + } + else { + + error = "stdout"; + } + } + if (errorId == -1) { + int fd = JimGetChannelFd(interp, error); + if (fd < 0) { + goto error; + } + errorId = dup(fd); + } + } + else { + errorId = Jim_OpenForWrite(error, errorFile == FILE_APPEND); + if (errorId == -1) { + Jim_SetResultFormatted(interp, "couldn't write file \"%s\": %s", error, strerror(Jim_Errno())); + goto error; + } + } + } + else if (errFilePtr != NULL) { + errorId = Jim_MakeTempFile(interp, NULL, 1); + if (errorId == -1) { + goto error; + } + *errFilePtr = dup(errorId); + } + + + pidPtr = Jim_Alloc(cmdCount * sizeof(*pidPtr)); + for (firstArg = 0; firstArg < arg_count; numPids++, firstArg = lastArg + 1) { + int pipe_dup_err = 0; + int origErrorId = errorId; + + for (lastArg = firstArg; lastArg < arg_count; lastArg++) { + if (strcmp(arg_array[lastArg], "|") == 0) { + break; + } + if (strcmp(arg_array[lastArg], "|&") == 0) { + pipe_dup_err = 1; + break; + } + } + + if (lastArg == firstArg) { + Jim_SetResultString(interp, "missing command to exec", -1); + goto error; + } + + + arg_array[lastArg] = NULL; + if (lastArg == arg_count) { + outputId = lastOutputId; + lastOutputId = -1; + } + else { + if (pipe(pipeIds) != 0) { + Jim_SetResultErrno(interp, "couldn't create pipe"); + goto error; + } + outputId = pipeIds[1]; + } + + + if (pipe_dup_err) { + errorId = outputId; + } + + + +#ifdef __MINGW32__ + phandle = JimStartWinProcess(interp, &arg_array[firstArg], save_environ, inputId, outputId, errorId); + if (phandle == JIM_BAD_PHANDLE) { + Jim_SetResultFormatted(interp, "couldn't exec \"%s\"", arg_array[firstArg]); + goto error; + } +#else + i = strlen(arg_array[firstArg]); + +#ifdef HAVE_EXECVPE + child_environ = Jim_GetEnviron(); +#endif +#ifdef HAVE_VFORK + phandle = vfork(); +#else + phandle = fork(); +#endif + if (phandle < 0) { + Jim_SetResultErrno(interp, "couldn't fork child process"); + goto error; + } + if (phandle == 0) { + + + if (inputId != -1 && inputId != fileno(stdin)) { + dup2(inputId, fileno(stdin)); + close(inputId); + } + if (outputId != -1 && outputId != fileno(stdout)) { + dup2(outputId, fileno(stdout)); + if (outputId != errorId) { + close(outputId); + } + } + if (errorId != -1 && errorId != fileno(stderr)) { + dup2(errorId, fileno(stderr)); + close(errorId); + } + + if (outPipePtr && *outPipePtr != -1) { + close(*outPipePtr); + } + if (errFilePtr && *errFilePtr != -1) { + close(*errFilePtr); + } + if (pipeIds[0] != -1) { + close(pipeIds[0]); + } + if (lastOutputId != -1) { + close(lastOutputId); + } + + execvpe(arg_array[firstArg], &arg_array[firstArg], child_environ); + + if (write(fileno(stderr), "couldn't exec \"", 15) && + write(fileno(stderr), arg_array[firstArg], i) && + write(fileno(stderr), "\"\n", 2)) { + + } +#ifdef JIM_MAINTAINER + { + + static char *const false_argv[2] = {"false", NULL}; + execvp(false_argv[0],false_argv); + } +#endif + _exit(127); + } +#endif + + + + if (table->used == table->size) { + table->size += WAIT_TABLE_GROW_BY; + table->info = Jim_Realloc(table->info, table->size * sizeof(*table->info)); + } + + table->info[table->used].phandle = phandle; + table->info[table->used].flags = 0; + table->used++; + + pidPtr[numPids] = phandle; + + + errorId = origErrorId; + + + if (inputId != -1) { + close(inputId); + } + if (outputId != -1) { + close(outputId); + } + inputId = pipeIds[0]; + pipeIds[0] = pipeIds[1] = -1; + } + *pidArrayPtr = pidPtr; + + + cleanup: + if (inputId != -1) { + close(inputId); + } + if (lastOutputId != -1) { + close(lastOutputId); + } + if (errorId != -1) { + close(errorId); + } + Jim_Free(arg_array); + + JimRestoreEnv(save_environ); + + return numPids; + + + error: + if ((inPipePtr != NULL) && (*inPipePtr != -1)) { + close(*inPipePtr); + *inPipePtr = -1; + } + if ((outPipePtr != NULL) && (*outPipePtr != -1)) { + close(*outPipePtr); + *outPipePtr = -1; + } + if ((errFilePtr != NULL) && (*errFilePtr != -1)) { + close(*errFilePtr); + *errFilePtr = -1; + } + if (pipeIds[0] != -1) { + close(pipeIds[0]); + } + if (pipeIds[1] != -1) { + close(pipeIds[1]); + } + if (pidPtr != NULL) { + for (i = 0; i < numPids; i++) { + if (pidPtr[i] != JIM_BAD_PHANDLE) { + JimDetachPids(table, 1, &pidPtr[i]); + } + } + Jim_Free(pidPtr); + } + numPids = -1; + goto cleanup; +} + + +static int JimCleanupChildren(Jim_Interp *interp, int numPids, phandle_t *pidPtr, Jim_Obj *errStrObj) +{ + struct WaitInfoTable *table = Jim_CmdPrivData(interp); + int result = JIM_OK; + int i; + + + for (i = 0; i < numPids; i++) { + int waitStatus = 0; + long pid = JimWaitForProcess(table, pidPtr[i], &waitStatus); + if (pid > 0) { + if (JimCheckWaitStatus(interp, pid, waitStatus, errStrObj) != JIM_OK) { + result = JIM_ERR; + } + } + } + Jim_Free(pidPtr); + + return result; +} + +int Jim_execInit(Jim_Interp *interp) +{ + struct WaitInfoTable *waitinfo; + + Jim_PackageProvideCheck(interp, "exec"); + + waitinfo = JimAllocWaitInfoTable(); + Jim_CreateCommand(interp, "exec", Jim_ExecCmd, waitinfo, JimFreeWaitInfoTable); + waitinfo->refcount++; + Jim_CreateCommand(interp, "wait", Jim_WaitCommand, waitinfo, JimFreeWaitInfoTable); + Jim_CreateCommand(interp, "pid", Jim_PidCommand, 0, 0); + + return JIM_OK; +} + +#if defined(__MINGW32__) + + +static int +JimWinFindExecutable(const char *originalName, char fullPath[MAX_PATH]) +{ + int i; + static char extensions[][5] = {".exe", "", ".bat"}; + + for (i = 0; i < (int) (sizeof(extensions) / sizeof(extensions[0])); i++) { + snprintf(fullPath, MAX_PATH, "%s%s", originalName, extensions[i]); + + if (SearchPath(NULL, fullPath, NULL, MAX_PATH, fullPath, NULL) == 0) { + continue; + } + if (GetFileAttributes(fullPath) & FILE_ATTRIBUTE_DIRECTORY) { + continue; + } + return 0; + } + + return -1; +} + +static char **JimSaveEnv(char **env) +{ + return env; +} + +static void JimRestoreEnv(char **env) +{ + JimFreeEnv(env, Jim_GetEnviron()); +} + +static char **JimOriginalEnviron(void) +{ + return NULL; +} + +static Jim_Obj * +JimWinBuildCommandLine(Jim_Interp *interp, char **argv) +{ + char *start, *special; + int quote, i; + + Jim_Obj *strObj = Jim_NewStringObj(interp, "", 0); + + for (i = 0; argv[i]; i++) { + if (i > 0) { + Jim_AppendString(interp, strObj, " ", 1); + } + + if (argv[i][0] == '\0') { + quote = 1; + } + else { + quote = 0; + for (start = argv[i]; *start != '\0'; start++) { + if (isspace(UCHAR(*start))) { + quote = 1; + break; + } + } + } + if (quote) { + Jim_AppendString(interp, strObj, "\"" , 1); + } + + start = argv[i]; + for (special = argv[i]; ; ) { + if ((*special == '\\') && (special[1] == '\\' || + special[1] == '"' || (quote && special[1] == '\0'))) { + Jim_AppendString(interp, strObj, start, special - start); + start = special; + while (1) { + special++; + if (*special == '"' || (quote && *special == '\0')) { + + Jim_AppendString(interp, strObj, start, special - start); + break; + } + if (*special != '\\') { + break; + } + } + Jim_AppendString(interp, strObj, start, special - start); + start = special; + } + if (*special == '"') { + if (special == start) { + Jim_AppendString(interp, strObj, "\"", 1); + } + else { + Jim_AppendString(interp, strObj, start, special - start); + } + Jim_AppendString(interp, strObj, "\\\"", 2); + start = special + 1; + } + if (*special == '\0') { + break; + } + special++; + } + Jim_AppendString(interp, strObj, start, special - start); + if (quote) { + Jim_AppendString(interp, strObj, "\"", 1); + } + } + return strObj; +} + +static phandle_t +JimStartWinProcess(Jim_Interp *interp, char **argv, char **env, int inputId, int outputId, int errorId) +{ + STARTUPINFO startInfo; + PROCESS_INFORMATION procInfo; + HANDLE hProcess; + char execPath[MAX_PATH]; + phandle_t phandle = INVALID_HANDLE_VALUE; + Jim_Obj *cmdLineObj; + char *winenv; + + if (JimWinFindExecutable(argv[0], execPath) < 0) { + return phandle; + } + argv[0] = execPath; + + hProcess = GetCurrentProcess(); + cmdLineObj = JimWinBuildCommandLine(interp, argv); + + + ZeroMemory(&startInfo, sizeof(startInfo)); + startInfo.cb = sizeof(startInfo); + startInfo.dwFlags = STARTF_USESTDHANDLES; + startInfo.hStdInput = INVALID_HANDLE_VALUE; + startInfo.hStdOutput= INVALID_HANDLE_VALUE; + startInfo.hStdError = INVALID_HANDLE_VALUE; + + if (inputId == -1) { + inputId = _fileno(stdin); + } + DuplicateHandle(hProcess, (HANDLE)_get_osfhandle(inputId), hProcess, &startInfo.hStdInput, + 0, TRUE, DUPLICATE_SAME_ACCESS); + if (startInfo.hStdInput == INVALID_HANDLE_VALUE) { + goto end; + } + + if (outputId == -1) { + outputId = _fileno(stdout); + } + DuplicateHandle(hProcess, (HANDLE)_get_osfhandle(outputId), hProcess, &startInfo.hStdOutput, + 0, TRUE, DUPLICATE_SAME_ACCESS); + if (startInfo.hStdOutput == INVALID_HANDLE_VALUE) { + goto end; + } + + + if (errorId == -1) { + errorId = _fileno(stderr); + } + DuplicateHandle(hProcess, (HANDLE)_get_osfhandle(errorId), hProcess, &startInfo.hStdError, + 0, TRUE, DUPLICATE_SAME_ACCESS); + if (startInfo.hStdError == INVALID_HANDLE_VALUE) { + goto end; + } + + if (env == NULL) { + + winenv = NULL; + } + else if (env[0] == NULL) { + winenv = (char *)"\0"; + } + else { + winenv = env[0]; + } + + if (!CreateProcess(NULL, (char *)Jim_String(cmdLineObj), NULL, NULL, TRUE, + 0, winenv, NULL, &startInfo, &procInfo)) { + goto end; + } + + + WaitForInputIdle(procInfo.hProcess, 5000); + CloseHandle(procInfo.hThread); + + phandle = procInfo.hProcess; + + end: + Jim_FreeNewObj(interp, cmdLineObj); + if (startInfo.hStdInput != INVALID_HANDLE_VALUE) { + CloseHandle(startInfo.hStdInput); + } + if (startInfo.hStdOutput != INVALID_HANDLE_VALUE) { + CloseHandle(startInfo.hStdOutput); + } + if (startInfo.hStdError != INVALID_HANDLE_VALUE) { + CloseHandle(startInfo.hStdError); + } + return phandle; +} + +#else + +static char **JimOriginalEnviron(void) +{ + return Jim_GetEnviron(); +} + +static char **JimSaveEnv(char **env) +{ + char **saveenv = Jim_GetEnviron(); + Jim_SetEnviron(env); + return saveenv; +} + +static void JimRestoreEnv(char **env) +{ + JimFreeEnv(Jim_GetEnviron(), env); + Jim_SetEnviron(env); +} +#endif +#endif + + +#include +#include +#include +#include + + +#ifdef HAVE_SYS_TIME_H +#include +#endif + +struct clock_options { + int gmt; + const char *format; +}; + +static int parse_clock_options(Jim_Interp *interp, int argc, Jim_Obj *const *argv, struct clock_options *opts) +{ + static const char * const options[] = { "-gmt", "-format", NULL }; + enum { OPT_GMT, OPT_FORMAT, }; + int i; + + for (i = 0; i < argc; i += 2) { + int option; + if (Jim_GetEnum(interp, argv[i], options, &option, NULL, JIM_ERRMSG | JIM_ENUM_ABBREV) != JIM_OK) { + return JIM_ERR; + } + switch (option) { + case OPT_GMT: + if (Jim_GetBoolean(interp, argv[i + 1], &opts->gmt) != JIM_OK) { + return JIM_ERR; + } + break; + case OPT_FORMAT: + opts->format = Jim_String(argv[i + 1]); + break; + } + } + return JIM_OK; +} + +static int clock_cmd_format(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + + char buf[100]; + time_t t; + jim_wide seconds; + struct clock_options options = { 0, "%a %b %d %H:%M:%S %Z %Y" }; + struct tm *tm; + + if (Jim_GetWide(interp, argv[0], &seconds) != JIM_OK) { + return JIM_ERR; + } + if (argc % 2 == 0) { + return -1; + } + if (parse_clock_options(interp, argc - 1, argv + 1, &options) == JIM_ERR) { + return JIM_ERR; + } + + t = seconds; + tm = options.gmt ? gmtime(&t) : localtime(&t); + + if (tm == NULL || strftime(buf, sizeof(buf), options.format, tm) == 0) { + Jim_SetResultString(interp, "format string too long or invalid time", -1); + return JIM_ERR; + } + + Jim_SetResultString(interp, buf, -1); + + return JIM_OK; +} + +#ifdef HAVE_STRPTIME +static time_t jim_timegm(const struct tm *tm) +{ + int m = tm->tm_mon + 1; + int y = 1900 + tm->tm_year - (m <= 2); + int era = (y >= 0 ? y : y - 399) / 400; + unsigned yoe = (unsigned)(y - era * 400); + unsigned doy = (153 * (m + (m > 2 ? -3 : 9)) + 2) / 5 + tm->tm_mday - 1; + unsigned doe = yoe * 365 + yoe / 4 - yoe / 100 + doy; + long days = (era * 146097 + (int)doe - 719468); + int secs = tm->tm_hour * 3600 + tm->tm_min * 60 + tm->tm_sec; + + return days * 24 * 60 * 60 + secs; +} + +static int clock_cmd_scan(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + char *pt; + struct tm tm; + time_t now = time(NULL); + + struct clock_options options = { 0, NULL }; + + if (argc % 2 == 0) { + return -1; + } + + if (parse_clock_options(interp, argc - 1, argv + 1, &options) == JIM_ERR) { + return JIM_ERR; + } + if (options.format == NULL) { + return -1; + } + + localtime_r(&now, &tm); + + pt = strptime(Jim_String(argv[0]), options.format, &tm); + if (pt == 0 || *pt != 0) { + Jim_SetResultString(interp, "Failed to parse time according to format", -1); + return JIM_ERR; + } + + + tm.tm_isdst = options.gmt ? 0 : -1; + Jim_SetResultInt(interp, options.gmt ? jim_timegm(&tm) : mktime(&tm)); + + return JIM_OK; +} +#endif + +static int clock_cmd_seconds(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + Jim_SetResultInt(interp, Jim_GetTimeUsec(CLOCK_REALTIME) / 1000000); + return JIM_OK; +} + +static int clock_cmd_clicks(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + Jim_SetResultInt(interp, Jim_GetTimeUsec(CLOCK_MONOTONIC_RAW)); + return JIM_OK; +} + +static int clock_cmd_micros(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + Jim_SetResultInt(interp, Jim_GetTimeUsec(CLOCK_REALTIME)); + return JIM_OK; +} + +static int clock_cmd_millis(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + Jim_SetResultInt(interp, Jim_GetTimeUsec(CLOCK_REALTIME) / 1000); + return JIM_OK; +} + +static const jim_subcmd_type clock_command_table[] = { + { "clicks", + NULL, + clock_cmd_clicks, + 0, + 0, + + }, + { "format", + "seconds ?-format string? ?-gmt boolean?", + clock_cmd_format, + 1, + 5, + + }, + { "microseconds", + NULL, + clock_cmd_micros, + 0, + 0, + + }, + { "milliseconds", + NULL, + clock_cmd_millis, + 0, + 0, + + }, +#ifdef HAVE_STRPTIME + { "scan", + "str -format format ?-gmt boolean?", + clock_cmd_scan, + 3, + 5, + + }, +#endif + { "seconds", + NULL, + clock_cmd_seconds, + 0, + 0, + + }, + { NULL } +}; + +int Jim_clockInit(Jim_Interp *interp) +{ + Jim_PackageProvideCheck(interp, "clock"); + Jim_CreateCommand(interp, "clock", Jim_SubCmdProc, (void *)clock_command_table, NULL); + return JIM_OK; +} + +#include +#include +#include +#include +#include + + +static int array_cmd_exists(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + + Jim_Obj *dictObj = Jim_GetVariable(interp, argv[0], JIM_UNSHARED); + Jim_SetResultInt(interp, dictObj && Jim_DictSize(interp, dictObj) != -1); + return JIM_OK; +} + +static int array_cmd_get(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + Jim_Obj *objPtr = Jim_GetVariable(interp, argv[0], JIM_NONE); + Jim_Obj *patternObj; + + if (!objPtr) { + return JIM_OK; + } + + patternObj = (argc == 1) ? NULL : argv[1]; + + + if (patternObj == NULL || Jim_CompareStringImmediate(interp, patternObj, "*")) { + if (Jim_IsList(objPtr) && Jim_ListLength(interp, objPtr) % 2 == 0) { + + Jim_SetResult(interp, objPtr); + return JIM_OK; + } + } + + return Jim_DictMatchTypes(interp, objPtr, patternObj, JIM_DICTMATCH_KEYS, JIM_DICTMATCH_KEYS | JIM_DICTMATCH_VALUES); +} + +static int array_cmd_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + Jim_Obj *objPtr = Jim_GetVariable(interp, argv[0], JIM_NONE); + + if (!objPtr) { + return JIM_OK; + } + + return Jim_DictMatchTypes(interp, objPtr, argc == 1 ? NULL : argv[1], JIM_DICTMATCH_KEYS, JIM_DICTMATCH_KEYS); +} + +static int array_cmd_unset(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + int i; + int len; + Jim_Obj *resultObj; + Jim_Obj *objPtr; + Jim_Obj **dictValuesObj; + + if (argc == 1 || Jim_CompareStringImmediate(interp, argv[1], "*")) { + + Jim_UnsetVariable(interp, argv[0], JIM_NONE); + return JIM_OK; + } + + objPtr = Jim_GetVariable(interp, argv[0], JIM_NONE); + + if (objPtr == NULL) { + + return JIM_OK; + } + + dictValuesObj = Jim_DictPairs(interp, objPtr, &len); + if (dictValuesObj == NULL) { + + Jim_SetResultString(interp, "", -1); + return JIM_OK; + } + + + resultObj = Jim_NewDictObj(interp, NULL, 0); + + for (i = 0; i < len; i += 2) { + if (!Jim_StringMatchObj(interp, argv[1], dictValuesObj[i], 0)) { + Jim_DictAddElement(interp, resultObj, dictValuesObj[i], dictValuesObj[i + 1]); + } + } + + Jim_SetVariable(interp, argv[0], resultObj); + return JIM_OK; +} + +static int array_cmd_size(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + Jim_Obj *objPtr; + int len = 0; + + + objPtr = Jim_GetVariable(interp, argv[0], JIM_NONE); + if (objPtr) { + len = Jim_DictSize(interp, objPtr); + if (len < 0) { + + Jim_SetResultInt(interp, 0); + return JIM_OK; + } + } + + Jim_SetResultInt(interp, len); + + return JIM_OK; +} + +static int array_cmd_stat(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + Jim_Obj *objPtr = Jim_GetVariable(interp, argv[0], JIM_NONE); + if (objPtr) { + return Jim_DictInfo(interp, objPtr); + } + Jim_SetResultFormatted(interp, "\"%#s\" isn't an array", argv[0], NULL); + return JIM_ERR; +} + +static int array_cmd_set(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + int i; + int len; + Jim_Obj *listObj = argv[1]; + Jim_Obj *dictObj; + + len = Jim_ListLength(interp, listObj); + if (len % 2) { + Jim_SetResultString(interp, "list must have an even number of elements", -1); + return JIM_ERR; + } + + dictObj = Jim_GetVariable(interp, argv[0], JIM_UNSHARED); + if (!dictObj) { + + return Jim_SetVariable(interp, argv[0], listObj); + } + else if (Jim_DictSize(interp, dictObj) < 0) { + return JIM_ERR; + } + + if (Jim_IsShared(dictObj)) { + dictObj = Jim_DuplicateObj(interp, dictObj); + } + + for (i = 0; i < len; i += 2) { + Jim_Obj *nameObj; + Jim_Obj *valueObj; + + Jim_ListIndex(interp, listObj, i, &nameObj, JIM_NONE); + Jim_ListIndex(interp, listObj, i + 1, &valueObj, JIM_NONE); + + Jim_DictAddElement(interp, dictObj, nameObj, valueObj); + } + return Jim_SetVariable(interp, argv[0], dictObj); +} + +static const jim_subcmd_type array_command_table[] = { + { "exists", + "arrayName", + array_cmd_exists, + 1, + 1, + + }, + { "get", + "arrayName ?pattern?", + array_cmd_get, + 1, + 2, + + }, + { "names", + "arrayName ?pattern?", + array_cmd_names, + 1, + 2, + + }, + { "set", + "arrayName list", + array_cmd_set, + 2, + 2, + + }, + { "size", + "arrayName", + array_cmd_size, + 1, + 1, + + }, + { "stat", + "arrayName", + array_cmd_stat, + 1, + 1, + + }, + { "unset", + "arrayName ?pattern?", + array_cmd_unset, + 1, + 2, + + }, + { NULL + } +}; + +int Jim_arrayInit(Jim_Interp *interp) +{ + Jim_PackageProvideCheck(interp, "array"); + Jim_CreateCommand(interp, "array", Jim_SubCmdProc, (void *)array_command_table, NULL); + return JIM_OK; +} +int Jim_InitStaticExtensions(Jim_Interp *interp) +{ +extern int Jim_bootstrapInit(Jim_Interp *); +extern int Jim_aioInit(Jim_Interp *); +extern int Jim_readdirInit(Jim_Interp *); +extern int Jim_regexpInit(Jim_Interp *); +extern int Jim_fileInit(Jim_Interp *); +extern int Jim_globInit(Jim_Interp *); +extern int Jim_execInit(Jim_Interp *); +extern int Jim_clockInit(Jim_Interp *); +extern int Jim_arrayInit(Jim_Interp *); +extern int Jim_stdlibInit(Jim_Interp *); +extern int Jim_tclcompatInit(Jim_Interp *); +Jim_bootstrapInit(interp); +Jim_aioInit(interp); +Jim_readdirInit(interp); +Jim_regexpInit(interp); +Jim_fileInit(interp); +Jim_globInit(interp); +Jim_execInit(interp); +Jim_clockInit(interp); +Jim_arrayInit(interp); +Jim_stdlibInit(interp); +Jim_tclcompatInit(interp); +return JIM_OK; +} +#ifndef JIM_TINY +#define JIM_OPTIMIZATION +#endif + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + + +#ifdef HAVE_SYS_TIME_H +#include +#endif +#ifdef HAVE_EXECINFO_H +#include +#endif +#ifdef HAVE_CRT_EXTERNS_H +#include +#endif + + +#include + + + + + +#ifndef TCL_LIBRARY +#define TCL_LIBRARY "." +#endif +#ifndef TCL_PLATFORM_OS +#define TCL_PLATFORM_OS "unknown" +#endif +#ifndef TCL_PLATFORM_PLATFORM +#define TCL_PLATFORM_PLATFORM "unknown" +#endif +#ifndef TCL_PLATFORM_PATH_SEPARATOR +#define TCL_PLATFORM_PATH_SEPARATOR ":" +#endif + + + + + + + +#ifdef JIM_MAINTAINER +#define JIM_DEBUG_COMMAND +#define JIM_DEBUG_PANIC +#endif + + + +#define JIM_INTEGER_SPACE 24 + +#if defined(DEBUG_SHOW_SCRIPT) || defined(DEBUG_SHOW_SCRIPT_TOKENS) || defined(JIM_DEBUG_COMMAND) || defined(DEBUG_SHOW_SUBST) +static const char *jim_tt_name(int type); +#endif + +#ifdef JIM_DEBUG_PANIC +static void JimPanicDump(int fail_condition, const char *fmt, ...); +#define JimPanic(X) JimPanicDump X +#else +#define JimPanic(X) +#endif + +#ifdef JIM_OPTIMIZATION +static int JimIsWide(Jim_Obj *objPtr); +#define JIM_IF_OPTIM(X) X +#else +#define JIM_IF_OPTIM(X) +#endif + + +static char JimEmptyStringRep[] = ""; + +static void JimFreeCallFrame(Jim_Interp *interp, Jim_CallFrame *cf, int action); +static int ListSetIndex(Jim_Interp *interp, Jim_Obj *listPtr, int listindex, Jim_Obj *newObjPtr, + int flags); +static int Jim_ListIndices(Jim_Interp *interp, Jim_Obj *listPtr, Jim_Obj *const *indexv, int indexc, + Jim_Obj **resultObj, int flags); +static int JimDeleteLocalProcs(Jim_Interp *interp, Jim_Stack *localCommands); +static Jim_Obj *JimExpandDictSugar(Jim_Interp *interp, Jim_Obj *objPtr); +static void SetDictSubstFromAny(Jim_Interp *interp, Jim_Obj *objPtr); +static void JimSetFailedEnumResult(Jim_Interp *interp, const char *arg, const char *badtype, + const char *prefix, const char *const *tablePtr, const char *name); +static int JimCallProcedure(Jim_Interp *interp, Jim_Cmd *cmd, int argc, Jim_Obj *const *argv); +static int JimGetWideNoErr(Jim_Interp *interp, Jim_Obj *objPtr, jim_wide * widePtr); +static int JimSign(jim_wide w); +static void JimPrngSeed(Jim_Interp *interp, unsigned char *seed, int seedLen); +static void JimRandomBytes(Jim_Interp *interp, void *dest, unsigned int len); +static int JimSetNewVariable(Jim_HashTable *ht, Jim_Obj *nameObjPtr, Jim_VarVal *vv); +static Jim_VarVal *JimFindVariable(Jim_HashTable *ht, Jim_Obj *nameObjPtr); +static int SetVariableFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr); + +#define JIM_DICT_SUGAR 100 + + + + +#define JimWideValue(objPtr) (objPtr)->internalRep.wideValue + +#define JimObjTypeName(O) ((O)->typePtr ? (O)->typePtr->name : "none") + +static int utf8_tounicode_case(const char *s, int *uc, int upper) +{ + int l = utf8_tounicode(s, uc); + if (upper) { + *uc = utf8_upper(*uc); + } + return l; +} + +static Jim_Obj *JimPushInterpObjImpl(Jim_Obj **iop, Jim_Obj *no) +{ + Jim_Obj *io = *iop; + Jim_IncrRefCount(no); + *iop = no; + return io; +} + +#define JimPushInterpObj(IO, NO) JimPushInterpObjImpl(&(IO), NO) +#define JimPopInterpObj(I, IO, SO) do { Jim_DecrRefCount(I, IO); IO = SO; } while (0) + + +#define JIM_CHARSET_SCAN 2 +#define JIM_CHARSET_GLOB 0 + +static const char *JimCharsetMatch(const char *pattern, int plen, int c, int flags) +{ + int not = 0; + int pchar; + int match = 0; + int nocase = 0; + int n; + + if (flags & JIM_NOCASE) { + nocase++; + c = utf8_upper(c); + } + + if (flags & JIM_CHARSET_SCAN) { + if (*pattern == '^') { + not++; + pattern++; + plen--; + } + + + if (*pattern == ']') { + goto first; + } + } + + while (plen && *pattern != ']') { + + if (pattern[0] == '\\') { +first: + n = utf8_tounicode_case(pattern, &pchar, nocase); + pattern += n; + plen -= n; + } + else { + + int start; + int end; + + n = utf8_tounicode_case(pattern, &start, nocase); + pattern += n; + plen -= n; + if (pattern[0] == '-' && plen > 1) { + + n = 1 + utf8_tounicode_case(pattern + 1, &end, nocase); + pattern += n; + plen -= n; + + + if ((c >= start && c <= end) || (c >= end && c <= start)) { + match = 1; + } + continue; + } + pchar = start; + } + + if (pchar == c) { + match = 1; + } + } + if (not) { + match = !match; + } + + return match ? pattern : NULL; +} + + + +static int JimGlobMatch(const char *pattern, int plen, const char *string, int slen, int nocase) +{ + int c; + int pchar; + int n; + const char *p; + while (plen) { + switch (pattern[0]) { + case '*': + while (pattern[1] == '*' && plen) { + pattern++; + plen--; + } + pattern++; + plen--; + if (!plen) { + return 1; + } + while (slen) { + + if (JimGlobMatch(pattern, plen, string, slen, nocase)) + return 1; + n = utf8_tounicode(string, &c); + string += n; + slen -= n; + } + return 0; + + case '?': + n = utf8_tounicode(string, &c); + string += n; + slen -= n; + break; + + case '[': { + n = utf8_tounicode(string, &c); + string += n; + slen -= n; + p = JimCharsetMatch(pattern + 1, plen - 1, c, nocase ? JIM_NOCASE : 0); + if (!p) { + return 0; + } + plen -= p - pattern; + pattern = p; + + if (!plen) { + + continue; + } + break; + } + case '\\': + if (pattern[1]) { + pattern++; + plen--; + } + + default: + n = utf8_tounicode_case(string, &c, nocase); + string += n; + slen -= n; + utf8_tounicode_case(pattern, &pchar, nocase); + if (pchar != c) { + return 0; + } + break; + } + n = utf8_tounicode_case(pattern, &pchar, nocase); + pattern += n; + plen -= n; + if (!slen) { + while (*pattern == '*' && plen) { + pattern++; + plen--; + } + break; + } + } + if (!plen && !slen) { + return 1; + } + return 0; +} + +static int JimStringCompareUtf8(const char *s1, int l1, const char *s2, int l2, int nocase) +{ + int minlen = l1; + if (l2 < l1) { + minlen = l2; + } + while (minlen) { + int c1, c2; + s1 += utf8_tounicode_case(s1, &c1, nocase); + s2 += utf8_tounicode_case(s2, &c2, nocase); + if (c1 != c2) { + return JimSign(c1 - c2); + } + minlen--; + } + + if (l1 < l2) { + return -1; + } + if (l1 > l2) { + return 1; + } + return 0; +} + +static int JimStringFirst(const char *s1, int l1, const char *s2, int l2, int idx) +{ + int i; + int l1bytelen; + + if (!l1 || !l2 || l1 > l2) { + return -1; + } + if (idx < 0) + idx = 0; + s2 += utf8_index(s2, idx); + + l1bytelen = utf8_index(s1, l1); + + for (i = idx; i <= l2 - l1; i++) { + int c; + if (memcmp(s2, s1, l1bytelen) == 0) { + return i; + } + s2 += utf8_tounicode(s2, &c); + } + return -1; +} + +static int JimStringLast(const char *s1, int l1, const char *s2, int l2) +{ + const char *p; + + if (!l1 || !l2 || l1 > l2) + return -1; + + + for (p = s2 + l2 - 1; p != s2 - 1; p--) { + if (*p == *s1 && memcmp(s1, p, l1) == 0) { + return p - s2; + } + } + return -1; +} + +#ifdef JIM_UTF8 +static int JimStringLastUtf8(const char *s1, int l1, const char *s2, int l2) +{ + int n = JimStringLast(s1, utf8_index(s1, l1), s2, utf8_index(s2, l2)); + if (n > 0) { + n = utf8_strlen(s2, n); + } + return n; +} +#endif + +static int JimCheckConversion(const char *str, const char *endptr) +{ + if (str[0] == '\0' || str == endptr) { + return JIM_ERR; + } + + if (endptr[0] != '\0') { + while (*endptr) { + if (!isspace(UCHAR(*endptr))) { + return JIM_ERR; + } + endptr++; + } + } + return JIM_OK; +} + +static int JimNumberBase(const char *str, int *base, int *sign) +{ + int i = 0; + + *base = 0; + + while (isspace(UCHAR(str[i]))) { + i++; + } + + if (str[i] == '-') { + *sign = -1; + i++; + } + else { + if (str[i] == '+') { + i++; + } + *sign = 1; + } + + if (str[i] != '0') { + + return 0; + } + + + switch (str[i + 1]) { + case 'x': case 'X': *base = 16; break; + case 'o': case 'O': *base = 8; break; + case 'b': case 'B': *base = 2; break; + case 'd': case 'D': *base = 10; break; + default: return 0; + } + i += 2; + + if (str[i] != '-' && str[i] != '+' && !isspace(UCHAR(str[i]))) { + + return i; + } + + *base = 0; + return 0; +} + +static long jim_strtol(const char *str, char **endptr) +{ + int sign; + int base; + int i = JimNumberBase(str, &base, &sign); + + if (base != 0) { + long value = strtol(str + i, endptr, base); + if (endptr == NULL || *endptr != str + i) { + return value * sign; + } + } + + + return strtol(str, endptr, 10); +} + + +static jim_wide jim_strtoull(const char *str, char **endptr) +{ +#ifdef HAVE_LONG_LONG + int sign; + int base; + int i = JimNumberBase(str, &base, &sign); + + if (base != 0) { + jim_wide value = strtoull(str + i, endptr, base); + if (endptr == NULL || *endptr != str + i) { + return value * sign; + } + } + + + return strtoull(str, endptr, 10); +#else + return (unsigned long)jim_strtol(str, endptr); +#endif +} + +int Jim_StringToWide(const char *str, jim_wide * widePtr, int base) +{ + char *endptr; + + if (base) { + *widePtr = strtoull(str, &endptr, base); + } + else { + *widePtr = jim_strtoull(str, &endptr); + } + + return JimCheckConversion(str, endptr); +} + +int Jim_StringToDouble(const char *str, double *doublePtr) +{ + char *endptr; + + + errno = 0; + + *doublePtr = strtod(str, &endptr); + + return JimCheckConversion(str, endptr); +} + +static jim_wide JimPowWide(jim_wide b, jim_wide e) +{ + jim_wide res = 1; + + + if (b == 1) { + + return 1; + } + if (e < 0) { + if (b != -1) { + return 0; + } + e = -e; + } + while (e) + { + if (e & 1) { + res *= b; + } + e >>= 1; + b *= b; + } + return res; +} + +#ifdef JIM_DEBUG_PANIC +static void JimPanicDump(int condition, const char *fmt, ...) +{ + va_list ap; + + if (!condition) { + return; + } + + va_start(ap, fmt); + + fprintf(stderr, "\nJIM INTERPRETER PANIC: "); + vfprintf(stderr, fmt, ap); + fprintf(stderr, "\n\n"); + va_end(ap); + +#if defined(HAVE_BACKTRACE) + { + void *array[40]; + int size, i; + char **strings; + + size = backtrace(array, 40); + strings = backtrace_symbols(array, size); + for (i = 0; i < size; i++) + fprintf(stderr, "[backtrace] %s\n", strings[i]); + fprintf(stderr, "[backtrace] Include the above lines and the output\n"); + fprintf(stderr, "[backtrace] of 'nm ' in the bug report.\n"); + } +#endif + + exit(1); +} +#endif + + +void *JimDefaultAllocator(void *ptr, size_t size) +{ + if (size == 0) { + free(ptr); + return NULL; + } + else if (ptr) { + return realloc(ptr, size); + } + else { + return malloc(size); + } +} + +void *(*Jim_Allocator)(void *ptr, size_t size) = JimDefaultAllocator; + +char *Jim_StrDup(const char *s) +{ + return Jim_StrDupLen(s, strlen(s)); +} + +char *Jim_StrDupLen(const char *s, int l) +{ + char *copy = Jim_Alloc(l + 1); + + memcpy(copy, s, l); + copy[l] = 0; + return copy; +} + + +jim_wide Jim_GetTimeUsec(unsigned type) +{ + long long now; + struct timeval tv; + +#if defined(HAVE_CLOCK_GETTIME) + struct timespec ts; + + if (clock_gettime(type, &ts) == 0) { + now = ts.tv_sec * 1000000LL + ts.tv_nsec / 1000; + } + else +#endif + { + gettimeofday(&tv, NULL); + + now = tv.tv_sec * 1000000LL + tv.tv_usec; + } + + return now; +} + + + + + +static void JimExpandHashTableIfNeeded(Jim_HashTable *ht); +static unsigned int JimHashTableNextPower(unsigned int size); +static Jim_HashEntry *JimInsertHashEntry(Jim_HashTable *ht, const void *key, int replace); + + + + +unsigned int Jim_IntHashFunction(unsigned int key) +{ + key += ~(key << 15); + key ^= (key >> 10); + key += (key << 3); + key ^= (key >> 6); + key += ~(key << 11); + key ^= (key >> 16); + return key; +} + + +unsigned int Jim_GenHashFunction(const unsigned char *string, int length) +{ + unsigned result = 0; + string += length; + while (length--) { + result += (result << 3) + (unsigned char)(*--string); + } + return result; +} + + + +static void JimResetHashTable(Jim_HashTable *ht) +{ + ht->table = NULL; + ht->size = 0; + ht->sizemask = 0; + ht->used = 0; + ht->collisions = 0; +#ifdef JIM_RANDOMISE_HASH + ht->uniq = (rand() ^ time(NULL) ^ clock()); +#else + ht->uniq = 0; +#endif +} + +static void JimInitHashTableIterator(Jim_HashTable *ht, Jim_HashTableIterator *iter) +{ + iter->ht = ht; + iter->index = -1; + iter->entry = NULL; + iter->nextEntry = NULL; +} + + +int Jim_InitHashTable(Jim_HashTable *ht, const Jim_HashTableType *type, void *privDataPtr) +{ + JimResetHashTable(ht); + ht->type = type; + ht->privdata = privDataPtr; + return JIM_OK; +} + + +void Jim_ExpandHashTable(Jim_HashTable *ht, unsigned int size) +{ + Jim_HashTable n; + unsigned int realsize = JimHashTableNextPower(size), i; + + if (size <= ht->used) + return; + + Jim_InitHashTable(&n, ht->type, ht->privdata); + n.size = realsize; + n.sizemask = realsize - 1; + n.table = Jim_Alloc(realsize * sizeof(Jim_HashEntry *)); + + n.uniq = ht->uniq; + + + memset(n.table, 0, realsize * sizeof(Jim_HashEntry *)); + + n.used = ht->used; + for (i = 0; ht->used > 0; i++) { + Jim_HashEntry *he, *nextHe; + + if (ht->table[i] == NULL) + continue; + + + he = ht->table[i]; + while (he) { + unsigned int h; + + nextHe = he->next; + + h = Jim_HashKey(ht, he->key) & n.sizemask; + he->next = n.table[h]; + n.table[h] = he; + ht->used--; + + he = nextHe; + } + } + assert(ht->used == 0); + Jim_Free(ht->table); + + + *ht = n; +} + +int Jim_AddHashEntry(Jim_HashTable *ht, const void *key, void *val) +{ + Jim_HashEntry *entry = JimInsertHashEntry(ht, key, 0);; + if (entry == NULL) + return JIM_ERR; + + + Jim_SetHashKey(ht, entry, key); + Jim_SetHashVal(ht, entry, val); + return JIM_OK; +} + + +int Jim_ReplaceHashEntry(Jim_HashTable *ht, const void *key, void *val) +{ + int existed; + Jim_HashEntry *entry; + + entry = JimInsertHashEntry(ht, key, 1); + if (entry->key) { + if (ht->type->valDestructor && ht->type->valDup) { + void *newval = ht->type->valDup(ht->privdata, val); + ht->type->valDestructor(ht->privdata, entry->u.val); + entry->u.val = newval; + } + else { + Jim_FreeEntryVal(ht, entry); + Jim_SetHashVal(ht, entry, val); + } + existed = 1; + } + else { + + Jim_SetHashKey(ht, entry, key); + Jim_SetHashVal(ht, entry, val); + existed = 0; + } + + return existed; +} + +int Jim_DeleteHashEntry(Jim_HashTable *ht, const void *key) +{ + if (ht->used) { + unsigned int h = Jim_HashKey(ht, key) & ht->sizemask; + Jim_HashEntry *prevHe = NULL; + Jim_HashEntry *he = ht->table[h]; + + while (he) { + if (Jim_CompareHashKeys(ht, key, he->key)) { + + if (prevHe) + prevHe->next = he->next; + else + ht->table[h] = he->next; + ht->used--; + Jim_FreeEntryKey(ht, he); + Jim_FreeEntryVal(ht, he); + Jim_Free(he); + return JIM_OK; + } + prevHe = he; + he = he->next; + } + } + + return JIM_ERR; +} + +void Jim_ClearHashTable(Jim_HashTable *ht) +{ + unsigned int i; + + + for (i = 0; ht->used > 0; i++) { + Jim_HashEntry *he, *nextHe; + + he = ht->table[i]; + while (he) { + nextHe = he->next; + Jim_FreeEntryKey(ht, he); + Jim_FreeEntryVal(ht, he); + Jim_Free(he); + ht->used--; + he = nextHe; + } + ht->table[i] = NULL; + } +} + +int Jim_FreeHashTable(Jim_HashTable *ht) +{ + Jim_ClearHashTable(ht); + + Jim_Free(ht->table); + + JimResetHashTable(ht); + return JIM_OK; +} + +Jim_HashEntry *Jim_FindHashEntry(Jim_HashTable *ht, const void *key) +{ + Jim_HashEntry *he; + unsigned int h; + + if (ht->used == 0) + return NULL; + h = Jim_HashKey(ht, key) & ht->sizemask; + he = ht->table[h]; + while (he) { + if (Jim_CompareHashKeys(ht, key, he->key)) + return he; + he = he->next; + } + return NULL; +} + +Jim_HashTableIterator *Jim_GetHashTableIterator(Jim_HashTable *ht) +{ + Jim_HashTableIterator *iter = Jim_Alloc(sizeof(*iter)); + JimInitHashTableIterator(ht, iter); + return iter; +} + +Jim_HashEntry *Jim_NextHashEntry(Jim_HashTableIterator *iter) +{ + while (1) { + if (iter->entry == NULL) { + iter->index++; + if (iter->index >= (signed)iter->ht->size) + break; + iter->entry = iter->ht->table[iter->index]; + } + else { + iter->entry = iter->nextEntry; + } + if (iter->entry) { + iter->nextEntry = iter->entry->next; + return iter->entry; + } + } + return NULL; +} + + + + +static void JimExpandHashTableIfNeeded(Jim_HashTable *ht) +{ + if (ht->size == 0) + Jim_ExpandHashTable(ht, JIM_HT_INITIAL_SIZE); + if (ht->size == ht->used) + Jim_ExpandHashTable(ht, ht->size * 2); +} + + +static unsigned int JimHashTableNextPower(unsigned int size) +{ + unsigned int i = JIM_HT_INITIAL_SIZE; + + if (size >= 2147483648U) + return 2147483648U; + while (1) { + if (i >= size) + return i; + i *= 2; + } +} + +static Jim_HashEntry *JimInsertHashEntry(Jim_HashTable *ht, const void *key, int replace) +{ + unsigned int h; + Jim_HashEntry *he; + + + JimExpandHashTableIfNeeded(ht); + + + h = Jim_HashKey(ht, key) & ht->sizemask; + + he = ht->table[h]; + while (he) { + if (Jim_CompareHashKeys(ht, key, he->key)) + return replace ? he : NULL; + he = he->next; + } + + + he = Jim_Alloc(sizeof(*he)); + he->next = ht->table[h]; + ht->table[h] = he; + ht->used++; + he->key = NULL; + + return he; +} + + + +static unsigned int JimStringCopyHTHashFunction(const void *key) +{ + return Jim_GenHashFunction(key, strlen(key)); +} + +static void *JimStringCopyHTDup(void *privdata, const void *key) +{ + return Jim_StrDup(key); +} + +static int JimStringCopyHTKeyCompare(void *privdata, const void *key1, const void *key2) +{ + return strcmp(key1, key2) == 0; +} + +static void JimStringCopyHTKeyDestructor(void *privdata, void *key) +{ + Jim_Free(key); +} + +static const Jim_HashTableType JimPackageHashTableType = { + JimStringCopyHTHashFunction, + JimStringCopyHTDup, + NULL, + JimStringCopyHTKeyCompare, + JimStringCopyHTKeyDestructor, + NULL +}; + +typedef struct AssocDataValue +{ + Jim_InterpDeleteProc *delProc; + void *data; +} AssocDataValue; + +static void JimAssocDataHashTableValueDestructor(void *privdata, void *data) +{ + AssocDataValue *assocPtr = (AssocDataValue *) data; + + if (assocPtr->delProc != NULL) + assocPtr->delProc((Jim_Interp *)privdata, assocPtr->data); + Jim_Free(data); +} + +static const Jim_HashTableType JimAssocDataHashTableType = { + JimStringCopyHTHashFunction, + JimStringCopyHTDup, + NULL, + JimStringCopyHTKeyCompare, + JimStringCopyHTKeyDestructor, + JimAssocDataHashTableValueDestructor +}; + +void Jim_InitStack(Jim_Stack *stack) +{ + stack->len = 0; + stack->maxlen = 0; + stack->vector = NULL; +} + +void Jim_FreeStack(Jim_Stack *stack) +{ + Jim_Free(stack->vector); +} + +int Jim_StackLen(Jim_Stack *stack) +{ + return stack->len; +} + +void Jim_StackPush(Jim_Stack *stack, void *element) +{ + int neededLen = stack->len + 1; + + if (neededLen > stack->maxlen) { + stack->maxlen = neededLen < 20 ? 20 : neededLen * 2; + stack->vector = Jim_Realloc(stack->vector, sizeof(void *) * stack->maxlen); + } + stack->vector[stack->len] = element; + stack->len++; +} + +void *Jim_StackPop(Jim_Stack *stack) +{ + if (stack->len == 0) + return NULL; + stack->len--; + return stack->vector[stack->len]; +} + +void *Jim_StackPeek(Jim_Stack *stack) +{ + if (stack->len == 0) + return NULL; + return stack->vector[stack->len - 1]; +} + +void Jim_FreeStackElements(Jim_Stack *stack, void (*freeFunc) (void *ptr)) +{ + int i; + + for (i = 0; i < stack->len; i++) + freeFunc(stack->vector[i]); +} + + + +#define JIM_TT_NONE 0 +#define JIM_TT_STR 1 +#define JIM_TT_ESC 2 +#define JIM_TT_VAR 3 +#define JIM_TT_DICTSUGAR 4 +#define JIM_TT_CMD 5 + +#define JIM_TT_SEP 6 +#define JIM_TT_EOL 7 +#define JIM_TT_EOF 8 + +#define JIM_TT_LINE 9 +#define JIM_TT_WORD 10 + + +#define JIM_TT_SUBEXPR_START 11 +#define JIM_TT_SUBEXPR_END 12 +#define JIM_TT_SUBEXPR_COMMA 13 +#define JIM_TT_EXPR_INT 14 +#define JIM_TT_EXPR_DOUBLE 15 +#define JIM_TT_EXPR_BOOLEAN 16 + +#define JIM_TT_EXPRSUGAR 17 + + +#define JIM_TT_EXPR_OP 20 + +#define TOKEN_IS_SEP(type) (type >= JIM_TT_SEP && type <= JIM_TT_EOF) + +#define TOKEN_IS_EXPR_START(type) (type == JIM_TT_NONE || type == JIM_TT_SUBEXPR_START || type == JIM_TT_SUBEXPR_COMMA) + +#define TOKEN_IS_EXPR_OP(type) (type >= JIM_TT_EXPR_OP) + +struct JimParseMissing { + int ch; + int line; +}; + +struct JimParserCtx +{ + const char *p; + int len; + int linenr; + const char *tstart; + const char *tend; + int tline; + int tt; + int eof; + int inquote; + int comment; + struct JimParseMissing missing; + const char *errmsg; +}; + +static int JimParseScript(struct JimParserCtx *pc); +static int JimParseSep(struct JimParserCtx *pc); +static int JimParseEol(struct JimParserCtx *pc); +static int JimParseCmd(struct JimParserCtx *pc); +static int JimParseQuote(struct JimParserCtx *pc); +static int JimParseVar(struct JimParserCtx *pc); +static int JimParseBrace(struct JimParserCtx *pc); +static int JimParseStr(struct JimParserCtx *pc); +static int JimParseComment(struct JimParserCtx *pc); +static void JimParseSubCmd(struct JimParserCtx *pc); +static int JimParseSubQuote(struct JimParserCtx *pc); +static Jim_Obj *JimParserGetTokenObj(Jim_Interp *interp, struct JimParserCtx *pc); + +static void JimParserInit(struct JimParserCtx *pc, const char *prg, int len, int linenr) +{ + pc->p = prg; + pc->len = len; + pc->tstart = NULL; + pc->tend = NULL; + pc->tline = 0; + pc->tt = JIM_TT_NONE; + pc->eof = 0; + pc->inquote = 0; + pc->linenr = linenr; + pc->comment = 1; + pc->missing.ch = ' '; + pc->missing.line = linenr; +} + +static int JimParseScript(struct JimParserCtx *pc) +{ + while (1) { + if (!pc->len) { + pc->tstart = pc->p; + pc->tend = pc->p - 1; + pc->tline = pc->linenr; + pc->tt = JIM_TT_EOL; + if (pc->inquote) { + pc->missing.ch = '"'; + } + pc->eof = 1; + return JIM_OK; + } + switch (*(pc->p)) { + case '\\': + if (*(pc->p + 1) == '\n' && !pc->inquote) { + return JimParseSep(pc); + } + pc->comment = 0; + return JimParseStr(pc); + case ' ': + case '\t': + case '\r': + case '\f': + if (!pc->inquote) + return JimParseSep(pc); + pc->comment = 0; + return JimParseStr(pc); + case '\n': + case ';': + pc->comment = 1; + if (!pc->inquote) + return JimParseEol(pc); + return JimParseStr(pc); + case '[': + pc->comment = 0; + return JimParseCmd(pc); + case '$': + pc->comment = 0; + if (JimParseVar(pc) == JIM_ERR) { + + pc->tstart = pc->tend = pc->p++; + pc->len--; + pc->tt = JIM_TT_ESC; + } + return JIM_OK; + case '#': + if (pc->comment) { + JimParseComment(pc); + continue; + } + return JimParseStr(pc); + default: + pc->comment = 0; + return JimParseStr(pc); + } + return JIM_OK; + } +} + +static int JimParseSep(struct JimParserCtx *pc) +{ + pc->tstart = pc->p; + pc->tline = pc->linenr; + while (isspace(UCHAR(*pc->p)) || (*pc->p == '\\' && *(pc->p + 1) == '\n')) { + if (*pc->p == '\n') { + break; + } + if (*pc->p == '\\') { + pc->p++; + pc->len--; + pc->linenr++; + } + pc->p++; + pc->len--; + } + pc->tend = pc->p - 1; + pc->tt = JIM_TT_SEP; + return JIM_OK; +} + +static int JimParseEol(struct JimParserCtx *pc) +{ + pc->tstart = pc->p; + pc->tline = pc->linenr; + while (isspace(UCHAR(*pc->p)) || *pc->p == ';') { + if (*pc->p == '\n') + pc->linenr++; + pc->p++; + pc->len--; + } + pc->tend = pc->p - 1; + pc->tt = JIM_TT_EOL; + return JIM_OK; +} + + +static void JimParseSubBrace(struct JimParserCtx *pc) +{ + int level = 1; + + + pc->p++; + pc->len--; + while (pc->len) { + switch (*pc->p) { + case '\\': + if (pc->len > 1) { + if (*++pc->p == '\n') { + pc->linenr++; + } + pc->len--; + } + break; + + case '{': + level++; + break; + + case '}': + if (--level == 0) { + pc->tend = pc->p - 1; + pc->p++; + pc->len--; + return; + } + break; + + case '\n': + pc->linenr++; + break; + } + pc->p++; + pc->len--; + } + pc->missing.ch = '{'; + pc->missing.line = pc->tline; + pc->tend = pc->p - 1; +} + +static int JimParseSubQuote(struct JimParserCtx *pc) +{ + int tt = JIM_TT_STR; + int line = pc->tline; + + + pc->p++; + pc->len--; + while (pc->len) { + switch (*pc->p) { + case '\\': + if (pc->len > 1) { + if (*++pc->p == '\n') { + pc->linenr++; + } + pc->len--; + tt = JIM_TT_ESC; + } + break; + + case '"': + pc->tend = pc->p - 1; + pc->p++; + pc->len--; + return tt; + + case '[': + JimParseSubCmd(pc); + tt = JIM_TT_ESC; + continue; + + case '\n': + pc->linenr++; + break; + + case '$': + tt = JIM_TT_ESC; + break; + } + pc->p++; + pc->len--; + } + pc->missing.ch = '"'; + pc->missing.line = line; + pc->tend = pc->p - 1; + return tt; +} + +static void JimParseSubCmd(struct JimParserCtx *pc) +{ + int level = 1; + int startofword = 1; + int line = pc->tline; + + + pc->p++; + pc->len--; + while (pc->len) { + switch (*pc->p) { + case '\\': + if (pc->len > 1) { + if (*++pc->p == '\n') { + pc->linenr++; + } + pc->len--; + } + break; + + case '[': + level++; + break; + + case ']': + if (--level == 0) { + pc->tend = pc->p - 1; + pc->p++; + pc->len--; + return; + } + break; + + case '"': + if (startofword) { + JimParseSubQuote(pc); + if (pc->missing.ch == '"') { + return; + } + continue; + } + break; + + case '{': + JimParseSubBrace(pc); + startofword = 0; + continue; + + case '\n': + pc->linenr++; + break; + } + startofword = isspace(UCHAR(*pc->p)); + pc->p++; + pc->len--; + } + pc->missing.ch = '['; + pc->missing.line = line; + pc->tend = pc->p - 1; +} + +static int JimParseBrace(struct JimParserCtx *pc) +{ + pc->tstart = pc->p + 1; + pc->tline = pc->linenr; + pc->tt = JIM_TT_STR; + JimParseSubBrace(pc); + return JIM_OK; +} + +static int JimParseCmd(struct JimParserCtx *pc) +{ + pc->tstart = pc->p + 1; + pc->tline = pc->linenr; + pc->tt = JIM_TT_CMD; + JimParseSubCmd(pc); + return JIM_OK; +} + +static int JimParseQuote(struct JimParserCtx *pc) +{ + pc->tstart = pc->p + 1; + pc->tline = pc->linenr; + pc->tt = JimParseSubQuote(pc); + return JIM_OK; +} + +static int JimParseVar(struct JimParserCtx *pc) +{ + + pc->p++; + pc->len--; + +#ifdef EXPRSUGAR_BRACKET + if (*pc->p == '[') { + + JimParseCmd(pc); + pc->tt = JIM_TT_EXPRSUGAR; + return JIM_OK; + } +#endif + + pc->tstart = pc->p; + pc->tt = JIM_TT_VAR; + pc->tline = pc->linenr; + + if (*pc->p == '{') { + pc->tstart = ++pc->p; + pc->len--; + + while (pc->len && *pc->p != '}') { + if (*pc->p == '\n') { + pc->linenr++; + } + pc->p++; + pc->len--; + } + pc->tend = pc->p - 1; + if (pc->len) { + pc->p++; + pc->len--; + } + } + else { + while (1) { + + if (pc->p[0] == ':' && pc->p[1] == ':') { + while (*pc->p == ':') { + pc->p++; + pc->len--; + } + continue; + } + if (isalnum(UCHAR(*pc->p)) || *pc->p == '_' || UCHAR(*pc->p) >= 0x80) { + pc->p++; + pc->len--; + continue; + } + break; + } + + if (*pc->p == '(') { + int count = 1; + const char *paren = NULL; + + pc->tt = JIM_TT_DICTSUGAR; + + while (count && pc->len) { + pc->p++; + pc->len--; + if (*pc->p == '\\' && pc->len >= 1) { + pc->p++; + pc->len--; + } + else if (*pc->p == '(') { + count++; + } + else if (*pc->p == ')') { + paren = pc->p; + count--; + } + } + if (count == 0) { + pc->p++; + pc->len--; + } + else if (paren) { + + paren++; + pc->len += (pc->p - paren); + pc->p = paren; + } +#ifndef EXPRSUGAR_BRACKET + if (*pc->tstart == '(') { + pc->tt = JIM_TT_EXPRSUGAR; + } +#endif + } + pc->tend = pc->p - 1; + } + if (pc->tstart == pc->p) { + pc->p--; + pc->len++; + return JIM_ERR; + } + return JIM_OK; +} + +static int JimParseStr(struct JimParserCtx *pc) +{ + if (pc->tt == JIM_TT_SEP || pc->tt == JIM_TT_EOL || + pc->tt == JIM_TT_NONE || pc->tt == JIM_TT_STR) { + + if (*pc->p == '{') { + return JimParseBrace(pc); + } + if (*pc->p == '"') { + pc->inquote = 1; + pc->p++; + pc->len--; + + pc->missing.line = pc->tline; + } + } + pc->tstart = pc->p; + pc->tline = pc->linenr; + while (1) { + if (pc->len == 0) { + if (pc->inquote) { + pc->missing.ch = '"'; + } + pc->tend = pc->p - 1; + pc->tt = JIM_TT_ESC; + return JIM_OK; + } + switch (*pc->p) { + case '\\': + if (!pc->inquote && *(pc->p + 1) == '\n') { + pc->tend = pc->p - 1; + pc->tt = JIM_TT_ESC; + return JIM_OK; + } + if (pc->len >= 2) { + if (*(pc->p + 1) == '\n') { + pc->linenr++; + } + pc->p++; + pc->len--; + } + else if (pc->len == 1) { + + pc->missing.ch = '\\'; + } + break; + case '(': + + if (pc->len > 1 && pc->p[1] != '$') { + break; + } + + case ')': + + if (*pc->p == '(' || pc->tt == JIM_TT_VAR) { + if (pc->p == pc->tstart) { + + pc->p++; + pc->len--; + } + pc->tend = pc->p - 1; + pc->tt = JIM_TT_ESC; + return JIM_OK; + } + break; + + case '$': + case '[': + pc->tend = pc->p - 1; + pc->tt = JIM_TT_ESC; + return JIM_OK; + case ' ': + case '\t': + case '\n': + case '\r': + case '\f': + case ';': + if (!pc->inquote) { + pc->tend = pc->p - 1; + pc->tt = JIM_TT_ESC; + return JIM_OK; + } + else if (*pc->p == '\n') { + pc->linenr++; + } + break; + case '"': + if (pc->inquote) { + pc->tend = pc->p - 1; + pc->tt = JIM_TT_ESC; + pc->p++; + pc->len--; + pc->inquote = 0; + return JIM_OK; + } + break; + } + pc->p++; + pc->len--; + } + return JIM_OK; +} + +static int JimParseComment(struct JimParserCtx *pc) +{ + while (*pc->p) { + if (*pc->p == '\\') { + pc->p++; + pc->len--; + if (pc->len == 0) { + pc->missing.ch = '\\'; + return JIM_OK; + } + if (*pc->p == '\n') { + pc->linenr++; + } + } + else if (*pc->p == '\n') { + pc->p++; + pc->len--; + pc->linenr++; + break; + } + pc->p++; + pc->len--; + } + return JIM_OK; +} + + +static int xdigitval(int c) +{ + if (c >= '0' && c <= '9') + return c - '0'; + if (c >= 'a' && c <= 'f') + return c - 'a' + 10; + if (c >= 'A' && c <= 'F') + return c - 'A' + 10; + return -1; +} + +static int odigitval(int c) +{ + if (c >= '0' && c <= '7') + return c - '0'; + return -1; +} + +static int JimEscape(char *dest, const char *s, int slen) +{ + char *p = dest; + int i, len; + + for (i = 0; i < slen; i++) { + switch (s[i]) { + case '\\': + switch (s[i + 1]) { + case 'a': + *p++ = 0x7; + i++; + break; + case 'b': + *p++ = 0x8; + i++; + break; + case 'f': + *p++ = 0xc; + i++; + break; + case 'n': + *p++ = 0xa; + i++; + break; + case 'r': + *p++ = 0xd; + i++; + break; + case 't': + *p++ = 0x9; + i++; + break; + case 'u': + case 'U': + case 'x': + { + unsigned val = 0; + int k; + int maxchars = 2; + + i++; + + if (s[i] == 'U') { + maxchars = 8; + } + else if (s[i] == 'u') { + if (s[i + 1] == '{') { + maxchars = 6; + i++; + } + else { + maxchars = 4; + } + } + + for (k = 0; k < maxchars; k++) { + int c = xdigitval(s[i + k + 1]); + if (c == -1) { + break; + } + val = (val << 4) | c; + } + + if (s[i] == '{') { + if (k == 0 || val > 0x1fffff || s[i + k + 1] != '}') { + + i--; + k = 0; + } + else { + + k++; + } + } + if (k) { + + if (s[i] == 'x') { + *p++ = val; + } + else { + p += utf8_fromunicode(p, val); + } + i += k; + break; + } + + *p++ = s[i]; + } + break; + case 'v': + *p++ = 0xb; + i++; + break; + case '\0': + *p++ = '\\'; + i++; + break; + case '\n': + + *p++ = ' '; + do { + i++; + } while (s[i + 1] == ' ' || s[i + 1] == '\t'); + break; + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + + { + int val = 0; + int c = odigitval(s[i + 1]); + + val = c; + c = odigitval(s[i + 2]); + if (c == -1) { + *p++ = val; + i++; + break; + } + val = (val * 8) + c; + c = odigitval(s[i + 3]); + if (c == -1) { + *p++ = val; + i += 2; + break; + } + val = (val * 8) + c; + *p++ = val; + i += 3; + } + break; + default: + *p++ = s[i + 1]; + i++; + break; + } + break; + default: + *p++ = s[i]; + break; + } + } + len = p - dest; + *p = '\0'; + return len; +} + +static Jim_Obj *JimParserGetTokenObj(Jim_Interp *interp, struct JimParserCtx *pc) +{ + const char *start, *end; + char *token; + int len; + + start = pc->tstart; + end = pc->tend; + len = (end - start) + 1; + if (len < 0) { + len = 0; + } + token = Jim_Alloc(len + 1); + if (pc->tt != JIM_TT_ESC) { + + memcpy(token, start, len); + token[len] = '\0'; + } + else { + + len = JimEscape(token, start, len); + } + + return Jim_NewStringObjNoAlloc(interp, token, len); +} + +static int JimParseListSep(struct JimParserCtx *pc); +static int JimParseListStr(struct JimParserCtx *pc); +static int JimParseListQuote(struct JimParserCtx *pc); + +static int JimParseList(struct JimParserCtx *pc) +{ + if (isspace(UCHAR(*pc->p))) { + return JimParseListSep(pc); + } + switch (*pc->p) { + case '"': + return JimParseListQuote(pc); + + case '{': + return JimParseBrace(pc); + + default: + if (pc->len) { + return JimParseListStr(pc); + } + break; + } + + pc->tstart = pc->tend = pc->p; + pc->tline = pc->linenr; + pc->tt = JIM_TT_EOL; + pc->eof = 1; + return JIM_OK; +} + +static int JimParseListSep(struct JimParserCtx *pc) +{ + pc->tstart = pc->p; + pc->tline = pc->linenr; + while (isspace(UCHAR(*pc->p))) { + if (*pc->p == '\n') { + pc->linenr++; + } + pc->p++; + pc->len--; + } + pc->tend = pc->p - 1; + pc->tt = JIM_TT_SEP; + return JIM_OK; +} + +static int JimParseListQuote(struct JimParserCtx *pc) +{ + pc->p++; + pc->len--; + + pc->tstart = pc->p; + pc->tline = pc->linenr; + pc->tt = JIM_TT_STR; + + while (pc->len) { + switch (*pc->p) { + case '\\': + pc->tt = JIM_TT_ESC; + if (--pc->len == 0) { + + pc->tend = pc->p; + return JIM_OK; + } + pc->p++; + break; + case '\n': + pc->linenr++; + break; + case '"': + pc->tend = pc->p - 1; + pc->p++; + pc->len--; + return JIM_OK; + } + pc->p++; + pc->len--; + } + + pc->tend = pc->p - 1; + return JIM_OK; +} + +static int JimParseListStr(struct JimParserCtx *pc) +{ + pc->tstart = pc->p; + pc->tline = pc->linenr; + pc->tt = JIM_TT_STR; + + while (pc->len) { + if (isspace(UCHAR(*pc->p))) { + pc->tend = pc->p - 1; + return JIM_OK; + } + if (*pc->p == '\\') { + if (--pc->len == 0) { + + pc->tend = pc->p; + return JIM_OK; + } + pc->tt = JIM_TT_ESC; + pc->p++; + } + pc->p++; + pc->len--; + } + pc->tend = pc->p - 1; + return JIM_OK; +} + + + +Jim_Obj *Jim_NewObj(Jim_Interp *interp) +{ + Jim_Obj *objPtr; + + + if (interp->freeList != NULL) { + + objPtr = interp->freeList; + interp->freeList = objPtr->nextObjPtr; + } + else { + + objPtr = Jim_Alloc(sizeof(*objPtr)); + } + + objPtr->refCount = 0; + + + objPtr->prevObjPtr = NULL; + objPtr->nextObjPtr = interp->liveList; + if (interp->liveList) + interp->liveList->prevObjPtr = objPtr; + interp->liveList = objPtr; + + return objPtr; +} + +void Jim_FreeObj(Jim_Interp *interp, Jim_Obj *objPtr) +{ + + JimPanic((objPtr->refCount != 0, "!!!Object %p freed with bad refcount %d, type=%s", objPtr, + objPtr->refCount, objPtr->typePtr ? objPtr->typePtr->name : "")); + + + Jim_FreeIntRep(interp, objPtr); + + if (objPtr->bytes != NULL) { + if (objPtr->bytes != JimEmptyStringRep) + Jim_Free(objPtr->bytes); + } + + if (objPtr->prevObjPtr) + objPtr->prevObjPtr->nextObjPtr = objPtr->nextObjPtr; + if (objPtr->nextObjPtr) + objPtr->nextObjPtr->prevObjPtr = objPtr->prevObjPtr; + if (interp->liveList == objPtr) + interp->liveList = objPtr->nextObjPtr; +#ifdef JIM_DISABLE_OBJECT_POOL + Jim_Free(objPtr); +#else + + objPtr->prevObjPtr = NULL; + objPtr->nextObjPtr = interp->freeList; + if (interp->freeList) + interp->freeList->prevObjPtr = objPtr; + interp->freeList = objPtr; + objPtr->refCount = -1; +#endif +} + + +void Jim_InvalidateStringRep(Jim_Obj *objPtr) +{ + if (objPtr->bytes != NULL) { + if (objPtr->bytes != JimEmptyStringRep) + Jim_Free(objPtr->bytes); + } + objPtr->bytes = NULL; +} + + +Jim_Obj *Jim_DuplicateObj(Jim_Interp *interp, Jim_Obj *objPtr) +{ + Jim_Obj *dupPtr; + + dupPtr = Jim_NewObj(interp); + if (objPtr->bytes == NULL) { + + dupPtr->bytes = NULL; + } + else if (objPtr->length == 0) { + dupPtr->bytes = JimEmptyStringRep; + dupPtr->length = 0; + dupPtr->typePtr = NULL; + return dupPtr; + } + else { + dupPtr->bytes = Jim_Alloc(objPtr->length + 1); + dupPtr->length = objPtr->length; + + memcpy(dupPtr->bytes, objPtr->bytes, objPtr->length + 1); + } + + + dupPtr->typePtr = objPtr->typePtr; + if (objPtr->typePtr != NULL) { + if (objPtr->typePtr->dupIntRepProc == NULL) { + dupPtr->internalRep = objPtr->internalRep; + } + else { + + objPtr->typePtr->dupIntRepProc(interp, objPtr, dupPtr); + } + } + return dupPtr; +} + +const char *Jim_GetString(Jim_Obj *objPtr, int *lenPtr) +{ + if (objPtr->bytes == NULL) { + + JimPanic((objPtr->typePtr->updateStringProc == NULL, "UpdateStringProc called against '%s' type.", objPtr->typePtr->name)); + objPtr->typePtr->updateStringProc(objPtr); + } + if (lenPtr) + *lenPtr = objPtr->length; + return objPtr->bytes; +} + + +int Jim_Length(Jim_Obj *objPtr) +{ + if (objPtr->bytes == NULL) { + + Jim_GetString(objPtr, NULL); + } + return objPtr->length; +} + + +const char *Jim_String(Jim_Obj *objPtr) +{ + if (objPtr->bytes == NULL) { + + Jim_GetString(objPtr, NULL); + } + return objPtr->bytes; +} + +static void JimSetStringBytes(Jim_Obj *objPtr, const char *str) +{ + objPtr->bytes = Jim_StrDup(str); + objPtr->length = strlen(str); +} + +static void FreeDictSubstInternalRep(Jim_Interp *interp, Jim_Obj *objPtr); +static void DupDictSubstInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr); + +static const Jim_ObjType dictSubstObjType = { + "dict-substitution", + FreeDictSubstInternalRep, + DupDictSubstInternalRep, + NULL, + JIM_TYPE_NONE, +}; + +static void FreeInterpolatedInternalRep(Jim_Interp *interp, Jim_Obj *objPtr); +static void DupInterpolatedInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr); + +static const Jim_ObjType interpolatedObjType = { + "interpolated", + FreeInterpolatedInternalRep, + DupInterpolatedInternalRep, + NULL, + JIM_TYPE_NONE, +}; + +static void FreeInterpolatedInternalRep(Jim_Interp *interp, Jim_Obj *objPtr) +{ + Jim_DecrRefCount(interp, objPtr->internalRep.dictSubstValue.indexObjPtr); +} + +static void DupInterpolatedInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr) +{ + + dupPtr->internalRep = srcPtr->internalRep; + + Jim_IncrRefCount(dupPtr->internalRep.dictSubstValue.indexObjPtr); +} + +static void DupStringInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr); +static int SetStringFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr); + +static const Jim_ObjType stringObjType = { + "string", + NULL, + DupStringInternalRep, + NULL, + JIM_TYPE_REFERENCES, +}; + +static void DupStringInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr) +{ + JIM_NOTUSED(interp); + + dupPtr->internalRep.strValue.maxLength = srcPtr->length; + dupPtr->internalRep.strValue.charLength = srcPtr->internalRep.strValue.charLength; +} + +static int SetStringFromAny(Jim_Interp *interp, Jim_Obj *objPtr) +{ + if (objPtr->typePtr != &stringObjType) { + + if (objPtr->bytes == NULL) { + + JimPanic((objPtr->typePtr->updateStringProc == NULL, "UpdateStringProc called against '%s' type.", objPtr->typePtr->name)); + objPtr->typePtr->updateStringProc(objPtr); + } + + Jim_FreeIntRep(interp, objPtr); + + objPtr->typePtr = &stringObjType; + objPtr->internalRep.strValue.maxLength = objPtr->length; + + objPtr->internalRep.strValue.charLength = -1; + } + return JIM_OK; +} + +int Jim_Utf8Length(Jim_Interp *interp, Jim_Obj *objPtr) +{ +#ifdef JIM_UTF8 + SetStringFromAny(interp, objPtr); + + if (objPtr->internalRep.strValue.charLength < 0) { + objPtr->internalRep.strValue.charLength = utf8_strlen(objPtr->bytes, objPtr->length); + } + return objPtr->internalRep.strValue.charLength; +#else + return Jim_Length(objPtr); +#endif +} + + +Jim_Obj *Jim_NewStringObj(Jim_Interp *interp, const char *s, int len) +{ + Jim_Obj *objPtr = Jim_NewObj(interp); + + + if (len == -1) + len = strlen(s); + + if (len == 0) { + objPtr->bytes = JimEmptyStringRep; + } + else { + objPtr->bytes = Jim_StrDupLen(s, len); + } + objPtr->length = len; + + + objPtr->typePtr = NULL; + return objPtr; +} + + +Jim_Obj *Jim_NewStringObjUtf8(Jim_Interp *interp, const char *s, int charlen) +{ +#ifdef JIM_UTF8 + + int bytelen = utf8_index(s, charlen); + + Jim_Obj *objPtr = Jim_NewStringObj(interp, s, bytelen); + + + objPtr->typePtr = &stringObjType; + objPtr->internalRep.strValue.maxLength = bytelen; + objPtr->internalRep.strValue.charLength = charlen; + + return objPtr; +#else + return Jim_NewStringObj(interp, s, charlen); +#endif +} + +Jim_Obj *Jim_NewStringObjNoAlloc(Jim_Interp *interp, char *s, int len) +{ + Jim_Obj *objPtr = Jim_NewObj(interp); + + objPtr->bytes = s; + objPtr->length = (len == -1) ? strlen(s) : len; + objPtr->typePtr = NULL; + return objPtr; +} + +static void StringAppendString(Jim_Obj *objPtr, const char *str, int len) +{ + int needlen; + + if (len == -1) + len = strlen(str); + needlen = objPtr->length + len; + if (objPtr->internalRep.strValue.maxLength < needlen || + objPtr->internalRep.strValue.maxLength == 0) { + needlen *= 2; + + if (needlen < 7) { + needlen = 7; + } + if (objPtr->bytes == JimEmptyStringRep) { + objPtr->bytes = Jim_Alloc(needlen + 1); + } + else { + objPtr->bytes = Jim_Realloc(objPtr->bytes, needlen + 1); + } + objPtr->internalRep.strValue.maxLength = needlen; + } + memcpy(objPtr->bytes + objPtr->length, str, len); + objPtr->bytes[objPtr->length + len] = '\0'; + + if (objPtr->internalRep.strValue.charLength >= 0) { + + objPtr->internalRep.strValue.charLength += utf8_strlen(objPtr->bytes + objPtr->length, len); + } + objPtr->length += len; +} + +void Jim_AppendString(Jim_Interp *interp, Jim_Obj *objPtr, const char *str, int len) +{ + JimPanic((Jim_IsShared(objPtr), "Jim_AppendString called with shared object")); + SetStringFromAny(interp, objPtr); + StringAppendString(objPtr, str, len); +} + +void Jim_AppendObj(Jim_Interp *interp, Jim_Obj *objPtr, Jim_Obj *appendObjPtr) +{ + int len; + const char *str = Jim_GetString(appendObjPtr, &len); + Jim_AppendString(interp, objPtr, str, len); +} + +void Jim_AppendStrings(Jim_Interp *interp, Jim_Obj *objPtr, ...) +{ + va_list ap; + + SetStringFromAny(interp, objPtr); + va_start(ap, objPtr); + while (1) { + const char *s = va_arg(ap, const char *); + + if (s == NULL) + break; + Jim_AppendString(interp, objPtr, s, -1); + } + va_end(ap); +} + +int Jim_StringEqObj(Jim_Obj *aObjPtr, Jim_Obj *bObjPtr) +{ + if (aObjPtr == bObjPtr) { + return 1; + } + else { + int Alen, Blen; + const char *sA = Jim_GetString(aObjPtr, &Alen); + const char *sB = Jim_GetString(bObjPtr, &Blen); + + return Alen == Blen && memcmp(sA, sB, Alen) == 0; + } +} + +int Jim_StringMatchObj(Jim_Interp *interp, Jim_Obj *patternObjPtr, Jim_Obj *objPtr, int nocase) +{ + int plen, slen; + const char *pattern = Jim_GetString(patternObjPtr, &plen); + const char *string = Jim_GetString(objPtr, &slen); + return JimGlobMatch(pattern, plen, string, slen, nocase); +} + +int Jim_StringCompareObj(Jim_Interp *interp, Jim_Obj *firstObjPtr, Jim_Obj *secondObjPtr, int nocase) +{ + const char *s1 = Jim_String(firstObjPtr); + int l1 = Jim_Utf8Length(interp, firstObjPtr); + const char *s2 = Jim_String(secondObjPtr); + int l2 = Jim_Utf8Length(interp, secondObjPtr); + return JimStringCompareUtf8(s1, l1, s2, l2, nocase); +} + +static int JimRelToAbsIndex(int len, int idx) +{ + if (idx < 0 && idx > -INT_MAX) + return len + idx; + return idx; +} + +static void JimRelToAbsRange(int len, int *firstPtr, int *lastPtr, int *rangeLenPtr) +{ + int rangeLen; + + if (*firstPtr > *lastPtr) { + rangeLen = 0; + } + else { + rangeLen = *lastPtr - *firstPtr + 1; + if (rangeLen) { + if (*firstPtr < 0) { + rangeLen += *firstPtr; + *firstPtr = 0; + } + if (*lastPtr >= len) { + rangeLen -= (*lastPtr - (len - 1)); + *lastPtr = len - 1; + } + } + } + if (rangeLen < 0) + rangeLen = 0; + + *rangeLenPtr = rangeLen; +} + +static int JimStringGetRange(Jim_Interp *interp, Jim_Obj *firstObjPtr, Jim_Obj *lastObjPtr, + int len, int *first, int *last, int *range) +{ + if (Jim_GetIndex(interp, firstObjPtr, first) != JIM_OK) { + return JIM_ERR; + } + if (Jim_GetIndex(interp, lastObjPtr, last) != JIM_OK) { + return JIM_ERR; + } + *first = JimRelToAbsIndex(len, *first); + *last = JimRelToAbsIndex(len, *last); + JimRelToAbsRange(len, first, last, range); + return JIM_OK; +} + +Jim_Obj *Jim_StringByteRangeObj(Jim_Interp *interp, + Jim_Obj *strObjPtr, Jim_Obj *firstObjPtr, Jim_Obj *lastObjPtr) +{ + int first, last; + const char *str; + int rangeLen; + int bytelen; + + str = Jim_GetString(strObjPtr, &bytelen); + + if (JimStringGetRange(interp, firstObjPtr, lastObjPtr, bytelen, &first, &last, &rangeLen) != JIM_OK) { + return NULL; + } + + if (first == 0 && rangeLen == bytelen) { + return strObjPtr; + } + return Jim_NewStringObj(interp, str + first, rangeLen); +} + +Jim_Obj *Jim_StringRangeObj(Jim_Interp *interp, + Jim_Obj *strObjPtr, Jim_Obj *firstObjPtr, Jim_Obj *lastObjPtr) +{ +#ifdef JIM_UTF8 + int first, last; + const char *str; + int len, rangeLen; + int bytelen; + + str = Jim_GetString(strObjPtr, &bytelen); + len = Jim_Utf8Length(interp, strObjPtr); + + if (JimStringGetRange(interp, firstObjPtr, lastObjPtr, len, &first, &last, &rangeLen) != JIM_OK) { + return NULL; + } + + if (first == 0 && rangeLen == len) { + return strObjPtr; + } + if (len == bytelen) { + + return Jim_NewStringObj(interp, str + first, rangeLen); + } + return Jim_NewStringObjUtf8(interp, str + utf8_index(str, first), rangeLen); +#else + return Jim_StringByteRangeObj(interp, strObjPtr, firstObjPtr, lastObjPtr); +#endif +} + +Jim_Obj *JimStringReplaceObj(Jim_Interp *interp, + Jim_Obj *strObjPtr, Jim_Obj *firstObjPtr, Jim_Obj *lastObjPtr, Jim_Obj *newStrObj) +{ + int first, last; + const char *str; + int len, rangeLen; + Jim_Obj *objPtr; + + len = Jim_Utf8Length(interp, strObjPtr); + + if (JimStringGetRange(interp, firstObjPtr, lastObjPtr, len, &first, &last, &rangeLen) != JIM_OK) { + return NULL; + } + + if (last < first) { + return strObjPtr; + } + + str = Jim_String(strObjPtr); + + + objPtr = Jim_NewStringObjUtf8(interp, str, first); + + + if (newStrObj) { + Jim_AppendObj(interp, objPtr, newStrObj); + } + + + Jim_AppendString(interp, objPtr, str + utf8_index(str, last + 1), len - last - 1); + + return objPtr; +} + +static void JimStrCopyUpperLower(char *dest, const char *str, int uc) +{ + while (*str) { + int c; + str += utf8_tounicode(str, &c); + dest += utf8_getchars(dest, uc ? utf8_upper(c) : utf8_lower(c)); + } + *dest = 0; +} + +static Jim_Obj *JimStringToLower(Jim_Interp *interp, Jim_Obj *strObjPtr) +{ + char *buf; + int len; + const char *str; + + str = Jim_GetString(strObjPtr, &len); + +#ifdef JIM_UTF8 + len *= 2; +#endif + buf = Jim_Alloc(len + 1); + JimStrCopyUpperLower(buf, str, 0); + return Jim_NewStringObjNoAlloc(interp, buf, -1); +} + +static Jim_Obj *JimStringToUpper(Jim_Interp *interp, Jim_Obj *strObjPtr) +{ + char *buf; + const char *str; + int len; + + str = Jim_GetString(strObjPtr, &len); + +#ifdef JIM_UTF8 + len *= 2; +#endif + buf = Jim_Alloc(len + 1); + JimStrCopyUpperLower(buf, str, 1); + return Jim_NewStringObjNoAlloc(interp, buf, -1); +} + +static Jim_Obj *JimStringToTitle(Jim_Interp *interp, Jim_Obj *strObjPtr) +{ + char *buf, *p; + int len; + int c; + const char *str; + + str = Jim_GetString(strObjPtr, &len); + +#ifdef JIM_UTF8 + len *= 2; +#endif + buf = p = Jim_Alloc(len + 1); + + str += utf8_tounicode(str, &c); + p += utf8_getchars(p, utf8_title(c)); + + JimStrCopyUpperLower(p, str, 0); + + return Jim_NewStringObjNoAlloc(interp, buf, -1); +} + +static const char *utf8_memchr(const char *str, int len, int c) +{ +#ifdef JIM_UTF8 + while (len) { + int sc; + int n = utf8_tounicode(str, &sc); + if (sc == c) { + return str; + } + str += n; + len -= n; + } + return NULL; +#else + return memchr(str, c, len); +#endif +} + +static const char *JimFindTrimLeft(const char *str, int len, const char *trimchars, int trimlen) +{ + while (len) { + int c; + int n = utf8_tounicode(str, &c); + + if (utf8_memchr(trimchars, trimlen, c) == NULL) { + + break; + } + str += n; + len -= n; + } + return str; +} + +static const char *JimFindTrimRight(const char *str, int len, const char *trimchars, int trimlen) +{ + str += len; + + while (len) { + int c; + int n = utf8_prev_len(str, len); + + len -= n; + str -= n; + + n = utf8_tounicode(str, &c); + + if (utf8_memchr(trimchars, trimlen, c) == NULL) { + return str + n; + } + } + + return NULL; +} + +static const char default_trim_chars[] = " \t\n\r"; + +static int default_trim_chars_len = sizeof(default_trim_chars); + +static Jim_Obj *JimStringTrimLeft(Jim_Interp *interp, Jim_Obj *strObjPtr, Jim_Obj *trimcharsObjPtr) +{ + int len; + const char *str = Jim_GetString(strObjPtr, &len); + const char *trimchars = default_trim_chars; + int trimcharslen = default_trim_chars_len; + const char *newstr; + + if (trimcharsObjPtr) { + trimchars = Jim_GetString(trimcharsObjPtr, &trimcharslen); + } + + newstr = JimFindTrimLeft(str, len, trimchars, trimcharslen); + if (newstr == str) { + return strObjPtr; + } + + return Jim_NewStringObj(interp, newstr, len - (newstr - str)); +} + +static Jim_Obj *JimStringTrimRight(Jim_Interp *interp, Jim_Obj *strObjPtr, Jim_Obj *trimcharsObjPtr) +{ + int len; + const char *trimchars = default_trim_chars; + int trimcharslen = default_trim_chars_len; + const char *nontrim; + + if (trimcharsObjPtr) { + trimchars = Jim_GetString(trimcharsObjPtr, &trimcharslen); + } + + SetStringFromAny(interp, strObjPtr); + + len = Jim_Length(strObjPtr); + nontrim = JimFindTrimRight(strObjPtr->bytes, len, trimchars, trimcharslen); + + if (nontrim == NULL) { + + return Jim_NewEmptyStringObj(interp); + } + if (nontrim == strObjPtr->bytes + len) { + + return strObjPtr; + } + + if (Jim_IsShared(strObjPtr)) { + strObjPtr = Jim_NewStringObj(interp, strObjPtr->bytes, (nontrim - strObjPtr->bytes)); + } + else { + + strObjPtr->bytes[nontrim - strObjPtr->bytes] = 0; + strObjPtr->length = (nontrim - strObjPtr->bytes); + } + + return strObjPtr; +} + +static Jim_Obj *JimStringTrim(Jim_Interp *interp, Jim_Obj *strObjPtr, Jim_Obj *trimcharsObjPtr) +{ + + Jim_Obj *objPtr = JimStringTrimLeft(interp, strObjPtr, trimcharsObjPtr); + + + strObjPtr = JimStringTrimRight(interp, objPtr, trimcharsObjPtr); + + + if (objPtr != strObjPtr && objPtr->refCount == 0) { + + Jim_FreeNewObj(interp, objPtr); + } + + return strObjPtr; +} + + +#ifdef HAVE_ISASCII +#define jim_isascii isascii +#else +static int jim_isascii(int c) +{ + return !(c & ~0x7f); +} +#endif + +static int JimStringIs(Jim_Interp *interp, Jim_Obj *strObjPtr, Jim_Obj *strClass, int strict) +{ + static const char * const strclassnames[] = { + "integer", "alpha", "alnum", "ascii", "digit", + "double", "lower", "upper", "space", "xdigit", + "control", "print", "graph", "punct", "boolean", + NULL + }; + enum { + STR_IS_INTEGER, STR_IS_ALPHA, STR_IS_ALNUM, STR_IS_ASCII, STR_IS_DIGIT, + STR_IS_DOUBLE, STR_IS_LOWER, STR_IS_UPPER, STR_IS_SPACE, STR_IS_XDIGIT, + STR_IS_CONTROL, STR_IS_PRINT, STR_IS_GRAPH, STR_IS_PUNCT, STR_IS_BOOLEAN, + }; + int strclass; + int len; + int i; + const char *str; + int (*isclassfunc)(int c) = NULL; + + if (Jim_GetEnum(interp, strClass, strclassnames, &strclass, "class", JIM_ERRMSG | JIM_ENUM_ABBREV) != JIM_OK) { + return JIM_ERR; + } + + str = Jim_GetString(strObjPtr, &len); + if (len == 0) { + Jim_SetResultBool(interp, !strict); + return JIM_OK; + } + + switch (strclass) { + case STR_IS_INTEGER: + { + jim_wide w; + Jim_SetResultBool(interp, JimGetWideNoErr(interp, strObjPtr, &w) == JIM_OK); + return JIM_OK; + } + + case STR_IS_DOUBLE: + { + double d; + Jim_SetResultBool(interp, Jim_GetDouble(interp, strObjPtr, &d) == JIM_OK && errno != ERANGE); + return JIM_OK; + } + + case STR_IS_BOOLEAN: + { + int b; + Jim_SetResultBool(interp, Jim_GetBoolean(interp, strObjPtr, &b) == JIM_OK); + return JIM_OK; + } + + case STR_IS_ALPHA: isclassfunc = isalpha; break; + case STR_IS_ALNUM: isclassfunc = isalnum; break; + case STR_IS_ASCII: isclassfunc = jim_isascii; break; + case STR_IS_DIGIT: isclassfunc = isdigit; break; + case STR_IS_LOWER: isclassfunc = islower; break; + case STR_IS_UPPER: isclassfunc = isupper; break; + case STR_IS_SPACE: isclassfunc = isspace; break; + case STR_IS_XDIGIT: isclassfunc = isxdigit; break; + case STR_IS_CONTROL: isclassfunc = iscntrl; break; + case STR_IS_PRINT: isclassfunc = isprint; break; + case STR_IS_GRAPH: isclassfunc = isgraph; break; + case STR_IS_PUNCT: isclassfunc = ispunct; break; + default: + return JIM_ERR; + } + + for (i = 0; i < len; i++) { + if (!isclassfunc(UCHAR(str[i]))) { + Jim_SetResultBool(interp, 0); + return JIM_OK; + } + } + Jim_SetResultBool(interp, 1); + return JIM_OK; +} + + + +static const Jim_ObjType comparedStringObjType = { + "compared-string", + NULL, + NULL, + NULL, + JIM_TYPE_REFERENCES, +}; + +int Jim_CompareStringImmediate(Jim_Interp *interp, Jim_Obj *objPtr, const char *str) +{ + if (objPtr->typePtr == &comparedStringObjType && objPtr->internalRep.ptr == str) { + return 1; + } + else { + if (strcmp(str, Jim_String(objPtr)) != 0) + return 0; + + if (objPtr->typePtr != &comparedStringObjType) { + Jim_FreeIntRep(interp, objPtr); + objPtr->typePtr = &comparedStringObjType; + } + objPtr->internalRep.ptr = (char *)str; + return 1; + } +} + +static int qsortCompareStringPointers(const void *a, const void *b) +{ + char *const *sa = (char *const *)a; + char *const *sb = (char *const *)b; + + return strcmp(*sa, *sb); +} + + + +static void FreeSourceInternalRep(Jim_Interp *interp, Jim_Obj *objPtr); +static void DupSourceInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr); + +static const Jim_ObjType sourceObjType = { + "source", + FreeSourceInternalRep, + DupSourceInternalRep, + NULL, + JIM_TYPE_REFERENCES, +}; + +void FreeSourceInternalRep(Jim_Interp *interp, Jim_Obj *objPtr) +{ + Jim_DecrRefCount(interp, objPtr->internalRep.sourceValue.fileNameObj); +} + +void DupSourceInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr) +{ + dupPtr->internalRep.sourceValue = srcPtr->internalRep.sourceValue; + Jim_IncrRefCount(dupPtr->internalRep.sourceValue.fileNameObj); +} + +static const Jim_ObjType scriptLineObjType = { + "scriptline", + NULL, + NULL, + NULL, + JIM_NONE, +}; + +static Jim_Obj *JimNewScriptLineObj(Jim_Interp *interp, int argc, int line) +{ + Jim_Obj *objPtr; + +#ifdef DEBUG_SHOW_SCRIPT + char buf[100]; + snprintf(buf, sizeof(buf), "line=%d, argc=%d", line, argc); + objPtr = Jim_NewStringObj(interp, buf, -1); +#else + objPtr = Jim_NewEmptyStringObj(interp); +#endif + objPtr->typePtr = &scriptLineObjType; + objPtr->internalRep.scriptLineValue.argc = argc; + objPtr->internalRep.scriptLineValue.line = line; + + return objPtr; +} + +static void FreeScriptInternalRep(Jim_Interp *interp, Jim_Obj *objPtr); +static void DupScriptInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr); + +static const Jim_ObjType scriptObjType = { + "script", + FreeScriptInternalRep, + DupScriptInternalRep, + NULL, + JIM_TYPE_NONE, +}; + +typedef struct ScriptToken +{ + Jim_Obj *objPtr; + int type; +} ScriptToken; + +typedef struct ScriptObj +{ + ScriptToken *token; + Jim_Obj *fileNameObj; + int len; + int substFlags; + int inUse; /* Used to share a ScriptObj. Currently + only used by Jim_EvalObj() as protection against + shimmering of the currently evaluated object. */ + int firstline; + int linenr; + int missing; +} ScriptObj; + +static void JimSetScriptFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr); +static int JimParseCheckMissing(Jim_Interp *interp, int ch); +static ScriptObj *JimGetScript(Jim_Interp *interp, Jim_Obj *objPtr); +static void JimSetErrorStack(Jim_Interp *interp, ScriptObj *script); + +void FreeScriptInternalRep(Jim_Interp *interp, Jim_Obj *objPtr) +{ + int i; + struct ScriptObj *script = (void *)objPtr->internalRep.ptr; + + if (--script->inUse != 0) + return; + for (i = 0; i < script->len; i++) { + Jim_DecrRefCount(interp, script->token[i].objPtr); + } + Jim_Free(script->token); + Jim_DecrRefCount(interp, script->fileNameObj); + Jim_Free(script); +} + +void DupScriptInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr) +{ + JIM_NOTUSED(interp); + JIM_NOTUSED(srcPtr); + + dupPtr->typePtr = NULL; +} + +typedef struct +{ + const char *token; + int len; + int type; + int line; +} ParseToken; + +typedef struct +{ + + ParseToken *list; + int size; + int count; + ParseToken static_list[20]; +} ParseTokenList; + +static void ScriptTokenListInit(ParseTokenList *tokenlist) +{ + tokenlist->list = tokenlist->static_list; + tokenlist->size = sizeof(tokenlist->static_list) / sizeof(ParseToken); + tokenlist->count = 0; +} + +static void ScriptTokenListFree(ParseTokenList *tokenlist) +{ + if (tokenlist->list != tokenlist->static_list) { + Jim_Free(tokenlist->list); + } +} + +static void ScriptAddToken(ParseTokenList *tokenlist, const char *token, int len, int type, + int line) +{ + ParseToken *t; + + if (tokenlist->count == tokenlist->size) { + + tokenlist->size *= 2; + if (tokenlist->list != tokenlist->static_list) { + tokenlist->list = + Jim_Realloc(tokenlist->list, tokenlist->size * sizeof(*tokenlist->list)); + } + else { + + tokenlist->list = Jim_Alloc(tokenlist->size * sizeof(*tokenlist->list)); + memcpy(tokenlist->list, tokenlist->static_list, + tokenlist->count * sizeof(*tokenlist->list)); + } + } + t = &tokenlist->list[tokenlist->count++]; + t->token = token; + t->len = len; + t->type = type; + t->line = line; +} + +static int JimCountWordTokens(struct ScriptObj *script, ParseToken *t) +{ + int expand = 1; + int count = 0; + + + if (t->type == JIM_TT_STR && !TOKEN_IS_SEP(t[1].type)) { + if ((t->len == 1 && *t->token == '*') || (t->len == 6 && strncmp(t->token, "expand", 6) == 0)) { + + expand = -1; + t++; + } + else { + if (script->missing == ' ') { + + script->missing = '}'; + script->linenr = t[1].line; + } + } + } + + + while (!TOKEN_IS_SEP(t->type)) { + t++; + count++; + } + + return count * expand; +} + +static Jim_Obj *JimMakeScriptObj(Jim_Interp *interp, const ParseToken *t) +{ + Jim_Obj *objPtr; + + if (t->type == JIM_TT_ESC && memchr(t->token, '\\', t->len) != NULL) { + + int len = t->len; + char *str = Jim_Alloc(len + 1); + len = JimEscape(str, t->token, len); + objPtr = Jim_NewStringObjNoAlloc(interp, str, len); + } + else { + objPtr = Jim_NewStringObj(interp, t->token, t->len); + } + return objPtr; +} + +static void ScriptObjAddTokens(Jim_Interp *interp, struct ScriptObj *script, + ParseTokenList *tokenlist) +{ + int i; + struct ScriptToken *token; + + int lineargs = 0; + + ScriptToken *linefirst; + int count; + int linenr; + +#ifdef DEBUG_SHOW_SCRIPT_TOKENS + printf("==== Tokens ====\n"); + for (i = 0; i < tokenlist->count; i++) { + printf("[%2d]@%d %s '%.*s'\n", i, tokenlist->list[i].line, jim_tt_name(tokenlist->list[i].type), + tokenlist->list[i].len, tokenlist->list[i].token); + } +#endif + + + count = tokenlist->count; + for (i = 0; i < tokenlist->count; i++) { + if (tokenlist->list[i].type == JIM_TT_EOL) { + count++; + } + } + linenr = script->firstline = tokenlist->list[0].line; + + token = script->token = Jim_Alloc(sizeof(ScriptToken) * count); + + + linefirst = token++; + + for (i = 0; i < tokenlist->count; ) { + + int wordtokens; + + + while (tokenlist->list[i].type == JIM_TT_SEP) { + i++; + } + + wordtokens = JimCountWordTokens(script, tokenlist->list + i); + + if (wordtokens == 0) { + + if (lineargs) { + linefirst->type = JIM_TT_LINE; + linefirst->objPtr = JimNewScriptLineObj(interp, lineargs, linenr); + Jim_IncrRefCount(linefirst->objPtr); + + + lineargs = 0; + linefirst = token++; + } + i++; + continue; + } + else if (wordtokens != 1) { + + token->type = JIM_TT_WORD; + token->objPtr = Jim_NewIntObj(interp, wordtokens); + Jim_IncrRefCount(token->objPtr); + token++; + if (wordtokens < 0) { + + i++; + wordtokens = -wordtokens - 1; + lineargs--; + } + } + + if (lineargs == 0) { + + linenr = tokenlist->list[i].line; + } + lineargs++; + + + while (wordtokens--) { + const ParseToken *t = &tokenlist->list[i++]; + + token->type = t->type; + token->objPtr = JimMakeScriptObj(interp, t); + Jim_IncrRefCount(token->objPtr); + + Jim_SetSourceInfo(interp, token->objPtr, script->fileNameObj, t->line); + token++; + } + } + + if (lineargs == 0) { + token--; + } + + script->len = token - script->token; + + JimPanic((script->len >= count, "allocated script array is too short")); + +#ifdef DEBUG_SHOW_SCRIPT + printf("==== Script (%s) ====\n", Jim_String(script->fileNameObj)); + for (i = 0; i < script->len; i++) { + const ScriptToken *t = &script->token[i]; + printf("[%2d] %s %s\n", i, jim_tt_name(t->type), Jim_String(t->objPtr)); + } +#endif + +} + +int Jim_ScriptIsComplete(Jim_Interp *interp, Jim_Obj *scriptObj, char *stateCharPtr) +{ + ScriptObj *script = JimGetScript(interp, scriptObj); + if (stateCharPtr) { + *stateCharPtr = script->missing; + } + return script->missing == ' ' || script->missing == '}'; +} + +static int JimParseCheckMissing(Jim_Interp *interp, int ch) +{ + const char *msg; + + switch (ch) { + case '\\': + case ' ': + return JIM_OK; + + case '[': + msg = "unmatched \"[\""; + break; + case '{': + msg = "missing close-brace"; + break; + case '}': + msg = "extra characters after close-brace"; + break; + case '"': + default: + msg = "missing quote"; + break; + } + + Jim_SetResultString(interp, msg, -1); + return JIM_ERR; +} + +Jim_Obj *Jim_GetSourceInfo(Jim_Interp *interp, Jim_Obj *objPtr, int *lineptr) +{ + int line; + Jim_Obj *fileNameObj; + + if (objPtr->typePtr == &sourceObjType) { + fileNameObj = objPtr->internalRep.sourceValue.fileNameObj; + line = objPtr->internalRep.sourceValue.lineNumber; + } + else if (objPtr->typePtr == &scriptObjType) { + ScriptObj *script = JimGetScript(interp, objPtr); + fileNameObj = script->fileNameObj; + line = script->firstline; + } + else { + fileNameObj = interp->emptyObj; + line = 1; + } + *lineptr = line; + return fileNameObj; +} + +void Jim_SetSourceInfo(Jim_Interp *interp, Jim_Obj *objPtr, + Jim_Obj *fileNameObj, int lineNumber) +{ + JimPanic((Jim_IsShared(objPtr), "Jim_SetSourceInfo called with shared object")); + Jim_FreeIntRep(interp, objPtr); + Jim_IncrRefCount(fileNameObj); + objPtr->internalRep.sourceValue.fileNameObj = fileNameObj; + objPtr->internalRep.sourceValue.lineNumber = lineNumber; + objPtr->typePtr = &sourceObjType; +} + +static void SubstObjAddTokens(Jim_Interp *interp, struct ScriptObj *script, + ParseTokenList *tokenlist) +{ + int i; + struct ScriptToken *token; + + token = script->token = Jim_Alloc(sizeof(ScriptToken) * tokenlist->count); + + for (i = 0; i < tokenlist->count; i++) { + const ParseToken *t = &tokenlist->list[i]; + + + token->type = t->type; + token->objPtr = JimMakeScriptObj(interp, t); + Jim_IncrRefCount(token->objPtr); + token++; + } + + script->len = i; +} + +static void JimSetScriptFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr) +{ + int scriptTextLen; + const char *scriptText = Jim_GetString(objPtr, &scriptTextLen); + struct JimParserCtx parser; + struct ScriptObj *script; + ParseTokenList tokenlist; + Jim_Obj *fileNameObj; + int line; + + + fileNameObj = Jim_GetSourceInfo(interp, objPtr, &line); + + + ScriptTokenListInit(&tokenlist); + + JimParserInit(&parser, scriptText, scriptTextLen, line); + while (!parser.eof) { + JimParseScript(&parser); + ScriptAddToken(&tokenlist, parser.tstart, parser.tend - parser.tstart + 1, parser.tt, + parser.tline); + } + + + ScriptAddToken(&tokenlist, scriptText + scriptTextLen, 0, JIM_TT_EOF, 0); + + + script = Jim_Alloc(sizeof(*script)); + memset(script, 0, sizeof(*script)); + script->inUse = 1; + script->fileNameObj = fileNameObj; + Jim_IncrRefCount(script->fileNameObj); + script->missing = parser.missing.ch; + script->linenr = parser.missing.line; + + ScriptObjAddTokens(interp, script, &tokenlist); + + + ScriptTokenListFree(&tokenlist); + + + Jim_FreeIntRep(interp, objPtr); + Jim_SetIntRepPtr(objPtr, script); + objPtr->typePtr = &scriptObjType; +} + +static ScriptObj *JimGetScript(Jim_Interp *interp, Jim_Obj *objPtr) +{ + if (objPtr == interp->emptyObj) { + + objPtr = interp->nullScriptObj; + } + + if (objPtr->typePtr != &scriptObjType || ((struct ScriptObj *)Jim_GetIntRepPtr(objPtr))->substFlags) { + JimSetScriptFromAny(interp, objPtr); + } + + return (ScriptObj *)Jim_GetIntRepPtr(objPtr); +} + +void Jim_InterpIncrProcEpoch(Jim_Interp *interp) +{ + interp->procEpoch++; + + + while (interp->oldCmdCache) { + Jim_Cmd *next = interp->oldCmdCache->prevCmd; + Jim_Free(interp->oldCmdCache); + interp->oldCmdCache = next; + } + interp->oldCmdCacheSize = 0; +} + +static void JimIncrCmdRefCount(Jim_Cmd *cmdPtr) +{ + cmdPtr->inUse++; +} + +static void JimDecrCmdRefCount(Jim_Interp *interp, Jim_Cmd *cmdPtr) +{ + if (--cmdPtr->inUse == 0) { + if (cmdPtr->isproc) { + Jim_DecrRefCount(interp, cmdPtr->u.proc.argListObjPtr); + Jim_DecrRefCount(interp, cmdPtr->u.proc.bodyObjPtr); + Jim_DecrRefCount(interp, cmdPtr->u.proc.nsObj); + if (cmdPtr->u.proc.staticVars) { + Jim_FreeHashTable(cmdPtr->u.proc.staticVars); + Jim_Free(cmdPtr->u.proc.staticVars); + } + } + else { + + if (cmdPtr->u.native.delProc) { + cmdPtr->u.native.delProc(interp, cmdPtr->u.native.privData); + } + } + if (cmdPtr->prevCmd) { + + JimDecrCmdRefCount(interp, cmdPtr->prevCmd); + } + + cmdPtr->prevCmd = interp->oldCmdCache; + interp->oldCmdCache = cmdPtr; + if (!interp->quitting && ++interp->oldCmdCacheSize >= 1000) { + Jim_InterpIncrProcEpoch(interp); + } + } +} + +static void JimIncrVarRef(Jim_VarVal *vv) +{ + vv->refCount++; +} + +static void JimDecrVarRef(Jim_Interp *interp, Jim_VarVal *vv) +{ + assert(vv->refCount > 0); + if (--vv->refCount == 0) { + if (vv->objPtr) { + Jim_DecrRefCount(interp, vv->objPtr); + } + Jim_Free(vv); + } +} + +static void JimVariablesHTValDestructor(void *interp, void *val) +{ + JimDecrVarRef(interp, val); +} + +static unsigned int JimObjectHTHashFunction(const void *key) +{ + Jim_Obj *keyObj = (Jim_Obj *)key; + int length; + const char *string; + +#ifdef JIM_OPTIMIZATION + if (JimIsWide(keyObj) && keyObj->bytes == NULL) { + + jim_wide objValue = JimWideValue(keyObj); + if (objValue > INT_MIN && objValue < INT_MAX) { + unsigned result = 0; + unsigned value = (unsigned)objValue; + + if (objValue < 0) { + value = (unsigned)-objValue; + } + + + do { + result += (result << 3) + (value % 10 + '0'); + value /= 10; + } while (value); + + if (objValue < 0) { + result += (result << 3) + '-'; + } + return result; + } + } +#endif + string = Jim_GetString(keyObj, &length); + return Jim_GenHashFunction((const unsigned char *)string, length); +} + +static int JimObjectHTKeyCompare(void *privdata, const void *key1, const void *key2) +{ + return Jim_StringEqObj((Jim_Obj *)key1, (Jim_Obj *)key2); +} + +static void *JimObjectHTKeyValDup(void *privdata, const void *val) +{ + Jim_IncrRefCount((Jim_Obj *)val); + return (void *)val; +} + +static void JimObjectHTKeyValDestructor(void *interp, void *val) +{ + Jim_DecrRefCount(interp, (Jim_Obj *)val); +} + + +static void *JimVariablesHTValDup(void *privdata, const void *val) +{ + JimIncrVarRef((Jim_VarVal *)val); + return (void *)val; +} + +static const Jim_HashTableType JimVariablesHashTableType = { + JimObjectHTHashFunction, + JimObjectHTKeyValDup, + JimVariablesHTValDup, + JimObjectHTKeyCompare, + JimObjectHTKeyValDestructor, + JimVariablesHTValDestructor +}; + + +static const char *Jim_GetStringNoQualifier(Jim_Obj *objPtr, int *length) +{ + int len; + const char *str = Jim_GetString(objPtr, &len); + if (len >= 2 && str[0] == ':' && str[1] == ':') { + while (len && *str == ':') { + len--; + str++; + } + } + *length = len; + return str; +} + +static unsigned int JimCommandsHT_HashFunction(const void *key) +{ + int len; + const char *str = Jim_GetStringNoQualifier((Jim_Obj *)key, &len); + return Jim_GenHashFunction((const unsigned char *)str, len); +} + +static int JimCommandsHT_KeyCompare(void *privdata, const void *key1, const void *key2) +{ + int len1, len2; + const char *str1 = Jim_GetStringNoQualifier((Jim_Obj *)key1, &len1); + const char *str2 = Jim_GetStringNoQualifier((Jim_Obj *)key2, &len2); + return len1 == len2 && memcmp(str1, str2, len1) == 0; +} + +static void JimCommandsHT_ValDestructor(void *interp, void *val) +{ + JimDecrCmdRefCount(interp, val); +} + +static const Jim_HashTableType JimCommandsHashTableType = { + JimCommandsHT_HashFunction, + JimObjectHTKeyValDup, + NULL, + JimCommandsHT_KeyCompare, + JimObjectHTKeyValDestructor, + JimCommandsHT_ValDestructor +}; + + + +Jim_Obj *Jim_MakeGlobalNamespaceName(Jim_Interp *interp, Jim_Obj *nameObjPtr) +{ +#ifdef jim_ext_namespace + Jim_Obj *resultObj; + + const char *name = Jim_String(nameObjPtr); + if (name[0] == ':' && name[1] == ':') { + return nameObjPtr; + } + Jim_IncrRefCount(nameObjPtr); + resultObj = Jim_NewStringObj(interp, "::", -1); + Jim_AppendObj(interp, resultObj, nameObjPtr); + Jim_DecrRefCount(interp, nameObjPtr); + + return resultObj; +#else + return nameObjPtr; +#endif +} + +static Jim_Obj *JimQualifyName(Jim_Interp *interp, Jim_Obj *objPtr) +{ +#ifdef jim_ext_namespace + if (Jim_Length(interp->framePtr->nsObj)) { + int len; + const char *name = Jim_GetString(objPtr, &len); + if (len < 2 || name[0] != ':' || name[1] != ':') { + + objPtr = Jim_DuplicateObj(interp, interp->framePtr->nsObj); + Jim_AppendStrings(interp, objPtr, "::", name, NULL); + } + } +#endif + Jim_IncrRefCount(objPtr); + return objPtr; +} + +static void JimCreateCommand(Jim_Interp *interp, Jim_Obj *nameObjPtr, Jim_Cmd *cmd) +{ + JimPanic((nameObjPtr->refCount == 0, "JimCreateCommand called with zero ref count name")); + + if (interp->local) { + Jim_HashEntry *he = Jim_FindHashEntry(&interp->commands, nameObjPtr); + if (he) { + + cmd->prevCmd = Jim_GetHashEntryVal(he); + Jim_SetHashVal(&interp->commands, he, cmd); + + Jim_InterpIncrProcEpoch(interp); + return; + } + } + + + + Jim_ReplaceHashEntry(&interp->commands, nameObjPtr, cmd); +} + +int Jim_CreateCommandObj(Jim_Interp *interp, Jim_Obj *cmdNameObj, + Jim_CmdProc *cmdProc, void *privData, Jim_DelCmdProc *delProc) +{ + Jim_Cmd *cmdPtr = Jim_Alloc(sizeof(*cmdPtr)); + + + memset(cmdPtr, 0, sizeof(*cmdPtr)); + cmdPtr->inUse = 1; + cmdPtr->u.native.delProc = delProc; + cmdPtr->u.native.cmdProc = cmdProc; + cmdPtr->u.native.privData = privData; + + Jim_IncrRefCount(cmdNameObj); + JimCreateCommand(interp, cmdNameObj, cmdPtr); + Jim_DecrRefCount(interp, cmdNameObj); + + return JIM_OK; +} + + +int Jim_CreateCommand(Jim_Interp *interp, const char *cmdNameStr, + Jim_CmdProc *cmdProc, void *privData, Jim_DelCmdProc *delProc) +{ + return Jim_CreateCommandObj(interp, Jim_NewStringObj(interp, cmdNameStr, -1), cmdProc, privData, delProc); +} + +static int JimCreateProcedureStatics(Jim_Interp *interp, Jim_Cmd *cmdPtr, Jim_Obj *staticsListObjPtr) +{ + int len, i; + + len = Jim_ListLength(interp, staticsListObjPtr); + if (len == 0) { + return JIM_OK; + } + + cmdPtr->u.proc.staticVars = Jim_Alloc(sizeof(Jim_HashTable)); + Jim_InitHashTable(cmdPtr->u.proc.staticVars, &JimVariablesHashTableType, interp); + for (i = 0; i < len; i++) { + Jim_Obj *initObjPtr = NULL; + Jim_Obj *nameObjPtr; + Jim_VarVal *vv = NULL; + Jim_Obj *objPtr = Jim_ListGetIndex(interp, staticsListObjPtr, i); + int subLen = Jim_ListLength(interp, objPtr); + int byref = 0; + + + if (subLen != 1 && subLen != 2) { + Jim_SetResultFormatted(interp, "too many fields in static specifier \"%#s\"", + objPtr); + return JIM_ERR; + } + + nameObjPtr = Jim_ListGetIndex(interp, objPtr, 0); + + + if (subLen == 1) { + int len; + const char *pt = Jim_GetString(nameObjPtr, &len); + if (*pt == '&') { + + nameObjPtr = Jim_NewStringObj(interp, pt + 1, len - 1); + byref = 1; + } + } + Jim_IncrRefCount(nameObjPtr); + + if (subLen == 1) { + switch (SetVariableFromAny(interp, nameObjPtr)) { + case JIM_DICT_SUGAR: + + if (byref) { + Jim_SetResultFormatted(interp, "Can't link to array element \"%#s\"", nameObjPtr); + } + else { + Jim_SetResultFormatted(interp, "Can't initialise array element \"%#s\"", nameObjPtr); + } + Jim_DecrRefCount(interp, nameObjPtr); + return JIM_ERR; + + case JIM_OK: + if (byref) { + vv = nameObjPtr->internalRep.varValue.vv; + } + else { + initObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_NONE); + } + break; + + case JIM_ERR: + + Jim_SetResultFormatted(interp, + "variable for initialization of static \"%#s\" not found in the local context", + nameObjPtr); + Jim_DecrRefCount(interp, nameObjPtr); + return JIM_ERR; + } + } + else { + initObjPtr = Jim_ListGetIndex(interp, objPtr, 1); + } + + if (vv == NULL) { + vv = Jim_Alloc(sizeof(*vv)); + vv->objPtr = initObjPtr; + Jim_IncrRefCount(vv->objPtr); + vv->linkFramePtr = NULL; + vv->refCount = 0; + } + + if (JimSetNewVariable(cmdPtr->u.proc.staticVars, nameObjPtr, vv) != JIM_OK) { + Jim_SetResultFormatted(interp, + "static variable name \"%#s\" duplicated in statics list", nameObjPtr); + JimIncrVarRef(vv); + JimDecrVarRef(interp, vv); + Jim_DecrRefCount(interp, nameObjPtr); + return JIM_ERR; + } + + Jim_DecrRefCount(interp, nameObjPtr); + } + return JIM_OK; +} + + +#ifdef jim_ext_namespace +static const char *Jim_memrchr(const char *p, int c, int len) +{ + int i; + for (i = len; i > 0; i--) { + if (p[i] == c) { + return p + i; + } + } + return NULL; +} +#endif + +static void JimUpdateProcNamespace(Jim_Interp *interp, Jim_Cmd *cmdPtr, Jim_Obj *nameObjPtr) +{ +#ifdef jim_ext_namespace + if (cmdPtr->isproc) { + int len; + const char *cmdname = Jim_GetStringNoQualifier(nameObjPtr, &len); + + const char *pt = Jim_memrchr(cmdname, ':', len); + if (pt && pt != cmdname && pt[-1] == ':') { + pt++; + Jim_DecrRefCount(interp, cmdPtr->u.proc.nsObj); + cmdPtr->u.proc.nsObj = Jim_NewStringObj(interp, cmdname, pt - cmdname - 2); + Jim_IncrRefCount(cmdPtr->u.proc.nsObj); + + Jim_Obj *tempObj = Jim_NewStringObj(interp, pt, len - (pt - cmdname)); + if (Jim_FindHashEntry(&interp->commands, tempObj)) { + + Jim_InterpIncrProcEpoch(interp); + } + Jim_FreeNewObj(interp, tempObj); + } + } +#endif +} + +static Jim_Cmd *JimCreateProcedureCmd(Jim_Interp *interp, Jim_Obj *argListObjPtr, + Jim_Obj *staticsListObjPtr, Jim_Obj *bodyObjPtr, Jim_Obj *nsObj) +{ + Jim_Cmd *cmdPtr; + int argListLen; + int i; + + argListLen = Jim_ListLength(interp, argListObjPtr); + + + cmdPtr = Jim_Alloc(sizeof(*cmdPtr) + sizeof(struct Jim_ProcArg) * argListLen); + assert(cmdPtr); + memset(cmdPtr, 0, sizeof(*cmdPtr)); + cmdPtr->inUse = 1; + cmdPtr->isproc = 1; + cmdPtr->u.proc.argListObjPtr = argListObjPtr; + cmdPtr->u.proc.argListLen = argListLen; + cmdPtr->u.proc.bodyObjPtr = bodyObjPtr; + cmdPtr->u.proc.argsPos = -1; + cmdPtr->u.proc.arglist = (struct Jim_ProcArg *)(cmdPtr + 1); + cmdPtr->u.proc.nsObj = nsObj ? nsObj : interp->emptyObj; + Jim_IncrRefCount(argListObjPtr); + Jim_IncrRefCount(bodyObjPtr); + Jim_IncrRefCount(cmdPtr->u.proc.nsObj); + + + if (staticsListObjPtr && JimCreateProcedureStatics(interp, cmdPtr, staticsListObjPtr) != JIM_OK) { + goto err; + } + + + + for (i = 0; i < argListLen; i++) { + Jim_Obj *argPtr; + Jim_Obj *nameObjPtr; + Jim_Obj *defaultObjPtr; + int len; + + + argPtr = Jim_ListGetIndex(interp, argListObjPtr, i); + len = Jim_ListLength(interp, argPtr); + if (len == 0) { + Jim_SetResultString(interp, "argument with no name", -1); +err: + JimDecrCmdRefCount(interp, cmdPtr); + return NULL; + } + if (len > 2) { + Jim_SetResultFormatted(interp, "too many fields in argument specifier \"%#s\"", argPtr); + goto err; + } + + if (len == 2) { + + nameObjPtr = Jim_ListGetIndex(interp, argPtr, 0); + defaultObjPtr = Jim_ListGetIndex(interp, argPtr, 1); + } + else { + + nameObjPtr = argPtr; + defaultObjPtr = NULL; + } + + + if (Jim_CompareStringImmediate(interp, nameObjPtr, "args")) { + if (cmdPtr->u.proc.argsPos >= 0) { + Jim_SetResultString(interp, "'args' specified more than once", -1); + goto err; + } + cmdPtr->u.proc.argsPos = i; + } + else { + if (len == 2) { + cmdPtr->u.proc.optArity++; + } + else { + cmdPtr->u.proc.reqArity++; + } + } + + cmdPtr->u.proc.arglist[i].nameObjPtr = nameObjPtr; + cmdPtr->u.proc.arglist[i].defaultObjPtr = defaultObjPtr; + } + + return cmdPtr; +} + +int Jim_DeleteCommand(Jim_Interp *interp, Jim_Obj *nameObj) +{ + int ret = JIM_OK; + + nameObj = JimQualifyName(interp, nameObj); + + if (Jim_DeleteHashEntry(&interp->commands, nameObj) == JIM_ERR) { + Jim_SetResultFormatted(interp, "can't delete \"%#s\": command doesn't exist", nameObj); + ret = JIM_ERR; + } + Jim_DecrRefCount(interp, nameObj); + + return ret; +} + +int Jim_RenameCommand(Jim_Interp *interp, Jim_Obj *oldNameObj, Jim_Obj *newNameObj) +{ + int ret = JIM_ERR; + Jim_HashEntry *he; + Jim_Cmd *cmdPtr; + + if (Jim_Length(newNameObj) == 0) { + return Jim_DeleteCommand(interp, oldNameObj); + } + + + + oldNameObj = JimQualifyName(interp, oldNameObj); + newNameObj = JimQualifyName(interp, newNameObj); + + + he = Jim_FindHashEntry(&interp->commands, oldNameObj); + if (he == NULL) { + Jim_SetResultFormatted(interp, "can't rename \"%#s\": command doesn't exist", oldNameObj); + } + else if (Jim_FindHashEntry(&interp->commands, newNameObj)) { + Jim_SetResultFormatted(interp, "can't rename to \"%#s\": command already exists", newNameObj); + } + else { + cmdPtr = Jim_GetHashEntryVal(he); + if (cmdPtr->prevCmd) { + Jim_SetResultFormatted(interp, "can't rename local command \"%#s\"", oldNameObj); + } + else { + + JimIncrCmdRefCount(cmdPtr); + JimUpdateProcNamespace(interp, cmdPtr, newNameObj); + Jim_AddHashEntry(&interp->commands, newNameObj, cmdPtr); + + + Jim_DeleteHashEntry(&interp->commands, oldNameObj); + + + Jim_InterpIncrProcEpoch(interp); + + ret = JIM_OK; + } + } + + Jim_DecrRefCount(interp, oldNameObj); + Jim_DecrRefCount(interp, newNameObj); + + return ret; +} + + +static void FreeCommandInternalRep(Jim_Interp *interp, Jim_Obj *objPtr) +{ + Jim_DecrRefCount(interp, objPtr->internalRep.cmdValue.nsObj); +} + +static void DupCommandInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr) +{ + dupPtr->internalRep.cmdValue = srcPtr->internalRep.cmdValue; + dupPtr->typePtr = srcPtr->typePtr; + Jim_IncrRefCount(dupPtr->internalRep.cmdValue.nsObj); +} + +static const Jim_ObjType commandObjType = { + "command", + FreeCommandInternalRep, + DupCommandInternalRep, + NULL, + JIM_TYPE_REFERENCES, +}; + +Jim_Cmd *Jim_GetCommand(Jim_Interp *interp, Jim_Obj *objPtr, int flags) +{ + Jim_Cmd *cmd; + + if (objPtr->typePtr == &commandObjType + && objPtr->internalRep.cmdValue.procEpoch == interp->procEpoch +#ifdef jim_ext_namespace + && Jim_StringEqObj(objPtr->internalRep.cmdValue.nsObj, interp->framePtr->nsObj) +#endif + && objPtr->internalRep.cmdValue.cmdPtr->inUse) { + + cmd = objPtr->internalRep.cmdValue.cmdPtr; + } + else { + Jim_Obj *qualifiedNameObj = JimQualifyName(interp, objPtr); + Jim_HashEntry *he = Jim_FindHashEntry(&interp->commands, qualifiedNameObj); +#ifdef jim_ext_namespace + if (he == NULL && Jim_Length(interp->framePtr->nsObj)) { + he = Jim_FindHashEntry(&interp->commands, objPtr); + } +#endif + if (he == NULL) { + if (flags & JIM_ERRMSG) { + Jim_SetResultFormatted(interp, "invalid command name \"%#s\"", objPtr); + } + Jim_DecrRefCount(interp, qualifiedNameObj); + return NULL; + } + cmd = Jim_GetHashEntryVal(he); + + cmd->cmdNameObj = Jim_GetHashEntryKey(he); + + + Jim_FreeIntRep(interp, objPtr); + objPtr->typePtr = &commandObjType; + objPtr->internalRep.cmdValue.procEpoch = interp->procEpoch; + objPtr->internalRep.cmdValue.cmdPtr = cmd; + objPtr->internalRep.cmdValue.nsObj = interp->framePtr->nsObj; + Jim_IncrRefCount(interp->framePtr->nsObj); + Jim_DecrRefCount(interp, qualifiedNameObj); + } + while (cmd->u.proc.upcall) { + cmd = cmd->prevCmd; + } + return cmd; +} + + + +static const Jim_ObjType variableObjType = { + "variable", + NULL, + NULL, + NULL, + JIM_TYPE_REFERENCES, +}; + +static int SetVariableFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr) +{ + const char *varName; + Jim_CallFrame *framePtr; + int global; + int len; + Jim_VarVal *vv; + + + if (objPtr->typePtr == &variableObjType) { + framePtr = objPtr->internalRep.varValue.global ? interp->topFramePtr : interp->framePtr; + if (objPtr->internalRep.varValue.callFrameId == framePtr->id) { + + return JIM_OK; + } + + } + else if (objPtr->typePtr == &dictSubstObjType) { + return JIM_DICT_SUGAR; + } + + varName = Jim_GetString(objPtr, &len); + + + if (len && varName[len - 1] == ')' && strchr(varName, '(') != NULL) { + return JIM_DICT_SUGAR; + } + + if (varName[0] == ':' && varName[1] == ':') { + while (*varName == ':') { + varName++; + len--; + } + global = 1; + framePtr = interp->topFramePtr; + + Jim_Obj *tempObj = Jim_NewStringObj(interp, varName, len); + vv = JimFindVariable(&framePtr->vars, tempObj); + Jim_FreeNewObj(interp, tempObj); + } + else { + global = 0; + framePtr = interp->framePtr; + + vv = JimFindVariable(&framePtr->vars, objPtr); + if (vv == NULL && framePtr->staticVars) { + + vv = JimFindVariable(framePtr->staticVars, objPtr); + } + } + + if (vv == NULL) { + return JIM_ERR; + } + + + Jim_FreeIntRep(interp, objPtr); + objPtr->typePtr = &variableObjType; + objPtr->internalRep.varValue.callFrameId = framePtr->id; + objPtr->internalRep.varValue.vv = vv; + objPtr->internalRep.varValue.global = global; + return JIM_OK; +} + + +static int JimDictSugarSet(Jim_Interp *interp, Jim_Obj *ObjPtr, Jim_Obj *valObjPtr); +static Jim_Obj *JimDictSugarGet(Jim_Interp *interp, Jim_Obj *ObjPtr, int flags); + +static int JimSetNewVariable(Jim_HashTable *ht, Jim_Obj *nameObjPtr, Jim_VarVal *vv) +{ + return Jim_AddHashEntry(ht, nameObjPtr, vv); +} + +static Jim_VarVal *JimFindVariable(Jim_HashTable *ht, Jim_Obj *nameObjPtr) +{ + Jim_HashEntry *he = Jim_FindHashEntry(ht, nameObjPtr); + if (he) { + return (Jim_VarVal *)Jim_GetHashEntryVal(he); + } + return NULL; +} + +static int JimUnsetVariable(Jim_HashTable *ht, Jim_Obj *nameObjPtr) +{ + return Jim_DeleteHashEntry(ht, nameObjPtr); +} + +static Jim_VarVal *JimCreateVariable(Jim_Interp *interp, Jim_Obj *nameObjPtr, Jim_Obj *valObjPtr) +{ + const char *name; + Jim_CallFrame *framePtr; + int global; + int len; + + + Jim_VarVal *vv = Jim_Alloc(sizeof(*vv)); + + vv->objPtr = valObjPtr; + Jim_IncrRefCount(valObjPtr); + vv->linkFramePtr = NULL; + vv->refCount = 0; + + name = Jim_GetString(nameObjPtr, &len); + if (name[0] == ':' && name[1] == ':') { + while (*name == ':') { + name++; + len--; + } + framePtr = interp->topFramePtr; + global = 1; + JimSetNewVariable(&framePtr->vars, Jim_NewStringObj(interp, name, len), vv); + } + else { + framePtr = interp->framePtr; + global = 0; + JimSetNewVariable(&framePtr->vars, nameObjPtr, vv); + } + + + Jim_FreeIntRep(interp, nameObjPtr); + nameObjPtr->typePtr = &variableObjType; + nameObjPtr->internalRep.varValue.callFrameId = framePtr->id; + nameObjPtr->internalRep.varValue.vv = vv; + nameObjPtr->internalRep.varValue.global = global; + + return vv; +} + +int Jim_SetVariable(Jim_Interp *interp, Jim_Obj *nameObjPtr, Jim_Obj *valObjPtr) +{ + int err; + Jim_VarVal *vv; + + switch (SetVariableFromAny(interp, nameObjPtr)) { + case JIM_DICT_SUGAR: + return JimDictSugarSet(interp, nameObjPtr, valObjPtr); + + case JIM_ERR: + JimCreateVariable(interp, nameObjPtr, valObjPtr); + break; + + case JIM_OK: + vv = nameObjPtr->internalRep.varValue.vv; + if (vv->linkFramePtr == NULL) { + Jim_IncrRefCount(valObjPtr); + Jim_DecrRefCount(interp, vv->objPtr); + vv->objPtr = valObjPtr; + } + else { + Jim_CallFrame *savedCallFrame; + + savedCallFrame = interp->framePtr; + interp->framePtr = vv->linkFramePtr; + err = Jim_SetVariable(interp, vv->objPtr, valObjPtr); + interp->framePtr = savedCallFrame; + if (err != JIM_OK) + return err; + } + } + return JIM_OK; +} + +int Jim_SetVariableStr(Jim_Interp *interp, const char *name, Jim_Obj *objPtr) +{ + Jim_Obj *nameObjPtr; + int result; + + nameObjPtr = Jim_NewStringObj(interp, name, -1); + Jim_IncrRefCount(nameObjPtr); + result = Jim_SetVariable(interp, nameObjPtr, objPtr); + Jim_DecrRefCount(interp, nameObjPtr); + return result; +} + +int Jim_SetGlobalVariableStr(Jim_Interp *interp, const char *name, Jim_Obj *objPtr) +{ + Jim_CallFrame *savedFramePtr; + int result; + + savedFramePtr = interp->framePtr; + interp->framePtr = interp->topFramePtr; + result = Jim_SetVariableStr(interp, name, objPtr); + interp->framePtr = savedFramePtr; + return result; +} + +int Jim_SetVariableStrWithStr(Jim_Interp *interp, const char *name, const char *val) +{ + Jim_Obj *valObjPtr; + int result; + + valObjPtr = Jim_NewStringObj(interp, val, -1); + Jim_IncrRefCount(valObjPtr); + result = Jim_SetVariableStr(interp, name, valObjPtr); + Jim_DecrRefCount(interp, valObjPtr); + return result; +} + +int Jim_SetVariableLink(Jim_Interp *interp, Jim_Obj *nameObjPtr, + Jim_Obj *targetNameObjPtr, Jim_CallFrame *targetCallFrame) +{ + const char *varName; + const char *targetName; + Jim_CallFrame *framePtr; + Jim_VarVal *vv; + int len; + int varnamelen; + + + switch (SetVariableFromAny(interp, nameObjPtr)) { + case JIM_DICT_SUGAR: + + Jim_SetResultFormatted(interp, "bad variable name \"%#s\": upvar won't create a scalar variable that looks like an array element", nameObjPtr); + return JIM_ERR; + + case JIM_OK: + vv = nameObjPtr->internalRep.varValue.vv; + + if (vv->linkFramePtr == NULL) { + Jim_SetResultFormatted(interp, "variable \"%#s\" already exists", nameObjPtr); + return JIM_ERR; + } + + + vv->linkFramePtr = NULL; + break; + } + + + + varName = Jim_GetString(nameObjPtr, &varnamelen); + + if (varName[0] == ':' && varName[1] == ':') { + while (*varName == ':') { + varName++; + varnamelen--; + } + + framePtr = interp->topFramePtr; + } + else { + framePtr = interp->framePtr; + } + + targetName = Jim_GetString(targetNameObjPtr, &len); + if (targetName[0] == ':' && targetName[1] == ':') { + while (*targetName == ':') { + targetName++; + len--; + } + targetNameObjPtr = Jim_NewStringObj(interp, targetName, len); + targetCallFrame = interp->topFramePtr; + } + Jim_IncrRefCount(targetNameObjPtr); + + if (framePtr->level < targetCallFrame->level) { + Jim_SetResultFormatted(interp, + "bad variable name \"%#s\": upvar won't create namespace variable that refers to procedure variable", + nameObjPtr); + Jim_DecrRefCount(interp, targetNameObjPtr); + return JIM_ERR; + } + + + if (framePtr == targetCallFrame) { + Jim_Obj *objPtr = targetNameObjPtr; + + + while (1) { + if (Jim_Length(objPtr) == varnamelen && memcmp(Jim_String(objPtr), varName, varnamelen) == 0) { + Jim_SetResultString(interp, "can't upvar from variable to itself", -1); + Jim_DecrRefCount(interp, targetNameObjPtr); + return JIM_ERR; + } + if (SetVariableFromAny(interp, objPtr) != JIM_OK) + break; + vv = objPtr->internalRep.varValue.vv; + if (vv->linkFramePtr != targetCallFrame) + break; + objPtr = vv->objPtr; + } + } + + + Jim_SetVariable(interp, nameObjPtr, targetNameObjPtr); + + nameObjPtr->internalRep.varValue.vv->linkFramePtr = targetCallFrame; + Jim_DecrRefCount(interp, targetNameObjPtr); + return JIM_OK; +} + +Jim_Obj *Jim_GetVariable(Jim_Interp *interp, Jim_Obj *nameObjPtr, int flags) +{ + if (interp->safeexpr) { + return nameObjPtr; + } + switch (SetVariableFromAny(interp, nameObjPtr)) { + case JIM_OK:{ + Jim_VarVal *vv = nameObjPtr->internalRep.varValue.vv; + + if (vv->linkFramePtr == NULL) { + return vv->objPtr; + } + else { + Jim_Obj *objPtr; + + + Jim_CallFrame *savedCallFrame = interp->framePtr; + + interp->framePtr = vv->linkFramePtr; + objPtr = Jim_GetVariable(interp, vv->objPtr, flags); + interp->framePtr = savedCallFrame; + if (objPtr) { + return objPtr; + } + + } + } + break; + + case JIM_DICT_SUGAR: + + return JimDictSugarGet(interp, nameObjPtr, flags); + } + if (flags & JIM_ERRMSG) { + Jim_SetResultFormatted(interp, "can't read \"%#s\": no such variable", nameObjPtr); + } + return NULL; +} + +Jim_Obj *Jim_GetGlobalVariable(Jim_Interp *interp, Jim_Obj *nameObjPtr, int flags) +{ + Jim_CallFrame *savedFramePtr; + Jim_Obj *objPtr; + + savedFramePtr = interp->framePtr; + interp->framePtr = interp->topFramePtr; + objPtr = Jim_GetVariable(interp, nameObjPtr, flags); + interp->framePtr = savedFramePtr; + + return objPtr; +} + +Jim_Obj *Jim_GetVariableStr(Jim_Interp *interp, const char *name, int flags) +{ + Jim_Obj *nameObjPtr, *varObjPtr; + + nameObjPtr = Jim_NewStringObj(interp, name, -1); + Jim_IncrRefCount(nameObjPtr); + varObjPtr = Jim_GetVariable(interp, nameObjPtr, flags); + Jim_DecrRefCount(interp, nameObjPtr); + return varObjPtr; +} + +Jim_Obj *Jim_GetGlobalVariableStr(Jim_Interp *interp, const char *name, int flags) +{ + Jim_CallFrame *savedFramePtr; + Jim_Obj *objPtr; + + savedFramePtr = interp->framePtr; + interp->framePtr = interp->topFramePtr; + objPtr = Jim_GetVariableStr(interp, name, flags); + interp->framePtr = savedFramePtr; + + return objPtr; +} + +int Jim_UnsetVariable(Jim_Interp *interp, Jim_Obj *nameObjPtr, int flags) +{ + Jim_VarVal *vv; + int retval; + Jim_CallFrame *framePtr; + + retval = SetVariableFromAny(interp, nameObjPtr); + if (retval == JIM_DICT_SUGAR) { + + return JimDictSugarSet(interp, nameObjPtr, NULL); + } + else if (retval == JIM_OK) { + vv = nameObjPtr->internalRep.varValue.vv; + + + if (vv->linkFramePtr) { + framePtr = interp->framePtr; + interp->framePtr = vv->linkFramePtr; + retval = Jim_UnsetVariable(interp, vv->objPtr, JIM_NONE); + interp->framePtr = framePtr; + } + else { + if (nameObjPtr->internalRep.varValue.global) { + int len; + const char *name = Jim_GetString(nameObjPtr, &len); + while (*name == ':') { + name++; + len--; + } + framePtr = interp->topFramePtr; + Jim_Obj *tempObj = Jim_NewStringObj(interp, name, len); + retval = JimUnsetVariable(&framePtr->vars, tempObj); + Jim_FreeNewObj(interp, tempObj); + } + else { + framePtr = interp->framePtr; + retval = JimUnsetVariable(&framePtr->vars, nameObjPtr); + } + + if (retval == JIM_OK) { + + framePtr->id = interp->callFrameEpoch++; + } + } + } + if (retval != JIM_OK && (flags & JIM_ERRMSG)) { + Jim_SetResultFormatted(interp, "can't unset \"%#s\": no such variable", nameObjPtr); + } + return retval; +} + + + +static void JimDictSugarParseVarKey(Jim_Interp *interp, Jim_Obj *objPtr, + Jim_Obj **varPtrPtr, Jim_Obj **keyPtrPtr) +{ + const char *str, *p; + int len, keyLen; + Jim_Obj *varObjPtr, *keyObjPtr; + + str = Jim_GetString(objPtr, &len); + + p = strchr(str, '('); + JimPanic((p == NULL, "JimDictSugarParseVarKey() called for non-dict-sugar (%s)", str)); + + varObjPtr = Jim_NewStringObj(interp, str, p - str); + + p++; + keyLen = (str + len) - p; + if (str[len - 1] == ')') { + keyLen--; + } + + + keyObjPtr = Jim_NewStringObj(interp, p, keyLen); + + Jim_IncrRefCount(varObjPtr); + Jim_IncrRefCount(keyObjPtr); + *varPtrPtr = varObjPtr; + *keyPtrPtr = keyObjPtr; +} + +static int JimDictSugarSet(Jim_Interp *interp, Jim_Obj *objPtr, Jim_Obj *valObjPtr) +{ + int err; + + SetDictSubstFromAny(interp, objPtr); + + err = Jim_SetDictKeysVector(interp, objPtr->internalRep.dictSubstValue.varNameObjPtr, + &objPtr->internalRep.dictSubstValue.indexObjPtr, 1, valObjPtr, JIM_MUSTEXIST); + + if (err == JIM_OK) { + + Jim_SetEmptyResult(interp); + } + else { + if (!valObjPtr) { + + if (Jim_GetVariable(interp, objPtr->internalRep.dictSubstValue.varNameObjPtr, JIM_NONE)) { + Jim_SetResultFormatted(interp, "can't unset \"%#s\": no such element in array", + objPtr); + return err; + } + } + + Jim_SetResultFormatted(interp, "can't %s \"%#s\": variable isn't array", + (valObjPtr ? "set" : "unset"), objPtr); + } + return err; +} + +static Jim_Obj *JimDictExpandArrayVariable(Jim_Interp *interp, Jim_Obj *varObjPtr, + Jim_Obj *keyObjPtr, int flags) +{ + Jim_Obj *dictObjPtr; + Jim_Obj *resObjPtr = NULL; + int ret; + + dictObjPtr = Jim_GetVariable(interp, varObjPtr, JIM_ERRMSG); + if (!dictObjPtr) { + return NULL; + } + + ret = Jim_DictKey(interp, dictObjPtr, keyObjPtr, &resObjPtr, JIM_NONE); + if (ret != JIM_OK) { + Jim_SetResultFormatted(interp, + "can't read \"%#s(%#s)\": %s array", varObjPtr, keyObjPtr, + ret < 0 ? "variable isn't" : "no such element in"); + } + else if ((flags & JIM_UNSHARED) && Jim_IsShared(dictObjPtr)) { + + Jim_SetVariable(interp, varObjPtr, Jim_DuplicateObj(interp, dictObjPtr)); + } + + return resObjPtr; +} + + +static Jim_Obj *JimDictSugarGet(Jim_Interp *interp, Jim_Obj *objPtr, int flags) +{ + SetDictSubstFromAny(interp, objPtr); + + return JimDictExpandArrayVariable(interp, + objPtr->internalRep.dictSubstValue.varNameObjPtr, + objPtr->internalRep.dictSubstValue.indexObjPtr, flags); +} + + + +void FreeDictSubstInternalRep(Jim_Interp *interp, Jim_Obj *objPtr) +{ + Jim_DecrRefCount(interp, objPtr->internalRep.dictSubstValue.varNameObjPtr); + Jim_DecrRefCount(interp, objPtr->internalRep.dictSubstValue.indexObjPtr); +} + +static void DupDictSubstInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr) +{ + + dupPtr->internalRep = srcPtr->internalRep; + + Jim_IncrRefCount(dupPtr->internalRep.dictSubstValue.varNameObjPtr); + Jim_IncrRefCount(dupPtr->internalRep.dictSubstValue.indexObjPtr); +} + + +static void SetDictSubstFromAny(Jim_Interp *interp, Jim_Obj *objPtr) +{ + if (objPtr->typePtr != &dictSubstObjType) { + Jim_Obj *varObjPtr, *keyObjPtr; + + if (objPtr->typePtr == &interpolatedObjType) { + + + varObjPtr = objPtr->internalRep.dictSubstValue.varNameObjPtr; + keyObjPtr = objPtr->internalRep.dictSubstValue.indexObjPtr; + + Jim_IncrRefCount(varObjPtr); + Jim_IncrRefCount(keyObjPtr); + } + else { + JimDictSugarParseVarKey(interp, objPtr, &varObjPtr, &keyObjPtr); + } + + Jim_FreeIntRep(interp, objPtr); + objPtr->typePtr = &dictSubstObjType; + objPtr->internalRep.dictSubstValue.varNameObjPtr = varObjPtr; + objPtr->internalRep.dictSubstValue.indexObjPtr = keyObjPtr; + } +} + +static Jim_Obj *JimExpandDictSugar(Jim_Interp *interp, Jim_Obj *objPtr) +{ + Jim_Obj *resObjPtr = NULL; + Jim_Obj *substKeyObjPtr = NULL; + + if (interp->safeexpr) { + return objPtr; + } + + SetDictSubstFromAny(interp, objPtr); + + if (Jim_SubstObj(interp, objPtr->internalRep.dictSubstValue.indexObjPtr, + &substKeyObjPtr, JIM_NONE) + != JIM_OK) { + return NULL; + } + Jim_IncrRefCount(substKeyObjPtr); + resObjPtr = + JimDictExpandArrayVariable(interp, objPtr->internalRep.dictSubstValue.varNameObjPtr, + substKeyObjPtr, 0); + Jim_DecrRefCount(interp, substKeyObjPtr); + + return resObjPtr; +} + + +static Jim_CallFrame *JimCreateCallFrame(Jim_Interp *interp, Jim_CallFrame *parent, Jim_Obj *nsObj) +{ + Jim_CallFrame *cf; + + if (interp->freeFramesList) { + cf = interp->freeFramesList; + interp->freeFramesList = cf->next; + + cf->argv = NULL; + cf->argc = 0; + cf->procArgsObjPtr = NULL; + cf->procBodyObjPtr = NULL; + cf->next = NULL; + cf->staticVars = NULL; + cf->localCommands = NULL; + cf->tailcallObj = NULL; + cf->tailcallCmd = NULL; + } + else { + cf = Jim_Alloc(sizeof(*cf)); + memset(cf, 0, sizeof(*cf)); + + Jim_InitHashTable(&cf->vars, &JimVariablesHashTableType, interp); + } + + cf->id = interp->callFrameEpoch++; + cf->parent = parent; + cf->level = parent ? parent->level + 1 : 0; + cf->nsObj = nsObj; + Jim_IncrRefCount(nsObj); + + return cf; +} + +static int JimDeleteLocalProcs(Jim_Interp *interp, Jim_Stack *localCommands) +{ + + if (localCommands) { + Jim_Obj *cmdNameObj; + + while ((cmdNameObj = Jim_StackPop(localCommands)) != NULL) { + Jim_HashTable *ht = &interp->commands; + Jim_HashEntry *he = Jim_FindHashEntry(ht, cmdNameObj); + if (he) { + Jim_Cmd *cmd = Jim_GetHashEntryVal(he); + if (cmd->prevCmd) { + Jim_Cmd *prevCmd = cmd->prevCmd; + cmd->prevCmd = NULL; + + + JimDecrCmdRefCount(interp, cmd); + + + Jim_SetHashVal(ht, he, prevCmd); + } + else { + Jim_DeleteHashEntry(ht, cmdNameObj); + } + } + Jim_DecrRefCount(interp, cmdNameObj); + } + Jim_FreeStack(localCommands); + Jim_Free(localCommands); + } + return JIM_OK; +} + +static int JimInvokeDefer(Jim_Interp *interp, int retcode) +{ + Jim_Obj *objPtr; + + + if (JimFindVariable(&interp->framePtr->vars, interp->defer) == NULL) { + return retcode; + } + objPtr = Jim_GetVariable(interp, interp->defer, JIM_NONE); + + if (objPtr) { + int ret = JIM_OK; + int i; + int listLen = Jim_ListLength(interp, objPtr); + Jim_Obj *resultObjPtr; + + Jim_IncrRefCount(objPtr); + + resultObjPtr = Jim_GetResult(interp); + Jim_IncrRefCount(resultObjPtr); + Jim_SetEmptyResult(interp); + + + for (i = listLen; i > 0; i--) { + + Jim_Obj *scriptObjPtr = Jim_ListGetIndex(interp, objPtr, i - 1); + ret = Jim_EvalObj(interp, scriptObjPtr); + if (ret != JIM_OK) { + break; + } + } + + if (ret == JIM_OK || retcode == JIM_ERR) { + + Jim_SetResult(interp, resultObjPtr); + } + else { + retcode = ret; + } + + Jim_DecrRefCount(interp, resultObjPtr); + Jim_DecrRefCount(interp, objPtr); + } + return retcode; +} + +#define JIM_FCF_FULL 0 +#define JIM_FCF_REUSE 1 +static void JimFreeCallFrame(Jim_Interp *interp, Jim_CallFrame *cf, int action) + { + JimDeleteLocalProcs(interp, cf->localCommands); + + if (cf->procArgsObjPtr) + Jim_DecrRefCount(interp, cf->procArgsObjPtr); + if (cf->procBodyObjPtr) + Jim_DecrRefCount(interp, cf->procBodyObjPtr); + Jim_DecrRefCount(interp, cf->nsObj); + if (action == JIM_FCF_FULL || cf->vars.size != JIM_HT_INITIAL_SIZE) + Jim_FreeHashTable(&cf->vars); + else { + Jim_ClearHashTable(&cf->vars); + } + cf->next = interp->freeFramesList; + interp->freeFramesList = cf; +} + + + +int Jim_IsBigEndian(void) +{ + union { + unsigned short s; + unsigned char c[2]; + } uval = {0x0102}; + + return uval.c[0] == 1; +} + + +Jim_Interp *Jim_CreateInterp(void) +{ + Jim_Interp *i = Jim_Alloc(sizeof(*i)); + + memset(i, 0, sizeof(*i)); + + i->maxCallFrameDepth = JIM_MAX_CALLFRAME_DEPTH; + i->maxEvalDepth = JIM_MAX_EVAL_DEPTH; + i->lastCollectTime = Jim_GetTimeUsec(CLOCK_MONOTONIC_RAW); + + Jim_InitHashTable(&i->commands, &JimCommandsHashTableType, i); +#ifdef JIM_REFERENCES + Jim_InitHashTable(&i->references, &JimReferencesHashTableType, i); +#endif + Jim_InitHashTable(&i->assocData, &JimAssocDataHashTableType, i); + Jim_InitHashTable(&i->packages, &JimPackageHashTableType, NULL); + i->emptyObj = Jim_NewEmptyStringObj(i); + i->trueObj = Jim_NewIntObj(i, 1); + i->falseObj = Jim_NewIntObj(i, 0); + i->framePtr = i->topFramePtr = JimCreateCallFrame(i, NULL, i->emptyObj); + i->result = i->emptyObj; + i->stackTrace = Jim_NewListObj(i, NULL, 0); + i->unknown = Jim_NewStringObj(i, "unknown", -1); + i->defer = Jim_NewStringObj(i, "jim::defer", -1); + i->errorProc = i->emptyObj; + i->nullScriptObj = Jim_NewEmptyStringObj(i); + i->evalFrame = &i->topEvalFrame; + i->currentFilenameObj = Jim_NewEmptyStringObj(i); + Jim_IncrRefCount(i->emptyObj); + Jim_IncrRefCount(i->result); + Jim_IncrRefCount(i->stackTrace); + Jim_IncrRefCount(i->unknown); + Jim_IncrRefCount(i->defer); + Jim_IncrRefCount(i->nullScriptObj); + Jim_IncrRefCount(i->errorProc); + Jim_IncrRefCount(i->trueObj); + Jim_IncrRefCount(i->falseObj); + Jim_IncrRefCount(i->currentFilenameObj); + + + Jim_SetVariableStrWithStr(i, JIM_LIBPATH, TCL_LIBRARY); + Jim_SetVariableStrWithStr(i, JIM_INTERACTIVE, "0"); + + Jim_SetVariableStrWithStr(i, "tcl_platform(engine)", "Jim"); + Jim_SetVariableStrWithStr(i, "tcl_platform(os)", TCL_PLATFORM_OS); + Jim_SetVariableStrWithStr(i, "tcl_platform(platform)", TCL_PLATFORM_PLATFORM); + Jim_SetVariableStrWithStr(i, "tcl_platform(pathSeparator)", TCL_PLATFORM_PATH_SEPARATOR); + Jim_SetVariableStrWithStr(i, "tcl_platform(byteOrder)", Jim_IsBigEndian() ? "bigEndian" : "littleEndian"); + Jim_SetVariableStrWithStr(i, "tcl_platform(threaded)", "0"); + Jim_SetVariableStrWithStr(i, "tcl_platform(bootstrap)", "0"); + Jim_SetVariableStr(i, "tcl_platform(pointerSize)", Jim_NewIntObj(i, sizeof(void *))); + Jim_SetVariableStr(i, "tcl_platform(wordSize)", Jim_NewIntObj(i, sizeof(jim_wide))); + Jim_SetVariableStr(i, "tcl_platform(stackFormat)", Jim_NewIntObj(i, 4)); + + return i; +} + +void Jim_FreeInterp(Jim_Interp *i) +{ + Jim_CallFrame *cf, *cfx; + + Jim_Obj *objPtr, *nextObjPtr; + + i->quitting = 1; + + + for (cf = i->framePtr; cf; cf = cfx) { + + JimInvokeDefer(i, JIM_OK); + cfx = cf->parent; + JimFreeCallFrame(i, cf, JIM_FCF_FULL); + } + + + Jim_FreeHashTable(&i->commands); + + Jim_DecrRefCount(i, i->emptyObj); + Jim_DecrRefCount(i, i->trueObj); + Jim_DecrRefCount(i, i->falseObj); + Jim_DecrRefCount(i, i->result); + Jim_DecrRefCount(i, i->stackTrace); + Jim_DecrRefCount(i, i->errorProc); + Jim_DecrRefCount(i, i->unknown); + Jim_DecrRefCount(i, i->defer); + Jim_DecrRefCount(i, i->nullScriptObj); + Jim_DecrRefCount(i, i->currentFilenameObj); + + + Jim_InterpIncrProcEpoch(i); + +#ifdef JIM_REFERENCES + Jim_FreeHashTable(&i->references); +#endif + Jim_FreeHashTable(&i->packages); + Jim_Free(i->prngState); + Jim_FreeHashTable(&i->assocData); + if (i->traceCmdObj) { + Jim_DecrRefCount(i, i->traceCmdObj); + } + +#ifdef JIM_MAINTAINER + if (i->liveList != NULL) { + objPtr = i->liveList; + + printf("\n-------------------------------------\n"); + printf("Objects still in the free list:\n"); + while (objPtr) { + const char *type = objPtr->typePtr ? objPtr->typePtr->name : "string"; + Jim_String(objPtr); + + if (objPtr->bytes && strlen(objPtr->bytes) > 20) { + printf("%p (%d) %-10s: '%.20s...'\n", + (void *)objPtr, objPtr->refCount, type, objPtr->bytes); + } + else { + printf("%p (%d) %-10s: '%s'\n", + (void *)objPtr, objPtr->refCount, type, objPtr->bytes ? objPtr->bytes : "(null)"); + } + if (objPtr->typePtr == &sourceObjType) { + printf("FILE %s LINE %d\n", + Jim_String(objPtr->internalRep.sourceValue.fileNameObj), + objPtr->internalRep.sourceValue.lineNumber); + } + objPtr = objPtr->nextObjPtr; + } + printf("-------------------------------------\n\n"); + JimPanic((1, "Live list non empty freeing the interpreter! Leak?")); + } +#endif + + + objPtr = i->freeList; + while (objPtr) { + nextObjPtr = objPtr->nextObjPtr; + Jim_Free(objPtr); + objPtr = nextObjPtr; + } + + + for (cf = i->freeFramesList; cf; cf = cfx) { + cfx = cf->next; + if (cf->vars.table) + Jim_FreeHashTable(&cf->vars); + Jim_Free(cf); + } + + + Jim_Free(i); +} + +Jim_CallFrame *Jim_GetCallFrameByLevel(Jim_Interp *interp, Jim_Obj *levelObjPtr) +{ + long level; + const char *str; + Jim_CallFrame *framePtr; + + if (levelObjPtr) { + str = Jim_String(levelObjPtr); + if (str[0] == '#') { + char *endptr; + + level = jim_strtol(str + 1, &endptr); + if (str[1] == '\0' || endptr[0] != '\0') { + level = -1; + } + } + else { + if (Jim_GetLong(interp, levelObjPtr, &level) != JIM_OK || level < 0) { + level = -1; + } + else { + + level = interp->framePtr->level - level; + } + } + } + else { + str = "1"; + level = interp->framePtr->level - 1; + } + + if (level == 0) { + return interp->topFramePtr; + } + if (level > 0) { + + for (framePtr = interp->framePtr; framePtr; framePtr = framePtr->parent) { + if (framePtr->level == level) { + return framePtr; + } + } + } + + Jim_SetResultFormatted(interp, "bad level \"%s\"", str); + return NULL; +} + +static Jim_CallFrame *JimGetCallFrameByInteger(Jim_Interp *interp, long level) +{ + Jim_CallFrame *framePtr; + + if (level == 0) { + return interp->framePtr; + } + + if (level < 0) { + + level = interp->framePtr->level + level; + } + + if (level > 0) { + + for (framePtr = interp->framePtr; framePtr; framePtr = framePtr->parent) { + if (framePtr->level == level) { + return framePtr; + } + } + } + return NULL; +} + +static Jim_EvalFrame *JimGetEvalFrameByProcLevel(Jim_Interp *interp, int proclevel) +{ + Jim_EvalFrame *evalFrame; + + if (proclevel == 0) { + return interp->evalFrame; + } + + if (proclevel < 0) { + + proclevel = interp->procLevel + proclevel; + } + + if (proclevel >= 0) { + + for (evalFrame = interp->evalFrame; evalFrame; evalFrame = evalFrame->parent) { + if (evalFrame->procLevel == proclevel) { + return evalFrame; + } + } + } + return NULL; +} + +static Jim_Obj *JimProcForEvalFrame(Jim_Interp *interp, Jim_EvalFrame *frame) +{ + if (frame == interp->evalFrame || (frame->cmd && frame->cmd->cmdNameObj)) { + Jim_EvalFrame *e; + for (e = frame->parent; e; e = e->parent) { + if (e->cmd && e->cmd->isproc && e->cmd->cmdNameObj) { + break; + } + } + if (e && e->cmd && e->cmd->cmdNameObj) { + return e->cmd->cmdNameObj; + } + } + return NULL; +} + +static void JimAddStackFrame(Jim_Interp *interp, Jim_EvalFrame *frame, Jim_Obj *listObj) +{ + Jim_Obj *procNameObj = JimProcForEvalFrame(interp, frame); + Jim_Obj *fileNameObj = interp->emptyObj; + int linenr = 1; + + if (frame->scriptObj) { + ScriptObj *script = JimGetScript(interp, frame->scriptObj); + fileNameObj = script->fileNameObj; + linenr = script->linenr; + } + + Jim_ListAppendElement(interp, listObj, procNameObj ? procNameObj : interp->emptyObj); + Jim_ListAppendElement(interp, listObj, fileNameObj); + Jim_ListAppendElement(interp, listObj, Jim_NewIntObj(interp, linenr)); + Jim_ListAppendElement(interp, listObj, Jim_NewListObj(interp, frame->argv, frame->argc)); +} + +static void JimSetStackTrace(Jim_Interp *interp, Jim_Obj *stackTraceObj) +{ + + Jim_IncrRefCount(stackTraceObj); + Jim_DecrRefCount(interp, interp->stackTrace); + interp->stackTrace = stackTraceObj; + interp->errorFlag = 1; +} + +static void JimSetErrorStack(Jim_Interp *interp, ScriptObj *script) +{ + if (!interp->errorFlag) { + int i; + Jim_Obj *stackTrace = Jim_NewListObj(interp, NULL, 0); + + if (interp->procLevel == 0 && script) { + Jim_ListAppendElement(interp, stackTrace, interp->emptyObj); + Jim_ListAppendElement(interp, stackTrace, script->fileNameObj); + Jim_ListAppendElement(interp, stackTrace, Jim_NewIntObj(interp, script->linenr)); + Jim_ListAppendElement(interp, stackTrace, interp->emptyObj); + } + else { + for (i = 0; i <= interp->procLevel; i++) { + Jim_EvalFrame *frame = JimGetEvalFrameByProcLevel(interp, -i); + if (frame) { + JimAddStackFrame(interp, frame, stackTrace); + } + } + } + JimSetStackTrace(interp, stackTrace); + } +} + +int Jim_SetAssocData(Jim_Interp *interp, const char *key, Jim_InterpDeleteProc * delProc, + void *data) +{ + AssocDataValue *assocEntryPtr = (AssocDataValue *) Jim_Alloc(sizeof(AssocDataValue)); + + assocEntryPtr->delProc = delProc; + assocEntryPtr->data = data; + return Jim_AddHashEntry(&interp->assocData, key, assocEntryPtr); +} + +void *Jim_GetAssocData(Jim_Interp *interp, const char *key) +{ + Jim_HashEntry *entryPtr = Jim_FindHashEntry(&interp->assocData, key); + + if (entryPtr != NULL) { + AssocDataValue *assocEntryPtr = Jim_GetHashEntryVal(entryPtr); + return assocEntryPtr->data; + } + return NULL; +} + +int Jim_DeleteAssocData(Jim_Interp *interp, const char *key) +{ + return Jim_DeleteHashEntry(&interp->assocData, key); +} + +int Jim_GetExitCode(Jim_Interp *interp) +{ + return interp->exitCode; +} + +static void UpdateStringOfInt(struct Jim_Obj *objPtr); +static int SetIntFromAny(Jim_Interp *interp, Jim_Obj *objPtr, int flags); + +static const Jim_ObjType intObjType = { + "int", + NULL, + NULL, + UpdateStringOfInt, + JIM_TYPE_NONE, +}; + +static const Jim_ObjType coercedDoubleObjType = { + "coerced-double", + NULL, + NULL, + UpdateStringOfInt, + JIM_TYPE_NONE, +}; + + +static void UpdateStringOfInt(struct Jim_Obj *objPtr) +{ + char buf[JIM_INTEGER_SPACE + 1]; + jim_wide wideValue = JimWideValue(objPtr); + int pos = 0; + + if (wideValue == 0) { + buf[pos++] = '0'; + } + else { + char tmp[JIM_INTEGER_SPACE]; + int num = 0; + int i; + + if (wideValue < 0) { + buf[pos++] = '-'; + i = wideValue % 10; + tmp[num++] = (i > 0) ? (10 - i) : -i; + wideValue /= -10; + } + + while (wideValue) { + tmp[num++] = wideValue % 10; + wideValue /= 10; + } + + for (i = 0; i < num; i++) { + buf[pos++] = '0' + tmp[num - i - 1]; + } + } + buf[pos] = 0; + + JimSetStringBytes(objPtr, buf); +} + +static int SetIntFromAny(Jim_Interp *interp, Jim_Obj *objPtr, int flags) +{ + jim_wide wideValue; + const char *str; + + if (objPtr->typePtr == &coercedDoubleObjType) { + + objPtr->typePtr = &intObjType; + return JIM_OK; + } + + + str = Jim_String(objPtr); + + if (Jim_StringToWide(str, &wideValue, 0) != JIM_OK) { + if (flags & JIM_ERRMSG) { + Jim_SetResultFormatted(interp, "expected integer but got \"%#s\"", objPtr); + } + return JIM_ERR; + } + if ((wideValue == JIM_WIDE_MIN || wideValue == JIM_WIDE_MAX) && errno == ERANGE) { + Jim_SetResultString(interp, "Integer value too big to be represented", -1); + return JIM_ERR; + } + + Jim_FreeIntRep(interp, objPtr); + objPtr->typePtr = &intObjType; + objPtr->internalRep.wideValue = wideValue; + return JIM_OK; +} + +#ifdef JIM_OPTIMIZATION +static int JimIsWide(Jim_Obj *objPtr) +{ + return objPtr->typePtr == &intObjType; +} +#endif + +int Jim_GetWide(Jim_Interp *interp, Jim_Obj *objPtr, jim_wide * widePtr) +{ + if (objPtr->typePtr != &intObjType && SetIntFromAny(interp, objPtr, JIM_ERRMSG) == JIM_ERR) + return JIM_ERR; + *widePtr = JimWideValue(objPtr); + return JIM_OK; +} + +int Jim_GetWideExpr(Jim_Interp *interp, Jim_Obj *objPtr, jim_wide * widePtr) +{ + int ret = JIM_OK; + + if (objPtr->typePtr == &sourceObjType || objPtr->typePtr == NULL) { + SetIntFromAny(interp, objPtr, 0); + } + if (objPtr->typePtr == &intObjType) { + *widePtr = JimWideValue(objPtr); + } + else { + JimPanic((interp->safeexpr, "interp->safeexpr is set")); + interp->safeexpr++; + ret = Jim_EvalExpression(interp, objPtr); + interp->safeexpr--; + + if (ret == JIM_OK) { + ret = Jim_GetWide(interp, Jim_GetResult(interp), widePtr); + } + if (ret != JIM_OK) { + Jim_SetResultFormatted(interp, "expected integer expression but got \"%#s\"", objPtr); + } + } + return ret; +} + + +static int JimGetWideNoErr(Jim_Interp *interp, Jim_Obj *objPtr, jim_wide * widePtr) +{ + if (objPtr->typePtr != &intObjType && SetIntFromAny(interp, objPtr, JIM_NONE) == JIM_ERR) + return JIM_ERR; + *widePtr = JimWideValue(objPtr); + return JIM_OK; +} + +int Jim_GetLong(Jim_Interp *interp, Jim_Obj *objPtr, long *longPtr) +{ + jim_wide wideValue; + int retval; + + retval = Jim_GetWide(interp, objPtr, &wideValue); + if (retval == JIM_OK) { + *longPtr = (long)wideValue; + return JIM_OK; + } + return JIM_ERR; +} + +Jim_Obj *Jim_NewIntObj(Jim_Interp *interp, jim_wide wideValue) +{ + Jim_Obj *objPtr; + + objPtr = Jim_NewObj(interp); + objPtr->typePtr = &intObjType; + objPtr->bytes = NULL; + objPtr->internalRep.wideValue = wideValue; + return objPtr; +} + +#define JIM_DOUBLE_SPACE 30 + +static void UpdateStringOfDouble(struct Jim_Obj *objPtr); +static int SetDoubleFromAny(Jim_Interp *interp, Jim_Obj *objPtr); + +static const Jim_ObjType doubleObjType = { + "double", + NULL, + NULL, + UpdateStringOfDouble, + JIM_TYPE_NONE, +}; + +#if !HAVE_DECL_ISNAN +#undef isnan +#define isnan(X) ((X) != (X)) +#endif +#if !HAVE_DECL_ISINF +#undef isinf +#define isinf(X) (1.0 / (X) == 0.0) +#endif + +static void UpdateStringOfDouble(struct Jim_Obj *objPtr) +{ + double value = objPtr->internalRep.doubleValue; + + if (isnan(value)) { + JimSetStringBytes(objPtr, "NaN"); + return; + } + if (isinf(value)) { + if (value < 0) { + JimSetStringBytes(objPtr, "-Inf"); + } + else { + JimSetStringBytes(objPtr, "Inf"); + } + return; + } + { + char buf[JIM_DOUBLE_SPACE + 1]; + int i; + int len = sprintf(buf, "%.12g", value); + + + for (i = 0; i < len; i++) { + if (buf[i] == '.' || buf[i] == 'e') { +#if defined(JIM_SPRINTF_DOUBLE_NEEDS_FIX) + char *e = strchr(buf, 'e'); + if (e && (e[1] == '-' || e[1] == '+') && e[2] == '0') { + + e += 2; + memmove(e, e + 1, len - (e - buf)); + } +#endif + break; + } + } + if (buf[i] == '\0') { + buf[i++] = '.'; + buf[i++] = '0'; + buf[i] = '\0'; + } + JimSetStringBytes(objPtr, buf); + } +} + +static int SetDoubleFromAny(Jim_Interp *interp, Jim_Obj *objPtr) +{ + double doubleValue; + jim_wide wideValue; + const char *str; + +#ifdef HAVE_LONG_LONG + +#define MIN_INT_IN_DOUBLE -(1LL << 53) +#define MAX_INT_IN_DOUBLE -(MIN_INT_IN_DOUBLE + 1) + + if (objPtr->typePtr == &intObjType + && JimWideValue(objPtr) >= MIN_INT_IN_DOUBLE + && JimWideValue(objPtr) <= MAX_INT_IN_DOUBLE) { + + + objPtr->typePtr = &coercedDoubleObjType; + return JIM_OK; + } +#endif + str = Jim_String(objPtr); + + if (Jim_StringToWide(str, &wideValue, 10) == JIM_OK) { + + Jim_FreeIntRep(interp, objPtr); + objPtr->typePtr = &coercedDoubleObjType; + objPtr->internalRep.wideValue = wideValue; + return JIM_OK; + } + else { + + if (Jim_StringToDouble(str, &doubleValue) != JIM_OK) { + Jim_SetResultFormatted(interp, "expected floating-point number but got \"%#s\"", objPtr); + return JIM_ERR; + } + + Jim_FreeIntRep(interp, objPtr); + } + objPtr->typePtr = &doubleObjType; + objPtr->internalRep.doubleValue = doubleValue; + return JIM_OK; +} + +int Jim_GetDouble(Jim_Interp *interp, Jim_Obj *objPtr, double *doublePtr) +{ + if (objPtr->typePtr == &coercedDoubleObjType) { + *doublePtr = JimWideValue(objPtr); + return JIM_OK; + } + if (objPtr->typePtr != &doubleObjType && SetDoubleFromAny(interp, objPtr) == JIM_ERR) + return JIM_ERR; + + if (objPtr->typePtr == &coercedDoubleObjType) { + *doublePtr = JimWideValue(objPtr); + } + else { + *doublePtr = objPtr->internalRep.doubleValue; + } + return JIM_OK; +} + +Jim_Obj *Jim_NewDoubleObj(Jim_Interp *interp, double doubleValue) +{ + Jim_Obj *objPtr; + + objPtr = Jim_NewObj(interp); + objPtr->typePtr = &doubleObjType; + objPtr->bytes = NULL; + objPtr->internalRep.doubleValue = doubleValue; + return objPtr; +} + +static int SetBooleanFromAny(Jim_Interp *interp, Jim_Obj *objPtr, int flags); + +int Jim_GetBoolean(Jim_Interp *interp, Jim_Obj *objPtr, int * booleanPtr) +{ + if (objPtr->typePtr != &intObjType && SetBooleanFromAny(interp, objPtr, JIM_ERRMSG) == JIM_ERR) + return JIM_ERR; + *booleanPtr = (int) JimWideValue(objPtr); + return JIM_OK; +} + +static const char * const jim_true_false_strings[8] = { + "1", "true", "yes", "on", + "0", "false", "no", "off" +}; + +static const int jim_true_false_lens[8] = { + 1, 4, 3, 2, + 1, 5, 2, 3, +}; + +static int SetBooleanFromAny(Jim_Interp *interp, Jim_Obj *objPtr, int flags) +{ + int index = Jim_FindByName(Jim_String(objPtr), jim_true_false_strings, + sizeof(jim_true_false_strings) / sizeof(*jim_true_false_strings)); + if (index < 0) { + if (flags & JIM_ERRMSG) { + Jim_SetResultFormatted(interp, "expected boolean but got \"%#s\"", objPtr); + } + return JIM_ERR; + } + + + Jim_FreeIntRep(interp, objPtr); + objPtr->typePtr = &intObjType; + + objPtr->internalRep.wideValue = index < 4 ? 1 : 0; + return JIM_OK; +} + +static void ListInsertElements(Jim_Obj *listPtr, int idx, int elemc, Jim_Obj *const *elemVec); +static void ListAppendElement(Jim_Obj *listPtr, Jim_Obj *objPtr); +static void FreeListInternalRep(Jim_Interp *interp, Jim_Obj *objPtr); +static void DupListInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr); +static void UpdateStringOfList(struct Jim_Obj *objPtr); +static int SetListFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr); + +static const Jim_ObjType listObjType = { + "list", + FreeListInternalRep, + DupListInternalRep, + UpdateStringOfList, + JIM_TYPE_NONE, +}; + +void FreeListInternalRep(Jim_Interp *interp, Jim_Obj *objPtr) +{ + int i; + + for (i = 0; i < objPtr->internalRep.listValue.len; i++) { + Jim_DecrRefCount(interp, objPtr->internalRep.listValue.ele[i]); + } + Jim_Free(objPtr->internalRep.listValue.ele); +} + +void DupListInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr) +{ + int i; + + JIM_NOTUSED(interp); + + dupPtr->internalRep.listValue.len = srcPtr->internalRep.listValue.len; + dupPtr->internalRep.listValue.maxLen = srcPtr->internalRep.listValue.maxLen; + dupPtr->internalRep.listValue.ele = + Jim_Alloc(sizeof(Jim_Obj *) * srcPtr->internalRep.listValue.maxLen); + memcpy(dupPtr->internalRep.listValue.ele, srcPtr->internalRep.listValue.ele, + sizeof(Jim_Obj *) * srcPtr->internalRep.listValue.len); + for (i = 0; i < dupPtr->internalRep.listValue.len; i++) { + Jim_IncrRefCount(dupPtr->internalRep.listValue.ele[i]); + } + dupPtr->typePtr = &listObjType; +} + +#define JIM_ELESTR_SIMPLE 0 +#define JIM_ELESTR_BRACE 1 +#define JIM_ELESTR_QUOTE 2 +static unsigned char ListElementQuotingType(const char *s, int len) +{ + int i, level, blevel, trySimple = 1; + + + if (len == 0) + return JIM_ELESTR_BRACE; + if (s[0] == '"' || s[0] == '{') { + trySimple = 0; + goto testbrace; + } + for (i = 0; i < len; i++) { + switch (s[i]) { + case ' ': + case '$': + case '"': + case '[': + case ']': + case ';': + case '\\': + case '\r': + case '\n': + case '\t': + case '\f': + case '\v': + trySimple = 0; + + case '{': + case '}': + goto testbrace; + } + } + return JIM_ELESTR_SIMPLE; + + testbrace: + + if (s[len - 1] == '\\') + return JIM_ELESTR_QUOTE; + level = 0; + blevel = 0; + for (i = 0; i < len; i++) { + switch (s[i]) { + case '{': + level++; + break; + case '}': + level--; + if (level < 0) + return JIM_ELESTR_QUOTE; + break; + case '[': + blevel++; + break; + case ']': + blevel--; + break; + case '\\': + if (s[i + 1] == '\n') + return JIM_ELESTR_QUOTE; + else if (s[i + 1] != '\0') + i++; + break; + } + } + if (blevel < 0) { + return JIM_ELESTR_QUOTE; + } + + if (level == 0) { + if (!trySimple) + return JIM_ELESTR_BRACE; + for (i = 0; i < len; i++) { + switch (s[i]) { + case ' ': + case '$': + case '"': + case '[': + case ']': + case ';': + case '\\': + case '\r': + case '\n': + case '\t': + case '\f': + case '\v': + return JIM_ELESTR_BRACE; + break; + } + } + return JIM_ELESTR_SIMPLE; + } + return JIM_ELESTR_QUOTE; +} + +static int BackslashQuoteString(const char *s, int len, char *q) +{ + char *p = q; + + while (len--) { + switch (*s) { + case ' ': + case '$': + case '"': + case '[': + case ']': + case '{': + case '}': + case ';': + case '\\': + *p++ = '\\'; + *p++ = *s++; + break; + case '\n': + *p++ = '\\'; + *p++ = 'n'; + s++; + break; + case '\r': + *p++ = '\\'; + *p++ = 'r'; + s++; + break; + case '\t': + *p++ = '\\'; + *p++ = 't'; + s++; + break; + case '\f': + *p++ = '\\'; + *p++ = 'f'; + s++; + break; + case '\v': + *p++ = '\\'; + *p++ = 'v'; + s++; + break; + default: + *p++ = *s++; + break; + } + } + *p = '\0'; + + return p - q; +} + +static void JimMakeListStringRep(Jim_Obj *objPtr, Jim_Obj **objv, int objc) +{ + #define STATIC_QUOTING_LEN 32 + int i, bufLen, realLength; + const char *strRep; + char *p; + unsigned char *quotingType, staticQuoting[STATIC_QUOTING_LEN]; + + + if (objc > STATIC_QUOTING_LEN) { + quotingType = Jim_Alloc(objc); + } + else { + quotingType = staticQuoting; + } + bufLen = 0; + for (i = 0; i < objc; i++) { + int len; + + strRep = Jim_GetString(objv[i], &len); + quotingType[i] = ListElementQuotingType(strRep, len); + switch (quotingType[i]) { + case JIM_ELESTR_SIMPLE: + if (i != 0 || strRep[0] != '#') { + bufLen += len; + break; + } + + quotingType[i] = JIM_ELESTR_BRACE; + + case JIM_ELESTR_BRACE: + bufLen += len + 2; + break; + case JIM_ELESTR_QUOTE: + bufLen += len * 2; + break; + } + bufLen++; + } + bufLen++; + + + p = objPtr->bytes = Jim_Alloc(bufLen + 1); + realLength = 0; + for (i = 0; i < objc; i++) { + int len, qlen; + + strRep = Jim_GetString(objv[i], &len); + + switch (quotingType[i]) { + case JIM_ELESTR_SIMPLE: + memcpy(p, strRep, len); + p += len; + realLength += len; + break; + case JIM_ELESTR_BRACE: + *p++ = '{'; + memcpy(p, strRep, len); + p += len; + *p++ = '}'; + realLength += len + 2; + break; + case JIM_ELESTR_QUOTE: + if (i == 0 && strRep[0] == '#') { + *p++ = '\\'; + realLength++; + } + qlen = BackslashQuoteString(strRep, len, p); + p += qlen; + realLength += qlen; + break; + } + + if (i + 1 != objc) { + *p++ = ' '; + realLength++; + } + } + *p = '\0'; + objPtr->length = realLength; + + if (quotingType != staticQuoting) { + Jim_Free(quotingType); + } +} + +static void UpdateStringOfList(struct Jim_Obj *objPtr) +{ + JimMakeListStringRep(objPtr, objPtr->internalRep.listValue.ele, objPtr->internalRep.listValue.len); +} + +static int SetListFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr) +{ + struct JimParserCtx parser; + const char *str; + int strLen; + Jim_Obj *fileNameObj; + int linenr; + + if (objPtr->typePtr == &listObjType) { + return JIM_OK; + } + + + if (Jim_IsDict(objPtr) && objPtr->bytes == NULL) { + Jim_Dict *dict = objPtr->internalRep.dictValue; + + + objPtr->typePtr = &listObjType; + objPtr->internalRep.listValue.len = dict->len; + objPtr->internalRep.listValue.maxLen = dict->maxLen; + objPtr->internalRep.listValue.ele = dict->table; + + + Jim_Free(dict->ht); + + + Jim_Free(dict); + return JIM_OK; + } + + + fileNameObj = Jim_GetSourceInfo(interp, objPtr, &linenr); + Jim_IncrRefCount(fileNameObj); + + + str = Jim_GetString(objPtr, &strLen); + + Jim_FreeIntRep(interp, objPtr); + objPtr->typePtr = &listObjType; + objPtr->internalRep.listValue.len = 0; + objPtr->internalRep.listValue.maxLen = 0; + objPtr->internalRep.listValue.ele = NULL; + + + if (strLen) { + JimParserInit(&parser, str, strLen, linenr); + while (!parser.eof) { + Jim_Obj *elementPtr; + + JimParseList(&parser); + if (parser.tt != JIM_TT_STR && parser.tt != JIM_TT_ESC) + continue; + elementPtr = JimParserGetTokenObj(interp, &parser); + Jim_SetSourceInfo(interp, elementPtr, fileNameObj, parser.tline); + ListAppendElement(objPtr, elementPtr); + } + } + Jim_DecrRefCount(interp, fileNameObj); + return JIM_OK; +} + +Jim_Obj *Jim_NewListObj(Jim_Interp *interp, Jim_Obj *const *elements, int len) +{ + Jim_Obj *objPtr; + + objPtr = Jim_NewObj(interp); + objPtr->typePtr = &listObjType; + objPtr->bytes = NULL; + objPtr->internalRep.listValue.ele = NULL; + objPtr->internalRep.listValue.len = 0; + objPtr->internalRep.listValue.maxLen = 0; + + if (len) { + ListInsertElements(objPtr, 0, len, elements); + } + + return objPtr; +} + +static void JimListGetElements(Jim_Interp *interp, Jim_Obj *listObj, int *listLen, + Jim_Obj ***listVec) +{ + *listLen = Jim_ListLength(interp, listObj); + *listVec = listObj->internalRep.listValue.ele; +} + + +static int JimSign(jim_wide w) +{ + if (w == 0) { + return 0; + } + else if (w < 0) { + return -1; + } + return 1; +} + + +struct lsort_info { + jmp_buf jmpbuf; + Jim_Obj *command; + Jim_Interp *interp; + enum { + JIM_LSORT_ASCII, + JIM_LSORT_NOCASE, + JIM_LSORT_INTEGER, + JIM_LSORT_REAL, + JIM_LSORT_COMMAND, + JIM_LSORT_DICT + } type; + int order; + Jim_Obj **indexv; + int indexc; + int unique; + int (*subfn)(Jim_Obj **, Jim_Obj **); +}; + +static struct lsort_info *sort_info; + +static int ListSortIndexHelper(Jim_Obj **lhsObj, Jim_Obj **rhsObj) +{ + Jim_Obj *lObj, *rObj; + + if (Jim_ListIndices(sort_info->interp, *lhsObj, sort_info->indexv, sort_info->indexc, &lObj, JIM_ERRMSG) != JIM_OK || + Jim_ListIndices(sort_info->interp, *rhsObj, sort_info->indexv, sort_info->indexc, &rObj, JIM_ERRMSG) != JIM_OK) { + longjmp(sort_info->jmpbuf, JIM_ERR); + } + return sort_info->subfn(&lObj, &rObj); +} + + +static int ListSortString(Jim_Obj **lhsObj, Jim_Obj **rhsObj) +{ + return Jim_StringCompareObj(sort_info->interp, *lhsObj, *rhsObj, 0) * sort_info->order; +} + +static int ListSortStringNoCase(Jim_Obj **lhsObj, Jim_Obj **rhsObj) +{ + return Jim_StringCompareObj(sort_info->interp, *lhsObj, *rhsObj, 1) * sort_info->order; +} + +static int ListSortDict(Jim_Obj **lhsObj, Jim_Obj **rhsObj) +{ + + const char *left = Jim_String(*lhsObj); + const char *right = Jim_String(*rhsObj); + + while (1) { + if (isdigit(UCHAR(*left)) && isdigit(UCHAR(*right))) { + + jim_wide lint, rint; + char *lend, *rend; + lint = jim_strtoull(left, &lend); + rint = jim_strtoull(right, &rend); + if (lint != rint) { + return JimSign(lint - rint) * sort_info->order; + } + if (lend -left != rend - right) { + return JimSign((lend - left) - (rend - right)) * sort_info->order; + } + left = lend; + right = rend; + } + else { + int cl, cr; + left += utf8_tounicode_case(left, &cl, 1); + right += utf8_tounicode_case(right, &cr, 1); + if (cl != cr) { + return JimSign(cl - cr) * sort_info->order; + } + if (cl == 0) { + + return Jim_StringCompareObj(sort_info->interp, *lhsObj, *rhsObj, 0) * sort_info->order; + } + } + } +} + +static int ListSortInteger(Jim_Obj **lhsObj, Jim_Obj **rhsObj) +{ + jim_wide lhs = 0, rhs = 0; + + if (Jim_GetWide(sort_info->interp, *lhsObj, &lhs) != JIM_OK || + Jim_GetWide(sort_info->interp, *rhsObj, &rhs) != JIM_OK) { + longjmp(sort_info->jmpbuf, JIM_ERR); + } + + return JimSign(lhs - rhs) * sort_info->order; +} + +static int ListSortReal(Jim_Obj **lhsObj, Jim_Obj **rhsObj) +{ + double lhs = 0, rhs = 0; + + if (Jim_GetDouble(sort_info->interp, *lhsObj, &lhs) != JIM_OK || + Jim_GetDouble(sort_info->interp, *rhsObj, &rhs) != JIM_OK) { + longjmp(sort_info->jmpbuf, JIM_ERR); + } + if (lhs == rhs) { + return 0; + } + if (lhs > rhs) { + return sort_info->order; + } + return -sort_info->order; +} + +static int ListSortCommand(Jim_Obj **lhsObj, Jim_Obj **rhsObj) +{ + Jim_Obj *compare_script; + int rc; + + jim_wide ret = 0; + + + compare_script = Jim_DuplicateObj(sort_info->interp, sort_info->command); + Jim_ListAppendElement(sort_info->interp, compare_script, *lhsObj); + Jim_ListAppendElement(sort_info->interp, compare_script, *rhsObj); + + rc = Jim_EvalObj(sort_info->interp, compare_script); + + if (rc != JIM_OK || Jim_GetWide(sort_info->interp, Jim_GetResult(sort_info->interp), &ret) != JIM_OK) { + longjmp(sort_info->jmpbuf, rc); + } + + return JimSign(ret) * sort_info->order; +} + +static void ListRemoveDuplicates(Jim_Obj *listObjPtr, int (*comp)(Jim_Obj **lhs, Jim_Obj **rhs)) +{ + int src; + int dst = 0; + Jim_Obj **ele = listObjPtr->internalRep.listValue.ele; + + for (src = 1; src < listObjPtr->internalRep.listValue.len; src++) { + if (comp(&ele[dst], &ele[src]) == 0) { + + Jim_DecrRefCount(sort_info->interp, ele[dst]); + } + else { + + dst++; + } + ele[dst] = ele[src]; + } + + + dst++; + if (dst < listObjPtr->internalRep.listValue.len) { + ele[dst] = ele[src]; + } + + + listObjPtr->internalRep.listValue.len = dst; +} + + +static int ListSortElements(Jim_Interp *interp, Jim_Obj *listObjPtr, struct lsort_info *info) +{ + struct lsort_info *prev_info; + + typedef int (qsort_comparator) (const void *, const void *); + int (*fn) (Jim_Obj **, Jim_Obj **); + Jim_Obj **vector; + int len; + int rc; + + JimPanic((Jim_IsShared(listObjPtr), "ListSortElements called with shared object")); + SetListFromAny(interp, listObjPtr); + + + prev_info = sort_info; + sort_info = info; + + vector = listObjPtr->internalRep.listValue.ele; + len = listObjPtr->internalRep.listValue.len; + switch (info->type) { + case JIM_LSORT_ASCII: + fn = ListSortString; + break; + case JIM_LSORT_NOCASE: + fn = ListSortStringNoCase; + break; + case JIM_LSORT_INTEGER: + fn = ListSortInteger; + break; + case JIM_LSORT_REAL: + fn = ListSortReal; + break; + case JIM_LSORT_COMMAND: + fn = ListSortCommand; + break; + case JIM_LSORT_DICT: + fn = ListSortDict; + break; + default: + fn = NULL; + JimPanic((1, "ListSort called with invalid sort type")); + return -1; + } + + if (info->indexc) { + + info->subfn = fn; + fn = ListSortIndexHelper; + } + + if ((rc = setjmp(info->jmpbuf)) == 0) { + qsort(vector, len, sizeof(Jim_Obj *), (qsort_comparator *) fn); + + if (info->unique && len > 1) { + ListRemoveDuplicates(listObjPtr, fn); + } + + Jim_InvalidateStringRep(listObjPtr); + } + sort_info = prev_info; + + return rc; +} + + +static void ListEnsureLength(Jim_Obj *listPtr, int idx) +{ + assert(idx >= 0); + if (idx >= listPtr->internalRep.listValue.maxLen) { + if (idx < 4) { + + idx = 4; + } + listPtr->internalRep.listValue.ele = Jim_Realloc(listPtr->internalRep.listValue.ele, + sizeof(Jim_Obj *) * idx); + + listPtr->internalRep.listValue.maxLen = idx; + } +} + +static void ListInsertElements(Jim_Obj *listPtr, int idx, int elemc, Jim_Obj *const *elemVec) +{ + int currentLen = listPtr->internalRep.listValue.len; + int requiredLen = currentLen + elemc; + int i; + Jim_Obj **point; + + if (elemc == 0) { + + return; + } + + if (requiredLen > listPtr->internalRep.listValue.maxLen) { + if (currentLen) { + + requiredLen *= 2; + } + ListEnsureLength(listPtr, requiredLen); + } + if (idx < 0) { + idx = currentLen; + } + point = listPtr->internalRep.listValue.ele + idx; + memmove(point + elemc, point, (currentLen - idx) * sizeof(Jim_Obj *)); + for (i = 0; i < elemc; ++i) { + point[i] = elemVec[i]; + Jim_IncrRefCount(point[i]); + } + listPtr->internalRep.listValue.len += elemc; +} + +static void ListAppendElement(Jim_Obj *listPtr, Jim_Obj *objPtr) +{ + ListInsertElements(listPtr, -1, 1, &objPtr); +} + +static void ListAppendList(Jim_Obj *listPtr, Jim_Obj *appendListPtr) +{ + ListInsertElements(listPtr, -1, + appendListPtr->internalRep.listValue.len, appendListPtr->internalRep.listValue.ele); +} + +void Jim_ListAppendElement(Jim_Interp *interp, Jim_Obj *listPtr, Jim_Obj *objPtr) +{ + JimPanic((Jim_IsShared(listPtr), "Jim_ListAppendElement called with shared object")); + SetListFromAny(interp, listPtr); + Jim_InvalidateStringRep(listPtr); + ListAppendElement(listPtr, objPtr); +} + +void Jim_ListAppendList(Jim_Interp *interp, Jim_Obj *listPtr, Jim_Obj *appendListPtr) +{ + JimPanic((Jim_IsShared(listPtr), "Jim_ListAppendList called with shared object")); + SetListFromAny(interp, listPtr); + SetListFromAny(interp, appendListPtr); + Jim_InvalidateStringRep(listPtr); + ListAppendList(listPtr, appendListPtr); +} + +int Jim_ListLength(Jim_Interp *interp, Jim_Obj *objPtr) +{ + SetListFromAny(interp, objPtr); + return objPtr->internalRep.listValue.len; +} + +void Jim_ListInsertElements(Jim_Interp *interp, Jim_Obj *listPtr, int idx, + int objc, Jim_Obj *const *objVec) +{ + JimPanic((Jim_IsShared(listPtr), "Jim_ListInsertElement called with shared object")); + SetListFromAny(interp, listPtr); + if (idx >= 0 && idx > listPtr->internalRep.listValue.len) + idx = listPtr->internalRep.listValue.len; + else if (idx < 0) + idx = 0; + Jim_InvalidateStringRep(listPtr); + ListInsertElements(listPtr, idx, objc, objVec); +} + +Jim_Obj *Jim_ListGetIndex(Jim_Interp *interp, Jim_Obj *listPtr, int idx) +{ + SetListFromAny(interp, listPtr); + if ((idx >= 0 && idx >= listPtr->internalRep.listValue.len) || + (idx < 0 && (-idx - 1) >= listPtr->internalRep.listValue.len)) { + return NULL; + } + if (idx < 0) + idx = listPtr->internalRep.listValue.len + idx; + return listPtr->internalRep.listValue.ele[idx]; +} + +int Jim_ListIndex(Jim_Interp *interp, Jim_Obj *listPtr, int idx, Jim_Obj **objPtrPtr, int flags) +{ + *objPtrPtr = Jim_ListGetIndex(interp, listPtr, idx); + if (*objPtrPtr == NULL) { + if (flags & JIM_ERRMSG) { + Jim_SetResultString(interp, "list index out of range", -1); + } + return JIM_ERR; + } + return JIM_OK; +} + +static int Jim_ListIndices(Jim_Interp *interp, Jim_Obj *listPtr, + Jim_Obj *const *indexv, int indexc, Jim_Obj **resultObj, int flags) +{ + int i; + int static_idxes[5]; + int *idxes = static_idxes; + int ret = JIM_OK; + + if (indexc > sizeof(static_idxes) / sizeof(*static_idxes)) { + idxes = Jim_Alloc(indexc * sizeof(*idxes)); + } + + for (i = 0; i < indexc; i++) { + ret = Jim_GetIndex(interp, indexv[i], &idxes[i]); + if (ret != JIM_OK) { + goto err; + } + } + + for (i = 0; i < indexc; i++) { + Jim_Obj *objPtr = Jim_ListGetIndex(interp, listPtr, idxes[i]); + if (!objPtr) { + if (flags & JIM_ERRMSG) { + if (idxes[i] < 0 || idxes[i] > Jim_ListLength(interp, listPtr)) { + Jim_SetResultFormatted(interp, "index \"%#s\" out of range", indexv[i]); + } + else { + Jim_SetResultFormatted(interp, "element %#s missing from sublist \"%#s\"", indexv[i], listPtr); + } + } + return -1; + } + listPtr = objPtr; + } + *resultObj = listPtr; +err: + if (idxes != static_idxes) + Jim_Free(idxes); + return ret; +} + +static int ListSetIndex(Jim_Interp *interp, Jim_Obj *listPtr, int idx, + Jim_Obj *newObjPtr, int flags) +{ + SetListFromAny(interp, listPtr); + if ((idx >= 0 && idx >= listPtr->internalRep.listValue.len) || + (idx < 0 && (-idx - 1) >= listPtr->internalRep.listValue.len)) { + if (flags & JIM_ERRMSG) { + Jim_SetResultString(interp, "list index out of range", -1); + } + return JIM_ERR; + } + if (idx < 0) + idx = listPtr->internalRep.listValue.len + idx; + Jim_DecrRefCount(interp, listPtr->internalRep.listValue.ele[idx]); + listPtr->internalRep.listValue.ele[idx] = newObjPtr; + Jim_IncrRefCount(newObjPtr); + return JIM_OK; +} + +int Jim_ListSetIndex(Jim_Interp *interp, Jim_Obj *varNamePtr, + Jim_Obj *const *indexv, int indexc, Jim_Obj *newObjPtr) +{ + Jim_Obj *varObjPtr, *objPtr, *listObjPtr; + int shared, i, idx; + + varObjPtr = objPtr = Jim_GetVariable(interp, varNamePtr, JIM_ERRMSG | JIM_UNSHARED); + if (objPtr == NULL) + return JIM_ERR; + if ((shared = Jim_IsShared(objPtr))) + varObjPtr = objPtr = Jim_DuplicateObj(interp, objPtr); + for (i = 0; i < indexc - 1; i++) { + listObjPtr = objPtr; + if (Jim_GetIndex(interp, indexv[i], &idx) != JIM_OK) + goto err; + + objPtr = Jim_ListGetIndex(interp, listObjPtr, idx); + if (objPtr == NULL) { + Jim_SetResultFormatted(interp, "index \"%#s\" out of range", indexv[i]); + goto err; + } + if (Jim_IsShared(objPtr)) { + objPtr = Jim_DuplicateObj(interp, objPtr); + ListSetIndex(interp, listObjPtr, idx, objPtr, JIM_NONE); + } + Jim_InvalidateStringRep(listObjPtr); + } + if (Jim_GetIndex(interp, indexv[indexc - 1], &idx) != JIM_OK) + goto err; + if (ListSetIndex(interp, objPtr, idx, newObjPtr, JIM_ERRMSG) == JIM_ERR) + goto err; + Jim_InvalidateStringRep(objPtr); + Jim_InvalidateStringRep(varObjPtr); + if (Jim_SetVariable(interp, varNamePtr, varObjPtr) != JIM_OK) + goto err; + Jim_SetResult(interp, varObjPtr); + return JIM_OK; + err: + if (shared) { + Jim_FreeNewObj(interp, varObjPtr); + } + return JIM_ERR; +} + +Jim_Obj *Jim_ListJoin(Jim_Interp *interp, Jim_Obj *listObjPtr, const char *joinStr, int joinStrLen) +{ + int i; + int listLen = Jim_ListLength(interp, listObjPtr); + Jim_Obj *resObjPtr = Jim_NewEmptyStringObj(interp); + + for (i = 0; i < listLen; ) { + Jim_AppendObj(interp, resObjPtr, Jim_ListGetIndex(interp, listObjPtr, i)); + if (++i != listLen) { + Jim_AppendString(interp, resObjPtr, joinStr, joinStrLen); + } + } + return resObjPtr; +} + +Jim_Obj *Jim_ConcatObj(Jim_Interp *interp, int objc, Jim_Obj *const *objv) +{ + int i; + + for (i = 0; i < objc; i++) { + if (!Jim_IsList(objv[i])) + break; + } + if (i == objc) { + Jim_Obj *objPtr = Jim_NewListObj(interp, NULL, 0); + + for (i = 0; i < objc; i++) + ListAppendList(objPtr, objv[i]); + return objPtr; + } + else { + + int len = 0, objLen; + char *bytes, *p; + + + for (i = 0; i < objc; i++) { + len += Jim_Length(objv[i]); + } + if (objc) + len += objc - 1; + + p = bytes = Jim_Alloc(len + 1); + for (i = 0; i < objc; i++) { + const char *s = Jim_GetString(objv[i], &objLen); + + + while (objLen && isspace(UCHAR(*s))) { + s++; + objLen--; + len--; + } + + while (objLen && isspace(UCHAR(s[objLen - 1]))) { + + if (objLen > 1 && s[objLen - 2] == '\\') { + break; + } + objLen--; + len--; + } + memcpy(p, s, objLen); + p += objLen; + if (i + 1 != objc) { + if (objLen) + *p++ = ' '; + else { + len--; + } + } + } + *p = '\0'; + return Jim_NewStringObjNoAlloc(interp, bytes, len); + } +} + +Jim_Obj *Jim_ListRange(Jim_Interp *interp, Jim_Obj *listObjPtr, Jim_Obj *firstObjPtr, + Jim_Obj *lastObjPtr) +{ + int first, last; + int len, rangeLen; + + if (Jim_GetIndex(interp, firstObjPtr, &first) != JIM_OK || + Jim_GetIndex(interp, lastObjPtr, &last) != JIM_OK) + return NULL; + len = Jim_ListLength(interp, listObjPtr); + first = JimRelToAbsIndex(len, first); + last = JimRelToAbsIndex(len, last); + JimRelToAbsRange(len, &first, &last, &rangeLen); + if (first == 0 && last == len) { + return listObjPtr; + } + return Jim_NewListObj(interp, listObjPtr->internalRep.listValue.ele + first, rangeLen); +} + +static void FreeDictInternalRep(Jim_Interp *interp, Jim_Obj *objPtr); +static void DupDictInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr); +static void UpdateStringOfDict(struct Jim_Obj *objPtr); +static int SetDictFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr); + + +static const Jim_ObjType dictObjType = { + "dict", + FreeDictInternalRep, + DupDictInternalRep, + UpdateStringOfDict, + JIM_TYPE_NONE, +}; + +static void JimFreeDict(Jim_Interp *interp, Jim_Dict *dict) +{ + int i; + for (i = 0; i < dict->len; i++) { + Jim_DecrRefCount(interp, dict->table[i]); + } + Jim_Free(dict->table); + Jim_Free(dict->ht); + Jim_Free(dict); +} + +enum { + DICT_HASH_FIND = -1, + DICT_HASH_REMOVE = -2, + DICT_HASH_ADD = -3, +}; + +static int JimDictHashFind(Jim_Dict *dict, Jim_Obj *keyObjPtr, int op_tvoffset) +{ + unsigned h = (JimObjectHTHashFunction(keyObjPtr) + dict->uniq); + unsigned idx = h & dict->sizemask; + int tvoffset = 0; + unsigned peturb = h; + unsigned first_removed = ~0; + + if (dict->len) { + while ((tvoffset = dict->ht[idx].offset)) { + if (tvoffset == -1) { + if (first_removed == ~0) { + first_removed = idx; + } + } + else if (dict->ht[idx].hash == h) { + if (Jim_StringEqObj(keyObjPtr, dict->table[tvoffset - 1])) { + break; + } + } + + peturb >>= 5; + idx = (5 * idx + 1 + peturb) & dict->sizemask; + } + } + + switch (op_tvoffset) { + case DICT_HASH_FIND: + + break; + case DICT_HASH_REMOVE: + if (tvoffset) { + + dict->ht[idx].offset = -1; + dict->dummy++; + } + + break; + case DICT_HASH_ADD: + if (tvoffset == 0) { + + if (first_removed != ~0) { + idx = first_removed; + dict->dummy--; + } + dict->ht[idx].offset = dict->len + 1; + dict->ht[idx].hash = h; + } + + break; + default: + assert(tvoffset); + + dict->ht[idx].offset = op_tvoffset; + break; + } + + return tvoffset; +} + +static void JimDictExpandHashTable(Jim_Dict *dict, unsigned int size) +{ + int i; + struct JimDictHashEntry *prevht = dict->ht; + int prevsize = dict->size; + + dict->size = JimHashTableNextPower(size); + dict->sizemask = dict->size - 1; + + + dict->ht = Jim_Alloc(dict->size * sizeof(*dict->ht)); + memset(dict->ht, 0, dict->size * sizeof(*dict->ht)); + + + for (i = 0; i < prevsize; i++) { + if (prevht[i].offset > 0) { + + unsigned h = prevht[i].hash; + unsigned idx = h & dict->sizemask; + unsigned peturb = h; + + while (dict->ht[idx].offset) { + peturb >>= 5; + idx = (5 * idx + 1 + peturb) & dict->sizemask; + } + dict->ht[idx].offset = prevht[i].offset; + dict->ht[idx].hash = h; + } + } + Jim_Free(prevht); +} + +static int JimDictAdd(Jim_Dict *dict, Jim_Obj *keyObjPtr) +{ + if (dict->size <= dict->len + dict->dummy) { + JimDictExpandHashTable(dict, dict->size ? dict->size * 2 : 8); + } + return JimDictHashFind(dict, keyObjPtr, DICT_HASH_ADD); +} + +static Jim_Dict *JimDictNew(Jim_Interp *interp, int table_size, int ht_size) +{ + Jim_Dict *dict = Jim_Alloc(sizeof(*dict)); + memset(dict, 0, sizeof(*dict)); + + if (ht_size) { + JimDictExpandHashTable(dict, ht_size); + } + if (table_size) { + dict->table = Jim_Alloc(table_size * sizeof(*dict->table)); + dict->maxLen = table_size; + } +#ifdef JIM_RANDOMISE_HASH + dict->uniq = (rand() ^ time(NULL) ^ clock()); +#endif + return dict; +} + +static void FreeDictInternalRep(Jim_Interp *interp, Jim_Obj *objPtr) +{ + JimFreeDict(interp, objPtr->internalRep.dictValue); +} + +static void DupDictInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr) +{ + Jim_Dict *oldDict = srcPtr->internalRep.dictValue; + int i; + + + Jim_Dict *newDict = JimDictNew(interp, oldDict->maxLen, oldDict->size); + + + for (i = 0; i < oldDict->len; i++) { + newDict->table[i] = oldDict->table[i]; + Jim_IncrRefCount(newDict->table[i]); + } + newDict->len = oldDict->len; + + + newDict->uniq = oldDict->uniq; + + + memcpy(newDict->ht, oldDict->ht, sizeof(*oldDict->ht) * oldDict->size); + + dupPtr->internalRep.dictValue = newDict; + dupPtr->typePtr = &dictObjType; +} + +static void UpdateStringOfDict(struct Jim_Obj *objPtr) +{ + JimMakeListStringRep(objPtr, objPtr->internalRep.dictValue->table, objPtr->internalRep.dictValue->len); +} + +static int SetDictFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr) +{ + int listlen; + + if (objPtr->typePtr == &dictObjType) { + return JIM_OK; + } + + if (Jim_IsList(objPtr) && Jim_IsShared(objPtr)) { + Jim_String(objPtr); + } + + listlen = Jim_ListLength(interp, objPtr); + if (listlen % 2) { + Jim_SetResultString(interp, "missing value to go with key", -1); + return JIM_ERR; + } + else { + + Jim_Dict *dict = JimDictNew(interp, 0, listlen); + int i; + + + dict->table = objPtr->internalRep.listValue.ele; + dict->maxLen = objPtr->internalRep.listValue.maxLen; + + + for (i = 0; i < listlen; i += 2) { + int tvoffset = JimDictAdd(dict, dict->table[i]); + if (tvoffset) { + + + Jim_DecrRefCount(interp, dict->table[tvoffset]); + + dict->table[tvoffset] = dict->table[i + 1]; + + Jim_DecrRefCount(interp, dict->table[i]); + } + else { + if (dict->len != i) { + dict->table[dict->len++] = dict->table[i]; + dict->table[dict->len++] = dict->table[i + 1]; + } + else { + dict->len += 2; + } + } + } + + objPtr->typePtr = &dictObjType; + objPtr->internalRep.dictValue = dict; + + return JIM_OK; + } +} + + + +static int DictAddElement(Jim_Interp *interp, Jim_Obj *objPtr, + Jim_Obj *keyObjPtr, Jim_Obj *valueObjPtr) +{ + Jim_Dict *dict = objPtr->internalRep.dictValue; + if (valueObjPtr == NULL) { + + int tvoffset = JimDictHashFind(dict, keyObjPtr, DICT_HASH_REMOVE); + if (tvoffset) { + + Jim_DecrRefCount(interp, dict->table[tvoffset - 1]); + Jim_DecrRefCount(interp, dict->table[tvoffset]); + dict->len -= 2; + if (tvoffset != dict->len + 1) { + + dict->table[tvoffset - 1] = dict->table[dict->len]; + dict->table[tvoffset] = dict->table[dict->len + 1]; + + + JimDictHashFind(dict, dict->table[tvoffset - 1], tvoffset); + } + return JIM_OK; + } + return JIM_ERR; + } + else { + + int tvoffset = JimDictAdd(dict, keyObjPtr); + if (tvoffset) { + + Jim_IncrRefCount(valueObjPtr); + Jim_DecrRefCount(interp, dict->table[tvoffset]); + dict->table[tvoffset] = valueObjPtr; + } + else { + if (dict->maxLen == dict->len) { + + if (dict->maxLen < 4) { + dict->maxLen = 4; + } + else { + dict->maxLen *= 2; + } + dict->table = Jim_Realloc(dict->table, dict->maxLen * sizeof(*dict->table)); + } + Jim_IncrRefCount(keyObjPtr); + Jim_IncrRefCount(valueObjPtr); + + dict->table[dict->len++] = keyObjPtr; + dict->table[dict->len++] = valueObjPtr; + + } + return JIM_OK; + } +} + +int Jim_DictAddElement(Jim_Interp *interp, Jim_Obj *objPtr, + Jim_Obj *keyObjPtr, Jim_Obj *valueObjPtr) +{ + JimPanic((Jim_IsShared(objPtr), "Jim_DictAddElement called with shared object")); + if (SetDictFromAny(interp, objPtr) != JIM_OK) { + return JIM_ERR; + } + Jim_InvalidateStringRep(objPtr); + return DictAddElement(interp, objPtr, keyObjPtr, valueObjPtr); +} + +Jim_Obj *Jim_NewDictObj(Jim_Interp *interp, Jim_Obj *const *elements, int len) +{ + Jim_Obj *objPtr; + int i; + + JimPanic((len % 2, "Jim_NewDictObj() 'len' argument must be even")); + + objPtr = Jim_NewObj(interp); + objPtr->typePtr = &dictObjType; + objPtr->bytes = NULL; + + objPtr->internalRep.dictValue = JimDictNew(interp, len, len); + for (i = 0; i < len; i += 2) + DictAddElement(interp, objPtr, elements[i], elements[i + 1]); + return objPtr; +} + +int Jim_DictKey(Jim_Interp *interp, Jim_Obj *dictPtr, Jim_Obj *keyPtr, + Jim_Obj **objPtrPtr, int flags) +{ + int tvoffset; + Jim_Dict *dict; + + if (SetDictFromAny(interp, dictPtr) != JIM_OK) { + return -1; + } + dict = dictPtr->internalRep.dictValue; + tvoffset = JimDictHashFind(dict, keyPtr, DICT_HASH_FIND); + if (tvoffset == 0) { + if (flags & JIM_ERRMSG) { + Jim_SetResultFormatted(interp, "key \"%#s\" not known in dictionary", keyPtr); + } + return JIM_ERR; + } + *objPtrPtr = dict->table[tvoffset]; + return JIM_OK; +} + +Jim_Obj **Jim_DictPairs(Jim_Interp *interp, Jim_Obj *dictPtr, int *len) +{ + + if (Jim_IsList(dictPtr)) { + Jim_Obj **table; + JimListGetElements(interp, dictPtr, len, &table); + if (*len % 2 == 0) { + return table; + } + + } + if (SetDictFromAny(interp, dictPtr) != JIM_OK) { + + *len = 1; + return NULL; + } + *len = dictPtr->internalRep.dictValue->len; + return dictPtr->internalRep.dictValue->table; +} + + +int Jim_DictKeysVector(Jim_Interp *interp, Jim_Obj *dictPtr, + Jim_Obj *const *keyv, int keyc, Jim_Obj **objPtrPtr, int flags) +{ + int i; + + if (keyc == 0) { + *objPtrPtr = dictPtr; + return JIM_OK; + } + + for (i = 0; i < keyc; i++) { + Jim_Obj *objPtr; + + int rc = Jim_DictKey(interp, dictPtr, keyv[i], &objPtr, flags); + if (rc != JIM_OK) { + return rc; + } + dictPtr = objPtr; + } + *objPtrPtr = dictPtr; + return JIM_OK; +} + +int Jim_SetDictKeysVector(Jim_Interp *interp, Jim_Obj *varNamePtr, + Jim_Obj *const *keyv, int keyc, Jim_Obj *newObjPtr, int flags) +{ + Jim_Obj *varObjPtr, *objPtr, *dictObjPtr; + int shared, i; + + varObjPtr = objPtr = Jim_GetVariable(interp, varNamePtr, flags); + if (objPtr == NULL) { + if (newObjPtr == NULL && (flags & JIM_MUSTEXIST)) { + + return JIM_ERR; + } + varObjPtr = objPtr = Jim_NewDictObj(interp, NULL, 0); + if (Jim_SetVariable(interp, varNamePtr, objPtr) != JIM_OK) { + Jim_FreeNewObj(interp, varObjPtr); + return JIM_ERR; + } + } + if ((shared = Jim_IsShared(objPtr))) + varObjPtr = objPtr = Jim_DuplicateObj(interp, objPtr); + for (i = 0; i < keyc; i++) { + dictObjPtr = objPtr; + + + if (SetDictFromAny(interp, dictObjPtr) != JIM_OK) { + goto err; + } + + if (i == keyc - 1) { + + if (Jim_DictAddElement(interp, objPtr, keyv[keyc - 1], newObjPtr) != JIM_OK) { + if (newObjPtr || (flags & JIM_MUSTEXIST)) { + goto err; + } + } + break; + } + + + Jim_InvalidateStringRep(dictObjPtr); + if (Jim_DictKey(interp, dictObjPtr, keyv[i], &objPtr, + newObjPtr ? JIM_NONE : JIM_ERRMSG) == JIM_OK) { + if (Jim_IsShared(objPtr)) { + objPtr = Jim_DuplicateObj(interp, objPtr); + DictAddElement(interp, dictObjPtr, keyv[i], objPtr); + } + } + else { + if (newObjPtr == NULL) { + goto err; + } + objPtr = Jim_NewDictObj(interp, NULL, 0); + DictAddElement(interp, dictObjPtr, keyv[i], objPtr); + } + } + + Jim_InvalidateStringRep(objPtr); + Jim_InvalidateStringRep(varObjPtr); + if (Jim_SetVariable(interp, varNamePtr, varObjPtr) != JIM_OK) { + goto err; + } + + if (!(flags & JIM_NORESULT)) { + Jim_SetResult(interp, varObjPtr); + } + return JIM_OK; + err: + if (shared) { + Jim_FreeNewObj(interp, varObjPtr); + } + return JIM_ERR; +} + +static void UpdateStringOfIndex(struct Jim_Obj *objPtr); +static int SetIndexFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr); + +static const Jim_ObjType indexObjType = { + "index", + NULL, + NULL, + UpdateStringOfIndex, + JIM_TYPE_NONE, +}; + +static void UpdateStringOfIndex(struct Jim_Obj *objPtr) +{ + if (objPtr->internalRep.intValue == -1) { + JimSetStringBytes(objPtr, "end"); + } + else { + char buf[JIM_INTEGER_SPACE + 1]; + if (objPtr->internalRep.intValue >= 0 || objPtr->internalRep.intValue == -INT_MAX) { + sprintf(buf, "%d", objPtr->internalRep.intValue); + } + else { + + sprintf(buf, "end%d", objPtr->internalRep.intValue + 1); + } + JimSetStringBytes(objPtr, buf); + } +} + +static int SetIndexFromAny(Jim_Interp *interp, Jim_Obj *objPtr) +{ + jim_wide idx; + int end = 0; + const char *str; + Jim_Obj *exprObj = objPtr; + + JimPanic((objPtr->refCount == 0, "SetIndexFromAny() called with zero refcount object")); + + + str = Jim_String(objPtr); + + + if (strncmp(str, "end", 3) == 0) { + end = 1; + str += 3; + idx = 0; + switch (*str) { + case '\0': + exprObj = NULL; + break; + + case '-': + case '+': + exprObj = Jim_NewStringObj(interp, str, -1); + break; + + default: + goto badindex; + } + } + if (exprObj) { + int ret; + Jim_IncrRefCount(exprObj); + ret = Jim_GetWideExpr(interp, exprObj, &idx); + Jim_DecrRefCount(interp, exprObj); + if (ret != JIM_OK) { + goto badindex; + } + } + + if (end) { + if (idx > 0) { + idx = INT_MAX; + } + else { + + idx--; + } + } + else if (idx < 0) { + idx = -INT_MAX; + } + + + Jim_FreeIntRep(interp, objPtr); + objPtr->typePtr = &indexObjType; + objPtr->internalRep.intValue = idx; + return JIM_OK; + + badindex: + Jim_SetResultFormatted(interp, + "bad index \"%#s\": must be intexpr or end?[+-]intexpr?", objPtr); + return JIM_ERR; +} + +int Jim_GetIndex(Jim_Interp *interp, Jim_Obj *objPtr, int *indexPtr) +{ + + if (objPtr->typePtr == &intObjType) { + jim_wide val = JimWideValue(objPtr); + + if (val < 0) + *indexPtr = -INT_MAX; + else if (val > INT_MAX) + *indexPtr = INT_MAX; + else + *indexPtr = (int)val; + return JIM_OK; + } + if (objPtr->typePtr != &indexObjType && SetIndexFromAny(interp, objPtr) == JIM_ERR) + return JIM_ERR; + *indexPtr = objPtr->internalRep.intValue; + return JIM_OK; +} + + + +static const char * const jimReturnCodes[] = { + "ok", + "error", + "return", + "break", + "continue", + "signal", + "exit", + "eval", + NULL +}; + +#define jimReturnCodesSize (sizeof(jimReturnCodes)/sizeof(*jimReturnCodes) - 1) + +static const Jim_ObjType returnCodeObjType = { + "return-code", + NULL, + NULL, + NULL, + JIM_TYPE_NONE, +}; + +const char *Jim_ReturnCode(int code) +{ + if (code < 0 || code >= (int)jimReturnCodesSize) { + return "?"; + } + else { + return jimReturnCodes[code]; + } +} + +static int SetReturnCodeFromAny(Jim_Interp *interp, Jim_Obj *objPtr) +{ + int returnCode; + jim_wide wideValue; + + + if (JimGetWideNoErr(interp, objPtr, &wideValue) != JIM_ERR) + returnCode = (int)wideValue; + else if (Jim_GetEnum(interp, objPtr, jimReturnCodes, &returnCode, NULL, JIM_NONE) != JIM_OK) { + Jim_SetResultFormatted(interp, "expected return code but got \"%#s\"", objPtr); + return JIM_ERR; + } + + Jim_FreeIntRep(interp, objPtr); + objPtr->typePtr = &returnCodeObjType; + objPtr->internalRep.intValue = returnCode; + return JIM_OK; +} + +int Jim_GetReturnCode(Jim_Interp *interp, Jim_Obj *objPtr, int *intPtr) +{ + if (objPtr->typePtr != &returnCodeObjType && SetReturnCodeFromAny(interp, objPtr) == JIM_ERR) + return JIM_ERR; + *intPtr = objPtr->internalRep.intValue; + return JIM_OK; +} + +static int JimParseExprOperator(struct JimParserCtx *pc); +static int JimParseExprNumber(struct JimParserCtx *pc); +static int JimParseExprIrrational(struct JimParserCtx *pc); +static int JimParseExprBoolean(struct JimParserCtx *pc); + + +enum +{ + + + + JIM_EXPROP_MUL = JIM_TT_EXPR_OP, + JIM_EXPROP_DIV, + JIM_EXPROP_MOD, + JIM_EXPROP_SUB, + JIM_EXPROP_ADD, + JIM_EXPROP_LSHIFT, + JIM_EXPROP_RSHIFT, + JIM_EXPROP_ROTL, + JIM_EXPROP_ROTR, + JIM_EXPROP_LT, + JIM_EXPROP_GT, + JIM_EXPROP_LTE, + JIM_EXPROP_GTE, + JIM_EXPROP_NUMEQ, + JIM_EXPROP_NUMNE, + JIM_EXPROP_BITAND, + JIM_EXPROP_BITXOR, + JIM_EXPROP_BITOR, + JIM_EXPROP_LOGICAND, + JIM_EXPROP_LOGICOR, + JIM_EXPROP_TERNARY, + JIM_EXPROP_COLON, + JIM_EXPROP_POW, + + + JIM_EXPROP_STREQ, + JIM_EXPROP_STRNE, + JIM_EXPROP_STRIN, + JIM_EXPROP_STRNI, + JIM_EXPROP_STRLT, + JIM_EXPROP_STRGT, + JIM_EXPROP_STRLE, + JIM_EXPROP_STRGE, + + + JIM_EXPROP_NOT, + JIM_EXPROP_BITNOT, + JIM_EXPROP_UNARYMINUS, + JIM_EXPROP_UNARYPLUS, + + + JIM_EXPROP_FUNC_INT, + JIM_EXPROP_FUNC_WIDE, + JIM_EXPROP_FUNC_ABS, + JIM_EXPROP_FUNC_DOUBLE, + JIM_EXPROP_FUNC_ROUND, + JIM_EXPROP_FUNC_RAND, + JIM_EXPROP_FUNC_SRAND, + + + JIM_EXPROP_FUNC_SIN, + JIM_EXPROP_FUNC_COS, + JIM_EXPROP_FUNC_TAN, + JIM_EXPROP_FUNC_ASIN, + JIM_EXPROP_FUNC_ACOS, + JIM_EXPROP_FUNC_ATAN, + JIM_EXPROP_FUNC_ATAN2, + JIM_EXPROP_FUNC_SINH, + JIM_EXPROP_FUNC_COSH, + JIM_EXPROP_FUNC_TANH, + JIM_EXPROP_FUNC_CEIL, + JIM_EXPROP_FUNC_FLOOR, + JIM_EXPROP_FUNC_EXP, + JIM_EXPROP_FUNC_LOG, + JIM_EXPROP_FUNC_LOG10, + JIM_EXPROP_FUNC_SQRT, + JIM_EXPROP_FUNC_POW, + JIM_EXPROP_FUNC_HYPOT, + JIM_EXPROP_FUNC_FMOD, +}; + +struct JimExprNode { + int type; + struct Jim_Obj *objPtr; + + struct JimExprNode *left; + struct JimExprNode *right; + struct JimExprNode *ternary; +}; + + +typedef struct Jim_ExprOperator +{ + const char *name; + int (*funcop) (Jim_Interp *interp, struct JimExprNode *opnode); + unsigned char precedence; + unsigned char arity; + unsigned char attr; + unsigned char namelen; +} Jim_ExprOperator; + +static int JimExprGetTerm(Jim_Interp *interp, struct JimExprNode *node, Jim_Obj **objPtrPtr); +static int JimExprGetTermBoolean(Jim_Interp *interp, struct JimExprNode *node); +static int JimExprEvalTermNode(Jim_Interp *interp, struct JimExprNode *node); + +static int JimExprOpNumUnary(Jim_Interp *interp, struct JimExprNode *node) +{ + int intresult = 1; + int rc, bA = 0; + double dA, dC = 0; + jim_wide wA, wC = 0; + Jim_Obj *A; + + if ((rc = JimExprGetTerm(interp, node->left, &A)) != JIM_OK) { + return rc; + } + + if ((A->typePtr != &doubleObjType || A->bytes) && JimGetWideNoErr(interp, A, &wA) == JIM_OK) { + switch (node->type) { + case JIM_EXPROP_FUNC_INT: + case JIM_EXPROP_FUNC_WIDE: + case JIM_EXPROP_FUNC_ROUND: + case JIM_EXPROP_UNARYPLUS: + wC = wA; + break; + case JIM_EXPROP_FUNC_DOUBLE: + dC = wA; + intresult = 0; + break; + case JIM_EXPROP_FUNC_ABS: + wC = wA >= 0 ? wA : -wA; + break; + case JIM_EXPROP_UNARYMINUS: + wC = -wA; + break; + case JIM_EXPROP_NOT: + wC = !wA; + break; + default: + abort(); + } + } + else if ((rc = Jim_GetDouble(interp, A, &dA)) == JIM_OK) { + switch (node->type) { + case JIM_EXPROP_FUNC_INT: + case JIM_EXPROP_FUNC_WIDE: + wC = dA; + break; + case JIM_EXPROP_FUNC_ROUND: + wC = dA < 0 ? (dA - 0.5) : (dA + 0.5); + break; + case JIM_EXPROP_FUNC_DOUBLE: + case JIM_EXPROP_UNARYPLUS: + dC = dA; + intresult = 0; + break; + case JIM_EXPROP_FUNC_ABS: +#ifdef JIM_MATH_FUNCTIONS + dC = fabs(dA); +#else + dC = dA >= 0 ? dA : -dA; +#endif + intresult = 0; + break; + case JIM_EXPROP_UNARYMINUS: + dC = -dA; + intresult = 0; + break; + case JIM_EXPROP_NOT: + wC = !dA; + break; + default: + abort(); + } + } + else if ((rc = Jim_GetBoolean(interp, A, &bA)) == JIM_OK) { + switch (node->type) { + case JIM_EXPROP_NOT: + wC = !bA; + break; + default: + abort(); + } + } + + if (rc == JIM_OK) { + if (intresult) { + Jim_SetResultInt(interp, wC); + } + else { + Jim_SetResult(interp, Jim_NewDoubleObj(interp, dC)); + } + } + + Jim_DecrRefCount(interp, A); + + return rc; +} + +static double JimRandDouble(Jim_Interp *interp) +{ + unsigned long x; + JimRandomBytes(interp, &x, sizeof(x)); + + return (double)x / (double)~0UL; +} + +static int JimExprOpIntUnary(Jim_Interp *interp, struct JimExprNode *node) +{ + jim_wide wA; + Jim_Obj *A; + int rc; + + if ((rc = JimExprGetTerm(interp, node->left, &A)) != JIM_OK) { + return rc; + } + + rc = Jim_GetWide(interp, A, &wA); + if (rc == JIM_OK) { + switch (node->type) { + case JIM_EXPROP_BITNOT: + Jim_SetResultInt(interp, ~wA); + break; + case JIM_EXPROP_FUNC_SRAND: + JimPrngSeed(interp, (unsigned char *)&wA, sizeof(wA)); + Jim_SetResult(interp, Jim_NewDoubleObj(interp, JimRandDouble(interp))); + break; + default: + abort(); + } + } + + Jim_DecrRefCount(interp, A); + + return rc; +} + +static int JimExprOpNone(Jim_Interp *interp, struct JimExprNode *node) +{ + JimPanic((node->type != JIM_EXPROP_FUNC_RAND, "JimExprOpNone only support rand()")); + + Jim_SetResult(interp, Jim_NewDoubleObj(interp, JimRandDouble(interp))); + + return JIM_OK; +} + +#ifdef JIM_MATH_FUNCTIONS +static int JimExprOpDoubleUnary(Jim_Interp *interp, struct JimExprNode *node) +{ + int rc; + double dA, dC; + Jim_Obj *A; + + if ((rc = JimExprGetTerm(interp, node->left, &A)) != JIM_OK) { + return rc; + } + + rc = Jim_GetDouble(interp, A, &dA); + if (rc == JIM_OK) { + switch (node->type) { + case JIM_EXPROP_FUNC_SIN: + dC = sin(dA); + break; + case JIM_EXPROP_FUNC_COS: + dC = cos(dA); + break; + case JIM_EXPROP_FUNC_TAN: + dC = tan(dA); + break; + case JIM_EXPROP_FUNC_ASIN: + dC = asin(dA); + break; + case JIM_EXPROP_FUNC_ACOS: + dC = acos(dA); + break; + case JIM_EXPROP_FUNC_ATAN: + dC = atan(dA); + break; + case JIM_EXPROP_FUNC_SINH: + dC = sinh(dA); + break; + case JIM_EXPROP_FUNC_COSH: + dC = cosh(dA); + break; + case JIM_EXPROP_FUNC_TANH: + dC = tanh(dA); + break; + case JIM_EXPROP_FUNC_CEIL: + dC = ceil(dA); + break; + case JIM_EXPROP_FUNC_FLOOR: + dC = floor(dA); + break; + case JIM_EXPROP_FUNC_EXP: + dC = exp(dA); + break; + case JIM_EXPROP_FUNC_LOG: + dC = log(dA); + break; + case JIM_EXPROP_FUNC_LOG10: + dC = log10(dA); + break; + case JIM_EXPROP_FUNC_SQRT: + dC = sqrt(dA); + break; + default: + abort(); + } + Jim_SetResult(interp, Jim_NewDoubleObj(interp, dC)); + } + + Jim_DecrRefCount(interp, A); + + return rc; +} +#endif + + +static int JimExprOpIntBin(Jim_Interp *interp, struct JimExprNode *node) +{ + jim_wide wA, wB; + int rc; + Jim_Obj *A, *B; + + if ((rc = JimExprGetTerm(interp, node->left, &A)) != JIM_OK) { + return rc; + } + if ((rc = JimExprGetTerm(interp, node->right, &B)) != JIM_OK) { + Jim_DecrRefCount(interp, A); + return rc; + } + + rc = JIM_ERR; + + if (Jim_GetWide(interp, A, &wA) == JIM_OK && Jim_GetWide(interp, B, &wB) == JIM_OK) { + jim_wide wC; + + rc = JIM_OK; + + switch (node->type) { + case JIM_EXPROP_LSHIFT: + wC = wA << wB; + break; + case JIM_EXPROP_RSHIFT: + wC = wA >> wB; + break; + case JIM_EXPROP_BITAND: + wC = wA & wB; + break; + case JIM_EXPROP_BITXOR: + wC = wA ^ wB; + break; + case JIM_EXPROP_BITOR: + wC = wA | wB; + break; + case JIM_EXPROP_MOD: + if (wB == 0) { + wC = 0; + Jim_SetResultString(interp, "Division by zero", -1); + rc = JIM_ERR; + } + else { + int negative = 0; + + if (wB < 0) { + wB = -wB; + wA = -wA; + negative = 1; + } + wC = wA % wB; + if (wC < 0) { + wC += wB; + } + if (negative) { + wC = -wC; + } + } + break; + case JIM_EXPROP_ROTL: + case JIM_EXPROP_ROTR:{ + + unsigned long uA = (unsigned long)wA; + unsigned long uB = (unsigned long)wB; + const unsigned int S = sizeof(unsigned long) * 8; + + + uB %= S; + + if (node->type == JIM_EXPROP_ROTR) { + uB = S - uB; + } + wC = (unsigned long)(uA << uB) | (uA >> (S - uB)); + break; + } + default: + abort(); + } + Jim_SetResultInt(interp, wC); + } + + Jim_DecrRefCount(interp, A); + Jim_DecrRefCount(interp, B); + + return rc; +} + + + +static int JimExprOpBin(Jim_Interp *interp, struct JimExprNode *node) +{ + int rc = JIM_OK; + double dA, dB, dC = 0; + jim_wide wA, wB, wC = 0; + Jim_Obj *A, *B; + + if ((rc = JimExprGetTerm(interp, node->left, &A)) != JIM_OK) { + return rc; + } + if ((rc = JimExprGetTerm(interp, node->right, &B)) != JIM_OK) { + Jim_DecrRefCount(interp, A); + return rc; + } + + if ((A->typePtr != &doubleObjType || A->bytes) && + (B->typePtr != &doubleObjType || B->bytes) && + JimGetWideNoErr(interp, A, &wA) == JIM_OK && JimGetWideNoErr(interp, B, &wB) == JIM_OK) { + + + + switch (node->type) { + case JIM_EXPROP_POW: + case JIM_EXPROP_FUNC_POW: + if (wA == 0 && wB < 0) { + Jim_SetResultString(interp, "exponentiation of zero by negative power", -1); + rc = JIM_ERR; + goto done; + } + wC = JimPowWide(wA, wB); + goto intresult; + case JIM_EXPROP_ADD: + wC = wA + wB; + goto intresult; + case JIM_EXPROP_SUB: + wC = wA - wB; + goto intresult; + case JIM_EXPROP_MUL: + wC = wA * wB; + goto intresult; + case JIM_EXPROP_DIV: + if (wB == 0) { + Jim_SetResultString(interp, "Division by zero", -1); + rc = JIM_ERR; + goto done; + } + else { + if (wB < 0) { + wB = -wB; + wA = -wA; + } + wC = wA / wB; + if (wA % wB < 0) { + wC--; + } + goto intresult; + } + case JIM_EXPROP_LT: + wC = wA < wB; + goto intresult; + case JIM_EXPROP_GT: + wC = wA > wB; + goto intresult; + case JIM_EXPROP_LTE: + wC = wA <= wB; + goto intresult; + case JIM_EXPROP_GTE: + wC = wA >= wB; + goto intresult; + case JIM_EXPROP_NUMEQ: + wC = wA == wB; + goto intresult; + case JIM_EXPROP_NUMNE: + wC = wA != wB; + goto intresult; + } + } + if (Jim_GetDouble(interp, A, &dA) == JIM_OK && Jim_GetDouble(interp, B, &dB) == JIM_OK) { + switch (node->type) { +#ifndef JIM_MATH_FUNCTIONS + case JIM_EXPROP_POW: + case JIM_EXPROP_FUNC_POW: + case JIM_EXPROP_FUNC_ATAN2: + case JIM_EXPROP_FUNC_HYPOT: + case JIM_EXPROP_FUNC_FMOD: + Jim_SetResultString(interp, "unsupported", -1); + rc = JIM_ERR; + goto done; +#else + case JIM_EXPROP_POW: + case JIM_EXPROP_FUNC_POW: + dC = pow(dA, dB); + goto doubleresult; + case JIM_EXPROP_FUNC_ATAN2: + dC = atan2(dA, dB); + goto doubleresult; + case JIM_EXPROP_FUNC_HYPOT: + dC = hypot(dA, dB); + goto doubleresult; + case JIM_EXPROP_FUNC_FMOD: + dC = fmod(dA, dB); + goto doubleresult; +#endif + case JIM_EXPROP_ADD: + dC = dA + dB; + goto doubleresult; + case JIM_EXPROP_SUB: + dC = dA - dB; + goto doubleresult; + case JIM_EXPROP_MUL: + dC = dA * dB; + goto doubleresult; + case JIM_EXPROP_DIV: + if (dB == 0) { +#ifdef INFINITY + dC = dA < 0 ? -INFINITY : INFINITY; +#else + dC = (dA < 0 ? -1.0 : 1.0) * strtod("Inf", NULL); +#endif + } + else { + dC = dA / dB; + } + goto doubleresult; + case JIM_EXPROP_LT: + wC = dA < dB; + goto intresult; + case JIM_EXPROP_GT: + wC = dA > dB; + goto intresult; + case JIM_EXPROP_LTE: + wC = dA <= dB; + goto intresult; + case JIM_EXPROP_GTE: + wC = dA >= dB; + goto intresult; + case JIM_EXPROP_NUMEQ: + wC = dA == dB; + goto intresult; + case JIM_EXPROP_NUMNE: + wC = dA != dB; + goto intresult; + } + } + else { + + + + int i = Jim_StringCompareObj(interp, A, B, 0); + + switch (node->type) { + case JIM_EXPROP_LT: + wC = i < 0; + goto intresult; + case JIM_EXPROP_GT: + wC = i > 0; + goto intresult; + case JIM_EXPROP_LTE: + wC = i <= 0; + goto intresult; + case JIM_EXPROP_GTE: + wC = i >= 0; + goto intresult; + case JIM_EXPROP_NUMEQ: + wC = i == 0; + goto intresult; + case JIM_EXPROP_NUMNE: + wC = i != 0; + goto intresult; + } + } + + rc = JIM_ERR; +done: + Jim_DecrRefCount(interp, A); + Jim_DecrRefCount(interp, B); + return rc; +intresult: + Jim_SetResultInt(interp, wC); + goto done; +doubleresult: + Jim_SetResult(interp, Jim_NewDoubleObj(interp, dC)); + goto done; +} + +static int JimSearchList(Jim_Interp *interp, Jim_Obj *listObjPtr, Jim_Obj *valObj) +{ + int listlen; + int i; + + listlen = Jim_ListLength(interp, listObjPtr); + for (i = 0; i < listlen; i++) { + if (Jim_StringEqObj(Jim_ListGetIndex(interp, listObjPtr, i), valObj)) { + return 1; + } + } + return 0; +} + + + +static int JimExprOpStrBin(Jim_Interp *interp, struct JimExprNode *node) +{ + Jim_Obj *A, *B; + jim_wide wC; + int comp, rc; + + if ((rc = JimExprGetTerm(interp, node->left, &A)) != JIM_OK) { + return rc; + } + if ((rc = JimExprGetTerm(interp, node->right, &B)) != JIM_OK) { + Jim_DecrRefCount(interp, A); + return rc; + } + + switch (node->type) { + case JIM_EXPROP_STREQ: + case JIM_EXPROP_STRNE: + wC = Jim_StringEqObj(A, B); + if (node->type == JIM_EXPROP_STRNE) { + wC = !wC; + } + break; + case JIM_EXPROP_STRLT: + case JIM_EXPROP_STRGT: + case JIM_EXPROP_STRLE: + case JIM_EXPROP_STRGE: + comp = Jim_StringCompareObj(interp, A, B, 0); + if (node->type == JIM_EXPROP_STRLT) { + wC = comp == -1; + } else if (node->type == JIM_EXPROP_STRGT) { + wC = comp == 1; + } else if (node->type == JIM_EXPROP_STRLE) { + wC = comp == -1 || comp == 0; + } else { + wC = comp == 0 || comp == 1; + } + break; + case JIM_EXPROP_STRIN: + wC = JimSearchList(interp, B, A); + break; + case JIM_EXPROP_STRNI: + wC = !JimSearchList(interp, B, A); + break; + default: + abort(); + } + Jim_SetResultInt(interp, wC); + + Jim_DecrRefCount(interp, A); + Jim_DecrRefCount(interp, B); + + return rc; +} + +static int ExprBool(Jim_Interp *interp, Jim_Obj *obj) +{ + long l; + double d; + int b; + int ret = -1; + + + Jim_IncrRefCount(obj); + + if (Jim_GetLong(interp, obj, &l) == JIM_OK) { + ret = (l != 0); + } + else if (Jim_GetDouble(interp, obj, &d) == JIM_OK) { + ret = (d != 0); + } + else if (Jim_GetBoolean(interp, obj, &b) == JIM_OK) { + ret = (b != 0); + } + + Jim_DecrRefCount(interp, obj); + return ret; +} + +static int JimExprOpAnd(Jim_Interp *interp, struct JimExprNode *node) +{ + + int result = JimExprGetTermBoolean(interp, node->left); + + if (result == 1) { + + result = JimExprGetTermBoolean(interp, node->right); + } + if (result == -1) { + return JIM_ERR; + } + Jim_SetResultInt(interp, result); + return JIM_OK; +} + +static int JimExprOpOr(Jim_Interp *interp, struct JimExprNode *node) +{ + + int result = JimExprGetTermBoolean(interp, node->left); + + if (result == 0) { + + result = JimExprGetTermBoolean(interp, node->right); + } + if (result == -1) { + return JIM_ERR; + } + Jim_SetResultInt(interp, result); + return JIM_OK; +} + +static int JimExprOpTernary(Jim_Interp *interp, struct JimExprNode *node) +{ + + int result = JimExprGetTermBoolean(interp, node->left); + + if (result == 1) { + + return JimExprEvalTermNode(interp, node->right); + } + else if (result == 0) { + + return JimExprEvalTermNode(interp, node->ternary); + } + + return JIM_ERR; +} + +enum +{ + OP_FUNC = 0x0001, + OP_RIGHT_ASSOC = 0x0002, +}; + +#define OPRINIT_ATTR(N, P, ARITY, F, ATTR) {N, F, P, ARITY, ATTR, sizeof(N) - 1} +#define OPRINIT(N, P, ARITY, F) OPRINIT_ATTR(N, P, ARITY, F, 0) + +static const struct Jim_ExprOperator Jim_ExprOperators[] = { + OPRINIT("*", 110, 2, JimExprOpBin), + OPRINIT("/", 110, 2, JimExprOpBin), + OPRINIT("%", 110, 2, JimExprOpIntBin), + + OPRINIT("-", 100, 2, JimExprOpBin), + OPRINIT("+", 100, 2, JimExprOpBin), + + OPRINIT("<<", 90, 2, JimExprOpIntBin), + OPRINIT(">>", 90, 2, JimExprOpIntBin), + + OPRINIT("<<<", 90, 2, JimExprOpIntBin), + OPRINIT(">>>", 90, 2, JimExprOpIntBin), + + OPRINIT("<", 80, 2, JimExprOpBin), + OPRINIT(">", 80, 2, JimExprOpBin), + OPRINIT("<=", 80, 2, JimExprOpBin), + OPRINIT(">=", 80, 2, JimExprOpBin), + + OPRINIT("==", 70, 2, JimExprOpBin), + OPRINIT("!=", 70, 2, JimExprOpBin), + + OPRINIT("&", 50, 2, JimExprOpIntBin), + OPRINIT("^", 49, 2, JimExprOpIntBin), + OPRINIT("|", 48, 2, JimExprOpIntBin), + + OPRINIT("&&", 10, 2, JimExprOpAnd), + OPRINIT("||", 9, 2, JimExprOpOr), + OPRINIT_ATTR("?", 5, 3, JimExprOpTernary, OP_RIGHT_ASSOC), + OPRINIT_ATTR(":", 5, 3, NULL, OP_RIGHT_ASSOC), + + + OPRINIT_ATTR("**", 120, 2, JimExprOpBin, OP_RIGHT_ASSOC), + + OPRINIT("eq", 60, 2, JimExprOpStrBin), + OPRINIT("ne", 60, 2, JimExprOpStrBin), + + OPRINIT("in", 55, 2, JimExprOpStrBin), + OPRINIT("ni", 55, 2, JimExprOpStrBin), + + OPRINIT("lt", 75, 2, JimExprOpStrBin), + OPRINIT("gt", 75, 2, JimExprOpStrBin), + OPRINIT("le", 75, 2, JimExprOpStrBin), + OPRINIT("ge", 75, 2, JimExprOpStrBin), + + OPRINIT_ATTR("!", 150, 1, JimExprOpNumUnary, OP_RIGHT_ASSOC), + OPRINIT_ATTR("~", 150, 1, JimExprOpIntUnary, OP_RIGHT_ASSOC), + OPRINIT_ATTR(" -", 150, 1, JimExprOpNumUnary, OP_RIGHT_ASSOC), + OPRINIT_ATTR(" +", 150, 1, JimExprOpNumUnary, OP_RIGHT_ASSOC), + + + + OPRINIT_ATTR("int", 200, 1, JimExprOpNumUnary, OP_FUNC), + OPRINIT_ATTR("wide", 200, 1, JimExprOpNumUnary, OP_FUNC), + OPRINIT_ATTR("abs", 200, 1, JimExprOpNumUnary, OP_FUNC), + OPRINIT_ATTR("double", 200, 1, JimExprOpNumUnary, OP_FUNC), + OPRINIT_ATTR("round", 200, 1, JimExprOpNumUnary, OP_FUNC), + OPRINIT_ATTR("rand", 200, 0, JimExprOpNone, OP_FUNC), + OPRINIT_ATTR("srand", 200, 1, JimExprOpIntUnary, OP_FUNC), + +#ifdef JIM_MATH_FUNCTIONS + OPRINIT_ATTR("sin", 200, 1, JimExprOpDoubleUnary, OP_FUNC), + OPRINIT_ATTR("cos", 200, 1, JimExprOpDoubleUnary, OP_FUNC), + OPRINIT_ATTR("tan", 200, 1, JimExprOpDoubleUnary, OP_FUNC), + OPRINIT_ATTR("asin", 200, 1, JimExprOpDoubleUnary, OP_FUNC), + OPRINIT_ATTR("acos", 200, 1, JimExprOpDoubleUnary, OP_FUNC), + OPRINIT_ATTR("atan", 200, 1, JimExprOpDoubleUnary, OP_FUNC), + OPRINIT_ATTR("atan2", 200, 2, JimExprOpBin, OP_FUNC), + OPRINIT_ATTR("sinh", 200, 1, JimExprOpDoubleUnary, OP_FUNC), + OPRINIT_ATTR("cosh", 200, 1, JimExprOpDoubleUnary, OP_FUNC), + OPRINIT_ATTR("tanh", 200, 1, JimExprOpDoubleUnary, OP_FUNC), + OPRINIT_ATTR("ceil", 200, 1, JimExprOpDoubleUnary, OP_FUNC), + OPRINIT_ATTR("floor", 200, 1, JimExprOpDoubleUnary, OP_FUNC), + OPRINIT_ATTR("exp", 200, 1, JimExprOpDoubleUnary, OP_FUNC), + OPRINIT_ATTR("log", 200, 1, JimExprOpDoubleUnary, OP_FUNC), + OPRINIT_ATTR("log10", 200, 1, JimExprOpDoubleUnary, OP_FUNC), + OPRINIT_ATTR("sqrt", 200, 1, JimExprOpDoubleUnary, OP_FUNC), + OPRINIT_ATTR("pow", 200, 2, JimExprOpBin, OP_FUNC), + OPRINIT_ATTR("hypot", 200, 2, JimExprOpBin, OP_FUNC), + OPRINIT_ATTR("fmod", 200, 2, JimExprOpBin, OP_FUNC), +#endif +}; +#undef OPRINIT +#undef OPRINIT_ATTR + +#define JIM_EXPR_OPERATORS_NUM \ + (sizeof(Jim_ExprOperators)/sizeof(struct Jim_ExprOperator)) + +static int JimParseExpression(struct JimParserCtx *pc) +{ + pc->errmsg = NULL; + + while (1) { + + while (isspace(UCHAR(*pc->p)) || (*(pc->p) == '\\' && *(pc->p + 1) == '\n')) { + if (*pc->p == '\n') { + pc->linenr++; + } + pc->p++; + pc->len--; + } + + if (*pc->p == '#') { + JimParseComment(pc); + + continue; + } + break; + } + + + pc->tline = pc->linenr; + pc->tstart = pc->p; + + if (pc->len == 0) { + pc->tend = pc->p; + pc->tt = JIM_TT_EOL; + pc->eof = 1; + return JIM_OK; + } + switch (*(pc->p)) { + case '(': + pc->tt = JIM_TT_SUBEXPR_START; + goto singlechar; + case ')': + pc->tt = JIM_TT_SUBEXPR_END; + goto singlechar; + case ',': + pc->tt = JIM_TT_SUBEXPR_COMMA; +singlechar: + pc->tend = pc->p; + pc->p++; + pc->len--; + break; + case '[': + return JimParseCmd(pc); + case '$': + if (JimParseVar(pc) == JIM_ERR) + return JimParseExprOperator(pc); + else { + + if (pc->tt == JIM_TT_EXPRSUGAR) { + pc->errmsg = "nesting expr in expr is not allowed"; + return JIM_ERR; + } + return JIM_OK; + } + break; + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + case '.': + return JimParseExprNumber(pc); + case '"': + return JimParseQuote(pc); + case '{': + return JimParseBrace(pc); + + case 'N': + case 'I': + case 'n': + case 'i': + if (JimParseExprIrrational(pc) == JIM_ERR) + if (JimParseExprBoolean(pc) == JIM_ERR) + return JimParseExprOperator(pc); + break; + case 't': + case 'f': + case 'o': + case 'y': + if (JimParseExprBoolean(pc) == JIM_ERR) + return JimParseExprOperator(pc); + break; + default: + return JimParseExprOperator(pc); + break; + } + return JIM_OK; +} + +static int JimParseExprNumber(struct JimParserCtx *pc) +{ + char *end; + + + pc->tt = JIM_TT_EXPR_INT; + + jim_strtoull(pc->p, (char **)&pc->p); + + if (strchr("eENnIi.", *pc->p) || pc->p == pc->tstart) { + if (strtod(pc->tstart, &end)) { } + if (end == pc->tstart) + return JIM_ERR; + if (end > pc->p) { + + pc->tt = JIM_TT_EXPR_DOUBLE; + pc->p = end; + } + } + pc->tend = pc->p - 1; + pc->len -= (pc->p - pc->tstart); + return JIM_OK; +} + +static int JimParseExprIrrational(struct JimParserCtx *pc) +{ + const char *irrationals[] = { "NaN", "nan", "NAN", "Inf", "inf", "INF", NULL }; + int i; + + for (i = 0; irrationals[i]; i++) { + const char *irr = irrationals[i]; + + if (strncmp(irr, pc->p, 3) == 0) { + pc->p += 3; + pc->len -= 3; + pc->tend = pc->p - 1; + pc->tt = JIM_TT_EXPR_DOUBLE; + return JIM_OK; + } + } + return JIM_ERR; +} + +static int JimParseExprBoolean(struct JimParserCtx *pc) +{ + int i; + for (i = 0; i < sizeof(jim_true_false_strings) / sizeof(*jim_true_false_strings); i++) { + if (strncmp(pc->p, jim_true_false_strings[i], jim_true_false_lens[i]) == 0) { + pc->p += jim_true_false_lens[i]; + pc->len -= jim_true_false_lens[i]; + pc->tend = pc->p - 1; + pc->tt = JIM_TT_EXPR_BOOLEAN; + return JIM_OK; + } + } + return JIM_ERR; +} + +static const struct Jim_ExprOperator *JimExprOperatorInfoByOpcode(int opcode) +{ + static Jim_ExprOperator dummy_op; + if (opcode < JIM_TT_EXPR_OP) { + return &dummy_op; + } + return &Jim_ExprOperators[opcode - JIM_TT_EXPR_OP]; +} + +static int JimParseExprOperator(struct JimParserCtx *pc) +{ + int i; + const struct Jim_ExprOperator *bestOp = NULL; + int bestLen = 0; + + + for (i = 0; i < (signed)JIM_EXPR_OPERATORS_NUM; i++) { + const struct Jim_ExprOperator *op = &Jim_ExprOperators[i]; + + if (op->name[0] != pc->p[0]) { + continue; + } + + if (op->namelen > bestLen && strncmp(op->name, pc->p, op->namelen) == 0) { + bestOp = op; + bestLen = op->namelen; + } + } + if (bestOp == NULL) { + return JIM_ERR; + } + + + if (bestOp->attr & OP_FUNC) { + const char *p = pc->p + bestLen; + int len = pc->len - bestLen; + + while (len && isspace(UCHAR(*p))) { + len--; + p++; + } + if (*p != '(') { + pc->errmsg = "function requires parentheses"; + return JIM_ERR; + } + } + pc->tend = pc->p + bestLen - 1; + pc->p += bestLen; + pc->len -= bestLen; + + pc->tt = (bestOp - Jim_ExprOperators) + JIM_TT_EXPR_OP; + return JIM_OK; +} + + +static void FreeExprInternalRep(Jim_Interp *interp, Jim_Obj *objPtr); +static void DupExprInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr); +static int SetExprFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr); + +static const Jim_ObjType exprObjType = { + "expression", + FreeExprInternalRep, + DupExprInternalRep, + NULL, + JIM_TYPE_NONE, +}; + + +struct ExprTree +{ + struct JimExprNode *expr; + struct JimExprNode *nodes; + int len; + int inUse; +}; + +static void ExprTreeFreeNodes(Jim_Interp *interp, struct JimExprNode *nodes, int num) +{ + int i; + for (i = 0; i < num; i++) { + if (nodes[i].objPtr) { + Jim_DecrRefCount(interp, nodes[i].objPtr); + } + } + Jim_Free(nodes); +} + +static void ExprTreeFree(Jim_Interp *interp, struct ExprTree *expr) +{ + ExprTreeFreeNodes(interp, expr->nodes, expr->len); + Jim_Free(expr); +} + +static void FreeExprInternalRep(Jim_Interp *interp, Jim_Obj *objPtr) +{ + struct ExprTree *expr = (void *)objPtr->internalRep.ptr; + + if (expr) { + if (--expr->inUse != 0) { + return; + } + + ExprTreeFree(interp, expr); + } +} + +static void DupExprInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr) +{ + JIM_NOTUSED(interp); + JIM_NOTUSED(srcPtr); + + + dupPtr->typePtr = NULL; +} + +struct ExprBuilder { + int parencount; + int level; + ParseToken *token; + ParseToken *first_token; + Jim_Stack stack; + Jim_Obj *exprObjPtr; + Jim_Obj *fileNameObj; + struct JimExprNode *nodes; + struct JimExprNode *next; +}; + +#ifdef DEBUG_SHOW_EXPR +static void JimShowExprNode(struct JimExprNode *node, int level) +{ + int i; + for (i = 0; i < level; i++) { + printf(" "); + } + if (TOKEN_IS_EXPR_OP(node->type)) { + printf("%s\n", jim_tt_name(node->type)); + if (node->left) { + JimShowExprNode(node->left, level + 1); + } + if (node->right) { + JimShowExprNode(node->right, level + 1); + } + if (node->ternary) { + JimShowExprNode(node->ternary, level + 1); + } + } + else { + printf("[%s] %s\n", jim_tt_name(node->type), Jim_String(node->objPtr)); + } +} +#endif + +#define EXPR_UNTIL_CLOSE 0x0001 +#define EXPR_FUNC_ARGS 0x0002 +#define EXPR_TERNARY 0x0004 + +static int ExprTreeBuildTree(Jim_Interp *interp, struct ExprBuilder *builder, int precedence, int flags, int exp_numterms) { + int rc; + struct JimExprNode *node; + + int exp_stacklen = builder->stack.len + exp_numterms; + + if (builder->level++ > 200) { + Jim_SetResultString(interp, "Expression too complex", -1); + return JIM_ERR; + } + + while (builder->token->type != JIM_TT_EOL) { + ParseToken *t = builder->token++; + int prevtt; + + if (t == builder->first_token) { + prevtt = JIM_TT_NONE; + } + else { + prevtt = t[-1].type; + } + + if (t->type == JIM_TT_SUBEXPR_START) { + if (builder->stack.len == exp_stacklen) { + Jim_SetResultFormatted(interp, "unexpected open parenthesis in expression: \"%#s\"", builder->exprObjPtr); + return JIM_ERR; + } + builder->parencount++; + rc = ExprTreeBuildTree(interp, builder, 0, EXPR_UNTIL_CLOSE, 1); + if (rc != JIM_OK) { + return rc; + } + + } + else if (t->type == JIM_TT_SUBEXPR_END) { + if (!(flags & EXPR_UNTIL_CLOSE)) { + if (builder->stack.len == exp_stacklen && builder->level > 1) { + builder->token--; + builder->level--; + return JIM_OK; + } + Jim_SetResultFormatted(interp, "unexpected closing parenthesis in expression: \"%#s\"", builder->exprObjPtr); + return JIM_ERR; + } + builder->parencount--; + if (builder->stack.len == exp_stacklen) { + + break; + } + } + else if (t->type == JIM_TT_SUBEXPR_COMMA) { + if (!(flags & EXPR_FUNC_ARGS)) { + if (builder->stack.len == exp_stacklen) { + + builder->token--; + builder->level--; + return JIM_OK; + } + Jim_SetResultFormatted(interp, "unexpected comma in expression: \"%#s\"", builder->exprObjPtr); + return JIM_ERR; + } + else { + + if (builder->stack.len > exp_stacklen) { + Jim_SetResultFormatted(interp, "too many arguments to math function"); + return JIM_ERR; + } + } + + } + else if (t->type == JIM_EXPROP_COLON) { + if (!(flags & EXPR_TERNARY)) { + if (builder->level != 1) { + + builder->token--; + builder->level--; + return JIM_OK; + } + Jim_SetResultFormatted(interp, ": without ? in expression: \"%#s\"", builder->exprObjPtr); + return JIM_ERR; + } + if (builder->stack.len == exp_stacklen) { + + builder->token--; + builder->level--; + return JIM_OK; + } + + } + else if (TOKEN_IS_EXPR_OP(t->type)) { + const struct Jim_ExprOperator *op; + + + if (TOKEN_IS_EXPR_OP(prevtt) || TOKEN_IS_EXPR_START(prevtt)) { + if (t->type == JIM_EXPROP_SUB) { + t->type = JIM_EXPROP_UNARYMINUS; + } + else if (t->type == JIM_EXPROP_ADD) { + t->type = JIM_EXPROP_UNARYPLUS; + } + } + + op = JimExprOperatorInfoByOpcode(t->type); + + if (op->precedence < precedence || (!(op->attr & OP_RIGHT_ASSOC) && op->precedence == precedence)) { + + builder->token--; + break; + } + + if (op->attr & OP_FUNC) { + if (builder->token->type != JIM_TT_SUBEXPR_START) { + Jim_SetResultString(interp, "missing arguments for math function", -1); + return JIM_ERR; + } + builder->token++; + if (op->arity == 0) { + if (builder->token->type != JIM_TT_SUBEXPR_END) { + Jim_SetResultString(interp, "too many arguments for math function", -1); + return JIM_ERR; + } + builder->token++; + goto noargs; + } + builder->parencount++; + + + rc = ExprTreeBuildTree(interp, builder, 0, EXPR_FUNC_ARGS | EXPR_UNTIL_CLOSE, op->arity); + } + else if (t->type == JIM_EXPROP_TERNARY) { + + rc = ExprTreeBuildTree(interp, builder, op->precedence, EXPR_TERNARY, 2); + } + else { + rc = ExprTreeBuildTree(interp, builder, op->precedence, 0, 1); + } + + if (rc != JIM_OK) { + return rc; + } + +noargs: + node = builder->next++; + node->type = t->type; + + if (op->arity >= 3) { + node->ternary = Jim_StackPop(&builder->stack); + if (node->ternary == NULL) { + goto missingoperand; + } + } + if (op->arity >= 2) { + node->right = Jim_StackPop(&builder->stack); + if (node->right == NULL) { + goto missingoperand; + } + } + if (op->arity >= 1) { + node->left = Jim_StackPop(&builder->stack); + if (node->left == NULL) { +missingoperand: + Jim_SetResultFormatted(interp, "missing operand to %s in expression: \"%#s\"", op->name, builder->exprObjPtr); + builder->next--; + return JIM_ERR; + + } + } + + + Jim_StackPush(&builder->stack, node); + } + else { + Jim_Obj *objPtr = NULL; + + + + + if (!TOKEN_IS_EXPR_START(prevtt) && !TOKEN_IS_EXPR_OP(prevtt)) { + Jim_SetResultFormatted(interp, "missing operator in expression: \"%#s\"", builder->exprObjPtr); + return JIM_ERR; + } + + + if (t->type == JIM_TT_EXPR_INT || t->type == JIM_TT_EXPR_DOUBLE) { + char *endptr; + if (t->type == JIM_TT_EXPR_INT) { + objPtr = Jim_NewIntObj(interp, jim_strtoull(t->token, &endptr)); + } + else { + objPtr = Jim_NewDoubleObj(interp, strtod(t->token, &endptr)); + } + if (endptr != t->token + t->len) { + + Jim_FreeNewObj(interp, objPtr); + objPtr = NULL; + } + } + + if (!objPtr) { + + objPtr = Jim_NewStringObj(interp, t->token, t->len); + if (t->type == JIM_TT_CMD) { + + Jim_SetSourceInfo(interp, objPtr, builder->fileNameObj, t->line); + } + } + + + node = builder->next++; + node->objPtr = objPtr; + Jim_IncrRefCount(node->objPtr); + node->type = t->type; + Jim_StackPush(&builder->stack, node); + } + } + + if (builder->stack.len == exp_stacklen) { + builder->level--; + return JIM_OK; + } + + if ((flags & EXPR_FUNC_ARGS)) { + Jim_SetResultFormatted(interp, "too %s arguments for math function", (builder->stack.len < exp_stacklen) ? "few" : "many"); + } + else { + if (builder->stack.len < exp_stacklen) { + if (builder->level == 0) { + Jim_SetResultFormatted(interp, "empty expression"); + } + else { + Jim_SetResultFormatted(interp, "syntax error in expression \"%#s\": premature end of expression", builder->exprObjPtr); + } + } + else { + Jim_SetResultFormatted(interp, "extra terms after expression"); + } + } + + return JIM_ERR; +} + +static struct ExprTree *ExprTreeCreateTree(Jim_Interp *interp, const ParseTokenList *tokenlist, Jim_Obj *exprObjPtr, Jim_Obj *fileNameObj) +{ + struct ExprTree *expr; + struct ExprBuilder builder; + int rc; + struct JimExprNode *top = NULL; + + builder.parencount = 0; + builder.level = 0; + builder.token = builder.first_token = tokenlist->list; + builder.exprObjPtr = exprObjPtr; + builder.fileNameObj = fileNameObj; + + builder.nodes = Jim_Alloc(sizeof(struct JimExprNode) * (tokenlist->count - 1)); + memset(builder.nodes, 0, sizeof(struct JimExprNode) * (tokenlist->count - 1)); + builder.next = builder.nodes; + Jim_InitStack(&builder.stack); + + rc = ExprTreeBuildTree(interp, &builder, 0, 0, 1); + + if (rc == JIM_OK) { + top = Jim_StackPop(&builder.stack); + + if (builder.parencount) { + Jim_SetResultString(interp, "missing close parenthesis", -1); + rc = JIM_ERR; + } + } + + + Jim_FreeStack(&builder.stack); + + if (rc != JIM_OK) { + ExprTreeFreeNodes(interp, builder.nodes, builder.next - builder.nodes); + return NULL; + } + + expr = Jim_Alloc(sizeof(*expr)); + expr->inUse = 1; + expr->expr = top; + expr->nodes = builder.nodes; + expr->len = builder.next - builder.nodes; + + assert(expr->len <= tokenlist->count - 1); + + return expr; +} + +static int SetExprFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr) +{ + int exprTextLen; + const char *exprText; + struct JimParserCtx parser; + struct ExprTree *expr; + ParseTokenList tokenlist; + int line; + Jim_Obj *fileNameObj; + int rc = JIM_ERR; + + + fileNameObj = Jim_GetSourceInfo(interp, objPtr, &line); + Jim_IncrRefCount(fileNameObj); + + exprText = Jim_GetString(objPtr, &exprTextLen); + + + ScriptTokenListInit(&tokenlist); + + JimParserInit(&parser, exprText, exprTextLen, line); + while (!parser.eof) { + if (JimParseExpression(&parser) != JIM_OK) { + ScriptTokenListFree(&tokenlist); + Jim_SetResultFormatted(interp, "syntax error in expression: \"%#s\"", objPtr); + if (parser.errmsg) { + Jim_AppendStrings(interp, Jim_GetResult(interp), ": ", parser.errmsg, NULL); + } + expr = NULL; + goto err; + } + + ScriptAddToken(&tokenlist, parser.tstart, parser.tend - parser.tstart + 1, parser.tt, + parser.tline); + } + +#ifdef DEBUG_SHOW_EXPR_TOKENS + { + int i; + printf("==== Expr Tokens (%s) ====\n", Jim_String(fileNameObj)); + for (i = 0; i < tokenlist.count; i++) { + printf("[%2d]@%d %s '%.*s'\n", i, tokenlist.list[i].line, jim_tt_name(tokenlist.list[i].type), + tokenlist.list[i].len, tokenlist.list[i].token); + } + } +#endif + + if (tokenlist.count <= 1) { + Jim_SetResultString(interp, "empty expression", -1); + rc = JIM_ERR; + } + else { + rc = JimParseCheckMissing(interp, parser.missing.ch); + } + if (rc != JIM_OK) { + ScriptTokenListFree(&tokenlist); + Jim_DecrRefCount(interp, fileNameObj); + return rc; + } + + + expr = ExprTreeCreateTree(interp, &tokenlist, objPtr, fileNameObj); + + + ScriptTokenListFree(&tokenlist); + + if (!expr) { + goto err; + } + +#ifdef DEBUG_SHOW_EXPR + printf("==== Expr ====\n"); + JimShowExprNode(expr->expr, 0); +#endif + + rc = JIM_OK; + + err: + + Jim_DecrRefCount(interp, fileNameObj); + Jim_FreeIntRep(interp, objPtr); + Jim_SetIntRepPtr(objPtr, expr); + objPtr->typePtr = &exprObjType; + return rc; +} + +static struct ExprTree *JimGetExpression(Jim_Interp *interp, Jim_Obj *objPtr) +{ + if (objPtr->typePtr != &exprObjType) { + if (SetExprFromAny(interp, objPtr) != JIM_OK) { + return NULL; + } + } + return (struct ExprTree *) Jim_GetIntRepPtr(objPtr); +} + +#ifdef JIM_OPTIMIZATION +static Jim_Obj *JimExprIntValOrVar(Jim_Interp *interp, struct JimExprNode *node) +{ + if (node->type == JIM_TT_EXPR_INT) + return node->objPtr; + else if (node->type == JIM_TT_VAR) + return Jim_GetVariable(interp, node->objPtr, JIM_NONE); + else if (node->type == JIM_TT_DICTSUGAR) + return JimExpandDictSugar(interp, node->objPtr); + else + return NULL; +} +#endif + + +static int JimExprEvalTermNode(Jim_Interp *interp, struct JimExprNode *node) +{ + if (TOKEN_IS_EXPR_OP(node->type)) { + const struct Jim_ExprOperator *op = JimExprOperatorInfoByOpcode(node->type); + return op->funcop(interp, node); + } + else { + Jim_Obj *objPtr; + + + switch (node->type) { + case JIM_TT_EXPR_INT: + case JIM_TT_EXPR_DOUBLE: + case JIM_TT_EXPR_BOOLEAN: + case JIM_TT_STR: + Jim_SetResult(interp, node->objPtr); + return JIM_OK; + + case JIM_TT_VAR: + objPtr = Jim_GetVariable(interp, node->objPtr, JIM_ERRMSG); + if (objPtr) { + Jim_SetResult(interp, objPtr); + return JIM_OK; + } + return JIM_ERR; + + case JIM_TT_DICTSUGAR: + objPtr = JimExpandDictSugar(interp, node->objPtr); + if (objPtr) { + Jim_SetResult(interp, objPtr); + return JIM_OK; + } + return JIM_ERR; + + case JIM_TT_ESC: + if (interp->safeexpr) { + return JIM_ERR; + } + if (Jim_SubstObj(interp, node->objPtr, &objPtr, JIM_NONE) == JIM_OK) { + Jim_SetResult(interp, objPtr); + return JIM_OK; + } + return JIM_ERR; + + case JIM_TT_CMD: + if (interp->safeexpr) { + return JIM_ERR; + } + return Jim_EvalObj(interp, node->objPtr); + + default: + + return JIM_ERR; + } + } +} + +static int JimExprGetTerm(Jim_Interp *interp, struct JimExprNode *node, Jim_Obj **objPtrPtr) +{ + int rc = JimExprEvalTermNode(interp, node); + if (rc == JIM_OK) { + *objPtrPtr = Jim_GetResult(interp); + Jim_IncrRefCount(*objPtrPtr); + } + return rc; +} + +static int JimExprGetTermBoolean(Jim_Interp *interp, struct JimExprNode *node) +{ + if (JimExprEvalTermNode(interp, node) == JIM_OK) { + return ExprBool(interp, Jim_GetResult(interp)); + } + return -1; +} + +int Jim_EvalExpression(Jim_Interp *interp, Jim_Obj *exprObjPtr) +{ + struct ExprTree *expr; + int retcode = JIM_OK; + + Jim_IncrRefCount(exprObjPtr); + expr = JimGetExpression(interp, exprObjPtr); + if (!expr) { + retcode = JIM_ERR; + goto done; + } + +#ifdef JIM_OPTIMIZATION + if (!interp->safeexpr) { + Jim_Obj *objPtr; + + + switch (expr->len) { + case 1: + objPtr = JimExprIntValOrVar(interp, expr->expr); + if (objPtr) { + Jim_SetResult(interp, objPtr); + goto done; + } + break; + + case 2: + if (expr->expr->type == JIM_EXPROP_NOT) { + objPtr = JimExprIntValOrVar(interp, expr->expr->left); + + if (objPtr && JimIsWide(objPtr)) { + Jim_SetResult(interp, JimWideValue(objPtr) ? interp->falseObj : interp->trueObj); + goto done; + } + } + break; + + case 3: + objPtr = JimExprIntValOrVar(interp, expr->expr->left); + if (objPtr && JimIsWide(objPtr)) { + Jim_Obj *objPtr2 = JimExprIntValOrVar(interp, expr->expr->right); + if (objPtr2 && JimIsWide(objPtr2)) { + jim_wide wideValueA = JimWideValue(objPtr); + jim_wide wideValueB = JimWideValue(objPtr2); + int cmpRes; + switch (expr->expr->type) { + case JIM_EXPROP_LT: + cmpRes = wideValueA < wideValueB; + break; + case JIM_EXPROP_LTE: + cmpRes = wideValueA <= wideValueB; + break; + case JIM_EXPROP_GT: + cmpRes = wideValueA > wideValueB; + break; + case JIM_EXPROP_GTE: + cmpRes = wideValueA >= wideValueB; + break; + case JIM_EXPROP_NUMEQ: + cmpRes = wideValueA == wideValueB; + break; + case JIM_EXPROP_NUMNE: + cmpRes = wideValueA != wideValueB; + break; + default: + goto noopt; + } + Jim_SetResult(interp, cmpRes ? interp->trueObj : interp->falseObj); + goto done; + } + } + break; + } + } +noopt: +#endif + + expr->inUse++; + + + retcode = JimExprEvalTermNode(interp, expr->expr); + + + Jim_FreeIntRep(interp, exprObjPtr); + exprObjPtr->typePtr = &exprObjType; + Jim_SetIntRepPtr(exprObjPtr, expr); + +done: + Jim_DecrRefCount(interp, exprObjPtr); + + return retcode; +} + +int Jim_GetBoolFromExpr(Jim_Interp *interp, Jim_Obj *exprObjPtr, int *boolPtr) +{ + int retcode = Jim_EvalExpression(interp, exprObjPtr); + + if (retcode == JIM_OK) { + switch (ExprBool(interp, Jim_GetResult(interp))) { + case 0: + *boolPtr = 0; + break; + + case 1: + *boolPtr = 1; + break; + + case -1: + retcode = JIM_ERR; + break; + } + } + return retcode; +} + + + + +typedef struct ScanFmtPartDescr +{ + const char *arg; + const char *prefix; + size_t width; + int pos; + char type; + char modifier; +} ScanFmtPartDescr; + + +typedef struct ScanFmtStringObj +{ + jim_wide size; + char *stringRep; + size_t count; + size_t convCount; + size_t maxPos; + const char *error; + char *scratch; + ScanFmtPartDescr descr[1]; +} ScanFmtStringObj; + + +static void FreeScanFmtInternalRep(Jim_Interp *interp, Jim_Obj *objPtr); +static void DupScanFmtInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr); +static void UpdateStringOfScanFmt(Jim_Obj *objPtr); + +static const Jim_ObjType scanFmtStringObjType = { + "scanformatstring", + FreeScanFmtInternalRep, + DupScanFmtInternalRep, + UpdateStringOfScanFmt, + JIM_TYPE_NONE, +}; + +void FreeScanFmtInternalRep(Jim_Interp *interp, Jim_Obj *objPtr) +{ + JIM_NOTUSED(interp); + Jim_Free((char *)objPtr->internalRep.ptr); + objPtr->internalRep.ptr = 0; +} + +void DupScanFmtInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr) +{ + size_t size = (size_t) ((ScanFmtStringObj *) srcPtr->internalRep.ptr)->size; + ScanFmtStringObj *newVec = (ScanFmtStringObj *) Jim_Alloc(size); + + JIM_NOTUSED(interp); + memcpy(newVec, srcPtr->internalRep.ptr, size); + dupPtr->internalRep.ptr = newVec; + dupPtr->typePtr = &scanFmtStringObjType; +} + +static void UpdateStringOfScanFmt(Jim_Obj *objPtr) +{ + JimSetStringBytes(objPtr, ((ScanFmtStringObj *) objPtr->internalRep.ptr)->stringRep); +} + + +static int SetScanFmtFromAny(Jim_Interp *interp, Jim_Obj *objPtr) +{ + ScanFmtStringObj *fmtObj; + char *buffer; + int maxCount, i, approxSize, lastPos = -1; + const char *fmt = Jim_String(objPtr); + int maxFmtLen = Jim_Length(objPtr); + const char *fmtEnd = fmt + maxFmtLen; + int curr; + + Jim_FreeIntRep(interp, objPtr); + + for (i = 0, maxCount = 0; i < maxFmtLen; ++i) + if (fmt[i] == '%') + ++maxCount; + + approxSize = sizeof(ScanFmtStringObj) + +(maxCount + 1) * sizeof(ScanFmtPartDescr) + +maxFmtLen * sizeof(char) + 3 + 1 + + maxFmtLen * sizeof(char) + 1 + + maxFmtLen * sizeof(char) + +(maxCount + 1) * sizeof(char) + +1; + fmtObj = (ScanFmtStringObj *) Jim_Alloc(approxSize); + memset(fmtObj, 0, approxSize); + fmtObj->size = approxSize; + fmtObj->maxPos = 0; + fmtObj->scratch = (char *)&fmtObj->descr[maxCount + 1]; + fmtObj->stringRep = fmtObj->scratch + maxFmtLen + 3 + 1; + memcpy(fmtObj->stringRep, fmt, maxFmtLen); + buffer = fmtObj->stringRep + maxFmtLen + 1; + objPtr->internalRep.ptr = fmtObj; + objPtr->typePtr = &scanFmtStringObjType; + for (i = 0, curr = 0; fmt < fmtEnd; ++fmt) { + int width = 0, skip; + ScanFmtPartDescr *descr = &fmtObj->descr[curr]; + + fmtObj->count++; + descr->width = 0; + + if (*fmt != '%' || fmt[1] == '%') { + descr->type = 0; + descr->prefix = &buffer[i]; + for (; fmt < fmtEnd; ++fmt) { + if (*fmt == '%') { + if (fmt[1] != '%') + break; + ++fmt; + } + buffer[i++] = *fmt; + } + buffer[i++] = 0; + } + + ++fmt; + + if (fmt >= fmtEnd) + goto done; + descr->pos = 0; + if (*fmt == '*') { + descr->pos = -1; + ++fmt; + } + else + fmtObj->convCount++; + + if (sscanf(fmt, "%d%n", &width, &skip) == 1) { + fmt += skip; + + if (descr->pos != -1 && *fmt == '$') { + int prev; + + ++fmt; + descr->pos = width; + width = 0; + + if ((lastPos == 0 && descr->pos > 0) + || (lastPos > 0 && descr->pos == 0)) { + fmtObj->error = "cannot mix \"%\" and \"%n$\" conversion specifiers"; + return JIM_ERR; + } + + for (prev = 0; prev < curr; ++prev) { + if (fmtObj->descr[prev].pos == -1) + continue; + if (fmtObj->descr[prev].pos == descr->pos) { + fmtObj->error = + "variable is assigned by multiple \"%n$\" conversion specifiers"; + return JIM_ERR; + } + } + if (descr->pos < 0) { + fmtObj->error = + "\"%n$\" conversion specifier is negative"; + return JIM_ERR; + } + + if (sscanf(fmt, "%d%n", &width, &skip) == 1) { + descr->width = width; + fmt += skip; + } + if (descr->pos > 0 && (size_t) descr->pos > fmtObj->maxPos) + fmtObj->maxPos = descr->pos; + } + else { + + descr->width = width; + } + } + + if (lastPos == -1) + lastPos = descr->pos; + + if (*fmt == '[') { + int swapped = 1, beg = i, end, j; + + descr->type = '['; + descr->arg = &buffer[i]; + ++fmt; + if (*fmt == '^') + buffer[i++] = *fmt++; + if (*fmt == ']') + buffer[i++] = *fmt++; + while (*fmt && *fmt != ']') + buffer[i++] = *fmt++; + if (*fmt != ']') { + fmtObj->error = "unmatched [ in format string"; + return JIM_ERR; + } + end = i; + buffer[i++] = 0; + + while (swapped) { + swapped = 0; + for (j = beg + 1; j < end - 1; ++j) { + if (buffer[j] == '-' && buffer[j - 1] > buffer[j + 1]) { + char tmp = buffer[j - 1]; + + buffer[j - 1] = buffer[j + 1]; + buffer[j + 1] = tmp; + swapped = 1; + } + } + } + } + else { + + if (fmt < fmtEnd && strchr("hlL", *fmt)) + descr->modifier = tolower((int)*fmt++); + + if (fmt >= fmtEnd) { + fmtObj->error = "missing scan conversion character"; + return JIM_ERR; + } + + descr->type = *fmt; + if (strchr("efgcsndoxui", *fmt) == 0) { + fmtObj->error = "bad scan conversion character"; + return JIM_ERR; + } + else if (*fmt == 'c' && descr->width != 0) { + fmtObj->error = "field width may not be specified in %c " "conversion"; + return JIM_ERR; + } + else if (*fmt == 'u' && descr->modifier == 'l') { + fmtObj->error = "unsigned wide not supported"; + return JIM_ERR; + } + } + curr++; + } + done: + return JIM_OK; +} + + + +#define FormatGetCnvCount(_fo_) \ + ((ScanFmtStringObj*)((_fo_)->internalRep.ptr))->convCount +#define FormatGetMaxPos(_fo_) \ + ((ScanFmtStringObj*)((_fo_)->internalRep.ptr))->maxPos +#define FormatGetError(_fo_) \ + ((ScanFmtStringObj*)((_fo_)->internalRep.ptr))->error + +static Jim_Obj *JimScanAString(Jim_Interp *interp, const char *sdescr, const char *str) +{ + char *buffer = Jim_StrDup(str); + char *p = buffer; + + while (*str) { + int c; + int n; + + if (!sdescr && isspace(UCHAR(*str))) + break; + + n = utf8_tounicode(str, &c); + if (sdescr && !JimCharsetMatch(sdescr, strlen(sdescr), c, JIM_CHARSET_SCAN)) + break; + while (n--) + *p++ = *str++; + } + *p = 0; + return Jim_NewStringObjNoAlloc(interp, buffer, p - buffer); +} + + +static int ScanOneEntry(Jim_Interp *interp, const char *str, int pos, int str_bytelen, + ScanFmtStringObj * fmtObj, long idx, Jim_Obj **valObjPtr) +{ + const char *tok; + const ScanFmtPartDescr *descr = &fmtObj->descr[idx]; + size_t scanned = 0; + size_t anchor = pos; + int i; + Jim_Obj *tmpObj = NULL; + + + *valObjPtr = 0; + if (descr->prefix) { + for (i = 0; pos < str_bytelen && descr->prefix[i]; ++i) { + + if (isspace(UCHAR(descr->prefix[i]))) + while (pos < str_bytelen && isspace(UCHAR(str[pos]))) + ++pos; + else if (descr->prefix[i] != str[pos]) + break; + else + ++pos; + } + if (pos >= str_bytelen) { + return -1; + } + else if (descr->prefix[i] != 0) + return 0; + } + + if (descr->type != 'c' && descr->type != '[' && descr->type != 'n') + while (isspace(UCHAR(str[pos]))) + ++pos; + + + scanned = pos - anchor; + + + if (descr->type == 'n') { + + *valObjPtr = Jim_NewIntObj(interp, anchor + scanned); + } + else if (pos >= str_bytelen) { + + return -1; + } + else if (descr->type == 'c') { + int c; + scanned += utf8_tounicode(&str[pos], &c); + *valObjPtr = Jim_NewIntObj(interp, c); + return scanned; + } + else { + + if (descr->width > 0) { + size_t sLen = utf8_strlen(&str[pos], str_bytelen - pos); + size_t tLen = descr->width > sLen ? sLen : descr->width; + + tmpObj = Jim_NewStringObjUtf8(interp, str + pos, tLen); + tok = tmpObj->bytes; + } + else { + + tok = &str[pos]; + } + switch (descr->type) { + case 'd': + case 'o': + case 'x': + case 'u': + case 'i':{ + char *endp; + jim_wide w; + + int base = descr->type == 'o' ? 8 + : descr->type == 'x' ? 16 : descr->type == 'i' ? 0 : 10; + + + if (base == 0) { + w = jim_strtoull(tok, &endp); + } + else { + w = strtoull(tok, &endp, base); + } + + if (endp != tok) { + + *valObjPtr = Jim_NewIntObj(interp, w); + + + scanned += endp - tok; + } + else { + scanned = *tok ? 0 : -1; + } + break; + } + case 's': + case '[':{ + *valObjPtr = JimScanAString(interp, descr->arg, tok); + scanned += Jim_Length(*valObjPtr); + break; + } + case 'e': + case 'f': + case 'g':{ + char *endp; + double value = strtod(tok, &endp); + + if (endp != tok) { + + *valObjPtr = Jim_NewDoubleObj(interp, value); + + scanned += endp - tok; + } + else { + scanned = *tok ? 0 : -1; + } + break; + } + } + if (tmpObj) { + Jim_FreeNewObj(interp, tmpObj); + } + } + return scanned; +} + + +Jim_Obj *Jim_ScanString(Jim_Interp *interp, Jim_Obj *strObjPtr, Jim_Obj *fmtObjPtr, int flags) +{ + size_t i, pos; + int scanned = 1; + const char *str = Jim_String(strObjPtr); + int str_bytelen = Jim_Length(strObjPtr); + Jim_Obj *resultList = 0; + Jim_Obj **resultVec = 0; + int resultc; + Jim_Obj *emptyStr = 0; + ScanFmtStringObj *fmtObj; + + + JimPanic((fmtObjPtr->typePtr != &scanFmtStringObjType, "Jim_ScanString() for non-scan format")); + + fmtObj = (ScanFmtStringObj *) fmtObjPtr->internalRep.ptr; + + if (fmtObj->error != 0) { + if (flags & JIM_ERRMSG) + Jim_SetResultString(interp, fmtObj->error, -1); + return 0; + } + + emptyStr = Jim_NewEmptyStringObj(interp); + Jim_IncrRefCount(emptyStr); + + resultList = Jim_NewListObj(interp, NULL, 0); + if (fmtObj->maxPos > 0) { + for (i = 0; i < fmtObj->maxPos; ++i) + Jim_ListAppendElement(interp, resultList, emptyStr); + JimListGetElements(interp, resultList, &resultc, &resultVec); + } + + for (i = 0, pos = 0; i < fmtObj->count; ++i) { + ScanFmtPartDescr *descr = &(fmtObj->descr[i]); + Jim_Obj *value = 0; + + + if (descr->type == 0) + continue; + + if (scanned > 0) + scanned = ScanOneEntry(interp, str, pos, str_bytelen, fmtObj, i, &value); + + if (scanned == -1 && i == 0) + goto eof; + + pos += scanned; + + + if (value == 0) + value = Jim_NewEmptyStringObj(interp); + + if (descr->pos == -1) { + Jim_FreeNewObj(interp, value); + } + else if (descr->pos == 0) + + Jim_ListAppendElement(interp, resultList, value); + else if (resultVec[descr->pos - 1] == emptyStr) { + + Jim_DecrRefCount(interp, resultVec[descr->pos - 1]); + Jim_IncrRefCount(value); + resultVec[descr->pos - 1] = value; + } + else { + + Jim_FreeNewObj(interp, value); + goto err; + } + } + Jim_DecrRefCount(interp, emptyStr); + return resultList; + eof: + Jim_DecrRefCount(interp, emptyStr); + Jim_FreeNewObj(interp, resultList); + return (Jim_Obj *)EOF; + err: + Jim_DecrRefCount(interp, emptyStr); + Jim_FreeNewObj(interp, resultList); + return 0; +} + + +static void JimPrngInit(Jim_Interp *interp) +{ +#define PRNG_SEED_SIZE 256 + int i; + unsigned int *seed; + time_t t = time(NULL); + + interp->prngState = Jim_Alloc(sizeof(Jim_PrngState)); + + seed = Jim_Alloc(PRNG_SEED_SIZE * sizeof(*seed)); + for (i = 0; i < PRNG_SEED_SIZE; i++) { + seed[i] = (rand() ^ t ^ clock()); + } + JimPrngSeed(interp, (unsigned char *)seed, PRNG_SEED_SIZE * sizeof(*seed)); + Jim_Free(seed); +} + + +static void JimRandomBytes(Jim_Interp *interp, void *dest, unsigned int len) +{ + Jim_PrngState *prng; + unsigned char *destByte = (unsigned char *)dest; + unsigned int si, sj, x; + + + if (interp->prngState == NULL) + JimPrngInit(interp); + prng = interp->prngState; + + for (x = 0; x < len; x++) { + prng->i = (prng->i + 1) & 0xff; + si = prng->sbox[prng->i]; + prng->j = (prng->j + si) & 0xff; + sj = prng->sbox[prng->j]; + prng->sbox[prng->i] = sj; + prng->sbox[prng->j] = si; + *destByte++ = prng->sbox[(si + sj) & 0xff]; + } +} + + +static void JimPrngSeed(Jim_Interp *interp, unsigned char *seed, int seedLen) +{ + int i; + Jim_PrngState *prng; + + + if (interp->prngState == NULL) + JimPrngInit(interp); + prng = interp->prngState; + + + for (i = 0; i < 256; i++) + prng->sbox[i] = i; + + for (i = 0; i < seedLen; i++) { + unsigned char t; + + t = prng->sbox[i & 0xFF]; + prng->sbox[i & 0xFF] = prng->sbox[seed[i]]; + prng->sbox[seed[i]] = t; + } + prng->i = prng->j = 0; + + for (i = 0; i < 256; i += seedLen) { + JimRandomBytes(interp, seed, seedLen); + } +} + + +static int Jim_IncrCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + jim_wide wideValue, increment = 1; + Jim_Obj *intObjPtr; + + if (argc != 2 && argc != 3) { + Jim_WrongNumArgs(interp, 1, argv, "varName ?increment?"); + return JIM_ERR; + } + if (argc == 3) { + if (Jim_GetWideExpr(interp, argv[2], &increment) != JIM_OK) + return JIM_ERR; + } + intObjPtr = Jim_GetVariable(interp, argv[1], JIM_UNSHARED); + if (!intObjPtr) { + + wideValue = 0; + } + else if (Jim_GetWide(interp, intObjPtr, &wideValue) != JIM_OK) { + return JIM_ERR; + } + if (!intObjPtr || Jim_IsShared(intObjPtr)) { + intObjPtr = Jim_NewIntObj(interp, wideValue + increment); + if (Jim_SetVariable(interp, argv[1], intObjPtr) != JIM_OK) { + Jim_FreeNewObj(interp, intObjPtr); + return JIM_ERR; + } + } + else { + + Jim_InvalidateStringRep(intObjPtr); + JimWideValue(intObjPtr) = wideValue + increment; + + if (argv[1]->typePtr != &variableObjType) { + + Jim_SetVariable(interp, argv[1], intObjPtr); + } + } + Jim_SetResult(interp, intObjPtr); + return JIM_OK; +} + + +#define JIM_EVAL_SARGV_LEN 8 +#define JIM_EVAL_SINTV_LEN 8 + +static int JimTraceCallback(Jim_Interp *interp, const char *type, int argc, Jim_Obj *const *argv) +{ + JimPanic((interp->traceCmdObj == NULL, "xtrace invoked with no object")); + + int ret; + Jim_Obj *nargv[7]; + Jim_Obj *traceCmdObj = interp->traceCmdObj; + Jim_Obj *resultObj = Jim_GetResult(interp); + ScriptObj *script = NULL; + + + + if (interp->evalFrame->scriptObj) { + script = JimGetScript(interp, interp->evalFrame->scriptObj); + } + + nargv[0] = traceCmdObj; + nargv[1] = Jim_NewStringObj(interp, type, -1); + nargv[2] = script ? script->fileNameObj : interp->emptyObj; + nargv[3] = Jim_NewIntObj(interp, script ? script->linenr : 1); + nargv[4] = resultObj; + nargv[5] = argv[0]; + nargv[6] = Jim_NewListObj(interp, argv + 1, argc - 1); + + + interp->traceCmdObj = NULL; + + Jim_IncrRefCount(resultObj); + ret = Jim_EvalObjVector(interp, 7, nargv); + Jim_DecrRefCount(interp, resultObj); + + if (ret == JIM_OK || ret == JIM_RETURN) { + + interp->traceCmdObj = traceCmdObj; + Jim_SetEmptyResult(interp); + ret = JIM_OK; + } + else { + + Jim_DecrRefCount(interp, traceCmdObj); + } + return ret; +} + + +static int JimUnknown(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + int retcode; + + if (interp->unknown_called > 50) { + return JIM_ERR; + } + + + + if (Jim_GetCommand(interp, interp->unknown, JIM_NONE) == NULL) + return JIM_ERR; + + interp->unknown_called++; + + retcode = Jim_EvalObjPrefix(interp, interp->unknown, argc, argv); + interp->unknown_called--; + + return retcode; +} + +static void JimPushEvalFrame(Jim_Interp *interp, Jim_EvalFrame *frame, Jim_Obj *scriptObj) +{ + memset(frame, 0, sizeof(*frame)); + frame->parent = interp->evalFrame; + frame->level = frame->parent->level + 1; + frame->procLevel = interp->procLevel; + frame->framePtr = interp->framePtr; + if (scriptObj) { + frame->scriptObj = scriptObj; + } + else { + frame->scriptObj = frame->parent->scriptObj; + } + interp->evalFrame = frame; +#if 0 + if (frame->scriptObj) { + printf("script: %.*s\n", 20, Jim_String(frame->scriptObj)); + } +#endif +} + +static void JimPopEvalFrame(Jim_Interp *interp) +{ + interp->evalFrame = interp->evalFrame->parent; +} + + +static int JimInvokeCommand(Jim_Interp *interp, int objc, Jim_Obj *const *objv) +{ + int retcode; + Jim_Cmd *cmdPtr; + void *prevPrivData; + Jim_Obj *tailcallObj = NULL; + +#if 0 + printf("invoke"); + int j; + for (j = 0; j < objc; j++) { + printf(" '%s'", Jim_String(objv[j])); + } + printf("\n"); +#endif + + cmdPtr = Jim_GetCommand(interp, objv[0], JIM_ERRMSG); + if (cmdPtr == NULL) { + return JimUnknown(interp, objc, objv); + } + JimIncrCmdRefCount(cmdPtr); + + if (interp->evalDepth == interp->maxEvalDepth) { + Jim_SetResultString(interp, "Infinite eval recursion", -1); + retcode = JIM_ERR; + goto out; + } + interp->evalDepth++; + prevPrivData = interp->cmdPrivData; + +tailcall: + + interp->evalFrame->argc = objc; + interp->evalFrame->argv = objv; + interp->evalFrame->cmd = cmdPtr; + + if (!interp->traceCmdObj || + (retcode = JimTraceCallback(interp, "cmd", objc, objv)) == JIM_OK) { + + Jim_SetEmptyResult(interp); + if (cmdPtr->isproc) { + retcode = JimCallProcedure(interp, cmdPtr, objc, objv); + } + else { + interp->cmdPrivData = cmdPtr->u.native.privData; + retcode = cmdPtr->u.native.cmdProc(interp, objc, objv); + } + if (retcode == JIM_ERR) { + JimSetErrorStack(interp, NULL); + } + } + + if (tailcallObj) { + + Jim_DecrRefCount(interp, tailcallObj); + tailcallObj = NULL; + } + + + interp->evalFrame->argc = 0; + interp->evalFrame->argv = NULL; + + + if (retcode == JIM_EVAL && interp->framePtr->tailcallObj) { + JimDecrCmdRefCount(interp, cmdPtr); + + + cmdPtr = interp->framePtr->tailcallCmd; + interp->framePtr->tailcallCmd = NULL; + tailcallObj = interp->framePtr->tailcallObj; + interp->framePtr->tailcallObj = NULL; + objc = tailcallObj->internalRep.listValue.len; + objv = tailcallObj->internalRep.listValue.ele; + goto tailcall; + } + + interp->cmdPrivData = prevPrivData; + interp->evalDepth--; + +out: + JimDecrCmdRefCount(interp, cmdPtr); + + if (retcode == JIM_ERR) { + JimSetErrorStack(interp, NULL); + } + + if (interp->framePtr->tailcallObj) { + JimDecrCmdRefCount(interp, interp->framePtr->tailcallCmd); + Jim_DecrRefCount(interp, interp->framePtr->tailcallObj); + interp->framePtr->tailcallCmd = NULL; + interp->framePtr->tailcallObj = NULL; + } + + return retcode; +} + +int Jim_EvalObjVector(Jim_Interp *interp, int objc, Jim_Obj *const *objv) +{ + int i, retcode; + Jim_EvalFrame frame; + + + for (i = 0; i < objc; i++) + Jim_IncrRefCount(objv[i]); + + + JimPushEvalFrame(interp, &frame, NULL); + + retcode = JimInvokeCommand(interp, objc, objv); + + JimPopEvalFrame(interp); + + + for (i = 0; i < objc; i++) + Jim_DecrRefCount(interp, objv[i]); + + return retcode; +} + +int Jim_EvalObjPrefix(Jim_Interp *interp, Jim_Obj *prefix, int objc, Jim_Obj *const *objv) +{ + int ret; + Jim_Obj **nargv = Jim_Alloc((objc + 1) * sizeof(*nargv)); + + nargv[0] = prefix; + memcpy(&nargv[1], &objv[0], sizeof(nargv[0]) * objc); + ret = Jim_EvalObjVector(interp, objc + 1, nargv); + Jim_Free(nargv); + return ret; +} + +static int JimSubstOneToken(Jim_Interp *interp, const ScriptToken *token, Jim_Obj **objPtrPtr) +{ + Jim_Obj *objPtr; + int ret = JIM_ERR; + + switch (token->type) { + case JIM_TT_STR: + case JIM_TT_ESC: + objPtr = token->objPtr; + break; + case JIM_TT_VAR: + objPtr = Jim_GetVariable(interp, token->objPtr, JIM_ERRMSG); + break; + case JIM_TT_DICTSUGAR: + objPtr = JimExpandDictSugar(interp, token->objPtr); + break; + case JIM_TT_EXPRSUGAR: + ret = Jim_EvalExpression(interp, token->objPtr); + if (ret == JIM_OK) { + objPtr = Jim_GetResult(interp); + } + else { + objPtr = NULL; + } + break; + case JIM_TT_CMD: + ret = Jim_EvalObj(interp, token->objPtr); + if (ret == JIM_OK || ret == JIM_RETURN) { + objPtr = interp->result; + } else { + + objPtr = NULL; + } + break; + default: + JimPanic((1, + "default token type (%d) reached " "in Jim_SubstObj().", token->type)); + objPtr = NULL; + break; + } + if (objPtr) { + *objPtrPtr = objPtr; + return JIM_OK; + } + return ret; +} + +static Jim_Obj *JimInterpolateTokens(Jim_Interp *interp, const ScriptToken * token, int tokens, int flags) +{ + int totlen = 0, i; + Jim_Obj **intv; + Jim_Obj *sintv[JIM_EVAL_SINTV_LEN]; + Jim_Obj *objPtr; + char *s; + + if (tokens <= JIM_EVAL_SINTV_LEN) + intv = sintv; + else + intv = Jim_Alloc(sizeof(Jim_Obj *) * tokens); + + for (i = 0; i < tokens; i++) { + switch (JimSubstOneToken(interp, &token[i], &intv[i])) { + case JIM_OK: + case JIM_RETURN: + break; + case JIM_BREAK: + if (flags & JIM_SUBST_FLAG) { + + tokens = i; + continue; + } + + + case JIM_CONTINUE: + if (flags & JIM_SUBST_FLAG) { + intv[i] = NULL; + continue; + } + + + default: + while (i--) { + Jim_DecrRefCount(interp, intv[i]); + } + if (intv != sintv) { + Jim_Free(intv); + } + return NULL; + } + Jim_IncrRefCount(intv[i]); + Jim_String(intv[i]); + totlen += intv[i]->length; + } + + + if (tokens == 1 && intv[0] && intv == sintv) { + + intv[0]->refCount--; + return intv[0]; + } + + objPtr = Jim_NewStringObjNoAlloc(interp, NULL, 0); + + if (tokens == 4 && token[0].type == JIM_TT_ESC && token[1].type == JIM_TT_ESC + && token[2].type == JIM_TT_VAR) { + + objPtr->typePtr = &interpolatedObjType; + objPtr->internalRep.dictSubstValue.varNameObjPtr = token[0].objPtr; + objPtr->internalRep.dictSubstValue.indexObjPtr = intv[2]; + Jim_IncrRefCount(intv[2]); + } + else if (tokens && intv[0] && intv[0]->typePtr == &sourceObjType) { + + int line; + Jim_Obj *fileNameObj = Jim_GetSourceInfo(interp, intv[0], &line); + Jim_SetSourceInfo(interp, objPtr, fileNameObj, line); + } + + + s = objPtr->bytes = Jim_Alloc(totlen + 1); + objPtr->length = totlen; + for (i = 0; i < tokens; i++) { + if (intv[i]) { + memcpy(s, intv[i]->bytes, intv[i]->length); + s += intv[i]->length; + Jim_DecrRefCount(interp, intv[i]); + } + } + objPtr->bytes[totlen] = '\0'; + + if (intv != sintv) { + Jim_Free(intv); + } + + return objPtr; +} + + +static int JimEvalObjList(Jim_Interp *interp, Jim_Obj *listPtr) +{ + int retcode = JIM_OK; + Jim_EvalFrame frame; + + JimPanic((Jim_IsList(listPtr) == 0, "JimEvalObjList() invoked on non-list.")); + + JimPushEvalFrame(interp, &frame, NULL); + + if (listPtr->internalRep.listValue.len) { + Jim_IncrRefCount(listPtr); + retcode = JimInvokeCommand(interp, + listPtr->internalRep.listValue.len, + listPtr->internalRep.listValue.ele); + Jim_DecrRefCount(interp, listPtr); + } + + JimPopEvalFrame(interp); + + return retcode; +} + +int Jim_EvalObjList(Jim_Interp *interp, Jim_Obj *listPtr) +{ + SetListFromAny(interp, listPtr); + return JimEvalObjList(interp, listPtr); +} + +int Jim_EvalObj(Jim_Interp *interp, Jim_Obj *scriptObjPtr) +{ + int i; + ScriptObj *script; + ScriptToken *token; + int retcode = JIM_OK; + Jim_Obj *sargv[JIM_EVAL_SARGV_LEN], **argv = NULL; + Jim_EvalFrame frame; + + if (Jim_IsList(scriptObjPtr) && scriptObjPtr->bytes == NULL) { + return JimEvalObjList(interp, scriptObjPtr); + } + + Jim_IncrRefCount(scriptObjPtr); + script = JimGetScript(interp, scriptObjPtr); + if (JimParseCheckMissing(interp, script->missing) == JIM_ERR) { + JimSetErrorStack(interp, script); + Jim_DecrRefCount(interp, scriptObjPtr); + return JIM_ERR; + } + + Jim_SetEmptyResult(interp); + + token = script->token; + +#ifdef JIM_OPTIMIZATION + if (script->len == 0) { + Jim_DecrRefCount(interp, scriptObjPtr); + return JIM_OK; + } + if (script->len == 3 + && token[1].objPtr->typePtr == &commandObjType + && token[1].objPtr->internalRep.cmdValue.cmdPtr->isproc == 0 + && token[1].objPtr->internalRep.cmdValue.cmdPtr->u.native.cmdProc == Jim_IncrCoreCommand + && token[2].objPtr->typePtr == &variableObjType) { + + Jim_Obj *objPtr = Jim_GetVariable(interp, token[2].objPtr, JIM_NONE); + + if (objPtr && !Jim_IsShared(objPtr) && objPtr->typePtr == &intObjType) { + JimWideValue(objPtr)++; + Jim_InvalidateStringRep(objPtr); + Jim_DecrRefCount(interp, scriptObjPtr); + Jim_SetResult(interp, objPtr); + return JIM_OK; + } + } +#endif + + script->inUse++; + + JimPushEvalFrame(interp, &frame, scriptObjPtr); + + + interp->errorFlag = 0; + argv = sargv; + + for (i = 0; i < script->len && retcode == JIM_OK; ) { + int argc; + int j; + + + argc = token[i].objPtr->internalRep.scriptLineValue.argc; + script->linenr = token[i].objPtr->internalRep.scriptLineValue.line; + + + if (argc > JIM_EVAL_SARGV_LEN) + argv = Jim_Alloc(sizeof(Jim_Obj *) * argc); + + + i++; + + for (j = 0; j < argc; j++) { + long wordtokens = 1; + int expand = 0; + Jim_Obj *wordObjPtr = NULL; + + if (token[i].type == JIM_TT_WORD) { + wordtokens = JimWideValue(token[i++].objPtr); + if (wordtokens < 0) { + expand = 1; + wordtokens = -wordtokens; + } + } + + if (wordtokens == 1) { + + switch (token[i].type) { + case JIM_TT_ESC: + case JIM_TT_STR: + wordObjPtr = token[i].objPtr; + break; + case JIM_TT_VAR: + wordObjPtr = Jim_GetVariable(interp, token[i].objPtr, JIM_ERRMSG); + break; + case JIM_TT_EXPRSUGAR: + retcode = Jim_EvalExpression(interp, token[i].objPtr); + if (retcode == JIM_OK) { + wordObjPtr = Jim_GetResult(interp); + } + else { + wordObjPtr = NULL; + } + break; + case JIM_TT_DICTSUGAR: + wordObjPtr = JimExpandDictSugar(interp, token[i].objPtr); + break; + case JIM_TT_CMD: + retcode = Jim_EvalObj(interp, token[i].objPtr); + if (retcode == JIM_OK) { + wordObjPtr = Jim_GetResult(interp); + } + break; + default: + JimPanic((1, "default token type reached " "in Jim_EvalObj().")); + } + } + else { + wordObjPtr = JimInterpolateTokens(interp, token + i, wordtokens, JIM_NONE); + } + + if (!wordObjPtr) { + if (retcode == JIM_OK) { + retcode = JIM_ERR; + } + break; + } + + Jim_IncrRefCount(wordObjPtr); + i += wordtokens; + + if (!expand) { + argv[j] = wordObjPtr; + } + else { + + int len = Jim_ListLength(interp, wordObjPtr); + int newargc = argc + len - 1; + int k; + + if (len > 1) { + if (argv == sargv) { + if (newargc > JIM_EVAL_SARGV_LEN) { + argv = Jim_Alloc(sizeof(*argv) * newargc); + memcpy(argv, sargv, sizeof(*argv) * j); + } + } + else { + + argv = Jim_Realloc(argv, sizeof(*argv) * newargc); + } + } + + + for (k = 0; k < len; k++) { + argv[j++] = wordObjPtr->internalRep.listValue.ele[k]; + Jim_IncrRefCount(wordObjPtr->internalRep.listValue.ele[k]); + } + + Jim_DecrRefCount(interp, wordObjPtr); + + + j--; + argc += len - 1; + } + } + + if (retcode == JIM_OK && argc) { + + retcode = JimInvokeCommand(interp, argc, argv); + + if (Jim_CheckSignal(interp)) { + retcode = JIM_SIGNAL; + } + } + + + while (j-- > 0) { + Jim_DecrRefCount(interp, argv[j]); + } + + if (argv != sargv) { + Jim_Free(argv); + argv = sargv; + } + } + + + if (retcode == JIM_ERR) { + JimSetErrorStack(interp, NULL); + } + + JimPopEvalFrame(interp); + + Jim_FreeIntRep(interp, scriptObjPtr); + scriptObjPtr->typePtr = &scriptObjType; + Jim_SetIntRepPtr(scriptObjPtr, script); + Jim_DecrRefCount(interp, scriptObjPtr); + + return retcode; +} + +static int JimSetProcArg(Jim_Interp *interp, Jim_Obj *argNameObj, Jim_Obj *argValObj) +{ + int retcode; + + const char *varname = Jim_String(argNameObj); + if (*varname == '&') { + + Jim_Obj *objPtr; + Jim_CallFrame *savedCallFrame = interp->framePtr; + + interp->framePtr = interp->framePtr->parent; + objPtr = Jim_GetVariable(interp, argValObj, JIM_ERRMSG); + interp->framePtr = savedCallFrame; + if (!objPtr) { + return JIM_ERR; + } + + + objPtr = Jim_NewStringObj(interp, varname + 1, -1); + Jim_IncrRefCount(objPtr); + retcode = Jim_SetVariableLink(interp, objPtr, argValObj, interp->framePtr->parent); + Jim_DecrRefCount(interp, objPtr); + } + else { + retcode = Jim_SetVariable(interp, argNameObj, argValObj); + } + return retcode; +} + +static void JimSetProcWrongArgs(Jim_Interp *interp, Jim_Obj *procNameObj, Jim_Cmd *cmd) +{ + + Jim_Obj *argmsg = Jim_NewStringObj(interp, "", 0); + int i; + + for (i = 0; i < cmd->u.proc.argListLen; i++) { + Jim_AppendString(interp, argmsg, " ", 1); + + if (i == cmd->u.proc.argsPos) { + if (cmd->u.proc.arglist[i].defaultObjPtr) { + + Jim_AppendString(interp, argmsg, "?", 1); + Jim_AppendObj(interp, argmsg, cmd->u.proc.arglist[i].defaultObjPtr); + Jim_AppendString(interp, argmsg, " ...?", -1); + } + else { + + Jim_AppendString(interp, argmsg, "?arg ...?", -1); + } + } + else { + if (cmd->u.proc.arglist[i].defaultObjPtr) { + Jim_AppendString(interp, argmsg, "?", 1); + Jim_AppendObj(interp, argmsg, cmd->u.proc.arglist[i].nameObjPtr); + Jim_AppendString(interp, argmsg, "?", 1); + } + else { + const char *arg = Jim_String(cmd->u.proc.arglist[i].nameObjPtr); + if (*arg == '&') { + arg++; + } + Jim_AppendString(interp, argmsg, arg, -1); + } + } + } + Jim_SetResultFormatted(interp, "wrong # args: should be \"%#s%#s\"", procNameObj, argmsg); +} + +#ifdef jim_ext_namespace +int Jim_EvalNamespace(Jim_Interp *interp, Jim_Obj *scriptObj, Jim_Obj *nsObj) +{ + Jim_CallFrame *callFramePtr; + int retcode; + + + callFramePtr = JimCreateCallFrame(interp, interp->framePtr, nsObj); + callFramePtr->argv = interp->evalFrame->argv; + callFramePtr->argc = interp->evalFrame->argc; + callFramePtr->procArgsObjPtr = NULL; + callFramePtr->procBodyObjPtr = scriptObj; + callFramePtr->staticVars = NULL; + Jim_IncrRefCount(scriptObj); + interp->framePtr = callFramePtr; + + + if (interp->framePtr->level == interp->maxCallFrameDepth) { + Jim_SetResultString(interp, "Too many nested calls. Infinite recursion?", -1); + retcode = JIM_ERR; + } + else { + + retcode = Jim_EvalObj(interp, scriptObj); + } + + + interp->framePtr = interp->framePtr->parent; + JimFreeCallFrame(interp, callFramePtr, JIM_FCF_REUSE); + + return retcode; +} +#endif + +static int JimCallProcedure(Jim_Interp *interp, Jim_Cmd *cmd, int argc, Jim_Obj *const *argv) +{ + Jim_CallFrame *callFramePtr; + int i, d, retcode, optargs; + + + if (argc - 1 < cmd->u.proc.reqArity || + (cmd->u.proc.argsPos < 0 && argc - 1 > cmd->u.proc.reqArity + cmd->u.proc.optArity)) { + JimSetProcWrongArgs(interp, argv[0], cmd); + return JIM_ERR; + } + + if (Jim_Length(cmd->u.proc.bodyObjPtr) == 0) { + + return JIM_OK; + } + + + if (interp->framePtr->level == interp->maxCallFrameDepth) { + Jim_SetResultString(interp, "Too many nested calls. Infinite recursion?", -1); + return JIM_ERR; + } + + + callFramePtr = JimCreateCallFrame(interp, interp->framePtr, cmd->u.proc.nsObj); + callFramePtr->argv = argv; + callFramePtr->argc = argc; + callFramePtr->procArgsObjPtr = cmd->u.proc.argListObjPtr; + callFramePtr->procBodyObjPtr = cmd->u.proc.bodyObjPtr; + callFramePtr->staticVars = cmd->u.proc.staticVars; + + interp->procLevel++; + + Jim_IncrRefCount(cmd->u.proc.argListObjPtr); + Jim_IncrRefCount(cmd->u.proc.bodyObjPtr); + interp->framePtr = callFramePtr; + + + optargs = (argc - 1 - cmd->u.proc.reqArity); + + + i = 1; + for (d = 0; d < cmd->u.proc.argListLen; d++) { + Jim_Obj *nameObjPtr = cmd->u.proc.arglist[d].nameObjPtr; + if (d == cmd->u.proc.argsPos) { + + Jim_Obj *listObjPtr; + int argsLen = 0; + if (cmd->u.proc.reqArity + cmd->u.proc.optArity < argc - 1) { + argsLen = argc - 1 - (cmd->u.proc.reqArity + cmd->u.proc.optArity); + } + listObjPtr = Jim_NewListObj(interp, &argv[i], argsLen); + + + if (cmd->u.proc.arglist[d].defaultObjPtr) { + nameObjPtr =cmd->u.proc.arglist[d].defaultObjPtr; + } + retcode = Jim_SetVariable(interp, nameObjPtr, listObjPtr); + if (retcode != JIM_OK) { + goto badargset; + } + + i += argsLen; + continue; + } + + + if (cmd->u.proc.arglist[d].defaultObjPtr == NULL || optargs-- > 0) { + retcode = JimSetProcArg(interp, nameObjPtr, argv[i++]); + } + else { + + retcode = Jim_SetVariable(interp, nameObjPtr, cmd->u.proc.arglist[d].defaultObjPtr); + } + if (retcode != JIM_OK) { + goto badargset; + } + } + + if (interp->traceCmdObj == NULL || + (retcode = JimTraceCallback(interp, "proc", argc, argv)) == JIM_OK) { + + retcode = Jim_EvalObj(interp, cmd->u.proc.bodyObjPtr); + } + +badargset: + + + retcode = JimInvokeDefer(interp, retcode); + interp->framePtr = interp->framePtr->parent; + JimFreeCallFrame(interp, callFramePtr, JIM_FCF_REUSE); + + + if (retcode == JIM_RETURN) { + if (--interp->returnLevel <= 0) { + retcode = interp->returnCode; + interp->returnCode = JIM_OK; + interp->returnLevel = 0; + } + } + interp->procLevel--; + + return retcode; +} + +int Jim_EvalSource(Jim_Interp *interp, const char *filename, int lineno, const char *script) +{ + int retval; + Jim_Obj *scriptObjPtr; + + scriptObjPtr = Jim_NewStringObj(interp, script, -1); + Jim_IncrRefCount(scriptObjPtr); + if (filename) { + Jim_SetSourceInfo(interp, scriptObjPtr, Jim_NewStringObj(interp, filename, -1), lineno); + } + retval = Jim_EvalObj(interp, scriptObjPtr); + Jim_DecrRefCount(interp, scriptObjPtr); + return retval; +} + +int Jim_Eval(Jim_Interp *interp, const char *script) +{ + return Jim_EvalObj(interp, Jim_NewStringObj(interp, script, -1)); +} + + +int Jim_EvalGlobal(Jim_Interp *interp, const char *script) +{ + int retval; + Jim_CallFrame *savedFramePtr = interp->framePtr; + + interp->framePtr = interp->topFramePtr; + retval = Jim_Eval(interp, script); + interp->framePtr = savedFramePtr; + + return retval; +} + +int Jim_EvalFileGlobal(Jim_Interp *interp, const char *filename) +{ + int retval; + Jim_CallFrame *savedFramePtr = interp->framePtr; + + interp->framePtr = interp->topFramePtr; + retval = Jim_EvalFile(interp, filename); + interp->framePtr = savedFramePtr; + + return retval; +} + +#include + +static Jim_Obj *JimReadTextFile(Jim_Interp *interp, const char *filename) +{ + jim_stat_t sb; + int fd; + char *buf; + int readlen; + + if (Jim_Stat(filename, &sb) == -1 || (fd = open(filename, O_RDONLY | O_TEXT, 0666)) < 0) { + Jim_SetResultFormatted(interp, "couldn't read file \"%s\": %s", filename, strerror(errno)); + return NULL; + } + buf = Jim_Alloc(sb.st_size + 1); + readlen = read(fd, buf, sb.st_size); + close(fd); + if (readlen < 0) { + Jim_Free(buf); + Jim_SetResultFormatted(interp, "failed to load file \"%s\": %s", filename, strerror(errno)); + return NULL; + } + else { + Jim_Obj *objPtr; + buf[readlen] = 0; + + objPtr = Jim_NewStringObjNoAlloc(interp, buf, readlen); + + return objPtr; + } +} + + +int Jim_EvalFile(Jim_Interp *interp, const char *filename) +{ + Jim_Obj *filenameObj; + Jim_Obj *oldFilenameObj; + Jim_Obj *scriptObjPtr; + int retcode; + + scriptObjPtr = JimReadTextFile(interp, filename); + if (!scriptObjPtr) { + return JIM_ERR; + } + + filenameObj = Jim_NewStringObj(interp, filename, -1); + Jim_SetSourceInfo(interp, scriptObjPtr, filenameObj, 1); + + oldFilenameObj = JimPushInterpObj(interp->currentFilenameObj, filenameObj); + + retcode = Jim_EvalObj(interp, scriptObjPtr); + + JimPopInterpObj(interp, interp->currentFilenameObj, oldFilenameObj); + + + if (retcode == JIM_RETURN) { + if (--interp->returnLevel <= 0) { + retcode = interp->returnCode; + interp->returnCode = JIM_OK; + interp->returnLevel = 0; + } + } + + return retcode; +} + +static void JimParseSubst(struct JimParserCtx *pc, int flags) +{ + pc->tstart = pc->p; + pc->tline = pc->linenr; + + if (pc->len == 0) { + pc->tend = pc->p; + pc->tt = JIM_TT_EOL; + pc->eof = 1; + return; + } + if (*pc->p == '[' && !(flags & JIM_SUBST_NOCMD)) { + JimParseCmd(pc); + return; + } + if (*pc->p == '$' && !(flags & JIM_SUBST_NOVAR)) { + if (JimParseVar(pc) == JIM_OK) { + return; + } + + pc->tstart = pc->p; + + pc->p++; + pc->len--; + } + while (pc->len) { + if (*pc->p == '$' && !(flags & JIM_SUBST_NOVAR)) { + break; + } + if (*pc->p == '[' && !(flags & JIM_SUBST_NOCMD)) { + break; + } + if (*pc->p == '\\' && pc->len > 1) { + pc->p++; + pc->len--; + } + pc->p++; + pc->len--; + } + pc->tend = pc->p - 1; + pc->tt = (flags & JIM_SUBST_NOESC) ? JIM_TT_STR : JIM_TT_ESC; +} + + +static int SetSubstFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr, int flags) +{ + int scriptTextLen; + const char *scriptText = Jim_GetString(objPtr, &scriptTextLen); + struct JimParserCtx parser; + struct ScriptObj *script = Jim_Alloc(sizeof(*script)); + ParseTokenList tokenlist; + + + ScriptTokenListInit(&tokenlist); + + JimParserInit(&parser, scriptText, scriptTextLen, 1); + while (1) { + JimParseSubst(&parser, flags); + if (parser.eof) { + + break; + } + ScriptAddToken(&tokenlist, parser.tstart, parser.tend - parser.tstart + 1, parser.tt, + parser.tline); + } + + + script->inUse = 1; + script->substFlags = flags; + script->fileNameObj = interp->emptyObj; + Jim_IncrRefCount(script->fileNameObj); + SubstObjAddTokens(interp, script, &tokenlist); + + + ScriptTokenListFree(&tokenlist); + +#ifdef DEBUG_SHOW_SUBST + { + int i; + + printf("==== Subst ====\n"); + for (i = 0; i < script->len; i++) { + printf("[%2d] %s '%s'\n", i, jim_tt_name(script->token[i].type), + Jim_String(script->token[i].objPtr)); + } + } +#endif + + + Jim_FreeIntRep(interp, objPtr); + Jim_SetIntRepPtr(objPtr, script); + objPtr->typePtr = &scriptObjType; + return JIM_OK; +} + +static ScriptObj *Jim_GetSubst(Jim_Interp *interp, Jim_Obj *objPtr, int flags) +{ + if (objPtr->typePtr != &scriptObjType || ((ScriptObj *)Jim_GetIntRepPtr(objPtr))->substFlags != flags) + SetSubstFromAny(interp, objPtr, flags); + return (ScriptObj *) Jim_GetIntRepPtr(objPtr); +} + +int Jim_SubstObj(Jim_Interp *interp, Jim_Obj *substObjPtr, Jim_Obj **resObjPtrPtr, int flags) +{ + ScriptObj *script; + + JimPanic((substObjPtr->refCount == 0, "Jim_SubstObj() called with zero refcount object")); + + script = Jim_GetSubst(interp, substObjPtr, flags); + + Jim_IncrRefCount(substObjPtr); + script->inUse++; + + *resObjPtrPtr = JimInterpolateTokens(interp, script->token, script->len, flags); + + script->inUse--; + Jim_DecrRefCount(interp, substObjPtr); + if (*resObjPtrPtr == NULL) { + return JIM_ERR; + } + return JIM_OK; +} + +void Jim_WrongNumArgs(Jim_Interp *interp, int argc, Jim_Obj *const *argv, const char *msg) +{ + Jim_Obj *objPtr; + Jim_Obj *listObjPtr; + + JimPanic((argc == 0, "Jim_WrongNumArgs() called with argc=0")); + + listObjPtr = Jim_NewListObj(interp, argv, argc); + + if (msg && *msg) { + Jim_ListAppendElement(interp, listObjPtr, Jim_NewStringObj(interp, msg, -1)); + } + Jim_IncrRefCount(listObjPtr); + objPtr = Jim_ListJoin(interp, listObjPtr, " ", 1); + Jim_DecrRefCount(interp, listObjPtr); + + Jim_SetResultFormatted(interp, "wrong # args: should be \"%#s\"", objPtr); +} + +typedef void JimHashtableIteratorCallbackType(Jim_Interp *interp, Jim_Obj *listObjPtr, + Jim_Obj *keyObjPtr, void *value, Jim_Obj *patternObjPtr, int type); + +#define JimTrivialMatch(pattern) (strpbrk((pattern), "*[?\\") == NULL) + +static Jim_Obj *JimHashtablePatternMatch(Jim_Interp *interp, Jim_HashTable *ht, Jim_Obj *patternObjPtr, + JimHashtableIteratorCallbackType *callback, int type) +{ + Jim_HashEntry *he; + Jim_Obj *listObjPtr = Jim_NewListObj(interp, NULL, 0); + + + if (patternObjPtr && JimTrivialMatch(Jim_String(patternObjPtr))) { + he = Jim_FindHashEntry(ht, patternObjPtr); + if (he) { + callback(interp, listObjPtr, Jim_GetHashEntryKey(he), Jim_GetHashEntryVal(he), + patternObjPtr, type); + } + } + else { + Jim_HashTableIterator htiter; + JimInitHashTableIterator(ht, &htiter); + while ((he = Jim_NextHashEntry(&htiter)) != NULL) { + callback(interp, listObjPtr, Jim_GetHashEntryKey(he), Jim_GetHashEntryVal(he), + patternObjPtr, type); + } + } + return listObjPtr; +} + + +#define JIM_CMDLIST_COMMANDS 0 +#define JIM_CMDLIST_PROCS 1 +#define JIM_CMDLIST_CHANNELS 2 + +static void JimCommandMatch(Jim_Interp *interp, Jim_Obj *listObjPtr, + Jim_Obj *keyObj, void *value, Jim_Obj *patternObj, int type) +{ + Jim_Cmd *cmdPtr = (Jim_Cmd *)value; + + if (type == JIM_CMDLIST_PROCS && !cmdPtr->isproc) { + + return; + } + + Jim_IncrRefCount(keyObj); + + if (type != JIM_CMDLIST_CHANNELS || Jim_AioFilehandle(interp, keyObj) >= 0) { + int match = 1; + if (patternObj) { + int plen, slen; + const char *pattern = Jim_GetStringNoQualifier(patternObj, &plen); + const char *str = Jim_GetStringNoQualifier(keyObj, &slen); +#ifdef JIM_NO_INTROSPECTION + + match = (JimStringCompareUtf8(pattern, plen, str, slen, 0) == 0); +#else + match = JimGlobMatch(pattern, plen, str, slen, 0); +#endif + } + if (match) { + Jim_ListAppendElement(interp, listObjPtr, keyObj); + } + } + Jim_DecrRefCount(interp, keyObj); +} + +static Jim_Obj *JimCommandsList(Jim_Interp *interp, Jim_Obj *patternObjPtr, int type) +{ + return JimHashtablePatternMatch(interp, &interp->commands, patternObjPtr, JimCommandMatch, type); +} + + +#define JIM_VARLIST_GLOBALS 0 +#define JIM_VARLIST_LOCALS 1 +#define JIM_VARLIST_VARS 2 +#define JIM_VARLIST_MASK 0x000f + +#define JIM_VARLIST_VALUES 0x1000 + +static void JimVariablesMatch(Jim_Interp *interp, Jim_Obj *listObjPtr, + Jim_Obj *keyObj, void *value, Jim_Obj *patternObj, int type) +{ + Jim_VarVal *vv = (Jim_VarVal *)value; + + if ((type & JIM_VARLIST_MASK) != JIM_VARLIST_LOCALS || vv->linkFramePtr == NULL) { + if (patternObj == NULL || Jim_StringMatchObj(interp, patternObj, keyObj, 0)) { + Jim_ListAppendElement(interp, listObjPtr, keyObj); + if (type & JIM_VARLIST_VALUES) { + Jim_ListAppendElement(interp, listObjPtr, vv->objPtr); + } + } + } +} + + +static Jim_Obj *JimVariablesList(Jim_Interp *interp, Jim_Obj *patternObjPtr, int mode) +{ + if (mode == JIM_VARLIST_LOCALS && interp->framePtr == interp->topFramePtr) { + return interp->emptyObj; + } + else { + Jim_CallFrame *framePtr = (mode == JIM_VARLIST_GLOBALS) ? interp->topFramePtr : interp->framePtr; + return JimHashtablePatternMatch(interp, &framePtr->vars, patternObjPtr, JimVariablesMatch, + mode); + } +} + +static int JimInfoLevel(Jim_Interp *interp, Jim_Obj *levelObjPtr, Jim_Obj **objPtrPtr) +{ + long level; + + if (Jim_GetLong(interp, levelObjPtr, &level) == JIM_OK) { + Jim_CallFrame *targetCallFrame = JimGetCallFrameByInteger(interp, level); + if (targetCallFrame && targetCallFrame != interp->topFramePtr) { +#ifdef JIM_NO_INTROSPECTION + + *objPtrPtr = Jim_NewListObj(interp, targetCallFrame->argv, 1); +#else + *objPtrPtr = Jim_NewListObj(interp, targetCallFrame->argv, targetCallFrame->argc); +#endif + return JIM_OK; + } + } + Jim_SetResultFormatted(interp, "bad level \"%#s\"", levelObjPtr); + return JIM_ERR; +} + +static int JimInfoFrame(Jim_Interp *interp, Jim_Obj *levelObjPtr, Jim_Obj **objPtrPtr) +{ + long level; + + if (Jim_GetLong(interp, levelObjPtr, &level) == JIM_OK) { + Jim_EvalFrame *frame = JimGetEvalFrameByProcLevel(interp, level); + if (frame) { + Jim_Obj *listObj = Jim_NewListObj(interp, NULL, 0); + + Jim_ListAppendElement(interp, listObj, Jim_NewStringObj(interp, "type", -1)); + Jim_ListAppendElement(interp, listObj, Jim_NewStringObj(interp, "source", -1)); + if (frame->scriptObj) { + ScriptObj *script = JimGetScript(interp, frame->scriptObj); + Jim_ListAppendElement(interp, listObj, Jim_NewStringObj(interp, "line", -1)); + Jim_ListAppendElement(interp, listObj, Jim_NewIntObj(interp, script->linenr)); + Jim_ListAppendElement(interp, listObj, Jim_NewStringObj(interp, "file", -1)); + Jim_ListAppendElement(interp, listObj, script->fileNameObj); + } +#ifndef JIM_NO_INTROSPECTION + { + Jim_Obj *cmdObj = Jim_NewListObj(interp, frame->argv, frame->argc); + + Jim_ListAppendElement(interp, listObj, Jim_NewStringObj(interp, "cmd", -1)); + Jim_ListAppendElement(interp, listObj, cmdObj); + } +#endif + { + Jim_Obj *procNameObj = JimProcForEvalFrame(interp, frame); + if (procNameObj) { + Jim_ListAppendElement(interp, listObj, Jim_NewStringObj(interp, "proc", -1)); + Jim_ListAppendElement(interp, listObj, procNameObj); + } + } + Jim_ListAppendElement(interp, listObj, Jim_NewStringObj(interp, "level", -1)); + Jim_ListAppendElement(interp, listObj, Jim_NewIntObj(interp, interp->framePtr->level - frame->framePtr->level)); + + *objPtrPtr = listObj; + return JIM_OK; + } + } + Jim_SetResultFormatted(interp, "bad level \"%#s\"", levelObjPtr); + return JIM_ERR; +} + + +static int Jim_PutsCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + if (argc != 2 && argc != 3) { + Jim_WrongNumArgs(interp, 1, argv, "?-nonewline? string"); + return JIM_ERR; + } + if (argc == 3) { + if (!Jim_CompareStringImmediate(interp, argv[1], "-nonewline")) { + Jim_SetResultString(interp, "The second argument must " "be -nonewline", -1); + return JIM_ERR; + } + else { + fputs(Jim_String(argv[2]), stdout); + } + } + else { + puts(Jim_String(argv[1])); + } + return JIM_OK; +} + + +static int JimAddMulHelper(Jim_Interp *interp, int argc, Jim_Obj *const *argv, int op) +{ + jim_wide wideValue, res; + double doubleValue, doubleRes; + int i; + + res = (op == JIM_EXPROP_ADD) ? 0 : 1; + + for (i = 1; i < argc; i++) { + if (Jim_GetWide(interp, argv[i], &wideValue) != JIM_OK) + goto trydouble; + if (op == JIM_EXPROP_ADD) + res += wideValue; + else + res *= wideValue; + } + Jim_SetResultInt(interp, res); + return JIM_OK; + trydouble: + doubleRes = (double)res; + for (; i < argc; i++) { + if (Jim_GetDouble(interp, argv[i], &doubleValue) != JIM_OK) + return JIM_ERR; + if (op == JIM_EXPROP_ADD) + doubleRes += doubleValue; + else + doubleRes *= doubleValue; + } + Jim_SetResult(interp, Jim_NewDoubleObj(interp, doubleRes)); + return JIM_OK; +} + + +static int JimSubDivHelper(Jim_Interp *interp, int argc, Jim_Obj *const *argv, int op) +{ + jim_wide wideValue, res = 0; + double doubleValue, doubleRes = 0; + int i = 2; + + if (argc < 2) { + Jim_WrongNumArgs(interp, 1, argv, "number ?number ... number?"); + return JIM_ERR; + } + else if (argc == 2) { + if (Jim_GetWide(interp, argv[1], &wideValue) != JIM_OK) { + if (Jim_GetDouble(interp, argv[1], &doubleValue) != JIM_OK) { + return JIM_ERR; + } + else { + if (op == JIM_EXPROP_SUB) + doubleRes = -doubleValue; + else + doubleRes = 1.0 / doubleValue; + Jim_SetResult(interp, Jim_NewDoubleObj(interp, doubleRes)); + return JIM_OK; + } + } + if (op == JIM_EXPROP_SUB) { + res = -wideValue; + Jim_SetResultInt(interp, res); + } + else { + doubleRes = 1.0 / wideValue; + Jim_SetResult(interp, Jim_NewDoubleObj(interp, doubleRes)); + } + return JIM_OK; + } + else { + if (Jim_GetWide(interp, argv[1], &res) != JIM_OK) { + if (Jim_GetDouble(interp, argv[1], &doubleRes) + != JIM_OK) { + return JIM_ERR; + } + else { + goto trydouble; + } + } + } + for (i = 2; i < argc; i++) { + if (Jim_GetWide(interp, argv[i], &wideValue) != JIM_OK) { + doubleRes = (double)res; + goto trydouble; + } + if (op == JIM_EXPROP_SUB) + res -= wideValue; + else { + if (wideValue == 0) { + Jim_SetResultString(interp, "Division by zero", -1); + return JIM_ERR; + } + res /= wideValue; + } + } + Jim_SetResultInt(interp, res); + return JIM_OK; + trydouble: + for (; i < argc; i++) { + if (Jim_GetDouble(interp, argv[i], &doubleValue) != JIM_OK) + return JIM_ERR; + if (op == JIM_EXPROP_SUB) + doubleRes -= doubleValue; + else + doubleRes /= doubleValue; + } + Jim_SetResult(interp, Jim_NewDoubleObj(interp, doubleRes)); + return JIM_OK; +} + + + +static int Jim_AddCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + return JimAddMulHelper(interp, argc, argv, JIM_EXPROP_ADD); +} + + +static int Jim_MulCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + return JimAddMulHelper(interp, argc, argv, JIM_EXPROP_MUL); +} + + +static int Jim_SubCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + return JimSubDivHelper(interp, argc, argv, JIM_EXPROP_SUB); +} + + +static int Jim_DivCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + return JimSubDivHelper(interp, argc, argv, JIM_EXPROP_DIV); +} + + +static int Jim_SetCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + if (argc != 2 && argc != 3) { + Jim_WrongNumArgs(interp, 1, argv, "varName ?newValue?"); + return JIM_ERR; + } + if (argc == 2) { + Jim_Obj *objPtr; + + objPtr = Jim_GetVariable(interp, argv[1], JIM_ERRMSG); + if (!objPtr) + return JIM_ERR; + Jim_SetResult(interp, objPtr); + return JIM_OK; + } + + if (Jim_SetVariable(interp, argv[1], argv[2]) != JIM_OK) + return JIM_ERR; + Jim_SetResult(interp, argv[2]); + return JIM_OK; +} + +static int Jim_UnsetCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + int i = 1; + int complain = 1; + + while (i < argc) { + if (Jim_CompareStringImmediate(interp, argv[i], "--")) { + i++; + break; + } + if (Jim_CompareStringImmediate(interp, argv[i], "-nocomplain")) { + complain = 0; + i++; + continue; + } + break; + } + + while (i < argc) { + if (Jim_UnsetVariable(interp, argv[i], complain ? JIM_ERRMSG : JIM_NONE) != JIM_OK + && complain) { + return JIM_ERR; + } + i++; + } + + Jim_SetEmptyResult(interp); + return JIM_OK; +} + +static int JimCheckLoopRetcode(Jim_Interp *interp, int retval) +{ + if (retval == JIM_BREAK || retval == JIM_CONTINUE) { + if (--interp->break_level > 0) { + return 1; + } + } + return 0; +} + + +static int Jim_WhileCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + if (argc != 3) { + Jim_WrongNumArgs(interp, 1, argv, "condition body"); + return JIM_ERR; + } + + + while (1) { + int boolean = 0, retval; + + if ((retval = Jim_GetBoolFromExpr(interp, argv[1], &boolean)) != JIM_OK) + return retval; + if (!boolean) + break; + + if ((retval = Jim_EvalObj(interp, argv[2])) != JIM_OK) { + if (JimCheckLoopRetcode(interp, retval)) { + return retval; + } + switch (retval) { + case JIM_BREAK: + goto out; + case JIM_CONTINUE: + continue; + default: + return retval; + } + } + } + out: + Jim_SetEmptyResult(interp); + return JIM_OK; +} + + +static int Jim_ForCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + int retval; + int boolean = 1; + int immediate = 0; + Jim_Obj *varNamePtr = NULL; + Jim_Obj *stopVarNamePtr = NULL; + + if (argc != 5) { + Jim_WrongNumArgs(interp, 1, argv, "start test next body"); + return JIM_ERR; + } + + + if ((retval = Jim_EvalObj(interp, argv[1])) != JIM_OK) { + return retval; + } + + retval = Jim_GetBoolFromExpr(interp, argv[2], &boolean); + + +#ifdef JIM_OPTIMIZATION + if (retval == JIM_OK && boolean) { + ScriptObj *incrScript; + struct ExprTree *expr; + jim_wide stop, currentVal; + Jim_Obj *objPtr; + int cmpOffset; + + + expr = JimGetExpression(interp, argv[2]); + incrScript = JimGetScript(interp, argv[3]); + + + if (incrScript == NULL || incrScript->len != 3 || !expr || expr->len != 3) { + goto evalstart; + } + + if (incrScript->token[1].type != JIM_TT_ESC) { + goto evalstart; + } + + if (expr->expr->type == JIM_EXPROP_LT) { + cmpOffset = 0; + } + else if (expr->expr->type == JIM_EXPROP_LTE) { + cmpOffset = 1; + } + else { + goto evalstart; + } + + if (expr->expr->left->type != JIM_TT_VAR) { + goto evalstart; + } + + if (expr->expr->right->type != JIM_TT_VAR && expr->expr->right->type != JIM_TT_EXPR_INT) { + goto evalstart; + } + + + if (!Jim_CompareStringImmediate(interp, incrScript->token[1].objPtr, "incr")) { + goto evalstart; + } + + + if (!Jim_StringEqObj(incrScript->token[2].objPtr, expr->expr->left->objPtr)) { + goto evalstart; + } + + + if (expr->expr->right->type == JIM_TT_EXPR_INT) { + if (Jim_GetWideExpr(interp, expr->expr->right->objPtr, &stop) == JIM_ERR) { + goto evalstart; + } + } + else { + stopVarNamePtr = expr->expr->right->objPtr; + Jim_IncrRefCount(stopVarNamePtr); + + stop = 0; + } + + + varNamePtr = expr->expr->left->objPtr; + Jim_IncrRefCount(varNamePtr); + + objPtr = Jim_GetVariable(interp, varNamePtr, JIM_NONE); + if (objPtr == NULL || Jim_GetWide(interp, objPtr, ¤tVal) != JIM_OK) { + goto testcond; + } + + + while (retval == JIM_OK) { + + + + + if (stopVarNamePtr) { + objPtr = Jim_GetVariable(interp, stopVarNamePtr, JIM_NONE); + if (objPtr == NULL || Jim_GetWide(interp, objPtr, &stop) != JIM_OK) { + goto testcond; + } + } + + if (currentVal >= stop + cmpOffset) { + break; + } + + + retval = Jim_EvalObj(interp, argv[4]); + if (JimCheckLoopRetcode(interp, retval)) { + immediate++; + goto out; + } + if (retval == JIM_OK || retval == JIM_CONTINUE) { + retval = JIM_OK; + + objPtr = Jim_GetVariable(interp, varNamePtr, JIM_ERRMSG); + + + if (objPtr == NULL) { + retval = JIM_ERR; + goto out; + } + if (!Jim_IsShared(objPtr) && objPtr->typePtr == &intObjType) { + currentVal = ++JimWideValue(objPtr); + Jim_InvalidateStringRep(objPtr); + } + else { + if (Jim_GetWide(interp, objPtr, ¤tVal) != JIM_OK || + Jim_SetVariable(interp, varNamePtr, Jim_NewIntObj(interp, + ++currentVal)) != JIM_OK) { + goto evalnext; + } + } + } + } + goto out; + } + evalstart: +#endif + + while (boolean && (retval == JIM_OK || retval == JIM_CONTINUE)) { + + retval = Jim_EvalObj(interp, argv[4]); + if (JimCheckLoopRetcode(interp, retval)) { + immediate++; + break; + } + if (retval == JIM_OK || retval == JIM_CONTINUE) { + +JIM_IF_OPTIM(evalnext:) + retval = Jim_EvalObj(interp, argv[3]); + if (retval == JIM_OK || retval == JIM_CONTINUE) { + +JIM_IF_OPTIM(testcond:) + retval = Jim_GetBoolFromExpr(interp, argv[2], &boolean); + } + } + } +JIM_IF_OPTIM(out:) + if (stopVarNamePtr) { + Jim_DecrRefCount(interp, stopVarNamePtr); + } + if (varNamePtr) { + Jim_DecrRefCount(interp, varNamePtr); + } + + if (!immediate) { + if (retval == JIM_CONTINUE || retval == JIM_BREAK || retval == JIM_OK) { + Jim_SetEmptyResult(interp); + return JIM_OK; + } + } + + return retval; +} + + +static int Jim_LoopCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + int retval; + jim_wide i; + jim_wide limit = 0; + jim_wide incr = 1; + Jim_Obj *bodyObjPtr; + + if (argc < 4 || argc > 6) { + Jim_WrongNumArgs(interp, 1, argv, "var ?first? limit ?incr? body"); + return JIM_ERR; + } + + retval = Jim_GetWideExpr(interp, argv[2], &i); + if (argc > 4 && retval == JIM_OK) { + retval = Jim_GetWideExpr(interp, argv[3], &limit); + } + if (argc > 5 && retval == JIM_OK) { + Jim_GetWideExpr(interp, argv[4], &incr); + } + if (retval != JIM_OK) { + return retval; + } + if (argc == 4) { + limit = i; + i = 0; + } + bodyObjPtr = argv[argc - 1]; + + retval = Jim_SetVariable(interp, argv[1], Jim_NewIntObj(interp, i)); + + while (((i < limit && incr > 0) || (i > limit && incr < 0)) && retval == JIM_OK) { + retval = Jim_EvalObj(interp, bodyObjPtr); + if (JimCheckLoopRetcode(interp, retval)) { + return retval; + } + if (retval == JIM_OK || retval == JIM_CONTINUE) { + Jim_Obj *objPtr = Jim_GetVariable(interp, argv[1], JIM_ERRMSG); + + retval = JIM_OK; + + + i += incr; + + if (objPtr && !Jim_IsShared(objPtr) && objPtr->typePtr == &intObjType) { + if (argv[1]->typePtr != &variableObjType) { + if (Jim_SetVariable(interp, argv[1], objPtr) != JIM_OK) { + return JIM_ERR; + } + } + JimWideValue(objPtr) = i; + Jim_InvalidateStringRep(objPtr); + + if (argv[1]->typePtr != &variableObjType) { + if (Jim_SetVariable(interp, argv[1], objPtr) != JIM_OK) { + retval = JIM_ERR; + break; + } + } + } + else { + objPtr = Jim_NewIntObj(interp, i); + retval = Jim_SetVariable(interp, argv[1], objPtr); + if (retval != JIM_OK) { + Jim_FreeNewObj(interp, objPtr); + } + } + } + } + + if (retval == JIM_OK || retval == JIM_CONTINUE || retval == JIM_BREAK) { + Jim_SetEmptyResult(interp); + return JIM_OK; + } + return retval; +} + +typedef struct { + Jim_Obj *objPtr; + int idx; +} Jim_ListIter; + +static void JimListIterInit(Jim_ListIter *iter, Jim_Obj *objPtr) +{ + iter->objPtr = objPtr; + iter->idx = 0; +} + +static Jim_Obj *JimListIterNext(Jim_Interp *interp, Jim_ListIter *iter) +{ + if (iter->idx >= Jim_ListLength(interp, iter->objPtr)) { + return NULL; + } + return iter->objPtr->internalRep.listValue.ele[iter->idx++]; +} + +static int JimListIterDone(Jim_Interp *interp, Jim_ListIter *iter) +{ + return iter->idx >= Jim_ListLength(interp, iter->objPtr); +} + + +static int JimForeachMapHelper(Jim_Interp *interp, int argc, Jim_Obj *const *argv, int doMap) +{ + int result = JIM_OK; + int i, numargs; + Jim_ListIter twoiters[2]; + Jim_ListIter *iters; + Jim_Obj *script; + Jim_Obj *resultObj; + + if (argc < 4 || argc % 2 != 0) { + Jim_WrongNumArgs(interp, 1, argv, "varList list ?varList list ...? script"); + return JIM_ERR; + } + script = argv[argc - 1]; + numargs = (argc - 1 - 1); + + if (numargs == 2) { + iters = twoiters; + } + else { + iters = Jim_Alloc(numargs * sizeof(*iters)); + } + for (i = 0; i < numargs; i++) { + JimListIterInit(&iters[i], argv[i + 1]); + if (i % 2 == 0 && JimListIterDone(interp, &iters[i])) { + result = JIM_ERR; + } + } + if (result != JIM_OK) { + Jim_SetResultString(interp, "foreach varlist is empty", -1); + goto empty_varlist; + } + + if (doMap) { + resultObj = Jim_NewListObj(interp, NULL, 0); + } + else { + resultObj = interp->emptyObj; + } + Jim_IncrRefCount(resultObj); + + while (1) { + + for (i = 0; i < numargs; i += 2) { + if (!JimListIterDone(interp, &iters[i + 1])) { + break; + } + } + if (i == numargs) { + + break; + } + + + for (i = 0; i < numargs; i += 2) { + Jim_Obj *varName; + + + JimListIterInit(&iters[i], argv[i + 1]); + while ((varName = JimListIterNext(interp, &iters[i])) != NULL) { + Jim_Obj *valObj = JimListIterNext(interp, &iters[i + 1]); + if (!valObj) { + + valObj = interp->emptyObj; + } + + Jim_IncrRefCount(valObj); + result = Jim_SetVariable(interp, varName, valObj); + Jim_DecrRefCount(interp, valObj); + if (result != JIM_OK) { + goto err; + } + } + } + result = Jim_EvalObj(interp, script); + if (JimCheckLoopRetcode(interp, result)) { + goto err; + } + switch (result) { + case JIM_OK: + if (doMap) { + Jim_ListAppendElement(interp, resultObj, interp->result); + } + break; + case JIM_CONTINUE: + break; + case JIM_BREAK: + goto out; + default: + goto err; + } + } + out: + result = JIM_OK; + Jim_SetResult(interp, resultObj); + err: + Jim_DecrRefCount(interp, resultObj); + empty_varlist: + if (numargs > 2) { + Jim_Free(iters); + } + return result; +} + + +static int Jim_ForeachCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + return JimForeachMapHelper(interp, argc, argv, 0); +} + + +static int Jim_LmapCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + return JimForeachMapHelper(interp, argc, argv, 1); +} + + +static int Jim_LassignCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + int result = JIM_ERR; + int i; + Jim_ListIter iter; + Jim_Obj *resultObj; + + if (argc < 2) { + Jim_WrongNumArgs(interp, 1, argv, "varList list ?varName ...?"); + return JIM_ERR; + } + + JimListIterInit(&iter, argv[1]); + + for (i = 2; i < argc; i++) { + Jim_Obj *valObj = JimListIterNext(interp, &iter); + result = Jim_SetVariable(interp, argv[i], valObj ? valObj : interp->emptyObj); + if (result != JIM_OK) { + return result; + } + } + + resultObj = Jim_NewListObj(interp, NULL, 0); + while (!JimListIterDone(interp, &iter)) { + Jim_ListAppendElement(interp, resultObj, JimListIterNext(interp, &iter)); + } + + Jim_SetResult(interp, resultObj); + + return JIM_OK; +} + + +static int Jim_IfCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + int boolean, retval, current = 1, falsebody = 0; + + if (argc >= 3) { + while (1) { + + if (current >= argc) + goto err; + if ((retval = Jim_GetBoolFromExpr(interp, argv[current++], &boolean)) + != JIM_OK) + return retval; + + if (current >= argc) + goto err; + if (Jim_CompareStringImmediate(interp, argv[current], "then")) + current++; + + if (current >= argc) + goto err; + if (boolean) + return Jim_EvalObj(interp, argv[current]); + + if (++current >= argc) { + Jim_SetResult(interp, Jim_NewEmptyStringObj(interp)); + return JIM_OK; + } + falsebody = current++; + if (Jim_CompareStringImmediate(interp, argv[falsebody], "else")) { + + if (current != argc - 1) + goto err; + return Jim_EvalObj(interp, argv[current]); + } + else if (Jim_CompareStringImmediate(interp, argv[falsebody], "elseif")) + continue; + + else if (falsebody != argc - 1) + goto err; + return Jim_EvalObj(interp, argv[falsebody]); + } + return JIM_OK; + } + err: + Jim_WrongNumArgs(interp, 1, argv, "condition ?then? trueBody ?elseif ...? ?else? falseBody"); + return JIM_ERR; +} + + +int Jim_CommandMatchObj(Jim_Interp *interp, Jim_Obj *commandObj, Jim_Obj *patternObj, + Jim_Obj *stringObj, int flags) +{ + Jim_Obj *parms[5]; + int argc = 0; + long eq; + int rc; + + parms[argc++] = commandObj; + if (flags & JIM_NOCASE) { + parms[argc++] = Jim_NewStringObj(interp, "-nocase", -1); + } + if (flags & JIM_OPT_END) { + parms[argc++] = Jim_NewStringObj(interp, "--", -1); + } + parms[argc++] = patternObj; + parms[argc++] = stringObj; + + rc = Jim_EvalObjVector(interp, argc, parms); + + if (rc != JIM_OK || Jim_GetLong(interp, Jim_GetResult(interp), &eq) != JIM_OK) { + eq = -rc; + } + + return eq; +} + + +static int Jim_SwitchCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + enum { SWITCH_EXACT, SWITCH_GLOB, SWITCH_RE, SWITCH_CMD }; + int matchOpt = SWITCH_EXACT, opt = 1, patCount, i; + int match_flags = 0; + Jim_Obj *command = NULL, *scriptObj = NULL, *strObj; + Jim_Obj **caseList; + + if (argc < 3) { + wrongnumargs: + Jim_WrongNumArgs(interp, 1, argv, "?options? string " + "pattern body ... ?default body? or " "{pattern body ?pattern body ...?}"); + return JIM_ERR; + } + for (opt = 1; opt < argc; ++opt) { + const char *option = Jim_String(argv[opt]); + + if (*option != '-') + break; + else if (strncmp(option, "--", 2) == 0) { + ++opt; + break; + } + else if (strncmp(option, "-exact", 2) == 0) + matchOpt = SWITCH_EXACT; + else if (strncmp(option, "-glob", 2) == 0) + matchOpt = SWITCH_GLOB; + else if (strncmp(option, "-regexp", 2) == 0) { + matchOpt = SWITCH_RE; + match_flags |= JIM_OPT_END; + } + else if (strncmp(option, "-command", 2) == 0) { + matchOpt = SWITCH_CMD; + if ((argc - opt) < 2) + goto wrongnumargs; + command = argv[++opt]; + } + else { + Jim_SetResultFormatted(interp, + "bad option \"%#s\": must be -exact, -glob, -regexp, -command procname or --", + argv[opt]); + return JIM_ERR; + } + if ((argc - opt) < 2) + goto wrongnumargs; + } + strObj = argv[opt++]; + patCount = argc - opt; + if (patCount == 1) { + JimListGetElements(interp, argv[opt], &patCount, &caseList); + } + else + caseList = (Jim_Obj **)&argv[opt]; + if (patCount == 0 || patCount % 2 != 0) + goto wrongnumargs; + for (i = 0; scriptObj == NULL && i < patCount; i += 2) { + Jim_Obj *patObj = caseList[i]; + + if (!Jim_CompareStringImmediate(interp, patObj, "default") + || i < (patCount - 2)) { + switch (matchOpt) { + case SWITCH_EXACT: + if (Jim_StringEqObj(strObj, patObj)) + scriptObj = caseList[i + 1]; + break; + case SWITCH_GLOB: + if (Jim_StringMatchObj(interp, patObj, strObj, 0)) + scriptObj = caseList[i + 1]; + break; + case SWITCH_RE: + command = Jim_NewStringObj(interp, "regexp", -1); + + case SWITCH_CMD:{ + int rc = Jim_CommandMatchObj(interp, command, patObj, strObj, match_flags); + + if (argc - opt == 1) { + JimListGetElements(interp, argv[opt], &patCount, &caseList); + } + + if (rc < 0) { + return -rc; + } + if (rc) + scriptObj = caseList[i + 1]; + break; + } + } + } + else { + scriptObj = caseList[i + 1]; + } + } + for (; i < patCount && Jim_CompareStringImmediate(interp, scriptObj, "-"); i += 2) + scriptObj = caseList[i + 1]; + if (scriptObj && Jim_CompareStringImmediate(interp, scriptObj, "-")) { + Jim_SetResultFormatted(interp, "no body specified for pattern \"%#s\"", caseList[i - 2]); + return JIM_ERR; + } + Jim_SetEmptyResult(interp); + if (scriptObj) { + return Jim_EvalObj(interp, scriptObj); + } + return JIM_OK; +} + + +static int Jim_ListCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + Jim_Obj *listObjPtr; + + listObjPtr = Jim_NewListObj(interp, argv + 1, argc - 1); + Jim_SetResult(interp, listObjPtr); + return JIM_OK; +} + + +static int Jim_LindexCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + Jim_Obj *objPtr; + int ret; + + if (argc < 2) { + Jim_WrongNumArgs(interp, 1, argv, "list ?index ...?"); + return JIM_ERR; + } + ret = Jim_ListIndices(interp, argv[1], argv + 2, argc - 2, &objPtr, JIM_NONE); + if (ret < 0) { + ret = JIM_OK; + Jim_SetEmptyResult(interp); + } + else if (ret == JIM_OK) { + Jim_SetResult(interp, objPtr); + } + return ret; +} + + +static int Jim_LlengthCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + if (argc != 2) { + Jim_WrongNumArgs(interp, 1, argv, "list"); + return JIM_ERR; + } + Jim_SetResultInt(interp, Jim_ListLength(interp, argv[1])); + return JIM_OK; +} + + +static int Jim_LsearchCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + static const char * const options[] = { + "-bool", "-not", "-nocase", "-exact", "-glob", "-regexp", "-all", "-inline", "-command", + "-stride", "-index", NULL + }; + enum + { OPT_BOOL, OPT_NOT, OPT_NOCASE, OPT_EXACT, OPT_GLOB, OPT_REGEXP, OPT_ALL, OPT_INLINE, + OPT_COMMAND, OPT_STRIDE, OPT_INDEX }; + int i; + int opt_bool = 0; + int opt_not = 0; + int opt_all = 0; + int opt_inline = 0; + int opt_match = OPT_EXACT; + int listlen; + int rc = JIM_OK; + Jim_Obj *listObjPtr = NULL; + Jim_Obj *commandObj = NULL; + Jim_Obj *indexObj = NULL; + int match_flags = 0; + long stride = 1; + + if (argc < 3) { + wrongargs: + Jim_WrongNumArgs(interp, 1, argv, + "?-exact|-glob|-regexp|-command 'command'? ?-bool|-inline? ?-not? ?-nocase? ?-all? ?-stride len? ?-index val? list value"); + return JIM_ERR; + } + + for (i = 1; i < argc - 2; i++) { + int option; + + if (Jim_GetEnum(interp, argv[i], options, &option, NULL, JIM_ERRMSG) != JIM_OK) { + return JIM_ERR; + } + switch (option) { + case OPT_BOOL: + opt_bool = 1; + opt_inline = 0; + break; + case OPT_NOT: + opt_not = 1; + break; + case OPT_NOCASE: + match_flags |= JIM_NOCASE; + break; + case OPT_INLINE: + opt_inline = 1; + opt_bool = 0; + break; + case OPT_ALL: + opt_all = 1; + break; + case OPT_REGEXP: + opt_match = option; + match_flags |= JIM_OPT_END; + break; + case OPT_COMMAND: + if (i >= argc - 2) { + goto wrongargs; + } + commandObj = argv[++i]; + + case OPT_EXACT: + case OPT_GLOB: + opt_match = option; + break; + case OPT_INDEX: + if (i >= argc - 2) { + goto wrongargs; + } + indexObj = argv[++i]; + break; + case OPT_STRIDE: + if (i >= argc - 2) { + goto wrongargs; + } + if (Jim_GetLong(interp, argv[++i], &stride) != JIM_OK) { + return JIM_ERR; + } + if (stride < 1) { + Jim_SetResultString(interp, "stride length must be at least 1", -1); + return JIM_ERR; + } + break; + } + } + + argc -= i; + if (argc < 2) { + goto wrongargs; + } + argv += i; + + listlen = Jim_ListLength(interp, argv[0]); + if (listlen % stride) { + Jim_SetResultString(interp, "list size must be a multiple of the stride length", -1); + return JIM_ERR; + } + + if (opt_all) { + listObjPtr = Jim_NewListObj(interp, NULL, 0); + } + if (opt_match == OPT_REGEXP) { + commandObj = Jim_NewStringObj(interp, "regexp", -1); + } + if (commandObj) { + Jim_IncrRefCount(commandObj); + } + + for (i = 0; i < listlen; i += stride) { + int eq = 0; + Jim_Obj *searchListObj; + Jim_Obj *objPtr; + int offset; + + if (indexObj) { + int indexlen = Jim_ListLength(interp, indexObj); + if (stride == 1) { + searchListObj = Jim_ListGetIndex(interp, argv[0], i); + } + else { + searchListObj = Jim_NewListObj(interp, argv[0]->internalRep.listValue.ele + i, stride); + } + Jim_IncrRefCount(searchListObj); + rc = Jim_ListIndices(interp, searchListObj, indexObj->internalRep.listValue.ele, indexlen, &objPtr, JIM_ERRMSG); + if (rc != JIM_OK) { + Jim_DecrRefCount(interp, searchListObj); + rc = JIM_ERR; + goto done; + } + + offset = 0; + } + else { + + searchListObj = argv[0]; + offset = i; + objPtr = Jim_ListGetIndex(interp, searchListObj, i); + Jim_IncrRefCount(searchListObj); + } + + switch (opt_match) { + case OPT_EXACT: + eq = Jim_StringCompareObj(interp, argv[1], objPtr, match_flags) == 0; + break; + + case OPT_GLOB: + eq = Jim_StringMatchObj(interp, argv[1], objPtr, match_flags); + break; + + case OPT_REGEXP: + case OPT_COMMAND: + eq = Jim_CommandMatchObj(interp, commandObj, argv[1], objPtr, match_flags); + if (eq < 0) { + Jim_DecrRefCount(interp, searchListObj); + rc = JIM_ERR; + goto done; + } + break; + } + + + if ((!opt_bool && eq == !opt_not) || (opt_bool && (eq || opt_all))) { + Jim_Obj *resultObj; + + if (opt_bool) { + resultObj = Jim_NewIntObj(interp, eq ^ opt_not); + } + else if (!opt_inline) { + resultObj = Jim_NewIntObj(interp, i); + } + else if (stride == 1) { + resultObj = objPtr; + } + else if (opt_all) { + + ListInsertElements(listObjPtr, -1, stride, + searchListObj->internalRep.listValue.ele + offset); + + resultObj = NULL; + } + else { + resultObj = Jim_NewListObj(interp, searchListObj->internalRep.listValue.ele + offset, stride); + } + + if (opt_all) { + + if (stride == 1) { + Jim_ListAppendElement(interp, listObjPtr, resultObj); + } + } + else { + Jim_SetResult(interp, resultObj); + Jim_DecrRefCount(interp, searchListObj); + goto done; + } + } + Jim_DecrRefCount(interp, searchListObj); + } + + if (opt_all) { + Jim_SetResult(interp, listObjPtr); + listObjPtr = NULL; + } + else { + + if (opt_bool) { + Jim_SetResultBool(interp, opt_not); + } + else if (!opt_inline) { + Jim_SetResultInt(interp, -1); + } + } + + done: + if (listObjPtr) { + Jim_FreeNewObj(interp, listObjPtr); + } + if (commandObj) { + Jim_DecrRefCount(interp, commandObj); + } + return rc; +} + + +static int Jim_LappendCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + Jim_Obj *listObjPtr; + int new_obj = 0; + int i; + + if (argc < 2) { + Jim_WrongNumArgs(interp, 1, argv, "varName ?value value ...?"); + return JIM_ERR; + } + listObjPtr = Jim_GetVariable(interp, argv[1], JIM_UNSHARED); + if (!listObjPtr) { + + listObjPtr = Jim_NewListObj(interp, NULL, 0); + new_obj = 1; + } + else if (Jim_IsShared(listObjPtr)) { + listObjPtr = Jim_DuplicateObj(interp, listObjPtr); + new_obj = 1; + } + for (i = 2; i < argc; i++) + Jim_ListAppendElement(interp, listObjPtr, argv[i]); + if (Jim_SetVariable(interp, argv[1], listObjPtr) != JIM_OK) { + if (new_obj) + Jim_FreeNewObj(interp, listObjPtr); + return JIM_ERR; + } + Jim_SetResult(interp, listObjPtr); + return JIM_OK; +} + + +static int Jim_LinsertCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + int idx, len; + Jim_Obj *listPtr; + + if (argc < 3) { + Jim_WrongNumArgs(interp, 1, argv, "list index ?element ...?"); + return JIM_ERR; + } + listPtr = argv[1]; + if (Jim_IsShared(listPtr)) + listPtr = Jim_DuplicateObj(interp, listPtr); + if (Jim_GetIndex(interp, argv[2], &idx) != JIM_OK) + goto err; + len = Jim_ListLength(interp, listPtr); + if (idx >= len) + idx = len; + else if (idx < 0) + idx = len + idx + 1; + Jim_ListInsertElements(interp, listPtr, idx, argc - 3, &argv[3]); + Jim_SetResult(interp, listPtr); + return JIM_OK; + err: + if (listPtr != argv[1]) { + Jim_FreeNewObj(interp, listPtr); + } + return JIM_ERR; +} + + +static int Jim_LreplaceCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + int first, last, len, rangeLen; + Jim_Obj *listObj; + Jim_Obj *newListObj; + + if (argc < 4) { + Jim_WrongNumArgs(interp, 1, argv, "list first last ?element ...?"); + return JIM_ERR; + } + if (Jim_GetIndex(interp, argv[2], &first) != JIM_OK || + Jim_GetIndex(interp, argv[3], &last) != JIM_OK) { + return JIM_ERR; + } + + listObj = argv[1]; + len = Jim_ListLength(interp, listObj); + + first = JimRelToAbsIndex(len, first); + last = JimRelToAbsIndex(len, last); + JimRelToAbsRange(len, &first, &last, &rangeLen); + + + if (first > len) { + first = len; + } + + + newListObj = Jim_NewListObj(interp, listObj->internalRep.listValue.ele, first); + + + ListInsertElements(newListObj, -1, argc - 4, argv + 4); + + + ListInsertElements(newListObj, -1, len - first - rangeLen, listObj->internalRep.listValue.ele + first + rangeLen); + + Jim_SetResult(interp, newListObj); + return JIM_OK; +} + + +static int Jim_LsetCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + if (argc < 3) { + Jim_WrongNumArgs(interp, 1, argv, "listVar ?index ...? value"); + return JIM_ERR; + } + else if (argc == 3) { + + if (Jim_SetVariable(interp, argv[1], argv[2]) != JIM_OK) + return JIM_ERR; + Jim_SetResult(interp, argv[2]); + return JIM_OK; + } + return Jim_ListSetIndex(interp, argv[1], argv + 2, argc - 3, argv[argc - 1]); +} + + +static int Jim_LsortCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const argv[]) +{ + static const char * const options[] = { + "-ascii", "-nocase", "-increasing", "-decreasing", "-command", "-integer", "-real", "-index", "-unique", + "-stride", "-dictionary", NULL + }; + enum { + OPT_ASCII, OPT_NOCASE, OPT_INCREASING, OPT_DECREASING, OPT_COMMAND, OPT_INTEGER, OPT_REAL, OPT_INDEX, OPT_UNIQUE, + OPT_STRIDE, OPT_DICT + }; + Jim_Obj *resObj; + int i; + int retCode; + int shared; + long stride = 1; + Jim_Obj **elements; + int listlen; + + struct lsort_info info; + + if (argc < 2) { +wrongargs: + Jim_WrongNumArgs(interp, 1, argv, "?options? list"); + return JIM_ERR; + } + + info.type = JIM_LSORT_ASCII; + info.order = 1; + info.indexc = 0; + info.unique = 0; + info.command = NULL; + info.interp = interp; + + for (i = 1; i < (argc - 1); i++) { + int option; + + if (Jim_GetEnum(interp, argv[i], options, &option, NULL, JIM_ENUM_ABBREV | JIM_ERRMSG) + != JIM_OK) + return JIM_ERR; + switch (option) { + case OPT_ASCII: + info.type = JIM_LSORT_ASCII; + break; + case OPT_DICT: + info.type = JIM_LSORT_DICT; + break; + case OPT_NOCASE: + info.type = JIM_LSORT_NOCASE; + break; + case OPT_INTEGER: + info.type = JIM_LSORT_INTEGER; + break; + case OPT_REAL: + info.type = JIM_LSORT_REAL; + break; + case OPT_INCREASING: + info.order = 1; + break; + case OPT_DECREASING: + info.order = -1; + break; + case OPT_UNIQUE: + info.unique = 1; + break; + case OPT_COMMAND: + if (i >= (argc - 2)) { + Jim_SetResultString(interp, "\"-command\" option must be followed by comparison command", -1); + return JIM_ERR; + } + info.type = JIM_LSORT_COMMAND; + info.command = argv[i + 1]; + i++; + break; + case OPT_STRIDE: + if (i >= argc - 2) { + goto wrongargs; + } + if (Jim_GetLong(interp, argv[++i], &stride) != JIM_OK) { + return JIM_ERR; + } + if (stride < 2) { + Jim_SetResultString(interp, "stride length must be at least 2", -1); + return JIM_ERR; + } + break; + case OPT_INDEX: + if (i >= (argc - 2)) { +badindex: + Jim_SetResultString(interp, "\"-index\" option must be followed by list index", -1); + return JIM_ERR; + } + JimListGetElements(interp, argv[i + 1], &info.indexc, &info.indexv); + if (info.indexc == 0) { + goto badindex; + } + i++; + break; + } + } + resObj = argv[argc - 1]; + JimListGetElements(interp, resObj, &listlen, &elements); + if (listlen <= 1) { + + Jim_SetResult(interp, resObj); + return JIM_OK; + } + + if (stride > 1) { + Jim_Obj *tmpListObj; + int i; + + if (listlen % stride) { + Jim_SetResultString(interp, "list size must be a multiple of the stride length", -1); + return JIM_ERR; + } + + tmpListObj = Jim_NewListObj(interp, NULL, 0); + Jim_IncrRefCount(tmpListObj); + for (i = 0; i < listlen; i += stride) { + Jim_ListAppendElement(interp, tmpListObj, Jim_NewListObj(interp, elements + i, stride)); + } + retCode = ListSortElements(interp, tmpListObj, &info); + if (retCode == JIM_OK) { + resObj = Jim_NewListObj(interp, NULL, 0); + + for (i = 0; i < listlen; i += stride) { + Jim_ListAppendList(interp, resObj, Jim_ListGetIndex(interp, tmpListObj, i / stride)); + } + Jim_SetResult(interp, resObj); + } + Jim_DecrRefCount(interp, tmpListObj); + } + else { + if ((shared = Jim_IsShared(resObj))) { + resObj = Jim_DuplicateObj(interp, resObj); + } + retCode = ListSortElements(interp, resObj, &info); + if (retCode == JIM_OK) { + Jim_SetResult(interp, resObj); + } + else if (shared) { + Jim_FreeNewObj(interp, resObj); + } + } + return retCode; +} + + +static int Jim_AppendCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + Jim_Obj *stringObjPtr; + int i; + + if (argc < 2) { + Jim_WrongNumArgs(interp, 1, argv, "varName ?value ...?"); + return JIM_ERR; + } + if (argc == 2) { + stringObjPtr = Jim_GetVariable(interp, argv[1], JIM_ERRMSG); + if (!stringObjPtr) + return JIM_ERR; + } + else { + int new_obj = 0; + stringObjPtr = Jim_GetVariable(interp, argv[1], JIM_UNSHARED); + if (!stringObjPtr) { + + stringObjPtr = Jim_NewEmptyStringObj(interp); + new_obj = 1; + } + else if (Jim_IsShared(stringObjPtr)) { + new_obj = 1; + stringObjPtr = Jim_DuplicateObj(interp, stringObjPtr); + } + for (i = 2; i < argc; i++) { + Jim_AppendObj(interp, stringObjPtr, argv[i]); + } + if (Jim_SetVariable(interp, argv[1], stringObjPtr) != JIM_OK) { + if (new_obj) { + Jim_FreeNewObj(interp, stringObjPtr); + } + return JIM_ERR; + } + } + Jim_SetResult(interp, stringObjPtr); + return JIM_OK; +} + + + + + +static int Jim_EvalCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + int rc; + + if (argc < 2) { + Jim_WrongNumArgs(interp, 1, argv, "arg ?arg ...?"); + return JIM_ERR; + } + + if (argc == 2) { + rc = Jim_EvalObj(interp, argv[1]); + } + else { + rc = Jim_EvalObj(interp, Jim_ConcatObj(interp, argc - 1, argv + 1)); + } + + return rc; +} + + +static int Jim_UplevelCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + if (argc >= 2) { + int retcode; + Jim_CallFrame *savedCallFrame, *targetCallFrame; + const char *str; + + + savedCallFrame = interp->framePtr; + + + str = Jim_String(argv[1]); + if ((str[0] >= '0' && str[0] <= '9') || str[0] == '#') { + targetCallFrame = Jim_GetCallFrameByLevel(interp, argv[1]); + argc--; + argv++; + } + else { + targetCallFrame = Jim_GetCallFrameByLevel(interp, NULL); + } + if (targetCallFrame == NULL) { + return JIM_ERR; + } + if (argc < 2) { + Jim_WrongNumArgs(interp, 1, argv - 1, "?level? command ?arg ...?"); + return JIM_ERR; + } + + interp->framePtr = targetCallFrame; + if (argc == 2) { + retcode = Jim_EvalObj(interp, argv[1]); + } + else { + retcode = Jim_EvalObj(interp, Jim_ConcatObj(interp, argc - 1, argv + 1)); + } + interp->framePtr = savedCallFrame; + return retcode; + } + else { + Jim_WrongNumArgs(interp, 1, argv, "?level? command ?arg ...?"); + return JIM_ERR; + } +} + + +static int Jim_ExprCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + int retcode; + + if (argc == 2) { + retcode = Jim_EvalExpression(interp, argv[1]); + } +#ifndef JIM_COMPAT + else { + Jim_WrongNumArgs(interp, 1, argv, "expression"); + retcode = JIM_ERR; + } +#else + else if (argc > 2) { + Jim_Obj *objPtr; + + objPtr = Jim_ConcatObj(interp, argc - 1, argv + 1); + Jim_IncrRefCount(objPtr); + retcode = Jim_EvalExpression(interp, objPtr); + Jim_DecrRefCount(interp, objPtr); + } + else { + Jim_WrongNumArgs(interp, 1, argv, "expression ?...?"); + return JIM_ERR; + } +#endif + return retcode; +} + +static int JimBreakContinueHelper(Jim_Interp *interp, int argc, Jim_Obj *const *argv, int retcode) +{ + if (argc != 1 && argc != 2) { + Jim_WrongNumArgs(interp, 1, argv, "?level?"); + return JIM_ERR; + } + if (argc == 2) { + long level; + int ret = Jim_GetLong(interp, argv[1], &level); + if (ret != JIM_OK) { + return ret; + } + interp->break_level = level; + } + return retcode; +} + + +static int Jim_BreakCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + return JimBreakContinueHelper(interp, argc, argv, JIM_BREAK); +} + + +static int Jim_ContinueCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + return JimBreakContinueHelper(interp, argc, argv, JIM_CONTINUE); +} + + +static int Jim_StacktraceCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + Jim_Obj *listObj; + int i; + jim_wide skip = 0; + jim_wide last = 0; + + if (argc > 1) { + if (Jim_GetWideExpr(interp, argv[1], &skip) != JIM_OK) { + return JIM_ERR; + } + } + if (argc > 2) { + if (Jim_GetWideExpr(interp, argv[2], &last) != JIM_OK) { + return JIM_ERR; + } + } + + listObj = Jim_NewListObj(interp, NULL, 0); + for (i = skip; i <= interp->procLevel; i++) { + Jim_EvalFrame *frame = JimGetEvalFrameByProcLevel(interp, -i); + if (frame->procLevel < last) { + break; + } + JimAddStackFrame(interp, frame, listObj); + } + Jim_SetResult(interp, listObj); + return JIM_OK; +} + + +static int Jim_ReturnCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + int i; + Jim_Obj *stackTraceObj = NULL; + Jim_Obj *errorCodeObj = NULL; + int returnCode = JIM_OK; + long level = 1; + + for (i = 1; i < argc - 1; i += 2) { + if (Jim_CompareStringImmediate(interp, argv[i], "-code")) { + if (Jim_GetReturnCode(interp, argv[i + 1], &returnCode) == JIM_ERR) { + return JIM_ERR; + } + } + else if (Jim_CompareStringImmediate(interp, argv[i], "-errorinfo")) { + stackTraceObj = argv[i + 1]; + } + else if (Jim_CompareStringImmediate(interp, argv[i], "-errorcode")) { + errorCodeObj = argv[i + 1]; + } + else if (Jim_CompareStringImmediate(interp, argv[i], "-level")) { + if (Jim_GetLong(interp, argv[i + 1], &level) != JIM_OK || level < 0) { + Jim_SetResultFormatted(interp, "bad level \"%#s\"", argv[i + 1]); + return JIM_ERR; + } + } + else { + break; + } + } + + if (i != argc - 1 && i != argc) { + Jim_WrongNumArgs(interp, 1, argv, + "?-code code? ?-errorinfo stacktrace? ?-level level? ?result?"); + } + + + if (stackTraceObj && returnCode == JIM_ERR) { + JimSetStackTrace(interp, stackTraceObj); + } + + if (errorCodeObj && returnCode == JIM_ERR) { + Jim_SetGlobalVariableStr(interp, "errorCode", errorCodeObj); + } + interp->returnCode = returnCode; + interp->returnLevel = level; + + if (i == argc - 1) { + Jim_SetResult(interp, argv[i]); + } + return level == 0 ? returnCode : JIM_RETURN; +} + + +static int Jim_TailcallCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + if (interp->framePtr->level == 0) { + Jim_SetResultString(interp, "tailcall can only be called from a proc or lambda", -1); + return JIM_ERR; + } + else if (argc >= 2) { + + Jim_CallFrame *cf = interp->framePtr->parent; + + Jim_Cmd *cmdPtr = Jim_GetCommand(interp, argv[1], JIM_ERRMSG); + if (cmdPtr == NULL) { + return JIM_ERR; + } + + JimPanic((cf->tailcallCmd != NULL, "Already have a tailcallCmd")); + + + JimIncrCmdRefCount(cmdPtr); + cf->tailcallCmd = cmdPtr; + + + JimPanic((cf->tailcallObj != NULL, "Already have a tailcallobj")); + + cf->tailcallObj = Jim_NewListObj(interp, argv + 1, argc - 1); + Jim_IncrRefCount(cf->tailcallObj); + + + return JIM_EVAL; + } + return JIM_OK; +} + +static int JimAliasCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + Jim_Obj *cmdList; + Jim_Obj *prefixListObj = Jim_CmdPrivData(interp); + + + cmdList = Jim_DuplicateObj(interp, prefixListObj); + Jim_ListInsertElements(interp, cmdList, Jim_ListLength(interp, cmdList), argc - 1, argv + 1); + + return JimEvalObjList(interp, cmdList); +} + +static void JimAliasCmdDelete(Jim_Interp *interp, void *privData) +{ + Jim_Obj *prefixListObj = privData; + Jim_DecrRefCount(interp, prefixListObj); +} + +static int Jim_AliasCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + Jim_Obj *prefixListObj; + + if (argc < 3) { + Jim_WrongNumArgs(interp, 1, argv, "newname command ?args ...?"); + return JIM_ERR; + } + + prefixListObj = Jim_NewListObj(interp, argv + 2, argc - 2); + Jim_IncrRefCount(prefixListObj); + Jim_SetResult(interp, argv[1]); + + return Jim_CreateCommandObj(interp, argv[1], JimAliasCmd, prefixListObj, JimAliasCmdDelete); +} + + +static int Jim_ProcCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + Jim_Cmd *cmd; + + if (argc != 4 && argc != 5) { + Jim_WrongNumArgs(interp, 1, argv, "name arglist ?statics? body"); + return JIM_ERR; + } + + if (argc == 4) { + cmd = JimCreateProcedureCmd(interp, argv[2], NULL, argv[3], NULL); + } + else { + cmd = JimCreateProcedureCmd(interp, argv[2], argv[3], argv[4], NULL); + } + + if (cmd) { + + Jim_Obj *nameObjPtr = JimQualifyName(interp, argv[1]); + JimCreateCommand(interp, nameObjPtr, cmd); + + + JimUpdateProcNamespace(interp, cmd, nameObjPtr); + Jim_DecrRefCount(interp, nameObjPtr); + + + Jim_SetResult(interp, argv[1]); + return JIM_OK; + } + return JIM_ERR; +} + + +static int Jim_XtraceCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + if (argc != 2) { + Jim_WrongNumArgs(interp, 1, argv, "callback"); + return JIM_ERR; + } + + if (interp->traceCmdObj) { + Jim_DecrRefCount(interp, interp->traceCmdObj); + interp->traceCmdObj = NULL; + } + + if (Jim_Length(argv[1])) { + + interp->traceCmdObj = argv[1]; + Jim_IncrRefCount(interp->traceCmdObj); + } + return JIM_OK; +} + + +static int Jim_LocalCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + int retcode; + + if (argc < 2) { + Jim_WrongNumArgs(interp, 1, argv, "cmd ?args ...?"); + return JIM_ERR; + } + + + interp->local++; + retcode = Jim_EvalObjVector(interp, argc - 1, argv + 1); + interp->local--; + + + + if (retcode == 0) { + Jim_Obj *cmdNameObj = Jim_GetResult(interp); + + if (Jim_GetCommand(interp, cmdNameObj, JIM_ERRMSG) == NULL) { + return JIM_ERR; + } + if (interp->framePtr->localCommands == NULL) { + interp->framePtr->localCommands = Jim_Alloc(sizeof(*interp->framePtr->localCommands)); + Jim_InitStack(interp->framePtr->localCommands); + } + Jim_IncrRefCount(cmdNameObj); + Jim_StackPush(interp->framePtr->localCommands, cmdNameObj); + } + + return retcode; +} + + +static int Jim_UpcallCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + if (argc < 2) { + Jim_WrongNumArgs(interp, 1, argv, "cmd ?args ...?"); + return JIM_ERR; + } + else { + int retcode; + + Jim_Cmd *cmdPtr = Jim_GetCommand(interp, argv[1], JIM_ERRMSG); + if (cmdPtr == NULL || !cmdPtr->isproc || !cmdPtr->prevCmd) { + Jim_SetResultFormatted(interp, "no previous command: \"%#s\"", argv[1]); + return JIM_ERR; + } + + cmdPtr->u.proc.upcall++; + JimIncrCmdRefCount(cmdPtr); + + + retcode = Jim_EvalObjVector(interp, argc - 1, argv + 1); + + + cmdPtr->u.proc.upcall--; + JimDecrCmdRefCount(interp, cmdPtr); + + return retcode; + } +} + + +static int Jim_ApplyCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + if (argc < 2) { + Jim_WrongNumArgs(interp, 1, argv, "lambdaExpr ?arg ...?"); + return JIM_ERR; + } + else { + int ret; + Jim_Cmd *cmd; + Jim_Obj *argListObjPtr; + Jim_Obj *bodyObjPtr; + Jim_Obj *nsObj = NULL; + Jim_Obj **nargv; + + int len = Jim_ListLength(interp, argv[1]); + if (len != 2 && len != 3) { + Jim_SetResultFormatted(interp, "can't interpret \"%#s\" as a lambda expression", argv[1]); + return JIM_ERR; + } + + if (len == 3) { +#ifdef jim_ext_namespace + + nsObj = Jim_ListGetIndex(interp, argv[1], 2); +#else + Jim_SetResultString(interp, "namespaces not enabled", -1); + return JIM_ERR; +#endif + } + argListObjPtr = Jim_ListGetIndex(interp, argv[1], 0); + bodyObjPtr = Jim_ListGetIndex(interp, argv[1], 1); + + cmd = JimCreateProcedureCmd(interp, argListObjPtr, NULL, bodyObjPtr, nsObj); + + if (cmd) { + + nargv = Jim_Alloc((argc - 2 + 1) * sizeof(*nargv)); + nargv[0] = Jim_NewStringObj(interp, "apply lambdaExpr", -1); + Jim_IncrRefCount(nargv[0]); + memcpy(&nargv[1], argv + 2, (argc - 2) * sizeof(*nargv)); + ret = JimCallProcedure(interp, cmd, argc - 2 + 1, nargv); + Jim_DecrRefCount(interp, nargv[0]); + Jim_Free(nargv); + + JimDecrCmdRefCount(interp, cmd); + return ret; + } + return JIM_ERR; + } +} + + + +static int Jim_ConcatCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + Jim_SetResult(interp, Jim_ConcatObj(interp, argc - 1, argv + 1)); + return JIM_OK; +} + + +static int Jim_UpvarCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + int i; + Jim_CallFrame *targetCallFrame; + + + if (argc > 3 && (argc % 2 == 0)) { + targetCallFrame = Jim_GetCallFrameByLevel(interp, argv[1]); + argc--; + argv++; + } + else { + targetCallFrame = Jim_GetCallFrameByLevel(interp, NULL); + } + if (targetCallFrame == NULL) { + return JIM_ERR; + } + + + if (argc < 3) { + Jim_WrongNumArgs(interp, 1, argv, "?level? otherVar localVar ?otherVar localVar ...?"); + return JIM_ERR; + } + + + for (i = 1; i < argc; i += 2) { + if (Jim_SetVariableLink(interp, argv[i + 1], argv[i], targetCallFrame) != JIM_OK) + return JIM_ERR; + } + return JIM_OK; +} + + +static int Jim_GlobalCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + int i; + + if (argc < 2) { + Jim_WrongNumArgs(interp, 1, argv, "varName ?varName ...?"); + return JIM_ERR; + } + + if (interp->framePtr->level == 0) + return JIM_OK; + for (i = 1; i < argc; i++) { + + const char *name = Jim_String(argv[i]); + if (name[0] != ':' || name[1] != ':') { + if (Jim_SetVariableLink(interp, argv[i], argv[i], interp->topFramePtr) != JIM_OK) + return JIM_ERR; + } + } + return JIM_OK; +} + +static Jim_Obj *JimStringMap(Jim_Interp *interp, Jim_Obj *mapListObjPtr, + Jim_Obj *objPtr, int nocase) +{ + int numMaps; + const char *str, *noMatchStart = NULL; + int strLen, i; + Jim_Obj *resultObjPtr; + + numMaps = Jim_ListLength(interp, mapListObjPtr); + if (numMaps % 2) { + Jim_SetResultString(interp, "list must contain an even number of elements", -1); + return NULL; + } + + str = Jim_String(objPtr); + strLen = Jim_Utf8Length(interp, objPtr); + + + resultObjPtr = Jim_NewStringObj(interp, "", 0); + while (strLen) { + for (i = 0; i < numMaps; i += 2) { + Jim_Obj *eachObjPtr; + const char *k; + int kl; + + eachObjPtr = Jim_ListGetIndex(interp, mapListObjPtr, i); + k = Jim_String(eachObjPtr); + kl = Jim_Utf8Length(interp, eachObjPtr); + + if (strLen >= kl && kl) { + int rc; + rc = JimStringCompareUtf8(str, kl, k, kl, nocase); + if (rc == 0) { + if (noMatchStart) { + Jim_AppendString(interp, resultObjPtr, noMatchStart, str - noMatchStart); + noMatchStart = NULL; + } + Jim_AppendObj(interp, resultObjPtr, Jim_ListGetIndex(interp, mapListObjPtr, i + 1)); + str += utf8_index(str, kl); + strLen -= kl; + break; + } + } + } + if (i == numMaps) { + int c; + if (noMatchStart == NULL) + noMatchStart = str; + str += utf8_tounicode(str, &c); + strLen--; + } + } + if (noMatchStart) { + Jim_AppendString(interp, resultObjPtr, noMatchStart, str - noMatchStart); + } + return resultObjPtr; +} + + +static int Jim_StringCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + int len; + int opt_case = 1; + int option; + static const char * const nocase_options[] = { + "-nocase", NULL + }; + static const char * const nocase_length_options[] = { + "-nocase", "-length", NULL + }; + + enum { + OPT_BYTELENGTH, + OPT_BYTERANGE, + OPT_CAT, + OPT_COMPARE, + OPT_EQUAL, + OPT_FIRST, + OPT_INDEX, + OPT_IS, + OPT_LAST, + OPT_LENGTH, + OPT_MAP, + OPT_MATCH, + OPT_RANGE, + OPT_REPEAT, + OPT_REPLACE, + OPT_REVERSE, + OPT_TOLOWER, + OPT_TOTITLE, + OPT_TOUPPER, + OPT_TRIM, + OPT_TRIMLEFT, + OPT_TRIMRIGHT, + OPT_COUNT + }; + static const jim_subcmd_type cmds[OPT_COUNT + 1] = { + JIM_DEF_SUBCMD("bytelength", "string", 1, 1), + JIM_DEF_SUBCMD("byterange", "string first last", 3, 3), + JIM_DEF_SUBCMD("cat", "?...?", 0, -1), + JIM_DEF_SUBCMD("compare", "?-nocase? ?-length int? string1 string2", 2, 5), + JIM_DEF_SUBCMD("equal", "?-nocase? ?-length int? string1 string2", 2, 5), + JIM_DEF_SUBCMD("first", "subString string ?index?", 2, 3), + JIM_DEF_SUBCMD("index", "string index", 2, 2), + JIM_DEF_SUBCMD("is", "class ?-strict? str", 2, 3), + JIM_DEF_SUBCMD("last", "subString string ?index?", 2, 3), + JIM_DEF_SUBCMD("length","string", 1, 1), + JIM_DEF_SUBCMD("map", "?-nocase? mapList string", 2, 3), + JIM_DEF_SUBCMD("match", "?-nocase? pattern string", 2, 3), + JIM_DEF_SUBCMD("range", "string first last", 3, 3), + JIM_DEF_SUBCMD("repeat", "string count", 2, 2), + JIM_DEF_SUBCMD("replace", "string first last ?string?", 3, 4), + JIM_DEF_SUBCMD("reverse", "string", 1, 1), + JIM_DEF_SUBCMD("tolower", "string", 1, 1), + JIM_DEF_SUBCMD("totitle", "string", 1, 1), + JIM_DEF_SUBCMD("toupper", "string", 1, 1), + JIM_DEF_SUBCMD("trim", "string ?trimchars?", 1, 2), + JIM_DEF_SUBCMD("trimleft", "string ?trimchars?", 1, 2), + JIM_DEF_SUBCMD("trimright", "string ?trimchars?", 1, 2), + { NULL } + }; + const jim_subcmd_type *ct = Jim_ParseSubCmd(interp, cmds, argc, argv); + if (!ct) { + return JIM_ERR; + } + if (ct->function) { + + return ct->function(interp, argc, argv); + } + + option = ct - cmds; + + switch (option) { + case OPT_LENGTH: + Jim_SetResultInt(interp, Jim_Utf8Length(interp, argv[2])); + return JIM_OK; + + case OPT_BYTELENGTH: + Jim_SetResultInt(interp, Jim_Length(argv[2])); + return JIM_OK; + + case OPT_CAT:{ + Jim_Obj *objPtr; + if (argc == 3) { + + objPtr = argv[2]; + } + else { + int i; + + objPtr = Jim_NewStringObj(interp, "", 0); + + for (i = 2; i < argc; i++) { + Jim_AppendObj(interp, objPtr, argv[i]); + } + } + Jim_SetResult(interp, objPtr); + return JIM_OK; + } + + case OPT_COMPARE: + case OPT_EQUAL: + { + + long opt_length = -1; + int n = argc - 4; + int i = 2; + while (n > 0) { + int subopt; + if (Jim_GetEnum(interp, argv[i++], nocase_length_options, &subopt, NULL, + JIM_ENUM_ABBREV) != JIM_OK) { +badcompareargs: + Jim_SubCmdArgError(interp, ct, argv[0]); + return JIM_ERR; + } + if (subopt == 0) { + + opt_case = 0; + n--; + } + else { + + if (n < 2) { + goto badcompareargs; + } + if (Jim_GetLong(interp, argv[i++], &opt_length) != JIM_OK) { + return JIM_ERR; + } + n -= 2; + } + } + if (n) { + goto badcompareargs; + } + argv += argc - 2; + if (opt_length < 0 && option != OPT_COMPARE && opt_case) { + + Jim_SetResultBool(interp, Jim_StringEqObj(argv[0], argv[1])); + } + else { + const char *s1 = Jim_String(argv[0]); + int l1 = Jim_Utf8Length(interp, argv[0]); + const char *s2 = Jim_String(argv[1]); + int l2 = Jim_Utf8Length(interp, argv[1]); + if (opt_length >= 0) { + if (l1 > opt_length) { + l1 = opt_length; + } + if (l2 > opt_length) { + l2 = opt_length; + } + } + n = JimStringCompareUtf8(s1, l1, s2, l2, !opt_case); + Jim_SetResultInt(interp, option == OPT_COMPARE ? n : n == 0); + } + return JIM_OK; + } + + case OPT_MATCH: + if (argc != 4 && + (argc != 5 || + Jim_GetEnum(interp, argv[2], nocase_options, &opt_case, NULL, + JIM_ENUM_ABBREV) != JIM_OK)) { + Jim_WrongNumArgs(interp, 2, argv, "?-nocase? pattern string"); + return JIM_ERR; + } + if (opt_case == 0) { + argv++; + } + Jim_SetResultBool(interp, Jim_StringMatchObj(interp, argv[2], argv[3], !opt_case)); + return JIM_OK; + + case OPT_MAP:{ + Jim_Obj *objPtr; + + if (argc != 4 && + (argc != 5 || + Jim_GetEnum(interp, argv[2], nocase_options, &opt_case, NULL, + JIM_ENUM_ABBREV) != JIM_OK)) { + Jim_WrongNumArgs(interp, 2, argv, "?-nocase? mapList string"); + return JIM_ERR; + } + + if (opt_case == 0) { + argv++; + } + objPtr = JimStringMap(interp, argv[2], argv[3], !opt_case); + if (objPtr == NULL) { + return JIM_ERR; + } + Jim_SetResult(interp, objPtr); + return JIM_OK; + } + + case OPT_RANGE:{ + Jim_Obj *objPtr = Jim_StringRangeObj(interp, argv[2], argv[3], argv[4]); + if (objPtr == NULL) { + return JIM_ERR; + } + Jim_SetResult(interp, objPtr); + return JIM_OK; + } + + case OPT_BYTERANGE:{ + Jim_Obj *objPtr = Jim_StringByteRangeObj(interp, argv[2], argv[3], argv[4]); + if (objPtr == NULL) { + return JIM_ERR; + } + Jim_SetResult(interp, objPtr); + return JIM_OK; + } + + case OPT_REPLACE:{ + Jim_Obj *objPtr = JimStringReplaceObj(interp, argv[2], argv[3], argv[4], argc == 6 ? argv[5] : NULL); + if (objPtr == NULL) { + return JIM_ERR; + } + Jim_SetResult(interp, objPtr); + return JIM_OK; + } + + + case OPT_REPEAT:{ + Jim_Obj *objPtr; + jim_wide count; + + if (Jim_GetWideExpr(interp, argv[3], &count) != JIM_OK) { + return JIM_ERR; + } + objPtr = Jim_NewStringObj(interp, "", 0); + if (count > 0) { + while (count--) { + Jim_AppendObj(interp, objPtr, argv[2]); + } + } + Jim_SetResult(interp, objPtr); + return JIM_OK; + } + + case OPT_REVERSE:{ + char *buf, *p; + const char *str; + int i; + + str = Jim_GetString(argv[2], &len); + buf = Jim_Alloc(len + 1); + assert(buf); + p = buf + len; + *p = 0; + for (i = 0; i < len; ) { + int c; + int l = utf8_tounicode(str, &c); + memcpy(p - l, str, l); + p -= l; + i += l; + str += l; + } + Jim_SetResult(interp, Jim_NewStringObjNoAlloc(interp, buf, len)); + return JIM_OK; + } + + case OPT_INDEX:{ + int idx; + const char *str; + + if (Jim_GetIndex(interp, argv[3], &idx) != JIM_OK) { + return JIM_ERR; + } + str = Jim_String(argv[2]); + len = Jim_Utf8Length(interp, argv[2]); + idx = JimRelToAbsIndex(len, idx); + if (idx < 0 || idx >= len || str == NULL) { + Jim_SetResultString(interp, "", 0); + } + else if (len == Jim_Length(argv[2])) { + + Jim_SetResultString(interp, str + idx, 1); + } + else { + int c; + int i = utf8_index(str, idx); + Jim_SetResultString(interp, str + i, utf8_tounicode(str + i, &c)); + } + return JIM_OK; + } + + case OPT_FIRST: + case OPT_LAST:{ + int idx = 0, l1, l2; + const char *s1, *s2; + + s1 = Jim_String(argv[2]); + s2 = Jim_String(argv[3]); + l1 = Jim_Utf8Length(interp, argv[2]); + l2 = Jim_Utf8Length(interp, argv[3]); + if (argc == 5) { + if (Jim_GetIndex(interp, argv[4], &idx) != JIM_OK) { + return JIM_ERR; + } + idx = JimRelToAbsIndex(l2, idx); + if (idx < 0) { + idx = 0; + } + } + else if (option == OPT_LAST) { + idx = l2; + } + if (option == OPT_FIRST) { + Jim_SetResultInt(interp, JimStringFirst(s1, l1, s2, l2, idx)); + } + else { +#ifdef JIM_UTF8 + Jim_SetResultInt(interp, JimStringLastUtf8(s1, l1, s2, idx)); +#else + Jim_SetResultInt(interp, JimStringLast(s1, l1, s2, idx)); +#endif + } + return JIM_OK; + } + + case OPT_TRIM: + Jim_SetResult(interp, JimStringTrim(interp, argv[2], argc == 4 ? argv[3] : NULL)); + return JIM_OK; + case OPT_TRIMLEFT: + Jim_SetResult(interp, JimStringTrimLeft(interp, argv[2], argc == 4 ? argv[3] : NULL)); + return JIM_OK; + case OPT_TRIMRIGHT:{ + Jim_SetResult(interp, JimStringTrimRight(interp, argv[2], argc == 4 ? argv[3] : NULL)); + return JIM_OK; + } + + case OPT_TOLOWER: + Jim_SetResult(interp, JimStringToLower(interp, argv[2])); + return JIM_OK; + case OPT_TOUPPER: + Jim_SetResult(interp, JimStringToUpper(interp, argv[2])); + return JIM_OK; + case OPT_TOTITLE: + Jim_SetResult(interp, JimStringToTitle(interp, argv[2])); + return JIM_OK; + + case OPT_IS: + if (argc == 5 && !Jim_CompareStringImmediate(interp, argv[3], "-strict")) { + Jim_SubCmdArgError(interp, ct, argv[0]); + return JIM_ERR; + } + return JimStringIs(interp, argv[argc - 1], argv[2], argc == 5); + } + return JIM_OK; +} + + +static int Jim_TimeCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + long i, count = 1; + jim_wide start, elapsed; + + if (argc < 2) { + Jim_WrongNumArgs(interp, 1, argv, "script ?count?"); + return JIM_ERR; + } + if (argc == 3) { + if (Jim_GetLong(interp, argv[2], &count) != JIM_OK) + return JIM_ERR; + } + if (count < 0) + return JIM_OK; + i = count; + start = Jim_GetTimeUsec(CLOCK_MONOTONIC_RAW); + while (i-- > 0) { + int retval; + + retval = Jim_EvalObj(interp, argv[1]); + if (retval != JIM_OK) { + return retval; + } + } + elapsed = Jim_GetTimeUsec(CLOCK_MONOTONIC_RAW) - start; + if (elapsed < count * 10) { + Jim_SetResult(interp, Jim_NewDoubleObj(interp, elapsed * 1.0 / count)); + } + else { + Jim_SetResultInt(interp, count == 0 ? 0 : elapsed / count); + } + Jim_AppendString(interp, Jim_GetResult(interp)," microseconds per iteration", -1); + return JIM_OK; +} + + +static int Jim_TimeRateCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + long us = 0; + jim_wide start, delta, overhead; + Jim_Obj *objPtr; + double us_per_iter; + int count; + int n; + + if (argc < 2) { + Jim_WrongNumArgs(interp, 1, argv, "script ?milliseconds?"); + return JIM_ERR; + } + if (argc == 3) { + if (Jim_GetLong(interp, argv[2], &us) != JIM_OK) + return JIM_ERR; + us *= 1000; + } + if (us < 1) { + + us = 1000 * 1000; + } + + + start = Jim_GetTimeUsec(CLOCK_MONOTONIC_RAW); + count = 0; + do { + int retval = Jim_EvalObj(interp, argv[1]); + delta = Jim_GetTimeUsec(CLOCK_MONOTONIC_RAW) - start; + if (retval != JIM_OK) { + return retval; + } + count++; + } while (delta < us); + + + start = Jim_GetTimeUsec(CLOCK_MONOTONIC_RAW); + n = 0; + do { + int retval = Jim_EvalObj(interp, interp->nullScriptObj); + overhead = Jim_GetTimeUsec(CLOCK_MONOTONIC_RAW) - start; + if (retval != JIM_OK) { + return retval; + } + n++; + } while (n < count); + + delta -= overhead; + + us_per_iter = (double)delta / count; + objPtr = Jim_NewListObj(interp, NULL, 0); + + Jim_ListAppendElement(interp, objPtr, Jim_NewStringObj(interp, "us_per_iter", -1)); + Jim_ListAppendElement(interp, objPtr, Jim_NewDoubleObj(interp, us_per_iter)); + Jim_ListAppendElement(interp, objPtr, Jim_NewStringObj(interp, "iters_per_sec", -1)); + Jim_ListAppendElement(interp, objPtr, Jim_NewDoubleObj(interp, 1e6 / us_per_iter)); + Jim_ListAppendElement(interp, objPtr, Jim_NewStringObj(interp, "count", -1)); + Jim_ListAppendElement(interp, objPtr, Jim_NewIntObj(interp, count)); + Jim_ListAppendElement(interp, objPtr, Jim_NewStringObj(interp, "elapsed_us", -1)); + Jim_ListAppendElement(interp, objPtr, Jim_NewIntObj(interp, delta)); + Jim_SetResult(interp, objPtr); + return JIM_OK; +} + + +static int Jim_ExitCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + long exitCode = 0; + + if (argc > 2) { + Jim_WrongNumArgs(interp, 1, argv, "?exitCode?"); + return JIM_ERR; + } + if (argc == 2) { + if (Jim_GetLong(interp, argv[1], &exitCode) != JIM_OK) + return JIM_ERR; + Jim_SetResult(interp, argv[1]); + } + interp->exitCode = exitCode; + return JIM_EXIT; +} + +static int JimMatchReturnCodes(Jim_Interp *interp, Jim_Obj *retcodeListObj, int rc) +{ + int len = Jim_ListLength(interp, retcodeListObj); + int i; + for (i = 0; i < len; i++) { + int returncode; + if (Jim_GetReturnCode(interp, Jim_ListGetIndex(interp, retcodeListObj, i), &returncode) != JIM_OK) { + return JIM_ERR; + } + if (rc == returncode) { + return JIM_OK; + } + } + return -1; +} + + +static int JimCatchTryHelper(Jim_Interp *interp, int istry, int argc, Jim_Obj *const *argv) +{ + static const char * const wrongargs_catchtry[2] = { + "?-?no?code ... --? script ?resultVarName? ?optionVarName?", + "?-?no?code ... --? script ?on|trap codes vars script? ... ?finally script?" + }; + int exitCode = 0; + int i; + int sig = 0; + int ok; + Jim_Obj *finallyScriptObj = NULL; + Jim_Obj *msgVarObj = NULL; + Jim_Obj *optsVarObj = NULL; + Jim_Obj *handlerScriptObj = NULL; + Jim_Obj *errorCodeObj; + int idx; + + + jim_wide ignore_mask = (1 << JIM_EXIT) | (1 << JIM_EVAL) | (1 << JIM_SIGNAL); + static const int max_ignore_code = sizeof(ignore_mask) * 8; + + JimPanic((istry != 0 && istry != 1, "wrong args to JimCatchTryHelper")); + + Jim_SetGlobalVariableStr(interp, "errorCode", Jim_NewStringObj(interp, "NONE", -1)); + + for (i = 1; i < argc - 1; i++) { + const char *arg = Jim_String(argv[i]); + jim_wide option; + int ignore; + + + if (strcmp(arg, "--") == 0) { + i++; + break; + } + if (*arg != '-') { + break; + } + + if (strncmp(arg, "-no", 3) == 0) { + arg += 3; + ignore = 1; + } + else { + arg++; + ignore = 0; + } + + if (Jim_StringToWide(arg, &option, 10) != JIM_OK) { + option = -1; + } + if (option < 0) { + option = Jim_FindByName(arg, jimReturnCodes, jimReturnCodesSize); + } + if (option < 0) { + goto wrongargs; + } + + if (ignore) { + ignore_mask |= ((jim_wide)1 << option); + } + else { + ignore_mask &= (~((jim_wide)1 << option)); + } + } + + idx = i; + + if (argc - idx < 1) { +wrongargs: + Jim_WrongNumArgs(interp, 1, argv, wrongargs_catchtry[istry]); + return JIM_ERR; + } + + if ((ignore_mask & (1 << JIM_SIGNAL)) == 0) { + sig++; + } + + interp->signal_level += sig; + if (Jim_CheckSignal(interp)) { + + exitCode = JIM_SIGNAL; + } + else { + exitCode = Jim_EvalObj(interp, argv[idx]); + + interp->errorFlag = 0; + } + interp->signal_level -= sig; + + errorCodeObj = Jim_GetGlobalVariableStr(interp, "errorCode", JIM_NONE); + + idx++; + if (istry) { + while (idx < argc) { + int option; + int ret; + static const char * const try_options[] = { "on", "trap", "finally", NULL }; + enum { TRY_ON, TRY_TRAP, TRY_FINALLY, }; + + if (Jim_GetEnum(interp, argv[idx], try_options, &option, "handler", JIM_ERRMSG) != JIM_OK) { + return JIM_ERR; + } + switch (option) { + case TRY_ON: + case TRY_TRAP: + if (idx + 4 > argc) { + goto wrongargs; + } + if (option == TRY_ON) { + ret = JimMatchReturnCodes(interp, argv[idx + 1], exitCode); + if (ret > JIM_OK) { + goto wrongargs; + } + } + else if (errorCodeObj) { + int len = Jim_ListLength(interp, argv[idx + 1]); + int i; + + ret = JIM_OK; + + for (i = 0; i < len; i++) { + Jim_Obj *matchObj = Jim_ListGetIndex(interp, argv[idx + 1], i); + Jim_Obj *objPtr = Jim_ListGetIndex(interp, errorCodeObj, i); + if (Jim_StringCompareObj(interp, matchObj, objPtr, 0) != 0) { + ret = -1; + break; + } + } + } + else { + + ret = -1; + } + + if (ret == JIM_OK && handlerScriptObj == NULL) { + msgVarObj = Jim_ListGetIndex(interp, argv[idx + 2], 0); + optsVarObj = Jim_ListGetIndex(interp, argv[idx + 2], 1); + handlerScriptObj = argv[idx + 3]; + } + idx += 4; + break; + case TRY_FINALLY: + if (idx + 2 != argc) { + goto wrongargs; + } + finallyScriptObj = argv[idx + 1]; + idx += 2; + break; + } + } + } + else { + if (argc - idx >= 1) { + msgVarObj = argv[idx]; + idx++; + if (argc - idx >= 1) { + optsVarObj = argv[idx]; + idx++; + } + } + } + + + if (exitCode >= 0 && exitCode < max_ignore_code && (((unsigned jim_wide)1 << exitCode) & ignore_mask)) { + + if (finallyScriptObj) { + Jim_EvalObj(interp, finallyScriptObj); + } + return exitCode; + } + + if (sig && exitCode == JIM_SIGNAL) { + + if (interp->signal_set_result) { + interp->signal_set_result(interp, interp->sigmask); + } + else if (!istry) { + Jim_SetResultInt(interp, interp->sigmask); + } + interp->sigmask = 0; + } + + ok = 1; + if (msgVarObj && Jim_Length(msgVarObj)) { + if (Jim_SetVariable(interp, msgVarObj, Jim_GetResult(interp)) != JIM_OK) { + ok = 0; + } + } + if (ok && optsVarObj && Jim_Length(optsVarObj)) { + Jim_Obj *optListObj = Jim_NewListObj(interp, NULL, 0); + + Jim_ListAppendElement(interp, optListObj, Jim_NewStringObj(interp, "-code", -1)); + Jim_ListAppendElement(interp, optListObj, + Jim_NewIntObj(interp, exitCode == JIM_RETURN ? interp->returnCode : exitCode)); + Jim_ListAppendElement(interp, optListObj, Jim_NewStringObj(interp, "-level", -1)); + Jim_ListAppendElement(interp, optListObj, Jim_NewIntObj(interp, interp->returnLevel)); + if (exitCode == JIM_ERR) { + Jim_ListAppendElement(interp, optListObj, Jim_NewStringObj(interp, "-errorinfo", + -1)); + Jim_ListAppendElement(interp, optListObj, interp->stackTrace); + + if (errorCodeObj) { + Jim_ListAppendElement(interp, optListObj, Jim_NewStringObj(interp, "-errorcode", -1)); + Jim_ListAppendElement(interp, optListObj, errorCodeObj); + } + } + if (Jim_SetVariable(interp, optsVarObj, optListObj) != JIM_OK) { + ok = 0; + } + } + if (ok && handlerScriptObj) { + + exitCode = Jim_EvalObj(interp, handlerScriptObj); + } + + if (finallyScriptObj) { + + Jim_Obj *prevResultObj = Jim_GetResult(interp); + Jim_IncrRefCount(prevResultObj); + int ret = Jim_EvalObj(interp, finallyScriptObj); + if (ret == JIM_OK) { + Jim_SetResult(interp, prevResultObj); + } + else { + exitCode = ret; + } + Jim_DecrRefCount(interp, prevResultObj); + } + if (!istry) { + Jim_SetResultInt(interp, exitCode); + exitCode = JIM_OK; + } + return exitCode; +} + + +static int Jim_CatchCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + return JimCatchTryHelper(interp, 0, argc, argv); +} + + +static int Jim_TryCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + return JimCatchTryHelper(interp, 1, argc, argv); +} + + + +static int Jim_RenameCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + if (argc != 3) { + Jim_WrongNumArgs(interp, 1, argv, "oldName newName"); + return JIM_ERR; + } + + return Jim_RenameCommand(interp, argv[1], argv[2]); +} + +#define JIM_DICTMATCH_KEYS 0x0001 +#define JIM_DICTMATCH_VALUES 0x002 + +int Jim_DictMatchTypes(Jim_Interp *interp, Jim_Obj *objPtr, Jim_Obj *patternObj, int match_type, int return_types) +{ + Jim_Obj *listObjPtr; + Jim_Dict *dict; + int i; + + if (SetDictFromAny(interp, objPtr) != JIM_OK) { + return JIM_ERR; + } + dict = objPtr->internalRep.dictValue; + + listObjPtr = Jim_NewListObj(interp, NULL, 0); + + for (i = 0; i < dict->len; i += 2 ) { + Jim_Obj *keyObj = dict->table[i]; + Jim_Obj *valObj = dict->table[i + 1]; + if (patternObj) { + Jim_Obj *matchObj = (match_type == JIM_DICTMATCH_KEYS) ? keyObj : valObj; + if (!Jim_StringMatchObj(interp, patternObj, matchObj, 0)) { + + continue; + } + } + if (return_types & JIM_DICTMATCH_KEYS) { + Jim_ListAppendElement(interp, listObjPtr, keyObj); + } + if (return_types & JIM_DICTMATCH_VALUES) { + Jim_ListAppendElement(interp, listObjPtr, valObj); + } + } + + Jim_SetResult(interp, listObjPtr); + return JIM_OK; +} + +int Jim_DictSize(Jim_Interp *interp, Jim_Obj *objPtr) +{ + if (SetDictFromAny(interp, objPtr) != JIM_OK) { + return -1; + } + return objPtr->internalRep.dictValue->len / 2; +} + +Jim_Obj *Jim_DictMerge(Jim_Interp *interp, int objc, Jim_Obj *const *objv) +{ + Jim_Obj *objPtr = Jim_NewDictObj(interp, NULL, 0); + int i; + + JimPanic((objc == 0, "Jim_DictMerge called with objc=0")); + + + + for (i = 0; i < objc; i++) { + Jim_Obj **table; + int tablelen; + int j; + + table = Jim_DictPairs(interp, objv[i], &tablelen); + if (tablelen && !table) { + Jim_FreeNewObj(interp, objPtr); + return NULL; + } + for (j = 0; j < tablelen; j += 2) { + DictAddElement(interp, objPtr, table[j], table[j + 1]); + } + } + return objPtr; +} + +int Jim_DictInfo(Jim_Interp *interp, Jim_Obj *objPtr) +{ + char buffer[100]; + Jim_Obj *output; + Jim_Dict *dict; + + if (SetDictFromAny(interp, objPtr) != JIM_OK) { + return JIM_ERR; + } + + dict = objPtr->internalRep.dictValue; + + + snprintf(buffer, sizeof(buffer), "%d entries in table, %d buckets", dict->len, dict->size); + output = Jim_NewStringObj(interp, buffer, -1); + Jim_SetResult(interp, output); + return JIM_OK; +} + +static int Jim_EvalEnsemble(Jim_Interp *interp, const char *basecmd, const char *subcmd, int argc, Jim_Obj *const *argv) +{ + Jim_Obj *prefixObj = Jim_NewStringObj(interp, basecmd, -1); + + Jim_AppendString(interp, prefixObj, " ", 1); + Jim_AppendString(interp, prefixObj, subcmd, -1); + + return Jim_EvalObjPrefix(interp, prefixObj, argc, argv); +} + +static int JimDictWith(Jim_Interp *interp, Jim_Obj *dictVarName, Jim_Obj *const *keyv, int keyc, Jim_Obj *scriptObj) +{ + int i; + Jim_Obj *objPtr; + Jim_Obj *dictObj; + Jim_Obj **dictValues; + int len; + int ret = JIM_OK; + + + dictObj = Jim_GetVariable(interp, dictVarName, JIM_ERRMSG); + if (dictObj == NULL || Jim_DictKeysVector(interp, dictObj, keyv, keyc, &objPtr, JIM_ERRMSG) != JIM_OK) { + return JIM_ERR; + } + + dictValues = Jim_DictPairs(interp, objPtr, &len); + if (len && dictValues == NULL) { + return JIM_ERR; + } + for (i = 0; i < len; i += 2) { + if (Jim_SetVariable(interp, dictValues[i], dictValues[i + 1]) == JIM_ERR) { + return JIM_ERR; + } + } + + + if (Jim_Length(scriptObj)) { + ret = Jim_EvalObj(interp, scriptObj); + + + if (ret == JIM_OK && Jim_GetVariable(interp, dictVarName, 0) != NULL) { + + Jim_Obj **newkeyv = Jim_Alloc(sizeof(*newkeyv) * (keyc + 1)); + for (i = 0; i < keyc; i++) { + newkeyv[i] = keyv[i]; + } + + for (i = 0; i < len; i += 2) { + + if (Jim_StringCompareObj(interp, dictVarName, dictValues[i], 0) != 0) { + + objPtr = Jim_GetVariable(interp, dictValues[i], 0); + newkeyv[keyc] = dictValues[i]; + Jim_SetDictKeysVector(interp, dictVarName, newkeyv, keyc + 1, objPtr, JIM_NORESULT); + } + } + Jim_Free(newkeyv); + } + } + + return ret; +} + + +static int Jim_DictCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + Jim_Obj *objPtr; + int types = JIM_DICTMATCH_KEYS; + + enum { + OPT_CREATE, + OPT_GET, + OPT_GETDEF, + OPT_GETWITHDEFAULT, + OPT_SET, + OPT_UNSET, + OPT_EXISTS, + OPT_KEYS, + OPT_SIZE, + OPT_INFO, + OPT_MERGE, + OPT_WITH, + OPT_APPEND, + OPT_LAPPEND, + OPT_INCR, + OPT_REMOVE, + OPT_VALUES, + OPT_FOR, + OPT_REPLACE, + OPT_UPDATE, + OPT_COUNT + }; + static const jim_subcmd_type cmds[OPT_COUNT + 1] = { + JIM_DEF_SUBCMD("create", "?key value ...?", 0, -2), + JIM_DEF_SUBCMD("get", "dictionary ?key ...?", 1, -1), + JIM_DEF_SUBCMD_HIDDEN("getdef", "dictionary ?key ...? key default", 3, -1), + JIM_DEF_SUBCMD("getwithdefault", "dictionary ?key ...? key default", 3, -1), + JIM_DEF_SUBCMD("set", "varName key ?key ...? value", 3, -1), + JIM_DEF_SUBCMD("unset", "varName key ?key ...?", 2, -1), + JIM_DEF_SUBCMD("exists", "dictionary key ?key ...?", 2, -1), + JIM_DEF_SUBCMD("keys", "dictionary ?pattern?", 1, 2), + JIM_DEF_SUBCMD("size", "dictionary", 1, 1), + JIM_DEF_SUBCMD("info", "dictionary", 1, 1), + JIM_DEF_SUBCMD("merge", "?...?", 0, -1), + JIM_DEF_SUBCMD("with", "dictVar ?key ...? script", 2, -1), + JIM_DEF_SUBCMD("append", "varName key ?value ...?", 2, -1), + JIM_DEF_SUBCMD("lappend", "varName key ?value ...?", 2, -1), + JIM_DEF_SUBCMD("incr", "varName key ?increment?", 2, 3), + JIM_DEF_SUBCMD("remove", "dictionary ?key ...?", 1, -1), + JIM_DEF_SUBCMD("values", "dictionary ?pattern?", 1, 2), + JIM_DEF_SUBCMD("for", "vars dictionary script", 3, 3), + JIM_DEF_SUBCMD("replace", "dictionary ?key value ...?", 1, -1), + JIM_DEF_SUBCMD("update", "varName ?arg ...? script", 2, -1), + { NULL } + }; + const jim_subcmd_type *ct = Jim_ParseSubCmd(interp, cmds, argc, argv); + if (!ct) { + return JIM_ERR; + } + if (ct->function) { + + return ct->function(interp, argc, argv); + } + + + switch (ct - cmds) { + case OPT_GET: + if (Jim_DictKeysVector(interp, argv[2], argv + 3, argc - 3, &objPtr, + JIM_ERRMSG) != JIM_OK) { + return JIM_ERR; + } + Jim_SetResult(interp, objPtr); + return JIM_OK; + + case OPT_GETDEF: + case OPT_GETWITHDEFAULT:{ + int rc = Jim_DictKeysVector(interp, argv[2], argv + 3, argc - 4, &objPtr, JIM_ERRMSG); + if (rc == -1) { + + return JIM_ERR; + } + if (rc == JIM_ERR) { + Jim_SetResult(interp, argv[argc - 1]); + } + else { + Jim_SetResult(interp, objPtr); + } + return JIM_OK; + } + + case OPT_SET: + return Jim_SetDictKeysVector(interp, argv[2], argv + 3, argc - 4, argv[argc - 1], JIM_ERRMSG); + + case OPT_EXISTS:{ + int rc = Jim_DictKeysVector(interp, argv[2], argv + 3, argc - 3, &objPtr, JIM_NONE); + if (rc < 0) { + return JIM_ERR; + } + Jim_SetResultBool(interp, rc == JIM_OK); + return JIM_OK; + } + + case OPT_UNSET: + if (Jim_SetDictKeysVector(interp, argv[2], argv + 3, argc - 3, NULL, JIM_NONE) != JIM_OK) { + return JIM_ERR; + } + return JIM_OK; + + case OPT_VALUES: + types = JIM_DICTMATCH_VALUES; + + case OPT_KEYS: + return Jim_DictMatchTypes(interp, argv[2], argc == 4 ? argv[3] : NULL, types, types); + + case OPT_SIZE: + if (Jim_DictSize(interp, argv[2]) < 0) { + return JIM_ERR; + } + Jim_SetResultInt(interp, Jim_DictSize(interp, argv[2])); + return JIM_OK; + + case OPT_MERGE: + if (argc == 2) { + return JIM_OK; + } + objPtr = Jim_DictMerge(interp, argc - 2, argv + 2); + if (objPtr == NULL) { + return JIM_ERR; + } + Jim_SetResult(interp, objPtr); + return JIM_OK; + + case OPT_CREATE: + objPtr = Jim_NewDictObj(interp, argv + 2, argc - 2); + Jim_SetResult(interp, objPtr); + return JIM_OK; + + case OPT_INFO: + return Jim_DictInfo(interp, argv[2]); + + case OPT_WITH: + return JimDictWith(interp, argv[2], argv + 3, argc - 4, argv[argc - 1]); + + case OPT_UPDATE: + if (argc < 6 || argc % 2) { + + argc = 2; + } + + default: + return Jim_EvalEnsemble(interp, "dict", Jim_String(argv[1]), argc - 2, argv + 2); + } +} + + +static int Jim_SubstCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + static const char * const options[] = { + "-nobackslashes", "-nocommands", "-novariables", NULL + }; + enum + { OPT_NOBACKSLASHES, OPT_NOCOMMANDS, OPT_NOVARIABLES }; + int i; + int flags = JIM_SUBST_FLAG; + Jim_Obj *objPtr; + + if (argc < 2) { + Jim_WrongNumArgs(interp, 1, argv, "?options? string"); + return JIM_ERR; + } + for (i = 1; i < (argc - 1); i++) { + int option; + + if (Jim_GetEnum(interp, argv[i], options, &option, NULL, + JIM_ERRMSG | JIM_ENUM_ABBREV) != JIM_OK) { + return JIM_ERR; + } + switch (option) { + case OPT_NOBACKSLASHES: + flags |= JIM_SUBST_NOESC; + break; + case OPT_NOCOMMANDS: + flags |= JIM_SUBST_NOCMD; + break; + case OPT_NOVARIABLES: + flags |= JIM_SUBST_NOVAR; + break; + } + } + if (Jim_SubstObj(interp, argv[argc - 1], &objPtr, flags) != JIM_OK) { + return JIM_ERR; + } + Jim_SetResult(interp, objPtr); + return JIM_OK; +} + +#ifdef jim_ext_namespace +static int JimIsGlobalNamespace(Jim_Obj *objPtr) +{ + int len; + const char *str = Jim_GetString(objPtr, &len); + return len >= 2 && str[0] == ':' && str[1] == ':'; +} +#endif + + +static int Jim_InfoCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + Jim_Obj *objPtr; + int mode = 0; + + + enum { + INFO_ALIAS, + INFO_ARGS, + INFO_BODY, + INFO_CHANNELS, + INFO_COMMANDS, + INFO_COMPLETE, + INFO_EXISTS, + INFO_FRAME, + INFO_GLOBALS, + INFO_HOSTNAME, + INFO_LEVEL, + INFO_LOCALS, + INFO_NAMEOFEXECUTABLE, + INFO_PATCHLEVEL, + INFO_PROCS, + INFO_REFERENCES, + INFO_RETURNCODES, + INFO_SCRIPT, + INFO_SOURCE, + INFO_STACKTRACE, + INFO_STATICS, + INFO_VARS, + INFO_VERSION, + INFO_COUNT + }; + static const jim_subcmd_type cmds[INFO_COUNT + 1] = { + JIM_DEF_SUBCMD("alias", "command", 1, 1), + JIM_DEF_SUBCMD("args", "procname", 1, 1), + JIM_DEF_SUBCMD("body", "procname", 1, 1), + JIM_DEF_SUBCMD("channels", "?pattern?", 0, 1), + JIM_DEF_SUBCMD("commands", "?pattern?", 0, 1), + JIM_DEF_SUBCMD("complete", "script ?missing?", 1, 2), + JIM_DEF_SUBCMD("exists", "varName", 1, 1), + JIM_DEF_SUBCMD("frame", "?levelNum?", 0, 1), + JIM_DEF_SUBCMD("globals", "?pattern?", 0, 1), + JIM_DEF_SUBCMD("hostname", NULL, 0, 0), + JIM_DEF_SUBCMD("level", "?levelNum?", 0, 1), + JIM_DEF_SUBCMD("locals", "?pattern?", 0, 1), + JIM_DEF_SUBCMD("nameofexecutable", NULL, 0, 0), + JIM_DEF_SUBCMD("patchlevel", NULL, 0, 0), + JIM_DEF_SUBCMD("procs", "?pattern?", 0, 1), + JIM_DEF_SUBCMD("references", NULL, 0, 0), + JIM_DEF_SUBCMD("returncodes", "?code?", 0, 1), + JIM_DEF_SUBCMD("script", "?filename?", 0, 1), + JIM_DEF_SUBCMD("source", "source ?filename line?", 1, 3), + JIM_DEF_SUBCMD("stacktrace", NULL, 0, 0), + JIM_DEF_SUBCMD("statics", "procname", 1, 1), + JIM_DEF_SUBCMD("vars", "?pattern?", 0, 1), + JIM_DEF_SUBCMD("version", NULL, 0, 0), + { NULL } + }; + const jim_subcmd_type *ct; +#ifdef jim_ext_namespace + int nons = 0; + + if (argc > 2 && Jim_CompareStringImmediate(interp, argv[1], "-nons")) { + + argc--; + argv++; + nons = 1; + } +#endif + ct = Jim_ParseSubCmd(interp, cmds, argc, argv); + if (!ct) { + return JIM_ERR; + } + if (ct->function) { + + return ct->function(interp, argc, argv); + } + + int option = ct - cmds; + + switch (option) { + case INFO_EXISTS: + Jim_SetResultBool(interp, Jim_GetVariable(interp, argv[2], 0) != NULL); + return JIM_OK; + + case INFO_ALIAS:{ + Jim_Cmd *cmdPtr; + + if ((cmdPtr = Jim_GetCommand(interp, argv[2], JIM_ERRMSG)) == NULL) { + return JIM_ERR; + } + if (cmdPtr->isproc || cmdPtr->u.native.cmdProc != JimAliasCmd) { + Jim_SetResultFormatted(interp, "command \"%#s\" is not an alias", argv[2]); + return JIM_ERR; + } + Jim_SetResult(interp, (Jim_Obj *)cmdPtr->u.native.privData); + return JIM_OK; + } + + case INFO_CHANNELS: + mode++; +#ifndef jim_ext_aio + Jim_SetResultString(interp, "aio not enabled", -1); + return JIM_ERR; +#endif + + case INFO_PROCS: + mode++; + + case INFO_COMMANDS: + +#ifdef jim_ext_namespace + if (!nons) { + if (Jim_Length(interp->framePtr->nsObj) || (argc == 3 && JimIsGlobalNamespace(argv[2]))) { + return Jim_EvalPrefix(interp, "namespace info", argc - 1, argv + 1); + } + } +#endif + Jim_SetResult(interp, JimCommandsList(interp, (argc == 3) ? argv[2] : NULL, mode)); + return JIM_OK; + + case INFO_VARS: + mode++; + + case INFO_LOCALS: + mode++; + + case INFO_GLOBALS: + +#ifdef jim_ext_namespace + if (!nons) { + if (Jim_Length(interp->framePtr->nsObj) || (argc == 3 && JimIsGlobalNamespace(argv[2]))) { + return Jim_EvalPrefix(interp, "namespace info", argc - 1, argv + 1); + } + } +#endif + Jim_SetResult(interp, JimVariablesList(interp, argc == 3 ? argv[2] : NULL, mode)); + return JIM_OK; + + case INFO_SCRIPT: + if (argc == 3) { + Jim_IncrRefCount(argv[2]); + Jim_DecrRefCount(interp, interp->currentFilenameObj); + interp->currentFilenameObj = argv[2]; + } + Jim_SetResult(interp, interp->currentFilenameObj); + return JIM_OK; + + case INFO_SOURCE:{ + Jim_Obj *resObjPtr; + Jim_Obj *fileNameObj; + + if (argc == 4) { + Jim_SubCmdArgError(interp, ct, argv[0]); + return JIM_ERR; + } + if (argc == 5) { + jim_wide line; + if (Jim_GetWide(interp, argv[4], &line) != JIM_OK) { + return JIM_ERR; + } + resObjPtr = Jim_NewStringObj(interp, Jim_String(argv[2]), Jim_Length(argv[2])); + Jim_SetSourceInfo(interp, resObjPtr, argv[3], line); + } + else { + int line; + fileNameObj = Jim_GetSourceInfo(interp, argv[2], &line); + resObjPtr = Jim_NewListObj(interp, NULL, 0); + Jim_ListAppendElement(interp, resObjPtr, fileNameObj); + Jim_ListAppendElement(interp, resObjPtr, Jim_NewIntObj(interp, line)); + } + Jim_SetResult(interp, resObjPtr); + return JIM_OK; + } + + case INFO_STACKTRACE: + Jim_SetResult(interp, interp->stackTrace); + return JIM_OK; + + case INFO_LEVEL: + if (argc == 2) { + Jim_SetResultInt(interp, interp->framePtr->level); + } + else { + if (JimInfoLevel(interp, argv[2], &objPtr) != JIM_OK) { + return JIM_ERR; + } + Jim_SetResult(interp, objPtr); + } + return JIM_OK; + + case INFO_FRAME: + if (argc == 2) { + Jim_SetResultInt(interp, interp->procLevel + 1); + } + else { + if (JimInfoFrame(interp, argv[2], &objPtr) != JIM_OK) { + return JIM_ERR; + } + Jim_SetResult(interp, objPtr); + } + return JIM_OK; + + case INFO_BODY: + case INFO_STATICS: + case INFO_ARGS:{ + Jim_Cmd *cmdPtr; + + if ((cmdPtr = Jim_GetCommand(interp, argv[2], JIM_ERRMSG)) == NULL) { + return JIM_ERR; + } + if (!cmdPtr->isproc) { + Jim_SetResultFormatted(interp, "command \"%#s\" is not a procedure", argv[2]); + return JIM_ERR; + } + switch (option) { +#ifdef JIM_NO_INTROSPECTION + default: + Jim_SetResultString(interp, "unsupported", -1); + return JIM_ERR; +#else + case INFO_BODY: + Jim_SetResult(interp, cmdPtr->u.proc.bodyObjPtr); + break; + case INFO_ARGS: + Jim_SetResult(interp, cmdPtr->u.proc.argListObjPtr); + break; +#endif + case INFO_STATICS: + if (cmdPtr->u.proc.staticVars) { + Jim_SetResult(interp, JimHashtablePatternMatch(interp, cmdPtr->u.proc.staticVars, + NULL, JimVariablesMatch, JIM_VARLIST_LOCALS | JIM_VARLIST_VALUES)); + } + break; + } + return JIM_OK; + } + + case INFO_VERSION: + case INFO_PATCHLEVEL:{ + char buf[(JIM_INTEGER_SPACE * 2) + 1]; + + sprintf(buf, "%d.%d", JIM_VERSION / 100, JIM_VERSION % 100); + Jim_SetResultString(interp, buf, -1); + return JIM_OK; + } + + case INFO_COMPLETE: { + char missing; + + Jim_SetResultBool(interp, Jim_ScriptIsComplete(interp, argv[2], &missing)); + if (missing != ' ' && argc == 4) { + Jim_SetVariable(interp, argv[3], Jim_NewStringObj(interp, &missing, 1)); + } + return JIM_OK; + } + + case INFO_HOSTNAME: + + return Jim_Eval(interp, "os.gethostname"); + + case INFO_NAMEOFEXECUTABLE: + + return Jim_Eval(interp, "{info nameofexecutable}"); + + case INFO_RETURNCODES: + if (argc == 2) { + int i; + Jim_Obj *listObjPtr = Jim_NewListObj(interp, NULL, 0); + + for (i = 0; jimReturnCodes[i]; i++) { + Jim_ListAppendElement(interp, listObjPtr, Jim_NewIntObj(interp, i)); + Jim_ListAppendElement(interp, listObjPtr, Jim_NewStringObj(interp, + jimReturnCodes[i], -1)); + } + + Jim_SetResult(interp, listObjPtr); + } + else if (argc == 3) { + long code; + const char *name; + + if (Jim_GetLong(interp, argv[2], &code) != JIM_OK) { + return JIM_ERR; + } + name = Jim_ReturnCode(code); + if (*name == '?') { + Jim_SetResultInt(interp, code); + } + else { + Jim_SetResultString(interp, name, -1); + } + } + return JIM_OK; + case INFO_REFERENCES: +#ifdef JIM_REFERENCES + return JimInfoReferences(interp, argc, argv); +#else + Jim_SetResultString(interp, "not supported", -1); + return JIM_ERR; +#endif + default: + abort(); + } +} + + +static int Jim_ExistsCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + Jim_Obj *objPtr; + int result = 0; + + static const char * const options[] = { + "-command", "-proc", "-alias", "-var", NULL + }; + enum + { + OPT_COMMAND, OPT_PROC, OPT_ALIAS, OPT_VAR + }; + int option; + + if (argc == 2) { + option = OPT_VAR; + objPtr = argv[1]; + } + else if (argc == 3) { + if (Jim_GetEnum(interp, argv[1], options, &option, NULL, JIM_ERRMSG | JIM_ENUM_ABBREV) != JIM_OK) { + return JIM_ERR; + } + objPtr = argv[2]; + } + else { + Jim_WrongNumArgs(interp, 1, argv, "?option? name"); + return JIM_ERR; + } + + if (option == OPT_VAR) { + result = Jim_GetVariable(interp, objPtr, 0) != NULL; + } + else { + + Jim_Cmd *cmd = Jim_GetCommand(interp, objPtr, JIM_NONE); + + if (cmd) { + switch (option) { + case OPT_COMMAND: + result = 1; + break; + + case OPT_ALIAS: + result = cmd->isproc == 0 && cmd->u.native.cmdProc == JimAliasCmd; + break; + + case OPT_PROC: + result = cmd->isproc; + break; + } + } + } + Jim_SetResultBool(interp, result); + return JIM_OK; +} + + +static int Jim_SplitCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + const char *str, *splitChars, *noMatchStart; + int splitLen, strLen; + Jim_Obj *resObjPtr; + int c; + int len; + + if (argc != 2 && argc != 3) { + Jim_WrongNumArgs(interp, 1, argv, "string ?splitChars?"); + return JIM_ERR; + } + + str = Jim_GetString(argv[1], &len); + if (len == 0) { + return JIM_OK; + } + strLen = Jim_Utf8Length(interp, argv[1]); + + + if (argc == 2) { + splitChars = " \n\t\r"; + splitLen = 4; + } + else { + splitChars = Jim_String(argv[2]); + splitLen = Jim_Utf8Length(interp, argv[2]); + } + + noMatchStart = str; + resObjPtr = Jim_NewListObj(interp, NULL, 0); + + + if (splitLen) { + Jim_Obj *objPtr; + while (strLen--) { + const char *sc = splitChars; + int scLen = splitLen; + int sl = utf8_tounicode(str, &c); + while (scLen--) { + int pc; + sc += utf8_tounicode(sc, &pc); + if (c == pc) { + objPtr = Jim_NewStringObj(interp, noMatchStart, (str - noMatchStart)); + Jim_ListAppendElement(interp, resObjPtr, objPtr); + noMatchStart = str + sl; + break; + } + } + str += sl; + } + objPtr = Jim_NewStringObj(interp, noMatchStart, (str - noMatchStart)); + Jim_ListAppendElement(interp, resObjPtr, objPtr); + } + else { + Jim_Obj **commonObj = NULL; +#define NUM_COMMON (128 - 9) + while (strLen--) { + int n = utf8_tounicode(str, &c); +#ifdef JIM_OPTIMIZATION + if (c >= 9 && c < 128) { + + c -= 9; + if (!commonObj) { + commonObj = Jim_Alloc(sizeof(*commonObj) * NUM_COMMON); + memset(commonObj, 0, sizeof(*commonObj) * NUM_COMMON); + } + if (!commonObj[c]) { + commonObj[c] = Jim_NewStringObj(interp, str, 1); + } + Jim_ListAppendElement(interp, resObjPtr, commonObj[c]); + str++; + continue; + } +#endif + Jim_ListAppendElement(interp, resObjPtr, Jim_NewStringObjUtf8(interp, str, 1)); + str += n; + } + Jim_Free(commonObj); + } + + Jim_SetResult(interp, resObjPtr); + return JIM_OK; +} + + +static int Jim_JoinCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + const char *joinStr; + int joinStrLen; + + if (argc != 2 && argc != 3) { + Jim_WrongNumArgs(interp, 1, argv, "list ?joinString?"); + return JIM_ERR; + } + + if (argc == 2) { + joinStr = " "; + joinStrLen = 1; + } + else { + joinStr = Jim_GetString(argv[2], &joinStrLen); + } + Jim_SetResult(interp, Jim_ListJoin(interp, argv[1], joinStr, joinStrLen)); + return JIM_OK; +} + + +static int Jim_FormatCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + Jim_Obj *objPtr; + + if (argc < 2) { + Jim_WrongNumArgs(interp, 1, argv, "formatString ?arg arg ...?"); + return JIM_ERR; + } + objPtr = Jim_FormatString(interp, argv[1], argc - 2, argv + 2); + if (objPtr == NULL) + return JIM_ERR; + Jim_SetResult(interp, objPtr); + return JIM_OK; +} + + +static int Jim_ScanCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + Jim_Obj *listPtr, **outVec; + int outc, i; + + if (argc < 3) { + Jim_WrongNumArgs(interp, 1, argv, "string format ?varName varName ...?"); + return JIM_ERR; + } + if (argv[2]->typePtr != &scanFmtStringObjType) + SetScanFmtFromAny(interp, argv[2]); + if (FormatGetError(argv[2]) != 0) { + Jim_SetResultString(interp, FormatGetError(argv[2]), -1); + return JIM_ERR; + } + if (argc > 3) { + int maxPos = FormatGetMaxPos(argv[2]); + int count = FormatGetCnvCount(argv[2]); + + if (maxPos > argc - 3) { + Jim_SetResultString(interp, "\"%n$\" argument index out of range", -1); + return JIM_ERR; + } + else if (count > argc - 3) { + Jim_SetResultString(interp, "different numbers of variable names and " + "field specifiers", -1); + return JIM_ERR; + } + else if (count < argc - 3) { + Jim_SetResultString(interp, "variable is not assigned by any " + "conversion specifiers", -1); + return JIM_ERR; + } + } + listPtr = Jim_ScanString(interp, argv[1], argv[2], JIM_ERRMSG); + if (listPtr == 0) + return JIM_ERR; + if (argc > 3) { + int rc = JIM_OK; + int count = 0; + + if (listPtr != 0 && listPtr != (Jim_Obj *)EOF) { + int len = Jim_ListLength(interp, listPtr); + + if (len != 0) { + JimListGetElements(interp, listPtr, &outc, &outVec); + for (i = 0; i < outc; ++i) { + if (Jim_Length(outVec[i]) > 0) { + ++count; + if (Jim_SetVariable(interp, argv[3 + i], outVec[i]) != JIM_OK) { + rc = JIM_ERR; + } + } + } + } + Jim_FreeNewObj(interp, listPtr); + } + else { + count = -1; + } + if (rc == JIM_OK) { + Jim_SetResultInt(interp, count); + } + return rc; + } + else { + if (listPtr == (Jim_Obj *)EOF) { + Jim_SetResult(interp, Jim_NewListObj(interp, 0, 0)); + return JIM_OK; + } + Jim_SetResult(interp, listPtr); + } + return JIM_OK; +} + + +static int Jim_ErrorCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + if (argc != 2 && argc != 3) { + Jim_WrongNumArgs(interp, 1, argv, "message ?stacktrace?"); + return JIM_ERR; + } + Jim_SetResult(interp, argv[1]); + if (argc == 3) { + JimSetStackTrace(interp, argv[2]); + return JIM_ERR; + } + return JIM_ERR; +} + + +static int Jim_LrangeCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + Jim_Obj *objPtr; + + if (argc != 4) { + Jim_WrongNumArgs(interp, 1, argv, "list first last"); + return JIM_ERR; + } + if ((objPtr = Jim_ListRange(interp, argv[1], argv[2], argv[3])) == NULL) + return JIM_ERR; + Jim_SetResult(interp, objPtr); + return JIM_OK; +} + + +static int Jim_LrepeatCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + Jim_Obj *objPtr; + jim_wide count; + + if (argc < 2 || Jim_GetWideExpr(interp, argv[1], &count) != JIM_OK || count < 0) { + Jim_WrongNumArgs(interp, 1, argv, "count ?value ...?"); + return JIM_ERR; + } + if (count == 0 || argc == 2) { + Jim_SetEmptyResult(interp); + return JIM_OK; + } + + argc -= 2; + argv += 2; + + objPtr = Jim_NewListObj(interp, NULL, 0); + ListEnsureLength(objPtr, argc * count); + while (count--) { + ListInsertElements(objPtr, -1, argc, argv); + } + + Jim_SetResult(interp, objPtr); + return JIM_OK; +} + +char **Jim_GetEnviron(void) +{ +#if defined(HAVE__NSGETENVIRON) + return *_NSGetEnviron(); +#elif defined(_environ) + return _environ; +#else + #if !defined(NO_ENVIRON_EXTERN) + extern char **environ; + #endif + return environ; +#endif +} + +void Jim_SetEnviron(char **env) +{ +#if defined(HAVE__NSGETENVIRON) + *_NSGetEnviron() = env; +#elif defined(_environ) + _environ = env; +#else + #if !defined(NO_ENVIRON_EXTERN) + extern char **environ; + #endif + + environ = env; +#endif +} + + +static int Jim_EnvCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + const char *key; + const char *val; + + if (argc == 1) { + char **e = Jim_GetEnviron(); + + int i; + Jim_Obj *listObjPtr = Jim_NewListObj(interp, NULL, 0); + + for (i = 0; e[i]; i++) { + const char *equals = strchr(e[i], '='); + + if (equals) { + Jim_ListAppendElement(interp, listObjPtr, Jim_NewStringObj(interp, e[i], + equals - e[i])); + Jim_ListAppendElement(interp, listObjPtr, Jim_NewStringObj(interp, equals + 1, -1)); + } + } + + Jim_SetResult(interp, listObjPtr); + return JIM_OK; + } + + if (argc > 3) { + Jim_WrongNumArgs(interp, 1, argv, "varName ?default?"); + return JIM_ERR; + } + key = Jim_String(argv[1]); + val = getenv(key); + if (val == NULL) { + if (argc < 3) { + Jim_SetResultFormatted(interp, "environment variable \"%#s\" does not exist", argv[1]); + return JIM_ERR; + } + val = Jim_String(argv[2]); + } + Jim_SetResult(interp, Jim_NewStringObj(interp, val, -1)); + return JIM_OK; +} + + +static int Jim_SourceCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + int retval; + + if (argc != 2) { + Jim_WrongNumArgs(interp, 1, argv, "fileName"); + return JIM_ERR; + } + retval = Jim_EvalFile(interp, Jim_String(argv[1])); + if (retval == JIM_RETURN) + return JIM_OK; + return retval; +} + + +static int Jim_LreverseCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + Jim_Obj *revObjPtr, **ele; + int len; + + if (argc != 2) { + Jim_WrongNumArgs(interp, 1, argv, "list"); + return JIM_ERR; + } + JimListGetElements(interp, argv[1], &len, &ele); + revObjPtr = Jim_NewListObj(interp, NULL, 0); + ListEnsureLength(revObjPtr, len); + len--; + while (len >= 0) + ListAppendElement(revObjPtr, ele[len--]); + Jim_SetResult(interp, revObjPtr); + return JIM_OK; +} + +static int JimRangeLen(jim_wide start, jim_wide end, jim_wide step) +{ + jim_wide len; + + if (step == 0) + return -1; + if (start == end) + return 0; + else if (step > 0 && start > end) + return -1; + else if (step < 0 && end > start) + return -1; + len = end - start; + if (len < 0) + len = -len; + if (step < 0) + step = -step; + len = 1 + ((len - 1) / step); + if (len > INT_MAX) + len = INT_MAX; + return (int)((len < 0) ? -1 : len); +} + + +static int Jim_RangeCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + jim_wide start = 0, end, step = 1; + int len, i; + Jim_Obj *objPtr; + + if (argc < 2 || argc > 4) { + Jim_WrongNumArgs(interp, 1, argv, "?start? end ?step?"); + return JIM_ERR; + } + if (argc == 2) { + if (Jim_GetWideExpr(interp, argv[1], &end) != JIM_OK) + return JIM_ERR; + } + else { + if (Jim_GetWideExpr(interp, argv[1], &start) != JIM_OK || + Jim_GetWideExpr(interp, argv[2], &end) != JIM_OK) + return JIM_ERR; + if (argc == 4 && Jim_GetWideExpr(interp, argv[3], &step) != JIM_OK) + return JIM_ERR; + } + if ((len = JimRangeLen(start, end, step)) == -1) { + Jim_SetResultString(interp, "Invalid (infinite?) range specified", -1); + return JIM_ERR; + } + objPtr = Jim_NewListObj(interp, NULL, 0); + ListEnsureLength(objPtr, len); + for (i = 0; i < len; i++) + ListAppendElement(objPtr, Jim_NewIntObj(interp, start + i * step)); + Jim_SetResult(interp, objPtr); + return JIM_OK; +} + + +static int Jim_RandCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + jim_wide min = 0, max = 0, len, maxMul; + + if (argc < 1 || argc > 3) { + Jim_WrongNumArgs(interp, 1, argv, "?min? max"); + return JIM_ERR; + } + if (argc == 1) { + max = JIM_WIDE_MAX; + } else if (argc == 2) { + if (Jim_GetWideExpr(interp, argv[1], &max) != JIM_OK) + return JIM_ERR; + } else if (argc == 3) { + if (Jim_GetWideExpr(interp, argv[1], &min) != JIM_OK || + Jim_GetWideExpr(interp, argv[2], &max) != JIM_OK) + return JIM_ERR; + } + len = max-min; + if (len < 0) { + Jim_SetResultString(interp, "Invalid arguments (max < min)", -1); + return JIM_ERR; + } + maxMul = JIM_WIDE_MAX - (len ? (JIM_WIDE_MAX%len) : 0); + while (1) { + jim_wide r; + + JimRandomBytes(interp, &r, sizeof(jim_wide)); + if (r < 0 || r >= maxMul) continue; + r = (len == 0) ? 0 : r%len; + Jim_SetResultInt(interp, min+r); + return JIM_OK; + } +} + +static const struct { + const char *name; + Jim_CmdProc *cmdProc; +} Jim_CoreCommandsTable[] = { + {"alias", Jim_AliasCoreCommand}, + {"set", Jim_SetCoreCommand}, + {"unset", Jim_UnsetCoreCommand}, + {"puts", Jim_PutsCoreCommand}, + {"+", Jim_AddCoreCommand}, + {"*", Jim_MulCoreCommand}, + {"-", Jim_SubCoreCommand}, + {"/", Jim_DivCoreCommand}, + {"incr", Jim_IncrCoreCommand}, + {"while", Jim_WhileCoreCommand}, + {"loop", Jim_LoopCoreCommand}, + {"for", Jim_ForCoreCommand}, + {"foreach", Jim_ForeachCoreCommand}, + {"lmap", Jim_LmapCoreCommand}, + {"lassign", Jim_LassignCoreCommand}, + {"if", Jim_IfCoreCommand}, + {"switch", Jim_SwitchCoreCommand}, + {"list", Jim_ListCoreCommand}, + {"lindex", Jim_LindexCoreCommand}, + {"lset", Jim_LsetCoreCommand}, + {"lsearch", Jim_LsearchCoreCommand}, + {"llength", Jim_LlengthCoreCommand}, + {"lappend", Jim_LappendCoreCommand}, + {"linsert", Jim_LinsertCoreCommand}, + {"lreplace", Jim_LreplaceCoreCommand}, + {"lsort", Jim_LsortCoreCommand}, + {"append", Jim_AppendCoreCommand}, + {"eval", Jim_EvalCoreCommand}, + {"uplevel", Jim_UplevelCoreCommand}, + {"expr", Jim_ExprCoreCommand}, + {"break", Jim_BreakCoreCommand}, + {"continue", Jim_ContinueCoreCommand}, + {"proc", Jim_ProcCoreCommand}, + {"xtrace", Jim_XtraceCoreCommand}, + {"concat", Jim_ConcatCoreCommand}, + {"return", Jim_ReturnCoreCommand}, + {"upvar", Jim_UpvarCoreCommand}, + {"global", Jim_GlobalCoreCommand}, + {"string", Jim_StringCoreCommand}, + {"time", Jim_TimeCoreCommand}, + {"timerate", Jim_TimeRateCoreCommand}, + {"exit", Jim_ExitCoreCommand}, + {"catch", Jim_CatchCoreCommand}, + {"try", Jim_TryCoreCommand}, +#ifdef JIM_REFERENCES + {"ref", Jim_RefCoreCommand}, + {"getref", Jim_GetrefCoreCommand}, + {"setref", Jim_SetrefCoreCommand}, + {"finalize", Jim_FinalizeCoreCommand}, + {"collect", Jim_CollectCoreCommand}, +#endif + {"rename", Jim_RenameCoreCommand}, + {"dict", Jim_DictCoreCommand}, + {"subst", Jim_SubstCoreCommand}, + {"info", Jim_InfoCoreCommand}, + {"exists", Jim_ExistsCoreCommand}, + {"split", Jim_SplitCoreCommand}, + {"join", Jim_JoinCoreCommand}, + {"format", Jim_FormatCoreCommand}, + {"scan", Jim_ScanCoreCommand}, + {"error", Jim_ErrorCoreCommand}, + {"lrange", Jim_LrangeCoreCommand}, + {"lrepeat", Jim_LrepeatCoreCommand}, + {"env", Jim_EnvCoreCommand}, + {"source", Jim_SourceCoreCommand}, + {"lreverse", Jim_LreverseCoreCommand}, + {"range", Jim_RangeCoreCommand}, + {"rand", Jim_RandCoreCommand}, + {"tailcall", Jim_TailcallCoreCommand}, + {"local", Jim_LocalCoreCommand}, + {"upcall", Jim_UpcallCoreCommand}, + {"apply", Jim_ApplyCoreCommand}, + {"stacktrace", Jim_StacktraceCoreCommand}, + {NULL, NULL}, +}; + +void Jim_RegisterCoreCommands(Jim_Interp *interp) +{ + int i = 0; + + while (Jim_CoreCommandsTable[i].name != NULL) { + Jim_CreateCommand(interp, + Jim_CoreCommandsTable[i].name, Jim_CoreCommandsTable[i].cmdProc, NULL, NULL); + i++; + } +} + +void Jim_MakeErrorMessage(Jim_Interp *interp) +{ + Jim_Obj *argv[2]; + + argv[0] = Jim_NewStringObj(interp, "errorInfo", -1); + argv[1] = interp->result; + + Jim_EvalObjVector(interp, 2, argv); +} + +static char **JimSortStringTable(const char *const *tablePtr) +{ + int count; + char **tablePtrSorted; + + + for (count = 0; tablePtr[count]; count++) { + } + + + tablePtrSorted = Jim_Alloc(sizeof(char *) * (count + 1)); + memcpy(tablePtrSorted, tablePtr, sizeof(char *) * count); + qsort(tablePtrSorted, count, sizeof(char *), qsortCompareStringPointers); + tablePtrSorted[count] = NULL; + + return tablePtrSorted; +} + +static void JimSetFailedEnumResult(Jim_Interp *interp, const char *arg, const char *badtype, + const char *prefix, const char *const *tablePtr, const char *name) +{ + char **tablePtrSorted; + int i; + + if (name == NULL) { + name = "option"; + } + + Jim_SetResultFormatted(interp, "%s%s \"%s\": must be ", badtype, name, arg); + tablePtrSorted = JimSortStringTable(tablePtr); + for (i = 0; tablePtrSorted[i]; i++) { + if (tablePtrSorted[i + 1] == NULL && i > 0) { + Jim_AppendString(interp, Jim_GetResult(interp), "or ", -1); + } + Jim_AppendStrings(interp, Jim_GetResult(interp), prefix, tablePtrSorted[i], NULL); + if (tablePtrSorted[i + 1]) { + Jim_AppendString(interp, Jim_GetResult(interp), ", ", -1); + } + } + Jim_Free(tablePtrSorted); +} + + +int Jim_CheckShowCommands(Jim_Interp *interp, Jim_Obj *objPtr, const char *const *tablePtr) +{ + if (Jim_CompareStringImmediate(interp, objPtr, "-commands")) { + int i; + char **tablePtrSorted = JimSortStringTable(tablePtr); + Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0)); + for (i = 0; tablePtrSorted[i]; i++) { + Jim_ListAppendElement(interp, Jim_GetResult(interp), Jim_NewStringObj(interp, tablePtrSorted[i], -1)); + } + Jim_Free(tablePtrSorted); + return JIM_OK; + } + return JIM_ERR; +} + +static const Jim_ObjType getEnumObjType = { + "get-enum", + NULL, + NULL, + NULL, + JIM_TYPE_REFERENCES +}; + +int Jim_GetEnum(Jim_Interp *interp, Jim_Obj *objPtr, + const char *const *tablePtr, int *indexPtr, const char *name, int flags) +{ + const char *bad = "bad "; + const char *const *entryPtr = NULL; + int i; + int match = -1; + int arglen; + const char *arg; + + if (objPtr->typePtr == &getEnumObjType) { + if (objPtr->internalRep.ptrIntValue.ptr == tablePtr && objPtr->internalRep.ptrIntValue.int1 == flags) { + *indexPtr = objPtr->internalRep.ptrIntValue.int2; + return JIM_OK; + } + } + + arg = Jim_GetString(objPtr, &arglen); + + *indexPtr = -1; + + for (entryPtr = tablePtr, i = 0; *entryPtr != NULL; entryPtr++, i++) { + if (Jim_CompareStringImmediate(interp, objPtr, *entryPtr)) { + + match = i; + goto found; + } + if (flags & JIM_ENUM_ABBREV) { + if (strncmp(arg, *entryPtr, arglen) == 0) { + if (*arg == '-' && arglen == 1) { + break; + } + if (match >= 0) { + bad = "ambiguous "; + goto ambiguous; + } + match = i; + } + } + } + + + if (match >= 0) { + found: + + Jim_FreeIntRep(interp, objPtr); + objPtr->typePtr = &getEnumObjType; + objPtr->internalRep.ptrIntValue.ptr = (void *)tablePtr; + objPtr->internalRep.ptrIntValue.int1 = flags; + objPtr->internalRep.ptrIntValue.int2 = match; + + *indexPtr = match; + return JIM_OK; + } + + ambiguous: + if (flags & JIM_ERRMSG) { + JimSetFailedEnumResult(interp, arg, bad, "", tablePtr, name); + } + return JIM_ERR; +} + +int Jim_FindByName(const char *name, const char * const array[], size_t len) +{ + int i; + + for (i = 0; i < (int)len; i++) { + if (array[i] && strcmp(array[i], name) == 0) { + return i; + } + } + return -1; +} + +int Jim_IsDict(Jim_Obj *objPtr) +{ + return objPtr->typePtr == &dictObjType; +} + +int Jim_IsList(Jim_Obj *objPtr) +{ + return objPtr->typePtr == &listObjType; +} + +void Jim_SetResultFormatted(Jim_Interp *interp, const char *format, ...) +{ + + int len = strlen(format); + int extra = 0; + int n = 0; + const char *params[5]; + int nobjparam = 0; + Jim_Obj *objparam[5]; + char *buf; + va_list args; + int i; + + va_start(args, format); + + for (i = 0; i < len && n < 5; i++) { + int l; + + if (strncmp(format + i, "%s", 2) == 0) { + params[n] = va_arg(args, char *); + + l = strlen(params[n]); + } + else if (strncmp(format + i, "%#s", 3) == 0) { + Jim_Obj *objPtr = va_arg(args, Jim_Obj *); + + params[n] = Jim_GetString(objPtr, &l); + objparam[nobjparam++] = objPtr; + Jim_IncrRefCount(objPtr); + } + else { + if (format[i] == '%') { + i++; + } + continue; + } + n++; + extra += l; + } + + len += extra; + buf = Jim_Alloc(len + 1); + len = snprintf(buf, len + 1, format, params[0], params[1], params[2], params[3], params[4]); + + va_end(args); + + Jim_SetResult(interp, Jim_NewStringObjNoAlloc(interp, buf, len)); + + for (i = 0; i < nobjparam; i++) { + Jim_DecrRefCount(interp, objparam[i]); + } +} + +int Jim_CheckAbiVersion(Jim_Interp *interp, int abi_version) +{ + if (abi_version != JIM_ABI_VERSION) { + Jim_SetResultString(interp, "ABI version mismatch", -1); + return JIM_ERR; + } + return JIM_OK; +} + + +#ifndef jim_ext_package +int Jim_PackageProvide(Jim_Interp *interp, const char *name, const char *ver, int flags) +{ + return JIM_OK; +} +#endif +#ifndef jim_ext_aio +int Jim_AioFilehandle(Jim_Interp *interp, Jim_Obj *fhObj) +{ + return -1; +} +#endif + + +#include +#include + + +static int subcmd_null(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + + return JIM_OK; +} + +static const jim_subcmd_type dummy_subcmd = { + "dummy", NULL, subcmd_null, 0, 0, JIM_MODFLAG_HIDDEN +}; + +static Jim_Obj *subcmd_cmd_list(Jim_Interp *interp, const jim_subcmd_type * ct, const char *sep) +{ + + Jim_Obj *listObj = Jim_NewListObj(interp, NULL, 0); + Jim_Obj *sortCmd[2]; + + for (; ct->cmd; ct++) { + if (!(ct->flags & JIM_MODFLAG_HIDDEN)) { + Jim_ListAppendElement(interp, listObj, Jim_NewStringObj(interp, ct->cmd, -1)); + } + } + + + sortCmd[0] = Jim_NewStringObj(interp, "lsort", -1); + sortCmd[1] = listObj; + + if (Jim_EvalObjVector(interp, 2, sortCmd) == JIM_OK) { + return Jim_ListJoin(interp, Jim_GetResult(interp), sep, strlen(sep)); + } + + return Jim_GetResult(interp); +} + +static void bad_subcmd(Jim_Interp *interp, const jim_subcmd_type * command_table, const char *type, + Jim_Obj *cmd, Jim_Obj *subcmd) +{ + Jim_SetResultFormatted(interp, "%#s, %s command \"%#s\": should be %#s", cmd, type, + subcmd, subcmd_cmd_list(interp, command_table, ", ")); +} + +static void show_cmd_usage(Jim_Interp *interp, const jim_subcmd_type * command_table, int argc, + Jim_Obj *const *argv) +{ + Jim_SetResultFormatted(interp, "Usage: \"%#s command ... \", where command is one of: %#s", + argv[0], subcmd_cmd_list(interp, command_table, ", ")); +} + +static void add_cmd_usage(Jim_Interp *interp, const jim_subcmd_type * ct, Jim_Obj *cmd) +{ + if (cmd) { + Jim_AppendStrings(interp, Jim_GetResult(interp), Jim_String(cmd), " ", NULL); + } + Jim_AppendStrings(interp, Jim_GetResult(interp), ct->cmd, NULL); + if (ct->args && *ct->args) { + Jim_AppendStrings(interp, Jim_GetResult(interp), " ", ct->args, NULL); + } +} + +void Jim_SubCmdArgError(Jim_Interp *interp, const jim_subcmd_type * ct, Jim_Obj *subcmd) +{ + Jim_SetResultString(interp, "wrong # args: should be \"", -1); + add_cmd_usage(interp, ct, subcmd); + Jim_AppendStrings(interp, Jim_GetResult(interp), "\"", NULL); +} + +static const Jim_ObjType subcmdLookupObjType = { + "subcmd-lookup", + NULL, + NULL, + NULL, + JIM_TYPE_REFERENCES +}; + +const jim_subcmd_type *Jim_ParseSubCmd(Jim_Interp *interp, const jim_subcmd_type * command_table, + int argc, Jim_Obj *const *argv) +{ + const jim_subcmd_type *ct; + const jim_subcmd_type *partial = 0; + int cmdlen; + Jim_Obj *cmd; + const char *cmdstr; + int help = 0; + int argsok = 1; + + if (argc < 2) { + Jim_SetResultFormatted(interp, "wrong # args: should be \"%#s command ...\"\n" + "Use \"%#s -help ?command?\" for help", argv[0], argv[0]); + return 0; + } + + cmd = argv[1]; + + + if (cmd->typePtr == &subcmdLookupObjType) { + if (cmd->internalRep.ptrIntValue.ptr == command_table) { + ct = command_table + cmd->internalRep.ptrIntValue.int1; + goto found; + } + } + + + if (Jim_CompareStringImmediate(interp, cmd, "-help")) { + if (argc == 2) { + + show_cmd_usage(interp, command_table, argc, argv); + return &dummy_subcmd; + } + help = 1; + + + cmd = argv[2]; + } + + + if (Jim_CompareStringImmediate(interp, cmd, "-commands")) { + Jim_SetResult(interp, subcmd_cmd_list(interp, command_table, " ")); + return &dummy_subcmd; + } + + cmdstr = Jim_GetString(cmd, &cmdlen); + + for (ct = command_table; ct->cmd; ct++) { + if (Jim_CompareStringImmediate(interp, cmd, ct->cmd)) { + + break; + } + if (strncmp(cmdstr, ct->cmd, cmdlen) == 0) { + if (partial) { + + if (help) { + + show_cmd_usage(interp, command_table, argc, argv); + return &dummy_subcmd; + } + bad_subcmd(interp, command_table, "ambiguous", argv[0], argv[1 + help]); + return 0; + } + partial = ct; + } + continue; + } + + + if (partial && !ct->cmd) { + ct = partial; + } + + if (!ct->cmd) { + + if (help) { + + show_cmd_usage(interp, command_table, argc, argv); + return &dummy_subcmd; + } + bad_subcmd(interp, command_table, "unknown", argv[0], argv[1 + help]); + return 0; + } + + if (help) { + Jim_SetResultString(interp, "Usage: ", -1); + + add_cmd_usage(interp, ct, argv[0]); + return &dummy_subcmd; + } + + + Jim_FreeIntRep(interp, cmd); + cmd->typePtr = &subcmdLookupObjType; + cmd->internalRep.ptrIntValue.ptr = (void *)command_table; + cmd->internalRep.ptrIntValue.int1 = ct - command_table; + +found: + + + if (argc - 2 < ct->minargs) { + argsok = 0; + } + else if (ct->maxargs >= 0 && argc - 2 > ct->maxargs) { + argsok = 0; + } + else if (ct->maxargs < -1 && (argc - 2) % -ct->maxargs != 0) { + + argsok = 0; + } + if (!argsok) { + Jim_SetResultString(interp, "wrong # args: should be \"", -1); + + add_cmd_usage(interp, ct, argv[0]); + Jim_AppendStrings(interp, Jim_GetResult(interp), "\"", NULL); + + return 0; + } + + + return ct; +} + +int Jim_CallSubCmd(Jim_Interp *interp, const jim_subcmd_type * ct, int argc, Jim_Obj *const *argv) +{ + int ret = JIM_ERR; + + if (ct) { + if (ct->flags & JIM_MODFLAG_FULLARGV) { + ret = ct->function(interp, argc, argv); + } + else { + ret = ct->function(interp, argc - 2, argv + 2); + } + if (ret < 0) { + Jim_SubCmdArgError(interp, ct, argv[0]); + ret = JIM_ERR; + } + } + return ret; +} + +int Jim_SubCmdProc(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + const jim_subcmd_type *ct = + Jim_ParseSubCmd(interp, (const jim_subcmd_type *)Jim_CmdPrivData(interp), argc, argv); + + return Jim_CallSubCmd(interp, ct, argc, argv); +} + +#include +#include +#include +#include +#include + + +int utf8_fromunicode(char *p, unsigned uc) +{ + if (uc <= 0x7f) { + *p = uc; + return 1; + } + else if (uc <= 0x7ff) { + *p++ = 0xc0 | ((uc & 0x7c0) >> 6); + *p = 0x80 | (uc & 0x3f); + return 2; + } + else if (uc <= 0xffff) { + *p++ = 0xe0 | ((uc & 0xf000) >> 12); + *p++ = 0x80 | ((uc & 0xfc0) >> 6); + *p = 0x80 | (uc & 0x3f); + return 3; + } + + else { + *p++ = 0xf0 | ((uc & 0x1c0000) >> 18); + *p++ = 0x80 | ((uc & 0x3f000) >> 12); + *p++ = 0x80 | ((uc & 0xfc0) >> 6); + *p = 0x80 | (uc & 0x3f); + return 4; + } +} + +#include +#include +#include + + +#define JIM_INTEGER_SPACE 24 +#define MAX_FLOAT_WIDTH 320 + +Jim_Obj *Jim_FormatString(Jim_Interp *interp, Jim_Obj *fmtObjPtr, int objc, Jim_Obj *const *objv) +{ + const char *span, *format, *formatEnd, *msg; + int numBytes = 0, objIndex = 0, gotXpg = 0, gotSequential = 0; + static const char * const mixedXPG = + "cannot mix \"%\" and \"%n$\" conversion specifiers"; + static const char * const badIndex[2] = { + "not enough arguments for all format specifiers", + "\"%n$\" argument index out of range" + }; + int formatLen; + Jim_Obj *resultPtr; + + char *num_buffer = NULL; + int num_buffer_size = 0; + + span = format = Jim_GetString(fmtObjPtr, &formatLen); + formatEnd = format + formatLen; + resultPtr = Jim_NewEmptyStringObj(interp); + + while (format != formatEnd) { + char *end; + int gotMinus, sawFlag; + int gotPrecision, useShort; + long width, precision; + int newXpg; + int ch; + int step; + int doubleType; + char pad = ' '; + char spec[2*JIM_INTEGER_SPACE + 12]; + char *p; + + int formatted_chars; + int formatted_bytes; + const char *formatted_buf; + + step = utf8_tounicode(format, &ch); + format += step; + if (ch != '%') { + numBytes += step; + continue; + } + if (numBytes) { + Jim_AppendString(interp, resultPtr, span, numBytes); + numBytes = 0; + } + + + step = utf8_tounicode(format, &ch); + if (ch == '%') { + span = format; + numBytes = step; + format += step; + continue; + } + + + newXpg = 0; + if (isdigit(ch)) { + int position = strtoul(format, &end, 10); + if (*end == '$') { + newXpg = 1; + objIndex = position - 1; + format = end + 1; + step = utf8_tounicode(format, &ch); + } + } + if (newXpg) { + if (gotSequential) { + msg = mixedXPG; + goto errorMsg; + } + gotXpg = 1; + } else { + if (gotXpg) { + msg = mixedXPG; + goto errorMsg; + } + gotSequential = 1; + } + if ((objIndex < 0) || (objIndex >= objc)) { + msg = badIndex[gotXpg]; + goto errorMsg; + } + + p = spec; + *p++ = '%'; + + gotMinus = 0; + sawFlag = 1; + do { + switch (ch) { + case '-': + gotMinus = 1; + break; + case '0': + pad = ch; + break; + case ' ': + case '+': + case '#': + break; + default: + sawFlag = 0; + continue; + } + *p++ = ch; + format += step; + step = utf8_tounicode(format, &ch); + + } while (sawFlag && (p - spec <= 5)); + + + width = 0; + if (isdigit(ch)) { + width = strtoul(format, &end, 10); + format = end; + step = utf8_tounicode(format, &ch); + } else if (ch == '*') { + if (objIndex >= objc - 1) { + msg = badIndex[gotXpg]; + goto errorMsg; + } + if (Jim_GetLong(interp, objv[objIndex], &width) != JIM_OK) { + goto error; + } + if (width < 0) { + width = -width; + if (!gotMinus) { + *p++ = '-'; + gotMinus = 1; + } + } + objIndex++; + format += step; + step = utf8_tounicode(format, &ch); + } + + + gotPrecision = precision = 0; + if (ch == '.') { + gotPrecision = 1; + format += step; + step = utf8_tounicode(format, &ch); + } + if (isdigit(ch)) { + precision = strtoul(format, &end, 10); + format = end; + step = utf8_tounicode(format, &ch); + } else if (ch == '*') { + if (objIndex >= objc - 1) { + msg = badIndex[gotXpg]; + goto errorMsg; + } + if (Jim_GetLong(interp, objv[objIndex], &precision) != JIM_OK) { + goto error; + } + + + if (precision < 0) { + precision = 0; + } + objIndex++; + format += step; + step = utf8_tounicode(format, &ch); + } + + + useShort = 0; + if (ch == 'h') { + useShort = 1; + format += step; + step = utf8_tounicode(format, &ch); + } else if (ch == 'l') { + + format += step; + step = utf8_tounicode(format, &ch); + if (ch == 'l') { + format += step; + step = utf8_tounicode(format, &ch); + } + } + + format += step; + span = format; + + + if (ch == 'i') { + ch = 'd'; + } + + doubleType = 0; + + switch (ch) { + case '\0': + msg = "format string ended in middle of field specifier"; + goto errorMsg; + case 's': { + formatted_buf = Jim_GetString(objv[objIndex], &formatted_bytes); + formatted_chars = Jim_Utf8Length(interp, objv[objIndex]); + if (gotPrecision && (precision < formatted_chars)) { + + formatted_chars = precision; + formatted_bytes = utf8_index(formatted_buf, precision); + } + break; + } + case 'c': { + jim_wide code; + + if (Jim_GetWide(interp, objv[objIndex], &code) != JIM_OK) { + goto error; + } + + formatted_bytes = utf8_getchars(spec, code); + formatted_buf = spec; + formatted_chars = 1; + break; + } + case 'b': { + unsigned jim_wide w; + int length; + int i; + int j; + + if (Jim_GetWide(interp, objv[objIndex], (jim_wide *)&w) != JIM_OK) { + goto error; + } + length = sizeof(w) * 8; + + + + if (num_buffer_size < length + 1) { + num_buffer_size = length + 1; + num_buffer = Jim_Realloc(num_buffer, num_buffer_size); + } + + j = 0; + for (i = length; i > 0; ) { + i--; + if (w & ((unsigned jim_wide)1 << i)) { + num_buffer[j++] = '1'; + } + else if (j || i == 0) { + num_buffer[j++] = '0'; + } + } + num_buffer[j] = 0; + formatted_chars = formatted_bytes = j; + formatted_buf = num_buffer; + break; + } + + case 'e': + case 'E': + case 'f': + case 'g': + case 'G': + doubleType = 1; + + case 'd': + case 'u': + case 'o': + case 'x': + case 'X': { + jim_wide w; + double d; + int length; + + + if (width) { + p += sprintf(p, "%ld", width); + } + if (gotPrecision) { + p += sprintf(p, ".%ld", precision); + } + + + if (doubleType) { + if (Jim_GetDouble(interp, objv[objIndex], &d) != JIM_OK) { + goto error; + } + length = MAX_FLOAT_WIDTH; + } + else { + if (Jim_GetWide(interp, objv[objIndex], &w) != JIM_OK) { + goto error; + } + length = JIM_INTEGER_SPACE; + if (useShort) { + if (ch == 'd') { + w = (short)w; + } + else { + w = (unsigned short)w; + } + } + *p++ = 'l'; +#ifdef HAVE_LONG_LONG + if (sizeof(long long) == sizeof(jim_wide)) { + *p++ = 'l'; + } +#endif + } + + *p++ = (char) ch; + *p = '\0'; + + + if (width > 10000 || length > 10000 || precision > 10000) { + Jim_SetResultString(interp, "format too long", -1); + goto error; + } + + + + if (width > length) { + length = width; + } + if (gotPrecision) { + length += precision; + } + + + if (num_buffer_size < length + 1) { + num_buffer_size = length + 1; + num_buffer = Jim_Realloc(num_buffer, num_buffer_size); + } + + if (doubleType) { + snprintf(num_buffer, length + 1, spec, d); + } + else { + formatted_bytes = snprintf(num_buffer, length + 1, spec, w); + } + formatted_chars = formatted_bytes = strlen(num_buffer); + formatted_buf = num_buffer; + break; + } + + default: { + + spec[0] = ch; + spec[1] = '\0'; + Jim_SetResultFormatted(interp, "bad field specifier \"%s\"", spec); + goto error; + } + } + + if (!gotMinus) { + while (formatted_chars < width) { + Jim_AppendString(interp, resultPtr, &pad, 1); + formatted_chars++; + } + } + + Jim_AppendString(interp, resultPtr, formatted_buf, formatted_bytes); + + while (formatted_chars < width) { + Jim_AppendString(interp, resultPtr, &pad, 1); + formatted_chars++; + } + + objIndex += gotSequential; + } + if (numBytes) { + Jim_AppendString(interp, resultPtr, span, numBytes); + } + + Jim_Free(num_buffer); + return resultPtr; + + errorMsg: + Jim_SetResultString(interp, msg, -1); + error: + Jim_FreeNewObj(interp, resultPtr); + Jim_Free(num_buffer); + return NULL; +} + + +#if defined(JIM_REGEXP) +#include +#include +#include +#include + + + +#define REG_MAX_PAREN 100 + + + +#define END 0 +#define BOL 1 +#define EOL 2 +#define ANY 3 +#define ANYOF 4 +#define ANYBUT 5 +#define BRANCH 6 +#define BACK 7 +#define EXACTLY 8 +#define NOTHING 9 +#define REP 10 +#define REPMIN 11 +#define REPX 12 +#define REPXMIN 13 +#define BOLX 14 +#define EOLX 15 +#define WORDA 16 +#define WORDZ 17 + +#define OPENNC 1000 +#define OPEN 1001 + + + + +#define CLOSENC 2000 +#define CLOSE 2001 +#define CLOSE_END (CLOSE+REG_MAX_PAREN) + +#define REG_MAGIC 0xFADED00D + + +#define OP(preg, p) (preg->program[p]) +#define NEXT(preg, p) (preg->program[p + 1]) +#define OPERAND(p) ((p) + 2) + + + + +#define FAIL(R,M) { (R)->err = (M); return (M); } +#define ISMULT(c) ((c) == '*' || (c) == '+' || (c) == '?' || (c) == '{') +#define META "^$.[()|?{+*" + +#define HASWIDTH 1 +#define SIMPLE 2 +#define SPSTART 4 +#define WORST 0 + +#define MAX_REP_COUNT 1000000 + +static int reg(regex_t *preg, int paren, int *flagp ); +static int regpiece(regex_t *preg, int *flagp ); +static int regbranch(regex_t *preg, int *flagp ); +static int regatom(regex_t *preg, int *flagp ); +static int regnode(regex_t *preg, int op ); +static int regnext(regex_t *preg, int p ); +static void regc(regex_t *preg, int b ); +static int reginsert(regex_t *preg, int op, int size, int opnd ); +static void regtail(regex_t *preg, int p, int val); +static void regoptail(regex_t *preg, int p, int val ); +static int regopsize(regex_t *preg, int p ); + +static int reg_range_find(const int *string, int c); +static const char *str_find(const char *string, int c, int nocase); +static int prefix_cmp(const int *prog, int proglen, const char *string, int nocase); + + +#ifdef DEBUG +static int regnarrate = 0; +static void regdump(regex_t *preg); +static const char *regprop( int op ); +#endif + + +static int str_int_len(const int *seq) +{ + int n = 0; + while (*seq++) { + n++; + } + return n; +} + +int jim_regcomp(regex_t *preg, const char *exp, int cflags) +{ + int scan; + int longest; + unsigned len; + int flags; + +#ifdef DEBUG + fprintf(stderr, "Compiling: '%s'\n", exp); +#endif + memset(preg, 0, sizeof(*preg)); + + if (exp == NULL) + FAIL(preg, REG_ERR_NULL_ARGUMENT); + + + preg->cflags = cflags; + preg->regparse = exp; + + + preg->proglen = (strlen(exp) + 1) * 5; + preg->program = malloc(preg->proglen * sizeof(int)); + if (preg->program == NULL) + FAIL(preg, REG_ERR_NOMEM); + + regc(preg, REG_MAGIC); + if (reg(preg, 0, &flags) == 0) { + return preg->err; + } + + + if (preg->re_nsub >= REG_MAX_PAREN) + FAIL(preg,REG_ERR_TOO_BIG); + + + preg->regstart = 0; + preg->reganch = 0; + preg->regmust = 0; + preg->regmlen = 0; + scan = 1; + if (OP(preg, regnext(preg, scan)) == END) { + scan = OPERAND(scan); + + + if (OP(preg, scan) == EXACTLY) { + preg->regstart = preg->program[OPERAND(scan)]; + } + else if (OP(preg, scan) == BOL) + preg->reganch++; + + if (flags&SPSTART) { + longest = 0; + len = 0; + for (; scan != 0; scan = regnext(preg, scan)) { + if (OP(preg, scan) == EXACTLY) { + int plen = str_int_len(preg->program + OPERAND(scan)); + if (plen >= len) { + longest = OPERAND(scan); + len = plen; + } + } + } + preg->regmust = longest; + preg->regmlen = len; + } + } + +#ifdef DEBUG + regdump(preg); +#endif + + return 0; +} + +static int reg(regex_t *preg, int paren, int *flagp ) +{ + int ret; + int br; + int ender; + int parno = 0; + int flags; + + *flagp = HASWIDTH; + + + if (paren) { + if (preg->regparse[0] == '?' && preg->regparse[1] == ':') { + + preg->regparse += 2; + parno = -1; + } + else { + parno = ++preg->re_nsub; + } + ret = regnode(preg, OPEN+parno); + } else + ret = 0; + + + br = regbranch(preg, &flags); + if (br == 0) + return 0; + if (ret != 0) + regtail(preg, ret, br); + else + ret = br; + if (!(flags&HASWIDTH)) + *flagp &= ~HASWIDTH; + *flagp |= flags&SPSTART; + while (*preg->regparse == '|') { + preg->regparse++; + br = regbranch(preg, &flags); + if (br == 0) + return 0; + regtail(preg, ret, br); + if (!(flags&HASWIDTH)) + *flagp &= ~HASWIDTH; + *flagp |= flags&SPSTART; + } + + + ender = regnode(preg, (paren) ? CLOSE+parno : END); + regtail(preg, ret, ender); + + + for (br = ret; br != 0; br = regnext(preg, br)) + regoptail(preg, br, ender); + + + if (paren && *preg->regparse++ != ')') { + preg->err = REG_ERR_UNMATCHED_PAREN; + return 0; + } else if (!paren && *preg->regparse != '\0') { + if (*preg->regparse == ')') { + preg->err = REG_ERR_UNMATCHED_PAREN; + return 0; + } else { + preg->err = REG_ERR_JUNK_ON_END; + return 0; + } + } + + return(ret); +} + +static int regbranch(regex_t *preg, int *flagp ) +{ + int ret; + int chain; + int latest; + int flags; + + *flagp = WORST; + + ret = regnode(preg, BRANCH); + chain = 0; + while (*preg->regparse != '\0' && *preg->regparse != ')' && + *preg->regparse != '|') { + latest = regpiece(preg, &flags); + if (latest == 0) + return 0; + *flagp |= flags&HASWIDTH; + if (chain == 0) { + *flagp |= flags&SPSTART; + } + else { + regtail(preg, chain, latest); + } + chain = latest; + } + if (chain == 0) + (void) regnode(preg, NOTHING); + + return(ret); +} + +static int regpiece(regex_t *preg, int *flagp) +{ + int ret; + char op; + int next; + int flags; + int min; + int max; + + ret = regatom(preg, &flags); + if (ret == 0) + return 0; + + op = *preg->regparse; + if (!ISMULT(op)) { + *flagp = flags; + return(ret); + } + + if (!(flags&HASWIDTH) && op != '?') { + preg->err = REG_ERR_OPERAND_COULD_BE_EMPTY; + return 0; + } + + + if (op == '{') { + char *end; + + min = strtoul(preg->regparse + 1, &end, 10); + if (end == preg->regparse + 1) { + preg->err = REG_ERR_BAD_COUNT; + return 0; + } + if (*end == '}') { + max = min; + } + else if (*end == '\0') { + preg->err = REG_ERR_UNMATCHED_BRACES; + return 0; + } + else { + preg->regparse = end; + max = strtoul(preg->regparse + 1, &end, 10); + if (*end != '}') { + preg->err = REG_ERR_UNMATCHED_BRACES; + return 0; + } + } + if (end == preg->regparse + 1) { + max = MAX_REP_COUNT; + } + else if (max < min || max >= 100) { + preg->err = REG_ERR_BAD_COUNT; + return 0; + } + if (min >= 100) { + preg->err = REG_ERR_BAD_COUNT; + return 0; + } + + preg->regparse = strchr(preg->regparse, '}'); + } + else { + min = (op == '+'); + max = (op == '?' ? 1 : MAX_REP_COUNT); + } + + if (preg->regparse[1] == '?') { + preg->regparse++; + next = reginsert(preg, flags & SIMPLE ? REPMIN : REPXMIN, 5, ret); + } + else { + next = reginsert(preg, flags & SIMPLE ? REP: REPX, 5, ret); + } + preg->program[ret + 2] = max; + preg->program[ret + 3] = min; + preg->program[ret + 4] = 0; + + *flagp = (min) ? (WORST|HASWIDTH) : (WORST|SPSTART); + + if (!(flags & SIMPLE)) { + int back = regnode(preg, BACK); + regtail(preg, back, ret); + regtail(preg, next, back); + } + + preg->regparse++; + if (ISMULT(*preg->regparse)) { + preg->err = REG_ERR_NESTED_COUNT; + return 0; + } + + return ret; +} + +static void reg_addrange(regex_t *preg, int lower, int upper) +{ + if (lower > upper) { + reg_addrange(preg, upper, lower); + } + + regc(preg, upper - lower + 1); + regc(preg, lower); +} + +static void reg_addrange_str(regex_t *preg, const char *str) +{ + while (*str) { + reg_addrange(preg, *str, *str); + str++; + } +} + +static int reg_utf8_tounicode_case(const char *s, int *uc, int upper) +{ + int l = utf8_tounicode(s, uc); + if (upper) { + *uc = utf8_upper(*uc); + } + return l; +} + +static int hexdigitval(int c) +{ + if (c >= '0' && c <= '9') + return c - '0'; + if (c >= 'a' && c <= 'f') + return c - 'a' + 10; + if (c >= 'A' && c <= 'F') + return c - 'A' + 10; + return -1; +} + +static int parse_hex(const char *s, int n, int *uc) +{ + int val = 0; + int k; + + for (k = 0; k < n; k++) { + int c = hexdigitval(*s++); + if (c == -1) { + break; + } + val = (val << 4) | c; + } + if (k) { + *uc = val; + } + return k; +} + +static int reg_decode_escape(const char *s, int *ch) +{ + int n; + const char *s0 = s; + + *ch = *s++; + + switch (*ch) { + case 'b': *ch = '\b'; break; + case 'e': *ch = 27; break; + case 'f': *ch = '\f'; break; + case 'n': *ch = '\n'; break; + case 'r': *ch = '\r'; break; + case 't': *ch = '\t'; break; + case 'v': *ch = '\v'; break; + case 'u': + if (*s == '{') { + + n = parse_hex(s + 1, 6, ch); + if (n > 0 && s[n + 1] == '}' && *ch >= 0 && *ch <= 0x1fffff) { + s += n + 2; + } + else { + + *ch = 'u'; + } + } + else if ((n = parse_hex(s, 4, ch)) > 0) { + s += n; + } + break; + case 'U': + if ((n = parse_hex(s, 8, ch)) > 0) { + s += n; + } + break; + case 'x': + if ((n = parse_hex(s, 2, ch)) > 0) { + s += n; + } + break; + case '\0': + s--; + *ch = '\\'; + break; + } + return s - s0; +} + +static int regatom(regex_t *preg, int *flagp) +{ + int ret; + int flags; + int nocase = (preg->cflags & REG_ICASE); + + int ch; + int n = reg_utf8_tounicode_case(preg->regparse, &ch, nocase); + + *flagp = WORST; + + preg->regparse += n; + switch (ch) { + + case '^': + ret = regnode(preg, BOL); + break; + case '$': + ret = regnode(preg, EOL); + break; + case '.': + ret = regnode(preg, ANY); + *flagp |= HASWIDTH|SIMPLE; + break; + case '[': { + const char *pattern = preg->regparse; + + if (*pattern == '^') { + ret = regnode(preg, ANYBUT); + pattern++; + } else + ret = regnode(preg, ANYOF); + + + if (*pattern == ']' || *pattern == '-') { + reg_addrange(preg, *pattern, *pattern); + pattern++; + } + + while (*pattern != ']') { + + int start; + int end; + + enum { + CC_ALPHA, CC_ALNUM, CC_SPACE, CC_BLANK, CC_UPPER, CC_LOWER, + CC_DIGIT, CC_XDIGIT, CC_CNTRL, CC_GRAPH, CC_PRINT, CC_PUNCT, + CC_NUM + }; + int cc; + + if (!*pattern) { + preg->err = REG_ERR_UNMATCHED_BRACKET; + return 0; + } + + pattern += reg_utf8_tounicode_case(pattern, &start, nocase); + if (start == '\\') { + + switch (*pattern) { + case 's': + pattern++; + cc = CC_SPACE; + goto cc_switch; + case 'd': + pattern++; + cc = CC_DIGIT; + goto cc_switch; + case 'w': + pattern++; + reg_addrange(preg, '_', '_'); + cc = CC_ALNUM; + goto cc_switch; + } + pattern += reg_decode_escape(pattern, &start); + if (start == 0) { + preg->err = REG_ERR_NULL_CHAR; + return 0; + } + if (start == '\\' && *pattern == 0) { + preg->err = REG_ERR_INVALID_ESCAPE; + return 0; + } + } + if (pattern[0] == '-' && pattern[1] && pattern[1] != ']') { + + pattern += utf8_tounicode(pattern, &end); + pattern += reg_utf8_tounicode_case(pattern, &end, nocase); + if (end == '\\') { + pattern += reg_decode_escape(pattern, &end); + if (end == 0) { + preg->err = REG_ERR_NULL_CHAR; + return 0; + } + if (end == '\\' && *pattern == 0) { + preg->err = REG_ERR_INVALID_ESCAPE; + return 0; + } + } + + reg_addrange(preg, start, end); + continue; + } + if (start == '[' && pattern[0] == ':') { + static const char *character_class[] = { + ":alpha:", ":alnum:", ":space:", ":blank:", ":upper:", ":lower:", + ":digit:", ":xdigit:", ":cntrl:", ":graph:", ":print:", ":punct:", + }; + + for (cc = 0; cc < CC_NUM; cc++) { + n = strlen(character_class[cc]); + if (strncmp(pattern, character_class[cc], n) == 0) { + if (pattern[n] != ']') { + preg->err = REG_ERR_UNMATCHED_BRACKET; + return 0; + } + + pattern += n + 1; + break; + } + } + if (cc != CC_NUM) { +cc_switch: + switch (cc) { + case CC_ALNUM: + reg_addrange(preg, '0', '9'); + + case CC_ALPHA: + if ((preg->cflags & REG_ICASE) == 0) { + reg_addrange(preg, 'a', 'z'); + } + reg_addrange(preg, 'A', 'Z'); + break; + case CC_SPACE: + reg_addrange_str(preg, " \t\r\n\f\v"); + break; + case CC_BLANK: + reg_addrange_str(preg, " \t"); + break; + case CC_UPPER: + reg_addrange(preg, 'A', 'Z'); + break; + case CC_LOWER: + reg_addrange(preg, 'a', 'z'); + break; + case CC_XDIGIT: + reg_addrange(preg, 'a', 'f'); + reg_addrange(preg, 'A', 'F'); + + case CC_DIGIT: + reg_addrange(preg, '0', '9'); + break; + case CC_CNTRL: + reg_addrange(preg, 0, 31); + reg_addrange(preg, 127, 127); + break; + case CC_PRINT: + reg_addrange(preg, ' ', '~'); + break; + case CC_GRAPH: + reg_addrange(preg, '!', '~'); + break; + case CC_PUNCT: + reg_addrange(preg, '!', '/'); + reg_addrange(preg, ':', '@'); + reg_addrange(preg, '[', '`'); + reg_addrange(preg, '{', '~'); + break; + } + continue; + } + } + + reg_addrange(preg, start, start); + } + regc(preg, '\0'); + + if (*pattern) { + pattern++; + } + preg->regparse = pattern; + + *flagp |= HASWIDTH|SIMPLE; + } + break; + case '(': + ret = reg(preg, 1, &flags); + if (ret == 0) + return 0; + *flagp |= flags&(HASWIDTH|SPSTART); + break; + case '\0': + case '|': + case ')': + preg->err = REG_ERR_INTERNAL; + return 0; + case '?': + case '+': + case '*': + case '{': + preg->err = REG_ERR_COUNT_FOLLOWS_NOTHING; + return 0; + case '\\': + ch = *preg->regparse++; + switch (ch) { + case '\0': + preg->err = REG_ERR_INVALID_ESCAPE; + return 0; + case 'A': + ret = regnode(preg, BOLX); + break; + case 'Z': + ret = regnode(preg, EOLX); + break; + case '<': + case 'm': + ret = regnode(preg, WORDA); + break; + case '>': + case 'M': + ret = regnode(preg, WORDZ); + break; + case 'd': + case 'D': + ret = regnode(preg, ch == 'd' ? ANYOF : ANYBUT); + reg_addrange(preg, '0', '9'); + regc(preg, '\0'); + *flagp |= HASWIDTH|SIMPLE; + break; + case 'w': + case 'W': + ret = regnode(preg, ch == 'w' ? ANYOF : ANYBUT); + if ((preg->cflags & REG_ICASE) == 0) { + reg_addrange(preg, 'a', 'z'); + } + reg_addrange(preg, 'A', 'Z'); + reg_addrange(preg, '0', '9'); + reg_addrange(preg, '_', '_'); + regc(preg, '\0'); + *flagp |= HASWIDTH|SIMPLE; + break; + case 's': + case 'S': + ret = regnode(preg, ch == 's' ? ANYOF : ANYBUT); + reg_addrange_str(preg," \t\r\n\f\v"); + regc(preg, '\0'); + *flagp |= HASWIDTH|SIMPLE; + break; + + default: + + + preg->regparse--; + goto de_fault; + } + break; + de_fault: + default: { + int added = 0; + + + preg->regparse -= n; + + ret = regnode(preg, EXACTLY); + + + + while (*preg->regparse && strchr(META, *preg->regparse) == NULL) { + n = reg_utf8_tounicode_case(preg->regparse, &ch, (preg->cflags & REG_ICASE)); + if (ch == '\\' && preg->regparse[n]) { + if (strchr("<>mMwWdDsSAZ", preg->regparse[n])) { + + break; + } + n += reg_decode_escape(preg->regparse + n, &ch); + if (ch == 0) { + preg->err = REG_ERR_NULL_CHAR; + return 0; + } + } + + + if (ISMULT(preg->regparse[n])) { + + if (added) { + + break; + } + + regc(preg, ch); + added++; + preg->regparse += n; + break; + } + + + regc(preg, ch); + added++; + preg->regparse += n; + } + regc(preg, '\0'); + + *flagp |= HASWIDTH; + if (added == 1) + *flagp |= SIMPLE; + break; + } + break; + } + + return(ret); +} + +static void reg_grow(regex_t *preg, int n) +{ + if (preg->p + n >= preg->proglen) { + preg->proglen = (preg->p + n) * 2; + preg->program = realloc(preg->program, preg->proglen * sizeof(int)); + } +} + + +static int regnode(regex_t *preg, int op) +{ + reg_grow(preg, 2); + + + preg->program[preg->p++] = op; + preg->program[preg->p++] = 0; + + + return preg->p - 2; +} + +static void regc(regex_t *preg, int b ) +{ + reg_grow(preg, 1); + preg->program[preg->p++] = b; +} + +static int reginsert(regex_t *preg, int op, int size, int opnd ) +{ + reg_grow(preg, size); + + + memmove(preg->program + opnd + size, preg->program + opnd, sizeof(int) * (preg->p - opnd)); + + memset(preg->program + opnd, 0, sizeof(int) * size); + + preg->program[opnd] = op; + + preg->p += size; + + return opnd + size; +} + +static void regtail(regex_t *preg, int p, int val) +{ + int scan; + int temp; + int offset; + + + scan = p; + for (;;) { + temp = regnext(preg, scan); + if (temp == 0) + break; + scan = temp; + } + + if (OP(preg, scan) == BACK) + offset = scan - val; + else + offset = val - scan; + + preg->program[scan + 1] = offset; +} + + +static void regoptail(regex_t *preg, int p, int val ) +{ + + if (p != 0 && OP(preg, p) == BRANCH) { + regtail(preg, OPERAND(p), val); + } +} + + +static int regtry(regex_t *preg, const char *string ); +static int regmatch(regex_t *preg, int prog); +static int regrepeat(regex_t *preg, int p, int max); + +int jim_regexec(regex_t *preg, const char *string, size_t nmatch, regmatch_t pmatch[], int eflags) +{ + const char *s; + int scan; + + + if (preg == NULL || preg->program == NULL || string == NULL) { + return REG_ERR_NULL_ARGUMENT; + } + + + if (*preg->program != REG_MAGIC) { + return REG_ERR_CORRUPTED; + } + +#ifdef DEBUG + fprintf(stderr, "regexec: %s\n", string); + regdump(preg); +#endif + + preg->eflags = eflags; + preg->pmatch = pmatch; + preg->nmatch = nmatch; + preg->start = string; + + + for (scan = OPERAND(1); scan != 0; scan += regopsize(preg, scan)) { + int op = OP(preg, scan); + if (op == END) + break; + if (op == REPX || op == REPXMIN) + preg->program[scan + 4] = 0; + } + + + if (preg->regmust != 0) { + s = string; + while ((s = str_find(s, preg->program[preg->regmust], preg->cflags & REG_ICASE)) != NULL) { + if (prefix_cmp(preg->program + preg->regmust, preg->regmlen, s, preg->cflags & REG_ICASE) >= 0) { + break; + } + s++; + } + if (s == NULL) + return REG_NOMATCH; + } + + + preg->regbol = string; + + + if (preg->reganch) { + if (eflags & REG_NOTBOL) { + + goto nextline; + } + while (1) { + if (regtry(preg, string)) { + return REG_NOERROR; + } + if (*string) { +nextline: + if (preg->cflags & REG_NEWLINE) { + + string = strchr(string, '\n'); + if (string) { + preg->regbol = ++string; + continue; + } + } + } + return REG_NOMATCH; + } + } + + + s = string; + if (preg->regstart != '\0') { + + while ((s = str_find(s, preg->regstart, preg->cflags & REG_ICASE)) != NULL) { + if (regtry(preg, s)) + return REG_NOERROR; + s++; + } + } + else + + while (1) { + if (regtry(preg, s)) + return REG_NOERROR; + if (*s == '\0') { + break; + } + else { + int c; + s += utf8_tounicode(s, &c); + } + } + + + return REG_NOMATCH; +} + + +static int regtry( regex_t *preg, const char *string ) +{ + int i; + + preg->reginput = string; + + for (i = 0; i < preg->nmatch; i++) { + preg->pmatch[i].rm_so = -1; + preg->pmatch[i].rm_eo = -1; + } + if (regmatch(preg, 1)) { + preg->pmatch[0].rm_so = string - preg->start; + preg->pmatch[0].rm_eo = preg->reginput - preg->start; + return(1); + } else + return(0); +} + +static int prefix_cmp(const int *prog, int proglen, const char *string, int nocase) +{ + const char *s = string; + while (proglen && *s) { + int ch; + int n = reg_utf8_tounicode_case(s, &ch, nocase); + if (ch != *prog) { + return -1; + } + prog++; + s += n; + proglen--; + } + if (proglen == 0) { + return s - string; + } + return -1; +} + +static int reg_range_find(const int *range, int c) +{ + while (*range) { + + if (c >= range[1] && c <= (range[0] + range[1] - 1)) { + return 1; + } + range += 2; + } + return 0; +} + +static const char *str_find(const char *string, int c, int nocase) +{ + if (nocase) { + + c = utf8_upper(c); + } + while (*string) { + int ch; + int n = reg_utf8_tounicode_case(string, &ch, nocase); + if (c == ch) { + return string; + } + string += n; + } + return NULL; +} + +static int reg_iseol(regex_t *preg, int ch) +{ + if (preg->cflags & REG_NEWLINE) { + return ch == '\0' || ch == '\n'; + } + else { + return ch == '\0'; + } +} + +static int regmatchsimplerepeat(regex_t *preg, int scan, int matchmin) +{ + int nextch = '\0'; + const char *save; + int no; + int c; + + int max = preg->program[scan + 2]; + int min = preg->program[scan + 3]; + int next = regnext(preg, scan); + + if (OP(preg, next) == EXACTLY) { + nextch = preg->program[OPERAND(next)]; + } + save = preg->reginput; + no = regrepeat(preg, scan + 5, max); + if (no < min) { + return 0; + } + if (matchmin) { + + max = no; + no = min; + } + + while (1) { + if (matchmin) { + if (no > max) { + break; + } + } + else { + if (no < min) { + break; + } + } + preg->reginput = save + utf8_index(save, no); + reg_utf8_tounicode_case(preg->reginput, &c, (preg->cflags & REG_ICASE)); + + if (reg_iseol(preg, nextch) || c == nextch) { + if (regmatch(preg, next)) { + return(1); + } + } + if (matchmin) { + + no++; + } + else { + + no--; + } + } + return(0); +} + +static int regmatchrepeat(regex_t *preg, int scan, int matchmin) +{ + int *scanpt = preg->program + scan; + + int max = scanpt[2]; + int min = scanpt[3]; + + + if (scanpt[4] < min) { + + scanpt[4]++; + if (regmatch(preg, scan + 5)) { + return 1; + } + scanpt[4]--; + return 0; + } + if (scanpt[4] > max) { + return 0; + } + + if (matchmin) { + + if (regmatch(preg, regnext(preg, scan))) { + return 1; + } + + scanpt[4]++; + if (regmatch(preg, scan + 5)) { + return 1; + } + scanpt[4]--; + return 0; + } + + if (scanpt[4] < max) { + scanpt[4]++; + if (regmatch(preg, scan + 5)) { + return 1; + } + scanpt[4]--; + } + + return regmatch(preg, regnext(preg, scan)); +} + + +static int regmatch(regex_t *preg, int prog) +{ + int scan; + int next; + const char *save; + + scan = prog; + +#ifdef DEBUG + if (scan != 0 && regnarrate) + fprintf(stderr, "%s(\n", regprop(scan)); +#endif + while (scan != 0) { + int n; + int c; +#ifdef DEBUG + if (regnarrate) { + fprintf(stderr, "%3d: %s...\n", scan, regprop(OP(preg, scan))); + } +#endif + next = regnext(preg, scan); + n = reg_utf8_tounicode_case(preg->reginput, &c, (preg->cflags & REG_ICASE)); + + switch (OP(preg, scan)) { + case BOLX: + if ((preg->eflags & REG_NOTBOL)) { + return(0); + } + + case BOL: + if (preg->reginput != preg->regbol) { + return(0); + } + break; + case EOLX: + if (c != 0) { + + return 0; + } + break; + case EOL: + if (!reg_iseol(preg, c)) { + return(0); + } + break; + case WORDA: + + if ((!isalnum(UCHAR(c))) && c != '_') + return(0); + + if (preg->reginput > preg->regbol && + (isalnum(UCHAR(preg->reginput[-1])) || preg->reginput[-1] == '_')) + return(0); + break; + case WORDZ: + + if (preg->reginput > preg->regbol) { + + if (reg_iseol(preg, c) || !(isalnum(UCHAR(c)) || c == '_')) { + c = preg->reginput[-1]; + + if (isalnum(UCHAR(c)) || c == '_') { + break; + } + } + } + + return(0); + + case ANY: + if (reg_iseol(preg, c)) + return 0; + preg->reginput += n; + break; + case EXACTLY: { + int opnd; + int len; + int slen; + + opnd = OPERAND(scan); + len = str_int_len(preg->program + opnd); + + slen = prefix_cmp(preg->program + opnd, len, preg->reginput, preg->cflags & REG_ICASE); + if (slen < 0) { + return(0); + } + preg->reginput += slen; + } + break; + case ANYOF: + if (reg_iseol(preg, c) || reg_range_find(preg->program + OPERAND(scan), c) == 0) { + return(0); + } + preg->reginput += n; + break; + case ANYBUT: + if (reg_iseol(preg, c) || reg_range_find(preg->program + OPERAND(scan), c) != 0) { + return(0); + } + preg->reginput += n; + break; + case NOTHING: + break; + case BACK: + break; + case BRANCH: + if (OP(preg, next) != BRANCH) + next = OPERAND(scan); + else { + do { + save = preg->reginput; + if (regmatch(preg, OPERAND(scan))) { + return(1); + } + preg->reginput = save; + scan = regnext(preg, scan); + } while (scan != 0 && OP(preg, scan) == BRANCH); + return(0); + + } + break; + case REP: + case REPMIN: + return regmatchsimplerepeat(preg, scan, OP(preg, scan) == REPMIN); + + case REPX: + case REPXMIN: + return regmatchrepeat(preg, scan, OP(preg, scan) == REPXMIN); + + case END: + return 1; + + case OPENNC: + case CLOSENC: + return regmatch(preg, next); + + default: + if (OP(preg, scan) >= OPEN+1 && OP(preg, scan) < CLOSE_END) { + save = preg->reginput; + if (regmatch(preg, next)) { + if (OP(preg, scan) < CLOSE) { + int no = OP(preg, scan) - OPEN; + if (no < preg->nmatch && preg->pmatch[no].rm_so == -1) { + preg->pmatch[no].rm_so = save - preg->start; + } + } + else { + int no = OP(preg, scan) - CLOSE; + if (no < preg->nmatch && preg->pmatch[no].rm_eo == -1) { + preg->pmatch[no].rm_eo = save - preg->start; + } + } + return(1); + } + + preg->reginput = save; + return(0); + } + return REG_ERR_INTERNAL; + } + + scan = next; + } + + return REG_ERR_INTERNAL; +} + +static int regrepeat(regex_t *preg, int p, int max) +{ + int count = 0; + const char *scan; + int opnd; + int ch; + int n; + + scan = preg->reginput; + opnd = OPERAND(p); + switch (OP(preg, p)) { + case ANY: + while (!reg_iseol(preg, *scan) && count < max) { + count++; + scan += utf8_charlen(*scan); + } + break; + case EXACTLY: + while (count < max) { + n = reg_utf8_tounicode_case(scan, &ch, preg->cflags & REG_ICASE); + if (preg->program[opnd] != ch) { + break; + } + count++; + scan += n; + } + break; + case ANYOF: + while (count < max) { + n = reg_utf8_tounicode_case(scan, &ch, preg->cflags & REG_ICASE); + if (reg_iseol(preg, ch) || reg_range_find(preg->program + opnd, ch) == 0) { + break; + } + count++; + scan += n; + } + break; + case ANYBUT: + while (count < max) { + n = reg_utf8_tounicode_case(scan, &ch, preg->cflags & REG_ICASE); + if (reg_iseol(preg, ch) || reg_range_find(preg->program + opnd, ch) != 0) { + break; + } + count++; + scan += n; + } + break; + default: + preg->err = REG_ERR_INTERNAL; + count = 0; + break; + } + preg->reginput = scan; + + return(count); +} + +static int regnext(regex_t *preg, int p ) +{ + int offset; + + offset = NEXT(preg, p); + + if (offset == 0) + return 0; + + if (OP(preg, p) == BACK) + return(p-offset); + else + return(p+offset); +} + +static int regopsize(regex_t *preg, int p ) +{ + + switch (OP(preg, p)) { + case REP: + case REPMIN: + case REPX: + case REPXMIN: + return 5; + + case ANYOF: + case ANYBUT: + case EXACTLY: { + int s = p + 2; + while (preg->program[s++]) { + } + return s - p; + } + } + return 2; +} + + +size_t jim_regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size) +{ + static const char *error_strings[] = { + "success", + "no match", + "bad pattern", + "null argument", + "unknown error", + "too big", + "out of memory", + "too many ()", + "parentheses () not balanced", + "braces {} not balanced", + "invalid repetition count(s)", + "extra characters", + "*+ of empty atom", + "nested count", + "internal error", + "count follows nothing", + "invalid escape \\ sequence", + "corrupted program", + "contains null char", + "brackets [] not balanced", + }; + const char *err; + + if (errcode < 0 || errcode >= REG_ERR_NUM) { + err = "Bad error code"; + } + else { + err = error_strings[errcode]; + } + + return snprintf(errbuf, errbuf_size, "%s", err); +} + +void jim_regfree(regex_t *preg) +{ + free(preg->program); +} + +#endif +#include + +void Jim_SetResultErrno(Jim_Interp *interp, const char *msg) +{ + Jim_SetResultFormatted(interp, "%s: %s", msg, strerror(Jim_Errno())); +} + +#if defined(_WIN32) || defined(WIN32) +#include + +int Jim_Errno(void) +{ + switch (GetLastError()) { + case ERROR_FILE_NOT_FOUND: return ENOENT; + case ERROR_PATH_NOT_FOUND: return ENOENT; + case ERROR_TOO_MANY_OPEN_FILES: return EMFILE; + case ERROR_ACCESS_DENIED: return EACCES; + case ERROR_INVALID_HANDLE: return EBADF; + case ERROR_BAD_ENVIRONMENT: return E2BIG; + case ERROR_BAD_FORMAT: return ENOEXEC; + case ERROR_INVALID_ACCESS: return EACCES; + case ERROR_INVALID_DRIVE: return ENOENT; + case ERROR_CURRENT_DIRECTORY: return EACCES; + case ERROR_NOT_SAME_DEVICE: return EXDEV; + case ERROR_NO_MORE_FILES: return ENOENT; + case ERROR_WRITE_PROTECT: return EROFS; + case ERROR_BAD_UNIT: return ENXIO; + case ERROR_NOT_READY: return EBUSY; + case ERROR_BAD_COMMAND: return EIO; + case ERROR_CRC: return EIO; + case ERROR_BAD_LENGTH: return EIO; + case ERROR_SEEK: return EIO; + case ERROR_WRITE_FAULT: return EIO; + case ERROR_READ_FAULT: return EIO; + case ERROR_GEN_FAILURE: return EIO; + case ERROR_SHARING_VIOLATION: return EACCES; + case ERROR_LOCK_VIOLATION: return EACCES; + case ERROR_SHARING_BUFFER_EXCEEDED: return ENFILE; + case ERROR_HANDLE_DISK_FULL: return ENOSPC; + case ERROR_NOT_SUPPORTED: return ENODEV; + case ERROR_REM_NOT_LIST: return EBUSY; + case ERROR_DUP_NAME: return EEXIST; + case ERROR_BAD_NETPATH: return ENOENT; + case ERROR_NETWORK_BUSY: return EBUSY; + case ERROR_DEV_NOT_EXIST: return ENODEV; + case ERROR_TOO_MANY_CMDS: return EAGAIN; + case ERROR_ADAP_HDW_ERR: return EIO; + case ERROR_BAD_NET_RESP: return EIO; + case ERROR_UNEXP_NET_ERR: return EIO; + case ERROR_NETNAME_DELETED: return ENOENT; + case ERROR_NETWORK_ACCESS_DENIED: return EACCES; + case ERROR_BAD_DEV_TYPE: return ENODEV; + case ERROR_BAD_NET_NAME: return ENOENT; + case ERROR_TOO_MANY_NAMES: return ENFILE; + case ERROR_TOO_MANY_SESS: return EIO; + case ERROR_SHARING_PAUSED: return EAGAIN; + case ERROR_REDIR_PAUSED: return EAGAIN; + case ERROR_FILE_EXISTS: return EEXIST; + case ERROR_CANNOT_MAKE: return ENOSPC; + case ERROR_OUT_OF_STRUCTURES: return ENFILE; + case ERROR_ALREADY_ASSIGNED: return EEXIST; + case ERROR_INVALID_PASSWORD: return EPERM; + case ERROR_NET_WRITE_FAULT: return EIO; + case ERROR_NO_PROC_SLOTS: return EAGAIN; + case ERROR_DISK_CHANGE: return EXDEV; + case ERROR_BROKEN_PIPE: return EPIPE; + case ERROR_OPEN_FAILED: return ENOENT; + case ERROR_DISK_FULL: return ENOSPC; + case ERROR_NO_MORE_SEARCH_HANDLES: return EMFILE; + case ERROR_INVALID_TARGET_HANDLE: return EBADF; + case ERROR_INVALID_NAME: return ENOENT; + case ERROR_PROC_NOT_FOUND: return ESRCH; + case ERROR_WAIT_NO_CHILDREN: return ECHILD; + case ERROR_CHILD_NOT_COMPLETE: return ECHILD; + case ERROR_DIRECT_ACCESS_HANDLE: return EBADF; + case ERROR_SEEK_ON_DEVICE: return ESPIPE; + case ERROR_BUSY_DRIVE: return EAGAIN; + case ERROR_DIR_NOT_EMPTY: return EEXIST; + case ERROR_NOT_LOCKED: return EACCES; + case ERROR_BAD_PATHNAME: return ENOENT; + case ERROR_LOCK_FAILED: return EACCES; + case ERROR_ALREADY_EXISTS: return EEXIST; + case ERROR_FILENAME_EXCED_RANGE: return ENAMETOOLONG; + case ERROR_BAD_PIPE: return EPIPE; + case ERROR_PIPE_BUSY: return EAGAIN; + case ERROR_PIPE_NOT_CONNECTED: return EPIPE; + case ERROR_DIRECTORY: return ENOTDIR; + } + return EINVAL; +} + +long JimProcessPid(phandle_t pid) +{ + if (pid == INVALID_HANDLE_VALUE) { + return -1; + } + return GetProcessId(pid); +} + +phandle_t JimWaitPid(long pid, int *status, int nohang) +{ + if (pid > 0) { + HANDLE h = OpenProcess(PROCESS_QUERY_INFORMATION | SYNCHRONIZE, FALSE, pid); + if (h) { + long pid = waitpid(h, status, nohang); + CloseHandle(h); + if (pid > 0) { + return h; + } + } + } + return JIM_BAD_PHANDLE; +} + +long waitpid(phandle_t phandle, int *status, int nohang) +{ + long pid; + DWORD ret = WaitForSingleObject(phandle, nohang ? 0 : INFINITE); + if (ret == WAIT_TIMEOUT || ret == WAIT_FAILED) { + + return -1; + } + GetExitCodeProcess(phandle, &ret); + *status = ret; + + pid = GetProcessId(phandle); + CloseHandle(phandle); + return pid; +} + +int Jim_MakeTempFile(Jim_Interp *interp, const char *filename_template, int unlink_file) +{ + char name[MAX_PATH]; + HANDLE handle; + + if (!GetTempPath(MAX_PATH, name) || !GetTempFileName(name, filename_template ? filename_template : "JIM", 0, name)) { + return -1; + } + + handle = CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0, NULL, + CREATE_ALWAYS, FILE_ATTRIBUTE_TEMPORARY | (unlink_file ? FILE_FLAG_DELETE_ON_CLOSE : 0), + NULL); + + if (handle == INVALID_HANDLE_VALUE) { + goto error; + } + + Jim_SetResultString(interp, name, -1); + return _open_osfhandle((intptr_t)handle, _O_RDWR | _O_TEXT); + + error: + Jim_SetResultErrno(interp, name); + DeleteFile(name); + return -1; +} + +int Jim_OpenForWrite(const char *filename, int append) +{ + if (strcmp(filename, "/dev/null") == 0) { + filename = "nul:"; + } + int fd = _open(filename, _O_WRONLY | _O_CREAT | _O_TEXT | (append ? _O_APPEND : _O_TRUNC), _S_IREAD | _S_IWRITE); + if (fd >= 0 && append) { + + _lseek(fd, 0L, SEEK_END); + } + return fd; +} + +int Jim_OpenForRead(const char *filename) +{ + if (strcmp(filename, "/dev/null") == 0) { + filename = "nul:"; + } + return _open(filename, _O_RDONLY | _O_TEXT, 0); +} + +#elif defined(HAVE_UNISTD_H) + + + +int Jim_MakeTempFile(Jim_Interp *interp, const char *filename_template, int unlink_file) +{ + int fd; + mode_t mask; + Jim_Obj *filenameObj; + + if (filename_template == NULL) { + const char *tmpdir = getenv("TMPDIR"); + if (tmpdir == NULL || *tmpdir == '\0' || access(tmpdir, W_OK) != 0) { + tmpdir = "/tmp/"; + } + filenameObj = Jim_NewStringObj(interp, tmpdir, -1); + if (tmpdir[0] && tmpdir[strlen(tmpdir) - 1] != '/') { + Jim_AppendString(interp, filenameObj, "/", 1); + } + Jim_AppendString(interp, filenameObj, "tcl.tmp.XXXXXX", -1); + } + else { + filenameObj = Jim_NewStringObj(interp, filename_template, -1); + } + + +#ifdef HAVE_UMASK + mask = umask(S_IXUSR | S_IRWXG | S_IRWXO); +#endif +#ifdef HAVE_MKSTEMP + fd = mkstemp(filenameObj->bytes); +#else + if (mktemp(filenameObj->bytes) == NULL) { + fd = -1; + } + else { + fd = open(filenameObj->bytes, O_RDWR | O_CREAT | O_TRUNC); + } +#endif +#ifdef HAVE_UMASK + umask(mask); +#endif + if (fd < 0) { + Jim_SetResultErrno(interp, Jim_String(filenameObj)); + Jim_FreeNewObj(interp, filenameObj); + return -1; + } + if (unlink_file) { + remove(Jim_String(filenameObj)); + } + + Jim_SetResult(interp, filenameObj); + return fd; +} + +int Jim_OpenForWrite(const char *filename, int append) +{ + return open(filename, O_WRONLY | O_CREAT | (append ? O_APPEND : O_TRUNC), 0666); +} + +int Jim_OpenForRead(const char *filename) +{ + return open(filename, O_RDONLY, 0); +} + +#endif + +#if defined(_WIN32) || defined(WIN32) +#ifndef STRICT +#define STRICT +#endif +#define WIN32_LEAN_AND_MEAN +#include + +#if defined(HAVE_DLOPEN_COMPAT) +void *dlopen(const char *path, int mode) +{ + JIM_NOTUSED(mode); + + return (void *)LoadLibraryA(path); +} + +int dlclose(void *handle) +{ + FreeLibrary((HANDLE)handle); + return 0; +} + +void *dlsym(void *handle, const char *symbol) +{ + return GetProcAddress((HMODULE)handle, symbol); +} + +char *dlerror(void) +{ + static char msg[121]; + FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), + LANG_NEUTRAL, msg, sizeof(msg) - 1, NULL); + return msg; +} +#endif + +#ifdef _MSC_VER + +#include + + +int gettimeofday(struct timeval *tv, void *unused) +{ + struct _timeb tb; + + _ftime(&tb); + tv->tv_sec = tb.time; + tv->tv_usec = tb.millitm * 1000; + + return 0; +} + + +DIR *opendir(const char *name) +{ + DIR *dir = 0; + + if (name && name[0]) { + size_t base_length = strlen(name); + const char *all = + strchr("/\\", name[base_length - 1]) ? "*" : "/*"; + + if ((dir = (DIR *) Jim_Alloc(sizeof *dir)) != 0 && + (dir->name = (char *)Jim_Alloc(base_length + strlen(all) + 1)) != 0) { + strcat(strcpy(dir->name, name), all); + + if ((dir->handle = (long)_findfirst(dir->name, &dir->info)) != -1) + dir->result.d_name = 0; + else { + Jim_Free(dir->name); + Jim_Free(dir); + dir = 0; + } + } + else { + Jim_Free(dir); + dir = 0; + errno = ENOMEM; + } + } + else { + errno = EINVAL; + } + return dir; +} + +int closedir(DIR * dir) +{ + int result = -1; + + if (dir) { + if (dir->handle != -1) + result = _findclose(dir->handle); + Jim_Free(dir->name); + Jim_Free(dir); + } + if (result == -1) + errno = EBADF; + return result; +} + +struct dirent *readdir(DIR * dir) +{ + struct dirent *result = 0; + + if (dir && dir->handle != -1) { + if (!dir->result.d_name || _findnext(dir->handle, &dir->info) != -1) { + result = &dir->result; + result->d_name = dir->info.name; + } + } + else { + errno = EBADF; + } + return result; +} +#endif +#endif +#include +#include + + + + + + +#ifndef SIGPIPE +#define SIGPIPE 13 +#endif +#ifndef SIGINT +#define SIGINT 2 +#endif + +const char *Jim_SignalId(int sig) +{ + static char buf[10]; + switch (sig) { + case SIGINT: return "SIGINT"; + case SIGPIPE: return "SIGPIPE"; + + } + snprintf(buf, sizeof(buf), "%d", sig); + return buf; +} +#ifndef JIM_BOOTSTRAP_LIB_ONLY +#include +#include +#include + + +#ifdef USE_LINENOISE +#ifdef HAVE_UNISTD_H + #include +#endif +#ifdef HAVE_SYS_STAT_H + #include +#endif +#include "linenoise.h" +#else +#define MAX_LINE_LEN 512 +#endif + +#ifdef USE_LINENOISE +struct JimCompletionInfo { + Jim_Interp *interp; + Jim_Obj *completion_command; + Jim_Obj *hints_command; + +}; + +static struct JimCompletionInfo *JimGetCompletionInfo(Jim_Interp *interp); +static void JimCompletionCallback(const char *prefix, linenoiseCompletions *comp, void *userdata); +static const char completion_callback_assoc_key[] = "interactive-completion"; +static char *JimHintsCallback(const char *prefix, int *color, int *bold, void *userdata); +static void JimFreeHintsCallback(void *hint, void *userdata); +#endif + +char *Jim_HistoryGetline(Jim_Interp *interp, const char *prompt) +{ +#ifdef USE_LINENOISE + struct JimCompletionInfo *compinfo = JimGetCompletionInfo(interp); + char *result; + Jim_Obj *objPtr; + long mlmode = 0; + if (compinfo->completion_command) { + linenoiseSetCompletionCallback(JimCompletionCallback, compinfo); + } + if (compinfo->hints_command) { + linenoiseSetHintsCallback(JimHintsCallback, compinfo); + linenoiseSetFreeHintsCallback(JimFreeHintsCallback); + } + objPtr = Jim_GetVariableStr(interp, "history::multiline", JIM_NONE); + if (objPtr && Jim_GetLong(interp, objPtr, &mlmode) == JIM_NONE) { + linenoiseSetMultiLine(mlmode); + } + + result = linenoise(prompt); + + linenoiseSetCompletionCallback(NULL, NULL); + linenoiseSetHintsCallback(NULL, NULL); + linenoiseSetFreeHintsCallback(NULL); + return result; +#else + int len; + char *line = Jim_Alloc(MAX_LINE_LEN); + + fputs(prompt, stdout); + fflush(stdout); + + if (fgets(line, MAX_LINE_LEN, stdin) == NULL) { + Jim_Free(line); + return NULL; + } + len = strlen(line); + if (len && line[len - 1] == '\n') { + line[len - 1] = '\0'; + } + return line; +#endif +} + +void Jim_HistoryLoad(const char *filename) +{ +#ifdef USE_LINENOISE + linenoiseHistoryLoad(filename); +#endif +} + +void Jim_HistoryAdd(const char *line) +{ +#ifdef USE_LINENOISE + linenoiseHistoryAdd(line); +#endif +} + +void Jim_HistorySave(const char *filename) +{ +#ifdef USE_LINENOISE +#ifdef HAVE_UMASK + mode_t mask; + + mask = umask(S_IXUSR | S_IRWXG | S_IRWXO); +#endif + linenoiseHistorySave(filename); +#ifdef HAVE_UMASK + umask(mask); +#endif +#endif +} + +void Jim_HistoryShow(void) +{ +#ifdef USE_LINENOISE + + int i; + int len; + char **history = linenoiseHistory(&len); + for (i = 0; i < len; i++) { + printf("%4d %s\n", i + 1, history[i]); + } +#endif +} + +void Jim_HistorySetMaxLen(int length) +{ +#ifdef USE_LINENOISE + linenoiseHistorySetMaxLen(length); +#endif +} + +int Jim_HistoryGetMaxLen(void) +{ +#ifdef USE_LINENOISE + return linenoiseHistoryGetMaxLen(); +#endif + return 0; +} + +#ifdef USE_LINENOISE +static void JimCompletionCallback(const char *prefix, linenoiseCompletions *comp, void *userdata) +{ + struct JimCompletionInfo *info = (struct JimCompletionInfo *)userdata; + Jim_Obj *objv[2]; + int ret; + + objv[0] = info->completion_command; + objv[1] = Jim_NewStringObj(info->interp, prefix, -1); + + ret = Jim_EvalObjVector(info->interp, 2, objv); + + + if (ret == JIM_OK) { + int i; + Jim_Obj *listObj = Jim_GetResult(info->interp); + int len = Jim_ListLength(info->interp, listObj); + for (i = 0; i < len; i++) { + linenoiseAddCompletion(comp, Jim_String(Jim_ListGetIndex(info->interp, listObj, i))); + } + } +} + +static char *JimHintsCallback(const char *prefix, int *color, int *bold, void *userdata) +{ + struct JimCompletionInfo *info = (struct JimCompletionInfo *)userdata; + Jim_Obj *objv[2]; + int ret; + char *result = NULL; + + objv[0] = info->hints_command; + objv[1] = Jim_NewStringObj(info->interp, prefix, -1); + + ret = Jim_EvalObjVector(info->interp, 2, objv); + + + if (ret == JIM_OK) { + Jim_Obj *listObj = Jim_GetResult(info->interp); + Jim_IncrRefCount(listObj); + + int len = Jim_ListLength(info->interp, listObj); + if (len >= 1) { + long x; + result = Jim_StrDup(Jim_String(Jim_ListGetIndex(info->interp, listObj, 0))); + if (len >= 2 && Jim_GetLong(info->interp, Jim_ListGetIndex(info->interp, listObj, 1), &x) == JIM_OK) { + *color = x; + } + if (len >= 3 && Jim_GetLong(info->interp, Jim_ListGetIndex(info->interp, listObj, 2), &x) == JIM_OK) { + *bold = x; + } + } + Jim_DecrRefCount(info->interp, listObj); + } + return result; +} + +static void JimFreeHintsCallback(void *hint, void *userdata) +{ + Jim_Free(hint); +} + +static void JimHistoryFreeCompletion(Jim_Interp *interp, void *data) +{ + struct JimCompletionInfo *compinfo = data; + + if (compinfo->completion_command) { + Jim_DecrRefCount(interp, compinfo->completion_command); + } + if (compinfo->hints_command) { + Jim_DecrRefCount(interp, compinfo->hints_command); + } + + Jim_Free(compinfo); +} + +static struct JimCompletionInfo *JimGetCompletionInfo(Jim_Interp *interp) +{ + struct JimCompletionInfo *compinfo = Jim_GetAssocData(interp, completion_callback_assoc_key); + if (compinfo == NULL) { + compinfo = Jim_Alloc(sizeof(*compinfo)); + compinfo->interp = interp; + compinfo->completion_command = NULL; + compinfo->hints_command = NULL; + Jim_SetAssocData(interp, completion_callback_assoc_key, JimHistoryFreeCompletion, compinfo); + } + return compinfo; +} +#endif + +void Jim_HistorySetCompletion(Jim_Interp *interp, Jim_Obj *completionCommandObj) +{ +#ifdef USE_LINENOISE + struct JimCompletionInfo *compinfo = JimGetCompletionInfo(interp); + + if (completionCommandObj) { + + Jim_IncrRefCount(completionCommandObj); + } + if (compinfo->completion_command) { + Jim_DecrRefCount(interp, compinfo->completion_command); + } + compinfo->completion_command = completionCommandObj; +#endif +} + +void Jim_HistorySetHints(Jim_Interp *interp, Jim_Obj *hintsCommandObj) +{ +#ifdef USE_LINENOISE + struct JimCompletionInfo *compinfo = JimGetCompletionInfo(interp); + + if (hintsCommandObj) { + + Jim_IncrRefCount(hintsCommandObj); + } + if (compinfo->hints_command) { + Jim_DecrRefCount(interp, compinfo->hints_command); + } + compinfo->hints_command = hintsCommandObj; +#endif +} + +int Jim_InteractivePrompt(Jim_Interp *interp) +{ + int retcode = JIM_OK; + char *history_file = NULL; +#ifdef USE_LINENOISE + const char *home; + + home = getenv("HOME"); + if (home && isatty(STDIN_FILENO)) { + int history_len = strlen(home) + sizeof("/.jim_history"); + history_file = Jim_Alloc(history_len); + snprintf(history_file, history_len, "%s/.jim_history", home); + Jim_HistoryLoad(history_file); + } + + Jim_HistorySetCompletion(interp, Jim_NewStringObj(interp, "tcl::autocomplete", -1)); + Jim_HistorySetHints(interp, Jim_NewStringObj(interp, "tcl::stdhint", -1)); +#endif + + printf("Welcome to Jim version %d.%d\n", + JIM_VERSION / 100, JIM_VERSION % 100); + Jim_SetVariableStrWithStr(interp, JIM_INTERACTIVE, "1"); + + while (1) { + Jim_Obj *scriptObjPtr; + const char *result; + int reslen; + char prompt[20]; + + if (retcode != JIM_OK) { + const char *retcodestr = Jim_ReturnCode(retcode); + + if (*retcodestr == '?') { + snprintf(prompt, sizeof(prompt) - 3, "[%d] . ", retcode); + } + else { + snprintf(prompt, sizeof(prompt) - 3, "[%s] . ", retcodestr); + } + } + else { + strcpy(prompt, ". "); + } + + scriptObjPtr = Jim_NewStringObj(interp, "", 0); + Jim_IncrRefCount(scriptObjPtr); + while (1) { + char state; + char *line; + + line = Jim_HistoryGetline(interp, prompt); + if (line == NULL) { + if (errno == EINTR) { + continue; + } + Jim_DecrRefCount(interp, scriptObjPtr); + retcode = JIM_OK; + goto out; + } + if (Jim_Length(scriptObjPtr) != 0) { + + Jim_AppendString(interp, scriptObjPtr, "\n", 1); + } + Jim_AppendString(interp, scriptObjPtr, line, -1); + Jim_Free(line); + if (Jim_ScriptIsComplete(interp, scriptObjPtr, &state)) + break; + + snprintf(prompt, sizeof(prompt), "%c> ", state); + } +#ifdef USE_LINENOISE + if (strcmp(Jim_String(scriptObjPtr), "h") == 0) { + + Jim_HistoryShow(); + Jim_DecrRefCount(interp, scriptObjPtr); + continue; + } + + Jim_HistoryAdd(Jim_String(scriptObjPtr)); + if (history_file) { + Jim_HistorySave(history_file); + } +#endif + retcode = Jim_EvalObj(interp, scriptObjPtr); + Jim_DecrRefCount(interp, scriptObjPtr); + + if (retcode == JIM_EXIT) { + break; + } + if (retcode == JIM_ERR) { + Jim_MakeErrorMessage(interp); + } + result = Jim_GetString(Jim_GetResult(interp), &reslen); + if (reslen) { + if (fwrite(result, reslen, 1, stdout) == 0) { + + } + putchar('\n'); + } + } + out: + Jim_Free(history_file); + + return retcode; +} + +#include +#include +#include + + + +extern int Jim_initjimshInit(Jim_Interp *interp); + +static void JimSetArgv(Jim_Interp *interp, int argc, char *const argv[]) +{ + int n; + Jim_Obj *listObj = Jim_NewListObj(interp, NULL, 0); + + + for (n = 0; n < argc; n++) { + Jim_Obj *obj = Jim_NewStringObj(interp, argv[n], -1); + + Jim_ListAppendElement(interp, listObj, obj); + } + + Jim_SetVariableStr(interp, "argv", listObj); + Jim_SetVariableStr(interp, "argc", Jim_NewIntObj(interp, argc)); +} + +static void JimPrintErrorMessage(Jim_Interp *interp) +{ + Jim_MakeErrorMessage(interp); + fprintf(stderr, "%s\n", Jim_String(Jim_GetResult(interp))); +} + +void usage(const char* executable_name) +{ + printf("jimsh version %d.%d\n", JIM_VERSION / 100, JIM_VERSION % 100); + printf("Usage: %s\n", executable_name); + printf("or : %s [options] [filename]\n", executable_name); + printf("\n"); + printf("Without options: Interactive mode\n"); + printf("\n"); + printf("Options:\n"); + printf(" --version : prints the version string\n"); + printf(" --help : prints this text\n"); + printf(" -e CMD : executes command CMD\n"); + printf(" NOTE: all subsequent options will be passed as arguments to the command\n"); + printf(" [filename|-] : executes the script contained in the named file, or from stdin if \"-\"\n"); + printf(" NOTE: all subsequent options will be passed to the script\n\n"); +} + +int main(int argc, char *const argv[]) +{ + int retcode; + Jim_Interp *interp; + char *const orig_argv0 = argv[0]; + + + if (argc > 1 && strcmp(argv[1], "--version") == 0) { + printf("%d.%d\n", JIM_VERSION / 100, JIM_VERSION % 100); + return 0; + } + else if (argc > 1 && strcmp(argv[1], "--help") == 0) { + usage(argv[0]); + return 0; + } + + + interp = Jim_CreateInterp(); + Jim_RegisterCoreCommands(interp); + + + if (Jim_InitStaticExtensions(interp) != JIM_OK) { + JimPrintErrorMessage(interp); + } + + Jim_SetVariableStrWithStr(interp, "jim::argv0", orig_argv0); + Jim_SetVariableStrWithStr(interp, JIM_INTERACTIVE, argc == 1 ? "1" : "0"); +#ifdef USE_LINENOISE + Jim_SetVariableStrWithStr(interp, "jim::lineedit", "1"); +#else + Jim_SetVariableStrWithStr(interp, "jim::lineedit", "0"); +#endif + retcode = Jim_initjimshInit(interp); + + if (argc == 1) { + + if (retcode == JIM_ERR) { + JimPrintErrorMessage(interp); + } + if (retcode != JIM_EXIT) { + JimSetArgv(interp, 0, NULL); + if (!isatty(STDIN_FILENO)) { + + goto eval_stdin; + } + retcode = Jim_InteractivePrompt(interp); + } + } + else { + + if (argc > 2 && strcmp(argv[1], "-e") == 0) { + + JimSetArgv(interp, argc - 3, argv + 3); + retcode = Jim_Eval(interp, argv[2]); + if (retcode != JIM_ERR) { + int len; + const char *msg = Jim_GetString(Jim_GetResult(interp), &len); + if (fwrite(msg, len, 1, stdout) == 0) { + + } + putchar('\n'); + } + } + else { + Jim_SetVariableStr(interp, "argv0", Jim_NewStringObj(interp, argv[1], -1)); + JimSetArgv(interp, argc - 2, argv + 2); + if (strcmp(argv[1], "-") == 0) { +eval_stdin: + retcode = Jim_Eval(interp, "eval [info source [stdin read] stdin 1]"); + } else { + retcode = Jim_EvalFile(interp, argv[1]); + } + } + if (retcode == JIM_ERR) { + JimPrintErrorMessage(interp); + } + } + if (retcode == JIM_EXIT) { + retcode = Jim_GetExitCode(interp); + } + else if (retcode == JIM_ERR) { + retcode = 1; + } + else { + retcode = 0; + } + Jim_FreeInterp(interp); + return retcode; +} +#endif diff --git a/autosetup/pkg-config.tcl b/autosetup/pkg-config.tcl new file mode 100644 index 0000000000..9ce7111f55 --- /dev/null +++ b/autosetup/pkg-config.tcl @@ -0,0 +1,168 @@ +# Copyright (c) 2016 WorkWare Systems http://www.workware.net.au/ +# All rights reserved + +# @synopsis: +# +# The 'pkg-config' module allows package information to be found via 'pkg-config'. +# +# If not cross-compiling, the package path should be determined automatically +# by 'pkg-config'. +# If cross-compiling, the default package path is the compiler sysroot. +# If the C compiler doesn't support '-print-sysroot', the path can be supplied +# by the '--sysroot' option or by defining 'SYSROOT'. +# +# 'PKG_CONFIG' may be set to use an alternative to 'pkg-config'. + +use cc + +options { + sysroot:dir => "Override compiler sysroot for pkg-config search path" +} + +# @pkg-config-init ?required? +# +# Initialises the 'pkg-config' system. Unless '$required' is set to 0, +# it is a fatal error if a usable 'pkg-config' is not found . +# +# This command will normally be called automatically as required, +# but it may be invoked explicitly if lack of 'pkg-config' is acceptable. +# +# Returns 1 if ok, or 0 if 'pkg-config' not found/usable (only if '$required' is 0). +# +proc pkg-config-init {{required 1}} { + if {[is-defined HAVE_PKG_CONFIG]} { + return [get-define HAVE_PKG_CONFIG] + } + set found 0 + + define PKG_CONFIG [get-env PKG_CONFIG pkg-config] + msg-checking "Checking for pkg-config..." + + if {[catch {exec [get-define PKG_CONFIG] --version} version]} { + msg-result "[get-define PKG_CONFIG] (not found)" + if {$required} { + user-error "No usable pkg-config" + } + } else { + msg-result $version + define PKG_CONFIG_VERSION $version + + set found 1 + + if {[opt-str sysroot o]} { + define SYSROOT [file-normalize $o] + msg-result "Using specified sysroot [get-define SYSROOT]" + } elseif {[get-define build] ne [get-define host]} { + if {[catch {exec-with-stderr {*}[get-define CC] -print-sysroot} result errinfo] == 0} { + # Use the compiler sysroot, if there is one + define SYSROOT $result + msg-result "Found compiler sysroot $result" + } else { + configlog "[get-define CC] -print-sysroot: $result" + set msg "pkg-config: Cross compiling, but no compiler sysroot and no --sysroot supplied" + if {$required} { + user-error $msg + } else { + msg-result $msg + } + set found 0 + } + } + if {[is-defined SYSROOT]} { + set sysroot [get-define SYSROOT] + + # XXX: It's possible that these should be set only when invoking pkg-config + global env + set env(PKG_CONFIG_DIR) "" + # Supposedly setting PKG_CONFIG_LIBDIR means that PKG_CONFIG_PATH is ignored, + # but it doesn't seem to work that way in practice + set env(PKG_CONFIG_PATH) "" + # Do we need to try /usr/local as well or instead? + set env(PKG_CONFIG_LIBDIR) $sysroot/usr/lib/pkgconfig:$sysroot/usr/share/pkgconfig + set env(PKG_CONFIG_SYSROOT_DIR) $sysroot + } + } + define HAVE_PKG_CONFIG $found + return $found +} + +# @pkg-config module ?requirements? +# +# Use 'pkg-config' to find the given module meeting the given requirements. +# e.g. +# +## pkg-config pango >= 1.37.0 +# +# If found, returns 1 and sets 'HAVE_PKG_PANGO' to 1 along with: +# +## PKG_PANGO_VERSION to the found version +## PKG_PANGO_LIBS to the required libs (--libs-only-l) +## PKG_PANGO_LDFLAGS to the required linker flags (--libs-only-L) +## PKG_PANGO_CFLAGS to the required compiler flags (--cflags) +# +# If not found, returns 0. +# +proc pkg-config {module args} { + set ok [pkg-config-init] + + msg-checking "Checking for $module $args..." + + if {!$ok} { + msg-result "no pkg-config" + return 0 + } + + set pkgconfig [get-define PKG_CONFIG] + + set ret [catch {exec $pkgconfig --modversion "$module $args"} version] + configlog "$pkgconfig --modversion $module $args: $version" + if {$ret} { + msg-result "not found" + return 0 + } + # Sometimes --modversion succeeds but because of dependencies it isn't usable + # This seems to show up with --cflags + set ret [catch {exec $pkgconfig --cflags $module} cflags] + if {$ret} { + msg-result "unusable ($version - see config.log)" + configlog "$pkgconfig --cflags $module" + configlog $cflags + return 0 + } + msg-result $version + set prefix [feature-define-name $module PKG_] + define HAVE_${prefix} + define ${prefix}_VERSION $version + define ${prefix}_CFLAGS $cflags + define ${prefix}_LIBS [exec $pkgconfig --libs-only-l $module] + define ${prefix}_LDFLAGS [exec $pkgconfig --libs-only-L $module] + return 1 +} + +# @pkg-config-get module setting +# +# Convenience access to the results of 'pkg-config'. +# +# For example, '[pkg-config-get pango CFLAGS]' returns +# the value of 'PKG_PANGO_CFLAGS', or '""' if not defined. +proc pkg-config-get {module name} { + set prefix [feature-define-name $module PKG_] + get-define ${prefix}_${name} "" +} + +# @pkg-config-get-var module variable +# +# Return the value of the given variable from the given pkg-config module. +# The module must already have been successfully detected with pkg-config. +# e.g. +# +## if {[pkg-config harfbuzz >= 2.5]} { +## define harfbuzz_libdir [pkg-config-get-var harfbuzz libdir] +## } +# +# Returns the empty string if the variable isn't defined. +proc pkg-config-get-var {module variable} { + set pkgconfig [get-define PKG_CONFIG] + set prefix [feature-define-name $module HAVE_PKG_] + exec $pkgconfig $module --variable $variable +} diff --git a/autosetup/proj.tcl b/autosetup/proj.tcl new file mode 100644 index 0000000000..86f4df44e2 --- /dev/null +++ b/autosetup/proj.tcl @@ -0,0 +1,2549 @@ +######################################################################## +# 2024 September 25 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# * May you do good and not evil. +# * May you find forgiveness for yourself and forgive others. +# * May you share freely, never taking more than you give. +# + +# +# ----- @module proj.tcl ----- +# @section Project-agnostic Helper APIs +# + +# +# Routines for Steve Bennett's autosetup which are common to trees +# managed in and around the umbrella of the SQLite project. +# +# The intent is that these routines be relatively generic, independent +# of a given project. +# +# For practical purposes, the copy of this file hosted in the SQLite +# project is the "canonical" one: +# +# https://sqlite.org/src/file/autosetup/proj.tcl +# +# This file was initially derived from one used in the libfossil +# project, authored by the same person who ported it here, and this is +# noted here only as an indication that there are no licensing issues +# despite this code having a handful of near-twins running around a +# handful of third-party source trees. +# +# Design notes: +# +# - Symbols with _ separators are intended for internal use within +# this file, and are not part of the API which auto.def files should +# rely on. Symbols with - separators are public APIs. +# +# - By and large, autosetup prefers to update global state with the +# results of feature checks, e.g. whether the compiler supports flag +# --X. In this developer's opinion that (A) causes more confusion +# than it solves[^1] and (B) adds an unnecessary layer of "voodoo" +# between the autosetup user and its internals. This module, in +# contrast, instead injects the results of its own tests into +# well-defined variables and leaves the integration of those values +# to the caller's discretion. +# +# [1]: As an example: testing for the -rpath flag, using +# cc-check-flags, can break later checks which use +# [cc-check-function-in-lib ...] because the resulting -rpath flag +# implicitly becomes part of those tests. In the case of an rpath +# test, downstream tests may not like the $prefix/lib path added by +# the rpath test. To avoid such problems, we avoid (intentionally) +# updating global state via feature tests. +# + +# +# $proj__Config is an internal-use-only array for storing whatever generic +# internal stuff we need stored. +# +array set ::proj__Config [subst { + self-tests [get-env proj.self-tests 0] + verbose-assert [get-env proj.assert-verbose 0] + isatty [isatty? stdout] +}] + +# +# List of dot-in files to filter in the final stages of +# configuration. Some configuration steps may append to this. Each +# one in this list which exists will trigger the generation of a +# file with that same name, minus the ".in", in the build directory +# (which differ from the source dir in out-of-tree builds). +# +# See: proj-dot-ins-append and proj-dot-ins-process +# +set ::proj__Config(dot-in-files) [list] + +# +# @proj-warn msg +# +# Emits a warning message to stderr. All args are appended with a +# space between each. +# +proc proj-warn {args} { + show-notices + puts stderr [join [list "WARNING:" \[ [proj-scope 1] \]: {*}$args] " "] +} + + +# +# Internal impl of [proj-fatal] and [proj-error]. It must be called +# using tailcall. +# +proc proj__faterr {failMode args} { + show-notices + set lvl 1 + while {"-up" eq [lindex $args 0]} { + set args [lassign $args -] + incr lvl + } + if {$failMode} { + puts stderr [join [list "FATAL:" \[ [proj-scope $lvl] \]: {*}$args]] + exit 1 + } else { + error [join [list in \[ [proj-scope $lvl] \]: {*}$args]] + } +} + +# +# @proj-fatal ?-up...? msg... +# +# Emits an error message to stderr and exits with non-0. All args are +# appended with a space between each. +# +# The calling scope's name is used in the error message. To instead +# use the name of a call higher up in the stack, use -up once for each +# additional level. +# +proc proj-fatal {args} { + tailcall proj__faterr 1 {*}$args +} + +# +# @proj-error ?-up...? msg... +# +# Works like proj-fatal but uses [error] intead of [exit]. +# +proc proj-error {args} { + tailcall proj__faterr 0 {*}$args +} + +# +# @proj-assert script ?message? +# +# Kind of like a C assert: if uplevel of [list expr $script] is false, +# a fatal error is triggered. The error message, by default, includes +# the body of the failed assertion, but if $msg is set then that is +# used instead. +# +proc proj-assert {script {msg ""}} { + if {1 eq $::proj__Config(verbose-assert)} { + msg-result [proj-bold "asserting: $script"] + } + if {![uplevel 1 [list expr $script]]} { + if {"" eq $msg} { + set msg $script + } + tailcall proj__faterr 1 "Assertion failed:" $msg + } +} + +# +# @proj-bold str +# +# If this function believes that the current console might support +# ANSI escape sequences then this returns $str wrapped in a sequence +# to bold that text, else it returns $str as-is. +# +proc proj-bold {args} { + if {$::autosetup(iswin) || !$::proj__Config(isatty)} { + return [join $args] + } + return "\033\[1m${args}\033\[0m" +} + +# +# @proj-indented-notice ?-error? ?-notice? msg +# +# Takes a multi-line message and emits it with consistent indentation. +# It does not perform any line-wrapping of its own. Which output +# routine it uses depends on its flags, defaulting to msg-result. +# For -error and -notice it uses user-notice. +# +# If the -notice flag it used then it emits using [user-notice], which +# means its rendering will (A) go to stderr and (B) be delayed until +# the next time autosetup goes to output a message. +# +# If the -error flag is provided then it renders the message +# immediately to stderr and then exits. +# +# If neither -notice nor -error are used, the message will be sent to +# stdout without delay. +# +proc proj-indented-notice {args} { + set fErr "" + set outFunc "msg-result" + while {[llength $args] > 1} { + switch -exact -- [lindex $args 0] { + -error { + set args [lassign $args fErr] + set outFunc "user-notice" + } + -notice { + set args [lassign $args -] + set outFunc "user-notice" + } + default { + break + } + } + } + set lines [split [join $args] \n] + foreach line $lines { + set line [string trimleft $line] + if {"" eq $line} { + $outFunc $line + } else { + $outFunc " $line" + } + } + if {"" ne $fErr} { + show-notices + exit 1 + } +} + +# +# @proj-is-cross-compiling +# +# Returns 1 if cross-compiling, else 0. +# +proc proj-is-cross-compiling {} { + expr {[get-define host] ne [get-define build]} +} + +# +# @proj-strip-hash-comments value +# +# Expects to receive string input, which it splits on newlines, strips +# out any lines which begin with any number of whitespace followed by +# a '#', and returns a value containing the [append]ed results of each +# remaining line with a \n between each. It does not strip out +# comments which appear after the first non-whitespace character. +# +proc proj-strip-hash-comments {val} { + set x {} + foreach line [split $val \n] { + if {![string match "#*" [string trimleft $line]]} { + append x $line \n + } + } + return $x +} + +# +# @proj-cflags-without-werror +# +# Fetches [define $var], strips out any -Werror entries, and returns +# the new value. This is intended for temporarily stripping -Werror +# from CFLAGS or CPPFLAGS within the scope of a [define-push] block. +# +proc proj-cflags-without-werror {{var CFLAGS}} { + set rv {} + foreach f [get-define $var ""] { + switch -exact -- $f { + -Werror {} + default { lappend rv $f } + } + } + join $rv " " +} + +# +# @proj-check-function-in-lib +# +# A proxy for cc-check-function-in-lib with the following differences: +# +# - Does not make any global changes to the LIBS define. +# +# - Strips out the -Werror flag from CFLAGS before running the test, +# as these feature tests will often fail if -Werror is used. +# +# Returns the result of cc-check-function-in-lib (i.e. true or false). +# The resulting linker flags are stored in the [define] named +# lib_${function}. +# +proc proj-check-function-in-lib {function libs {otherlibs {}}} { + set found 0 + define-push {LIBS CFLAGS} { + #puts "CFLAGS before=[get-define CFLAGS]" + define CFLAGS [proj-cflags-without-werror] + #puts "CFLAGS after =[get-define CFLAGS]" + set found [cc-check-function-in-lib $function $libs $otherlibs] + } + return $found +} + +# +# @proj-search-for-header-dir ?-dirs LIST? ?-subdirs LIST? header +# +# Searches for $header in a combination of dirs and subdirs, specified +# by the -dirs {LIST} and -subdirs {LIST} flags (each of which have +# sane defaults). Returns either the first matching dir or an empty +# string. The return value does not contain the filename part. +# +proc proj-search-for-header-dir {header args} { + set subdirs {include} + set dirs {/usr /usr/local /mingw} +# Debatable: +# if {![proj-is-cross-compiling]} { +# lappend dirs [get-define prefix] +# } + while {[llength $args]} { + switch -exact -- [lindex $args 0] { + -dirs { set args [lassign $args - dirs] } + -subdirs { set args [lassign $args - subdirs] } + default { + proj-error "Unhandled argument: $args" + } + } + } + foreach dir $dirs { + foreach sub $subdirs { + if {[file exists $dir/$sub/$header]} { + return "$dir/$sub" + } + } + } + return "" +} + +# +# @proj-find-executable-path ?-v? binaryName +# +# Works similarly to autosetup's [find-executable-path $binName] but: +# +# - If the first arg is -v, it's verbose about searching, else it's quiet. +# +# Returns the full path to the result or an empty string. +# +proc proj-find-executable-path {args} { + set binName $args + set verbose 0 + if {[lindex $args 0] eq "-v"} { + set verbose 1 + set args [lassign $args - binName] + msg-checking "Looking for $binName ... " + } + set check [find-executable-path $binName] + if {$verbose} { + if {"" eq $check} { + msg-result "not found" + } else { + msg-result $check + } + } + return $check +} + +# +# @proj-bin-define binName ?defName? +# +# Uses [proj-find-executable-path $binName] to (verbosely) search for +# a binary, sets a define (see below) to the result, and returns the +# result (an empty string if not found). +# +# The define'd name is: If $defName is not empty, it is used as-is. If +# $defName is empty then "BIN_X" is used, where X is the upper-case +# form of $binName with any '-' characters replaced with '_'. +# +proc proj-bin-define {binName {defName {}}} { + set check [proj-find-executable-path -v $binName] + if {"" eq $defName} { + set defName "BIN_[string toupper [string map {- _} $binName]]" + } + define $defName $check + return $check +} + +# +# @proj-first-bin-of bin... +# +# Looks for the first binary found of the names passed to this +# function. If a match is found, the full path to that binary is +# returned, else "" is returned. +# +# Despite using cc-path-progs to do the search, this function clears +# any define'd name that function stores for the result (because the +# caller has no sensible way of knowing which [define] name it has +# unless they pass only a single argument). +# +proc proj-first-bin-of {args} { + set rc "" + foreach b $args { + set u [string toupper $b] + # Note that cc-path-progs defines $u to "false" if it finds no + # match. + if {[cc-path-progs $b]} { + set rc [get-define $u] + } + undefine $u + if {"" ne $rc} break + } + return $rc +} + +# +# @proj-opt-was-provided key +# +# Returns 1 if the user specifically provided the given configure flag +# or if it was specifically set using proj-opt-set, else 0. This can +# be used to distinguish between options which have a default value +# and those which were explicitly provided by the user, even if the +# latter is done in a way which uses the default value. +# +# For example, with a configure flag defined like: +# +# { foo-bar:=baz => {its help text} } +# +# This function will, when passed foo-bar, return 1 only if the user +# passes --foo-bar to configure, even if that invocation would resolve +# to the default value of baz. If the user does not explicitly pass in +# --foo-bar (with or without a value) then this returns 0. +# +# Calling [proj-opt-set] is, for purposes of the above, equivalent to +# explicitly passing in the flag. +# +# Note: unlike most functions which deal with configure --flags, this +# one does not validate that $key refers to a pre-defined flag. i.e. +# it accepts arbitrary keys, even those not defined via an [options] +# call. [proj-opt-set] manipulates the internal list of flags, such +# that new options set via that function will cause this function to +# return true. (That's an unintended and unavoidable side-effect, not +# specifically a feature which should be made use of.) +# +proc proj-opt-was-provided {key} { + dict exists $::autosetup(optset) $key +} + +# +# @proj-opt-set flag ?val? +# +# Force-set autosetup option $flag to $val. The value can be fetched +# later with [opt-val], [opt-bool], and friends. +# +# Returns $val. +# +proc proj-opt-set {flag {val 1}} { + if {$flag ni $::autosetup(options)} { + # We have to add this to autosetup(options) or else future calls + # to [opt-bool $flag] will fail validation of $flag. + lappend ::autosetup(options) $flag + } + dict set ::autosetup(optset) $flag $val + return $val +} + +# +# @proj-opt-exists flag +# +# Returns 1 if the given flag has been defined as a legal configure +# option, else returns 0. Options set via proj-opt-set "exist" for +# this purpose even if they were not defined via autosetup's +# [options] function. +# +proc proj-opt-exists {flag} { + expr {$flag in $::autosetup(options)}; +} + +# +# @proj-val-truthy val +# +# Returns 1 if $val appears to be a truthy value, else returns +# 0. Truthy values are any of {1 on true yes enabled} +# +proc proj-val-truthy {val} { + expr {$val in {1 on true yes enabled}} +} + +# +# @proj-opt-truthy flag +# +# Returns 1 if [opt-val $flag] appears to be a truthy value or +# [opt-bool $flag] is true. See proj-val-truthy. +# +proc proj-opt-truthy {flag} { + if {[proj-val-truthy [opt-val $flag]]} { return 1 } + set rc 0 + catch { + # opt-bool will throw if $flag is not a known boolean flag + set rc [opt-bool $flag] + } + return $rc +} + +# +# @proj-if-opt-truthy boolFlag thenScript ?elseScript? +# +# If [proj-opt-truthy $flag] is true, eval $then, else eval $else. +# +proc proj-if-opt-truthy {boolFlag thenScript {elseScript {}}} { + if {[proj-opt-truthy $boolFlag]} { + uplevel 1 $thenScript + } else { + uplevel 1 $elseScript + } +} + +# +# @proj-define-for-opt flag def ?msg? ?iftrue? ?iffalse? +# +# If [proj-opt-truthy $flag] then [define $def $iftrue] else [define +# $def $iffalse]. If $msg is not empty, output [msg-checking $msg] and +# a [msg-results ...] which corresponds to the result. Returns 1 if +# the opt-truthy check passes, else 0. +# +proc proj-define-for-opt {flag def {msg ""} {iftrue 1} {iffalse 0}} { + if {"" ne $msg} { + msg-checking "$msg " + } + set rcMsg "" + set rc 0 + if {[proj-opt-truthy $flag]} { + define $def $iftrue + set rc 1 + } else { + define $def $iffalse + } + switch -- [proj-val-truthy [get-define $def]] { + 0 { set rcMsg no } + 1 { set rcMsg yes } + } + if {"" ne $msg} { + msg-result $rcMsg + } + return $rc +} + +# +# @proj-opt-define-bool ?-v? optName defName ?descr? +# +# Checks [proj-opt-truthy $optName] and calls [define $defName X] +# where X is 0 for false and 1 for true. $descr is an optional +# [msg-checking] argument which defaults to $defName. Returns X. +# +# If args[0] is -v then the boolean semantics are inverted: if +# the option is set, it gets define'd to 0, else 1. Returns the +# define'd value. +# +proc proj-opt-define-bool {args} { + set invert 0 + if {[lindex $args 0] eq "-v"} { + incr invert + lassign $args - optName defName descr + } else { + lassign $args optName defName descr + } + if {"" eq $descr} { + set descr $defName + } + #puts "optName=$optName defName=$defName descr=$descr" + set rc 0 + msg-checking "[join $descr] ... " + set rc [proj-opt-truthy $optName] + if {$invert} { + set rc [expr {!$rc}] + } + msg-result [string map {0 no 1 yes} $rc] + define $defName $rc + return $rc +} + +# +# @proj-check-module-loader +# +# Check for module-loading APIs (libdl/libltdl)... +# +# Looks for libltdl or dlopen(), the latter either in -ldl or built in +# to libc (as it is on some platforms). Returns 1 if found, else +# 0. Either way, it `define`'s: +# +# - HAVE_LIBLTDL to 1 or 0 if libltdl is found/not found +# - HAVE_LIBDL to 1 or 0 if dlopen() is found/not found +# - LDFLAGS_MODULE_LOADER one of ("-lltdl", "-ldl", or ""), noting +# that -ldl may legally be empty on some platforms even if +# HAVE_LIBDL is true (indicating that dlopen() is available without +# extra link flags). LDFLAGS_MODULE_LOADER also gets "-rdynamic" appended +# to it because otherwise trying to open DLLs will result in undefined +# symbol errors. +# +# Note that if it finds LIBLTDL it does not look for LIBDL, so will +# report only that is has LIBLTDL. +# +proc proj-check-module-loader {} { + msg-checking "Looking for module-loader APIs... " + if {99 ne [get-define LDFLAGS_MODULE_LOADER 99]} { + if {1 eq [get-define HAVE_LIBLTDL 0]} { + msg-result "(cached) libltdl" + return 1 + } elseif {1 eq [get-define HAVE_LIBDL 0]} { + msg-result "(cached) libdl" + return 1 + } + # else: wha??? + } + set HAVE_LIBLTDL 0 + set HAVE_LIBDL 0 + set LDFLAGS_MODULE_LOADER "" + set rc 0 + puts "" ;# cosmetic kludge for cc-check-XXX + if {[cc-check-includes ltdl.h] && [cc-check-function-in-lib lt_dlopen ltdl]} { + set HAVE_LIBLTDL 1 + set LDFLAGS_MODULE_LOADER "-lltdl -rdynamic" + msg-result " - Got libltdl." + set rc 1 + } elseif {[cc-with {-includes dlfcn.h} { + cctest -link 1 -declare "extern char* dlerror(void);" -code "dlerror();"}]} { + msg-result " - This system can use dlopen() without -ldl." + set HAVE_LIBDL 1 + set LDFLAGS_MODULE_LOADER "" + set rc 1 + } elseif {[cc-check-includes dlfcn.h]} { + set HAVE_LIBDL 1 + set rc 1 + if {[cc-check-function-in-lib dlopen dl]} { + msg-result " - dlopen() needs libdl." + set LDFLAGS_MODULE_LOADER "-ldl -rdynamic" + } else { + msg-result " - dlopen() not found in libdl. Assuming dlopen() is built-in." + set LDFLAGS_MODULE_LOADER "-rdynamic" + } + } + define HAVE_LIBLTDL $HAVE_LIBLTDL + define HAVE_LIBDL $HAVE_LIBDL + define LDFLAGS_MODULE_LOADER $LDFLAGS_MODULE_LOADER + return $rc +} + +# +# @proj-no-check-module-loader +# +# Sets all flags which would be set by proj-check-module-loader to +# empty/falsy values, as if those checks had failed to find a module +# loader. Intended to be called in place of that function when +# a module loader is explicitly not desired. +# +proc proj-no-check-module-loader {} { + define HAVE_LIBDL 0 + define HAVE_LIBLTDL 0 + define LDFLAGS_MODULE_LOADER "" +} + +# +# @proj-file-content ?-trim? filename +# +# Opens the given file, reads all of its content, and returns it. If +# the first arg is -trim, the contents of the file named by the second +# argument are trimmed before returning them. +# +proc proj-file-content {args} { + set trim 0 + set fname $args + if {"-trim" eq [lindex $args 0]} { + set trim 1 + lassign $args - fname + } + set fp [open $fname rb] + set rc [read $fp] + close $fp + if {$trim} { return [string trim $rc] } + return $rc +} + +# +# @proj-file-conent filename +# +# Returns the contents of the given file as an array of lines, with +# the EOL stripped from each input line. +# +proc proj-file-content-list {fname} { + set fp [open $fname rb] + set rc {} + while { [gets $fp line] >= 0 } { + lappend rc $line + } + close $fp + return $rc +} + +# +# @proj-file-write ?-ro? fname content +# +# Works like autosetup's [writefile] but explicitly uses binary mode +# to avoid EOL translation on Windows. If $fname already exists, it is +# overwritten, even if it's flagged as read-only. +# +proc proj-file-write {args} { + if {"-ro" eq [lindex $args 0]} { + lassign $args ro fname content + } else { + set ro "" + lassign $args fname content + } + file delete -force -- $fname; # in case it's read-only + set f [open $fname wb] + puts -nonewline $f $content + close $f + if {"" ne $ro} { + catch { + exec chmod -w $fname + #file attributes -w $fname; #jimtcl has no 'attributes' + } + } +} + +# +# @proj-check-compile-commands ?-assume-for-clang? ?configFlag? +# +# Checks the compiler for compile_commands.json support. If +# $configFlag is not empty then it is assumed to be the name of an +# autosetup boolean config which controls whether to run/skip this +# check. +# +# If -assume-for-clang is provided and $configFlag is not empty and CC +# matches *clang* and no --$configFlag was explicitly provided to the +# configure script then behave as if --$configFlag had been provided. +# To disable that assumption, either don't pass -assume-for-clang or +# pass --$configFlag=0 to the configure script. (The reason for this +# behavior is that clang supports compile-commands but some other +# compilers report false positives with these tests.) +# +# Returns 1 if supported, else 0, and defines HAVE_COMPILE_COMMANDS to +# that value. Defines MAKE_COMPILATION_DB to "yes" if supported, "no" +# if not. The use of MAKE_COMPILATION_DB is deprecated/discouraged: +# HAVE_COMPILE_COMMANDS is preferred. +# +# ACHTUNG: this test has a long history of false positive results +# because of compilers reacting differently to the -MJ flag. Because +# of this, it is recommended that this support be an opt-in feature, +# rather than an on-by-default default one. That is: in the +# configure script define the option as +# {--the-flag-name=0 => {Enable ....}} +# +proc proj-check-compile-commands {args} { + set i 0 + set configFlag {} + set fAssumeForClang 0 + set doAssume 0 + msg-checking "compile_commands.json support... " + if {"-assume-for-clang" eq [lindex $args 0]} { + lassign $args - configFlag + incr fAssumeForClang + } elseif {1 == [llength $args]} { + lassign $args configFlag + } else { + proj-error "Invalid arguments" + } + if {1 == $fAssumeForClang && "" ne $configFlag} { + if {[string match *clang* [get-define CC]] + && ![proj-opt-was-provided $configFlag] + && ![proj-opt-truthy $configFlag]} { + proj-indented-notice [subst -nocommands -nobackslashes { + CC appears to be clang, so assuming that --$configFlag is likely + to work. To disable this assumption use --$configFlag=0.}] + incr doAssume + } + } + if {!$doAssume && "" ne $configFlag && ![proj-opt-truthy $configFlag]} { + msg-result "check disabled. Use --${configFlag} to enable it." + define HAVE_COMPILE_COMMANDS 0 + define MAKE_COMPILATION_DB no + return 0 + } else { + if {[cctest -lang c -cflags {/dev/null -MJ} -source {}]} { + # This test reportedly incorrectly succeeds on one of + # Martin G.'s older systems. drh also reports a false + # positive on an unspecified older Mac system. + msg-result "compiler supports -MJ. Assuming it's useful for compile_commands.json" + define MAKE_COMPILATION_DB yes; # deprecated + define HAVE_COMPILE_COMMANDS 1 + return 1 + } else { + msg-result "compiler does not support compile_commands.json" + define MAKE_COMPILATION_DB no + define HAVE_COMPILE_COMMANDS 0 + return 0 + } + } +} + +# +# @proj-touch filename +# +# Runs the 'touch' external command on one or more files, ignoring any +# errors. +# +proc proj-touch {filename} { + catch { exec touch {*}$filename } +} + +# +# @proj-make-from-dot-in ?-touch? infile ?outfile? +# +# Uses [make-template] to create makefile(-like) file(s) $outfile from +# $infile but explicitly makes the output read-only, to avoid +# inadvertent editing (who, me?). +# +# If $outfile is empty then: +# +# - If $infile is a 2-element list, it is assumed to be an in/out pair, +# and $outfile is set from the 2nd entry in that list. Else... +# +# - $outfile is set to $infile stripped of its extension. +# +# If the first argument is -touch then the generated file is touched +# to update its timestamp. This can be used as a workaround for +# cases where (A) autosetup does not update the file because it was +# not really modified and (B) the file *really* needs to be updated to +# please the build process. +# +# Failures when running chmod or touch are silently ignored. +# +proc proj-make-from-dot-in {args} { + set fIn "" + set fOut "" + set touch 0 + if {[lindex $args 0] eq "-touch"} { + set touch 1 + lassign $args - fIn fOut + } else { + lassign $args fIn fOut + } + if {"" eq $fOut} { + if {[llength $fIn]>1} { + lassign $fIn fIn fOut + } else { + set fOut [file rootname $fIn] + } + } + #puts "filenames=$filename" + if {[file exists $fOut]} { + catch { exec chmod u+w $fOut } + } + #puts "making template: $fIn ==> $fOut" + #define-push {top_srcdir} { + #puts "--- $fIn $fOut top_srcdir=[get-define top_srcdir]" + make-template $fIn $fOut + #puts "--- $fIn $fOut top_srcdir=[get-define top_srcdir]" + # make-template modifies top_srcdir + #} + if {$touch} { + proj-touch $fOut + } + catch { + exec chmod -w $fOut + #file attributes -w $f; #jimtcl has no 'attributes' + } +} + +# +# @proj-check-profile-flag ?flagname? +# +# Checks for the boolean configure option named by $flagname. If set, +# it checks if $CC seems to refer to gcc. If it does (or appears to) +# then it defines CC_PROFILE_FLAG to "-pg" and returns 1, else it +# defines CC_PROFILE_FLAG to "" and returns 0. +# +# Note that the resulting flag must be added to both CFLAGS and +# LDFLAGS in order for binaries to be able to generate "gmon.out". In +# order to avoid potential problems with escaping, space-containing +# tokens, and interfering with autosetup's use of these vars, this +# routine does not directly modify CFLAGS or LDFLAGS. +# +proc proj-check-profile-flag {{flagname profile}} { + #puts "flagname=$flagname ?[proj-opt-truthy $flagname]?" + if {[proj-opt-truthy $flagname]} { + set CC [get-define CC] + regsub {.*ccache *} $CC "" CC + # ^^^ if CC="ccache gcc" then [exec] treats "ccache gcc" as a + # single binary name and fails. So strip any leading ccache part + # for this purpose. + if { ![catch { exec $CC --version } msg]} { + if {[string first gcc $CC] != -1} { + define CC_PROFILE_FLAG "-pg" + return 1 + } + } + } + define CC_PROFILE_FLAG "" + return 0 +} + +# +# @proj-looks-like-windows ?key? +# +# Returns 1 if this appears to be a Windows environment (MinGw, +# Cygwin, MSys), else returns 0. The optional argument is the name of +# an autosetup define which contains platform name info, defaulting to +# "host" (meaning, somewhat counterintuitively, the target system, not +# the current host). The other legal value is "build" (the build +# machine, i.e. the local host). If $key == "build" then some +# additional checks may be performed which are not applicable when +# $key == "host". +# +proc proj-looks-like-windows {{key host}} { + global autosetup + switch -glob -- [get-define $key] { + *-*-ming* - *-*-cygwin - *-*-msys - *windows* { + return 1 + } + } + if {$key eq "build"} { + # These apply only to the local OS, not a cross-compilation target, + # as the above check potentially can. + if {$::autosetup(iswin)} { return 1 } + if {[find-an-executable cygpath] ne "" || $::tcl_platform(os) eq "Windows NT"} { + return 1 + } + } + return 0 +} + +# +# @proj-looks-like-mac ?key? +# +# Looks at either the 'host' (==compilation target platform) or +# 'build' (==the being-built-on platform) define value and returns if +# if that value seems to indicate that it represents a Mac platform, +# else returns 0. +# +proc proj-looks-like-mac {{key host}} { + switch -glob -- [get-define $key] { + *-*-darwin* { + # https://sqlite.org/forum/forumpost/7b218c3c9f207646 + # There's at least one Linux out there which matches *apple*. + return 1 + } + default { + return 0 + } + } +} + +# +# @proj-exe-extension +# +# Checks autosetup's "host" and "build" defines to see if the build +# host and target are Windows-esque (Cygwin, MinGW, MSys). If the +# build environment is then BUILD_EXEEXT is [define]'d to ".exe", else +# "". If the target, a.k.a. "host", is then TARGET_EXEEXT is +# [define]'d to ".exe", else "". +# +proc proj-exe-extension {} { + set rH "" + set rB "" + if {[proj-looks-like-windows host]} { + set rH ".exe" + } + if {[proj-looks-like-windows build]} { + set rB ".exe" + } + define BUILD_EXEEXT $rB + define TARGET_EXEEXT $rH +} + +# +# @proj-dll-extension +# +# Works like proj-exe-extension except that it defines BUILD_DLLEXT +# and TARGET_DLLEXT to one of (.so, ,dll, .dylib). +# +# Trivia: for .dylib files, the linker needs the -dynamiclib flag +# instead of -shared. +# +proc proj-dll-extension {} { + set inner {{key} { + if {[proj-looks-like-mac $key]} { + return ".dylib" + } + if {[proj-looks-like-windows $key]} { + return ".dll" + } + return ".so" + }} + define BUILD_DLLEXT [apply $inner build] + define TARGET_DLLEXT [apply $inner host] +} + +# +# @proj-lib-extension +# +# Static-library counterpart of proj-dll-extension. Defines +# BUILD_LIBEXT and TARGET_LIBEXT to the conventional static library +# extension for the being-built-on resp. the target platform. +# +proc proj-lib-extension {} { + set inner {{key} { + switch -glob -- [get-define $key] { + *-*-ming* - *-*-cygwin - *-*-msys { + return ".a" + # ^^^ this was ".lib" until 2025-02-07. See + # https://sqlite.org/forum/forumpost/02db2d4240 + } + default { + return ".a" + } + } + }} + define BUILD_LIBEXT [apply $inner build] + define TARGET_LIBEXT [apply $inner host] +} + +# +# @proj-file-extensions +# +# Calls all of the proj-*-extension functions. +# +proc proj-file-extensions {} { + proj-exe-extension + proj-dll-extension + proj-lib-extension +} + +# +# @proj-affirm-files-exist ?-v? filename... +# +# Expects a list of file names. If any one of them does not exist in +# the filesystem, it fails fatally with an informative message. +# Returns the last file name it checks. If the first argument is -v +# then it emits msg-checking/msg-result messages for each file. +# +proc proj-affirm-files-exist {args} { + set rc "" + set verbose 0 + if {[lindex $args 0] eq "-v"} { + set verbose 1 + set args [lrange $args 1 end] + } + foreach f $args { + if {$verbose} { msg-checking "Looking for $f ... " } + if {![file exists $f]} { + user-error "not found: $f" + } + if {$verbose} { msg-result "" } + set rc $f + } + return rc +} + +# +# @proj-check-emsdk +# +# Emscripten is used for doing in-tree builds of web-based WASM stuff, +# as opposed to WASI-based WASM or WASM binaries we import from other +# places. This is only set up for Unix-style OSes and is untested +# anywhere but Linux. Requires that the --with-emsdk flag be +# registered with autosetup. +# +# It looks for the SDK in the location specified by --with-emsdk. +# Values of "" or "auto" mean to check for the environment var EMSDK +# (which gets set by the emsdk_env.sh script from the SDK) or that +# same var passed to configure. +# +# If the given directory is found, it expects to find emsdk_env.sh in +# that directory, as well as the emcc compiler somewhere under there. +# +# If the --with-emsdk[=DIR] flag is explicitly provided and the SDK is +# not found then a fatal error is generated, otherwise failure to find +# the SDK is not fatal. +# +# Defines the following: +# +# - HAVE_EMSDK = 0 or 1 (this function's return value) +# - EMSDK_HOME = "" or top dir of the emsdk +# - EMSDK_ENV_SH = "" or $EMSDK_HOME/emsdk_env.sh +# - BIN_EMCC = "" or $EMSDK_HOME/upstream/emscripten/emcc +# +# Returns 1 if EMSDK_ENV_SH is found, else 0. If EMSDK_HOME is not empty +# but BIN_EMCC is then emcc was not found in the EMSDK_HOME, in which +# case we have to rely on the fact that sourcing $EMSDK_ENV_SH from a +# shell will add emcc to the $PATH. +# +proc proj-check-emsdk {} { + set emsdkHome [opt-val with-emsdk] + define EMSDK_HOME "" + define EMSDK_ENV_SH "" + define BIN_EMCC "" + set hadValue [llength $emsdkHome] + msg-checking "Emscripten SDK? " + if {$emsdkHome in {"" "auto"}} { + # Check the environment. $EMSDK gets set by sourcing emsdk_env.sh. + set emsdkHome [get-env EMSDK ""] + } + set rc 0 + if {$emsdkHome ne ""} { + define EMSDK_HOME $emsdkHome + set emsdkEnv "$emsdkHome/emsdk_env.sh" + if {[file exists $emsdkEnv]} { + msg-result "$emsdkHome" + define EMSDK_ENV_SH $emsdkEnv + set rc 1 + set emcc "$emsdkHome/upstream/emscripten/emcc" + if {[file exists $emcc]} { + define BIN_EMCC $emcc + } + } else { + msg-result "emsdk_env.sh not found in $emsdkHome" + } + } else { + msg-result "not found" + } + if {$hadValue && 0 == $rc} { + # Fail if it was explicitly requested but not found + proj-fatal "Cannot find the Emscripten SDK" + } + define HAVE_EMSDK $rc + return $rc +} + +# +# @proj-cc-check-Wl-flag ?flag ?args?? +# +# Checks whether the given linker flag (and optional arguments) can be +# passed from the compiler to the linker using one of these formats: +# +# - -Wl,flag[,arg1[,...argN]] +# - -Wl,flag -Wl,arg1 ...-Wl,argN +# +# If so, that flag string is returned, else an empty string is +# returned. +# +proc proj-cc-check-Wl-flag {args} { + cc-with {-link 1} { + # Try -Wl,flag,...args + set fli "-Wl" + foreach f $args { append fli ",$f" } + if {[cc-check-flags $fli]} { + return $fli + } + # Try -Wl,flag -Wl,arg1 ...-Wl,argN + set fli "" + foreach f $args { append fli "-Wl,$f " } + if {[cc-check-flags $fli]} { + return [string trim $fli] + } + return "" + } +} + +# +# @proj-check-rpath +# +# Tries various approaches to handling the -rpath link-time +# flag. Defines LDFLAGS_RPATH to that/those flag(s) or an empty +# string. Returns 1 if it finds an option, else 0. +# +# By default, the rpath is set to $prefix/lib. However, if either of +# --exec-prefix=... or --libdir=... are explicitly passed to +# configure then [get-define libdir] is used (noting that it derives +# from exec-prefix by default). +# +proc proj-check-rpath {} { + if {[proj-opt-was-provided libdir] + || [proj-opt-was-provided exec-prefix]} { + set lp "[get-define libdir]" + } else { + set lp "[get-define prefix]/lib" + } + # If we _don't_ use cc-with {} here (to avoid updating the global + # CFLAGS or LIBS or whatever it is that cc-check-flags updates) then + # downstream tests may fail because the resulting rpath gets + # implicitly injected into them. + cc-with {-link 1} { + if {[cc-check-flags "-rpath $lp"]} { + define LDFLAGS_RPATH "-rpath $lp" + } else { + set wl [proj-cc-check-Wl-flag -rpath $lp] + if {"" eq $wl} { + set wl [proj-cc-check-Wl-flag -R$lp] + } + if {"" eq $wl} { + # HP-UX: https://sqlite.org/forum/forumpost/d80ecdaddd + set wl [proj-cc-check-Wl-flag +b $lp] + } + define LDFLAGS_RPATH $wl + } + } + expr {"" ne [get-define LDFLAGS_RPATH]} +} + +# +# @proj-check-soname ?libname? +# +# Checks whether CC supports the -Wl,-soname,lib... flag. If so, it +# returns 1 and defines LDFLAGS_SONAME_PREFIX to the flag's prefix, to +# which the client would need to append "libwhatever.N". If not, it +# returns 0 and defines LDFLAGS_SONAME_PREFIX to an empty string. +# +# The libname argument is only for purposes of running the flag +# compatibility test, and is not included in the resulting +# LDFLAGS_SONAME_PREFIX. It is provided so that clients may +# potentially avoid some end-user confusion by using their own lib's +# name here (which shows up in the "checking..." output). +# +proc proj-check-soname {{libname "libfoo.so.0"}} { + cc-with {-link 1} { + if {[cc-check-flags "-Wl,-soname,${libname}"]} { + define LDFLAGS_SONAME_PREFIX "-Wl,-soname," + return 1 + } elseif {[cc-check-flags "-Wl,+h,${libname}"]} { + # HP-UX: https://sqlite.org/forum/forumpost/d80ecdaddd + define LDFLAGS_SONAME_PREFIX "-Wl,+h," + return 1 + } else { + define LDFLAGS_SONAME_PREFIX "" + return 0 + } + } +} + +# +# @proj-check-fsanitize ?list-of-opts? +# +# Checks whether CC supports -fsanitize=X, where X is each entry of +# the given list of flags. If any of those flags are supported, it +# returns the string "-fsanitize=X..." where X... is a comma-separated +# list of all flags from the original set which are supported. If none +# of the given options are supported then it returns an empty string. +# +# Example: +# +# set f [proj-check-fsanitize {address bounds-check just-testing}] +# +# Will, on many systems, resolve to "-fsanitize=address,bounds-check", +# but may also resolve to "-fsanitize=address". +# +proc proj-check-fsanitize {{opts {address bounds-strict}}} { + set sup {} + foreach opt $opts { + # -nooutput is used because -fsanitize=hwaddress will otherwise + # pass this test on x86_64, but then warn at build time that + # "hwaddress is not supported for this target". + cc-with {-nooutput 1} { + if {[cc-check-flags "-fsanitize=$opt"]} { + lappend sup $opt + } + } + } + if {[llength $sup] > 0} { + return "-fsanitize=[join $sup ,]" + } + return "" +} + +# +# Internal helper for proj-dump-defs-json. Expects to be passed a +# [define] name and the variadic $args which are passed to +# proj-dump-defs-json. If it finds a pattern match for the given +# $name in the various $args, it returns the type flag for that $name, +# e.g. "-str" or "-bare", else returns an empty string. +# +proc proj-defs-type_ {name spec} { + foreach {type patterns} $spec { + foreach pattern $patterns { + if {[string match $pattern $name]} { + return $type + } + } + } + return "" +} + +# +# Internal helper for proj-defs-format_: returns a JSON-ish quoted +# form of the given string-type values. It only performs the most +# basic of escaping. The input must not contain any control +# characters. +# +proc proj-quote-str_ {value} { + return \"[string map [list \\ \\\\ \" \\\"] $value]\" +} + +# +# An internal impl detail of proj-dump-defs-json. Requires a data +# type specifier, as used by make-config-header, and a value. Returns +# the formatted value or the value $::proj__Config(defs-skip) if the caller +# should skip emitting that value. +# +set ::proj__Config(defs-skip) "-proj-defs-format_ sentinel" +proc proj-defs-format_ {type value} { + switch -exact -- $type { + -bare { + # Just output the value unchanged + } + -none { + set value $::proj__Config(defs-skip) + } + -str { + set value [proj-quote-str_ $value] + } + -auto { + # Automatically determine the type + if {![string is integer -strict $value]} { + set value [proj-quote-str_ $value] + } + } + -array { + set ar {} + foreach v $value { + set v [proj-defs-format_ -auto $v] + if {$::proj__Config(defs-skip) ne $v} { + lappend ar $v + } + } + set value "\[ [join $ar {, }] \]" + } + "" { + set value $::proj__Config(defs-skip) + } + default { + proj-fatal "Unknown type in proj-dump-defs-json: $type" + } + } + return $value +} + +# +# @proj-dump-defs-json outfile ...flags +# +# This function works almost identically to autosetup's +# make-config-header but emits its output in JSON form. It is not a +# fully-functional JSON emitter, and will emit broken JSON for +# complicated outputs, but should be sufficient for purposes of +# emitting most configure vars (numbers and simple strings). +# +# In addition to the formatting flags supported by make-config-header, +# it also supports: +# +# -array {patterns...} +# +# Any defines matching the given patterns will be treated as a list of +# values, each of which will be formatted as if it were in an -auto {...} +# set, and the define will be emitted to JSON in the form: +# +# "ITS_NAME": [ "value1", ...valueN ] +# +# Achtung: if a given -array pattern contains values which themselves +# contains spaces... +# +# define-append foo {"-DFOO=bar baz" -DBAR="baz barre"} +# +# will lead to: +# +# ["-DFOO=bar baz", "-DBAR=\"baz", "barre\""] +# +# Neither is especially satisfactory (and the second is useless), and +# handling of such values is subject to change if any such values ever +# _really_ need to be processed by our source trees. +# +proc proj-dump-defs-json {file args} { + file mkdir [file dirname $file] + set lines {} + lappend args -bare {SIZEOF_* HAVE_DECL_*} -auto HAVE_* + foreach n [lsort [dict keys [all-defines]]] { + set type [proj-defs-type_ $n $args] + set value [proj-defs-format_ $type [get-define $n]] + if {$::proj__Config(defs-skip) ne $value} { + lappend lines "\"$n\": ${value}" + } + } + set buf {} + lappend buf [join $lines ",\n"] + write-if-changed $file $buf { + msg-result "Created $file" + } +} + +# +# @proj-xfer-option-aliases map +# +# Expects a list of pairs of configure flags which have been +# registered with autosetup, in this form: +# +# { alias1 => canonical1 +# aliasN => canonicalN ... } +# +# The names must not have their leading -- part and must be in the +# form which autosetup will expect for passing to [opt-val NAME] and +# friends. +# +# Comment lines are permitted in the input. +# +# For each pair of ALIAS and CANONICAL, if --ALIAS is provided but +# --CANONICAL is not, the value of the former is copied to the +# latter. If --ALIAS is not provided, this is a no-op. If both have +# explicitly been provided a fatal usage error is triggered. +# +# Motivation: autosetup enables "hidden aliases" in [options] lists, +# and elides the aliases from --help output but does no further +# handling of them. For example, when --alias is a hidden alias of +# --canonical and a user passes --alias=X, [opt-val canonical] returns +# no value. i.e. the script must check both [opt-val alias] and +# [opt-val canonical]. The intent here is that this function be +# passed such mappings immediately after [options] is called, to carry +# over any values from hidden aliases into their canonical names, such +# that [opt-value canonical] will return X if --alias=X is passed to +# configure. +# +# That said: autosetup's [opt-str] does support alias forms, but it +# requires that the caller know all possible aliases. It's simpler, in +# terms of options handling, if there's only a single canonical name +# which each down-stream call of [opt-...] has to know. +# +proc proj-xfer-options-aliases {mapping} { + foreach {hidden - canonical} [proj-strip-hash-comments $mapping] { + if {[proj-opt-was-provided $hidden]} { + if {[proj-opt-was-provided $canonical]} { + proj-fatal "both --$canonical and its alias --$hidden were used. Use only one or the other." + } else { + proj-opt-set $canonical [opt-val $hidden] + } + } + } +} + +# +# Arguable/debatable... +# +# When _not_ cross-compiling and CC_FOR_BUILD is _not_ explicitly +# specified, force CC_FOR_BUILD to be the same as CC, so that: +# +# ./configure CC=clang +# +# will use CC_FOR_BUILD=clang, instead of cc, for building in-tree +# tools. This is based off of an email discussion and is thought to +# be likely to cause less confusion than seeing 'cc' invocations +# when when the user passes CC=clang. +# +# Sidebar: if we do this before the cc package is installed, it gets +# reverted by that package. Ergo, the cc package init will tell the +# user "Build C compiler...cc" shortly before we tell them otherwise. +# +proc proj-redefine-cc-for-build {} { + if {![proj-is-cross-compiling] + && [get-define CC] ne [get-define CC_FOR_BUILD] + && "nope" eq [get-env CC_FOR_BUILD "nope"]} { + user-notice "Re-defining CC_FOR_BUILD to CC=[get-define CC]. To avoid this, explicitly pass CC_FOR_BUILD=..." + define CC_FOR_BUILD [get-define CC] + } +} + +# +# @proj-which-linenoise headerFile +# +# Attempts to determine whether the given linenoise header file is of +# the "antirez" or "msteveb" flavor. It returns 2 for msteveb, else 1 +# (it does not validate that the header otherwise contains the +# linenoise API). +# +proc proj-which-linenoise {dotH} { + set srcHeader [proj-file-content $dotH] + if {[string match *userdata* $srcHeader]} { + return 2 + } else { + return 1 + } +} + +# +# @proj-remap-autoconf-dir-vars +# +# "Re-map" the autoconf-conventional --XYZdir flags into something +# which is more easily overridable from a make invocation. +# +# Based off of notes in . +# +# Consider: +# +# $ ./configure --prefix=/foo +# $ make install prefix=/blah +# +# In that make invocation, $(libdir) would, at make-time, normally be +# hard-coded to /foo/lib, rather than /blah/lib. That happens because +# autosetup exports conventional $prefix-based values for the numerous +# autoconfig-compatible XYZdir vars at configure-time. What we would +# normally want, however, is that --libdir derives from the make-time +# $(prefix). The distinction between configure-time and make-time is +# the significant factor there. +# +# This function attempts to reconcile those vars in such a way that +# they will derive, at make-time, from $(prefix) in a conventional +# manner unless they are explicitly overridden at configure-time, in +# which case those overrides takes precedence. +# +# Each autoconf-relvant --XYZ flag which is explicitly passed to +# configure is exported as-is, as are those which default to some +# top-level system directory, e.g. /etc or /var. All which derive +# from either $prefix or $exec_prefix are exported in the form of a +# Makefile var reference, e.g. libdir=${exec_prefix}/lib. Ergo, if +# --exec-prefix=FOO is passed to configure, libdir will still derive, +# at make-time, from whatever exec_prefix is passed to make, and will +# use FOO if exec_prefix is not overridden at make-time. Without this +# post-processing, libdir would be cemented in as FOO/lib at +# configure-time, so could be tedious to override properly via a make +# invocation. +# +proc proj-remap-autoconf-dir-vars {} { + set prefix [get-define prefix] + set exec_prefix [get-define exec_prefix $prefix] + # The following var derefs must be formulated such that they are + # legal for use in (A) makefiles, (B) pkgconfig files, and (C) TCL's + # [subst] command. i.e. they must use the form ${X}. + foreach {flag makeVar makeDeref} { + exec-prefix exec_prefix ${prefix} + datadir datadir ${prefix}/share + mandir mandir ${datadir}/man + includedir includedir ${prefix}/include + bindir bindir ${exec_prefix}/bin + libdir libdir ${exec_prefix}/lib + sbindir sbindir ${exec_prefix}/sbin + sysconfdir sysconfdir /etc + sharedstatedir sharedstatedir ${prefix}/com + localstatedir localstatedir /var + runstatedir runstatedir /run + infodir infodir ${datadir}/info + libexecdir libexecdir ${exec_prefix}/libexec + } { + if {[proj-opt-was-provided $flag]} { + define $makeVar [join [opt-val $flag]] + } else { + define $makeVar [join $makeDeref] + } + # Maintenance reminder: the [join] call is to avoid {braces} + # around the output when someone passes in, + # e.g. --libdir=\${prefix}/foo/bar. Debian's SQLite package build + # script does that. + } +} + +# +# @proj-env-file flag ?default? +# +# If a file named .env-$flag exists, this function returns a +# trimmed copy of its contents, else it returns $dflt. The intended +# usage is that things like developer-specific CFLAGS preferences can +# be stored in .env-CFLAGS. +# +proc proj-env-file {flag {dflt ""}} { + set fn ".env-${flag}" + if {[file readable $fn]} { + return [proj-file-content -trim $fn] + } + return $dflt +} + +# +# @proj-get-env var ?default? +# +# Extracts the value of "environment" variable $var from the first of +# the following places where it's defined: +# +# - Passed to configure as $var=... +# - Exists as an environment variable +# - A file named .env-$var (see [proj-env-file]) +# +# If none of those are set, $dflt is returned. +# +proc proj-get-env {var {dflt ""}} { + get-env $var [proj-env-file $var $dflt] +} + +# +# @proj-scope ?lvl? +# +# Returns the name of the _calling_ proc from ($lvl + 1) levels up the +# call stack (where the caller's level will be 1 up from _this_ +# call). If $lvl would resolve to global scope "global scope" is +# returned and if it would be negative then a string indicating such +# is returned (as opposed to throwing an error). +# +proc proj-scope {{lvl 0}} { + #uplevel [expr {$lvl + 1}] {lindex [info level 0] 0} + set ilvl [info level] + set offset [expr {$ilvl - $lvl - 1}] + if { $offset < 0} { + return "invalid scope ($offset)" + } elseif { $offset == 0} { + return "global scope" + } else { + return [lindex [info level $offset] 0] + } +} + +# +# Deprecated name of [proj-scope]. +# +proc proj-current-scope {{lvl 0}} { + puts stderr \ + "Deprecated proj-current-scope called from [proj-scope 1]. Use proj-scope instead." + proj-scope [incr lvl] +} + +# +# Converts parts of tclConfig.sh to autosetup [define]s. +# +# Expects to be passed the name of a value tclConfig.sh or an empty +# string. It converts certain parts of that file's contents to +# [define]s (see the code for the whole list). If $tclConfigSh is an +# empty string then it [define]s the various vars as empty strings. +# +proc proj-tclConfig-sh-to-autosetup {tclConfigSh} { + set shBody {} + set tclVars { + TCL_INCLUDE_SPEC + TCL_LIBS + TCL_LIB_SPEC + TCL_STUB_LIB_SPEC + TCL_EXEC_PREFIX + TCL_PREFIX + TCL_VERSION + TCL_MAJOR_VERSION + TCL_MINOR_VERSION + TCL_PACKAGE_PATH + TCL_PATCH_LEVEL + TCL_SHLIB_SUFFIX + } + # Build a small shell script which proxies the $tclVars from + # $tclConfigSh into autosetup code... + lappend shBody "if test x = \"x${tclConfigSh}\"; then" + foreach v $tclVars { + lappend shBody "$v= ;" + } + lappend shBody "else . \"${tclConfigSh}\"; fi" + foreach v $tclVars { + lappend shBody "echo define $v {\$$v} ;" + } + lappend shBody "exit" + set shBody [join $shBody "\n"] + #puts "shBody=$shBody\n"; exit + eval [exec echo $shBody | sh] +} + +# +# @proj-tweak-default-env-dirs +# +# This function is not useful before [use system] is called to set up +# --prefix and friends. It should be called as soon after [use system] +# as feasible. +# +# For certain target environments, if --prefix is _not_ passed in by +# the user, set the prefix to an environment-specific default. For +# such environments its does [define prefix ...] and [proj-opt-set +# prefix ...], but it does not process vars derived from the prefix, +# e.g. exec-prefix. To do so it is generally necessary to also call +# proj-remap-autoconf-dir-vars late in the config process (immediately +# before ".in" files are filtered). +# +# Similar modifications may be made for --mandir. +# +# Returns >0 if it modifies the environment, else 0. +# +proc proj-tweak-default-env-dirs {} { + set rc 0 + switch -glob -- [get-define host] { + *-haiku { + if {![proj-opt-was-provided prefix]} { + set hdir /boot/home/config/non-packaged + proj-opt-set prefix $hdir + define prefix $hdir + incr rc + } + if {![proj-opt-was-provided mandir]} { + set hdir /boot/system/documentation/man + proj-opt-set mandir $hdir + define mandir $hdir + incr rc + } + } + } + return $rc +} + +# +# @proj-dot-ins-append file ?fileOut ?postProcessScript?? +# +# Queues up an autosetup [make-template]-style file to be processed +# at a later time using [proj-dot-ins-process]. +# +# $file is the input file. If $fileOut is empty then this function +# derives $fileOut from $file, stripping both its directory and +# extension parts. i.e. it defaults to writing the output to the +# current directory (typically $::autosetup(builddir)). +# +# If $postProcessScript is not empty then, during +# [proj-dot-ins-process], it will be eval'd immediately after +# processing the file. In the context of that script, the vars +# $dotInsIn and $dotInsOut will be set to the input and output file +# names. This can be used, for example, to make the output file +# executable or perform validation on its contents: +# +## proj-dot-ins-append my.sh.in my.sh { +## catch {exec chmod u+x $dotInsOut} +## } +# +# See [proj-dot-ins-process], [proj-dot-ins-list] +# +proc proj-dot-ins-append {fileIn args} { + set srcdir $::autosetup(srcdir) + switch -exact -- [llength $args] { + 0 { + lappend fileIn [file rootname [file tail $fileIn]] "" + } + 1 { + lappend fileIn [join $args] "" + } + 2 { + lappend fileIn {*}$args + } + default { + proj-fatal "Too many arguments: $fileIn $args" + } + } + #puts "******* [proj-scope]: adding [llength $fileIn]-length item: $fileIn" + lappend ::proj__Config(dot-in-files) $fileIn +} + +# +# @proj-dot-ins-list +# +# Returns the current list of [proj-dot-ins-append]'d files, noting +# that each entry is a 3-element list of (inputFileName, +# outputFileName, postProcessScript). +# +proc proj-dot-ins-list {} { + return $::proj__Config(dot-in-files) +} + +# +# @proj-dot-ins-process ?-touch? ?-validate? ?-clear? +# +# Each file which has previously been passed to [proj-dot-ins-append] +# is processed, with its passing its in-file out-file names to +# [proj-make-from-dot-in]. +# +# The intent is that a project accumulate any number of files to +# filter and delay their actual filtering until the last stage of the +# configure script, calling this function at that time. +# +# Optional flags: +# +# -touch: gets passed on to [proj-make-from-dot-in] +# +# -validate: after processing each file, before running the file's +# associated script, if any, it runs the file through +# proj-validate-no-unresolved-ats, erroring out if that does. +# +# -clear: after processing, empty the dot-ins list. This effectively +# makes proj-dot-ins-append available for re-use. +# +proc proj-dot-ins-process {args} { + proj-parse-flags args flags { + -touch "" {return "-touch"} + -clear 0 {expr 1} + -validate 0 {expr 1} + } + #puts "args=$args"; parray flags + if {[llength $args] > 0} { + error "Invalid argument to [proj-scope]: $args" + } + foreach f $::proj__Config(dot-in-files) { + proj-assert {3==[llength $f]} \ + "Expecting proj-dot-ins-list to be stored in 3-entry lists. Got: $f" + lassign $f fIn fOut fScript + #puts "DOING $fIn ==> $fOut" + proj-make-from-dot-in {*}$flags(-touch) $fIn $fOut + if {$flags(-validate)} { + proj-validate-no-unresolved-ats $fOut + } + if {"" ne $fScript} { + uplevel 1 [join [list set dotInsIn $fIn \; \ + set dotInsOut $fOut \; \ + eval \{${fScript}\} \; \ + unset dotInsIn dotInsOut]] + } + } + if {$flags(-clear)} { + set ::proj__Config(dot-in-files) [list] + } +} + +# +# @proj-validate-no-unresolved-ats filenames... +# +# For each filename given to it, it validates that the file has no +# unresolved @VAR@ references. If it finds any, it produces an error +# with location information. +# +# Exception: if a filename matches the pattern {*[Mm]ake*} AND a given +# line begins with a # (not including leading whitespace) then that +# line is ignored for purposes of this validation. The intent is that +# @VAR@ inside of makefile comments should not (necessarily) cause +# validation to fail, as it's sometimes convenient to comment out +# sections during development of a configure script and its +# corresponding makefile(s). +# +proc proj-validate-no-unresolved-ats {args} { + foreach f $args { + set lnno 1 + set isMake [string match {*[Mm]ake*} $f] + foreach line [proj-file-content-list $f] { + if {!$isMake || ![string match "#*" [string trimleft $line]]} { + if {[regexp {(@[A-Za-z0-9_\.]+@)} $line match]} { + error "Unresolved reference to $match at line $lnno of $f" + } + } + incr lnno + } + } +} + +# +# @proj-first-file-found tgtVar fileList +# +# Searches $fileList for an existing file. If one is found, its name +# is assigned to tgtVar and 1 is returned, else tgtVar is set to "" +# and 0 is returned. +# +proc proj-first-file-found {tgtVar fileList} { + upvar $tgtVar tgt + foreach f $fileList { + if {[file exists $f]} { + set tgt $f + return 1 + } + } + set tgt "" + return 0 +} + +# +# Defines $defName to contain makefile recipe commands for re-running +# the configure script with its current set of $::argv flags. This +# can be used to automatically reconfigure. +# +proc proj-setup-autoreconfig {defName} { + define $defName \ + [join [list \ + cd \"$::autosetup(builddir)\" \ + && [get-define AUTOREMAKE "error - missing @AUTOREMAKE@"]]] +} + +# +# @prop-append-to defineName args... +# +# A proxy for Autosetup's [define-append]. Appends all non-empty $args +# to [define-append $defineName]. +# +proc proj-define-append {defineName args} { + foreach a $args { + if {"" ne $a} { + define-append $defineName {*}$a + } + } +} + +# +# @prod-define-amend ?-p|-prepend? ?-d|-define? defineName args... +# +# A proxy for Autosetup's [define-append]. +# +# Appends all non-empty $args to the define named by $defineName. If +# one of (-p | -prepend) are used it instead prepends them, in their +# given order, to $defineName. +# +# If -define is used then each argument is assumed to be a [define]'d +# flag and [get-define X ""] is used to fetch it. +# +# Re. linker flags: typically, -lXYZ flags need to be in "reverse" +# order, with each -lY resolving symbols for -lX's to its left. This +# order is largely historical, and not relevant on all environments, +# but it is technically correct and still relevant on some +# environments. +# +# See: proj-append-to +# +proc proj-define-amend {args} { + set defName "" + set prepend 0 + set isdefs 0 + set xargs [list] + foreach arg $args { + switch -exact -- $arg { + "" {} + -p - -prepend { incr prepend } + -d - -define { incr isdefs } + default { + if {"" eq $defName} { + set defName $arg + } else { + lappend xargs $arg + } + } + } + } + if {"" eq $defName} { + proj-error "Missing defineName argument in call from [proj-scope 1]" + } + if {$isdefs} { + set args $xargs + set xargs [list] + foreach arg $args { + lappend xargs [get-define $arg ""] + } + set args $xargs + } +# puts "**** args=$args" +# puts "**** xargs=$xargs" + + set args $xargs + if {$prepend} { + lappend args {*}[get-define $defName ""] + define $defName [join $args]; # join to eliminate {} entries + } else { + proj-define-append $defName {*}$args + } +} + +# +# @proj-define-to-cflag ?-list? ?-quote? ?-zero-undef? defineName... +# +# Treat each argument as the name of a [define] and renders it like a +# CFLAGS value in one of the following forms: +# +# -D$name +# -D$name=integer (strict integer matches only) +# '-D$name=value' (without -quote) +# '-D$name="value"' (with -quote) +# +# It treats integers as numbers and everything else as a quoted +# string, noting that it does not handle strings which themselves +# contain quotes. +# +# The -zero-undef flag causes no -D to be emitted for integer values +# of 0. +# +# By default it returns the result as string of all -D... flags, +# but if passed the -list flag it will return a list of the +# individual CFLAGS. +# +proc proj-define-to-cflag {args} { + set rv {} + proj-parse-flags args flags { + -list 0 {expr 1} + -quote 0 {expr 1} + -zero-undef 0 {expr 1} + } + foreach d $args { + set v [get-define $d ""] + set li {} + if {"" eq $d} { + set v "-D${d}" + } elseif {[string is integer -strict $v]} { + if {!$flags(-zero-undef) || $v ne "0"} { + set v "-D${d}=$v" + } + } elseif {$flags(-quote)} { + set v "'-D${d}=\"$v\"'" + } else { + set v "'-D${d}=$v'" + } + lappend rv $v + } + expr {$flags(-list) ? $rv : [join $rv]} +} + + +if {0} { + # Turns out that autosetup's [options-add] essentially does exactly + # this... + + # A list of lists of Autosetup [options]-format --flags definitions. + # Append to this using [proj-options-add] and use + # [proj-options-combine] to merge them into a single list for passing + # to [options]. + # + set ::proj__Config(extra-options) {} + + # @proj-options-add list + # + # Adds a list of options to the pending --flag processing. It must be + # in the format used by Autosetup's [options] function. + # + # This will have no useful effect if called from after [options] + # is called. + # + # Use [proj-options-combine] to get a combined list of all added + # options. + # + # PS: when writing this i wasn't aware of autosetup's [options-add], + # works quite similarly. Only the timing is different. + proc proj-options-add {list} { + lappend ::proj__Config(extra-options) $list + } + + # @proj-options-combine list1 ?...listN? + # + # Expects each argument to be a list of options compatible with + # autosetup's [options] function. This function concatenates the + # contents of each list into a new top-level list, stripping the outer + # list part of each argument, and returning that list + # + # If passed no arguments, it uses the list generated by calls to + # [proj-options-add]. + proc proj-options-combine {args} { + set rv [list] + if {0 == [llength $args]} { + set args $::proj__Config(extra-options) + } + foreach e $args { + lappend rv {*}$e + } + return $rv + } +}; # proj-options-* + +# Internal cache for use via proj-cache-*. +array set proj__Cache {} + +# +# @proj-cache-key arg {addLevel 0} +# +# Helper to generate cache keys for [proj-cache-*]. +# +# $addLevel should almost always be 0. +# +# Returns a cache key for the given argument: +# +# integer: relative call stack levels to get the scope name of for +# use as a key. [proj-scope [expr {1 + $arg + addLevel}]] is +# then used to generate the key. i.e. the default of 0 uses the +# calling scope's name as the key. +# +# Anything else: returned as-is +# +proc proj-cache-key {arg {addLevel 0}} { + if {[string is integer -strict $arg]} { + return [proj-scope [expr {$arg + $addLevel + 1}]] + } + return $arg +} + +# +# @proj-cache-set ?-key KEY? ?-level 0? value +# +# Sets a feature-check cache entry with the given key. +# +# See proj-cache-key for -key's and -level's semantics, noting that +# this function adds one to -level for purposes of that call. +proc proj-cache-set {args} { + proj-parse-flags args flags { + -key => 0 + -level => 0 + } + lassign $args val + set key [proj-cache-key $flags(-key) [expr {1 + $flags(-level)}]] + #puts "** fcheck set $key = $val" + set ::proj__Cache($key) $val +} + +# +# @proj-cache-remove ?key? ?addLevel? +# +# Removes an entry from the proj-cache. +proc proj-cache-remove {{key 0} {addLevel 0}} { + set key [proj-cache-key $key [expr {1 + $addLevel}]] + set rv "" + if {[info exists ::proj__Cache($key)]} { + set rv $::proj__Cache($key) + unset ::proj__Cache($key) + } + return $rv; +} + +# +# @proj-cache-check ?-key KEY? ?-level LEVEL? tgtVarName +# +# Checks for a feature-check cache entry with the given key. +# +# If the feature-check cache has a matching entry then this function +# assigns its value to tgtVar and returns 1, else it assigns tgtVar to +# "" and returns 0. +# +# See proj-cache-key for $key's and $addLevel's semantics, noting that +# this function adds one to $addLevel for purposes of that call. +proc proj-cache-check {args} { + proj-parse-flags args flags { + -key => 0 + -level => 0 + } + lassign $args tgtVar + upvar $tgtVar tgt + set rc 0 + set key [proj-cache-key $flags(-key) [expr {1 + $flags(-level)}]] + #puts "** fcheck get key=$key" + if {[info exists ::proj__Cache($key)]} { + set tgt $::proj__Cache($key) + incr rc + } else { + set tgt "" + } + return $rc +} + +# +# @proj-coalesce ...args +# +# Returns the first argument which is not empty (eq ""), or an empty +# string on no match. +proc proj-coalesce {args} { + foreach arg $args { + if {"" ne $arg} { + return $arg + } + } + return "" +} + +# +# @proj-parse-flags argvListName targetArrayName {prototype} +# +# A helper to parse flags from proc argument lists. +# +# The first argument is the name of a var holding the args to +# parse. It will be overwritten, possibly with a smaller list. +# +# The second argument is the name of an array variable to create in +# the caller's scope. +# +# The third argument, $prototype, is a description of how to handle +# the flags. Each entry in that list must be in one of the +# following forms: +# +# -flag defaultValue ?-literal|-call|-apply? +# script|number|incr|proc-name|{apply $aLambda} +# +# -flag* ...as above... +# +# -flag => defaultValue ?-call proc-name-and-args|-apply lambdaExpr? +# +# -flag* => ...as above... +# +# :PRAGMA +# +# The first two forms represents a basic flag with no associated +# following argument. The third and fourth forms, called arg-consuming +# flags, extract the value from the following argument in $argvName +# (pneumonic: => points to the next argument.). The :PRAGMA form +# offers a way to configure certain aspects of this call. +# +# If $argv contains any given flag from $prototype, its default value +# is overridden depending on several factors: +# +# - If the -literal flag is used, or the flag's script is a number, +# value is used verbatim. +# +# - Else if the -call flag is used, the argument must be a proc name +# and any leading arguments, e.g. {apply $myLambda}. The proc is passed +# the (flag, value) as arguments (non-consuming flags will get +# passed the flag's current/starting value and consuming flags will +# get the next argument). Its result becomes the result of the +# flag. +# +# - Else if -apply X is used, it's effectively shorthand for -call +# {apply X}. Its argument may either be a $lambaRef or a {{f v} +# {body}} construct. +# +# - Else if $script is one of the following values, it is treated as +# the result of... +# +# - incr: increments the current value of the flag. +# +# - Else $script is eval'd to get its result value. That result +# becomes the new flag value for $tgtArrayName(-flag). This +# function intercepts [return $val] from eval'ing $script. Any +# empty script will result in the flag having "" assigned to it. +# +# Unless the -flag has a trailing asterisk, e.g. -flag*, this function +# assumes that each flag is unique, and using a flag more than once +# causes an error to be triggered. the -flag* forms works similarly +# except that may appear in $argv any number of times: +# +# - For non-arg-consuming flags, each invocation of -flag causes the +# result of $script to overwrite the previous value. e.g. so +# {-flag* {x} {incr foo}} has a default value of x, but passing in +# -flag twice would change it to the result of incrementing foo +# twice. This form can be used to implement, e.g., increasing +# verbosity levels by passing -verbose multiple times. +# +# - For arg-consuming flags, the given flag starts with value X, but +# if the flag is provided in $argv, the default is cleared, then +# each instance of -flag causes its value to be appended to the +# result, so {-flag* => {a b c}} defaults to {a b c}, but passing +# in -flag y -flag z would change it to {y z}, not {a b c y z}.. +# +# By default, the args list is only inspected until the first argument +# which is not described by $prototype. i.e. the first "non-flag" (not +# counting values consumed for flags defined like -flag => default). +# The :all-flags pragma (see below) can modify this behavior. +# +# If a "--" flag is encountered, no more arguments are inspected as +# flags unless the :all-flags pragma (see below) is in effect. The +# first instance of "--" is removed from the target result list but +# all remaining instances of "--" are are passed through. +# +# Any argvName entries not described in $prototype are considered to +# be "non-flags" for purposes of this function, even if they +# ostensibly look like flags. +# +# Returns the number of flags it processed in $argvName, not counting +# "--". +# +# Example: +# +## set args [list -foo -bar {blah} -z 8 9 10 -theEnd] +## proj-parse-flags args flags { +## -foo 0 {expr 1} +## -bar => 0 +## -no-baz 1 {return 0} +## -z 0 2 +## } +# +# After that $flags would contain {-foo 1 -bar {blah} -no-baz 1 -z 2} +# and $args would be {8 9 10 -theEnd}. +# +# Pragmas: +# +# Passing :PRAGMAS to this function may modify how it works. The +# following pragmas are supported (note the leading ":"): +# +# :all-flags indicates that the whole input list should be scanned, +# not stopping at the first non-flag or "--". +# +proc proj-parse-flags {argvName tgtArrayName prototype} { + upvar $argvName argv + upvar $tgtArrayName outFlags + array set flags {}; # staging area + array set blob {}; # holds markers for various per-key state and options + set incrSkip 1; # 1 if we stop at the first non-flag, else 0 + # Parse $prototype for flag definitions... + set n [llength $prototype] + set checkProtoFlag { + #puts "**** checkProtoFlag #$i of $n k=$k fv=$fv" + switch -exact -- $fv { + -literal { + proj-assert {![info exists blob(${k}.consumes)]} + set blob(${k}.script) [list expr [lindex $prototype [incr i]]] + } + -apply { + set fv [lindex $prototype [incr i]] + if {2 == [llength $fv]} { + # Treat this as a lambda literal + set fv [list $fv] + } + lappend blob(${k}.call) "apply $fv" + } + -call { + # arg is either a proc name or {apply $aLambda} + set fv [lindex $prototype [incr i]] + lappend blob(${k}.call) $fv + } + default { + proj-assert {![info exists blob(${k}.consumes)]} + set blob(${k}.script) $fv + } + } + if {$i >= $n} { + proj-error -up "[proj-scope]: Missing argument for $k flag" + } + } + for {set i 0} {$i < $n} {incr i} { + set k [lindex $prototype $i] + #puts "**** #$i of $n k=$k" + + # Check for :PRAGMA... + switch -exact -- $k { + :all-flags { + set incrSkip 0 + continue + } + } + + proj-assert {[string match -* $k]} \ + "Invalid argument: $k" + + if {[string match {*\*} $k]} { + # Re-map -foo* to -foo and flag -foo as a repeatable flag + set k [string map {* ""} $k] + incr blob(${k}.multi) + } + + if {[info exists flags($k)]} { + proj-error -up "[proj-scope]: Duplicated prototype for flag $k" + } + + switch -exact -- [lindex $prototype [expr {$i + 1}]] { + => { + # -flag => DFLT ?-subflag arg? + incr i 2 + if {$i >= $n} { + proj-error -up "[proj-scope]: Missing argument for $k => flag" + } + incr blob(${k}.consumes) + set vi [lindex $prototype $i] + if {$vi in {-apply -call}} { + proj-error -up "[proj-scope]: Missing default value for $k flag" + } else { + set fv [lindex $prototype [expr {$i + 1}]] + if {$fv in {-apply -call}} { + incr i + eval $checkProtoFlag + } + } + } + default { + # -flag VALUE ?flag? SCRIPT + set vi [lindex $prototype [incr i]] + set fv [lindex $prototype [incr i]] + eval $checkProtoFlag + } + } + #puts "**** #$i of $n k=$k vi=$vi" + set flags($k) $vi + } + #puts "-- flags"; parray flags + #puts "-- blob"; parray blob + set rc 0 + set rv {}; # staging area for the target argv value + set skipMode 0 + set n [llength $argv] + # Now look for those flags in $argv... + for {set i 0} {$i < $n} {incr i} { + set arg [lindex $argv $i] + #puts "-- [proj-scope] arg=$arg" + if {$skipMode} { + lappend rv $arg + } elseif {"--" eq $arg} { + # "--" is the conventional way to end processing of args + if {[incr blob(--)] > 1} { + # Elide only the first one + lappend rv $arg + } + incr skipMode $incrSkip + } elseif {[info exists flags($arg)]} { + # A known flag... + set isMulti [info exists blob(${arg}.multi)] + incr blob(${arg}.seen) + if {1 < $blob(${arg}.seen) && !$isMulti} { + proj-error -up [proj-scope] "$arg flag was used multiple times" + } + set vMode 0; # 0=as-is, 1=eval, 2=call + set isConsuming [info exists blob(${arg}.consumes)] + if {$isConsuming} { + incr i + if {$i >= $n} { + proj-error -up [proj-scope] "is missing argument for $arg flag" + } + set vv [lindex $argv $i] + } elseif {[info exists blob(${arg}.script)]} { + set vMode 1 + set vv $blob(${arg}.script) + } else { + set vv $flags($arg) + } + + if {[info exists blob(${arg}.call)]} { + set vMode 2 + set vv [concat {*}$blob(${arg}.call) $arg $vv] + } elseif {$isConsuming} { + proj-assert {!$vMode} + # fall through + } elseif {"" eq $vv || [string is double -strict $vv]} { + set vMode 0 + } elseif {$vv in {incr}} { + set vMode 0 + switch -exact $vv { + incr { + set xx $flags($k); incr xx; set vv $xx; unset xx + } + default { + proj-error "Unhandled \$vv value $vv" + } + } + } else { + set vv [list eval $vv] + set vMode 1 + } + if {$vMode} { + set code [catch [list uplevel 1 $vv] vv xopt] + if {$code ni {0 2}} { + return {*}$xopt $vv + } + } + if {$isConsuming && $isMulti} { + if {1 == $blob(${arg}.seen)} { + # On the first hit, overwrite the default with a new list. + set flags($arg) [list $vv] + } else { + # On subsequent hits, append to the list. + lappend flags($arg) $vv + } + } else { + set flags($arg) $vv + } + incr rc + } else { + # Non-flag + incr skipMode $incrSkip + lappend rv $arg + } + } + set argv $rv + array set outFlags [array get flags] + #puts "-- rv=$rv argv=$argv flags="; parray flags + return $rc +}; # proj-parse-flags + +# +# Older (deprecated) name of proj-parse-flags. +# +proc proj-parse-simple-flags {args} { + tailcall proj-parse-flags {*}$args +} + +if {$::proj__Config(self-tests)} { + set __ova $::proj__Config(verbose-assert); + set ::proj__Config(verbose-assert) 1 + puts "Running [info script] self-tests..." + # proj-cache... + apply {{} { + #proj-warn "Test code for proj-cache" + proj-assert {![proj-cache-check -key here check]} + proj-assert {"here" eq [proj-cache-key here]} + proj-assert {"" eq $check} + proj-cache-set -key here thevalue + proj-assert {[proj-cache-check -key here check]} + proj-assert {"thevalue" eq $check} + + proj-assert {![proj-cache-check check]} + #puts "*** key = ([proj-cache-key 0])" + proj-assert {"" eq $check} + proj-cache-set abc + proj-assert {[proj-cache-check check]} + proj-assert {"abc" eq $check} + + #parray ::proj__Cache; + proj-assert {"" ne [proj-cache-remove]} + proj-assert {![proj-cache-check check]} + proj-assert {"" eq [proj-cache-remove]} + proj-assert {"" eq $check} + }} + + # proj-parse-flags ... + apply {{} { + set foo 3 + set argv {-a "hi - world" -b -b -b -- -a {bye bye} -- -d -D c -a "" --} + proj-parse-flags argv flags { + :all-flags + -a* => "gets overwritten" + -b* 7 {incr foo} + -d 1 0 + -D 0 1 + } + + #puts "-- argv = $argv"; parray flags; + proj-assert {"-- c --" eq $argv} + proj-assert {$flags(-a) eq "{hi - world} {bye bye} {}"} + proj-assert {$foo == 6} + proj-assert {$flags(-b) eq $foo} + proj-assert {$flags(-d) == 0} + proj-assert {$flags(-D) == 1} + set foo 0 + foreach x $flags(-a) { + proj-assert {$x in {{hi - world} {bye bye} {}}} + incr foo + } + proj-assert {3 == $foo} + + set argv {-a {hi world} -b -maybe -- -a {bye bye} -- -b c --} + set foo 0 + proj-parse-flags argv flags { + -a => "aaa" + -b 0 {incr foo} + -maybe no -literal yes + } + #parray flags; puts "--- argv = $argv" + proj-assert {"-a {bye bye} -- -b c --" eq $argv} + proj-assert {$flags(-a) eq "hi world"} + proj-assert {1 == $flags(-b)} + proj-assert {"yes" eq $flags(-maybe)} + + set argv {-f -g -a aaa -M -M -M -L -H -A AAA a b c} + set foo 0 + set myLambda {{flag val} { + proj-assert {$flag in {-f -g -M}} + #puts "myLambda flag=$flag val=$val" + incr val + }} + proc myNonLambda {flag val} { + proj-assert {$flag in {-A -a}} + #puts "myNonLambda flag=$flag val=$val" + concat $val $val + } + proj-parse-flags argv flags { + -f 0 -call {apply $myLambda} + -g 2 -apply $myLambda + -h 3 -apply $myLambda + -H 30 33 + -a => aAAAa -apply {{f v} { + set v + }} + -A => AaaaA -call myNonLambda + -B => 17 -call myNonLambda + -M* 0 -apply $myLambda + -L "" -literal $myLambda + } + rename myNonLambda "" + #puts "--- argv = $argv"; parray flags + proj-assert {$flags(-f) == 1} + proj-assert {$flags(-g) == 3} + proj-assert {$flags(-h) == 3} + proj-assert {$flags(-H) == 33} + proj-assert {$flags(-a) == {aaa}} + proj-assert {$flags(-A) eq "AAA AAA"} + proj-assert {$flags(-B) == 17} + proj-assert {$flags(-M) == 3} + proj-assert {$flags(-L) eq $myLambda} + + set argv {-touch -validate} + proj-parse-flags argv flags { + -touch "" {return "-touch"} + -validate 0 1 + } + #puts "----- argv = $argv"; parray flags + proj-assert {$flags(-touch) eq "-touch"} + proj-assert {$flags(-validate) == 1} + proj-assert {$argv eq {}} + + set argv {-i -i -i} + proj-parse-flags argv flags { + -i* 0 incr + } + proj-assert {3 == $flags(-i)} + }} + set ::proj__Config(verbose-assert) $__ova + unset __ova + puts "Done running [info script] self-tests." +}; # proj- API self-tests diff --git a/autosetup/sqlite-config.tcl b/autosetup/sqlite-config.tcl new file mode 100644 index 0000000000..7c798b31a2 --- /dev/null +++ b/autosetup/sqlite-config.tcl @@ -0,0 +1,2237 @@ +# This file holds functions for autosetup which are specific to the +# sqlite build tree. They are in this file, instead of auto.def, so +# that they can be reused in the autoconf sub-tree. This file requires +# functions from the project-agnostic proj.tcl. + +if {[string first " " $autosetup(srcdir)] != -1} { + user-error "The pathname of the source tree\ + may not contain space characters" +} +if {[string first " " $autosetup(builddir)] != -1} { + user-error "The pathname of the build directory\ + may not contain space characters" +} + +use proj +# +# We want the package version info to be emitted early on, but doing +# so requires a bit of juggling. We have to [use system] for +# --prefix=... to work and to emit the Host/Build system info, but we +# don't want those to interfere with --help output. +define PACKAGE_VERSION [proj-file-content -trim $::autosetup(srcdir)/VERSION] +if {"--help" ni $::argv} { + msg-result "Configuring SQLite version [get-define PACKAGE_VERSION]" +} +use system ; # Will output "Host System" and "Build System" lines +if {"--help" ni $::argv} { + proj-tweak-default-env-dirs + msg-result "Source dir = $::autosetup(srcdir)" + msg-result "Build dir = $::autosetup(builddir)" + use cc cc-db cc-shared cc-lib pkg-config +} + +# +# Object for communicating certain config-time state across various +# auto.def-related pieces. +array set sqliteConfig [subst [proj-strip-hash-comments { + # + # Gets set by [sqlite-configure] (the main configure script driver). + build-mode unknown + # + # Gets set to 1 when using jimsh for code generation. May affect + # later decisions. + use-jim-for-codegen 0 + # + # Set to 1 when cross-compiling This value may be changed by certain + # build options, so it's important that config code which checks for + # cross-compilation uses this var instead of + # [proj-is-cross-compiling]. + is-cross-compiling [proj-is-cross-compiling] + # + # Pass msg-debug=1 to configure to enable obnoxiously loud output + # from [msg-debug]. + msg-debug-enabled 0 + # + # Output file for --dump-defines. Intended only for build debugging + # and not part of the public build interface. + dump-defines-txt ./config.defines.txt + # + # If not empty then --dump-defines will dump not only + # (dump-defines-txt) but also a JSON file named after this option's + # value. + dump-defines-json "" + + # + # The list of feature --flags which the --all flag implies. This + # requires special handling in a few places. + # + all-flag-enables {fts4 fts5 rtree geopoly session dbpage dbstat carray} + + # + # Default value for the --all flag. Can hypothetically be modified + # by non-canonical builds (it was added for a Tcl extension build + # mode which was eventually removed). + # + all-flag-default 0 +}]] + +######################################################################## +# Processes all configure --flags for this build, run build-specific +# config checks, then finalize the configure process. $buildMode must +# be one of (canonical, autoconf), and others may be added in the +# future. After bootstrapping, $configScript is eval'd in the caller's +# scope, then post-configuration finalization is run. $configScript is +# intended to hold configure code which is specific to the given +# $buildMode, with the caveat that _some_ build-specific code is +# encapsulated in the configuration finalization step. +# +# The intent is that all (or almost all) build-mode-specific +# configuration goes inside the $configScript argument to this +# function, and that an auto.def file contains only two commands: +# +# use sqlite-config +# sqlite-configure BUILD_NAME { build-specific configure script } +# +# There are snippets of build-mode-specific decision-making in +# [sqlite-configure-finalize], which gets run after $configScript. +proc sqlite-configure {buildMode configScript} { + proj-assert {$::sqliteConfig(build-mode) eq "unknown"} \ + "sqlite-configure must not be called more than once" + set allBuildModes {canonical autoconf} + if {$buildMode ni $allBuildModes} { + user-error "Invalid build mode: $buildMode. Expecting one of: $allBuildModes" + } + if {$::sqliteConfig(all-flag-default)} { + set allFlagHelp "Disable these extensions: $::sqliteConfig(all-flag-enables)" + } else { + set allFlagHelp "Enable these extensions: $::sqliteConfig(all-flag-enables)" + } + + set ::sqliteConfig(build-mode) $buildMode + ######################################################################## + # A gentle introduction to flags handling in autosetup + # + # Reference: https://msteveb.github.io/autosetup/developer/ + # + # All configure flags must be described in one or more calls to + # autosetup's [options] and [options-add] functions. The general + # syntax of the single argument to those functions is a list contain + # a mapping of flags to help text: + # + # FLAG => {Help text} + # + # Where FLAG can have any of the following formats: + # + # boolopt => "a boolean option which defaults to disabled" + # boolopt2=1 => "a boolean option which defaults to enabled" + # stringopt: => "an option which takes an argument, e.g. --stringopt=value" + # stringopt:DESCR => As for stringopt: with a description for the value + # stringopt2:=value => "an option where the argument is optional and defaults to 'value'" + # optalias booltopt3 => "a boolean with a hidden alias. --optalias is not shown in --help" + # + # Autosetup does no small amount of specialized handling for flags, + # especially booleans. Each bool-type --FLAG implicitly gets + # --enable-FLAG and --disable-FLAG forms. That can lead lead to some + # confusion when writing help text. For example: + # + # options { json=1 {Disable JSON functions} } + # + # The reason the help text says "disable" is because a boolean option + # which defaults to true is, in the --help text, rendered as: + # + # --disable-json Disable JSON functions + # + # Whereas a bool flag which defaults to false will instead render as: + # + # --enable-FLAG + # + # Non-boolean flags, in contrast, use the names specifically given to + # them in the [options] invocation. e.g. "with-tcl" is the --with-tcl + # flag. + # + # Fetching values for flags: + # + # booleans: use one of: + # - [opt-bool FLAG] is autosetup's built-in command for this, but we + # have some convenience variants: + # - [proj-opt-truthy FLAG] + # - [proj-opt-if-truthy FLAG {THEN} {ELSE}] + # + # Non-boolean (i.e. string) flags: + # - [opt-val FLAG ?default?] + # - [opt-str ...] - see the docs in ./autosetup/autosetup + # + # [proj-opt-was-provided] can be used to determine whether a flag was + # explicitly provided, which is often useful for distinguishing from + # the case of a default value. + ######################################################################## + set allFlags { + # Structure: a list of M {Z} pairs, where M is a descriptive + # option group name and Z is a list of X Y pairs. X is a list of + # $buildMode name(s) to which the Y flags apply, or {*} to apply + # to all builds. Y is a {block} in the form expected by + # autosetup's [options] and [options-add] command. Each block + # which is applicable to $buildMode is passed on to + # [options-add]. The order of each Y and sub-Y is retained, which + # is significant for rendering of --help. + # + # Maintenance note: [options] does not support comments in + # options, but we filter this object through + # [proj-strip-hash-comments] to remove them before passing them on + # to [options]. + + # When writing {help text blocks}, be aware that: + # + # A) autosetup formats them differently if the {block} starts with + # a newline: it starts left-aligned, directly under the --flag, and + # the rest of the block is pasted verbatim rather than + # pretty-printed. + # + # B) Vars and commands are NOT expanded, but we use a [subst] call + # below which will replace (only) $var refs. + + # Options for how to build the library + build-modes { + {canonical autoconf} { + shared=1 => {Disable build of shared library} + static=1 => {Disable build of static library} + } + {canonical} { + amalgamation=1 => {Disable the amalgamation and instead build all files separately} + } + } + + # Library-level features and defaults + lib-features { + {*} { + threadsafe=1 => {Disable mutexing} + with-tempstore:=no => {Use an in-RAM database for temporary tables: never,no,yes,always} + load-extension=1 => {Disable loading of external extensions} + # ^^^ one of the downstream custom builds overrides the load-extension default to 0, which + # confuses the --help text generator. https://github.com/msteveb/autosetup/issues/77 + math=1 => {Disable math functions} + json=1 => {Disable JSON functions} + memsys5 => {Enable MEMSYS5} + memsys3 => {Enable MEMSYS3} + fts3 => {Enable the FTS3 extension} + fts4 => {Enable the FTS4 extension} + fts5 => {Enable the FTS5 extension} + update-limit => {Enable the UPDATE/DELETE LIMIT clause} + geopoly => {Enable the GEOPOLY extension} + rtree => {Enable the RTREE extension} + session => {Enable the SESSION extension} + dbpage => {Enable the sqlite3_dbpage extension} + dbstat => {Enable the sqlite3_dbstat extension} + carray=1 => {Disable the CARRAY extension} + all=$::sqliteConfig(all-flag-default) => {$allFlagHelp} + largefile=1 + => {This legacy flag has no effect on the library but may influence + the generated sqlite_cfg.h by adding #define HAVE_LFS} + } + {canonical} { + column-metadata => {Enable the column metadata APIs} + # ^^^ Affects how sqlite3.c is generated, so is not available in + # the autoconf build. + } + } + + # Options for TCL support + tcl { + {canonical} { + tcl=1 + => {Disable components which require TCL, including all tests. + This tree requires TCL for code generation but can use the in-tree + copy of autosetup/jimsh0.c for that. The SQLite TCL extension and the + test code require a canonical tclsh.} + with-tcl:DIR + => {Directory containing tclConfig.sh or a directory one level up from + that, from which we can derive a directory containing tclConfig.sh. + A dir name of "prefix" is equivalent to the directory specified by + the --prefix flag.} + with-tclsh:PATH + => {Full pathname of tclsh to use. It is used for (A) trying to find + tclConfig.sh and (B) all TCL-based code generation. Use --with-tcl + unless you have a specific need for this flag. Warning: if its + containing dir has multiple tclsh versions, it may select the + wrong tclConfig.sh!} + static-tclsqlite3=0 + => {Statically-link tclsqlite3. This only works if TCL support is + enabled and all requisite libraries are available in + static form. Note that glibc is unable to fully statically + link certain libraries required by tclsqlite3, so this won't + work on most Linux environments.} + } + } + + # Options for line-editing modes for the CLI shell + line-editing { + {canonical autoconf} { + readline=1 + => {Disable readline support} + # --with-readline-lib is a backwards-compatible alias for + # --with-readline-ldflags + with-readline-lib: + with-readline-ldflags:=auto + => {Readline LDFLAGS, e.g. -lreadline -lncurses} + # --with-readline-inc is a backwards-compatible alias for + # --with-readline-cflags. + with-readline-inc: + with-readline-cflags:=auto + => {Readline CFLAGS, e.g. -I/path/to/includes} + with-readline-header:PATH + => {Full path to readline.h, from which --with-readline-cflags will be derived} + with-linenoise:DIR + => {Source directory for linenoise.c and linenoise.h} + editline=0 + => {Enable BSD editline support} + } + } + + # Options for ICU: International Components for Unicode + icu { + {*} { + with-icu-ldflags:LDFLAGS + => {Enable SQLITE_ENABLE_ICU and add the given linker flags for the + ICU libraries. e.g. on Ubuntu systems, try '-licui18n -licuuc -licudata'.} + with-icu-cflags:CFLAGS + => {Apply extra CFLAGS/CPPFLAGS necessary for building with ICU. + e.g. -I/usr/local/include} + with-icu-config:=auto + => {Enable SQLITE_ENABLE_ICU. Value must be one of: auto, pkg-config, + /path/to/icu-config} + icu-collations=0 + => {Enable SQLITE_ENABLE_ICU_COLLATIONS. Requires --with-icu-ldflags=... + or --with-icu-config} + } + } + + # Options for exotic/alternative build modes + alternative-builds { + {canonical autoconf} { + with-wasi-sdk:=/opt/wasi-sdk + => {Top-most dir of the wasi-sdk for a WASI build} + } + + {*} { + # Note that --static-cli-shell has a completely different + # meaning from --static-shell in the autoconf build! + # --[disable-]static-shell is a legacy flag which we can't + # remove without breaking downstream builds. + static-cli-shell=0 + => {Statically-link the sqlite3 CLI shell. + This only works if the requisite libraries are all available in + static form.} + } + + {canonical} { + static-shells=0 + => {Shorthand for --static-cli-shell --static-tclsqlite3} + + with-emsdk:=auto + => {Top-most dir of the Emscripten SDK installation. + Needed only by ext/wasm. Default=EMSDK env var.} + + amalgamation-extra-src:FILES + => {Space-separated list of source files to append as-is to the resulting + sqlite3.c amalgamation file. May be provided multiple times.} + } + } + + # Options primarily for downstream packagers/package maintainers + packaging { + {autoconf} { + # --disable-static-shell: https://sqlite.org/forum/forumpost/cc219ee704 + # Note that this has a different meaning from --static-cli-shell in the + # canonical build! + static-shell=1 + => {Link the sqlite3 shell app against the DLL instead of embedding sqlite3.c} + } + {canonical autoconf} { + rpath=1 => {Disable use of the rpath linker flag} + # soname: https://sqlite.org/src/forumpost/5a3b44f510df8ded + soname:=legacy + => {SONAME for libsqlite3.so. "none", or not using this flag, sets no + soname. "legacy" sets it to its historical value of + libsqlite3.so.0. A value matching the glob "libsqlite3.*" sets + it to that literal value. Any other value is assumed to be a + suffix which gets applied to "libsqlite3.so.", + e.g. --soname=9.10 equates to "libsqlite3.so.9.10".} + # dll-basename: https://sqlite.org/forum/forumpost/828fdfe904 + dll-basename:=auto + => {Specifies the base name of the resulting DLL file. + If not provided, "libsqlite3" is usually assumed but on some platforms + a platform-dependent default is used. On some platforms this flag + gets automatically enabled if it is not provided. Use "default" to + explicitly disable platform-dependent activation on such systems.} + # out-implib: https://sqlite.org/forum/forumpost/0c7fc097b2 + out-implib:=auto + => {Enable use of --out-implib linker flag to generate an + "import library" for the DLL. The output's base name is + specified by this flag's value, with "auto" meaning to figure + out a name automatically. On some platforms this flag gets + automatically enabled if it is not provided. Use "none" to + explicitly disable this feature on such platforms.} + } + } + + # Options mostly for sqlite's own development + developer { + {*} { + # Note that using the --debug/--enable-debug flag here + # requires patching autosetup/autosetup to rename its builtin + # --debug to --autosetup-debug. See details in + # autosetup/README.md#patching. + with-debug=0 + debug=0 + => {Enable debug build flags. This option will impact performance by + as much as 4x, as it includes large numbers of assert()s in + performance-critical loops. Never use --debug for production + builds.} + scanstatus + => {Enable the SQLITE_ENABLE_STMT_SCANSTATUS feature flag} + } + {canonical} { + dev + => {Enable dev-mode build: automatically enables certain other flags} + test-status + => {Enable status of tests} + gcov=0 + => {Enable coverage testing using gcov} + linemacros + => {Enable #line macros in the amalgamation} + dynlink-tools + => {Dynamically link libsqlite3 to certain tools which normally statically embed it} + asan-fsanitize:=auto + => {Comma- or space-separated list of -fsanitize flags for use with the + fuzzcheck-asan tool. Only those which the compiler claims to support + will actually be used. May be provided multiple times.} + } + {*} { + dump-defines=0 + => {Dump autosetup defines to $::sqliteConfig(dump-defines-txt) + (for build debugging)} + } + } + }; # $allFlags + + set allFlags [proj-strip-hash-comments $allFlags] + # ^^^ lappend of [sqlite-custom-flags] introduces weirdness if + # we delay [proj-strip-hash-comments] until after that. + + ######################################################################## + # sqlite-custom.tcl is intended only for vendor-branch-specific + # customization. See autosetup/README.md#branch-customization for + # details. + if {[file exists $::autosetup(libdir)/sqlite-custom.tcl]} { + uplevel 1 {source $::autosetup(libdir)/sqlite-custom.tcl} + } + + if {[llength [info proc sqlite-custom-flags]] > 0} { + # sqlite-custom-flags is assumed to be imported via + # autosetup/sqlite-custom.tcl. + set scf [sqlite-custom-flags] + if {"" ne $scf} { + lappend allFlags sqlite-custom-flags $scf + } + } + + #lappend allFlags just-testing {{*} {soname:=duplicateEntry => {x}}} + + # Filter allFlags to create the set of [options] legal for this build + foreach {group XY} [subst -nobackslashes -nocommands $allFlags] { + foreach {X Y} $XY { + if { $buildMode in $X || "*" in $X } { + options-add $Y + } + } + } + + if {[catch {options {}} msg xopts]} { + # Workaround for + # where [options] behaves oddly on _some_ TCL builds when it's + # called from deeper than the global scope. + dict incr xopts -level + return {*}$xopts $msg + } + sqlite-configure-phase1 $buildMode + uplevel 1 $configScript + sqlite-configure-finalize +}; # sqlite-configure + +######################################################################## +# Runs "phase 1" of the configure process: after initial --flags +# handling but before sqlite-configure's $configScript argument is +# run. $buildMode must be the mode which was passed to +# [sqlite-configure]. +proc sqlite-configure-phase1 {buildMode} { + define PACKAGE_NAME sqlite + define PACKAGE_URL {https://sqlite.org} + define PACKAGE_BUGREPORT [get-define PACKAGE_URL]/forum + define PACKAGE_STRING "[get-define PACKAGE_NAME] [get-define PACKAGE_VERSION]" + proj-xfer-options-aliases { + # Carry values from hidden --flag aliases over to their canonical + # flag forms. This list must include only options which are common + # to all build modes supported by [sqlite-configure]. + with-readline-inc => with-readline-cflags + with-readline-lib => with-readline-ldflags + with-debug => debug + } + set ::sqliteConfig(msg-debug-enabled) [proj-val-truthy [get-env msg-debug 0]] + proc-debug "msg-debug is enabled" + proj-setup-autoreconfig SQLITE_AUTORECONFIG + proj-file-extensions + if {".exe" eq [get-define TARGET_EXEEXT]} { + define SQLITE_OS_UNIX 0 + define SQLITE_OS_WIN 1 + } else { + define SQLITE_OS_UNIX 1 + define SQLITE_OS_WIN 0 + } + sqlite-setup-default-cflags + define HAVE_LFS 0 + if {[opt-bool largefile]} { + # + # Insofar as we can determine HAVE_LFS has no effect on the + # library. Perhaps it did back in the early 2000's. The + # --enable/disable-largefile flag is retained because it's + # harmless, but it doesn't do anything useful. It does have + # visible side-effects, though: the generated sqlite_cfg.h may (or + # may not) define HAVE_LFS. + cc-check-lfs + } + set srcdir $::autosetup(srcdir) + proj-dot-ins-append $srcdir/Makefile.in + if {[file exists $srcdir/sqlite3.pc.in]} { + proj-dot-ins-append $srcdir/sqlite3.pc.in + } + sqlite-handle-hpux; # must be relatively early so that other config tests can work +}; # sqlite-configure-phase1 + +######################################################################## +# Performs late-stage config steps common to all supported +# $::sqliteConfig(build-mode) values. +proc sqlite-configure-finalize {} { + sqlite-handle-rpath + sqlite-handle-soname + sqlite-handle-threadsafe + sqlite-handle-tempstore + sqlite-handle-load-extension + sqlite-handle-math + sqlite-handle-icu + if {[proj-opt-exists readline]} { + sqlite-handle-line-editing + } + if {[proj-opt-exists shared]} { + proj-define-for-opt shared ENABLE_LIB_SHARED "Build shared library?" + } + if {[proj-opt-exists static]} { + if {![proj-define-for-opt static ENABLE_LIB_STATIC "Build static library?"]} { + # This notice really only applies to the canonical build... + proj-indented-notice { + NOTICE: static lib build may be implicitly re-activated by + other components, e.g. some test apps. + } + } + } + sqlite-handle-env-quirks + sqlite-handle-common-feature-flags + sqlite-finalize-feature-flags + sqlite-process-dot-in-files; # do not [define] anything after this + sqlite-dump-defines +} + +######################################################################## +# Internal config-time debugging output routine. It generates no +# output unless msg-debug=1 is passed to the configure script. +proc msg-debug {msg} { + if {$::sqliteConfig(msg-debug-enabled)} { + puts stderr [proj-bold "** DEBUG: $msg"] + } +} +######################################################################## +# A [msg-debug] proxy which prepends the name of the current proc to +# the debug message. It is not legal to call this from the global +# scope. +proc proc-debug {msg} { + msg-debug "\[[proj-scope 1]\]: $msg" +} + +define OPT_FEATURE_FLAGS {} ; # -DSQLITE_OMIT/ENABLE flags. +define OPT_SHELL {} ; # Feature-related CFLAGS for the sqlite3 CLI app +######################################################################## +# Adds $args, if not empty, to OPT_FEATURE_FLAGS. If the first arg is +# -shell then it strips that arg and passes the remaining args the +# sqlite-add-shell-opt in addition to adding them to +# OPT_FEATURE_FLAGS. This is intended only for holding +# -DSQLITE_ENABLE/OMIT/... flags, but that is not enforced here. +proc sqlite-add-feature-flag {args} { + set shell "" + if {"-shell" eq [lindex $args 0]} { + set args [lassign $args shell] + } + if {"" ne $args} { + if {"" ne $shell} { + sqlite-add-shell-opt {*}$args + } + define-append OPT_FEATURE_FLAGS {*}$args + } +} + +######################################################################## +# Appends $args, if not empty, to OPT_SHELL. +proc sqlite-add-shell-opt {args} { + if {"" ne $args} { + define-append OPT_SHELL {*}$args + } +} + +######################################################################## +# Check for log(3) in libm and die with an error if it is not +# found. $featureName should be the feature name which requires that +# function (it's used only in error messages). defines LDFLAGS_MATH to +# the required linker flags (which may be empty even if the math APIs +# are found, depending on the OS). +proc sqlite-affirm-have-math {featureName} { + if {"" eq [get-define LDFLAGS_MATH ""]} { + if {![msg-quiet proj-check-function-in-lib log m]} { + user-error "Missing math APIs for $featureName" + } + set lfl [get-define lib_log ""] + undefine lib_log + if {"" ne $lfl} { + user-notice "Forcing requirement of $lfl for $featureName" + } + define LDFLAGS_MATH $lfl + } +} + +######################################################################## +# Run checks for required binaries, like ld and ar. In the canonical +# build this must come before [sqlite-handle-wasi-sdk]. +proc sqlite-check-common-bins {} { + cc-check-tools ld ar ; # must come before [sqlite-handle-wasi-sdk] + if {"" eq [proj-bin-define install]} { + proj-warn "Cannot find install binary, so 'make install' will not work." + define BIN_INSTALL false + } +} + +######################################################################## +# Run checks for system-level includes and libs which are common to +# both the canonical build and the "autoconf" bundle. +# +# For the canonical build this must come after +# [sqlite-handle-wasi-sdk], as that function may change the +# environment in ways which affect this. +proc sqlite-check-common-system-deps {} { + # Check for needed/wanted data types + cc-with {-includes stdint.h} \ + {cc-check-types int8_t int16_t int32_t int64_t intptr_t \ + uint8_t uint16_t uint32_t uint64_t uintptr_t} + + # Check for needed/wanted functions + cc-check-functions gmtime_r isnan localtime_r localtime_s \ + usleep utime pread pread64 pwrite pwrite64 + + apply {{} { + set ldrt "" + # Collapse funcs from librt into LDFLAGS_RT. + # Some systems (ex: SunOS) require -lrt in order to use nanosleep + foreach func {fdatasync nanosleep} { + if {[proj-check-function-in-lib $func rt]} { + set ldrt [get-define lib_${func} ""] + undefine lib_${func} + if {"" ne $ldrt} { + break + } + } + } + define LDFLAGS_RT $ldrt + }} + + # Check for needed/wanted headers + cc-check-includes \ + sys/types.h sys/stat.h dlfcn.h unistd.h \ + stdlib.h malloc.h memory.h \ + string.h strings.h \ + inttypes.h + + if {[cc-check-includes zlib.h] && [proj-check-function-in-lib deflate z]} { + # TODO? port over the more sophisticated zlib search from the fossil auto.def + define HAVE_ZLIB 1 + define LDFLAGS_ZLIB -lz + sqlite-add-shell-opt -DSQLITE_HAVE_ZLIB=1 + } else { + define HAVE_ZLIB 0 + define LDFLAGS_ZLIB "" + } +} + +######################################################################## +# Move -DSQLITE_OMIT... and -DSQLITE_ENABLE... flags from CFLAGS and +# CPPFLAGS to OPT_FEATURE_FLAGS and remove them from BUILD_CFLAGS. +proc sqlite-munge-cflags {} { + # Move CFLAGS and CPPFLAGS entries matching -DSQLITE_OMIT* and + # -DSQLITE_ENABLE* to OPT_FEATURE_FLAGS. This behavior is derived + # from the legacy build and was missing the 3.48.0 release (the + # initial Autosetup port). + # https://sqlite.org/forum/forumpost/9801e54665afd728 + # + # Handling of CPPFLAGS, as well as removing ENABLE/OMIT from + # CFLAGS/CPPFLAGS, was missing in the 3.49.0 release as well. + # + # If any configure flags for features are in conflict with + # CFLAGS/CPPFLAGS-specified feature flags, all bets are off. There + # are no guarantees about which one will take precedence. + foreach flagDef {CFLAGS CPPFLAGS} { + set tmp "" + foreach cf [get-define $flagDef ""] { + switch -glob -- $cf { + -DSQLITE_OMIT* - + -DSQLITE_ENABLE* { + sqlite-add-feature-flag $cf + } + default { + lappend tmp $cf + } + } + } + define $flagDef $tmp + } + + # Strip all SQLITE_ENABLE/OMIT flags from BUILD_CFLAGS, + # for compatibility with the legacy build. + set tmp "" + foreach cf [get-define BUILD_CFLAGS ""] { + switch -glob -- $cf { + -DSQLITE_OMIT* - + -DSQLITE_ENABLE* {} + default { + lappend tmp $cf + } + } + } + define BUILD_CFLAGS $tmp +} + +######################################################################### +# Set up the default CFLAGS and BUILD_CFLAGS values. +proc sqlite-setup-default-cflags {} { + ######################################################################## + # We differentiate between two C compilers: the one used for binaries + # which are to run on the build system (in autosetup it's called + # CC_FOR_BUILD and in Makefile.in it's $(B.cc)) and the one used for + # compiling binaries for the target system (CC a.k.a. $(T.cc)). + # Normally they're the same, but they will differ when + # cross-compiling. + # + # When cross-compiling we default to not using the -g flag, based on a + # /chat discussion prompted by + # https://sqlite.org/forum/forumpost/9a67df63eda9925c + set defaultCFlags {-O2} + if {!$::sqliteConfig(is-cross-compiling)} { + lappend defaultCFlags -g + } + define CFLAGS [proj-get-env CFLAGS $defaultCFlags] + # BUILD_CFLAGS is the CFLAGS for CC_FOR_BUILD. + define BUILD_CFLAGS [proj-get-env BUILD_CFLAGS {-g}] + sqlite-munge-cflags +} + +######################################################################## +# Handle various SQLITE_ENABLE/OMIT_... feature flags. +proc sqlite-handle-common-feature-flags {} { + msg-result "Feature flags..." + if {![opt-bool all]} { + # Special handling for --disable-all + foreach flag $::sqliteConfig(all-flag-enables) { + if {![proj-opt-was-provided $flag]} { + proj-opt-set $flag 0 + } + } + } + foreach {boolFlag featureFlag ifSetEvalThis} [proj-strip-hash-comments { + all {} { + # The 'all' option must be first in this list. This impl makes + # an effort to only apply flags which the user did not already + # apply, so that combinations like (--all --disable-geopoly) + # will indeed disable geopoly. There are corner cases where + # flags which depend on each other will behave in non-intuitive + # ways: + # + # --all --disable-rtree + # + # Will NOT disable geopoly, though geopoly depends on rtree. + # The --geopoly flag, though, will automatically re-enable + # --rtree, so --disable-rtree won't actually disable anything in + # that case. + foreach k $::sqliteConfig(all-flag-enables) { + if {![proj-opt-was-provided $k]} { + proj-opt-set $k 1 + } + } + } + fts3 -DSQLITE_ENABLE_FTS3 {sqlite-affirm-have-math fts3} + fts4 -DSQLITE_ENABLE_FTS4 {sqlite-affirm-have-math fts4} + fts5 -DSQLITE_ENABLE_FTS5 {sqlite-affirm-have-math fts5} + geopoly -DSQLITE_ENABLE_GEOPOLY {proj-opt-set rtree} + rtree -DSQLITE_ENABLE_RTREE {} + session {-DSQLITE_ENABLE_SESSION -DSQLITE_ENABLE_PREUPDATE_HOOK} {} + update-limit -DSQLITE_ENABLE_UPDATE_DELETE_LIMIT {} + memsys5 -DSQLITE_ENABLE_MEMSYS5 {} + memsys3 {} { + if {[opt-bool memsys5]} { + proj-warn "not enabling memsys3 because memsys5 is enabled." + expr 0 + } else { + sqlite-add-feature-flag -DSQLITE_ENABLE_MEMSYS3 + } + } + scanstatus -DSQLITE_ENABLE_STMT_SCANSTATUS {} + column-metadata -DSQLITE_ENABLE_COLUMN_METADATA {} + dbpage -DSQLITE_ENABLE_DBPAGE_VTAB {} + dbstat -DSQLITE_ENABLE_DBSTAT_VTAB {} + carray -DSQLITE_ENABLE_CARRAY {} + }] { + if {$boolFlag ni $::autosetup(options)} { + # Skip flags which are in the canonical build but not + # the autoconf bundle. + continue + } + proj-if-opt-truthy $boolFlag { + sqlite-add-feature-flag $featureFlag + if {0 != [eval $ifSetEvalThis] && "all" ne $boolFlag} { + msg-result " + $boolFlag" + } + } { + if {"all" ne $boolFlag} { + msg-result " - $boolFlag" + } + } + } + ######################################################################## + # Invert the above loop's logic for some SQLITE_OMIT_... cases. If + # config option $boolFlag is false, [sqlite-add-feature-flag + # $featureFlag], where $featureFlag is intended to be + # -DSQLITE_OMIT_... + foreach {boolFlag featureFlag} { + json -DSQLITE_OMIT_JSON + } { + if {[proj-opt-truthy $boolFlag]} { + msg-result " + $boolFlag" + } else { + sqlite-add-feature-flag $featureFlag + msg-result " - $boolFlag" + } + } +} + +######################################################################### +# Remove duplicates from the final feature flag sets and show them to +# the user. +proc sqlite-finalize-feature-flags {} { + set oFF [get-define OPT_FEATURE_FLAGS] + if {"" ne $oFF} { + define OPT_FEATURE_FLAGS [lsort -unique $oFF] + msg-result "Library feature flags: [get-define OPT_FEATURE_FLAGS]" + } + set oFF [get-define OPT_SHELL] + if {"" ne $oFF} { + define OPT_SHELL [lsort -unique $oFF] + msg-result "Shell options: [get-define OPT_SHELL]" + } + if {"" ne [set extraSrc [get-define AMALGAMATION_EXTRA_SRC ""]]} { + proj-assert {"canonical" eq $::sqliteConfig(build-mode)} + msg-result "Appending source files to amalgamation: $extraSrc" + } + if {[lsearch [get-define TARGET_DEBUG ""] -DSQLITE_DEBUG=1] > -1} { + msg-result "Note: this is a debug build, so performance will suffer." + } +} + +######################################################################## +# Checks for the --debug flag and [define]s TARGET_DEBUG based on +# that. TARGET_DEBUG is unused in the autoconf build but that is +# arguably a bug. +proc sqlite-handle-debug {} { + msg-checking "SQLITE_DEBUG build? " + proj-if-opt-truthy debug { + define TARGET_DEBUG {-g -DSQLITE_DEBUG=1 -O0 -Wall} + sqlite-add-feature-flag -DSQLITE_ENABLE_SELECTTRACE -DSQLITE_ENABLE_WHERETRACE + proj-opt-set memsys5 + msg-result yes + } { + define TARGET_DEBUG {-DNDEBUG} + msg-result no + } +} + +######################################################################## +# "soname" for libsqlite3.so. See discussion at: +# https://sqlite.org/src/forumpost/5a3b44f510df8ded +proc sqlite-handle-soname {} { + define LDFLAGS_LIBSQLITE3_SONAME "" + if {[proj-opt-was-provided soname]} { + set soname [join [opt-val soname] ""] + } else { + # Enabling soname breaks linking for the --dynlink-tools feature, + # and this project has no direct use for soname, so default to + # none. Package maintainers, on the other hand, like to have an + # soname. + set soname none + } + switch -exact -- $soname { + none - "" { return 0 } + legacy { set soname libsqlite3.so.0 } + default { + if {[string match libsqlite3.* $soname]} { + # use it as-is + } else { + # Assume it's a suffix + set soname "libsqlite3.so.${soname}" + } + } + } + proc-debug "soname=$soname" + if {[proj-check-soname $soname]} { + define LDFLAGS_LIBSQLITE3_SONAME [get-define LDFLAGS_SONAME_PREFIX]$soname + msg-result "Setting SONAME using: [get-define LDFLAGS_LIBSQLITE3_SONAME]" + } elseif {[proj-opt-was-provided soname]} { + # --soname was explicitly requested but not available, so fail fatally + proj-fatal "This environment does not support SONAME." + } else { + # --soname was not explicitly requested but not available, so just warn + msg-result "This environment does not support SONAME." + } +} + +######################################################################## +# If --enable-threadsafe is set, this adds -DSQLITE_THREADSAFE=1 to +# OPT_FEATURE_FLAGS and sets LDFLAGS_PTHREAD to the linker flags +# needed for linking pthread (possibly an empty string). If +# --enable-threadsafe is not set, adds -DSQLITE_THREADSAFE=0 to +# OPT_FEATURE_FLAGS and sets LDFLAGS_PTHREAD to an empty string. +proc sqlite-handle-threadsafe {} { + msg-checking "Support threadsafe operation? " + define LDFLAGS_PTHREAD "" + set enable 0 + proj-if-opt-truthy threadsafe { + msg-result "Checking for libs..." + if {[proj-check-function-in-lib pthread_create pthread] + && [proj-check-function-in-lib pthread_mutexattr_init pthread]} { + set enable 1 + define LDFLAGS_PTHREAD [get-define lib_pthread_create] + undefine lib_pthread_create + undefine lib_pthread_mutexattr_init + } elseif {[proj-opt-was-provided threadsafe]} { + user-error "Missing required pthread libraries. Use --disable-threadsafe to disable this check." + } else { + msg-result "pthread support not detected" + } + # Recall that LDFLAGS_PTHREAD might be empty even if pthreads if + # found because it's in -lc on some platforms. + } { + msg-result "Disabled using --disable-threadsafe" + } + sqlite-add-feature-flag -DSQLITE_THREADSAFE=${enable} + return $enable +} + +######################################################################## +# Handles the --with-tempstore flag. +# +# The test fixture likes to set SQLITE_TEMP_STORE on its own, so do +# not set that feature flag unless it was explicitly provided to the +# configure script. +proc sqlite-handle-tempstore {} { + if {[proj-opt-was-provided with-tempstore]} { + set ts [opt-val with-tempstore no] + set tsn 1 + msg-checking "Use an in-RAM database for temporary tables? " + switch -exact -- $ts { + never { set tsn 0 } + no { set tsn 1 } + yes { set tsn 2 } + always { set tsn 3 } + default { + user-error "Invalid --with-tempstore value '$ts'. Use one of: never, no, yes, always" + } + } + msg-result $ts + sqlite-add-feature-flag -DSQLITE_TEMP_STORE=$tsn + } +} + +######################################################################## +# Check for the Emscripten SDK for building the web-based wasm +# components. The core lib and tools do not require this but ext/wasm +# does. Most of the work is done via [proj-check-emsdk], then this +# function adds the following defines: +# +# - EMCC_WRAPPER = "" or top-srcdir/tool/emcc.sh +# - BIN_WASM_OPT = "" or path to wasm-opt +# - BIN_WASM_STRIP = "" or path to wasm-strip +# +# Noting that: +# +# 1) Not finding the SDK is not fatal at this level, nor is failure to +# find one of the related binaries. +# +# 2) wasm-strip is part of the wabt package: +# +# https://github.com/WebAssembly/wabt +# +# and this project requires it for production-mode builds but not dev +# builds. +# +proc sqlite-handle-emsdk {} { + define EMCC_WRAPPER "" + define BIN_WASM_STRIP "" + define BIN_WASM_OPT "" + set srcdir $::autosetup(srcdir) + if {$srcdir ne $::autosetup(builddir)} { + # The EMSDK pieces require writing to the original source tree + # even when doing an out-of-tree build. The ext/wasm pieces do not + # support an out-of-tree build so we treat that case as if EMSDK + # were not found. + msg-result "Out-of tree build: not checking for EMSDK." + return + } + set emccSh $srcdir/tool/emcc.sh + set extWasmConfig $srcdir/ext/wasm/config.make + if {![get-define HAVE_WASI_SDK] && [proj-check-emsdk]} { + define EMCC_WRAPPER $emccSh + set emsdkHome [get-define EMSDK_HOME ""] + proj-assert {"" ne $emsdkHome} + #define EMCC_WRAPPER ""; # just for testing + proj-bin-define wasm-strip + proj-bin-define bash; # ext/wasm/GNUmakefile requires bash + if {[file-isexec $emsdkHome/upstream/bin/wasm-opt]} { + define BIN_WASM_OPT $emsdkHome/upstream/bin/wasm-opt + } else { + # Maybe there's a copy in the path? + proj-bin-define wasm-opt BIN_WASM_OPT + } + proj-dot-ins-append $emccSh.in $emccSh { + catch {exec chmod u+x $dotInsOut} + } + proj-dot-ins-append $extWasmConfig.in $extWasmConfig + } else { + define EMCC_WRAPPER "" + file delete -force -- $emccSh $extWasmConfig + } +} + +######################################################################## +# Internal helper for [sqlite-check-line-editing]. Returns a list of +# potential locations under which readline.h might be found. +# +# On some environments this function may perform extra work to help +# sqlite-check-line-editing figure out how to find libreadline and +# friends. It will communicate those results via means other than the +# result value, e.g. by modifying configure --flags. +proc sqlite-get-readline-dir-list {} { + # Historical note: the dirs list, except for the inclusion of + # $prefix and some platform-specific dirs, originates from the + # legacy configure script. + set dirs [list [get-define prefix]] + switch -glob -- [get-define host] { + *-linux-android { + # Possibly termux + lappend dirs /data/data/com.termux/files/usr + } + *-mingw32 { + lappend dirs /mingw32 /mingw + } + *-mingw64 { + lappend dirs /mingw64 /mingw + } + *-haiku { + lappend dirs /boot/system/develop/headers + if {[opt-val with-readline-ldflags] in {auto ""}} { + # If the user did not supply their own --with-readline-ldflags + # value, hijack that flag to inject options which are known to + # work on Haiku OS installations. + if {"" ne [glob -nocomplain /boot/system/lib/libreadline*]} { + proj-opt-set with-readline-ldflags {-L/boot/system/lib -lreadline} + } + } + } + } + lappend dirs /usr /usr/local /usr/local/readline /usr/contrib + set rv {} + foreach d $dirs { + if {[file isdir $d]} {lappend rv $d} + } + #proc-debug "dirs=$rv" + return $rv +} + +######################################################################## +# sqlite-check-line-editing jumps through proverbial hoops to try to +# find a working line-editing library, setting: +# +# - HAVE_READLINE to 0 or 1 +# - HAVE_LINENOISE to 0, 1, or 2 +# - HAVE_EDITLINE to 0 or 1 +# +# Only one of ^^^ those will be set to non-0. +# +# - LDFLAGS_READLINE = linker flags or empty string +# +# - CFLAGS_READLINE = compilation flags for clients or empty string. +# +# Note that LDFLAGS_READLINE and CFLAGS_READLINE may refer to +# linenoise or editline, not necessarily libreadline. In some cases +# it will set HAVE_READLINE=1 when it's really using editline, for +# reasons described in this function's comments. +# +# Returns a string describing which line-editing approach to use, or +# "none" if no option is available. +# +# Order of checks: +# +# 1) --with-linenoise trumps all others and skips all of the +# complexities involved with the remaining options. +# +# 2) --editline trumps --readline +# +# 3) --disable-readline trumps --readline +# +# 4) Default to automatic search for optional readline +# +# 5) Try to find readline or editline. If it's not found AND the +# corresponding --FEATURE flag was explicitly given then fail +# fatally, else fail non-fatally. +proc sqlite-check-line-editing {} { + msg-result "Checking for line-editing capability..." + define HAVE_READLINE 0 + define HAVE_LINENOISE 0 + define HAVE_EDITLINE 0 + define LDFLAGS_READLINE "" + define CFLAGS_READLINE "" + set failIfNotFound 0 ; # Gets set to 1 for explicit --FEATURE requests + # so that we know whether to fail fatally or not + # if the library is not found. + set libsForReadline {readline edit} ; # -l names to check for readline(). + # The libedit check changes this. + set editLibName "readline" ; # "readline" or "editline" + set editLibDef "HAVE_READLINE" ; # "HAVE_READLINE" or "HAVE_EDITLINE" + set dirLn [opt-val with-linenoise] + if {"" ne $dirLn} { + # Use linenoise from a copy of its sources (not a library)... + if {![file isdir $dirLn]} { + proj-fatal "--with-linenoise value is not a directory" + } + set lnH $dirLn/linenoise.h + if {![file exists $lnH] } { + proj-fatal "Cannot find linenoise.h in $dirLn" + } + set lnC "" + set lnCOpts {linenoise-ship.c linenoise.c} + foreach f $lnCOpts { + if {[file exists $dirLn/$f]} { + set lnC $dirLn/$f + break + } + } + if {"" eq $lnC} { + proj-fatal "Cannot find any of $lnCOpts in $dirLn" + } + set flavor "" + set lnVal [proj-which-linenoise $lnH] + switch -- $lnVal { + 1 { set flavor "antirez" } + 2 { set flavor "msteveb" } + default { + proj-fatal "Cannot determine the flavor of linenoise from $lnH" + } + } + define CFLAGS_READLINE "-I$dirLn $lnC" + define HAVE_LINENOISE $lnVal + sqlite-add-shell-opt -DHAVE_LINENOISE=$lnVal + if {$::sqliteConfig(use-jim-for-codegen) && 2 == $lnVal} { + define-append CFLAGS_JIMSH -DUSE_LINENOISE [get-define CFLAGS_READLINE] + user-notice "Adding linenoise support to jimsh." + } + return "linenoise ($flavor)" + } elseif {[opt-bool editline]} { + # libedit mimics libreadline and on some systems does not have its + # own header installed (instead, that of libreadline is used). + # + # shell.c historically expects HAVE_EDITLINE to be set for + # libedit, but it then expects to see , which + # some system's don't actually have despite having libedit. If we + # end up finding below, we will use + # -DHAVE_EDITLINE=1, else we will use -DHAVE_READLINE=1. In either + # case, we will link against libedit. + set failIfNotFound 1 + set libsForReadline {edit} + set editLibName editline + } elseif {![opt-bool readline]} { + msg-result "Readline support explicitly disabled with --disable-readline" + return "none" + } elseif {[proj-opt-was-provided readline]} { + # If an explicit --[enable-]readline was used, fail if it's not + # found, else treat the feature as optional. + set failIfNotFound 1 + } + + # Transform with-readline-header=X to with-readline-cflags=-I... + set v [opt-val with-readline-header] + proj-opt-set with-readline-header "" + if {"" ne $v} { + if {"auto" eq $v} { + proj-opt-set with-readline-cflags auto + } else { + set v [file dirname $v] + if {[string match */readline $v]} { + # Special case: if the path includes .../readline/readline.h, + # set the -I to one dir up from that because our sources + # #include or . + set v [file dirname $v] + } + proj-opt-set with-readline-cflags "-I$v" + } + } + + # Look for readline.h + set rlInc [opt-val with-readline-cflags auto] + if {"auto" eq $rlInc} { + set rlInc "" + if {$::sqliteConfig(is-cross-compiling)} { + # ^^^ this check is derived from the legacy configure script. + proj-warn "Skipping check for readline.h because we're cross-compiling." + } else { + set dirs [sqlite-get-readline-dir-list] + set subdirs [list \ + include/$editLibName \ + readline] + if {"editline" eq $editLibName} { + lappend subdirs include/readline + # ^^^ editline, on some systems, does not have its own header, + # and uses libreadline's header. + } + lappend subdirs include + set rlInc [proj-search-for-header-dir readline.h \ + -dirs $dirs -subdirs $subdirs] + #proc-debug "rlInc=$rlInc" + if {"" ne $rlInc} { + if {[string match */readline $rlInc]} { + set rlInc [file dirname $rlInc]; # CLI shell: #include + } elseif {[string match */editline $rlInc]} { + set editLibDef HAVE_EDITLINE + set rlInc [file dirname $rlInc]; # CLI shell: #include + } + set rlInc "-I${rlInc}" + } + } + } elseif {"" ne $rlInc && ![string match *-I* $rlInc]} { + proj-fatal "Argument to --with-readline-cflags is intended to be CFLAGS and contain -I..." + } + + # If readline.h was found/specified, look for lib(readline|edit)... + # + # This is not quite straightforward because both libreadline and + # libedit typically require some other library which (according to + # legacy autotools-generated tests) provides tgetent(3). On some + # systems that's built into libreadline/edit, on some (most?) its in + # lib[n]curses, and on some it's in libtermcap. + set rlLib "" + if {"" ne $rlInc} { + set rlLib [opt-val with-readline-ldflags] + #proc-debug "rlLib=$rlLib" + if {$rlLib in {auto ""}} { + set rlLib "" ; # make sure it's not "auto", as we may append to it below + set libTerm ""; # lib with tgetent(3) + if {[proj-check-function-in-lib tgetent [list $editLibName ncurses curses termcap]]} { + # ^^^ that libs list comes from the legacy configure script ^^^ + set libTerm [get-define lib_tgetent] + undefine lib_tgetent + } + if {$editLibName eq $libTerm} { + # tgetent(3) was found in the editing library + set rlLib $libTerm + } elseif {[proj-check-function-in-lib readline $libsForReadline $libTerm]} { + # tgetent(3) was found in an external lib + set rlLib [get-define lib_readline] + lappend rlLib $libTerm + undefine lib_readline + } + } + } + + # If we found a library, configure the build to use it... + if {"" ne $rlLib} { + if {"editline" eq $editLibName && "HAVE_READLINE" eq $editLibDef} { + # Alert the user that, despite outward appearances, we won't be + # linking to the GPL'd libreadline. Presumably that distinction is + # significant for those using --editline. + proj-indented-notice { + NOTE: the local libedit uses so we + will compile with -DHAVE_READLINE=1 but will link with + libedit. + } + } + set rlLib [join $rlLib] + set rlInc [join $rlInc] + define LDFLAGS_READLINE $rlLib + define CFLAGS_READLINE $rlInc + proj-assert {$editLibDef in {HAVE_READLINE HAVE_EDITLINE}} + proj-assert {$editLibName in {readline editline}} + sqlite-add-shell-opt -D${editLibDef}=1 + msg-result "Using $editLibName flags: $rlInc $rlLib" + # Check whether rl_completion_matches() has a signature we can use + # and disable that sub-feature if it doesn't. + if {![cctest -cflags "$rlInc -D${editLibDef}" -libs $rlLib -nooutput 1 \ + -source { + #include + #ifdef HAVE_EDITLINE + #include + #else + #include + #endif + static char * rcg(const char *z, int i){(void)z; (void)i; return 0;} + int main(void) { + char ** x = rl_completion_matches("one", rcg); + (void)x; + return 0; + } + }]} { + proj-warn "readline-style completion disabled due to rl_completion_matches() signature mismatch" + sqlite-add-shell-opt -DSQLITE_OMIT_READLINE_COMPLETION + } + return $editLibName + } + + if {$failIfNotFound} { + proj-fatal "Explicit --$editLibName failed to find a matching library." + } + return "none" +}; # sqlite-check-line-editing + +######################################################################## +# Runs sqlite-check-line-editing and adds a message around it. In the +# canonical build this must not be called before +# sqlite-determine-codegen-tcl for reasons now lost to history (and +# might not still be applicable). +proc sqlite-handle-line-editing {} { + msg-result "Line-editing support for the sqlite3 shell: [sqlite-check-line-editing]" +} + + +######################################################################## +# ICU - International Components for Unicode +# +# Handles these flags: +# +# --with-icu-ldflags=LDFLAGS +# --with-icu-cflags=CFLAGS +# --with-icu-config[=auto | pkg-config | /path/to/icu-config] +# --enable-icu-collations +# +# --with-icu-config values: +# +# - auto: use the first one of (pkg-config, icu-config) found on the +# system. +# - pkg-config: use only pkg-config to determine flags +# - /path/to/icu-config: use that to determine flags +# +# If --with-icu-config is used and neither pkg-config nor icu-config +# are found, fail fatally. +# +# If both --with-icu-ldflags and --with-icu-config are provided, they +# are cumulative. If neither are provided, icu-collations is not +# honored and a warning is emitted if it is provided. +# +# Design note: though we could automatically enable ICU if the +# icu-config binary or (pkg-config icu-io) are found, we specifically +# do not. ICU is always an opt-in feature. +proc sqlite-handle-icu {} { + define LDFLAGS_ICU [join [opt-val with-icu-ldflags ""]] + define CFLAGS_ICU [join [opt-val with-icu-cflags ""]] + if {[proj-opt-was-provided with-icu-config]} { + msg-result "Checking for ICU support..." + set icuConfigBin [opt-val with-icu-config] + set tryIcuConfigBin 1; # set to 0 if we end up using pkg-config + if {$icuConfigBin in {auto pkg-config}} { + if {[pkg-config-init 0] && [pkg-config icu-io]} { + # Maintenance reminder: historical docs say to use both of + # (icu-io, icu-uc). icu-uc lacks a required lib and icu-io has + # all of them on tested OSes. + set tryIcuConfigBin 0 + define LDFLAGS_ICU [get-define PKG_ICU_IO_LDFLAGS] + define-append LDFLAGS_ICU [get-define PKG_ICU_IO_LIBS] + define CFLAGS_ICU [get-define PKG_ICU_IO_CFLAGS] + } elseif {"pkg-config" eq $icuConfigBin} { + proj-fatal "pkg-config cannot find package icu-io" + } else { + proj-assert {"auto" eq $icuConfigBin} + } + } + if {$tryIcuConfigBin} { + if {"auto" eq $icuConfigBin} { + set icuConfigBin [proj-first-bin-of \ + /usr/local/bin/icu-config \ + /usr/bin/icu-config] + if {"" eq $icuConfigBin} { + proj-indented-notice -error { + --with-icu-config=auto cannot find (pkg-config icu-io) or icu-config binary. + On Ubuntu-like systems try: + --with-icu-ldflags='-licui18n -licuuc -licudata' + } + } + } + if {[file-isexec $icuConfigBin]} { + set x [exec $icuConfigBin --ldflags] + if {"" eq $x} { + proj-indented-notice -error \ + [subst { + $icuConfigBin --ldflags returned no data. + On Ubuntu-like systems try: + --with-icu-ldflags='-licui18n -licuuc -licudata' + }] + } + define-append LDFLAGS_ICU $x + set x [exec $icuConfigBin --cppflags] + define-append CFLAGS_ICU $x + } else { + proj-fatal "--with-icu-config=$icuConfigBin does not refer to an executable" + } + } + } + set ldflags [define LDFLAGS_ICU [string trim [get-define LDFLAGS_ICU]]] + set cflags [define CFLAGS_ICU [string trim [get-define CFLAGS_ICU]]] + if {"" ne $ldflags} { + sqlite-add-feature-flag -shell -DSQLITE_ENABLE_ICU + msg-result "Enabling ICU support with flags: $ldflags $cflags" + if {[opt-bool icu-collations]} { + msg-result "Enabling ICU collations." + sqlite-add-feature-flag -shell -DSQLITE_ENABLE_ICU_COLLATIONS + # Recall that shell.c builds with sqlite3.c except in the case + # of --disable-static-shell, a combination we do not + # specifically attempt to account for. + } + } elseif {[opt-bool icu-collations]} { + proj-warn "ignoring --enable-icu-collations because neither --with-icu-ldflags nor --with-icu-config provided any linker flags" + } else { + msg-result "ICU support is disabled." + } +}; # sqlite-handle-icu + + +######################################################################## +# Handles the --enable-load-extension flag. Returns 1 if the support +# is enabled, else 0. If support for that feature is not found, a +# fatal error is triggered if --enable-load-extension is explicitly +# provided, else a loud warning is instead emitted. If +# --disable-load-extension is used, no check is performed. +# +# Makes the following environment changes: +# +# - defines LDFLAGS_DLOPEN to any linker flags needed for this +# feature. It may legally be empty on (A) some systems where +# dlopen() is in libc and (B) certain Unix-esque Windows +# environments which identify as Windows for SQLite's purposes so +# use LoadLibrary(). +# +# - If the feature is not available, adds +# -DSQLITE_OMIT_LOAD_EXTENSION=1 to the feature flags list. +proc sqlite-handle-load-extension {} { + define LDFLAGS_DLOPEN "" + set found 0 + set suffix "" + proj-if-opt-truthy load-extension { + switch -glob -- [get-define host] { + *-*-mingw* - *windows* { + incr found + set suffix "Using LoadLibrary()" + } + default { + set found [proj-check-function-in-lib dlopen dl] + if {$found} { + set suffix [define LDFLAGS_DLOPEN [get-define lib_dlopen]] + undefine lib_dlopen + } else { + if {[proj-opt-was-provided load-extension]} { + # Explicit --enable-load-extension: fail if not found + proj-indented-notice -error { + --enable-load-extension was provided but dlopen() + not found. Use --disable-load-extension to bypass this + check. + } + } else { + # It was implicitly enabled: warn if not found + proj-indented-notice { + WARNING: dlopen() not found, so loadable module support will + be disabled. Use --disable-load-extension to bypass this + check. + } + } + } + } + } + } + if {$found} { + msg-result "Loadable extension support enabled. $suffix" + } else { + msg-result "Disabling loadable extension support. Use --enable-load-extension to enable them." + sqlite-add-feature-flag -DSQLITE_OMIT_LOAD_EXTENSION=1 + } + return $found +} + +######################################################################## +# Handles the --enable-math flag. +proc sqlite-handle-math {} { + proj-if-opt-truthy math { + if {![proj-check-function-in-lib ceil m]} { + user-error "Cannot find libm functions. Use --disable-math to bypass this." + } + define LDFLAGS_MATH [get-define lib_ceil] + undefine lib_ceil + sqlite-add-feature-flag -DSQLITE_ENABLE_MATH_FUNCTIONS -DSQLITE_ENABLE_PERCENTILE + msg-result "Enabling math SQL functions" + } { + define LDFLAGS_MATH "" + msg-result "Disabling math SQL functions" + } +} + +######################################################################## +# If this OS looks like a Mac, checks for the Mac-specific +# -current_version and -compatibility_version linker flags. Defines +# LDFLAGS_MAC_CVERSION to an empty string and returns 0 if they're not +# supported, else defines that to the linker flags and returns 1. +# +# We don't check this on non-Macs because this whole thing is a +# libtool compatibility kludge to account for a version stamp which +# libtool applied only on Mac platforms. +# +# Based on https://sqlite.org/forum/forumpost/9dfd5b8fd525a5d7. +proc sqlite-handle-mac-cversion {} { + define LDFLAGS_MAC_CVERSION "" + set rc 0 + if {[proj-looks-like-mac]} { + cc-with {-link 1} { + # These version numbers are historical libtool-defined values, not + # library-defined ones + if {[cc-check-flags "-Wl,-current_version,9.6.0"] + && [cc-check-flags "-Wl,-compatibility_version,9.0.0"]} { + define LDFLAGS_MAC_CVERSION "-Wl,-compatibility_version,9.0.0 -Wl,-current_version,9.6.0" + set rc 1 + } elseif {[cc-check-flags "-compatibility_version 9.0.0"] + && [cc-check-flags "-current_version 9.6.0"]} { + define LDFLAGS_MAC_CVERSION "-compatibility_version 9.0.0 -current_version 9.6.0" + set rc 1 + } + } + } + return $rc +} + +######################################################################## +# If this is a Mac platform, check for support for +# -Wl,-install_name,... and, if it's available, define +# LDFLAGS_MAC_INSTALL_NAME to a variant of that string which is +# intended to expand at make-time, else set LDFLAGS_MAC_INSTALL_NAME +# to an empty string. +# +# https://sqlite.org/forum/forumpost/5651662b8875ec0a +proc sqlite-handle-mac-install-name {} { + define LDFLAGS_MAC_INSTALL_NAME ""; # {-Wl,-install_name,"$(install-dir.lib)/$(libsqlite3.DLL)"} + set rc 0 + if {[proj-looks-like-mac]} { + cc-with {-link 1} { + if {[cc-check-flags "-Wl,-install_name,/usr/local/lib/libsqlite3.dylib"]} { + define LDFLAGS_MAC_INSTALL_NAME {-Wl,-install_name,"$(install-dir.lib)/$(libsqlite3.DLL)"} + set rc 1 + } + } + } + return $rc +} + +# +# Checks specific to HP-UX. +# +proc sqlite-handle-hpux {} { + switch -glob -- [get-define host] { + *hpux* { + if {[cc-check-flags "-Ae"]} { + define-append CFLAGS -Ae + } + } + } +} + +######################################################################## +# Handles the --dll-basename configure flag. [define]'s +# SQLITE_DLL_BASENAME to the DLL's preferred base name (minus +# extension). If --dll-basename is not provided (or programmatically +# set - see [sqlite-handle-env-quirks]) then this is always +# "libsqlite3", otherwise it may use a different value based on the +# value of [get-define host]. +proc sqlite-handle-dll-basename {} { + if {[proj-opt-was-provided dll-basename]} { + set dn [join [opt-val dll-basename] ""] + if {$dn in {none default}} { set dn libsqlite3 } + } else { + set dn libsqlite3 + } + if {$dn in {auto ""}} { + switch -glob -- [get-define host] { + *-*-cygwin { set dn cygsqlite3-0 } + *-*-ming* { set dn libsqlite3-0 } + *-*-msys { set dn msys-sqlite3-0 } + default { set dn libsqlite3 } + } + } + define SQLITE_DLL_BASENAME $dn +} + +######################################################################## +# [define]s LDFLAGS_OUT_IMPLIB to either an empty string or to a +# -Wl,... flag for the platform-specific --out-implib flag, which is +# used for building an "import library .dll.a" file on some platforms +# (e.g. msys2, mingw). SQLITE_OUT_IMPLIB is defined to the name of the +# import lib or an empty string. Returns 1 if supported, else 0. +# +# The name of the import library is [define]d in SQLITE_OUT_IMPLIB. +# +# If the configure flag --out-implib is not used (or programmatically +# set) then this simply sets the above-listed defines to empty strings +# (but see [sqlite-handle-env-quirks]). If that flag is used but the +# capability is not available, a fatal error is triggered. +# +# This feature is specifically opt-in because it's supported on far +# more platforms than actually need it and enabling it causes creation +# of libsqlite3.so.a files which are unnecessary in most environments. +# +# Added in response to: https://sqlite.org/forum/forumpost/0c7fc097b2 +# +# Platform notes: +# +# - cygwin sqlite packages historically install no .dll.a file. +# +# - msys2 and mingw sqlite packages historically install +# /usr/lib/libsqlite3.dll.a despite the DLL being in +# /usr/bin. +proc sqlite-handle-out-implib {} { + define LDFLAGS_OUT_IMPLIB "" + define SQLITE_OUT_IMPLIB "" + set rc 0 + if {[proj-opt-was-provided out-implib]} { + set olBaseName [join [opt-val out-implib] ""] + if {$olBaseName in {auto ""}} { + set olBaseName "libsqlite3" ;# [get-define SQLITE_DLL_BASENAME] + # Based on discussions with mingw/msys users, the import lib + # should always be called libsqlite3.dll.a even on platforms + # which rename libsqlite3.dll to something else. + } + if {$olBaseName ne "none"} { + cc-with {-link 1} { + set dll "${olBaseName}[get-define TARGET_DLLEXT]" + set flags [proj-cc-check-Wl-flag --out-implib ${dll}.a] + if {"" ne $flags} { + define LDFLAGS_OUT_IMPLIB $flags + define SQLITE_OUT_IMPLIB ${dll}.a + set rc 1 + } + } + if {!$rc} { + user-error "--out-implib is not supported on this platform" + } + } + } + return $rc +} + +######################################################################## +# If the given platform identifier (defaulting to [get-define host]) +# appears to be one of the Unix-on-Windows environments, returns a +# brief symbolic name for that environment, else returns an empty +# string. +# +# It does not distinguish between msys and msys2, returning msys for +# both. The build does not, as of this writing, specifically support +# msys v1. Similarly, this function returns "mingw" for both "mingw32" +# and "mingw64". +proc sqlite-env-is-unix-on-windows {{envTuple ""}} { + if {"" eq $envTuple} { + set envTuple [get-define host] + } + set name "" + switch -glob -- $envTuple { + *-*-cygwin { set name cygwin } + *-*-ming* { set name mingw } + *-*-msys { set name msys } + } + return $name +} + +######################################################################## +# Performs various tweaks to the build which are only relevant on +# certain platforms, e.g. Mac and "Unix on Windows" platforms (msys2, +# cygwin, ...). +# +# 1) DLL installation: +# +# [define]s SQLITE_DLL_INSTALL_RULES to a symbolic name suffix for a +# set of "make install" rules to use for installation of the DLL +# deliverable. The makefile is tasked with providing rules named +# install-dll-NAME which runs the installation for that set, as well +# as providing a rule named install-dll which resolves to +# install-dll-NAME (perhaps indirectly, depending on whether the DLL +# is (de)activated). +# +# The default value is "unix-generic". +# +# 2) --out-implib: +# +# On platforms where an "import library" is conventionally used but +# --out-implib was not explicitly used, automatically add that flag. +# This conventionally applies only to the "Unix on Windows" +# environments like msys and cygwin. +# +# 3) --dll-basename: +# +# On the same platforms addressed by --out-implib, if --dll-basename +# is not explicitly specified, --dll-basename=auto is implied. +proc sqlite-handle-env-quirks {} { + set instName unix-generic; # name of installation rules set + set autoDll 0; # true if --out-implib/--dll-basename should be implied + set host [get-define host] + switch -glob -- $host { + *apple* - + *darwin* { set instName darwin } + default { + set x [sqlite-env-is-unix-on-windows $host] + if {"" ne $x} { + set instName $x + set autoDll 1 + } + } + } + define SQLITE_DLL_INSTALL_RULES $instName + if {$autoDll} { + if {![proj-opt-was-provided out-implib]} { + # Imply --out-implib=auto + proj-indented-notice [subst -nocommands -nobackslashes { + NOTICE: auto-enabling --out-implib for environment [$host]. + Use --out-implib=none to disable this special case + or --out-implib=auto to squelch this notice. + }] + proj-opt-set out-implib auto + } + if {![proj-opt-was-provided dll-basename]} { + # Imply --dll-basename=auto + proj-indented-notice [subst -nocommands -nobackslashes { + NOTICE: auto-enabling --dll-basename for environment [$host]. + Use --dll-basename=default to disable this special case + or --dll-basename=auto to squelch this notice. + }] + proj-opt-set dll-basename auto + } + } + sqlite-handle-dll-basename + sqlite-handle-out-implib + sqlite-handle-mac-cversion + sqlite-handle-mac-install-name + if {[llength [info proc sqlite-custom-handle-flags]] > 0} { + # sqlite-custom-handle-flags is assumed to be imported via a + # client-specific import: autosetup/sqlite-custom.tcl. + sqlite-custom-handle-flags + } +} + +######################################################################## +# Perform some late-stage work and generate the configure-process +# output file(s). +proc sqlite-process-dot-in-files {} { + ######################################################################## + # "Re-export" the autoconf-conventional --XYZdir flags into something + # which is more easily overridable from a make invocation. See the docs + # for [proj-remap-autoconf-dir-vars] for the explanation of why. + # + # We do this late in the config process, immediately before we export + # the Makefile and other generated files, so that configure tests + # which make make use of the autotools-conventional flags + # (e.g. [proj-check-rpath]) may do so before we "mangle" them here. + proj-remap-autoconf-dir-vars + + proj-dot-ins-process -validate + make-config-header sqlite_cfg.h \ + -bare {SIZEOF_* HAVE_DECL_*} \ + -none {HAVE_CFLAG_* LDFLAGS_* SH_* SQLITE_AUTORECONFIG + TARGET_* USE_GCOV TCL_*} \ + -auto {HAVE_* PACKAGE_*} \ + -none * + proj-touch sqlite_cfg.h ; # help avoid frequent unnecessary @SQLITE_AUTORECONFIG@ +} + +######################################################################## +# Handle --with-wasi-sdk[=DIR] +# +# This must be run relatively early on because it may change the +# toolchain and disable a number of config options. However, in the +# canonical build this must come after [sqlite-check-common-bins]. +proc sqlite-handle-wasi-sdk {} { + set wasiSdkDir [opt-val with-wasi-sdk] ; # ??? [lindex [opt-val with-wasi-sdk] end] + define HAVE_WASI_SDK 0 + if {$wasiSdkDir eq ""} { + return 0 + } elseif {$::sqliteConfig(is-cross-compiling)} { + proj-fatal "Cannot combine --with-wasi-sdk with cross-compilation" + } + msg-result "Checking WASI SDK directory \[$wasiSdkDir]... " + proj-affirm-files-exist -v {*}[prefix "$wasiSdkDir/bin/" {clang wasm-ld ar}] + define HAVE_WASI_SDK 1 + define WASI_SDK_DIR $wasiSdkDir + # Disable numerous options which we know either can't work or are + # not useful in this build... + msg-result "Using wasi-sdk clang. Disabling CLI shell and modifying config flags:" + # Boolean (--enable-/--disable-) flags which must be switched off: + foreach opt { + dynlink-tools + editline + gcov + icu-collations + load-extension + readline + shared + tcl + threadsafe + } { + if {[proj-opt-exists $opt] && [opt-bool $opt]} { + # -^^^^ not all builds define all of these flags + msg-result " --disable-$opt" + proj-opt-set $opt 0 + } + } + # Non-boolean flags which need to be cleared: + foreach opt { + with-emsdk + with-icu-config + with-icu-ldflags + with-icu-cflags + with-linenoise + with-tcl + } { + if {[proj-opt-was-provided $opt]} { + msg-result " removing --$opt" + proj-opt-set $opt "" + } + } + # Remember that we now have a discrepancy between + # $::sqliteConfig(is-cross-compiling) and [proj-is-cross-compiling]. + set ::sqliteConfig(is-cross-compiling) 1 + + # + # Changing --host and --target have no effect here except to + # possibly cause confusion. Autosetup has finished processing them + # by this point. + # + # host_alias=wasm32-wasi + # target=wasm32-wasi + # + # Merely changing CC, LD, and AR to the wasi-sdk's is enough to get + # sqlite3.o building in WASM format. + # + define CC "${wasiSdkDir}/bin/clang" + define LD "${wasiSdkDir}/bin/wasm-ld" + define AR "${wasiSdkDir}/bin/ar" + #define STRIP "${wasiSdkDir}/bin/strip" + return 1 +}; # sqlite-handle-wasi-sdk + +######################################################################## +# TCL... +# +# sqlite-check-tcl performs most of the --with-tcl and --with-tclsh +# handling. Some related bits and pieces are performed before and +# after that function is called. +# +# Important [define]'d vars: +# +# - HAVE_TCL indicates whether we have a tclsh suitable for building +# the TCL SQLite extension and, by extension, the testing +# infrastructure. This must only be 1 for environments where +# tclConfig.sh can be found. +# +# - TCLSH_CMD is the path to the canonical tclsh or "". It never +# refers to jimtcl. +# +# - TCL_CONFIG_SH is the path to tclConfig.sh or "". +# +# - TCLLIBDIR is the dir to which libtclsqlite3 gets installed. +# +# - BTCLSH = the path to the tcl interpreter used for in-tree code +# generation. It may be jimtcl or the canonical tclsh but may not +# be empty - this tree requires TCL to generated numerous +# components. +# +# If --tcl or --with-tcl are provided but no TCL is found, this +# function fails fatally. If they are not explicitly provided then +# failure to find TCL is not fatal but a loud warning will be emitted. +# +proc sqlite-check-tcl {} { + define TCLSH_CMD false ; # Significant is that it exits with non-0 + define HAVE_TCL 0 ; # Will be enabled via --tcl or a successful search + define TCLLIBDIR "" ; # Installation dir for TCL extension lib + define TCL_CONFIG_SH ""; # full path to tclConfig.sh + + # Clear out all vars which would harvest from tclConfig.sh so that + # the late-config validation of @VARS@ works even if --disable-tcl + # is used. + proj-tclConfig-sh-to-autosetup "" + + file delete -force ".tclenv.sh"; # ensure no stale state from previous configures. + if {![opt-bool tcl]} { + proj-indented-notice { + NOTE: TCL is disabled via --disable-tcl. This means that none + of the TCL-based components will be built, including tests + and sqlite3_analyzer. + } + return + } + # TODO: document the steps this is taking. + set srcdir $::autosetup(srcdir) + msg-result "Checking for a suitable tcl... " + proj-assert [proj-opt-truthy tcl] + set use_tcl 1 + set with_tclsh [opt-val with-tclsh] + set with_tcl [opt-val with-tcl] + if {"prefix" eq $with_tcl} { + set with_tcl [get-define prefix] + } + proc-debug "use_tcl ${use_tcl}" + proc-debug "with_tclsh=${with_tclsh}" + proc-debug "with_tcl=$with_tcl" + if {"" eq $with_tclsh && "" eq $with_tcl} { + # If neither --with-tclsh nor --with-tcl are provided, try to find + # a workable tclsh. + set with_tclsh [proj-first-bin-of tclsh9.1 tclsh9.0 tclsh8.6 tclsh] + proc-debug "with_tclsh=${with_tclsh}" + } + + set doConfigLookup 1 ; # set to 0 to test the tclConfig.sh-not-found cases + if {"" ne $with_tclsh} { + # --with-tclsh was provided or found above. Validate it and use it + # to trump any value passed via --with-tcl=DIR. + if {![file-isexec $with_tclsh]} { + proj-fatal "TCL shell $with_tclsh is not executable" + } else { + define TCLSH_CMD $with_tclsh + #msg-result "Using tclsh: $with_tclsh" + } + if {$doConfigLookup && + [catch {exec $with_tclsh $::autosetup(libdir)/find_tclconfig.tcl} result] == 0} { + set with_tcl $result + } + if {"" ne $with_tcl && [file isdir $with_tcl]} { + msg-result "$with_tclsh recommends the tclConfig.sh from $with_tcl" + } else { + proj-warn "$with_tclsh is unable to recommend a tclConfig.sh" + set use_tcl 0 + } + } + set cfg "" + set tclSubdirs {tcl9.1 tcl9.0 tcl8.6 lib} + while {$use_tcl} { + if {"" ne $with_tcl} { + # Ensure that we can find tclConfig.sh under ${with_tcl}/... + if {$doConfigLookup} { + if {[file readable "${with_tcl}/tclConfig.sh"]} { + set cfg "${with_tcl}/tclConfig.sh" + } else { + foreach i $tclSubdirs { + if {[file readable "${with_tcl}/$i/tclConfig.sh"]} { + set cfg "${with_tcl}/$i/tclConfig.sh" + break + } + } + } + } + if {"" eq $cfg} { + proj-fatal "No tclConfig.sh found under ${with_tcl}" + } + } else { + # If we have not yet found a tclConfig.sh file, look in $libdir + # which is set automatically by autosetup or via the --prefix + # command-line option. See + # https://sqlite.org/forum/forumpost/e04e693439a22457 + set libdir [get-define libdir] + if {[file readable "${libdir}/tclConfig.sh"]} { + set cfg "${libdir}/tclConfig.sh" + } else { + foreach i $tclSubdirs { + if {[file readable "${libdir}/$i/tclConfig.sh"]} { + set cfg "${libdir}/$i/tclConfig.sh" + break + } + } + } + if {![file readable $cfg]} { + break + } + } + msg-result "Using tclConfig.sh: $cfg" + break + } + define TCL_CONFIG_SH $cfg + # Export a subset of tclConfig.sh to the current TCL-space. If $cfg + # is an empty string, this emits empty-string entries for the + # various options we're interested in. + proj-tclConfig-sh-to-autosetup $cfg + + if {"" eq $with_tclsh && $cfg ne ""} { + # We have tclConfig.sh but no tclsh. Attempt to locate a tclsh + # based on info from tclConfig.sh. + set tclExecPrefix [get-define TCL_EXEC_PREFIX] + proj-assert {"" ne $tclExecPrefix} + set tryThese [list \ + $tclExecPrefix/bin/tclsh[get-define TCL_VERSION] \ + $tclExecPrefix/bin/tclsh ] + foreach trySh $tryThese { + if {[file-isexec $trySh]} { + set with_tclsh $trySh + break + } + } + if {![file-isexec $with_tclsh]} { + proj-warn "Cannot find a usable tclsh (tried: $tryThese) + } + } + define TCLSH_CMD $with_tclsh + if {$use_tcl} { + # Set up the TCLLIBDIR + # + # 2024-10-28: calculation of TCLLIBDIR is now done via the shell + # in main.mk (search it for T.tcl.env.sh) so that + # static/hand-written makefiles which import main.mk do not have + # to define that before importing main.mk. Even so, we export + # TCLLIBDIR from here, which will cause the canonical makefile to + # use this one rather than to re-calculate it at make-time. + set tcllibdir [get-env TCLLIBDIR ""] + set sq3Ver [get-define PACKAGE_VERSION] + if {"" eq $tcllibdir} { + # Attempt to extract TCLLIBDIR from TCL's $auto_path + if {"" ne $with_tclsh && + [catch {exec echo "puts stdout \$auto_path" | "$with_tclsh"} result] == 0} { + foreach i $result { + if {[file isdir $i]} { + set tcllibdir $i/sqlite${sq3Ver} + break + } + } + } else { + proj-warn "Cannot determine TCLLIBDIR." + # The makefile will fail fatally in this case if a target is + # invoked which requires TCLLIBDIR. + } + } + #if {"" ne $tcllibdir} { msg-result "TCLLIBDIR = ${tcllibdir}"; } + define TCLLIBDIR $tcllibdir + }; # find TCLLIBDIR + + if {[file-isexec $with_tclsh]} { + msg-result "Using tclsh: $with_tclsh" + if {$cfg ne ""} { + define HAVE_TCL 1 + } else { + proj-warn "Found tclsh but no tclConfig.sh." + } + } + show-notices + # If TCL is not found: if it was explicitly requested then fail + # fatally, else just emit a warning. If we can find the APIs needed + # to generate a working JimTCL then that will suffice for build-time + # TCL purposes (see: proc sqlite-determine-codegen-tcl). + if {![get-define HAVE_TCL] && + ([proj-opt-was-provided tcl] || [proj-opt-was-provided with-tcl])} { + proj-fatal "TCL support was requested but no tclConfig.sh could be found." + } + if {"" eq $cfg} { + proj-assert {0 == [get-define HAVE_TCL]} + proj-indented-notice { + WARNING: Cannot find a usable tclConfig.sh file. Use + --with-tcl=DIR to specify a directory where tclConfig.sh can be + found. SQLite does not use TCL internally, but some optional + components require TCL, including tests and sqlite3_analyzer. + } + } +}; # sqlite-check-tcl + +######################################################################## +# sqlite-determine-codegen-tcl checks which TCL to use as a code +# generator. By default, prefer jimsh simply because we have it +# in-tree (it's part of autosetup) unless --with-tclsh=X is used, in +# which case prefer X. +# +# Returns the human-readable name of the TCL it selects. Fails fatally +# if it cannot detect a TCL appropriate for code generation. +# +# Defines: +# +# - BTCLSH = the TCL shell used for code generation. It may set this +# to an unexpanded makefile var name. +# +# - CFLAGS_JIMSH = any flags needed for buildng a BTCLSH-compatible +# jimsh. The defaults may be passed on to configure as +# CFLAGS_JIMSH=... +proc sqlite-determine-codegen-tcl {} { + msg-result "Checking for TCL to use for code generation... " + define CFLAGS_JIMSH [proj-get-env CFLAGS_JIMSH {-O1}] + set cgtcl [opt-val with-tclsh jimsh] + if {"jimsh" ne $cgtcl} { + # When --with-tclsh=X is used, use that for all TCL purposes, + # including in-tree code generation, per developer request. + define BTCLSH "\$(TCLSH_CMD)" + return $cgtcl + } + set flagsToRestore {CC CFLAGS AS_CFLAGS CPPFLAGS AS_CPPFLAGS LDFLAGS LINKFLAGS LIBS CROSS} + define-push $flagsToRestore { + # We have to swap CC to CC_FOR_BUILD for purposes of the various + # [cc-...] tests below. Recall that --with-wasi-sdk may have + # swapped out CC with one which is not appropriate for this block. + # Per consulation with autosetup's creator, doing this properly + # requires us to [define-push] the whole $flagsToRestore list + # (plus a few others which are not relevant in this tree). + # + # These will get set to their previous values at the end of this + # block. + foreach flag $flagsToRestore {define $flag ""} + define CC [get-define CC_FOR_BUILD] + # These headers are technically optional for JimTCL but necessary if + # we want to use it for code generation: + set sysh [cc-check-includes dirent.h sys/time.h] + # jimsh0.c hard-codes #define's for HAVE_DIRENT_H and + # HAVE_SYS_TIME_H on the platforms it supports, so we do not + # need to add -D... flags for those. We check for them here only + # so that we can avoid the situation that we later, at + # make-time, try to compile jimsh but it then fails due to + # missing headers (i.e. fail earlier rather than later). + if {$sysh && [cc-check-functions realpath]} { + define-append CFLAGS_JIMSH -DHAVE_REALPATH + define BTCLSH "\$(JIMSH)" + set ::sqliteConfig(use-jim-for-codegen) 1 + } elseif {$sysh && [cc-check-functions _fullpath]} { + # _fullpath() is a Windows API. It's not entirely clear + # whether we need to add {-DHAVE_SYS_TIME_H -DHAVE_DIRENT_H} + # to CFLAGS_JIMSH in this case. On MinGW32 we definitely do + # not want to because it already hard-codes them. On _MSC_VER + # builds it does not. + define-append CFLAGS_JIMSH -DHAVE__FULLPATH + define BTCLSH "\$(JIMSH)" + set ::sqliteConfig(use-jim-for-codegen) 1 + } elseif {[file-isexec [get-define TCLSH_CMD]]} { + set cgtcl [get-define TCLSH_CMD] + define BTCLSH "\$(TCLSH_CMD)" + } else { + # One last-ditch effort to find TCLSH_CMD: use info from + # tclConfig.sh to try to find a tclsh + if {"" eq [get-define TCLSH_CMD]} { + set tpre [get-define TCL_EXEC_PREFIX] + if {"" ne $tpre} { + set tv [get-define TCL_VERSION] + if {[file-isexec "${tpre}/bin/tclsh${tv}"]} { + define TCLSH_CMD "${tpre}/bin/tclsh${tv}" + } elseif {[file-isexec "${tpre}/bin/tclsh"]} { + define TCLSH_CMD "${tpre}/bin/tclsh" + } + } + } + set cgtcl [get-define TCLSH_CMD] + if {![file-isexec $cgtcl]} { + proj-fatal "Cannot find a tclsh to use for code generation." + } + define BTCLSH "\$(TCLSH_CMD)" + } + }; # /define-push $flagsToRestore + return $cgtcl +}; # sqlite-determine-codegen-tcl + +######################################################################## +# Runs sqlite-check-tcl and, if this is the canonical build, +# sqlite-determine-codegen-tcl. +proc sqlite-handle-tcl {} { + sqlite-check-tcl + if {"canonical" ne $::sqliteConfig(build-mode)} return + msg-result "TCL for code generation: [sqlite-determine-codegen-tcl]" + + # Determine the base name of the Tcl extension's DLL + # + if {[get-define HAVE_TCL]} { + if {[string match *-cygwin [get-define host]]} { + set libname cyg + } else { + set libname lib + } + if {[get-define TCL_MAJOR_VERSION] > 8} { + append libname tcl9 + } + append libname sqlite + } else { + set libname "" + } + define TCL_EXT_DLL_BASENAME $libname + # The extension is added in the makefile +} + +######################################################################## +# Handle the --enable/disable-rpath flag. +proc sqlite-handle-rpath {} { + # autosetup/cc-shared.tcl sets the rpath flag definition in + # [get-define SH_LINKRPATH], but it does so on a per-platform basis + # rather than as a compiler check. Though we should do a proper + # compiler check (as proj-check-rpath does), we may want to consider + # adopting its approach of clearing the rpath flags for environments + # for which sqlite-env-is-unix-on-windows returns a non-empty + # string. + + # https://sqlite.org/forum/forumpost/13cac3b56516f849 + if {[proj-opt-truthy rpath]} { + proj-check-rpath + } else { + msg-result "Disabling use of rpath." + define LDFLAGS_RPATH "" + } +} + +######################################################################## +# If the --dump-defines configure flag is provided then emit a list of +# all [define] values to config.defines.txt, else do nothing. +proc sqlite-dump-defines {} { + proj-if-opt-truthy dump-defines { + make-config-header $::sqliteConfig(dump-defines-txt) \ + -bare {SQLITE_OS* SQLITE_DEBUG USE_*} \ + -str {BIN_* CC LD AR LDFLAG* OPT_*} \ + -auto {*} + # achtung: ^^^^ whichever SQLITE_OS_foo flag which is set to 0 will + # get _undefined_ here unless it's part of the -bare set. + if {"" ne $::sqliteConfig(dump-defines-json)} { + msg-result "--dump-defines is creating $::sqliteConfig(dump-defines-json)" + ######################################################################## + # Dump config-defines.json... + # Demonstrate (mis?)handling of spaces in JSON-export array values: + # define-append OPT_FOO.list {"-DFOO=bar baz" -DBAR="baz barre"} + define OPT_FEATURE_FLAGS.list [get-define OPT_FEATURE_FLAGS] + define OPT_SHELL.list [get-define OPT_SHELL] + set dumpDefsOpt { + -bare {SIZEOF_* HAVE_DECL_*} + -none {HAVE_CFLAG_* LDFLAGS_* SH_* SQLITE_AUTORECONFIG TARGET_* USE_GCOV TCL_*} + -array {*.list} + -auto {OPT_* PACKAGE_* HAVE_*} + } +# if {$::sqliteConfig(dump-defines-json-include-lowercase)} { +# lappend dumpDefsOpt -none {lib_*} ; # remnants from proj-check-function-in-lib and friends +# lappend dumpDefsOpt -auto {[a-z]*} +# } + lappend dumpDefsOpt -none * + proj-dump-defs-json $::sqliteConfig(dump-defines-json) {*}$dumpDefsOpt + undefine OPT_FEATURE_FLAGS.list + undefine OPT_SHELL.list + } + } +} diff --git a/autosetup/system.tcl b/autosetup/system.tcl new file mode 100644 index 0000000000..05d378afdd --- /dev/null +++ b/autosetup/system.tcl @@ -0,0 +1,420 @@ +# Copyright (c) 2010 WorkWare Systems http://www.workware.net.au/ +# All rights reserved + +# @synopsis: +# +# This module supports common system interrogation and options +# such as '--host', '--build', '--prefix', and setting 'srcdir', 'builddir', and 'EXEEXT'. +# +# It also support the "feature" naming convention, where searching +# for a feature such as 'sys/type.h' defines 'HAVE_SYS_TYPES_H'. +# +# It defines the following variables, based on '--prefix' unless overridden by the user: +# +## datadir +## sysconfdir +## sharedstatedir +## localstatedir +## infodir +## mandir +## includedir +# +# If '--prefix' is not supplied, it defaults to '/usr/local' unless 'options-defaults { prefix ... }' is used *before* +# including the 'system' module. + +if {[is-defined defaultprefix]} { + user-notice "Note: defaultprefix is deprecated. Use options-defaults to set default options" + options-defaults [list prefix [get-define defaultprefix]] +} + +options { + host:host-alias => {a complete or partial cpu-vendor-opsys for the system where + the application will run (defaults to the same value as --build)} + build:build-alias => {a complete or partial cpu-vendor-opsys for the system + where the application will be built (defaults to the + result of running config.guess)} + prefix:dir=/usr/local => {the target directory for the build (default: '@default@')} + + # These (hidden) options are supported for autoconf/automake compatibility + exec-prefix: + bindir: + sbindir: + includedir: + mandir: + infodir: + libexecdir: + datadir: + libdir: + sysconfdir: + sharedstatedir: + localstatedir: + runstatedir: + maintainer-mode=0 + dependency-tracking=0 + silent-rules=0 + program-prefix: + program-suffix: + program-transform-name: + x-includes: + x-libraries: +} + +# @check-feature name { script } +# +# defines feature '$name' to the return value of '$script', +# which should be 1 if found or 0 if not found. +# +# e.g. the following will define 'HAVE_CONST' to 0 or 1. +# +## check-feature const { +## cctest -code {const int _x = 0;} +## } +proc check-feature {name code} { + msg-checking "Checking for $name..." + set r [uplevel 1 $code] + define-feature $name $r + if {$r} { + msg-result "ok" + } else { + msg-result "not found" + } + return $r +} + +# @have-feature name ?default=0? +# +# Returns the value of feature '$name' if defined, or '$default' if not. +# +# See 'feature-define-name' for how the "feature" name +# is translated into the "define" name. +# +proc have-feature {name {default 0}} { + get-define [feature-define-name $name] $default +} + +# @define-feature name ?value=1? +# +# Sets the feature 'define' to '$value'. +# +# See 'feature-define-name' for how the "feature" name +# is translated into the "define" name. +# +proc define-feature {name {value 1}} { + define [feature-define-name $name] $value +} + +# @feature-checked name +# +# Returns 1 if feature '$name' has been checked, whether true or not. +# +proc feature-checked {name} { + is-defined [feature-define-name $name] +} + +# @feature-define-name name ?prefix=HAVE_? +# +# Converts a "feature" name to the corresponding "define", +# e.g. 'sys/stat.h' becomes 'HAVE_SYS_STAT_H'. +# +# Converts '*' to 'P' and all non-alphanumeric to underscore. +# +proc feature-define-name {name {prefix HAVE_}} { + string toupper $prefix[regsub -all {[^a-zA-Z0-9]} [regsub -all {[*]} $name p] _] +} + +# @write-if-changed filename contents ?script? +# +# If '$filename' doesn't exist, or it's contents are different to '$contents', +# the file is written and '$script' is evaluated. +# +# Otherwise a "file is unchanged" message is displayed. +proc write-if-changed {file buf {script {}}} { + set old [readfile $file ""] + if {$old eq $buf && [file exists $file]} { + msg-result "$file is unchanged" + } else { + writefile $file $buf\n + uplevel 1 $script + } +} + + +# @include-file infile mapping +# +# The core of make-template, called recursively for each @include +# directive found within that template so that this proc's result +# is the fully-expanded template. +# +# The mapping parameter is how we expand @varname@ within the template. +# We do that inline within this step only for @include directives which +# can have variables in the filename arg. A separate substitution pass +# happens when this recursive function returns, expanding the rest of +# the variables. +# +proc include-file {infile mapping} { + # A stack of true/false conditions, one for each nested conditional + # starting with "true" + set condstack {1} + set result {} + set linenum 0 + foreach line [split [readfile $infile] \n] { + incr linenum + if {[regexp {^@(if|else|endif)(\s*)(.*)} $line -> condtype condspace condargs]} { + if {$condtype eq "if"} { + if {[string length $condspace] == 0} { + autosetup-error "$infile:$linenum: Invalid expression: $line" + } + if {[llength $condargs] == 1} { + # ABC => [get-define ABC] ni {0 ""} + # !ABC => [get-define ABC] in {0 ""} + lassign $condargs condvar + if {[regexp {^!(.*)} $condvar -> condvar]} { + set op in + } else { + set op ni + } + set condexpr "\[[list get-define $condvar]\] $op {0 {}}" + } else { + # Translate alphanumeric ABC into [get-define ABC] and leave the + # rest of the expression untouched + regsub -all {([A-Z][[:alnum:]_]*)} $condargs {[get-define \1]} condexpr + } + if {[catch [list expr $condexpr] condval]} { + dputs $condval + autosetup-error "$infile:$linenum: Invalid expression: $line" + } + dputs "@$condtype: $condexpr => $condval" + } + if {$condtype ne "if"} { + if {[llength $condstack] <= 1} { + autosetup-error "$infile:$linenum: Error: @$condtype missing @if" + } elseif {[string length $condargs] && [string index $condargs 0] ne "#"} { + autosetup-error "$infile:$linenum: Error: Extra arguments after @$condtype" + } + } + switch -exact $condtype { + if { + # push condval + lappend condstack $condval + } + else { + # Toggle the last entry + set condval [lpop condstack] + set condval [expr {!$condval}] + lappend condstack $condval + } + endif { + if {[llength $condstack] == 0} { + user-notice "$infile:$linenum: Error: @endif missing @if" + } + lpop condstack + } + } + continue + } + # Only continue if the stack contains all "true" + if {"0" in $condstack} { + continue + } + if {[regexp {^@include\s+(.*)} $line -> filearg]} { + set incfile [string map $mapping $filearg] + if {[file exists $incfile]} { + lappend ::autosetup(deps) [file-normalize $incfile] + lappend result {*}[include-file $incfile $mapping] + } else { + user-error "$infile:$linenum: Include file $incfile is missing" + } + continue + } + if {[regexp {^@define\s+(\w+)\s+(.*)} $line -> var val]} { + define $var $val + continue + } + lappend result $line + } + return $result +} + + +# @make-template template ?outfile? +# +# Reads the input file '/$template' and writes the output file '$outfile' +# (unless unchanged). +# If '$outfile' is blank/omitted, '$template' should end with '.in' which +# is removed to create the output file name. +# +# Each pattern of the form '@define@' is replaced with the corresponding +# "define", if it exists, or left unchanged if not. +# +# The special value '@srcdir@' is substituted with the relative +# path to the source directory from the directory where the output +# file is created, while the special value '@top_srcdir@' is substituted +# with the relative path to the top level source directory. +# +# Conditional sections may be specified as follows: +## @if NAME eq "value" +## lines +## @else +## lines +## @endif +# +# Where 'NAME' is a defined variable name and '@else' is optional. +# Note that variables names *must* start with an uppercase letter. +# If the expression does not match, all lines through '@endif' are ignored. +# +# The alternative forms may also be used: +## @if NAME (true if the variable is defined, but not empty and not "0") +## @if !NAME (opposite of the form above) +## @if +# +# In the general Tcl expression, any words beginning with an uppercase letter +# are translated into [get-define NAME] +# +# Expressions may be nested +# +proc make-template {template {out {}}} { + set infile [file join $::autosetup(srcdir) $template] + + if {![file exists $infile]} { + user-error "Template $template is missing" + } + + # Define this as late as possible + define AUTODEPS $::autosetup(deps) + + if {$out eq ""} { + if {[file ext $template] ne ".in"} { + autosetup-error "make_template $template has no target file and can't guess" + } + set out [file rootname $template] + } + + set outdir [file dirname $out] + + # Make sure the directory exists + file mkdir $outdir + + # Set up srcdir and top_srcdir to be relative to the target dir + define srcdir [relative-path [file join $::autosetup(srcdir) $outdir] $outdir] + define top_srcdir [relative-path $::autosetup(srcdir) $outdir] + + # Build map from global defines to their values so they can be + # substituted into @include file names. + proc build-define-mapping {} { + set mapping {} + foreach {n v} [array get ::define] { + lappend mapping @$n@ $v + } + return $mapping + } + set mapping [build-define-mapping] + + set result [include-file $infile $mapping] + + # Rebuild the define mapping in case we ran across @define + # directives in the template or a file it @included, then + # apply that mapping to the expanded template. + set mapping [build-define-mapping] + write-if-changed $out [string map $mapping [join $result \n]] { + msg-result "Created [relative-path $out] from [relative-path $template]" + } +} + +proc system-init {} { + global autosetup + + # build/host tuples and cross-compilation prefix + opt-str build build "" + define build_alias $build + if {$build eq ""} { + define build [config_guess] + } else { + define build [config_sub $build] + } + + opt-str host host "" + define host_alias $host + if {$host eq ""} { + define host [get-define build] + set cross "" + } else { + define host [config_sub $host] + set cross $host- + } + define cross [get-env CROSS $cross] + + # build/host _cpu, _vendor and _os + foreach type {build host} { + set v [get-define $type] + if {![regexp {^([^-]+)-([^-]+)-(.*)$} $v -> cpu vendor os]} { + user-error "Invalid canonical $type: $v" + } + define ${type}_cpu $cpu + define ${type}_vendor $vendor + define ${type}_os $os + } + + opt-str prefix prefix /usr/local + + # These are for compatibility with autoconf + define target [get-define host] + define prefix $prefix + define builddir $autosetup(builddir) + define srcdir $autosetup(srcdir) + define top_srcdir $autosetup(srcdir) + define abs_top_srcdir [file-normalize $autosetup(srcdir)] + define abs_top_builddir [file-normalize $autosetup(builddir)] + + # autoconf supports all of these + define exec_prefix [opt-str exec-prefix exec_prefix $prefix] + foreach {name defpath} { + bindir /bin + sbindir /sbin + libexecdir /libexec + libdir /lib + } { + define $name [opt-str $name o $exec_prefix$defpath] + } + foreach {name defpath} { + datadir /share + sharedstatedir /com + infodir /share/info + mandir /share/man + includedir /include + } { + define $name [opt-str $name o $prefix$defpath] + } + if {$prefix ne {/usr}} { + opt-str sysconfdir sysconfdir $prefix/etc + } else { + opt-str sysconfdir sysconfdir /etc + } + define sysconfdir $sysconfdir + + define localstatedir [opt-str localstatedir o /var] + define runstatedir [opt-str runstatedir o /run] + + define SHELL [get-env SHELL [find-an-executable sh bash ksh]] + + # These could be used to generate Makefiles following some automake conventions + define AM_SILENT_RULES [opt-bool silent-rules] + define AM_MAINTAINER_MODE [opt-bool maintainer-mode] + define AM_DEPENDENCY_TRACKING [opt-bool dependency-tracking] + + # Windows vs. non-Windows + switch -glob -- [get-define host] { + *-*-ming* - *-*-cygwin - *-*-msys { + define-feature windows + define EXEEXT .exe + } + default { + define EXEEXT "" + } + } + + # Display + msg-result "Host System...[get-define host]" + msg-result "Build System...[get-define build]" +} + +system-init diff --git a/autosetup/teaish/README.txt b/autosetup/teaish/README.txt new file mode 100644 index 0000000000..e11519b042 --- /dev/null +++ b/autosetup/teaish/README.txt @@ -0,0 +1,4 @@ +The *.tcl files in this directory are part of the SQLite's "autoconf" +bundle which are specific to the TEA(-ish) build. During the tarball +generation process, they are copied into /autoconf/autosetup/teaish +(which itself is created as part of that process). diff --git a/autosetup/teaish/core.tcl b/autosetup/teaish/core.tcl new file mode 100644 index 0000000000..c9abfa0626 --- /dev/null +++ b/autosetup/teaish/core.tcl @@ -0,0 +1,2564 @@ +######################################################################## +# 2025 April 5 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# * May you do good and not evil. +# * May you find forgiveness for yourself and forgive others. +# * May you share freely, never taking more than you give. +# +######################################################################## +# ----- @module teaish.tcl ----- +# @section TEA-ish ((TCL Extension Architecture)-ish) +# +# Functions in this file with a prefix of teaish__ are +# private/internal APIs. Those with a prefix of teaish- are +# public APIs. +# +# Teaish has a hard dependency on proj.tcl, and any public API members +# of that module are considered legal for use by teaish extensions. +# +# Project home page: https://fossil.wanderinghorse.net/r/teaish + +use proj + +# +# API-internal settings and shared state. +array set teaish__Config [proj-strip-hash-comments { + # + # Teaish's version number, not to be confused with + # teaish__PkgInfo(-version). + # + version 0.1-beta + + # set to 1 to enable some internal debugging output + debug-enabled 0 + + # + # 0 = don't yet have extension's pkgindex + # 0x01 = found TEAISH_EXT_DIR/pkgIndex.tcl.in + # 0x02 = found srcdir/pkgIndex.tcl.in + # 0x10 = found TEAISH_EXT_DIR/pkgIndex.tcl (static file) + # 0x20 = static-pkgIndex.tcl pragma: behave as if 0x10 + # 0x100 = disabled by -tm.tcl.in + # 0x200 = disabled by -tm.tcl + # + # Reminder: it's significant that the bottom 4 bits be + # cases where teaish manages ./pkgIndex.tcl. + # + pkgindex-policy 0 + + # + # The pkginit counterpart of pkgindex-policy: + # + # 0 = no pkginit + # 0x01 = found default X.in: generate X from X.in + # 0x10 = found static pkginit file X + # 0x02 = user-provided X.in generates ./X. + # 0x20 = user-provided static pkginit file X + # + # The 0x0f bits indicate that teaish is responsible for cleaning up + # the (generated) pkginit file. + # + pkginit-policy 0 + # + # 0 = no tm.tcl + # 0x01 = tm.tcl.in + # 0x10 = static tm.tcl + tm-policy 0 + + # + # If 1+ then teaish__verbose will emit messages. + # + verbose 0 + + # + # Mapping of pkginfo -flags to their TEAISH_xxx define (if any). + # This must not be modified after initialization. + # + pkginfo-f2d { + -name TEAISH_NAME + -name.dist TEAISH_DIST_NAME + -name.pkg TEAISH_PKGNAME + -version TEAISH_VERSION + -libDir TEAISH_LIBDIR_NAME + -loadPrefix TEAISH_LOAD_PREFIX + -vsatisfies TEAISH_VSATISFIES + -pkgInit.tcl TEAISH_PKGINIT_TCL + -pkgInit.tcl.in TEAISH_PKGINIT_TCL_IN + -url TEAISH_URL + -tm.tcl TEAISH_TM_TCL + -tm.tcl.in TEAISH_TM_TCL_IN + -options {} + -pragmas {} + -src {} + } + + # + # Queues for use with teaish-checks-queue and teaish-checks-run. + # + queued-checks-pre {} + queued-checks-post {} + + # Whether or not "make dist" parts are enabled. They get enabled + # when building from an extension's dir, disabled when building + # elsewhere. + dist-enabled 1 + # Whether or not "make install" parts are enabled. By default + # they are, but we have a single use case where they're + # both unnecessary and unhelpful, so... + install-enabled 1 + + # By default we enable compilation of a native extension but if the + # extension has no native code or the user wants to take that over + # via teaish.make.in or provide a script-only extension, we will + # elide the default compilation rules if this is 0. + dll-enabled 1 + + # Files to include in the "make dist" bundle. + dist-files {} + + # List of source files for the extension. + extension-src {} + + # Path to the teaish.tcl file. + teaish.tcl {} + + # Dir where teaish.tcl is found. + extension-dir {} + + # Whether the generates TEASH_VSATISFIES_CODE should error out on a + # satisfies error. If 0, it uses return instead of error. + vsatisfies-error 1 + + # Whether or not to allow a "full dist" - a "make dist" build which + # includes both the extension and teaish. By default this is only on + # if the extension dir is teaish's dir. + dist-full-enabled 0 +}] +set teaish__Config(core-dir) $::autosetup(libdir)/teaish + +# +# Array of info managed by teaish-pkginfo-get and friends. Has the +# same set of keys as $teaish__Config(pkginfo-f2d). +# +array set teaish__PkgInfo {} + +# +# Runs {*}$args if $lvl is <= the current verbosity level, else it has +# no side effects. +# +proc teaish__verbose {lvl args} { + if {$lvl <= $::teaish__Config(verbose)} { + {*}$args + } +} + +# +# @teaish-argv-has flags... +# +# Returns true if any arg in $::argv matches any of the given globs, +# else returns false. +# +proc teaish-argv-has {args} { + foreach glob $args { + foreach arg $::argv { + if {[string match $glob $arg]} { + return 1 + } + } + } + return 0 +} + +if {[teaish-argv-has --teaish-verbose --t-v]} { + # Check this early so that we can use verbose-only messages in the + # pre-options-parsing steps. + set ::teaish__Config(verbose) 1 + #teaish__verbose 1 msg-result "--teaish-verbose activated" +} + +msg-quiet use system ; # Outputs "Host System" and "Build System" lines +if {"--help" ni $::argv} { + teaish__verbose 1 msg-result "TEA(ish) Version = $::teaish__Config(version)" + teaish__verbose 1 msg-result "Source dir = $::autosetup(srcdir)" + teaish__verbose 1 msg-result "Build dir = $::autosetup(builddir)" +} + +# +# @teaish-configure-core +# +# Main entry point for the TEA-ish configure process. auto.def's primary +# (ideally only) job should be to call this. +# +proc teaish-configure-core {} { + proj-tweak-default-env-dirs + + set ::teaish__Config(install-mode) [teaish-argv-has --teaish-install*] + set ::teaish__Config(create-ext-mode) \ + [teaish-argv-has --teaish-create-extension=* --t-c-e=*] + set gotExt 0; # True if an extension config is found + if {!$::teaish__Config(create-ext-mode) + && !$::teaish__Config(install-mode)} { + # Don't look for an extension if we're in --t-c-e or --t-i mode + set gotExt [teaish__find_extension] + } + + # + # Set up the core --flags. This needs to come before teaish.tcl is + # sourced so that that file can use teaish-pkginfo-set to append + # options. + # + options-add [proj-strip-hash-comments { + with-tcl:DIR + => {Directory containing tclConfig.sh or a directory one level up from + that, from which we can derive a directory containing tclConfig.sh. + Defaults to the $TCL_HOME environment variable.} + + with-tclsh:PATH + => {Full pathname of tclsh to use. It is used for trying to find + tclConfig.sh. Warning: if its containing dir has multiple tclsh + versions, it may select the wrong tclConfig.sh! + Defaults to the $TCLSH environment variable.} + + tcl-stubs=0 => {Enable use of Tcl stubs library.} + + # TEA has --with-tclinclude but it appears to only be useful for + # building an extension against an uninstalled copy of TCL's own + # source tree. The policy here is that either we get that info + # from tclConfig.sh or we give up. + # + # with-tclinclude:DIR + # => {Specify the directory which contains the tcl.h. This should not + # normally be required, as that information comes from tclConfig.sh.} + + # We _generally_ want to reduce the possibility of flag collisions with + # extensions, and thus use a teaish-... prefix on most flags. However, + # --teaish-extension-dir is frequently needed, so... + # + # As of this spontaneous moment, we'll settle on using --t-A-X to + # abbreviate --teaish-A...-X... flags when doing so is + # unambiguous... + ted: t-e-d: + teaish-extension-dir:DIR + => {Looks for an extension in the given directory instead of the current + dir.} + + t-c-e: + teaish-create-extension:TARGET_DIRECTORY + => {Writes stub files for creating an extension. Will refuse to overwrite + existing files without --teaish-force.} + + t-f + teaish-force + => {Has a context-dependent meaning (autosetup defines --force for its + own use).} + + t-d-d + teaish-dump-defines + => {Dump all configure-defined vars to config.defines.txt} + + t-v:=0 + teaish-verbose:=0 + => {Enable more (often extraneous) messages from the teaish core.} + + t-d + teaish-debug=0 => {Enable teaish-specific debug output} + + t-i + teaish-install:=auto + => {Installs a copy of teaish, including autosetup, to the target dir. + When used with --teaish-create-extension=DIR, a value of "auto" + (no no value) will inherit that directory.} + + #TODO: --teaish-install-extension:=dir as short for + # --t-c-e=dir --t-i + + t-e-p: + teaish-extension-pkginfo:pkginfo + => {For use with --teaish-create-extension. If used, it must be a + list of arguments for use with teaish-pkginfo-set, e.g. + --teaish-extension-pkginfo="-name Foo -version 2.3"} + + t-v-c + teaish-vsatisfies-check=1 + => {Disable the configure-time "vsatisfies" check on the target tclsh.} + + }]; # main options. + + if {$gotExt} { + # We found an extension. Source it... + set ttcl $::teaish__Config(teaish.tcl) + proj-assert {"" ne [teaish-pkginfo-get -name]} + proj-assert {[file exists $ttcl]} \ + "Expecting to have found teaish.(tcl|config) by now" + if {[string match *.tcl $ttcl]} { + uplevel 1 {source $::teaish__Config(teaish.tcl)} + } else { + teaish-pkginfo-set {*}[proj-file-content -trim $ttcl] + } + unset ttcl + # Set up some default values if the extension did not set them. + # This must happen _after_ it's sourced but before + # teaish-configure is called. + array set f2d $::teaish__Config(pkginfo-f2d) + foreach {pflag key type val} { + - TEAISH_CFLAGS -v "" + - TEAISH_LDFLAGS -v "" + - TEAISH_MAKEFILE -v "" + - TEAISH_MAKEFILE_CODE -v "" + - TEAISH_MAKEFILE_IN -v "" + - TEAISH_PKGINDEX_TCL -v "" + - TEAISH_PKGINDEX_TCL_IN -v "" + - TEAISH_PKGINIT_TCL -v "" + - TEAISH_PKGINIT_TCL_IN -v "" + - TEAISH_PKGINIT_TCL_TAIL -v "" + - TEAISH_TEST_TCL -v "" + - TEAISH_TEST_TCL_IN -v "" + + -version - -v 0.0.0 + -name.pkg - -e {set ::teaish__PkgInfo(-name)} + -name.dist - -e {set ::teaish__PkgInfo(-name)} + -libDir - -e { + join [list \ + $::teaish__PkgInfo(-name.pkg) \ + $::teaish__PkgInfo(-version)] "" + } + -loadPrefix - -e { + string totitle $::teaish__PkgInfo(-name.pkg) + } + -vsatisfies - -v {{Tcl 8.5-}} + -pkgInit.tcl - -v "" + -pkgInit.tcl.in - -v "" + -url - -v "" + -tm.tcl - -v "" + -tm.tcl.in - -v "" + -src - -v "" + } { + #proj-assert 0 {Just testing} + set isPIFlag [expr {"-" ne $pflag}] + if {$isPIFlag} { + if {[info exists ::teaish__PkgInfo($pflag)]} { + # Was already set - skip it. + continue; + } + proj-assert {{-} eq $key};# "Unexpected pflag=$pflag key=$key type=$type val=$val" + set key $f2d($pflag) + } + if {"" ne $key} { + if {"" ne [get-define $key ""]} { + # Was already set - skip it. + continue + } + } + switch -exact -- $type { + -v {} + -e { set val [eval $val] } + default { proj-error "Invalid type flag: $type" } + } + #puts "***** defining default $pflag $key {$val} isPIFlag=$isPIFlag" + if {$key ne ""} { + define $key $val + } + if {$isPIFlag} { + set ::teaish__PkgInfo($pflag) $val + } + } + unset isPIFlag pflag key type val + array unset f2d + }; # sourcing extension's teaish.tcl + + if {[llength [info proc teaish-options]] > 0} { + # Add options defined by teaish-options, which is assumed to be + # imported via [teaish-get -teaish-tcl]. + set o [teaish-options] + if {"" ne $o} { + options-add $o + } + } + #set opts [proj-options-combine] + #lappend opts teaish-debug => {x}; #testing dupe entry handling + if {[catch {options {}} msg xopts]} { + # Workaround for + # where [options] behaves oddly on _some_ TCL builds when it's + # called from deeper than the global scope. + dict incr xopts -level + return {*}$xopts $msg + } + + proj-xfer-options-aliases { + t-c-e => teaish-create-extension + t-d => teaish-debug + t-d-d => teaish-dump-defines + ted => teaish-extension-dir + t-e-d => teaish-extension-dir + t-e-p => teaish-extension-pkginfo + t-f => teaish-force + t-i => teaish-install + t-v => teaish-verbose + t-v-c => teaish-vsatisfies-check + } + + scan [opt-val teaish-verbose 0] %d ::teaish__Config(verbose) + set ::teaish__Config(debug-enabled) [opt-bool teaish-debug] + + set exitEarly 0 + if {[proj-opt-was-provided teaish-create-extension]} { + teaish__create_extension [opt-val teaish-create-extension] + incr exitEarly + } + if {$::teaish__Config(install-mode)} { + teaish__install + incr exitEarly + } + + if {$exitEarly} { + file delete -force config.log + return + } + proj-assert {1==$gotExt} "Else we cannot have gotten this far" + + teaish__configure_phase1 +} + + +# +# Internal config-time debugging output routine. It is not legal to +# call this from the global scope. +# +proc teaish-debug {msg} { + if {$::teaish__Config(debug-enabled)} { + puts stderr [proj-bold "** DEBUG: \[[proj-scope 1]\]: $msg"] + } +} + +# +# Runs "phase 1" of the configuration, immediately after processing +# --flags. This is what will import the client-defined teaish.tcl. +# +proc teaish__configure_phase1 {} { + msg-result \ + [join [list "Configuring build of Tcl extension" \ + [proj-bold [teaish-pkginfo-get -name] \ + [teaish-pkginfo-get -version]] "..."]] + + uplevel 1 { + use cc cc-db cc-shared cc-lib; # pkg-config + } + teaish__check_tcl + apply {{} { + # + # If --prefix or --exec-prefix are _not_ provided, use their + # TCL_... counterpart from tclConfig.sh. Caveat: by the time we can + # reach this point, autosetup's system.tcl will have already done + # some non-trivial amount of work with these to create various + # derived values from them, so we temporarily end up with a mishmash + # of autotools-compatibility var values. That will be straightened + # out in the final stage of the configure script via + # [proj-remap-autoconf-dir-vars]. + # + foreach {flag uflag tclVar} { + prefix prefix TCL_PREFIX + exec-prefix exec_prefix TCL_EXEC_PREFIX + } { + if {![proj-opt-was-provided $flag]} { + if {"exec-prefix" eq $flag} { + # If --exec-prefix was not used, ensure that --exec-prefix + # derives from the --prefix we may have just redefined. + set v {${prefix}} + } else { + set v [get-define $tclVar "???"] + teaish__verbose 1 msg-result "Using \$$tclVar for --$flag=$v" + } + proj-assert {"???" ne $v} "Expecting teaish__check_tcl to have defined $tclVar" + #puts "*** $flag $uflag $tclVar = $v" + proj-opt-set $flag $v + define $uflag $v + + # ^^^ As of here, all autotools-compatibility vars which derive + # from --$flag, e.g. --libdir, still derive from the default + # --$flag value which was active when system.tcl was + # included. So long as those flags are not explicitly passed to + # the configure script, those will be straightened out via + # [proj-remap-autoconf-dir-vars]. + } + } + }}; # --[exec-]prefix defaults + teaish__check_common_bins + # + # Set up library file names + # + proj-file-extensions + teaish__define_pkginfo_derived * + + teaish-checks-run -pre + if {[llength [info proc teaish-configure]] > 0} { + # teaish-configure is assumed to be imported via + # teaish.tcl + teaish-configure + } + teaish-checks-run -post + + define TEAISH_USE_STUBS [opt-bool tcl-stubs] + + apply {{} { + # Set up "vsatisfies" code for pkgIndex.tcl.in, + # _teaish.tester.tcl.in, and for a configure-time check. We would + # like to put this before [teaish-checks-run -pre] but it's + # marginally conceivable that a client may need to dynamically + # calculate the vsatisfies and set it via [teaish-configure]. + set vs [get-define TEAISH_VSATISFIES ""] + if {"" eq $vs} return + set code {} + set n 0 + # Treat $vs as a list-of-lists {{Tcl 8.5-} {Foo 1.0- -3.0} ...} + # and generate Tcl which will run package vsatisfies tests with + # that info. + foreach pv $vs { + set n [llength $pv] + if {$n < 2} { + proj-error "-vsatisfies: {$pv} appears malformed. Whole list is: $vs" + } + set pkg [lindex $pv 0] + set vcheck {} + for {set i 1} {$i < $n} {incr i} { + lappend vcheck [lindex $pv $i] + } + if {[opt-bool teaish-vsatisfies-check]} { + set tclsh [get-define TCLSH_CMD] + set vsat "package vsatisfies \[ package provide $pkg \] $vcheck" + set vputs "puts \[ $vsat \]" + #puts "*** vputs = $vputs" + scan [exec echo $vputs | $tclsh] %d vvcheck + if {![info exists vvcheck] || 0 == $vvcheck} { + proj-fatal -up $tclsh "check failed:" $vsat + } + } + if {$::teaish__Config(vsatisfies-error)} { + set vunsat \ + [list error [list Package \ + $::teaish__PkgInfo(-name) $::teaish__PkgInfo(-version) \ + requires $pv]] + } else { + set vunsat return + } + lappend code \ + [string trim [subst -nocommands \ + {if { ![package vsatisfies [package provide $pkg] $vcheck] } {\n $vunsat\n}}]] + }; # foreach pv + define TEAISH_VSATISFIES_CODE [join $code "\n"] + }}; # vsatisfies + + if {[proj-looks-like-windows]} { + # Without this, linking of an extension will not work on Cygwin or + # Msys2. + msg-result "Using USE_TCL_STUBS for Unix(ish)-on-Windows environment" + teaish-cflags-add -DUSE_TCL_STUBS=1 + } + + #define AS_LIBDIR $::autosetup(libdir) + define TEAISH_TESTUTIL_TCL $::teaish__Config(core-dir)/tester.tcl + + apply {{} { + # + # Ensure we have a pkgIndex.tcl and don't have a stale generated one + # when rebuilding for different --with-tcl=... values. + # + if {!$::teaish__Config(pkgindex-policy)} { + proj-error "Cannot determine which pkgIndex.tcl to use" + } + if {0x300 & $::teaish__Config(pkgindex-policy)} { + teaish__verbose 1 msg-result "pkgIndex disabled by -tm.tcl(.in)" + } else { + set tpi [proj-coalesce \ + [get-define TEAISH_PKGINDEX_TCL_IN] \ + [get-define TEAISH_PKGINDEX_TCL]] + proj-assert {$tpi ne ""} \ + "TEAISH_PKGINDEX_TCL should have been set up by now" + teaish__verbose 1 msg-result "Using pkgIndex from $tpi" + if {0x0f & $::teaish__Config(pkgindex-policy)} { + # Don't leave stale pkgIndex.tcl laying around yet don't delete + # or overwrite a user-managed static pkgIndex.tcl. + file delete -force -- [get-define TEAISH_PKGINDEX_TCL] + proj-dot-ins-append [get-define TEAISH_PKGINDEX_TCL_IN] + } else { + teaish-dist-add [file tail $tpi] + } + } + }}; # $::teaish__Config(pkgindex-policy) + + # + # Ensure we clean up TEAISH_PKGINIT_TCL if needed and @-process + # TEAISH_PKGINIT_TCL_IN if needed. + # + if {0x0f & $::teaish__Config(pkginit-policy)} { + file delete -force -- [get-define TEAISH_PKGINIT_TCL] + proj-dot-ins-append [get-define TEAISH_PKGINIT_TCL_IN] \ + [get-define TEAISH_PKGINIT_TCL] + } + if {0x0f & $::teaish__Config(tm-policy)} { + file delete -force -- [get-define TEAISH_TM_TCL] + proj-dot-ins-append [get-define TEAISH_TM_TCL_IN] + } + + apply {{} { + # Queue up any remaining dot-in files + set dotIns [list] + foreach {dIn => dOut} { + TEAISH_TESTER_TCL_IN => TEAISH_TESTER_TCL + TEAISH_TEST_TCL_IN => TEAISH_TEST_TCL + TEAISH_MAKEFILE_IN => TEAISH_MAKEFILE + } { + lappend dotIns [get-define $dIn ""] [get-define $dOut ""] + } + lappend dotIns $::autosetup(srcdir)/Makefile.in Makefile; # must be after TEAISH_MAKEFILE_IN. + # Much later: probably because of timestamps for deps purposes :-? + #puts "dotIns=$dotIns" + foreach {i o} $dotIns { + if {"" ne $i && "" ne $o} { + #puts " pre-dot-ins-append: \[$i\] -> \[$o\]" + proj-dot-ins-append $i $o + } + } + }} + + define TEAISH_DIST_FULL \ + [expr { + $::teaish__Config(dist-enabled) + && $::teaish__Config(dist-full-enabled) + }] + + define TEAISH_AUTOSETUP_DIR $::teaish__Config(core-dir) + define TEAISH_ENABLE_DIST $::teaish__Config(dist-enabled) + define TEAISH_ENABLE_INSTALL $::teaish__Config(install-enabled) + define TEAISH_ENABLE_DLL $::teaish__Config(dll-enabled) + define TEAISH_TCL $::teaish__Config(teaish.tcl) + + define TEAISH_DIST_FILES [join $::teaish__Config(dist-files)] + define TEAISH_EXT_DIR [join $::teaish__Config(extension-dir)] + define TEAISH_EXT_SRC [join $::teaish__Config(extension-src)] + proj-setup-autoreconfig TEAISH_AUTORECONFIG + foreach f { + TEAISH_CFLAGS + TEAISH_LDFLAGS + } { + # Ensure that any of these lists are flattened + define $f [join [get-define $f]] + } + proj-remap-autoconf-dir-vars + set tdefs [teaish__defines_to_list] + define TEAISH__DEFINES_MAP $tdefs; # injected into _teaish.tester.tcl + + # + # NO [define]s after this point! + # + proj-if-opt-truthy teaish-dump-defines { + proj-file-write config.defines.txt $tdefs + } + proj-dot-ins-process -validate + +}; # teaish__configure_phase1 + +# +# Run checks for required binaries. +# +proc teaish__check_common_bins {} { + if {"" eq [proj-bin-define install]} { + proj-warn "Cannot find install binary, so 'make install' will not work." + define BIN_INSTALL false + } + if {"" eq [proj-bin-define zip]} { + proj-warn "Cannot find zip, so 'make dist.zip' will not work." + } + if {"" eq [proj-bin-define tar]} { + proj-warn "Cannot find tar, so 'make dist.tgz' will not work." + } +} + +# +# TCL... +# +# teaish__check_tcl performs most of the --with-tcl and --with-tclsh +# handling. Some related bits and pieces are performed before and +# after that function is called. +# +# Important [define]'d vars: +# +# - TCLSH_CMD is the path to the canonical tclsh or "". +# +# - TCL_CONFIG_SH is the path to tclConfig.sh or "". +# +# - TCLLIBDIR is the dir to which the extension library gets +# - installed. +# +proc teaish__check_tcl {} { + define TCLSH_CMD false ; # Significant is that it exits with non-0 + define TCLLIBDIR "" ; # Installation dir for TCL extension lib + define TCL_CONFIG_SH ""; # full path to tclConfig.sh + + # Clear out all vars which would harvest from tclConfig.sh so that + # the late-config validation of @VARS@ works even if --disable-tcl + # is used. + proj-tclConfig-sh-to-autosetup "" + + # TODO: better document the steps this is taking. + set srcdir $::autosetup(srcdir) + msg-result "Checking for a suitable tcl... " + set use_tcl 1 + set withSh [opt-val with-tclsh [proj-get-env TCLSH]] + set tclHome [opt-val with-tcl [proj-get-env TCL_HOME]] + if {[string match */lib $tclHome]} { + # TEA compatibility kludge: its --with-tcl wants the lib + # dir containing tclConfig.sh. + #proj-warn "Replacing --with-tcl=$tclHome for TEA compatibility" + regsub {/lib^} $tclHome "" tclHome + msg-result "NOTE: stripped /lib suffix from --with-tcl=$tclHome (a TEA-ism)" + } + if {0} { + # This misinteracts with the $TCL_PREFIX default: it will use the + # autosetup-defined --prefix default + if {"prefix" eq $tclHome} { + set tclHome [get-define prefix] + } + } + teaish-debug "use_tcl ${use_tcl}" + teaish-debug "withSh=${withSh}" + teaish-debug "tclHome=$tclHome" + if {"" eq $withSh && "" eq $tclHome} { + # If neither --with-tclsh nor --with-tcl are provided, try to find + # a workable tclsh. + set withSh [proj-first-bin-of tclsh9.1 tclsh9.0 tclsh8.6 tclsh] + teaish-debug "withSh=${withSh}" + } + + set doConfigLookup 1 ; # set to 0 to test the tclConfig.sh-not-found cases + if {"" ne $withSh} { + # --with-tclsh was provided or found above. Validate it and use it + # to trump any value passed via --with-tcl=DIR. + if {![file-isexec $withSh]} { + proj-error "TCL shell $withSh is not executable" + } else { + define TCLSH_CMD $withSh + #msg-result "Using tclsh: $withSh" + } + if {$doConfigLookup && + [catch {exec $withSh $::autosetup(libdir)/find_tclconfig.tcl} result] == 0} { + set tclHome $result + } + if {"" ne $tclHome && [file isdirectory $tclHome]} { + teaish__verbose 1 msg-result "$withSh recommends the tclConfig.sh from $tclHome" + } else { + proj-warn "$withSh is unable to recommend a tclConfig.sh" + set use_tcl 0 + } + } + set cfg "" + set tclSubdirs {tcl9.1 tcl9.0 tcl8.6 tcl8.5 lib} + while {$use_tcl} { + if {"" ne $tclHome} { + # Ensure that we can find tclConfig.sh under ${tclHome}/... + if {$doConfigLookup} { + if {[file readable "${tclHome}/tclConfig.sh"]} { + set cfg "${tclHome}/tclConfig.sh" + } else { + foreach i $tclSubdirs { + if {[file readable "${tclHome}/$i/tclConfig.sh"]} { + set cfg "${tclHome}/$i/tclConfig.sh" + break + } + } + } + } + if {"" eq $cfg} { + proj-error "No tclConfig.sh found under ${tclHome}" + } + } else { + # If we have not yet found a tclConfig.sh file, look in $libdir + # which is set automatically by autosetup or via the --prefix + # command-line option. See + # https://sqlite.org/forum/forumpost/e04e693439a22457 + set libdir [get-define libdir] + if {[file readable "${libdir}/tclConfig.sh"]} { + set cfg "${libdir}/tclConfig.sh" + } else { + foreach i $tclSubdirs { + if {[file readable "${libdir}/$i/tclConfig.sh"]} { + set cfg "${libdir}/$i/tclConfig.sh" + break + } + } + } + if {![file readable $cfg]} { + break + } + } + teaish__verbose 1 msg-result "Using tclConfig.sh = $cfg" + break + }; # while {$use_tcl} + define TCL_CONFIG_SH $cfg + # Export a subset of tclConfig.sh to the current TCL-space. If $cfg + # is an empty string, this emits empty-string entries for the + # various options we're interested in. + proj-tclConfig-sh-to-autosetup $cfg + + if {"" eq $withSh && $cfg ne ""} { + # We have tclConfig.sh but no tclsh. Attempt to locate a tclsh + # based on info from tclConfig.sh. + set tclExecPrefix [get-define TCL_EXEC_PREFIX] + proj-assert {"" ne $tclExecPrefix} + set tryThese [list \ + $tclExecPrefix/bin/tclsh[get-define TCL_VERSION] \ + $tclExecPrefix/bin/tclsh ] + foreach trySh $tryThese { + if {[file-isexec $trySh]} { + set withSh $trySh + break + } + } + if {![file-isexec $withSh]} { + proj-warn "Cannot find a usable tclsh (tried: $tryThese)" + } + } + define TCLSH_CMD $withSh + if {$use_tcl} { + # Set up the TCLLIBDIR + set tcllibdir [get-env TCLLIBDIR ""] + set extDirName [teaish-pkginfo-get -libDir] + if {"" eq $tcllibdir} { + # Attempt to extract TCLLIBDIR from TCL's $auto_path + if {"" ne $withSh && + [catch {exec echo "puts stdout \$auto_path" | "$withSh"} result] == 0} { + foreach i $result { + if {![string match //zip* $i] && [file isdirectory $i]} { + # isdirectory actually passes on //zipfs:/..., but those are + # useless for our purposes + set tcllibdir $i/$extDirName + break + } + } + } else { + proj-error "Cannot determine TCLLIBDIR." + } + } + define TCLLIBDIR $tcllibdir + }; # find TCLLIBDIR + + set gotSh [file-isexec $withSh] + set tmdir ""; # first tcl::tm::list entry + if {$gotSh} { + catch { + set tmli [exec echo {puts [tcl::tm::list]} | $withSh] + # Reminder: this list contains many names of dirs which do not + # exist but are legitimate. If we rely only on an is-dir check, + # we can end up not finding any of the many candidates. + set firstDir "" + foreach d $tmli { + if {"" eq $firstDir && ![string match //*:* $d]} { + # First non-VFS entry, e.g. not //zipfs: + set firstDir $d + } + if {[file isdirectory $d]} { + set tmdir $d + break + } + } + if {"" eq $tmdir} { + set tmdir $firstDir + } + }; # find tcl::tm path + } + define TEAISH_TCL_TM_DIR $tmdir + + # Finally, let's wrap up... + if {$gotSh} { + teaish__verbose 1 msg-result "Using tclsh = $withSh" + if {$cfg ne ""} { + define HAVE_TCL 1 + } else { + proj-warn "Found tclsh but no tclConfig.sh." + } + if {"" eq $tmdir} { + proj-warn "Did not find tcl::tm directory." + } + } + show-notices + # If TCL is not found: if it was explicitly requested then fail + # fatally, else just emit a warning. If we can find the APIs needed + # to generate a working JimTCL then that will suffice for build-time + # TCL purposes (see: proc sqlite-determine-codegen-tcl). + if {!$gotSh} { + proj-error "Did not find tclsh" + } elseif {"" eq $cfg} { + proj-indented-notice -error { + Cannot find a usable tclConfig.sh file. Use --with-tcl=DIR to + specify a directory near which tclConfig.sh can be found, or + --with-tclsh=/path/to/tclsh to allow the tclsh binary to locate + its tclConfig.sh, with the caveat that a symlink to tclsh, or + wrapper script around it, e.g. ~/bin/tclsh -> + $HOME/tcl/9.0/bin/tclsh9.1, may not work because tclsh emits + different library paths for the former than the latter. + } + } + msg-result "Using Tcl [get-define TCL_VERSION] from [get-define TCL_PREFIX]." + teaish__tcl_platform_quirks +}; # teaish__check_tcl + +# +# Perform last-minute platform-specific tweaks to account for quirks. +# +proc teaish__tcl_platform_quirks {} { + define TEAISH_POSTINST_PREREQUIRE "" + switch -glob -- [get-define host] { + *-haiku { + # Haiku's default TCLLIBDIR is "all wrong": it points to a + # read-only virtual filesystem mount-point. We bend it back to + # fit under $TCL_PACKAGE_PATH here. + foreach {k d} { + vj TCL_MAJOR_VERSION + vn TCL_MINOR_VERSION + pp TCL_PACKAGE_PATH + ld TCLLIBDIR + } { + set $k [get-define $d] + } + if {[string match /packages/* $ld]} { + set old $ld + set tail [file tail $ld] + if {8 == $vj} { + set ld "${pp}/tcl${vj}.${vn}/${tail}" + } else { + proj-assert {9 == $vj} + set ld "${pp}/${tail}" + } + define TCLLIBDIR $ld + # [load foo.so], without a directory part, does not work via + # automated tests on Haiku (but works when run + # manually). Similarly, the post-install [package require ...] + # test fails, presumably for a similar reason. We work around + # the former in _teaish.tester.tcl.in. We work around the + # latter by amending the post-install check's ::auto_path (in + # Makefile.in). This code MUST NOT contain any single-quotes. + define TEAISH_POSTINST_PREREQUIRE \ + [join [list set ::auto_path \ + \[ linsert \$::auto_path 0 $ld \] \; \ + ]] + proj-indented-notice [subst -nocommands -nobackslashes { + Haiku users take note: patching target installation dir to match + Tcl's home because Haiku's is not writable. + + Original : $old + Substitute: $ld + }] + } + } + } +}; # teaish__tcl_platform_quirks + +# +# Searches $::argv and/or the build dir and/or the source dir for +# teaish.tcl and friends. Fails if it cannot find teaish.tcl or if +# there are other irreconcilable problems. If it returns 0 then it did +# not find an extension but the --help flag was seen, in which case +# that's not an error. +# +# This does not _load_ the extension, it primarily locates the files +# which make up an extension and fills out no small amount of teaish +# state related to that. +# +proc teaish__find_extension {} { + proj-assert {!$::teaish__Config(install-mode)} + teaish__verbose 1 msg-result "Looking for teaish extension..." + + # Helper for the foreach loop below. + set checkTeaishTcl {{mustHave fid dir} { + set f [file join $dir $fid] + if {[file readable $f]} { + file-normalize $f + } elseif {$mustHave} { + proj-error "Missing required $dir/$fid" + } + }} + + # + # We have to handle some flags manually because the extension must + # be loaded before [options] is run (so that the extension can + # inject its own options). + # + set dirBld $::autosetup(builddir); # dir we're configuring under + set dirSrc $::autosetup(srcdir); # where teaish's configure script lives + set extT ""; # teaish.tcl + set largv {}; # rewritten $::argv + set gotHelpArg 0; # got the --help + foreach arg $::argv { + #puts "*** arg=$arg" + switch -glob -- $arg { + --ted=* - + --t-e-d=* - + --teaish-extension-dir=* { + # Ensure that $extD refers to a directory and contains a + # teaish.tcl. + regexp -- {--[^=]+=(.+)} $arg - extD + set extD [file-normalize $extD] + if {![file isdirectory $extD]} { + proj-error "--teaish-extension-dir value is not a directory: $extD" + } + set extT [apply $checkTeaishTcl 0 teaish.config $extD] + if {"" eq $extT} { + set extT [apply $checkTeaishTcl 1 teaish.tcl $extD] + } + set ::teaish__Config(extension-dir) $extD + } + --help { + incr gotHelpArg + lappend largv $arg + } + default { + lappend largv $arg + } + } + } + set ::argv $largv + + set dirExt $::teaish__Config(extension-dir); # dir with the extension + # + # teaish.tcl is a TCL script which implements various + # interfaces described by this framework. + # + # We use the first one we find in the builddir or srcdir. + # + if {"" eq $extT} { + set flist [list] + proj-assert {$dirExt eq ""} + lappend flist $dirBld/teaish.tcl $dirBld/teaish.config $dirSrc/teaish.tcl + if {![proj-first-file-found extT $flist]} { + if {$gotHelpArg} { + # Tell teaish-configure-core that the lack of extension is not + # an error when --help or --teaish-install is used. + return 0; + } + proj-indented-notice -error " +Did not find any of: $flist + +If you are attempting an out-of-tree build, use + --teaish-extension-dir=/path/to/extension" + } + } + if {![file readable $extT]} { + proj-error "extension tcl file is not readable: $extT" + } + set ::teaish__Config(teaish.tcl) $extT + set dirExt [file dirname $extT] + + set ::teaish__Config(extension-dir) $dirExt + set ::teaish__Config(blddir-is-extdir) [expr {$dirBld eq $dirExt}] + set ::teaish__Config(dist-enabled) $::teaish__Config(blddir-is-extdir); # may change later + set ::teaish__Config(dist-full-enabled) \ + [expr {[file-normalize $::autosetup(srcdir)] + eq [file-normalize $::teaish__Config(extension-dir)]}] + + set addDist {{file} { + teaish-dist-add [file tail $file] + }} + apply $addDist $extT + + teaish__verbose 1 msg-result "Extension dir = [teaish-get -dir]" + teaish__verbose 1 msg-result "Extension config = $extT" + + teaish-pkginfo-set -name [file tail [file dirname $extT]] + + # + # teaish.make[.in] provides some of the info for the main makefile, + # like which source(s) to build and their build flags. + # + # We use the first one of teaish.make.in or teaish.make we find in + # $dirExt. + # + if {[proj-first-file-found extM \ + [list \ + $dirExt/teaish.make.in \ + $dirExt/teaish.make \ + ]]} { + if {[string match *.in $extM]} { + define TEAISH_MAKEFILE_IN $extM + define TEAISH_MAKEFILE _[file rootname [file tail $extM]] + } else { + define TEAISH_MAKEFILE_IN "" + define TEAISH_MAKEFILE $extM + } + apply $addDist $extM + teaish__verbose 1 msg-result "Extension makefile = $extM" + } else { + define TEAISH_MAKEFILE_IN "" + define TEAISH_MAKEFILE "" + } + + # Look for teaish.pkginit.tcl[.in] + set piPolicy 0 + if {[proj-first-file-found extI \ + [list \ + $dirExt/teaish.pkginit.tcl.in \ + $dirExt/teaish.pkginit.tcl \ + ]]} { + if {[string match *.in $extI]} { + # Generate teaish.pkginit.tcl from $extI. + define TEAISH_PKGINIT_TCL_IN $extI + define TEAISH_PKGINIT_TCL [file rootname [file tail $extI]] + set piPolicy 0x01 + } else { + # Assume static $extI. + define TEAISH_PKGINIT_TCL_IN "" + define TEAISH_PKGINIT_TCL $extI + set piPolicy 0x10 + } + apply $addDist $extI + teaish__verbose 1 msg-result "Extension post-load init = $extI" + define TEAISH_PKGINIT_TCL_TAIL \ + [file tail [get-define TEAISH_PKGINIT_TCL]]; # for use in pkgIndex.tcl.in + } + set ::teaish__Config(pkginit-policy) $piPolicy + + # Look for pkgIndex.tcl[.in]... + set piPolicy 0 + if {[proj-first-file-found extPI $dirExt/pkgIndex.tcl.in]} { + # Generate ./pkgIndex.tcl from $extPI. + define TEAISH_PKGINDEX_TCL_IN $extPI + define TEAISH_PKGINDEX_TCL [file rootname [file tail $extPI]] + apply $addDist $extPI + set piPolicy 0x01 + } elseif {$dirExt ne $dirSrc + && [proj-first-file-found extPI $dirSrc/pkgIndex.tcl.in]} { + # Generate ./pkgIndex.tcl from $extPI. + define TEAISH_PKGINDEX_TCL_IN $extPI + define TEAISH_PKGINDEX_TCL [file rootname [file tail $extPI]] + set piPolicy 0x02 + } elseif {[proj-first-file-found extPI $dirExt/pkgIndex.tcl]} { + # Assume $extPI's a static file and use it. + define TEAISH_PKGINDEX_TCL_IN "" + define TEAISH_PKGINDEX_TCL $extPI + apply $addDist $extPI + set piPolicy 0x10 + } + # Reminder: we have to delay removal of stale TEAISH_PKGINDEX_TCL + # and the proj-dot-ins-append of TEAISH_PKGINDEX_TCL_IN until much + # later in the process. + set ::teaish__Config(pkgindex-policy) $piPolicy + + # Look for teaish.test.tcl[.in] + proj-assert {"" ne $dirExt} + set flist [list $dirExt/teaish.test.tcl.in $dirExt/teaish.test.tcl] + if {[proj-first-file-found ttt $flist]} { + if {[string match *.in $ttt]} { + # Generate _teaish.test.tcl from $ttt + set xt _[file rootname [file tail $ttt]] + file delete -force -- $xt; # ensure no stale copy is used + define TEAISH_TEST_TCL $xt + define TEAISH_TEST_TCL_IN $ttt + } else { + define TEAISH_TEST_TCL $ttt + define TEAISH_TEST_TCL_IN "" + } + apply $addDist $ttt + } else { + define TEAISH_TEST_TCL "" + define TEAISH_TEST_TCL_IN "" + } + + # Look for _teaish.tester.tcl[.in] + set flist [list $dirExt/_teaish.tester.tcl.in $dirSrc/_teaish.tester.tcl.in] + if {[proj-first-file-found ttt $flist]} { + # Generate teaish.test.tcl from $ttt + set xt [file rootname [file tail $ttt]] + file delete -force -- $xt; # ensure no stale copy is used + define TEAISH_TESTER_TCL $xt + define TEAISH_TESTER_TCL_IN $ttt + if {[lindex $flist 0] eq $ttt} { + apply $addDist $ttt + } + unset ttt xt + } else { + if {[file exists [set ttt [file join $dirSrc _teaish.tester.tcl.in]]]} { + set xt [file rootname [file tail $ttt]] + define TEAISH_TESTER_TCL $xt + define TEAISH_TESTER_TCL_IN $ttt + } else { + define TEAISH_TESTER_TCL "" + define TEAISH_TESTER_TCL_IN "" + } + } + unset flist + + # TEAISH_OUT_OF_EXT_TREE = 1 if we're building from a dir other + # than the extension's home dir. + define TEAISH_OUT_OF_EXT_TREE \ + [expr {[file-normalize $::autosetup(builddir)] ne \ + [file-normalize $::teaish__Config(extension-dir)]}] + return 1 +}; # teaish__find_extension + +# +# @teaish-cflags-add ?-p|prepend? ?-define? cflags... +# +# Equivalent to [proj-define-amend TEAISH_CFLAGS {*}$args]. +# +proc teaish-cflags-add {args} { + proj-define-amend TEAISH_CFLAGS {*}$args +} + +# +# @teaish-define-to-cflag ?flags? defineName...|{defineName...} +# +# Uses [proj-define-to-cflag] to expand a list of [define] keys, each +# one a separate argument, to CFLAGS-style -D... form then appends +# that to the current TEAISH_CFLAGS. +# +# It accepts these flags from proj-define-to-cflag: -quote, +# -zero-undef. It does _not_ support its -list flag. +# +# It accepts its non-flag argument(s) in 2 forms: (1) each arg is a +# single [define] key or (2) its one arg is a list of such keys. +# +# TODO: document teaish's well-defined (as it were) defines for this +# purpose. At a bare minimum: +# +# - TEAISH_NAME +# - TEAISH_PKGNAME +# - TEAISH_VERSION +# - TEAISH_LIBDIR_NAME +# - TEAISH_LOAD_PREFIX +# - TEAISH_URL +# +proc teaish-define-to-cflag {args} { + set flags {} + while {[string match -* [lindex $args 0]]} { + set arg [lindex $args 0] + switch -exact -- $arg { + -quote - + -zero-undef { + lappend flags $arg + set args [lassign $args -] + } + default break + } + } + if {1 == [llength $args]} { + set args [list {*}[lindex $args 0]] + } + #puts "***** flags=$flags args=$args" + teaish-cflags-add [proj-define-to-cflag {*}$flags {*}$args] +} + +# +# @teaish-cflags-for-tea ?...CFLAGS? +# +# Adds several -DPACKAGE_... CFLAGS using the extension's metadata, +# all as quoted strings. Those symbolic names are commonly used in +# TEA-based builds, and this function is intended to simplify porting +# of such builds. The -D... flags added are: +# +# -DPACKAGE_VERSION=... +# -DPACKAGE_NAME=... +# -DPACKAGE_URL=... +# -DPACKAGE_STRING=... +# +# Any arguments are passed-on as-is to teaish-cflags-add. +# +proc teaish-cflags-for-tea {args} { + set name $::teaish__PkgInfo(-name) + set version $::teaish__PkgInfo(-version) + set pstr [join [list $name $version]] + teaish-cflags-add \ + {*}$args \ + '-DPACKAGE_VERSION="$version"' \ + '-DPACKAGE_NAME="$name"' \ + '-DPACKAGE_STRING="$pstr"' \ + '-DPACKAGE_URL="[teaish-get -url]"' +} + +# +# @teaish-ldflags-add ?-p|-prepend? ?-define? ldflags... +# +# Equivalent to [proj-define-amend TEAISH_LDFLAGS {*}$args]. +# +# Typically, -lXYZ flags need to be in "reverse" order, with each -lY +# resolving symbols for -lX's to its left. This order is largely +# historical, and not relevant on all environments, but it is +# technically correct and still relevant on some environments. +# +# See: teaish-ldflags-prepend +# +proc teaish-ldflags-add {args} { + proj-define-amend TEAISH_LDFLAGS {*}$args +} + +# +# @teaish-ldflags-prepend args... +# +# Functionally equivalent to [teaish-ldflags-add -p {*}$args] +# +proc teaish-ldflags-prepend {args} { + teaish-ldflags-add -p {*}$args +} + +# +# @teaish-src-add ?-dist? ?-dir? src-files... +# +# Appends all non-empty $args to the project's list of C/C++ source or +# (in some cases) object files. +# +# If passed -dist then it also passes each filename, as-is, to +# [teaish-dist-add]. +# +# If passed -dir then each src-file has [teaish-get -dir] prepended to +# it before they're added to the list. As often as not, that will be +# the desired behavior so that out-of-tree builds can find the +# sources, but there are cases where it's not desired (e.g. when using +# a source file from outside of the extension's dir, or when adding +# object files (which are typically in the build tree)). +# +proc teaish-src-add {args} { + proj-parse-simple-flags args flags { + -dist 0 {expr 1} + -dir 0 {expr 1} + } + if {$flags(-dist)} { + teaish-dist-add {*}$args + } + if {$flags(-dir)} { + set xargs {} + foreach arg $args { + if {"" ne $arg} { + lappend xargs [file join $::teaish__Config(extension-dir) $arg] + } + } + set args $xargs + } + lappend ::teaish__Config(extension-src) {*}$args +} + +# +# @teaish-dist-add files-or-dirs... +# +# Adds the given files to the list of files to include with the "make +# dist" rules. +# +# This is a no-op when the current build is not in the extension's +# directory, as dist support is disabled in out-of-tree builds. +# +# It is not legal to call this until [teaish-get -dir] has been +# reliably set (via teaish__find_extension). +# +proc teaish-dist-add {args} { + if {$::teaish__Config(blddir-is-extdir)} { + # ^^^ reminder: we ignore $::teaish__Config(dist-enabled) here + # because the client might want to implement their own dist + # rules. + #proj-warn "**** args=$args" + lappend ::teaish__Config(dist-files) {*}$args + } +} + +# teaish-install-add files... +# Equivalent to [proj-define-apend TEAISH_INSTALL_FILES ...]. +#proc teaish-install-add {args} { +# proj-define-amend TEAISH_INSTALL_FILES {*}$args +#} + +# +# @teash-make-add args... +# +# Appends makefile code to the TEAISH_MAKEFILE_CODE define. Each +# arg may be any of: +# +# -tab: emit a literal tab +# -nl: emit a literal newline +# -nltab: short for -nl -tab +# -bnl: emit a backslash-escaped end-of-line +# -bnltab: short for -eol -tab +# +# Anything else is appended verbatim. This function adds no additional +# spacing between each argument nor between subsequent invocations. +# Generally speaking, a series of calls to this function need to +# be sure to end the series with a newline. +proc teaish-make-add {args} { + set out [get-define TEAISH_MAKEFILE_CODE ""] + foreach a $args { + switch -exact -- $a { + -bnl { set a " \\\n" } + -bnltab { set a " \\\n\t" } + -tab { set a "\t" } + -nl { set a "\n" } + -nltab { set a "\n\t" } + } + append out $a + } + define TEAISH_MAKEFILE_CODE $out +} + +# Internal helper to generate a clean/distclean rule name +proc teaish__cleanup_rule {{tgt clean}} { + set x [incr ::teaish__Config(teaish__cleanup_rule-counter-${tgt})] + return ${tgt}-_${x}_ +} + +# @teaish-make-obj ?flags? ?...args? +# +# Uses teaish-make-add to inject makefile rules for $objfile from +# $srcfile, which is assumed to be C code which uses libtcl. Unless +# -recipe is used (see below) it invokes the compiler using the +# makefile-defined $(CC.tcl) which, in the default Makefile.in +# template, includes any flags needed for building against the +# configured Tcl. +# +# This always terminates the resulting code with a newline. +# +# Any arguments after the 2nd may be flags described below or, if no +# -recipe is provided, flags for the compiler call. +# +# -obj obj-filename.o +# +# -src src-filename.c +# +# -recipe {...} +# Uses the trimmed value of {...} as the recipe, prefixing it with +# a single hard-tab character. +# +# -deps {...} +# List of extra files to list as dependencies of $o. +# +# -clean +# Generate cleanup rules as well. +proc teaish-make-obj {args} { + proj-parse-simple-flags args flags { + -clean 0 {expr 1} + -recipe => {} + -deps => {} + -obj => {} + -src => {} + } + #parray flags + if {"" eq $flags(-obj)} { + set args [lassign $args flags(-obj)] + if {"" eq $flags(-obj)} { + proj-error "Missing -obj flag." + } + } + foreach f {-deps -src} { + set flags($f) [string trim [string map {\n " "} $flags($f)]] + } + foreach f {-deps -src} { + set flags($f) [string trim $flags($f)] + } + #parray flags + #puts "-- args=$args" + teaish-make-add \ + "# [proj-scope 1] -> [proj-scope] $flags(-obj) $flags(-src)" -nl \ + "$flags(-obj): $flags(-src) $::teaish__Config(teaish.tcl)" + if {[info exists flags(-deps)]} { + teaish-make-add " " [join $flags(-deps)] + } + teaish-make-add -nltab + if {[info exists flags(-recipe)]} { + teaish-make-add [string trim $flags(-recipe)] -nl + } else { + teaish-make-add [join [list \$(CC.tcl) -c $flags(-src) {*}$args]] -nl + } + if {$flags(-clean)} { + set rule [teaish__cleanup_rule] + teaish-make-add \ + "clean: $rule\n$rule:\n\trm -f \"$flags(-obj)\"\n" + } +} + +# +# @teaish-make-clean ?-r? ?-dist? ...files|{...files} +# +# Adds makefile rules for cleaning up the given files via the "make +# clean" or (if -dist is used) "make distclean" makefile rules. The -r +# flag uses "rm -fr" instead of "rm -f", so be careful with that. +# +# The file names are taken literally as arguments to "rm", so they may +# be shell wildcards to be resolved at cleanup-time. To clean up whole +# directories, pass the -r flag. Each name gets quoted in +# double-quotes, so spaces in names should not be a problem (but +# double-quotes in names will be). +# +proc teaish-make-clean {args} { + if {1 == [llength $args]} { + set args [list {*}[lindex $args 0]] + } + + set tgt clean + set rmflags "-f" + proj-parse-simple-flags args flags { + -dist 0 { + set tgt distclean + } + -r 0 { + set rmflags "-fr" + } + } + set rule [teaish__cleanup_rule $tgt] + teaish-make-add "# [proj-scope 1] -> [proj-scope]: [join $args]\n" + teaish-make-add "${rule}:\n\trm ${rmflags}" + foreach a $args { + teaish-make-add " \"$a\"" + } + teaish-make-add "\n${tgt}: ${rule}\n" +} + +# +# @teaish-make-config-header filename +# +# Invokes autosetup's [make-config-header] and passes it $filename and +# a relatively generic list of options for controlling which defined +# symbols get exported. Clients which need more control over the +# exports can copy/paste/customize this. +# +# The exported file is then passed to [proj-touch] because, in +# practice, that's sometimes necessary to avoid build dependency +# issues. +# +proc teaish-make-config-header {filename} { + make-config-header $filename \ + -none {HAVE_CFLAG_* LDFLAGS_* SH_* TEAISH__* TEAISH_*_CODE} \ + -auto {SIZEOF_* HAVE_* TEAISH_* TCL_*} \ + -none * + proj-touch $filename; # help avoid frequent unnecessary auto-reconfig +} + +# +# @teaish-feature-cache-set $key value +# +# Sets a feature-check cache entry with the given key. +# See proj-cache-set for the key's semantics. $key should +# normally be 0. +# +proc teaish-feature-cache-set {key val} { + proj-cache-set -key $key -level 1 $val +} + +# +# @teaish-feature-cache-check key tgtVarName +# +# Checks for a feature-check cache entry with the given key. +# See proj-cache-set for the key's semantics. +# +# $key should also almost always be 0 but, due to a tclsh +# incompatibility in 1 OS, it cannot have a default value unless it's +# the second argument (but it should be the first one). +# +# If the feature-check cache has a matching entry then this function +# assigns its value to tgtVar and returns 1, else it assigns tgtVar to +# "" and returns 0. +# +# See proj-cache-check for $key's semantics. +# +proc teaish-feature-cache-check {key tgtVar} { + upvar $tgtVar tgt + proj-cache-check -key $key -level 1 tgt +} + +# +# @teaish-check-cached@ ?flags? msg script... +# +# A proxy for feature-test impls which handles caching of a feature +# flag check on per-function basis, using the calling scope's name as +# the cache key. +# +# It emits [msg-checking $msg]. If $msg is empty then it defaults to +# the name of the caller's scope. The -nomsg flag suppresses the +# message for non-cache-hit checks. At the end, it will [msg-result +# "ok"] [msg-result "no"] unless -nostatus is used, in which case the +# caller is responsible for emitting at least a newline when it's +# done. The -msg-0 and -msg-1 flags can be used to change the ok/no +# text. +# +# This function checks for a cache hit before running $script and +# caching the result. If no hit is found then $script is run in the +# calling scope and its result value is stored in the cache. This +# routine will intercept a 'return' from $script. +# +# $script may be a command and its arguments, as opposed to a single +# script block. +# +# Flags: +# +# -nostatus = do not emit "ok" or "no" at the end. This presumes +# that either $script will emit at least one newline before +# returning or the caller will account for it. Because of how this +# function is typically used, -nostatus is not honored when the +# response includes a cached result. +# +# -quiet = disable output from Autosetup's msg-checking and +# msg-result for the duration of the $script check. Note that when +# -quiet is in effect, Autosetup's user-notice can be used to queue +# up output to appear after the check is done. Also note that +# -quiet has no effect on _this_ function, only the $script part. +# +# -nomsg = do not emit $msg for initial check. Like -nostatus, this +# flag is not honored when the response includes a cached result +# because it would otherwise produce no output (which is confusing +# in this context). This is useful when a check runs several other +# verbose checks and they emit all the necessary info. +# +# -msg-0 and -msg-1 MSG = strings to show when the check has failed +# resp. passed. Defaults are "no" and "ok". The 0 and 1 refer to the +# result value from teaish-feature-cache-check. +# +# -key cachekey = set the cache context key. Only needs to be +# explicit when using this function multiple times from a single +# scope. See proj-cache-check and friends for details on the key +# name. Its default is the name of the scope which calls this +# function. +# +proc teaish-check-cached {args} { + proj-parse-simple-flags args flags { + -nostatus 0 {expr 1} + -quiet 0 {expr 1} + -key => 1 + -nomsg 0 {expr 1} + -msg-0 => no + -msg-1 => ok + } + set args [lassign $args msg] + set script [join $args] + if {"" eq $msg} { + set msg [proj-scope 1] + } + if {[teaish-feature-cache-check $flags(-key) check]} { + #if {0 == $flags(-nomsg)} { + msg-checking "${msg} ... (cached) " + #} + #if {!$flags(-nostatus)} { + msg-result $flags(-msg-[expr {0 != ${check}}]) + #} + return $check + } else { + if {0 == $flags(-nomsg)} { + msg-checking "${msg} ... " + } + if {$flags(-quiet)} { + incr ::autosetup(msg-quiet) + } + set code [catch {uplevel 1 $script} rc xopt] + if {$flags(-quiet)} { + incr ::autosetup(msg-quiet) -1 + } + #puts "***** cached-check got code=$code rc=$rc" + if {$code in {0 2}} { + teaish-feature-cache-set 1 $rc + if {!$flags(-nostatus)} { + msg-result $flags(-msg-[expr {0 != ${rc}}]) + } else { + #show-notices; # causes a phantom newline because we're in a + #msg-checking scope, so... + if {[info exists ::autosetup(notices)]} { + show-notices + } + } + } else { + #puts "**** code=$code rc=$rc xopt=$xopt" + teaish-feature-cache-set 1 0 + } + #puts "**** code=$code rc=$rc" + return {*}$xopt $rc + } +} + +# +# Internal helper for teaish__defs_format_: returns a JSON-ish quoted +# form of the given string-type values. +# +# If $asList is true then the return value is in {$value} form. If +# $asList is false it only performs the most basic of escaping and +# the input must not contain any control characters. +# +proc teaish__quote_str {asList value} { + if {$asList} { + return "{${value}}" + } + return \"[string map [list \\ \\\\ \" \\\"] $value]\" +} + +# +# Internal helper for teaish__defines_to_list. Expects to be passed +# a name and the variadic $args which are passed to +# teaish__defines_to_list.. If it finds a pattern match for the +# given $name in the various $args, it returns the type flag for that +# $name, e.g. "-str" or "-bare", else returns an empty string. +# +proc teaish__defs_type {name spec} { + foreach {type patterns} $spec { + foreach pattern $patterns { + if {[string match $pattern $name]} { + return $type + } + } + } + return "" +} + +# +# An internal impl detail. Requires a data type specifier, as used by +# Autosetup's [make-config-header], and a value. Returns the formatted +# value or the value $::teaish__Config(defs-skip) if the caller should +# skip emitting that value. +# +# In addition to -str, -auto, etc., as defined by make-config-header, +# it supports: +# +# -list {...} will cause non-integer values to be quoted in {...} +# instead of quotes. +# +# -autolist {...} works like -auto {...} except that it falls back to +# -list {...} type instead of -str {...} style for non-integers. +# +# -jsarray {...} emits the output in something which, for +# conservative inputs, will be a valid JSON array. It can only +# handle relatively simple values with no control characters in +# them. +# +set teaish__Config(defs-skip) "-teaish__defs_format sentinel" +proc teaish__defs_format {type value} { + switch -exact -- $type { + -bare { + # Just output the value unchanged + } + -none { + set value $::teaish__Config(defs-skip) + } + -str { + set value [teaish__quote_str 0 $value] + } + -auto { + # Automatically determine the type + if {![string is integer -strict $value]} { + set value [teaish__quote_str 0 $value] + } + } + -autolist { + if {![string is integer -strict $value]} { + set value [teaish__quote_str 1 $value] + } + } + -list { + set value [teaish__quote_str 1 $value] + } + -jsarray { + set ar {} + foreach v $value { + if {![string is integer -strict $v]} { + set v [teaish__quote_str 0 $v] + } + if {$::teaish__Config(defs-skip) ne $v} { + lappend ar $v + } + } + set value [concat \[ [join $ar {, }] \]] + } + "" { + # (Much later:) Why do we do this? + set value $::teaish__Config(defs-skip) + } + default { + proj-error \ + "Unknown [proj-scope] -type ($type) called from" \ + [proj-scope 1] + } + } + return $value +} + +# +# Returns Tcl code in the form of code which evaluates to a list of +# configure-time DEFINEs in the form {key val key2 val...}. It may +# misbehave for values which are not numeric or simple strings. Some +# defines are specifically filtered out of the result, either because +# their irrelevant to teaish or because they may be arbitrarily large +# (e.g. makefile content). +# +# The $args are explained in the docs for internal-use-only +# [teaish__defs_format]. The default mode is -autolist. +# +proc teaish__defines_to_list {args} { + set lines {} + lappend lines "\{" + set skipper $::teaish__Config(defs-skip) + set args [list \ + -none { + TEAISH__* + TEAISH_*_CODE + AM_* AS_* + } \ + {*}$args \ + -autolist *] + foreach d [lsort [dict keys [all-defines]]] { + set type [teaish__defs_type $d $args] + set value [teaish__defs_format $type [get-define $d]] + if {$skipper ne $value} { + lappend lines "$d $value" + } + } + lappend lines "\}" + tailcall join $lines "\n" +} + +# +# teaish__pragma ...flags +# +# Offers a way to tweak how teaish's core behaves in some cases, in +# particular those which require changing how the core looks for an +# extension and its files. +# +# Accepts the following flags. Those marked with [L] are safe to use +# during initial loading of tclish.tcl (recall that most teaish APIs +# cannot be used until [teaish-configure] is called). +# +# static-pkgIndex.tcl [L]: Tells teaish that ./pkgIndex.tcl is not +# a generated file, so it will not try to overwrite or delete +# it. Errors out if it does not find pkgIndex.tcl in the +# extension's dir. +# +# no-dist [L]: tells teaish to elide the 'make dist' recipe +# from the generated Makefile. +# +# no-dll [L]: tells teaish to elide the DLL-building recipe +# from the generated Makefile. +# +# no-vsatisfies-error [L]: tells teaish that failure to match the +# -vsatisfies value should simply "return" instead of "error". +# +# no-tester [L]: disables automatic generation of teaish.test.tcl +# even if a copy of _teaish.tester.tcl.in is found. +# +# no-full-dist [L]: changes the "make dist" rules to never include +# a copy of teaish itself. By default it will include itself only +# if the extension lives in the same directory as teaish. +# +# full-dist [L]: changes the "make dist" rules to always include +# a copy of teaish itself. +# +# Emits a warning message for unknown arguments. +# +proc teaish__pragma {args} { + foreach arg $args { + switch -exact -- $arg { + + static-pkgIndex.tcl { + if {$::teaish__Config(tm-policy)} { + proj-fatal -up "Cannot use pragma $arg together with -tm.tcl or -tm.tcl.in." + } + set tpi [file join $::teaish__Config(extension-dir) pkgIndex.tcl] + if {[file exists $tpi]} { + define TEAISH_PKGINDEX_TCL_IN "" + define TEAISH_PKGINDEX_TCL $tpi + set ::teaish__Config(pkgindex-policy) 0x20 + } else { + proj-error "pragma $arg: found no package-local pkgIndex.tcl\[.in]" + } + } + + no-dist { + set ::teaish__Config(dist-enabled) 0 + } + + no-install { + set ::teaish__Config(install-enabled) 0 + } + + full-dist { + set ::teaish__Config(dist-full-enabled) 1 + } + + no-full-dist { + set ::teaish__Config(dist-full-enabled) 0 + } + + no-dll { + set ::teaish__Config(dll-enabled) 0 + } + + no-vsatisfies-error { + set ::teaish__Config(vsatisfies-error) 0 + } + + no-tester { + define TEAISH_TESTER_TCL_IN "" + define TEAISH_TESTER_TCL "" + } + + default { + proj-error "Unknown flag: $arg" + } + } + } +} + +# +# @teaish-pkginfo-set ...flags +# +# The way to set up the initial package state. Used like: +# +# teaish-pkginfo-set -name foo -version 0.1.2 +# +# Or: +# +# teaish-pkginfo-set ?-vars|-subst? {-name foo -version 0.1.2} +# +# The latter may be easier to write for a multi-line invocation. +# +# For the second call form, passing the -vars flag tells it to perform +# a [subst] of (only) variables in the {...} part from the calling +# scope. The -subst flag will cause it to [subst] the {...} with +# command substitution as well (but no backslash substitution). When +# using -subst for string concatenation, e.g. with -libDir +# foo[get-version-number], be sure to wrap the value in braces: +# -libDir {foo[get-version-number]}. +# +# Each pkginfo flag corresponds to one piece of extension package +# info. Teaish provides usable default values for all of these flags, +# but at least the -name and -version should be set by clients. +# e.g. the default -name is the directory name the extension lives in, +# which may change (e.g. when building it from a "make dist" bundle). +# +# The flags: +# +# -name theName: The extension's name. It defaults to the name of the +# directory containing the extension. (In TEA this would be the +# PACKAGE_NAME, not to be confused with...) +# +# -name.pkg pkg-provide-name: The extension's name for purposes of +# Tcl_PkgProvide(), [package require], and friends. It defaults to +# the `-name`, and is normally the same, but some projects (like +# SQLite) have a different name here than they do in their +# historical TEA PACKAGE_NAME. +# +# -version version: The extension's package version. Defaults to +# 0.0.0. +# +# -libDir dirName: The base name of the directory into which this +# extension should be installed. It defaults to a concatenation of +# `-name.pkg` and `-version`. +# +# -loadPrefix prefix: For use as the second argument passed to +# Tcl's `load` command in the package-loading process. It defaults +# to title-cased `-name.pkg` because Tcl's `load` plugin system +# expects it in that form. +# +# -options {...}: If provided, it must be a list compatible with +# Autosetup's `options-add` function. These can also be set up via +# `teaish-options`. +# +# -vsatisfies {{...} ...}: Expects a list-of-lists of conditions +# for Tcl's `package vsatisfies` command: each list entry is a +# sub-list of `{PkgName Condition...}`. Teaish inserts those +# checks via its default pkgIndex.tcl.in and _teaish.tester.tcl.in +# templates to verify that the system's package dependencies meet +# these requirements. The default value is `{{Tcl 8.5-}}` (recall +# that it's a list-of-lists), as 8.5 is the minimum Tcl version +# teaish will run on, but some extensions may require newer +# versions or dependencies on other packages. As a special case, +# if `-vsatisfies` is given a single token, e.g. `8.6-`, then it +# is transformed into `{Tcl $thatToken}`, i.e. it checks the Tcl +# version which the package is being run with. If given multiple +# lists, each `package provides` check is run in the given +# order. Failure to meet a `vsatisfies` condition triggers an +# error. +# +# -url {...}: an optional URL for the extension. +# +# -pragmas {...} A list of infrequently-needed lower-level +# directives which can influence teaish, including: +# +# static-pkgIndex.tcl: tells teaish that the client manages their +# own pkgIndex.tcl, so that teaish won't try to overwrite it +# using a template. +# +# no-dist: tells teaish to elide the "make dist" recipe from the +# makefile so that the client can implement it. +# +# no-dll: tells teaish to elide the makefile rules which build +# the DLL, as well as any templated test script and pkgIndex.tcl +# references to them. The intent here is to (A) support +# client-defined build rules for the DLL and (B) eventually +# support script-only extensions. +# +# Unsupported flags or pragmas will trigger an error. +# +# Potential pothole: setting certain state, e.g. -version, after the +# initial call requires recalculating of some [define]s. Any such +# changes should be made as early as possible in teaish-configure so +# that any later use of those [define]s gets recorded properly (not +# with the old value). This is particularly relevant when it is not +# possible to determine the -version or -name until teaish-configure +# has been called, and it's updated dynamically from +# teaish-configure. Notably: +# +# - If -version or -name are updated, -libDir will almost certainly +# need to be explicitly set along with them. +# +# - If -name is updated, -loadPrefix probably needs to be as well. +# +proc teaish-pkginfo-set {args} { + set doVars 0 + set doCommands 0 + set xargs $args + set recalc {} + foreach arg $args { + switch -exact -- $arg { + -vars { + incr doVars + set xargs [lassign $xargs -] + } + -subst { + incr doVars + incr doCommands + set xargs [lassign $xargs -] + } + default { + break + } + } + } + set args $xargs + unset xargs + if {1 == [llength $args] && [llength [lindex $args 0]] > 1} { + # Transform a single {...} arg into the canonical call form + set a [list {*}[lindex $args 0]] + if {$doVars || $doCommands} { + set sflags -nobackslashes + if {!$doCommands} { + lappend sflags -nocommands + } + set a [uplevel 1 [list subst {*}$sflags $a]] + } + set args $a + } + set sentinel "" + set flagDefs [list] + foreach {f d} $::teaish__Config(pkginfo-f2d) { + lappend flagDefs $f => $sentinel + } + proj-parse-simple-flags args flags $flagDefs + if {[llength $args]} { + proj-error -up "Too many (or unknown) arguments to [proj-scope]: $args" + } + foreach {f d} $::teaish__Config(pkginfo-f2d) { + if {$sentinel eq [set v $flags($f)]} continue + switch -exact -- $f { + + -options { + proj-assert {"" eq $d} + options-add $v + } + + -pragmas { + teaish__pragma {*}$v + } + + -vsatisfies { + if {1 == [llength $v] && 1 == [llength [lindex $v 0]]} { + # Transform X to {Tcl $X} + set v [list [join [list Tcl $v]]] + } + define $d $v + } + + -pkgInit.tcl - + -pkgInit.tcl.in { + if {0x22 & $::teaish__Config(pkginit-policy)} { + proj-fatal "Cannot use -pkgInit.tcl(.in) more than once." + } + set x [file join $::teaish__Config(extension-dir) $v] + set tTail [file tail $v] + if {"-pkgInit.tcl.in" eq $f} { + # Generate pkginit file X from X.in + set pI 0x02 + set tIn $x + set tOut [file rootname $tTail] + set other -pkgInit.tcl + } else { + # Static pkginit file X + set pI 0x20 + set tIn "" + set tOut $x + set other -pkgInit.tcl.in + } + set ::teaish__Config(pkginit-policy) $pI + set ::teaish__PkgInfo($other) {} + define TEAISH_PKGINIT_TCL_IN $tIn + define TEAISH_PKGINIT_TCL $tOut + define TEAISH_PKGINIT_TCL_TAIL $tTail + teaish-dist-add $v + set v $x + } + + -src { + set d $::teaish__Config(extension-dir) + foreach f $v { + lappend ::teaish__Config(dist-files) $f + lappend ::teaish__Config(extension-src) $d/$f + lappend ::teaish__PkgInfo(-src) $f + # ^^^ so that default-value initialization in + # teaish-configure-core recognizes that it's been set. + } + } + + -tm.tcl - + -tm.tcl.in { + if {0x30 & $::teaish__Config(pkgindex-policy)} { + proj-fatal "Cannot use $f together with a pkgIndex.tcl." + } elseif {$::teaish__Config(tm-policy)} { + proj-fatal "Cannot use -tm.tcl(.in) more than once." + } + set x [file join $::teaish__Config(extension-dir) $v] + if {"-tm.tcl.in" eq $f} { + # Generate tm file X from X.in + set pT 0x02 + set pI 0x100 + set tIn $x + set tOut [file rootname [file tail $v]] + set other -tm.tcl + } else { + # Static tm file X + set pT 0x20 + set pI 0x200 + set tIn "" + set tOut $x + set other -tm.tcl.in + } + set ::teaish__Config(pkgindex-policy) $pI + set ::teaish__Config(tm-policy) $pT + set ::teaish__PkgInfo($other) {} + define TEAISH_TM_TCL_IN $tIn + define TEAISH_TM_TCL $tOut + define TEAISH_PKGINDEX_TCL "" + define TEAISH_PKGINDEX_TCL_IN "" + define TEAISH_PKGINDEX_TCL_TAIL "" + teaish-dist-add $v + teaish__pragma no-dll + set v $x + } + + default { + proj-assert {"" ne $d} + define $d $v + } + } + set ::teaish__PkgInfo($f) $v + if {$f in {-name -version -libDir -loadPrefix}} { + lappend recalc $f + } + } + if {"" ne $recalc} { + teaish__define_pkginfo_derived $recalc + } +} + +# +# @teaish-pkginfo-get ?arg? +# +# If passed no arguments, it returns the extension config info in the +# same form accepted by teaish-pkginfo-set. +# +# If passed one -flagname arg then it returns the value of that config +# option. +# +# Else it treats arg as the name of caller-scoped variable to +# which this function assigns an array containing the configuration +# state of this extension, in the same structure accepted by +# teaish-pkginfo-set. In this case it returns an empty string. +# +proc teaish-pkginfo-get {args} { + set cases {} + set argc [llength $args] + set rv {} + switch -exact $argc { + 0 { + # Return a list of (-flag value) pairs + lappend cases default {{ + if {[info exists ::teaish__PkgInfo($flag)]} { + lappend rv $flag $::teaish__PkgInfo($flag) + } else { + lappend rv $flag [get-define $defName] + } + }} + } + + 1 { + set arg $args + if {[string match -* $arg]} { + # Return the corresponding -flag's value + lappend cases $arg {{ + if {[info exists ::teaish__PkgInfo($flag)]} { + return $::teaish__PkgInfo($flag) + } else { + return [get-define $defName] + } + }} + } else { + # Populate target with an array of (-flag value). + upvar $arg tgt + array set tgt {} + lappend cases default {{ + if {[info exists ::teaish__PkgInfo($flag)]} { + set tgt($flag) $::teaish__PkgInfo($flag) + } else { + set tgt($flag) [get-define $defName] + } + }} + } + } + + default { + proj-error "invalid arg count from [proj-scope 1]" + } + } + + foreach {flag defName} $::teaish__Config(pkginfo-f2d) { + switch -exact -- $flag [join $cases] + } + if {0 == $argc} { return $rv } +} + +# (Re)set some defines based on pkginfo state. $flags is the list of +# pkginfo -flags which triggered this, or "*" for the initial call. +proc teaish__define_pkginfo_derived {flags} { + set all [expr {{*} in $flags}] + if {$all || "-version" in $flags || "-name" in $flags} { + set name $::teaish__PkgInfo(-name) ; # _not_ -name.pkg + if {[info exists ::teaish__PkgInfo(-version)]} { + set pkgver $::teaish__PkgInfo(-version) + set libname "lib" + if {[string match *-cygwin [get-define host]]} { + set libname cyg + } + define TEAISH_DLL8_BASENAME $libname$name$pkgver + define TEAISH_DLL9_BASENAME ${libname}tcl9$name$pkgver + set ext [get-define TARGET_DLLEXT] + define TEAISH_DLL8 [get-define TEAISH_DLL8_BASENAME]$ext + define TEAISH_DLL9 [get-define TEAISH_DLL9_BASENAME]$ext + } + } + if {$all || "-libDir" in $flags} { + if {[info exists ::teaish__PkgInfo(-libDir)]} { + define TCLLIBDIR \ + [file dirname [get-define TCLLIBDIR]]/$::teaish__PkgInfo(-libDir) + } + } +} + +# +# @teaish-checks-queue -pre|-post args... +# +# Queues one or more arbitrary "feature test" functions to be run when +# teaish-checks-run is called. $flag must be one of -pre or -post to +# specify whether the tests should be run before or after +# teaish-configure is run. Each additional arg is the name of a +# feature-test proc. +# +proc teaish-checks-queue {flag args} { + if {$flag ni {-pre -post}} { + proj-error "illegal flag: $flag" + } + lappend ::teaish__Config(queued-checks${flag}) {*}$args +} + +# +# @teaish-checks-run -pre|-post +# +# Runs all feature checks queued using teaish-checks-queue +# then cleares the queue. +# +proc teaish-checks-run {flag} { + if {$flag ni {-pre -post}} { + proj-error "illegal flag: $flag" + } + #puts "*** running $flag: $::teaish__Config(queued-checks${flag})" + set foo 0 + foreach f $::teaish__Config(queued-checks${flag}) { + if {![teaish-feature-cache-check $f foo]} { + set v [$f] + teaish-feature-cache-set $f $v + } + } + set ::teaish__Config(queued-checks${flag}) {} +} + +# +# A general-purpose getter for various teaish state. Requires one +# flag, which determines its result value. Flags marked with [L] below +# are safe for using at load-time, before teaish-pkginfo-set is called +# +# -dir [L]: returns the extension's directory, which may differ from +# the teaish core dir or the build dir. +# +# -teaish-home [L]: the "home" dir of teaish itself, which may +# differ from the extension dir or build dir. +# +# -build-dir [L]: the build directory (typically the current working +# -dir). +# +# Any of the teaish-pkginfo-get/get flags: returns the same as +# teaish-pkginfo-get. Not safe for use until teaish-pkginfo-set has +# been called. +# +# Triggers an error if passed an unknown flag. +# +proc teaish-get {flag} { + #-teaish.tcl {return $::teaish__Config(teaish.tcl)} + switch -exact -- $flag { + -dir { + return $::teaish__Config(extension-dir) + } + -teaish-home { + return $::autosetup(srcdir) + } + -build-dir { + return $::autosetup(builddir) + } + default { + if {[info exists ::teaish__PkgInfo($flag)]} { + return $::teaish__PkgInfo($flag) + } + } + } + proj-error "Unhandled flag: $flag" +} + +# +# Handles --teaish-create-extension=TARGET-DIR +# +proc teaish__create_extension {dir} { + set force [opt-bool teaish-force] + if {"" eq $dir} { + proj-error "--teaish-create-extension=X requires a directory name." + } + file mkdir $dir/generic + set cwd [pwd] + #set dir [file-normalize [file join $cwd $dir]] + teaish__verbose 1 msg-result "Created dir $dir" + cd $dir + if {!$force} { + # Ensure that we don't blindly overwrite anything + foreach f { + generic/teaish.c + teaish.tcl + teaish.make.in + teaish.test.tcl + } { + if {[file exists $f]} { + error "Cowardly refusing to overwrite $dir/$f. Use --teaish-force to overwrite." + } + } + } + + set name [file tail $dir] + set pkgName $name + set version 0.0.1 + set loadPrefix [string totitle $pkgName] + set content {teaish-pkginfo-set } + #puts "0 content=$content" + if {[opt-str teaish-extension-pkginfo epi]} { + set epi [string trim $epi] + if {[string match "*\n*" $epi]} { + set epi "{$epi}" + } elseif {![string match "{*}" $epi]} { + append content "\{" $epi "\}" + } else { + append content $epi + } + #puts "2 content=$content\nepi=$epi" + } else { + append content [subst -nocommands -nobackslashes {{ + -name ${name} + -name.pkg ${pkgName} + -name.dist ${pkgName} + -version ${version} + -loadPrefix $loadPrefix + -libDir ${name}${version} + -vsatisfies {{Tcl 8.5-}} + -url {} + -options {} + -pragmas {full-dist} + }}] + #puts "3 content=$content" + } + #puts "1 content=$content" + append content "\n" { +#proc teaish-options {} { + # Return a list and/or use \[options-add\] to add new + # configure flags. This is called before teaish's + # bootstrapping is finished, so only teaish-* + # APIs which are explicitly noted as being safe + # early on may be used here. Any autosetup-related + # APIs may be used here. + # + # Return an empty string if there are no options to + # add or if they are added using \[options-add\]. + # + # If there are no options to add, this proc need + # not be defined. +#} + +# Called by teaish once bootstrapping is complete. +# This function is responsible for the client-specific +# parts of the configuration process. +proc teaish-configure {} { + teaish-src-add -dir -dist generic/teaish.c + teaish-define-to-cflag -quote TEAISH_PKGNAME TEAISH_VERSION + + # TODO: your code goes here.. +} +}; # $content + proj-file-write teaish.tcl $content + teaish__verbose 1 msg-result "Created teaish.tcl" + + set content "# Teaish test script. +# When this tcl script is invoked via 'make test' it will have loaded +# the package, run any teaish.pkginit.tcl code, and loaded +# autosetup/teaish/tester.tcl. +" + proj-file-write teaish.test.tcl $content + teaish__verbose 1 msg-result "Created teaish.test.tcl" + + set content [subst -nocommands -nobackslashes { +#include +static int +${loadPrefix}_Cmd(ClientData cdata, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]){ + Tcl_SetObjResult(interp, Tcl_NewStringObj("this is the ${name} extension", -1)); + return TCL_OK; +} + +extern int DLLEXPORT ${loadPrefix}_Init(Tcl_Interp *interp){ + if (Tcl_InitStubs(interp, TCL_VERSION, 0) == NULL) { + return TCL_ERROR; + } + if (Tcl_PkgProvide(interp, TEAISH_PKGNAME, TEAISH_VERSION) == TCL_ERROR) { + return TCL_ERROR; + } + Tcl_CreateObjCommand(interp, TEAISH_PKGNAME, ${loadPrefix}_Cmd, NULL, NULL); + return TCL_OK; +} +}] + proj-file-write generic/teaish.c $content + teaish__verbose 1 msg-result "Created generic/teaish.c" + + set content "# teaish makefile for the ${name} extension +# tx.src = \$(tx.dir)/generic/teaish.c +# tx.LDFLAGS = +# tx.CFLAGS = +" + proj-file-write teaish.make.in $content + teaish__verbose 1 msg-result "Created teaish.make.in" + + msg-result "Created new extension \[$dir\]." + + cd $cwd + set ::teaish__Config(install-ext-dir) $dir +} + +# +# Internal helper for teaish__install +# +proc teaish__install_file {f destDir force} { + set dest $destDir/[file tail $f] + if {[file isdirectory $f]} { + file mkdir $dest + } elseif {!$force && [file exists $dest]} { + array set st1 [file stat $f] + array set st2 [file stat $dest] + if {($st1(mtime) == $st2(mtime)) + && ($st1(size) == $st2(size))} { + if {[file tail $f] in { + pkgIndex.tcl.in + _teaish.tester.tcl.in + }} { + # Assume they're the same. In the scope of the "make dist" + # rules, this happens legitimately when an extension with a + # copy of teaish installed in the same dir assumes that the + # pkgIndex.tcl.in and _teaish.tester.tcl.in belong to the + # extension, whereas teaish believes they belong to teaish. + # So we end up with dupes of those. + return + } + } + proj-error -up "Cowardly refusing to overwrite \[$dest\]." \ + "Use --teaish-force to enable overwriting." + } else { + # file copy -force $f $destDir; # loses +x bit + # + # JimTcl doesn't have [file attribute], so we can't use that here + # (in the context of an autosetup configure script). + exec cp -p $f $dest + } +} + +# +# Installs a copy of teaish, with autosetup, to $dDest, which defaults +# to the --teaish-install=X or --teash-create-extension=X dir. Won't +# overwrite files unless --teaish-force is used. +# +proc teaish__install {{dDest ""}} { + if {$dDest in {auto ""}} { + set dDest [opt-val teaish-install] + if {$dDest in {auto ""}} { + if {[info exists ::teaish__Config(install-ext-dir)]} { + set dDest $::teaish__Config(install-ext-dir) + } + } + } + set force [opt-bool teaish-force] + if {$dDest in {auto ""}} { + proj-error "Cannot determine installation directory." + } elseif {!$force && [file exists $dDest/auto.def]} { + proj-error \ + "Target dir looks like it already contains teaish and/or autosetup: $dDest" \ + "\nUse --teaish-force to overwrite it." + } + + set dSrc $::autosetup(srcdir) + set dAS $::autosetup(libdir) + set dAST $::teaish__Config(core-dir) + set dASTF $dAST/feature + teaish__verbose 1 msg-result "Installing teaish to \[$dDest\]..." + if {$::teaish__Config(verbose)>1} { + msg-result "dSrc = $dSrc" + msg-result "dAS = $dAS" + msg-result "dAST = $dAST" + msg-result "dASTF = $dASTF" + msg-result "dDest = $dDest" + } + + # Dest subdirs... + set ddAS $dDest/autosetup + set ddAST $ddAS/teaish + set ddASTF $ddAST/feature + foreach {srcDir destDir} [list \ + $dAS $ddAS \ + $dAST $ddAST \ + $dASTF $ddASTF \ + ] { + teaish__verbose 1 msg-result "Copying files to $destDir..." + file mkdir $destDir + foreach f [glob -nocomplain -directory $srcDir *] { + if {[string match {*~} $f] || [string match "#*#" [file tail $f]]} { + # Editor-generated backups and emacs lock files + continue + } + teaish__verbose 2 msg-result "\t$f" + teaish__install_file $f $destDir $force + } + } + teaish__verbose 1 msg-result "Copying files to $dDest..." + foreach f { + auto.def configure Makefile.in pkgIndex.tcl.in + _teaish.tester.tcl.in + } { + teaish__verbose 2 msg-result "\t$f" + teaish__install_file $dSrc/$f $dDest $force + } + set ::teaish__Config(install-self-dir) $dDest + msg-result "Teaish $::teaish__Config(version) installed in \[$dDest\]." +} diff --git a/autosetup/teaish/feature.tcl b/autosetup/teaish/feature.tcl new file mode 100644 index 0000000000..6c927d1a77 --- /dev/null +++ b/autosetup/teaish/feature.tcl @@ -0,0 +1,214 @@ +######################################################################## +# 2025 April 7 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# * May you do good and not evil. +# * May you find forgiveness for yourself and forgive others. +# * May you share freely, never taking more than you give. +# +######################################################################## +# ----- @module feature-tests.tcl ----- +# @section TEA-ish collection of feature tests. +# +# Functions in this file with a prefix of teaish__ are +# private/internal APIs. Those with a prefix of teaish- are +# public APIs. + + +# @teaish-check-libz +# +# Checks for zlib.h and the function deflate in libz. If found, +# prepends -lz to the extension's ldflags and returns 1, else returns +# 0. It also defines LDFLAGS_LIBZ to the libs flag. +# +proc teaish-check-libz {} { + teaish-check-cached "Checking for libz" { + set rc 0 + if {[msg-quiet cc-check-includes zlib.h] && [msg-quiet proj-check-function-in-lib deflate z]} { + teaish-ldflags-prepend [define LDFLAGS_LIBZ [get-define lib_deflate]] + undefine lib_deflate + incr rc + } + expr $rc + } +} + +# @teaish-check-librt ?funclist? +# +# Checks whether -lrt is needed for any of the given functions. If +# so, appends -lrt via [teaish-ldflags-prepend] and returns 1, else +# returns 0. It also defines LDFLAGS_LIBRT to the libs flag or an +# empty string. +# +# Some systems (ex: SunOS) require -lrt in order to use nanosleep. +# +proc teaish-check-librt {{funclist {fdatasync nanosleep}}} { + teaish-check-cached -nostatus "Checking whether ($funclist) need librt" { + define LDFLAGS_LIBRT "" + foreach func $funclist { + if {[msg-quiet proj-check-function-in-lib $func rt]} { + set ldrt [get-define lib_${func}] + undefine lib_${func} + if {"" ne $ldrt} { + teaish-ldflags-prepend -r [define LDFLAGS_LIBRT $ldrt] + msg-result $ldrt + return 1 + } else { + msg-result "no lib needed" + return 1 + } + } + } + msg-result "not found" + return 0 + } +} + +# @teaish-check-stdint +# +# A thin proxy for [cc-with] which checks for and the +# various fixed-size int types it declares. It defines HAVE_STDINT_T +# to 0 or 1 and (if it's 1) defines HAVE_XYZ_T for each XYZ int type +# to 0 or 1, depending on whether its available. +proc teaish-check-stdint {} { + teaish-check-cached "Checking for stdint.h" { + msg-quiet cc-with {-includes stdint.h} \ + {cc-check-types int8_t int16_t int32_t int64_t intptr_t \ + uint8_t uint16_t uint32_t uint64_t uintptr_t} + } +} + +# @teaish-is-mingw +# +# Returns 1 if building for mingw, else 0. +proc teaish-is-mingw {} { + return [expr { + [string match *mingw* [get-define host]] && + ![file exists /dev/null] + }] +} + +# @teaish-check-libdl +# +# Checks for whether dlopen() can be found and whether it requires +# -ldl for linking. If found, returns 1, defines LDFLAGS_DLOPEN to the +# linker flags (if any), and passes those flags to +# teaish-ldflags-prepend. It unconditionally defines HAVE_DLOPEN to 0 +# or 1 (the its return result value). +proc teaish-check-dlopen {} { + teaish-check-cached -nostatus "Checking for dlopen()" { + set rc 0 + set lfl "" + if {[cc-with {-includes dlfcn.h} { + cctest -link 1 -declare "extern char* dlerror(void);" -code "dlerror();"}]} { + msg-result "-ldl not needed" + incr rc + } elseif {[cc-check-includes dlfcn.h]} { + incr rc + if {[cc-check-function-in-lib dlopen dl]} { + set lfl [get-define lib_dlopen] + undefine lib_dlopen + msg-result " dlopen() needs $lfl" + } else { + msg-result " - dlopen() not found in libdl. Assuming dlopen() is built-in." + } + } else { + msg-result "not found" + } + teaish-ldflags-prepend [define LDFLAGS_DLOPEN $lfl] + define HAVE_DLOPEN $rc + } +} + +# +# @teaish-check-libmath +# +# Handles the --enable-math flag. Returns 1 if found, else 0. +# If found, it prepends -lm (if needed) to the linker flags. +proc teaish-check-libmath {} { + teaish-check-cached "Checking for libc math library" { + set lfl "" + set rc 0 + if {[msg-quiet proj-check-function-in-lib ceil m]} { + incr rc + set lfl [get-define lib_ceil] + undefine lib_ceil + teaish-ldflags-prepend $lfl + msg-checking "$lfl " + } + define LDFLAGS_LIBMATH $lfl + expr $rc + } +} + +# @teaish-import-features ?-flags? feature-names... +# +# For each $name in feature-names... it invokes: +# +# use teaish/feature/$name +# +# to load TEAISH_AUTOSETUP_DIR/feature/$name.tcl +# +# By default, if a proc named teaish-check-${name}-options is defined +# after sourcing a file, it is called and its result is passed to +# proj-append-options. This can be suppressed with the -no-options +# flag. +# +# Flags: +# +# -no-options: disables the automatic running of +# teaish-check-NAME-options, +# +# -run: if the function teaish-check-NAME exists after importing +# then it is called. This flag must not be used when calling this +# function from teaish-options. This trumps both -pre and -post. +# +# -pre: if the function teaish-check-NAME exists after importing +# then it is passed to [teaish-checks-queue -pre]. +# +# -post: works like -pre but instead uses[teaish-checks-queue -post]. +proc teaish-import-features {args} { + set pk "" + set doOpt 1 + proj-parse-simple-flags args flags { + -no-options 0 {set doOpt 0} + -run 0 {expr 1} + -pre 0 {set pk -pre} + -post 0 {set pk -post} + } + # + # TODO: never import the same module more than once. The "use" + # command is smart enough to not do that but we would need to + # remember whether or not any teaish-check-${arg}* procs have been + # called before, and skip them. + # + if {$flags(-run) && "" ne $pk} { + proj-error "Cannot use both -run and $pk" \ + " (called from [proj-scope 1])" + } + + foreach arg $args { + uplevel "use teaish/feature/$arg" + if {$doOpt} { + set n "teaish-check-${arg}-options" + if {[llength [info proc $n]] > 0} { + if {"" ne [set x [$n]]} { + options-add $x + } + } + } + if {$flags(-run)} { + set n "teaish-check-${arg}" + if {[llength [info proc $n]] > 0} { + uplevel 1 $n + } + } elseif {"" ne $pk} { + set n "teaish-check-${arg}" + if {[llength [info proc $n]] > 0} { + teaish-checks-queue {*}$pk $n + } + } + } +} diff --git a/autosetup/teaish/tester.tcl b/autosetup/teaish/tester.tcl new file mode 100644 index 0000000000..a25b366e8d --- /dev/null +++ b/autosetup/teaish/tester.tcl @@ -0,0 +1,293 @@ +######################################################################## +# 2025 April 5 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# * May you do good and not evil. +# * May you find forgiveness for yourself and forgive others. +# * May you share freely, never taking more than you give. +# +######################################################################## +# +# Helper routines for running tests on teaish extensions +# +######################################################################## +# ----- @module teaish/tester.tcl ----- +# +# @section TEA-ish Testing APIs. +# +# Though these are part of the autosup dir hierarchy, they are not +# intended to be run from autosetup code. Rather, they're for use +# with/via teaish.tester.tcl and target canonical Tcl only, not JimTcl +# (which the autosetup pieces do target). + +# +# @test-current-scope ?lvl? +# +# Returns the name of the _calling_ proc from ($lvl + 1) levels up the +# call stack (where the caller's level will be 1 up from _this_ +# call). If $lvl would resolve to global scope "global scope" is +# returned and if it would be negative then a string indicating such +# is returned (as opposed to throwing an error). +# +proc test-current-scope {{lvl 0}} { + #uplevel [expr {$lvl + 1}] {lindex [info level 0] 0} + set ilvl [info level] + set offset [expr {$ilvl - $lvl - 1}] + if { $offset < 0} { + return "invalid scope ($offset)" + } elseif { $offset == 0} { + return "global scope" + } else { + return [lindex [info level $offset] 0] + } +} + +# @test-msg +# +# Emits all arugments to stdout. +# +proc test-msg {args} { + puts "$args" +} + +# @test-warn +# +# Emits all arugments to stderr. +# +proc test-warn {args} { + puts stderr "WARNING: $args" +} + +# +# @test-error msg +# +# Triggers a test-failed error with a string describing the calling +# scope and the provided message. +# +proc test-fail {args} { + #puts stderr "ERROR: \[[test-current-scope 1]]: $msg" + #exit 1 + error "FAIL: \[[test-current-scope 1]]: $args" +} + +array set ::test__Counters {} +array set ::test__Config { + verbose-assert 0 verbose-affirm 0 +} + +# Internal impl for affirm and assert. +# +# $args = ?-v? script {msg-on-fail ""} +proc test__affert {failMode args} { + if {$failMode} { + set what assert + } else { + set what affirm + } + set verbose $::test__Config(verbose-$what) + if {"-v" eq [lindex $args 0]} { + lassign $args - script msg + if {1 == [llength $args]} { + # If -v is the only arg, toggle default verbose mode + set ::test__Config(verbose-$what) [expr {!$::test__Config(verbose-$what)}] + return + } + incr verbose + } else { + lassign $args script msg + } + incr ::test__Counters($what) + if {![uplevel 1 expr [list $script]]} { + if {"" eq $msg} { + set msg $script + } + set txt [join [list $what # $::test__Counters($what) "failed:" $msg]] + if {$failMode} { + puts stderr $txt + exit 1 + } else { + error $txt + } + } elseif {$verbose} { + puts stderr [join [list $what # $::test__Counters($what) "passed:" $script]] + } +} + +# +# @affirm ?-v? script ?msg? +# +# Works like a conventional assert method does, but reports failures +# using [error] instead of [exit]. If -v is used, it reports passing +# assertions to stderr. $script is evaluated in the caller's scope as +# an argument to [expr]. +# +proc affirm {args} { + tailcall test__affert 0 {*}$args +} + +# +# @assert ?-v? script ?msg? +# +# Works like [affirm] but exits on error. +# +proc assert {args} { + tailcall test__affert 1 {*}$args +} + +# +# @assert-matches ?-e? pattern ?-e? rhs ?msg? +# +# Equivalent to assert {[string match $pattern $rhs]} except that +# if either of those are prefixed with an -e flag, they are eval'd +# and their results are used. +# +proc assert-matches {args} { + set evalLhs 0 + set evalRhs 0 + if {"-e" eq [lindex $args 0]} { + incr evalLhs + set args [lassign $args -] + } + set args [lassign $args pattern] + if {"-e" eq [lindex $args 0]} { + incr evalRhs + set args [lassign $args -] + } + set args [lassign $args rhs msg] + + if {$evalLhs} { + set pattern [uplevel 1 $pattern] + } + if {$evalRhs} { + set rhs [uplevel 1 $rhs] + } + #puts "***pattern=$pattern\n***rhs=$rhs" + tailcall test__affert 1 \ + [join [list \[ string match [list $pattern] [list $rhs] \]]] $msg + # why does this not work? [list \[ string match [list $pattern] [list $rhs] \]] $msg + # "\[string match [list $pattern] [list $rhs]\]" +} + +# +# @test-assert testId script ?msg? +# +# Works like [assert] but emits $testId to stdout first. +# +proc test-assert {testId script {msg ""}} { + puts "test $testId" + tailcall test__affert 1 $script $msg +} + +# +# @test-expect testId script result +# +# Runs $script in the calling scope and compares its result to +# $result, minus any leading or trailing whitespace. If they differ, +# it triggers an [assert]. +# +proc test-expect {testId script result} { + puts "test $testId" + set x [string trim [uplevel 1 $script]] + set result [string trim $result] + tailcall test__affert 0 [list "{$x}" eq "{$result}"] \ + "\nEXPECTED: <<$result>>\nGOT: <<$x>>" +} + +# +# @test-catch cmd ?...args? +# +# Runs [cmd ...args], repressing any exception except to possibly log +# the failure. Returns 1 if it caught anything, 0 if it didn't. +# +proc test-catch {cmd args} { + if {[catch { + uplevel 1 $cmd {*}$args + } rc xopts]} { + puts "[test-current-scope] ignoring failure of: $cmd [lindex $args 0]: $rc" + return 1 + } + return 0 +} + +# +# @test-catch-matching pattern (script|cmd args...) +# +# Works like test-catch, but it expects its argument(s) to to throw an +# error matching the given string (checked with [string match]). If +# they do not throw, or the error does not match $pattern, this +# function throws, else it returns 1. +# +# If there is no second argument, the $cmd is assumed to be a script, +# and will be eval'd in the caller's scope. +# +# TODO: add -glob and -regex flags to control matching flavor. +# +proc test-catch-matching {pattern cmd args} { + if {[catch { + #puts "**** catch-matching cmd=$cmd args=$args" + if {0 == [llength $args]} { + uplevel 1 $cmd {*}$args + } else { + $cmd {*}$args + } + } rc xopts]} { + if {[string match $pattern $rc]} { + return 1 + } else { + error "[test-current-scope] exception does not match {$pattern}: {$rc}" + } + } + error "[test-current-scope] expecting to see an error matching {$pattern}" +} + +if {![array exists ::teaish__BuildFlags]} { + array set ::teaish__BuildFlags {} +} + +# +# @teaish-build-flag3 flag tgtVar ?dflt? +# +# If the current build has the configure-time flag named $flag set +# then tgtVar is assigned its value and 1 is returned, else tgtVal is +# assigned $dflt and 0 is returned. +# +# Caveat #1: only valid when called in the context of teaish's default +# "make test" recipe, e.g. from teaish.test.tcl. It is not valid from +# a teaish.tcl configure script because (A) the state it relies on +# doesn't fully exist at that point and (B) that level of the API has +# more direct access to the build state. This function requires that +# an external script have populated its internal state, which is +# normally handled via teaish.tester.tcl.in. +# +# Caveat #2: defines in the style of HAVE_FEATURENAME with a value of +# 0 are, by long-standing configure script conventions, treated as +# _undefined_ here. +# +proc teaish-build-flag3 {flag tgtVar {dflt ""}} { + upvar $tgtVar tgt + if {[info exists ::teaish__BuildFlags($flag)]} { + set tgt $::teaish__BuildFlags($flag) + return 1; + } elseif {0==[array size ::teaish__BuildFlags]} { + test-warn \ + "\[[test-current-scope]] was called from " \ + "[test-current-scope 1] without the build flags imported." + } + set tgt $dflt + return 0 +} + +# +# @teaish-build-flag flag ?dflt? +# +# Convenience form of teaish-build-flag3 which returns the +# configure-time-defined value of $flag or "" if it's not defined (or +# if it's an empty string). +# +proc teaish-build-flag {flag {dflt ""}} { + set tgt "" + teaish-build-flag3 $flag tgt $dflt + return $tgt +} diff --git a/config.guess b/config.guess deleted file mode 100644 index 34093cc6bb..0000000000 --- a/config.guess +++ /dev/null @@ -1,1535 +0,0 @@ -#! /bin/sh -# Attempt to guess a canonical system name. -# Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, -# 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, -# Inc. - -timestamp='2007-07-22' - -# This file is free software; you can redistribute it and/or modify it -# under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA -# 02110-1301, USA. -# -# As a special exception to the GNU General Public License, if you -# distribute this file as part of a program that contains a -# configuration script generated by Autoconf, you may include it under -# the same distribution terms that you use for the rest of that program. - - -# Originally written by Per Bothner . -# Please send patches to . Submit a context -# diff and a properly formatted ChangeLog entry. -# -# This script attempts to guess a canonical system name similar to -# config.sub. If it succeeds, it prints the system name on stdout, and -# exits with 0. Otherwise, it exits with 1. -# -# The plan is that this can be called by configure scripts if you -# don't specify an explicit build system type. - -me=`echo "$0" | sed -e 's,.*/,,'` - -usage="\ -Usage: $0 [OPTION] - -Output the configuration name of the system \`$me' is run on. - -Operation modes: - -h, --help print this help, then exit - -t, --time-stamp print date of last modification, then exit - -v, --version print version number, then exit - -Report bugs and patches to ." - -version="\ -GNU config.guess ($timestamp) - -Originally written by Per Bothner. -Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 -Free Software Foundation, Inc. - -This is free software; see the source for copying conditions. There is NO -warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." - -help=" -Try \`$me --help' for more information." - -# Parse command line -while test $# -gt 0 ; do - case $1 in - --time-stamp | --time* | -t ) - echo "$timestamp" ; exit ;; - --version | -v ) - echo "$version" ; exit ;; - --help | --h* | -h ) - echo "$usage"; exit ;; - -- ) # Stop option processing - shift; break ;; - - ) # Use stdin as input. - break ;; - -* ) - echo "$me: invalid option $1$help" >&2 - exit 1 ;; - * ) - break ;; - esac -done - -if test $# != 0; then - echo "$me: too many arguments$help" >&2 - exit 1 -fi - -trap 'exit 1' 1 2 15 - -# CC_FOR_BUILD -- compiler used by this script. Note that the use of a -# compiler to aid in system detection is discouraged as it requires -# temporary files to be created and, as you can see below, it is a -# headache to deal with in a portable fashion. - -# Historically, `CC_FOR_BUILD' used to be named `HOST_CC'. We still -# use `HOST_CC' if defined, but it is deprecated. - -# Portable tmp directory creation inspired by the Autoconf team. - -set_cc_for_build=' -trap "exitcode=\$?; (rm -f \$tmpfiles 2>/dev/null; rmdir \$tmp 2>/dev/null) && exit \$exitcode" 0 ; -trap "rm -f \$tmpfiles 2>/dev/null; rmdir \$tmp 2>/dev/null; exit 1" 1 2 13 15 ; -: ${TMPDIR=/tmp} ; - { tmp=`(umask 077 && mktemp -d "$TMPDIR/cgXXXXXX") 2>/dev/null` && test -n "$tmp" && test -d "$tmp" ; } || - { test -n "$RANDOM" && tmp=$TMPDIR/cg$$-$RANDOM && (umask 077 && mkdir $tmp) ; } || - { tmp=$TMPDIR/cg-$$ && (umask 077 && mkdir $tmp) && echo "Warning: creating insecure temp directory" >&2 ; } || - { echo "$me: cannot create a temporary directory in $TMPDIR" >&2 ; exit 1 ; } ; -dummy=$tmp/dummy ; -tmpfiles="$dummy.c $dummy.o $dummy.rel $dummy" ; -case $CC_FOR_BUILD,$HOST_CC,$CC in - ,,) echo "int x;" > $dummy.c ; - for c in cc gcc c89 c99 ; do - if ($c -c -o $dummy.o $dummy.c) >/dev/null 2>&1 ; then - CC_FOR_BUILD="$c"; break ; - fi ; - done ; - if test x"$CC_FOR_BUILD" = x ; then - CC_FOR_BUILD=no_compiler_found ; - fi - ;; - ,,*) CC_FOR_BUILD=$CC ;; - ,*,*) CC_FOR_BUILD=$HOST_CC ;; -esac ; set_cc_for_build= ;' - -# This is needed to find uname on a Pyramid OSx when run in the BSD universe. -# (ghazi@noc.rutgers.edu 1994-08-24) -if (test -f /.attbin/uname) >/dev/null 2>&1 ; then - PATH=$PATH:/.attbin ; export PATH -fi - -UNAME_MACHINE=`(uname -m) 2>/dev/null` || UNAME_MACHINE=unknown -UNAME_RELEASE=`(uname -r) 2>/dev/null` || UNAME_RELEASE=unknown -UNAME_SYSTEM=`(uname -s) 2>/dev/null` || UNAME_SYSTEM=unknown -UNAME_VERSION=`(uname -v) 2>/dev/null` || UNAME_VERSION=unknown - -if [ "${UNAME_SYSTEM}" = "Linux" ] ; then - eval $set_cc_for_build - cat << EOF > $dummy.c - #include - #ifdef __UCLIBC__ - # ifdef __UCLIBC_CONFIG_VERSION__ - LIBC=uclibc __UCLIBC_CONFIG_VERSION__ - # else - LIBC=uclibc - # endif - #else - LIBC=gnu - #endif -EOF - eval `$CC_FOR_BUILD -E $dummy.c 2>/dev/null | grep LIBC= | sed -e 's: ::g'` -fi - -# Note: order is significant - the case branches are not exclusive. - -case "${UNAME_MACHINE}:${UNAME_SYSTEM}:${UNAME_RELEASE}:${UNAME_VERSION}" in - *:NetBSD:*:*) - # NetBSD (nbsd) targets should (where applicable) match one or - # more of the tupples: *-*-netbsdelf*, *-*-netbsdaout*, - # *-*-netbsdecoff* and *-*-netbsd*. For targets that recently - # switched to ELF, *-*-netbsd* would select the old - # object file format. This provides both forward - # compatibility and a consistent mechanism for selecting the - # object file format. - # - # Note: NetBSD doesn't particularly care about the vendor - # portion of the name. We always set it to "unknown". - sysctl="sysctl -n hw.machine_arch" - UNAME_MACHINE_ARCH=`(/sbin/$sysctl 2>/dev/null || \ - /usr/sbin/$sysctl 2>/dev/null || echo unknown)` - case "${UNAME_MACHINE_ARCH}" in - armeb) machine=armeb-unknown ;; - arm*) machine=arm-unknown ;; - sh3el) machine=shl-unknown ;; - sh3eb) machine=sh-unknown ;; - sh5el) machine=sh5le-unknown ;; - *) machine=${UNAME_MACHINE_ARCH}-unknown ;; - esac - # The Operating System including object format, if it has switched - # to ELF recently, or will in the future. - case "${UNAME_MACHINE_ARCH}" in - arm*|i386|m68k|ns32k|sh3*|sparc|vax) - eval $set_cc_for_build - if echo __ELF__ | $CC_FOR_BUILD -E - 2>/dev/null \ - | grep __ELF__ >/dev/null - then - # Once all utilities can be ECOFF (netbsdecoff) or a.out (netbsdaout). - # Return netbsd for either. FIX? - os=netbsd - else - os=netbsdelf - fi - ;; - *) - os=netbsd - ;; - esac - # The OS release - # Debian GNU/NetBSD machines have a different userland, and - # thus, need a distinct triplet. However, they do not need - # kernel version information, so it can be replaced with a - # suitable tag, in the style of linux-gnu. - case "${UNAME_VERSION}" in - Debian*) - release='-gnu' - ;; - *) - release=`echo ${UNAME_RELEASE}|sed -e 's/[-_].*/\./'` - ;; - esac - # Since CPU_TYPE-MANUFACTURER-KERNEL-OPERATING_SYSTEM: - # contains redundant information, the shorter form: - # CPU_TYPE-MANUFACTURER-OPERATING_SYSTEM is used. - echo "${machine}-${os}${release}" - exit ;; - *:OpenBSD:*:*) - UNAME_MACHINE_ARCH=`arch | sed 's/OpenBSD.//'` - echo ${UNAME_MACHINE_ARCH}-unknown-openbsd${UNAME_RELEASE} - exit ;; - *:ekkoBSD:*:*) - echo ${UNAME_MACHINE}-unknown-ekkobsd${UNAME_RELEASE} - exit ;; - *:SolidBSD:*:*) - echo ${UNAME_MACHINE}-unknown-solidbsd${UNAME_RELEASE} - exit ;; - macppc:MirBSD:*:*) - echo powerpc-unknown-mirbsd${UNAME_RELEASE} - exit ;; - *:MirBSD:*:*) - echo ${UNAME_MACHINE}-unknown-mirbsd${UNAME_RELEASE} - exit ;; - alpha:OSF1:*:*) - case $UNAME_RELEASE in - *4.0) - UNAME_RELEASE=`/usr/sbin/sizer -v | awk '{print $3}'` - ;; - *5.*) - UNAME_RELEASE=`/usr/sbin/sizer -v | awk '{print $4}'` - ;; - esac - # According to Compaq, /usr/sbin/psrinfo has been available on - # OSF/1 and Tru64 systems produced since 1995. I hope that - # covers most systems running today. This code pipes the CPU - # types through head -n 1, so we only detect the type of CPU 0. - ALPHA_CPU_TYPE=`/usr/sbin/psrinfo -v | sed -n -e 's/^ The alpha \(.*\) processor.*$/\1/p' | head -n 1` - case "$ALPHA_CPU_TYPE" in - "EV4 (21064)") - UNAME_MACHINE="alpha" ;; - "EV4.5 (21064)") - UNAME_MACHINE="alpha" ;; - "LCA4 (21066/21068)") - UNAME_MACHINE="alpha" ;; - "EV5 (21164)") - UNAME_MACHINE="alphaev5" ;; - "EV5.6 (21164A)") - UNAME_MACHINE="alphaev56" ;; - "EV5.6 (21164PC)") - UNAME_MACHINE="alphapca56" ;; - "EV5.7 (21164PC)") - UNAME_MACHINE="alphapca57" ;; - "EV6 (21264)") - UNAME_MACHINE="alphaev6" ;; - "EV6.7 (21264A)") - UNAME_MACHINE="alphaev67" ;; - "EV6.8CB (21264C)") - UNAME_MACHINE="alphaev68" ;; - "EV6.8AL (21264B)") - UNAME_MACHINE="alphaev68" ;; - "EV6.8CX (21264D)") - UNAME_MACHINE="alphaev68" ;; - "EV6.9A (21264/EV69A)") - UNAME_MACHINE="alphaev69" ;; - "EV7 (21364)") - UNAME_MACHINE="alphaev7" ;; - "EV7.9 (21364A)") - UNAME_MACHINE="alphaev79" ;; - esac - # A Pn.n version is a patched version. - # A Vn.n version is a released version. - # A Tn.n version is a released field test version. - # A Xn.n version is an unreleased experimental baselevel. - # 1.2 uses "1.2" for uname -r. - echo ${UNAME_MACHINE}-dec-osf`echo ${UNAME_RELEASE} | sed -e 's/^[PVTX]//' | tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz'` - exit ;; - Alpha\ *:Windows_NT*:*) - # How do we know it's Interix rather than the generic POSIX subsystem? - # Should we change UNAME_MACHINE based on the output of uname instead - # of the specific Alpha model? - echo alpha-pc-interix - exit ;; - 21064:Windows_NT:50:3) - echo alpha-dec-winnt3.5 - exit ;; - Amiga*:UNIX_System_V:4.0:*) - echo m68k-unknown-sysv4 - exit ;; - *:[Aa]miga[Oo][Ss]:*:*) - echo ${UNAME_MACHINE}-unknown-amigaos - exit ;; - *:[Mm]orph[Oo][Ss]:*:*) - echo ${UNAME_MACHINE}-unknown-morphos - exit ;; - *:OS/390:*:*) - echo i370-ibm-openedition - exit ;; - *:z/VM:*:*) - echo s390-ibm-zvmoe - exit ;; - *:OS400:*:*) - echo powerpc-ibm-os400 - exit ;; - arm:RISC*:1.[012]*:*|arm:riscix:1.[012]*:*) - echo arm-acorn-riscix${UNAME_RELEASE} - exit ;; - arm:riscos:*:*|arm:RISCOS:*:*) - echo arm-unknown-riscos - exit ;; - SR2?01:HI-UX/MPP:*:* | SR8000:HI-UX/MPP:*:*) - echo hppa1.1-hitachi-hiuxmpp - exit ;; - Pyramid*:OSx*:*:* | MIS*:OSx*:*:* | MIS*:SMP_DC-OSx*:*:*) - # akee@wpdis03.wpafb.af.mil (Earle F. Ake) contributed MIS and NILE. - if test "`(/bin/universe) 2>/dev/null`" = att ; then - echo pyramid-pyramid-sysv3 - else - echo pyramid-pyramid-bsd - fi - exit ;; - NILE*:*:*:dcosx) - echo pyramid-pyramid-svr4 - exit ;; - DRS?6000:unix:4.0:6*) - echo sparc-icl-nx6 - exit ;; - DRS?6000:UNIX_SV:4.2*:7* | DRS?6000:isis:4.2*:7*) - case `/usr/bin/uname -p` in - sparc) echo sparc-icl-nx7; exit ;; - esac ;; - sun4H:SunOS:5.*:*) - echo sparc-hal-solaris2`echo ${UNAME_RELEASE}|sed -e 's/[^.]*//'` - exit ;; - sun4*:SunOS:5.*:* | tadpole*:SunOS:5.*:*) - echo sparc-sun-solaris2`echo ${UNAME_RELEASE}|sed -e 's/[^.]*//'` - exit ;; - i86pc:SunOS:5.*:* | i86xen:SunOS:5.*:*) - echo i386-pc-solaris2`echo ${UNAME_RELEASE}|sed -e 's/[^.]*//'` - exit ;; - sun4*:SunOS:6*:*) - # According to config.sub, this is the proper way to canonicalize - # SunOS6. Hard to guess exactly what SunOS6 will be like, but - # it's likely to be more like Solaris than SunOS4. - echo sparc-sun-solaris3`echo ${UNAME_RELEASE}|sed -e 's/[^.]*//'` - exit ;; - sun4*:SunOS:*:*) - case "`/usr/bin/arch -k`" in - Series*|S4*) - UNAME_RELEASE=`uname -v` - ;; - esac - # Japanese Language versions have a version number like `4.1.3-JL'. - echo sparc-sun-sunos`echo ${UNAME_RELEASE}|sed -e 's/-/_/'` - exit ;; - sun3*:SunOS:*:*) - echo m68k-sun-sunos${UNAME_RELEASE} - exit ;; - sun*:*:4.2BSD:*) - UNAME_RELEASE=`(sed 1q /etc/motd | awk '{print substr($5,1,3)}') 2>/dev/null` - test "x${UNAME_RELEASE}" = "x" && UNAME_RELEASE=3 - case "`/bin/arch`" in - sun3) - echo m68k-sun-sunos${UNAME_RELEASE} - ;; - sun4) - echo sparc-sun-sunos${UNAME_RELEASE} - ;; - esac - exit ;; - aushp:SunOS:*:*) - echo sparc-auspex-sunos${UNAME_RELEASE} - exit ;; - # The situation for MiNT is a little confusing. The machine name - # can be virtually everything (everything which is not - # "atarist" or "atariste" at least should have a processor - # > m68000). The system name ranges from "MiNT" over "FreeMiNT" - # to the lowercase version "mint" (or "freemint"). Finally - # the system name "TOS" denotes a system which is actually not - # MiNT. But MiNT is downward compatible to TOS, so this should - # be no problem. - atarist[e]:*MiNT:*:* | atarist[e]:*mint:*:* | atarist[e]:*TOS:*:*) - echo m68k-atari-mint${UNAME_RELEASE} - exit ;; - atari*:*MiNT:*:* | atari*:*mint:*:* | atarist[e]:*TOS:*:*) - echo m68k-atari-mint${UNAME_RELEASE} - exit ;; - *falcon*:*MiNT:*:* | *falcon*:*mint:*:* | *falcon*:*TOS:*:*) - echo m68k-atari-mint${UNAME_RELEASE} - exit ;; - milan*:*MiNT:*:* | milan*:*mint:*:* | *milan*:*TOS:*:*) - echo m68k-milan-mint${UNAME_RELEASE} - exit ;; - hades*:*MiNT:*:* | hades*:*mint:*:* | *hades*:*TOS:*:*) - echo m68k-hades-mint${UNAME_RELEASE} - exit ;; - *:*MiNT:*:* | *:*mint:*:* | *:*TOS:*:*) - echo m68k-unknown-mint${UNAME_RELEASE} - exit ;; - m68k:machten:*:*) - echo m68k-apple-machten${UNAME_RELEASE} - exit ;; - powerpc:machten:*:*) - echo powerpc-apple-machten${UNAME_RELEASE} - exit ;; - RISC*:Mach:*:*) - echo mips-dec-mach_bsd4.3 - exit ;; - RISC*:ULTRIX:*:*) - echo mips-dec-ultrix${UNAME_RELEASE} - exit ;; - VAX*:ULTRIX*:*:*) - echo vax-dec-ultrix${UNAME_RELEASE} - exit ;; - 2020:CLIX:*:* | 2430:CLIX:*:*) - echo clipper-intergraph-clix${UNAME_RELEASE} - exit ;; - mips:*:*:UMIPS | mips:*:*:RISCos) - eval $set_cc_for_build - sed 's/^ //' << EOF >$dummy.c -#ifdef __cplusplus -#include /* for printf() prototype */ - int main (int argc, char *argv[]) { -#else - int main (argc, argv) int argc; char *argv[]; { -#endif - #if defined (host_mips) && defined (MIPSEB) - #if defined (SYSTYPE_SYSV) - printf ("mips-mips-riscos%ssysv\n", argv[1]); exit (0); - #endif - #if defined (SYSTYPE_SVR4) - printf ("mips-mips-riscos%ssvr4\n", argv[1]); exit (0); - #endif - #if defined (SYSTYPE_BSD43) || defined(SYSTYPE_BSD) - printf ("mips-mips-riscos%sbsd\n", argv[1]); exit (0); - #endif - #endif - exit (-1); - } -EOF - $CC_FOR_BUILD -o $dummy $dummy.c && - dummyarg=`echo "${UNAME_RELEASE}" | sed -n 's/\([0-9]*\).*/\1/p'` && - SYSTEM_NAME=`$dummy $dummyarg` && - { echo "$SYSTEM_NAME"; exit; } - echo mips-mips-riscos${UNAME_RELEASE} - exit ;; - Motorola:PowerMAX_OS:*:*) - echo powerpc-motorola-powermax - exit ;; - Motorola:*:4.3:PL8-*) - echo powerpc-harris-powermax - exit ;; - Night_Hawk:*:*:PowerMAX_OS | Synergy:PowerMAX_OS:*:*) - echo powerpc-harris-powermax - exit ;; - Night_Hawk:Power_UNIX:*:*) - echo powerpc-harris-powerunix - exit ;; - m88k:CX/UX:7*:*) - echo m88k-harris-cxux7 - exit ;; - m88k:*:4*:R4*) - echo m88k-motorola-sysv4 - exit ;; - m88k:*:3*:R3*) - echo m88k-motorola-sysv3 - exit ;; - AViiON:dgux:*:*) - # DG/UX returns AViiON for all architectures - UNAME_PROCESSOR=`/usr/bin/uname -p` - if [ $UNAME_PROCESSOR = mc88100 ] || [ $UNAME_PROCESSOR = mc88110 ] - then - if [ ${TARGET_BINARY_INTERFACE}x = m88kdguxelfx ] || \ - [ ${TARGET_BINARY_INTERFACE}x = x ] - then - echo m88k-dg-dgux${UNAME_RELEASE} - else - echo m88k-dg-dguxbcs${UNAME_RELEASE} - fi - else - echo i586-dg-dgux${UNAME_RELEASE} - fi - exit ;; - M88*:DolphinOS:*:*) # DolphinOS (SVR3) - echo m88k-dolphin-sysv3 - exit ;; - M88*:*:R3*:*) - # Delta 88k system running SVR3 - echo m88k-motorola-sysv3 - exit ;; - XD88*:*:*:*) # Tektronix XD88 system running UTekV (SVR3) - echo m88k-tektronix-sysv3 - exit ;; - Tek43[0-9][0-9]:UTek:*:*) # Tektronix 4300 system running UTek (BSD) - echo m68k-tektronix-bsd - exit ;; - *:IRIX*:*:*) - echo mips-sgi-irix`echo ${UNAME_RELEASE}|sed -e 's/-/_/g'` - exit ;; - ????????:AIX?:[12].1:2) # AIX 2.2.1 or AIX 2.1.1 is RT/PC AIX. - echo romp-ibm-aix # uname -m gives an 8 hex-code CPU id - exit ;; # Note that: echo "'`uname -s`'" gives 'AIX ' - i*86:AIX:*:*) - echo i386-ibm-aix - exit ;; - ia64:AIX:*:*) - if [ -x /usr/bin/oslevel ] ; then - IBM_REV=`/usr/bin/oslevel` - else - IBM_REV=${UNAME_VERSION}.${UNAME_RELEASE} - fi - echo ${UNAME_MACHINE}-ibm-aix${IBM_REV} - exit ;; - *:AIX:2:3) - if grep bos325 /usr/include/stdio.h >/dev/null 2>&1; then - eval $set_cc_for_build - sed 's/^ //' << EOF >$dummy.c - #include - - main() - { - if (!__power_pc()) - exit(1); - puts("powerpc-ibm-aix3.2.5"); - exit(0); - } -EOF - if $CC_FOR_BUILD -o $dummy $dummy.c && SYSTEM_NAME=`$dummy` - then - echo "$SYSTEM_NAME" - else - echo rs6000-ibm-aix3.2.5 - fi - elif grep bos324 /usr/include/stdio.h >/dev/null 2>&1; then - echo rs6000-ibm-aix3.2.4 - else - echo rs6000-ibm-aix3.2 - fi - exit ;; - *:AIX:*:[45]) - IBM_CPU_ID=`/usr/sbin/lsdev -C -c processor -S available | sed 1q | awk '{ print $1 }'` - if /usr/sbin/lsattr -El ${IBM_CPU_ID} | grep ' POWER' >/dev/null 2>&1; then - IBM_ARCH=rs6000 - else - IBM_ARCH=powerpc - fi - if [ -x /usr/bin/oslevel ] ; then - IBM_REV=`/usr/bin/oslevel` - else - IBM_REV=${UNAME_VERSION}.${UNAME_RELEASE} - fi - echo ${IBM_ARCH}-ibm-aix${IBM_REV} - exit ;; - *:AIX:*:*) - echo rs6000-ibm-aix - exit ;; - ibmrt:4.4BSD:*|romp-ibm:BSD:*) - echo romp-ibm-bsd4.4 - exit ;; - ibmrt:*BSD:*|romp-ibm:BSD:*) # covers RT/PC BSD and - echo romp-ibm-bsd${UNAME_RELEASE} # 4.3 with uname added to - exit ;; # report: romp-ibm BSD 4.3 - *:BOSX:*:*) - echo rs6000-bull-bosx - exit ;; - DPX/2?00:B.O.S.:*:*) - echo m68k-bull-sysv3 - exit ;; - 9000/[34]??:4.3bsd:1.*:*) - echo m68k-hp-bsd - exit ;; - hp300:4.4BSD:*:* | 9000/[34]??:4.3bsd:2.*:*) - echo m68k-hp-bsd4.4 - exit ;; - 9000/[34678]??:HP-UX:*:*) - HPUX_REV=`echo ${UNAME_RELEASE}|sed -e 's/[^.]*.[0B]*//'` - case "${UNAME_MACHINE}" in - 9000/31? ) HP_ARCH=m68000 ;; - 9000/[34]?? ) HP_ARCH=m68k ;; - 9000/[678][0-9][0-9]) - if [ -x /usr/bin/getconf ]; then - sc_cpu_version=`/usr/bin/getconf SC_CPU_VERSION 2>/dev/null` - sc_kernel_bits=`/usr/bin/getconf SC_KERNEL_BITS 2>/dev/null` - case "${sc_cpu_version}" in - 523) HP_ARCH="hppa1.0" ;; # CPU_PA_RISC1_0 - 528) HP_ARCH="hppa1.1" ;; # CPU_PA_RISC1_1 - 532) # CPU_PA_RISC2_0 - case "${sc_kernel_bits}" in - 32) HP_ARCH="hppa2.0n" ;; - 64) HP_ARCH="hppa2.0w" ;; - '') HP_ARCH="hppa2.0" ;; # HP-UX 10.20 - esac ;; - esac - fi - if [ "${HP_ARCH}" = "" ]; then - eval $set_cc_for_build - sed 's/^ //' << EOF >$dummy.c - - #define _HPUX_SOURCE - #include - #include - - int main () - { - #if defined(_SC_KERNEL_BITS) - long bits = sysconf(_SC_KERNEL_BITS); - #endif - long cpu = sysconf (_SC_CPU_VERSION); - - switch (cpu) - { - case CPU_PA_RISC1_0: puts ("hppa1.0"); break; - case CPU_PA_RISC1_1: puts ("hppa1.1"); break; - case CPU_PA_RISC2_0: - #if defined(_SC_KERNEL_BITS) - switch (bits) - { - case 64: puts ("hppa2.0w"); break; - case 32: puts ("hppa2.0n"); break; - default: puts ("hppa2.0"); break; - } break; - #else /* !defined(_SC_KERNEL_BITS) */ - puts ("hppa2.0"); break; - #endif - default: puts ("hppa1.0"); break; - } - exit (0); - } -EOF - (CCOPTS= $CC_FOR_BUILD -o $dummy $dummy.c 2>/dev/null) && HP_ARCH=`$dummy` - test -z "$HP_ARCH" && HP_ARCH=hppa - fi ;; - esac - if [ ${HP_ARCH} = "hppa2.0w" ] - then - eval $set_cc_for_build - - # hppa2.0w-hp-hpux* has a 64-bit kernel and a compiler generating - # 32-bit code. hppa64-hp-hpux* has the same kernel and a compiler - # generating 64-bit code. GNU and HP use different nomenclature: - # - # $ CC_FOR_BUILD=cc ./config.guess - # => hppa2.0w-hp-hpux11.23 - # $ CC_FOR_BUILD="cc +DA2.0w" ./config.guess - # => hppa64-hp-hpux11.23 - - if echo __LP64__ | (CCOPTS= $CC_FOR_BUILD -E - 2>/dev/null) | - grep __LP64__ >/dev/null - then - HP_ARCH="hppa2.0w" - else - HP_ARCH="hppa64" - fi - fi - echo ${HP_ARCH}-hp-hpux${HPUX_REV} - exit ;; - ia64:HP-UX:*:*) - HPUX_REV=`echo ${UNAME_RELEASE}|sed -e 's/[^.]*.[0B]*//'` - echo ia64-hp-hpux${HPUX_REV} - exit ;; - 3050*:HI-UX:*:*) - eval $set_cc_for_build - sed 's/^ //' << EOF >$dummy.c - #include - int - main () - { - long cpu = sysconf (_SC_CPU_VERSION); - /* The order matters, because CPU_IS_HP_MC68K erroneously returns - true for CPU_PA_RISC1_0. CPU_IS_PA_RISC returns correct - results, however. */ - if (CPU_IS_PA_RISC (cpu)) - { - switch (cpu) - { - case CPU_PA_RISC1_0: puts ("hppa1.0-hitachi-hiuxwe2"); break; - case CPU_PA_RISC1_1: puts ("hppa1.1-hitachi-hiuxwe2"); break; - case CPU_PA_RISC2_0: puts ("hppa2.0-hitachi-hiuxwe2"); break; - default: puts ("hppa-hitachi-hiuxwe2"); break; - } - } - else if (CPU_IS_HP_MC68K (cpu)) - puts ("m68k-hitachi-hiuxwe2"); - else puts ("unknown-hitachi-hiuxwe2"); - exit (0); - } -EOF - $CC_FOR_BUILD -o $dummy $dummy.c && SYSTEM_NAME=`$dummy` && - { echo "$SYSTEM_NAME"; exit; } - echo unknown-hitachi-hiuxwe2 - exit ;; - 9000/7??:4.3bsd:*:* | 9000/8?[79]:4.3bsd:*:* ) - echo hppa1.1-hp-bsd - exit ;; - 9000/8??:4.3bsd:*:*) - echo hppa1.0-hp-bsd - exit ;; - *9??*:MPE/iX:*:* | *3000*:MPE/iX:*:*) - echo hppa1.0-hp-mpeix - exit ;; - hp7??:OSF1:*:* | hp8?[79]:OSF1:*:* ) - echo hppa1.1-hp-osf - exit ;; - hp8??:OSF1:*:*) - echo hppa1.0-hp-osf - exit ;; - i*86:OSF1:*:*) - if [ -x /usr/sbin/sysversion ] ; then - echo ${UNAME_MACHINE}-unknown-osf1mk - else - echo ${UNAME_MACHINE}-unknown-osf1 - fi - exit ;; - parisc*:Lites*:*:*) - echo hppa1.1-hp-lites - exit ;; - C1*:ConvexOS:*:* | convex:ConvexOS:C1*:*) - echo c1-convex-bsd - exit ;; - C2*:ConvexOS:*:* | convex:ConvexOS:C2*:*) - if getsysinfo -f scalar_acc - then echo c32-convex-bsd - else echo c2-convex-bsd - fi - exit ;; - C34*:ConvexOS:*:* | convex:ConvexOS:C34*:*) - echo c34-convex-bsd - exit ;; - C38*:ConvexOS:*:* | convex:ConvexOS:C38*:*) - echo c38-convex-bsd - exit ;; - C4*:ConvexOS:*:* | convex:ConvexOS:C4*:*) - echo c4-convex-bsd - exit ;; - CRAY*Y-MP:*:*:*) - echo ymp-cray-unicos${UNAME_RELEASE} | sed -e 's/\.[^.]*$/.X/' - exit ;; - CRAY*[A-Z]90:*:*:*) - echo ${UNAME_MACHINE}-cray-unicos${UNAME_RELEASE} \ - | sed -e 's/CRAY.*\([A-Z]90\)/\1/' \ - -e y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/ \ - -e 's/\.[^.]*$/.X/' - exit ;; - CRAY*TS:*:*:*) - echo t90-cray-unicos${UNAME_RELEASE} | sed -e 's/\.[^.]*$/.X/' - exit ;; - CRAY*T3E:*:*:*) - echo alphaev5-cray-unicosmk${UNAME_RELEASE} | sed -e 's/\.[^.]*$/.X/' - exit ;; - CRAY*SV1:*:*:*) - echo sv1-cray-unicos${UNAME_RELEASE} | sed -e 's/\.[^.]*$/.X/' - exit ;; - *:UNICOS/mp:*:*) - echo craynv-cray-unicosmp${UNAME_RELEASE} | sed -e 's/\.[^.]*$/.X/' - exit ;; - F30[01]:UNIX_System_V:*:* | F700:UNIX_System_V:*:*) - FUJITSU_PROC=`uname -m | tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz'` - FUJITSU_SYS=`uname -p | tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz' | sed -e 's/\///'` - FUJITSU_REL=`echo ${UNAME_RELEASE} | sed -e 's/ /_/'` - echo "${FUJITSU_PROC}-fujitsu-${FUJITSU_SYS}${FUJITSU_REL}" - exit ;; - 5000:UNIX_System_V:4.*:*) - FUJITSU_SYS=`uname -p | tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz' | sed -e 's/\///'` - FUJITSU_REL=`echo ${UNAME_RELEASE} | tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz' | sed -e 's/ /_/'` - echo "sparc-fujitsu-${FUJITSU_SYS}${FUJITSU_REL}" - exit ;; - i*86:BSD/386:*:* | i*86:BSD/OS:*:* | *:Ascend\ Embedded/OS:*:*) - echo ${UNAME_MACHINE}-pc-bsdi${UNAME_RELEASE} - exit ;; - sparc*:BSD/OS:*:*) - echo sparc-unknown-bsdi${UNAME_RELEASE} - exit ;; - *:BSD/OS:*:*) - echo ${UNAME_MACHINE}-unknown-bsdi${UNAME_RELEASE} - exit ;; - *:FreeBSD:*:*) - case ${UNAME_MACHINE} in - pc98) - echo i386-unknown-freebsd`echo ${UNAME_RELEASE}|sed -e 's/[-(].*//'` ;; - amd64) - echo x86_64-unknown-freebsd`echo ${UNAME_RELEASE}|sed -e 's/[-(].*//'` ;; - *) - echo ${UNAME_MACHINE}-unknown-freebsd`echo ${UNAME_RELEASE}|sed -e 's/[-(].*//'` ;; - esac - exit ;; - i*:CYGWIN*:*) - echo ${UNAME_MACHINE}-pc-cygwin - exit ;; - *:MINGW*:*) - echo ${UNAME_MACHINE}-pc-mingw32 - exit ;; - i*:windows32*:*) - # uname -m includes "-pc" on this system. - echo ${UNAME_MACHINE}-mingw32 - exit ;; - i*:PW*:*) - echo ${UNAME_MACHINE}-pc-pw32 - exit ;; - *:Interix*:[3456]*) - case ${UNAME_MACHINE} in - x86) - echo i586-pc-interix${UNAME_RELEASE} - exit ;; - EM64T | authenticamd) - echo x86_64-unknown-interix${UNAME_RELEASE} - exit ;; - esac ;; - [345]86:Windows_95:* | [345]86:Windows_98:* | [345]86:Windows_NT:*) - echo i${UNAME_MACHINE}-pc-mks - exit ;; - i*:Windows_NT*:* | Pentium*:Windows_NT*:*) - # How do we know it's Interix rather than the generic POSIX subsystem? - # It also conflicts with pre-2.0 versions of AT&T UWIN. Should we - # UNAME_MACHINE based on the output of uname instead of i386? - echo i586-pc-interix - exit ;; - i*:UWIN*:*) - echo ${UNAME_MACHINE}-pc-uwin - exit ;; - amd64:CYGWIN*:*:* | x86_64:CYGWIN*:*:*) - echo x86_64-unknown-cygwin - exit ;; - p*:CYGWIN*:*) - echo powerpcle-unknown-cygwin - exit ;; - prep*:SunOS:5.*:*) - echo powerpcle-unknown-solaris2`echo ${UNAME_RELEASE}|sed -e 's/[^.]*//'` - exit ;; - *:GNU:*:*) - # the GNU system - echo `echo ${UNAME_MACHINE}|sed -e 's,[-/].*$,,'`-unknown-gnu`echo ${UNAME_RELEASE}|sed -e 's,/.*$,,'` - exit ;; - *:GNU/*:*:*) - # other systems with GNU libc and userland - echo ${UNAME_MACHINE}-unknown-`echo ${UNAME_SYSTEM} | sed 's,^[^/]*/,,' | tr '[A-Z]' '[a-z]'``echo ${UNAME_RELEASE}|sed -e 's/[-(].*//'`-gnu - exit ;; - i*86:Minix:*:*) - echo ${UNAME_MACHINE}-pc-minix - exit ;; - arm*:Linux:*:*) - echo ${UNAME_MACHINE}-unknown-linux-${LIBC} - exit ;; - avr32*:Linux:*:*) - echo ${UNAME_MACHINE}-unknown-linux-${LIBC} - exit ;; - cris:Linux:*:*) - echo cris-axis-linux-${LIBC} - exit ;; - crisv32:Linux:*:*) - echo crisv32-axis-linux-${LIBC} - exit ;; - frv:Linux:*:*) - echo frv-unknown-linux-${LIBC} - exit ;; - ia64:Linux:*:*) - echo ${UNAME_MACHINE}-unknown-linux-${LIBC} - exit ;; - m32r*:Linux:*:*) - echo ${UNAME_MACHINE}-unknown-linux-${LIBC} - exit ;; - m68*:Linux:*:*) - echo ${UNAME_MACHINE}-unknown-linux-${LIBC} - exit ;; - mips:Linux:*:*) - eval $set_cc_for_build - sed 's/^ //' << EOF >$dummy.c - #undef CPU - #undef mips - #undef mipsel - #if defined(__MIPSEL__) || defined(__MIPSEL) || defined(_MIPSEL) || defined(MIPSEL) - CPU=mipsel - #else - #if defined(__MIPSEB__) || defined(__MIPSEB) || defined(_MIPSEB) || defined(MIPSEB) - CPU=mips - #else - CPU= - #endif - #endif -EOF - eval "`$CC_FOR_BUILD -E $dummy.c 2>/dev/null | sed -n ' - /^CPU/{ - s: ::g - p - }'`" - test x"${CPU}" != x && { echo "${CPU}-unknown-linux-${LIBC}"; exit; } - ;; - mips64:Linux:*:*) - eval $set_cc_for_build - sed 's/^ //' << EOF >$dummy.c - #undef CPU - #undef mips64 - #undef mips64el - #if defined(__MIPSEL__) || defined(__MIPSEL) || defined(_MIPSEL) || defined(MIPSEL) - CPU=mips64el - #else - #if defined(__MIPSEB__) || defined(__MIPSEB) || defined(_MIPSEB) || defined(MIPSEB) - CPU=mips64 - #else - CPU= - #endif - #endif -EOF - eval "`$CC_FOR_BUILD -E $dummy.c 2>/dev/null | sed -n ' - /^CPU/{ - s: ::g - p - }'`" - test x"${CPU}" != x && { echo "${CPU}-unknown-linux-${LIBC}"; exit; } - ;; - or32:Linux:*:*) - echo or32-unknown-linux-${LIBC} - exit ;; - ppc:Linux:*:*) - echo powerpc-unknown-linux-${LIBC} - exit ;; - ppc64:Linux:*:*) - echo powerpc64-unknown-linux-${LIBC} - exit ;; - alpha:Linux:*:*) - case `sed -n '/^cpu model/s/^.*: \(.*\)/\1/p' < /proc/cpuinfo` in - EV5) UNAME_MACHINE=alphaev5 ;; - EV56) UNAME_MACHINE=alphaev56 ;; - PCA56) UNAME_MACHINE=alphapca56 ;; - PCA57) UNAME_MACHINE=alphapca56 ;; - EV6) UNAME_MACHINE=alphaev6 ;; - EV67) UNAME_MACHINE=alphaev67 ;; - EV68*) UNAME_MACHINE=alphaev68 ;; - esac - objdump --private-headers /bin/sh | grep ld.so.1 >/dev/null - if test "$?" = 0 ; then LIBC="gnulibc1" ; fi - echo ${UNAME_MACHINE}-unknown-linux-${LIBC} - exit ;; - parisc:Linux:*:* | hppa:Linux:*:*) - # Look for CPU level - case `grep '^cpu[^a-z]*:' /proc/cpuinfo 2>/dev/null | cut -d' ' -f2` in - PA7*) echo hppa1.1-unknown-linux-${LIBC} ;; - PA8*) echo hppa2.0-unknown-linux-${LIBC} ;; - *) echo hppa-unknown-linux-${LIBC} ;; - esac - exit ;; - parisc64:Linux:*:* | hppa64:Linux:*:*) - echo hppa64-unknown-linux-${LIBC} - exit ;; - s390:Linux:*:* | s390x:Linux:*:*) - echo ${UNAME_MACHINE}-ibm-linux - exit ;; - sh64*:Linux:*:*) - echo ${UNAME_MACHINE}-unknown-linux-${LIBC} - exit ;; - sh*:Linux:*:*) - echo ${UNAME_MACHINE}-unknown-linux-${LIBC} - exit ;; - sparc:Linux:*:* | sparc64:Linux:*:*) - echo ${UNAME_MACHINE}-unknown-linux-${LIBC} - exit ;; - vax:Linux:*:*) - echo ${UNAME_MACHINE}-dec-linux-${LIBC} - exit ;; - x86_64:Linux:*:*) - echo x86_64-unknown-linux-${LIBC} - exit ;; - xtensa:Linux:*:*) - echo xtensa-unknown-linux-${LIBC} - exit ;; - i*86:Linux:*:*) - # The BFD linker knows what the default object file format is, so - # first see if it will tell us. cd to the root directory to prevent - # problems with other programs or directories called `ld' in the path. - # Set LC_ALL=C to ensure ld outputs messages in English. - ld_supported_targets=`cd /; LC_ALL=C ld --help 2>&1 \ - | sed -ne '/supported targets:/!d - s/[ ][ ]*/ /g - s/.*supported targets: *// - s/ .*// - p'` - case "$ld_supported_targets" in - elf32-i386) - TENTATIVE="${UNAME_MACHINE}-pc-linux-${LIBC}" - ;; - a.out-i386-linux) - echo "${UNAME_MACHINE}-pc-linux-${LIBC}aout" - exit ;; - coff-i386) - echo "${UNAME_MACHINE}-pc-linux-${LIBC}coff" - exit ;; - "") - # Either a pre-BFD a.out linker (linux-gnuoldld) or - # one that does not give us useful --help. - echo "${UNAME_MACHINE}-pc-linux-${LIBC}oldld" - exit ;; - esac - # This should get integrated into the C code below, but now we hack - if [ "$LIBC" != "gnu" ] ; then echo "$TENTATIVE" && exit 0 ; fi - # Determine whether the default compiler is a.out or elf - eval $set_cc_for_build - sed 's/^ //' << EOF >$dummy.c - #include - #ifdef __ELF__ - # ifdef __GLIBC__ - # if __GLIBC__ >= 2 - LIBC=gnu - # else - LIBC=gnulibc1 - # endif - # else - LIBC=gnulibc1 - # endif - #else - #if defined(__INTEL_COMPILER) || defined(__PGI) || defined(__SUNPRO_C) || defined(__SUNPRO_CC) - LIBC=gnu - #else - LIBC=gnuaout - #endif - #endif - #ifdef __dietlibc__ - LIBC=dietlibc - #endif -EOF - eval "`$CC_FOR_BUILD -E $dummy.c 2>/dev/null | sed -n ' - /^LIBC/{ - s: ::g - p - }'`" - test x"${LIBC}" != x && { - echo "${UNAME_MACHINE}-pc-linux-${LIBC}" - exit - } - test x"${TENTATIVE}" != x && { echo "${TENTATIVE}"; exit; } - ;; - i*86:DYNIX/ptx:4*:*) - # ptx 4.0 does uname -s correctly, with DYNIX/ptx in there. - # earlier versions are messed up and put the nodename in both - # sysname and nodename. - echo i386-sequent-sysv4 - exit ;; - i*86:UNIX_SV:4.2MP:2.*) - # Unixware is an offshoot of SVR4, but it has its own version - # number series starting with 2... - # I am not positive that other SVR4 systems won't match this, - # I just have to hope. -- rms. - # Use sysv4.2uw... so that sysv4* matches it. - echo ${UNAME_MACHINE}-pc-sysv4.2uw${UNAME_VERSION} - exit ;; - i*86:OS/2:*:*) - # If we were able to find `uname', then EMX Unix compatibility - # is probably installed. - echo ${UNAME_MACHINE}-pc-os2-emx - exit ;; - i*86:XTS-300:*:STOP) - echo ${UNAME_MACHINE}-unknown-stop - exit ;; - i*86:atheos:*:*) - echo ${UNAME_MACHINE}-unknown-atheos - exit ;; - i*86:syllable:*:*) - echo ${UNAME_MACHINE}-pc-syllable - exit ;; - i*86:LynxOS:2.*:* | i*86:LynxOS:3.[01]*:* | i*86:LynxOS:4.0*:*) - echo i386-unknown-lynxos${UNAME_RELEASE} - exit ;; - i*86:*DOS:*:*) - echo ${UNAME_MACHINE}-pc-msdosdjgpp - exit ;; - i*86:*:4.*:* | i*86:SYSTEM_V:4.*:*) - UNAME_REL=`echo ${UNAME_RELEASE} | sed 's/\/MP$//'` - if grep Novell /usr/include/link.h >/dev/null 2>/dev/null; then - echo ${UNAME_MACHINE}-univel-sysv${UNAME_REL} - else - echo ${UNAME_MACHINE}-pc-sysv${UNAME_REL} - fi - exit ;; - i*86:*:5:[678]*) - # UnixWare 7.x, OpenUNIX and OpenServer 6. - case `/bin/uname -X | grep "^Machine"` in - *486*) UNAME_MACHINE=i486 ;; - *Pentium) UNAME_MACHINE=i586 ;; - *Pent*|*Celeron) UNAME_MACHINE=i686 ;; - esac - echo ${UNAME_MACHINE}-unknown-sysv${UNAME_RELEASE}${UNAME_SYSTEM}${UNAME_VERSION} - exit ;; - i*86:*:3.2:*) - if test -f /usr/options/cb.name; then - UNAME_REL=`sed -n 's/.*Version //p' /dev/null >/dev/null ; then - UNAME_REL=`(/bin/uname -X|grep Release|sed -e 's/.*= //')` - (/bin/uname -X|grep i80486 >/dev/null) && UNAME_MACHINE=i486 - (/bin/uname -X|grep '^Machine.*Pentium' >/dev/null) \ - && UNAME_MACHINE=i586 - (/bin/uname -X|grep '^Machine.*Pent *II' >/dev/null) \ - && UNAME_MACHINE=i686 - (/bin/uname -X|grep '^Machine.*Pentium Pro' >/dev/null) \ - && UNAME_MACHINE=i686 - echo ${UNAME_MACHINE}-pc-sco$UNAME_REL - else - echo ${UNAME_MACHINE}-pc-sysv32 - fi - exit ;; - pc:*:*:*) - # Left here for compatibility: - # uname -m prints for DJGPP always 'pc', but it prints nothing about - # the processor, so we play safe by assuming i386. - echo i386-pc-msdosdjgpp - exit ;; - Intel:Mach:3*:*) - echo i386-pc-mach3 - exit ;; - paragon:*:*:*) - echo i860-intel-osf1 - exit ;; - i860:*:4.*:*) # i860-SVR4 - if grep Stardent /usr/include/sys/uadmin.h >/dev/null 2>&1 ; then - echo i860-stardent-sysv${UNAME_RELEASE} # Stardent Vistra i860-SVR4 - else # Add other i860-SVR4 vendors below as they are discovered. - echo i860-unknown-sysv${UNAME_RELEASE} # Unknown i860-SVR4 - fi - exit ;; - mini*:CTIX:SYS*5:*) - # "miniframe" - echo m68010-convergent-sysv - exit ;; - mc68k:UNIX:SYSTEM5:3.51m) - echo m68k-convergent-sysv - exit ;; - M680?0:D-NIX:5.3:*) - echo m68k-diab-dnix - exit ;; - M68*:*:R3V[5678]*:*) - test -r /sysV68 && { echo 'm68k-motorola-sysv'; exit; } ;; - 3[345]??:*:4.0:3.0 | 3[34]??A:*:4.0:3.0 | 3[34]??,*:*:4.0:3.0 | 3[34]??/*:*:4.0:3.0 | 4400:*:4.0:3.0 | 4850:*:4.0:3.0 | SKA40:*:4.0:3.0 | SDS2:*:4.0:3.0 | SHG2:*:4.0:3.0 | S7501*:*:4.0:3.0) - OS_REL='' - test -r /etc/.relid \ - && OS_REL=.`sed -n 's/[^ ]* [^ ]* \([0-9][0-9]\).*/\1/p' < /etc/.relid` - /bin/uname -p 2>/dev/null | grep 86 >/dev/null \ - && { echo i486-ncr-sysv4.3${OS_REL}; exit; } - /bin/uname -p 2>/dev/null | /bin/grep entium >/dev/null \ - && { echo i586-ncr-sysv4.3${OS_REL}; exit; } ;; - 3[34]??:*:4.0:* | 3[34]??,*:*:4.0:*) - /bin/uname -p 2>/dev/null | grep 86 >/dev/null \ - && { echo i486-ncr-sysv4; exit; } ;; - m68*:LynxOS:2.*:* | m68*:LynxOS:3.0*:*) - echo m68k-unknown-lynxos${UNAME_RELEASE} - exit ;; - mc68030:UNIX_System_V:4.*:*) - echo m68k-atari-sysv4 - exit ;; - TSUNAMI:LynxOS:2.*:*) - echo sparc-unknown-lynxos${UNAME_RELEASE} - exit ;; - rs6000:LynxOS:2.*:*) - echo rs6000-unknown-lynxos${UNAME_RELEASE} - exit ;; - PowerPC:LynxOS:2.*:* | PowerPC:LynxOS:3.[01]*:* | PowerPC:LynxOS:4.0*:*) - echo powerpc-unknown-lynxos${UNAME_RELEASE} - exit ;; - SM[BE]S:UNIX_SV:*:*) - echo mips-dde-sysv${UNAME_RELEASE} - exit ;; - RM*:ReliantUNIX-*:*:*) - echo mips-sni-sysv4 - exit ;; - RM*:SINIX-*:*:*) - echo mips-sni-sysv4 - exit ;; - *:SINIX-*:*:*) - if uname -p 2>/dev/null >/dev/null ; then - UNAME_MACHINE=`(uname -p) 2>/dev/null` - echo ${UNAME_MACHINE}-sni-sysv4 - else - echo ns32k-sni-sysv - fi - exit ;; - PENTIUM:*:4.0*:*) # Unisys `ClearPath HMP IX 4000' SVR4/MP effort - # says - echo i586-unisys-sysv4 - exit ;; - *:UNIX_System_V:4*:FTX*) - # From Gerald Hewes . - # How about differentiating between stratus architectures? -djm - echo hppa1.1-stratus-sysv4 - exit ;; - *:*:*:FTX*) - # From seanf@swdc.stratus.com. - echo i860-stratus-sysv4 - exit ;; - i*86:VOS:*:*) - # From Paul.Green@stratus.com. - echo ${UNAME_MACHINE}-stratus-vos - exit ;; - *:VOS:*:*) - # From Paul.Green@stratus.com. - echo hppa1.1-stratus-vos - exit ;; - mc68*:A/UX:*:*) - echo m68k-apple-aux${UNAME_RELEASE} - exit ;; - news*:NEWS-OS:6*:*) - echo mips-sony-newsos6 - exit ;; - R[34]000:*System_V*:*:* | R4000:UNIX_SYSV:*:* | R*000:UNIX_SV:*:*) - if [ -d /usr/nec ]; then - echo mips-nec-sysv${UNAME_RELEASE} - else - echo mips-unknown-sysv${UNAME_RELEASE} - fi - exit ;; - BeBox:BeOS:*:*) # BeOS running on hardware made by Be, PPC only. - echo powerpc-be-beos - exit ;; - BeMac:BeOS:*:*) # BeOS running on Mac or Mac clone, PPC only. - echo powerpc-apple-beos - exit ;; - BePC:BeOS:*:*) # BeOS running on Intel PC compatible. - echo i586-pc-beos - exit ;; - SX-4:SUPER-UX:*:*) - echo sx4-nec-superux${UNAME_RELEASE} - exit ;; - SX-5:SUPER-UX:*:*) - echo sx5-nec-superux${UNAME_RELEASE} - exit ;; - SX-6:SUPER-UX:*:*) - echo sx6-nec-superux${UNAME_RELEASE} - exit ;; - SX-7:SUPER-UX:*:*) - echo sx7-nec-superux${UNAME_RELEASE} - exit ;; - SX-8:SUPER-UX:*:*) - echo sx8-nec-superux${UNAME_RELEASE} - exit ;; - SX-8R:SUPER-UX:*:*) - echo sx8r-nec-superux${UNAME_RELEASE} - exit ;; - Power*:Rhapsody:*:*) - echo powerpc-apple-rhapsody${UNAME_RELEASE} - exit ;; - *:Rhapsody:*:*) - echo ${UNAME_MACHINE}-apple-rhapsody${UNAME_RELEASE} - exit ;; - *:Darwin:*:*) - UNAME_PROCESSOR=`uname -p` || UNAME_PROCESSOR=unknown - case $UNAME_PROCESSOR in - unknown) UNAME_PROCESSOR=powerpc ;; - esac - echo ${UNAME_PROCESSOR}-apple-darwin${UNAME_RELEASE} - exit ;; - *:procnto*:*:* | *:QNX:[0123456789]*:*) - UNAME_PROCESSOR=`uname -p` - if test "$UNAME_PROCESSOR" = "x86"; then - UNAME_PROCESSOR=i386 - UNAME_MACHINE=pc - fi - echo ${UNAME_PROCESSOR}-${UNAME_MACHINE}-nto-qnx${UNAME_RELEASE} - exit ;; - *:QNX:*:4*) - echo i386-pc-qnx - exit ;; - NSE-?:NONSTOP_KERNEL:*:*) - echo nse-tandem-nsk${UNAME_RELEASE} - exit ;; - NSR-?:NONSTOP_KERNEL:*:*) - echo nsr-tandem-nsk${UNAME_RELEASE} - exit ;; - *:NonStop-UX:*:*) - echo mips-compaq-nonstopux - exit ;; - BS2000:POSIX*:*:*) - echo bs2000-siemens-sysv - exit ;; - DS/*:UNIX_System_V:*:*) - echo ${UNAME_MACHINE}-${UNAME_SYSTEM}-${UNAME_RELEASE} - exit ;; - *:Plan9:*:*) - # "uname -m" is not consistent, so use $cputype instead. 386 - # is converted to i386 for consistency with other x86 - # operating systems. - if test "$cputype" = "386"; then - UNAME_MACHINE=i386 - else - UNAME_MACHINE="$cputype" - fi - echo ${UNAME_MACHINE}-unknown-plan9 - exit ;; - *:TOPS-10:*:*) - echo pdp10-unknown-tops10 - exit ;; - *:TENEX:*:*) - echo pdp10-unknown-tenex - exit ;; - KS10:TOPS-20:*:* | KL10:TOPS-20:*:* | TYPE4:TOPS-20:*:*) - echo pdp10-dec-tops20 - exit ;; - XKL-1:TOPS-20:*:* | TYPE5:TOPS-20:*:*) - echo pdp10-xkl-tops20 - exit ;; - *:TOPS-20:*:*) - echo pdp10-unknown-tops20 - exit ;; - *:ITS:*:*) - echo pdp10-unknown-its - exit ;; - SEI:*:*:SEIUX) - echo mips-sei-seiux${UNAME_RELEASE} - exit ;; - *:DragonFly:*:*) - echo ${UNAME_MACHINE}-unknown-dragonfly`echo ${UNAME_RELEASE}|sed -e 's/[-(].*//'` - exit ;; - *:*VMS:*:*) - UNAME_MACHINE=`(uname -p) 2>/dev/null` - case "${UNAME_MACHINE}" in - A*) echo alpha-dec-vms ; exit ;; - I*) echo ia64-dec-vms ; exit ;; - V*) echo vax-dec-vms ; exit ;; - esac ;; - *:XENIX:*:SysV) - echo i386-pc-xenix - exit ;; - i*86:skyos:*:*) - echo ${UNAME_MACHINE}-pc-skyos`echo ${UNAME_RELEASE}` | sed -e 's/ .*$//' - exit ;; - i*86:rdos:*:*) - echo ${UNAME_MACHINE}-pc-rdos - exit ;; -esac - -#echo '(No uname command or uname output not recognized.)' 1>&2 -#echo "${UNAME_MACHINE}:${UNAME_SYSTEM}:${UNAME_RELEASE}:${UNAME_VERSION}" 1>&2 - -eval $set_cc_for_build -cat >$dummy.c < -# include -#endif -main () -{ -#if defined (sony) -#if defined (MIPSEB) - /* BFD wants "bsd" instead of "newsos". Perhaps BFD should be changed, - I don't know.... */ - printf ("mips-sony-bsd\n"); exit (0); -#else -#include - printf ("m68k-sony-newsos%s\n", -#ifdef NEWSOS4 - "4" -#else - "" -#endif - ); exit (0); -#endif -#endif - -#if defined (__arm) && defined (__acorn) && defined (__unix) - printf ("arm-acorn-riscix\n"); exit (0); -#endif - -#if defined (hp300) && !defined (hpux) - printf ("m68k-hp-bsd\n"); exit (0); -#endif - -#if defined (NeXT) -#if !defined (__ARCHITECTURE__) -#define __ARCHITECTURE__ "m68k" -#endif - int version; - version=`(hostinfo | sed -n 's/.*NeXT Mach \([0-9]*\).*/\1/p') 2>/dev/null`; - if (version < 4) - printf ("%s-next-nextstep%d\n", __ARCHITECTURE__, version); - else - printf ("%s-next-openstep%d\n", __ARCHITECTURE__, version); - exit (0); -#endif - -#if defined (MULTIMAX) || defined (n16) -#if defined (UMAXV) - printf ("ns32k-encore-sysv\n"); exit (0); -#else -#if defined (CMU) - printf ("ns32k-encore-mach\n"); exit (0); -#else - printf ("ns32k-encore-bsd\n"); exit (0); -#endif -#endif -#endif - -#if defined (__386BSD__) - printf ("i386-pc-bsd\n"); exit (0); -#endif - -#if defined (sequent) -#if defined (i386) - printf ("i386-sequent-dynix\n"); exit (0); -#endif -#if defined (ns32000) - printf ("ns32k-sequent-dynix\n"); exit (0); -#endif -#endif - -#if defined (_SEQUENT_) - struct utsname un; - - uname(&un); - - if (strncmp(un.version, "V2", 2) == 0) { - printf ("i386-sequent-ptx2\n"); exit (0); - } - if (strncmp(un.version, "V1", 2) == 0) { /* XXX is V1 correct? */ - printf ("i386-sequent-ptx1\n"); exit (0); - } - printf ("i386-sequent-ptx\n"); exit (0); - -#endif - -#if defined (vax) -# if !defined (ultrix) -# include -# if defined (BSD) -# if BSD == 43 - printf ("vax-dec-bsd4.3\n"); exit (0); -# else -# if BSD == 199006 - printf ("vax-dec-bsd4.3reno\n"); exit (0); -# else - printf ("vax-dec-bsd\n"); exit (0); -# endif -# endif -# else - printf ("vax-dec-bsd\n"); exit (0); -# endif -# else - printf ("vax-dec-ultrix\n"); exit (0); -# endif -#endif - -#if defined (alliant) && defined (i860) - printf ("i860-alliant-bsd\n"); exit (0); -#endif - - exit (1); -} -EOF - -$CC_FOR_BUILD -o $dummy $dummy.c 2>/dev/null && SYSTEM_NAME=`$dummy` && - { echo "$SYSTEM_NAME"; exit; } - -# Apollos put the system type in the environment. - -test -d /usr/apollo && { echo ${ISP}-apollo-${SYSTYPE}; exit; } - -# Convex versions that predate uname can use getsysinfo(1) - -if [ -x /usr/convex/getsysinfo ] -then - case `getsysinfo -f cpu_type` in - c1*) - echo c1-convex-bsd - exit ;; - c2*) - if getsysinfo -f scalar_acc - then echo c32-convex-bsd - else echo c2-convex-bsd - fi - exit ;; - c34*) - echo c34-convex-bsd - exit ;; - c38*) - echo c38-convex-bsd - exit ;; - c4*) - echo c4-convex-bsd - exit ;; - esac -fi - -cat >&2 < in order to provide the needed -information to handle your system. - -config.guess timestamp = $timestamp - -uname -m = `(uname -m) 2>/dev/null || echo unknown` -uname -r = `(uname -r) 2>/dev/null || echo unknown` -uname -s = `(uname -s) 2>/dev/null || echo unknown` -uname -v = `(uname -v) 2>/dev/null || echo unknown` - -/usr/bin/uname -p = `(/usr/bin/uname -p) 2>/dev/null` -/bin/uname -X = `(/bin/uname -X) 2>/dev/null` - -hostinfo = `(hostinfo) 2>/dev/null` -/bin/universe = `(/bin/universe) 2>/dev/null` -/usr/bin/arch -k = `(/usr/bin/arch -k) 2>/dev/null` -/bin/arch = `(/bin/arch) 2>/dev/null` -/usr/bin/oslevel = `(/usr/bin/oslevel) 2>/dev/null` -/usr/convex/getsysinfo = `(/usr/convex/getsysinfo) 2>/dev/null` - -UNAME_MACHINE = ${UNAME_MACHINE} -UNAME_RELEASE = ${UNAME_RELEASE} -UNAME_SYSTEM = ${UNAME_SYSTEM} -UNAME_VERSION = ${UNAME_VERSION} -EOF - -exit 1 - -# Local variables: -# eval: (add-hook 'write-file-hooks 'time-stamp) -# time-stamp-start: "timestamp='" -# time-stamp-format: "%:y-%02m-%02d" -# time-stamp-end: "'" -# End: diff --git a/config.h.in b/config.h.in deleted file mode 100644 index 803c5ea81d..0000000000 --- a/config.h.in +++ /dev/null @@ -1,132 +0,0 @@ -/* config.h.in. Generated from configure.ac by autoheader. */ - -/* Define to 1 if you have the header file. */ -#undef HAVE_DLFCN_H - -/* Define to 1 if you have the `fdatasync' function. */ -#undef HAVE_FDATASYNC - -/* Define to 1 if you have the `gmtime_r' function. */ -#undef HAVE_GMTIME_R - -/* Define to 1 if the system has the type `int16_t'. */ -#undef HAVE_INT16_T - -/* Define to 1 if the system has the type `int32_t'. */ -#undef HAVE_INT32_T - -/* Define to 1 if the system has the type `int64_t'. */ -#undef HAVE_INT64_T - -/* Define to 1 if the system has the type `int8_t'. */ -#undef HAVE_INT8_T - -/* Define to 1 if the system has the type `intptr_t'. */ -#undef HAVE_INTPTR_T - -/* Define to 1 if you have the header file. */ -#undef HAVE_INTTYPES_H - -/* Define to 1 if you have the `isnan' function. */ -#undef HAVE_ISNAN - -/* Define to 1 if you have the `crypto' library (-lcrypto). */ -#undef HAVE_LIBCRYPTO - -/* Define to 1 if you have the `tomcrypt' library (-ltomcrypt). */ -#undef HAVE_LIBTOMCRYPT - -/* Define to 1 if you have the `localtime_r' function. */ -#undef HAVE_LOCALTIME_R - -/* Define to 1 if you have the `localtime_s' function. */ -#undef HAVE_LOCALTIME_S - -/* Define to 1 if you have the header file. */ -#undef HAVE_MALLOC_H - -/* Define to 1 if you have the `malloc_usable_size' function. */ -#undef HAVE_MALLOC_USABLE_SIZE - -/* Define to 1 if you have the header file. */ -#undef HAVE_MEMORY_H - -/* Define to 1 if you have the header file. */ -#undef HAVE_STDINT_H - -/* Define to 1 if you have the header file. */ -#undef HAVE_STDLIB_H - -/* Define to 1 if you have the `strchrnul' function. */ -#undef HAVE_STRCHRNUL - -/* Define to 1 if you have the header file. */ -#undef HAVE_STRINGS_H - -/* Define to 1 if you have the header file. */ -#undef HAVE_STRING_H - -/* Define to 1 if you have the header file. */ -#undef HAVE_SYS_STAT_H - -/* Define to 1 if you have the header file. */ -#undef HAVE_SYS_TYPES_H - -/* Define to 1 if the system has the type `uint16_t'. */ -#undef HAVE_UINT16_T - -/* Define to 1 if the system has the type `uint32_t'. */ -#undef HAVE_UINT32_T - -/* Define to 1 if the system has the type `uint64_t'. */ -#undef HAVE_UINT64_T - -/* Define to 1 if the system has the type `uint8_t'. */ -#undef HAVE_UINT8_T - -/* Define to 1 if the system has the type `uintptr_t'. */ -#undef HAVE_UINTPTR_T - -/* Define to 1 if you have the header file. */ -#undef HAVE_UNISTD_H - -/* Define to 1 if you have the `usleep' function. */ -#undef HAVE_USLEEP - -/* Define to 1 if you have the `utime' function. */ -#undef HAVE_UTIME - -/* Define to the sub-directory where libtool stores uninstalled libraries. */ -#undef LT_OBJDIR - -/* Define to the address where bug reports for this package should be sent. */ -#undef PACKAGE_BUGREPORT - -/* Define to the full name of this package. */ -#undef PACKAGE_NAME - -/* Define to the full name and version of this package. */ -#undef PACKAGE_STRING - -/* Define to the one symbol short name of this package. */ -#undef PACKAGE_TARNAME - -/* Define to the home page for this package. */ -#undef PACKAGE_URL - -/* Define to the version of this package. */ -#undef PACKAGE_VERSION - -/* Define to 1 if you have the ANSI C header files. */ -#undef STDC_HEADERS - -/* Enable large inode numbers on Mac OS X 10.5. */ -#ifndef _DARWIN_USE_64_BIT_INODE -# define _DARWIN_USE_64_BIT_INODE 1 -#endif - -/* Number of bits in a file offset, on hosts where this is settable. */ -#undef _FILE_OFFSET_BITS - -/* Define for large files, on AIX-style hosts. */ -#undef _LARGE_FILES diff --git a/config.sub b/config.sub deleted file mode 100644 index 63cdd0a35d..0000000000 --- a/config.sub +++ /dev/null @@ -1,1644 +0,0 @@ -#! /bin/sh -# Configuration validation subroutine script. -# Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, -# 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, -# Inc. - -timestamp='2007-06-28' - -# This file is (in principle) common to ALL GNU software. -# The presence of a machine in this file suggests that SOME GNU software -# can handle that machine. It does not imply ALL GNU software can. -# -# This file is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA -# 02110-1301, USA. -# -# As a special exception to the GNU General Public License, if you -# distribute this file as part of a program that contains a -# configuration script generated by Autoconf, you may include it under -# the same distribution terms that you use for the rest of that program. - - -# Please send patches to . Submit a context -# diff and a properly formatted ChangeLog entry. -# -# Configuration subroutine to validate and canonicalize a configuration type. -# Supply the specified configuration type as an argument. -# If it is invalid, we print an error message on stderr and exit with code 1. -# Otherwise, we print the canonical config type on stdout and succeed. - -# This file is supposed to be the same for all GNU packages -# and recognize all the CPU types, system types and aliases -# that are meaningful with *any* GNU software. -# Each package is responsible for reporting which valid configurations -# it does not support. The user should be able to distinguish -# a failure to support a valid configuration from a meaningless -# configuration. - -# The goal of this file is to map all the various variations of a given -# machine specification into a single specification in the form: -# CPU_TYPE-MANUFACTURER-OPERATING_SYSTEM -# or in some cases, the newer four-part form: -# CPU_TYPE-MANUFACTURER-KERNEL-OPERATING_SYSTEM -# It is wrong to echo any other type of specification. - -me=`echo "$0" | sed -e 's,.*/,,'` - -usage="\ -Usage: $0 [OPTION] CPU-MFR-OPSYS - $0 [OPTION] ALIAS - -Canonicalize a configuration name. - -Operation modes: - -h, --help print this help, then exit - -t, --time-stamp print date of last modification, then exit - -v, --version print version number, then exit - -Report bugs and patches to ." - -version="\ -GNU config.sub ($timestamp) - -Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 -Free Software Foundation, Inc. - -This is free software; see the source for copying conditions. There is NO -warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." - -help=" -Try \`$me --help' for more information." - -# Parse command line -while test $# -gt 0 ; do - case $1 in - --time-stamp | --time* | -t ) - echo "$timestamp" ; exit ;; - --version | -v ) - echo "$version" ; exit ;; - --help | --h* | -h ) - echo "$usage"; exit ;; - -- ) # Stop option processing - shift; break ;; - - ) # Use stdin as input. - break ;; - -* ) - echo "$me: invalid option $1$help" - exit 1 ;; - - *local*) - # First pass through any local machine types. - echo $1 - exit ;; - - * ) - break ;; - esac -done - -case $# in - 0) echo "$me: missing argument$help" >&2 - exit 1;; - 1) ;; - *) echo "$me: too many arguments$help" >&2 - exit 1;; -esac - -# Separate what the user gave into CPU-COMPANY and OS or KERNEL-OS (if any). -# Here we must recognize all the valid KERNEL-OS combinations. -maybe_os=`echo $1 | sed 's/^\(.*\)-\([^-]*-[^-]*\)$/\2/'` -case $maybe_os in - nto-qnx* | linux-gnu* | linux-dietlibc | linux-newlib* | linux-uclibc* | \ - uclinux-uclibc* | uclinux-gnu* | kfreebsd*-gnu* | knetbsd*-gnu* | netbsd*-gnu* | \ - storm-chaos* | os2-emx* | rtmk-nova*) - os=-$maybe_os - basic_machine=`echo $1 | sed 's/^\(.*\)-\([^-]*-[^-]*\)$/\1/'` - ;; - *) - basic_machine=`echo $1 | sed 's/-[^-]*$//'` - if [ $basic_machine != $1 ] - then os=`echo $1 | sed 's/.*-/-/'` - else os=; fi - ;; -esac - -### Let's recognize common machines as not being operating systems so -### that things like config.sub decstation-3100 work. We also -### recognize some manufacturers as not being operating systems, so we -### can provide default operating systems below. -case $os in - -sun*os*) - # Prevent following clause from handling this invalid input. - ;; - -dec* | -mips* | -sequent* | -encore* | -pc532* | -sgi* | -sony* | \ - -att* | -7300* | -3300* | -delta* | -motorola* | -sun[234]* | \ - -unicom* | -ibm* | -next | -hp | -isi* | -apollo | -altos* | \ - -convergent* | -ncr* | -news | -32* | -3600* | -3100* | -hitachi* |\ - -c[123]* | -convex* | -sun | -crds | -omron* | -dg | -ultra | -tti* | \ - -harris | -dolphin | -highlevel | -gould | -cbm | -ns | -masscomp | \ - -apple | -axis | -knuth | -cray) - os= - basic_machine=$1 - ;; - -sim | -cisco | -oki | -wec | -winbond) - os= - basic_machine=$1 - ;; - -scout) - ;; - -wrs) - os=-vxworks - basic_machine=$1 - ;; - -chorusos*) - os=-chorusos - basic_machine=$1 - ;; - -chorusrdb) - os=-chorusrdb - basic_machine=$1 - ;; - -hiux*) - os=-hiuxwe2 - ;; - -sco6) - os=-sco5v6 - basic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'` - ;; - -sco5) - os=-sco3.2v5 - basic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'` - ;; - -sco4) - os=-sco3.2v4 - basic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'` - ;; - -sco3.2.[4-9]*) - os=`echo $os | sed -e 's/sco3.2./sco3.2v/'` - basic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'` - ;; - -sco3.2v[4-9]*) - # Don't forget version if it is 3.2v4 or newer. - basic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'` - ;; - -sco5v6*) - # Don't forget version if it is 3.2v4 or newer. - basic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'` - ;; - -sco*) - os=-sco3.2v2 - basic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'` - ;; - -udk*) - basic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'` - ;; - -isc) - os=-isc2.2 - basic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'` - ;; - -clix*) - basic_machine=clipper-intergraph - ;; - -isc*) - basic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'` - ;; - -lynx*) - os=-lynxos - ;; - -ptx*) - basic_machine=`echo $1 | sed -e 's/86-.*/86-sequent/'` - ;; - -windowsnt*) - os=`echo $os | sed -e 's/windowsnt/winnt/'` - ;; - -psos*) - os=-psos - ;; - -mint | -mint[0-9]*) - basic_machine=m68k-atari - os=-mint - ;; -esac - -# Decode aliases for certain CPU-COMPANY combinations. -case $basic_machine in - # Recognize the basic CPU types without company name. - # Some are omitted here because they have special meanings below. - 1750a | 580 \ - | a29k \ - | alpha | alphaev[4-8] | alphaev56 | alphaev6[78] | alphapca5[67] \ - | alpha64 | alpha64ev[4-8] | alpha64ev56 | alpha64ev6[78] | alpha64pca5[67] \ - | am33_2.0 \ - | arc | arm | arm[bl]e | arme[lb] | armv[2345] | armv[345][lb] | avr | avr32 \ - | bfin \ - | c4x | clipper \ - | d10v | d30v | dlx | dsp16xx | dvp \ - | fido | fr30 | frv \ - | h8300 | h8500 | hppa | hppa1.[01] | hppa2.0 | hppa2.0[nw] | hppa64 \ - | i370 | i860 | i960 | ia64 \ - | ip2k | iq2000 \ - | m32c | m32r | m32rle | m68000 | m68k | m88k \ - | maxq | mb | microblaze | mcore | mep \ - | mips | mipsbe | mipseb | mipsel | mipsle \ - | mips16 \ - | mips64 | mips64el \ - | mips64vr | mips64vrel \ - | mips64orion | mips64orionel \ - | mips64vr4100 | mips64vr4100el \ - | mips64vr4300 | mips64vr4300el \ - | mips64vr5000 | mips64vr5000el \ - | mips64vr5900 | mips64vr5900el \ - | mipsisa32 | mipsisa32el \ - | mipsisa32r2 | mipsisa32r2el \ - | mipsisa64 | mipsisa64el \ - | mipsisa64r2 | mipsisa64r2el \ - | mipsisa64sb1 | mipsisa64sb1el \ - | mipsisa64sr71k | mipsisa64sr71kel \ - | mipstx39 | mipstx39el \ - | mn10200 | mn10300 \ - | mt \ - | msp430 \ - | nios | nios2 \ - | ns16k | ns32k \ - | or32 \ - | pdp10 | pdp11 | pj | pjl \ - | powerpc | powerpc64 | powerpc64le | powerpcle | ppcbe \ - | pyramid \ - | score \ - | sh | sh[1234] | sh[24]a | sh[24]a*eb | sh[23]e | sh[34]eb | sheb | shbe | shle | sh[1234]le | sh3ele \ - | sh64 | sh64le \ - | sparc | sparc64 | sparc64b | sparc64v | sparc86x | sparclet | sparclite \ - | sparcv8 | sparcv9 | sparcv9b | sparcv9v \ - | spu | strongarm \ - | tahoe | thumb | tic4x | tic80 | tron \ - | v850 | v850e \ - | we32k \ - | x86 | xc16x | xscale | xscalee[bl] | xstormy16 | xtensa \ - | z8k) - basic_machine=$basic_machine-unknown - ;; - m6811 | m68hc11 | m6812 | m68hc12) - # Motorola 68HC11/12. - basic_machine=$basic_machine-unknown - os=-none - ;; - m88110 | m680[12346]0 | m683?2 | m68360 | m5200 | v70 | w65 | z8k) - ;; - ms1) - basic_machine=mt-unknown - ;; - - # We use `pc' rather than `unknown' - # because (1) that's what they normally are, and - # (2) the word "unknown" tends to confuse beginning users. - i*86 | x86_64) - basic_machine=$basic_machine-pc - ;; - # Object if more than one company name word. - *-*-*) - echo Invalid configuration \`$1\': machine \`$basic_machine\' not recognized 1>&2 - exit 1 - ;; - # Recognize the basic CPU types with company name. - 580-* \ - | a29k-* \ - | alpha-* | alphaev[4-8]-* | alphaev56-* | alphaev6[78]-* \ - | alpha64-* | alpha64ev[4-8]-* | alpha64ev56-* | alpha64ev6[78]-* \ - | alphapca5[67]-* | alpha64pca5[67]-* | arc-* \ - | arm-* | armbe-* | armle-* | armeb-* | armv*-* \ - | avr-* | avr32-* \ - | bfin-* | bs2000-* \ - | c[123]* | c30-* | [cjt]90-* | c4x-* | c54x-* | c55x-* | c6x-* \ - | clipper-* | craynv-* | cydra-* \ - | d10v-* | d30v-* | dlx-* \ - | elxsi-* \ - | f30[01]-* | f700-* | fido-* | fr30-* | frv-* | fx80-* \ - | h8300-* | h8500-* \ - | hppa-* | hppa1.[01]-* | hppa2.0-* | hppa2.0[nw]-* | hppa64-* \ - | i*86-* | i860-* | i960-* | ia64-* \ - | ip2k-* | iq2000-* \ - | m32c-* | m32r-* | m32rle-* \ - | m68000-* | m680[012346]0-* | m68360-* | m683?2-* | m68k-* \ - | m88110-* | m88k-* | maxq-* | mcore-* \ - | mips-* | mipsbe-* | mipseb-* | mipsel-* | mipsle-* \ - | mips16-* \ - | mips64-* | mips64el-* \ - | mips64vr-* | mips64vrel-* \ - | mips64orion-* | mips64orionel-* \ - | mips64vr4100-* | mips64vr4100el-* \ - | mips64vr4300-* | mips64vr4300el-* \ - | mips64vr5000-* | mips64vr5000el-* \ - | mips64vr5900-* | mips64vr5900el-* \ - | mipsisa32-* | mipsisa32el-* \ - | mipsisa32r2-* | mipsisa32r2el-* \ - | mipsisa64-* | mipsisa64el-* \ - | mipsisa64r2-* | mipsisa64r2el-* \ - | mipsisa64sb1-* | mipsisa64sb1el-* \ - | mipsisa64sr71k-* | mipsisa64sr71kel-* \ - | mipstx39-* | mipstx39el-* \ - | mmix-* \ - | mt-* \ - | msp430-* \ - | nios-* | nios2-* \ - | none-* | np1-* | ns16k-* | ns32k-* \ - | orion-* \ - | pdp10-* | pdp11-* | pj-* | pjl-* | pn-* | power-* \ - | powerpc-* | powerpc64-* | powerpc64le-* | powerpcle-* | ppcbe-* \ - | pyramid-* \ - | romp-* | rs6000-* \ - | sh-* | sh[1234]-* | sh[24]a-* | sh[24]a*eb-* | sh[23]e-* | sh[34]eb-* | sheb-* | shbe-* \ - | shle-* | sh[1234]le-* | sh3ele-* | sh64-* | sh64le-* \ - | sparc-* | sparc64-* | sparc64b-* | sparc64v-* | sparc86x-* | sparclet-* \ - | sparclite-* \ - | sparcv8-* | sparcv9-* | sparcv9b-* | sparcv9v-* | strongarm-* | sv1-* | sx?-* \ - | tahoe-* | thumb-* \ - | tic30-* | tic4x-* | tic54x-* | tic55x-* | tic6x-* | tic80-* \ - | tron-* \ - | v850-* | v850e-* | vax-* \ - | we32k-* \ - | x86-* | x86_64-* | xc16x-* | xps100-* | xscale-* | xscalee[bl]-* \ - | xstormy16-* | xtensa-* \ - | ymp-* \ - | z8k-*) - ;; - # Recognize the various machine names and aliases which stand - # for a CPU type and a company and sometimes even an OS. - 386bsd) - basic_machine=i386-unknown - os=-bsd - ;; - 3b1 | 7300 | 7300-att | att-7300 | pc7300 | safari | unixpc) - basic_machine=m68000-att - ;; - 3b*) - basic_machine=we32k-att - ;; - a29khif) - basic_machine=a29k-amd - os=-udi - ;; - abacus) - basic_machine=abacus-unknown - ;; - adobe68k) - basic_machine=m68010-adobe - os=-scout - ;; - alliant | fx80) - basic_machine=fx80-alliant - ;; - altos | altos3068) - basic_machine=m68k-altos - ;; - am29k) - basic_machine=a29k-none - os=-bsd - ;; - amd64) - basic_machine=x86_64-pc - ;; - amd64-*) - basic_machine=x86_64-`echo $basic_machine | sed 's/^[^-]*-//'` - ;; - amdahl) - basic_machine=580-amdahl - os=-sysv - ;; - amiga | amiga-*) - basic_machine=m68k-unknown - ;; - amigaos | amigados) - basic_machine=m68k-unknown - os=-amigaos - ;; - amigaunix | amix) - basic_machine=m68k-unknown - os=-sysv4 - ;; - apollo68) - basic_machine=m68k-apollo - os=-sysv - ;; - apollo68bsd) - basic_machine=m68k-apollo - os=-bsd - ;; - aux) - basic_machine=m68k-apple - os=-aux - ;; - balance) - basic_machine=ns32k-sequent - os=-dynix - ;; - c90) - basic_machine=c90-cray - os=-unicos - ;; - convex-c1) - basic_machine=c1-convex - os=-bsd - ;; - convex-c2) - basic_machine=c2-convex - os=-bsd - ;; - convex-c32) - basic_machine=c32-convex - os=-bsd - ;; - convex-c34) - basic_machine=c34-convex - os=-bsd - ;; - convex-c38) - basic_machine=c38-convex - os=-bsd - ;; - cray | j90) - basic_machine=j90-cray - os=-unicos - ;; - craynv) - basic_machine=craynv-cray - os=-unicosmp - ;; - cr16) - basic_machine=cr16-unknown - os=-elf - ;; - crds | unos) - basic_machine=m68k-crds - ;; - crisv32 | crisv32-* | etraxfs*) - basic_machine=crisv32-axis - ;; - cris | cris-* | etrax*) - basic_machine=cris-axis - ;; - crx) - basic_machine=crx-unknown - os=-elf - ;; - da30 | da30-*) - basic_machine=m68k-da30 - ;; - decstation | decstation-3100 | pmax | pmax-* | pmin | dec3100 | decstatn) - basic_machine=mips-dec - ;; - decsystem10* | dec10*) - basic_machine=pdp10-dec - os=-tops10 - ;; - decsystem20* | dec20*) - basic_machine=pdp10-dec - os=-tops20 - ;; - delta | 3300 | motorola-3300 | motorola-delta \ - | 3300-motorola | delta-motorola) - basic_machine=m68k-motorola - ;; - delta88) - basic_machine=m88k-motorola - os=-sysv3 - ;; - djgpp) - basic_machine=i586-pc - os=-msdosdjgpp - ;; - dpx20 | dpx20-*) - basic_machine=rs6000-bull - os=-bosx - ;; - dpx2* | dpx2*-bull) - basic_machine=m68k-bull - os=-sysv3 - ;; - ebmon29k) - basic_machine=a29k-amd - os=-ebmon - ;; - elxsi) - basic_machine=elxsi-elxsi - os=-bsd - ;; - encore | umax | mmax) - basic_machine=ns32k-encore - ;; - es1800 | OSE68k | ose68k | ose | OSE) - basic_machine=m68k-ericsson - os=-ose - ;; - fx2800) - basic_machine=i860-alliant - ;; - genix) - basic_machine=ns32k-ns - ;; - gmicro) - basic_machine=tron-gmicro - os=-sysv - ;; - go32) - basic_machine=i386-pc - os=-go32 - ;; - h3050r* | hiux*) - basic_machine=hppa1.1-hitachi - os=-hiuxwe2 - ;; - h8300hms) - basic_machine=h8300-hitachi - os=-hms - ;; - h8300xray) - basic_machine=h8300-hitachi - os=-xray - ;; - h8500hms) - basic_machine=h8500-hitachi - os=-hms - ;; - harris) - basic_machine=m88k-harris - os=-sysv3 - ;; - hp300-*) - basic_machine=m68k-hp - ;; - hp300bsd) - basic_machine=m68k-hp - os=-bsd - ;; - hp300hpux) - basic_machine=m68k-hp - os=-hpux - ;; - hp3k9[0-9][0-9] | hp9[0-9][0-9]) - basic_machine=hppa1.0-hp - ;; - hp9k2[0-9][0-9] | hp9k31[0-9]) - basic_machine=m68000-hp - ;; - hp9k3[2-9][0-9]) - basic_machine=m68k-hp - ;; - hp9k6[0-9][0-9] | hp6[0-9][0-9]) - basic_machine=hppa1.0-hp - ;; - hp9k7[0-79][0-9] | hp7[0-79][0-9]) - basic_machine=hppa1.1-hp - ;; - hp9k78[0-9] | hp78[0-9]) - # FIXME: really hppa2.0-hp - basic_machine=hppa1.1-hp - ;; - hp9k8[67]1 | hp8[67]1 | hp9k80[24] | hp80[24] | hp9k8[78]9 | hp8[78]9 | hp9k893 | hp893) - # FIXME: really hppa2.0-hp - basic_machine=hppa1.1-hp - ;; - hp9k8[0-9][13679] | hp8[0-9][13679]) - basic_machine=hppa1.1-hp - ;; - hp9k8[0-9][0-9] | hp8[0-9][0-9]) - basic_machine=hppa1.0-hp - ;; - hppa-next) - os=-nextstep3 - ;; - hppaosf) - basic_machine=hppa1.1-hp - os=-osf - ;; - hppro) - basic_machine=hppa1.1-hp - os=-proelf - ;; - i370-ibm* | ibm*) - basic_machine=i370-ibm - ;; -# I'm not sure what "Sysv32" means. Should this be sysv3.2? - i*86v32) - basic_machine=`echo $1 | sed -e 's/86.*/86-pc/'` - os=-sysv32 - ;; - i*86v4*) - basic_machine=`echo $1 | sed -e 's/86.*/86-pc/'` - os=-sysv4 - ;; - i*86v) - basic_machine=`echo $1 | sed -e 's/86.*/86-pc/'` - os=-sysv - ;; - i*86sol2) - basic_machine=`echo $1 | sed -e 's/86.*/86-pc/'` - os=-solaris2 - ;; - i386mach) - basic_machine=i386-mach - os=-mach - ;; - i386-vsta | vsta) - basic_machine=i386-unknown - os=-vsta - ;; - iris | iris4d) - basic_machine=mips-sgi - case $os in - -irix*) - ;; - *) - os=-irix4 - ;; - esac - ;; - isi68 | isi) - basic_machine=m68k-isi - os=-sysv - ;; - m88k-omron*) - basic_machine=m88k-omron - ;; - magnum | m3230) - basic_machine=mips-mips - os=-sysv - ;; - merlin) - basic_machine=ns32k-utek - os=-sysv - ;; - mingw32) - basic_machine=i386-pc - os=-mingw32 - ;; - mingw32ce) - basic_machine=arm-unknown - os=-mingw32ce - ;; - miniframe) - basic_machine=m68000-convergent - ;; - *mint | -mint[0-9]* | *MiNT | *MiNT[0-9]*) - basic_machine=m68k-atari - os=-mint - ;; - mipsEE* | ee | ps2) - basic_machine=mips64r5900el-scei - case $os in - -linux*) - ;; - *) - os=-elf - ;; - esac - ;; - iop) - basic_machine=mipsel-scei - os=-irx - ;; - dvp) - basic_machine=dvp-scei - os=-elf - ;; - mips3*-*) - basic_machine=`echo $basic_machine | sed -e 's/mips3/mips64/'` - ;; - mips3*) - basic_machine=`echo $basic_machine | sed -e 's/mips3/mips64/'`-unknown - ;; - monitor) - basic_machine=m68k-rom68k - os=-coff - ;; - morphos) - basic_machine=powerpc-unknown - os=-morphos - ;; - msdos) - basic_machine=i386-pc - os=-msdos - ;; - ms1-*) - basic_machine=`echo $basic_machine | sed -e 's/ms1-/mt-/'` - ;; - mvs) - basic_machine=i370-ibm - os=-mvs - ;; - ncr3000) - basic_machine=i486-ncr - os=-sysv4 - ;; - netbsd386) - basic_machine=i386-unknown - os=-netbsd - ;; - netwinder) - basic_machine=armv4l-rebel - os=-linux - ;; - news | news700 | news800 | news900) - basic_machine=m68k-sony - os=-newsos - ;; - news1000) - basic_machine=m68030-sony - os=-newsos - ;; - news-3600 | risc-news) - basic_machine=mips-sony - os=-newsos - ;; - necv70) - basic_machine=v70-nec - os=-sysv - ;; - next | m*-next ) - basic_machine=m68k-next - case $os in - -nextstep* ) - ;; - -ns2*) - os=-nextstep2 - ;; - *) - os=-nextstep3 - ;; - esac - ;; - nh3000) - basic_machine=m68k-harris - os=-cxux - ;; - nh[45]000) - basic_machine=m88k-harris - os=-cxux - ;; - nindy960) - basic_machine=i960-intel - os=-nindy - ;; - mon960) - basic_machine=i960-intel - os=-mon960 - ;; - nonstopux) - basic_machine=mips-compaq - os=-nonstopux - ;; - np1) - basic_machine=np1-gould - ;; - nsr-tandem) - basic_machine=nsr-tandem - ;; - op50n-* | op60c-*) - basic_machine=hppa1.1-oki - os=-proelf - ;; - openrisc | openrisc-*) - basic_machine=or32-unknown - ;; - os400) - basic_machine=powerpc-ibm - os=-os400 - ;; - OSE68000 | ose68000) - basic_machine=m68000-ericsson - os=-ose - ;; - os68k) - basic_machine=m68k-none - os=-os68k - ;; - pa-hitachi) - basic_machine=hppa1.1-hitachi - os=-hiuxwe2 - ;; - paragon) - basic_machine=i860-intel - os=-osf - ;; - pbd) - basic_machine=sparc-tti - ;; - pbb) - basic_machine=m68k-tti - ;; - pc532 | pc532-*) - basic_machine=ns32k-pc532 - ;; - pc98) - basic_machine=i386-pc - ;; - pc98-*) - basic_machine=i386-`echo $basic_machine | sed 's/^[^-]*-//'` - ;; - pentium | p5 | k5 | k6 | nexgen | viac3) - basic_machine=i586-pc - ;; - pentiumpro | p6 | 6x86 | athlon | athlon_*) - basic_machine=i686-pc - ;; - pentiumii | pentium2 | pentiumiii | pentium3) - basic_machine=i686-pc - ;; - pentium4) - basic_machine=i786-pc - ;; - pentium-* | p5-* | k5-* | k6-* | nexgen-* | viac3-*) - basic_machine=i586-`echo $basic_machine | sed 's/^[^-]*-//'` - ;; - pentiumpro-* | p6-* | 6x86-* | athlon-*) - basic_machine=i686-`echo $basic_machine | sed 's/^[^-]*-//'` - ;; - pentiumii-* | pentium2-* | pentiumiii-* | pentium3-*) - basic_machine=i686-`echo $basic_machine | sed 's/^[^-]*-//'` - ;; - pentium4-*) - basic_machine=i786-`echo $basic_machine | sed 's/^[^-]*-//'` - ;; - pn) - basic_machine=pn-gould - ;; - power) basic_machine=power-ibm - ;; - ppc) basic_machine=powerpc-unknown - ;; - ppc-*) basic_machine=powerpc-`echo $basic_machine | sed 's/^[^-]*-//'` - ;; - ppcle | powerpclittle | ppc-le | powerpc-little) - basic_machine=powerpcle-unknown - ;; - ppcle-* | powerpclittle-*) - basic_machine=powerpcle-`echo $basic_machine | sed 's/^[^-]*-//'` - ;; - ppc64) basic_machine=powerpc64-unknown - ;; - ppc64-*) basic_machine=powerpc64-`echo $basic_machine | sed 's/^[^-]*-//'` - ;; - ppc64le | powerpc64little | ppc64-le | powerpc64-little) - basic_machine=powerpc64le-unknown - ;; - ppc64le-* | powerpc64little-*) - basic_machine=powerpc64le-`echo $basic_machine | sed 's/^[^-]*-//'` - ;; - ps2) - basic_machine=i386-ibm - ;; - pw32) - basic_machine=i586-unknown - os=-pw32 - ;; - rdos) - basic_machine=i386-pc - os=-rdos - ;; - rom68k) - basic_machine=m68k-rom68k - os=-coff - ;; - rm[46]00) - basic_machine=mips-siemens - ;; - rtpc | rtpc-*) - basic_machine=romp-ibm - ;; - s390 | s390-*) - basic_machine=s390-ibm - ;; - s390x | s390x-*) - basic_machine=s390x-ibm - ;; - sa29200) - basic_machine=a29k-amd - os=-udi - ;; - sb1) - basic_machine=mipsisa64sb1-unknown - ;; - sb1el) - basic_machine=mipsisa64sb1el-unknown - ;; - sde) - basic_machine=mipsisa32-sde - os=-elf - ;; - sei) - basic_machine=mips-sei - os=-seiux - ;; - sequent) - basic_machine=i386-sequent - ;; - sh) - basic_machine=sh-hitachi - os=-hms - ;; - sh5el) - basic_machine=sh5le-unknown - ;; - sh64) - basic_machine=sh64-unknown - ;; - sparclite-wrs | simso-wrs) - basic_machine=sparclite-wrs - os=-vxworks - ;; - sps7) - basic_machine=m68k-bull - os=-sysv2 - ;; - spur) - basic_machine=spur-unknown - ;; - st2000) - basic_machine=m68k-tandem - ;; - stratus) - basic_machine=i860-stratus - os=-sysv4 - ;; - sun2) - basic_machine=m68000-sun - ;; - sun2os3) - basic_machine=m68000-sun - os=-sunos3 - ;; - sun2os4) - basic_machine=m68000-sun - os=-sunos4 - ;; - sun3os3) - basic_machine=m68k-sun - os=-sunos3 - ;; - sun3os4) - basic_machine=m68k-sun - os=-sunos4 - ;; - sun4os3) - basic_machine=sparc-sun - os=-sunos3 - ;; - sun4os4) - basic_machine=sparc-sun - os=-sunos4 - ;; - sun4sol2) - basic_machine=sparc-sun - os=-solaris2 - ;; - sun3 | sun3-*) - basic_machine=m68k-sun - ;; - sun4) - basic_machine=sparc-sun - ;; - sun386 | sun386i | roadrunner) - basic_machine=i386-sun - ;; - sv1) - basic_machine=sv1-cray - os=-unicos - ;; - symmetry) - basic_machine=i386-sequent - os=-dynix - ;; - t3e) - basic_machine=alphaev5-cray - os=-unicos - ;; - t90) - basic_machine=t90-cray - os=-unicos - ;; - tic54x | c54x*) - basic_machine=tic54x-unknown - os=-coff - ;; - tic55x | c55x*) - basic_machine=tic55x-unknown - os=-coff - ;; - tic6x | c6x*) - basic_machine=tic6x-unknown - os=-coff - ;; - tx39) - basic_machine=mipstx39-unknown - ;; - tx39el) - basic_machine=mipstx39el-unknown - ;; - toad1) - basic_machine=pdp10-xkl - os=-tops20 - ;; - tower | tower-32) - basic_machine=m68k-ncr - ;; - tpf) - basic_machine=s390x-ibm - os=-tpf - ;; - udi29k) - basic_machine=a29k-amd - os=-udi - ;; - ultra3) - basic_machine=a29k-nyu - os=-sym1 - ;; - v810 | necv810) - basic_machine=v810-nec - os=-none - ;; - vaxv) - basic_machine=vax-dec - os=-sysv - ;; - vms) - basic_machine=vax-dec - os=-vms - ;; - vpp*|vx|vx-*) - basic_machine=f301-fujitsu - ;; - vxworks960) - basic_machine=i960-wrs - os=-vxworks - ;; - vxworks68) - basic_machine=m68k-wrs - os=-vxworks - ;; - vxworks29k) - basic_machine=a29k-wrs - os=-vxworks - ;; - w65*) - basic_machine=w65-wdc - os=-none - ;; - w89k-*) - basic_machine=hppa1.1-winbond - os=-proelf - ;; - xbox) - basic_machine=i686-pc - os=-mingw32 - ;; - xps | xps100) - basic_machine=xps100-honeywell - ;; - ymp) - basic_machine=ymp-cray - os=-unicos - ;; - z8k-*-coff) - basic_machine=z8k-unknown - os=-sim - ;; - none) - basic_machine=none-none - os=-none - ;; - -# Here we handle the default manufacturer of certain CPU types. It is in -# some cases the only manufacturer, in others, it is the most popular. - w89k) - basic_machine=hppa1.1-winbond - ;; - op50n) - basic_machine=hppa1.1-oki - ;; - op60c) - basic_machine=hppa1.1-oki - ;; - romp) - basic_machine=romp-ibm - ;; - mmix) - basic_machine=mmix-knuth - ;; - rs6000) - basic_machine=rs6000-ibm - ;; - vax) - basic_machine=vax-dec - ;; - pdp10) - # there are many clones, so DEC is not a safe bet - basic_machine=pdp10-unknown - ;; - pdp11) - basic_machine=pdp11-dec - ;; - we32k) - basic_machine=we32k-att - ;; - sh[1234] | sh[24]a | sh[34]eb | sh[1234]le | sh[23]ele) - basic_machine=sh-unknown - ;; - sparc | sparcv8 | sparcv9 | sparcv9b | sparcv9v) - basic_machine=sparc-sun - ;; - cydra) - basic_machine=cydra-cydrome - ;; - orion) - basic_machine=orion-highlevel - ;; - orion105) - basic_machine=clipper-highlevel - ;; - mac | mpw | mac-mpw) - basic_machine=m68k-apple - ;; - pmac | pmac-mpw) - basic_machine=powerpc-apple - ;; - *-unknown) - # Make sure to match an already-canonicalized machine name. - ;; - *) - echo Invalid configuration \`$1\': machine \`$basic_machine\' not recognized 1>&2 - exit 1 - ;; -esac - -# Here we canonicalize certain aliases for manufacturers. -case $basic_machine in - *-digital*) - basic_machine=`echo $basic_machine | sed 's/digital.*/dec/'` - ;; - *-commodore*) - basic_machine=`echo $basic_machine | sed 's/commodore.*/cbm/'` - ;; - *) - ;; -esac - -# Decode manufacturer-specific aliases for certain operating systems. - -if [ x"$os" != x"" ] -then -case $os in - # First match some system type aliases - # that might get confused with valid system types. - # -solaris* is a basic system type, with this one exception. - -solaris1 | -solaris1.*) - os=`echo $os | sed -e 's|solaris1|sunos4|'` - ;; - -solaris) - os=-solaris2 - ;; - -svr4*) - os=-sysv4 - ;; - -unixware*) - os=-sysv4.2uw - ;; - -gnu/linux*) - os=`echo $os | sed -e 's|gnu/linux|linux-gnu|'` - ;; - # First accept the basic system types. - # The portable systems comes first. - # Each alternative MUST END IN A *, to match a version number. - # -sysv* is not here because it comes later, after sysvr4. - -gnu* | -bsd* | -mach* | -minix* | -genix* | -ultrix* | -irix* \ - | -*vms* | -sco* | -esix* | -isc* | -aix* | -sunos | -sunos[34]*\ - | -hpux* | -unos* | -osf* | -luna* | -dgux* | -solaris* | -sym* \ - | -amigaos* | -amigados* | -msdos* | -newsos* | -unicos* | -aof* \ - | -aos* \ - | -nindy* | -vxsim* | -vxworks* | -ebmon* | -hms* | -mvs* \ - | -clix* | -riscos* | -uniplus* | -iris* | -rtu* | -xenix* \ - | -hiux* | -386bsd* | -knetbsd* | -mirbsd* | -netbsd* \ - | -openbsd* | -solidbsd* \ - | -ekkobsd* | -kfreebsd* | -freebsd* | -riscix* | -lynxos* \ - | -bosx* | -nextstep* | -cxux* | -aout* | -elf* | -oabi* \ - | -ptx* | -coff* | -ecoff* | -winnt* | -domain* | -vsta* \ - | -udi* | -eabi* | -lites* | -ieee* | -go32* | -aux* \ - | -chorusos* | -chorusrdb* \ - | -cygwin* | -pe* | -psos* | -moss* | -proelf* | -rtems* \ - | -mingw32* | -linux-gnu* | -linux-newlib* | -linux-uclibc* \ - | -uxpv* | -beos* | -mpeix* | -udk* \ - | -interix* | -uwin* | -mks* | -rhapsody* | -darwin* | -opened* \ - | -openstep* | -oskit* | -conix* | -pw32* | -nonstopux* \ - | -storm-chaos* | -tops10* | -tenex* | -tops20* | -its* \ - | -os2* | -vos* | -palmos* | -uclinux* | -nucleus* \ - | -morphos* | -superux* | -rtmk* | -rtmk-nova* | -windiss* \ - | -powermax* | -dnix* | -nx6 | -nx7 | -sei* | -dragonfly* \ - | -skyos* | -haiku* | -rdos* | -toppers* | -drops* | -irx*) - # Remember, each alternative MUST END IN *, to match a version number. - ;; - -qnx*) - case $basic_machine in - x86-* | i*86-*) - ;; - *) - os=-nto$os - ;; - esac - ;; - -nto-qnx*) - ;; - -nto*) - os=`echo $os | sed -e 's|nto|nto-qnx|'` - ;; - -sim | -es1800* | -hms* | -xray | -os68k* | -none* | -v88r* \ - | -windows* | -osx | -abug | -netware* | -os9* | -beos* | -haiku* \ - | -macos* | -mpw* | -magic* | -mmixware* | -mon960* | -lnews*) - ;; - -mac*) - os=`echo $os | sed -e 's|mac|macos|'` - ;; - -linux-dietlibc) - os=-linux-dietlibc - ;; - -linux*) - os=`echo $os | sed -e 's|linux|linux-gnu|'` - ;; - -sunos5*) - os=`echo $os | sed -e 's|sunos5|solaris2|'` - ;; - -sunos6*) - os=`echo $os | sed -e 's|sunos6|solaris3|'` - ;; - -opened*) - os=-openedition - ;; - -os400*) - os=-os400 - ;; - -wince*) - os=-wince - ;; - -osfrose*) - os=-osfrose - ;; - -osf*) - os=-osf - ;; - -utek*) - os=-bsd - ;; - -dynix*) - os=-bsd - ;; - -acis*) - os=-aos - ;; - -atheos*) - os=-atheos - ;; - -syllable*) - os=-syllable - ;; - -386bsd) - os=-bsd - ;; - -ctix* | -uts*) - os=-sysv - ;; - -nova*) - os=-rtmk-nova - ;; - -ns2 ) - os=-nextstep2 - ;; - -nsk*) - os=-nsk - ;; - # Preserve the version number of sinix5. - -sinix5.*) - os=`echo $os | sed -e 's|sinix|sysv|'` - ;; - -sinix*) - os=-sysv4 - ;; - -tpf*) - os=-tpf - ;; - -triton*) - os=-sysv3 - ;; - -oss*) - os=-sysv3 - ;; - -svr4) - os=-sysv4 - ;; - -svr3) - os=-sysv3 - ;; - -sysvr4) - os=-sysv4 - ;; - # This must come after -sysvr4. - -sysv*) - ;; - -ose*) - os=-ose - ;; - -es1800*) - os=-ose - ;; - -xenix) - os=-xenix - ;; - -*mint | -mint[0-9]* | -*MiNT | -MiNT[0-9]*) - os=-mint - ;; - -aros*) - os=-aros - ;; - -kaos*) - os=-kaos - ;; - -zvmoe) - os=-zvmoe - ;; - -none) - ;; - *) - # Get rid of the `-' at the beginning of $os. - os=`echo $os | sed 's/[^-]*-//'` - echo Invalid configuration \`$1\': system \`$os\' not recognized 1>&2 - exit 1 - ;; -esac -else - -# Here we handle the default operating systems that come with various machines. -# The value should be what the vendor currently ships out the door with their -# machine or put another way, the most popular os provided with the machine. - -# Note that if you're going to try to match "-MANUFACTURER" here (say, -# "-sun"), then you have to tell the case statement up towards the top -# that MANUFACTURER isn't an operating system. Otherwise, code above -# will signal an error saying that MANUFACTURER isn't an operating -# system, and we'll never get to this point. - -case $basic_machine in - score-*) - os=-elf - ;; - spu-*) - os=-elf - ;; - *-acorn) - os=-riscix1.2 - ;; - arm*-rebel) - os=-linux - ;; - arm*-semi) - os=-aout - ;; - c4x-* | tic4x-*) - os=-coff - ;; - # This must come before the *-dec entry. - pdp10-*) - os=-tops20 - ;; - pdp11-*) - os=-none - ;; - *-dec | vax-*) - os=-ultrix4.2 - ;; - m68*-apollo) - os=-domain - ;; - i386-sun) - os=-sunos4.0.2 - ;; - m68000-sun) - os=-sunos3 - # This also exists in the configure program, but was not the - # default. - # os=-sunos4 - ;; - m68*-cisco) - os=-aout - ;; - mep-*) - os=-elf - ;; - mips*-cisco) - os=-elf - ;; - mips*-*) - os=-elf - ;; - or32-*) - os=-coff - ;; - *-tti) # must be before sparc entry or we get the wrong os. - os=-sysv3 - ;; - sparc-* | *-sun) - os=-sunos4.1.1 - ;; - *-be) - os=-beos - ;; - *-haiku) - os=-haiku - ;; - *-ibm) - os=-aix - ;; - *-knuth) - os=-mmixware - ;; - *-wec) - os=-proelf - ;; - *-winbond) - os=-proelf - ;; - *-oki) - os=-proelf - ;; - *-hp) - os=-hpux - ;; - *-hitachi) - os=-hiux - ;; - i860-* | *-att | *-ncr | *-altos | *-motorola | *-convergent) - os=-sysv - ;; - *-cbm) - os=-amigaos - ;; - *-dg) - os=-dgux - ;; - *-dolphin) - os=-sysv3 - ;; - m68k-ccur) - os=-rtu - ;; - m88k-omron*) - os=-luna - ;; - *-next ) - os=-nextstep - ;; - *-sequent) - os=-ptx - ;; - *-crds) - os=-unos - ;; - *-ns) - os=-genix - ;; - i370-*) - os=-mvs - ;; - *-next) - os=-nextstep3 - ;; - *-gould) - os=-sysv - ;; - *-highlevel) - os=-bsd - ;; - *-encore) - os=-bsd - ;; - *-sgi) - os=-irix - ;; - *-siemens) - os=-sysv4 - ;; - *-masscomp) - os=-rtu - ;; - f30[01]-fujitsu | f700-fujitsu) - os=-uxpv - ;; - *-rom68k) - os=-coff - ;; - *-*bug) - os=-coff - ;; - *-apple) - os=-macos - ;; - *-atari*) - os=-mint - ;; - *) - os=-none - ;; -esac -fi - -# Here we handle the case where we know the os, and the CPU type, but not the -# manufacturer. We pick the logical manufacturer. -vendor=unknown -case $basic_machine in - *-unknown) - case $os in - -riscix*) - vendor=acorn - ;; - -sunos*) - vendor=sun - ;; - -aix*) - vendor=ibm - ;; - -beos*) - vendor=be - ;; - -hpux*) - vendor=hp - ;; - -mpeix*) - vendor=hp - ;; - -hiux*) - vendor=hitachi - ;; - -unos*) - vendor=crds - ;; - -dgux*) - vendor=dg - ;; - -luna*) - vendor=omron - ;; - -genix*) - vendor=ns - ;; - -mvs* | -opened*) - vendor=ibm - ;; - -os400*) - vendor=ibm - ;; - -ptx*) - vendor=sequent - ;; - -tpf*) - vendor=ibm - ;; - -vxsim* | -vxworks* | -windiss*) - vendor=wrs - ;; - -aux*) - vendor=apple - ;; - -hms*) - vendor=hitachi - ;; - -mpw* | -macos*) - vendor=apple - ;; - -*mint | -mint[0-9]* | -*MiNT | -MiNT[0-9]*) - vendor=atari - ;; - -vos*) - vendor=stratus - ;; - esac - basic_machine=`echo $basic_machine | sed "s/unknown/$vendor/"` - ;; -esac - -echo $basic_machine$os -exit - -# Local variables: -# eval: (add-hook 'write-file-hooks 'time-stamp) -# time-stamp-start: "timestamp='" -# time-stamp-format: "%:y-%02m-%02d" -# time-stamp-end: "'" -# End: diff --git a/configure b/configure index ec79279fdf..64b60f8b35 100755 --- a/configure +++ b/configure @@ -1,15219 +1,4 @@ -#! /bin/sh -# Guess values for system-dependent variables and create Makefiles. -# Generated by GNU Autoconf 2.69 for sqlcipher 3.11.0. -# -# -# Copyright (C) 1992-1996, 1998-2012 Free Software Foundation, Inc. -# -# -# This configure script is free software; the Free Software Foundation -# gives unlimited permission to copy, distribute and modify it. -## -------------------- ## -## M4sh Initialization. ## -## -------------------- ## - -# Be more Bourne compatible -DUALCASE=1; export DUALCASE # for MKS sh -if test -n "${ZSH_VERSION+set}" && (emulate sh) >/dev/null 2>&1; then : - emulate sh - NULLCMD=: - # Pre-4.2 versions of Zsh do word splitting on ${1+"$@"}, which - # is contrary to our usage. Disable this feature. - alias -g '${1+"$@"}'='"$@"' - setopt NO_GLOB_SUBST -else - case `(set -o) 2>/dev/null` in #( - *posix*) : - set -o posix ;; #( - *) : - ;; -esac -fi - - -as_nl=' -' -export as_nl -# Printing a long string crashes Solaris 7 /usr/bin/printf. -as_echo='\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\' -as_echo=$as_echo$as_echo$as_echo$as_echo$as_echo -as_echo=$as_echo$as_echo$as_echo$as_echo$as_echo$as_echo -# Prefer a ksh shell builtin over an external printf program on Solaris, -# but without wasting forks for bash or zsh. -if test -z "$BASH_VERSION$ZSH_VERSION" \ - && (test "X`print -r -- $as_echo`" = "X$as_echo") 2>/dev/null; then - as_echo='print -r --' - as_echo_n='print -rn --' -elif (test "X`printf %s $as_echo`" = "X$as_echo") 2>/dev/null; then - as_echo='printf %s\n' - as_echo_n='printf %s' -else - if test "X`(/usr/ucb/echo -n -n $as_echo) 2>/dev/null`" = "X-n $as_echo"; then - as_echo_body='eval /usr/ucb/echo -n "$1$as_nl"' - as_echo_n='/usr/ucb/echo -n' - else - as_echo_body='eval expr "X$1" : "X\\(.*\\)"' - as_echo_n_body='eval - arg=$1; - case $arg in #( - *"$as_nl"*) - expr "X$arg" : "X\\(.*\\)$as_nl"; - arg=`expr "X$arg" : ".*$as_nl\\(.*\\)"`;; - esac; - expr "X$arg" : "X\\(.*\\)" | tr -d "$as_nl" - ' - export as_echo_n_body - as_echo_n='sh -c $as_echo_n_body as_echo' - fi - export as_echo_body - as_echo='sh -c $as_echo_body as_echo' -fi - -# The user is always right. -if test "${PATH_SEPARATOR+set}" != set; then - PATH_SEPARATOR=: - (PATH='/bin;/bin'; FPATH=$PATH; sh -c :) >/dev/null 2>&1 && { - (PATH='/bin:/bin'; FPATH=$PATH; sh -c :) >/dev/null 2>&1 || - PATH_SEPARATOR=';' - } -fi - - -# IFS -# We need space, tab and new line, in precisely that order. Quoting is -# there to prevent editors from complaining about space-tab. -# (If _AS_PATH_WALK were called with IFS unset, it would disable word -# splitting by setting IFS to empty value.) -IFS=" "" $as_nl" - -# Find who we are. Look in the path if we contain no directory separator. -as_myself= -case $0 in #(( - *[\\/]* ) as_myself=$0 ;; - *) as_save_IFS=$IFS; IFS=$PATH_SEPARATOR -for as_dir in $PATH -do - IFS=$as_save_IFS - test -z "$as_dir" && as_dir=. - test -r "$as_dir/$0" && as_myself=$as_dir/$0 && break - done -IFS=$as_save_IFS - - ;; -esac -# We did not find ourselves, most probably we were run as `sh COMMAND' -# in which case we are not to be found in the path. -if test "x$as_myself" = x; then - as_myself=$0 -fi -if test ! -f "$as_myself"; then - $as_echo "$as_myself: error: cannot find myself; rerun with an absolute file name" >&2 - exit 1 -fi - -# Unset variables that we do not need and which cause bugs (e.g. in -# pre-3.0 UWIN ksh). But do not cause bugs in bash 2.01; the "|| exit 1" -# suppresses any "Segmentation fault" message there. '((' could -# trigger a bug in pdksh 5.2.14. -for as_var in BASH_ENV ENV MAIL MAILPATH -do eval test x\${$as_var+set} = xset \ - && ( (unset $as_var) || exit 1) >/dev/null 2>&1 && unset $as_var || : -done -PS1='$ ' -PS2='> ' -PS4='+ ' - -# NLS nuisances. -LC_ALL=C -export LC_ALL -LANGUAGE=C -export LANGUAGE - -# CDPATH. -(unset CDPATH) >/dev/null 2>&1 && unset CDPATH - -# Use a proper internal environment variable to ensure we don't fall - # into an infinite loop, continuously re-executing ourselves. - if test x"${_as_can_reexec}" != xno && test "x$CONFIG_SHELL" != x; then - _as_can_reexec=no; export _as_can_reexec; - # We cannot yet assume a decent shell, so we have to provide a -# neutralization value for shells without unset; and this also -# works around shells that cannot unset nonexistent variables. -# Preserve -v and -x to the replacement shell. -BASH_ENV=/dev/null -ENV=/dev/null -(unset BASH_ENV) >/dev/null 2>&1 && unset BASH_ENV ENV -case $- in # (((( - *v*x* | *x*v* ) as_opts=-vx ;; - *v* ) as_opts=-v ;; - *x* ) as_opts=-x ;; - * ) as_opts= ;; -esac -exec $CONFIG_SHELL $as_opts "$as_myself" ${1+"$@"} -# Admittedly, this is quite paranoid, since all the known shells bail -# out after a failed `exec'. -$as_echo "$0: could not re-execute with $CONFIG_SHELL" >&2 -as_fn_exit 255 - fi - # We don't want this to propagate to other subprocesses. - { _as_can_reexec=; unset _as_can_reexec;} -if test "x$CONFIG_SHELL" = x; then - as_bourne_compatible="if test -n \"\${ZSH_VERSION+set}\" && (emulate sh) >/dev/null 2>&1; then : - emulate sh - NULLCMD=: - # Pre-4.2 versions of Zsh do word splitting on \${1+\"\$@\"}, which - # is contrary to our usage. Disable this feature. - alias -g '\${1+\"\$@\"}'='\"\$@\"' - setopt NO_GLOB_SUBST -else - case \`(set -o) 2>/dev/null\` in #( - *posix*) : - set -o posix ;; #( - *) : - ;; -esac -fi -" - as_required="as_fn_return () { (exit \$1); } -as_fn_success () { as_fn_return 0; } -as_fn_failure () { as_fn_return 1; } -as_fn_ret_success () { return 0; } -as_fn_ret_failure () { return 1; } - -exitcode=0 -as_fn_success || { exitcode=1; echo as_fn_success failed.; } -as_fn_failure && { exitcode=1; echo as_fn_failure succeeded.; } -as_fn_ret_success || { exitcode=1; echo as_fn_ret_success failed.; } -as_fn_ret_failure && { exitcode=1; echo as_fn_ret_failure succeeded.; } -if ( set x; as_fn_ret_success y && test x = \"\$1\" ); then : - -else - exitcode=1; echo positional parameters were not saved. -fi -test x\$exitcode = x0 || exit 1 -test -x / || exit 1" - as_suggested=" as_lineno_1=";as_suggested=$as_suggested$LINENO;as_suggested=$as_suggested" as_lineno_1a=\$LINENO - as_lineno_2=";as_suggested=$as_suggested$LINENO;as_suggested=$as_suggested" as_lineno_2a=\$LINENO - eval 'test \"x\$as_lineno_1'\$as_run'\" != \"x\$as_lineno_2'\$as_run'\" && - test \"x\`expr \$as_lineno_1'\$as_run' + 1\`\" = \"x\$as_lineno_2'\$as_run'\"' || exit 1 - - test -n \"\${ZSH_VERSION+set}\${BASH_VERSION+set}\" || ( - ECHO='\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\' - ECHO=\$ECHO\$ECHO\$ECHO\$ECHO\$ECHO - ECHO=\$ECHO\$ECHO\$ECHO\$ECHO\$ECHO\$ECHO - PATH=/empty FPATH=/empty; export PATH FPATH - test \"X\`printf %s \$ECHO\`\" = \"X\$ECHO\" \\ - || test \"X\`print -r -- \$ECHO\`\" = \"X\$ECHO\" ) || exit 1 -test \$(( 1 + 1 )) = 2 || exit 1" - if (eval "$as_required") 2>/dev/null; then : - as_have_required=yes -else - as_have_required=no -fi - if test x$as_have_required = xyes && (eval "$as_suggested") 2>/dev/null; then : - -else - as_save_IFS=$IFS; IFS=$PATH_SEPARATOR -as_found=false -for as_dir in /bin$PATH_SEPARATOR/usr/bin$PATH_SEPARATOR$PATH -do - IFS=$as_save_IFS - test -z "$as_dir" && as_dir=. - as_found=: - case $as_dir in #( - /*) - for as_base in sh bash ksh sh5; do - # Try only shells that exist, to save several forks. - as_shell=$as_dir/$as_base - if { test -f "$as_shell" || test -f "$as_shell.exe"; } && - { $as_echo "$as_bourne_compatible""$as_required" | as_run=a "$as_shell"; } 2>/dev/null; then : - CONFIG_SHELL=$as_shell as_have_required=yes - if { $as_echo "$as_bourne_compatible""$as_suggested" | as_run=a "$as_shell"; } 2>/dev/null; then : - break 2 -fi -fi - done;; - esac - as_found=false -done -$as_found || { if { test -f "$SHELL" || test -f "$SHELL.exe"; } && - { $as_echo "$as_bourne_compatible""$as_required" | as_run=a "$SHELL"; } 2>/dev/null; then : - CONFIG_SHELL=$SHELL as_have_required=yes -fi; } -IFS=$as_save_IFS - - - if test "x$CONFIG_SHELL" != x; then : - export CONFIG_SHELL - # We cannot yet assume a decent shell, so we have to provide a -# neutralization value for shells without unset; and this also -# works around shells that cannot unset nonexistent variables. -# Preserve -v and -x to the replacement shell. -BASH_ENV=/dev/null -ENV=/dev/null -(unset BASH_ENV) >/dev/null 2>&1 && unset BASH_ENV ENV -case $- in # (((( - *v*x* | *x*v* ) as_opts=-vx ;; - *v* ) as_opts=-v ;; - *x* ) as_opts=-x ;; - * ) as_opts= ;; -esac -exec $CONFIG_SHELL $as_opts "$as_myself" ${1+"$@"} -# Admittedly, this is quite paranoid, since all the known shells bail -# out after a failed `exec'. -$as_echo "$0: could not re-execute with $CONFIG_SHELL" >&2 -exit 255 -fi - - if test x$as_have_required = xno; then : - $as_echo "$0: This script requires a shell more modern than all" - $as_echo "$0: the shells that I found on your system." - if test x${ZSH_VERSION+set} = xset ; then - $as_echo "$0: In particular, zsh $ZSH_VERSION has bugs and should" - $as_echo "$0: be upgraded to zsh 4.3.4 or later." - else - $as_echo "$0: Please tell bug-autoconf@gnu.org about your system, -$0: including any error possibly output before this -$0: message. Then install a modern shell, or manually run -$0: the script under such a shell if you do have one." - fi - exit 1 -fi -fi -fi -SHELL=${CONFIG_SHELL-/bin/sh} -export SHELL -# Unset more variables known to interfere with behavior of common tools. -CLICOLOR_FORCE= GREP_OPTIONS= -unset CLICOLOR_FORCE GREP_OPTIONS - -## --------------------- ## -## M4sh Shell Functions. ## -## --------------------- ## -# as_fn_unset VAR -# --------------- -# Portably unset VAR. -as_fn_unset () -{ - { eval $1=; unset $1;} -} -as_unset=as_fn_unset - -# as_fn_set_status STATUS -# ----------------------- -# Set $? to STATUS, without forking. -as_fn_set_status () -{ - return $1 -} # as_fn_set_status - -# as_fn_exit STATUS -# ----------------- -# Exit the shell with STATUS, even in a "trap 0" or "set -e" context. -as_fn_exit () -{ - set +e - as_fn_set_status $1 - exit $1 -} # as_fn_exit - -# as_fn_mkdir_p -# ------------- -# Create "$as_dir" as a directory, including parents if necessary. -as_fn_mkdir_p () -{ - - case $as_dir in #( - -*) as_dir=./$as_dir;; - esac - test -d "$as_dir" || eval $as_mkdir_p || { - as_dirs= - while :; do - case $as_dir in #( - *\'*) as_qdir=`$as_echo "$as_dir" | sed "s/'/'\\\\\\\\''/g"`;; #'( - *) as_qdir=$as_dir;; - esac - as_dirs="'$as_qdir' $as_dirs" - as_dir=`$as_dirname -- "$as_dir" || -$as_expr X"$as_dir" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \ - X"$as_dir" : 'X\(//\)[^/]' \| \ - X"$as_dir" : 'X\(//\)$' \| \ - X"$as_dir" : 'X\(/\)' \| . 2>/dev/null || -$as_echo X"$as_dir" | - sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{ - s//\1/ - q - } - /^X\(\/\/\)[^/].*/{ - s//\1/ - q - } - /^X\(\/\/\)$/{ - s//\1/ - q - } - /^X\(\/\).*/{ - s//\1/ - q - } - s/.*/./; q'` - test -d "$as_dir" && break - done - test -z "$as_dirs" || eval "mkdir $as_dirs" - } || test -d "$as_dir" || as_fn_error $? "cannot create directory $as_dir" - - -} # as_fn_mkdir_p - -# as_fn_executable_p FILE -# ----------------------- -# Test if FILE is an executable regular file. -as_fn_executable_p () -{ - test -f "$1" && test -x "$1" -} # as_fn_executable_p -# as_fn_append VAR VALUE -# ---------------------- -# Append the text in VALUE to the end of the definition contained in VAR. Take -# advantage of any shell optimizations that allow amortized linear growth over -# repeated appends, instead of the typical quadratic growth present in naive -# implementations. -if (eval "as_var=1; as_var+=2; test x\$as_var = x12") 2>/dev/null; then : - eval 'as_fn_append () - { - eval $1+=\$2 - }' -else - as_fn_append () - { - eval $1=\$$1\$2 - } -fi # as_fn_append - -# as_fn_arith ARG... -# ------------------ -# Perform arithmetic evaluation on the ARGs, and store the result in the -# global $as_val. Take advantage of shells that can avoid forks. The arguments -# must be portable across $(()) and expr. -if (eval "test \$(( 1 + 1 )) = 2") 2>/dev/null; then : - eval 'as_fn_arith () - { - as_val=$(( $* )) - }' -else - as_fn_arith () - { - as_val=`expr "$@" || test $? -eq 1` - } -fi # as_fn_arith - - -# as_fn_error STATUS ERROR [LINENO LOG_FD] -# ---------------------------------------- -# Output "`basename $0`: error: ERROR" to stderr. If LINENO and LOG_FD are -# provided, also output the error to LOG_FD, referencing LINENO. Then exit the -# script with STATUS, using 1 if that was 0. -as_fn_error () -{ - as_status=$1; test $as_status -eq 0 && as_status=1 - if test "$4"; then - as_lineno=${as_lineno-"$3"} as_lineno_stack=as_lineno_stack=$as_lineno_stack - $as_echo "$as_me:${as_lineno-$LINENO}: error: $2" >&$4 - fi - $as_echo "$as_me: error: $2" >&2 - as_fn_exit $as_status -} # as_fn_error - -if expr a : '\(a\)' >/dev/null 2>&1 && - test "X`expr 00001 : '.*\(...\)'`" = X001; then - as_expr=expr -else - as_expr=false -fi - -if (basename -- /) >/dev/null 2>&1 && test "X`basename -- / 2>&1`" = "X/"; then - as_basename=basename -else - as_basename=false -fi - -if (as_dir=`dirname -- /` && test "X$as_dir" = X/) >/dev/null 2>&1; then - as_dirname=dirname -else - as_dirname=false -fi - -as_me=`$as_basename -- "$0" || -$as_expr X/"$0" : '.*/\([^/][^/]*\)/*$' \| \ - X"$0" : 'X\(//\)$' \| \ - X"$0" : 'X\(/\)' \| . 2>/dev/null || -$as_echo X/"$0" | - sed '/^.*\/\([^/][^/]*\)\/*$/{ - s//\1/ - q - } - /^X\/\(\/\/\)$/{ - s//\1/ - q - } - /^X\/\(\/\).*/{ - s//\1/ - q - } - s/.*/./; q'` - -# Avoid depending upon Character Ranges. -as_cr_letters='abcdefghijklmnopqrstuvwxyz' -as_cr_LETTERS='ABCDEFGHIJKLMNOPQRSTUVWXYZ' -as_cr_Letters=$as_cr_letters$as_cr_LETTERS -as_cr_digits='0123456789' -as_cr_alnum=$as_cr_Letters$as_cr_digits - - - as_lineno_1=$LINENO as_lineno_1a=$LINENO - as_lineno_2=$LINENO as_lineno_2a=$LINENO - eval 'test "x$as_lineno_1'$as_run'" != "x$as_lineno_2'$as_run'" && - test "x`expr $as_lineno_1'$as_run' + 1`" = "x$as_lineno_2'$as_run'"' || { - # Blame Lee E. McMahon (1931-1989) for sed's syntax. :-) - sed -n ' - p - /[$]LINENO/= - ' <$as_myself | - sed ' - s/[$]LINENO.*/&-/ - t lineno - b - :lineno - N - :loop - s/[$]LINENO\([^'$as_cr_alnum'_].*\n\)\(.*\)/\2\1\2/ - t loop - s/-\n.*// - ' >$as_me.lineno && - chmod +x "$as_me.lineno" || - { $as_echo "$as_me: error: cannot create $as_me.lineno; rerun with a POSIX shell" >&2; as_fn_exit 1; } - - # If we had to re-execute with $CONFIG_SHELL, we're ensured to have - # already done that, so ensure we don't try to do so again and fall - # in an infinite loop. This has already happened in practice. - _as_can_reexec=no; export _as_can_reexec - # Don't try to exec as it changes $[0], causing all sort of problems - # (the dirname of $[0] is not the place where we might find the - # original and so on. Autoconf is especially sensitive to this). - . "./$as_me.lineno" - # Exit status is that of the last command. - exit -} - -ECHO_C= ECHO_N= ECHO_T= -case `echo -n x` in #((((( --n*) - case `echo 'xy\c'` in - *c*) ECHO_T=' ';; # ECHO_T is single tab character. - xy) ECHO_C='\c';; - *) echo `echo ksh88 bug on AIX 6.1` > /dev/null - ECHO_T=' ';; - esac;; -*) - ECHO_N='-n';; -esac - -rm -f conf$$ conf$$.exe conf$$.file -if test -d conf$$.dir; then - rm -f conf$$.dir/conf$$.file -else - rm -f conf$$.dir - mkdir conf$$.dir 2>/dev/null -fi -if (echo >conf$$.file) 2>/dev/null; then - if ln -s conf$$.file conf$$ 2>/dev/null; then - as_ln_s='ln -s' - # ... but there are two gotchas: - # 1) On MSYS, both `ln -s file dir' and `ln file dir' fail. - # 2) DJGPP < 2.04 has no symlinks; `ln -s' creates a wrapper executable. - # In both cases, we have to default to `cp -pR'. - ln -s conf$$.file conf$$.dir 2>/dev/null && test ! -f conf$$.exe || - as_ln_s='cp -pR' - elif ln conf$$.file conf$$ 2>/dev/null; then - as_ln_s=ln - else - as_ln_s='cp -pR' - fi -else - as_ln_s='cp -pR' -fi -rm -f conf$$ conf$$.exe conf$$.dir/conf$$.file conf$$.file -rmdir conf$$.dir 2>/dev/null - -if mkdir -p . 2>/dev/null; then - as_mkdir_p='mkdir -p "$as_dir"' -else - test -d ./-p && rmdir ./-p - as_mkdir_p=false -fi - -as_test_x='test -x' -as_executable_p=as_fn_executable_p - -# Sed expression to map a string onto a valid CPP name. -as_tr_cpp="eval sed 'y%*$as_cr_letters%P$as_cr_LETTERS%;s%[^_$as_cr_alnum]%_%g'" - -# Sed expression to map a string onto a valid variable name. -as_tr_sh="eval sed 'y%*+%pp%;s%[^_$as_cr_alnum]%_%g'" - -SHELL=${CONFIG_SHELL-/bin/sh} - - -test -n "$DJDIR" || exec 7<&0 &1 - -# Name of the host. -# hostname on some systems (SVR3.2, old GNU/Linux) returns a bogus exit status, -# so uname gets run too. -ac_hostname=`(hostname || uname -n) 2>/dev/null | sed 1q` - -# -# Initializations. -# -ac_default_prefix=/usr/local -ac_clean_files= -ac_config_libobj_dir=. -LIBOBJS= -cross_compiling=no -subdirs= -MFLAGS= -MAKEFLAGS= - -# Identity of this package. -PACKAGE_NAME='sqlcipher' -PACKAGE_TARNAME='sqlcipher' -PACKAGE_VERSION='3.11.0' -PACKAGE_STRING='sqlcipher 3.11.0' -PACKAGE_BUGREPORT='' -PACKAGE_URL='' - -# Factoring default headers for most tests. -ac_includes_default="\ -#include -#ifdef HAVE_SYS_TYPES_H -# include -#endif -#ifdef HAVE_SYS_STAT_H -# include -#endif -#ifdef STDC_HEADERS -# include -# include -#else -# ifdef HAVE_STDLIB_H -# include -# endif -#endif -#ifdef HAVE_STRING_H -# if !defined STDC_HEADERS && defined HAVE_MEMORY_H -# include -# endif -# include -#endif -#ifdef HAVE_STRINGS_H -# include -#endif -#ifdef HAVE_INTTYPES_H -# include -#endif -#ifdef HAVE_STDINT_H -# include -#endif -#ifdef HAVE_UNISTD_H -# include -#endif" - -ac_subst_vars='LTLIBOBJS -LIBOBJS -BUILD_CFLAGS -USE_GCOV -OPT_FEATURE_FLAGS -USE_AMALGAMATION -TARGET_DEBUG -TARGET_HAVE_EDITLINE -TARGET_HAVE_READLINE -TARGET_READLINE_INC -TARGET_READLINE_LIBS -HAVE_TCL -TCL_SHLIB_SUFFIX -TCL_STUB_LIB_SPEC -TCL_STUB_LIB_FLAG -TCL_STUB_LIB_FILE -TCL_LIB_SPEC -TCL_LIB_FLAG -TCL_LIB_FILE -TCL_INCLUDE_SPEC -TCL_SRC_DIR -TCL_BIN_DIR -TCL_VERSION -TARGET_EXEEXT -SQLITE_OS_WIN -SQLITE_OS_UNIX -BUILD_EXEEXT -TEMP_STORE -ALLOWRELEASE -XTHREADCONNECT -SQLITE_THREADSAFE -BUILD_CC -VERSION_NUMBER -RELEASE -VERSION -program_prefix -TCLLIBDIR -TCLSH_CMD -INSTALL_DATA -INSTALL_SCRIPT -INSTALL_PROGRAM -CPP -LT_SYS_LIBRARY_PATH -OTOOL64 -OTOOL -LIPO -NMEDIT -DSYMUTIL -MANIFEST_TOOL -AWK -RANLIB -STRIP -ac_ct_AR -AR -DLLTOOL -OBJDUMP -LN_S -NM -ac_ct_DUMPBIN -DUMPBIN -LD -FGREP -EGREP -GREP -SED -OBJEXT -EXEEXT -ac_ct_CC -CPPFLAGS -LDFLAGS -CFLAGS -CC -host_os -host_vendor -host_cpu -host -build_os -build_vendor -build_cpu -build -LIBTOOL -target_alias -host_alias -build_alias -LIBS -ECHO_T -ECHO_N -ECHO_C -DEFS -mandir -localedir -libdir -psdir -pdfdir -dvidir -htmldir -infodir -docdir -oldincludedir -includedir -localstatedir -sharedstatedir -sysconfdir -datadir -datarootdir -libexecdir -sbindir -bindir -program_transform_name -prefix -exec_prefix -PACKAGE_URL -PACKAGE_BUGREPORT -PACKAGE_STRING -PACKAGE_VERSION -PACKAGE_TARNAME -PACKAGE_NAME -PATH_SEPARATOR -SHELL' -ac_subst_files='' -ac_user_opts=' -enable_option_checking -enable_shared -enable_static -with_pic -enable_fast_install -with_aix_soname -with_gnu_ld -with_sysroot -enable_libtool_lock -enable_largefile -enable_threadsafe -with_crypto_lib -enable_cross_thread_connections -enable_releasemode -enable_tempstore -enable_tcl -with_tcl -enable_editline -enable_readline -with_readline_lib -with_readline_inc -enable_debug -enable_amalgamation -enable_load_extension -enable_fts3 -enable_fts4 -enable_fts5 -enable_json1 -enable_rtree -enable_gcov -' - ac_precious_vars='build_alias -host_alias -target_alias -CC -CFLAGS -LDFLAGS -LIBS -CPPFLAGS -LT_SYS_LIBRARY_PATH -CPP -TCLLIBDIR' - - -# Initialize some variables set by options. -ac_init_help= -ac_init_version=false -ac_unrecognized_opts= -ac_unrecognized_sep= -# The variables have the same names as the options, with -# dashes changed to underlines. -cache_file=/dev/null -exec_prefix=NONE -no_create= -no_recursion= -prefix=NONE -program_prefix=NONE -program_suffix=NONE -program_transform_name=s,x,x, -silent= -site= -srcdir= -verbose= -x_includes=NONE -x_libraries=NONE - -# Installation directory options. -# These are left unexpanded so users can "make install exec_prefix=/foo" -# and all the variables that are supposed to be based on exec_prefix -# by default will actually change. -# Use braces instead of parens because sh, perl, etc. also accept them. -# (The list follows the same order as the GNU Coding Standards.) -bindir='${exec_prefix}/bin' -sbindir='${exec_prefix}/sbin' -libexecdir='${exec_prefix}/libexec' -datarootdir='${prefix}/share' -datadir='${datarootdir}' -sysconfdir='${prefix}/etc' -sharedstatedir='${prefix}/com' -localstatedir='${prefix}/var' -includedir='${prefix}/include' -oldincludedir='/usr/include' -docdir='${datarootdir}/doc/${PACKAGE_TARNAME}' -infodir='${datarootdir}/info' -htmldir='${docdir}' -dvidir='${docdir}' -pdfdir='${docdir}' -psdir='${docdir}' -libdir='${exec_prefix}/lib' -localedir='${datarootdir}/locale' -mandir='${datarootdir}/man' - -ac_prev= -ac_dashdash= -for ac_option -do - # If the previous option needs an argument, assign it. - if test -n "$ac_prev"; then - eval $ac_prev=\$ac_option - ac_prev= - continue - fi - - case $ac_option in - *=?*) ac_optarg=`expr "X$ac_option" : '[^=]*=\(.*\)'` ;; - *=) ac_optarg= ;; - *) ac_optarg=yes ;; - esac - - # Accept the important Cygnus configure options, so we can diagnose typos. - - case $ac_dashdash$ac_option in - --) - ac_dashdash=yes ;; - - -bindir | --bindir | --bindi | --bind | --bin | --bi) - ac_prev=bindir ;; - -bindir=* | --bindir=* | --bindi=* | --bind=* | --bin=* | --bi=*) - bindir=$ac_optarg ;; - - -build | --build | --buil | --bui | --bu) - ac_prev=build_alias ;; - -build=* | --build=* | --buil=* | --bui=* | --bu=*) - build_alias=$ac_optarg ;; - - -cache-file | --cache-file | --cache-fil | --cache-fi \ - | --cache-f | --cache- | --cache | --cach | --cac | --ca | --c) - ac_prev=cache_file ;; - -cache-file=* | --cache-file=* | --cache-fil=* | --cache-fi=* \ - | --cache-f=* | --cache-=* | --cache=* | --cach=* | --cac=* | --ca=* | --c=*) - cache_file=$ac_optarg ;; - - --config-cache | -C) - cache_file=config.cache ;; - - -datadir | --datadir | --datadi | --datad) - ac_prev=datadir ;; - -datadir=* | --datadir=* | --datadi=* | --datad=*) - datadir=$ac_optarg ;; - - -datarootdir | --datarootdir | --datarootdi | --datarootd | --dataroot \ - | --dataroo | --dataro | --datar) - ac_prev=datarootdir ;; - -datarootdir=* | --datarootdir=* | --datarootdi=* | --datarootd=* \ - | --dataroot=* | --dataroo=* | --dataro=* | --datar=*) - datarootdir=$ac_optarg ;; - - -disable-* | --disable-*) - ac_useropt=`expr "x$ac_option" : 'x-*disable-\(.*\)'` - # Reject names that are not valid shell variable names. - expr "x$ac_useropt" : ".*[^-+._$as_cr_alnum]" >/dev/null && - as_fn_error $? "invalid feature name: $ac_useropt" - ac_useropt_orig=$ac_useropt - ac_useropt=`$as_echo "$ac_useropt" | sed 's/[-+.]/_/g'` - case $ac_user_opts in - *" -"enable_$ac_useropt" -"*) ;; - *) ac_unrecognized_opts="$ac_unrecognized_opts$ac_unrecognized_sep--disable-$ac_useropt_orig" - ac_unrecognized_sep=', ';; - esac - eval enable_$ac_useropt=no ;; - - -docdir | --docdir | --docdi | --doc | --do) - ac_prev=docdir ;; - -docdir=* | --docdir=* | --docdi=* | --doc=* | --do=*) - docdir=$ac_optarg ;; - - -dvidir | --dvidir | --dvidi | --dvid | --dvi | --dv) - ac_prev=dvidir ;; - -dvidir=* | --dvidir=* | --dvidi=* | --dvid=* | --dvi=* | --dv=*) - dvidir=$ac_optarg ;; - - -enable-* | --enable-*) - ac_useropt=`expr "x$ac_option" : 'x-*enable-\([^=]*\)'` - # Reject names that are not valid shell variable names. - expr "x$ac_useropt" : ".*[^-+._$as_cr_alnum]" >/dev/null && - as_fn_error $? "invalid feature name: $ac_useropt" - ac_useropt_orig=$ac_useropt - ac_useropt=`$as_echo "$ac_useropt" | sed 's/[-+.]/_/g'` - case $ac_user_opts in - *" -"enable_$ac_useropt" -"*) ;; - *) ac_unrecognized_opts="$ac_unrecognized_opts$ac_unrecognized_sep--enable-$ac_useropt_orig" - ac_unrecognized_sep=', ';; - esac - eval enable_$ac_useropt=\$ac_optarg ;; - - -exec-prefix | --exec_prefix | --exec-prefix | --exec-prefi \ - | --exec-pref | --exec-pre | --exec-pr | --exec-p | --exec- \ - | --exec | --exe | --ex) - ac_prev=exec_prefix ;; - -exec-prefix=* | --exec_prefix=* | --exec-prefix=* | --exec-prefi=* \ - | --exec-pref=* | --exec-pre=* | --exec-pr=* | --exec-p=* | --exec-=* \ - | --exec=* | --exe=* | --ex=*) - exec_prefix=$ac_optarg ;; - - -gas | --gas | --ga | --g) - # Obsolete; use --with-gas. - with_gas=yes ;; - - -help | --help | --hel | --he | -h) - ac_init_help=long ;; - -help=r* | --help=r* | --hel=r* | --he=r* | -hr*) - ac_init_help=recursive ;; - -help=s* | --help=s* | --hel=s* | --he=s* | -hs*) - ac_init_help=short ;; - - -host | --host | --hos | --ho) - ac_prev=host_alias ;; - -host=* | --host=* | --hos=* | --ho=*) - host_alias=$ac_optarg ;; - - -htmldir | --htmldir | --htmldi | --htmld | --html | --htm | --ht) - ac_prev=htmldir ;; - -htmldir=* | --htmldir=* | --htmldi=* | --htmld=* | --html=* | --htm=* \ - | --ht=*) - htmldir=$ac_optarg ;; - - -includedir | --includedir | --includedi | --included | --include \ - | --includ | --inclu | --incl | --inc) - ac_prev=includedir ;; - -includedir=* | --includedir=* | --includedi=* | --included=* | --include=* \ - | --includ=* | --inclu=* | --incl=* | --inc=*) - includedir=$ac_optarg ;; - - -infodir | --infodir | --infodi | --infod | --info | --inf) - ac_prev=infodir ;; - -infodir=* | --infodir=* | --infodi=* | --infod=* | --info=* | --inf=*) - infodir=$ac_optarg ;; - - -libdir | --libdir | --libdi | --libd) - ac_prev=libdir ;; - -libdir=* | --libdir=* | --libdi=* | --libd=*) - libdir=$ac_optarg ;; - - -libexecdir | --libexecdir | --libexecdi | --libexecd | --libexec \ - | --libexe | --libex | --libe) - ac_prev=libexecdir ;; - -libexecdir=* | --libexecdir=* | --libexecdi=* | --libexecd=* | --libexec=* \ - | --libexe=* | --libex=* | --libe=*) - libexecdir=$ac_optarg ;; - - -localedir | --localedir | --localedi | --localed | --locale) - ac_prev=localedir ;; - -localedir=* | --localedir=* | --localedi=* | --localed=* | --locale=*) - localedir=$ac_optarg ;; - - -localstatedir | --localstatedir | --localstatedi | --localstated \ - | --localstate | --localstat | --localsta | --localst | --locals) - ac_prev=localstatedir ;; - -localstatedir=* | --localstatedir=* | --localstatedi=* | --localstated=* \ - | --localstate=* | --localstat=* | --localsta=* | --localst=* | --locals=*) - localstatedir=$ac_optarg ;; - - -mandir | --mandir | --mandi | --mand | --man | --ma | --m) - ac_prev=mandir ;; - -mandir=* | --mandir=* | --mandi=* | --mand=* | --man=* | --ma=* | --m=*) - mandir=$ac_optarg ;; - - -nfp | --nfp | --nf) - # Obsolete; use --without-fp. - with_fp=no ;; - - -no-create | --no-create | --no-creat | --no-crea | --no-cre \ - | --no-cr | --no-c | -n) - no_create=yes ;; - - -no-recursion | --no-recursion | --no-recursio | --no-recursi \ - | --no-recurs | --no-recur | --no-recu | --no-rec | --no-re | --no-r) - no_recursion=yes ;; - - -oldincludedir | --oldincludedir | --oldincludedi | --oldincluded \ - | --oldinclude | --oldinclud | --oldinclu | --oldincl | --oldinc \ - | --oldin | --oldi | --old | --ol | --o) - ac_prev=oldincludedir ;; - -oldincludedir=* | --oldincludedir=* | --oldincludedi=* | --oldincluded=* \ - | --oldinclude=* | --oldinclud=* | --oldinclu=* | --oldincl=* | --oldinc=* \ - | --oldin=* | --oldi=* | --old=* | --ol=* | --o=*) - oldincludedir=$ac_optarg ;; - - -prefix | --prefix | --prefi | --pref | --pre | --pr | --p) - ac_prev=prefix ;; - -prefix=* | --prefix=* | --prefi=* | --pref=* | --pre=* | --pr=* | --p=*) - prefix=$ac_optarg ;; - - -program-prefix | --program-prefix | --program-prefi | --program-pref \ - | --program-pre | --program-pr | --program-p) - ac_prev=program_prefix ;; - -program-prefix=* | --program-prefix=* | --program-prefi=* \ - | --program-pref=* | --program-pre=* | --program-pr=* | --program-p=*) - program_prefix=$ac_optarg ;; - - -program-suffix | --program-suffix | --program-suffi | --program-suff \ - | --program-suf | --program-su | --program-s) - ac_prev=program_suffix ;; - -program-suffix=* | --program-suffix=* | --program-suffi=* \ - | --program-suff=* | --program-suf=* | --program-su=* | --program-s=*) - program_suffix=$ac_optarg ;; - - -program-transform-name | --program-transform-name \ - | --program-transform-nam | --program-transform-na \ - | --program-transform-n | --program-transform- \ - | --program-transform | --program-transfor \ - | --program-transfo | --program-transf \ - | --program-trans | --program-tran \ - | --progr-tra | --program-tr | --program-t) - ac_prev=program_transform_name ;; - -program-transform-name=* | --program-transform-name=* \ - | --program-transform-nam=* | --program-transform-na=* \ - | --program-transform-n=* | --program-transform-=* \ - | --program-transform=* | --program-transfor=* \ - | --program-transfo=* | --program-transf=* \ - | --program-trans=* | --program-tran=* \ - | --progr-tra=* | --program-tr=* | --program-t=*) - program_transform_name=$ac_optarg ;; - - -pdfdir | --pdfdir | --pdfdi | --pdfd | --pdf | --pd) - ac_prev=pdfdir ;; - -pdfdir=* | --pdfdir=* | --pdfdi=* | --pdfd=* | --pdf=* | --pd=*) - pdfdir=$ac_optarg ;; - - -psdir | --psdir | --psdi | --psd | --ps) - ac_prev=psdir ;; - -psdir=* | --psdir=* | --psdi=* | --psd=* | --ps=*) - psdir=$ac_optarg ;; - - -q | -quiet | --quiet | --quie | --qui | --qu | --q \ - | -silent | --silent | --silen | --sile | --sil) - silent=yes ;; - - -sbindir | --sbindir | --sbindi | --sbind | --sbin | --sbi | --sb) - ac_prev=sbindir ;; - -sbindir=* | --sbindir=* | --sbindi=* | --sbind=* | --sbin=* \ - | --sbi=* | --sb=*) - sbindir=$ac_optarg ;; - - -sharedstatedir | --sharedstatedir | --sharedstatedi \ - | --sharedstated | --sharedstate | --sharedstat | --sharedsta \ - | --sharedst | --shareds | --shared | --share | --shar \ - | --sha | --sh) - ac_prev=sharedstatedir ;; - -sharedstatedir=* | --sharedstatedir=* | --sharedstatedi=* \ - | --sharedstated=* | --sharedstate=* | --sharedstat=* | --sharedsta=* \ - | --sharedst=* | --shareds=* | --shared=* | --share=* | --shar=* \ - | --sha=* | --sh=*) - sharedstatedir=$ac_optarg ;; - - -site | --site | --sit) - ac_prev=site ;; - -site=* | --site=* | --sit=*) - site=$ac_optarg ;; - - -srcdir | --srcdir | --srcdi | --srcd | --src | --sr) - ac_prev=srcdir ;; - -srcdir=* | --srcdir=* | --srcdi=* | --srcd=* | --src=* | --sr=*) - srcdir=$ac_optarg ;; - - -sysconfdir | --sysconfdir | --sysconfdi | --sysconfd | --sysconf \ - | --syscon | --sysco | --sysc | --sys | --sy) - ac_prev=sysconfdir ;; - -sysconfdir=* | --sysconfdir=* | --sysconfdi=* | --sysconfd=* | --sysconf=* \ - | --syscon=* | --sysco=* | --sysc=* | --sys=* | --sy=*) - sysconfdir=$ac_optarg ;; - - -target | --target | --targe | --targ | --tar | --ta | --t) - ac_prev=target_alias ;; - -target=* | --target=* | --targe=* | --targ=* | --tar=* | --ta=* | --t=*) - target_alias=$ac_optarg ;; - - -v | -verbose | --verbose | --verbos | --verbo | --verb) - verbose=yes ;; - - -version | --version | --versio | --versi | --vers | -V) - ac_init_version=: ;; - - -with-* | --with-*) - ac_useropt=`expr "x$ac_option" : 'x-*with-\([^=]*\)'` - # Reject names that are not valid shell variable names. - expr "x$ac_useropt" : ".*[^-+._$as_cr_alnum]" >/dev/null && - as_fn_error $? "invalid package name: $ac_useropt" - ac_useropt_orig=$ac_useropt - ac_useropt=`$as_echo "$ac_useropt" | sed 's/[-+.]/_/g'` - case $ac_user_opts in - *" -"with_$ac_useropt" -"*) ;; - *) ac_unrecognized_opts="$ac_unrecognized_opts$ac_unrecognized_sep--with-$ac_useropt_orig" - ac_unrecognized_sep=', ';; - esac - eval with_$ac_useropt=\$ac_optarg ;; - - -without-* | --without-*) - ac_useropt=`expr "x$ac_option" : 'x-*without-\(.*\)'` - # Reject names that are not valid shell variable names. - expr "x$ac_useropt" : ".*[^-+._$as_cr_alnum]" >/dev/null && - as_fn_error $? "invalid package name: $ac_useropt" - ac_useropt_orig=$ac_useropt - ac_useropt=`$as_echo "$ac_useropt" | sed 's/[-+.]/_/g'` - case $ac_user_opts in - *" -"with_$ac_useropt" -"*) ;; - *) ac_unrecognized_opts="$ac_unrecognized_opts$ac_unrecognized_sep--without-$ac_useropt_orig" - ac_unrecognized_sep=', ';; - esac - eval with_$ac_useropt=no ;; - - --x) - # Obsolete; use --with-x. - with_x=yes ;; - - -x-includes | --x-includes | --x-include | --x-includ | --x-inclu \ - | --x-incl | --x-inc | --x-in | --x-i) - ac_prev=x_includes ;; - -x-includes=* | --x-includes=* | --x-include=* | --x-includ=* | --x-inclu=* \ - | --x-incl=* | --x-inc=* | --x-in=* | --x-i=*) - x_includes=$ac_optarg ;; - - -x-libraries | --x-libraries | --x-librarie | --x-librari \ - | --x-librar | --x-libra | --x-libr | --x-lib | --x-li | --x-l) - ac_prev=x_libraries ;; - -x-libraries=* | --x-libraries=* | --x-librarie=* | --x-librari=* \ - | --x-librar=* | --x-libra=* | --x-libr=* | --x-lib=* | --x-li=* | --x-l=*) - x_libraries=$ac_optarg ;; - - -*) as_fn_error $? "unrecognized option: \`$ac_option' -Try \`$0 --help' for more information" - ;; - - *=*) - ac_envvar=`expr "x$ac_option" : 'x\([^=]*\)='` - # Reject names that are not valid shell variable names. - case $ac_envvar in #( - '' | [0-9]* | *[!_$as_cr_alnum]* ) - as_fn_error $? "invalid variable name: \`$ac_envvar'" ;; - esac - eval $ac_envvar=\$ac_optarg - export $ac_envvar ;; - - *) - # FIXME: should be removed in autoconf 3.0. - $as_echo "$as_me: WARNING: you should use --build, --host, --target" >&2 - expr "x$ac_option" : ".*[^-._$as_cr_alnum]" >/dev/null && - $as_echo "$as_me: WARNING: invalid host type: $ac_option" >&2 - : "${build_alias=$ac_option} ${host_alias=$ac_option} ${target_alias=$ac_option}" - ;; - - esac -done - -if test -n "$ac_prev"; then - ac_option=--`echo $ac_prev | sed 's/_/-/g'` - as_fn_error $? "missing argument to $ac_option" -fi - -if test -n "$ac_unrecognized_opts"; then - case $enable_option_checking in - no) ;; - fatal) as_fn_error $? "unrecognized options: $ac_unrecognized_opts" ;; - *) $as_echo "$as_me: WARNING: unrecognized options: $ac_unrecognized_opts" >&2 ;; - esac -fi - -# Check all directory arguments for consistency. -for ac_var in exec_prefix prefix bindir sbindir libexecdir datarootdir \ - datadir sysconfdir sharedstatedir localstatedir includedir \ - oldincludedir docdir infodir htmldir dvidir pdfdir psdir \ - libdir localedir mandir -do - eval ac_val=\$$ac_var - # Remove trailing slashes. - case $ac_val in - */ ) - ac_val=`expr "X$ac_val" : 'X\(.*[^/]\)' \| "X$ac_val" : 'X\(.*\)'` - eval $ac_var=\$ac_val;; - esac - # Be sure to have absolute directory names. - case $ac_val in - [\\/$]* | ?:[\\/]* ) continue;; - NONE | '' ) case $ac_var in *prefix ) continue;; esac;; - esac - as_fn_error $? "expected an absolute directory name for --$ac_var: $ac_val" -done - -# There might be people who depend on the old broken behavior: `$host' -# used to hold the argument of --host etc. -# FIXME: To remove some day. -build=$build_alias -host=$host_alias -target=$target_alias - -# FIXME: To remove some day. -if test "x$host_alias" != x; then - if test "x$build_alias" = x; then - cross_compiling=maybe - elif test "x$build_alias" != "x$host_alias"; then - cross_compiling=yes - fi -fi - -ac_tool_prefix= -test -n "$host_alias" && ac_tool_prefix=$host_alias- - -test "$silent" = yes && exec 6>/dev/null - - -ac_pwd=`pwd` && test -n "$ac_pwd" && -ac_ls_di=`ls -di .` && -ac_pwd_ls_di=`cd "$ac_pwd" && ls -di .` || - as_fn_error $? "working directory cannot be determined" -test "X$ac_ls_di" = "X$ac_pwd_ls_di" || - as_fn_error $? "pwd does not report name of working directory" - - -# Find the source files, if location was not specified. -if test -z "$srcdir"; then - ac_srcdir_defaulted=yes - # Try the directory containing this script, then the parent directory. - ac_confdir=`$as_dirname -- "$as_myself" || -$as_expr X"$as_myself" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \ - X"$as_myself" : 'X\(//\)[^/]' \| \ - X"$as_myself" : 'X\(//\)$' \| \ - X"$as_myself" : 'X\(/\)' \| . 2>/dev/null || -$as_echo X"$as_myself" | - sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{ - s//\1/ - q - } - /^X\(\/\/\)[^/].*/{ - s//\1/ - q - } - /^X\(\/\/\)$/{ - s//\1/ - q - } - /^X\(\/\).*/{ - s//\1/ - q - } - s/.*/./; q'` - srcdir=$ac_confdir - if test ! -r "$srcdir/$ac_unique_file"; then - srcdir=.. - fi -else - ac_srcdir_defaulted=no -fi -if test ! -r "$srcdir/$ac_unique_file"; then - test "$ac_srcdir_defaulted" = yes && srcdir="$ac_confdir or .." - as_fn_error $? "cannot find sources ($ac_unique_file) in $srcdir" -fi -ac_msg="sources are in $srcdir, but \`cd $srcdir' does not work" -ac_abs_confdir=`( - cd "$srcdir" && test -r "./$ac_unique_file" || as_fn_error $? "$ac_msg" - pwd)` -# When building in place, set srcdir=. -if test "$ac_abs_confdir" = "$ac_pwd"; then - srcdir=. -fi -# Remove unnecessary trailing slashes from srcdir. -# Double slashes in file names in object file debugging info -# mess up M-x gdb in Emacs. -case $srcdir in -*/) srcdir=`expr "X$srcdir" : 'X\(.*[^/]\)' \| "X$srcdir" : 'X\(.*\)'`;; -esac -for ac_var in $ac_precious_vars; do - eval ac_env_${ac_var}_set=\${${ac_var}+set} - eval ac_env_${ac_var}_value=\$${ac_var} - eval ac_cv_env_${ac_var}_set=\${${ac_var}+set} - eval ac_cv_env_${ac_var}_value=\$${ac_var} -done - -# -# Report the --help message. -# -if test "$ac_init_help" = "long"; then - # Omit some internal or obsolete options to make the list less imposing. - # This message is too long to be a string in the A/UX 3.1 sh. - cat <<_ACEOF -\`configure' configures sqlcipher 3.11.0 to adapt to many kinds of systems. - -Usage: $0 [OPTION]... [VAR=VALUE]... - -To assign environment variables (e.g., CC, CFLAGS...), specify them as -VAR=VALUE. See below for descriptions of some of the useful variables. - -Defaults for the options are specified in brackets. - -Configuration: - -h, --help display this help and exit - --help=short display options specific to this package - --help=recursive display the short help of all the included packages - -V, --version display version information and exit - -q, --quiet, --silent do not print \`checking ...' messages - --cache-file=FILE cache test results in FILE [disabled] - -C, --config-cache alias for \`--cache-file=config.cache' - -n, --no-create do not create output files - --srcdir=DIR find the sources in DIR [configure dir or \`..'] - -Installation directories: - --prefix=PREFIX install architecture-independent files in PREFIX - [$ac_default_prefix] - --exec-prefix=EPREFIX install architecture-dependent files in EPREFIX - [PREFIX] - -By default, \`make install' will install all the files in -\`$ac_default_prefix/bin', \`$ac_default_prefix/lib' etc. You can specify -an installation prefix other than \`$ac_default_prefix' using \`--prefix', -for instance \`--prefix=\$HOME'. - -For better control, use the options below. - -Fine tuning of the installation directories: - --bindir=DIR user executables [EPREFIX/bin] - --sbindir=DIR system admin executables [EPREFIX/sbin] - --libexecdir=DIR program executables [EPREFIX/libexec] - --sysconfdir=DIR read-only single-machine data [PREFIX/etc] - --sharedstatedir=DIR modifiable architecture-independent data [PREFIX/com] - --localstatedir=DIR modifiable single-machine data [PREFIX/var] - --libdir=DIR object code libraries [EPREFIX/lib] - --includedir=DIR C header files [PREFIX/include] - --oldincludedir=DIR C header files for non-gcc [/usr/include] - --datarootdir=DIR read-only arch.-independent data root [PREFIX/share] - --datadir=DIR read-only architecture-independent data [DATAROOTDIR] - --infodir=DIR info documentation [DATAROOTDIR/info] - --localedir=DIR locale-dependent data [DATAROOTDIR/locale] - --mandir=DIR man documentation [DATAROOTDIR/man] - --docdir=DIR documentation root [DATAROOTDIR/doc/sqlcipher] - --htmldir=DIR html documentation [DOCDIR] - --dvidir=DIR dvi documentation [DOCDIR] - --pdfdir=DIR pdf documentation [DOCDIR] - --psdir=DIR ps documentation [DOCDIR] -_ACEOF - - cat <<\_ACEOF - -System types: - --build=BUILD configure for building on BUILD [guessed] - --host=HOST cross-compile to build programs to run on HOST [BUILD] -_ACEOF -fi - -if test -n "$ac_init_help"; then - case $ac_init_help in - short | recursive ) echo "Configuration of sqlcipher 3.11.0:";; - esac - cat <<\_ACEOF - -Optional Features: - --disable-option-checking ignore unrecognized --enable/--with options - --disable-FEATURE do not include FEATURE (same as --enable-FEATURE=no) - --enable-FEATURE[=ARG] include FEATURE [ARG=yes] - --enable-shared[=PKGS] build shared libraries [default=yes] - --enable-static[=PKGS] build static libraries [default=yes] - --enable-fast-install[=PKGS] - optimize for fast installation [default=yes] - --disable-libtool-lock avoid locking (might break parallel builds) - --disable-largefile omit support for large files - --disable-threadsafe Disable mutexing - --enable-cross-thread-connections - Allow connection sharing across threads - --enable-releasemode Support libtool link to release mode - --enable-tempstore Use an in-ram database for temporary tables - (never,no,yes,always) - --disable-tcl do not build TCL extension - --enable-editline enable BSD editline support - --disable-readline disable readline support - --enable-debug enable debugging & verbose explain - --disable-amalgamation Disable the amalgamation and instead build all files - separately - --disable-load-extension - Disable loading of external extensions - --enable-fts3 Enable the FTS3 extension - --enable-fts4 Enable the FTS4 extension - --enable-fts5 Enable the FTS5 extension - --enable-json1 Enable the JSON1 extension - --enable-rtree Enable the RTREE extension - --enable-gcov Enable coverage testing using gcov - -Optional Packages: - --with-PACKAGE[=ARG] use PACKAGE [ARG=yes] - --without-PACKAGE do not use PACKAGE (same as --with-PACKAGE=no) - --with-pic[=PKGS] try to use only PIC/non-PIC objects [default=use - both] - --with-aix-soname=aix|svr4|both - shared library versioning (aka "SONAME") variant to - provide on AIX, [default=aix]. - --with-gnu-ld assume the C compiler uses GNU ld [default=no] - --with-sysroot[=DIR] Search for dependent libraries within DIR (or the - compiler's sysroot if not specified). - --with-crypto-lib Specify which crypto library to use - --with-tcl=DIR directory containing tcl configuration - (tclConfig.sh) - --with-readline-lib specify readline library - --with-readline-inc specify readline include paths - -Some influential environment variables: - CC C compiler command - CFLAGS C compiler flags - LDFLAGS linker flags, e.g. -L if you have libraries in a - nonstandard directory - LIBS libraries to pass to the linker, e.g. -l - CPPFLAGS (Objective) C/C++ preprocessor flags, e.g. -I if - you have headers in a nonstandard directory - LT_SYS_LIBRARY_PATH - User-defined run-time library search path. - CPP C preprocessor - TCLLIBDIR Where to install tcl plugin - -Use these variables to override the choices made by `configure' or to help -it to find libraries and programs with nonstandard names/locations. - -Report bugs to the package provider. -_ACEOF -ac_status=$? -fi - -if test "$ac_init_help" = "recursive"; then - # If there are subdirs, report their specific --help. - for ac_dir in : $ac_subdirs_all; do test "x$ac_dir" = x: && continue - test -d "$ac_dir" || - { cd "$srcdir" && ac_pwd=`pwd` && srcdir=. && test -d "$ac_dir"; } || - continue - ac_builddir=. - -case "$ac_dir" in -.) ac_dir_suffix= ac_top_builddir_sub=. ac_top_build_prefix= ;; -*) - ac_dir_suffix=/`$as_echo "$ac_dir" | sed 's|^\.[\\/]||'` - # A ".." for each directory in $ac_dir_suffix. - ac_top_builddir_sub=`$as_echo "$ac_dir_suffix" | sed 's|/[^\\/]*|/..|g;s|/||'` - case $ac_top_builddir_sub in - "") ac_top_builddir_sub=. ac_top_build_prefix= ;; - *) ac_top_build_prefix=$ac_top_builddir_sub/ ;; - esac ;; -esac -ac_abs_top_builddir=$ac_pwd -ac_abs_builddir=$ac_pwd$ac_dir_suffix -# for backward compatibility: -ac_top_builddir=$ac_top_build_prefix - -case $srcdir in - .) # We are building in place. - ac_srcdir=. - ac_top_srcdir=$ac_top_builddir_sub - ac_abs_top_srcdir=$ac_pwd ;; - [\\/]* | ?:[\\/]* ) # Absolute name. - ac_srcdir=$srcdir$ac_dir_suffix; - ac_top_srcdir=$srcdir - ac_abs_top_srcdir=$srcdir ;; - *) # Relative name. - ac_srcdir=$ac_top_build_prefix$srcdir$ac_dir_suffix - ac_top_srcdir=$ac_top_build_prefix$srcdir - ac_abs_top_srcdir=$ac_pwd/$srcdir ;; -esac -ac_abs_srcdir=$ac_abs_top_srcdir$ac_dir_suffix - - cd "$ac_dir" || { ac_status=$?; continue; } - # Check for guested configure. - if test -f "$ac_srcdir/configure.gnu"; then - echo && - $SHELL "$ac_srcdir/configure.gnu" --help=recursive - elif test -f "$ac_srcdir/configure"; then - echo && - $SHELL "$ac_srcdir/configure" --help=recursive - else - $as_echo "$as_me: WARNING: no configuration information is in $ac_dir" >&2 - fi || ac_status=$? - cd "$ac_pwd" || { ac_status=$?; break; } - done -fi - -test -n "$ac_init_help" && exit $ac_status -if $ac_init_version; then - cat <<\_ACEOF -sqlcipher configure 3.11.0 -generated by GNU Autoconf 2.69 - -Copyright (C) 2012 Free Software Foundation, Inc. -This configure script is free software; the Free Software Foundation -gives unlimited permission to copy, distribute and modify it. -_ACEOF - exit -fi - -## ------------------------ ## -## Autoconf initialization. ## -## ------------------------ ## - -# ac_fn_c_try_compile LINENO -# -------------------------- -# Try to compile conftest.$ac_ext, and return whether this succeeded. -ac_fn_c_try_compile () -{ - as_lineno=${as_lineno-"$1"} as_lineno_stack=as_lineno_stack=$as_lineno_stack - rm -f conftest.$ac_objext - if { { ac_try="$ac_compile" -case "(($ac_try" in - *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;; - *) ac_try_echo=$ac_try;; -esac -eval ac_try_echo="\"\$as_me:${as_lineno-$LINENO}: $ac_try_echo\"" -$as_echo "$ac_try_echo"; } >&5 - (eval "$ac_compile") 2>conftest.err - ac_status=$? - if test -s conftest.err; then - grep -v '^ *+' conftest.err >conftest.er1 - cat conftest.er1 >&5 - mv -f conftest.er1 conftest.err - fi - $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5 - test $ac_status = 0; } && { - test -z "$ac_c_werror_flag" || - test ! -s conftest.err - } && test -s conftest.$ac_objext; then : - ac_retval=0 -else - $as_echo "$as_me: failed program was:" >&5 -sed 's/^/| /' conftest.$ac_ext >&5 - - ac_retval=1 -fi - eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno - as_fn_set_status $ac_retval - -} # ac_fn_c_try_compile - -# ac_fn_c_try_link LINENO -# ----------------------- -# Try to link conftest.$ac_ext, and return whether this succeeded. -ac_fn_c_try_link () -{ - as_lineno=${as_lineno-"$1"} as_lineno_stack=as_lineno_stack=$as_lineno_stack - rm -f conftest.$ac_objext conftest$ac_exeext - if { { ac_try="$ac_link" -case "(($ac_try" in - *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;; - *) ac_try_echo=$ac_try;; -esac -eval ac_try_echo="\"\$as_me:${as_lineno-$LINENO}: $ac_try_echo\"" -$as_echo "$ac_try_echo"; } >&5 - (eval "$ac_link") 2>conftest.err - ac_status=$? - if test -s conftest.err; then - grep -v '^ *+' conftest.err >conftest.er1 - cat conftest.er1 >&5 - mv -f conftest.er1 conftest.err - fi - $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5 - test $ac_status = 0; } && { - test -z "$ac_c_werror_flag" || - test ! -s conftest.err - } && test -s conftest$ac_exeext && { - test "$cross_compiling" = yes || - test -x conftest$ac_exeext - }; then : - ac_retval=0 -else - $as_echo "$as_me: failed program was:" >&5 -sed 's/^/| /' conftest.$ac_ext >&5 - - ac_retval=1 -fi - # Delete the IPA/IPO (Inter Procedural Analysis/Optimization) information - # created by the PGI compiler (conftest_ipa8_conftest.oo), as it would - # interfere with the next link command; also delete a directory that is - # left behind by Apple's compiler. We do this before executing the actions. - rm -rf conftest.dSYM conftest_ipa8_conftest.oo - eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno - as_fn_set_status $ac_retval - -} # ac_fn_c_try_link - -# ac_fn_c_check_header_compile LINENO HEADER VAR INCLUDES -# ------------------------------------------------------- -# Tests whether HEADER exists and can be compiled using the include files in -# INCLUDES, setting the cache variable VAR accordingly. -ac_fn_c_check_header_compile () -{ - as_lineno=${as_lineno-"$1"} as_lineno_stack=as_lineno_stack=$as_lineno_stack - { $as_echo "$as_me:${as_lineno-$LINENO}: checking for $2" >&5 -$as_echo_n "checking for $2... " >&6; } -if eval \${$3+:} false; then : - $as_echo_n "(cached) " >&6 -else - cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ -$4 -#include <$2> -_ACEOF -if ac_fn_c_try_compile "$LINENO"; then : - eval "$3=yes" -else - eval "$3=no" -fi -rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext -fi -eval ac_res=\$$3 - { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_res" >&5 -$as_echo "$ac_res" >&6; } - eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno - -} # ac_fn_c_check_header_compile - -# ac_fn_c_try_cpp LINENO -# ---------------------- -# Try to preprocess conftest.$ac_ext, and return whether this succeeded. -ac_fn_c_try_cpp () -{ - as_lineno=${as_lineno-"$1"} as_lineno_stack=as_lineno_stack=$as_lineno_stack - if { { ac_try="$ac_cpp conftest.$ac_ext" -case "(($ac_try" in - *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;; - *) ac_try_echo=$ac_try;; -esac -eval ac_try_echo="\"\$as_me:${as_lineno-$LINENO}: $ac_try_echo\"" -$as_echo "$ac_try_echo"; } >&5 - (eval "$ac_cpp conftest.$ac_ext") 2>conftest.err - ac_status=$? - if test -s conftest.err; then - grep -v '^ *+' conftest.err >conftest.er1 - cat conftest.er1 >&5 - mv -f conftest.er1 conftest.err - fi - $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5 - test $ac_status = 0; } > conftest.i && { - test -z "$ac_c_preproc_warn_flag$ac_c_werror_flag" || - test ! -s conftest.err - }; then : - ac_retval=0 -else - $as_echo "$as_me: failed program was:" >&5 -sed 's/^/| /' conftest.$ac_ext >&5 - - ac_retval=1 -fi - eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno - as_fn_set_status $ac_retval - -} # ac_fn_c_try_cpp - -# ac_fn_c_try_run LINENO -# ---------------------- -# Try to link conftest.$ac_ext, and return whether this succeeded. Assumes -# that executables *can* be run. -ac_fn_c_try_run () -{ - as_lineno=${as_lineno-"$1"} as_lineno_stack=as_lineno_stack=$as_lineno_stack - if { { ac_try="$ac_link" -case "(($ac_try" in - *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;; - *) ac_try_echo=$ac_try;; -esac -eval ac_try_echo="\"\$as_me:${as_lineno-$LINENO}: $ac_try_echo\"" -$as_echo "$ac_try_echo"; } >&5 - (eval "$ac_link") 2>&5 - ac_status=$? - $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5 - test $ac_status = 0; } && { ac_try='./conftest$ac_exeext' - { { case "(($ac_try" in - *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;; - *) ac_try_echo=$ac_try;; -esac -eval ac_try_echo="\"\$as_me:${as_lineno-$LINENO}: $ac_try_echo\"" -$as_echo "$ac_try_echo"; } >&5 - (eval "$ac_try") 2>&5 - ac_status=$? - $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5 - test $ac_status = 0; }; }; then : - ac_retval=0 -else - $as_echo "$as_me: program exited with status $ac_status" >&5 - $as_echo "$as_me: failed program was:" >&5 -sed 's/^/| /' conftest.$ac_ext >&5 - - ac_retval=$ac_status -fi - rm -rf conftest.dSYM conftest_ipa8_conftest.oo - eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno - as_fn_set_status $ac_retval - -} # ac_fn_c_try_run - -# ac_fn_c_check_func LINENO FUNC VAR -# ---------------------------------- -# Tests whether FUNC exists, setting the cache variable VAR accordingly -ac_fn_c_check_func () -{ - as_lineno=${as_lineno-"$1"} as_lineno_stack=as_lineno_stack=$as_lineno_stack - { $as_echo "$as_me:${as_lineno-$LINENO}: checking for $2" >&5 -$as_echo_n "checking for $2... " >&6; } -if eval \${$3+:} false; then : - $as_echo_n "(cached) " >&6 -else - cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ -/* Define $2 to an innocuous variant, in case declares $2. - For example, HP-UX 11i declares gettimeofday. */ -#define $2 innocuous_$2 - -/* System header to define __stub macros and hopefully few prototypes, - which can conflict with char $2 (); below. - Prefer to if __STDC__ is defined, since - exists even on freestanding compilers. */ - -#ifdef __STDC__ -# include -#else -# include -#endif - -#undef $2 - -/* Override any GCC internal prototype to avoid an error. - Use char because int might match the return type of a GCC - builtin and then its argument prototype would still apply. */ -#ifdef __cplusplus -extern "C" -#endif -char $2 (); -/* The GNU C library defines this for functions which it implements - to always fail with ENOSYS. Some functions are actually named - something starting with __ and the normal name is an alias. */ -#if defined __stub_$2 || defined __stub___$2 -choke me -#endif - -int -main () -{ -return $2 (); - ; - return 0; -} -_ACEOF -if ac_fn_c_try_link "$LINENO"; then : - eval "$3=yes" -else - eval "$3=no" -fi -rm -f core conftest.err conftest.$ac_objext \ - conftest$ac_exeext conftest.$ac_ext -fi -eval ac_res=\$$3 - { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_res" >&5 -$as_echo "$ac_res" >&6; } - eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno - -} # ac_fn_c_check_func - -# ac_fn_c_check_type LINENO TYPE VAR INCLUDES -# ------------------------------------------- -# Tests whether TYPE exists after having included INCLUDES, setting cache -# variable VAR accordingly. -ac_fn_c_check_type () -{ - as_lineno=${as_lineno-"$1"} as_lineno_stack=as_lineno_stack=$as_lineno_stack - { $as_echo "$as_me:${as_lineno-$LINENO}: checking for $2" >&5 -$as_echo_n "checking for $2... " >&6; } -if eval \${$3+:} false; then : - $as_echo_n "(cached) " >&6 -else - eval "$3=no" - cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ -$4 -int -main () -{ -if (sizeof ($2)) - return 0; - ; - return 0; -} -_ACEOF -if ac_fn_c_try_compile "$LINENO"; then : - cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ -$4 -int -main () -{ -if (sizeof (($2))) - return 0; - ; - return 0; -} -_ACEOF -if ac_fn_c_try_compile "$LINENO"; then : - -else - eval "$3=yes" -fi -rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext -fi -rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext -fi -eval ac_res=\$$3 - { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_res" >&5 -$as_echo "$ac_res" >&6; } - eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno - -} # ac_fn_c_check_type - -# ac_fn_c_check_header_mongrel LINENO HEADER VAR INCLUDES -# ------------------------------------------------------- -# Tests whether HEADER exists, giving a warning if it cannot be compiled using -# the include files in INCLUDES and setting the cache variable VAR -# accordingly. -ac_fn_c_check_header_mongrel () -{ - as_lineno=${as_lineno-"$1"} as_lineno_stack=as_lineno_stack=$as_lineno_stack - if eval \${$3+:} false; then : - { $as_echo "$as_me:${as_lineno-$LINENO}: checking for $2" >&5 -$as_echo_n "checking for $2... " >&6; } -if eval \${$3+:} false; then : - $as_echo_n "(cached) " >&6 -fi -eval ac_res=\$$3 - { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_res" >&5 -$as_echo "$ac_res" >&6; } -else - # Is the header compilable? -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking $2 usability" >&5 -$as_echo_n "checking $2 usability... " >&6; } -cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ -$4 -#include <$2> -_ACEOF -if ac_fn_c_try_compile "$LINENO"; then : - ac_header_compiler=yes -else - ac_header_compiler=no -fi -rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_header_compiler" >&5 -$as_echo "$ac_header_compiler" >&6; } - -# Is the header present? -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking $2 presence" >&5 -$as_echo_n "checking $2 presence... " >&6; } -cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ -#include <$2> -_ACEOF -if ac_fn_c_try_cpp "$LINENO"; then : - ac_header_preproc=yes -else - ac_header_preproc=no -fi -rm -f conftest.err conftest.i conftest.$ac_ext -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_header_preproc" >&5 -$as_echo "$ac_header_preproc" >&6; } - -# So? What about this header? -case $ac_header_compiler:$ac_header_preproc:$ac_c_preproc_warn_flag in #(( - yes:no: ) - { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: $2: accepted by the compiler, rejected by the preprocessor!" >&5 -$as_echo "$as_me: WARNING: $2: accepted by the compiler, rejected by the preprocessor!" >&2;} - { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: $2: proceeding with the compiler's result" >&5 -$as_echo "$as_me: WARNING: $2: proceeding with the compiler's result" >&2;} - ;; - no:yes:* ) - { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: $2: present but cannot be compiled" >&5 -$as_echo "$as_me: WARNING: $2: present but cannot be compiled" >&2;} - { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: $2: check for missing prerequisite headers?" >&5 -$as_echo "$as_me: WARNING: $2: check for missing prerequisite headers?" >&2;} - { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: $2: see the Autoconf documentation" >&5 -$as_echo "$as_me: WARNING: $2: see the Autoconf documentation" >&2;} - { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: $2: section \"Present But Cannot Be Compiled\"" >&5 -$as_echo "$as_me: WARNING: $2: section \"Present But Cannot Be Compiled\"" >&2;} - { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: $2: proceeding with the compiler's result" >&5 -$as_echo "$as_me: WARNING: $2: proceeding with the compiler's result" >&2;} - ;; -esac - { $as_echo "$as_me:${as_lineno-$LINENO}: checking for $2" >&5 -$as_echo_n "checking for $2... " >&6; } -if eval \${$3+:} false; then : - $as_echo_n "(cached) " >&6 -else - eval "$3=\$ac_header_compiler" -fi -eval ac_res=\$$3 - { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_res" >&5 -$as_echo "$ac_res" >&6; } -fi - eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno - -} # ac_fn_c_check_header_mongrel -cat >config.log <<_ACEOF -This file contains any messages produced by compilers while -running configure, to aid debugging if configure makes a mistake. - -It was created by sqlcipher $as_me 3.11.0, which was -generated by GNU Autoconf 2.69. Invocation command line was - - $ $0 $@ - -_ACEOF -exec 5>>config.log -{ -cat <<_ASUNAME -## --------- ## -## Platform. ## -## --------- ## - -hostname = `(hostname || uname -n) 2>/dev/null | sed 1q` -uname -m = `(uname -m) 2>/dev/null || echo unknown` -uname -r = `(uname -r) 2>/dev/null || echo unknown` -uname -s = `(uname -s) 2>/dev/null || echo unknown` -uname -v = `(uname -v) 2>/dev/null || echo unknown` - -/usr/bin/uname -p = `(/usr/bin/uname -p) 2>/dev/null || echo unknown` -/bin/uname -X = `(/bin/uname -X) 2>/dev/null || echo unknown` - -/bin/arch = `(/bin/arch) 2>/dev/null || echo unknown` -/usr/bin/arch -k = `(/usr/bin/arch -k) 2>/dev/null || echo unknown` -/usr/convex/getsysinfo = `(/usr/convex/getsysinfo) 2>/dev/null || echo unknown` -/usr/bin/hostinfo = `(/usr/bin/hostinfo) 2>/dev/null || echo unknown` -/bin/machine = `(/bin/machine) 2>/dev/null || echo unknown` -/usr/bin/oslevel = `(/usr/bin/oslevel) 2>/dev/null || echo unknown` -/bin/universe = `(/bin/universe) 2>/dev/null || echo unknown` - -_ASUNAME - -as_save_IFS=$IFS; IFS=$PATH_SEPARATOR -for as_dir in $PATH -do - IFS=$as_save_IFS - test -z "$as_dir" && as_dir=. - $as_echo "PATH: $as_dir" - done -IFS=$as_save_IFS - -} >&5 - -cat >&5 <<_ACEOF - - -## ----------- ## -## Core tests. ## -## ----------- ## - -_ACEOF - - -# Keep a trace of the command line. -# Strip out --no-create and --no-recursion so they do not pile up. -# Strip out --silent because we don't want to record it for future runs. -# Also quote any args containing shell meta-characters. -# Make two passes to allow for proper duplicate-argument suppression. -ac_configure_args= -ac_configure_args0= -ac_configure_args1= -ac_must_keep_next=false -for ac_pass in 1 2 -do - for ac_arg - do - case $ac_arg in - -no-create | --no-c* | -n | -no-recursion | --no-r*) continue ;; - -q | -quiet | --quiet | --quie | --qui | --qu | --q \ - | -silent | --silent | --silen | --sile | --sil) - continue ;; - *\'*) - ac_arg=`$as_echo "$ac_arg" | sed "s/'/'\\\\\\\\''/g"` ;; - esac - case $ac_pass in - 1) as_fn_append ac_configure_args0 " '$ac_arg'" ;; - 2) - as_fn_append ac_configure_args1 " '$ac_arg'" - if test $ac_must_keep_next = true; then - ac_must_keep_next=false # Got value, back to normal. - else - case $ac_arg in - *=* | --config-cache | -C | -disable-* | --disable-* \ - | -enable-* | --enable-* | -gas | --g* | -nfp | --nf* \ - | -q | -quiet | --q* | -silent | --sil* | -v | -verb* \ - | -with-* | --with-* | -without-* | --without-* | --x) - case "$ac_configure_args0 " in - "$ac_configure_args1"*" '$ac_arg' "* ) continue ;; - esac - ;; - -* ) ac_must_keep_next=true ;; - esac - fi - as_fn_append ac_configure_args " '$ac_arg'" - ;; - esac - done -done -{ ac_configure_args0=; unset ac_configure_args0;} -{ ac_configure_args1=; unset ac_configure_args1;} - -# When interrupted or exit'd, cleanup temporary files, and complete -# config.log. We remove comments because anyway the quotes in there -# would cause problems or look ugly. -# WARNING: Use '\'' to represent an apostrophe within the trap. -# WARNING: Do not start the trap code with a newline, due to a FreeBSD 4.0 bug. -trap 'exit_status=$? - # Save into config.log some information that might help in debugging. - { - echo - - $as_echo "## ---------------- ## -## Cache variables. ## -## ---------------- ##" - echo - # The following way of writing the cache mishandles newlines in values, -( - for ac_var in `(set) 2>&1 | sed -n '\''s/^\([a-zA-Z_][a-zA-Z0-9_]*\)=.*/\1/p'\''`; do - eval ac_val=\$$ac_var - case $ac_val in #( - *${as_nl}*) - case $ac_var in #( - *_cv_*) { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: cache variable $ac_var contains a newline" >&5 -$as_echo "$as_me: WARNING: cache variable $ac_var contains a newline" >&2;} ;; - esac - case $ac_var in #( - _ | IFS | as_nl) ;; #( - BASH_ARGV | BASH_SOURCE) eval $ac_var= ;; #( - *) { eval $ac_var=; unset $ac_var;} ;; - esac ;; - esac - done - (set) 2>&1 | - case $as_nl`(ac_space='\'' '\''; set) 2>&1` in #( - *${as_nl}ac_space=\ *) - sed -n \ - "s/'\''/'\''\\\\'\'''\''/g; - s/^\\([_$as_cr_alnum]*_cv_[_$as_cr_alnum]*\\)=\\(.*\\)/\\1='\''\\2'\''/p" - ;; #( - *) - sed -n "/^[_$as_cr_alnum]*_cv_[_$as_cr_alnum]*=/p" - ;; - esac | - sort -) - echo - - $as_echo "## ----------------- ## -## Output variables. ## -## ----------------- ##" - echo - for ac_var in $ac_subst_vars - do - eval ac_val=\$$ac_var - case $ac_val in - *\'\''*) ac_val=`$as_echo "$ac_val" | sed "s/'\''/'\''\\\\\\\\'\'''\''/g"`;; - esac - $as_echo "$ac_var='\''$ac_val'\''" - done | sort - echo - - if test -n "$ac_subst_files"; then - $as_echo "## ------------------- ## -## File substitutions. ## -## ------------------- ##" - echo - for ac_var in $ac_subst_files - do - eval ac_val=\$$ac_var - case $ac_val in - *\'\''*) ac_val=`$as_echo "$ac_val" | sed "s/'\''/'\''\\\\\\\\'\'''\''/g"`;; - esac - $as_echo "$ac_var='\''$ac_val'\''" - done | sort - echo - fi - - if test -s confdefs.h; then - $as_echo "## ----------- ## -## confdefs.h. ## -## ----------- ##" - echo - cat confdefs.h - echo - fi - test "$ac_signal" != 0 && - $as_echo "$as_me: caught signal $ac_signal" - $as_echo "$as_me: exit $exit_status" - } >&5 - rm -f core *.core core.conftest.* && - rm -f -r conftest* confdefs* conf$$* $ac_clean_files && - exit $exit_status -' 0 -for ac_signal in 1 2 13 15; do - trap 'ac_signal='$ac_signal'; as_fn_exit 1' $ac_signal -done -ac_signal=0 - -# confdefs.h avoids OS command line length limits that DEFS can exceed. -rm -f -r conftest* confdefs.h - -$as_echo "/* confdefs.h */" > confdefs.h - -# Predefined preprocessor variables. - -cat >>confdefs.h <<_ACEOF -#define PACKAGE_NAME "$PACKAGE_NAME" -_ACEOF - -cat >>confdefs.h <<_ACEOF -#define PACKAGE_TARNAME "$PACKAGE_TARNAME" -_ACEOF - -cat >>confdefs.h <<_ACEOF -#define PACKAGE_VERSION "$PACKAGE_VERSION" -_ACEOF - -cat >>confdefs.h <<_ACEOF -#define PACKAGE_STRING "$PACKAGE_STRING" -_ACEOF - -cat >>confdefs.h <<_ACEOF -#define PACKAGE_BUGREPORT "$PACKAGE_BUGREPORT" -_ACEOF - -cat >>confdefs.h <<_ACEOF -#define PACKAGE_URL "$PACKAGE_URL" -_ACEOF - - -# Let the site file select an alternate cache file if it wants to. -# Prefer an explicitly selected file to automatically selected ones. -ac_site_file1=NONE -ac_site_file2=NONE -if test -n "$CONFIG_SITE"; then - # We do not want a PATH search for config.site. - case $CONFIG_SITE in #(( - -*) ac_site_file1=./$CONFIG_SITE;; - */*) ac_site_file1=$CONFIG_SITE;; - *) ac_site_file1=./$CONFIG_SITE;; - esac -elif test "x$prefix" != xNONE; then - ac_site_file1=$prefix/share/config.site - ac_site_file2=$prefix/etc/config.site -else - ac_site_file1=$ac_default_prefix/share/config.site - ac_site_file2=$ac_default_prefix/etc/config.site -fi -for ac_site_file in "$ac_site_file1" "$ac_site_file2" -do - test "x$ac_site_file" = xNONE && continue - if test /dev/null != "$ac_site_file" && test -r "$ac_site_file"; then - { $as_echo "$as_me:${as_lineno-$LINENO}: loading site script $ac_site_file" >&5 -$as_echo "$as_me: loading site script $ac_site_file" >&6;} - sed 's/^/| /' "$ac_site_file" >&5 - . "$ac_site_file" \ - || { { $as_echo "$as_me:${as_lineno-$LINENO}: error: in \`$ac_pwd':" >&5 -$as_echo "$as_me: error: in \`$ac_pwd':" >&2;} -as_fn_error $? "failed to load site script $ac_site_file -See \`config.log' for more details" "$LINENO" 5; } - fi -done - -if test -r "$cache_file"; then - # Some versions of bash will fail to source /dev/null (special files - # actually), so we avoid doing that. DJGPP emulates it as a regular file. - if test /dev/null != "$cache_file" && test -f "$cache_file"; then - { $as_echo "$as_me:${as_lineno-$LINENO}: loading cache $cache_file" >&5 -$as_echo "$as_me: loading cache $cache_file" >&6;} - case $cache_file in - [\\/]* | ?:[\\/]* ) . "$cache_file";; - *) . "./$cache_file";; - esac - fi -else - { $as_echo "$as_me:${as_lineno-$LINENO}: creating cache $cache_file" >&5 -$as_echo "$as_me: creating cache $cache_file" >&6;} - >$cache_file -fi - -# Check that the precious variables saved in the cache have kept the same -# value. -ac_cache_corrupted=false -for ac_var in $ac_precious_vars; do - eval ac_old_set=\$ac_cv_env_${ac_var}_set - eval ac_new_set=\$ac_env_${ac_var}_set - eval ac_old_val=\$ac_cv_env_${ac_var}_value - eval ac_new_val=\$ac_env_${ac_var}_value - case $ac_old_set,$ac_new_set in - set,) - { $as_echo "$as_me:${as_lineno-$LINENO}: error: \`$ac_var' was set to \`$ac_old_val' in the previous run" >&5 -$as_echo "$as_me: error: \`$ac_var' was set to \`$ac_old_val' in the previous run" >&2;} - ac_cache_corrupted=: ;; - ,set) - { $as_echo "$as_me:${as_lineno-$LINENO}: error: \`$ac_var' was not set in the previous run" >&5 -$as_echo "$as_me: error: \`$ac_var' was not set in the previous run" >&2;} - ac_cache_corrupted=: ;; - ,);; - *) - if test "x$ac_old_val" != "x$ac_new_val"; then - # differences in whitespace do not lead to failure. - ac_old_val_w=`echo x $ac_old_val` - ac_new_val_w=`echo x $ac_new_val` - if test "$ac_old_val_w" != "$ac_new_val_w"; then - { $as_echo "$as_me:${as_lineno-$LINENO}: error: \`$ac_var' has changed since the previous run:" >&5 -$as_echo "$as_me: error: \`$ac_var' has changed since the previous run:" >&2;} - ac_cache_corrupted=: - else - { $as_echo "$as_me:${as_lineno-$LINENO}: warning: ignoring whitespace changes in \`$ac_var' since the previous run:" >&5 -$as_echo "$as_me: warning: ignoring whitespace changes in \`$ac_var' since the previous run:" >&2;} - eval $ac_var=\$ac_old_val - fi - { $as_echo "$as_me:${as_lineno-$LINENO}: former value: \`$ac_old_val'" >&5 -$as_echo "$as_me: former value: \`$ac_old_val'" >&2;} - { $as_echo "$as_me:${as_lineno-$LINENO}: current value: \`$ac_new_val'" >&5 -$as_echo "$as_me: current value: \`$ac_new_val'" >&2;} - fi;; - esac - # Pass precious variables to config.status. - if test "$ac_new_set" = set; then - case $ac_new_val in - *\'*) ac_arg=$ac_var=`$as_echo "$ac_new_val" | sed "s/'/'\\\\\\\\''/g"` ;; - *) ac_arg=$ac_var=$ac_new_val ;; - esac - case " $ac_configure_args " in - *" '$ac_arg' "*) ;; # Avoid dups. Use of quotes ensures accuracy. - *) as_fn_append ac_configure_args " '$ac_arg'" ;; - esac - fi -done -if $ac_cache_corrupted; then - { $as_echo "$as_me:${as_lineno-$LINENO}: error: in \`$ac_pwd':" >&5 -$as_echo "$as_me: error: in \`$ac_pwd':" >&2;} - { $as_echo "$as_me:${as_lineno-$LINENO}: error: changes in the environment can compromise the build" >&5 -$as_echo "$as_me: error: changes in the environment can compromise the build" >&2;} - as_fn_error $? "run \`make distclean' and/or \`rm $cache_file' and start over" "$LINENO" 5 -fi -## -------------------- ## -## Main body of script. ## -## -------------------- ## - -ac_ext=c -ac_cpp='$CPP $CPPFLAGS' -ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5' -ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5' -ac_compiler_gnu=$ac_cv_c_compiler_gnu - - - -sqlite_version_sanity_check=`cat $srcdir/VERSION | tr -d '\n'` -if test "$PACKAGE_VERSION" != "$sqlite_version_sanity_check" ; then -as_fn_error $? "configure script is out of date: - configure \$PACKAGE_VERSION = $PACKAGE_VERSION - top level VERSION file = $sqlite_version_sanity_check -please regen with autoconf" "$LINENO" 5 -fi - -######### -# Programs needed -# -ac_aux_dir= -for ac_dir in "$srcdir" "$srcdir/.." "$srcdir/../.."; do - if test -f "$ac_dir/install-sh"; then - ac_aux_dir=$ac_dir - ac_install_sh="$ac_aux_dir/install-sh -c" - break - elif test -f "$ac_dir/install.sh"; then - ac_aux_dir=$ac_dir - ac_install_sh="$ac_aux_dir/install.sh -c" - break - elif test -f "$ac_dir/shtool"; then - ac_aux_dir=$ac_dir - ac_install_sh="$ac_aux_dir/shtool install -c" - break - fi -done -if test -z "$ac_aux_dir"; then - as_fn_error $? "cannot find install-sh, install.sh, or shtool in \"$srcdir\" \"$srcdir/..\" \"$srcdir/../..\"" "$LINENO" 5 -fi - -# These three variables are undocumented and unsupported, -# and are intended to be withdrawn in a future Autoconf release. -# They can cause serious problems if a builder's source tree is in a directory -# whose full name contains unusual characters. -ac_config_guess="$SHELL $ac_aux_dir/config.guess" # Please don't use this var. -ac_config_sub="$SHELL $ac_aux_dir/config.sub" # Please don't use this var. -ac_configure="$SHELL $ac_aux_dir/configure" # Please don't use this var. - - -case `pwd` in - *\ * | *\ *) - { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: Libtool does not cope well with whitespace in \`pwd\`" >&5 -$as_echo "$as_me: WARNING: Libtool does not cope well with whitespace in \`pwd\`" >&2;} ;; -esac - - - -macro_version='2.4.6' -macro_revision='2.4.6' - - - - - - - - - - - - - -ltmain=$ac_aux_dir/ltmain.sh - -# Make sure we can run config.sub. -$SHELL "$ac_aux_dir/config.sub" sun4 >/dev/null 2>&1 || - as_fn_error $? "cannot run $SHELL $ac_aux_dir/config.sub" "$LINENO" 5 - -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking build system type" >&5 -$as_echo_n "checking build system type... " >&6; } -if ${ac_cv_build+:} false; then : - $as_echo_n "(cached) " >&6 -else - ac_build_alias=$build_alias -test "x$ac_build_alias" = x && - ac_build_alias=`$SHELL "$ac_aux_dir/config.guess"` -test "x$ac_build_alias" = x && - as_fn_error $? "cannot guess build type; you must specify one" "$LINENO" 5 -ac_cv_build=`$SHELL "$ac_aux_dir/config.sub" $ac_build_alias` || - as_fn_error $? "$SHELL $ac_aux_dir/config.sub $ac_build_alias failed" "$LINENO" 5 - -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_build" >&5 -$as_echo "$ac_cv_build" >&6; } -case $ac_cv_build in -*-*-*) ;; -*) as_fn_error $? "invalid value of canonical build" "$LINENO" 5;; -esac -build=$ac_cv_build -ac_save_IFS=$IFS; IFS='-' -set x $ac_cv_build -shift -build_cpu=$1 -build_vendor=$2 -shift; shift -# Remember, the first character of IFS is used to create $*, -# except with old shells: -build_os=$* -IFS=$ac_save_IFS -case $build_os in *\ *) build_os=`echo "$build_os" | sed 's/ /-/g'`;; esac - - -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking host system type" >&5 -$as_echo_n "checking host system type... " >&6; } -if ${ac_cv_host+:} false; then : - $as_echo_n "(cached) " >&6 -else - if test "x$host_alias" = x; then - ac_cv_host=$ac_cv_build -else - ac_cv_host=`$SHELL "$ac_aux_dir/config.sub" $host_alias` || - as_fn_error $? "$SHELL $ac_aux_dir/config.sub $host_alias failed" "$LINENO" 5 -fi - -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_host" >&5 -$as_echo "$ac_cv_host" >&6; } -case $ac_cv_host in -*-*-*) ;; -*) as_fn_error $? "invalid value of canonical host" "$LINENO" 5;; -esac -host=$ac_cv_host -ac_save_IFS=$IFS; IFS='-' -set x $ac_cv_host -shift -host_cpu=$1 -host_vendor=$2 -shift; shift -# Remember, the first character of IFS is used to create $*, -# except with old shells: -host_os=$* -IFS=$ac_save_IFS -case $host_os in *\ *) host_os=`echo "$host_os" | sed 's/ /-/g'`;; esac - - -# Backslashify metacharacters that are still active within -# double-quoted strings. -sed_quote_subst='s/\(["`$\\]\)/\\\1/g' - -# Same as above, but do not quote variable references. -double_quote_subst='s/\(["`\\]\)/\\\1/g' - -# Sed substitution to delay expansion of an escaped shell variable in a -# double_quote_subst'ed string. -delay_variable_subst='s/\\\\\\\\\\\$/\\\\\\$/g' - -# Sed substitution to delay expansion of an escaped single quote. -delay_single_quote_subst='s/'\''/'\'\\\\\\\'\''/g' - -# Sed substitution to avoid accidental globbing in evaled expressions -no_glob_subst='s/\*/\\\*/g' - -ECHO='\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\' -ECHO=$ECHO$ECHO$ECHO$ECHO$ECHO -ECHO=$ECHO$ECHO$ECHO$ECHO$ECHO$ECHO - -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking how to print strings" >&5 -$as_echo_n "checking how to print strings... " >&6; } -# Test print first, because it will be a builtin if present. -if test "X`( print -r -- -n ) 2>/dev/null`" = X-n && \ - test "X`print -r -- $ECHO 2>/dev/null`" = "X$ECHO"; then - ECHO='print -r --' -elif test "X`printf %s $ECHO 2>/dev/null`" = "X$ECHO"; then - ECHO='printf %s\n' -else - # Use this function as a fallback that always works. - func_fallback_echo () - { - eval 'cat <<_LTECHO_EOF -$1 -_LTECHO_EOF' - } - ECHO='func_fallback_echo' -fi - -# func_echo_all arg... -# Invoke $ECHO with all args, space-separated. -func_echo_all () -{ - $ECHO "" -} - -case $ECHO in - printf*) { $as_echo "$as_me:${as_lineno-$LINENO}: result: printf" >&5 -$as_echo "printf" >&6; } ;; - print*) { $as_echo "$as_me:${as_lineno-$LINENO}: result: print -r" >&5 -$as_echo "print -r" >&6; } ;; - *) { $as_echo "$as_me:${as_lineno-$LINENO}: result: cat" >&5 -$as_echo "cat" >&6; } ;; -esac - - - - - - - - - - - - - - -ac_ext=c -ac_cpp='$CPP $CPPFLAGS' -ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5' -ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5' -ac_compiler_gnu=$ac_cv_c_compiler_gnu -if test -n "$ac_tool_prefix"; then - # Extract the first word of "${ac_tool_prefix}gcc", so it can be a program name with args. -set dummy ${ac_tool_prefix}gcc; ac_word=$2 -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5 -$as_echo_n "checking for $ac_word... " >&6; } -if ${ac_cv_prog_CC+:} false; then : - $as_echo_n "(cached) " >&6 -else - if test -n "$CC"; then - ac_cv_prog_CC="$CC" # Let the user override the test. -else -as_save_IFS=$IFS; IFS=$PATH_SEPARATOR -for as_dir in $PATH -do - IFS=$as_save_IFS - test -z "$as_dir" && as_dir=. - for ac_exec_ext in '' $ac_executable_extensions; do - if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then - ac_cv_prog_CC="${ac_tool_prefix}gcc" - $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5 - break 2 - fi -done - done -IFS=$as_save_IFS - -fi -fi -CC=$ac_cv_prog_CC -if test -n "$CC"; then - { $as_echo "$as_me:${as_lineno-$LINENO}: result: $CC" >&5 -$as_echo "$CC" >&6; } -else - { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 -$as_echo "no" >&6; } -fi - - -fi -if test -z "$ac_cv_prog_CC"; then - ac_ct_CC=$CC - # Extract the first word of "gcc", so it can be a program name with args. -set dummy gcc; ac_word=$2 -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5 -$as_echo_n "checking for $ac_word... " >&6; } -if ${ac_cv_prog_ac_ct_CC+:} false; then : - $as_echo_n "(cached) " >&6 -else - if test -n "$ac_ct_CC"; then - ac_cv_prog_ac_ct_CC="$ac_ct_CC" # Let the user override the test. -else -as_save_IFS=$IFS; IFS=$PATH_SEPARATOR -for as_dir in $PATH -do - IFS=$as_save_IFS - test -z "$as_dir" && as_dir=. - for ac_exec_ext in '' $ac_executable_extensions; do - if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then - ac_cv_prog_ac_ct_CC="gcc" - $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5 - break 2 - fi -done - done -IFS=$as_save_IFS - -fi -fi -ac_ct_CC=$ac_cv_prog_ac_ct_CC -if test -n "$ac_ct_CC"; then - { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_ct_CC" >&5 -$as_echo "$ac_ct_CC" >&6; } -else - { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 -$as_echo "no" >&6; } -fi - - if test "x$ac_ct_CC" = x; then - CC="" - else - case $cross_compiling:$ac_tool_warned in -yes:) -{ $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: using cross tools not prefixed with host triplet" >&5 -$as_echo "$as_me: WARNING: using cross tools not prefixed with host triplet" >&2;} -ac_tool_warned=yes ;; -esac - CC=$ac_ct_CC - fi -else - CC="$ac_cv_prog_CC" -fi - -if test -z "$CC"; then - if test -n "$ac_tool_prefix"; then - # Extract the first word of "${ac_tool_prefix}cc", so it can be a program name with args. -set dummy ${ac_tool_prefix}cc; ac_word=$2 -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5 -$as_echo_n "checking for $ac_word... " >&6; } -if ${ac_cv_prog_CC+:} false; then : - $as_echo_n "(cached) " >&6 -else - if test -n "$CC"; then - ac_cv_prog_CC="$CC" # Let the user override the test. -else -as_save_IFS=$IFS; IFS=$PATH_SEPARATOR -for as_dir in $PATH -do - IFS=$as_save_IFS - test -z "$as_dir" && as_dir=. - for ac_exec_ext in '' $ac_executable_extensions; do - if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then - ac_cv_prog_CC="${ac_tool_prefix}cc" - $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5 - break 2 - fi -done - done -IFS=$as_save_IFS - -fi -fi -CC=$ac_cv_prog_CC -if test -n "$CC"; then - { $as_echo "$as_me:${as_lineno-$LINENO}: result: $CC" >&5 -$as_echo "$CC" >&6; } -else - { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 -$as_echo "no" >&6; } -fi - - - fi -fi -if test -z "$CC"; then - # Extract the first word of "cc", so it can be a program name with args. -set dummy cc; ac_word=$2 -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5 -$as_echo_n "checking for $ac_word... " >&6; } -if ${ac_cv_prog_CC+:} false; then : - $as_echo_n "(cached) " >&6 -else - if test -n "$CC"; then - ac_cv_prog_CC="$CC" # Let the user override the test. -else - ac_prog_rejected=no -as_save_IFS=$IFS; IFS=$PATH_SEPARATOR -for as_dir in $PATH -do - IFS=$as_save_IFS - test -z "$as_dir" && as_dir=. - for ac_exec_ext in '' $ac_executable_extensions; do - if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then - if test "$as_dir/$ac_word$ac_exec_ext" = "/usr/ucb/cc"; then - ac_prog_rejected=yes - continue - fi - ac_cv_prog_CC="cc" - $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5 - break 2 - fi -done - done -IFS=$as_save_IFS - -if test $ac_prog_rejected = yes; then - # We found a bogon in the path, so make sure we never use it. - set dummy $ac_cv_prog_CC - shift - if test $# != 0; then - # We chose a different compiler from the bogus one. - # However, it has the same basename, so the bogon will be chosen - # first if we set CC to just the basename; use the full file name. - shift - ac_cv_prog_CC="$as_dir/$ac_word${1+' '}$@" - fi -fi -fi -fi -CC=$ac_cv_prog_CC -if test -n "$CC"; then - { $as_echo "$as_me:${as_lineno-$LINENO}: result: $CC" >&5 -$as_echo "$CC" >&6; } -else - { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 -$as_echo "no" >&6; } -fi - - -fi -if test -z "$CC"; then - if test -n "$ac_tool_prefix"; then - for ac_prog in cl.exe - do - # Extract the first word of "$ac_tool_prefix$ac_prog", so it can be a program name with args. -set dummy $ac_tool_prefix$ac_prog; ac_word=$2 -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5 -$as_echo_n "checking for $ac_word... " >&6; } -if ${ac_cv_prog_CC+:} false; then : - $as_echo_n "(cached) " >&6 -else - if test -n "$CC"; then - ac_cv_prog_CC="$CC" # Let the user override the test. -else -as_save_IFS=$IFS; IFS=$PATH_SEPARATOR -for as_dir in $PATH -do - IFS=$as_save_IFS - test -z "$as_dir" && as_dir=. - for ac_exec_ext in '' $ac_executable_extensions; do - if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then - ac_cv_prog_CC="$ac_tool_prefix$ac_prog" - $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5 - break 2 - fi -done - done -IFS=$as_save_IFS - -fi -fi -CC=$ac_cv_prog_CC -if test -n "$CC"; then - { $as_echo "$as_me:${as_lineno-$LINENO}: result: $CC" >&5 -$as_echo "$CC" >&6; } -else - { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 -$as_echo "no" >&6; } -fi - - - test -n "$CC" && break - done -fi -if test -z "$CC"; then - ac_ct_CC=$CC - for ac_prog in cl.exe -do - # Extract the first word of "$ac_prog", so it can be a program name with args. -set dummy $ac_prog; ac_word=$2 -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5 -$as_echo_n "checking for $ac_word... " >&6; } -if ${ac_cv_prog_ac_ct_CC+:} false; then : - $as_echo_n "(cached) " >&6 -else - if test -n "$ac_ct_CC"; then - ac_cv_prog_ac_ct_CC="$ac_ct_CC" # Let the user override the test. -else -as_save_IFS=$IFS; IFS=$PATH_SEPARATOR -for as_dir in $PATH -do - IFS=$as_save_IFS - test -z "$as_dir" && as_dir=. - for ac_exec_ext in '' $ac_executable_extensions; do - if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then - ac_cv_prog_ac_ct_CC="$ac_prog" - $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5 - break 2 - fi -done - done -IFS=$as_save_IFS - -fi -fi -ac_ct_CC=$ac_cv_prog_ac_ct_CC -if test -n "$ac_ct_CC"; then - { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_ct_CC" >&5 -$as_echo "$ac_ct_CC" >&6; } -else - { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 -$as_echo "no" >&6; } -fi - - - test -n "$ac_ct_CC" && break -done - - if test "x$ac_ct_CC" = x; then - CC="" - else - case $cross_compiling:$ac_tool_warned in -yes:) -{ $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: using cross tools not prefixed with host triplet" >&5 -$as_echo "$as_me: WARNING: using cross tools not prefixed with host triplet" >&2;} -ac_tool_warned=yes ;; -esac - CC=$ac_ct_CC - fi -fi - -fi - - -test -z "$CC" && { { $as_echo "$as_me:${as_lineno-$LINENO}: error: in \`$ac_pwd':" >&5 -$as_echo "$as_me: error: in \`$ac_pwd':" >&2;} -as_fn_error $? "no acceptable C compiler found in \$PATH -See \`config.log' for more details" "$LINENO" 5; } - -# Provide some information about the compiler. -$as_echo "$as_me:${as_lineno-$LINENO}: checking for C compiler version" >&5 -set X $ac_compile -ac_compiler=$2 -for ac_option in --version -v -V -qversion; do - { { ac_try="$ac_compiler $ac_option >&5" -case "(($ac_try" in - *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;; - *) ac_try_echo=$ac_try;; -esac -eval ac_try_echo="\"\$as_me:${as_lineno-$LINENO}: $ac_try_echo\"" -$as_echo "$ac_try_echo"; } >&5 - (eval "$ac_compiler $ac_option >&5") 2>conftest.err - ac_status=$? - if test -s conftest.err; then - sed '10a\ -... rest of stderr output deleted ... - 10q' conftest.err >conftest.er1 - cat conftest.er1 >&5 - fi - rm -f conftest.er1 conftest.err - $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5 - test $ac_status = 0; } -done - -cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ - -int -main () -{ - - ; - return 0; -} -_ACEOF -ac_clean_files_save=$ac_clean_files -ac_clean_files="$ac_clean_files a.out a.out.dSYM a.exe b.out" -# Try to create an executable without -o first, disregard a.out. -# It will help us diagnose broken compilers, and finding out an intuition -# of exeext. -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether the C compiler works" >&5 -$as_echo_n "checking whether the C compiler works... " >&6; } -ac_link_default=`$as_echo "$ac_link" | sed 's/ -o *conftest[^ ]*//'` - -# The possible output files: -ac_files="a.out conftest.exe conftest a.exe a_out.exe b.out conftest.*" - -ac_rmfiles= -for ac_file in $ac_files -do - case $ac_file in - *.$ac_ext | *.xcoff | *.tds | *.d | *.pdb | *.xSYM | *.bb | *.bbg | *.map | *.inf | *.dSYM | *.o | *.obj ) ;; - * ) ac_rmfiles="$ac_rmfiles $ac_file";; - esac -done -rm -f $ac_rmfiles - -if { { ac_try="$ac_link_default" -case "(($ac_try" in - *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;; - *) ac_try_echo=$ac_try;; -esac -eval ac_try_echo="\"\$as_me:${as_lineno-$LINENO}: $ac_try_echo\"" -$as_echo "$ac_try_echo"; } >&5 - (eval "$ac_link_default") 2>&5 - ac_status=$? - $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5 - test $ac_status = 0; }; then : - # Autoconf-2.13 could set the ac_cv_exeext variable to `no'. -# So ignore a value of `no', otherwise this would lead to `EXEEXT = no' -# in a Makefile. We should not override ac_cv_exeext if it was cached, -# so that the user can short-circuit this test for compilers unknown to -# Autoconf. -for ac_file in $ac_files '' -do - test -f "$ac_file" || continue - case $ac_file in - *.$ac_ext | *.xcoff | *.tds | *.d | *.pdb | *.xSYM | *.bb | *.bbg | *.map | *.inf | *.dSYM | *.o | *.obj ) - ;; - [ab].out ) - # We found the default executable, but exeext='' is most - # certainly right. - break;; - *.* ) - if test "${ac_cv_exeext+set}" = set && test "$ac_cv_exeext" != no; - then :; else - ac_cv_exeext=`expr "$ac_file" : '[^.]*\(\..*\)'` - fi - # We set ac_cv_exeext here because the later test for it is not - # safe: cross compilers may not add the suffix if given an `-o' - # argument, so we may need to know it at that point already. - # Even if this section looks crufty: it has the advantage of - # actually working. - break;; - * ) - break;; - esac -done -test "$ac_cv_exeext" = no && ac_cv_exeext= - -else - ac_file='' -fi -if test -z "$ac_file"; then : - { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 -$as_echo "no" >&6; } -$as_echo "$as_me: failed program was:" >&5 -sed 's/^/| /' conftest.$ac_ext >&5 - -{ { $as_echo "$as_me:${as_lineno-$LINENO}: error: in \`$ac_pwd':" >&5 -$as_echo "$as_me: error: in \`$ac_pwd':" >&2;} -as_fn_error 77 "C compiler cannot create executables -See \`config.log' for more details" "$LINENO" 5; } -else - { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5 -$as_echo "yes" >&6; } -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for C compiler default output file name" >&5 -$as_echo_n "checking for C compiler default output file name... " >&6; } -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_file" >&5 -$as_echo "$ac_file" >&6; } -ac_exeext=$ac_cv_exeext - -rm -f -r a.out a.out.dSYM a.exe conftest$ac_cv_exeext b.out -ac_clean_files=$ac_clean_files_save -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for suffix of executables" >&5 -$as_echo_n "checking for suffix of executables... " >&6; } -if { { ac_try="$ac_link" -case "(($ac_try" in - *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;; - *) ac_try_echo=$ac_try;; -esac -eval ac_try_echo="\"\$as_me:${as_lineno-$LINENO}: $ac_try_echo\"" -$as_echo "$ac_try_echo"; } >&5 - (eval "$ac_link") 2>&5 - ac_status=$? - $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5 - test $ac_status = 0; }; then : - # If both `conftest.exe' and `conftest' are `present' (well, observable) -# catch `conftest.exe'. For instance with Cygwin, `ls conftest' will -# work properly (i.e., refer to `conftest.exe'), while it won't with -# `rm'. -for ac_file in conftest.exe conftest conftest.*; do - test -f "$ac_file" || continue - case $ac_file in - *.$ac_ext | *.xcoff | *.tds | *.d | *.pdb | *.xSYM | *.bb | *.bbg | *.map | *.inf | *.dSYM | *.o | *.obj ) ;; - *.* ) ac_cv_exeext=`expr "$ac_file" : '[^.]*\(\..*\)'` - break;; - * ) break;; - esac -done -else - { { $as_echo "$as_me:${as_lineno-$LINENO}: error: in \`$ac_pwd':" >&5 -$as_echo "$as_me: error: in \`$ac_pwd':" >&2;} -as_fn_error $? "cannot compute suffix of executables: cannot compile and link -See \`config.log' for more details" "$LINENO" 5; } -fi -rm -f conftest conftest$ac_cv_exeext -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_exeext" >&5 -$as_echo "$ac_cv_exeext" >&6; } - -rm -f conftest.$ac_ext -EXEEXT=$ac_cv_exeext -ac_exeext=$EXEEXT -cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ -#include -int -main () -{ -FILE *f = fopen ("conftest.out", "w"); - return ferror (f) || fclose (f) != 0; - - ; - return 0; -} -_ACEOF -ac_clean_files="$ac_clean_files conftest.out" -# Check that the compiler produces executables we can run. If not, either -# the compiler is broken, or we cross compile. -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether we are cross compiling" >&5 -$as_echo_n "checking whether we are cross compiling... " >&6; } -if test "$cross_compiling" != yes; then - { { ac_try="$ac_link" -case "(($ac_try" in - *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;; - *) ac_try_echo=$ac_try;; -esac -eval ac_try_echo="\"\$as_me:${as_lineno-$LINENO}: $ac_try_echo\"" -$as_echo "$ac_try_echo"; } >&5 - (eval "$ac_link") 2>&5 - ac_status=$? - $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5 - test $ac_status = 0; } - if { ac_try='./conftest$ac_cv_exeext' - { { case "(($ac_try" in - *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;; - *) ac_try_echo=$ac_try;; -esac -eval ac_try_echo="\"\$as_me:${as_lineno-$LINENO}: $ac_try_echo\"" -$as_echo "$ac_try_echo"; } >&5 - (eval "$ac_try") 2>&5 - ac_status=$? - $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5 - test $ac_status = 0; }; }; then - cross_compiling=no - else - if test "$cross_compiling" = maybe; then - cross_compiling=yes - else - { { $as_echo "$as_me:${as_lineno-$LINENO}: error: in \`$ac_pwd':" >&5 -$as_echo "$as_me: error: in \`$ac_pwd':" >&2;} -as_fn_error $? "cannot run C compiled programs. -If you meant to cross compile, use \`--host'. -See \`config.log' for more details" "$LINENO" 5; } - fi - fi -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $cross_compiling" >&5 -$as_echo "$cross_compiling" >&6; } - -rm -f conftest.$ac_ext conftest$ac_cv_exeext conftest.out -ac_clean_files=$ac_clean_files_save -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for suffix of object files" >&5 -$as_echo_n "checking for suffix of object files... " >&6; } -if ${ac_cv_objext+:} false; then : - $as_echo_n "(cached) " >&6 -else - cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ - -int -main () -{ - - ; - return 0; -} -_ACEOF -rm -f conftest.o conftest.obj -if { { ac_try="$ac_compile" -case "(($ac_try" in - *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;; - *) ac_try_echo=$ac_try;; -esac -eval ac_try_echo="\"\$as_me:${as_lineno-$LINENO}: $ac_try_echo\"" -$as_echo "$ac_try_echo"; } >&5 - (eval "$ac_compile") 2>&5 - ac_status=$? - $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5 - test $ac_status = 0; }; then : - for ac_file in conftest.o conftest.obj conftest.*; do - test -f "$ac_file" || continue; - case $ac_file in - *.$ac_ext | *.xcoff | *.tds | *.d | *.pdb | *.xSYM | *.bb | *.bbg | *.map | *.inf | *.dSYM ) ;; - *) ac_cv_objext=`expr "$ac_file" : '.*\.\(.*\)'` - break;; - esac -done -else - $as_echo "$as_me: failed program was:" >&5 -sed 's/^/| /' conftest.$ac_ext >&5 - -{ { $as_echo "$as_me:${as_lineno-$LINENO}: error: in \`$ac_pwd':" >&5 -$as_echo "$as_me: error: in \`$ac_pwd':" >&2;} -as_fn_error $? "cannot compute suffix of object files: cannot compile -See \`config.log' for more details" "$LINENO" 5; } -fi -rm -f conftest.$ac_cv_objext conftest.$ac_ext -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_objext" >&5 -$as_echo "$ac_cv_objext" >&6; } -OBJEXT=$ac_cv_objext -ac_objext=$OBJEXT -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether we are using the GNU C compiler" >&5 -$as_echo_n "checking whether we are using the GNU C compiler... " >&6; } -if ${ac_cv_c_compiler_gnu+:} false; then : - $as_echo_n "(cached) " >&6 -else - cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ - -int -main () -{ -#ifndef __GNUC__ - choke me -#endif - - ; - return 0; -} -_ACEOF -if ac_fn_c_try_compile "$LINENO"; then : - ac_compiler_gnu=yes -else - ac_compiler_gnu=no -fi -rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext -ac_cv_c_compiler_gnu=$ac_compiler_gnu - -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_c_compiler_gnu" >&5 -$as_echo "$ac_cv_c_compiler_gnu" >&6; } -if test $ac_compiler_gnu = yes; then - GCC=yes -else - GCC= -fi -ac_test_CFLAGS=${CFLAGS+set} -ac_save_CFLAGS=$CFLAGS -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether $CC accepts -g" >&5 -$as_echo_n "checking whether $CC accepts -g... " >&6; } -if ${ac_cv_prog_cc_g+:} false; then : - $as_echo_n "(cached) " >&6 -else - ac_save_c_werror_flag=$ac_c_werror_flag - ac_c_werror_flag=yes - ac_cv_prog_cc_g=no - CFLAGS="-g" - cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ - -int -main () -{ - - ; - return 0; -} -_ACEOF -if ac_fn_c_try_compile "$LINENO"; then : - ac_cv_prog_cc_g=yes -else - CFLAGS="" - cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ - -int -main () -{ - - ; - return 0; -} -_ACEOF -if ac_fn_c_try_compile "$LINENO"; then : - -else - ac_c_werror_flag=$ac_save_c_werror_flag - CFLAGS="-g" - cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ - -int -main () -{ - - ; - return 0; -} -_ACEOF -if ac_fn_c_try_compile "$LINENO"; then : - ac_cv_prog_cc_g=yes -fi -rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext -fi -rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext -fi -rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext - ac_c_werror_flag=$ac_save_c_werror_flag -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_prog_cc_g" >&5 -$as_echo "$ac_cv_prog_cc_g" >&6; } -if test "$ac_test_CFLAGS" = set; then - CFLAGS=$ac_save_CFLAGS -elif test $ac_cv_prog_cc_g = yes; then - if test "$GCC" = yes; then - CFLAGS="-g -O2" - else - CFLAGS="-g" - fi -else - if test "$GCC" = yes; then - CFLAGS="-O2" - else - CFLAGS= - fi -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $CC option to accept ISO C89" >&5 -$as_echo_n "checking for $CC option to accept ISO C89... " >&6; } -if ${ac_cv_prog_cc_c89+:} false; then : - $as_echo_n "(cached) " >&6 -else - ac_cv_prog_cc_c89=no -ac_save_CC=$CC -cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ -#include -#include -struct stat; -/* Most of the following tests are stolen from RCS 5.7's src/conf.sh. */ -struct buf { int x; }; -FILE * (*rcsopen) (struct buf *, struct stat *, int); -static char *e (p, i) - char **p; - int i; -{ - return p[i]; -} -static char *f (char * (*g) (char **, int), char **p, ...) -{ - char *s; - va_list v; - va_start (v,p); - s = g (p, va_arg (v,int)); - va_end (v); - return s; -} - -/* OSF 4.0 Compaq cc is some sort of almost-ANSI by default. It has - function prototypes and stuff, but not '\xHH' hex character constants. - These don't provoke an error unfortunately, instead are silently treated - as 'x'. The following induces an error, until -std is added to get - proper ANSI mode. Curiously '\x00'!='x' always comes out true, for an - array size at least. It's necessary to write '\x00'==0 to get something - that's true only with -std. */ -int osf4_cc_array ['\x00' == 0 ? 1 : -1]; - -/* IBM C 6 for AIX is almost-ANSI by default, but it replaces macro parameters - inside strings and character constants. */ -#define FOO(x) 'x' -int xlc6_cc_array[FOO(a) == 'x' ? 1 : -1]; - -int test (int i, double x); -struct s1 {int (*f) (int a);}; -struct s2 {int (*f) (double a);}; -int pairnames (int, char **, FILE *(*)(struct buf *, struct stat *, int), int, int); -int argc; -char **argv; -int -main () -{ -return f (e, argv, 0) != argv[0] || f (e, argv, 1) != argv[1]; - ; - return 0; -} -_ACEOF -for ac_arg in '' -qlanglvl=extc89 -qlanglvl=ansi -std \ - -Ae "-Aa -D_HPUX_SOURCE" "-Xc -D__EXTENSIONS__" -do - CC="$ac_save_CC $ac_arg" - if ac_fn_c_try_compile "$LINENO"; then : - ac_cv_prog_cc_c89=$ac_arg -fi -rm -f core conftest.err conftest.$ac_objext - test "x$ac_cv_prog_cc_c89" != "xno" && break -done -rm -f conftest.$ac_ext -CC=$ac_save_CC - -fi -# AC_CACHE_VAL -case "x$ac_cv_prog_cc_c89" in - x) - { $as_echo "$as_me:${as_lineno-$LINENO}: result: none needed" >&5 -$as_echo "none needed" >&6; } ;; - xno) - { $as_echo "$as_me:${as_lineno-$LINENO}: result: unsupported" >&5 -$as_echo "unsupported" >&6; } ;; - *) - CC="$CC $ac_cv_prog_cc_c89" - { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_prog_cc_c89" >&5 -$as_echo "$ac_cv_prog_cc_c89" >&6; } ;; -esac -if test "x$ac_cv_prog_cc_c89" != xno; then : - -fi - -ac_ext=c -ac_cpp='$CPP $CPPFLAGS' -ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5' -ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5' -ac_compiler_gnu=$ac_cv_c_compiler_gnu - -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for a sed that does not truncate output" >&5 -$as_echo_n "checking for a sed that does not truncate output... " >&6; } -if ${ac_cv_path_SED+:} false; then : - $as_echo_n "(cached) " >&6 -else - ac_script=s/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb/ - for ac_i in 1 2 3 4 5 6 7; do - ac_script="$ac_script$as_nl$ac_script" - done - echo "$ac_script" 2>/dev/null | sed 99q >conftest.sed - { ac_script=; unset ac_script;} - if test -z "$SED"; then - ac_path_SED_found=false - # Loop through the user's path and test for each of PROGNAME-LIST - as_save_IFS=$IFS; IFS=$PATH_SEPARATOR -for as_dir in $PATH -do - IFS=$as_save_IFS - test -z "$as_dir" && as_dir=. - for ac_prog in sed gsed; do - for ac_exec_ext in '' $ac_executable_extensions; do - ac_path_SED="$as_dir/$ac_prog$ac_exec_ext" - as_fn_executable_p "$ac_path_SED" || continue -# Check for GNU ac_path_SED and select it if it is found. - # Check for GNU $ac_path_SED -case `"$ac_path_SED" --version 2>&1` in -*GNU*) - ac_cv_path_SED="$ac_path_SED" ac_path_SED_found=:;; -*) - ac_count=0 - $as_echo_n 0123456789 >"conftest.in" - while : - do - cat "conftest.in" "conftest.in" >"conftest.tmp" - mv "conftest.tmp" "conftest.in" - cp "conftest.in" "conftest.nl" - $as_echo '' >> "conftest.nl" - "$ac_path_SED" -f conftest.sed < "conftest.nl" >"conftest.out" 2>/dev/null || break - diff "conftest.out" "conftest.nl" >/dev/null 2>&1 || break - as_fn_arith $ac_count + 1 && ac_count=$as_val - if test $ac_count -gt ${ac_path_SED_max-0}; then - # Best one so far, save it but keep looking for a better one - ac_cv_path_SED="$ac_path_SED" - ac_path_SED_max=$ac_count - fi - # 10*(2^10) chars as input seems more than enough - test $ac_count -gt 10 && break - done - rm -f conftest.in conftest.tmp conftest.nl conftest.out;; -esac - - $ac_path_SED_found && break 3 - done - done - done -IFS=$as_save_IFS - if test -z "$ac_cv_path_SED"; then - as_fn_error $? "no acceptable sed could be found in \$PATH" "$LINENO" 5 - fi -else - ac_cv_path_SED=$SED -fi - -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_path_SED" >&5 -$as_echo "$ac_cv_path_SED" >&6; } - SED="$ac_cv_path_SED" - rm -f conftest.sed - -test -z "$SED" && SED=sed -Xsed="$SED -e 1s/^X//" - - - - - - - - - - - -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for grep that handles long lines and -e" >&5 -$as_echo_n "checking for grep that handles long lines and -e... " >&6; } -if ${ac_cv_path_GREP+:} false; then : - $as_echo_n "(cached) " >&6 -else - if test -z "$GREP"; then - ac_path_GREP_found=false - # Loop through the user's path and test for each of PROGNAME-LIST - as_save_IFS=$IFS; IFS=$PATH_SEPARATOR -for as_dir in $PATH$PATH_SEPARATOR/usr/xpg4/bin -do - IFS=$as_save_IFS - test -z "$as_dir" && as_dir=. - for ac_prog in grep ggrep; do - for ac_exec_ext in '' $ac_executable_extensions; do - ac_path_GREP="$as_dir/$ac_prog$ac_exec_ext" - as_fn_executable_p "$ac_path_GREP" || continue -# Check for GNU ac_path_GREP and select it if it is found. - # Check for GNU $ac_path_GREP -case `"$ac_path_GREP" --version 2>&1` in -*GNU*) - ac_cv_path_GREP="$ac_path_GREP" ac_path_GREP_found=:;; -*) - ac_count=0 - $as_echo_n 0123456789 >"conftest.in" - while : - do - cat "conftest.in" "conftest.in" >"conftest.tmp" - mv "conftest.tmp" "conftest.in" - cp "conftest.in" "conftest.nl" - $as_echo 'GREP' >> "conftest.nl" - "$ac_path_GREP" -e 'GREP$' -e '-(cannot match)-' < "conftest.nl" >"conftest.out" 2>/dev/null || break - diff "conftest.out" "conftest.nl" >/dev/null 2>&1 || break - as_fn_arith $ac_count + 1 && ac_count=$as_val - if test $ac_count -gt ${ac_path_GREP_max-0}; then - # Best one so far, save it but keep looking for a better one - ac_cv_path_GREP="$ac_path_GREP" - ac_path_GREP_max=$ac_count - fi - # 10*(2^10) chars as input seems more than enough - test $ac_count -gt 10 && break - done - rm -f conftest.in conftest.tmp conftest.nl conftest.out;; -esac - - $ac_path_GREP_found && break 3 - done - done - done -IFS=$as_save_IFS - if test -z "$ac_cv_path_GREP"; then - as_fn_error $? "no acceptable grep could be found in $PATH$PATH_SEPARATOR/usr/xpg4/bin" "$LINENO" 5 - fi -else - ac_cv_path_GREP=$GREP -fi - -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_path_GREP" >&5 -$as_echo "$ac_cv_path_GREP" >&6; } - GREP="$ac_cv_path_GREP" - - -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for egrep" >&5 -$as_echo_n "checking for egrep... " >&6; } -if ${ac_cv_path_EGREP+:} false; then : - $as_echo_n "(cached) " >&6 -else - if echo a | $GREP -E '(a|b)' >/dev/null 2>&1 - then ac_cv_path_EGREP="$GREP -E" - else - if test -z "$EGREP"; then - ac_path_EGREP_found=false - # Loop through the user's path and test for each of PROGNAME-LIST - as_save_IFS=$IFS; IFS=$PATH_SEPARATOR -for as_dir in $PATH$PATH_SEPARATOR/usr/xpg4/bin -do - IFS=$as_save_IFS - test -z "$as_dir" && as_dir=. - for ac_prog in egrep; do - for ac_exec_ext in '' $ac_executable_extensions; do - ac_path_EGREP="$as_dir/$ac_prog$ac_exec_ext" - as_fn_executable_p "$ac_path_EGREP" || continue -# Check for GNU ac_path_EGREP and select it if it is found. - # Check for GNU $ac_path_EGREP -case `"$ac_path_EGREP" --version 2>&1` in -*GNU*) - ac_cv_path_EGREP="$ac_path_EGREP" ac_path_EGREP_found=:;; -*) - ac_count=0 - $as_echo_n 0123456789 >"conftest.in" - while : - do - cat "conftest.in" "conftest.in" >"conftest.tmp" - mv "conftest.tmp" "conftest.in" - cp "conftest.in" "conftest.nl" - $as_echo 'EGREP' >> "conftest.nl" - "$ac_path_EGREP" 'EGREP$' < "conftest.nl" >"conftest.out" 2>/dev/null || break - diff "conftest.out" "conftest.nl" >/dev/null 2>&1 || break - as_fn_arith $ac_count + 1 && ac_count=$as_val - if test $ac_count -gt ${ac_path_EGREP_max-0}; then - # Best one so far, save it but keep looking for a better one - ac_cv_path_EGREP="$ac_path_EGREP" - ac_path_EGREP_max=$ac_count - fi - # 10*(2^10) chars as input seems more than enough - test $ac_count -gt 10 && break - done - rm -f conftest.in conftest.tmp conftest.nl conftest.out;; -esac - - $ac_path_EGREP_found && break 3 - done - done - done -IFS=$as_save_IFS - if test -z "$ac_cv_path_EGREP"; then - as_fn_error $? "no acceptable egrep could be found in $PATH$PATH_SEPARATOR/usr/xpg4/bin" "$LINENO" 5 - fi -else - ac_cv_path_EGREP=$EGREP -fi - - fi -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_path_EGREP" >&5 -$as_echo "$ac_cv_path_EGREP" >&6; } - EGREP="$ac_cv_path_EGREP" - - -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for fgrep" >&5 -$as_echo_n "checking for fgrep... " >&6; } -if ${ac_cv_path_FGREP+:} false; then : - $as_echo_n "(cached) " >&6 -else - if echo 'ab*c' | $GREP -F 'ab*c' >/dev/null 2>&1 - then ac_cv_path_FGREP="$GREP -F" - else - if test -z "$FGREP"; then - ac_path_FGREP_found=false - # Loop through the user's path and test for each of PROGNAME-LIST - as_save_IFS=$IFS; IFS=$PATH_SEPARATOR -for as_dir in $PATH$PATH_SEPARATOR/usr/xpg4/bin -do - IFS=$as_save_IFS - test -z "$as_dir" && as_dir=. - for ac_prog in fgrep; do - for ac_exec_ext in '' $ac_executable_extensions; do - ac_path_FGREP="$as_dir/$ac_prog$ac_exec_ext" - as_fn_executable_p "$ac_path_FGREP" || continue -# Check for GNU ac_path_FGREP and select it if it is found. - # Check for GNU $ac_path_FGREP -case `"$ac_path_FGREP" --version 2>&1` in -*GNU*) - ac_cv_path_FGREP="$ac_path_FGREP" ac_path_FGREP_found=:;; -*) - ac_count=0 - $as_echo_n 0123456789 >"conftest.in" - while : - do - cat "conftest.in" "conftest.in" >"conftest.tmp" - mv "conftest.tmp" "conftest.in" - cp "conftest.in" "conftest.nl" - $as_echo 'FGREP' >> "conftest.nl" - "$ac_path_FGREP" FGREP < "conftest.nl" >"conftest.out" 2>/dev/null || break - diff "conftest.out" "conftest.nl" >/dev/null 2>&1 || break - as_fn_arith $ac_count + 1 && ac_count=$as_val - if test $ac_count -gt ${ac_path_FGREP_max-0}; then - # Best one so far, save it but keep looking for a better one - ac_cv_path_FGREP="$ac_path_FGREP" - ac_path_FGREP_max=$ac_count - fi - # 10*(2^10) chars as input seems more than enough - test $ac_count -gt 10 && break - done - rm -f conftest.in conftest.tmp conftest.nl conftest.out;; -esac - - $ac_path_FGREP_found && break 3 - done - done - done -IFS=$as_save_IFS - if test -z "$ac_cv_path_FGREP"; then - as_fn_error $? "no acceptable fgrep could be found in $PATH$PATH_SEPARATOR/usr/xpg4/bin" "$LINENO" 5 - fi -else - ac_cv_path_FGREP=$FGREP -fi - - fi -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_path_FGREP" >&5 -$as_echo "$ac_cv_path_FGREP" >&6; } - FGREP="$ac_cv_path_FGREP" - - -test -z "$GREP" && GREP=grep - - - - - - - - - - - - - - - - - - - -# Check whether --with-gnu-ld was given. -if test "${with_gnu_ld+set}" = set; then : - withval=$with_gnu_ld; test no = "$withval" || with_gnu_ld=yes -else - with_gnu_ld=no -fi - -ac_prog=ld -if test yes = "$GCC"; then - # Check if gcc -print-prog-name=ld gives a path. - { $as_echo "$as_me:${as_lineno-$LINENO}: checking for ld used by $CC" >&5 -$as_echo_n "checking for ld used by $CC... " >&6; } - case $host in - *-*-mingw*) - # gcc leaves a trailing carriage return, which upsets mingw - ac_prog=`($CC -print-prog-name=ld) 2>&5 | tr -d '\015'` ;; - *) - ac_prog=`($CC -print-prog-name=ld) 2>&5` ;; - esac - case $ac_prog in - # Accept absolute paths. - [\\/]* | ?:[\\/]*) - re_direlt='/[^/][^/]*/\.\./' - # Canonicalize the pathname of ld - ac_prog=`$ECHO "$ac_prog"| $SED 's%\\\\%/%g'` - while $ECHO "$ac_prog" | $GREP "$re_direlt" > /dev/null 2>&1; do - ac_prog=`$ECHO $ac_prog| $SED "s%$re_direlt%/%"` - done - test -z "$LD" && LD=$ac_prog - ;; - "") - # If it fails, then pretend we aren't using GCC. - ac_prog=ld - ;; - *) - # If it is relative, then search for the first ld in PATH. - with_gnu_ld=unknown - ;; - esac -elif test yes = "$with_gnu_ld"; then - { $as_echo "$as_me:${as_lineno-$LINENO}: checking for GNU ld" >&5 -$as_echo_n "checking for GNU ld... " >&6; } -else - { $as_echo "$as_me:${as_lineno-$LINENO}: checking for non-GNU ld" >&5 -$as_echo_n "checking for non-GNU ld... " >&6; } -fi -if ${lt_cv_path_LD+:} false; then : - $as_echo_n "(cached) " >&6 -else - if test -z "$LD"; then - lt_save_ifs=$IFS; IFS=$PATH_SEPARATOR - for ac_dir in $PATH; do - IFS=$lt_save_ifs - test -z "$ac_dir" && ac_dir=. - if test -f "$ac_dir/$ac_prog" || test -f "$ac_dir/$ac_prog$ac_exeext"; then - lt_cv_path_LD=$ac_dir/$ac_prog - # Check to see if the program is GNU ld. I'd rather use --version, - # but apparently some variants of GNU ld only accept -v. - # Break only if it was the GNU/non-GNU ld that we prefer. - case `"$lt_cv_path_LD" -v 2>&1 &5 -$as_echo "$LD" >&6; } -else - { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 -$as_echo "no" >&6; } -fi -test -z "$LD" && as_fn_error $? "no acceptable ld found in \$PATH" "$LINENO" 5 -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking if the linker ($LD) is GNU ld" >&5 -$as_echo_n "checking if the linker ($LD) is GNU ld... " >&6; } -if ${lt_cv_prog_gnu_ld+:} false; then : - $as_echo_n "(cached) " >&6 -else - # I'd rather use --version here, but apparently some GNU lds only accept -v. -case `$LD -v 2>&1 &5 -$as_echo "$lt_cv_prog_gnu_ld" >&6; } -with_gnu_ld=$lt_cv_prog_gnu_ld - - - - - - - - - -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for BSD- or MS-compatible name lister (nm)" >&5 -$as_echo_n "checking for BSD- or MS-compatible name lister (nm)... " >&6; } -if ${lt_cv_path_NM+:} false; then : - $as_echo_n "(cached) " >&6 -else - if test -n "$NM"; then - # Let the user override the test. - lt_cv_path_NM=$NM -else - lt_nm_to_check=${ac_tool_prefix}nm - if test -n "$ac_tool_prefix" && test "$build" = "$host"; then - lt_nm_to_check="$lt_nm_to_check nm" - fi - for lt_tmp_nm in $lt_nm_to_check; do - lt_save_ifs=$IFS; IFS=$PATH_SEPARATOR - for ac_dir in $PATH /usr/ccs/bin/elf /usr/ccs/bin /usr/ucb /bin; do - IFS=$lt_save_ifs - test -z "$ac_dir" && ac_dir=. - tmp_nm=$ac_dir/$lt_tmp_nm - if test -f "$tmp_nm" || test -f "$tmp_nm$ac_exeext"; then - # Check to see if the nm accepts a BSD-compat flag. - # Adding the 'sed 1q' prevents false positives on HP-UX, which says: - # nm: unknown option "B" ignored - # Tru64's nm complains that /dev/null is an invalid object file - # MSYS converts /dev/null to NUL, MinGW nm treats NUL as empty - case $build_os in - mingw*) lt_bad_file=conftest.nm/nofile ;; - *) lt_bad_file=/dev/null ;; - esac - case `"$tmp_nm" -B $lt_bad_file 2>&1 | sed '1q'` in - *$lt_bad_file* | *'Invalid file or object type'*) - lt_cv_path_NM="$tmp_nm -B" - break 2 - ;; - *) - case `"$tmp_nm" -p /dev/null 2>&1 | sed '1q'` in - */dev/null*) - lt_cv_path_NM="$tmp_nm -p" - break 2 - ;; - *) - lt_cv_path_NM=${lt_cv_path_NM="$tmp_nm"} # keep the first match, but - continue # so that we can try to find one that supports BSD flags - ;; - esac - ;; - esac - fi - done - IFS=$lt_save_ifs - done - : ${lt_cv_path_NM=no} -fi -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $lt_cv_path_NM" >&5 -$as_echo "$lt_cv_path_NM" >&6; } -if test no != "$lt_cv_path_NM"; then - NM=$lt_cv_path_NM -else - # Didn't find any BSD compatible name lister, look for dumpbin. - if test -n "$DUMPBIN"; then : - # Let the user override the test. - else - if test -n "$ac_tool_prefix"; then - for ac_prog in dumpbin "link -dump" - do - # Extract the first word of "$ac_tool_prefix$ac_prog", so it can be a program name with args. -set dummy $ac_tool_prefix$ac_prog; ac_word=$2 -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5 -$as_echo_n "checking for $ac_word... " >&6; } -if ${ac_cv_prog_DUMPBIN+:} false; then : - $as_echo_n "(cached) " >&6 -else - if test -n "$DUMPBIN"; then - ac_cv_prog_DUMPBIN="$DUMPBIN" # Let the user override the test. -else -as_save_IFS=$IFS; IFS=$PATH_SEPARATOR -for as_dir in $PATH -do - IFS=$as_save_IFS - test -z "$as_dir" && as_dir=. - for ac_exec_ext in '' $ac_executable_extensions; do - if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then - ac_cv_prog_DUMPBIN="$ac_tool_prefix$ac_prog" - $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5 - break 2 - fi -done - done -IFS=$as_save_IFS - -fi -fi -DUMPBIN=$ac_cv_prog_DUMPBIN -if test -n "$DUMPBIN"; then - { $as_echo "$as_me:${as_lineno-$LINENO}: result: $DUMPBIN" >&5 -$as_echo "$DUMPBIN" >&6; } -else - { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 -$as_echo "no" >&6; } -fi - - - test -n "$DUMPBIN" && break - done -fi -if test -z "$DUMPBIN"; then - ac_ct_DUMPBIN=$DUMPBIN - for ac_prog in dumpbin "link -dump" -do - # Extract the first word of "$ac_prog", so it can be a program name with args. -set dummy $ac_prog; ac_word=$2 -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5 -$as_echo_n "checking for $ac_word... " >&6; } -if ${ac_cv_prog_ac_ct_DUMPBIN+:} false; then : - $as_echo_n "(cached) " >&6 -else - if test -n "$ac_ct_DUMPBIN"; then - ac_cv_prog_ac_ct_DUMPBIN="$ac_ct_DUMPBIN" # Let the user override the test. -else -as_save_IFS=$IFS; IFS=$PATH_SEPARATOR -for as_dir in $PATH -do - IFS=$as_save_IFS - test -z "$as_dir" && as_dir=. - for ac_exec_ext in '' $ac_executable_extensions; do - if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then - ac_cv_prog_ac_ct_DUMPBIN="$ac_prog" - $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5 - break 2 - fi -done - done -IFS=$as_save_IFS - -fi -fi -ac_ct_DUMPBIN=$ac_cv_prog_ac_ct_DUMPBIN -if test -n "$ac_ct_DUMPBIN"; then - { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_ct_DUMPBIN" >&5 -$as_echo "$ac_ct_DUMPBIN" >&6; } -else - { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 -$as_echo "no" >&6; } -fi - - - test -n "$ac_ct_DUMPBIN" && break -done - - if test "x$ac_ct_DUMPBIN" = x; then - DUMPBIN=":" - else - case $cross_compiling:$ac_tool_warned in -yes:) -{ $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: using cross tools not prefixed with host triplet" >&5 -$as_echo "$as_me: WARNING: using cross tools not prefixed with host triplet" >&2;} -ac_tool_warned=yes ;; -esac - DUMPBIN=$ac_ct_DUMPBIN - fi -fi - - case `$DUMPBIN -symbols -headers /dev/null 2>&1 | sed '1q'` in - *COFF*) - DUMPBIN="$DUMPBIN -symbols -headers" - ;; - *) - DUMPBIN=: - ;; - esac - fi - - if test : != "$DUMPBIN"; then - NM=$DUMPBIN - fi -fi -test -z "$NM" && NM=nm - - - - - - -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking the name lister ($NM) interface" >&5 -$as_echo_n "checking the name lister ($NM) interface... " >&6; } -if ${lt_cv_nm_interface+:} false; then : - $as_echo_n "(cached) " >&6 -else - lt_cv_nm_interface="BSD nm" - echo "int some_variable = 0;" > conftest.$ac_ext - (eval echo "\"\$as_me:$LINENO: $ac_compile\"" >&5) - (eval "$ac_compile" 2>conftest.err) - cat conftest.err >&5 - (eval echo "\"\$as_me:$LINENO: $NM \\\"conftest.$ac_objext\\\"\"" >&5) - (eval "$NM \"conftest.$ac_objext\"" 2>conftest.err > conftest.out) - cat conftest.err >&5 - (eval echo "\"\$as_me:$LINENO: output\"" >&5) - cat conftest.out >&5 - if $GREP 'External.*some_variable' conftest.out > /dev/null; then - lt_cv_nm_interface="MS dumpbin" - fi - rm -f conftest* -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $lt_cv_nm_interface" >&5 -$as_echo "$lt_cv_nm_interface" >&6; } - -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether ln -s works" >&5 -$as_echo_n "checking whether ln -s works... " >&6; } -LN_S=$as_ln_s -if test "$LN_S" = "ln -s"; then - { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5 -$as_echo "yes" >&6; } -else - { $as_echo "$as_me:${as_lineno-$LINENO}: result: no, using $LN_S" >&5 -$as_echo "no, using $LN_S" >&6; } -fi - -# find the maximum length of command line arguments -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking the maximum length of command line arguments" >&5 -$as_echo_n "checking the maximum length of command line arguments... " >&6; } -if ${lt_cv_sys_max_cmd_len+:} false; then : - $as_echo_n "(cached) " >&6 -else - i=0 - teststring=ABCD - - case $build_os in - msdosdjgpp*) - # On DJGPP, this test can blow up pretty badly due to problems in libc - # (any single argument exceeding 2000 bytes causes a buffer overrun - # during glob expansion). Even if it were fixed, the result of this - # check would be larger than it should be. - lt_cv_sys_max_cmd_len=12288; # 12K is about right - ;; - - gnu*) - # Under GNU Hurd, this test is not required because there is - # no limit to the length of command line arguments. - # Libtool will interpret -1 as no limit whatsoever - lt_cv_sys_max_cmd_len=-1; - ;; - - cygwin* | mingw* | cegcc*) - # On Win9x/ME, this test blows up -- it succeeds, but takes - # about 5 minutes as the teststring grows exponentially. - # Worse, since 9x/ME are not pre-emptively multitasking, - # you end up with a "frozen" computer, even though with patience - # the test eventually succeeds (with a max line length of 256k). - # Instead, let's just punt: use the minimum linelength reported by - # all of the supported platforms: 8192 (on NT/2K/XP). - lt_cv_sys_max_cmd_len=8192; - ;; - - mint*) - # On MiNT this can take a long time and run out of memory. - lt_cv_sys_max_cmd_len=8192; - ;; - - amigaos*) - # On AmigaOS with pdksh, this test takes hours, literally. - # So we just punt and use a minimum line length of 8192. - lt_cv_sys_max_cmd_len=8192; - ;; - - bitrig* | darwin* | dragonfly* | freebsd* | netbsd* | openbsd*) - # This has been around since 386BSD, at least. Likely further. - if test -x /sbin/sysctl; then - lt_cv_sys_max_cmd_len=`/sbin/sysctl -n kern.argmax` - elif test -x /usr/sbin/sysctl; then - lt_cv_sys_max_cmd_len=`/usr/sbin/sysctl -n kern.argmax` - else - lt_cv_sys_max_cmd_len=65536 # usable default for all BSDs - fi - # And add a safety zone - lt_cv_sys_max_cmd_len=`expr $lt_cv_sys_max_cmd_len \/ 4` - lt_cv_sys_max_cmd_len=`expr $lt_cv_sys_max_cmd_len \* 3` - ;; - - interix*) - # We know the value 262144 and hardcode it with a safety zone (like BSD) - lt_cv_sys_max_cmd_len=196608 - ;; - - os2*) - # The test takes a long time on OS/2. - lt_cv_sys_max_cmd_len=8192 - ;; - - osf*) - # Dr. Hans Ekkehard Plesser reports seeing a kernel panic running configure - # due to this test when exec_disable_arg_limit is 1 on Tru64. It is not - # nice to cause kernel panics so lets avoid the loop below. - # First set a reasonable default. - lt_cv_sys_max_cmd_len=16384 - # - if test -x /sbin/sysconfig; then - case `/sbin/sysconfig -q proc exec_disable_arg_limit` in - *1*) lt_cv_sys_max_cmd_len=-1 ;; - esac - fi - ;; - sco3.2v5*) - lt_cv_sys_max_cmd_len=102400 - ;; - sysv5* | sco5v6* | sysv4.2uw2*) - kargmax=`grep ARG_MAX /etc/conf/cf.d/stune 2>/dev/null` - if test -n "$kargmax"; then - lt_cv_sys_max_cmd_len=`echo $kargmax | sed 's/.*[ ]//'` - else - lt_cv_sys_max_cmd_len=32768 - fi - ;; - *) - lt_cv_sys_max_cmd_len=`(getconf ARG_MAX) 2> /dev/null` - if test -n "$lt_cv_sys_max_cmd_len" && \ - test undefined != "$lt_cv_sys_max_cmd_len"; then - lt_cv_sys_max_cmd_len=`expr $lt_cv_sys_max_cmd_len \/ 4` - lt_cv_sys_max_cmd_len=`expr $lt_cv_sys_max_cmd_len \* 3` - else - # Make teststring a little bigger before we do anything with it. - # a 1K string should be a reasonable start. - for i in 1 2 3 4 5 6 7 8; do - teststring=$teststring$teststring - done - SHELL=${SHELL-${CONFIG_SHELL-/bin/sh}} - # If test is not a shell built-in, we'll probably end up computing a - # maximum length that is only half of the actual maximum length, but - # we can't tell. - while { test X`env echo "$teststring$teststring" 2>/dev/null` \ - = "X$teststring$teststring"; } >/dev/null 2>&1 && - test 17 != "$i" # 1/2 MB should be enough - do - i=`expr $i + 1` - teststring=$teststring$teststring - done - # Only check the string length outside the loop. - lt_cv_sys_max_cmd_len=`expr "X$teststring" : ".*" 2>&1` - teststring= - # Add a significant safety factor because C++ compilers can tack on - # massive amounts of additional arguments before passing them to the - # linker. It appears as though 1/2 is a usable value. - lt_cv_sys_max_cmd_len=`expr $lt_cv_sys_max_cmd_len \/ 2` - fi - ;; - esac - -fi - -if test -n "$lt_cv_sys_max_cmd_len"; then - { $as_echo "$as_me:${as_lineno-$LINENO}: result: $lt_cv_sys_max_cmd_len" >&5 -$as_echo "$lt_cv_sys_max_cmd_len" >&6; } -else - { $as_echo "$as_me:${as_lineno-$LINENO}: result: none" >&5 -$as_echo "none" >&6; } -fi -max_cmd_len=$lt_cv_sys_max_cmd_len - - - - - - -: ${CP="cp -f"} -: ${MV="mv -f"} -: ${RM="rm -f"} - -if ( (MAIL=60; unset MAIL) || exit) >/dev/null 2>&1; then - lt_unset=unset -else - lt_unset=false -fi - - - - - -# test EBCDIC or ASCII -case `echo X|tr X '\101'` in - A) # ASCII based system - # \n is not interpreted correctly by Solaris 8 /usr/ucb/tr - lt_SP2NL='tr \040 \012' - lt_NL2SP='tr \015\012 \040\040' - ;; - *) # EBCDIC based system - lt_SP2NL='tr \100 \n' - lt_NL2SP='tr \r\n \100\100' - ;; -esac - - - - - - - - - -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking how to convert $build file names to $host format" >&5 -$as_echo_n "checking how to convert $build file names to $host format... " >&6; } -if ${lt_cv_to_host_file_cmd+:} false; then : - $as_echo_n "(cached) " >&6 -else - case $host in - *-*-mingw* ) - case $build in - *-*-mingw* ) # actually msys - lt_cv_to_host_file_cmd=func_convert_file_msys_to_w32 - ;; - *-*-cygwin* ) - lt_cv_to_host_file_cmd=func_convert_file_cygwin_to_w32 - ;; - * ) # otherwise, assume *nix - lt_cv_to_host_file_cmd=func_convert_file_nix_to_w32 - ;; - esac - ;; - *-*-cygwin* ) - case $build in - *-*-mingw* ) # actually msys - lt_cv_to_host_file_cmd=func_convert_file_msys_to_cygwin - ;; - *-*-cygwin* ) - lt_cv_to_host_file_cmd=func_convert_file_noop - ;; - * ) # otherwise, assume *nix - lt_cv_to_host_file_cmd=func_convert_file_nix_to_cygwin - ;; - esac - ;; - * ) # unhandled hosts (and "normal" native builds) - lt_cv_to_host_file_cmd=func_convert_file_noop - ;; -esac - -fi - -to_host_file_cmd=$lt_cv_to_host_file_cmd -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $lt_cv_to_host_file_cmd" >&5 -$as_echo "$lt_cv_to_host_file_cmd" >&6; } - - - - - -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking how to convert $build file names to toolchain format" >&5 -$as_echo_n "checking how to convert $build file names to toolchain format... " >&6; } -if ${lt_cv_to_tool_file_cmd+:} false; then : - $as_echo_n "(cached) " >&6 -else - #assume ordinary cross tools, or native build. -lt_cv_to_tool_file_cmd=func_convert_file_noop -case $host in - *-*-mingw* ) - case $build in - *-*-mingw* ) # actually msys - lt_cv_to_tool_file_cmd=func_convert_file_msys_to_w32 - ;; - esac - ;; -esac - -fi - -to_tool_file_cmd=$lt_cv_to_tool_file_cmd -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $lt_cv_to_tool_file_cmd" >&5 -$as_echo "$lt_cv_to_tool_file_cmd" >&6; } - - - - - -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $LD option to reload object files" >&5 -$as_echo_n "checking for $LD option to reload object files... " >&6; } -if ${lt_cv_ld_reload_flag+:} false; then : - $as_echo_n "(cached) " >&6 -else - lt_cv_ld_reload_flag='-r' -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $lt_cv_ld_reload_flag" >&5 -$as_echo "$lt_cv_ld_reload_flag" >&6; } -reload_flag=$lt_cv_ld_reload_flag -case $reload_flag in -"" | " "*) ;; -*) reload_flag=" $reload_flag" ;; -esac -reload_cmds='$LD$reload_flag -o $output$reload_objs' -case $host_os in - cygwin* | mingw* | pw32* | cegcc*) - if test yes != "$GCC"; then - reload_cmds=false - fi - ;; - darwin*) - if test yes = "$GCC"; then - reload_cmds='$LTCC $LTCFLAGS -nostdlib $wl-r -o $output$reload_objs' - else - reload_cmds='$LD$reload_flag -o $output$reload_objs' - fi - ;; -esac - - - - - - - - - -if test -n "$ac_tool_prefix"; then - # Extract the first word of "${ac_tool_prefix}objdump", so it can be a program name with args. -set dummy ${ac_tool_prefix}objdump; ac_word=$2 -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5 -$as_echo_n "checking for $ac_word... " >&6; } -if ${ac_cv_prog_OBJDUMP+:} false; then : - $as_echo_n "(cached) " >&6 -else - if test -n "$OBJDUMP"; then - ac_cv_prog_OBJDUMP="$OBJDUMP" # Let the user override the test. -else -as_save_IFS=$IFS; IFS=$PATH_SEPARATOR -for as_dir in $PATH -do - IFS=$as_save_IFS - test -z "$as_dir" && as_dir=. - for ac_exec_ext in '' $ac_executable_extensions; do - if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then - ac_cv_prog_OBJDUMP="${ac_tool_prefix}objdump" - $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5 - break 2 - fi -done - done -IFS=$as_save_IFS - -fi -fi -OBJDUMP=$ac_cv_prog_OBJDUMP -if test -n "$OBJDUMP"; then - { $as_echo "$as_me:${as_lineno-$LINENO}: result: $OBJDUMP" >&5 -$as_echo "$OBJDUMP" >&6; } -else - { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 -$as_echo "no" >&6; } -fi - - -fi -if test -z "$ac_cv_prog_OBJDUMP"; then - ac_ct_OBJDUMP=$OBJDUMP - # Extract the first word of "objdump", so it can be a program name with args. -set dummy objdump; ac_word=$2 -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5 -$as_echo_n "checking for $ac_word... " >&6; } -if ${ac_cv_prog_ac_ct_OBJDUMP+:} false; then : - $as_echo_n "(cached) " >&6 -else - if test -n "$ac_ct_OBJDUMP"; then - ac_cv_prog_ac_ct_OBJDUMP="$ac_ct_OBJDUMP" # Let the user override the test. -else -as_save_IFS=$IFS; IFS=$PATH_SEPARATOR -for as_dir in $PATH -do - IFS=$as_save_IFS - test -z "$as_dir" && as_dir=. - for ac_exec_ext in '' $ac_executable_extensions; do - if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then - ac_cv_prog_ac_ct_OBJDUMP="objdump" - $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5 - break 2 - fi -done - done -IFS=$as_save_IFS - -fi -fi -ac_ct_OBJDUMP=$ac_cv_prog_ac_ct_OBJDUMP -if test -n "$ac_ct_OBJDUMP"; then - { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_ct_OBJDUMP" >&5 -$as_echo "$ac_ct_OBJDUMP" >&6; } -else - { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 -$as_echo "no" >&6; } -fi - - if test "x$ac_ct_OBJDUMP" = x; then - OBJDUMP="false" - else - case $cross_compiling:$ac_tool_warned in -yes:) -{ $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: using cross tools not prefixed with host triplet" >&5 -$as_echo "$as_me: WARNING: using cross tools not prefixed with host triplet" >&2;} -ac_tool_warned=yes ;; -esac - OBJDUMP=$ac_ct_OBJDUMP - fi -else - OBJDUMP="$ac_cv_prog_OBJDUMP" -fi - -test -z "$OBJDUMP" && OBJDUMP=objdump - - - - - - - - - -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking how to recognize dependent libraries" >&5 -$as_echo_n "checking how to recognize dependent libraries... " >&6; } -if ${lt_cv_deplibs_check_method+:} false; then : - $as_echo_n "(cached) " >&6 -else - lt_cv_file_magic_cmd='$MAGIC_CMD' -lt_cv_file_magic_test_file= -lt_cv_deplibs_check_method='unknown' -# Need to set the preceding variable on all platforms that support -# interlibrary dependencies. -# 'none' -- dependencies not supported. -# 'unknown' -- same as none, but documents that we really don't know. -# 'pass_all' -- all dependencies passed with no checks. -# 'test_compile' -- check by making test program. -# 'file_magic [[regex]]' -- check by looking for files in library path -# that responds to the $file_magic_cmd with a given extended regex. -# If you have 'file' or equivalent on your system and you're not sure -# whether 'pass_all' will *always* work, you probably want this one. - -case $host_os in -aix[4-9]*) - lt_cv_deplibs_check_method=pass_all - ;; - -beos*) - lt_cv_deplibs_check_method=pass_all - ;; - -bsdi[45]*) - lt_cv_deplibs_check_method='file_magic ELF [0-9][0-9]*-bit [ML]SB (shared object|dynamic lib)' - lt_cv_file_magic_cmd='/usr/bin/file -L' - lt_cv_file_magic_test_file=/shlib/libc.so - ;; - -cygwin*) - # func_win32_libid is a shell function defined in ltmain.sh - lt_cv_deplibs_check_method='file_magic ^x86 archive import|^x86 DLL' - lt_cv_file_magic_cmd='func_win32_libid' - ;; - -mingw* | pw32*) - # Base MSYS/MinGW do not provide the 'file' command needed by - # func_win32_libid shell function, so use a weaker test based on 'objdump', - # unless we find 'file', for example because we are cross-compiling. - if ( file / ) >/dev/null 2>&1; then - lt_cv_deplibs_check_method='file_magic ^x86 archive import|^x86 DLL' - lt_cv_file_magic_cmd='func_win32_libid' - else - # Keep this pattern in sync with the one in func_win32_libid. - lt_cv_deplibs_check_method='file_magic file format (pei*-i386(.*architecture: i386)?|pe-arm-wince|pe-x86-64)' - lt_cv_file_magic_cmd='$OBJDUMP -f' - fi - ;; - -cegcc*) - # use the weaker test based on 'objdump'. See mingw*. - lt_cv_deplibs_check_method='file_magic file format pe-arm-.*little(.*architecture: arm)?' - lt_cv_file_magic_cmd='$OBJDUMP -f' - ;; - -darwin* | rhapsody*) - lt_cv_deplibs_check_method=pass_all - ;; - -freebsd* | dragonfly*) - if echo __ELF__ | $CC -E - | $GREP __ELF__ > /dev/null; then - case $host_cpu in - i*86 ) - # Not sure whether the presence of OpenBSD here was a mistake. - # Let's accept both of them until this is cleared up. - lt_cv_deplibs_check_method='file_magic (FreeBSD|OpenBSD|DragonFly)/i[3-9]86 (compact )?demand paged shared library' - lt_cv_file_magic_cmd=/usr/bin/file - lt_cv_file_magic_test_file=`echo /usr/lib/libc.so.*` - ;; - esac - else - lt_cv_deplibs_check_method=pass_all - fi - ;; - -haiku*) - lt_cv_deplibs_check_method=pass_all - ;; - -hpux10.20* | hpux11*) - lt_cv_file_magic_cmd=/usr/bin/file - case $host_cpu in - ia64*) - lt_cv_deplibs_check_method='file_magic (s[0-9][0-9][0-9]|ELF-[0-9][0-9]) shared object file - IA64' - lt_cv_file_magic_test_file=/usr/lib/hpux32/libc.so - ;; - hppa*64*) - lt_cv_deplibs_check_method='file_magic (s[0-9][0-9][0-9]|ELF[ -][0-9][0-9])(-bit)?( [LM]SB)? shared object( file)?[, -]* PA-RISC [0-9]\.[0-9]' - lt_cv_file_magic_test_file=/usr/lib/pa20_64/libc.sl - ;; - *) - lt_cv_deplibs_check_method='file_magic (s[0-9][0-9][0-9]|PA-RISC[0-9]\.[0-9]) shared library' - lt_cv_file_magic_test_file=/usr/lib/libc.sl - ;; - esac - ;; - -interix[3-9]*) - # PIC code is broken on Interix 3.x, that's why |\.a not |_pic\.a here - lt_cv_deplibs_check_method='match_pattern /lib[^/]+(\.so|\.a)$' - ;; - -irix5* | irix6* | nonstopux*) - case $LD in - *-32|*"-32 ") libmagic=32-bit;; - *-n32|*"-n32 ") libmagic=N32;; - *-64|*"-64 ") libmagic=64-bit;; - *) libmagic=never-match;; - esac - lt_cv_deplibs_check_method=pass_all - ;; - -# This must be glibc/ELF. -linux* | k*bsd*-gnu | kopensolaris*-gnu | gnu*) - lt_cv_deplibs_check_method=pass_all - ;; - -netbsd*) - if echo __ELF__ | $CC -E - | $GREP __ELF__ > /dev/null; then - lt_cv_deplibs_check_method='match_pattern /lib[^/]+(\.so\.[0-9]+\.[0-9]+|_pic\.a)$' - else - lt_cv_deplibs_check_method='match_pattern /lib[^/]+(\.so|_pic\.a)$' - fi - ;; - -newos6*) - lt_cv_deplibs_check_method='file_magic ELF [0-9][0-9]*-bit [ML]SB (executable|dynamic lib)' - lt_cv_file_magic_cmd=/usr/bin/file - lt_cv_file_magic_test_file=/usr/lib/libnls.so - ;; - -*nto* | *qnx*) - lt_cv_deplibs_check_method=pass_all - ;; - -openbsd* | bitrig*) - if test -z "`echo __ELF__ | $CC -E - | $GREP __ELF__`"; then - lt_cv_deplibs_check_method='match_pattern /lib[^/]+(\.so\.[0-9]+\.[0-9]+|\.so|_pic\.a)$' - else - lt_cv_deplibs_check_method='match_pattern /lib[^/]+(\.so\.[0-9]+\.[0-9]+|_pic\.a)$' - fi - ;; - -osf3* | osf4* | osf5*) - lt_cv_deplibs_check_method=pass_all - ;; - -rdos*) - lt_cv_deplibs_check_method=pass_all - ;; - -solaris*) - lt_cv_deplibs_check_method=pass_all - ;; - -sysv5* | sco3.2v5* | sco5v6* | unixware* | OpenUNIX* | sysv4*uw2*) - lt_cv_deplibs_check_method=pass_all - ;; - -sysv4 | sysv4.3*) - case $host_vendor in - motorola) - lt_cv_deplibs_check_method='file_magic ELF [0-9][0-9]*-bit [ML]SB (shared object|dynamic lib) M[0-9][0-9]* Version [0-9]' - lt_cv_file_magic_test_file=`echo /usr/lib/libc.so*` - ;; - ncr) - lt_cv_deplibs_check_method=pass_all - ;; - sequent) - lt_cv_file_magic_cmd='/bin/file' - lt_cv_deplibs_check_method='file_magic ELF [0-9][0-9]*-bit [LM]SB (shared object|dynamic lib )' - ;; - sni) - lt_cv_file_magic_cmd='/bin/file' - lt_cv_deplibs_check_method="file_magic ELF [0-9][0-9]*-bit [LM]SB dynamic lib" - lt_cv_file_magic_test_file=/lib/libc.so - ;; - siemens) - lt_cv_deplibs_check_method=pass_all - ;; - pc) - lt_cv_deplibs_check_method=pass_all - ;; - esac - ;; - -tpf*) - lt_cv_deplibs_check_method=pass_all - ;; -os2*) - lt_cv_deplibs_check_method=pass_all - ;; -esac - -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $lt_cv_deplibs_check_method" >&5 -$as_echo "$lt_cv_deplibs_check_method" >&6; } - -file_magic_glob= -want_nocaseglob=no -if test "$build" = "$host"; then - case $host_os in - mingw* | pw32*) - if ( shopt | grep nocaseglob ) >/dev/null 2>&1; then - want_nocaseglob=yes - else - file_magic_glob=`echo aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ | $SED -e "s/\(..\)/s\/[\1]\/[\1]\/g;/g"` - fi - ;; - esac -fi - -file_magic_cmd=$lt_cv_file_magic_cmd -deplibs_check_method=$lt_cv_deplibs_check_method -test -z "$deplibs_check_method" && deplibs_check_method=unknown - - - - - - - - - - - - - - - - - - - - - - -if test -n "$ac_tool_prefix"; then - # Extract the first word of "${ac_tool_prefix}dlltool", so it can be a program name with args. -set dummy ${ac_tool_prefix}dlltool; ac_word=$2 -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5 -$as_echo_n "checking for $ac_word... " >&6; } -if ${ac_cv_prog_DLLTOOL+:} false; then : - $as_echo_n "(cached) " >&6 -else - if test -n "$DLLTOOL"; then - ac_cv_prog_DLLTOOL="$DLLTOOL" # Let the user override the test. -else -as_save_IFS=$IFS; IFS=$PATH_SEPARATOR -for as_dir in $PATH -do - IFS=$as_save_IFS - test -z "$as_dir" && as_dir=. - for ac_exec_ext in '' $ac_executable_extensions; do - if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then - ac_cv_prog_DLLTOOL="${ac_tool_prefix}dlltool" - $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5 - break 2 - fi -done - done -IFS=$as_save_IFS - -fi -fi -DLLTOOL=$ac_cv_prog_DLLTOOL -if test -n "$DLLTOOL"; then - { $as_echo "$as_me:${as_lineno-$LINENO}: result: $DLLTOOL" >&5 -$as_echo "$DLLTOOL" >&6; } -else - { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 -$as_echo "no" >&6; } -fi - - -fi -if test -z "$ac_cv_prog_DLLTOOL"; then - ac_ct_DLLTOOL=$DLLTOOL - # Extract the first word of "dlltool", so it can be a program name with args. -set dummy dlltool; ac_word=$2 -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5 -$as_echo_n "checking for $ac_word... " >&6; } -if ${ac_cv_prog_ac_ct_DLLTOOL+:} false; then : - $as_echo_n "(cached) " >&6 -else - if test -n "$ac_ct_DLLTOOL"; then - ac_cv_prog_ac_ct_DLLTOOL="$ac_ct_DLLTOOL" # Let the user override the test. -else -as_save_IFS=$IFS; IFS=$PATH_SEPARATOR -for as_dir in $PATH -do - IFS=$as_save_IFS - test -z "$as_dir" && as_dir=. - for ac_exec_ext in '' $ac_executable_extensions; do - if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then - ac_cv_prog_ac_ct_DLLTOOL="dlltool" - $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5 - break 2 - fi -done - done -IFS=$as_save_IFS - -fi -fi -ac_ct_DLLTOOL=$ac_cv_prog_ac_ct_DLLTOOL -if test -n "$ac_ct_DLLTOOL"; then - { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_ct_DLLTOOL" >&5 -$as_echo "$ac_ct_DLLTOOL" >&6; } -else - { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 -$as_echo "no" >&6; } -fi - - if test "x$ac_ct_DLLTOOL" = x; then - DLLTOOL="false" - else - case $cross_compiling:$ac_tool_warned in -yes:) -{ $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: using cross tools not prefixed with host triplet" >&5 -$as_echo "$as_me: WARNING: using cross tools not prefixed with host triplet" >&2;} -ac_tool_warned=yes ;; -esac - DLLTOOL=$ac_ct_DLLTOOL - fi -else - DLLTOOL="$ac_cv_prog_DLLTOOL" -fi - -test -z "$DLLTOOL" && DLLTOOL=dlltool - - - - - - - - - - -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking how to associate runtime and link libraries" >&5 -$as_echo_n "checking how to associate runtime and link libraries... " >&6; } -if ${lt_cv_sharedlib_from_linklib_cmd+:} false; then : - $as_echo_n "(cached) " >&6 -else - lt_cv_sharedlib_from_linklib_cmd='unknown' - -case $host_os in -cygwin* | mingw* | pw32* | cegcc*) - # two different shell functions defined in ltmain.sh; - # decide which one to use based on capabilities of $DLLTOOL - case `$DLLTOOL --help 2>&1` in - *--identify-strict*) - lt_cv_sharedlib_from_linklib_cmd=func_cygming_dll_for_implib - ;; - *) - lt_cv_sharedlib_from_linklib_cmd=func_cygming_dll_for_implib_fallback - ;; - esac - ;; -*) - # fallback: assume linklib IS sharedlib - lt_cv_sharedlib_from_linklib_cmd=$ECHO - ;; -esac - -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $lt_cv_sharedlib_from_linklib_cmd" >&5 -$as_echo "$lt_cv_sharedlib_from_linklib_cmd" >&6; } -sharedlib_from_linklib_cmd=$lt_cv_sharedlib_from_linklib_cmd -test -z "$sharedlib_from_linklib_cmd" && sharedlib_from_linklib_cmd=$ECHO - - - - - - - - -if test -n "$ac_tool_prefix"; then - for ac_prog in ar - do - # Extract the first word of "$ac_tool_prefix$ac_prog", so it can be a program name with args. -set dummy $ac_tool_prefix$ac_prog; ac_word=$2 -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5 -$as_echo_n "checking for $ac_word... " >&6; } -if ${ac_cv_prog_AR+:} false; then : - $as_echo_n "(cached) " >&6 -else - if test -n "$AR"; then - ac_cv_prog_AR="$AR" # Let the user override the test. -else -as_save_IFS=$IFS; IFS=$PATH_SEPARATOR -for as_dir in $PATH -do - IFS=$as_save_IFS - test -z "$as_dir" && as_dir=. - for ac_exec_ext in '' $ac_executable_extensions; do - if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then - ac_cv_prog_AR="$ac_tool_prefix$ac_prog" - $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5 - break 2 - fi -done - done -IFS=$as_save_IFS - -fi -fi -AR=$ac_cv_prog_AR -if test -n "$AR"; then - { $as_echo "$as_me:${as_lineno-$LINENO}: result: $AR" >&5 -$as_echo "$AR" >&6; } -else - { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 -$as_echo "no" >&6; } -fi - - - test -n "$AR" && break - done -fi -if test -z "$AR"; then - ac_ct_AR=$AR - for ac_prog in ar -do - # Extract the first word of "$ac_prog", so it can be a program name with args. -set dummy $ac_prog; ac_word=$2 -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5 -$as_echo_n "checking for $ac_word... " >&6; } -if ${ac_cv_prog_ac_ct_AR+:} false; then : - $as_echo_n "(cached) " >&6 -else - if test -n "$ac_ct_AR"; then - ac_cv_prog_ac_ct_AR="$ac_ct_AR" # Let the user override the test. -else -as_save_IFS=$IFS; IFS=$PATH_SEPARATOR -for as_dir in $PATH -do - IFS=$as_save_IFS - test -z "$as_dir" && as_dir=. - for ac_exec_ext in '' $ac_executable_extensions; do - if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then - ac_cv_prog_ac_ct_AR="$ac_prog" - $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5 - break 2 - fi -done - done -IFS=$as_save_IFS - -fi -fi -ac_ct_AR=$ac_cv_prog_ac_ct_AR -if test -n "$ac_ct_AR"; then - { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_ct_AR" >&5 -$as_echo "$ac_ct_AR" >&6; } -else - { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 -$as_echo "no" >&6; } -fi - - - test -n "$ac_ct_AR" && break -done - - if test "x$ac_ct_AR" = x; then - AR="false" - else - case $cross_compiling:$ac_tool_warned in -yes:) -{ $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: using cross tools not prefixed with host triplet" >&5 -$as_echo "$as_me: WARNING: using cross tools not prefixed with host triplet" >&2;} -ac_tool_warned=yes ;; -esac - AR=$ac_ct_AR - fi -fi - -: ${AR=ar} -: ${AR_FLAGS=cru} - - - - - - - - - - - -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for archiver @FILE support" >&5 -$as_echo_n "checking for archiver @FILE support... " >&6; } -if ${lt_cv_ar_at_file+:} false; then : - $as_echo_n "(cached) " >&6 -else - lt_cv_ar_at_file=no - cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ - -int -main () -{ - - ; - return 0; -} -_ACEOF -if ac_fn_c_try_compile "$LINENO"; then : - echo conftest.$ac_objext > conftest.lst - lt_ar_try='$AR $AR_FLAGS libconftest.a @conftest.lst >&5' - { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$lt_ar_try\""; } >&5 - (eval $lt_ar_try) 2>&5 - ac_status=$? - $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5 - test $ac_status = 0; } - if test 0 -eq "$ac_status"; then - # Ensure the archiver fails upon bogus file names. - rm -f conftest.$ac_objext libconftest.a - { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$lt_ar_try\""; } >&5 - (eval $lt_ar_try) 2>&5 - ac_status=$? - $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5 - test $ac_status = 0; } - if test 0 -ne "$ac_status"; then - lt_cv_ar_at_file=@ - fi - fi - rm -f conftest.* libconftest.a - -fi -rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext - -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $lt_cv_ar_at_file" >&5 -$as_echo "$lt_cv_ar_at_file" >&6; } - -if test no = "$lt_cv_ar_at_file"; then - archiver_list_spec= -else - archiver_list_spec=$lt_cv_ar_at_file -fi - - - - - - - -if test -n "$ac_tool_prefix"; then - # Extract the first word of "${ac_tool_prefix}strip", so it can be a program name with args. -set dummy ${ac_tool_prefix}strip; ac_word=$2 -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5 -$as_echo_n "checking for $ac_word... " >&6; } -if ${ac_cv_prog_STRIP+:} false; then : - $as_echo_n "(cached) " >&6 -else - if test -n "$STRIP"; then - ac_cv_prog_STRIP="$STRIP" # Let the user override the test. -else -as_save_IFS=$IFS; IFS=$PATH_SEPARATOR -for as_dir in $PATH -do - IFS=$as_save_IFS - test -z "$as_dir" && as_dir=. - for ac_exec_ext in '' $ac_executable_extensions; do - if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then - ac_cv_prog_STRIP="${ac_tool_prefix}strip" - $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5 - break 2 - fi -done - done -IFS=$as_save_IFS - -fi -fi -STRIP=$ac_cv_prog_STRIP -if test -n "$STRIP"; then - { $as_echo "$as_me:${as_lineno-$LINENO}: result: $STRIP" >&5 -$as_echo "$STRIP" >&6; } -else - { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 -$as_echo "no" >&6; } -fi - - -fi -if test -z "$ac_cv_prog_STRIP"; then - ac_ct_STRIP=$STRIP - # Extract the first word of "strip", so it can be a program name with args. -set dummy strip; ac_word=$2 -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5 -$as_echo_n "checking for $ac_word... " >&6; } -if ${ac_cv_prog_ac_ct_STRIP+:} false; then : - $as_echo_n "(cached) " >&6 -else - if test -n "$ac_ct_STRIP"; then - ac_cv_prog_ac_ct_STRIP="$ac_ct_STRIP" # Let the user override the test. -else -as_save_IFS=$IFS; IFS=$PATH_SEPARATOR -for as_dir in $PATH -do - IFS=$as_save_IFS - test -z "$as_dir" && as_dir=. - for ac_exec_ext in '' $ac_executable_extensions; do - if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then - ac_cv_prog_ac_ct_STRIP="strip" - $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5 - break 2 - fi -done - done -IFS=$as_save_IFS - -fi -fi -ac_ct_STRIP=$ac_cv_prog_ac_ct_STRIP -if test -n "$ac_ct_STRIP"; then - { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_ct_STRIP" >&5 -$as_echo "$ac_ct_STRIP" >&6; } -else - { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 -$as_echo "no" >&6; } -fi - - if test "x$ac_ct_STRIP" = x; then - STRIP=":" - else - case $cross_compiling:$ac_tool_warned in -yes:) -{ $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: using cross tools not prefixed with host triplet" >&5 -$as_echo "$as_me: WARNING: using cross tools not prefixed with host triplet" >&2;} -ac_tool_warned=yes ;; -esac - STRIP=$ac_ct_STRIP - fi -else - STRIP="$ac_cv_prog_STRIP" -fi - -test -z "$STRIP" && STRIP=: - - - - - - -if test -n "$ac_tool_prefix"; then - # Extract the first word of "${ac_tool_prefix}ranlib", so it can be a program name with args. -set dummy ${ac_tool_prefix}ranlib; ac_word=$2 -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5 -$as_echo_n "checking for $ac_word... " >&6; } -if ${ac_cv_prog_RANLIB+:} false; then : - $as_echo_n "(cached) " >&6 -else - if test -n "$RANLIB"; then - ac_cv_prog_RANLIB="$RANLIB" # Let the user override the test. -else -as_save_IFS=$IFS; IFS=$PATH_SEPARATOR -for as_dir in $PATH -do - IFS=$as_save_IFS - test -z "$as_dir" && as_dir=. - for ac_exec_ext in '' $ac_executable_extensions; do - if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then - ac_cv_prog_RANLIB="${ac_tool_prefix}ranlib" - $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5 - break 2 - fi -done - done -IFS=$as_save_IFS - -fi -fi -RANLIB=$ac_cv_prog_RANLIB -if test -n "$RANLIB"; then - { $as_echo "$as_me:${as_lineno-$LINENO}: result: $RANLIB" >&5 -$as_echo "$RANLIB" >&6; } -else - { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 -$as_echo "no" >&6; } -fi - - -fi -if test -z "$ac_cv_prog_RANLIB"; then - ac_ct_RANLIB=$RANLIB - # Extract the first word of "ranlib", so it can be a program name with args. -set dummy ranlib; ac_word=$2 -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5 -$as_echo_n "checking for $ac_word... " >&6; } -if ${ac_cv_prog_ac_ct_RANLIB+:} false; then : - $as_echo_n "(cached) " >&6 -else - if test -n "$ac_ct_RANLIB"; then - ac_cv_prog_ac_ct_RANLIB="$ac_ct_RANLIB" # Let the user override the test. -else -as_save_IFS=$IFS; IFS=$PATH_SEPARATOR -for as_dir in $PATH -do - IFS=$as_save_IFS - test -z "$as_dir" && as_dir=. - for ac_exec_ext in '' $ac_executable_extensions; do - if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then - ac_cv_prog_ac_ct_RANLIB="ranlib" - $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5 - break 2 - fi -done - done -IFS=$as_save_IFS - -fi -fi -ac_ct_RANLIB=$ac_cv_prog_ac_ct_RANLIB -if test -n "$ac_ct_RANLIB"; then - { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_ct_RANLIB" >&5 -$as_echo "$ac_ct_RANLIB" >&6; } -else - { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 -$as_echo "no" >&6; } -fi - - if test "x$ac_ct_RANLIB" = x; then - RANLIB=":" - else - case $cross_compiling:$ac_tool_warned in -yes:) -{ $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: using cross tools not prefixed with host triplet" >&5 -$as_echo "$as_me: WARNING: using cross tools not prefixed with host triplet" >&2;} -ac_tool_warned=yes ;; -esac - RANLIB=$ac_ct_RANLIB - fi -else - RANLIB="$ac_cv_prog_RANLIB" -fi - -test -z "$RANLIB" && RANLIB=: - - - - - - -# Determine commands to create old-style static archives. -old_archive_cmds='$AR $AR_FLAGS $oldlib$oldobjs' -old_postinstall_cmds='chmod 644 $oldlib' -old_postuninstall_cmds= - -if test -n "$RANLIB"; then - case $host_os in - bitrig* | openbsd*) - old_postinstall_cmds="$old_postinstall_cmds~\$RANLIB -t \$tool_oldlib" - ;; - *) - old_postinstall_cmds="$old_postinstall_cmds~\$RANLIB \$tool_oldlib" - ;; - esac - old_archive_cmds="$old_archive_cmds~\$RANLIB \$tool_oldlib" -fi - -case $host_os in - darwin*) - lock_old_archive_extraction=yes ;; - *) - lock_old_archive_extraction=no ;; -esac - - - - - - - - - - - - - - - - - - - - - -for ac_prog in gawk mawk nawk awk -do - # Extract the first word of "$ac_prog", so it can be a program name with args. -set dummy $ac_prog; ac_word=$2 -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5 -$as_echo_n "checking for $ac_word... " >&6; } -if ${ac_cv_prog_AWK+:} false; then : - $as_echo_n "(cached) " >&6 -else - if test -n "$AWK"; then - ac_cv_prog_AWK="$AWK" # Let the user override the test. -else -as_save_IFS=$IFS; IFS=$PATH_SEPARATOR -for as_dir in $PATH -do - IFS=$as_save_IFS - test -z "$as_dir" && as_dir=. - for ac_exec_ext in '' $ac_executable_extensions; do - if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then - ac_cv_prog_AWK="$ac_prog" - $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5 - break 2 - fi -done - done -IFS=$as_save_IFS - -fi -fi -AWK=$ac_cv_prog_AWK -if test -n "$AWK"; then - { $as_echo "$as_me:${as_lineno-$LINENO}: result: $AWK" >&5 -$as_echo "$AWK" >&6; } -else - { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 -$as_echo "no" >&6; } -fi - - - test -n "$AWK" && break -done - - - - - - - - - - - - - - - - - - - -# If no C compiler was specified, use CC. -LTCC=${LTCC-"$CC"} - -# If no C compiler flags were specified, use CFLAGS. -LTCFLAGS=${LTCFLAGS-"$CFLAGS"} - -# Allow CC to be a program name with arguments. -compiler=$CC - - -# Check for command to grab the raw symbol name followed by C symbol from nm. -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking command to parse $NM output from $compiler object" >&5 -$as_echo_n "checking command to parse $NM output from $compiler object... " >&6; } -if ${lt_cv_sys_global_symbol_pipe+:} false; then : - $as_echo_n "(cached) " >&6 -else - -# These are sane defaults that work on at least a few old systems. -# [They come from Ultrix. What could be older than Ultrix?!! ;)] - -# Character class describing NM global symbol codes. -symcode='[BCDEGRST]' - -# Regexp to match symbols that can be accessed directly from C. -sympat='\([_A-Za-z][_A-Za-z0-9]*\)' - -# Define system-specific variables. -case $host_os in -aix*) - symcode='[BCDT]' - ;; -cygwin* | mingw* | pw32* | cegcc*) - symcode='[ABCDGISTW]' - ;; -hpux*) - if test ia64 = "$host_cpu"; then - symcode='[ABCDEGRST]' - fi - ;; -irix* | nonstopux*) - symcode='[BCDEGRST]' - ;; -osf*) - symcode='[BCDEGQRST]' - ;; -solaris*) - symcode='[BDRT]' - ;; -sco3.2v5*) - symcode='[DT]' - ;; -sysv4.2uw2*) - symcode='[DT]' - ;; -sysv5* | sco5v6* | unixware* | OpenUNIX*) - symcode='[ABDT]' - ;; -sysv4) - symcode='[DFNSTU]' - ;; -esac - -# If we're using GNU nm, then use its standard symbol codes. -case `$NM -V 2>&1` in -*GNU* | *'with BFD'*) - symcode='[ABCDGIRSTW]' ;; -esac - -if test "$lt_cv_nm_interface" = "MS dumpbin"; then - # Gets list of data symbols to import. - lt_cv_sys_global_symbol_to_import="sed -n -e 's/^I .* \(.*\)$/\1/p'" - # Adjust the below global symbol transforms to fixup imported variables. - lt_cdecl_hook=" -e 's/^I .* \(.*\)$/extern __declspec(dllimport) char \1;/p'" - lt_c_name_hook=" -e 's/^I .* \(.*\)$/ {\"\1\", (void *) 0},/p'" - lt_c_name_lib_hook="\ - -e 's/^I .* \(lib.*\)$/ {\"\1\", (void *) 0},/p'\ - -e 's/^I .* \(.*\)$/ {\"lib\1\", (void *) 0},/p'" -else - # Disable hooks by default. - lt_cv_sys_global_symbol_to_import= - lt_cdecl_hook= - lt_c_name_hook= - lt_c_name_lib_hook= -fi - -# Transform an extracted symbol line into a proper C declaration. -# Some systems (esp. on ia64) link data and code symbols differently, -# so use this general approach. -lt_cv_sys_global_symbol_to_cdecl="sed -n"\ -$lt_cdecl_hook\ -" -e 's/^T .* \(.*\)$/extern int \1();/p'"\ -" -e 's/^$symcode$symcode* .* \(.*\)$/extern char \1;/p'" - -# Transform an extracted symbol line into symbol name and symbol address -lt_cv_sys_global_symbol_to_c_name_address="sed -n"\ -$lt_c_name_hook\ -" -e 's/^: \(.*\) .*$/ {\"\1\", (void *) 0},/p'"\ -" -e 's/^$symcode$symcode* .* \(.*\)$/ {\"\1\", (void *) \&\1},/p'" - -# Transform an extracted symbol line into symbol name with lib prefix and -# symbol address. -lt_cv_sys_global_symbol_to_c_name_address_lib_prefix="sed -n"\ -$lt_c_name_lib_hook\ -" -e 's/^: \(.*\) .*$/ {\"\1\", (void *) 0},/p'"\ -" -e 's/^$symcode$symcode* .* \(lib.*\)$/ {\"\1\", (void *) \&\1},/p'"\ -" -e 's/^$symcode$symcode* .* \(.*\)$/ {\"lib\1\", (void *) \&\1},/p'" - -# Handle CRLF in mingw tool chain -opt_cr= -case $build_os in -mingw*) - opt_cr=`$ECHO 'x\{0,1\}' | tr x '\015'` # option cr in regexp - ;; -esac - -# Try without a prefix underscore, then with it. -for ac_symprfx in "" "_"; do - - # Transform symcode, sympat, and symprfx into a raw symbol and a C symbol. - symxfrm="\\1 $ac_symprfx\\2 \\2" - - # Write the raw and C identifiers. - if test "$lt_cv_nm_interface" = "MS dumpbin"; then - # Fake it for dumpbin and say T for any non-static function, - # D for any global variable and I for any imported variable. - # Also find C++ and __fastcall symbols from MSVC++, - # which start with @ or ?. - lt_cv_sys_global_symbol_pipe="$AWK '"\ -" {last_section=section; section=\$ 3};"\ -" /^COFF SYMBOL TABLE/{for(i in hide) delete hide[i]};"\ -" /Section length .*#relocs.*(pick any)/{hide[last_section]=1};"\ -" /^ *Symbol name *: /{split(\$ 0,sn,\":\"); si=substr(sn[2],2)};"\ -" /^ *Type *: code/{print \"T\",si,substr(si,length(prfx))};"\ -" /^ *Type *: data/{print \"I\",si,substr(si,length(prfx))};"\ -" \$ 0!~/External *\|/{next};"\ -" / 0+ UNDEF /{next}; / UNDEF \([^|]\)*()/{next};"\ -" {if(hide[section]) next};"\ -" {f=\"D\"}; \$ 0~/\(\).*\|/{f=\"T\"};"\ -" {split(\$ 0,a,/\||\r/); split(a[2],s)};"\ -" s[1]~/^[@?]/{print f,s[1],s[1]; next};"\ -" s[1]~prfx {split(s[1],t,\"@\"); print f,t[1],substr(t[1],length(prfx))}"\ -" ' prfx=^$ac_symprfx" - else - lt_cv_sys_global_symbol_pipe="sed -n -e 's/^.*[ ]\($symcode$symcode*\)[ ][ ]*$ac_symprfx$sympat$opt_cr$/$symxfrm/p'" - fi - lt_cv_sys_global_symbol_pipe="$lt_cv_sys_global_symbol_pipe | sed '/ __gnu_lto/d'" - - # Check to see that the pipe works correctly. - pipe_works=no - - rm -f conftest* - cat > conftest.$ac_ext <<_LT_EOF -#ifdef __cplusplus -extern "C" { -#endif -char nm_test_var; -void nm_test_func(void); -void nm_test_func(void){} -#ifdef __cplusplus -} -#endif -int main(){nm_test_var='a';nm_test_func();return(0);} -_LT_EOF - - if { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_compile\""; } >&5 - (eval $ac_compile) 2>&5 - ac_status=$? - $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5 - test $ac_status = 0; }; then - # Now try to grab the symbols. - nlist=conftest.nm - if { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$NM conftest.$ac_objext \| "$lt_cv_sys_global_symbol_pipe" \> $nlist\""; } >&5 - (eval $NM conftest.$ac_objext \| "$lt_cv_sys_global_symbol_pipe" \> $nlist) 2>&5 - ac_status=$? - $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5 - test $ac_status = 0; } && test -s "$nlist"; then - # Try sorting and uniquifying the output. - if sort "$nlist" | uniq > "$nlist"T; then - mv -f "$nlist"T "$nlist" - else - rm -f "$nlist"T - fi - - # Make sure that we snagged all the symbols we need. - if $GREP ' nm_test_var$' "$nlist" >/dev/null; then - if $GREP ' nm_test_func$' "$nlist" >/dev/null; then - cat <<_LT_EOF > conftest.$ac_ext -/* Keep this code in sync between libtool.m4, ltmain, lt_system.h, and tests. */ -#if defined _WIN32 || defined __CYGWIN__ || defined _WIN32_WCE -/* DATA imports from DLLs on WIN32 can't be const, because runtime - relocations are performed -- see ld's documentation on pseudo-relocs. */ -# define LT_DLSYM_CONST -#elif defined __osf__ -/* This system does not cope well with relocations in const data. */ -# define LT_DLSYM_CONST -#else -# define LT_DLSYM_CONST const -#endif - -#ifdef __cplusplus -extern "C" { -#endif - -_LT_EOF - # Now generate the symbol file. - eval "$lt_cv_sys_global_symbol_to_cdecl"' < "$nlist" | $GREP -v main >> conftest.$ac_ext' - - cat <<_LT_EOF >> conftest.$ac_ext - -/* The mapping between symbol names and symbols. */ -LT_DLSYM_CONST struct { - const char *name; - void *address; -} -lt__PROGRAM__LTX_preloaded_symbols[] = -{ - { "@PROGRAM@", (void *) 0 }, -_LT_EOF - $SED "s/^$symcode$symcode* .* \(.*\)$/ {\"\1\", (void *) \&\1},/" < "$nlist" | $GREP -v main >> conftest.$ac_ext - cat <<\_LT_EOF >> conftest.$ac_ext - {0, (void *) 0} -}; - -/* This works around a problem in FreeBSD linker */ -#ifdef FREEBSD_WORKAROUND -static const void *lt_preloaded_setup() { - return lt__PROGRAM__LTX_preloaded_symbols; -} -#endif - -#ifdef __cplusplus -} -#endif -_LT_EOF - # Now try linking the two files. - mv conftest.$ac_objext conftstm.$ac_objext - lt_globsym_save_LIBS=$LIBS - lt_globsym_save_CFLAGS=$CFLAGS - LIBS=conftstm.$ac_objext - CFLAGS="$CFLAGS$lt_prog_compiler_no_builtin_flag" - if { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_link\""; } >&5 - (eval $ac_link) 2>&5 - ac_status=$? - $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5 - test $ac_status = 0; } && test -s conftest$ac_exeext; then - pipe_works=yes - fi - LIBS=$lt_globsym_save_LIBS - CFLAGS=$lt_globsym_save_CFLAGS - else - echo "cannot find nm_test_func in $nlist" >&5 - fi - else - echo "cannot find nm_test_var in $nlist" >&5 - fi - else - echo "cannot run $lt_cv_sys_global_symbol_pipe" >&5 - fi - else - echo "$progname: failed program was:" >&5 - cat conftest.$ac_ext >&5 - fi - rm -rf conftest* conftst* - - # Do not use the global_symbol_pipe unless it works. - if test yes = "$pipe_works"; then - break - else - lt_cv_sys_global_symbol_pipe= - fi -done - -fi - -if test -z "$lt_cv_sys_global_symbol_pipe"; then - lt_cv_sys_global_symbol_to_cdecl= -fi -if test -z "$lt_cv_sys_global_symbol_pipe$lt_cv_sys_global_symbol_to_cdecl"; then - { $as_echo "$as_me:${as_lineno-$LINENO}: result: failed" >&5 -$as_echo "failed" >&6; } -else - { $as_echo "$as_me:${as_lineno-$LINENO}: result: ok" >&5 -$as_echo "ok" >&6; } -fi - -# Response file support. -if test "$lt_cv_nm_interface" = "MS dumpbin"; then - nm_file_list_spec='@' -elif $NM --help 2>/dev/null | grep '[@]FILE' >/dev/null; then - nm_file_list_spec='@' -fi - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for sysroot" >&5 -$as_echo_n "checking for sysroot... " >&6; } - -# Check whether --with-sysroot was given. -if test "${with_sysroot+set}" = set; then : - withval=$with_sysroot; -else - with_sysroot=no -fi - - -lt_sysroot= -case $with_sysroot in #( - yes) - if test yes = "$GCC"; then - lt_sysroot=`$CC --print-sysroot 2>/dev/null` - fi - ;; #( - /*) - lt_sysroot=`echo "$with_sysroot" | sed -e "$sed_quote_subst"` - ;; #( - no|'') - ;; #( - *) - { $as_echo "$as_me:${as_lineno-$LINENO}: result: $with_sysroot" >&5 -$as_echo "$with_sysroot" >&6; } - as_fn_error $? "The sysroot must be an absolute path." "$LINENO" 5 - ;; -esac - - { $as_echo "$as_me:${as_lineno-$LINENO}: result: ${lt_sysroot:-no}" >&5 -$as_echo "${lt_sysroot:-no}" >&6; } - - - - - -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for a working dd" >&5 -$as_echo_n "checking for a working dd... " >&6; } -if ${ac_cv_path_lt_DD+:} false; then : - $as_echo_n "(cached) " >&6 -else - printf 0123456789abcdef0123456789abcdef >conftest.i -cat conftest.i conftest.i >conftest2.i -: ${lt_DD:=$DD} -if test -z "$lt_DD"; then - ac_path_lt_DD_found=false - # Loop through the user's path and test for each of PROGNAME-LIST - as_save_IFS=$IFS; IFS=$PATH_SEPARATOR -for as_dir in $PATH -do - IFS=$as_save_IFS - test -z "$as_dir" && as_dir=. - for ac_prog in dd; do - for ac_exec_ext in '' $ac_executable_extensions; do - ac_path_lt_DD="$as_dir/$ac_prog$ac_exec_ext" - as_fn_executable_p "$ac_path_lt_DD" || continue -if "$ac_path_lt_DD" bs=32 count=1 conftest.out 2>/dev/null; then - cmp -s conftest.i conftest.out \ - && ac_cv_path_lt_DD="$ac_path_lt_DD" ac_path_lt_DD_found=: -fi - $ac_path_lt_DD_found && break 3 - done - done - done -IFS=$as_save_IFS - if test -z "$ac_cv_path_lt_DD"; then - : - fi -else - ac_cv_path_lt_DD=$lt_DD -fi - -rm -f conftest.i conftest2.i conftest.out -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_path_lt_DD" >&5 -$as_echo "$ac_cv_path_lt_DD" >&6; } - - -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking how to truncate binary pipes" >&5 -$as_echo_n "checking how to truncate binary pipes... " >&6; } -if ${lt_cv_truncate_bin+:} false; then : - $as_echo_n "(cached) " >&6 -else - printf 0123456789abcdef0123456789abcdef >conftest.i -cat conftest.i conftest.i >conftest2.i -lt_cv_truncate_bin= -if "$ac_cv_path_lt_DD" bs=32 count=1 conftest.out 2>/dev/null; then - cmp -s conftest.i conftest.out \ - && lt_cv_truncate_bin="$ac_cv_path_lt_DD bs=4096 count=1" -fi -rm -f conftest.i conftest2.i conftest.out -test -z "$lt_cv_truncate_bin" && lt_cv_truncate_bin="$SED -e 4q" -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $lt_cv_truncate_bin" >&5 -$as_echo "$lt_cv_truncate_bin" >&6; } - - - - - - - -# Calculate cc_basename. Skip known compiler wrappers and cross-prefix. -func_cc_basename () -{ - for cc_temp in $*""; do - case $cc_temp in - compile | *[\\/]compile | ccache | *[\\/]ccache ) ;; - distcc | *[\\/]distcc | purify | *[\\/]purify ) ;; - \-*) ;; - *) break;; - esac - done - func_cc_basename_result=`$ECHO "$cc_temp" | $SED "s%.*/%%; s%^$host_alias-%%"` -} - -# Check whether --enable-libtool-lock was given. -if test "${enable_libtool_lock+set}" = set; then : - enableval=$enable_libtool_lock; -fi - -test no = "$enable_libtool_lock" || enable_libtool_lock=yes - -# Some flags need to be propagated to the compiler or linker for good -# libtool support. -case $host in -ia64-*-hpux*) - # Find out what ABI is being produced by ac_compile, and set mode - # options accordingly. - echo 'int i;' > conftest.$ac_ext - if { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_compile\""; } >&5 - (eval $ac_compile) 2>&5 - ac_status=$? - $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5 - test $ac_status = 0; }; then - case `/usr/bin/file conftest.$ac_objext` in - *ELF-32*) - HPUX_IA64_MODE=32 - ;; - *ELF-64*) - HPUX_IA64_MODE=64 - ;; - esac - fi - rm -rf conftest* - ;; -*-*-irix6*) - # Find out what ABI is being produced by ac_compile, and set linker - # options accordingly. - echo '#line '$LINENO' "configure"' > conftest.$ac_ext - if { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_compile\""; } >&5 - (eval $ac_compile) 2>&5 - ac_status=$? - $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5 - test $ac_status = 0; }; then - if test yes = "$lt_cv_prog_gnu_ld"; then - case `/usr/bin/file conftest.$ac_objext` in - *32-bit*) - LD="${LD-ld} -melf32bsmip" - ;; - *N32*) - LD="${LD-ld} -melf32bmipn32" - ;; - *64-bit*) - LD="${LD-ld} -melf64bmip" - ;; - esac - else - case `/usr/bin/file conftest.$ac_objext` in - *32-bit*) - LD="${LD-ld} -32" - ;; - *N32*) - LD="${LD-ld} -n32" - ;; - *64-bit*) - LD="${LD-ld} -64" - ;; - esac - fi - fi - rm -rf conftest* - ;; - -mips64*-*linux*) - # Find out what ABI is being produced by ac_compile, and set linker - # options accordingly. - echo '#line '$LINENO' "configure"' > conftest.$ac_ext - if { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_compile\""; } >&5 - (eval $ac_compile) 2>&5 - ac_status=$? - $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5 - test $ac_status = 0; }; then - emul=elf - case `/usr/bin/file conftest.$ac_objext` in - *32-bit*) - emul="${emul}32" - ;; - *64-bit*) - emul="${emul}64" - ;; - esac - case `/usr/bin/file conftest.$ac_objext` in - *MSB*) - emul="${emul}btsmip" - ;; - *LSB*) - emul="${emul}ltsmip" - ;; - esac - case `/usr/bin/file conftest.$ac_objext` in - *N32*) - emul="${emul}n32" - ;; - esac - LD="${LD-ld} -m $emul" - fi - rm -rf conftest* - ;; - -x86_64-*kfreebsd*-gnu|x86_64-*linux*|powerpc*-*linux*| \ -s390*-*linux*|s390*-*tpf*|sparc*-*linux*) - # Find out what ABI is being produced by ac_compile, and set linker - # options accordingly. Note that the listed cases only cover the - # situations where additional linker options are needed (such as when - # doing 32-bit compilation for a host where ld defaults to 64-bit, or - # vice versa); the common cases where no linker options are needed do - # not appear in the list. - echo 'int i;' > conftest.$ac_ext - if { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_compile\""; } >&5 - (eval $ac_compile) 2>&5 - ac_status=$? - $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5 - test $ac_status = 0; }; then - case `/usr/bin/file conftest.o` in - *32-bit*) - case $host in - x86_64-*kfreebsd*-gnu) - LD="${LD-ld} -m elf_i386_fbsd" - ;; - x86_64-*linux*) - case `/usr/bin/file conftest.o` in - *x86-64*) - LD="${LD-ld} -m elf32_x86_64" - ;; - *) - LD="${LD-ld} -m elf_i386" - ;; - esac - ;; - powerpc64le-*linux*) - LD="${LD-ld} -m elf32lppclinux" - ;; - powerpc64-*linux*) - LD="${LD-ld} -m elf32ppclinux" - ;; - s390x-*linux*) - LD="${LD-ld} -m elf_s390" - ;; - sparc64-*linux*) - LD="${LD-ld} -m elf32_sparc" - ;; - esac - ;; - *64-bit*) - case $host in - x86_64-*kfreebsd*-gnu) - LD="${LD-ld} -m elf_x86_64_fbsd" - ;; - x86_64-*linux*) - LD="${LD-ld} -m elf_x86_64" - ;; - powerpcle-*linux*) - LD="${LD-ld} -m elf64lppc" - ;; - powerpc-*linux*) - LD="${LD-ld} -m elf64ppc" - ;; - s390*-*linux*|s390*-*tpf*) - LD="${LD-ld} -m elf64_s390" - ;; - sparc*-*linux*) - LD="${LD-ld} -m elf64_sparc" - ;; - esac - ;; - esac - fi - rm -rf conftest* - ;; - -*-*-sco3.2v5*) - # On SCO OpenServer 5, we need -belf to get full-featured binaries. - SAVE_CFLAGS=$CFLAGS - CFLAGS="$CFLAGS -belf" - { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether the C compiler needs -belf" >&5 -$as_echo_n "checking whether the C compiler needs -belf... " >&6; } -if ${lt_cv_cc_needs_belf+:} false; then : - $as_echo_n "(cached) " >&6 -else - ac_ext=c -ac_cpp='$CPP $CPPFLAGS' -ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5' -ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5' -ac_compiler_gnu=$ac_cv_c_compiler_gnu - - cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ - -int -main () -{ - - ; - return 0; -} -_ACEOF -if ac_fn_c_try_link "$LINENO"; then : - lt_cv_cc_needs_belf=yes -else - lt_cv_cc_needs_belf=no -fi -rm -f core conftest.err conftest.$ac_objext \ - conftest$ac_exeext conftest.$ac_ext - ac_ext=c -ac_cpp='$CPP $CPPFLAGS' -ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5' -ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5' -ac_compiler_gnu=$ac_cv_c_compiler_gnu - -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $lt_cv_cc_needs_belf" >&5 -$as_echo "$lt_cv_cc_needs_belf" >&6; } - if test yes != "$lt_cv_cc_needs_belf"; then - # this is probably gcc 2.8.0, egcs 1.0 or newer; no need for -belf - CFLAGS=$SAVE_CFLAGS - fi - ;; -*-*solaris*) - # Find out what ABI is being produced by ac_compile, and set linker - # options accordingly. - echo 'int i;' > conftest.$ac_ext - if { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_compile\""; } >&5 - (eval $ac_compile) 2>&5 - ac_status=$? - $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5 - test $ac_status = 0; }; then - case `/usr/bin/file conftest.o` in - *64-bit*) - case $lt_cv_prog_gnu_ld in - yes*) - case $host in - i?86-*-solaris*|x86_64-*-solaris*) - LD="${LD-ld} -m elf_x86_64" - ;; - sparc*-*-solaris*) - LD="${LD-ld} -m elf64_sparc" - ;; - esac - # GNU ld 2.21 introduced _sol2 emulations. Use them if available. - if ${LD-ld} -V | grep _sol2 >/dev/null 2>&1; then - LD=${LD-ld}_sol2 - fi - ;; - *) - if ${LD-ld} -64 -r -o conftest2.o conftest.o >/dev/null 2>&1; then - LD="${LD-ld} -64" - fi - ;; - esac - ;; - esac - fi - rm -rf conftest* - ;; -esac - -need_locks=$enable_libtool_lock - -if test -n "$ac_tool_prefix"; then - # Extract the first word of "${ac_tool_prefix}mt", so it can be a program name with args. -set dummy ${ac_tool_prefix}mt; ac_word=$2 -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5 -$as_echo_n "checking for $ac_word... " >&6; } -if ${ac_cv_prog_MANIFEST_TOOL+:} false; then : - $as_echo_n "(cached) " >&6 -else - if test -n "$MANIFEST_TOOL"; then - ac_cv_prog_MANIFEST_TOOL="$MANIFEST_TOOL" # Let the user override the test. -else -as_save_IFS=$IFS; IFS=$PATH_SEPARATOR -for as_dir in $PATH -do - IFS=$as_save_IFS - test -z "$as_dir" && as_dir=. - for ac_exec_ext in '' $ac_executable_extensions; do - if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then - ac_cv_prog_MANIFEST_TOOL="${ac_tool_prefix}mt" - $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5 - break 2 - fi -done - done -IFS=$as_save_IFS - -fi -fi -MANIFEST_TOOL=$ac_cv_prog_MANIFEST_TOOL -if test -n "$MANIFEST_TOOL"; then - { $as_echo "$as_me:${as_lineno-$LINENO}: result: $MANIFEST_TOOL" >&5 -$as_echo "$MANIFEST_TOOL" >&6; } -else - { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 -$as_echo "no" >&6; } -fi - - -fi -if test -z "$ac_cv_prog_MANIFEST_TOOL"; then - ac_ct_MANIFEST_TOOL=$MANIFEST_TOOL - # Extract the first word of "mt", so it can be a program name with args. -set dummy mt; ac_word=$2 -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5 -$as_echo_n "checking for $ac_word... " >&6; } -if ${ac_cv_prog_ac_ct_MANIFEST_TOOL+:} false; then : - $as_echo_n "(cached) " >&6 -else - if test -n "$ac_ct_MANIFEST_TOOL"; then - ac_cv_prog_ac_ct_MANIFEST_TOOL="$ac_ct_MANIFEST_TOOL" # Let the user override the test. -else -as_save_IFS=$IFS; IFS=$PATH_SEPARATOR -for as_dir in $PATH -do - IFS=$as_save_IFS - test -z "$as_dir" && as_dir=. - for ac_exec_ext in '' $ac_executable_extensions; do - if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then - ac_cv_prog_ac_ct_MANIFEST_TOOL="mt" - $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5 - break 2 - fi -done - done -IFS=$as_save_IFS - -fi -fi -ac_ct_MANIFEST_TOOL=$ac_cv_prog_ac_ct_MANIFEST_TOOL -if test -n "$ac_ct_MANIFEST_TOOL"; then - { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_ct_MANIFEST_TOOL" >&5 -$as_echo "$ac_ct_MANIFEST_TOOL" >&6; } -else - { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 -$as_echo "no" >&6; } -fi - - if test "x$ac_ct_MANIFEST_TOOL" = x; then - MANIFEST_TOOL=":" - else - case $cross_compiling:$ac_tool_warned in -yes:) -{ $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: using cross tools not prefixed with host triplet" >&5 -$as_echo "$as_me: WARNING: using cross tools not prefixed with host triplet" >&2;} -ac_tool_warned=yes ;; -esac - MANIFEST_TOOL=$ac_ct_MANIFEST_TOOL - fi -else - MANIFEST_TOOL="$ac_cv_prog_MANIFEST_TOOL" -fi - -test -z "$MANIFEST_TOOL" && MANIFEST_TOOL=mt -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking if $MANIFEST_TOOL is a manifest tool" >&5 -$as_echo_n "checking if $MANIFEST_TOOL is a manifest tool... " >&6; } -if ${lt_cv_path_mainfest_tool+:} false; then : - $as_echo_n "(cached) " >&6 -else - lt_cv_path_mainfest_tool=no - echo "$as_me:$LINENO: $MANIFEST_TOOL '-?'" >&5 - $MANIFEST_TOOL '-?' 2>conftest.err > conftest.out - cat conftest.err >&5 - if $GREP 'Manifest Tool' conftest.out > /dev/null; then - lt_cv_path_mainfest_tool=yes - fi - rm -f conftest* -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $lt_cv_path_mainfest_tool" >&5 -$as_echo "$lt_cv_path_mainfest_tool" >&6; } -if test yes != "$lt_cv_path_mainfest_tool"; then - MANIFEST_TOOL=: -fi - - - - - - - case $host_os in - rhapsody* | darwin*) - if test -n "$ac_tool_prefix"; then - # Extract the first word of "${ac_tool_prefix}dsymutil", so it can be a program name with args. -set dummy ${ac_tool_prefix}dsymutil; ac_word=$2 -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5 -$as_echo_n "checking for $ac_word... " >&6; } -if ${ac_cv_prog_DSYMUTIL+:} false; then : - $as_echo_n "(cached) " >&6 -else - if test -n "$DSYMUTIL"; then - ac_cv_prog_DSYMUTIL="$DSYMUTIL" # Let the user override the test. -else -as_save_IFS=$IFS; IFS=$PATH_SEPARATOR -for as_dir in $PATH -do - IFS=$as_save_IFS - test -z "$as_dir" && as_dir=. - for ac_exec_ext in '' $ac_executable_extensions; do - if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then - ac_cv_prog_DSYMUTIL="${ac_tool_prefix}dsymutil" - $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5 - break 2 - fi -done - done -IFS=$as_save_IFS - -fi -fi -DSYMUTIL=$ac_cv_prog_DSYMUTIL -if test -n "$DSYMUTIL"; then - { $as_echo "$as_me:${as_lineno-$LINENO}: result: $DSYMUTIL" >&5 -$as_echo "$DSYMUTIL" >&6; } -else - { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 -$as_echo "no" >&6; } -fi - - -fi -if test -z "$ac_cv_prog_DSYMUTIL"; then - ac_ct_DSYMUTIL=$DSYMUTIL - # Extract the first word of "dsymutil", so it can be a program name with args. -set dummy dsymutil; ac_word=$2 -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5 -$as_echo_n "checking for $ac_word... " >&6; } -if ${ac_cv_prog_ac_ct_DSYMUTIL+:} false; then : - $as_echo_n "(cached) " >&6 -else - if test -n "$ac_ct_DSYMUTIL"; then - ac_cv_prog_ac_ct_DSYMUTIL="$ac_ct_DSYMUTIL" # Let the user override the test. -else -as_save_IFS=$IFS; IFS=$PATH_SEPARATOR -for as_dir in $PATH -do - IFS=$as_save_IFS - test -z "$as_dir" && as_dir=. - for ac_exec_ext in '' $ac_executable_extensions; do - if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then - ac_cv_prog_ac_ct_DSYMUTIL="dsymutil" - $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5 - break 2 - fi -done - done -IFS=$as_save_IFS - -fi -fi -ac_ct_DSYMUTIL=$ac_cv_prog_ac_ct_DSYMUTIL -if test -n "$ac_ct_DSYMUTIL"; then - { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_ct_DSYMUTIL" >&5 -$as_echo "$ac_ct_DSYMUTIL" >&6; } -else - { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 -$as_echo "no" >&6; } -fi - - if test "x$ac_ct_DSYMUTIL" = x; then - DSYMUTIL=":" - else - case $cross_compiling:$ac_tool_warned in -yes:) -{ $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: using cross tools not prefixed with host triplet" >&5 -$as_echo "$as_me: WARNING: using cross tools not prefixed with host triplet" >&2;} -ac_tool_warned=yes ;; -esac - DSYMUTIL=$ac_ct_DSYMUTIL - fi -else - DSYMUTIL="$ac_cv_prog_DSYMUTIL" -fi - - if test -n "$ac_tool_prefix"; then - # Extract the first word of "${ac_tool_prefix}nmedit", so it can be a program name with args. -set dummy ${ac_tool_prefix}nmedit; ac_word=$2 -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5 -$as_echo_n "checking for $ac_word... " >&6; } -if ${ac_cv_prog_NMEDIT+:} false; then : - $as_echo_n "(cached) " >&6 -else - if test -n "$NMEDIT"; then - ac_cv_prog_NMEDIT="$NMEDIT" # Let the user override the test. -else -as_save_IFS=$IFS; IFS=$PATH_SEPARATOR -for as_dir in $PATH -do - IFS=$as_save_IFS - test -z "$as_dir" && as_dir=. - for ac_exec_ext in '' $ac_executable_extensions; do - if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then - ac_cv_prog_NMEDIT="${ac_tool_prefix}nmedit" - $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5 - break 2 - fi -done - done -IFS=$as_save_IFS - -fi -fi -NMEDIT=$ac_cv_prog_NMEDIT -if test -n "$NMEDIT"; then - { $as_echo "$as_me:${as_lineno-$LINENO}: result: $NMEDIT" >&5 -$as_echo "$NMEDIT" >&6; } -else - { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 -$as_echo "no" >&6; } -fi - - -fi -if test -z "$ac_cv_prog_NMEDIT"; then - ac_ct_NMEDIT=$NMEDIT - # Extract the first word of "nmedit", so it can be a program name with args. -set dummy nmedit; ac_word=$2 -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5 -$as_echo_n "checking for $ac_word... " >&6; } -if ${ac_cv_prog_ac_ct_NMEDIT+:} false; then : - $as_echo_n "(cached) " >&6 -else - if test -n "$ac_ct_NMEDIT"; then - ac_cv_prog_ac_ct_NMEDIT="$ac_ct_NMEDIT" # Let the user override the test. -else -as_save_IFS=$IFS; IFS=$PATH_SEPARATOR -for as_dir in $PATH -do - IFS=$as_save_IFS - test -z "$as_dir" && as_dir=. - for ac_exec_ext in '' $ac_executable_extensions; do - if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then - ac_cv_prog_ac_ct_NMEDIT="nmedit" - $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5 - break 2 - fi -done - done -IFS=$as_save_IFS - -fi -fi -ac_ct_NMEDIT=$ac_cv_prog_ac_ct_NMEDIT -if test -n "$ac_ct_NMEDIT"; then - { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_ct_NMEDIT" >&5 -$as_echo "$ac_ct_NMEDIT" >&6; } -else - { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 -$as_echo "no" >&6; } -fi - - if test "x$ac_ct_NMEDIT" = x; then - NMEDIT=":" - else - case $cross_compiling:$ac_tool_warned in -yes:) -{ $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: using cross tools not prefixed with host triplet" >&5 -$as_echo "$as_me: WARNING: using cross tools not prefixed with host triplet" >&2;} -ac_tool_warned=yes ;; -esac - NMEDIT=$ac_ct_NMEDIT - fi -else - NMEDIT="$ac_cv_prog_NMEDIT" -fi - - if test -n "$ac_tool_prefix"; then - # Extract the first word of "${ac_tool_prefix}lipo", so it can be a program name with args. -set dummy ${ac_tool_prefix}lipo; ac_word=$2 -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5 -$as_echo_n "checking for $ac_word... " >&6; } -if ${ac_cv_prog_LIPO+:} false; then : - $as_echo_n "(cached) " >&6 -else - if test -n "$LIPO"; then - ac_cv_prog_LIPO="$LIPO" # Let the user override the test. -else -as_save_IFS=$IFS; IFS=$PATH_SEPARATOR -for as_dir in $PATH -do - IFS=$as_save_IFS - test -z "$as_dir" && as_dir=. - for ac_exec_ext in '' $ac_executable_extensions; do - if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then - ac_cv_prog_LIPO="${ac_tool_prefix}lipo" - $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5 - break 2 - fi -done - done -IFS=$as_save_IFS - -fi -fi -LIPO=$ac_cv_prog_LIPO -if test -n "$LIPO"; then - { $as_echo "$as_me:${as_lineno-$LINENO}: result: $LIPO" >&5 -$as_echo "$LIPO" >&6; } -else - { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 -$as_echo "no" >&6; } -fi - - -fi -if test -z "$ac_cv_prog_LIPO"; then - ac_ct_LIPO=$LIPO - # Extract the first word of "lipo", so it can be a program name with args. -set dummy lipo; ac_word=$2 -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5 -$as_echo_n "checking for $ac_word... " >&6; } -if ${ac_cv_prog_ac_ct_LIPO+:} false; then : - $as_echo_n "(cached) " >&6 -else - if test -n "$ac_ct_LIPO"; then - ac_cv_prog_ac_ct_LIPO="$ac_ct_LIPO" # Let the user override the test. -else -as_save_IFS=$IFS; IFS=$PATH_SEPARATOR -for as_dir in $PATH -do - IFS=$as_save_IFS - test -z "$as_dir" && as_dir=. - for ac_exec_ext in '' $ac_executable_extensions; do - if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then - ac_cv_prog_ac_ct_LIPO="lipo" - $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5 - break 2 - fi -done - done -IFS=$as_save_IFS - -fi -fi -ac_ct_LIPO=$ac_cv_prog_ac_ct_LIPO -if test -n "$ac_ct_LIPO"; then - { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_ct_LIPO" >&5 -$as_echo "$ac_ct_LIPO" >&6; } -else - { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 -$as_echo "no" >&6; } -fi - - if test "x$ac_ct_LIPO" = x; then - LIPO=":" - else - case $cross_compiling:$ac_tool_warned in -yes:) -{ $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: using cross tools not prefixed with host triplet" >&5 -$as_echo "$as_me: WARNING: using cross tools not prefixed with host triplet" >&2;} -ac_tool_warned=yes ;; -esac - LIPO=$ac_ct_LIPO - fi -else - LIPO="$ac_cv_prog_LIPO" -fi - - if test -n "$ac_tool_prefix"; then - # Extract the first word of "${ac_tool_prefix}otool", so it can be a program name with args. -set dummy ${ac_tool_prefix}otool; ac_word=$2 -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5 -$as_echo_n "checking for $ac_word... " >&6; } -if ${ac_cv_prog_OTOOL+:} false; then : - $as_echo_n "(cached) " >&6 -else - if test -n "$OTOOL"; then - ac_cv_prog_OTOOL="$OTOOL" # Let the user override the test. -else -as_save_IFS=$IFS; IFS=$PATH_SEPARATOR -for as_dir in $PATH -do - IFS=$as_save_IFS - test -z "$as_dir" && as_dir=. - for ac_exec_ext in '' $ac_executable_extensions; do - if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then - ac_cv_prog_OTOOL="${ac_tool_prefix}otool" - $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5 - break 2 - fi -done - done -IFS=$as_save_IFS - -fi -fi -OTOOL=$ac_cv_prog_OTOOL -if test -n "$OTOOL"; then - { $as_echo "$as_me:${as_lineno-$LINENO}: result: $OTOOL" >&5 -$as_echo "$OTOOL" >&6; } -else - { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 -$as_echo "no" >&6; } -fi - - -fi -if test -z "$ac_cv_prog_OTOOL"; then - ac_ct_OTOOL=$OTOOL - # Extract the first word of "otool", so it can be a program name with args. -set dummy otool; ac_word=$2 -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5 -$as_echo_n "checking for $ac_word... " >&6; } -if ${ac_cv_prog_ac_ct_OTOOL+:} false; then : - $as_echo_n "(cached) " >&6 -else - if test -n "$ac_ct_OTOOL"; then - ac_cv_prog_ac_ct_OTOOL="$ac_ct_OTOOL" # Let the user override the test. -else -as_save_IFS=$IFS; IFS=$PATH_SEPARATOR -for as_dir in $PATH -do - IFS=$as_save_IFS - test -z "$as_dir" && as_dir=. - for ac_exec_ext in '' $ac_executable_extensions; do - if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then - ac_cv_prog_ac_ct_OTOOL="otool" - $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5 - break 2 - fi -done - done -IFS=$as_save_IFS - -fi -fi -ac_ct_OTOOL=$ac_cv_prog_ac_ct_OTOOL -if test -n "$ac_ct_OTOOL"; then - { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_ct_OTOOL" >&5 -$as_echo "$ac_ct_OTOOL" >&6; } -else - { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 -$as_echo "no" >&6; } -fi - - if test "x$ac_ct_OTOOL" = x; then - OTOOL=":" - else - case $cross_compiling:$ac_tool_warned in -yes:) -{ $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: using cross tools not prefixed with host triplet" >&5 -$as_echo "$as_me: WARNING: using cross tools not prefixed with host triplet" >&2;} -ac_tool_warned=yes ;; -esac - OTOOL=$ac_ct_OTOOL - fi -else - OTOOL="$ac_cv_prog_OTOOL" -fi - - if test -n "$ac_tool_prefix"; then - # Extract the first word of "${ac_tool_prefix}otool64", so it can be a program name with args. -set dummy ${ac_tool_prefix}otool64; ac_word=$2 -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5 -$as_echo_n "checking for $ac_word... " >&6; } -if ${ac_cv_prog_OTOOL64+:} false; then : - $as_echo_n "(cached) " >&6 -else - if test -n "$OTOOL64"; then - ac_cv_prog_OTOOL64="$OTOOL64" # Let the user override the test. -else -as_save_IFS=$IFS; IFS=$PATH_SEPARATOR -for as_dir in $PATH -do - IFS=$as_save_IFS - test -z "$as_dir" && as_dir=. - for ac_exec_ext in '' $ac_executable_extensions; do - if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then - ac_cv_prog_OTOOL64="${ac_tool_prefix}otool64" - $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5 - break 2 - fi -done - done -IFS=$as_save_IFS - -fi -fi -OTOOL64=$ac_cv_prog_OTOOL64 -if test -n "$OTOOL64"; then - { $as_echo "$as_me:${as_lineno-$LINENO}: result: $OTOOL64" >&5 -$as_echo "$OTOOL64" >&6; } -else - { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 -$as_echo "no" >&6; } -fi - - -fi -if test -z "$ac_cv_prog_OTOOL64"; then - ac_ct_OTOOL64=$OTOOL64 - # Extract the first word of "otool64", so it can be a program name with args. -set dummy otool64; ac_word=$2 -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5 -$as_echo_n "checking for $ac_word... " >&6; } -if ${ac_cv_prog_ac_ct_OTOOL64+:} false; then : - $as_echo_n "(cached) " >&6 -else - if test -n "$ac_ct_OTOOL64"; then - ac_cv_prog_ac_ct_OTOOL64="$ac_ct_OTOOL64" # Let the user override the test. -else -as_save_IFS=$IFS; IFS=$PATH_SEPARATOR -for as_dir in $PATH -do - IFS=$as_save_IFS - test -z "$as_dir" && as_dir=. - for ac_exec_ext in '' $ac_executable_extensions; do - if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then - ac_cv_prog_ac_ct_OTOOL64="otool64" - $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5 - break 2 - fi -done - done -IFS=$as_save_IFS - -fi -fi -ac_ct_OTOOL64=$ac_cv_prog_ac_ct_OTOOL64 -if test -n "$ac_ct_OTOOL64"; then - { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_ct_OTOOL64" >&5 -$as_echo "$ac_ct_OTOOL64" >&6; } -else - { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 -$as_echo "no" >&6; } -fi - - if test "x$ac_ct_OTOOL64" = x; then - OTOOL64=":" - else - case $cross_compiling:$ac_tool_warned in -yes:) -{ $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: using cross tools not prefixed with host triplet" >&5 -$as_echo "$as_me: WARNING: using cross tools not prefixed with host triplet" >&2;} -ac_tool_warned=yes ;; -esac - OTOOL64=$ac_ct_OTOOL64 - fi -else - OTOOL64="$ac_cv_prog_OTOOL64" -fi - - - - - - - - - - - - - - - - - - - - - - - - - - - - { $as_echo "$as_me:${as_lineno-$LINENO}: checking for -single_module linker flag" >&5 -$as_echo_n "checking for -single_module linker flag... " >&6; } -if ${lt_cv_apple_cc_single_mod+:} false; then : - $as_echo_n "(cached) " >&6 -else - lt_cv_apple_cc_single_mod=no - if test -z "$LT_MULTI_MODULE"; then - # By default we will add the -single_module flag. You can override - # by either setting the environment variable LT_MULTI_MODULE - # non-empty at configure time, or by adding -multi_module to the - # link flags. - rm -rf libconftest.dylib* - echo "int foo(void){return 1;}" > conftest.c - echo "$LTCC $LTCFLAGS $LDFLAGS -o libconftest.dylib \ --dynamiclib -Wl,-single_module conftest.c" >&5 - $LTCC $LTCFLAGS $LDFLAGS -o libconftest.dylib \ - -dynamiclib -Wl,-single_module conftest.c 2>conftest.err - _lt_result=$? - # If there is a non-empty error log, and "single_module" - # appears in it, assume the flag caused a linker warning - if test -s conftest.err && $GREP single_module conftest.err; then - cat conftest.err >&5 - # Otherwise, if the output was created with a 0 exit code from - # the compiler, it worked. - elif test -f libconftest.dylib && test 0 = "$_lt_result"; then - lt_cv_apple_cc_single_mod=yes - else - cat conftest.err >&5 - fi - rm -rf libconftest.dylib* - rm -f conftest.* - fi -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $lt_cv_apple_cc_single_mod" >&5 -$as_echo "$lt_cv_apple_cc_single_mod" >&6; } - - { $as_echo "$as_me:${as_lineno-$LINENO}: checking for -exported_symbols_list linker flag" >&5 -$as_echo_n "checking for -exported_symbols_list linker flag... " >&6; } -if ${lt_cv_ld_exported_symbols_list+:} false; then : - $as_echo_n "(cached) " >&6 -else - lt_cv_ld_exported_symbols_list=no - save_LDFLAGS=$LDFLAGS - echo "_main" > conftest.sym - LDFLAGS="$LDFLAGS -Wl,-exported_symbols_list,conftest.sym" - cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ - -int -main () -{ - - ; - return 0; -} -_ACEOF -if ac_fn_c_try_link "$LINENO"; then : - lt_cv_ld_exported_symbols_list=yes -else - lt_cv_ld_exported_symbols_list=no -fi -rm -f core conftest.err conftest.$ac_objext \ - conftest$ac_exeext conftest.$ac_ext - LDFLAGS=$save_LDFLAGS - -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $lt_cv_ld_exported_symbols_list" >&5 -$as_echo "$lt_cv_ld_exported_symbols_list" >&6; } - - { $as_echo "$as_me:${as_lineno-$LINENO}: checking for -force_load linker flag" >&5 -$as_echo_n "checking for -force_load linker flag... " >&6; } -if ${lt_cv_ld_force_load+:} false; then : - $as_echo_n "(cached) " >&6 -else - lt_cv_ld_force_load=no - cat > conftest.c << _LT_EOF -int forced_loaded() { return 2;} -_LT_EOF - echo "$LTCC $LTCFLAGS -c -o conftest.o conftest.c" >&5 - $LTCC $LTCFLAGS -c -o conftest.o conftest.c 2>&5 - echo "$AR cru libconftest.a conftest.o" >&5 - $AR cru libconftest.a conftest.o 2>&5 - echo "$RANLIB libconftest.a" >&5 - $RANLIB libconftest.a 2>&5 - cat > conftest.c << _LT_EOF -int main() { return 0;} -_LT_EOF - echo "$LTCC $LTCFLAGS $LDFLAGS -o conftest conftest.c -Wl,-force_load,./libconftest.a" >&5 - $LTCC $LTCFLAGS $LDFLAGS -o conftest conftest.c -Wl,-force_load,./libconftest.a 2>conftest.err - _lt_result=$? - if test -s conftest.err && $GREP force_load conftest.err; then - cat conftest.err >&5 - elif test -f conftest && test 0 = "$_lt_result" && $GREP forced_load conftest >/dev/null 2>&1; then - lt_cv_ld_force_load=yes - else - cat conftest.err >&5 - fi - rm -f conftest.err libconftest.a conftest conftest.c - rm -rf conftest.dSYM - -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $lt_cv_ld_force_load" >&5 -$as_echo "$lt_cv_ld_force_load" >&6; } - case $host_os in - rhapsody* | darwin1.[012]) - _lt_dar_allow_undefined='$wl-undefined ${wl}suppress' ;; - darwin1.*) - _lt_dar_allow_undefined='$wl-flat_namespace $wl-undefined ${wl}suppress' ;; - darwin*) # darwin 5.x on - # if running on 10.5 or later, the deployment target defaults - # to the OS version, if on x86, and 10.4, the deployment - # target defaults to 10.4. Don't you love it? - case ${MACOSX_DEPLOYMENT_TARGET-10.0},$host in - 10.0,*86*-darwin8*|10.0,*-darwin[91]*) - _lt_dar_allow_undefined='$wl-undefined ${wl}dynamic_lookup' ;; - 10.[012][,.]*) - _lt_dar_allow_undefined='$wl-flat_namespace $wl-undefined ${wl}suppress' ;; - 10.*) - _lt_dar_allow_undefined='$wl-undefined ${wl}dynamic_lookup' ;; - esac - ;; - esac - if test yes = "$lt_cv_apple_cc_single_mod"; then - _lt_dar_single_mod='$single_module' - fi - if test yes = "$lt_cv_ld_exported_symbols_list"; then - _lt_dar_export_syms=' $wl-exported_symbols_list,$output_objdir/$libname-symbols.expsym' - else - _lt_dar_export_syms='~$NMEDIT -s $output_objdir/$libname-symbols.expsym $lib' - fi - if test : != "$DSYMUTIL" && test no = "$lt_cv_ld_force_load"; then - _lt_dsymutil='~$DSYMUTIL $lib || :' - else - _lt_dsymutil= - fi - ;; - esac - -# func_munge_path_list VARIABLE PATH -# ----------------------------------- -# VARIABLE is name of variable containing _space_ separated list of -# directories to be munged by the contents of PATH, which is string -# having a format: -# "DIR[:DIR]:" -# string "DIR[ DIR]" will be prepended to VARIABLE -# ":DIR[:DIR]" -# string "DIR[ DIR]" will be appended to VARIABLE -# "DIRP[:DIRP]::[DIRA:]DIRA" -# string "DIRP[ DIRP]" will be prepended to VARIABLE and string -# "DIRA[ DIRA]" will be appended to VARIABLE -# "DIR[:DIR]" -# VARIABLE will be replaced by "DIR[ DIR]" -func_munge_path_list () -{ - case x$2 in - x) - ;; - *:) - eval $1=\"`$ECHO $2 | $SED 's/:/ /g'` \$$1\" - ;; - x:*) - eval $1=\"\$$1 `$ECHO $2 | $SED 's/:/ /g'`\" - ;; - *::*) - eval $1=\"\$$1\ `$ECHO $2 | $SED -e 's/.*:://' -e 's/:/ /g'`\" - eval $1=\"`$ECHO $2 | $SED -e 's/::.*//' -e 's/:/ /g'`\ \$$1\" - ;; - *) - eval $1=\"`$ECHO $2 | $SED 's/:/ /g'`\" - ;; - esac -} - -ac_ext=c -ac_cpp='$CPP $CPPFLAGS' -ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5' -ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5' -ac_compiler_gnu=$ac_cv_c_compiler_gnu -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking how to run the C preprocessor" >&5 -$as_echo_n "checking how to run the C preprocessor... " >&6; } -# On Suns, sometimes $CPP names a directory. -if test -n "$CPP" && test -d "$CPP"; then - CPP= -fi -if test -z "$CPP"; then - if ${ac_cv_prog_CPP+:} false; then : - $as_echo_n "(cached) " >&6 -else - # Double quotes because CPP needs to be expanded - for CPP in "$CC -E" "$CC -E -traditional-cpp" "/lib/cpp" - do - ac_preproc_ok=false -for ac_c_preproc_warn_flag in '' yes -do - # Use a header file that comes with gcc, so configuring glibc - # with a fresh cross-compiler works. - # Prefer to if __STDC__ is defined, since - # exists even on freestanding compilers. - # On the NeXT, cc -E runs the code through the compiler's parser, - # not just through cpp. "Syntax error" is here to catch this case. - cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ -#ifdef __STDC__ -# include -#else -# include -#endif - Syntax error -_ACEOF -if ac_fn_c_try_cpp "$LINENO"; then : - -else - # Broken: fails on valid input. -continue -fi -rm -f conftest.err conftest.i conftest.$ac_ext - - # OK, works on sane cases. Now check whether nonexistent headers - # can be detected and how. - cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ -#include -_ACEOF -if ac_fn_c_try_cpp "$LINENO"; then : - # Broken: success on invalid input. -continue -else - # Passes both tests. -ac_preproc_ok=: -break -fi -rm -f conftest.err conftest.i conftest.$ac_ext - -done -# Because of `break', _AC_PREPROC_IFELSE's cleaning code was skipped. -rm -f conftest.i conftest.err conftest.$ac_ext -if $ac_preproc_ok; then : - break -fi - - done - ac_cv_prog_CPP=$CPP - -fi - CPP=$ac_cv_prog_CPP -else - ac_cv_prog_CPP=$CPP -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $CPP" >&5 -$as_echo "$CPP" >&6; } -ac_preproc_ok=false -for ac_c_preproc_warn_flag in '' yes -do - # Use a header file that comes with gcc, so configuring glibc - # with a fresh cross-compiler works. - # Prefer to if __STDC__ is defined, since - # exists even on freestanding compilers. - # On the NeXT, cc -E runs the code through the compiler's parser, - # not just through cpp. "Syntax error" is here to catch this case. - cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ -#ifdef __STDC__ -# include -#else -# include -#endif - Syntax error -_ACEOF -if ac_fn_c_try_cpp "$LINENO"; then : - -else - # Broken: fails on valid input. -continue -fi -rm -f conftest.err conftest.i conftest.$ac_ext - - # OK, works on sane cases. Now check whether nonexistent headers - # can be detected and how. - cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ -#include -_ACEOF -if ac_fn_c_try_cpp "$LINENO"; then : - # Broken: success on invalid input. -continue -else - # Passes both tests. -ac_preproc_ok=: -break -fi -rm -f conftest.err conftest.i conftest.$ac_ext - -done -# Because of `break', _AC_PREPROC_IFELSE's cleaning code was skipped. -rm -f conftest.i conftest.err conftest.$ac_ext -if $ac_preproc_ok; then : - -else - { { $as_echo "$as_me:${as_lineno-$LINENO}: error: in \`$ac_pwd':" >&5 -$as_echo "$as_me: error: in \`$ac_pwd':" >&2;} -as_fn_error $? "C preprocessor \"$CPP\" fails sanity check -See \`config.log' for more details" "$LINENO" 5; } -fi - -ac_ext=c -ac_cpp='$CPP $CPPFLAGS' -ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5' -ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5' -ac_compiler_gnu=$ac_cv_c_compiler_gnu - - -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for ANSI C header files" >&5 -$as_echo_n "checking for ANSI C header files... " >&6; } -if ${ac_cv_header_stdc+:} false; then : - $as_echo_n "(cached) " >&6 -else - cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ -#include -#include -#include -#include - -int -main () -{ - - ; - return 0; -} -_ACEOF -if ac_fn_c_try_compile "$LINENO"; then : - ac_cv_header_stdc=yes -else - ac_cv_header_stdc=no -fi -rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext - -if test $ac_cv_header_stdc = yes; then - # SunOS 4.x string.h does not declare mem*, contrary to ANSI. - cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ -#include - -_ACEOF -if (eval "$ac_cpp conftest.$ac_ext") 2>&5 | - $EGREP "memchr" >/dev/null 2>&1; then : - -else - ac_cv_header_stdc=no -fi -rm -f conftest* - -fi - -if test $ac_cv_header_stdc = yes; then - # ISC 2.0.2 stdlib.h does not declare free, contrary to ANSI. - cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ -#include - -_ACEOF -if (eval "$ac_cpp conftest.$ac_ext") 2>&5 | - $EGREP "free" >/dev/null 2>&1; then : - -else - ac_cv_header_stdc=no -fi -rm -f conftest* - -fi - -if test $ac_cv_header_stdc = yes; then - # /bin/cc in Irix-4.0.5 gets non-ANSI ctype macros unless using -ansi. - if test "$cross_compiling" = yes; then : - : -else - cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ -#include -#include -#if ((' ' & 0x0FF) == 0x020) -# define ISLOWER(c) ('a' <= (c) && (c) <= 'z') -# define TOUPPER(c) (ISLOWER(c) ? 'A' + ((c) - 'a') : (c)) -#else -# define ISLOWER(c) \ - (('a' <= (c) && (c) <= 'i') \ - || ('j' <= (c) && (c) <= 'r') \ - || ('s' <= (c) && (c) <= 'z')) -# define TOUPPER(c) (ISLOWER(c) ? ((c) | 0x40) : (c)) -#endif - -#define XOR(e, f) (((e) && !(f)) || (!(e) && (f))) -int -main () -{ - int i; - for (i = 0; i < 256; i++) - if (XOR (islower (i), ISLOWER (i)) - || toupper (i) != TOUPPER (i)) - return 2; - return 0; -} -_ACEOF -if ac_fn_c_try_run "$LINENO"; then : - -else - ac_cv_header_stdc=no -fi -rm -f core *.core core.conftest.* gmon.out bb.out conftest$ac_exeext \ - conftest.$ac_objext conftest.beam conftest.$ac_ext -fi - -fi -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_header_stdc" >&5 -$as_echo "$ac_cv_header_stdc" >&6; } -if test $ac_cv_header_stdc = yes; then - -$as_echo "#define STDC_HEADERS 1" >>confdefs.h - -fi - -# On IRIX 5.3, sys/types and inttypes.h are conflicting. -for ac_header in sys/types.h sys/stat.h stdlib.h string.h memory.h strings.h \ - inttypes.h stdint.h unistd.h -do : - as_ac_Header=`$as_echo "ac_cv_header_$ac_header" | $as_tr_sh` -ac_fn_c_check_header_compile "$LINENO" "$ac_header" "$as_ac_Header" "$ac_includes_default -" -if eval test \"x\$"$as_ac_Header"\" = x"yes"; then : - cat >>confdefs.h <<_ACEOF -#define `$as_echo "HAVE_$ac_header" | $as_tr_cpp` 1 -_ACEOF - -fi - -done - - -for ac_header in dlfcn.h -do : - ac_fn_c_check_header_compile "$LINENO" "dlfcn.h" "ac_cv_header_dlfcn_h" "$ac_includes_default -" -if test "x$ac_cv_header_dlfcn_h" = xyes; then : - cat >>confdefs.h <<_ACEOF -#define HAVE_DLFCN_H 1 -_ACEOF - -fi - -done - - - - - -# Set options - - - - enable_dlopen=no - - - enable_win32_dll=no - - - # Check whether --enable-shared was given. -if test "${enable_shared+set}" = set; then : - enableval=$enable_shared; p=${PACKAGE-default} - case $enableval in - yes) enable_shared=yes ;; - no) enable_shared=no ;; - *) - enable_shared=no - # Look at the argument we got. We use all the common list separators. - lt_save_ifs=$IFS; IFS=$IFS$PATH_SEPARATOR, - for pkg in $enableval; do - IFS=$lt_save_ifs - if test "X$pkg" = "X$p"; then - enable_shared=yes - fi - done - IFS=$lt_save_ifs - ;; - esac -else - enable_shared=yes -fi - - - - - - - - - - # Check whether --enable-static was given. -if test "${enable_static+set}" = set; then : - enableval=$enable_static; p=${PACKAGE-default} - case $enableval in - yes) enable_static=yes ;; - no) enable_static=no ;; - *) - enable_static=no - # Look at the argument we got. We use all the common list separators. - lt_save_ifs=$IFS; IFS=$IFS$PATH_SEPARATOR, - for pkg in $enableval; do - IFS=$lt_save_ifs - if test "X$pkg" = "X$p"; then - enable_static=yes - fi - done - IFS=$lt_save_ifs - ;; - esac -else - enable_static=yes -fi - - - - - - - - - - -# Check whether --with-pic was given. -if test "${with_pic+set}" = set; then : - withval=$with_pic; lt_p=${PACKAGE-default} - case $withval in - yes|no) pic_mode=$withval ;; - *) - pic_mode=default - # Look at the argument we got. We use all the common list separators. - lt_save_ifs=$IFS; IFS=$IFS$PATH_SEPARATOR, - for lt_pkg in $withval; do - IFS=$lt_save_ifs - if test "X$lt_pkg" = "X$lt_p"; then - pic_mode=yes - fi - done - IFS=$lt_save_ifs - ;; - esac -else - pic_mode=default -fi - - - - - - - - - # Check whether --enable-fast-install was given. -if test "${enable_fast_install+set}" = set; then : - enableval=$enable_fast_install; p=${PACKAGE-default} - case $enableval in - yes) enable_fast_install=yes ;; - no) enable_fast_install=no ;; - *) - enable_fast_install=no - # Look at the argument we got. We use all the common list separators. - lt_save_ifs=$IFS; IFS=$IFS$PATH_SEPARATOR, - for pkg in $enableval; do - IFS=$lt_save_ifs - if test "X$pkg" = "X$p"; then - enable_fast_install=yes - fi - done - IFS=$lt_save_ifs - ;; - esac -else - enable_fast_install=yes -fi - - - - - - - - - shared_archive_member_spec= -case $host,$enable_shared in -power*-*-aix[5-9]*,yes) - { $as_echo "$as_me:${as_lineno-$LINENO}: checking which variant of shared library versioning to provide" >&5 -$as_echo_n "checking which variant of shared library versioning to provide... " >&6; } - -# Check whether --with-aix-soname was given. -if test "${with_aix_soname+set}" = set; then : - withval=$with_aix_soname; case $withval in - aix|svr4|both) - ;; - *) - as_fn_error $? "Unknown argument to --with-aix-soname" "$LINENO" 5 - ;; - esac - lt_cv_with_aix_soname=$with_aix_soname -else - if ${lt_cv_with_aix_soname+:} false; then : - $as_echo_n "(cached) " >&6 -else - lt_cv_with_aix_soname=aix -fi - - with_aix_soname=$lt_cv_with_aix_soname -fi - - { $as_echo "$as_me:${as_lineno-$LINENO}: result: $with_aix_soname" >&5 -$as_echo "$with_aix_soname" >&6; } - if test aix != "$with_aix_soname"; then - # For the AIX way of multilib, we name the shared archive member - # based on the bitwidth used, traditionally 'shr.o' or 'shr_64.o', - # and 'shr.imp' or 'shr_64.imp', respectively, for the Import File. - # Even when GNU compilers ignore OBJECT_MODE but need '-maix64' flag, - # the AIX toolchain works better with OBJECT_MODE set (default 32). - if test 64 = "${OBJECT_MODE-32}"; then - shared_archive_member_spec=shr_64 - else - shared_archive_member_spec=shr - fi - fi - ;; -*) - with_aix_soname=aix - ;; -esac - - - - - - - - - - -# This can be used to rebuild libtool when needed -LIBTOOL_DEPS=$ltmain - -# Always use our own libtool. -LIBTOOL='$(SHELL) $(top_builddir)/libtool' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -test -z "$LN_S" && LN_S="ln -s" - - - - - - - - - - - - - - -if test -n "${ZSH_VERSION+set}"; then - setopt NO_GLOB_SUBST -fi - -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for objdir" >&5 -$as_echo_n "checking for objdir... " >&6; } -if ${lt_cv_objdir+:} false; then : - $as_echo_n "(cached) " >&6 -else - rm -f .libs 2>/dev/null -mkdir .libs 2>/dev/null -if test -d .libs; then - lt_cv_objdir=.libs -else - # MS-DOS does not allow filenames that begin with a dot. - lt_cv_objdir=_libs -fi -rmdir .libs 2>/dev/null -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $lt_cv_objdir" >&5 -$as_echo "$lt_cv_objdir" >&6; } -objdir=$lt_cv_objdir - - - - - -cat >>confdefs.h <<_ACEOF -#define LT_OBJDIR "$lt_cv_objdir/" -_ACEOF - - - - -case $host_os in -aix3*) - # AIX sometimes has problems with the GCC collect2 program. For some - # reason, if we set the COLLECT_NAMES environment variable, the problems - # vanish in a puff of smoke. - if test set != "${COLLECT_NAMES+set}"; then - COLLECT_NAMES= - export COLLECT_NAMES - fi - ;; -esac - -# Global variables: -ofile=libtool -can_build_shared=yes - -# All known linkers require a '.a' archive for static linking (except MSVC, -# which needs '.lib'). -libext=a - -with_gnu_ld=$lt_cv_prog_gnu_ld - -old_CC=$CC -old_CFLAGS=$CFLAGS - -# Set sane defaults for various variables -test -z "$CC" && CC=cc -test -z "$LTCC" && LTCC=$CC -test -z "$LTCFLAGS" && LTCFLAGS=$CFLAGS -test -z "$LD" && LD=ld -test -z "$ac_objext" && ac_objext=o - -func_cc_basename $compiler -cc_basename=$func_cc_basename_result - - -# Only perform the check for file, if the check method requires it -test -z "$MAGIC_CMD" && MAGIC_CMD=file -case $deplibs_check_method in -file_magic*) - if test "$file_magic_cmd" = '$MAGIC_CMD'; then - { $as_echo "$as_me:${as_lineno-$LINENO}: checking for ${ac_tool_prefix}file" >&5 -$as_echo_n "checking for ${ac_tool_prefix}file... " >&6; } -if ${lt_cv_path_MAGIC_CMD+:} false; then : - $as_echo_n "(cached) " >&6 -else - case $MAGIC_CMD in -[\\/*] | ?:[\\/]*) - lt_cv_path_MAGIC_CMD=$MAGIC_CMD # Let the user override the test with a path. - ;; -*) - lt_save_MAGIC_CMD=$MAGIC_CMD - lt_save_ifs=$IFS; IFS=$PATH_SEPARATOR - ac_dummy="/usr/bin$PATH_SEPARATOR$PATH" - for ac_dir in $ac_dummy; do - IFS=$lt_save_ifs - test -z "$ac_dir" && ac_dir=. - if test -f "$ac_dir/${ac_tool_prefix}file"; then - lt_cv_path_MAGIC_CMD=$ac_dir/"${ac_tool_prefix}file" - if test -n "$file_magic_test_file"; then - case $deplibs_check_method in - "file_magic "*) - file_magic_regex=`expr "$deplibs_check_method" : "file_magic \(.*\)"` - MAGIC_CMD=$lt_cv_path_MAGIC_CMD - if eval $file_magic_cmd \$file_magic_test_file 2> /dev/null | - $EGREP "$file_magic_regex" > /dev/null; then - : - else - cat <<_LT_EOF 1>&2 - -*** Warning: the command libtool uses to detect shared libraries, -*** $file_magic_cmd, produces output that libtool cannot recognize. -*** The result is that libtool may fail to recognize shared libraries -*** as such. This will affect the creation of libtool libraries that -*** depend on shared libraries, but programs linked with such libtool -*** libraries will work regardless of this problem. Nevertheless, you -*** may want to report the problem to your system manager and/or to -*** bug-libtool@gnu.org - -_LT_EOF - fi ;; - esac - fi - break - fi - done - IFS=$lt_save_ifs - MAGIC_CMD=$lt_save_MAGIC_CMD - ;; -esac -fi - -MAGIC_CMD=$lt_cv_path_MAGIC_CMD -if test -n "$MAGIC_CMD"; then - { $as_echo "$as_me:${as_lineno-$LINENO}: result: $MAGIC_CMD" >&5 -$as_echo "$MAGIC_CMD" >&6; } -else - { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 -$as_echo "no" >&6; } -fi - - - - - -if test -z "$lt_cv_path_MAGIC_CMD"; then - if test -n "$ac_tool_prefix"; then - { $as_echo "$as_me:${as_lineno-$LINENO}: checking for file" >&5 -$as_echo_n "checking for file... " >&6; } -if ${lt_cv_path_MAGIC_CMD+:} false; then : - $as_echo_n "(cached) " >&6 -else - case $MAGIC_CMD in -[\\/*] | ?:[\\/]*) - lt_cv_path_MAGIC_CMD=$MAGIC_CMD # Let the user override the test with a path. - ;; -*) - lt_save_MAGIC_CMD=$MAGIC_CMD - lt_save_ifs=$IFS; IFS=$PATH_SEPARATOR - ac_dummy="/usr/bin$PATH_SEPARATOR$PATH" - for ac_dir in $ac_dummy; do - IFS=$lt_save_ifs - test -z "$ac_dir" && ac_dir=. - if test -f "$ac_dir/file"; then - lt_cv_path_MAGIC_CMD=$ac_dir/"file" - if test -n "$file_magic_test_file"; then - case $deplibs_check_method in - "file_magic "*) - file_magic_regex=`expr "$deplibs_check_method" : "file_magic \(.*\)"` - MAGIC_CMD=$lt_cv_path_MAGIC_CMD - if eval $file_magic_cmd \$file_magic_test_file 2> /dev/null | - $EGREP "$file_magic_regex" > /dev/null; then - : - else - cat <<_LT_EOF 1>&2 - -*** Warning: the command libtool uses to detect shared libraries, -*** $file_magic_cmd, produces output that libtool cannot recognize. -*** The result is that libtool may fail to recognize shared libraries -*** as such. This will affect the creation of libtool libraries that -*** depend on shared libraries, but programs linked with such libtool -*** libraries will work regardless of this problem. Nevertheless, you -*** may want to report the problem to your system manager and/or to -*** bug-libtool@gnu.org - -_LT_EOF - fi ;; - esac - fi - break - fi - done - IFS=$lt_save_ifs - MAGIC_CMD=$lt_save_MAGIC_CMD - ;; -esac -fi - -MAGIC_CMD=$lt_cv_path_MAGIC_CMD -if test -n "$MAGIC_CMD"; then - { $as_echo "$as_me:${as_lineno-$LINENO}: result: $MAGIC_CMD" >&5 -$as_echo "$MAGIC_CMD" >&6; } -else - { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 -$as_echo "no" >&6; } -fi - - - else - MAGIC_CMD=: - fi -fi - - fi - ;; -esac - -# Use C for the default configuration in the libtool script - -lt_save_CC=$CC -ac_ext=c -ac_cpp='$CPP $CPPFLAGS' -ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5' -ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5' -ac_compiler_gnu=$ac_cv_c_compiler_gnu - - -# Source file extension for C test sources. -ac_ext=c - -# Object file extension for compiled C test sources. -objext=o -objext=$objext - -# Code to be used in simple compile tests -lt_simple_compile_test_code="int some_variable = 0;" - -# Code to be used in simple link tests -lt_simple_link_test_code='int main(){return(0);}' - - - - - - - -# If no C compiler was specified, use CC. -LTCC=${LTCC-"$CC"} - -# If no C compiler flags were specified, use CFLAGS. -LTCFLAGS=${LTCFLAGS-"$CFLAGS"} - -# Allow CC to be a program name with arguments. -compiler=$CC - -# Save the default compiler, since it gets overwritten when the other -# tags are being tested, and _LT_TAGVAR(compiler, []) is a NOP. -compiler_DEFAULT=$CC - -# save warnings/boilerplate of simple test code -ac_outfile=conftest.$ac_objext -echo "$lt_simple_compile_test_code" >conftest.$ac_ext -eval "$ac_compile" 2>&1 >/dev/null | $SED '/^$/d; /^ *+/d' >conftest.err -_lt_compiler_boilerplate=`cat conftest.err` -$RM conftest* - -ac_outfile=conftest.$ac_objext -echo "$lt_simple_link_test_code" >conftest.$ac_ext -eval "$ac_link" 2>&1 >/dev/null | $SED '/^$/d; /^ *+/d' >conftest.err -_lt_linker_boilerplate=`cat conftest.err` -$RM -r conftest* - - -if test -n "$compiler"; then - -lt_prog_compiler_no_builtin_flag= - -if test yes = "$GCC"; then - case $cc_basename in - nvcc*) - lt_prog_compiler_no_builtin_flag=' -Xcompiler -fno-builtin' ;; - *) - lt_prog_compiler_no_builtin_flag=' -fno-builtin' ;; - esac - - { $as_echo "$as_me:${as_lineno-$LINENO}: checking if $compiler supports -fno-rtti -fno-exceptions" >&5 -$as_echo_n "checking if $compiler supports -fno-rtti -fno-exceptions... " >&6; } -if ${lt_cv_prog_compiler_rtti_exceptions+:} false; then : - $as_echo_n "(cached) " >&6 -else - lt_cv_prog_compiler_rtti_exceptions=no - ac_outfile=conftest.$ac_objext - echo "$lt_simple_compile_test_code" > conftest.$ac_ext - lt_compiler_flag="-fno-rtti -fno-exceptions" ## exclude from sc_useless_quotes_in_assignment - # Insert the option either (1) after the last *FLAGS variable, or - # (2) before a word containing "conftest.", or (3) at the end. - # Note that $ac_compile itself does not contain backslashes and begins - # with a dollar sign (not a hyphen), so the echo should work correctly. - # The option is referenced via a variable to avoid confusing sed. - lt_compile=`echo "$ac_compile" | $SED \ - -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \ - -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \ - -e 's:$: $lt_compiler_flag:'` - (eval echo "\"\$as_me:$LINENO: $lt_compile\"" >&5) - (eval "$lt_compile" 2>conftest.err) - ac_status=$? - cat conftest.err >&5 - echo "$as_me:$LINENO: \$? = $ac_status" >&5 - if (exit $ac_status) && test -s "$ac_outfile"; then - # The compiler can only warn and ignore the option if not recognized - # So say no if there are warnings other than the usual output. - $ECHO "$_lt_compiler_boilerplate" | $SED '/^$/d' >conftest.exp - $SED '/^$/d; /^ *+/d' conftest.err >conftest.er2 - if test ! -s conftest.er2 || diff conftest.exp conftest.er2 >/dev/null; then - lt_cv_prog_compiler_rtti_exceptions=yes - fi - fi - $RM conftest* - -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $lt_cv_prog_compiler_rtti_exceptions" >&5 -$as_echo "$lt_cv_prog_compiler_rtti_exceptions" >&6; } - -if test yes = "$lt_cv_prog_compiler_rtti_exceptions"; then - lt_prog_compiler_no_builtin_flag="$lt_prog_compiler_no_builtin_flag -fno-rtti -fno-exceptions" -else - : -fi - -fi - - - - - - - lt_prog_compiler_wl= -lt_prog_compiler_pic= -lt_prog_compiler_static= - - - if test yes = "$GCC"; then - lt_prog_compiler_wl='-Wl,' - lt_prog_compiler_static='-static' - - case $host_os in - aix*) - # All AIX code is PIC. - if test ia64 = "$host_cpu"; then - # AIX 5 now supports IA64 processor - lt_prog_compiler_static='-Bstatic' - fi - lt_prog_compiler_pic='-fPIC' - ;; - - amigaos*) - case $host_cpu in - powerpc) - # see comment about AmigaOS4 .so support - lt_prog_compiler_pic='-fPIC' - ;; - m68k) - # FIXME: we need at least 68020 code to build shared libraries, but - # adding the '-m68020' flag to GCC prevents building anything better, - # like '-m68040'. - lt_prog_compiler_pic='-m68020 -resident32 -malways-restore-a4' - ;; - esac - ;; - - beos* | irix5* | irix6* | nonstopux* | osf3* | osf4* | osf5*) - # PIC is the default for these OSes. - ;; - - mingw* | cygwin* | pw32* | os2* | cegcc*) - # This hack is so that the source file can tell whether it is being - # built for inclusion in a dll (and should export symbols for example). - # Although the cygwin gcc ignores -fPIC, still need this for old-style - # (--disable-auto-import) libraries - lt_prog_compiler_pic='-DDLL_EXPORT' - case $host_os in - os2*) - lt_prog_compiler_static='$wl-static' - ;; - esac - ;; - - darwin* | rhapsody*) - # PIC is the default on this platform - # Common symbols not allowed in MH_DYLIB files - lt_prog_compiler_pic='-fno-common' - ;; - - haiku*) - # PIC is the default for Haiku. - # The "-static" flag exists, but is broken. - lt_prog_compiler_static= - ;; - - hpux*) - # PIC is the default for 64-bit PA HP-UX, but not for 32-bit - # PA HP-UX. On IA64 HP-UX, PIC is the default but the pic flag - # sets the default TLS model and affects inlining. - case $host_cpu in - hppa*64*) - # +Z the default - ;; - *) - lt_prog_compiler_pic='-fPIC' - ;; - esac - ;; - - interix[3-9]*) - # Interix 3.x gcc -fpic/-fPIC options generate broken code. - # Instead, we relocate shared libraries at runtime. - ;; - - msdosdjgpp*) - # Just because we use GCC doesn't mean we suddenly get shared libraries - # on systems that don't support them. - lt_prog_compiler_can_build_shared=no - enable_shared=no - ;; - - *nto* | *qnx*) - # QNX uses GNU C++, but need to define -shared option too, otherwise - # it will coredump. - lt_prog_compiler_pic='-fPIC -shared' - ;; - - sysv4*MP*) - if test -d /usr/nec; then - lt_prog_compiler_pic=-Kconform_pic - fi - ;; - - *) - lt_prog_compiler_pic='-fPIC' - ;; - esac - - case $cc_basename in - nvcc*) # Cuda Compiler Driver 2.2 - lt_prog_compiler_wl='-Xlinker ' - if test -n "$lt_prog_compiler_pic"; then - lt_prog_compiler_pic="-Xcompiler $lt_prog_compiler_pic" - fi - ;; - esac - else - # PORTME Check for flag to pass linker flags through the system compiler. - case $host_os in - aix*) - lt_prog_compiler_wl='-Wl,' - if test ia64 = "$host_cpu"; then - # AIX 5 now supports IA64 processor - lt_prog_compiler_static='-Bstatic' - else - lt_prog_compiler_static='-bnso -bI:/lib/syscalls.exp' - fi - ;; - - darwin* | rhapsody*) - # PIC is the default on this platform - # Common symbols not allowed in MH_DYLIB files - lt_prog_compiler_pic='-fno-common' - case $cc_basename in - nagfor*) - # NAG Fortran compiler - lt_prog_compiler_wl='-Wl,-Wl,,' - lt_prog_compiler_pic='-PIC' - lt_prog_compiler_static='-Bstatic' - ;; - esac - ;; - - mingw* | cygwin* | pw32* | os2* | cegcc*) - # This hack is so that the source file can tell whether it is being - # built for inclusion in a dll (and should export symbols for example). - lt_prog_compiler_pic='-DDLL_EXPORT' - case $host_os in - os2*) - lt_prog_compiler_static='$wl-static' - ;; - esac - ;; - - hpux9* | hpux10* | hpux11*) - lt_prog_compiler_wl='-Wl,' - # PIC is the default for IA64 HP-UX and 64-bit HP-UX, but - # not for PA HP-UX. - case $host_cpu in - hppa*64*|ia64*) - # +Z the default - ;; - *) - lt_prog_compiler_pic='+Z' - ;; - esac - # Is there a better lt_prog_compiler_static that works with the bundled CC? - lt_prog_compiler_static='$wl-a ${wl}archive' - ;; - - irix5* | irix6* | nonstopux*) - lt_prog_compiler_wl='-Wl,' - # PIC (with -KPIC) is the default. - lt_prog_compiler_static='-non_shared' - ;; - - linux* | k*bsd*-gnu | kopensolaris*-gnu | gnu*) - case $cc_basename in - # old Intel for x86_64, which still supported -KPIC. - ecc*) - lt_prog_compiler_wl='-Wl,' - lt_prog_compiler_pic='-KPIC' - lt_prog_compiler_static='-static' - ;; - # icc used to be incompatible with GCC. - # ICC 10 doesn't accept -KPIC any more. - icc* | ifort*) - lt_prog_compiler_wl='-Wl,' - lt_prog_compiler_pic='-fPIC' - lt_prog_compiler_static='-static' - ;; - # Lahey Fortran 8.1. - lf95*) - lt_prog_compiler_wl='-Wl,' - lt_prog_compiler_pic='--shared' - lt_prog_compiler_static='--static' - ;; - nagfor*) - # NAG Fortran compiler - lt_prog_compiler_wl='-Wl,-Wl,,' - lt_prog_compiler_pic='-PIC' - lt_prog_compiler_static='-Bstatic' - ;; - tcc*) - # Fabrice Bellard et al's Tiny C Compiler - lt_prog_compiler_wl='-Wl,' - lt_prog_compiler_pic='-fPIC' - lt_prog_compiler_static='-static' - ;; - pgcc* | pgf77* | pgf90* | pgf95* | pgfortran*) - # Portland Group compilers (*not* the Pentium gcc compiler, - # which looks to be a dead project) - lt_prog_compiler_wl='-Wl,' - lt_prog_compiler_pic='-fpic' - lt_prog_compiler_static='-Bstatic' - ;; - ccc*) - lt_prog_compiler_wl='-Wl,' - # All Alpha code is PIC. - lt_prog_compiler_static='-non_shared' - ;; - xl* | bgxl* | bgf* | mpixl*) - # IBM XL C 8.0/Fortran 10.1, 11.1 on PPC and BlueGene - lt_prog_compiler_wl='-Wl,' - lt_prog_compiler_pic='-qpic' - lt_prog_compiler_static='-qstaticlink' - ;; - *) - case `$CC -V 2>&1 | sed 5q` in - *Sun\ Ceres\ Fortran* | *Sun*Fortran*\ [1-7].* | *Sun*Fortran*\ 8.[0-3]*) - # Sun Fortran 8.3 passes all unrecognized flags to the linker - lt_prog_compiler_pic='-KPIC' - lt_prog_compiler_static='-Bstatic' - lt_prog_compiler_wl='' - ;; - *Sun\ F* | *Sun*Fortran*) - lt_prog_compiler_pic='-KPIC' - lt_prog_compiler_static='-Bstatic' - lt_prog_compiler_wl='-Qoption ld ' - ;; - *Sun\ C*) - # Sun C 5.9 - lt_prog_compiler_pic='-KPIC' - lt_prog_compiler_static='-Bstatic' - lt_prog_compiler_wl='-Wl,' - ;; - *Intel*\ [CF]*Compiler*) - lt_prog_compiler_wl='-Wl,' - lt_prog_compiler_pic='-fPIC' - lt_prog_compiler_static='-static' - ;; - *Portland\ Group*) - lt_prog_compiler_wl='-Wl,' - lt_prog_compiler_pic='-fpic' - lt_prog_compiler_static='-Bstatic' - ;; - esac - ;; - esac - ;; - - newsos6) - lt_prog_compiler_pic='-KPIC' - lt_prog_compiler_static='-Bstatic' - ;; - - *nto* | *qnx*) - # QNX uses GNU C++, but need to define -shared option too, otherwise - # it will coredump. - lt_prog_compiler_pic='-fPIC -shared' - ;; - - osf3* | osf4* | osf5*) - lt_prog_compiler_wl='-Wl,' - # All OSF/1 code is PIC. - lt_prog_compiler_static='-non_shared' - ;; - - rdos*) - lt_prog_compiler_static='-non_shared' - ;; - - solaris*) - lt_prog_compiler_pic='-KPIC' - lt_prog_compiler_static='-Bstatic' - case $cc_basename in - f77* | f90* | f95* | sunf77* | sunf90* | sunf95*) - lt_prog_compiler_wl='-Qoption ld ';; - *) - lt_prog_compiler_wl='-Wl,';; - esac - ;; - - sunos4*) - lt_prog_compiler_wl='-Qoption ld ' - lt_prog_compiler_pic='-PIC' - lt_prog_compiler_static='-Bstatic' - ;; - - sysv4 | sysv4.2uw2* | sysv4.3*) - lt_prog_compiler_wl='-Wl,' - lt_prog_compiler_pic='-KPIC' - lt_prog_compiler_static='-Bstatic' - ;; - - sysv4*MP*) - if test -d /usr/nec; then - lt_prog_compiler_pic='-Kconform_pic' - lt_prog_compiler_static='-Bstatic' - fi - ;; - - sysv5* | unixware* | sco3.2v5* | sco5v6* | OpenUNIX*) - lt_prog_compiler_wl='-Wl,' - lt_prog_compiler_pic='-KPIC' - lt_prog_compiler_static='-Bstatic' - ;; - - unicos*) - lt_prog_compiler_wl='-Wl,' - lt_prog_compiler_can_build_shared=no - ;; - - uts4*) - lt_prog_compiler_pic='-pic' - lt_prog_compiler_static='-Bstatic' - ;; - - *) - lt_prog_compiler_can_build_shared=no - ;; - esac - fi - -case $host_os in - # For platforms that do not support PIC, -DPIC is meaningless: - *djgpp*) - lt_prog_compiler_pic= - ;; - *) - lt_prog_compiler_pic="$lt_prog_compiler_pic -DPIC" - ;; -esac - -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $compiler option to produce PIC" >&5 -$as_echo_n "checking for $compiler option to produce PIC... " >&6; } -if ${lt_cv_prog_compiler_pic+:} false; then : - $as_echo_n "(cached) " >&6 -else - lt_cv_prog_compiler_pic=$lt_prog_compiler_pic -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $lt_cv_prog_compiler_pic" >&5 -$as_echo "$lt_cv_prog_compiler_pic" >&6; } -lt_prog_compiler_pic=$lt_cv_prog_compiler_pic - -# -# Check to make sure the PIC flag actually works. -# -if test -n "$lt_prog_compiler_pic"; then - { $as_echo "$as_me:${as_lineno-$LINENO}: checking if $compiler PIC flag $lt_prog_compiler_pic works" >&5 -$as_echo_n "checking if $compiler PIC flag $lt_prog_compiler_pic works... " >&6; } -if ${lt_cv_prog_compiler_pic_works+:} false; then : - $as_echo_n "(cached) " >&6 -else - lt_cv_prog_compiler_pic_works=no - ac_outfile=conftest.$ac_objext - echo "$lt_simple_compile_test_code" > conftest.$ac_ext - lt_compiler_flag="$lt_prog_compiler_pic -DPIC" ## exclude from sc_useless_quotes_in_assignment - # Insert the option either (1) after the last *FLAGS variable, or - # (2) before a word containing "conftest.", or (3) at the end. - # Note that $ac_compile itself does not contain backslashes and begins - # with a dollar sign (not a hyphen), so the echo should work correctly. - # The option is referenced via a variable to avoid confusing sed. - lt_compile=`echo "$ac_compile" | $SED \ - -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \ - -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \ - -e 's:$: $lt_compiler_flag:'` - (eval echo "\"\$as_me:$LINENO: $lt_compile\"" >&5) - (eval "$lt_compile" 2>conftest.err) - ac_status=$? - cat conftest.err >&5 - echo "$as_me:$LINENO: \$? = $ac_status" >&5 - if (exit $ac_status) && test -s "$ac_outfile"; then - # The compiler can only warn and ignore the option if not recognized - # So say no if there are warnings other than the usual output. - $ECHO "$_lt_compiler_boilerplate" | $SED '/^$/d' >conftest.exp - $SED '/^$/d; /^ *+/d' conftest.err >conftest.er2 - if test ! -s conftest.er2 || diff conftest.exp conftest.er2 >/dev/null; then - lt_cv_prog_compiler_pic_works=yes - fi - fi - $RM conftest* - -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $lt_cv_prog_compiler_pic_works" >&5 -$as_echo "$lt_cv_prog_compiler_pic_works" >&6; } - -if test yes = "$lt_cv_prog_compiler_pic_works"; then - case $lt_prog_compiler_pic in - "" | " "*) ;; - *) lt_prog_compiler_pic=" $lt_prog_compiler_pic" ;; - esac -else - lt_prog_compiler_pic= - lt_prog_compiler_can_build_shared=no -fi - -fi - - - - - - - - - - - -# -# Check to make sure the static flag actually works. -# -wl=$lt_prog_compiler_wl eval lt_tmp_static_flag=\"$lt_prog_compiler_static\" -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking if $compiler static flag $lt_tmp_static_flag works" >&5 -$as_echo_n "checking if $compiler static flag $lt_tmp_static_flag works... " >&6; } -if ${lt_cv_prog_compiler_static_works+:} false; then : - $as_echo_n "(cached) " >&6 -else - lt_cv_prog_compiler_static_works=no - save_LDFLAGS=$LDFLAGS - LDFLAGS="$LDFLAGS $lt_tmp_static_flag" - echo "$lt_simple_link_test_code" > conftest.$ac_ext - if (eval $ac_link 2>conftest.err) && test -s conftest$ac_exeext; then - # The linker can only warn and ignore the option if not recognized - # So say no if there are warnings - if test -s conftest.err; then - # Append any errors to the config.log. - cat conftest.err 1>&5 - $ECHO "$_lt_linker_boilerplate" | $SED '/^$/d' > conftest.exp - $SED '/^$/d; /^ *+/d' conftest.err >conftest.er2 - if diff conftest.exp conftest.er2 >/dev/null; then - lt_cv_prog_compiler_static_works=yes - fi - else - lt_cv_prog_compiler_static_works=yes - fi - fi - $RM -r conftest* - LDFLAGS=$save_LDFLAGS - -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $lt_cv_prog_compiler_static_works" >&5 -$as_echo "$lt_cv_prog_compiler_static_works" >&6; } - -if test yes = "$lt_cv_prog_compiler_static_works"; then - : -else - lt_prog_compiler_static= -fi - - - - - - - - { $as_echo "$as_me:${as_lineno-$LINENO}: checking if $compiler supports -c -o file.$ac_objext" >&5 -$as_echo_n "checking if $compiler supports -c -o file.$ac_objext... " >&6; } -if ${lt_cv_prog_compiler_c_o+:} false; then : - $as_echo_n "(cached) " >&6 -else - lt_cv_prog_compiler_c_o=no - $RM -r conftest 2>/dev/null - mkdir conftest - cd conftest - mkdir out - echo "$lt_simple_compile_test_code" > conftest.$ac_ext - - lt_compiler_flag="-o out/conftest2.$ac_objext" - # Insert the option either (1) after the last *FLAGS variable, or - # (2) before a word containing "conftest.", or (3) at the end. - # Note that $ac_compile itself does not contain backslashes and begins - # with a dollar sign (not a hyphen), so the echo should work correctly. - lt_compile=`echo "$ac_compile" | $SED \ - -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \ - -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \ - -e 's:$: $lt_compiler_flag:'` - (eval echo "\"\$as_me:$LINENO: $lt_compile\"" >&5) - (eval "$lt_compile" 2>out/conftest.err) - ac_status=$? - cat out/conftest.err >&5 - echo "$as_me:$LINENO: \$? = $ac_status" >&5 - if (exit $ac_status) && test -s out/conftest2.$ac_objext - then - # The compiler can only warn and ignore the option if not recognized - # So say no if there are warnings - $ECHO "$_lt_compiler_boilerplate" | $SED '/^$/d' > out/conftest.exp - $SED '/^$/d; /^ *+/d' out/conftest.err >out/conftest.er2 - if test ! -s out/conftest.er2 || diff out/conftest.exp out/conftest.er2 >/dev/null; then - lt_cv_prog_compiler_c_o=yes - fi - fi - chmod u+w . 2>&5 - $RM conftest* - # SGI C++ compiler will create directory out/ii_files/ for - # template instantiation - test -d out/ii_files && $RM out/ii_files/* && rmdir out/ii_files - $RM out/* && rmdir out - cd .. - $RM -r conftest - $RM conftest* - -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $lt_cv_prog_compiler_c_o" >&5 -$as_echo "$lt_cv_prog_compiler_c_o" >&6; } - - - - - - - { $as_echo "$as_me:${as_lineno-$LINENO}: checking if $compiler supports -c -o file.$ac_objext" >&5 -$as_echo_n "checking if $compiler supports -c -o file.$ac_objext... " >&6; } -if ${lt_cv_prog_compiler_c_o+:} false; then : - $as_echo_n "(cached) " >&6 -else - lt_cv_prog_compiler_c_o=no - $RM -r conftest 2>/dev/null - mkdir conftest - cd conftest - mkdir out - echo "$lt_simple_compile_test_code" > conftest.$ac_ext - - lt_compiler_flag="-o out/conftest2.$ac_objext" - # Insert the option either (1) after the last *FLAGS variable, or - # (2) before a word containing "conftest.", or (3) at the end. - # Note that $ac_compile itself does not contain backslashes and begins - # with a dollar sign (not a hyphen), so the echo should work correctly. - lt_compile=`echo "$ac_compile" | $SED \ - -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \ - -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \ - -e 's:$: $lt_compiler_flag:'` - (eval echo "\"\$as_me:$LINENO: $lt_compile\"" >&5) - (eval "$lt_compile" 2>out/conftest.err) - ac_status=$? - cat out/conftest.err >&5 - echo "$as_me:$LINENO: \$? = $ac_status" >&5 - if (exit $ac_status) && test -s out/conftest2.$ac_objext - then - # The compiler can only warn and ignore the option if not recognized - # So say no if there are warnings - $ECHO "$_lt_compiler_boilerplate" | $SED '/^$/d' > out/conftest.exp - $SED '/^$/d; /^ *+/d' out/conftest.err >out/conftest.er2 - if test ! -s out/conftest.er2 || diff out/conftest.exp out/conftest.er2 >/dev/null; then - lt_cv_prog_compiler_c_o=yes - fi - fi - chmod u+w . 2>&5 - $RM conftest* - # SGI C++ compiler will create directory out/ii_files/ for - # template instantiation - test -d out/ii_files && $RM out/ii_files/* && rmdir out/ii_files - $RM out/* && rmdir out - cd .. - $RM -r conftest - $RM conftest* - -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $lt_cv_prog_compiler_c_o" >&5 -$as_echo "$lt_cv_prog_compiler_c_o" >&6; } - - - - -hard_links=nottested -if test no = "$lt_cv_prog_compiler_c_o" && test no != "$need_locks"; then - # do not overwrite the value of need_locks provided by the user - { $as_echo "$as_me:${as_lineno-$LINENO}: checking if we can lock with hard links" >&5 -$as_echo_n "checking if we can lock with hard links... " >&6; } - hard_links=yes - $RM conftest* - ln conftest.a conftest.b 2>/dev/null && hard_links=no - touch conftest.a - ln conftest.a conftest.b 2>&5 || hard_links=no - ln conftest.a conftest.b 2>/dev/null && hard_links=no - { $as_echo "$as_me:${as_lineno-$LINENO}: result: $hard_links" >&5 -$as_echo "$hard_links" >&6; } - if test no = "$hard_links"; then - { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: '$CC' does not support '-c -o', so 'make -j' may be unsafe" >&5 -$as_echo "$as_me: WARNING: '$CC' does not support '-c -o', so 'make -j' may be unsafe" >&2;} - need_locks=warn - fi -else - need_locks=no -fi - - - - - - - { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether the $compiler linker ($LD) supports shared libraries" >&5 -$as_echo_n "checking whether the $compiler linker ($LD) supports shared libraries... " >&6; } - - runpath_var= - allow_undefined_flag= - always_export_symbols=no - archive_cmds= - archive_expsym_cmds= - compiler_needs_object=no - enable_shared_with_static_runtimes=no - export_dynamic_flag_spec= - export_symbols_cmds='$NM $libobjs $convenience | $global_symbol_pipe | $SED '\''s/.* //'\'' | sort | uniq > $export_symbols' - hardcode_automatic=no - hardcode_direct=no - hardcode_direct_absolute=no - hardcode_libdir_flag_spec= - hardcode_libdir_separator= - hardcode_minus_L=no - hardcode_shlibpath_var=unsupported - inherit_rpath=no - link_all_deplibs=unknown - module_cmds= - module_expsym_cmds= - old_archive_from_new_cmds= - old_archive_from_expsyms_cmds= - thread_safe_flag_spec= - whole_archive_flag_spec= - # include_expsyms should be a list of space-separated symbols to be *always* - # included in the symbol list - include_expsyms= - # exclude_expsyms can be an extended regexp of symbols to exclude - # it will be wrapped by ' (' and ')$', so one must not match beginning or - # end of line. Example: 'a|bc|.*d.*' will exclude the symbols 'a' and 'bc', - # as well as any symbol that contains 'd'. - exclude_expsyms='_GLOBAL_OFFSET_TABLE_|_GLOBAL__F[ID]_.*' - # Although _GLOBAL_OFFSET_TABLE_ is a valid symbol C name, most a.out - # platforms (ab)use it in PIC code, but their linkers get confused if - # the symbol is explicitly referenced. Since portable code cannot - # rely on this symbol name, it's probably fine to never include it in - # preloaded symbol tables. - # Exclude shared library initialization/finalization symbols. - extract_expsyms_cmds= - - case $host_os in - cygwin* | mingw* | pw32* | cegcc*) - # FIXME: the MSVC++ port hasn't been tested in a loooong time - # When not using gcc, we currently assume that we are using - # Microsoft Visual C++. - if test yes != "$GCC"; then - with_gnu_ld=no - fi - ;; - interix*) - # we just hope/assume this is gcc and not c89 (= MSVC++) - with_gnu_ld=yes - ;; - openbsd* | bitrig*) - with_gnu_ld=no - ;; - esac - - ld_shlibs=yes - - # On some targets, GNU ld is compatible enough with the native linker - # that we're better off using the native interface for both. - lt_use_gnu_ld_interface=no - if test yes = "$with_gnu_ld"; then - case $host_os in - aix*) - # The AIX port of GNU ld has always aspired to compatibility - # with the native linker. However, as the warning in the GNU ld - # block says, versions before 2.19.5* couldn't really create working - # shared libraries, regardless of the interface used. - case `$LD -v 2>&1` in - *\ \(GNU\ Binutils\)\ 2.19.5*) ;; - *\ \(GNU\ Binutils\)\ 2.[2-9]*) ;; - *\ \(GNU\ Binutils\)\ [3-9]*) ;; - *) - lt_use_gnu_ld_interface=yes - ;; - esac - ;; - *) - lt_use_gnu_ld_interface=yes - ;; - esac - fi - - if test yes = "$lt_use_gnu_ld_interface"; then - # If archive_cmds runs LD, not CC, wlarc should be empty - wlarc='$wl' - - # Set some defaults for GNU ld with shared library support. These - # are reset later if shared libraries are not supported. Putting them - # here allows them to be overridden if necessary. - runpath_var=LD_RUN_PATH - hardcode_libdir_flag_spec='$wl-rpath $wl$libdir' - export_dynamic_flag_spec='$wl--export-dynamic' - # ancient GNU ld didn't support --whole-archive et. al. - if $LD --help 2>&1 | $GREP 'no-whole-archive' > /dev/null; then - whole_archive_flag_spec=$wlarc'--whole-archive$convenience '$wlarc'--no-whole-archive' - else - whole_archive_flag_spec= - fi - supports_anon_versioning=no - case `$LD -v | $SED -e 's/(^)\+)\s\+//' 2>&1` in - *GNU\ gold*) supports_anon_versioning=yes ;; - *\ [01].* | *\ 2.[0-9].* | *\ 2.10.*) ;; # catch versions < 2.11 - *\ 2.11.93.0.2\ *) supports_anon_versioning=yes ;; # RH7.3 ... - *\ 2.11.92.0.12\ *) supports_anon_versioning=yes ;; # Mandrake 8.2 ... - *\ 2.11.*) ;; # other 2.11 versions - *) supports_anon_versioning=yes ;; - esac - - # See if GNU ld supports shared libraries. - case $host_os in - aix[3-9]*) - # On AIX/PPC, the GNU linker is very broken - if test ia64 != "$host_cpu"; then - ld_shlibs=no - cat <<_LT_EOF 1>&2 - -*** Warning: the GNU linker, at least up to release 2.19, is reported -*** to be unable to reliably create shared libraries on AIX. -*** Therefore, libtool is disabling shared libraries support. If you -*** really care for shared libraries, you may want to install binutils -*** 2.20 or above, or modify your PATH so that a non-GNU linker is found. -*** You will then need to restart the configuration process. - -_LT_EOF - fi - ;; - - amigaos*) - case $host_cpu in - powerpc) - # see comment about AmigaOS4 .so support - archive_cmds='$CC -shared $libobjs $deplibs $compiler_flags $wl-soname $wl$soname -o $lib' - archive_expsym_cmds='' - ;; - m68k) - archive_cmds='$RM $output_objdir/a2ixlibrary.data~$ECHO "#define NAME $libname" > $output_objdir/a2ixlibrary.data~$ECHO "#define LIBRARY_ID 1" >> $output_objdir/a2ixlibrary.data~$ECHO "#define VERSION $major" >> $output_objdir/a2ixlibrary.data~$ECHO "#define REVISION $revision" >> $output_objdir/a2ixlibrary.data~$AR $AR_FLAGS $lib $libobjs~$RANLIB $lib~(cd $output_objdir && a2ixlibrary -32)' - hardcode_libdir_flag_spec='-L$libdir' - hardcode_minus_L=yes - ;; - esac - ;; - - beos*) - if $LD --help 2>&1 | $GREP ': supported targets:.* elf' > /dev/null; then - allow_undefined_flag=unsupported - # Joseph Beckenbach says some releases of gcc - # support --undefined. This deserves some investigation. FIXME - archive_cmds='$CC -nostart $libobjs $deplibs $compiler_flags $wl-soname $wl$soname -o $lib' - else - ld_shlibs=no - fi - ;; - - cygwin* | mingw* | pw32* | cegcc*) - # _LT_TAGVAR(hardcode_libdir_flag_spec, ) is actually meaningless, - # as there is no search path for DLLs. - hardcode_libdir_flag_spec='-L$libdir' - export_dynamic_flag_spec='$wl--export-all-symbols' - allow_undefined_flag=unsupported - always_export_symbols=no - enable_shared_with_static_runtimes=yes - export_symbols_cmds='$NM $libobjs $convenience | $global_symbol_pipe | $SED -e '\''/^[BCDGRS][ ]/s/.*[ ]\([^ ]*\)/\1 DATA/;s/^.*[ ]__nm__\([^ ]*\)[ ][^ ]*/\1 DATA/;/^I[ ]/d;/^[AITW][ ]/s/.* //'\'' | sort | uniq > $export_symbols' - exclude_expsyms='[_]+GLOBAL_OFFSET_TABLE_|[_]+GLOBAL__[FID]_.*|[_]+head_[A-Za-z0-9_]+_dll|[A-Za-z0-9_]+_dll_iname' - - if $LD --help 2>&1 | $GREP 'auto-import' > /dev/null; then - archive_cmds='$CC -shared $libobjs $deplibs $compiler_flags -o $output_objdir/$soname $wl--enable-auto-image-base -Xlinker --out-implib -Xlinker $lib' - # If the export-symbols file already is a .def file, use it as - # is; otherwise, prepend EXPORTS... - archive_expsym_cmds='if test DEF = "`$SED -n -e '\''s/^[ ]*//'\'' -e '\''/^\(;.*\)*$/d'\'' -e '\''s/^\(EXPORTS\|LIBRARY\)\([ ].*\)*$/DEF/p'\'' -e q $export_symbols`" ; then - cp $export_symbols $output_objdir/$soname.def; - else - echo EXPORTS > $output_objdir/$soname.def; - cat $export_symbols >> $output_objdir/$soname.def; - fi~ - $CC -shared $output_objdir/$soname.def $libobjs $deplibs $compiler_flags -o $output_objdir/$soname $wl--enable-auto-image-base -Xlinker --out-implib -Xlinker $lib' - else - ld_shlibs=no - fi - ;; - - haiku*) - archive_cmds='$CC -shared $libobjs $deplibs $compiler_flags $wl-soname $wl$soname -o $lib' - link_all_deplibs=yes - ;; - - os2*) - hardcode_libdir_flag_spec='-L$libdir' - hardcode_minus_L=yes - allow_undefined_flag=unsupported - shrext_cmds=.dll - archive_cmds='$ECHO "LIBRARY ${soname%$shared_ext} INITINSTANCE TERMINSTANCE" > $output_objdir/$libname.def~ - $ECHO "DESCRIPTION \"$libname\"" >> $output_objdir/$libname.def~ - $ECHO "DATA MULTIPLE NONSHARED" >> $output_objdir/$libname.def~ - $ECHO EXPORTS >> $output_objdir/$libname.def~ - emxexp $libobjs | $SED /"_DLL_InitTerm"/d >> $output_objdir/$libname.def~ - $CC -Zdll -Zcrtdll -o $output_objdir/$soname $libobjs $deplibs $compiler_flags $output_objdir/$libname.def~ - emximp -o $lib $output_objdir/$libname.def' - archive_expsym_cmds='$ECHO "LIBRARY ${soname%$shared_ext} INITINSTANCE TERMINSTANCE" > $output_objdir/$libname.def~ - $ECHO "DESCRIPTION \"$libname\"" >> $output_objdir/$libname.def~ - $ECHO "DATA MULTIPLE NONSHARED" >> $output_objdir/$libname.def~ - $ECHO EXPORTS >> $output_objdir/$libname.def~ - prefix_cmds="$SED"~ - if test EXPORTS = "`$SED 1q $export_symbols`"; then - prefix_cmds="$prefix_cmds -e 1d"; - fi~ - prefix_cmds="$prefix_cmds -e \"s/^\(.*\)$/_\1/g\""~ - cat $export_symbols | $prefix_cmds >> $output_objdir/$libname.def~ - $CC -Zdll -Zcrtdll -o $output_objdir/$soname $libobjs $deplibs $compiler_flags $output_objdir/$libname.def~ - emximp -o $lib $output_objdir/$libname.def' - old_archive_From_new_cmds='emximp -o $output_objdir/${libname}_dll.a $output_objdir/$libname.def' - enable_shared_with_static_runtimes=yes - ;; - - interix[3-9]*) - hardcode_direct=no - hardcode_shlibpath_var=no - hardcode_libdir_flag_spec='$wl-rpath,$libdir' - export_dynamic_flag_spec='$wl-E' - # Hack: On Interix 3.x, we cannot compile PIC because of a broken gcc. - # Instead, shared libraries are loaded at an image base (0x10000000 by - # default) and relocated if they conflict, which is a slow very memory - # consuming and fragmenting process. To avoid this, we pick a random, - # 256 KiB-aligned image base between 0x50000000 and 0x6FFC0000 at link - # time. Moving up from 0x10000000 also allows more sbrk(2) space. - archive_cmds='$CC -shared $pic_flag $libobjs $deplibs $compiler_flags $wl-h,$soname $wl--image-base,`expr ${RANDOM-$$} % 4096 / 2 \* 262144 + 1342177280` -o $lib' - archive_expsym_cmds='sed "s|^|_|" $export_symbols >$output_objdir/$soname.expsym~$CC -shared $pic_flag $libobjs $deplibs $compiler_flags $wl-h,$soname $wl--retain-symbols-file,$output_objdir/$soname.expsym $wl--image-base,`expr ${RANDOM-$$} % 4096 / 2 \* 262144 + 1342177280` -o $lib' - ;; - - gnu* | linux* | tpf* | k*bsd*-gnu | kopensolaris*-gnu) - tmp_diet=no - if test linux-dietlibc = "$host_os"; then - case $cc_basename in - diet\ *) tmp_diet=yes;; # linux-dietlibc with static linking (!diet-dyn) - esac - fi - if $LD --help 2>&1 | $EGREP ': supported targets:.* elf' > /dev/null \ - && test no = "$tmp_diet" - then - tmp_addflag=' $pic_flag' - tmp_sharedflag='-shared' - case $cc_basename,$host_cpu in - pgcc*) # Portland Group C compiler - whole_archive_flag_spec='$wl--whole-archive`for conv in $convenience\"\"; do test -n \"$conv\" && new_convenience=\"$new_convenience,$conv\"; done; func_echo_all \"$new_convenience\"` $wl--no-whole-archive' - tmp_addflag=' $pic_flag' - ;; - pgf77* | pgf90* | pgf95* | pgfortran*) - # Portland Group f77 and f90 compilers - whole_archive_flag_spec='$wl--whole-archive`for conv in $convenience\"\"; do test -n \"$conv\" && new_convenience=\"$new_convenience,$conv\"; done; func_echo_all \"$new_convenience\"` $wl--no-whole-archive' - tmp_addflag=' $pic_flag -Mnomain' ;; - ecc*,ia64* | icc*,ia64*) # Intel C compiler on ia64 - tmp_addflag=' -i_dynamic' ;; - efc*,ia64* | ifort*,ia64*) # Intel Fortran compiler on ia64 - tmp_addflag=' -i_dynamic -nofor_main' ;; - ifc* | ifort*) # Intel Fortran compiler - tmp_addflag=' -nofor_main' ;; - lf95*) # Lahey Fortran 8.1 - whole_archive_flag_spec= - tmp_sharedflag='--shared' ;; - nagfor*) # NAGFOR 5.3 - tmp_sharedflag='-Wl,-shared' ;; - xl[cC]* | bgxl[cC]* | mpixl[cC]*) # IBM XL C 8.0 on PPC (deal with xlf below) - tmp_sharedflag='-qmkshrobj' - tmp_addflag= ;; - nvcc*) # Cuda Compiler Driver 2.2 - whole_archive_flag_spec='$wl--whole-archive`for conv in $convenience\"\"; do test -n \"$conv\" && new_convenience=\"$new_convenience,$conv\"; done; func_echo_all \"$new_convenience\"` $wl--no-whole-archive' - compiler_needs_object=yes - ;; - esac - case `$CC -V 2>&1 | sed 5q` in - *Sun\ C*) # Sun C 5.9 - whole_archive_flag_spec='$wl--whole-archive`new_convenience=; for conv in $convenience\"\"; do test -z \"$conv\" || new_convenience=\"$new_convenience,$conv\"; done; func_echo_all \"$new_convenience\"` $wl--no-whole-archive' - compiler_needs_object=yes - tmp_sharedflag='-G' ;; - *Sun\ F*) # Sun Fortran 8.3 - tmp_sharedflag='-G' ;; - esac - archive_cmds='$CC '"$tmp_sharedflag""$tmp_addflag"' $libobjs $deplibs $compiler_flags $wl-soname $wl$soname -o $lib' - - if test yes = "$supports_anon_versioning"; then - archive_expsym_cmds='echo "{ global:" > $output_objdir/$libname.ver~ - cat $export_symbols | sed -e "s/\(.*\)/\1;/" >> $output_objdir/$libname.ver~ - echo "local: *; };" >> $output_objdir/$libname.ver~ - $CC '"$tmp_sharedflag""$tmp_addflag"' $libobjs $deplibs $compiler_flags $wl-soname $wl$soname $wl-version-script $wl$output_objdir/$libname.ver -o $lib' - fi - - case $cc_basename in - tcc*) - export_dynamic_flag_spec='-rdynamic' - ;; - xlf* | bgf* | bgxlf* | mpixlf*) - # IBM XL Fortran 10.1 on PPC cannot create shared libs itself - whole_archive_flag_spec='--whole-archive$convenience --no-whole-archive' - hardcode_libdir_flag_spec='$wl-rpath $wl$libdir' - archive_cmds='$LD -shared $libobjs $deplibs $linker_flags -soname $soname -o $lib' - if test yes = "$supports_anon_versioning"; then - archive_expsym_cmds='echo "{ global:" > $output_objdir/$libname.ver~ - cat $export_symbols | sed -e "s/\(.*\)/\1;/" >> $output_objdir/$libname.ver~ - echo "local: *; };" >> $output_objdir/$libname.ver~ - $LD -shared $libobjs $deplibs $linker_flags -soname $soname -version-script $output_objdir/$libname.ver -o $lib' - fi - ;; - esac - else - ld_shlibs=no - fi - ;; - - netbsd*) - if echo __ELF__ | $CC -E - | $GREP __ELF__ >/dev/null; then - archive_cmds='$LD -Bshareable $libobjs $deplibs $linker_flags -o $lib' - wlarc= - else - archive_cmds='$CC -shared $pic_flag $libobjs $deplibs $compiler_flags $wl-soname $wl$soname -o $lib' - archive_expsym_cmds='$CC -shared $pic_flag $libobjs $deplibs $compiler_flags $wl-soname $wl$soname $wl-retain-symbols-file $wl$export_symbols -o $lib' - fi - ;; - - solaris*) - if $LD -v 2>&1 | $GREP 'BFD 2\.8' > /dev/null; then - ld_shlibs=no - cat <<_LT_EOF 1>&2 - -*** Warning: The releases 2.8.* of the GNU linker cannot reliably -*** create shared libraries on Solaris systems. Therefore, libtool -*** is disabling shared libraries support. We urge you to upgrade GNU -*** binutils to release 2.9.1 or newer. Another option is to modify -*** your PATH or compiler configuration so that the native linker is -*** used, and then restart. - -_LT_EOF - elif $LD --help 2>&1 | $GREP ': supported targets:.* elf' > /dev/null; then - archive_cmds='$CC -shared $pic_flag $libobjs $deplibs $compiler_flags $wl-soname $wl$soname -o $lib' - archive_expsym_cmds='$CC -shared $pic_flag $libobjs $deplibs $compiler_flags $wl-soname $wl$soname $wl-retain-symbols-file $wl$export_symbols -o $lib' - else - ld_shlibs=no - fi - ;; - - sysv5* | sco3.2v5* | sco5v6* | unixware* | OpenUNIX*) - case `$LD -v 2>&1` in - *\ [01].* | *\ 2.[0-9].* | *\ 2.1[0-5].*) - ld_shlibs=no - cat <<_LT_EOF 1>&2 - -*** Warning: Releases of the GNU linker prior to 2.16.91.0.3 cannot -*** reliably create shared libraries on SCO systems. Therefore, libtool -*** is disabling shared libraries support. We urge you to upgrade GNU -*** binutils to release 2.16.91.0.3 or newer. Another option is to modify -*** your PATH or compiler configuration so that the native linker is -*** used, and then restart. - -_LT_EOF - ;; - *) - # For security reasons, it is highly recommended that you always - # use absolute paths for naming shared libraries, and exclude the - # DT_RUNPATH tag from executables and libraries. But doing so - # requires that you compile everything twice, which is a pain. - if $LD --help 2>&1 | $GREP ': supported targets:.* elf' > /dev/null; then - hardcode_libdir_flag_spec='$wl-rpath $wl$libdir' - archive_cmds='$CC -shared $libobjs $deplibs $compiler_flags $wl-soname $wl$soname -o $lib' - archive_expsym_cmds='$CC -shared $libobjs $deplibs $compiler_flags $wl-soname $wl$soname $wl-retain-symbols-file $wl$export_symbols -o $lib' - else - ld_shlibs=no - fi - ;; - esac - ;; - - sunos4*) - archive_cmds='$LD -assert pure-text -Bshareable -o $lib $libobjs $deplibs $linker_flags' - wlarc= - hardcode_direct=yes - hardcode_shlibpath_var=no - ;; - - *) - if $LD --help 2>&1 | $GREP ': supported targets:.* elf' > /dev/null; then - archive_cmds='$CC -shared $pic_flag $libobjs $deplibs $compiler_flags $wl-soname $wl$soname -o $lib' - archive_expsym_cmds='$CC -shared $pic_flag $libobjs $deplibs $compiler_flags $wl-soname $wl$soname $wl-retain-symbols-file $wl$export_symbols -o $lib' - else - ld_shlibs=no - fi - ;; - esac - - if test no = "$ld_shlibs"; then - runpath_var= - hardcode_libdir_flag_spec= - export_dynamic_flag_spec= - whole_archive_flag_spec= - fi - else - # PORTME fill in a description of your system's linker (not GNU ld) - case $host_os in - aix3*) - allow_undefined_flag=unsupported - always_export_symbols=yes - archive_expsym_cmds='$LD -o $output_objdir/$soname $libobjs $deplibs $linker_flags -bE:$export_symbols -T512 -H512 -bM:SRE~$AR $AR_FLAGS $lib $output_objdir/$soname' - # Note: this linker hardcodes the directories in LIBPATH if there - # are no directories specified by -L. - hardcode_minus_L=yes - if test yes = "$GCC" && test -z "$lt_prog_compiler_static"; then - # Neither direct hardcoding nor static linking is supported with a - # broken collect2. - hardcode_direct=unsupported - fi - ;; - - aix[4-9]*) - if test ia64 = "$host_cpu"; then - # On IA64, the linker does run time linking by default, so we don't - # have to do anything special. - aix_use_runtimelinking=no - exp_sym_flag='-Bexport' - no_entry_flag= - else - # If we're using GNU nm, then we don't want the "-C" option. - # -C means demangle to GNU nm, but means don't demangle to AIX nm. - # Without the "-l" option, or with the "-B" option, AIX nm treats - # weak defined symbols like other global defined symbols, whereas - # GNU nm marks them as "W". - # While the 'weak' keyword is ignored in the Export File, we need - # it in the Import File for the 'aix-soname' feature, so we have - # to replace the "-B" option with "-P" for AIX nm. - if $NM -V 2>&1 | $GREP 'GNU' > /dev/null; then - export_symbols_cmds='$NM -Bpg $libobjs $convenience | awk '\''{ if (((\$ 2 == "T") || (\$ 2 == "D") || (\$ 2 == "B") || (\$ 2 == "W")) && (substr(\$ 3,1,1) != ".")) { if (\$ 2 == "W") { print \$ 3 " weak" } else { print \$ 3 } } }'\'' | sort -u > $export_symbols' - else - export_symbols_cmds='`func_echo_all $NM | $SED -e '\''s/B\([^B]*\)$/P\1/'\''` -PCpgl $libobjs $convenience | awk '\''{ if (((\$ 2 == "T") || (\$ 2 == "D") || (\$ 2 == "B") || (\$ 2 == "W") || (\$ 2 == "V") || (\$ 2 == "Z")) && (substr(\$ 1,1,1) != ".")) { if ((\$ 2 == "W") || (\$ 2 == "V") || (\$ 2 == "Z")) { print \$ 1 " weak" } else { print \$ 1 } } }'\'' | sort -u > $export_symbols' - fi - aix_use_runtimelinking=no - - # Test if we are trying to use run time linking or normal - # AIX style linking. If -brtl is somewhere in LDFLAGS, we - # have runtime linking enabled, and use it for executables. - # For shared libraries, we enable/disable runtime linking - # depending on the kind of the shared library created - - # when "with_aix_soname,aix_use_runtimelinking" is: - # "aix,no" lib.a(lib.so.V) shared, rtl:no, for executables - # "aix,yes" lib.so shared, rtl:yes, for executables - # lib.a static archive - # "both,no" lib.so.V(shr.o) shared, rtl:yes - # lib.a(lib.so.V) shared, rtl:no, for executables - # "both,yes" lib.so.V(shr.o) shared, rtl:yes, for executables - # lib.a(lib.so.V) shared, rtl:no - # "svr4,*" lib.so.V(shr.o) shared, rtl:yes, for executables - # lib.a static archive - case $host_os in aix4.[23]|aix4.[23].*|aix[5-9]*) - for ld_flag in $LDFLAGS; do - if (test x-brtl = "x$ld_flag" || test x-Wl,-brtl = "x$ld_flag"); then - aix_use_runtimelinking=yes - break - fi - done - if test svr4,no = "$with_aix_soname,$aix_use_runtimelinking"; then - # With aix-soname=svr4, we create the lib.so.V shared archives only, - # so we don't have lib.a shared libs to link our executables. - # We have to force runtime linking in this case. - aix_use_runtimelinking=yes - LDFLAGS="$LDFLAGS -Wl,-brtl" - fi - ;; - esac - - exp_sym_flag='-bexport' - no_entry_flag='-bnoentry' - fi - - # When large executables or shared objects are built, AIX ld can - # have problems creating the table of contents. If linking a library - # or program results in "error TOC overflow" add -mminimal-toc to - # CXXFLAGS/CFLAGS for g++/gcc. In the cases where that is not - # enough to fix the problem, add -Wl,-bbigtoc to LDFLAGS. - - archive_cmds='' - hardcode_direct=yes - hardcode_direct_absolute=yes - hardcode_libdir_separator=':' - link_all_deplibs=yes - file_list_spec='$wl-f,' - case $with_aix_soname,$aix_use_runtimelinking in - aix,*) ;; # traditional, no import file - svr4,* | *,yes) # use import file - # The Import File defines what to hardcode. - hardcode_direct=no - hardcode_direct_absolute=no - ;; - esac - - if test yes = "$GCC"; then - case $host_os in aix4.[012]|aix4.[012].*) - # We only want to do this on AIX 4.2 and lower, the check - # below for broken collect2 doesn't work under 4.3+ - collect2name=`$CC -print-prog-name=collect2` - if test -f "$collect2name" && - strings "$collect2name" | $GREP resolve_lib_name >/dev/null - then - # We have reworked collect2 - : - else - # We have old collect2 - hardcode_direct=unsupported - # It fails to find uninstalled libraries when the uninstalled - # path is not listed in the libpath. Setting hardcode_minus_L - # to unsupported forces relinking - hardcode_minus_L=yes - hardcode_libdir_flag_spec='-L$libdir' - hardcode_libdir_separator= - fi - ;; - esac - shared_flag='-shared' - if test yes = "$aix_use_runtimelinking"; then - shared_flag="$shared_flag "'$wl-G' - fi - # Need to ensure runtime linking is disabled for the traditional - # shared library, or the linker may eventually find shared libraries - # /with/ Import File - we do not want to mix them. - shared_flag_aix='-shared' - shared_flag_svr4='-shared $wl-G' - else - # not using gcc - if test ia64 = "$host_cpu"; then - # VisualAge C++, Version 5.5 for AIX 5L for IA-64, Beta 3 Release - # chokes on -Wl,-G. The following line is correct: - shared_flag='-G' - else - if test yes = "$aix_use_runtimelinking"; then - shared_flag='$wl-G' - else - shared_flag='$wl-bM:SRE' - fi - shared_flag_aix='$wl-bM:SRE' - shared_flag_svr4='$wl-G' - fi - fi - - export_dynamic_flag_spec='$wl-bexpall' - # It seems that -bexpall does not export symbols beginning with - # underscore (_), so it is better to generate a list of symbols to export. - always_export_symbols=yes - if test aix,yes = "$with_aix_soname,$aix_use_runtimelinking"; then - # Warning - without using the other runtime loading flags (-brtl), - # -berok will link without error, but may produce a broken library. - allow_undefined_flag='-berok' - # Determine the default libpath from the value encoded in an - # empty executable. - if test set = "${lt_cv_aix_libpath+set}"; then - aix_libpath=$lt_cv_aix_libpath -else - if ${lt_cv_aix_libpath_+:} false; then : - $as_echo_n "(cached) " >&6 -else - cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ - -int -main () -{ - - ; - return 0; -} -_ACEOF -if ac_fn_c_try_link "$LINENO"; then : - - lt_aix_libpath_sed=' - /Import File Strings/,/^$/ { - /^0/ { - s/^0 *\([^ ]*\) *$/\1/ - p - } - }' - lt_cv_aix_libpath_=`dump -H conftest$ac_exeext 2>/dev/null | $SED -n -e "$lt_aix_libpath_sed"` - # Check for a 64-bit object if we didn't find anything. - if test -z "$lt_cv_aix_libpath_"; then - lt_cv_aix_libpath_=`dump -HX64 conftest$ac_exeext 2>/dev/null | $SED -n -e "$lt_aix_libpath_sed"` - fi -fi -rm -f core conftest.err conftest.$ac_objext \ - conftest$ac_exeext conftest.$ac_ext - if test -z "$lt_cv_aix_libpath_"; then - lt_cv_aix_libpath_=/usr/lib:/lib - fi - -fi - - aix_libpath=$lt_cv_aix_libpath_ -fi - - hardcode_libdir_flag_spec='$wl-blibpath:$libdir:'"$aix_libpath" - archive_expsym_cmds='$CC -o $output_objdir/$soname $libobjs $deplibs $wl'$no_entry_flag' $compiler_flags `if test -n "$allow_undefined_flag"; then func_echo_all "$wl$allow_undefined_flag"; else :; fi` $wl'$exp_sym_flag:\$export_symbols' '$shared_flag - else - if test ia64 = "$host_cpu"; then - hardcode_libdir_flag_spec='$wl-R $libdir:/usr/lib:/lib' - allow_undefined_flag="-z nodefs" - archive_expsym_cmds="\$CC $shared_flag"' -o $output_objdir/$soname $libobjs $deplibs '"\$wl$no_entry_flag"' $compiler_flags $wl$allow_undefined_flag '"\$wl$exp_sym_flag:\$export_symbols" - else - # Determine the default libpath from the value encoded in an - # empty executable. - if test set = "${lt_cv_aix_libpath+set}"; then - aix_libpath=$lt_cv_aix_libpath -else - if ${lt_cv_aix_libpath_+:} false; then : - $as_echo_n "(cached) " >&6 -else - cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ - -int -main () -{ - - ; - return 0; -} -_ACEOF -if ac_fn_c_try_link "$LINENO"; then : - - lt_aix_libpath_sed=' - /Import File Strings/,/^$/ { - /^0/ { - s/^0 *\([^ ]*\) *$/\1/ - p - } - }' - lt_cv_aix_libpath_=`dump -H conftest$ac_exeext 2>/dev/null | $SED -n -e "$lt_aix_libpath_sed"` - # Check for a 64-bit object if we didn't find anything. - if test -z "$lt_cv_aix_libpath_"; then - lt_cv_aix_libpath_=`dump -HX64 conftest$ac_exeext 2>/dev/null | $SED -n -e "$lt_aix_libpath_sed"` - fi -fi -rm -f core conftest.err conftest.$ac_objext \ - conftest$ac_exeext conftest.$ac_ext - if test -z "$lt_cv_aix_libpath_"; then - lt_cv_aix_libpath_=/usr/lib:/lib - fi - -fi - - aix_libpath=$lt_cv_aix_libpath_ -fi - - hardcode_libdir_flag_spec='$wl-blibpath:$libdir:'"$aix_libpath" - # Warning - without using the other run time loading flags, - # -berok will link without error, but may produce a broken library. - no_undefined_flag=' $wl-bernotok' - allow_undefined_flag=' $wl-berok' - if test yes = "$with_gnu_ld"; then - # We only use this code for GNU lds that support --whole-archive. - whole_archive_flag_spec='$wl--whole-archive$convenience $wl--no-whole-archive' - else - # Exported symbols can be pulled into shared objects from archives - whole_archive_flag_spec='$convenience' - fi - archive_cmds_need_lc=yes - archive_expsym_cmds='$RM -r $output_objdir/$realname.d~$MKDIR $output_objdir/$realname.d' - # -brtl affects multiple linker settings, -berok does not and is overridden later - compiler_flags_filtered='`func_echo_all "$compiler_flags " | $SED -e "s%-brtl\\([, ]\\)%-berok\\1%g"`' - if test svr4 != "$with_aix_soname"; then - # This is similar to how AIX traditionally builds its shared libraries. - archive_expsym_cmds="$archive_expsym_cmds"'~$CC '$shared_flag_aix' -o $output_objdir/$realname.d/$soname $libobjs $deplibs $wl-bnoentry '$compiler_flags_filtered'$wl-bE:$export_symbols$allow_undefined_flag~$AR $AR_FLAGS $output_objdir/$libname$release.a $output_objdir/$realname.d/$soname' - fi - if test aix != "$with_aix_soname"; then - archive_expsym_cmds="$archive_expsym_cmds"'~$CC '$shared_flag_svr4' -o $output_objdir/$realname.d/$shared_archive_member_spec.o $libobjs $deplibs $wl-bnoentry '$compiler_flags_filtered'$wl-bE:$export_symbols$allow_undefined_flag~$STRIP -e $output_objdir/$realname.d/$shared_archive_member_spec.o~( func_echo_all "#! $soname($shared_archive_member_spec.o)"; if test shr_64 = "$shared_archive_member_spec"; then func_echo_all "# 64"; else func_echo_all "# 32"; fi; cat $export_symbols ) > $output_objdir/$realname.d/$shared_archive_member_spec.imp~$AR $AR_FLAGS $output_objdir/$soname $output_objdir/$realname.d/$shared_archive_member_spec.o $output_objdir/$realname.d/$shared_archive_member_spec.imp' - else - # used by -dlpreopen to get the symbols - archive_expsym_cmds="$archive_expsym_cmds"'~$MV $output_objdir/$realname.d/$soname $output_objdir' - fi - archive_expsym_cmds="$archive_expsym_cmds"'~$RM -r $output_objdir/$realname.d' - fi - fi - ;; - - amigaos*) - case $host_cpu in - powerpc) - # see comment about AmigaOS4 .so support - archive_cmds='$CC -shared $libobjs $deplibs $compiler_flags $wl-soname $wl$soname -o $lib' - archive_expsym_cmds='' - ;; - m68k) - archive_cmds='$RM $output_objdir/a2ixlibrary.data~$ECHO "#define NAME $libname" > $output_objdir/a2ixlibrary.data~$ECHO "#define LIBRARY_ID 1" >> $output_objdir/a2ixlibrary.data~$ECHO "#define VERSION $major" >> $output_objdir/a2ixlibrary.data~$ECHO "#define REVISION $revision" >> $output_objdir/a2ixlibrary.data~$AR $AR_FLAGS $lib $libobjs~$RANLIB $lib~(cd $output_objdir && a2ixlibrary -32)' - hardcode_libdir_flag_spec='-L$libdir' - hardcode_minus_L=yes - ;; - esac - ;; - - bsdi[45]*) - export_dynamic_flag_spec=-rdynamic - ;; - - cygwin* | mingw* | pw32* | cegcc*) - # When not using gcc, we currently assume that we are using - # Microsoft Visual C++. - # hardcode_libdir_flag_spec is actually meaningless, as there is - # no search path for DLLs. - case $cc_basename in - cl*) - # Native MSVC - hardcode_libdir_flag_spec=' ' - allow_undefined_flag=unsupported - always_export_symbols=yes - file_list_spec='@' - # Tell ltmain to make .lib files, not .a files. - libext=lib - # Tell ltmain to make .dll files, not .so files. - shrext_cmds=.dll - # FIXME: Setting linknames here is a bad hack. - archive_cmds='$CC -o $output_objdir/$soname $libobjs $compiler_flags $deplibs -Wl,-DLL,-IMPLIB:"$tool_output_objdir$libname.dll.lib"~linknames=' - archive_expsym_cmds='if test DEF = "`$SED -n -e '\''s/^[ ]*//'\'' -e '\''/^\(;.*\)*$/d'\'' -e '\''s/^\(EXPORTS\|LIBRARY\)\([ ].*\)*$/DEF/p'\'' -e q $export_symbols`" ; then - cp "$export_symbols" "$output_objdir/$soname.def"; - echo "$tool_output_objdir$soname.def" > "$output_objdir/$soname.exp"; - else - $SED -e '\''s/^/-link -EXPORT:/'\'' < $export_symbols > $output_objdir/$soname.exp; - fi~ - $CC -o $tool_output_objdir$soname $libobjs $compiler_flags $deplibs "@$tool_output_objdir$soname.exp" -Wl,-DLL,-IMPLIB:"$tool_output_objdir$libname.dll.lib"~ - linknames=' - # The linker will not automatically build a static lib if we build a DLL. - # _LT_TAGVAR(old_archive_from_new_cmds, )='true' - enable_shared_with_static_runtimes=yes - exclude_expsyms='_NULL_IMPORT_DESCRIPTOR|_IMPORT_DESCRIPTOR_.*' - export_symbols_cmds='$NM $libobjs $convenience | $global_symbol_pipe | $SED -e '\''/^[BCDGRS][ ]/s/.*[ ]\([^ ]*\)/\1,DATA/'\'' | $SED -e '\''/^[AITW][ ]/s/.*[ ]//'\'' | sort | uniq > $export_symbols' - # Don't use ranlib - old_postinstall_cmds='chmod 644 $oldlib' - postlink_cmds='lt_outputfile="@OUTPUT@"~ - lt_tool_outputfile="@TOOL_OUTPUT@"~ - case $lt_outputfile in - *.exe|*.EXE) ;; - *) - lt_outputfile=$lt_outputfile.exe - lt_tool_outputfile=$lt_tool_outputfile.exe - ;; - esac~ - if test : != "$MANIFEST_TOOL" && test -f "$lt_outputfile.manifest"; then - $MANIFEST_TOOL -manifest "$lt_tool_outputfile.manifest" -outputresource:"$lt_tool_outputfile" || exit 1; - $RM "$lt_outputfile.manifest"; - fi' - ;; - *) - # Assume MSVC wrapper - hardcode_libdir_flag_spec=' ' - allow_undefined_flag=unsupported - # Tell ltmain to make .lib files, not .a files. - libext=lib - # Tell ltmain to make .dll files, not .so files. - shrext_cmds=.dll - # FIXME: Setting linknames here is a bad hack. - archive_cmds='$CC -o $lib $libobjs $compiler_flags `func_echo_all "$deplibs" | $SED '\''s/ -lc$//'\''` -link -dll~linknames=' - # The linker will automatically build a .lib file if we build a DLL. - old_archive_from_new_cmds='true' - # FIXME: Should let the user specify the lib program. - old_archive_cmds='lib -OUT:$oldlib$oldobjs$old_deplibs' - enable_shared_with_static_runtimes=yes - ;; - esac - ;; - - darwin* | rhapsody*) - - - archive_cmds_need_lc=no - hardcode_direct=no - hardcode_automatic=yes - hardcode_shlibpath_var=unsupported - if test yes = "$lt_cv_ld_force_load"; then - whole_archive_flag_spec='`for conv in $convenience\"\"; do test -n \"$conv\" && new_convenience=\"$new_convenience $wl-force_load,$conv\"; done; func_echo_all \"$new_convenience\"`' - - else - whole_archive_flag_spec='' - fi - link_all_deplibs=yes - allow_undefined_flag=$_lt_dar_allow_undefined - case $cc_basename in - ifort*|nagfor*) _lt_dar_can_shared=yes ;; - *) _lt_dar_can_shared=$GCC ;; - esac - if test yes = "$_lt_dar_can_shared"; then - output_verbose_link_cmd=func_echo_all - archive_cmds="\$CC -dynamiclib \$allow_undefined_flag -o \$lib \$libobjs \$deplibs \$compiler_flags -install_name \$rpath/\$soname \$verstring $_lt_dar_single_mod$_lt_dsymutil" - module_cmds="\$CC \$allow_undefined_flag -o \$lib -bundle \$libobjs \$deplibs \$compiler_flags$_lt_dsymutil" - archive_expsym_cmds="sed 's|^|_|' < \$export_symbols > \$output_objdir/\$libname-symbols.expsym~\$CC -dynamiclib \$allow_undefined_flag -o \$lib \$libobjs \$deplibs \$compiler_flags -install_name \$rpath/\$soname \$verstring $_lt_dar_single_mod$_lt_dar_export_syms$_lt_dsymutil" - module_expsym_cmds="sed -e 's|^|_|' < \$export_symbols > \$output_objdir/\$libname-symbols.expsym~\$CC \$allow_undefined_flag -o \$lib -bundle \$libobjs \$deplibs \$compiler_flags$_lt_dar_export_syms$_lt_dsymutil" - - else - ld_shlibs=no - fi - - ;; - - dgux*) - archive_cmds='$LD -G -h $soname -o $lib $libobjs $deplibs $linker_flags' - hardcode_libdir_flag_spec='-L$libdir' - hardcode_shlibpath_var=no - ;; - - # FreeBSD 2.2.[012] allows us to include c++rt0.o to get C++ constructor - # support. Future versions do this automatically, but an explicit c++rt0.o - # does not break anything, and helps significantly (at the cost of a little - # extra space). - freebsd2.2*) - archive_cmds='$LD -Bshareable -o $lib $libobjs $deplibs $linker_flags /usr/lib/c++rt0.o' - hardcode_libdir_flag_spec='-R$libdir' - hardcode_direct=yes - hardcode_shlibpath_var=no - ;; - - # Unfortunately, older versions of FreeBSD 2 do not have this feature. - freebsd2.*) - archive_cmds='$LD -Bshareable -o $lib $libobjs $deplibs $linker_flags' - hardcode_direct=yes - hardcode_minus_L=yes - hardcode_shlibpath_var=no - ;; - - # FreeBSD 3 and greater uses gcc -shared to do shared libraries. - freebsd* | dragonfly*) - archive_cmds='$CC -shared $pic_flag -o $lib $libobjs $deplibs $compiler_flags' - hardcode_libdir_flag_spec='-R$libdir' - hardcode_direct=yes - hardcode_shlibpath_var=no - ;; - - hpux9*) - if test yes = "$GCC"; then - archive_cmds='$RM $output_objdir/$soname~$CC -shared $pic_flag $wl+b $wl$install_libdir -o $output_objdir/$soname $libobjs $deplibs $compiler_flags~test "x$output_objdir/$soname" = "x$lib" || mv $output_objdir/$soname $lib' - else - archive_cmds='$RM $output_objdir/$soname~$LD -b +b $install_libdir -o $output_objdir/$soname $libobjs $deplibs $linker_flags~test "x$output_objdir/$soname" = "x$lib" || mv $output_objdir/$soname $lib' - fi - hardcode_libdir_flag_spec='$wl+b $wl$libdir' - hardcode_libdir_separator=: - hardcode_direct=yes - - # hardcode_minus_L: Not really in the search PATH, - # but as the default location of the library. - hardcode_minus_L=yes - export_dynamic_flag_spec='$wl-E' - ;; - - hpux10*) - if test yes,no = "$GCC,$with_gnu_ld"; then - archive_cmds='$CC -shared $pic_flag $wl+h $wl$soname $wl+b $wl$install_libdir -o $lib $libobjs $deplibs $compiler_flags' - else - archive_cmds='$LD -b +h $soname +b $install_libdir -o $lib $libobjs $deplibs $linker_flags' - fi - if test no = "$with_gnu_ld"; then - hardcode_libdir_flag_spec='$wl+b $wl$libdir' - hardcode_libdir_separator=: - hardcode_direct=yes - hardcode_direct_absolute=yes - export_dynamic_flag_spec='$wl-E' - # hardcode_minus_L: Not really in the search PATH, - # but as the default location of the library. - hardcode_minus_L=yes - fi - ;; - - hpux11*) - if test yes,no = "$GCC,$with_gnu_ld"; then - case $host_cpu in - hppa*64*) - archive_cmds='$CC -shared $wl+h $wl$soname -o $lib $libobjs $deplibs $compiler_flags' - ;; - ia64*) - archive_cmds='$CC -shared $pic_flag $wl+h $wl$soname $wl+nodefaultrpath -o $lib $libobjs $deplibs $compiler_flags' - ;; - *) - archive_cmds='$CC -shared $pic_flag $wl+h $wl$soname $wl+b $wl$install_libdir -o $lib $libobjs $deplibs $compiler_flags' - ;; - esac - else - case $host_cpu in - hppa*64*) - archive_cmds='$CC -b $wl+h $wl$soname -o $lib $libobjs $deplibs $compiler_flags' - ;; - ia64*) - archive_cmds='$CC -b $wl+h $wl$soname $wl+nodefaultrpath -o $lib $libobjs $deplibs $compiler_flags' - ;; - *) - - # Older versions of the 11.00 compiler do not understand -b yet - # (HP92453-01 A.11.01.20 doesn't, HP92453-01 B.11.X.35175-35176.GP does) - { $as_echo "$as_me:${as_lineno-$LINENO}: checking if $CC understands -b" >&5 -$as_echo_n "checking if $CC understands -b... " >&6; } -if ${lt_cv_prog_compiler__b+:} false; then : - $as_echo_n "(cached) " >&6 -else - lt_cv_prog_compiler__b=no - save_LDFLAGS=$LDFLAGS - LDFLAGS="$LDFLAGS -b" - echo "$lt_simple_link_test_code" > conftest.$ac_ext - if (eval $ac_link 2>conftest.err) && test -s conftest$ac_exeext; then - # The linker can only warn and ignore the option if not recognized - # So say no if there are warnings - if test -s conftest.err; then - # Append any errors to the config.log. - cat conftest.err 1>&5 - $ECHO "$_lt_linker_boilerplate" | $SED '/^$/d' > conftest.exp - $SED '/^$/d; /^ *+/d' conftest.err >conftest.er2 - if diff conftest.exp conftest.er2 >/dev/null; then - lt_cv_prog_compiler__b=yes - fi - else - lt_cv_prog_compiler__b=yes - fi - fi - $RM -r conftest* - LDFLAGS=$save_LDFLAGS - -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $lt_cv_prog_compiler__b" >&5 -$as_echo "$lt_cv_prog_compiler__b" >&6; } - -if test yes = "$lt_cv_prog_compiler__b"; then - archive_cmds='$CC -b $wl+h $wl$soname $wl+b $wl$install_libdir -o $lib $libobjs $deplibs $compiler_flags' -else - archive_cmds='$LD -b +h $soname +b $install_libdir -o $lib $libobjs $deplibs $linker_flags' -fi - - ;; - esac - fi - if test no = "$with_gnu_ld"; then - hardcode_libdir_flag_spec='$wl+b $wl$libdir' - hardcode_libdir_separator=: - - case $host_cpu in - hppa*64*|ia64*) - hardcode_direct=no - hardcode_shlibpath_var=no - ;; - *) - hardcode_direct=yes - hardcode_direct_absolute=yes - export_dynamic_flag_spec='$wl-E' - - # hardcode_minus_L: Not really in the search PATH, - # but as the default location of the library. - hardcode_minus_L=yes - ;; - esac - fi - ;; - - irix5* | irix6* | nonstopux*) - if test yes = "$GCC"; then - archive_cmds='$CC -shared $pic_flag $libobjs $deplibs $compiler_flags $wl-soname $wl$soname `test -n "$verstring" && func_echo_all "$wl-set_version $wl$verstring"` $wl-update_registry $wl$output_objdir/so_locations -o $lib' - # Try to use the -exported_symbol ld option, if it does not - # work, assume that -exports_file does not work either and - # implicitly export all symbols. - # This should be the same for all languages, so no per-tag cache variable. - { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether the $host_os linker accepts -exported_symbol" >&5 -$as_echo_n "checking whether the $host_os linker accepts -exported_symbol... " >&6; } -if ${lt_cv_irix_exported_symbol+:} false; then : - $as_echo_n "(cached) " >&6 -else - save_LDFLAGS=$LDFLAGS - LDFLAGS="$LDFLAGS -shared $wl-exported_symbol ${wl}foo $wl-update_registry $wl/dev/null" - cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ -int foo (void) { return 0; } -_ACEOF -if ac_fn_c_try_link "$LINENO"; then : - lt_cv_irix_exported_symbol=yes -else - lt_cv_irix_exported_symbol=no -fi -rm -f core conftest.err conftest.$ac_objext \ - conftest$ac_exeext conftest.$ac_ext - LDFLAGS=$save_LDFLAGS -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $lt_cv_irix_exported_symbol" >&5 -$as_echo "$lt_cv_irix_exported_symbol" >&6; } - if test yes = "$lt_cv_irix_exported_symbol"; then - archive_expsym_cmds='$CC -shared $pic_flag $libobjs $deplibs $compiler_flags $wl-soname $wl$soname `test -n "$verstring" && func_echo_all "$wl-set_version $wl$verstring"` $wl-update_registry $wl$output_objdir/so_locations $wl-exports_file $wl$export_symbols -o $lib' - fi - else - archive_cmds='$CC -shared $libobjs $deplibs $compiler_flags -soname $soname `test -n "$verstring" && func_echo_all "-set_version $verstring"` -update_registry $output_objdir/so_locations -o $lib' - archive_expsym_cmds='$CC -shared $libobjs $deplibs $compiler_flags -soname $soname `test -n "$verstring" && func_echo_all "-set_version $verstring"` -update_registry $output_objdir/so_locations -exports_file $export_symbols -o $lib' - fi - archive_cmds_need_lc='no' - hardcode_libdir_flag_spec='$wl-rpath $wl$libdir' - hardcode_libdir_separator=: - inherit_rpath=yes - link_all_deplibs=yes - ;; - - linux*) - case $cc_basename in - tcc*) - # Fabrice Bellard et al's Tiny C Compiler - ld_shlibs=yes - archive_cmds='$CC -shared $pic_flag -o $lib $libobjs $deplibs $compiler_flags' - ;; - esac - ;; - - netbsd*) - if echo __ELF__ | $CC -E - | $GREP __ELF__ >/dev/null; then - archive_cmds='$LD -Bshareable -o $lib $libobjs $deplibs $linker_flags' # a.out - else - archive_cmds='$LD -shared -o $lib $libobjs $deplibs $linker_flags' # ELF - fi - hardcode_libdir_flag_spec='-R$libdir' - hardcode_direct=yes - hardcode_shlibpath_var=no - ;; - - newsos6) - archive_cmds='$LD -G -h $soname -o $lib $libobjs $deplibs $linker_flags' - hardcode_direct=yes - hardcode_libdir_flag_spec='$wl-rpath $wl$libdir' - hardcode_libdir_separator=: - hardcode_shlibpath_var=no - ;; - - *nto* | *qnx*) - ;; - - openbsd* | bitrig*) - if test -f /usr/libexec/ld.so; then - hardcode_direct=yes - hardcode_shlibpath_var=no - hardcode_direct_absolute=yes - if test -z "`echo __ELF__ | $CC -E - | $GREP __ELF__`"; then - archive_cmds='$CC -shared $pic_flag -o $lib $libobjs $deplibs $compiler_flags' - archive_expsym_cmds='$CC -shared $pic_flag -o $lib $libobjs $deplibs $compiler_flags $wl-retain-symbols-file,$export_symbols' - hardcode_libdir_flag_spec='$wl-rpath,$libdir' - export_dynamic_flag_spec='$wl-E' - else - archive_cmds='$CC -shared $pic_flag -o $lib $libobjs $deplibs $compiler_flags' - hardcode_libdir_flag_spec='$wl-rpath,$libdir' - fi - else - ld_shlibs=no - fi - ;; - - os2*) - hardcode_libdir_flag_spec='-L$libdir' - hardcode_minus_L=yes - allow_undefined_flag=unsupported - shrext_cmds=.dll - archive_cmds='$ECHO "LIBRARY ${soname%$shared_ext} INITINSTANCE TERMINSTANCE" > $output_objdir/$libname.def~ - $ECHO "DESCRIPTION \"$libname\"" >> $output_objdir/$libname.def~ - $ECHO "DATA MULTIPLE NONSHARED" >> $output_objdir/$libname.def~ - $ECHO EXPORTS >> $output_objdir/$libname.def~ - emxexp $libobjs | $SED /"_DLL_InitTerm"/d >> $output_objdir/$libname.def~ - $CC -Zdll -Zcrtdll -o $output_objdir/$soname $libobjs $deplibs $compiler_flags $output_objdir/$libname.def~ - emximp -o $lib $output_objdir/$libname.def' - archive_expsym_cmds='$ECHO "LIBRARY ${soname%$shared_ext} INITINSTANCE TERMINSTANCE" > $output_objdir/$libname.def~ - $ECHO "DESCRIPTION \"$libname\"" >> $output_objdir/$libname.def~ - $ECHO "DATA MULTIPLE NONSHARED" >> $output_objdir/$libname.def~ - $ECHO EXPORTS >> $output_objdir/$libname.def~ - prefix_cmds="$SED"~ - if test EXPORTS = "`$SED 1q $export_symbols`"; then - prefix_cmds="$prefix_cmds -e 1d"; - fi~ - prefix_cmds="$prefix_cmds -e \"s/^\(.*\)$/_\1/g\""~ - cat $export_symbols | $prefix_cmds >> $output_objdir/$libname.def~ - $CC -Zdll -Zcrtdll -o $output_objdir/$soname $libobjs $deplibs $compiler_flags $output_objdir/$libname.def~ - emximp -o $lib $output_objdir/$libname.def' - old_archive_From_new_cmds='emximp -o $output_objdir/${libname}_dll.a $output_objdir/$libname.def' - enable_shared_with_static_runtimes=yes - ;; - - osf3*) - if test yes = "$GCC"; then - allow_undefined_flag=' $wl-expect_unresolved $wl\*' - archive_cmds='$CC -shared$allow_undefined_flag $libobjs $deplibs $compiler_flags $wl-soname $wl$soname `test -n "$verstring" && func_echo_all "$wl-set_version $wl$verstring"` $wl-update_registry $wl$output_objdir/so_locations -o $lib' - else - allow_undefined_flag=' -expect_unresolved \*' - archive_cmds='$CC -shared$allow_undefined_flag $libobjs $deplibs $compiler_flags -soname $soname `test -n "$verstring" && func_echo_all "-set_version $verstring"` -update_registry $output_objdir/so_locations -o $lib' - fi - archive_cmds_need_lc='no' - hardcode_libdir_flag_spec='$wl-rpath $wl$libdir' - hardcode_libdir_separator=: - ;; - - osf4* | osf5*) # as osf3* with the addition of -msym flag - if test yes = "$GCC"; then - allow_undefined_flag=' $wl-expect_unresolved $wl\*' - archive_cmds='$CC -shared$allow_undefined_flag $pic_flag $libobjs $deplibs $compiler_flags $wl-msym $wl-soname $wl$soname `test -n "$verstring" && func_echo_all "$wl-set_version $wl$verstring"` $wl-update_registry $wl$output_objdir/so_locations -o $lib' - hardcode_libdir_flag_spec='$wl-rpath $wl$libdir' - else - allow_undefined_flag=' -expect_unresolved \*' - archive_cmds='$CC -shared$allow_undefined_flag $libobjs $deplibs $compiler_flags -msym -soname $soname `test -n "$verstring" && func_echo_all "-set_version $verstring"` -update_registry $output_objdir/so_locations -o $lib' - archive_expsym_cmds='for i in `cat $export_symbols`; do printf "%s %s\\n" -exported_symbol "\$i" >> $lib.exp; done; printf "%s\\n" "-hidden">> $lib.exp~ - $CC -shared$allow_undefined_flag $wl-input $wl$lib.exp $compiler_flags $libobjs $deplibs -soname $soname `test -n "$verstring" && $ECHO "-set_version $verstring"` -update_registry $output_objdir/so_locations -o $lib~$RM $lib.exp' - - # Both c and cxx compiler support -rpath directly - hardcode_libdir_flag_spec='-rpath $libdir' - fi - archive_cmds_need_lc='no' - hardcode_libdir_separator=: - ;; - - solaris*) - no_undefined_flag=' -z defs' - if test yes = "$GCC"; then - wlarc='$wl' - archive_cmds='$CC -shared $pic_flag $wl-z ${wl}text $wl-h $wl$soname -o $lib $libobjs $deplibs $compiler_flags' - archive_expsym_cmds='echo "{ global:" > $lib.exp~cat $export_symbols | $SED -e "s/\(.*\)/\1;/" >> $lib.exp~echo "local: *; };" >> $lib.exp~ - $CC -shared $pic_flag $wl-z ${wl}text $wl-M $wl$lib.exp $wl-h $wl$soname -o $lib $libobjs $deplibs $compiler_flags~$RM $lib.exp' - else - case `$CC -V 2>&1` in - *"Compilers 5.0"*) - wlarc='' - archive_cmds='$LD -G$allow_undefined_flag -h $soname -o $lib $libobjs $deplibs $linker_flags' - archive_expsym_cmds='echo "{ global:" > $lib.exp~cat $export_symbols | $SED -e "s/\(.*\)/\1;/" >> $lib.exp~echo "local: *; };" >> $lib.exp~ - $LD -G$allow_undefined_flag -M $lib.exp -h $soname -o $lib $libobjs $deplibs $linker_flags~$RM $lib.exp' - ;; - *) - wlarc='$wl' - archive_cmds='$CC -G$allow_undefined_flag -h $soname -o $lib $libobjs $deplibs $compiler_flags' - archive_expsym_cmds='echo "{ global:" > $lib.exp~cat $export_symbols | $SED -e "s/\(.*\)/\1;/" >> $lib.exp~echo "local: *; };" >> $lib.exp~ - $CC -G$allow_undefined_flag -M $lib.exp -h $soname -o $lib $libobjs $deplibs $compiler_flags~$RM $lib.exp' - ;; - esac - fi - hardcode_libdir_flag_spec='-R$libdir' - hardcode_shlibpath_var=no - case $host_os in - solaris2.[0-5] | solaris2.[0-5].*) ;; - *) - # The compiler driver will combine and reorder linker options, - # but understands '-z linker_flag'. GCC discards it without '$wl', - # but is careful enough not to reorder. - # Supported since Solaris 2.6 (maybe 2.5.1?) - if test yes = "$GCC"; then - whole_archive_flag_spec='$wl-z ${wl}allextract$convenience $wl-z ${wl}defaultextract' - else - whole_archive_flag_spec='-z allextract$convenience -z defaultextract' - fi - ;; - esac - link_all_deplibs=yes - ;; - - sunos4*) - if test sequent = "$host_vendor"; then - # Use $CC to link under sequent, because it throws in some extra .o - # files that make .init and .fini sections work. - archive_cmds='$CC -G $wl-h $soname -o $lib $libobjs $deplibs $compiler_flags' - else - archive_cmds='$LD -assert pure-text -Bstatic -o $lib $libobjs $deplibs $linker_flags' - fi - hardcode_libdir_flag_spec='-L$libdir' - hardcode_direct=yes - hardcode_minus_L=yes - hardcode_shlibpath_var=no - ;; - - sysv4) - case $host_vendor in - sni) - archive_cmds='$LD -G -h $soname -o $lib $libobjs $deplibs $linker_flags' - hardcode_direct=yes # is this really true??? - ;; - siemens) - ## LD is ld it makes a PLAMLIB - ## CC just makes a GrossModule. - archive_cmds='$LD -G -o $lib $libobjs $deplibs $linker_flags' - reload_cmds='$CC -r -o $output$reload_objs' - hardcode_direct=no - ;; - motorola) - archive_cmds='$LD -G -h $soname -o $lib $libobjs $deplibs $linker_flags' - hardcode_direct=no #Motorola manual says yes, but my tests say they lie - ;; - esac - runpath_var='LD_RUN_PATH' - hardcode_shlibpath_var=no - ;; - - sysv4.3*) - archive_cmds='$LD -G -h $soname -o $lib $libobjs $deplibs $linker_flags' - hardcode_shlibpath_var=no - export_dynamic_flag_spec='-Bexport' - ;; - - sysv4*MP*) - if test -d /usr/nec; then - archive_cmds='$LD -G -h $soname -o $lib $libobjs $deplibs $linker_flags' - hardcode_shlibpath_var=no - runpath_var=LD_RUN_PATH - hardcode_runpath_var=yes - ld_shlibs=yes - fi - ;; - - sysv4*uw2* | sysv5OpenUNIX* | sysv5UnixWare7.[01].[10]* | unixware7* | sco3.2v5.0.[024]*) - no_undefined_flag='$wl-z,text' - archive_cmds_need_lc=no - hardcode_shlibpath_var=no - runpath_var='LD_RUN_PATH' - - if test yes = "$GCC"; then - archive_cmds='$CC -shared $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags' - archive_expsym_cmds='$CC -shared $wl-Bexport:$export_symbols $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags' - else - archive_cmds='$CC -G $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags' - archive_expsym_cmds='$CC -G $wl-Bexport:$export_symbols $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags' - fi - ;; - - sysv5* | sco3.2v5* | sco5v6*) - # Note: We CANNOT use -z defs as we might desire, because we do not - # link with -lc, and that would cause any symbols used from libc to - # always be unresolved, which means just about no library would - # ever link correctly. If we're not using GNU ld we use -z text - # though, which does catch some bad symbols but isn't as heavy-handed - # as -z defs. - no_undefined_flag='$wl-z,text' - allow_undefined_flag='$wl-z,nodefs' - archive_cmds_need_lc=no - hardcode_shlibpath_var=no - hardcode_libdir_flag_spec='$wl-R,$libdir' - hardcode_libdir_separator=':' - link_all_deplibs=yes - export_dynamic_flag_spec='$wl-Bexport' - runpath_var='LD_RUN_PATH' - - if test yes = "$GCC"; then - archive_cmds='$CC -shared $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags' - archive_expsym_cmds='$CC -shared $wl-Bexport:$export_symbols $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags' - else - archive_cmds='$CC -G $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags' - archive_expsym_cmds='$CC -G $wl-Bexport:$export_symbols $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags' - fi - ;; - - uts4*) - archive_cmds='$LD -G -h $soname -o $lib $libobjs $deplibs $linker_flags' - hardcode_libdir_flag_spec='-L$libdir' - hardcode_shlibpath_var=no - ;; - - *) - ld_shlibs=no - ;; - esac - - if test sni = "$host_vendor"; then - case $host in - sysv4 | sysv4.2uw2* | sysv4.3* | sysv5*) - export_dynamic_flag_spec='$wl-Blargedynsym' - ;; - esac - fi - fi - -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ld_shlibs" >&5 -$as_echo "$ld_shlibs" >&6; } -test no = "$ld_shlibs" && can_build_shared=no - -with_gnu_ld=$with_gnu_ld - - - - - - - - - - - - - - - -# -# Do we need to explicitly link libc? -# -case "x$archive_cmds_need_lc" in -x|xyes) - # Assume -lc should be added - archive_cmds_need_lc=yes - - if test yes,yes = "$GCC,$enable_shared"; then - case $archive_cmds in - *'~'*) - # FIXME: we may have to deal with multi-command sequences. - ;; - '$CC '*) - # Test whether the compiler implicitly links with -lc since on some - # systems, -lgcc has to come before -lc. If gcc already passes -lc - # to ld, don't add -lc before -lgcc. - { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether -lc should be explicitly linked in" >&5 -$as_echo_n "checking whether -lc should be explicitly linked in... " >&6; } -if ${lt_cv_archive_cmds_need_lc+:} false; then : - $as_echo_n "(cached) " >&6 -else - $RM conftest* - echo "$lt_simple_compile_test_code" > conftest.$ac_ext - - if { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_compile\""; } >&5 - (eval $ac_compile) 2>&5 - ac_status=$? - $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5 - test $ac_status = 0; } 2>conftest.err; then - soname=conftest - lib=conftest - libobjs=conftest.$ac_objext - deplibs= - wl=$lt_prog_compiler_wl - pic_flag=$lt_prog_compiler_pic - compiler_flags=-v - linker_flags=-v - verstring= - output_objdir=. - libname=conftest - lt_save_allow_undefined_flag=$allow_undefined_flag - allow_undefined_flag= - if { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$archive_cmds 2\>\&1 \| $GREP \" -lc \" \>/dev/null 2\>\&1\""; } >&5 - (eval $archive_cmds 2\>\&1 \| $GREP \" -lc \" \>/dev/null 2\>\&1) 2>&5 - ac_status=$? - $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5 - test $ac_status = 0; } - then - lt_cv_archive_cmds_need_lc=no - else - lt_cv_archive_cmds_need_lc=yes - fi - allow_undefined_flag=$lt_save_allow_undefined_flag - else - cat conftest.err 1>&5 - fi - $RM conftest* - -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $lt_cv_archive_cmds_need_lc" >&5 -$as_echo "$lt_cv_archive_cmds_need_lc" >&6; } - archive_cmds_need_lc=$lt_cv_archive_cmds_need_lc - ;; - esac - fi - ;; -esac - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - { $as_echo "$as_me:${as_lineno-$LINENO}: checking dynamic linker characteristics" >&5 -$as_echo_n "checking dynamic linker characteristics... " >&6; } - -if test yes = "$GCC"; then - case $host_os in - darwin*) lt_awk_arg='/^libraries:/,/LR/' ;; - *) lt_awk_arg='/^libraries:/' ;; - esac - case $host_os in - mingw* | cegcc*) lt_sed_strip_eq='s|=\([A-Za-z]:\)|\1|g' ;; - *) lt_sed_strip_eq='s|=/|/|g' ;; - esac - lt_search_path_spec=`$CC -print-search-dirs | awk $lt_awk_arg | $SED -e "s/^libraries://" -e $lt_sed_strip_eq` - case $lt_search_path_spec in - *\;*) - # if the path contains ";" then we assume it to be the separator - # otherwise default to the standard path separator (i.e. ":") - it is - # assumed that no part of a normal pathname contains ";" but that should - # okay in the real world where ";" in dirpaths is itself problematic. - lt_search_path_spec=`$ECHO "$lt_search_path_spec" | $SED 's/;/ /g'` - ;; - *) - lt_search_path_spec=`$ECHO "$lt_search_path_spec" | $SED "s/$PATH_SEPARATOR/ /g"` - ;; - esac - # Ok, now we have the path, separated by spaces, we can step through it - # and add multilib dir if necessary... - lt_tmp_lt_search_path_spec= - lt_multi_os_dir=/`$CC $CPPFLAGS $CFLAGS $LDFLAGS -print-multi-os-directory 2>/dev/null` - # ...but if some path component already ends with the multilib dir we assume - # that all is fine and trust -print-search-dirs as is (GCC 4.2? or newer). - case "$lt_multi_os_dir; $lt_search_path_spec " in - "/; "* | "/.; "* | "/./; "* | *"$lt_multi_os_dir "* | *"$lt_multi_os_dir/ "*) - lt_multi_os_dir= - ;; - esac - for lt_sys_path in $lt_search_path_spec; do - if test -d "$lt_sys_path$lt_multi_os_dir"; then - lt_tmp_lt_search_path_spec="$lt_tmp_lt_search_path_spec $lt_sys_path$lt_multi_os_dir" - elif test -n "$lt_multi_os_dir"; then - test -d "$lt_sys_path" && \ - lt_tmp_lt_search_path_spec="$lt_tmp_lt_search_path_spec $lt_sys_path" - fi - done - lt_search_path_spec=`$ECHO "$lt_tmp_lt_search_path_spec" | awk ' -BEGIN {RS = " "; FS = "/|\n";} { - lt_foo = ""; - lt_count = 0; - for (lt_i = NF; lt_i > 0; lt_i--) { - if ($lt_i != "" && $lt_i != ".") { - if ($lt_i == "..") { - lt_count++; - } else { - if (lt_count == 0) { - lt_foo = "/" $lt_i lt_foo; - } else { - lt_count--; - } - } - } - } - if (lt_foo != "") { lt_freq[lt_foo]++; } - if (lt_freq[lt_foo] == 1) { print lt_foo; } -}'` - # AWK program above erroneously prepends '/' to C:/dos/paths - # for these hosts. - case $host_os in - mingw* | cegcc*) lt_search_path_spec=`$ECHO "$lt_search_path_spec" |\ - $SED 's|/\([A-Za-z]:\)|\1|g'` ;; - esac - sys_lib_search_path_spec=`$ECHO "$lt_search_path_spec" | $lt_NL2SP` -else - sys_lib_search_path_spec="/lib /usr/lib /usr/local/lib" -fi -library_names_spec= -libname_spec='lib$name' -soname_spec= -shrext_cmds=.so -postinstall_cmds= -postuninstall_cmds= -finish_cmds= -finish_eval= -shlibpath_var= -shlibpath_overrides_runpath=unknown -version_type=none -dynamic_linker="$host_os ld.so" -sys_lib_dlsearch_path_spec="/lib /usr/lib" -need_lib_prefix=unknown -hardcode_into_libs=no - -# when you set need_version to no, make sure it does not cause -set_version -# flags to be left without arguments -need_version=unknown - - - -case $host_os in -aix3*) - version_type=linux # correct to gnu/linux during the next big refactor - library_names_spec='$libname$release$shared_ext$versuffix $libname.a' - shlibpath_var=LIBPATH - - # AIX 3 has no versioning support, so we append a major version to the name. - soname_spec='$libname$release$shared_ext$major' - ;; - -aix[4-9]*) - version_type=linux # correct to gnu/linux during the next big refactor - need_lib_prefix=no - need_version=no - hardcode_into_libs=yes - if test ia64 = "$host_cpu"; then - # AIX 5 supports IA64 - library_names_spec='$libname$release$shared_ext$major $libname$release$shared_ext$versuffix $libname$shared_ext' - shlibpath_var=LD_LIBRARY_PATH - else - # With GCC up to 2.95.x, collect2 would create an import file - # for dependence libraries. The import file would start with - # the line '#! .'. This would cause the generated library to - # depend on '.', always an invalid library. This was fixed in - # development snapshots of GCC prior to 3.0. - case $host_os in - aix4 | aix4.[01] | aix4.[01].*) - if { echo '#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 97)' - echo ' yes ' - echo '#endif'; } | $CC -E - | $GREP yes > /dev/null; then - : - else - can_build_shared=no - fi - ;; - esac - # Using Import Files as archive members, it is possible to support - # filename-based versioning of shared library archives on AIX. While - # this would work for both with and without runtime linking, it will - # prevent static linking of such archives. So we do filename-based - # shared library versioning with .so extension only, which is used - # when both runtime linking and shared linking is enabled. - # Unfortunately, runtime linking may impact performance, so we do - # not want this to be the default eventually. Also, we use the - # versioned .so libs for executables only if there is the -brtl - # linker flag in LDFLAGS as well, or --with-aix-soname=svr4 only. - # To allow for filename-based versioning support, we need to create - # libNAME.so.V as an archive file, containing: - # *) an Import File, referring to the versioned filename of the - # archive as well as the shared archive member, telling the - # bitwidth (32 or 64) of that shared object, and providing the - # list of exported symbols of that shared object, eventually - # decorated with the 'weak' keyword - # *) the shared object with the F_LOADONLY flag set, to really avoid - # it being seen by the linker. - # At run time we better use the real file rather than another symlink, - # but for link time we create the symlink libNAME.so -> libNAME.so.V - - case $with_aix_soname,$aix_use_runtimelinking in - # AIX (on Power*) has no versioning support, so currently we cannot hardcode correct - # soname into executable. Probably we can add versioning support to - # collect2, so additional links can be useful in future. - aix,yes) # traditional libtool - dynamic_linker='AIX unversionable lib.so' - # If using run time linking (on AIX 4.2 or later) use lib.so - # instead of lib.a to let people know that these are not - # typical AIX shared libraries. - library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' - ;; - aix,no) # traditional AIX only - dynamic_linker='AIX lib.a(lib.so.V)' - # We preserve .a as extension for shared libraries through AIX4.2 - # and later when we are not doing run time linking. - library_names_spec='$libname$release.a $libname.a' - soname_spec='$libname$release$shared_ext$major' - ;; - svr4,*) # full svr4 only - dynamic_linker="AIX lib.so.V($shared_archive_member_spec.o)" - library_names_spec='$libname$release$shared_ext$major $libname$shared_ext' - # We do not specify a path in Import Files, so LIBPATH fires. - shlibpath_overrides_runpath=yes - ;; - *,yes) # both, prefer svr4 - dynamic_linker="AIX lib.so.V($shared_archive_member_spec.o), lib.a(lib.so.V)" - library_names_spec='$libname$release$shared_ext$major $libname$shared_ext' - # unpreferred sharedlib libNAME.a needs extra handling - postinstall_cmds='test -n "$linkname" || linkname="$realname"~func_stripname "" ".so" "$linkname"~$install_shared_prog "$dir/$func_stripname_result.$libext" "$destdir/$func_stripname_result.$libext"~test -z "$tstripme" || test -z "$striplib" || $striplib "$destdir/$func_stripname_result.$libext"' - postuninstall_cmds='for n in $library_names $old_library; do :; done~func_stripname "" ".so" "$n"~test "$func_stripname_result" = "$n" || func_append rmfiles " $odir/$func_stripname_result.$libext"' - # We do not specify a path in Import Files, so LIBPATH fires. - shlibpath_overrides_runpath=yes - ;; - *,no) # both, prefer aix - dynamic_linker="AIX lib.a(lib.so.V), lib.so.V($shared_archive_member_spec.o)" - library_names_spec='$libname$release.a $libname.a' - soname_spec='$libname$release$shared_ext$major' - # unpreferred sharedlib libNAME.so.V and symlink libNAME.so need extra handling - postinstall_cmds='test -z "$dlname" || $install_shared_prog $dir/$dlname $destdir/$dlname~test -z "$tstripme" || test -z "$striplib" || $striplib $destdir/$dlname~test -n "$linkname" || linkname=$realname~func_stripname "" ".a" "$linkname"~(cd "$destdir" && $LN_S -f $dlname $func_stripname_result.so)' - postuninstall_cmds='test -z "$dlname" || func_append rmfiles " $odir/$dlname"~for n in $old_library $library_names; do :; done~func_stripname "" ".a" "$n"~func_append rmfiles " $odir/$func_stripname_result.so"' - ;; - esac - shlibpath_var=LIBPATH - fi - ;; - -amigaos*) - case $host_cpu in - powerpc) - # Since July 2007 AmigaOS4 officially supports .so libraries. - # When compiling the executable, add -use-dynld -Lsobjs: to the compileline. - library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' - ;; - m68k) - library_names_spec='$libname.ixlibrary $libname.a' - # Create ${libname}_ixlibrary.a entries in /sys/libs. - finish_eval='for lib in `ls $libdir/*.ixlibrary 2>/dev/null`; do libname=`func_echo_all "$lib" | $SED '\''s%^.*/\([^/]*\)\.ixlibrary$%\1%'\''`; $RM /sys/libs/${libname}_ixlibrary.a; $show "cd /sys/libs && $LN_S $lib ${libname}_ixlibrary.a"; cd /sys/libs && $LN_S $lib ${libname}_ixlibrary.a || exit 1; done' - ;; - esac - ;; - -beos*) - library_names_spec='$libname$shared_ext' - dynamic_linker="$host_os ld.so" - shlibpath_var=LIBRARY_PATH - ;; - -bsdi[45]*) - version_type=linux # correct to gnu/linux during the next big refactor - need_version=no - library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' - soname_spec='$libname$release$shared_ext$major' - finish_cmds='PATH="\$PATH:/sbin" ldconfig $libdir' - shlibpath_var=LD_LIBRARY_PATH - sys_lib_search_path_spec="/shlib /usr/lib /usr/X11/lib /usr/contrib/lib /lib /usr/local/lib" - sys_lib_dlsearch_path_spec="/shlib /usr/lib /usr/local/lib" - # the default ld.so.conf also contains /usr/contrib/lib and - # /usr/X11R6/lib (/usr/X11 is a link to /usr/X11R6), but let us allow - # libtool to hard-code these into programs - ;; - -cygwin* | mingw* | pw32* | cegcc*) - version_type=windows - shrext_cmds=.dll - need_version=no - need_lib_prefix=no - - case $GCC,$cc_basename in - yes,*) - # gcc - library_names_spec='$libname.dll.a' - # DLL is installed to $(libdir)/../bin by postinstall_cmds - postinstall_cmds='base_file=`basename \$file`~ - dlpath=`$SHELL 2>&1 -c '\''. $dir/'\''\$base_file'\''i; echo \$dlname'\''`~ - dldir=$destdir/`dirname \$dlpath`~ - test -d \$dldir || mkdir -p \$dldir~ - $install_prog $dir/$dlname \$dldir/$dlname~ - chmod a+x \$dldir/$dlname~ - if test -n '\''$stripme'\'' && test -n '\''$striplib'\''; then - eval '\''$striplib \$dldir/$dlname'\'' || exit \$?; - fi' - postuninstall_cmds='dldll=`$SHELL 2>&1 -c '\''. $file; echo \$dlname'\''`~ - dlpath=$dir/\$dldll~ - $RM \$dlpath' - shlibpath_overrides_runpath=yes - - case $host_os in - cygwin*) - # Cygwin DLLs use 'cyg' prefix rather than 'lib' - soname_spec='`echo $libname | sed -e 's/^lib/cyg/'``echo $release | $SED -e 's/[.]/-/g'`$versuffix$shared_ext' - - sys_lib_search_path_spec="$sys_lib_search_path_spec /usr/lib/w32api" - ;; - mingw* | cegcc*) - # MinGW DLLs use traditional 'lib' prefix - soname_spec='$libname`echo $release | $SED -e 's/[.]/-/g'`$versuffix$shared_ext' - ;; - pw32*) - # pw32 DLLs use 'pw' prefix rather than 'lib' - library_names_spec='`echo $libname | sed -e 's/^lib/pw/'``echo $release | $SED -e 's/[.]/-/g'`$versuffix$shared_ext' - ;; - esac - dynamic_linker='Win32 ld.exe' - ;; - - *,cl*) - # Native MSVC - libname_spec='$name' - soname_spec='$libname`echo $release | $SED -e 's/[.]/-/g'`$versuffix$shared_ext' - library_names_spec='$libname.dll.lib' - - case $build_os in - mingw*) - sys_lib_search_path_spec= - lt_save_ifs=$IFS - IFS=';' - for lt_path in $LIB - do - IFS=$lt_save_ifs - # Let DOS variable expansion print the short 8.3 style file name. - lt_path=`cd "$lt_path" 2>/dev/null && cmd //C "for %i in (".") do @echo %~si"` - sys_lib_search_path_spec="$sys_lib_search_path_spec $lt_path" - done - IFS=$lt_save_ifs - # Convert to MSYS style. - sys_lib_search_path_spec=`$ECHO "$sys_lib_search_path_spec" | sed -e 's|\\\\|/|g' -e 's| \\([a-zA-Z]\\):| /\\1|g' -e 's|^ ||'` - ;; - cygwin*) - # Convert to unix form, then to dos form, then back to unix form - # but this time dos style (no spaces!) so that the unix form looks - # like /cygdrive/c/PROGRA~1:/cygdr... - sys_lib_search_path_spec=`cygpath --path --unix "$LIB"` - sys_lib_search_path_spec=`cygpath --path --dos "$sys_lib_search_path_spec" 2>/dev/null` - sys_lib_search_path_spec=`cygpath --path --unix "$sys_lib_search_path_spec" | $SED -e "s/$PATH_SEPARATOR/ /g"` - ;; - *) - sys_lib_search_path_spec=$LIB - if $ECHO "$sys_lib_search_path_spec" | $GREP ';[c-zC-Z]:/' >/dev/null; then - # It is most probably a Windows format PATH. - sys_lib_search_path_spec=`$ECHO "$sys_lib_search_path_spec" | $SED -e 's/;/ /g'` - else - sys_lib_search_path_spec=`$ECHO "$sys_lib_search_path_spec" | $SED -e "s/$PATH_SEPARATOR/ /g"` - fi - # FIXME: find the short name or the path components, as spaces are - # common. (e.g. "Program Files" -> "PROGRA~1") - ;; - esac - - # DLL is installed to $(libdir)/../bin by postinstall_cmds - postinstall_cmds='base_file=`basename \$file`~ - dlpath=`$SHELL 2>&1 -c '\''. $dir/'\''\$base_file'\''i; echo \$dlname'\''`~ - dldir=$destdir/`dirname \$dlpath`~ - test -d \$dldir || mkdir -p \$dldir~ - $install_prog $dir/$dlname \$dldir/$dlname' - postuninstall_cmds='dldll=`$SHELL 2>&1 -c '\''. $file; echo \$dlname'\''`~ - dlpath=$dir/\$dldll~ - $RM \$dlpath' - shlibpath_overrides_runpath=yes - dynamic_linker='Win32 link.exe' - ;; - - *) - # Assume MSVC wrapper - library_names_spec='$libname`echo $release | $SED -e 's/[.]/-/g'`$versuffix$shared_ext $libname.lib' - dynamic_linker='Win32 ld.exe' - ;; - esac - # FIXME: first we should search . and the directory the executable is in - shlibpath_var=PATH - ;; - -darwin* | rhapsody*) - dynamic_linker="$host_os dyld" - version_type=darwin - need_lib_prefix=no - need_version=no - library_names_spec='$libname$release$major$shared_ext $libname$shared_ext' - soname_spec='$libname$release$major$shared_ext' - shlibpath_overrides_runpath=yes - shlibpath_var=DYLD_LIBRARY_PATH - shrext_cmds='`test .$module = .yes && echo .so || echo .dylib`' - - sys_lib_search_path_spec="$sys_lib_search_path_spec /usr/local/lib" - sys_lib_dlsearch_path_spec='/usr/local/lib /lib /usr/lib' - ;; - -dgux*) - version_type=linux # correct to gnu/linux during the next big refactor - need_lib_prefix=no - need_version=no - library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' - soname_spec='$libname$release$shared_ext$major' - shlibpath_var=LD_LIBRARY_PATH - ;; - -freebsd* | dragonfly*) - # DragonFly does not have aout. When/if they implement a new - # versioning mechanism, adjust this. - if test -x /usr/bin/objformat; then - objformat=`/usr/bin/objformat` - else - case $host_os in - freebsd[23].*) objformat=aout ;; - *) objformat=elf ;; - esac - fi - version_type=freebsd-$objformat - case $version_type in - freebsd-elf*) - library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' - soname_spec='$libname$release$shared_ext$major' - need_version=no - need_lib_prefix=no - ;; - freebsd-*) - library_names_spec='$libname$release$shared_ext$versuffix $libname$shared_ext$versuffix' - need_version=yes - ;; - esac - shlibpath_var=LD_LIBRARY_PATH - case $host_os in - freebsd2.*) - shlibpath_overrides_runpath=yes - ;; - freebsd3.[01]* | freebsdelf3.[01]*) - shlibpath_overrides_runpath=yes - hardcode_into_libs=yes - ;; - freebsd3.[2-9]* | freebsdelf3.[2-9]* | \ - freebsd4.[0-5] | freebsdelf4.[0-5] | freebsd4.1.1 | freebsdelf4.1.1) - shlibpath_overrides_runpath=no - hardcode_into_libs=yes - ;; - *) # from 4.6 on, and DragonFly - shlibpath_overrides_runpath=yes - hardcode_into_libs=yes - ;; - esac - ;; - -haiku*) - version_type=linux # correct to gnu/linux during the next big refactor - need_lib_prefix=no - need_version=no - dynamic_linker="$host_os runtime_loader" - library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' - soname_spec='$libname$release$shared_ext$major' - shlibpath_var=LIBRARY_PATH - shlibpath_overrides_runpath=no - sys_lib_dlsearch_path_spec='/boot/home/config/lib /boot/common/lib /boot/system/lib' - hardcode_into_libs=yes - ;; - -hpux9* | hpux10* | hpux11*) - # Give a soname corresponding to the major version so that dld.sl refuses to - # link against other versions. - version_type=sunos - need_lib_prefix=no - need_version=no - case $host_cpu in - ia64*) - shrext_cmds='.so' - hardcode_into_libs=yes - dynamic_linker="$host_os dld.so" - shlibpath_var=LD_LIBRARY_PATH - shlibpath_overrides_runpath=yes # Unless +noenvvar is specified. - library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' - soname_spec='$libname$release$shared_ext$major' - if test 32 = "$HPUX_IA64_MODE"; then - sys_lib_search_path_spec="/usr/lib/hpux32 /usr/local/lib/hpux32 /usr/local/lib" - sys_lib_dlsearch_path_spec=/usr/lib/hpux32 - else - sys_lib_search_path_spec="/usr/lib/hpux64 /usr/local/lib/hpux64" - sys_lib_dlsearch_path_spec=/usr/lib/hpux64 - fi - ;; - hppa*64*) - shrext_cmds='.sl' - hardcode_into_libs=yes - dynamic_linker="$host_os dld.sl" - shlibpath_var=LD_LIBRARY_PATH # How should we handle SHLIB_PATH - shlibpath_overrides_runpath=yes # Unless +noenvvar is specified. - library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' - soname_spec='$libname$release$shared_ext$major' - sys_lib_search_path_spec="/usr/lib/pa20_64 /usr/ccs/lib/pa20_64" - sys_lib_dlsearch_path_spec=$sys_lib_search_path_spec - ;; - *) - shrext_cmds='.sl' - dynamic_linker="$host_os dld.sl" - shlibpath_var=SHLIB_PATH - shlibpath_overrides_runpath=no # +s is required to enable SHLIB_PATH - library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' - soname_spec='$libname$release$shared_ext$major' - ;; - esac - # HP-UX runs *really* slowly unless shared libraries are mode 555, ... - postinstall_cmds='chmod 555 $lib' - # or fails outright, so override atomically: - install_override_mode=555 - ;; - -interix[3-9]*) - version_type=linux # correct to gnu/linux during the next big refactor - need_lib_prefix=no - need_version=no - library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' - soname_spec='$libname$release$shared_ext$major' - dynamic_linker='Interix 3.x ld.so.1 (PE, like ELF)' - shlibpath_var=LD_LIBRARY_PATH - shlibpath_overrides_runpath=no - hardcode_into_libs=yes - ;; - -irix5* | irix6* | nonstopux*) - case $host_os in - nonstopux*) version_type=nonstopux ;; - *) - if test yes = "$lt_cv_prog_gnu_ld"; then - version_type=linux # correct to gnu/linux during the next big refactor - else - version_type=irix - fi ;; - esac - need_lib_prefix=no - need_version=no - soname_spec='$libname$release$shared_ext$major' - library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$release$shared_ext $libname$shared_ext' - case $host_os in - irix5* | nonstopux*) - libsuff= shlibsuff= - ;; - *) - case $LD in # libtool.m4 will add one of these switches to LD - *-32|*"-32 "|*-melf32bsmip|*"-melf32bsmip ") - libsuff= shlibsuff= libmagic=32-bit;; - *-n32|*"-n32 "|*-melf32bmipn32|*"-melf32bmipn32 ") - libsuff=32 shlibsuff=N32 libmagic=N32;; - *-64|*"-64 "|*-melf64bmip|*"-melf64bmip ") - libsuff=64 shlibsuff=64 libmagic=64-bit;; - *) libsuff= shlibsuff= libmagic=never-match;; - esac - ;; - esac - shlibpath_var=LD_LIBRARY${shlibsuff}_PATH - shlibpath_overrides_runpath=no - sys_lib_search_path_spec="/usr/lib$libsuff /lib$libsuff /usr/local/lib$libsuff" - sys_lib_dlsearch_path_spec="/usr/lib$libsuff /lib$libsuff" - hardcode_into_libs=yes - ;; - -# No shared lib support for Linux oldld, aout, or coff. -linux*oldld* | linux*aout* | linux*coff*) - dynamic_linker=no - ;; - -linux*android*) - version_type=none # Android doesn't support versioned libraries. - need_lib_prefix=no - need_version=no - library_names_spec='$libname$release$shared_ext' - soname_spec='$libname$release$shared_ext' - finish_cmds= - shlibpath_var=LD_LIBRARY_PATH - shlibpath_overrides_runpath=yes - - # This implies no fast_install, which is unacceptable. - # Some rework will be needed to allow for fast_install - # before this can be enabled. - hardcode_into_libs=yes - - dynamic_linker='Android linker' - # Don't embed -rpath directories since the linker doesn't support them. - hardcode_libdir_flag_spec='-L$libdir' - ;; - -# This must be glibc/ELF. -linux* | k*bsd*-gnu | kopensolaris*-gnu | gnu*) - version_type=linux # correct to gnu/linux during the next big refactor - need_lib_prefix=no - need_version=no - library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' - soname_spec='$libname$release$shared_ext$major' - finish_cmds='PATH="\$PATH:/sbin" ldconfig -n $libdir' - shlibpath_var=LD_LIBRARY_PATH - shlibpath_overrides_runpath=no - - # Some binutils ld are patched to set DT_RUNPATH - if ${lt_cv_shlibpath_overrides_runpath+:} false; then : - $as_echo_n "(cached) " >&6 -else - lt_cv_shlibpath_overrides_runpath=no - save_LDFLAGS=$LDFLAGS - save_libdir=$libdir - eval "libdir=/foo; wl=\"$lt_prog_compiler_wl\"; \ - LDFLAGS=\"\$LDFLAGS $hardcode_libdir_flag_spec\"" - cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ - -int -main () -{ - - ; - return 0; -} -_ACEOF -if ac_fn_c_try_link "$LINENO"; then : - if ($OBJDUMP -p conftest$ac_exeext) 2>/dev/null | grep "RUNPATH.*$libdir" >/dev/null; then : - lt_cv_shlibpath_overrides_runpath=yes -fi -fi -rm -f core conftest.err conftest.$ac_objext \ - conftest$ac_exeext conftest.$ac_ext - LDFLAGS=$save_LDFLAGS - libdir=$save_libdir - -fi - - shlibpath_overrides_runpath=$lt_cv_shlibpath_overrides_runpath - - # This implies no fast_install, which is unacceptable. - # Some rework will be needed to allow for fast_install - # before this can be enabled. - hardcode_into_libs=yes - - # Ideally, we could use ldconfig to report *all* directores which are - # searched for libraries, however this is still not possible. Aside from not - # being certain /sbin/ldconfig is available, command - # 'ldconfig -N -X -v | grep ^/' on 64bit Fedora does not report /usr/lib64, - # even though it is searched at run-time. Try to do the best guess by - # appending ld.so.conf contents (and includes) to the search path. - if test -f /etc/ld.so.conf; then - lt_ld_extra=`awk '/^include / { system(sprintf("cd /etc; cat %s 2>/dev/null", \$2)); skip = 1; } { if (!skip) print \$0; skip = 0; }' < /etc/ld.so.conf | $SED -e 's/#.*//;/^[ ]*hwcap[ ]/d;s/[:, ]/ /g;s/=[^=]*$//;s/=[^= ]* / /g;s/"//g;/^$/d' | tr '\n' ' '` - sys_lib_dlsearch_path_spec="/lib /usr/lib $lt_ld_extra" - fi - - # We used to test for /lib/ld.so.1 and disable shared libraries on - # powerpc, because MkLinux only supported shared libraries with the - # GNU dynamic linker. Since this was broken with cross compilers, - # most powerpc-linux boxes support dynamic linking these days and - # people can always --disable-shared, the test was removed, and we - # assume the GNU/Linux dynamic linker is in use. - dynamic_linker='GNU/Linux ld.so' - ;; - -netbsd*) - version_type=sunos - need_lib_prefix=no - need_version=no - if echo __ELF__ | $CC -E - | $GREP __ELF__ >/dev/null; then - library_names_spec='$libname$release$shared_ext$versuffix $libname$shared_ext$versuffix' - finish_cmds='PATH="\$PATH:/sbin" ldconfig -m $libdir' - dynamic_linker='NetBSD (a.out) ld.so' - else - library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' - soname_spec='$libname$release$shared_ext$major' - dynamic_linker='NetBSD ld.elf_so' - fi - shlibpath_var=LD_LIBRARY_PATH - shlibpath_overrides_runpath=yes - hardcode_into_libs=yes - ;; - -newsos6) - version_type=linux # correct to gnu/linux during the next big refactor - library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' - shlibpath_var=LD_LIBRARY_PATH - shlibpath_overrides_runpath=yes - ;; - -*nto* | *qnx*) - version_type=qnx - need_lib_prefix=no - need_version=no - library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' - soname_spec='$libname$release$shared_ext$major' - shlibpath_var=LD_LIBRARY_PATH - shlibpath_overrides_runpath=no - hardcode_into_libs=yes - dynamic_linker='ldqnx.so' - ;; - -openbsd* | bitrig*) - version_type=sunos - sys_lib_dlsearch_path_spec=/usr/lib - need_lib_prefix=no - if test -z "`echo __ELF__ | $CC -E - | $GREP __ELF__`"; then - need_version=no - else - need_version=yes - fi - library_names_spec='$libname$release$shared_ext$versuffix $libname$shared_ext$versuffix' - finish_cmds='PATH="\$PATH:/sbin" ldconfig -m $libdir' - shlibpath_var=LD_LIBRARY_PATH - shlibpath_overrides_runpath=yes - ;; - -os2*) - libname_spec='$name' - version_type=windows - shrext_cmds=.dll - need_version=no - need_lib_prefix=no - # OS/2 can only load a DLL with a base name of 8 characters or less. - soname_spec='`test -n "$os2dllname" && libname="$os2dllname"; - v=$($ECHO $release$versuffix | tr -d .-); - n=$($ECHO $libname | cut -b -$((8 - ${#v})) | tr . _); - $ECHO $n$v`$shared_ext' - library_names_spec='${libname}_dll.$libext' - dynamic_linker='OS/2 ld.exe' - shlibpath_var=BEGINLIBPATH - sys_lib_search_path_spec="/lib /usr/lib /usr/local/lib" - sys_lib_dlsearch_path_spec=$sys_lib_search_path_spec - postinstall_cmds='base_file=`basename \$file`~ - dlpath=`$SHELL 2>&1 -c '\''. $dir/'\''\$base_file'\''i; $ECHO \$dlname'\''`~ - dldir=$destdir/`dirname \$dlpath`~ - test -d \$dldir || mkdir -p \$dldir~ - $install_prog $dir/$dlname \$dldir/$dlname~ - chmod a+x \$dldir/$dlname~ - if test -n '\''$stripme'\'' && test -n '\''$striplib'\''; then - eval '\''$striplib \$dldir/$dlname'\'' || exit \$?; - fi' - postuninstall_cmds='dldll=`$SHELL 2>&1 -c '\''. $file; $ECHO \$dlname'\''`~ - dlpath=$dir/\$dldll~ - $RM \$dlpath' - ;; - -osf3* | osf4* | osf5*) - version_type=osf - need_lib_prefix=no - need_version=no - soname_spec='$libname$release$shared_ext$major' - library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' - shlibpath_var=LD_LIBRARY_PATH - sys_lib_search_path_spec="/usr/shlib /usr/ccs/lib /usr/lib/cmplrs/cc /usr/lib /usr/local/lib /var/shlib" - sys_lib_dlsearch_path_spec=$sys_lib_search_path_spec - ;; - -rdos*) - dynamic_linker=no - ;; - -solaris*) - version_type=linux # correct to gnu/linux during the next big refactor - need_lib_prefix=no - need_version=no - library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' - soname_spec='$libname$release$shared_ext$major' - shlibpath_var=LD_LIBRARY_PATH - shlibpath_overrides_runpath=yes - hardcode_into_libs=yes - # ldd complains unless libraries are executable - postinstall_cmds='chmod +x $lib' - ;; - -sunos4*) - version_type=sunos - library_names_spec='$libname$release$shared_ext$versuffix $libname$shared_ext$versuffix' - finish_cmds='PATH="\$PATH:/usr/etc" ldconfig $libdir' - shlibpath_var=LD_LIBRARY_PATH - shlibpath_overrides_runpath=yes - if test yes = "$with_gnu_ld"; then - need_lib_prefix=no - fi - need_version=yes - ;; - -sysv4 | sysv4.3*) - version_type=linux # correct to gnu/linux during the next big refactor - library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' - soname_spec='$libname$release$shared_ext$major' - shlibpath_var=LD_LIBRARY_PATH - case $host_vendor in - sni) - shlibpath_overrides_runpath=no - need_lib_prefix=no - runpath_var=LD_RUN_PATH - ;; - siemens) - need_lib_prefix=no - ;; - motorola) - need_lib_prefix=no - need_version=no - shlibpath_overrides_runpath=no - sys_lib_search_path_spec='/lib /usr/lib /usr/ccs/lib' - ;; - esac - ;; - -sysv4*MP*) - if test -d /usr/nec; then - version_type=linux # correct to gnu/linux during the next big refactor - library_names_spec='$libname$shared_ext.$versuffix $libname$shared_ext.$major $libname$shared_ext' - soname_spec='$libname$shared_ext.$major' - shlibpath_var=LD_LIBRARY_PATH - fi - ;; - -sysv5* | sco3.2v5* | sco5v6* | unixware* | OpenUNIX* | sysv4*uw2*) - version_type=sco - need_lib_prefix=no - need_version=no - library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext $libname$shared_ext' - soname_spec='$libname$release$shared_ext$major' - shlibpath_var=LD_LIBRARY_PATH - shlibpath_overrides_runpath=yes - hardcode_into_libs=yes - if test yes = "$with_gnu_ld"; then - sys_lib_search_path_spec='/usr/local/lib /usr/gnu/lib /usr/ccs/lib /usr/lib /lib' - else - sys_lib_search_path_spec='/usr/ccs/lib /usr/lib' - case $host_os in - sco3.2v5*) - sys_lib_search_path_spec="$sys_lib_search_path_spec /lib" - ;; - esac - fi - sys_lib_dlsearch_path_spec='/usr/lib' - ;; - -tpf*) - # TPF is a cross-target only. Preferred cross-host = GNU/Linux. - version_type=linux # correct to gnu/linux during the next big refactor - need_lib_prefix=no - need_version=no - library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' - shlibpath_var=LD_LIBRARY_PATH - shlibpath_overrides_runpath=no - hardcode_into_libs=yes - ;; - -uts4*) - version_type=linux # correct to gnu/linux during the next big refactor - library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext' - soname_spec='$libname$release$shared_ext$major' - shlibpath_var=LD_LIBRARY_PATH - ;; - -*) - dynamic_linker=no - ;; -esac -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $dynamic_linker" >&5 -$as_echo "$dynamic_linker" >&6; } -test no = "$dynamic_linker" && can_build_shared=no - -variables_saved_for_relink="PATH $shlibpath_var $runpath_var" -if test yes = "$GCC"; then - variables_saved_for_relink="$variables_saved_for_relink GCC_EXEC_PREFIX COMPILER_PATH LIBRARY_PATH" -fi - -if test set = "${lt_cv_sys_lib_search_path_spec+set}"; then - sys_lib_search_path_spec=$lt_cv_sys_lib_search_path_spec -fi - -if test set = "${lt_cv_sys_lib_dlsearch_path_spec+set}"; then - sys_lib_dlsearch_path_spec=$lt_cv_sys_lib_dlsearch_path_spec -fi - -# remember unaugmented sys_lib_dlsearch_path content for libtool script decls... -configure_time_dlsearch_path=$sys_lib_dlsearch_path_spec - -# ... but it needs LT_SYS_LIBRARY_PATH munging for other configure-time code -func_munge_path_list sys_lib_dlsearch_path_spec "$LT_SYS_LIBRARY_PATH" - -# to be used as default LT_SYS_LIBRARY_PATH value in generated libtool -configure_time_lt_sys_library_path=$LT_SYS_LIBRARY_PATH - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - { $as_echo "$as_me:${as_lineno-$LINENO}: checking how to hardcode library paths into programs" >&5 -$as_echo_n "checking how to hardcode library paths into programs... " >&6; } -hardcode_action= -if test -n "$hardcode_libdir_flag_spec" || - test -n "$runpath_var" || - test yes = "$hardcode_automatic"; then - - # We can hardcode non-existent directories. - if test no != "$hardcode_direct" && - # If the only mechanism to avoid hardcoding is shlibpath_var, we - # have to relink, otherwise we might link with an installed library - # when we should be linking with a yet-to-be-installed one - ## test no != "$_LT_TAGVAR(hardcode_shlibpath_var, )" && - test no != "$hardcode_minus_L"; then - # Linking always hardcodes the temporary library directory. - hardcode_action=relink - else - # We can link without hardcoding, and we can hardcode nonexisting dirs. - hardcode_action=immediate - fi -else - # We cannot hardcode anything, or else we can only hardcode existing - # directories. - hardcode_action=unsupported -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $hardcode_action" >&5 -$as_echo "$hardcode_action" >&6; } - -if test relink = "$hardcode_action" || - test yes = "$inherit_rpath"; then - # Fast installation is not supported - enable_fast_install=no -elif test yes = "$shlibpath_overrides_runpath" || - test no = "$enable_shared"; then - # Fast installation is not necessary - enable_fast_install=needless -fi - - - - - - - if test yes != "$enable_dlopen"; then - enable_dlopen=unknown - enable_dlopen_self=unknown - enable_dlopen_self_static=unknown -else - lt_cv_dlopen=no - lt_cv_dlopen_libs= - - case $host_os in - beos*) - lt_cv_dlopen=load_add_on - lt_cv_dlopen_libs= - lt_cv_dlopen_self=yes - ;; - - mingw* | pw32* | cegcc*) - lt_cv_dlopen=LoadLibrary - lt_cv_dlopen_libs= - ;; - - cygwin*) - lt_cv_dlopen=dlopen - lt_cv_dlopen_libs= - ;; - - darwin*) - # if libdl is installed we need to link against it - { $as_echo "$as_me:${as_lineno-$LINENO}: checking for dlopen in -ldl" >&5 -$as_echo_n "checking for dlopen in -ldl... " >&6; } -if ${ac_cv_lib_dl_dlopen+:} false; then : - $as_echo_n "(cached) " >&6 -else - ac_check_lib_save_LIBS=$LIBS -LIBS="-ldl $LIBS" -cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ - -/* Override any GCC internal prototype to avoid an error. - Use char because int might match the return type of a GCC - builtin and then its argument prototype would still apply. */ -#ifdef __cplusplus -extern "C" -#endif -char dlopen (); -int -main () -{ -return dlopen (); - ; - return 0; -} -_ACEOF -if ac_fn_c_try_link "$LINENO"; then : - ac_cv_lib_dl_dlopen=yes -else - ac_cv_lib_dl_dlopen=no -fi -rm -f core conftest.err conftest.$ac_objext \ - conftest$ac_exeext conftest.$ac_ext -LIBS=$ac_check_lib_save_LIBS -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_dl_dlopen" >&5 -$as_echo "$ac_cv_lib_dl_dlopen" >&6; } -if test "x$ac_cv_lib_dl_dlopen" = xyes; then : - lt_cv_dlopen=dlopen lt_cv_dlopen_libs=-ldl -else - - lt_cv_dlopen=dyld - lt_cv_dlopen_libs= - lt_cv_dlopen_self=yes - -fi - - ;; - - tpf*) - # Don't try to run any link tests for TPF. We know it's impossible - # because TPF is a cross-compiler, and we know how we open DSOs. - lt_cv_dlopen=dlopen - lt_cv_dlopen_libs= - lt_cv_dlopen_self=no - ;; - - *) - ac_fn_c_check_func "$LINENO" "shl_load" "ac_cv_func_shl_load" -if test "x$ac_cv_func_shl_load" = xyes; then : - lt_cv_dlopen=shl_load -else - { $as_echo "$as_me:${as_lineno-$LINENO}: checking for shl_load in -ldld" >&5 -$as_echo_n "checking for shl_load in -ldld... " >&6; } -if ${ac_cv_lib_dld_shl_load+:} false; then : - $as_echo_n "(cached) " >&6 -else - ac_check_lib_save_LIBS=$LIBS -LIBS="-ldld $LIBS" -cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ - -/* Override any GCC internal prototype to avoid an error. - Use char because int might match the return type of a GCC - builtin and then its argument prototype would still apply. */ -#ifdef __cplusplus -extern "C" -#endif -char shl_load (); -int -main () -{ -return shl_load (); - ; - return 0; -} -_ACEOF -if ac_fn_c_try_link "$LINENO"; then : - ac_cv_lib_dld_shl_load=yes -else - ac_cv_lib_dld_shl_load=no -fi -rm -f core conftest.err conftest.$ac_objext \ - conftest$ac_exeext conftest.$ac_ext -LIBS=$ac_check_lib_save_LIBS -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_dld_shl_load" >&5 -$as_echo "$ac_cv_lib_dld_shl_load" >&6; } -if test "x$ac_cv_lib_dld_shl_load" = xyes; then : - lt_cv_dlopen=shl_load lt_cv_dlopen_libs=-ldld -else - ac_fn_c_check_func "$LINENO" "dlopen" "ac_cv_func_dlopen" -if test "x$ac_cv_func_dlopen" = xyes; then : - lt_cv_dlopen=dlopen -else - { $as_echo "$as_me:${as_lineno-$LINENO}: checking for dlopen in -ldl" >&5 -$as_echo_n "checking for dlopen in -ldl... " >&6; } -if ${ac_cv_lib_dl_dlopen+:} false; then : - $as_echo_n "(cached) " >&6 -else - ac_check_lib_save_LIBS=$LIBS -LIBS="-ldl $LIBS" -cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ - -/* Override any GCC internal prototype to avoid an error. - Use char because int might match the return type of a GCC - builtin and then its argument prototype would still apply. */ -#ifdef __cplusplus -extern "C" -#endif -char dlopen (); -int -main () -{ -return dlopen (); - ; - return 0; -} -_ACEOF -if ac_fn_c_try_link "$LINENO"; then : - ac_cv_lib_dl_dlopen=yes -else - ac_cv_lib_dl_dlopen=no -fi -rm -f core conftest.err conftest.$ac_objext \ - conftest$ac_exeext conftest.$ac_ext -LIBS=$ac_check_lib_save_LIBS -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_dl_dlopen" >&5 -$as_echo "$ac_cv_lib_dl_dlopen" >&6; } -if test "x$ac_cv_lib_dl_dlopen" = xyes; then : - lt_cv_dlopen=dlopen lt_cv_dlopen_libs=-ldl -else - { $as_echo "$as_me:${as_lineno-$LINENO}: checking for dlopen in -lsvld" >&5 -$as_echo_n "checking for dlopen in -lsvld... " >&6; } -if ${ac_cv_lib_svld_dlopen+:} false; then : - $as_echo_n "(cached) " >&6 -else - ac_check_lib_save_LIBS=$LIBS -LIBS="-lsvld $LIBS" -cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ - -/* Override any GCC internal prototype to avoid an error. - Use char because int might match the return type of a GCC - builtin and then its argument prototype would still apply. */ -#ifdef __cplusplus -extern "C" -#endif -char dlopen (); -int -main () -{ -return dlopen (); - ; - return 0; -} -_ACEOF -if ac_fn_c_try_link "$LINENO"; then : - ac_cv_lib_svld_dlopen=yes -else - ac_cv_lib_svld_dlopen=no -fi -rm -f core conftest.err conftest.$ac_objext \ - conftest$ac_exeext conftest.$ac_ext -LIBS=$ac_check_lib_save_LIBS -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_svld_dlopen" >&5 -$as_echo "$ac_cv_lib_svld_dlopen" >&6; } -if test "x$ac_cv_lib_svld_dlopen" = xyes; then : - lt_cv_dlopen=dlopen lt_cv_dlopen_libs=-lsvld -else - { $as_echo "$as_me:${as_lineno-$LINENO}: checking for dld_link in -ldld" >&5 -$as_echo_n "checking for dld_link in -ldld... " >&6; } -if ${ac_cv_lib_dld_dld_link+:} false; then : - $as_echo_n "(cached) " >&6 -else - ac_check_lib_save_LIBS=$LIBS -LIBS="-ldld $LIBS" -cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ - -/* Override any GCC internal prototype to avoid an error. - Use char because int might match the return type of a GCC - builtin and then its argument prototype would still apply. */ -#ifdef __cplusplus -extern "C" -#endif -char dld_link (); -int -main () -{ -return dld_link (); - ; - return 0; -} -_ACEOF -if ac_fn_c_try_link "$LINENO"; then : - ac_cv_lib_dld_dld_link=yes -else - ac_cv_lib_dld_dld_link=no -fi -rm -f core conftest.err conftest.$ac_objext \ - conftest$ac_exeext conftest.$ac_ext -LIBS=$ac_check_lib_save_LIBS -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_dld_dld_link" >&5 -$as_echo "$ac_cv_lib_dld_dld_link" >&6; } -if test "x$ac_cv_lib_dld_dld_link" = xyes; then : - lt_cv_dlopen=dld_link lt_cv_dlopen_libs=-ldld -fi - - -fi - - -fi - - -fi - - -fi - - -fi - - ;; - esac - - if test no = "$lt_cv_dlopen"; then - enable_dlopen=no - else - enable_dlopen=yes - fi - - case $lt_cv_dlopen in - dlopen) - save_CPPFLAGS=$CPPFLAGS - test yes = "$ac_cv_header_dlfcn_h" && CPPFLAGS="$CPPFLAGS -DHAVE_DLFCN_H" - - save_LDFLAGS=$LDFLAGS - wl=$lt_prog_compiler_wl eval LDFLAGS=\"\$LDFLAGS $export_dynamic_flag_spec\" - - save_LIBS=$LIBS - LIBS="$lt_cv_dlopen_libs $LIBS" - - { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether a program can dlopen itself" >&5 -$as_echo_n "checking whether a program can dlopen itself... " >&6; } -if ${lt_cv_dlopen_self+:} false; then : - $as_echo_n "(cached) " >&6 -else - if test yes = "$cross_compiling"; then : - lt_cv_dlopen_self=cross -else - lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2 - lt_status=$lt_dlunknown - cat > conftest.$ac_ext <<_LT_EOF -#line $LINENO "configure" -#include "confdefs.h" - -#if HAVE_DLFCN_H -#include -#endif - -#include - -#ifdef RTLD_GLOBAL -# define LT_DLGLOBAL RTLD_GLOBAL -#else -# ifdef DL_GLOBAL -# define LT_DLGLOBAL DL_GLOBAL -# else -# define LT_DLGLOBAL 0 -# endif -#endif - -/* We may have to define LT_DLLAZY_OR_NOW in the command line if we - find out it does not work in some platform. */ -#ifndef LT_DLLAZY_OR_NOW -# ifdef RTLD_LAZY -# define LT_DLLAZY_OR_NOW RTLD_LAZY -# else -# ifdef DL_LAZY -# define LT_DLLAZY_OR_NOW DL_LAZY -# else -# ifdef RTLD_NOW -# define LT_DLLAZY_OR_NOW RTLD_NOW -# else -# ifdef DL_NOW -# define LT_DLLAZY_OR_NOW DL_NOW -# else -# define LT_DLLAZY_OR_NOW 0 -# endif -# endif -# endif -# endif -#endif - -/* When -fvisibility=hidden is used, assume the code has been annotated - correspondingly for the symbols needed. */ -#if defined __GNUC__ && (((__GNUC__ == 3) && (__GNUC_MINOR__ >= 3)) || (__GNUC__ > 3)) -int fnord () __attribute__((visibility("default"))); -#endif - -int fnord () { return 42; } -int main () -{ - void *self = dlopen (0, LT_DLGLOBAL|LT_DLLAZY_OR_NOW); - int status = $lt_dlunknown; - - if (self) - { - if (dlsym (self,"fnord")) status = $lt_dlno_uscore; - else - { - if (dlsym( self,"_fnord")) status = $lt_dlneed_uscore; - else puts (dlerror ()); - } - /* dlclose (self); */ - } - else - puts (dlerror ()); - - return status; -} -_LT_EOF - if { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_link\""; } >&5 - (eval $ac_link) 2>&5 - ac_status=$? - $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5 - test $ac_status = 0; } && test -s "conftest$ac_exeext" 2>/dev/null; then - (./conftest; exit; ) >&5 2>/dev/null - lt_status=$? - case x$lt_status in - x$lt_dlno_uscore) lt_cv_dlopen_self=yes ;; - x$lt_dlneed_uscore) lt_cv_dlopen_self=yes ;; - x$lt_dlunknown|x*) lt_cv_dlopen_self=no ;; - esac - else : - # compilation failed - lt_cv_dlopen_self=no - fi -fi -rm -fr conftest* - - -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $lt_cv_dlopen_self" >&5 -$as_echo "$lt_cv_dlopen_self" >&6; } - - if test yes = "$lt_cv_dlopen_self"; then - wl=$lt_prog_compiler_wl eval LDFLAGS=\"\$LDFLAGS $lt_prog_compiler_static\" - { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether a statically linked program can dlopen itself" >&5 -$as_echo_n "checking whether a statically linked program can dlopen itself... " >&6; } -if ${lt_cv_dlopen_self_static+:} false; then : - $as_echo_n "(cached) " >&6 -else - if test yes = "$cross_compiling"; then : - lt_cv_dlopen_self_static=cross -else - lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2 - lt_status=$lt_dlunknown - cat > conftest.$ac_ext <<_LT_EOF -#line $LINENO "configure" -#include "confdefs.h" - -#if HAVE_DLFCN_H -#include -#endif - -#include - -#ifdef RTLD_GLOBAL -# define LT_DLGLOBAL RTLD_GLOBAL -#else -# ifdef DL_GLOBAL -# define LT_DLGLOBAL DL_GLOBAL -# else -# define LT_DLGLOBAL 0 -# endif -#endif - -/* We may have to define LT_DLLAZY_OR_NOW in the command line if we - find out it does not work in some platform. */ -#ifndef LT_DLLAZY_OR_NOW -# ifdef RTLD_LAZY -# define LT_DLLAZY_OR_NOW RTLD_LAZY -# else -# ifdef DL_LAZY -# define LT_DLLAZY_OR_NOW DL_LAZY -# else -# ifdef RTLD_NOW -# define LT_DLLAZY_OR_NOW RTLD_NOW -# else -# ifdef DL_NOW -# define LT_DLLAZY_OR_NOW DL_NOW -# else -# define LT_DLLAZY_OR_NOW 0 -# endif -# endif -# endif -# endif -#endif - -/* When -fvisibility=hidden is used, assume the code has been annotated - correspondingly for the symbols needed. */ -#if defined __GNUC__ && (((__GNUC__ == 3) && (__GNUC_MINOR__ >= 3)) || (__GNUC__ > 3)) -int fnord () __attribute__((visibility("default"))); -#endif - -int fnord () { return 42; } -int main () -{ - void *self = dlopen (0, LT_DLGLOBAL|LT_DLLAZY_OR_NOW); - int status = $lt_dlunknown; - - if (self) - { - if (dlsym (self,"fnord")) status = $lt_dlno_uscore; - else - { - if (dlsym( self,"_fnord")) status = $lt_dlneed_uscore; - else puts (dlerror ()); - } - /* dlclose (self); */ - } - else - puts (dlerror ()); - - return status; -} -_LT_EOF - if { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_link\""; } >&5 - (eval $ac_link) 2>&5 - ac_status=$? - $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5 - test $ac_status = 0; } && test -s "conftest$ac_exeext" 2>/dev/null; then - (./conftest; exit; ) >&5 2>/dev/null - lt_status=$? - case x$lt_status in - x$lt_dlno_uscore) lt_cv_dlopen_self_static=yes ;; - x$lt_dlneed_uscore) lt_cv_dlopen_self_static=yes ;; - x$lt_dlunknown|x*) lt_cv_dlopen_self_static=no ;; - esac - else : - # compilation failed - lt_cv_dlopen_self_static=no - fi -fi -rm -fr conftest* - - -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $lt_cv_dlopen_self_static" >&5 -$as_echo "$lt_cv_dlopen_self_static" >&6; } - fi - - CPPFLAGS=$save_CPPFLAGS - LDFLAGS=$save_LDFLAGS - LIBS=$save_LIBS - ;; - esac - - case $lt_cv_dlopen_self in - yes|no) enable_dlopen_self=$lt_cv_dlopen_self ;; - *) enable_dlopen_self=unknown ;; - esac - - case $lt_cv_dlopen_self_static in - yes|no) enable_dlopen_self_static=$lt_cv_dlopen_self_static ;; - *) enable_dlopen_self_static=unknown ;; - esac -fi - - - - - - - - - - - - - - - - - -striplib= -old_striplib= -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether stripping libraries is possible" >&5 -$as_echo_n "checking whether stripping libraries is possible... " >&6; } -if test -n "$STRIP" && $STRIP -V 2>&1 | $GREP "GNU strip" >/dev/null; then - test -z "$old_striplib" && old_striplib="$STRIP --strip-debug" - test -z "$striplib" && striplib="$STRIP --strip-unneeded" - { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5 -$as_echo "yes" >&6; } -else -# FIXME - insert some real tests, host_os isn't really good enough - case $host_os in - darwin*) - if test -n "$STRIP"; then - striplib="$STRIP -x" - old_striplib="$STRIP -S" - { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5 -$as_echo "yes" >&6; } - else - { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 -$as_echo "no" >&6; } - fi - ;; - *) - { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 -$as_echo "no" >&6; } - ;; - esac -fi - - - - - - - - - - - - - # Report what library types will actually be built - { $as_echo "$as_me:${as_lineno-$LINENO}: checking if libtool supports shared libraries" >&5 -$as_echo_n "checking if libtool supports shared libraries... " >&6; } - { $as_echo "$as_me:${as_lineno-$LINENO}: result: $can_build_shared" >&5 -$as_echo "$can_build_shared" >&6; } - - { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether to build shared libraries" >&5 -$as_echo_n "checking whether to build shared libraries... " >&6; } - test no = "$can_build_shared" && enable_shared=no - - # On AIX, shared libraries and static libraries use the same namespace, and - # are all built from PIC. - case $host_os in - aix3*) - test yes = "$enable_shared" && enable_static=no - if test -n "$RANLIB"; then - archive_cmds="$archive_cmds~\$RANLIB \$lib" - postinstall_cmds='$RANLIB $lib' - fi - ;; - - aix[4-9]*) - if test ia64 != "$host_cpu"; then - case $enable_shared,$with_aix_soname,$aix_use_runtimelinking in - yes,aix,yes) ;; # shared object as lib.so file only - yes,svr4,*) ;; # shared object as lib.so archive member only - yes,*) enable_static=no ;; # shared object in lib.a archive as well - esac - fi - ;; - esac - { $as_echo "$as_me:${as_lineno-$LINENO}: result: $enable_shared" >&5 -$as_echo "$enable_shared" >&6; } - - { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether to build static libraries" >&5 -$as_echo_n "checking whether to build static libraries... " >&6; } - # Make sure either enable_shared or enable_static is yes. - test yes = "$enable_shared" || enable_static=yes - { $as_echo "$as_me:${as_lineno-$LINENO}: result: $enable_static" >&5 -$as_echo "$enable_static" >&6; } - - - - -fi -ac_ext=c -ac_cpp='$CPP $CPPFLAGS' -ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5' -ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5' -ac_compiler_gnu=$ac_cv_c_compiler_gnu - -CC=$lt_save_CC - - - - - - - - - - - - - - - - ac_config_commands="$ac_config_commands libtool" - - - - -# Only expand once: - - -# Find a good install program. We prefer a C program (faster), -# so one script is as good as another. But avoid the broken or -# incompatible versions: -# SysV /etc/install, /usr/sbin/install -# SunOS /usr/etc/install -# IRIX /sbin/install -# AIX /bin/install -# AmigaOS /C/install, which installs bootblocks on floppy discs -# AIX 4 /usr/bin/installbsd, which doesn't work without a -g flag -# AFS /usr/afsws/bin/install, which mishandles nonexistent args -# SVR4 /usr/ucb/install, which tries to use the nonexistent group "staff" -# OS/2's system install, which has a completely different semantic -# ./install, which can be erroneously created by make from ./install.sh. -# Reject install programs that cannot install multiple files. -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for a BSD-compatible install" >&5 -$as_echo_n "checking for a BSD-compatible install... " >&6; } -if test -z "$INSTALL"; then -if ${ac_cv_path_install+:} false; then : - $as_echo_n "(cached) " >&6 -else - as_save_IFS=$IFS; IFS=$PATH_SEPARATOR -for as_dir in $PATH -do - IFS=$as_save_IFS - test -z "$as_dir" && as_dir=. - # Account for people who put trailing slashes in PATH elements. -case $as_dir/ in #(( - ./ | .// | /[cC]/* | \ - /etc/* | /usr/sbin/* | /usr/etc/* | /sbin/* | /usr/afsws/bin/* | \ - ?:[\\/]os2[\\/]install[\\/]* | ?:[\\/]OS2[\\/]INSTALL[\\/]* | \ - /usr/ucb/* ) ;; - *) - # OSF1 and SCO ODT 3.0 have their own names for install. - # Don't use installbsd from OSF since it installs stuff as root - # by default. - for ac_prog in ginstall scoinst install; do - for ac_exec_ext in '' $ac_executable_extensions; do - if as_fn_executable_p "$as_dir/$ac_prog$ac_exec_ext"; then - if test $ac_prog = install && - grep dspmsg "$as_dir/$ac_prog$ac_exec_ext" >/dev/null 2>&1; then - # AIX install. It has an incompatible calling convention. - : - elif test $ac_prog = install && - grep pwplus "$as_dir/$ac_prog$ac_exec_ext" >/dev/null 2>&1; then - # program-specific install script used by HP pwplus--don't use. - : - else - rm -rf conftest.one conftest.two conftest.dir - echo one > conftest.one - echo two > conftest.two - mkdir conftest.dir - if "$as_dir/$ac_prog$ac_exec_ext" -c conftest.one conftest.two "`pwd`/conftest.dir" && - test -s conftest.one && test -s conftest.two && - test -s conftest.dir/conftest.one && - test -s conftest.dir/conftest.two - then - ac_cv_path_install="$as_dir/$ac_prog$ac_exec_ext -c" - break 3 - fi - fi - fi - done - done - ;; -esac - - done -IFS=$as_save_IFS - -rm -rf conftest.one conftest.two conftest.dir - -fi - if test "${ac_cv_path_install+set}" = set; then - INSTALL=$ac_cv_path_install - else - # As a last resort, use the slow shell script. Don't cache a - # value for INSTALL within a source directory, because that will - # break other packages using the cache if that directory is - # removed, or if the value is a relative name. - INSTALL=$ac_install_sh - fi -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $INSTALL" >&5 -$as_echo "$INSTALL" >&6; } - -# Use test -z because SunOS4 sh mishandles braces in ${var-val}. -# It thinks the first close brace ends the variable substitution. -test -z "$INSTALL_PROGRAM" && INSTALL_PROGRAM='${INSTALL}' - -test -z "$INSTALL_SCRIPT" && INSTALL_SCRIPT='${INSTALL}' - -test -z "$INSTALL_DATA" && INSTALL_DATA='${INSTALL} -m 644' - - -######### -# Enable large file support (if special flags are necessary) -# -# Check whether --enable-largefile was given. -if test "${enable_largefile+set}" = set; then : - enableval=$enable_largefile; -fi - -if test "$enable_largefile" != no; then - - { $as_echo "$as_me:${as_lineno-$LINENO}: checking for special C compiler options needed for large files" >&5 -$as_echo_n "checking for special C compiler options needed for large files... " >&6; } -if ${ac_cv_sys_largefile_CC+:} false; then : - $as_echo_n "(cached) " >&6 -else - ac_cv_sys_largefile_CC=no - if test "$GCC" != yes; then - ac_save_CC=$CC - while :; do - # IRIX 6.2 and later do not support large files by default, - # so use the C compiler's -n32 option if that helps. - cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ -#include - /* Check that off_t can represent 2**63 - 1 correctly. - We can't simply define LARGE_OFF_T to be 9223372036854775807, - since some C++ compilers masquerading as C compilers - incorrectly reject 9223372036854775807. */ -#define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62)) - int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721 - && LARGE_OFF_T % 2147483647 == 1) - ? 1 : -1]; -int -main () -{ - - ; - return 0; -} -_ACEOF - if ac_fn_c_try_compile "$LINENO"; then : - break -fi -rm -f core conftest.err conftest.$ac_objext - CC="$CC -n32" - if ac_fn_c_try_compile "$LINENO"; then : - ac_cv_sys_largefile_CC=' -n32'; break -fi -rm -f core conftest.err conftest.$ac_objext - break - done - CC=$ac_save_CC - rm -f conftest.$ac_ext - fi -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_sys_largefile_CC" >&5 -$as_echo "$ac_cv_sys_largefile_CC" >&6; } - if test "$ac_cv_sys_largefile_CC" != no; then - CC=$CC$ac_cv_sys_largefile_CC - fi - - { $as_echo "$as_me:${as_lineno-$LINENO}: checking for _FILE_OFFSET_BITS value needed for large files" >&5 -$as_echo_n "checking for _FILE_OFFSET_BITS value needed for large files... " >&6; } -if ${ac_cv_sys_file_offset_bits+:} false; then : - $as_echo_n "(cached) " >&6 -else - while :; do - cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ -#include - /* Check that off_t can represent 2**63 - 1 correctly. - We can't simply define LARGE_OFF_T to be 9223372036854775807, - since some C++ compilers masquerading as C compilers - incorrectly reject 9223372036854775807. */ -#define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62)) - int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721 - && LARGE_OFF_T % 2147483647 == 1) - ? 1 : -1]; -int -main () -{ - - ; - return 0; -} -_ACEOF -if ac_fn_c_try_compile "$LINENO"; then : - ac_cv_sys_file_offset_bits=no; break -fi -rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext - cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ -#define _FILE_OFFSET_BITS 64 -#include - /* Check that off_t can represent 2**63 - 1 correctly. - We can't simply define LARGE_OFF_T to be 9223372036854775807, - since some C++ compilers masquerading as C compilers - incorrectly reject 9223372036854775807. */ -#define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62)) - int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721 - && LARGE_OFF_T % 2147483647 == 1) - ? 1 : -1]; -int -main () -{ - - ; - return 0; -} -_ACEOF -if ac_fn_c_try_compile "$LINENO"; then : - ac_cv_sys_file_offset_bits=64; break -fi -rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext - ac_cv_sys_file_offset_bits=unknown - break -done -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_sys_file_offset_bits" >&5 -$as_echo "$ac_cv_sys_file_offset_bits" >&6; } -case $ac_cv_sys_file_offset_bits in #( - no | unknown) ;; - *) -cat >>confdefs.h <<_ACEOF -#define _FILE_OFFSET_BITS $ac_cv_sys_file_offset_bits -_ACEOF -;; -esac -rm -rf conftest* - if test $ac_cv_sys_file_offset_bits = unknown; then - { $as_echo "$as_me:${as_lineno-$LINENO}: checking for _LARGE_FILES value needed for large files" >&5 -$as_echo_n "checking for _LARGE_FILES value needed for large files... " >&6; } -if ${ac_cv_sys_large_files+:} false; then : - $as_echo_n "(cached) " >&6 -else - while :; do - cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ -#include - /* Check that off_t can represent 2**63 - 1 correctly. - We can't simply define LARGE_OFF_T to be 9223372036854775807, - since some C++ compilers masquerading as C compilers - incorrectly reject 9223372036854775807. */ -#define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62)) - int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721 - && LARGE_OFF_T % 2147483647 == 1) - ? 1 : -1]; -int -main () -{ - - ; - return 0; -} -_ACEOF -if ac_fn_c_try_compile "$LINENO"; then : - ac_cv_sys_large_files=no; break -fi -rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext - cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ -#define _LARGE_FILES 1 -#include - /* Check that off_t can represent 2**63 - 1 correctly. - We can't simply define LARGE_OFF_T to be 9223372036854775807, - since some C++ compilers masquerading as C compilers - incorrectly reject 9223372036854775807. */ -#define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62)) - int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721 - && LARGE_OFF_T % 2147483647 == 1) - ? 1 : -1]; -int -main () -{ - - ; - return 0; -} -_ACEOF -if ac_fn_c_try_compile "$LINENO"; then : - ac_cv_sys_large_files=1; break -fi -rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext - ac_cv_sys_large_files=unknown - break -done -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_sys_large_files" >&5 -$as_echo "$ac_cv_sys_large_files" >&6; } -case $ac_cv_sys_large_files in #( - no | unknown) ;; - *) -cat >>confdefs.h <<_ACEOF -#define _LARGE_FILES $ac_cv_sys_large_files -_ACEOF -;; -esac -rm -rf conftest* - fi - - -fi - - -######### -# Check for needed/wanted data types -ac_fn_c_check_type "$LINENO" "int8_t" "ac_cv_type_int8_t" "$ac_includes_default" -if test "x$ac_cv_type_int8_t" = xyes; then : - -cat >>confdefs.h <<_ACEOF -#define HAVE_INT8_T 1 -_ACEOF - - -fi -ac_fn_c_check_type "$LINENO" "int16_t" "ac_cv_type_int16_t" "$ac_includes_default" -if test "x$ac_cv_type_int16_t" = xyes; then : - -cat >>confdefs.h <<_ACEOF -#define HAVE_INT16_T 1 -_ACEOF - - -fi -ac_fn_c_check_type "$LINENO" "int32_t" "ac_cv_type_int32_t" "$ac_includes_default" -if test "x$ac_cv_type_int32_t" = xyes; then : - -cat >>confdefs.h <<_ACEOF -#define HAVE_INT32_T 1 -_ACEOF - - -fi -ac_fn_c_check_type "$LINENO" "int64_t" "ac_cv_type_int64_t" "$ac_includes_default" -if test "x$ac_cv_type_int64_t" = xyes; then : - -cat >>confdefs.h <<_ACEOF -#define HAVE_INT64_T 1 -_ACEOF - - -fi -ac_fn_c_check_type "$LINENO" "intptr_t" "ac_cv_type_intptr_t" "$ac_includes_default" -if test "x$ac_cv_type_intptr_t" = xyes; then : - -cat >>confdefs.h <<_ACEOF -#define HAVE_INTPTR_T 1 -_ACEOF - - -fi -ac_fn_c_check_type "$LINENO" "uint8_t" "ac_cv_type_uint8_t" "$ac_includes_default" -if test "x$ac_cv_type_uint8_t" = xyes; then : - -cat >>confdefs.h <<_ACEOF -#define HAVE_UINT8_T 1 -_ACEOF - - -fi -ac_fn_c_check_type "$LINENO" "uint16_t" "ac_cv_type_uint16_t" "$ac_includes_default" -if test "x$ac_cv_type_uint16_t" = xyes; then : - -cat >>confdefs.h <<_ACEOF -#define HAVE_UINT16_T 1 -_ACEOF - - -fi -ac_fn_c_check_type "$LINENO" "uint32_t" "ac_cv_type_uint32_t" "$ac_includes_default" -if test "x$ac_cv_type_uint32_t" = xyes; then : - -cat >>confdefs.h <<_ACEOF -#define HAVE_UINT32_T 1 -_ACEOF - - -fi -ac_fn_c_check_type "$LINENO" "uint64_t" "ac_cv_type_uint64_t" "$ac_includes_default" -if test "x$ac_cv_type_uint64_t" = xyes; then : - -cat >>confdefs.h <<_ACEOF -#define HAVE_UINT64_T 1 -_ACEOF - - -fi -ac_fn_c_check_type "$LINENO" "uintptr_t" "ac_cv_type_uintptr_t" "$ac_includes_default" -if test "x$ac_cv_type_uintptr_t" = xyes; then : - -cat >>confdefs.h <<_ACEOF -#define HAVE_UINTPTR_T 1 -_ACEOF - - -fi - - -######### -# Check for needed/wanted headers -for ac_header in sys/types.h stdlib.h stdint.h inttypes.h malloc.h -do : - as_ac_Header=`$as_echo "ac_cv_header_$ac_header" | $as_tr_sh` -ac_fn_c_check_header_mongrel "$LINENO" "$ac_header" "$as_ac_Header" "$ac_includes_default" -if eval test \"x\$"$as_ac_Header"\" = x"yes"; then : - cat >>confdefs.h <<_ACEOF -#define `$as_echo "HAVE_$ac_header" | $as_tr_cpp` 1 -_ACEOF - -fi - -done - - -######### -# Figure out whether or not we have these functions -# -for ac_func in fdatasync gmtime_r isnan localtime_r localtime_s malloc_usable_size strchrnul usleep utime -do : - as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh` -ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var" -if eval test \"x\$"$as_ac_var"\" = x"yes"; then : - cat >>confdefs.h <<_ACEOF -#define `$as_echo "HAVE_$ac_func" | $as_tr_cpp` 1 -_ACEOF - -fi -done - - -######### -# By default, we use the amalgamation (this may be changed below...) -# -USE_AMALGAMATION=1 - -######### -# See whether we can run specific tclsh versions known to work well; -# if not, then we fall back to plain tclsh. -# TODO: try other versions before falling back? -# -for ac_prog in tclsh8.6 tclsh8.5 tclsh -do - # Extract the first word of "$ac_prog", so it can be a program name with args. -set dummy $ac_prog; ac_word=$2 -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5 -$as_echo_n "checking for $ac_word... " >&6; } -if ${ac_cv_prog_TCLSH_CMD+:} false; then : - $as_echo_n "(cached) " >&6 -else - if test -n "$TCLSH_CMD"; then - ac_cv_prog_TCLSH_CMD="$TCLSH_CMD" # Let the user override the test. -else -as_save_IFS=$IFS; IFS=$PATH_SEPARATOR -for as_dir in $PATH -do - IFS=$as_save_IFS - test -z "$as_dir" && as_dir=. - for ac_exec_ext in '' $ac_executable_extensions; do - if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then - ac_cv_prog_TCLSH_CMD="$ac_prog" - $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5 - break 2 - fi -done - done -IFS=$as_save_IFS - -fi -fi -TCLSH_CMD=$ac_cv_prog_TCLSH_CMD -if test -n "$TCLSH_CMD"; then - { $as_echo "$as_me:${as_lineno-$LINENO}: result: $TCLSH_CMD" >&5 -$as_echo "$TCLSH_CMD" >&6; } -else - { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 -$as_echo "no" >&6; } -fi - - - test -n "$TCLSH_CMD" && break -done -test -n "$TCLSH_CMD" || TCLSH_CMD="none" - -if test "$TCLSH_CMD" = "none"; then - # If we can't find a local tclsh, then building the amalgamation will fail. - # We act as though --disable-amalgamation has been used. - echo "Warning: can't find tclsh - defaulting to non-amalgamation build." - USE_AMALGAMATION=0 - TCLSH_CMD="tclsh" -fi - - - -if test "x${TCLLIBDIR+set}" != "xset" ; then - TCLLIBDIR='$(libdir)' - for i in `echo 'puts stdout $auto_path' | ${TCLSH_CMD}` ; do - TCLLIBDIR=$i - break - done - TCLLIBDIR="${TCLLIBDIR}/sqlite3" -fi - -######### -# Set up an appropriate program prefix -# -if test "$program_prefix" = "NONE"; then - program_prefix="" -fi - - -VERSION=`cat $srcdir/VERSION | sed 's/^\([0-9]*\.*[0-9]*\).*/\1/'` -{ $as_echo "$as_me:${as_lineno-$LINENO}: Version set to $VERSION" >&5 -$as_echo "$as_me: Version set to $VERSION" >&6;} - -RELEASE=`cat $srcdir/VERSION` -{ $as_echo "$as_me:${as_lineno-$LINENO}: Release set to $RELEASE" >&5 -$as_echo "$as_me: Release set to $RELEASE" >&6;} - -VERSION_NUMBER=`cat $srcdir/VERSION \ - | sed 's/[^0-9]/ /g' \ - | awk '{printf "%d%03d%03d",$1,$2,$3}'` -{ $as_echo "$as_me:${as_lineno-$LINENO}: Version number set to $VERSION_NUMBER" >&5 -$as_echo "$as_me: Version number set to $VERSION_NUMBER" >&6;} - - -######### -# Locate a compiler for the build machine. This compiler should -# generate command-line programs that run on the build machine. -# -if test x"$cross_compiling" = xno; then - BUILD_CC=$CC - BUILD_CFLAGS=$CFLAGS -else - if test "${BUILD_CC+set}" != set; then - for ac_prog in gcc cc cl -do - # Extract the first word of "$ac_prog", so it can be a program name with args. -set dummy $ac_prog; ac_word=$2 -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5 -$as_echo_n "checking for $ac_word... " >&6; } -if ${ac_cv_prog_BUILD_CC+:} false; then : - $as_echo_n "(cached) " >&6 -else - if test -n "$BUILD_CC"; then - ac_cv_prog_BUILD_CC="$BUILD_CC" # Let the user override the test. -else -as_save_IFS=$IFS; IFS=$PATH_SEPARATOR -for as_dir in $PATH -do - IFS=$as_save_IFS - test -z "$as_dir" && as_dir=. - for ac_exec_ext in '' $ac_executable_extensions; do - if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then - ac_cv_prog_BUILD_CC="$ac_prog" - $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5 - break 2 - fi -done - done -IFS=$as_save_IFS - -fi -fi -BUILD_CC=$ac_cv_prog_BUILD_CC -if test -n "$BUILD_CC"; then - { $as_echo "$as_me:${as_lineno-$LINENO}: result: $BUILD_CC" >&5 -$as_echo "$BUILD_CC" >&6; } -else - { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 -$as_echo "no" >&6; } -fi - - - test -n "$BUILD_CC" && break -done - - fi - if test "${BUILD_CFLAGS+set}" != set; then - BUILD_CFLAGS="-g" - fi -fi - - -########## -# Do we want to support multithreaded use of sqlite -# -# Check whether --enable-threadsafe was given. -if test "${enable_threadsafe+set}" = set; then : - enableval=$enable_threadsafe; -else - enable_threadsafe=yes -fi - -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether to support threadsafe operation" >&5 -$as_echo_n "checking whether to support threadsafe operation... " >&6; } -if test "$enable_threadsafe" = "no"; then - SQLITE_THREADSAFE=0 - { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 -$as_echo "no" >&6; } -else - SQLITE_THREADSAFE=1 - { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5 -$as_echo "yes" >&6; } -fi - - -if test "$SQLITE_THREADSAFE" = "1"; then - { $as_echo "$as_me:${as_lineno-$LINENO}: checking for library containing pthread_create" >&5 -$as_echo_n "checking for library containing pthread_create... " >&6; } -if ${ac_cv_search_pthread_create+:} false; then : - $as_echo_n "(cached) " >&6 -else - ac_func_search_save_LIBS=$LIBS -cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ - -/* Override any GCC internal prototype to avoid an error. - Use char because int might match the return type of a GCC - builtin and then its argument prototype would still apply. */ -#ifdef __cplusplus -extern "C" -#endif -char pthread_create (); -int -main () -{ -return pthread_create (); - ; - return 0; -} -_ACEOF -for ac_lib in '' pthread; do - if test -z "$ac_lib"; then - ac_res="none required" - else - ac_res=-l$ac_lib - LIBS="-l$ac_lib $ac_func_search_save_LIBS" - fi - if ac_fn_c_try_link "$LINENO"; then : - ac_cv_search_pthread_create=$ac_res -fi -rm -f core conftest.err conftest.$ac_objext \ - conftest$ac_exeext - if ${ac_cv_search_pthread_create+:} false; then : - break -fi -done -if ${ac_cv_search_pthread_create+:} false; then : - -else - ac_cv_search_pthread_create=no -fi -rm conftest.$ac_ext -LIBS=$ac_func_search_save_LIBS -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_search_pthread_create" >&5 -$as_echo "$ac_cv_search_pthread_create" >&6; } -ac_res=$ac_cv_search_pthread_create -if test "$ac_res" != no; then : - test "$ac_res" = "none required" || LIBS="$ac_res $LIBS" - -fi - - { $as_echo "$as_me:${as_lineno-$LINENO}: checking for library containing pthread_mutexattr_init" >&5 -$as_echo_n "checking for library containing pthread_mutexattr_init... " >&6; } -if ${ac_cv_search_pthread_mutexattr_init+:} false; then : - $as_echo_n "(cached) " >&6 -else - ac_func_search_save_LIBS=$LIBS -cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ - -/* Override any GCC internal prototype to avoid an error. - Use char because int might match the return type of a GCC - builtin and then its argument prototype would still apply. */ -#ifdef __cplusplus -extern "C" -#endif -char pthread_mutexattr_init (); -int -main () -{ -return pthread_mutexattr_init (); - ; - return 0; -} -_ACEOF -for ac_lib in '' pthread; do - if test -z "$ac_lib"; then - ac_res="none required" - else - ac_res=-l$ac_lib - LIBS="-l$ac_lib $ac_func_search_save_LIBS" - fi - if ac_fn_c_try_link "$LINENO"; then : - ac_cv_search_pthread_mutexattr_init=$ac_res -fi -rm -f core conftest.err conftest.$ac_objext \ - conftest$ac_exeext - if ${ac_cv_search_pthread_mutexattr_init+:} false; then : - break -fi -done -if ${ac_cv_search_pthread_mutexattr_init+:} false; then : - -else - ac_cv_search_pthread_mutexattr_init=no -fi -rm conftest.$ac_ext -LIBS=$ac_func_search_save_LIBS -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_search_pthread_mutexattr_init" >&5 -$as_echo "$ac_cv_search_pthread_mutexattr_init" >&6; } -ac_res=$ac_cv_search_pthread_mutexattr_init -if test "$ac_res" != no; then : - test "$ac_res" = "none required" || LIBS="$ac_res $LIBS" - -fi - -fi - -########## -# Which crypto library do we use -# - -# Check whether --with-crypto-lib was given. -if test "${with_crypto_lib+set}" = set; then : - withval=$with_crypto_lib; crypto_lib=$withval -fi - -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for crypto library to use" >&5 -$as_echo_n "checking for crypto library to use... " >&6; } -if test "$crypto_lib" = "none"; then - { $as_echo "$as_me:${as_lineno-$LINENO}: result: none" >&5 -$as_echo "none" >&6; } -else - if test "$crypto_lib" = "commoncrypto"; then - CFLAGS+=" -DSQLCIPHER_CRYPTO_CC" - BUILD_CFLAGS+=" -DSQLCIPHER_CRYPTO_CC" - { $as_echo "$as_me:${as_lineno-$LINENO}: result: commoncrypto" >&5 -$as_echo "commoncrypto" >&6; } - else - if test "$crypto_lib" = "libtomcrypt"; then - CFLAGS+=" -DSQLCIPHER_CRYPTO_LIBTOMCRYPT" - BUILD_CFLAGS+=" -DSQLCIPHER_CRYPTO_LIBTOMCRYPT" - { $as_echo "$as_me:${as_lineno-$LINENO}: result: libtomcrypt" >&5 -$as_echo "libtomcrypt" >&6; } - { $as_echo "$as_me:${as_lineno-$LINENO}: checking for register_cipher in -ltomcrypt" >&5 -$as_echo_n "checking for register_cipher in -ltomcrypt... " >&6; } -if ${ac_cv_lib_tomcrypt_register_cipher+:} false; then : - $as_echo_n "(cached) " >&6 -else - ac_check_lib_save_LIBS=$LIBS -LIBS="-ltomcrypt $LIBS" -cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ - -/* Override any GCC internal prototype to avoid an error. - Use char because int might match the return type of a GCC - builtin and then its argument prototype would still apply. */ -#ifdef __cplusplus -extern "C" -#endif -char register_cipher (); -int -main () -{ -return register_cipher (); - ; - return 0; -} -_ACEOF -if ac_fn_c_try_link "$LINENO"; then : - ac_cv_lib_tomcrypt_register_cipher=yes -else - ac_cv_lib_tomcrypt_register_cipher=no -fi -rm -f core conftest.err conftest.$ac_objext \ - conftest$ac_exeext conftest.$ac_ext -LIBS=$ac_check_lib_save_LIBS -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_tomcrypt_register_cipher" >&5 -$as_echo "$ac_cv_lib_tomcrypt_register_cipher" >&6; } -if test "x$ac_cv_lib_tomcrypt_register_cipher" = xyes; then : - cat >>confdefs.h <<_ACEOF -#define HAVE_LIBTOMCRYPT 1 -_ACEOF - - LIBS="-ltomcrypt $LIBS" - -else - as_fn_error $? "Library crypto not found. Install libtomcrypt!\"" "$LINENO" 5 -fi - - else - CFLAGS+=" -DSQLCIPHER_CRYPTO_OPENSSL" - BUILD_CFLAGS+=" -DSQLCIPHER_CRYPTO_OPENSSL" - { $as_echo "$as_me:${as_lineno-$LINENO}: result: openssl" >&5 -$as_echo "openssl" >&6; } - { $as_echo "$as_me:${as_lineno-$LINENO}: checking for HMAC_Init_ex in -lcrypto" >&5 -$as_echo_n "checking for HMAC_Init_ex in -lcrypto... " >&6; } -if ${ac_cv_lib_crypto_HMAC_Init_ex+:} false; then : - $as_echo_n "(cached) " >&6 -else - ac_check_lib_save_LIBS=$LIBS -LIBS="-lcrypto $LIBS" -cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ - -/* Override any GCC internal prototype to avoid an error. - Use char because int might match the return type of a GCC - builtin and then its argument prototype would still apply. */ -#ifdef __cplusplus -extern "C" -#endif -char HMAC_Init_ex (); -int -main () -{ -return HMAC_Init_ex (); - ; - return 0; -} -_ACEOF -if ac_fn_c_try_link "$LINENO"; then : - ac_cv_lib_crypto_HMAC_Init_ex=yes -else - ac_cv_lib_crypto_HMAC_Init_ex=no -fi -rm -f core conftest.err conftest.$ac_objext \ - conftest$ac_exeext conftest.$ac_ext -LIBS=$ac_check_lib_save_LIBS -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_crypto_HMAC_Init_ex" >&5 -$as_echo "$ac_cv_lib_crypto_HMAC_Init_ex" >&6; } -if test "x$ac_cv_lib_crypto_HMAC_Init_ex" = xyes; then : - cat >>confdefs.h <<_ACEOF -#define HAVE_LIBCRYPTO 1 -_ACEOF - - LIBS="-lcrypto $LIBS" - -else - as_fn_error $? "Library crypto not found. Install openssl!\"" "$LINENO" 5 -fi - - fi - fi -fi - -########## -# Do we want to allow a connection created in one thread to be used -# in another thread. This does not work on many Linux systems (ex: RedHat 9) -# due to bugs in the threading implementations. This is thus off by default. -# -# Check whether --enable-cross-thread-connections was given. -if test "${enable_cross_thread_connections+set}" = set; then : - enableval=$enable_cross_thread_connections; -else - enable_xthreadconnect=no -fi - -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether to allow connections to be shared across threads" >&5 -$as_echo_n "checking whether to allow connections to be shared across threads... " >&6; } -if test "$enable_xthreadconnect" = "no"; then - XTHREADCONNECT='' - { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 -$as_echo "no" >&6; } -else - XTHREADCONNECT='-DSQLITE_ALLOW_XTHREAD_CONNECT=1' - { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5 -$as_echo "yes" >&6; } -fi - - -########## -# Do we want to support release -# -# Check whether --enable-releasemode was given. -if test "${enable_releasemode+set}" = set; then : - enableval=$enable_releasemode; -else - enable_releasemode=no -fi - -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether to support shared library linked as release mode or not" >&5 -$as_echo_n "checking whether to support shared library linked as release mode or not... " >&6; } -if test "$enable_releasemode" = "no"; then - ALLOWRELEASE="" - { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 -$as_echo "no" >&6; } -else - ALLOWRELEASE="-release `cat $srcdir/VERSION`" - { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5 -$as_echo "yes" >&6; } -fi - - -########## -# Do we want temporary databases in memory -# -# Check whether --enable-tempstore was given. -if test "${enable_tempstore+set}" = set; then : - enableval=$enable_tempstore; -else - enable_tempstore=no -fi - -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether to use an in-ram database for temporary tables" >&5 -$as_echo_n "checking whether to use an in-ram database for temporary tables... " >&6; } -case "$enable_tempstore" in - never ) - TEMP_STORE=0 - { $as_echo "$as_me:${as_lineno-$LINENO}: result: never" >&5 -$as_echo "never" >&6; } - ;; - no ) - TEMP_STORE=1 - { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 -$as_echo "no" >&6; } - ;; - yes ) - TEMP_STORE=2 - { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5 -$as_echo "yes" >&6; } - ;; - always ) - TEMP_STORE=3 - { $as_echo "$as_me:${as_lineno-$LINENO}: result: always" >&5 -$as_echo "always" >&6; } - ;; - * ) - TEMP_STORE=1 - { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 -$as_echo "no" >&6; } - ;; -esac - - - -########### -# Lots of things are different if we are compiling for Windows using -# the CYGWIN environment. So check for that special case and handle -# things accordingly. -# -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking if executables have the .exe suffix" >&5 -$as_echo_n "checking if executables have the .exe suffix... " >&6; } -if test "$config_BUILD_EXEEXT" = ".exe"; then - CYGWIN=yes - { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5 -$as_echo "yes" >&6; } -else - { $as_echo "$as_me:${as_lineno-$LINENO}: result: unknown" >&5 -$as_echo "unknown" >&6; } -fi -if test "$CYGWIN" != "yes"; then - -case $host_os in - *cygwin* ) CYGWIN=yes;; - * ) CYGWIN=no;; -esac - -fi -if test "$CYGWIN" = "yes"; then - BUILD_EXEEXT=.exe -else - BUILD_EXEEXT=$EXEEXT -fi -if test x"$cross_compiling" = xno; then - TARGET_EXEEXT=$BUILD_EXEEXT -else - TARGET_EXEEXT=$config_TARGET_EXEEXT -fi -if test "$TARGET_EXEEXT" = ".exe"; then - SQLITE_OS_UNIX=0 - SQLITE_OS_WIN=1 - CFLAGS="$CFLAGS -DSQLITE_OS_WIN=1" -else - SQLITE_OS_UNIX=1 - SQLITE_OS_WIN=0 - CFLAGS="$CFLAGS -DSQLITE_OS_UNIX=1" -fi - - - - - - -########## -# Figure out all the parameters needed to compile against Tcl. -# -# This code is derived from the SC_PATH_TCLCONFIG and SC_LOAD_TCLCONFIG -# macros in the in the tcl.m4 file of the standard TCL distribution. -# Those macros could not be used directly since we have to make some -# minor changes to accomodate systems that do not have TCL installed. -# -# Check whether --enable-tcl was given. -if test "${enable_tcl+set}" = set; then : - enableval=$enable_tcl; use_tcl=$enableval -else - use_tcl=yes -fi - -if test "${use_tcl}" = "yes" ; then - -# Check whether --with-tcl was given. -if test "${with_tcl+set}" = set; then : - withval=$with_tcl; with_tclconfig=${withval} -fi - - { $as_echo "$as_me:${as_lineno-$LINENO}: checking for Tcl configuration" >&5 -$as_echo_n "checking for Tcl configuration... " >&6; } - if ${ac_cv_c_tclconfig+:} false; then : - $as_echo_n "(cached) " >&6 -else - - # First check to see if --with-tcl was specified. - if test x"${with_tclconfig}" != x ; then - if test -f "${with_tclconfig}/tclConfig.sh" ; then - ac_cv_c_tclconfig=`(cd ${with_tclconfig}; pwd)` - else - as_fn_error $? "${with_tclconfig} directory doesn't contain tclConfig.sh" "$LINENO" 5 - fi - fi - - # Start autosearch by asking tclsh - if test x"${ac_cv_c_tclconfig}" = x ; then - if test x"$cross_compiling" = xno; then - for i in `echo 'puts stdout $auto_path' | ${TCLSH_CMD}` - do - if test -f "$i/tclConfig.sh" ; then - ac_cv_c_tclconfig="$i" - break - fi - done - fi - fi - - # On ubuntu 14.10, $auto_path on tclsh is not quite correct. - # So try again after applying corrections. - if test x"${ac_cv_c_tclconfig}" = x ; then - if test x"$cross_compiling" = xno; then - for i in `echo 'puts stdout $auto_path' | ${TCLSH_CMD} | sed 's,/tcltk/tcl,/tcl,g'` - do - if test -f "$i/tclConfig.sh" ; then - ac_cv_c_tclconfig="$i" - break - fi - done - fi - fi - - # then check for a private Tcl installation - if test x"${ac_cv_c_tclconfig}" = x ; then - for i in \ - ../tcl \ - `ls -dr ../tcl[8-9].[0-9].[0-9]* 2>/dev/null` \ - `ls -dr ../tcl[8-9].[0-9] 2>/dev/null` \ - `ls -dr ../tcl[8-9].[0-9]* 2>/dev/null` \ - ../../tcl \ - `ls -dr ../../tcl[8-9].[0-9].[0-9]* 2>/dev/null` \ - `ls -dr ../../tcl[8-9].[0-9] 2>/dev/null` \ - `ls -dr ../../tcl[8-9].[0-9]* 2>/dev/null` \ - ../../../tcl \ - `ls -dr ../../../tcl[8-9].[0-9].[0-9]* 2>/dev/null` \ - `ls -dr ../../../tcl[8-9].[0-9] 2>/dev/null` \ - `ls -dr ../../../tcl[8-9].[0-9]* 2>/dev/null` - do - if test -f "$i/unix/tclConfig.sh" ; then - ac_cv_c_tclconfig=`(cd $i/unix; pwd)` - break - fi - done - fi - - # check in a few common install locations - if test x"${ac_cv_c_tclconfig}" = x ; then - for i in \ - `ls -d ${libdir} 2>/dev/null` \ - `ls -d /usr/local/lib 2>/dev/null` \ - `ls -d /usr/contrib/lib 2>/dev/null` \ - `ls -d /usr/lib 2>/dev/null` - do - if test -f "$i/tclConfig.sh" ; then - ac_cv_c_tclconfig=`(cd $i; pwd)` - break - fi - done - fi - - # check in a few other private locations - if test x"${ac_cv_c_tclconfig}" = x ; then - for i in \ - ${srcdir}/../tcl \ - `ls -dr ${srcdir}/../tcl[8-9].[0-9].[0-9]* 2>/dev/null` \ - `ls -dr ${srcdir}/../tcl[8-9].[0-9] 2>/dev/null` \ - `ls -dr ${srcdir}/../tcl[8-9].[0-9]* 2>/dev/null` - do - if test -f "$i/unix/tclConfig.sh" ; then - ac_cv_c_tclconfig=`(cd $i/unix; pwd)` - break - fi - done - fi - -fi - - - if test x"${ac_cv_c_tclconfig}" = x ; then - use_tcl=no - { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: Can't find Tcl configuration definitions" >&5 -$as_echo "$as_me: WARNING: Can't find Tcl configuration definitions" >&2;} - { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: *** Without Tcl the regression tests cannot be executed ***" >&5 -$as_echo "$as_me: WARNING: *** Without Tcl the regression tests cannot be executed ***" >&2;} - { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: *** Consider using --with-tcl=... to define location of Tcl ***" >&5 -$as_echo "$as_me: WARNING: *** Consider using --with-tcl=... to define location of Tcl ***" >&2;} - else - TCL_BIN_DIR=${ac_cv_c_tclconfig} - { $as_echo "$as_me:${as_lineno-$LINENO}: result: found $TCL_BIN_DIR/tclConfig.sh" >&5 -$as_echo "found $TCL_BIN_DIR/tclConfig.sh" >&6; } - - { $as_echo "$as_me:${as_lineno-$LINENO}: checking for existence of $TCL_BIN_DIR/tclConfig.sh" >&5 -$as_echo_n "checking for existence of $TCL_BIN_DIR/tclConfig.sh... " >&6; } - if test -f "$TCL_BIN_DIR/tclConfig.sh" ; then - { $as_echo "$as_me:${as_lineno-$LINENO}: result: loading" >&5 -$as_echo "loading" >&6; } - . $TCL_BIN_DIR/tclConfig.sh - else - { $as_echo "$as_me:${as_lineno-$LINENO}: result: file not found" >&5 -$as_echo "file not found" >&6; } - fi - - # - # If the TCL_BIN_DIR is the build directory (not the install directory), - # then set the common variable name to the value of the build variables. - # For example, the variable TCL_LIB_SPEC will be set to the value - # of TCL_BUILD_LIB_SPEC. An extension should make use of TCL_LIB_SPEC - # instead of TCL_BUILD_LIB_SPEC since it will work with both an - # installed and uninstalled version of Tcl. - # - - if test -f $TCL_BIN_DIR/Makefile ; then - TCL_LIB_SPEC=${TCL_BUILD_LIB_SPEC} - TCL_STUB_LIB_SPEC=${TCL_BUILD_STUB_LIB_SPEC} - TCL_STUB_LIB_PATH=${TCL_BUILD_STUB_LIB_PATH} - fi - - # - # eval is required to do the TCL_DBGX substitution - # - - eval "TCL_LIB_FILE=\"${TCL_LIB_FILE}\"" - eval "TCL_LIB_FLAG=\"${TCL_LIB_FLAG}\"" - eval "TCL_LIB_SPEC=\"${TCL_LIB_SPEC}\"" - - eval "TCL_STUB_LIB_FILE=\"${TCL_STUB_LIB_FILE}\"" - eval "TCL_STUB_LIB_FLAG=\"${TCL_STUB_LIB_FLAG}\"" - eval "TCL_STUB_LIB_SPEC=\"${TCL_STUB_LIB_SPEC}\"" - - - - - - - - - - - - - - - fi -fi -if test "${use_tcl}" = "no" ; then - HAVE_TCL="" -else - HAVE_TCL=1 -fi - - -########## -# Figure out what C libraries are required to compile programs -# that use "readline()" library. -# -TARGET_READLINE_LIBS="" -TARGET_READLINE_INC="" -TARGET_HAVE_READLINE=0 -TARGET_HAVE_EDITLINE=0 -# Check whether --enable-editline was given. -if test "${enable_editline+set}" = set; then : - enableval=$enable_editline; with_editline=$enableval -else - with_editline=auto -fi - -# Check whether --enable-readline was given. -if test "${enable_readline+set}" = set; then : - enableval=$enable_readline; with_readline=$enableval -else - with_readline=auto -fi - - -if test x"$with_editline" != xno; then - sLIBS=$LIBS - LIBS="" - TARGET_HAVE_EDITLINE=1 - { $as_echo "$as_me:${as_lineno-$LINENO}: checking for library containing readline" >&5 -$as_echo_n "checking for library containing readline... " >&6; } -if ${ac_cv_search_readline+:} false; then : - $as_echo_n "(cached) " >&6 -else - ac_func_search_save_LIBS=$LIBS -cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ - -/* Override any GCC internal prototype to avoid an error. - Use char because int might match the return type of a GCC - builtin and then its argument prototype would still apply. */ -#ifdef __cplusplus -extern "C" -#endif -char readline (); -int -main () -{ -return readline (); - ; - return 0; -} -_ACEOF -for ac_lib in '' edit; do - if test -z "$ac_lib"; then - ac_res="none required" - else - ac_res=-l$ac_lib - LIBS="-l$ac_lib $ac_func_search_save_LIBS" - fi - if ac_fn_c_try_link "$LINENO"; then : - ac_cv_search_readline=$ac_res -fi -rm -f core conftest.err conftest.$ac_objext \ - conftest$ac_exeext - if ${ac_cv_search_readline+:} false; then : - break -fi -done -if ${ac_cv_search_readline+:} false; then : - -else - ac_cv_search_readline=no -fi -rm conftest.$ac_ext -LIBS=$ac_func_search_save_LIBS -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_search_readline" >&5 -$as_echo "$ac_cv_search_readline" >&6; } -ac_res=$ac_cv_search_readline -if test "$ac_res" != no; then : - test "$ac_res" = "none required" || LIBS="$ac_res $LIBS" - with_readline=no -else - TARGET_HAVE_EDITLINE=0 -fi - - TARGET_READLINE_LIBS=$LIBS - LIBS=$sLIBS -fi -if test x"$with_readline" != xno; then - found="yes" - - -# Check whether --with-readline-lib was given. -if test "${with_readline_lib+set}" = set; then : - withval=$with_readline_lib; with_readline_lib=$withval -else - with_readline_lib="auto" -fi - - if test "x$with_readline_lib" = xauto; then - save_LIBS="$LIBS" - LIBS="" - { $as_echo "$as_me:${as_lineno-$LINENO}: checking for library containing tgetent" >&5 -$as_echo_n "checking for library containing tgetent... " >&6; } -if ${ac_cv_search_tgetent+:} false; then : - $as_echo_n "(cached) " >&6 -else - ac_func_search_save_LIBS=$LIBS -cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ - -/* Override any GCC internal prototype to avoid an error. - Use char because int might match the return type of a GCC - builtin and then its argument prototype would still apply. */ -#ifdef __cplusplus -extern "C" -#endif -char tgetent (); -int -main () -{ -return tgetent (); - ; - return 0; -} -_ACEOF -for ac_lib in '' readline ncurses curses termcap; do - if test -z "$ac_lib"; then - ac_res="none required" - else - ac_res=-l$ac_lib - LIBS="-l$ac_lib $ac_func_search_save_LIBS" - fi - if ac_fn_c_try_link "$LINENO"; then : - ac_cv_search_tgetent=$ac_res -fi -rm -f core conftest.err conftest.$ac_objext \ - conftest$ac_exeext - if ${ac_cv_search_tgetent+:} false; then : - break -fi -done -if ${ac_cv_search_tgetent+:} false; then : - -else - ac_cv_search_tgetent=no -fi -rm conftest.$ac_ext -LIBS=$ac_func_search_save_LIBS -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_search_tgetent" >&5 -$as_echo "$ac_cv_search_tgetent" >&6; } -ac_res=$ac_cv_search_tgetent -if test "$ac_res" != no; then : - test "$ac_res" = "none required" || LIBS="$ac_res $LIBS" - term_LIBS="$LIBS" -else - term_LIBS="" -fi - - { $as_echo "$as_me:${as_lineno-$LINENO}: checking for readline in -lreadline" >&5 -$as_echo_n "checking for readline in -lreadline... " >&6; } -if ${ac_cv_lib_readline_readline+:} false; then : - $as_echo_n "(cached) " >&6 -else - ac_check_lib_save_LIBS=$LIBS -LIBS="-lreadline $LIBS" -cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ - -/* Override any GCC internal prototype to avoid an error. - Use char because int might match the return type of a GCC - builtin and then its argument prototype would still apply. */ -#ifdef __cplusplus -extern "C" -#endif -char readline (); -int -main () -{ -return readline (); - ; - return 0; -} -_ACEOF -if ac_fn_c_try_link "$LINENO"; then : - ac_cv_lib_readline_readline=yes -else - ac_cv_lib_readline_readline=no -fi -rm -f core conftest.err conftest.$ac_objext \ - conftest$ac_exeext conftest.$ac_ext -LIBS=$ac_check_lib_save_LIBS -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_readline_readline" >&5 -$as_echo "$ac_cv_lib_readline_readline" >&6; } -if test "x$ac_cv_lib_readline_readline" = xyes; then : - TARGET_READLINE_LIBS="-lreadline" -else - found="no" -fi - - TARGET_READLINE_LIBS="$TARGET_READLINE_LIBS $term_LIBS" - LIBS="$save_LIBS" - else - TARGET_READLINE_LIBS="$with_readline_lib" - fi - - -# Check whether --with-readline-inc was given. -if test "${with_readline_inc+set}" = set; then : - withval=$with_readline_inc; with_readline_inc=$withval -else - with_readline_inc="auto" -fi - - if test "x$with_readline_inc" = xauto; then - ac_fn_c_check_header_mongrel "$LINENO" "readline.h" "ac_cv_header_readline_h" "$ac_includes_default" -if test "x$ac_cv_header_readline_h" = xyes; then : - found="yes" -else - - found="no" - if test "$cross_compiling" != yes; then - for dir in /usr /usr/local /usr/local/readline /usr/contrib /mingw; do - for subdir in include include/readline; do - as_ac_File=`$as_echo "ac_cv_file_$dir/$subdir/readline.h" | $as_tr_sh` -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $dir/$subdir/readline.h" >&5 -$as_echo_n "checking for $dir/$subdir/readline.h... " >&6; } -if eval \${$as_ac_File+:} false; then : - $as_echo_n "(cached) " >&6 -else - test "$cross_compiling" = yes && - as_fn_error $? "cannot check for file existence when cross compiling" "$LINENO" 5 -if test -r "$dir/$subdir/readline.h"; then - eval "$as_ac_File=yes" -else - eval "$as_ac_File=no" -fi -fi -eval ac_res=\$$as_ac_File - { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_res" >&5 -$as_echo "$ac_res" >&6; } -if eval test \"x\$"$as_ac_File"\" = x"yes"; then : - found=yes -fi - - if test "$found" = "yes"; then - TARGET_READLINE_INC="-I$dir/$subdir" - break - fi - done - test "$found" = "yes" && break - done - fi - -fi - - - else - TARGET_READLINE_INC="$with_readline_inc" - fi - - if test x"$found" = xno; then - TARGET_READLINE_LIBS="" - TARGET_READLINE_INC="" - TARGET_HAVE_READLINE=0 - else - TARGET_HAVE_READLINE=1 - fi -fi - - - - - - -########## -# Figure out what C libraries are required to compile programs -# that use "fdatasync()" function. -# -{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for library containing fdatasync" >&5 -$as_echo_n "checking for library containing fdatasync... " >&6; } -if ${ac_cv_search_fdatasync+:} false; then : - $as_echo_n "(cached) " >&6 -else - ac_func_search_save_LIBS=$LIBS -cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ - -/* Override any GCC internal prototype to avoid an error. - Use char because int might match the return type of a GCC - builtin and then its argument prototype would still apply. */ -#ifdef __cplusplus -extern "C" -#endif -char fdatasync (); -int -main () -{ -return fdatasync (); - ; - return 0; -} -_ACEOF -for ac_lib in '' rt; do - if test -z "$ac_lib"; then - ac_res="none required" - else - ac_res=-l$ac_lib - LIBS="-l$ac_lib $ac_func_search_save_LIBS" - fi - if ac_fn_c_try_link "$LINENO"; then : - ac_cv_search_fdatasync=$ac_res -fi -rm -f core conftest.err conftest.$ac_objext \ - conftest$ac_exeext - if ${ac_cv_search_fdatasync+:} false; then : - break -fi -done -if ${ac_cv_search_fdatasync+:} false; then : - -else - ac_cv_search_fdatasync=no -fi -rm conftest.$ac_ext -LIBS=$ac_func_search_save_LIBS -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_search_fdatasync" >&5 -$as_echo "$ac_cv_search_fdatasync" >&6; } -ac_res=$ac_cv_search_fdatasync -if test "$ac_res" != no; then : - test "$ac_res" = "none required" || LIBS="$ac_res $LIBS" - -fi - - -######### -# check for debug enabled -# Check whether --enable-debug was given. -if test "${enable_debug+set}" = set; then : - enableval=$enable_debug; use_debug=$enableval -else - use_debug=no -fi - -if test "${use_debug}" = "yes" ; then - TARGET_DEBUG="-DSQLITE_DEBUG=1" -else - TARGET_DEBUG="-DNDEBUG" -fi - - -######### -# See whether we should use the amalgamation to build -# Check whether --enable-amalgamation was given. -if test "${enable_amalgamation+set}" = set; then : - enableval=$enable_amalgamation; use_amalgamation=$enableval -else - use_amalgamation=yes -fi - -if test "${use_amalgamation}" != "yes" ; then - USE_AMALGAMATION=0 -fi - - -######### -# See whether we should allow loadable extensions -# Check whether --enable-load-extension was given. -if test "${enable_load_extension+set}" = set; then : - enableval=$enable_load_extension; use_loadextension=$enableval -else - use_loadextension=yes -fi - -if test "${use_loadextension}" = "yes" ; then - OPT_FEATURE_FLAGS="" - { $as_echo "$as_me:${as_lineno-$LINENO}: checking for library containing dlopen" >&5 -$as_echo_n "checking for library containing dlopen... " >&6; } -if ${ac_cv_search_dlopen+:} false; then : - $as_echo_n "(cached) " >&6 -else - ac_func_search_save_LIBS=$LIBS -cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ - -/* Override any GCC internal prototype to avoid an error. - Use char because int might match the return type of a GCC - builtin and then its argument prototype would still apply. */ -#ifdef __cplusplus -extern "C" -#endif -char dlopen (); -int -main () -{ -return dlopen (); - ; - return 0; -} -_ACEOF -for ac_lib in '' dl; do - if test -z "$ac_lib"; then - ac_res="none required" - else - ac_res=-l$ac_lib - LIBS="-l$ac_lib $ac_func_search_save_LIBS" - fi - if ac_fn_c_try_link "$LINENO"; then : - ac_cv_search_dlopen=$ac_res -fi -rm -f core conftest.err conftest.$ac_objext \ - conftest$ac_exeext - if ${ac_cv_search_dlopen+:} false; then : - break -fi -done -if ${ac_cv_search_dlopen+:} false; then : - -else - ac_cv_search_dlopen=no -fi -rm conftest.$ac_ext -LIBS=$ac_func_search_save_LIBS -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_search_dlopen" >&5 -$as_echo "$ac_cv_search_dlopen" >&6; } -ac_res=$ac_cv_search_dlopen -if test "$ac_res" != no; then : - test "$ac_res" = "none required" || LIBS="$ac_res $LIBS" - -fi - -else - OPT_FEATURE_FLAGS="-DSQLITE_OMIT_LOAD_EXTENSION=1" -fi - -######### -# See whether we should enable Full Text Search extensions -# Check whether --enable-fts3 was given. -if test "${enable_fts3+set}" = set; then : - enableval=$enable_fts3; enable_fts3=yes -else - enable_fts3=no -fi - -if test "${enable_fts3}" = "yes" ; then - OPT_FEATURE_FLAGS+=" -DSQLITE_ENABLE_FTS3" -fi -# Check whether --enable-fts4 was given. -if test "${enable_fts4+set}" = set; then : - enableval=$enable_fts4; enable_fts4=yes -else - enable_fts4=no -fi - -if test "${enable_fts4}" = "yes" ; then - OPT_FEATURE_FLAGS+=" -DSQLITE_ENABLE_FTS4" - { $as_echo "$as_me:${as_lineno-$LINENO}: checking for library containing log" >&5 -$as_echo_n "checking for library containing log... " >&6; } -if ${ac_cv_search_log+:} false; then : - $as_echo_n "(cached) " >&6 -else - ac_func_search_save_LIBS=$LIBS -cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ - -/* Override any GCC internal prototype to avoid an error. - Use char because int might match the return type of a GCC - builtin and then its argument prototype would still apply. */ -#ifdef __cplusplus -extern "C" -#endif -char log (); -int -main () -{ -return log (); - ; - return 0; -} -_ACEOF -for ac_lib in '' m; do - if test -z "$ac_lib"; then - ac_res="none required" - else - ac_res=-l$ac_lib - LIBS="-l$ac_lib $ac_func_search_save_LIBS" - fi - if ac_fn_c_try_link "$LINENO"; then : - ac_cv_search_log=$ac_res -fi -rm -f core conftest.err conftest.$ac_objext \ - conftest$ac_exeext - if ${ac_cv_search_log+:} false; then : - break -fi -done -if ${ac_cv_search_log+:} false; then : - -else - ac_cv_search_log=no -fi -rm conftest.$ac_ext -LIBS=$ac_func_search_save_LIBS -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_search_log" >&5 -$as_echo "$ac_cv_search_log" >&6; } -ac_res=$ac_cv_search_log -if test "$ac_res" != no; then : - test "$ac_res" = "none required" || LIBS="$ac_res $LIBS" - -fi - -fi -# Check whether --enable-fts5 was given. -if test "${enable_fts5+set}" = set; then : - enableval=$enable_fts5; enable_fts5=yes -else - enable_fts5=no -fi - -if test "${enable_fts5}" = "yes" ; then - OPT_FEATURE_FLAGS+=" -DSQLITE_ENABLE_FTS5" - { $as_echo "$as_me:${as_lineno-$LINENO}: checking for library containing log" >&5 -$as_echo_n "checking for library containing log... " >&6; } -if ${ac_cv_search_log+:} false; then : - $as_echo_n "(cached) " >&6 -else - ac_func_search_save_LIBS=$LIBS -cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ - -/* Override any GCC internal prototype to avoid an error. - Use char because int might match the return type of a GCC - builtin and then its argument prototype would still apply. */ -#ifdef __cplusplus -extern "C" -#endif -char log (); -int -main () -{ -return log (); - ; - return 0; -} -_ACEOF -for ac_lib in '' m; do - if test -z "$ac_lib"; then - ac_res="none required" - else - ac_res=-l$ac_lib - LIBS="-l$ac_lib $ac_func_search_save_LIBS" - fi - if ac_fn_c_try_link "$LINENO"; then : - ac_cv_search_log=$ac_res -fi -rm -f core conftest.err conftest.$ac_objext \ - conftest$ac_exeext - if ${ac_cv_search_log+:} false; then : - break -fi -done -if ${ac_cv_search_log+:} false; then : - -else - ac_cv_search_log=no -fi -rm conftest.$ac_ext -LIBS=$ac_func_search_save_LIBS -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_search_log" >&5 -$as_echo "$ac_cv_search_log" >&6; } -ac_res=$ac_cv_search_log -if test "$ac_res" != no; then : - test "$ac_res" = "none required" || LIBS="$ac_res $LIBS" - -fi - -fi - -######### -# See whether we should enable JSON1 -# Check whether --enable-json1 was given. -if test "${enable_json1+set}" = set; then : - enableval=$enable_json1; enable_json1=yes -else - enable_json1=no -fi - -if test "${enable_json1}" = "yes" ; then - OPT_FEATURE_FLAGS+=" -DSQLITE_ENABLE_JSON1" -fi - -######### -# See whether we should enable RTREE -# Check whether --enable-rtree was given. -if test "${enable_rtree+set}" = set; then : - enableval=$enable_rtree; enable_rtree=yes -else - enable_rtree=no -fi - -if test "${enable_rtree}" = "yes" ; then - OPT_FEATURE_FLAGS+=" -DSQLITE_ENABLE_RTREE" -fi - -######### -# attempt to duplicate any OMITS and ENABLES into the $(OPT_FEATURE_FLAGS) parameter -for option in $CFLAGS $CPPFLAGS -do - case $option in - -DSQLITE_OMIT*) OPT_FEATURE_FLAGS="$OPT_FEATURE_FLAGS $option";; - -DSQLITE_ENABLE*) OPT_FEATURE_FLAGS="$OPT_FEATURE_FLAGS $option";; - esac -done - - - -# attempt to remove any OMITS and ENABLES from the $(CFLAGS) parameter -ac_temp_CFLAGS="" -for option in $CFLAGS -do - case $option in - -DSQLITE_OMIT*) ;; - -DSQLITE_ENABLE*) ;; - *) ac_temp_CFLAGS="$ac_temp_CFLAGS $option";; - esac -done -CFLAGS=$ac_temp_CFLAGS - - -# attempt to remove any OMITS and ENABLES from the $(CPPFLAGS) parameter -ac_temp_CPPFLAGS="" -for option in $CPPFLAGS -do - case $option in - -DSQLITE_OMIT*) ;; - -DSQLITE_ENABLE*) ;; - *) ac_temp_CPPFLAGS="$ac_temp_CPPFLAGS $option";; - esac -done -CPPFLAGS=$ac_temp_CPPFLAGS - - -# attempt to remove any OMITS and ENABLES from the $(BUILD_CFLAGS) parameter -ac_temp_BUILD_CFLAGS="" -for option in $BUILD_CFLAGS -do - case $option in - -DSQLITE_OMIT*) ;; - -DSQLITE_ENABLE*) ;; - *) ac_temp_BUILD_CFLAGS="$ac_temp_BUILD_CFLAGS $option";; - esac -done -BUILD_CFLAGS=$ac_temp_BUILD_CFLAGS - - -######### -# See whether we should use GCOV -# Check whether --enable-gcov was given. -if test "${enable_gcov+set}" = set; then : - enableval=$enable_gcov; use_gcov=$enableval -else - use_gcov=no -fi - -if test "${use_gcov}" = "yes" ; then - USE_GCOV=1 -else - USE_GCOV=0 -fi - - - -######### -# Output the config header -ac_config_headers="$ac_config_headers config.h" - - -######### -# Generate the output files. -# - -ac_config_files="$ac_config_files Makefile sqlcipher.pc" - -cat >confcache <<\_ACEOF -# This file is a shell script that caches the results of configure -# tests run on this system so they can be shared between configure -# scripts and configure runs, see configure's option --config-cache. -# It is not useful on other systems. If it contains results you don't -# want to keep, you may remove or edit it. -# -# config.status only pays attention to the cache file if you give it -# the --recheck option to rerun configure. -# -# `ac_cv_env_foo' variables (set or unset) will be overridden when -# loading this file, other *unset* `ac_cv_foo' will be assigned the -# following values. - -_ACEOF - -# The following way of writing the cache mishandles newlines in values, -# but we know of no workaround that is simple, portable, and efficient. -# So, we kill variables containing newlines. -# Ultrix sh set writes to stderr and can't be redirected directly, -# and sets the high bit in the cache file unless we assign to the vars. -( - for ac_var in `(set) 2>&1 | sed -n 's/^\([a-zA-Z_][a-zA-Z0-9_]*\)=.*/\1/p'`; do - eval ac_val=\$$ac_var - case $ac_val in #( - *${as_nl}*) - case $ac_var in #( - *_cv_*) { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: cache variable $ac_var contains a newline" >&5 -$as_echo "$as_me: WARNING: cache variable $ac_var contains a newline" >&2;} ;; - esac - case $ac_var in #( - _ | IFS | as_nl) ;; #( - BASH_ARGV | BASH_SOURCE) eval $ac_var= ;; #( - *) { eval $ac_var=; unset $ac_var;} ;; - esac ;; - esac - done - - (set) 2>&1 | - case $as_nl`(ac_space=' '; set) 2>&1` in #( - *${as_nl}ac_space=\ *) - # `set' does not quote correctly, so add quotes: double-quote - # substitution turns \\\\ into \\, and sed turns \\ into \. - sed -n \ - "s/'/'\\\\''/g; - s/^\\([_$as_cr_alnum]*_cv_[_$as_cr_alnum]*\\)=\\(.*\\)/\\1='\\2'/p" - ;; #( - *) - # `set' quotes correctly as required by POSIX, so do not add quotes. - sed -n "/^[_$as_cr_alnum]*_cv_[_$as_cr_alnum]*=/p" - ;; - esac | - sort -) | - sed ' - /^ac_cv_env_/b end - t clear - :clear - s/^\([^=]*\)=\(.*[{}].*\)$/test "${\1+set}" = set || &/ - t end - s/^\([^=]*\)=\(.*\)$/\1=${\1=\2}/ - :end' >>confcache -if diff "$cache_file" confcache >/dev/null 2>&1; then :; else - if test -w "$cache_file"; then - if test "x$cache_file" != "x/dev/null"; then - { $as_echo "$as_me:${as_lineno-$LINENO}: updating cache $cache_file" >&5 -$as_echo "$as_me: updating cache $cache_file" >&6;} - if test ! -f "$cache_file" || test -h "$cache_file"; then - cat confcache >"$cache_file" - else - case $cache_file in #( - */* | ?:*) - mv -f confcache "$cache_file"$$ && - mv -f "$cache_file"$$ "$cache_file" ;; #( - *) - mv -f confcache "$cache_file" ;; - esac - fi - fi - else - { $as_echo "$as_me:${as_lineno-$LINENO}: not updating unwritable cache $cache_file" >&5 -$as_echo "$as_me: not updating unwritable cache $cache_file" >&6;} - fi -fi -rm -f confcache - -test "x$prefix" = xNONE && prefix=$ac_default_prefix -# Let make expand exec_prefix. -test "x$exec_prefix" = xNONE && exec_prefix='${prefix}' - -DEFS=-DHAVE_CONFIG_H - -ac_libobjs= -ac_ltlibobjs= -U= -for ac_i in : $LIBOBJS; do test "x$ac_i" = x: && continue - # 1. Remove the extension, and $U if already installed. - ac_script='s/\$U\././;s/\.o$//;s/\.obj$//' - ac_i=`$as_echo "$ac_i" | sed "$ac_script"` - # 2. Prepend LIBOBJDIR. When used with automake>=1.10 LIBOBJDIR - # will be set to the directory where LIBOBJS objects are built. - as_fn_append ac_libobjs " \${LIBOBJDIR}$ac_i\$U.$ac_objext" - as_fn_append ac_ltlibobjs " \${LIBOBJDIR}$ac_i"'$U.lo' -done -LIBOBJS=$ac_libobjs - -LTLIBOBJS=$ac_ltlibobjs - - - -: "${CONFIG_STATUS=./config.status}" -ac_write_fail=0 -ac_clean_files_save=$ac_clean_files -ac_clean_files="$ac_clean_files $CONFIG_STATUS" -{ $as_echo "$as_me:${as_lineno-$LINENO}: creating $CONFIG_STATUS" >&5 -$as_echo "$as_me: creating $CONFIG_STATUS" >&6;} -as_write_fail=0 -cat >$CONFIG_STATUS <<_ASEOF || as_write_fail=1 -#! $SHELL -# Generated by $as_me. -# Run this file to recreate the current configuration. -# Compiler output produced by configure, useful for debugging -# configure, is in config.log if it exists. - -debug=false -ac_cs_recheck=false -ac_cs_silent=false - -SHELL=\${CONFIG_SHELL-$SHELL} -export SHELL -_ASEOF -cat >>$CONFIG_STATUS <<\_ASEOF || as_write_fail=1 -## -------------------- ## -## M4sh Initialization. ## -## -------------------- ## - -# Be more Bourne compatible -DUALCASE=1; export DUALCASE # for MKS sh -if test -n "${ZSH_VERSION+set}" && (emulate sh) >/dev/null 2>&1; then : - emulate sh - NULLCMD=: - # Pre-4.2 versions of Zsh do word splitting on ${1+"$@"}, which - # is contrary to our usage. Disable this feature. - alias -g '${1+"$@"}'='"$@"' - setopt NO_GLOB_SUBST -else - case `(set -o) 2>/dev/null` in #( - *posix*) : - set -o posix ;; #( - *) : - ;; -esac -fi - - -as_nl=' -' -export as_nl -# Printing a long string crashes Solaris 7 /usr/bin/printf. -as_echo='\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\' -as_echo=$as_echo$as_echo$as_echo$as_echo$as_echo -as_echo=$as_echo$as_echo$as_echo$as_echo$as_echo$as_echo -# Prefer a ksh shell builtin over an external printf program on Solaris, -# but without wasting forks for bash or zsh. -if test -z "$BASH_VERSION$ZSH_VERSION" \ - && (test "X`print -r -- $as_echo`" = "X$as_echo") 2>/dev/null; then - as_echo='print -r --' - as_echo_n='print -rn --' -elif (test "X`printf %s $as_echo`" = "X$as_echo") 2>/dev/null; then - as_echo='printf %s\n' - as_echo_n='printf %s' -else - if test "X`(/usr/ucb/echo -n -n $as_echo) 2>/dev/null`" = "X-n $as_echo"; then - as_echo_body='eval /usr/ucb/echo -n "$1$as_nl"' - as_echo_n='/usr/ucb/echo -n' - else - as_echo_body='eval expr "X$1" : "X\\(.*\\)"' - as_echo_n_body='eval - arg=$1; - case $arg in #( - *"$as_nl"*) - expr "X$arg" : "X\\(.*\\)$as_nl"; - arg=`expr "X$arg" : ".*$as_nl\\(.*\\)"`;; - esac; - expr "X$arg" : "X\\(.*\\)" | tr -d "$as_nl" - ' - export as_echo_n_body - as_echo_n='sh -c $as_echo_n_body as_echo' - fi - export as_echo_body - as_echo='sh -c $as_echo_body as_echo' -fi - -# The user is always right. -if test "${PATH_SEPARATOR+set}" != set; then - PATH_SEPARATOR=: - (PATH='/bin;/bin'; FPATH=$PATH; sh -c :) >/dev/null 2>&1 && { - (PATH='/bin:/bin'; FPATH=$PATH; sh -c :) >/dev/null 2>&1 || - PATH_SEPARATOR=';' - } -fi - - -# IFS -# We need space, tab and new line, in precisely that order. Quoting is -# there to prevent editors from complaining about space-tab. -# (If _AS_PATH_WALK were called with IFS unset, it would disable word -# splitting by setting IFS to empty value.) -IFS=" "" $as_nl" - -# Find who we are. Look in the path if we contain no directory separator. -as_myself= -case $0 in #(( - *[\\/]* ) as_myself=$0 ;; - *) as_save_IFS=$IFS; IFS=$PATH_SEPARATOR -for as_dir in $PATH -do - IFS=$as_save_IFS - test -z "$as_dir" && as_dir=. - test -r "$as_dir/$0" && as_myself=$as_dir/$0 && break - done -IFS=$as_save_IFS - - ;; -esac -# We did not find ourselves, most probably we were run as `sh COMMAND' -# in which case we are not to be found in the path. -if test "x$as_myself" = x; then - as_myself=$0 -fi -if test ! -f "$as_myself"; then - $as_echo "$as_myself: error: cannot find myself; rerun with an absolute file name" >&2 - exit 1 -fi - -# Unset variables that we do not need and which cause bugs (e.g. in -# pre-3.0 UWIN ksh). But do not cause bugs in bash 2.01; the "|| exit 1" -# suppresses any "Segmentation fault" message there. '((' could -# trigger a bug in pdksh 5.2.14. -for as_var in BASH_ENV ENV MAIL MAILPATH -do eval test x\${$as_var+set} = xset \ - && ( (unset $as_var) || exit 1) >/dev/null 2>&1 && unset $as_var || : -done -PS1='$ ' -PS2='> ' -PS4='+ ' - -# NLS nuisances. -LC_ALL=C -export LC_ALL -LANGUAGE=C -export LANGUAGE - -# CDPATH. -(unset CDPATH) >/dev/null 2>&1 && unset CDPATH - - -# as_fn_error STATUS ERROR [LINENO LOG_FD] -# ---------------------------------------- -# Output "`basename $0`: error: ERROR" to stderr. If LINENO and LOG_FD are -# provided, also output the error to LOG_FD, referencing LINENO. Then exit the -# script with STATUS, using 1 if that was 0. -as_fn_error () -{ - as_status=$1; test $as_status -eq 0 && as_status=1 - if test "$4"; then - as_lineno=${as_lineno-"$3"} as_lineno_stack=as_lineno_stack=$as_lineno_stack - $as_echo "$as_me:${as_lineno-$LINENO}: error: $2" >&$4 - fi - $as_echo "$as_me: error: $2" >&2 - as_fn_exit $as_status -} # as_fn_error - - -# as_fn_set_status STATUS -# ----------------------- -# Set $? to STATUS, without forking. -as_fn_set_status () -{ - return $1 -} # as_fn_set_status - -# as_fn_exit STATUS -# ----------------- -# Exit the shell with STATUS, even in a "trap 0" or "set -e" context. -as_fn_exit () -{ - set +e - as_fn_set_status $1 - exit $1 -} # as_fn_exit - -# as_fn_unset VAR -# --------------- -# Portably unset VAR. -as_fn_unset () -{ - { eval $1=; unset $1;} -} -as_unset=as_fn_unset -# as_fn_append VAR VALUE -# ---------------------- -# Append the text in VALUE to the end of the definition contained in VAR. Take -# advantage of any shell optimizations that allow amortized linear growth over -# repeated appends, instead of the typical quadratic growth present in naive -# implementations. -if (eval "as_var=1; as_var+=2; test x\$as_var = x12") 2>/dev/null; then : - eval 'as_fn_append () - { - eval $1+=\$2 - }' -else - as_fn_append () - { - eval $1=\$$1\$2 - } -fi # as_fn_append - -# as_fn_arith ARG... -# ------------------ -# Perform arithmetic evaluation on the ARGs, and store the result in the -# global $as_val. Take advantage of shells that can avoid forks. The arguments -# must be portable across $(()) and expr. -if (eval "test \$(( 1 + 1 )) = 2") 2>/dev/null; then : - eval 'as_fn_arith () - { - as_val=$(( $* )) - }' -else - as_fn_arith () - { - as_val=`expr "$@" || test $? -eq 1` - } -fi # as_fn_arith - - -if expr a : '\(a\)' >/dev/null 2>&1 && - test "X`expr 00001 : '.*\(...\)'`" = X001; then - as_expr=expr -else - as_expr=false -fi - -if (basename -- /) >/dev/null 2>&1 && test "X`basename -- / 2>&1`" = "X/"; then - as_basename=basename -else - as_basename=false -fi - -if (as_dir=`dirname -- /` && test "X$as_dir" = X/) >/dev/null 2>&1; then - as_dirname=dirname -else - as_dirname=false -fi - -as_me=`$as_basename -- "$0" || -$as_expr X/"$0" : '.*/\([^/][^/]*\)/*$' \| \ - X"$0" : 'X\(//\)$' \| \ - X"$0" : 'X\(/\)' \| . 2>/dev/null || -$as_echo X/"$0" | - sed '/^.*\/\([^/][^/]*\)\/*$/{ - s//\1/ - q - } - /^X\/\(\/\/\)$/{ - s//\1/ - q - } - /^X\/\(\/\).*/{ - s//\1/ - q - } - s/.*/./; q'` - -# Avoid depending upon Character Ranges. -as_cr_letters='abcdefghijklmnopqrstuvwxyz' -as_cr_LETTERS='ABCDEFGHIJKLMNOPQRSTUVWXYZ' -as_cr_Letters=$as_cr_letters$as_cr_LETTERS -as_cr_digits='0123456789' -as_cr_alnum=$as_cr_Letters$as_cr_digits - -ECHO_C= ECHO_N= ECHO_T= -case `echo -n x` in #((((( --n*) - case `echo 'xy\c'` in - *c*) ECHO_T=' ';; # ECHO_T is single tab character. - xy) ECHO_C='\c';; - *) echo `echo ksh88 bug on AIX 6.1` > /dev/null - ECHO_T=' ';; - esac;; -*) - ECHO_N='-n';; -esac - -rm -f conf$$ conf$$.exe conf$$.file -if test -d conf$$.dir; then - rm -f conf$$.dir/conf$$.file -else - rm -f conf$$.dir - mkdir conf$$.dir 2>/dev/null -fi -if (echo >conf$$.file) 2>/dev/null; then - if ln -s conf$$.file conf$$ 2>/dev/null; then - as_ln_s='ln -s' - # ... but there are two gotchas: - # 1) On MSYS, both `ln -s file dir' and `ln file dir' fail. - # 2) DJGPP < 2.04 has no symlinks; `ln -s' creates a wrapper executable. - # In both cases, we have to default to `cp -pR'. - ln -s conf$$.file conf$$.dir 2>/dev/null && test ! -f conf$$.exe || - as_ln_s='cp -pR' - elif ln conf$$.file conf$$ 2>/dev/null; then - as_ln_s=ln - else - as_ln_s='cp -pR' - fi -else - as_ln_s='cp -pR' -fi -rm -f conf$$ conf$$.exe conf$$.dir/conf$$.file conf$$.file -rmdir conf$$.dir 2>/dev/null - - -# as_fn_mkdir_p -# ------------- -# Create "$as_dir" as a directory, including parents if necessary. -as_fn_mkdir_p () -{ - - case $as_dir in #( - -*) as_dir=./$as_dir;; - esac - test -d "$as_dir" || eval $as_mkdir_p || { - as_dirs= - while :; do - case $as_dir in #( - *\'*) as_qdir=`$as_echo "$as_dir" | sed "s/'/'\\\\\\\\''/g"`;; #'( - *) as_qdir=$as_dir;; - esac - as_dirs="'$as_qdir' $as_dirs" - as_dir=`$as_dirname -- "$as_dir" || -$as_expr X"$as_dir" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \ - X"$as_dir" : 'X\(//\)[^/]' \| \ - X"$as_dir" : 'X\(//\)$' \| \ - X"$as_dir" : 'X\(/\)' \| . 2>/dev/null || -$as_echo X"$as_dir" | - sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{ - s//\1/ - q - } - /^X\(\/\/\)[^/].*/{ - s//\1/ - q - } - /^X\(\/\/\)$/{ - s//\1/ - q - } - /^X\(\/\).*/{ - s//\1/ - q - } - s/.*/./; q'` - test -d "$as_dir" && break - done - test -z "$as_dirs" || eval "mkdir $as_dirs" - } || test -d "$as_dir" || as_fn_error $? "cannot create directory $as_dir" - - -} # as_fn_mkdir_p -if mkdir -p . 2>/dev/null; then - as_mkdir_p='mkdir -p "$as_dir"' -else - test -d ./-p && rmdir ./-p - as_mkdir_p=false -fi - - -# as_fn_executable_p FILE -# ----------------------- -# Test if FILE is an executable regular file. -as_fn_executable_p () -{ - test -f "$1" && test -x "$1" -} # as_fn_executable_p -as_test_x='test -x' -as_executable_p=as_fn_executable_p - -# Sed expression to map a string onto a valid CPP name. -as_tr_cpp="eval sed 'y%*$as_cr_letters%P$as_cr_LETTERS%;s%[^_$as_cr_alnum]%_%g'" - -# Sed expression to map a string onto a valid variable name. -as_tr_sh="eval sed 'y%*+%pp%;s%[^_$as_cr_alnum]%_%g'" - - -exec 6>&1 -## ----------------------------------- ## -## Main body of $CONFIG_STATUS script. ## -## ----------------------------------- ## -_ASEOF -test $as_write_fail = 0 && chmod +x $CONFIG_STATUS || ac_write_fail=1 - -cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1 -# Save the log message, to keep $0 and so on meaningful, and to -# report actual input values of CONFIG_FILES etc. instead of their -# values after options handling. -ac_log=" -This file was extended by sqlcipher $as_me 3.11.0, which was -generated by GNU Autoconf 2.69. Invocation command line was - - CONFIG_FILES = $CONFIG_FILES - CONFIG_HEADERS = $CONFIG_HEADERS - CONFIG_LINKS = $CONFIG_LINKS - CONFIG_COMMANDS = $CONFIG_COMMANDS - $ $0 $@ - -on `(hostname || uname -n) 2>/dev/null | sed 1q` -" - -_ACEOF - -case $ac_config_files in *" -"*) set x $ac_config_files; shift; ac_config_files=$*;; -esac - -case $ac_config_headers in *" -"*) set x $ac_config_headers; shift; ac_config_headers=$*;; -esac - - -cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1 -# Files that config.status was made for. -config_files="$ac_config_files" -config_headers="$ac_config_headers" -config_commands="$ac_config_commands" - -_ACEOF - -cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1 -ac_cs_usage="\ -\`$as_me' instantiates files and other configuration actions -from templates according to the current configuration. Unless the files -and actions are specified as TAGs, all are instantiated by default. - -Usage: $0 [OPTION]... [TAG]... - - -h, --help print this help, then exit - -V, --version print version number and configuration settings, then exit - --config print configuration, then exit - -q, --quiet, --silent - do not print progress messages - -d, --debug don't remove temporary files - --recheck update $as_me by reconfiguring in the same conditions - --file=FILE[:TEMPLATE] - instantiate the configuration file FILE - --header=FILE[:TEMPLATE] - instantiate the configuration header FILE - -Configuration files: -$config_files - -Configuration headers: -$config_headers - -Configuration commands: -$config_commands - -Report bugs to the package provider." - -_ACEOF -cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1 -ac_cs_config="`$as_echo "$ac_configure_args" | sed 's/^ //; s/[\\""\`\$]/\\\\&/g'`" -ac_cs_version="\\ -sqlcipher config.status 3.11.0 -configured by $0, generated by GNU Autoconf 2.69, - with options \\"\$ac_cs_config\\" - -Copyright (C) 2012 Free Software Foundation, Inc. -This config.status script is free software; the Free Software Foundation -gives unlimited permission to copy, distribute and modify it." - -ac_pwd='$ac_pwd' -srcdir='$srcdir' -INSTALL='$INSTALL' -AWK='$AWK' -test -n "\$AWK" || AWK=awk -_ACEOF - -cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1 -# The default lists apply if the user does not specify any file. -ac_need_defaults=: -while test $# != 0 -do - case $1 in - --*=?*) - ac_option=`expr "X$1" : 'X\([^=]*\)='` - ac_optarg=`expr "X$1" : 'X[^=]*=\(.*\)'` - ac_shift=: - ;; - --*=) - ac_option=`expr "X$1" : 'X\([^=]*\)='` - ac_optarg= - ac_shift=: - ;; - *) - ac_option=$1 - ac_optarg=$2 - ac_shift=shift - ;; - esac - - case $ac_option in - # Handling of the options. - -recheck | --recheck | --rechec | --reche | --rech | --rec | --re | --r) - ac_cs_recheck=: ;; - --version | --versio | --versi | --vers | --ver | --ve | --v | -V ) - $as_echo "$ac_cs_version"; exit ;; - --config | --confi | --conf | --con | --co | --c ) - $as_echo "$ac_cs_config"; exit ;; - --debug | --debu | --deb | --de | --d | -d ) - debug=: ;; - --file | --fil | --fi | --f ) - $ac_shift - case $ac_optarg in - *\'*) ac_optarg=`$as_echo "$ac_optarg" | sed "s/'/'\\\\\\\\''/g"` ;; - '') as_fn_error $? "missing file argument" ;; - esac - as_fn_append CONFIG_FILES " '$ac_optarg'" - ac_need_defaults=false;; - --header | --heade | --head | --hea ) - $ac_shift - case $ac_optarg in - *\'*) ac_optarg=`$as_echo "$ac_optarg" | sed "s/'/'\\\\\\\\''/g"` ;; - esac - as_fn_append CONFIG_HEADERS " '$ac_optarg'" - ac_need_defaults=false;; - --he | --h) - # Conflict between --help and --header - as_fn_error $? "ambiguous option: \`$1' -Try \`$0 --help' for more information.";; - --help | --hel | -h ) - $as_echo "$ac_cs_usage"; exit ;; - -q | -quiet | --quiet | --quie | --qui | --qu | --q \ - | -silent | --silent | --silen | --sile | --sil | --si | --s) - ac_cs_silent=: ;; - - # This is an error. - -*) as_fn_error $? "unrecognized option: \`$1' -Try \`$0 --help' for more information." ;; - - *) as_fn_append ac_config_targets " $1" - ac_need_defaults=false ;; - - esac - shift -done - -ac_configure_extra_args= - -if $ac_cs_silent; then - exec 6>/dev/null - ac_configure_extra_args="$ac_configure_extra_args --silent" -fi - -_ACEOF -cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1 -if \$ac_cs_recheck; then - set X $SHELL '$0' $ac_configure_args \$ac_configure_extra_args --no-create --no-recursion - shift - \$as_echo "running CONFIG_SHELL=$SHELL \$*" >&6 - CONFIG_SHELL='$SHELL' - export CONFIG_SHELL - exec "\$@" -fi - -_ACEOF -cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1 -exec 5>>config.log -{ - echo - sed 'h;s/./-/g;s/^.../## /;s/...$/ ##/;p;x;p;x' <<_ASBOX -## Running $as_me. ## -_ASBOX - $as_echo "$ac_log" -} >&5 - -_ACEOF -cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1 -# -# INIT-COMMANDS -# - - -# The HP-UX ksh and POSIX shell print the target directory to stdout -# if CDPATH is set. -(unset CDPATH) >/dev/null 2>&1 && unset CDPATH - -sed_quote_subst='$sed_quote_subst' -double_quote_subst='$double_quote_subst' -delay_variable_subst='$delay_variable_subst' -macro_version='`$ECHO "$macro_version" | $SED "$delay_single_quote_subst"`' -macro_revision='`$ECHO "$macro_revision" | $SED "$delay_single_quote_subst"`' -enable_shared='`$ECHO "$enable_shared" | $SED "$delay_single_quote_subst"`' -enable_static='`$ECHO "$enable_static" | $SED "$delay_single_quote_subst"`' -pic_mode='`$ECHO "$pic_mode" | $SED "$delay_single_quote_subst"`' -enable_fast_install='`$ECHO "$enable_fast_install" | $SED "$delay_single_quote_subst"`' -shared_archive_member_spec='`$ECHO "$shared_archive_member_spec" | $SED "$delay_single_quote_subst"`' -SHELL='`$ECHO "$SHELL" | $SED "$delay_single_quote_subst"`' -ECHO='`$ECHO "$ECHO" | $SED "$delay_single_quote_subst"`' -PATH_SEPARATOR='`$ECHO "$PATH_SEPARATOR" | $SED "$delay_single_quote_subst"`' -host_alias='`$ECHO "$host_alias" | $SED "$delay_single_quote_subst"`' -host='`$ECHO "$host" | $SED "$delay_single_quote_subst"`' -host_os='`$ECHO "$host_os" | $SED "$delay_single_quote_subst"`' -build_alias='`$ECHO "$build_alias" | $SED "$delay_single_quote_subst"`' -build='`$ECHO "$build" | $SED "$delay_single_quote_subst"`' -build_os='`$ECHO "$build_os" | $SED "$delay_single_quote_subst"`' -SED='`$ECHO "$SED" | $SED "$delay_single_quote_subst"`' -Xsed='`$ECHO "$Xsed" | $SED "$delay_single_quote_subst"`' -GREP='`$ECHO "$GREP" | $SED "$delay_single_quote_subst"`' -EGREP='`$ECHO "$EGREP" | $SED "$delay_single_quote_subst"`' -FGREP='`$ECHO "$FGREP" | $SED "$delay_single_quote_subst"`' -LD='`$ECHO "$LD" | $SED "$delay_single_quote_subst"`' -NM='`$ECHO "$NM" | $SED "$delay_single_quote_subst"`' -LN_S='`$ECHO "$LN_S" | $SED "$delay_single_quote_subst"`' -max_cmd_len='`$ECHO "$max_cmd_len" | $SED "$delay_single_quote_subst"`' -ac_objext='`$ECHO "$ac_objext" | $SED "$delay_single_quote_subst"`' -exeext='`$ECHO "$exeext" | $SED "$delay_single_quote_subst"`' -lt_unset='`$ECHO "$lt_unset" | $SED "$delay_single_quote_subst"`' -lt_SP2NL='`$ECHO "$lt_SP2NL" | $SED "$delay_single_quote_subst"`' -lt_NL2SP='`$ECHO "$lt_NL2SP" | $SED "$delay_single_quote_subst"`' -lt_cv_to_host_file_cmd='`$ECHO "$lt_cv_to_host_file_cmd" | $SED "$delay_single_quote_subst"`' -lt_cv_to_tool_file_cmd='`$ECHO "$lt_cv_to_tool_file_cmd" | $SED "$delay_single_quote_subst"`' -reload_flag='`$ECHO "$reload_flag" | $SED "$delay_single_quote_subst"`' -reload_cmds='`$ECHO "$reload_cmds" | $SED "$delay_single_quote_subst"`' -OBJDUMP='`$ECHO "$OBJDUMP" | $SED "$delay_single_quote_subst"`' -deplibs_check_method='`$ECHO "$deplibs_check_method" | $SED "$delay_single_quote_subst"`' -file_magic_cmd='`$ECHO "$file_magic_cmd" | $SED "$delay_single_quote_subst"`' -file_magic_glob='`$ECHO "$file_magic_glob" | $SED "$delay_single_quote_subst"`' -want_nocaseglob='`$ECHO "$want_nocaseglob" | $SED "$delay_single_quote_subst"`' -DLLTOOL='`$ECHO "$DLLTOOL" | $SED "$delay_single_quote_subst"`' -sharedlib_from_linklib_cmd='`$ECHO "$sharedlib_from_linklib_cmd" | $SED "$delay_single_quote_subst"`' -AR='`$ECHO "$AR" | $SED "$delay_single_quote_subst"`' -AR_FLAGS='`$ECHO "$AR_FLAGS" | $SED "$delay_single_quote_subst"`' -archiver_list_spec='`$ECHO "$archiver_list_spec" | $SED "$delay_single_quote_subst"`' -STRIP='`$ECHO "$STRIP" | $SED "$delay_single_quote_subst"`' -RANLIB='`$ECHO "$RANLIB" | $SED "$delay_single_quote_subst"`' -old_postinstall_cmds='`$ECHO "$old_postinstall_cmds" | $SED "$delay_single_quote_subst"`' -old_postuninstall_cmds='`$ECHO "$old_postuninstall_cmds" | $SED "$delay_single_quote_subst"`' -old_archive_cmds='`$ECHO "$old_archive_cmds" | $SED "$delay_single_quote_subst"`' -lock_old_archive_extraction='`$ECHO "$lock_old_archive_extraction" | $SED "$delay_single_quote_subst"`' -CC='`$ECHO "$CC" | $SED "$delay_single_quote_subst"`' -CFLAGS='`$ECHO "$CFLAGS" | $SED "$delay_single_quote_subst"`' -compiler='`$ECHO "$compiler" | $SED "$delay_single_quote_subst"`' -GCC='`$ECHO "$GCC" | $SED "$delay_single_quote_subst"`' -lt_cv_sys_global_symbol_pipe='`$ECHO "$lt_cv_sys_global_symbol_pipe" | $SED "$delay_single_quote_subst"`' -lt_cv_sys_global_symbol_to_cdecl='`$ECHO "$lt_cv_sys_global_symbol_to_cdecl" | $SED "$delay_single_quote_subst"`' -lt_cv_sys_global_symbol_to_import='`$ECHO "$lt_cv_sys_global_symbol_to_import" | $SED "$delay_single_quote_subst"`' -lt_cv_sys_global_symbol_to_c_name_address='`$ECHO "$lt_cv_sys_global_symbol_to_c_name_address" | $SED "$delay_single_quote_subst"`' -lt_cv_sys_global_symbol_to_c_name_address_lib_prefix='`$ECHO "$lt_cv_sys_global_symbol_to_c_name_address_lib_prefix" | $SED "$delay_single_quote_subst"`' -lt_cv_nm_interface='`$ECHO "$lt_cv_nm_interface" | $SED "$delay_single_quote_subst"`' -nm_file_list_spec='`$ECHO "$nm_file_list_spec" | $SED "$delay_single_quote_subst"`' -lt_sysroot='`$ECHO "$lt_sysroot" | $SED "$delay_single_quote_subst"`' -lt_cv_truncate_bin='`$ECHO "$lt_cv_truncate_bin" | $SED "$delay_single_quote_subst"`' -objdir='`$ECHO "$objdir" | $SED "$delay_single_quote_subst"`' -MAGIC_CMD='`$ECHO "$MAGIC_CMD" | $SED "$delay_single_quote_subst"`' -lt_prog_compiler_no_builtin_flag='`$ECHO "$lt_prog_compiler_no_builtin_flag" | $SED "$delay_single_quote_subst"`' -lt_prog_compiler_pic='`$ECHO "$lt_prog_compiler_pic" | $SED "$delay_single_quote_subst"`' -lt_prog_compiler_wl='`$ECHO "$lt_prog_compiler_wl" | $SED "$delay_single_quote_subst"`' -lt_prog_compiler_static='`$ECHO "$lt_prog_compiler_static" | $SED "$delay_single_quote_subst"`' -lt_cv_prog_compiler_c_o='`$ECHO "$lt_cv_prog_compiler_c_o" | $SED "$delay_single_quote_subst"`' -need_locks='`$ECHO "$need_locks" | $SED "$delay_single_quote_subst"`' -MANIFEST_TOOL='`$ECHO "$MANIFEST_TOOL" | $SED "$delay_single_quote_subst"`' -DSYMUTIL='`$ECHO "$DSYMUTIL" | $SED "$delay_single_quote_subst"`' -NMEDIT='`$ECHO "$NMEDIT" | $SED "$delay_single_quote_subst"`' -LIPO='`$ECHO "$LIPO" | $SED "$delay_single_quote_subst"`' -OTOOL='`$ECHO "$OTOOL" | $SED "$delay_single_quote_subst"`' -OTOOL64='`$ECHO "$OTOOL64" | $SED "$delay_single_quote_subst"`' -libext='`$ECHO "$libext" | $SED "$delay_single_quote_subst"`' -shrext_cmds='`$ECHO "$shrext_cmds" | $SED "$delay_single_quote_subst"`' -extract_expsyms_cmds='`$ECHO "$extract_expsyms_cmds" | $SED "$delay_single_quote_subst"`' -archive_cmds_need_lc='`$ECHO "$archive_cmds_need_lc" | $SED "$delay_single_quote_subst"`' -enable_shared_with_static_runtimes='`$ECHO "$enable_shared_with_static_runtimes" | $SED "$delay_single_quote_subst"`' -export_dynamic_flag_spec='`$ECHO "$export_dynamic_flag_spec" | $SED "$delay_single_quote_subst"`' -whole_archive_flag_spec='`$ECHO "$whole_archive_flag_spec" | $SED "$delay_single_quote_subst"`' -compiler_needs_object='`$ECHO "$compiler_needs_object" | $SED "$delay_single_quote_subst"`' -old_archive_from_new_cmds='`$ECHO "$old_archive_from_new_cmds" | $SED "$delay_single_quote_subst"`' -old_archive_from_expsyms_cmds='`$ECHO "$old_archive_from_expsyms_cmds" | $SED "$delay_single_quote_subst"`' -archive_cmds='`$ECHO "$archive_cmds" | $SED "$delay_single_quote_subst"`' -archive_expsym_cmds='`$ECHO "$archive_expsym_cmds" | $SED "$delay_single_quote_subst"`' -module_cmds='`$ECHO "$module_cmds" | $SED "$delay_single_quote_subst"`' -module_expsym_cmds='`$ECHO "$module_expsym_cmds" | $SED "$delay_single_quote_subst"`' -with_gnu_ld='`$ECHO "$with_gnu_ld" | $SED "$delay_single_quote_subst"`' -allow_undefined_flag='`$ECHO "$allow_undefined_flag" | $SED "$delay_single_quote_subst"`' -no_undefined_flag='`$ECHO "$no_undefined_flag" | $SED "$delay_single_quote_subst"`' -hardcode_libdir_flag_spec='`$ECHO "$hardcode_libdir_flag_spec" | $SED "$delay_single_quote_subst"`' -hardcode_libdir_separator='`$ECHO "$hardcode_libdir_separator" | $SED "$delay_single_quote_subst"`' -hardcode_direct='`$ECHO "$hardcode_direct" | $SED "$delay_single_quote_subst"`' -hardcode_direct_absolute='`$ECHO "$hardcode_direct_absolute" | $SED "$delay_single_quote_subst"`' -hardcode_minus_L='`$ECHO "$hardcode_minus_L" | $SED "$delay_single_quote_subst"`' -hardcode_shlibpath_var='`$ECHO "$hardcode_shlibpath_var" | $SED "$delay_single_quote_subst"`' -hardcode_automatic='`$ECHO "$hardcode_automatic" | $SED "$delay_single_quote_subst"`' -inherit_rpath='`$ECHO "$inherit_rpath" | $SED "$delay_single_quote_subst"`' -link_all_deplibs='`$ECHO "$link_all_deplibs" | $SED "$delay_single_quote_subst"`' -always_export_symbols='`$ECHO "$always_export_symbols" | $SED "$delay_single_quote_subst"`' -export_symbols_cmds='`$ECHO "$export_symbols_cmds" | $SED "$delay_single_quote_subst"`' -exclude_expsyms='`$ECHO "$exclude_expsyms" | $SED "$delay_single_quote_subst"`' -include_expsyms='`$ECHO "$include_expsyms" | $SED "$delay_single_quote_subst"`' -prelink_cmds='`$ECHO "$prelink_cmds" | $SED "$delay_single_quote_subst"`' -postlink_cmds='`$ECHO "$postlink_cmds" | $SED "$delay_single_quote_subst"`' -file_list_spec='`$ECHO "$file_list_spec" | $SED "$delay_single_quote_subst"`' -variables_saved_for_relink='`$ECHO "$variables_saved_for_relink" | $SED "$delay_single_quote_subst"`' -need_lib_prefix='`$ECHO "$need_lib_prefix" | $SED "$delay_single_quote_subst"`' -need_version='`$ECHO "$need_version" | $SED "$delay_single_quote_subst"`' -version_type='`$ECHO "$version_type" | $SED "$delay_single_quote_subst"`' -runpath_var='`$ECHO "$runpath_var" | $SED "$delay_single_quote_subst"`' -shlibpath_var='`$ECHO "$shlibpath_var" | $SED "$delay_single_quote_subst"`' -shlibpath_overrides_runpath='`$ECHO "$shlibpath_overrides_runpath" | $SED "$delay_single_quote_subst"`' -libname_spec='`$ECHO "$libname_spec" | $SED "$delay_single_quote_subst"`' -library_names_spec='`$ECHO "$library_names_spec" | $SED "$delay_single_quote_subst"`' -soname_spec='`$ECHO "$soname_spec" | $SED "$delay_single_quote_subst"`' -install_override_mode='`$ECHO "$install_override_mode" | $SED "$delay_single_quote_subst"`' -postinstall_cmds='`$ECHO "$postinstall_cmds" | $SED "$delay_single_quote_subst"`' -postuninstall_cmds='`$ECHO "$postuninstall_cmds" | $SED "$delay_single_quote_subst"`' -finish_cmds='`$ECHO "$finish_cmds" | $SED "$delay_single_quote_subst"`' -finish_eval='`$ECHO "$finish_eval" | $SED "$delay_single_quote_subst"`' -hardcode_into_libs='`$ECHO "$hardcode_into_libs" | $SED "$delay_single_quote_subst"`' -sys_lib_search_path_spec='`$ECHO "$sys_lib_search_path_spec" | $SED "$delay_single_quote_subst"`' -configure_time_dlsearch_path='`$ECHO "$configure_time_dlsearch_path" | $SED "$delay_single_quote_subst"`' -configure_time_lt_sys_library_path='`$ECHO "$configure_time_lt_sys_library_path" | $SED "$delay_single_quote_subst"`' -hardcode_action='`$ECHO "$hardcode_action" | $SED "$delay_single_quote_subst"`' -enable_dlopen='`$ECHO "$enable_dlopen" | $SED "$delay_single_quote_subst"`' -enable_dlopen_self='`$ECHO "$enable_dlopen_self" | $SED "$delay_single_quote_subst"`' -enable_dlopen_self_static='`$ECHO "$enable_dlopen_self_static" | $SED "$delay_single_quote_subst"`' -old_striplib='`$ECHO "$old_striplib" | $SED "$delay_single_quote_subst"`' -striplib='`$ECHO "$striplib" | $SED "$delay_single_quote_subst"`' - -LTCC='$LTCC' -LTCFLAGS='$LTCFLAGS' -compiler='$compiler_DEFAULT' - -# A function that is used when there is no print builtin or printf. -func_fallback_echo () -{ - eval 'cat <<_LTECHO_EOF -\$1 -_LTECHO_EOF' -} - -# Quote evaled strings. -for var in SHELL \ -ECHO \ -PATH_SEPARATOR \ -SED \ -GREP \ -EGREP \ -FGREP \ -LD \ -NM \ -LN_S \ -lt_SP2NL \ -lt_NL2SP \ -reload_flag \ -OBJDUMP \ -deplibs_check_method \ -file_magic_cmd \ -file_magic_glob \ -want_nocaseglob \ -DLLTOOL \ -sharedlib_from_linklib_cmd \ -AR \ -AR_FLAGS \ -archiver_list_spec \ -STRIP \ -RANLIB \ -CC \ -CFLAGS \ -compiler \ -lt_cv_sys_global_symbol_pipe \ -lt_cv_sys_global_symbol_to_cdecl \ -lt_cv_sys_global_symbol_to_import \ -lt_cv_sys_global_symbol_to_c_name_address \ -lt_cv_sys_global_symbol_to_c_name_address_lib_prefix \ -lt_cv_nm_interface \ -nm_file_list_spec \ -lt_cv_truncate_bin \ -lt_prog_compiler_no_builtin_flag \ -lt_prog_compiler_pic \ -lt_prog_compiler_wl \ -lt_prog_compiler_static \ -lt_cv_prog_compiler_c_o \ -need_locks \ -MANIFEST_TOOL \ -DSYMUTIL \ -NMEDIT \ -LIPO \ -OTOOL \ -OTOOL64 \ -shrext_cmds \ -export_dynamic_flag_spec \ -whole_archive_flag_spec \ -compiler_needs_object \ -with_gnu_ld \ -allow_undefined_flag \ -no_undefined_flag \ -hardcode_libdir_flag_spec \ -hardcode_libdir_separator \ -exclude_expsyms \ -include_expsyms \ -file_list_spec \ -variables_saved_for_relink \ -libname_spec \ -library_names_spec \ -soname_spec \ -install_override_mode \ -finish_eval \ -old_striplib \ -striplib; do - case \`eval \\\\\$ECHO \\\\""\\\\\$\$var"\\\\"\` in - *[\\\\\\\`\\"\\\$]*) - eval "lt_\$var=\\\\\\"\\\`\\\$ECHO \\"\\\$\$var\\" | \\\$SED \\"\\\$sed_quote_subst\\"\\\`\\\\\\"" ## exclude from sc_prohibit_nested_quotes - ;; - *) - eval "lt_\$var=\\\\\\"\\\$\$var\\\\\\"" - ;; - esac -done - -# Double-quote double-evaled strings. -for var in reload_cmds \ -old_postinstall_cmds \ -old_postuninstall_cmds \ -old_archive_cmds \ -extract_expsyms_cmds \ -old_archive_from_new_cmds \ -old_archive_from_expsyms_cmds \ -archive_cmds \ -archive_expsym_cmds \ -module_cmds \ -module_expsym_cmds \ -export_symbols_cmds \ -prelink_cmds \ -postlink_cmds \ -postinstall_cmds \ -postuninstall_cmds \ -finish_cmds \ -sys_lib_search_path_spec \ -configure_time_dlsearch_path \ -configure_time_lt_sys_library_path; do - case \`eval \\\\\$ECHO \\\\""\\\\\$\$var"\\\\"\` in - *[\\\\\\\`\\"\\\$]*) - eval "lt_\$var=\\\\\\"\\\`\\\$ECHO \\"\\\$\$var\\" | \\\$SED -e \\"\\\$double_quote_subst\\" -e \\"\\\$sed_quote_subst\\" -e \\"\\\$delay_variable_subst\\"\\\`\\\\\\"" ## exclude from sc_prohibit_nested_quotes - ;; - *) - eval "lt_\$var=\\\\\\"\\\$\$var\\\\\\"" - ;; - esac -done - -ac_aux_dir='$ac_aux_dir' - -# See if we are running on zsh, and set the options that allow our -# commands through without removal of \ escapes INIT. -if test -n "\${ZSH_VERSION+set}"; then - setopt NO_GLOB_SUBST -fi - - - PACKAGE='$PACKAGE' - VERSION='$VERSION' - RM='$RM' - ofile='$ofile' - - - - -_ACEOF - -cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1 - -# Handling of arguments. -for ac_config_target in $ac_config_targets -do - case $ac_config_target in - "libtool") CONFIG_COMMANDS="$CONFIG_COMMANDS libtool" ;; - "config.h") CONFIG_HEADERS="$CONFIG_HEADERS config.h" ;; - "Makefile") CONFIG_FILES="$CONFIG_FILES Makefile" ;; - "sqlcipher.pc") CONFIG_FILES="$CONFIG_FILES sqlcipher.pc" ;; - - *) as_fn_error $? "invalid argument: \`$ac_config_target'" "$LINENO" 5;; - esac -done - - -# If the user did not use the arguments to specify the items to instantiate, -# then the envvar interface is used. Set only those that are not. -# We use the long form for the default assignment because of an extremely -# bizarre bug on SunOS 4.1.3. -if $ac_need_defaults; then - test "${CONFIG_FILES+set}" = set || CONFIG_FILES=$config_files - test "${CONFIG_HEADERS+set}" = set || CONFIG_HEADERS=$config_headers - test "${CONFIG_COMMANDS+set}" = set || CONFIG_COMMANDS=$config_commands -fi - -# Have a temporary directory for convenience. Make it in the build tree -# simply because there is no reason against having it here, and in addition, -# creating and moving files from /tmp can sometimes cause problems. -# Hook for its removal unless debugging. -# Note that there is a small window in which the directory will not be cleaned: -# after its creation but before its name has been assigned to `$tmp'. -$debug || -{ - tmp= ac_tmp= - trap 'exit_status=$? - : "${ac_tmp:=$tmp}" - { test ! -d "$ac_tmp" || rm -fr "$ac_tmp"; } && exit $exit_status -' 0 - trap 'as_fn_exit 1' 1 2 13 15 -} -# Create a (secure) tmp directory for tmp files. - -{ - tmp=`(umask 077 && mktemp -d "./confXXXXXX") 2>/dev/null` && - test -d "$tmp" -} || -{ - tmp=./conf$$-$RANDOM - (umask 077 && mkdir "$tmp") -} || as_fn_error $? "cannot create a temporary directory in ." "$LINENO" 5 -ac_tmp=$tmp - -# Set up the scripts for CONFIG_FILES section. -# No need to generate them if there are no CONFIG_FILES. -# This happens for instance with `./config.status config.h'. -if test -n "$CONFIG_FILES"; then - - -ac_cr=`echo X | tr X '\015'` -# On cygwin, bash can eat \r inside `` if the user requested igncr. -# But we know of no other shell where ac_cr would be empty at this -# point, so we can use a bashism as a fallback. -if test "x$ac_cr" = x; then - eval ac_cr=\$\'\\r\' -fi -ac_cs_awk_cr=`$AWK 'BEGIN { print "a\rb" }' /dev/null` -if test "$ac_cs_awk_cr" = "a${ac_cr}b"; then - ac_cs_awk_cr='\\r' -else - ac_cs_awk_cr=$ac_cr -fi - -echo 'BEGIN {' >"$ac_tmp/subs1.awk" && -_ACEOF - - -{ - echo "cat >conf$$subs.awk <<_ACEOF" && - echo "$ac_subst_vars" | sed 's/.*/&!$&$ac_delim/' && - echo "_ACEOF" -} >conf$$subs.sh || - as_fn_error $? "could not make $CONFIG_STATUS" "$LINENO" 5 -ac_delim_num=`echo "$ac_subst_vars" | grep -c '^'` -ac_delim='%!_!# ' -for ac_last_try in false false false false false :; do - . ./conf$$subs.sh || - as_fn_error $? "could not make $CONFIG_STATUS" "$LINENO" 5 - - ac_delim_n=`sed -n "s/.*$ac_delim\$/X/p" conf$$subs.awk | grep -c X` - if test $ac_delim_n = $ac_delim_num; then - break - elif $ac_last_try; then - as_fn_error $? "could not make $CONFIG_STATUS" "$LINENO" 5 - else - ac_delim="$ac_delim!$ac_delim _$ac_delim!! " - fi -done -rm -f conf$$subs.sh - -cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1 -cat >>"\$ac_tmp/subs1.awk" <<\\_ACAWK && -_ACEOF -sed -n ' -h -s/^/S["/; s/!.*/"]=/ -p -g -s/^[^!]*!// -:repl -t repl -s/'"$ac_delim"'$// -t delim -:nl -h -s/\(.\{148\}\)..*/\1/ -t more1 -s/["\\]/\\&/g; s/^/"/; s/$/\\n"\\/ -p -n -b repl -:more1 -s/["\\]/\\&/g; s/^/"/; s/$/"\\/ -p -g -s/.\{148\}// -t nl -:delim -h -s/\(.\{148\}\)..*/\1/ -t more2 -s/["\\]/\\&/g; s/^/"/; s/$/"/ -p -b -:more2 -s/["\\]/\\&/g; s/^/"/; s/$/"\\/ -p -g -s/.\{148\}// -t delim -' >$CONFIG_STATUS || ac_write_fail=1 -rm -f conf$$subs.awk -cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1 -_ACAWK -cat >>"\$ac_tmp/subs1.awk" <<_ACAWK && - for (key in S) S_is_set[key] = 1 - FS = "" - -} -{ - line = $ 0 - nfields = split(line, field, "@") - substed = 0 - len = length(field[1]) - for (i = 2; i < nfields; i++) { - key = field[i] - keylen = length(key) - if (S_is_set[key]) { - value = S[key] - line = substr(line, 1, len) "" value "" substr(line, len + keylen + 3) - len += length(value) + length(field[++i]) - substed = 1 - } else - len += 1 + keylen - } - - print line -} - -_ACAWK -_ACEOF -cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1 -if sed "s/$ac_cr//" < /dev/null > /dev/null 2>&1; then - sed "s/$ac_cr\$//; s/$ac_cr/$ac_cs_awk_cr/g" -else - cat -fi < "$ac_tmp/subs1.awk" > "$ac_tmp/subs.awk" \ - || as_fn_error $? "could not setup config files machinery" "$LINENO" 5 -_ACEOF - -# VPATH may cause trouble with some makes, so we remove sole $(srcdir), -# ${srcdir} and @srcdir@ entries from VPATH if srcdir is ".", strip leading and -# trailing colons and then remove the whole line if VPATH becomes empty -# (actually we leave an empty line to preserve line numbers). -if test "x$srcdir" = x.; then - ac_vpsub='/^[ ]*VPATH[ ]*=[ ]*/{ -h -s/// -s/^/:/ -s/[ ]*$/:/ -s/:\$(srcdir):/:/g -s/:\${srcdir}:/:/g -s/:@srcdir@:/:/g -s/^:*// -s/:*$// -x -s/\(=[ ]*\).*/\1/ -G -s/\n// -s/^[^=]*=[ ]*$// -}' -fi - -cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1 -fi # test -n "$CONFIG_FILES" - -# Set up the scripts for CONFIG_HEADERS section. -# No need to generate them if there are no CONFIG_HEADERS. -# This happens for instance with `./config.status Makefile'. -if test -n "$CONFIG_HEADERS"; then -cat >"$ac_tmp/defines.awk" <<\_ACAWK || -BEGIN { -_ACEOF - -# Transform confdefs.h into an awk script `defines.awk', embedded as -# here-document in config.status, that substitutes the proper values into -# config.h.in to produce config.h. - -# Create a delimiter string that does not exist in confdefs.h, to ease -# handling of long lines. -ac_delim='%!_!# ' -for ac_last_try in false false :; do - ac_tt=`sed -n "/$ac_delim/p" confdefs.h` - if test -z "$ac_tt"; then - break - elif $ac_last_try; then - as_fn_error $? "could not make $CONFIG_HEADERS" "$LINENO" 5 - else - ac_delim="$ac_delim!$ac_delim _$ac_delim!! " - fi -done - -# For the awk script, D is an array of macro values keyed by name, -# likewise P contains macro parameters if any. Preserve backslash -# newline sequences. - -ac_word_re=[_$as_cr_Letters][_$as_cr_alnum]* -sed -n ' -s/.\{148\}/&'"$ac_delim"'/g -t rset -:rset -s/^[ ]*#[ ]*define[ ][ ]*/ / -t def -d -:def -s/\\$// -t bsnl -s/["\\]/\\&/g -s/^ \('"$ac_word_re"'\)\(([^()]*)\)[ ]*\(.*\)/P["\1"]="\2"\ -D["\1"]=" \3"/p -s/^ \('"$ac_word_re"'\)[ ]*\(.*\)/D["\1"]=" \2"/p -d -:bsnl -s/["\\]/\\&/g -s/^ \('"$ac_word_re"'\)\(([^()]*)\)[ ]*\(.*\)/P["\1"]="\2"\ -D["\1"]=" \3\\\\\\n"\\/p -t cont -s/^ \('"$ac_word_re"'\)[ ]*\(.*\)/D["\1"]=" \2\\\\\\n"\\/p -t cont -d -:cont -n -s/.\{148\}/&'"$ac_delim"'/g -t clear -:clear -s/\\$// -t bsnlc -s/["\\]/\\&/g; s/^/"/; s/$/"/p -d -:bsnlc -s/["\\]/\\&/g; s/^/"/; s/$/\\\\\\n"\\/p -b cont -' >$CONFIG_STATUS || ac_write_fail=1 - -cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1 - for (key in D) D_is_set[key] = 1 - FS = "" -} -/^[\t ]*#[\t ]*(define|undef)[\t ]+$ac_word_re([\t (]|\$)/ { - line = \$ 0 - split(line, arg, " ") - if (arg[1] == "#") { - defundef = arg[2] - mac1 = arg[3] - } else { - defundef = substr(arg[1], 2) - mac1 = arg[2] - } - split(mac1, mac2, "(") #) - macro = mac2[1] - prefix = substr(line, 1, index(line, defundef) - 1) - if (D_is_set[macro]) { - # Preserve the white space surrounding the "#". - print prefix "define", macro P[macro] D[macro] - next - } else { - # Replace #undef with comments. This is necessary, for example, - # in the case of _POSIX_SOURCE, which is predefined and required - # on some systems where configure will not decide to define it. - if (defundef == "undef") { - print "/*", prefix defundef, macro, "*/" - next - } - } -} -{ print } -_ACAWK -_ACEOF -cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1 - as_fn_error $? "could not setup config headers machinery" "$LINENO" 5 -fi # test -n "$CONFIG_HEADERS" - - -eval set X " :F $CONFIG_FILES :H $CONFIG_HEADERS :C $CONFIG_COMMANDS" -shift -for ac_tag -do - case $ac_tag in - :[FHLC]) ac_mode=$ac_tag; continue;; - esac - case $ac_mode$ac_tag in - :[FHL]*:*);; - :L* | :C*:*) as_fn_error $? "invalid tag \`$ac_tag'" "$LINENO" 5;; - :[FH]-) ac_tag=-:-;; - :[FH]*) ac_tag=$ac_tag:$ac_tag.in;; - esac - ac_save_IFS=$IFS - IFS=: - set x $ac_tag - IFS=$ac_save_IFS - shift - ac_file=$1 - shift - - case $ac_mode in - :L) ac_source=$1;; - :[FH]) - ac_file_inputs= - for ac_f - do - case $ac_f in - -) ac_f="$ac_tmp/stdin";; - *) # Look for the file first in the build tree, then in the source tree - # (if the path is not absolute). The absolute path cannot be DOS-style, - # because $ac_f cannot contain `:'. - test -f "$ac_f" || - case $ac_f in - [\\/$]*) false;; - *) test -f "$srcdir/$ac_f" && ac_f="$srcdir/$ac_f";; - esac || - as_fn_error 1 "cannot find input file: \`$ac_f'" "$LINENO" 5;; - esac - case $ac_f in *\'*) ac_f=`$as_echo "$ac_f" | sed "s/'/'\\\\\\\\''/g"`;; esac - as_fn_append ac_file_inputs " '$ac_f'" - done - - # Let's still pretend it is `configure' which instantiates (i.e., don't - # use $as_me), people would be surprised to read: - # /* config.h. Generated by config.status. */ - configure_input='Generated from '` - $as_echo "$*" | sed 's|^[^:]*/||;s|:[^:]*/|, |g' - `' by configure.' - if test x"$ac_file" != x-; then - configure_input="$ac_file. $configure_input" - { $as_echo "$as_me:${as_lineno-$LINENO}: creating $ac_file" >&5 -$as_echo "$as_me: creating $ac_file" >&6;} - fi - # Neutralize special characters interpreted by sed in replacement strings. - case $configure_input in #( - *\&* | *\|* | *\\* ) - ac_sed_conf_input=`$as_echo "$configure_input" | - sed 's/[\\\\&|]/\\\\&/g'`;; #( - *) ac_sed_conf_input=$configure_input;; - esac - - case $ac_tag in - *:-:* | *:-) cat >"$ac_tmp/stdin" \ - || as_fn_error $? "could not create $ac_file" "$LINENO" 5 ;; - esac - ;; - esac - - ac_dir=`$as_dirname -- "$ac_file" || -$as_expr X"$ac_file" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \ - X"$ac_file" : 'X\(//\)[^/]' \| \ - X"$ac_file" : 'X\(//\)$' \| \ - X"$ac_file" : 'X\(/\)' \| . 2>/dev/null || -$as_echo X"$ac_file" | - sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{ - s//\1/ - q - } - /^X\(\/\/\)[^/].*/{ - s//\1/ - q - } - /^X\(\/\/\)$/{ - s//\1/ - q - } - /^X\(\/\).*/{ - s//\1/ - q - } - s/.*/./; q'` - as_dir="$ac_dir"; as_fn_mkdir_p - ac_builddir=. - -case "$ac_dir" in -.) ac_dir_suffix= ac_top_builddir_sub=. ac_top_build_prefix= ;; -*) - ac_dir_suffix=/`$as_echo "$ac_dir" | sed 's|^\.[\\/]||'` - # A ".." for each directory in $ac_dir_suffix. - ac_top_builddir_sub=`$as_echo "$ac_dir_suffix" | sed 's|/[^\\/]*|/..|g;s|/||'` - case $ac_top_builddir_sub in - "") ac_top_builddir_sub=. ac_top_build_prefix= ;; - *) ac_top_build_prefix=$ac_top_builddir_sub/ ;; - esac ;; -esac -ac_abs_top_builddir=$ac_pwd -ac_abs_builddir=$ac_pwd$ac_dir_suffix -# for backward compatibility: -ac_top_builddir=$ac_top_build_prefix - -case $srcdir in - .) # We are building in place. - ac_srcdir=. - ac_top_srcdir=$ac_top_builddir_sub - ac_abs_top_srcdir=$ac_pwd ;; - [\\/]* | ?:[\\/]* ) # Absolute name. - ac_srcdir=$srcdir$ac_dir_suffix; - ac_top_srcdir=$srcdir - ac_abs_top_srcdir=$srcdir ;; - *) # Relative name. - ac_srcdir=$ac_top_build_prefix$srcdir$ac_dir_suffix - ac_top_srcdir=$ac_top_build_prefix$srcdir - ac_abs_top_srcdir=$ac_pwd/$srcdir ;; -esac -ac_abs_srcdir=$ac_abs_top_srcdir$ac_dir_suffix - - - case $ac_mode in - :F) - # - # CONFIG_FILE - # - - case $INSTALL in - [\\/$]* | ?:[\\/]* ) ac_INSTALL=$INSTALL ;; - *) ac_INSTALL=$ac_top_build_prefix$INSTALL ;; - esac -_ACEOF - -cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1 -# If the template does not know about datarootdir, expand it. -# FIXME: This hack should be removed a few years after 2.60. -ac_datarootdir_hack=; ac_datarootdir_seen= -ac_sed_dataroot=' -/datarootdir/ { - p - q -} -/@datadir@/p -/@docdir@/p -/@infodir@/p -/@localedir@/p -/@mandir@/p' -case `eval "sed -n \"\$ac_sed_dataroot\" $ac_file_inputs"` in -*datarootdir*) ac_datarootdir_seen=yes;; -*@datadir@*|*@docdir@*|*@infodir@*|*@localedir@*|*@mandir@*) - { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: $ac_file_inputs seems to ignore the --datarootdir setting" >&5 -$as_echo "$as_me: WARNING: $ac_file_inputs seems to ignore the --datarootdir setting" >&2;} -_ACEOF -cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1 - ac_datarootdir_hack=' - s&@datadir@&$datadir&g - s&@docdir@&$docdir&g - s&@infodir@&$infodir&g - s&@localedir@&$localedir&g - s&@mandir@&$mandir&g - s&\\\${datarootdir}&$datarootdir&g' ;; -esac -_ACEOF - -# Neutralize VPATH when `$srcdir' = `.'. -# Shell code in configure.ac might set extrasub. -# FIXME: do we really want to maintain this feature? -cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1 -ac_sed_extra="$ac_vpsub -$extrasub -_ACEOF -cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1 -:t -/@[a-zA-Z_][a-zA-Z_0-9]*@/!b -s|@configure_input@|$ac_sed_conf_input|;t t -s&@top_builddir@&$ac_top_builddir_sub&;t t -s&@top_build_prefix@&$ac_top_build_prefix&;t t -s&@srcdir@&$ac_srcdir&;t t -s&@abs_srcdir@&$ac_abs_srcdir&;t t -s&@top_srcdir@&$ac_top_srcdir&;t t -s&@abs_top_srcdir@&$ac_abs_top_srcdir&;t t -s&@builddir@&$ac_builddir&;t t -s&@abs_builddir@&$ac_abs_builddir&;t t -s&@abs_top_builddir@&$ac_abs_top_builddir&;t t -s&@INSTALL@&$ac_INSTALL&;t t -$ac_datarootdir_hack -" -eval sed \"\$ac_sed_extra\" "$ac_file_inputs" | $AWK -f "$ac_tmp/subs.awk" \ - >$ac_tmp/out || as_fn_error $? "could not create $ac_file" "$LINENO" 5 - -test -z "$ac_datarootdir_hack$ac_datarootdir_seen" && - { ac_out=`sed -n '/\${datarootdir}/p' "$ac_tmp/out"`; test -n "$ac_out"; } && - { ac_out=`sed -n '/^[ ]*datarootdir[ ]*:*=/p' \ - "$ac_tmp/out"`; test -z "$ac_out"; } && - { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: $ac_file contains a reference to the variable \`datarootdir' -which seems to be undefined. Please make sure it is defined" >&5 -$as_echo "$as_me: WARNING: $ac_file contains a reference to the variable \`datarootdir' -which seems to be undefined. Please make sure it is defined" >&2;} - - rm -f "$ac_tmp/stdin" - case $ac_file in - -) cat "$ac_tmp/out" && rm -f "$ac_tmp/out";; - *) rm -f "$ac_file" && mv "$ac_tmp/out" "$ac_file";; - esac \ - || as_fn_error $? "could not create $ac_file" "$LINENO" 5 - ;; - :H) - # - # CONFIG_HEADER - # - if test x"$ac_file" != x-; then - { - $as_echo "/* $configure_input */" \ - && eval '$AWK -f "$ac_tmp/defines.awk"' "$ac_file_inputs" - } >"$ac_tmp/config.h" \ - || as_fn_error $? "could not create $ac_file" "$LINENO" 5 - if diff "$ac_file" "$ac_tmp/config.h" >/dev/null 2>&1; then - { $as_echo "$as_me:${as_lineno-$LINENO}: $ac_file is unchanged" >&5 -$as_echo "$as_me: $ac_file is unchanged" >&6;} - else - rm -f "$ac_file" - mv "$ac_tmp/config.h" "$ac_file" \ - || as_fn_error $? "could not create $ac_file" "$LINENO" 5 - fi - else - $as_echo "/* $configure_input */" \ - && eval '$AWK -f "$ac_tmp/defines.awk"' "$ac_file_inputs" \ - || as_fn_error $? "could not create -" "$LINENO" 5 - fi - ;; - - :C) { $as_echo "$as_me:${as_lineno-$LINENO}: executing $ac_file commands" >&5 -$as_echo "$as_me: executing $ac_file commands" >&6;} - ;; - esac - - - case $ac_file$ac_mode in - "libtool":C) - - # See if we are running on zsh, and set the options that allow our - # commands through without removal of \ escapes. - if test -n "${ZSH_VERSION+set}"; then - setopt NO_GLOB_SUBST - fi - - cfgfile=${ofile}T - trap "$RM \"$cfgfile\"; exit 1" 1 2 15 - $RM "$cfgfile" - - cat <<_LT_EOF >> "$cfgfile" -#! $SHELL -# Generated automatically by $as_me ($PACKAGE) $VERSION -# Libtool was configured on host `(hostname || uname -n) 2>/dev/null | sed 1q`: -# NOTE: Changes made to this file will be lost: look at ltmain.sh. - -# Provide generalized library-building support services. -# Written by Gordon Matzigkeit, 1996 - -# Copyright (C) 2014 Free Software Foundation, Inc. -# This is free software; see the source for copying conditions. There is NO -# warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - -# GNU Libtool is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of of the License, or -# (at your option) any later version. -# -# As a special exception to the GNU General Public License, if you -# distribute this file as part of a program or library that is built -# using GNU Libtool, you may include this file under the same -# distribution terms that you use for the rest of that program. -# -# GNU Libtool is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - - -# The names of the tagged configurations supported by this script. -available_tags='' - -# Configured defaults for sys_lib_dlsearch_path munging. -: \${LT_SYS_LIBRARY_PATH="$configure_time_lt_sys_library_path"} - -# ### BEGIN LIBTOOL CONFIG - -# Which release of libtool.m4 was used? -macro_version=$macro_version -macro_revision=$macro_revision - -# Whether or not to build shared libraries. -build_libtool_libs=$enable_shared - -# Whether or not to build static libraries. -build_old_libs=$enable_static - -# What type of objects to build. -pic_mode=$pic_mode - -# Whether or not to optimize for fast installation. -fast_install=$enable_fast_install - -# Shared archive member basename,for filename based shared library versioning on AIX. -shared_archive_member_spec=$shared_archive_member_spec - -# Shell to use when invoking shell scripts. -SHELL=$lt_SHELL - -# An echo program that protects backslashes. -ECHO=$lt_ECHO - -# The PATH separator for the build system. -PATH_SEPARATOR=$lt_PATH_SEPARATOR - -# The host system. -host_alias=$host_alias -host=$host -host_os=$host_os - -# The build system. -build_alias=$build_alias -build=$build -build_os=$build_os - -# A sed program that does not truncate output. -SED=$lt_SED - -# Sed that helps us avoid accidentally triggering echo(1) options like -n. -Xsed="\$SED -e 1s/^X//" - -# A grep program that handles long lines. -GREP=$lt_GREP - -# An ERE matcher. -EGREP=$lt_EGREP - -# A literal string matcher. -FGREP=$lt_FGREP - -# A BSD- or MS-compatible name lister. -NM=$lt_NM - -# Whether we need soft or hard links. -LN_S=$lt_LN_S - -# What is the maximum length of a command? -max_cmd_len=$max_cmd_len - -# Object file suffix (normally "o"). -objext=$ac_objext - -# Executable file suffix (normally ""). -exeext=$exeext - -# whether the shell understands "unset". -lt_unset=$lt_unset - -# turn spaces into newlines. -SP2NL=$lt_lt_SP2NL - -# turn newlines into spaces. -NL2SP=$lt_lt_NL2SP - -# convert \$build file names to \$host format. -to_host_file_cmd=$lt_cv_to_host_file_cmd - -# convert \$build files to toolchain format. -to_tool_file_cmd=$lt_cv_to_tool_file_cmd - -# An object symbol dumper. -OBJDUMP=$lt_OBJDUMP - -# Method to check whether dependent libraries are shared objects. -deplibs_check_method=$lt_deplibs_check_method - -# Command to use when deplibs_check_method = "file_magic". -file_magic_cmd=$lt_file_magic_cmd - -# How to find potential files when deplibs_check_method = "file_magic". -file_magic_glob=$lt_file_magic_glob - -# Find potential files using nocaseglob when deplibs_check_method = "file_magic". -want_nocaseglob=$lt_want_nocaseglob - -# DLL creation program. -DLLTOOL=$lt_DLLTOOL - -# Command to associate shared and link libraries. -sharedlib_from_linklib_cmd=$lt_sharedlib_from_linklib_cmd - -# The archiver. -AR=$lt_AR - -# Flags to create an archive. -AR_FLAGS=$lt_AR_FLAGS - -# How to feed a file listing to the archiver. -archiver_list_spec=$lt_archiver_list_spec - -# A symbol stripping program. -STRIP=$lt_STRIP - -# Commands used to install an old-style archive. -RANLIB=$lt_RANLIB -old_postinstall_cmds=$lt_old_postinstall_cmds -old_postuninstall_cmds=$lt_old_postuninstall_cmds - -# Whether to use a lock for old archive extraction. -lock_old_archive_extraction=$lock_old_archive_extraction - -# A C compiler. -LTCC=$lt_CC - -# LTCC compiler flags. -LTCFLAGS=$lt_CFLAGS - -# Take the output of nm and produce a listing of raw symbols and C names. -global_symbol_pipe=$lt_lt_cv_sys_global_symbol_pipe - -# Transform the output of nm in a proper C declaration. -global_symbol_to_cdecl=$lt_lt_cv_sys_global_symbol_to_cdecl - -# Transform the output of nm into a list of symbols to manually relocate. -global_symbol_to_import=$lt_lt_cv_sys_global_symbol_to_import - -# Transform the output of nm in a C name address pair. -global_symbol_to_c_name_address=$lt_lt_cv_sys_global_symbol_to_c_name_address - -# Transform the output of nm in a C name address pair when lib prefix is needed. -global_symbol_to_c_name_address_lib_prefix=$lt_lt_cv_sys_global_symbol_to_c_name_address_lib_prefix - -# The name lister interface. -nm_interface=$lt_lt_cv_nm_interface - -# Specify filename containing input files for \$NM. -nm_file_list_spec=$lt_nm_file_list_spec - -# The root where to search for dependent libraries,and where our libraries should be installed. -lt_sysroot=$lt_sysroot - -# Command to truncate a binary pipe. -lt_truncate_bin=$lt_lt_cv_truncate_bin - -# The name of the directory that contains temporary libtool files. -objdir=$objdir - -# Used to examine libraries when file_magic_cmd begins with "file". -MAGIC_CMD=$MAGIC_CMD - -# Must we lock files when doing compilation? -need_locks=$lt_need_locks - -# Manifest tool. -MANIFEST_TOOL=$lt_MANIFEST_TOOL - -# Tool to manipulate archived DWARF debug symbol files on Mac OS X. -DSYMUTIL=$lt_DSYMUTIL - -# Tool to change global to local symbols on Mac OS X. -NMEDIT=$lt_NMEDIT - -# Tool to manipulate fat objects and archives on Mac OS X. -LIPO=$lt_LIPO - -# ldd/readelf like tool for Mach-O binaries on Mac OS X. -OTOOL=$lt_OTOOL - -# ldd/readelf like tool for 64 bit Mach-O binaries on Mac OS X 10.4. -OTOOL64=$lt_OTOOL64 - -# Old archive suffix (normally "a"). -libext=$libext - -# Shared library suffix (normally ".so"). -shrext_cmds=$lt_shrext_cmds - -# The commands to extract the exported symbol list from a shared archive. -extract_expsyms_cmds=$lt_extract_expsyms_cmds - -# Variables whose values should be saved in libtool wrapper scripts and -# restored at link time. -variables_saved_for_relink=$lt_variables_saved_for_relink - -# Do we need the "lib" prefix for modules? -need_lib_prefix=$need_lib_prefix - -# Do we need a version for libraries? -need_version=$need_version - -# Library versioning type. -version_type=$version_type - -# Shared library runtime path variable. -runpath_var=$runpath_var - -# Shared library path variable. -shlibpath_var=$shlibpath_var - -# Is shlibpath searched before the hard-coded library search path? -shlibpath_overrides_runpath=$shlibpath_overrides_runpath - -# Format of library name prefix. -libname_spec=$lt_libname_spec - -# List of archive names. First name is the real one, the rest are links. -# The last name is the one that the linker finds with -lNAME -library_names_spec=$lt_library_names_spec - -# The coded name of the library, if different from the real name. -soname_spec=$lt_soname_spec - -# Permission mode override for installation of shared libraries. -install_override_mode=$lt_install_override_mode - -# Command to use after installation of a shared archive. -postinstall_cmds=$lt_postinstall_cmds - -# Command to use after uninstallation of a shared archive. -postuninstall_cmds=$lt_postuninstall_cmds - -# Commands used to finish a libtool library installation in a directory. -finish_cmds=$lt_finish_cmds - -# As "finish_cmds", except a single script fragment to be evaled but -# not shown. -finish_eval=$lt_finish_eval - -# Whether we should hardcode library paths into libraries. -hardcode_into_libs=$hardcode_into_libs - -# Compile-time system search path for libraries. -sys_lib_search_path_spec=$lt_sys_lib_search_path_spec - -# Detected run-time system search path for libraries. -sys_lib_dlsearch_path_spec=$lt_configure_time_dlsearch_path - -# Explicit LT_SYS_LIBRARY_PATH set during ./configure time. -configure_time_lt_sys_library_path=$lt_configure_time_lt_sys_library_path - -# Whether dlopen is supported. -dlopen_support=$enable_dlopen - -# Whether dlopen of programs is supported. -dlopen_self=$enable_dlopen_self - -# Whether dlopen of statically linked programs is supported. -dlopen_self_static=$enable_dlopen_self_static - -# Commands to strip libraries. -old_striplib=$lt_old_striplib -striplib=$lt_striplib - - -# The linker used to build libraries. -LD=$lt_LD - -# How to create reloadable object files. -reload_flag=$lt_reload_flag -reload_cmds=$lt_reload_cmds - -# Commands used to build an old-style archive. -old_archive_cmds=$lt_old_archive_cmds - -# A language specific compiler. -CC=$lt_compiler - -# Is the compiler the GNU compiler? -with_gcc=$GCC - -# Compiler flag to turn off builtin functions. -no_builtin_flag=$lt_lt_prog_compiler_no_builtin_flag - -# Additional compiler flags for building library objects. -pic_flag=$lt_lt_prog_compiler_pic - -# How to pass a linker flag through the compiler. -wl=$lt_lt_prog_compiler_wl - -# Compiler flag to prevent dynamic linking. -link_static_flag=$lt_lt_prog_compiler_static - -# Does compiler simultaneously support -c and -o options? -compiler_c_o=$lt_lt_cv_prog_compiler_c_o - -# Whether or not to add -lc for building shared libraries. -build_libtool_need_lc=$archive_cmds_need_lc - -# Whether or not to disallow shared libs when runtime libs are static. -allow_libtool_libs_with_static_runtimes=$enable_shared_with_static_runtimes - -# Compiler flag to allow reflexive dlopens. -export_dynamic_flag_spec=$lt_export_dynamic_flag_spec - -# Compiler flag to generate shared objects directly from archives. -whole_archive_flag_spec=$lt_whole_archive_flag_spec - -# Whether the compiler copes with passing no objects directly. -compiler_needs_object=$lt_compiler_needs_object - -# Create an old-style archive from a shared archive. -old_archive_from_new_cmds=$lt_old_archive_from_new_cmds - -# Create a temporary old-style archive to link instead of a shared archive. -old_archive_from_expsyms_cmds=$lt_old_archive_from_expsyms_cmds - -# Commands used to build a shared archive. -archive_cmds=$lt_archive_cmds -archive_expsym_cmds=$lt_archive_expsym_cmds - -# Commands used to build a loadable module if different from building -# a shared archive. -module_cmds=$lt_module_cmds -module_expsym_cmds=$lt_module_expsym_cmds - -# Whether we are building with GNU ld or not. -with_gnu_ld=$lt_with_gnu_ld - -# Flag that allows shared libraries with undefined symbols to be built. -allow_undefined_flag=$lt_allow_undefined_flag - -# Flag that enforces no undefined symbols. -no_undefined_flag=$lt_no_undefined_flag - -# Flag to hardcode \$libdir into a binary during linking. -# This must work even if \$libdir does not exist -hardcode_libdir_flag_spec=$lt_hardcode_libdir_flag_spec - -# Whether we need a single "-rpath" flag with a separated argument. -hardcode_libdir_separator=$lt_hardcode_libdir_separator - -# Set to "yes" if using DIR/libNAME\$shared_ext during linking hardcodes -# DIR into the resulting binary. -hardcode_direct=$hardcode_direct - -# Set to "yes" if using DIR/libNAME\$shared_ext during linking hardcodes -# DIR into the resulting binary and the resulting library dependency is -# "absolute",i.e impossible to change by setting \$shlibpath_var if the -# library is relocated. -hardcode_direct_absolute=$hardcode_direct_absolute - -# Set to "yes" if using the -LDIR flag during linking hardcodes DIR -# into the resulting binary. -hardcode_minus_L=$hardcode_minus_L - -# Set to "yes" if using SHLIBPATH_VAR=DIR during linking hardcodes DIR -# into the resulting binary. -hardcode_shlibpath_var=$hardcode_shlibpath_var - -# Set to "yes" if building a shared library automatically hardcodes DIR -# into the library and all subsequent libraries and executables linked -# against it. -hardcode_automatic=$hardcode_automatic - -# Set to yes if linker adds runtime paths of dependent libraries -# to runtime path list. -inherit_rpath=$inherit_rpath - -# Whether libtool must link a program against all its dependency libraries. -link_all_deplibs=$link_all_deplibs - -# Set to "yes" if exported symbols are required. -always_export_symbols=$always_export_symbols - -# The commands to list exported symbols. -export_symbols_cmds=$lt_export_symbols_cmds - -# Symbols that should not be listed in the preloaded symbols. -exclude_expsyms=$lt_exclude_expsyms - -# Symbols that must always be exported. -include_expsyms=$lt_include_expsyms - -# Commands necessary for linking programs (against libraries) with templates. -prelink_cmds=$lt_prelink_cmds - -# Commands necessary for finishing linking programs. -postlink_cmds=$lt_postlink_cmds - -# Specify filename containing input files. -file_list_spec=$lt_file_list_spec - -# How to hardcode a shared library path into an executable. -hardcode_action=$hardcode_action - -# ### END LIBTOOL CONFIG - -_LT_EOF - - cat <<'_LT_EOF' >> "$cfgfile" - -# ### BEGIN FUNCTIONS SHARED WITH CONFIGURE - -# func_munge_path_list VARIABLE PATH -# ----------------------------------- -# VARIABLE is name of variable containing _space_ separated list of -# directories to be munged by the contents of PATH, which is string -# having a format: -# "DIR[:DIR]:" -# string "DIR[ DIR]" will be prepended to VARIABLE -# ":DIR[:DIR]" -# string "DIR[ DIR]" will be appended to VARIABLE -# "DIRP[:DIRP]::[DIRA:]DIRA" -# string "DIRP[ DIRP]" will be prepended to VARIABLE and string -# "DIRA[ DIRA]" will be appended to VARIABLE -# "DIR[:DIR]" -# VARIABLE will be replaced by "DIR[ DIR]" -func_munge_path_list () -{ - case x$2 in - x) - ;; - *:) - eval $1=\"`$ECHO $2 | $SED 's/:/ /g'` \$$1\" - ;; - x:*) - eval $1=\"\$$1 `$ECHO $2 | $SED 's/:/ /g'`\" - ;; - *::*) - eval $1=\"\$$1\ `$ECHO $2 | $SED -e 's/.*:://' -e 's/:/ /g'`\" - eval $1=\"`$ECHO $2 | $SED -e 's/::.*//' -e 's/:/ /g'`\ \$$1\" - ;; - *) - eval $1=\"`$ECHO $2 | $SED 's/:/ /g'`\" - ;; - esac -} - - -# Calculate cc_basename. Skip known compiler wrappers and cross-prefix. -func_cc_basename () -{ - for cc_temp in $*""; do - case $cc_temp in - compile | *[\\/]compile | ccache | *[\\/]ccache ) ;; - distcc | *[\\/]distcc | purify | *[\\/]purify ) ;; - \-*) ;; - *) break;; - esac - done - func_cc_basename_result=`$ECHO "$cc_temp" | $SED "s%.*/%%; s%^$host_alias-%%"` -} - - -# ### END FUNCTIONS SHARED WITH CONFIGURE - -_LT_EOF - - case $host_os in - aix3*) - cat <<\_LT_EOF >> "$cfgfile" -# AIX sometimes has problems with the GCC collect2 program. For some -# reason, if we set the COLLECT_NAMES environment variable, the problems -# vanish in a puff of smoke. -if test set != "${COLLECT_NAMES+set}"; then - COLLECT_NAMES= - export COLLECT_NAMES -fi -_LT_EOF - ;; - esac - - -ltmain=$ac_aux_dir/ltmain.sh - - - # We use sed instead of cat because bash on DJGPP gets confused if - # if finds mixed CR/LF and LF-only lines. Since sed operates in - # text mode, it properly converts lines to CR/LF. This bash problem - # is reportedly fixed, but why not run on old versions too? - sed '$q' "$ltmain" >> "$cfgfile" \ - || (rm -f "$cfgfile"; exit 1) - - mv -f "$cfgfile" "$ofile" || - (rm -f "$ofile" && cp "$cfgfile" "$ofile" && rm -f "$cfgfile") - chmod +x "$ofile" - - ;; - - esac -done # for ac_tag - - -as_fn_exit 0 -_ACEOF -ac_clean_files=$ac_clean_files_save - -test $ac_write_fail = 0 || - as_fn_error $? "write failure creating $CONFIG_STATUS" "$LINENO" 5 - - -# configure is writing to config.log, and then calls config.status. -# config.status does its own redirection, appending to config.log. -# Unfortunately, on DOS this fails, as config.log is still kept open -# by configure, so config.status won't be able to write to it; its -# output is simply discarded. So we exec the FD to /dev/null, -# effectively closing config.log, so it can be properly (re)opened and -# appended to by config.status. When coming back to configure, we -# need to make the FD available again. -if test "$no_create" != yes; then - ac_cs_success=: - ac_config_status_args= - test "$silent" = yes && - ac_config_status_args="$ac_config_status_args --quiet" - exec 5>/dev/null - $SHELL $CONFIG_STATUS $ac_config_status_args || ac_cs_success=false - exec 5>>config.log - # Use ||, not &&, to avoid exiting from the if with $? = 1, which - # would make configure fail if this is the last instruction. - $ac_cs_success || as_fn_exit 1 -fi -if test -n "$ac_unrecognized_opts" && test "$enable_option_checking" != no; then - { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: unrecognized options: $ac_unrecognized_opts" >&5 -$as_echo "$as_me: WARNING: unrecognized options: $ac_unrecognized_opts" >&2;} -fi - +#!/bin/sh +dir="`dirname "$0"`/autosetup" +#@@INITCHECK@@# +WRAPPER="$0"; export WRAPPER; exec "`"$dir/autosetup-find-tclsh"`" "$dir/autosetup" "$@" diff --git a/configure.ac b/configure.ac deleted file mode 100644 index 9a1fe1a415..0000000000 --- a/configure.ac +++ /dev/null @@ -1,740 +0,0 @@ -# -# The build process allows for using a cross-compiler. But the default -# action is to target the same platform that we are running on. The -# configure script needs to discover the following properties of the -# build and target systems: -# -# srcdir -# -# The is the name of the directory that contains the -# "configure" shell script. All source files are -# located relative to this directory. -# -# bindir -# -# The name of the directory where executables should be -# written by the "install" target of the makefile. -# -# program_prefix -# -# Add this prefix to the names of all executables that run -# on the target machine. Default: "" -# -# ENABLE_SHARED -# -# True if shared libraries should be generated. -# -# BUILD_CC -# -# The name of a command that is used to convert C -# source files into executables that run on the build -# platform. -# -# BUILD_CFLAGS -# -# Switches that the build compiler needs in order to construct -# command-line programs. -# -# BUILD_LIBS -# -# Libraries that the build compiler needs in order to construct -# command-line programs. -# -# BUILD_EXEEXT -# -# The filename extension for executables on the build -# platform. "" for Unix and ".exe" for Windows. -# -# TCL_* -# -# Lots of values are read in from the tclConfig.sh script, -# if that script is available. This values are used for -# constructing and installing the TCL extension. -# -# TARGET_READLINE_LIBS -# -# This is the library directives passed to the target linker -# that cause the executable to link against the readline library. -# This might be a switch like "-lreadline" or pathnames of library -# file like "../../src/libreadline.a". -# -# TARGET_READLINE_INC -# -# This variables define the directory that contain header -# files for the readline library. If the compiler is able -# to find on its own, then this can be blank. -# -# TARGET_EXEEXT -# -# The filename extension for executables on the -# target platform. "" for Unix and ".exe" for windows. -# -# This configure.in file is easy to reuse on other projects. Just -# change the argument to AC_INIT(). And disable any features that -# you don't need (for example BLT) by erasing or commenting out -# the corresponding code. -# -AC_INIT(sqlcipher, m4_esyscmd([cat VERSION | tr -d '\n'])) - -dnl Make sure the local VERSION file matches this configure script -sqlite_version_sanity_check=`cat $srcdir/VERSION | tr -d '\n'` -if test "$PACKAGE_VERSION" != "$sqlite_version_sanity_check" ; then -AC_MSG_ERROR([configure script is out of date: - configure \$PACKAGE_VERSION = $PACKAGE_VERSION - top level VERSION file = $sqlite_version_sanity_check -please regen with autoconf]) -fi - -######### -# Programs needed -# -AC_PROG_LIBTOOL -AC_PROG_INSTALL - -######### -# Enable large file support (if special flags are necessary) -# -AC_SYS_LARGEFILE - -######### -# Check for needed/wanted data types -AC_CHECK_TYPES([int8_t, int16_t, int32_t, int64_t, intptr_t, uint8_t, - uint16_t, uint32_t, uint64_t, uintptr_t]) - -######### -# Check for needed/wanted headers -AC_CHECK_HEADERS([sys/types.h stdlib.h stdint.h inttypes.h malloc.h]) - -######### -# Figure out whether or not we have these functions -# -AC_CHECK_FUNCS([fdatasync gmtime_r isnan localtime_r localtime_s malloc_usable_size strchrnul usleep utime]) - -######### -# By default, we use the amalgamation (this may be changed below...) -# -USE_AMALGAMATION=1 - -######### -# See whether we can run specific tclsh versions known to work well; -# if not, then we fall back to plain tclsh. -# TODO: try other versions before falling back? -# -AC_CHECK_PROGS(TCLSH_CMD, [tclsh8.6 tclsh8.5 tclsh], none) -if test "$TCLSH_CMD" = "none"; then - # If we can't find a local tclsh, then building the amalgamation will fail. - # We act as though --disable-amalgamation has been used. - echo "Warning: can't find tclsh - defaulting to non-amalgamation build." - USE_AMALGAMATION=0 - TCLSH_CMD="tclsh" -fi -AC_SUBST(TCLSH_CMD) - -AC_ARG_VAR([TCLLIBDIR], [Where to install tcl plugin]) -if test "x${TCLLIBDIR+set}" != "xset" ; then - TCLLIBDIR='$(libdir)' - for i in `echo 'puts stdout $auto_path' | ${TCLSH_CMD}` ; do - TCLLIBDIR=$i - break - done - TCLLIBDIR="${TCLLIBDIR}/sqlite3" -fi - -######### -# Set up an appropriate program prefix -# -if test "$program_prefix" = "NONE"; then - program_prefix="" -fi -AC_SUBST(program_prefix) - -VERSION=[`cat $srcdir/VERSION | sed 's/^\([0-9]*\.*[0-9]*\).*/\1/'`] -AC_MSG_NOTICE(Version set to $VERSION) -AC_SUBST(VERSION) -RELEASE=`cat $srcdir/VERSION` -AC_MSG_NOTICE(Release set to $RELEASE) -AC_SUBST(RELEASE) -VERSION_NUMBER=[`cat $srcdir/VERSION \ - | sed 's/[^0-9]/ /g' \ - | awk '{printf "%d%03d%03d",$1,$2,$3}'`] -AC_MSG_NOTICE(Version number set to $VERSION_NUMBER) -AC_SUBST(VERSION_NUMBER) - -######### -# Locate a compiler for the build machine. This compiler should -# generate command-line programs that run on the build machine. -# -if test x"$cross_compiling" = xno; then - BUILD_CC=$CC - BUILD_CFLAGS=$CFLAGS -else - if test "${BUILD_CC+set}" != set; then - AC_CHECK_PROGS(BUILD_CC, gcc cc cl) - fi - if test "${BUILD_CFLAGS+set}" != set; then - BUILD_CFLAGS="-g" - fi -fi -AC_SUBST(BUILD_CC) - -########## -# Do we want to support multithreaded use of sqlite -# -AC_ARG_ENABLE(threadsafe, -AC_HELP_STRING([--disable-threadsafe],[Disable mutexing]),,enable_threadsafe=yes) -AC_MSG_CHECKING([whether to support threadsafe operation]) -if test "$enable_threadsafe" = "no"; then - SQLITE_THREADSAFE=0 - AC_MSG_RESULT([no]) -else - SQLITE_THREADSAFE=1 - AC_MSG_RESULT([yes]) -fi -AC_SUBST(SQLITE_THREADSAFE) - -if test "$SQLITE_THREADSAFE" = "1"; then - AC_SEARCH_LIBS(pthread_create, pthread) - AC_SEARCH_LIBS(pthread_mutexattr_init, pthread) -fi - -########## -# Which crypto library do we use -# -AC_ARG_WITH([crypto-lib], -AC_HELP_STRING([--with-crypto-lib],[Specify which crypto library to use]), -crypto_lib=$withval) -AC_MSG_CHECKING([for crypto library to use]) -if test "$crypto_lib" = "none"; then - AC_MSG_RESULT([none]) -else - if test "$crypto_lib" = "commoncrypto"; then - CFLAGS+=" -DSQLCIPHER_CRYPTO_CC" - BUILD_CFLAGS+=" -DSQLCIPHER_CRYPTO_CC" - AC_MSG_RESULT([commoncrypto]) - else - if test "$crypto_lib" = "libtomcrypt"; then - CFLAGS+=" -DSQLCIPHER_CRYPTO_LIBTOMCRYPT" - BUILD_CFLAGS+=" -DSQLCIPHER_CRYPTO_LIBTOMCRYPT" - AC_MSG_RESULT([libtomcrypt]) - AC_CHECK_LIB([tomcrypt], [register_cipher], , - AC_MSG_ERROR([Library crypto not found. Install libtomcrypt!"])) - else - CFLAGS+=" -DSQLCIPHER_CRYPTO_OPENSSL" - BUILD_CFLAGS+=" -DSQLCIPHER_CRYPTO_OPENSSL" - AC_MSG_RESULT([openssl]) - AC_CHECK_LIB([crypto], [HMAC_Init_ex], , - AC_MSG_ERROR([Library crypto not found. Install openssl!"])) - fi - fi -fi - -########## -# Do we want to allow a connection created in one thread to be used -# in another thread. This does not work on many Linux systems (ex: RedHat 9) -# due to bugs in the threading implementations. This is thus off by default. -# -AC_ARG_ENABLE(cross-thread-connections, -AC_HELP_STRING([--enable-cross-thread-connections],[Allow connection sharing across threads]),,enable_xthreadconnect=no) -AC_MSG_CHECKING([whether to allow connections to be shared across threads]) -if test "$enable_xthreadconnect" = "no"; then - XTHREADCONNECT='' - AC_MSG_RESULT([no]) -else - XTHREADCONNECT='-DSQLITE_ALLOW_XTHREAD_CONNECT=1' - AC_MSG_RESULT([yes]) -fi -AC_SUBST(XTHREADCONNECT) - -########## -# Do we want to support release -# -AC_ARG_ENABLE(releasemode, -AC_HELP_STRING([--enable-releasemode],[Support libtool link to release mode]),,enable_releasemode=no) -AC_MSG_CHECKING([whether to support shared library linked as release mode or not]) -if test "$enable_releasemode" = "no"; then - ALLOWRELEASE="" - AC_MSG_RESULT([no]) -else - ALLOWRELEASE="-release `cat $srcdir/VERSION`" - AC_MSG_RESULT([yes]) -fi -AC_SUBST(ALLOWRELEASE) - -########## -# Do we want temporary databases in memory -# -AC_ARG_ENABLE(tempstore, -AC_HELP_STRING([--enable-tempstore],[Use an in-ram database for temporary tables (never,no,yes,always)]),,enable_tempstore=no) -AC_MSG_CHECKING([whether to use an in-ram database for temporary tables]) -case "$enable_tempstore" in - never ) - TEMP_STORE=0 - AC_MSG_RESULT([never]) - ;; - no ) - TEMP_STORE=1 - AC_MSG_RESULT([no]) - ;; - yes ) - TEMP_STORE=2 - AC_MSG_RESULT([yes]) - ;; - always ) - TEMP_STORE=3 - AC_MSG_RESULT([always]) - ;; - * ) - TEMP_STORE=1 - AC_MSG_RESULT([no]) - ;; -esac - -AC_SUBST(TEMP_STORE) - -########### -# Lots of things are different if we are compiling for Windows using -# the CYGWIN environment. So check for that special case and handle -# things accordingly. -# -AC_MSG_CHECKING([if executables have the .exe suffix]) -if test "$config_BUILD_EXEEXT" = ".exe"; then - CYGWIN=yes - AC_MSG_RESULT(yes) -else - AC_MSG_RESULT(unknown) -fi -if test "$CYGWIN" != "yes"; then - AC_CYGWIN -fi -if test "$CYGWIN" = "yes"; then - BUILD_EXEEXT=.exe -else - BUILD_EXEEXT=$EXEEXT -fi -if test x"$cross_compiling" = xno; then - TARGET_EXEEXT=$BUILD_EXEEXT -else - TARGET_EXEEXT=$config_TARGET_EXEEXT -fi -if test "$TARGET_EXEEXT" = ".exe"; then - SQLITE_OS_UNIX=0 - SQLITE_OS_WIN=1 - CFLAGS="$CFLAGS -DSQLITE_OS_WIN=1" -else - SQLITE_OS_UNIX=1 - SQLITE_OS_WIN=0 - CFLAGS="$CFLAGS -DSQLITE_OS_UNIX=1" -fi - -AC_SUBST(BUILD_EXEEXT) -AC_SUBST(SQLITE_OS_UNIX) -AC_SUBST(SQLITE_OS_WIN) -AC_SUBST(TARGET_EXEEXT) - -########## -# Figure out all the parameters needed to compile against Tcl. -# -# This code is derived from the SC_PATH_TCLCONFIG and SC_LOAD_TCLCONFIG -# macros in the in the tcl.m4 file of the standard TCL distribution. -# Those macros could not be used directly since we have to make some -# minor changes to accomodate systems that do not have TCL installed. -# -AC_ARG_ENABLE(tcl, AC_HELP_STRING([--disable-tcl],[do not build TCL extension]), - [use_tcl=$enableval],[use_tcl=yes]) -if test "${use_tcl}" = "yes" ; then - AC_ARG_WITH(tcl, AC_HELP_STRING([--with-tcl=DIR],[directory containing tcl configuration (tclConfig.sh)]), with_tclconfig=${withval}) - AC_MSG_CHECKING([for Tcl configuration]) - AC_CACHE_VAL(ac_cv_c_tclconfig,[ - # First check to see if --with-tcl was specified. - if test x"${with_tclconfig}" != x ; then - if test -f "${with_tclconfig}/tclConfig.sh" ; then - ac_cv_c_tclconfig=`(cd ${with_tclconfig}; pwd)` - else - AC_MSG_ERROR([${with_tclconfig} directory doesn't contain tclConfig.sh]) - fi - fi - - # Start autosearch by asking tclsh - if test x"${ac_cv_c_tclconfig}" = x ; then - if test x"$cross_compiling" = xno; then - for i in `echo 'puts stdout $auto_path' | ${TCLSH_CMD}` - do - if test -f "$i/tclConfig.sh" ; then - ac_cv_c_tclconfig="$i" - break - fi - done - fi - fi - - # On ubuntu 14.10, $auto_path on tclsh is not quite correct. - # So try again after applying corrections. - if test x"${ac_cv_c_tclconfig}" = x ; then - if test x"$cross_compiling" = xno; then - for i in `echo 'puts stdout $auto_path' | ${TCLSH_CMD} | sed 's,/tcltk/tcl,/tcl,g'` - do - if test -f "$i/tclConfig.sh" ; then - ac_cv_c_tclconfig="$i" - break - fi - done - fi - fi - - # then check for a private Tcl installation - if test x"${ac_cv_c_tclconfig}" = x ; then - for i in \ - ../tcl \ - `ls -dr ../tcl[[8-9]].[[0-9]].[[0-9]]* 2>/dev/null` \ - `ls -dr ../tcl[[8-9]].[[0-9]] 2>/dev/null` \ - `ls -dr ../tcl[[8-9]].[[0-9]]* 2>/dev/null` \ - ../../tcl \ - `ls -dr ../../tcl[[8-9]].[[0-9]].[[0-9]]* 2>/dev/null` \ - `ls -dr ../../tcl[[8-9]].[[0-9]] 2>/dev/null` \ - `ls -dr ../../tcl[[8-9]].[[0-9]]* 2>/dev/null` \ - ../../../tcl \ - `ls -dr ../../../tcl[[8-9]].[[0-9]].[[0-9]]* 2>/dev/null` \ - `ls -dr ../../../tcl[[8-9]].[[0-9]] 2>/dev/null` \ - `ls -dr ../../../tcl[[8-9]].[[0-9]]* 2>/dev/null` - do - if test -f "$i/unix/tclConfig.sh" ; then - ac_cv_c_tclconfig=`(cd $i/unix; pwd)` - break - fi - done - fi - - # check in a few common install locations - if test x"${ac_cv_c_tclconfig}" = x ; then - for i in \ - `ls -d ${libdir} 2>/dev/null` \ - `ls -d /usr/local/lib 2>/dev/null` \ - `ls -d /usr/contrib/lib 2>/dev/null` \ - `ls -d /usr/lib 2>/dev/null` - do - if test -f "$i/tclConfig.sh" ; then - ac_cv_c_tclconfig=`(cd $i; pwd)` - break - fi - done - fi - - # check in a few other private locations - if test x"${ac_cv_c_tclconfig}" = x ; then - for i in \ - ${srcdir}/../tcl \ - `ls -dr ${srcdir}/../tcl[[8-9]].[[0-9]].[[0-9]]* 2>/dev/null` \ - `ls -dr ${srcdir}/../tcl[[8-9]].[[0-9]] 2>/dev/null` \ - `ls -dr ${srcdir}/../tcl[[8-9]].[[0-9]]* 2>/dev/null` - do - if test -f "$i/unix/tclConfig.sh" ; then - ac_cv_c_tclconfig=`(cd $i/unix; pwd)` - break - fi - done - fi - ]) - - if test x"${ac_cv_c_tclconfig}" = x ; then - use_tcl=no - AC_MSG_WARN(Can't find Tcl configuration definitions) - AC_MSG_WARN(*** Without Tcl the regression tests cannot be executed ***) - AC_MSG_WARN(*** Consider using --with-tcl=... to define location of Tcl ***) - else - TCL_BIN_DIR=${ac_cv_c_tclconfig} - AC_MSG_RESULT(found $TCL_BIN_DIR/tclConfig.sh) - - AC_MSG_CHECKING([for existence of $TCL_BIN_DIR/tclConfig.sh]) - if test -f "$TCL_BIN_DIR/tclConfig.sh" ; then - AC_MSG_RESULT([loading]) - . $TCL_BIN_DIR/tclConfig.sh - else - AC_MSG_RESULT([file not found]) - fi - - # - # If the TCL_BIN_DIR is the build directory (not the install directory), - # then set the common variable name to the value of the build variables. - # For example, the variable TCL_LIB_SPEC will be set to the value - # of TCL_BUILD_LIB_SPEC. An extension should make use of TCL_LIB_SPEC - # instead of TCL_BUILD_LIB_SPEC since it will work with both an - # installed and uninstalled version of Tcl. - # - - if test -f $TCL_BIN_DIR/Makefile ; then - TCL_LIB_SPEC=${TCL_BUILD_LIB_SPEC} - TCL_STUB_LIB_SPEC=${TCL_BUILD_STUB_LIB_SPEC} - TCL_STUB_LIB_PATH=${TCL_BUILD_STUB_LIB_PATH} - fi - - # - # eval is required to do the TCL_DBGX substitution - # - - eval "TCL_LIB_FILE=\"${TCL_LIB_FILE}\"" - eval "TCL_LIB_FLAG=\"${TCL_LIB_FLAG}\"" - eval "TCL_LIB_SPEC=\"${TCL_LIB_SPEC}\"" - - eval "TCL_STUB_LIB_FILE=\"${TCL_STUB_LIB_FILE}\"" - eval "TCL_STUB_LIB_FLAG=\"${TCL_STUB_LIB_FLAG}\"" - eval "TCL_STUB_LIB_SPEC=\"${TCL_STUB_LIB_SPEC}\"" - - AC_SUBST(TCL_VERSION) - AC_SUBST(TCL_BIN_DIR) - AC_SUBST(TCL_SRC_DIR) - AC_SUBST(TCL_INCLUDE_SPEC) - - AC_SUBST(TCL_LIB_FILE) - AC_SUBST(TCL_LIB_FLAG) - AC_SUBST(TCL_LIB_SPEC) - - AC_SUBST(TCL_STUB_LIB_FILE) - AC_SUBST(TCL_STUB_LIB_FLAG) - AC_SUBST(TCL_STUB_LIB_SPEC) - AC_SUBST(TCL_SHLIB_SUFFIX) - fi -fi -if test "${use_tcl}" = "no" ; then - HAVE_TCL="" -else - HAVE_TCL=1 -fi -AC_SUBST(HAVE_TCL) - -########## -# Figure out what C libraries are required to compile programs -# that use "readline()" library. -# -TARGET_READLINE_LIBS="" -TARGET_READLINE_INC="" -TARGET_HAVE_READLINE=0 -TARGET_HAVE_EDITLINE=0 -AC_ARG_ENABLE([editline], - [AC_HELP_STRING([--enable-editline],[enable BSD editline support])], - [with_editline=$enableval], - [with_editline=auto]) -AC_ARG_ENABLE([readline], - [AC_HELP_STRING([--disable-readline],[disable readline support])], - [with_readline=$enableval], - [with_readline=auto]) - -if test x"$with_editline" != xno; then - sLIBS=$LIBS - LIBS="" - TARGET_HAVE_EDITLINE=1 - AC_SEARCH_LIBS(readline,edit,[with_readline=no],[TARGET_HAVE_EDITLINE=0]) - TARGET_READLINE_LIBS=$LIBS - LIBS=$sLIBS -fi -if test x"$with_readline" != xno; then - found="yes" - - AC_ARG_WITH([readline-lib], - [AC_HELP_STRING([--with-readline-lib],[specify readline library])], - [with_readline_lib=$withval], - [with_readline_lib="auto"]) - if test "x$with_readline_lib" = xauto; then - save_LIBS="$LIBS" - LIBS="" - AC_SEARCH_LIBS(tgetent, [readline ncurses curses termcap], [term_LIBS="$LIBS"], [term_LIBS=""]) - AC_CHECK_LIB([readline], [readline], [TARGET_READLINE_LIBS="-lreadline"], [found="no"]) - TARGET_READLINE_LIBS="$TARGET_READLINE_LIBS $term_LIBS" - LIBS="$save_LIBS" - else - TARGET_READLINE_LIBS="$with_readline_lib" - fi - - AC_ARG_WITH([readline-inc], - [AC_HELP_STRING([--with-readline-inc],[specify readline include paths])], - [with_readline_inc=$withval], - [with_readline_inc="auto"]) - if test "x$with_readline_inc" = xauto; then - AC_CHECK_HEADER(readline.h, [found="yes"], [ - found="no" - if test "$cross_compiling" != yes; then - for dir in /usr /usr/local /usr/local/readline /usr/contrib /mingw; do - for subdir in include include/readline; do - AC_CHECK_FILE($dir/$subdir/readline.h, found=yes) - if test "$found" = "yes"; then - TARGET_READLINE_INC="-I$dir/$subdir" - break - fi - done - test "$found" = "yes" && break - done - fi - ]) - else - TARGET_READLINE_INC="$with_readline_inc" - fi - - if test x"$found" = xno; then - TARGET_READLINE_LIBS="" - TARGET_READLINE_INC="" - TARGET_HAVE_READLINE=0 - else - TARGET_HAVE_READLINE=1 - fi -fi - -AC_SUBST(TARGET_READLINE_LIBS) -AC_SUBST(TARGET_READLINE_INC) -AC_SUBST(TARGET_HAVE_READLINE) -AC_SUBST(TARGET_HAVE_EDITLINE) - -########## -# Figure out what C libraries are required to compile programs -# that use "fdatasync()" function. -# -AC_SEARCH_LIBS(fdatasync, [rt]) - -######### -# check for debug enabled -AC_ARG_ENABLE(debug, AC_HELP_STRING([--enable-debug],[enable debugging & verbose explain]), - [use_debug=$enableval],[use_debug=no]) -if test "${use_debug}" = "yes" ; then - TARGET_DEBUG="-DSQLITE_DEBUG=1" -else - TARGET_DEBUG="-DNDEBUG" -fi -AC_SUBST(TARGET_DEBUG) - -######### -# See whether we should use the amalgamation to build -AC_ARG_ENABLE(amalgamation, AC_HELP_STRING([--disable-amalgamation], - [Disable the amalgamation and instead build all files separately]), - [use_amalgamation=$enableval],[use_amalgamation=yes]) -if test "${use_amalgamation}" != "yes" ; then - USE_AMALGAMATION=0 -fi -AC_SUBST(USE_AMALGAMATION) - -######### -# See whether we should allow loadable extensions -AC_ARG_ENABLE(load-extension, AC_HELP_STRING([--disable-load-extension], - [Disable loading of external extensions]), - [use_loadextension=$enableval],[use_loadextension=yes]) -if test "${use_loadextension}" = "yes" ; then - OPT_FEATURE_FLAGS="" - AC_SEARCH_LIBS(dlopen, dl) -else - OPT_FEATURE_FLAGS="-DSQLITE_OMIT_LOAD_EXTENSION=1" -fi - -######### -# See whether we should enable Full Text Search extensions -AC_ARG_ENABLE(fts3, AC_HELP_STRING([--enable-fts3], - [Enable the FTS3 extension]), - [enable_fts3=yes],[enable_fts3=no]) -if test "${enable_fts3}" = "yes" ; then - OPT_FEATURE_FLAGS+=" -DSQLITE_ENABLE_FTS3" -fi -AC_ARG_ENABLE(fts4, AC_HELP_STRING([--enable-fts4], - [Enable the FTS4 extension]), - [enable_fts4=yes],[enable_fts4=no]) -if test "${enable_fts4}" = "yes" ; then - OPT_FEATURE_FLAGS+=" -DSQLITE_ENABLE_FTS4" - AC_SEARCH_LIBS([log],[m]) -fi -AC_ARG_ENABLE(fts5, AC_HELP_STRING([--enable-fts5], - [Enable the FTS5 extension]), - [enable_fts5=yes],[enable_fts5=no]) -if test "${enable_fts5}" = "yes" ; then - OPT_FEATURE_FLAGS+=" -DSQLITE_ENABLE_FTS5" - AC_SEARCH_LIBS([log],[m]) -fi - -######### -# See whether we should enable JSON1 -AC_ARG_ENABLE(json1, AC_HELP_STRING([--enable-json1], - [Enable the JSON1 extension]), - [enable_json1=yes],[enable_json1=no]) -if test "${enable_json1}" = "yes" ; then - OPT_FEATURE_FLAGS+=" -DSQLITE_ENABLE_JSON1" -fi - -######### -# See whether we should enable RTREE -AC_ARG_ENABLE(rtree, AC_HELP_STRING([--enable-rtree], - [Enable the RTREE extension]), - [enable_rtree=yes],[enable_rtree=no]) -if test "${enable_rtree}" = "yes" ; then - OPT_FEATURE_FLAGS+=" -DSQLITE_ENABLE_RTREE" -fi - -######### -# attempt to duplicate any OMITS and ENABLES into the $(OPT_FEATURE_FLAGS) parameter -for option in $CFLAGS $CPPFLAGS -do - case $option in - -DSQLITE_OMIT*) OPT_FEATURE_FLAGS="$OPT_FEATURE_FLAGS $option";; - -DSQLITE_ENABLE*) OPT_FEATURE_FLAGS="$OPT_FEATURE_FLAGS $option";; - esac -done -AC_SUBST(OPT_FEATURE_FLAGS) - - -# attempt to remove any OMITS and ENABLES from the $(CFLAGS) parameter -ac_temp_CFLAGS="" -for option in $CFLAGS -do - case $option in - -DSQLITE_OMIT*) ;; - -DSQLITE_ENABLE*) ;; - *) ac_temp_CFLAGS="$ac_temp_CFLAGS $option";; - esac -done -CFLAGS=$ac_temp_CFLAGS - - -# attempt to remove any OMITS and ENABLES from the $(CPPFLAGS) parameter -ac_temp_CPPFLAGS="" -for option in $CPPFLAGS -do - case $option in - -DSQLITE_OMIT*) ;; - -DSQLITE_ENABLE*) ;; - *) ac_temp_CPPFLAGS="$ac_temp_CPPFLAGS $option";; - esac -done -CPPFLAGS=$ac_temp_CPPFLAGS - - -# attempt to remove any OMITS and ENABLES from the $(BUILD_CFLAGS) parameter -ac_temp_BUILD_CFLAGS="" -for option in $BUILD_CFLAGS -do - case $option in - -DSQLITE_OMIT*) ;; - -DSQLITE_ENABLE*) ;; - *) ac_temp_BUILD_CFLAGS="$ac_temp_BUILD_CFLAGS $option";; - esac -done -BUILD_CFLAGS=$ac_temp_BUILD_CFLAGS - - -######### -# See whether we should use GCOV -AC_ARG_ENABLE(gcov, AC_HELP_STRING([--enable-gcov], - [Enable coverage testing using gcov]), - [use_gcov=$enableval],[use_gcov=no]) -if test "${use_gcov}" = "yes" ; then - USE_GCOV=1 -else - USE_GCOV=0 -fi -AC_SUBST(USE_GCOV) - - -######### -# Output the config header -AC_CONFIG_HEADERS(config.h) - -######### -# Generate the output files. -# -AC_SUBST(BUILD_CFLAGS) -AC_OUTPUT([ -Makefile -sqlcipher.pc -]) diff --git a/contrib/sqlitecon.tcl b/contrib/sqlitecon.tcl index b5dbcafc2a..78463a1ffa 100644 --- a/contrib/sqlitecon.tcl +++ b/contrib/sqlitecon.tcl @@ -567,7 +567,7 @@ proc sqlitecon::Cut w { } } -# Do a paste opeation. +# Do a paste operation. # proc sqlitecon::Paste w { if {[sqlitecon::canCut $w]==1} { diff --git a/doc/F2FS.txt b/doc/F2FS.txt new file mode 100644 index 0000000000..47ad2297f4 --- /dev/null +++ b/doc/F2FS.txt @@ -0,0 +1,87 @@ + +SQLite's OS layer contains the following definitions used in F2FS related +calls: + +#define F2FS_IOCTL_MAGIC 0xf5 +#define F2FS_IOC_START_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 1) +#define F2FS_IOC_COMMIT_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 2) +#define F2FS_IOC_START_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 3) +#define F2FS_IOC_ABORT_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 5) +#define F2FS_IOC_GET_FEATURES _IOR(F2FS_IOCTL_MAGIC, 12, u32) +#define F2FS_FEATURE_ATOMIC_WRITE 0x0004 + +After opening a database file on Linux (including Android), SQLite determines +whether or not a file supports F2FS atomic commits as follows: + + u32 flags = 0; + rc = ioctl(fd, F2FS_IOC_GET_FEATURES, &flags); + if( rc==0 && (flags & F2FS_FEATURE_ATOMIC_WRITE) ){ + /* File supports F2FS atomic commits */ + }else{ + /* File does NOT support F2FS atomic commits */ + } + +where "fd" is the file-descriptor open on the database file. + +Usually, when writing to a database file that supports atomic commits, SQLite +accumulates the entire transaction in heap memory, deferring all writes to the +db file until the transaction is committed. + +When it is time to commit a transaction on a file that supports atomic +commits, SQLite does: + + /* Take an F_WRLCK lock on the database file. This prevents any other + ** SQLite clients from reading or writing the file until the lock + ** is released. */ + rc = fcntl(fd, F_SETLK, ...); + if( rc!=0 ) goto failed; + + rc = ioctl(fd, F2FS_IOC_START_ATOMIC_WRITE); + if( rc!=0 ) goto fallback_to_legacy_journal_commit; + + foreach (dirty page){ + rc = write(fd, ...dirty page...); + if( rc!=0 ){ + ioctl(fd, F2FS_IOC_ABORT_VOLATILE_WRITE); + goto fallback_to_legacy_journal_commit; + } + } + + rc = ioctl(fd, F2FS_IOC_COMMIT_ATOMIC_WRITE); + if( rc!=0 ){ + ioctl(fd, F2FS_IOC_ABORT_VOLATILE_WRITE); + goto fallback_to_legacy_journal_commit; + } + + /* If we get there, the transaction has been successfully + ** committed to persistent storage. The following call + ** relinquishes the F_WRLCK lock. */ + fcntl(fd, F_SETLK, ...); + +Assumptions: + +1. After either of the F2FS_IOC_ABORT_VOLATILE_WRITE calls return, + the database file is in the state that it was in before + F2FS_IOC_START_ATOMIC_WRITE was invoked. Even if the ioctl() + fails - we're ignoring the return code. + + This is true regardless of the type of error that occurred in + ioctl() or write(). + +2. If the system fails before the F2FS_IOC_COMMIT_ATOMIC_WRITE is + completed, then following a reboot the database file is in the + state that it was in before F2FS_IOC_START_ATOMIC_WRITE was invoked. + Or, if the write was commited right before the system failed, in a + state indicating that all write() calls were successfully committed + to persistent storage before the failure occurred. + +3. If the process crashes before the F2FS_IOC_COMMIT_ATOMIC_WRITE is + completed then the file is automatically restored to the state that + it was in before F2FS_IOC_START_ATOMIC_WRITE was called. This occurs + before the posix advisory lock is automatically dropped - there is + no chance that another client will be able to read the file in a + half-committed state before the rollback operation occurs. + + + + diff --git a/doc/compile-for-unix.md b/doc/compile-for-unix.md new file mode 100644 index 0000000000..ce76b97bae --- /dev/null +++ b/doc/compile-for-unix.md @@ -0,0 +1,70 @@ +# Notes On Compiling SQLite On All Kinds Of Unix + +Here are step-by-step instructions on how to build SQLite from +canonical source on any modern machine that isn't Windows. These +notes are tested (on 2024-10-11) on Ubuntu and on MacOS, but they +are general and should work on most any modern unix platform. +See the companion document ([](./compile-for-windows.md>)) for +guidance on building for Windows. + + 1. Install a C-compiler. GCC or Clang both work fine. If you are + reading this document, you've probably already done that. + + 2. *(Optional):* Install TCL development libraries. In this note, + we'll do a private install in the $HOME/local directory, + but you can make adjustments to install TCL wherever you like. + This document assumes you are working with TCL version 9.0. + See also the [](./tcl-extension-testing.md) document that contains + more details on compiling Tcl for use with SQLite. +
    +
  1. Get the TCL source archive, perhaps from + + or . +
  2. Untar the source archive. CD into the "unix/" subfolder + of the source tree. +
  3. Run: `mkdir $HOME/local` +
  4. Run: `./configure --prefix=$HOME/local` +
  5. Run: `make install` +
+

+ As of 2024-10-25, TCL is not longer required for many + common build targets, such as "sqlite3.c" or the "sqlite3" + command-line tool. So you can skip this step if that is all + you want to build. TCL is still required to run "make test" + and similar, or to build the TCL extension, of course. + + 4. Download the SQLite source tree and unpack it. CD into the + toplevel directory of the source tree. + + 5. Run: `./configure --enable-all --with-tclsh=$HOME/local/bin/tclsh9.0` + + You do not need to use --with-tclsh if the tclsh you want to use is the + first one on your PATH or if you are building without TCL. + + 6. Run the "`Makefile`" makefile with an appropriate target. + Examples: +

    +
  • `make sqlite3.c` +
  • `make sqlite3` +
  • `make sqldiff` +
  • `make sqlite3_rsync` +
+

None of the targets above require TCL. TCL is needed + for the following targets: +

    +
  • `make tclextension-install` +
  • `make devtest` +
  • `make releasetest` +
  • `make sqlite3_analyzer` +
+ + It is not required that you run the "tclextension-install" target prior to + running tests. However, the tests will run more smoothly if you do. + The version of SQLite used for the TCL extension does *not* need to + correspond to the version of SQLite under test. So you can install the + SQLite TCL extension once, and then use it to test many different versions + of SQLite. + + + 7. For a debugging build of the CLI, where the ".treetrace" and ".wheretrace" + commands work, add the the --with-debug argument to configure. diff --git a/doc/compile-for-windows.md b/doc/compile-for-windows.md new file mode 100644 index 0000000000..0e59c83fed --- /dev/null +++ b/doc/compile-for-windows.md @@ -0,0 +1,182 @@ +# Notes On Compiling SQLite On Windows 11 + +Below are step-by-step instructions on how to build SQLite from +canonical source on a new Windows 11 PC, as of 2025-10-31. +See [](./compile-for-unix.md) for a similar guide for unix-like +systems, including MacOS. + + 1. Install Microsoft Visual Studio. The free "community edition" + will work fine. Do a standard install for C++ development. + SQLite only needs the + "cl" compiler and the "nmake" build tool. +
  • Note: + VS2015 or later is required for the procedures below to + all work. You *might* be able to get the build to work with + earlier versions of MSVC, but in that case the TCL installation + of step 3 will be required, since the "jimsh0.c" program of + Autosetup that is used as a substitute for "tclsh.exe" won't + compile with versions of Visual Studio prior to VS2015. In any + event, building SQLite from canonical source code on Windows + is not supported for earlier versions of Visual Studio.
+ + 2. Under the "Start" menu, find "All Apps" then go to "Visual Studio 20XX" + and find "x64 Native Tools Command Prompt for VS 20XX". Pin that + application to your task bar, as you will use it a lot. Bring up + an instance of this command prompt and do all of the subsequent steps + in that "x64 Native Tools" command prompt. (Or use "x86" if you want + a 32-bit build. Or use "ARM64" if you want to do a build for Windows + on ARM.) The subsequent steps will not work in a vanilla + DOS prompt. Nor will they work in PowerShell. + + 3. *(Optional):* Install TCL development libraries. + This note assumes that you will + install the TCL development libraries in the "`c:\Tcl`" directory. + Make adjustments + if you want TCL installed somewhere else. SQLite needs both the + "tclsh90.exe" command-line tool as part of the build process, and + the "tcl90.lib" and "tclstub.lib" libraries in order to run tests. + This document assumes you are working with TCL version 9.0. + See [](./tcl-extension-testing.md#windows) for guidance on how + to compile TCL version 8.6 for use with SQLite. +
    +
  1. Get the TCL source archive, perhaps from + + or . +
  2. Untar or unzip the source archive. CD into the "win/" subfolder + of the source tree. +
  3. Run: `nmake /f makefile.vc INSTALLDIR=c:\Tcl release` +
  4. Run: `nmake /f makefile.vc INSTALLDIR=c:\Tcl install`
    + Notes: +
      +
    1. The previous two `nmake` commands must be run separately. +
    2. Also, the INSTALLDIR=... argument is required on both. +
    +
  5. Optional: CD to `c:\Tcl\bin` and make a copy of + `tclsh90.exe` over into just `tclsh.exe`. +
  6. Optional: + Add `c:\Tcl\bin` to your %PATH%. To do this, go to Settings + and search for "path". Select "edit environment variables for + your account" and modify your default PATH accordingly. + You will need to close and reopen your command prompts after + making this change. +
+ + As of 2024-10-25, TCL is not longer required for many + common build targets, such as "sqlite3.c" or the "sqlite3.exe" + command-line tool. So you can skip this step if that is all + you want to build. TCL is still required to run "make test" + and similar, or to build the TCL extension, of course. + + 4. Download the SQLite source tree and unpack it. CD into the + toplevel directory of the source tree. + + 5. Run the "`Makefile.msc`" makefile with an appropriate target. + Examples: +
    +
  • `nmake /f makefile.msc` +
  • `nmake /f makefile.msc sqlite3.c` +
  • `nmake /f makefile.msc sqlite3.exe` +
  • `nmake /f makefile.msc sqldiff.exe` +
  • `nmake /f makefile.msc sqlite3_rsync.exe` +
+

No TCL is required for the nmake targets above. But for the ones + that follow, you will need a TCL installation, as described in step 3 + above. If you install TCL in some directory other than C:\\Tcl, then + you will also need to add the "TCLDIR=<dir>" option on the + nmake command line to tell nmake where your TCL is installed. +

    +
  • `nmake /f makefile.msc tclextension-install` +
  • `nmake /f makefile.msc devtest` +
  • `nmake /f makefile.msc releasetest` +
  • `nmake /f makefile.msc sqlite3_analyzer.exe` +
+ + It is not required that you run the "tclextension-install" target prior to + running tests. However, the tests will run more smoothly if you do. + The version of SQLite used for the TCL extension does *not* need to + correspond to the version of SQLite under test. So you can install the + SQLite TCL extension once, and then use it to test many different versions + of SQLite. + + + 7. For a debugging build of the CLI, where the ".treetrace" and ".wheretrace" + commands work, add the DEBUG=3 argument to nmake. Like this: +
    +
  • `nmake /f makefile.msc DEBUG=3 clean sqlite3.exe` +
+ + +## 32-bit Builds + +Doing a 32-bit build is just like doing a 64-bit build with the +following minor changes: + + 1. Use the "x86 Native Tools Command Prompt" instead of + "x64 Native Tools Command Prompt". "**x86**" instead of "**x64**". + + 2. Use a different installation directory for TCL. + The recommended directory is `c:\tcl32`. Thus you end up + with two TCL builds: +
    +
  • `c:\tcl` ← 64-bit (the default) +
  • `c:\tcl32` ← 32-bit +
+ + 3. Ensure that `c:\tcl32\bin` comes before `c:\tcl\bin` on + your PATH environment variable. You can achieve this using + a command like: +
    +
  • `set PATH=c:\tcl32\bin;%PATH%` +
+ +## Building a DLL + +The command the developers use for building the deliverable DLL on the +[download page](https://sqlite.org/download.html) is as follows: + +> nmake /f Makefile.msc sqlite3.dll USE_NATIVE_LIBPATHS=1 "OPTS=-DSQLITE_ENABLE_FTS3=1 -DSQLITE_ENABLE_FTS4=1 -DSQLITE_ENABLE_FTS5=1 -DSQLITE_ENABLE_RTREE=1 -DSQLITE_ENABLE_JSON1=1 -DSQLITE_ENABLE_GEOPOLY=1 -DSQLITE_ENABLE_SESSION=1 -DSQLITE_ENABLE_PREUPDATE_HOOK=1 -DSQLITE_ENABLE_SERIALIZE=1 -DSQLITE_ENABLE_MATH_FUNCTIONS=1" + +That command generates both the sqlite3.dll and sqlite3.def files. The same +command works for both 32-bit and 64-bit builds. + +## Statically Linking The TCL Library + +Some utility programs associated with SQLite need to be linked +with TCL in order to function. The [sqlite3_analyzer.exe program](https://sqlite.org/sqlanalyze.html) +is an example. You can build as described above, and then +enter: + +> nmake /f Makefile.msc sqlite3_analyzer.exe + +And you will end up with a working executable. However, that executable +will depend on having the "tcl98.dll" library somewhere on your %PATH%. +Use the following steps to build an executable that has the TCL library +statically linked so that it does not depend on separate DLL: + + 1. Use the appropriate "Command Prompt" window - either x86 or + x64, depending on whether you want a 32-bit or 64-bit executable. + + 2. Untar the TCL source tarball into a fresh directory. CD into + the "win/" subfolder. + + 3. Run: `nmake /f makefile.vc OPTS=static shell` + + 4. CD into the "Release*" subfolder that is created (note the + wildcard - the full name of the directory might vary). There + you will find the "tcl90s.lib" file. Copy this file into the + same directory that you put the "tcl90.lib" on your initial + installation. (In this document, that directory is + "`C:\Tcl32\lib`" for 32-bit builds and + "`C:\Tcl\lib`" for 64-bit builds.) + + 5. CD into your SQLite source code directory and build the desired + utility program, but add the following extra argument to the + nmake command line: +
STATICALLY_LINK_TCL=1
+

So, for example, to build a statically linked version of + sqlite3_analyzer.exe, you might type: +

nmake /f Makefile.msc STATICALLY_LINK_TCL=1 sqlite3_analyzer.exe
+ + 6. After your executable is built, you can verify that it does not + depend on the TCL DLL by running: +
dumpbin /dependents sqlite3_analyzer.exe
diff --git a/doc/json-enhancements.md b/doc/json-enhancements.md new file mode 100644 index 0000000000..bc03e8978c --- /dev/null +++ b/doc/json-enhancements.md @@ -0,0 +1,144 @@ +# JSON Functions Enhancements (2022) + +This document summaries enhancements to the SQLite JSON support added in +early 2022. + +## 1.0 Change summary: + + 1. New **->** and **->>** operators that work like MySQL and PostgreSQL (PG). + 2. JSON functions are built-in rather than being an extension. They + are included by default, but can be omitted using the + -DSQLITE_OMIT_JSON compile-time option. + + +## 2.0 New operators **->** and **->>** + +The SQLite language adds two new binary operators **->** and **->>**. +Both operators are similar to json_extract(). The left operand is +JSON and the right operand is a JSON path expression (possibly abbreviated +for compatibility with PG - see below). So they are similar to a +two-argument call to json_extract(). + +The difference between -> and ->> (and json_extract()) is as follows: + + * The -> operator always returns JSON. + + * The ->> operator converts the answer into a primitive SQL datatype + such as TEXT, INTEGER, REAL, or NULL. If a JSON object or array + is selected, that object or array is rendered as text. If a JSON + value is selected, that value is converted into its corresponding + SQL type + + * The json_extract() interface returns JSON when a JSON object or + array is selected, or a primitive SQL datatype when a JSON value + is selected. This is different from MySQL, in which json_extract() + always returns JSON, but the difference is retained because it has + worked that way for 6 years and changing it now would likely break + a lot of legacy code. + +In MySQL and PG, the ->> operator always returns TEXT (or NULL) and never +INTEGER or REAL. This is due to limitations in the type handling capabilities +of those systems. In MySQL and PG, the result type a function or operator +may only depend on the type of its arguments, never the value of its arguments. +But the underlying JSON type depends on the value of the JSON path +expression, not the type of the JSON path expression (which is always TEXT). +Hence, the result type of ->> in MySQL and PG is unable to vary according +to the type of the JSON value being extracted. + +The type system in SQLite is more general. Functions in SQLite are able +to return different datatypes depending on the value of their arguments. +So the ->> operator in SQLite is able to return TEXT, INTEGER, REAL, or NULL +depending on the JSON type of the value being extracted. This means that +the behavior of the ->> is slightly different in SQLite versus MySQL and PG +in that it will sometimes return INTEGER and REAL values, depending on its +inputs. It is possible to implement the ->> operator in SQLite so that it +always operates exactly like MySQL and PG and always returns TEXT or NULL, +but I have been unable to think of any situations where returning the +actual JSON value this would cause problems, so I'm including the enhanced +functionality in SQLite. + +The table below attempts to summarize the differences between the +-> and ->> operators and the json_extract() function, for SQLite, MySQL, +and PG. JSON values are shown using their SQL text representation but +in a bold font. + + + +
JSONPATH-> operator
(all)
->> operator
(MySQL/PG) +
->> operator
(SQLite)
json_extract()
(SQLite) +
**'{"a":123}'** '$.a' **'123'** '123' 123 123 +
**'{"a":4.5}'** '$.a' **'4.5'** '4.5' 4.5 4.5 +
**'{"a":"xyz"}'** '$.a' **'"xyz"'** 'xyz' 'xyz' 'xyz' +
**'{"a":null}'** '$.a' **'null'** NULL NULL NULL +
**'{"a":[6,7,8]}'** '$.a' **'[6,7,8]'** '[6,7,8]' '[6,7,8]' **'[6,7,8]'** +
**'{"a":{"x":9}}'** '$.a' **'{"x":9}'** '{"x":9}' '{"x":9}' **'{"x":9}'** +
**'{"b":999}'** '$.a' NULL NULL NULL NULL +
+ +Important points about the table above: + + * The -> operator always returns either JSON or NULL. + + * The ->> operator never returns JSON. It always returns TEXT or NULL, or in the + case of SQLite, INTEGER or REAL. + + * The MySQL json_extract() function works exactly the same + as the MySQL -> operator. + + * The SQLite json_extract() operator works like -> for JSON objects and + arrays, and like ->> for JSON values. + + * The -> operator works the same for all systems. + + * The only difference in ->> between SQLite and other systems is that + when the JSON value is numeric, SQLite returns a numeric SQL value, + whereas the other systems return a text representation of the numeric + value. + +### 2.1 Abbreviated JSON path expressions for PG compatibility + +The table above always shows the full JSON path expression: '$.a'. But +PG does not accept this syntax. PG only allows a single JSON object label +name or a single integer array index. In order to provide compatibility +with PG, The -> and ->> operators in SQLite are extended to also support +a JSON object label or an integer array index for the right-hand side +operand, in addition to a full JSON path expression. + +Thus, a -> or ->> operator that works on MySQL will work in +SQLite. And a -> or ->> operator that works in PG will work in SQLite. +But because SQLite supports the union of the disjoint capabilities of +MySQL and PG, there will always be -> and ->> operators that work in +SQLite that do not work in one of MySQL and PG. This is an unavoidable +consequence of the different syntax for -> and ->> in MySQL and PG. + +In the following table, assume that "value1" is a JSON object and +"value2" is a JSON array. + + +
SQL expression Works in MySQL?Works in PG?Works in SQLite +
value1->'$.a' yes no yes +
value1->'a' no yes yes +
value2->'$[2]' yes no yes +
value2->2 no yes yes +
+ +The abbreviated JSON path expressions only work for the -> and ->> operators +in SQLite. The json_extract() function, and all other built-in SQLite +JSON functions, continue to require complete JSON path expressions for their +PATH arguments. + +## 3.0 JSON moved into the core + +The JSON interface is now moved into the SQLite core. + +When originally written in 2015, the JSON functions were an extension +that could be optionally included at compile-time, or loaded at run-time. +The implementation was in a source file named ext/misc/json1.c in the +source tree. JSON functions were only compiled in if the +-DSQLITE_ENABLE_JSON1 compile-time option was used. + +After these enhancements, the JSON functions are now built-ins. +The source file that implements the JSON functions is moved to src/json.c. +No special compile-time options are needed to load JSON into the build. +Instead, there is a new -DSQLITE_OMIT_JSON compile-time option to leave +them out. diff --git a/doc/jsonb.md b/doc/jsonb.md new file mode 100644 index 0000000000..63ce77b170 --- /dev/null +++ b/doc/jsonb.md @@ -0,0 +1,289 @@ +# The JSONB Format + +This document describes SQLite's JSONB binary encoding of +JSON. + +## 1.0 What Is JSONB? + +Beginning with version 3.45.0 (circa 2024-01-01), SQLite supports an +alternative binary encoding of JSON which we call "JSONB". JSONB is +a binary format that stored as a BLOB. + +The advantage of JSONB over ordinary text RFC 8259 JSON is that JSONB +is both slightly smaller (by between 5% and 10% in most cases) and +can be processed in less than half the number of CPU cycles. The built-in +[JSON SQL functions] of SQLite can accept either ordinary text JSON +or the binary JSONB encoding for any of their JSON inputs. + +The "JSONB" name is inspired by [PostgreSQL](https://postgresql.org), but the +on-disk format for SQLite's JSONB is not the same as PostgreSQL's. +The two formats have the same name, but they have wildly different internal +representations and are not in any way binary compatible. + +The central idea behind this JSONB specification is that each element +begins with a header that includes the size and type of that element. +The header takes the place of punctuation such as double-quotes, +curly-brackes, square-brackets, commas, and colons. Since the size +and type of each element is contained in its header, the element can +be read faster since it is no longer necessary to carefully scan forward +looking for the closing delimiter. The payload of JSONB is the same +as for corresponding text JSON. The same payload bytes occur in the +same order. The only real difference between JSONB and ordinary text +JSON is that JSONB includes a binary header on +each element and omits delimiter and separator punctuation. + +### 1.1 Internal Use Only + +The details of the JSONB are not intended to be visible to application +developers. Application developers should look at JSONB as an opaque BLOB +used internally by SQLite. Nevertheless, we want the format to be backwards +compatible across all future versions of SQLite. To that end, the format +is documented by this file in the source tree. But this file should be +used only by SQLite core developers, not by developers of applications +that only use SQLite. + +## 2.0 The Purpose Of This Document + +JSONB is not intended as an external format to be used by +applications. JSONB is designed for internal use by SQLite only. +Programmers do not need to understand the JSONB format in order to +use it effectively. +Applications should access JSONB only through the [JSON SQL functions], +not by looking at individual bytes of the BLOB. + +However, JSONB is intended to be portable and backwards compatible +for all future versions of SQLite. In other words, you should not have +to export and reimport your SQLite database files when you upgrade to +a newer SQLite version. For that reason, the JSONB format needs to +be well-defined. + +This document is therefore similar in purpose to the +[SQLite database file format] document that describes the on-disk +format of an SQLite database file. Applications are not expected +to directly read and write the bits and bytes of SQLite database files. +The SQLite database file format is carefully documented so that it +can be stable and enduring. In the same way, the JSONB representation +of JSON is documented here so that it too can be stable and enduring, +not so that applications can read or writes individual bytes. + +## 3.0 Encoding + +JSONB is a direct translation of the underlying text JSON. The difference +is that JSONB uses a binary encoding that is faster to parse compared to +the detailed syntax of text JSON. + +Each JSON element is encoded as a header and a payload. The header +determines type of element (string, numeric, boolean, null, object, or +array) and the size of the payload. The header can be between 1 and +9 bytes in size. The payload can be any size from zero bytes up to the +maximum allowed BLOB size. + +### 3.1 Payload Size + +The upper four bits of the first byte of the header determine size of the +header and possibly also the size of the payload. +If the upper four bits have a value between 0 and 11, then the header is +exactly one byte in size and the payload size is determined by those +upper four bits. If the upper four bits have a value between 12 and 15, +that means that the total header size is 2, 3, 5, or 9 bytes and the +payload size is unsigned big-endian integer that is contained in the +subsequent bytes. The size integer is the one byte that following the +initial header byte if the upper four bits +are 12, two bytes if the upper bits are 13, four bytes if the upper bits +are 14, and eight bytes if the upper bits are 15. The current design +of SQLite does not support BLOB values larger than 2GiB, so the eight-byte +variant of the payload size integer will never be used by the current code. +The eight-byte payload size integer is included in the specification +to allow for future expansion. + +The header for an element does *not* need to be in its simplest +form. For example, consider the JSON numeric value "`1`". +That element can be encode in five different ways: + + * `0x13 0x31` + * `0xc3 0x01 0x31` + * `0xd3 0x00 0x01 0x31` + * `0xe3 0x00 0x00 0x00 0x01 0x31` + * `0xf3 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x01 0x31` + +The shortest encoding is preferred, of course, and usually happens with +primitive elements such as numbers. However the total size of an array +or object might not be known exactly when the header of the element is +first generated. It is convenient to reserve space for the largest +possible header and then go back and fill in the correct payload size +at the end. This technique can result in array or object headers that +are larger than absolutely necessary. + +### 3.2 Element Type + +The least-significant four bits of the first byte of the header (the first +byte masked against 0x0f) determine element type. The following codes are +used: + +
    +
  1. NULL → +The element is a JSON "null". The payload size for a true JSON NULL must +must be zero. Future versions of SQLite might extend the JSONB format +with elements that have a zero element type but a non-zero size. In that +way, legacy versions of SQLite will interpret the element as a NULL +for backwards compatibility while newer versions will interpret the +element in some other way. + +

  2. TRUE → +The element is a JSON "true". The payload size must be zero for a actual +"true" value. Elements with type 1 and a non-zero payload size are +reserved for future expansion. Legacy implementations that see an element +type of 1 with a non-zero payload size should continue to interpret that +element as "true" for compatibility. + +

  3. FALSE → +The element is a JSON "false". The payload size must be zero for a actual +"false" value. Elements with type 2 and a non-zero payload size are +reserved for future expansion. Legacy implementations that see an element +type of 2 with a non-zero payload size should continue to interpret that +element as "false" for compatibility. + +

  4. INT → +The element is a JSON integer value in the canonical +RFC 8259 format, without extensions. The payload is the ASCII +text representation of that numeric value. + +

  5. INT5 → +The element is a JSON integer literal in hexadecimal notation. +The payload is the ASCII text representation of the literal. +Because the payload is in a non-standard format, it will need +to be translated when the JSONB is converted into RFC 8259 text JSON. + +

  6. FLOAT → +The element is a JSON floating-point value in the canonical +RFC 8259 format, without extensions. The payload is the ASCII +text representation of that numeric value. + +

  7. FLOAT5 → +The element is a JSON floating-point value that is not in the +canonical JSON format but rather the extended JSON5 format. +The payload is the ASCII text representation of that numeric value. +Because the payload is in a non-standard format, it will need to +be translated when the JSONB is converted into RFC 8259 text JSON. + +

  8. TEXT → +The element is a JSON string value that does not contain +any escapes nor any characters that need to be escaped for either SQL or +JSON. The payload is the UTF8 text representation of the string value. +The payload does not include string delimiters. + +

  9. TEXTJ → +The element is a JSON string value that contains +RFC 8259 character escapes (such as "\n" or "\u0020"). +Those escapes will need to be translated into actual UTF8 if this element +is [json_extract|extracted] into SQL. +The payload is the UTF8 text representation of the escaped string value. +The payload does not include string delimiters. + +

  10. TEXT5 → +The element is a JSON string value that contains +character escapes, including some character escapes that part of JSON5 +and which are not found in the canonical RFC 8259 spec. +Those escapes will need to be translated into standard JSON prior to +rendering the JSON as text, or into their actual UTF8 characters if this +element is [json_extract|extracted] into SQL. +The payload is the UTF8 text representation of the escaped string value. +The payload does not include string delimiters. + +

  11. TEXTRAW → +The element is a JSON string value that contains +UTF8 characters that need to be escaped if this string is rendered into +standard JSON text. +The payload does not include string delimiters. + +

  12. ARRAY → +The element is a JSON array. The payload contains +JSONB elements that comprise values contained within the array. + +

  13. OBJECT → +The element is a JSON object. The payload contains +pairs of JSONB elements that comprise entries for the JSON object. +The first element in each pair must be a string (types 7 through 10). +The second element of each pair may be any types, including nested +arrays or objects. + +

  14. RESERVED-13 → +Reserved for future expansion. Legacy implements that encounter this +element type should raise an error. + +

  15. RESERVED-14 → +Reserved for future expansion. Legacy implements that encounter this +element type should raise an error. + +

  16. RESERVED-15 → +Reserved for future expansion. Legacy implements that encounter this +element type should raise an error. +

+ +Element types outside the range of 0 to 12 are reserved for future +expansion. The current implement raises an error if see an element type +other than those listed above. However, future versions of SQLite might +use of the three remaining element types to implement indexing or similar +optimizations, to speed up lookup against large JSON arrays and/or objects. + +### 3.3 Design Rationale For Element Types + +A key goal of JSONB is that it should be quick to translate +to and from text JSON and/or be constructed from SQL values. +When converting from text into JSONB, we do not want the +converter subroutine to burn CPU cycles converting elements +values into some standard format which might never be used. +Format conversion is "lazy" - it is deferred until actually +needed. This has implications for the JSONB format design: + + 1. Numeric values are stored as text, not a numbers. The values are + a direct copy of the text JSON values from which they are derived. + + 2. There are multiple element types depending on the details of value + formats. For example, INT is used for pure RFC-8259 integer + literals and INT5 exists for JSON5 extensions such as hexadecimal + notation. FLOAT is used for pure RFC-8259 floating point literals + and FLOAT5 is used for JSON5 extensions. There are four different + representations of strings, depending on where the string came from + and how special characters within the string are escaped. + +A second goal of JSONB is that it should be capable of serving as the +"parse tree" for JSON when a JSON value is being processed by the +various [JSON SQL functions] built into SQLite. Before JSONB was +developed, operations such [json_replace()] and [json_patch()] +and similar worked in three stages: + + + 1. Translate the text JSON into a internal format that is + easier to scan and edit. + 2. Perform the requested operation on the JSON. + 3. Translate the internal format back into text. + +JSONB seeks to serve as the internal format directly - bypassing +the first and third stages of that process. Since most of the CPU +cycles are spent on the first and third stages, that suggests that +JSONB processing will be much faster than text JSON processing. + +So when processing JSONB, only the second stage of the three-stage +process is required. But when processing text JSON, it is still necessary +to do stages one and three. If JSONB is to be used as the internal +binary representation, this is yet another reason to store numeric +values as text. Storing numbers as text minimizes the amount of +conversion work needed for stages one and three. This is also why +there are four different representations of text in JSONB. Different +text representations are used for text coming from different sources +(RFC-8259 JSON, JSON5, or SQL string values) and conversions only +happen if and when they are actually needed. + +### 3.4 Valid JSONB BLOBs + +A valid JSONB BLOB consists of a single JSON element. The element must +exactly fill the BLOB. This one element is often a JSON object or array +and those usually contain additional elements as its payload, but the +element can be a primitive value such a string, number, boolean, or null. + +When the built-in JSON functions are attempting to determine if a BLOB +argument is a JSONB or just a random BLOB, they look at the header of +the outer element to see that it is well-formed and that the element +completely fills the BLOB. If these conditions are met, then the BLOB +is accepted as a JSONB value. diff --git a/doc/lemon.html b/doc/lemon.html index b16e35960e..965f305c04 100644 --- a/doc/lemon.html +++ b/doc/lemon.html @@ -2,110 +2,175 @@ The Lemon Parser Generator - -

The Lemon Parser Generator

+ + +

The Lemon Parser Generator

-

Lemon is an LALR(1) parser generator for C or C++. -It does the same job as ``bison'' and ``yacc''. -But lemon is not another bison or yacc clone. It +

Lemon is an LALR(1) parser generator for C. +It does the same job as "bison" and "yacc". +But Lemon is not a bison or yacc clone. Lemon uses a different grammar syntax which is designed to -reduce the number of coding errors. Lemon also uses a more -sophisticated parsing engine that is faster than yacc and -bison and which is both reentrant and thread-safe. -Furthermore, Lemon implements features that can be used -to eliminate resource leaks, making is suitable for use +reduce the number of coding errors. Lemon also uses a +parsing engine that is faster than yacc and +bison and which is both reentrant and threadsafe. +(Update: Since the previous sentence was written, bison +has also been updated so that it too can generate a +reentrant and threadsafe parser.) +Lemon also implements features that can be used +to eliminate resource leaks, making it suitable for use in long-running programs such as graphical user interfaces or embedded controllers.

This document is an introduction to the Lemon parser generator.

-

Theory of Operation

+ +

1.0 Table of Contents

+ + + +

2.0 Security Note

+ +

The language parser code created by Lemon is very robust and +is well-suited for use in internet-facing applications that need to +safely process maliciously crafted inputs.

-

The main goal of Lemon is to translate a context free grammar (CFG) +

The "lemon.exe" command-line tool itself works great when given a valid +input grammar file and almost always gives helpful +error messages for malformed inputs. However, it is possible for +a malicious user to craft a grammar file that will cause +lemon.exe to crash. +We do not see this as a problem, as lemon.exe is not intended to be used +with hostile inputs. +To summarize:

+ +
    +
  • Parser code generated by lemon → Robust and secure +
  • The "lemon.exe" command line tool itself → Not so much +
+ + +

3.0 Theory of Operation

+ +

Lemon is computer program that translates a context free grammar (CFG) for a particular language into C code that implements a parser for that language. -The program has two inputs: +The Lemon program has two inputs:

  • The grammar specification.
  • A parser template file.
-Typically, only the grammar specification is supplied by the programmer. -Lemon comes with a default parser template which works fine for most -applications. But the user is free to substitute a different parser -template if desired.

- -

Depending on command-line options, Lemon will generate between -one and three files of outputs. +

Typically, only the grammar specification is supplied by the programmer. +Lemon comes with a default parser template +("lempar.c") +that works fine for most applications. But the user is free to substitute +a different parser template if desired.

+ +

Depending on command-line options, Lemon will generate up to +three output files.

    -
  • C code to implement the parser. -
  • A header file defining an integer ID for each terminal symbol. +
  • C code to implement a parser for the input grammar. +
  • A header file defining an integer ID for each terminal symbol + (or "token").
  • An information file that describes the states of the generated parser automaton.
-By default, all three of these output files are generated. -The header file is suppressed if the ``-m'' command-line option is -used and the report file is omitted when ``-q'' is selected.

+

By default, all three of these output files are generated. +The header file is suppressed if the "-m" command-line option is +used and the report file is omitted when "-q" is selected.

-

The grammar specification file uses a ``.y'' suffix, by convention. +

The grammar specification file uses a ".y" suffix, by convention. In the examples used in this document, we'll assume the name of the -grammar file is ``gram.y''. A typical use of Lemon would be the -following command: +grammar file is "gram.y". A typical use of Lemon would be the +following command:

    lemon gram.y
 
-This command will generate three output files named ``gram.c'', -``gram.h'' and ``gram.out''. +

This command will generate three output files named "gram.c", +"gram.h" and "gram.out". The first is C code to implement the parser. The second is the header file that defines numerical values for all terminal symbols, and the last is the report that explains the states used by the parser automaton.

-

Command Line Options

+ +

3.1 Command Line Options

The behavior of Lemon can be modified using command-line options. You can obtain a list of the available command-line options together -with a brief explanation of what each does by typing +with a brief explanation of what each does by typing

-   lemon -?
+   lemon "-?"
 
-As of this writing, the following command-line options are supported: +

As of this writing, the following command-line options are supported:

    -
  • -b -
  • -c -
  • -g -
  • -m -
  • -q -
  • -s -
  • -x +
  • -b +Show only the basis for each parser state in the report file. +
  • -c +Do not compress the generated action tables. The parser will be a +little larger and slower, but it will detect syntax errors sooner. +
  • -ddirectory +Write all output files into directory. Normally, output files +are written into the directory that contains the input grammar file. +
  • -Dname +Define C preprocessor macro name. This macro is usable by +"%ifdef", +"%ifndef", and +"%if lines +in the grammar file. +
  • -E +Run the "%if" preprocessor step only and print the revised grammar +file. +
  • -g +Do not generate a parser. Instead write the input grammar to standard +output with all comments, actions, and other extraneous text removed. +
  • -l +Omit "#line" directives in the generated parser C code. +
  • -m +Cause the output C source code to be compatible with the "makeheaders" +program. +
  • -p +Display all conflicts that are resolved by +precedence rules. +
  • -q +Suppress generation of the report file. +
  • -r +Do not sort or renumber the parser states as part of optimization. +
  • -s +Show parser statistics before exiting. +
  • -Tfile +Use file as the template for the generated C-code parser implementation. +
  • -x +Print the Lemon version number.
-The ``-b'' option reduces the amount of text in the report file by -printing only the basis of each parser state, rather than the full -configuration. -The ``-c'' option suppresses action table compression. Using -c -will make the parser a little larger and slower but it will detect -syntax errors sooner. -The ``-g'' option causes no output files to be generated at all. -Instead, the input grammar file is printed on standard output but -with all comments, actions and other extraneous text deleted. This -is a useful way to get a quick summary of a grammar. -The ``-m'' option causes the output C source file to be compatible -with the ``makeheaders'' program. -Makeheaders is a program that automatically generates header files -from C source code. When the ``-m'' option is used, the header -file is not output since the makeheaders program will take care -of generated all header files automatically. -The ``-q'' option suppresses the report file. -Using ``-s'' causes a brief summary of parser statistics to be -printed. Like this: -
-   Parser statistics: 74 terminals, 70 nonterminals, 179 rules
-                      340 states, 2026 parser table entries, 0 conflicts
-
-Finally, the ``-x'' option causes Lemon to print its version number -and then stops without attempting to read the grammar or generate a parser.

- -

The Parser Interface

+ + +

3.2 The Parser Interface

Lemon doesn't generate a complete, working program. It only generates a few subroutines that implement a parser. This section describes @@ -115,94 +180,95 @@

The Parser Interface

Before a program begins using a Lemon-generated parser, the program must first create the parser. -A new parser is created as follows: +A new parser is created as follows:

    void *pParser = ParseAlloc( malloc );
 
-The ParseAlloc() routine allocates and initializes a new parser and +

The ParseAlloc() routine allocates and initializes a new parser and returns a pointer to it. -The actual data structure used to represent a parser is opaque -- +The actual data structure used to represent a parser is opaque — its internal structure is not visible or usable by the calling routine. For this reason, the ParseAlloc() routine returns a pointer to void rather than a pointer to some particular structure. The sole argument to the ParseAlloc() routine is a pointer to the -subroutine used to allocate memory. Typically this means ``malloc()''.

+subroutine used to allocate memory. Typically this means malloc().

After a program is finished using a parser, it can reclaim all -memory allocated by that parser by calling +memory allocated by that parser by calling

    ParseFree(pParser, free);
 
-The first argument is the same pointer returned by ParseAlloc(). The +

The first argument is the same pointer returned by ParseAlloc(). The second argument is a pointer to the function used to release bulk memory back to the system.

After a parser has been allocated using ParseAlloc(), the programmer must supply the parser with a sequence of tokens (terminal symbols) to be parsed. This is accomplished by calling the following function -once for each token: +once for each token:

    Parse(pParser, hTokenID, sTokenData, pArg);
 
-The first argument to the Parse() routine is the pointer returned by +

The first argument to the Parse() routine is the pointer returned by ParseAlloc(). -The second argument is a small positive integer that tells the parse the +The second argument is a small positive integer that tells the parser the type of the next token in the data stream. There is one token type for each terminal symbol in the grammar. The gram.h file generated by Lemon contains #define statements that map symbolic terminal symbol names into appropriate integer values. -(A value of 0 for the second argument is a special flag to the -parser to indicate that the end of input has been reached.) +A value of 0 for the second argument is a special flag to the +parser to indicate that the end of input has been reached. The third argument is the value of the given token. By default, -the type of the third argument is integer, but the grammar will +the type of the third argument is "void*", but the grammar will usually redefine this type to be some kind of structure. Typically the second argument will be a broad category of tokens -such as ``identifier'' or ``number'' and the third argument will +such as "identifier" or "number" and the third argument will be the name of the identifier or the value of the number.

The Parse() function may have either three or four arguments, -depending on the grammar. If the grammar specification file request -it, the Parse() function will have a fourth parameter that can be +depending on the grammar. If the grammar specification file requests +it (via the %extra_argument directive), +the Parse() function will have a fourth parameter that can be of any type chosen by the programmer. The parser doesn't do anything with this argument except to pass it through to action routines. This is a convenient mechanism for passing state information down to the action routines without having to use global variables.

A typical use of a Lemon parser might look something like the -following: -

-   01 ParseTree *ParseFile(const char *zFilename){
-   02    Tokenizer *pTokenizer;
-   03    void *pParser;
-   04    Token sToken;
-   05    int hTokenId;
-   06    ParserState sState;
-   07
-   08    pTokenizer = TokenizerCreate(zFilename);
-   09    pParser = ParseAlloc( malloc );
-   10    InitParserState(&sState);
-   11    while( GetNextToken(pTokenizer, &hTokenId, &sToken) ){
-   12       Parse(pParser, hTokenId, sToken, &sState);
+following:

+
+    1 ParseTree *ParseFile(const char *zFilename){
+    2    Tokenizer *pTokenizer;
+    3    void *pParser;
+    4    Token sToken;
+    5    int hTokenId;
+    6    ParserState sState;
+    7
+    8    pTokenizer = TokenizerCreate(zFilename);
+    9    pParser = ParseAlloc( malloc );
+   10    InitParserState(&sState);
+   11    while( GetNextToken(pTokenizer, &hTokenId, &sToken) ){
+   12       Parse(pParser, hTokenId, sToken, &sState);
    13    }
-   14    Parse(pParser, 0, sToken, &sState);
+   14    Parse(pParser, 0, sToken, &sState);
    15    ParseFree(pParser, free );
    16    TokenizerFree(pTokenizer);
    17    return sState.treeRoot;
    18 }
 
-This example shows a user-written routine that parses a file of +

This example shows a user-written routine that parses a file of text and returns a pointer to the parse tree. -(We've omitted all error-handling from this example to keep it +(All error-handling code is omitted from this example to keep it simple.) We assume the existence of some kind of tokenizer which is created using TokenizerCreate() on line 8 and deleted by TokenizerFree() on line 16. The GetNextToken() function on line 11 retrieves the -next token from the input file and puts its type in the +next token from the input file and puts its type in the integer variable hTokenId. The sToken variable is assumed to be some kind of structure that contains details about each token, -such as its complete text, what line it occurs on, etc.

+such as its complete text, what line it occurs on, etc.

-

This example also assumes the existence of structure of type +

This example also assumes the existence of a structure of type ParserState that holds state information about a particular parse. An instance of such a structure is created on line 6 and initialized on line 10. A pointer to this structure is passed into the Parse() @@ -213,18 +279,18 @@

The Parser Interface

the ParserState structure is left pointing to the root of the parse tree.

-

The core of this example as it relates to Lemon is as follows: +

The core of this example as it relates to Lemon is as follows:

    ParseFile(){
       pParser = ParseAlloc( malloc );
-      while( GetNextToken(pTokenizer,&hTokenId, &sToken) ){
+      while( GetNextToken(pTokenizer,&hTokenId, &sToken) ){
          Parse(pParser, hTokenId, sToken);
       }
       Parse(pParser, 0, sToken);
       ParseFree(pParser, free );
    }
 
-Basically, what a program has to do to use a Lemon-generated parser +

Basically, what a program has to do to use a Lemon-generated parser is first create the parser, then send it lots of tokens obtained by tokenizing an input source. When the end of input is reached, the Parse() routine should be called one last time with a token type @@ -235,21 +301,75 @@

The Parser Interface

There is one other interface routine that should be mentioned before we move on. The ParseTrace() function can be used to generate debugging output -from the parser. A prototype for this routine is as follows: +from the parser. A prototype for this routine is as follows:

    ParseTrace(FILE *stream, char *zPrefix);
 
-After this routine is called, a short (one-line) message is written +

After this routine is called, a short (one-line) message is written to the designated output stream every time the parser changes states or calls an action routine. Each such message is prefaced using the text given by zPrefix. This debugging output can be turned off by calling ParseTrace() again with a first argument of NULL (0).

-

Differences With YACC and BISON

+ +

3.2.1 Allocating The Parse Object On Stack

+ +

If all calls to the Parse() interface are made from within +%code directives, then the parse +object can be allocated from the stack rather than from the heap. +These are the steps: + +

    +
  • Declare a local variable of type "yyParser" +
  • Initialize the variable using ParseInit() +
  • Pass a pointer to the variable in calls to Parse() +
  • Deallocate substructure in the parse variable using ParseFinalize(). +
+ +

The following code illustrates how this is done: + +

+   ParseFile(){
+      yyParser x;
+      ParseInit( &x );
+      while( GetNextToken(pTokenizer,&hTokenId, &sToken) ){
+         Parse(&x, hTokenId, sToken);
+      }
+      Parse(&x, 0, sToken);
+      ParseFinalize( &x );
+   }
+
+ + +

3.2.2 Interface Summary

+ +

Here is a quick overview of the C-language interface to a +Lemon-generated parser:

+ +
+void *ParseAlloc( (void*(*malloc)(size_t) );
+void ParseFree(void *pParser, (void(*free)(void*) );
+void Parse(void *pParser, int tokenCode, ParseTOKENTYPE token, ...);
+void ParseTrace(FILE *stream, char *zPrefix);
+
+ +

Notes:

+ + + +

3.3 Differences With YACC and BISON

Programmers who have previously used the yacc or bison parser generator will notice several important differences between yacc and/or -bison and Lemon. +bison and Lemon.

  • In yacc and bison, the parser calls the tokenizer. In Lemon, the tokenizer calls the parser. @@ -258,12 +378,47 @@

    Differences With YACC and BISON

  • Lemon allows multiple parsers to be running simultaneously. Yacc and bison do not.
-These differences may cause some initial confusion for programmers +

These differences may cause some initial confusion for programmers with prior yacc and bison experience. But after years of experience using Lemon, I firmly believe that the Lemon way of doing things is better.

-

Input File Syntax

+

Updated as of 2016-02-16: +The text above was written in the 1990s. +We are told that Bison has lately been enhanced to support the +tokenizer-calls-parser paradigm used by Lemon, eliminating the +need for global variables.

+ + +

3.4 Building The "lemon" or "lemon.exe" Executable

+ +

The "lemon" or "lemon.exe" program is built from a single file +of C-code named +"lemon.c". +The Lemon source code is generic C89 code that uses +no unusual or non-standard libraries. Any +reasonable C compiler should suffice to compile the lemon program. +A command-line like the following will usually work:

+ +
+cc -o lemon lemon.c
+
On Windows machines with Visual C++ installed, bring up a +"VS20NN x64 Native Tools Command Prompt" window and enter: + +
+cl lemon.c
+
+ +

Compiling Lemon really is that simple. +Additional compiler options such as +"-O2" or "-g" or "-Wall" can be added if desired, but they are not +necessary.

+ + + +

4.0 Input File Syntax

The main purpose of the grammar specification file for Lemon is to define the grammar for the parser. But the input file also @@ -271,25 +426,26 @@

Input File Syntax

Most of the work in using Lemon is in writing an appropriate grammar file.

-

The grammar file for lemon is, for the most part, free format. +

The grammar file for Lemon is, for the most part, a free format. It does not have sections or divisions like yacc or bison. Any -declaration can occur at any point in the file. -Lemon ignores whitespace (except where it is needed to separate -tokens) and it honors the same commenting conventions as C and C++.

+declaration can occur at any point in the file. Lemon ignores +whitespace (except where it is needed to separate tokens), and it +honors the same commenting conventions as C and C++.

-

Terminals and Nonterminals

+ +

4.1 Terminals and Nonterminals

A terminal symbol (token) is any string of alphanumeric -and underscore characters -that begins with an upper case letter. +and/or underscore characters +that begins with an uppercase letter. A terminal can contain lowercase letters after the first character, -but the usual convention is to make terminals all upper case. +but the usual convention is to make terminals all uppercase. A nonterminal, on the other hand, is any string of alphanumeric -and underscore characters than begins with a lower case letter. -Again, the usual convention is to make nonterminals use all lower -case letters.

+and underscore characters than begins with a lowercase letter. +Again, the usual convention is to make nonterminals use all lowercase +letters.

-

In Lemon, terminal and nonterminal symbols do not need to +

In Lemon, terminal and nonterminal symbols do not need to be declared or identified in a separate section of the grammar file. Lemon is able to generate a list of all terminals and nonterminals by examining the grammar rules, and it can always distinguish a @@ -302,30 +458,31 @@

Terminals and Nonterminals

terminal symbols. With Lemon, all symbols, terminals and nonterminals, must have alphanumeric names.

-

Grammar Rules

+ +

4.2 Grammar Rules

The main component of a Lemon grammar file is a sequence of grammar rules. Each grammar rule consists of a nonterminal symbol followed by -the special symbol ``::='' and then a list of terminals and/or nonterminals. +the special symbol "::=" and then a list of terminals and/or nonterminals. The rule is terminated by a period. The list of terminals and nonterminals on the right-hand side of the rule can be empty. Rules can occur in any order, except that the left-hand side of the first rule is assumed to be the start symbol for the grammar (unless -specified otherwise using the %start directive described below.) -A typical sequence of grammar rules might look something like this: +specified otherwise using the %start_symbol +directive described below.) +A typical sequence of grammar rules might look something like this:

   expr ::= expr PLUS expr.
   expr ::= expr TIMES expr.
   expr ::= LPAREN expr RPAREN.
   expr ::= VALUE.
 
-

-

There is one non-terminal in this example, ``expr'', and five -terminal symbols or tokens: ``PLUS'', ``TIMES'', ``LPAREN'', -``RPAREN'' and ``VALUE''.

+

There is one non-terminal in this example, "expr", and five +terminal symbols or tokens: "PLUS", "TIMES", "LPAREN", +"RPAREN" and "VALUE".

Like yacc and bison, Lemon allows the grammar to specify a block of C code that will be executed whenever a grammar rule is reduced @@ -333,39 +490,38 @@

Grammar Rules

In Lemon, this action is specified by putting the C code (contained within curly braces {...}) immediately after the period that closes the rule. -For example: +For example:

   expr ::= expr PLUS expr.   { printf("Doing an addition...\n"); }
 
-

In order to be useful, grammar actions must normally be linked to their associated grammar rules. -In yacc and bison, this is accomplished by embedding a ``$$'' in the +In yacc and bison, this is accomplished by embedding a "$$" in the action to stand for the value of the left-hand side of the rule and -symbols ``$1'', ``$2'', and so forth to stand for the value of +symbols "$1", "$2", and so forth to stand for the value of the terminal or nonterminal at position 1, 2 and so forth on the right-hand side of the rule. This idea is very powerful, but it is also very error-prone. The single most common source of errors in a yacc or bison grammar is to miscount the number of symbols on the right-hand side of a grammar -rule and say ``$7'' when you really mean ``$8''.

+rule and say "$7" when you really mean "$8".

Lemon avoids the need to count grammar symbols by assigning symbolic names to each symbol in a grammar rule and then using those symbolic names in the action. -In yacc or bison, one would write this: +In yacc or bison, one would write this:

-  expr -> expr PLUS expr  { $$ = $1 + $3; };
+  expr -> expr PLUS expr  { $$ = $1 + $3; };
 
-But in Lemon, the same rule becomes the following: +

But in Lemon, the same rule becomes the following:

   expr(A) ::= expr(B) PLUS expr(C).  { A = B+C; }
 
-In the Lemon rule, any symbol in parentheses after a grammar rule +

In the Lemon rule, any symbol in parentheses after a grammar rule symbol becomes a place holder for that symbol in the grammar rule. This place holder can then be used in the associated C action to -stand for the value of that symbol.

+stand for the value of that symbol.

The Lemon notation for linking a grammar rule with its reduce action is superior to yacc/bison on several counts. @@ -375,11 +531,11 @@

Grammar Rules

includes a linking symbol in parentheses but that linking symbol is not actually used in the reduce action, then an error message is generated. -For example, the rule +For example, the rule

   expr(A) ::= expr(B) PLUS expr(C).  { A = B; }
 
-will generate an error because the linking symbol ``C'' is used +

will generate an error because the linking symbol "C" is used in the grammar rule but not in the reduce action.

The Lemon notation for linking grammar rules to reduce actions @@ -387,7 +543,8 @@

Grammar Rules

allocated by the values of terminals and nonterminals on the right-hand side of a rule.

-

Precedence Rules

+ +

4.3 Precedence Rules

Lemon resolves parsing ambiguities in exactly the same way as yacc and bison. A shift-reduce conflict is resolved in favor @@ -395,70 +552,73 @@

Precedence Rules

whichever rule comes first in the grammar file.

Just like in -yacc and bison, Lemon allows a measure of control -over the resolution of paring conflicts using precedence rules. +yacc and bison, Lemon allows a measure of control +over the resolution of parsing conflicts using precedence rules. A precedence value can be assigned to any terminal symbol -using the %left, %right or %nonassoc directives. Terminal symbols -mentioned in earlier directives have a lower precedence that +using the +%left, +%right or +%nonassoc directives. Terminal symbols +mentioned in earlier directives have a lower precedence than terminal symbols mentioned in later directives. For example:

-

+
    %left AND.
    %left OR.
    %nonassoc EQ NE GT GE LT LE.
    %left PLUS MINUS.
    %left TIMES DIVIDE MOD.
    %right EXP NOT.
-

+

In the preceding sequence of directives, the AND operator is defined to have the lowest precedence. The OR operator is one precedence level higher. And so forth. Hence, the grammar would -attempt to group the ambiguous expression +attempt to group the ambiguous expression

      a AND b OR c
 
-like this +

like this

      a AND (b OR c).
 
-The associativity (left, right or nonassoc) is used to determine +

The associativity (left, right or nonassoc) is used to determine the grouping when the precedence is the same. AND is left-associative -in our example, so +in our example, so

      a AND b AND c
 
-is parsed like this +

is parsed like this

      (a AND b) AND c.
 
-The EXP operator is right-associative, though, so +

The EXP operator is right-associative, though, so

      a EXP b EXP c
 
-is parsed like this +

is parsed like this

      a EXP (b EXP c).
 
-The nonassoc precedence is used for non-associative operators. -So +

The nonassoc precedence is used for non-associative operators. +So

      a EQ b EQ c
 
-is an error.

+

is an error.

The precedence of non-terminals is transferred to rules as follows: The precedence of a grammar rule is equal to the precedence of the left-most terminal symbol in the rule for which a precedence is defined. This is normally what you want, but in those cases where -you want to precedence of a grammar rule to be something different, +you want the precedence of a grammar rule to be something different, you can specify an alternative precedence symbol by putting the symbol in square braces after the period at the end of the rule and before any C-code. For example:

-

+
    expr = MINUS expr.  [NOT]
-

+

This rule has a precedence equal to that of the NOT symbol, not the MINUS symbol as would have been the case by default.

@@ -467,7 +627,7 @@

Precedence Rules

symbols and individual grammar rules, we can now explain precisely how parsing conflicts are resolved in Lemon. Shift-reduce conflicts are resolved -as follows: +as follows:

  • If either the token to be shifted or the rule to be reduced lacks precedence information, then resolve in favor of the @@ -475,354 +635,518 @@

    Precedence Rules

  • If the precedence of the token to be shifted is greater than the precedence of the rule to reduce, then resolve in favor of the shift. No parsing conflict is reported. -
  • If the precedence of the token it be shifted is less than the +
  • If the precedence of the token to be shifted is less than the precedence of the rule to reduce, then resolve in favor of the reduce action. No parsing conflict is reported.
  • If the precedences are the same and the shift token is right-associative, then resolve in favor of the shift. No parsing conflict is reported. -
  • If the precedences are the same the shift token is +
  • If the precedences are the same and the shift token is left-associative, then resolve in favor of the reduce. No parsing conflict is reported. -
  • Otherwise, resolve the conflict by doing the shift and - report the parsing conflict. +
  • Otherwise, resolve the conflict by doing the shift, and + report a parsing conflict.
-Reduce-reduce conflicts are resolved this way: +

Reduce-reduce conflicts are resolved this way:

    -
  • If either reduce rule +
  • If either reduce rule lacks precedence information, then resolve in favor of the - rule that appears first in the grammar and report a parsing + rule that appears first in the grammar, and report a parsing conflict. -
  • If both rules have precedence and the precedence is different +
  • If both rules have precedence and the precedence is different, then resolve the dispute in favor of the rule with the highest - precedence and do not report a conflict. + precedence, and do not report a conflict.
  • Otherwise, resolve the conflict by reducing by the rule that - appears first in the grammar and report a parsing conflict. + appears first in the grammar, and report a parsing conflict.
-

Special Directives

+ +

4.4 Special Directives

The input grammar to Lemon consists of grammar rules and special directives. We've described all the grammar rules, so now we'll talk about the special directives.

-

Directives in lemon can occur in any order. You can put them before -the grammar rules, or after the grammar rules, or in the mist of the +

Directives in Lemon can occur in any order. You can put them before +the grammar rules, or after the grammar rules, or in the midst of the grammar rules. It doesn't matter. The relative order of directives used to assign precedence to terminals is important, but other than that, the order of directives in Lemon is arbitrary.

-

Lemon supports the following special directives: +

Lemon supports the following special directives:

-Each of these directives will be described separately in the +

Each of these directives will be described separately in the following sections:

-

The %code directive

+ +

4.4.1 The %code directive

-

The %code directive is used to specify addition C/C++ code that +

The %code directive is used to specify additional C code that is added to the end of the main output file. This is similar to -the %include directive except that %include is inserted at the -beginning of the main output file.

+the %include directive except that +%include is inserted at the beginning of the main output file.

+ +

%code is typically used to include some action routines or perhaps +a tokenizer or even the "main()" function +as part of the output file.

-

%code is typically used to include some action routines or perhaps -a tokenizer as part of the output file.

+

There can be multiple %code directives. The arguments of +all %code directives are concatenated.

-

The %default_destructor directive

+ +

4.4.2 The %default_destructor directive

-

The %default_destructor directive specifies a destructor to +

The %default_destructor directive specifies a destructor to use for non-terminals that do not have their own destructor -specified by a separate %destructor directive. See the documentation -on the %destructor directive below for additional information.

+specified by a separate %destructor directive. See the documentation +on the %destructor directive below for +additional information.

-

In some grammers, many different non-terminal symbols have the -same datatype and hence the same destructor. This directive is -a convenience way to specify the same destructor for all those +

In some grammars, many different non-terminal symbols have the +same data type and hence the same destructor. This directive is +a convenient way to specify the same destructor for all those non-terminals using a single statement.

-

The %default_type directive

+ +

4.4.3 The %default_type directive

-

The %default_type directive specifies the datatype of non-terminal -symbols that do no have their own datatype defined using a separate -%type directive. See the documentation on %type below for addition -information.

+

The %default_type directive specifies the data type of non-terminal +symbols that do not have their own data type defined using a separate +%type directive.

-

The %destructor directive

+ +

4.4.4 The %destructor directive

-

The %destructor directive is used to specify a destructor for +

The %destructor directive is used to specify a destructor for a non-terminal symbol. -(See also the %token_destructor directive which is used to -specify a destructor for terminal symbols.)

+(See also the %token_destructor +directive which is used to specify a destructor for terminal symbols.)

A non-terminal's destructor is called to dispose of the non-terminal's value whenever the non-terminal is popped from -the stack. This includes all of the following circumstances: +the stack. This includes all of the following circumstances:

  • When a rule reduces and the value of a non-terminal on the right-hand side is not linked to C code.
  • When the stack is popped during error processing.
  • When the ParseFree() function runs.
-The destructor can do whatever it wants with the value of +

The destructor can do whatever it wants with the value of the non-terminal, but its design is to deallocate memory or other resources held by that non-terminal.

-

Consider an example: +

Consider an example:

    %type nt {void*}
    %destructor nt { free($$); }
    nt(A) ::= ID NUM.   { A = malloc( 100 ); }
 
-This example is a bit contrived but it serves to illustrate how +

This example is a bit contrived, but it serves to illustrate how destructors work. The example shows a non-terminal named -``nt'' that holds values of type ``void*''. When the rule for -an ``nt'' reduces, it sets the value of the non-terminal to +"nt" that holds values of type "void*". When the rule for +an "nt" reduces, it sets the value of the non-terminal to space obtained from malloc(). Later, when the nt non-terminal is popped from the stack, the destructor will fire and call free() on this malloced space, thus avoiding a memory leak. -(Note that the symbol ``$$'' in the destructor code is replaced +(Note that the symbol "$$" in the destructor code is replaced by the value of the non-terminal.)

It is important to note that the value of a non-terminal is passed to the destructor whenever the non-terminal is removed from the stack, unless the non-terminal is used in a C-code action. If the non-terminal is used by C-code, then it is assumed that the -C-code will take care of destroying it if it should really -be destroyed. More commonly, the value is used to build some -larger structure and we don't want to destroy it, which is why +C-code will take care of destroying it. +More commonly, the value is used to build some +larger structure, and we don't want to destroy it, which is why the destructor is not called in this circumstance.

-

By appropriate use of destructors, it is possible to -build a parser using Lemon that can be used within a long-running -program, such as a GUI, that will not leak memory or other resources. +

Destructors help avoid memory leaks by automatically freeing +allocated objects when they go out of scope. To do the same using yacc or bison is much more difficult.

-

The %extra_argument directive

+ +

4.4.5 The %extra_argument directive

-The %extra_argument directive instructs Lemon to add a 4th parameter +

The %extra_argument directive instructs Lemon to add a 4th parameter to the parameter list of the Parse() function it generates. Lemon doesn't do anything itself with this extra argument, but it does make the argument available to C-code action routines, destructors, and so forth. For example, if the grammar file contains:

-

+
     %extra_argument { MyStruct *pAbc }
-

+

Then the Parse() function generated will have an 4th parameter -of type ``MyStruct*'' and all action routines will have access to -a variable named ``pAbc'' that is the value of the 4th parameter +of type "MyStruct*" and all action routines will have access to +a variable named "pAbc" that is the value of the 4th parameter in the most recent call to Parse().

-

The %include directive

+

The %extra_context directive works the same except that it +is passed in on the ParseAlloc() or ParseInit() routines instead of +on Parse().

+ + +

4.4.6 The %extra_context directive

+ +

The %extra_context directive instructs Lemon to add a 2nd parameter +to the parameter list of the ParseAlloc() and ParseInit() functions. Lemon +doesn't do anything itself with these extra argument, but it does +store the value make it available to C-code action routines, destructors, +and so forth. For example, if the grammar file contains:

-

The %include directive specifies C code that is included at the -top of the generated parser. You can include any text you want -- +

+    %extra_context { MyStruct *pAbc }
+
+ +

Then the ParseAlloc() and ParseInit() functions will have an 2nd parameter +of type "MyStruct*" and all action routines will have access to +a variable named "pAbc" that is the value of that 2nd parameter.

+ +

The %extra_argument directive works the same except that it +is passed in on the Parse() routine instead of on ParseAlloc()/ParseInit().

+ + +

4.4.7 The %fallback directive

+ +

The %fallback directive specifies an alternative meaning for one +or more tokens. The alternative meaning is tried if the original token +would have generated a syntax error.

+ +

The %fallback directive was added to support robust parsing of SQL +syntax in SQLite. +The SQL language contains a large assortment of keywords, each of which +appears as a different token to the language parser. SQL contains so +many keywords that it can be difficult for programmers to keep up with +them all. Programmers will, therefore, sometimes mistakenly use an +obscure language keyword for an identifier. The %fallback directive +provides a mechanism to tell the parser: "If you are unable to parse +this keyword, try treating it as an identifier instead."

+ +

The syntax of %fallback is as follows:

+ +
+%fallback ID TOKEN... . +

+ +

In words, the %fallback directive is followed by a list of token +names terminated by a period. +The first token name is the fallback token — the +token to which all the other tokens fall back to. The second and subsequent +arguments are tokens which fall back to the token identified by the first +argument.

+ + +

4.4.8 The %if directive and its friends

+ +

The %if, %ifdef, %ifndef, %else, +and %endif directives +are similar to #if, #ifdef, #ifndef, #else, and #endif in the C-preprocessor, +just not as general. +Each of these directives must begin at the left margin. No whitespace +is allowed between the "%" and the directive name.

+ +

Grammar text in between "%ifdef MACRO" and the next nested +"%endif" is +ignored unless the "-DMACRO" command-line option is used. Grammar text +between "%ifndef MACRO" and the next nested "%endif" is +included except when the "-DMACRO" command-line option is used.

+ +

The text in between "%if CONDITIONAL" and its +corresponding %endif is included only if CONDITIONAL +is true. The CONDITION is one or more macro names, optionally connected +using the "||" and "&&" binary operators, the "!" unary operator, +and grouped using balanced parentheses. Each term is true if the +corresponding macro exists, and false if it does not exist.

+ +

An optional "%else" directive can occur anywhere in between a +%ifdef, %ifndef, or %if directive and +its corresponding %endif.

+ +

Note that the argument to %ifdef and %ifndef is +intended to be a single preprocessor symbol name, not a general expression. +Use the "%if" directive for general expressions.

+ + +

4.4.9 The %include directive

+ +

The %include directive specifies C code that is included at the +top of the generated parser. You can include any text you want — the Lemon parser generator copies it blindly. If you have multiple -%include directives in your grammar file the value of the last -%include directive overwrites all the others.%include directives in your grammar file, their values are concatenated +so that all %include code ultimately appears near the top of the +generated parser, in the same order as it appeared in the grammar.

-

The %include directive is very handy for getting some extra #include +

The %include directive is very handy for getting some extra #include preprocessor statements at the beginning of the generated parser. For example:

-

+
    %include {#include <unistd.h>}
-

+

This might be needed, for example, if some of the C actions in the -grammar call functions that are prototyed in unistd.h.

+grammar call functions that are prototyped in unistd.h.

+ +

Use the %code directive to add code to +the end of the generated parser.

-

The %left directive

+ +

4.4.10 The %left directive

-The %left directive is used (along with the %right and -%nonassoc directives) to declare precedences of terminal -symbols. Every terminal symbol whose name appears after -a %left directive but before the next period (``.'') is +The %left directive is used (along with the +%right and +%nonassoc directives) to declare +precedences of terminal symbols. +Every terminal symbol whose name appears after +a %left directive but before the next period (".") is given the same left-associative precedence value. Subsequent -%left directives have higher precedence. For example:

+%left directives have higher precedence. For example:

-

+
    %left AND.
    %left OR.
    %nonassoc EQ NE GT GE LT LE.
    %left PLUS MINUS.
    %left TIMES DIVIDE MOD.
    %right EXP NOT.
-

+
-

Note the period that terminates each %left, %right or %nonassoc +

Note the period that terminates each %left, +%right or %nonassoc directive.

LALR(1) grammars can get into a situation where they require a large amount of stack space if you make heavy use or right-associative -operators. For this reason, it is recommended that you use %left -rather than %right whenever possible.

+operators. For this reason, it is recommended that you use %left +rather than %right whenever possible.

-

The %name directive

+ +

4.4.11 The %name directive

By default, the functions generated by Lemon all begin with the -five-character string ``Parse''. You can change this string to something -different using the %name directive. For instance:

+five-character string "Parse". You can change this string to something +different using the %name directive. For instance:

-

+
    %name Abcde
-

+

Putting this directive in the grammar file will cause Lemon to generate -functions named +functions named

  • AbcdeAlloc(),
  • AbcdeFree(),
  • AbcdeTrace(), and
  • Abcde().
-The %name directive allows you to generator two or more different -parsers and link them all into the same executable. -

+

The %name directive allows you to generate two or more different +parsers and link them all into the same executable.

-

The %nonassoc directive

+ +

4.4.12 The %nonassoc directive

This directive is used to assign non-associative precedence to -one or more terminal symbols. See the section on precedence rules -or on the %left directive for additional information.

+one or more terminal symbols. See the section on +precedence rules +or on the %left directive +for additional information.

-

The %parse_accept directive

+ +

4.4.13 The %parse_accept directive

-

The %parse_accept directive specifies a block of C code that is -executed whenever the parser accepts its input string. To ``accept'' +

The %parse_accept directive specifies a block of C code that is +executed whenever the parser accepts its input string. To "accept" an input string means that the parser was able to process all tokens without error.

For example:

-

+
    %parse_accept {
       printf("parsing complete!\n");
    }
-

- +
-

The %parse_failure directive

+ +

4.4.14 The %parse_failure directive

-

The %parse_failure directive specifies a block of C code that +

The %parse_failure directive specifies a block of C code that is executed whenever the parser fails complete. This code is not executed until the parser has tried and failed to resolve an input error using is usual error recovery strategy. The routine is only invoked when parsing is unable to continue.

-

+
    %parse_failure {
      fprintf(stderr,"Giving up.  Parser is hopelessly lost...\n");
    }
-

+
-

The %right directive

+ +

4.4.15 The %right directive

This directive is used to assign right-associative precedence to -one or more terminal symbols. See the section on precedence rules -or on the %left directive for additional information.

+one or more terminal symbols. See the section on +precedence rules +or on the %left directive for additional information.

-

The %stack_overflow directive

+ +

4.4.16 The %stack_overflow directive

-

The %stack_overflow directive specifies a block of C code that +

The %stack_overflow directive specifies a block of C code that is executed if the parser's internal stack ever overflows. Typically this just prints an error message. After a stack overflow, the parser will be unable to continue and must be reset.

-

+
    %stack_overflow {
      fprintf(stderr,"Giving up.  Parser stack overflow\n");
    }
-

+

You can help prevent parser stack overflows by avoiding the use of right recursion and right-precedence operators in your grammar. -Use left recursion and and left-precedence operators instead, to +Use left recursion and and left-precedence operators instead to encourage rules to reduce sooner and keep the stack size down. -For example, do rules like this: +For example, do rules like this:

    list ::= list element.      // left-recursion.  Good!
    list ::= .
 
-Not like this: +

Not like this:

    list ::= element list.      // right-recursion.  Bad!
    list ::= .
 
-

The %stack_size directive

+ +

4.4.17 The %stack_size directive

If stack overflow is a problem and you can't resolve the trouble by using left-recursion, then you might want to increase the size of the parser's stack using this directive. Put an positive integer -after the %stack_size directive and Lemon will generate a parse +after the %stack_size directive and Lemon will generate a parse with a stack of the requested size. The default value is 100.

-

+
    %stack_size 2000
-

+
-

The %start_symbol directive

+ +

4.4.18 The %start_symbol directive

-

By default, the start-symbol for the grammar that Lemon generates +

By default, the start symbol for the grammar that Lemon generates is the first non-terminal that appears in the grammar file. But you -can choose a different start-symbol using the %start_symbol directive.

+can choose a different start symbol using the +%start_symbol directive.

-

+
    %start_symbol  prog
-

+
+ + +

4.4.19 The %syntax_error directive

+ +

See Error Processing.

+ + +

4.4.20 The %token directive

+ +

Tokens are normally created automatically, the first time they are used. +Any identifier that begins with an upper-case letter is a token. + +

Sometimes it is useful to declare tokens in advance, however. The +integer values assigned to each token determined by the order in which +the tokens are seen. So by declaring tokens in advance, it is possible to +cause some tokens to have low-numbered values, which might be desirable in +some grammers, or to have sequential values assigned to a sequence of +related tokens. For this reason, the %token directive is provided to +declare tokens in advance. The syntax is as follows: + +

+%token TOKEN TOKEN... . +

+ +

The %token directive is followed by zero or more token symbols and +terminated by a single ".". Each token named is created if it does not +already exist. Tokens are created in order. -

The %token_destructor directive

-

The %destructor directive assigns a destructor to a non-terminal -symbol. (See the description of the %destructor directive above.) -This directive does the same thing for all terminal symbols.

+ +

4.4.21 The %token_class directive

-

Unlike non-terminal symbols which may each have a different data type +

Undocumented. Appears to be related to the MULTITERMINAL concept. +Implementation.

+ + +

4.4.22 The %token_destructor directive

+ +

The %destructor directive assigns a destructor to a non-terminal +symbol. (See the description of the +%destructor directive above.) +The %token_destructor directive does the same thing +for all terminal symbols.

+ +

Unlike non-terminal symbols, which may each have a different data type for their values, terminals all use the same data type (defined by -the %token_type directive) and so they use a common destructor. Other -than that, the token destructor works just like the non-terminal +the %token_type directive) +and so they use a common destructor. +Other than that, the token destructor works just like the non-terminal destructors.

-

The %token_prefix directive

+ +

4.4.23 The %token_prefix directive

Lemon generates #defines that assign small integer constants to each terminal symbol in the grammar. If desired, Lemon will add a prefix specified by this directive -to each of the #defines it generates. -So if the default output of Lemon looked like this: +to each of the #defines it generates.

+ +

So if the default output of Lemon looked like this:

     #define AND              1
     #define MINUS            2
     #define OR               3
     #define PLUS             4
 
-You can insert a statement into the grammar like this: +

You can insert a statement into the grammar like this:

     %token_prefix    TOKEN_
 
-to cause Lemon to produce these symbols instead: +

to cause Lemon to produce these symbols instead:

     #define TOKEN_AND        1
     #define TOKEN_MINUS      2
@@ -830,31 +1154,32 @@ 

The %token_prefix directive

#define TOKEN_PLUS 4
-

The %token_type and %type directives

+ +

4.4.24 The %token_type and %type directives

These directives are used to specify the data types for values on the parser's stack associated with terminal and non-terminal symbols. The values of all terminal symbols must be of the same type. This turns out to be the same data type as the 3rd parameter to the Parse() function generated by Lemon. Typically, you will -make the value of a terminal symbol by a pointer to some kind of +make the value of a terminal symbol be a pointer to some kind of token structure. Like this:

-

+
    %token_type    {Token*}
-

+

If the data type of terminals is not specified, the default value -is ``int''.

+is "void*".

Non-terminal symbols can each have their own data types. Typically -the data type of a non-terminal is a pointer to the root of a parse-tree +the data type of a non-terminal is a pointer to the root of a parse tree structure that contains all information about that non-terminal. For example:

-

+
    %type   expr  {Expr*}
-

+

Each entry on the parser's stack is actually a union containing instances of all data types for every non-terminal and terminal symbol. @@ -866,27 +1191,92 @@

The %token_type and %type directives

entry parser stack will require 100K of heap space. If you are willing and able to pay that price, fine. You just need to know.

-

Error Processing

+ +

4.4.25 The %wildcard directive

+ +

The %wildcard directive is followed by a single token name and a +period. This directive specifies that the identified token should +match any input token.

+ +

When the generated parser has the choice of matching an input against +the wildcard token and some other token, the other token is always used. +The wildcard token is only matched if there are no alternatives.

+ + +

4.4.26 The %realloc and %free directives

+ +

The %realloc and %free directives defines function +that allocate and free heap memory. The signatures of these functions +should be the same as the realloc() and free() functions from the standard +C library. + +

If both of these functions are defined +then these functions are used to allocate and free +memory for supplemental parser stack space, if the initial +parse stack space is exceeded. The initial parser stack size +is specified by either %stack_size or the +-DYYSTACKDEPTH compile-time flag. + + +

5.0 Error Processing

After extensive experimentation over several years, it has been discovered that the error recovery strategy used by yacc is about as good as it gets. And so that is what Lemon uses.

When a Lemon-generated parser encounters a syntax error, it -first invokes the code specified by the %syntax_error directive, if +first invokes the code specified by the %syntax_error directive, if any. It then enters its error recovery strategy. The error recovery strategy is to begin popping the parsers stack until it enters a state where it is permitted to shift a special non-terminal symbol -named ``error''. It then shifts this non-terminal and continues -parsing. But the %syntax_error routine will not be called again +named "error". It then shifts this non-terminal and continues +parsing. The %syntax_error routine will not be called again until at least three new tokens have been successfully shifted.

If the parser pops its stack until the stack is empty, and it still -is unable to shift the error symbol, then the %parse_failed routine +is unable to shift the error symbol, then the +%parse_failure routine is invoked and the parser resets itself to its start state, ready to begin parsing a new file. This is what will happen at the very -first syntax error, of course, if there are no instances of the -``error'' non-terminal in your grammar.

+first syntax error, of course, if there are no instances of the +"error" non-terminal in your grammar.

+ + + +

6.0 History of Lemon

+ +

Lemon was originally written by Richard Hipp sometime in the late +1980s on a Sun4 Workstation using K&R C. +There was a companion LL(1) parser generator program named "Lime". +The Lime source code has been lost.

+ +

The lemon.c source file was originally many separate files that were +compiled together to generate the "lemon" executable. Sometime in the +1990s, the individual source code files were combined together into +the current single large "lemon.c" source file. You can still see traces +of original filenames in the code.

+ +

Since 2001, Lemon has been part of the +SQLite project and the source code +to Lemon has been managed as a part of the +SQLite source tree in the following +files:

+ + + + +

7.0 Copyright

+ +

All of the source code to Lemon, including the template parser file +"lempar.c" and this documentation file ("lemon.html") are in the public +domain. You can use the code for any purpose and without attribution.

+ +

The code comes with no warranty. If it breaks, you get to keep both +pieces.

diff --git a/doc/pager-invariants.txt b/doc/pager-invariants.txt index 44444dad54..0fea0a698d 100644 --- a/doc/pager-invariants.txt +++ b/doc/pager-invariants.txt @@ -45,7 +45,7 @@ *** Definition: Two databases (or the same database at two points it time) are said to be "logically equivalent" if they give the same answer to all queries. Note in particular the content of freelist leaf - pages can be changed arbitarily without effecting the logical equivalence + pages can be changed arbitrarily without effecting the logical equivalence of the database. (7) At any time, if any subset, including the empty set and the total set, diff --git a/doc/tcl-extension-testing.md b/doc/tcl-extension-testing.md new file mode 100644 index 0000000000..eb2a8c3a3b --- /dev/null +++ b/doc/tcl-extension-testing.md @@ -0,0 +1,264 @@ +# Test Procedures For The SQLite TCL Extension + +## 1.0 Background + +The SQLite TCL extension logic (in the +"[tclsqlite.c](/file/src/tclsqlite.c)" source +file) is statically linked into "textfixture" executable +which is the program used to do most of the testing +associated with "make test", "make devtest", and/or +"make releasetest". So the functionality of the SQLite +TCL extension is thoroughly vetted during normal testing. The +procedures below are designed to test the loadable extension +aspect of the SQLite TCL extension, and in particular to verify +that the "make tclextension-install" build target works and that +an ordinary tclsh can subsequently run "package require sqlite3". + +This procedure can also be used as a template for how to set up +a local TCL+SQLite development environment. In other words, it +can be be used as a guide on how to compile per-user copies of +Tcl that are used to develop, test, and debug SQLite. In that +case, perhaps make minor changes to the procedure such as: + + * Make TCLBUILD directory is permanent. + * Enable debugging symbols on the Tcl library build. + * Reduce the optimization level to -O0 for easier debugging. + * Also compile "wish" to go with each "tclsh". + + + +## 2.0 Testing On Unix-like Systems (Including Mac) + +See also the [](./compile-for-unix.md) document which provides another +perspective on how to compile SQLite on unix-like systems. + +### 2.1 Setup + +
    +
  1. + [Fossil][] installed. +
  2. Check out source code and set environment variables: +
      +
    1. **TCLSOURCE** → + The top-level directory of a [Fossil][] check-out of the + [TCL source tree][tcl-fossil]. +
    2. **SQLITESOURCE** → + A Fossil check-out of the SQLite source tree. +
    3. **TCLHOME** → + A directory that does not exist at the start of the test and which + will be deleted at the end of the test, and that will contain the + test builds of the TCL libraries and the SQLite TCL Extensions. + It is the top-most installation directory, i.e. the one provided + to Tcl's `./configure --prefix=/path/to/tcl`. +
    4. **TCLVERSION** → + The `X.Y`-form version of Tcl being used: 8.6, 9.0, 9.1... +
    +
+ +### 2.2 Testing TCL 8.x and 9.x on unix + +From a checked-out copy of [the core Tcl tree][tcl-fossil] + +
    +
  1. `TCLVERSION=8.6`
    + ↑ A version of your choice. This process has been tested with + values of 8.6, 9.0, and 9.1 (as of 2025-04-16). The out-of-life + version 8.5 fails some of `make devtest` for undetermined reasons. +
  2. `TCLHOME=$HOME/tcl/$TCLVERSION` +
  3. `TCLSOURCE=/path/to/tcl/checkout` +
  4. `SQLITESOURCE=/path/to/sqlite/checkout` +
  5. `rm -fr $TCLHOME`
    + ↑ Ensure that no stale Tcl installation is laying around. +
  6. `cd $TCLSOURCE` +
  7. `fossil up core-8-6-branch`
    + ↑ The branch corresponding to `$TCLVERSION`, e.g. + `core-9-0-branch` or `trunk`. +
  8. `fossil clean -x` +
  9. `cd unix` +
  10. `./configure --prefix=$TCLHOME --disable-shared`
    + ↑ The `--disable-shared` is to avoid the need to set `LD_LIBRARY_PATH` + when using this Tcl build. +
  11. `make install` +
  12. `cd $SQLITESOURCE` +
  13. `fossil clean -x` +
  14. `./configure --with-tcl=$TCLHOME --all` +
  15. `make tclextension-install`
    + ↑ Verify extension installed at + `$TCLHOME/lib/tcl${TCLVERSION}/sqlite`. +
  16. `make tclextension-list`
    + ↑ Verify TCL extension correctly installed. +
  17. `make tclextension-verify`
    + ↑ Verify that the correct version is installed. +
  18. `$TCLHOME/bin/tclsh[89].[0-9] test/testrunner.tcl release --explain`
    + ↑ Verify thousands of lines of output with no errors. Or + consider running "devtest" without --explain instead of "release". +
+ +### 2.3 Cleanup + +
    +
  1. `rm -rf $TCLHOME` +
+ + +## 3.0 Testing On Windows + +See also the [](./compile-for-windows.md) document which provides another +perspective on how to compile SQLite on Windows. + +### 3.1 Setup for Windows + +(These docs are not as up-to-date as the Unix docs, above.) + +
    +
  1. + [Fossil][] installed. +
  2. + Unix-like command-line tools installed. Example: + [unxutils](https://unxutils.sourceforge.net/) +
  3. [Visual Studio](https://visualstudio.microsoft.com/vs/community/) + installed. VS2015 or later required. +
  4. Check out source code and set environment variables. +
      +
    1. **TCLSOURCE** → + The top-level directory of a Fossil check-out of the TCL source tree. +
    2. **SQLITESOURCE** → + A Fossil check-out of the SQLite source tree. +
    3. **TCLBUILD** → + A directory that does not exist at the start of the test and which + will be deleted at the end of the test, and that will contain the + test builds of the TCL libraries and the SQLite TCL Extensions. +
    4. **ORIGINALPATH** → + The original value of %PATH%. In other words, set as follows: + `set ORIGINALPATH %PATH%` +
    +
+ +### 3.2 Testing TCL 8.6 on Windows + +
    +
  1. `mkdir %TCLBUILD%\tcl86` +
  2. `cd %TCLSOURCE%\win` +
  3. `fossil up core-8-6-16`
    + ↑ Or some other version of Tcl8.6. +
  4. `fossil clean -x` +
  5. `set INSTALLDIR=%TCLBUILD%\tcl86` +
  6. `nmake /f makefile.vc release`
    + ⇅ You *must* invoke the "release" and "install" targets + using separate "nmake" commands or tclsh86t.exe won't be + installed. +
  7. `nmake /f makefile.vc install` +
  8. `cd %SQLITESOURCE%` +
  9. `fossil clean -x` +
  10. `set TCLDIR=%TCLBUILD%\tcl86` +
  11. `set PATH=%TCLBUILD%\tcl86\bin;%ORIGINALPATH%` +
  12. `set TCLSH_CMD=%TCLBUILD%\tcl86\bin\tclsh86t.exe` +
  13. `nmake /f Makefile.msc tclextension-install`
    + ↑ Verify extension installed at %TCLBUILD%\\tcl86\\lib\\tcl8.6\\sqlite3.* +
  14. `nmake /f Makefile.msc tclextension-verify` +
  15. `tclsh86t test/testrunner.tcl release --explain`
    + ↑ Verify thousands of lines of output with no errors. Or + consider running "devtest" without --explain instead of "release". +
+ +### 3.3 Testing TCL 9.0 on Windows + +
    +
  1. `mkdir %TCLBUILD%\tcl90` +
  2. `cd %TCLSOURCE%\win` +
  3. `fossil up core-9-0-0`
    + ↑ Or some other version of Tcl9 +
  4. `fossil clean -x` +
  5. `set INSTALLDIR=%TCLBUILD%\tcl90` +
  6. `nmake /f makefile.vc release`
    + ⇅ You *must* invoke the "release" and "install" targets + using separate "nmake" commands or tclsh90.exe won't be + installed. +
  7. `nmake /f makefile.vc install` +
  8. `cd %SQLITESOURCE%` +
  9. `fossil clean -x` +
  10. `set TCLDIR=%TCLBUILD%\tcl90` +
  11. `set PATH=%TCLBUILD%\tcl90\bin;%ORIGINALPATH%` +
  12. `set TCLSH_CMD=%TCLBUILD%\tcl90\bin\tclsh90.exe` +
  13. `nmake /f Makefile.msc tclextension-install`
    + ↑ Verify extension installed at %TCLBUILD%\\tcl90\\lib\\sqlite3.* +
  14. `nmake /f Makefile.msc tclextension-verify` +
  15. `tclsh90 test/testrunner.tcl release --explain`
    + ↑ Verify thousands of lines of output with no errors. Or + consider running "devtest" without --explain instead of "release". +
+ +### 3.4 Cleanup + +
    +
  1. `rm -rf %TCLBUILD%` +
+ +## 4.0 Testing the TEA(ish) Build (unix only) + +This part requires following the setup instructions for Unix systems, +at the top of this document. + +The former TEA, now TEA(ish), build of this extension uses the same +code as the builds described above but is provided in a form more +convenient for downstream Tcl users. + +It lives in `autoconf/tea` and, as part of the `autoconf` bundle, +_cannot be tested directly from the canonical tree_. Instead it has to +be packaged. + +### 4.1 Teaish Setup + +Follow the same Tcl- and environment-related related setup described +in the first section of this document, up to and including the +installation of Tcl (unless, of course, it was already installed using +those same instructions). + +### 4.2 Teaish Testing + +
    +
  1. `cd $SQLITESOURCE` +
  2. Run either `make snapshot-tarball` or `make amalgamation-tarball` + ↑ + Those steps will leave behind a temp dir called `mkpkg_tmp_dir`, + under which the extension is most readily reached. It can optionally + be extracted from the generated tarball, but that tarball was + generated from this dir, and reusing this dir is a time saver + during development. +
  3. `cd mkpkg_tmp/tea` +
  4. `./configure --with-tcl=$TCLHOME` +
  5. `make test install`
    + ↑ Should run to completion without any errors. +
  6. `make uninstall`
    + ↑ Will uninstall the extension. This _can_ be run + in the same invocation as the `install` target, but only + if the `-j#` make flag is _not_ used. If it is, the + install/uninstall steps will race and make a mess of things. + Parallel builds do not help in this build, anyway, as there's + only a single C file to compile. +
+ +When actively developing and testing the teaish build, which requires +going through the tarball generation, there's a caveat about the +`mkpkg_tmp_dir` dir: it will be deleted every time a tarball is +built, the shell console which is parked in that +directory for testing needs to add `cd $PWD &&` to the start of the +build commands, like: + +> +``` +[user@host:.../mkpkg_tmp_dir/tea]$ \ + cd $PWD && ./configure CFLAGS=-O0 --with-tcl=$TCLHOME \ + && make test install uninstall +``` + +### 4.3 Teaish Cleanup + + +
    +
  1. `rm -rf $TCLHOME` +
  2. `cd $SQLITESOURCE; rm -fr mkpkg_tmp_dir; fossil clean -x` +
+ +[Fossil]: https://fossil-scm.org/home +[tcl-fossil]: https://core.tcl-lang.org/tcl diff --git a/doc/testrunner.md b/doc/testrunner.md new file mode 100644 index 0000000000..d1696e9d1d --- /dev/null +++ b/doc/testrunner.md @@ -0,0 +1,404 @@ + + +# The testrunner.tcl Script + + + + +# 1. Overview + +The testrunner.tcl program is a Tcl script used to run multiple SQLite +tests in parallel, thus reducing testing time on multi-core machines. +It supports the following types of tests: + + * Tcl test scripts. + + * Fuzzcheck tests, including using an external fuzzcheck database. + + * Tests run with `make` commands. Examples: + - `make devtest` + - `make releasetest` + - `make sdevtest` + - `make testrunner` + +The testrunner.tcl program stores output of all tests and builds run in +log file **testrunner.log**, created in the current working directory. +Search this file to find details of errors. Suggested search commands: + + * `grep "^!" testrunner.log` + * `grep failed testrunner.log` + +The testrunner.tcl program also populates SQLite database **testrunner.db**. +This database contains details of all tests run, running and to be run. +A useful query might be: + +``` + SELECT * FROM script WHERE state='failed' +``` + +You can get a summary of errors in a prior run by invoking commands like +these: + +``` + tclsh $(TESTDIR)/testrunner.tcl errors + tclsh $(TESTDIR)/testrunner.tcl errors -v +``` + +Running the command: + +``` + tclsh $(TESTDIR)/testrunner.tcl status +``` + +in the directory containing the testrunner.db database runs various queries +to produce a succinct report on the state of a running testrunner.tcl script. +A good way to keep and eye on test progress is to run either of the two +following commands: + +``` + watch tclsh $(TESTDIR)/testrunner.tcl status + tclsh $(TESTDIR)/testrunner.tcl status -d 2 +``` + +Both of the commands above accomplish about the same thing, but the second +one has the advantage of not requiring "watch" to be installed on your +system. + +Sometimes testrunner.tcl uses the `testfixture` binary that it is run with +to run tests (see "Binary Tests" below). Sometimes it builds testfixture and +other binaries in specific configurations to test (see "Source Tests"). + + +# 2. Binary Tests + +The commands described in this section all run various combinations of the Tcl +test scripts using the `testfixture` binary used to run the testrunner.tcl +script (i.e. they do not invoke the compiler to build new binaries, or the +`make` command to run tests that are not Tcl scripts). The procedure to run +these tests is therefore: + + 1. Build the "testfixture" (or "testfixture.exe" for windows) binary using + whatever method seems convenient. + + 2. Test the binary built in step 1 by running testrunner.tcl with it, + perhaps with various options. + +The following sub-sections describe the various options that can be +passed to testrunner.tcl to test binary testfixture builds. + + +## 2.1. Organization of Tcl Tests + +Tcl tests are stored in files that match the pattern *\*.test*. They are +found in both the $TOP/test/ directory, and in the various sub-directories +of the $TOP/ext/ directory of the source tree. Not all *\*.test* files +contain Tcl tests - a handful are Tcl scripts designed to invoke other +*\*.test* files. + +The **veryquick** set of tests is a subset of all Tcl test scripts in the +source tree. In includes most tests, but excludes some that are very slow. +Almost all fault-injection tests (those that test the response of the library +to OOM or IO errors) are excluded. It is defined in source file +*test/permutations.test*. + +The **full** set of tests includes all Tcl test scripts in the source tree. +To run a "full" test is to run all Tcl test scripts that can be found in the +source tree. + +File *permutations.test* defines various test "permutations". A permutation +consists of: + + * A subset of Tcl test scripts, and + + * Runtime configuration to apply before running each test script + (e.g. enabling auto-vacuum, or disable lookaside). + +Running **all** tests is to run all tests in the full test set, plus a dozen +or so permutations. The specific permutations that are run as part of "all" +are defined in file *testrunner_data.tcl*. + + +## 2.2. Commands to Run Tests + +To run the "veryquick" test set, use either of the following: + +``` + ./testfixture $TESTDIR/testrunner.tcl + ./testfixture $TESTDIR/testrunner.tcl veryquick +``` + +To run the "full" test suite: + +``` + ./testfixture $TESTDIR/testrunner.tcl full +``` + +To run the subset of the "full" test suite for which the test file name matches +a specified pattern (e.g. all tests that start with "fts5"), either of: + +``` + ./testfixture $TESTDIR/testrunner.tcl fts5% + ./testfixture $TESTDIR/testrunner.tcl 'fts5*' +``` + +Strictly speaking, for a test to be run the pattern must match the script +filename, not including the directory, using the rules of Tcl's +\[string match\] command. Except that before the matching is done, any "%" +characters specified as part of the pattern are transformed to "\*". + + +To run "all" tests (full + permutations): + +``` + ./testfixture $TESTDIR/testrunner.tcl all +``` + + +## 2.3. Investigating Binary Test Failures + +If a test fails, testrunner.tcl reports name of the Tcl test script and, if +applicable, the name of the permutation, to stdout. This information can also +be retrieved from either *testrunner.log* or *testrunner.db*. + +If there is no permutation, the individual test script may be run with: + +``` + ./testfixture $PATH_TO_SCRIPT +``` + +Or, if the failure occured as part of a permutation: + +``` + ./testfixture $TESTDIR/testrunner.tcl $PERMUTATION $PATH_TO_SCRIPT +``` + +TODO: An example instead of "$PERMUTATION" and $PATH\_TO\_SCRIPT? + + +# 3. Source Code Tests + +The commands described in this section invoke the C compiler to build +binaries from the source tree, then use those binaries to run Tcl and +other tests. The advantages of this are that: + + * it is possible to test multiple build configurations with a single + command, and + + * it ensures that tests are always run using binaries created with the + same set of compiler options. + +The testrunner.tcl commands described in this section may be run using +either a *testfixture* (or testfixture.exe) build, or with any other Tcl +shell that supports SQLite 3.31.1 or newer via "package require sqlite3". + +TODO: ./configure + Makefile.msc build systems. + + +## 3.1. Commands to Run SQLite Tests + +The **mdevtest** command is equivalent to running the veryquick tests and +the `make fuzztest` target once for each of two --enable-all builds - one +with debugging enabled and one without: + +``` + tclsh $TESTDIR/testrunner.tcl mdevtest +``` + +In other words, it is equivalent to running: + +``` + $TOP/configure --enable-all --enable-debug + make fuzztest + make testfixture + ./testfixture $TOP/test/testrunner.tcl veryquick + + # Then, after removing files created by the tests above: + $TOP/configure --enable-all OPTS="-O0" + make fuzztest + make testfixture + ./testfixture $TOP/test/testrunner.tcl veryquick +``` + +The **sdevtest** command is identical to the mdevtest command, except that the +second of the two builds is a sanitizer build. Specifically, this means that +OPTS="-fsanitize=address,undefined" is specified instead of OPTS="-O0": + +``` + tclsh $TESTDIR/testrunner.tcl sdevtest +``` + +The **release** command runs lots of tests under lots of builds. It runs +different combinations of builds and tests depending on whether it is run +on Linux, Windows or OSX. Refer to *testrunner\_data.tcl* for the details +of the specific tests run. + +``` + tclsh $TESTDIR/testrunner.tcl release +``` + +As with source code tests, one or more patterns +may be appended to any of the above commands (mdevtest, sdevtest or release). +Pattern matching is used for both Tcl tests and fuzz tests. + +``` + tclsh $TESTDIR/testrunner.tcl release rtree% +``` + + +## 3.2. Running ZipVFS Tests + +testrunner.tcl can build a zipvfs-enabled testfixture and use it to run +tests from the Zipvfs project with the following command: + +``` + tclsh $TESTDIR/testrunner.tcl --zipvfs $PATH_TO_ZIPVFS +``` + +This can be combined with any of "mdevtest", "sdevtest" or "release" to +test both SQLite and Zipvfs with a single command: + +``` + tclsh $TESTDIR/testrunner.tcl --zipvfs $PATH_TO_ZIPVFS mdevtest +``` + + +## 3.3. Investigating Source Code Test Failures + +Investigating a test failure that occurs during source code testing is a +two step process: + + 1. Recreating the build configuration in which the test failed, and + + 2. Re-running the actual test. + +To recreate a build configuration, use the testrunner.tcl **script** command +to create a build script. A build script is a bash script on Linux or OSX, or +a dos \*.bat file on windows. For example: + +``` + # Create a script that recreates build configuration "Device-One" on + # Linux or OSX: + tclsh $TESTDIR/testrunner.tcl script Device-One > make.sh + + # Create a script that recreates build configuration "Have-Not" on Windows: + tclsh $TESTDIR/testrunner.tcl script Have-Not > make.bat +``` + +The generated bash or \*.bat file script accepts a single argument - a makefile +target to build. This may be used either to run a `make` command test directly, +or else to build a testfixture (or testfixture.exe) binary with which to +run a Tcl test script, as described above. + + +## 3.4 External Fuzzcheck Databases + +Testrunner.tcl will also run fuzzcheck against an external (out of tree) +database, for example fuzzcheck databases generated by dbsqlfuzz. To do +this, simply add the "`--fuzzdb` *FILENAME*" command-line option or set +the FUZZDB environment variable to the name of the external +database. For large external databases, testrunner.tcl will automatically use +the "`--slice`" command-line option of fuzzcheck to divide the work up into +multiple jobs, to increase parallelism. + +Thus, for example, to run a full releasetest including an external +dbsqlfuzz database, run a command like one of these: + +``` + tclsh test/testrunner.tcl releasetest --fuzzdb ../fuzz/20250415.db + FUZZDB=../fuzz/20250415.db make releasetest + nmake /f Makefile.msc FUZZDB=../fuzz/20250415.db releasetest +``` + +The patternlist option to testrunner.tcl will match against fuzzcheck +databases. So if you want to run *only* tests involving the external +database, you can use a command something like this: + +``` + tclsh test/testrunner.tcl releasetest 20250415 --fuzzdb ../fuzz/20250415.db +``` + + +# 4. Extra testrunner.tcl Options + +The testrunner.tcl script options in this section may be used with both source +code and binary tests. + +The **--buildonly** option instructs testrunner.tcl just to build the binaries +required by a test, not to run any actual tests. For example: + +``` + # Build binaries required by release test. + tclsh $TESTDIR/testrunner.tcl --buildonly release" +``` + +The **--dryrun** option prevents testrunner.tcl from building any binaries +or running any tests. Instead, it just writes the shell commands that it +would normally execute into the testrunner.log file. Example: + +``` + # Log the shell commmands that make up the mdevtest test. + tclsh $TESTDIR/testrunner.tcl --dryrun mdevtest" +``` + +The **--explain** option is similar to --dryrun in that it prevents +testrunner.tcl from building any binaries or running any tests. The +difference is that --explain prints on standard output a human-readable +summary of all the builds and tests that would have been run. + +``` + # Show what builds and tests would have been run + tclsh $TESTDIR/testrunner.tcl --explain mdevtest +``` + +The **--status** option uses VT100 escape sequences to display the test +status full-screen. This is similar to running +"`watch test/testrunner status`" in a separate window, just more convenient. +Unfortunately, this option does not work correctly on Windows, due to the +sketchy implementation of VT100 escapes on the Windows console. + + +# 5. Controlling CPU Core Utilization + +When running either binary or source code tests, testrunner.tcl reports the +number of jobs it intends to use to stdout. e.g. + +``` + $ ./testfixture $TESTDIR/testrunner.tcl + splitting work across 16 jobs + ... more output ... +``` + +By default, testfixture.tcl attempts to set the number of jobs to the number +of real cores on the machine. This can be overridden using the "--jobs" (or -j) +switch: + +``` + $ ./testfixture $TESTDIR/testrunner.tcl --jobs 8 + splitting work across 8 jobs + ... more output ... +``` + +The number of jobs may also be changed while an instance of testrunner.tcl is +running by exucuting the following command from the directory containing the +testrunner.log and testrunner.db files: + +``` + $ ./testfixture $TESTDIR/testrunner.tcl njob $NEW_NUMBER_OF_JOBS +``` diff --git a/doc/trusted-schema.md b/doc/trusted-schema.md new file mode 100644 index 0000000000..d431fd49a3 --- /dev/null +++ b/doc/trusted-schema.md @@ -0,0 +1,142 @@ +# The new-security-options branch + +## The problem that the [new-security-options](/timeline?r=new-security-options) branch tries to solve + +An attacker might modify the schema of an SQLite database by adding +structures that cause code to run when some other application opens and +reads the database. For example, the attacker might replace a table +definition with a view. Or the attacker might add triggers to tables +or views, or add new CHECK constraints or generated columns or indexes +with expressions in the index list or in the WHERE clause. If the +added features invoke SQL functions or virtual tables with side effects, +that might cause harm to the system if run by a high-privilege victim. +Or, the added features might exfiltrate information if the database is +read by a high-privilege victim. + +The changes in this branch strive to make it easier for high-privilege +applications to safely read SQLite database files that might have been +maliciously corrupted by an attacker. + +## Overview of changes in [new-security-options](/timeline?r=new-security-options) + +The basic idea is to tag every SQL function and virtual table with one +of three risk levels: + + 1. Innocuous + 2. Normal + 3. Direct-Only + +Innocuous functions/vtabs are safe and can be used at any time. +Direct-only elements, in contrast, might have cause side-effects and +should only be used from top-level SQL, not from within triggers or views nor +in elements of the schema such as CHECK constraint, DEFAULT values, +generated columns, index expressions, or in the WHERE clause of a +partial index that are potentially under the control of an attacker. +Normal elements behave like Innocuous if TRUSTED\_SCHEMA=on +and behave like direct-only if TRUSTED\_SCHEMA=off. + +Application-defined functions and virtual tables go in as Normal unless +the application takes deliberate steps to change the risk level. + +For backwards compatibility, the default is TRUSTED\_SCHEMA=on. Documentation +will be updated to recommend applications turn TRUSTED\_SCHEMA to off. + +An innocuous function or virtual table is one that can only read content +from the database file in which it resides, and can only alter the database +in which it resides. Most SQL functions are innocuous. For example, there +is no harm in an attacker running the abs() function. + +Direct-only elements that have side-effects that go outside the database file +in which it lives, or return information from outside of the database file. +Examples of direct-only elements include: + + 1. The fts3\_tokenizer() function + 2. The writefile() function + 3. The readfile() function + 4. The zipvfs virtual table + 5. The csv virtual table + +We do not want an attacker to be able to add these kinds of things to +the database schema and possibly trick a high-privilege application +from performing any of these actions. Therefore, functions and vtabs +with side-effects are marked as Direct-Only. + +Legacy applications might add other risky functions or vtabs. Those will +go in as "Normal" by default. For optimal security, we want those risky +app-defined functions and vtabs to be direct-only, but making that the +default might break some legacy applications. Hence, all app-defined +functions and vtabs go in as Normal, but the application can switch them +over to "Direct-Only" behavior using a single pragma. + +The restrictions on the use of functions and virtual tables do not apply +to TEMP. A TEMP VIEW or a TEMP TRIGGER can use any valid SQL function +or virtual table. The idea is that TEMP views and triggers must be +directly created by the application and are thus under the control of the +application. TEMP views and triggers cannot be created by an attacker who +corrupts the schema of a persistent database file. Hence TEMP views and +triggers are safe. + +## Specific changes + + 1. New sqlite3\_db\_config() option SQLITE\_DBCONFIG\_TRUSTED\_SCHEMA for + turning TRUSTED\_SCHEMA on and off. It defaults to ON. + + 2. Compile-time option -DSQLITE\_TRUSTED\_SCHEMA=0 causes the default + TRUSTED\_SCHEMA setting to be off. + + 3. New pragma "PRAGMA trusted\_schema=(ON\|OFF);". This provides access + to the TRUSTED_SCHEMA setting for application coded using scripting + languages or other secondary languages where they are unable to make + calls to sqlite3\_db\_config(). + + 4. New options for the "enc" parameter to sqlite3\_create\_function() and + its kin: +
    +
  1. _SQLITE\_INNOCUOUS_ → tags the new functions as Innocuous +
  2. _SQLITE\_DIRECTONLY_ → tags the new functions as Direct-Only +
+ + 5. New options to sqlite3\_vtab\_config(): +
    +
  1. _SQLITE\_VTAB\_INNOCUOUS_ → tags the vtab as Innocuous +
  2. _SQLITE\_VTAB\_DIRECTONLY_ → tags the vtab as Direct-Only +
+ + 6. Change many of the functions and virtual tables in the SQLite source + tree to use one of the tags above. + + 7. Enhanced PRAGMA function\_list and virtual-table "pragma\_function\_list" + with additional columns. The columns now are: +
    +
  • _name_ → Name of the function +
  • _builtin_ → 1 for built-in functions. 0 otherwise. +
  • _type_ → 's'=Scalar, 'a'=Aggregate, 'w'=Window +
  • _enc_ → 'utf8', 'utf16le', or 'utf16be' +
  • _narg_ → number of argument +
  • _flags_ → Bitmask of SQLITE\_INNOCUOUS, SQLITE\_DIRECTONLY, + SQLITE\_DETERMINISTIC, SQLITE\_SUBTYPE, and + SQLITE\_FUNC\_INTERNAL flags. +
+

The last four columns are new. + + 8. The function\_list PRAGMA now also shows all entries for each function. + So, for example, if a function can take either 2 or 3 arguments, + there are separate rows for the 2-argument and 3-argument versions of + the function. + +## Additional Notes + +The function_list enhancements allow the application to query the set +of SQL functions that meet various criteria. For example, to see all +SQL functions that are never allowed to be used in the schema or in +trigger or views: + +~~~ + SELECT DISTINCT name FROM pragma_function_list + WHERE (flags & 0x80000)!=0 + ORDER BY name; +~~~ + +Doing the same is not possible for virtual tables, as a virtual table +might be Innocuous, Normal, or Direct-Only depending on the arguments +passed into the xConnect method. diff --git a/doc/vdbesort-memory.md b/doc/vdbesort-memory.md new file mode 100644 index 0000000000..5c3dd62d2f --- /dev/null +++ b/doc/vdbesort-memory.md @@ -0,0 +1,49 @@ + +20-11-2020 + +# Memory Allocation In vdbesort.c + +Memory allocation is slightly different depending on: + + * whether or not SQLITE_CONFIG_SMALL_MALLOC is set, and + * whether or not worker threads are enabled. + +## SQLITE_CONFIG_SMALL_MALLOC=0 + +Assuming SQLITE_CONFIG_SMALL_MALLOC is not set, keys passed to the sorter are +added to an in-memory buffer. This buffer is grown using sqlite3Realloc() as +required it reaches the size configured for the main pager cache using "PRAGMA +cache_size". i.e. if the user has executed "PRAGMA main.cache_size = -2048", +then this buffer is allowed to grow up to 2MB in size. + +Once the buffer has grown to its threshold, keys are sorted and written to +a temp file. If worker threads are not enabled, this is the only significant +allocation the sorter module makes. After keys are sorted and flushed out to +the temp file, the buffer is reused to accumulate the next batch of keys. + +If worker threads are available, then the buffer is passed to a worker thread +to sort and flush once it is full, and a new buffer allocated to allow the +main thread to continue to accumulate keys. Buffers are reused once they +have been flushed, so in this case at most (nWorker+1) buffers are allocated +and used, where nWorker is the number of configured worker threads. + +There are no other significant users of heap memory in the sorter module. +Once sorted buffers of keys have been flushed to disk, they are read back +either by mapping the file (via sqlite3_file.xFetch()) or else read back +in one page at a time. + +All buffers are allocated by the main thread. A sorter object is associated +with a single database connection, to which it holds a pointer. + +## SQLITE_CONFIG_SMALL_MALLOC=1 + +This case is similar to the above, except that instead of accumulating +multiple keys in a single large buffer, sqlite3VdbeSorterWrite() stores +keys in a regular heap-memory linked list (one allocation per element). +List elements are freed as they are flushed to disk, either by the main +thread or by a worker thread. + +Each time a key is added the sorter (and an allocation made), +sqlite3HeapNearlyFull() is called. If it returns true, the current +list of keys is flushed to a temporary file, even if it has not yet +reached the size threshold. diff --git a/doc/vfs-shm.txt b/doc/vfs-shm.txt index c1f125a120..a483e9b159 100644 --- a/doc/vfs-shm.txt +++ b/doc/vfs-shm.txt @@ -1,6 +1,6 @@ The 5 states of an historical rollback lock as implemented by the xLock, xUnlock, and xCheckReservedLock methods of the sqlite3_io_methods -objec are: +object are: UNLOCKED SHARED @@ -58,7 +58,7 @@ The meanings of the various wal-index locking states is as follows: A particular lock manager implementation may coalesce one or more of the wal-index locking states, though with a reduction in concurrency. -For example, an implemention might implement only exclusive locking, +For example, an implementation might implement only exclusive locking, in which case all states would be equivalent to CHECKPOINT, meaning that only one reader or one writer or one checkpointer could be active at a time. Or, an implementation might combine READ and READ_FULL into diff --git a/doc/wal-lock.md b/doc/wal-lock.md new file mode 100644 index 0000000000..8df7cc836c --- /dev/null +++ b/doc/wal-lock.md @@ -0,0 +1,88 @@ +# Wal-Mode Blocking Locks + +On some Unix-like systems, SQLite may be configured to use POSIX blocking locks +by: + + * building the library with SQLITE\_ENABLE\_SETLK\_TIMEOUT defined, and + * configuring a timeout in ms using the sqlite3\_busy\_timeout() API. + +Blocking locks may be advantageous as (a) waiting database clients do not +need to continuously poll the database lock, and (b) using blocking locks +facilitates transfer of OS priority between processes when a high priority +process is blocked by a lower priority one. + +Only read/write clients use blocking locks. Clients that have read-only access +to the \*-shm file never use blocking locks. + +Threads or processes that access a single database at a time never deadlock as +a result of blocking database locks. But it is of course possible for threads +that lock multiple databases simultaneously to do so. In most cases the OS will +detect the deadlock and return an error. + +## Wal Recovery + +Wal database "recovery" is a process required when the number of connected +database clients changes from zero to one. In this case, a client is +considered to connect to the database when it first reads data from it. +Before recovery commences, an exclusive WRITER lock is taken. + +Without blocking locks, if two clients attempt recovery simultaneously, one +fails to obtain the WRITER lock and either invokes the busy-handler callback or +returns SQLITE\_BUSY to the user. With blocking locks configured, the second +client blocks on the WRITER lock. + +## Database Readers + +Usually, read-only are not blocked by any other database clients, so they +have no need of blocking locks. + +If a read-only transaction is being opened on a snapshot, the CHECKPOINTER +lock is required briefly as part of opening the transaction (to check that a +checkpointer is not currently overwriting the snapshot being opened). A +blocking lock is used to obtain the CHECKPOINTER lock in this case. A snapshot +opener may therefore block on and transfer priority to a checkpointer in some +cases. + +## Database Writers + +A database writer must obtain the exclusive WRITER lock. It uses a blocking +lock to do so if any of the following are true: + + * the transaction is an implicit one consisting of a single DML or DDL + statement, or + * the transaction is opened using BEGIN IMMEDIATE or BEGIN EXCLUSIVE, or + * the first SQL statement executed following the BEGIN command is a DML or + DDL statement (not a read-only statement like a SELECT). + +In other words, in all cases except when an open read-transaction is upgraded +to a write-transaction. In that case a non-blocking lock is used. + +## Database Checkpointers + +Database checkpointers takes the following locks, in order: + + * The exclusive CHECKPOINTER lock. + * The exclusive WRITER lock (FULL, RESTART and TRUNCATE only). + * Exclusive lock on read-mark slots 1-N. These are immediately released after being taken. + * Exclusive lock on read-mark 0. + * Exclusive lock on read-mark slots 1-N again. These are immediately released + after being taken (RESTART and TRUNCATE only). + +All of the above use blocking locks. + +## Summary + +With blocking locks configured, the only cases in which clients should see an +SQLITE\_BUSY error are: + + * if the OS does not grant a blocking lock before the configured timeout + expires, and + * when an open read-transaction is upgraded to a write-transaction. + +In all other cases the blocking locks implementation should prevent clients +from having to handle SQLITE\_BUSY errors and facilitate appropriate transfer +of priorities between competing clients. + +Clients that lock multiple databases simultaneously must be wary of deadlock. + + diff --git a/ext/README.md b/ext/README.md new file mode 100644 index 0000000000..78312819ab --- /dev/null +++ b/ext/README.md @@ -0,0 +1,8 @@ +## Loadable Extensions + +Various [loadable extensions](https://sqlite.org/loadext.html) for +SQLite are found in subfolders. + +Most subfolders are dedicated to a single loadable extension (for +example FTS5, or RTREE). But the misc/ subfolder contains a collection +of smaller single-file extensions. diff --git a/ext/README.txt b/ext/README.txt deleted file mode 100644 index 009495f59d..0000000000 --- a/ext/README.txt +++ /dev/null @@ -1,2 +0,0 @@ -Version loadable extensions to SQLite are found in subfolders -of this folder. diff --git a/ext/async/README.txt b/ext/async/README.txt deleted file mode 100644 index f62fa2fc17..0000000000 --- a/ext/async/README.txt +++ /dev/null @@ -1,170 +0,0 @@ -NOTE (2012-11-29): - -The functionality implemented by this extension has been superseded -by WAL-mode. This module is no longer supported or maintained. The -code is retained for historical reference only. - ------------------------------------------------------------------------------- - -Normally, when SQLite writes to a database file, it waits until the write -operation is finished before returning control to the calling application. -Since writing to the file-system is usually very slow compared with CPU -bound operations, this can be a performance bottleneck. This directory -contains an extension that causes SQLite to perform all write requests -using a separate thread running in the background. Although this does not -reduce the overall system resources (CPU, disk bandwidth etc.) at all, it -allows SQLite to return control to the caller quickly even when writing to -the database, eliminating the bottleneck. - - 1. Functionality - - 1.1 How it Works - 1.2 Limitations - 1.3 Locking and Concurrency - - 2. Compilation and Usage - - 3. Porting - - - -1. FUNCTIONALITY - - With asynchronous I/O, write requests are handled by a separate thread - running in the background. This means that the thread that initiates - a database write does not have to wait for (sometimes slow) disk I/O - to occur. The write seems to happen very quickly, though in reality - it is happening at its usual slow pace in the background. - - Asynchronous I/O appears to give better responsiveness, but at a price. - You lose the Durable property. With the default I/O backend of SQLite, - once a write completes, you know that the information you wrote is - safely on disk. With the asynchronous I/O, this is not the case. If - your program crashes or if a power loss occurs after the database - write but before the asynchronous write thread has completed, then the - database change might never make it to disk and the next user of the - database might not see your change. - - You lose Durability with asynchronous I/O, but you still retain the - other parts of ACID: Atomic, Consistent, and Isolated. Many - appliations get along fine without the Durablity. - - 1.1 How it Works - - Asynchronous I/O works by creating a special SQLite "vfs" structure - and registering it with sqlite3_vfs_register(). When files opened via - this vfs are written to (using the vfs xWrite() method), the data is not - written directly to disk, but is placed in the "write-queue" to be - handled by the background thread. - - When files opened with the asynchronous vfs are read from - (using the vfs xRead() method), the data is read from the file on - disk and the write-queue, so that from the point of view of - the vfs reader the xWrite() appears to have already completed. - - The special vfs is registered (and unregistered) by calls to the - API functions sqlite3async_initialize() and sqlite3async_shutdown(). - See section "Compilation and Usage" below for details. - - 1.2 Limitations - - In order to gain experience with the main ideas surrounding asynchronous - IO, this implementation is deliberately kept simple. Additional - capabilities may be added in the future. - - For example, as currently implemented, if writes are happening at a - steady stream that exceeds the I/O capability of the background writer - thread, the queue of pending write operations will grow without bound. - If this goes on for long enough, the host system could run out of memory. - A more sophisticated module could to keep track of the quantity of - pending writes and stop accepting new write requests when the queue of - pending writes grows too large. - - 1.3 Locking and Concurrency - - Multiple connections from within a single process that use this - implementation of asynchronous IO may access a single database - file concurrently. From the point of view of the user, if all - connections are from within a single process, there is no difference - between the concurrency offered by "normal" SQLite and SQLite - using the asynchronous backend. - - If file-locking is enabled (it is enabled by default), then connections - from multiple processes may also read and write the database file. - However concurrency is reduced as follows: - - * When a connection using asynchronous IO begins a database - transaction, the database is locked immediately. However the - lock is not released until after all relevant operations - in the write-queue have been flushed to disk. This means - (for example) that the database may remain locked for some - time after a "COMMIT" or "ROLLBACK" is issued. - - * If an application using asynchronous IO executes transactions - in quick succession, other database users may be effectively - locked out of the database. This is because when a BEGIN - is executed, a database lock is established immediately. But - when the corresponding COMMIT or ROLLBACK occurs, the lock - is not released until the relevant part of the write-queue - has been flushed through. As a result, if a COMMIT is followed - by a BEGIN before the write-queue is flushed through, the database - is never unlocked,preventing other processes from accessing - the database. - - File-locking may be disabled at runtime using the sqlite3async_control() - API (see below). This may improve performance when an NFS or other - network file-system, as the synchronous round-trips to the server be - required to establish file locks are avoided. However, if multiple - connections attempt to access the same database file when file-locking - is disabled, application crashes and database corruption is a likely - outcome. - - -2. COMPILATION AND USAGE - - The asynchronous IO extension consists of a single file of C code - (sqlite3async.c), and a header file (sqlite3async.h) that defines the - C API used by applications to activate and control the modules - functionality. - - To use the asynchronous IO extension, compile sqlite3async.c as - part of the application that uses SQLite. Then use the API defined - in sqlite3async.h to initialize and configure the module. - - The asynchronous IO VFS API is described in detail in comments in - sqlite3async.h. Using the API usually consists of the following steps: - - 1. Register the asynchronous IO VFS with SQLite by calling the - sqlite3async_initialize() function. - - 2. Create a background thread to perform write operations and call - sqlite3async_run(). - - 3. Use the normal SQLite API to read and write to databases via - the asynchronous IO VFS. - - Refer to sqlite3async.h for details. - - -3. PORTING - - Currently the asynchronous IO extension is compatible with win32 systems - and systems that support the pthreads interface, including Mac OSX, Linux, - and other varieties of Unix. - - To port the asynchronous IO extension to another platform, the user must - implement mutex and condition variable primitives for the new platform. - Currently there is no externally available interface to allow this, but - modifying the code within sqlite3async.c to include the new platforms - concurrency primitives is relatively easy. Search within sqlite3async.c - for the comment string "PORTING FUNCTIONS" for details. Then implement - new versions of each of the following: - - static void async_mutex_enter(int eMutex); - static void async_mutex_leave(int eMutex); - static void async_cond_wait(int eCond, int eMutex); - static void async_cond_signal(int eCond); - static void async_sched_yield(void); - - The functionality required of each of the above functions is described - in comments in sqlite3async.c. diff --git a/ext/async/sqlite3async.c b/ext/async/sqlite3async.c deleted file mode 100644 index b6f4a4bd36..0000000000 --- a/ext/async/sqlite3async.c +++ /dev/null @@ -1,1707 +0,0 @@ -/* -** 2005 December 14 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -************************************************************************* -** -** $Id: sqlite3async.c,v 1.7 2009/07/18 11:52:04 danielk1977 Exp $ -** -** This file contains the implementation of an asynchronous IO backend -** for SQLite. -*/ - -#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ASYNCIO) - -#include "sqlite3async.h" -#include "sqlite3.h" -#include -#include -#include - -/* Useful macros used in several places */ -#define MIN(x,y) ((x)<(y)?(x):(y)) -#define MAX(x,y) ((x)>(y)?(x):(y)) - -#ifndef SQLITE_AMALGAMATION -/* Macro to mark parameters as unused and silence compiler warnings. */ -#define UNUSED_PARAMETER(x) (void)(x) -#endif - -/* Forward references */ -typedef struct AsyncWrite AsyncWrite; -typedef struct AsyncFile AsyncFile; -typedef struct AsyncFileData AsyncFileData; -typedef struct AsyncFileLock AsyncFileLock; -typedef struct AsyncLock AsyncLock; - -/* Enable for debugging */ -#ifndef NDEBUG -#include -static int sqlite3async_trace = 0; -# define ASYNC_TRACE(X) if( sqlite3async_trace ) asyncTrace X -static void asyncTrace(const char *zFormat, ...){ - char *z; - va_list ap; - va_start(ap, zFormat); - z = sqlite3_vmprintf(zFormat, ap); - va_end(ap); - fprintf(stderr, "[%d] %s", 0 /* (int)pthread_self() */, z); - sqlite3_free(z); -} -#else -# define ASYNC_TRACE(X) -#endif - -/* -** THREAD SAFETY NOTES -** -** Basic rules: -** -** * Both read and write access to the global write-op queue must be -** protected by the async.queueMutex. As are the async.ioError and -** async.nFile variables. -** -** * The async.pLock list and all AsyncLock and AsyncFileLock -** structures must be protected by the async.lockMutex mutex. -** -** * The file handles from the underlying system are not assumed to -** be thread safe. -** -** * See the last two paragraphs under "The Writer Thread" for -** an assumption to do with file-handle synchronization by the Os. -** -** Deadlock prevention: -** -** There are three mutex used by the system: the "writer" mutex, -** the "queue" mutex and the "lock" mutex. Rules are: -** -** * It is illegal to block on the writer mutex when any other mutex -** are held, and -** -** * It is illegal to block on the queue mutex when the lock mutex -** is held. -** -** i.e. mutex's must be grabbed in the order "writer", "queue", "lock". -** -** File system operations (invoked by SQLite thread): -** -** xOpen -** xDelete -** xFileExists -** -** File handle operations (invoked by SQLite thread): -** -** asyncWrite, asyncClose, asyncTruncate, asyncSync -** -** The operations above add an entry to the global write-op list. They -** prepare the entry, acquire the async.queueMutex momentarily while -** list pointers are manipulated to insert the new entry, then release -** the mutex and signal the writer thread to wake up in case it happens -** to be asleep. -** -** -** asyncRead, asyncFileSize. -** -** Read operations. Both of these read from both the underlying file -** first then adjust their result based on pending writes in the -** write-op queue. So async.queueMutex is held for the duration -** of these operations to prevent other threads from changing the -** queue in mid operation. -** -** -** asyncLock, asyncUnlock, asyncCheckReservedLock -** -** These primitives implement in-process locking using a hash table -** on the file name. Files are locked correctly for connections coming -** from the same process. But other processes cannot see these locks -** and will therefore not honor them. -** -** -** The writer thread: -** -** The async.writerMutex is used to make sure only there is only -** a single writer thread running at a time. -** -** Inside the writer thread is a loop that works like this: -** -** WHILE (write-op list is not empty) -** Do IO operation at head of write-op list -** Remove entry from head of write-op list -** END WHILE -** -** The async.queueMutex is always held during the test, and when the entry is removed from the head -** of the write-op list. Sometimes it is held for the interim -** period (while the IO is performed), and sometimes it is -** relinquished. It is relinquished if (a) the IO op is an -** ASYNC_CLOSE or (b) when the file handle was opened, two of -** the underlying systems handles were opened on the same -** file-system entry. -** -** If condition (b) above is true, then one file-handle -** (AsyncFile.pBaseRead) is used exclusively by sqlite threads to read the -** file, the other (AsyncFile.pBaseWrite) by sqlite3_async_flush() -** threads to perform write() operations. This means that read -** operations are not blocked by asynchronous writes (although -** asynchronous writes may still be blocked by reads). -** -** This assumes that the OS keeps two handles open on the same file -** properly in sync. That is, any read operation that starts after a -** write operation on the same file system entry has completed returns -** data consistent with the write. We also assume that if one thread -** reads a file while another is writing it all bytes other than the -** ones actually being written contain valid data. -** -** If the above assumptions are not true, set the preprocessor symbol -** SQLITE_ASYNC_TWO_FILEHANDLES to 0. -*/ - - -#ifndef NDEBUG -# define TESTONLY( X ) X -#else -# define TESTONLY( X ) -#endif - -/* -** PORTING FUNCTIONS -** -** There are two definitions of the following functions. One for pthreads -** compatible systems and one for Win32. These functions isolate the OS -** specific code required by each platform. -** -** The system uses three mutexes and a single condition variable. To -** block on a mutex, async_mutex_enter() is called. The parameter passed -** to async_mutex_enter(), which must be one of ASYNC_MUTEX_LOCK, -** ASYNC_MUTEX_QUEUE or ASYNC_MUTEX_WRITER, identifies which of the three -** mutexes to lock. Similarly, to unlock a mutex, async_mutex_leave() is -** called with a parameter identifying the mutex being unlocked. Mutexes -** are not recursive - it is an error to call async_mutex_enter() to -** lock a mutex that is already locked, or to call async_mutex_leave() -** to unlock a mutex that is not currently locked. -** -** The async_cond_wait() and async_cond_signal() functions are modelled -** on the pthreads functions with similar names. The first parameter to -** both functions is always ASYNC_COND_QUEUE. When async_cond_wait() -** is called the mutex identified by the second parameter must be held. -** The mutex is unlocked, and the calling thread simultaneously begins -** waiting for the condition variable to be signalled by another thread. -** After another thread signals the condition variable, the calling -** thread stops waiting, locks mutex eMutex and returns. The -** async_cond_signal() function is used to signal the condition variable. -** It is assumed that the mutex used by the thread calling async_cond_wait() -** is held by the caller of async_cond_signal() (otherwise there would be -** a race condition). -** -** It is guaranteed that no other thread will call async_cond_wait() when -** there is already a thread waiting on the condition variable. -** -** The async_sched_yield() function is called to suggest to the operating -** system that it would be a good time to shift the current thread off the -** CPU. The system will still work if this function is not implemented -** (it is not currently implemented for win32), but it might be marginally -** more efficient if it is. -*/ -static void async_mutex_enter(int eMutex); -static void async_mutex_leave(int eMutex); -static void async_cond_wait(int eCond, int eMutex); -static void async_cond_signal(int eCond); -static void async_sched_yield(void); - -/* -** There are also two definitions of the following. async_os_initialize() -** is called when the asynchronous VFS is first installed, and os_shutdown() -** is called when it is uninstalled (from within sqlite3async_shutdown()). -** -** For pthreads builds, both of these functions are no-ops. For win32, -** they provide an opportunity to initialize and finalize the required -** mutex and condition variables. -** -** If async_os_initialize() returns other than zero, then the initialization -** fails and SQLITE_ERROR is returned to the user. -*/ -static int async_os_initialize(void); -static void async_os_shutdown(void); - -/* Values for use as the 'eMutex' argument of the above functions. The -** integer values assigned to these constants are important for assert() -** statements that verify that mutexes are locked in the correct order. -** Specifically, it is unsafe to try to lock mutex N while holding a lock -** on mutex M if (M<=N). -*/ -#define ASYNC_MUTEX_LOCK 0 -#define ASYNC_MUTEX_QUEUE 1 -#define ASYNC_MUTEX_WRITER 2 - -/* Values for use as the 'eCond' argument of the above functions. */ -#define ASYNC_COND_QUEUE 0 - -/************************************************************************* -** Start of OS specific code. -*/ -#if SQLITE_OS_WIN || defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__) - -#include - -/* The following block contains the win32 specific code. */ - -#define mutex_held(X) (GetCurrentThreadId()==primitives.aHolder[X]) - -static struct AsyncPrimitives { - int isInit; - DWORD aHolder[3]; - CRITICAL_SECTION aMutex[3]; - HANDLE aCond[1]; -} primitives = { 0 }; - -static int async_os_initialize(void){ - if( !primitives.isInit ){ - primitives.aCond[0] = CreateEvent(NULL, TRUE, FALSE, 0); - if( primitives.aCond[0]==NULL ){ - return 1; - } - InitializeCriticalSection(&primitives.aMutex[0]); - InitializeCriticalSection(&primitives.aMutex[1]); - InitializeCriticalSection(&primitives.aMutex[2]); - primitives.isInit = 1; - } - return 0; -} -static void async_os_shutdown(void){ - if( primitives.isInit ){ - DeleteCriticalSection(&primitives.aMutex[0]); - DeleteCriticalSection(&primitives.aMutex[1]); - DeleteCriticalSection(&primitives.aMutex[2]); - CloseHandle(primitives.aCond[0]); - primitives.isInit = 0; - } -} - -/* The following block contains the Win32 specific code. */ -static void async_mutex_enter(int eMutex){ - assert( eMutex==0 || eMutex==1 || eMutex==2 ); - assert( eMutex!=2 || (!mutex_held(0) && !mutex_held(1) && !mutex_held(2)) ); - assert( eMutex!=1 || (!mutex_held(0) && !mutex_held(1)) ); - assert( eMutex!=0 || (!mutex_held(0)) ); - EnterCriticalSection(&primitives.aMutex[eMutex]); - TESTONLY( primitives.aHolder[eMutex] = GetCurrentThreadId(); ) -} -static void async_mutex_leave(int eMutex){ - assert( eMutex==0 || eMutex==1 || eMutex==2 ); - assert( mutex_held(eMutex) ); - TESTONLY( primitives.aHolder[eMutex] = 0; ) - LeaveCriticalSection(&primitives.aMutex[eMutex]); -} -static void async_cond_wait(int eCond, int eMutex){ - ResetEvent(primitives.aCond[eCond]); - async_mutex_leave(eMutex); - WaitForSingleObject(primitives.aCond[eCond], INFINITE); - async_mutex_enter(eMutex); -} -static void async_cond_signal(int eCond){ - assert( mutex_held(ASYNC_MUTEX_QUEUE) ); - SetEvent(primitives.aCond[eCond]); -} -static void async_sched_yield(void){ - Sleep(0); -} -#else - -/* The following block contains the pthreads specific code. */ -#include -#include - -#define mutex_held(X) pthread_equal(primitives.aHolder[X], pthread_self()) - -static int async_os_initialize(void) {return 0;} -static void async_os_shutdown(void) {} - -static struct AsyncPrimitives { - pthread_mutex_t aMutex[3]; - pthread_cond_t aCond[1]; - pthread_t aHolder[3]; -} primitives = { - { PTHREAD_MUTEX_INITIALIZER, - PTHREAD_MUTEX_INITIALIZER, - PTHREAD_MUTEX_INITIALIZER - } , { - PTHREAD_COND_INITIALIZER - } , { 0, 0, 0 } -}; - -static void async_mutex_enter(int eMutex){ - assert( eMutex==0 || eMutex==1 || eMutex==2 ); - assert( eMutex!=2 || (!mutex_held(0) && !mutex_held(1) && !mutex_held(2)) ); - assert( eMutex!=1 || (!mutex_held(0) && !mutex_held(1)) ); - assert( eMutex!=0 || (!mutex_held(0)) ); - pthread_mutex_lock(&primitives.aMutex[eMutex]); - TESTONLY( primitives.aHolder[eMutex] = pthread_self(); ) -} -static void async_mutex_leave(int eMutex){ - assert( eMutex==0 || eMutex==1 || eMutex==2 ); - assert( mutex_held(eMutex) ); - TESTONLY( primitives.aHolder[eMutex] = 0; ) - pthread_mutex_unlock(&primitives.aMutex[eMutex]); -} -static void async_cond_wait(int eCond, int eMutex){ - assert( eMutex==0 || eMutex==1 || eMutex==2 ); - assert( mutex_held(eMutex) ); - TESTONLY( primitives.aHolder[eMutex] = 0; ) - pthread_cond_wait(&primitives.aCond[eCond], &primitives.aMutex[eMutex]); - TESTONLY( primitives.aHolder[eMutex] = pthread_self(); ) -} -static void async_cond_signal(int eCond){ - assert( mutex_held(ASYNC_MUTEX_QUEUE) ); - pthread_cond_signal(&primitives.aCond[eCond]); -} -static void async_sched_yield(void){ - sched_yield(); -} -#endif -/* -** End of OS specific code. -*************************************************************************/ - -#define assert_mutex_is_held(X) assert( mutex_held(X) ) - - -#ifndef SQLITE_ASYNC_TWO_FILEHANDLES -/* #define SQLITE_ASYNC_TWO_FILEHANDLES 0 */ -#define SQLITE_ASYNC_TWO_FILEHANDLES 1 -#endif - -/* -** State information is held in the static variable "async" defined -** as the following structure. -** -** Both async.ioError and async.nFile are protected by async.queueMutex. -*/ -static struct TestAsyncStaticData { - AsyncWrite *pQueueFirst; /* Next write operation to be processed */ - AsyncWrite *pQueueLast; /* Last write operation on the list */ - AsyncLock *pLock; /* Linked list of all AsyncLock structures */ - volatile int ioDelay; /* Extra delay between write operations */ - volatile int eHalt; /* One of the SQLITEASYNC_HALT_XXX values */ - volatile int bLockFiles; /* Current value of "lockfiles" parameter */ - int ioError; /* True if an IO error has occurred */ - int nFile; /* Number of open files (from sqlite pov) */ -} async = { 0,0,0,0,0,1,0,0 }; - -/* Possible values of AsyncWrite.op */ -#define ASYNC_NOOP 0 -#define ASYNC_WRITE 1 -#define ASYNC_SYNC 2 -#define ASYNC_TRUNCATE 3 -#define ASYNC_CLOSE 4 -#define ASYNC_DELETE 5 -#define ASYNC_OPENEXCLUSIVE 6 -#define ASYNC_UNLOCK 7 - -/* Names of opcodes. Used for debugging only. -** Make sure these stay in sync with the macros above! -*/ -static const char *azOpcodeName[] = { - "NOOP", "WRITE", "SYNC", "TRUNCATE", "CLOSE", "DELETE", "OPENEX", "UNLOCK" -}; - -/* -** Entries on the write-op queue are instances of the AsyncWrite -** structure, defined here. -** -** The interpretation of the iOffset and nByte variables varies depending -** on the value of AsyncWrite.op: -** -** ASYNC_NOOP: -** No values used. -** -** ASYNC_WRITE: -** iOffset -> Offset in file to write to. -** nByte -> Number of bytes of data to write (pointed to by zBuf). -** -** ASYNC_SYNC: -** nByte -> flags to pass to sqlite3OsSync(). -** -** ASYNC_TRUNCATE: -** iOffset -> Size to truncate file to. -** nByte -> Unused. -** -** ASYNC_CLOSE: -** iOffset -> Unused. -** nByte -> Unused. -** -** ASYNC_DELETE: -** iOffset -> Contains the "syncDir" flag. -** nByte -> Number of bytes of zBuf points to (file name). -** -** ASYNC_OPENEXCLUSIVE: -** iOffset -> Value of "delflag". -** nByte -> Number of bytes of zBuf points to (file name). -** -** ASYNC_UNLOCK: -** nByte -> Argument to sqlite3OsUnlock(). -** -** -** For an ASYNC_WRITE operation, zBuf points to the data to write to the file. -** This space is sqlite3_malloc()d along with the AsyncWrite structure in a -** single blob, so is deleted when sqlite3_free() is called on the parent -** structure. -*/ -struct AsyncWrite { - AsyncFileData *pFileData; /* File to write data to or sync */ - int op; /* One of ASYNC_xxx etc. */ - sqlite_int64 iOffset; /* See above */ - int nByte; /* See above */ - char *zBuf; /* Data to write to file (or NULL if op!=ASYNC_WRITE) */ - AsyncWrite *pNext; /* Next write operation (to any file) */ -}; - -/* -** An instance of this structure is created for each distinct open file -** (i.e. if two handles are opened on the one file, only one of these -** structures is allocated) and stored in the async.aLock hash table. The -** keys for async.aLock are the full pathnames of the opened files. -** -** AsyncLock.pList points to the head of a linked list of AsyncFileLock -** structures, one for each handle currently open on the file. -** -** If the opened file is not a main-database (the SQLITE_OPEN_MAIN_DB is -** not passed to the sqlite3OsOpen() call), or if async.bLockFiles is -** false, variables AsyncLock.pFile and AsyncLock.eLock are never used. -** Otherwise, pFile is a file handle opened on the file in question and -** used to obtain the file-system locks required by database connections -** within this process. -** -** See comments above the asyncLock() function for more details on -** the implementation of database locking used by this backend. -*/ -struct AsyncLock { - char *zFile; - int nFile; - sqlite3_file *pFile; - int eLock; - AsyncFileLock *pList; - AsyncLock *pNext; /* Next in linked list headed by async.pLock */ -}; - -/* -** An instance of the following structure is allocated along with each -** AsyncFileData structure (see AsyncFileData.lock), but is only used if the -** file was opened with the SQLITE_OPEN_MAIN_DB. -*/ -struct AsyncFileLock { - int eLock; /* Internally visible lock state (sqlite pov) */ - int eAsyncLock; /* Lock-state with write-queue unlock */ - AsyncFileLock *pNext; -}; - -/* -** The AsyncFile structure is a subclass of sqlite3_file used for -** asynchronous IO. -** -** All of the actual data for the structure is stored in the structure -** pointed to by AsyncFile.pData, which is allocated as part of the -** sqlite3OsOpen() using sqlite3_malloc(). The reason for this is that the -** lifetime of the AsyncFile structure is ended by the caller after OsClose() -** is called, but the data in AsyncFileData may be required by the -** writer thread after that point. -*/ -struct AsyncFile { - sqlite3_io_methods *pMethod; - AsyncFileData *pData; -}; -struct AsyncFileData { - char *zName; /* Underlying OS filename - used for debugging */ - int nName; /* Number of characters in zName */ - sqlite3_file *pBaseRead; /* Read handle to the underlying Os file */ - sqlite3_file *pBaseWrite; /* Write handle to the underlying Os file */ - AsyncFileLock lock; /* Lock state for this handle */ - AsyncLock *pLock; /* AsyncLock object for this file system entry */ - AsyncWrite closeOp; /* Preallocated close operation */ -}; - -/* -** Add an entry to the end of the global write-op list. pWrite should point -** to an AsyncWrite structure allocated using sqlite3_malloc(). The writer -** thread will call sqlite3_free() to free the structure after the specified -** operation has been completed. -** -** Once an AsyncWrite structure has been added to the list, it becomes the -** property of the writer thread and must not be read or modified by the -** caller. -*/ -static void addAsyncWrite(AsyncWrite *pWrite){ - /* We must hold the queue mutex in order to modify the queue pointers */ - if( pWrite->op!=ASYNC_UNLOCK ){ - async_mutex_enter(ASYNC_MUTEX_QUEUE); - } - - /* Add the record to the end of the write-op queue */ - assert( !pWrite->pNext ); - if( async.pQueueLast ){ - assert( async.pQueueFirst ); - async.pQueueLast->pNext = pWrite; - }else{ - async.pQueueFirst = pWrite; - } - async.pQueueLast = pWrite; - ASYNC_TRACE(("PUSH %p (%s %s %d)\n", pWrite, azOpcodeName[pWrite->op], - pWrite->pFileData ? pWrite->pFileData->zName : "-", pWrite->iOffset)); - - if( pWrite->op==ASYNC_CLOSE ){ - async.nFile--; - } - - /* The writer thread might have been idle because there was nothing - ** on the write-op queue for it to do. So wake it up. */ - async_cond_signal(ASYNC_COND_QUEUE); - - /* Drop the queue mutex */ - if( pWrite->op!=ASYNC_UNLOCK ){ - async_mutex_leave(ASYNC_MUTEX_QUEUE); - } -} - -/* -** Increment async.nFile in a thread-safe manner. -*/ -static void incrOpenFileCount(void){ - /* We must hold the queue mutex in order to modify async.nFile */ - async_mutex_enter(ASYNC_MUTEX_QUEUE); - if( async.nFile==0 ){ - async.ioError = SQLITE_OK; - } - async.nFile++; - async_mutex_leave(ASYNC_MUTEX_QUEUE); -} - -/* -** This is a utility function to allocate and populate a new AsyncWrite -** structure and insert it (via addAsyncWrite() ) into the global list. -*/ -static int addNewAsyncWrite( - AsyncFileData *pFileData, - int op, - sqlite3_int64 iOffset, - int nByte, - const char *zByte -){ - AsyncWrite *p; - if( op!=ASYNC_CLOSE && async.ioError ){ - return async.ioError; - } - p = sqlite3_malloc(sizeof(AsyncWrite) + (zByte?nByte:0)); - if( !p ){ - /* The upper layer does not expect operations like OsWrite() to - ** return SQLITE_NOMEM. This is partly because under normal conditions - ** SQLite is required to do rollback without calling malloc(). So - ** if malloc() fails here, treat it as an I/O error. The above - ** layer knows how to handle that. - */ - return SQLITE_IOERR; - } - p->op = op; - p->iOffset = iOffset; - p->nByte = nByte; - p->pFileData = pFileData; - p->pNext = 0; - if( zByte ){ - p->zBuf = (char *)&p[1]; - memcpy(p->zBuf, zByte, nByte); - }else{ - p->zBuf = 0; - } - addAsyncWrite(p); - return SQLITE_OK; -} - -/* -** Close the file. This just adds an entry to the write-op list, the file is -** not actually closed. -*/ -static int asyncClose(sqlite3_file *pFile){ - AsyncFileData *p = ((AsyncFile *)pFile)->pData; - - /* Unlock the file, if it is locked */ - async_mutex_enter(ASYNC_MUTEX_LOCK); - p->lock.eLock = 0; - async_mutex_leave(ASYNC_MUTEX_LOCK); - - addAsyncWrite(&p->closeOp); - return SQLITE_OK; -} - -/* -** Implementation of sqlite3OsWrite() for asynchronous files. Instead of -** writing to the underlying file, this function adds an entry to the end of -** the global AsyncWrite list. Either SQLITE_OK or SQLITE_NOMEM may be -** returned. -*/ -static int asyncWrite( - sqlite3_file *pFile, - const void *pBuf, - int amt, - sqlite3_int64 iOff -){ - AsyncFileData *p = ((AsyncFile *)pFile)->pData; - return addNewAsyncWrite(p, ASYNC_WRITE, iOff, amt, pBuf); -} - -/* -** Read data from the file. First we read from the filesystem, then adjust -** the contents of the buffer based on ASYNC_WRITE operations in the -** write-op queue. -** -** This method holds the mutex from start to finish. -*/ -static int asyncRead( - sqlite3_file *pFile, - void *zOut, - int iAmt, - sqlite3_int64 iOffset -){ - AsyncFileData *p = ((AsyncFile *)pFile)->pData; - int rc = SQLITE_OK; - sqlite3_int64 filesize = 0; - sqlite3_file *pBase = p->pBaseRead; - sqlite3_int64 iAmt64 = (sqlite3_int64)iAmt; - - /* Grab the write queue mutex for the duration of the call */ - async_mutex_enter(ASYNC_MUTEX_QUEUE); - - /* If an I/O error has previously occurred in this virtual file - ** system, then all subsequent operations fail. - */ - if( async.ioError!=SQLITE_OK ){ - rc = async.ioError; - goto asyncread_out; - } - - if( pBase->pMethods ){ - sqlite3_int64 nRead; - rc = pBase->pMethods->xFileSize(pBase, &filesize); - if( rc!=SQLITE_OK ){ - goto asyncread_out; - } - nRead = MIN(filesize - iOffset, iAmt64); - if( nRead>0 ){ - rc = pBase->pMethods->xRead(pBase, zOut, (int)nRead, iOffset); - ASYNC_TRACE(("READ %s %d bytes at %d\n", p->zName, nRead, iOffset)); - } - } - - if( rc==SQLITE_OK ){ - AsyncWrite *pWrite; - char *zName = p->zName; - - for(pWrite=async.pQueueFirst; pWrite; pWrite = pWrite->pNext){ - if( pWrite->op==ASYNC_WRITE && ( - (pWrite->pFileData==p) || - (zName && pWrite->pFileData->zName==zName) - )){ - sqlite3_int64 nCopy; - sqlite3_int64 nByte64 = (sqlite3_int64)pWrite->nByte; - - /* Set variable iBeginIn to the offset in buffer pWrite->zBuf[] from - ** which data should be copied. Set iBeginOut to the offset within - ** the output buffer to which data should be copied. If either of - ** these offsets is a negative number, set them to 0. - */ - sqlite3_int64 iBeginOut = (pWrite->iOffset-iOffset); - sqlite3_int64 iBeginIn = -iBeginOut; - if( iBeginIn<0 ) iBeginIn = 0; - if( iBeginOut<0 ) iBeginOut = 0; - - filesize = MAX(filesize, pWrite->iOffset+nByte64); - - nCopy = MIN(nByte64-iBeginIn, iAmt64-iBeginOut); - if( nCopy>0 ){ - memcpy(&((char *)zOut)[iBeginOut], &pWrite->zBuf[iBeginIn], (size_t)nCopy); - ASYNC_TRACE(("OVERREAD %d bytes at %d\n", nCopy, iBeginOut+iOffset)); - } - } - } - } - -asyncread_out: - async_mutex_leave(ASYNC_MUTEX_QUEUE); - if( rc==SQLITE_OK && filesize<(iOffset+iAmt) ){ - rc = SQLITE_IOERR_SHORT_READ; - } - return rc; -} - -/* -** Truncate the file to nByte bytes in length. This just adds an entry to -** the write-op list, no IO actually takes place. -*/ -static int asyncTruncate(sqlite3_file *pFile, sqlite3_int64 nByte){ - AsyncFileData *p = ((AsyncFile *)pFile)->pData; - return addNewAsyncWrite(p, ASYNC_TRUNCATE, nByte, 0, 0); -} - -/* -** Sync the file. This just adds an entry to the write-op list, the -** sync() is done later by sqlite3_async_flush(). -*/ -static int asyncSync(sqlite3_file *pFile, int flags){ - AsyncFileData *p = ((AsyncFile *)pFile)->pData; - return addNewAsyncWrite(p, ASYNC_SYNC, 0, flags, 0); -} - -/* -** Read the size of the file. First we read the size of the file system -** entry, then adjust for any ASYNC_WRITE or ASYNC_TRUNCATE operations -** currently in the write-op list. -** -** This method holds the mutex from start to finish. -*/ -int asyncFileSize(sqlite3_file *pFile, sqlite3_int64 *piSize){ - AsyncFileData *p = ((AsyncFile *)pFile)->pData; - int rc = SQLITE_OK; - sqlite3_int64 s = 0; - sqlite3_file *pBase; - - async_mutex_enter(ASYNC_MUTEX_QUEUE); - - /* Read the filesystem size from the base file. If pMethods is NULL, this - ** means the file hasn't been opened yet. In this case all relevant data - ** must be in the write-op queue anyway, so we can omit reading from the - ** file-system. - */ - pBase = p->pBaseRead; - if( pBase->pMethods ){ - rc = pBase->pMethods->xFileSize(pBase, &s); - } - - if( rc==SQLITE_OK ){ - AsyncWrite *pWrite; - for(pWrite=async.pQueueFirst; pWrite; pWrite = pWrite->pNext){ - if( pWrite->op==ASYNC_DELETE - && p->zName - && strcmp(p->zName, pWrite->zBuf)==0 - ){ - s = 0; - }else if( pWrite->pFileData && ( - (pWrite->pFileData==p) - || (p->zName && pWrite->pFileData->zName==p->zName) - )){ - switch( pWrite->op ){ - case ASYNC_WRITE: - s = MAX(pWrite->iOffset + (sqlite3_int64)(pWrite->nByte), s); - break; - case ASYNC_TRUNCATE: - s = MIN(s, pWrite->iOffset); - break; - } - } - } - *piSize = s; - } - async_mutex_leave(ASYNC_MUTEX_QUEUE); - return rc; -} - -/* -** Lock or unlock the actual file-system entry. -*/ -static int getFileLock(AsyncLock *pLock){ - int rc = SQLITE_OK; - AsyncFileLock *pIter; - int eRequired = 0; - - if( pLock->pFile ){ - for(pIter=pLock->pList; pIter; pIter=pIter->pNext){ - assert(pIter->eAsyncLock>=pIter->eLock); - if( pIter->eAsyncLock>eRequired ){ - eRequired = pIter->eAsyncLock; - assert(eRequired>=0 && eRequired<=SQLITE_LOCK_EXCLUSIVE); - } - } - - if( eRequired>pLock->eLock ){ - rc = pLock->pFile->pMethods->xLock(pLock->pFile, eRequired); - if( rc==SQLITE_OK ){ - pLock->eLock = eRequired; - } - } - else if( eRequiredeLock && eRequired<=SQLITE_LOCK_SHARED ){ - rc = pLock->pFile->pMethods->xUnlock(pLock->pFile, eRequired); - if( rc==SQLITE_OK ){ - pLock->eLock = eRequired; - } - } - } - - return rc; -} - -/* -** Return the AsyncLock structure from the global async.pLock list -** associated with the file-system entry identified by path zName -** (a string of nName bytes). If no such structure exists, return 0. -*/ -static AsyncLock *findLock(const char *zName, int nName){ - AsyncLock *p = async.pLock; - while( p && (p->nFile!=nName || memcmp(p->zFile, zName, nName)) ){ - p = p->pNext; - } - return p; -} - -/* -** The following two methods - asyncLock() and asyncUnlock() - are used -** to obtain and release locks on database files opened with the -** asynchronous backend. -*/ -static int asyncLock(sqlite3_file *pFile, int eLock){ - int rc = SQLITE_OK; - AsyncFileData *p = ((AsyncFile *)pFile)->pData; - - if( p->zName ){ - async_mutex_enter(ASYNC_MUTEX_LOCK); - if( p->lock.eLockpLock; - AsyncFileLock *pIter; - assert(pLock && pLock->pList); - for(pIter=pLock->pList; pIter; pIter=pIter->pNext){ - if( pIter!=&p->lock && ( - (eLock==SQLITE_LOCK_EXCLUSIVE && pIter->eLock>=SQLITE_LOCK_SHARED) || - (eLock==SQLITE_LOCK_PENDING && pIter->eLock>=SQLITE_LOCK_RESERVED) || - (eLock==SQLITE_LOCK_RESERVED && pIter->eLock>=SQLITE_LOCK_RESERVED) || - (eLock==SQLITE_LOCK_SHARED && pIter->eLock>=SQLITE_LOCK_PENDING) - )){ - rc = SQLITE_BUSY; - } - } - if( rc==SQLITE_OK ){ - p->lock.eLock = eLock; - p->lock.eAsyncLock = MAX(p->lock.eAsyncLock, eLock); - } - assert(p->lock.eAsyncLock>=p->lock.eLock); - if( rc==SQLITE_OK ){ - rc = getFileLock(pLock); - } - } - async_mutex_leave(ASYNC_MUTEX_LOCK); - } - - ASYNC_TRACE(("LOCK %d (%s) rc=%d\n", eLock, p->zName, rc)); - return rc; -} -static int asyncUnlock(sqlite3_file *pFile, int eLock){ - int rc = SQLITE_OK; - AsyncFileData *p = ((AsyncFile *)pFile)->pData; - if( p->zName ){ - AsyncFileLock *pLock = &p->lock; - async_mutex_enter(ASYNC_MUTEX_QUEUE); - async_mutex_enter(ASYNC_MUTEX_LOCK); - pLock->eLock = MIN(pLock->eLock, eLock); - rc = addNewAsyncWrite(p, ASYNC_UNLOCK, 0, eLock, 0); - async_mutex_leave(ASYNC_MUTEX_LOCK); - async_mutex_leave(ASYNC_MUTEX_QUEUE); - } - return rc; -} - -/* -** This function is called when the pager layer first opens a database file -** and is checking for a hot-journal. -*/ -static int asyncCheckReservedLock(sqlite3_file *pFile, int *pResOut){ - int ret = 0; - AsyncFileLock *pIter; - AsyncFileData *p = ((AsyncFile *)pFile)->pData; - - async_mutex_enter(ASYNC_MUTEX_LOCK); - for(pIter=p->pLock->pList; pIter; pIter=pIter->pNext){ - if( pIter->eLock>=SQLITE_LOCK_RESERVED ){ - ret = 1; - break; - } - } - async_mutex_leave(ASYNC_MUTEX_LOCK); - - ASYNC_TRACE(("CHECK-LOCK %d (%s)\n", ret, p->zName)); - *pResOut = ret; - return SQLITE_OK; -} - -/* -** sqlite3_file_control() implementation. -*/ -static int asyncFileControl(sqlite3_file *id, int op, void *pArg){ - switch( op ){ - case SQLITE_FCNTL_LOCKSTATE: { - async_mutex_enter(ASYNC_MUTEX_LOCK); - *(int*)pArg = ((AsyncFile*)id)->pData->lock.eLock; - async_mutex_leave(ASYNC_MUTEX_LOCK); - return SQLITE_OK; - } - } - return SQLITE_NOTFOUND; -} - -/* -** Return the device characteristics and sector-size of the device. It -** is tricky to implement these correctly, as this backend might -** not have an open file handle at this point. -*/ -static int asyncSectorSize(sqlite3_file *pFile){ - UNUSED_PARAMETER(pFile); - return 512; -} -static int asyncDeviceCharacteristics(sqlite3_file *pFile){ - UNUSED_PARAMETER(pFile); - return 0; -} - -static int unlinkAsyncFile(AsyncFileData *pData){ - AsyncFileLock **ppIter; - int rc = SQLITE_OK; - - if( pData->zName ){ - AsyncLock *pLock = pData->pLock; - for(ppIter=&pLock->pList; *ppIter; ppIter=&((*ppIter)->pNext)){ - if( (*ppIter)==&pData->lock ){ - *ppIter = pData->lock.pNext; - break; - } - } - if( !pLock->pList ){ - AsyncLock **pp; - if( pLock->pFile ){ - pLock->pFile->pMethods->xClose(pLock->pFile); - } - for(pp=&async.pLock; *pp!=pLock; pp=&((*pp)->pNext)); - *pp = pLock->pNext; - sqlite3_free(pLock); - }else{ - rc = getFileLock(pLock); - } - } - - return rc; -} - -/* -** The parameter passed to this function is a copy of a 'flags' parameter -** passed to this modules xOpen() method. This function returns true -** if the file should be opened asynchronously, or false if it should -** be opened immediately. -** -** If the file is to be opened asynchronously, then asyncOpen() will add -** an entry to the event queue and the file will not actually be opened -** until the event is processed. Otherwise, the file is opened directly -** by the caller. -*/ -static int doAsynchronousOpen(int flags){ - return (flags&SQLITE_OPEN_CREATE) && ( - (flags&SQLITE_OPEN_MAIN_JOURNAL) || - (flags&SQLITE_OPEN_TEMP_JOURNAL) || - (flags&SQLITE_OPEN_DELETEONCLOSE) - ); -} - -/* -** Open a file. -*/ -static int asyncOpen( - sqlite3_vfs *pAsyncVfs, - const char *zName, - sqlite3_file *pFile, - int flags, - int *pOutFlags -){ - static sqlite3_io_methods async_methods = { - 1, /* iVersion */ - asyncClose, /* xClose */ - asyncRead, /* xRead */ - asyncWrite, /* xWrite */ - asyncTruncate, /* xTruncate */ - asyncSync, /* xSync */ - asyncFileSize, /* xFileSize */ - asyncLock, /* xLock */ - asyncUnlock, /* xUnlock */ - asyncCheckReservedLock, /* xCheckReservedLock */ - asyncFileControl, /* xFileControl */ - asyncSectorSize, /* xSectorSize */ - asyncDeviceCharacteristics /* xDeviceCharacteristics */ - }; - - sqlite3_vfs *pVfs = (sqlite3_vfs *)pAsyncVfs->pAppData; - AsyncFile *p = (AsyncFile *)pFile; - int nName = 0; - int rc = SQLITE_OK; - int nByte; - AsyncFileData *pData; - AsyncLock *pLock = 0; - char *z; - int isAsyncOpen = doAsynchronousOpen(flags); - - /* If zName is NULL, then the upper layer is requesting an anonymous file. - ** Otherwise, allocate enough space to make a copy of the file name (along - ** with the second nul-terminator byte required by xOpen). - */ - if( zName ){ - nName = (int)strlen(zName); - } - - nByte = ( - sizeof(AsyncFileData) + /* AsyncFileData structure */ - 2 * pVfs->szOsFile + /* AsyncFileData.pBaseRead and pBaseWrite */ - nName + 2 /* AsyncFileData.zName */ - ); - z = sqlite3_malloc(nByte); - if( !z ){ - return SQLITE_NOMEM; - } - memset(z, 0, nByte); - pData = (AsyncFileData*)z; - z += sizeof(pData[0]); - pData->pBaseRead = (sqlite3_file*)z; - z += pVfs->szOsFile; - pData->pBaseWrite = (sqlite3_file*)z; - pData->closeOp.pFileData = pData; - pData->closeOp.op = ASYNC_CLOSE; - - if( zName ){ - z += pVfs->szOsFile; - pData->zName = z; - pData->nName = nName; - memcpy(pData->zName, zName, nName); - } - - if( !isAsyncOpen ){ - int flagsout; - rc = pVfs->xOpen(pVfs, pData->zName, pData->pBaseRead, flags, &flagsout); - if( rc==SQLITE_OK - && (flagsout&SQLITE_OPEN_READWRITE) - && (flags&SQLITE_OPEN_EXCLUSIVE)==0 - ){ - rc = pVfs->xOpen(pVfs, pData->zName, pData->pBaseWrite, flags, 0); - } - if( pOutFlags ){ - *pOutFlags = flagsout; - } - } - - async_mutex_enter(ASYNC_MUTEX_LOCK); - - if( zName && rc==SQLITE_OK ){ - pLock = findLock(pData->zName, pData->nName); - if( !pLock ){ - int nByte = pVfs->szOsFile + sizeof(AsyncLock) + pData->nName + 1; - pLock = (AsyncLock *)sqlite3_malloc(nByte); - if( pLock ){ - memset(pLock, 0, nByte); - if( async.bLockFiles && (flags&SQLITE_OPEN_MAIN_DB) ){ - pLock->pFile = (sqlite3_file *)&pLock[1]; - rc = pVfs->xOpen(pVfs, pData->zName, pLock->pFile, flags, 0); - if( rc!=SQLITE_OK ){ - sqlite3_free(pLock); - pLock = 0; - } - } - if( pLock ){ - pLock->nFile = pData->nName; - pLock->zFile = &((char *)(&pLock[1]))[pVfs->szOsFile]; - memcpy(pLock->zFile, pData->zName, pLock->nFile); - pLock->pNext = async.pLock; - async.pLock = pLock; - } - }else{ - rc = SQLITE_NOMEM; - } - } - } - - if( rc==SQLITE_OK ){ - p->pMethod = &async_methods; - p->pData = pData; - - /* Link AsyncFileData.lock into the linked list of - ** AsyncFileLock structures for this file. - */ - if( zName ){ - pData->lock.pNext = pLock->pList; - pLock->pList = &pData->lock; - pData->zName = pLock->zFile; - } - }else{ - if( pData->pBaseRead->pMethods ){ - pData->pBaseRead->pMethods->xClose(pData->pBaseRead); - } - if( pData->pBaseWrite->pMethods ){ - pData->pBaseWrite->pMethods->xClose(pData->pBaseWrite); - } - sqlite3_free(pData); - } - - async_mutex_leave(ASYNC_MUTEX_LOCK); - - if( rc==SQLITE_OK ){ - pData->pLock = pLock; - } - - if( rc==SQLITE_OK && isAsyncOpen ){ - rc = addNewAsyncWrite(pData, ASYNC_OPENEXCLUSIVE, (sqlite3_int64)flags,0,0); - if( rc==SQLITE_OK ){ - if( pOutFlags ) *pOutFlags = flags; - }else{ - async_mutex_enter(ASYNC_MUTEX_LOCK); - unlinkAsyncFile(pData); - async_mutex_leave(ASYNC_MUTEX_LOCK); - sqlite3_free(pData); - } - } - if( rc!=SQLITE_OK ){ - p->pMethod = 0; - }else{ - incrOpenFileCount(); - } - - return rc; -} - -/* -** Implementation of sqlite3OsDelete. Add an entry to the end of the -** write-op queue to perform the delete. -*/ -static int asyncDelete(sqlite3_vfs *pAsyncVfs, const char *z, int syncDir){ - UNUSED_PARAMETER(pAsyncVfs); - return addNewAsyncWrite(0, ASYNC_DELETE, syncDir, (int)strlen(z)+1, z); -} - -/* -** Implementation of sqlite3OsAccess. This method holds the mutex from -** start to finish. -*/ -static int asyncAccess( - sqlite3_vfs *pAsyncVfs, - const char *zName, - int flags, - int *pResOut -){ - int rc; - int ret; - AsyncWrite *p; - sqlite3_vfs *pVfs = (sqlite3_vfs *)pAsyncVfs->pAppData; - - assert(flags==SQLITE_ACCESS_READWRITE - || flags==SQLITE_ACCESS_READ - || flags==SQLITE_ACCESS_EXISTS - ); - - async_mutex_enter(ASYNC_MUTEX_QUEUE); - rc = pVfs->xAccess(pVfs, zName, flags, &ret); - if( rc==SQLITE_OK && flags==SQLITE_ACCESS_EXISTS ){ - for(p=async.pQueueFirst; p; p = p->pNext){ - if( p->op==ASYNC_DELETE && 0==strcmp(p->zBuf, zName) ){ - ret = 0; - }else if( p->op==ASYNC_OPENEXCLUSIVE - && p->pFileData->zName - && 0==strcmp(p->pFileData->zName, zName) - ){ - ret = 1; - } - } - } - ASYNC_TRACE(("ACCESS(%s): %s = %d\n", - flags==SQLITE_ACCESS_READWRITE?"read-write": - flags==SQLITE_ACCESS_READ?"read":"exists" - , zName, ret) - ); - async_mutex_leave(ASYNC_MUTEX_QUEUE); - *pResOut = ret; - return rc; -} - -/* -** Fill in zPathOut with the full path to the file identified by zPath. -*/ -static int asyncFullPathname( - sqlite3_vfs *pAsyncVfs, - const char *zPath, - int nPathOut, - char *zPathOut -){ - int rc; - sqlite3_vfs *pVfs = (sqlite3_vfs *)pAsyncVfs->pAppData; - rc = pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut); - - /* Because of the way intra-process file locking works, this backend - ** needs to return a canonical path. The following block assumes the - ** file-system uses unix style paths. - */ - if( rc==SQLITE_OK ){ - int i, j; - char *z = zPathOut; - int n = (int)strlen(z); - while( n>1 && z[n-1]=='/' ){ n--; } - for(i=j=0; i0 && z[j-1]!='/' ){ j--; } - if( j>0 ){ j--; } - i += 2; - continue; - } - } - z[j++] = z[i]; - } - z[j] = 0; - } - - return rc; -} -static void *asyncDlOpen(sqlite3_vfs *pAsyncVfs, const char *zPath){ - sqlite3_vfs *pVfs = (sqlite3_vfs *)pAsyncVfs->pAppData; - return pVfs->xDlOpen(pVfs, zPath); -} -static void asyncDlError(sqlite3_vfs *pAsyncVfs, int nByte, char *zErrMsg){ - sqlite3_vfs *pVfs = (sqlite3_vfs *)pAsyncVfs->pAppData; - pVfs->xDlError(pVfs, nByte, zErrMsg); -} -static void (*asyncDlSym( - sqlite3_vfs *pAsyncVfs, - void *pHandle, - const char *zSymbol -))(void){ - sqlite3_vfs *pVfs = (sqlite3_vfs *)pAsyncVfs->pAppData; - return pVfs->xDlSym(pVfs, pHandle, zSymbol); -} -static void asyncDlClose(sqlite3_vfs *pAsyncVfs, void *pHandle){ - sqlite3_vfs *pVfs = (sqlite3_vfs *)pAsyncVfs->pAppData; - pVfs->xDlClose(pVfs, pHandle); -} -static int asyncRandomness(sqlite3_vfs *pAsyncVfs, int nByte, char *zBufOut){ - sqlite3_vfs *pVfs = (sqlite3_vfs *)pAsyncVfs->pAppData; - return pVfs->xRandomness(pVfs, nByte, zBufOut); -} -static int asyncSleep(sqlite3_vfs *pAsyncVfs, int nMicro){ - sqlite3_vfs *pVfs = (sqlite3_vfs *)pAsyncVfs->pAppData; - return pVfs->xSleep(pVfs, nMicro); -} -static int asyncCurrentTime(sqlite3_vfs *pAsyncVfs, double *pTimeOut){ - sqlite3_vfs *pVfs = (sqlite3_vfs *)pAsyncVfs->pAppData; - return pVfs->xCurrentTime(pVfs, pTimeOut); -} - -static sqlite3_vfs async_vfs = { - 1, /* iVersion */ - sizeof(AsyncFile), /* szOsFile */ - 0, /* mxPathname */ - 0, /* pNext */ - SQLITEASYNC_VFSNAME, /* zName */ - 0, /* pAppData */ - asyncOpen, /* xOpen */ - asyncDelete, /* xDelete */ - asyncAccess, /* xAccess */ - asyncFullPathname, /* xFullPathname */ - asyncDlOpen, /* xDlOpen */ - asyncDlError, /* xDlError */ - asyncDlSym, /* xDlSym */ - asyncDlClose, /* xDlClose */ - asyncRandomness, /* xDlError */ - asyncSleep, /* xDlSym */ - asyncCurrentTime /* xDlClose */ -}; - -/* -** This procedure runs in a separate thread, reading messages off of the -** write queue and processing them one by one. -** -** If async.writerHaltNow is true, then this procedure exits -** after processing a single message. -** -** If async.writerHaltWhenIdle is true, then this procedure exits when -** the write queue is empty. -** -** If both of the above variables are false, this procedure runs -** indefinately, waiting for operations to be added to the write queue -** and processing them in the order in which they arrive. -** -** An artifical delay of async.ioDelay milliseconds is inserted before -** each write operation in order to simulate the effect of a slow disk. -** -** Only one instance of this procedure may be running at a time. -*/ -static void asyncWriterThread(void){ - sqlite3_vfs *pVfs = (sqlite3_vfs *)(async_vfs.pAppData); - AsyncWrite *p = 0; - int rc = SQLITE_OK; - int holdingMutex = 0; - - async_mutex_enter(ASYNC_MUTEX_WRITER); - - while( async.eHalt!=SQLITEASYNC_HALT_NOW ){ - int doNotFree = 0; - sqlite3_file *pBase = 0; - - if( !holdingMutex ){ - async_mutex_enter(ASYNC_MUTEX_QUEUE); - } - while( (p = async.pQueueFirst)==0 ){ - if( async.eHalt!=SQLITEASYNC_HALT_NEVER ){ - async_mutex_leave(ASYNC_MUTEX_QUEUE); - break; - }else{ - ASYNC_TRACE(("IDLE\n")); - async_cond_wait(ASYNC_COND_QUEUE, ASYNC_MUTEX_QUEUE); - ASYNC_TRACE(("WAKEUP\n")); - } - } - if( p==0 ) break; - holdingMutex = 1; - - /* Right now this thread is holding the mutex on the write-op queue. - ** Variable 'p' points to the first entry in the write-op queue. In - ** the general case, we hold on to the mutex for the entire body of - ** the loop. - ** - ** However in the cases enumerated below, we relinquish the mutex, - ** perform the IO, and then re-request the mutex before removing 'p' from - ** the head of the write-op queue. The idea is to increase concurrency with - ** sqlite threads. - ** - ** * An ASYNC_CLOSE operation. - ** * An ASYNC_OPENEXCLUSIVE operation. For this one, we relinquish - ** the mutex, call the underlying xOpenExclusive() function, then - ** re-aquire the mutex before seting the AsyncFile.pBaseRead - ** variable. - ** * ASYNC_SYNC and ASYNC_WRITE operations, if - ** SQLITE_ASYNC_TWO_FILEHANDLES was set at compile time and two - ** file-handles are open for the particular file being "synced". - */ - if( async.ioError!=SQLITE_OK && p->op!=ASYNC_CLOSE ){ - p->op = ASYNC_NOOP; - } - if( p->pFileData ){ - pBase = p->pFileData->pBaseWrite; - if( - p->op==ASYNC_CLOSE || - p->op==ASYNC_OPENEXCLUSIVE || - (pBase->pMethods && (p->op==ASYNC_SYNC || p->op==ASYNC_WRITE) ) - ){ - async_mutex_leave(ASYNC_MUTEX_QUEUE); - holdingMutex = 0; - } - if( !pBase->pMethods ){ - pBase = p->pFileData->pBaseRead; - } - } - - switch( p->op ){ - case ASYNC_NOOP: - break; - - case ASYNC_WRITE: - assert( pBase ); - ASYNC_TRACE(("WRITE %s %d bytes at %d\n", - p->pFileData->zName, p->nByte, p->iOffset)); - rc = pBase->pMethods->xWrite(pBase, (void *)(p->zBuf), p->nByte, p->iOffset); - break; - - case ASYNC_SYNC: - assert( pBase ); - ASYNC_TRACE(("SYNC %s\n", p->pFileData->zName)); - rc = pBase->pMethods->xSync(pBase, p->nByte); - break; - - case ASYNC_TRUNCATE: - assert( pBase ); - ASYNC_TRACE(("TRUNCATE %s to %d bytes\n", - p->pFileData->zName, p->iOffset)); - rc = pBase->pMethods->xTruncate(pBase, p->iOffset); - break; - - case ASYNC_CLOSE: { - AsyncFileData *pData = p->pFileData; - ASYNC_TRACE(("CLOSE %s\n", p->pFileData->zName)); - if( pData->pBaseWrite->pMethods ){ - pData->pBaseWrite->pMethods->xClose(pData->pBaseWrite); - } - if( pData->pBaseRead->pMethods ){ - pData->pBaseRead->pMethods->xClose(pData->pBaseRead); - } - - /* Unlink AsyncFileData.lock from the linked list of AsyncFileLock - ** structures for this file. Obtain the async.lockMutex mutex - ** before doing so. - */ - async_mutex_enter(ASYNC_MUTEX_LOCK); - rc = unlinkAsyncFile(pData); - async_mutex_leave(ASYNC_MUTEX_LOCK); - - if( !holdingMutex ){ - async_mutex_enter(ASYNC_MUTEX_QUEUE); - holdingMutex = 1; - } - assert_mutex_is_held(ASYNC_MUTEX_QUEUE); - async.pQueueFirst = p->pNext; - sqlite3_free(pData); - doNotFree = 1; - break; - } - - case ASYNC_UNLOCK: { - AsyncWrite *pIter; - AsyncFileData *pData = p->pFileData; - int eLock = p->nByte; - - /* When a file is locked by SQLite using the async backend, it is - ** locked within the 'real' file-system synchronously. When it is - ** unlocked, an ASYNC_UNLOCK event is added to the write-queue to - ** unlock the file asynchronously. The design of the async backend - ** requires that the 'real' file-system file be locked from the - ** time that SQLite first locks it (and probably reads from it) - ** until all asynchronous write events that were scheduled before - ** SQLite unlocked the file have been processed. - ** - ** This is more complex if SQLite locks and unlocks the file multiple - ** times in quick succession. For example, if SQLite does: - ** - ** lock, write, unlock, lock, write, unlock - ** - ** Each "lock" operation locks the file immediately. Each "write" - ** and "unlock" operation adds an event to the event queue. If the - ** second "lock" operation is performed before the first "unlock" - ** operation has been processed asynchronously, then the first - ** "unlock" cannot be safely processed as is, since this would mean - ** the file was unlocked when the second "write" operation is - ** processed. To work around this, when processing an ASYNC_UNLOCK - ** operation, SQLite: - ** - ** 1) Unlocks the file to the minimum of the argument passed to - ** the xUnlock() call and the current lock from SQLite's point - ** of view, and - ** - ** 2) Only unlocks the file at all if this event is the last - ** ASYNC_UNLOCK event on this file in the write-queue. - */ - assert( holdingMutex==1 ); - assert( async.pQueueFirst==p ); - for(pIter=async.pQueueFirst->pNext; pIter; pIter=pIter->pNext){ - if( pIter->pFileData==pData && pIter->op==ASYNC_UNLOCK ) break; - } - if( !pIter ){ - async_mutex_enter(ASYNC_MUTEX_LOCK); - pData->lock.eAsyncLock = MIN( - pData->lock.eAsyncLock, MAX(pData->lock.eLock, eLock) - ); - assert(pData->lock.eAsyncLock>=pData->lock.eLock); - rc = getFileLock(pData->pLock); - async_mutex_leave(ASYNC_MUTEX_LOCK); - } - break; - } - - case ASYNC_DELETE: - ASYNC_TRACE(("DELETE %s\n", p->zBuf)); - rc = pVfs->xDelete(pVfs, p->zBuf, (int)p->iOffset); - if( rc==SQLITE_IOERR_DELETE_NOENT ) rc = SQLITE_OK; - break; - - case ASYNC_OPENEXCLUSIVE: { - int flags = (int)p->iOffset; - AsyncFileData *pData = p->pFileData; - ASYNC_TRACE(("OPEN %s flags=%d\n", p->zBuf, (int)p->iOffset)); - assert(pData->pBaseRead->pMethods==0 && pData->pBaseWrite->pMethods==0); - rc = pVfs->xOpen(pVfs, pData->zName, pData->pBaseRead, flags, 0); - assert( holdingMutex==0 ); - async_mutex_enter(ASYNC_MUTEX_QUEUE); - holdingMutex = 1; - break; - } - - default: assert(!"Illegal value for AsyncWrite.op"); - } - - /* If we didn't hang on to the mutex during the IO op, obtain it now - ** so that the AsyncWrite structure can be safely removed from the - ** global write-op queue. - */ - if( !holdingMutex ){ - async_mutex_enter(ASYNC_MUTEX_QUEUE); - holdingMutex = 1; - } - /* ASYNC_TRACE(("UNLINK %p\n", p)); */ - if( p==async.pQueueLast ){ - async.pQueueLast = 0; - } - if( !doNotFree ){ - assert_mutex_is_held(ASYNC_MUTEX_QUEUE); - async.pQueueFirst = p->pNext; - sqlite3_free(p); - } - assert( holdingMutex ); - - /* An IO error has occurred. We cannot report the error back to the - ** connection that requested the I/O since the error happened - ** asynchronously. The connection has already moved on. There - ** really is nobody to report the error to. - ** - ** The file for which the error occurred may have been a database or - ** journal file. Regardless, none of the currently queued operations - ** associated with the same database should now be performed. Nor should - ** any subsequently requested IO on either a database or journal file - ** handle for the same database be accepted until the main database - ** file handle has been closed and reopened. - ** - ** Furthermore, no further IO should be queued or performed on any file - ** handle associated with a database that may have been part of a - ** multi-file transaction that included the database associated with - ** the IO error (i.e. a database ATTACHed to the same handle at some - ** point in time). - */ - if( rc!=SQLITE_OK ){ - async.ioError = rc; - } - - if( async.ioError && !async.pQueueFirst ){ - async_mutex_enter(ASYNC_MUTEX_LOCK); - if( 0==async.pLock ){ - async.ioError = SQLITE_OK; - } - async_mutex_leave(ASYNC_MUTEX_LOCK); - } - - /* Drop the queue mutex before continuing to the next write operation - ** in order to give other threads a chance to work with the write queue. - */ - if( !async.pQueueFirst || !async.ioError ){ - async_mutex_leave(ASYNC_MUTEX_QUEUE); - holdingMutex = 0; - if( async.ioDelay>0 ){ - pVfs->xSleep(pVfs, async.ioDelay*1000); - }else{ - async_sched_yield(); - } - } - } - - async_mutex_leave(ASYNC_MUTEX_WRITER); - return; -} - -/* -** Install the asynchronous VFS. -*/ -int sqlite3async_initialize(const char *zParent, int isDefault){ - int rc = SQLITE_OK; - if( async_vfs.pAppData==0 ){ - sqlite3_vfs *pParent = sqlite3_vfs_find(zParent); - if( !pParent || async_os_initialize() ){ - rc = SQLITE_ERROR; - }else if( SQLITE_OK!=(rc = sqlite3_vfs_register(&async_vfs, isDefault)) ){ - async_os_shutdown(); - }else{ - async_vfs.pAppData = (void *)pParent; - async_vfs.mxPathname = ((sqlite3_vfs *)async_vfs.pAppData)->mxPathname; - } - } - return rc; -} - -/* -** Uninstall the asynchronous VFS. -*/ -void sqlite3async_shutdown(void){ - if( async_vfs.pAppData ){ - async_os_shutdown(); - sqlite3_vfs_unregister((sqlite3_vfs *)&async_vfs); - async_vfs.pAppData = 0; - } -} - -/* -** Process events on the write-queue. -*/ -void sqlite3async_run(void){ - asyncWriterThread(); -} - -/* -** Control/configure the asynchronous IO system. -*/ -int sqlite3async_control(int op, ...){ - int rc = SQLITE_OK; - va_list ap; - va_start(ap, op); - switch( op ){ - case SQLITEASYNC_HALT: { - int eWhen = va_arg(ap, int); - if( eWhen!=SQLITEASYNC_HALT_NEVER - && eWhen!=SQLITEASYNC_HALT_NOW - && eWhen!=SQLITEASYNC_HALT_IDLE - ){ - rc = SQLITE_MISUSE; - break; - } - async.eHalt = eWhen; - async_mutex_enter(ASYNC_MUTEX_QUEUE); - async_cond_signal(ASYNC_COND_QUEUE); - async_mutex_leave(ASYNC_MUTEX_QUEUE); - break; - } - - case SQLITEASYNC_DELAY: { - int iDelay = va_arg(ap, int); - if( iDelay<0 ){ - rc = SQLITE_MISUSE; - break; - } - async.ioDelay = iDelay; - break; - } - - case SQLITEASYNC_LOCKFILES: { - int bLock = va_arg(ap, int); - async_mutex_enter(ASYNC_MUTEX_QUEUE); - if( async.nFile || async.pQueueFirst ){ - async_mutex_leave(ASYNC_MUTEX_QUEUE); - rc = SQLITE_MISUSE; - break; - } - async.bLockFiles = bLock; - async_mutex_leave(ASYNC_MUTEX_QUEUE); - break; - } - - case SQLITEASYNC_GET_HALT: { - int *peWhen = va_arg(ap, int *); - *peWhen = async.eHalt; - break; - } - case SQLITEASYNC_GET_DELAY: { - int *piDelay = va_arg(ap, int *); - *piDelay = async.ioDelay; - break; - } - case SQLITEASYNC_GET_LOCKFILES: { - int *piDelay = va_arg(ap, int *); - *piDelay = async.bLockFiles; - break; - } - - default: - rc = SQLITE_ERROR; - break; - } - va_end(ap); - return rc; -} - -#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ASYNCIO) */ - diff --git a/ext/async/sqlite3async.h b/ext/async/sqlite3async.h deleted file mode 100644 index 5b20d7189a..0000000000 --- a/ext/async/sqlite3async.h +++ /dev/null @@ -1,223 +0,0 @@ - -#ifndef __SQLITEASYNC_H_ -#define __SQLITEASYNC_H_ 1 - -/* -** Make sure we can call this stuff from C++. -*/ -#ifdef __cplusplus -extern "C" { -#endif - -#define SQLITEASYNC_VFSNAME "sqlite3async" - -/* -** THREAD SAFETY NOTES: -** -** Of the four API functions in this file, the following are not threadsafe: -** -** sqlite3async_initialize() -** sqlite3async_shutdown() -** -** Care must be taken that neither of these functions is called while -** another thread may be calling either any sqlite3async_XXX() function -** or an sqlite3_XXX() API function related to a database handle that -** is using the asynchronous IO VFS. -** -** These functions: -** -** sqlite3async_run() -** sqlite3async_control() -** -** are threadsafe. It is quite safe to call either of these functions even -** if another thread may also be calling one of them or an sqlite3_XXX() -** function related to a database handle that uses the asynchronous IO VFS. -*/ - -/* -** Initialize the asynchronous IO VFS and register it with SQLite using -** sqlite3_vfs_register(). If the asynchronous VFS is already initialized -** and registered, this function is a no-op. The asynchronous IO VFS -** is registered as "sqlite3async". -** -** The asynchronous IO VFS does not make operating system IO requests -** directly. Instead, it uses an existing VFS implementation for all -** required file-system operations. If the first parameter to this function -** is NULL, then the current default VFS is used for IO. If it is not -** NULL, then it must be the name of an existing VFS. In other words, the -** first argument to this function is passed to sqlite3_vfs_find() to -** locate the VFS to use for all real IO operations. This VFS is known -** as the "parent VFS". -** -** If the second parameter to this function is non-zero, then the -** asynchronous IO VFS is registered as the default VFS for all SQLite -** database connections within the process. Otherwise, the asynchronous IO -** VFS is only used by connections opened using sqlite3_open_v2() that -** specifically request VFS "sqlite3async". -** -** If a parent VFS cannot be located, then SQLITE_ERROR is returned. -** In the unlikely event that operating system specific initialization -** fails (win32 systems create the required critical section and event -** objects within this function), then SQLITE_ERROR is also returned. -** Finally, if the call to sqlite3_vfs_register() returns an error, then -** the error code is returned to the user by this function. In all three -** of these cases, intialization has failed and the asynchronous IO VFS -** is not registered with SQLite. -** -** Otherwise, if no error occurs, SQLITE_OK is returned. -*/ -int sqlite3async_initialize(const char *zParent, int isDefault); - -/* -** This function unregisters the asynchronous IO VFS using -** sqlite3_vfs_unregister(). -** -** On win32 platforms, this function also releases the small number of -** critical section and event objects created by sqlite3async_initialize(). -*/ -void sqlite3async_shutdown(void); - -/* -** This function may only be called when the asynchronous IO VFS is -** installed (after a call to sqlite3async_initialize()). It processes -** zero or more queued write operations before returning. It is expected -** (but not required) that this function will be called by a different -** thread than those threads that use SQLite. The "background thread" -** that performs IO. -** -** How many queued write operations are performed before returning -** depends on the global setting configured by passing the SQLITEASYNC_HALT -** verb to sqlite3async_control() (see below for details). By default -** this function never returns - it processes all pending operations and -** then blocks waiting for new ones. -** -** If multiple simultaneous calls are made to sqlite3async_run() from two -** or more threads, then the calls are serialized internally. -*/ -void sqlite3async_run(void); - -/* -** This function may only be called when the asynchronous IO VFS is -** installed (after a call to sqlite3async_initialize()). It is used -** to query or configure various parameters that affect the operation -** of the asynchronous IO VFS. At present there are three parameters -** supported: -** -** * The "halt" parameter, which configures the circumstances under -** which the sqlite3async_run() parameter is configured. -** -** * The "delay" parameter. Setting the delay parameter to a non-zero -** value causes the sqlite3async_run() function to sleep for the -** configured number of milliseconds between each queued write -** operation. -** -** * The "lockfiles" parameter. This parameter determines whether or -** not the asynchronous IO VFS locks the database files it operates -** on. Disabling file locking can improve throughput. -** -** This function is always passed two arguments. When setting the value -** of a parameter, the first argument must be one of SQLITEASYNC_HALT, -** SQLITEASYNC_DELAY or SQLITEASYNC_LOCKFILES. The second argument must -** be passed the new value for the parameter as type "int". -** -** When querying the current value of a paramter, the first argument must -** be one of SQLITEASYNC_GET_HALT, GET_DELAY or GET_LOCKFILES. The second -** argument to this function must be of type (int *). The current value -** of the queried parameter is copied to the memory pointed to by the -** second argument. For example: -** -** int eCurrentHalt; -** int eNewHalt = SQLITEASYNC_HALT_IDLE; -** -** sqlite3async_control(SQLITEASYNC_HALT, eNewHalt); -** sqlite3async_control(SQLITEASYNC_GET_HALT, &eCurrentHalt); -** assert( eNewHalt==eCurrentHalt ); -** -** See below for more detail on each configuration parameter. -** -** SQLITEASYNC_HALT: -** -** This is used to set the value of the "halt" parameter. The second -** argument must be one of the SQLITEASYNC_HALT_XXX symbols defined -** below (either NEVER, IDLE and NOW). -** -** If the parameter is set to NEVER, then calls to sqlite3async_run() -** never return. This is the default setting. If the parameter is set -** to IDLE, then calls to sqlite3async_run() return as soon as the -** queue of pending write operations is empty. If the parameter is set -** to NOW, then calls to sqlite3async_run() return as quickly as -** possible, without processing any pending write requests. -** -** If an attempt is made to set this parameter to an integer value other -** than SQLITEASYNC_HALT_NEVER, IDLE or NOW, then sqlite3async_control() -** returns SQLITE_MISUSE and the current value of the parameter is not -** modified. -** -** Modifying the "halt" parameter affects calls to sqlite3async_run() -** made by other threads that are currently in progress. -** -** SQLITEASYNC_DELAY: -** -** This is used to set the value of the "delay" parameter. If set to -** a non-zero value, then after completing a pending write request, the -** sqlite3async_run() function sleeps for the configured number of -** milliseconds. -** -** If an attempt is made to set this parameter to a negative value, -** sqlite3async_control() returns SQLITE_MISUSE and the current value -** of the parameter is not modified. -** -** Modifying the "delay" parameter affects calls to sqlite3async_run() -** made by other threads that are currently in progress. -** -** SQLITEASYNC_LOCKFILES: -** -** This is used to set the value of the "lockfiles" parameter. This -** parameter must be set to either 0 or 1. If set to 1, then the -** asynchronous IO VFS uses the xLock() and xUnlock() methods of the -** parent VFS to lock database files being read and/or written. If -** the parameter is set to 0, then these locks are omitted. -** -** This parameter may only be set when there are no open database -** connections using the VFS and the queue of pending write requests -** is empty. Attempting to set it when this is not true, or to set it -** to a value other than 0 or 1 causes sqlite3async_control() to return -** SQLITE_MISUSE and the value of the parameter to remain unchanged. -** -** If this parameter is set to zero, then it is only safe to access the -** database via the asynchronous IO VFS from within a single process. If -** while writing to the database via the asynchronous IO VFS the database -** is also read or written from within another process, or via another -** connection that does not use the asynchronous IO VFS within the same -** process, the results are undefined (and may include crashes or database -** corruption). -** -** Alternatively, if this parameter is set to 1, then it is safe to access -** the database from multiple connections within multiple processes using -** either the asynchronous IO VFS or the parent VFS directly. -*/ -int sqlite3async_control(int op, ...); - -/* -** Values that can be used as the first argument to sqlite3async_control(). -*/ -#define SQLITEASYNC_HALT 1 -#define SQLITEASYNC_GET_HALT 2 -#define SQLITEASYNC_DELAY 3 -#define SQLITEASYNC_GET_DELAY 4 -#define SQLITEASYNC_LOCKFILES 5 -#define SQLITEASYNC_GET_LOCKFILES 6 - -/* -** If the first argument to sqlite3async_control() is SQLITEASYNC_HALT, -** the second argument should be one of the following. -*/ -#define SQLITEASYNC_HALT_NEVER 0 /* Never halt (default value) */ -#define SQLITEASYNC_HALT_NOW 1 /* Halt as soon as possible */ -#define SQLITEASYNC_HALT_IDLE 2 /* Halt when write-queue is empty */ - -#ifdef __cplusplus -} /* End of the 'extern "C"' block */ -#endif -#endif /* ifndef __SQLITEASYNC_H_ */ - diff --git a/ext/expert/README.md b/ext/expert/README.md new file mode 100644 index 0000000000..28886fd1f2 --- /dev/null +++ b/ext/expert/README.md @@ -0,0 +1,83 @@ +## SQLite Expert Extension + +This folder contains code for a simple system to propose useful indexes +given a database and a set of SQL queries. It works as follows: + + 1. The user database schema is copied to a temporary database. + + 1. All SQL queries are prepared against the temporary database. + Information regarding the WHERE and ORDER BY clauses, and other query + features that affect index selection are recorded. + + 1. The information gathered in step 2 is used to create candidate + indexes - indexes that the planner might have made use of in the previous + step, had they been available. + + 1. A subset of the data in the user database is used to generate statistics + for all existing indexes and the candidate indexes generated in step 3 + above. + + 1. The SQL queries are prepared a second time. If the planner uses any + of the indexes created in step 3, they are recommended to the user. + +# C API + +The SQLite expert C API is defined in sqlite3expert.h. Most uses will proceed +as follows: + + 1. An sqlite3expert object is created by calling **sqlite3\_expert\_new()**. + A database handle opened by the user is passed as an argument. + + 1. The sqlite3expert object is configured with one or more SQL statements + by making one or more calls to **sqlite3\_expert\_sql()**. Each call may + specify a single SQL statement, or multiple statements separated by + semi-colons. + + 1. Optionally, the **sqlite3\_expert\_config()** API may be used to + configure the size of the data subset used to generate index statistics. + Using a smaller subset of the data can speed up the analysis. + + 1. **sqlite3\_expert\_analyze()** is called to run the analysis. + + 1. One or more calls are made to **sqlite3\_expert\_report()** to extract + components of the results of the analysis. + + 1. **sqlite3\_expert\_destroy()** is called to free all resources. + +Refer to comments in sqlite3expert.h for further details. + +# sqlite3_expert application + +The file "expert.c" contains the code for a command line application that +uses the API described above. It can be compiled with (for example): + +

+  gcc -O2 sqlite3.c expert.c sqlite3expert.c -o sqlite3_expert
+
+ +Assuming the database is named "test.db", it can then be run to analyze a +single query: + +
+  ./sqlite3_expert -sql <sql-query> test.db
+
+ +Or an entire text file worth of queries with: + +
+  ./sqlite3_expert -file <text-file> test.db
+
+ +By default, sqlite3\_expert generates index statistics using all the data in +the user database. For a large database, this may be prohibitively time +consuming. The "-sample" option may be used to configure sqlite3\_expert to +generate statistics based on an integer percentage of the user database as +follows: + +
+  # Generate statistics based on 25% of the user database rows:
+  ./sqlite3_expert -sample 25 -sql <sql-query> test.db
+
+  # Do not generate any statistics at all:
+  ./sqlite3_expert -sample 0 -sql <sql-query> test.db
+
diff --git a/ext/expert/expert.c b/ext/expert/expert.c new file mode 100644 index 0000000000..051480f896 --- /dev/null +++ b/ext/expert/expert.c @@ -0,0 +1,156 @@ +/* +** 2017 April 07 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +*/ + + +#include +#include +#include +#include +#include "sqlite3expert.h" + + +static void option_requires_argument(const char *zOpt){ + fprintf(stderr, "Option requires an argument: %s\n", zOpt); + exit(-3); +} + +static int option_integer_arg(const char *zVal){ + return atoi(zVal); +} + +static void usage(char **argv){ + fprintf(stderr, "\n"); + fprintf(stderr, "Usage %s ?OPTIONS? DATABASE\n", argv[0]); + fprintf(stderr, "\n"); + fprintf(stderr, "Options are:\n"); + fprintf(stderr, " -sql SQL (analyze SQL statements passed as argument)\n"); + fprintf(stderr, " -file FILE (read SQL statements from file FILE)\n"); + fprintf(stderr, " -verbose LEVEL (integer verbosity level. default 1)\n"); + fprintf(stderr, " -sample PERCENT (percent of db to sample. default 100)\n"); + exit(-1); +} + +static int readSqlFromFile(sqlite3expert *p, const char *zFile, char **pzErr){ + FILE *in = fopen(zFile, "rb"); + long nIn; + size_t nRead; + char *pBuf; + int rc; + if( in==0 ){ + *pzErr = sqlite3_mprintf("failed to open file %s\n", zFile); + return SQLITE_ERROR; + } + fseek(in, 0, SEEK_END); + nIn = ftell(in); + rewind(in); + pBuf = sqlite3_malloc64( nIn+1 ); + nRead = fread(pBuf, nIn, 1, in); + fclose(in); + if( nRead!=1 ){ + sqlite3_free(pBuf); + *pzErr = sqlite3_mprintf("failed to read file %s\n", zFile); + return SQLITE_ERROR; + } + pBuf[nIn] = 0; + rc = sqlite3_expert_sql(p, pBuf, pzErr); + sqlite3_free(pBuf); + return rc; +} + +int main(int argc, char **argv){ + const char *zDb; + int rc = 0; + char *zErr = 0; + int i; + int iVerbose = 1; /* -verbose option */ + + sqlite3 *db = 0; + sqlite3expert *p = 0; + + if( argc<2 ) usage(argv); + zDb = argv[argc-1]; + if( zDb[0]=='-' ) usage(argv); + rc = sqlite3_open(zDb, &db); + if( rc!=SQLITE_OK ){ + fprintf(stderr, "Cannot open db file: %s - %s\n", zDb, sqlite3_errmsg(db)); + exit(-2); + } + + p = sqlite3_expert_new(db, &zErr); + if( p==0 ){ + fprintf(stderr, "Cannot run analysis: %s\n", zErr); + rc = 1; + }else{ + for(i=1; i<(argc-1); i++){ + char *zArg = argv[i]; + int nArg; + if( zArg[0]=='-' && zArg[1]=='-' && zArg[2]!=0 ) zArg++; + nArg = (int)strlen(zArg); + if( nArg>=2 && 0==sqlite3_strnicmp(zArg, "-file", nArg) ){ + if( ++i==(argc-1) ) option_requires_argument("-file"); + rc = readSqlFromFile(p, argv[i], &zErr); + } + + else if( nArg>=3 && 0==sqlite3_strnicmp(zArg, "-sql", nArg) ){ + if( ++i==(argc-1) ) option_requires_argument("-sql"); + rc = sqlite3_expert_sql(p, argv[i], &zErr); + } + + else if( nArg>=3 && 0==sqlite3_strnicmp(zArg, "-sample", nArg) ){ + int iSample; + if( ++i==(argc-1) ) option_requires_argument("-sample"); + iSample = option_integer_arg(argv[i]); + sqlite3_expert_config(p, EXPERT_CONFIG_SAMPLE, iSample); + } + + else if( nArg>=2 && 0==sqlite3_strnicmp(zArg, "-verbose", nArg) ){ + if( ++i==(argc-1) ) option_requires_argument("-verbose"); + iVerbose = option_integer_arg(argv[i]); + } + + else{ + usage(argv); + } + } + } + + if( rc==SQLITE_OK ){ + rc = sqlite3_expert_analyze(p, &zErr); + } + + if( rc==SQLITE_OK ){ + int nQuery = sqlite3_expert_count(p); + if( iVerbose>0 ){ + const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES); + fprintf(stdout, "-- Candidates -------------------------------\n"); + fprintf(stdout, "%s\n", zCand); + } + for(i=0; i0 ){ + fprintf(stdout, "-- Query %d ----------------------------------\n",i+1); + fprintf(stdout, "%s\n\n", zSql); + } + fprintf(stdout, "%s\n%s\n", zIdx, zEQP); + } + }else{ + fprintf(stderr, "Error: %s\n", zErr ? zErr : "?"); + } + + sqlite3_expert_destroy(p); + sqlite3_free(zErr); + return rc; +} diff --git a/ext/expert/expert1.test b/ext/expert/expert1.test new file mode 100644 index 0000000000..0c3b512af0 --- /dev/null +++ b/ext/expert/expert1.test @@ -0,0 +1,609 @@ +# 2009 Nov 11 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# TESTRUNNER: shell +# +# The focus of this file is testing the CLI shell tool. Specifically, +# the ".recommend" command. +# +# + +# Test plan: +# +# +if {![info exists testdir]} { + set testdir [file join [file dirname [info script]] .. .. test] +} +source $testdir/tester.tcl +set testprefix expert1 + +if {[info commands sqlite3_expert_new]==""} { + finish_test + return +} + + +set CLI [test_binary_name sqlite3] +set CMD [test_binary_name sqlite3_expert] + +proc squish {txt} { + regsub -all {[[:space:]]+} $txt { } +} + +proc do_setup_rec_test {tn setup sql res} { + reset_db + if {[info exists ::set_main_db_name]} { + dbconfig_maindbname_icecube db + } + db eval $setup + uplevel [list do_rec_test $tn $sql $res] +} + +foreach {tn setup} { + 1 { + if {![file executable $CMD]} { continue } + + proc do_rec_test {tn sql res} { + set res [squish [string trim $res]] + set tst [subst -nocommands { + squish [string trim [exec $::CMD -verbose 0 -sql {$sql;} test.db]] + }] + uplevel [list do_test $tn $tst $res] + } + } + 2 { + if {[info commands sqlite3_expert_new]==""} { continue } + + proc do_rec_test {tn sql res} { + set expert [sqlite3_expert_new db] + $expert sql $sql + $expert analyze + + set result [list] + for {set i 0} {$i < [$expert count]} {incr i} { + set idx [string trim [$expert report $i indexes]] + if {$idx==""} {set idx "(no new indexes)"} + lappend result $idx + lappend result [string trim [$expert report $i plan]] + } + + $expert destroy + + set tst [subst -nocommands {set {} [squish [join {$result}]]}] + uplevel [list do_test $tn $tst [string trim [squish $res]]] + } + } + 3 { + if {[info commands sqlite3_expert_new]==""} { continue } + set ::set_main_db_name 1 + } + 4 { + if {![file executable $CLI]} { continue } + + proc do_rec_test {tn sql res} { + set res [squish [string trim $res]] + set tst [subst -nocommands { + squish [string trim [exec $::CLI test.db ".expert" {$sql;}]] + }] + uplevel [list do_test $tn $tst $res] + } + } +} { + + eval $setup + + +do_setup_rec_test $tn.1 { CREATE TABLE t1(a, b, c) } { + SELECT * FROM t1 +} { + (no new indexes) + SCAN t1 +} + +do_setup_rec_test $tn.2 { + CREATE TABLE t1(a, b, c); +} { + SELECT * FROM t1 WHERE b>?; +} { + CREATE INDEX t1_idx_00000062 ON t1(b); + SEARCH t1 USING INDEX t1_idx_00000062 (b>?) +} + +do_setup_rec_test $tn.3 { + CREATE TABLE t1(a, b, c); +} { + SELECT * FROM t1 WHERE b COLLATE nocase BETWEEN ? AND ? +} { + CREATE INDEX t1_idx_3e094c27 ON t1(b COLLATE NOCASE); + SEARCH t1 USING INDEX t1_idx_3e094c27 (b>? AND b? AND b? +} { + (no new indexes) + SEARCH example USING INDEX sqlite_autoindex_example_1 (A=? AND B>?) +} +do_setup_rec_test $tn.17.5 { + CREATE TABLE example (A INTEGER, B INTEGER, C INTEGER, PRIMARY KEY (A,B)); +} { + SELECT * FROM example WHERE a>? AND b=? +} { + CREATE INDEX example_idx_0000cb3f ON example(B, A); + SEARCH example USING INDEX example_idx_0000cb3f (B=? AND A>?) +} + +do_setup_rec_test $tn.18.0 { + CREATE TABLE SomeObject ( + a INTEGER PRIMARY KEY, + x TEXT GENERATED ALWAYS AS(HEX(a)) VIRTUAL + ); +} { + SELECT x FROM SomeObject; +} { + (no new indexes) + SCAN SomeObject +} +do_setup_rec_test $tn.18.1 { + CREATE TABLE SomeObject ( + a INTEGER PRIMARY KEY, + x TEXT GENERATED ALWAYS AS(HEX(a)) VIRTUAL + ); +} { + SELECT * FROM SomeObject WHERE x=?; +} { + CREATE INDEX SomeObject_idx_00000078 ON SomeObject(x); + SEARCH SomeObject USING COVERING INDEX SomeObject_idx_00000078 (x=?) +} + + +do_setup_rec_test $tn.19.0 { + CREATE TABLE t1("index"); +} { + SELECT * FROM t1 ORDER BY "index"; +} { + CREATE INDEX t1_idx_01a7214e ON t1('index'); + SCAN t1 USING COVERING INDEX t1_idx_01a7214e +} + +ifcapable fts5 { + do_setup_rec_test $tn.20.0 { + CREATE VIRTUAL TABLE ft USING fts5(a); + CREATE TABLE t1(x, y); + } { + SELECT * FROM ft, t1 WHERE a=x + } { + CREATE INDEX t1_idx_00000078 ON t1(x); + SCAN ft VIRTUAL TABLE INDEX 0: + SEARCH t1 USING INDEX t1_idx_00000078 (x=?) + } +} + +} + +proc do_candidates_test {tn sql res} { + set res [squish [string trim $res]] + + set expert [sqlite3_expert_new db] + $expert sql $sql + $expert analyze + + set candidates [squish [string trim [$expert report 0 candidates]]] + $expert destroy + + uplevel [list do_test $tn [list set {} $candidates] $res] +} + + +reset_db +do_execsql_test 5.0 { + CREATE TABLE t1(a, b); + CREATE TABLE t2(c, d); + + WITH s(i) AS ( VALUES(1) UNION ALL SELECT i+1 FROM s WHERE i<100) + INSERT INTO t1 SELECT (i-1)/50, (i-1)/20 FROM s; + + WITH s(i) AS ( VALUES(1) UNION ALL SELECT i+1 FROM s WHERE i<100) + INSERT INTO t2 SELECT (i-1)/20, (i-1)/5 FROM s; + + CREATE INDEX i1 ON t1( lower(a) ); +} +do_candidates_test 5.1 { + SELECT * FROM t1,t2 WHERE (b=? OR a=?) AND (c=? OR d=?) +} { + CREATE INDEX t1_idx_00000062 ON t1(b); -- stat1: 100 20 + CREATE INDEX t1_idx_00000061 ON t1(a); -- stat1: 100 50 + CREATE INDEX t2_idx_00000063 ON t2(c); -- stat1: 100 20 + CREATE INDEX t2_idx_00000064 ON t2(d); -- stat1: 100 5 +} + +do_candidates_test 5.2 { + SELECT * FROM t1,t2 WHERE a=? AND b=? AND c=? AND d=? +} { + CREATE INDEX t1_idx_000123a7 ON t1(a, b); -- stat1: 100 50 17 + CREATE INDEX t2_idx_0001295b ON t2(c, d); -- stat1: 100 20 5 +} + +do_execsql_test 5.3 { + CREATE INDEX t1_idx_00000061 ON t1(a); -- stat1: 100 50 + CREATE INDEX t1_idx_00000062 ON t1(b); -- stat1: 100 20 + CREATE INDEX t1_idx_000123a7 ON t1(a, b); -- stat1: 100 50 16 + + CREATE INDEX t2_idx_00000063 ON t2(c); -- stat1: 100 20 + CREATE INDEX t2_idx_00000064 ON t2(d); -- stat1: 100 5 + CREATE INDEX t2_idx_0001295b ON t2(c, d); -- stat1: 100 20 5 + + ANALYZE; + SELECT * FROM sqlite_stat1 ORDER BY 1, 2; +} { + t1 i1 {100 50} + t1 t1_idx_00000061 {100 50} + t1 t1_idx_00000062 {100 20} + t1 t1_idx_000123a7 {100 50 17} + t2 t2_idx_00000063 {100 20} + t2 t2_idx_00000064 {100 5} + t2 t2_idx_0001295b {100 20 5} +} + +do_catchsql_test 5.4 { + SELECT sqlite_expert_rem(123, 123); +} {1 {no such function: sqlite_expert_rem}} +do_catchsql_test 5.5 { + SELECT sqlite_expert_sample(); +} {1 {no such function: sqlite_expert_sample}} + +if 0 { +do_test expert1-6.0 { + catchcmd :memory: { +.expert +select base64(''); +.expert +select name from pragma_collation_list order by name collate uint; +} +} {0 {(no new indexes) + +SCAN CONSTANT ROW + +(no new indexes) + +SCAN pragma_collation_list VIRTUAL TABLE INDEX 0: +USE TEMP B-TREE FOR ORDER BY +}} +} + +do_execsql_test 6.0 { + CREATE TABLE x1(a, b, c, d); + CREATE INDEX x1ab ON x1(a, lower(b)); + CREATE INDEX x1dcba ON x1(d, b+c, a); +} + +do_candidates_test 6.1 { + SELECT * FROM x1 WHERE b=? ORDER BY a; +} { + CREATE INDEX x1_idx_0001267f ON x1(b, a); + CREATE INDEX x1_idx_00000062 ON x1(b); +} + +#------------------------------------------------------------------------- +ifcapable fts5 { + reset_db + do_execsql_test 7.0 { + CREATE VIRTUAL TABLE ft USING fts5(a); + CREATE TABLE t1(x, y); + } + + do_candidates_test 7.1 { + SELECT * FROM ft, t1 WHERE a=x + } { + CREATE INDEX t1_idx_00000078 ON t1(x); + } + + register_tcl_module db + proc vtab_command {method args} { + global G + + switch -- $method { + xConnect { + return "CREATE TABLE t1(a, b, c);" + } + + xBestIndex { + return [list] + } + + xFilter { + return [list sql "SELECT rowid, * FROM t0"] + } + } + + return {} + } + + do_execsql_test 7.2 { + CREATE TABLE t0(a, b, c); + INSERT INTO t0 VALUES(1, 2, 3), (11, 22, 33); + CREATE VIRTUAL TABLE t2 USING tcl(vtab_command); + } + + do_execsql_test 7.3 { + SELECT * FROM t2 + } { + 1 2 3 + 11 22 33 + } + + do_candidates_test 7.4 { + SELECT * FROM ft, t1 WHERE a=x + } { + CREATE INDEX t1_idx_00000078 ON t1(x); + } + + do_test 7.5 { + set expert [sqlite3_expert_new db] + list [catch { $expert sql "SELECT * FROM ft, t2 WHERE b=1" } msg] $msg + } {1 {no such table: t2}} + $expert destroy + + reset_db + do_execsql_test 7.6 { + BEGIN TRANSACTION; + CREATE TABLE IF NOT EXISTS 'bfts_idx_data'(id INTEGER PRIMARY KEY, block BLOB); + CREATE TABLE IF NOT EXISTS 'fts_idx_data'(id INTEGER PRIMARY KEY, block BLOB); + INSERT INTO fts_idx_data VALUES(1,X''); + INSERT INTO fts_idx_data VALUES(10,X'00000000ff000001000000'); + CREATE TABLE IF NOT EXISTS 'fts_idx_idx'(segid, term, pgno, PRIMARY KEY(segid, term)) WITHOUT ROWID; + CREATE TABLE IF NOT EXISTS 'fts_idx_docsize'(id INTEGER PRIMARY KEY, sz BLOB, origin INTEGER); + CREATE TABLE IF NOT EXISTS 'fts_idx_config'(k PRIMARY KEY, v) WITHOUT ROWID; + INSERT INTO fts_idx_config VALUES('version',4); + PRAGMA writable_schema=ON; + INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)VALUES('table','fts_idx','fts_idx',0,'CREATE VIRTUAL TABLE fts_idx USING fts5(Title, Description, Channel, Tags, content='''', contentless_delete=1)'); + + CREATE TABLE f(x BLOB, y); + COMMIT; + PRAGMA writable_schema = RESET; + } + + do_candidates_test 7.4 { + SELECT * FROM fts_idx, f WHERE x = fts_idx.Channel + } { + CREATE INDEX f_idx_00000078 ON f(x); + } +} + +finish_test diff --git a/ext/expert/sqlite3expert.c b/ext/expert/sqlite3expert.c new file mode 100644 index 0000000000..c430c3ae95 --- /dev/null +++ b/ext/expert/sqlite3expert.c @@ -0,0 +1,2236 @@ +/* +** 2017 April 09 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +*/ +#include "sqlite3expert.h" +#include +#include +#include + +#if !defined(SQLITE_AMALGAMATION) +#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST) +# define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1 +#endif +#if defined(SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS) +# define ALWAYS(X) (1) +# define NEVER(X) (0) +#elif !defined(NDEBUG) +# define ALWAYS(X) ((X)?1:(assert(0),0)) +# define NEVER(X) ((X)?(assert(0),1):0) +#else +# define ALWAYS(X) (X) +# define NEVER(X) (X) +#endif +#endif /* !defined(SQLITE_AMALGAMATION) */ + + +#ifndef SQLITE_OMIT_VIRTUALTABLE + +typedef sqlite3_int64 i64; +typedef sqlite3_uint64 u64; + +typedef struct IdxColumn IdxColumn; +typedef struct IdxConstraint IdxConstraint; +typedef struct IdxScan IdxScan; +typedef struct IdxStatement IdxStatement; +typedef struct IdxTable IdxTable; +typedef struct IdxWrite IdxWrite; + +#define STRLEN (int)strlen + +/* +** A temp table name that we assume no user database will actually use. +** If this assumption proves incorrect triggers on the table with the +** conflicting name will be ignored. +*/ +#define UNIQUE_TABLE_NAME "t592690916721053953805701627921227776" + +/* +** A single constraint. Equivalent to either "col = ?" or "col < ?" (or +** any other type of single-ended range constraint on a column). +** +** pLink: +** Used to temporarily link IdxConstraint objects into lists while +** creating candidate indexes. +*/ +struct IdxConstraint { + char *zColl; /* Collation sequence */ + int bRange; /* True for range, false for eq */ + int iCol; /* Constrained table column */ + int bFlag; /* Used by idxFindCompatible() */ + int bDesc; /* True if ORDER BY DESC */ + IdxConstraint *pNext; /* Next constraint in pEq or pRange list */ + IdxConstraint *pLink; /* See above */ +}; + +/* +** A single scan of a single table. +*/ +struct IdxScan { + IdxTable *pTab; /* Associated table object */ + int iDb; /* Database containing table zTable */ + i64 covering; /* Mask of columns required for cov. index */ + IdxConstraint *pOrder; /* ORDER BY columns */ + IdxConstraint *pEq; /* List of == constraints */ + IdxConstraint *pRange; /* List of < constraints */ + IdxScan *pNextScan; /* Next IdxScan object for same analysis */ +}; + +/* +** Information regarding a single database table. Extracted from +** "PRAGMA table_info" by function idxGetTableInfo(). +*/ +struct IdxColumn { + char *zName; + char *zColl; + int iPk; +}; +struct IdxTable { + int nCol; + char *zName; /* Table name */ + IdxColumn *aCol; + IdxTable *pNext; /* Next table in linked list of all tables */ +}; + +/* +** An object of the following type is created for each unique table/write-op +** seen. The objects are stored in a singly-linked list beginning at +** sqlite3expert.pWrite. +*/ +struct IdxWrite { + IdxTable *pTab; + int eOp; /* SQLITE_UPDATE, DELETE or INSERT */ + IdxWrite *pNext; +}; + +/* +** Each statement being analyzed is represented by an instance of this +** structure. +*/ +struct IdxStatement { + int iId; /* Statement number */ + char *zSql; /* SQL statement */ + char *zIdx; /* Indexes */ + char *zEQP; /* Plan */ + IdxStatement *pNext; +}; + + +/* +** A hash table for storing strings. With space for a payload string +** with each entry. Methods are: +** +** idxHashInit() +** idxHashClear() +** idxHashAdd() +** idxHashSearch() +*/ +#define IDX_HASH_SIZE 1023 +typedef struct IdxHashEntry IdxHashEntry; +typedef struct IdxHash IdxHash; +struct IdxHashEntry { + char *zKey; /* nul-terminated key */ + char *zVal; /* nul-terminated value string */ + char *zVal2; /* nul-terminated value string 2 */ + IdxHashEntry *pHashNext; /* Next entry in same hash bucket */ + IdxHashEntry *pNext; /* Next entry in hash */ +}; +struct IdxHash { + IdxHashEntry *pFirst; + IdxHashEntry *aHash[IDX_HASH_SIZE]; +}; + +/* +** sqlite3expert object. +*/ +struct sqlite3expert { + int iSample; /* Percentage of tables to sample for stat1 */ + sqlite3 *db; /* User database */ + sqlite3 *dbm; /* In-memory db for this analysis */ + sqlite3 *dbv; /* Vtab schema for this analysis */ + IdxTable *pTable; /* List of all IdxTable objects */ + IdxScan *pScan; /* List of scan objects */ + IdxWrite *pWrite; /* List of write objects */ + IdxStatement *pStatement; /* List of IdxStatement objects */ + int bRun; /* True once analysis has run */ + char **pzErrmsg; + int rc; /* Error code from whereinfo hook */ + IdxHash hIdx; /* Hash containing all candidate indexes */ + char *zCandidates; /* For EXPERT_REPORT_CANDIDATES */ +}; + + +/* +** Allocate and return nByte bytes of zeroed memory using sqlite3_malloc(). +** If the allocation fails, set *pRc to SQLITE_NOMEM and return NULL. +*/ +static void *idxMalloc(int *pRc, i64 nByte){ + void *pRet; + assert( *pRc==SQLITE_OK ); + assert( nByte>0 ); + pRet = sqlite3_malloc64(nByte); + if( pRet ){ + memset(pRet, 0, nByte); + }else{ + *pRc = SQLITE_NOMEM; + } + return pRet; +} + +/* +** Initialize an IdxHash hash table. +*/ +static void idxHashInit(IdxHash *pHash){ + memset(pHash, 0, sizeof(IdxHash)); +} + +/* +** Reset an IdxHash hash table. +*/ +static void idxHashClear(IdxHash *pHash){ + int i; + for(i=0; iaHash[i]; pEntry; pEntry=pNext){ + pNext = pEntry->pHashNext; + sqlite3_free(pEntry->zVal2); + sqlite3_free(pEntry); + } + } + memset(pHash, 0, sizeof(IdxHash)); +} + +/* +** Return the index of the hash bucket that the string specified by the +** arguments to this function belongs. +*/ +static int idxHashString(const char *z, int n){ + unsigned int ret = 0; + int i; + for(i=0; i=0 ); + for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){ + if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){ + return 1; + } + } + pEntry = idxMalloc(pRc, sizeof(IdxHashEntry) + (i64)nKey+1 + (i64)nVal+1); + if( pEntry ){ + pEntry->zKey = (char*)&pEntry[1]; + memcpy(pEntry->zKey, zKey, nKey); + if( zVal ){ + pEntry->zVal = &pEntry->zKey[nKey+1]; + memcpy(pEntry->zVal, zVal, nVal); + } + pEntry->pHashNext = pHash->aHash[iHash]; + pHash->aHash[iHash] = pEntry; + + pEntry->pNext = pHash->pFirst; + pHash->pFirst = pEntry; + } + return 0; +} + +/* +** If zKey/nKey is present in the hash table, return a pointer to the +** hash-entry object. +*/ +static IdxHashEntry *idxHashFind(IdxHash *pHash, const char *zKey, int nKey){ + int iHash; + IdxHashEntry *pEntry; + if( nKey<0 ) nKey = STRLEN(zKey); + iHash = idxHashString(zKey, nKey); + assert( iHash>=0 ); + for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){ + if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){ + return pEntry; + } + } + return 0; +} + +/* +** If the hash table contains an entry with a key equal to the string +** passed as the final two arguments to this function, return a pointer +** to the payload string. Otherwise, if zKey/nKey is not present in the +** hash table, return NULL. +*/ +static const char *idxHashSearch(IdxHash *pHash, const char *zKey, int nKey){ + IdxHashEntry *pEntry = idxHashFind(pHash, zKey, nKey); + if( pEntry ) return pEntry->zVal; + return 0; +} + +/* +** Allocate and return a new IdxConstraint object. Set the IdxConstraint.zColl +** variable to point to a copy of nul-terminated string zColl. +*/ +static IdxConstraint *idxNewConstraint(int *pRc, const char *zColl){ + IdxConstraint *pNew; + int nColl = STRLEN(zColl); + + assert( *pRc==SQLITE_OK ); + pNew = (IdxConstraint*)idxMalloc(pRc, sizeof(IdxConstraint) * nColl + 1); + if( pNew ){ + pNew->zColl = (char*)&pNew[1]; + memcpy(pNew->zColl, zColl, nColl+1); + } + return pNew; +} + +/* +** An error associated with database handle db has just occurred. Pass +** the error message to callback function xOut. +*/ +static void idxDatabaseError( + sqlite3 *db, /* Database handle */ + char **pzErrmsg /* Write error here */ +){ + *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db)); +} + +/* +** Prepare an SQL statement. +*/ +static int idxPrepareStmt( + sqlite3 *db, /* Database handle to compile against */ + sqlite3_stmt **ppStmt, /* OUT: Compiled SQL statement */ + char **pzErrmsg, /* OUT: sqlite3_malloc()ed error message */ + const char *zSql /* SQL statement to compile */ +){ + int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0); + if( rc!=SQLITE_OK ){ + *ppStmt = 0; + idxDatabaseError(db, pzErrmsg); + } + return rc; +} + +/* +** Prepare an SQL statement using the results of a printf() formatting. +*/ +static int idxPrintfPrepareStmt( + sqlite3 *db, /* Database handle to compile against */ + sqlite3_stmt **ppStmt, /* OUT: Compiled SQL statement */ + char **pzErrmsg, /* OUT: sqlite3_malloc()ed error message */ + const char *zFmt, /* printf() format of SQL statement */ + ... /* Trailing printf() arguments */ +){ + va_list ap; + int rc; + char *zSql; + va_start(ap, zFmt); + zSql = sqlite3_vmprintf(zFmt, ap); + if( zSql==0 ){ + rc = SQLITE_NOMEM; + }else{ + rc = idxPrepareStmt(db, ppStmt, pzErrmsg, zSql); + sqlite3_free(zSql); + } + va_end(ap); + return rc; +} + + +/************************************************************************* +** Beginning of virtual table implementation. +*/ +typedef struct ExpertVtab ExpertVtab; +struct ExpertVtab { + sqlite3_vtab base; + IdxTable *pTab; + sqlite3expert *pExpert; +}; + +typedef struct ExpertCsr ExpertCsr; +struct ExpertCsr { + sqlite3_vtab_cursor base; + sqlite3_stmt *pData; +}; + +static char *expertDequote(const char *zIn){ + i64 n = STRLEN(zIn); + char *zRet = sqlite3_malloc64(n); + + assert( zIn[0]=='\'' ); + assert( zIn[n-1]=='\'' ); + + if( zRet ){ + i64 iOut = 0; + i64 iIn = 0; + for(iIn=1; iIn<(n-1); iIn++){ + if( zIn[iIn]=='\'' ){ + assert( zIn[iIn+1]=='\'' ); + iIn++; + } + zRet[iOut++] = zIn[iIn]; + } + zRet[iOut] = '\0'; + } + + return zRet; +} + +/* +** This function is the implementation of both the xConnect and xCreate +** methods of the r-tree virtual table. +** +** argv[0] -> module name +** argv[1] -> database name +** argv[2] -> table name +** argv[...] -> column names... +*/ +static int expertConnect( + sqlite3 *db, + void *pAux, + int argc, const char *const*argv, + sqlite3_vtab **ppVtab, + char **pzErr +){ + sqlite3expert *pExpert = (sqlite3expert*)pAux; + ExpertVtab *p = 0; + int rc; + + if( argc!=4 ){ + *pzErr = sqlite3_mprintf("internal error!"); + rc = SQLITE_ERROR; + }else{ + char *zCreateTable = expertDequote(argv[3]); + if( zCreateTable ){ + rc = sqlite3_declare_vtab(db, zCreateTable); + if( rc==SQLITE_OK ){ + p = idxMalloc(&rc, sizeof(ExpertVtab)); + } + if( rc==SQLITE_OK ){ + p->pExpert = pExpert; + p->pTab = pExpert->pTable; + assert( sqlite3_stricmp(p->pTab->zName, argv[2])==0 ); + } + sqlite3_free(zCreateTable); + }else{ + rc = SQLITE_NOMEM; + } + } + + *ppVtab = (sqlite3_vtab*)p; + return rc; +} + +static int expertDisconnect(sqlite3_vtab *pVtab){ + ExpertVtab *p = (ExpertVtab*)pVtab; + sqlite3_free(p); + return SQLITE_OK; +} + +static int expertBestIndex(sqlite3_vtab *pVtab, sqlite3_index_info *pIdxInfo){ + ExpertVtab *p = (ExpertVtab*)pVtab; + int rc = SQLITE_OK; + int n = 0; + IdxScan *pScan; + const int opmask = + SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_GT | + SQLITE_INDEX_CONSTRAINT_LT | SQLITE_INDEX_CONSTRAINT_GE | + SQLITE_INDEX_CONSTRAINT_LE; + + pScan = idxMalloc(&rc, sizeof(IdxScan)); + if( pScan ){ + int i; + + /* Link the new scan object into the list */ + pScan->pTab = p->pTab; + pScan->pNextScan = p->pExpert->pScan; + p->pExpert->pScan = pScan; + + /* Add the constraints to the IdxScan object */ + for(i=0; inConstraint; i++){ + struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i]; + if( pCons->usable + && pCons->iColumn>=0 + && p->pTab->aCol[pCons->iColumn].iPk==0 + && (pCons->op & opmask) + ){ + IdxConstraint *pNew; + const char *zColl = sqlite3_vtab_collation(pIdxInfo, i); + pNew = idxNewConstraint(&rc, zColl); + if( pNew ){ + pNew->iCol = pCons->iColumn; + if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){ + pNew->pNext = pScan->pEq; + pScan->pEq = pNew; + }else{ + pNew->bRange = 1; + pNew->pNext = pScan->pRange; + pScan->pRange = pNew; + } + } + n++; + pIdxInfo->aConstraintUsage[i].argvIndex = n; + } + } + + /* Add the ORDER BY to the IdxScan object */ + for(i=pIdxInfo->nOrderBy-1; i>=0; i--){ + int iCol = pIdxInfo->aOrderBy[i].iColumn; + if( iCol>=0 ){ + IdxConstraint *pNew = idxNewConstraint(&rc, p->pTab->aCol[iCol].zColl); + if( pNew ){ + pNew->iCol = iCol; + pNew->bDesc = pIdxInfo->aOrderBy[i].desc; + pNew->pNext = pScan->pOrder; + pNew->pLink = pScan->pOrder; + pScan->pOrder = pNew; + n++; + } + } + } + } + + pIdxInfo->estimatedCost = 1000000.0 / (n+1); + return rc; +} + +static int expertUpdate( + sqlite3_vtab *pVtab, + int nData, + sqlite3_value **azData, + sqlite_int64 *pRowid +){ + (void)pVtab; + (void)nData; + (void)azData; + (void)pRowid; + return SQLITE_OK; +} + +/* +** Virtual table module xOpen method. +*/ +static int expertOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){ + int rc = SQLITE_OK; + ExpertCsr *pCsr; + (void)pVTab; + pCsr = idxMalloc(&rc, sizeof(ExpertCsr)); + *ppCursor = (sqlite3_vtab_cursor*)pCsr; + return rc; +} + +/* +** Virtual table module xClose method. +*/ +static int expertClose(sqlite3_vtab_cursor *cur){ + ExpertCsr *pCsr = (ExpertCsr*)cur; + sqlite3_finalize(pCsr->pData); + sqlite3_free(pCsr); + return SQLITE_OK; +} + +/* +** Virtual table module xEof method. +** +** Return non-zero if the cursor does not currently point to a valid +** record (i.e if the scan has finished), or zero otherwise. +*/ +static int expertEof(sqlite3_vtab_cursor *cur){ + ExpertCsr *pCsr = (ExpertCsr*)cur; + return pCsr->pData==0; +} + +/* +** Virtual table module xNext method. +*/ +static int expertNext(sqlite3_vtab_cursor *cur){ + ExpertCsr *pCsr = (ExpertCsr*)cur; + int rc = SQLITE_OK; + + assert( pCsr->pData ); + rc = sqlite3_step(pCsr->pData); + if( rc!=SQLITE_ROW ){ + rc = sqlite3_finalize(pCsr->pData); + pCsr->pData = 0; + }else{ + rc = SQLITE_OK; + } + + return rc; +} + +/* +** Virtual table module xRowid method. +*/ +static int expertRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ + (void)cur; + *pRowid = 0; + return SQLITE_OK; +} + +/* +** Virtual table module xColumn method. +*/ +static int expertColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){ + ExpertCsr *pCsr = (ExpertCsr*)cur; + sqlite3_value *pVal; + pVal = sqlite3_column_value(pCsr->pData, i); + if( pVal ){ + sqlite3_result_value(ctx, pVal); + } + return SQLITE_OK; +} + +/* +** Virtual table module xFilter method. +*/ +static int expertFilter( + sqlite3_vtab_cursor *cur, + int idxNum, const char *idxStr, + int argc, sqlite3_value **argv +){ + ExpertCsr *pCsr = (ExpertCsr*)cur; + ExpertVtab *pVtab = (ExpertVtab*)(cur->pVtab); + sqlite3expert *pExpert = pVtab->pExpert; + int rc; + + (void)idxNum; + (void)idxStr; + (void)argc; + (void)argv; + rc = sqlite3_finalize(pCsr->pData); + pCsr->pData = 0; + if( rc==SQLITE_OK ){ + rc = idxPrintfPrepareStmt(pExpert->db, &pCsr->pData, &pVtab->base.zErrMsg, + "SELECT * FROM main.%Q WHERE sqlite_expert_sample()", pVtab->pTab->zName + ); + } + + if( rc==SQLITE_OK ){ + rc = expertNext(cur); + } + return rc; +} + +static int idxRegisterVtab(sqlite3expert *p){ + static sqlite3_module expertModule = { + 2, /* iVersion */ + expertConnect, /* xCreate - create a table */ + expertConnect, /* xConnect - connect to an existing table */ + expertBestIndex, /* xBestIndex - Determine search strategy */ + expertDisconnect, /* xDisconnect - Disconnect from a table */ + expertDisconnect, /* xDestroy - Drop a table */ + expertOpen, /* xOpen - open a cursor */ + expertClose, /* xClose - close a cursor */ + expertFilter, /* xFilter - configure scan constraints */ + expertNext, /* xNext - advance a cursor */ + expertEof, /* xEof */ + expertColumn, /* xColumn - read data */ + expertRowid, /* xRowid - read data */ + expertUpdate, /* xUpdate - write data */ + 0, /* xBegin - begin transaction */ + 0, /* xSync - sync transaction */ + 0, /* xCommit - commit transaction */ + 0, /* xRollback - rollback transaction */ + 0, /* xFindFunction - function overloading */ + 0, /* xRename - rename the table */ + 0, /* xSavepoint */ + 0, /* xRelease */ + 0, /* xRollbackTo */ + 0, /* xShadowName */ + 0, /* xIntegrity */ + }; + + return sqlite3_create_module(p->dbv, "expert", &expertModule, (void*)p); +} +/* +** End of virtual table implementation. +*************************************************************************/ +/* +** Finalize SQL statement pStmt. If (*pRc) is SQLITE_OK when this function +** is called, set it to the return value of sqlite3_finalize() before +** returning. Otherwise, discard the sqlite3_finalize() return value. +*/ +static void idxFinalize(int *pRc, sqlite3_stmt *pStmt){ + int rc = sqlite3_finalize(pStmt); + if( *pRc==SQLITE_OK ) *pRc = rc; +} + +/* +** Attempt to allocate an IdxTable structure corresponding to table zTab +** in the main database of connection db. If successful, set (*ppOut) to +** point to the new object and return SQLITE_OK. Otherwise, return an +** SQLite error code and set (*ppOut) to NULL. In this case *pzErrmsg may be +** set to point to an error string. +** +** It is the responsibility of the caller to eventually free either the +** IdxTable object or error message using sqlite3_free(). +*/ +static int idxGetTableInfo( + sqlite3 *db, /* Database connection to read details from */ + const char *zTab, /* Table name */ + IdxTable **ppOut, /* OUT: New object (if successful) */ + char **pzErrmsg /* OUT: Error message (if not) */ +){ + sqlite3_stmt *p1 = 0; + int nCol = 0; + int nTab; + i64 nByte; + IdxTable *pNew = 0; + int rc, rc2; + char *pCsr = 0; + int nPk = 0; + + *ppOut = 0; + if( zTab==0 ) return SQLITE_ERROR; + nTab = STRLEN(zTab); + nByte = sizeof(IdxTable) + nTab + 1; + rc = idxPrintfPrepareStmt(db, &p1, pzErrmsg, "PRAGMA table_xinfo=%Q", zTab); + while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){ + const char *zCol = (const char*)sqlite3_column_text(p1, 1); + const char *zColSeq = 0; + if( zCol==0 ){ + rc = SQLITE_ERROR; + break; + } + nByte += 1 + STRLEN(zCol); + rc = sqlite3_table_column_metadata( + db, "main", zTab, zCol, 0, &zColSeq, 0, 0, 0 + ); + if( zColSeq==0 ) zColSeq = "binary"; + nByte += 1 + STRLEN(zColSeq); + nCol++; + nPk += (sqlite3_column_int(p1, 5)>0); + } + rc2 = sqlite3_reset(p1); + if( rc==SQLITE_OK ) rc = rc2; + + nByte += sizeof(IdxColumn) * nCol; + if( rc==SQLITE_OK ){ + pNew = idxMalloc(&rc, nByte); + } + if( rc==SQLITE_OK ){ + pNew->aCol = (IdxColumn*)&pNew[1]; + pNew->nCol = nCol; + pCsr = (char*)&pNew->aCol[nCol]; + } + + nCol = 0; + while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){ + const char *zCol = (const char*)sqlite3_column_text(p1, 1); + const char *zColSeq = 0; + int nCopy; + if( zCol==0 ) continue; + nCopy = STRLEN(zCol) + 1; + pNew->aCol[nCol].zName = pCsr; + pNew->aCol[nCol].iPk = (sqlite3_column_int(p1, 5)==1 && nPk==1); + memcpy(pCsr, zCol, nCopy); + pCsr += nCopy; + + rc = sqlite3_table_column_metadata( + db, "main", zTab, zCol, 0, &zColSeq, 0, 0, 0 + ); + if( rc==SQLITE_OK ){ + if( zColSeq==0 ) zColSeq = "binary"; + nCopy = STRLEN(zColSeq) + 1; + pNew->aCol[nCol].zColl = pCsr; + memcpy(pCsr, zColSeq, nCopy); + pCsr += nCopy; + } + + nCol++; + } + idxFinalize(&rc, p1); + + if( rc!=SQLITE_OK ){ + sqlite3_free(pNew); + pNew = 0; + }else if( ALWAYS(pNew!=0) ){ + pNew->zName = pCsr; + if( ALWAYS(pNew->zName!=0) ) memcpy(pNew->zName, zTab, nTab+1); + } + + *ppOut = pNew; + return rc; +} + +/* +** This function is a no-op if *pRc is set to anything other than +** SQLITE_OK when it is called. +** +** If *pRc is initially set to SQLITE_OK, then the text specified by +** the printf() style arguments is appended to zIn and the result returned +** in a buffer allocated by sqlite3_malloc(). sqlite3_free() is called on +** zIn before returning. +*/ +static char *idxAppendText(int *pRc, char *zIn, const char *zFmt, ...){ + va_list ap; + char *zAppend = 0; + char *zRet = 0; + i64 nIn = zIn ? STRLEN(zIn) : 0; + i64 nAppend = 0; + va_start(ap, zFmt); + if( *pRc==SQLITE_OK ){ + zAppend = sqlite3_vmprintf(zFmt, ap); + if( zAppend ){ + nAppend = STRLEN(zAppend); + zRet = (char*)sqlite3_malloc64(nIn + nAppend + 1); + } + if( zAppend && zRet ){ + if( nIn ) memcpy(zRet, zIn, nIn); + memcpy(&zRet[nIn], zAppend, nAppend+1); + }else{ + sqlite3_free(zRet); + zRet = 0; + *pRc = SQLITE_NOMEM; + } + sqlite3_free(zAppend); + sqlite3_free(zIn); + } + va_end(ap); + return zRet; +} + +/* +** Return true if zId must be quoted in order to use it as an SQL +** identifier, or false otherwise. +*/ +static int idxIdentifierRequiresQuotes(const char *zId){ + int i; + int nId = STRLEN(zId); + + if( sqlite3_keyword_check(zId, nId) ) return 1; + + for(i=0; zId[i]; i++){ + if( !(zId[i]=='_') + && !(zId[i]>='0' && zId[i]<='9') + && !(zId[i]>='a' && zId[i]<='z') + && !(zId[i]>='A' && zId[i]<='Z') + ){ + return 1; + } + } + return 0; +} + +/* +** This function appends an index column definition suitable for constraint +** pCons to the string passed as zIn and returns the result. +*/ +static char *idxAppendColDefn( + int *pRc, /* IN/OUT: Error code */ + char *zIn, /* Column defn accumulated so far */ + IdxTable *pTab, /* Table index will be created on */ + IdxConstraint *pCons +){ + char *zRet = zIn; + IdxColumn *p = &pTab->aCol[pCons->iCol]; + if( zRet ) zRet = idxAppendText(pRc, zRet, ", "); + + if( idxIdentifierRequiresQuotes(p->zName) ){ + zRet = idxAppendText(pRc, zRet, "%Q", p->zName); + }else{ + zRet = idxAppendText(pRc, zRet, "%s", p->zName); + } + + if( sqlite3_stricmp(p->zColl, pCons->zColl) ){ + if( idxIdentifierRequiresQuotes(pCons->zColl) ){ + zRet = idxAppendText(pRc, zRet, " COLLATE %Q", pCons->zColl); + }else{ + zRet = idxAppendText(pRc, zRet, " COLLATE %s", pCons->zColl); + } + } + + if( pCons->bDesc ){ + zRet = idxAppendText(pRc, zRet, " DESC"); + } + return zRet; +} + +/* +** Search database dbm for an index compatible with the one idxCreateFromCons() +** would create from arguments pScan, pEq and pTail. If no error occurs and +** such an index is found, return non-zero. Or, if no such index is found, +** return zero. +** +** If an error occurs, set *pRc to an SQLite error code and return zero. +*/ +static int idxFindCompatible( + int *pRc, /* OUT: Error code */ + sqlite3* dbm, /* Database to search */ + IdxScan *pScan, /* Scan for table to search for index on */ + IdxConstraint *pEq, /* List of == constraints */ + IdxConstraint *pTail /* List of range constraints */ +){ + const char *zTbl = pScan->pTab->zName; + sqlite3_stmt *pIdxList = 0; + IdxConstraint *pIter; + int nEq = 0; /* Number of elements in pEq */ + int rc; + + /* Count the elements in list pEq */ + for(pIter=pEq; pIter; pIter=pIter->pLink) nEq++; + + rc = idxPrintfPrepareStmt(dbm, &pIdxList, 0, "PRAGMA index_list=%Q", zTbl); + while( rc==SQLITE_OK && sqlite3_step(pIdxList)==SQLITE_ROW ){ + int bMatch = 1; + IdxConstraint *pT = pTail; + sqlite3_stmt *pInfo = 0; + const char *zIdx = (const char*)sqlite3_column_text(pIdxList, 1); + if( zIdx==0 ) continue; + + /* Zero the IdxConstraint.bFlag values in the pEq list */ + for(pIter=pEq; pIter; pIter=pIter->pLink) pIter->bFlag = 0; + + rc = idxPrintfPrepareStmt(dbm, &pInfo, 0, "PRAGMA index_xInfo=%Q", zIdx); + while( rc==SQLITE_OK && sqlite3_step(pInfo)==SQLITE_ROW ){ + int iIdx = sqlite3_column_int(pInfo, 0); + int iCol = sqlite3_column_int(pInfo, 1); + const char *zColl = (const char*)sqlite3_column_text(pInfo, 4); + + if( iIdxpLink){ + if( pIter->bFlag ) continue; + if( pIter->iCol!=iCol ) continue; + if( sqlite3_stricmp(pIter->zColl, zColl) ) continue; + pIter->bFlag = 1; + break; + } + if( pIter==0 ){ + bMatch = 0; + break; + } + }else{ + if( pT ){ + if( pT->iCol!=iCol || sqlite3_stricmp(pT->zColl, zColl) ){ + bMatch = 0; + break; + } + pT = pT->pLink; + } + } + } + idxFinalize(&rc, pInfo); + + if( rc==SQLITE_OK && bMatch ){ + sqlite3_finalize(pIdxList); + return 1; + } + } + idxFinalize(&rc, pIdxList); + + *pRc = rc; + return 0; +} + +/* Callback for sqlite3_exec() with query with leading count(*) column. + * The first argument is expected to be an int*, referent to be incremented + * if that leading column is not exactly '0'. + */ +static int countNonzeros(void* pCount, int nc, + char* azResults[], char* azColumns[]){ + (void)azColumns; /* Suppress unused parameter warning */ + if( nc>0 && (azResults[0][0]!='0' || azResults[0][1]!=0) ){ + *((int *)pCount) += 1; + } + return 0; +} + +static int idxCreateFromCons( + sqlite3expert *p, + IdxScan *pScan, + IdxConstraint *pEq, + IdxConstraint *pTail +){ + sqlite3 *dbm = p->dbm; + int rc = SQLITE_OK; + if( (pEq || pTail) && 0==idxFindCompatible(&rc, dbm, pScan, pEq, pTail) ){ + IdxTable *pTab = pScan->pTab; + char *zCols = 0; + char *zIdx = 0; + IdxConstraint *pCons; + unsigned int h = 0; + const char *zFmt; + + for(pCons=pEq; pCons; pCons=pCons->pLink){ + zCols = idxAppendColDefn(&rc, zCols, pTab, pCons); + } + for(pCons=pTail; pCons; pCons=pCons->pLink){ + zCols = idxAppendColDefn(&rc, zCols, pTab, pCons); + } + + if( rc==SQLITE_OK ){ + /* Hash the list of columns to come up with a name for the index */ + const char *zTable = pScan->pTab->zName; + int quoteTable = idxIdentifierRequiresQuotes(zTable); + char *zName = 0; /* Index name */ + int collisions = 0; + do{ + int i; + char *zFind; + for(i=0; zCols[i]; i++){ + h += ((h<<3) + zCols[i]); + } + sqlite3_free(zName); + zName = sqlite3_mprintf("%s_idx_%08x", zTable, h); + if( zName==0 ) break; + /* Is is unique among table, view and index names? */ + zFmt = "SELECT count(*) FROM sqlite_schema WHERE name=%Q" + " AND type in ('index','table','view')"; + zFind = sqlite3_mprintf(zFmt, zName); + i = 0; + rc = sqlite3_exec(dbm, zFind, countNonzeros, &i, 0); + assert(rc==SQLITE_OK); + sqlite3_free(zFind); + if( i==0 ){ + collisions = 0; + break; + } + ++collisions; + }while( collisions<50 && zName!=0 ); + if( collisions ){ + /* This return means "Gave up trying to find a unique index name." */ + rc = SQLITE_BUSY_TIMEOUT; + }else if( zName==0 ){ + rc = SQLITE_NOMEM; + }else{ + if( quoteTable ){ + zFmt = "CREATE INDEX \"%w\" ON \"%w\"(%s)"; + }else{ + zFmt = "CREATE INDEX %s ON %s(%s)"; + } + zIdx = sqlite3_mprintf(zFmt, zName, zTable, zCols); + if( !zIdx ){ + rc = SQLITE_NOMEM; + }else{ + rc = sqlite3_exec(dbm, zIdx, 0, 0, p->pzErrmsg); + if( rc!=SQLITE_OK ){ + rc = SQLITE_BUSY_TIMEOUT; + }else{ + idxHashAdd(&rc, &p->hIdx, zName, zIdx); + } + } + sqlite3_free(zName); + sqlite3_free(zIdx); + } + } + + sqlite3_free(zCols); + } + return rc; +} + +/* +** Return true if list pList (linked by IdxConstraint.pLink) contains +** a constraint compatible with *p. Otherwise return false. +*/ +static int idxFindConstraint(IdxConstraint *pList, IdxConstraint *p){ + IdxConstraint *pCmp; + for(pCmp=pList; pCmp; pCmp=pCmp->pLink){ + if( p->iCol==pCmp->iCol ) return 1; + } + return 0; +} + +static int idxCreateFromWhere( + sqlite3expert *p, + IdxScan *pScan, /* Create indexes for this scan */ + IdxConstraint *pTail /* range/ORDER BY constraints for inclusion */ +){ + IdxConstraint *p1 = 0; + IdxConstraint *pCon; + int rc; + + /* Gather up all the == constraints. */ + for(pCon=pScan->pEq; pCon; pCon=pCon->pNext){ + if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){ + pCon->pLink = p1; + p1 = pCon; + } + } + + /* Create an index using the == constraints collected above. And the + ** range constraint/ORDER BY terms passed in by the caller, if any. */ + rc = idxCreateFromCons(p, pScan, p1, pTail); + + /* If no range/ORDER BY passed by the caller, create a version of the + ** index for each range constraint. */ + if( pTail==0 ){ + for(pCon=pScan->pRange; rc==SQLITE_OK && pCon; pCon=pCon->pNext){ + assert( pCon->pLink==0 ); + if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){ + rc = idxCreateFromCons(p, pScan, p1, pCon); + } + } + } + + return rc; +} + +/* +** Create candidate indexes in database [dbm] based on the data in +** linked-list pScan. +*/ +static int idxCreateCandidates(sqlite3expert *p){ + int rc = SQLITE_OK; + IdxScan *pIter; + + for(pIter=p->pScan; pIter && rc==SQLITE_OK; pIter=pIter->pNextScan){ + rc = idxCreateFromWhere(p, pIter, 0); + if( rc==SQLITE_OK && pIter->pOrder ){ + rc = idxCreateFromWhere(p, pIter, pIter->pOrder); + } + } + + return rc; +} + +/* +** Free all elements of the linked list starting at pConstraint. +*/ +static void idxConstraintFree(IdxConstraint *pConstraint){ + IdxConstraint *pNext; + IdxConstraint *p; + + for(p=pConstraint; p; p=pNext){ + pNext = p->pNext; + sqlite3_free(p); + } +} + +/* +** Free all elements of the linked list starting from pScan up until pLast +** (pLast is not freed). +*/ +static void idxScanFree(IdxScan *pScan, IdxScan *pLast){ + IdxScan *p; + IdxScan *pNext; + for(p=pScan; p!=pLast; p=pNext){ + pNext = p->pNextScan; + idxConstraintFree(p->pOrder); + idxConstraintFree(p->pEq); + idxConstraintFree(p->pRange); + sqlite3_free(p); + } +} + +/* +** Free all elements of the linked list starting from pStatement up +** until pLast (pLast is not freed). +*/ +static void idxStatementFree(IdxStatement *pStatement, IdxStatement *pLast){ + IdxStatement *p; + IdxStatement *pNext; + for(p=pStatement; p!=pLast; p=pNext){ + pNext = p->pNext; + sqlite3_free(p->zEQP); + sqlite3_free(p->zIdx); + sqlite3_free(p); + } +} + +/* +** Free the linked list of IdxTable objects starting at pTab. +*/ +static void idxTableFree(IdxTable *pTab){ + IdxTable *pIter; + IdxTable *pNext; + for(pIter=pTab; pIter; pIter=pNext){ + pNext = pIter->pNext; + sqlite3_free(pIter); + } +} + +/* +** Free the linked list of IdxWrite objects starting at pTab. +*/ +static void idxWriteFree(IdxWrite *pTab){ + IdxWrite *pIter; + IdxWrite *pNext; + for(pIter=pTab; pIter; pIter=pNext){ + pNext = pIter->pNext; + sqlite3_free(pIter); + } +} + + + +/* +** This function is called after candidate indexes have been created. It +** runs all the queries to see which indexes they prefer, and populates +** IdxStatement.zIdx and IdxStatement.zEQP with the results. +*/ +static int idxFindIndexes( + sqlite3expert *p, + char **pzErr /* OUT: Error message (sqlite3_malloc) */ +){ + IdxStatement *pStmt; + sqlite3 *dbm = p->dbm; + int rc = SQLITE_OK; + + IdxHash hIdx; + idxHashInit(&hIdx); + + for(pStmt=p->pStatement; rc==SQLITE_OK && pStmt; pStmt=pStmt->pNext){ + IdxHashEntry *pEntry; + sqlite3_stmt *pExplain = 0; + idxHashClear(&hIdx); + rc = idxPrintfPrepareStmt(dbm, &pExplain, pzErr, + "EXPLAIN QUERY PLAN %s", pStmt->zSql + ); + while( rc==SQLITE_OK && sqlite3_step(pExplain)==SQLITE_ROW ){ + /* int iId = sqlite3_column_int(pExplain, 0); */ + /* int iParent = sqlite3_column_int(pExplain, 1); */ + /* int iNotUsed = sqlite3_column_int(pExplain, 2); */ + const char *zDetail = (const char*)sqlite3_column_text(pExplain, 3); + int nDetail; + int i; + + if( !zDetail ) continue; + nDetail = STRLEN(zDetail); + + for(i=0; ihIdx, zIdx, nIdx); + if( zSql ){ + idxHashAdd(&rc, &hIdx, zSql, 0); + if( rc ) goto find_indexes_out; + } + break; + } + } + + if( zDetail[0]!='-' ){ + pStmt->zEQP = idxAppendText(&rc, pStmt->zEQP, "%s\n", zDetail); + } + } + + for(pEntry=hIdx.pFirst; pEntry; pEntry=pEntry->pNext){ + pStmt->zIdx = idxAppendText(&rc, pStmt->zIdx, "%s;\n", pEntry->zKey); + } + + idxFinalize(&rc, pExplain); + } + + find_indexes_out: + idxHashClear(&hIdx); + return rc; +} + +static int idxAuthCallback( + void *pCtx, + int eOp, + const char *z3, + const char *z4, + const char *zDb, + const char *zTrigger +){ + int rc = SQLITE_OK; + (void)z4; + (void)zTrigger; + if( eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE || eOp==SQLITE_DELETE ){ + if( sqlite3_stricmp(zDb, "main")==0 ){ + sqlite3expert *p = (sqlite3expert*)pCtx; + IdxTable *pTab; + for(pTab=p->pTable; pTab; pTab=pTab->pNext){ + if( 0==sqlite3_stricmp(z3, pTab->zName) ) break; + } + if( pTab ){ + IdxWrite *pWrite; + for(pWrite=p->pWrite; pWrite; pWrite=pWrite->pNext){ + if( pWrite->pTab==pTab && pWrite->eOp==eOp ) break; + } + if( pWrite==0 ){ + pWrite = idxMalloc(&rc, sizeof(IdxWrite)); + if( rc==SQLITE_OK ){ + pWrite->pTab = pTab; + pWrite->eOp = eOp; + pWrite->pNext = p->pWrite; + p->pWrite = pWrite; + } + } + } + } + } + return rc; +} + +static int idxProcessOneTrigger( + sqlite3expert *p, + IdxWrite *pWrite, + char **pzErr +){ + static const char *zInt = UNIQUE_TABLE_NAME; + static const char *zDrop = "DROP TABLE " UNIQUE_TABLE_NAME; + IdxTable *pTab = pWrite->pTab; + const char *zTab = pTab->zName; + const char *zSql = + "SELECT 'CREATE TEMP' || substr(sql, 7) FROM sqlite_schema " + "WHERE tbl_name = %Q AND type IN ('table', 'trigger') " + "ORDER BY type;"; + sqlite3_stmt *pSelect = 0; + int rc = SQLITE_OK; + char *zWrite = 0; + + /* Create the table and its triggers in the temp schema */ + rc = idxPrintfPrepareStmt(p->db, &pSelect, pzErr, zSql, zTab, zTab); + while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSelect) ){ + const char *zCreate = (const char*)sqlite3_column_text(pSelect, 0); + if( zCreate==0 ) continue; + rc = sqlite3_exec(p->dbv, zCreate, 0, 0, pzErr); + } + idxFinalize(&rc, pSelect); + + /* Rename the table in the temp schema to zInt */ + if( rc==SQLITE_OK ){ + char *z = sqlite3_mprintf("ALTER TABLE temp.%Q RENAME TO %Q", zTab, zInt); + if( z==0 ){ + rc = SQLITE_NOMEM; + }else{ + rc = sqlite3_exec(p->dbv, z, 0, 0, pzErr); + sqlite3_free(z); + } + } + + switch( pWrite->eOp ){ + case SQLITE_INSERT: { + int i; + zWrite = idxAppendText(&rc, zWrite, "INSERT INTO %Q VALUES(", zInt); + for(i=0; inCol; i++){ + zWrite = idxAppendText(&rc, zWrite, "%s?", i==0 ? "" : ", "); + } + zWrite = idxAppendText(&rc, zWrite, ")"); + break; + } + case SQLITE_UPDATE: { + int i; + zWrite = idxAppendText(&rc, zWrite, "UPDATE %Q SET ", zInt); + for(i=0; inCol; i++){ + zWrite = idxAppendText(&rc, zWrite, "%s%Q=?", i==0 ? "" : ", ", + pTab->aCol[i].zName + ); + } + break; + } + default: { + assert( pWrite->eOp==SQLITE_DELETE ); + if( rc==SQLITE_OK ){ + zWrite = sqlite3_mprintf("DELETE FROM %Q", zInt); + if( zWrite==0 ) rc = SQLITE_NOMEM; + } + } + } + + if( rc==SQLITE_OK ){ + sqlite3_stmt *pX = 0; + rc = sqlite3_prepare_v2(p->dbv, zWrite, -1, &pX, 0); + idxFinalize(&rc, pX); + if( rc!=SQLITE_OK ){ + idxDatabaseError(p->dbv, pzErr); + } + } + sqlite3_free(zWrite); + + if( rc==SQLITE_OK ){ + rc = sqlite3_exec(p->dbv, zDrop, 0, 0, pzErr); + } + + return rc; +} + +static int idxProcessTriggers(sqlite3expert *p, char **pzErr){ + int rc = SQLITE_OK; + IdxWrite *pEnd = 0; + IdxWrite *pFirst = p->pWrite; + + while( rc==SQLITE_OK && pFirst!=pEnd ){ + IdxWrite *pIter; + for(pIter=pFirst; rc==SQLITE_OK && pIter!=pEnd; pIter=pIter->pNext){ + rc = idxProcessOneTrigger(p, pIter, pzErr); + } + pEnd = pFirst; + pFirst = p->pWrite; + } + + return rc; +} + +/* +** This function tests if the schema of the main database of database handle +** db contains an object named zTab. Assuming no error occurs, output parameter +** (*pbContains) is set to true if zTab exists, or false if it does not. +** +** Or, if an error occurs, an SQLite error code is returned. The final value +** of (*pbContains) is undefined in this case. +*/ +static int expertDbContainsObject( + sqlite3 *db, + const char *zTab, + int *pbContains /* OUT: True if object exists */ +){ + const char *zSql = "SELECT 1 FROM sqlite_schema WHERE name = ?"; + sqlite3_stmt *pSql = 0; + int rc = SQLITE_OK; + int ret = 0; + + rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0); + if( rc==SQLITE_OK ){ + sqlite3_bind_text(pSql, 1, zTab, -1, SQLITE_STATIC); + if( SQLITE_ROW==sqlite3_step(pSql) ){ + ret = 1; + } + rc = sqlite3_finalize(pSql); + } + + *pbContains = ret; + return rc; +} + +/* +** Execute SQL command zSql using database handle db. If no error occurs, +** set (*pzErr) to NULL and return SQLITE_OK. +** +** If an error does occur, return an SQLite error code and set (*pzErr) to +** point to a buffer containing an English language error message. Except, +** if the error message begins with "no such module:", then ignore the +** error and return as if the SQL statement had succeeded. +** +** This is used to copy as much of the database schema as possible while +** ignoring any errors related to missing virtual table modules. +*/ +static int expertSchemaSql(sqlite3 *db, const char *zSql, char **pzErr){ + int rc = SQLITE_OK; + char *zErr = 0; + + rc = sqlite3_exec(db, zSql, 0, 0, &zErr); + if( rc!=SQLITE_OK && zErr ){ + int nErr = STRLEN(zErr); + if( nErr>=15 && memcmp(zErr, "no such module:", 15)==0 ){ + sqlite3_free(zErr); + rc = SQLITE_OK; + zErr = 0; + } + } + + *pzErr = zErr; + return rc; +} + +static int idxCreateVtabSchema(sqlite3expert *p, char **pzErrmsg){ + int rc = idxRegisterVtab(p); + sqlite3_stmt *pSchema = 0; + + /* For each table in the main db schema: + ** + ** 1) Add an entry to the p->pTable list, and + ** 2) Create the equivalent virtual table in dbv. + */ + rc = idxPrepareStmt(p->db, &pSchema, pzErrmsg, + "SELECT type, name, sql, 1, " + " substr(sql,1,14)=='create virtual' COLLATE nocase " + "FROM sqlite_schema " + "WHERE type IN ('table','view') AND " + " substr(name,1,7)!='sqlite_' COLLATE nocase " + " UNION ALL " + "SELECT type, name, sql, 2, 0 FROM sqlite_schema " + "WHERE type = 'trigger'" + " AND tbl_name IN(SELECT name FROM sqlite_schema WHERE type = 'view') " + "ORDER BY 4, 5 DESC, 1" + ); + while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSchema) ){ + const char *zType = (const char*)sqlite3_column_text(pSchema, 0); + const char *zName = (const char*)sqlite3_column_text(pSchema, 1); + const char *zSql = (const char*)sqlite3_column_text(pSchema, 2); + int bVirtual = sqlite3_column_int(pSchema, 4); + int bExists = 0; + + if( zType==0 || zName==0 ) continue; + rc = expertDbContainsObject(p->dbv, zName, &bExists); + if( rc || bExists ) continue; + + if( zType[0]=='v' || zType[1]=='r' || bVirtual ){ + /* A view. Or a trigger on a view. */ + if( zSql ) rc = expertSchemaSql(p->dbv, zSql, pzErrmsg); + }else{ + IdxTable *pTab; + rc = idxGetTableInfo(p->db, zName, &pTab, pzErrmsg); + if( rc==SQLITE_OK && ALWAYS(pTab!=0) ){ + int i; + char *zInner = 0; + char *zOuter = 0; + pTab->pNext = p->pTable; + p->pTable = pTab; + + /* The statement the vtab will pass to sqlite3_declare_vtab() */ + zInner = idxAppendText(&rc, 0, "CREATE TABLE x("); + for(i=0; inCol; i++){ + zInner = idxAppendText(&rc, zInner, "%s%Q COLLATE %s", + (i==0 ? "" : ", "), pTab->aCol[i].zName, pTab->aCol[i].zColl + ); + } + zInner = idxAppendText(&rc, zInner, ")"); + + /* The CVT statement to create the vtab */ + zOuter = idxAppendText(&rc, 0, + "CREATE VIRTUAL TABLE %Q USING expert(%Q)", zName, zInner + ); + if( rc==SQLITE_OK ){ + rc = sqlite3_exec(p->dbv, zOuter, 0, 0, pzErrmsg); + } + sqlite3_free(zInner); + sqlite3_free(zOuter); + } + } + } + idxFinalize(&rc, pSchema); + return rc; +} + +struct IdxSampleCtx { + int iTarget; + double target; /* Target nRet/nRow value */ + double nRow; /* Number of rows seen */ + double nRet; /* Number of rows returned */ +}; + +static void idxSampleFunc( + sqlite3_context *pCtx, + int argc, + sqlite3_value **argv +){ + struct IdxSampleCtx *p = (struct IdxSampleCtx*)sqlite3_user_data(pCtx); + int bRet; + + (void)argv; + assert( argc==0 ); + if( p->nRow==0.0 ){ + bRet = 1; + }else{ + bRet = (p->nRet / p->nRow) <= p->target; + if( bRet==0 ){ + unsigned short rnd; + sqlite3_randomness(2, (void*)&rnd); + bRet = ((int)rnd % 100) <= p->iTarget; + } + } + + sqlite3_result_int(pCtx, bRet); + p->nRow += 1.0; + p->nRet += (double)bRet; +} + +struct IdxRemCtx { + int nSlot; + struct IdxRemSlot { + int eType; /* SQLITE_NULL, INTEGER, REAL, TEXT, BLOB */ + i64 iVal; /* SQLITE_INTEGER value */ + double rVal; /* SQLITE_FLOAT value */ + i64 nByte; /* Bytes of space allocated at z */ + i64 n; /* Size of buffer z */ + char *z; /* SQLITE_TEXT/BLOB value */ + } aSlot[1]; +}; + +/* +** Implementation of scalar function sqlite_expert_rem(). +*/ +static void idxRemFunc( + sqlite3_context *pCtx, + int argc, + sqlite3_value **argv +){ + struct IdxRemCtx *p = (struct IdxRemCtx*)sqlite3_user_data(pCtx); + struct IdxRemSlot *pSlot; + int iSlot; + assert( argc==2 ); + + iSlot = sqlite3_value_int(argv[0]); + assert( iSlotnSlot ); + pSlot = &p->aSlot[iSlot]; + + switch( pSlot->eType ){ + case SQLITE_NULL: + /* no-op */ + break; + + case SQLITE_INTEGER: + sqlite3_result_int64(pCtx, pSlot->iVal); + break; + + case SQLITE_FLOAT: + sqlite3_result_double(pCtx, pSlot->rVal); + break; + + case SQLITE_BLOB: + assert( pSlot->n <= 0x7fffffff ); + sqlite3_result_blob(pCtx, pSlot->z, (int)pSlot->n, SQLITE_TRANSIENT); + break; + + case SQLITE_TEXT: + assert( pSlot->n <= 0x7fffffff ); + sqlite3_result_text(pCtx, pSlot->z, (int)pSlot->n, SQLITE_TRANSIENT); + break; + } + + pSlot->eType = sqlite3_value_type(argv[1]); + switch( pSlot->eType ){ + case SQLITE_NULL: + /* no-op */ + break; + + case SQLITE_INTEGER: + pSlot->iVal = sqlite3_value_int64(argv[1]); + break; + + case SQLITE_FLOAT: + pSlot->rVal = sqlite3_value_double(argv[1]); + break; + + case SQLITE_BLOB: + case SQLITE_TEXT: { + i64 nByte = sqlite3_value_bytes(argv[1]); + const void *pData = 0; + if( nByte>pSlot->nByte ){ + char *zNew = (char*)sqlite3_realloc64(pSlot->z, nByte*2); + if( zNew==0 ){ + sqlite3_result_error_nomem(pCtx); + return; + } + pSlot->nByte = nByte*2; + pSlot->z = zNew; + } + pSlot->n = nByte; + if( pSlot->eType==SQLITE_BLOB ){ + pData = sqlite3_value_blob(argv[1]); + if( pData ) memcpy(pSlot->z, pData, nByte); + }else{ + pData = sqlite3_value_text(argv[1]); + memcpy(pSlot->z, pData, nByte); + } + break; + } + } +} + +static int idxLargestIndex(sqlite3 *db, int *pnMax, char **pzErr){ + int rc = SQLITE_OK; + const char *zMax = + "SELECT max(i.seqno) FROM " + " sqlite_schema AS s, " + " pragma_index_list(s.name) AS l, " + " pragma_index_info(l.name) AS i " + "WHERE s.type = 'table'"; + sqlite3_stmt *pMax = 0; + + *pnMax = 0; + rc = idxPrepareStmt(db, &pMax, pzErr, zMax); + if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pMax) ){ + *pnMax = sqlite3_column_int(pMax, 0) + 1; + } + idxFinalize(&rc, pMax); + + return rc; +} + +static int idxPopulateOneStat1( + sqlite3expert *p, + sqlite3_stmt *pIndexXInfo, + sqlite3_stmt *pWriteStat, + const char *zTab, + const char *zIdx, + char **pzErr +){ + char *zCols = 0; + char *zOrder = 0; + char *zQuery = 0; + int nCol = 0; + int i; + sqlite3_stmt *pQuery = 0; + i64 *aStat = 0; + int rc = SQLITE_OK; + + assert( p->iSample>0 ); + + /* Formulate the query text */ + sqlite3_bind_text(pIndexXInfo, 1, zIdx, -1, SQLITE_STATIC); + while( SQLITE_OK==rc && SQLITE_ROW==sqlite3_step(pIndexXInfo) ){ + const char *zComma = zCols==0 ? "" : ", "; + const char *zName = (const char*)sqlite3_column_text(pIndexXInfo, 0); + const char *zColl = (const char*)sqlite3_column_text(pIndexXInfo, 1); + if( zName==0 ){ + /* This index contains an expression. Ignore it. */ + sqlite3_free(zCols); + sqlite3_free(zOrder); + return sqlite3_reset(pIndexXInfo); + } + zCols = idxAppendText(&rc, zCols, + "%sx.%Q IS sqlite_expert_rem(%d, x.%Q) COLLATE %s", + zComma, zName, nCol, zName, zColl + ); + zOrder = idxAppendText(&rc, zOrder, "%s%d", zComma, ++nCol); + } + sqlite3_reset(pIndexXInfo); + if( rc==SQLITE_OK ){ + if( p->iSample==100 ){ + zQuery = sqlite3_mprintf( + "SELECT %s FROM %Q x ORDER BY %s", zCols, zTab, zOrder + ); + }else{ + zQuery = sqlite3_mprintf( + "SELECT %s FROM temp."UNIQUE_TABLE_NAME" x ORDER BY %s", zCols, zOrder + ); + } + } + sqlite3_free(zCols); + sqlite3_free(zOrder); + + /* Formulate the query text */ + if( rc==SQLITE_OK ){ + sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv); + rc = idxPrepareStmt(dbrem, &pQuery, pzErr, zQuery); + } + sqlite3_free(zQuery); + + if( rc==SQLITE_OK ){ + aStat = (i64*)idxMalloc(&rc, sizeof(i64)*(nCol+1)); + } + if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){ + IdxHashEntry *pEntry; + char *zStat = 0; + for(i=0; i<=nCol; i++) aStat[i] = 1; + while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){ + aStat[0]++; + for(i=0; ihIdx, zIdx, STRLEN(zIdx)); + if( pEntry ){ + assert( pEntry->zVal2==0 ); + pEntry->zVal2 = zStat; + }else{ + sqlite3_free(zStat); + } + } + sqlite3_free(aStat); + idxFinalize(&rc, pQuery); + + return rc; +} + +static int idxBuildSampleTable(sqlite3expert *p, const char *zTab){ + int rc; + char *zSql; + + rc = sqlite3_exec(p->dbv,"DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0); + if( rc!=SQLITE_OK ) return rc; + + zSql = sqlite3_mprintf( + "CREATE TABLE temp." UNIQUE_TABLE_NAME " AS SELECT * FROM %Q", zTab + ); + if( zSql==0 ) return SQLITE_NOMEM; + rc = sqlite3_exec(p->dbv, zSql, 0, 0, 0); + sqlite3_free(zSql); + + return rc; +} + +/* +** This function is called as part of sqlite3_expert_analyze(). Candidate +** indexes have already been created in database sqlite3expert.dbm, this +** function populates sqlite_stat1 table in the same database. +** +** The stat1 data is generated by querying the +*/ +static int idxPopulateStat1(sqlite3expert *p, char **pzErr){ + int rc = SQLITE_OK; + int nMax =0; + struct IdxRemCtx *pCtx = 0; + struct IdxSampleCtx samplectx; + int i; + i64 iPrev = -100000; + sqlite3_stmt *pAllIndex = 0; + sqlite3_stmt *pIndexXInfo = 0; + sqlite3_stmt *pWrite = 0; + + const char *zAllIndex = + "SELECT s.rowid, s.name, l.name FROM " + " sqlite_schema AS s, " + " pragma_index_list(s.name) AS l " + "WHERE s.type = 'table'"; + const char *zIndexXInfo = + "SELECT name, coll FROM pragma_index_xinfo(?) WHERE key"; + const char *zWrite = "INSERT INTO sqlite_stat1 VALUES(?, ?, ?)"; + + /* If iSample==0, no sqlite_stat1 data is required. */ + if( p->iSample==0 ) return SQLITE_OK; + + rc = idxLargestIndex(p->dbm, &nMax, pzErr); + if( nMax<=0 || rc!=SQLITE_OK ) return rc; + + rc = sqlite3_exec(p->dbm, "ANALYZE; PRAGMA writable_schema=1", 0, 0, 0); + + if( rc==SQLITE_OK ){ + i64 nByte = sizeof(struct IdxRemCtx) + (sizeof(struct IdxRemSlot) * nMax); + pCtx = (struct IdxRemCtx*)idxMalloc(&rc, nByte); + } + + if( rc==SQLITE_OK ){ + sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv); + rc = sqlite3_create_function(dbrem, "sqlite_expert_rem", + 2, SQLITE_UTF8, (void*)pCtx, idxRemFunc, 0, 0 + ); + } + if( rc==SQLITE_OK ){ + rc = sqlite3_create_function(p->db, "sqlite_expert_sample", + 0, SQLITE_UTF8, (void*)&samplectx, idxSampleFunc, 0, 0 + ); + } + + if( rc==SQLITE_OK ){ + pCtx->nSlot = (i64)nMax+1; + rc = idxPrepareStmt(p->dbm, &pAllIndex, pzErr, zAllIndex); + } + if( rc==SQLITE_OK ){ + rc = idxPrepareStmt(p->dbm, &pIndexXInfo, pzErr, zIndexXInfo); + } + if( rc==SQLITE_OK ){ + rc = idxPrepareStmt(p->dbm, &pWrite, pzErr, zWrite); + } + + while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pAllIndex) ){ + i64 iRowid = sqlite3_column_int64(pAllIndex, 0); + const char *zTab = (const char*)sqlite3_column_text(pAllIndex, 1); + const char *zIdx = (const char*)sqlite3_column_text(pAllIndex, 2); + if( zTab==0 || zIdx==0 ) continue; + if( p->iSample<100 && iPrev!=iRowid ){ + samplectx.target = (double)p->iSample / 100.0; + samplectx.iTarget = p->iSample; + samplectx.nRow = 0.0; + samplectx.nRet = 0.0; + rc = idxBuildSampleTable(p, zTab); + if( rc!=SQLITE_OK ) break; + } + rc = idxPopulateOneStat1(p, pIndexXInfo, pWrite, zTab, zIdx, pzErr); + iPrev = iRowid; + } + if( rc==SQLITE_OK && p->iSample<100 ){ + rc = sqlite3_exec(p->dbv, + "DROP TABLE IF EXISTS temp." UNIQUE_TABLE_NAME, 0,0,0 + ); + } + + idxFinalize(&rc, pAllIndex); + idxFinalize(&rc, pIndexXInfo); + idxFinalize(&rc, pWrite); + + if( pCtx ){ + for(i=0; inSlot; i++){ + sqlite3_free(pCtx->aSlot[i].z); + } + sqlite3_free(pCtx); + } + + if( rc==SQLITE_OK ){ + rc = sqlite3_exec(p->dbm, "ANALYZE sqlite_schema", 0, 0, 0); + } + + sqlite3_create_function(p->db, "sqlite_expert_rem", 2, SQLITE_UTF8, 0,0,0,0); + sqlite3_create_function(p->db, "sqlite_expert_sample", 0,SQLITE_UTF8,0,0,0,0); + + sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0); + return rc; +} + +/* +** Define and possibly pretend to use a useless collation sequence. +** This pretense allows expert to accept SQL using custom collations. +*/ +int dummyCompare(void *up1, int up2, const void *up3, int up4, const void *up5){ + (void)up1; + (void)up2; + (void)up3; + (void)up4; + (void)up5; + assert(0); /* VDBE should never be run. */ + return 0; +} +/* And a callback to register above upon actual need */ +void useDummyCS(void *up1, sqlite3 *db, int etr, const char *zName){ + (void)up1; + sqlite3_create_collation_v2(db, zName, etr, 0, dummyCompare, 0); +} + +#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) \ + && !defined(SQLITE_OMIT_INTROSPECTION_PRAGMAS) +/* +** dummy functions for no-op implementation of UDFs during expert's work +*/ +void dummyUDF(sqlite3_context *up1, int up2, sqlite3_value **up3){ + (void)up1; + (void)up2; + (void)up3; + assert(0); /* VDBE should never be run. */ +} +void dummyUDFvalue(sqlite3_context *up1){ + (void)up1; + assert(0); /* VDBE should never be run. */ +} + +/* +** Register UDFs from user database with another. +*/ +int registerUDFs(sqlite3 *dbSrc, sqlite3 *dbDst){ + sqlite3_stmt *pStmt; + int rc = sqlite3_prepare_v2(dbSrc, + "SELECT name,type,enc,narg,flags " + "FROM pragma_function_list() " + "WHERE builtin==0", -1, &pStmt, 0); + if( rc==SQLITE_OK ){ + while( SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){ + int nargs = sqlite3_column_int(pStmt,3); + int flags = sqlite3_column_int(pStmt,4); + const char *name = (char*)sqlite3_column_text(pStmt,0); + const char *type = (char*)sqlite3_column_text(pStmt,1); + const char *enc = (char*)sqlite3_column_text(pStmt,2); + if( name==0 || type==0 || enc==0 ){ + /* no-op. Only happens on OOM */ + }else{ + int ienc = SQLITE_UTF8; + int rcf = SQLITE_ERROR; + if( strcmp(enc,"utf16le")==0 ) ienc = SQLITE_UTF16LE; + else if( strcmp(enc,"utf16be")==0 ) ienc = SQLITE_UTF16BE; + ienc |= (flags & (SQLITE_DETERMINISTIC|SQLITE_DIRECTONLY)); + if( strcmp(type,"w")==0 ){ + rcf = sqlite3_create_window_function(dbDst,name,nargs,ienc,0, + dummyUDF,dummyUDFvalue,0,0,0); + }else if( strcmp(type,"a")==0 ){ + rcf = sqlite3_create_function(dbDst,name,nargs,ienc,0, + 0,dummyUDF,dummyUDFvalue); + }else if( strcmp(type,"s")==0 ){ + rcf = sqlite3_create_function(dbDst,name,nargs,ienc,0, + dummyUDF,0,0); + } + if( rcf!=SQLITE_OK ){ + rc = rcf; + break; + } + } + } + sqlite3_finalize(pStmt); + if( rc==SQLITE_DONE ) rc = SQLITE_OK; + } + return rc; +} +#endif + +/* +** Allocate a new sqlite3expert object. +*/ +sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErrmsg){ + int rc = SQLITE_OK; + sqlite3expert *pNew; + + pNew = (sqlite3expert*)idxMalloc(&rc, sizeof(sqlite3expert)); + + /* Open two in-memory databases to work with. The "vtab database" (dbv) + ** will contain a virtual table corresponding to each real table in + ** the user database schema, and a copy of each view. It is used to + ** collect information regarding the WHERE, ORDER BY and other clauses + ** of the user's query. + */ + if( rc==SQLITE_OK ){ + pNew->db = db; + pNew->iSample = 100; + rc = sqlite3_open(":memory:", &pNew->dbv); + } + if( rc==SQLITE_OK ){ + rc = sqlite3_open(":memory:", &pNew->dbm); + if( rc==SQLITE_OK ){ + sqlite3_db_config(pNew->dbm, SQLITE_DBCONFIG_TRIGGER_EQP, 1, (int*)0); + } + } + + /* Allow custom collations to be dealt with through prepare. */ + if( rc==SQLITE_OK ) rc = sqlite3_collation_needed(pNew->dbm,0,useDummyCS); + if( rc==SQLITE_OK ) rc = sqlite3_collation_needed(pNew->dbv,0,useDummyCS); + +#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) \ + && !defined(SQLITE_OMIT_INTROSPECTION_PRAGMAS) + /* Register UDFs from database [db] with [dbm] and [dbv]. */ + if( rc==SQLITE_OK ){ + rc = registerUDFs(pNew->db, pNew->dbm); + } + if( rc==SQLITE_OK ){ + rc = registerUDFs(pNew->db, pNew->dbv); + } +#endif + + /* Copy the entire schema of database [db] into [dbm]. */ + if( rc==SQLITE_OK ){ + sqlite3_stmt *pSql = 0; + rc = idxPrintfPrepareStmt(pNew->db, &pSql, pzErrmsg, + "SELECT sql, name, substr(sql,1,14)=='create virtual' COLLATE nocase" + " FROM sqlite_schema WHERE substr(name,1,7)!='sqlite_' COLLATE nocase" + " ORDER BY 3 DESC, rowid" + ); + while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){ + const char *zSql = (const char*)sqlite3_column_text(pSql, 0); + const char *zName = (const char*)sqlite3_column_text(pSql, 1); + int bExists = 0; + rc = expertDbContainsObject(pNew->dbm, zName, &bExists); + if( rc==SQLITE_OK && zSql && bExists==0 ){ + rc = expertSchemaSql(pNew->dbm, zSql, pzErrmsg); + } + } + idxFinalize(&rc, pSql); + } + + /* Create the vtab schema */ + if( rc==SQLITE_OK ){ + rc = idxCreateVtabSchema(pNew, pzErrmsg); + } + + /* Register the auth callback with dbv */ + if( rc==SQLITE_OK ){ + sqlite3_set_authorizer(pNew->dbv, idxAuthCallback, (void*)pNew); + } + + /* If an error has occurred, free the new object and return NULL. Otherwise, + ** return the new sqlite3expert handle. */ + if( rc!=SQLITE_OK ){ + sqlite3_expert_destroy(pNew); + pNew = 0; + } + return pNew; +} + +/* +** Configure an sqlite3expert object. +*/ +int sqlite3_expert_config(sqlite3expert *p, int op, ...){ + int rc = SQLITE_OK; + va_list ap; + va_start(ap, op); + switch( op ){ + case EXPERT_CONFIG_SAMPLE: { + int iVal = va_arg(ap, int); + if( iVal<0 ) iVal = 0; + if( iVal>100 ) iVal = 100; + p->iSample = iVal; + break; + } + default: + rc = SQLITE_NOTFOUND; + break; + } + + va_end(ap); + return rc; +} + +/* +** Add an SQL statement to the analysis. +*/ +int sqlite3_expert_sql( + sqlite3expert *p, /* From sqlite3_expert_new() */ + const char *zSql, /* SQL statement to add */ + char **pzErr /* OUT: Error message (if any) */ +){ + IdxScan *pScanOrig = p->pScan; + IdxStatement *pStmtOrig = p->pStatement; + int rc = SQLITE_OK; + const char *zStmt = zSql; + + if( p->bRun ) return SQLITE_MISUSE; + + while( rc==SQLITE_OK && zStmt && zStmt[0] ){ + sqlite3_stmt *pStmt = 0; + /* Ensure that the provided statement compiles against user's DB. */ + rc = idxPrepareStmt(p->db, &pStmt, pzErr, zStmt); + if( rc!=SQLITE_OK ) break; + sqlite3_finalize(pStmt); + rc = sqlite3_prepare_v2(p->dbv, zStmt, -1, &pStmt, &zStmt); + if( rc==SQLITE_OK ){ + if( pStmt ){ + IdxStatement *pNew; + const char *z = sqlite3_sql(pStmt); + i64 n = STRLEN(z); + pNew = (IdxStatement*)idxMalloc(&rc, sizeof(IdxStatement) + n+1); + if( rc==SQLITE_OK ){ + pNew->zSql = (char*)&pNew[1]; + memcpy(pNew->zSql, z, n+1); + pNew->pNext = p->pStatement; + if( p->pStatement ) pNew->iId = p->pStatement->iId+1; + p->pStatement = pNew; + } + sqlite3_finalize(pStmt); + } + }else{ + idxDatabaseError(p->dbv, pzErr); + } + } + + if( rc!=SQLITE_OK ){ + idxScanFree(p->pScan, pScanOrig); + idxStatementFree(p->pStatement, pStmtOrig); + p->pScan = pScanOrig; + p->pStatement = pStmtOrig; + } + + return rc; +} + +int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr){ + int rc; + IdxHashEntry *pEntry; + + /* Do trigger processing to collect any extra IdxScan structures */ + rc = idxProcessTriggers(p, pzErr); + + /* Create candidate indexes within the in-memory database file */ + if( rc==SQLITE_OK ){ + rc = idxCreateCandidates(p); + }else if ( rc==SQLITE_BUSY_TIMEOUT ){ + if( pzErr ) + *pzErr = sqlite3_mprintf("Cannot find a unique index name to propose."); + return rc; + } + + /* Generate the stat1 data */ + if( rc==SQLITE_OK ){ + rc = idxPopulateStat1(p, pzErr); + } + + /* Formulate the EXPERT_REPORT_CANDIDATES text */ + for(pEntry=p->hIdx.pFirst; pEntry; pEntry=pEntry->pNext){ + p->zCandidates = idxAppendText(&rc, p->zCandidates, + "%s;%s%s\n", pEntry->zVal, + pEntry->zVal2 ? " -- stat1: " : "", pEntry->zVal2 + ); + } + + /* Figure out which of the candidate indexes are preferred by the query + ** planner and report the results to the user. */ + if( rc==SQLITE_OK ){ + rc = idxFindIndexes(p, pzErr); + } + + if( rc==SQLITE_OK ){ + p->bRun = 1; + } + return rc; +} + +/* +** Return the total number of statements that have been added to this +** sqlite3expert using sqlite3_expert_sql(). +*/ +int sqlite3_expert_count(sqlite3expert *p){ + int nRet = 0; + if( p->pStatement ) nRet = p->pStatement->iId+1; + return nRet; +} + +/* +** Return a component of the report. +*/ +const char *sqlite3_expert_report(sqlite3expert *p, int iStmt, int eReport){ + const char *zRet = 0; + IdxStatement *pStmt; + + if( p->bRun==0 ) return 0; + for(pStmt=p->pStatement; pStmt && pStmt->iId!=iStmt; pStmt=pStmt->pNext); + switch( eReport ){ + case EXPERT_REPORT_SQL: + if( pStmt ) zRet = pStmt->zSql; + break; + case EXPERT_REPORT_INDEXES: + if( pStmt ) zRet = pStmt->zIdx; + break; + case EXPERT_REPORT_PLAN: + if( pStmt ) zRet = pStmt->zEQP; + break; + case EXPERT_REPORT_CANDIDATES: + zRet = p->zCandidates; + break; + } + return zRet; +} + +/* +** Free an sqlite3expert object. +*/ +void sqlite3_expert_destroy(sqlite3expert *p){ + if( p ){ + sqlite3_close(p->dbm); + sqlite3_close(p->dbv); + idxScanFree(p->pScan, 0); + idxStatementFree(p->pStatement, 0); + idxTableFree(p->pTable); + idxWriteFree(p->pWrite); + idxHashClear(&p->hIdx); + sqlite3_free(p->zCandidates); + sqlite3_free(p); + } +} + +#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */ diff --git a/ext/expert/sqlite3expert.h b/ext/expert/sqlite3expert.h new file mode 100644 index 0000000000..6048137237 --- /dev/null +++ b/ext/expert/sqlite3expert.h @@ -0,0 +1,168 @@ +/* +** 2017 April 07 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +*/ +#if !defined(SQLITEEXPERT_H) +#define SQLITEEXPERT_H 1 +#include "sqlite3.h" + +typedef struct sqlite3expert sqlite3expert; + +/* +** Create a new sqlite3expert object. +** +** If successful, a pointer to the new object is returned and (*pzErr) set +** to NULL. Or, if an error occurs, NULL is returned and (*pzErr) set to +** an English-language error message. In this case it is the responsibility +** of the caller to eventually free the error message buffer using +** sqlite3_free(). +*/ +sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErr); + +/* +** Configure an sqlite3expert object. +** +** EXPERT_CONFIG_SAMPLE: +** By default, sqlite3_expert_analyze() generates sqlite_stat1 data for +** each candidate index. This involves scanning and sorting the entire +** contents of each user database table once for each candidate index +** associated with the table. For large databases, this can be +** prohibitively slow. This option allows the sqlite3expert object to +** be configured so that sqlite_stat1 data is instead generated based on a +** subset of each table, or so that no sqlite_stat1 data is used at all. +** +** A single integer argument is passed to this option. If the value is less +** than or equal to zero, then no sqlite_stat1 data is generated or used by +** the analysis - indexes are recommended based on the database schema only. +** Or, if the value is 100 or greater, complete sqlite_stat1 data is +** generated for each candidate index (this is the default). Finally, if the +** value falls between 0 and 100, then it represents the percentage of user +** table rows that should be considered when generating sqlite_stat1 data. +** +** Examples: +** +** // Do not generate any sqlite_stat1 data +** sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 0); +** +** // Generate sqlite_stat1 data based on 10% of the rows in each table. +** sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 10); +*/ +int sqlite3_expert_config(sqlite3expert *p, int op, ...); + +#define EXPERT_CONFIG_SAMPLE 1 /* int */ + +/* +** Specify zero or more SQL statements to be included in the analysis. +** +** Buffer zSql must contain zero or more complete SQL statements. This +** function parses all statements contained in the buffer and adds them +** to the internal list of statements to analyze. If successful, SQLITE_OK +** is returned and (*pzErr) set to NULL. Or, if an error occurs - for example +** due to a error in the SQL - an SQLite error code is returned and (*pzErr) +** may be set to point to an English language error message. In this case +** the caller is responsible for eventually freeing the error message buffer +** using sqlite3_free(). +** +** If an error does occur while processing one of the statements in the +** buffer passed as the second argument, none of the statements in the +** buffer are added to the analysis. +** +** This function must be called before sqlite3_expert_analyze(). If a call +** to this function is made on an sqlite3expert object that has already +** been passed to sqlite3_expert_analyze() SQLITE_MISUSE is returned +** immediately and no statements are added to the analysis. +*/ +int sqlite3_expert_sql( + sqlite3expert *p, /* From a successful sqlite3_expert_new() */ + const char *zSql, /* SQL statement(s) to add */ + char **pzErr /* OUT: Error message (if any) */ +); + + +/* +** This function is called after the sqlite3expert object has been configured +** with all SQL statements using sqlite3_expert_sql() to actually perform +** the analysis. Once this function has been called, it is not possible to +** add further SQL statements to the analysis. +** +** If successful, SQLITE_OK is returned and (*pzErr) is set to NULL. Or, if +** an error occurs, an SQLite error code is returned and (*pzErr) set to +** point to a buffer containing an English language error message. In this +** case it is the responsibility of the caller to eventually free the buffer +** using sqlite3_free(). +** +** If an error does occur within this function, the sqlite3expert object +** is no longer useful for any purpose. At that point it is no longer +** possible to add further SQL statements to the object or to re-attempt +** the analysis. The sqlite3expert object must still be freed using a call +** sqlite3_expert_destroy(). +*/ +int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr); + +/* +** Return the total number of statements loaded using sqlite3_expert_sql(). +** The total number of SQL statements may be different from the total number +** to calls to sqlite3_expert_sql(). +*/ +int sqlite3_expert_count(sqlite3expert*); + +/* +** Return a component of the report. +** +** This function is called after sqlite3_expert_analyze() to extract the +** results of the analysis. Each call to this function returns either a +** NULL pointer or a pointer to a buffer containing a nul-terminated string. +** The value passed as the third argument must be one of the EXPERT_REPORT_* +** #define constants defined below. +** +** For some EXPERT_REPORT_* parameters, the buffer returned contains +** information relating to a specific SQL statement. In these cases that +** SQL statement is identified by the value passed as the second argument. +** SQL statements are numbered from 0 in the order in which they are parsed. +** If an out-of-range value (less than zero or equal to or greater than the +** value returned by sqlite3_expert_count()) is passed as the second argument +** along with such an EXPERT_REPORT_* parameter, NULL is always returned. +** +** EXPERT_REPORT_SQL: +** Return the text of SQL statement iStmt. +** +** EXPERT_REPORT_INDEXES: +** Return a buffer containing the CREATE INDEX statements for all recommended +** indexes for statement iStmt. If there are no new recommeded indexes, NULL +** is returned. +** +** EXPERT_REPORT_PLAN: +** Return a buffer containing the EXPLAIN QUERY PLAN output for SQL query +** iStmt after the proposed indexes have been added to the database schema. +** +** EXPERT_REPORT_CANDIDATES: +** Return a pointer to a buffer containing the CREATE INDEX statements +** for all indexes that were tested (for all SQL statements). The iStmt +** parameter is ignored for EXPERT_REPORT_CANDIDATES calls. +*/ +const char *sqlite3_expert_report(sqlite3expert*, int iStmt, int eReport); + +/* +** Values for the third argument passed to sqlite3_expert_report(). +*/ +#define EXPERT_REPORT_SQL 1 +#define EXPERT_REPORT_INDEXES 2 +#define EXPERT_REPORT_PLAN 3 +#define EXPERT_REPORT_CANDIDATES 4 + +/* +** Free an (sqlite3expert*) handle and all associated resources. There +** should be one call to this function for each successful call to +** sqlite3-expert_new(). +*/ +void sqlite3_expert_destroy(sqlite3expert*); + +#endif /* !defined(SQLITEEXPERT_H) */ diff --git a/ext/expert/test_expert.c b/ext/expert/test_expert.c new file mode 100644 index 0000000000..4383d7c7bb --- /dev/null +++ b/ext/expert/test_expert.c @@ -0,0 +1,212 @@ +/* +** 2017 April 07 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +*/ + +#if defined(SQLITE_TEST) + +#include "sqlite3expert.h" +#include +#include +#include "tclsqlite.h" + +#ifndef SQLITE_OMIT_VIRTUALTABLE + +/* +** Extract an sqlite3* db handle from the object passed as the second +** argument. If successful, set *pDb to point to the db handle and return +** TCL_OK. Otherwise, return TCL_ERROR. +*/ +static int dbHandleFromObj(Tcl_Interp *interp, Tcl_Obj *pObj, sqlite3 **pDb){ + Tcl_CmdInfo info; + if( 0==Tcl_GetCommandInfo(interp, Tcl_GetString(pObj), &info) ){ + Tcl_AppendResult(interp, "no such handle: ", Tcl_GetString(pObj), NULL); + return TCL_ERROR; + } + + *pDb = *(sqlite3 **)info.objClientData; + return TCL_OK; +} + + +/* +** Tclcmd: $expert sql SQL +** $expert analyze +** $expert count +** $expert report STMT EREPORT +** $expert destroy +*/ +static int SQLITE_TCLAPI testExpertCmd( + void *clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + sqlite3expert *pExpert = (sqlite3expert*)clientData; + struct Subcmd { + const char *zSub; + int nArg; + const char *zMsg; + } aSub[] = { + { "sql", 1, "TABLE", }, /* 0 */ + { "analyze", 0, "", }, /* 1 */ + { "count", 0, "", }, /* 2 */ + { "report", 2, "STMT EREPORT", }, /* 3 */ + { "destroy", 0, "", }, /* 4 */ + { 0 } + }; + int iSub; + int rc = TCL_OK; + char *zErr = 0; + + if( objc<2 ){ + Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ..."); + return TCL_ERROR; + } + rc = Tcl_GetIndexFromObjStruct(interp, + objv[1], aSub, sizeof(aSub[0]), "sub-command", 0, &iSub + ); + if( rc!=TCL_OK ) return rc; + if( objc!=2+aSub[iSub].nArg ){ + Tcl_WrongNumArgs(interp, 2, objv, aSub[iSub].zMsg); + return TCL_ERROR; + } + + switch( iSub ){ + case 0: { /* sql */ + char *zArg = Tcl_GetString(objv[2]); + rc = sqlite3_expert_sql(pExpert, zArg, &zErr); + break; + } + + case 1: { /* analyze */ + rc = sqlite3_expert_analyze(pExpert, &zErr); + break; + } + + case 2: { /* count */ + int n = sqlite3_expert_count(pExpert); + Tcl_SetObjResult(interp, Tcl_NewIntObj(n)); + break; + } + + case 3: { /* report */ + const char *aEnum[] = { + "sql", "indexes", "plan", "candidates", 0 + }; + int iEnum; + int iStmt; + const char *zReport; + + if( Tcl_GetIntFromObj(interp, objv[2], &iStmt) + || Tcl_GetIndexFromObj(interp, objv[3], aEnum, "report", 0, &iEnum) + ){ + return TCL_ERROR; + } + + assert( EXPERT_REPORT_SQL==1 ); + assert( EXPERT_REPORT_INDEXES==2 ); + assert( EXPERT_REPORT_PLAN==3 ); + assert( EXPERT_REPORT_CANDIDATES==4 ); + zReport = sqlite3_expert_report(pExpert, iStmt, 1+iEnum); + Tcl_SetObjResult(interp, Tcl_NewStringObj(zReport, -1)); + break; + } + + default: /* destroy */ + assert( iSub==4 ); + Tcl_DeleteCommand(interp, Tcl_GetString(objv[0])); + break; + } + + if( rc!=TCL_OK ){ + if( zErr ){ + Tcl_SetObjResult(interp, Tcl_NewStringObj(zErr, -1)); + }else{ + extern const char *sqlite3ErrName(int); + Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1)); + } + } + sqlite3_free(zErr); + return rc; +} + +static void SQLITE_TCLAPI testExpertDel(void *clientData){ + sqlite3expert *pExpert = (sqlite3expert*)clientData; + sqlite3_expert_destroy(pExpert); +} + +/* +** sqlite3_expert_new DB +*/ +static int SQLITE_TCLAPI test_sqlite3_expert_new( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + static int iCmd = 0; + sqlite3 *db; + char *zCmd = 0; + char *zErr = 0; + sqlite3expert *pExpert; + int rc = TCL_OK; + + if( objc!=2 ){ + Tcl_WrongNumArgs(interp, 1, objv, "DB"); + return TCL_ERROR; + } + if( dbHandleFromObj(interp, objv[1], &db) ){ + return TCL_ERROR; + } + + zCmd = sqlite3_mprintf("sqlite3expert%d", ++iCmd); + if( zCmd==0 ){ + Tcl_AppendResult(interp, "out of memory", (char*)0); + return TCL_ERROR; + } + + pExpert = sqlite3_expert_new(db, &zErr); + if( pExpert==0 ){ + Tcl_AppendResult(interp, zErr, (char*)0); + rc = TCL_ERROR; + }else{ + void *p = (void*)pExpert; + Tcl_CreateObjCommand(interp, zCmd, testExpertCmd, p, testExpertDel); + Tcl_SetObjResult(interp, Tcl_NewStringObj(zCmd, -1)); + } + + sqlite3_free(zCmd); + sqlite3_free(zErr); + return rc; +} + +#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */ + +int TestExpert_Init(Tcl_Interp *interp){ +#ifndef SQLITE_OMIT_VIRTUALTABLE + struct Cmd { + const char *zCmd; + Tcl_ObjCmdProc *xProc; + } aCmd[] = { + { "sqlite3_expert_new", test_sqlite3_expert_new }, + }; + int i; + + for(i=0; izCmd, p->xProc, 0, 0); + } +#endif + return TCL_OK; +} + +#endif diff --git a/ext/fts1/README.txt b/ext/fts1/README.txt deleted file mode 100644 index 292b7daa0b..0000000000 --- a/ext/fts1/README.txt +++ /dev/null @@ -1,2 +0,0 @@ -This folder contains source code to the first full-text search -extension for SQLite. diff --git a/ext/fts1/ft_hash.c b/ext/fts1/ft_hash.c deleted file mode 100644 index 8b3a7064ee..0000000000 --- a/ext/fts1/ft_hash.c +++ /dev/null @@ -1,404 +0,0 @@ -/* -** 2001 September 22 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -************************************************************************* -** This is the implementation of generic hash-tables used in SQLite. -** We've modified it slightly to serve as a standalone hash table -** implementation for the full-text indexing module. -*/ -#include -#include -#include - -#include "ft_hash.h" - -void *malloc_and_zero(int n){ - void *p = malloc(n); - if( p ){ - memset(p, 0, n); - } - return p; -} - -/* Turn bulk memory into a hash table object by initializing the -** fields of the Hash structure. -** -** "pNew" is a pointer to the hash table that is to be initialized. -** keyClass is one of the constants HASH_INT, HASH_POINTER, -** HASH_BINARY, or HASH_STRING. The value of keyClass -** determines what kind of key the hash table will use. "copyKey" is -** true if the hash table should make its own private copy of keys and -** false if it should just use the supplied pointer. CopyKey only makes -** sense for HASH_STRING and HASH_BINARY and is ignored -** for other key classes. -*/ -void HashInit(Hash *pNew, int keyClass, int copyKey){ - assert( pNew!=0 ); - assert( keyClass>=HASH_STRING && keyClass<=HASH_BINARY ); - pNew->keyClass = keyClass; -#if 0 - if( keyClass==HASH_POINTER || keyClass==HASH_INT ) copyKey = 0; -#endif - pNew->copyKey = copyKey; - pNew->first = 0; - pNew->count = 0; - pNew->htsize = 0; - pNew->ht = 0; - pNew->xMalloc = malloc_and_zero; - pNew->xFree = free; -} - -/* Remove all entries from a hash table. Reclaim all memory. -** Call this routine to delete a hash table or to reset a hash table -** to the empty state. -*/ -void HashClear(Hash *pH){ - HashElem *elem; /* For looping over all elements of the table */ - - assert( pH!=0 ); - elem = pH->first; - pH->first = 0; - if( pH->ht ) pH->xFree(pH->ht); - pH->ht = 0; - pH->htsize = 0; - while( elem ){ - HashElem *next_elem = elem->next; - if( pH->copyKey && elem->pKey ){ - pH->xFree(elem->pKey); - } - pH->xFree(elem); - elem = next_elem; - } - pH->count = 0; -} - -#if 0 /* NOT USED */ -/* -** Hash and comparison functions when the mode is HASH_INT -*/ -static int intHash(const void *pKey, int nKey){ - return nKey ^ (nKey<<8) ^ (nKey>>8); -} -static int intCompare(const void *pKey1, int n1, const void *pKey2, int n2){ - return n2 - n1; -} -#endif - -#if 0 /* NOT USED */ -/* -** Hash and comparison functions when the mode is HASH_POINTER -*/ -static int ptrHash(const void *pKey, int nKey){ - uptr x = Addr(pKey); - return x ^ (x<<8) ^ (x>>8); -} -static int ptrCompare(const void *pKey1, int n1, const void *pKey2, int n2){ - if( pKey1==pKey2 ) return 0; - if( pKey1 0 ){ - h = (h<<3) ^ h ^ *z++; - nKey--; - } - return h & 0x7fffffff; -} -static int strCompare(const void *pKey1, int n1, const void *pKey2, int n2){ - if( n1!=n2 ) return 1; - return strncmp((const char*)pKey1,(const char*)pKey2,n1); -} - -/* -** Hash and comparison functions when the mode is HASH_BINARY -*/ -static int binHash(const void *pKey, int nKey){ - int h = 0; - const char *z = (const char *)pKey; - while( nKey-- > 0 ){ - h = (h<<3) ^ h ^ *(z++); - } - return h & 0x7fffffff; -} -static int binCompare(const void *pKey1, int n1, const void *pKey2, int n2){ - if( n1!=n2 ) return 1; - return memcmp(pKey1,pKey2,n1); -} - -/* -** Return a pointer to the appropriate hash function given the key class. -** -** The C syntax in this function definition may be unfamilar to some -** programmers, so we provide the following additional explanation: -** -** The name of the function is "hashFunction". The function takes a -** single parameter "keyClass". The return value of hashFunction() -** is a pointer to another function. Specifically, the return value -** of hashFunction() is a pointer to a function that takes two parameters -** with types "const void*" and "int" and returns an "int". -*/ -static int (*hashFunction(int keyClass))(const void*,int){ -#if 0 /* HASH_INT and HASH_POINTER are never used */ - switch( keyClass ){ - case HASH_INT: return &intHash; - case HASH_POINTER: return &ptrHash; - case HASH_STRING: return &strHash; - case HASH_BINARY: return &binHash;; - default: break; - } - return 0; -#else - if( keyClass==HASH_STRING ){ - return &strHash; - }else{ - assert( keyClass==HASH_BINARY ); - return &binHash; - } -#endif -} - -/* -** Return a pointer to the appropriate hash function given the key class. -** -** For help in interpreted the obscure C code in the function definition, -** see the header comment on the previous function. -*/ -static int (*compareFunction(int keyClass))(const void*,int,const void*,int){ -#if 0 /* HASH_INT and HASH_POINTER are never used */ - switch( keyClass ){ - case HASH_INT: return &intCompare; - case HASH_POINTER: return &ptrCompare; - case HASH_STRING: return &strCompare; - case HASH_BINARY: return &binCompare; - default: break; - } - return 0; -#else - if( keyClass==HASH_STRING ){ - return &strCompare; - }else{ - assert( keyClass==HASH_BINARY ); - return &binCompare; - } -#endif -} - -/* Link an element into the hash table -*/ -static void insertElement( - Hash *pH, /* The complete hash table */ - struct _ht *pEntry, /* The entry into which pNew is inserted */ - HashElem *pNew /* The element to be inserted */ -){ - HashElem *pHead; /* First element already in pEntry */ - pHead = pEntry->chain; - if( pHead ){ - pNew->next = pHead; - pNew->prev = pHead->prev; - if( pHead->prev ){ pHead->prev->next = pNew; } - else { pH->first = pNew; } - pHead->prev = pNew; - }else{ - pNew->next = pH->first; - if( pH->first ){ pH->first->prev = pNew; } - pNew->prev = 0; - pH->first = pNew; - } - pEntry->count++; - pEntry->chain = pNew; -} - - -/* Resize the hash table so that it cantains "new_size" buckets. -** "new_size" must be a power of 2. The hash table might fail -** to resize if sqliteMalloc() fails. -*/ -static void rehash(Hash *pH, int new_size){ - struct _ht *new_ht; /* The new hash table */ - HashElem *elem, *next_elem; /* For looping over existing elements */ - int (*xHash)(const void*,int); /* The hash function */ - - assert( (new_size & (new_size-1))==0 ); - new_ht = (struct _ht *)pH->xMalloc( new_size*sizeof(struct _ht) ); - if( new_ht==0 ) return; - if( pH->ht ) pH->xFree(pH->ht); - pH->ht = new_ht; - pH->htsize = new_size; - xHash = hashFunction(pH->keyClass); - for(elem=pH->first, pH->first=0; elem; elem = next_elem){ - int h = (*xHash)(elem->pKey, elem->nKey) & (new_size-1); - next_elem = elem->next; - insertElement(pH, &new_ht[h], elem); - } -} - -/* This function (for internal use only) locates an element in an -** hash table that matches the given key. The hash for this key has -** already been computed and is passed as the 4th parameter. -*/ -static HashElem *findElementGivenHash( - const Hash *pH, /* The pH to be searched */ - const void *pKey, /* The key we are searching for */ - int nKey, - int h /* The hash for this key. */ -){ - HashElem *elem; /* Used to loop thru the element list */ - int count; /* Number of elements left to test */ - int (*xCompare)(const void*,int,const void*,int); /* comparison function */ - - if( pH->ht ){ - struct _ht *pEntry = &pH->ht[h]; - elem = pEntry->chain; - count = pEntry->count; - xCompare = compareFunction(pH->keyClass); - while( count-- && elem ){ - if( (*xCompare)(elem->pKey,elem->nKey,pKey,nKey)==0 ){ - return elem; - } - elem = elem->next; - } - } - return 0; -} - -/* Remove a single entry from the hash table given a pointer to that -** element and a hash on the element's key. -*/ -static void removeElementGivenHash( - Hash *pH, /* The pH containing "elem" */ - HashElem* elem, /* The element to be removed from the pH */ - int h /* Hash value for the element */ -){ - struct _ht *pEntry; - if( elem->prev ){ - elem->prev->next = elem->next; - }else{ - pH->first = elem->next; - } - if( elem->next ){ - elem->next->prev = elem->prev; - } - pEntry = &pH->ht[h]; - if( pEntry->chain==elem ){ - pEntry->chain = elem->next; - } - pEntry->count--; - if( pEntry->count<=0 ){ - pEntry->chain = 0; - } - if( pH->copyKey && elem->pKey ){ - pH->xFree(elem->pKey); - } - pH->xFree( elem ); - pH->count--; - if( pH->count<=0 ){ - assert( pH->first==0 ); - assert( pH->count==0 ); - HashClear(pH); - } -} - -/* Attempt to locate an element of the hash table pH with a key -** that matches pKey,nKey. Return the data for this element if it is -** found, or NULL if there is no match. -*/ -void *HashFind(const Hash *pH, const void *pKey, int nKey){ - int h; /* A hash on key */ - HashElem *elem; /* The element that matches key */ - int (*xHash)(const void*,int); /* The hash function */ - - if( pH==0 || pH->ht==0 ) return 0; - xHash = hashFunction(pH->keyClass); - assert( xHash!=0 ); - h = (*xHash)(pKey,nKey); - assert( (pH->htsize & (pH->htsize-1))==0 ); - elem = findElementGivenHash(pH,pKey,nKey, h & (pH->htsize-1)); - return elem ? elem->data : 0; -} - -/* Insert an element into the hash table pH. The key is pKey,nKey -** and the data is "data". -** -** If no element exists with a matching key, then a new -** element is created. A copy of the key is made if the copyKey -** flag is set. NULL is returned. -** -** If another element already exists with the same key, then the -** new data replaces the old data and the old data is returned. -** The key is not copied in this instance. If a malloc fails, then -** the new data is returned and the hash table is unchanged. -** -** If the "data" parameter to this function is NULL, then the -** element corresponding to "key" is removed from the hash table. -*/ -void *HashInsert(Hash *pH, const void *pKey, int nKey, void *data){ - int hraw; /* Raw hash value of the key */ - int h; /* the hash of the key modulo hash table size */ - HashElem *elem; /* Used to loop thru the element list */ - HashElem *new_elem; /* New element added to the pH */ - int (*xHash)(const void*,int); /* The hash function */ - - assert( pH!=0 ); - xHash = hashFunction(pH->keyClass); - assert( xHash!=0 ); - hraw = (*xHash)(pKey, nKey); - assert( (pH->htsize & (pH->htsize-1))==0 ); - h = hraw & (pH->htsize-1); - elem = findElementGivenHash(pH,pKey,nKey,h); - if( elem ){ - void *old_data = elem->data; - if( data==0 ){ - removeElementGivenHash(pH,elem,h); - }else{ - elem->data = data; - } - return old_data; - } - if( data==0 ) return 0; - new_elem = (HashElem*)pH->xMalloc( sizeof(HashElem) ); - if( new_elem==0 ) return data; - if( pH->copyKey && pKey!=0 ){ - new_elem->pKey = pH->xMalloc( nKey ); - if( new_elem->pKey==0 ){ - pH->xFree(new_elem); - return data; - } - memcpy((void*)new_elem->pKey, pKey, nKey); - }else{ - new_elem->pKey = (void*)pKey; - } - new_elem->nKey = nKey; - pH->count++; - if( pH->htsize==0 ){ - rehash(pH,8); - if( pH->htsize==0 ){ - pH->count = 0; - pH->xFree(new_elem); - return data; - } - } - if( pH->count > pH->htsize ){ - rehash(pH,pH->htsize*2); - } - assert( pH->htsize>0 ); - assert( (pH->htsize & (pH->htsize-1))==0 ); - h = hraw & (pH->htsize-1); - insertElement(pH, &pH->ht[h], new_elem); - new_elem->data = data; - return 0; -} diff --git a/ext/fts1/ft_hash.h b/ext/fts1/ft_hash.h deleted file mode 100644 index 95871a4590..0000000000 --- a/ext/fts1/ft_hash.h +++ /dev/null @@ -1,111 +0,0 @@ -/* -** 2001 September 22 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -************************************************************************* -** This is the header file for the generic hash-table implementation -** used in SQLite. We've modified it slightly to serve as a standalone -** hash table implementation for the full-text indexing module. -** -*/ -#ifndef _HASH_H_ -#define _HASH_H_ - -/* Forward declarations of structures. */ -typedef struct Hash Hash; -typedef struct HashElem HashElem; - -/* A complete hash table is an instance of the following structure. -** The internals of this structure are intended to be opaque -- client -** code should not attempt to access or modify the fields of this structure -** directly. Change this structure only by using the routines below. -** However, many of the "procedures" and "functions" for modifying and -** accessing this structure are really macros, so we can't really make -** this structure opaque. -*/ -struct Hash { - char keyClass; /* HASH_INT, _POINTER, _STRING, _BINARY */ - char copyKey; /* True if copy of key made on insert */ - int count; /* Number of entries in this table */ - HashElem *first; /* The first element of the array */ - void *(*xMalloc)(int); /* malloc() function to use */ - void (*xFree)(void *); /* free() function to use */ - int htsize; /* Number of buckets in the hash table */ - struct _ht { /* the hash table */ - int count; /* Number of entries with this hash */ - HashElem *chain; /* Pointer to first entry with this hash */ - } *ht; -}; - -/* Each element in the hash table is an instance of the following -** structure. All elements are stored on a single doubly-linked list. -** -** Again, this structure is intended to be opaque, but it can't really -** be opaque because it is used by macros. -*/ -struct HashElem { - HashElem *next, *prev; /* Next and previous elements in the table */ - void *data; /* Data associated with this element */ - void *pKey; int nKey; /* Key associated with this element */ -}; - -/* -** There are 4 different modes of operation for a hash table: -** -** HASH_INT nKey is used as the key and pKey is ignored. -** -** HASH_POINTER pKey is used as the key and nKey is ignored. -** -** HASH_STRING pKey points to a string that is nKey bytes long -** (including the null-terminator, if any). Case -** is respected in comparisons. -** -** HASH_BINARY pKey points to binary data nKey bytes long. -** memcmp() is used to compare keys. -** -** A copy of the key is made for HASH_STRING and HASH_BINARY -** if the copyKey parameter to HashInit is 1. -*/ -/* #define HASH_INT 1 // NOT USED */ -/* #define HASH_POINTER 2 // NOT USED */ -#define HASH_STRING 3 -#define HASH_BINARY 4 - -/* -** Access routines. To delete, insert a NULL pointer. -*/ -void HashInit(Hash*, int keytype, int copyKey); -void *HashInsert(Hash*, const void *pKey, int nKey, void *pData); -void *HashFind(const Hash*, const void *pKey, int nKey); -void HashClear(Hash*); - -/* -** Macros for looping over all elements of a hash table. The idiom is -** like this: -** -** Hash h; -** HashElem *p; -** ... -** for(p=HashFirst(&h); p; p=HashNext(p)){ -** SomeStructure *pData = HashData(p); -** // do something with pData -** } -*/ -#define HashFirst(H) ((H)->first) -#define HashNext(E) ((E)->next) -#define HashData(E) ((E)->data) -#define HashKey(E) ((E)->pKey) -#define HashKeysize(E) ((E)->nKey) - -/* -** Number of entries in a hash table -*/ -#define HashCount(H) ((H)->count) - -#endif /* _HASH_H_ */ diff --git a/ext/fts1/fts1.c b/ext/fts1/fts1.c deleted file mode 100644 index 77fa9e23f5..0000000000 --- a/ext/fts1/fts1.c +++ /dev/null @@ -1,3348 +0,0 @@ -/* fts1 has a design flaw which can lead to database corruption (see -** below). It is recommended not to use it any longer, instead use -** fts3 (or higher). If you believe that your use of fts1 is safe, -** add -DSQLITE_ENABLE_BROKEN_FTS1=1 to your CFLAGS. -*/ -#if (!defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS1)) \ - && !defined(SQLITE_ENABLE_BROKEN_FTS1) -#error fts1 has a design flaw and has been deprecated. -#endif -/* The flaw is that fts1 uses the content table's unaliased rowid as -** the unique docid. fts1 embeds the rowid in the index it builds, -** and expects the rowid to not change. The SQLite VACUUM operation -** will renumber such rowids, thereby breaking fts1. If you are using -** fts1 in a system which has disabled VACUUM, then you can continue -** to use it safely. Note that PRAGMA auto_vacuum does NOT disable -** VACUUM, though systems using auto_vacuum are unlikely to invoke -** VACUUM. -** -** fts1 should be safe even across VACUUM if you only insert documents -** and never delete. -*/ - -/* The author disclaims copyright to this source code. - * - * This is an SQLite module implementing full-text search. - */ - -/* -** The code in this file is only compiled if: -** -** * The FTS1 module is being built as an extension -** (in which case SQLITE_CORE is not defined), or -** -** * The FTS1 module is being built into the core of -** SQLite (in which case SQLITE_ENABLE_FTS1 is defined). -*/ -#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS1) - -#if defined(SQLITE_ENABLE_FTS1) && !defined(SQLITE_CORE) -# define SQLITE_CORE 1 -#endif - -#include -#include -#include -#include -#include - -#include "fts1.h" -#include "fts1_hash.h" -#include "fts1_tokenizer.h" -#include "sqlite3.h" -#include "sqlite3ext.h" -SQLITE_EXTENSION_INIT1 - - -#if 0 -# define TRACE(A) printf A; fflush(stdout) -#else -# define TRACE(A) -#endif - -/* utility functions */ - -typedef struct StringBuffer { - int len; /* length, not including null terminator */ - int alloced; /* Space allocated for s[] */ - char *s; /* Content of the string */ -} StringBuffer; - -static void initStringBuffer(StringBuffer *sb){ - sb->len = 0; - sb->alloced = 100; - sb->s = malloc(100); - sb->s[0] = '\0'; -} - -static void nappend(StringBuffer *sb, const char *zFrom, int nFrom){ - if( sb->len + nFrom >= sb->alloced ){ - sb->alloced = sb->len + nFrom + 100; - sb->s = realloc(sb->s, sb->alloced+1); - if( sb->s==0 ){ - initStringBuffer(sb); - return; - } - } - memcpy(sb->s + sb->len, zFrom, nFrom); - sb->len += nFrom; - sb->s[sb->len] = 0; -} -static void append(StringBuffer *sb, const char *zFrom){ - nappend(sb, zFrom, strlen(zFrom)); -} - -/* We encode variable-length integers in little-endian order using seven bits - * per byte as follows: -** -** KEY: -** A = 0xxxxxxx 7 bits of data and one flag bit -** B = 1xxxxxxx 7 bits of data and one flag bit -** -** 7 bits - A -** 14 bits - BA -** 21 bits - BBA -** and so on. -*/ - -/* We may need up to VARINT_MAX bytes to store an encoded 64-bit integer. */ -#define VARINT_MAX 10 - -/* Write a 64-bit variable-length integer to memory starting at p[0]. - * The length of data written will be between 1 and VARINT_MAX bytes. - * The number of bytes written is returned. */ -static int putVarint(char *p, sqlite_int64 v){ - unsigned char *q = (unsigned char *) p; - sqlite_uint64 vu = v; - do{ - *q++ = (unsigned char) ((vu & 0x7f) | 0x80); - vu >>= 7; - }while( vu!=0 ); - q[-1] &= 0x7f; /* turn off high bit in final byte */ - assert( q - (unsigned char *)p <= VARINT_MAX ); - return (int) (q - (unsigned char *)p); -} - -/* Read a 64-bit variable-length integer from memory starting at p[0]. - * Return the number of bytes read, or 0 on error. - * The value is stored in *v. */ -static int getVarint(const char *p, sqlite_int64 *v){ - const unsigned char *q = (const unsigned char *) p; - sqlite_uint64 x = 0, y = 1; - while( (*q & 0x80) == 0x80 ){ - x += y * (*q++ & 0x7f); - y <<= 7; - if( q - (unsigned char *)p >= VARINT_MAX ){ /* bad data */ - assert( 0 ); - return 0; - } - } - x += y * (*q++); - *v = (sqlite_int64) x; - return (int) (q - (unsigned char *)p); -} - -static int getVarint32(const char *p, int *pi){ - sqlite_int64 i; - int ret = getVarint(p, &i); - *pi = (int) i; - assert( *pi==i ); - return ret; -} - -/*** Document lists *** - * - * A document list holds a sorted list of varint-encoded document IDs. - * - * A doclist with type DL_POSITIONS_OFFSETS is stored like this: - * - * array { - * varint docid; - * array { - * varint position; (delta from previous position plus POS_BASE) - * varint startOffset; (delta from previous startOffset) - * varint endOffset; (delta from startOffset) - * } - * } - * - * Here, array { X } means zero or more occurrences of X, adjacent in memory. - * - * A position list may hold positions for text in multiple columns. A position - * POS_COLUMN is followed by a varint containing the index of the column for - * following positions in the list. Any positions appearing before any - * occurrences of POS_COLUMN are for column 0. - * - * A doclist with type DL_POSITIONS is like the above, but holds only docids - * and positions without offset information. - * - * A doclist with type DL_DOCIDS is like the above, but holds only docids - * without positions or offset information. - * - * On disk, every document list has positions and offsets, so we don't bother - * to serialize a doclist's type. - * - * We don't yet delta-encode document IDs; doing so will probably be a - * modest win. - * - * NOTE(shess) I've thought of a slightly (1%) better offset encoding. - * After the first offset, estimate the next offset by using the - * current token position and the previous token position and offset, - * offset to handle some variance. So the estimate would be - * (iPosition*w->iStartOffset/w->iPosition-64), which is delta-encoded - * as normal. Offsets more than 64 chars from the estimate are - * encoded as the delta to the previous start offset + 128. An - * additional tiny increment can be gained by using the end offset of - * the previous token to make the estimate a tiny bit more precise. -*/ - -/* It is not safe to call isspace(), tolower(), or isalnum() on -** hi-bit-set characters. This is the same solution used in the -** tokenizer. -*/ -/* TODO(shess) The snippet-generation code should be using the -** tokenizer-generated tokens rather than doing its own local -** tokenization. -*/ -/* TODO(shess) Is __isascii() a portable version of (c&0x80)==0? */ -static int safe_isspace(char c){ - return (c&0x80)==0 ? isspace((unsigned char)c) : 0; -} -static int safe_tolower(char c){ - return (c&0x80)==0 ? tolower((unsigned char)c) : c; -} -static int safe_isalnum(char c){ - return (c&0x80)==0 ? isalnum((unsigned char)c) : 0; -} - -typedef enum DocListType { - DL_DOCIDS, /* docids only */ - DL_POSITIONS, /* docids + positions */ - DL_POSITIONS_OFFSETS /* docids + positions + offsets */ -} DocListType; - -/* -** By default, only positions and not offsets are stored in the doclists. -** To change this so that offsets are stored too, compile with -** -** -DDL_DEFAULT=DL_POSITIONS_OFFSETS -** -*/ -#ifndef DL_DEFAULT -# define DL_DEFAULT DL_POSITIONS -#endif - -typedef struct DocList { - char *pData; - int nData; - DocListType iType; - int iLastColumn; /* the last column written */ - int iLastPos; /* the last position written */ - int iLastOffset; /* the last start offset written */ -} DocList; - -enum { - POS_END = 0, /* end of this position list */ - POS_COLUMN, /* followed by new column number */ - POS_BASE -}; - -/* Initialize a new DocList to hold the given data. */ -static void docListInit(DocList *d, DocListType iType, - const char *pData, int nData){ - d->nData = nData; - if( nData>0 ){ - d->pData = malloc(nData); - memcpy(d->pData, pData, nData); - } else { - d->pData = NULL; - } - d->iType = iType; - d->iLastColumn = 0; - d->iLastPos = d->iLastOffset = 0; -} - -/* Create a new dynamically-allocated DocList. */ -static DocList *docListNew(DocListType iType){ - DocList *d = (DocList *) malloc(sizeof(DocList)); - docListInit(d, iType, 0, 0); - return d; -} - -static void docListDestroy(DocList *d){ - free(d->pData); -#ifndef NDEBUG - memset(d, 0x55, sizeof(*d)); -#endif -} - -static void docListDelete(DocList *d){ - docListDestroy(d); - free(d); -} - -static char *docListEnd(DocList *d){ - return d->pData + d->nData; -} - -/* Append a varint to a DocList's data. */ -static void appendVarint(DocList *d, sqlite_int64 i){ - char c[VARINT_MAX]; - int n = putVarint(c, i); - d->pData = realloc(d->pData, d->nData + n); - memcpy(d->pData + d->nData, c, n); - d->nData += n; -} - -static void docListAddDocid(DocList *d, sqlite_int64 iDocid){ - appendVarint(d, iDocid); - if( d->iType>=DL_POSITIONS ){ - appendVarint(d, POS_END); /* initially empty position list */ - d->iLastColumn = 0; - d->iLastPos = d->iLastOffset = 0; - } -} - -/* helper function for docListAddPos and docListAddPosOffset */ -static void addPos(DocList *d, int iColumn, int iPos){ - assert( d->nData>0 ); - --d->nData; /* remove previous terminator */ - if( iColumn!=d->iLastColumn ){ - assert( iColumn>d->iLastColumn ); - appendVarint(d, POS_COLUMN); - appendVarint(d, iColumn); - d->iLastColumn = iColumn; - d->iLastPos = d->iLastOffset = 0; - } - assert( iPos>=d->iLastPos ); - appendVarint(d, iPos-d->iLastPos+POS_BASE); - d->iLastPos = iPos; -} - -/* Add a position to the last position list in a doclist. */ -static void docListAddPos(DocList *d, int iColumn, int iPos){ - assert( d->iType==DL_POSITIONS ); - addPos(d, iColumn, iPos); - appendVarint(d, POS_END); /* add new terminator */ -} - -/* -** Add a position and starting and ending offsets to a doclist. -** -** If the doclist is setup to handle only positions, then insert -** the position only and ignore the offsets. -*/ -static void docListAddPosOffset( - DocList *d, /* Doclist under construction */ - int iColumn, /* Column the inserted term is part of */ - int iPos, /* Position of the inserted term */ - int iStartOffset, /* Starting offset of inserted term */ - int iEndOffset /* Ending offset of inserted term */ -){ - assert( d->iType>=DL_POSITIONS ); - addPos(d, iColumn, iPos); - if( d->iType==DL_POSITIONS_OFFSETS ){ - assert( iStartOffset>=d->iLastOffset ); - appendVarint(d, iStartOffset-d->iLastOffset); - d->iLastOffset = iStartOffset; - assert( iEndOffset>=iStartOffset ); - appendVarint(d, iEndOffset-iStartOffset); - } - appendVarint(d, POS_END); /* add new terminator */ -} - -/* -** A DocListReader object is a cursor into a doclist. Initialize -** the cursor to the beginning of the doclist by calling readerInit(). -** Then use routines -** -** peekDocid() -** readDocid() -** readPosition() -** skipPositionList() -** and so forth... -** -** to read information out of the doclist. When we reach the end -** of the doclist, atEnd() returns TRUE. -*/ -typedef struct DocListReader { - DocList *pDoclist; /* The document list we are stepping through */ - char *p; /* Pointer to next unread byte in the doclist */ - int iLastColumn; - int iLastPos; /* the last position read, or -1 when not in a position list */ -} DocListReader; - -/* -** Initialize the DocListReader r to point to the beginning of pDoclist. -*/ -static void readerInit(DocListReader *r, DocList *pDoclist){ - r->pDoclist = pDoclist; - if( pDoclist!=NULL ){ - r->p = pDoclist->pData; - } - r->iLastColumn = -1; - r->iLastPos = -1; -} - -/* -** Return TRUE if we have reached then end of pReader and there is -** nothing else left to read. -*/ -static int atEnd(DocListReader *pReader){ - return pReader->pDoclist==0 || (pReader->p >= docListEnd(pReader->pDoclist)); -} - -/* Peek at the next docid without advancing the read pointer. -*/ -static sqlite_int64 peekDocid(DocListReader *pReader){ - sqlite_int64 ret; - assert( !atEnd(pReader) ); - assert( pReader->iLastPos==-1 ); - getVarint(pReader->p, &ret); - return ret; -} - -/* Read the next docid. See also nextDocid(). -*/ -static sqlite_int64 readDocid(DocListReader *pReader){ - sqlite_int64 ret; - assert( !atEnd(pReader) ); - assert( pReader->iLastPos==-1 ); - pReader->p += getVarint(pReader->p, &ret); - if( pReader->pDoclist->iType>=DL_POSITIONS ){ - pReader->iLastColumn = 0; - pReader->iLastPos = 0; - } - return ret; -} - -/* Read the next position and column index from a position list. - * Returns the position, or -1 at the end of the list. */ -static int readPosition(DocListReader *pReader, int *iColumn){ - int i; - int iType = pReader->pDoclist->iType; - - if( pReader->iLastPos==-1 ){ - return -1; - } - assert( !atEnd(pReader) ); - - if( iTypep += getVarint32(pReader->p, &i); - if( i==POS_END ){ - pReader->iLastColumn = pReader->iLastPos = -1; - *iColumn = -1; - return -1; - } - if( i==POS_COLUMN ){ - pReader->p += getVarint32(pReader->p, &pReader->iLastColumn); - pReader->iLastPos = 0; - pReader->p += getVarint32(pReader->p, &i); - assert( i>=POS_BASE ); - } - pReader->iLastPos += ((int) i)-POS_BASE; - if( iType>=DL_POSITIONS_OFFSETS ){ - /* Skip over offsets, ignoring them for now. */ - int iStart, iEnd; - pReader->p += getVarint32(pReader->p, &iStart); - pReader->p += getVarint32(pReader->p, &iEnd); - } - *iColumn = pReader->iLastColumn; - return pReader->iLastPos; -} - -/* Skip past the end of a position list. */ -static void skipPositionList(DocListReader *pReader){ - DocList *p = pReader->pDoclist; - if( p && p->iType>=DL_POSITIONS ){ - int iColumn; - while( readPosition(pReader, &iColumn)!=-1 ){} - } -} - -/* Skip over a docid, including its position list if the doclist has - * positions. */ -static void skipDocument(DocListReader *pReader){ - readDocid(pReader); - skipPositionList(pReader); -} - -/* Skip past all docids which are less than [iDocid]. Returns 1 if a docid - * matching [iDocid] was found. */ -static int skipToDocid(DocListReader *pReader, sqlite_int64 iDocid){ - sqlite_int64 d = 0; - while( !atEnd(pReader) && (d=peekDocid(pReader))iType>=DL_POSITIONS ){ - int iPos, iCol; - const char *zDiv = ""; - printf("("); - while( (iPos = readPosition(&r, &iCol))>=0 ){ - printf("%s%d:%d", zDiv, iCol, iPos); - zDiv = ":"; - } - printf(")"); - } - } - printf("\n"); - fflush(stdout); -} -#endif /* SQLITE_DEBUG */ - -/* Trim the given doclist to contain only positions in column - * [iRestrictColumn]. */ -static void docListRestrictColumn(DocList *in, int iRestrictColumn){ - DocListReader r; - DocList out; - - assert( in->iType>=DL_POSITIONS ); - readerInit(&r, in); - docListInit(&out, DL_POSITIONS, NULL, 0); - - while( !atEnd(&r) ){ - sqlite_int64 iDocid = readDocid(&r); - int iPos, iColumn; - - docListAddDocid(&out, iDocid); - while( (iPos = readPosition(&r, &iColumn)) != -1 ){ - if( iColumn==iRestrictColumn ){ - docListAddPos(&out, iColumn, iPos); - } - } - } - - docListDestroy(in); - *in = out; -} - -/* Trim the given doclist by discarding any docids without any remaining - * positions. */ -static void docListDiscardEmpty(DocList *in) { - DocListReader r; - DocList out; - - /* TODO: It would be nice to implement this operation in place; that - * could save a significant amount of memory in queries with long doclists. */ - assert( in->iType>=DL_POSITIONS ); - readerInit(&r, in); - docListInit(&out, DL_POSITIONS, NULL, 0); - - while( !atEnd(&r) ){ - sqlite_int64 iDocid = readDocid(&r); - int match = 0; - int iPos, iColumn; - while( (iPos = readPosition(&r, &iColumn)) != -1 ){ - if( !match ){ - docListAddDocid(&out, iDocid); - match = 1; - } - docListAddPos(&out, iColumn, iPos); - } - } - - docListDestroy(in); - *in = out; -} - -/* Helper function for docListUpdate() and docListAccumulate(). -** Splices a doclist element into the doclist represented by r, -** leaving r pointing after the newly spliced element. -*/ -static void docListSpliceElement(DocListReader *r, sqlite_int64 iDocid, - const char *pSource, int nSource){ - DocList *d = r->pDoclist; - char *pTarget; - int nTarget, found; - - found = skipToDocid(r, iDocid); - - /* Describe slice in d to place pSource/nSource. */ - pTarget = r->p; - if( found ){ - skipDocument(r); - nTarget = r->p-pTarget; - }else{ - nTarget = 0; - } - - /* The sense of the following is that there are three possibilities. - ** If nTarget==nSource, we should not move any memory nor realloc. - ** If nTarget>nSource, trim target and realloc. - ** If nTargetnSource ){ - memmove(pTarget+nSource, pTarget+nTarget, docListEnd(d)-(pTarget+nTarget)); - } - if( nTarget!=nSource ){ - int iDoclist = pTarget-d->pData; - d->pData = realloc(d->pData, d->nData+nSource-nTarget); - pTarget = d->pData+iDoclist; - } - if( nTargetnData += nSource-nTarget; - r->p = pTarget+nSource; -} - -/* Insert/update pUpdate into the doclist. */ -static void docListUpdate(DocList *d, DocList *pUpdate){ - DocListReader reader; - - assert( d!=NULL && pUpdate!=NULL ); - assert( d->iType==pUpdate->iType); - - readerInit(&reader, d); - docListSpliceElement(&reader, firstDocid(pUpdate), - pUpdate->pData, pUpdate->nData); -} - -/* Propagate elements from pUpdate to pAcc, overwriting elements with -** matching docids. -*/ -static void docListAccumulate(DocList *pAcc, DocList *pUpdate){ - DocListReader accReader, updateReader; - - /* Handle edge cases where one doclist is empty. */ - assert( pAcc!=NULL ); - if( pUpdate==NULL || pUpdate->nData==0 ) return; - if( pAcc->nData==0 ){ - pAcc->pData = malloc(pUpdate->nData); - memcpy(pAcc->pData, pUpdate->pData, pUpdate->nData); - pAcc->nData = pUpdate->nData; - return; - } - - readerInit(&accReader, pAcc); - readerInit(&updateReader, pUpdate); - - while( !atEnd(&updateReader) ){ - char *pSource = updateReader.p; - sqlite_int64 iDocid = readDocid(&updateReader); - skipPositionList(&updateReader); - docListSpliceElement(&accReader, iDocid, pSource, updateReader.p-pSource); - } -} - -/* -** Read the next docid off of pIn. Return 0 if we reach the end. -* -* TODO: This assumes that docids are never 0, but they may actually be 0 since -* users can choose docids when inserting into a full-text table. Fix this. -*/ -static sqlite_int64 nextDocid(DocListReader *pIn){ - skipPositionList(pIn); - return atEnd(pIn) ? 0 : readDocid(pIn); -} - -/* -** pLeft and pRight are two DocListReaders that are pointing to -** positions lists of the same document: iDocid. -** -** If there are no instances in pLeft or pRight where the position -** of pLeft is one less than the position of pRight, then this -** routine adds nothing to pOut. -** -** If there are one or more instances where positions from pLeft -** are exactly one less than positions from pRight, then add a new -** document record to pOut. If pOut wants to hold positions, then -** include the positions from pRight that are one more than a -** position in pLeft. In other words: pRight.iPos==pLeft.iPos+1. -** -** pLeft and pRight are left pointing at the next document record. -*/ -static void mergePosList( - DocListReader *pLeft, /* Left position list */ - DocListReader *pRight, /* Right position list */ - sqlite_int64 iDocid, /* The docid from pLeft and pRight */ - DocList *pOut /* Write the merged document record here */ -){ - int iLeftCol, iLeftPos = readPosition(pLeft, &iLeftCol); - int iRightCol, iRightPos = readPosition(pRight, &iRightCol); - int match = 0; - - /* Loop until we've reached the end of both position lists. */ - while( iLeftPos!=-1 && iRightPos!=-1 ){ - if( iLeftCol==iRightCol && iLeftPos+1==iRightPos ){ - if( !match ){ - docListAddDocid(pOut, iDocid); - match = 1; - } - if( pOut->iType>=DL_POSITIONS ){ - docListAddPos(pOut, iRightCol, iRightPos); - } - iLeftPos = readPosition(pLeft, &iLeftCol); - iRightPos = readPosition(pRight, &iRightCol); - }else if( iRightCol=0 ) skipPositionList(pLeft); - if( iRightPos>=0 ) skipPositionList(pRight); -} - -/* We have two doclists: pLeft and pRight. -** Write the phrase intersection of these two doclists into pOut. -** -** A phrase intersection means that two documents only match -** if pLeft.iPos+1==pRight.iPos. -** -** The output pOut may or may not contain positions. If pOut -** does contain positions, they are the positions of pRight. -*/ -static void docListPhraseMerge( - DocList *pLeft, /* Doclist resulting from the words on the left */ - DocList *pRight, /* Doclist for the next word to the right */ - DocList *pOut /* Write the combined doclist here */ -){ - DocListReader left, right; - sqlite_int64 docidLeft, docidRight; - - readerInit(&left, pLeft); - readerInit(&right, pRight); - docidLeft = nextDocid(&left); - docidRight = nextDocid(&right); - - while( docidLeft>0 && docidRight>0 ){ - if( docidLeftiType0 && docidRight>0 ){ - if( docidLeft0 && docidRight>0 ){ - if( docidLeft<=docidRight ){ - docListAddDocid(pOut, docidLeft); - }else{ - docListAddDocid(pOut, docidRight); - } - priorLeft = docidLeft; - if( docidLeft<=docidRight ){ - docidLeft = nextDocid(&left); - } - if( docidRight>0 && docidRight<=priorLeft ){ - docidRight = nextDocid(&right); - } - } - while( docidLeft>0 ){ - docListAddDocid(pOut, docidLeft); - docidLeft = nextDocid(&left); - } - while( docidRight>0 ){ - docListAddDocid(pOut, docidRight); - docidRight = nextDocid(&right); - } -} - -/* We have two doclists: pLeft and pRight. -** Write into pOut all documents that occur in pLeft but not -** in pRight. -** -** Only docids are matched. Position information is ignored. -** -** The output pOut never holds positions. -*/ -static void docListExceptMerge( - DocList *pLeft, /* Doclist resulting from the words on the left */ - DocList *pRight, /* Doclist for the next word to the right */ - DocList *pOut /* Write the combined doclist here */ -){ - DocListReader left, right; - sqlite_int64 docidLeft, docidRight, priorLeft; - - readerInit(&left, pLeft); - readerInit(&right, pRight); - docidLeft = nextDocid(&left); - docidRight = nextDocid(&right); - - while( docidLeft>0 && docidRight>0 ){ - priorLeft = docidLeft; - if( docidLeft0 && docidRight<=priorLeft ){ - docidRight = nextDocid(&right); - } - } - while( docidLeft>0 ){ - docListAddDocid(pOut, docidLeft); - docidLeft = nextDocid(&left); - } -} - -static char *string_dup_n(const char *s, int n){ - char *str = malloc(n + 1); - memcpy(str, s, n); - str[n] = '\0'; - return str; -} - -/* Duplicate a string; the caller must free() the returned string. - * (We don't use strdup() since it is not part of the standard C library and - * may not be available everywhere.) */ -static char *string_dup(const char *s){ - return string_dup_n(s, strlen(s)); -} - -/* Format a string, replacing each occurrence of the % character with - * zDb.zName. This may be more convenient than sqlite_mprintf() - * when one string is used repeatedly in a format string. - * The caller must free() the returned string. */ -static char *string_format(const char *zFormat, - const char *zDb, const char *zName){ - const char *p; - size_t len = 0; - size_t nDb = strlen(zDb); - size_t nName = strlen(zName); - size_t nFullTableName = nDb+1+nName; - char *result; - char *r; - - /* first compute length needed */ - for(p = zFormat ; *p ; ++p){ - len += (*p=='%' ? nFullTableName : 1); - } - len += 1; /* for null terminator */ - - r = result = malloc(len); - for(p = zFormat; *p; ++p){ - if( *p=='%' ){ - memcpy(r, zDb, nDb); - r += nDb; - *r++ = '.'; - memcpy(r, zName, nName); - r += nName; - } else { - *r++ = *p; - } - } - *r++ = '\0'; - assert( r == result + len ); - return result; -} - -static int sql_exec(sqlite3 *db, const char *zDb, const char *zName, - const char *zFormat){ - char *zCommand = string_format(zFormat, zDb, zName); - int rc; - TRACE(("FTS1 sql: %s\n", zCommand)); - rc = sqlite3_exec(db, zCommand, NULL, 0, NULL); - free(zCommand); - return rc; -} - -static int sql_prepare(sqlite3 *db, const char *zDb, const char *zName, - sqlite3_stmt **ppStmt, const char *zFormat){ - char *zCommand = string_format(zFormat, zDb, zName); - int rc; - TRACE(("FTS1 prepare: %s\n", zCommand)); - rc = sqlite3_prepare(db, zCommand, -1, ppStmt, NULL); - free(zCommand); - return rc; -} - -/* end utility functions */ - -/* Forward reference */ -typedef struct fulltext_vtab fulltext_vtab; - -/* A single term in a query is represented by an instances of -** the following structure. -*/ -typedef struct QueryTerm { - short int nPhrase; /* How many following terms are part of the same phrase */ - short int iPhrase; /* This is the i-th term of a phrase. */ - short int iColumn; /* Column of the index that must match this term */ - signed char isOr; /* this term is preceded by "OR" */ - signed char isNot; /* this term is preceded by "-" */ - char *pTerm; /* text of the term. '\000' terminated. malloced */ - int nTerm; /* Number of bytes in pTerm[] */ -} QueryTerm; - - -/* A query string is parsed into a Query structure. - * - * We could, in theory, allow query strings to be complicated - * nested expressions with precedence determined by parentheses. - * But none of the major search engines do this. (Perhaps the - * feeling is that an parenthesized expression is two complex of - * an idea for the average user to grasp.) Taking our lead from - * the major search engines, we will allow queries to be a list - * of terms (with an implied AND operator) or phrases in double-quotes, - * with a single optional "-" before each non-phrase term to designate - * negation and an optional OR connector. - * - * OR binds more tightly than the implied AND, which is what the - * major search engines seem to do. So, for example: - * - * [one two OR three] ==> one AND (two OR three) - * [one OR two three] ==> (one OR two) AND three - * - * A "-" before a term matches all entries that lack that term. - * The "-" must occur immediately before the term with in intervening - * space. This is how the search engines do it. - * - * A NOT term cannot be the right-hand operand of an OR. If this - * occurs in the query string, the NOT is ignored: - * - * [one OR -two] ==> one OR two - * - */ -typedef struct Query { - fulltext_vtab *pFts; /* The full text index */ - int nTerms; /* Number of terms in the query */ - QueryTerm *pTerms; /* Array of terms. Space obtained from malloc() */ - int nextIsOr; /* Set the isOr flag on the next inserted term */ - int nextColumn; /* Next word parsed must be in this column */ - int dfltColumn; /* The default column */ -} Query; - - -/* -** An instance of the following structure keeps track of generated -** matching-word offset information and snippets. -*/ -typedef struct Snippet { - int nMatch; /* Total number of matches */ - int nAlloc; /* Space allocated for aMatch[] */ - struct snippetMatch { /* One entry for each matching term */ - char snStatus; /* Status flag for use while constructing snippets */ - short int iCol; /* The column that contains the match */ - short int iTerm; /* The index in Query.pTerms[] of the matching term */ - short int nByte; /* Number of bytes in the term */ - int iStart; /* The offset to the first character of the term */ - } *aMatch; /* Points to space obtained from malloc */ - char *zOffset; /* Text rendering of aMatch[] */ - int nOffset; /* strlen(zOffset) */ - char *zSnippet; /* Snippet text */ - int nSnippet; /* strlen(zSnippet) */ -} Snippet; - - -typedef enum QueryType { - QUERY_GENERIC, /* table scan */ - QUERY_ROWID, /* lookup by rowid */ - QUERY_FULLTEXT /* QUERY_FULLTEXT + [i] is a full-text search for column i*/ -} QueryType; - -/* TODO(shess) CHUNK_MAX controls how much data we allow in segment 0 -** before we start aggregating into larger segments. Lower CHUNK_MAX -** means that for a given input we have more individual segments per -** term, which means more rows in the table and a bigger index (due to -** both more rows and bigger rowids). But it also reduces the average -** cost of adding new elements to the segment 0 doclist, and it seems -** to reduce the number of pages read and written during inserts. 256 -** was chosen by measuring insertion times for a certain input (first -** 10k documents of Enron corpus), though including query performance -** in the decision may argue for a larger value. -*/ -#define CHUNK_MAX 256 - -typedef enum fulltext_statement { - CONTENT_INSERT_STMT, - CONTENT_SELECT_STMT, - CONTENT_UPDATE_STMT, - CONTENT_DELETE_STMT, - - TERM_SELECT_STMT, - TERM_SELECT_ALL_STMT, - TERM_INSERT_STMT, - TERM_UPDATE_STMT, - TERM_DELETE_STMT, - - MAX_STMT /* Always at end! */ -} fulltext_statement; - -/* These must exactly match the enum above. */ -/* TODO(adam): Is there some risk that a statement (in particular, -** pTermSelectStmt) will be used in two cursors at once, e.g. if a -** query joins a virtual table to itself? If so perhaps we should -** move some of these to the cursor object. -*/ -static const char *const fulltext_zStatement[MAX_STMT] = { - /* CONTENT_INSERT */ NULL, /* generated in contentInsertStatement() */ - /* CONTENT_SELECT */ "select * from %_content where rowid = ?", - /* CONTENT_UPDATE */ NULL, /* generated in contentUpdateStatement() */ - /* CONTENT_DELETE */ "delete from %_content where rowid = ?", - - /* TERM_SELECT */ - "select rowid, doclist from %_term where term = ? and segment = ?", - /* TERM_SELECT_ALL */ - "select doclist from %_term where term = ? order by segment", - /* TERM_INSERT */ - "insert into %_term (rowid, term, segment, doclist) values (?, ?, ?, ?)", - /* TERM_UPDATE */ "update %_term set doclist = ? where rowid = ?", - /* TERM_DELETE */ "delete from %_term where rowid = ?", -}; - -/* -** A connection to a fulltext index is an instance of the following -** structure. The xCreate and xConnect methods create an instance -** of this structure and xDestroy and xDisconnect free that instance. -** All other methods receive a pointer to the structure as one of their -** arguments. -*/ -struct fulltext_vtab { - sqlite3_vtab base; /* Base class used by SQLite core */ - sqlite3 *db; /* The database connection */ - const char *zDb; /* logical database name */ - const char *zName; /* virtual table name */ - int nColumn; /* number of columns in virtual table */ - char **azColumn; /* column names. malloced */ - char **azContentColumn; /* column names in content table; malloced */ - sqlite3_tokenizer *pTokenizer; /* tokenizer for inserts and queries */ - - /* Precompiled statements which we keep as long as the table is - ** open. - */ - sqlite3_stmt *pFulltextStatements[MAX_STMT]; -}; - -/* -** When the core wants to do a query, it create a cursor using a -** call to xOpen. This structure is an instance of a cursor. It -** is destroyed by xClose. -*/ -typedef struct fulltext_cursor { - sqlite3_vtab_cursor base; /* Base class used by SQLite core */ - QueryType iCursorType; /* Copy of sqlite3_index_info.idxNum */ - sqlite3_stmt *pStmt; /* Prepared statement in use by the cursor */ - int eof; /* True if at End Of Results */ - Query q; /* Parsed query string */ - Snippet snippet; /* Cached snippet for the current row */ - int iColumn; /* Column being searched */ - DocListReader result; /* used when iCursorType == QUERY_FULLTEXT */ -} fulltext_cursor; - -static struct fulltext_vtab *cursor_vtab(fulltext_cursor *c){ - return (fulltext_vtab *) c->base.pVtab; -} - -static const sqlite3_module fulltextModule; /* forward declaration */ - -/* Append a list of strings separated by commas to a StringBuffer. */ -static void appendList(StringBuffer *sb, int nString, char **azString){ - int i; - for(i=0; i0 ) append(sb, ", "); - append(sb, azString[i]); - } -} - -/* Return a dynamically generated statement of the form - * insert into %_content (rowid, ...) values (?, ...) - */ -static const char *contentInsertStatement(fulltext_vtab *v){ - StringBuffer sb; - int i; - - initStringBuffer(&sb); - append(&sb, "insert into %_content (rowid, "); - appendList(&sb, v->nColumn, v->azContentColumn); - append(&sb, ") values (?"); - for(i=0; inColumn; ++i) - append(&sb, ", ?"); - append(&sb, ")"); - return sb.s; -} - -/* Return a dynamically generated statement of the form - * update %_content set [col_0] = ?, [col_1] = ?, ... - * where rowid = ? - */ -static const char *contentUpdateStatement(fulltext_vtab *v){ - StringBuffer sb; - int i; - - initStringBuffer(&sb); - append(&sb, "update %_content set "); - for(i=0; inColumn; ++i) { - if( i>0 ){ - append(&sb, ", "); - } - append(&sb, v->azContentColumn[i]); - append(&sb, " = ?"); - } - append(&sb, " where rowid = ?"); - return sb.s; -} - -/* Puts a freshly-prepared statement determined by iStmt in *ppStmt. -** If the indicated statement has never been prepared, it is prepared -** and cached, otherwise the cached version is reset. -*/ -static int sql_get_statement(fulltext_vtab *v, fulltext_statement iStmt, - sqlite3_stmt **ppStmt){ - assert( iStmtpFulltextStatements[iStmt]==NULL ){ - const char *zStmt; - int rc; - switch( iStmt ){ - case CONTENT_INSERT_STMT: - zStmt = contentInsertStatement(v); break; - case CONTENT_UPDATE_STMT: - zStmt = contentUpdateStatement(v); break; - default: - zStmt = fulltext_zStatement[iStmt]; - } - rc = sql_prepare(v->db, v->zDb, v->zName, &v->pFulltextStatements[iStmt], - zStmt); - if( zStmt != fulltext_zStatement[iStmt]) free((void *) zStmt); - if( rc!=SQLITE_OK ) return rc; - } else { - int rc = sqlite3_reset(v->pFulltextStatements[iStmt]); - if( rc!=SQLITE_OK ) return rc; - } - - *ppStmt = v->pFulltextStatements[iStmt]; - return SQLITE_OK; -} - -/* Step the indicated statement, handling errors SQLITE_BUSY (by -** retrying) and SQLITE_SCHEMA (by re-preparing and transferring -** bindings to the new statement). -** TODO(adam): We should extend this function so that it can work with -** statements declared locally, not only globally cached statements. -*/ -static int sql_step_statement(fulltext_vtab *v, fulltext_statement iStmt, - sqlite3_stmt **ppStmt){ - int rc; - sqlite3_stmt *s = *ppStmt; - assert( iStmtpFulltextStatements[iStmt] ); - - while( (rc=sqlite3_step(s))!=SQLITE_DONE && rc!=SQLITE_ROW ){ - if( rc==SQLITE_BUSY ) continue; - if( rc!=SQLITE_ERROR ) return rc; - - /* If an SQLITE_SCHEMA error has occurred, then finalizing this - * statement is going to delete the fulltext_vtab structure. If - * the statement just executed is in the pFulltextStatements[] - * array, it will be finalized twice. So remove it before - * calling sqlite3_finalize(). - */ - v->pFulltextStatements[iStmt] = NULL; - rc = sqlite3_finalize(s); - break; - } - return rc; - - err: - sqlite3_finalize(s); - return rc; -} - -/* Like sql_step_statement(), but convert SQLITE_DONE to SQLITE_OK. -** Useful for statements like UPDATE, where we expect no results. -*/ -static int sql_single_step_statement(fulltext_vtab *v, - fulltext_statement iStmt, - sqlite3_stmt **ppStmt){ - int rc = sql_step_statement(v, iStmt, ppStmt); - return (rc==SQLITE_DONE) ? SQLITE_OK : rc; -} - -/* insert into %_content (rowid, ...) values ([rowid], [pValues]) */ -static int content_insert(fulltext_vtab *v, sqlite3_value *rowid, - sqlite3_value **pValues){ - sqlite3_stmt *s; - int i; - int rc = sql_get_statement(v, CONTENT_INSERT_STMT, &s); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_value(s, 1, rowid); - if( rc!=SQLITE_OK ) return rc; - - for(i=0; inColumn; ++i){ - rc = sqlite3_bind_value(s, 2+i, pValues[i]); - if( rc!=SQLITE_OK ) return rc; - } - - return sql_single_step_statement(v, CONTENT_INSERT_STMT, &s); -} - -/* update %_content set col0 = pValues[0], col1 = pValues[1], ... - * where rowid = [iRowid] */ -static int content_update(fulltext_vtab *v, sqlite3_value **pValues, - sqlite_int64 iRowid){ - sqlite3_stmt *s; - int i; - int rc = sql_get_statement(v, CONTENT_UPDATE_STMT, &s); - if( rc!=SQLITE_OK ) return rc; - - for(i=0; inColumn; ++i){ - rc = sqlite3_bind_value(s, 1+i, pValues[i]); - if( rc!=SQLITE_OK ) return rc; - } - - rc = sqlite3_bind_int64(s, 1+v->nColumn, iRowid); - if( rc!=SQLITE_OK ) return rc; - - return sql_single_step_statement(v, CONTENT_UPDATE_STMT, &s); -} - -static void freeStringArray(int nString, const char **pString){ - int i; - - for (i=0 ; i < nString ; ++i) { - if( pString[i]!=NULL ) free((void *) pString[i]); - } - free((void *) pString); -} - -/* select * from %_content where rowid = [iRow] - * The caller must delete the returned array and all strings in it. - * null fields will be NULL in the returned array. - * - * TODO: Perhaps we should return pointer/length strings here for consistency - * with other code which uses pointer/length. */ -static int content_select(fulltext_vtab *v, sqlite_int64 iRow, - const char ***pValues){ - sqlite3_stmt *s; - const char **values; - int i; - int rc; - - *pValues = NULL; - - rc = sql_get_statement(v, CONTENT_SELECT_STMT, &s); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_int64(s, 1, iRow); - if( rc!=SQLITE_OK ) return rc; - - rc = sql_step_statement(v, CONTENT_SELECT_STMT, &s); - if( rc!=SQLITE_ROW ) return rc; - - values = (const char **) malloc(v->nColumn * sizeof(const char *)); - for(i=0; inColumn; ++i){ - if( sqlite3_column_type(s, i)==SQLITE_NULL ){ - values[i] = NULL; - }else{ - values[i] = string_dup((char*)sqlite3_column_text(s, i)); - } - } - - /* We expect only one row. We must execute another sqlite3_step() - * to complete the iteration; otherwise the table will remain locked. */ - rc = sqlite3_step(s); - if( rc==SQLITE_DONE ){ - *pValues = values; - return SQLITE_OK; - } - - freeStringArray(v->nColumn, values); - return rc; -} - -/* delete from %_content where rowid = [iRow ] */ -static int content_delete(fulltext_vtab *v, sqlite_int64 iRow){ - sqlite3_stmt *s; - int rc = sql_get_statement(v, CONTENT_DELETE_STMT, &s); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_int64(s, 1, iRow); - if( rc!=SQLITE_OK ) return rc; - - return sql_single_step_statement(v, CONTENT_DELETE_STMT, &s); -} - -/* select rowid, doclist from %_term - * where term = [pTerm] and segment = [iSegment] - * If found, returns SQLITE_ROW; the caller must free the - * returned doclist. If no rows found, returns SQLITE_DONE. */ -static int term_select(fulltext_vtab *v, const char *pTerm, int nTerm, - int iSegment, - sqlite_int64 *rowid, DocList *out){ - sqlite3_stmt *s; - int rc = sql_get_statement(v, TERM_SELECT_STMT, &s); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_text(s, 1, pTerm, nTerm, SQLITE_STATIC); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_int(s, 2, iSegment); - if( rc!=SQLITE_OK ) return rc; - - rc = sql_step_statement(v, TERM_SELECT_STMT, &s); - if( rc!=SQLITE_ROW ) return rc; - - *rowid = sqlite3_column_int64(s, 0); - docListInit(out, DL_DEFAULT, - sqlite3_column_blob(s, 1), sqlite3_column_bytes(s, 1)); - - /* We expect only one row. We must execute another sqlite3_step() - * to complete the iteration; otherwise the table will remain locked. */ - rc = sqlite3_step(s); - return rc==SQLITE_DONE ? SQLITE_ROW : rc; -} - -/* Load the segment doclists for term pTerm and merge them in -** appropriate order into out. Returns SQLITE_OK if successful. If -** there are no segments for pTerm, successfully returns an empty -** doclist in out. -** -** Each document consists of 1 or more "columns". The number of -** columns is v->nColumn. If iColumn==v->nColumn, then return -** position information about all columns. If iColumnnColumn, -** then only return position information about the iColumn-th column -** (where the first column is 0). -*/ -static int term_select_all( - fulltext_vtab *v, /* The fulltext index we are querying against */ - int iColumn, /* If nColumn ){ /* querying a single column */ - docListRestrictColumn(&old, iColumn); - } - - /* doclist contains the newer data, so write it over old. Then - ** steal accumulated result for doclist. - */ - docListAccumulate(&old, &doclist); - docListDestroy(&doclist); - doclist = old; - } - if( rc!=SQLITE_DONE ){ - docListDestroy(&doclist); - return rc; - } - - docListDiscardEmpty(&doclist); - *out = doclist; - return SQLITE_OK; -} - -/* insert into %_term (rowid, term, segment, doclist) - values ([piRowid], [pTerm], [iSegment], [doclist]) -** Lets sqlite select rowid if piRowid is NULL, else uses *piRowid. -** -** NOTE(shess) piRowid is IN, with values of "space of int64" plus -** null, it is not used to pass data back to the caller. -*/ -static int term_insert(fulltext_vtab *v, sqlite_int64 *piRowid, - const char *pTerm, int nTerm, - int iSegment, DocList *doclist){ - sqlite3_stmt *s; - int rc = sql_get_statement(v, TERM_INSERT_STMT, &s); - if( rc!=SQLITE_OK ) return rc; - - if( piRowid==NULL ){ - rc = sqlite3_bind_null(s, 1); - }else{ - rc = sqlite3_bind_int64(s, 1, *piRowid); - } - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_text(s, 2, pTerm, nTerm, SQLITE_STATIC); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_int(s, 3, iSegment); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_blob(s, 4, doclist->pData, doclist->nData, SQLITE_STATIC); - if( rc!=SQLITE_OK ) return rc; - - return sql_single_step_statement(v, TERM_INSERT_STMT, &s); -} - -/* update %_term set doclist = [doclist] where rowid = [rowid] */ -static int term_update(fulltext_vtab *v, sqlite_int64 rowid, - DocList *doclist){ - sqlite3_stmt *s; - int rc = sql_get_statement(v, TERM_UPDATE_STMT, &s); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_blob(s, 1, doclist->pData, doclist->nData, SQLITE_STATIC); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_int64(s, 2, rowid); - if( rc!=SQLITE_OK ) return rc; - - return sql_single_step_statement(v, TERM_UPDATE_STMT, &s); -} - -static int term_delete(fulltext_vtab *v, sqlite_int64 rowid){ - sqlite3_stmt *s; - int rc = sql_get_statement(v, TERM_DELETE_STMT, &s); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_int64(s, 1, rowid); - if( rc!=SQLITE_OK ) return rc; - - return sql_single_step_statement(v, TERM_DELETE_STMT, &s); -} - -/* -** Free the memory used to contain a fulltext_vtab structure. -*/ -static void fulltext_vtab_destroy(fulltext_vtab *v){ - int iStmt, i; - - TRACE(("FTS1 Destroy %p\n", v)); - for( iStmt=0; iStmtpFulltextStatements[iStmt]!=NULL ){ - sqlite3_finalize(v->pFulltextStatements[iStmt]); - v->pFulltextStatements[iStmt] = NULL; - } - } - - if( v->pTokenizer!=NULL ){ - v->pTokenizer->pModule->xDestroy(v->pTokenizer); - v->pTokenizer = NULL; - } - - free(v->azColumn); - for(i = 0; i < v->nColumn; ++i) { - sqlite3_free(v->azContentColumn[i]); - } - free(v->azContentColumn); - free(v); -} - -/* -** Token types for parsing the arguments to xConnect or xCreate. -*/ -#define TOKEN_EOF 0 /* End of file */ -#define TOKEN_SPACE 1 /* Any kind of whitespace */ -#define TOKEN_ID 2 /* An identifier */ -#define TOKEN_STRING 3 /* A string literal */ -#define TOKEN_PUNCT 4 /* A single punctuation character */ - -/* -** If X is a character that can be used in an identifier then -** IdChar(X) will be true. Otherwise it is false. -** -** For ASCII, any character with the high-order bit set is -** allowed in an identifier. For 7-bit characters, -** sqlite3IsIdChar[X] must be 1. -** -** Ticket #1066. the SQL standard does not allow '$' in the -** middle of identfiers. But many SQL implementations do. -** SQLite will allow '$' in identifiers for compatibility. -** But the feature is undocumented. -*/ -static const char isIdChar[] = { -/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */ - 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */ - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */ - 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */ - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */ - 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */ - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */ -}; -#define IdChar(C) (((c=C)&0x80)!=0 || (c>0x1f && isIdChar[c-0x20])) - - -/* -** Return the length of the token that begins at z[0]. -** Store the token type in *tokenType before returning. -*/ -static int getToken(const char *z, int *tokenType){ - int i, c; - switch( *z ){ - case 0: { - *tokenType = TOKEN_EOF; - return 0; - } - case ' ': case '\t': case '\n': case '\f': case '\r': { - for(i=1; safe_isspace(z[i]); i++){} - *tokenType = TOKEN_SPACE; - return i; - } - case '`': - case '\'': - case '"': { - int delim = z[0]; - for(i=1; (c=z[i])!=0; i++){ - if( c==delim ){ - if( z[i+1]==delim ){ - i++; - }else{ - break; - } - } - } - *tokenType = TOKEN_STRING; - return i + (c!=0); - } - case '[': { - for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){} - *tokenType = TOKEN_ID; - return i; - } - default: { - if( !IdChar(*z) ){ - break; - } - for(i=1; IdChar(z[i]); i++){} - *tokenType = TOKEN_ID; - return i; - } - } - *tokenType = TOKEN_PUNCT; - return 1; -} - -/* -** A token extracted from a string is an instance of the following -** structure. -*/ -typedef struct Token { - const char *z; /* Pointer to token text. Not '\000' terminated */ - short int n; /* Length of the token text in bytes. */ -} Token; - -/* -** Given a input string (which is really one of the argv[] parameters -** passed into xConnect or xCreate) split the string up into tokens. -** Return an array of pointers to '\000' terminated strings, one string -** for each non-whitespace token. -** -** The returned array is terminated by a single NULL pointer. -** -** Space to hold the returned array is obtained from a single -** malloc and should be freed by passing the return value to free(). -** The individual strings within the token list are all a part of -** the single memory allocation and will all be freed at once. -*/ -static char **tokenizeString(const char *z, int *pnToken){ - int nToken = 0; - Token *aToken = malloc( strlen(z) * sizeof(aToken[0]) ); - int n = 1; - int e, i; - int totalSize = 0; - char **azToken; - char *zCopy; - while( n>0 ){ - n = getToken(z, &e); - if( e!=TOKEN_SPACE ){ - aToken[nToken].z = z; - aToken[nToken].n = n; - nToken++; - totalSize += n+1; - } - z += n; - } - azToken = (char**)malloc( nToken*sizeof(char*) + totalSize ); - zCopy = (char*)&azToken[nToken]; - nToken--; - for(i=0; i=0 ){ - azIn[j] = azIn[i]; - } - j++; - } - } - azIn[j] = 0; - } -} - - -/* -** Find the first alphanumeric token in the string zIn. Null-terminate -** this token. Remove any quotation marks. And return a pointer to -** the result. -*/ -static char *firstToken(char *zIn, char **pzTail){ - int n, ttype; - while(1){ - n = getToken(zIn, &ttype); - if( ttype==TOKEN_SPACE ){ - zIn += n; - }else if( ttype==TOKEN_EOF ){ - *pzTail = zIn; - return 0; - }else{ - zIn[n] = 0; - *pzTail = &zIn[1]; - dequoteString(zIn); - return zIn; - } - } - /*NOTREACHED*/ -} - -/* Return true if... -** -** * s begins with the string t, ignoring case -** * s is longer than t -** * The first character of s beyond t is not a alphanumeric -** -** Ignore leading space in *s. -** -** To put it another way, return true if the first token of -** s[] is t[]. -*/ -static int startsWith(const char *s, const char *t){ - while( safe_isspace(*s) ){ s++; } - while( *t ){ - if( safe_tolower(*s++)!=safe_tolower(*t++) ) return 0; - } - return *s!='_' && !safe_isalnum(*s); -} - -/* -** An instance of this structure defines the "spec" of a -** full text index. This structure is populated by parseSpec -** and use by fulltextConnect and fulltextCreate. -*/ -typedef struct TableSpec { - const char *zDb; /* Logical database name */ - const char *zName; /* Name of the full-text index */ - int nColumn; /* Number of columns to be indexed */ - char **azColumn; /* Original names of columns to be indexed */ - char **azContentColumn; /* Column names for %_content */ - char **azTokenizer; /* Name of tokenizer and its arguments */ -} TableSpec; - -/* -** Reclaim all of the memory used by a TableSpec -*/ -static void clearTableSpec(TableSpec *p) { - free(p->azColumn); - free(p->azContentColumn); - free(p->azTokenizer); -} - -/* Parse a CREATE VIRTUAL TABLE statement, which looks like this: - * - * CREATE VIRTUAL TABLE email - * USING fts1(subject, body, tokenize mytokenizer(myarg)) - * - * We return parsed information in a TableSpec structure. - * - */ -static int parseSpec(TableSpec *pSpec, int argc, const char *const*argv, - char**pzErr){ - int i, n; - char *z, *zDummy; - char **azArg; - const char *zTokenizer = 0; /* argv[] entry describing the tokenizer */ - - assert( argc>=3 ); - /* Current interface: - ** argv[0] - module name - ** argv[1] - database name - ** argv[2] - table name - ** argv[3..] - columns, optionally followed by tokenizer specification - ** and snippet delimiters specification. - */ - - /* Make a copy of the complete argv[][] array in a single allocation. - ** The argv[][] array is read-only and transient. We can write to the - ** copy in order to modify things and the copy is persistent. - */ - memset(pSpec, 0, sizeof(*pSpec)); - for(i=n=0; izDb = azArg[1]; - pSpec->zName = azArg[2]; - pSpec->nColumn = 0; - pSpec->azColumn = azArg; - zTokenizer = "tokenize simple"; - for(i=3; inColumn] = firstToken(azArg[i], &zDummy); - pSpec->nColumn++; - } - } - if( pSpec->nColumn==0 ){ - azArg[0] = "content"; - pSpec->nColumn = 1; - } - - /* - ** Construct the list of content column names. - ** - ** Each content column name will be of the form cNNAAAA - ** where NN is the column number and AAAA is the sanitized - ** column name. "sanitized" means that special characters are - ** converted to "_". The cNN prefix guarantees that all column - ** names are unique. - ** - ** The AAAA suffix is not strictly necessary. It is included - ** for the convenience of people who might examine the generated - ** %_content table and wonder what the columns are used for. - */ - pSpec->azContentColumn = malloc( pSpec->nColumn * sizeof(char *) ); - if( pSpec->azContentColumn==0 ){ - clearTableSpec(pSpec); - return SQLITE_NOMEM; - } - for(i=0; inColumn; i++){ - char *p; - pSpec->azContentColumn[i] = sqlite3_mprintf("c%d%s", i, azArg[i]); - for (p = pSpec->azContentColumn[i]; *p ; ++p) { - if( !safe_isalnum(*p) ) *p = '_'; - } - } - - /* - ** Parse the tokenizer specification string. - */ - pSpec->azTokenizer = tokenizeString(zTokenizer, &n); - tokenListToIdList(pSpec->azTokenizer); - - return SQLITE_OK; -} - -/* -** Generate a CREATE TABLE statement that describes the schema of -** the virtual table. Return a pointer to this schema string. -** -** Space is obtained from sqlite3_mprintf() and should be freed -** using sqlite3_free(). -*/ -static char *fulltextSchema( - int nColumn, /* Number of columns */ - const char *const* azColumn, /* List of columns */ - const char *zTableName /* Name of the table */ -){ - int i; - char *zSchema, *zNext; - const char *zSep = "("; - zSchema = sqlite3_mprintf("CREATE TABLE x"); - for(i=0; ibase */ - v->db = db; - v->zDb = spec->zDb; /* Freed when azColumn is freed */ - v->zName = spec->zName; /* Freed when azColumn is freed */ - v->nColumn = spec->nColumn; - v->azContentColumn = spec->azContentColumn; - spec->azContentColumn = 0; - v->azColumn = spec->azColumn; - spec->azColumn = 0; - - if( spec->azTokenizer==0 ){ - return SQLITE_NOMEM; - } - /* TODO(shess) For now, add new tokenizers as else if clauses. */ - if( spec->azTokenizer[0]==0 || startsWith(spec->azTokenizer[0], "simple") ){ - sqlite3Fts1SimpleTokenizerModule(&m); - }else if( startsWith(spec->azTokenizer[0], "porter") ){ - sqlite3Fts1PorterTokenizerModule(&m); - }else{ - *pzErr = sqlite3_mprintf("unknown tokenizer: %s", spec->azTokenizer[0]); - rc = SQLITE_ERROR; - goto err; - } - for(n=0; spec->azTokenizer[n]; n++){} - if( n ){ - rc = m->xCreate(n-1, (const char*const*)&spec->azTokenizer[1], - &v->pTokenizer); - }else{ - rc = m->xCreate(0, 0, &v->pTokenizer); - } - if( rc!=SQLITE_OK ) goto err; - v->pTokenizer->pModule = m; - - /* TODO: verify the existence of backing tables foo_content, foo_term */ - - schema = fulltextSchema(v->nColumn, (const char*const*)v->azColumn, - spec->zName); - rc = sqlite3_declare_vtab(db, schema); - sqlite3_free(schema); - if( rc!=SQLITE_OK ) goto err; - - memset(v->pFulltextStatements, 0, sizeof(v->pFulltextStatements)); - - *ppVTab = &v->base; - TRACE(("FTS1 Connect %p\n", v)); - - return rc; - -err: - fulltext_vtab_destroy(v); - return rc; -} - -static int fulltextConnect( - sqlite3 *db, - void *pAux, - int argc, const char *const*argv, - sqlite3_vtab **ppVTab, - char **pzErr -){ - TableSpec spec; - int rc = parseSpec(&spec, argc, argv, pzErr); - if( rc!=SQLITE_OK ) return rc; - - rc = constructVtab(db, &spec, ppVTab, pzErr); - clearTableSpec(&spec); - return rc; -} - - /* The %_content table holds the text of each document, with - ** the rowid used as the docid. - ** - ** The %_term table maps each term to a document list blob - ** containing elements sorted by ascending docid, each element - ** encoded as: - ** - ** docid varint-encoded - ** token elements: - ** position+1 varint-encoded as delta from previous position - ** start offset varint-encoded as delta from previous start offset - ** end offset varint-encoded as delta from start offset - ** - ** The sentinel position of 0 indicates the end of the token list. - ** - ** Additionally, doclist blobs are chunked into multiple segments, - ** using segment to order the segments. New elements are added to - ** the segment at segment 0, until it exceeds CHUNK_MAX. Then - ** segment 0 is deleted, and the doclist is inserted at segment 1. - ** If there is already a doclist at segment 1, the segment 0 doclist - ** is merged with it, the segment 1 doclist is deleted, and the - ** merged doclist is inserted at segment 2, repeating those - ** operations until an insert succeeds. - ** - ** Since this structure doesn't allow us to update elements in place - ** in case of deletion or update, these are simply written to - ** segment 0 (with an empty token list in case of deletion), with - ** docListAccumulate() taking care to retain lower-segment - ** information in preference to higher-segment information. - */ - /* TODO(shess) Provide a VACUUM type operation which both removes - ** deleted elements which are no longer necessary, and duplicated - ** elements. I suspect this will probably not be necessary in - ** practice, though. - */ -static int fulltextCreate(sqlite3 *db, void *pAux, - int argc, const char * const *argv, - sqlite3_vtab **ppVTab, char **pzErr){ - int rc; - TableSpec spec; - StringBuffer schema; - TRACE(("FTS1 Create\n")); - - rc = parseSpec(&spec, argc, argv, pzErr); - if( rc!=SQLITE_OK ) return rc; - - initStringBuffer(&schema); - append(&schema, "CREATE TABLE %_content("); - appendList(&schema, spec.nColumn, spec.azContentColumn); - append(&schema, ")"); - rc = sql_exec(db, spec.zDb, spec.zName, schema.s); - free(schema.s); - if( rc!=SQLITE_OK ) goto out; - - rc = sql_exec(db, spec.zDb, spec.zName, - "create table %_term(term text, segment integer, doclist blob, " - "primary key(term, segment));"); - if( rc!=SQLITE_OK ) goto out; - - rc = constructVtab(db, &spec, ppVTab, pzErr); - -out: - clearTableSpec(&spec); - return rc; -} - -/* Decide how to handle an SQL query. */ -static int fulltextBestIndex(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){ - int i; - TRACE(("FTS1 BestIndex\n")); - - for(i=0; inConstraint; ++i){ - const struct sqlite3_index_constraint *pConstraint; - pConstraint = &pInfo->aConstraint[i]; - if( pConstraint->usable ) { - if( pConstraint->iColumn==-1 && - pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){ - pInfo->idxNum = QUERY_ROWID; /* lookup by rowid */ - TRACE(("FTS1 QUERY_ROWID\n")); - } else if( pConstraint->iColumn>=0 && - pConstraint->op==SQLITE_INDEX_CONSTRAINT_MATCH ){ - /* full-text search */ - pInfo->idxNum = QUERY_FULLTEXT + pConstraint->iColumn; - TRACE(("FTS1 QUERY_FULLTEXT %d\n", pConstraint->iColumn)); - } else continue; - - pInfo->aConstraintUsage[i].argvIndex = 1; - pInfo->aConstraintUsage[i].omit = 1; - - /* An arbitrary value for now. - * TODO: Perhaps rowid matches should be considered cheaper than - * full-text searches. */ - pInfo->estimatedCost = 1.0; - - return SQLITE_OK; - } - } - pInfo->idxNum = QUERY_GENERIC; - return SQLITE_OK; -} - -static int fulltextDisconnect(sqlite3_vtab *pVTab){ - TRACE(("FTS1 Disconnect %p\n", pVTab)); - fulltext_vtab_destroy((fulltext_vtab *)pVTab); - return SQLITE_OK; -} - -static int fulltextDestroy(sqlite3_vtab *pVTab){ - fulltext_vtab *v = (fulltext_vtab *)pVTab; - int rc; - - TRACE(("FTS1 Destroy %p\n", pVTab)); - rc = sql_exec(v->db, v->zDb, v->zName, - "drop table if exists %_content;" - "drop table if exists %_term;" - ); - if( rc!=SQLITE_OK ) return rc; - - fulltext_vtab_destroy((fulltext_vtab *)pVTab); - return SQLITE_OK; -} - -static int fulltextOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){ - fulltext_cursor *c; - - c = (fulltext_cursor *) calloc(sizeof(fulltext_cursor), 1); - /* sqlite will initialize c->base */ - *ppCursor = &c->base; - TRACE(("FTS1 Open %p: %p\n", pVTab, c)); - - return SQLITE_OK; -} - - -/* Free all of the dynamically allocated memory held by *q -*/ -static void queryClear(Query *q){ - int i; - for(i = 0; i < q->nTerms; ++i){ - free(q->pTerms[i].pTerm); - } - free(q->pTerms); - memset(q, 0, sizeof(*q)); -} - -/* Free all of the dynamically allocated memory held by the -** Snippet -*/ -static void snippetClear(Snippet *p){ - free(p->aMatch); - free(p->zOffset); - free(p->zSnippet); - memset(p, 0, sizeof(*p)); -} -/* -** Append a single entry to the p->aMatch[] log. -*/ -static void snippetAppendMatch( - Snippet *p, /* Append the entry to this snippet */ - int iCol, int iTerm, /* The column and query term */ - int iStart, int nByte /* Offset and size of the match */ -){ - int i; - struct snippetMatch *pMatch; - if( p->nMatch+1>=p->nAlloc ){ - p->nAlloc = p->nAlloc*2 + 10; - p->aMatch = realloc(p->aMatch, p->nAlloc*sizeof(p->aMatch[0]) ); - if( p->aMatch==0 ){ - p->nMatch = 0; - p->nAlloc = 0; - return; - } - } - i = p->nMatch++; - pMatch = &p->aMatch[i]; - pMatch->iCol = iCol; - pMatch->iTerm = iTerm; - pMatch->iStart = iStart; - pMatch->nByte = nByte; -} - -/* -** Sizing information for the circular buffer used in snippetOffsetsOfColumn() -*/ -#define FTS1_ROTOR_SZ (32) -#define FTS1_ROTOR_MASK (FTS1_ROTOR_SZ-1) - -/* -** Add entries to pSnippet->aMatch[] for every match that occurs against -** document zDoc[0..nDoc-1] which is stored in column iColumn. -*/ -static void snippetOffsetsOfColumn( - Query *pQuery, - Snippet *pSnippet, - int iColumn, - const char *zDoc, - int nDoc -){ - const sqlite3_tokenizer_module *pTModule; /* The tokenizer module */ - sqlite3_tokenizer *pTokenizer; /* The specific tokenizer */ - sqlite3_tokenizer_cursor *pTCursor; /* Tokenizer cursor */ - fulltext_vtab *pVtab; /* The full text index */ - int nColumn; /* Number of columns in the index */ - const QueryTerm *aTerm; /* Query string terms */ - int nTerm; /* Number of query string terms */ - int i, j; /* Loop counters */ - int rc; /* Return code */ - unsigned int match, prevMatch; /* Phrase search bitmasks */ - const char *zToken; /* Next token from the tokenizer */ - int nToken; /* Size of zToken */ - int iBegin, iEnd, iPos; /* Offsets of beginning and end */ - - /* The following variables keep a circular buffer of the last - ** few tokens */ - unsigned int iRotor = 0; /* Index of current token */ - int iRotorBegin[FTS1_ROTOR_SZ]; /* Beginning offset of token */ - int iRotorLen[FTS1_ROTOR_SZ]; /* Length of token */ - - pVtab = pQuery->pFts; - nColumn = pVtab->nColumn; - pTokenizer = pVtab->pTokenizer; - pTModule = pTokenizer->pModule; - rc = pTModule->xOpen(pTokenizer, zDoc, nDoc, &pTCursor); - if( rc ) return; - pTCursor->pTokenizer = pTokenizer; - aTerm = pQuery->pTerms; - nTerm = pQuery->nTerms; - if( nTerm>=FTS1_ROTOR_SZ ){ - nTerm = FTS1_ROTOR_SZ - 1; - } - prevMatch = 0; - while(1){ - rc = pTModule->xNext(pTCursor, &zToken, &nToken, &iBegin, &iEnd, &iPos); - if( rc ) break; - iRotorBegin[iRotor&FTS1_ROTOR_MASK] = iBegin; - iRotorLen[iRotor&FTS1_ROTOR_MASK] = iEnd-iBegin; - match = 0; - for(i=0; i=0 && iCol1 && (prevMatch & (1<=0; j--){ - int k = (iRotor-j) & FTS1_ROTOR_MASK; - snippetAppendMatch(pSnippet, iColumn, i-j, - iRotorBegin[k], iRotorLen[k]); - } - } - } - prevMatch = match<<1; - iRotor++; - } - pTModule->xClose(pTCursor); -} - - -/* -** Compute all offsets for the current row of the query. -** If the offsets have already been computed, this routine is a no-op. -*/ -static void snippetAllOffsets(fulltext_cursor *p){ - int nColumn; - int iColumn, i; - int iFirst, iLast; - fulltext_vtab *pFts; - - if( p->snippet.nMatch ) return; - if( p->q.nTerms==0 ) return; - pFts = p->q.pFts; - nColumn = pFts->nColumn; - iColumn = p->iCursorType - QUERY_FULLTEXT; - if( iColumn<0 || iColumn>=nColumn ){ - iFirst = 0; - iLast = nColumn-1; - }else{ - iFirst = iColumn; - iLast = iColumn; - } - for(i=iFirst; i<=iLast; i++){ - const char *zDoc; - int nDoc; - zDoc = (const char*)sqlite3_column_text(p->pStmt, i+1); - nDoc = sqlite3_column_bytes(p->pStmt, i+1); - snippetOffsetsOfColumn(&p->q, &p->snippet, i, zDoc, nDoc); - } -} - -/* -** Convert the information in the aMatch[] array of the snippet -** into the string zOffset[0..nOffset-1]. -*/ -static void snippetOffsetText(Snippet *p){ - int i; - int cnt = 0; - StringBuffer sb; - char zBuf[200]; - if( p->zOffset ) return; - initStringBuffer(&sb); - for(i=0; inMatch; i++){ - struct snippetMatch *pMatch = &p->aMatch[i]; - zBuf[0] = ' '; - sqlite3_snprintf(sizeof(zBuf)-1, &zBuf[cnt>0], "%d %d %d %d", - pMatch->iCol, pMatch->iTerm, pMatch->iStart, pMatch->nByte); - append(&sb, zBuf); - cnt++; - } - p->zOffset = sb.s; - p->nOffset = sb.len; -} - -/* -** zDoc[0..nDoc-1] is phrase of text. aMatch[0..nMatch-1] are a set -** of matching words some of which might be in zDoc. zDoc is column -** number iCol. -** -** iBreak is suggested spot in zDoc where we could begin or end an -** excerpt. Return a value similar to iBreak but possibly adjusted -** to be a little left or right so that the break point is better. -*/ -static int wordBoundary( - int iBreak, /* The suggested break point */ - const char *zDoc, /* Document text */ - int nDoc, /* Number of bytes in zDoc[] */ - struct snippetMatch *aMatch, /* Matching words */ - int nMatch, /* Number of entries in aMatch[] */ - int iCol /* The column number for zDoc[] */ -){ - int i; - if( iBreak<=10 ){ - return 0; - } - if( iBreak>=nDoc-10 ){ - return nDoc; - } - for(i=0; i0 && aMatch[i-1].iStart+aMatch[i-1].nByte>=iBreak ){ - return aMatch[i-1].iStart; - } - } - for(i=1; i<=10; i++){ - if( safe_isspace(zDoc[iBreak-i]) ){ - return iBreak - i + 1; - } - if( safe_isspace(zDoc[iBreak+i]) ){ - return iBreak + i + 1; - } - } - return iBreak; -} - -/* -** If the StringBuffer does not end in white space, add a single -** space character to the end. -*/ -static void appendWhiteSpace(StringBuffer *p){ - if( p->len==0 ) return; - if( safe_isspace(p->s[p->len-1]) ) return; - append(p, " "); -} - -/* -** Remove white space from teh end of the StringBuffer -*/ -static void trimWhiteSpace(StringBuffer *p){ - while( p->len>0 && safe_isspace(p->s[p->len-1]) ){ - p->len--; - } -} - - - -/* -** Allowed values for Snippet.aMatch[].snStatus -*/ -#define SNIPPET_IGNORE 0 /* It is ok to omit this match from the snippet */ -#define SNIPPET_DESIRED 1 /* We want to include this match in the snippet */ - -/* -** Generate the text of a snippet. -*/ -static void snippetText( - fulltext_cursor *pCursor, /* The cursor we need the snippet for */ - const char *zStartMark, /* Markup to appear before each match */ - const char *zEndMark, /* Markup to appear after each match */ - const char *zEllipsis /* Ellipsis mark */ -){ - int i, j; - struct snippetMatch *aMatch; - int nMatch; - int nDesired; - StringBuffer sb; - int tailCol; - int tailOffset; - int iCol; - int nDoc; - const char *zDoc; - int iStart, iEnd; - int tailEllipsis = 0; - int iMatch; - - - free(pCursor->snippet.zSnippet); - pCursor->snippet.zSnippet = 0; - aMatch = pCursor->snippet.aMatch; - nMatch = pCursor->snippet.nMatch; - initStringBuffer(&sb); - - for(i=0; iq.nTerms; i++){ - for(j=0; j0; i++){ - if( aMatch[i].snStatus!=SNIPPET_DESIRED ) continue; - nDesired--; - iCol = aMatch[i].iCol; - zDoc = (const char*)sqlite3_column_text(pCursor->pStmt, iCol+1); - nDoc = sqlite3_column_bytes(pCursor->pStmt, iCol+1); - iStart = aMatch[i].iStart - 40; - iStart = wordBoundary(iStart, zDoc, nDoc, aMatch, nMatch, iCol); - if( iStart<=10 ){ - iStart = 0; - } - if( iCol==tailCol && iStart<=tailOffset+20 ){ - iStart = tailOffset; - } - if( (iCol!=tailCol && tailCol>=0) || iStart!=tailOffset ){ - trimWhiteSpace(&sb); - appendWhiteSpace(&sb); - append(&sb, zEllipsis); - appendWhiteSpace(&sb); - } - iEnd = aMatch[i].iStart + aMatch[i].nByte + 40; - iEnd = wordBoundary(iEnd, zDoc, nDoc, aMatch, nMatch, iCol); - if( iEnd>=nDoc-10 ){ - iEnd = nDoc; - tailEllipsis = 0; - }else{ - tailEllipsis = 1; - } - while( iMatchsnippet.zSnippet = sb.s; - pCursor->snippet.nSnippet = sb.len; -} - - -/* -** Close the cursor. For additional information see the documentation -** on the xClose method of the virtual table interface. -*/ -static int fulltextClose(sqlite3_vtab_cursor *pCursor){ - fulltext_cursor *c = (fulltext_cursor *) pCursor; - TRACE(("FTS1 Close %p\n", c)); - sqlite3_finalize(c->pStmt); - queryClear(&c->q); - snippetClear(&c->snippet); - if( c->result.pDoclist!=NULL ){ - docListDelete(c->result.pDoclist); - } - free(c); - return SQLITE_OK; -} - -static int fulltextNext(sqlite3_vtab_cursor *pCursor){ - fulltext_cursor *c = (fulltext_cursor *) pCursor; - sqlite_int64 iDocid; - int rc; - - TRACE(("FTS1 Next %p\n", pCursor)); - snippetClear(&c->snippet); - if( c->iCursorType < QUERY_FULLTEXT ){ - /* TODO(shess) Handle SQLITE_SCHEMA AND SQLITE_BUSY. */ - rc = sqlite3_step(c->pStmt); - switch( rc ){ - case SQLITE_ROW: - c->eof = 0; - return SQLITE_OK; - case SQLITE_DONE: - c->eof = 1; - return SQLITE_OK; - default: - c->eof = 1; - return rc; - } - } else { /* full-text query */ - rc = sqlite3_reset(c->pStmt); - if( rc!=SQLITE_OK ) return rc; - - iDocid = nextDocid(&c->result); - if( iDocid==0 ){ - c->eof = 1; - return SQLITE_OK; - } - rc = sqlite3_bind_int64(c->pStmt, 1, iDocid); - if( rc!=SQLITE_OK ) return rc; - /* TODO(shess) Handle SQLITE_SCHEMA AND SQLITE_BUSY. */ - rc = sqlite3_step(c->pStmt); - if( rc==SQLITE_ROW ){ /* the case we expect */ - c->eof = 0; - return SQLITE_OK; - } - /* an error occurred; abort */ - return rc==SQLITE_DONE ? SQLITE_ERROR : rc; - } -} - - -/* Return a DocList corresponding to the query term *pTerm. If *pTerm -** is the first term of a phrase query, go ahead and evaluate the phrase -** query and return the doclist for the entire phrase query. -** -** The result is stored in pTerm->doclist. -*/ -static int docListOfTerm( - fulltext_vtab *v, /* The full text index */ - int iColumn, /* column to restrict to. No restrition if >=nColumn */ - QueryTerm *pQTerm, /* Term we are looking for, or 1st term of a phrase */ - DocList **ppResult /* Write the result here */ -){ - DocList *pLeft, *pRight, *pNew; - int i, rc; - - pLeft = docListNew(DL_POSITIONS); - rc = term_select_all(v, iColumn, pQTerm->pTerm, pQTerm->nTerm, pLeft); - if( rc ){ - docListDelete(pLeft); - return rc; - } - for(i=1; i<=pQTerm->nPhrase; i++){ - pRight = docListNew(DL_POSITIONS); - rc = term_select_all(v, iColumn, pQTerm[i].pTerm, pQTerm[i].nTerm, pRight); - if( rc ){ - docListDelete(pLeft); - return rc; - } - pNew = docListNew(inPhrase ? DL_POSITIONS : DL_DOCIDS); - docListPhraseMerge(pLeft, pRight, pNew); - docListDelete(pLeft); - docListDelete(pRight); - pLeft = pNew; - } - *ppResult = pLeft; - return SQLITE_OK; -} - -/* Add a new term pTerm[0..nTerm-1] to the query *q. -*/ -static void queryAdd(Query *q, const char *pTerm, int nTerm){ - QueryTerm *t; - ++q->nTerms; - q->pTerms = realloc(q->pTerms, q->nTerms * sizeof(q->pTerms[0])); - if( q->pTerms==0 ){ - q->nTerms = 0; - return; - } - t = &q->pTerms[q->nTerms - 1]; - memset(t, 0, sizeof(*t)); - t->pTerm = malloc(nTerm+1); - memcpy(t->pTerm, pTerm, nTerm); - t->pTerm[nTerm] = 0; - t->nTerm = nTerm; - t->isOr = q->nextIsOr; - q->nextIsOr = 0; - t->iColumn = q->nextColumn; - q->nextColumn = q->dfltColumn; -} - -/* -** Check to see if the string zToken[0...nToken-1] matches any -** column name in the virtual table. If it does, -** return the zero-indexed column number. If not, return -1. -*/ -static int checkColumnSpecifier( - fulltext_vtab *pVtab, /* The virtual table */ - const char *zToken, /* Text of the token */ - int nToken /* Number of characters in the token */ -){ - int i; - for(i=0; inColumn; i++){ - if( memcmp(pVtab->azColumn[i], zToken, nToken)==0 - && pVtab->azColumn[i][nToken]==0 ){ - return i; - } - } - return -1; -} - -/* -** Parse the text at pSegment[0..nSegment-1]. Add additional terms -** to the query being assemblied in pQuery. -** -** inPhrase is true if pSegment[0..nSegement-1] is contained within -** double-quotes. If inPhrase is true, then the first term -** is marked with the number of terms in the phrase less one and -** OR and "-" syntax is ignored. If inPhrase is false, then every -** term found is marked with nPhrase=0 and OR and "-" syntax is significant. -*/ -static int tokenizeSegment( - sqlite3_tokenizer *pTokenizer, /* The tokenizer to use */ - const char *pSegment, int nSegment, /* Query expression being parsed */ - int inPhrase, /* True if within "..." */ - Query *pQuery /* Append results here */ -){ - const sqlite3_tokenizer_module *pModule = pTokenizer->pModule; - sqlite3_tokenizer_cursor *pCursor; - int firstIndex = pQuery->nTerms; - int iCol; - int nTerm = 1; - - int rc = pModule->xOpen(pTokenizer, pSegment, nSegment, &pCursor); - if( rc!=SQLITE_OK ) return rc; - pCursor->pTokenizer = pTokenizer; - - while( 1 ){ - const char *pToken; - int nToken, iBegin, iEnd, iPos; - - rc = pModule->xNext(pCursor, - &pToken, &nToken, - &iBegin, &iEnd, &iPos); - if( rc!=SQLITE_OK ) break; - if( !inPhrase && - pSegment[iEnd]==':' && - (iCol = checkColumnSpecifier(pQuery->pFts, pToken, nToken))>=0 ){ - pQuery->nextColumn = iCol; - continue; - } - if( !inPhrase && pQuery->nTerms>0 && nToken==2 - && pSegment[iBegin]=='O' && pSegment[iBegin+1]=='R' ){ - pQuery->nextIsOr = 1; - continue; - } - queryAdd(pQuery, pToken, nToken); - if( !inPhrase && iBegin>0 && pSegment[iBegin-1]=='-' ){ - pQuery->pTerms[pQuery->nTerms-1].isNot = 1; - } - pQuery->pTerms[pQuery->nTerms-1].iPhrase = nTerm; - if( inPhrase ){ - nTerm++; - } - } - - if( inPhrase && pQuery->nTerms>firstIndex ){ - pQuery->pTerms[firstIndex].nPhrase = pQuery->nTerms - firstIndex - 1; - } - - return pModule->xClose(pCursor); -} - -/* Parse a query string, yielding a Query object pQuery. -** -** The calling function will need to queryClear() to clean up -** the dynamically allocated memory held by pQuery. -*/ -static int parseQuery( - fulltext_vtab *v, /* The fulltext index */ - const char *zInput, /* Input text of the query string */ - int nInput, /* Size of the input text */ - int dfltColumn, /* Default column of the index to match against */ - Query *pQuery /* Write the parse results here. */ -){ - int iInput, inPhrase = 0; - - if( zInput==0 ) nInput = 0; - if( nInput<0 ) nInput = strlen(zInput); - pQuery->nTerms = 0; - pQuery->pTerms = NULL; - pQuery->nextIsOr = 0; - pQuery->nextColumn = dfltColumn; - pQuery->dfltColumn = dfltColumn; - pQuery->pFts = v; - - for(iInput=0; iInputiInput ){ - tokenizeSegment(v->pTokenizer, zInput+iInput, i-iInput, inPhrase, - pQuery); - } - iInput = i; - if( i=nColumn -** they are allowed to match against any column. -*/ -static int fulltextQuery( - fulltext_vtab *v, /* The full text index */ - int iColumn, /* Match against this column by default */ - const char *zInput, /* The query string */ - int nInput, /* Number of bytes in zInput[] */ - DocList **pResult, /* Write the result doclist here */ - Query *pQuery /* Put parsed query string here */ -){ - int i, iNext, rc; - DocList *pLeft = NULL; - DocList *pRight, *pNew, *pOr; - int nNot = 0; - QueryTerm *aTerm; - - rc = parseQuery(v, zInput, nInput, iColumn, pQuery); - if( rc!=SQLITE_OK ) return rc; - - /* Merge AND terms. */ - aTerm = pQuery->pTerms; - for(i = 0; inTerms; i=iNext){ - if( aTerm[i].isNot ){ - /* Handle all NOT terms in a separate pass */ - nNot++; - iNext = i + aTerm[i].nPhrase+1; - continue; - } - iNext = i + aTerm[i].nPhrase + 1; - rc = docListOfTerm(v, aTerm[i].iColumn, &aTerm[i], &pRight); - if( rc ){ - queryClear(pQuery); - return rc; - } - while( iNextnTerms && aTerm[iNext].isOr ){ - rc = docListOfTerm(v, aTerm[iNext].iColumn, &aTerm[iNext], &pOr); - iNext += aTerm[iNext].nPhrase + 1; - if( rc ){ - queryClear(pQuery); - return rc; - } - pNew = docListNew(DL_DOCIDS); - docListOrMerge(pRight, pOr, pNew); - docListDelete(pRight); - docListDelete(pOr); - pRight = pNew; - } - if( pLeft==0 ){ - pLeft = pRight; - }else{ - pNew = docListNew(DL_DOCIDS); - docListAndMerge(pLeft, pRight, pNew); - docListDelete(pRight); - docListDelete(pLeft); - pLeft = pNew; - } - } - - if( nNot && pLeft==0 ){ - /* We do not yet know how to handle a query of only NOT terms */ - return SQLITE_ERROR; - } - - /* Do the EXCEPT terms */ - for(i=0; inTerms; i += aTerm[i].nPhrase + 1){ - if( !aTerm[i].isNot ) continue; - rc = docListOfTerm(v, aTerm[i].iColumn, &aTerm[i], &pRight); - if( rc ){ - queryClear(pQuery); - docListDelete(pLeft); - return rc; - } - pNew = docListNew(DL_DOCIDS); - docListExceptMerge(pLeft, pRight, pNew); - docListDelete(pRight); - docListDelete(pLeft); - pLeft = pNew; - } - - *pResult = pLeft; - return rc; -} - -/* -** This is the xFilter interface for the virtual table. See -** the virtual table xFilter method documentation for additional -** information. -** -** If idxNum==QUERY_GENERIC then do a full table scan against -** the %_content table. -** -** If idxNum==QUERY_ROWID then do a rowid lookup for a single entry -** in the %_content table. -** -** If idxNum>=QUERY_FULLTEXT then use the full text index. The -** column on the left-hand side of the MATCH operator is column -** number idxNum-QUERY_FULLTEXT, 0 indexed. argv[0] is the right-hand -** side of the MATCH operator. -*/ -/* TODO(shess) Upgrade the cursor initialization and destruction to -** account for fulltextFilter() being called multiple times on the -** same cursor. The current solution is very fragile. Apply fix to -** fts2 as appropriate. -*/ -static int fulltextFilter( - sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */ - int idxNum, const char *idxStr, /* Which indexing scheme to use */ - int argc, sqlite3_value **argv /* Arguments for the indexing scheme */ -){ - fulltext_cursor *c = (fulltext_cursor *) pCursor; - fulltext_vtab *v = cursor_vtab(c); - int rc; - char *zSql; - - TRACE(("FTS1 Filter %p\n",pCursor)); - - zSql = sqlite3_mprintf("select rowid, * from %%_content %s", - idxNum==QUERY_GENERIC ? "" : "where rowid=?"); - sqlite3_finalize(c->pStmt); - rc = sql_prepare(v->db, v->zDb, v->zName, &c->pStmt, zSql); - sqlite3_free(zSql); - if( rc!=SQLITE_OK ) return rc; - - c->iCursorType = idxNum; - switch( idxNum ){ - case QUERY_GENERIC: - break; - - case QUERY_ROWID: - rc = sqlite3_bind_int64(c->pStmt, 1, sqlite3_value_int64(argv[0])); - if( rc!=SQLITE_OK ) return rc; - break; - - default: /* full-text search */ - { - const char *zQuery = (const char *)sqlite3_value_text(argv[0]); - DocList *pResult; - assert( idxNum<=QUERY_FULLTEXT+v->nColumn); - assert( argc==1 ); - queryClear(&c->q); - rc = fulltextQuery(v, idxNum-QUERY_FULLTEXT, zQuery, -1, &pResult, &c->q); - if( rc!=SQLITE_OK ) return rc; - if( c->result.pDoclist!=NULL ) docListDelete(c->result.pDoclist); - readerInit(&c->result, pResult); - break; - } - } - - return fulltextNext(pCursor); -} - -/* This is the xEof method of the virtual table. The SQLite core -** calls this routine to find out if it has reached the end of -** a query's results set. -*/ -static int fulltextEof(sqlite3_vtab_cursor *pCursor){ - fulltext_cursor *c = (fulltext_cursor *) pCursor; - return c->eof; -} - -/* This is the xColumn method of the virtual table. The SQLite -** core calls this method during a query when it needs the value -** of a column from the virtual table. This method needs to use -** one of the sqlite3_result_*() routines to store the requested -** value back in the pContext. -*/ -static int fulltextColumn(sqlite3_vtab_cursor *pCursor, - sqlite3_context *pContext, int idxCol){ - fulltext_cursor *c = (fulltext_cursor *) pCursor; - fulltext_vtab *v = cursor_vtab(c); - - if( idxColnColumn ){ - sqlite3_value *pVal = sqlite3_column_value(c->pStmt, idxCol+1); - sqlite3_result_value(pContext, pVal); - }else if( idxCol==v->nColumn ){ - /* The extra column whose name is the same as the table. - ** Return a blob which is a pointer to the cursor - */ - sqlite3_result_blob(pContext, &c, sizeof(c), SQLITE_TRANSIENT); - } - return SQLITE_OK; -} - -/* This is the xRowid method. The SQLite core calls this routine to -** retrive the rowid for the current row of the result set. The -** rowid should be written to *pRowid. -*/ -static int fulltextRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){ - fulltext_cursor *c = (fulltext_cursor *) pCursor; - - *pRowid = sqlite3_column_int64(c->pStmt, 0); - return SQLITE_OK; -} - -/* Add all terms in [zText] to the given hash table. If [iColumn] > 0, - * we also store positions and offsets in the hash table using the given - * column number. */ -static int buildTerms(fulltext_vtab *v, fts1Hash *terms, sqlite_int64 iDocid, - const char *zText, int iColumn){ - sqlite3_tokenizer *pTokenizer = v->pTokenizer; - sqlite3_tokenizer_cursor *pCursor; - const char *pToken; - int nTokenBytes; - int iStartOffset, iEndOffset, iPosition; - int rc; - - rc = pTokenizer->pModule->xOpen(pTokenizer, zText, -1, &pCursor); - if( rc!=SQLITE_OK ) return rc; - - pCursor->pTokenizer = pTokenizer; - while( SQLITE_OK==pTokenizer->pModule->xNext(pCursor, - &pToken, &nTokenBytes, - &iStartOffset, &iEndOffset, - &iPosition) ){ - DocList *p; - - /* Positions can't be negative; we use -1 as a terminator internally. */ - if( iPosition<0 ){ - pTokenizer->pModule->xClose(pCursor); - return SQLITE_ERROR; - } - - p = fts1HashFind(terms, pToken, nTokenBytes); - if( p==NULL ){ - p = docListNew(DL_DEFAULT); - docListAddDocid(p, iDocid); - fts1HashInsert(terms, pToken, nTokenBytes, p); - } - if( iColumn>=0 ){ - docListAddPosOffset(p, iColumn, iPosition, iStartOffset, iEndOffset); - } - } - - /* TODO(shess) Check return? Should this be able to cause errors at - ** this point? Actually, same question about sqlite3_finalize(), - ** though one could argue that failure there means that the data is - ** not durable. *ponder* - */ - pTokenizer->pModule->xClose(pCursor); - return rc; -} - -/* Update the %_terms table to map the term [pTerm] to the given rowid. */ -static int index_insert_term(fulltext_vtab *v, const char *pTerm, int nTerm, - DocList *d){ - sqlite_int64 iIndexRow; - DocList doclist; - int iSegment = 0, rc; - - rc = term_select(v, pTerm, nTerm, iSegment, &iIndexRow, &doclist); - if( rc==SQLITE_DONE ){ - docListInit(&doclist, DL_DEFAULT, 0, 0); - docListUpdate(&doclist, d); - /* TODO(shess) Consider length(doclist)>CHUNK_MAX? */ - rc = term_insert(v, NULL, pTerm, nTerm, iSegment, &doclist); - goto err; - } - if( rc!=SQLITE_ROW ) return SQLITE_ERROR; - - docListUpdate(&doclist, d); - if( doclist.nData<=CHUNK_MAX ){ - rc = term_update(v, iIndexRow, &doclist); - goto err; - } - - /* Doclist doesn't fit, delete what's there, and accumulate - ** forward. - */ - rc = term_delete(v, iIndexRow); - if( rc!=SQLITE_OK ) goto err; - - /* Try to insert the doclist into a higher segment bucket. On - ** failure, accumulate existing doclist with the doclist from that - ** bucket, and put results in the next bucket. - */ - iSegment++; - while( (rc=term_insert(v, &iIndexRow, pTerm, nTerm, iSegment, - &doclist))!=SQLITE_OK ){ - sqlite_int64 iSegmentRow; - DocList old; - int rc2; - - /* Retain old error in case the term_insert() error was really an - ** error rather than a bounced insert. - */ - rc2 = term_select(v, pTerm, nTerm, iSegment, &iSegmentRow, &old); - if( rc2!=SQLITE_ROW ) goto err; - - rc = term_delete(v, iSegmentRow); - if( rc!=SQLITE_OK ) goto err; - - /* Reusing lowest-number deleted row keeps the index smaller. */ - if( iSegmentRownColumn ; ++i){ - char *zText = (char*)sqlite3_value_text(pValues[i]); - int rc = buildTerms(v, terms, iRowid, zText, i); - if( rc!=SQLITE_OK ) return rc; - } - return SQLITE_OK; -} - -/* Add empty doclists for all terms in the given row's content to the hash - * table [pTerms]. */ -static int deleteTerms(fulltext_vtab *v, fts1Hash *pTerms, sqlite_int64 iRowid){ - const char **pValues; - int i; - - int rc = content_select(v, iRowid, &pValues); - if( rc!=SQLITE_OK ) return rc; - - for(i = 0 ; i < v->nColumn; ++i) { - rc = buildTerms(v, pTerms, iRowid, pValues[i], -1); - if( rc!=SQLITE_OK ) break; - } - - freeStringArray(v->nColumn, pValues); - return SQLITE_OK; -} - -/* Insert a row into the %_content table; set *piRowid to be the ID of the - * new row. Fill [pTerms] with new doclists for the %_term table. */ -static int index_insert(fulltext_vtab *v, sqlite3_value *pRequestRowid, - sqlite3_value **pValues, - sqlite_int64 *piRowid, fts1Hash *pTerms){ - int rc; - - rc = content_insert(v, pRequestRowid, pValues); /* execute an SQL INSERT */ - if( rc!=SQLITE_OK ) return rc; - *piRowid = sqlite3_last_insert_rowid(v->db); - return insertTerms(v, pTerms, *piRowid, pValues); -} - -/* Delete a row from the %_content table; fill [pTerms] with empty doclists - * to be written to the %_term table. */ -static int index_delete(fulltext_vtab *v, sqlite_int64 iRow, fts1Hash *pTerms){ - int rc = deleteTerms(v, pTerms, iRow); - if( rc!=SQLITE_OK ) return rc; - return content_delete(v, iRow); /* execute an SQL DELETE */ -} - -/* Update a row in the %_content table; fill [pTerms] with new doclists for the - * %_term table. */ -static int index_update(fulltext_vtab *v, sqlite_int64 iRow, - sqlite3_value **pValues, fts1Hash *pTerms){ - /* Generate an empty doclist for each term that previously appeared in this - * row. */ - int rc = deleteTerms(v, pTerms, iRow); - if( rc!=SQLITE_OK ) return rc; - - rc = content_update(v, pValues, iRow); /* execute an SQL UPDATE */ - if( rc!=SQLITE_OK ) return rc; - - /* Now add positions for terms which appear in the updated row. */ - return insertTerms(v, pTerms, iRow, pValues); -} - -/* This function implements the xUpdate callback; it is the top-level entry - * point for inserting, deleting or updating a row in a full-text table. */ -static int fulltextUpdate(sqlite3_vtab *pVtab, int nArg, sqlite3_value **ppArg, - sqlite_int64 *pRowid){ - fulltext_vtab *v = (fulltext_vtab *) pVtab; - fts1Hash terms; /* maps term string -> PosList */ - int rc; - fts1HashElem *e; - - TRACE(("FTS1 Update %p\n", pVtab)); - - fts1HashInit(&terms, FTS1_HASH_STRING, 1); - - if( nArg<2 ){ - rc = index_delete(v, sqlite3_value_int64(ppArg[0]), &terms); - } else if( sqlite3_value_type(ppArg[0]) != SQLITE_NULL ){ - /* An update: - * ppArg[0] = old rowid - * ppArg[1] = new rowid - * ppArg[2..2+v->nColumn-1] = values - * ppArg[2+v->nColumn] = value for magic column (we ignore this) - */ - sqlite_int64 rowid = sqlite3_value_int64(ppArg[0]); - if( sqlite3_value_type(ppArg[1]) != SQLITE_INTEGER || - sqlite3_value_int64(ppArg[1]) != rowid ){ - rc = SQLITE_ERROR; /* we don't allow changing the rowid */ - } else { - assert( nArg==2+v->nColumn+1); - rc = index_update(v, rowid, &ppArg[2], &terms); - } - } else { - /* An insert: - * ppArg[1] = requested rowid - * ppArg[2..2+v->nColumn-1] = values - * ppArg[2+v->nColumn] = value for magic column (we ignore this) - */ - assert( nArg==2+v->nColumn+1); - rc = index_insert(v, ppArg[1], &ppArg[2], pRowid, &terms); - } - - if( rc==SQLITE_OK ){ - /* Write updated doclists to disk. */ - for(e=fts1HashFirst(&terms); e; e=fts1HashNext(e)){ - DocList *p = fts1HashData(e); - rc = index_insert_term(v, fts1HashKey(e), fts1HashKeysize(e), p); - if( rc!=SQLITE_OK ) break; - } - } - - /* clean up */ - for(e=fts1HashFirst(&terms); e; e=fts1HashNext(e)){ - DocList *p = fts1HashData(e); - docListDelete(p); - } - fts1HashClear(&terms); - - return rc; -} - -/* -** Implementation of the snippet() function for FTS1 -*/ -static void snippetFunc( - sqlite3_context *pContext, - int argc, - sqlite3_value **argv -){ - fulltext_cursor *pCursor; - if( argc<1 ) return; - if( sqlite3_value_type(argv[0])!=SQLITE_BLOB || - sqlite3_value_bytes(argv[0])!=sizeof(pCursor) ){ - sqlite3_result_error(pContext, "illegal first argument to html_snippet",-1); - }else{ - const char *zStart = ""; - const char *zEnd = ""; - const char *zEllipsis = "..."; - memcpy(&pCursor, sqlite3_value_blob(argv[0]), sizeof(pCursor)); - if( argc>=2 ){ - zStart = (const char*)sqlite3_value_text(argv[1]); - if( argc>=3 ){ - zEnd = (const char*)sqlite3_value_text(argv[2]); - if( argc>=4 ){ - zEllipsis = (const char*)sqlite3_value_text(argv[3]); - } - } - } - snippetAllOffsets(pCursor); - snippetText(pCursor, zStart, zEnd, zEllipsis); - sqlite3_result_text(pContext, pCursor->snippet.zSnippet, - pCursor->snippet.nSnippet, SQLITE_STATIC); - } -} - -/* -** Implementation of the offsets() function for FTS1 -*/ -static void snippetOffsetsFunc( - sqlite3_context *pContext, - int argc, - sqlite3_value **argv -){ - fulltext_cursor *pCursor; - if( argc<1 ) return; - if( sqlite3_value_type(argv[0])!=SQLITE_BLOB || - sqlite3_value_bytes(argv[0])!=sizeof(pCursor) ){ - sqlite3_result_error(pContext, "illegal first argument to offsets",-1); - }else{ - memcpy(&pCursor, sqlite3_value_blob(argv[0]), sizeof(pCursor)); - snippetAllOffsets(pCursor); - snippetOffsetText(&pCursor->snippet); - sqlite3_result_text(pContext, - pCursor->snippet.zOffset, pCursor->snippet.nOffset, - SQLITE_STATIC); - } -} - -/* -** This routine implements the xFindFunction method for the FTS1 -** virtual table. -*/ -static int fulltextFindFunction( - sqlite3_vtab *pVtab, - int nArg, - const char *zName, - void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), - void **ppArg -){ - if( strcmp(zName,"snippet")==0 ){ - *pxFunc = snippetFunc; - return 1; - }else if( strcmp(zName,"offsets")==0 ){ - *pxFunc = snippetOffsetsFunc; - return 1; - } - return 0; -} - -/* -** Rename an fts1 table. -*/ -static int fulltextRename( - sqlite3_vtab *pVtab, - const char *zName -){ - fulltext_vtab *p = (fulltext_vtab *)pVtab; - int rc = SQLITE_NOMEM; - char *zSql = sqlite3_mprintf( - "ALTER TABLE %Q.'%q_content' RENAME TO '%q_content';" - "ALTER TABLE %Q.'%q_term' RENAME TO '%q_term';" - , p->zDb, p->zName, zName - , p->zDb, p->zName, zName - ); - if( zSql ){ - rc = sqlite3_exec(p->db, zSql, 0, 0, 0); - sqlite3_free(zSql); - } - return rc; -} - -static const sqlite3_module fulltextModule = { - /* iVersion */ 0, - /* xCreate */ fulltextCreate, - /* xConnect */ fulltextConnect, - /* xBestIndex */ fulltextBestIndex, - /* xDisconnect */ fulltextDisconnect, - /* xDestroy */ fulltextDestroy, - /* xOpen */ fulltextOpen, - /* xClose */ fulltextClose, - /* xFilter */ fulltextFilter, - /* xNext */ fulltextNext, - /* xEof */ fulltextEof, - /* xColumn */ fulltextColumn, - /* xRowid */ fulltextRowid, - /* xUpdate */ fulltextUpdate, - /* xBegin */ 0, - /* xSync */ 0, - /* xCommit */ 0, - /* xRollback */ 0, - /* xFindFunction */ fulltextFindFunction, - /* xRename */ fulltextRename, -}; - -int sqlite3Fts1Init(sqlite3 *db){ - sqlite3_overload_function(db, "snippet", -1); - sqlite3_overload_function(db, "offsets", -1); - return sqlite3_create_module(db, "fts1", &fulltextModule, 0); -} - -#if !SQLITE_CORE -#ifdef _WIN32 -__declspec(dllexport) -#endif -int sqlite3_fts1_init(sqlite3 *db, char **pzErrMsg, - const sqlite3_api_routines *pApi){ - SQLITE_EXTENSION_INIT2(pApi) - return sqlite3Fts1Init(db); -} -#endif - -#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS1) */ diff --git a/ext/fts1/fts1.h b/ext/fts1/fts1.h deleted file mode 100644 index d55e689733..0000000000 --- a/ext/fts1/fts1.h +++ /dev/null @@ -1,11 +0,0 @@ -#include "sqlite3.h" - -#ifdef __cplusplus -extern "C" { -#endif /* __cplusplus */ - -int sqlite3Fts1Init(sqlite3 *db); - -#ifdef __cplusplus -} /* extern "C" */ -#endif /* __cplusplus */ diff --git a/ext/fts1/fts1_hash.c b/ext/fts1/fts1_hash.c deleted file mode 100644 index 463a52b645..0000000000 --- a/ext/fts1/fts1_hash.c +++ /dev/null @@ -1,369 +0,0 @@ -/* -** 2001 September 22 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -************************************************************************* -** This is the implementation of generic hash-tables used in SQLite. -** We've modified it slightly to serve as a standalone hash table -** implementation for the full-text indexing module. -*/ -#include -#include -#include - -/* -** The code in this file is only compiled if: -** -** * The FTS1 module is being built as an extension -** (in which case SQLITE_CORE is not defined), or -** -** * The FTS1 module is being built into the core of -** SQLite (in which case SQLITE_ENABLE_FTS1 is defined). -*/ -#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS1) - - -#include "fts1_hash.h" - -static void *malloc_and_zero(int n){ - void *p = malloc(n); - if( p ){ - memset(p, 0, n); - } - return p; -} - -/* Turn bulk memory into a hash table object by initializing the -** fields of the Hash structure. -** -** "pNew" is a pointer to the hash table that is to be initialized. -** keyClass is one of the constants -** FTS1_HASH_BINARY or FTS1_HASH_STRING. The value of keyClass -** determines what kind of key the hash table will use. "copyKey" is -** true if the hash table should make its own private copy of keys and -** false if it should just use the supplied pointer. -*/ -void sqlite3Fts1HashInit(fts1Hash *pNew, int keyClass, int copyKey){ - assert( pNew!=0 ); - assert( keyClass>=FTS1_HASH_STRING && keyClass<=FTS1_HASH_BINARY ); - pNew->keyClass = keyClass; - pNew->copyKey = copyKey; - pNew->first = 0; - pNew->count = 0; - pNew->htsize = 0; - pNew->ht = 0; - pNew->xMalloc = malloc_and_zero; - pNew->xFree = free; -} - -/* Remove all entries from a hash table. Reclaim all memory. -** Call this routine to delete a hash table or to reset a hash table -** to the empty state. -*/ -void sqlite3Fts1HashClear(fts1Hash *pH){ - fts1HashElem *elem; /* For looping over all elements of the table */ - - assert( pH!=0 ); - elem = pH->first; - pH->first = 0; - if( pH->ht ) pH->xFree(pH->ht); - pH->ht = 0; - pH->htsize = 0; - while( elem ){ - fts1HashElem *next_elem = elem->next; - if( pH->copyKey && elem->pKey ){ - pH->xFree(elem->pKey); - } - pH->xFree(elem); - elem = next_elem; - } - pH->count = 0; -} - -/* -** Hash and comparison functions when the mode is FTS1_HASH_STRING -*/ -static int strHash(const void *pKey, int nKey){ - const char *z = (const char *)pKey; - int h = 0; - if( nKey<=0 ) nKey = (int) strlen(z); - while( nKey > 0 ){ - h = (h<<3) ^ h ^ *z++; - nKey--; - } - return h & 0x7fffffff; -} -static int strCompare(const void *pKey1, int n1, const void *pKey2, int n2){ - if( n1!=n2 ) return 1; - return strncmp((const char*)pKey1,(const char*)pKey2,n1); -} - -/* -** Hash and comparison functions when the mode is FTS1_HASH_BINARY -*/ -static int binHash(const void *pKey, int nKey){ - int h = 0; - const char *z = (const char *)pKey; - while( nKey-- > 0 ){ - h = (h<<3) ^ h ^ *(z++); - } - return h & 0x7fffffff; -} -static int binCompare(const void *pKey1, int n1, const void *pKey2, int n2){ - if( n1!=n2 ) return 1; - return memcmp(pKey1,pKey2,n1); -} - -/* -** Return a pointer to the appropriate hash function given the key class. -** -** The C syntax in this function definition may be unfamilar to some -** programmers, so we provide the following additional explanation: -** -** The name of the function is "hashFunction". The function takes a -** single parameter "keyClass". The return value of hashFunction() -** is a pointer to another function. Specifically, the return value -** of hashFunction() is a pointer to a function that takes two parameters -** with types "const void*" and "int" and returns an "int". -*/ -static int (*hashFunction(int keyClass))(const void*,int){ - if( keyClass==FTS1_HASH_STRING ){ - return &strHash; - }else{ - assert( keyClass==FTS1_HASH_BINARY ); - return &binHash; - } -} - -/* -** Return a pointer to the appropriate hash function given the key class. -** -** For help in interpreted the obscure C code in the function definition, -** see the header comment on the previous function. -*/ -static int (*compareFunction(int keyClass))(const void*,int,const void*,int){ - if( keyClass==FTS1_HASH_STRING ){ - return &strCompare; - }else{ - assert( keyClass==FTS1_HASH_BINARY ); - return &binCompare; - } -} - -/* Link an element into the hash table -*/ -static void insertElement( - fts1Hash *pH, /* The complete hash table */ - struct _fts1ht *pEntry, /* The entry into which pNew is inserted */ - fts1HashElem *pNew /* The element to be inserted */ -){ - fts1HashElem *pHead; /* First element already in pEntry */ - pHead = pEntry->chain; - if( pHead ){ - pNew->next = pHead; - pNew->prev = pHead->prev; - if( pHead->prev ){ pHead->prev->next = pNew; } - else { pH->first = pNew; } - pHead->prev = pNew; - }else{ - pNew->next = pH->first; - if( pH->first ){ pH->first->prev = pNew; } - pNew->prev = 0; - pH->first = pNew; - } - pEntry->count++; - pEntry->chain = pNew; -} - - -/* Resize the hash table so that it cantains "new_size" buckets. -** "new_size" must be a power of 2. The hash table might fail -** to resize if sqliteMalloc() fails. -*/ -static void rehash(fts1Hash *pH, int new_size){ - struct _fts1ht *new_ht; /* The new hash table */ - fts1HashElem *elem, *next_elem; /* For looping over existing elements */ - int (*xHash)(const void*,int); /* The hash function */ - - assert( (new_size & (new_size-1))==0 ); - new_ht = (struct _fts1ht *)pH->xMalloc( new_size*sizeof(struct _fts1ht) ); - if( new_ht==0 ) return; - if( pH->ht ) pH->xFree(pH->ht); - pH->ht = new_ht; - pH->htsize = new_size; - xHash = hashFunction(pH->keyClass); - for(elem=pH->first, pH->first=0; elem; elem = next_elem){ - int h = (*xHash)(elem->pKey, elem->nKey) & (new_size-1); - next_elem = elem->next; - insertElement(pH, &new_ht[h], elem); - } -} - -/* This function (for internal use only) locates an element in an -** hash table that matches the given key. The hash for this key has -** already been computed and is passed as the 4th parameter. -*/ -static fts1HashElem *findElementGivenHash( - const fts1Hash *pH, /* The pH to be searched */ - const void *pKey, /* The key we are searching for */ - int nKey, - int h /* The hash for this key. */ -){ - fts1HashElem *elem; /* Used to loop thru the element list */ - int count; /* Number of elements left to test */ - int (*xCompare)(const void*,int,const void*,int); /* comparison function */ - - if( pH->ht ){ - struct _fts1ht *pEntry = &pH->ht[h]; - elem = pEntry->chain; - count = pEntry->count; - xCompare = compareFunction(pH->keyClass); - while( count-- && elem ){ - if( (*xCompare)(elem->pKey,elem->nKey,pKey,nKey)==0 ){ - return elem; - } - elem = elem->next; - } - } - return 0; -} - -/* Remove a single entry from the hash table given a pointer to that -** element and a hash on the element's key. -*/ -static void removeElementGivenHash( - fts1Hash *pH, /* The pH containing "elem" */ - fts1HashElem* elem, /* The element to be removed from the pH */ - int h /* Hash value for the element */ -){ - struct _fts1ht *pEntry; - if( elem->prev ){ - elem->prev->next = elem->next; - }else{ - pH->first = elem->next; - } - if( elem->next ){ - elem->next->prev = elem->prev; - } - pEntry = &pH->ht[h]; - if( pEntry->chain==elem ){ - pEntry->chain = elem->next; - } - pEntry->count--; - if( pEntry->count<=0 ){ - pEntry->chain = 0; - } - if( pH->copyKey && elem->pKey ){ - pH->xFree(elem->pKey); - } - pH->xFree( elem ); - pH->count--; - if( pH->count<=0 ){ - assert( pH->first==0 ); - assert( pH->count==0 ); - fts1HashClear(pH); - } -} - -/* Attempt to locate an element of the hash table pH with a key -** that matches pKey,nKey. Return the data for this element if it is -** found, or NULL if there is no match. -*/ -void *sqlite3Fts1HashFind(const fts1Hash *pH, const void *pKey, int nKey){ - int h; /* A hash on key */ - fts1HashElem *elem; /* The element that matches key */ - int (*xHash)(const void*,int); /* The hash function */ - - if( pH==0 || pH->ht==0 ) return 0; - xHash = hashFunction(pH->keyClass); - assert( xHash!=0 ); - h = (*xHash)(pKey,nKey); - assert( (pH->htsize & (pH->htsize-1))==0 ); - elem = findElementGivenHash(pH,pKey,nKey, h & (pH->htsize-1)); - return elem ? elem->data : 0; -} - -/* Insert an element into the hash table pH. The key is pKey,nKey -** and the data is "data". -** -** If no element exists with a matching key, then a new -** element is created. A copy of the key is made if the copyKey -** flag is set. NULL is returned. -** -** If another element already exists with the same key, then the -** new data replaces the old data and the old data is returned. -** The key is not copied in this instance. If a malloc fails, then -** the new data is returned and the hash table is unchanged. -** -** If the "data" parameter to this function is NULL, then the -** element corresponding to "key" is removed from the hash table. -*/ -void *sqlite3Fts1HashInsert( - fts1Hash *pH, /* The hash table to insert into */ - const void *pKey, /* The key */ - int nKey, /* Number of bytes in the key */ - void *data /* The data */ -){ - int hraw; /* Raw hash value of the key */ - int h; /* the hash of the key modulo hash table size */ - fts1HashElem *elem; /* Used to loop thru the element list */ - fts1HashElem *new_elem; /* New element added to the pH */ - int (*xHash)(const void*,int); /* The hash function */ - - assert( pH!=0 ); - xHash = hashFunction(pH->keyClass); - assert( xHash!=0 ); - hraw = (*xHash)(pKey, nKey); - assert( (pH->htsize & (pH->htsize-1))==0 ); - h = hraw & (pH->htsize-1); - elem = findElementGivenHash(pH,pKey,nKey,h); - if( elem ){ - void *old_data = elem->data; - if( data==0 ){ - removeElementGivenHash(pH,elem,h); - }else{ - elem->data = data; - } - return old_data; - } - if( data==0 ) return 0; - new_elem = (fts1HashElem*)pH->xMalloc( sizeof(fts1HashElem) ); - if( new_elem==0 ) return data; - if( pH->copyKey && pKey!=0 ){ - new_elem->pKey = pH->xMalloc( nKey ); - if( new_elem->pKey==0 ){ - pH->xFree(new_elem); - return data; - } - memcpy((void*)new_elem->pKey, pKey, nKey); - }else{ - new_elem->pKey = (void*)pKey; - } - new_elem->nKey = nKey; - pH->count++; - if( pH->htsize==0 ){ - rehash(pH,8); - if( pH->htsize==0 ){ - pH->count = 0; - pH->xFree(new_elem); - return data; - } - } - if( pH->count > pH->htsize ){ - rehash(pH,pH->htsize*2); - } - assert( pH->htsize>0 ); - assert( (pH->htsize & (pH->htsize-1))==0 ); - h = hraw & (pH->htsize-1); - insertElement(pH, &pH->ht[h], new_elem); - new_elem->data = data; - return 0; -} - -#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS1) */ diff --git a/ext/fts1/fts1_hash.h b/ext/fts1/fts1_hash.h deleted file mode 100644 index 9001152931..0000000000 --- a/ext/fts1/fts1_hash.h +++ /dev/null @@ -1,112 +0,0 @@ -/* -** 2001 September 22 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -************************************************************************* -** This is the header file for the generic hash-table implementation -** used in SQLite. We've modified it slightly to serve as a standalone -** hash table implementation for the full-text indexing module. -** -*/ -#ifndef _FTS1_HASH_H_ -#define _FTS1_HASH_H_ - -/* Forward declarations of structures. */ -typedef struct fts1Hash fts1Hash; -typedef struct fts1HashElem fts1HashElem; - -/* A complete hash table is an instance of the following structure. -** The internals of this structure are intended to be opaque -- client -** code should not attempt to access or modify the fields of this structure -** directly. Change this structure only by using the routines below. -** However, many of the "procedures" and "functions" for modifying and -** accessing this structure are really macros, so we can't really make -** this structure opaque. -*/ -struct fts1Hash { - char keyClass; /* HASH_INT, _POINTER, _STRING, _BINARY */ - char copyKey; /* True if copy of key made on insert */ - int count; /* Number of entries in this table */ - fts1HashElem *first; /* The first element of the array */ - void *(*xMalloc)(int); /* malloc() function to use */ - void (*xFree)(void *); /* free() function to use */ - int htsize; /* Number of buckets in the hash table */ - struct _fts1ht { /* the hash table */ - int count; /* Number of entries with this hash */ - fts1HashElem *chain; /* Pointer to first entry with this hash */ - } *ht; -}; - -/* Each element in the hash table is an instance of the following -** structure. All elements are stored on a single doubly-linked list. -** -** Again, this structure is intended to be opaque, but it can't really -** be opaque because it is used by macros. -*/ -struct fts1HashElem { - fts1HashElem *next, *prev; /* Next and previous elements in the table */ - void *data; /* Data associated with this element */ - void *pKey; int nKey; /* Key associated with this element */ -}; - -/* -** There are 2 different modes of operation for a hash table: -** -** FTS1_HASH_STRING pKey points to a string that is nKey bytes long -** (including the null-terminator, if any). Case -** is respected in comparisons. -** -** FTS1_HASH_BINARY pKey points to binary data nKey bytes long. -** memcmp() is used to compare keys. -** -** A copy of the key is made if the copyKey parameter to fts1HashInit is 1. -*/ -#define FTS1_HASH_STRING 1 -#define FTS1_HASH_BINARY 2 - -/* -** Access routines. To delete, insert a NULL pointer. -*/ -void sqlite3Fts1HashInit(fts1Hash*, int keytype, int copyKey); -void *sqlite3Fts1HashInsert(fts1Hash*, const void *pKey, int nKey, void *pData); -void *sqlite3Fts1HashFind(const fts1Hash*, const void *pKey, int nKey); -void sqlite3Fts1HashClear(fts1Hash*); - -/* -** Shorthand for the functions above -*/ -#define fts1HashInit sqlite3Fts1HashInit -#define fts1HashInsert sqlite3Fts1HashInsert -#define fts1HashFind sqlite3Fts1HashFind -#define fts1HashClear sqlite3Fts1HashClear - -/* -** Macros for looping over all elements of a hash table. The idiom is -** like this: -** -** fts1Hash h; -** fts1HashElem *p; -** ... -** for(p=fts1HashFirst(&h); p; p=fts1HashNext(p)){ -** SomeStructure *pData = fts1HashData(p); -** // do something with pData -** } -*/ -#define fts1HashFirst(H) ((H)->first) -#define fts1HashNext(E) ((E)->next) -#define fts1HashData(E) ((E)->data) -#define fts1HashKey(E) ((E)->pKey) -#define fts1HashKeysize(E) ((E)->nKey) - -/* -** Number of entries in a hash table -*/ -#define fts1HashCount(H) ((H)->count) - -#endif /* _FTS1_HASH_H_ */ diff --git a/ext/fts1/fts1_porter.c b/ext/fts1/fts1_porter.c deleted file mode 100644 index 1d26236681..0000000000 --- a/ext/fts1/fts1_porter.c +++ /dev/null @@ -1,643 +0,0 @@ -/* -** 2006 September 30 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -************************************************************************* -** Implementation of the full-text-search tokenizer that implements -** a Porter stemmer. -*/ - -/* -** The code in this file is only compiled if: -** -** * The FTS1 module is being built as an extension -** (in which case SQLITE_CORE is not defined), or -** -** * The FTS1 module is being built into the core of -** SQLite (in which case SQLITE_ENABLE_FTS1 is defined). -*/ -#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS1) - - -#include -#include -#include -#include -#include - -#include "fts1_tokenizer.h" - -/* -** Class derived from sqlite3_tokenizer -*/ -typedef struct porter_tokenizer { - sqlite3_tokenizer base; /* Base class */ -} porter_tokenizer; - -/* -** Class derived from sqlit3_tokenizer_cursor -*/ -typedef struct porter_tokenizer_cursor { - sqlite3_tokenizer_cursor base; - const char *zInput; /* input we are tokenizing */ - int nInput; /* size of the input */ - int iOffset; /* current position in zInput */ - int iToken; /* index of next token to be returned */ - char *zToken; /* storage for current token */ - int nAllocated; /* space allocated to zToken buffer */ -} porter_tokenizer_cursor; - - -/* Forward declaration */ -static const sqlite3_tokenizer_module porterTokenizerModule; - - -/* -** Create a new tokenizer instance. -*/ -static int porterCreate( - int argc, const char * const *argv, - sqlite3_tokenizer **ppTokenizer -){ - porter_tokenizer *t; - t = (porter_tokenizer *) calloc(sizeof(*t), 1); - if( t==NULL ) return SQLITE_NOMEM; - - *ppTokenizer = &t->base; - return SQLITE_OK; -} - -/* -** Destroy a tokenizer -*/ -static int porterDestroy(sqlite3_tokenizer *pTokenizer){ - free(pTokenizer); - return SQLITE_OK; -} - -/* -** Prepare to begin tokenizing a particular string. The input -** string to be tokenized is zInput[0..nInput-1]. A cursor -** used to incrementally tokenize this string is returned in -** *ppCursor. -*/ -static int porterOpen( - sqlite3_tokenizer *pTokenizer, /* The tokenizer */ - const char *zInput, int nInput, /* String to be tokenized */ - sqlite3_tokenizer_cursor **ppCursor /* OUT: Tokenization cursor */ -){ - porter_tokenizer_cursor *c; - - c = (porter_tokenizer_cursor *) malloc(sizeof(*c)); - if( c==NULL ) return SQLITE_NOMEM; - - c->zInput = zInput; - if( zInput==0 ){ - c->nInput = 0; - }else if( nInput<0 ){ - c->nInput = (int)strlen(zInput); - }else{ - c->nInput = nInput; - } - c->iOffset = 0; /* start tokenizing at the beginning */ - c->iToken = 0; - c->zToken = NULL; /* no space allocated, yet. */ - c->nAllocated = 0; - - *ppCursor = &c->base; - return SQLITE_OK; -} - -/* -** Close a tokenization cursor previously opened by a call to -** porterOpen() above. -*/ -static int porterClose(sqlite3_tokenizer_cursor *pCursor){ - porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor; - free(c->zToken); - free(c); - return SQLITE_OK; -} -/* -** Vowel or consonant -*/ -static const char cType[] = { - 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, - 1, 1, 1, 2, 1 -}; - -/* -** isConsonant() and isVowel() determine if their first character in -** the string they point to is a consonant or a vowel, according -** to Porter ruls. -** -** A consonate is any letter other than 'a', 'e', 'i', 'o', or 'u'. -** 'Y' is a consonant unless it follows another consonant, -** in which case it is a vowel. -** -** In these routine, the letters are in reverse order. So the 'y' rule -** is that 'y' is a consonant unless it is followed by another -** consonent. -*/ -static int isVowel(const char*); -static int isConsonant(const char *z){ - int j; - char x = *z; - if( x==0 ) return 0; - assert( x>='a' && x<='z' ); - j = cType[x-'a']; - if( j<2 ) return j; - return z[1]==0 || isVowel(z + 1); -} -static int isVowel(const char *z){ - int j; - char x = *z; - if( x==0 ) return 0; - assert( x>='a' && x<='z' ); - j = cType[x-'a']; - if( j<2 ) return 1-j; - return isConsonant(z + 1); -} - -/* -** Let any sequence of one or more vowels be represented by V and let -** C be sequence of one or more consonants. Then every word can be -** represented as: -** -** [C] (VC){m} [V] -** -** In prose: A word is an optional consonant followed by zero or -** vowel-consonant pairs followed by an optional vowel. "m" is the -** number of vowel consonant pairs. This routine computes the value -** of m for the first i bytes of a word. -** -** Return true if the m-value for z is 1 or more. In other words, -** return true if z contains at least one vowel that is followed -** by a consonant. -** -** In this routine z[] is in reverse order. So we are really looking -** for an instance of of a consonant followed by a vowel. -*/ -static int m_gt_0(const char *z){ - while( isVowel(z) ){ z++; } - if( *z==0 ) return 0; - while( isConsonant(z) ){ z++; } - return *z!=0; -} - -/* Like mgt0 above except we are looking for a value of m which is -** exactly 1 -*/ -static int m_eq_1(const char *z){ - while( isVowel(z) ){ z++; } - if( *z==0 ) return 0; - while( isConsonant(z) ){ z++; } - if( *z==0 ) return 0; - while( isVowel(z) ){ z++; } - if( *z==0 ) return 1; - while( isConsonant(z) ){ z++; } - return *z==0; -} - -/* Like mgt0 above except we are looking for a value of m>1 instead -** or m>0 -*/ -static int m_gt_1(const char *z){ - while( isVowel(z) ){ z++; } - if( *z==0 ) return 0; - while( isConsonant(z) ){ z++; } - if( *z==0 ) return 0; - while( isVowel(z) ){ z++; } - if( *z==0 ) return 0; - while( isConsonant(z) ){ z++; } - return *z!=0; -} - -/* -** Return TRUE if there is a vowel anywhere within z[0..n-1] -*/ -static int hasVowel(const char *z){ - while( isConsonant(z) ){ z++; } - return *z!=0; -} - -/* -** Return TRUE if the word ends in a double consonant. -** -** The text is reversed here. So we are really looking at -** the first two characters of z[]. -*/ -static int doubleConsonant(const char *z){ - return isConsonant(z) && z[0]==z[1] && isConsonant(z+1); -} - -/* -** Return TRUE if the word ends with three letters which -** are consonant-vowel-consonent and where the final consonant -** is not 'w', 'x', or 'y'. -** -** The word is reversed here. So we are really checking the -** first three letters and the first one cannot be in [wxy]. -*/ -static int star_oh(const char *z){ - return - z[0]!=0 && isConsonant(z) && - z[0]!='w' && z[0]!='x' && z[0]!='y' && - z[1]!=0 && isVowel(z+1) && - z[2]!=0 && isConsonant(z+2); -} - -/* -** If the word ends with zFrom and xCond() is true for the stem -** of the word that preceeds the zFrom ending, then change the -** ending to zTo. -** -** The input word *pz and zFrom are both in reverse order. zTo -** is in normal order. -** -** Return TRUE if zFrom matches. Return FALSE if zFrom does not -** match. Not that TRUE is returned even if xCond() fails and -** no substitution occurs. -*/ -static int stem( - char **pz, /* The word being stemmed (Reversed) */ - const char *zFrom, /* If the ending matches this... (Reversed) */ - const char *zTo, /* ... change the ending to this (not reversed) */ - int (*xCond)(const char*) /* Condition that must be true */ -){ - char *z = *pz; - while( *zFrom && *zFrom==*z ){ z++; zFrom++; } - if( *zFrom!=0 ) return 0; - if( xCond && !xCond(z) ) return 1; - while( *zTo ){ - *(--z) = *(zTo++); - } - *pz = z; - return 1; -} - -/* -** This is the fallback stemmer used when the porter stemmer is -** inappropriate. The input word is copied into the output with -** US-ASCII case folding. If the input word is too long (more -** than 20 bytes if it contains no digits or more than 6 bytes if -** it contains digits) then word is truncated to 20 or 6 bytes -** by taking 10 or 3 bytes from the beginning and end. -*/ -static void copy_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){ - int i, mx, j; - int hasDigit = 0; - for(i=0; i='A' && c<='Z' ){ - zOut[i] = c - 'A' + 'a'; - }else{ - if( c>='0' && c<='9' ) hasDigit = 1; - zOut[i] = c; - } - } - mx = hasDigit ? 3 : 10; - if( nIn>mx*2 ){ - for(j=mx, i=nIn-mx; i=sizeof(zReverse)-7 ){ - /* The word is too big or too small for the porter stemmer. - ** Fallback to the copy stemmer */ - copy_stemmer(zIn, nIn, zOut, pnOut); - return; - } - for(i=0, j=sizeof(zReverse)-6; i='A' && c<='Z' ){ - zReverse[j] = c + 'a' - 'A'; - }else if( c>='a' && c<='z' ){ - zReverse[j] = c; - }else{ - /* The use of a character not in [a-zA-Z] means that we fallback - ** to the copy stemmer */ - copy_stemmer(zIn, nIn, zOut, pnOut); - return; - } - } - memset(&zReverse[sizeof(zReverse)-5], 0, 5); - z = &zReverse[j+1]; - - - /* Step 1a */ - if( z[0]=='s' ){ - if( - !stem(&z, "sess", "ss", 0) && - !stem(&z, "sei", "i", 0) && - !stem(&z, "ss", "ss", 0) - ){ - z++; - } - } - - /* Step 1b */ - z2 = z; - if( stem(&z, "dee", "ee", m_gt_0) ){ - /* Do nothing. The work was all in the test */ - }else if( - (stem(&z, "gni", "", hasVowel) || stem(&z, "de", "", hasVowel)) - && z!=z2 - ){ - if( stem(&z, "ta", "ate", 0) || - stem(&z, "lb", "ble", 0) || - stem(&z, "zi", "ize", 0) ){ - /* Do nothing. The work was all in the test */ - }else if( doubleConsonant(z) && (*z!='l' && *z!='s' && *z!='z') ){ - z++; - }else if( m_eq_1(z) && star_oh(z) ){ - *(--z) = 'e'; - } - } - - /* Step 1c */ - if( z[0]=='y' && hasVowel(z+1) ){ - z[0] = 'i'; - } - - /* Step 2 */ - switch( z[1] ){ - case 'a': - stem(&z, "lanoita", "ate", m_gt_0) || - stem(&z, "lanoit", "tion", m_gt_0); - break; - case 'c': - stem(&z, "icne", "ence", m_gt_0) || - stem(&z, "icna", "ance", m_gt_0); - break; - case 'e': - stem(&z, "rezi", "ize", m_gt_0); - break; - case 'g': - stem(&z, "igol", "log", m_gt_0); - break; - case 'l': - stem(&z, "ilb", "ble", m_gt_0) || - stem(&z, "illa", "al", m_gt_0) || - stem(&z, "iltne", "ent", m_gt_0) || - stem(&z, "ile", "e", m_gt_0) || - stem(&z, "ilsuo", "ous", m_gt_0); - break; - case 'o': - stem(&z, "noitazi", "ize", m_gt_0) || - stem(&z, "noita", "ate", m_gt_0) || - stem(&z, "rota", "ate", m_gt_0); - break; - case 's': - stem(&z, "msila", "al", m_gt_0) || - stem(&z, "ssenevi", "ive", m_gt_0) || - stem(&z, "ssenluf", "ful", m_gt_0) || - stem(&z, "ssensuo", "ous", m_gt_0); - break; - case 't': - stem(&z, "itila", "al", m_gt_0) || - stem(&z, "itivi", "ive", m_gt_0) || - stem(&z, "itilib", "ble", m_gt_0); - break; - } - - /* Step 3 */ - switch( z[0] ){ - case 'e': - stem(&z, "etaci", "ic", m_gt_0) || - stem(&z, "evita", "", m_gt_0) || - stem(&z, "ezila", "al", m_gt_0); - break; - case 'i': - stem(&z, "itici", "ic", m_gt_0); - break; - case 'l': - stem(&z, "laci", "ic", m_gt_0) || - stem(&z, "luf", "", m_gt_0); - break; - case 's': - stem(&z, "ssen", "", m_gt_0); - break; - } - - /* Step 4 */ - switch( z[1] ){ - case 'a': - if( z[0]=='l' && m_gt_1(z+2) ){ - z += 2; - } - break; - case 'c': - if( z[0]=='e' && z[2]=='n' && (z[3]=='a' || z[3]=='e') && m_gt_1(z+4) ){ - z += 4; - } - break; - case 'e': - if( z[0]=='r' && m_gt_1(z+2) ){ - z += 2; - } - break; - case 'i': - if( z[0]=='c' && m_gt_1(z+2) ){ - z += 2; - } - break; - case 'l': - if( z[0]=='e' && z[2]=='b' && (z[3]=='a' || z[3]=='i') && m_gt_1(z+4) ){ - z += 4; - } - break; - case 'n': - if( z[0]=='t' ){ - if( z[2]=='a' ){ - if( m_gt_1(z+3) ){ - z += 3; - } - }else if( z[2]=='e' ){ - stem(&z, "tneme", "", m_gt_1) || - stem(&z, "tnem", "", m_gt_1) || - stem(&z, "tne", "", m_gt_1); - } - } - break; - case 'o': - if( z[0]=='u' ){ - if( m_gt_1(z+2) ){ - z += 2; - } - }else if( z[3]=='s' || z[3]=='t' ){ - stem(&z, "noi", "", m_gt_1); - } - break; - case 's': - if( z[0]=='m' && z[2]=='i' && m_gt_1(z+3) ){ - z += 3; - } - break; - case 't': - stem(&z, "eta", "", m_gt_1) || - stem(&z, "iti", "", m_gt_1); - break; - case 'u': - if( z[0]=='s' && z[2]=='o' && m_gt_1(z+3) ){ - z += 3; - } - break; - case 'v': - case 'z': - if( z[0]=='e' && z[2]=='i' && m_gt_1(z+3) ){ - z += 3; - } - break; - } - - /* Step 5a */ - if( z[0]=='e' ){ - if( m_gt_1(z+1) ){ - z++; - }else if( m_eq_1(z+1) && !star_oh(z+1) ){ - z++; - } - } - - /* Step 5b */ - if( m_gt_1(z) && z[0]=='l' && z[1]=='l' ){ - z++; - } - - /* z[] is now the stemmed word in reverse order. Flip it back - ** around into forward order and return. - */ - *pnOut = i = strlen(z); - zOut[i] = 0; - while( *z ){ - zOut[--i] = *(z++); - } -} - -/* -** Characters that can be part of a token. We assume any character -** whose value is greater than 0x80 (any UTF character) can be -** part of a token. In other words, delimiters all must have -** values of 0x7f or lower. -*/ -static const char isIdChar[] = { -/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */ - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */ - 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */ - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */ - 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */ - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */ -}; -#define idChar(C) (((ch=C)&0x80)!=0 || (ch>0x2f && isIdChar[ch-0x30])) -#define isDelim(C) (((ch=C)&0x80)==0 && (ch<0x30 || !isIdChar[ch-0x30])) - -/* -** Extract the next token from a tokenization cursor. The cursor must -** have been opened by a prior call to porterOpen(). -*/ -static int porterNext( - sqlite3_tokenizer_cursor *pCursor, /* Cursor returned by porterOpen */ - const char **pzToken, /* OUT: *pzToken is the token text */ - int *pnBytes, /* OUT: Number of bytes in token */ - int *piStartOffset, /* OUT: Starting offset of token */ - int *piEndOffset, /* OUT: Ending offset of token */ - int *piPosition /* OUT: Position integer of token */ -){ - porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor; - const char *z = c->zInput; - - while( c->iOffsetnInput ){ - int iStartOffset, ch; - - /* Scan past delimiter characters */ - while( c->iOffsetnInput && isDelim(z[c->iOffset]) ){ - c->iOffset++; - } - - /* Count non-delimiter characters. */ - iStartOffset = c->iOffset; - while( c->iOffsetnInput && !isDelim(z[c->iOffset]) ){ - c->iOffset++; - } - - if( c->iOffset>iStartOffset ){ - int n = c->iOffset-iStartOffset; - if( n>c->nAllocated ){ - c->nAllocated = n+20; - c->zToken = realloc(c->zToken, c->nAllocated); - if( c->zToken==NULL ) return SQLITE_NOMEM; - } - porter_stemmer(&z[iStartOffset], n, c->zToken, pnBytes); - *pzToken = c->zToken; - *piStartOffset = iStartOffset; - *piEndOffset = c->iOffset; - *piPosition = c->iToken++; - return SQLITE_OK; - } - } - return SQLITE_DONE; -} - -/* -** The set of routines that implement the porter-stemmer tokenizer -*/ -static const sqlite3_tokenizer_module porterTokenizerModule = { - 0, - porterCreate, - porterDestroy, - porterOpen, - porterClose, - porterNext, -}; - -/* -** Allocate a new porter tokenizer. Return a pointer to the new -** tokenizer in *ppModule -*/ -void sqlite3Fts1PorterTokenizerModule( - sqlite3_tokenizer_module const**ppModule -){ - *ppModule = &porterTokenizerModule; -} - -#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS1) */ diff --git a/ext/fts1/fts1_tokenizer.h b/ext/fts1/fts1_tokenizer.h deleted file mode 100644 index a48cb74519..0000000000 --- a/ext/fts1/fts1_tokenizer.h +++ /dev/null @@ -1,90 +0,0 @@ -/* -** 2006 July 10 -** -** The author disclaims copyright to this source code. -** -************************************************************************* -** Defines the interface to tokenizers used by fulltext-search. There -** are three basic components: -** -** sqlite3_tokenizer_module is a singleton defining the tokenizer -** interface functions. This is essentially the class structure for -** tokenizers. -** -** sqlite3_tokenizer is used to define a particular tokenizer, perhaps -** including customization information defined at creation time. -** -** sqlite3_tokenizer_cursor is generated by a tokenizer to generate -** tokens from a particular input. -*/ -#ifndef _FTS1_TOKENIZER_H_ -#define _FTS1_TOKENIZER_H_ - -/* TODO(shess) Only used for SQLITE_OK and SQLITE_DONE at this time. -** If tokenizers are to be allowed to call sqlite3_*() functions, then -** we will need a way to register the API consistently. -*/ -#include "sqlite3.h" - -/* -** Structures used by the tokenizer interface. -*/ -typedef struct sqlite3_tokenizer sqlite3_tokenizer; -typedef struct sqlite3_tokenizer_cursor sqlite3_tokenizer_cursor; -typedef struct sqlite3_tokenizer_module sqlite3_tokenizer_module; - -struct sqlite3_tokenizer_module { - int iVersion; /* currently 0 */ - - /* - ** Create and destroy a tokenizer. argc/argv are passed down from - ** the fulltext virtual table creation to allow customization. - */ - int (*xCreate)(int argc, const char *const*argv, - sqlite3_tokenizer **ppTokenizer); - int (*xDestroy)(sqlite3_tokenizer *pTokenizer); - - /* - ** Tokenize a particular input. Call xOpen() to prepare to - ** tokenize, xNext() repeatedly until it returns SQLITE_DONE, then - ** xClose() to free any internal state. The pInput passed to - ** xOpen() must exist until the cursor is closed. The ppToken - ** result from xNext() is only valid until the next call to xNext() - ** or until xClose() is called. - */ - /* TODO(shess) current implementation requires pInput to be - ** nul-terminated. This should either be fixed, or pInput/nBytes - ** should be converted to zInput. - */ - int (*xOpen)(sqlite3_tokenizer *pTokenizer, - const char *pInput, int nBytes, - sqlite3_tokenizer_cursor **ppCursor); - int (*xClose)(sqlite3_tokenizer_cursor *pCursor); - int (*xNext)(sqlite3_tokenizer_cursor *pCursor, - const char **ppToken, int *pnBytes, - int *piStartOffset, int *piEndOffset, int *piPosition); -}; - -struct sqlite3_tokenizer { - const sqlite3_tokenizer_module *pModule; /* The module for this tokenizer */ - /* Tokenizer implementations will typically add additional fields */ -}; - -struct sqlite3_tokenizer_cursor { - sqlite3_tokenizer *pTokenizer; /* Tokenizer for this cursor. */ - /* Tokenizer implementations will typically add additional fields */ -}; - -/* -** Get the module for a tokenizer which generates tokens based on a -** set of non-token characters. The default is to break tokens at any -** non-alnum character, though the set of delimiters can also be -** specified by the first argv argument to xCreate(). -*/ -/* TODO(shess) This doesn't belong here. Need some sort of -** registration process. -*/ -void sqlite3Fts1SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule); -void sqlite3Fts1PorterTokenizerModule(sqlite3_tokenizer_module const**ppModule); - -#endif /* _FTS1_TOKENIZER_H_ */ diff --git a/ext/fts1/fts1_tokenizer1.c b/ext/fts1/fts1_tokenizer1.c deleted file mode 100644 index f58fba8f8e..0000000000 --- a/ext/fts1/fts1_tokenizer1.c +++ /dev/null @@ -1,221 +0,0 @@ -/* -** The author disclaims copyright to this source code. -** -************************************************************************* -** Implementation of the "simple" full-text-search tokenizer. -*/ - -/* -** The code in this file is only compiled if: -** -** * The FTS1 module is being built as an extension -** (in which case SQLITE_CORE is not defined), or -** -** * The FTS1 module is being built into the core of -** SQLite (in which case SQLITE_ENABLE_FTS1 is defined). -*/ -#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS1) - - -#include -#include -#include -#include -#include - -#include "fts1_tokenizer.h" - -typedef struct simple_tokenizer { - sqlite3_tokenizer base; - char delim[128]; /* flag ASCII delimiters */ -} simple_tokenizer; - -typedef struct simple_tokenizer_cursor { - sqlite3_tokenizer_cursor base; - const char *pInput; /* input we are tokenizing */ - int nBytes; /* size of the input */ - int iOffset; /* current position in pInput */ - int iToken; /* index of next token to be returned */ - char *pToken; /* storage for current token */ - int nTokenAllocated; /* space allocated to zToken buffer */ -} simple_tokenizer_cursor; - - -/* Forward declaration */ -static const sqlite3_tokenizer_module simpleTokenizerModule; - -static int isDelim(simple_tokenizer *t, unsigned char c){ - return c<0x80 && t->delim[c]; -} - -/* -** Create a new tokenizer instance. -*/ -static int simpleCreate( - int argc, const char * const *argv, - sqlite3_tokenizer **ppTokenizer -){ - simple_tokenizer *t; - - t = (simple_tokenizer *) calloc(sizeof(*t), 1); - if( t==NULL ) return SQLITE_NOMEM; - - /* TODO(shess) Delimiters need to remain the same from run to run, - ** else we need to reindex. One solution would be a meta-table to - ** track such information in the database, then we'd only want this - ** information on the initial create. - */ - if( argc>1 ){ - int i, n = strlen(argv[1]); - for(i=0; i=0x80 ){ - free(t); - return SQLITE_ERROR; - } - t->delim[ch] = 1; - } - } else { - /* Mark non-alphanumeric ASCII characters as delimiters */ - int i; - for(i=1; i<0x80; i++){ - t->delim[i] = !isalnum(i); - } - } - - *ppTokenizer = &t->base; - return SQLITE_OK; -} - -/* -** Destroy a tokenizer -*/ -static int simpleDestroy(sqlite3_tokenizer *pTokenizer){ - free(pTokenizer); - return SQLITE_OK; -} - -/* -** Prepare to begin tokenizing a particular string. The input -** string to be tokenized is pInput[0..nBytes-1]. A cursor -** used to incrementally tokenize this string is returned in -** *ppCursor. -*/ -static int simpleOpen( - sqlite3_tokenizer *pTokenizer, /* The tokenizer */ - const char *pInput, int nBytes, /* String to be tokenized */ - sqlite3_tokenizer_cursor **ppCursor /* OUT: Tokenization cursor */ -){ - simple_tokenizer_cursor *c; - - c = (simple_tokenizer_cursor *) malloc(sizeof(*c)); - if( c==NULL ) return SQLITE_NOMEM; - - c->pInput = pInput; - if( pInput==0 ){ - c->nBytes = 0; - }else if( nBytes<0 ){ - c->nBytes = (int)strlen(pInput); - }else{ - c->nBytes = nBytes; - } - c->iOffset = 0; /* start tokenizing at the beginning */ - c->iToken = 0; - c->pToken = NULL; /* no space allocated, yet. */ - c->nTokenAllocated = 0; - - *ppCursor = &c->base; - return SQLITE_OK; -} - -/* -** Close a tokenization cursor previously opened by a call to -** simpleOpen() above. -*/ -static int simpleClose(sqlite3_tokenizer_cursor *pCursor){ - simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor; - free(c->pToken); - free(c); - return SQLITE_OK; -} - -/* -** Extract the next token from a tokenization cursor. The cursor must -** have been opened by a prior call to simpleOpen(). -*/ -static int simpleNext( - sqlite3_tokenizer_cursor *pCursor, /* Cursor returned by simpleOpen */ - const char **ppToken, /* OUT: *ppToken is the token text */ - int *pnBytes, /* OUT: Number of bytes in token */ - int *piStartOffset, /* OUT: Starting offset of token */ - int *piEndOffset, /* OUT: Ending offset of token */ - int *piPosition /* OUT: Position integer of token */ -){ - simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor; - simple_tokenizer *t = (simple_tokenizer *) pCursor->pTokenizer; - unsigned char *p = (unsigned char *)c->pInput; - - while( c->iOffsetnBytes ){ - int iStartOffset; - - /* Scan past delimiter characters */ - while( c->iOffsetnBytes && isDelim(t, p[c->iOffset]) ){ - c->iOffset++; - } - - /* Count non-delimiter characters. */ - iStartOffset = c->iOffset; - while( c->iOffsetnBytes && !isDelim(t, p[c->iOffset]) ){ - c->iOffset++; - } - - if( c->iOffset>iStartOffset ){ - int i, n = c->iOffset-iStartOffset; - if( n>c->nTokenAllocated ){ - c->nTokenAllocated = n+20; - c->pToken = realloc(c->pToken, c->nTokenAllocated); - if( c->pToken==NULL ) return SQLITE_NOMEM; - } - for(i=0; ipToken[i] = ch<0x80 ? tolower(ch) : ch; - } - *ppToken = c->pToken; - *pnBytes = n; - *piStartOffset = iStartOffset; - *piEndOffset = c->iOffset; - *piPosition = c->iToken++; - - return SQLITE_OK; - } - } - return SQLITE_DONE; -} - -/* -** The set of routines that implement the simple tokenizer -*/ -static const sqlite3_tokenizer_module simpleTokenizerModule = { - 0, - simpleCreate, - simpleDestroy, - simpleOpen, - simpleClose, - simpleNext, -}; - -/* -** Allocate a new simple tokenizer. Return a pointer to the new -** tokenizer in *ppModule -*/ -void sqlite3Fts1SimpleTokenizerModule( - sqlite3_tokenizer_module const**ppModule -){ - *ppModule = &simpleTokenizerModule; -} - -#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS1) */ diff --git a/ext/fts1/fulltext.c b/ext/fts1/fulltext.c deleted file mode 100644 index 313ff303e1..0000000000 --- a/ext/fts1/fulltext.c +++ /dev/null @@ -1,1511 +0,0 @@ -/* The author disclaims copyright to this source code. - * - * This is an SQLite module implementing full-text search. - */ - -#include -#if !defined(__APPLE__) -#include -#else -#include -#endif -#include -#include -#include - -#include "fulltext.h" -#include "ft_hash.h" -#include "tokenizer.h" -#include "sqlite3.h" -#include "sqlite3ext.h" -SQLITE_EXTENSION_INIT1 - -/* utility functions */ - -/* We encode variable-length integers in little-endian order using seven bits - * per byte as follows: -** -** KEY: -** A = 0xxxxxxx 7 bits of data and one flag bit -** B = 1xxxxxxx 7 bits of data and one flag bit -** -** 7 bits - A -** 14 bits - BA -** 21 bits - BBA -** and so on. -*/ - -/* We may need up to VARINT_MAX bytes to store an encoded 64-bit integer. */ -#define VARINT_MAX 10 - -/* Write a 64-bit variable-length integer to memory starting at p[0]. - * The length of data written will be between 1 and VARINT_MAX bytes. - * The number of bytes written is returned. */ -static int putVarint(char *p, sqlite_int64 v){ - unsigned char *q = (unsigned char *) p; - sqlite_uint64 vu = v; - do{ - *q++ = (unsigned char) ((vu & 0x7f) | 0x80); - vu >>= 7; - }while( vu!=0 ); - q[-1] &= 0x7f; /* turn off high bit in final byte */ - assert( q - (unsigned char *)p <= VARINT_MAX ); - return (int) (q - (unsigned char *)p); -} - -/* Read a 64-bit variable-length integer from memory starting at p[0]. - * Return the number of bytes read, or 0 on error. - * The value is stored in *v. */ -static int getVarint(const char *p, sqlite_int64 *v){ - const unsigned char *q = (const unsigned char *) p; - sqlite_uint64 x = 0, y = 1; - while( (*q & 0x80) == 0x80 ){ - x += y * (*q++ & 0x7f); - y <<= 7; - if( q - (unsigned char *)p >= VARINT_MAX ){ /* bad data */ - assert( 0 ); - return 0; - } - } - x += y * (*q++); - *v = (sqlite_int64) x; - return (int) (q - (unsigned char *)p); -} - -static int getVarint32(const char *p, int *pi){ - sqlite_int64 i; - int ret = getVarint(p, &i); - *pi = (int) i; - assert( *pi==i ); - return ret; -} - -/*** Document lists *** - * - * A document list holds a sorted list of varint-encoded document IDs. - * - * A doclist with type DL_POSITIONS_OFFSETS is stored like this: - * - * array { - * varint docid; - * array { - * varint position; (delta from previous position plus 1, or 0 for end) - * varint startOffset; (delta from previous startOffset) - * varint endOffset; (delta from startOffset) - * } - * } - * - * Here, array { X } means zero or more occurrences of X, adjacent in memory. - * - * A doclist with type DL_POSITIONS is like the above, but holds only docids - * and positions without offset information. - * - * A doclist with type DL_DOCIDS is like the above, but holds only docids - * without positions or offset information. - * - * On disk, every document list has positions and offsets, so we don't bother - * to serialize a doclist's type. - * - * We don't yet delta-encode document IDs; doing so will probably be a - * modest win. - * - * NOTE(shess) I've thought of a slightly (1%) better offset encoding. - * After the first offset, estimate the next offset by using the - * current token position and the previous token position and offset, - * offset to handle some variance. So the estimate would be - * (iPosition*w->iStartOffset/w->iPosition-64), which is delta-encoded - * as normal. Offsets more than 64 chars from the estimate are - * encoded as the delta to the previous start offset + 128. An - * additional tiny increment can be gained by using the end offset of - * the previous token to make the estimate a tiny bit more precise. -*/ - -typedef enum DocListType { - DL_DOCIDS, /* docids only */ - DL_POSITIONS, /* docids + positions */ - DL_POSITIONS_OFFSETS /* docids + positions + offsets */ -} DocListType; - -typedef struct DocList { - char *pData; - int nData; - DocListType iType; - int iLastPos; /* the last position written */ - int iLastOffset; /* the last start offset written */ -} DocList; - -/* Initialize a new DocList to hold the given data. */ -static void docListInit(DocList *d, DocListType iType, - const char *pData, int nData){ - d->nData = nData; - if( nData>0 ){ - d->pData = malloc(nData); - memcpy(d->pData, pData, nData); - } else { - d->pData = NULL; - } - d->iType = iType; - d->iLastPos = 0; - d->iLastOffset = 0; -} - -/* Create a new dynamically-allocated DocList. */ -static DocList *docListNew(DocListType iType){ - DocList *d = (DocList *) malloc(sizeof(DocList)); - docListInit(d, iType, 0, 0); - return d; -} - -static void docListDestroy(DocList *d){ - free(d->pData); -#ifndef NDEBUG - memset(d, 0x55, sizeof(*d)); -#endif -} - -static void docListDelete(DocList *d){ - docListDestroy(d); - free(d); -} - -static char *docListEnd(DocList *d){ - return d->pData + d->nData; -} - -/* Append a varint to a DocList's data. */ -static void appendVarint(DocList *d, sqlite_int64 i){ - char c[VARINT_MAX]; - int n = putVarint(c, i); - d->pData = realloc(d->pData, d->nData + n); - memcpy(d->pData + d->nData, c, n); - d->nData += n; -} - -static void docListAddDocid(DocList *d, sqlite_int64 iDocid){ - appendVarint(d, iDocid); - d->iLastPos = 0; -} - -/* Add a position to the last position list in a doclist. */ -static void docListAddPos(DocList *d, int iPos){ - assert( d->iType>=DL_POSITIONS ); - appendVarint(d, iPos-d->iLastPos+1); - d->iLastPos = iPos; -} - -static void docListAddPosOffset(DocList *d, int iPos, - int iStartOffset, int iEndOffset){ - assert( d->iType==DL_POSITIONS_OFFSETS ); - docListAddPos(d, iPos); - appendVarint(d, iStartOffset-d->iLastOffset); - d->iLastOffset = iStartOffset; - appendVarint(d, iEndOffset-iStartOffset); -} - -/* Terminate the last position list in the given doclist. */ -static void docListAddEndPos(DocList *d){ - appendVarint(d, 0); -} - -typedef struct DocListReader { - DocList *pDoclist; - char *p; - int iLastPos; /* the last position read */ -} DocListReader; - -static void readerInit(DocListReader *r, DocList *pDoclist){ - r->pDoclist = pDoclist; - if( pDoclist!=NULL ){ - r->p = pDoclist->pData; - } - r->iLastPos = 0; -} - -static int readerAtEnd(DocListReader *pReader){ - return pReader->p >= docListEnd(pReader->pDoclist); -} - -/* Peek at the next docid without advancing the read pointer. */ -static sqlite_int64 peekDocid(DocListReader *pReader){ - sqlite_int64 ret; - assert( !readerAtEnd(pReader) ); - getVarint(pReader->p, &ret); - return ret; -} - -/* Read the next docid. */ -static sqlite_int64 readDocid(DocListReader *pReader){ - sqlite_int64 ret; - assert( !readerAtEnd(pReader) ); - pReader->p += getVarint(pReader->p, &ret); - pReader->iLastPos = 0; - return ret; -} - -/* Read the next position from a position list. - * Returns the position, or -1 at the end of the list. */ -static int readPosition(DocListReader *pReader){ - int i; - int iType = pReader->pDoclist->iType; - assert( iType>=DL_POSITIONS ); - assert( !readerAtEnd(pReader) ); - - pReader->p += getVarint32(pReader->p, &i); - if( i==0 ){ - pReader->iLastPos = -1; - return -1; - } - pReader->iLastPos += ((int) i)-1; - if( iType>=DL_POSITIONS_OFFSETS ){ - /* Skip over offsets, ignoring them for now. */ - int iStart, iEnd; - pReader->p += getVarint32(pReader->p, &iStart); - pReader->p += getVarint32(pReader->p, &iEnd); - } - return pReader->iLastPos; -} - -/* Skip past the end of a position list. */ -static void skipPositionList(DocListReader *pReader){ - while( readPosition(pReader)!=-1 ) - ; -} - -/* Skip over a docid, including its position list if the doclist has - * positions. */ -static void skipDocument(DocListReader *pReader){ - readDocid(pReader); - if( pReader->pDoclist->iType >= DL_POSITIONS ){ - skipPositionList(pReader); - } -} - -static sqlite_int64 firstDocid(DocList *d){ - DocListReader r; - readerInit(&r, d); - return readDocid(&r); -} - -/* Doclist multi-tool. Pass pUpdate==NULL to delete the indicated docid; - * otherwise pUpdate, which must contain only the single docid [iDocid], is - * inserted (if not present) or updated (if already present). */ -static int docListUpdate(DocList *d, sqlite_int64 iDocid, DocList *pUpdate){ - int modified = 0; - DocListReader reader; - char *p; - - if( pUpdate!=NULL ){ - assert( d->iType==pUpdate->iType); - assert( iDocid==firstDocid(pUpdate) ); - } - - readerInit(&reader, d); - while( !readerAtEnd(&reader) && peekDocid(&reader)nData -= (reader.p - p); - modified = 1; - } - - /* Insert if indicated. */ - if( pUpdate!=NULL ){ - int iDoclist = p-d->pData; - docListAddEndPos(pUpdate); - - d->pData = realloc(d->pData, d->nData+pUpdate->nData); - p = d->pData + iDoclist; - - memmove(p+pUpdate->nData, p, docListEnd(d) - p); - memcpy(p, pUpdate->pData, pUpdate->nData); - d->nData += pUpdate->nData; - modified = 1; - } - - return modified; -} - -/* Split the second half of doclist d into a separate doclist d2. Returns 1 - * if successful, or 0 if d contains a single document and hence can't be - * split. */ -static int docListSplit(DocList *d, DocList *d2){ - const char *pSplitPoint = d->pData + d->nData / 2; - DocListReader reader; - - readerInit(&reader, d); - while( reader.piType, reader.p, docListEnd(d) - reader.p); - d->nData = reader.p - d->pData; - d->pData = realloc(d->pData, d->nData); - return 1; -} - -/* A DocListMerge computes the AND of an in-memory DocList [in] and a chunked - * on-disk doclist, resulting in another in-memory DocList [out]. [in] - * and [out] may or may not store position information according to the - * caller's wishes. The on-disk doclist always comes with positions. - * - * The caller must read each chunk of the on-disk doclist in succession and - * pass it to mergeBlock(). - * - * If [in] has positions, then the merge output contains only documents with - * matching positions in the two input doclists. If [in] does not have - * positions, then the merge output contains all documents common to the two - * input doclists. - * - * If [in] is NULL, then the on-disk doclist is copied to [out] directly. - * - * A merge is performed using an integer [iOffset] provided by the caller. - * [iOffset] is subtracted from each position in the on-disk doclist for the - * purpose of position comparison; this is helpful in implementing phrase - * searches. - * - * A DocListMerge is not yet able to propagate offsets through query - * processing; we should add that capability soon. -*/ -typedef struct DocListMerge { - DocListReader in; - DocList *pOut; - int iOffset; -} DocListMerge; - -static void mergeInit(DocListMerge *m, - DocList *pIn, int iOffset, DocList *pOut){ - readerInit(&m->in, pIn); - m->pOut = pOut; - m->iOffset = iOffset; - - /* can't handle offsets yet */ - assert( pIn==NULL || pIn->iType <= DL_POSITIONS ); - assert( pOut->iType <= DL_POSITIONS ); -} - -/* A helper function for mergeBlock(), below. Merge the position lists - * pointed to by m->in and pBlockReader. - * If the merge matches, write [iDocid] to m->pOut; if m->pOut - * has positions then write all matching positions as well. */ -static void mergePosList(DocListMerge *m, sqlite_int64 iDocid, - DocListReader *pBlockReader){ - int block_pos = readPosition(pBlockReader); - int in_pos = readPosition(&m->in); - int match = 0; - while( block_pos!=-1 || in_pos!=-1 ){ - if( block_pos-m->iOffset==in_pos ){ - if( !match ){ - docListAddDocid(m->pOut, iDocid); - match = 1; - } - if( m->pOut->iType >= DL_POSITIONS ){ - docListAddPos(m->pOut, in_pos); - } - block_pos = readPosition(pBlockReader); - in_pos = readPosition(&m->in); - } else if( in_pos==-1 || (block_pos!=-1 && block_pos-m->iOffsetin); - } - } - if( m->pOut->iType >= DL_POSITIONS && match ){ - docListAddEndPos(m->pOut); - } -} - -/* Merge one block of an on-disk doclist into a DocListMerge. */ -static void mergeBlock(DocListMerge *m, DocList *pBlock){ - DocListReader blockReader; - assert( pBlock->iType >= DL_POSITIONS ); - readerInit(&blockReader, pBlock); - while( !readerAtEnd(&blockReader) ){ - sqlite_int64 iDocid = readDocid(&blockReader); - if( m->in.pDoclist!=NULL ){ - while( 1 ){ - if( readerAtEnd(&m->in) ) return; /* nothing more to merge */ - if( peekDocid(&m->in)>=iDocid ) break; - skipDocument(&m->in); - } - if( peekDocid(&m->in)>iDocid ){ /* [pIn] has no match with iDocid */ - skipPositionList(&blockReader); /* skip this docid in the block */ - continue; - } - readDocid(&m->in); - } - /* We have a document match. */ - if( m->in.pDoclist==NULL || m->in.pDoclist->iType < DL_POSITIONS ){ - /* We don't need to do a poslist merge. */ - docListAddDocid(m->pOut, iDocid); - if( m->pOut->iType >= DL_POSITIONS ){ - /* Copy all positions to the output doclist. */ - while( 1 ){ - int pos = readPosition(&blockReader); - if( pos==-1 ) break; - docListAddPos(m->pOut, pos); - } - docListAddEndPos(m->pOut); - } else skipPositionList(&blockReader); - continue; - } - mergePosList(m, iDocid, &blockReader); - } -} - -static char *string_dup_n(const char *s, int n){ - char *str = malloc(n + 1); - memcpy(str, s, n); - str[n] = '\0'; - return str; -} - -/* Duplicate a string; the caller must free() the returned string. - * (We don't use strdup() since it's not part of the standard C library and - * may not be available everywhere.) */ -static char *string_dup(const char *s){ - return string_dup_n(s, strlen(s)); -} - -/* Format a string, replacing each occurrence of the % character with - * zName. This may be more convenient than sqlite_mprintf() - * when one string is used repeatedly in a format string. - * The caller must free() the returned string. */ -static char *string_format(const char *zFormat, const char *zName){ - const char *p; - size_t len = 0; - size_t nName = strlen(zName); - char *result; - char *r; - - /* first compute length needed */ - for(p = zFormat ; *p ; ++p){ - len += (*p=='%' ? nName : 1); - } - len += 1; /* for null terminator */ - - r = result = malloc(len); - for(p = zFormat; *p; ++p){ - if( *p=='%' ){ - memcpy(r, zName, nName); - r += nName; - } else { - *r++ = *p; - } - } - *r++ = '\0'; - assert( r == result + len ); - return result; -} - -static int sql_exec(sqlite3 *db, const char *zName, const char *zFormat){ - char *zCommand = string_format(zFormat, zName); - int rc = sqlite3_exec(db, zCommand, NULL, 0, NULL); - free(zCommand); - return rc; -} - -static int sql_prepare(sqlite3 *db, const char *zName, sqlite3_stmt **ppStmt, - const char *zFormat){ - char *zCommand = string_format(zFormat, zName); - int rc = sqlite3_prepare(db, zCommand, -1, ppStmt, NULL); - free(zCommand); - return rc; -} - -/* end utility functions */ - -#define QUERY_GENERIC 0 -#define QUERY_FULLTEXT 1 - -#define CHUNK_MAX 1024 - -typedef enum fulltext_statement { - CONTENT_INSERT_STMT, - CONTENT_SELECT_STMT, - CONTENT_DELETE_STMT, - - TERM_SELECT_STMT, - TERM_CHUNK_SELECT_STMT, - TERM_INSERT_STMT, - TERM_UPDATE_STMT, - TERM_DELETE_STMT, - - MAX_STMT /* Always at end! */ -} fulltext_statement; - -/* These must exactly match the enum above. */ -/* TODO(adam): Is there some risk that a statement (in particular, -** pTermSelectStmt) will be used in two cursors at once, e.g. if a -** query joins a virtual table to itself? If so perhaps we should -** move some of these to the cursor object. -*/ -static const char *fulltext_zStatement[MAX_STMT] = { - /* CONTENT_INSERT */ "insert into %_content (rowid, content) values (?, ?)", - /* CONTENT_SELECT */ "select content from %_content where rowid = ?", - /* CONTENT_DELETE */ "delete from %_content where rowid = ?", - - /* TERM_SELECT */ - "select rowid, doclist from %_term where term = ? and first = ?", - /* TERM_CHUNK_SELECT */ - "select max(first) from %_term where term = ? and first <= ?", - /* TERM_INSERT */ - "insert into %_term (term, first, doclist) values (?, ?, ?)", - /* TERM_UPDATE */ "update %_term set doclist = ? where rowid = ?", - /* TERM_DELETE */ "delete from %_term where rowid = ?", -}; - -typedef struct fulltext_vtab { - sqlite3_vtab base; - sqlite3 *db; - const char *zName; /* virtual table name */ - sqlite3_tokenizer *pTokenizer; /* tokenizer for inserts and queries */ - - /* Precompiled statements which we keep as long as the table is - ** open. - */ - sqlite3_stmt *pFulltextStatements[MAX_STMT]; -} fulltext_vtab; - -typedef struct fulltext_cursor { - sqlite3_vtab_cursor base; - int iCursorType; /* QUERY_GENERIC or QUERY_FULLTEXT */ - - sqlite3_stmt *pStmt; - - int eof; - - /* The following is used only when iCursorType == QUERY_FULLTEXT. */ - DocListReader result; -} fulltext_cursor; - -static struct fulltext_vtab *cursor_vtab(fulltext_cursor *c){ - return (fulltext_vtab *) c->base.pVtab; -} - -static sqlite3_module fulltextModule; /* forward declaration */ - -/* Puts a freshly-prepared statement determined by iStmt in *ppStmt. -** If the indicated statement has never been prepared, it is prepared -** and cached, otherwise the cached version is reset. -*/ -static int sql_get_statement(fulltext_vtab *v, fulltext_statement iStmt, - sqlite3_stmt **ppStmt){ - assert( iStmtpFulltextStatements[iStmt]==NULL ){ - int rc = sql_prepare(v->db, v->zName, &v->pFulltextStatements[iStmt], - fulltext_zStatement[iStmt]); - if( rc!=SQLITE_OK ) return rc; - } else { - int rc = sqlite3_reset(v->pFulltextStatements[iStmt]); - if( rc!=SQLITE_OK ) return rc; - } - - *ppStmt = v->pFulltextStatements[iStmt]; - return SQLITE_OK; -} - -/* Step the indicated statement, handling errors SQLITE_BUSY (by -** retrying) and SQLITE_SCHEMA (by re-preparing and transferring -** bindings to the new statement). -** TODO(adam): We should extend this function so that it can work with -** statements declared locally, not only globally cached statements. -*/ -static int sql_step_statement(fulltext_vtab *v, fulltext_statement iStmt, - sqlite3_stmt **ppStmt){ - int rc; - sqlite3_stmt *s = *ppStmt; - assert( iStmtpFulltextStatements[iStmt] ); - - while( (rc=sqlite3_step(s))!=SQLITE_DONE && rc!=SQLITE_ROW ){ - sqlite3_stmt *pNewStmt; - - if( rc==SQLITE_BUSY ) continue; - if( rc!=SQLITE_ERROR ) return rc; - - rc = sqlite3_reset(s); - if( rc!=SQLITE_SCHEMA ) return SQLITE_ERROR; - - v->pFulltextStatements[iStmt] = NULL; /* Still in s */ - rc = sql_get_statement(v, iStmt, &pNewStmt); - if( rc!=SQLITE_OK ) goto err; - *ppStmt = pNewStmt; - - rc = sqlite3_transfer_bindings(s, pNewStmt); - if( rc!=SQLITE_OK ) goto err; - - rc = sqlite3_finalize(s); - if( rc!=SQLITE_OK ) return rc; - s = pNewStmt; - } - return rc; - - err: - sqlite3_finalize(s); - return rc; -} - -/* Like sql_step_statement(), but convert SQLITE_DONE to SQLITE_OK. -** Useful for statements like UPDATE, where we expect no results. -*/ -static int sql_single_step_statement(fulltext_vtab *v, - fulltext_statement iStmt, - sqlite3_stmt **ppStmt){ - int rc = sql_step_statement(v, iStmt, ppStmt); - return (rc==SQLITE_DONE) ? SQLITE_OK : rc; -} - -/* insert into %_content (rowid, content) values ([rowid], [zContent]) */ -static int content_insert(fulltext_vtab *v, sqlite3_value *rowid, - const char *zContent, int nContent){ - sqlite3_stmt *s; - int rc = sql_get_statement(v, CONTENT_INSERT_STMT, &s); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_value(s, 1, rowid); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_text(s, 2, zContent, nContent, SQLITE_STATIC); - if( rc!=SQLITE_OK ) return rc; - - return sql_single_step_statement(v, CONTENT_INSERT_STMT, &s); -} - -/* select content from %_content where rowid = [iRow] - * The caller must delete the returned string. */ -static int content_select(fulltext_vtab *v, sqlite_int64 iRow, - char **pzContent){ - sqlite3_stmt *s; - int rc = sql_get_statement(v, CONTENT_SELECT_STMT, &s); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_int64(s, 1, iRow); - if( rc!=SQLITE_OK ) return rc; - - rc = sql_step_statement(v, CONTENT_SELECT_STMT, &s); - if( rc!=SQLITE_ROW ) return rc; - - *pzContent = string_dup((const char *)sqlite3_column_text(s, 0)); - - /* We expect only one row. We must execute another sqlite3_step() - * to complete the iteration; otherwise the table will remain locked. */ - rc = sqlite3_step(s); - if( rc==SQLITE_DONE ) return SQLITE_OK; - - free(*pzContent); - return rc; -} - -/* delete from %_content where rowid = [iRow ] */ -static int content_delete(fulltext_vtab *v, sqlite_int64 iRow){ - sqlite3_stmt *s; - int rc = sql_get_statement(v, CONTENT_DELETE_STMT, &s); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_int64(s, 1, iRow); - if( rc!=SQLITE_OK ) return rc; - - return sql_single_step_statement(v, CONTENT_DELETE_STMT, &s); -} - -/* select rowid, doclist from %_term where term = [zTerm] and first = [iFirst] - * If found, returns SQLITE_OK; the caller must free the returned doclist. - * If no rows found, returns SQLITE_ERROR. */ -static int term_select(fulltext_vtab *v, const char *zTerm, int nTerm, - sqlite_int64 iFirst, - sqlite_int64 *rowid, - DocList *out){ - sqlite3_stmt *s; - int rc = sql_get_statement(v, TERM_SELECT_STMT, &s); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_text(s, 1, zTerm, nTerm, SQLITE_TRANSIENT); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_int64(s, 2, iFirst); - if( rc!=SQLITE_OK ) return rc; - - rc = sql_step_statement(v, TERM_SELECT_STMT, &s); - if( rc!=SQLITE_ROW ) return rc==SQLITE_DONE ? SQLITE_ERROR : rc; - - *rowid = sqlite3_column_int64(s, 0); - docListInit(out, DL_POSITIONS_OFFSETS, - sqlite3_column_blob(s, 1), sqlite3_column_bytes(s, 1)); - - /* We expect only one row. We must execute another sqlite3_step() - * to complete the iteration; otherwise the table will remain locked. */ - rc = sqlite3_step(s); - return rc==SQLITE_DONE ? SQLITE_OK : rc; -} - -/* select max(first) from %_term where term = [zTerm] and first <= [iFirst] - * If found, returns SQLITE_ROW and result in *piResult; if the query returns - * NULL (meaning no row found) returns SQLITE_DONE. - */ -static int term_chunk_select(fulltext_vtab *v, const char *zTerm, int nTerm, - sqlite_int64 iFirst, sqlite_int64 *piResult){ - sqlite3_stmt *s; - int rc = sql_get_statement(v, TERM_CHUNK_SELECT_STMT, &s); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_text(s, 1, zTerm, nTerm, SQLITE_STATIC); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_int64(s, 2, iFirst); - if( rc!=SQLITE_OK ) return rc; - - rc = sql_step_statement(v, TERM_CHUNK_SELECT_STMT, &s); - if( rc!=SQLITE_ROW ) return rc==SQLITE_DONE ? SQLITE_ERROR : rc; - - switch( sqlite3_column_type(s, 0) ){ - case SQLITE_NULL: - rc = SQLITE_DONE; - break; - case SQLITE_INTEGER: - *piResult = sqlite3_column_int64(s, 0); - break; - default: - return SQLITE_ERROR; - } - /* We expect only one row. We must execute another sqlite3_step() - * to complete the iteration; otherwise the table will remain locked. */ - if( sqlite3_step(s) != SQLITE_DONE ) return SQLITE_ERROR; - return rc; -} - -/* insert into %_term (term, first, doclist) - values ([zTerm], [iFirst], [doclist]) */ -static int term_insert(fulltext_vtab *v, const char *zTerm, int nTerm, - sqlite_int64 iFirst, DocList *doclist){ - sqlite3_stmt *s; - int rc = sql_get_statement(v, TERM_INSERT_STMT, &s); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_text(s, 1, zTerm, nTerm, SQLITE_STATIC); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_int64(s, 2, iFirst); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_blob(s, 3, doclist->pData, doclist->nData, SQLITE_STATIC); - if( rc!=SQLITE_OK ) return rc; - - return sql_single_step_statement(v, TERM_INSERT_STMT, &s); -} - -/* update %_term set doclist = [doclist] where rowid = [rowid] */ -static int term_update(fulltext_vtab *v, sqlite_int64 rowid, - DocList *doclist){ - sqlite3_stmt *s; - int rc = sql_get_statement(v, TERM_UPDATE_STMT, &s); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_blob(s, 1, doclist->pData, doclist->nData, - SQLITE_STATIC); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_int64(s, 2, rowid); - if( rc!=SQLITE_OK ) return rc; - - return sql_single_step_statement(v, TERM_UPDATE_STMT, &s); -} - -static int term_delete(fulltext_vtab *v, sqlite_int64 rowid){ - sqlite3_stmt *s; - int rc = sql_get_statement(v, TERM_DELETE_STMT, &s); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_int64(s, 1, rowid); - if( rc!=SQLITE_OK ) return rc; - - return sql_single_step_statement(v, TERM_DELETE_STMT, &s); -} - -static void fulltext_vtab_destroy(fulltext_vtab *v){ - int iStmt; - - for( iStmt=0; iStmtpFulltextStatements[iStmt]!=NULL ){ - sqlite3_finalize(v->pFulltextStatements[iStmt]); - v->pFulltextStatements[iStmt] = NULL; - } - } - - if( v->pTokenizer!=NULL ){ - v->pTokenizer->pModule->xDestroy(v->pTokenizer); - v->pTokenizer = NULL; - } - - free((void *) v->zName); - free(v); -} - -/* Current interface: -** argv[0] - module name -** argv[1] - database name -** argv[2] - table name -** argv[3] - tokenizer name (optional, a sensible default is provided) -** argv[4..] - passed to tokenizer (optional based on tokenizer) -**/ -static int fulltextConnect( - sqlite3 *db, - void *pAux, - int argc, - const char * const *argv, - sqlite3_vtab **ppVTab, - char **pzErr -){ - int rc; - fulltext_vtab *v; - sqlite3_tokenizer_module *m = NULL; - - assert( argc>=3 ); - v = (fulltext_vtab *) malloc(sizeof(fulltext_vtab)); - /* sqlite will initialize v->base */ - v->db = db; - v->zName = string_dup(argv[2]); - v->pTokenizer = NULL; - - if( argc==3 ){ - get_simple_tokenizer_module(&m); - } else { - /* TODO(shess) For now, add new tokenizers as else if clauses. */ - if( !strcmp(argv[3], "simple") ){ - get_simple_tokenizer_module(&m); - } else { - assert( "unrecognized tokenizer"==NULL ); - } - } - - /* TODO(shess) Since tokenization impacts the index, the parameters - ** to the tokenizer need to be identical when a persistent virtual - ** table is re-created. One solution would be a meta-table to track - ** such information in the database. Then we could verify that the - ** information is identical on subsequent creates. - */ - /* TODO(shess) Why isn't argv already (const char **)? */ - rc = m->xCreate(argc-3, (const char **) (argv+3), &v->pTokenizer); - if( rc!=SQLITE_OK ) return rc; - v->pTokenizer->pModule = m; - - /* TODO: verify the existence of backing tables foo_content, foo_term */ - - rc = sqlite3_declare_vtab(db, "create table x(content text)"); - if( rc!=SQLITE_OK ) return rc; - - memset(v->pFulltextStatements, 0, sizeof(v->pFulltextStatements)); - - *ppVTab = &v->base; - return SQLITE_OK; -} - -static int fulltextCreate( - sqlite3 *db, - void *pAux, - int argc, - const char * const *argv, - sqlite3_vtab **ppVTab, - char **pzErr -){ - int rc; - assert( argc>=3 ); - - /* The %_content table holds the text of each full-text item, with - ** the rowid used as the docid. - ** - ** The %_term table maps each term to a document list blob - ** containing elements sorted by ascending docid, each element - ** encoded as: - ** - ** docid varint-encoded - ** token count varint-encoded - ** "count" token elements (poslist): - ** position varint-encoded as delta from previous position - ** start offset varint-encoded as delta from previous start offset - ** end offset varint-encoded as delta from start offset - ** - ** Additionally, doclist blobs can be chunked into multiple rows, - ** using "first" to order the blobs. "first" is simply the first - ** docid in the blob. - */ - /* - ** NOTE(shess) That last sentence is incorrect in the face of - ** deletion, which can leave a doclist that doesn't contain the - ** first from that row. I _believe_ this does not matter to the - ** operation of the system, but it might be reasonable to update - ** appropriately in case this assumption becomes more important. - */ - rc = sql_exec(db, argv[2], - "create table %_content(content text);" - "create table %_term(term text, first integer, doclist blob);" - "create index %_index on %_term(term, first)"); - if( rc!=SQLITE_OK ) return rc; - - return fulltextConnect(db, pAux, argc, argv, ppVTab, pzErr); -} - -/* Decide how to handle an SQL query. - * At the moment, MATCH queries can include implicit boolean ANDs; we - * haven't implemented phrase searches or OR yet. */ -static int fulltextBestIndex(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){ - int i; - - for(i=0; inConstraint; ++i){ - const struct sqlite3_index_constraint *pConstraint; - pConstraint = &pInfo->aConstraint[i]; - if( pConstraint->iColumn==0 && - pConstraint->op==SQLITE_INDEX_CONSTRAINT_MATCH && - pConstraint->usable ){ /* a full-text search */ - pInfo->aConstraintUsage[i].argvIndex = 1; - pInfo->aConstraintUsage[i].omit = 1; - pInfo->idxNum = QUERY_FULLTEXT; - pInfo->estimatedCost = 1.0; /* an arbitrary value for now */ - return SQLITE_OK; - } - } - pInfo->idxNum = QUERY_GENERIC; - return SQLITE_OK; -} - -static int fulltextDisconnect(sqlite3_vtab *pVTab){ - fulltext_vtab_destroy((fulltext_vtab *)pVTab); - return SQLITE_OK; -} - -static int fulltextDestroy(sqlite3_vtab *pVTab){ - fulltext_vtab *v = (fulltext_vtab *)pVTab; - - int rc = sql_exec(v->db, v->zName, - "drop table %_content; drop table %_term"); - if( rc!=SQLITE_OK ) return rc; - - fulltext_vtab_destroy((fulltext_vtab *)pVTab); - return SQLITE_OK; -} - -static int fulltextOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){ - fulltext_cursor *c; - - c = (fulltext_cursor *) calloc(sizeof(fulltext_cursor), 1); - /* sqlite will initialize c->base */ - *ppCursor = &c->base; - - return SQLITE_OK; -} - -static int fulltextClose(sqlite3_vtab_cursor *pCursor){ - fulltext_cursor *c = (fulltext_cursor *) pCursor; - sqlite3_finalize(c->pStmt); - if( c->result.pDoclist!=NULL ){ - docListDelete(c->result.pDoclist); - } - free(c); - return SQLITE_OK; -} - -static int fulltextNext(sqlite3_vtab_cursor *pCursor){ - fulltext_cursor *c = (fulltext_cursor *) pCursor; - sqlite_int64 iDocid; - int rc; - - switch( c->iCursorType ){ - case QUERY_GENERIC: - /* TODO(shess) Handle SQLITE_SCHEMA AND SQLITE_BUSY. */ - rc = sqlite3_step(c->pStmt); - switch( rc ){ - case SQLITE_ROW: - c->eof = 0; - return SQLITE_OK; - case SQLITE_DONE: - c->eof = 1; - return SQLITE_OK; - default: - c->eof = 1; - return rc; - } - case QUERY_FULLTEXT: - rc = sqlite3_reset(c->pStmt); - if( rc!=SQLITE_OK ) return rc; - - if( readerAtEnd(&c->result)){ - c->eof = 1; - return SQLITE_OK; - } - iDocid = readDocid(&c->result); - rc = sqlite3_bind_int64(c->pStmt, 1, iDocid); - if( rc!=SQLITE_OK ) return rc; - /* TODO(shess) Handle SQLITE_SCHEMA AND SQLITE_BUSY. */ - rc = sqlite3_step(c->pStmt); - if( rc==SQLITE_ROW ){ /* the case we expect */ - c->eof = 0; - return SQLITE_OK; - } - /* an error occurred; abort */ - return rc==SQLITE_DONE ? SQLITE_ERROR : rc; - default: - assert( 0 ); - return SQLITE_ERROR; /* not reached */ - } -} - -static int term_select_doclist(fulltext_vtab *v, const char *pTerm, int nTerm, - sqlite3_stmt **ppStmt){ - int rc; - if( *ppStmt ){ - rc = sqlite3_reset(*ppStmt); - } else { - rc = sql_prepare(v->db, v->zName, ppStmt, - "select doclist from %_term where term = ? order by first"); - } - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_text(*ppStmt, 1, pTerm, nTerm, SQLITE_TRANSIENT); - if( rc!=SQLITE_OK ) return rc; - - return sqlite3_step(*ppStmt); /* TODO(adamd): handle schema error */ -} - -/* Read the posting list for [zTerm]; AND it with the doclist [in] to - * produce the doclist [out], using the given offset [iOffset] for phrase - * matching. - * (*pSelect) is used to hold an SQLite statement used inside this function; - * the caller should initialize *pSelect to NULL before the first call. - */ -static int query_merge(fulltext_vtab *v, sqlite3_stmt **pSelect, - const char *zTerm, - DocList *pIn, int iOffset, DocList *out){ - int rc; - DocListMerge merge; - - if( pIn!=NULL && !pIn->nData ){ - /* If [pIn] is already empty, there's no point in reading the - * posting list to AND it in; return immediately. */ - return SQLITE_OK; - } - - rc = term_select_doclist(v, zTerm, -1, pSelect); - if( rc!=SQLITE_ROW && rc!=SQLITE_DONE ) return rc; - - mergeInit(&merge, pIn, iOffset, out); - while( rc==SQLITE_ROW ){ - DocList block; - docListInit(&block, DL_POSITIONS_OFFSETS, - sqlite3_column_blob(*pSelect, 0), - sqlite3_column_bytes(*pSelect, 0)); - mergeBlock(&merge, &block); - docListDestroy(&block); - - rc = sqlite3_step(*pSelect); - if( rc!=SQLITE_ROW && rc!=SQLITE_DONE ){ - return rc; - } - } - - return SQLITE_OK; -} - -typedef struct QueryTerm { - int is_phrase; /* true if this term begins a new phrase */ - const char *zTerm; -} QueryTerm; - -/* A parsed query. - * - * As an example, parsing the query ["four score" years "new nation"] will - * yield a Query with 5 terms: - * "four", is_phrase = 1 - * "score", is_phrase = 0 - * "years", is_phrase = 1 - * "new", is_phrase = 1 - * "nation", is_phrase = 0 - */ -typedef struct Query { - int nTerms; - QueryTerm *pTerm; -} Query; - -static void query_add(Query *q, int is_phrase, const char *zTerm){ - QueryTerm *t; - ++q->nTerms; - q->pTerm = realloc(q->pTerm, q->nTerms * sizeof(q->pTerm[0])); - t = &q->pTerm[q->nTerms - 1]; - t->is_phrase = is_phrase; - t->zTerm = zTerm; -} - -static void query_free(Query *q){ - int i; - for(i = 0; i < q->nTerms; ++i){ - free((void *) q->pTerm[i].zTerm); - } - free(q->pTerm); -} - -static int tokenize_segment(sqlite3_tokenizer *pTokenizer, - const char *zQuery, int in_phrase, - Query *pQuery){ - sqlite3_tokenizer_module *pModule = pTokenizer->pModule; - sqlite3_tokenizer_cursor *pCursor; - int is_first = 1; - - int rc = pModule->xOpen(pTokenizer, zQuery, -1, &pCursor); - if( rc!=SQLITE_OK ) return rc; - pCursor->pTokenizer = pTokenizer; - - while( 1 ){ - const char *zToken; - int nToken, iStartOffset, iEndOffset, dummy_pos; - - rc = pModule->xNext(pCursor, - &zToken, &nToken, - &iStartOffset, &iEndOffset, - &dummy_pos); - if( rc!=SQLITE_OK ) break; - query_add(pQuery, !in_phrase || is_first, string_dup_n(zToken, nToken)); - is_first = 0; - } - - return pModule->xClose(pCursor); -} - -/* Parse a query string, yielding a Query object. */ -static int parse_query(fulltext_vtab *v, const char *zQuery, Query *pQuery){ - char *zQuery1 = string_dup(zQuery); - int in_phrase = 0; - char *s = zQuery1; - pQuery->nTerms = 0; - pQuery->pTerm = NULL; - - while( *s ){ - char *t = s; - while( *t ){ - if( *t=='"' ){ - *t++ = '\0'; - break; - } - ++t; - } - if( *s ){ - tokenize_segment(v->pTokenizer, s, in_phrase, pQuery); - } - s = t; - in_phrase = !in_phrase; - } - - free(zQuery1); - return SQLITE_OK; -} - -/* Perform a full-text query; return a list of documents in [pResult]. */ -static int fulltext_query(fulltext_vtab *v, const char *zQuery, - DocList **pResult){ - Query q; - int phrase_start = -1; - int i; - sqlite3_stmt *pSelect = NULL; - DocList *d = NULL; - - int rc = parse_query(v, zQuery, &q); - if( rc!=SQLITE_OK ) return rc; - - /* Merge terms. */ - for(i = 0 ; i < q.nTerms ; ++i){ - /* In each merge step, we need to generate positions whenever we're - * processing a phrase which hasn't ended yet. */ - int need_positions = iiCursorType = idxNum; - switch( idxNum ){ - case QUERY_GENERIC: - zStatement = "select rowid, content from %_content"; - break; - - case QUERY_FULLTEXT: /* full-text search */ - { - const char *zQuery = (const char *)sqlite3_value_text(argv[0]); - DocList *pResult; - assert( argc==1 ); - rc = fulltext_query(v, zQuery, &pResult); - if( rc!=SQLITE_OK ) return rc; - readerInit(&c->result, pResult); - zStatement = "select rowid, content from %_content where rowid = ?"; - break; - } - - default: - assert( 0 ); - } - - rc = sql_prepare(v->db, v->zName, &c->pStmt, zStatement); - if( rc!=SQLITE_OK ) return rc; - - return fulltextNext(pCursor); -} - -static int fulltextEof(sqlite3_vtab_cursor *pCursor){ - fulltext_cursor *c = (fulltext_cursor *) pCursor; - return c->eof; -} - -static int fulltextColumn(sqlite3_vtab_cursor *pCursor, - sqlite3_context *pContext, int idxCol){ - fulltext_cursor *c = (fulltext_cursor *) pCursor; - const char *s; - - assert( idxCol==0 ); - s = (const char *) sqlite3_column_text(c->pStmt, 1); - sqlite3_result_text(pContext, s, -1, SQLITE_TRANSIENT); - - return SQLITE_OK; -} - -static int fulltextRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){ - fulltext_cursor *c = (fulltext_cursor *) pCursor; - - *pRowid = sqlite3_column_int64(c->pStmt, 0); - return SQLITE_OK; -} - -/* Build a hash table containing all terms in zText. */ -static int build_terms(Hash *terms, sqlite3_tokenizer *pTokenizer, - const char *zText, sqlite_int64 iDocid){ - sqlite3_tokenizer_cursor *pCursor; - const char *pToken; - int nTokenBytes; - int iStartOffset, iEndOffset, iPosition; - - int rc = pTokenizer->pModule->xOpen(pTokenizer, zText, -1, &pCursor); - if( rc!=SQLITE_OK ) return rc; - - pCursor->pTokenizer = pTokenizer; - HashInit(terms, HASH_STRING, 1); - while( SQLITE_OK==pTokenizer->pModule->xNext(pCursor, - &pToken, &nTokenBytes, - &iStartOffset, &iEndOffset, - &iPosition) ){ - DocList *p; - - /* Positions can't be negative; we use -1 as a terminator internally. */ - if( iPosition<0 ) { - rc = SQLITE_ERROR; - goto err; - } - - p = HashFind(terms, pToken, nTokenBytes); - if( p==NULL ){ - p = docListNew(DL_POSITIONS_OFFSETS); - docListAddDocid(p, iDocid); - HashInsert(terms, pToken, nTokenBytes, p); - } - docListAddPosOffset(p, iPosition, iStartOffset, iEndOffset); - } - -err: - /* TODO(shess) Check return? Should this be able to cause errors at - ** this point? Actually, same question about sqlite3_finalize(), - ** though one could argue that failure there means that the data is - ** not durable. *ponder* - */ - pTokenizer->pModule->xClose(pCursor); - return rc; -} -/* Update the %_terms table to map the term [zTerm] to the given rowid. */ -static int index_insert_term(fulltext_vtab *v, const char *zTerm, int nTerm, - sqlite_int64 iDocid, DocList *p){ - sqlite_int64 iFirst; - sqlite_int64 iIndexRow; - DocList doclist; - - int rc = term_chunk_select(v, zTerm, nTerm, iDocid, &iFirst); - if( rc==SQLITE_DONE ){ - docListInit(&doclist, DL_POSITIONS_OFFSETS, 0, 0); - if( docListUpdate(&doclist, iDocid, p) ){ - rc = term_insert(v, zTerm, nTerm, iDocid, &doclist); - docListDestroy(&doclist); - return rc; - } - return SQLITE_OK; - } - if( rc!=SQLITE_ROW ) return SQLITE_ERROR; - - /* This word is in the index; add this document ID to its blob. */ - - rc = term_select(v, zTerm, nTerm, iFirst, &iIndexRow, &doclist); - if( rc!=SQLITE_OK ) return rc; - - if( docListUpdate(&doclist, iDocid, p) ){ - /* If the blob is too big, split it in half. */ - if( doclist.nData>CHUNK_MAX ){ - DocList half; - if( docListSplit(&doclist, &half) ){ - rc = term_insert(v, zTerm, nTerm, firstDocid(&half), &half); - docListDestroy(&half); - if( rc!=SQLITE_OK ) goto err; - } - } - rc = term_update(v, iIndexRow, &doclist); - } - -err: - docListDestroy(&doclist); - return rc; -} - -/* Insert a row into the full-text index; set *piRowid to be the ID of the - * new row. */ -static int index_insert(fulltext_vtab *v, - sqlite3_value *pRequestRowid, const char *zText, - sqlite_int64 *piRowid){ - Hash terms; /* maps term string -> PosList */ - HashElem *e; - - int rc = content_insert(v, pRequestRowid, zText, -1); - if( rc!=SQLITE_OK ) return rc; - *piRowid = sqlite3_last_insert_rowid(v->db); - - if( !zText ) return SQLITE_OK; /* nothing to index */ - - rc = build_terms(&terms, v->pTokenizer, zText, *piRowid); - if( rc!=SQLITE_OK ) return rc; - - for(e=HashFirst(&terms); e; e=HashNext(e)){ - DocList *p = HashData(e); - rc = index_insert_term(v, HashKey(e), HashKeysize(e), *piRowid, p); - if( rc!=SQLITE_OK ) break; - } - - for(e=HashFirst(&terms); e; e=HashNext(e)){ - DocList *p = HashData(e); - docListDelete(p); - } - HashClear(&terms); - return rc; -} - -static int index_delete_term(fulltext_vtab *v, const char *zTerm, int nTerm, - sqlite_int64 iDocid){ - sqlite_int64 iFirst; - sqlite_int64 iIndexRow; - DocList doclist; - - int rc = term_chunk_select(v, zTerm, nTerm, iDocid, &iFirst); - if( rc!=SQLITE_ROW ) return SQLITE_ERROR; - - rc = term_select(v, zTerm, nTerm, iFirst, &iIndexRow, &doclist); - if( rc!=SQLITE_OK ) return rc; - - if( docListUpdate(&doclist, iDocid, NULL) ){ - if( doclist.nData>0 ){ - rc = term_update(v, iIndexRow, &doclist); - } else { /* empty posting list */ - rc = term_delete(v, iIndexRow); - } - } - docListDestroy(&doclist); - return rc; -} - -/* Delete a row from the full-text index. */ -static int index_delete(fulltext_vtab *v, sqlite_int64 iRow){ - char *zText; - Hash terms; - HashElem *e; - - int rc = content_select(v, iRow, &zText); - if( rc!=SQLITE_OK ) return rc; - - rc = build_terms(&terms, v->pTokenizer, zText, iRow); - free(zText); - if( rc!=SQLITE_OK ) return rc; - - for(e=HashFirst(&terms); e; e=HashNext(e)){ - rc = index_delete_term(v, HashKey(e), HashKeysize(e), iRow); - if( rc!=SQLITE_OK ) break; - } - for(e=HashFirst(&terms); e; e=HashNext(e)){ - DocList *p = HashData(e); - docListDelete(p); - } - HashClear(&terms); - - return content_delete(v, iRow); -} - -static int fulltextUpdate(sqlite3_vtab *pVtab, int nArg, sqlite3_value **ppArg, - sqlite_int64 *pRowid){ - fulltext_vtab *v = (fulltext_vtab *) pVtab; - - if( nArg<2 ){ - return index_delete(v, sqlite3_value_int64(ppArg[0])); - } - - if( sqlite3_value_type(ppArg[0]) != SQLITE_NULL ){ - return SQLITE_ERROR; /* an update; not yet supported */ - } - - assert( nArg==3 ); /* ppArg[1] = rowid, ppArg[2] = content */ - return index_insert(v, ppArg[1], - (const char *)sqlite3_value_text(ppArg[2]), pRowid); -} - -static sqlite3_module fulltextModule = { - 0, - fulltextCreate, - fulltextConnect, - fulltextBestIndex, - fulltextDisconnect, - fulltextDestroy, - fulltextOpen, - fulltextClose, - fulltextFilter, - fulltextNext, - fulltextEof, - fulltextColumn, - fulltextRowid, - fulltextUpdate -}; - -int fulltext_init(sqlite3 *db){ - return sqlite3_create_module(db, "fulltext", &fulltextModule, 0); -} - -#if !SQLITE_CORE -#ifdef _WIN32 -__declspec(dllexport) -#endif -int sqlite3_fulltext_init(sqlite3 *db, char **pzErrMsg, - const sqlite3_api_routines *pApi){ - SQLITE_EXTENSION_INIT2(pApi) - return fulltext_init(db); -} -#endif diff --git a/ext/fts1/fulltext.h b/ext/fts1/fulltext.h deleted file mode 100644 index 477dcab2ad..0000000000 --- a/ext/fts1/fulltext.h +++ /dev/null @@ -1,11 +0,0 @@ -#include "sqlite3.h" - -#ifdef __cplusplus -extern "C" { -#endif /* __cplusplus */ - -int fulltext_init(sqlite3 *db); - -#ifdef __cplusplus -} /* extern "C" */ -#endif /* __cplusplus */ diff --git a/ext/fts1/simple_tokenizer.c b/ext/fts1/simple_tokenizer.c deleted file mode 100644 index 0ddc7055af..0000000000 --- a/ext/fts1/simple_tokenizer.c +++ /dev/null @@ -1,174 +0,0 @@ -/* -** The author disclaims copyright to this source code. -** -************************************************************************* -** Implementation of the "simple" full-text-search tokenizer. -*/ - -#include -#if !defined(__APPLE__) -#include -#else -#include -#endif -#include -#include -#include - -#include "tokenizer.h" - -/* Duplicate a string; the caller must free() the returned string. - * (We don't use strdup() since it's not part of the standard C library and - * may not be available everywhere.) */ -/* TODO(shess) Copied from fulltext.c, consider util.c for such -** things. */ -static char *string_dup(const char *s){ - char *str = malloc(strlen(s) + 1); - strcpy(str, s); - return str; -} - -typedef struct simple_tokenizer { - sqlite3_tokenizer base; - const char *zDelim; /* token delimiters */ -} simple_tokenizer; - -typedef struct simple_tokenizer_cursor { - sqlite3_tokenizer_cursor base; - const char *pInput; /* input we are tokenizing */ - int nBytes; /* size of the input */ - const char *pCurrent; /* current position in pInput */ - int iToken; /* index of next token to be returned */ - char *zToken; /* storage for current token */ - int nTokenBytes; /* actual size of current token */ - int nTokenAllocated; /* space allocated to zToken buffer */ -} simple_tokenizer_cursor; - -static sqlite3_tokenizer_module simpleTokenizerModule;/* forward declaration */ - -static int simpleCreate( - int argc, const char **argv, - sqlite3_tokenizer **ppTokenizer -){ - simple_tokenizer *t; - - t = (simple_tokenizer *) malloc(sizeof(simple_tokenizer)); - /* TODO(shess) Delimiters need to remain the same from run to run, - ** else we need to reindex. One solution would be a meta-table to - ** track such information in the database, then we'd only want this - ** information on the initial create. - */ - if( argc>1 ){ - t->zDelim = string_dup(argv[1]); - } else { - /* Build a string excluding alphanumeric ASCII characters */ - char zDelim[0x80]; /* nul-terminated, so nul not a member */ - int i, j; - for(i=1, j=0; i<0x80; i++){ - if( !isalnum(i) ){ - zDelim[j++] = i; - } - } - zDelim[j++] = '\0'; - assert( j<=sizeof(zDelim) ); - t->zDelim = string_dup(zDelim); - } - - *ppTokenizer = &t->base; - return SQLITE_OK; -} - -static int simpleDestroy(sqlite3_tokenizer *pTokenizer){ - simple_tokenizer *t = (simple_tokenizer *) pTokenizer; - - free((void *) t->zDelim); - free(t); - - return SQLITE_OK; -} - -static int simpleOpen( - sqlite3_tokenizer *pTokenizer, - const char *pInput, int nBytes, - sqlite3_tokenizer_cursor **ppCursor -){ - simple_tokenizer_cursor *c; - - c = (simple_tokenizer_cursor *) malloc(sizeof(simple_tokenizer_cursor)); - c->pInput = pInput; - c->nBytes = nBytes<0 ? (int) strlen(pInput) : nBytes; - c->pCurrent = c->pInput; /* start tokenizing at the beginning */ - c->iToken = 0; - c->zToken = NULL; /* no space allocated, yet. */ - c->nTokenBytes = 0; - c->nTokenAllocated = 0; - - *ppCursor = &c->base; - return SQLITE_OK; -} - -static int simpleClose(sqlite3_tokenizer_cursor *pCursor){ - simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor; - - if( NULL!=c->zToken ){ - free(c->zToken); - } - free(c); - - return SQLITE_OK; -} - -static int simpleNext( - sqlite3_tokenizer_cursor *pCursor, - const char **ppToken, int *pnBytes, - int *piStartOffset, int *piEndOffset, int *piPosition -){ - simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor; - simple_tokenizer *t = (simple_tokenizer *) pCursor->pTokenizer; - int ii; - - while( c->pCurrent-c->pInputnBytes ){ - int n = (int) strcspn(c->pCurrent, t->zDelim); - if( n>0 ){ - if( n+1>c->nTokenAllocated ){ - c->zToken = realloc(c->zToken, n+1); - } - for(ii=0; iipCurrent[ii]; - c->zToken[ii] = (unsigned char)ch<0x80 ? tolower((unsigned char)ch):ch; - } - c->zToken[n] = '\0'; - *ppToken = c->zToken; - *pnBytes = n; - *piStartOffset = (int) (c->pCurrent-c->pInput); - *piEndOffset = *piStartOffset+n; - *piPosition = c->iToken++; - c->pCurrent += n + 1; - - return SQLITE_OK; - } - c->pCurrent += n + 1; - /* TODO(shess) could strspn() to skip delimiters en masse. Needs - ** to happen in two places, though, which is annoying. - */ - } - return SQLITE_DONE; -} - -static sqlite3_tokenizer_module simpleTokenizerModule = { - 0, - simpleCreate, - simpleDestroy, - simpleOpen, - simpleClose, - simpleNext, -}; - -void get_simple_tokenizer_module( - sqlite3_tokenizer_module **ppModule -){ - *ppModule = &simpleTokenizerModule; -} diff --git a/ext/fts1/tokenizer.h b/ext/fts1/tokenizer.h deleted file mode 100644 index 1d7bd1f670..0000000000 --- a/ext/fts1/tokenizer.h +++ /dev/null @@ -1,89 +0,0 @@ -/* -** 2006 July 10 -** -** The author disclaims copyright to this source code. -** -************************************************************************* -** Defines the interface to tokenizers used by fulltext-search. There -** are three basic components: -** -** sqlite3_tokenizer_module is a singleton defining the tokenizer -** interface functions. This is essentially the class structure for -** tokenizers. -** -** sqlite3_tokenizer is used to define a particular tokenizer, perhaps -** including customization information defined at creation time. -** -** sqlite3_tokenizer_cursor is generated by a tokenizer to generate -** tokens from a particular input. -*/ -#ifndef _TOKENIZER_H_ -#define _TOKENIZER_H_ - -/* TODO(shess) Only used for SQLITE_OK and SQLITE_DONE at this time. -** If tokenizers are to be allowed to call sqlite3_*() functions, then -** we will need a way to register the API consistently. -*/ -#include "sqlite3.h" - -/* -** Structures used by the tokenizer interface. -*/ -typedef struct sqlite3_tokenizer sqlite3_tokenizer; -typedef struct sqlite3_tokenizer_cursor sqlite3_tokenizer_cursor; -typedef struct sqlite3_tokenizer_module sqlite3_tokenizer_module; - -struct sqlite3_tokenizer_module { - int iVersion; /* currently 0 */ - - /* - ** Create and destroy a tokenizer. argc/argv are passed down from - ** the fulltext virtual table creation to allow customization. - */ - int (*xCreate)(int argc, const char **argv, - sqlite3_tokenizer **ppTokenizer); - int (*xDestroy)(sqlite3_tokenizer *pTokenizer); - - /* - ** Tokenize a particular input. Call xOpen() to prepare to - ** tokenize, xNext() repeatedly until it returns SQLITE_DONE, then - ** xClose() to free any internal state. The pInput passed to - ** xOpen() must exist until the cursor is closed. The ppToken - ** result from xNext() is only valid until the next call to xNext() - ** or until xClose() is called. - */ - /* TODO(shess) current implementation requires pInput to be - ** nul-terminated. This should either be fixed, or pInput/nBytes - ** should be converted to zInput. - */ - int (*xOpen)(sqlite3_tokenizer *pTokenizer, - const char *pInput, int nBytes, - sqlite3_tokenizer_cursor **ppCursor); - int (*xClose)(sqlite3_tokenizer_cursor *pCursor); - int (*xNext)(sqlite3_tokenizer_cursor *pCursor, - const char **ppToken, int *pnBytes, - int *piStartOffset, int *piEndOffset, int *piPosition); -}; - -struct sqlite3_tokenizer { - sqlite3_tokenizer_module *pModule; /* The module for this tokenizer */ - /* Tokenizer implementations will typically add additional fields */ -}; - -struct sqlite3_tokenizer_cursor { - sqlite3_tokenizer *pTokenizer; /* Tokenizer for this cursor. */ - /* Tokenizer implementations will typically add additional fields */ -}; - -/* -** Get the module for a tokenizer which generates tokens based on a -** set of non-token characters. The default is to break tokens at any -** non-alnum character, though the set of delimiters can also be -** specified by the first argv argument to xCreate(). -*/ -/* TODO(shess) This doesn't belong here. Need some sort of -** registration process. -*/ -void get_simple_tokenizer_module(sqlite3_tokenizer_module **ppModule); - -#endif /* _TOKENIZER_H_ */ diff --git a/ext/fts2/README.tokenizers b/ext/fts2/README.tokenizers deleted file mode 100644 index 98d2021ba1..0000000000 --- a/ext/fts2/README.tokenizers +++ /dev/null @@ -1,133 +0,0 @@ - -1. FTS2 Tokenizers - - When creating a new full-text table, FTS2 allows the user to select - the text tokenizer implementation to be used when indexing text - by specifying a "tokenizer" clause as part of the CREATE VIRTUAL TABLE - statement: - - CREATE VIRTUAL TABLE USING fts2( - [, tokenizer []] - ); - - The built-in tokenizers (valid values to pass as ) are - "simple" and "porter". - - should consist of zero or more white-space separated - arguments to pass to the selected tokenizer implementation. The - interpretation of the arguments, if any, depends on the individual - tokenizer. - -2. Custom Tokenizers - - FTS2 allows users to provide custom tokenizer implementations. The - interface used to create a new tokenizer is defined and described in - the fts2_tokenizer.h source file. - - Registering a new FTS2 tokenizer is similar to registering a new - virtual table module with SQLite. The user passes a pointer to a - structure containing pointers to various callback functions that - make up the implementation of the new tokenizer type. For tokenizers, - the structure (defined in fts2_tokenizer.h) is called - "sqlite3_tokenizer_module". - - FTS2 does not expose a C-function that users call to register new - tokenizer types with a database handle. Instead, the pointer must - be encoded as an SQL blob value and passed to FTS2 through the SQL - engine by evaluating a special scalar function, "fts2_tokenizer()". - The fts2_tokenizer() function may be called with one or two arguments, - as follows: - - SELECT fts2_tokenizer(); - SELECT fts2_tokenizer(, ); - - Where is a string identifying the tokenizer and - is a pointer to an sqlite3_tokenizer_module - structure encoded as an SQL blob. If the second argument is present, - it is registered as tokenizer and a copy of it - returned. If only one argument is passed, a pointer to the tokenizer - implementation currently registered as is returned, - encoded as a blob. Or, if no such tokenizer exists, an SQL exception - (error) is raised. - - SECURITY: If the fts2 extension is used in an environment where potentially - malicious users may execute arbitrary SQL (i.e. gears), they should be - prevented from invoking the fts2_tokenizer() function, possibly using the - authorisation callback. - - See "Sample code" below for an example of calling the fts2_tokenizer() - function from C code. - -3. ICU Library Tokenizers - - If this extension is compiled with the SQLITE_ENABLE_ICU pre-processor - symbol defined, then there exists a built-in tokenizer named "icu" - implemented using the ICU library. The first argument passed to the - xCreate() method (see fts2_tokenizer.h) of this tokenizer may be - an ICU locale identifier. For example "tr_TR" for Turkish as used - in Turkey, or "en_AU" for English as used in Australia. For example: - - "CREATE VIRTUAL TABLE thai_text USING fts2(text, tokenizer icu th_TH)" - - The ICU tokenizer implementation is very simple. It splits the input - text according to the ICU rules for finding word boundaries and discards - any tokens that consist entirely of white-space. This may be suitable - for some applications in some locales, but not all. If more complex - processing is required, for example to implement stemming or - discard punctuation, this can be done by creating a tokenizer - implementation that uses the ICU tokenizer as part of its implementation. - - When using the ICU tokenizer this way, it is safe to overwrite the - contents of the strings returned by the xNext() method (see - fts2_tokenizer.h). - -4. Sample code. - - The following two code samples illustrate the way C code should invoke - the fts2_tokenizer() scalar function: - - int registerTokenizer( - sqlite3 *db, - char *zName, - const sqlite3_tokenizer_module *p - ){ - int rc; - sqlite3_stmt *pStmt; - const char zSql[] = "SELECT fts2_tokenizer(?, ?)"; - - rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); - if( rc!=SQLITE_OK ){ - return rc; - } - - sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC); - sqlite3_bind_blob(pStmt, 2, &p, sizeof(p), SQLITE_STATIC); - sqlite3_step(pStmt); - - return sqlite3_finalize(pStmt); - } - - int queryTokenizer( - sqlite3 *db, - char *zName, - const sqlite3_tokenizer_module **pp - ){ - int rc; - sqlite3_stmt *pStmt; - const char zSql[] = "SELECT fts2_tokenizer(?)"; - - *pp = 0; - rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); - if( rc!=SQLITE_OK ){ - return rc; - } - - sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC); - if( SQLITE_ROW==sqlite3_step(pStmt) ){ - if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB ){ - memcpy(pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp)); - } - } - - return sqlite3_finalize(pStmt); - } diff --git a/ext/fts2/README.txt b/ext/fts2/README.txt deleted file mode 100644 index 517a2a0434..0000000000 --- a/ext/fts2/README.txt +++ /dev/null @@ -1,4 +0,0 @@ -This folder contains source code to the second full-text search -extension for SQLite. While the API is the same, this version uses a -substantially different storage schema from fts1, so tables will need -to be rebuilt. diff --git a/ext/fts2/fts2.c b/ext/fts2/fts2.c deleted file mode 100644 index 0405fb7b1e..0000000000 --- a/ext/fts2/fts2.c +++ /dev/null @@ -1,6860 +0,0 @@ -/* fts2 has a design flaw which can lead to database corruption (see -** below). It is recommended not to use it any longer, instead use -** fts3 (or higher). If you believe that your use of fts2 is safe, -** add -DSQLITE_ENABLE_BROKEN_FTS2=1 to your CFLAGS. -*/ -#if (!defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS2)) \ - && !defined(SQLITE_ENABLE_BROKEN_FTS2) -#error fts2 has a design flaw and has been deprecated. -#endif -/* The flaw is that fts2 uses the content table's unaliased rowid as -** the unique docid. fts2 embeds the rowid in the index it builds, -** and expects the rowid to not change. The SQLite VACUUM operation -** will renumber such rowids, thereby breaking fts2. If you are using -** fts2 in a system which has disabled VACUUM, then you can continue -** to use it safely. Note that PRAGMA auto_vacuum does NOT disable -** VACUUM, though systems using auto_vacuum are unlikely to invoke -** VACUUM. -** -** Unlike fts1, which is safe across VACUUM if you never delete -** documents, fts2 has a second exposure to this flaw, in the segments -** table. So fts2 should be considered unsafe across VACUUM in all -** cases. -*/ - -/* -** 2006 Oct 10 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -****************************************************************************** -** -** This is an SQLite module implementing full-text search. -*/ - -/* -** The code in this file is only compiled if: -** -** * The FTS2 module is being built as an extension -** (in which case SQLITE_CORE is not defined), or -** -** * The FTS2 module is being built into the core of -** SQLite (in which case SQLITE_ENABLE_FTS2 is defined). -*/ - -/* TODO(shess) Consider exporting this comment to an HTML file or the -** wiki. -*/ -/* The full-text index is stored in a series of b+tree (-like) -** structures called segments which map terms to doclists. The -** structures are like b+trees in layout, but are constructed from the -** bottom up in optimal fashion and are not updatable. Since trees -** are built from the bottom up, things will be described from the -** bottom up. -** -** -**** Varints **** -** The basic unit of encoding is a variable-length integer called a -** varint. We encode variable-length integers in little-endian order -** using seven bits * per byte as follows: -** -** KEY: -** A = 0xxxxxxx 7 bits of data and one flag bit -** B = 1xxxxxxx 7 bits of data and one flag bit -** -** 7 bits - A -** 14 bits - BA -** 21 bits - BBA -** and so on. -** -** This is identical to how sqlite encodes varints (see util.c). -** -** -**** Document lists **** -** A doclist (document list) holds a docid-sorted list of hits for a -** given term. Doclists hold docids, and can optionally associate -** token positions and offsets with docids. -** -** A DL_POSITIONS_OFFSETS doclist is stored like this: -** -** array { -** varint docid; -** array { (position list for column 0) -** varint position; (delta from previous position plus POS_BASE) -** varint startOffset; (delta from previous startOffset) -** varint endOffset; (delta from startOffset) -** } -** array { -** varint POS_COLUMN; (marks start of position list for new column) -** varint column; (index of new column) -** array { -** varint position; (delta from previous position plus POS_BASE) -** varint startOffset;(delta from previous startOffset) -** varint endOffset; (delta from startOffset) -** } -** } -** varint POS_END; (marks end of positions for this document. -** } -** -** Here, array { X } means zero or more occurrences of X, adjacent in -** memory. A "position" is an index of a token in the token stream -** generated by the tokenizer, while an "offset" is a byte offset, -** both based at 0. Note that POS_END and POS_COLUMN occur in the -** same logical place as the position element, and act as sentinals -** ending a position list array. -** -** A DL_POSITIONS doclist omits the startOffset and endOffset -** information. A DL_DOCIDS doclist omits both the position and -** offset information, becoming an array of varint-encoded docids. -** -** On-disk data is stored as type DL_DEFAULT, so we don't serialize -** the type. Due to how deletion is implemented in the segmentation -** system, on-disk doclists MUST store at least positions. -** -** -**** Segment leaf nodes **** -** Segment leaf nodes store terms and doclists, ordered by term. Leaf -** nodes are written using LeafWriter, and read using LeafReader (to -** iterate through a single leaf node's data) and LeavesReader (to -** iterate through a segment's entire leaf layer). Leaf nodes have -** the format: -** -** varint iHeight; (height from leaf level, always 0) -** varint nTerm; (length of first term) -** char pTerm[nTerm]; (content of first term) -** varint nDoclist; (length of term's associated doclist) -** char pDoclist[nDoclist]; (content of doclist) -** array { -** (further terms are delta-encoded) -** varint nPrefix; (length of prefix shared with previous term) -** varint nSuffix; (length of unshared suffix) -** char pTermSuffix[nSuffix];(unshared suffix of next term) -** varint nDoclist; (length of term's associated doclist) -** char pDoclist[nDoclist]; (content of doclist) -** } -** -** Here, array { X } means zero or more occurrences of X, adjacent in -** memory. -** -** Leaf nodes are broken into blocks which are stored contiguously in -** the %_segments table in sorted order. This means that when the end -** of a node is reached, the next term is in the node with the next -** greater node id. -** -** New data is spilled to a new leaf node when the current node -** exceeds LEAF_MAX bytes (default 2048). New data which itself is -** larger than STANDALONE_MIN (default 1024) is placed in a standalone -** node (a leaf node with a single term and doclist). The goal of -** these settings is to pack together groups of small doclists while -** making it efficient to directly access large doclists. The -** assumption is that large doclists represent terms which are more -** likely to be query targets. -** -** TODO(shess) It may be useful for blocking decisions to be more -** dynamic. For instance, it may make more sense to have a 2.5k leaf -** node rather than splitting into 2k and .5k nodes. My intuition is -** that this might extend through 2x or 4x the pagesize. -** -** -**** Segment interior nodes **** -** Segment interior nodes store blockids for subtree nodes and terms -** to describe what data is stored by the each subtree. Interior -** nodes are written using InteriorWriter, and read using -** InteriorReader. InteriorWriters are created as needed when -** SegmentWriter creates new leaf nodes, or when an interior node -** itself grows too big and must be split. The format of interior -** nodes: -** -** varint iHeight; (height from leaf level, always >0) -** varint iBlockid; (block id of node's leftmost subtree) -** optional { -** varint nTerm; (length of first term) -** char pTerm[nTerm]; (content of first term) -** array { -** (further terms are delta-encoded) -** varint nPrefix; (length of shared prefix with previous term) -** varint nSuffix; (length of unshared suffix) -** char pTermSuffix[nSuffix]; (unshared suffix of next term) -** } -** } -** -** Here, optional { X } means an optional element, while array { X } -** means zero or more occurrences of X, adjacent in memory. -** -** An interior node encodes n terms separating n+1 subtrees. The -** subtree blocks are contiguous, so only the first subtree's blockid -** is encoded. The subtree at iBlockid will contain all terms less -** than the first term encoded (or all terms if no term is encoded). -** Otherwise, for terms greater than or equal to pTerm[i] but less -** than pTerm[i+1], the subtree for that term will be rooted at -** iBlockid+i. Interior nodes only store enough term data to -** distinguish adjacent children (if the rightmost term of the left -** child is "something", and the leftmost term of the right child is -** "wicked", only "w" is stored). -** -** New data is spilled to a new interior node at the same height when -** the current node exceeds INTERIOR_MAX bytes (default 2048). -** INTERIOR_MIN_TERMS (default 7) keeps large terms from monopolizing -** interior nodes and making the tree too skinny. The interior nodes -** at a given height are naturally tracked by interior nodes at -** height+1, and so on. -** -** -**** Segment directory **** -** The segment directory in table %_segdir stores meta-information for -** merging and deleting segments, and also the root node of the -** segment's tree. -** -** The root node is the top node of the segment's tree after encoding -** the entire segment, restricted to ROOT_MAX bytes (default 1024). -** This could be either a leaf node or an interior node. If the top -** node requires more than ROOT_MAX bytes, it is flushed to %_segments -** and a new root interior node is generated (which should always fit -** within ROOT_MAX because it only needs space for 2 varints, the -** height and the blockid of the previous root). -** -** The meta-information in the segment directory is: -** level - segment level (see below) -** idx - index within level -** - (level,idx uniquely identify a segment) -** start_block - first leaf node -** leaves_end_block - last leaf node -** end_block - last block (including interior nodes) -** root - contents of root node -** -** If the root node is a leaf node, then start_block, -** leaves_end_block, and end_block are all 0. -** -** -**** Segment merging **** -** To amortize update costs, segments are groups into levels and -** merged in matches. Each increase in level represents exponentially -** more documents. -** -** New documents (actually, document updates) are tokenized and -** written individually (using LeafWriter) to a level 0 segment, with -** incrementing idx. When idx reaches MERGE_COUNT (default 16), all -** level 0 segments are merged into a single level 1 segment. Level 1 -** is populated like level 0, and eventually MERGE_COUNT level 1 -** segments are merged to a single level 2 segment (representing -** MERGE_COUNT^2 updates), and so on. -** -** A segment merge traverses all segments at a given level in -** parallel, performing a straightforward sorted merge. Since segment -** leaf nodes are written in to the %_segments table in order, this -** merge traverses the underlying sqlite disk structures efficiently. -** After the merge, all segment blocks from the merged level are -** deleted. -** -** MERGE_COUNT controls how often we merge segments. 16 seems to be -** somewhat of a sweet spot for insertion performance. 32 and 64 show -** very similar performance numbers to 16 on insertion, though they're -** a tiny bit slower (perhaps due to more overhead in merge-time -** sorting). 8 is about 20% slower than 16, 4 about 50% slower than -** 16, 2 about 66% slower than 16. -** -** At query time, high MERGE_COUNT increases the number of segments -** which need to be scanned and merged. For instance, with 100k docs -** inserted: -** -** MERGE_COUNT segments -** 16 25 -** 8 12 -** 4 10 -** 2 6 -** -** This appears to have only a moderate impact on queries for very -** frequent terms (which are somewhat dominated by segment merge -** costs), and infrequent and non-existent terms still seem to be fast -** even with many segments. -** -** TODO(shess) That said, it would be nice to have a better query-side -** argument for MERGE_COUNT of 16. Also, it is possible/likely that -** optimizations to things like doclist merging will swing the sweet -** spot around. -** -** -** -**** Handling of deletions and updates **** -** Since we're using a segmented structure, with no docid-oriented -** index into the term index, we clearly cannot simply update the term -** index when a document is deleted or updated. For deletions, we -** write an empty doclist (varint(docid) varint(POS_END)), for updates -** we simply write the new doclist. Segment merges overwrite older -** data for a particular docid with newer data, so deletes or updates -** will eventually overtake the earlier data and knock it out. The -** query logic likewise merges doclists so that newer data knocks out -** older data. -** -** TODO(shess) Provide a VACUUM type operation to clear out all -** deletions and duplications. This would basically be a forced merge -** into a single segment. -*/ - -#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS2) - -#if defined(SQLITE_ENABLE_FTS2) && !defined(SQLITE_CORE) -# define SQLITE_CORE 1 -#endif - -#include -#include -#include -#include -#include "fts2.h" -#include "fts2_hash.h" -#include "fts2_tokenizer.h" -#include "sqlite3.h" -#include "sqlite3ext.h" -SQLITE_EXTENSION_INIT1 - - -/* TODO(shess) MAN, this thing needs some refactoring. At minimum, it -** would be nice to order the file better, perhaps something along the -** lines of: -** -** - utility functions -** - table setup functions -** - table update functions -** - table query functions -** -** Put the query functions last because they're likely to reference -** typedefs or functions from the table update section. -*/ - -#if 0 -# define TRACE(A) printf A; fflush(stdout) -#else -# define TRACE(A) -#endif - -/* It is not safe to call isspace(), tolower(), or isalnum() on -** hi-bit-set characters. This is the same solution used in the -** tokenizer. -*/ -/* TODO(shess) The snippet-generation code should be using the -** tokenizer-generated tokens rather than doing its own local -** tokenization. -*/ -/* TODO(shess) Is __isascii() a portable version of (c&0x80)==0? */ -static int safe_isspace(char c){ - return c==' ' || c=='\t' || c=='\n' || c=='\r' || c=='\v' || c=='\f'; -} -static int safe_tolower(char c){ - return (c>='A' && c<='Z') ? (c - 'A' + 'a') : c; -} -static int safe_isalnum(char c){ - return (c>='0' && c<='9') || (c>='A' && c<='Z') || (c>='a' && c<='z'); -} - -typedef enum DocListType { - DL_DOCIDS, /* docids only */ - DL_POSITIONS, /* docids + positions */ - DL_POSITIONS_OFFSETS /* docids + positions + offsets */ -} DocListType; - -/* -** By default, only positions and not offsets are stored in the doclists. -** To change this so that offsets are stored too, compile with -** -** -DDL_DEFAULT=DL_POSITIONS_OFFSETS -** -** If DL_DEFAULT is set to DL_DOCIDS, your table can only be inserted -** into (no deletes or updates). -*/ -#ifndef DL_DEFAULT -# define DL_DEFAULT DL_POSITIONS -#endif - -enum { - POS_END = 0, /* end of this position list */ - POS_COLUMN, /* followed by new column number */ - POS_BASE -}; - -/* MERGE_COUNT controls how often we merge segments (see comment at -** top of file). -*/ -#define MERGE_COUNT 16 - -/* utility functions */ - -/* CLEAR() and SCRAMBLE() abstract memset() on a pointer to a single -** record to prevent errors of the form: -** -** my_function(SomeType *b){ -** memset(b, '\0', sizeof(b)); // sizeof(b)!=sizeof(*b) -** } -*/ -/* TODO(shess) Obvious candidates for a header file. */ -#define CLEAR(b) memset(b, '\0', sizeof(*(b))) - -#ifndef NDEBUG -# define SCRAMBLE(b) memset(b, 0x55, sizeof(*(b))) -#else -# define SCRAMBLE(b) -#endif - -/* We may need up to VARINT_MAX bytes to store an encoded 64-bit integer. */ -#define VARINT_MAX 10 - -/* Write a 64-bit variable-length integer to memory starting at p[0]. - * The length of data written will be between 1 and VARINT_MAX bytes. - * The number of bytes written is returned. */ -static int putVarint(char *p, sqlite_int64 v){ - unsigned char *q = (unsigned char *) p; - sqlite_uint64 vu = v; - do{ - *q++ = (unsigned char) ((vu & 0x7f) | 0x80); - vu >>= 7; - }while( vu!=0 ); - q[-1] &= 0x7f; /* turn off high bit in final byte */ - assert( q - (unsigned char *)p <= VARINT_MAX ); - return (int) (q - (unsigned char *)p); -} - -/* Read a 64-bit variable-length integer from memory starting at p[0]. - * Return the number of bytes read, or 0 on error. - * The value is stored in *v. */ -static int getVarint(const char *p, sqlite_int64 *v){ - const unsigned char *q = (const unsigned char *) p; - sqlite_uint64 x = 0, y = 1; - while( (*q & 0x80) == 0x80 ){ - x += y * (*q++ & 0x7f); - y <<= 7; - if( q - (unsigned char *)p >= VARINT_MAX ){ /* bad data */ - assert( 0 ); - return 0; - } - } - x += y * (*q++); - *v = (sqlite_int64) x; - return (int) (q - (unsigned char *)p); -} - -static int getVarint32(const char *p, int *pi){ - sqlite_int64 i; - int ret = getVarint(p, &i); - *pi = (int) i; - assert( *pi==i ); - return ret; -} - -/*******************************************************************/ -/* DataBuffer is used to collect data into a buffer in piecemeal -** fashion. It implements the usual distinction between amount of -** data currently stored (nData) and buffer capacity (nCapacity). -** -** dataBufferInit - create a buffer with given initial capacity. -** dataBufferReset - forget buffer's data, retaining capacity. -** dataBufferDestroy - free buffer's data. -** dataBufferSwap - swap contents of two buffers. -** dataBufferExpand - expand capacity without adding data. -** dataBufferAppend - append data. -** dataBufferAppend2 - append two pieces of data at once. -** dataBufferReplace - replace buffer's data. -*/ -typedef struct DataBuffer { - char *pData; /* Pointer to malloc'ed buffer. */ - int nCapacity; /* Size of pData buffer. */ - int nData; /* End of data loaded into pData. */ -} DataBuffer; - -static void dataBufferInit(DataBuffer *pBuffer, int nCapacity){ - assert( nCapacity>=0 ); - pBuffer->nData = 0; - pBuffer->nCapacity = nCapacity; - pBuffer->pData = nCapacity==0 ? NULL : sqlite3_malloc(nCapacity); -} -static void dataBufferReset(DataBuffer *pBuffer){ - pBuffer->nData = 0; -} -static void dataBufferDestroy(DataBuffer *pBuffer){ - if( pBuffer->pData!=NULL ) sqlite3_free(pBuffer->pData); - SCRAMBLE(pBuffer); -} -static void dataBufferSwap(DataBuffer *pBuffer1, DataBuffer *pBuffer2){ - DataBuffer tmp = *pBuffer1; - *pBuffer1 = *pBuffer2; - *pBuffer2 = tmp; -} -static void dataBufferExpand(DataBuffer *pBuffer, int nAddCapacity){ - assert( nAddCapacity>0 ); - /* TODO(shess) Consider expanding more aggressively. Note that the - ** underlying malloc implementation may take care of such things for - ** us already. - */ - if( pBuffer->nData+nAddCapacity>pBuffer->nCapacity ){ - pBuffer->nCapacity = pBuffer->nData+nAddCapacity; - pBuffer->pData = sqlite3_realloc(pBuffer->pData, pBuffer->nCapacity); - } -} -static void dataBufferAppend(DataBuffer *pBuffer, - const char *pSource, int nSource){ - assert( nSource>0 && pSource!=NULL ); - dataBufferExpand(pBuffer, nSource); - memcpy(pBuffer->pData+pBuffer->nData, pSource, nSource); - pBuffer->nData += nSource; -} -static void dataBufferAppend2(DataBuffer *pBuffer, - const char *pSource1, int nSource1, - const char *pSource2, int nSource2){ - assert( nSource1>0 && pSource1!=NULL ); - assert( nSource2>0 && pSource2!=NULL ); - dataBufferExpand(pBuffer, nSource1+nSource2); - memcpy(pBuffer->pData+pBuffer->nData, pSource1, nSource1); - memcpy(pBuffer->pData+pBuffer->nData+nSource1, pSource2, nSource2); - pBuffer->nData += nSource1+nSource2; -} -static void dataBufferReplace(DataBuffer *pBuffer, - const char *pSource, int nSource){ - dataBufferReset(pBuffer); - dataBufferAppend(pBuffer, pSource, nSource); -} - -/* StringBuffer is a null-terminated version of DataBuffer. */ -typedef struct StringBuffer { - DataBuffer b; /* Includes null terminator. */ -} StringBuffer; - -static void initStringBuffer(StringBuffer *sb){ - dataBufferInit(&sb->b, 100); - dataBufferReplace(&sb->b, "", 1); -} -static int stringBufferLength(StringBuffer *sb){ - return sb->b.nData-1; -} -static char *stringBufferData(StringBuffer *sb){ - return sb->b.pData; -} -static void stringBufferDestroy(StringBuffer *sb){ - dataBufferDestroy(&sb->b); -} - -static void nappend(StringBuffer *sb, const char *zFrom, int nFrom){ - assert( sb->b.nData>0 ); - if( nFrom>0 ){ - sb->b.nData--; - dataBufferAppend2(&sb->b, zFrom, nFrom, "", 1); - } -} -static void append(StringBuffer *sb, const char *zFrom){ - nappend(sb, zFrom, strlen(zFrom)); -} - -/* Append a list of strings separated by commas. */ -static void appendList(StringBuffer *sb, int nString, char **azString){ - int i; - for(i=0; i0 ) append(sb, ", "); - append(sb, azString[i]); - } -} - -static int endsInWhiteSpace(StringBuffer *p){ - return stringBufferLength(p)>0 && - safe_isspace(stringBufferData(p)[stringBufferLength(p)-1]); -} - -/* If the StringBuffer ends in something other than white space, add a -** single space character to the end. -*/ -static void appendWhiteSpace(StringBuffer *p){ - if( stringBufferLength(p)==0 ) return; - if( !endsInWhiteSpace(p) ) append(p, " "); -} - -/* Remove white space from the end of the StringBuffer */ -static void trimWhiteSpace(StringBuffer *p){ - while( endsInWhiteSpace(p) ){ - p->b.pData[--p->b.nData-1] = '\0'; - } -} - -/*******************************************************************/ -/* DLReader is used to read document elements from a doclist. The -** current docid is cached, so dlrDocid() is fast. DLReader does not -** own the doclist buffer. -** -** dlrAtEnd - true if there's no more data to read. -** dlrDocid - docid of current document. -** dlrDocData - doclist data for current document (including docid). -** dlrDocDataBytes - length of same. -** dlrAllDataBytes - length of all remaining data. -** dlrPosData - position data for current document. -** dlrPosDataLen - length of pos data for current document (incl POS_END). -** dlrStep - step to current document. -** dlrInit - initial for doclist of given type against given data. -** dlrDestroy - clean up. -** -** Expected usage is something like: -** -** DLReader reader; -** dlrInit(&reader, pData, nData); -** while( !dlrAtEnd(&reader) ){ -** // calls to dlrDocid() and kin. -** dlrStep(&reader); -** } -** dlrDestroy(&reader); -*/ -typedef struct DLReader { - DocListType iType; - const char *pData; - int nData; - - sqlite_int64 iDocid; - int nElement; -} DLReader; - -static int dlrAtEnd(DLReader *pReader){ - assert( pReader->nData>=0 ); - return pReader->nData==0; -} -static sqlite_int64 dlrDocid(DLReader *pReader){ - assert( !dlrAtEnd(pReader) ); - return pReader->iDocid; -} -static const char *dlrDocData(DLReader *pReader){ - assert( !dlrAtEnd(pReader) ); - return pReader->pData; -} -static int dlrDocDataBytes(DLReader *pReader){ - assert( !dlrAtEnd(pReader) ); - return pReader->nElement; -} -static int dlrAllDataBytes(DLReader *pReader){ - assert( !dlrAtEnd(pReader) ); - return pReader->nData; -} -/* TODO(shess) Consider adding a field to track iDocid varint length -** to make these two functions faster. This might matter (a tiny bit) -** for queries. -*/ -static const char *dlrPosData(DLReader *pReader){ - sqlite_int64 iDummy; - int n = getVarint(pReader->pData, &iDummy); - assert( !dlrAtEnd(pReader) ); - return pReader->pData+n; -} -static int dlrPosDataLen(DLReader *pReader){ - sqlite_int64 iDummy; - int n = getVarint(pReader->pData, &iDummy); - assert( !dlrAtEnd(pReader) ); - return pReader->nElement-n; -} -static void dlrStep(DLReader *pReader){ - assert( !dlrAtEnd(pReader) ); - - /* Skip past current doclist element. */ - assert( pReader->nElement<=pReader->nData ); - pReader->pData += pReader->nElement; - pReader->nData -= pReader->nElement; - - /* If there is more data, read the next doclist element. */ - if( pReader->nData!=0 ){ - sqlite_int64 iDocidDelta; - int iDummy, n = getVarint(pReader->pData, &iDocidDelta); - pReader->iDocid += iDocidDelta; - if( pReader->iType>=DL_POSITIONS ){ - assert( nnData ); - while( 1 ){ - n += getVarint32(pReader->pData+n, &iDummy); - assert( n<=pReader->nData ); - if( iDummy==POS_END ) break; - if( iDummy==POS_COLUMN ){ - n += getVarint32(pReader->pData+n, &iDummy); - assert( nnData ); - }else if( pReader->iType==DL_POSITIONS_OFFSETS ){ - n += getVarint32(pReader->pData+n, &iDummy); - n += getVarint32(pReader->pData+n, &iDummy); - assert( nnData ); - } - } - } - pReader->nElement = n; - assert( pReader->nElement<=pReader->nData ); - } -} -static void dlrInit(DLReader *pReader, DocListType iType, - const char *pData, int nData){ - assert( pData!=NULL && nData!=0 ); - pReader->iType = iType; - pReader->pData = pData; - pReader->nData = nData; - pReader->nElement = 0; - pReader->iDocid = 0; - - /* Load the first element's data. There must be a first element. */ - dlrStep(pReader); -} -static void dlrDestroy(DLReader *pReader){ - SCRAMBLE(pReader); -} - -#ifndef NDEBUG -/* Verify that the doclist can be validly decoded. Also returns the -** last docid found because it is convenient in other assertions for -** DLWriter. -*/ -static void docListValidate(DocListType iType, const char *pData, int nData, - sqlite_int64 *pLastDocid){ - sqlite_int64 iPrevDocid = 0; - assert( nData>0 ); - assert( pData!=0 ); - assert( pData+nData>pData ); - while( nData!=0 ){ - sqlite_int64 iDocidDelta; - int n = getVarint(pData, &iDocidDelta); - iPrevDocid += iDocidDelta; - if( iType>DL_DOCIDS ){ - int iDummy; - while( 1 ){ - n += getVarint32(pData+n, &iDummy); - if( iDummy==POS_END ) break; - if( iDummy==POS_COLUMN ){ - n += getVarint32(pData+n, &iDummy); - }else if( iType>DL_POSITIONS ){ - n += getVarint32(pData+n, &iDummy); - n += getVarint32(pData+n, &iDummy); - } - assert( n<=nData ); - } - } - assert( n<=nData ); - pData += n; - nData -= n; - } - if( pLastDocid ) *pLastDocid = iPrevDocid; -} -#define ASSERT_VALID_DOCLIST(i, p, n, o) docListValidate(i, p, n, o) -#else -#define ASSERT_VALID_DOCLIST(i, p, n, o) assert( 1 ) -#endif - -/*******************************************************************/ -/* DLWriter is used to write doclist data to a DataBuffer. DLWriter -** always appends to the buffer and does not own it. -** -** dlwInit - initialize to write a given type doclistto a buffer. -** dlwDestroy - clear the writer's memory. Does not free buffer. -** dlwAppend - append raw doclist data to buffer. -** dlwCopy - copy next doclist from reader to writer. -** dlwAdd - construct doclist element and append to buffer. -** Only apply dlwAdd() to DL_DOCIDS doclists (else use PLWriter). -*/ -typedef struct DLWriter { - DocListType iType; - DataBuffer *b; - sqlite_int64 iPrevDocid; -#ifndef NDEBUG - int has_iPrevDocid; -#endif -} DLWriter; - -static void dlwInit(DLWriter *pWriter, DocListType iType, DataBuffer *b){ - pWriter->b = b; - pWriter->iType = iType; - pWriter->iPrevDocid = 0; -#ifndef NDEBUG - pWriter->has_iPrevDocid = 0; -#endif -} -static void dlwDestroy(DLWriter *pWriter){ - SCRAMBLE(pWriter); -} -/* iFirstDocid is the first docid in the doclist in pData. It is -** needed because pData may point within a larger doclist, in which -** case the first item would be delta-encoded. -** -** iLastDocid is the final docid in the doclist in pData. It is -** needed to create the new iPrevDocid for future delta-encoding. The -** code could decode the passed doclist to recreate iLastDocid, but -** the only current user (docListMerge) already has decoded this -** information. -*/ -/* TODO(shess) This has become just a helper for docListMerge. -** Consider a refactor to make this cleaner. -*/ -static void dlwAppend(DLWriter *pWriter, - const char *pData, int nData, - sqlite_int64 iFirstDocid, sqlite_int64 iLastDocid){ - sqlite_int64 iDocid = 0; - char c[VARINT_MAX]; - int nFirstOld, nFirstNew; /* Old and new varint len of first docid. */ -#ifndef NDEBUG - sqlite_int64 iLastDocidDelta; -#endif - - /* Recode the initial docid as delta from iPrevDocid. */ - nFirstOld = getVarint(pData, &iDocid); - assert( nFirstOldiType==DL_DOCIDS) ); - nFirstNew = putVarint(c, iFirstDocid-pWriter->iPrevDocid); - - /* Verify that the incoming doclist is valid AND that it ends with - ** the expected docid. This is essential because we'll trust this - ** docid in future delta-encoding. - */ - ASSERT_VALID_DOCLIST(pWriter->iType, pData, nData, &iLastDocidDelta); - assert( iLastDocid==iFirstDocid-iDocid+iLastDocidDelta ); - - /* Append recoded initial docid and everything else. Rest of docids - ** should have been delta-encoded from previous initial docid. - */ - if( nFirstOldb, c, nFirstNew, - pData+nFirstOld, nData-nFirstOld); - }else{ - dataBufferAppend(pWriter->b, c, nFirstNew); - } - pWriter->iPrevDocid = iLastDocid; -} -static void dlwCopy(DLWriter *pWriter, DLReader *pReader){ - dlwAppend(pWriter, dlrDocData(pReader), dlrDocDataBytes(pReader), - dlrDocid(pReader), dlrDocid(pReader)); -} -static void dlwAdd(DLWriter *pWriter, sqlite_int64 iDocid){ - char c[VARINT_MAX]; - int n = putVarint(c, iDocid-pWriter->iPrevDocid); - - /* Docids must ascend. */ - assert( !pWriter->has_iPrevDocid || iDocid>pWriter->iPrevDocid ); - assert( pWriter->iType==DL_DOCIDS ); - - dataBufferAppend(pWriter->b, c, n); - pWriter->iPrevDocid = iDocid; -#ifndef NDEBUG - pWriter->has_iPrevDocid = 1; -#endif -} - -/*******************************************************************/ -/* PLReader is used to read data from a document's position list. As -** the caller steps through the list, data is cached so that varints -** only need to be decoded once. -** -** plrInit, plrDestroy - create/destroy a reader. -** plrColumn, plrPosition, plrStartOffset, plrEndOffset - accessors -** plrAtEnd - at end of stream, only call plrDestroy once true. -** plrStep - step to the next element. -*/ -typedef struct PLReader { - /* These refer to the next position's data. nData will reach 0 when - ** reading the last position, so plrStep() signals EOF by setting - ** pData to NULL. - */ - const char *pData; - int nData; - - DocListType iType; - int iColumn; /* the last column read */ - int iPosition; /* the last position read */ - int iStartOffset; /* the last start offset read */ - int iEndOffset; /* the last end offset read */ -} PLReader; - -static int plrAtEnd(PLReader *pReader){ - return pReader->pData==NULL; -} -static int plrColumn(PLReader *pReader){ - assert( !plrAtEnd(pReader) ); - return pReader->iColumn; -} -static int plrPosition(PLReader *pReader){ - assert( !plrAtEnd(pReader) ); - return pReader->iPosition; -} -static int plrStartOffset(PLReader *pReader){ - assert( !plrAtEnd(pReader) ); - return pReader->iStartOffset; -} -static int plrEndOffset(PLReader *pReader){ - assert( !plrAtEnd(pReader) ); - return pReader->iEndOffset; -} -static void plrStep(PLReader *pReader){ - int i, n; - - assert( !plrAtEnd(pReader) ); - - if( pReader->nData==0 ){ - pReader->pData = NULL; - return; - } - - n = getVarint32(pReader->pData, &i); - if( i==POS_COLUMN ){ - n += getVarint32(pReader->pData+n, &pReader->iColumn); - pReader->iPosition = 0; - pReader->iStartOffset = 0; - n += getVarint32(pReader->pData+n, &i); - } - /* Should never see adjacent column changes. */ - assert( i!=POS_COLUMN ); - - if( i==POS_END ){ - pReader->nData = 0; - pReader->pData = NULL; - return; - } - - pReader->iPosition += i-POS_BASE; - if( pReader->iType==DL_POSITIONS_OFFSETS ){ - n += getVarint32(pReader->pData+n, &i); - pReader->iStartOffset += i; - n += getVarint32(pReader->pData+n, &i); - pReader->iEndOffset = pReader->iStartOffset+i; - } - assert( n<=pReader->nData ); - pReader->pData += n; - pReader->nData -= n; -} - -static void plrInit(PLReader *pReader, DLReader *pDLReader){ - pReader->pData = dlrPosData(pDLReader); - pReader->nData = dlrPosDataLen(pDLReader); - pReader->iType = pDLReader->iType; - pReader->iColumn = 0; - pReader->iPosition = 0; - pReader->iStartOffset = 0; - pReader->iEndOffset = 0; - plrStep(pReader); -} -static void plrDestroy(PLReader *pReader){ - SCRAMBLE(pReader); -} - -/*******************************************************************/ -/* PLWriter is used in constructing a document's position list. As a -** convenience, if iType is DL_DOCIDS, PLWriter becomes a no-op. -** PLWriter writes to the associated DLWriter's buffer. -** -** plwInit - init for writing a document's poslist. -** plwDestroy - clear a writer. -** plwAdd - append position and offset information. -** plwCopy - copy next position's data from reader to writer. -** plwTerminate - add any necessary doclist terminator. -** -** Calling plwAdd() after plwTerminate() may result in a corrupt -** doclist. -*/ -/* TODO(shess) Until we've written the second item, we can cache the -** first item's information. Then we'd have three states: -** -** - initialized with docid, no positions. -** - docid and one position. -** - docid and multiple positions. -** -** Only the last state needs to actually write to dlw->b, which would -** be an improvement in the DLCollector case. -*/ -typedef struct PLWriter { - DLWriter *dlw; - - int iColumn; /* the last column written */ - int iPos; /* the last position written */ - int iOffset; /* the last start offset written */ -} PLWriter; - -/* TODO(shess) In the case where the parent is reading these values -** from a PLReader, we could optimize to a copy if that PLReader has -** the same type as pWriter. -*/ -static void plwAdd(PLWriter *pWriter, int iColumn, int iPos, - int iStartOffset, int iEndOffset){ - /* Worst-case space for POS_COLUMN, iColumn, iPosDelta, - ** iStartOffsetDelta, and iEndOffsetDelta. - */ - char c[5*VARINT_MAX]; - int n = 0; - - /* Ban plwAdd() after plwTerminate(). */ - assert( pWriter->iPos!=-1 ); - - if( pWriter->dlw->iType==DL_DOCIDS ) return; - - if( iColumn!=pWriter->iColumn ){ - n += putVarint(c+n, POS_COLUMN); - n += putVarint(c+n, iColumn); - pWriter->iColumn = iColumn; - pWriter->iPos = 0; - pWriter->iOffset = 0; - } - assert( iPos>=pWriter->iPos ); - n += putVarint(c+n, POS_BASE+(iPos-pWriter->iPos)); - pWriter->iPos = iPos; - if( pWriter->dlw->iType==DL_POSITIONS_OFFSETS ){ - assert( iStartOffset>=pWriter->iOffset ); - n += putVarint(c+n, iStartOffset-pWriter->iOffset); - pWriter->iOffset = iStartOffset; - assert( iEndOffset>=iStartOffset ); - n += putVarint(c+n, iEndOffset-iStartOffset); - } - dataBufferAppend(pWriter->dlw->b, c, n); -} -static void plwCopy(PLWriter *pWriter, PLReader *pReader){ - plwAdd(pWriter, plrColumn(pReader), plrPosition(pReader), - plrStartOffset(pReader), plrEndOffset(pReader)); -} -static void plwInit(PLWriter *pWriter, DLWriter *dlw, sqlite_int64 iDocid){ - char c[VARINT_MAX]; - int n; - - pWriter->dlw = dlw; - - /* Docids must ascend. */ - assert( !pWriter->dlw->has_iPrevDocid || iDocid>pWriter->dlw->iPrevDocid ); - n = putVarint(c, iDocid-pWriter->dlw->iPrevDocid); - dataBufferAppend(pWriter->dlw->b, c, n); - pWriter->dlw->iPrevDocid = iDocid; -#ifndef NDEBUG - pWriter->dlw->has_iPrevDocid = 1; -#endif - - pWriter->iColumn = 0; - pWriter->iPos = 0; - pWriter->iOffset = 0; -} -/* TODO(shess) Should plwDestroy() also terminate the doclist? But -** then plwDestroy() would no longer be just a destructor, it would -** also be doing work, which isn't consistent with the overall idiom. -** Another option would be for plwAdd() to always append any necessary -** terminator, so that the output is always correct. But that would -** add incremental work to the common case with the only benefit being -** API elegance. Punt for now. -*/ -static void plwTerminate(PLWriter *pWriter){ - if( pWriter->dlw->iType>DL_DOCIDS ){ - char c[VARINT_MAX]; - int n = putVarint(c, POS_END); - dataBufferAppend(pWriter->dlw->b, c, n); - } -#ifndef NDEBUG - /* Mark as terminated for assert in plwAdd(). */ - pWriter->iPos = -1; -#endif -} -static void plwDestroy(PLWriter *pWriter){ - SCRAMBLE(pWriter); -} - -/*******************************************************************/ -/* DLCollector wraps PLWriter and DLWriter to provide a -** dynamically-allocated doclist area to use during tokenization. -** -** dlcNew - malloc up and initialize a collector. -** dlcDelete - destroy a collector and all contained items. -** dlcAddPos - append position and offset information. -** dlcAddDoclist - add the collected doclist to the given buffer. -** dlcNext - terminate the current document and open another. -*/ -typedef struct DLCollector { - DataBuffer b; - DLWriter dlw; - PLWriter plw; -} DLCollector; - -/* TODO(shess) This could also be done by calling plwTerminate() and -** dataBufferAppend(). I tried that, expecting nominal performance -** differences, but it seemed to pretty reliably be worth 1% to code -** it this way. I suspect it is the incremental malloc overhead (some -** percentage of the plwTerminate() calls will cause a realloc), so -** this might be worth revisiting if the DataBuffer implementation -** changes. -*/ -static void dlcAddDoclist(DLCollector *pCollector, DataBuffer *b){ - if( pCollector->dlw.iType>DL_DOCIDS ){ - char c[VARINT_MAX]; - int n = putVarint(c, POS_END); - dataBufferAppend2(b, pCollector->b.pData, pCollector->b.nData, c, n); - }else{ - dataBufferAppend(b, pCollector->b.pData, pCollector->b.nData); - } -} -static void dlcNext(DLCollector *pCollector, sqlite_int64 iDocid){ - plwTerminate(&pCollector->plw); - plwDestroy(&pCollector->plw); - plwInit(&pCollector->plw, &pCollector->dlw, iDocid); -} -static void dlcAddPos(DLCollector *pCollector, int iColumn, int iPos, - int iStartOffset, int iEndOffset){ - plwAdd(&pCollector->plw, iColumn, iPos, iStartOffset, iEndOffset); -} - -static DLCollector *dlcNew(sqlite_int64 iDocid, DocListType iType){ - DLCollector *pCollector = sqlite3_malloc(sizeof(DLCollector)); - dataBufferInit(&pCollector->b, 0); - dlwInit(&pCollector->dlw, iType, &pCollector->b); - plwInit(&pCollector->plw, &pCollector->dlw, iDocid); - return pCollector; -} -static void dlcDelete(DLCollector *pCollector){ - plwDestroy(&pCollector->plw); - dlwDestroy(&pCollector->dlw); - dataBufferDestroy(&pCollector->b); - SCRAMBLE(pCollector); - sqlite3_free(pCollector); -} - - -/* Copy the doclist data of iType in pData/nData into *out, trimming -** unnecessary data as we go. Only columns matching iColumn are -** copied, all columns copied if iColumn is -1. Elements with no -** matching columns are dropped. The output is an iOutType doclist. -*/ -/* NOTE(shess) This code is only valid after all doclists are merged. -** If this is run before merges, then doclist items which represent -** deletion will be trimmed, and will thus not effect a deletion -** during the merge. -*/ -static void docListTrim(DocListType iType, const char *pData, int nData, - int iColumn, DocListType iOutType, DataBuffer *out){ - DLReader dlReader; - DLWriter dlWriter; - - assert( iOutType<=iType ); - - dlrInit(&dlReader, iType, pData, nData); - dlwInit(&dlWriter, iOutType, out); - - while( !dlrAtEnd(&dlReader) ){ - PLReader plReader; - PLWriter plWriter; - int match = 0; - - plrInit(&plReader, &dlReader); - - while( !plrAtEnd(&plReader) ){ - if( iColumn==-1 || plrColumn(&plReader)==iColumn ){ - if( !match ){ - plwInit(&plWriter, &dlWriter, dlrDocid(&dlReader)); - match = 1; - } - plwAdd(&plWriter, plrColumn(&plReader), plrPosition(&plReader), - plrStartOffset(&plReader), plrEndOffset(&plReader)); - } - plrStep(&plReader); - } - if( match ){ - plwTerminate(&plWriter); - plwDestroy(&plWriter); - } - - plrDestroy(&plReader); - dlrStep(&dlReader); - } - dlwDestroy(&dlWriter); - dlrDestroy(&dlReader); -} - -/* Used by docListMerge() to keep doclists in the ascending order by -** docid, then ascending order by age (so the newest comes first). -*/ -typedef struct OrderedDLReader { - DLReader *pReader; - - /* TODO(shess) If we assume that docListMerge pReaders is ordered by - ** age (which we do), then we could use pReader comparisons to break - ** ties. - */ - int idx; -} OrderedDLReader; - -/* Order eof to end, then by docid asc, idx desc. */ -static int orderedDLReaderCmp(OrderedDLReader *r1, OrderedDLReader *r2){ - if( dlrAtEnd(r1->pReader) ){ - if( dlrAtEnd(r2->pReader) ) return 0; /* Both atEnd(). */ - return 1; /* Only r1 atEnd(). */ - } - if( dlrAtEnd(r2->pReader) ) return -1; /* Only r2 atEnd(). */ - - if( dlrDocid(r1->pReader)pReader) ) return -1; - if( dlrDocid(r1->pReader)>dlrDocid(r2->pReader) ) return 1; - - /* Descending on idx. */ - return r2->idx-r1->idx; -} - -/* Bubble p[0] to appropriate place in p[1..n-1]. Assumes that -** p[1..n-1] is already sorted. -*/ -/* TODO(shess) Is this frequent enough to warrant a binary search? -** Before implementing that, instrument the code to check. In most -** current usage, I expect that p[0] will be less than p[1] a very -** high proportion of the time. -*/ -static void orderedDLReaderReorder(OrderedDLReader *p, int n){ - while( n>1 && orderedDLReaderCmp(p, p+1)>0 ){ - OrderedDLReader tmp = p[0]; - p[0] = p[1]; - p[1] = tmp; - n--; - p++; - } -} - -/* Given an array of doclist readers, merge their doclist elements -** into out in sorted order (by docid), dropping elements from older -** readers when there is a duplicate docid. pReaders is assumed to be -** ordered by age, oldest first. -*/ -/* TODO(shess) nReaders must be <= MERGE_COUNT. This should probably -** be fixed. -*/ -static void docListMerge(DataBuffer *out, - DLReader *pReaders, int nReaders){ - OrderedDLReader readers[MERGE_COUNT]; - DLWriter writer; - int i, n; - const char *pStart = 0; - int nStart = 0; - sqlite_int64 iFirstDocid = 0, iLastDocid = 0; - - assert( nReaders>0 ); - if( nReaders==1 ){ - dataBufferAppend(out, dlrDocData(pReaders), dlrAllDataBytes(pReaders)); - return; - } - - assert( nReaders<=MERGE_COUNT ); - n = 0; - for(i=0; i0 ){ - orderedDLReaderReorder(readers+i, nReaders-i); - } - - dlwInit(&writer, pReaders[0].iType, out); - while( !dlrAtEnd(readers[0].pReader) ){ - sqlite_int64 iDocid = dlrDocid(readers[0].pReader); - - /* If this is a continuation of the current buffer to copy, extend - ** that buffer. memcpy() seems to be more efficient if it has a - ** lots of data to copy. - */ - if( dlrDocData(readers[0].pReader)==pStart+nStart ){ - nStart += dlrDocDataBytes(readers[0].pReader); - }else{ - if( pStart!=0 ){ - dlwAppend(&writer, pStart, nStart, iFirstDocid, iLastDocid); - } - pStart = dlrDocData(readers[0].pReader); - nStart = dlrDocDataBytes(readers[0].pReader); - iFirstDocid = iDocid; - } - iLastDocid = iDocid; - dlrStep(readers[0].pReader); - - /* Drop all of the older elements with the same docid. */ - for(i=1; i0 ){ - orderedDLReaderReorder(readers+i, nReaders-i); - } - } - - /* Copy over any remaining elements. */ - if( nStart>0 ) dlwAppend(&writer, pStart, nStart, iFirstDocid, iLastDocid); - dlwDestroy(&writer); -} - -/* Helper function for posListUnion(). Compares the current position -** between left and right, returning as standard C idiom of <0 if -** left0 if left>right, and 0 if left==right. "End" always -** compares greater. -*/ -static int posListCmp(PLReader *pLeft, PLReader *pRight){ - assert( pLeft->iType==pRight->iType ); - if( pLeft->iType==DL_DOCIDS ) return 0; - - if( plrAtEnd(pLeft) ) return plrAtEnd(pRight) ? 0 : 1; - if( plrAtEnd(pRight) ) return -1; - - if( plrColumn(pLeft)plrColumn(pRight) ) return 1; - - if( plrPosition(pLeft)plrPosition(pRight) ) return 1; - if( pLeft->iType==DL_POSITIONS ) return 0; - - if( plrStartOffset(pLeft)plrStartOffset(pRight) ) return 1; - - if( plrEndOffset(pLeft)plrEndOffset(pRight) ) return 1; - - return 0; -} - -/* Write the union of position lists in pLeft and pRight to pOut. -** "Union" in this case meaning "All unique position tuples". Should -** work with any doclist type, though both inputs and the output -** should be the same type. -*/ -static void posListUnion(DLReader *pLeft, DLReader *pRight, DLWriter *pOut){ - PLReader left, right; - PLWriter writer; - - assert( dlrDocid(pLeft)==dlrDocid(pRight) ); - assert( pLeft->iType==pRight->iType ); - assert( pLeft->iType==pOut->iType ); - - plrInit(&left, pLeft); - plrInit(&right, pRight); - plwInit(&writer, pOut, dlrDocid(pLeft)); - - while( !plrAtEnd(&left) || !plrAtEnd(&right) ){ - int c = posListCmp(&left, &right); - if( c<0 ){ - plwCopy(&writer, &left); - plrStep(&left); - }else if( c>0 ){ - plwCopy(&writer, &right); - plrStep(&right); - }else{ - plwCopy(&writer, &left); - plrStep(&left); - plrStep(&right); - } - } - - plwTerminate(&writer); - plwDestroy(&writer); - plrDestroy(&left); - plrDestroy(&right); -} - -/* Write the union of doclists in pLeft and pRight to pOut. For -** docids in common between the inputs, the union of the position -** lists is written. Inputs and outputs are always type DL_DEFAULT. -*/ -static void docListUnion( - const char *pLeft, int nLeft, - const char *pRight, int nRight, - DataBuffer *pOut /* Write the combined doclist here */ -){ - DLReader left, right; - DLWriter writer; - - if( nLeft==0 ){ - if( nRight!=0) dataBufferAppend(pOut, pRight, nRight); - return; - } - if( nRight==0 ){ - dataBufferAppend(pOut, pLeft, nLeft); - return; - } - - dlrInit(&left, DL_DEFAULT, pLeft, nLeft); - dlrInit(&right, DL_DEFAULT, pRight, nRight); - dlwInit(&writer, DL_DEFAULT, pOut); - - while( !dlrAtEnd(&left) || !dlrAtEnd(&right) ){ - if( dlrAtEnd(&right) ){ - dlwCopy(&writer, &left); - dlrStep(&left); - }else if( dlrAtEnd(&left) ){ - dlwCopy(&writer, &right); - dlrStep(&right); - }else if( dlrDocid(&left)dlrDocid(&right) ){ - dlwCopy(&writer, &right); - dlrStep(&right); - }else{ - posListUnion(&left, &right, &writer); - dlrStep(&left); - dlrStep(&right); - } - } - - dlrDestroy(&left); - dlrDestroy(&right); - dlwDestroy(&writer); -} - -/* pLeft and pRight are DLReaders positioned to the same docid. -** -** If there are no instances in pLeft or pRight where the position -** of pLeft is one less than the position of pRight, then this -** routine adds nothing to pOut. -** -** If there are one or more instances where positions from pLeft -** are exactly one less than positions from pRight, then add a new -** document record to pOut. If pOut wants to hold positions, then -** include the positions from pRight that are one more than a -** position in pLeft. In other words: pRight.iPos==pLeft.iPos+1. -*/ -static void posListPhraseMerge(DLReader *pLeft, DLReader *pRight, - DLWriter *pOut){ - PLReader left, right; - PLWriter writer; - int match = 0; - - assert( dlrDocid(pLeft)==dlrDocid(pRight) ); - assert( pOut->iType!=DL_POSITIONS_OFFSETS ); - - plrInit(&left, pLeft); - plrInit(&right, pRight); - - while( !plrAtEnd(&left) && !plrAtEnd(&right) ){ - if( plrColumn(&left)plrColumn(&right) ){ - plrStep(&right); - }else if( plrPosition(&left)+1plrPosition(&right) ){ - plrStep(&right); - }else{ - if( !match ){ - plwInit(&writer, pOut, dlrDocid(pLeft)); - match = 1; - } - plwAdd(&writer, plrColumn(&right), plrPosition(&right), 0, 0); - plrStep(&left); - plrStep(&right); - } - } - - if( match ){ - plwTerminate(&writer); - plwDestroy(&writer); - } - - plrDestroy(&left); - plrDestroy(&right); -} - -/* We have two doclists with positions: pLeft and pRight. -** Write the phrase intersection of these two doclists into pOut. -** -** A phrase intersection means that two documents only match -** if pLeft.iPos+1==pRight.iPos. -** -** iType controls the type of data written to pOut. If iType is -** DL_POSITIONS, the positions are those from pRight. -*/ -static void docListPhraseMerge( - const char *pLeft, int nLeft, - const char *pRight, int nRight, - DocListType iType, - DataBuffer *pOut /* Write the combined doclist here */ -){ - DLReader left, right; - DLWriter writer; - - if( nLeft==0 || nRight==0 ) return; - - assert( iType!=DL_POSITIONS_OFFSETS ); - - dlrInit(&left, DL_POSITIONS, pLeft, nLeft); - dlrInit(&right, DL_POSITIONS, pRight, nRight); - dlwInit(&writer, iType, pOut); - - while( !dlrAtEnd(&left) && !dlrAtEnd(&right) ){ - if( dlrDocid(&left) one AND (two OR three) - * [one OR two three] ==> (one OR two) AND three - * - * A "-" before a term matches all entries that lack that term. - * The "-" must occur immediately before the term with in intervening - * space. This is how the search engines do it. - * - * A NOT term cannot be the right-hand operand of an OR. If this - * occurs in the query string, the NOT is ignored: - * - * [one OR -two] ==> one OR two - * - */ -typedef struct Query { - fulltext_vtab *pFts; /* The full text index */ - int nTerms; /* Number of terms in the query */ - QueryTerm *pTerms; /* Array of terms. Space obtained from malloc() */ - int nextIsOr; /* Set the isOr flag on the next inserted term */ - int nextColumn; /* Next word parsed must be in this column */ - int dfltColumn; /* The default column */ -} Query; - - -/* -** An instance of the following structure keeps track of generated -** matching-word offset information and snippets. -*/ -typedef struct Snippet { - int nMatch; /* Total number of matches */ - int nAlloc; /* Space allocated for aMatch[] */ - struct snippetMatch { /* One entry for each matching term */ - char snStatus; /* Status flag for use while constructing snippets */ - short int iCol; /* The column that contains the match */ - short int iTerm; /* The index in Query.pTerms[] of the matching term */ - short int nByte; /* Number of bytes in the term */ - int iStart; /* The offset to the first character of the term */ - } *aMatch; /* Points to space obtained from malloc */ - char *zOffset; /* Text rendering of aMatch[] */ - int nOffset; /* strlen(zOffset) */ - char *zSnippet; /* Snippet text */ - int nSnippet; /* strlen(zSnippet) */ -} Snippet; - - -typedef enum QueryType { - QUERY_GENERIC, /* table scan */ - QUERY_ROWID, /* lookup by rowid */ - QUERY_FULLTEXT /* QUERY_FULLTEXT + [i] is a full-text search for column i*/ -} QueryType; - -typedef enum fulltext_statement { - CONTENT_INSERT_STMT, - CONTENT_SELECT_STMT, - CONTENT_UPDATE_STMT, - CONTENT_DELETE_STMT, - CONTENT_EXISTS_STMT, - - BLOCK_INSERT_STMT, - BLOCK_SELECT_STMT, - BLOCK_DELETE_STMT, - BLOCK_DELETE_ALL_STMT, - - SEGDIR_MAX_INDEX_STMT, - SEGDIR_SET_STMT, - SEGDIR_SELECT_LEVEL_STMT, - SEGDIR_SPAN_STMT, - SEGDIR_DELETE_STMT, - SEGDIR_SELECT_SEGMENT_STMT, - SEGDIR_SELECT_ALL_STMT, - SEGDIR_DELETE_ALL_STMT, - SEGDIR_COUNT_STMT, - - MAX_STMT /* Always at end! */ -} fulltext_statement; - -/* These must exactly match the enum above. */ -/* TODO(shess): Is there some risk that a statement will be used in two -** cursors at once, e.g. if a query joins a virtual table to itself? -** If so perhaps we should move some of these to the cursor object. -*/ -static const char *const fulltext_zStatement[MAX_STMT] = { - /* CONTENT_INSERT */ NULL, /* generated in contentInsertStatement() */ - /* CONTENT_SELECT */ "select * from %_content where rowid = ?", - /* CONTENT_UPDATE */ NULL, /* generated in contentUpdateStatement() */ - /* CONTENT_DELETE */ "delete from %_content where rowid = ?", - /* CONTENT_EXISTS */ "select rowid from %_content limit 1", - - /* BLOCK_INSERT */ "insert into %_segments values (?)", - /* BLOCK_SELECT */ "select block from %_segments where rowid = ?", - /* BLOCK_DELETE */ "delete from %_segments where rowid between ? and ?", - /* BLOCK_DELETE_ALL */ "delete from %_segments", - - /* SEGDIR_MAX_INDEX */ "select max(idx) from %_segdir where level = ?", - /* SEGDIR_SET */ "insert into %_segdir values (?, ?, ?, ?, ?, ?)", - /* SEGDIR_SELECT_LEVEL */ - "select start_block, leaves_end_block, root from %_segdir " - " where level = ? order by idx", - /* SEGDIR_SPAN */ - "select min(start_block), max(end_block) from %_segdir " - " where level = ? and start_block <> 0", - /* SEGDIR_DELETE */ "delete from %_segdir where level = ?", - - /* NOTE(shess): The first three results of the following two - ** statements must match. - */ - /* SEGDIR_SELECT_SEGMENT */ - "select start_block, leaves_end_block, root from %_segdir " - " where level = ? and idx = ?", - /* SEGDIR_SELECT_ALL */ - "select start_block, leaves_end_block, root from %_segdir " - " order by level desc, idx asc", - /* SEGDIR_DELETE_ALL */ "delete from %_segdir", - /* SEGDIR_COUNT */ "select count(*), ifnull(max(level),0) from %_segdir", -}; - -/* -** A connection to a fulltext index is an instance of the following -** structure. The xCreate and xConnect methods create an instance -** of this structure and xDestroy and xDisconnect free that instance. -** All other methods receive a pointer to the structure as one of their -** arguments. -*/ -struct fulltext_vtab { - sqlite3_vtab base; /* Base class used by SQLite core */ - sqlite3 *db; /* The database connection */ - const char *zDb; /* logical database name */ - const char *zName; /* virtual table name */ - int nColumn; /* number of columns in virtual table */ - char **azColumn; /* column names. malloced */ - char **azContentColumn; /* column names in content table; malloced */ - sqlite3_tokenizer *pTokenizer; /* tokenizer for inserts and queries */ - - /* Precompiled statements which we keep as long as the table is - ** open. - */ - sqlite3_stmt *pFulltextStatements[MAX_STMT]; - - /* Precompiled statements used for segment merges. We run a - ** separate select across the leaf level of each tree being merged. - */ - sqlite3_stmt *pLeafSelectStmts[MERGE_COUNT]; - /* The statement used to prepare pLeafSelectStmts. */ -#define LEAF_SELECT \ - "select block from %_segments where rowid between ? and ? order by rowid" - - /* These buffer pending index updates during transactions. - ** nPendingData estimates the memory size of the pending data. It - ** doesn't include the hash-bucket overhead, nor any malloc - ** overhead. When nPendingData exceeds kPendingThreshold, the - ** buffer is flushed even before the transaction closes. - ** pendingTerms stores the data, and is only valid when nPendingData - ** is >=0 (nPendingData<0 means pendingTerms has not been - ** initialized). iPrevDocid is the last docid written, used to make - ** certain we're inserting in sorted order. - */ - int nPendingData; -#define kPendingThreshold (1*1024*1024) - sqlite_int64 iPrevDocid; - fts2Hash pendingTerms; -}; - -/* -** When the core wants to do a query, it create a cursor using a -** call to xOpen. This structure is an instance of a cursor. It -** is destroyed by xClose. -*/ -typedef struct fulltext_cursor { - sqlite3_vtab_cursor base; /* Base class used by SQLite core */ - QueryType iCursorType; /* Copy of sqlite3_index_info.idxNum */ - sqlite3_stmt *pStmt; /* Prepared statement in use by the cursor */ - int eof; /* True if at End Of Results */ - Query q; /* Parsed query string */ - Snippet snippet; /* Cached snippet for the current row */ - int iColumn; /* Column being searched */ - DataBuffer result; /* Doclist results from fulltextQuery */ - DLReader reader; /* Result reader if result not empty */ -} fulltext_cursor; - -static struct fulltext_vtab *cursor_vtab(fulltext_cursor *c){ - return (fulltext_vtab *) c->base.pVtab; -} - -static const sqlite3_module fts2Module; /* forward declaration */ - -/* Return a dynamically generated statement of the form - * insert into %_content (rowid, ...) values (?, ...) - */ -static const char *contentInsertStatement(fulltext_vtab *v){ - StringBuffer sb; - int i; - - initStringBuffer(&sb); - append(&sb, "insert into %_content (rowid, "); - appendList(&sb, v->nColumn, v->azContentColumn); - append(&sb, ") values (?"); - for(i=0; inColumn; ++i) - append(&sb, ", ?"); - append(&sb, ")"); - return stringBufferData(&sb); -} - -/* Return a dynamically generated statement of the form - * update %_content set [col_0] = ?, [col_1] = ?, ... - * where rowid = ? - */ -static const char *contentUpdateStatement(fulltext_vtab *v){ - StringBuffer sb; - int i; - - initStringBuffer(&sb); - append(&sb, "update %_content set "); - for(i=0; inColumn; ++i) { - if( i>0 ){ - append(&sb, ", "); - } - append(&sb, v->azContentColumn[i]); - append(&sb, " = ?"); - } - append(&sb, " where rowid = ?"); - return stringBufferData(&sb); -} - -/* Puts a freshly-prepared statement determined by iStmt in *ppStmt. -** If the indicated statement has never been prepared, it is prepared -** and cached, otherwise the cached version is reset. -*/ -static int sql_get_statement(fulltext_vtab *v, fulltext_statement iStmt, - sqlite3_stmt **ppStmt){ - assert( iStmtpFulltextStatements[iStmt]==NULL ){ - const char *zStmt; - int rc; - switch( iStmt ){ - case CONTENT_INSERT_STMT: - zStmt = contentInsertStatement(v); break; - case CONTENT_UPDATE_STMT: - zStmt = contentUpdateStatement(v); break; - default: - zStmt = fulltext_zStatement[iStmt]; - } - rc = sql_prepare(v->db, v->zDb, v->zName, &v->pFulltextStatements[iStmt], - zStmt); - if( zStmt != fulltext_zStatement[iStmt]) sqlite3_free((void *) zStmt); - if( rc!=SQLITE_OK ) return rc; - } else { - int rc = sqlite3_reset(v->pFulltextStatements[iStmt]); - if( rc!=SQLITE_OK ) return rc; - } - - *ppStmt = v->pFulltextStatements[iStmt]; - return SQLITE_OK; -} - -/* Like sqlite3_step(), but convert SQLITE_DONE to SQLITE_OK and -** SQLITE_ROW to SQLITE_ERROR. Useful for statements like UPDATE, -** where we expect no results. -*/ -static int sql_single_step(sqlite3_stmt *s){ - int rc = sqlite3_step(s); - return (rc==SQLITE_DONE) ? SQLITE_OK : rc; -} - -/* Like sql_get_statement(), but for special replicated LEAF_SELECT -** statements. idx -1 is a special case for an uncached version of -** the statement (used in the optimize implementation). -*/ -/* TODO(shess) Write version for generic statements and then share -** that between the cached-statement functions. -*/ -static int sql_get_leaf_statement(fulltext_vtab *v, int idx, - sqlite3_stmt **ppStmt){ - assert( idx>=-1 && idxdb, v->zDb, v->zName, ppStmt, LEAF_SELECT); - }else if( v->pLeafSelectStmts[idx]==NULL ){ - int rc = sql_prepare(v->db, v->zDb, v->zName, &v->pLeafSelectStmts[idx], - LEAF_SELECT); - if( rc!=SQLITE_OK ) return rc; - }else{ - int rc = sqlite3_reset(v->pLeafSelectStmts[idx]); - if( rc!=SQLITE_OK ) return rc; - } - - *ppStmt = v->pLeafSelectStmts[idx]; - return SQLITE_OK; -} - -/* insert into %_content (rowid, ...) values ([rowid], [pValues]) */ -static int content_insert(fulltext_vtab *v, sqlite3_value *rowid, - sqlite3_value **pValues){ - sqlite3_stmt *s; - int i; - int rc = sql_get_statement(v, CONTENT_INSERT_STMT, &s); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_value(s, 1, rowid); - if( rc!=SQLITE_OK ) return rc; - - for(i=0; inColumn; ++i){ - rc = sqlite3_bind_value(s, 2+i, pValues[i]); - if( rc!=SQLITE_OK ) return rc; - } - - return sql_single_step(s); -} - -/* update %_content set col0 = pValues[0], col1 = pValues[1], ... - * where rowid = [iRowid] */ -static int content_update(fulltext_vtab *v, sqlite3_value **pValues, - sqlite_int64 iRowid){ - sqlite3_stmt *s; - int i; - int rc = sql_get_statement(v, CONTENT_UPDATE_STMT, &s); - if( rc!=SQLITE_OK ) return rc; - - for(i=0; inColumn; ++i){ - rc = sqlite3_bind_value(s, 1+i, pValues[i]); - if( rc!=SQLITE_OK ) return rc; - } - - rc = sqlite3_bind_int64(s, 1+v->nColumn, iRowid); - if( rc!=SQLITE_OK ) return rc; - - return sql_single_step(s); -} - -static void freeStringArray(int nString, const char **pString){ - int i; - - for (i=0 ; i < nString ; ++i) { - if( pString[i]!=NULL ) sqlite3_free((void *) pString[i]); - } - sqlite3_free((void *) pString); -} - -/* select * from %_content where rowid = [iRow] - * The caller must delete the returned array and all strings in it. - * null fields will be NULL in the returned array. - * - * TODO: Perhaps we should return pointer/length strings here for consistency - * with other code which uses pointer/length. */ -static int content_select(fulltext_vtab *v, sqlite_int64 iRow, - const char ***pValues){ - sqlite3_stmt *s; - const char **values; - int i; - int rc; - - *pValues = NULL; - - rc = sql_get_statement(v, CONTENT_SELECT_STMT, &s); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_int64(s, 1, iRow); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_step(s); - if( rc!=SQLITE_ROW ) return rc; - - values = (const char **) sqlite3_malloc(v->nColumn * sizeof(const char *)); - for(i=0; inColumn; ++i){ - if( sqlite3_column_type(s, i)==SQLITE_NULL ){ - values[i] = NULL; - }else{ - values[i] = string_dup((char*)sqlite3_column_text(s, i)); - } - } - - /* We expect only one row. We must execute another sqlite3_step() - * to complete the iteration; otherwise the table will remain locked. */ - rc = sqlite3_step(s); - if( rc==SQLITE_DONE ){ - *pValues = values; - return SQLITE_OK; - } - - freeStringArray(v->nColumn, values); - return rc; -} - -/* delete from %_content where rowid = [iRow ] */ -static int content_delete(fulltext_vtab *v, sqlite_int64 iRow){ - sqlite3_stmt *s; - int rc = sql_get_statement(v, CONTENT_DELETE_STMT, &s); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_int64(s, 1, iRow); - if( rc!=SQLITE_OK ) return rc; - - return sql_single_step(s); -} - -/* Returns SQLITE_ROW if any rows exist in %_content, SQLITE_DONE if -** no rows exist, and any error in case of failure. -*/ -static int content_exists(fulltext_vtab *v){ - sqlite3_stmt *s; - int rc = sql_get_statement(v, CONTENT_EXISTS_STMT, &s); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_step(s); - if( rc!=SQLITE_ROW ) return rc; - - /* We expect only one row. We must execute another sqlite3_step() - * to complete the iteration; otherwise the table will remain locked. */ - rc = sqlite3_step(s); - if( rc==SQLITE_DONE ) return SQLITE_ROW; - if( rc==SQLITE_ROW ) return SQLITE_ERROR; - return rc; -} - -/* insert into %_segments values ([pData]) -** returns assigned rowid in *piBlockid -*/ -static int block_insert(fulltext_vtab *v, const char *pData, int nData, - sqlite_int64 *piBlockid){ - sqlite3_stmt *s; - int rc = sql_get_statement(v, BLOCK_INSERT_STMT, &s); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_blob(s, 1, pData, nData, SQLITE_STATIC); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_step(s); - if( rc==SQLITE_ROW ) return SQLITE_ERROR; - if( rc!=SQLITE_DONE ) return rc; - - *piBlockid = sqlite3_last_insert_rowid(v->db); - return SQLITE_OK; -} - -/* delete from %_segments -** where rowid between [iStartBlockid] and [iEndBlockid] -** -** Deletes the range of blocks, inclusive, used to delete the blocks -** which form a segment. -*/ -static int block_delete(fulltext_vtab *v, - sqlite_int64 iStartBlockid, sqlite_int64 iEndBlockid){ - sqlite3_stmt *s; - int rc = sql_get_statement(v, BLOCK_DELETE_STMT, &s); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_int64(s, 1, iStartBlockid); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_int64(s, 2, iEndBlockid); - if( rc!=SQLITE_OK ) return rc; - - return sql_single_step(s); -} - -/* Returns SQLITE_ROW with *pidx set to the maximum segment idx found -** at iLevel. Returns SQLITE_DONE if there are no segments at -** iLevel. Otherwise returns an error. -*/ -static int segdir_max_index(fulltext_vtab *v, int iLevel, int *pidx){ - sqlite3_stmt *s; - int rc = sql_get_statement(v, SEGDIR_MAX_INDEX_STMT, &s); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_int(s, 1, iLevel); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_step(s); - /* Should always get at least one row due to how max() works. */ - if( rc==SQLITE_DONE ) return SQLITE_DONE; - if( rc!=SQLITE_ROW ) return rc; - - /* NULL means that there were no inputs to max(). */ - if( SQLITE_NULL==sqlite3_column_type(s, 0) ){ - rc = sqlite3_step(s); - if( rc==SQLITE_ROW ) return SQLITE_ERROR; - return rc; - } - - *pidx = sqlite3_column_int(s, 0); - - /* We expect only one row. We must execute another sqlite3_step() - * to complete the iteration; otherwise the table will remain locked. */ - rc = sqlite3_step(s); - if( rc==SQLITE_ROW ) return SQLITE_ERROR; - if( rc!=SQLITE_DONE ) return rc; - return SQLITE_ROW; -} - -/* insert into %_segdir values ( -** [iLevel], [idx], -** [iStartBlockid], [iLeavesEndBlockid], [iEndBlockid], -** [pRootData] -** ) -*/ -static int segdir_set(fulltext_vtab *v, int iLevel, int idx, - sqlite_int64 iStartBlockid, - sqlite_int64 iLeavesEndBlockid, - sqlite_int64 iEndBlockid, - const char *pRootData, int nRootData){ - sqlite3_stmt *s; - int rc = sql_get_statement(v, SEGDIR_SET_STMT, &s); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_int(s, 1, iLevel); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_int(s, 2, idx); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_int64(s, 3, iStartBlockid); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_int64(s, 4, iLeavesEndBlockid); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_int64(s, 5, iEndBlockid); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_blob(s, 6, pRootData, nRootData, SQLITE_STATIC); - if( rc!=SQLITE_OK ) return rc; - - return sql_single_step(s); -} - -/* Queries %_segdir for the block span of the segments in level -** iLevel. Returns SQLITE_DONE if there are no blocks for iLevel, -** SQLITE_ROW if there are blocks, else an error. -*/ -static int segdir_span(fulltext_vtab *v, int iLevel, - sqlite_int64 *piStartBlockid, - sqlite_int64 *piEndBlockid){ - sqlite3_stmt *s; - int rc = sql_get_statement(v, SEGDIR_SPAN_STMT, &s); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_int(s, 1, iLevel); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_step(s); - if( rc==SQLITE_DONE ) return SQLITE_DONE; /* Should never happen */ - if( rc!=SQLITE_ROW ) return rc; - - /* This happens if all segments at this level are entirely inline. */ - if( SQLITE_NULL==sqlite3_column_type(s, 0) ){ - /* We expect only one row. We must execute another sqlite3_step() - * to complete the iteration; otherwise the table will remain locked. */ - int rc2 = sqlite3_step(s); - if( rc2==SQLITE_ROW ) return SQLITE_ERROR; - return rc2; - } - - *piStartBlockid = sqlite3_column_int64(s, 0); - *piEndBlockid = sqlite3_column_int64(s, 1); - - /* We expect only one row. We must execute another sqlite3_step() - * to complete the iteration; otherwise the table will remain locked. */ - rc = sqlite3_step(s); - if( rc==SQLITE_ROW ) return SQLITE_ERROR; - if( rc!=SQLITE_DONE ) return rc; - return SQLITE_ROW; -} - -/* Delete the segment blocks and segment directory records for all -** segments at iLevel. -*/ -static int segdir_delete(fulltext_vtab *v, int iLevel){ - sqlite3_stmt *s; - sqlite_int64 iStartBlockid, iEndBlockid; - int rc = segdir_span(v, iLevel, &iStartBlockid, &iEndBlockid); - if( rc!=SQLITE_ROW && rc!=SQLITE_DONE ) return rc; - - if( rc==SQLITE_ROW ){ - rc = block_delete(v, iStartBlockid, iEndBlockid); - if( rc!=SQLITE_OK ) return rc; - } - - /* Delete the segment directory itself. */ - rc = sql_get_statement(v, SEGDIR_DELETE_STMT, &s); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_int64(s, 1, iLevel); - if( rc!=SQLITE_OK ) return rc; - - return sql_single_step(s); -} - -/* Delete entire fts index, SQLITE_OK on success, relevant error on -** failure. -*/ -static int segdir_delete_all(fulltext_vtab *v){ - sqlite3_stmt *s; - int rc = sql_get_statement(v, SEGDIR_DELETE_ALL_STMT, &s); - if( rc!=SQLITE_OK ) return rc; - - rc = sql_single_step(s); - if( rc!=SQLITE_OK ) return rc; - - rc = sql_get_statement(v, BLOCK_DELETE_ALL_STMT, &s); - if( rc!=SQLITE_OK ) return rc; - - return sql_single_step(s); -} - -/* Returns SQLITE_OK with *pnSegments set to the number of entries in -** %_segdir and *piMaxLevel set to the highest level which has a -** segment. Otherwise returns the SQLite error which caused failure. -*/ -static int segdir_count(fulltext_vtab *v, int *pnSegments, int *piMaxLevel){ - sqlite3_stmt *s; - int rc = sql_get_statement(v, SEGDIR_COUNT_STMT, &s); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_step(s); - /* TODO(shess): This case should not be possible? Should stronger - ** measures be taken if it happens? - */ - if( rc==SQLITE_DONE ){ - *pnSegments = 0; - *piMaxLevel = 0; - return SQLITE_OK; - } - if( rc!=SQLITE_ROW ) return rc; - - *pnSegments = sqlite3_column_int(s, 0); - *piMaxLevel = sqlite3_column_int(s, 1); - - /* We expect only one row. We must execute another sqlite3_step() - * to complete the iteration; otherwise the table will remain locked. */ - rc = sqlite3_step(s); - if( rc==SQLITE_DONE ) return SQLITE_OK; - if( rc==SQLITE_ROW ) return SQLITE_ERROR; - return rc; -} - -/* TODO(shess) clearPendingTerms() is far down the file because -** writeZeroSegment() is far down the file because LeafWriter is far -** down the file. Consider refactoring the code to move the non-vtab -** code above the vtab code so that we don't need this forward -** reference. -*/ -static int clearPendingTerms(fulltext_vtab *v); - -/* -** Free the memory used to contain a fulltext_vtab structure. -*/ -static void fulltext_vtab_destroy(fulltext_vtab *v){ - int iStmt, i; - - TRACE(("FTS2 Destroy %p\n", v)); - for( iStmt=0; iStmtpFulltextStatements[iStmt]!=NULL ){ - sqlite3_finalize(v->pFulltextStatements[iStmt]); - v->pFulltextStatements[iStmt] = NULL; - } - } - - for( i=0; ipLeafSelectStmts[i]!=NULL ){ - sqlite3_finalize(v->pLeafSelectStmts[i]); - v->pLeafSelectStmts[i] = NULL; - } - } - - if( v->pTokenizer!=NULL ){ - v->pTokenizer->pModule->xDestroy(v->pTokenizer); - v->pTokenizer = NULL; - } - - clearPendingTerms(v); - - sqlite3_free(v->azColumn); - for(i = 0; i < v->nColumn; ++i) { - sqlite3_free(v->azContentColumn[i]); - } - sqlite3_free(v->azContentColumn); - sqlite3_free(v); -} - -/* -** Token types for parsing the arguments to xConnect or xCreate. -*/ -#define TOKEN_EOF 0 /* End of file */ -#define TOKEN_SPACE 1 /* Any kind of whitespace */ -#define TOKEN_ID 2 /* An identifier */ -#define TOKEN_STRING 3 /* A string literal */ -#define TOKEN_PUNCT 4 /* A single punctuation character */ - -/* -** If X is a character that can be used in an identifier then -** IdChar(X) will be true. Otherwise it is false. -** -** For ASCII, any character with the high-order bit set is -** allowed in an identifier. For 7-bit characters, -** sqlite3IsIdChar[X] must be 1. -** -** Ticket #1066. the SQL standard does not allow '$' in the -** middle of identfiers. But many SQL implementations do. -** SQLite will allow '$' in identifiers for compatibility. -** But the feature is undocumented. -*/ -static const char isIdChar[] = { -/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */ - 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */ - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */ - 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */ - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */ - 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */ - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */ -}; -#define IdChar(C) (((c=C)&0x80)!=0 || (c>0x1f && isIdChar[c-0x20])) - - -/* -** Return the length of the token that begins at z[0]. -** Store the token type in *tokenType before returning. -*/ -static int getToken(const char *z, int *tokenType){ - int i, c; - switch( *z ){ - case 0: { - *tokenType = TOKEN_EOF; - return 0; - } - case ' ': case '\t': case '\n': case '\f': case '\r': { - for(i=1; safe_isspace(z[i]); i++){} - *tokenType = TOKEN_SPACE; - return i; - } - case '`': - case '\'': - case '"': { - int delim = z[0]; - for(i=1; (c=z[i])!=0; i++){ - if( c==delim ){ - if( z[i+1]==delim ){ - i++; - }else{ - break; - } - } - } - *tokenType = TOKEN_STRING; - return i + (c!=0); - } - case '[': { - for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){} - *tokenType = TOKEN_ID; - return i; - } - default: { - if( !IdChar(*z) ){ - break; - } - for(i=1; IdChar(z[i]); i++){} - *tokenType = TOKEN_ID; - return i; - } - } - *tokenType = TOKEN_PUNCT; - return 1; -} - -/* -** A token extracted from a string is an instance of the following -** structure. -*/ -typedef struct Token { - const char *z; /* Pointer to token text. Not '\000' terminated */ - short int n; /* Length of the token text in bytes. */ -} Token; - -/* -** Given a input string (which is really one of the argv[] parameters -** passed into xConnect or xCreate) split the string up into tokens. -** Return an array of pointers to '\000' terminated strings, one string -** for each non-whitespace token. -** -** The returned array is terminated by a single NULL pointer. -** -** Space to hold the returned array is obtained from a single -** malloc and should be freed by passing the return value to free(). -** The individual strings within the token list are all a part of -** the single memory allocation and will all be freed at once. -*/ -static char **tokenizeString(const char *z, int *pnToken){ - int nToken = 0; - Token *aToken = sqlite3_malloc( strlen(z) * sizeof(aToken[0]) ); - int n = 1; - int e, i; - int totalSize = 0; - char **azToken; - char *zCopy; - while( n>0 ){ - n = getToken(z, &e); - if( e!=TOKEN_SPACE ){ - aToken[nToken].z = z; - aToken[nToken].n = n; - nToken++; - totalSize += n+1; - } - z += n; - } - azToken = (char**)sqlite3_malloc( nToken*sizeof(char*) + totalSize ); - zCopy = (char*)&azToken[nToken]; - nToken--; - for(i=0; i=0 ){ - azIn[j] = azIn[i]; - } - j++; - } - } - azIn[j] = 0; - } -} - - -/* -** Find the first alphanumeric token in the string zIn. Null-terminate -** this token. Remove any quotation marks. And return a pointer to -** the result. -*/ -static char *firstToken(char *zIn, char **pzTail){ - int n, ttype; - while(1){ - n = getToken(zIn, &ttype); - if( ttype==TOKEN_SPACE ){ - zIn += n; - }else if( ttype==TOKEN_EOF ){ - *pzTail = zIn; - return 0; - }else{ - zIn[n] = 0; - *pzTail = &zIn[1]; - dequoteString(zIn); - return zIn; - } - } - /*NOTREACHED*/ -} - -/* Return true if... -** -** * s begins with the string t, ignoring case -** * s is longer than t -** * The first character of s beyond t is not a alphanumeric -** -** Ignore leading space in *s. -** -** To put it another way, return true if the first token of -** s[] is t[]. -*/ -static int startsWith(const char *s, const char *t){ - while( safe_isspace(*s) ){ s++; } - while( *t ){ - if( safe_tolower(*s++)!=safe_tolower(*t++) ) return 0; - } - return *s!='_' && !safe_isalnum(*s); -} - -/* -** An instance of this structure defines the "spec" of a -** full text index. This structure is populated by parseSpec -** and use by fulltextConnect and fulltextCreate. -*/ -typedef struct TableSpec { - const char *zDb; /* Logical database name */ - const char *zName; /* Name of the full-text index */ - int nColumn; /* Number of columns to be indexed */ - char **azColumn; /* Original names of columns to be indexed */ - char **azContentColumn; /* Column names for %_content */ - char **azTokenizer; /* Name of tokenizer and its arguments */ -} TableSpec; - -/* -** Reclaim all of the memory used by a TableSpec -*/ -static void clearTableSpec(TableSpec *p) { - sqlite3_free(p->azColumn); - sqlite3_free(p->azContentColumn); - sqlite3_free(p->azTokenizer); -} - -/* Parse a CREATE VIRTUAL TABLE statement, which looks like this: - * - * CREATE VIRTUAL TABLE email - * USING fts2(subject, body, tokenize mytokenizer(myarg)) - * - * We return parsed information in a TableSpec structure. - * - */ -static int parseSpec(TableSpec *pSpec, int argc, const char *const*argv, - char**pzErr){ - int i, n; - char *z, *zDummy; - char **azArg; - const char *zTokenizer = 0; /* argv[] entry describing the tokenizer */ - - assert( argc>=3 ); - /* Current interface: - ** argv[0] - module name - ** argv[1] - database name - ** argv[2] - table name - ** argv[3..] - columns, optionally followed by tokenizer specification - ** and snippet delimiters specification. - */ - - /* Make a copy of the complete argv[][] array in a single allocation. - ** The argv[][] array is read-only and transient. We can write to the - ** copy in order to modify things and the copy is persistent. - */ - CLEAR(pSpec); - for(i=n=0; izDb = azArg[1]; - pSpec->zName = azArg[2]; - pSpec->nColumn = 0; - pSpec->azColumn = azArg; - zTokenizer = "tokenize simple"; - for(i=3; inColumn] = firstToken(azArg[i], &zDummy); - pSpec->nColumn++; - } - } - if( pSpec->nColumn==0 ){ - azArg[0] = "content"; - pSpec->nColumn = 1; - } - - /* - ** Construct the list of content column names. - ** - ** Each content column name will be of the form cNNAAAA - ** where NN is the column number and AAAA is the sanitized - ** column name. "sanitized" means that special characters are - ** converted to "_". The cNN prefix guarantees that all column - ** names are unique. - ** - ** The AAAA suffix is not strictly necessary. It is included - ** for the convenience of people who might examine the generated - ** %_content table and wonder what the columns are used for. - */ - pSpec->azContentColumn = sqlite3_malloc( pSpec->nColumn * sizeof(char *) ); - if( pSpec->azContentColumn==0 ){ - clearTableSpec(pSpec); - return SQLITE_NOMEM; - } - for(i=0; inColumn; i++){ - char *p; - pSpec->azContentColumn[i] = sqlite3_mprintf("c%d%s", i, azArg[i]); - for (p = pSpec->azContentColumn[i]; *p ; ++p) { - if( !safe_isalnum(*p) ) *p = '_'; - } - } - - /* - ** Parse the tokenizer specification string. - */ - pSpec->azTokenizer = tokenizeString(zTokenizer, &n); - tokenListToIdList(pSpec->azTokenizer); - - return SQLITE_OK; -} - -/* -** Generate a CREATE TABLE statement that describes the schema of -** the virtual table. Return a pointer to this schema string. -** -** Space is obtained from sqlite3_mprintf() and should be freed -** using sqlite3_free(). -*/ -static char *fulltextSchema( - int nColumn, /* Number of columns */ - const char *const* azColumn, /* List of columns */ - const char *zTableName /* Name of the table */ -){ - int i; - char *zSchema, *zNext; - const char *zSep = "("; - zSchema = sqlite3_mprintf("CREATE TABLE x"); - for(i=0; ibase */ - v->db = db; - v->zDb = spec->zDb; /* Freed when azColumn is freed */ - v->zName = spec->zName; /* Freed when azColumn is freed */ - v->nColumn = spec->nColumn; - v->azContentColumn = spec->azContentColumn; - spec->azContentColumn = 0; - v->azColumn = spec->azColumn; - spec->azColumn = 0; - - if( spec->azTokenizer==0 ){ - return SQLITE_NOMEM; - } - - zTok = spec->azTokenizer[0]; - if( !zTok ){ - zTok = "simple"; - } - nTok = strlen(zTok)+1; - - m = (sqlite3_tokenizer_module *)sqlite3Fts2HashFind(pHash, zTok, nTok); - if( !m ){ - *pzErr = sqlite3_mprintf("unknown tokenizer: %s", spec->azTokenizer[0]); - rc = SQLITE_ERROR; - goto err; - } - - for(n=0; spec->azTokenizer[n]; n++){} - if( n ){ - rc = m->xCreate(n-1, (const char*const*)&spec->azTokenizer[1], - &v->pTokenizer); - }else{ - rc = m->xCreate(0, 0, &v->pTokenizer); - } - if( rc!=SQLITE_OK ) goto err; - v->pTokenizer->pModule = m; - - /* TODO: verify the existence of backing tables foo_content, foo_term */ - - schema = fulltextSchema(v->nColumn, (const char*const*)v->azColumn, - spec->zName); - rc = sqlite3_declare_vtab(db, schema); - sqlite3_free(schema); - if( rc!=SQLITE_OK ) goto err; - - memset(v->pFulltextStatements, 0, sizeof(v->pFulltextStatements)); - - /* Indicate that the buffer is not live. */ - v->nPendingData = -1; - - *ppVTab = &v->base; - TRACE(("FTS2 Connect %p\n", v)); - - return rc; - -err: - fulltext_vtab_destroy(v); - return rc; -} - -static int fulltextConnect( - sqlite3 *db, - void *pAux, - int argc, const char *const*argv, - sqlite3_vtab **ppVTab, - char **pzErr -){ - TableSpec spec; - int rc = parseSpec(&spec, argc, argv, pzErr); - if( rc!=SQLITE_OK ) return rc; - - rc = constructVtab(db, (fts2Hash *)pAux, &spec, ppVTab, pzErr); - clearTableSpec(&spec); - return rc; -} - -/* The %_content table holds the text of each document, with -** the rowid used as the docid. -*/ -/* TODO(shess) This comment needs elaboration to match the updated -** code. Work it into the top-of-file comment at that time. -*/ -static int fulltextCreate(sqlite3 *db, void *pAux, - int argc, const char * const *argv, - sqlite3_vtab **ppVTab, char **pzErr){ - int rc; - TableSpec spec; - StringBuffer schema; - TRACE(("FTS2 Create\n")); - - rc = parseSpec(&spec, argc, argv, pzErr); - if( rc!=SQLITE_OK ) return rc; - - initStringBuffer(&schema); - append(&schema, "CREATE TABLE %_content("); - appendList(&schema, spec.nColumn, spec.azContentColumn); - append(&schema, ")"); - rc = sql_exec(db, spec.zDb, spec.zName, stringBufferData(&schema)); - stringBufferDestroy(&schema); - if( rc!=SQLITE_OK ) goto out; - - rc = sql_exec(db, spec.zDb, spec.zName, - "create table %_segments(block blob);"); - if( rc!=SQLITE_OK ) goto out; - - rc = sql_exec(db, spec.zDb, spec.zName, - "create table %_segdir(" - " level integer," - " idx integer," - " start_block integer," - " leaves_end_block integer," - " end_block integer," - " root blob," - " primary key(level, idx)" - ");"); - if( rc!=SQLITE_OK ) goto out; - - rc = constructVtab(db, (fts2Hash *)pAux, &spec, ppVTab, pzErr); - -out: - clearTableSpec(&spec); - return rc; -} - -/* Decide how to handle an SQL query. */ -static int fulltextBestIndex(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){ - int i; - TRACE(("FTS2 BestIndex\n")); - - for(i=0; inConstraint; ++i){ - const struct sqlite3_index_constraint *pConstraint; - pConstraint = &pInfo->aConstraint[i]; - if( pConstraint->usable ) { - if( pConstraint->iColumn==-1 && - pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){ - pInfo->idxNum = QUERY_ROWID; /* lookup by rowid */ - TRACE(("FTS2 QUERY_ROWID\n")); - } else if( pConstraint->iColumn>=0 && - pConstraint->op==SQLITE_INDEX_CONSTRAINT_MATCH ){ - /* full-text search */ - pInfo->idxNum = QUERY_FULLTEXT + pConstraint->iColumn; - TRACE(("FTS2 QUERY_FULLTEXT %d\n", pConstraint->iColumn)); - } else continue; - - pInfo->aConstraintUsage[i].argvIndex = 1; - pInfo->aConstraintUsage[i].omit = 1; - - /* An arbitrary value for now. - * TODO: Perhaps rowid matches should be considered cheaper than - * full-text searches. */ - pInfo->estimatedCost = 1.0; - - return SQLITE_OK; - } - } - pInfo->idxNum = QUERY_GENERIC; - return SQLITE_OK; -} - -static int fulltextDisconnect(sqlite3_vtab *pVTab){ - TRACE(("FTS2 Disconnect %p\n", pVTab)); - fulltext_vtab_destroy((fulltext_vtab *)pVTab); - return SQLITE_OK; -} - -static int fulltextDestroy(sqlite3_vtab *pVTab){ - fulltext_vtab *v = (fulltext_vtab *)pVTab; - int rc; - - TRACE(("FTS2 Destroy %p\n", pVTab)); - rc = sql_exec(v->db, v->zDb, v->zName, - "drop table if exists %_content;" - "drop table if exists %_segments;" - "drop table if exists %_segdir;" - ); - if( rc!=SQLITE_OK ) return rc; - - fulltext_vtab_destroy((fulltext_vtab *)pVTab); - return SQLITE_OK; -} - -static int fulltextOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){ - fulltext_cursor *c; - - c = (fulltext_cursor *) sqlite3_malloc(sizeof(fulltext_cursor)); - if( c ){ - memset(c, 0, sizeof(fulltext_cursor)); - /* sqlite will initialize c->base */ - *ppCursor = &c->base; - TRACE(("FTS2 Open %p: %p\n", pVTab, c)); - return SQLITE_OK; - }else{ - return SQLITE_NOMEM; - } -} - - -/* Free all of the dynamically allocated memory held by *q -*/ -static void queryClear(Query *q){ - int i; - for(i = 0; i < q->nTerms; ++i){ - sqlite3_free(q->pTerms[i].pTerm); - } - sqlite3_free(q->pTerms); - CLEAR(q); -} - -/* Free all of the dynamically allocated memory held by the -** Snippet -*/ -static void snippetClear(Snippet *p){ - sqlite3_free(p->aMatch); - sqlite3_free(p->zOffset); - sqlite3_free(p->zSnippet); - CLEAR(p); -} -/* -** Append a single entry to the p->aMatch[] log. -*/ -static void snippetAppendMatch( - Snippet *p, /* Append the entry to this snippet */ - int iCol, int iTerm, /* The column and query term */ - int iStart, int nByte /* Offset and size of the match */ -){ - int i; - struct snippetMatch *pMatch; - if( p->nMatch+1>=p->nAlloc ){ - p->nAlloc = p->nAlloc*2 + 10; - p->aMatch = sqlite3_realloc(p->aMatch, p->nAlloc*sizeof(p->aMatch[0]) ); - if( p->aMatch==0 ){ - p->nMatch = 0; - p->nAlloc = 0; - return; - } - } - i = p->nMatch++; - pMatch = &p->aMatch[i]; - pMatch->iCol = iCol; - pMatch->iTerm = iTerm; - pMatch->iStart = iStart; - pMatch->nByte = nByte; -} - -/* -** Sizing information for the circular buffer used in snippetOffsetsOfColumn() -*/ -#define FTS2_ROTOR_SZ (32) -#define FTS2_ROTOR_MASK (FTS2_ROTOR_SZ-1) - -/* -** Add entries to pSnippet->aMatch[] for every match that occurs against -** document zDoc[0..nDoc-1] which is stored in column iColumn. -*/ -static void snippetOffsetsOfColumn( - Query *pQuery, - Snippet *pSnippet, - int iColumn, - const char *zDoc, - int nDoc -){ - const sqlite3_tokenizer_module *pTModule; /* The tokenizer module */ - sqlite3_tokenizer *pTokenizer; /* The specific tokenizer */ - sqlite3_tokenizer_cursor *pTCursor; /* Tokenizer cursor */ - fulltext_vtab *pVtab; /* The full text index */ - int nColumn; /* Number of columns in the index */ - const QueryTerm *aTerm; /* Query string terms */ - int nTerm; /* Number of query string terms */ - int i, j; /* Loop counters */ - int rc; /* Return code */ - unsigned int match, prevMatch; /* Phrase search bitmasks */ - const char *zToken; /* Next token from the tokenizer */ - int nToken; /* Size of zToken */ - int iBegin, iEnd, iPos; /* Offsets of beginning and end */ - - /* The following variables keep a circular buffer of the last - ** few tokens */ - unsigned int iRotor = 0; /* Index of current token */ - int iRotorBegin[FTS2_ROTOR_SZ]; /* Beginning offset of token */ - int iRotorLen[FTS2_ROTOR_SZ]; /* Length of token */ - - pVtab = pQuery->pFts; - nColumn = pVtab->nColumn; - pTokenizer = pVtab->pTokenizer; - pTModule = pTokenizer->pModule; - rc = pTModule->xOpen(pTokenizer, zDoc, nDoc, &pTCursor); - if( rc ) return; - pTCursor->pTokenizer = pTokenizer; - aTerm = pQuery->pTerms; - nTerm = pQuery->nTerms; - if( nTerm>=FTS2_ROTOR_SZ ){ - nTerm = FTS2_ROTOR_SZ - 1; - } - prevMatch = 0; - while(1){ - rc = pTModule->xNext(pTCursor, &zToken, &nToken, &iBegin, &iEnd, &iPos); - if( rc ) break; - iRotorBegin[iRotor&FTS2_ROTOR_MASK] = iBegin; - iRotorLen[iRotor&FTS2_ROTOR_MASK] = iEnd-iBegin; - match = 0; - for(i=0; i=0 && iColnToken ) continue; - if( !aTerm[i].isPrefix && aTerm[i].nTerm1 && (prevMatch & (1<=0; j--){ - int k = (iRotor-j) & FTS2_ROTOR_MASK; - snippetAppendMatch(pSnippet, iColumn, i-j, - iRotorBegin[k], iRotorLen[k]); - } - } - } - prevMatch = match<<1; - iRotor++; - } - pTModule->xClose(pTCursor); -} - - -/* -** Compute all offsets for the current row of the query. -** If the offsets have already been computed, this routine is a no-op. -*/ -static void snippetAllOffsets(fulltext_cursor *p){ - int nColumn; - int iColumn, i; - int iFirst, iLast; - fulltext_vtab *pFts; - - if( p->snippet.nMatch ) return; - if( p->q.nTerms==0 ) return; - pFts = p->q.pFts; - nColumn = pFts->nColumn; - iColumn = (p->iCursorType - QUERY_FULLTEXT); - if( iColumn<0 || iColumn>=nColumn ){ - iFirst = 0; - iLast = nColumn-1; - }else{ - iFirst = iColumn; - iLast = iColumn; - } - for(i=iFirst; i<=iLast; i++){ - const char *zDoc; - int nDoc; - zDoc = (const char*)sqlite3_column_text(p->pStmt, i+1); - nDoc = sqlite3_column_bytes(p->pStmt, i+1); - snippetOffsetsOfColumn(&p->q, &p->snippet, i, zDoc, nDoc); - } -} - -/* -** Convert the information in the aMatch[] array of the snippet -** into the string zOffset[0..nOffset-1]. -*/ -static void snippetOffsetText(Snippet *p){ - int i; - int cnt = 0; - StringBuffer sb; - char zBuf[200]; - if( p->zOffset ) return; - initStringBuffer(&sb); - for(i=0; inMatch; i++){ - struct snippetMatch *pMatch = &p->aMatch[i]; - zBuf[0] = ' '; - sqlite3_snprintf(sizeof(zBuf)-1, &zBuf[cnt>0], "%d %d %d %d", - pMatch->iCol, pMatch->iTerm, pMatch->iStart, pMatch->nByte); - append(&sb, zBuf); - cnt++; - } - p->zOffset = stringBufferData(&sb); - p->nOffset = stringBufferLength(&sb); -} - -/* -** zDoc[0..nDoc-1] is phrase of text. aMatch[0..nMatch-1] are a set -** of matching words some of which might be in zDoc. zDoc is column -** number iCol. -** -** iBreak is suggested spot in zDoc where we could begin or end an -** excerpt. Return a value similar to iBreak but possibly adjusted -** to be a little left or right so that the break point is better. -*/ -static int wordBoundary( - int iBreak, /* The suggested break point */ - const char *zDoc, /* Document text */ - int nDoc, /* Number of bytes in zDoc[] */ - struct snippetMatch *aMatch, /* Matching words */ - int nMatch, /* Number of entries in aMatch[] */ - int iCol /* The column number for zDoc[] */ -){ - int i; - if( iBreak<=10 ){ - return 0; - } - if( iBreak>=nDoc-10 ){ - return nDoc; - } - for(i=0; i0 && aMatch[i-1].iStart+aMatch[i-1].nByte>=iBreak ){ - return aMatch[i-1].iStart; - } - } - for(i=1; i<=10; i++){ - if( safe_isspace(zDoc[iBreak-i]) ){ - return iBreak - i + 1; - } - if( safe_isspace(zDoc[iBreak+i]) ){ - return iBreak + i + 1; - } - } - return iBreak; -} - - - -/* -** Allowed values for Snippet.aMatch[].snStatus -*/ -#define SNIPPET_IGNORE 0 /* It is ok to omit this match from the snippet */ -#define SNIPPET_DESIRED 1 /* We want to include this match in the snippet */ - -/* -** Generate the text of a snippet. -*/ -static void snippetText( - fulltext_cursor *pCursor, /* The cursor we need the snippet for */ - const char *zStartMark, /* Markup to appear before each match */ - const char *zEndMark, /* Markup to appear after each match */ - const char *zEllipsis /* Ellipsis mark */ -){ - int i, j; - struct snippetMatch *aMatch; - int nMatch; - int nDesired; - StringBuffer sb; - int tailCol; - int tailOffset; - int iCol; - int nDoc; - const char *zDoc; - int iStart, iEnd; - int tailEllipsis = 0; - int iMatch; - - - sqlite3_free(pCursor->snippet.zSnippet); - pCursor->snippet.zSnippet = 0; - aMatch = pCursor->snippet.aMatch; - nMatch = pCursor->snippet.nMatch; - initStringBuffer(&sb); - - for(i=0; iq.nTerms; i++){ - for(j=0; j0; i++){ - if( aMatch[i].snStatus!=SNIPPET_DESIRED ) continue; - nDesired--; - iCol = aMatch[i].iCol; - zDoc = (const char*)sqlite3_column_text(pCursor->pStmt, iCol+1); - nDoc = sqlite3_column_bytes(pCursor->pStmt, iCol+1); - iStart = aMatch[i].iStart - 40; - iStart = wordBoundary(iStart, zDoc, nDoc, aMatch, nMatch, iCol); - if( iStart<=10 ){ - iStart = 0; - } - if( iCol==tailCol && iStart<=tailOffset+20 ){ - iStart = tailOffset; - } - if( (iCol!=tailCol && tailCol>=0) || iStart!=tailOffset ){ - trimWhiteSpace(&sb); - appendWhiteSpace(&sb); - append(&sb, zEllipsis); - appendWhiteSpace(&sb); - } - iEnd = aMatch[i].iStart + aMatch[i].nByte + 40; - iEnd = wordBoundary(iEnd, zDoc, nDoc, aMatch, nMatch, iCol); - if( iEnd>=nDoc-10 ){ - iEnd = nDoc; - tailEllipsis = 0; - }else{ - tailEllipsis = 1; - } - while( iMatchsnippet.zSnippet = stringBufferData(&sb); - pCursor->snippet.nSnippet = stringBufferLength(&sb); -} - - -/* -** Close the cursor. For additional information see the documentation -** on the xClose method of the virtual table interface. -*/ -static int fulltextClose(sqlite3_vtab_cursor *pCursor){ - fulltext_cursor *c = (fulltext_cursor *) pCursor; - TRACE(("FTS2 Close %p\n", c)); - sqlite3_finalize(c->pStmt); - queryClear(&c->q); - snippetClear(&c->snippet); - if( c->result.nData!=0 ) dlrDestroy(&c->reader); - dataBufferDestroy(&c->result); - sqlite3_free(c); - return SQLITE_OK; -} - -static int fulltextNext(sqlite3_vtab_cursor *pCursor){ - fulltext_cursor *c = (fulltext_cursor *) pCursor; - int rc; - - TRACE(("FTS2 Next %p\n", pCursor)); - snippetClear(&c->snippet); - if( c->iCursorType < QUERY_FULLTEXT ){ - /* TODO(shess) Handle SQLITE_SCHEMA AND SQLITE_BUSY. */ - rc = sqlite3_step(c->pStmt); - switch( rc ){ - case SQLITE_ROW: - c->eof = 0; - return SQLITE_OK; - case SQLITE_DONE: - c->eof = 1; - return SQLITE_OK; - default: - c->eof = 1; - return rc; - } - } else { /* full-text query */ - rc = sqlite3_reset(c->pStmt); - if( rc!=SQLITE_OK ) return rc; - - if( c->result.nData==0 || dlrAtEnd(&c->reader) ){ - c->eof = 1; - return SQLITE_OK; - } - rc = sqlite3_bind_int64(c->pStmt, 1, dlrDocid(&c->reader)); - dlrStep(&c->reader); - if( rc!=SQLITE_OK ) return rc; - /* TODO(shess) Handle SQLITE_SCHEMA AND SQLITE_BUSY. */ - rc = sqlite3_step(c->pStmt); - if( rc==SQLITE_ROW ){ /* the case we expect */ - c->eof = 0; - return SQLITE_OK; - } - /* an error occurred; abort */ - return rc==SQLITE_DONE ? SQLITE_ERROR : rc; - } -} - - -/* TODO(shess) If we pushed LeafReader to the top of the file, or to -** another file, term_select() could be pushed above -** docListOfTerm(). -*/ -static int termSelect(fulltext_vtab *v, int iColumn, - const char *pTerm, int nTerm, int isPrefix, - DocListType iType, DataBuffer *out); - -/* Return a DocList corresponding to the query term *pTerm. If *pTerm -** is the first term of a phrase query, go ahead and evaluate the phrase -** query and return the doclist for the entire phrase query. -** -** The resulting DL_DOCIDS doclist is stored in pResult, which is -** overwritten. -*/ -static int docListOfTerm( - fulltext_vtab *v, /* The full text index */ - int iColumn, /* column to restrict to. No restriction if >=nColumn */ - QueryTerm *pQTerm, /* Term we are looking for, or 1st term of a phrase */ - DataBuffer *pResult /* Write the result here */ -){ - DataBuffer left, right, new; - int i, rc; - - /* No phrase search if no position info. */ - assert( pQTerm->nPhrase==0 || DL_DEFAULT!=DL_DOCIDS ); - - /* This code should never be called with buffered updates. */ - assert( v->nPendingData<0 ); - - dataBufferInit(&left, 0); - rc = termSelect(v, iColumn, pQTerm->pTerm, pQTerm->nTerm, pQTerm->isPrefix, - 0nPhrase ? DL_POSITIONS : DL_DOCIDS, &left); - if( rc ) return rc; - for(i=1; i<=pQTerm->nPhrase && left.nData>0; i++){ - dataBufferInit(&right, 0); - rc = termSelect(v, iColumn, pQTerm[i].pTerm, pQTerm[i].nTerm, - pQTerm[i].isPrefix, DL_POSITIONS, &right); - if( rc ){ - dataBufferDestroy(&left); - return rc; - } - dataBufferInit(&new, 0); - docListPhraseMerge(left.pData, left.nData, right.pData, right.nData, - inPhrase ? DL_POSITIONS : DL_DOCIDS, &new); - dataBufferDestroy(&left); - dataBufferDestroy(&right); - left = new; - } - *pResult = left; - return SQLITE_OK; -} - -/* Add a new term pTerm[0..nTerm-1] to the query *q. -*/ -static void queryAdd(Query *q, const char *pTerm, int nTerm){ - QueryTerm *t; - ++q->nTerms; - q->pTerms = sqlite3_realloc(q->pTerms, q->nTerms * sizeof(q->pTerms[0])); - if( q->pTerms==0 ){ - q->nTerms = 0; - return; - } - t = &q->pTerms[q->nTerms - 1]; - CLEAR(t); - t->pTerm = sqlite3_malloc(nTerm+1); - memcpy(t->pTerm, pTerm, nTerm); - t->pTerm[nTerm] = 0; - t->nTerm = nTerm; - t->isOr = q->nextIsOr; - t->isPrefix = 0; - q->nextIsOr = 0; - t->iColumn = q->nextColumn; - q->nextColumn = q->dfltColumn; -} - -/* -** Check to see if the string zToken[0...nToken-1] matches any -** column name in the virtual table. If it does, -** return the zero-indexed column number. If not, return -1. -*/ -static int checkColumnSpecifier( - fulltext_vtab *pVtab, /* The virtual table */ - const char *zToken, /* Text of the token */ - int nToken /* Number of characters in the token */ -){ - int i; - for(i=0; inColumn; i++){ - if( memcmp(pVtab->azColumn[i], zToken, nToken)==0 - && pVtab->azColumn[i][nToken]==0 ){ - return i; - } - } - return -1; -} - -/* -** Parse the text at pSegment[0..nSegment-1]. Add additional terms -** to the query being assemblied in pQuery. -** -** inPhrase is true if pSegment[0..nSegement-1] is contained within -** double-quotes. If inPhrase is true, then the first term -** is marked with the number of terms in the phrase less one and -** OR and "-" syntax is ignored. If inPhrase is false, then every -** term found is marked with nPhrase=0 and OR and "-" syntax is significant. -*/ -static int tokenizeSegment( - sqlite3_tokenizer *pTokenizer, /* The tokenizer to use */ - const char *pSegment, int nSegment, /* Query expression being parsed */ - int inPhrase, /* True if within "..." */ - Query *pQuery /* Append results here */ -){ - const sqlite3_tokenizer_module *pModule = pTokenizer->pModule; - sqlite3_tokenizer_cursor *pCursor; - int firstIndex = pQuery->nTerms; - int iCol; - int nTerm = 1; - - int rc = pModule->xOpen(pTokenizer, pSegment, nSegment, &pCursor); - if( rc!=SQLITE_OK ) return rc; - pCursor->pTokenizer = pTokenizer; - - while( 1 ){ - const char *pToken; - int nToken, iBegin, iEnd, iPos; - - rc = pModule->xNext(pCursor, - &pToken, &nToken, - &iBegin, &iEnd, &iPos); - if( rc!=SQLITE_OK ) break; - if( !inPhrase && - pSegment[iEnd]==':' && - (iCol = checkColumnSpecifier(pQuery->pFts, pToken, nToken))>=0 ){ - pQuery->nextColumn = iCol; - continue; - } - if( !inPhrase && pQuery->nTerms>0 && nToken==2 - && pSegment[iBegin]=='O' && pSegment[iBegin+1]=='R' ){ - pQuery->nextIsOr = 1; - continue; - } - queryAdd(pQuery, pToken, nToken); - if( !inPhrase && iBegin>0 && pSegment[iBegin-1]=='-' ){ - pQuery->pTerms[pQuery->nTerms-1].isNot = 1; - } - if( iEndpTerms[pQuery->nTerms-1].isPrefix = 1; - } - pQuery->pTerms[pQuery->nTerms-1].iPhrase = nTerm; - if( inPhrase ){ - nTerm++; - } - } - - if( inPhrase && pQuery->nTerms>firstIndex ){ - pQuery->pTerms[firstIndex].nPhrase = pQuery->nTerms - firstIndex - 1; - } - - return pModule->xClose(pCursor); -} - -/* Parse a query string, yielding a Query object pQuery. -** -** The calling function will need to queryClear() to clean up -** the dynamically allocated memory held by pQuery. -*/ -static int parseQuery( - fulltext_vtab *v, /* The fulltext index */ - const char *zInput, /* Input text of the query string */ - int nInput, /* Size of the input text */ - int dfltColumn, /* Default column of the index to match against */ - Query *pQuery /* Write the parse results here. */ -){ - int iInput, inPhrase = 0; - - if( zInput==0 ) nInput = 0; - if( nInput<0 ) nInput = strlen(zInput); - pQuery->nTerms = 0; - pQuery->pTerms = NULL; - pQuery->nextIsOr = 0; - pQuery->nextColumn = dfltColumn; - pQuery->dfltColumn = dfltColumn; - pQuery->pFts = v; - - for(iInput=0; iInputiInput ){ - tokenizeSegment(v->pTokenizer, zInput+iInput, i-iInput, inPhrase, - pQuery); - } - iInput = i; - if( i=nColumn -** they are allowed to match against any column. -*/ -static int fulltextQuery( - fulltext_vtab *v, /* The full text index */ - int iColumn, /* Match against this column by default */ - const char *zInput, /* The query string */ - int nInput, /* Number of bytes in zInput[] */ - DataBuffer *pResult, /* Write the result doclist here */ - Query *pQuery /* Put parsed query string here */ -){ - int i, iNext, rc; - DataBuffer left, right, or, new; - int nNot = 0; - QueryTerm *aTerm; - - /* TODO(shess) Instead of flushing pendingTerms, we could query for - ** the relevant term and merge the doclist into what we receive from - ** the database. Wait and see if this is a common issue, first. - ** - ** A good reason not to flush is to not generate update-related - ** error codes from here. - */ - - /* Flush any buffered updates before executing the query. */ - rc = flushPendingTerms(v); - if( rc!=SQLITE_OK ) return rc; - - /* TODO(shess) I think that the queryClear() calls below are not - ** necessary, because fulltextClose() already clears the query. - */ - rc = parseQuery(v, zInput, nInput, iColumn, pQuery); - if( rc!=SQLITE_OK ) return rc; - - /* Empty or NULL queries return no results. */ - if( pQuery->nTerms==0 ){ - dataBufferInit(pResult, 0); - return SQLITE_OK; - } - - /* Merge AND terms. */ - /* TODO(shess) I think we can early-exit if( i>nNot && left.nData==0 ). */ - aTerm = pQuery->pTerms; - for(i = 0; inTerms; i=iNext){ - if( aTerm[i].isNot ){ - /* Handle all NOT terms in a separate pass */ - nNot++; - iNext = i + aTerm[i].nPhrase+1; - continue; - } - iNext = i + aTerm[i].nPhrase + 1; - rc = docListOfTerm(v, aTerm[i].iColumn, &aTerm[i], &right); - if( rc ){ - if( i!=nNot ) dataBufferDestroy(&left); - queryClear(pQuery); - return rc; - } - while( iNextnTerms && aTerm[iNext].isOr ){ - rc = docListOfTerm(v, aTerm[iNext].iColumn, &aTerm[iNext], &or); - iNext += aTerm[iNext].nPhrase + 1; - if( rc ){ - if( i!=nNot ) dataBufferDestroy(&left); - dataBufferDestroy(&right); - queryClear(pQuery); - return rc; - } - dataBufferInit(&new, 0); - docListOrMerge(right.pData, right.nData, or.pData, or.nData, &new); - dataBufferDestroy(&right); - dataBufferDestroy(&or); - right = new; - } - if( i==nNot ){ /* first term processed. */ - left = right; - }else{ - dataBufferInit(&new, 0); - docListAndMerge(left.pData, left.nData, right.pData, right.nData, &new); - dataBufferDestroy(&right); - dataBufferDestroy(&left); - left = new; - } - } - - if( nNot==pQuery->nTerms ){ - /* We do not yet know how to handle a query of only NOT terms */ - return SQLITE_ERROR; - } - - /* Do the EXCEPT terms */ - for(i=0; inTerms; i += aTerm[i].nPhrase + 1){ - if( !aTerm[i].isNot ) continue; - rc = docListOfTerm(v, aTerm[i].iColumn, &aTerm[i], &right); - if( rc ){ - queryClear(pQuery); - dataBufferDestroy(&left); - return rc; - } - dataBufferInit(&new, 0); - docListExceptMerge(left.pData, left.nData, right.pData, right.nData, &new); - dataBufferDestroy(&right); - dataBufferDestroy(&left); - left = new; - } - - *pResult = left; - return rc; -} - -/* -** This is the xFilter interface for the virtual table. See -** the virtual table xFilter method documentation for additional -** information. -** -** If idxNum==QUERY_GENERIC then do a full table scan against -** the %_content table. -** -** If idxNum==QUERY_ROWID then do a rowid lookup for a single entry -** in the %_content table. -** -** If idxNum>=QUERY_FULLTEXT then use the full text index. The -** column on the left-hand side of the MATCH operator is column -** number idxNum-QUERY_FULLTEXT, 0 indexed. argv[0] is the right-hand -** side of the MATCH operator. -*/ -/* TODO(shess) Upgrade the cursor initialization and destruction to -** account for fulltextFilter() being called multiple times on the -** same cursor. The current solution is very fragile. Apply fix to -** fts2 as appropriate. -*/ -static int fulltextFilter( - sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */ - int idxNum, const char *idxStr, /* Which indexing scheme to use */ - int argc, sqlite3_value **argv /* Arguments for the indexing scheme */ -){ - fulltext_cursor *c = (fulltext_cursor *) pCursor; - fulltext_vtab *v = cursor_vtab(c); - int rc; - - TRACE(("FTS2 Filter %p\n",pCursor)); - - /* If the cursor has a statement that was not prepared according to - ** idxNum, clear it. I believe all calls to fulltextFilter with a - ** given cursor will have the same idxNum , but in this case it's - ** easy to be safe. - */ - if( c->pStmt && c->iCursorType!=idxNum ){ - sqlite3_finalize(c->pStmt); - c->pStmt = NULL; - } - - /* Get a fresh statement appropriate to idxNum. */ - /* TODO(shess): Add a prepared-statement cache in the vt structure. - ** The cache must handle multiple open cursors. Easier to cache the - ** statement variants at the vt to reduce malloc/realloc/free here. - ** Or we could have a StringBuffer variant which allowed stack - ** construction for small values. - */ - if( !c->pStmt ){ - char *zSql = sqlite3_mprintf("select rowid, * from %%_content %s", - idxNum==QUERY_GENERIC ? "" : "where rowid=?"); - rc = sql_prepare(v->db, v->zDb, v->zName, &c->pStmt, zSql); - sqlite3_free(zSql); - if( rc!=SQLITE_OK ) return rc; - c->iCursorType = idxNum; - }else{ - sqlite3_reset(c->pStmt); - assert( c->iCursorType==idxNum ); - } - - switch( idxNum ){ - case QUERY_GENERIC: - break; - - case QUERY_ROWID: - rc = sqlite3_bind_int64(c->pStmt, 1, sqlite3_value_int64(argv[0])); - if( rc!=SQLITE_OK ) return rc; - break; - - default: /* full-text search */ - { - const char *zQuery = (const char *)sqlite3_value_text(argv[0]); - assert( idxNum<=QUERY_FULLTEXT+v->nColumn); - assert( argc==1 ); - queryClear(&c->q); - if( c->result.nData!=0 ){ - /* This case happens if the same cursor is used repeatedly. */ - dlrDestroy(&c->reader); - dataBufferReset(&c->result); - }else{ - dataBufferInit(&c->result, 0); - } - rc = fulltextQuery(v, idxNum-QUERY_FULLTEXT, zQuery, -1, &c->result, &c->q); - if( rc!=SQLITE_OK ) return rc; - if( c->result.nData!=0 ){ - dlrInit(&c->reader, DL_DOCIDS, c->result.pData, c->result.nData); - } - break; - } - } - - return fulltextNext(pCursor); -} - -/* This is the xEof method of the virtual table. The SQLite core -** calls this routine to find out if it has reached the end of -** a query's results set. -*/ -static int fulltextEof(sqlite3_vtab_cursor *pCursor){ - fulltext_cursor *c = (fulltext_cursor *) pCursor; - return c->eof; -} - -/* This is the xColumn method of the virtual table. The SQLite -** core calls this method during a query when it needs the value -** of a column from the virtual table. This method needs to use -** one of the sqlite3_result_*() routines to store the requested -** value back in the pContext. -*/ -static int fulltextColumn(sqlite3_vtab_cursor *pCursor, - sqlite3_context *pContext, int idxCol){ - fulltext_cursor *c = (fulltext_cursor *) pCursor; - fulltext_vtab *v = cursor_vtab(c); - - if( idxColnColumn ){ - sqlite3_value *pVal = sqlite3_column_value(c->pStmt, idxCol+1); - sqlite3_result_value(pContext, pVal); - }else if( idxCol==v->nColumn ){ - /* The extra column whose name is the same as the table. - ** Return a blob which is a pointer to the cursor - */ - sqlite3_result_blob(pContext, &c, sizeof(c), SQLITE_TRANSIENT); - } - return SQLITE_OK; -} - -/* This is the xRowid method. The SQLite core calls this routine to -** retrive the rowid for the current row of the result set. The -** rowid should be written to *pRowid. -*/ -static int fulltextRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){ - fulltext_cursor *c = (fulltext_cursor *) pCursor; - - *pRowid = sqlite3_column_int64(c->pStmt, 0); - return SQLITE_OK; -} - -/* Add all terms in [zText] to pendingTerms table. If [iColumn] > 0, -** we also store positions and offsets in the hash table using that -** column number. -*/ -static int buildTerms(fulltext_vtab *v, sqlite_int64 iDocid, - const char *zText, int iColumn){ - sqlite3_tokenizer *pTokenizer = v->pTokenizer; - sqlite3_tokenizer_cursor *pCursor; - const char *pToken; - int nTokenBytes; - int iStartOffset, iEndOffset, iPosition; - int rc; - - rc = pTokenizer->pModule->xOpen(pTokenizer, zText, -1, &pCursor); - if( rc!=SQLITE_OK ) return rc; - - pCursor->pTokenizer = pTokenizer; - while( SQLITE_OK==(rc=pTokenizer->pModule->xNext(pCursor, - &pToken, &nTokenBytes, - &iStartOffset, &iEndOffset, - &iPosition)) ){ - DLCollector *p; - int nData; /* Size of doclist before our update. */ - - /* Positions can't be negative; we use -1 as a terminator - * internally. Token can't be NULL or empty. */ - if( iPosition<0 || pToken == NULL || nTokenBytes == 0 ){ - rc = SQLITE_ERROR; - break; - } - - p = fts2HashFind(&v->pendingTerms, pToken, nTokenBytes); - if( p==NULL ){ - nData = 0; - p = dlcNew(iDocid, DL_DEFAULT); - fts2HashInsert(&v->pendingTerms, pToken, nTokenBytes, p); - - /* Overhead for our hash table entry, the key, and the value. */ - v->nPendingData += sizeof(struct fts2HashElem)+sizeof(*p)+nTokenBytes; - }else{ - nData = p->b.nData; - if( p->dlw.iPrevDocid!=iDocid ) dlcNext(p, iDocid); - } - if( iColumn>=0 ){ - dlcAddPos(p, iColumn, iPosition, iStartOffset, iEndOffset); - } - - /* Accumulate data added by dlcNew or dlcNext, and dlcAddPos. */ - v->nPendingData += p->b.nData-nData; - } - - /* TODO(shess) Check return? Should this be able to cause errors at - ** this point? Actually, same question about sqlite3_finalize(), - ** though one could argue that failure there means that the data is - ** not durable. *ponder* - */ - pTokenizer->pModule->xClose(pCursor); - if( SQLITE_DONE == rc ) return SQLITE_OK; - return rc; -} - -/* Add doclists for all terms in [pValues] to pendingTerms table. */ -static int insertTerms(fulltext_vtab *v, sqlite_int64 iRowid, - sqlite3_value **pValues){ - int i; - for(i = 0; i < v->nColumn ; ++i){ - char *zText = (char*)sqlite3_value_text(pValues[i]); - int rc = buildTerms(v, iRowid, zText, i); - if( rc!=SQLITE_OK ) return rc; - } - return SQLITE_OK; -} - -/* Add empty doclists for all terms in the given row's content to -** pendingTerms. -*/ -static int deleteTerms(fulltext_vtab *v, sqlite_int64 iRowid){ - const char **pValues; - int i, rc; - - /* TODO(shess) Should we allow such tables at all? */ - if( DL_DEFAULT==DL_DOCIDS ) return SQLITE_ERROR; - - rc = content_select(v, iRowid, &pValues); - if( rc!=SQLITE_OK ) return rc; - - for(i = 0 ; i < v->nColumn; ++i) { - rc = buildTerms(v, iRowid, pValues[i], -1); - if( rc!=SQLITE_OK ) break; - } - - freeStringArray(v->nColumn, pValues); - return SQLITE_OK; -} - -/* TODO(shess) Refactor the code to remove this forward decl. */ -static int initPendingTerms(fulltext_vtab *v, sqlite_int64 iDocid); - -/* Insert a row into the %_content table; set *piRowid to be the ID of the -** new row. Add doclists for terms to pendingTerms. -*/ -static int index_insert(fulltext_vtab *v, sqlite3_value *pRequestRowid, - sqlite3_value **pValues, sqlite_int64 *piRowid){ - int rc; - - rc = content_insert(v, pRequestRowid, pValues); /* execute an SQL INSERT */ - if( rc!=SQLITE_OK ) return rc; - - *piRowid = sqlite3_last_insert_rowid(v->db); - rc = initPendingTerms(v, *piRowid); - if( rc!=SQLITE_OK ) return rc; - - return insertTerms(v, *piRowid, pValues); -} - -/* Delete a row from the %_content table; add empty doclists for terms -** to pendingTerms. -*/ -static int index_delete(fulltext_vtab *v, sqlite_int64 iRow){ - int rc = initPendingTerms(v, iRow); - if( rc!=SQLITE_OK ) return rc; - - rc = deleteTerms(v, iRow); - if( rc!=SQLITE_OK ) return rc; - - return content_delete(v, iRow); /* execute an SQL DELETE */ -} - -/* Update a row in the %_content table; add delete doclists to -** pendingTerms for old terms not in the new data, add insert doclists -** to pendingTerms for terms in the new data. -*/ -static int index_update(fulltext_vtab *v, sqlite_int64 iRow, - sqlite3_value **pValues){ - int rc = initPendingTerms(v, iRow); - if( rc!=SQLITE_OK ) return rc; - - /* Generate an empty doclist for each term that previously appeared in this - * row. */ - rc = deleteTerms(v, iRow); - if( rc!=SQLITE_OK ) return rc; - - rc = content_update(v, pValues, iRow); /* execute an SQL UPDATE */ - if( rc!=SQLITE_OK ) return rc; - - /* Now add positions for terms which appear in the updated row. */ - return insertTerms(v, iRow, pValues); -} - -/*******************************************************************/ -/* InteriorWriter is used to collect terms and block references into -** interior nodes in %_segments. See commentary at top of file for -** format. -*/ - -/* How large interior nodes can grow. */ -#define INTERIOR_MAX 2048 - -/* Minimum number of terms per interior node (except the root). This -** prevents large terms from making the tree too skinny - must be >0 -** so that the tree always makes progress. Note that the min tree -** fanout will be INTERIOR_MIN_TERMS+1. -*/ -#define INTERIOR_MIN_TERMS 7 -#if INTERIOR_MIN_TERMS<1 -# error INTERIOR_MIN_TERMS must be greater than 0. -#endif - -/* ROOT_MAX controls how much data is stored inline in the segment -** directory. -*/ -/* TODO(shess) Push ROOT_MAX down to whoever is writing things. It's -** only here so that interiorWriterRootInfo() and leafWriterRootInfo() -** can both see it, but if the caller passed it in, we wouldn't even -** need a define. -*/ -#define ROOT_MAX 1024 -#if ROOT_MAXterm, 0); - dataBufferReplace(&block->term, pTerm, nTerm); - - n = putVarint(c, iHeight); - n += putVarint(c+n, iChildBlock); - dataBufferInit(&block->data, INTERIOR_MAX); - dataBufferReplace(&block->data, c, n); - } - return block; -} - -#ifndef NDEBUG -/* Verify that the data is readable as an interior node. */ -static void interiorBlockValidate(InteriorBlock *pBlock){ - const char *pData = pBlock->data.pData; - int nData = pBlock->data.nData; - int n, iDummy; - sqlite_int64 iBlockid; - - assert( nData>0 ); - assert( pData!=0 ); - assert( pData+nData>pData ); - - /* Must lead with height of node as a varint(n), n>0 */ - n = getVarint32(pData, &iDummy); - assert( n>0 ); - assert( iDummy>0 ); - assert( n0 ); - assert( n<=nData ); - pData += n; - nData -= n; - - /* Zero or more terms of positive length */ - if( nData!=0 ){ - /* First term is not delta-encoded. */ - n = getVarint32(pData, &iDummy); - assert( n>0 ); - assert( iDummy>0 ); - assert( n+iDummy>0); - assert( n+iDummy<=nData ); - pData += n+iDummy; - nData -= n+iDummy; - - /* Following terms delta-encoded. */ - while( nData!=0 ){ - /* Length of shared prefix. */ - n = getVarint32(pData, &iDummy); - assert( n>0 ); - assert( iDummy>=0 ); - assert( n0 ); - assert( iDummy>0 ); - assert( n+iDummy>0); - assert( n+iDummy<=nData ); - pData += n+iDummy; - nData -= n+iDummy; - } - } -} -#define ASSERT_VALID_INTERIOR_BLOCK(x) interiorBlockValidate(x) -#else -#define ASSERT_VALID_INTERIOR_BLOCK(x) assert( 1 ) -#endif - -typedef struct InteriorWriter { - int iHeight; /* from 0 at leaves. */ - InteriorBlock *first, *last; - struct InteriorWriter *parentWriter; - - DataBuffer term; /* Last term written to block "last". */ - sqlite_int64 iOpeningChildBlock; /* First child block in block "last". */ -#ifndef NDEBUG - sqlite_int64 iLastChildBlock; /* for consistency checks. */ -#endif -} InteriorWriter; - -/* Initialize an interior node where pTerm[nTerm] marks the leftmost -** term in the tree. iChildBlock is the leftmost child block at the -** next level down the tree. -*/ -static void interiorWriterInit(int iHeight, const char *pTerm, int nTerm, - sqlite_int64 iChildBlock, - InteriorWriter *pWriter){ - InteriorBlock *block; - assert( iHeight>0 ); - CLEAR(pWriter); - - pWriter->iHeight = iHeight; - pWriter->iOpeningChildBlock = iChildBlock; -#ifndef NDEBUG - pWriter->iLastChildBlock = iChildBlock; -#endif - block = interiorBlockNew(iHeight, iChildBlock, pTerm, nTerm); - pWriter->last = pWriter->first = block; - ASSERT_VALID_INTERIOR_BLOCK(pWriter->last); - dataBufferInit(&pWriter->term, 0); -} - -/* Append the child node rooted at iChildBlock to the interior node, -** with pTerm[nTerm] as the leftmost term in iChildBlock's subtree. -*/ -static void interiorWriterAppend(InteriorWriter *pWriter, - const char *pTerm, int nTerm, - sqlite_int64 iChildBlock){ - char c[VARINT_MAX+VARINT_MAX]; - int n, nPrefix = 0; - - ASSERT_VALID_INTERIOR_BLOCK(pWriter->last); - - /* The first term written into an interior node is actually - ** associated with the second child added (the first child was added - ** in interiorWriterInit, or in the if clause at the bottom of this - ** function). That term gets encoded straight up, with nPrefix left - ** at 0. - */ - if( pWriter->term.nData==0 ){ - n = putVarint(c, nTerm); - }else{ - while( nPrefixterm.nData && - pTerm[nPrefix]==pWriter->term.pData[nPrefix] ){ - nPrefix++; - } - - n = putVarint(c, nPrefix); - n += putVarint(c+n, nTerm-nPrefix); - } - -#ifndef NDEBUG - pWriter->iLastChildBlock++; -#endif - assert( pWriter->iLastChildBlock==iChildBlock ); - - /* Overflow to a new block if the new term makes the current block - ** too big, and the current block already has enough terms. - */ - if( pWriter->last->data.nData+n+nTerm-nPrefix>INTERIOR_MAX && - iChildBlock-pWriter->iOpeningChildBlock>INTERIOR_MIN_TERMS ){ - pWriter->last->next = interiorBlockNew(pWriter->iHeight, iChildBlock, - pTerm, nTerm); - pWriter->last = pWriter->last->next; - pWriter->iOpeningChildBlock = iChildBlock; - dataBufferReset(&pWriter->term); - }else{ - dataBufferAppend2(&pWriter->last->data, c, n, - pTerm+nPrefix, nTerm-nPrefix); - dataBufferReplace(&pWriter->term, pTerm, nTerm); - } - ASSERT_VALID_INTERIOR_BLOCK(pWriter->last); -} - -/* Free the space used by pWriter, including the linked-list of -** InteriorBlocks, and parentWriter, if present. -*/ -static int interiorWriterDestroy(InteriorWriter *pWriter){ - InteriorBlock *block = pWriter->first; - - while( block!=NULL ){ - InteriorBlock *b = block; - block = block->next; - dataBufferDestroy(&b->term); - dataBufferDestroy(&b->data); - sqlite3_free(b); - } - if( pWriter->parentWriter!=NULL ){ - interiorWriterDestroy(pWriter->parentWriter); - sqlite3_free(pWriter->parentWriter); - } - dataBufferDestroy(&pWriter->term); - SCRAMBLE(pWriter); - return SQLITE_OK; -} - -/* If pWriter can fit entirely in ROOT_MAX, return it as the root info -** directly, leaving *piEndBlockid unchanged. Otherwise, flush -** pWriter to %_segments, building a new layer of interior nodes, and -** recursively ask for their root into. -*/ -static int interiorWriterRootInfo(fulltext_vtab *v, InteriorWriter *pWriter, - char **ppRootInfo, int *pnRootInfo, - sqlite_int64 *piEndBlockid){ - InteriorBlock *block = pWriter->first; - sqlite_int64 iBlockid = 0; - int rc; - - /* If we can fit the segment inline */ - if( block==pWriter->last && block->data.nDatadata.pData; - *pnRootInfo = block->data.nData; - return SQLITE_OK; - } - - /* Flush the first block to %_segments, and create a new level of - ** interior node. - */ - ASSERT_VALID_INTERIOR_BLOCK(block); - rc = block_insert(v, block->data.pData, block->data.nData, &iBlockid); - if( rc!=SQLITE_OK ) return rc; - *piEndBlockid = iBlockid; - - pWriter->parentWriter = sqlite3_malloc(sizeof(*pWriter->parentWriter)); - interiorWriterInit(pWriter->iHeight+1, - block->term.pData, block->term.nData, - iBlockid, pWriter->parentWriter); - - /* Flush additional blocks and append to the higher interior - ** node. - */ - for(block=block->next; block!=NULL; block=block->next){ - ASSERT_VALID_INTERIOR_BLOCK(block); - rc = block_insert(v, block->data.pData, block->data.nData, &iBlockid); - if( rc!=SQLITE_OK ) return rc; - *piEndBlockid = iBlockid; - - interiorWriterAppend(pWriter->parentWriter, - block->term.pData, block->term.nData, iBlockid); - } - - /* Parent node gets the chance to be the root. */ - return interiorWriterRootInfo(v, pWriter->parentWriter, - ppRootInfo, pnRootInfo, piEndBlockid); -} - -/****************************************************************/ -/* InteriorReader is used to read off the data from an interior node -** (see comment at top of file for the format). -*/ -typedef struct InteriorReader { - const char *pData; - int nData; - - DataBuffer term; /* previous term, for decoding term delta. */ - - sqlite_int64 iBlockid; -} InteriorReader; - -static void interiorReaderDestroy(InteriorReader *pReader){ - dataBufferDestroy(&pReader->term); - SCRAMBLE(pReader); -} - -/* TODO(shess) The assertions are great, but what if we're in NDEBUG -** and the blob is empty or otherwise contains suspect data? -*/ -static void interiorReaderInit(const char *pData, int nData, - InteriorReader *pReader){ - int n, nTerm; - - /* Require at least the leading flag byte */ - assert( nData>0 ); - assert( pData[0]!='\0' ); - - CLEAR(pReader); - - /* Decode the base blockid, and set the cursor to the first term. */ - n = getVarint(pData+1, &pReader->iBlockid); - assert( 1+n<=nData ); - pReader->pData = pData+1+n; - pReader->nData = nData-(1+n); - - /* A single-child interior node (such as when a leaf node was too - ** large for the segment directory) won't have any terms. - ** Otherwise, decode the first term. - */ - if( pReader->nData==0 ){ - dataBufferInit(&pReader->term, 0); - }else{ - n = getVarint32(pReader->pData, &nTerm); - dataBufferInit(&pReader->term, nTerm); - dataBufferReplace(&pReader->term, pReader->pData+n, nTerm); - assert( n+nTerm<=pReader->nData ); - pReader->pData += n+nTerm; - pReader->nData -= n+nTerm; - } -} - -static int interiorReaderAtEnd(InteriorReader *pReader){ - return pReader->term.nData==0; -} - -static sqlite_int64 interiorReaderCurrentBlockid(InteriorReader *pReader){ - return pReader->iBlockid; -} - -static int interiorReaderTermBytes(InteriorReader *pReader){ - assert( !interiorReaderAtEnd(pReader) ); - return pReader->term.nData; -} -static const char *interiorReaderTerm(InteriorReader *pReader){ - assert( !interiorReaderAtEnd(pReader) ); - return pReader->term.pData; -} - -/* Step forward to the next term in the node. */ -static void interiorReaderStep(InteriorReader *pReader){ - assert( !interiorReaderAtEnd(pReader) ); - - /* If the last term has been read, signal eof, else construct the - ** next term. - */ - if( pReader->nData==0 ){ - dataBufferReset(&pReader->term); - }else{ - int n, nPrefix, nSuffix; - - n = getVarint32(pReader->pData, &nPrefix); - n += getVarint32(pReader->pData+n, &nSuffix); - - /* Truncate the current term and append suffix data. */ - pReader->term.nData = nPrefix; - dataBufferAppend(&pReader->term, pReader->pData+n, nSuffix); - - assert( n+nSuffix<=pReader->nData ); - pReader->pData += n+nSuffix; - pReader->nData -= n+nSuffix; - } - pReader->iBlockid++; -} - -/* Compare the current term to pTerm[nTerm], returning strcmp-style -** results. If isPrefix, equality means equal through nTerm bytes. -*/ -static int interiorReaderTermCmp(InteriorReader *pReader, - const char *pTerm, int nTerm, int isPrefix){ - const char *pReaderTerm = interiorReaderTerm(pReader); - int nReaderTerm = interiorReaderTermBytes(pReader); - int c, n = nReaderTerm0 ) return -1; - if( nTerm>0 ) return 1; - return 0; - } - - c = memcmp(pReaderTerm, pTerm, n); - if( c!=0 ) return c; - if( isPrefix && n==nTerm ) return 0; - return nReaderTerm - nTerm; -} - -/****************************************************************/ -/* LeafWriter is used to collect terms and associated doclist data -** into leaf blocks in %_segments (see top of file for format info). -** Expected usage is: -** -** LeafWriter writer; -** leafWriterInit(0, 0, &writer); -** while( sorted_terms_left_to_process ){ -** // data is doclist data for that term. -** rc = leafWriterStep(v, &writer, pTerm, nTerm, pData, nData); -** if( rc!=SQLITE_OK ) goto err; -** } -** rc = leafWriterFinalize(v, &writer); -**err: -** leafWriterDestroy(&writer); -** return rc; -** -** leafWriterStep() may write a collected leaf out to %_segments. -** leafWriterFinalize() finishes writing any buffered data and stores -** a root node in %_segdir. leafWriterDestroy() frees all buffers and -** InteriorWriters allocated as part of writing this segment. -** -** TODO(shess) Document leafWriterStepMerge(). -*/ - -/* Put terms with data this big in their own block. */ -#define STANDALONE_MIN 1024 - -/* Keep leaf blocks below this size. */ -#define LEAF_MAX 2048 - -typedef struct LeafWriter { - int iLevel; - int idx; - sqlite_int64 iStartBlockid; /* needed to create the root info */ - sqlite_int64 iEndBlockid; /* when we're done writing. */ - - DataBuffer term; /* previous encoded term */ - DataBuffer data; /* encoding buffer */ - - /* bytes of first term in the current node which distinguishes that - ** term from the last term of the previous node. - */ - int nTermDistinct; - - InteriorWriter parentWriter; /* if we overflow */ - int has_parent; -} LeafWriter; - -static void leafWriterInit(int iLevel, int idx, LeafWriter *pWriter){ - CLEAR(pWriter); - pWriter->iLevel = iLevel; - pWriter->idx = idx; - - dataBufferInit(&pWriter->term, 32); - - /* Start out with a reasonably sized block, though it can grow. */ - dataBufferInit(&pWriter->data, LEAF_MAX); -} - -#ifndef NDEBUG -/* Verify that the data is readable as a leaf node. */ -static void leafNodeValidate(const char *pData, int nData){ - int n, iDummy; - - if( nData==0 ) return; - assert( nData>0 ); - assert( pData!=0 ); - assert( pData+nData>pData ); - - /* Must lead with a varint(0) */ - n = getVarint32(pData, &iDummy); - assert( iDummy==0 ); - assert( n>0 ); - assert( n0 ); - assert( iDummy>0 ); - assert( n+iDummy>0 ); - assert( n+iDummy0 ); - assert( iDummy>0 ); - assert( n+iDummy>0 ); - assert( n+iDummy<=nData ); - ASSERT_VALID_DOCLIST(DL_DEFAULT, pData+n, iDummy, NULL); - pData += n+iDummy; - nData -= n+iDummy; - - /* Verify that trailing terms and doclists also are readable. */ - while( nData!=0 ){ - n = getVarint32(pData, &iDummy); - assert( n>0 ); - assert( iDummy>=0 ); - assert( n0 ); - assert( iDummy>0 ); - assert( n+iDummy>0 ); - assert( n+iDummy0 ); - assert( iDummy>0 ); - assert( n+iDummy>0 ); - assert( n+iDummy<=nData ); - ASSERT_VALID_DOCLIST(DL_DEFAULT, pData+n, iDummy, NULL); - pData += n+iDummy; - nData -= n+iDummy; - } -} -#define ASSERT_VALID_LEAF_NODE(p, n) leafNodeValidate(p, n) -#else -#define ASSERT_VALID_LEAF_NODE(p, n) assert( 1 ) -#endif - -/* Flush the current leaf node to %_segments, and adding the resulting -** blockid and the starting term to the interior node which will -** contain it. -*/ -static int leafWriterInternalFlush(fulltext_vtab *v, LeafWriter *pWriter, - int iData, int nData){ - sqlite_int64 iBlockid = 0; - const char *pStartingTerm; - int nStartingTerm, rc, n; - - /* Must have the leading varint(0) flag, plus at least some - ** valid-looking data. - */ - assert( nData>2 ); - assert( iData>=0 ); - assert( iData+nData<=pWriter->data.nData ); - ASSERT_VALID_LEAF_NODE(pWriter->data.pData+iData, nData); - - rc = block_insert(v, pWriter->data.pData+iData, nData, &iBlockid); - if( rc!=SQLITE_OK ) return rc; - assert( iBlockid!=0 ); - - /* Reconstruct the first term in the leaf for purposes of building - ** the interior node. - */ - n = getVarint32(pWriter->data.pData+iData+1, &nStartingTerm); - pStartingTerm = pWriter->data.pData+iData+1+n; - assert( pWriter->data.nData>iData+1+n+nStartingTerm ); - assert( pWriter->nTermDistinct>0 ); - assert( pWriter->nTermDistinct<=nStartingTerm ); - nStartingTerm = pWriter->nTermDistinct; - - if( pWriter->has_parent ){ - interiorWriterAppend(&pWriter->parentWriter, - pStartingTerm, nStartingTerm, iBlockid); - }else{ - interiorWriterInit(1, pStartingTerm, nStartingTerm, iBlockid, - &pWriter->parentWriter); - pWriter->has_parent = 1; - } - - /* Track the span of this segment's leaf nodes. */ - if( pWriter->iEndBlockid==0 ){ - pWriter->iEndBlockid = pWriter->iStartBlockid = iBlockid; - }else{ - pWriter->iEndBlockid++; - assert( iBlockid==pWriter->iEndBlockid ); - } - - return SQLITE_OK; -} -static int leafWriterFlush(fulltext_vtab *v, LeafWriter *pWriter){ - int rc = leafWriterInternalFlush(v, pWriter, 0, pWriter->data.nData); - if( rc!=SQLITE_OK ) return rc; - - /* Re-initialize the output buffer. */ - dataBufferReset(&pWriter->data); - - return SQLITE_OK; -} - -/* Fetch the root info for the segment. If the entire leaf fits -** within ROOT_MAX, then it will be returned directly, otherwise it -** will be flushed and the root info will be returned from the -** interior node. *piEndBlockid is set to the blockid of the last -** interior or leaf node written to disk (0 if none are written at -** all). -*/ -static int leafWriterRootInfo(fulltext_vtab *v, LeafWriter *pWriter, - char **ppRootInfo, int *pnRootInfo, - sqlite_int64 *piEndBlockid){ - /* we can fit the segment entirely inline */ - if( !pWriter->has_parent && pWriter->data.nDatadata.pData; - *pnRootInfo = pWriter->data.nData; - *piEndBlockid = 0; - return SQLITE_OK; - } - - /* Flush remaining leaf data. */ - if( pWriter->data.nData>0 ){ - int rc = leafWriterFlush(v, pWriter); - if( rc!=SQLITE_OK ) return rc; - } - - /* We must have flushed a leaf at some point. */ - assert( pWriter->has_parent ); - - /* Tenatively set the end leaf blockid as the end blockid. If the - ** interior node can be returned inline, this will be the final - ** blockid, otherwise it will be overwritten by - ** interiorWriterRootInfo(). - */ - *piEndBlockid = pWriter->iEndBlockid; - - return interiorWriterRootInfo(v, &pWriter->parentWriter, - ppRootInfo, pnRootInfo, piEndBlockid); -} - -/* Collect the rootInfo data and store it into the segment directory. -** This has the effect of flushing the segment's leaf data to -** %_segments, and also flushing any interior nodes to %_segments. -*/ -static int leafWriterFinalize(fulltext_vtab *v, LeafWriter *pWriter){ - sqlite_int64 iEndBlockid; - char *pRootInfo; - int rc, nRootInfo; - - rc = leafWriterRootInfo(v, pWriter, &pRootInfo, &nRootInfo, &iEndBlockid); - if( rc!=SQLITE_OK ) return rc; - - /* Don't bother storing an entirely empty segment. */ - if( iEndBlockid==0 && nRootInfo==0 ) return SQLITE_OK; - - return segdir_set(v, pWriter->iLevel, pWriter->idx, - pWriter->iStartBlockid, pWriter->iEndBlockid, - iEndBlockid, pRootInfo, nRootInfo); -} - -static void leafWriterDestroy(LeafWriter *pWriter){ - if( pWriter->has_parent ) interiorWriterDestroy(&pWriter->parentWriter); - dataBufferDestroy(&pWriter->term); - dataBufferDestroy(&pWriter->data); -} - -/* Encode a term into the leafWriter, delta-encoding as appropriate. -** Returns the length of the new term which distinguishes it from the -** previous term, which can be used to set nTermDistinct when a node -** boundary is crossed. -*/ -static int leafWriterEncodeTerm(LeafWriter *pWriter, - const char *pTerm, int nTerm){ - char c[VARINT_MAX+VARINT_MAX]; - int n, nPrefix = 0; - - assert( nTerm>0 ); - while( nPrefixterm.nData && - pTerm[nPrefix]==pWriter->term.pData[nPrefix] ){ - nPrefix++; - /* Failing this implies that the terms weren't in order. */ - assert( nPrefixdata.nData==0 ){ - /* Encode the node header and leading term as: - ** varint(0) - ** varint(nTerm) - ** char pTerm[nTerm] - */ - n = putVarint(c, '\0'); - n += putVarint(c+n, nTerm); - dataBufferAppend2(&pWriter->data, c, n, pTerm, nTerm); - }else{ - /* Delta-encode the term as: - ** varint(nPrefix) - ** varint(nSuffix) - ** char pTermSuffix[nSuffix] - */ - n = putVarint(c, nPrefix); - n += putVarint(c+n, nTerm-nPrefix); - dataBufferAppend2(&pWriter->data, c, n, pTerm+nPrefix, nTerm-nPrefix); - } - dataBufferReplace(&pWriter->term, pTerm, nTerm); - - return nPrefix+1; -} - -/* Used to avoid a memmove when a large amount of doclist data is in -** the buffer. This constructs a node and term header before -** iDoclistData and flushes the resulting complete node using -** leafWriterInternalFlush(). -*/ -static int leafWriterInlineFlush(fulltext_vtab *v, LeafWriter *pWriter, - const char *pTerm, int nTerm, - int iDoclistData){ - char c[VARINT_MAX+VARINT_MAX]; - int iData, n = putVarint(c, 0); - n += putVarint(c+n, nTerm); - - /* There should always be room for the header. Even if pTerm shared - ** a substantial prefix with the previous term, the entire prefix - ** could be constructed from earlier data in the doclist, so there - ** should be room. - */ - assert( iDoclistData>=n+nTerm ); - - iData = iDoclistData-(n+nTerm); - memcpy(pWriter->data.pData+iData, c, n); - memcpy(pWriter->data.pData+iData+n, pTerm, nTerm); - - return leafWriterInternalFlush(v, pWriter, iData, pWriter->data.nData-iData); -} - -/* Push pTerm[nTerm] along with the doclist data to the leaf layer of -** %_segments. -*/ -static int leafWriterStepMerge(fulltext_vtab *v, LeafWriter *pWriter, - const char *pTerm, int nTerm, - DLReader *pReaders, int nReaders){ - char c[VARINT_MAX+VARINT_MAX]; - int iTermData = pWriter->data.nData, iDoclistData; - int i, nData, n, nActualData, nActual, rc, nTermDistinct; - - ASSERT_VALID_LEAF_NODE(pWriter->data.pData, pWriter->data.nData); - nTermDistinct = leafWriterEncodeTerm(pWriter, pTerm, nTerm); - - /* Remember nTermDistinct if opening a new node. */ - if( iTermData==0 ) pWriter->nTermDistinct = nTermDistinct; - - iDoclistData = pWriter->data.nData; - - /* Estimate the length of the merged doclist so we can leave space - ** to encode it. - */ - for(i=0, nData=0; idata, c, n); - - docListMerge(&pWriter->data, pReaders, nReaders); - ASSERT_VALID_DOCLIST(DL_DEFAULT, - pWriter->data.pData+iDoclistData+n, - pWriter->data.nData-iDoclistData-n, NULL); - - /* The actual amount of doclist data at this point could be smaller - ** than the length we encoded. Additionally, the space required to - ** encode this length could be smaller. For small doclists, this is - ** not a big deal, we can just use memmove() to adjust things. - */ - nActualData = pWriter->data.nData-(iDoclistData+n); - nActual = putVarint(c, nActualData); - assert( nActualData<=nData ); - assert( nActual<=n ); - - /* If the new doclist is big enough for force a standalone leaf - ** node, we can immediately flush it inline without doing the - ** memmove(). - */ - /* TODO(shess) This test matches leafWriterStep(), which does this - ** test before it knows the cost to varint-encode the term and - ** doclist lengths. At some point, change to - ** pWriter->data.nData-iTermData>STANDALONE_MIN. - */ - if( nTerm+nActualData>STANDALONE_MIN ){ - /* Push leaf node from before this term. */ - if( iTermData>0 ){ - rc = leafWriterInternalFlush(v, pWriter, 0, iTermData); - if( rc!=SQLITE_OK ) return rc; - - pWriter->nTermDistinct = nTermDistinct; - } - - /* Fix the encoded doclist length. */ - iDoclistData += n - nActual; - memcpy(pWriter->data.pData+iDoclistData, c, nActual); - - /* Push the standalone leaf node. */ - rc = leafWriterInlineFlush(v, pWriter, pTerm, nTerm, iDoclistData); - if( rc!=SQLITE_OK ) return rc; - - /* Leave the node empty. */ - dataBufferReset(&pWriter->data); - - return rc; - } - - /* At this point, we know that the doclist was small, so do the - ** memmove if indicated. - */ - if( nActualdata.pData+iDoclistData+nActual, - pWriter->data.pData+iDoclistData+n, - pWriter->data.nData-(iDoclistData+n)); - pWriter->data.nData -= n-nActual; - } - - /* Replace written length with actual length. */ - memcpy(pWriter->data.pData+iDoclistData, c, nActual); - - /* If the node is too large, break things up. */ - /* TODO(shess) This test matches leafWriterStep(), which does this - ** test before it knows the cost to varint-encode the term and - ** doclist lengths. At some point, change to - ** pWriter->data.nData>LEAF_MAX. - */ - if( iTermData+nTerm+nActualData>LEAF_MAX ){ - /* Flush out the leading data as a node */ - rc = leafWriterInternalFlush(v, pWriter, 0, iTermData); - if( rc!=SQLITE_OK ) return rc; - - pWriter->nTermDistinct = nTermDistinct; - - /* Rebuild header using the current term */ - n = putVarint(pWriter->data.pData, 0); - n += putVarint(pWriter->data.pData+n, nTerm); - memcpy(pWriter->data.pData+n, pTerm, nTerm); - n += nTerm; - - /* There should always be room, because the previous encoding - ** included all data necessary to construct the term. - */ - assert( ndata.nData-iDoclistDatadata.pData+n, - pWriter->data.pData+iDoclistData, - pWriter->data.nData-iDoclistData); - pWriter->data.nData -= iDoclistData-n; - } - ASSERT_VALID_LEAF_NODE(pWriter->data.pData, pWriter->data.nData); - - return SQLITE_OK; -} - -/* Push pTerm[nTerm] along with the doclist data to the leaf layer of -** %_segments. -*/ -/* TODO(shess) Revise writeZeroSegment() so that doclists are -** constructed directly in pWriter->data. -*/ -static int leafWriterStep(fulltext_vtab *v, LeafWriter *pWriter, - const char *pTerm, int nTerm, - const char *pData, int nData){ - int rc; - DLReader reader; - - dlrInit(&reader, DL_DEFAULT, pData, nData); - rc = leafWriterStepMerge(v, pWriter, pTerm, nTerm, &reader, 1); - dlrDestroy(&reader); - - return rc; -} - - -/****************************************************************/ -/* LeafReader is used to iterate over an individual leaf node. */ -typedef struct LeafReader { - DataBuffer term; /* copy of current term. */ - - const char *pData; /* data for current term. */ - int nData; -} LeafReader; - -static void leafReaderDestroy(LeafReader *pReader){ - dataBufferDestroy(&pReader->term); - SCRAMBLE(pReader); -} - -static int leafReaderAtEnd(LeafReader *pReader){ - return pReader->nData<=0; -} - -/* Access the current term. */ -static int leafReaderTermBytes(LeafReader *pReader){ - return pReader->term.nData; -} -static const char *leafReaderTerm(LeafReader *pReader){ - assert( pReader->term.nData>0 ); - return pReader->term.pData; -} - -/* Access the doclist data for the current term. */ -static int leafReaderDataBytes(LeafReader *pReader){ - int nData; - assert( pReader->term.nData>0 ); - getVarint32(pReader->pData, &nData); - return nData; -} -static const char *leafReaderData(LeafReader *pReader){ - int n, nData; - assert( pReader->term.nData>0 ); - n = getVarint32(pReader->pData, &nData); - return pReader->pData+n; -} - -static void leafReaderInit(const char *pData, int nData, - LeafReader *pReader){ - int nTerm, n; - - assert( nData>0 ); - assert( pData[0]=='\0' ); - - CLEAR(pReader); - - /* Read the first term, skipping the header byte. */ - n = getVarint32(pData+1, &nTerm); - dataBufferInit(&pReader->term, nTerm); - dataBufferReplace(&pReader->term, pData+1+n, nTerm); - - /* Position after the first term. */ - assert( 1+n+nTermpData = pData+1+n+nTerm; - pReader->nData = nData-1-n-nTerm; -} - -/* Step the reader forward to the next term. */ -static void leafReaderStep(LeafReader *pReader){ - int n, nData, nPrefix, nSuffix; - assert( !leafReaderAtEnd(pReader) ); - - /* Skip previous entry's data block. */ - n = getVarint32(pReader->pData, &nData); - assert( n+nData<=pReader->nData ); - pReader->pData += n+nData; - pReader->nData -= n+nData; - - if( !leafReaderAtEnd(pReader) ){ - /* Construct the new term using a prefix from the old term plus a - ** suffix from the leaf data. - */ - n = getVarint32(pReader->pData, &nPrefix); - n += getVarint32(pReader->pData+n, &nSuffix); - assert( n+nSuffixnData ); - pReader->term.nData = nPrefix; - dataBufferAppend(&pReader->term, pReader->pData+n, nSuffix); - - pReader->pData += n+nSuffix; - pReader->nData -= n+nSuffix; - } -} - -/* strcmp-style comparison of pReader's current term against pTerm. -** If isPrefix, equality means equal through nTerm bytes. -*/ -static int leafReaderTermCmp(LeafReader *pReader, - const char *pTerm, int nTerm, int isPrefix){ - int c, n = pReader->term.nDataterm.nData : nTerm; - if( n==0 ){ - if( pReader->term.nData>0 ) return -1; - if(nTerm>0 ) return 1; - return 0; - } - - c = memcmp(pReader->term.pData, pTerm, n); - if( c!=0 ) return c; - if( isPrefix && n==nTerm ) return 0; - return pReader->term.nData - nTerm; -} - - -/****************************************************************/ -/* LeavesReader wraps LeafReader to allow iterating over the entire -** leaf layer of the tree. -*/ -typedef struct LeavesReader { - int idx; /* Index within the segment. */ - - sqlite3_stmt *pStmt; /* Statement we're streaming leaves from. */ - int eof; /* we've seen SQLITE_DONE from pStmt. */ - - LeafReader leafReader; /* reader for the current leaf. */ - DataBuffer rootData; /* root data for inline. */ -} LeavesReader; - -/* Access the current term. */ -static int leavesReaderTermBytes(LeavesReader *pReader){ - assert( !pReader->eof ); - return leafReaderTermBytes(&pReader->leafReader); -} -static const char *leavesReaderTerm(LeavesReader *pReader){ - assert( !pReader->eof ); - return leafReaderTerm(&pReader->leafReader); -} - -/* Access the doclist data for the current term. */ -static int leavesReaderDataBytes(LeavesReader *pReader){ - assert( !pReader->eof ); - return leafReaderDataBytes(&pReader->leafReader); -} -static const char *leavesReaderData(LeavesReader *pReader){ - assert( !pReader->eof ); - return leafReaderData(&pReader->leafReader); -} - -static int leavesReaderAtEnd(LeavesReader *pReader){ - return pReader->eof; -} - -/* loadSegmentLeaves() may not read all the way to SQLITE_DONE, thus -** leaving the statement handle open, which locks the table. -*/ -/* TODO(shess) This "solution" is not satisfactory. Really, there -** should be check-in function for all statement handles which -** arranges to call sqlite3_reset(). This most likely will require -** modification to control flow all over the place, though, so for now -** just punt. -** -** Note the current system assumes that segment merges will run to -** completion, which is why this particular probably hasn't arisen in -** this case. Probably a brittle assumption. -*/ -static int leavesReaderReset(LeavesReader *pReader){ - return sqlite3_reset(pReader->pStmt); -} - -static void leavesReaderDestroy(LeavesReader *pReader){ - /* If idx is -1, that means we're using a non-cached statement - ** handle in the optimize() case, so we need to release it. - */ - if( pReader->pStmt!=NULL && pReader->idx==-1 ){ - sqlite3_finalize(pReader->pStmt); - } - leafReaderDestroy(&pReader->leafReader); - dataBufferDestroy(&pReader->rootData); - SCRAMBLE(pReader); -} - -/* Initialize pReader with the given root data (if iStartBlockid==0 -** the leaf data was entirely contained in the root), or from the -** stream of blocks between iStartBlockid and iEndBlockid, inclusive. -*/ -static int leavesReaderInit(fulltext_vtab *v, - int idx, - sqlite_int64 iStartBlockid, - sqlite_int64 iEndBlockid, - const char *pRootData, int nRootData, - LeavesReader *pReader){ - CLEAR(pReader); - pReader->idx = idx; - - dataBufferInit(&pReader->rootData, 0); - if( iStartBlockid==0 ){ - /* Entire leaf level fit in root data. */ - dataBufferReplace(&pReader->rootData, pRootData, nRootData); - leafReaderInit(pReader->rootData.pData, pReader->rootData.nData, - &pReader->leafReader); - }else{ - sqlite3_stmt *s; - int rc = sql_get_leaf_statement(v, idx, &s); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_int64(s, 1, iStartBlockid); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_int64(s, 2, iEndBlockid); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_step(s); - if( rc==SQLITE_DONE ){ - pReader->eof = 1; - return SQLITE_OK; - } - if( rc!=SQLITE_ROW ) return rc; - - pReader->pStmt = s; - leafReaderInit(sqlite3_column_blob(pReader->pStmt, 0), - sqlite3_column_bytes(pReader->pStmt, 0), - &pReader->leafReader); - } - return SQLITE_OK; -} - -/* Step the current leaf forward to the next term. If we reach the -** end of the current leaf, step forward to the next leaf block. -*/ -static int leavesReaderStep(fulltext_vtab *v, LeavesReader *pReader){ - assert( !leavesReaderAtEnd(pReader) ); - leafReaderStep(&pReader->leafReader); - - if( leafReaderAtEnd(&pReader->leafReader) ){ - int rc; - if( pReader->rootData.pData ){ - pReader->eof = 1; - return SQLITE_OK; - } - rc = sqlite3_step(pReader->pStmt); - if( rc!=SQLITE_ROW ){ - pReader->eof = 1; - return rc==SQLITE_DONE ? SQLITE_OK : rc; - } - leafReaderDestroy(&pReader->leafReader); - leafReaderInit(sqlite3_column_blob(pReader->pStmt, 0), - sqlite3_column_bytes(pReader->pStmt, 0), - &pReader->leafReader); - } - return SQLITE_OK; -} - -/* Order LeavesReaders by their term, ignoring idx. Readers at eof -** always sort to the end. -*/ -static int leavesReaderTermCmp(LeavesReader *lr1, LeavesReader *lr2){ - if( leavesReaderAtEnd(lr1) ){ - if( leavesReaderAtEnd(lr2) ) return 0; - return 1; - } - if( leavesReaderAtEnd(lr2) ) return -1; - - return leafReaderTermCmp(&lr1->leafReader, - leavesReaderTerm(lr2), leavesReaderTermBytes(lr2), - 0); -} - -/* Similar to leavesReaderTermCmp(), with additional ordering by idx -** so that older segments sort before newer segments. -*/ -static int leavesReaderCmp(LeavesReader *lr1, LeavesReader *lr2){ - int c = leavesReaderTermCmp(lr1, lr2); - if( c!=0 ) return c; - return lr1->idx-lr2->idx; -} - -/* Assume that pLr[1]..pLr[nLr] are sorted. Bubble pLr[0] into its -** sorted position. -*/ -static void leavesReaderReorder(LeavesReader *pLr, int nLr){ - while( nLr>1 && leavesReaderCmp(pLr, pLr+1)>0 ){ - LeavesReader tmp = pLr[0]; - pLr[0] = pLr[1]; - pLr[1] = tmp; - nLr--; - pLr++; - } -} - -/* Initializes pReaders with the segments from level iLevel, returning -** the number of segments in *piReaders. Leaves pReaders in sorted -** order. -*/ -static int leavesReadersInit(fulltext_vtab *v, int iLevel, - LeavesReader *pReaders, int *piReaders){ - sqlite3_stmt *s; - int i, rc = sql_get_statement(v, SEGDIR_SELECT_LEVEL_STMT, &s); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_int(s, 1, iLevel); - if( rc!=SQLITE_OK ) return rc; - - i = 0; - while( (rc = sqlite3_step(s))==SQLITE_ROW ){ - sqlite_int64 iStart = sqlite3_column_int64(s, 0); - sqlite_int64 iEnd = sqlite3_column_int64(s, 1); - const char *pRootData = sqlite3_column_blob(s, 2); - int nRootData = sqlite3_column_bytes(s, 2); - - assert( i0 ){ - leavesReaderDestroy(&pReaders[i]); - } - return rc; - } - - *piReaders = i; - - /* Leave our results sorted by term, then age. */ - while( i-- ){ - leavesReaderReorder(pReaders+i, *piReaders-i); - } - return SQLITE_OK; -} - -/* Merge doclists from pReaders[nReaders] into a single doclist, which -** is written to pWriter. Assumes pReaders is ordered oldest to -** newest. -*/ -/* TODO(shess) Consider putting this inline in segmentMerge(). */ -static int leavesReadersMerge(fulltext_vtab *v, - LeavesReader *pReaders, int nReaders, - LeafWriter *pWriter){ - DLReader dlReaders[MERGE_COUNT]; - const char *pTerm = leavesReaderTerm(pReaders); - int i, nTerm = leavesReaderTermBytes(pReaders); - - assert( nReaders<=MERGE_COUNT ); - - for(i=0; i0 ){ - rc = leavesReaderStep(v, lrs+i); - if( rc!=SQLITE_OK ) goto err; - - /* Reorder by term, then by age. */ - leavesReaderReorder(lrs+i, MERGE_COUNT-i); - } - } - - for(i=0; i0 ); - - for(rc=SQLITE_OK; rc==SQLITE_OK && !leavesReaderAtEnd(pReader); - rc=leavesReaderStep(v, pReader)){ - /* TODO(shess) Really want leavesReaderTermCmp(), but that name is - ** already taken to compare the terms of two LeavesReaders. Think - ** on a better name. [Meanwhile, break encapsulation rather than - ** use a confusing name.] - */ - int c = leafReaderTermCmp(&pReader->leafReader, pTerm, nTerm, isPrefix); - if( c>0 ) break; /* Past any possible matches. */ - if( c==0 ){ - const char *pData = leavesReaderData(pReader); - int iBuffer, nData = leavesReaderDataBytes(pReader); - - /* Find the first empty buffer. */ - for(iBuffer=0; iBuffer0 ){ - assert(pBuffers!=NULL); - memcpy(p, pBuffers, nBuffers*sizeof(*pBuffers)); - sqlite3_free(pBuffers); - } - pBuffers = p; - } - dataBufferInit(&(pBuffers[nBuffers]), 0); - nBuffers++; - } - - /* At this point, must have an empty at iBuffer. */ - assert(iBufferpData, p->nData); - - /* dataBufferReset() could allow a large doclist to blow up - ** our memory requirements. - */ - if( p->nCapacity<1024 ){ - dataBufferReset(p); - }else{ - dataBufferDestroy(p); - dataBufferInit(p, 0); - } - } - } - } - } - - /* Union all the doclists together into *out. */ - /* TODO(shess) What if *out is big? Sigh. */ - if( rc==SQLITE_OK && nBuffers>0 ){ - int iBuffer; - for(iBuffer=0; iBuffer0 ){ - if( out->nData==0 ){ - dataBufferSwap(out, &(pBuffers[iBuffer])); - }else{ - docListAccumulateUnion(out, pBuffers[iBuffer].pData, - pBuffers[iBuffer].nData); - } - } - } - } - - while( nBuffers-- ){ - dataBufferDestroy(&(pBuffers[nBuffers])); - } - if( pBuffers!=NULL ) sqlite3_free(pBuffers); - - return rc; -} - -/* Call loadSegmentLeavesInt() with pData/nData as input. */ -static int loadSegmentLeaf(fulltext_vtab *v, const char *pData, int nData, - const char *pTerm, int nTerm, int isPrefix, - DataBuffer *out){ - LeavesReader reader; - int rc; - - assert( nData>1 ); - assert( *pData=='\0' ); - rc = leavesReaderInit(v, 0, 0, 0, pData, nData, &reader); - if( rc!=SQLITE_OK ) return rc; - - rc = loadSegmentLeavesInt(v, &reader, pTerm, nTerm, isPrefix, out); - leavesReaderReset(&reader); - leavesReaderDestroy(&reader); - return rc; -} - -/* Call loadSegmentLeavesInt() with the leaf nodes from iStartLeaf to -** iEndLeaf (inclusive) as input, and merge the resulting doclist into -** out. -*/ -static int loadSegmentLeaves(fulltext_vtab *v, - sqlite_int64 iStartLeaf, sqlite_int64 iEndLeaf, - const char *pTerm, int nTerm, int isPrefix, - DataBuffer *out){ - int rc; - LeavesReader reader; - - assert( iStartLeaf<=iEndLeaf ); - rc = leavesReaderInit(v, 0, iStartLeaf, iEndLeaf, NULL, 0, &reader); - if( rc!=SQLITE_OK ) return rc; - - rc = loadSegmentLeavesInt(v, &reader, pTerm, nTerm, isPrefix, out); - leavesReaderReset(&reader); - leavesReaderDestroy(&reader); - return rc; -} - -/* Taking pData/nData as an interior node, find the sequence of child -** nodes which could include pTerm/nTerm/isPrefix. Note that the -** interior node terms logically come between the blocks, so there is -** one more blockid than there are terms (that block contains terms >= -** the last interior-node term). -*/ -/* TODO(shess) The calling code may already know that the end child is -** not worth calculating, because the end may be in a later sibling -** node. Consider whether breaking symmetry is worthwhile. I suspect -** it is not worthwhile. -*/ -static void getChildrenContaining(const char *pData, int nData, - const char *pTerm, int nTerm, int isPrefix, - sqlite_int64 *piStartChild, - sqlite_int64 *piEndChild){ - InteriorReader reader; - - assert( nData>1 ); - assert( *pData!='\0' ); - interiorReaderInit(pData, nData, &reader); - - /* Scan for the first child which could contain pTerm/nTerm. */ - while( !interiorReaderAtEnd(&reader) ){ - if( interiorReaderTermCmp(&reader, pTerm, nTerm, 0)>0 ) break; - interiorReaderStep(&reader); - } - *piStartChild = interiorReaderCurrentBlockid(&reader); - - /* Keep scanning to find a term greater than our term, using prefix - ** comparison if indicated. If isPrefix is false, this will be the - ** same blockid as the starting block. - */ - while( !interiorReaderAtEnd(&reader) ){ - if( interiorReaderTermCmp(&reader, pTerm, nTerm, isPrefix)>0 ) break; - interiorReaderStep(&reader); - } - *piEndChild = interiorReaderCurrentBlockid(&reader); - - interiorReaderDestroy(&reader); - - /* Children must ascend, and if !prefix, both must be the same. */ - assert( *piEndChild>=*piStartChild ); - assert( isPrefix || *piStartChild==*piEndChild ); -} - -/* Read block at iBlockid and pass it with other params to -** getChildrenContaining(). -*/ -static int loadAndGetChildrenContaining( - fulltext_vtab *v, - sqlite_int64 iBlockid, - const char *pTerm, int nTerm, int isPrefix, - sqlite_int64 *piStartChild, sqlite_int64 *piEndChild -){ - sqlite3_stmt *s = NULL; - int rc; - - assert( iBlockid!=0 ); - assert( pTerm!=NULL ); - assert( nTerm!=0 ); /* TODO(shess) Why not allow this? */ - assert( piStartChild!=NULL ); - assert( piEndChild!=NULL ); - - rc = sql_get_statement(v, BLOCK_SELECT_STMT, &s); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_int64(s, 1, iBlockid); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_step(s); - if( rc==SQLITE_DONE ) return SQLITE_ERROR; - if( rc!=SQLITE_ROW ) return rc; - - getChildrenContaining(sqlite3_column_blob(s, 0), sqlite3_column_bytes(s, 0), - pTerm, nTerm, isPrefix, piStartChild, piEndChild); - - /* We expect only one row. We must execute another sqlite3_step() - * to complete the iteration; otherwise the table will remain - * locked. */ - rc = sqlite3_step(s); - if( rc==SQLITE_ROW ) return SQLITE_ERROR; - if( rc!=SQLITE_DONE ) return rc; - - return SQLITE_OK; -} - -/* Traverse the tree represented by pData[nData] looking for -** pTerm[nTerm], placing its doclist into *out. This is internal to -** loadSegment() to make error-handling cleaner. -*/ -static int loadSegmentInt(fulltext_vtab *v, const char *pData, int nData, - sqlite_int64 iLeavesEnd, - const char *pTerm, int nTerm, int isPrefix, - DataBuffer *out){ - /* Special case where root is a leaf. */ - if( *pData=='\0' ){ - return loadSegmentLeaf(v, pData, nData, pTerm, nTerm, isPrefix, out); - }else{ - int rc; - sqlite_int64 iStartChild, iEndChild; - - /* Process pData as an interior node, then loop down the tree - ** until we find the set of leaf nodes to scan for the term. - */ - getChildrenContaining(pData, nData, pTerm, nTerm, isPrefix, - &iStartChild, &iEndChild); - while( iStartChild>iLeavesEnd ){ - sqlite_int64 iNextStart, iNextEnd; - rc = loadAndGetChildrenContaining(v, iStartChild, pTerm, nTerm, isPrefix, - &iNextStart, &iNextEnd); - if( rc!=SQLITE_OK ) return rc; - - /* If we've branched, follow the end branch, too. */ - if( iStartChild!=iEndChild ){ - sqlite_int64 iDummy; - rc = loadAndGetChildrenContaining(v, iEndChild, pTerm, nTerm, isPrefix, - &iDummy, &iNextEnd); - if( rc!=SQLITE_OK ) return rc; - } - - assert( iNextStart<=iNextEnd ); - iStartChild = iNextStart; - iEndChild = iNextEnd; - } - assert( iStartChild<=iLeavesEnd ); - assert( iEndChild<=iLeavesEnd ); - - /* Scan through the leaf segments for doclists. */ - return loadSegmentLeaves(v, iStartChild, iEndChild, - pTerm, nTerm, isPrefix, out); - } -} - -/* Call loadSegmentInt() to collect the doclist for pTerm/nTerm, then -** merge its doclist over *out (any duplicate doclists read from the -** segment rooted at pData will overwrite those in *out). -*/ -/* TODO(shess) Consider changing this to determine the depth of the -** leaves using either the first characters of interior nodes (when -** ==1, we're one level above the leaves), or the first character of -** the root (which will describe the height of the tree directly). -** Either feels somewhat tricky to me. -*/ -/* TODO(shess) The current merge is likely to be slow for large -** doclists (though it should process from newest/smallest to -** oldest/largest, so it may not be that bad). It might be useful to -** modify things to allow for N-way merging. This could either be -** within a segment, with pairwise merges across segments, or across -** all segments at once. -*/ -static int loadSegment(fulltext_vtab *v, const char *pData, int nData, - sqlite_int64 iLeavesEnd, - const char *pTerm, int nTerm, int isPrefix, - DataBuffer *out){ - DataBuffer result; - int rc; - - assert( nData>1 ); - - /* This code should never be called with buffered updates. */ - assert( v->nPendingData<0 ); - - dataBufferInit(&result, 0); - rc = loadSegmentInt(v, pData, nData, iLeavesEnd, - pTerm, nTerm, isPrefix, &result); - if( rc==SQLITE_OK && result.nData>0 ){ - if( out->nData==0 ){ - DataBuffer tmp = *out; - *out = result; - result = tmp; - }else{ - DataBuffer merged; - DLReader readers[2]; - - dlrInit(&readers[0], DL_DEFAULT, out->pData, out->nData); - dlrInit(&readers[1], DL_DEFAULT, result.pData, result.nData); - dataBufferInit(&merged, out->nData+result.nData); - docListMerge(&merged, readers, 2); - dataBufferDestroy(out); - *out = merged; - dlrDestroy(&readers[0]); - dlrDestroy(&readers[1]); - } - } - dataBufferDestroy(&result); - return rc; -} - -/* Scan the database and merge together the posting lists for the term -** into *out. -*/ -static int termSelect(fulltext_vtab *v, int iColumn, - const char *pTerm, int nTerm, int isPrefix, - DocListType iType, DataBuffer *out){ - DataBuffer doclist; - sqlite3_stmt *s; - int rc = sql_get_statement(v, SEGDIR_SELECT_ALL_STMT, &s); - if( rc!=SQLITE_OK ) return rc; - - /* This code should never be called with buffered updates. */ - assert( v->nPendingData<0 ); - - dataBufferInit(&doclist, 0); - - /* Traverse the segments from oldest to newest so that newer doclist - ** elements for given docids overwrite older elements. - */ - while( (rc = sqlite3_step(s))==SQLITE_ROW ){ - const char *pData = sqlite3_column_blob(s, 2); - const int nData = sqlite3_column_bytes(s, 2); - const sqlite_int64 iLeavesEnd = sqlite3_column_int64(s, 1); - rc = loadSegment(v, pData, nData, iLeavesEnd, pTerm, nTerm, isPrefix, - &doclist); - if( rc!=SQLITE_OK ) goto err; - } - if( rc==SQLITE_DONE ){ - if( doclist.nData!=0 ){ - /* TODO(shess) The old term_select_all() code applied the column - ** restrict as we merged segments, leading to smaller buffers. - ** This is probably worthwhile to bring back, once the new storage - ** system is checked in. - */ - if( iColumn==v->nColumn) iColumn = -1; - docListTrim(DL_DEFAULT, doclist.pData, doclist.nData, - iColumn, iType, out); - } - rc = SQLITE_OK; - } - - err: - dataBufferDestroy(&doclist); - return rc; -} - -/****************************************************************/ -/* Used to hold hashtable data for sorting. */ -typedef struct TermData { - const char *pTerm; - int nTerm; - DLCollector *pCollector; -} TermData; - -/* Orders TermData elements in strcmp fashion ( <0 for less-than, 0 -** for equal, >0 for greater-than). -*/ -static int termDataCmp(const void *av, const void *bv){ - const TermData *a = (const TermData *)av; - const TermData *b = (const TermData *)bv; - int n = a->nTermnTerm ? a->nTerm : b->nTerm; - int c = memcmp(a->pTerm, b->pTerm, n); - if( c!=0 ) return c; - return a->nTerm-b->nTerm; -} - -/* Order pTerms data by term, then write a new level 0 segment using -** LeafWriter. -*/ -static int writeZeroSegment(fulltext_vtab *v, fts2Hash *pTerms){ - fts2HashElem *e; - int idx, rc, i, n; - TermData *pData; - LeafWriter writer; - DataBuffer dl; - - /* Determine the next index at level 0, merging as necessary. */ - rc = segdirNextIndex(v, 0, &idx); - if( rc!=SQLITE_OK ) return rc; - - n = fts2HashCount(pTerms); - pData = sqlite3_malloc(n*sizeof(TermData)); - - for(i = 0, e = fts2HashFirst(pTerms); e; i++, e = fts2HashNext(e)){ - assert( i1 ) qsort(pData, n, sizeof(*pData), termDataCmp); - - /* TODO(shess) Refactor so that we can write directly to the segment - ** DataBuffer, as happens for segment merges. - */ - leafWriterInit(0, idx, &writer); - dataBufferInit(&dl, 0); - for(i=0; inPendingData>=0 ){ - fts2HashElem *e; - for(e=fts2HashFirst(&v->pendingTerms); e; e=fts2HashNext(e)){ - dlcDelete(fts2HashData(e)); - } - fts2HashClear(&v->pendingTerms); - v->nPendingData = -1; - } - return SQLITE_OK; -} - -/* If pendingTerms has data, flush it to a level-zero segment, and -** free it. -*/ -static int flushPendingTerms(fulltext_vtab *v){ - if( v->nPendingData>=0 ){ - int rc = writeZeroSegment(v, &v->pendingTerms); - if( rc==SQLITE_OK ) clearPendingTerms(v); - return rc; - } - return SQLITE_OK; -} - -/* If pendingTerms is "too big", or docid is out of order, flush it. -** Regardless, be certain that pendingTerms is initialized for use. -*/ -static int initPendingTerms(fulltext_vtab *v, sqlite_int64 iDocid){ - /* TODO(shess) Explore whether partially flushing the buffer on - ** forced-flush would provide better performance. I suspect that if - ** we ordered the doclists by size and flushed the largest until the - ** buffer was half empty, that would let the less frequent terms - ** generate longer doclists. - */ - if( iDocid<=v->iPrevDocid || v->nPendingData>kPendingThreshold ){ - int rc = flushPendingTerms(v); - if( rc!=SQLITE_OK ) return rc; - } - if( v->nPendingData<0 ){ - fts2HashInit(&v->pendingTerms, FTS2_HASH_STRING, 1); - v->nPendingData = 0; - } - v->iPrevDocid = iDocid; - return SQLITE_OK; -} - -/* This function implements the xUpdate callback; it is the top-level entry - * point for inserting, deleting or updating a row in a full-text table. */ -static int fulltextUpdate(sqlite3_vtab *pVtab, int nArg, sqlite3_value **ppArg, - sqlite_int64 *pRowid){ - fulltext_vtab *v = (fulltext_vtab *) pVtab; - int rc; - - TRACE(("FTS2 Update %p\n", pVtab)); - - if( nArg<2 ){ - rc = index_delete(v, sqlite3_value_int64(ppArg[0])); - if( rc==SQLITE_OK ){ - /* If we just deleted the last row in the table, clear out the - ** index data. - */ - rc = content_exists(v); - if( rc==SQLITE_ROW ){ - rc = SQLITE_OK; - }else if( rc==SQLITE_DONE ){ - /* Clear the pending terms so we don't flush a useless level-0 - ** segment when the transaction closes. - */ - rc = clearPendingTerms(v); - if( rc==SQLITE_OK ){ - rc = segdir_delete_all(v); - } - } - } - } else if( sqlite3_value_type(ppArg[0]) != SQLITE_NULL ){ - /* An update: - * ppArg[0] = old rowid - * ppArg[1] = new rowid - * ppArg[2..2+v->nColumn-1] = values - * ppArg[2+v->nColumn] = value for magic column (we ignore this) - */ - sqlite_int64 rowid = sqlite3_value_int64(ppArg[0]); - if( sqlite3_value_type(ppArg[1]) != SQLITE_INTEGER || - sqlite3_value_int64(ppArg[1]) != rowid ){ - rc = SQLITE_ERROR; /* we don't allow changing the rowid */ - } else { - assert( nArg==2+v->nColumn+1); - rc = index_update(v, rowid, &ppArg[2]); - } - } else { - /* An insert: - * ppArg[1] = requested rowid - * ppArg[2..2+v->nColumn-1] = values - * ppArg[2+v->nColumn] = value for magic column (we ignore this) - */ - assert( nArg==2+v->nColumn+1); - rc = index_insert(v, ppArg[1], &ppArg[2], pRowid); - } - - return rc; -} - -static int fulltextSync(sqlite3_vtab *pVtab){ - TRACE(("FTS2 xSync()\n")); - return flushPendingTerms((fulltext_vtab *)pVtab); -} - -static int fulltextBegin(sqlite3_vtab *pVtab){ - fulltext_vtab *v = (fulltext_vtab *) pVtab; - TRACE(("FTS2 xBegin()\n")); - - /* Any buffered updates should have been cleared by the previous - ** transaction. - */ - assert( v->nPendingData<0 ); - return clearPendingTerms(v); -} - -static int fulltextCommit(sqlite3_vtab *pVtab){ - fulltext_vtab *v = (fulltext_vtab *) pVtab; - TRACE(("FTS2 xCommit()\n")); - - /* Buffered updates should have been cleared by fulltextSync(). */ - assert( v->nPendingData<0 ); - return clearPendingTerms(v); -} - -static int fulltextRollback(sqlite3_vtab *pVtab){ - TRACE(("FTS2 xRollback()\n")); - return clearPendingTerms((fulltext_vtab *)pVtab); -} - -/* -** Implementation of the snippet() function for FTS2 -*/ -static void snippetFunc( - sqlite3_context *pContext, - int argc, - sqlite3_value **argv -){ - fulltext_cursor *pCursor; - if( argc<1 ) return; - if( sqlite3_value_type(argv[0])!=SQLITE_BLOB || - sqlite3_value_bytes(argv[0])!=sizeof(pCursor) ){ - sqlite3_result_error(pContext, "illegal first argument to html_snippet",-1); - }else{ - const char *zStart = ""; - const char *zEnd = ""; - const char *zEllipsis = "..."; - memcpy(&pCursor, sqlite3_value_blob(argv[0]), sizeof(pCursor)); - if( argc>=2 ){ - zStart = (const char*)sqlite3_value_text(argv[1]); - if( argc>=3 ){ - zEnd = (const char*)sqlite3_value_text(argv[2]); - if( argc>=4 ){ - zEllipsis = (const char*)sqlite3_value_text(argv[3]); - } - } - } - snippetAllOffsets(pCursor); - snippetText(pCursor, zStart, zEnd, zEllipsis); - sqlite3_result_text(pContext, pCursor->snippet.zSnippet, - pCursor->snippet.nSnippet, SQLITE_STATIC); - } -} - -/* -** Implementation of the offsets() function for FTS2 -*/ -static void snippetOffsetsFunc( - sqlite3_context *pContext, - int argc, - sqlite3_value **argv -){ - fulltext_cursor *pCursor; - if( argc<1 ) return; - if( sqlite3_value_type(argv[0])!=SQLITE_BLOB || - sqlite3_value_bytes(argv[0])!=sizeof(pCursor) ){ - sqlite3_result_error(pContext, "illegal first argument to offsets",-1); - }else{ - memcpy(&pCursor, sqlite3_value_blob(argv[0]), sizeof(pCursor)); - snippetAllOffsets(pCursor); - snippetOffsetText(&pCursor->snippet); - sqlite3_result_text(pContext, - pCursor->snippet.zOffset, pCursor->snippet.nOffset, - SQLITE_STATIC); - } -} - -/* OptLeavesReader is nearly identical to LeavesReader, except that -** where LeavesReader is geared towards the merging of complete -** segment levels (with exactly MERGE_COUNT segments), OptLeavesReader -** is geared towards implementation of the optimize() function, and -** can merge all segments simultaneously. This version may be -** somewhat less efficient than LeavesReader because it merges into an -** accumulator rather than doing an N-way merge, but since segment -** size grows exponentially (so segment count logrithmically) this is -** probably not an immediate problem. -*/ -/* TODO(shess): Prove that assertion, or extend the merge code to -** merge tree fashion (like the prefix-searching code does). -*/ -/* TODO(shess): OptLeavesReader and LeavesReader could probably be -** merged with little or no loss of performance for LeavesReader. The -** merged code would need to handle >MERGE_COUNT segments, and would -** also need to be able to optionally optimize away deletes. -*/ -typedef struct OptLeavesReader { - /* Segment number, to order readers by age. */ - int segment; - LeavesReader reader; -} OptLeavesReader; - -static int optLeavesReaderAtEnd(OptLeavesReader *pReader){ - return leavesReaderAtEnd(&pReader->reader); -} -static int optLeavesReaderTermBytes(OptLeavesReader *pReader){ - return leavesReaderTermBytes(&pReader->reader); -} -static const char *optLeavesReaderData(OptLeavesReader *pReader){ - return leavesReaderData(&pReader->reader); -} -static int optLeavesReaderDataBytes(OptLeavesReader *pReader){ - return leavesReaderDataBytes(&pReader->reader); -} -static const char *optLeavesReaderTerm(OptLeavesReader *pReader){ - return leavesReaderTerm(&pReader->reader); -} -static int optLeavesReaderStep(fulltext_vtab *v, OptLeavesReader *pReader){ - return leavesReaderStep(v, &pReader->reader); -} -static int optLeavesReaderTermCmp(OptLeavesReader *lr1, OptLeavesReader *lr2){ - return leavesReaderTermCmp(&lr1->reader, &lr2->reader); -} -/* Order by term ascending, segment ascending (oldest to newest), with -** exhausted readers to the end. -*/ -static int optLeavesReaderCmp(OptLeavesReader *lr1, OptLeavesReader *lr2){ - int c = optLeavesReaderTermCmp(lr1, lr2); - if( c!=0 ) return c; - return lr1->segment-lr2->segment; -} -/* Bubble pLr[0] to appropriate place in pLr[1..nLr-1]. Assumes that -** pLr[1..nLr-1] is already sorted. -*/ -static void optLeavesReaderReorder(OptLeavesReader *pLr, int nLr){ - while( nLr>1 && optLeavesReaderCmp(pLr, pLr+1)>0 ){ - OptLeavesReader tmp = pLr[0]; - pLr[0] = pLr[1]; - pLr[1] = tmp; - nLr--; - pLr++; - } -} - -/* optimize() helper function. Put the readers in order and iterate -** through them, merging doclists for matching terms into pWriter. -** Returns SQLITE_OK on success, or the SQLite error code which -** prevented success. -*/ -static int optimizeInternal(fulltext_vtab *v, - OptLeavesReader *readers, int nReaders, - LeafWriter *pWriter){ - int i, rc = SQLITE_OK; - DataBuffer doclist, merged, tmp; - - /* Order the readers. */ - i = nReaders; - while( i-- > 0 ){ - optLeavesReaderReorder(&readers[i], nReaders-i); - } - - dataBufferInit(&doclist, LEAF_MAX); - dataBufferInit(&merged, LEAF_MAX); - - /* Exhausted readers bubble to the end, so when the first reader is - ** at eof, all are at eof. - */ - while( !optLeavesReaderAtEnd(&readers[0]) ){ - - /* Figure out how many readers share the next term. */ - for(i=1; i 0 ){ - dlrDestroy(&dlReaders[nReaders]); - } - - /* Accumulated doclist to reader 0 for next pass. */ - dlrInit(&dlReaders[0], DL_DEFAULT, doclist.pData, doclist.nData); - } - - /* Destroy reader that was left in the pipeline. */ - dlrDestroy(&dlReaders[0]); - - /* Trim deletions from the doclist. */ - dataBufferReset(&merged); - docListTrim(DL_DEFAULT, doclist.pData, doclist.nData, - -1, DL_DEFAULT, &merged); - } - - /* Only pass doclists with hits (skip if all hits deleted). */ - if( merged.nData>0 ){ - rc = leafWriterStep(v, pWriter, - optLeavesReaderTerm(&readers[0]), - optLeavesReaderTermBytes(&readers[0]), - merged.pData, merged.nData); - if( rc!=SQLITE_OK ) goto err; - } - - /* Step merged readers to next term and reorder. */ - while( i-- > 0 ){ - rc = optLeavesReaderStep(v, &readers[i]); - if( rc!=SQLITE_OK ) goto err; - - optLeavesReaderReorder(&readers[i], nReaders-i); - } - } - - err: - dataBufferDestroy(&doclist); - dataBufferDestroy(&merged); - return rc; -} - -/* Implement optimize() function for FTS3. optimize(t) merges all -** segments in the fts index into a single segment. 't' is the magic -** table-named column. -*/ -static void optimizeFunc(sqlite3_context *pContext, - int argc, sqlite3_value **argv){ - fulltext_cursor *pCursor; - if( argc>1 ){ - sqlite3_result_error(pContext, "excess arguments to optimize()",-1); - }else if( sqlite3_value_type(argv[0])!=SQLITE_BLOB || - sqlite3_value_bytes(argv[0])!=sizeof(pCursor) ){ - sqlite3_result_error(pContext, "illegal first argument to optimize",-1); - }else{ - fulltext_vtab *v; - int i, rc, iMaxLevel; - OptLeavesReader *readers; - int nReaders; - LeafWriter writer; - sqlite3_stmt *s; - - memcpy(&pCursor, sqlite3_value_blob(argv[0]), sizeof(pCursor)); - v = cursor_vtab(pCursor); - - /* Flush any buffered updates before optimizing. */ - rc = flushPendingTerms(v); - if( rc!=SQLITE_OK ) goto err; - - rc = segdir_count(v, &nReaders, &iMaxLevel); - if( rc!=SQLITE_OK ) goto err; - if( nReaders==0 || nReaders==1 ){ - sqlite3_result_text(pContext, "Index already optimal", -1, - SQLITE_STATIC); - return; - } - - rc = sql_get_statement(v, SEGDIR_SELECT_ALL_STMT, &s); - if( rc!=SQLITE_OK ) goto err; - - readers = sqlite3_malloc(nReaders*sizeof(readers[0])); - if( readers==NULL ) goto err; - - /* Note that there will already be a segment at this position - ** until we call segdir_delete() on iMaxLevel. - */ - leafWriterInit(iMaxLevel, 0, &writer); - - i = 0; - while( (rc = sqlite3_step(s))==SQLITE_ROW ){ - sqlite_int64 iStart = sqlite3_column_int64(s, 0); - sqlite_int64 iEnd = sqlite3_column_int64(s, 1); - const char *pRootData = sqlite3_column_blob(s, 2); - int nRootData = sqlite3_column_bytes(s, 2); - - assert( i 0 ){ - leavesReaderDestroy(&readers[i].reader); - } - sqlite3_free(readers); - - /* If we've successfully gotten to here, delete the old segments - ** and flush the interior structure of the new segment. - */ - if( rc==SQLITE_OK ){ - for( i=0; i<=iMaxLevel; i++ ){ - rc = segdir_delete(v, i); - if( rc!=SQLITE_OK ) break; - } - - if( rc==SQLITE_OK ) rc = leafWriterFinalize(v, &writer); - } - - leafWriterDestroy(&writer); - - if( rc!=SQLITE_OK ) goto err; - - sqlite3_result_text(pContext, "Index optimized", -1, SQLITE_STATIC); - return; - - /* TODO(shess): Error-handling needs to be improved along the - ** lines of the dump_ functions. - */ - err: - { - char buf[512]; - sqlite3_snprintf(sizeof(buf), buf, "Error in optimize: %s", - sqlite3_errmsg(sqlite3_context_db_handle(pContext))); - sqlite3_result_error(pContext, buf, -1); - } - } -} - -#ifdef SQLITE_TEST -/* Generate an error of the form ": ". If msg is NULL, -** pull the error from the context's db handle. -*/ -static void generateError(sqlite3_context *pContext, - const char *prefix, const char *msg){ - char buf[512]; - if( msg==NULL ) msg = sqlite3_errmsg(sqlite3_context_db_handle(pContext)); - sqlite3_snprintf(sizeof(buf), buf, "%s: %s", prefix, msg); - sqlite3_result_error(pContext, buf, -1); -} - -/* Helper function to collect the set of terms in the segment into -** pTerms. The segment is defined by the leaf nodes between -** iStartBlockid and iEndBlockid, inclusive, or by the contents of -** pRootData if iStartBlockid is 0 (in which case the entire segment -** fit in a leaf). -*/ -static int collectSegmentTerms(fulltext_vtab *v, sqlite3_stmt *s, - fts2Hash *pTerms){ - const sqlite_int64 iStartBlockid = sqlite3_column_int64(s, 0); - const sqlite_int64 iEndBlockid = sqlite3_column_int64(s, 1); - const char *pRootData = sqlite3_column_blob(s, 2); - const int nRootData = sqlite3_column_bytes(s, 2); - LeavesReader reader; - int rc = leavesReaderInit(v, 0, iStartBlockid, iEndBlockid, - pRootData, nRootData, &reader); - if( rc!=SQLITE_OK ) return rc; - - while( rc==SQLITE_OK && !leavesReaderAtEnd(&reader) ){ - const char *pTerm = leavesReaderTerm(&reader); - const int nTerm = leavesReaderTermBytes(&reader); - void *oldValue = sqlite3Fts2HashFind(pTerms, pTerm, nTerm); - void *newValue = (void *)((char *)oldValue+1); - - /* From the comment before sqlite3Fts2HashInsert in fts2_hash.c, - ** the data value passed is returned in case of malloc failure. - */ - if( newValue==sqlite3Fts2HashInsert(pTerms, pTerm, nTerm, newValue) ){ - rc = SQLITE_NOMEM; - }else{ - rc = leavesReaderStep(v, &reader); - } - } - - leavesReaderDestroy(&reader); - return rc; -} - -/* Helper function to build the result string for dump_terms(). */ -static int generateTermsResult(sqlite3_context *pContext, fts2Hash *pTerms){ - int iTerm, nTerms, nResultBytes, iByte; - char *result; - TermData *pData; - fts2HashElem *e; - - /* Iterate pTerms to generate an array of terms in pData for - ** sorting. - */ - nTerms = fts2HashCount(pTerms); - assert( nTerms>0 ); - pData = sqlite3_malloc(nTerms*sizeof(TermData)); - if( pData==NULL ) return SQLITE_NOMEM; - - nResultBytes = 0; - for(iTerm = 0, e = fts2HashFirst(pTerms); e; iTerm++, e = fts2HashNext(e)){ - nResultBytes += fts2HashKeysize(e)+1; /* Term plus trailing space */ - assert( iTerm0 ); /* nTerms>0, nResultsBytes must be, too. */ - result = sqlite3_malloc(nResultBytes); - if( result==NULL ){ - sqlite3_free(pData); - return SQLITE_NOMEM; - } - - if( nTerms>1 ) qsort(pData, nTerms, sizeof(*pData), termDataCmp); - - /* Read the terms in order to build the result. */ - iByte = 0; - for(iTerm=0; iTerm0 ){ - rc = generateTermsResult(pContext, &terms); - if( rc==SQLITE_NOMEM ){ - generateError(pContext, "dump_terms", "out of memory"); - }else{ - assert( rc==SQLITE_OK ); - } - }else if( argc==3 ){ - /* The specific segment asked for could not be found. */ - generateError(pContext, "dump_terms", "segment not found"); - }else{ - /* No segments found. */ - /* TODO(shess): It should be impossible to reach this. This - ** case can only happen for an empty table, in which case - ** SQLite has no rows to call this function on. - */ - sqlite3_result_null(pContext); - } - } - sqlite3Fts2HashClear(&terms); - } -} - -/* Expand the DL_DEFAULT doclist in pData into a text result in -** pContext. -*/ -static void createDoclistResult(sqlite3_context *pContext, - const char *pData, int nData){ - DataBuffer dump; - DLReader dlReader; - - assert( pData!=NULL && nData>0 ); - - dataBufferInit(&dump, 0); - dlrInit(&dlReader, DL_DEFAULT, pData, nData); - for( ; !dlrAtEnd(&dlReader); dlrStep(&dlReader) ){ - char buf[256]; - PLReader plReader; - - plrInit(&plReader, &dlReader); - if( DL_DEFAULT==DL_DOCIDS || plrAtEnd(&plReader) ){ - sqlite3_snprintf(sizeof(buf), buf, "[%lld] ", dlrDocid(&dlReader)); - dataBufferAppend(&dump, buf, strlen(buf)); - }else{ - int iColumn = plrColumn(&plReader); - - sqlite3_snprintf(sizeof(buf), buf, "[%lld %d[", - dlrDocid(&dlReader), iColumn); - dataBufferAppend(&dump, buf, strlen(buf)); - - for( ; !plrAtEnd(&plReader); plrStep(&plReader) ){ - if( plrColumn(&plReader)!=iColumn ){ - iColumn = plrColumn(&plReader); - sqlite3_snprintf(sizeof(buf), buf, "] %d[", iColumn); - assert( dump.nData>0 ); - dump.nData--; /* Overwrite trailing space. */ - assert( dump.pData[dump.nData]==' '); - dataBufferAppend(&dump, buf, strlen(buf)); - } - if( DL_DEFAULT==DL_POSITIONS_OFFSETS ){ - sqlite3_snprintf(sizeof(buf), buf, "%d,%d,%d ", - plrPosition(&plReader), - plrStartOffset(&plReader), plrEndOffset(&plReader)); - }else if( DL_DEFAULT==DL_POSITIONS ){ - sqlite3_snprintf(sizeof(buf), buf, "%d ", plrPosition(&plReader)); - }else{ - assert( NULL=="Unhandled DL_DEFAULT value"); - } - dataBufferAppend(&dump, buf, strlen(buf)); - } - plrDestroy(&plReader); - - assert( dump.nData>0 ); - dump.nData--; /* Overwrite trailing space. */ - assert( dump.pData[dump.nData]==' '); - dataBufferAppend(&dump, "]] ", 3); - } - } - dlrDestroy(&dlReader); - - assert( dump.nData>0 ); - dump.nData--; /* Overwrite trailing space. */ - assert( dump.pData[dump.nData]==' '); - dump.pData[dump.nData] = '\0'; - assert( dump.nData>0 ); - - /* Passes ownership of dump's buffer to pContext. */ - sqlite3_result_text(pContext, dump.pData, dump.nData, sqlite3_free); - dump.pData = NULL; - dump.nData = dump.nCapacity = 0; -} - -/* Implements dump_doclist() for use in inspecting the fts2 index from -** tests. TEXT result containing a string representation of the -** doclist for the indicated term. dump_doclist(t, term, level, idx) -** dumps the doclist for term from the segment specified by level, idx -** (in %_segdir), while dump_doclist(t, term) dumps the logical -** doclist for the term across all segments. The per-segment doclist -** can contain deletions, while the full-index doclist will not -** (deletions are omitted). -** -** Result formats differ with the setting of DL_DEFAULTS. Examples: -** -** DL_DOCIDS: [1] [3] [7] -** DL_POSITIONS: [1 0[0 4] 1[17]] [3 1[5]] -** DL_POSITIONS_OFFSETS: [1 0[0,0,3 4,23,26] 1[17,102,105]] [3 1[5,20,23]] -** -** In each case the number after the outer '[' is the docid. In the -** latter two cases, the number before the inner '[' is the column -** associated with the values within. For DL_POSITIONS the numbers -** within are the positions, for DL_POSITIONS_OFFSETS they are the -** position, the start offset, and the end offset. -*/ -static void dumpDoclistFunc( - sqlite3_context *pContext, - int argc, sqlite3_value **argv -){ - fulltext_cursor *pCursor; - if( argc!=2 && argc!=4 ){ - generateError(pContext, "dump_doclist", "incorrect arguments"); - }else if( sqlite3_value_type(argv[0])!=SQLITE_BLOB || - sqlite3_value_bytes(argv[0])!=sizeof(pCursor) ){ - generateError(pContext, "dump_doclist", "illegal first argument"); - }else if( sqlite3_value_text(argv[1])==NULL || - sqlite3_value_text(argv[1])[0]=='\0' ){ - generateError(pContext, "dump_doclist", "empty second argument"); - }else{ - const char *pTerm = (const char *)sqlite3_value_text(argv[1]); - const int nTerm = strlen(pTerm); - fulltext_vtab *v; - int rc; - DataBuffer doclist; - - memcpy(&pCursor, sqlite3_value_blob(argv[0]), sizeof(pCursor)); - v = cursor_vtab(pCursor); - - dataBufferInit(&doclist, 0); - - /* termSelect() yields the same logical doclist that queries are - ** run against. - */ - if( argc==2 ){ - rc = termSelect(v, v->nColumn, pTerm, nTerm, 0, DL_DEFAULT, &doclist); - }else{ - sqlite3_stmt *s = NULL; - - /* Get our specific segment's information. */ - rc = sql_get_statement(v, SEGDIR_SELECT_SEGMENT_STMT, &s); - if( rc==SQLITE_OK ){ - rc = sqlite3_bind_int(s, 1, sqlite3_value_int(argv[2])); - if( rc==SQLITE_OK ){ - rc = sqlite3_bind_int(s, 2, sqlite3_value_int(argv[3])); - } - } - - if( rc==SQLITE_OK ){ - rc = sqlite3_step(s); - - if( rc==SQLITE_DONE ){ - dataBufferDestroy(&doclist); - generateError(pContext, "dump_doclist", "segment not found"); - return; - } - - /* Found a segment, load it into doclist. */ - if( rc==SQLITE_ROW ){ - const sqlite_int64 iLeavesEnd = sqlite3_column_int64(s, 1); - const char *pData = sqlite3_column_blob(s, 2); - const int nData = sqlite3_column_bytes(s, 2); - - /* loadSegment() is used by termSelect() to load each - ** segment's data. - */ - rc = loadSegment(v, pData, nData, iLeavesEnd, pTerm, nTerm, 0, - &doclist); - if( rc==SQLITE_OK ){ - rc = sqlite3_step(s); - - /* Should not have more than one matching segment. */ - if( rc!=SQLITE_DONE ){ - sqlite3_reset(s); - dataBufferDestroy(&doclist); - generateError(pContext, "dump_doclist", "invalid segdir"); - return; - } - rc = SQLITE_OK; - } - } - } - - sqlite3_reset(s); - } - - if( rc==SQLITE_OK ){ - if( doclist.nData>0 ){ - createDoclistResult(pContext, doclist.pData, doclist.nData); - }else{ - /* TODO(shess): This can happen if the term is not present, or - ** if all instances of the term have been deleted and this is - ** an all-index dump. It may be interesting to distinguish - ** these cases. - */ - sqlite3_result_text(pContext, "", 0, SQLITE_STATIC); - } - }else if( rc==SQLITE_NOMEM ){ - /* Handle out-of-memory cases specially because if they are - ** generated in fts2 code they may not be reflected in the db - ** handle. - */ - /* TODO(shess): Handle this more comprehensively. - ** sqlite3ErrStr() has what I need, but is internal. - */ - generateError(pContext, "dump_doclist", "out of memory"); - }else{ - generateError(pContext, "dump_doclist", NULL); - } - - dataBufferDestroy(&doclist); - } -} -#endif - -/* -** This routine implements the xFindFunction method for the FTS2 -** virtual table. -*/ -static int fulltextFindFunction( - sqlite3_vtab *pVtab, - int nArg, - const char *zName, - void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), - void **ppArg -){ - if( strcmp(zName,"snippet")==0 ){ - *pxFunc = snippetFunc; - return 1; - }else if( strcmp(zName,"offsets")==0 ){ - *pxFunc = snippetOffsetsFunc; - return 1; - }else if( strcmp(zName,"optimize")==0 ){ - *pxFunc = optimizeFunc; - return 1; -#ifdef SQLITE_TEST - /* NOTE(shess): These functions are present only for testing - ** purposes. No particular effort is made to optimize their - ** execution or how they build their results. - */ - }else if( strcmp(zName,"dump_terms")==0 ){ - /* fprintf(stderr, "Found dump_terms\n"); */ - *pxFunc = dumpTermsFunc; - return 1; - }else if( strcmp(zName,"dump_doclist")==0 ){ - /* fprintf(stderr, "Found dump_doclist\n"); */ - *pxFunc = dumpDoclistFunc; - return 1; -#endif - } - return 0; -} - -/* -** Rename an fts2 table. -*/ -static int fulltextRename( - sqlite3_vtab *pVtab, - const char *zName -){ - fulltext_vtab *p = (fulltext_vtab *)pVtab; - int rc = SQLITE_NOMEM; - char *zSql = sqlite3_mprintf( - "ALTER TABLE %Q.'%q_content' RENAME TO '%q_content';" - "ALTER TABLE %Q.'%q_segments' RENAME TO '%q_segments';" - "ALTER TABLE %Q.'%q_segdir' RENAME TO '%q_segdir';" - , p->zDb, p->zName, zName - , p->zDb, p->zName, zName - , p->zDb, p->zName, zName - ); - if( zSql ){ - rc = sqlite3_exec(p->db, zSql, 0, 0, 0); - sqlite3_free(zSql); - } - return rc; -} - -static const sqlite3_module fts2Module = { - /* iVersion */ 0, - /* xCreate */ fulltextCreate, - /* xConnect */ fulltextConnect, - /* xBestIndex */ fulltextBestIndex, - /* xDisconnect */ fulltextDisconnect, - /* xDestroy */ fulltextDestroy, - /* xOpen */ fulltextOpen, - /* xClose */ fulltextClose, - /* xFilter */ fulltextFilter, - /* xNext */ fulltextNext, - /* xEof */ fulltextEof, - /* xColumn */ fulltextColumn, - /* xRowid */ fulltextRowid, - /* xUpdate */ fulltextUpdate, - /* xBegin */ fulltextBegin, - /* xSync */ fulltextSync, - /* xCommit */ fulltextCommit, - /* xRollback */ fulltextRollback, - /* xFindFunction */ fulltextFindFunction, - /* xRename */ fulltextRename, -}; - -static void hashDestroy(void *p){ - fts2Hash *pHash = (fts2Hash *)p; - sqlite3Fts2HashClear(pHash); - sqlite3_free(pHash); -} - -/* -** The fts2 built-in tokenizers - "simple" and "porter" - are implemented -** in files fts2_tokenizer1.c and fts2_porter.c respectively. The following -** two forward declarations are for functions declared in these files -** used to retrieve the respective implementations. -** -** Calling sqlite3Fts2SimpleTokenizerModule() sets the value pointed -** to by the argument to point a the "simple" tokenizer implementation. -** Function ...PorterTokenizerModule() sets *pModule to point to the -** porter tokenizer/stemmer implementation. -*/ -void sqlite3Fts2SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule); -void sqlite3Fts2PorterTokenizerModule(sqlite3_tokenizer_module const**ppModule); -void sqlite3Fts2IcuTokenizerModule(sqlite3_tokenizer_module const**ppModule); - -int sqlite3Fts2InitHashTable(sqlite3 *, fts2Hash *, const char *); - -/* -** Initialize the fts2 extension. If this extension is built as part -** of the sqlite library, then this function is called directly by -** SQLite. If fts2 is built as a dynamically loadable extension, this -** function is called by the sqlite3_extension_init() entry point. -*/ -int sqlite3Fts2Init(sqlite3 *db){ - int rc = SQLITE_OK; - fts2Hash *pHash = 0; - const sqlite3_tokenizer_module *pSimple = 0; - const sqlite3_tokenizer_module *pPorter = 0; - const sqlite3_tokenizer_module *pIcu = 0; - - sqlite3Fts2SimpleTokenizerModule(&pSimple); - sqlite3Fts2PorterTokenizerModule(&pPorter); -#ifdef SQLITE_ENABLE_ICU - sqlite3Fts2IcuTokenizerModule(&pIcu); -#endif - - /* Allocate and initialize the hash-table used to store tokenizers. */ - pHash = sqlite3_malloc(sizeof(fts2Hash)); - if( !pHash ){ - rc = SQLITE_NOMEM; - }else{ - sqlite3Fts2HashInit(pHash, FTS2_HASH_STRING, 1); - } - - /* Load the built-in tokenizers into the hash table */ - if( rc==SQLITE_OK ){ - if( sqlite3Fts2HashInsert(pHash, "simple", 7, (void *)pSimple) - || sqlite3Fts2HashInsert(pHash, "porter", 7, (void *)pPorter) - || (pIcu && sqlite3Fts2HashInsert(pHash, "icu", 4, (void *)pIcu)) - ){ - rc = SQLITE_NOMEM; - } - } - - /* Create the virtual table wrapper around the hash-table and overload - ** the two scalar functions. If this is successful, register the - ** module with sqlite. - */ - if( SQLITE_OK==rc - && SQLITE_OK==(rc = sqlite3Fts2InitHashTable(db, pHash, "fts2_tokenizer")) - && SQLITE_OK==(rc = sqlite3_overload_function(db, "snippet", -1)) - && SQLITE_OK==(rc = sqlite3_overload_function(db, "offsets", -1)) - && SQLITE_OK==(rc = sqlite3_overload_function(db, "optimize", -1)) -#ifdef SQLITE_TEST - && SQLITE_OK==(rc = sqlite3_overload_function(db, "dump_terms", -1)) - && SQLITE_OK==(rc = sqlite3_overload_function(db, "dump_doclist", -1)) -#endif - ){ - return sqlite3_create_module_v2( - db, "fts2", &fts2Module, (void *)pHash, hashDestroy - ); - } - - /* An error has occurred. Delete the hash table and return the error code. */ - assert( rc!=SQLITE_OK ); - if( pHash ){ - sqlite3Fts2HashClear(pHash); - sqlite3_free(pHash); - } - return rc; -} - -#if !SQLITE_CORE -#ifdef _WIN32 -__declspec(dllexport) -#endif -int sqlite3_fts2_init( - sqlite3 *db, - char **pzErrMsg, - const sqlite3_api_routines *pApi -){ - SQLITE_EXTENSION_INIT2(pApi) - return sqlite3Fts2Init(db); -} -#endif - -#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS2) */ diff --git a/ext/fts2/fts2.h b/ext/fts2/fts2.h deleted file mode 100644 index 4da4c3877b..0000000000 --- a/ext/fts2/fts2.h +++ /dev/null @@ -1,26 +0,0 @@ -/* -** 2006 Oct 10 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -****************************************************************************** -** -** This header file is used by programs that want to link against the -** FTS2 library. All it does is declare the sqlite3Fts2Init() interface. -*/ -#include "sqlite3.h" - -#ifdef __cplusplus -extern "C" { -#endif /* __cplusplus */ - -int sqlite3Fts2Init(sqlite3 *db); - -#ifdef __cplusplus -} /* extern "C" */ -#endif /* __cplusplus */ diff --git a/ext/fts2/fts2_hash.c b/ext/fts2/fts2_hash.c deleted file mode 100644 index 3596dcf0b8..0000000000 --- a/ext/fts2/fts2_hash.c +++ /dev/null @@ -1,376 +0,0 @@ -/* -** 2001 September 22 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -************************************************************************* -** This is the implementation of generic hash-tables used in SQLite. -** We've modified it slightly to serve as a standalone hash table -** implementation for the full-text indexing module. -*/ - -/* -** The code in this file is only compiled if: -** -** * The FTS2 module is being built as an extension -** (in which case SQLITE_CORE is not defined), or -** -** * The FTS2 module is being built into the core of -** SQLite (in which case SQLITE_ENABLE_FTS2 is defined). -*/ -#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS2) - -#include -#include -#include - -#include "sqlite3.h" -#include "sqlite3ext.h" -SQLITE_EXTENSION_INIT3 -#include "fts2_hash.h" - -/* -** Malloc and Free functions -*/ -static void *fts2HashMalloc(int n){ - void *p = sqlite3_malloc(n); - if( p ){ - memset(p, 0, n); - } - return p; -} -static void fts2HashFree(void *p){ - sqlite3_free(p); -} - -/* Turn bulk memory into a hash table object by initializing the -** fields of the Hash structure. -** -** "pNew" is a pointer to the hash table that is to be initialized. -** keyClass is one of the constants -** FTS2_HASH_BINARY or FTS2_HASH_STRING. The value of keyClass -** determines what kind of key the hash table will use. "copyKey" is -** true if the hash table should make its own private copy of keys and -** false if it should just use the supplied pointer. -*/ -void sqlite3Fts2HashInit(fts2Hash *pNew, int keyClass, int copyKey){ - assert( pNew!=0 ); - assert( keyClass>=FTS2_HASH_STRING && keyClass<=FTS2_HASH_BINARY ); - pNew->keyClass = keyClass; - pNew->copyKey = copyKey; - pNew->first = 0; - pNew->count = 0; - pNew->htsize = 0; - pNew->ht = 0; -} - -/* Remove all entries from a hash table. Reclaim all memory. -** Call this routine to delete a hash table or to reset a hash table -** to the empty state. -*/ -void sqlite3Fts2HashClear(fts2Hash *pH){ - fts2HashElem *elem; /* For looping over all elements of the table */ - - assert( pH!=0 ); - elem = pH->first; - pH->first = 0; - fts2HashFree(pH->ht); - pH->ht = 0; - pH->htsize = 0; - while( elem ){ - fts2HashElem *next_elem = elem->next; - if( pH->copyKey && elem->pKey ){ - fts2HashFree(elem->pKey); - } - fts2HashFree(elem); - elem = next_elem; - } - pH->count = 0; -} - -/* -** Hash and comparison functions when the mode is FTS2_HASH_STRING -*/ -static int strHash(const void *pKey, int nKey){ - const char *z = (const char *)pKey; - int h = 0; - if( nKey<=0 ) nKey = (int) strlen(z); - while( nKey > 0 ){ - h = (h<<3) ^ h ^ *z++; - nKey--; - } - return h & 0x7fffffff; -} -static int strCompare(const void *pKey1, int n1, const void *pKey2, int n2){ - if( n1!=n2 ) return 1; - return strncmp((const char*)pKey1,(const char*)pKey2,n1); -} - -/* -** Hash and comparison functions when the mode is FTS2_HASH_BINARY -*/ -static int binHash(const void *pKey, int nKey){ - int h = 0; - const char *z = (const char *)pKey; - while( nKey-- > 0 ){ - h = (h<<3) ^ h ^ *(z++); - } - return h & 0x7fffffff; -} -static int binCompare(const void *pKey1, int n1, const void *pKey2, int n2){ - if( n1!=n2 ) return 1; - return memcmp(pKey1,pKey2,n1); -} - -/* -** Return a pointer to the appropriate hash function given the key class. -** -** The C syntax in this function definition may be unfamilar to some -** programmers, so we provide the following additional explanation: -** -** The name of the function is "hashFunction". The function takes a -** single parameter "keyClass". The return value of hashFunction() -** is a pointer to another function. Specifically, the return value -** of hashFunction() is a pointer to a function that takes two parameters -** with types "const void*" and "int" and returns an "int". -*/ -static int (*hashFunction(int keyClass))(const void*,int){ - if( keyClass==FTS2_HASH_STRING ){ - return &strHash; - }else{ - assert( keyClass==FTS2_HASH_BINARY ); - return &binHash; - } -} - -/* -** Return a pointer to the appropriate hash function given the key class. -** -** For help in interpreted the obscure C code in the function definition, -** see the header comment on the previous function. -*/ -static int (*compareFunction(int keyClass))(const void*,int,const void*,int){ - if( keyClass==FTS2_HASH_STRING ){ - return &strCompare; - }else{ - assert( keyClass==FTS2_HASH_BINARY ); - return &binCompare; - } -} - -/* Link an element into the hash table -*/ -static void insertElement( - fts2Hash *pH, /* The complete hash table */ - struct _fts2ht *pEntry, /* The entry into which pNew is inserted */ - fts2HashElem *pNew /* The element to be inserted */ -){ - fts2HashElem *pHead; /* First element already in pEntry */ - pHead = pEntry->chain; - if( pHead ){ - pNew->next = pHead; - pNew->prev = pHead->prev; - if( pHead->prev ){ pHead->prev->next = pNew; } - else { pH->first = pNew; } - pHead->prev = pNew; - }else{ - pNew->next = pH->first; - if( pH->first ){ pH->first->prev = pNew; } - pNew->prev = 0; - pH->first = pNew; - } - pEntry->count++; - pEntry->chain = pNew; -} - - -/* Resize the hash table so that it cantains "new_size" buckets. -** "new_size" must be a power of 2. The hash table might fail -** to resize if sqliteMalloc() fails. -*/ -static void rehash(fts2Hash *pH, int new_size){ - struct _fts2ht *new_ht; /* The new hash table */ - fts2HashElem *elem, *next_elem; /* For looping over existing elements */ - int (*xHash)(const void*,int); /* The hash function */ - - assert( (new_size & (new_size-1))==0 ); - new_ht = (struct _fts2ht *)fts2HashMalloc( new_size*sizeof(struct _fts2ht) ); - if( new_ht==0 ) return; - fts2HashFree(pH->ht); - pH->ht = new_ht; - pH->htsize = new_size; - xHash = hashFunction(pH->keyClass); - for(elem=pH->first, pH->first=0; elem; elem = next_elem){ - int h = (*xHash)(elem->pKey, elem->nKey) & (new_size-1); - next_elem = elem->next; - insertElement(pH, &new_ht[h], elem); - } -} - -/* This function (for internal use only) locates an element in an -** hash table that matches the given key. The hash for this key has -** already been computed and is passed as the 4th parameter. -*/ -static fts2HashElem *findElementGivenHash( - const fts2Hash *pH, /* The pH to be searched */ - const void *pKey, /* The key we are searching for */ - int nKey, - int h /* The hash for this key. */ -){ - fts2HashElem *elem; /* Used to loop thru the element list */ - int count; /* Number of elements left to test */ - int (*xCompare)(const void*,int,const void*,int); /* comparison function */ - - if( pH->ht ){ - struct _fts2ht *pEntry = &pH->ht[h]; - elem = pEntry->chain; - count = pEntry->count; - xCompare = compareFunction(pH->keyClass); - while( count-- && elem ){ - if( (*xCompare)(elem->pKey,elem->nKey,pKey,nKey)==0 ){ - return elem; - } - elem = elem->next; - } - } - return 0; -} - -/* Remove a single entry from the hash table given a pointer to that -** element and a hash on the element's key. -*/ -static void removeElementGivenHash( - fts2Hash *pH, /* The pH containing "elem" */ - fts2HashElem* elem, /* The element to be removed from the pH */ - int h /* Hash value for the element */ -){ - struct _fts2ht *pEntry; - if( elem->prev ){ - elem->prev->next = elem->next; - }else{ - pH->first = elem->next; - } - if( elem->next ){ - elem->next->prev = elem->prev; - } - pEntry = &pH->ht[h]; - if( pEntry->chain==elem ){ - pEntry->chain = elem->next; - } - pEntry->count--; - if( pEntry->count<=0 ){ - pEntry->chain = 0; - } - if( pH->copyKey && elem->pKey ){ - fts2HashFree(elem->pKey); - } - fts2HashFree( elem ); - pH->count--; - if( pH->count<=0 ){ - assert( pH->first==0 ); - assert( pH->count==0 ); - fts2HashClear(pH); - } -} - -/* Attempt to locate an element of the hash table pH with a key -** that matches pKey,nKey. Return the data for this element if it is -** found, or NULL if there is no match. -*/ -void *sqlite3Fts2HashFind(const fts2Hash *pH, const void *pKey, int nKey){ - int h; /* A hash on key */ - fts2HashElem *elem; /* The element that matches key */ - int (*xHash)(const void*,int); /* The hash function */ - - if( pH==0 || pH->ht==0 ) return 0; - xHash = hashFunction(pH->keyClass); - assert( xHash!=0 ); - h = (*xHash)(pKey,nKey); - assert( (pH->htsize & (pH->htsize-1))==0 ); - elem = findElementGivenHash(pH,pKey,nKey, h & (pH->htsize-1)); - return elem ? elem->data : 0; -} - -/* Insert an element into the hash table pH. The key is pKey,nKey -** and the data is "data". -** -** If no element exists with a matching key, then a new -** element is created. A copy of the key is made if the copyKey -** flag is set. NULL is returned. -** -** If another element already exists with the same key, then the -** new data replaces the old data and the old data is returned. -** The key is not copied in this instance. If a malloc fails, then -** the new data is returned and the hash table is unchanged. -** -** If the "data" parameter to this function is NULL, then the -** element corresponding to "key" is removed from the hash table. -*/ -void *sqlite3Fts2HashInsert( - fts2Hash *pH, /* The hash table to insert into */ - const void *pKey, /* The key */ - int nKey, /* Number of bytes in the key */ - void *data /* The data */ -){ - int hraw; /* Raw hash value of the key */ - int h; /* the hash of the key modulo hash table size */ - fts2HashElem *elem; /* Used to loop thru the element list */ - fts2HashElem *new_elem; /* New element added to the pH */ - int (*xHash)(const void*,int); /* The hash function */ - - assert( pH!=0 ); - xHash = hashFunction(pH->keyClass); - assert( xHash!=0 ); - hraw = (*xHash)(pKey, nKey); - assert( (pH->htsize & (pH->htsize-1))==0 ); - h = hraw & (pH->htsize-1); - elem = findElementGivenHash(pH,pKey,nKey,h); - if( elem ){ - void *old_data = elem->data; - if( data==0 ){ - removeElementGivenHash(pH,elem,h); - }else{ - elem->data = data; - } - return old_data; - } - if( data==0 ) return 0; - new_elem = (fts2HashElem*)fts2HashMalloc( sizeof(fts2HashElem) ); - if( new_elem==0 ) return data; - if( pH->copyKey && pKey!=0 ){ - new_elem->pKey = fts2HashMalloc( nKey ); - if( new_elem->pKey==0 ){ - fts2HashFree(new_elem); - return data; - } - memcpy((void*)new_elem->pKey, pKey, nKey); - }else{ - new_elem->pKey = (void*)pKey; - } - new_elem->nKey = nKey; - pH->count++; - if( pH->htsize==0 ){ - rehash(pH,8); - if( pH->htsize==0 ){ - pH->count = 0; - fts2HashFree(new_elem); - return data; - } - } - if( pH->count > pH->htsize ){ - rehash(pH,pH->htsize*2); - } - assert( pH->htsize>0 ); - assert( (pH->htsize & (pH->htsize-1))==0 ); - h = hraw & (pH->htsize-1); - insertElement(pH, &pH->ht[h], new_elem); - new_elem->data = data; - return 0; -} - -#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS2) */ diff --git a/ext/fts2/fts2_hash.h b/ext/fts2/fts2_hash.h deleted file mode 100644 index 02936f18bb..0000000000 --- a/ext/fts2/fts2_hash.h +++ /dev/null @@ -1,110 +0,0 @@ -/* -** 2001 September 22 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -************************************************************************* -** This is the header file for the generic hash-table implementation -** used in SQLite. We've modified it slightly to serve as a standalone -** hash table implementation for the full-text indexing module. -** -*/ -#ifndef _FTS2_HASH_H_ -#define _FTS2_HASH_H_ - -/* Forward declarations of structures. */ -typedef struct fts2Hash fts2Hash; -typedef struct fts2HashElem fts2HashElem; - -/* A complete hash table is an instance of the following structure. -** The internals of this structure are intended to be opaque -- client -** code should not attempt to access or modify the fields of this structure -** directly. Change this structure only by using the routines below. -** However, many of the "procedures" and "functions" for modifying and -** accessing this structure are really macros, so we can't really make -** this structure opaque. -*/ -struct fts2Hash { - char keyClass; /* HASH_INT, _POINTER, _STRING, _BINARY */ - char copyKey; /* True if copy of key made on insert */ - int count; /* Number of entries in this table */ - fts2HashElem *first; /* The first element of the array */ - int htsize; /* Number of buckets in the hash table */ - struct _fts2ht { /* the hash table */ - int count; /* Number of entries with this hash */ - fts2HashElem *chain; /* Pointer to first entry with this hash */ - } *ht; -}; - -/* Each element in the hash table is an instance of the following -** structure. All elements are stored on a single doubly-linked list. -** -** Again, this structure is intended to be opaque, but it can't really -** be opaque because it is used by macros. -*/ -struct fts2HashElem { - fts2HashElem *next, *prev; /* Next and previous elements in the table */ - void *data; /* Data associated with this element */ - void *pKey; int nKey; /* Key associated with this element */ -}; - -/* -** There are 2 different modes of operation for a hash table: -** -** FTS2_HASH_STRING pKey points to a string that is nKey bytes long -** (including the null-terminator, if any). Case -** is respected in comparisons. -** -** FTS2_HASH_BINARY pKey points to binary data nKey bytes long. -** memcmp() is used to compare keys. -** -** A copy of the key is made if the copyKey parameter to fts2HashInit is 1. -*/ -#define FTS2_HASH_STRING 1 -#define FTS2_HASH_BINARY 2 - -/* -** Access routines. To delete, insert a NULL pointer. -*/ -void sqlite3Fts2HashInit(fts2Hash*, int keytype, int copyKey); -void *sqlite3Fts2HashInsert(fts2Hash*, const void *pKey, int nKey, void *pData); -void *sqlite3Fts2HashFind(const fts2Hash*, const void *pKey, int nKey); -void sqlite3Fts2HashClear(fts2Hash*); - -/* -** Shorthand for the functions above -*/ -#define fts2HashInit sqlite3Fts2HashInit -#define fts2HashInsert sqlite3Fts2HashInsert -#define fts2HashFind sqlite3Fts2HashFind -#define fts2HashClear sqlite3Fts2HashClear - -/* -** Macros for looping over all elements of a hash table. The idiom is -** like this: -** -** fts2Hash h; -** fts2HashElem *p; -** ... -** for(p=fts2HashFirst(&h); p; p=fts2HashNext(p)){ -** SomeStructure *pData = fts2HashData(p); -** // do something with pData -** } -*/ -#define fts2HashFirst(H) ((H)->first) -#define fts2HashNext(E) ((E)->next) -#define fts2HashData(E) ((E)->data) -#define fts2HashKey(E) ((E)->pKey) -#define fts2HashKeysize(E) ((E)->nKey) - -/* -** Number of entries in a hash table -*/ -#define fts2HashCount(H) ((H)->count) - -#endif /* _FTS2_HASH_H_ */ diff --git a/ext/fts2/fts2_icu.c b/ext/fts2/fts2_icu.c deleted file mode 100644 index 2670301f51..0000000000 --- a/ext/fts2/fts2_icu.c +++ /dev/null @@ -1,260 +0,0 @@ -/* -** 2007 June 22 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -************************************************************************* -** This file implements a tokenizer for fts2 based on the ICU library. -** -** $Id: fts2_icu.c,v 1.3 2008/12/18 05:30:26 danielk1977 Exp $ -*/ - -#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS2) -#ifdef SQLITE_ENABLE_ICU - -#include -#include -#include "fts2_tokenizer.h" - -#include -#include -#include -#include - -typedef struct IcuTokenizer IcuTokenizer; -typedef struct IcuCursor IcuCursor; - -struct IcuTokenizer { - sqlite3_tokenizer base; - char *zLocale; -}; - -struct IcuCursor { - sqlite3_tokenizer_cursor base; - - UBreakIterator *pIter; /* ICU break-iterator object */ - int nChar; /* Number of UChar elements in pInput */ - UChar *aChar; /* Copy of input using utf-16 encoding */ - int *aOffset; /* Offsets of each character in utf-8 input */ - - int nBuffer; - char *zBuffer; - - int iToken; -}; - -/* -** Create a new tokenizer instance. -*/ -static int icuCreate( - int argc, /* Number of entries in argv[] */ - const char * const *argv, /* Tokenizer creation arguments */ - sqlite3_tokenizer **ppTokenizer /* OUT: Created tokenizer */ -){ - IcuTokenizer *p; - int n = 0; - - if( argc>0 ){ - n = strlen(argv[0])+1; - } - p = (IcuTokenizer *)sqlite3_malloc(sizeof(IcuTokenizer)+n); - if( !p ){ - return SQLITE_NOMEM; - } - memset(p, 0, sizeof(IcuTokenizer)); - - if( n ){ - p->zLocale = (char *)&p[1]; - memcpy(p->zLocale, argv[0], n); - } - - *ppTokenizer = (sqlite3_tokenizer *)p; - - return SQLITE_OK; -} - -/* -** Destroy a tokenizer -*/ -static int icuDestroy(sqlite3_tokenizer *pTokenizer){ - IcuTokenizer *p = (IcuTokenizer *)pTokenizer; - sqlite3_free(p); - return SQLITE_OK; -} - -/* -** Prepare to begin tokenizing a particular string. The input -** string to be tokenized is pInput[0..nBytes-1]. A cursor -** used to incrementally tokenize this string is returned in -** *ppCursor. -*/ -static int icuOpen( - sqlite3_tokenizer *pTokenizer, /* The tokenizer */ - const char *zInput, /* Input string */ - int nInput, /* Length of zInput in bytes */ - sqlite3_tokenizer_cursor **ppCursor /* OUT: Tokenization cursor */ -){ - IcuTokenizer *p = (IcuTokenizer *)pTokenizer; - IcuCursor *pCsr; - - const int32_t opt = U_FOLD_CASE_DEFAULT; - UErrorCode status = U_ZERO_ERROR; - int nChar; - - UChar32 c; - int iInput = 0; - int iOut = 0; - - *ppCursor = 0; - - if( nInput<0 ){ - nInput = strlen(zInput); - } - nChar = nInput+1; - pCsr = (IcuCursor *)sqlite3_malloc( - sizeof(IcuCursor) + /* IcuCursor */ - ((nChar+3)&~3) * sizeof(UChar) + /* IcuCursor.aChar[] */ - (nChar+1) * sizeof(int) /* IcuCursor.aOffset[] */ - ); - if( !pCsr ){ - return SQLITE_NOMEM; - } - memset(pCsr, 0, sizeof(IcuCursor)); - pCsr->aChar = (UChar *)&pCsr[1]; - pCsr->aOffset = (int *)&pCsr->aChar[(nChar+3)&~3]; - - pCsr->aOffset[iOut] = iInput; - U8_NEXT(zInput, iInput, nInput, c); - while( c>0 ){ - int isError = 0; - c = u_foldCase(c, opt); - U16_APPEND(pCsr->aChar, iOut, nChar, c, isError); - if( isError ){ - sqlite3_free(pCsr); - return SQLITE_ERROR; - } - pCsr->aOffset[iOut] = iInput; - - if( iInputpIter = ubrk_open(UBRK_WORD, p->zLocale, pCsr->aChar, iOut, &status); - if( !U_SUCCESS(status) ){ - sqlite3_free(pCsr); - return SQLITE_ERROR; - } - pCsr->nChar = iOut; - - ubrk_first(pCsr->pIter); - *ppCursor = (sqlite3_tokenizer_cursor *)pCsr; - return SQLITE_OK; -} - -/* -** Close a tokenization cursor previously opened by a call to icuOpen(). -*/ -static int icuClose(sqlite3_tokenizer_cursor *pCursor){ - IcuCursor *pCsr = (IcuCursor *)pCursor; - ubrk_close(pCsr->pIter); - sqlite3_free(pCsr->zBuffer); - sqlite3_free(pCsr); - return SQLITE_OK; -} - -/* -** Extract the next token from a tokenization cursor. -*/ -static int icuNext( - sqlite3_tokenizer_cursor *pCursor, /* Cursor returned by simpleOpen */ - const char **ppToken, /* OUT: *ppToken is the token text */ - int *pnBytes, /* OUT: Number of bytes in token */ - int *piStartOffset, /* OUT: Starting offset of token */ - int *piEndOffset, /* OUT: Ending offset of token */ - int *piPosition /* OUT: Position integer of token */ -){ - IcuCursor *pCsr = (IcuCursor *)pCursor; - - int iStart = 0; - int iEnd = 0; - int nByte = 0; - - while( iStart==iEnd ){ - UChar32 c; - - iStart = ubrk_current(pCsr->pIter); - iEnd = ubrk_next(pCsr->pIter); - if( iEnd==UBRK_DONE ){ - return SQLITE_DONE; - } - - while( iStartaChar, iWhite, pCsr->nChar, c); - if( u_isspace(c) ){ - iStart = iWhite; - }else{ - break; - } - } - assert(iStart<=iEnd); - } - - do { - UErrorCode status = U_ZERO_ERROR; - if( nByte ){ - char *zNew = sqlite3_realloc(pCsr->zBuffer, nByte); - if( !zNew ){ - return SQLITE_NOMEM; - } - pCsr->zBuffer = zNew; - pCsr->nBuffer = nByte; - } - - u_strToUTF8( - pCsr->zBuffer, pCsr->nBuffer, &nByte, /* Output vars */ - &pCsr->aChar[iStart], iEnd-iStart, /* Input vars */ - &status /* Output success/failure */ - ); - } while( nByte>pCsr->nBuffer ); - - *ppToken = pCsr->zBuffer; - *pnBytes = nByte; - *piStartOffset = pCsr->aOffset[iStart]; - *piEndOffset = pCsr->aOffset[iEnd]; - *piPosition = pCsr->iToken++; - - return SQLITE_OK; -} - -/* -** The set of routines that implement the simple tokenizer -*/ -static const sqlite3_tokenizer_module icuTokenizerModule = { - 0, /* iVersion */ - icuCreate, /* xCreate */ - icuDestroy, /* xCreate */ - icuOpen, /* xOpen */ - icuClose, /* xClose */ - icuNext, /* xNext */ -}; - -/* -** Set *ppModule to point at the implementation of the ICU tokenizer. -*/ -void sqlite3Fts2IcuTokenizerModule( - sqlite3_tokenizer_module const**ppModule -){ - *ppModule = &icuTokenizerModule; -} - -#endif /* defined(SQLITE_ENABLE_ICU) */ -#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS2) */ diff --git a/ext/fts2/fts2_porter.c b/ext/fts2/fts2_porter.c deleted file mode 100644 index 881baf7100..0000000000 --- a/ext/fts2/fts2_porter.c +++ /dev/null @@ -1,644 +0,0 @@ -/* -** 2006 September 30 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -************************************************************************* -** Implementation of the full-text-search tokenizer that implements -** a Porter stemmer. -*/ - -/* -** The code in this file is only compiled if: -** -** * The FTS2 module is being built as an extension -** (in which case SQLITE_CORE is not defined), or -** -** * The FTS2 module is being built into the core of -** SQLite (in which case SQLITE_ENABLE_FTS2 is defined). -*/ -#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS2) - - -#include -#include -#include -#include - -#include "sqlite3.h" -#include "sqlite3ext.h" -SQLITE_EXTENSION_INIT3 -#include "fts2_tokenizer.h" - -/* -** Class derived from sqlite3_tokenizer -*/ -typedef struct porter_tokenizer { - sqlite3_tokenizer base; /* Base class */ -} porter_tokenizer; - -/* -** Class derived from sqlit3_tokenizer_cursor -*/ -typedef struct porter_tokenizer_cursor { - sqlite3_tokenizer_cursor base; - const char *zInput; /* input we are tokenizing */ - int nInput; /* size of the input */ - int iOffset; /* current position in zInput */ - int iToken; /* index of next token to be returned */ - char *zToken; /* storage for current token */ - int nAllocated; /* space allocated to zToken buffer */ -} porter_tokenizer_cursor; - - -/* Forward declaration */ -static const sqlite3_tokenizer_module porterTokenizerModule; - - -/* -** Create a new tokenizer instance. -*/ -static int porterCreate( - int argc, const char * const *argv, - sqlite3_tokenizer **ppTokenizer -){ - porter_tokenizer *t; - t = (porter_tokenizer *) sqlite3_malloc(sizeof(*t)); - if( t==NULL ) return SQLITE_NOMEM; - memset(t, 0, sizeof(*t)); - *ppTokenizer = &t->base; - return SQLITE_OK; -} - -/* -** Destroy a tokenizer -*/ -static int porterDestroy(sqlite3_tokenizer *pTokenizer){ - sqlite3_free(pTokenizer); - return SQLITE_OK; -} - -/* -** Prepare to begin tokenizing a particular string. The input -** string to be tokenized is zInput[0..nInput-1]. A cursor -** used to incrementally tokenize this string is returned in -** *ppCursor. -*/ -static int porterOpen( - sqlite3_tokenizer *pTokenizer, /* The tokenizer */ - const char *zInput, int nInput, /* String to be tokenized */ - sqlite3_tokenizer_cursor **ppCursor /* OUT: Tokenization cursor */ -){ - porter_tokenizer_cursor *c; - - c = (porter_tokenizer_cursor *) sqlite3_malloc(sizeof(*c)); - if( c==NULL ) return SQLITE_NOMEM; - - c->zInput = zInput; - if( zInput==0 ){ - c->nInput = 0; - }else if( nInput<0 ){ - c->nInput = (int)strlen(zInput); - }else{ - c->nInput = nInput; - } - c->iOffset = 0; /* start tokenizing at the beginning */ - c->iToken = 0; - c->zToken = NULL; /* no space allocated, yet. */ - c->nAllocated = 0; - - *ppCursor = &c->base; - return SQLITE_OK; -} - -/* -** Close a tokenization cursor previously opened by a call to -** porterOpen() above. -*/ -static int porterClose(sqlite3_tokenizer_cursor *pCursor){ - porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor; - sqlite3_free(c->zToken); - sqlite3_free(c); - return SQLITE_OK; -} -/* -** Vowel or consonant -*/ -static const char cType[] = { - 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, - 1, 1, 1, 2, 1 -}; - -/* -** isConsonant() and isVowel() determine if their first character in -** the string they point to is a consonant or a vowel, according -** to Porter ruls. -** -** A consonate is any letter other than 'a', 'e', 'i', 'o', or 'u'. -** 'Y' is a consonant unless it follows another consonant, -** in which case it is a vowel. -** -** In these routine, the letters are in reverse order. So the 'y' rule -** is that 'y' is a consonant unless it is followed by another -** consonent. -*/ -static int isVowel(const char*); -static int isConsonant(const char *z){ - int j; - char x = *z; - if( x==0 ) return 0; - assert( x>='a' && x<='z' ); - j = cType[x-'a']; - if( j<2 ) return j; - return z[1]==0 || isVowel(z + 1); -} -static int isVowel(const char *z){ - int j; - char x = *z; - if( x==0 ) return 0; - assert( x>='a' && x<='z' ); - j = cType[x-'a']; - if( j<2 ) return 1-j; - return isConsonant(z + 1); -} - -/* -** Let any sequence of one or more vowels be represented by V and let -** C be sequence of one or more consonants. Then every word can be -** represented as: -** -** [C] (VC){m} [V] -** -** In prose: A word is an optional consonant followed by zero or -** vowel-consonant pairs followed by an optional vowel. "m" is the -** number of vowel consonant pairs. This routine computes the value -** of m for the first i bytes of a word. -** -** Return true if the m-value for z is 1 or more. In other words, -** return true if z contains at least one vowel that is followed -** by a consonant. -** -** In this routine z[] is in reverse order. So we are really looking -** for an instance of of a consonant followed by a vowel. -*/ -static int m_gt_0(const char *z){ - while( isVowel(z) ){ z++; } - if( *z==0 ) return 0; - while( isConsonant(z) ){ z++; } - return *z!=0; -} - -/* Like mgt0 above except we are looking for a value of m which is -** exactly 1 -*/ -static int m_eq_1(const char *z){ - while( isVowel(z) ){ z++; } - if( *z==0 ) return 0; - while( isConsonant(z) ){ z++; } - if( *z==0 ) return 0; - while( isVowel(z) ){ z++; } - if( *z==0 ) return 1; - while( isConsonant(z) ){ z++; } - return *z==0; -} - -/* Like mgt0 above except we are looking for a value of m>1 instead -** or m>0 -*/ -static int m_gt_1(const char *z){ - while( isVowel(z) ){ z++; } - if( *z==0 ) return 0; - while( isConsonant(z) ){ z++; } - if( *z==0 ) return 0; - while( isVowel(z) ){ z++; } - if( *z==0 ) return 0; - while( isConsonant(z) ){ z++; } - return *z!=0; -} - -/* -** Return TRUE if there is a vowel anywhere within z[0..n-1] -*/ -static int hasVowel(const char *z){ - while( isConsonant(z) ){ z++; } - return *z!=0; -} - -/* -** Return TRUE if the word ends in a double consonant. -** -** The text is reversed here. So we are really looking at -** the first two characters of z[]. -*/ -static int doubleConsonant(const char *z){ - return isConsonant(z) && z[0]==z[1] && isConsonant(z+1); -} - -/* -** Return TRUE if the word ends with three letters which -** are consonant-vowel-consonent and where the final consonant -** is not 'w', 'x', or 'y'. -** -** The word is reversed here. So we are really checking the -** first three letters and the first one cannot be in [wxy]. -*/ -static int star_oh(const char *z){ - return - z[0]!=0 && isConsonant(z) && - z[0]!='w' && z[0]!='x' && z[0]!='y' && - z[1]!=0 && isVowel(z+1) && - z[2]!=0 && isConsonant(z+2); -} - -/* -** If the word ends with zFrom and xCond() is true for the stem -** of the word that preceeds the zFrom ending, then change the -** ending to zTo. -** -** The input word *pz and zFrom are both in reverse order. zTo -** is in normal order. -** -** Return TRUE if zFrom matches. Return FALSE if zFrom does not -** match. Not that TRUE is returned even if xCond() fails and -** no substitution occurs. -*/ -static int stem( - char **pz, /* The word being stemmed (Reversed) */ - const char *zFrom, /* If the ending matches this... (Reversed) */ - const char *zTo, /* ... change the ending to this (not reversed) */ - int (*xCond)(const char*) /* Condition that must be true */ -){ - char *z = *pz; - while( *zFrom && *zFrom==*z ){ z++; zFrom++; } - if( *zFrom!=0 ) return 0; - if( xCond && !xCond(z) ) return 1; - while( *zTo ){ - *(--z) = *(zTo++); - } - *pz = z; - return 1; -} - -/* -** This is the fallback stemmer used when the porter stemmer is -** inappropriate. The input word is copied into the output with -** US-ASCII case folding. If the input word is too long (more -** than 20 bytes if it contains no digits or more than 6 bytes if -** it contains digits) then word is truncated to 20 or 6 bytes -** by taking 10 or 3 bytes from the beginning and end. -*/ -static void copy_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){ - int i, mx, j; - int hasDigit = 0; - for(i=0; i='A' && c<='Z' ){ - zOut[i] = c - 'A' + 'a'; - }else{ - if( c>='0' && c<='9' ) hasDigit = 1; - zOut[i] = c; - } - } - mx = hasDigit ? 3 : 10; - if( nIn>mx*2 ){ - for(j=mx, i=nIn-mx; i=sizeof(zReverse)-7 ){ - /* The word is too big or too small for the porter stemmer. - ** Fallback to the copy stemmer */ - copy_stemmer(zIn, nIn, zOut, pnOut); - return; - } - for(i=0, j=sizeof(zReverse)-6; i='A' && c<='Z' ){ - zReverse[j] = c + 'a' - 'A'; - }else if( c>='a' && c<='z' ){ - zReverse[j] = c; - }else{ - /* The use of a character not in [a-zA-Z] means that we fallback - ** to the copy stemmer */ - copy_stemmer(zIn, nIn, zOut, pnOut); - return; - } - } - memset(&zReverse[sizeof(zReverse)-5], 0, 5); - z = &zReverse[j+1]; - - - /* Step 1a */ - if( z[0]=='s' ){ - if( - !stem(&z, "sess", "ss", 0) && - !stem(&z, "sei", "i", 0) && - !stem(&z, "ss", "ss", 0) - ){ - z++; - } - } - - /* Step 1b */ - z2 = z; - if( stem(&z, "dee", "ee", m_gt_0) ){ - /* Do nothing. The work was all in the test */ - }else if( - (stem(&z, "gni", "", hasVowel) || stem(&z, "de", "", hasVowel)) - && z!=z2 - ){ - if( stem(&z, "ta", "ate", 0) || - stem(&z, "lb", "ble", 0) || - stem(&z, "zi", "ize", 0) ){ - /* Do nothing. The work was all in the test */ - }else if( doubleConsonant(z) && (*z!='l' && *z!='s' && *z!='z') ){ - z++; - }else if( m_eq_1(z) && star_oh(z) ){ - *(--z) = 'e'; - } - } - - /* Step 1c */ - if( z[0]=='y' && hasVowel(z+1) ){ - z[0] = 'i'; - } - - /* Step 2 */ - switch( z[1] ){ - case 'a': - stem(&z, "lanoita", "ate", m_gt_0) || - stem(&z, "lanoit", "tion", m_gt_0); - break; - case 'c': - stem(&z, "icne", "ence", m_gt_0) || - stem(&z, "icna", "ance", m_gt_0); - break; - case 'e': - stem(&z, "rezi", "ize", m_gt_0); - break; - case 'g': - stem(&z, "igol", "log", m_gt_0); - break; - case 'l': - stem(&z, "ilb", "ble", m_gt_0) || - stem(&z, "illa", "al", m_gt_0) || - stem(&z, "iltne", "ent", m_gt_0) || - stem(&z, "ile", "e", m_gt_0) || - stem(&z, "ilsuo", "ous", m_gt_0); - break; - case 'o': - stem(&z, "noitazi", "ize", m_gt_0) || - stem(&z, "noita", "ate", m_gt_0) || - stem(&z, "rota", "ate", m_gt_0); - break; - case 's': - stem(&z, "msila", "al", m_gt_0) || - stem(&z, "ssenevi", "ive", m_gt_0) || - stem(&z, "ssenluf", "ful", m_gt_0) || - stem(&z, "ssensuo", "ous", m_gt_0); - break; - case 't': - stem(&z, "itila", "al", m_gt_0) || - stem(&z, "itivi", "ive", m_gt_0) || - stem(&z, "itilib", "ble", m_gt_0); - break; - } - - /* Step 3 */ - switch( z[0] ){ - case 'e': - stem(&z, "etaci", "ic", m_gt_0) || - stem(&z, "evita", "", m_gt_0) || - stem(&z, "ezila", "al", m_gt_0); - break; - case 'i': - stem(&z, "itici", "ic", m_gt_0); - break; - case 'l': - stem(&z, "laci", "ic", m_gt_0) || - stem(&z, "luf", "", m_gt_0); - break; - case 's': - stem(&z, "ssen", "", m_gt_0); - break; - } - - /* Step 4 */ - switch( z[1] ){ - case 'a': - if( z[0]=='l' && m_gt_1(z+2) ){ - z += 2; - } - break; - case 'c': - if( z[0]=='e' && z[2]=='n' && (z[3]=='a' || z[3]=='e') && m_gt_1(z+4) ){ - z += 4; - } - break; - case 'e': - if( z[0]=='r' && m_gt_1(z+2) ){ - z += 2; - } - break; - case 'i': - if( z[0]=='c' && m_gt_1(z+2) ){ - z += 2; - } - break; - case 'l': - if( z[0]=='e' && z[2]=='b' && (z[3]=='a' || z[3]=='i') && m_gt_1(z+4) ){ - z += 4; - } - break; - case 'n': - if( z[0]=='t' ){ - if( z[2]=='a' ){ - if( m_gt_1(z+3) ){ - z += 3; - } - }else if( z[2]=='e' ){ - stem(&z, "tneme", "", m_gt_1) || - stem(&z, "tnem", "", m_gt_1) || - stem(&z, "tne", "", m_gt_1); - } - } - break; - case 'o': - if( z[0]=='u' ){ - if( m_gt_1(z+2) ){ - z += 2; - } - }else if( z[3]=='s' || z[3]=='t' ){ - stem(&z, "noi", "", m_gt_1); - } - break; - case 's': - if( z[0]=='m' && z[2]=='i' && m_gt_1(z+3) ){ - z += 3; - } - break; - case 't': - stem(&z, "eta", "", m_gt_1) || - stem(&z, "iti", "", m_gt_1); - break; - case 'u': - if( z[0]=='s' && z[2]=='o' && m_gt_1(z+3) ){ - z += 3; - } - break; - case 'v': - case 'z': - if( z[0]=='e' && z[2]=='i' && m_gt_1(z+3) ){ - z += 3; - } - break; - } - - /* Step 5a */ - if( z[0]=='e' ){ - if( m_gt_1(z+1) ){ - z++; - }else if( m_eq_1(z+1) && !star_oh(z+1) ){ - z++; - } - } - - /* Step 5b */ - if( m_gt_1(z) && z[0]=='l' && z[1]=='l' ){ - z++; - } - - /* z[] is now the stemmed word in reverse order. Flip it back - ** around into forward order and return. - */ - *pnOut = i = strlen(z); - zOut[i] = 0; - while( *z ){ - zOut[--i] = *(z++); - } -} - -/* -** Characters that can be part of a token. We assume any character -** whose value is greater than 0x80 (any UTF character) can be -** part of a token. In other words, delimiters all must have -** values of 0x7f or lower. -*/ -static const char porterIdChar[] = { -/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */ - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */ - 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */ - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */ - 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */ - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */ -}; -#define isDelim(C) (((ch=C)&0x80)==0 && (ch<0x30 || !porterIdChar[ch-0x30])) - -/* -** Extract the next token from a tokenization cursor. The cursor must -** have been opened by a prior call to porterOpen(). -*/ -static int porterNext( - sqlite3_tokenizer_cursor *pCursor, /* Cursor returned by porterOpen */ - const char **pzToken, /* OUT: *pzToken is the token text */ - int *pnBytes, /* OUT: Number of bytes in token */ - int *piStartOffset, /* OUT: Starting offset of token */ - int *piEndOffset, /* OUT: Ending offset of token */ - int *piPosition /* OUT: Position integer of token */ -){ - porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor; - const char *z = c->zInput; - - while( c->iOffsetnInput ){ - int iStartOffset, ch; - - /* Scan past delimiter characters */ - while( c->iOffsetnInput && isDelim(z[c->iOffset]) ){ - c->iOffset++; - } - - /* Count non-delimiter characters. */ - iStartOffset = c->iOffset; - while( c->iOffsetnInput && !isDelim(z[c->iOffset]) ){ - c->iOffset++; - } - - if( c->iOffset>iStartOffset ){ - int n = c->iOffset-iStartOffset; - if( n>c->nAllocated ){ - c->nAllocated = n+20; - c->zToken = sqlite3_realloc(c->zToken, c->nAllocated); - if( c->zToken==NULL ) return SQLITE_NOMEM; - } - porter_stemmer(&z[iStartOffset], n, c->zToken, pnBytes); - *pzToken = c->zToken; - *piStartOffset = iStartOffset; - *piEndOffset = c->iOffset; - *piPosition = c->iToken++; - return SQLITE_OK; - } - } - return SQLITE_DONE; -} - -/* -** The set of routines that implement the porter-stemmer tokenizer -*/ -static const sqlite3_tokenizer_module porterTokenizerModule = { - 0, - porterCreate, - porterDestroy, - porterOpen, - porterClose, - porterNext, -}; - -/* -** Allocate a new porter tokenizer. Return a pointer to the new -** tokenizer in *ppModule -*/ -void sqlite3Fts2PorterTokenizerModule( - sqlite3_tokenizer_module const**ppModule -){ - *ppModule = &porterTokenizerModule; -} - -#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS2) */ diff --git a/ext/fts2/fts2_tokenizer.c b/ext/fts2/fts2_tokenizer.c deleted file mode 100644 index a3d6a6312a..0000000000 --- a/ext/fts2/fts2_tokenizer.c +++ /dev/null @@ -1,371 +0,0 @@ -/* -** 2007 June 22 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -****************************************************************************** -** -** This is part of an SQLite module implementing full-text search. -** This particular file implements the generic tokenizer interface. -*/ - -/* -** The code in this file is only compiled if: -** -** * The FTS2 module is being built as an extension -** (in which case SQLITE_CORE is not defined), or -** -** * The FTS2 module is being built into the core of -** SQLite (in which case SQLITE_ENABLE_FTS2 is defined). -*/ -#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS2) - - -#include "sqlite3.h" -#include "sqlite3ext.h" -SQLITE_EXTENSION_INIT3 - -#include "fts2_hash.h" -#include "fts2_tokenizer.h" -#include - -/* -** Implementation of the SQL scalar function for accessing the underlying -** hash table. This function may be called as follows: -** -** SELECT (); -** SELECT (, ); -** -** where is the name passed as the second argument -** to the sqlite3Fts2InitHashTable() function (e.g. 'fts2_tokenizer'). -** -** If the argument is specified, it must be a blob value -** containing a pointer to be stored as the hash data corresponding -** to the string . If is not specified, then -** the string must already exist in the has table. Otherwise, -** an error is returned. -** -** Whether or not the argument is specified, the value returned -** is a blob containing the pointer stored as the hash data corresponding -** to string (after the hash-table is updated, if applicable). -*/ -static void scalarFunc( - sqlite3_context *context, - int argc, - sqlite3_value **argv -){ - fts2Hash *pHash; - void *pPtr = 0; - const unsigned char *zName; - int nName; - - assert( argc==1 || argc==2 ); - - pHash = (fts2Hash *)sqlite3_user_data(context); - - zName = sqlite3_value_text(argv[0]); - nName = sqlite3_value_bytes(argv[0])+1; - - if( argc==2 ){ - void *pOld; - int n = sqlite3_value_bytes(argv[1]); - if( n!=sizeof(pPtr) ){ - sqlite3_result_error(context, "argument type mismatch", -1); - return; - } - pPtr = *(void **)sqlite3_value_blob(argv[1]); - pOld = sqlite3Fts2HashInsert(pHash, (void *)zName, nName, pPtr); - if( pOld==pPtr ){ - sqlite3_result_error(context, "out of memory", -1); - return; - } - }else{ - pPtr = sqlite3Fts2HashFind(pHash, zName, nName); - if( !pPtr ){ - char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName); - sqlite3_result_error(context, zErr, -1); - sqlite3_free(zErr); - return; - } - } - - sqlite3_result_blob(context, (void *)&pPtr, sizeof(pPtr), SQLITE_TRANSIENT); -} - -#ifdef SQLITE_TEST - -#include -#include - -/* -** Implementation of a special SQL scalar function for testing tokenizers -** designed to be used in concert with the Tcl testing framework. This -** function must be called with two arguments: -** -** SELECT (, ); -** SELECT (, ); -** -** where is the name passed as the second argument -** to the sqlite3Fts2InitHashTable() function (e.g. 'fts2_tokenizer') -** concatenated with the string '_test' (e.g. 'fts2_tokenizer_test'). -** -** The return value is a string that may be interpreted as a Tcl -** list. For each token in the , three elements are -** added to the returned list. The first is the token position, the -** second is the token text (folded, stemmed, etc.) and the third is the -** substring of associated with the token. For example, -** using the built-in "simple" tokenizer: -** -** SELECT fts_tokenizer_test('simple', 'I don't see how'); -** -** will return the string: -** -** "{0 i I 1 dont don't 2 see see 3 how how}" -** -*/ -static void testFunc( - sqlite3_context *context, - int argc, - sqlite3_value **argv -){ - fts2Hash *pHash; - sqlite3_tokenizer_module *p; - sqlite3_tokenizer *pTokenizer = 0; - sqlite3_tokenizer_cursor *pCsr = 0; - - const char *zErr = 0; - - const char *zName; - int nName; - const char *zInput; - int nInput; - - const char *zArg = 0; - - const char *zToken; - int nToken; - int iStart; - int iEnd; - int iPos; - - Tcl_Obj *pRet; - - assert( argc==2 || argc==3 ); - - nName = sqlite3_value_bytes(argv[0]); - zName = (const char *)sqlite3_value_text(argv[0]); - nInput = sqlite3_value_bytes(argv[argc-1]); - zInput = (const char *)sqlite3_value_text(argv[argc-1]); - - if( argc==3 ){ - zArg = (const char *)sqlite3_value_text(argv[1]); - } - - pHash = (fts2Hash *)sqlite3_user_data(context); - p = (sqlite3_tokenizer_module *)sqlite3Fts2HashFind(pHash, zName, nName+1); - - if( !p ){ - char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName); - sqlite3_result_error(context, zErr, -1); - sqlite3_free(zErr); - return; - } - - pRet = Tcl_NewObj(); - Tcl_IncrRefCount(pRet); - - if( SQLITE_OK!=p->xCreate(zArg ? 1 : 0, &zArg, &pTokenizer) ){ - zErr = "error in xCreate()"; - goto finish; - } - pTokenizer->pModule = p; - if( SQLITE_OK!=p->xOpen(pTokenizer, zInput, nInput, &pCsr) ){ - zErr = "error in xOpen()"; - goto finish; - } - pCsr->pTokenizer = pTokenizer; - - while( SQLITE_OK==p->xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos) ){ - Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(iPos)); - Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken)); - zToken = &zInput[iStart]; - nToken = iEnd-iStart; - Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken)); - } - - if( SQLITE_OK!=p->xClose(pCsr) ){ - zErr = "error in xClose()"; - goto finish; - } - if( SQLITE_OK!=p->xDestroy(pTokenizer) ){ - zErr = "error in xDestroy()"; - goto finish; - } - -finish: - if( zErr ){ - sqlite3_result_error(context, zErr, -1); - }else{ - sqlite3_result_text(context, Tcl_GetString(pRet), -1, SQLITE_TRANSIENT); - } - Tcl_DecrRefCount(pRet); -} - -static -int registerTokenizer( - sqlite3 *db, - char *zName, - const sqlite3_tokenizer_module *p -){ - int rc; - sqlite3_stmt *pStmt; - const char zSql[] = "SELECT fts2_tokenizer(?, ?)"; - - rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); - if( rc!=SQLITE_OK ){ - return rc; - } - - sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC); - sqlite3_bind_blob(pStmt, 2, &p, sizeof(p), SQLITE_STATIC); - sqlite3_step(pStmt); - - return sqlite3_finalize(pStmt); -} - -static -int queryFts2Tokenizer( - sqlite3 *db, - char *zName, - const sqlite3_tokenizer_module **pp -){ - int rc; - sqlite3_stmt *pStmt; - const char zSql[] = "SELECT fts2_tokenizer(?)"; - - *pp = 0; - rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); - if( rc!=SQLITE_OK ){ - return rc; - } - - sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC); - if( SQLITE_ROW==sqlite3_step(pStmt) ){ - if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB ){ - memcpy(pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp)); - } - } - - return sqlite3_finalize(pStmt); -} - -void sqlite3Fts2SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule); - -/* -** Implementation of the scalar function fts2_tokenizer_internal_test(). -** This function is used for testing only, it is not included in the -** build unless SQLITE_TEST is defined. -** -** The purpose of this is to test that the fts2_tokenizer() function -** can be used as designed by the C-code in the queryFts2Tokenizer and -** registerTokenizer() functions above. These two functions are repeated -** in the README.tokenizer file as an example, so it is important to -** test them. -** -** To run the tests, evaluate the fts2_tokenizer_internal_test() scalar -** function with no arguments. An assert() will fail if a problem is -** detected. i.e.: -** -** SELECT fts2_tokenizer_internal_test(); -** -*/ -static void intTestFunc( - sqlite3_context *context, - int argc, - sqlite3_value **argv -){ - int rc; - const sqlite3_tokenizer_module *p1; - const sqlite3_tokenizer_module *p2; - sqlite3 *db = (sqlite3 *)sqlite3_user_data(context); - - /* Test the query function */ - sqlite3Fts2SimpleTokenizerModule(&p1); - rc = queryFts2Tokenizer(db, "simple", &p2); - assert( rc==SQLITE_OK ); - assert( p1==p2 ); - rc = queryFts2Tokenizer(db, "nosuchtokenizer", &p2); - assert( rc==SQLITE_ERROR ); - assert( p2==0 ); - assert( 0==strcmp(sqlite3_errmsg(db), "unknown tokenizer: nosuchtokenizer") ); - - /* Test the storage function */ - rc = registerTokenizer(db, "nosuchtokenizer", p1); - assert( rc==SQLITE_OK ); - rc = queryFts2Tokenizer(db, "nosuchtokenizer", &p2); - assert( rc==SQLITE_OK ); - assert( p2==p1 ); - - sqlite3_result_text(context, "ok", -1, SQLITE_STATIC); -} - -#endif - -/* -** Set up SQL objects in database db used to access the contents of -** the hash table pointed to by argument pHash. The hash table must -** been initialized to use string keys, and to take a private copy -** of the key when a value is inserted. i.e. by a call similar to: -** -** sqlite3Fts2HashInit(pHash, FTS2_HASH_STRING, 1); -** -** This function adds a scalar function (see header comment above -** scalarFunc() in this file for details) and, if ENABLE_TABLE is -** defined at compilation time, a temporary virtual table (see header -** comment above struct HashTableVtab) to the database schema. Both -** provide read/write access to the contents of *pHash. -** -** The third argument to this function, zName, is used as the name -** of both the scalar and, if created, the virtual table. -*/ -int sqlite3Fts2InitHashTable( - sqlite3 *db, - fts2Hash *pHash, - const char *zName -){ - int rc = SQLITE_OK; - void *p = (void *)pHash; - const int any = SQLITE_ANY; - char *zTest = 0; - char *zTest2 = 0; - -#ifdef SQLITE_TEST - void *pdb = (void *)db; - zTest = sqlite3_mprintf("%s_test", zName); - zTest2 = sqlite3_mprintf("%s_internal_test", zName); - if( !zTest || !zTest2 ){ - rc = SQLITE_NOMEM; - } -#endif - - if( rc!=SQLITE_OK - || (rc = sqlite3_create_function(db, zName, 1, any, p, scalarFunc, 0, 0)) - || (rc = sqlite3_create_function(db, zName, 2, any, p, scalarFunc, 0, 0)) -#ifdef SQLITE_TEST - || (rc = sqlite3_create_function(db, zTest, 2, any, p, testFunc, 0, 0)) - || (rc = sqlite3_create_function(db, zTest, 3, any, p, testFunc, 0, 0)) - || (rc = sqlite3_create_function(db, zTest2, 0, any, pdb, intTestFunc, 0, 0)) -#endif - ); - - sqlite3_free(zTest); - sqlite3_free(zTest2); - return rc; -} - -#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS2) */ diff --git a/ext/fts2/fts2_tokenizer.h b/ext/fts2/fts2_tokenizer.h deleted file mode 100644 index 8db2048d6b..0000000000 --- a/ext/fts2/fts2_tokenizer.h +++ /dev/null @@ -1,145 +0,0 @@ -/* -** 2006 July 10 -** -** The author disclaims copyright to this source code. -** -************************************************************************* -** Defines the interface to tokenizers used by fulltext-search. There -** are three basic components: -** -** sqlite3_tokenizer_module is a singleton defining the tokenizer -** interface functions. This is essentially the class structure for -** tokenizers. -** -** sqlite3_tokenizer is used to define a particular tokenizer, perhaps -** including customization information defined at creation time. -** -** sqlite3_tokenizer_cursor is generated by a tokenizer to generate -** tokens from a particular input. -*/ -#ifndef _FTS2_TOKENIZER_H_ -#define _FTS2_TOKENIZER_H_ - -/* TODO(shess) Only used for SQLITE_OK and SQLITE_DONE at this time. -** If tokenizers are to be allowed to call sqlite3_*() functions, then -** we will need a way to register the API consistently. -*/ -#include "sqlite3.h" - -/* -** Structures used by the tokenizer interface. When a new tokenizer -** implementation is registered, the caller provides a pointer to -** an sqlite3_tokenizer_module containing pointers to the callback -** functions that make up an implementation. -** -** When an fts2 table is created, it passes any arguments passed to -** the tokenizer clause of the CREATE VIRTUAL TABLE statement to the -** sqlite3_tokenizer_module.xCreate() function of the requested tokenizer -** implementation. The xCreate() function in turn returns an -** sqlite3_tokenizer structure representing the specific tokenizer to -** be used for the fts2 table (customized by the tokenizer clause arguments). -** -** To tokenize an input buffer, the sqlite3_tokenizer_module.xOpen() -** method is called. It returns an sqlite3_tokenizer_cursor object -** that may be used to tokenize a specific input buffer based on -** the tokenization rules supplied by a specific sqlite3_tokenizer -** object. -*/ -typedef struct sqlite3_tokenizer_module sqlite3_tokenizer_module; -typedef struct sqlite3_tokenizer sqlite3_tokenizer; -typedef struct sqlite3_tokenizer_cursor sqlite3_tokenizer_cursor; - -struct sqlite3_tokenizer_module { - - /* - ** Structure version. Should always be set to 0. - */ - int iVersion; - - /* - ** Create a new tokenizer. The values in the argv[] array are the - ** arguments passed to the "tokenizer" clause of the CREATE VIRTUAL - ** TABLE statement that created the fts2 table. For example, if - ** the following SQL is executed: - ** - ** CREATE .. USING fts2( ... , tokenizer arg1 arg2) - ** - ** then argc is set to 2, and the argv[] array contains pointers - ** to the strings "arg1" and "arg2". - ** - ** This method should return either SQLITE_OK (0), or an SQLite error - ** code. If SQLITE_OK is returned, then *ppTokenizer should be set - ** to point at the newly created tokenizer structure. The generic - ** sqlite3_tokenizer.pModule variable should not be initialized by - ** this callback. The caller will do so. - */ - int (*xCreate)( - int argc, /* Size of argv array */ - const char *const*argv, /* Tokenizer argument strings */ - sqlite3_tokenizer **ppTokenizer /* OUT: Created tokenizer */ - ); - - /* - ** Destroy an existing tokenizer. The fts2 module calls this method - ** exactly once for each successful call to xCreate(). - */ - int (*xDestroy)(sqlite3_tokenizer *pTokenizer); - - /* - ** Create a tokenizer cursor to tokenize an input buffer. The caller - ** is responsible for ensuring that the input buffer remains valid - ** until the cursor is closed (using the xClose() method). - */ - int (*xOpen)( - sqlite3_tokenizer *pTokenizer, /* Tokenizer object */ - const char *pInput, int nBytes, /* Input buffer */ - sqlite3_tokenizer_cursor **ppCursor /* OUT: Created tokenizer cursor */ - ); - - /* - ** Destroy an existing tokenizer cursor. The fts2 module calls this - ** method exactly once for each successful call to xOpen(). - */ - int (*xClose)(sqlite3_tokenizer_cursor *pCursor); - - /* - ** Retrieve the next token from the tokenizer cursor pCursor. This - ** method should either return SQLITE_OK and set the values of the - ** "OUT" variables identified below, or SQLITE_DONE to indicate that - ** the end of the buffer has been reached, or an SQLite error code. - ** - ** *ppToken should be set to point at a buffer containing the - ** normalized version of the token (i.e. after any case-folding and/or - ** stemming has been performed). *pnBytes should be set to the length - ** of this buffer in bytes. The input text that generated the token is - ** identified by the byte offsets returned in *piStartOffset and - ** *piEndOffset. - ** - ** The buffer *ppToken is set to point at is managed by the tokenizer - ** implementation. It is only required to be valid until the next call - ** to xNext() or xClose(). - */ - /* TODO(shess) current implementation requires pInput to be - ** nul-terminated. This should either be fixed, or pInput/nBytes - ** should be converted to zInput. - */ - int (*xNext)( - sqlite3_tokenizer_cursor *pCursor, /* Tokenizer cursor */ - const char **ppToken, int *pnBytes, /* OUT: Normalized text for token */ - int *piStartOffset, /* OUT: Byte offset of token in input buffer */ - int *piEndOffset, /* OUT: Byte offset of end of token in input buffer */ - int *piPosition /* OUT: Number of tokens returned before this one */ - ); -}; - -struct sqlite3_tokenizer { - const sqlite3_tokenizer_module *pModule; /* The module for this tokenizer */ - /* Tokenizer implementations will typically add additional fields */ -}; - -struct sqlite3_tokenizer_cursor { - sqlite3_tokenizer *pTokenizer; /* Tokenizer for this cursor. */ - /* Tokenizer implementations will typically add additional fields */ -}; - -#endif /* _FTS2_TOKENIZER_H_ */ diff --git a/ext/fts2/fts2_tokenizer1.c b/ext/fts2/fts2_tokenizer1.c deleted file mode 100644 index fe4f9eb4b5..0000000000 --- a/ext/fts2/fts2_tokenizer1.c +++ /dev/null @@ -1,233 +0,0 @@ -/* -** 2006 Oct 10 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -****************************************************************************** -** -** Implementation of the "simple" full-text-search tokenizer. -*/ - -/* -** The code in this file is only compiled if: -** -** * The FTS2 module is being built as an extension -** (in which case SQLITE_CORE is not defined), or -** -** * The FTS2 module is being built into the core of -** SQLite (in which case SQLITE_ENABLE_FTS2 is defined). -*/ -#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS2) - - -#include -#include -#include -#include - -#include "sqlite3.h" -#include "sqlite3ext.h" -SQLITE_EXTENSION_INIT3 -#include "fts2_tokenizer.h" - -typedef struct simple_tokenizer { - sqlite3_tokenizer base; - char delim[128]; /* flag ASCII delimiters */ -} simple_tokenizer; - -typedef struct simple_tokenizer_cursor { - sqlite3_tokenizer_cursor base; - const char *pInput; /* input we are tokenizing */ - int nBytes; /* size of the input */ - int iOffset; /* current position in pInput */ - int iToken; /* index of next token to be returned */ - char *pToken; /* storage for current token */ - int nTokenAllocated; /* space allocated to zToken buffer */ -} simple_tokenizer_cursor; - - -/* Forward declaration */ -static const sqlite3_tokenizer_module simpleTokenizerModule; - -static int simpleDelim(simple_tokenizer *t, unsigned char c){ - return c<0x80 && t->delim[c]; -} - -/* -** Create a new tokenizer instance. -*/ -static int simpleCreate( - int argc, const char * const *argv, - sqlite3_tokenizer **ppTokenizer -){ - simple_tokenizer *t; - - t = (simple_tokenizer *) sqlite3_malloc(sizeof(*t)); - if( t==NULL ) return SQLITE_NOMEM; - memset(t, 0, sizeof(*t)); - - /* TODO(shess) Delimiters need to remain the same from run to run, - ** else we need to reindex. One solution would be a meta-table to - ** track such information in the database, then we'd only want this - ** information on the initial create. - */ - if( argc>1 ){ - int i, n = strlen(argv[1]); - for(i=0; i=0x80 ){ - sqlite3_free(t); - return SQLITE_ERROR; - } - t->delim[ch] = 1; - } - } else { - /* Mark non-alphanumeric ASCII characters as delimiters */ - int i; - for(i=1; i<0x80; i++){ - t->delim[i] = !((i>='0' && i<='9') || (i>='A' && i<='Z') || - (i>='a' && i<='z')); - } - } - - *ppTokenizer = &t->base; - return SQLITE_OK; -} - -/* -** Destroy a tokenizer -*/ -static int simpleDestroy(sqlite3_tokenizer *pTokenizer){ - sqlite3_free(pTokenizer); - return SQLITE_OK; -} - -/* -** Prepare to begin tokenizing a particular string. The input -** string to be tokenized is pInput[0..nBytes-1]. A cursor -** used to incrementally tokenize this string is returned in -** *ppCursor. -*/ -static int simpleOpen( - sqlite3_tokenizer *pTokenizer, /* The tokenizer */ - const char *pInput, int nBytes, /* String to be tokenized */ - sqlite3_tokenizer_cursor **ppCursor /* OUT: Tokenization cursor */ -){ - simple_tokenizer_cursor *c; - - c = (simple_tokenizer_cursor *) sqlite3_malloc(sizeof(*c)); - if( c==NULL ) return SQLITE_NOMEM; - - c->pInput = pInput; - if( pInput==0 ){ - c->nBytes = 0; - }else if( nBytes<0 ){ - c->nBytes = (int)strlen(pInput); - }else{ - c->nBytes = nBytes; - } - c->iOffset = 0; /* start tokenizing at the beginning */ - c->iToken = 0; - c->pToken = NULL; /* no space allocated, yet. */ - c->nTokenAllocated = 0; - - *ppCursor = &c->base; - return SQLITE_OK; -} - -/* -** Close a tokenization cursor previously opened by a call to -** simpleOpen() above. -*/ -static int simpleClose(sqlite3_tokenizer_cursor *pCursor){ - simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor; - sqlite3_free(c->pToken); - sqlite3_free(c); - return SQLITE_OK; -} - -/* -** Extract the next token from a tokenization cursor. The cursor must -** have been opened by a prior call to simpleOpen(). -*/ -static int simpleNext( - sqlite3_tokenizer_cursor *pCursor, /* Cursor returned by simpleOpen */ - const char **ppToken, /* OUT: *ppToken is the token text */ - int *pnBytes, /* OUT: Number of bytes in token */ - int *piStartOffset, /* OUT: Starting offset of token */ - int *piEndOffset, /* OUT: Ending offset of token */ - int *piPosition /* OUT: Position integer of token */ -){ - simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor; - simple_tokenizer *t = (simple_tokenizer *) pCursor->pTokenizer; - unsigned char *p = (unsigned char *)c->pInput; - - while( c->iOffsetnBytes ){ - int iStartOffset; - - /* Scan past delimiter characters */ - while( c->iOffsetnBytes && simpleDelim(t, p[c->iOffset]) ){ - c->iOffset++; - } - - /* Count non-delimiter characters. */ - iStartOffset = c->iOffset; - while( c->iOffsetnBytes && !simpleDelim(t, p[c->iOffset]) ){ - c->iOffset++; - } - - if( c->iOffset>iStartOffset ){ - int i, n = c->iOffset-iStartOffset; - if( n>c->nTokenAllocated ){ - c->nTokenAllocated = n+20; - c->pToken = sqlite3_realloc(c->pToken, c->nTokenAllocated); - if( c->pToken==NULL ) return SQLITE_NOMEM; - } - for(i=0; ipToken[i] = (ch>='A' && ch<='Z') ? (ch - 'A' + 'a') : ch; - } - *ppToken = c->pToken; - *pnBytes = n; - *piStartOffset = iStartOffset; - *piEndOffset = c->iOffset; - *piPosition = c->iToken++; - - return SQLITE_OK; - } - } - return SQLITE_DONE; -} - -/* -** The set of routines that implement the simple tokenizer -*/ -static const sqlite3_tokenizer_module simpleTokenizerModule = { - 0, - simpleCreate, - simpleDestroy, - simpleOpen, - simpleClose, - simpleNext, -}; - -/* -** Allocate a new simple tokenizer. Return a pointer to the new -** tokenizer in *ppModule -*/ -void sqlite3Fts2SimpleTokenizerModule( - sqlite3_tokenizer_module const**ppModule -){ - *ppModule = &simpleTokenizerModule; -} - -#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS2) */ diff --git a/ext/fts2/mkfts2amal.tcl b/ext/fts2/mkfts2amal.tcl deleted file mode 100644 index 5c8d1e93d7..0000000000 --- a/ext/fts2/mkfts2amal.tcl +++ /dev/null @@ -1,116 +0,0 @@ -#!/usr/bin/tclsh -# -# This script builds a single C code file holding all of FTS2 code. -# The name of the output file is fts2amal.c. To build this file, -# first do: -# -# make target_source -# -# The make target above moves all of the source code files into -# a subdirectory named "tsrc". (This script expects to find the files -# there and will not work if they are not found.) -# -# After the "tsrc" directory has been created and populated, run -# this script: -# -# tclsh mkfts2amal.tcl -# -# The amalgamated FTS2 code will be written into fts2amal.c -# - -# Open the output file and write a header comment at the beginning -# of the file. -# -set out [open fts2amal.c w] -set today [clock format [clock seconds] -format "%Y-%m-%d %H:%M:%S UTC" -gmt 1] -puts $out [subst \ -{/****************************************************************************** -** This file is an amalgamation of separate C source files from the SQLite -** Full Text Search extension 2 (fts2). By combining all the individual C -** code files into this single large file, the entire code can be compiled -** as a one translation unit. This allows many compilers to do optimizations -** that would not be possible if the files were compiled separately. It also -** makes the code easier to import into other projects. -** -** This amalgamation was generated on $today. -*/}] - -# These are the header files used by FTS2. The first time any of these -# files are seen in a #include statement in the C code, include the complete -# text of the file in-line. The file only needs to be included once. -# -foreach hdr { - fts2.h - fts2_hash.h - fts2_tokenizer.h - sqlite3.h - sqlite3ext.h -} { - set available_hdr($hdr) 1 -} - -# 78 stars used for comment formatting. -set s78 \ -{*****************************************************************************} - -# Insert a comment into the code -# -proc section_comment {text} { - global out s78 - set n [string length $text] - set nstar [expr {60 - $n}] - set stars [string range $s78 0 $nstar] - puts $out "/************** $text $stars/" -} - -# Read the source file named $filename and write it into the -# sqlite3.c output file. If any #include statements are seen, -# process them approprately. -# -proc copy_file {filename} { - global seen_hdr available_hdr out - set tail [file tail $filename] - section_comment "Begin file $tail" - set in [open $filename r] - while {![eof $in]} { - set line [gets $in] - if {[regexp {^#\s*include\s+["<]([^">]+)[">]} $line all hdr]} { - if {[info exists available_hdr($hdr)]} { - if {$available_hdr($hdr)} { - section_comment "Include $hdr in the middle of $tail" - copy_file tsrc/$hdr - section_comment "Continuing where we left off in $tail" - } - } elseif {![info exists seen_hdr($hdr)]} { - set seen_hdr($hdr) 1 - puts $out $line - } - } elseif {[regexp {^#ifdef __cplusplus} $line]} { - puts $out "#if 0" - } elseif {[regexp {^#line} $line]} { - # Skip #line directives. - } else { - puts $out $line - } - } - close $in - section_comment "End of $tail" -} - - -# Process the source files. Process files containing commonly -# used subroutines first in order to help the compiler find -# inlining opportunities. -# -foreach file { - fts2.c - fts2_hash.c - fts2_porter.c - fts2_tokenizer.c - fts2_tokenizer1.c - fts2_icu.c -} { - copy_file tsrc/$file -} - -close $out diff --git a/ext/fts3/README.content b/ext/fts3/README.content index ab986754df..b6a75399be 100644 --- a/ext/fts3/README.content +++ b/ext/fts3/README.content @@ -174,5 +174,3 @@ EXTERNAL CONTENT FTS4 TABLES only be useful if the full-text index has somehow become corrupt. It is an error to attempt to rebuild the full-text index maintained by a contentless FTS4 table. - - diff --git a/ext/fts3/README.syntax b/ext/fts3/README.syntax index 01bc80c5fb..d32ae384c5 100644 --- a/ext/fts3/README.syntax +++ b/ext/fts3/README.syntax @@ -62,20 +62,20 @@ matches rows that contain both the "engineering" and "consultancy" tokens in the same column with not more than 10 other words between them. It does not matter which of the two terms occurs first in the document, only that - they be seperated by only 10 tokens or less. The user may also specify + they be separated by only 10 tokens or less. The user may also specify a different required proximity by adding "/N" immediately after the NEAR operator, where N is an integer. For example: MATCH 'engineering NEAR/5 consultancy' - searches for a row containing an instance of each specified token seperated + searches for a row containing an instance of each specified token separated by not more than 5 other tokens. More than one NEAR operator can be used in as sequence. For example this query: MATCH 'reliable NEAR/2 engineering NEAR/5 consultancy' searches for a row that contains an instance of the token "reliable" - seperated by not more than two tokens from an instance of "engineering", + separated by not more than two tokens from an instance of "engineering", which is in turn separated by not more than 5 other tokens from an instance of the term "consultancy". Phrases enclosed in quotes may also be used as arguments to the NEAR operator. @@ -146,7 +146,7 @@ MATCH '(hello world) OR (simple example)' matches documents that contain both "hello" and "world", and documents - that contain both "simple" and "example". It is not possible to forumlate + that contain both "simple" and "example". It is not possible to formulate such a query using the standard syntax. 2) Instead of separating tokens and phrases by whitespace, an AND operator @@ -174,7 +174,7 @@ 4) Unlike in the standard syntax, where the OR operator has a higher precedence than the implicit AND operator, when using the enhanced - syntax implicit and explict AND operators have a higher precedence + syntax implicit and explicit AND operators have a higher precedence than OR operators. Using the enhanced syntax, the following two queries are equivalent: diff --git a/ext/fts3/README.tokenizers b/ext/fts3/README.tokenizers index 7f2345a81f..70bdceff06 100644 --- a/ext/fts3/README.tokenizers +++ b/ext/fts3/README.tokenizers @@ -52,8 +52,10 @@ SECURITY: If the fts3 extension is used in an environment where potentially malicious users may execute arbitrary SQL (i.e. gears), they should be - prevented from invoking the fts3_tokenizer() function, possibly using the - authorisation callback. + prevented from invoking the fts3_tokenizer() function. The + fts3_tokenizer() function is disabled by default. It is only enabled + by SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER. Do not enable it in + security sensitive environments. See "Sample code" below for an example of calling the fts3_tokenizer() function from C code. diff --git a/ext/fts3/fts3.c b/ext/fts3/fts3.c index 748faefec5..f178abafed 100644 --- a/ext/fts3/fts3.c +++ b/ext/fts3/fts3.c @@ -87,7 +87,7 @@ ** Here, array { X } means zero or more occurrences of X, adjacent in ** memory. A "position" is an index of a token in the token stream ** generated by the tokenizer. Note that POS_END and POS_COLUMN occur -** in the same logical place as the position element, and act as sentinals +** in the same logical place as the position element, and act as sentinels ** ending a position list array. POS_END is 0. POS_COLUMN is 1. ** The positions numbers are not stored literally but rather as two more ** than the difference from the prior position, or the just the position plus @@ -295,12 +295,6 @@ # define SQLITE_CORE 1 #endif -#include -#include -#include -#include -#include -#include #include "fts3.h" #ifndef SQLITE_CORE @@ -308,16 +302,25 @@ SQLITE_EXTENSION_INIT1 #endif +typedef struct Fts3HashWrapper Fts3HashWrapper; +struct Fts3HashWrapper { + Fts3Hash hash; /* Hash table */ + int nRef; /* Number of pointers to this object */ +}; + static int fts3EvalNext(Fts3Cursor *pCsr); static int fts3EvalStart(Fts3Cursor *pCsr); static int fts3TermSegReaderCursor( Fts3Cursor *, const char *, int, int, Fts3MultiSegReader **); -#ifndef SQLITE_AMALGAMATION -# if defined(SQLITE_DEBUG) -int sqlite3Fts3Always(int b) { assert( b ); return b; } -int sqlite3Fts3Never(int b) { assert( !b ); return b; } -# endif +/* +** This variable is set to false when running tests for which the on disk +** structures should not be corrupt. Otherwise, true. If it is false, extra +** assert() conditions in the fts3 code are activated - conditions that are +** only true if it is guaranteed that the fts3 database is not corrupt. +*/ +#ifdef SQLITE_DEBUG +int sqlite3_fts3_may_be_corrupt = 1; #endif /* @@ -338,19 +341,15 @@ int sqlite3Fts3PutVarint(char *p, sqlite_int64 v){ } #define GETVARINT_STEP(v, ptr, shift, mask1, mask2, var, ret) \ - v = (v & mask1) | ( (*ptr++) << shift ); \ + v = (v & mask1) | ( (*(const unsigned char*)(ptr++)) << shift ); \ if( (v & mask2)==0 ){ var = v; return ret; } #define GETVARINT_INIT(v, ptr, shift, mask1, mask2, var, ret) \ v = (*ptr++); \ if( (v & mask2)==0 ){ var = v; return ret; } -/* -** Read a 64-bit variable-length integer from memory starting at p[0]. -** Return the number of bytes read, or 0 on error. -** The value is stored in *v. -*/ -int sqlite3Fts3GetVarint(const char *p, sqlite_int64 *v){ - const char *pStart = p; +int sqlite3Fts3GetVarintU(const char *pBuf, sqlite_uint64 *v){ + const unsigned char *p = (const unsigned char*)pBuf; + const unsigned char *pStart = p; u32 a; u64 b; int shift; @@ -370,25 +369,63 @@ int sqlite3Fts3GetVarint(const char *p, sqlite_int64 *v){ return (int)(p - pStart); } +/* +** Read a 64-bit variable-length integer from memory starting at p[0]. +** Return the number of bytes read, or 0 on error. +** The value is stored in *v. +*/ +int sqlite3Fts3GetVarint(const char *pBuf, sqlite_int64 *v){ + return sqlite3Fts3GetVarintU(pBuf, (sqlite3_uint64*)v); +} + +/* +** Read a 64-bit variable-length integer from memory starting at p[0] and +** not extending past pEnd[-1]. +** Return the number of bytes read, or 0 on error. +** The value is stored in *v. +*/ +int sqlite3Fts3GetVarintBounded( + const char *pBuf, + const char *pEnd, + sqlite_int64 *v +){ + const unsigned char *p = (const unsigned char*)pBuf; + const unsigned char *pStart = p; + const unsigned char *pX = (const unsigned char*)pEnd; + u64 b = 0; + int shift; + for(shift=0; shift<=63; shift+=7){ + u64 c = p=0 ); return 5; } @@ -492,6 +529,7 @@ static int fts3DisconnectMethod(sqlite3_vtab *pVtab){ assert( p->pSegments==0 ); /* Free any prepared statements held */ + sqlite3_finalize(p->pSeekStmt); for(i=0; iaStmt); i++){ sqlite3_finalize(p->aStmt[i]); } @@ -556,13 +594,18 @@ static int fts3DestroyMethod(sqlite3_vtab *pVtab){ sqlite3 *db = p->db; /* Database handle */ /* Drop the shadow tables */ - if( p->zContentTbl==0 ){ - fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_content'", zDb, p->zName); - } - fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_segments'", zDb,p->zName); - fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_segdir'", zDb, p->zName); - fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_docsize'", zDb, p->zName); - fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_stat'", zDb, p->zName); + fts3DbExec(&rc, db, + "DROP TABLE IF EXISTS %Q.'%q_segments';" + "DROP TABLE IF EXISTS %Q.'%q_segdir';" + "DROP TABLE IF EXISTS %Q.'%q_docsize';" + "DROP TABLE IF EXISTS %Q.'%q_stat';" + "%s DROP TABLE IF EXISTS %Q.'%q_content';", + zDb, p->zName, + zDb, p->zName, + zDb, p->zName, + zDb, p->zName, + (p->zContentTbl ? "--" : ""), zDb,p->zName + ); /* If everything has worked, invoke fts3DisconnectMethod() to free the ** memory associated with the Fts3Table structure and return SQLITE_OK. @@ -591,6 +634,7 @@ static void fts3DeclareVtab(int *pRc, Fts3Table *p){ zLanguageid = (p->zLanguageid ? p->zLanguageid : "__langid"); sqlite3_vtab_config(p->db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1); + sqlite3_vtab_config(p->db, SQLITE_VTAB_INNOCUOUS); /* Create a list of user columns for the virtual table */ zCols = sqlite3_mprintf("%Q, ", p->azColumn[0]); @@ -794,10 +838,10 @@ static void fts3Appendf( ** memory. */ static char *fts3QuoteId(char const *zInput){ - int nRet; + sqlite3_int64 nRet; char *zRet; nRet = 2 + (int)strlen(zInput)*2 + 1; - zRet = sqlite3_malloc(nRet); + zRet = sqlite3_malloc64(nRet); if( zRet ){ int i; char *z = zRet; @@ -914,6 +958,22 @@ static char *fts3WriteExprList(Fts3Table *p, const char *zFunc, int *pRc){ return zRet; } +/* +** Buffer z contains a positive integer value encoded as utf-8 text. +** Decode this value and store it in *pnOut, returning the number of bytes +** consumed. If an overflow error occurs return a negative value. +*/ +int sqlite3Fts3ReadInt(const char *z, int *pnOut){ + u64 iVal = 0; + int i; + for(i=0; z[i]>='0' && z[i]<='9'; i++){ + iVal = iVal*10 + (z[i] - '0'); + if( iVal>0x7FFFFFFF ) return -1; + } + *pnOut = (int)iVal; + return i; +} + /* ** This function interprets the string at (*pp) as a non-negative integer ** value. It reads the integer and sets *pnOut to the value read, then @@ -929,19 +989,17 @@ static char *fts3WriteExprList(Fts3Table *p, const char *zFunc, int *pRc){ */ static int fts3GobbleInt(const char **pp, int *pnOut){ const int MAX_NPREFIX = 10000000; - const char *p; /* Iterator pointer */ int nInt = 0; /* Output value */ - - for(p=*pp; p[0]>='0' && p[0]<='9'; p++){ - nInt = nInt * 10 + (p[0] - '0'); - if( nInt>MAX_NPREFIX ){ - nInt = 0; - break; - } + int nByte; + nByte = sqlite3Fts3ReadInt(*pp, &nInt); + if( nInt>MAX_NPREFIX ){ + nInt = 0; + } + if( nByte==0 ){ + return SQLITE_ERROR; } - if( p==*pp ) return SQLITE_ERROR; *pnOut = nInt; - *pp = p; + *pp += nByte; return SQLITE_OK; } @@ -978,7 +1036,7 @@ static int fts3PrefixParameter( } } - aIndex = sqlite3_malloc(sizeof(struct Fts3Index) * nIndex); + aIndex = sqlite3_malloc64(sizeof(struct Fts3Index) * nIndex); *apIndex = aIndex; if( !aIndex ){ return SQLITE_NOMEM; @@ -1057,7 +1115,7 @@ static int fts3ContentColumns( if( rc==SQLITE_OK ){ const char **azCol; /* Output array */ - int nStr = 0; /* Size of all column names (incl. 0x00) */ + sqlite3_int64 nStr = 0; /* Size of all column names (incl. 0x00) */ int nCol; /* Number of table columns */ int i; /* Used to iterate through columns */ @@ -1067,11 +1125,11 @@ static int fts3ContentColumns( nCol = sqlite3_column_count(pStmt); for(i=0; ihash; Fts3Table *p = 0; /* Pointer to allocated vtab */ int rc = SQLITE_OK; /* Return code */ int i; /* Iterator variable */ - int nByte; /* Size of allocation used for *p */ + sqlite3_int64 nByte; /* Size of allocation used for *p */ int iCol; /* Column index */ int nString = 0; /* Bytes required to hold all column names */ int nCol = 0; /* Number of columns in the FTS table */ @@ -1153,10 +1211,10 @@ static int fts3InitVtab( nName = (int)strlen(argv[2]) + 1; nByte = sizeof(const char *) * (argc-2); - aCol = (const char **)sqlite3_malloc(nByte); + aCol = (const char **)sqlite3_malloc64(nByte); if( aCol ){ memset((void*)aCol, 0, nByte); - azNotindexed = (char **)sqlite3_malloc(nByte); + azNotindexed = (char **)sqlite3_malloc64(nByte); } if( azNotindexed ){ memset(azNotindexed, 0, nByte); @@ -1217,65 +1275,66 @@ static int fts3InitVtab( break; } } - if( iOpt==SizeofArray(aFts4Opt) ){ - sqlite3Fts3ErrMsg(pzErr, "unrecognized parameter: %s", z); - rc = SQLITE_ERROR; - }else{ - switch( iOpt ){ - case 0: /* MATCHINFO */ - if( strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "fts3", 4) ){ - sqlite3Fts3ErrMsg(pzErr, "unrecognized matchinfo: %s", zVal); - rc = SQLITE_ERROR; - } - bNoDocsize = 1; - break; - - case 1: /* PREFIX */ - sqlite3_free(zPrefix); - zPrefix = zVal; - zVal = 0; - break; - - case 2: /* COMPRESS */ - sqlite3_free(zCompress); - zCompress = zVal; - zVal = 0; - break; - - case 3: /* UNCOMPRESS */ - sqlite3_free(zUncompress); - zUncompress = zVal; - zVal = 0; - break; - - case 4: /* ORDER */ - if( (strlen(zVal)!=3 || sqlite3_strnicmp(zVal, "asc", 3)) - && (strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "desc", 4)) - ){ - sqlite3Fts3ErrMsg(pzErr, "unrecognized order: %s", zVal); - rc = SQLITE_ERROR; - } - bDescIdx = (zVal[0]=='d' || zVal[0]=='D'); - break; - - case 5: /* CONTENT */ - sqlite3_free(zContent); - zContent = zVal; - zVal = 0; - break; - - case 6: /* LANGUAGEID */ - assert( iOpt==6 ); - sqlite3_free(zLanguageid); - zLanguageid = zVal; - zVal = 0; - break; - - case 7: /* NOTINDEXED */ - azNotindexed[nNotindexed++] = zVal; - zVal = 0; - break; - } + switch( iOpt ){ + case 0: /* MATCHINFO */ + if( strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "fts3", 4) ){ + sqlite3Fts3ErrMsg(pzErr, "unrecognized matchinfo: %s", zVal); + rc = SQLITE_ERROR; + } + bNoDocsize = 1; + break; + + case 1: /* PREFIX */ + sqlite3_free(zPrefix); + zPrefix = zVal; + zVal = 0; + break; + + case 2: /* COMPRESS */ + sqlite3_free(zCompress); + zCompress = zVal; + zVal = 0; + break; + + case 3: /* UNCOMPRESS */ + sqlite3_free(zUncompress); + zUncompress = zVal; + zVal = 0; + break; + + case 4: /* ORDER */ + if( (strlen(zVal)!=3 || sqlite3_strnicmp(zVal, "asc", 3)) + && (strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "desc", 4)) + ){ + sqlite3Fts3ErrMsg(pzErr, "unrecognized order: %s", zVal); + rc = SQLITE_ERROR; + } + bDescIdx = (zVal[0]=='d' || zVal[0]=='D'); + break; + + case 5: /* CONTENT */ + sqlite3_free(zContent); + zContent = zVal; + zVal = 0; + break; + + case 6: /* LANGUAGEID */ + assert( iOpt==6 ); + sqlite3_free(zLanguageid); + zLanguageid = zVal; + zVal = 0; + break; + + case 7: /* NOTINDEXED */ + azNotindexed[nNotindexed++] = zVal; + zVal = 0; + break; + + default: + assert( iOpt==SizeofArray(aFts4Opt) ); + sqlite3Fts3ErrMsg(pzErr, "unrecognized parameter: %s", z); + rc = SQLITE_ERROR; + break; } sqlite3_free(zVal); } @@ -1350,7 +1409,7 @@ static int fts3InitVtab( nName + /* zName */ nDb + /* zDb */ nString; /* Space for azColumn strings */ - p = (Fts3Table*)sqlite3_malloc(nByte); + p = (Fts3Table*)sqlite3_malloc64(nByte); if( p==0 ){ rc = SQLITE_NOMEM; goto fts3_init_out; @@ -1363,9 +1422,9 @@ static int fts3InitVtab( p->pTokenizer = pTokenizer; p->nMaxPendingData = FTS3_MAX_PENDING_DATA; p->bHasDocsize = (isFts4 && bNoDocsize==0); - p->bHasStat = isFts4; - p->bFts4 = isFts4; - p->bDescIdx = bDescIdx; + p->bHasStat = (u8)isFts4; + p->bFts4 = (u8)isFts4; + p->bDescIdx = (u8)bDescIdx; p->nAutoincrmerge = 0xff; /* 0xff means setting unknown */ p->zContentTbl = zContent; p->zLanguageid = zLanguageid; @@ -1396,7 +1455,9 @@ static int fts3InitVtab( char *z; int n = 0; z = (char *)sqlite3Fts3NextToken(aCol[iCol], &n); - memcpy(zCsr, z, n); + if( n>0 ){ + memcpy(zCsr, z, n); + } zCsr[n] = '\0'; sqlite3Fts3Dequote(zCsr); p->azColumn[iCol] = zCsr; @@ -1453,6 +1514,10 @@ static int fts3InitVtab( fts3DatabasePageSize(&rc, p); p->nNodeSize = p->nPgsz-35; +#if defined(SQLITE_DEBUG)||defined(SQLITE_TEST) + p->nMergeCount = FTS3_MERGE_COUNT; +#endif + /* Declare the table schema to SQLite. */ fts3DeclareVtab(&rc, p); @@ -1548,6 +1613,10 @@ static int fts3BestIndexMethod(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){ int iDocidLe = -1; /* Index of docid<=x constraint, if present */ int iIdx; + if( p->bLock ){ + return SQLITE_ERROR; + } + /* By default use a full table scan. This is an expensive option, ** so search through the constraints to see if a more efficient ** strategy is possible. @@ -1680,6 +1749,39 @@ static int fts3OpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){ return SQLITE_OK; } +/* +** Finalize the statement handle at pCsr->pStmt. +** +** Or, if that statement handle is one created by fts3CursorSeekStmt(), +** and the Fts3Table.pSeekStmt slot is currently NULL, save the statement +** pointer there instead of finalizing it. +*/ +static void fts3CursorFinalizeStmt(Fts3Cursor *pCsr){ + if( pCsr->bSeekStmt ){ + Fts3Table *p = (Fts3Table *)pCsr->base.pVtab; + if( p->pSeekStmt==0 ){ + p->pSeekStmt = pCsr->pStmt; + sqlite3_reset(pCsr->pStmt); + pCsr->pStmt = 0; + } + pCsr->bSeekStmt = 0; + } + sqlite3_finalize(pCsr->pStmt); +} + +/* +** Free all resources currently held by the cursor passed as the only +** argument. +*/ +static void fts3ClearCursor(Fts3Cursor *pCsr){ + fts3CursorFinalizeStmt(pCsr); + sqlite3Fts3FreeDeferredTokens(pCsr); + sqlite3_free(pCsr->aDoclist); + sqlite3Fts3MIBufferFree(pCsr->pMIBuffer); + sqlite3Fts3ExprFree(pCsr->pExpr); + memset(&(&pCsr->base)[1], 0, sizeof(Fts3Cursor)-sizeof(sqlite3_vtab_cursor)); +} + /* ** Close the cursor. For additional information see the documentation ** on the xClose method of the virtual table interface. @@ -1687,11 +1789,7 @@ static int fts3OpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){ static int fts3CloseMethod(sqlite3_vtab_cursor *pCursor){ Fts3Cursor *pCsr = (Fts3Cursor *)pCursor; assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 ); - sqlite3_finalize(pCsr->pStmt); - sqlite3Fts3ExprFree(pCsr->pExpr); - sqlite3Fts3FreeDeferredTokens(pCsr); - sqlite3_free(pCsr->aDoclist); - sqlite3Fts3MIBufferFree(pCsr->pMIBuffer); + fts3ClearCursor(pCsr); assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 ); sqlite3_free(pCsr); return SQLITE_OK; @@ -1705,20 +1803,27 @@ static int fts3CloseMethod(sqlite3_vtab_cursor *pCursor){ ** ** (or the equivalent for a content=xxx table) and set pCsr->pStmt to ** it. If an error occurs, return an SQLite error code. -** -** Otherwise, set *ppStmt to point to pCsr->pStmt and return SQLITE_OK. */ -static int fts3CursorSeekStmt(Fts3Cursor *pCsr, sqlite3_stmt **ppStmt){ +static int fts3CursorSeekStmt(Fts3Cursor *pCsr){ int rc = SQLITE_OK; if( pCsr->pStmt==0 ){ Fts3Table *p = (Fts3Table *)pCsr->base.pVtab; char *zSql; - zSql = sqlite3_mprintf("SELECT %s WHERE rowid = ?", p->zReadExprlist); - if( !zSql ) return SQLITE_NOMEM; - rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0); - sqlite3_free(zSql); + if( p->pSeekStmt ){ + pCsr->pStmt = p->pSeekStmt; + p->pSeekStmt = 0; + }else{ + zSql = sqlite3_mprintf("SELECT %s WHERE rowid = ?", p->zReadExprlist); + if( !zSql ) return SQLITE_NOMEM; + p->bLock++; + rc = sqlite3_prepare_v3( + p->db, zSql,-1,SQLITE_PREPARE_PERSISTENT,&pCsr->pStmt,0 + ); + p->bLock--; + sqlite3_free(zSql); + } + if( rc==SQLITE_OK ) pCsr->bSeekStmt = 1; } - *ppStmt = pCsr->pStmt; return rc; } @@ -1730,15 +1835,17 @@ static int fts3CursorSeekStmt(Fts3Cursor *pCsr, sqlite3_stmt **ppStmt){ static int fts3CursorSeek(sqlite3_context *pContext, Fts3Cursor *pCsr){ int rc = SQLITE_OK; if( pCsr->isRequireSeek ){ - sqlite3_stmt *pStmt = 0; - - rc = fts3CursorSeekStmt(pCsr, &pStmt); + rc = fts3CursorSeekStmt(pCsr); if( rc==SQLITE_OK ){ + Fts3Table *pTab = (Fts3Table*)pCsr->base.pVtab; + pTab->bLock++; sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iPrevId); pCsr->isRequireSeek = 0; if( SQLITE_ROW==sqlite3_step(pCsr->pStmt) ){ + pTab->bLock--; return SQLITE_OK; }else{ + pTab->bLock--; rc = sqlite3_reset(pCsr->pStmt); if( rc==SQLITE_OK && ((Fts3Table *)pCsr->base.pVtab)->zContentTbl==0 ){ /* If no row was found and no error has occurred, then the %_content @@ -1784,9 +1891,10 @@ static int fts3ScanInteriorNode( const char *zCsr = zNode; /* Cursor to iterate through node */ const char *zEnd = &zCsr[nNode];/* End of interior node buffer */ char *zBuffer = 0; /* Buffer to load terms into */ - int nAlloc = 0; /* Size of allocated buffer */ + i64 nAlloc = 0; /* Size of allocated buffer */ int isFirstTerm = 1; /* True when processing first term on page */ - sqlite3_int64 iChild; /* Block id of child node to descend to */ + u64 iChild; /* Block id of child node to descend to */ + int nBuffer = 0; /* Total term size */ /* Skip over the 'height' varint that occurs at the start of every ** interior node. Then load the blockid of the left-child of the b-tree @@ -1801,8 +1909,8 @@ static int fts3ScanInteriorNode( ** table, then there are always 20 bytes of zeroed padding following the ** nNode bytes of content (see sqlite3Fts3ReadBlock() for details). */ - zCsr += sqlite3Fts3GetVarint(zCsr, &iChild); - zCsr += sqlite3Fts3GetVarint(zCsr, &iChild); + zCsr += sqlite3Fts3GetVarintU(zCsr, &iChild); + zCsr += sqlite3Fts3GetVarintU(zCsr, &iChild); if( zCsr>zEnd ){ return FTS_CORRUPT_VTAB; } @@ -1811,24 +1919,28 @@ static int fts3ScanInteriorNode( int cmp; /* memcmp() result */ int nSuffix; /* Size of term suffix */ int nPrefix = 0; /* Size of term prefix */ - int nBuffer; /* Total term size */ /* Load the next term on the node into zBuffer. Use realloc() to expand ** the size of zBuffer if required. */ if( !isFirstTerm ){ zCsr += fts3GetVarint32(zCsr, &nPrefix); + if( nPrefix>nBuffer ){ + rc = FTS_CORRUPT_VTAB; + goto finish_scan; + } } isFirstTerm = 0; zCsr += fts3GetVarint32(zCsr, &nSuffix); - if( nPrefix<0 || nSuffix<0 || &zCsr[nSuffix]>zEnd ){ + assert( nPrefix>=0 && nSuffix>=0 ); + if( nPrefix>zCsr-zNode || nSuffix>zEnd-zCsr || nSuffix==0 ){ rc = FTS_CORRUPT_VTAB; goto finish_scan; } - if( nPrefix+nSuffix>nAlloc ){ + if( (i64)nPrefix+nSuffix>nAlloc ){ char *zNew; - nAlloc = (nPrefix+nSuffix) * 2; - zNew = (char *)sqlite3_realloc(zBuffer, nAlloc); + nAlloc = ((i64)nPrefix+nSuffix) * 2; + zNew = (char *)sqlite3_realloc64(zBuffer, nAlloc); if( !zNew ){ rc = SQLITE_NOMEM; goto finish_scan; @@ -1851,20 +1963,20 @@ static int fts3ScanInteriorNode( */ cmp = memcmp(zTerm, zBuffer, (nBuffer>nTerm ? nTerm : nBuffer)); if( piFirst && (cmp<0 || (cmp==0 && nBuffer>nTerm)) ){ - *piFirst = iChild; + *piFirst = (i64)iChild; piFirst = 0; } if( piLast && cmp<0 ){ - *piLast = iChild; + *piLast = (i64)iChild; piLast = 0; } iChild++; }; - if( piFirst ) *piFirst = iChild; - if( piLast ) *piLast = iChild; + if( piFirst ) *piFirst = (i64)iChild; + if( piLast ) *piLast = (i64)iChild; finish_scan: sqlite3_free(zBuffer); @@ -1909,7 +2021,7 @@ static int fts3SelectLeaf( fts3GetVarint32(zNode, &iHeight); rc = fts3ScanInteriorNode(zTerm, nTerm, zNode, nNode, piLeaf, piLeaf2); - assert( !piLeaf2 || !piLeaf || rc!=SQLITE_OK || (*piLeaf<=*piLeaf2) ); + assert_fts3_nc( !piLeaf2 || !piLeaf || rc!=SQLITE_OK || (*piLeaf<=*piLeaf2) ); if( rc==SQLITE_OK && iHeight>1 ){ char *zBlob = 0; /* Blob read from %_segments table */ @@ -1929,7 +2041,13 @@ static int fts3SelectLeaf( rc = sqlite3Fts3ReadBlock(p, piLeaf?*piLeaf:*piLeaf2, &zBlob, &nBlob, 0); } if( rc==SQLITE_OK ){ - rc = fts3SelectLeaf(p, zTerm, nTerm, zBlob, nBlob, piLeaf, piLeaf2); + int iNewHeight = 0; + fts3GetVarint32(zBlob, &iNewHeight); + if( iNewHeight>=iHeight ){ + rc = FTS_CORRUPT_VTAB; + }else{ + rc = fts3SelectLeaf(p, zTerm, nTerm, zBlob, nBlob, piLeaf, piLeaf2); + } } sqlite3_free(zBlob); } @@ -1946,7 +2064,7 @@ static void fts3PutDeltaVarint( sqlite3_int64 *piPrev, /* IN/OUT: Previous value written to list */ sqlite3_int64 iVal /* Write this value to the list */ ){ - assert( iVal-*piPrev > 0 || (*piPrev==0 && iVal==0) ); + assert_fts3_nc( iVal-*piPrev > 0 || (*piPrev==0 && iVal==0) ); *pp += sqlite3Fts3PutVarint(*pp, iVal-*piPrev); *piPrev = iVal; } @@ -2034,10 +2152,11 @@ static void fts3ColumnlistCopy(char **pp, char **ppPoslist){ } /* -** Value used to signify the end of an position-list. This is safe because -** it is not possible to have a document with 2^31 terms. +** Value used to signify the end of an position-list. This must be +** as large or larger than any value that might appear on the +** position-list, even a position list that has been corrupted. */ -#define POSITION_LIST_END 0x7fffffff +#define POSITION_LIST_END LARGEST_INT64 /* ** This function is used to help parse position-lists. When this function is @@ -2062,7 +2181,9 @@ static void fts3ReadNextPos( sqlite3_int64 *pi /* IN/OUT: Value read from position-list */ ){ if( (**pp)&0xFE ){ - fts3GetDeltaVarint(pp, pi); + int iVal; + *pp += fts3GetVarint32((*pp), &iVal); + *pi += iVal; *pi -= 2; }else{ *pi = POSITION_LIST_END; @@ -2096,7 +2217,7 @@ static int fts3PutColNumber(char **pp, int iCol){ ** updated appropriately. The caller is responsible for insuring ** that there is enough space in *pp to hold the complete output. */ -static void fts3PoslistMerge( +static int fts3PoslistMerge( char **pp, /* Output buffer */ char **pp1, /* Left input list */ char **pp2 /* Right input list */ @@ -2109,12 +2230,18 @@ static void fts3PoslistMerge( int iCol1; /* The current column index in pp1 */ int iCol2; /* The current column index in pp2 */ - if( *p1==POS_COLUMN ) fts3GetVarint32(&p1[1], &iCol1); - else if( *p1==POS_END ) iCol1 = POSITION_LIST_END; + if( *p1==POS_COLUMN ){ + fts3GetVarint32(&p1[1], &iCol1); + if( iCol1==0 ) return FTS_CORRUPT_VTAB; + } + else if( *p1==POS_END ) iCol1 = 0x7fffffff; else iCol1 = 0; - if( *p2==POS_COLUMN ) fts3GetVarint32(&p2[1], &iCol2); - else if( *p2==POS_END ) iCol2 = POSITION_LIST_END; + if( *p2==POS_COLUMN ){ + fts3GetVarint32(&p2[1], &iCol2); + if( iCol2==0 ) return FTS_CORRUPT_VTAB; + } + else if( *p2==POS_END ) iCol2 = 0x7fffffff; else iCol2 = 0; if( iCol1==iCol2 ){ @@ -2136,6 +2263,9 @@ static void fts3PoslistMerge( */ fts3GetDeltaVarint(&p1, &i1); fts3GetDeltaVarint(&p2, &i2); + if( i1<2 || i2<2 ){ + break; + } do { fts3PutDeltaVarint(&p, &iPrev, (i1=pEnd ){ *pp = 0; }else{ - sqlite3_int64 iVal; - *pp += sqlite3Fts3GetVarint(*pp, &iVal); + u64 iVal; + *pp += sqlite3Fts3GetVarintU(*pp, &iVal); if( bDescIdx ){ - *pVal -= iVal; + *pVal = (i64)((u64)*pVal - iVal); }else{ - *pVal += iVal; + *pVal = (i64)((u64)*pVal + iVal); } } } @@ -2409,14 +2544,16 @@ static void fts3PutDeltaVarint3( int *pbFirst, /* IN/OUT: True after first int written */ sqlite3_int64 iVal /* Write this value to the list */ ){ - sqlite3_int64 iWrite; + sqlite3_uint64 iWrite; if( bDescIdx==0 || *pbFirst==0 ){ - iWrite = iVal - *piPrev; + assert_fts3_nc( *pbFirst==0 || iVal>=*piPrev ); + iWrite = (u64)iVal - (u64)*piPrev; }else{ - iWrite = *piPrev - iVal; + assert_fts3_nc( *piPrev>=iVal ); + iWrite = (u64)*piPrev - (u64)iVal; } assert( *pbFirst || *piPrev==0 ); - assert( *pbFirst==0 || iWrite>0 ); + assert_fts3_nc( *pbFirst==0 || iWrite>0 ); *pp += sqlite3Fts3PutVarint(*pp, iWrite); *piPrev = iVal; *pbFirst = 1; @@ -2432,7 +2569,8 @@ static void fts3PutDeltaVarint3( ** Using this makes it easier to write code that can merge doclists that are ** sorted in either ascending or descending order. */ -#define DOCID_CMP(i1, i2) ((bDescDoclist?-1:1) * (i1-i2)) +/* #define DOCID_CMP(i1, i2) ((bDescDoclist?-1:1) * (i64)((u64)i1-i2)) */ +#define DOCID_CMP(i1, i2) ((bDescDoclist?-1:1) * (i1>i2?1:((i1==i2)?0:-1))) /* ** This function does an "OR" merge of two doclists (output contains all @@ -2454,6 +2592,7 @@ static int fts3DoclistOrMerge( char *a2, int n2, /* Second doclist */ char **paOut, int *pnOut /* OUT: Malloc'd doclist */ ){ + int rc = SQLITE_OK; sqlite3_int64 i1 = 0; sqlite3_int64 i2 = 0; sqlite3_int64 iPrev = 0; @@ -2494,10 +2633,10 @@ static int fts3DoclistOrMerge( ** sizes of the two inputs, plus enough space for exactly one of the input ** docids to grow. ** - ** A symetric argument may be made if the doclists are in descending + ** A symmetric argument may be made if the doclists are in descending ** order. */ - aOut = sqlite3_malloc(n1+n2+FTS3_VARINT_MAX-1); + aOut = sqlite3_malloc64((i64)n1+n2+FTS3_VARINT_MAX-1+FTS3_BUFFER_PADDING); if( !aOut ) return SQLITE_NOMEM; p = aOut; @@ -2508,7 +2647,8 @@ static int fts3DoclistOrMerge( if( p2 && p1 && iDiff==0 ){ fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1); - fts3PoslistMerge(&p, &p1, &p2); + rc = fts3PoslistMerge(&p, &p1, &p2); + if( rc ) break; fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1); fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2); }else if( !p2 || (p1 && iDiff<0) ){ @@ -2520,12 +2660,20 @@ static int fts3DoclistOrMerge( fts3PoslistCopy(&p, &p2); fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2); } + + assert( (p-aOut)<=((p1?(p1-a1):n1)+(p2?(p2-a2):n2)+FTS3_VARINT_MAX-1) ); } + if( rc!=SQLITE_OK ){ + sqlite3_free(aOut); + p = aOut = 0; + }else{ + assert( (p-aOut)<=n1+n2+FTS3_VARINT_MAX-1 ); + memset(&aOut[(p-aOut)], 0, FTS3_BUFFER_PADDING); + } *paOut = aOut; *pnOut = (int)(p-aOut); - assert( *pnOut<=n1+n2+FTS3_VARINT_MAX-1 ); - return SQLITE_OK; + return rc; } /* @@ -2560,7 +2708,7 @@ static int fts3DoclistPhraseMerge( assert( nDist>0 ); if( bDescDoclist ){ - aOut = sqlite3_malloc(*pnRight + FTS3_VARINT_MAX); + aOut = sqlite3_malloc64((sqlite3_int64)*pnRight + FTS3_VARINT_MAX); if( aOut==0 ) return SQLITE_NOMEM; }else{ aOut = aRight; @@ -2631,7 +2779,7 @@ int sqlite3Fts3FirstFilter( fts3ColumnlistCopy(0, &p); } - while( paaOutput[0] = sqlite3_malloc(nDoclist + FTS3_VARINT_MAX + 1); + pTS->aaOutput[0] = sqlite3_malloc64((i64)nDoclist + FTS3_VARINT_MAX + 1); pTS->anOutput[0] = nDoclist; if( pTS->aaOutput[0] ){ memcpy(pTS->aaOutput[0], aDoclist, nDoclist); + memset(&pTS->aaOutput[0][nDoclist], 0, FTS3_VARINT_MAX); }else{ return SQLITE_NOMEM; } @@ -2795,8 +2944,8 @@ static int fts3SegReaderCursorAppend( ){ if( (pCsr->nSegment%16)==0 ){ Fts3SegReader **apNew; - int nByte = (pCsr->nSegment + 16)*sizeof(Fts3SegReader*); - apNew = (Fts3SegReader **)sqlite3_realloc(pCsr->apSegment, nByte); + sqlite3_int64 nByte = (pCsr->nSegment + 16)*sizeof(Fts3SegReader*); + apNew = (Fts3SegReader **)sqlite3_realloc64(pCsr->apSegment, nByte); if( !apNew ){ sqlite3Fts3SegReaderFree(pNew); return SQLITE_NOMEM; @@ -2835,7 +2984,7 @@ static int fts3SegReaderCursor( ** Fts3SegReaderPending might segfault, as the data structures used by ** fts4aux are not completely populated. So it's easiest to filter these ** calls out here. */ - if( iLevel<0 && p->aIndex ){ + if( iLevel<0 && p->aIndex && p->iPrevLangid==iLangid ){ Fts3SegReader *pSeg = 0; rc = sqlite3Fts3SegReaderPending(p, iIndex, zTerm, nTerm, isPrefix||isScan, &pSeg); if( rc==SQLITE_OK && pSeg ){ @@ -2860,7 +3009,7 @@ static int fts3SegReaderCursor( /* If zTerm is not NULL, and this segment is not stored entirely on its ** root node, the range of leaves scanned can be reduced. Do this. */ - if( iStartBlock && zTerm ){ + if( iStartBlock && zTerm && zRoot ){ sqlite3_int64 *pi = (isPrefix ? &iLeavesEndBlock : 0); rc = fts3SelectLeaf(p, zTerm, nTerm, zRoot, nRoot, &iStartBlock, pi); if( rc!=SQLITE_OK ) goto finished; @@ -3098,6 +3247,8 @@ static int fts3NextMethod(sqlite3_vtab_cursor *pCursor){ int rc; Fts3Cursor *pCsr = (Fts3Cursor *)pCursor; if( pCsr->eSearch==FTS3_DOCID_SEARCH || pCsr->eSearch==FTS3_FULLSCAN_SEARCH ){ + Fts3Table *pTab = (Fts3Table*)pCursor->pVtab; + pTab->bLock++; if( SQLITE_ROW!=sqlite3_step(pCsr->pStmt) ){ pCsr->isEof = 1; rc = sqlite3_reset(pCsr->pStmt); @@ -3105,6 +3256,7 @@ static int fts3NextMethod(sqlite3_vtab_cursor *pCursor){ pCsr->iPrevId = sqlite3_column_int64(pCsr->pStmt, 0); rc = SQLITE_OK; } + pTab->bLock--; }else{ rc = fts3EvalNext((Fts3Cursor *)pCursor); } @@ -3112,18 +3264,6 @@ static int fts3NextMethod(sqlite3_vtab_cursor *pCursor){ return rc; } -/* -** The following are copied from sqliteInt.h. -** -** Constants for the largest and smallest possible 64-bit signed integers. -** These macros are designed to work correctly on both 32-bit and 64-bit -** compilers. -*/ -#ifndef SQLITE_AMALGAMATION -# define LARGEST_INT64 (0xffffffff|(((sqlite3_int64)0x7fffffff)<<32)) -# define SMALLEST_INT64 (((sqlite3_int64)-1) - LARGEST_INT64) -#endif - /* ** If the numeric type of argument pVal is "integer", then return it ** converted to a 64-bit signed integer. Otherwise, return a copy of @@ -3177,6 +3317,10 @@ static int fts3FilterMethod( UNUSED_PARAMETER(idxStr); UNUSED_PARAMETER(nVal); + if( p->bLock ){ + return SQLITE_ERROR; + } + eSearch = (idxNum & 0x0000FFFF); assert( eSearch>=0 && eSearch<=(FTS3_FULLTEXT_SEARCH+p->nColumn) ); assert( p->pSegments==0 ); @@ -3190,11 +3334,7 @@ static int fts3FilterMethod( assert( iIdx==nVal ); /* In case the cursor has been used before, clear it now. */ - sqlite3_finalize(pCsr->pStmt); - sqlite3_free(pCsr->aDoclist); - sqlite3Fts3MIBufferFree(pCsr->pMIBuffer); - sqlite3Fts3ExprFree(pCsr->pExpr); - memset(&pCursor[1], 0, sizeof(Fts3Cursor)-sizeof(sqlite3_vtab_cursor)); + fts3ClearCursor(pCsr); /* Set the lower and upper bounds on docids to return */ pCsr->iMinDocid = fts3DocidRange(pDocidGe, SMALLEST_INT64); @@ -3252,13 +3392,17 @@ static int fts3FilterMethod( ); } if( zSql ){ - rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0); + p->bLock++; + rc = sqlite3_prepare_v3( + p->db,zSql,-1,SQLITE_PREPARE_PERSISTENT,&pCsr->pStmt,0 + ); + p->bLock--; sqlite3_free(zSql); }else{ rc = SQLITE_NOMEM; } }else if( eSearch==FTS3_DOCID_SEARCH ){ - rc = fts3CursorSeekStmt(pCsr, &pCsr->pStmt); + rc = fts3CursorSeekStmt(pCsr); if( rc==SQLITE_OK ){ rc = sqlite3_bind_value(pCsr->pStmt, 1, pCons); } @@ -3273,7 +3417,12 @@ static int fts3FilterMethod( ** routine to find out if it has reached the end of a result set. */ static int fts3EofMethod(sqlite3_vtab_cursor *pCursor){ - return ((Fts3Cursor *)pCursor)->isEof; + Fts3Cursor *pCsr = (Fts3Cursor*)pCursor; + if( pCsr->isEof ){ + fts3ClearCursor(pCsr); + pCsr->isEof = 1; + } + return pCsr->isEof; } /* @@ -3311,33 +3460,37 @@ static int fts3ColumnMethod( /* The column value supplied by SQLite must be in range. */ assert( iCol>=0 && iCol<=p->nColumn+2 ); - if( iCol==p->nColumn+1 ){ - /* This call is a request for the "docid" column. Since "docid" is an - ** alias for "rowid", use the xRowid() method to obtain the value. - */ - sqlite3_result_int64(pCtx, pCsr->iPrevId); - }else if( iCol==p->nColumn ){ - /* The extra column whose name is the same as the table. - ** Return a blob which is a pointer to the cursor. */ - sqlite3_result_blob(pCtx, &pCsr, sizeof(pCsr), SQLITE_TRANSIENT); - }else if( iCol==p->nColumn+2 && pCsr->pExpr ){ - sqlite3_result_int64(pCtx, pCsr->iLangid); - }else{ - /* The requested column is either a user column (one that contains - ** indexed data), or the language-id column. */ - rc = fts3CursorSeek(0, pCsr); + switch( iCol-p->nColumn ){ + case 0: + /* The special 'table-name' column */ + sqlite3_result_pointer(pCtx, pCsr, "fts3cursor", 0); + break; - if( rc==SQLITE_OK ){ - if( iCol==p->nColumn+2 ){ - int iLangid = 0; - if( p->zLanguageid ){ - iLangid = sqlite3_column_int(pCsr->pStmt, p->nColumn+1); - } - sqlite3_result_int(pCtx, iLangid); - }else if( sqlite3_data_count(pCsr->pStmt)>(iCol+1) ){ + case 1: + /* The docid column */ + sqlite3_result_int64(pCtx, pCsr->iPrevId); + break; + + case 2: + if( pCsr->pExpr ){ + sqlite3_result_int64(pCtx, pCsr->iLangid); + break; + }else if( p->zLanguageid==0 ){ + sqlite3_result_int(pCtx, 0); + break; + }else{ + iCol = p->nColumn; + /* no break */ deliberate_fall_through + } + + default: + /* A user column. Or, if this is a full-table scan, possibly the + ** language-id column. Seek the cursor. */ + rc = fts3CursorSeek(0, pCsr); + if( rc==SQLITE_OK && sqlite3_data_count(pCsr->pStmt)-1>iCol ){ sqlite3_result_value(pCtx, sqlite3_column_value(pCsr->pStmt, iCol+1)); } - } + break; } assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 ); @@ -3386,8 +3539,10 @@ static int fts3SyncMethod(sqlite3_vtab *pVtab){ const u32 nMinMerge = 64; /* Minimum amount of incr-merge work to do */ Fts3Table *p = (Fts3Table*)pVtab; - int rc = sqlite3Fts3PendingTermsFlush(p); + int rc; + i64 iLastRowid = sqlite3_last_insert_rowid(p->db); + rc = sqlite3Fts3PendingTermsFlush(p); if( rc==SQLITE_OK && p->nLeafAdd>(nMinMerge/16) && p->nAutoincrmerge && p->nAutoincrmerge!=0xff @@ -3402,6 +3557,7 @@ static int fts3SyncMethod(sqlite3_vtab *pVtab){ if( A>(int)nMinMerge ) rc = sqlite3Fts3Incrmerge(p, A, p->nAutoincrmerge); } sqlite3Fts3SegmentsClose(p); + sqlite3_set_last_insert_rowid(p->db, iLastRowid); return rc; } @@ -3414,17 +3570,11 @@ static int fts3SyncMethod(sqlite3_vtab *pVtab){ static int fts3SetHasStat(Fts3Table *p){ int rc = SQLITE_OK; if( p->bHasStat==2 ){ - const char *zFmt ="SELECT 1 FROM %Q.sqlite_master WHERE tbl_name='%q_stat'"; - char *zSql = sqlite3_mprintf(zFmt, p->zDb, p->zName); - if( zSql ){ - sqlite3_stmt *pStmt = 0; - rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); - if( rc==SQLITE_OK ){ - int bHasStat = (sqlite3_step(pStmt)==SQLITE_ROW); - rc = sqlite3_finalize(pStmt); - if( rc==SQLITE_OK ) p->bHasStat = bHasStat; - } - sqlite3_free(zSql); + char *zTbl = sqlite3_mprintf("%s_stat", p->zName); + if( zTbl ){ + int res = sqlite3_table_column_metadata(p->db, p->zDb, zTbl, 0,0,0,0,0,0); + sqlite3_free(zTbl); + p->bHasStat = (res==SQLITE_OK); }else{ rc = SQLITE_NOMEM; } @@ -3437,14 +3587,20 @@ static int fts3SetHasStat(Fts3Table *p){ */ static int fts3BeginMethod(sqlite3_vtab *pVtab){ Fts3Table *p = (Fts3Table*)pVtab; + int rc; UNUSED_PARAMETER(pVtab); assert( p->pSegments==0 ); assert( p->nPendingData==0 ); assert( p->inTransaction!=1 ); - TESTONLY( p->inTransaction = 1 ); - TESTONLY( p->mxSavepoint = -1; ); p->nLeafAdd = 0; - return fts3SetHasStat(p); + rc = fts3SetHasStat(p); +#ifdef SQLITE_DEBUG + if( rc==SQLITE_OK ){ + p->inTransaction = 1; + p->mxSavepoint = -1; + } +#endif + return rc; } /* @@ -3531,18 +3687,17 @@ static int fts3FunctionArg( sqlite3_value *pVal, /* argv[0] passed to function */ Fts3Cursor **ppCsr /* OUT: Store cursor handle here */ ){ - Fts3Cursor *pRet; - if( sqlite3_value_type(pVal)!=SQLITE_BLOB - || sqlite3_value_bytes(pVal)!=sizeof(Fts3Cursor *) - ){ + int rc; + *ppCsr = (Fts3Cursor*)sqlite3_value_pointer(pVal, "fts3cursor"); + if( (*ppCsr)!=0 ){ + rc = SQLITE_OK; + }else{ char *zErr = sqlite3_mprintf("illegal first argument to %s", zFunc); sqlite3_result_error(pContext, zErr, -1); sqlite3_free(zErr); - return SQLITE_ERROR; + rc = SQLITE_ERROR; } - memcpy(&pRet, sqlite3_value_blob(pVal), sizeof(Fts3Cursor *)); - *ppCsr = pRet; - return SQLITE_OK; + return rc; } /* @@ -3574,9 +3729,13 @@ static void fts3SnippetFunc( switch( nVal ){ case 6: nToken = sqlite3_value_int(apVal[5]); + /* no break */ deliberate_fall_through case 5: iCol = sqlite3_value_int(apVal[4]); + /* no break */ deliberate_fall_through case 4: zEllipsis = (const char*)sqlite3_value_text(apVal[3]); + /* no break */ deliberate_fall_through case 3: zEnd = (const char*)sqlite3_value_text(apVal[2]); + /* no break */ deliberate_fall_through case 2: zStart = (const char*)sqlite3_value_text(apVal[1]); } if( !zEllipsis || !zEnd || !zStart ){ @@ -3730,6 +3889,8 @@ static int fts3RenameMethod( rc = sqlite3Fts3PendingTermsFlush(p); } + p->bIgnoreSavepoint = 1; + if( p->zContentTbl==0 ){ fts3DbExec(&rc, db, "ALTER TABLE %Q.'%q_content' RENAME TO '%q_content';", @@ -3757,6 +3918,8 @@ static int fts3RenameMethod( "ALTER TABLE %Q.'%q_segdir' RENAME TO '%q_segdir';", p->zDb, p->zName, zName ); + + p->bIgnoreSavepoint = 0; return rc; } @@ -3767,12 +3930,28 @@ static int fts3RenameMethod( */ static int fts3SavepointMethod(sqlite3_vtab *pVtab, int iSavepoint){ int rc = SQLITE_OK; - UNUSED_PARAMETER(iSavepoint); - assert( ((Fts3Table *)pVtab)->inTransaction ); - assert( ((Fts3Table *)pVtab)->mxSavepoint < iSavepoint ); - TESTONLY( ((Fts3Table *)pVtab)->mxSavepoint = iSavepoint ); - if( ((Fts3Table *)pVtab)->bIgnoreSavepoint==0 ){ - rc = fts3SyncMethod(pVtab); + Fts3Table *pTab = (Fts3Table*)pVtab; + assert( pTab->inTransaction ); + assert( pTab->mxSavepoint<=iSavepoint ); + TESTONLY( pTab->mxSavepoint = iSavepoint ); + + if( pTab->bIgnoreSavepoint==0 ){ + if( fts3HashCount(&pTab->aIndex[0].hPending)>0 ){ + char *zSql = sqlite3_mprintf("INSERT INTO %Q.%Q(%Q) VALUES('flush')", + pTab->zDb, pTab->zName, pTab->zName + ); + if( zSql ){ + pTab->bIgnoreSavepoint = 1; + rc = sqlite3_exec(pTab->db, zSql, 0, 0, 0); + pTab->bIgnoreSavepoint = 0; + sqlite3_free(zSql); + }else{ + rc = SQLITE_NOMEM; + } + } + if( rc==SQLITE_OK ){ + pTab->iSavepoint = iSavepoint+1; + } } return rc; } @@ -3783,12 +3962,11 @@ static int fts3SavepointMethod(sqlite3_vtab *pVtab, int iSavepoint){ ** This is a no-op. */ static int fts3ReleaseMethod(sqlite3_vtab *pVtab, int iSavepoint){ - TESTONLY( Fts3Table *p = (Fts3Table*)pVtab ); - UNUSED_PARAMETER(iSavepoint); - UNUSED_PARAMETER(pVtab); - assert( p->inTransaction ); - assert( p->mxSavepoint >= iSavepoint ); - TESTONLY( p->mxSavepoint = iSavepoint-1 ); + Fts3Table *pTab = (Fts3Table*)pVtab; + assert( pTab->inTransaction ); + assert( pTab->mxSavepoint >= iSavepoint ); + TESTONLY( pTab->mxSavepoint = iSavepoint-1 ); + pTab->iSavepoint = iSavepoint; return SQLITE_OK; } @@ -3798,17 +3976,67 @@ static int fts3ReleaseMethod(sqlite3_vtab *pVtab, int iSavepoint){ ** Discard the contents of the pending terms table. */ static int fts3RollbackToMethod(sqlite3_vtab *pVtab, int iSavepoint){ - Fts3Table *p = (Fts3Table*)pVtab; + Fts3Table *pTab = (Fts3Table*)pVtab; UNUSED_PARAMETER(iSavepoint); - assert( p->inTransaction ); - assert( p->mxSavepoint >= iSavepoint ); - TESTONLY( p->mxSavepoint = iSavepoint ); - sqlite3Fts3PendingTermsClear(p); + assert( pTab->inTransaction ); + TESTONLY( pTab->mxSavepoint = iSavepoint ); + if( (iSavepoint+1)<=pTab->iSavepoint ){ + sqlite3Fts3PendingTermsClear(pTab); + } return SQLITE_OK; } +/* +** Return true if zName is the extension on one of the shadow tables used +** by this module. +*/ +static int fts3ShadowName(const char *zName){ + static const char *azName[] = { + "content", "docsize", "segdir", "segments", "stat", + }; + unsigned int i; + for(i=0; ibFts4 ? 4 : 3, zSchema, zTabname, sqlite3_errstr(rc)); + if( *pzErr ) rc = SQLITE_OK; + }else if( rc==SQLITE_OK && bOk==0 ){ + *pzErr = sqlite3_mprintf("malformed inverted index for FTS%d table %s.%s", + p->bFts4 ? 4 : 3, zSchema, zTabname); + if( *pzErr==0 ) rc = SQLITE_NOMEM; + } + sqlite3Fts3SegmentsClose(p); + return rc; +} + + + static const sqlite3_module fts3Module = { - /* iVersion */ 2, + /* iVersion */ 4, /* xCreate */ fts3CreateMethod, /* xConnect */ fts3ConnectMethod, /* xBestIndex */ fts3BestIndexMethod, @@ -3831,6 +4059,8 @@ static const sqlite3_module fts3Module = { /* xSavepoint */ fts3SavepointMethod, /* xRelease */ fts3ReleaseMethod, /* xRollbackTo */ fts3RollbackToMethod, + /* xShadowName */ fts3ShadowName, + /* xIntegrity */ fts3IntegrityMethod, }; /* @@ -3839,9 +4069,12 @@ static const sqlite3_module fts3Module = { ** allocated for the tokenizer hash table. */ static void hashDestroy(void *p){ - Fts3Hash *pHash = (Fts3Hash *)p; - sqlite3Fts3HashClear(pHash); - sqlite3_free(pHash); + Fts3HashWrapper *pHash = (Fts3HashWrapper *)p; + pHash->nRef--; + if( pHash->nRef<=0 ){ + sqlite3Fts3HashClear(&pHash->hash); + sqlite3_free(pHash); + } } /* @@ -3871,7 +4104,7 @@ void sqlite3Fts3IcuTokenizerModule(sqlite3_tokenizer_module const**ppModule); */ int sqlite3Fts3Init(sqlite3 *db){ int rc = SQLITE_OK; - Fts3Hash *pHash = 0; + Fts3HashWrapper *pHash = 0; const sqlite3_tokenizer_module *pSimple = 0; const sqlite3_tokenizer_module *pPorter = 0; #ifndef SQLITE_DISABLE_FTS3_UNICODE @@ -3899,23 +4132,24 @@ int sqlite3Fts3Init(sqlite3 *db){ sqlite3Fts3PorterTokenizerModule(&pPorter); /* Allocate and initialize the hash-table used to store tokenizers. */ - pHash = sqlite3_malloc(sizeof(Fts3Hash)); + pHash = sqlite3_malloc(sizeof(Fts3HashWrapper)); if( !pHash ){ rc = SQLITE_NOMEM; }else{ - sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1); + sqlite3Fts3HashInit(&pHash->hash, FTS3_HASH_STRING, 1); + pHash->nRef = 0; } /* Load the built-in tokenizers into the hash table */ if( rc==SQLITE_OK ){ - if( sqlite3Fts3HashInsert(pHash, "simple", 7, (void *)pSimple) - || sqlite3Fts3HashInsert(pHash, "porter", 7, (void *)pPorter) + if( sqlite3Fts3HashInsert(&pHash->hash, "simple", 7, (void *)pSimple) + || sqlite3Fts3HashInsert(&pHash->hash, "porter", 7, (void *)pPorter) #ifndef SQLITE_DISABLE_FTS3_UNICODE - || sqlite3Fts3HashInsert(pHash, "unicode61", 10, (void *)pUnicode) + || sqlite3Fts3HashInsert(&pHash->hash, "unicode61", 10, (void *)pUnicode) #endif #ifdef SQLITE_ENABLE_ICU - || (pIcu && sqlite3Fts3HashInsert(pHash, "icu", 4, (void *)pIcu)) + || (pIcu && sqlite3Fts3HashInsert(&pHash->hash, "icu", 4, (void *)pIcu)) #endif ){ rc = SQLITE_NOMEM; @@ -3924,32 +4158,35 @@ int sqlite3Fts3Init(sqlite3 *db){ #ifdef SQLITE_TEST if( rc==SQLITE_OK ){ - rc = sqlite3Fts3ExprInitTestInterface(db); + rc = sqlite3Fts3ExprInitTestInterface(db, &pHash->hash); } #endif /* Create the virtual table wrapper around the hash-table and overload - ** the two scalar functions. If this is successful, register the + ** the four scalar functions. If this is successful, register the ** module with sqlite. */ if( SQLITE_OK==rc - && SQLITE_OK==(rc = sqlite3Fts3InitHashTable(db, pHash, "fts3_tokenizer")) + && SQLITE_OK==(rc=sqlite3Fts3InitHashTable(db,&pHash->hash,"fts3_tokenizer")) && SQLITE_OK==(rc = sqlite3_overload_function(db, "snippet", -1)) && SQLITE_OK==(rc = sqlite3_overload_function(db, "offsets", 1)) && SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 1)) && SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 2)) && SQLITE_OK==(rc = sqlite3_overload_function(db, "optimize", 1)) ){ + pHash->nRef++; rc = sqlite3_create_module_v2( db, "fts3", &fts3Module, (void *)pHash, hashDestroy ); if( rc==SQLITE_OK ){ + pHash->nRef++; rc = sqlite3_create_module_v2( - db, "fts4", &fts3Module, (void *)pHash, 0 + db, "fts4", &fts3Module, (void *)pHash, hashDestroy ); } if( rc==SQLITE_OK ){ - rc = sqlite3Fts3InitTok(db, (void *)pHash); + pHash->nRef++; + rc = sqlite3Fts3InitTok(db, (void *)pHash, hashDestroy); } return rc; } @@ -3958,7 +4195,7 @@ int sqlite3Fts3Init(sqlite3 *db){ /* An error has occurred. Delete the hash table and return the error code. */ assert( rc!=SQLITE_OK ); if( pHash ){ - sqlite3Fts3HashClear(pHash); + sqlite3Fts3HashClear(&pHash->hash); sqlite3_free(pHash); } return rc; @@ -4111,6 +4348,7 @@ static int fts3EvalPhraseLoad( return rc; } +#ifndef SQLITE_DISABLE_FTS4_DEFERRED /* ** This function is called on each phrase after the position lists for ** any deferred tokens have been loaded into memory. It updates the phrases @@ -4126,8 +4364,7 @@ static int fts3EvalDeferredPhrase(Fts3Cursor *pCsr, Fts3Phrase *pPhrase){ char *aPoslist = 0; /* Position list for deferred tokens */ int nPoslist = 0; /* Number of bytes in aPoslist */ int iPrev = -1; /* Token number of previous deferred token */ - - assert( pPhrase->doclist.bFreeList==0 ); + char *aFree = (pPhrase->doclist.bFreeList ? pPhrase->doclist.pList : 0); for(iToken=0; iTokennToken; iToken++){ Fts3PhraseToken *pToken = &pPhrase->aToken[iToken]; @@ -4141,6 +4378,7 @@ static int fts3EvalDeferredPhrase(Fts3Cursor *pCsr, Fts3Phrase *pPhrase){ if( pList==0 ){ sqlite3_free(aPoslist); + sqlite3_free(aFree); pPhrase->doclist.pList = 0; pPhrase->doclist.nList = 0; return SQLITE_OK; @@ -4161,6 +4399,7 @@ static int fts3EvalDeferredPhrase(Fts3Cursor *pCsr, Fts3Phrase *pPhrase){ nPoslist = (int)(aOut - aPoslist); if( nPoslist==0 ){ sqlite3_free(aPoslist); + sqlite3_free(aFree); pPhrase->doclist.pList = 0; pPhrase->doclist.nList = 0; return SQLITE_OK; @@ -4193,13 +4432,14 @@ static int fts3EvalDeferredPhrase(Fts3Cursor *pCsr, Fts3Phrase *pPhrase){ nDistance = iPrev - nMaxUndeferred; } - aOut = (char *)sqlite3_malloc(nPoslist+8); + aOut = (char *)sqlite3Fts3MallocZero(((i64)nPoslist)+FTS3_BUFFER_PADDING); if( !aOut ){ sqlite3_free(aPoslist); return SQLITE_NOMEM; } pPhrase->doclist.pList = aOut; + assert( p1 && p2 ); if( fts3PoslistPhraseMerge(&aOut, nDistance, 0, 1, &p1, &p2) ){ pPhrase->doclist.bFreeList = 1; pPhrase->doclist.nList = (int)(aOut - pPhrase->doclist.pList); @@ -4212,8 +4452,10 @@ static int fts3EvalDeferredPhrase(Fts3Cursor *pCsr, Fts3Phrase *pPhrase){ } } + if( pPhrase->doclist.pList!=aFree ) sqlite3_free(aFree); return SQLITE_OK; } +#endif /* SQLITE_DISABLE_FTS4_DEFERRED */ /* ** Maximum number of tokens a phrase may have to be considered for the @@ -4247,7 +4489,7 @@ static int fts3EvalPhraseStart(Fts3Cursor *pCsr, int bOptOk, Fts3Phrase *p){ int bIncrOk = (bOptOk && pCsr->bDesc==pTab->bDescIdx && p->nToken<=MAX_INCR_PHRASE_TOKENS && p->nToken>0 -#ifdef SQLITE_TEST +#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST) && pTab->bNoIncrDoclist==0 #endif ); @@ -4303,7 +4545,7 @@ void sqlite3Fts3DoclistPrev( assert( nDoclist>0 ); assert( *pbEof==0 ); - assert( p || *piDocid==0 ); + assert_fts3_nc( p || *piDocid==0 ); assert( !p || (p>aDoclist && p<&aDoclist[nDoclist]) ); if( p==0 ){ @@ -4358,7 +4600,7 @@ void sqlite3Fts3DoclistNext( assert( nDoclist>0 ); assert( *pbEof==0 ); - assert( p || *piDocid==0 ); + assert_fts3_nc( p || *piDocid==0 ); assert( !p || (p>=aDoclist && p<=&aDoclist[nDoclist]) ); if( p==0 ){ @@ -4389,15 +4631,16 @@ static void fts3EvalDlPhraseNext( u8 *pbEof ){ char *pIter; /* Used to iterate through aAll */ - char *pEnd = &pDL->aAll[pDL->nAll]; /* 1 byte past end of aAll */ + char *pEnd; /* 1 byte past end of aAll */ if( pDL->pNextDocid ){ pIter = pDL->pNextDocid; + assert( pDL->aAll!=0 || pIter==0 ); }else{ pIter = pDL->aAll; } - if( pIter>=pEnd ){ + if( pIter==0 || pIter>=(pEnd = pDL->aAll + pDL->nAll) ){ /* We have already reached the end of this doclist. EOF. */ *pbEof = 1; }else{ @@ -4488,7 +4731,7 @@ static int incrPhraseTokenNext( ** ** * does not contain any deferred tokens. ** -** Advance it to the next matching documnent in the database and populate +** Advance it to the next matching document in the database and populate ** the Fts3Doclist.pList and nList fields. ** ** If there is no "next" entry and no error occurs, then *pbEof is set to @@ -4512,7 +4755,7 @@ static int fts3EvalIncrPhraseNext( ** one incremental token. In which case the bIncr flag is set. */ assert( p->bIncr==1 ); - if( p->nToken==1 && p->bIncr ){ + if( p->nToken==1 ){ rc = sqlite3Fts3MsrIncrNext(pTab, p->aToken[0].pSegcsr, &pDL->iDocid, &pDL->pList, &pDL->nList ); @@ -4558,9 +4801,10 @@ static int fts3EvalIncrPhraseNext( if( bEof==0 ){ int nList = 0; int nByte = a[p->nToken-1].nList; - char *aDoclist = sqlite3_malloc(nByte+1); + char *aDoclist = sqlite3_malloc64((i64)nByte+FTS3_BUFFER_PADDING); if( !aDoclist ) return SQLITE_NOMEM; memcpy(aDoclist, a[p->nToken-1].pList, nByte+1); + memset(&aDoclist[nByte], 0, FTS3_BUFFER_PADDING); for(i=0; i<(p->nToken-1); i++){ if( a[i].bIgnore==0 ){ @@ -4745,6 +4989,7 @@ static void fts3EvalTokenCosts( ** the number of overflow pages consumed by a record B bytes in size. */ static int fts3EvalAverageDocsize(Fts3Cursor *pCsr, int *pnPage){ + int rc = SQLITE_OK; if( pCsr->nRowAvg==0 ){ /* The average document size, which is required to calculate the cost ** of each doclist, has not yet been determined. Read the required @@ -4757,7 +5002,6 @@ static int fts3EvalAverageDocsize(Fts3Cursor *pCsr, int *pnPage){ ** data stored in all rows of each column of the table, from left ** to right. */ - int rc; Fts3Table *p = (Fts3Table*)pCsr->base.pVtab; sqlite3_stmt *pStmt; sqlite3_int64 nDoc = 0; @@ -4768,12 +5012,13 @@ static int fts3EvalAverageDocsize(Fts3Cursor *pCsr, int *pnPage){ rc = sqlite3Fts3SelectDoctotal(p, &pStmt); if( rc!=SQLITE_OK ) return rc; a = sqlite3_column_blob(pStmt, 0); - assert( a ); - - pEnd = &a[sqlite3_column_bytes(pStmt, 0)]; - a += sqlite3Fts3GetVarint(a, &nDoc); - while( anRowAvg = (int)(((nByte / nDoc) + p->nPgsz) / p->nPgsz); assert( pCsr->nRowAvg>0 ); rc = sqlite3_reset(pStmt); - if( rc!=SQLITE_OK ) return rc; } *pnPage = pCsr->nRowAvg; - return SQLITE_OK; + return rc; } /* @@ -4951,16 +5195,15 @@ static int fts3EvalStart(Fts3Cursor *pCsr){ #ifndef SQLITE_DISABLE_FTS4_DEFERRED if( rc==SQLITE_OK && nToken>1 && pTab->bFts4 ){ Fts3TokenAndCost *aTC; - Fts3Expr **apOr; - aTC = (Fts3TokenAndCost *)sqlite3_malloc( + aTC = (Fts3TokenAndCost *)sqlite3_malloc64( sizeof(Fts3TokenAndCost) * nToken + sizeof(Fts3Expr *) * nOr * 2 ); - apOr = (Fts3Expr **)&aTC[nToken]; if( !aTC ){ rc = SQLITE_NOMEM; }else{ + Fts3Expr **apOr = (Fts3Expr **)&aTC[nToken]; int ii; Fts3TokenAndCost *pTC = aTC; Fts3Expr **ppOr = apOr; @@ -5006,7 +5249,7 @@ static void fts3EvalInvalidatePoslist(Fts3Phrase *pPhrase){ ** ** Parameter nNear is passed the NEAR distance of the expression (5 in ** the example above). When this function is called, *paPoslist points to -** the position list, and *pnToken is the number of phrase tokens in, the +** the position list, and *pnToken is the number of phrase tokens in the ** phrase on the other side of the NEAR operator to pPhrase. For example, ** if pPhrase refers to the "def ghi" phrase, then *paPoslist points to ** the position list associated with phrase "abc". @@ -5041,10 +5284,12 @@ static int fts3EvalNearTrim( ); if( res ){ nNew = (int)(pOut - pPhrase->doclist.pList) - 1; - assert( pPhrase->doclist.pList[nNew]=='\0' ); - assert( nNew<=pPhrase->doclist.nList && nNew>0 ); - memset(&pPhrase->doclist.pList[nNew], 0, pPhrase->doclist.nList - nNew); - pPhrase->doclist.nList = nNew; + assert_fts3_nc( nNew<=pPhrase->doclist.nList && nNew>0 ); + if( nNew>=0 && nNew<=pPhrase->doclist.nList ){ + assert( pPhrase->doclist.pList[nNew]=='\0' ); + memset(&pPhrase->doclist.pList[nNew], 0, pPhrase->doclist.nList - nNew); + pPhrase->doclist.nList = nNew; + } *paPoslist = pPhrase->doclist.pList; *pnToken = pPhrase->nToken; } @@ -5098,9 +5343,8 @@ static void fts3EvalNextRow( Fts3Expr *pExpr, /* Expr. to advance to next matching row */ int *pRc /* IN/OUT: Error code */ ){ - if( *pRc==SQLITE_OK ){ + if( *pRc==SQLITE_OK && pExpr->bEof==0 ){ int bDescDoclist = pCsr->bDesc; /* Used by DOCID_CMP() macro */ - assert( pExpr->bEof==0 ); pExpr->bStart = 1; switch( pExpr->eType ){ @@ -5138,7 +5382,8 @@ static void fts3EvalNextRow( pExpr->iDocid = pLeft->iDocid; pExpr->bEof = (pLeft->bEof || pRight->bEof); if( pExpr->eType==FTSQUERY_NEAR && pExpr->bEof ){ - if( pRight->pPhrase && pRight->pPhrase->doclist.aAll ){ + assert( pRight->eType==FTSQUERY_PHRASE ); + if( pRight->pPhrase->doclist.aAll ){ Fts3Doclist *pDl = &pRight->pPhrase->doclist; while( *pRc==SQLITE_OK && pRight->bEof==0 ){ memset(pDl->pList, 0, pDl->nList); @@ -5152,6 +5397,7 @@ static void fts3EvalNextRow( fts3EvalNextRow(pCsr, pLeft, pRc); } } + pRight->bEof = pLeft->bEof = 1; } } break; @@ -5162,12 +5408,12 @@ static void fts3EvalNextRow( Fts3Expr *pRight = pExpr->pRight; sqlite3_int64 iCmp = DOCID_CMP(pLeft->iDocid, pRight->iDocid); - assert( pLeft->bStart || pLeft->iDocid==pRight->iDocid ); - assert( pRight->bStart || pLeft->iDocid==pRight->iDocid ); + assert_fts3_nc( pLeft->bStart || pLeft->iDocid==pRight->iDocid ); + assert_fts3_nc( pRight->bStart || pLeft->iDocid==pRight->iDocid ); if( pRight->bEof || (pLeft->bEof==0 && iCmp<0) ){ fts3EvalNextRow(pCsr, pLeft, pRc); - }else if( pLeft->bEof || (pRight->bEof==0 && iCmp>0) ){ + }else if( pLeft->bEof || iCmp>0 ){ fts3EvalNextRow(pCsr, pRight, pRc); }else{ fts3EvalNextRow(pCsr, pLeft, pRc); @@ -5259,51 +5505,47 @@ static int fts3EvalNearTest(Fts3Expr *pExpr, int *pRc){ */ if( *pRc==SQLITE_OK && pExpr->eType==FTSQUERY_NEAR - && pExpr->bEof==0 && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR) ){ Fts3Expr *p; - int nTmp = 0; /* Bytes of temp space */ + sqlite3_int64 nTmp = 0; /* Bytes of temp space */ char *aTmp; /* Temp space for PoslistNearMerge() */ /* Allocate temporary working space. */ for(p=pExpr; p->pLeft; p=p->pLeft){ + assert( p->pRight->pPhrase->doclist.nList>0 ); nTmp += p->pRight->pPhrase->doclist.nList; } nTmp += p->pPhrase->doclist.nList; - if( nTmp==0 ){ + aTmp = sqlite3_malloc64(nTmp*2 + FTS3_VARINT_MAX); + if( !aTmp ){ + *pRc = SQLITE_NOMEM; res = 0; }else{ - aTmp = sqlite3_malloc(nTmp*2); - if( !aTmp ){ - *pRc = SQLITE_NOMEM; - res = 0; - }else{ - char *aPoslist = p->pPhrase->doclist.pList; - int nToken = p->pPhrase->nToken; - - for(p=p->pParent;res && p && p->eType==FTSQUERY_NEAR; p=p->pParent){ - Fts3Phrase *pPhrase = p->pRight->pPhrase; - int nNear = p->nNear; - res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase); - } + char *aPoslist = p->pPhrase->doclist.pList; + int nToken = p->pPhrase->nToken; - aPoslist = pExpr->pRight->pPhrase->doclist.pList; - nToken = pExpr->pRight->pPhrase->nToken; - for(p=pExpr->pLeft; p && res; p=p->pLeft){ - int nNear; - Fts3Phrase *pPhrase; - assert( p->pParent && p->pParent->pLeft==p ); - nNear = p->pParent->nNear; - pPhrase = ( - p->eType==FTSQUERY_NEAR ? p->pRight->pPhrase : p->pPhrase - ); - res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase); - } + for(p=p->pParent;res && p && p->eType==FTSQUERY_NEAR; p=p->pParent){ + Fts3Phrase *pPhrase = p->pRight->pPhrase; + int nNear = p->nNear; + res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase); } - sqlite3_free(aTmp); + aPoslist = pExpr->pRight->pPhrase->doclist.pList; + nToken = pExpr->pRight->pPhrase->nToken; + for(p=pExpr->pLeft; p && res; p=p->pLeft){ + int nNear; + Fts3Phrase *pPhrase; + assert( p->pParent && p->pParent->pLeft==p ); + nNear = p->pParent->nNear; + pPhrase = ( + p->eType==FTSQUERY_NEAR ? p->pRight->pPhrase : p->pPhrase + ); + res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase); + } } + + sqlite3_free(aTmp); } return res; @@ -5384,11 +5626,10 @@ static int fts3EvalTestExpr( default: { #ifndef SQLITE_DISABLE_FTS4_DEFERRED - if( pCsr->pDeferred - && (pExpr->iDocid==pCsr->iPrevId || pExpr->bDeferred) - ){ + if( pCsr->pDeferred && (pExpr->bDeferred || ( + pExpr->iDocid==pCsr->iPrevId && pExpr->pPhrase->doclist.pList + ))){ Fts3Phrase *pPhrase = pExpr->pPhrase; - assert( pExpr->bDeferred || pPhrase->doclist.bFreeList==0 ); if( pExpr->bDeferred ){ fts3EvalInvalidatePoslist(pPhrase); } @@ -5398,7 +5639,10 @@ static int fts3EvalTestExpr( }else #endif { - bHit = (pExpr->bEof==0 && pExpr->iDocid==pCsr->iPrevId); + bHit = ( + pExpr->bEof==0 && pExpr->iDocid==pCsr->iPrevId + && pExpr->pPhrase->doclist.nList>0 + ); } break; } @@ -5494,7 +5738,7 @@ static int fts3EvalNext(Fts3Cursor *pCsr){ } /* -** Restart interation for expression pExpr so that the next call to +** Restart iteration for expression pExpr so that the next call to ** fts3EvalNext() visits the first row. Do not allow incremental ** loading or merging of phrase doclists for this iteration. ** @@ -5537,6 +5781,24 @@ static void fts3EvalRestart( } } +/* +** Expression node pExpr is an MSR phrase. This function restarts pExpr +** so that it is a regular phrase query, not an MSR. SQLITE_OK is returned +** if successful, or an SQLite error code otherwise. +*/ +int sqlite3Fts3MsrCancel(Fts3Cursor *pCsr, Fts3Expr *pExpr){ + int rc = SQLITE_OK; + if( pExpr->bEof==0 ){ + i64 iDocid = pExpr->iDocid; + fts3EvalRestart(pCsr, pExpr, &rc); + while( rc==SQLITE_OK && pExpr->iDocid!=iDocid ){ + fts3EvalNextRow(pCsr, pExpr, &rc); + if( pExpr->bEof ) rc = FTS_CORRUPT_VTAB; + } + } + return rc; +} + /* ** After allocating the Fts3Expr.aMI[] array for each phrase in the ** expression rooted at pExpr, the cursor iterates through all rows matched @@ -5545,15 +5807,14 @@ static void fts3EvalRestart( ** found in Fts3Expr.pPhrase->doclist.pList for each of the phrase ** expression nodes. */ -static void fts3EvalUpdateCounts(Fts3Expr *pExpr){ +static void fts3EvalUpdateCounts(Fts3Expr *pExpr, int nCol){ if( pExpr ){ Fts3Phrase *pPhrase = pExpr->pPhrase; if( pPhrase && pPhrase->doclist.pList ){ int iCol = 0; char *p = pPhrase->doclist.pList; - assert( *p ); - while( 1 ){ + do{ u8 c = 0; int iCnt = 0; while( 0xFE & (*p | c) ){ @@ -5569,12 +5830,28 @@ static void fts3EvalUpdateCounts(Fts3Expr *pExpr){ if( *p==0x00 ) break; p++; p += fts3GetVarint32(p, &iCol); - } + }while( iColpLeft); - fts3EvalUpdateCounts(pExpr->pRight); + fts3EvalUpdateCounts(pExpr->pLeft, nCol); + fts3EvalUpdateCounts(pExpr->pRight, nCol); + } +} + +/* +** This is an sqlite3Fts3ExprIterate() callback. If the Fts3Expr.aMI[] array +** has not yet been allocated, allocate and zero it. Otherwise, just zero +** it. +*/ +static int fts3AllocateMSI(Fts3Expr *pExpr, int iPhrase, void *pCtx){ + Fts3Table *pTab = (Fts3Table*)pCtx; + UNUSED_PARAMETER(iPhrase); + if( pExpr->aMI==0 ){ + pExpr->aMI = (u32 *)sqlite3_malloc64(pTab->nColumn * 3 * sizeof(u32)); + if( pExpr->aMI==0 ) return SQLITE_NOMEM; } + memset(pExpr->aMI, 0, pTab->nColumn * 3 * sizeof(u32)); + return SQLITE_OK; } /* @@ -5598,7 +5875,6 @@ static int fts3EvalGatherStats( if( pExpr->aMI==0 ){ Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab; Fts3Expr *pRoot; /* Root of NEAR expression */ - Fts3Expr *p; /* Iterator used for several purposes */ sqlite3_int64 iPrevId = pCsr->iPrevId; sqlite3_int64 iDocid; @@ -5606,7 +5882,9 @@ static int fts3EvalGatherStats( /* Find the root of the NEAR expression */ pRoot = pExpr; - while( pRoot->pParent && pRoot->pParent->eType==FTSQUERY_NEAR ){ + while( pRoot->pParent + && (pRoot->pParent->eType==FTSQUERY_NEAR || pRoot->bDeferred) + ){ pRoot = pRoot->pParent; } iDocid = pRoot->iDocid; @@ -5614,14 +5892,8 @@ static int fts3EvalGatherStats( assert( pRoot->bStart ); /* Allocate space for the aMSI[] array of each FTSQUERY_PHRASE node */ - for(p=pRoot; p; p=p->pLeft){ - Fts3Expr *pE = (p->eType==FTSQUERY_PHRASE?p:p->pRight); - assert( pE->aMI==0 ); - pE->aMI = (u32 *)sqlite3_malloc(pTab->nColumn * 3 * sizeof(u32)); - if( !pE->aMI ) return SQLITE_NOMEM; - memset(pE->aMI, 0, pTab->nColumn * 3 * sizeof(u32)); - } - + rc = sqlite3Fts3ExprIterate(pRoot, fts3AllocateMSI, (void*)pTab); + if( rc!=SQLITE_OK ) return rc; fts3EvalRestart(pCsr, pRoot, &rc); while( pCsr->isEof==0 && rc==SQLITE_OK ){ @@ -5643,7 +5915,7 @@ static int fts3EvalGatherStats( ); if( rc==SQLITE_OK && pCsr->isEof==0 ){ - fts3EvalUpdateCounts(pRoot); + fts3EvalUpdateCounts(pRoot, pTab->nColumn); } } @@ -5662,7 +5934,8 @@ static int fts3EvalGatherStats( fts3EvalRestart(pCsr, pRoot, &rc); do { fts3EvalNextRow(pCsr, pRoot, &rc); - assert( pRoot->bEof==0 ); + assert_fts3_nc( pRoot->bEof==0 ); + if( pRoot->bEof ) rc = FTS_CORRUPT_VTAB; }while( pRoot->iDocid!=iDocid && rc==SQLITE_OK ); } } @@ -5776,6 +6049,7 @@ int sqlite3Fts3EvalPhrasePoslist( u8 bTreeEof = 0; Fts3Expr *p; /* Used to iterate from pExpr to root */ Fts3Expr *pNear; /* Most senior NEAR ancestor (or pExpr) */ + Fts3Expr *pRun; /* Closest non-deferred ancestor of pNear */ int bMatch; /* Check if this phrase descends from an OR expression node. If not, @@ -5790,22 +6064,30 @@ int sqlite3Fts3EvalPhrasePoslist( if( p->bEof ) bTreeEof = 1; } if( bOr==0 ) return SQLITE_OK; + pRun = pNear; + while( pRun->bDeferred ){ + assert( pRun->pParent ); + pRun = pRun->pParent; + } /* This is the descendent of an OR node. In this case we cannot use ** an incremental phrase. Load the entire doclist for the phrase ** into memory in this case. */ if( pPhrase->bIncr ){ - int bEofSave = pNear->bEof; - fts3EvalRestart(pCsr, pNear, &rc); - while( rc==SQLITE_OK && !pNear->bEof ){ - fts3EvalNextRow(pCsr, pNear, &rc); - if( bEofSave==0 && pNear->iDocid==iDocid ) break; + int bEofSave = pRun->bEof; + fts3EvalRestart(pCsr, pRun, &rc); + while( rc==SQLITE_OK && !pRun->bEof ){ + fts3EvalNextRow(pCsr, pRun, &rc); + if( bEofSave==0 && pRun->iDocid==iDocid ) break; } assert( rc!=SQLITE_OK || pPhrase->bIncr==0 ); + if( rc==SQLITE_OK && pRun->bEof!=bEofSave ){ + rc = FTS_CORRUPT_VTAB; + } } if( bTreeEof ){ - while( rc==SQLITE_OK && !pNear->bEof ){ - fts3EvalNextRow(pCsr, pNear, &rc); + while( rc==SQLITE_OK && !pRun->bEof ){ + fts3EvalNextRow(pCsr, pRun, &rc); } } if( rc!=SQLITE_OK ) return rc; @@ -5904,7 +6186,7 @@ int sqlite3Fts3Corrupt(){ } #endif -#if !SQLITE_CORE +#if !defined(SQLITE_CORE) /* ** Initialize API pointer table, if required. */ diff --git a/ext/fts3/fts3Int.h b/ext/fts3/fts3Int.h index 0c86c4217e..e98b90a753 100644 --- a/ext/fts3/fts3Int.h +++ b/ext/fts3/fts3Int.h @@ -14,10 +14,20 @@ #ifndef _FTSINT_H #define _FTSINT_H -#if !defined(NDEBUG) && !defined(SQLITE_DEBUG) +/* +** Activate assert() only if SQLITE_TEST is enabled. +*/ +#if !defined(NDEBUG) && !defined(SQLITE_DEBUG) # define NDEBUG 1 #endif +#include +#include +#include +#include +#include +#include + /* FTS3/FTS4 require virtual tables */ #ifdef SQLITE_OMIT_VIRTUALTABLE # undef SQLITE_ENABLE_FTS3 @@ -37,7 +47,7 @@ /* If not building as part of the core, include sqlite3ext.h. */ #ifndef SQLITE_CORE -# include "sqlite3ext.h" +# include "sqlite3ext.h" SQLITE_EXTENSION_INIT3 #endif @@ -95,6 +105,8 @@ SQLITE_EXTENSION_INIT3 */ #define FTS3_VARINT_MAX 10 +#define FTS3_BUFFER_PADDING 8 + /* ** FTS4 virtual tables may maintain multiple indexes - one index of all terms ** in the document set and zero or more prefix indexes. All indexes are stored @@ -127,6 +139,18 @@ SQLITE_EXTENSION_INIT3 #define POS_COLUMN (1) /* Column-list terminator */ #define POS_END (0) /* Position-list terminator */ +/* +** The assert_fts3_nc() macro is similar to the assert() macro, except that it +** is used for assert() conditions that are true only if it can be +** guranteed that the database is not corrupt. +*/ +#ifdef SQLITE_DEBUG +extern int sqlite3_fts3_may_be_corrupt; +# define assert_fts3_nc(x) assert(sqlite3_fts3_may_be_corrupt || (x)) +#else +# define assert_fts3_nc(x) assert(x) +#endif + /* ** This section provides definitions to allow the ** FTS3 extension to be compiled outside of the @@ -137,17 +161,18 @@ SQLITE_EXTENSION_INIT3 ** Macros indicating that conditional expressions are always true or ** false. */ -#ifdef SQLITE_COVERAGE_TEST -# define ALWAYS(x) (1) -# define NEVER(X) (0) -#elif defined(SQLITE_DEBUG) -# define ALWAYS(x) sqlite3Fts3Always((x)!=0) -# define NEVER(x) sqlite3Fts3Never((x)!=0) -int sqlite3Fts3Always(int b); -int sqlite3Fts3Never(int b); +#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST) +# define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1 +#endif +#if defined(SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS) +# define ALWAYS(X) (1) +# define NEVER(X) (0) +#elif !defined(NDEBUG) +# define ALWAYS(X) ((X)?1:(assert(0),0)) +# define NEVER(X) ((X)?(assert(0),1):0) #else -# define ALWAYS(x) (x) -# define NEVER(x) (x) +# define ALWAYS(X) (X) +# define NEVER(X) (X) #endif /* @@ -164,13 +189,6 @@ typedef sqlite3_int64 i64; /* 8-byte signed integer */ */ #define UNUSED_PARAMETER(x) (void)(x) -/* -** Activate assert() only if SQLITE_TEST is enabled. -*/ -#if !defined(NDEBUG) && !defined(SQLITE_DEBUG) -# define NDEBUG 1 -#endif - /* ** The TESTONLY macro is used to enclose variable declarations or ** other bits of code that are needed to support the arguments @@ -182,6 +200,24 @@ typedef sqlite3_int64 i64; /* 8-byte signed integer */ # define TESTONLY(X) #endif +#define LARGEST_INT64 (0xffffffff|(((i64)0x7fffffff)<<32)) +#define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64) + +#define deliberate_fall_through + +/* +** Macros needed to provide flexible arrays in a portable way +*/ +#ifndef offsetof +# define offsetof(ST,M) ((size_t)((char*)&((ST*)0)->M - (char*)0)) +#endif +#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) +# define FLEXARRAY +#else +# define FLEXARRAY 1 +#endif + + #endif /* SQLITE_AMALGAMATION */ #ifdef SQLITE_DEBUG @@ -225,11 +261,13 @@ struct Fts3Table { char *zLanguageid; /* languageid=xxx option, or NULL */ int nAutoincrmerge; /* Value configured by 'automerge' */ u32 nLeafAdd; /* Number of leaf blocks added this trans */ + int bLock; /* Used to prevent recursive content= tbls */ /* Precompiled statements used by the implementation. Each of these ** statements is run and reset within a single virtual table API call. */ sqlite3_stmt *aStmt[40]; + sqlite3_stmt *pSeekStmt; /* Cache for fts3CursorSeekStmt() */ char *zReadExprlist; char *zWriteExprlist; @@ -243,6 +281,7 @@ struct Fts3Table { int nPgsz; /* Page size for host database */ char *zSegmentsTbl; /* Name of %_segments table */ sqlite3_blob *pSegments; /* Blob handle open on %_segments table */ + int iSavepoint; /* ** The following array of hash tables is used to buffer pending index @@ -282,13 +321,23 @@ struct Fts3Table { int mxSavepoint; /* Largest valid xSavepoint integer */ #endif -#ifdef SQLITE_TEST - /* True to disable the incremental doclist optimization. This is controled +#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST) + /* True to disable the incremental doclist optimization. This is controlled ** by special insert command 'test-no-incr-doclist'. */ int bNoIncrDoclist; + + /* Number of segments in a level */ + int nMergeCount; #endif }; +/* Macro to find the number of segments to merge */ +#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST) +# define MergeCount(P) ((P)->nMergeCount) +#else +# define MergeCount(P) FTS3_MERGE_COUNT +#endif + /* ** When the core wants to read from the virtual table, it creates a ** virtual table cursor (an instance of the following structure) using @@ -299,6 +348,7 @@ struct Fts3Cursor { i16 eSearch; /* Search strategy (see below) */ u8 isEof; /* True if at End Of Results */ u8 isRequireSeek; /* True if must seek pStmt to %_content row */ + u8 bSeekStmt; /* True if pStmt is a seek */ sqlite3_stmt *pStmt; /* Prepared statement in use by the cursor */ Fts3Expr *pExpr; /* Parsed MATCH query string */ int iLangid; /* Language being queried for */ @@ -324,7 +374,7 @@ struct Fts3Cursor { /* ** The Fts3Cursor.eSearch member is always set to one of the following. -** Actualy, Fts3Cursor.eSearch can be greater than or equal to +** Actually, Fts3Cursor.eSearch can be greater than or equal to ** FTS3_FULLTEXT_SEARCH. If so, then Fts3Cursor.eSearch - 2 is the index ** of the column to be searched. For example, in ** @@ -397,9 +447,13 @@ struct Fts3Phrase { */ int nToken; /* Number of tokens in the phrase */ int iColumn; /* Index of column this phrase must match */ - Fts3PhraseToken aToken[1]; /* One entry for each token in the phrase */ + Fts3PhraseToken aToken[FLEXARRAY]; /* One for each token in the phrase */ }; +/* Size (in bytes) of an Fts3Phrase object large enough to hold N tokens */ +#define SZ_FTS3PHRASE(N) \ + (offsetof(Fts3Phrase,aToken)+(N)*sizeof(Fts3PhraseToken)) + /* ** A tree of these objects forms the RHS of a MATCH operator. ** @@ -525,7 +579,7 @@ struct Fts3MultiSegReader { int nAdvance; /* How many seg-readers to advance */ Fts3SegFilter *pFilter; /* Pointer to filter object */ char *aBuffer; /* Buffer to merge doclists in */ - int nBuffer; /* Allocated size of aBuffer[] in bytes */ + i64 nBuffer; /* Allocated size of aBuffer[] in bytes */ int iColFilter; /* If >=0, filter for this column */ int bRestart; @@ -551,6 +605,8 @@ int sqlite3Fts3Incrmerge(Fts3Table*,int,int); void sqlite3Fts3ErrMsg(char**,const char*,...); int sqlite3Fts3PutVarint(char *, sqlite3_int64); int sqlite3Fts3GetVarint(const char *, sqlite_int64 *); +int sqlite3Fts3GetVarintU(const char *, sqlite_uint64 *); +int sqlite3Fts3GetVarintBounded(const char*,const char*,sqlite3_int64*); int sqlite3Fts3GetVarint32(const char *, int *); int sqlite3Fts3VarintLen(sqlite3_uint64); void sqlite3Fts3Dequote(char *); @@ -559,6 +615,7 @@ int sqlite3Fts3EvalPhraseStats(Fts3Cursor *, Fts3Expr *, u32 *); int sqlite3Fts3FirstFilter(sqlite3_int64, char *, int, char *); void sqlite3Fts3CreateStatTable(int*, Fts3Table*); int sqlite3Fts3EvalTestDeferred(Fts3Cursor *pCsr, int *pRc); +int sqlite3Fts3ReadInt(const char *z, int *pnOut); /* fts3_tokenizer.c */ const char *sqlite3Fts3NextToken(const char *, int *); @@ -582,9 +639,10 @@ int sqlite3Fts3ExprParse(sqlite3_tokenizer *, int, ); void sqlite3Fts3ExprFree(Fts3Expr *); #ifdef SQLITE_TEST -int sqlite3Fts3ExprInitTestInterface(sqlite3 *db); +int sqlite3Fts3ExprInitTestInterface(sqlite3 *db, Fts3Hash*); int sqlite3Fts3InitTerm(sqlite3 *db); #endif +void *sqlite3Fts3MallocZero(i64 nByte); int sqlite3Fts3OpenTokenizer(sqlite3_tokenizer *, int, const char *, int, sqlite3_tokenizer_cursor ** @@ -602,9 +660,10 @@ int sqlite3Fts3MsrIncrNext( int sqlite3Fts3EvalPhrasePoslist(Fts3Cursor *, Fts3Expr *, int iCol, char **); int sqlite3Fts3MsrOvfl(Fts3Cursor *, Fts3MultiSegReader *, int *); int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr); +int sqlite3Fts3MsrCancel(Fts3Cursor*, Fts3Expr*); /* fts3_tokenize_vtab.c */ -int sqlite3Fts3InitTok(sqlite3*, Fts3Hash *); +int sqlite3Fts3InitTok(sqlite3*, Fts3Hash *, void(*xDestroy)(void*)); /* fts3_unicode2.c (functions generated by parsing unicode text files) */ #ifndef SQLITE_DISABLE_FTS3_UNICODE @@ -613,5 +672,9 @@ int sqlite3FtsUnicodeIsalnum(int); int sqlite3FtsUnicodeIsdiacritic(int); #endif +int sqlite3Fts3ExprIterate(Fts3Expr*, int (*x)(Fts3Expr*,int,void*), void*); + +int sqlite3Fts3IntegrityCheck(Fts3Table *p, int *pbOk); + #endif /* !SQLITE_CORE || SQLITE_ENABLE_FTS3 */ #endif /* _FTSINT_H */ diff --git a/ext/fts3/fts3_aux.c b/ext/fts3/fts3_aux.c index f85a48ae02..439d579366 100644 --- a/ext/fts3/fts3_aux.c +++ b/ext/fts3/fts3_aux.c @@ -66,7 +66,7 @@ static int fts3auxConnectMethod( char const *zFts3; /* Name of fts3 table */ int nDb; /* Result of strlen(zDb) */ int nFts3; /* Result of strlen(zFts3) */ - int nByte; /* Bytes of space to allocate here */ + sqlite3_int64 nByte; /* Bytes of space to allocate here */ int rc; /* value returned by declare_vtab() */ Fts3auxTable *p; /* Virtual table object to return */ @@ -98,7 +98,7 @@ static int fts3auxConnectMethod( if( rc!=SQLITE_OK ) return rc; nByte = sizeof(Fts3auxTable) + sizeof(Fts3Table) + nDb + nFts3 + 2; - p = (Fts3auxTable *)sqlite3_malloc(nByte); + p = (Fts3auxTable *)sqlite3_malloc64(nByte); if( !p ) return SQLITE_NOMEM; memset(p, 0, nByte); @@ -248,7 +248,7 @@ static int fts3auxCloseMethod(sqlite3_vtab_cursor *pCursor){ static int fts3auxGrowStatArray(Fts3auxCursor *pCsr, int nSize){ if( nSize>pCsr->nStat ){ struct Fts3auxColstats *aNew; - aNew = (struct Fts3auxColstats *)sqlite3_realloc(pCsr->aStat, + aNew = (struct Fts3auxColstats *)sqlite3_realloc64(pCsr->aStat, sizeof(struct Fts3auxColstats) * nSize ); if( aNew==0 ) return SQLITE_NOMEM; @@ -297,6 +297,7 @@ static int fts3auxNextMethod(sqlite3_vtab_cursor *pCursor){ if( fts3auxGrowStatArray(pCsr, 2) ) return SQLITE_NOMEM; memset(pCsr->aStat, 0, sizeof(struct Fts3auxColstats) * pCsr->nStat); iCol = 0; + rc = SQLITE_OK; while( iaStat[iCol+1].nDoc++; eState = 2; @@ -348,7 +353,6 @@ static int fts3auxNextMethod(sqlite3_vtab_cursor *pCursor){ } pCsr->iCol = 0; - rc = SQLITE_OK; }else{ pCsr->isEof = 1; } @@ -406,6 +410,7 @@ static int fts3auxFilterMethod( sqlite3Fts3SegReaderFinish(&pCsr->csr); sqlite3_free((void *)pCsr->filter.zTerm); sqlite3_free(pCsr->aStat); + sqlite3_free(pCsr->zStop); memset(&pCsr->csr, 0, ((u8*)&pCsr[1]) - (u8*)&pCsr->csr); pCsr->filter.flags = FTS3_SEGMENT_REQUIRE_POS|FTS3_SEGMENT_IGNORE_EMPTY; @@ -416,15 +421,15 @@ static int fts3auxFilterMethod( assert( (iEq==0 && iGe==-1) || (iEq==-1 && iGe==0) ); if( zStr ){ pCsr->filter.zTerm = sqlite3_mprintf("%s", zStr); - pCsr->filter.nTerm = sqlite3_value_bytes(apVal[0]); if( pCsr->filter.zTerm==0 ) return SQLITE_NOMEM; + pCsr->filter.nTerm = (int)strlen(pCsr->filter.zTerm); } } if( iLe>=0 ){ pCsr->zStop = sqlite3_mprintf("%s", sqlite3_value_text(apVal[iLe])); - pCsr->nStop = sqlite3_value_bytes(apVal[iLe]); if( pCsr->zStop==0 ) return SQLITE_NOMEM; + pCsr->nStop = (int)strlen(pCsr->zStop); } if( iLangid>=0 ){ @@ -539,7 +544,9 @@ int sqlite3Fts3InitAux(sqlite3 *db){ 0, /* xRename */ 0, /* xSavepoint */ 0, /* xRelease */ - 0 /* xRollbackTo */ + 0, /* xRollbackTo */ + 0, /* xShadowName */ + 0 /* xIntegrity */ }; int rc; /* Return code */ diff --git a/ext/fts3/fts3_expr.c b/ext/fts3/fts3_expr.c index 788e5021ec..681d4e8625 100644 --- a/ext/fts3/fts3_expr.c +++ b/ext/fts3/fts3_expr.c @@ -122,8 +122,8 @@ static int fts3isspace(char c){ ** zero the memory before returning a pointer to it. If unsuccessful, ** return NULL. */ -static void *fts3MallocZero(int nByte){ - void *pRet = sqlite3_malloc(nByte); +void *sqlite3Fts3MallocZero(sqlite3_int64 nByte){ + void *pRet = sqlite3_malloc64(nByte); if( pRet ) memset(pRet, 0, nByte); return pRet; } @@ -161,6 +161,23 @@ int sqlite3Fts3OpenTokenizer( */ static int fts3ExprParse(ParseContext *, const char *, int, Fts3Expr **, int *); +/* +** Search buffer z[], size n, for a '"' character. Or, if enable_parenthesis +** is defined, search for '(' and ')' as well. Return the index of the first +** such character in the buffer. If there is no such character, return -1. +*/ +static int findBarredChar(const char *z, int n){ + int ii; + for(ii=0; iiiLangid, z, i, &pCursor); + *pnConsumed = n; + rc = sqlite3Fts3OpenTokenizer(pTokenizer, pParse->iLangid, z, n, &pCursor); if( rc==SQLITE_OK ){ const char *zToken; int nToken = 0, iStart = 0, iEnd = 0, iPosition = 0; - int nByte; /* total space to allocate */ + sqlite3_int64 nByte; /* total space to allocate */ rc = pModule->xNext(pCursor, &zToken, &nToken, &iStart, &iEnd, &iPosition); if( rc==SQLITE_OK ){ - nByte = sizeof(Fts3Expr) + sizeof(Fts3Phrase) + nToken; - pRet = (Fts3Expr *)fts3MallocZero(nByte); + /* Check that this tokenization did not gobble up any " characters. Or, + ** if enable_parenthesis is true, that it did not gobble up any + ** open or close parenthesis characters either. If it did, call + ** getNextToken() again, but pass only that part of the input buffer + ** up to the first such character. */ + int iBarred = findBarredChar(z, iEnd); + if( iBarred>=0 ){ + pModule->xClose(pCursor); + return getNextToken(pParse, iCol, z, iBarred, ppExpr, pnConsumed); + } + + nByte = sizeof(Fts3Expr) + SZ_FTS3PHRASE(1) + nToken; + pRet = (Fts3Expr *)sqlite3Fts3MallocZero(nByte); if( !pRet ){ rc = SQLITE_NOMEM; }else{ @@ -212,7 +233,7 @@ static int getNextToken( pRet->pPhrase->nToken = 1; pRet->pPhrase->iColumn = iCol; pRet->pPhrase->aToken[0].n = nToken; - pRet->pPhrase->aToken[0].z = (char *)&pRet->pPhrase[1]; + pRet->pPhrase->aToken[0].z = (char*)&pRet->pPhrase->aToken[1]; memcpy(pRet->pPhrase->aToken[0].z, zToken, nToken); if( iEnd=0 ){ + *pnConsumed = iBarred; + } rc = SQLITE_OK; } @@ -252,8 +277,8 @@ static int getNextToken( ** Enlarge a memory allocation. If an out-of-memory allocation occurs, ** then free the old allocation. */ -static void *fts3ReallocOrFree(void *pOrig, int nNew){ - void *pRet = sqlite3_realloc(pOrig, nNew); +static void *fts3ReallocOrFree(void *pOrig, sqlite3_int64 nNew){ + void *pRet = sqlite3_realloc64(pOrig, nNew); if( !pRet ){ sqlite3_free(pOrig); } @@ -283,9 +308,9 @@ static int getNextString( Fts3Expr *p = 0; sqlite3_tokenizer_cursor *pCursor = 0; char *zTemp = 0; - int nTemp = 0; + i64 nTemp = 0; - const int nSpace = sizeof(Fts3Expr) + sizeof(Fts3Phrase); + const int nSpace = sizeof(Fts3Expr) + SZ_FTS3PHRASE(1); int nToken = 0; /* The final Fts3Expr data structure, including the Fts3Phrase, @@ -319,10 +344,11 @@ static int getNextString( Fts3PhraseToken *pToken; p = fts3ReallocOrFree(p, nSpace + ii*sizeof(Fts3PhraseToken)); - if( !p ) goto no_mem; - zTemp = fts3ReallocOrFree(zTemp, nTemp + nByte); - if( !zTemp ) goto no_mem; + if( !zTemp || !p ){ + rc = SQLITE_NOMEM; + goto getnextstring_out; + } assert( nToken==ii ); pToken = &((Fts3Phrase *)(&p[1]))->aToken[ii]; @@ -337,9 +363,6 @@ static int getNextString( nToken = ii+1; } } - - pModule->xClose(pCursor); - pCursor = 0; } if( rc==SQLITE_DONE ){ @@ -347,7 +370,10 @@ static int getNextString( char *zBuf = 0; p = fts3ReallocOrFree(p, nSpace + nToken*sizeof(Fts3PhraseToken) + nTemp); - if( !p ) goto no_mem; + if( !p ){ + rc = SQLITE_NOMEM; + goto getnextstring_out; + } memset(p, 0, (char *)&(((Fts3Phrase *)&p[1])->aToken[0])-(char *)p); p->eType = FTSQUERY_PHRASE; p->pPhrase = (Fts3Phrase *)&p[1]; @@ -355,11 +381,9 @@ static int getNextString( p->pPhrase->nToken = nToken; zBuf = (char *)&p->pPhrase->aToken[nToken]; + assert( nTemp==0 || zTemp ); if( zTemp ){ memcpy(zBuf, zTemp, nTemp); - sqlite3_free(zTemp); - }else{ - assert( nTemp==0 ); } for(jj=0; jjpPhrase->nToken; jj++){ @@ -369,17 +393,17 @@ static int getNextString( rc = SQLITE_OK; } - *ppExpr = p; - return rc; -no_mem: - + getnextstring_out: if( pCursor ){ pModule->xClose(pCursor); } sqlite3_free(zTemp); - sqlite3_free(p); - *ppExpr = 0; - return SQLITE_NOMEM; + if( rc!=SQLITE_OK ){ + sqlite3_free(p); + p = 0; + } + *ppExpr = p; + return rc; } /* @@ -446,10 +470,7 @@ static int getNextNode( if( pKey->eType==FTSQUERY_NEAR ){ assert( nKey==4 ); if( zInput[4]=='/' && zInput[5]>='0' && zInput[5]<='9' ){ - nNear = 0; - for(nKey=5; zInput[nKey]>='0' && zInput[nKey]<='9'; nKey++){ - nNear = nNear * 10 + (zInput[nKey] - '0'); - } + nKey += 1+sqlite3Fts3ReadInt(&zInput[nKey+1], &nNear); } } @@ -461,7 +482,7 @@ static int getNextNode( if( fts3isspace(cNext) || cNext=='"' || cNext=='(' || cNext==')' || cNext==0 ){ - pRet = (Fts3Expr *)fts3MallocZero(sizeof(Fts3Expr)); + pRet = (Fts3Expr *)sqlite3Fts3MallocZero(sizeof(Fts3Expr)); if( !pRet ){ return SQLITE_NOMEM; } @@ -496,8 +517,12 @@ static int getNextNode( if( *zInput=='(' ){ int nConsumed = 0; pParse->nNest++; +#if !defined(SQLITE_MAX_EXPR_DEPTH) + if( pParse->nNest>1000 ) return SQLITE_ERROR; +#elif SQLITE_MAX_EXPR_DEPTH>0 + if( pParse->nNest>SQLITE_MAX_EXPR_DEPTH ) return SQLITE_ERROR; +#endif rc = fts3ExprParse(pParse, zInput+1, nInput-1, ppExpr, &nConsumed); - if( rc==SQLITE_OK && !*ppExpr ){ rc = SQLITE_DONE; } *pnConsumed = (int)(zInput - z) + 1 + nConsumed; return rc; }else if( *zInput==')' ){ @@ -636,7 +661,7 @@ static int fts3ExprParse( && p->eType==FTSQUERY_PHRASE && pParse->isNot ){ /* Create an implicit NOT operator. */ - Fts3Expr *pNot = fts3MallocZero(sizeof(Fts3Expr)); + Fts3Expr *pNot = sqlite3Fts3MallocZero(sizeof(Fts3Expr)); if( !pNot ){ sqlite3Fts3ExprFree(p); rc = SQLITE_NOMEM; @@ -657,7 +682,7 @@ static int fts3ExprParse( /* The isRequirePhrase variable is set to true if a phrase or ** an expression contained in parenthesis is required. If a - ** binary operator (AND, OR, NOT or NEAR) is encounted when + ** binary operator (AND, OR, NOT or NEAR) is encountered when ** isRequirePhrase is set, this is a syntax error. */ if( !isPhrase && isRequirePhrase ){ @@ -670,7 +695,7 @@ static int fts3ExprParse( /* Insert an implicit AND operator. */ Fts3Expr *pAnd; assert( pRet && pPrev ); - pAnd = fts3MallocZero(sizeof(Fts3Expr)); + pAnd = sqlite3Fts3MallocZero(sizeof(Fts3Expr)); if( !pAnd ){ sqlite3Fts3ExprFree(p); rc = SQLITE_NOMEM; @@ -796,7 +821,7 @@ static int fts3ExprBalance(Fts3Expr **pp, int nMaxDepth){ if( rc==SQLITE_OK ){ if( (eType==FTSQUERY_AND || eType==FTSQUERY_OR) ){ Fts3Expr **apLeaf; - apLeaf = (Fts3Expr **)sqlite3_malloc(sizeof(Fts3Expr *) * nMaxDepth); + apLeaf = (Fts3Expr **)sqlite3_malloc64(sizeof(Fts3Expr *) * nMaxDepth); if( 0==apLeaf ){ rc = SQLITE_NOMEM; }else{ @@ -1108,34 +1133,6 @@ void sqlite3Fts3ExprFree(Fts3Expr *pDel){ #include -/* -** Function to query the hash-table of tokenizers (see README.tokenizers). -*/ -static int queryTestTokenizer( - sqlite3 *db, - const char *zName, - const sqlite3_tokenizer_module **pp -){ - int rc; - sqlite3_stmt *pStmt; - const char zSql[] = "SELECT fts3_tokenizer(?)"; - - *pp = 0; - rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); - if( rc!=SQLITE_OK ){ - return rc; - } - - sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC); - if( SQLITE_ROW==sqlite3_step(pStmt) ){ - if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB ){ - memcpy((void *)pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp)); - } - } - - return sqlite3_finalize(pStmt); -} - /* ** Return a pointer to a buffer containing a text representation of the ** expression passed as the first argument. The buffer is obtained from @@ -1203,12 +1200,12 @@ static char *exprToString(Fts3Expr *pExpr, char *zBuf){ ** ** SELECT fts3_exprtest('simple', 'Bill col2:Bloggs', 'col1', 'col2'); */ -static void fts3ExprTest( +static void fts3ExprTestCommon( + int bRebalance, sqlite3_context *context, int argc, sqlite3_value **argv ){ - sqlite3_tokenizer_module const *pModule = 0; sqlite3_tokenizer *pTokenizer = 0; int rc; char **azCol = 0; @@ -1218,7 +1215,9 @@ static void fts3ExprTest( int ii; Fts3Expr *pExpr; char *zBuf = 0; - sqlite3 *db = sqlite3_context_db_handle(context); + Fts3Hash *pHash = (Fts3Hash*)sqlite3_user_data(context); + const char *zTokenizer = 0; + char *zErr = 0; if( argc<3 ){ sqlite3_result_error(context, @@ -1227,28 +1226,22 @@ static void fts3ExprTest( return; } - rc = queryTestTokenizer(db, - (const char *)sqlite3_value_text(argv[0]), &pModule); - if( rc==SQLITE_NOMEM ){ - sqlite3_result_error_nomem(context); - goto exprtest_out; - }else if( !pModule ){ - sqlite3_result_error(context, "No such tokenizer module", -1); - goto exprtest_out; - } - - rc = pModule->xCreate(0, 0, &pTokenizer); - assert( rc==SQLITE_NOMEM || rc==SQLITE_OK ); - if( rc==SQLITE_NOMEM ){ - sqlite3_result_error_nomem(context); - goto exprtest_out; + zTokenizer = (const char*)sqlite3_value_text(argv[0]); + rc = sqlite3Fts3InitTokenizer(pHash, zTokenizer, &pTokenizer, &zErr); + if( rc!=SQLITE_OK ){ + if( rc==SQLITE_NOMEM ){ + sqlite3_result_error_nomem(context); + }else{ + sqlite3_result_error(context, zErr, -1); + } + sqlite3_free(zErr); + return; } - pTokenizer->pModule = pModule; zExpr = (const char *)sqlite3_value_text(argv[1]); nExpr = sqlite3_value_bytes(argv[1]); nCol = argc-2; - azCol = (char **)sqlite3_malloc(nCol*sizeof(char *)); + azCol = (char **)sqlite3_malloc64(nCol*sizeof(char *)); if( !azCol ){ sqlite3_result_error_nomem(context); goto exprtest_out; @@ -1257,7 +1250,7 @@ static void fts3ExprTest( azCol[ii] = (char *)sqlite3_value_text(argv[ii+2]); } - if( sqlite3_user_data(context) ){ + if( bRebalance ){ char *zDummy = 0; rc = sqlite3Fts3ExprParse( pTokenizer, 0, azCol, 0, nCol, nCol, zExpr, nExpr, &pExpr, &zDummy @@ -1271,7 +1264,6 @@ static void fts3ExprTest( } if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM ){ - sqlite3Fts3ExprFree(pExpr); sqlite3_result_error(context, "Error parsing expression", -1); }else if( rc==SQLITE_NOMEM || !(zBuf = exprToString(pExpr, 0)) ){ sqlite3_result_error_nomem(context); @@ -1283,23 +1275,38 @@ static void fts3ExprTest( sqlite3Fts3ExprFree(pExpr); exprtest_out: - if( pModule && pTokenizer ){ - rc = pModule->xDestroy(pTokenizer); + if( pTokenizer ){ + rc = pTokenizer->pModule->xDestroy(pTokenizer); } sqlite3_free(azCol); } +static void fts3ExprTest( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + fts3ExprTestCommon(0, context, argc, argv); +} +static void fts3ExprTestRebalance( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + fts3ExprTestCommon(1, context, argc, argv); +} + /* ** Register the query expression parser test function fts3_exprtest() ** with database connection db. */ -int sqlite3Fts3ExprInitTestInterface(sqlite3* db){ +int sqlite3Fts3ExprInitTestInterface(sqlite3 *db, Fts3Hash *pHash){ int rc = sqlite3_create_function( - db, "fts3_exprtest", -1, SQLITE_UTF8, 0, fts3ExprTest, 0, 0 + db, "fts3_exprtest", -1, SQLITE_UTF8, (void*)pHash, fts3ExprTest, 0, 0 ); if( rc==SQLITE_OK ){ rc = sqlite3_create_function(db, "fts3_exprtest_rebalance", - -1, SQLITE_UTF8, (void *)1, fts3ExprTest, 0, 0 + -1, SQLITE_UTF8, (void*)pHash, fts3ExprTestRebalance, 0, 0 ); } return rc; diff --git a/ext/fts3/fts3_hash.c b/ext/fts3/fts3_hash.c index 1a32a537b4..1918be4cb7 100644 --- a/ext/fts3/fts3_hash.c +++ b/ext/fts3/fts3_hash.c @@ -35,8 +35,8 @@ /* ** Malloc and Free functions */ -static void *fts3HashMalloc(int n){ - void *p = sqlite3_malloc(n); +static void *fts3HashMalloc(sqlite3_int64 n){ + void *p = sqlite3_malloc64(n); if( p ){ memset(p, 0, n); } @@ -187,7 +187,7 @@ static void fts3HashInsertElement( } -/* Resize the hash table so that it cantains "new_size" buckets. +/* Resize the hash table so that it contains "new_size" buckets. ** "new_size" must be a power of 2. The hash table might fail ** to resize if sqliteMalloc() fails. ** diff --git a/ext/fts3/fts3_icu.c b/ext/fts3/fts3_icu.c index 6f90e1ebad..0848a5aaba 100644 --- a/ext/fts3/fts3_icu.c +++ b/ext/fts3/fts3_icu.c @@ -60,7 +60,7 @@ static int icuCreate( if( argc>0 ){ n = strlen(argv[0])+1; } - p = (IcuTokenizer *)sqlite3_malloc(sizeof(IcuTokenizer)+n); + p = (IcuTokenizer *)sqlite3_malloc64(sizeof(IcuTokenizer)+n); if( !p ){ return SQLITE_NOMEM; } @@ -117,7 +117,7 @@ static int icuOpen( nInput = strlen(zInput); } nChar = nInput+1; - pCsr = (IcuCursor *)sqlite3_malloc( + pCsr = (IcuCursor *)sqlite3_malloc64( sizeof(IcuCursor) + /* IcuCursor */ ((nChar+3)&~3) * sizeof(UChar) + /* IcuCursor.aChar[] */ (nChar+1) * sizeof(int) /* IcuCursor.aOffset[] */ diff --git a/ext/fts3/fts3_porter.c b/ext/fts3/fts3_porter.c index 8fb4c25daa..35e81b74af 100644 --- a/ext/fts3/fts3_porter.c +++ b/ext/fts3/fts3_porter.c @@ -256,7 +256,7 @@ static int star_oh(const char *z){ /* ** If the word ends with zFrom and xCond() is true for the stem -** of the word that preceeds the zFrom ending, then change the +** of the word that precedes the zFrom ending, then change the ** ending to zTo. ** ** The input word *pz and zFrom are both in reverse order. zTo @@ -621,7 +621,7 @@ static int porterNext( if( n>c->nAllocated ){ char *pNew; c->nAllocated = n+20; - pNew = sqlite3_realloc(c->zToken, c->nAllocated); + pNew = sqlite3_realloc64(c->zToken, c->nAllocated); if( !pNew ) return SQLITE_NOMEM; c->zToken = pNew; } diff --git a/ext/fts3/fts3_snippet.c b/ext/fts3/fts3_snippet.c index a0771c0b30..62e27d30bf 100644 --- a/ext/fts3/fts3_snippet.c +++ b/ext/fts3/fts3_snippet.c @@ -37,7 +37,7 @@ /* -** Used as an fts3ExprIterate() context when loading phrase doclists to +** Used as an sqlite3Fts3ExprIterate() context when loading phrase doclists to ** Fts3Expr.aDoclist[]/nDoclist. */ typedef struct LoadDoclistCtx LoadDoclistCtx; @@ -67,9 +67,9 @@ struct SnippetIter { struct SnippetPhrase { int nToken; /* Number of tokens in phrase */ char *pList; /* Pointer to start of phrase position list */ - int iHead; /* Next value in position list */ + i64 iHead; /* Next value in position list */ char *pHead; /* Position list data following iHead */ - int iTail; /* Next value in trailing position list */ + i64 iTail; /* Next value in trailing position list */ char *pTail; /* Position list data following iTail */ }; @@ -81,7 +81,7 @@ struct SnippetFragment { }; /* -** This type is used as an fts3ExprIterate() context object while +** This type is used as an sqlite3Fts3ExprIterate() context object while ** accumulating the data returned by the matchinfo() function. */ typedef struct MatchInfo MatchInfo; @@ -104,9 +104,13 @@ struct MatchinfoBuffer { int nElem; int bGlobal; /* Set if global data is loaded */ char *zMatchinfo; - u32 aMatchinfo[1]; + u32 aMI[FLEXARRAY]; }; +/* Size (in bytes) of a MatchinfoBuffer sufficient for N elements */ +#define SZ_MATCHINFOBUFFER(N) \ + (offsetof(MatchinfoBuffer,aMI)+(((N)+1)/2)*sizeof(u64)) + /* ** The snippet() and offsets() functions both return text values. An instance @@ -128,17 +132,18 @@ struct StrBuffer { /* ** Allocate a two-slot MatchinfoBuffer object. */ -static MatchinfoBuffer *fts3MIBufferNew(int nElem, const char *zMatchinfo){ +static MatchinfoBuffer *fts3MIBufferNew(size_t nElem, const char *zMatchinfo){ MatchinfoBuffer *pRet; - int nByte = sizeof(u32) * (2*nElem + 1) + sizeof(MatchinfoBuffer); - int nStr = (int)strlen(zMatchinfo); + sqlite3_int64 nByte = sizeof(u32) * (2*(sqlite3_int64)nElem + 1) + + SZ_MATCHINFOBUFFER(1); + sqlite3_int64 nStr = strlen(zMatchinfo); - pRet = sqlite3_malloc(nByte + nStr+1); + pRet = sqlite3Fts3MallocZero(nByte + nStr+1); if( pRet ){ - memset(pRet, 0, nByte); - pRet->aMatchinfo[0] = (u8*)(&pRet->aMatchinfo[1]) - (u8*)pRet; - pRet->aMatchinfo[1+nElem] = pRet->aMatchinfo[0] + sizeof(u32)*(nElem+1); - pRet->nElem = nElem; + pRet->aMI[0] = (u8*)(&pRet->aMI[1]) - (u8*)pRet; + pRet->aMI[1+nElem] = pRet->aMI[0] + + sizeof(u32)*((int)nElem+1); + pRet->nElem = (int)nElem; pRet->zMatchinfo = ((char*)pRet) + nByte; memcpy(pRet->zMatchinfo, zMatchinfo, nStr+1); pRet->aRef[0] = 1; @@ -150,10 +155,10 @@ static MatchinfoBuffer *fts3MIBufferNew(int nElem, const char *zMatchinfo){ static void fts3MIBufferFree(void *p){ MatchinfoBuffer *pBuf = (MatchinfoBuffer*)((u8*)p - ((u32*)p)[-1]); - assert( (u32*)p==&pBuf->aMatchinfo[1] - || (u32*)p==&pBuf->aMatchinfo[pBuf->nElem+2] + assert( (u32*)p==&pBuf->aMI[1] + || (u32*)p==&pBuf->aMI[pBuf->nElem+2] ); - if( (u32*)p==&pBuf->aMatchinfo[1] ){ + if( (u32*)p==&pBuf->aMI[1] ){ pBuf->aRef[1] = 0; }else{ pBuf->aRef[2] = 0; @@ -170,18 +175,18 @@ static void (*fts3MIBufferAlloc(MatchinfoBuffer *p, u32 **paOut))(void*){ if( p->aRef[1]==0 ){ p->aRef[1] = 1; - aOut = &p->aMatchinfo[1]; + aOut = &p->aMI[1]; xRet = fts3MIBufferFree; } else if( p->aRef[2]==0 ){ p->aRef[2] = 1; - aOut = &p->aMatchinfo[p->nElem+2]; + aOut = &p->aMI[p->nElem+2]; xRet = fts3MIBufferFree; }else{ - aOut = (u32*)sqlite3_malloc(p->nElem * sizeof(u32)); + aOut = (u32*)sqlite3_malloc64(p->nElem * sizeof(u32)); if( aOut ){ xRet = sqlite3_free; - if( p->bGlobal ) memcpy(aOut, &p->aMatchinfo[1], p->nElem*sizeof(u32)); + if( p->bGlobal ) memcpy(aOut, &p->aMI[1], p->nElem*sizeof(u32)); } } @@ -191,7 +196,7 @@ static void (*fts3MIBufferAlloc(MatchinfoBuffer *p, u32 **paOut))(void*){ static void fts3MIBufferSetGlobal(MatchinfoBuffer *p){ p->bGlobal = 1; - memcpy(&p->aMatchinfo[2+p->nElem], &p->aMatchinfo[1], p->nElem*sizeof(u32)); + memcpy(&p->aMI[2+p->nElem], &p->aMI[1], p->nElem*sizeof(u32)); } /* @@ -232,14 +237,14 @@ void sqlite3Fts3MIBufferFree(MatchinfoBuffer *p){ ** After it returns, *piPos contains the value of the next element of the ** list and *pp is advanced to the following varint. */ -static void fts3GetDeltaPosition(char **pp, int *piPos){ +static void fts3GetDeltaPosition(char **pp, i64 *piPos){ int iVal; *pp += fts3GetVarint32(*pp, &iVal); *piPos += (iVal-2); } /* -** Helper function for fts3ExprIterate() (see below). +** Helper function for sqlite3Fts3ExprIterate() (see below). */ static int fts3ExprIterate2( Fts3Expr *pExpr, /* Expression to iterate phrases of */ @@ -273,7 +278,7 @@ static int fts3ExprIterate2( ** Otherwise, SQLITE_OK is returned after a callback has been made for ** all eligible phrase nodes. */ -static int fts3ExprIterate( +int sqlite3Fts3ExprIterate( Fts3Expr *pExpr, /* Expression to iterate phrases of */ int (*x)(Fts3Expr*,int,void*), /* Callback function to invoke for phrases */ void *pCtx /* Second argument to pass to callback */ @@ -282,10 +287,9 @@ static int fts3ExprIterate( return fts3ExprIterate2(pExpr, &iPhrase, x, pCtx); } - /* -** This is an fts3ExprIterate() callback used while loading the doclists -** for each phrase into Fts3Expr.aDoclist[]/nDoclist. See also +** This is an sqlite3Fts3ExprIterate() callback used while loading the +** doclists for each phrase into Fts3Expr.aDoclist[]/nDoclist. See also ** fts3ExprLoadDoclists(). */ static int fts3ExprLoadDoclistsCb(Fts3Expr *pExpr, int iPhrase, void *ctx){ @@ -317,9 +321,9 @@ static int fts3ExprLoadDoclists( int *pnToken /* OUT: Number of tokens in query */ ){ int rc; /* Return Code */ - LoadDoclistCtx sCtx = {0,0,0}; /* Context for fts3ExprIterate() */ + LoadDoclistCtx sCtx = {0,0,0}; /* Context for sqlite3Fts3ExprIterate() */ sCtx.pCsr = pCsr; - rc = fts3ExprIterate(pCsr->pExpr, fts3ExprLoadDoclistsCb, (void *)&sCtx); + rc = sqlite3Fts3ExprIterate(pCsr->pExpr,fts3ExprLoadDoclistsCb,(void*)&sCtx); if( pnPhrase ) *pnPhrase = sCtx.nPhrase; if( pnToken ) *pnToken = sCtx.nToken; return rc; @@ -332,7 +336,7 @@ static int fts3ExprPhraseCountCb(Fts3Expr *pExpr, int iPhrase, void *ctx){ } static int fts3ExprPhraseCount(Fts3Expr *pExpr){ int nPhrase = 0; - (void)fts3ExprIterate(pExpr, fts3ExprPhraseCountCb, (void *)&nPhrase); + (void)sqlite3Fts3ExprIterate(pExpr, fts3ExprPhraseCountCb, (void *)&nPhrase); return nPhrase; } @@ -341,10 +345,10 @@ static int fts3ExprPhraseCount(Fts3Expr *pExpr){ ** arguments so that it points to the first element with a value greater ** than or equal to parameter iNext. */ -static void fts3SnippetAdvance(char **ppIter, int *piIter, int iNext){ +static void fts3SnippetAdvance(char **ppIter, i64 *piIter, int iNext){ char *pIter = *ppIter; if( pIter ){ - int iIter = *piIter; + i64 iIter = *piIter; while( iIternSnippet>=0 ); pIter->iCurrent = iStart = iEnd - pIter->nSnippet + 1; for(i=0; inPhrase; i++){ SnippetPhrase *pPhrase = &pIter->aPhrase[i]; @@ -427,13 +432,14 @@ static void fts3SnippetDetails( SnippetPhrase *pPhrase = &pIter->aPhrase[i]; if( pPhrase->pTail ){ char *pCsr = pPhrase->pTail; - int iCsr = pPhrase->iTail; + i64 iCsr = pPhrase->iTail; - while( iCsr<(iStart+pIter->nSnippet) ){ + while( iCsr<(iStart+pIter->nSnippet) && iCsr>=iStart ){ int j; - u64 mPhrase = (u64)1 << i; + u64 mPhrase = (u64)1 << (i%64); u64 mPos = (u64)1 << (iCsr - iStart); - assert( iCsr>=iStart ); + assert( iCsr>=iStart && (iCsr - iStart)<=64 ); + assert( i>=0 ); if( (mCover|mCovered)&mPhrase ){ iScore++; }else{ @@ -441,7 +447,7 @@ static void fts3SnippetDetails( } mCover |= mPhrase; - for(j=0; jnToken; j++){ + for(j=0; jnToken && jnSnippet; j++){ mHighlight |= (mPos>>j); } @@ -459,8 +465,9 @@ static void fts3SnippetDetails( } /* -** This function is an fts3ExprIterate() callback used by fts3BestSnippet(). -** Each invocation populates an element of the SnippetIter.aPhrase[] array. +** This function is an sqlite3Fts3ExprIterate() callback used by +** fts3BestSnippet(). Each invocation populates an element of the +** SnippetIter.aPhrase[] array. */ static int fts3SnippetFindPositions(Fts3Expr *pExpr, int iPhrase, void *ctx){ SnippetIter *p = (SnippetIter *)ctx; @@ -472,14 +479,17 @@ static int fts3SnippetFindPositions(Fts3Expr *pExpr, int iPhrase, void *ctx){ rc = sqlite3Fts3EvalPhrasePoslist(p->pCsr, pExpr, p->iCol, &pCsr); assert( rc==SQLITE_OK || pCsr==0 ); if( pCsr ){ - int iFirst = 0; + i64 iFirst = 0; pPhrase->pList = pCsr; fts3GetDeltaPosition(&pCsr, &iFirst); - assert( iFirst>=0 ); - pPhrase->pHead = pCsr; - pPhrase->pTail = pCsr; - pPhrase->iHead = iFirst; - pPhrase->iTail = iFirst; + if( iFirst<0 ){ + rc = FTS_CORRUPT_VTAB; + }else{ + pPhrase->pHead = pCsr; + pPhrase->pTail = pCsr; + pPhrase->iHead = iFirst; + pPhrase->iTail = iFirst; + } }else{ assert( rc!=SQLITE_OK || ( pPhrase->pList==0 && pPhrase->pHead==0 && pPhrase->pTail==0 @@ -516,7 +526,7 @@ static int fts3BestSnippet( int rc; /* Return Code */ int nList; /* Number of phrases in expression */ SnippetIter sIter; /* Iterates through snippet candidates */ - int nByte; /* Number of bytes of space to allocate */ + sqlite3_int64 nByte; /* Number of bytes of space to allocate */ int iBestScore = -1; /* Best snippet score found so far */ int i; /* Loop counter */ @@ -534,11 +544,10 @@ static int fts3BestSnippet( ** the required space using malloc(). */ nByte = sizeof(SnippetPhrase) * nList; - sIter.aPhrase = (SnippetPhrase *)sqlite3_malloc(nByte); + sIter.aPhrase = (SnippetPhrase *)sqlite3Fts3MallocZero(nByte); if( !sIter.aPhrase ){ return SQLITE_NOMEM; } - memset(sIter.aPhrase, 0, nByte); /* Initialize the contents of the SnippetIter object. Then iterate through ** the set of phrases in the expression to populate the aPhrase[] array. @@ -548,13 +557,15 @@ static int fts3BestSnippet( sIter.nSnippet = nSnippet; sIter.nPhrase = nList; sIter.iCurrent = -1; - rc = fts3ExprIterate(pCsr->pExpr, fts3SnippetFindPositions, (void*)&sIter); + rc = sqlite3Fts3ExprIterate( + pCsr->pExpr, fts3SnippetFindPositions, (void*)&sIter + ); if( rc==SQLITE_OK ){ /* Set the *pmSeen output variable. */ for(i=0; in+nAppend+1>=pStr->nAlloc ){ - int nAlloc = pStr->nAlloc+nAppend+100; - char *zNew = sqlite3_realloc(pStr->z, nAlloc); + sqlite3_int64 nAlloc = pStr->nAlloc+(sqlite3_int64)nAppend+100; + char *zNew = sqlite3_realloc64(pStr->z, nAlloc); if( !zNew ){ return SQLITE_NOMEM; } @@ -660,6 +671,7 @@ static int fts3SnippetShift( for(nLeft=0; !(hlmask & ((u64)1 << nLeft)); nLeft++); for(nRight=0; !(hlmask & ((u64)1 << (nSnippet-1-nRight))); nRight++); + assert( (nSnippet-1-nRight)<=63 && (nSnippet-1-nRight)>=0 ); nDesired = (nLeft-nRight)/2; /* Ideally, the start of the snippet should be pushed forward in the @@ -852,7 +864,7 @@ static int fts3ColumnlistCount(char **ppCollist){ /* ** This function gathers 'y' or 'b' data for a single phrase. */ -static void fts3ExprLHits( +static int fts3ExprLHits( Fts3Expr *pExpr, /* Phrase expression node */ MatchInfo *p /* Matchinfo context */ ){ @@ -869,7 +881,7 @@ static void fts3ExprLHits( iStart = pExpr->iPhrase * ((p->nCol + 31) / 32); } - while( 1 ){ + if( pIter ) while( 1 ){ int nHit = fts3ColumnlistCount(&pIter); if( (pPhrase->iColumn>=pTab->nColumn || pPhrase->iColumn==iCol) ){ if( p->flag==FTS3_MATCHINFO_LHITS ){ @@ -882,32 +894,36 @@ static void fts3ExprLHits( if( *pIter!=0x01 ) break; pIter++; pIter += fts3GetVarint32(pIter, &iCol); + if( iCol>=p->nCol ) return FTS_CORRUPT_VTAB; } + return SQLITE_OK; } /* ** Gather the results for matchinfo directives 'y' and 'b'. */ -static void fts3ExprLHitGather( +static int fts3ExprLHitGather( Fts3Expr *pExpr, MatchInfo *p ){ + int rc = SQLITE_OK; assert( (pExpr->pLeft==0)==(pExpr->pRight==0) ); if( pExpr->bEof==0 && pExpr->iDocid==p->pCursor->iPrevId ){ if( pExpr->pLeft ){ - fts3ExprLHitGather(pExpr->pLeft, p); - fts3ExprLHitGather(pExpr->pRight, p); + rc = fts3ExprLHitGather(pExpr->pLeft, p); + if( rc==SQLITE_OK ) rc = fts3ExprLHitGather(pExpr->pRight, p); }else{ - fts3ExprLHits(pExpr, p); + rc = fts3ExprLHits(pExpr, p); } } + return rc; } /* -** fts3ExprIterate() callback used to collect the "global" matchinfo stats -** for a single query. +** sqlite3Fts3ExprIterate() callback used to collect the "global" matchinfo +** stats for a single query. ** -** fts3ExprIterate() callback to load the 'global' elements of a +** sqlite3Fts3ExprIterate() callback to load the 'global' elements of a ** FTS3_MATCHINFO_HITS matchinfo array. The global stats are those elements ** of the matchinfo array that are constant for all rows returned by the ** current query. @@ -942,7 +958,7 @@ static int fts3ExprGlobalHitsCb( } /* -** fts3ExprIterate() callback used to collect the "local" part of the +** sqlite3Fts3ExprIterate() callback used to collect the "local" part of the ** FTS3_MATCHINFO_HITS array. The local stats are those elements of the ** array that are different for each row returned by the query. */ @@ -990,8 +1006,8 @@ static int fts3MatchinfoCheck( return SQLITE_ERROR; } -static int fts3MatchinfoSize(MatchInfo *pInfo, char cArg){ - int nVal; /* Number of integers output by cArg */ +static size_t fts3MatchinfoSize(MatchInfo *pInfo, char cArg){ + size_t nVal; /* Number of integers output by cArg */ switch( cArg ){ case FTS3_MATCHINFO_NDOC: @@ -1007,16 +1023,16 @@ static int fts3MatchinfoSize(MatchInfo *pInfo, char cArg){ break; case FTS3_MATCHINFO_LHITS: - nVal = pInfo->nCol * pInfo->nPhrase; + nVal = (size_t)pInfo->nCol * pInfo->nPhrase; break; case FTS3_MATCHINFO_LHITS_BM: - nVal = pInfo->nPhrase * ((pInfo->nCol + 31) / 32); + nVal = (size_t)pInfo->nPhrase * ((pInfo->nCol + 31) / 32); break; default: assert( cArg==FTS3_MATCHINFO_HITS ); - nVal = pInfo->nCol * pInfo->nPhrase * 3; + nVal = (size_t)pInfo->nCol * pInfo->nPhrase * 3; break; } @@ -1027,11 +1043,15 @@ static int fts3MatchinfoSelectDoctotal( Fts3Table *pTab, sqlite3_stmt **ppStmt, sqlite3_int64 *pnDoc, - const char **paLen + const char **paLen, + const char **ppEnd ){ sqlite3_stmt *pStmt; const char *a; + const char *pEnd; sqlite3_int64 nDoc; + int n; + if( !*ppStmt ){ int rc = sqlite3Fts3SelectDoctotal(pTab, ppStmt); @@ -1040,12 +1060,20 @@ static int fts3MatchinfoSelectDoctotal( pStmt = *ppStmt; assert( sqlite3_data_count(pStmt)==1 ); + n = sqlite3_column_bytes(pStmt, 0); a = sqlite3_column_blob(pStmt, 0); - a += sqlite3Fts3GetVarint(a, &nDoc); - if( nDoc==0 ) return FTS_CORRUPT_VTAB; - *pnDoc = (u32)nDoc; + if( a==0 ){ + return FTS_CORRUPT_VTAB; + } + pEnd = a + n; + a += sqlite3Fts3GetVarintBounded(a, pEnd, &nDoc); + if( nDoc<=0 || a>pEnd ){ + return FTS_CORRUPT_VTAB; + } + *pnDoc = nDoc; if( paLen ) *paLen = a; + if( ppEnd ) *ppEnd = pEnd; return SQLITE_OK; } @@ -1085,10 +1113,12 @@ static int fts3MatchinfoLcsCb( ** position list for the next column. */ static int fts3LcsIteratorAdvance(LcsIterator *pIter){ - char *pRead = pIter->pRead; + char *pRead; sqlite3_int64 iRead; int rc = 0; + if( NEVER(pIter==0) ) return 1; + pRead = pIter->pRead; pRead += sqlite3Fts3GetVarint(pRead, &iRead); if( iRead==0 || iRead==1 ){ pRead = 0; @@ -1117,14 +1147,14 @@ static int fts3MatchinfoLcs(Fts3Cursor *pCsr, MatchInfo *pInfo){ int i; int iCol; int nToken = 0; + int rc = SQLITE_OK; /* Allocate and populate the array of LcsIterator objects. The array ** contains one element for each matchable phrase in the query. **/ - aIter = sqlite3_malloc(sizeof(LcsIterator) * pCsr->nPhrase); + aIter = sqlite3Fts3MallocZero(sizeof(LcsIterator) * pCsr->nPhrase); if( !aIter ) return SQLITE_NOMEM; - memset(aIter, 0, sizeof(LcsIterator) * pCsr->nPhrase); - (void)fts3ExprIterate(pCsr->pExpr, fts3MatchinfoLcsCb, (void*)aIter); + (void)sqlite3Fts3ExprIterate(pCsr->pExpr, fts3MatchinfoLcsCb, (void*)aIter); for(i=0; inPhrase; i++){ LcsIterator *pIter = &aIter[i]; @@ -1137,13 +1167,16 @@ static int fts3MatchinfoLcs(Fts3Cursor *pCsr, MatchInfo *pInfo){ int nLive = 0; /* Number of iterators in aIter not at EOF */ for(i=0; inPhrase; i++){ - int rc; LcsIterator *pIt = &aIter[i]; rc = sqlite3Fts3EvalPhrasePoslist(pCsr, pIt->pExpr, iCol, &pIt->pRead); - if( rc!=SQLITE_OK ) return rc; + if( rc!=SQLITE_OK ) goto matchinfo_lcs_out; if( pIt->pRead ){ pIt->iPos = pIt->iPosOffset; - fts3LcsIteratorAdvance(&aIter[i]); + fts3LcsIteratorAdvance(pIt); + if( pIt->pRead==0 ){ + rc = FTS_CORRUPT_VTAB; + goto matchinfo_lcs_out; + } nLive++; } } @@ -1175,8 +1208,9 @@ static int fts3MatchinfoLcs(Fts3Cursor *pCsr, MatchInfo *pInfo){ pInfo->aMatchinfo[iCol] = nLcs; } + matchinfo_lcs_out: sqlite3_free(aIter); - return SQLITE_OK; + return rc; } /* @@ -1221,7 +1255,7 @@ static int fts3MatchinfoValues( case FTS3_MATCHINFO_NDOC: if( bGlobal ){ sqlite3_int64 nDoc = 0; - rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &nDoc, 0); + rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &nDoc, 0, 0); pInfo->aMatchinfo[0] = (u32)nDoc; } break; @@ -1230,14 +1264,19 @@ static int fts3MatchinfoValues( if( bGlobal ){ sqlite3_int64 nDoc; /* Number of rows in table */ const char *a; /* Aggregate column length array */ + const char *pEnd; /* First byte past end of length array */ - rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &nDoc, &a); + rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &nDoc, &a, &pEnd); if( rc==SQLITE_OK ){ int iCol; for(iCol=0; iColnCol; iCol++){ u32 iVal; sqlite3_int64 nToken; a += sqlite3Fts3GetVarint(a, &nToken); + if( a>pEnd ){ + rc = SQLITE_CORRUPT_VTAB; + break; + } iVal = (u32)(((u32)(nToken&0xffffffff)+nDoc/2)/nDoc); pInfo->aMatchinfo[iCol] = iVal; } @@ -1251,9 +1290,14 @@ static int fts3MatchinfoValues( if( rc==SQLITE_OK ){ int iCol; const char *a = sqlite3_column_blob(pSelectDocsize, 0); + const char *pEnd = a + sqlite3_column_bytes(pSelectDocsize, 0); for(iCol=0; iColnCol; iCol++){ sqlite3_int64 nToken; - a += sqlite3Fts3GetVarint(a, &nToken); + a += sqlite3Fts3GetVarintBounded(a, pEnd, &nToken); + if( a>pEnd ){ + rc = SQLITE_CORRUPT_VTAB; + break; + } pInfo->aMatchinfo[iCol] = (u32)nToken; } } @@ -1270,9 +1314,9 @@ static int fts3MatchinfoValues( case FTS3_MATCHINFO_LHITS_BM: case FTS3_MATCHINFO_LHITS: { - int nZero = fts3MatchinfoSize(pInfo, zArg[i]) * sizeof(u32); + size_t nZero = fts3MatchinfoSize(pInfo, zArg[i]) * sizeof(u32); memset(pInfo->aMatchinfo, 0, nZero); - fts3ExprLHitGather(pCsr->pExpr, pInfo); + rc = fts3ExprLHitGather(pCsr->pExpr, pInfo); break; } @@ -1284,14 +1328,14 @@ static int fts3MatchinfoValues( if( rc!=SQLITE_OK ) break; if( bGlobal ){ if( pCsr->pDeferred ){ - rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &pInfo->nDoc, 0); + rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &pInfo->nDoc,0,0); if( rc!=SQLITE_OK ) break; } - rc = fts3ExprIterate(pExpr, fts3ExprGlobalHitsCb,(void*)pInfo); + rc = sqlite3Fts3ExprIterate(pExpr, fts3ExprGlobalHitsCb,(void*)pInfo); sqlite3Fts3EvalTestDeferred(pCsr, &rc); if( rc!=SQLITE_OK ) break; } - (void)fts3ExprIterate(pExpr, fts3ExprLocalHitsCb,(void*)pInfo); + (void)sqlite3Fts3ExprIterate(pExpr, fts3ExprLocalHitsCb,(void*)pInfo); break; } } @@ -1339,7 +1383,7 @@ static void fts3GetMatchinfo( ** initialize those elements that are constant for every row. */ if( pCsr->pMIBuffer==0 ){ - int nMatchinfo = 0; /* Number of u32 elements in match-info */ + size_t nMatchinfo = 0; /* Number of u32 elements in match-info */ int i; /* Used to iterate through zArg */ /* Determine the number of phrases in the query */ @@ -1424,6 +1468,10 @@ void sqlite3Fts3Snippet( return; } + /* Limit the snippet length to 64 tokens. */ + if( nToken<-64 ) nToken = -64; + if( nToken>+64 ) nToken = +64; + for(nSnippet=1; 1; nSnippet++){ int iSnip; /* Loop counter 0..nSnippet-1 */ @@ -1497,8 +1545,8 @@ typedef struct TermOffsetCtx TermOffsetCtx; struct TermOffset { char *pList; /* Position-list */ - int iPos; /* Position just read from pList */ - int iOff; /* Offset of this term from read positions */ + i64 iPos; /* Position just read from pList */ + i64 iOff; /* Offset of this term from read positions */ }; struct TermOffsetCtx { @@ -1510,14 +1558,14 @@ struct TermOffsetCtx { }; /* -** This function is an fts3ExprIterate() callback used by sqlite3Fts3Offsets(). +** This function is an sqlite3Fts3ExprIterate() callback used by sqlite3Fts3Offsets(). */ static int fts3ExprTermOffsetInit(Fts3Expr *pExpr, int iPhrase, void *ctx){ TermOffsetCtx *p = (TermOffsetCtx *)ctx; int nTerm; /* Number of tokens in phrase */ int iTerm; /* For looping through nTerm phrase terms */ char *pList; /* Pointer to position list for phrase */ - int iPos = 0; /* First position in position-list */ + i64 iPos = 0; /* First position in position-list */ int rc; UNUSED_PARAMETER(iPhrase); @@ -1525,7 +1573,7 @@ static int fts3ExprTermOffsetInit(Fts3Expr *pExpr, int iPhrase, void *ctx){ nTerm = pExpr->pPhrase->nToken; if( pList ){ fts3GetDeltaPosition(&pList, &iPos); - assert( iPos>=0 ); + assert_fts3_nc( iPos>=0 ); } for(iTerm=0; iTermpPhrase && pExpr->pPhrase->bIncr ){ + rc = sqlite3Fts3MsrCancel(p->pCsr, pExpr); + pExpr->pPhrase->bIncr = 0; + } + return rc; +} + /* ** Implementation of offsets() function. */ @@ -1566,7 +1630,7 @@ void sqlite3Fts3Offsets( if( rc!=SQLITE_OK ) goto offsets_out; /* Allocate the array of TermOffset iterators. */ - sCtx.aTerm = (TermOffset *)sqlite3_malloc(sizeof(TermOffset)*nToken); + sCtx.aTerm = (TermOffset *)sqlite3Fts3MallocZero(sizeof(TermOffset)*nToken); if( 0==sCtx.aTerm ){ rc = SQLITE_NOMEM; goto offsets_out; @@ -1574,6 +1638,12 @@ void sqlite3Fts3Offsets( sCtx.iDocid = pCsr->iPrevId; sCtx.pCsr = pCsr; + /* If a query restart will be required, do it here, rather than later of + ** after pointers to poslist buffers that may be invalidated by a restart + ** have been saved. */ + rc = sqlite3Fts3ExprIterate(pCsr->pExpr, fts3ExprRestartIfCb, (void*)&sCtx); + if( rc!=SQLITE_OK ) goto offsets_out; + /* Loop through the table columns, appending offset information to ** string-buffer res for each column. */ @@ -1587,13 +1657,15 @@ void sqlite3Fts3Offsets( const char *zDoc; int nDoc; - /* Initialize the contents of sCtx.aTerm[] for column iCol. There is - ** no way that this operation can fail, so the return code from - ** fts3ExprIterate() can be discarded. + /* Initialize the contents of sCtx.aTerm[] for column iCol. This + ** operation may fail if the database contains corrupt records. */ sCtx.iCol = iCol; sCtx.iTerm = 0; - (void)fts3ExprIterate(pCsr->pExpr, fts3ExprTermOffsetInit, (void*)&sCtx); + rc = sqlite3Fts3ExprIterate( + pCsr->pExpr, fts3ExprTermOffsetInit, (void*)&sCtx + ); + if( rc!=SQLITE_OK ) goto offsets_out; /* Retreive the text stored in column iCol. If an SQL NULL is stored ** in column iCol, jump immediately to the next iteration of the loop. @@ -1635,7 +1707,7 @@ void sqlite3Fts3Offsets( /* All offsets for this column have been gathered. */ rc = SQLITE_DONE; }else{ - assert( iCurrent<=iMinPos ); + assert_fts3_nc( iCurrent<=iMinPos ); if( 0==(0xFE&*pTerm->pList) ){ pTerm->pList = 0; }else{ diff --git a/ext/fts3/fts3_term.c b/ext/fts3/fts3_term.c index 7edd072892..655dd9f35a 100644 --- a/ext/fts3/fts3_term.c +++ b/ext/fts3/fts3_term.c @@ -68,9 +68,9 @@ static int fts3termConnectMethod( char const *zFts3; /* Name of fts3 table */ int nDb; /* Result of strlen(zDb) */ int nFts3; /* Result of strlen(zFts3) */ - int nByte; /* Bytes of space to allocate here */ + sqlite3_int64 nByte; /* Bytes of space to allocate here */ int rc; /* value returned by declare_vtab() */ - Fts3termTable *p; /* Virtual table object to return */ + Fts3termTable *p; /* Virtual table object to return */ int iIndex = 0; UNUSED_PARAMETER(pCtx); @@ -78,6 +78,8 @@ static int fts3termConnectMethod( iIndex = atoi(argv[4]); argc--; } + + *ppVtab = 0; /* The user should specify a single argument - the name of an fts3 table. */ if( argc!=4 ){ @@ -95,12 +97,17 @@ static int fts3termConnectMethod( rc = sqlite3_declare_vtab(db, FTS3_TERMS_SCHEMA); if( rc!=SQLITE_OK ) return rc; - nByte = sizeof(Fts3termTable) + sizeof(Fts3Table) + nDb + nFts3 + 2; - p = (Fts3termTable *)sqlite3_malloc(nByte); + nByte = sizeof(Fts3termTable); + p = (Fts3termTable *)sqlite3Fts3MallocZero(nByte); if( !p ) return SQLITE_NOMEM; - memset(p, 0, nByte); - p->pFts3Tab = (Fts3Table *)&p[1]; + p->pFts3Tab = (Fts3Table*)sqlite3Fts3MallocZero( + sizeof(Fts3Table) + nDb + nFts3 + 2 + ); + if( p->pFts3Tab==0 ){ + sqlite3_free(p); + return SQLITE_NOMEM; + } p->pFts3Tab->zDb = (char *)&p->pFts3Tab[1]; p->pFts3Tab->zName = &p->pFts3Tab->zDb[nDb+1]; p->pFts3Tab->db = db; @@ -130,6 +137,7 @@ static int fts3termDisconnectMethod(sqlite3_vtab *pVtab){ sqlite3_finalize(pFts3->aStmt[i]); } sqlite3_free(pFts3->zSegmentsTbl); + sqlite3_free(pFts3); sqlite3_free(p); return SQLITE_OK; } @@ -361,7 +369,9 @@ int sqlite3Fts3InitTerm(sqlite3 *db){ 0, /* xRename */ 0, /* xSavepoint */ 0, /* xRelease */ - 0 /* xRollbackTo */ + 0, /* xRollbackTo */ + 0, /* xShadowName */ + 0 /* xIntegrity */ }; int rc; /* Return code */ diff --git a/ext/fts3/fts3_test.c b/ext/fts3/fts3_test.c index 36dcc94e6d..70bccf0c52 100644 --- a/ext/fts3/fts3_test.c +++ b/ext/fts3/fts3_test.c @@ -18,7 +18,7 @@ ** that the sqlite3_tokenizer_module.xLanguage() method is invoked correctly. */ -#include +#include "tclsqlite.h" #include #include @@ -143,7 +143,7 @@ static int nm_match_count( /* ** Tclcmd: fts3_near_match DOCUMENT EXPR ?OPTIONS? */ -static int fts3_near_match_cmd( +static int SQLITE_TCLAPI fts3_near_match_cmd( ClientData clientData, Tcl_Interp *interp, int objc, @@ -160,7 +160,8 @@ static int fts3_near_match_cmd( Tcl_Obj *pPhrasecount = 0; Tcl_Obj **apExprToken; - int nExprToken; + Tcl_Size nExprToken; + Tcl_Size nn; UNUSED_PARAMETER(clientData); @@ -194,37 +195,40 @@ static int fts3_near_match_cmd( } } - rc = Tcl_ListObjGetElements(interp, objv[1], &doc.nToken, &apDocToken); + rc = Tcl_ListObjGetElements(interp, objv[1], &nn, &apDocToken); + doc.nToken = (int)nn; if( rc!=TCL_OK ) goto near_match_out; doc.aToken = (NearToken *)ckalloc(doc.nToken*sizeof(NearToken)); for(ii=0; iiNM_MAX_TOKEN ){ - Tcl_AppendResult(interp, "Too many tokens in phrase", 0); + Tcl_AppendResult(interp, "Too many tokens in phrase", NULL); rc = TCL_ERROR; goto near_match_out; } - for(jj=0; jjz = Tcl_GetStringFromObj(apToken[jj], &pT->n); + pT->z = Tcl_GetStringFromObj(apToken[jj], &nn); + pT->n = (int)nn; } - aPhrase[ii].nToken = nToken; + aPhrase[ii].nToken = (int)nToken; } for(ii=1; iipCsr->nBuffer ){ sqlite3_free(pCsr->aBuffer); - pCsr->aBuffer = sqlite3_malloc(nToken); + pCsr->aBuffer = sqlite3_malloc64(nToken); } if( pCsr->aBuffer==0 ){ rc = SQLITE_NOMEM; @@ -458,13 +462,13 @@ static int testTokenizerNext( if( pCsr->iLangid & 0x00000001 ){ for(i=0; iaBuffer[i] = pToken[i]; }else{ - for(i=0; iaBuffer[i] = testTolower(pToken[i]); + for(i=0; iaBuffer[i] = (char)testTolower(pToken[i]); } pCsr->iToken++; pCsr->iInput = (int)(p - pCsr->aInput); *ppToken = pCsr->aBuffer; - *pnBytes = nToken; + *pnBytes = (int)nToken; *piStartOffset = (int)(pToken - pCsr->aInput); *piEndOffset = (int)(p - pCsr->aInput); *piPosition = pCsr->iToken; @@ -488,7 +492,7 @@ static int testTokenizerLanguage( } #endif -static int fts3_test_tokenizer_cmd( +static int SQLITE_TCLAPI fts3_test_tokenizer_cmd( ClientData clientData, Tcl_Interp *interp, int objc, @@ -517,7 +521,7 @@ static int fts3_test_tokenizer_cmd( return TCL_OK; } -static int fts3_test_varint_cmd( +static int SQLITE_TCLAPI fts3_test_varint_cmd( ClientData clientData, Tcl_Interp *interp, int objc, @@ -526,7 +530,8 @@ static int fts3_test_varint_cmd( #ifdef SQLITE_ENABLE_FTS3 char aBuf[24]; int rc; - Tcl_WideInt w, w2; + Tcl_WideInt w; + sqlite3_int64 w2; int nByte, nByte2; if( objc!=2 ){ @@ -542,7 +547,7 @@ static int fts3_test_varint_cmd( if( w!=w2 || nByte!=nByte2 ){ char *zErr = sqlite3_mprintf("error testing %lld", w); Tcl_ResetResult(interp); - Tcl_AppendResult(interp, zErr, 0); + Tcl_AppendResult(interp, zErr, NULL); return TCL_ERROR; } @@ -552,7 +557,7 @@ static int fts3_test_varint_cmd( if( (int)w!=i || nByte!=nByte2 ){ char *zErr = sqlite3_mprintf("error testing %lld (32-bit)", w); Tcl_ResetResult(interp); - Tcl_AppendResult(interp, zErr, 0); + Tcl_AppendResult(interp, zErr, NULL); return TCL_ERROR; } } @@ -566,6 +571,35 @@ static int fts3_test_varint_cmd( ** End of tokenizer code. **************************************************************************/ +/* +** sqlite3_fts3_may_be_corrupt BOOLEAN +** +** Set or clear the global "may-be-corrupt" flag. Return the old value. +*/ +static int SQLITE_TCLAPI fts3_may_be_corrupt( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ +#ifdef SQLITE_DEBUG + int bOld = sqlite3_fts3_may_be_corrupt; + + if( objc!=2 && objc!=1 ){ + Tcl_WrongNumArgs(interp, 1, objv, "?BOOLEAN?"); + return TCL_ERROR; + } + if( objc==2 ){ + int bNew; + if( Tcl_GetBooleanFromObj(interp, objv[1], &bNew) ) return TCL_ERROR; + sqlite3_fts3_may_be_corrupt = bNew; + } + + Tcl_SetObjResult(interp, Tcl_NewIntObj(bOld)); +#endif + return TCL_OK; +} + int Sqlitetestfts3_Init(Tcl_Interp *interp){ Tcl_CreateObjCommand(interp, "fts3_near_match", fts3_near_match_cmd, 0, 0); Tcl_CreateObjCommand(interp, @@ -574,10 +608,12 @@ int Sqlitetestfts3_Init(Tcl_Interp *interp){ Tcl_CreateObjCommand( interp, "fts3_test_tokenizer", fts3_test_tokenizer_cmd, 0, 0 ); - Tcl_CreateObjCommand( interp, "fts3_test_varint", fts3_test_varint_cmd, 0, 0 ); + Tcl_CreateObjCommand( + interp, "sqlite3_fts3_may_be_corrupt", fts3_may_be_corrupt, 0, 0 + ); return TCL_OK; } #endif /* SQLITE_ENABLE_FTS3 || SQLITE_ENABLE_FTS4 */ diff --git a/ext/fts3/fts3_tokenize_vtab.c b/ext/fts3/fts3_tokenize_vtab.c index dfeddfeb96..b9d83982c8 100644 --- a/ext/fts3/fts3_tokenize_vtab.c +++ b/ext/fts3/fts3_tokenize_vtab.c @@ -122,7 +122,7 @@ static int fts3tokDequoteArray( nByte += (int)(strlen(argv[i]) + 1); } - *pazDequote = azDequote = sqlite3_malloc(sizeof(char *)*argc + nByte); + *pazDequote = azDequote = sqlite3_malloc64(sizeof(char *)*argc + nByte); if( azDequote==0 ){ rc = SQLITE_NOMEM; }else{ @@ -188,7 +188,8 @@ static int fts3tokConnectMethod( assert( (rc==SQLITE_OK)==(pMod!=0) ); if( rc==SQLITE_OK ){ - const char * const *azArg = (const char * const *)&azDequote[1]; + const char * const *azArg = 0; + if( nDequote>1 ) azArg = (const char * const *)&azDequote[1]; rc = pMod->xCreate((nDequote>1 ? nDequote-1 : 0), azArg, &pTok); } @@ -345,12 +346,12 @@ static int fts3tokFilterMethod( fts3tokResetCursor(pCsr); if( idxNum==1 ){ const char *zByte = (const char *)sqlite3_value_text(apVal[0]); - int nByte = sqlite3_value_bytes(apVal[0]); - pCsr->zInput = sqlite3_malloc(nByte+1); + sqlite3_int64 nByte = sqlite3_value_bytes(apVal[0]); + pCsr->zInput = sqlite3_malloc64(nByte+1); if( pCsr->zInput==0 ){ rc = SQLITE_NOMEM; }else{ - memcpy(pCsr->zInput, zByte, nByte); + if( nByte>0 ) memcpy(pCsr->zInput, zByte, nByte); pCsr->zInput[nByte] = 0; rc = pTab->pMod->xOpen(pTab->pTok, pCsr->zInput, nByte, &pCsr->pCsr); if( rc==SQLITE_OK ){ @@ -419,7 +420,7 @@ static int fts3tokRowidMethod( ** Register the fts3tok module with database connection db. Return SQLITE_OK ** if successful or an error code if sqlite3_create_module() fails. */ -int sqlite3Fts3InitTok(sqlite3 *db, Fts3Hash *pHash){ +int sqlite3Fts3InitTok(sqlite3 *db, Fts3Hash *pHash, void(*xDestroy)(void*)){ static const sqlite3_module fts3tok_module = { 0, /* iVersion */ fts3tokConnectMethod, /* xCreate */ @@ -443,11 +444,15 @@ int sqlite3Fts3InitTok(sqlite3 *db, Fts3Hash *pHash){ 0, /* xRename */ 0, /* xSavepoint */ 0, /* xRelease */ - 0 /* xRollbackTo */ + 0, /* xRollbackTo */ + 0, /* xShadowName */ + 0 /* xIntegrity */ }; int rc; /* Return code */ - rc = sqlite3_create_module(db, "fts3tokenize", &fts3tok_module, (void*)pHash); + rc = sqlite3_create_module_v2( + db, "fts3tokenize", &fts3tok_module, (void*)pHash, xDestroy + ); return rc; } diff --git a/ext/fts3/fts3_tokenizer.c b/ext/fts3/fts3_tokenizer.c index fcabe5cca2..24c237a89d 100644 --- a/ext/fts3/fts3_tokenizer.c +++ b/ext/fts3/fts3_tokenizer.c @@ -29,6 +29,18 @@ #include #include +/* +** Return true if the two-argument version of fts3_tokenizer() +** has been activated via a prior call to sqlite3_db_config(db, +** SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, 1, 0); +*/ +static int fts3TokenizerEnabled(sqlite3_context *context){ + sqlite3 *db = sqlite3_context_db_handle(context); + int isEnabled = 0; + sqlite3_db_config(db,SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER,-1,&isEnabled); + return isEnabled; +} + /* ** Implementation of the SQL scalar function for accessing the underlying ** hash table. This function may be called as follows: @@ -49,7 +61,7 @@ ** is a blob containing the pointer stored as the hash data corresponding ** to string (after the hash-table is updated, if applicable). */ -static void scalarFunc( +static void fts3TokenizerFunc( sqlite3_context *context, int argc, sqlite3_value **argv @@ -67,27 +79,23 @@ static void scalarFunc( nName = sqlite3_value_bytes(argv[0])+1; if( argc==2 ){ -#ifdef SQLITE_ENABLE_FTS3_TOKENIZER - void *pOld; - int n = sqlite3_value_bytes(argv[1]); - if( zName==0 || n!=sizeof(pPtr) ){ - sqlite3_result_error(context, "argument type mismatch", -1); - return; - } - pPtr = *(void **)sqlite3_value_blob(argv[1]); - pOld = sqlite3Fts3HashInsert(pHash, (void *)zName, nName, pPtr); - if( pOld==pPtr ){ - sqlite3_result_error(context, "out of memory", -1); + if( fts3TokenizerEnabled(context) || sqlite3_value_frombind(argv[1]) ){ + void *pOld; + int n = sqlite3_value_bytes(argv[1]); + if( zName==0 || n!=sizeof(pPtr) ){ + sqlite3_result_error(context, "argument type mismatch", -1); + return; + } + pPtr = *(void **)sqlite3_value_blob(argv[1]); + pOld = sqlite3Fts3HashInsert(pHash, (void *)zName, nName, pPtr); + if( pOld==pPtr ){ + sqlite3_result_error(context, "out of memory", -1); + } + }else{ + sqlite3_result_error(context, "fts3tokenize disabled", -1); return; } -#else - sqlite3_result_error(context, "fts3tokenize: " - "disabled - rebuild with -DSQLITE_ENABLE_FTS3_TOKENIZER", -1 - ); - return; -#endif /* SQLITE_ENABLE_FTS3_TOKENIZER */ - }else - { + }else{ if( zName ){ pPtr = sqlite3Fts3HashFind(pHash, zName, nName); } @@ -98,8 +106,9 @@ static void scalarFunc( return; } } - - sqlite3_result_blob(context, (void *)&pPtr, sizeof(pPtr), SQLITE_TRANSIENT); + if( fts3TokenizerEnabled(context) || sqlite3_value_frombind(argv[0]) ){ + sqlite3_result_blob(context, (void *)&pPtr, sizeof(pPtr), SQLITE_TRANSIENT); + } } int sqlite3Fts3IsIdChar(char c){ @@ -187,8 +196,8 @@ int sqlite3Fts3InitTokenizer( int iArg = 0; z = &z[n+1]; while( z +#include "tclsqlite.h" #include /* @@ -336,7 +345,6 @@ static void testFunc( Tcl_DecrRefCount(pRet); } -#ifdef SQLITE_ENABLE_FTS3_TOKENIZER static int registerTokenizer( sqlite3 *db, @@ -358,7 +366,6 @@ int registerTokenizer( return sqlite3_finalize(pStmt); } -#endif /* SQLITE_ENABLE_FTS3_TOKENIZER */ static @@ -379,7 +386,9 @@ int queryTokenizer( sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC); if( SQLITE_ROW==sqlite3_step(pStmt) ){ - if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB ){ + if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB + && sqlite3_column_bytes(pStmt, 0)==sizeof(*pp) + ){ memcpy((void *)pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp)); } } @@ -431,13 +440,13 @@ static void intTestFunc( assert( 0==strcmp(sqlite3_errmsg(db), "unknown tokenizer: nosuchtokenizer") ); /* Test the storage function */ -#ifdef SQLITE_ENABLE_FTS3_TOKENIZER - rc = registerTokenizer(db, "nosuchtokenizer", p1); - assert( rc==SQLITE_OK ); - rc = queryTokenizer(db, "nosuchtokenizer", &p2); - assert( rc==SQLITE_OK ); - assert( p2==p1 ); -#endif + if( fts3TokenizerEnabled(context) ){ + rc = registerTokenizer(db, "nosuchtokenizer", p1); + assert( rc==SQLITE_OK ); + rc = queryTokenizer(db, "nosuchtokenizer", &p2); + assert( rc==SQLITE_OK ); + assert( p2==p1 ); + } sqlite3_result_text(context, "ok", -1, SQLITE_STATIC); } @@ -453,7 +462,7 @@ static void intTestFunc( ** sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1); ** ** This function adds a scalar function (see header comment above -** scalarFunc() in this file for details) and, if ENABLE_TABLE is +** fts3TokenizerFunc() in this file for details) and, if ENABLE_TABLE is ** defined at compilation time, a temporary virtual table (see header ** comment above struct HashTableVtab) to the database schema. Both ** provide read/write access to the contents of *pHash. @@ -468,7 +477,7 @@ int sqlite3Fts3InitHashTable( ){ int rc = SQLITE_OK; void *p = (void *)pHash; - const int any = SQLITE_ANY; + const int any = SQLITE_UTF8|SQLITE_DIRECTONLY; #ifdef SQLITE_TEST char *zTest = 0; @@ -482,10 +491,10 @@ int sqlite3Fts3InitHashTable( #endif if( SQLITE_OK==rc ){ - rc = sqlite3_create_function(db, zName, 1, any, p, scalarFunc, 0, 0); + rc = sqlite3_create_function(db, zName, 1, any, p, fts3TokenizerFunc, 0, 0); } if( SQLITE_OK==rc ){ - rc = sqlite3_create_function(db, zName, 2, any, p, scalarFunc, 0, 0); + rc = sqlite3_create_function(db, zName, 2, any, p, fts3TokenizerFunc, 0, 0); } #ifdef SQLITE_TEST if( SQLITE_OK==rc ){ diff --git a/ext/fts3/fts3_tokenizer1.c b/ext/fts3/fts3_tokenizer1.c index deea06d92b..78e5889da5 100644 --- a/ext/fts3/fts3_tokenizer1.c +++ b/ext/fts3/fts3_tokenizer1.c @@ -185,7 +185,7 @@ static int simpleNext( if( n>c->nTokenAllocated ){ char *pNew; c->nTokenAllocated = n+20; - pNew = sqlite3_realloc(c->pToken, c->nTokenAllocated); + pNew = sqlite3_realloc64(c->pToken, c->nTokenAllocated); if( !pNew ) return SQLITE_NOMEM; c->pToken = pNew; } diff --git a/ext/fts3/fts3_unicode.c b/ext/fts3/fts3_unicode.c index 94fc27b5b4..2fd4b39fb7 100644 --- a/ext/fts3/fts3_unicode.c +++ b/ext/fts3/fts3_unicode.c @@ -82,7 +82,7 @@ typedef struct unicode_cursor unicode_cursor; struct unicode_tokenizer { sqlite3_tokenizer base; - int bRemoveDiacritic; + int eRemoveDiacritic; int nException; int *aiException; }; @@ -136,16 +136,16 @@ static int unicodeAddExceptions( ){ const unsigned char *z = (const unsigned char *)zIn; const unsigned char *zTerm = &z[nIn]; - int iCode; + unsigned int iCode; int nEntry = 0; assert( bAlnum==0 || bAlnum==1 ); while( zaiException, (p->nException+nEntry)*sizeof(int)); + aNew = sqlite3_realloc64(p->aiException,(p->nException+nEntry)*sizeof(int)); if( aNew==0 ) return SQLITE_NOMEM; nNew = p->nException; z = (const unsigned char *)zIn; while( zi; j--) aNew[j] = aNew[j-1]; - aNew[i] = iCode; + aNew[i] = (int)iCode; nNew++; } } @@ -227,17 +227,20 @@ static int unicodeCreate( pNew = (unicode_tokenizer *) sqlite3_malloc(sizeof(unicode_tokenizer)); if( pNew==NULL ) return SQLITE_NOMEM; memset(pNew, 0, sizeof(unicode_tokenizer)); - pNew->bRemoveDiacritic = 1; + pNew->eRemoveDiacritic = 1; for(i=0; rc==SQLITE_OK && ibRemoveDiacritic = 1; + pNew->eRemoveDiacritic = 1; } else if( n==19 && memcmp("remove_diacritics=0", z, 19)==0 ){ - pNew->bRemoveDiacritic = 0; + pNew->eRemoveDiacritic = 0; + } + else if( n==19 && memcmp("remove_diacritics=2", z, 19)==0 ){ + pNew->eRemoveDiacritic = 2; } else if( n>=11 && memcmp("tokenchars=", z, 11)==0 ){ rc = unicodeAddExceptions(pNew, 1, &z[11], n-11); @@ -282,6 +285,7 @@ static int unicodeOpen( pCsr->aInput = (const unsigned char *)aInput; if( aInput==0 ){ pCsr->nInput = 0; + pCsr->aInput = (const unsigned char*)""; }else if( nInput<0 ){ pCsr->nInput = (int)strlen(aInput); }else{ @@ -318,7 +322,7 @@ static int unicodeNext( ){ unicode_cursor *pCsr = (unicode_cursor *)pC; unicode_tokenizer *p = ((unicode_tokenizer *)pCsr->base.pTokenizer); - int iCode = 0; + unsigned int iCode = 0; char *zOut; const unsigned char *z = &pCsr->aInput[pCsr->iOff]; const unsigned char *zStart = z; @@ -330,7 +334,7 @@ static int unicodeNext( ** the input. */ while( z=zTerm ) return SQLITE_DONE; @@ -341,7 +345,7 @@ static int unicodeNext( /* Grow the output buffer if required. */ if( (zOut-pCsr->zToken)>=(pCsr->nAlloc-4) ){ - char *zNew = sqlite3_realloc(pCsr->zToken, pCsr->nAlloc+64); + char *zNew = sqlite3_realloc64(pCsr->zToken, pCsr->nAlloc+64); if( !zNew ) return SQLITE_NOMEM; zOut = &zNew[zOut - pCsr->zToken]; pCsr->zToken = zNew; @@ -350,7 +354,7 @@ static int unicodeNext( /* Write the folded case of the last character read to the output */ zEnd = z; - iOut = sqlite3FtsUnicodeFold(iCode, p->bRemoveDiacritic); + iOut = sqlite3FtsUnicodeFold((int)iCode, p->eRemoveDiacritic); if( iOut ){ WRITE_UTF8(zOut, iOut); } @@ -358,8 +362,8 @@ static int unicodeNext( /* If the cursor is not at EOF, read the next character */ if( z>=zTerm ) break; READ_UTF8(z, zTerm, iCode); - }while( unicodeIsAlnum(p, iCode) - || sqlite3FtsUnicodeIsdiacritic(iCode) + }while( unicodeIsAlnum(p, (int)iCode) + || sqlite3FtsUnicodeIsdiacritic((int)iCode) ); /* Set the output variables and return. */ diff --git a/ext/fts3/fts3_unicode2.c b/ext/fts3/fts3_unicode2.c index 20b7a25dbf..c510162496 100644 --- a/ext/fts3/fts3_unicode2.c +++ b/ext/fts3/fts3_unicode2.c @@ -1,5 +1,5 @@ /* -** 2012 May 25 +** 2012-05-25 ** ** The author disclaims copyright to this source code. In place of ** a legal notice, here is a blessing: @@ -127,9 +127,9 @@ int sqlite3FtsUnicodeIsalnum(int c){ 0xFFFFFFFF, 0xFC00FFFF, 0xF8000001, 0xF8000001, }; - if( c<128 ){ - return ( (aAscii[c >> 5] & (1 << (c & 0x001F)))==0 ); - }else if( c<(1<<22) ){ + if( (unsigned int)c<128 ){ + return ( (aAscii[c >> 5] & ((unsigned int)1 << (c & 0x001F)))==0 ); + }else if( (unsigned int)c<(1<<22) ){ unsigned int key = (((unsigned int)c)<<10) | 0x000003FF; int iRes = 0; int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1; @@ -159,32 +159,48 @@ int sqlite3FtsUnicodeIsalnum(int c){ ** E"). The resuls of passing a codepoint that corresponds to an ** uppercase letter are undefined. */ -static int remove_diacritic(int c){ +static int remove_diacritic(int c, int bComplex){ unsigned short aDia[] = { 0, 1797, 1848, 1859, 1891, 1928, 1940, 1995, 2024, 2040, 2060, 2110, 2168, 2206, 2264, 2286, 2344, 2383, 2472, 2488, 2516, 2596, 2668, 2732, 2782, 2842, 2894, 2954, 2984, 3000, 3028, 3336, - 3456, 3696, 3712, 3728, 3744, 3896, 3912, 3928, - 3968, 4008, 4040, 4106, 4138, 4170, 4202, 4234, - 4266, 4296, 4312, 4344, 4408, 4424, 4472, 4504, - 6148, 6198, 6264, 6280, 6360, 6429, 6505, 6529, - 61448, 61468, 61534, 61592, 61642, 61688, 61704, 61726, - 61784, 61800, 61836, 61880, 61914, 61948, 61998, 62122, - 62154, 62200, 62218, 62302, 62364, 62442, 62478, 62536, - 62554, 62584, 62604, 62640, 62648, 62656, 62664, 62730, - 62924, 63050, 63082, 63274, 63390, + 3456, 3696, 3712, 3728, 3744, 3766, 3832, 3896, + 3912, 3928, 3944, 3968, 4008, 4040, 4056, 4106, + 4138, 4170, 4202, 4234, 4266, 4296, 4312, 4344, + 4408, 4424, 4442, 4472, 4488, 4504, 6148, 6198, + 6264, 6280, 6360, 6429, 6505, 6529, 61448, 61468, + 61512, 61534, 61592, 61610, 61642, 61672, 61688, 61704, + 61726, 61784, 61800, 61816, 61836, 61880, 61896, 61914, + 61948, 61998, 62062, 62122, 62154, 62184, 62200, 62218, + 62252, 62302, 62364, 62410, 62442, 62478, 62536, 62554, + 62584, 62604, 62640, 62648, 62656, 62664, 62730, 62766, + 62830, 62890, 62924, 62974, 63032, 63050, 63082, 63118, + 63182, 63242, 63274, 63310, 63368, 63390, }; - char aChar[] = { - '\0', 'a', 'c', 'e', 'i', 'n', 'o', 'u', 'y', 'y', 'a', 'c', - 'd', 'e', 'e', 'g', 'h', 'i', 'j', 'k', 'l', 'n', 'o', 'r', - 's', 't', 'u', 'u', 'w', 'y', 'z', 'o', 'u', 'a', 'i', 'o', - 'u', 'g', 'k', 'o', 'j', 'g', 'n', 'a', 'e', 'i', 'o', 'r', - 'u', 's', 't', 'h', 'a', 'e', 'o', 'y', '\0', '\0', '\0', '\0', - '\0', '\0', '\0', '\0', 'a', 'b', 'd', 'd', 'e', 'f', 'g', 'h', - 'h', 'i', 'k', 'l', 'l', 'm', 'n', 'p', 'r', 'r', 's', 't', - 'u', 'v', 'w', 'w', 'x', 'y', 'z', 'h', 't', 'w', 'y', 'a', - 'e', 'i', 'o', 'u', 'y', +#define HIBIT ((unsigned char)0x80) + unsigned char aChar[] = { + '\0', 'a', 'c', 'e', 'i', 'n', + 'o', 'u', 'y', 'y', 'a', 'c', + 'd', 'e', 'e', 'g', 'h', 'i', + 'j', 'k', 'l', 'n', 'o', 'r', + 's', 't', 'u', 'u', 'w', 'y', + 'z', 'o', 'u', 'a', 'i', 'o', + 'u', 'u'|HIBIT, 'a'|HIBIT, 'g', 'k', 'o', + 'o'|HIBIT, 'j', 'g', 'n', 'a'|HIBIT, 'a', + 'e', 'i', 'o', 'r', 'u', 's', + 't', 'h', 'a', 'e', 'o'|HIBIT, 'o', + 'o'|HIBIT, 'y', '\0', '\0', '\0', '\0', + '\0', '\0', '\0', '\0', 'a', 'b', + 'c'|HIBIT, 'd', 'd', 'e'|HIBIT, 'e', 'e'|HIBIT, + 'f', 'g', 'h', 'h', 'i', 'i'|HIBIT, + 'k', 'l', 'l'|HIBIT, 'l', 'm', 'n', + 'o'|HIBIT, 'p', 'r', 'r'|HIBIT, 'r', 's', + 's'|HIBIT, 't', 'u', 'u'|HIBIT, 'v', 'w', + 'w', 'x', 'y', 'z', 'h', 't', + 'w', 'y', 'a', 'a'|HIBIT, 'a'|HIBIT, 'a'|HIBIT, + 'e', 'e'|HIBIT, 'e'|HIBIT, 'i', 'o', 'o'|HIBIT, + 'o'|HIBIT, 'o'|HIBIT, 'u', 'u'|HIBIT, 'u'|HIBIT, 'y', }; unsigned int key = (((unsigned int)c)<<3) | 0x00000007; @@ -201,7 +217,8 @@ static int remove_diacritic(int c){ } } assert( key>=aDia[iRes] ); - return ((c > (aDia[iRes]>>3) + (aDia[iRes]&0x07)) ? c : (int)aChar[iRes]); + if( bComplex==0 && (aChar[iRes] & 0x80) ) return c; + return (c > (aDia[iRes]>>3) + (aDia[iRes]&0x07)) ? c : ((int)aChar[iRes] & 0x7F); } @@ -214,8 +231,8 @@ int sqlite3FtsUnicodeIsdiacritic(int c){ unsigned int mask1 = 0x000361F8; if( c<768 || c>817 ) return 0; return (c < 768+32) ? - (mask0 & (1 << (c-768))) : - (mask1 & (1 << (c-768-32))); + (mask0 & ((unsigned int)1 << (c-768))) : + (mask1 & ((unsigned int)1 << (c-768-32))); } @@ -228,7 +245,7 @@ int sqlite3FtsUnicodeIsdiacritic(int c){ ** The results are undefined if the value passed to this function ** is less than zero. */ -int sqlite3FtsUnicodeFold(int c, int bRemoveDiacritic){ +int sqlite3FtsUnicodeFold(int c, int eRemoveDiacritic){ /* Each entry in the following array defines a rule for folding a range ** of codepoints to lower case. The rule applies to a range of nRange ** codepoints starting at codepoint iCode. @@ -322,16 +339,17 @@ int sqlite3FtsUnicodeFold(int c, int bRemoveDiacritic){ int ret = c; - assert( c>=0 ); assert( sizeof(unsigned short)==2 && sizeof(unsigned char)==1 ); if( c<128 ){ if( c>='A' && c<='Z' ) ret = c + ('a' - 'A'); }else if( c<65536 ){ + const struct TableEntry *p; int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1; int iLo = 0; int iRes = -1; + assert( c>aEntry[0].iCode ); while( iHi>=iLo ){ int iTest = (iHi + iLo) / 2; int cmp = (c - aEntry[iTest].iCode); @@ -342,17 +360,17 @@ int sqlite3FtsUnicodeFold(int c, int bRemoveDiacritic){ iHi = iTest-1; } } - assert( iRes<0 || c>=aEntry[iRes].iCode ); - if( iRes>=0 ){ - const struct TableEntry *p = &aEntry[iRes]; - if( c<(p->iCode + p->nRange) && 0==(0x01 & p->flags & (p->iCode ^ c)) ){ - ret = (c + (aiOff[p->flags>>1])) & 0x0000FFFF; - assert( ret>0 ); - } + assert( iRes>=0 && c>=aEntry[iRes].iCode ); + p = &aEntry[iRes]; + if( c<(p->iCode + p->nRange) && 0==(0x01 & p->flags & (p->iCode ^ c)) ){ + ret = (c + (aiOff[p->flags>>1])) & 0x0000FFFF; + assert( ret>0 ); } - if( bRemoveDiacritic ) ret = remove_diacritic(ret); + if( eRemoveDiacritic ){ + ret = remove_diacritic(ret, eRemoveDiacritic==2); + } } else if( c>=66560 && c<66600 ){ diff --git a/ext/fts3/fts3_write.c b/ext/fts3/fts3_write.c index d5a408222e..19dff31f00 100644 --- a/ext/fts3/fts3_write.c +++ b/ext/fts3/fts3_write.c @@ -23,7 +23,7 @@ #include #include #include - +#include #define FTS_MAX_APPENDABLE_HEIGHT 16 @@ -67,7 +67,7 @@ int test_fts3_node_chunk_threshold = (4*1024)*4; #endif /* -** The two values that may be meaningfully bound to the :1 parameter in +** The values that may be meaningfully bound to the :1 parameter in ** statements SQL_REPLACE_STAT and SQL_SELECT_STAT. */ #define FTS_STAT_DOCTOTAL 0 @@ -333,14 +333,17 @@ static int fts3SqlStmt( ** of the oldest level in the db that contains at least ? segments. Or, ** if no level in the FTS index contains more than ? segments, the statement ** returns zero rows. */ -/* 28 */ "SELECT level FROM %Q.'%q_segdir' GROUP BY level HAVING count(*)>=?" - " ORDER BY (level %% 1024) ASC LIMIT 1", +/* 28 */ "SELECT level, count(*) AS cnt FROM %Q.'%q_segdir' " + " GROUP BY level HAVING cnt>=?" + " ORDER BY (level %% 1024) ASC, 2 DESC LIMIT 1", /* Estimate the upper limit on the number of leaf nodes in a new segment ** created by merging the oldest :2 segments from absolute level :1. See ** function sqlite3Fts3Incrmerge() for details. */ /* 29 */ "SELECT 2 * total(1 + leaves_end_block - start_block) " - " FROM %Q.'%q_segdir' WHERE level = ? AND idx < ?", + " FROM (SELECT * FROM %Q.'%q_segdir' " + " WHERE level = ? ORDER BY idx ASC LIMIT ?" + " )", /* SQL_DELETE_SEGDIR_ENTRY ** Delete the %_segdir entry on absolute level :1 with index :2. */ @@ -395,10 +398,12 @@ static int fts3SqlStmt( pStmt = p->aStmt[eStmt]; if( !pStmt ){ + int f = SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_NO_VTAB; char *zSql; if( eStmt==SQL_CONTENT_INSERT ){ zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName, p->zWriteExprlist); }else if( eStmt==SQL_SELECT_CONTENT_BY_ROWID ){ + f &= ~SQLITE_PREPARE_NO_VTAB; zSql = sqlite3_mprintf(azSql[eStmt], p->zReadExprlist); }else{ zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName); @@ -406,7 +411,7 @@ static int fts3SqlStmt( if( !zSql ){ rc = SQLITE_NOMEM; }else{ - rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, NULL); + rc = sqlite3_prepare_v3(p->db, zSql, -1, f, &pStmt, NULL); sqlite3_free(zSql); assert( rc==SQLITE_OK || pStmt==0 ); p->aStmt[eStmt] = pStmt; @@ -564,7 +569,7 @@ static sqlite3_int64 getAbsoluteLevel( int iLevel /* Level of segments */ ){ sqlite3_int64 iBase; /* First absolute level for iLangid/iIndex */ - assert( iLangid>=0 ); + assert_fts3_nc( iLangid>=0 ); assert( p->nIndex>0 ); assert( iIndex>=0 && iIndexnIndex ); @@ -644,7 +649,7 @@ static int fts3PendingListAppendVarint( /* Allocate or grow the PendingList as required. */ if( !p ){ - p = sqlite3_malloc(sizeof(*p) + 100); + p = sqlite3_malloc64(sizeof(*p) + 100); if( !p ){ return SQLITE_NOMEM; } @@ -653,14 +658,14 @@ static int fts3PendingListAppendVarint( p->nData = 0; } else if( p->nData+FTS3_VARINT_MAX+1>p->nSpace ){ - int nNew = p->nSpace * 2; - p = sqlite3_realloc(p, sizeof(*p) + nNew); + i64 nNew = p->nSpace * 2; + p = sqlite3_realloc64(p, sizeof(*p) + nNew); if( !p ){ sqlite3_free(*pp); *pp = 0; return SQLITE_NOMEM; } - p->nSpace = nNew; + p->nSpace = (int)nNew; p->aData = (char *)&p[1]; } @@ -693,7 +698,7 @@ static int fts3PendingListAppend( assert( !p || p->iLastDocid<=iDocid ); if( !p || p->iLastDocid!=iDocid ){ - sqlite3_int64 iDelta = iDocid - (p ? p->iLastDocid : 0); + u64 iDelta = (u64)iDocid - (u64)(p ? p->iLastDocid : 0); if( p ){ assert( p->nDatanSpace ); assert( p->aData[p->nData]==0 ); @@ -1150,7 +1155,7 @@ static int fts3AllocateSegdirIdx( ** segment and allocate (newly freed) index 0 at level iLevel. Otherwise, ** if iNext is less than FTS3_MERGE_COUNT, allocate index iNext. */ - if( iNext>=FTS3_MERGE_COUNT ){ + if( iNext>=MergeCount(p) ){ fts3LogMerge(16, getAbsoluteLevel(p, iLangid, iIndex, iLevel)); rc = fts3SegmentMerge(p, iLangid, iIndex, iLevel); *piIdx = 0; @@ -1217,7 +1222,7 @@ int sqlite3Fts3ReadBlock( int nByte = sqlite3_blob_bytes(p->pSegments); *pnBlob = nByte; if( paBlob ){ - char *aByte = sqlite3_malloc(nByte + FTS3_NODE_PADDING); + char *aByte = sqlite3_malloc64((i64)nByte + FTS3_NODE_PADDING); if( !aByte ){ rc = SQLITE_NOMEM; }else{ @@ -1234,6 +1239,8 @@ int sqlite3Fts3ReadBlock( } *paBlob = aByte; } + }else if( rc==SQLITE_ERROR ){ + rc = FTS_CORRUPT_VTAB; } return rc; @@ -1328,9 +1335,19 @@ static int fts3SegReaderNext( char *aCopy; PendingList *pList = (PendingList *)fts3HashData(pElem); int nCopy = pList->nData+1; - pReader->zTerm = (char *)fts3HashKey(pElem); - pReader->nTerm = fts3HashKeysize(pElem); - aCopy = (char*)sqlite3_malloc(nCopy); + + int nTerm = fts3HashKeysize(pElem); + if( (nTerm+1)>pReader->nTermAlloc ){ + sqlite3_free(pReader->zTerm); + pReader->zTerm = (char*)sqlite3_malloc64(((i64)nTerm+1)*2); + if( !pReader->zTerm ) return SQLITE_NOMEM; + pReader->nTermAlloc = (nTerm+1)*2; + } + memcpy(pReader->zTerm, fts3HashKey(pElem), nTerm); + pReader->zTerm[nTerm] = '\0'; + pReader->nTerm = nTerm; + + aCopy = (char*)sqlite3_malloc64(nCopy); if( !aCopy ) return SQLITE_NOMEM; memcpy(aCopy, pList->aData, nCopy); pReader->nNode = pReader->nDoclist = nCopy; @@ -1345,7 +1362,9 @@ static int fts3SegReaderNext( /* If iCurrentBlock>=iLeafEndBlock, this is an EOF condition. All leaf ** blocks have already been traversed. */ - assert( pReader->iCurrentBlock<=pReader->iLeafEndBlock ); +#ifdef CORRUPT_DB + assert( pReader->iCurrentBlock<=pReader->iLeafEndBlock || CORRUPT_DB ); +#endif if( pReader->iCurrentBlock>=pReader->iLeafEndBlock ){ return SQLITE_OK; } @@ -1372,15 +1391,19 @@ static int fts3SegReaderNext( ** safe (no risk of overread) even if the node data is corrupted. */ pNext += fts3GetVarint32(pNext, &nPrefix); pNext += fts3GetVarint32(pNext, &nSuffix); - if( nPrefix<0 || nSuffix<=0 - || &pNext[nSuffix]>&pReader->aNode[pReader->nNode] + if( nSuffix<=0 + || (&pReader->aNode[pReader->nNode] - pNext)pReader->nTerm ){ return FTS_CORRUPT_VTAB; } - if( nPrefix+nSuffix>pReader->nTermAlloc ){ - int nNew = (nPrefix+nSuffix)*2; - char *zNew = sqlite3_realloc(pReader->zTerm, nNew); + /* Both nPrefix and nSuffix were read by fts3GetVarint32() and so are + ** between 0 and 0x7FFFFFFF. But the sum of the two may cause integer + ** overflow - hence the (i64) casts. */ + if( (i64)nPrefix+nSuffix>(i64)pReader->nTermAlloc ){ + i64 nNew = ((i64)nPrefix+nSuffix)*2; + char *zNew = sqlite3_realloc64(pReader->zTerm, nNew); if( !zNew ){ return SQLITE_NOMEM; } @@ -1402,8 +1425,9 @@ static int fts3SegReaderNext( ** b-tree node. And that the final byte of the doclist is 0x00. If either ** of these statements is untrue, then the data structure is corrupt. */ - if( &pReader->aDoclist[pReader->nDoclist]>&pReader->aNode[pReader->nNode] + if( pReader->nDoclist > pReader->nNode-(pReader->aDoclist-pReader->aNode) || (pReader->nPopulate==0 && pReader->aDoclist[pReader->nDoclist-1]) + || pReader->nDoclist==0 ){ return FTS_CORRUPT_VTAB; } @@ -1520,18 +1544,18 @@ static int fts3SegReaderNextDocid( }else{ rc = fts3SegReaderRequire(pReader, p, FTS3_VARINT_MAX); if( rc==SQLITE_OK ){ - sqlite3_int64 iDelta; - pReader->pOffsetList = p + sqlite3Fts3GetVarint(p, &iDelta); + u64 iDelta; + pReader->pOffsetList = p + sqlite3Fts3GetVarintU(p, &iDelta); if( pTab->bDescIdx ){ - pReader->iDocid -= iDelta; + pReader->iDocid = (i64)((u64)pReader->iDocid - iDelta); }else{ - pReader->iDocid += iDelta; + pReader->iDocid = (i64)((u64)pReader->iDocid + iDelta); } } } } - return SQLITE_OK; + return rc; } @@ -1575,9 +1599,7 @@ int sqlite3Fts3MsrOvfl( */ void sqlite3Fts3SegReaderFree(Fts3SegReader *pReader){ if( pReader ){ - if( !fts3SegReaderIsPending(pReader) ){ - sqlite3_free(pReader->zTerm); - } + sqlite3_free(pReader->zTerm); if( !fts3SegReaderIsRootOnly(pReader) ){ sqlite3_free(pReader->aNode); } @@ -1602,12 +1624,17 @@ int sqlite3Fts3SegReaderNew( Fts3SegReader *pReader; /* Newly allocated SegReader object */ int nExtra = 0; /* Bytes to allocate segment root node */ - assert( iStartLeaf<=iEndLeaf ); + assert( zRoot!=0 || nRoot==0 ); +#ifdef CORRUPT_DB + assert( zRoot!=0 || CORRUPT_DB ); +#endif + if( iStartLeaf==0 ){ + if( iEndLeaf!=0 ) return FTS_CORRUPT_VTAB; nExtra = nRoot + FTS3_NODE_PADDING; } - pReader = (Fts3SegReader *)sqlite3_malloc(sizeof(Fts3SegReader) + nExtra); + pReader = (Fts3SegReader *)sqlite3_malloc64(sizeof(Fts3SegReader) + nExtra); if( !pReader ){ return SQLITE_NOMEM; } @@ -1623,7 +1650,7 @@ int sqlite3Fts3SegReaderNew( pReader->aNode = (char *)&pReader[1]; pReader->rootOnly = 1; pReader->nNode = nRoot; - memcpy(pReader->aNode, zRoot, nRoot); + if( nRoot ) memcpy(pReader->aNode, zRoot, nRoot); memset(&pReader->aNode[nRoot], 0, FTS3_NODE_PADDING); }else{ pReader->iCurrentBlock = iStartLeaf-1; @@ -1699,7 +1726,7 @@ int sqlite3Fts3SegReaderPending( if( nElem==nAlloc ){ Fts3HashElem **aElem2; nAlloc += 16; - aElem2 = (Fts3HashElem **)sqlite3_realloc( + aElem2 = (Fts3HashElem **)sqlite3_realloc64( aElem, nAlloc*sizeof(Fts3HashElem *) ); if( !aElem2 ){ @@ -1738,8 +1765,9 @@ int sqlite3Fts3SegReaderPending( } if( nElem>0 ){ - int nByte = sizeof(Fts3SegReader) + (nElem+1)*sizeof(Fts3HashElem *); - pReader = (Fts3SegReader *)sqlite3_malloc(nByte); + sqlite3_int64 nByte; + nByte = sizeof(Fts3SegReader) + (nElem+1)*sizeof(Fts3HashElem *); + pReader = (Fts3SegReader *)sqlite3_malloc64(nByte); if( !pReader ){ rc = SQLITE_NOMEM; }else{ @@ -1787,7 +1815,7 @@ static int fts3SegReaderCmp(Fts3SegReader *pLhs, Fts3SegReader *pRhs){ if( rc==0 ){ rc = pRhs->iIdx - pLhs->iIdx; } - assert( rc!=0 ); + assert_fts3_nc( rc!=0 ); return rc; } @@ -1906,6 +1934,7 @@ static int fts3WriteSegment( sqlite3_bind_blob(pStmt, 2, z, n, SQLITE_STATIC); sqlite3_step(pStmt); rc = sqlite3_reset(pStmt); + sqlite3_bind_null(pStmt, 2); } return rc; } @@ -1962,6 +1991,7 @@ static int fts3WriteSegdir( sqlite3_bind_blob(pStmt, 6, zRoot, nRoot, SQLITE_STATIC); sqlite3_step(pStmt); rc = sqlite3_reset(pStmt); + sqlite3_bind_null(pStmt, 6); } return rc; } @@ -1981,8 +2011,8 @@ static int fts3PrefixCompress( int nNext /* Size of buffer zNext in bytes */ ){ int n; - UNUSED_PARAMETER(nNext); - for(n=0; nzTerm, pTree->nTerm, zTerm, nTerm); nSuffix = nTerm-nPrefix; + /* If nSuffix is zero or less, then zTerm/nTerm must be a prefix of + ** pWriter->zTerm/pWriter->nTerm. i.e. must be equal to or less than when + ** compared with BINARY collation. This indicates corruption. */ + if( nSuffix<=0 ) return FTS_CORRUPT_VTAB; + nReq += sqlite3Fts3VarintLen(nPrefix)+sqlite3Fts3VarintLen(nSuffix)+nSuffix; if( nReq<=p->nNodeSize || !pTree->zTerm ){ @@ -2025,7 +2060,7 @@ static int fts3NodeAddTerm( ** this is not expected to be a serious problem. */ assert( pTree->aData==(char *)&pTree[1] ); - pTree->aData = (char *)sqlite3_malloc(nReq); + pTree->aData = (char *)sqlite3_malloc64(nReq); if( !pTree->aData ){ return SQLITE_NOMEM; } @@ -2043,7 +2078,7 @@ static int fts3NodeAddTerm( if( isCopyTerm ){ if( pTree->nMalloczMalloc, nTerm*2); + char *zNew = sqlite3_realloc64(pTree->zMalloc, (i64)nTerm*2); if( !zNew ){ return SQLITE_NOMEM; } @@ -2069,7 +2104,7 @@ static int fts3NodeAddTerm( ** now. Instead, the term is inserted into the parent of pTree. If pTree ** has no parent, one is created here. */ - pNew = (SegmentNode *)sqlite3_malloc(sizeof(SegmentNode) + p->nNodeSize); + pNew = (SegmentNode *)sqlite3_malloc64(sizeof(SegmentNode) + p->nNodeSize); if( !pNew ){ return SQLITE_NOMEM; } @@ -2207,7 +2242,7 @@ static int fts3SegWriterAdd( ){ int nPrefix; /* Size of term prefix in bytes */ int nSuffix; /* Size of term suffix in bytes */ - int nReq; /* Number of bytes required on leaf page */ + i64 nReq; /* Number of bytes required on leaf page */ int nData; SegmentWriter *pWriter = *ppWriter; @@ -2216,13 +2251,13 @@ static int fts3SegWriterAdd( sqlite3_stmt *pStmt; /* Allocate the SegmentWriter structure */ - pWriter = (SegmentWriter *)sqlite3_malloc(sizeof(SegmentWriter)); + pWriter = (SegmentWriter *)sqlite3_malloc64(sizeof(SegmentWriter)); if( !pWriter ) return SQLITE_NOMEM; memset(pWriter, 0, sizeof(SegmentWriter)); *ppWriter = pWriter; /* Allocate a buffer in which to accumulate data */ - pWriter->aData = (char *)sqlite3_malloc(p->nNodeSize); + pWriter->aData = (char *)sqlite3_malloc64(p->nNodeSize); if( !pWriter->aData ) return SQLITE_NOMEM; pWriter->nSize = p->nNodeSize; @@ -2241,6 +2276,11 @@ static int fts3SegWriterAdd( nPrefix = fts3PrefixCompress(pWriter->zTerm, pWriter->nTerm, zTerm, nTerm); nSuffix = nTerm-nPrefix; + /* If nSuffix is zero or less, then zTerm/nTerm must be a prefix of + ** pWriter->zTerm/pWriter->nTerm. i.e. must be equal to or less than when + ** compared with BINARY collation. This indicates corruption. */ + if( nSuffix<=0 ) return FTS_CORRUPT_VTAB; + /* Figure out how many bytes are required by this new entry */ nReq = sqlite3Fts3VarintLen(nPrefix) + /* varint containing prefix size */ sqlite3Fts3VarintLen(nSuffix) + /* varint containing suffix size */ @@ -2252,6 +2292,7 @@ static int fts3SegWriterAdd( int rc; /* The current leaf node is full. Write it out to the database. */ + if( pWriter->iFree==LARGEST_INT64 ) return FTS_CORRUPT_VTAB; rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, nData); if( rc!=SQLITE_OK ) return rc; p->nLeafAdd++; @@ -2291,7 +2332,7 @@ static int fts3SegWriterAdd( ** the buffer to make it large enough. */ if( nReq>pWriter->nSize ){ - char *aNew = sqlite3_realloc(pWriter->aData, nReq); + char *aNew = sqlite3_realloc64(pWriter->aData, nReq); if( !aNew ) return SQLITE_NOMEM; pWriter->aData = aNew; pWriter->nSize = nReq; @@ -2301,9 +2342,11 @@ static int fts3SegWriterAdd( /* Append the prefix-compressed term and doclist to the buffer. */ nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nPrefix); nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nSuffix); + assert( nSuffix>0 ); memcpy(&pWriter->aData[nData], &zTerm[nPrefix], nSuffix); nData += nSuffix; nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nDoclist); + assert( nDoclist>0 ); memcpy(&pWriter->aData[nData], aDoclist, nDoclist); pWriter->nData = nData + nDoclist; @@ -2314,7 +2357,7 @@ static int fts3SegWriterAdd( */ if( isCopyTerm ){ if( nTerm>pWriter->nMalloc ){ - char *zNew = sqlite3_realloc(pWriter->zMalloc, nTerm*2); + char *zNew = sqlite3_realloc64(pWriter->zMalloc, (i64)nTerm*2); if( !zNew ){ return SQLITE_NOMEM; } @@ -2323,6 +2366,7 @@ static int fts3SegWriterAdd( pWriter->zTerm = zNew; } assert( pWriter->zTerm==pWriter->zMalloc ); + assert( nTerm>0 ); memcpy(pWriter->zTerm, zTerm, nTerm); }else{ pWriter->zTerm = (char *)zTerm; @@ -2469,7 +2513,7 @@ static int fts3SegmentIsMaxLevel(Fts3Table *p, i64 iAbsLevel, int *pbMax){ if( rc!=SQLITE_OK ) return rc; sqlite3_bind_int64(pStmt, 1, iAbsLevel+1); sqlite3_bind_int64(pStmt, 2, - ((iAbsLevel/FTS3_SEGDIR_MAXLEVEL)+1) * FTS3_SEGDIR_MAXLEVEL + (((u64)iAbsLevel/FTS3_SEGDIR_MAXLEVEL)+1) * FTS3_SEGDIR_MAXLEVEL ); *pbMax = 0; @@ -2597,14 +2641,14 @@ static void fts3ColumnFilter( nList -= (int)(p - pList); pList = p; - if( nList==0 ){ + if( nList<=0 ){ break; } p = &pList[1]; p += fts3GetVarint32(p, &iCurrent); } - if( bZero && &pList[nList]!=pEnd ){ + if( bZero && (pEnd - &pList[nList])>0){ memset(&pList[nList], 0, pEnd - &pList[nList]); } *ppList = pList; @@ -2621,17 +2665,20 @@ static void fts3ColumnFilter( static int fts3MsrBufferData( Fts3MultiSegReader *pMsr, /* Multi-segment-reader handle */ char *pList, - int nList + i64 nList ){ - if( nList>pMsr->nBuffer ){ + if( (nList+FTS3_NODE_PADDING)>pMsr->nBuffer ){ char *pNew; - pMsr->nBuffer = nList*2; - pNew = (char *)sqlite3_realloc(pMsr->aBuffer, pMsr->nBuffer); + int nNew = nList*2 + FTS3_NODE_PADDING; + pNew = (char *)sqlite3_realloc64(pMsr->aBuffer, nNew); if( !pNew ) return SQLITE_NOMEM; pMsr->aBuffer = pNew; + pMsr->nBuffer = nNew; } + assert( nList>0 ); memcpy(pMsr->aBuffer, pList, nList); + memset(&pMsr->aBuffer[nList], 0, FTS3_NODE_PADDING); return SQLITE_OK; } @@ -2681,7 +2728,7 @@ int sqlite3Fts3MsrIncrNext( fts3SegReaderSort(pMsr->apSegment, nMerge, j, xCmp); if( nList>0 && fts3SegReaderIsPending(apSegment[0]) ){ - rc = fts3MsrBufferData(pMsr, pList, nList+1); + rc = fts3MsrBufferData(pMsr, pList, (i64)nList+1); if( rc!=SQLITE_OK ) return rc; assert( (pMsr->aBuffer[nList] & 0xFE)==0x00 ); pList = pMsr->aBuffer; @@ -2818,6 +2865,19 @@ int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr){ return SQLITE_OK; } +static int fts3GrowSegReaderBuffer(Fts3MultiSegReader *pCsr, i64 nReq){ + if( nReq>pCsr->nBuffer ){ + char *aNew; + pCsr->nBuffer = nReq*2; + aNew = sqlite3_realloc64(pCsr->aBuffer, pCsr->nBuffer); + if( !aNew ){ + return SQLITE_NOMEM; + } + pCsr->aBuffer = aNew; + } + return SQLITE_OK; +} + int sqlite3Fts3SegReaderStep( Fts3Table *p, /* Virtual table handle */ @@ -2900,7 +2960,8 @@ int sqlite3Fts3SegReaderStep( ){ pCsr->nDoclist = apSegment[0]->nDoclist; if( fts3SegReaderIsPending(apSegment[0]) ){ - rc = fts3MsrBufferData(pCsr, apSegment[0]->aDoclist, pCsr->nDoclist); + rc = fts3MsrBufferData(pCsr, apSegment[0]->aDoclist, + (i64)pCsr->nDoclist); pCsr->aDoclist = pCsr->aBuffer; }else{ pCsr->aDoclist = apSegment[0]->aDoclist; @@ -2944,23 +3005,18 @@ int sqlite3Fts3SegReaderStep( ** doclist. */ sqlite3_int64 iDelta; if( p->bDescIdx && nDoclist>0 ){ - iDelta = iPrev - iDocid; + if( iPrev<=iDocid ) return FTS_CORRUPT_VTAB; + iDelta = (i64)((u64)iPrev - (u64)iDocid); }else{ - iDelta = iDocid - iPrev; + if( nDoclist>0 && iPrev>=iDocid ) return FTS_CORRUPT_VTAB; + iDelta = (i64)((u64)iDocid - (u64)iPrev); } - assert( iDelta>0 || (nDoclist==0 && iDelta==iDocid) ); - assert( nDoclist>0 || iDelta==iDocid ); nByte = sqlite3Fts3VarintLen(iDelta) + (isRequirePos?nList+1:0); - if( nDoclist+nByte>pCsr->nBuffer ){ - char *aNew; - pCsr->nBuffer = (nDoclist+nByte)*2; - aNew = sqlite3_realloc(pCsr->aBuffer, pCsr->nBuffer); - if( !aNew ){ - return SQLITE_NOMEM; - } - pCsr->aBuffer = aNew; - } + + rc = fts3GrowSegReaderBuffer(pCsr, + (i64)nByte+nDoclist+FTS3_NODE_PADDING); + if( rc ) return rc; if( isFirst ){ char *a = &pCsr->aBuffer[nDoclist]; @@ -2985,6 +3041,9 @@ int sqlite3Fts3SegReaderStep( fts3SegReaderSort(apSegment, nMerge, j, xCmp); } if( nDoclist>0 ){ + rc = fts3GrowSegReaderBuffer(pCsr, (i64)nDoclist+FTS3_NODE_PADDING); + if( rc ) return rc; + memset(&pCsr->aBuffer[nDoclist], 0, FTS3_NODE_PADDING); pCsr->aDoclist = pCsr->aBuffer; pCsr->nDoclist = nDoclist; rc = SQLITE_ROW; @@ -3034,11 +3093,11 @@ static void fts3ReadEndBlockField( if( zText ){ int i; int iMul = 1; - i64 iVal = 0; + u64 iVal = 0; for(i=0; zText[i]>='0' && zText[i]<='9'; i++){ iVal = iVal*10 + (zText[i] - '0'); } - *piEndBlock = iVal; + *piEndBlock = (i64)iVal; while( zText[i]==' ' ) i++; iVal = 0; if( zText[i]=='-' ){ @@ -3048,7 +3107,7 @@ static void fts3ReadEndBlockField( for(/* no-op */; zText[i]>='0' && zText[i]<='9'; i++){ iVal = iVal*10 + (zText[i] - '0'); } - *pnByte = (iVal * (i64)iMul); + *pnByte = ((i64)iVal * (i64)iMul); } } @@ -3194,7 +3253,7 @@ static int fts3SegmentMerge( ** segment. The level of the new segment is equal to the numerically ** greatest segment level currently present in the database for this ** index. The idx of the new segment is always 0. */ - if( csr.nSegment==1 ){ + if( csr.nSegment==1 && 0==fts3SegReaderIsPending(csr.apSegment[0]) ){ rc = SQLITE_DONE; goto finished; } @@ -3214,8 +3273,10 @@ static int fts3SegmentMerge( if( rc!=SQLITE_OK ) goto finished; assert( csr.nSegment>0 ); - assert( iNewLevel>=getAbsoluteLevel(p, iLangid, iIndex, 0) ); - assert( iNewLevel=getAbsoluteLevel(p, iLangid, iIndex, 0) ); + assert_fts3_nc( + iNewLeveliPrevLangid, i, FTS3_SEGCURSOR_PENDING); if( rc==SQLITE_DONE ) rc = SQLITE_OK; } - sqlite3Fts3PendingTermsClear(p); /* Determine the auto-incr-merge setting if unknown. If enabled, ** estimate the number of leaf blocks of content to be written @@ -3286,6 +3346,10 @@ int sqlite3Fts3PendingTermsFlush(Fts3Table *p){ rc = sqlite3_reset(pStmt); } } + + if( rc==SQLITE_OK ){ + sqlite3Fts3PendingTermsClear(p); + } return rc; } @@ -3314,14 +3378,16 @@ static void fts3DecodeIntArray( const char *zBuf, /* The BLOB containing the varints */ int nBuf /* size of the BLOB */ ){ - int i, j; - UNUSED_PARAMETER(nBuf); - for(i=j=0; inColumn ); + pBlob = sqlite3_malloc64( 10*(sqlite3_int64)p->nColumn ); if( pBlob==0 ){ *pRC = SQLITE_NOMEM; return; @@ -3390,7 +3456,7 @@ static void fts3UpdateDocTotals( const int nStat = p->nColumn+2; if( *pRC ) return; - a = sqlite3_malloc( (sizeof(u32)+10)*nStat ); + a = sqlite3_malloc64( (sizeof(u32)+10)*(sqlite3_int64)nStat ); if( a==0 ){ *pRC = SQLITE_NOMEM; return; @@ -3441,6 +3507,7 @@ static void fts3UpdateDocTotals( sqlite3_bind_blob(pStmt, 2, pBlob, nBlob, SQLITE_STATIC); sqlite3_step(pStmt); *pRC = sqlite3_reset(pStmt); + sqlite3_bind_null(pStmt, 2); sqlite3_free(a); } @@ -3453,7 +3520,10 @@ static int fts3DoOptimize(Fts3Table *p, int bReturnDone){ int rc; sqlite3_stmt *pAllLangid = 0; - rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID, &pAllLangid, 0); + rc = sqlite3Fts3PendingTermsFlush(p); + if( rc==SQLITE_OK ){ + rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID, &pAllLangid, 0); + } if( rc==SQLITE_OK ){ int rc2; sqlite3_bind_int(pAllLangid, 1, p->iPrevLangid); @@ -3474,7 +3544,6 @@ static int fts3DoOptimize(Fts3Table *p, int bReturnDone){ } sqlite3Fts3SegmentsClose(p); - sqlite3Fts3PendingTermsClear(p); return (rc==SQLITE_OK && bReturnDone && bSeenDone) ? SQLITE_DONE : rc; } @@ -3510,8 +3579,8 @@ static int fts3DoRebuild(Fts3Table *p){ } if( rc==SQLITE_OK ){ - int nByte = sizeof(u32) * (p->nColumn+1)*3; - aSz = (u32 *)sqlite3_malloc(nByte); + sqlite3_int64 nByte = sizeof(u32) * ((sqlite3_int64)p->nColumn+1)*3; + aSz = (u32 *)sqlite3_malloc64(nByte); if( aSz==0 ){ rc = SQLITE_NOMEM; }else{ @@ -3577,12 +3646,12 @@ static int fts3IncrmergeCsr( ){ int rc; /* Return Code */ sqlite3_stmt *pStmt = 0; /* Statement used to read %_segdir entry */ - int nByte; /* Bytes allocated at pCsr->apSegment[] */ + sqlite3_int64 nByte; /* Bytes allocated at pCsr->apSegment[] */ /* Allocate space for the Fts3MultiSegReader.aCsr[] array */ memset(pCsr, 0, sizeof(*pCsr)); nByte = sizeof(Fts3SegReader *) * nSeg; - pCsr->apSegment = (Fts3SegReader **)sqlite3_malloc(nByte); + pCsr->apSegment = (Fts3SegReader **)sqlite3_malloc64(nByte); if( pCsr->apSegment==0 ){ rc = SQLITE_NOMEM; @@ -3645,8 +3714,8 @@ struct NodeWriter { ** to an appendable b-tree segment. */ struct IncrmergeWriter { - int nLeafEst; /* Space allocated for leaf blocks */ - int nWork; /* Number of leaf pages flushed */ + i64 nLeafEst; /* Space allocated for leaf blocks */ + i64 nWork; /* Number of leaf pages flushed */ sqlite3_int64 iAbsLevel; /* Absolute level of input segments */ int iIdx; /* Index of *output* segment in iAbsLevel+1 */ sqlite3_int64 iStart; /* Block number of first allocated block */ @@ -3688,7 +3757,7 @@ struct NodeReader { static void blobGrowBuffer(Blob *pBlob, int nMin, int *pRc){ if( *pRc==SQLITE_OK && nMin>pBlob->nAlloc ){ int nAlloc = nMin; - char *a = (char *)sqlite3_realloc(pBlob->a, nAlloc); + char *a = (char *)sqlite3_realloc64(pBlob->a, nAlloc); if( a ){ pBlob->nAlloc = nAlloc; pBlob->a = a; @@ -3725,21 +3794,26 @@ static int nodeReaderNext(NodeReader *p){ } p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &nSuffix); + if( nPrefix>p->term.n || nSuffix>p->nNode-p->iOff || nSuffix==0 ){ + return FTS_CORRUPT_VTAB; + } blobGrowBuffer(&p->term, nPrefix+nSuffix, &rc); - if( rc==SQLITE_OK ){ + if( rc==SQLITE_OK && ALWAYS(p->term.a!=0) ){ memcpy(&p->term.a[nPrefix], &p->aNode[p->iOff], nSuffix); p->term.n = nPrefix+nSuffix; p->iOff += nSuffix; if( p->iChild==0 ){ p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &p->nDoclist); + if( (p->nNode-p->iOff)nDoclist ){ + return FTS_CORRUPT_VTAB; + } p->aDoclist = &p->aNode[p->iOff]; p->iOff += p->nDoclist; } } } - assert( p->iOff<=p->nNode ); - + assert_fts3_nc( p->iOff<=p->nNode ); return rc; } @@ -3763,14 +3837,14 @@ static int nodeReaderInit(NodeReader *p, const char *aNode, int nNode){ p->nNode = nNode; /* Figure out if this is a leaf or an internal node. */ - if( p->aNode[0] ){ + if( aNode && aNode[0] ){ /* An internal node. */ p->iOff = 1 + sqlite3Fts3GetVarint(&p->aNode[1], &p->iChild); }else{ p->iOff = 1; } - return nodeReaderNext(p); + return aNode ? nodeReaderNext(p) : SQLITE_OK; } /* @@ -3807,6 +3881,7 @@ static int fts3IncrmergePush( ** be added to. */ nPrefix = fts3PrefixCompress(pNode->key.a, pNode->key.n, zTerm, nTerm); nSuffix = nTerm - nPrefix; + if(nSuffix<=0 ) return FTS_CORRUPT_VTAB; nSpace = sqlite3Fts3VarintLen(nPrefix); nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix; @@ -3831,6 +3906,8 @@ static int fts3IncrmergePush( pBlk->n += sqlite3Fts3PutVarint(&pBlk->a[pBlk->n], nPrefix); } pBlk->n += sqlite3Fts3PutVarint(&pBlk->a[pBlk->n], nSuffix); + assert( nPrefix+nSuffix<=nTerm ); + assert( nPrefix>=0 ); memcpy(&pBlk->a[pBlk->n], &zTerm[nPrefix], nSuffix); pBlk->n += nSuffix; @@ -3879,7 +3956,7 @@ static int fts3IncrmergePush( ** ** It is assumed that the buffer associated with pNode is already large ** enough to accommodate the new entry. The buffer associated with pPrev -** is extended by this function if requrired. +** is extended by this function if required. ** ** If an error (i.e. OOM condition) occurs, an SQLite error code is ** returned. Otherwise, SQLITE_OK. @@ -3900,13 +3977,16 @@ static int fts3AppendToNode( /* Node must have already been started. There must be a doclist for a ** leaf node, and there must not be a doclist for an internal node. */ assert( pNode->n>0 ); - assert( (pNode->a[0]=='\0')==(aDoclist!=0) ); + assert_fts3_nc( (pNode->a[0]=='\0')==(aDoclist!=0) ); blobGrowBuffer(pPrev, nTerm, &rc); if( rc!=SQLITE_OK ) return rc; + assert( pPrev!=0 ); + assert( pPrev->a!=0 ); nPrefix = fts3PrefixCompress(pPrev->a, pPrev->n, zTerm, nTerm); nSuffix = nTerm - nPrefix; + if( nSuffix<=0 ) return FTS_CORRUPT_VTAB; memcpy(pPrev->a, zTerm, nTerm); pPrev->n = nTerm; @@ -3952,15 +4032,20 @@ static int fts3IncrmergeAppend( pLeaf = &pWriter->aNodeWriter[0]; nPrefix = fts3PrefixCompress(pLeaf->key.a, pLeaf->key.n, zTerm, nTerm); nSuffix = nTerm - nPrefix; + if(nSuffix<=0 ) return FTS_CORRUPT_VTAB; nSpace = sqlite3Fts3VarintLen(nPrefix); nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix; nSpace += sqlite3Fts3VarintLen(nDoclist) + nDoclist; /* If the current block is not empty, and if adding this term/doclist - ** to the current block would make it larger than Fts3Table.nNodeSize - ** bytes, write this block out to the database. */ - if( pLeaf->block.n>0 && (pLeaf->block.n + nSpace)>p->nNodeSize ){ + ** to the current block would make it larger than Fts3Table.nNodeSize bytes, + ** and if there is still room for another leaf page, write this block out to + ** the database. */ + if( pLeaf->block.n>0 + && (pLeaf->block.n + nSpace)>p->nNodeSize + && pLeaf->iBlock < (pWriter->iStart + pWriter->nLeafEst) + ){ rc = fts3WriteSegment(p, pLeaf->iBlock, pLeaf->block.a, pLeaf->block.n); pWriter->nWork++; @@ -4116,7 +4201,11 @@ static int fts3TermCmp( int nCmp = MIN(nLhs, nRhs); int res; - res = memcmp(zLhs, zRhs, nCmp); + if( nCmp && ALWAYS(zLhs) && ALWAYS(zRhs) ){ + res = memcmp(zLhs, zRhs, nCmp); + }else{ + res = 0; + } if( res==0 ) res = nLhs - nRhs; return res; @@ -4200,6 +4289,10 @@ static int fts3IncrmergeLoad( pWriter->bNoLeafData = (pWriter->nLeafData==0); nRoot = sqlite3_column_bytes(pSelect, 4); aRoot = sqlite3_column_blob(pSelect, 4); + if( aRoot==0 ){ + sqlite3_reset(pSelect); + return nRoot ? SQLITE_NOMEM : FTS_CORRUPT_VTAB; + } }else{ return sqlite3_reset(pSelect); } @@ -4235,6 +4328,10 @@ static int fts3IncrmergeLoad( int i; int nHeight = (int)aRoot[0]; NodeWriter *pNode; + if( nHeight<1 || nHeight>=FTS_MAX_APPENDABLE_HEIGHT ){ + sqlite3_reset(pSelect); + return FTS_CORRUPT_VTAB; + } pWriter->nLeafEst = (int)((iEnd - iStart) + 1)/FTS_MAX_APPENDABLE_HEIGHT; pWriter->iStart = iStart; @@ -4248,34 +4345,46 @@ static int fts3IncrmergeLoad( pNode = &pWriter->aNodeWriter[nHeight]; pNode->iBlock = pWriter->iStart + pWriter->nLeafEst*nHeight; - blobGrowBuffer(&pNode->block, MAX(nRoot, p->nNodeSize), &rc); + blobGrowBuffer(&pNode->block, + MAX(nRoot, p->nNodeSize)+FTS3_NODE_PADDING, &rc + ); if( rc==SQLITE_OK ){ memcpy(pNode->block.a, aRoot, nRoot); pNode->block.n = nRoot; + memset(&pNode->block.a[nRoot], 0, FTS3_NODE_PADDING); } for(i=nHeight; i>=0 && rc==SQLITE_OK; i--){ NodeReader reader; + memset(&reader, 0, sizeof(reader)); pNode = &pWriter->aNodeWriter[i]; - rc = nodeReaderInit(&reader, pNode->block.a, pNode->block.n); - while( reader.aNode && rc==SQLITE_OK ) rc = nodeReaderNext(&reader); - blobGrowBuffer(&pNode->key, reader.term.n, &rc); - if( rc==SQLITE_OK ){ - memcpy(pNode->key.a, reader.term.a, reader.term.n); - pNode->key.n = reader.term.n; - if( i>0 ){ - char *aBlock = 0; - int nBlock = 0; - pNode = &pWriter->aNodeWriter[i-1]; - pNode->iBlock = reader.iChild; - rc = sqlite3Fts3ReadBlock(p, reader.iChild, &aBlock, &nBlock, 0); - blobGrowBuffer(&pNode->block, MAX(nBlock, p->nNodeSize), &rc); - if( rc==SQLITE_OK ){ - memcpy(pNode->block.a, aBlock, nBlock); - pNode->block.n = nBlock; + if( pNode->block.a){ + rc = nodeReaderInit(&reader, pNode->block.a, pNode->block.n); + while( reader.aNode && rc==SQLITE_OK ) rc = nodeReaderNext(&reader); + blobGrowBuffer(&pNode->key, reader.term.n, &rc); + if( rc==SQLITE_OK ){ + assert_fts3_nc( reader.term.n>0 || reader.aNode==0 ); + if( reader.term.n>0 ){ + memcpy(pNode->key.a, reader.term.a, reader.term.n); + } + pNode->key.n = reader.term.n; + if( i>0 ){ + char *aBlock = 0; + int nBlock = 0; + pNode = &pWriter->aNodeWriter[i-1]; + pNode->iBlock = reader.iChild; + rc = sqlite3Fts3ReadBlock(p, reader.iChild, &aBlock, &nBlock,0); + blobGrowBuffer(&pNode->block, + MAX(nBlock, p->nNodeSize)+FTS3_NODE_PADDING, &rc + ); + if( rc==SQLITE_OK ){ + memcpy(pNode->block.a, aBlock, nBlock); + pNode->block.n = nBlock; + memset(&pNode->block.a[nBlock], 0, FTS3_NODE_PADDING); + } + sqlite3_free(aBlock); } - sqlite3_free(aBlock); } } nodeReaderRelease(&reader); @@ -4352,7 +4461,7 @@ static int fts3IncrmergeWriter( ){ int rc; /* Return Code */ int i; /* Iterator variable */ - int nLeafEst = 0; /* Blocks allocated for leaf nodes */ + i64 nLeafEst = 0; /* Blocks allocated for leaf nodes */ sqlite3_stmt *pLeafEst = 0; /* SQL used to determine nLeafEst */ sqlite3_stmt *pFirstBlock = 0; /* SQL used to determine first block */ @@ -4362,7 +4471,7 @@ static int fts3IncrmergeWriter( sqlite3_bind_int64(pLeafEst, 1, iAbsLevel); sqlite3_bind_int64(pLeafEst, 2, pCsr->nSegment); if( SQLITE_ROW==sqlite3_step(pLeafEst) ){ - nLeafEst = sqlite3_column_int(pLeafEst, 0); + nLeafEst = sqlite3_column_int64(pLeafEst, 0); } rc = sqlite3_reset(pLeafEst); } @@ -4452,7 +4561,7 @@ static int fts3RepackSegdirLevel( if( nIdx>=nAlloc ){ int *aNew; nAlloc += 16; - aNew = sqlite3_realloc(aIdx, nAlloc*sizeof(int)); + aNew = sqlite3_realloc64(aIdx, nAlloc*sizeof(int)); if( !aNew ){ rc = SQLITE_NOMEM; break; @@ -4518,7 +4627,10 @@ static int fts3TruncateNode( NodeReader reader; /* Reader object */ Blob prev = {0, 0, 0}; /* Previous term written to new node */ int rc = SQLITE_OK; /* Return code */ - int bLeaf = aNode[0]=='\0'; /* True for a leaf node */ + int bLeaf; /* True for a leaf node */ + + if( nNode<1 ) return FTS_CORRUPT_VTAB; + bLeaf = aNode[0]=='\0'; /* Allocate required output space */ blobGrowBuffer(pNew, nNode, &rc); @@ -4629,6 +4741,7 @@ static int fts3TruncateSegment( sqlite3_bind_int(pChomp, 4, iIdx); sqlite3_step(pChomp); rc = sqlite3_reset(pChomp); + sqlite3_bind_null(pChomp, 2); } } @@ -4708,6 +4821,7 @@ static int fts3IncrmergeHintStore(Fts3Table *p, Blob *pHint){ sqlite3_bind_blob(pReplace, 2, pHint->a, pHint->n, SQLITE_STATIC); sqlite3_step(pReplace); rc = sqlite3_reset(pReplace); + sqlite3_bind_null(pReplace, 2); } return rc; @@ -4736,7 +4850,7 @@ static int fts3IncrmergeHintLoad(Fts3Table *p, Blob *pHint){ if( aHint ){ blobGrowBuffer(pHint, nHint, &rc); if( rc==SQLITE_OK ){ - memcpy(pHint->a, aHint, nHint); + if( ALWAYS(pHint->a!=0) ) memcpy(pHint->a, aHint, nHint); pHint->n = nHint; } } @@ -4782,13 +4896,17 @@ static int fts3IncrmergeHintPop(Blob *pHint, i64 *piAbsLevel, int *pnInput){ const int nHint = pHint->n; int i; - i = pHint->n-2; + i = pHint->n-1; + if( (pHint->a[i] & 0x80) ) return FTS_CORRUPT_VTAB; while( i>0 && (pHint->a[i-1] & 0x80) ) i--; + if( i==0 ) return FTS_CORRUPT_VTAB; + i--; while( i>0 && (pHint->a[i-1] & 0x80) ) i--; pHint->n = i; i += sqlite3Fts3GetVarint(&pHint->a[i], piAbsLevel); i += fts3GetVarint32(&pHint->a[i], pnInput); + assert( i<=nHint ); if( i!=nHint ) return FTS_CORRUPT_VTAB; return SQLITE_OK; @@ -4817,7 +4935,7 @@ int sqlite3Fts3Incrmerge(Fts3Table *p, int nMerge, int nMin){ /* Allocate space for the cursor, filter and writer objects */ const int nAlloc = sizeof(*pCsr) + sizeof(*pFilter) + sizeof(*pWriter); - pWriter = (IncrmergeWriter *)sqlite3_malloc(nAlloc); + pWriter = (IncrmergeWriter *)sqlite3_malloc64(nAlloc); if( !pWriter ) return SQLITE_NOMEM; pFilter = (Fts3SegFilter *)&pWriter[1]; pCsr = (Fts3MultiSegReader *)&pFilter[1]; @@ -4836,10 +4954,11 @@ int sqlite3Fts3Incrmerge(Fts3Table *p, int nMerge, int nMin){ ** set nSeg to -1. */ rc = fts3SqlStmt(p, SQL_FIND_MERGE_LEVEL, &pFindLevel, 0); - sqlite3_bind_int(pFindLevel, 1, nMin); + sqlite3_bind_int(pFindLevel, 1, MAX(2, nMin)); if( sqlite3_step(pFindLevel)==SQLITE_ROW ){ iAbsLevel = sqlite3_column_int64(pFindLevel, 0); - nSeg = nMin; + nSeg = sqlite3_column_int(pFindLevel, 1); + assert( nSeg>=2 ); }else{ nSeg = -1; } @@ -4857,8 +4976,14 @@ int sqlite3Fts3Incrmerge(Fts3Table *p, int nMerge, int nMin){ rc = fts3IncrmergeHintPop(&hint, &iHintAbsLevel, &nHintSeg); if( nSeg<0 || (iAbsLevel % nMod) >= (iHintAbsLevel % nMod) ){ + /* Based on the scan in the block above, it is known that there + ** are no levels with a relative level smaller than that of + ** iAbsLevel with more than nSeg segments, or if nSeg is -1, + ** no levels with more than nMin segments. Use this to limit the + ** value of nHintSeg to avoid a large memory allocation in case the + ** merge-hint is corrupt*/ iAbsLevel = iHintAbsLevel; - nSeg = nHintSeg; + nSeg = MIN(MAX(nMin,nSeg), nHintSeg); bUseHint = 1; bDirtyHint = 1; }else{ @@ -4871,7 +4996,13 @@ int sqlite3Fts3Incrmerge(Fts3Table *p, int nMerge, int nMin){ /* If nSeg is less that zero, then there is no level with at least ** nMin segments and no hint in the %_stat table. No work to do. ** Exit early in this case. */ - if( nSeg<0 ) break; + if( nSeg<=0 ) break; + + assert( nMod<=0x7FFFFFFF ); + if( iAbsLevel<0 || iAbsLevel>(nMod<<32) ){ + rc = FTS_CORRUPT_VTAB; + break; + } /* Open a cursor to iterate through the contents of the oldest nSeg ** indexes of absolute level iAbsLevel. If this cursor is opened using @@ -4899,8 +5030,15 @@ int sqlite3Fts3Incrmerge(Fts3Table *p, int nMerge, int nMin){ } if( SQLITE_OK==rc && pCsr->nSegment==nSeg && SQLITE_OK==(rc = sqlite3Fts3SegReaderStart(p, pCsr, pFilter)) - && SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, pCsr)) ){ + int bEmpty = 0; + rc = sqlite3Fts3SegReaderStep(p, pCsr); + if( rc==SQLITE_OK ){ + bEmpty = 1; + }else if( rc!=SQLITE_ROW ){ + sqlite3Fts3SegReaderFinish(pCsr); + break; + } if( bUseHint && iIdx>0 ){ const char *zKey = pCsr->zTerm; int nKey = pCsr->nTerm; @@ -4911,11 +5049,13 @@ int sqlite3Fts3Incrmerge(Fts3Table *p, int nMerge, int nMin){ if( rc==SQLITE_OK && pWriter->nLeafEst ){ fts3LogMerge(nSeg, iAbsLevel); - do { - rc = fts3IncrmergeAppend(p, pWriter, pCsr); - if( rc==SQLITE_OK ) rc = sqlite3Fts3SegReaderStep(p, pCsr); - if( pWriter->nWork>=nRem && rc==SQLITE_ROW ) rc = SQLITE_OK; - }while( rc==SQLITE_ROW ); + if( bEmpty==0 ){ + do { + rc = fts3IncrmergeAppend(p, pWriter, pCsr); + if( rc==SQLITE_OK ) rc = sqlite3Fts3SegReaderStep(p, pCsr); + if( pWriter->nWork>=nRem && rc==SQLITE_ROW ) rc = SQLITE_OK; + }while( rc==SQLITE_ROW ); + } /* Update or delete the input segments */ if( rc==SQLITE_OK ){ @@ -4954,11 +5094,14 @@ int sqlite3Fts3Incrmerge(Fts3Table *p, int nMerge, int nMin){ ** Convert the text beginning at *pz into an integer and return ** its value. Advance *pz to point to the first character past ** the integer. +** +** This function used for parameters to merge= and incrmerge= +** commands. */ static int fts3Getint(const char **pz){ const char *z = *pz; int i = 0; - while( (*z)>='0' && (*z)<='9' ) i = 10*i + *(z++) - '0'; + while( (*z)>='0' && (*z)<='9' && i<214748363 ) i = 10*i + *(z++) - '0'; *pz = z; return i; } @@ -4977,7 +5120,7 @@ static int fts3DoIncrmerge( const char *zParam /* Nul-terminated string containing "A,B" */ ){ int rc; - int nMin = (FTS3_MERGE_COUNT / 2); + int nMin = (MergeCount(p) / 2); int nMerge = 0; const char *z = zParam; @@ -5022,7 +5165,7 @@ static int fts3DoAutoincrmerge( int rc = SQLITE_OK; sqlite3_stmt *pStmt = 0; p->nAutoincrmerge = fts3Getint(&zParam); - if( p->nAutoincrmerge==1 || p->nAutoincrmerge>FTS3_MERGE_COUNT ){ + if( p->nAutoincrmerge==1 || p->nAutoincrmerge>MergeCount(p) ){ p->nAutoincrmerge = 8; } if( !p->bHasStat ){ @@ -5084,7 +5227,7 @@ static u64 fts3ChecksumIndex( int rc; u64 cksum = 0; - assert( *pRc==SQLITE_OK ); + if( *pRc ) return 0; memset(&filter, 0, sizeof(filter)); memset(&csr, 0, sizeof(csr)); @@ -5105,12 +5248,12 @@ static u64 fts3ChecksumIndex( i64 iDocid = 0; i64 iCol = 0; - i64 iPos = 0; + u64 iPos = 0; pCsr += sqlite3Fts3GetVarint(pCsr, &iDocid); while( pCsrbDescIdx ){ + iDocid = (i64)((u64)iDocid - iVal); + }else{ + iDocid = (i64)((u64)iDocid + iVal); + } } }else{ iPos += (iVal - 2); @@ -5147,7 +5294,7 @@ static u64 fts3ChecksumIndex( ** If an error occurs (e.g. an OOM or IO error), return an SQLite error ** code. The final value of *pbOk is undefined in this case. */ -static int fts3IntegrityCheck(Fts3Table *p, int *pbOk){ +int sqlite3Fts3IntegrityCheck(Fts3Table *p, int *pbOk){ int rc = SQLITE_OK; /* Return code */ u64 cksum1 = 0; /* Checksum based on FTS index contents */ u64 cksum2 = 0; /* Checksum based on %_content contents */ @@ -5192,10 +5339,9 @@ static int fts3IntegrityCheck(Fts3Table *p, int *pbOk){ for(iCol=0; rc==SQLITE_OK && iColnColumn; iCol++){ if( p->abNotindexed[iCol]==0 ){ const char *zText = (const char *)sqlite3_column_text(pStmt, iCol+1); - int nText = sqlite3_column_bytes(pStmt, iCol+1); sqlite3_tokenizer_cursor *pT = 0; - rc = sqlite3Fts3OpenTokenizer(p->pTokenizer, iLang, zText, nText,&pT); + rc = sqlite3Fts3OpenTokenizer(p->pTokenizer, iLang, zText, -1, &pT); while( rc==SQLITE_OK ){ char const *zToken; /* Buffer containing token */ int nToken = 0; /* Number of bytes in token */ @@ -5226,7 +5372,12 @@ static int fts3IntegrityCheck(Fts3Table *p, int *pbOk){ sqlite3_finalize(pStmt); } - *pbOk = (cksum1==cksum2); + if( rc==SQLITE_CORRUPT_VTAB ){ + rc = SQLITE_OK; + *pbOk = 0; + }else{ + *pbOk = (rc==SQLITE_OK && cksum1==cksum2); + } return rc; } @@ -5266,7 +5417,7 @@ static int fts3DoIntegrityCheck( ){ int rc; int bOk = 0; - rc = fts3IntegrityCheck(p, &bOk); + rc = sqlite3Fts3IntegrityCheck(p, &bOk); if( rc==SQLITE_OK && bOk==0 ) rc = FTS_CORRUPT_VTAB; return rc; } @@ -5280,7 +5431,7 @@ static int fts3DoIntegrityCheck( ** meaningful value to insert is the text 'optimize'. */ static int fts3SpecialInsert(Fts3Table *p, sqlite3_value *pVal){ - int rc; /* Return Code */ + int rc = SQLITE_ERROR; /* Return Code */ const char *zVal = (const char *)sqlite3_value_text(pVal); int nVal = sqlite3_value_bytes(pVal); @@ -5296,21 +5447,30 @@ static int fts3SpecialInsert(Fts3Table *p, sqlite3_value *pVal){ rc = fts3DoIncrmerge(p, &zVal[6]); }else if( nVal>10 && 0==sqlite3_strnicmp(zVal, "automerge=", 10) ){ rc = fts3DoAutoincrmerge(p, &zVal[10]); -#ifdef SQLITE_TEST - }else if( nVal>9 && 0==sqlite3_strnicmp(zVal, "nodesize=", 9) ){ - p->nNodeSize = atoi(&zVal[9]); - rc = SQLITE_OK; - }else if( nVal>11 && 0==sqlite3_strnicmp(zVal, "maxpending=", 9) ){ - p->nMaxPendingData = atoi(&zVal[11]); - rc = SQLITE_OK; - }else if( nVal>21 && 0==sqlite3_strnicmp(zVal, "test-no-incr-doclist=", 21) ){ - p->bNoIncrDoclist = atoi(&zVal[21]); - rc = SQLITE_OK; -#endif - }else{ - rc = SQLITE_ERROR; + }else if( nVal==5 && 0==sqlite3_strnicmp(zVal, "flush", 5) ){ + rc = sqlite3Fts3PendingTermsFlush(p); + } +#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST) + else{ + int v; + if( nVal>9 && 0==sqlite3_strnicmp(zVal, "nodesize=", 9) ){ + v = atoi(&zVal[9]); + if( v>=24 && v<=p->nPgsz-35 ) p->nNodeSize = v; + rc = SQLITE_OK; + }else if( nVal>11 && 0==sqlite3_strnicmp(zVal, "maxpending=", 9) ){ + v = atoi(&zVal[11]); + if( v>=64 && v<=FTS3_MAX_PENDING_DATA ) p->nMaxPendingData = v; + rc = SQLITE_OK; + }else if( nVal>21 && 0==sqlite3_strnicmp(zVal,"test-no-incr-doclist=",21) ){ + p->bNoIncrDoclist = atoi(&zVal[21]); + rc = SQLITE_OK; + }else if( nVal>11 && 0==sqlite3_strnicmp(zVal,"mergecount=",11) ){ + v = atoi(&zVal[11]); + if( v>=4 && v<=FTS3_MERGE_COUNT && (v&1)==0 ) p->nMergeCount = v; + rc = SQLITE_OK; + } } - +#endif return rc; } @@ -5419,7 +5579,7 @@ int sqlite3Fts3DeferredTokenList( return SQLITE_OK; } - pRet = (char *)sqlite3_malloc(p->pList->nData); + pRet = (char *)sqlite3_malloc64(p->pList->nData); if( !pRet ) return SQLITE_NOMEM; nSkip = sqlite3Fts3GetVarint(p->pList->aData, &dummy); @@ -5439,7 +5599,7 @@ int sqlite3Fts3DeferToken( int iCol /* Column that token must appear in (or -1) */ ){ Fts3DeferredToken *pDeferred; - pDeferred = sqlite3_malloc(sizeof(*pDeferred)); + pDeferred = sqlite3_malloc64(sizeof(*pDeferred)); if( !pDeferred ){ return SQLITE_NOMEM; } @@ -5459,7 +5619,7 @@ int sqlite3Fts3DeferToken( /* ** SQLite value pRowid contains the rowid of a row that may or may not be ** present in the FTS3 table. If it is, delete it and adjust the contents -** of subsiduary data structures accordingly. +** of subsidiary data structures accordingly. */ static int fts3DeleteByRowid( Fts3Table *p, @@ -5518,7 +5678,6 @@ int sqlite3Fts3UpdateMethod( ){ Fts3Table *p = (Fts3Table *)pVtab; int rc = SQLITE_OK; /* Return Code */ - int isRemove = 0; /* True for an UPDATE or DELETE */ u32 *aSzIns = 0; /* Sizes of inserted documents */ u32 *aSzDel = 0; /* Sizes of deleted documents */ int nChng = 0; /* Net change in number of documents */ @@ -5552,7 +5711,7 @@ int sqlite3Fts3UpdateMethod( } /* Allocate space to hold the change in document sizes */ - aSzDel = sqlite3_malloc( sizeof(aSzDel[0])*(p->nColumn+1)*2 ); + aSzDel = sqlite3_malloc64(sizeof(aSzDel[0])*((sqlite3_int64)p->nColumn+1)*2); if( aSzDel==0 ){ rc = SQLITE_NOMEM; goto update_out; @@ -5616,7 +5775,6 @@ int sqlite3Fts3UpdateMethod( if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){ assert( sqlite3_value_type(apVal[0])==SQLITE_INTEGER ); rc = fts3DeleteByRowid(p, apVal[0], &nChng, aSzDel); - isRemove = 1; } /* If this is an INSERT or UPDATE operation, insert the new record. */ @@ -5628,7 +5786,7 @@ int sqlite3Fts3UpdateMethod( rc = FTS_CORRUPT_VTAB; } } - if( rc==SQLITE_OK && (!isRemove || *pRowid!=p->iPrevDocid ) ){ + if( rc==SQLITE_OK ){ rc = fts3PendingTermsDocid(p, 0, iLangid, *pRowid); } if( rc==SQLITE_OK ){ diff --git a/ext/fts3/mkfts3amal.tcl b/ext/fts3/mkfts3amal.tcl deleted file mode 100644 index 059048717f..0000000000 --- a/ext/fts3/mkfts3amal.tcl +++ /dev/null @@ -1,115 +0,0 @@ -#!/usr/bin/tclsh -# -# This script builds a single C code file holding all of FTS3 code. -# The name of the output file is fts3amal.c. To build this file, -# first do: -# -# make target_source -# -# The make target above moves all of the source code files into -# a subdirectory named "tsrc". (This script expects to find the files -# there and will not work if they are not found.) -# -# After the "tsrc" directory has been created and populated, run -# this script: -# -# tclsh mkfts3amal.tcl -# -# The amalgamated FTS3 code will be written into fts3amal.c -# - -# Open the output file and write a header comment at the beginning -# of the file. -# -set out [open fts3amal.c w] -set today [clock format [clock seconds] -format "%Y-%m-%d %H:%M:%S UTC" -gmt 1] -puts $out [subst \ -{/****************************************************************************** -** This file is an amalgamation of separate C source files from the SQLite -** Full Text Search extension 2 (fts3). By combining all the individual C -** code files into this single large file, the entire code can be compiled -** as a one translation unit. This allows many compilers to do optimizations -** that would not be possible if the files were compiled separately. It also -** makes the code easier to import into other projects. -** -** This amalgamation was generated on $today. -*/}] - -# These are the header files used by FTS3. The first time any of these -# files are seen in a #include statement in the C code, include the complete -# text of the file in-line. The file only needs to be included once. -# -foreach hdr { - fts3.h - fts3_hash.h - fts3_tokenizer.h - sqlite3.h - sqlite3ext.h -} { - set available_hdr($hdr) 1 -} - -# 78 stars used for comment formatting. -set s78 \ -{*****************************************************************************} - -# Insert a comment into the code -# -proc section_comment {text} { - global out s78 - set n [string length $text] - set nstar [expr {60 - $n}] - set stars [string range $s78 0 $nstar] - puts $out "/************** $text $stars/" -} - -# Read the source file named $filename and write it into the -# sqlite3.c output file. If any #include statements are seen, -# process them approprately. -# -proc copy_file {filename} { - global seen_hdr available_hdr out - set tail [file tail $filename] - section_comment "Begin file $tail" - set in [open $filename r] - while {![eof $in]} { - set line [gets $in] - if {[regexp {^#\s*include\s+["<]([^">]+)[">]} $line all hdr]} { - if {[info exists available_hdr($hdr)]} { - if {$available_hdr($hdr)} { - section_comment "Include $hdr in the middle of $tail" - copy_file tsrc/$hdr - section_comment "Continuing where we left off in $tail" - } - } elseif {![info exists seen_hdr($hdr)]} { - set seen_hdr($hdr) 1 - puts $out $line - } - } elseif {[regexp {^#ifdef __cplusplus} $line]} { - puts $out "#if 0" - } elseif {[regexp {^#line} $line]} { - # Skip #line directives. - } else { - puts $out $line - } - } - close $in - section_comment "End of $tail" -} - - -# Process the source files. Process files containing commonly -# used subroutines first in order to help the compiler find -# inlining opportunities. -# -foreach file { - fts3.c - fts3_hash.c - fts3_porter.c - fts3_tokenizer.c - fts3_tokenizer1.c -} { - copy_file tsrc/$file -} - -close $out diff --git a/ext/fts3/tool/fts3cov.sh b/ext/fts3/tool/fts3cov.sh new file mode 100644 index 0000000000..b1f34dce76 --- /dev/null +++ b/ext/fts3/tool/fts3cov.sh @@ -0,0 +1,16 @@ +#!/bin/sh + +set -e + +srcdir=`dirname $(dirname $(dirname $(dirname $0)))` +./testfixture $srcdir/test/fts3.test --output=fts3cov-out.txt + +echo "" + +for f in `ls $srcdir/ext/fts3/*.c` +do + f=`basename $f` + echo -ne "$f: " + gcov -b $f | grep Taken | sed 's/Taken at least once://' +done + diff --git a/ext/fts3/tool/fts3view.c b/ext/fts3/tool/fts3view.c index a8d7981af0..9558cde0d0 100644 --- a/ext/fts3/tool/fts3view.c +++ b/ext/fts3/tool/fts3view.c @@ -93,7 +93,7 @@ static int runSql(sqlite3 *db, const char *zFormat, ...){ static void showSchema(sqlite3 *db, const char *zTab){ sqlite3_stmt *pStmt; pStmt = prepare(db, - "SELECT sql FROM sqlite_master" + "SELECT sql FROM sqlite_schema" " WHERE name LIKE '%q%%'" " ORDER BY 1", zTab); @@ -398,8 +398,8 @@ static void showSegmentStats(sqlite3 *db, const char *zTab){ if( sqlite3_step(pStmt)==SQLITE_ROW && (nLeaf = sqlite3_column_int(pStmt, 0))>0 ){ - nIdx = sqlite3_column_int(pStmt, 5); sqlite3_int64 sz; + nIdx = sqlite3_column_int(pStmt, 5); printf("For level %d:\n", i); printf(" Number of indexes...................... %9d\n", nIdx); printf(" Number of leaf segments................ %9d\n", nLeaf); @@ -831,7 +831,7 @@ int main(int argc, char **argv){ sqlite3_stmt *pStmt; int cnt = 0; pStmt = prepare(db, "SELECT b.sql" - " FROM sqlite_master a, sqlite_master b" + " FROM sqlite_schema a, sqlite_schema b" " WHERE a.name GLOB '*_segdir'" " AND b.name=substr(a.name,1,length(a.name)-7)" " ORDER BY 1"); diff --git a/ext/fts3/unicode/mkunicode.tcl b/ext/fts3/unicode/mkunicode.tcl index aafb4e9f9b..3bf866ef74 100644 --- a/ext/fts3/unicode/mkunicode.tcl +++ b/ext/fts3/unicode/mkunicode.tcl @@ -9,11 +9,12 @@ proc print_rd {map} { set nRange 1 set iFirst [lindex $map 0 0] set cPrev [lindex $map 0 1] + set fPrev [lindex $map 0 2] foreach m [lrange $map 1 end] { - foreach {i c} $m {} + foreach {i c f} $m {} - if {$cPrev == $c} { + if {$cPrev == $c && $fPrev==$f} { for {set j [expr $iFirst+$nRange]} {$j<$i} {incr j} { if {[info exists tl_lookup_table($j)]==0} break } @@ -29,13 +30,16 @@ proc print_rd {map} { lappend lRange [list $iFirst $nRange] lappend aChar $cPrev + lappend aFlag $fPrev set iFirst $i set cPrev $c + set fPrev $f set nRange 1 } lappend lRange [list $iFirst $nRange] lappend aChar $cPrev + lappend aFlag $fPrev puts "/*" puts "** If the argument is a codepoint corresponding to a lowercase letter" @@ -45,7 +49,7 @@ proc print_rd {map} { puts "** E\"). The resuls of passing a codepoint that corresponds to an" puts "** uppercase letter are undefined." puts "*/" - puts "static int ${::remove_diacritic}(int c)\{" + puts "static int ${::remove_diacritic}(int c, int bComplex)\{" puts " unsigned short aDia\[\] = \{" puts -nonewline " 0, " set i 1 @@ -59,14 +63,19 @@ proc print_rd {map} { } puts "" puts " \};" - puts " char aChar\[\] = \{" - puts -nonewline " '\\0', " + puts "#define HIBIT ((unsigned char)0x80)" + puts " unsigned char aChar\[\] = \{" + puts -nonewline " '\\0', " set i 1 - foreach c $aChar { - set str "'$c', " - if {$c == ""} { set str "'\\0', " } + foreach c $aChar f $aFlag { + if { $f } { + set str "'$c'|HIBIT, " + } else { + set str "'$c', " + } + if {$c == ""} { set str "'\\0', " } - if {($i % 12)==0} {puts "" ; puts -nonewline " " } + if {($i % 6)==0} {puts "" ; puts -nonewline " " } incr i puts -nonewline "$str" } @@ -87,7 +96,8 @@ proc print_rd {map} { } } assert( key>=aDia[iRes] ); - return ((c > (aDia[iRes]>>3) + (aDia[iRes]&0x07)) ? c : (int)aChar[iRes]);} + if( bComplex==0 && (aChar[iRes] & 0x80) ) return c; + return (c > (aDia[iRes]>>3) + (aDia[iRes]&0x07)) ? c : ((int)aChar[iRes] & 0x7F);} puts "\}" } @@ -95,7 +105,8 @@ proc print_isdiacritic {zFunc map} { set lCode [list] foreach m $map { - foreach {code char} $m {} + foreach {code char flag} $m {} + if {$flag} continue if {$code && $char == ""} { lappend lCode $code } } set lCode [lsort -integer $lCode] @@ -124,8 +135,8 @@ proc print_isdiacritic {zFunc map} { puts " if( c<$iFirst || c>$iLast ) return 0;" puts " return (c < $iFirst+32) ?" - puts " (mask0 & (1 << (c-$iFirst))) :" - puts " (mask1 & (1 << (c-$iFirst-32)));" + puts " (mask0 & ((unsigned int)1 << (c-$iFirst))) :" + puts " (mask1 & ((unsigned int)1 << (c-$iFirst-32)));" puts "\}" } @@ -227,7 +238,7 @@ proc print_isalnum {zFunc lRange} { an_print_ascii_bitmap $lRange puts { if( (unsigned int)c<128 ){ - return ( (aAscii[c >> 5] & (1 << (c & 0x001F)))==0 ); + return ( (aAscii[c >> 5] & ((unsigned int)1 << (c & 0x001F)))==0 ); }else if( (unsigned int)c<(1<<22) ){ unsigned int key = (((unsigned int)c)<<10) | 0x000003FF; int iRes = 0; @@ -472,7 +483,7 @@ proc print_fold {zFunc} { puts "** The results are undefined if the value passed to this function" puts "** is less than zero." puts "*/" - puts "int ${zFunc}\(int c, int bRemoveDiacritic)\{" + puts "int ${zFunc}\(int c, int eRemoveDiacritic)\{" set liOff [tl_generate_ioff_table $lRecord] tl_print_table_header @@ -516,7 +527,9 @@ proc print_fold {zFunc} { assert( ret>0 ); } - if( bRemoveDiacritic ) ret = ${::remove_diacritic}(ret); + if( eRemoveDiacritic ){ + ret = ${::remove_diacritic}(ret, eRemoveDiacritic==2); + } } }] @@ -529,6 +542,263 @@ proc print_fold {zFunc} { puts "\}" } +proc code {txt} { + set txt [string trimright $txt] + set txt [string trimleft $txt "\n"] + set n [expr {[string length $txt] - [string length [string trim $txt]]}] + set ret "" + foreach L [split $txt "\n"] { + append ret "[string range $L $n end]\n" + } + return [uplevel "subst -nocommands {$ret}"] +} + +proc intarray {lInt} { + set ret "" + set n [llength $lInt] + for {set i 0} {$i < $n} {incr i 10} { + append ret "\n " + foreach int [lrange $lInt $i [expr $i+9]] { + append ret [format "%-7s" "$int, "] + } + } + append ret "\n " + set ret +} + +proc categories_switch {Cvar first lSecond} { + upvar $Cvar C + set ret "" + append ret "case '$first':\n" + append ret " switch( zCat\[1\] ){\n" + foreach s $lSecond { + append ret " case '$s': aArray\[$C($first$s)\] = 1; break;\n" + } + append ret " case '*': \n" + foreach s $lSecond { + append ret " aArray\[$C($first$s)\] = 1;\n" + } + append ret " break;\n" + append ret " default: return 1;" + append ret " }\n" + append ret " break;\n" +} + +# Argument is a list. Each element of which is itself a list of two elements: +# +# * the codepoint +# * the category +# +# List elements are sorted in order of codepoint. +# +proc print_categories {lMap} { + set categories { + Cc Cf Cn Cs + Ll Lm Lo Lt Lu + Mc Me Mn + Nd Nl No + Pc Pd Pe Pf Pi Po Ps + Sc Sk Sm So + Zl Zp Zs + + LC Co + } + + for {set i 0} {$i < [llength $categories]} {incr i} { + set C([lindex $categories $i]) [expr 1+$i] + } + + set caseC [categories_switch C C {c f n s o}] + set caseL [categories_switch C L {l m o t u C}] + set caseM [categories_switch C M {c e n}] + set caseN [categories_switch C N {d l o}] + set caseP [categories_switch C P {c d e f i o s}] + set caseS [categories_switch C S {c k m o}] + set caseZ [categories_switch C Z {l p s}] + + set nCat [expr [llength [array names C]] + 1] + puts [code { + int sqlite3Fts5UnicodeCatParse(const char *zCat, u8 *aArray){ + aArray[0] = 1; + switch( zCat[0] ){ + $caseC + $caseL + $caseM + $caseN + $caseP + $caseS + $caseZ + + default: + return 1; + } + return 0; + } + }] + + set nRepeat 0 + set first [lindex $lMap 0 0] + set class [lindex $lMap 0 1] + set prev -1 + + set CASE(0) "Lu" + set CASE(1) "Ll" + + foreach m $lMap { + foreach {codepoint cl} $m {} + set codepoint [expr "0x$codepoint"] + if {$codepoint>=(1<<20)} continue + + set bNew 0 + if {$codepoint!=($prev+1)} { + set bNew 1 + } elseif { + $cl==$class || ($class=="LC" && $cl==$CASE([expr $nRepeat & 0x01])) + } { + incr nRepeat + } elseif {$class=="Lu" && $nRepeat==1 && $cl=="Ll"} { + set class LC + incr nRepeat + } else { + set bNew 1 + } + if {$bNew} { + lappend lEntries [list $first $class $nRepeat] + set nRepeat 1 + set first $codepoint + set class $cl + } + set prev $codepoint + } + if {$nRepeat>0} { + lappend lEntries [list $first $class $nRepeat] + } + + set aBlock [list 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] + set aMap [list] + foreach e $lEntries { + foreach {cp class nRepeat} $e {} + set block [expr ($cp>>16)] + if {$block>0 && [lindex $aBlock $block]==0} { + for {set i 1} {$i<=$block} {incr i} { + if {[lindex $aBlock $i]==0} { + lset aBlock $i [llength $aMap] + } + } + } + lappend aMap [expr {$cp & 0xFFFF}] + lappend aData [expr {($nRepeat << 5) + $C($class)}] + } + for {set i 1} {$i<[llength $aBlock]} {incr i} { + if {[lindex $aBlock $i]==0} { + lset aBlock $i [llength $aMap] + } + } + + set aBlockArray [intarray $aBlock] + set aMapArray [intarray $aMap] + set aDataArray [intarray $aData] + puts [code { + static u16 aFts5UnicodeBlock[] = {$aBlockArray}; + static u16 aFts5UnicodeMap[] = {$aMapArray}; + static u16 aFts5UnicodeData[] = {$aDataArray}; + + int sqlite3Fts5UnicodeCategory(u32 iCode) { + int iRes = -1; + int iHi; + int iLo; + int ret; + u16 iKey; + + if( iCode>=(1<<20) ){ + return 0; + } + iLo = aFts5UnicodeBlock[(iCode>>16)]; + iHi = aFts5UnicodeBlock[1+(iCode>>16)]; + iKey = (iCode & 0xFFFF); + while( iHi>iLo ){ + int iTest = (iHi + iLo) / 2; + assert( iTest>=iLo && iTest=aFts5UnicodeMap[iTest] ){ + iRes = iTest; + iLo = iTest+1; + }else{ + iHi = iTest; + } + } + + if( iRes<0 ) return 0; + if( iKey>=(aFts5UnicodeMap[iRes]+(aFts5UnicodeData[iRes]>>5)) ) return 0; + ret = aFts5UnicodeData[iRes] & 0x1F; + if( ret!=$C(LC) ) return ret; + return ((iKey - aFts5UnicodeMap[iRes]) & 0x01) ? $C(Ll) : $C(Lu); + } + + void sqlite3Fts5UnicodeAscii(u8 *aArray, u8 *aAscii){ + int i = 0; + int iTbl = 0; + while( i<128 ){ + int bToken = aArray[ aFts5UnicodeData[iTbl] & 0x1F ]; + int n = (aFts5UnicodeData[iTbl] >> 5) + i; + for(; i<128 && i" puts "" puts "int main(int argc, char **argv)\{" - puts " int r1, r2;" + puts " int r1, r2, r3;" puts " int code;" + puts " r3 = 0;" puts " r1 = isalnum_test(&code);" puts " if( r1 ) printf(\"isalnum(): Problem with code %d\\n\",code);" puts " else printf(\"isalnum(): test passed\\n\");" puts " r2 = fold_test(&code);" puts " if( r2 ) printf(\"fold(): Problem with code %d\\n\",code);" puts " else printf(\"fold(): test passed\\n\");" - puts " return (r1 || r2);" + if {$::generate_fts5_code} { + puts " r3 = categories_test(&code);" + puts " if( r3 ) printf(\"categories(): Problem with code %d\\n\",code);" + puts " else printf(\"categories(): test passed\\n\");" + } + puts " return (r1 || r2 || r3);" puts "\}" } -# Proces the command line arguments. Exit early if they are not to +# Process the command line arguments. Exit early if they are not to # our liking. # proc usage {} { @@ -651,10 +927,18 @@ for {set i 0} {$i < [llength $argv]-2} {incr i} { print_fileheader +if {$::generate_test_code} { + puts "typedef unsigned short int u16;" + puts "typedef unsigned char u8;" + puts "#include " +} + # Print the isalnum() function to stdout. # set lRange [an_load_separator_ranges] -print_isalnum ${function_prefix}UnicodeIsalnum $lRange +if {$generate_fts5_code==0} { + print_isalnum ${function_prefix}UnicodeIsalnum $lRange +} # Leave a gap between the two generated C functions. # @@ -677,12 +961,21 @@ puts "" # print_fold ${function_prefix}UnicodeFold +if {$generate_fts5_code} { + puts "" + puts "" + print_categories [cc_load_unicodedata_text ${unicodedata.txt}] +} + # Print the test routines and main() function to stdout, if -test # was specified. # if {$::generate_test_code} { - print_test_isalnum ${function_prefix}UnicodeIsalnum $lRange + if {$generate_fts5_code==0} { + print_test_isalnum ${function_prefix}UnicodeIsalnum $lRange + } print_fold_test ${function_prefix}UnicodeFold $mappings + print_test_categories [cc_load_unicodedata_text ${unicodedata.txt}] print_test_main } diff --git a/ext/fts3/unicode/parseunicode.tcl b/ext/fts3/unicode/parseunicode.tcl index 0cb2c83a18..7c246a4a09 100644 --- a/ext/fts3/unicode/parseunicode.tcl +++ b/ext/fts3/unicode/parseunicode.tcl @@ -7,12 +7,24 @@ # character that it should be replaced with, or an empty string if the # codepoint should simply be removed from the input. Examples: # -# { 224 a } (replace codepoint 224 to "a") -# { 769 "" } (remove codepoint 769 from input) +# { 224 a 0 } (replace codepoint 224 to "a") +# { 769 "" 0 } (remove codepoint 769 from input) # # Mappings are only returned for non-upper case codepoints. It is assumed # that the input has already been folded to lower case. # +# The third value in the list is always either 0 or 1. 0 if the +# UnicodeData.txt file maps the codepoint to a single ASCII character and +# a diacritic, or 1 if the mapping is indirect. For example, consider the +# two entries: +# +# 1ECD;LATIN SMALL LETTER O WITH DOT BELOW;Ll;0;L;006F 0323;;;;N;;;1ECC;;1ECC +# 1ED9;LATIN SMALL LETTER O WITH CIRCUMFLEX AND DOT BELOW;Ll;0;L;1ECD 0302;;;;N;;;1ED8;;1ED8 +# +# The first codepoint is a direct mapping (as 006F is ASCII and 0323 is a +# diacritic). The second is an indirect mapping, as it maps to the +# first codepoint plus 0302 (a diacritic). +# proc rd_load_unicodedata_text {zName} { global tl_lookup_table @@ -53,18 +65,29 @@ proc rd_load_unicodedata_text {zName} { set iAscii [expr "0x[lindex $character_decomposition_mapping 0]"] set iDia [expr "0x[lindex $character_decomposition_mapping 1]"] + # Filter out upper-case characters, as they will be mapped to their + # lower-case equivalents before this data is used. if {[info exists tl_lookup_table($iCode)]} continue + # Check if this is an indirect mapping. If so, set bIndirect to true + # and change $iAscii to the indirectly mappped ASCII character. + set bIndirect 0 + if {[info exists dia($iDia)] && [info exists mapping($iAscii)]} { + set iAscii $mapping($iAscii) + set bIndirect 1 + } + if { ($iAscii >= 97 && $iAscii <= 122) || ($iAscii >= 65 && $iAscii <= 90) } { - lappend lRet [list $iCode [string tolower [format %c $iAscii]]] + lappend lRet [list $iCode [string tolower [format %c $iAscii]] $bIndirect] + set mapping($iCode) $iAscii set dia($iDia) 1 } } foreach d [array names dia] { - lappend lRet [list $d ""] + lappend lRet [list $d "" 0] } set lRet [lsort -integer -index 0 $lRet] @@ -143,4 +166,40 @@ proc tl_load_casefolding_txt {zName} { } } +proc cc_load_unicodedata_text {zName} { + set fd [open $zName] + set lField { + code + character_name + general_category + canonical_combining_classes + bidirectional_category + character_decomposition_mapping + decimal_digit_value + digit_value + numeric_value + mirrored + unicode_1_name + iso10646_comment_field + uppercase_mapping + lowercase_mapping + titlecase_mapping + } + set lRet [list] + + while { ![eof $fd] } { + set line [gets $fd] + if {$line == ""} continue + + set fields [split $line ";"] + if {[llength $fields] != [llength $lField]} { error "parse error: $line" } + foreach $lField $fields {} + + lappend lRet [list $code $general_category] + } + + close $fd + set lRet +} + diff --git a/ext/fts5/extract_api_docs.tcl b/ext/fts5/extract_api_docs.tcl index 2320d70b7d..6ee71c262c 100644 --- a/ext/fts5/extract_api_docs.tcl +++ b/ext/fts5/extract_api_docs.tcl @@ -82,7 +82,7 @@ proc get_struct_docs {data names} { set current_doc "" } set subject n/a - regexp {^ *([[:alpha:]]*)} $line -> subject + regexp {^ *([[:alnum:]_]*)} $line -> subject if {[lsearch $names $subject]>=0} { set current_header $subject } else { @@ -108,8 +108,11 @@ proc get_tokenizer_docs {data} { append res "
$line

\n" continue } + if {[regexp {FTS5_TOKENIZER} $line]} { + set line

+ } if {[regexp {SYNONYM SUPPORT} $line]} { - set line "

Synonym Support

" + set line "

Synonym Support

" } if {[string trim $line] == ""} { append res "

\n" @@ -223,10 +226,12 @@ proc main {data} { Fts5ExtensionApi { set struct [get_fts5_struct $data "^struct Fts5ExtensionApi" "^.;"] set map [list] + set lKey [list] foreach {k v} [get_struct_members $data] { if {[string match x* $k]==0} continue - lappend map $k "$k" + lappend lKey $k } + foreach k [lsort -decr $lKey] { lappend map $k "$k" } output [string map $map $struct] } diff --git a/ext/fts5/fts5.h b/ext/fts5/fts5.h index 96ecb38e33..907ea232a4 100644 --- a/ext/fts5/fts5.h +++ b/ext/fts5/fts5.h @@ -55,8 +55,8 @@ struct Fts5PhraseIter { ** EXTENSION API FUNCTIONS ** ** xUserData(pFts): -** Return a copy of the context pointer the extension function was -** registered with. +** Return a copy of the pUserData pointer passed to the xCreateFunction() +** API when the extension function was registered. ** ** xColumnTotalSize(pFts, iCol, pnToken): ** If parameter iCol is less than zero, set output variable *pnToken @@ -88,8 +88,11 @@ struct Fts5PhraseIter { ** created with the "columnsize=0" option. ** ** xColumnText: -** This function attempts to retrieve the text of column iCol of the -** current document. If successful, (*pz) is set to point to a buffer +** If parameter iCol is less than zero, or greater than or equal to the +** number of columns in the table, SQLITE_RANGE is returned. +** +** Otherwise, this function attempts to retrieve the text of column iCol of +** the current document. If successful, (*pz) is set to point to a buffer ** containing the text in utf-8 encoding, (*pn) is set to the size in bytes ** (not characters) of the buffer and SQLITE_OK is returned. Otherwise, ** if an error occurs, an SQLite error code is returned and the final values @@ -99,8 +102,10 @@ struct Fts5PhraseIter { ** Returns the number of phrases in the current query expression. ** ** xPhraseSize: -** Returns the number of tokens in phrase iPhrase of the query. Phrases -** are numbered starting from zero. +** If parameter iCol is less than zero, or greater than or equal to the +** number of phrases in the current query, as returned by xPhraseCount, +** 0 is returned. Otherwise, this function returns the number of tokens in +** phrase iPhrase of the query. Phrases are numbered starting from zero. ** ** xInstCount: ** Set *pnInst to the total number of occurrences of all phrases within @@ -116,16 +121,13 @@ struct Fts5PhraseIter { ** Query for the details of phrase match iIdx within the current row. ** Phrase matches are numbered starting from zero, so the iIdx argument ** should be greater than or equal to zero and smaller than the value -** output by xInstCount(). +** output by xInstCount(). If iIdx is less than zero or greater than +** or equal to the value returned by xInstCount(), SQLITE_RANGE is returned. ** -** Usually, output parameter *piPhrase is set to the phrase number, *piCol +** Otherwise, output parameter *piPhrase is set to the phrase number, *piCol ** to the column in which it occurs and *piOff the token offset of the -** first token of the phrase. The exception is if the table was created -** with the offsets=0 option specified. In this case *piOff is always -** set to -1. -** -** Returns SQLITE_OK if successful, or an error code (i.e. SQLITE_NOMEM) -** if an error occurs. +** first token of the phrase. SQLITE_OK is returned if successful, or an +** error code (i.e. SQLITE_NOMEM) if an error occurs. ** ** This API can be quite slow if used with an FTS5 table created with the ** "detail=none" or "detail=column" option. @@ -143,11 +145,17 @@ struct Fts5PhraseIter { ** ... FROM ftstable WHERE ftstable MATCH $p ORDER BY rowid ** ** with $p set to a phrase equivalent to the phrase iPhrase of the -** current query is executed. For each row visited, the callback function -** passed as the fourth argument is invoked. The context and API objects -** passed to the callback function may be used to access the properties of -** each matched row. Invoking Api.xUserData() returns a copy of the pointer -** passed as the third argument to pUserData. +** current query is executed. Any column filter that applies to +** phrase iPhrase of the current query is included in $p. For each +** row visited, the callback function passed as the fourth argument +** is invoked. The context and API objects passed to the callback +** function may be used to access the properties of each matched row. +** Invoking Api.xUserData() returns a copy of the pointer passed as +** the third argument to pUserData. +** +** If parameter iPhrase is less than zero, or greater than or equal to +** the number of phrases in the query, as returned by xPhraseCount(), +** this function returns SQLITE_RANGE. ** ** If the callback function returns any value other than SQLITE_OK, the ** query is abandoned and the xQueryPhrase function returns immediately. @@ -161,10 +169,10 @@ struct Fts5PhraseIter { ** ** xSetAuxdata(pFts5, pAux, xDelete) ** -** Save the pointer passed as the second argument as the extension functions +** Save the pointer passed as the second argument as the extension function's ** "auxiliary data". The pointer may then be retrieved by the current or any ** future invocation of the same fts5 extension function made as part of -** of the same MATCH query using the xGetAuxdata() API. +** the same MATCH query using the xGetAuxdata() API. ** ** Each extension function is allocated a single auxiliary data slot for ** each FTS query (MATCH expression). If the extension function is invoked @@ -179,7 +187,7 @@ struct Fts5PhraseIter { ** The xDelete callback, if one is specified, is also invoked on the ** auxiliary data pointer after the FTS5 query has finished. ** -** If an error (e.g. an OOM condition) occurs within this function, an +** If an error (e.g. an OOM condition) occurs within this function, ** the auxiliary data is set to NULL and an error code returned. If the ** xDelete parameter was not NULL, it is invoked on the auxiliary data ** pointer before returning. @@ -230,6 +238,10 @@ struct Fts5PhraseIter { ** (i.e. if it is a contentless table), then this API always iterates ** through an empty set (all calls to xPhraseFirst() set iCol to -1). ** +** In all cases, matches are visited in (column ASC, offset ASC) order. +** i.e. all those in column 0, sorted by offset, followed by those in +** column 1, etc. +** ** xPhraseNext() ** See xPhraseFirst above. ** @@ -263,9 +275,80 @@ struct Fts5PhraseIter { ** ** xPhraseNextColumn() ** See xPhraseFirstColumn above. +** +** xQueryToken(pFts5, iPhrase, iToken, ppToken, pnToken) +** This is used to access token iToken of phrase iPhrase of the current +** query. Before returning, output parameter *ppToken is set to point +** to a buffer containing the requested token, and *pnToken to the +** size of this buffer in bytes. +** +** If iPhrase or iToken are less than zero, or if iPhrase is greater than +** or equal to the number of phrases in the query as reported by +** xPhraseCount(), or if iToken is equal to or greater than the number of +** tokens in the phrase, SQLITE_RANGE is returned and *ppToken and *pnToken + are both zeroed. +** +** The output text is not a copy of the query text that specified the +** token. It is the output of the tokenizer module. For tokendata=1 +** tables, this includes any embedded 0x00 and trailing data. +** +** xInstToken(pFts5, iIdx, iToken, ppToken, pnToken) +** This is used to access token iToken of phrase hit iIdx within the +** current row. If iIdx is less than zero or greater than or equal to the +** value returned by xInstCount(), SQLITE_RANGE is returned. Otherwise, +** output variable (*ppToken) is set to point to a buffer containing the +** matching document token, and (*pnToken) to the size of that buffer in +** bytes. +** +** The output text is not a copy of the document text that was tokenized. +** It is the output of the tokenizer module. For tokendata=1 tables, this +** includes any embedded 0x00 and trailing data. +** +** This API may be slow in some cases if the token identified by parameters +** iIdx and iToken matched a prefix token in the query. In most cases, the +** first call to this API for each prefix token in the query is forced +** to scan the portion of the full-text index that matches the prefix +** token to collect the extra data required by this API. If the prefix +** token matches a large number of token instances in the document set, +** this may be a performance problem. +** +** If the user knows in advance that a query may use this API for a +** prefix token, FTS5 may be configured to collect all required data as part +** of the initial querying of the full-text index, avoiding the second scan +** entirely. This also causes prefix queries that do not use this API to +** run more slowly and use more memory. FTS5 may be configured in this way +** either on a per-table basis using the [FTS5 insttoken | 'insttoken'] +** option, or on a per-query basis using the +** [fts5_insttoken | fts5_insttoken()] user function. +** +** This API can be quite slow if used with an FTS5 table created with the +** "detail=none" or "detail=column" option. +** +** xColumnLocale(pFts5, iIdx, pzLocale, pnLocale) +** If parameter iCol is less than zero, or greater than or equal to the +** number of columns in the table, SQLITE_RANGE is returned. +** +** Otherwise, this function attempts to retrieve the locale associated +** with column iCol of the current row. Usually, there is no associated +** locale, and output parameters (*pzLocale) and (*pnLocale) are set +** to NULL and 0, respectively. However, if the fts5_locale() function +** was used to associate a locale with the value when it was inserted +** into the fts5 table, then (*pzLocale) is set to point to a nul-terminated +** buffer containing the name of the locale in utf-8 encoding. (*pnLocale) +** is set to the size in bytes of the buffer, not including the +** nul-terminator. +** +** If successful, SQLITE_OK is returned. Or, if an error occurs, an +** SQLite error code is returned. The final value of the output parameters +** is undefined in this case. +** +** xTokenize_v2: +** Tokenize text using the tokenizer belonging to the FTS5 table. This +** API is the same as the xTokenize() API, except that it allows a tokenizer +** locale to be specified. */ struct Fts5ExtensionApi { - int iVersion; /* Currently always set to 3 */ + int iVersion; /* Currently always set to 4 */ void *(*xUserData)(Fts5Context*); @@ -300,6 +383,22 @@ struct Fts5ExtensionApi { int (*xPhraseFirstColumn)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*); void (*xPhraseNextColumn)(Fts5Context*, Fts5PhraseIter*, int *piCol); + + /* Below this point are iVersion>=3 only */ + int (*xQueryToken)(Fts5Context*, + int iPhrase, int iToken, + const char **ppToken, int *pnToken + ); + int (*xInstToken)(Fts5Context*, int iIdx, int iToken, const char**, int*); + + /* Below this point are iVersion>=4 only */ + int (*xColumnLocale)(Fts5Context*, int iCol, const char **pz, int *pn); + int (*xTokenize_v2)(Fts5Context*, + const char *pText, int nText, /* Text to tokenize */ + const char *pLocale, int nLocale, /* Locale to pass to tokenizer */ + void *pCtx, /* Context passed to xToken() */ + int (*xToken)(void*, int, const char*, int, int, int) /* Callback */ + ); }; /* @@ -316,11 +415,11 @@ struct Fts5ExtensionApi { ** behaviour. The structure methods are expected to function as follows: ** ** xCreate: -** This function is used to allocate and inititalize a tokenizer instance. +** This function is used to allocate and initialize a tokenizer instance. ** A tokenizer instance is required to actually tokenize text. ** ** The first argument passed to this function is a copy of the (void*) -** pointer provided by the application when the fts5_tokenizer object +** pointer provided by the application when the fts5_tokenizer_v2 object ** was registered with FTS5 (the third argument to xCreateTokenizer()). ** The second and third arguments are an array of nul-terminated strings ** containing the tokenizer arguments, if any, specified following the @@ -344,7 +443,7 @@ struct Fts5ExtensionApi { ** argument passed to this function is a pointer to an Fts5Tokenizer object ** returned by an earlier call to xCreate(). ** -** The second argument indicates the reason that FTS5 is requesting +** The third argument indicates the reason that FTS5 is requesting ** tokenization of the supplied text. This is always one of the following ** four values: ** @@ -368,6 +467,13 @@ struct Fts5ExtensionApi { ** on a columnsize=0 database. ** ** +** The sixth and seventh arguments passed to xTokenize() - pLocale and +** nLocale - are a pointer to a buffer containing the locale to use for +** tokenization (e.g. "en_US") and its size in bytes, respectively. The +** pLocale buffer is not nul-terminated. pLocale may be passed NULL (in +** which case nLocale is always 0) to indicate that the tokenizer should +** use its default locale. +** ** For each token in the input string, the supplied callback xToken() must ** be invoked. The first argument to it should be a copy of the pointer ** passed as the second argument to xTokenize(). The third and fourth @@ -391,6 +497,30 @@ struct Fts5ExtensionApi { ** may abandon the tokenization and return any error code other than ** SQLITE_OK or SQLITE_DONE. ** +** If the tokenizer is registered using an fts5_tokenizer_v2 object, +** then the xTokenize() method has two additional arguments - pLocale +** and nLocale. These specify the locale that the tokenizer should use +** for the current request. If pLocale and nLocale are both 0, then the +** tokenizer should use its default locale. Otherwise, pLocale points to +** an nLocale byte buffer containing the name of the locale to use as utf-8 +** text. pLocale is not nul-terminated. +** +** FTS5_TOKENIZER +** +** There is also an fts5_tokenizer object. This is an older, deprecated, +** version of fts5_tokenizer_v2. It is similar except that: +** +**

    +**
  • There is no "iVersion" field, and +**
  • The xTokenize() method does not take a locale argument. +**
+** +** Legacy fts5_tokenizer tokenizers must be registered using the +** legacy xCreateTokenizer() function, instead of xCreateTokenizer_v2(). +** +** Tokenizer implementations registered using either API may be retrieved +** using both xFindTokenizer() and xFindTokenizer_v2(). +** ** SYNONYM SUPPORT ** ** Custom tokenizers may also support synonyms. Consider a case in which a @@ -403,8 +533,8 @@ struct Fts5ExtensionApi { ** ** There are several ways to approach this in FTS5: ** -**
  1. By mapping all synonyms to a single token. In this case, the -** In the above example, this means that the tokenizer returns the +**
    1. By mapping all synonyms to a single token. In this case, using +** the above example, this means that the tokenizer returns the ** same token for inputs "first" and "1st". Say that token is in ** fact "first", so that when the user inserts the document "I won ** 1st place" entries are added to the index for tokens "i", "won", @@ -412,11 +542,11 @@ struct Fts5ExtensionApi { ** the tokenizer substitutes "first" for "1st" and the query works ** as expected. ** -**
    2. By adding multiple synonyms for a single term to the FTS index. -** In this case, when tokenizing query text, the tokenizer may -** provide multiple synonyms for a single term within the document. -** FTS5 then queries the index for each synonym individually. For -** example, faced with the query: +**
    3. By querying the index for all synonyms of each query term +** separately. In this case, when tokenizing query text, the +** tokenizer may provide multiple synonyms for a single term +** within the document. FTS5 then queries the index for each +** synonym individually. For example, faced with the query: ** ** ** ... MATCH 'first place' @@ -440,9 +570,9 @@ struct Fts5ExtensionApi { ** "place". ** ** This way, even if the tokenizer does not provide synonyms -** when tokenizing query text (it should not - to do would be +** when tokenizing query text (it should not - to do so would be ** inefficient), it doesn't matter if the user queries for -** 'first + place' or '1st + place', as there are entires in the +** 'first + place' or '1st + place', as there are entries in the ** FTS index corresponding to both forms of the first token. **
    ** @@ -470,7 +600,7 @@ struct Fts5ExtensionApi { ** extra data to the FTS index or require FTS5 to query for multiple terms, ** so it is efficient in terms of disk space and query speed. However, it ** does not support prefix queries very well. If, as suggested above, the -** token "first" is subsituted for "1st" by the tokenizer, then the query: +** token "first" is substituted for "1st" by the tokenizer, then the query: ** ** ** ... MATCH '1s*' @@ -494,11 +624,38 @@ struct Fts5ExtensionApi { ** as separate queries of the FTS index are required for each synonym. ** ** When using methods (2) or (3), it is important that the tokenizer only -** provide synonyms when tokenizing document text (method (2)) or query -** text (method (3)), not both. Doing so will not cause any errors, but is +** provide synonyms when tokenizing document text (method (3)) or query +** text (method (2)), not both. Doing so will not cause any errors, but is ** inefficient. */ typedef struct Fts5Tokenizer Fts5Tokenizer; +typedef struct fts5_tokenizer_v2 fts5_tokenizer_v2; +struct fts5_tokenizer_v2 { + int iVersion; /* Currently always 2 */ + + int (*xCreate)(void*, const char **azArg, int nArg, Fts5Tokenizer **ppOut); + void (*xDelete)(Fts5Tokenizer*); + int (*xTokenize)(Fts5Tokenizer*, + void *pCtx, + int flags, /* Mask of FTS5_TOKENIZE_* flags */ + const char *pText, int nText, + const char *pLocale, int nLocale, + int (*xToken)( + void *pCtx, /* Copy of 2nd argument to xTokenize() */ + int tflags, /* Mask of FTS5_TOKEN_* flags */ + const char *pToken, /* Pointer to buffer containing token */ + int nToken, /* Size of token in bytes */ + int iStart, /* Byte offset of token within input text */ + int iEnd /* Byte offset of end of token within input text */ + ) + ); +}; + +/* +** New code should use the fts5_tokenizer_v2 type to define tokenizer +** implementations. The following type is included for legacy applications +** that still use it. +*/ typedef struct fts5_tokenizer fts5_tokenizer; struct fts5_tokenizer { int (*xCreate)(void*, const char **azArg, int nArg, Fts5Tokenizer **ppOut); @@ -518,6 +675,7 @@ struct fts5_tokenizer { ); }; + /* Flags that may be passed as the third argument to xTokenize() */ #define FTS5_TOKENIZE_QUERY 0x0001 #define FTS5_TOKENIZE_PREFIX 0x0002 @@ -537,13 +695,13 @@ struct fts5_tokenizer { */ typedef struct fts5_api fts5_api; struct fts5_api { - int iVersion; /* Currently always set to 2 */ + int iVersion; /* Currently always set to 3 */ /* Create a new tokenizer */ int (*xCreateTokenizer)( fts5_api *pApi, const char *zName, - void *pContext, + void *pUserData, fts5_tokenizer *pTokenizer, void (*xDestroy)(void*) ); @@ -552,7 +710,7 @@ struct fts5_api { int (*xFindTokenizer)( fts5_api *pApi, const char *zName, - void **ppContext, + void **ppUserData, fts5_tokenizer *pTokenizer ); @@ -560,10 +718,29 @@ struct fts5_api { int (*xCreateFunction)( fts5_api *pApi, const char *zName, - void *pContext, + void *pUserData, fts5_extension_function xFunction, void (*xDestroy)(void*) ); + + /* APIs below this point are only available if iVersion>=3 */ + + /* Create a new tokenizer */ + int (*xCreateTokenizer_v2)( + fts5_api *pApi, + const char *zName, + void *pUserData, + fts5_tokenizer_v2 *pTokenizer, + void (*xDestroy)(void*) + ); + + /* Find an existing tokenizer */ + int (*xFindTokenizer_v2)( + fts5_api *pApi, + const char *zName, + void **ppUserData, + fts5_tokenizer_v2 **ppTokenizer + ); }; /* @@ -575,4 +752,3 @@ struct fts5_api { #endif #endif /* _FTS5_H */ - diff --git a/ext/fts5/fts5Int.h b/ext/fts5/fts5Int.h index af40412167..d5404535cc 100644 --- a/ext/fts5/fts5Int.h +++ b/ext/fts5/fts5Int.h @@ -20,6 +20,7 @@ SQLITE_EXTENSION_INIT1 #include #include +#include #ifndef SQLITE_AMALGAMATION @@ -30,11 +31,25 @@ typedef short i16; typedef sqlite3_int64 i64; typedef sqlite3_uint64 u64; -#define ArraySize(x) ((int)(sizeof(x) / sizeof(x[0]))) +#ifndef ArraySize +# define ArraySize(x) ((int)(sizeof(x) / sizeof(x[0]))) +#endif #define testcase(x) -#define ALWAYS(x) 1 -#define NEVER(x) 0 + +#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST) +# define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1 +#endif +#if defined(SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS) +# define ALWAYS(X) (1) +# define NEVER(X) (0) +#elif !defined(NDEBUG) +# define ALWAYS(X) ((X)?1:(assert(0),0)) +# define NEVER(X) ((X)?(assert(0),1):0) +#else +# define ALWAYS(X) (X) +# define NEVER(X) (X) +#endif #define MIN(x,y) (((x) < (y)) ? (x) : (y)) #define MAX(x,y) (((x) > (y)) ? (x) : (y)) @@ -45,8 +60,39 @@ typedef sqlite3_uint64 u64; # define LARGEST_INT64 (0xffffffff|(((i64)0x7fffffff)<<32)) # define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64) +/* +** This macro is used in a single assert() within fts5 to check that an +** allocation is aligned to an 8-byte boundary. But it is a complicated +** macro to get right for multiple platforms without generating warnings. +** So instead of reproducing the entire definition from sqliteInt.h, we +** just do without this assert() for the rare non-amalgamation builds. +*/ +#define EIGHT_BYTE_ALIGNMENT(x) 1 + +/* +** Macros needed to provide flexible arrays in a portable way +*/ +#ifndef offsetof +# define offsetof(ST,M) ((size_t)((char*)&((ST*)0)->M - (char*)0)) +#endif +#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) +# define FLEXARRAY +#else +# define FLEXARRAY 1 #endif +#endif /* SQLITE_AMALGAMATION */ + +/* +** Constants for the largest and smallest possible 32-bit signed integers. +*/ +# define LARGEST_INT32 ((int)(0x7fffffff)) +# define SMALLEST_INT32 ((int)((-1) - LARGEST_INT32)) + +/* Truncate very long tokens to this many bytes. Hard limit is +** (65536-1-1-4-9)==65521 bytes. The limiting factor is the 16-bit offset +** field that occurs at the start of each leaf page (see fts5_index.c). */ +#define FTS5_MAX_TOKEN_SIZE 32768 /* ** Maximum number of prefix indexes on single FTS5 table. This must be @@ -55,6 +101,11 @@ typedef sqlite3_uint64 u64; */ #define FTS5_MAX_PREFIX_INDEXES 31 +/* +** Maximum segments permitted in a single index +*/ +#define FTS5_MAX_SEGMENT 2000 + #define FTS5_DEFAULT_NEARDIST 10 #define FTS5_DEFAULT_RANK "bm25" @@ -81,6 +132,12 @@ extern int sqlite3_fts5_may_be_corrupt; # define assert_nc(x) assert(x) #endif +/* +** A version of memcmp() that does not cause asan errors if one of the pointer +** parameters is NULL and the number of bytes to compare is zero. +*/ +#define fts5Memcmp(s1, s2, n) ((n)<=0 ? 0 : memcmp((s1), (s2), (n))) + /* Mark a function parameter as unused, to suppress nuisance compiler ** warnings. */ #ifndef UNUSED_PARAM @@ -102,10 +159,11 @@ typedef struct Fts5Colset Fts5Colset; */ struct Fts5Colset { int nCol; - int aiCol[1]; + int aiCol[FLEXARRAY]; }; - +/* Size (int bytes) of a complete Fts5Colset object with N columns. */ +#define SZ_FTS5COLSET(N) (sizeof(i64)*((N+2)/2)) /************************************************************************** ** Interface to code in fts5_config.c. fts5_config.c contains contains code @@ -113,6 +171,18 @@ struct Fts5Colset { */ typedef struct Fts5Config Fts5Config; +typedef struct Fts5TokenizerConfig Fts5TokenizerConfig; + +struct Fts5TokenizerConfig { + Fts5Tokenizer *pTok; + fts5_tokenizer_v2 *pApi2; + fts5_tokenizer *pApi1; + const char **azArg; + int nArg; + int ePattern; /* FTS_PATTERN_XXX constant */ + const char *pLocale; /* Current locale to use */ + int nLocale; /* Size of pLocale in bytes */ +}; /* ** An instance of the following structure encodes all information that can @@ -125,6 +195,10 @@ typedef struct Fts5Config Fts5Config; ** attempt to merge together. A value of 1 sets the object to use the ** compile time default. Zero disables auto-merge altogether. ** +** bContentlessDelete: +** True if the contentless_delete option was present in the CREATE +** VIRTUAL TABLE statement. +** ** zContent: ** ** zContentRowid: @@ -148,9 +222,12 @@ typedef struct Fts5Config Fts5Config; ** ** INSERT INTO tbl(tbl, rank) VALUES('prefix-index', $bPrefixIndex); ** +** bLocale: +** Set to true if locale=1 was specified when the table was created. */ struct Fts5Config { sqlite3 *db; /* Database handle */ + Fts5Global *pGlobal; /* Global fts5 object for handle db */ char *zDb; /* Database holding FTS index (e.g. "main") */ char *zName; /* Name of FTS index */ int nCol; /* Number of columns */ @@ -159,22 +236,32 @@ struct Fts5Config { int nPrefix; /* Number of prefix indexes */ int *aPrefix; /* Sizes in bytes of nPrefix prefix indexes */ int eContent; /* An FTS5_CONTENT value */ + int bContentlessDelete; /* "contentless_delete=" option (dflt==0) */ + int bContentlessUnindexed; /* "contentless_unindexed=" option (dflt=0) */ char *zContent; /* content table */ char *zContentRowid; /* "content_rowid=" option value */ int bColumnsize; /* "columnsize=" option value (dflt==1) */ + int bTokendata; /* "tokendata=" option value (dflt==0) */ + int bLocale; /* "locale=" option value (dflt==0) */ int eDetail; /* FTS5_DETAIL_XXX value */ char *zContentExprlist; - Fts5Tokenizer *pTok; - fts5_tokenizer *pTokApi; + Fts5TokenizerConfig t; + int bLock; /* True when table is preparing statement */ + /* Values loaded from the %_config table */ + int iVersion; /* fts5 file format 'version' */ int iCookie; /* Incremented when %_config is modified */ int pgsz; /* Approximate page size used in %_data */ int nAutomerge; /* 'automerge' setting */ int nCrisisMerge; /* Maximum allowed segments per level */ + int nUsermerge; /* 'usermerge' setting */ int nHashSize; /* Bytes of memory for in-memory hash */ char *zRank; /* Name of rank function */ char *zRankArgs; /* Arguments to rank function */ + int bSecureDelete; /* 'secure-delete' */ + int nDeleteMerge; /* 'deletemerge' */ + int bPrefixInsttoken; /* 'prefix-insttoken' */ /* If non-NULL, points to sqlite3_vtab.base.zErrmsg. Often NULL. */ char **pzErrmsg; @@ -184,18 +271,24 @@ struct Fts5Config { #endif }; -/* Current expected value of %_config table 'version' field */ -#define FTS5_CURRENT_VERSION 4 +/* Current expected value of %_config table 'version' field. And +** the expected version if the 'secure-delete' option has ever been +** set on the table. */ +#define FTS5_CURRENT_VERSION 4 +#define FTS5_CURRENT_VERSION_SECUREDELETE 5 -#define FTS5_CONTENT_NORMAL 0 -#define FTS5_CONTENT_NONE 1 -#define FTS5_CONTENT_EXTERNAL 2 - -#define FTS5_DETAIL_FULL 0 -#define FTS5_DETAIL_NONE 1 -#define FTS5_DETAIL_COLUMNS 2 +#define FTS5_CONTENT_NORMAL 0 +#define FTS5_CONTENT_NONE 1 +#define FTS5_CONTENT_EXTERNAL 2 +#define FTS5_CONTENT_UNINDEXED 3 +#define FTS5_DETAIL_FULL 0 +#define FTS5_DETAIL_NONE 1 +#define FTS5_DETAIL_COLUMNS 2 +#define FTS5_PATTERN_NONE 0 +#define FTS5_PATTERN_LIKE 65 /* matches SQLITE_INDEX_CONSTRAINT_LIKE */ +#define FTS5_PATTERN_GLOB 66 /* matches SQLITE_INDEX_CONSTRAINT_GLOB */ int sqlite3Fts5ConfigParse( Fts5Global*, sqlite3*, int, const char **, Fts5Config**, char** @@ -222,6 +315,8 @@ int sqlite3Fts5ConfigSetValue(Fts5Config*, const char*, sqlite3_value*, int*); int sqlite3Fts5ConfigParseRank(const char*, char**, char**); +void sqlite3Fts5ConfigErrmsg(Fts5Config *pConfig, const char *zFmt, ...); + /* ** End of interface to code in fts5_config.c. **************************************************************************/ @@ -252,7 +347,7 @@ void sqlite3Fts5BufferAppendPrintf(int *, Fts5Buffer*, char *zFmt, ...); char *sqlite3Fts5Mprintf(int *pRc, const char *zFmt, ...); #define fts5BufferZero(x) sqlite3Fts5BufferZero(x) -#define fts5BufferAppendVarint(a,b,c) sqlite3Fts5BufferAppendVarint(a,b,c) +#define fts5BufferAppendVarint(a,b,c) sqlite3Fts5BufferAppendVarint(a,b,(i64)c) #define fts5BufferFree(a) sqlite3Fts5BufferFree(a) #define fts5BufferAppendBlob(a,b,c,d) sqlite3Fts5BufferAppendBlob(a,b,c,d) #define fts5BufferSet(a,b,c,d) sqlite3Fts5BufferSet(a,b,c,d) @@ -266,8 +361,8 @@ char *sqlite3Fts5Mprintf(int *pRc, const char *zFmt, ...); void sqlite3Fts5Put32(u8*, int); int sqlite3Fts5Get32(const u8*); -#define FTS5_POS2COLUMN(iPos) (int)(iPos >> 32) -#define FTS5_POS2OFFSET(iPos) (int)(iPos & 0xFFFFFFFF) +#define FTS5_POS2COLUMN(iPos) (int)((iPos >> 32) & 0x7FFFFFFF) +#define FTS5_POS2OFFSET(iPos) (int)(iPos & 0x7FFFFFFF) typedef struct Fts5PoslistReader Fts5PoslistReader; struct Fts5PoslistReader { @@ -302,7 +397,7 @@ int sqlite3Fts5PoslistNext64( ); /* Malloc utility */ -void *sqlite3Fts5MallocZero(int *pRc, int nByte); +void *sqlite3Fts5MallocZero(int *pRc, sqlite3_int64 nByte); char *sqlite3Fts5Strndup(int *pRc, const char *pIn, int nIn); /* Character set tests (like isspace(), isalpha() etc.) */ @@ -339,16 +434,19 @@ struct Fts5IndexIter { /* ** Values used as part of the flags argument passed to IndexQuery(). */ -#define FTS5INDEX_QUERY_PREFIX 0x0001 /* Prefix query */ -#define FTS5INDEX_QUERY_DESC 0x0002 /* Docs in descending rowid order */ -#define FTS5INDEX_QUERY_TEST_NOIDX 0x0004 /* Do not use prefix index */ -#define FTS5INDEX_QUERY_SCAN 0x0008 /* Scan query (fts5vocab) */ +#define FTS5INDEX_QUERY_PREFIX 0x0001 /* Prefix query */ +#define FTS5INDEX_QUERY_DESC 0x0002 /* Docs in descending rowid order */ +#define FTS5INDEX_QUERY_TEST_NOIDX 0x0004 /* Do not use prefix index */ +#define FTS5INDEX_QUERY_SCAN 0x0008 /* Scan query (fts5vocab) */ /* The following are used internally by the fts5_index.c module. They are ** defined here only to make it easier to avoid clashes with the flags ** above. */ -#define FTS5INDEX_QUERY_SKIPEMPTY 0x0010 -#define FTS5INDEX_QUERY_NOOUTPUT 0x0020 +#define FTS5INDEX_QUERY_SKIPEMPTY 0x0010 +#define FTS5INDEX_QUERY_NOOUTPUT 0x0020 +#define FTS5INDEX_QUERY_SKIPHASH 0x0040 +#define FTS5INDEX_QUERY_NOTOKENDATA 0x0080 +#define FTS5INDEX_QUERY_SCANONETERM 0x0100 /* ** Create/destroy an Fts5Index object. @@ -403,12 +501,31 @@ int sqlite3Fts5IterNextFrom(Fts5IndexIter*, i64 iMatch); */ void sqlite3Fts5IterClose(Fts5IndexIter*); +/* +** Close the reader blob handle, if it is open. +*/ +void sqlite3Fts5IndexCloseReader(Fts5Index*); + /* ** This interface is used by the fts5vocab module. */ const char *sqlite3Fts5IterTerm(Fts5IndexIter*, int*); int sqlite3Fts5IterNextScan(Fts5IndexIter*); +void *sqlite3Fts5StructureRef(Fts5Index*); +void sqlite3Fts5StructureRelease(void*); +int sqlite3Fts5StructureTest(Fts5Index*, void*); +/* +** Used by xInstToken(): +*/ +int sqlite3Fts5IterToken( + Fts5IndexIter *pIndexIter, + const char *pToken, int nToken, + i64 iRowid, + int iCol, + int iOff, + const char **ppOut, int *pnOut +); /* ** Insert or remove data to or from the index. Each time a document is @@ -439,9 +556,9 @@ int sqlite3Fts5IndexBeginWrite( /* ** Flush any data stored in the in-memory hash tables to the database. -** If the bCommit flag is true, also close any open blob handles. +** Also close any open blob handles. */ -int sqlite3Fts5IndexSync(Fts5Index *p, int bCommit); +int sqlite3Fts5IndexSync(Fts5Index *p); /* ** Discard any data stored in the in-memory hash tables. Do not write it @@ -460,7 +577,7 @@ int sqlite3Fts5IndexSetAverages(Fts5Index *p, const u8*, int); /* ** Functions called by the storage module as part of integrity-check. */ -int sqlite3Fts5IndexIntegrityCheck(Fts5Index*, u64 cksum); +int sqlite3Fts5IndexIntegrityCheck(Fts5Index*, u64 cksum, int bUseCksum); /* ** Called during virtual module initialization to register UDF @@ -479,9 +596,20 @@ int sqlite3Fts5IndexReads(Fts5Index *p); int sqlite3Fts5IndexReinit(Fts5Index *p); int sqlite3Fts5IndexOptimize(Fts5Index *p); int sqlite3Fts5IndexMerge(Fts5Index *p, int nMerge); +int sqlite3Fts5IndexReset(Fts5Index *p); int sqlite3Fts5IndexLoadConfig(Fts5Index *p); +int sqlite3Fts5IndexGetOrigin(Fts5Index *p, i64 *piOrigin); +int sqlite3Fts5IndexContentlessDelete(Fts5Index *p, i64 iOrigin, i64 iRowid); + +void sqlite3Fts5IndexIterClearTokendata(Fts5IndexIter*); + +/* Used to populate hash tables for xInstToken in detail=none/column mode. */ +int sqlite3Fts5IndexIterWriteTokendata( + Fts5IndexIter*, const char*, int, i64 iRowid, int iCol, int iOff +); + /* ** End of interface to code in fts5_index.c. **************************************************************************/ @@ -494,7 +622,7 @@ int sqlite3Fts5GetVarintLen(u32 iVal); u8 sqlite3Fts5GetVarint(const unsigned char*, u64*); int sqlite3Fts5PutVarint(unsigned char *p, u64 v); -#define fts5GetVarint32(a,b) sqlite3Fts5GetVarint32(a,(u32*)&b) +#define fts5GetVarint32(a,b) sqlite3Fts5GetVarint32(a,(u32*)&(b)) #define fts5GetVarint sqlite3Fts5GetVarint #define fts5FastGetVarint32(a, iOff, nVal) { \ @@ -512,19 +640,32 @@ int sqlite3Fts5PutVarint(unsigned char *p, u64 v); /************************************************************************** -** Interface to code in fts5.c. +** Interface to code in fts5_main.c. */ -int sqlite3Fts5GetTokenizer( - Fts5Global*, - const char **azArg, - int nArg, - Fts5Tokenizer**, - fts5_tokenizer**, - char **pzErr -); +/* +** Virtual-table object. +*/ +typedef struct Fts5Table Fts5Table; +struct Fts5Table { + sqlite3_vtab base; /* Base class used by SQLite core */ + Fts5Config *pConfig; /* Virtual table configuration */ + Fts5Index *pIndex; /* Full-text index */ +}; + +int sqlite3Fts5LoadTokenizer(Fts5Config *pConfig); -Fts5Index *sqlite3Fts5IndexFromCsrid(Fts5Global*, i64, Fts5Config **); +Fts5Table *sqlite3Fts5TableFromCsrid(Fts5Global*, i64); + +int sqlite3Fts5FlushToDisk(Fts5Table*); + +void sqlite3Fts5ClearLocale(Fts5Config *pConfig); +void sqlite3Fts5SetLocale(Fts5Config *pConfig, const char *pLoc, int nLoc); + +int sqlite3Fts5IsLocaleValue(Fts5Config *pConfig, sqlite3_value *pVal); +int sqlite3Fts5DecodeLocaleValue(sqlite3_value *pVal, + const char **ppText, int *pnText, const char **ppLoc, int *pnLoc +); /* ** End of interface to code in fts5.c. @@ -555,10 +696,16 @@ int sqlite3Fts5HashWrite( */ void sqlite3Fts5HashClear(Fts5Hash*); +/* +** Return true if the hash is empty, false otherwise. +*/ +int sqlite3Fts5HashIsEmpty(Fts5Hash*); + int sqlite3Fts5HashQuery( Fts5Hash*, /* Hash table to query */ + int nPre, const char *pTerm, int nTerm, /* Query term */ - const u8 **ppDoclist, /* OUT: Pointer to doclist for pTerm */ + void **ppObj, /* OUT: Pointer to doclist for pTerm */ int *pnDoclist /* OUT: Size of doclist in bytes */ ); @@ -570,11 +717,13 @@ void sqlite3Fts5HashScanNext(Fts5Hash*); int sqlite3Fts5HashScanEof(Fts5Hash*); void sqlite3Fts5HashScanEntry(Fts5Hash *, const char **pzTerm, /* OUT: term (nul-terminated) */ + int *pnTerm, /* OUT: Size of term in bytes */ const u8 **ppDoclist, /* OUT: pointer to doclist */ int *pnDoclist /* OUT: size of doclist in bytes */ ); + /* ** End of interface to code in fts5_hash.c. **************************************************************************/ @@ -597,11 +746,11 @@ int sqlite3Fts5StorageRename(Fts5Storage*, const char *zName); int sqlite3Fts5DropAll(Fts5Config*); int sqlite3Fts5CreateTable(Fts5Config*, const char*, const char*, int, char **); -int sqlite3Fts5StorageDelete(Fts5Storage *p, i64, sqlite3_value**); -int sqlite3Fts5StorageContentInsert(Fts5Storage *p, sqlite3_value**, i64*); +int sqlite3Fts5StorageDelete(Fts5Storage *p, i64, sqlite3_value**, int); +int sqlite3Fts5StorageContentInsert(Fts5Storage *p, int, sqlite3_value**, i64*); int sqlite3Fts5StorageIndexInsert(Fts5Storage *p, sqlite3_value**, i64); -int sqlite3Fts5StorageIntegrity(Fts5Storage *p); +int sqlite3Fts5StorageIntegrity(Fts5Storage *p, int iArg); int sqlite3Fts5StorageStmt(Fts5Storage *p, int eStmt, sqlite3_stmt**, char**); void sqlite3Fts5StorageStmtRelease(Fts5Storage *p, int eStmt, sqlite3_stmt*); @@ -610,7 +759,7 @@ int sqlite3Fts5StorageDocsize(Fts5Storage *p, i64 iRowid, int *aCol); int sqlite3Fts5StorageSize(Fts5Storage *p, int iCol, i64 *pnAvg); int sqlite3Fts5StorageRowCount(Fts5Storage *p, i64 *pnRow); -int sqlite3Fts5StorageSync(Fts5Storage *p, int bCommit); +int sqlite3Fts5StorageSync(Fts5Storage *p); int sqlite3Fts5StorageRollback(Fts5Storage *p); int sqlite3Fts5StorageConfigValue( @@ -621,6 +770,10 @@ int sqlite3Fts5StorageDeleteAll(Fts5Storage *p); int sqlite3Fts5StorageRebuild(Fts5Storage *p); int sqlite3Fts5StorageOptimize(Fts5Storage *p); int sqlite3Fts5StorageMerge(Fts5Storage *p, int nMerge); +int sqlite3Fts5StorageReset(Fts5Storage *p); + +void sqlite3Fts5StorageReleaseDeleteRow(Fts5Storage*); +int sqlite3Fts5StorageFindDeleteRow(Fts5Storage *p, i64 iDel); /* ** End of interface to code in fts5_storage.c. @@ -645,10 +798,19 @@ struct Fts5Token { /* Parse a MATCH expression. */ int sqlite3Fts5ExprNew( Fts5Config *pConfig, + int bPhraseToAnd, + int iCol, /* Column on LHS of MATCH operator */ const char *zExpr, Fts5Expr **ppNew, char **pzErr ); +int sqlite3Fts5ExprPattern( + Fts5Config *pConfig, + int bGlob, + int iCol, + const char *zText, + Fts5Expr **pp +); /* ** for(rc = sqlite3Fts5ExprFirst(pExpr, pIdx, bDesc); @@ -659,12 +821,13 @@ int sqlite3Fts5ExprNew( ** i64 iRowid = sqlite3Fts5ExprRowid(pExpr); ** } */ -int sqlite3Fts5ExprFirst(Fts5Expr*, Fts5Index *pIdx, i64 iMin, int bDesc); +int sqlite3Fts5ExprFirst(Fts5Expr*, Fts5Index *pIdx, i64 iMin, i64, int bDesc); int sqlite3Fts5ExprNext(Fts5Expr*, i64 iMax); int sqlite3Fts5ExprEof(Fts5Expr*); i64 sqlite3Fts5ExprRowid(Fts5Expr*); void sqlite3Fts5ExprFree(Fts5Expr*); +int sqlite3Fts5ExprAnd(Fts5Expr **pp1, Fts5Expr *p2); /* Called during startup to register a UDF with SQLite */ int sqlite3Fts5ExprInit(Fts5Global*, sqlite3*); @@ -679,12 +842,15 @@ int sqlite3Fts5ExprPopulatePoslists( Fts5Config*, Fts5Expr*, Fts5PoslistPopulator*, int, const char*, int ); void sqlite3Fts5ExprCheckPoslists(Fts5Expr*, i64); -void sqlite3Fts5ExprClearEof(Fts5Expr*); int sqlite3Fts5ExprClonePhrase(Fts5Expr*, int, Fts5Expr**); int sqlite3Fts5ExprPhraseCollist(Fts5Expr *, int, const u8 **, int *); +int sqlite3Fts5ExprQueryToken(Fts5Expr*, int, int, const char**, int*); +int sqlite3Fts5ExprInstToken(Fts5Expr*, i64, int, int, int, int, const char**, int*); +void sqlite3Fts5ExprClearTokens(Fts5Expr*); + /******************************************* ** The fts5_expr.c API above this point is used by the other hand-written ** C code in this module. The interfaces below this point are called by @@ -700,6 +866,12 @@ Fts5ExprNode *sqlite3Fts5ParseNode( Fts5ExprNearset *pNear ); +Fts5ExprNode *sqlite3Fts5ParseImplicitAnd( + Fts5Parse *pParse, + Fts5ExprNode *pLeft, + Fts5ExprNode *pRight +); + Fts5ExprPhrase *sqlite3Fts5ParseTerm( Fts5Parse *pParse, Fts5ExprPhrase *pPhrase, @@ -707,6 +879,8 @@ Fts5ExprPhrase *sqlite3Fts5ParseTerm( int bPrefix ); +void sqlite3Fts5ParseSetCaret(Fts5ExprPhrase*); + Fts5ExprNearset *sqlite3Fts5ParseNearset( Fts5Parse*, Fts5ExprNearset*, @@ -724,7 +898,8 @@ void sqlite3Fts5ParseNearsetFree(Fts5ExprNearset*); void sqlite3Fts5ParseNodeFree(Fts5ExprNode*); void sqlite3Fts5ParseSetDistance(Fts5Parse*, Fts5ExprNearset*, Fts5Token*); -void sqlite3Fts5ParseSetColset(Fts5Parse*, Fts5ExprNearset*, Fts5Colset*); +void sqlite3Fts5ParseSetColset(Fts5Parse*, Fts5ExprNode*, Fts5Colset*); +Fts5Colset *sqlite3Fts5ParseColsetInvert(Fts5Parse*, Fts5Colset*); void sqlite3Fts5ParseFinished(Fts5Parse *pParse, Fts5ExprNode *p); void sqlite3Fts5ParseNear(Fts5Parse *pParse, Fts5Token*); @@ -748,6 +923,11 @@ int sqlite3Fts5AuxInit(fts5_api*); */ int sqlite3Fts5TokenizerInit(fts5_api*); +int sqlite3Fts5TokenizerPattern( + int (*xCreate)(void*, const char**, int, Fts5Tokenizer**), + Fts5Tokenizer *pTok +); +int sqlite3Fts5TokenizerPreload(Fts5TokenizerConfig*); /* ** End of interface to code in fts5_tokenizer.c. **************************************************************************/ @@ -766,9 +946,12 @@ int sqlite3Fts5VocabInit(Fts5Global*, sqlite3*); /************************************************************************** ** Interface to automatically generated code in fts5_unicode2.c. */ -int sqlite3Fts5UnicodeIsalnum(int c); int sqlite3Fts5UnicodeIsdiacritic(int c); int sqlite3Fts5UnicodeFold(int c, int bRemoveDiacritic); + +int sqlite3Fts5UnicodeCatParse(const char*, u8*); +int sqlite3Fts5UnicodeCategory(u32 iCode); +void sqlite3Fts5UnicodeAscii(u8*, u8*); /* ** End of interface to code in fts5_unicode2.c. **************************************************************************/ diff --git a/ext/fts5/fts5_aux.c b/ext/fts5/fts5_aux.c index 836de8a0f0..95b33ea318 100644 --- a/ext/fts5/fts5_aux.c +++ b/ext/fts5/fts5_aux.c @@ -110,15 +110,19 @@ static int fts5CInstIterInit( */ typedef struct HighlightContext HighlightContext; struct HighlightContext { - CInstIter iter; /* Coalesced Instance Iterator */ - int iPos; /* Current token offset in zIn[] */ + /* Constant parameters to fts5HighlightCb() */ int iRangeStart; /* First token to include */ int iRangeEnd; /* If non-zero, last token to include */ const char *zOpen; /* Opening highlight */ const char *zClose; /* Closing highlight */ const char *zIn; /* Input text */ int nIn; /* Size of input text in bytes */ - int iOff; /* Current offset within zIn[] */ + + /* Variables modified by fts5HighlightCb() */ + CInstIter iter; /* Coalesced Instance Iterator */ + int iPos; /* Current token offset in zIn[] */ + int iOff; /* Have copied up to this offset in zIn[] */ + int bOpen; /* True if highlight is open */ char *zOut; /* Output value */ }; @@ -136,7 +140,7 @@ static void fts5HighlightAppend( HighlightContext *p, const char *z, int n ){ - if( *pRc==SQLITE_OK ){ + if( *pRc==SQLITE_OK && z ){ if( n<0 ) n = (int)strlen(z); p->zOut = sqlite3_mprintf("%z%.*s", p->zOut, n, z); if( p->zOut==0 ) *pRc = SQLITE_NOMEM; @@ -151,8 +155,8 @@ static int fts5HighlightCb( int tflags, /* Mask of FTS5_TOKEN_* flags */ const char *pToken, /* Buffer containing token */ int nToken, /* Size of token in bytes */ - int iStartOff, /* Start offset of token */ - int iEndOff /* End offset of token */ + int iStartOff, /* Start byte offset of token */ + int iEndOff /* End byte offset of token */ ){ HighlightContext *p = (HighlightContext*)pContext; int rc = SQLITE_OK; @@ -163,40 +167,66 @@ static int fts5HighlightCb( if( tflags & FTS5_TOKEN_COLOCATED ) return SQLITE_OK; iPos = p->iPos++; - if( p->iRangeEnd>0 ){ + if( p->iRangeEnd>=0 ){ if( iPosiRangeStart || iPos>p->iRangeEnd ) return SQLITE_OK; if( p->iRangeStart && iPos==p->iRangeStart ) p->iOff = iStartOff; } - if( iPos==p->iter.iStart ){ + /* If the parenthesis is open, and this token is not part of the current + ** phrase, and the starting byte offset of this token is past the point + ** that has currently been copied into the output buffer, close the + ** parenthesis. */ + if( p->bOpen + && (iPos<=p->iter.iStart || p->iter.iStart<0) + && iStartOff>p->iOff + ){ + fts5HighlightAppend(&rc, p, p->zClose, -1); + p->bOpen = 0; + } + + /* If this is the start of a new phrase, and the highlight is not open: + ** + ** * copy text from the input up to the start of the phrase, and + ** * open the highlight. + */ + if( iPos==p->iter.iStart && p->bOpen==0 ){ fts5HighlightAppend(&rc, p, &p->zIn[p->iOff], iStartOff - p->iOff); fts5HighlightAppend(&rc, p, p->zOpen, -1); p->iOff = iStartOff; + p->bOpen = 1; } if( iPos==p->iter.iEnd ){ - if( p->iRangeEnd && p->iter.iStartiRangeStart ){ + if( p->bOpen==0 ){ + assert( p->iRangeEnd>=0 ); fts5HighlightAppend(&rc, p, p->zOpen, -1); + p->bOpen = 1; } fts5HighlightAppend(&rc, p, &p->zIn[p->iOff], iEndOff - p->iOff); - fts5HighlightAppend(&rc, p, p->zClose, -1); p->iOff = iEndOff; + if( rc==SQLITE_OK ){ rc = fts5CInstIterNext(&p->iter); } } - if( p->iRangeEnd>0 && iPos==p->iRangeEnd ){ - fts5HighlightAppend(&rc, p, &p->zIn[p->iOff], iEndOff - p->iOff); - p->iOff = iEndOff; - if( iPositer.iEnd ){ + if( iPos==p->iRangeEnd ){ + if( p->bOpen ){ + if( p->iter.iStart>=0 && iPos>=p->iter.iStart ){ + fts5HighlightAppend(&rc, p, &p->zIn[p->iOff], iEndOff - p->iOff); + p->iOff = iEndOff; + } fts5HighlightAppend(&rc, p, p->zClose, -1); + p->bOpen = 0; } + fts5HighlightAppend(&rc, p, &p->zIn[p->iOff], iEndOff - p->iOff); + p->iOff = iEndOff; } return rc; } + /* ** Implementation of highlight() function. */ @@ -221,15 +251,28 @@ static void fts5HighlightFunction( memset(&ctx, 0, sizeof(HighlightContext)); ctx.zOpen = (const char*)sqlite3_value_text(apVal[1]); ctx.zClose = (const char*)sqlite3_value_text(apVal[2]); + ctx.iRangeEnd = -1; rc = pApi->xColumnText(pFts, iCol, &ctx.zIn, &ctx.nIn); - - if( ctx.zIn ){ + if( rc==SQLITE_RANGE ){ + sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC); + rc = SQLITE_OK; + }else if( ctx.zIn ){ + const char *pLoc = 0; /* Locale of column iCol */ + int nLoc = 0; /* Size of pLoc in bytes */ if( rc==SQLITE_OK ){ rc = fts5CInstIterInit(pApi, pFts, iCol, &ctx.iter); } if( rc==SQLITE_OK ){ - rc = pApi->xTokenize(pFts, ctx.zIn, ctx.nIn, (void*)&ctx,fts5HighlightCb); + rc = pApi->xColumnLocale(pFts, iCol, &pLoc, &nLoc); + } + if( rc==SQLITE_OK ){ + rc = pApi->xTokenize_v2( + pFts, ctx.zIn, ctx.nIn, pLoc, nLoc, (void*)&ctx, fts5HighlightCb + ); + } + if( ctx.bOpen ){ + fts5HighlightAppend(&rc, &ctx, ctx.zClose, -1); } fts5HighlightAppend(&rc, &ctx, &ctx.zIn[ctx.iOff], ctx.nIn - ctx.iOff); @@ -246,6 +289,129 @@ static void fts5HighlightFunction( ** End of highlight() implementation. **************************************************************************/ +/* +** Context object passed to the fts5SentenceFinderCb() function. +*/ +typedef struct Fts5SFinder Fts5SFinder; +struct Fts5SFinder { + int iPos; /* Current token position */ + int nFirstAlloc; /* Allocated size of aFirst[] */ + int nFirst; /* Number of entries in aFirst[] */ + int *aFirst; /* Array of first token in each sentence */ + const char *zDoc; /* Document being tokenized */ +}; + +/* +** Add an entry to the Fts5SFinder.aFirst[] array. Grow the array if +** necessary. Return SQLITE_OK if successful, or SQLITE_NOMEM if an +** error occurs. +*/ +static int fts5SentenceFinderAdd(Fts5SFinder *p, int iAdd){ + if( p->nFirstAlloc==p->nFirst ){ + int nNew = p->nFirstAlloc ? p->nFirstAlloc*2 : 64; + int *aNew; + + aNew = (int*)sqlite3_realloc64(p->aFirst, nNew*sizeof(int)); + if( aNew==0 ) return SQLITE_NOMEM; + p->aFirst = aNew; + p->nFirstAlloc = nNew; + } + p->aFirst[p->nFirst++] = iAdd; + return SQLITE_OK; +} + +/* +** This function is an xTokenize() callback used by the auxiliary snippet() +** function. Its job is to identify tokens that are the first in a sentence. +** For each such token, an entry is added to the SFinder.aFirst[] array. +*/ +static int fts5SentenceFinderCb( + void *pContext, /* Pointer to HighlightContext object */ + int tflags, /* Mask of FTS5_TOKEN_* flags */ + const char *pToken, /* Buffer containing token */ + int nToken, /* Size of token in bytes */ + int iStartOff, /* Start offset of token */ + int iEndOff /* End offset of token */ +){ + int rc = SQLITE_OK; + + UNUSED_PARAM2(pToken, nToken); + UNUSED_PARAM(iEndOff); + + if( (tflags & FTS5_TOKEN_COLOCATED)==0 ){ + Fts5SFinder *p = (Fts5SFinder*)pContext; + if( p->iPos>0 ){ + int i; + char c = 0; + for(i=iStartOff-1; i>=0; i--){ + c = p->zDoc[i]; + if( c!=' ' && c!='\t' && c!='\n' && c!='\r' ) break; + } + if( i!=iStartOff-1 && (c=='.' || c==':') ){ + rc = fts5SentenceFinderAdd(p, p->iPos); + } + }else{ + rc = fts5SentenceFinderAdd(p, 0); + } + p->iPos++; + } + return rc; +} + +static int fts5SnippetScore( + const Fts5ExtensionApi *pApi, /* API offered by current FTS version */ + Fts5Context *pFts, /* First arg to pass to pApi functions */ + int nDocsize, /* Size of column in tokens */ + unsigned char *aSeen, /* Array with one element per query phrase */ + int iCol, /* Column to score */ + int iPos, /* Starting offset to score */ + int nToken, /* Max tokens per snippet */ + int *pnScore, /* OUT: Score */ + int *piPos /* OUT: Adjusted offset */ +){ + int rc; + int i; + int ip = 0; + int ic = 0; + int iOff = 0; + int iFirst = -1; + int nInst; + int nScore = 0; + int iLast = 0; + sqlite3_int64 iEnd = (sqlite3_int64)iPos + nToken; + + rc = pApi->xInstCount(pFts, &nInst); + for(i=0; ixInst(pFts, i, &ip, &ic, &iOff); + if( rc==SQLITE_OK && ic==iCol && iOff>=iPos && iOffxPhraseSize(pFts, ip); + } + } + + *pnScore = nScore; + if( piPos ){ + sqlite3_int64 iAdj = iFirst - (nToken - (iLast-iFirst)) / 2; + if( (iAdj+nToken)>nDocsize ) iAdj = nDocsize - nToken; + if( iAdj<0 ) iAdj = 0; + *piPos = (int)iAdj; + } + + return rc; +} + +/* +** Return the value in pVal interpreted as utf-8 text. Except, if pVal +** contains a NULL value, return a pointer to a static string zero +** bytes in length instead of a NULL pointer. +*/ +static const char *fts5ValueToText(sqlite3_value *pVal){ + const char *zRet = (const char*)sqlite3_value_text(pVal); + return zRet ? zRet : ""; +} + /* ** Implementation of snippet() function. */ @@ -267,9 +433,10 @@ static void fts5SnippetFunction( unsigned char *aSeen; /* Array of "seen instance" flags */ int iBestCol; /* Column containing best snippet */ int iBestStart = 0; /* First token of best snippet */ - int iBestLast; /* Last token of best snippet */ int nBestScore = 0; /* Score of best snippet */ int nColSize = 0; /* Total size of iBestCol in tokens */ + Fts5SFinder sFinder; /* Used to find the beginnings of sentences */ + int nCol; if( nVal!=5 ){ const char *zErr = "wrong number of arguments to function snippet()"; @@ -277,13 +444,14 @@ static void fts5SnippetFunction( return; } + nCol = pApi->xColumnCount(pFts); memset(&ctx, 0, sizeof(HighlightContext)); iCol = sqlite3_value_int(apVal[0]); - ctx.zOpen = (const char*)sqlite3_value_text(apVal[1]); - ctx.zClose = (const char*)sqlite3_value_text(apVal[2]); - zEllips = (const char*)sqlite3_value_text(apVal[3]); + ctx.zOpen = fts5ValueToText(apVal[1]); + ctx.zClose = fts5ValueToText(apVal[2]); + ctx.iRangeEnd = -1; + zEllips = fts5ValueToText(apVal[3]); nToken = sqlite3_value_int(apVal[4]); - iBestLast = nToken-1; iBestCol = (iCol>=0 ? iCol : 0); nPhrase = pApi->xPhraseCount(pFts); @@ -291,82 +459,128 @@ static void fts5SnippetFunction( if( aSeen==0 ){ rc = SQLITE_NOMEM; } - if( rc==SQLITE_OK ){ rc = pApi->xInstCount(pFts, &nInst); } - for(i=0; rc==SQLITE_OK && ixInst(pFts, i, &ip, &iSnippetCol, &iStart); - if( rc==SQLITE_OK && (iCol<0 || iSnippetCol==iCol) ){ - int nScore = 1000; - int iLast = iStart - 1 + pApi->xPhraseSize(pFts, ip); - int j; - aSeen[ip] = 1; - for(j=i+1; rc==SQLITE_OK && jxInst(pFts, j, &ip, &ic, &io); - iFinal = io + pApi->xPhraseSize(pFts, ip) - 1; - if( rc==SQLITE_OK && ic==iSnippetCol && iLastiLast ) iLast = iFinal; + memset(&sFinder, 0, sizeof(Fts5SFinder)); + for(i=0; ixColumnText(pFts, i, &sFinder.zDoc, &nDoc); + if( rc!=SQLITE_OK ) break; + rc = pApi->xColumnLocale(pFts, i, &pLoc, &nLoc); + if( rc!=SQLITE_OK ) break; + rc = pApi->xTokenize_v2(pFts, + sFinder.zDoc, nDoc, pLoc, nLoc, (void*)&sFinder, fts5SentenceFinderCb + ); + if( rc!=SQLITE_OK ) break; + rc = pApi->xColumnSize(pFts, i, &nDocsize); + if( rc!=SQLITE_OK ) break; + + for(ii=0; rc==SQLITE_OK && iixInst(pFts, ii, &ip, &ic, &io); + if( ic!=i ) continue; + if( io>nDocsize ) rc = FTS5_CORRUPT; + if( rc!=SQLITE_OK ) continue; + memset(aSeen, 0, nPhrase); + rc = fts5SnippetScore(pApi, pFts, nDocsize, aSeen, i, + io, nToken, &nScore, &iAdj + ); + if( rc==SQLITE_OK && nScore>nBestScore ){ + nBestScore = nScore; + iBestCol = i; + iBestStart = iAdj; + nColSize = nDocsize; } - } - if( rc==SQLITE_OK && nScore>nBestScore ){ - iBestCol = iSnippetCol; - iBestStart = iStart; - iBestLast = iLast; - nBestScore = nScore; + if( rc==SQLITE_OK && sFinder.nFirst && nDocsize>nToken ){ + for(jj=0; jj<(sFinder.nFirst-1); jj++){ + if( sFinder.aFirst[jj+1]>io ) break; + } + + if( sFinder.aFirst[jj]nBestScore ){ + nBestScore = nScore; + iBestCol = i; + iBestStart = sFinder.aFirst[jj]; + nColSize = nDocsize; + } + } + } } } } - if( rc==SQLITE_OK ){ - rc = pApi->xColumnSize(pFts, iBestCol, &nColSize); - } if( rc==SQLITE_OK ){ rc = pApi->xColumnText(pFts, iBestCol, &ctx.zIn, &ctx.nIn); } + if( rc==SQLITE_OK && nColSize==0 ){ + rc = pApi->xColumnSize(pFts, iBestCol, &nColSize); + } if( ctx.zIn ){ + const char *pLoc = 0; /* Locale of column iBestCol */ + int nLoc = 0; /* Bytes in pLoc */ + if( rc==SQLITE_OK ){ rc = fts5CInstIterInit(pApi, pFts, iBestCol, &ctx.iter); } - if( (iBestStart+nToken-1)>iBestLast ){ - iBestStart -= (iBestStart+nToken-1-iBestLast) / 2; - } - if( iBestStart+nToken>nColSize ){ - iBestStart = nColSize - nToken; - } - if( iBestStart<0 ) iBestStart = 0; - ctx.iRangeStart = iBestStart; ctx.iRangeEnd = iBestStart + nToken - 1; if( iBestStart>0 ){ fts5HighlightAppend(&rc, &ctx, zEllips, -1); } + + /* Advance iterator ctx.iter so that it points to the first coalesced + ** phrase instance at or following position iBestStart. */ + while( ctx.iter.iStart>=0 && ctx.iter.iStartxTokenize(pFts, ctx.zIn, ctx.nIn, (void*)&ctx,fts5HighlightCb); + rc = pApi->xColumnLocale(pFts, iBestCol, &pLoc, &nLoc); + } + if( rc==SQLITE_OK ){ + rc = pApi->xTokenize_v2( + pFts, ctx.zIn, ctx.nIn, pLoc, nLoc, (void*)&ctx,fts5HighlightCb + ); + } + if( ctx.bOpen ){ + fts5HighlightAppend(&rc, &ctx, ctx.zClose, -1); } if( ctx.iRangeEnd>=(nColSize-1) ){ fts5HighlightAppend(&rc, &ctx, &ctx.zIn[ctx.iOff], ctx.nIn - ctx.iOff); }else{ fts5HighlightAppend(&rc, &ctx, zEllips, -1); } - - if( rc==SQLITE_OK ){ - sqlite3_result_text(pCtx, (const char*)ctx.zOut, -1, SQLITE_TRANSIENT); - }else{ - sqlite3_result_error_code(pCtx, rc); - } - sqlite3_free(ctx.zOut); } + if( rc==SQLITE_OK ){ + sqlite3_result_text(pCtx, (const char*)ctx.zOut, -1, SQLITE_TRANSIENT); + }else{ + sqlite3_result_error_code(pCtx, rc); + } + sqlite3_free(ctx.zOut); sqlite3_free(aSeen); + sqlite3_free(sFinder.aFirst); } /************************************************************************/ @@ -411,22 +625,22 @@ static int fts5Bm25GetData( int rc = SQLITE_OK; /* Return code */ Fts5Bm25Data *p; /* Object to return */ - p = pApi->xGetAuxdata(pFts, 0); + p = (Fts5Bm25Data*)pApi->xGetAuxdata(pFts, 0); if( p==0 ){ int nPhrase; /* Number of phrases in query */ sqlite3_int64 nRow = 0; /* Number of rows in table */ sqlite3_int64 nToken = 0; /* Number of tokens in table */ - int nByte; /* Bytes of space to allocate */ + sqlite3_int64 nByte; /* Bytes of space to allocate */ int i; /* Allocate the Fts5Bm25Data object */ nPhrase = pApi->xPhraseCount(pFts); nByte = sizeof(Fts5Bm25Data) + nPhrase*2*sizeof(double); - p = (Fts5Bm25Data*)sqlite3_malloc(nByte); + p = (Fts5Bm25Data*)sqlite3_malloc64(nByte); if( p==0 ){ rc = SQLITE_NOMEM; }else{ - memset(p, 0, nByte); + memset(p, 0, (size_t)nByte); p->nPhrase = nPhrase; p->aIDF = (double*)&p[1]; p->aFreq = &p->aIDF[nPhrase]; @@ -434,6 +648,7 @@ static int fts5Bm25GetData( /* Calculate the average document length for this FTS5 table */ if( rc==SQLITE_OK ) rc = pApi->xRowCount(pFts, &nRow); + assert( rc!=SQLITE_OK || nRow>0 ); if( rc==SQLITE_OK ) rc = pApi->xColumnTotalSize(pFts, -1, &nToken); if( rc==SQLITE_OK ) p->avgdl = (double)nToken / (double)nRow; @@ -452,7 +667,7 @@ static int fts5Bm25GetData( ** under consideration. ** ** The problem with this is that if (N < 2*nHit), the IDF is - ** negative. Which is undesirable. So the mimimum allowable IDF is + ** negative. Which is undesirable. So the minimum allowable IDF is ** (1e-6) - roughly the same as a term that appears in just over ** half of set of 5,000,000 documents. */ double idf = log( (nRow - nHit + 0.5) / (nHit + 0.5) ); @@ -484,7 +699,7 @@ static void fts5Bm25Function( ){ const double k1 = 1.2; /* Constant "k1" from BM25 formula */ const double b = 0.75; /* Constant "b" from BM25 formula */ - int rc = SQLITE_OK; /* Error code */ + int rc; /* Error code */ double score = 0.0; /* SQL function return value */ Fts5Bm25Data *pData; /* Values allocated/calculated once only */ int i; /* Iterator variable */ @@ -516,23 +731,68 @@ static void fts5Bm25Function( D = (double)nTok; } - /* Determine the BM25 score for the current row. */ - for(i=0; rc==SQLITE_OK && inPhrase; i++){ - score += pData->aIDF[i] * ( - ( aFreq[i] * (k1 + 1.0) ) / - ( aFreq[i] + k1 * (1 - b + b * D / pData->avgdl) ) - ); - } - - /* If no error has occurred, return the calculated score. Otherwise, - ** throw an SQL exception. */ + /* Determine and return the BM25 score for the current row. Or, if an + ** error has occurred, throw an exception. */ if( rc==SQLITE_OK ){ + for(i=0; inPhrase; i++){ + score += pData->aIDF[i] * ( + ( aFreq[i] * (k1 + 1.0) ) / + ( aFreq[i] + k1 * (1 - b + b * D / pData->avgdl) ) + ); + } sqlite3_result_double(pCtx, -1.0 * score); }else{ sqlite3_result_error_code(pCtx, rc); } } +/* +** Implementation of fts5_get_locale() function. +*/ +static void fts5GetLocaleFunction( + const Fts5ExtensionApi *pApi, /* API offered by current FTS version */ + Fts5Context *pFts, /* First arg to pass to pApi functions */ + sqlite3_context *pCtx, /* Context for returning result/error */ + int nVal, /* Number of values in apVal[] array */ + sqlite3_value **apVal /* Array of trailing arguments */ +){ + int iCol = 0; + int eType = 0; + int rc = SQLITE_OK; + const char *zLocale = 0; + int nLocale = 0; + + /* xColumnLocale() must be available */ + assert( pApi->iVersion>=4 ); + + if( nVal!=1 ){ + const char *z = "wrong number of arguments to function fts5_get_locale()"; + sqlite3_result_error(pCtx, z, -1); + return; + } + + eType = sqlite3_value_numeric_type(apVal[0]); + if( eType!=SQLITE_INTEGER ){ + const char *z = "non-integer argument passed to function fts5_get_locale()"; + sqlite3_result_error(pCtx, z, -1); + return; + } + + iCol = sqlite3_value_int(apVal[0]); + if( iCol<0 || iCol>=pApi->xColumnCount(pFts) ){ + sqlite3_result_error_code(pCtx, SQLITE_RANGE); + return; + } + + rc = pApi->xColumnLocale(pFts, iCol, &zLocale, &nLocale); + if( rc!=SQLITE_OK ){ + sqlite3_result_error_code(pCtx, rc); + return; + } + + sqlite3_result_text(pCtx, zLocale, nLocale, SQLITE_TRANSIENT); +} + int sqlite3Fts5AuxInit(fts5_api *pApi){ struct Builtin { const char *zFunc; /* Function name (nul-terminated) */ @@ -540,9 +800,10 @@ int sqlite3Fts5AuxInit(fts5_api *pApi){ fts5_extension_function xFunc;/* Callback function */ void (*xDestroy)(void*); /* Destructor function */ } aBuiltin [] = { - { "snippet", 0, fts5SnippetFunction, 0 }, - { "highlight", 0, fts5HighlightFunction, 0 }, - { "bm25", 0, fts5Bm25Function, 0 }, + { "snippet", 0, fts5SnippetFunction, 0 }, + { "highlight", 0, fts5HighlightFunction, 0 }, + { "bm25", 0, fts5Bm25Function, 0 }, + { "fts5_get_locale", 0, fts5GetLocaleFunction, 0 }, }; int rc = SQLITE_OK; /* Return code */ int i; /* To iterate through builtin functions */ @@ -558,5 +819,3 @@ int sqlite3Fts5AuxInit(fts5_api *pApi){ return rc; } - - diff --git a/ext/fts5/fts5_buffer.c b/ext/fts5/fts5_buffer.c index 0098846be9..afcd83b6ba 100644 --- a/ext/fts5/fts5_buffer.c +++ b/ext/fts5/fts5_buffer.c @@ -17,17 +17,17 @@ int sqlite3Fts5BufferSize(int *pRc, Fts5Buffer *pBuf, u32 nByte){ if( (u32)pBuf->nSpacenSpace ? pBuf->nSpace : 64; + u64 nNew = pBuf->nSpace ? pBuf->nSpace : 64; u8 *pNew; while( nNewp, nNew); + pNew = sqlite3_realloc64(pBuf->p, nNew); if( pNew==0 ){ *pRc = SQLITE_NOMEM; return 1; }else{ - pBuf->nSpace = nNew; + pBuf->nSpace = (int)nNew; pBuf->p = pNew; } } @@ -52,7 +52,7 @@ void sqlite3Fts5Put32(u8 *aBuf, int iVal){ } int sqlite3Fts5Get32(const u8 *aBuf){ - return (aBuf[0] << 24) + (aBuf[1] << 16) + (aBuf[2] << 8) + aBuf[3]; + return (int)((((u32)aBuf[0])<<24) + (aBuf[1]<<16) + (aBuf[2]<<8) + aBuf[3]); } /* @@ -66,10 +66,12 @@ void sqlite3Fts5BufferAppendBlob( u32 nData, const u8 *pData ){ - assert_nc( *pRc || nData>=0 ); - if( fts5BufferGrow(pRc, pBuf, nData) ) return; - memcpy(&pBuf->p[pBuf->n], pData, nData); - pBuf->n += nData; + if( nData ){ + if( fts5BufferGrow(pRc, pBuf, nData) ) return; + assert( pBuf->p!=0 ); + memcpy(&pBuf->p[pBuf->n], pData, nData); + pBuf->n += nData; + } } /* @@ -168,21 +170,36 @@ int sqlite3Fts5PoslistNext64( i64 *piOff /* IN/OUT: Current offset */ ){ int i = *pi; + assert( a!=0 || i==0 ); if( i>=n ){ /* EOF */ *piOff = -1; return 1; }else{ i64 iOff = *piOff; - int iVal; + u32 iVal; + assert( a!=0 ); fts5FastGetVarint32(a, i, iVal); - if( iVal==1 ){ + if( iVal<=1 ){ + if( iVal==0 ){ + *pi = i; + return 0; + } fts5FastGetVarint32(a, i, iVal); iOff = ((i64)iVal) << 32; + assert( iOff>=0 ); fts5FastGetVarint32(a, i, iVal); + if( iVal<2 ){ + /* This is a corrupt record. So stop parsing it here. */ + *piOff = -1; + return 1; + } + *piOff = iOff + ((iVal-2) & 0x7FFFFFFF); + }else{ + *piOff = (iOff & (i64)0x7FFFFFFF<<32)+((iOff + (iVal-2)) & 0x7FFFFFFF); } - *piOff = iOff + (iVal-2); *pi = i; + assert_nc( *piOff>=iOff ); return 0; } } @@ -221,14 +238,16 @@ void sqlite3Fts5PoslistSafeAppend( i64 *piPrev, i64 iPos ){ - static const i64 colmask = ((i64)(0x7FFFFFFF)) << 32; - if( (iPos & colmask) != (*piPrev & colmask) ){ - pBuf->p[pBuf->n++] = 1; - pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], (iPos>>32)); - *piPrev = (iPos & colmask); + if( iPos>=*piPrev ){ + static const i64 colmask = ((i64)(0x7FFFFFFF)) << 32; + if( (iPos & colmask) != (*piPrev & colmask) ){ + pBuf->p[pBuf->n++] = 1; + pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], (iPos>>32)); + *piPrev = (iPos & colmask); + } + pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], (iPos-*piPrev)+2); + *piPrev = iPos; } - pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], (iPos-*piPrev)+2); - *piPrev = iPos; } int sqlite3Fts5PoslistWriterAppend( @@ -242,14 +261,14 @@ int sqlite3Fts5PoslistWriterAppend( return SQLITE_OK; } -void *sqlite3Fts5MallocZero(int *pRc, int nByte){ +void *sqlite3Fts5MallocZero(int *pRc, sqlite3_int64 nByte){ void *pRet = 0; if( *pRc==SQLITE_OK ){ - pRet = sqlite3_malloc(nByte); - if( pRet==0 && nByte>0 ){ - *pRc = SQLITE_NOMEM; + pRet = sqlite3_malloc64(nByte); + if( pRet==0 ){ + if( nByte>0 ) *pRc = SQLITE_NOMEM; }else{ - memset(pRet, 0, nByte); + memset(pRet, 0, (size_t)nByte); } } return pRet; @@ -289,7 +308,7 @@ char *sqlite3Fts5Strndup(int *pRc, const char *pIn, int nIn){ ** * The 52 upper and lower case ASCII characters, and ** * The 10 integer ASCII characters. ** * The underscore character "_" (0x5F). -** * The unicode "subsitute" character (0x1A). +** * The unicode "substitute" character (0x1A). */ int sqlite3Fts5IsBareword(char t){ u8 aBareword[128] = { diff --git a/ext/fts5/fts5_config.c b/ext/fts5/fts5_config.c index f49cede129..eea82b046d 100644 --- a/ext/fts5/fts5_config.c +++ b/ext/fts5/fts5_config.c @@ -18,11 +18,14 @@ #define FTS5_DEFAULT_PAGE_SIZE 4050 #define FTS5_DEFAULT_AUTOMERGE 4 +#define FTS5_DEFAULT_USERMERGE 4 #define FTS5_DEFAULT_CRISISMERGE 16 #define FTS5_DEFAULT_HASHSIZE (1024*1024) +#define FTS5_DEFAULT_DELETE_AUTOMERGE 10 /* default 10% */ + /* Maximum allowed page size */ -#define FTS5_MAX_PAGE_SIZE (128*1024) +#define FTS5_MAX_PAGE_SIZE (64*1024) static int fts5_iswhitespace(char x){ return (x==' '); @@ -149,7 +152,7 @@ static int fts5Dequote(char *z){ assert( q=='[' || q=='\'' || q=='"' || q=='`' ); if( q=='[' ) q = ']'; - while( ALWAYS(z[iIn]) ){ + while( z[iIn] ){ if( z[iIn]==q ){ if( z[iIn+1]!=q ){ /* Character iIn was the close quote. */ @@ -231,7 +234,6 @@ static int fts5ConfigSetEnum( ** eventually free any such error message using sqlite3_free(). */ static int fts5ConfigParseSpecial( - Fts5Global *pGlobal, Fts5Config *pConfig, /* Configuration object to update */ const char *zCmd, /* Special command to parse */ const char *zArg, /* Argument to parse */ @@ -239,6 +241,7 @@ static int fts5ConfigParseSpecial( ){ int rc = SQLITE_OK; int nCmd = (int)strlen(zCmd); + if( sqlite3_strnicmp("prefix", zCmd, nCmd)==0 ){ const int nByte = sizeof(int) * FTS5_MAX_PREFIX_INDEXES; const char *p; @@ -294,13 +297,12 @@ static int fts5ConfigParseSpecial( if( sqlite3_strnicmp("tokenize", zCmd, nCmd)==0 ){ const char *p = (const char*)zArg; - int nArg = (int)strlen(zArg) + 1; - char **azArg = sqlite3Fts5MallocZero(&rc, sizeof(char*) * nArg); - char *pDel = sqlite3Fts5MallocZero(&rc, nArg * 2); - char *pSpace = pDel; + sqlite3_int64 nArg = strlen(zArg) + 1; + char **azArg = sqlite3Fts5MallocZero(&rc, (sizeof(char*) + 2) * nArg); - if( azArg && pSpace ){ - if( pConfig->pTok ){ + if( azArg ){ + char *pSpace = (char*)&azArg[nArg]; + if( pConfig->t.azArg ){ *pzErr = sqlite3_mprintf("multiple tokenize=... directives"); rc = SQLITE_ERROR; }else{ @@ -323,16 +325,14 @@ static int fts5ConfigParseSpecial( *pzErr = sqlite3_mprintf("parse error in tokenize directive"); rc = SQLITE_ERROR; }else{ - rc = sqlite3Fts5GetTokenizer(pGlobal, - (const char**)azArg, nArg, &pConfig->pTok, &pConfig->pTokApi, - pzErr - ); + pConfig->t.azArg = (const char**)azArg; + pConfig->t.nArg = nArg; + azArg = 0; } } } - sqlite3_free(azArg); - sqlite3_free(pDel); + return rc; } @@ -351,6 +351,26 @@ static int fts5ConfigParseSpecial( return rc; } + if( sqlite3_strnicmp("contentless_delete", zCmd, nCmd)==0 ){ + if( (zArg[0]!='0' && zArg[0]!='1') || zArg[1]!='\0' ){ + *pzErr = sqlite3_mprintf("malformed contentless_delete=... directive"); + rc = SQLITE_ERROR; + }else{ + pConfig->bContentlessDelete = (zArg[0]=='1'); + } + return rc; + } + + if( sqlite3_strnicmp("contentless_unindexed", zCmd, nCmd)==0 ){ + if( (zArg[0]!='0' && zArg[0]!='1') || zArg[1]!='\0' ){ + *pzErr = sqlite3_mprintf("malformed contentless_delete=... directive"); + rc = SQLITE_ERROR; + }else{ + pConfig->bContentlessUnindexed = (zArg[0]=='1'); + } + return rc; + } + if( sqlite3_strnicmp("content_rowid", zCmd, nCmd)==0 ){ if( pConfig->zContentRowid ){ *pzErr = sqlite3_mprintf("multiple content_rowid=... directives"); @@ -371,6 +391,16 @@ static int fts5ConfigParseSpecial( return rc; } + if( sqlite3_strnicmp("locale", zCmd, nCmd)==0 ){ + if( (zArg[0]!='0' && zArg[0]!='1') || zArg[1]!='\0' ){ + *pzErr = sqlite3_mprintf("malformed locale=... directive"); + rc = SQLITE_ERROR; + }else{ + pConfig->bLocale = (zArg[0]=='1'); + } + return rc; + } + if( sqlite3_strnicmp("detail", zCmd, nCmd)==0 ){ const Fts5Enum aDetail[] = { { "none", FTS5_DETAIL_NONE }, @@ -385,22 +415,20 @@ static int fts5ConfigParseSpecial( return rc; } + if( sqlite3_strnicmp("tokendata", zCmd, nCmd)==0 ){ + if( (zArg[0]!='0' && zArg[0]!='1') || zArg[1]!='\0' ){ + *pzErr = sqlite3_mprintf("malformed tokendata=... directive"); + rc = SQLITE_ERROR; + }else{ + pConfig->bTokendata = (zArg[0]=='1'); + } + return rc; + } + *pzErr = sqlite3_mprintf("unrecognized option: \"%.*s\"", nCmd, zCmd); return SQLITE_ERROR; } -/* -** Allocate an instance of the default tokenizer ("simple") at -** Fts5Config.pTokenizer. Return SQLITE_OK if successful, or an SQLite error -** code if an error occurs. -*/ -static int fts5ConfigDefaultTokenizer(Fts5Global *pGlobal, Fts5Config *pConfig){ - assert( pConfig->pTok==0 && pConfig->pTokApi==0 ); - return sqlite3Fts5GetTokenizer( - pGlobal, 0, 0, &pConfig->pTok, &pConfig->pTokApi, 0 - ); -} - /* ** Gobble up the first bareword or quoted word from the input buffer zIn. ** Return a pointer to the character immediately following the last in @@ -424,8 +452,8 @@ static const char *fts5ConfigGobbleWord( ){ const char *zRet = 0; - int nIn = (int)strlen(zIn); - char *zOut = sqlite3_malloc(nIn+1); + sqlite3_int64 nIn = strlen(zIn); + char *zOut = sqlite3_malloc64(nIn+1); assert( *pRc==SQLITE_OK ); *pbQuoted = 0; @@ -434,14 +462,16 @@ static const char *fts5ConfigGobbleWord( if( zOut==0 ){ *pRc = SQLITE_NOMEM; }else{ - memcpy(zOut, zIn, nIn+1); + memcpy(zOut, zIn, (size_t)(nIn+1)); if( fts5_isopenquote(zOut[0]) ){ int ii = fts5Dequote(zOut); zRet = &zIn[ii]; *pbQuoted = 1; }else{ zRet = fts5ConfigSkipBareword(zIn); - zOut[zRet-zIn] = '\0'; + if( zRet ){ + zOut[zRet-zIn] = '\0'; + } } } @@ -458,7 +488,8 @@ static int fts5ConfigParseColumn( Fts5Config *p, char *zCol, char *zArg, - char **pzErr + char **pzErr, + int *pbUnindexed ){ int rc = SQLITE_OK; if( 0==sqlite3_stricmp(zCol, FTS5_RANK_NAME) @@ -469,6 +500,7 @@ static int fts5ConfigParseColumn( }else if( zArg ){ if( 0==sqlite3_stricmp(zArg, "unindexed") ){ p->abUnindexed[p->nCol] = 1; + *pbUnindexed = 1; }else{ *pzErr = sqlite3_mprintf("unrecognized column option: %s", zArg); rc = SQLITE_ERROR; @@ -489,11 +521,26 @@ static int fts5ConfigMakeExprlist(Fts5Config *p){ sqlite3Fts5BufferAppendPrintf(&rc, &buf, "T.%Q", p->zContentRowid); if( p->eContent!=FTS5_CONTENT_NONE ){ + assert( p->eContent==FTS5_CONTENT_EXTERNAL + || p->eContent==FTS5_CONTENT_NORMAL + || p->eContent==FTS5_CONTENT_UNINDEXED + ); for(i=0; inCol; i++){ if( p->eContent==FTS5_CONTENT_EXTERNAL ){ sqlite3Fts5BufferAppendPrintf(&rc, &buf, ", T.%Q", p->azCol[i]); - }else{ + }else if( p->eContent==FTS5_CONTENT_NORMAL || p->abUnindexed[i] ){ sqlite3Fts5BufferAppendPrintf(&rc, &buf, ", T.c%d", i); + }else{ + sqlite3Fts5BufferAppendPrintf(&rc, &buf, ", NULL"); + } + } + } + if( p->eContent==FTS5_CONTENT_NORMAL && p->bLocale ){ + for(i=0; inCol; i++){ + if( p->abUnindexed[i]==0 ){ + sqlite3Fts5BufferAppendPrintf(&rc, &buf, ", T.l%d", i); + }else{ + sqlite3Fts5BufferAppendPrintf(&rc, &buf, ", NULL"); } } } @@ -526,17 +573,19 @@ int sqlite3Fts5ConfigParse( int rc = SQLITE_OK; /* Return code */ Fts5Config *pRet; /* New object to return */ int i; - int nByte; + sqlite3_int64 nByte; + int bUnindexed = 0; /* True if there are one or more UNINDEXED */ *ppOut = pRet = (Fts5Config*)sqlite3_malloc(sizeof(Fts5Config)); if( pRet==0 ) return SQLITE_NOMEM; memset(pRet, 0, sizeof(Fts5Config)); + pRet->pGlobal = pGlobal; pRet->db = db; pRet->iCookie = -1; nByte = nArg * (sizeof(char*) + sizeof(u8)); pRet->azCol = (char**)sqlite3Fts5MallocZero(&rc, nByte); - pRet->abUnindexed = (u8*)&pRet->azCol[nArg]; + pRet->abUnindexed = pRet->azCol ? (u8*)&pRet->azCol[nArg] : 0; pRet->zDb = sqlite3Fts5Strndup(&rc, azArg[1], -1); pRet->zName = sqlite3Fts5Strndup(&rc, azArg[2], -1); pRet->bColumnsize = 1; @@ -549,6 +598,7 @@ int sqlite3Fts5ConfigParse( rc = SQLITE_ERROR; } + assert( (pRet->abUnindexed && pRet->azCol) || rc!=SQLITE_OK ); for(i=3; rc==SQLITE_OK && ipTok==0 ){ - rc = fts5ConfigDefaultTokenizer(pGlobal, pRet); + /* We only allow contentless_delete=1 if the table is indeed contentless. */ + if( rc==SQLITE_OK + && pRet->bContentlessDelete + && pRet->eContent!=FTS5_CONTENT_NONE + ){ + *pzErr = sqlite3_mprintf( + "contentless_delete=1 requires a contentless table" + ); + rc = SQLITE_ERROR; + } + + /* We only allow contentless_delete=1 if columnsize=0 is not present. + ** + ** This restriction may be removed at some point. + */ + if( rc==SQLITE_OK && pRet->bContentlessDelete && pRet->bColumnsize==0 ){ + *pzErr = sqlite3_mprintf( + "contentless_delete=1 is incompatible with columnsize=0" + ); + rc = SQLITE_ERROR; + } + + /* We only allow contentless_unindexed=1 if the table is actually a + ** contentless one. + */ + if( rc==SQLITE_OK + && pRet->bContentlessUnindexed + && pRet->eContent!=FTS5_CONTENT_NONE + ){ + *pzErr = sqlite3_mprintf( + "contentless_unindexed=1 requires a contentless table" + ); + rc = SQLITE_ERROR; } /* If no zContent option was specified, fill in the default values. */ if( rc==SQLITE_OK && pRet->zContent==0 ){ const char *zTail = 0; - assert( pRet->eContent==FTS5_CONTENT_NORMAL - || pRet->eContent==FTS5_CONTENT_NONE + assert( pRet->eContent==FTS5_CONTENT_NORMAL + || pRet->eContent==FTS5_CONTENT_NONE ); if( pRet->eContent==FTS5_CONTENT_NORMAL ){ zTail = "content"; + }else if( bUnindexed && pRet->bContentlessUnindexed ){ + pRet->eContent = FTS5_CONTENT_UNINDEXED; + zTail = "content"; }else if( pRet->bColumnsize ){ zTail = "docsize"; } @@ -637,9 +723,14 @@ int sqlite3Fts5ConfigParse( void sqlite3Fts5ConfigFree(Fts5Config *pConfig){ if( pConfig ){ int i; - if( pConfig->pTok ){ - pConfig->pTokApi->xDelete(pConfig->pTok); + if( pConfig->t.pTok ){ + if( pConfig->t.pApi1 ){ + pConfig->t.pApi1->xDelete(pConfig->t.pTok); + }else{ + pConfig->t.pApi2->xDelete(pConfig->t.pTok); + } } + sqlite3_free((char*)pConfig->t.azArg); sqlite3_free(pConfig->zDb); sqlite3_free(pConfig->zName); for(i=0; inCol; i++){ @@ -680,7 +771,7 @@ int sqlite3Fts5ConfigDeclareVtab(Fts5Config *pConfig){ rc = sqlite3_declare_vtab(pConfig->db, zSql); sqlite3_free(zSql); } - + return rc; } @@ -714,10 +805,24 @@ int sqlite3Fts5Tokenize( void *pCtx, /* Context passed to xToken() */ int (*xToken)(void*, int, const char*, int, int, int) /* Callback */ ){ - if( pText==0 ) return SQLITE_OK; - return pConfig->pTokApi->xTokenize( - pConfig->pTok, pCtx, flags, pText, nText, xToken - ); + int rc = SQLITE_OK; + if( pText ){ + if( pConfig->t.pTok==0 ){ + rc = sqlite3Fts5LoadTokenizer(pConfig); + } + if( rc==SQLITE_OK ){ + if( pConfig->t.pApi1 ){ + rc = pConfig->t.pApi1->xTokenize( + pConfig->t.pTok, pCtx, flags, pText, nText, xToken + ); + }else{ + rc = pConfig->t.pApi2->xTokenize(pConfig->t.pTok, pCtx, flags, + pText, nText, pConfig->t.pLocale, pConfig->t.nLocale, xToken + ); + } + } + } + return rc; } /* @@ -825,7 +930,7 @@ int sqlite3Fts5ConfigSetValue( if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){ pgsz = sqlite3_value_int(pVal); } - if( pgsz<=0 || pgsz>FTS5_MAX_PAGE_SIZE ){ + if( pgsz<32 || pgsz>FTS5_MAX_PAGE_SIZE ){ *pbBadkey = 1; }else{ pConfig->pgsz = pgsz; @@ -857,6 +962,18 @@ int sqlite3Fts5ConfigSetValue( } } + else if( 0==sqlite3_stricmp(zKey, "usermerge") ){ + int nUsermerge = -1; + if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){ + nUsermerge = sqlite3_value_int(pVal); + } + if( nUsermerge<2 || nUsermerge>16 ){ + *pbBadkey = 1; + }else{ + pConfig->nUsermerge = nUsermerge; + } + } + else if( 0==sqlite3_stricmp(zKey, "crisismerge") ){ int nCrisisMerge = -1; if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){ @@ -866,10 +983,23 @@ int sqlite3Fts5ConfigSetValue( *pbBadkey = 1; }else{ if( nCrisisMerge<=1 ) nCrisisMerge = FTS5_DEFAULT_CRISISMERGE; + if( nCrisisMerge>=FTS5_MAX_SEGMENT ) nCrisisMerge = FTS5_MAX_SEGMENT-1; pConfig->nCrisisMerge = nCrisisMerge; } } + else if( 0==sqlite3_stricmp(zKey, "deletemerge") ){ + int nVal = -1; + if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){ + nVal = sqlite3_value_int(pVal); + }else{ + *pbBadkey = 1; + } + if( nVal<0 ) nVal = FTS5_DEFAULT_DELETE_AUTOMERGE; + if( nVal>100 ) nVal = 0; + pConfig->nDeleteMerge = nVal; + } + else if( 0==sqlite3_stricmp(zKey, "rank") ){ const char *zIn = (const char*)sqlite3_value_text(pVal); char *zRank; @@ -884,6 +1014,31 @@ int sqlite3Fts5ConfigSetValue( rc = SQLITE_OK; *pbBadkey = 1; } + } + + else if( 0==sqlite3_stricmp(zKey, "secure-delete") ){ + int bVal = -1; + if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){ + bVal = sqlite3_value_int(pVal); + } + if( bVal<0 ){ + *pbBadkey = 1; + }else{ + pConfig->bSecureDelete = (bVal ? 1 : 0); + } + } + + else if( 0==sqlite3_stricmp(zKey, "insttoken") ){ + int bVal = -1; + if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){ + bVal = sqlite3_value_int(pVal); + } + if( bVal<0 ){ + *pbBadkey = 1; + }else{ + pConfig->bPrefixInsttoken = (bVal ? 1 : 0); + } + }else{ *pbBadkey = 1; } @@ -903,8 +1058,10 @@ int sqlite3Fts5ConfigLoad(Fts5Config *pConfig, int iCookie){ /* Set default values */ pConfig->pgsz = FTS5_DEFAULT_PAGE_SIZE; pConfig->nAutomerge = FTS5_DEFAULT_AUTOMERGE; + pConfig->nUsermerge = FTS5_DEFAULT_USERMERGE; pConfig->nCrisisMerge = FTS5_DEFAULT_CRISISMERGE; pConfig->nHashSize = FTS5_DEFAULT_HASHSIZE; + pConfig->nDeleteMerge = FTS5_DEFAULT_DELETE_AUTOMERGE; zSql = sqlite3Fts5Mprintf(&rc, zSelect, pConfig->zDb, pConfig->zName); if( zSql ){ @@ -927,15 +1084,17 @@ int sqlite3Fts5ConfigLoad(Fts5Config *pConfig, int iCookie){ rc = sqlite3_finalize(p); } - if( rc==SQLITE_OK && iVersion!=FTS5_CURRENT_VERSION ){ + if( rc==SQLITE_OK + && iVersion!=FTS5_CURRENT_VERSION + && iVersion!=FTS5_CURRENT_VERSION_SECUREDELETE + ){ rc = SQLITE_ERROR; - if( pConfig->pzErrmsg ){ - assert( 0==*pConfig->pzErrmsg ); - *pConfig->pzErrmsg = sqlite3_mprintf( - "invalid fts5 file format (found %d, expected %d) - run 'rebuild'", - iVersion, FTS5_CURRENT_VERSION - ); - } + sqlite3Fts5ConfigErrmsg(pConfig, "invalid fts5 file format " + "(found %d, expected %d or %d) - run 'rebuild'", + iVersion, FTS5_CURRENT_VERSION, FTS5_CURRENT_VERSION_SECUREDELETE + ); + }else{ + pConfig->iVersion = iVersion; } if( rc==SQLITE_OK ){ @@ -943,3 +1102,26 @@ int sqlite3Fts5ConfigLoad(Fts5Config *pConfig, int iCookie){ } return rc; } + +/* +** Set (*pConfig->pzErrmsg) to point to an sqlite3_malloc()ed buffer +** containing the error message created using printf() style formatting +** string zFmt and its trailing arguments. +*/ +void sqlite3Fts5ConfigErrmsg(Fts5Config *pConfig, const char *zFmt, ...){ + va_list ap; /* ... printf arguments */ + char *zMsg = 0; + + va_start(ap, zFmt); + zMsg = sqlite3_vmprintf(zFmt, ap); + if( pConfig->pzErrmsg ){ + assert( *pConfig->pzErrmsg==0 ); + *pConfig->pzErrmsg = zMsg; + }else{ + sqlite3_free(zMsg); + } + + va_end(ap); +} + + diff --git a/ext/fts5/fts5_expr.c b/ext/fts5/fts5_expr.c index d3f801b022..352df81f4f 100644 --- a/ext/fts5/fts5_expr.c +++ b/ext/fts5/fts5_expr.c @@ -17,6 +17,10 @@ #include "fts5Int.h" #include "fts5parse.h" +#ifndef SQLITE_FTS5_MAX_EXPR_DEPTH +# define SQLITE_FTS5_MAX_EXPR_DEPTH 256 +#endif + /* ** All token types in the generated fts5parse.h file are greater than 0. */ @@ -36,6 +40,7 @@ void sqlite3Fts5Parser(void*, int, Fts5Token, Fts5Parse*); #include void sqlite3Fts5ParserTrace(FILE*, char*); #endif +int sqlite3Fts5ParserFallback(int); struct Fts5Expr { @@ -49,18 +54,28 @@ struct Fts5Expr { /* ** eType: -** Expression node type. Always one of: +** Expression node type. Usually one of: ** ** FTS5_AND (nChild, apChild valid) ** FTS5_OR (nChild, apChild valid) ** FTS5_NOT (nChild, apChild valid) ** FTS5_STRING (pNear valid) ** FTS5_TERM (pNear valid) +** +** An expression node with eType==0 may also exist. It always matches zero +** rows. This is created when a phrase containing no tokens is parsed. +** e.g. "". +** +** iHeight: +** Distance from this node to furthest leaf. This is always 0 for nodes +** of type FTS5_STRING and FTS5_TERM. For all other nodes it is one +** greater than the largest child value. */ struct Fts5ExprNode { int eType; /* Node type */ int bEof; /* True at EOF */ int bNomatch; /* True if entry is not a match */ + int iHeight; /* Distance to tree leaf nodes */ /* Next method for this node. */ int (*xNext)(Fts5Expr*, Fts5ExprNode*, int, i64); @@ -71,9 +86,13 @@ struct Fts5ExprNode { /* Child nodes. For a NOT node, this array always contains 2 entries. For ** AND or OR nodes, it contains 2 or more entries. */ int nChild; /* Number of child nodes */ - Fts5ExprNode *apChild[1]; /* Array of child nodes */ + Fts5ExprNode *apChild[FLEXARRAY]; /* Array of child nodes */ }; +/* Size (in bytes) of an Fts5ExprNode object that holds up to N children */ +#define SZ_FTS5EXPRNODE(N) \ + (offsetof(Fts5ExprNode,apChild) + (N)*sizeof(Fts5ExprNode*)) + #define Fts5NodeIsString(p) ((p)->eType==FTS5_TERM || (p)->eType==FTS5_STRING) /* @@ -87,8 +106,11 @@ struct Fts5ExprNode { ** or term prefix. */ struct Fts5ExprTerm { - int bPrefix; /* True for a prefix term */ - char *zTerm; /* nul-terminated term */ + u8 bPrefix; /* True for a prefix term */ + u8 bFirst; /* True if token must be first in column */ + char *pTerm; /* Term data */ + int nQueryTerm; /* Effective size of term in bytes */ + int nFullTerm; /* Size of term in bytes incl. tokendata */ Fts5IndexIter *pIter; /* Iterator for this term */ Fts5ExprTerm *pSynonym; /* Pointer to first in list of synonyms */ }; @@ -101,9 +123,13 @@ struct Fts5ExprPhrase { Fts5ExprNode *pNode; /* FTS5_STRING node this phrase is part of */ Fts5Buffer poslist; /* Current position list */ int nTerm; /* Number of entries in aTerm[] */ - Fts5ExprTerm aTerm[1]; /* Terms that make up this phrase */ + Fts5ExprTerm aTerm[FLEXARRAY]; /* Terms that make up this phrase */ }; +/* Size (in bytes) of an Fts5ExprPhrase object that holds up to N terms */ +#define SZ_FTS5EXPRPHRASE(N) \ + (offsetof(Fts5ExprPhrase,aTerm) + (N)*sizeof(Fts5ExprTerm)) + /* ** One or more phrases that must appear within a certain token distance of ** each other within each matching document. @@ -112,9 +138,12 @@ struct Fts5ExprNearset { int nNear; /* NEAR parameter */ Fts5Colset *pColset; /* Columns to search (NULL -> all columns) */ int nPhrase; /* Number of entries in aPhrase[] array */ - Fts5ExprPhrase *apPhrase[1]; /* Array of phrase pointers */ + Fts5ExprPhrase *apPhrase[FLEXARRAY]; /* Array of phrase pointers */ }; +/* Size (in bytes) of an Fts5ExprNearset object covering up to N phrases */ +#define SZ_FTS5EXPRNEARSET(N) \ + (offsetof(Fts5ExprNearset,apPhrase)+(N)*sizeof(Fts5ExprPhrase*)) /* ** Parse context. @@ -126,12 +155,39 @@ struct Fts5Parse { int nPhrase; /* Size of apPhrase array */ Fts5ExprPhrase **apPhrase; /* Array of all phrases */ Fts5ExprNode *pExpr; /* Result of a successful parse */ + int bPhraseToAnd; /* Convert "a+b" to "a AND b" */ }; +/* +** Check that the Fts5ExprNode.iHeight variables are set correctly in +** the expression tree passed as the only argument. +*/ +#ifndef NDEBUG +static void assert_expr_depth_ok(int rc, Fts5ExprNode *p){ + if( rc==SQLITE_OK ){ + if( p->eType==FTS5_TERM || p->eType==FTS5_STRING || p->eType==0 ){ + assert( p->iHeight==0 ); + }else{ + int ii; + int iMaxChild = 0; + for(ii=0; iinChild; ii++){ + Fts5ExprNode *pChild = p->apChild[ii]; + iMaxChild = MAX(iMaxChild, pChild->iHeight); + assert_expr_depth_ok(SQLITE_OK, pChild); + } + assert( p->iHeight==iMaxChild+1 ); + } + } +} +#else +# define assert_expr_depth_ok(rc, p) +#endif + void sqlite3Fts5ParseError(Fts5Parse *pParse, const char *zFmt, ...){ va_list ap; va_start(ap, zFmt); if( pParse->rc==SQLITE_OK ){ + assert( pParse->zErr==0 ); pParse->zErr = sqlite3_vmprintf(zFmt, ap); pParse->rc = SQLITE_ERROR; } @@ -167,6 +223,8 @@ static int fts5ExprGetToken( case ',': tok = FTS5_COMMA; break; case '+': tok = FTS5_PLUS; break; case '*': tok = FTS5_STAR; break; + case '-': tok = FTS5_MINUS; break; + case '^': tok = FTS5_CARET; break; case '\0': tok = FTS5_EOF; break; case '"': { @@ -207,11 +265,13 @@ static int fts5ExprGetToken( return tok; } -static void *fts5ParseAlloc(u64 t){ return sqlite3_malloc((int)t); } +static void *fts5ParseAlloc(u64 t){ return sqlite3_malloc64((sqlite3_int64)t);} static void fts5ParseFree(void *p){ sqlite3_free(p); } int sqlite3Fts5ExprNew( Fts5Config *pConfig, /* FTS5 Configuration */ + int bPhraseToAnd, + int iCol, const char *zExpr, /* Expression text */ Fts5Expr **ppNew, char **pzErr @@ -226,6 +286,7 @@ int sqlite3Fts5ExprNew( *ppNew = 0; *pzErr = 0; memset(&sParse, 0, sizeof(sParse)); + sParse.bPhraseToAnd = bPhraseToAnd; pEngine = sqlite3Fts5ParserAlloc(fts5ParseAlloc); if( pEngine==0 ){ return SQLITE_NOMEM; } sParse.pConfig = pConfig; @@ -236,6 +297,21 @@ int sqlite3Fts5ExprNew( }while( sParse.rc==SQLITE_OK && t!=FTS5_EOF ); sqlite3Fts5ParserFree(pEngine, fts5ParseFree); + assert( sParse.pExpr || sParse.rc!=SQLITE_OK ); + assert_expr_depth_ok(sParse.rc, sParse.pExpr); + + /* If the LHS of the MATCH expression was a user column, apply the + ** implicit column-filter. */ + if( sParse.rc==SQLITE_OK && iColnCol ){ + int n = SZ_FTS5COLSET(1); + Fts5Colset *pColset = (Fts5Colset*)sqlite3Fts5MallocZero(&sParse.rc, n); + if( pColset ){ + pColset->nCol = 1; + pColset->aiCol[0] = iCol; + sqlite3Fts5ParseSetColset(&sParse, sParse.pExpr, pColset); + } + } + assert( sParse.rc!=SQLITE_OK || sParse.zErr==0 ); if( sParse.rc==SQLITE_OK ){ *ppNew = pNew = sqlite3_malloc(sizeof(Fts5Expr)); @@ -243,28 +319,116 @@ int sqlite3Fts5ExprNew( sParse.rc = SQLITE_NOMEM; sqlite3Fts5ParseNodeFree(sParse.pExpr); }else{ - if( !sParse.pExpr ){ - const int nByte = sizeof(Fts5ExprNode); - pNew->pRoot = (Fts5ExprNode*)sqlite3Fts5MallocZero(&sParse.rc, nByte); - if( pNew->pRoot ){ - pNew->pRoot->bEof = 1; - } - }else{ - pNew->pRoot = sParse.pExpr; - } + pNew->pRoot = sParse.pExpr; pNew->pIndex = 0; pNew->pConfig = pConfig; pNew->apExprPhrase = sParse.apPhrase; pNew->nPhrase = sParse.nPhrase; + pNew->bDesc = 0; sParse.apPhrase = 0; } + }else{ + sqlite3Fts5ParseNodeFree(sParse.pExpr); } sqlite3_free(sParse.apPhrase); - *pzErr = sParse.zErr; + if( 0==*pzErr ){ + *pzErr = sParse.zErr; + }else{ + sqlite3_free(sParse.zErr); + } return sParse.rc; } +/* +** Assuming that buffer z is at least nByte bytes in size and contains a +** valid utf-8 string, return the number of characters in the string. +*/ +static int fts5ExprCountChar(const char *z, int nByte){ + int nRet = 0; + int ii; + for(ii=0; ii=3 ){ + int jj; + zExpr[iOut++] = '"'; + for(jj=iFirst; jj0 ){ + int bAnd = 0; + if( pConfig->eDetail!=FTS5_DETAIL_FULL ){ + bAnd = 1; + if( pConfig->eDetail==FTS5_DETAIL_NONE ){ + iCol = pConfig->nCol; + } + } + zExpr[iOut] = '\0'; + rc = sqlite3Fts5ExprNew(pConfig, bAnd, iCol, zExpr, pp,pConfig->pzErrmsg); + }else{ + *pp = 0; + } + sqlite3_free(zExpr); + } + + return rc; +} + /* ** Free the expression node object passed as the only argument. */ @@ -290,6 +454,42 @@ void sqlite3Fts5ExprFree(Fts5Expr *p){ } } +int sqlite3Fts5ExprAnd(Fts5Expr **pp1, Fts5Expr *p2){ + Fts5Parse sParse; + memset(&sParse, 0, sizeof(sParse)); + + if( *pp1 && p2 ){ + Fts5Expr *p1 = *pp1; + int nPhrase = p1->nPhrase + p2->nPhrase; + + p1->pRoot = sqlite3Fts5ParseNode(&sParse, FTS5_AND, p1->pRoot, p2->pRoot,0); + p2->pRoot = 0; + + if( sParse.rc==SQLITE_OK ){ + Fts5ExprPhrase **ap = (Fts5ExprPhrase**)sqlite3_realloc( + p1->apExprPhrase, nPhrase * sizeof(Fts5ExprPhrase*) + ); + if( ap==0 ){ + sParse.rc = SQLITE_NOMEM; + }else{ + int i; + memmove(&ap[p2->nPhrase], ap, p1->nPhrase*sizeof(Fts5ExprPhrase*)); + for(i=0; inPhrase; i++){ + ap[i] = p2->apExprPhrase[i]; + } + p1->nPhrase = nPhrase; + p1->apExprPhrase = ap; + } + } + sqlite3_free(p2->apExprPhrase); + sqlite3_free(p2); + }else if( p2 ){ + *pp1 = p2; + } + + return sParse.rc; +} + /* ** Argument pTerm must be a synonym iterator. Return the current rowid ** that it points to. @@ -299,6 +499,7 @@ static i64 fts5ExprSynonymRowid(Fts5ExprTerm *pTerm, int bDesc, int *pbEof){ int bRetValid = 0; Fts5ExprTerm *p; + assert( pTerm ); assert( pTerm->pSynonym ); assert( bDesc==0 || bDesc==1 ); for(p=pTerm; p; p=p->pSynonym){ @@ -337,8 +538,8 @@ static int fts5ExprSynonymList( if( sqlite3Fts5IterEof(pIter)==0 && pIter->iRowid==iRowid ){ if( pIter->nData==0 ) continue; if( nIter==nAlloc ){ - int nByte = sizeof(Fts5PoslistReader) * nAlloc * 2; - Fts5PoslistReader *aNew = (Fts5PoslistReader*)sqlite3_malloc(nByte); + sqlite3_int64 nByte = sizeof(Fts5PoslistReader) * nAlloc * 2; + Fts5PoslistReader *aNew = (Fts5PoslistReader*)sqlite3_malloc64(nByte); if( aNew==0 ){ rc = SQLITE_NOMEM; goto synonym_poslist_out; @@ -411,14 +612,15 @@ static int fts5ExprPhraseIsMatch( Fts5PoslistReader *aIter = aStatic; int i; int rc = SQLITE_OK; + int bFirst = pPhrase->aTerm[0].bFirst; fts5BufferZero(&pPhrase->poslist); /* If the aStatic[] array is not large enough, allocate a large array ** using sqlite3_malloc(). This approach could be improved upon. */ if( pPhrase->nTerm>ArraySize(aStatic) ){ - int nByte = sizeof(Fts5PoslistReader) * pPhrase->nTerm; - aIter = (Fts5PoslistReader*)sqlite3_malloc(nByte); + sqlite3_int64 nByte = sizeof(Fts5PoslistReader) * pPhrase->nTerm; + aIter = (Fts5PoslistReader*)sqlite3_malloc64(nByte); if( !aIter ) return SQLITE_NOMEM; } memset(aIter, 0, sizeof(Fts5PoslistReader) * pPhrase->nTerm); @@ -465,8 +667,10 @@ static int fts5ExprPhraseIsMatch( }while( bMatch==0 ); /* Append position iPos to the output */ - rc = sqlite3Fts5PoslistWriterAppend(&pPhrase->poslist, &writer, iPos); - if( rc!=SQLITE_OK ) goto ismatch_out; + if( bFirst==0 || FTS5_POS2OFFSET(iPos)==0 ){ + rc = sqlite3Fts5PoslistWriterAppend(&pPhrase->poslist, &writer, iPos); + if( rc!=SQLITE_OK ) goto ismatch_out; + } for(i=0; inTerm; i++){ if( sqlite3Fts5PoslistReaderNext(&aIter[i]) ) goto ismatch_out; @@ -550,7 +754,7 @@ static int fts5ExprNearIsMatch(int *pRc, Fts5ExprNearset *pNear){ /* If the aStatic[] array is not large enough, allocate a large array ** using sqlite3_malloc(). This approach could be improved upon. */ if( pNear->nPhrase>ArraySize(aStatic) ){ - int nByte = sizeof(Fts5NearTrimmer) * pNear->nPhrase; + sqlite3_int64 nByte = sizeof(Fts5NearTrimmer) * pNear->nPhrase; a = (Fts5NearTrimmer*)sqlite3Fts5MallocZero(&rc, nByte); }else{ memset(aStatic, 0, sizeof(aStatic)); @@ -720,7 +924,9 @@ static int fts5ExprNearTest( ** phrase is not a match, break out of the loop early. */ for(i=0; rc==SQLITE_OK && inPhrase; i++){ Fts5ExprPhrase *pPhrase = pNear->apPhrase[i]; - if( pPhrase->nTerm>1 || pPhrase->aTerm[0].pSynonym || pNear->pColset ){ + if( pPhrase->nTerm>1 || pPhrase->aTerm[0].pSynonym + || pNear->pColset || pPhrase->aTerm[0].bFirst + ){ int bMatch = 0; rc = fts5ExprPhraseIsMatch(pNode, pPhrase, &bMatch); if( bMatch==0 ) break; @@ -743,49 +949,61 @@ static int fts5ExprNearTest( ** Initialize all term iterators in the pNear object. If any term is found ** to match no documents at all, return immediately without initializing any ** further iterators. +** +** If an error occurs, return an SQLite error code. Otherwise, return +** SQLITE_OK. It is not considered an error if some term matches zero +** documents. */ static int fts5ExprNearInitAll( Fts5Expr *pExpr, Fts5ExprNode *pNode ){ Fts5ExprNearset *pNear = pNode->pNear; - int i, j; - int rc = SQLITE_OK; + int i; assert( pNode->bNomatch==0 ); - for(i=0; rc==SQLITE_OK && inPhrase; i++){ + for(i=0; inPhrase; i++){ Fts5ExprPhrase *pPhrase = pNear->apPhrase[i]; - for(j=0; jnTerm; j++){ - Fts5ExprTerm *pTerm = &pPhrase->aTerm[j]; - Fts5ExprTerm *p; - int bEof = 1; - - for(p=pTerm; p && rc==SQLITE_OK; p=p->pSynonym){ - if( p->pIter ){ - sqlite3Fts5IterClose(p->pIter); - p->pIter = 0; - } - rc = sqlite3Fts5IndexQuery( - pExpr->pIndex, p->zTerm, (int)strlen(p->zTerm), - (pTerm->bPrefix ? FTS5INDEX_QUERY_PREFIX : 0) | - (pExpr->bDesc ? FTS5INDEX_QUERY_DESC : 0), - pNear->pColset, - &p->pIter - ); - assert( rc==SQLITE_OK || p->pIter==0 ); - if( p->pIter && 0==sqlite3Fts5IterEof(p->pIter) ){ - bEof = 0; + if( pPhrase->nTerm==0 ){ + pNode->bEof = 1; + return SQLITE_OK; + }else{ + int j; + for(j=0; jnTerm; j++){ + Fts5ExprTerm *pTerm = &pPhrase->aTerm[j]; + Fts5ExprTerm *p; + int bHit = 0; + + for(p=pTerm; p; p=p->pSynonym){ + int rc; + if( p->pIter ){ + sqlite3Fts5IterClose(p->pIter); + p->pIter = 0; + } + rc = sqlite3Fts5IndexQuery( + pExpr->pIndex, p->pTerm, p->nQueryTerm, + (pTerm->bPrefix ? FTS5INDEX_QUERY_PREFIX : 0) | + (pExpr->bDesc ? FTS5INDEX_QUERY_DESC : 0), + pNear->pColset, + &p->pIter + ); + assert( (rc==SQLITE_OK)==(p->pIter!=0) ); + if( rc!=SQLITE_OK ) return rc; + if( 0==sqlite3Fts5IterEof(p->pIter) ){ + bHit = 1; + } } - } - if( bEof ){ - pNode->bEof = 1; - return rc; + if( bHit==0 ){ + pNode->bEof = 1; + return SQLITE_OK; + } } } } - return rc; + pNode->bEof = 0; + return SQLITE_OK; } /* @@ -889,6 +1107,7 @@ static int fts5ExprNodeTest_STRING( assert( pNear->nPhrase>1 || pNear->apPhrase[0]->nTerm>1 || pNear->apPhrase[0]->aTerm[0].pSynonym + || pNear->apPhrase[0]->aTerm[0].bFirst ); /* Initialize iLast, the "lastest" rowid any iterator points to. If the @@ -1095,7 +1314,10 @@ static int fts5ExprNodeNext_OR( || (bFromValid && fts5RowidCmp(pExpr, p1->iRowid, iFrom)<0) ){ int rc = fts5ExprNodeNext(pExpr, p1, bFromValid, iFrom); - if( rc!=SQLITE_OK ) return rc; + if( rc!=SQLITE_OK ){ + pNode->bNomatch = 0; + return rc; + } } } } @@ -1126,7 +1348,10 @@ static int fts5ExprNodeTest_AND( if( cmp>0 ){ /* Advance pChild until it points to iLast or laster */ rc = fts5ExprNodeNext(pExpr, pChild, 1, iLast); - if( rc!=SQLITE_OK ) return rc; + if( rc!=SQLITE_OK ){ + pAnd->bNomatch = 0; + return rc; + } } /* If the child node is now at EOF, so is the parent AND node. Otherwise, @@ -1165,6 +1390,8 @@ static int fts5ExprNodeNext_AND( int rc = fts5ExprNodeNext(pExpr, pNode->apChild[0], bFromValid, iFrom); if( rc==SQLITE_OK ){ rc = fts5ExprNodeTest_AND(pExpr, pNode); + }else{ + pNode->bNomatch = 0; } return rc; } @@ -1207,6 +1434,9 @@ static int fts5ExprNodeNext_NOT( if( rc==SQLITE_OK ){ rc = fts5ExprNodeTest_NOT(pExpr, pNode); } + if( rc!=SQLITE_OK ){ + pNode->bNomatch = 0; + } return rc; } @@ -1268,6 +1498,8 @@ static int fts5ExprNodeFirst(Fts5Expr *pExpr, Fts5ExprNode *pNode){ if( Fts5NodeIsString(pNode) ){ /* Initialize all term iterators in the NEAR object. */ rc = fts5ExprNearInitAll(pExpr, pNode); + }else if( pNode->xNext==0 ){ + pNode->bEof = 1; }else{ int i; int nEof = 0; @@ -1317,25 +1549,36 @@ static int fts5ExprNodeFirst(Fts5Expr *pExpr, Fts5ExprNode *pNode){ ** Return SQLITE_OK if successful, or an SQLite error code otherwise. It ** is not considered an error if the query does not match any documents. */ -int sqlite3Fts5ExprFirst(Fts5Expr *p, Fts5Index *pIdx, i64 iFirst, int bDesc){ +int sqlite3Fts5ExprFirst( + Fts5Expr *p, + Fts5Index *pIdx, + i64 iFirst, + i64 iLast, + int bDesc +){ Fts5ExprNode *pRoot = p->pRoot; - int rc = SQLITE_OK; - if( pRoot->xNext ){ - p->pIndex = pIdx; - p->bDesc = bDesc; - rc = fts5ExprNodeFirst(p, pRoot); + int rc; /* Return code */ - /* If not at EOF but the current rowid occurs earlier than iFirst in - ** the iteration order, move to document iFirst or later. */ - if( pRoot->bEof==0 && fts5RowidCmp(p, pRoot->iRowid, iFirst)<0 ){ - rc = fts5ExprNodeNext(p, pRoot, 1, iFirst); - } + p->pIndex = pIdx; + p->bDesc = bDesc; + rc = fts5ExprNodeFirst(p, pRoot); - /* If the iterator is not at a real match, skip forward until it is. */ - while( pRoot->bNomatch ){ - assert( pRoot->bEof==0 && rc==SQLITE_OK ); - rc = fts5ExprNodeNext(p, pRoot, 0, 0); - } + /* If not at EOF but the current rowid occurs earlier than iFirst in + ** the iteration order, move to document iFirst or later. */ + if( rc==SQLITE_OK + && 0==pRoot->bEof + && fts5RowidCmp(p, pRoot->iRowid, iFirst)<0 + ){ + rc = fts5ExprNodeNext(p, pRoot, 1, iFirst); + } + + /* If the iterator is not at a real match, skip forward until it is. */ + while( pRoot->bNomatch && rc==SQLITE_OK ){ + assert( pRoot->bEof==0 ); + rc = fts5ExprNodeNext(p, pRoot, 0, 0); + } + if( fts5RowidCmp(p, pRoot->iRowid, iLast)>0 ){ + pRoot->bEof = 1; } return rc; } @@ -1384,7 +1627,7 @@ static void fts5ExprPhraseFree(Fts5ExprPhrase *pPhrase){ Fts5ExprTerm *pSyn; Fts5ExprTerm *pNext; Fts5ExprTerm *pTerm = &pPhrase->aTerm[i]; - sqlite3_free(pTerm->zTerm); + sqlite3_free(pTerm->pTerm); sqlite3Fts5IterClose(pTerm->pIter); for(pSyn=pTerm->pSynonym; pSyn; pSyn=pNext){ pNext = pSyn->pSynonym; @@ -1398,6 +1641,16 @@ static void fts5ExprPhraseFree(Fts5ExprPhrase *pPhrase){ } } +/* +** Set the "bFirst" flag on the first token of the phrase passed as the +** only argument. +*/ +void sqlite3Fts5ParseSetCaret(Fts5ExprPhrase *pPhrase){ + if( pPhrase && pPhrase->nTerm ){ + pPhrase->aTerm[0].bFirst = 1; + } +} + /* ** If argument pNear is NULL, then a new Fts5ExprNearset object is allocated ** and populated with pPhrase. Or, if pNear is not NULL, phrase pPhrase is @@ -1415,22 +1668,21 @@ Fts5ExprNearset *sqlite3Fts5ParseNearset( Fts5ExprNearset *pRet = 0; if( pParse->rc==SQLITE_OK ){ - if( pPhrase==0 ){ - return pNear; - } if( pNear==0 ){ - int nByte = sizeof(Fts5ExprNearset) + SZALLOC * sizeof(Fts5ExprPhrase*); - pRet = sqlite3_malloc(nByte); + sqlite3_int64 nByte; + nByte = SZ_FTS5EXPRNEARSET(SZALLOC+1); + pRet = sqlite3_malloc64(nByte); if( pRet==0 ){ pParse->rc = SQLITE_NOMEM; }else{ - memset(pRet, 0, nByte); + memset(pRet, 0, (size_t)nByte); } }else if( (pNear->nPhrase % SZALLOC)==0 ){ int nNew = pNear->nPhrase + SZALLOC; - int nByte = sizeof(Fts5ExprNearset) + nNew * sizeof(Fts5ExprPhrase*); + sqlite3_int64 nByte; - pRet = (Fts5ExprNearset*)sqlite3_realloc(pNear, nByte); + nByte = SZ_FTS5EXPRNEARSET(nNew+1); + pRet = (Fts5ExprNearset*)sqlite3_realloc64(pNear, nByte); if( pRet==0 ){ pParse->rc = SQLITE_NOMEM; } @@ -1444,6 +1696,24 @@ Fts5ExprNearset *sqlite3Fts5ParseNearset( sqlite3Fts5ParseNearsetFree(pNear); sqlite3Fts5ParsePhraseFree(pPhrase); }else{ + if( pRet->nPhrase>0 ){ + Fts5ExprPhrase *pLast = pRet->apPhrase[pRet->nPhrase-1]; + assert( pParse!=0 ); + assert( pParse->apPhrase!=0 ); + assert( pParse->nPhrase>=2 ); + assert( pLast==pParse->apPhrase[pParse->nPhrase-2] ); + if( pPhrase->nTerm==0 ){ + fts5ExprPhraseFree(pPhrase); + pRet->nPhrase--; + pParse->nPhrase--; + pPhrase = pLast; + }else if( pLast->nTerm==0 ){ + fts5ExprPhraseFree(pLast); + pParse->apPhrase[pParse->nPhrase-2] = pPhrase; + pParse->nPhrase--; + pRet->nPhrase--; + } + } pRet->apPhrase[pRet->nPhrase++] = pPhrase; } return pRet; @@ -1452,6 +1722,7 @@ Fts5ExprNearset *sqlite3Fts5ParseNearset( typedef struct TokenCtx TokenCtx; struct TokenCtx { Fts5ExprPhrase *pPhrase; + Fts5Config *pConfig; int rc; }; @@ -1475,18 +1746,22 @@ static int fts5ParseTokenize( /* If an error has already occurred, this is a no-op */ if( pCtx->rc!=SQLITE_OK ) return pCtx->rc; + if( nToken>FTS5_MAX_TOKEN_SIZE ) nToken = FTS5_MAX_TOKEN_SIZE; - assert( pPhrase==0 || pPhrase->nTerm>0 ); - if( pPhrase && (tflags & FTS5_TOKEN_COLOCATED) ){ + if( pPhrase && pPhrase->nTerm>0 && (tflags & FTS5_TOKEN_COLOCATED) ){ Fts5ExprTerm *pSyn; - int nByte = sizeof(Fts5ExprTerm) + sizeof(Fts5Buffer) + nToken+1; - pSyn = (Fts5ExprTerm*)sqlite3_malloc(nByte); + sqlite3_int64 nByte = sizeof(Fts5ExprTerm) + sizeof(Fts5Buffer) + nToken+1; + pSyn = (Fts5ExprTerm*)sqlite3_malloc64(nByte); if( pSyn==0 ){ rc = SQLITE_NOMEM; }else{ - memset(pSyn, 0, nByte); - pSyn->zTerm = ((char*)pSyn) + sizeof(Fts5ExprTerm) + sizeof(Fts5Buffer); - memcpy(pSyn->zTerm, pToken, nToken); + memset(pSyn, 0, (size_t)nByte); + pSyn->pTerm = ((char*)pSyn) + sizeof(Fts5ExprTerm) + sizeof(Fts5Buffer); + pSyn->nFullTerm = pSyn->nQueryTerm = nToken; + if( pCtx->pConfig->bTokendata ){ + pSyn->nQueryTerm = (int)strlen(pSyn->pTerm); + } + memcpy(pSyn->pTerm, pToken, nToken); pSyn->pSynonym = pPhrase->aTerm[pPhrase->nTerm-1].pSynonym; pPhrase->aTerm[pPhrase->nTerm-1].pSynonym = pSyn; } @@ -1496,13 +1771,13 @@ static int fts5ParseTokenize( Fts5ExprPhrase *pNew; int nNew = SZALLOC + (pPhrase ? pPhrase->nTerm : 0); - pNew = (Fts5ExprPhrase*)sqlite3_realloc(pPhrase, - sizeof(Fts5ExprPhrase) + sizeof(Fts5ExprTerm) * nNew + pNew = (Fts5ExprPhrase*)sqlite3_realloc64(pPhrase, + SZ_FTS5EXPRPHRASE(nNew+1) ); if( pNew==0 ){ rc = SQLITE_NOMEM; }else{ - if( pPhrase==0 ) memset(pNew, 0, sizeof(Fts5ExprPhrase)); + if( pPhrase==0 ) memset(pNew, 0, SZ_FTS5EXPRPHRASE(1)); pCtx->pPhrase = pPhrase = pNew; pNew->nTerm = nNew - SZALLOC; } @@ -1511,7 +1786,11 @@ static int fts5ParseTokenize( if( rc==SQLITE_OK ){ pTerm = &pPhrase->aTerm[pPhrase->nTerm++]; memset(pTerm, 0, sizeof(Fts5ExprTerm)); - pTerm->zTerm = sqlite3Fts5Strndup(&rc, pToken, nToken); + pTerm->pTerm = sqlite3Fts5Strndup(&rc, pToken, nToken); + pTerm->nFullTerm = pTerm->nQueryTerm = nToken; + if( pCtx->pConfig->bTokendata && rc==SQLITE_OK ){ + pTerm->nQueryTerm = (int)strlen(pTerm->pTerm); + } } } @@ -1546,6 +1825,20 @@ void sqlite3Fts5ParseFinished(Fts5Parse *pParse, Fts5ExprNode *p){ pParse->pExpr = p; } +static int parseGrowPhraseArray(Fts5Parse *pParse){ + if( (pParse->nPhrase % 8)==0 ){ + sqlite3_int64 nByte = sizeof(Fts5ExprPhrase*) * (pParse->nPhrase + 8); + Fts5ExprPhrase **apNew; + apNew = (Fts5ExprPhrase**)sqlite3_realloc64(pParse->apPhrase, nByte); + if( apNew==0 ){ + pParse->rc = SQLITE_NOMEM; + return SQLITE_NOMEM; + } + pParse->apPhrase = apNew; + } + return SQLITE_OK; +} + /* ** This function is called by the parser to process a string token. The ** string may or may not be quoted. In any case it is tokenized and a @@ -1564,10 +1857,11 @@ Fts5ExprPhrase *sqlite3Fts5ParseTerm( memset(&sCtx, 0, sizeof(TokenCtx)); sCtx.pPhrase = pAppend; + sCtx.pConfig = pConfig; rc = fts5ParseStringFromToken(pToken, &z); if( rc==SQLITE_OK ){ - int flags = FTS5_TOKENIZE_QUERY | (bPrefix ? FTS5_TOKENIZE_QUERY : 0); + int flags = FTS5_TOKENIZE_QUERY | (bPrefix ? FTS5_TOKENIZE_PREFIX : 0); int n; sqlite3Fts5Dequote(z); n = (int)strlen(z); @@ -1578,26 +1872,25 @@ Fts5ExprPhrase *sqlite3Fts5ParseTerm( pParse->rc = rc; fts5ExprPhraseFree(sCtx.pPhrase); sCtx.pPhrase = 0; - }else if( sCtx.pPhrase ){ + }else{ if( pAppend==0 ){ - if( (pParse->nPhrase % 8)==0 ){ - int nByte = sizeof(Fts5ExprPhrase*) * (pParse->nPhrase + 8); - Fts5ExprPhrase **apNew; - apNew = (Fts5ExprPhrase**)sqlite3_realloc(pParse->apPhrase, nByte); - if( apNew==0 ){ - pParse->rc = SQLITE_NOMEM; - fts5ExprPhraseFree(sCtx.pPhrase); - return 0; - } - pParse->apPhrase = apNew; + if( parseGrowPhraseArray(pParse) ){ + fts5ExprPhraseFree(sCtx.pPhrase); + return 0; } pParse->nPhrase++; } + if( sCtx.pPhrase==0 ){ + /* This happens when parsing a token or quoted phrase that contains + ** no token characters at all. (e.g ... MATCH '""'). */ + sCtx.pPhrase = sqlite3Fts5MallocZero(&pParse->rc, SZ_FTS5EXPRPHRASE(1)); + }else if( sCtx.pPhrase->nTerm ){ + sCtx.pPhrase->aTerm[sCtx.pPhrase->nTerm-1].bPrefix = (u8)bPrefix; + } + assert( pParse->apPhrase!=0 ); pParse->apPhrase[pParse->nPhrase-1] = sCtx.pPhrase; - assert( sCtx.pPhrase->nTerm>0 ); - sCtx.pPhrase->aTerm[sCtx.pPhrase->nTerm-1].bPrefix = bPrefix; } return sCtx.pPhrase; @@ -1613,41 +1906,64 @@ int sqlite3Fts5ExprClonePhrase( Fts5Expr **ppNew ){ int rc = SQLITE_OK; /* Return code */ - Fts5ExprPhrase *pOrig; /* The phrase extracted from pExpr */ - int i; /* Used to iterate through phrase terms */ + Fts5ExprPhrase *pOrig = 0; /* The phrase extracted from pExpr */ Fts5Expr *pNew = 0; /* Expression to return via *ppNew */ - TokenCtx sCtx = {0,0}; /* Context object for fts5ParseTokenize */ - - pOrig = pExpr->apExprPhrase[iPhrase]; - pNew = (Fts5Expr*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5Expr)); + TokenCtx sCtx = {0,0,0}; /* Context object for fts5ParseTokenize */ + if( !pExpr || iPhrase<0 || iPhrase>=pExpr->nPhrase ){ + rc = SQLITE_RANGE; + }else{ + pOrig = pExpr->apExprPhrase[iPhrase]; + pNew = (Fts5Expr*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5Expr)); + } if( rc==SQLITE_OK ){ pNew->apExprPhrase = (Fts5ExprPhrase**)sqlite3Fts5MallocZero(&rc, sizeof(Fts5ExprPhrase*)); } if( rc==SQLITE_OK ){ - pNew->pRoot = (Fts5ExprNode*)sqlite3Fts5MallocZero(&rc, - sizeof(Fts5ExprNode)); + pNew->pRoot = (Fts5ExprNode*)sqlite3Fts5MallocZero(&rc, SZ_FTS5EXPRNODE(1)); } if( rc==SQLITE_OK ){ - pNew->pRoot->pNear = (Fts5ExprNearset*)sqlite3Fts5MallocZero(&rc, - sizeof(Fts5ExprNearset) + sizeof(Fts5ExprPhrase*)); + pNew->pRoot->pNear = (Fts5ExprNearset*)sqlite3Fts5MallocZero(&rc, + SZ_FTS5EXPRNEARSET(2)); + } + if( rc==SQLITE_OK && ALWAYS(pOrig!=0) ){ + Fts5Colset *pColsetOrig = pOrig->pNode->pNear->pColset; + if( pColsetOrig ){ + sqlite3_int64 nByte; + Fts5Colset *pColset; + nByte = SZ_FTS5COLSET(pColsetOrig->nCol); + pColset = (Fts5Colset*)sqlite3Fts5MallocZero(&rc, nByte); + if( pColset ){ + memcpy(pColset, pColsetOrig, (size_t)nByte); + } + pNew->pRoot->pNear->pColset = pColset; + } } - for(i=0; rc==SQLITE_OK && inTerm; i++){ - int tflags = 0; - Fts5ExprTerm *p; - for(p=&pOrig->aTerm[i]; p && rc==SQLITE_OK; p=p->pSynonym){ - const char *zTerm = p->zTerm; - rc = fts5ParseTokenize((void*)&sCtx, tflags, zTerm, (int)strlen(zTerm), - 0, 0); - tflags = FTS5_TOKEN_COLOCATED; - } - if( rc==SQLITE_OK ){ - sCtx.pPhrase->aTerm[i].bPrefix = pOrig->aTerm[i].bPrefix; + if( rc==SQLITE_OK ){ + if( pOrig->nTerm ){ + int i; /* Used to iterate through phrase terms */ + sCtx.pConfig = pExpr->pConfig; + for(i=0; rc==SQLITE_OK && inTerm; i++){ + int tflags = 0; + Fts5ExprTerm *p; + for(p=&pOrig->aTerm[i]; p && rc==SQLITE_OK; p=p->pSynonym){ + rc = fts5ParseTokenize((void*)&sCtx,tflags,p->pTerm,p->nFullTerm,0,0); + tflags = FTS5_TOKEN_COLOCATED; + } + if( rc==SQLITE_OK ){ + sCtx.pPhrase->aTerm[i].bPrefix = pOrig->aTerm[i].bPrefix; + sCtx.pPhrase->aTerm[i].bFirst = pOrig->aTerm[i].bFirst; + } + } + }else{ + /* This happens when parsing a token or quoted phrase that contains + ** no token characters at all. (e.g ... MATCH '""'). */ + sCtx.pPhrase = sqlite3Fts5MallocZero(&rc, SZ_FTS5EXPRPHRASE(1)); } } - if( rc==SQLITE_OK ){ + if( rc==SQLITE_OK && ALWAYS(sCtx.pPhrase) ){ /* All the allocations succeeded. Put the expression object together. */ pNew->pIndex = pExpr->pIndex; pNew->pConfig = pExpr->pConfig; @@ -1657,7 +1973,10 @@ int sqlite3Fts5ExprClonePhrase( pNew->pRoot->pNear->nPhrase = 1; sCtx.pPhrase->pNode = pNew->pRoot; - if( pOrig->nTerm==1 && pOrig->aTerm[0].pSynonym==0 ){ + if( pOrig->nTerm==1 + && pOrig->aTerm[0].pSynonym==0 + && pOrig->aTerm[0].bFirst==0 + ){ pNew->pRoot->eType = FTS5_TERM; pNew->pRoot->xNext = fts5ExprNodeNext_TERM; }else{ @@ -1693,23 +2012,26 @@ void sqlite3Fts5ParseSetDistance( Fts5ExprNearset *pNear, Fts5Token *p ){ - int nNear = 0; - int i; - if( p->n ){ - for(i=0; in; i++){ - char c = (char)p->p[i]; - if( c<'0' || c>'9' ){ - sqlite3Fts5ParseError( - pParse, "expected integer, got \"%.*s\"", p->n, p->p - ); - return; + if( pNear ){ + int nNear = 0; + int i; + if( p->n ){ + for(i=0; in; i++){ + char c = (char)p->p[i]; + if( c<'0' || c>'9' ){ + sqlite3Fts5ParseError( + pParse, "expected integer, got \"%.*s\"", p->n, p->p + ); + return; + } + if( nNear<214748363 ) nNear = nNear * 10 + (p->p[i] - '0'); + /* ^^^^^^^^^^^^^^^--- Prevent integer overflow */ } - nNear = nNear * 10 + (p->p[i] - '0'); + }else{ + nNear = FTS5_DEFAULT_NEARDIST; } - }else{ - nNear = FTS5_DEFAULT_NEARDIST; + pNear->nNear = nNear; } - pNear->nNear = nNear; } /* @@ -1732,7 +2054,7 @@ static Fts5Colset *fts5ParseColset( assert( pParse->rc==SQLITE_OK ); assert( iCol>=0 && iColpConfig->nCol ); - pNew = sqlite3_realloc(p, sizeof(Fts5Colset) + sizeof(int)*nCol); + pNew = sqlite3_realloc64(p, SZ_FTS5COLSET(nCol+1)); if( pNew==0 ){ pParse->rc = SQLITE_NOMEM; }else{ @@ -1757,6 +2079,34 @@ static Fts5Colset *fts5ParseColset( return pNew; } +/* +** Allocate and return an Fts5Colset object specifying the inverse of +** the colset passed as the second argument. Free the colset passed +** as the second argument before returning. +*/ +Fts5Colset *sqlite3Fts5ParseColsetInvert(Fts5Parse *pParse, Fts5Colset *p){ + Fts5Colset *pRet; + int nCol = pParse->pConfig->nCol; + + pRet = (Fts5Colset*)sqlite3Fts5MallocZero(&pParse->rc, + SZ_FTS5COLSET(nCol+1) + ); + if( pRet ){ + int i; + int iOld = 0; + for(i=0; i=p->nCol || p->aiCol[iOld]!=i ){ + pRet->aiCol[pRet->nCol++] = i; + }else{ + iOld++; + } + } + } + + sqlite3_free(p); + return pRet; +} + Fts5Colset *sqlite3Fts5ParseColset( Fts5Parse *pParse, /* Store SQLITE_NOMEM here if required */ Fts5Colset *pColset, /* Existing colset object */ @@ -1789,25 +2139,109 @@ Fts5Colset *sqlite3Fts5ParseColset( return pRet; } +/* +** If argument pOrig is NULL, or if (*pRc) is set to anything other than +** SQLITE_OK when this function is called, NULL is returned. +** +** Otherwise, a copy of (*pOrig) is made into memory obtained from +** sqlite3Fts5MallocZero() and a pointer to it returned. If the allocation +** fails, (*pRc) is set to SQLITE_NOMEM and NULL is returned. +*/ +static Fts5Colset *fts5CloneColset(int *pRc, Fts5Colset *pOrig){ + Fts5Colset *pRet; + if( pOrig ){ + sqlite3_int64 nByte = SZ_FTS5COLSET(pOrig->nCol); + pRet = (Fts5Colset*)sqlite3Fts5MallocZero(pRc, nByte); + if( pRet ){ + memcpy(pRet, pOrig, (size_t)nByte); + } + }else{ + pRet = 0; + } + return pRet; +} + +/* +** Remove from colset pColset any columns that are not also in colset pMerge. +*/ +static void fts5MergeColset(Fts5Colset *pColset, Fts5Colset *pMerge){ + int iIn = 0; /* Next input in pColset */ + int iMerge = 0; /* Next input in pMerge */ + int iOut = 0; /* Next output slot in pColset */ + + while( iInnCol && iMergenCol ){ + int iDiff = pColset->aiCol[iIn] - pMerge->aiCol[iMerge]; + if( iDiff==0 ){ + pColset->aiCol[iOut++] = pMerge->aiCol[iMerge]; + iMerge++; + iIn++; + }else if( iDiff>0 ){ + iMerge++; + }else{ + iIn++; + } + } + pColset->nCol = iOut; +} + +/* +** Recursively apply colset pColset to expression node pNode and all of +** its decendents. If (*ppFree) is not NULL, it contains a spare copy +** of pColset. This function may use the spare copy and set (*ppFree) to +** zero, or it may create copies of pColset using fts5CloneColset(). +*/ +static void fts5ParseSetColset( + Fts5Parse *pParse, + Fts5ExprNode *pNode, + Fts5Colset *pColset, + Fts5Colset **ppFree +){ + if( pParse->rc==SQLITE_OK ){ + assert( pNode->eType==FTS5_TERM || pNode->eType==FTS5_STRING + || pNode->eType==FTS5_AND || pNode->eType==FTS5_OR + || pNode->eType==FTS5_NOT || pNode->eType==FTS5_EOF + ); + if( pNode->eType==FTS5_STRING || pNode->eType==FTS5_TERM ){ + Fts5ExprNearset *pNear = pNode->pNear; + if( pNear->pColset ){ + fts5MergeColset(pNear->pColset, pColset); + if( pNear->pColset->nCol==0 ){ + pNode->eType = FTS5_EOF; + pNode->xNext = 0; + } + }else if( *ppFree ){ + pNear->pColset = pColset; + *ppFree = 0; + }else{ + pNear->pColset = fts5CloneColset(&pParse->rc, pColset); + } + }else{ + int i; + assert( pNode->eType!=FTS5_EOF || pNode->nChild==0 ); + for(i=0; inChild; i++){ + fts5ParseSetColset(pParse, pNode->apChild[i], pColset, ppFree); + } + } + } +} + +/* +** Apply colset pColset to expression node pExpr and all of its descendents. +*/ void sqlite3Fts5ParseSetColset( Fts5Parse *pParse, - Fts5ExprNearset *pNear, + Fts5ExprNode *pExpr, Fts5Colset *pColset ){ + Fts5Colset *pFree = pColset; if( pParse->pConfig->eDetail==FTS5_DETAIL_NONE ){ - pParse->rc = SQLITE_ERROR; - pParse->zErr = sqlite3_mprintf( - "fts5: column queries are not supported (detail=none)" + sqlite3Fts5ParseError(pParse, + "fts5: column queries are not supported (detail=none)" ); - sqlite3_free(pColset); - return; - } - - if( pNear ){ - pNear->pColset = pColset; }else{ - sqlite3_free(pColset); + fts5ParseSetColset(pParse, pExpr, pColset, &pFree); } + sqlite3_free(pFree); } static void fts5ExprAssignXNext(Fts5ExprNode *pNode){ @@ -1816,6 +2250,7 @@ static void fts5ExprAssignXNext(Fts5ExprNode *pNode){ Fts5ExprNearset *pNear = pNode->pNear; if( pNear->nPhrase==1 && pNear->apPhrase[0]->nTerm==1 && pNear->apPhrase[0]->aTerm[0].pSynonym==0 + && pNear->apPhrase[0]->aTerm[0].bFirst==0 ){ pNode->eType = FTS5_TERM; pNode->xNext = fts5ExprNodeNext_TERM; @@ -1842,7 +2277,11 @@ static void fts5ExprAssignXNext(Fts5ExprNode *pNode){ } } +/* +** Add pSub as a child of p. +*/ static void fts5ExprAddChildren(Fts5ExprNode *p, Fts5ExprNode *pSub){ + int ii = p->nChild; if( p->eType!=FTS5_NOT && pSub->eType==p->eType ){ int nByte = sizeof(Fts5ExprNode*) * pSub->nChild; memcpy(&p->apChild[p->nChild], pSub->apChild, nByte); @@ -1851,6 +2290,73 @@ static void fts5ExprAddChildren(Fts5ExprNode *p, Fts5ExprNode *pSub){ }else{ p->apChild[p->nChild++] = pSub; } + for( ; iinChild; ii++){ + p->iHeight = MAX(p->iHeight, p->apChild[ii]->iHeight + 1); + } +} + +/* +** This function is used when parsing LIKE or GLOB patterns against +** trigram indexes that specify either detail=column or detail=none. +** It converts a phrase: +** +** abc + def + ghi +** +** into an AND tree: +** +** abc AND def AND ghi +*/ +static Fts5ExprNode *fts5ParsePhraseToAnd( + Fts5Parse *pParse, + Fts5ExprNearset *pNear +){ + int nTerm = pNear->apPhrase[0]->nTerm; + int ii; + int nByte; + Fts5ExprNode *pRet; + + assert( pNear->nPhrase==1 ); + assert( pParse->bPhraseToAnd ); + + nByte = SZ_FTS5EXPRNODE(nTerm+1); + pRet = (Fts5ExprNode*)sqlite3Fts5MallocZero(&pParse->rc, nByte); + if( pRet ){ + pRet->eType = FTS5_AND; + pRet->nChild = nTerm; + pRet->iHeight = 1; + fts5ExprAssignXNext(pRet); + pParse->nPhrase--; + for(ii=0; iirc, SZ_FTS5EXPRPHRASE(1) + ); + if( pPhrase ){ + if( parseGrowPhraseArray(pParse) ){ + fts5ExprPhraseFree(pPhrase); + }else{ + Fts5ExprTerm *p = &pNear->apPhrase[0]->aTerm[ii]; + Fts5ExprTerm *pTo = &pPhrase->aTerm[0]; + pParse->apPhrase[pParse->nPhrase++] = pPhrase; + pPhrase->nTerm = 1; + pTo->pTerm = sqlite3Fts5Strndup(&pParse->rc, p->pTerm, p->nFullTerm); + pTo->nQueryTerm = p->nQueryTerm; + pTo->nFullTerm = p->nFullTerm; + pRet->apChild[ii] = sqlite3Fts5ParseNode(pParse, FTS5_STRING, + 0, 0, sqlite3Fts5ParseNearset(pParse, 0, pPhrase) + ); + } + } + } + + if( pParse->rc ){ + sqlite3Fts5ParseNodeFree(pRet); + pRet = 0; + }else{ + sqlite3Fts5ParseNearsetFree(pNear); + } + } + + return pRet; } /* @@ -1868,7 +2374,7 @@ Fts5ExprNode *sqlite3Fts5ParseNode( if( pParse->rc==SQLITE_OK ){ int nChild = 0; /* Number of children of returned node */ - int nByte; /* Bytes of space to allocate for this node */ + sqlite3_int64 nByte; /* Bytes of space to allocate for this node */ assert( (eType!=FTS5_STRING && !pNear) || (eType==FTS5_STRING && !pLeft && !pRight) @@ -1877,44 +2383,67 @@ Fts5ExprNode *sqlite3Fts5ParseNode( if( eType!=FTS5_STRING && pLeft==0 ) return pRight; if( eType!=FTS5_STRING && pRight==0 ) return pLeft; - if( eType==FTS5_NOT ){ - nChild = 2; - }else if( eType==FTS5_AND || eType==FTS5_OR ){ - nChild = 2; - if( pLeft->eType==eType ) nChild += pLeft->nChild-1; - if( pRight->eType==eType ) nChild += pRight->nChild-1; - } - - nByte = sizeof(Fts5ExprNode) + sizeof(Fts5ExprNode*)*(nChild-1); - pRet = (Fts5ExprNode*)sqlite3Fts5MallocZero(&pParse->rc, nByte); - - if( pRet ){ - pRet->eType = eType; - pRet->pNear = pNear; - fts5ExprAssignXNext(pRet); - if( eType==FTS5_STRING ){ - int iPhrase; - for(iPhrase=0; iPhrasenPhrase; iPhrase++){ - pNear->apPhrase[iPhrase]->pNode = pRet; - } + if( eType==FTS5_STRING + && pParse->bPhraseToAnd + && pNear->apPhrase[0]->nTerm>1 + ){ + pRet = fts5ParsePhraseToAnd(pParse, pNear); + }else{ + if( eType==FTS5_NOT ){ + nChild = 2; + }else if( eType==FTS5_AND || eType==FTS5_OR ){ + nChild = 2; + if( pLeft->eType==eType ) nChild += pLeft->nChild-1; + if( pRight->eType==eType ) nChild += pRight->nChild-1; + } - if( pParse->pConfig->eDetail!=FTS5_DETAIL_FULL - && (pNear->nPhrase!=1 || pNear->apPhrase[0]->nTerm!=1) - ){ - assert( pParse->rc==SQLITE_OK ); - pParse->rc = SQLITE_ERROR; - assert( pParse->zErr==0 ); - pParse->zErr = sqlite3_mprintf( - "fts5: %s queries are not supported (detail!=full)", - pNear->nPhrase==1 ? "phrase": "NEAR" - ); - sqlite3_free(pRet); - pRet = 0; - } + nByte = SZ_FTS5EXPRNODE(nChild); + pRet = (Fts5ExprNode*)sqlite3Fts5MallocZero(&pParse->rc, nByte); + + if( pRet ){ + pRet->eType = eType; + pRet->pNear = pNear; + fts5ExprAssignXNext(pRet); + if( eType==FTS5_STRING ){ + int iPhrase; + for(iPhrase=0; iPhrasenPhrase; iPhrase++){ + pNear->apPhrase[iPhrase]->pNode = pRet; + if( pNear->apPhrase[iPhrase]->nTerm==0 ){ + pRet->xNext = 0; + pRet->eType = FTS5_EOF; + } + } - }else{ - fts5ExprAddChildren(pRet, pLeft); - fts5ExprAddChildren(pRet, pRight); + if( pParse->pConfig->eDetail!=FTS5_DETAIL_FULL ){ + Fts5ExprPhrase *pPhrase = pNear->apPhrase[0]; + if( pNear->nPhrase!=1 + || pPhrase->nTerm>1 + || (pPhrase->nTerm>0 && pPhrase->aTerm[0].bFirst) + ){ + sqlite3Fts5ParseError(pParse, + "fts5: %s queries are not supported (detail!=full)", + pNear->nPhrase==1 ? "phrase": "NEAR" + ); + sqlite3Fts5ParseNodeFree(pRet); + pRet = 0; + pNear = 0; + assert( pLeft==0 && pRight==0 ); + } + } + }else{ + assert( pNear==0 ); + fts5ExprAddChildren(pRet, pLeft); + fts5ExprAddChildren(pRet, pRight); + pLeft = pRight = 0; + if( pRet->iHeight>SQLITE_FTS5_MAX_EXPR_DEPTH ){ + sqlite3Fts5ParseError(pParse, + "fts5 expression tree is too large (maximum depth %d)", + SQLITE_FTS5_MAX_EXPR_DEPTH + ); + sqlite3Fts5ParseNodeFree(pRet); + pRet = 0; + } + } } } } @@ -1928,23 +2457,92 @@ Fts5ExprNode *sqlite3Fts5ParseNode( return pRet; } +Fts5ExprNode *sqlite3Fts5ParseImplicitAnd( + Fts5Parse *pParse, /* Parse context */ + Fts5ExprNode *pLeft, /* Left hand child expression */ + Fts5ExprNode *pRight /* Right hand child expression */ +){ + Fts5ExprNode *pRet = 0; + Fts5ExprNode *pPrev; + + if( pParse->rc ){ + sqlite3Fts5ParseNodeFree(pLeft); + sqlite3Fts5ParseNodeFree(pRight); + }else{ + + assert( pLeft->eType==FTS5_STRING + || pLeft->eType==FTS5_TERM + || pLeft->eType==FTS5_EOF + || pLeft->eType==FTS5_AND + ); + assert( pRight->eType==FTS5_STRING + || pRight->eType==FTS5_TERM + || pRight->eType==FTS5_EOF + || (pRight->eType==FTS5_AND && pParse->bPhraseToAnd) + ); + + if( pLeft->eType==FTS5_AND ){ + pPrev = pLeft->apChild[pLeft->nChild-1]; + }else{ + pPrev = pLeft; + } + assert( pPrev->eType==FTS5_STRING + || pPrev->eType==FTS5_TERM + || pPrev->eType==FTS5_EOF + ); + + if( pRight->eType==FTS5_EOF ){ + assert( pParse->apPhrase!=0 ); + assert( pParse->nPhrase>0 ); + assert( pParse->apPhrase[pParse->nPhrase-1]==pRight->pNear->apPhrase[0] ); + sqlite3Fts5ParseNodeFree(pRight); + pRet = pLeft; + pParse->nPhrase--; + } + else if( pPrev->eType==FTS5_EOF ){ + Fts5ExprPhrase **ap; + + if( pPrev==pLeft ){ + pRet = pRight; + }else{ + pLeft->apChild[pLeft->nChild-1] = pRight; + pRet = pLeft; + } + + ap = &pParse->apPhrase[pParse->nPhrase-1-pRight->pNear->nPhrase]; + assert( ap[0]==pPrev->pNear->apPhrase[0] ); + memmove(ap, &ap[1], sizeof(Fts5ExprPhrase*)*pRight->pNear->nPhrase); + pParse->nPhrase--; + + sqlite3Fts5ParseNodeFree(pPrev); + } + else{ + pRet = sqlite3Fts5ParseNode(pParse, FTS5_AND, pLeft, pRight, 0); + } + } + + return pRet; +} + +#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG) static char *fts5ExprTermPrint(Fts5ExprTerm *pTerm){ - int nByte = 0; + sqlite3_int64 nByte = 0; Fts5ExprTerm *p; char *zQuoted; /* Determine the maximum amount of space required. */ for(p=pTerm; p; p=p->pSynonym){ - nByte += (int)strlen(pTerm->zTerm) * 2 + 3 + 2; + nByte += pTerm->nQueryTerm * 2 + 3 + 2; } - zQuoted = sqlite3_malloc(nByte); + zQuoted = sqlite3_malloc64(nByte); if( zQuoted ){ int i = 0; for(p=pTerm; p; p=p->pSynonym){ - char *zIn = p->zTerm; + char *zIn = p->pTerm; + char *zEnd = &zIn[p->nQueryTerm]; zQuoted[i++] = '"'; - while( *zIn ){ + while( zInnTerm; iTerm++){ - char *zTerm = pPhrase->aTerm[iTerm].zTerm; - zRet = fts5PrintfAppend(zRet, "%s%s", iTerm==0?"":" ", zTerm); + Fts5ExprTerm *p = &pPhrase->aTerm[iTerm]; + zRet = fts5PrintfAppend(zRet, "%s%.*s", iTerm==0?"":" ", + p->nQueryTerm, p->pTerm + ); if( pPhrase->aTerm[iTerm].bPrefix ){ zRet = fts5PrintfAppend(zRet, "*"); } @@ -2033,6 +2633,8 @@ static char *fts5ExprPrintTcl( if( zRet==0 ) return 0; } + }else if( pExpr->eType==0 ){ + zRet = sqlite3_mprintf("{}"); }else{ char const *zOp = 0; int i; @@ -2062,14 +2664,26 @@ static char *fts5ExprPrintTcl( static char *fts5ExprPrint(Fts5Config *pConfig, Fts5ExprNode *pExpr){ char *zRet = 0; + if( pExpr->eType==0 ){ + return sqlite3_mprintf("\"\""); + }else if( pExpr->eType==FTS5_STRING || pExpr->eType==FTS5_TERM ){ Fts5ExprNearset *pNear = pExpr->pNear; int i; int iTerm; if( pNear->pColset ){ - int iCol = pNear->pColset->aiCol[0]; - zRet = fts5PrintfAppend(zRet, "%s : ", pConfig->azCol[iCol]); + int ii; + Fts5Colset *pColset = pNear->pColset; + if( pColset->nCol>1 ) zRet = fts5PrintfAppend(zRet, "{"); + for(ii=0; iinCol; ii++){ + zRet = fts5PrintfAppend(zRet, "%s%s", + pConfig->azCol[pColset->aiCol[ii]], ii==pColset->nCol-1 ? "" : " " + ); + } + if( zRet ){ + zRet = fts5PrintfAppend(zRet, "%s : ", pColset->nCol>1 ? "}" : ""); + } if( zRet==0 ) return 0; } @@ -2122,7 +2736,7 @@ static char *fts5ExprPrint(Fts5Config *pConfig, Fts5ExprNode *pExpr){ zRet = 0; }else{ int e = pExpr->apChild[i]->eType; - int b = (e!=FTS5_STRING && e!=FTS5_TERM); + int b = (e!=FTS5_STRING && e!=FTS5_TERM && e!=FTS5_EOF); zRet = fts5PrintfAppend(zRet, "%s%s%z%s", (i==0 ? "" : zOp), (b?"(":""), z, (b?")":"") @@ -2174,7 +2788,7 @@ static void fts5ExprFunction( } nConfig = 3 + (nArg-iArg); - azConfig = (const char**)sqlite3_malloc(sizeof(char*) * nConfig); + azConfig = (const char**)sqlite3_malloc64(sizeof(char*) * nConfig); if( azConfig==0 ){ sqlite3_result_error_nomem(pCtx); return; @@ -2183,14 +2797,16 @@ static void fts5ExprFunction( azConfig[1] = "main"; azConfig[2] = "tbl"; for(i=3; iArgnCol, zExpr, &pExpr, &zErr); } if( rc==SQLITE_OK ){ char *zText; @@ -2248,14 +2864,19 @@ static void fts5ExprIsAlnum( sqlite3_value **apVal /* Function arguments */ ){ int iCode; + u8 aArr[32]; if( nArg!=1 ){ sqlite3_result_error(pCtx, "wrong number of arguments to function fts5_isalnum", -1 ); return; } + memset(aArr, 0, sizeof(aArr)); + sqlite3Fts5UnicodeCatParse("L*", aArr); + sqlite3Fts5UnicodeCatParse("N*", aArr); + sqlite3Fts5UnicodeCatParse("Co", aArr); iCode = sqlite3_value_int(apVal[0]); - sqlite3_result_int(pCtx, sqlite3Fts5UnicodeIsalnum(iCode)); + sqlite3_result_int(pCtx, aArr[sqlite3Fts5UnicodeCategory((u32)iCode)]); } static void fts5ExprFold( @@ -2275,12 +2896,14 @@ static void fts5ExprFold( sqlite3_result_int(pCtx, sqlite3Fts5UnicodeFold(iCode, bRemoveDiacritics)); } } +#endif /* if SQLITE_TEST || SQLITE_FTS5_DEBUG */ /* ** This is called during initialization to register the fts5_expr() scalar ** UDF with the SQLite handle passed as the only argument. */ int sqlite3Fts5ExprInit(Fts5Global *pGlobal, sqlite3 *db){ +#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG) struct Fts5ExprFunc { const char *z; void (*x)(sqlite3_context*,int,sqlite3_value**); @@ -2298,11 +2921,17 @@ int sqlite3Fts5ExprInit(Fts5Global *pGlobal, sqlite3 *db){ struct Fts5ExprFunc *p = &aFunc[i]; rc = sqlite3_create_function(db, p->z, -1, SQLITE_UTF8, pCtx, p->x, 0, 0); } +#else + int rc = SQLITE_OK; + UNUSED_PARAM2(pGlobal,db); +#endif - /* Avoid a warning indicating that sqlite3Fts5ParserTrace() is unused */ + /* Avoid warnings indicating that sqlite3Fts5ParserTrace() and + ** sqlite3Fts5ParserFallback() are unused */ #ifndef NDEBUG (void)sqlite3Fts5ParserTrace; #endif + (void)sqlite3Fts5ParserFallback; return rc; } @@ -2346,16 +2975,25 @@ struct Fts5PoslistPopulator { int bMiss; }; +/* +** Clear the position lists associated with all phrases in the expression +** passed as the first argument. Argument bLive is true if the expression +** might be pointing to a real entry, otherwise it has just been reset. +** +** At present this function is only used for detail=col and detail=none +** fts5 tables. This implies that all phrases must be at most 1 token +** in size, as phrase matches are not supported without detail=full. +*/ Fts5PoslistPopulator *sqlite3Fts5ExprClearPoslists(Fts5Expr *pExpr, int bLive){ Fts5PoslistPopulator *pRet; - pRet = sqlite3_malloc(sizeof(Fts5PoslistPopulator)*pExpr->nPhrase); + pRet = sqlite3_malloc64(sizeof(Fts5PoslistPopulator)*pExpr->nPhrase); if( pRet ){ int i; memset(pRet, 0, sizeof(Fts5PoslistPopulator)*pExpr->nPhrase); for(i=0; inPhrase; i++){ Fts5Buffer *pBuf = &pExpr->apExprPhrase[i]->poslist; Fts5ExprNode *pNode = pExpr->apExprPhrase[i]->pNode; - assert( pExpr->apExprPhrase[i]->nTerm==1 ); + assert( pExpr->apExprPhrase[i]->nTerm<=1 ); if( bLive && (pBuf->n==0 || pNode->iRowid!=pExpr->pRoot->iRowid || pNode->bEof) ){ @@ -2386,6 +3024,17 @@ static int fts5ExprColsetTest(Fts5Colset *pColset, int iCol){ return 0; } +/* +** pToken is a buffer nToken bytes in size that may or may not contain +** an embedded 0x00 byte. If it does, return the number of bytes in +** the buffer before the 0x00. If it does not, return nToken. +*/ +static int fts5QueryTerm(const char *pToken, int nToken){ + int ii; + for(ii=0; iipExpr; int i; + int nQuery = nToken; + i64 iRowid = pExpr->pRoot->iRowid; UNUSED_PARAM2(iUnused1, iUnused2); + if( nQuery>FTS5_MAX_TOKEN_SIZE ) nQuery = FTS5_MAX_TOKEN_SIZE; + if( pExpr->pConfig->bTokendata ){ + nQuery = fts5QueryTerm(pToken, nQuery); + } if( (tflags & FTS5_TOKEN_COLOCATED)==0 ) p->iOff++; for(i=0; inPhrase; i++){ - Fts5ExprTerm *pTerm; + Fts5ExprTerm *pT; if( p->aPopulator[i].bOk==0 ) continue; - for(pTerm=&pExpr->apExprPhrase[i]->aTerm[0]; pTerm; pTerm=pTerm->pSynonym){ - int nTerm = strlen(pTerm->zTerm); - if( (nTerm==nToken || (nTermbPrefix)) - && memcmp(pTerm->zTerm, pToken, nTerm)==0 + for(pT=&pExpr->apExprPhrase[i]->aTerm[0]; pT; pT=pT->pSynonym){ + if( (pT->nQueryTerm==nQuery || (pT->nQueryTermbPrefix)) + && memcmp(pT->pTerm, pToken, pT->nQueryTerm)==0 ){ int rc = sqlite3Fts5PoslistWriterAppend( &pExpr->apExprPhrase[i]->poslist, &p->aPopulator[i].writer, p->iOff ); + if( rc==SQLITE_OK && (pExpr->pConfig->bTokendata || pT->bPrefix) ){ + int iCol = p->iOff>>32; + int iTokOff = p->iOff & 0x7FFFFFFF; + rc = sqlite3Fts5IndexIterWriteTokendata( + pT->pIter, pToken, nToken, iRowid, iCol, iTokOff + ); + } if( rc ) return rc; break; } @@ -2465,6 +3126,7 @@ static int fts5ExprCheckPoslists(Fts5ExprNode *pNode, i64 iRowid){ pNode->iRowid = iRowid; pNode->bEof = 0; switch( pNode->eType ){ + case 0: case FTS5_TERM: case FTS5_STRING: return (pNode->pNear->apPhrase[0]->poslist.n>0); @@ -2509,17 +3171,6 @@ void sqlite3Fts5ExprCheckPoslists(Fts5Expr *pExpr, i64 iRowid){ fts5ExprCheckPoslists(pExpr->pRoot, iRowid); } -static void fts5ExprClearEof(Fts5ExprNode *pNode){ - int i; - for(i=0; inChild; i++){ - fts5ExprClearEof(pNode->apChild[i]); - } - pNode->bEof = 0; -} -void sqlite3Fts5ExprClearEof(Fts5Expr *pExpr){ - fts5ExprClearEof(pExpr->pRoot); -} - /* ** This function is only called for detail=columns tables. */ @@ -2558,3 +3209,78 @@ int sqlite3Fts5ExprPhraseCollist( return rc; } +/* +** Does the work of the fts5_api.xQueryToken() API method. +*/ +int sqlite3Fts5ExprQueryToken( + Fts5Expr *pExpr, + int iPhrase, + int iToken, + const char **ppOut, + int *pnOut +){ + Fts5ExprPhrase *pPhrase = 0; + + if( iPhrase<0 || iPhrase>=pExpr->nPhrase ){ + return SQLITE_RANGE; + } + pPhrase = pExpr->apExprPhrase[iPhrase]; + if( iToken<0 || iToken>=pPhrase->nTerm ){ + return SQLITE_RANGE; + } + + *ppOut = pPhrase->aTerm[iToken].pTerm; + *pnOut = pPhrase->aTerm[iToken].nFullTerm; + return SQLITE_OK; +} + +/* +** Does the work of the fts5_api.xInstToken() API method. +*/ +int sqlite3Fts5ExprInstToken( + Fts5Expr *pExpr, + i64 iRowid, + int iPhrase, + int iCol, + int iOff, + int iToken, + const char **ppOut, + int *pnOut +){ + Fts5ExprPhrase *pPhrase = 0; + Fts5ExprTerm *pTerm = 0; + int rc = SQLITE_OK; + + if( iPhrase<0 || iPhrase>=pExpr->nPhrase ){ + return SQLITE_RANGE; + } + pPhrase = pExpr->apExprPhrase[iPhrase]; + if( iToken<0 || iToken>=pPhrase->nTerm ){ + return SQLITE_RANGE; + } + pTerm = &pPhrase->aTerm[iToken]; + if( pExpr->pConfig->bTokendata || pTerm->bPrefix ){ + rc = sqlite3Fts5IterToken( + pTerm->pIter, pTerm->pTerm, pTerm->nQueryTerm, + iRowid, iCol, iOff+iToken, ppOut, pnOut + ); + }else{ + *ppOut = pTerm->pTerm; + *pnOut = pTerm->nFullTerm; + } + return rc; +} + +/* +** Clear the token mappings for all Fts5IndexIter objects managed by +** the expression passed as the only argument. +*/ +void sqlite3Fts5ExprClearTokens(Fts5Expr *pExpr){ + int ii; + for(ii=0; iinPhrase; ii++){ + Fts5ExprTerm *pT; + for(pT=&pExpr->apExprPhrase[ii]->aTerm[0]; pT; pT=pT->pSynonym){ + sqlite3Fts5IndexIterClearTokendata(pT->pIter); + } + } +} diff --git a/ext/fts5/fts5_hash.c b/ext/fts5/fts5_hash.c index ada8bb16cb..a33dec9a92 100644 --- a/ext/fts5/fts5_hash.c +++ b/ext/fts5/fts5_hash.c @@ -20,7 +20,7 @@ typedef struct Fts5HashEntry Fts5HashEntry; /* ** This file contains the implementation of an in-memory hash table used -** to accumuluate "term -> doclist" content before it is flused to a level-0 +** to accumulate "term -> doclist" content before it is flushed to a level-0 ** segment. */ @@ -36,9 +36,15 @@ struct Fts5Hash { /* ** Each entry in the hash table is represented by an object of the -** following type. Each object, its key (zKey[]) and its current data -** are stored in a single memory allocation. The position list data -** immediately follows the key data in memory. +** following type. Each object, its key, and its current data are stored +** in a single memory allocation. The key immediately follows the object +** in memory. The position list data immediately follows the key data +** in memory. +** +** The key is Fts5HashEntry.nKey bytes in size. It consists of a single +** byte identifying the index (either the main term index or a prefix-index), +** followed by the term data. For example: "0token". There is no +** nul-terminator - in this case nKey=6. ** ** The data that follows the key is in a similar, but not identical format ** to the doclist data stored in the database. It is: @@ -62,20 +68,20 @@ struct Fts5HashEntry { int nAlloc; /* Total size of allocation */ int iSzPoslist; /* Offset of space for 4-byte poslist size */ int nData; /* Total bytes of data (incl. structure) */ - int nKey; /* Length of zKey[] in bytes */ + int nKey; /* Length of key in bytes */ u8 bDel; /* Set delete-flag @ iSzPoslist */ u8 bContent; /* Set content-flag (detail=none mode) */ i16 iCol; /* Column of last value written */ int iPos; /* Position of last value written */ i64 iRowid; /* Rowid of last value written */ - char zKey[8]; /* Nul-terminated entry key */ }; /* -** Size of Fts5HashEntry without the zKey[] array. +** Equivalent to: +** +** char *fts5EntryKey(Fts5HashEntry *pEntry){ return zKey; } */ -#define FTS5_HASHENTRYSIZE (sizeof(Fts5HashEntry)-8) - +#define fts5EntryKey(p) ( ((char *)(&(p)[1])) ) /* @@ -89,20 +95,20 @@ int sqlite3Fts5HashNew(Fts5Config *pConfig, Fts5Hash **ppNew, int *pnByte){ if( pNew==0 ){ rc = SQLITE_NOMEM; }else{ - int nByte; + sqlite3_int64 nByte; memset(pNew, 0, sizeof(Fts5Hash)); pNew->pnByte = pnByte; pNew->eDetail = pConfig->eDetail; pNew->nSlot = 1024; nByte = sizeof(Fts5HashEntry*) * pNew->nSlot; - pNew->aSlot = (Fts5HashEntry**)sqlite3_malloc(nByte); + pNew->aSlot = (Fts5HashEntry**)sqlite3_malloc64(nByte); if( pNew->aSlot==0 ){ sqlite3_free(pNew); *ppNew = 0; rc = SQLITE_NOMEM; }else{ - memset(pNew->aSlot, 0, nByte); + memset(pNew->aSlot, 0, (size_t)nByte); } } return rc; @@ -164,16 +170,16 @@ static int fts5HashResize(Fts5Hash *pHash){ Fts5HashEntry **apNew; Fts5HashEntry **apOld = pHash->aSlot; - apNew = (Fts5HashEntry**)sqlite3_malloc(nNew*sizeof(Fts5HashEntry*)); + apNew = (Fts5HashEntry**)sqlite3_malloc64(nNew*sizeof(Fts5HashEntry*)); if( !apNew ) return SQLITE_NOMEM; memset(apNew, 0, nNew*sizeof(Fts5HashEntry*)); for(i=0; inSlot; i++){ while( apOld[i] ){ - int iHash; + unsigned int iHash; Fts5HashEntry *p = apOld[i]; apOld[i] = p->pHashNext; - iHash = fts5HashKey(nNew, (u8*)p->zKey, (int)strlen(p->zKey)); + iHash = fts5HashKey(nNew, (u8*)fts5EntryKey(p), p->nKey); p->pHashNext = apNew[iHash]; apNew[iHash] = p; } @@ -185,19 +191,25 @@ static int fts5HashResize(Fts5Hash *pHash){ return SQLITE_OK; } -static void fts5HashAddPoslistSize(Fts5Hash *pHash, Fts5HashEntry *p){ +static int fts5HashAddPoslistSize( + Fts5Hash *pHash, + Fts5HashEntry *p, + Fts5HashEntry *p2 +){ + int nRet = 0; if( p->iSzPoslist ){ - u8 *pPtr = (u8*)p; + u8 *pPtr = p2 ? (u8*)p2 : (u8*)p; + int nData = p->nData; if( pHash->eDetail==FTS5_DETAIL_NONE ){ - assert( p->nData==p->iSzPoslist ); + assert( nData==p->iSzPoslist ); if( p->bDel ){ - pPtr[p->nData++] = 0x00; + pPtr[nData++] = 0x00; if( p->bContent ){ - pPtr[p->nData++] = 0x00; + pPtr[nData++] = 0x00; } } }else{ - int nSz = (p->nData - p->iSzPoslist - 1); /* Size in bytes */ + int nSz = (nData - p->iSzPoslist - 1); /* Size in bytes */ int nPos = nSz*2 + p->bDel; /* Value of nPos field */ assert( p->bDel==0 || p->bDel==1 ); @@ -207,14 +219,19 @@ static void fts5HashAddPoslistSize(Fts5Hash *pHash, Fts5HashEntry *p){ int nByte = sqlite3Fts5GetVarintLen((u32)nPos); memmove(&pPtr[p->iSzPoslist + nByte], &pPtr[p->iSzPoslist + 1], nSz); sqlite3Fts5PutVarint(&pPtr[p->iSzPoslist], nPos); - p->nData += (nByte-1); + nData += (nByte-1); } } - p->iSzPoslist = 0; - p->bDel = 0; - p->bContent = 0; + nRet = nData - p->nData; + if( p2==0 ){ + p->iSzPoslist = 0; + p->bDel = 0; + p->bContent = 0; + p->nData = nData; + } } + return nRet; } /* @@ -244,9 +261,10 @@ int sqlite3Fts5HashWrite( /* Attempt to locate an existing hash entry */ iHash = fts5HashKey2(pHash->nSlot, (u8)bByte, (const u8*)pToken, nToken); for(p=pHash->aSlot[iHash]; p; p=p->pHashNext){ - if( p->zKey[0]==bByte - && p->nKey==nToken - && memcmp(&p->zKey[1], pToken, nToken)==0 + char *zKey = fts5EntryKey(p); + if( zKey[0]==bByte + && p->nKey==nToken+1 + && memcmp(&zKey[1], pToken, nToken)==0 ){ break; } @@ -255,7 +273,8 @@ int sqlite3Fts5HashWrite( /* If an existing hash entry cannot be found, create a new one. */ if( p==0 ){ /* Figure out how much space to allocate */ - int nByte = FTS5_HASHENTRYSIZE + (nToken+1) + 1 + 64; + char *zKey; + sqlite3_int64 nByte = sizeof(Fts5HashEntry) + (nToken+1) + 1 + 64; if( nByte<128 ) nByte = 128; /* Grow the Fts5Hash.aSlot[] array if necessary. */ @@ -266,16 +285,17 @@ int sqlite3Fts5HashWrite( } /* Allocate new Fts5HashEntry and add it to the hash table. */ - p = (Fts5HashEntry*)sqlite3_malloc(nByte); + p = (Fts5HashEntry*)sqlite3_malloc64(nByte); if( !p ) return SQLITE_NOMEM; - memset(p, 0, FTS5_HASHENTRYSIZE); - p->nAlloc = nByte; - p->zKey[0] = bByte; - memcpy(&p->zKey[1], pToken, nToken); - assert( iHash==fts5HashKey(pHash->nSlot, (u8*)p->zKey, nToken+1) ); - p->nKey = nToken; - p->zKey[nToken+1] = '\0'; - p->nData = nToken+1 + 1 + FTS5_HASHENTRYSIZE; + memset(p, 0, sizeof(Fts5HashEntry)); + p->nAlloc = (int)nByte; + zKey = fts5EntryKey(p); + zKey[0] = bByte; + memcpy(&zKey[1], pToken, nToken); + assert( iHash==fts5HashKey(pHash->nSlot, (u8*)zKey, nToken+1) ); + p->nKey = nToken+1; + zKey[nToken+1] = '\0'; + p->nData = nToken+1 + sizeof(Fts5HashEntry); p->pHashNext = pHash->aSlot[iHash]; pHash->aSlot[iHash] = p; pHash->nEntry++; @@ -290,7 +310,6 @@ int sqlite3Fts5HashWrite( p->iCol = (pHash->eDetail==FTS5_DETAIL_FULL ? 0 : -1); } - nIncr += p->nData; }else{ /* Appending to an existing hash-entry. Check that there is enough @@ -304,12 +323,12 @@ int sqlite3Fts5HashWrite( ** + 5 bytes for the new position offset (32-bit max). */ if( (p->nAlloc - p->nData) < (9 + 4 + 1 + 3 + 5) ){ - int nNew = p->nAlloc * 2; + sqlite3_int64 nNew = p->nAlloc * 2; Fts5HashEntry *pNew; Fts5HashEntry **pp; - pNew = (Fts5HashEntry*)sqlite3_realloc(p, nNew); + pNew = (Fts5HashEntry*)sqlite3_realloc64(p, nNew); if( pNew==0 ) return SQLITE_NOMEM; - pNew->nAlloc = nNew; + pNew->nAlloc = (int)nNew; for(pp=&pHash->aSlot[iHash]; *pp!=p; pp=&(*pp)->pHashNext); *pp = pNew; p = pNew; @@ -323,8 +342,9 @@ int sqlite3Fts5HashWrite( /* If this is a new rowid, append the 4-byte size field for the previous ** entry, and the new rowid for this entry. */ if( iRowid!=p->iRowid ){ - fts5HashAddPoslistSize(pHash, p); - p->nData += sqlite3Fts5PutVarint(&pPtr[p->nData], iRowid - p->iRowid); + u64 iDiff = (u64)iRowid - (u64)p->iRowid; + fts5HashAddPoslistSize(pHash, p, 0); + p->nData += sqlite3Fts5PutVarint(&pPtr[p->nData], iDiff); p->iRowid = iRowid; bNew = 1; p->iSzPoslist = p->nData; @@ -340,16 +360,16 @@ int sqlite3Fts5HashWrite( p->bContent = 1; }else{ /* Append a new column value, if necessary */ - assert( iCol>=p->iCol ); + assert_nc( iCol>=p->iCol ); if( iCol!=p->iCol ){ if( pHash->eDetail==FTS5_DETAIL_FULL ){ pPtr[p->nData++] = 0x01; p->nData += sqlite3Fts5PutVarint(&pPtr[p->nData], iCol); - p->iCol = iCol; + p->iCol = (i16)iCol; p->iPos = 0; }else{ bNew = 1; - p->iCol = iPos = iCol; + p->iCol = (i16)(iPos = iCol); } } @@ -392,10 +412,17 @@ static Fts5HashEntry *fts5HashEntryMerge( *ppOut = p1; p1 = 0; }else{ - int i = 0; - while( p1->zKey[i]==p2->zKey[i] ) i++; + char *zKey1 = fts5EntryKey(p1); + char *zKey2 = fts5EntryKey(p2); + int nMin = MIN(p1->nKey, p2->nKey); - if( ((u8)p1->zKey[i])>((u8)p2->zKey[i]) ){ + int cmp = memcmp(zKey1, zKey2, nMin); + if( cmp==0 ){ + cmp = p1->nKey - p2->nKey; + } + assert( cmp!=0 ); + + if( cmp>0 ){ /* p2 is smaller */ *ppOut = p2; ppOut = &p2->pScanNext; @@ -414,10 +441,8 @@ static Fts5HashEntry *fts5HashEntryMerge( } /* -** Extract all tokens from hash table iHash and link them into a list -** in sorted order. The hash table is cleared before returning. It is -** the responsibility of the caller to free the elements of the returned -** list. +** Link all tokens from hash table iHash into a list in sorted order. The +** tokens are not removed from the hash table. */ static int fts5HashEntrySort( Fts5Hash *pHash, @@ -431,14 +456,16 @@ static int fts5HashEntrySort( int i; *ppSorted = 0; - ap = sqlite3_malloc(sizeof(Fts5HashEntry*) * nMergeSlot); + ap = sqlite3_malloc64(sizeof(Fts5HashEntry*) * nMergeSlot); if( !ap ) return SQLITE_NOMEM; memset(ap, 0, sizeof(Fts5HashEntry*) * nMergeSlot); for(iSlot=0; iSlotnSlot; iSlot++){ Fts5HashEntry *pIter; for(pIter=pHash->aSlot[iSlot]; pIter; pIter=pIter->pHashNext){ - if( pTerm==0 || 0==memcmp(pIter->zKey, pTerm, nTerm) ){ + if( pTerm==0 + || (pIter->nKey>=nTerm && 0==memcmp(fts5EntryKey(pIter), pTerm, nTerm)) + ){ Fts5HashEntry *pEntry = pIter; pEntry->pScanNext = 0; for(i=0; ap[i]; i++){ @@ -455,7 +482,6 @@ static int fts5HashEntrySort( pList = fts5HashEntryMerge(pList, ap[i]); } - pHash->nEntry = 0; sqlite3_free(ap); *ppSorted = pList; return SQLITE_OK; @@ -466,23 +492,35 @@ static int fts5HashEntrySort( */ int sqlite3Fts5HashQuery( Fts5Hash *pHash, /* Hash table to query */ + int nPre, const char *pTerm, int nTerm, /* Query term */ - const u8 **ppDoclist, /* OUT: Pointer to doclist for pTerm */ + void **ppOut, /* OUT: Pointer to new object */ int *pnDoclist /* OUT: Size of doclist in bytes */ ){ unsigned int iHash = fts5HashKey(pHash->nSlot, (const u8*)pTerm, nTerm); + char *zKey = 0; Fts5HashEntry *p; for(p=pHash->aSlot[iHash]; p; p=p->pHashNext){ - if( memcmp(p->zKey, pTerm, nTerm)==0 && p->zKey[nTerm]==0 ) break; + zKey = fts5EntryKey(p); + if( nTerm==p->nKey && memcmp(zKey, pTerm, nTerm)==0 ) break; } if( p ){ - fts5HashAddPoslistSize(pHash, p); - *ppDoclist = (const u8*)&p->zKey[nTerm+1]; - *pnDoclist = p->nData - (FTS5_HASHENTRYSIZE + nTerm + 1); + int nHashPre = sizeof(Fts5HashEntry) + nTerm; + int nList = p->nData - nHashPre; + u8 *pRet = (u8*)(*ppOut = sqlite3_malloc64(nPre + nList + 10)); + if( pRet ){ + Fts5HashEntry *pFaux = (Fts5HashEntry*)&pRet[nPre-nHashPre]; + memcpy(&pRet[nPre], &((u8*)p)[nHashPre], nList); + nList += fts5HashAddPoslistSize(pHash, p, pFaux); + *pnDoclist = nList; + }else{ + *pnDoclist = 0; + return SQLITE_NOMEM; + } }else{ - *ppDoclist = 0; + *ppOut = 0; *pnDoclist = 0; } @@ -496,6 +534,28 @@ int sqlite3Fts5HashScanInit( return fts5HashEntrySort(p, pTerm, nTerm, &p->pScan); } +#ifdef SQLITE_DEBUG +static int fts5HashCount(Fts5Hash *pHash){ + int nEntry = 0; + int ii; + for(ii=0; iinSlot; ii++){ + Fts5HashEntry *p = 0; + for(p=pHash->aSlot[ii]; p; p=p->pHashNext){ + nEntry++; + } + } + return nEntry; +} +#endif + +/* +** Return true if the hash table is empty, false otherwise. +*/ +int sqlite3Fts5HashIsEmpty(Fts5Hash *pHash){ + assert( pHash->nEntry==fts5HashCount(pHash) ); + return pHash->nEntry==0; +} + void sqlite3Fts5HashScanNext(Fts5Hash *p){ assert( !sqlite3Fts5HashScanEof(p) ); p->pScan = p->pScan->pScanNext; @@ -508,20 +568,23 @@ int sqlite3Fts5HashScanEof(Fts5Hash *p){ void sqlite3Fts5HashScanEntry( Fts5Hash *pHash, const char **pzTerm, /* OUT: term (nul-terminated) */ + int *pnTerm, /* OUT: Size of term in bytes */ const u8 **ppDoclist, /* OUT: pointer to doclist */ int *pnDoclist /* OUT: size of doclist in bytes */ ){ Fts5HashEntry *p; if( (p = pHash->pScan) ){ - int nTerm = (int)strlen(p->zKey); - fts5HashAddPoslistSize(pHash, p); - *pzTerm = p->zKey; - *ppDoclist = (const u8*)&p->zKey[nTerm+1]; - *pnDoclist = p->nData - (FTS5_HASHENTRYSIZE + nTerm + 1); + char *zKey = fts5EntryKey(p); + int nTerm = p->nKey; + fts5HashAddPoslistSize(pHash, p, 0); + *pzTerm = zKey; + *pnTerm = nTerm; + *ppDoclist = (const u8*)&zKey[nTerm]; + *pnDoclist = p->nData - (sizeof(Fts5HashEntry) + nTerm); }else{ *pzTerm = 0; + *pnTerm = 0; *ppDoclist = 0; *pnDoclist = 0; } } - diff --git a/ext/fts5/fts5_index.c b/ext/fts5/fts5_index.c index c40ffbcbb5..acd0570a5d 100644 --- a/ext/fts5/fts5_index.c +++ b/ext/fts5/fts5_index.c @@ -54,6 +54,26 @@ # error "FTS5_MAX_PREFIX_INDEXES is too large" #endif +#define FTS5_MAX_LEVEL 64 + +/* +** There are two versions of the format used for the structure record: +** +** 1. the legacy format, that may be read by all fts5 versions, and +** +** 2. the V2 format, which is used by contentless_delete=1 databases. +** +** Both begin with a 4-byte "configuration cookie" value. Then, a legacy +** format structure record contains a varint - the number of levels in +** the structure. Whereas a V2 structure record contains the constant +** 4 bytes [0xff 0x00 0x00 0x01]. This is unambiguous as the value of a +** varint has to be at least 16256 to begin with "0xFF". And the default +** maximum number of levels is 64. +** +** See below for more on structure record formats. +*/ +#define FTS5_STRUCTURE_V2 "\xFF\x00\x00\x01" + /* ** Details: ** @@ -61,7 +81,7 @@ ** ** CREATE TABLE %_data(id INTEGER PRIMARY KEY, block BLOB); ** -** , contains the following 5 types of records. See the comments surrounding +** , contains the following 6 types of records. See the comments surrounding ** the FTS5_*_ROWID macros below for a description of how %_data rowids are ** assigned to each fo them. ** @@ -69,13 +89,13 @@ ** ** The set of segments that make up an index - the index structure - are ** recorded in a single record within the %_data table. The record consists -** of a single 32-bit configuration cookie value followed by a list of -** SQLite varints. If the FTS table features more than one index (because -** there are one or more prefix indexes), it is guaranteed that all share -** the same cookie value. +** of a single 32-bit configuration cookie value followed by a list of +** SQLite varints. ** -** Immediately following the configuration cookie, the record begins with -** three varints: +** If the structure record is a V2 record, the configuration cookie is +** followed by the following 4 bytes: [0xFF 0x00 0x00 0x01]. +** +** Next, the record continues with three varints: ** ** + number of levels, ** + total number of segments on all levels, @@ -90,6 +110,12 @@ ** + first leaf page number (often 1, always greater than 0) ** + final leaf page number ** +** Then, for V2 structures only: +** +** + lower origin counter value, +** + upper origin counter value, +** + the number of tombstone hash pages. +** ** 2. The Averages Record: ** ** A single record within the %_data table. The data is a list of varints. @@ -205,6 +231,38 @@ ** * A list of delta-encoded varints - the first rowid on each subsequent ** child page. ** +** 6. Tombstone Hash Page +** +** These records are only ever present in contentless_delete=1 tables. +** There are zero or more of these associated with each segment. They +** are used to store the tombstone rowids for rows contained in the +** associated segments. +** +** The set of nHashPg tombstone hash pages associated with a single +** segment together form a single hash table containing tombstone rowids. +** To find the page of the hash on which a key might be stored: +** +** iPg = (rowid % nHashPg) +** +** Then, within page iPg, which has nSlot slots: +** +** iSlot = (rowid / nHashPg) % nSlot +** +** Each tombstone hash page begins with an 8 byte header: +** +** 1-byte: Key-size (the size in bytes of each slot). Either 4 or 8. +** 1-byte: rowid-0-tombstone flag. This flag is only valid on the +** first tombstone hash page for each segment (iPg=0). If set, +** the hash table contains rowid 0. If clear, it does not. +** Rowid 0 is handled specially. +** 2-bytes: unused. +** 4-bytes: Big-endian integer containing number of entries on page. +** +** Following this are nSlot 4 or 8 byte slots (depending on the key-size +** in the first byte of the page header). The number of slots may be +** determined based on the size of the page record and the key-size: +** +** nSlot = (nByte - 8) / key-size */ /* @@ -238,11 +296,7 @@ #define FTS5_SEGMENT_ROWID(segid, pgno) fts5_dri(segid, 0, 0, pgno) #define FTS5_DLIDX_ROWID(segid, height, pgno) fts5_dri(segid, 1, height, pgno) - -/* -** Maximum segments permitted in a single index -*/ -#define FTS5_MAX_SEGMENT 2000 +#define FTS5_TOMBSTONE_ROWID(segid,ipg) fts5_dri(segid+(1<<16), 0, 0, ipg) #ifdef SQLITE_DEBUG int sqlite3Fts5Corrupt() { return SQLITE_CORRUPT_VTAB; } @@ -269,6 +323,9 @@ typedef struct Fts5SegWriter Fts5SegWriter; typedef struct Fts5Structure Fts5Structure; typedef struct Fts5StructureLevel Fts5StructureLevel; typedef struct Fts5StructureSegment Fts5StructureSegment; +typedef struct Fts5TokenDataIter Fts5TokenDataIter; +typedef struct Fts5TokenDataMap Fts5TokenDataMap; +typedef struct Fts5TombstoneArray Fts5TombstoneArray; struct Fts5Data { u8 *p; /* Pointer to buffer containing record */ @@ -278,6 +335,12 @@ struct Fts5Data { /* ** One object per %_data table. +** +** nContentlessDelete: +** The number of contentless delete operations since the most recent +** call to fts5IndexFlush() or fts5IndexDiscardData(). This is tracked +** so that extra auto-merge work can be done by fts5IndexFlush() to +** account for the delete operations. */ struct Fts5Index { Fts5Config *pConfig; /* Virtual table configuration */ @@ -292,18 +355,28 @@ struct Fts5Index { int nPendingData; /* Current bytes of pending data */ i64 iWriteRowid; /* Rowid for current doc being written */ int bDelete; /* Current write is a delete */ + int nContentlessDelete; /* Number of contentless delete ops */ + int nPendingRow; /* Number of INSERT in hash table */ /* Error state. */ int rc; /* Current error code */ + int flushRc; /* State used by the fts5DataXXX() functions. */ sqlite3_blob *pReader; /* RO incr-blob open on %_data table */ sqlite3_stmt *pWriter; /* "INSERT ... %_data VALUES(?,?)" */ sqlite3_stmt *pDeleter; /* "DELETE FROM %_data ... id>=? AND id<=?" */ sqlite3_stmt *pIdxWriter; /* "INSERT ... %_idx VALUES(?,?,?,?)" */ - sqlite3_stmt *pIdxDeleter; /* "DELETE FROM %_idx WHERE segid=? */ + sqlite3_stmt *pIdxDeleter; /* "DELETE FROM %_idx WHERE segid=?" */ sqlite3_stmt *pIdxSelect; + sqlite3_stmt *pIdxNextSelect; int nRead; /* Total number of blocks read */ + + sqlite3_stmt *pDeleteFromIdx; + + sqlite3_stmt *pDataVersion; + i64 iStructVersion; /* data_version when pStruct read */ + Fts5Structure *pStruct; /* Current db structure (or NULL) */ }; struct Fts5DoclistIter { @@ -320,11 +393,23 @@ struct Fts5DoclistIter { ** The contents of the "structure" record for each index are represented ** using an Fts5Structure record in memory. Which uses instances of the ** other Fts5StructureXXX types as components. +** +** nOriginCntr: +** This value is set to non-zero for structure records created for +** contentlessdelete=1 tables only. In that case it represents the +** origin value to apply to the next top-level segment created. */ struct Fts5StructureSegment { int iSegid; /* Segment id */ int pgnoFirst; /* First leaf page number in segment */ int pgnoLast; /* Last leaf page number in segment */ + + /* contentlessdelete=1 tables only: */ + u64 iOrigin1; + u64 iOrigin2; + int nPgTombstone; /* Number of tombstone hash table pages */ + u64 nEntryTombstone; /* Number of tombstone entries that "count" */ + u64 nEntry; /* Number of rows in this segment */ }; struct Fts5StructureLevel { int nMerge; /* Number of segments in incr-merge */ @@ -334,11 +419,16 @@ struct Fts5StructureLevel { struct Fts5Structure { int nRef; /* Object reference count */ u64 nWriteCounter; /* Total leaves written to level 0 */ + u64 nOriginCntr; /* Origin value for next top-level segment */ int nSegment; /* Total segments in this structure */ int nLevel; /* Number of levels in this index */ - Fts5StructureLevel aLevel[1]; /* Array of nLevel level objects */ + Fts5StructureLevel aLevel[FLEXARRAY]; /* Array of nLevel level objects */ }; +/* Size (in bytes) of an Fts5Structure object holding up to N levels */ +#define SZ_FTS5STRUCTURE(N) \ + (offsetof(Fts5Structure,aLevel) + (N)*sizeof(Fts5StructureLevel)) + /* ** An object of type Fts5SegWriter is used to write to segments. */ @@ -393,9 +483,6 @@ struct Fts5CResult { ** iLeafOffset: ** Byte offset within the current leaf that is the first byte of the ** position list data (one byte passed the position-list size field). -** rowid field of the current entry. Usually this is the size field of the -** position list data. The exception is if the rowid for the current entry -** is the last thing on the leaf page. ** ** pLeaf: ** Buffer containing current leaf page data. Set to NULL at EOF. @@ -425,6 +512,13 @@ struct Fts5CResult { ** ** iTermIdx: ** Index of current term on iTermLeafPgno. +** +** apTombstone/nTombstone: +** These are used for contentless_delete=1 tables only. When the cursor +** is first allocated, the apTombstone[] array is allocated so that it +** is large enough for all tombstones hash pages associated with the +** segment. The pages themselves are loaded lazily from the database as +** they are required. */ struct Fts5SegIter { Fts5StructureSegment *pSeg; /* Segment to iterate through */ @@ -432,7 +526,8 @@ struct Fts5SegIter { int iLeafPgno; /* Current leaf page number */ Fts5Data *pLeaf; /* Current leaf data */ Fts5Data *pNextLeaf; /* Leaf page (iLeafPgno+1) */ - int iLeafOffset; /* Byte offset within current leaf */ + i64 iLeafOffset; /* Byte offset within current leaf */ + Fts5TombstoneArray *pTombArray; /* Array of tombstone pages */ /* Next method */ void (*xNext)(Fts5Index*, Fts5SegIter*, int*); @@ -459,6 +554,49 @@ struct Fts5SegIter { u8 bDel; /* True if the delete flag is set */ }; +static int fts5IndexCorruptRowid(Fts5Index *pIdx, i64 iRowid){ + pIdx->rc = FTS5_CORRUPT; + sqlite3Fts5ConfigErrmsg(pIdx->pConfig, + "fts5: corruption found reading blob %lld from table \"%s\"", + iRowid, pIdx->pConfig->zName + ); + return SQLITE_CORRUPT_VTAB; +} +#define FTS5_CORRUPT_ROWID(pIdx, iRowid) fts5IndexCorruptRowid(pIdx, iRowid) + +static int fts5IndexCorruptIter(Fts5Index *pIdx, Fts5SegIter *pIter){ + pIdx->rc = FTS5_CORRUPT; + sqlite3Fts5ConfigErrmsg(pIdx->pConfig, + "fts5: corruption on page %d, segment %d, table \"%s\"", + pIter->iLeafPgno, pIter->pSeg->iSegid, pIdx->pConfig->zName + ); + return SQLITE_CORRUPT_VTAB; +} +#define FTS5_CORRUPT_ITER(pIdx, pIter) fts5IndexCorruptIter(pIdx, pIter) + +static int fts5IndexCorruptIdx(Fts5Index *pIdx){ + pIdx->rc = FTS5_CORRUPT; + sqlite3Fts5ConfigErrmsg(pIdx->pConfig, + "fts5: corruption in table \"%s\"", pIdx->pConfig->zName + ); + return SQLITE_CORRUPT_VTAB; +} +#define FTS5_CORRUPT_IDX(pIdx) fts5IndexCorruptIdx(pIdx) + + +/* +** Array of tombstone pages. Reference counted. +*/ +struct Fts5TombstoneArray { + int nRef; /* Number of pointers to this object */ + int nTombstone; + Fts5Data *apTombstone[FLEXARRAY]; /* Array of tombstone pages */ +}; + +/* Size (in bytes) of an Fts5TombstoneArray holding up to N tombstones */ +#define SZ_FTS5TOMBSTONEARRAY(N) \ + (offsetof(Fts5TombstoneArray,apTombstone)+(N)*sizeof(Fts5Data*)) + /* ** Argument is a pointer to an Fts5Data structure that contains a ** leaf page. @@ -503,12 +641,18 @@ struct Fts5SegIter { ** poslist: ** Used by sqlite3Fts5IterPoslist() when the poslist needs to be buffered. ** There is no way to tell if this is populated or not. +** +** pColset: +** If not NULL, points to an object containing a set of column indices. +** Only matches that occur in one of these columns will be returned. +** The Fts5Iter does not own the Fts5Colset object, and so it is not +** freed when the iterator is closed - it is owned by the upper layer. */ struct Fts5Iter { Fts5IndexIter base; /* Base class containing output vars */ + Fts5TokenDataIter *pTokenDataIter; Fts5Index *pIndex; /* Index that owns this iterator */ - Fts5Structure *pStruct; /* Database structure for this iterator */ Fts5Buffer poslist; /* Buffer containing current poslist */ Fts5Colset *pColset; /* Restrict matches to these columns */ @@ -521,9 +665,11 @@ struct Fts5Iter { i64 iSwitchRowid; /* Firstest rowid of other than aFirst[1] */ Fts5CResult *aFirst; /* Current merge state (see above) */ - Fts5SegIter aSeg[1]; /* Array of segment iterators */ + Fts5SegIter aSeg[FLEXARRAY]; /* Array of segment iterators */ }; +/* Size (in bytes) of an Fts5Iter object holding up to N segment iterators */ +#define SZ_FTS5ITER(N) (offsetof(Fts5Iter,aSeg)+(N)*sizeof(Fts5SegIter)) /* ** An instance of the following type is used to iterate through the contents @@ -551,9 +697,13 @@ struct Fts5DlidxLvl { struct Fts5DlidxIter { int nLvl; int iSegid; - Fts5DlidxLvl aLvl[1]; + Fts5DlidxLvl aLvl[FLEXARRAY]; }; +/* Size (in bytes) of an Fts5DlidxIter object with up to N levels */ +#define SZ_FTS5DLIDXITER(N) \ + (offsetof(Fts5DlidxIter,aLvl)+(N)*sizeof(Fts5DlidxLvl)) + static void fts5PutU16(u8 *aOut, u16 iVal){ aOut[0] = (iVal>>8); aOut[1] = (iVal&0xFF); @@ -563,13 +713,67 @@ static u16 fts5GetU16(const u8 *aIn){ return ((u16)aIn[0] << 8) + aIn[1]; } +/* +** The only argument points to a buffer at least 8 bytes in size. This +** function interprets the first 8 bytes of the buffer as a 64-bit big-endian +** unsigned integer and returns the result. +*/ +static u64 fts5GetU64(u8 *a){ + return ((u64)a[0] << 56) + + ((u64)a[1] << 48) + + ((u64)a[2] << 40) + + ((u64)a[3] << 32) + + ((u64)a[4] << 24) + + ((u64)a[5] << 16) + + ((u64)a[6] << 8) + + ((u64)a[7] << 0); +} + +/* +** The only argument points to a buffer at least 4 bytes in size. This +** function interprets the first 4 bytes of the buffer as a 32-bit big-endian +** unsigned integer and returns the result. +*/ +static u32 fts5GetU32(const u8 *a){ + return ((u32)a[0] << 24) + + ((u32)a[1] << 16) + + ((u32)a[2] << 8) + + ((u32)a[3] << 0); +} + +/* +** Write iVal, formated as a 64-bit big-endian unsigned integer, to the +** buffer indicated by the first argument. +*/ +static void fts5PutU64(u8 *a, u64 iVal){ + a[0] = ((iVal >> 56) & 0xFF); + a[1] = ((iVal >> 48) & 0xFF); + a[2] = ((iVal >> 40) & 0xFF); + a[3] = ((iVal >> 32) & 0xFF); + a[4] = ((iVal >> 24) & 0xFF); + a[5] = ((iVal >> 16) & 0xFF); + a[6] = ((iVal >> 8) & 0xFF); + a[7] = ((iVal >> 0) & 0xFF); +} + +/* +** Write iVal, formated as a 32-bit big-endian unsigned integer, to the +** buffer indicated by the first argument. +*/ +static void fts5PutU32(u8 *a, u32 iVal){ + a[0] = ((iVal >> 24) & 0xFF); + a[1] = ((iVal >> 16) & 0xFF); + a[2] = ((iVal >> 8) & 0xFF); + a[3] = ((iVal >> 0) & 0xFF); +} + /* ** Allocate and return a buffer at least nByte bytes in size. ** ** If an OOM error is encountered, return NULL and set the error code in ** the Fts5Index handle passed as the first argument. */ -static void *fts5IdxMalloc(Fts5Index *p, int nByte){ +static void *fts5IdxMalloc(Fts5Index *p, sqlite3_int64 nByte){ return sqlite3Fts5MallocZero(&p->rc, nByte); } @@ -602,8 +806,11 @@ static int fts5BufferCompareBlob( ** res = *pLeft - *pRight */ static int fts5BufferCompare(Fts5Buffer *pLeft, Fts5Buffer *pRight){ - int nCmp = MIN(pLeft->n, pRight->n); - int res = memcmp(pLeft->p, pRight->p, nCmp); + int nCmp, res; + nCmp = MIN(pLeft->n, pRight->n); + assert( nCmp<=0 || pLeft->p!=0 ); + assert( nCmp<=0 || pRight->p!=0 ); + res = fts5Memcmp(pLeft->p, pRight->p, nCmp); return (res==0 ? (pLeft->n - pRight->n) : res); } @@ -616,15 +823,16 @@ static int fts5LeafFirstTermOff(Fts5Data *pLeaf){ /* ** Close the read-only blob handle, if it is open. */ -static void fts5CloseReader(Fts5Index *p){ +static void fts5IndexCloseReader(Fts5Index *p){ if( p->pReader ){ + int rc; sqlite3_blob *pReader = p->pReader; p->pReader = 0; - sqlite3_blob_close(pReader); + rc = sqlite3_blob_close(pReader); + if( p->rc==SQLITE_OK ) p->rc = rc; } } - /* ** Retrieve a record from the %_data table. ** @@ -646,7 +854,7 @@ static Fts5Data *fts5DataRead(Fts5Index *p, i64 iRowid){ assert( p->pReader==0 ); p->pReader = pBlob; if( rc!=SQLITE_OK ){ - fts5CloseReader(p); + fts5IndexCloseReader(p); } if( rc==SQLITE_ABORT ) rc = SQLITE_OK; } @@ -665,16 +873,17 @@ static Fts5Data *fts5DataRead(Fts5Index *p, i64 iRowid){ ** All the reasons those functions might return SQLITE_ERROR - missing ** table, missing row, non-blob/text in block column - indicate ** backing store corruption. */ - if( rc==SQLITE_ERROR ) rc = FTS5_CORRUPT; + if( rc==SQLITE_ERROR ) rc = FTS5_CORRUPT_ROWID(p, iRowid); if( rc==SQLITE_OK ){ u8 *aOut = 0; /* Read blob data into this buffer */ - int nByte = sqlite3_blob_bytes(p->pReader); - int nAlloc = sizeof(Fts5Data) + nByte + FTS5_DATA_PADDING; - pRet = (Fts5Data*)sqlite3_malloc(nAlloc); + i64 nByte = sqlite3_blob_bytes(p->pReader); + i64 szData = (sizeof(Fts5Data) + 7) & ~7; + i64 nAlloc = szData + nByte + FTS5_DATA_PADDING; + pRet = (Fts5Data*)sqlite3_malloc64(nAlloc); if( pRet ){ pRet->nn = nByte; - aOut = pRet->p = (u8*)&pRet[1]; + aOut = pRet->p = (u8*)pRet + szData; }else{ rc = SQLITE_NOMEM; } @@ -687,6 +896,8 @@ static Fts5Data *fts5DataRead(Fts5Index *p, i64 iRowid){ pRet = 0; }else{ /* TODO1: Fix this */ + pRet->p[nByte] = 0x00; + pRet->p[nByte+1] = 0x00; pRet->szLeaf = fts5GetU16(&pRet->p[2]); } } @@ -695,9 +906,11 @@ static Fts5Data *fts5DataRead(Fts5Index *p, i64 iRowid){ } assert( (pRet==0)==(p->rc!=SQLITE_OK) ); + assert( pRet==0 || EIGHT_BYTE_ALIGNMENT( pRet->p ) ); return pRet; } + /* ** Release a reference to data record returned by an earlier call to ** fts5DataRead(). @@ -706,6 +919,18 @@ static void fts5DataRelease(Fts5Data *pData){ sqlite3_free(pData); } +static Fts5Data *fts5LeafRead(Fts5Index *p, i64 iRowid){ + Fts5Data *pRet = fts5DataRead(p, iRowid); + if( pRet ){ + if( pRet->nn<4 || pRet->szLeaf>pRet->nn ){ + FTS5_CORRUPT_ROWID(p, iRowid); + fts5DataRelease(pRet); + pRet = 0; + } + } + return pRet; +} + static int fts5IndexPrepareStmt( Fts5Index *p, sqlite3_stmt **ppStmt, @@ -713,7 +938,13 @@ static int fts5IndexPrepareStmt( ){ if( p->rc==SQLITE_OK ){ if( zSql ){ - p->rc = sqlite3_prepare_v2(p->pConfig->db, zSql, -1, ppStmt, 0); + int rc = sqlite3_prepare_v3(p->pConfig->db, zSql, -1, + SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_NO_VTAB, + ppStmt, 0); + /* If this prepare() call fails with SQLITE_ERROR, then one of the + ** %_idx or %_data tables has been removed or modified. Call this + ** corruption. */ + p->rc = (rc==SQLITE_ERROR ? SQLITE_CORRUPT : rc); }else{ p->rc = SQLITE_NOMEM; } @@ -742,6 +973,7 @@ static void fts5DataWrite(Fts5Index *p, i64 iRowid, const u8 *pData, int nData){ sqlite3_bind_blob(p->pWriter, 2, pData, nData, SQLITE_STATIC); sqlite3_step(p->pWriter); p->rc = sqlite3_reset(p->pWriter); + sqlite3_bind_null(p->pWriter, 2); } /* @@ -753,22 +985,12 @@ static void fts5DataDelete(Fts5Index *p, i64 iFirst, i64 iLast){ if( p->rc!=SQLITE_OK ) return; if( p->pDeleter==0 ){ - int rc; Fts5Config *pConfig = p->pConfig; char *zSql = sqlite3_mprintf( "DELETE FROM '%q'.'%q_data' WHERE id>=? AND id<=?", pConfig->zDb, pConfig->zName ); - if( zSql==0 ){ - rc = SQLITE_NOMEM; - }else{ - rc = sqlite3_prepare_v2(pConfig->db, zSql, -1, &p->pDeleter, 0); - sqlite3_free(zSql); - } - if( rc!=SQLITE_OK ){ - p->rc = rc; - return; - } + if( fts5IndexPrepareStmt(p, &p->pDeleter, zSql) ) return; } sqlite3_bind_int64(p->pDeleter, 1, iFirst); @@ -780,10 +1002,17 @@ static void fts5DataDelete(Fts5Index *p, i64 iFirst, i64 iLast){ /* ** Remove all records associated with segment iSegid. */ -static void fts5DataRemoveSegment(Fts5Index *p, int iSegid){ +static void fts5DataRemoveSegment(Fts5Index *p, Fts5StructureSegment *pSeg){ + int iSegid = pSeg->iSegid; i64 iFirst = FTS5_SEGMENT_ROWID(iSegid, 0); i64 iLast = FTS5_SEGMENT_ROWID(iSegid+1, 0)-1; fts5DataDelete(p, iFirst, iLast); + + if( pSeg->nPgTombstone ){ + i64 iTomb1 = FTS5_TOMBSTONE_ROWID(iSegid, 0); + i64 iTomb2 = FTS5_TOMBSTONE_ROWID(iSegid, pSeg->nPgTombstone-1); + fts5DataDelete(p, iTomb1, iTomb2); + } if( p->pIdxDeleter==0 ){ Fts5Config *pConfig = p->pConfig; fts5IndexPrepareStmt(p, &p->pIdxDeleter, sqlite3_mprintf( @@ -817,6 +1046,58 @@ static void fts5StructureRef(Fts5Structure *pStruct){ pStruct->nRef++; } +void *sqlite3Fts5StructureRef(Fts5Index *p){ + fts5StructureRef(p->pStruct); + return (void*)p->pStruct; +} +void sqlite3Fts5StructureRelease(void *p){ + if( p ){ + fts5StructureRelease((Fts5Structure*)p); + } +} +int sqlite3Fts5StructureTest(Fts5Index *p, void *pStruct){ + if( p->pStruct!=(Fts5Structure*)pStruct ){ + return SQLITE_ABORT; + } + return SQLITE_OK; +} + +/* +** Ensure that structure object (*pp) is writable. +** +** This function is a no-op if (*pRc) is not SQLITE_OK when it is called. If +** an error occurs, (*pRc) is set to an SQLite error code before returning. +*/ +static void fts5StructureMakeWritable(int *pRc, Fts5Structure **pp){ + Fts5Structure *p = *pp; + if( *pRc==SQLITE_OK && p->nRef>1 ){ + i64 nByte = SZ_FTS5STRUCTURE(p->nLevel); + Fts5Structure *pNew; + pNew = (Fts5Structure*)sqlite3Fts5MallocZero(pRc, nByte); + if( pNew ){ + int i; + memcpy(pNew, p, nByte); + for(i=0; inLevel; i++) pNew->aLevel[i].aSeg = 0; + for(i=0; inLevel; i++){ + Fts5StructureLevel *pLvl = &pNew->aLevel[i]; + nByte = sizeof(Fts5StructureSegment) * pNew->aLevel[i].nSeg; + pLvl->aSeg = (Fts5StructureSegment*)sqlite3Fts5MallocZero(pRc, nByte); + if( pLvl->aSeg==0 ){ + for(i=0; inLevel; i++){ + sqlite3_free(pNew->aLevel[i].aSeg); + } + sqlite3_free(pNew); + return; + } + memcpy(pLvl->aSeg, p->aLevel[i].aSeg, nByte); + } + p->nRef--; + pNew->nRef = 1; + } + *pp = pNew; + } +} + /* ** Deserialize and return the structure record currently stored in serialized ** form within buffer pData/nData. @@ -840,21 +1121,31 @@ static int fts5StructureDecode( int iLvl; int nLevel = 0; int nSegment = 0; - int nByte; /* Bytes of space to allocate at pRet */ + sqlite3_int64 nByte; /* Bytes of space to allocate at pRet */ Fts5Structure *pRet = 0; /* Structure object to return */ + int bStructureV2 = 0; /* True for FTS5_STRUCTURE_V2 */ + u64 nOriginCntr = 0; /* Largest origin value seen so far */ /* Grab the cookie value */ if( piCookie ) *piCookie = sqlite3Fts5Get32(pData); i = 4; + /* Check if this is a V2 structure record. Set bStructureV2 if it is. */ + if( 0==memcmp(&pData[i], FTS5_STRUCTURE_V2, 4) ){ + i += 4; + bStructureV2 = 1; + } + /* Read the total number of levels and segments from the start of the ** structure record. */ i += fts5GetVarint32(&pData[i], nLevel); i += fts5GetVarint32(&pData[i], nSegment); - nByte = ( - sizeof(Fts5Structure) + /* Main structure */ - sizeof(Fts5StructureLevel) * (nLevel-1) /* aLevel[] array */ - ); + if( nLevel>FTS5_MAX_SEGMENT || nLevel<0 + || nSegment>FTS5_MAX_SEGMENT || nSegment<0 + ){ + return FTS5_CORRUPT; + } + nByte = SZ_FTS5STRUCTURE(nLevel); pRet = (Fts5Structure*)sqlite3Fts5MallocZero(&rc, nByte); if( pRet ){ @@ -865,7 +1156,7 @@ static int fts5StructureDecode( for(iLvl=0; rc==SQLITE_OK && iLvlaLevel[iLvl]; - int nTotal; + int nTotal = 0; int iSeg; if( i>=nData ){ @@ -873,25 +1164,47 @@ static int fts5StructureDecode( }else{ i += fts5GetVarint32(&pData[i], pLvl->nMerge); i += fts5GetVarint32(&pData[i], nTotal); - assert( nTotal>=pLvl->nMerge ); + if( nTotalnMerge ) rc = FTS5_CORRUPT; pLvl->aSeg = (Fts5StructureSegment*)sqlite3Fts5MallocZero(&rc, nTotal * sizeof(Fts5StructureSegment) ); + nSegment -= nTotal; } if( rc==SQLITE_OK ){ pLvl->nSeg = nTotal; for(iSeg=0; iSegaSeg[iSeg]; if( i>=nData ){ rc = FTS5_CORRUPT; break; } - i += fts5GetVarint32(&pData[i], pLvl->aSeg[iSeg].iSegid); - i += fts5GetVarint32(&pData[i], pLvl->aSeg[iSeg].pgnoFirst); - i += fts5GetVarint32(&pData[i], pLvl->aSeg[iSeg].pgnoLast); + assert( pSeg!=0 ); + i += fts5GetVarint32(&pData[i], pSeg->iSegid); + i += fts5GetVarint32(&pData[i], pSeg->pgnoFirst); + i += fts5GetVarint32(&pData[i], pSeg->pgnoLast); + if( bStructureV2 ){ + i += fts5GetVarint(&pData[i], &pSeg->iOrigin1); + i += fts5GetVarint(&pData[i], &pSeg->iOrigin2); + i += fts5GetVarint32(&pData[i], pSeg->nPgTombstone); + i += fts5GetVarint(&pData[i], &pSeg->nEntryTombstone); + i += fts5GetVarint(&pData[i], &pSeg->nEntry); + nOriginCntr = MAX(nOriginCntr, pSeg->iOrigin2); + } + if( pSeg->pgnoLastpgnoFirst ){ + rc = FTS5_CORRUPT; + break; + } } + if( iLvl>0 && pLvl[-1].nMerge && nTotal==0 ) rc = FTS5_CORRUPT; + if( iLvl==nLevel-1 && pLvl->nMerge ) rc = FTS5_CORRUPT; } } + if( nSegment!=0 && rc==SQLITE_OK ) rc = FTS5_CORRUPT; + if( bStructureV2 ){ + pRet->nOriginCntr = nOriginCntr+1; + } + if( rc!=SQLITE_OK ){ fts5StructureRelease(pRet); pRet = 0; @@ -903,18 +1216,18 @@ static int fts5StructureDecode( } /* -** +** Add a level to the Fts5Structure.aLevel[] array of structure object +** (*ppStruct). */ static void fts5StructureAddLevel(int *pRc, Fts5Structure **ppStruct){ + fts5StructureMakeWritable(pRc, ppStruct); + assert( (ppStruct!=0 && (*ppStruct)!=0) || (*pRc)!=SQLITE_OK ); if( *pRc==SQLITE_OK ){ Fts5Structure *pStruct = *ppStruct; int nLevel = pStruct->nLevel; - int nByte = ( - sizeof(Fts5Structure) + /* Main structure */ - sizeof(Fts5StructureLevel) * (nLevel+1) /* aLevel[] array */ - ); + sqlite3_int64 nByte = SZ_FTS5STRUCTURE(nLevel+2); - pStruct = sqlite3_realloc(pStruct, nByte); + pStruct = sqlite3_realloc64(pStruct, nByte); if( pStruct ){ memset(&pStruct->aLevel[nLevel], 0, sizeof(Fts5StructureLevel)); pStruct->nLevel++; @@ -939,10 +1252,10 @@ static void fts5StructureExtendLevel( if( *pRc==SQLITE_OK ){ Fts5StructureLevel *pLvl = &pStruct->aLevel[iLvl]; Fts5StructureSegment *aNew; - int nByte; + sqlite3_int64 nByte; nByte = (pLvl->nSeg + nExtra) * sizeof(Fts5StructureSegment); - aNew = sqlite3_realloc(pLvl->aSeg, nByte); + aNew = sqlite3_realloc64(pLvl->aSeg, nByte); if( aNew ){ if( bInsert==0 ){ memset(&aNew[pLvl->nSeg], 0, sizeof(Fts5StructureSegment) * nExtra); @@ -958,6 +1271,56 @@ static void fts5StructureExtendLevel( } } +static Fts5Structure *fts5StructureReadUncached(Fts5Index *p){ + Fts5Structure *pRet = 0; + Fts5Config *pConfig = p->pConfig; + int iCookie; /* Configuration cookie */ + Fts5Data *pData; + + pData = fts5DataRead(p, FTS5_STRUCTURE_ROWID); + if( p->rc==SQLITE_OK ){ + /* TODO: Do we need this if the leaf-index is appended? Probably... */ + memset(&pData->p[pData->nn], 0, FTS5_DATA_PADDING); + p->rc = fts5StructureDecode(pData->p, pData->nn, &iCookie, &pRet); + if( p->rc==SQLITE_OK ){ + if( (pConfig->pgsz==0 || pConfig->iCookie!=iCookie) ){ + p->rc = sqlite3Fts5ConfigLoad(pConfig, iCookie); + } + }else if( p->rc==SQLITE_CORRUPT_VTAB ){ + sqlite3Fts5ConfigErrmsg(p->pConfig, + "fts5: corrupt structure record for table \"%s\"", p->pConfig->zName + ); + } + fts5DataRelease(pData); + if( p->rc!=SQLITE_OK ){ + fts5StructureRelease(pRet); + pRet = 0; + } + } + + return pRet; +} + +static i64 fts5IndexDataVersion(Fts5Index *p){ + i64 iVersion = 0; + + if( p->rc==SQLITE_OK ){ + if( p->pDataVersion==0 ){ + p->rc = fts5IndexPrepareStmt(p, &p->pDataVersion, + sqlite3_mprintf("PRAGMA %Q.data_version", p->pConfig->zDb) + ); + if( p->rc ) return 0; + } + + if( SQLITE_ROW==sqlite3_step(p->pDataVersion) ){ + iVersion = sqlite3_column_int64(p->pDataVersion, 0); + } + p->rc = sqlite3_reset(p->pDataVersion); + } + + return iVersion; +} + /* ** Read, deserialize and return the structure record. ** @@ -970,26 +1333,49 @@ static void fts5StructureExtendLevel( ** is called, it is a no-op. */ static Fts5Structure *fts5StructureRead(Fts5Index *p){ - Fts5Config *pConfig = p->pConfig; - Fts5Structure *pRet = 0; /* Object to return */ - int iCookie; /* Configuration cookie */ - Fts5Data *pData; - pData = fts5DataRead(p, FTS5_STRUCTURE_ROWID); - if( p->rc ) return 0; - /* TODO: Do we need this if the leaf-index is appended? Probably... */ - memset(&pData->p[pData->nn], 0, FTS5_DATA_PADDING); - p->rc = fts5StructureDecode(pData->p, pData->nn, &iCookie, &pRet); - if( p->rc==SQLITE_OK && pConfig->iCookie!=iCookie ){ - p->rc = sqlite3Fts5ConfigLoad(pConfig, iCookie); + if( p->pStruct==0 ){ + p->iStructVersion = fts5IndexDataVersion(p); + if( p->rc==SQLITE_OK ){ + p->pStruct = fts5StructureReadUncached(p); + } } - fts5DataRelease(pData); - if( p->rc!=SQLITE_OK ){ - fts5StructureRelease(pRet); - pRet = 0; +#if 0 + else{ + Fts5Structure *pTest = fts5StructureReadUncached(p); + if( pTest ){ + int i, j; + assert_nc( p->pStruct->nSegment==pTest->nSegment ); + assert_nc( p->pStruct->nLevel==pTest->nLevel ); + for(i=0; inLevel; i++){ + assert_nc( p->pStruct->aLevel[i].nMerge==pTest->aLevel[i].nMerge ); + assert_nc( p->pStruct->aLevel[i].nSeg==pTest->aLevel[i].nSeg ); + for(j=0; jaLevel[i].nSeg; j++){ + Fts5StructureSegment *p1 = &pTest->aLevel[i].aSeg[j]; + Fts5StructureSegment *p2 = &p->pStruct->aLevel[i].aSeg[j]; + assert_nc( p1->iSegid==p2->iSegid ); + assert_nc( p1->pgnoFirst==p2->pgnoFirst ); + assert_nc( p1->pgnoLast==p2->pgnoLast ); + } + } + fts5StructureRelease(pTest); + } + } +#endif + + if( p->rc!=SQLITE_OK ) return 0; + assert( p->iStructVersion!=0 ); + assert( p->pStruct!=0 ); + fts5StructureRef(p->pStruct); + return p->pStruct; +} + +static void fts5StructureInvalidate(Fts5Index *p){ + if( p->pStruct ){ + fts5StructureRelease(p->pStruct); + p->pStruct = 0; } - return pRet; } /* @@ -1033,6 +1419,7 @@ static void fts5StructureWrite(Fts5Index *p, Fts5Structure *pStruct){ Fts5Buffer buf; /* Buffer to serialize record into */ int iLvl; /* Used to iterate through levels */ int iCookie; /* Cookie value to store */ + int nHdr = (pStruct->nOriginCntr>0 ? (4+4+9+9+9) : (4+9+9)); assert( pStruct->nSegment==fts5StructureCountSegments(pStruct) ); memset(&buf, 0, sizeof(Fts5Buffer)); @@ -1041,9 +1428,12 @@ static void fts5StructureWrite(Fts5Index *p, Fts5Structure *pStruct){ iCookie = p->pConfig->iCookie; if( iCookie<0 ) iCookie = 0; - if( 0==sqlite3Fts5BufferSize(&p->rc, &buf, 4+9+9+9) ){ + if( 0==sqlite3Fts5BufferSize(&p->rc, &buf, nHdr) ){ sqlite3Fts5Put32(buf.p, iCookie); buf.n = 4; + if( pStruct->nOriginCntr>0 ){ + fts5BufferSafeAppendBlob(&buf, FTS5_STRUCTURE_V2, 4); + } fts5BufferSafeAppendVarint(&buf, pStruct->nLevel); fts5BufferSafeAppendVarint(&buf, pStruct->nSegment); fts5BufferSafeAppendVarint(&buf, (i64)pStruct->nWriteCounter); @@ -1057,9 +1447,17 @@ static void fts5StructureWrite(Fts5Index *p, Fts5Structure *pStruct){ assert( pLvl->nMerge<=pLvl->nSeg ); for(iSeg=0; iSegnSeg; iSeg++){ - fts5BufferAppendVarint(&p->rc, &buf, pLvl->aSeg[iSeg].iSegid); - fts5BufferAppendVarint(&p->rc, &buf, pLvl->aSeg[iSeg].pgnoFirst); - fts5BufferAppendVarint(&p->rc, &buf, pLvl->aSeg[iSeg].pgnoLast); + Fts5StructureSegment *pSeg = &pLvl->aSeg[iSeg]; + fts5BufferAppendVarint(&p->rc, &buf, pSeg->iSegid); + fts5BufferAppendVarint(&p->rc, &buf, pSeg->pgnoFirst); + fts5BufferAppendVarint(&p->rc, &buf, pSeg->pgnoLast); + if( pStruct->nOriginCntr>0 ){ + fts5BufferAppendVarint(&p->rc, &buf, pSeg->iOrigin1); + fts5BufferAppendVarint(&p->rc, &buf, pSeg->iOrigin2); + fts5BufferAppendVarint(&p->rc, &buf, pSeg->nPgTombstone); + fts5BufferAppendVarint(&p->rc, &buf, pSeg->nEntryTombstone); + fts5BufferAppendVarint(&p->rc, &buf, pSeg->nEntry); + } } } @@ -1202,9 +1600,9 @@ static int fts5DlidxLvlNext(Fts5DlidxLvl *pLvl){ } if( iOffnn ){ - i64 iVal; + u64 iVal; pLvl->iLeafPgno += (iOff - pLvl->iOff) + 1; - iOff += fts5GetVarint(&pData->p[iOff], (u64*)&iVal); + iOff += fts5GetVarint(&pData->p[iOff], &iVal); pLvl->iRowid += iVal; pLvl->iOff = iOff; }else{ @@ -1297,42 +1695,25 @@ static int fts5DlidxLvlPrev(Fts5DlidxLvl *pLvl){ pLvl->bEof = 1; }else{ u8 *a = pLvl->pData->p; - i64 iVal; - int iLimit; - int ii; - int nZero = 0; - - /* Currently iOff points to the first byte of a varint. This block - ** decrements iOff until it points to the first byte of the previous - ** varint. Taking care not to read any memory locations that occur - ** before the buffer in memory. */ - iLimit = (iOff>9 ? iOff-9 : 0); - for(iOff--; iOff>iLimit; iOff--){ - if( (a[iOff-1] & 0x80)==0 ) break; - } - fts5GetVarint(&a[iOff], (u64*)&iVal); - pLvl->iRowid -= iVal; - pLvl->iLeafPgno--; + pLvl->iOff = 0; + fts5DlidxLvlNext(pLvl); + while( 1 ){ + int nZero = 0; + int ii = pLvl->iOff; + u64 delta = 0; - /* Skip backwards past any 0x00 varints. */ - for(ii=iOff-1; ii>=pLvl->iFirstOff && a[ii]==0x00; ii--){ - nZero++; - } - if( ii>=pLvl->iFirstOff && (a[ii] & 0x80) ){ - /* The byte immediately before the last 0x00 byte has the 0x80 bit - ** set. So the last 0x00 is only a varint 0 if there are 8 more 0x80 - ** bytes before a[ii]. */ - int bZero = 0; /* True if last 0x00 counts */ - if( (ii-8)>=pLvl->iFirstOff ){ - int j; - for(j=1; j<=8 && (a[ii-j] & 0x80); j++); - bZero = (j>8); + while( a[ii]==0 ){ + nZero++; + ii++; } - if( bZero==0 ) nZero--; + ii += sqlite3Fts5GetVarint(&a[ii], &delta); + + if( ii>=iOff ) break; + pLvl->iLeafPgno += nZero+1; + pLvl->iRowid += delta; + pLvl->iOff = ii; } - pLvl->iLeafPgno -= nZero; - pLvl->iOff = iOff - nZero; } return pLvl->bEof; @@ -1389,10 +1770,10 @@ static Fts5DlidxIter *fts5DlidxIterInit( int bDone = 0; for(i=0; p->rc==SQLITE_OK && bDone==0; i++){ - int nByte = sizeof(Fts5DlidxIter) + i * sizeof(Fts5DlidxLvl); + sqlite3_int64 nByte = SZ_FTS5DLIDXITER(i+1); Fts5DlidxIter *pNew; - pNew = (Fts5DlidxIter*)sqlite3_realloc(pIter, nByte); + pNew = (Fts5DlidxIter*)sqlite3_realloc64(pIter, nByte); if( pNew==0 ){ p->rc = SQLITE_NOMEM; }else{ @@ -1447,7 +1828,7 @@ static void fts5SegIterNextPage( pIter->pLeaf = pIter->pNextLeaf; pIter->pNextLeaf = 0; }else if( pIter->iLeafPgno<=pSeg->pgnoLast ){ - pIter->pLeaf = fts5DataRead(p, + pIter->pLeaf = fts5LeafRead(p, FTS5_SEGMENT_ROWID(pSeg->iSegid, pIter->iLeafPgno) ); }else{ @@ -1525,13 +1906,13 @@ static void fts5SegIterLoadNPos(Fts5Index *p, Fts5SegIter *pIter){ static void fts5SegIterLoadRowid(Fts5Index *p, Fts5SegIter *pIter){ u8 *a = pIter->pLeaf->p; /* Buffer to read data from */ - int iOff = pIter->iLeafOffset; + i64 iOff = pIter->iLeafOffset; ASSERT_SZLEAF_OK(pIter->pLeaf); - if( iOff>=pIter->pLeaf->szLeaf ){ + while( iOff>=pIter->pLeaf->szLeaf ){ fts5SegIterNextPage(p, pIter); if( pIter->pLeaf==0 ){ - if( p->rc==SQLITE_OK ) p->rc = FTS5_CORRUPT; + if( p->rc==SQLITE_OK ) FTS5_CORRUPT_ITER(p, pIter); return; } iOff = 4; @@ -1558,16 +1939,17 @@ static void fts5SegIterLoadRowid(Fts5Index *p, Fts5SegIter *pIter){ */ static void fts5SegIterLoadTerm(Fts5Index *p, Fts5SegIter *pIter, int nKeep){ u8 *a = pIter->pLeaf->p; /* Buffer to read data from */ - int iOff = pIter->iLeafOffset; /* Offset to read at */ + i64 iOff = pIter->iLeafOffset; /* Offset to read at */ int nNew; /* Bytes of new data */ iOff += fts5GetVarint32(&a[iOff], nNew); - if( iOff+nNew>pIter->pLeaf->nn ){ - p->rc = FTS5_CORRUPT; + if( iOff+nNew>pIter->pLeaf->szLeaf || nKeep>pIter->term.n || nNew==0 ){ + FTS5_CORRUPT_ITER(p, pIter); return; } pIter->term.n = nKeep; fts5BufferAppendBlob(&p->rc, &pIter->term, nNew, &a[iOff]); + assert( pIter->term.n<=pIter->term.nSpace ); iOff += nNew; pIter->iTermLeafOffset = iOff; pIter->iTermLeafPgno = pIter->iLeafPgno; @@ -1598,6 +1980,25 @@ static void fts5SegIterSetNext(Fts5Index *p, Fts5SegIter *pIter){ } } +/* +** Allocate a tombstone hash page array object (pIter->pTombArray) for +** the iterator passed as the second argument. If an OOM error occurs, +** leave an error in the Fts5Index object. +*/ +static void fts5SegIterAllocTombstone(Fts5Index *p, Fts5SegIter *pIter){ + const i64 nTomb = (i64)pIter->pSeg->nPgTombstone; + if( nTomb>0 ){ + i64 nByte = SZ_FTS5TOMBSTONEARRAY(nTomb+1); + Fts5TombstoneArray *pNew; + pNew = (Fts5TombstoneArray*)sqlite3Fts5MallocZero(&p->rc, nByte); + if( pNew ){ + pNew->nTombstone = nTomb; + pNew->nRef = 1; + pIter->pTombArray = pNew; + } + } +} + /* ** Initialize the iterator object pIter to iterate through the entries in ** segment pSeg. The iterator is left pointing to the first entry when @@ -1626,16 +2027,20 @@ static void fts5SegIterInit( fts5SegIterSetNext(p, pIter); pIter->pSeg = pSeg; pIter->iLeafPgno = pSeg->pgnoFirst-1; - fts5SegIterNextPage(p, pIter); + do { + fts5SegIterNextPage(p, pIter); + }while( p->rc==SQLITE_OK && pIter->pLeaf && pIter->pLeaf->nn==4 ); } - if( p->rc==SQLITE_OK ){ + if( p->rc==SQLITE_OK && pIter->pLeaf ){ pIter->iLeafOffset = 4; + assert( pIter->pLeaf!=0 ); assert_nc( pIter->pLeaf->nn>4 ); - assert( fts5LeafFirstTermOff(pIter->pLeaf)==4 ); + assert_nc( fts5LeafFirstTermOff(pIter->pLeaf)==4 ); pIter->iPgidxOff = pIter->pLeaf->szLeaf+1; fts5SegIterLoadTerm(p, pIter, 0); fts5SegIterLoadNPos(p, pIter); + fts5SegIterAllocTombstone(p, pIter); } } @@ -1667,8 +2072,9 @@ static void fts5SegIterReverseInitPage(Fts5Index *p, Fts5SegIter *pIter){ ASSERT_SZLEAF_OK(pIter->pLeaf); while( 1 ){ - i64 iDelta = 0; + u64 iDelta = 0; + if( i>=n ) break; if( eDetail==FTS5_DETAIL_NONE ){ /* todo */ if( i=n ) break; - i += fts5GetVarint(&a[i], (u64*)&iDelta); + i += fts5GetVarint(&a[i], &iDelta); pIter->iRowid += iDelta; /* If necessary, grow the pIter->aRowidOffset[] array. */ if( iRowidOffset>=pIter->nRowidOffset ){ int nNew = pIter->nRowidOffset + 8; - int *aNew = (int*)sqlite3_realloc(pIter->aRowidOffset, nNew*sizeof(int)); + int *aNew = (int*)sqlite3_realloc64(pIter->aRowidOffset,nNew*sizeof(int)); if( aNew==0 ){ p->rc = SQLITE_NOMEM; break; @@ -1733,8 +2139,12 @@ static void fts5SegIterReverseNewPage(Fts5Index *p, Fts5SegIter *pIter){ int iRowidOff; iRowidOff = fts5LeafFirstRowidOff(pNew); if( iRowidOff ){ - pIter->pLeaf = pNew; - pIter->iLeafOffset = iRowidOff; + if( iRowidOff>=pNew->szLeaf ){ + FTS5_CORRUPT_ITER(p, pIter); + }else{ + pIter->pLeaf = pNew; + pIter->iLeafOffset = iRowidOff; + } } } @@ -1781,7 +2191,7 @@ static void fts5SegIterNext_Reverse( if( pIter->iRowidOffset>0 ){ u8 *a = pIter->pLeaf->p; int iOff; - i64 iDelta; + u64 iDelta; pIter->iRowidOffset--; pIter->iLeafOffset = pIter->aRowidOffset[pIter->iRowidOffset]; @@ -1790,7 +2200,7 @@ static void fts5SegIterNext_Reverse( if( p->pConfig->eDetail!=FTS5_DETAIL_NONE ){ iOff += pIter->nPos; } - fts5GetVarint(&a[iOff], (u64*)&iDelta); + fts5GetVarint(&a[iOff], &iDelta); pIter->iRowid -= iDelta; }else{ fts5SegIterReverseNewPage(p, pIter); @@ -1818,7 +2228,7 @@ static void fts5SegIterNext_None( iOff = pIter->iLeafOffset; /* Next entry is on the next page */ - if( pIter->pSeg && iOff>=pIter->pLeaf->szLeaf ){ + while( pIter->pSeg && iOff>=pIter->pLeaf->szLeaf ){ fts5SegIterNextPage(p, pIter); if( p->rc || pIter->pLeaf==0 ) return; pIter->iRowid = 0; @@ -1827,7 +2237,7 @@ static void fts5SegIterNext_None( if( iOffiEndofDoclist ){ /* Next entry is on the current page */ - i64 iDelta; + u64 iDelta; iOff += sqlite3Fts5GetVarint(&pIter->pLeaf->p[iOff], (u64*)&iDelta); pIter->iLeafOffset = iOff; pIter->iRowid += iDelta; @@ -1842,15 +2252,16 @@ static void fts5SegIterNext_None( }else{ const u8 *pList = 0; const char *zTerm = 0; + int nTerm = 0; int nList; sqlite3Fts5HashScanNext(p->pHash); - sqlite3Fts5HashScanEntry(p->pHash, &zTerm, &pList, &nList); + sqlite3Fts5HashScanEntry(p->pHash, &zTerm, &nTerm, &pList, &nList); if( pList==0 ) goto next_none_eof; pIter->pLeaf->p = (u8*)pList; pIter->pLeaf->nn = nList; pIter->pLeaf->szLeaf = nList; pIter->iEndofDoclist = nList; - sqlite3Fts5BufferSet(&p->rc,&pIter->term, (int)strlen(zTerm), (u8*)zTerm); + sqlite3Fts5BufferSet(&p->rc,&pIter->term, nTerm, (u8*)zTerm); pIter->iLeafOffset = fts5GetVarint(pList, (u64*)&pIter->iRowid); } @@ -1916,11 +2327,12 @@ static void fts5SegIterNext( }else if( pIter->pSeg==0 ){ const u8 *pList = 0; const char *zTerm = 0; + int nTerm = 0; int nList = 0; assert( (pIter->flags & FTS5_SEGITER_ONETERM) || pbNewTerm ); if( 0==(pIter->flags & FTS5_SEGITER_ONETERM) ){ sqlite3Fts5HashScanNext(p->pHash); - sqlite3Fts5HashScanEntry(p->pHash, &zTerm, &pList, &nList); + sqlite3Fts5HashScanEntry(p->pHash, &zTerm, &nTerm, &pList, &nList); } if( pList==0 ){ fts5DataRelease(pIter->pLeaf); @@ -1930,8 +2342,7 @@ static void fts5SegIterNext( pIter->pLeaf->nn = nList; pIter->pLeaf->szLeaf = nList; pIter->iEndofDoclist = nList+1; - sqlite3Fts5BufferSet(&p->rc, &pIter->term, (int)strlen(zTerm), - (u8*)zTerm); + sqlite3Fts5BufferSet(&p->rc, &pIter->term, nTerm, (u8*)zTerm); pIter->iLeafOffset = fts5GetVarint(pList, (u64*)&pIter->iRowid); *pbNewTerm = 1; } @@ -1950,21 +2361,20 @@ static void fts5SegIterNext( if( pLeaf->nn>pLeaf->szLeaf ){ pIter->iPgidxOff = pLeaf->szLeaf + fts5GetVarint32( &pLeaf->p[pLeaf->szLeaf], pIter->iEndofDoclist - ); + ); } - } else if( pLeaf->nn>pLeaf->szLeaf ){ pIter->iPgidxOff = pLeaf->szLeaf + fts5GetVarint32( &pLeaf->p[pLeaf->szLeaf], iOff - ); + ); pIter->iLeafOffset = iOff; pIter->iEndofDoclist = iOff; bNewTerm = 1; } assert_nc( iOffszLeaf ); if( iOff>pLeaf->szLeaf ){ - p->rc = FTS5_CORRUPT; + FTS5_CORRUPT_ITER(p, pIter); return; } } @@ -1984,13 +2394,9 @@ static void fts5SegIterNext( }else{ /* The following could be done by calling fts5SegIterLoadNPos(). But ** this block is particularly performance critical, so equivalent - ** code is inlined. - ** - ** Later: Switched back to fts5SegIterLoadNPos() because it supports - ** detail=none mode. Not ideal. - */ + ** code is inlined. */ int nSz; - assert( p->rc==SQLITE_OK ); + assert_nc( pIter->iLeafOffset<=pIter->pLeaf->nn ); fts5FastGetVarint32(pIter->pLeaf->p, pIter->iLeafOffset, nSz); pIter->bDel = (nSz & 0x0001); pIter->nPos = nSz>>1; @@ -2016,10 +2422,10 @@ static void fts5SegIterReverse(Fts5Index *p, Fts5SegIter *pIter){ Fts5Data *pLast = 0; int pgnoLast = 0; - if( pDlidx ){ + if( pDlidx && p->pConfig->iVersion==FTS5_CURRENT_VERSION ){ int iSegid = pIter->pSeg->iSegid; pgnoLast = fts5DlidxIterPgno(pDlidx); - pLast = fts5DataRead(p, FTS5_SEGMENT_ROWID(iSegid, pgnoLast)); + pLast = fts5LeafRead(p, FTS5_SEGMENT_ROWID(iSegid, pgnoLast)); }else{ Fts5Data *pLeaf = pIter->pLeaf; /* Current leaf data */ @@ -2046,7 +2452,7 @@ static void fts5SegIterReverse(Fts5Index *p, Fts5SegIter *pIter){ ** forward to find the page containing the last rowid. */ for(pgno=pIter->iLeafPgno+1; !p->rc && pgno<=pSeg->pgnoLast; pgno++){ i64 iAbs = FTS5_SEGMENT_ROWID(pSeg->iSegid, pgno); - Fts5Data *pNew = fts5DataRead(p, iAbs); + Fts5Data *pNew = fts5LeafRead(p, iAbs); if( pNew ){ int iRowid, bTermless; iRowid = fts5LeafFirstRowidOff(pNew); @@ -2076,16 +2482,21 @@ static void fts5SegIterReverse(Fts5Index *p, Fts5SegIter *pIter){ fts5DataRelease(pIter->pLeaf); pIter->pLeaf = pLast; pIter->iLeafPgno = pgnoLast; - iOff = fts5LeafFirstRowidOff(pLast); - iOff += fts5GetVarint(&pLast->p[iOff], (u64*)&pIter->iRowid); - pIter->iLeafOffset = iOff; + if( p->rc==SQLITE_OK ){ + iOff = fts5LeafFirstRowidOff(pLast); + if( iOff>pLast->szLeaf ){ + FTS5_CORRUPT_ITER(p, pIter); + return; + } + iOff += fts5GetVarint(&pLast->p[iOff], (u64*)&pIter->iRowid); + pIter->iLeafOffset = iOff; - if( fts5LeafIsTermless(pLast) ){ - pIter->iEndofDoclist = pLast->nn+1; - }else{ - pIter->iEndofDoclist = fts5LeafFirstTermOff(pLast); + if( fts5LeafIsTermless(pLast) ){ + pIter->iEndofDoclist = pLast->nn+1; + }else{ + pIter->iEndofDoclist = fts5LeafFirstTermOff(pLast); + } } - } fts5SegIterReverseInitPage(p, pIter); @@ -2137,23 +2548,26 @@ static void fts5LeafSeek( Fts5SegIter *pIter, /* Iterator to seek */ const u8 *pTerm, int nTerm /* Term to search for */ ){ - int iOff; + u32 iOff; const u8 *a = pIter->pLeaf->p; - int szLeaf = pIter->pLeaf->szLeaf; - int n = pIter->pLeaf->nn; + u32 n = (u32)pIter->pLeaf->nn; - int nMatch = 0; - int nKeep = 0; - int nNew = 0; - int iTermOff; - int iPgidx; /* Current offset in pgidx */ + u32 nMatch = 0; + u32 nKeep = 0; + u32 nNew = 0; + u32 iTermOff; + u32 iPgidx; /* Current offset in pgidx */ int bEndOfPage = 0; assert( p->rc==SQLITE_OK ); - iPgidx = szLeaf; + iPgidx = (u32)pIter->pLeaf->szLeaf; iPgidx += fts5GetVarint32(&a[iPgidx], iTermOff); iOff = iTermOff; + if( iOff>n ){ + FTS5_CORRUPT_ITER(p, pIter); + return; + } while( 1 ){ @@ -2165,15 +2579,15 @@ static void fts5LeafSeek( assert( nKeep>=nMatch ); if( nKeep==nMatch ){ - int nCmp; - int i; - nCmp = MIN(nNew, nTerm-nMatch); + u32 nCmp; + u32 i; + nCmp = (u32)MIN(nNew, nTerm-nMatch); for(i=0; i=n ){ + FTS5_CORRUPT_ITER(p, pIter); + return; + } + /* Read the nKeep field of the next term. */ fts5FastGetVarint32(a, iOff, nKeep); } @@ -2208,14 +2627,15 @@ static void fts5LeafSeek( if( pIter->pLeaf==0 ) return; a = pIter->pLeaf->p; if( fts5LeafIsTermless(pIter->pLeaf)==0 ){ - iPgidx = pIter->pLeaf->szLeaf; + iPgidx = (u32)pIter->pLeaf->szLeaf; iPgidx += fts5GetVarint32(&pIter->pLeaf->p[iPgidx], iOff); - if( iOff<4 || iOff>=pIter->pLeaf->szLeaf ){ - p->rc = FTS5_CORRUPT; + if( iOff<4 || (i64)iOff>=pIter->pLeaf->szLeaf ){ + FTS5_CORRUPT_ITER(p, pIter); + return; }else{ nKeep = 0; iTermOff = iOff; - n = pIter->pLeaf->nn; + n = (u32)pIter->pLeaf->nn; iOff += fts5GetVarint32(&a[iOff], nNew); break; } @@ -2224,7 +2644,10 @@ static void fts5LeafSeek( } search_success: - + if( (i64)iOff+nNew>n || nNew<1 ){ + FTS5_CORRUPT_ITER(p, pIter); + return; + } pIter->iLeafOffset = iOff + nNew; pIter->iTermLeafOffset = pIter->iLeafOffset; pIter->iTermLeafPgno = pIter->iLeafPgno; @@ -2245,6 +2668,18 @@ static void fts5LeafSeek( fts5SegIterLoadNPos(p, pIter); } +static sqlite3_stmt *fts5IdxSelectStmt(Fts5Index *p){ + if( p->pIdxSelect==0 ){ + Fts5Config *pConfig = p->pConfig; + fts5IndexPrepareStmt(p, &p->pIdxSelect, sqlite3_mprintf( + "SELECT pgno FROM '%q'.'%q_idx' WHERE " + "segid=? AND term<=? ORDER BY term DESC LIMIT 1", + pConfig->zDb, pConfig->zName + )); + } + return p->pIdxSelect; +} + /* ** Initialize the object pIter to point to term pTerm/nTerm within segment ** pSeg. If there is no such term in the index, the iterator is set to EOF. @@ -2262,6 +2697,7 @@ static void fts5SegIterSeekInit( int iPg = 1; int bGe = (flags & FTS5INDEX_QUERY_SCAN); int bDlidx = 0; /* True if there is a doclist-index */ + sqlite3_stmt *pIdxSelect = 0; assert( bGe==0 || (flags & FTS5INDEX_QUERY_DESC)==0 ); assert( pTerm && nTerm ); @@ -2270,23 +2706,17 @@ static void fts5SegIterSeekInit( /* This block sets stack variable iPg to the leaf page number that may ** contain term (pTerm/nTerm), if it is present in the segment. */ - if( p->pIdxSelect==0 ){ - Fts5Config *pConfig = p->pConfig; - fts5IndexPrepareStmt(p, &p->pIdxSelect, sqlite3_mprintf( - "SELECT pgno FROM '%q'.'%q_idx' WHERE " - "segid=? AND term<=? ORDER BY term DESC LIMIT 1", - pConfig->zDb, pConfig->zName - )); - } + pIdxSelect = fts5IdxSelectStmt(p); if( p->rc ) return; - sqlite3_bind_int(p->pIdxSelect, 1, pSeg->iSegid); - sqlite3_bind_blob(p->pIdxSelect, 2, pTerm, nTerm, SQLITE_STATIC); - if( SQLITE_ROW==sqlite3_step(p->pIdxSelect) ){ - i64 val = sqlite3_column_int(p->pIdxSelect, 0); + sqlite3_bind_int(pIdxSelect, 1, pSeg->iSegid); + sqlite3_bind_blob(pIdxSelect, 2, pTerm, nTerm, SQLITE_STATIC); + if( SQLITE_ROW==sqlite3_step(pIdxSelect) ){ + i64 val = sqlite3_column_int(pIdxSelect, 0); iPg = (int)(val>>1); bDlidx = (val & 0x0001); } - p->rc = sqlite3_reset(p->pIdxSelect); + p->rc = sqlite3_reset(pIdxSelect); + sqlite3_bind_null(pIdxSelect, 2); if( iPgpgnoFirst ){ iPg = pSeg->pgnoFirst; @@ -2300,7 +2730,7 @@ static void fts5SegIterSeekInit( fts5LeafSeek(p, bGe, pIter, pTerm, nTerm); } - if( p->rc==SQLITE_OK && bGe==0 ){ + if( p->rc==SQLITE_OK && (bGe==0 || (flags & FTS5INDEX_QUERY_SCANONETERM)) ){ pIter->flags |= FTS5_SEGITER_ONETERM; if( pIter->pLeaf ){ if( flags & FTS5INDEX_QUERY_DESC ){ @@ -2316,6 +2746,9 @@ static void fts5SegIterSeekInit( } fts5SegIterSetNext(p, pIter); + if( 0==(flags & FTS5INDEX_QUERY_SCANONETERM) ){ + fts5SegIterAllocTombstone(p, pIter); + } /* Either: ** @@ -2325,52 +2758,141 @@ static void fts5SegIterSeekInit( ** 4) the FTS5INDEX_QUERY_SCAN flag was set and the iterator points ** to an entry with a term greater than or equal to (pTerm/nTerm). */ - assert( p->rc!=SQLITE_OK /* 1 */ + assert_nc( p->rc!=SQLITE_OK /* 1 */ || pIter->pLeaf==0 /* 2 */ || fts5BufferCompareBlob(&pIter->term, pTerm, nTerm)==0 /* 3 */ || (bGe && fts5BufferCompareBlob(&pIter->term, pTerm, nTerm)>0) /* 4 */ ); } + /* -** Initialize the object pIter to point to term pTerm/nTerm within the -** in-memory hash table. If there is no such term in the hash-table, the -** iterator is set to EOF. -** -** If an error occurs, Fts5Index.rc is set to an appropriate error code. If -** an error has already occurred when this function is called, it is a no-op. +** SQL used by fts5SegIterNextInit() to find the page to open. */ -static void fts5SegIterHashInit( - Fts5Index *p, /* FTS5 backend */ - const u8 *pTerm, int nTerm, /* Term to seek to */ - int flags, /* Mask of FTS5INDEX_XXX flags */ - Fts5SegIter *pIter /* Object to populate */ -){ - const u8 *pList = 0; - int nList = 0; - const u8 *z = 0; - int n = 0; - - assert( p->pHash ); - assert( p->rc==SQLITE_OK ); - - if( pTerm==0 || (flags & FTS5INDEX_QUERY_SCAN) ){ +static sqlite3_stmt *fts5IdxNextStmt(Fts5Index *p){ + if( p->pIdxNextSelect==0 ){ + Fts5Config *pConfig = p->pConfig; + fts5IndexPrepareStmt(p, &p->pIdxNextSelect, sqlite3_mprintf( + "SELECT pgno FROM '%q'.'%q_idx' WHERE " + "segid=? AND term>? ORDER BY term ASC LIMIT 1", + pConfig->zDb, pConfig->zName + )); + + } + return p->pIdxNextSelect; +} + +/* +** This is similar to fts5SegIterSeekInit(), except that it initializes +** the segment iterator to point to the first term following the page +** with pToken/nToken on it. +*/ +static void fts5SegIterNextInit( + Fts5Index *p, + const char *pTerm, int nTerm, + Fts5StructureSegment *pSeg, /* Description of segment */ + Fts5SegIter *pIter /* Object to populate */ +){ + int iPg = -1; /* Page of segment to open */ + int bDlidx = 0; + sqlite3_stmt *pSel = 0; /* SELECT to find iPg */ + + pSel = fts5IdxNextStmt(p); + if( pSel ){ + assert( p->rc==SQLITE_OK ); + sqlite3_bind_int(pSel, 1, pSeg->iSegid); + sqlite3_bind_blob(pSel, 2, pTerm, nTerm, SQLITE_STATIC); + + if( sqlite3_step(pSel)==SQLITE_ROW ){ + i64 val = sqlite3_column_int64(pSel, 0); + iPg = (int)(val>>1); + bDlidx = (val & 0x0001); + } + p->rc = sqlite3_reset(pSel); + sqlite3_bind_null(pSel, 2); + if( p->rc ) return; + } + + memset(pIter, 0, sizeof(*pIter)); + pIter->pSeg = pSeg; + pIter->flags |= FTS5_SEGITER_ONETERM; + if( iPg>=0 ){ + pIter->iLeafPgno = iPg - 1; + fts5SegIterNextPage(p, pIter); + fts5SegIterSetNext(p, pIter); + } + if( pIter->pLeaf ){ + const u8 *a = pIter->pLeaf->p; + int iTermOff = 0; + + pIter->iPgidxOff = pIter->pLeaf->szLeaf; + pIter->iPgidxOff += fts5GetVarint32(&a[pIter->iPgidxOff], iTermOff); + pIter->iLeafOffset = iTermOff; + fts5SegIterLoadTerm(p, pIter, 0); + fts5SegIterLoadNPos(p, pIter); + if( bDlidx ) fts5SegIterLoadDlidx(p, pIter); + + assert( p->rc!=SQLITE_OK || + fts5BufferCompareBlob(&pIter->term, (const u8*)pTerm, nTerm)>0 + ); + } +} + +/* +** Initialize the object pIter to point to term pTerm/nTerm within the +** in-memory hash table. If there is no such term in the hash-table, the +** iterator is set to EOF. +** +** If an error occurs, Fts5Index.rc is set to an appropriate error code. If +** an error has already occurred when this function is called, it is a no-op. +*/ +static void fts5SegIterHashInit( + Fts5Index *p, /* FTS5 backend */ + const u8 *pTerm, int nTerm, /* Term to seek to */ + int flags, /* Mask of FTS5INDEX_XXX flags */ + Fts5SegIter *pIter /* Object to populate */ +){ + int nList = 0; + const u8 *z = 0; + int n = 0; + Fts5Data *pLeaf = 0; + + assert( p->pHash ); + assert( p->rc==SQLITE_OK ); + + if( pTerm==0 || (flags & FTS5INDEX_QUERY_SCAN) ){ + const u8 *pList = 0; + p->rc = sqlite3Fts5HashScanInit(p->pHash, (const char*)pTerm, nTerm); - sqlite3Fts5HashScanEntry(p->pHash, (const char**)&z, &pList, &nList); - n = (z ? (int)strlen((const char*)z) : 0); + sqlite3Fts5HashScanEntry(p->pHash, (const char**)&z, &n, &pList, &nList); + if( pList ){ + pLeaf = fts5IdxMalloc(p, sizeof(Fts5Data)); + if( pLeaf ){ + pLeaf->p = (u8*)pList; + } + } + + /* The call to sqlite3Fts5HashScanInit() causes the hash table to + ** fill the size field of all existing position lists. This means they + ** can no longer be appended to. Since the only scenario in which they + ** can be appended to is if the previous operation on this table was + ** a DELETE, by clearing the Fts5Index.bDelete flag we can avoid this + ** possibility altogether. */ + p->bDelete = 0; }else{ - pIter->flags |= FTS5_SEGITER_ONETERM; - sqlite3Fts5HashQuery(p->pHash, (const char*)pTerm, nTerm, &pList, &nList); + p->rc = sqlite3Fts5HashQuery(p->pHash, sizeof(Fts5Data), + (const char*)pTerm, nTerm, (void**)&pLeaf, &nList + ); + if( pLeaf ){ + pLeaf->p = (u8*)&pLeaf[1]; + } z = pTerm; n = nTerm; + pIter->flags |= FTS5_SEGITER_ONETERM; } - if( pList ){ - Fts5Data *pLeaf; + if( pLeaf ){ sqlite3Fts5BufferSet(&p->rc, &pIter->term, n, z); - pLeaf = fts5IdxMalloc(p, sizeof(Fts5Data)); - if( pLeaf==0 ) return; - pLeaf->p = (u8*)pList; pLeaf->nn = pLeaf->szLeaf = nList; pIter->pLeaf = pLeaf; pIter->iLeafOffset = fts5GetVarint(pLeaf->p, (u64*)&pIter->iRowid); @@ -2387,6 +2909,37 @@ static void fts5SegIterHashInit( fts5SegIterSetNext(p, pIter); } +/* +** Array ap[] contains n elements. Release each of these elements using +** fts5DataRelease(). Then free the array itself using sqlite3_free(). +*/ +static void fts5IndexFreeArray(Fts5Data **ap, int n){ + if( ap ){ + int ii; + for(ii=0; iinRef--; + if( p->nRef<=0 ){ + int ii; + for(ii=0; iinTombstone; ii++){ + fts5DataRelease(p->apTombstone[ii]); + } + sqlite3_free(p); + } + } +} + /* ** Zero the iterator passed as the only argument. */ @@ -2394,6 +2947,7 @@ static void fts5SegIterClear(Fts5SegIter *pIter){ fts5BufferFree(&pIter->term); fts5DataRelease(pIter->pLeaf); fts5DataRelease(pIter->pNextLeaf); + fts5TombstoneArrayDelete(pIter->pTombArray); fts5DlidxIterFree(pIter->pDlidx); sqlite3_free(pIter->aRowidOffset); memset(pIter, 0, sizeof(Fts5SegIter)); @@ -2423,7 +2977,7 @@ static void fts5AssertComparisonResult( assert( pRes->iFirst==i1 ); }else{ int nMin = MIN(p1->term.n, p2->term.n); - int res = memcmp(p1->term.p, p2->term.p, nMin); + int res = fts5Memcmp(p1->term.p, p2->term.p, nMin); if( res==0 ) res = p1->term.n - p2->term.n; if( res==0 ){ @@ -2523,11 +3077,10 @@ static int fts5MultiIterDoCompare(Fts5Iter *pIter, int iOut){ }else{ int res = fts5BufferCompare(&p1->term, &p2->term); if( res==0 ){ - assert( i2>i1 ); - assert( i2!=0 ); + assert_nc( i2>i1 ); + assert_nc( i2!=0 ); pRes->bTermEq = 1; if( p1->iRowid==p2->iRowid ){ - p1->bDel = p2->bDel; return i2; } res = ((p1->iRowid > p2->iRowid)==pIter->bRev) ? -1 : +1; @@ -2546,7 +3099,8 @@ static int fts5MultiIterDoCompare(Fts5Iter *pIter, int iOut){ /* ** Move the seg-iter so that it points to the first rowid on page iLeafPgno. -** It is an error if leaf iLeafPgno does not exist or contains no rowids. +** It is an error if leaf iLeafPgno does not exist. Unless the db is +** a 'secure-delete' db, if it contains no rowids then this is also an error. */ static void fts5SegIterGotoPage( Fts5Index *p, /* FTS5 backend object */ @@ -2556,26 +3110,28 @@ static void fts5SegIterGotoPage( assert( iLeafPgno>pIter->iLeafPgno ); if( iLeafPgno>pIter->pSeg->pgnoLast ){ - p->rc = FTS5_CORRUPT; + FTS5_CORRUPT_IDX(p); }else{ fts5DataRelease(pIter->pNextLeaf); pIter->pNextLeaf = 0; pIter->iLeafPgno = iLeafPgno-1; - fts5SegIterNextPage(p, pIter); - assert( p->rc!=SQLITE_OK || pIter->iLeafPgno==iLeafPgno ); - if( p->rc==SQLITE_OK ){ + while( p->rc==SQLITE_OK ){ int iOff; - u8 *a = pIter->pLeaf->p; - int n = pIter->pLeaf->szLeaf; - + fts5SegIterNextPage(p, pIter); + if( pIter->pLeaf==0 ) break; iOff = fts5LeafFirstRowidOff(pIter->pLeaf); - if( iOff<4 || iOff>=n ){ - p->rc = FTS5_CORRUPT; - }else{ - iOff += fts5GetVarint(&a[iOff], (u64*)&pIter->iRowid); - pIter->iLeafOffset = iOff; - fts5SegIterLoadNPos(p, pIter); + if( iOff>0 ){ + u8 *a = pIter->pLeaf->p; + int n = pIter->pLeaf->szLeaf; + if( iOff<4 || iOff>=n ){ + FTS5_CORRUPT_IDX(p); + }else{ + iOff += fts5GetVarint(&a[iOff], (u64*)&pIter->iRowid); + pIter->iLeafOffset = iOff; + fts5SegIterLoadNPos(p, pIter); + } + break; } } } @@ -2636,7 +3192,6 @@ static void fts5SegIterNextFrom( }while( p->rc==SQLITE_OK ); } - /* ** Free the iterator object passed as the second argument. */ @@ -2646,7 +3201,6 @@ static void fts5MultiIterFree(Fts5Iter *pIter){ for(i=0; inSeg; i++){ fts5SegIterClear(&pIter->aSeg[i]); } - fts5StructureRelease(pIter->pStruct); fts5BufferFree(&pIter->poslist); sqlite3_free(pIter); } @@ -2729,6 +3283,85 @@ static void fts5MultiIterSetEof(Fts5Iter *pIter){ pIter->iSwitchRowid = pSeg->iRowid; } +/* +** The argument to this macro must be an Fts5Data structure containing a +** tombstone hash page. This macro returns the key-size of the hash-page. +*/ +#define TOMBSTONE_KEYSIZE(pPg) (pPg->p[0]==4 ? 4 : 8) + +#define TOMBSTONE_NSLOT(pPg) \ + ((pPg->nn > 16) ? ((pPg->nn-8) / TOMBSTONE_KEYSIZE(pPg)) : 1) + +/* +** Query a single tombstone hash table for rowid iRowid. Return true if +** it is found or false otherwise. The tombstone hash table is one of +** nHashTable tables. +*/ +static int fts5IndexTombstoneQuery( + Fts5Data *pHash, /* Hash table page to query */ + int nHashTable, /* Number of pages attached to segment */ + u64 iRowid /* Rowid to query hash for */ +){ + const int szKey = TOMBSTONE_KEYSIZE(pHash); + const int nSlot = TOMBSTONE_NSLOT(pHash); + int iSlot = (iRowid / nHashTable) % nSlot; + int nCollide = nSlot; + + if( iRowid==0 ){ + return pHash->p[1]; + }else if( szKey==4 ){ + u32 *aSlot = (u32*)&pHash->p[8]; + while( aSlot[iSlot] ){ + if( fts5GetU32((u8*)&aSlot[iSlot])==iRowid ) return 1; + if( nCollide--==0 ) break; + iSlot = (iSlot+1)%nSlot; + } + }else{ + u64 *aSlot = (u64*)&pHash->p[8]; + while( aSlot[iSlot] ){ + if( fts5GetU64((u8*)&aSlot[iSlot])==iRowid ) return 1; + if( nCollide--==0 ) break; + iSlot = (iSlot+1)%nSlot; + } + } + + return 0; +} + +/* +** Return true if the iterator passed as the only argument points +** to an segment entry for which there is a tombstone. Return false +** if there is no tombstone or if the iterator is already at EOF. +*/ +static int fts5MultiIterIsDeleted(Fts5Iter *pIter){ + int iFirst = pIter->aFirst[1].iFirst; + Fts5SegIter *pSeg = &pIter->aSeg[iFirst]; + Fts5TombstoneArray *pArray = pSeg->pTombArray; + + if( pSeg->pLeaf && pArray ){ + /* Figure out which page the rowid might be present on. */ + int iPg = ((u64)pSeg->iRowid) % pArray->nTombstone; + assert( iPg>=0 ); + + /* If tombstone hash page iPg has not yet been loaded from the + ** database, load it now. */ + if( pArray->apTombstone[iPg]==0 ){ + pArray->apTombstone[iPg] = fts5DataRead(pIter->pIndex, + FTS5_TOMBSTONE_ROWID(pSeg->pSeg->iSegid, iPg) + ); + if( pArray->apTombstone[iPg]==0 ) return 0; + } + + return fts5IndexTombstoneQuery( + pArray->apTombstone[iPg], + pArray->nTombstone, + pSeg->iRowid + ); + } + + return 0; +} + /* ** Move the iterator to the next entry. ** @@ -2743,6 +3376,7 @@ static void fts5MultiIterNext( i64 iFrom /* Advance at least as far as this */ ){ int bUseFrom = bFrom; + assert( pIter->base.bEof==0 ); while( p->rc==SQLITE_OK ){ int iFirst = pIter->aFirst[1].iFirst; int bNewTerm = 0; @@ -2765,7 +3399,9 @@ static void fts5MultiIterNext( fts5AssertMultiIterSetup(p, pIter); assert( pSeg==&pIter->aSeg[pIter->aFirst[1].iFirst] && pSeg->pLeaf ); - if( pIter->bSkipEmpty==0 || pSeg->nPos ){ + if( (pIter->bSkipEmpty==0 || pSeg->nPos) + && 0==fts5MultiIterIsDeleted(pIter) + ){ pIter->xSetOutputs(pIter, pSeg); return; } @@ -2780,7 +3416,8 @@ static void fts5MultiIterNext2( ){ assert( pIter->bSkipEmpty ); if( p->rc==SQLITE_OK ){ - do { + *pbNewTerm = 0; + do{ int iFirst = pIter->aFirst[1].iFirst; Fts5SegIter *pSeg = &pIter->aSeg[iFirst]; int bNewTerm = 0; @@ -2793,12 +3430,12 @@ static void fts5MultiIterNext2( fts5MultiIterAdvanced(p, pIter, iFirst, 1); fts5MultiIterSetEof(pIter); *pbNewTerm = 1; - }else{ - *pbNewTerm = 0; } fts5AssertMultiIterSetup(p, pIter); - }while( fts5MultiIterIsEmpty(p, pIter) ); + }while( (fts5MultiIterIsEmpty(p, pIter) || fts5MultiIterIsDeleted(pIter)) + && (p->rc==SQLITE_OK) + ); } } @@ -2811,12 +3448,11 @@ static Fts5Iter *fts5MultiIterAlloc( int nSeg ){ Fts5Iter *pNew; - int nSlot; /* Power of two >= nSeg */ + i64 nSlot; /* Power of two >= nSeg */ for(nSlot=2; nSlotaSeg[] */ + SZ_FTS5ITER(nSlot) + /* pNew + pNew->aSeg[] */ sizeof(Fts5CResult) * nSlot /* pNew->aFirst[] */ ); if( pNew ){ @@ -2954,7 +3590,7 @@ static void fts5ChunkIterate( int pgno = pSeg->iLeafPgno; int pgnoSave = 0; - /* This function does notmwork with detail=none databases. */ + /* This function does not work with detail=none databases. */ assert( p->pConfig->eDetail!=FTS5_DETAIL_NONE ); if( (pSeg->flags & FTS5_SEGITER_REVERSE)==0 ){ @@ -2967,9 +3603,12 @@ static void fts5ChunkIterate( fts5DataRelease(pData); if( nRem<=0 ){ break; + }else if( pSeg->pSeg==0 ){ + FTS5_CORRUPT_IDX(p); + return; }else{ pgno++; - pData = fts5DataRead(p, FTS5_SEGMENT_ROWID(pSeg->pSeg->iSegid, pgno)); + pData = fts5LeafRead(p, FTS5_SEGMENT_ROWID(pSeg->pSeg->iSegid, pgno)); if( pData==0 ) break; pChunk = &pData->p[4]; nChunk = MIN(nRem, pData->szLeaf - 4); @@ -2994,7 +3633,12 @@ static void fts5SegiterPoslist( Fts5Colset *pColset, Fts5Buffer *pBuf ){ - if( 0==fts5BufferGrow(&p->rc, pBuf, pSeg->nPos) ){ + assert( pBuf!=0 ); + assert( pSeg!=0 ); + if( 0==fts5BufferGrow(&p->rc, pBuf, pSeg->nPos+FTS5_DATA_ZERO_PADDING) ){ + assert( pBuf->p!=0 ); + assert( pBuf->nSpace >= pBuf->n+pSeg->nPos+FTS5_DATA_ZERO_PADDING ); + memset(&pBuf->p[pBuf->n+pSeg->nPos], 0, FTS5_DATA_ZERO_PADDING); if( pColset==0 ){ fts5ChunkIterate(p, pSeg, (void*)pBuf, fts5PoslistCallback); }else{ @@ -3017,66 +3661,72 @@ static void fts5SegiterPoslist( } /* -** IN/OUT parameter (*pa) points to a position list n bytes in size. If -** the position list contains entries for column iCol, then (*pa) is set -** to point to the sub-position-list for that column and the number of -** bytes in it returned. Or, if the argument position list does not -** contain any entries for column iCol, return 0. +** Parameter pPos points to a buffer containing a position list, size nPos. +** This function filters it according to pColset (which must be non-NULL) +** and sets pIter->base.pData/nData to point to the new position list. +** If memory is required for the new position list, use buffer pIter->poslist. +** Or, if the new position list is a contiguous subset of the input, set +** pIter->base.pData/nData to point directly to it. +** +** This function is a no-op if *pRc is other than SQLITE_OK when it is +** called. If an OOM error is encountered, *pRc is set to SQLITE_NOMEM +** before returning. */ -static int fts5IndexExtractCol( - const u8 **pa, /* IN/OUT: Pointer to poslist */ - int n, /* IN: Size of poslist in bytes */ - int iCol /* Column to extract from poslist */ +static void fts5IndexExtractColset( + int *pRc, + Fts5Colset *pColset, /* Colset to filter on */ + const u8 *pPos, int nPos, /* Position list */ + Fts5Iter *pIter ){ - int iCurrent = 0; /* Anything before the first 0x01 is col 0 */ - const u8 *p = *pa; - const u8 *pEnd = &p[n]; /* One byte past end of position list */ + if( *pRc==SQLITE_OK ){ + const u8 *p = pPos; + const u8 *aCopy = p; + const u8 *pEnd = &p[nPos]; /* One byte past end of position list */ + int i = 0; + int iCurrent = 0; - while( iCol>iCurrent ){ - /* Advance pointer p until it points to pEnd or an 0x01 byte that is - ** not part of a varint. Note that it is not possible for a negative - ** or extremely large varint to occur within an uncorrupted position - ** list. So the last byte of each varint may be assumed to have a clear - ** 0x80 bit. */ - while( *p!=0x01 ){ - while( *p++ & 0x80 ); - if( p>=pEnd ) return 0; - } - *pa = p++; - iCurrent = *p++; - if( iCurrent & 0x80 ){ - p--; - p += fts5GetVarint32(p, iCurrent); + if( pColset->nCol>1 && sqlite3Fts5BufferSize(pRc, &pIter->poslist, nPos) ){ + return; } - } - if( iCol!=iCurrent ) return 0; - /* Advance pointer p until it points to pEnd or an 0x01 byte that is - ** not part of a varint */ - while( paiCol[i]nCol ){ + pIter->base.pData = pIter->poslist.p; + pIter->base.nData = pIter->poslist.n; + return; + } + } -static int fts5IndexExtractColset ( - Fts5Colset *pColset, /* Colset to filter on */ - const u8 *pPos, int nPos, /* Position list */ - Fts5Buffer *pBuf /* Output buffer */ -){ - int rc = SQLITE_OK; - int i; + /* Advance pointer p until it points to pEnd or an 0x01 byte that is + ** not part of a varint */ + while( pnCol; i++){ - const u8 *pSub = pPos; - int nSub = fts5IndexExtractCol(&pSub, nPos, pColset->aiCol[i]); - if( nSub ){ - fts5BufferAppendBlob(&rc, pBuf, nSub, pSub); + if( pColset->aiCol[i]==iCurrent ){ + if( pColset->nCol==1 ){ + pIter->base.pData = aCopy; + pIter->base.nData = p-aCopy; + return; + } + fts5BufferSafeAppendBlob(&pIter->poslist, aCopy, p-aCopy); + } + if( p>=pEnd ){ + pIter->base.pData = pIter->poslist.p; + pIter->base.nData = pIter->poslist.n; + return; + } + aCopy = p++; + iCurrent = *p++; + if( iCurrent & 0x80 ){ + p--; + p += fts5GetVarint32(p, iCurrent); + } } } - return rc; + } /* @@ -3113,6 +3763,15 @@ static void fts5IterSetOutputs_Nocolset(Fts5Iter *pIter, Fts5SegIter *pSeg){ } } +/* +** xSetOutputs callback used when the Fts5Colset object has nCol==0 (match +** against no columns at all). +*/ +static void fts5IterSetOutputs_ZeroColset(Fts5Iter *pIter, Fts5SegIter *pSeg){ + UNUSED_PARAM(pSeg); + pIter->base.nData = 0; +} + /* ** xSetOutputs callback used by detail=col when there is a column filter ** and there are 100 or more columns. Also called as a fallback from @@ -3162,7 +3821,7 @@ static void fts5IterSetOutputs_Col100(Fts5Iter *pIter, Fts5SegIter *pSeg){ if( aiCol==aiColEnd ) goto setoutputs_col_out; } if( *aiCol==iPrev ){ - *aOut++ = (iPrev - iPrevOut) + 2; + *aOut++ = (u8)((iPrev - iPrevOut) + 2); iPrevOut = iPrev; } } @@ -3187,15 +3846,9 @@ static void fts5IterSetOutputs_Full(Fts5Iter *pIter, Fts5SegIter *pSeg){ /* All data is stored on the current page. Populate the output ** variables to point into the body of the page object. */ const u8 *a = &pSeg->pLeaf->p[pSeg->iLeafOffset]; - if( pColset->nCol==1 ){ - pIter->base.nData = fts5IndexExtractCol(&a, pSeg->nPos,pColset->aiCol[0]); - pIter->base.pData = a; - }else{ - fts5BufferZero(&pIter->poslist); - fts5IndexExtractColset(pColset, a, pSeg->nPos, &pIter->poslist); - pIter->base.pData = pIter->poslist.p; - pIter->base.nData = pIter->poslist.n; - } + int *pRc = &pIter->pIndex->rc; + fts5BufferZero(&pIter->poslist); + fts5IndexExtractColset(pRc, pColset, a, pSeg->nPos, pIter); }else{ /* The data is distributed over two or more pages. Copy it into the ** Fts5Iter.poslist buffer and then set the output pointer to point @@ -3208,6 +3861,7 @@ static void fts5IterSetOutputs_Full(Fts5Iter *pIter, Fts5SegIter *pSeg){ } static void fts5IterSetOutputCb(int *pRc, Fts5Iter *pIter){ + assert( pIter!=0 || (*pRc)!=SQLITE_OK ); if( *pRc==SQLITE_OK ){ Fts5Config *pConfig = pIter->pIndex->pConfig; if( pConfig->eDetail==FTS5_DETAIL_NONE ){ @@ -3218,6 +3872,10 @@ static void fts5IterSetOutputCb(int *pRc, Fts5Iter *pIter){ pIter->xSetOutputs = fts5IterSetOutputs_Nocolset; } + else if( pIter->pColset->nCol==0 ){ + pIter->xSetOutputs = fts5IterSetOutputs_ZeroColset; + } + else if( pConfig->eDetail==FTS5_DETAIL_FULL ){ pIter->xSetOutputs = fts5IterSetOutputs_Full; } @@ -3234,6 +3892,32 @@ static void fts5IterSetOutputCb(int *pRc, Fts5Iter *pIter){ } } +/* +** All the component segment-iterators of pIter have been set up. This +** functions finishes setup for iterator pIter itself. +*/ +static void fts5MultiIterFinishSetup(Fts5Index *p, Fts5Iter *pIter){ + int iIter; + for(iIter=pIter->nSeg-1; iIter>0; iIter--){ + int iEq; + if( (iEq = fts5MultiIterDoCompare(pIter, iIter)) ){ + Fts5SegIter *pSeg = &pIter->aSeg[iEq]; + if( p->rc==SQLITE_OK ) pSeg->xNext(p, pSeg, 0); + fts5MultiIterAdvanced(p, pIter, iEq, iIter); + } + } + fts5MultiIterSetEof(pIter); + fts5AssertMultiIterSetup(p, pIter); + + if( (pIter->bSkipEmpty && fts5MultiIterIsEmpty(p, pIter)) + || fts5MultiIterIsDeleted(pIter) + ){ + fts5MultiIterNext(p, pIter, 0, 0); + }else if( pIter->base.bEof==0 ){ + Fts5SegIter *pSeg = &pIter->aSeg[pIter->aFirst[1].iFirst]; + pIter->xSetOutputs(pIter, pSeg); + } +} /* ** Allocate a new Fts5Iter object. @@ -3269,18 +3953,19 @@ static void fts5MultiIterNew( if( iLevel<0 ){ assert( pStruct->nSegment==fts5StructureCountSegments(pStruct) ); nSeg = pStruct->nSegment; - nSeg += (p->pHash ? 1 : 0); + nSeg += (p->pHash && 0==(flags & FTS5INDEX_QUERY_SKIPHASH)); }else{ nSeg = MIN(pStruct->aLevel[iLevel].nSeg, nSegment); } } *ppOut = pNew = fts5MultiIterAlloc(p, nSeg); - if( pNew==0 ) return; + if( pNew==0 ){ + assert( p->rc!=SQLITE_OK ); + goto fts5MultiIterNew_post_check; + } pNew->bRev = (0!=(flags & FTS5INDEX_QUERY_DESC)); pNew->bSkipEmpty = (0!=(flags & FTS5INDEX_QUERY_SKIPEMPTY)); - pNew->pStruct = pStruct; pNew->pColset = pColset; - fts5StructureRef(pStruct); if( (flags & FTS5INDEX_QUERY_NOOUTPUT)==0 ){ fts5IterSetOutputCb(&p->rc, pNew); } @@ -3289,7 +3974,7 @@ static void fts5MultiIterNew( if( p->rc==SQLITE_OK ){ if( iLevel<0 ){ Fts5StructureLevel *pEnd = &pStruct->aLevel[pStruct->nLevel]; - if( p->pHash ){ + if( p->pHash && 0==(flags & FTS5INDEX_QUERY_SKIPHASH) ){ /* Add a segment iterator for the current contents of the hash table. */ Fts5SegIter *pIter = &pNew->aSeg[iIter++]; fts5SegIterHashInit(p, pTerm, nTerm, flags, pIter); @@ -3314,33 +3999,20 @@ static void fts5MultiIterNew( assert( iIter==nSeg ); } - /* If the above was successful, each component iterators now points + /* If the above was successful, each component iterator now points ** to the first entry in its segment. In this case initialize the ** aFirst[] array. Or, if an error has occurred, free the iterator ** object and set the output variable to NULL. */ if( p->rc==SQLITE_OK ){ - for(iIter=pNew->nSeg-1; iIter>0; iIter--){ - int iEq; - if( (iEq = fts5MultiIterDoCompare(pNew, iIter)) ){ - Fts5SegIter *pSeg = &pNew->aSeg[iEq]; - if( p->rc==SQLITE_OK ) pSeg->xNext(p, pSeg, 0); - fts5MultiIterAdvanced(p, pNew, iEq, iIter); - } - } - fts5MultiIterSetEof(pNew); - fts5AssertMultiIterSetup(p, pNew); - - if( pNew->bSkipEmpty && fts5MultiIterIsEmpty(p, pNew) ){ - fts5MultiIterNext(p, pNew, 0, 0); - }else if( pNew->base.bEof==0 ){ - Fts5SegIter *pSeg = &pNew->aSeg[pNew->aFirst[1].iFirst]; - pNew->xSetOutputs(pNew, pSeg); - } - + fts5MultiIterFinishSetup(p, pNew); }else{ fts5MultiIterFree(pNew); *ppOut = 0; } + +fts5MultiIterNew_post_check: + assert( (*ppOut)!=0 || p->rc!=SQLITE_OK ); + return; } /* @@ -3357,7 +4029,6 @@ static void fts5MultiIterNew2( pNew = fts5MultiIterAlloc(p, 2); if( pNew ){ Fts5SegIter *pIter = &pNew->aSeg[1]; - pIter->flags = FTS5_SEGITER_ONETERM; if( pData->szLeaf>0 ){ pIter->pLeaf = pData; @@ -3388,7 +4059,8 @@ static void fts5MultiIterNew2( ** False otherwise. */ static int fts5MultiIterEof(Fts5Index *p, Fts5Iter *pIter){ - assert( p->rc + assert( pIter!=0 || p->rc!=SQLITE_OK ); + assert( p->rc!=SQLITE_OK || (pIter->aSeg[ pIter->aFirst[1].iFirst ].pLeaf==0)==pIter->base.bEof ); return (p->rc || pIter->base.bEof); @@ -3448,18 +4120,47 @@ static int fts5AllocateSegid(Fts5Index *p, Fts5Structure *pStruct){ if( pStruct->nSegment>=FTS5_MAX_SEGMENT ){ p->rc = SQLITE_FULL; }else{ - while( iSegid==0 ){ - int iLvl, iSeg; - sqlite3_randomness(sizeof(u32), (void*)&iSegid); - iSegid = iSegid & ((1 << FTS5_DATA_ID_B)-1); - for(iLvl=0; iLvlnLevel; iLvl++){ - for(iSeg=0; iSegaLevel[iLvl].nSeg; iSeg++){ - if( iSegid==pStruct->aLevel[iLvl].aSeg[iSeg].iSegid ){ - iSegid = 0; - } + /* FTS5_MAX_SEGMENT is currently defined as 2000. So the following + ** array is 63 elements, or 252 bytes, in size. */ + u32 aUsed[(FTS5_MAX_SEGMENT+31) / 32]; + int iLvl, iSeg; + int i; + u32 mask; + memset(aUsed, 0, sizeof(aUsed)); + for(iLvl=0; iLvlnLevel; iLvl++){ + for(iSeg=0; iSegaLevel[iLvl].nSeg; iSeg++){ + int iId = pStruct->aLevel[iLvl].aSeg[iSeg].iSegid; + if( iId<=FTS5_MAX_SEGMENT && iId>0 ){ + aUsed[(iId-1) / 32] |= (u32)1 << ((iId-1) % 32); } } } + + for(i=0; aUsed[i]==0xFFFFFFFF; i++); + mask = aUsed[i]; + for(iSegid=0; mask & ((u32)1 << iSegid); iSegid++); + iSegid += 1 + i*32; + +#ifdef SQLITE_DEBUG + for(iLvl=0; iLvlnLevel; iLvl++){ + for(iSeg=0; iSegaLevel[iLvl].nSeg; iSeg++){ + assert_nc( iSegid!=pStruct->aLevel[iLvl].aSeg[iSeg].iSegid ); + } + } + assert_nc( iSegid>0 && iSegid<=FTS5_MAX_SEGMENT ); + + { + sqlite3_stmt *pIdxSelect = fts5IdxSelectStmt(p); + if( p->rc==SQLITE_OK ){ + u8 aBlob[2] = {0xff, 0xff}; + sqlite3_bind_int(pIdxSelect, 1, iSegid); + sqlite3_bind_blob(pIdxSelect, 2, aBlob, 2, SQLITE_STATIC); + assert_nc( sqlite3_step(pIdxSelect)!=SQLITE_ROW ); + p->rc = sqlite3_reset(pIdxSelect); + sqlite3_bind_null(pIdxSelect, 2); + } + } +#endif } } @@ -3474,7 +4175,10 @@ static void fts5IndexDiscardData(Fts5Index *p){ if( p->pHash ){ sqlite3Fts5HashClear(p->pHash); p->nPendingData = 0; + p->nPendingRow = 0; + p->flushRc = SQLITE_OK; } + p->nContentlessDelete = 0; } /* @@ -3524,13 +4228,13 @@ static int fts5WriteDlidxGrow( int nLvl ){ if( p->rc==SQLITE_OK && nLvl>=pWriter->nDlidx ){ - Fts5DlidxWriter *aDlidx = (Fts5DlidxWriter*)sqlite3_realloc( + Fts5DlidxWriter *aDlidx = (Fts5DlidxWriter*)sqlite3_realloc64( pWriter->aDlidx, sizeof(Fts5DlidxWriter) * nLvl ); if( aDlidx==0 ){ p->rc = SQLITE_NOMEM; }else{ - int nByte = sizeof(Fts5DlidxWriter) * (nLvl - pWriter->nDlidx); + size_t nByte = sizeof(Fts5DlidxWriter) * (nLvl - pWriter->nDlidx); memset(&aDlidx[pWriter->nDlidx], 0, nByte); pWriter->aDlidx = aDlidx; pWriter->nDlidx = nLvl; @@ -3583,6 +4287,7 @@ static void fts5WriteFlushBtree(Fts5Index *p, Fts5SegWriter *pWriter){ sqlite3_bind_int64(p->pIdxWriter, 3, bFlag + ((i64)pWriter->iBtPage<<1)); sqlite3_step(p->pIdxWriter); p->rc = sqlite3_reset(p->pIdxWriter); + sqlite3_bind_null(p->pIdxWriter, 2); } pWriter->iBtPage = 0; } @@ -3602,8 +4307,10 @@ static void fts5WriteBtreeTerm( int nTerm, const u8 *pTerm /* First term on new page */ ){ fts5WriteFlushBtree(p, pWriter); - fts5BufferSet(&p->rc, &pWriter->btterm, nTerm, pTerm); - pWriter->iBtPage = pWriter->writer.pgno; + if( p->rc==SQLITE_OK ){ + fts5BufferSet(&p->rc, &pWriter->btterm, nTerm, pTerm); + pWriter->iBtPage = pWriter->writer.pgno; + } } /* @@ -3685,7 +4392,7 @@ static void fts5WriteDlidxAppend( } if( pDlidx->bPrevValid ){ - iVal = iRowid - pDlidx->iPrev; + iVal = (u64)iRowid - (u64)pDlidx->iPrev; }else{ i64 iPgno = (i==0 ? pWriter->writer.pgno : pDlidx[-1].pgno); assert( pDlidx->buf.n==0 ); @@ -3754,6 +4461,7 @@ static void fts5WriteAppendTerm( int nPrefix; /* Bytes of prefix compression for term */ Fts5PageWriter *pPage = &pWriter->writer; Fts5Buffer *pPgidx = &pWriter->writer.pgidx; + int nMin = MIN(pPage->term.n, nTerm); assert( p->rc==SQLITE_OK ); assert( pPage->buf.n>=4 ); @@ -3763,6 +4471,7 @@ static void fts5WriteAppendTerm( if( (pPage->buf.n + pPgidx->n + nTerm + 2)>=p->pConfig->pgsz ){ if( pPage->buf.n>4 ){ fts5WriteFlushLeaf(p, pWriter); + if( p->rc!=SQLITE_OK ) return; } fts5BufferGrow(&p->rc, &pPage->buf, nTerm+FTS5_DATA_PADDING); } @@ -3795,13 +4504,14 @@ static void fts5WriteAppendTerm( ** inefficient, but still correct. */ int n = nTerm; if( pPage->term.n ){ - n = 1 + fts5PrefixCompress(pPage->term.n, pPage->term.p, pTerm); + n = 1 + fts5PrefixCompress(nMin, pPage->term.p, pTerm); } fts5WriteBtreeTerm(p, pWriter, n, pTerm); + if( p->rc!=SQLITE_OK ) return; pPage = &pWriter->writer; } }else{ - nPrefix = fts5PrefixCompress(pPage->term.n, pPage->term.p, pTerm); + nPrefix = fts5PrefixCompress(nMin, pPage->term.p, pTerm); fts5BufferAppendVarint(&p->rc, &pPage->buf, nPrefix); } @@ -3848,8 +4558,10 @@ static void fts5WriteAppendRowid( if( pWriter->bFirstRowidInDoclist || pWriter->bFirstRowidInPage ){ fts5BufferAppendVarint(&p->rc, &pPage->buf, iRowid); }else{ - assert( p->rc || iRowid>pWriter->iPrevRowid ); - fts5BufferAppendVarint(&p->rc, &pPage->buf, iRowid - pWriter->iPrevRowid); + assert_nc( p->rc || iRowid>pWriter->iPrevRowid ); + fts5BufferAppendVarint(&p->rc, &pPage->buf, + (u64)iRowid - (u64)pWriter->iPrevRowid + ); } pWriter->iPrevRowid = iRowid; pWriter->bFirstRowidInDoclist = 0; @@ -3867,7 +4579,7 @@ static void fts5WriteAppendPoslistData( const u8 *a = aData; int n = nData; - assert( p->pConfig->pgsz>0 ); + assert( p->pConfig->pgsz>0 || p->rc!=SQLITE_OK ); while( p->rc==SQLITE_OK && (pPage->buf.n + pPage->pgidx.n + n)>=p->pConfig->pgsz ){ @@ -3904,7 +4616,9 @@ static void fts5WriteFinish( fts5WriteFlushLeaf(p, pWriter); } *pnLeaf = pLeaf->pgno-1; - fts5WriteFlushBtree(p, pWriter); + if( pLeaf->pgno>1 ){ + fts5WriteFlushBtree(p, pWriter); + } } fts5BufferFree(&pLeaf->term); fts5BufferFree(&pLeaf->buf); @@ -3968,7 +4682,7 @@ static void fts5TrimSegments(Fts5Index *p, Fts5Iter *pIter){ int i; Fts5Buffer buf; memset(&buf, 0, sizeof(Fts5Buffer)); - for(i=0; inSeg; i++){ + for(i=0; inSeg && p->rc==SQLITE_OK; i++){ Fts5SegIter *pSeg = &pIter->aSeg[i]; if( pSeg->pSeg==0 ){ /* no-op */ @@ -3986,35 +4700,44 @@ static void fts5TrimSegments(Fts5Index *p, Fts5Iter *pIter){ u8 aHdr[4] = {0x00, 0x00, 0x00, 0x00}; iLeafRowid = FTS5_SEGMENT_ROWID(iId, pSeg->iTermLeafPgno); - pData = fts5DataRead(p, iLeafRowid); + pData = fts5LeafRead(p, iLeafRowid); if( pData ){ - fts5BufferZero(&buf); - fts5BufferGrow(&p->rc, &buf, pData->nn); - fts5BufferAppendBlob(&p->rc, &buf, sizeof(aHdr), aHdr); - fts5BufferAppendVarint(&p->rc, &buf, pSeg->term.n); - fts5BufferAppendBlob(&p->rc, &buf, pSeg->term.n, pSeg->term.p); - fts5BufferAppendBlob(&p->rc, &buf, pData->szLeaf-iOff, &pData->p[iOff]); - if( p->rc==SQLITE_OK ){ - /* Set the szLeaf field */ - fts5PutU16(&buf.p[2], (u16)buf.n); - } + if( iOff>pData->szLeaf ){ + /* This can occur if the pages that the segments occupy overlap - if + ** a single page has been assigned to more than one segment. In + ** this case a prior iteration of this loop may have corrupted the + ** segment currently being trimmed. */ + FTS5_CORRUPT_ROWID(p, iLeafRowid); + }else{ + fts5BufferZero(&buf); + fts5BufferGrow(&p->rc, &buf, pData->nn); + fts5BufferAppendBlob(&p->rc, &buf, sizeof(aHdr), aHdr); + fts5BufferAppendVarint(&p->rc, &buf, pSeg->term.n); + fts5BufferAppendBlob(&p->rc, &buf, pSeg->term.n, pSeg->term.p); + fts5BufferAppendBlob(&p->rc, &buf,pData->szLeaf-iOff,&pData->p[iOff]); + if( p->rc==SQLITE_OK ){ + /* Set the szLeaf field */ + fts5PutU16(&buf.p[2], (u16)buf.n); + } - /* Set up the new page-index array */ - fts5BufferAppendVarint(&p->rc, &buf, 4); - if( pSeg->iLeafPgno==pSeg->iTermLeafPgno - && pSeg->iEndofDoclistszLeaf - ){ - int nDiff = pData->szLeaf - pSeg->iEndofDoclist; - fts5BufferAppendVarint(&p->rc, &buf, buf.n - 1 - nDiff - 4); - fts5BufferAppendBlob(&p->rc, &buf, - pData->nn - pSeg->iPgidxOff, &pData->p[pSeg->iPgidxOff] - ); - } + /* Set up the new page-index array */ + fts5BufferAppendVarint(&p->rc, &buf, 4); + if( pSeg->iLeafPgno==pSeg->iTermLeafPgno + && pSeg->iEndofDoclistszLeaf + && pSeg->iPgidxOff<=pData->nn + ){ + int nDiff = pData->szLeaf - pSeg->iEndofDoclist; + fts5BufferAppendVarint(&p->rc, &buf, buf.n - 1 - nDiff - 4); + fts5BufferAppendBlob(&p->rc, &buf, + pData->nn - pSeg->iPgidxOff, &pData->p[pSeg->iPgidxOff] + ); + } + pSeg->pSeg->pgnoFirst = pSeg->iTermLeafPgno; + fts5DataDelete(p, FTS5_SEGMENT_ROWID(iId, 1), iLeafRowid); + fts5DataWrite(p, iLeafRowid, buf.p, buf.n); + } fts5DataRelease(pData); - pSeg->pSeg->pgnoFirst = pSeg->iTermLeafPgno; - fts5DataDelete(p, FTS5_SEGMENT_ROWID(iId, 1), iLeafRowid); - fts5DataWrite(p, iLeafRowid, buf.p, buf.n); } } } @@ -4051,6 +4774,7 @@ static void fts5IndexMergeLevel( int bOldest; /* True if the output segment is the oldest */ int eDetail = p->pConfig->eDetail; const int flags = FTS5INDEX_QUERY_NOOUTPUT; + int bTermWritten = 0; /* True if current term already output */ assert( iLvlnLevel ); assert( pLvl->nMerge<=pLvl->nSeg ); @@ -4091,6 +4815,12 @@ static void fts5IndexMergeLevel( /* Read input from all segments in the input level */ nInput = pLvl->nSeg; + + /* Set the range of origins that will go into the output segment. */ + if( pStruct->nOriginCntr>0 ){ + pSeg->iOrigin1 = pLvl->aSeg[0].iOrigin1; + pSeg->iOrigin2 = pLvl->aSeg[pLvl->nSeg-1].iOrigin2; + } } bOldest = (pLvlOut->nSeg==1 && pStruct->nLevel==iLvl+2); @@ -4104,18 +4834,22 @@ static void fts5IndexMergeLevel( int nTerm; const u8 *pTerm; - /* Check for key annihilation. */ - if( pSegIter->nPos==0 && (bOldest || pSegIter->bDel==0) ) continue; - pTerm = fts5MultiIterTerm(pIter, &nTerm); - if( nTerm!=term.n || memcmp(pTerm, term.p, nTerm) ){ + if( nTerm!=term.n || fts5Memcmp(pTerm, term.p, nTerm) ){ if( pnRem && writer.nLeafWritten>nRem ){ break; } + fts5BufferSet(&p->rc, &term, nTerm, pTerm); + bTermWritten =0; + } + /* Check for key annihilation. */ + if( pSegIter->nPos==0 && (bOldest || pSegIter->bDel==0) ) continue; + + if( p->rc==SQLITE_OK && bTermWritten==0 ){ /* This is a new term. Append a term to the output segment. */ fts5WriteAppendTerm(p, &writer, nTerm, pTerm); - fts5BufferSet(&p->rc, &term, nTerm, pTerm); + bTermWritten = 1; } /* Append the rowid to the output */ @@ -4141,12 +4875,16 @@ static void fts5IndexMergeLevel( ** and last leaf page number at the same time. */ fts5WriteFinish(p, &writer, &pSeg->pgnoLast); + assert( pIter!=0 || p->rc!=SQLITE_OK ); if( fts5MultiIterEof(p, pIter) ){ int i; /* Remove the redundant segments from the %_data table */ + assert( pSeg->nEntry==0 ); for(i=0; iaSeg[i].iSegid); + Fts5StructureSegment *pOld = &pLvl->aSeg[i]; + pSeg->nEntry += (pOld->nEntry - pOld->nEntryTombstone); + fts5DataRemoveSegment(p, pOld); } /* Remove the redundant segments from the input level */ @@ -4172,29 +4910,76 @@ static void fts5IndexMergeLevel( if( pnRem ) *pnRem -= writer.nLeafWritten; } +/* +** If this is not a contentless_delete=1 table, or if the 'deletemerge' +** configuration option is set to 0, then this function always returns -1. +** Otherwise, it searches the structure object passed as the second argument +** for a level suitable for merging due to having a large number of +** tombstones in the tombstone hash. If one is found, its index is returned. +** Otherwise, if there is no suitable level, -1. +*/ +static int fts5IndexFindDeleteMerge(Fts5Index *p, Fts5Structure *pStruct){ + Fts5Config *pConfig = p->pConfig; + int iRet = -1; + if( pConfig->bContentlessDelete && pConfig->nDeleteMerge>0 ){ + int ii; + int nBest = 0; + + for(ii=0; iinLevel; ii++){ + Fts5StructureLevel *pLvl = &pStruct->aLevel[ii]; + i64 nEntry = 0; + i64 nTomb = 0; + int iSeg; + for(iSeg=0; iSegnSeg; iSeg++){ + nEntry += pLvl->aSeg[iSeg].nEntry; + nTomb += pLvl->aSeg[iSeg].nEntryTombstone; + } + assert_nc( nEntry>0 || pLvl->nSeg==0 ); + if( nEntry>0 ){ + int nPercent = (nTomb * 100) / nEntry; + if( nPercent>=pConfig->nDeleteMerge && nPercent>nBest ){ + iRet = ii; + nBest = nPercent; + } + } + + /* If pLvl is already the input level to an ongoing merge, look no + ** further for a merge candidate. The caller should be allowed to + ** continue merging from pLvl first. */ + if( pLvl->nMerge ) break; + } + } + return iRet; +} + /* ** Do up to nPg pages of automerge work on the index. +** +** Return true if any changes were actually made, or false otherwise. */ -static void fts5IndexMerge( +static int fts5IndexMerge( Fts5Index *p, /* FTS5 backend object */ Fts5Structure **ppStruct, /* IN/OUT: Current structure of index */ - int nPg /* Pages of work to do */ + int nPg, /* Pages of work to do */ + int nMin /* Minimum number of segments to merge */ ){ int nRem = nPg; + int bRet = 0; Fts5Structure *pStruct = *ppStruct; while( nRem>0 && p->rc==SQLITE_OK ){ int iLvl; /* To iterate through levels */ int iBestLvl = 0; /* Level offering the most input segments */ int nBest = 0; /* Number of input segments on best level */ - /* Set iBestLvl to the level to read input segments from. */ + /* Set iBestLvl to the level to read input segments from. Or to -1 if + ** there is no level suitable to merge segments from. */ assert( pStruct->nLevel>0 ); for(iLvl=0; iLvlnLevel; iLvl++){ Fts5StructureLevel *pLvl = &pStruct->aLevel[iLvl]; if( pLvl->nMerge ){ if( pLvl->nMerge>nBest ){ iBestLvl = iLvl; - nBest = pLvl->nMerge; + nBest = nMin; } break; } @@ -4203,25 +4988,21 @@ static void fts5IndexMerge( iBestLvl = iLvl; } } - - /* If nBest is still 0, then the index must be empty. */ -#ifdef SQLITE_DEBUG - for(iLvl=0; nBest==0 && iLvlnLevel; iLvl++){ - assert( pStruct->aLevel[iLvl].nSeg==0 ); + if( nBestpConfig->nAutomerge - && pStruct->aLevel[iBestLvl].nMerge==0 - ){ - break; - } + if( iBestLvl<0 ) break; + bRet = 1; fts5IndexMergeLevel(p, &pStruct, iBestLvl, &nRem); if( p->rc==SQLITE_OK && pStruct->aLevel[iBestLvl].nMerge==0 ){ fts5StructurePromote(p, iBestLvl+1, pStruct); } + + if( nMin==1 ) nMin = 2; } *ppStruct = pStruct; + return bRet; } /* @@ -4237,7 +5018,7 @@ static void fts5IndexAutomerge( Fts5Structure **ppStruct, /* IN/OUT: Current structure of index */ int nLeaf /* Number of output leaves just written */ ){ - if( p->rc==SQLITE_OK && p->pConfig->nAutomerge>0 ){ + if( p->rc==SQLITE_OK && p->pConfig->nAutomerge>0 && ALWAYS((*ppStruct)!=0) ){ Fts5Structure *pStruct = *ppStruct; u64 nWrite; /* Initial value of write-counter */ int nWork; /* Number of work-quanta to perform */ @@ -4249,7 +5030,7 @@ static void fts5IndexAutomerge( pStruct->nWriteCounter += nLeaf; nRem = (int)(p->nWorkUnit * nWork * pStruct->nLevel); - fts5IndexMerge(p, ppStruct, nRem); + fts5IndexMerge(p, ppStruct, nRem, p->pConfig->nAutomerge); } } @@ -4259,16 +5040,16 @@ static void fts5IndexCrisismerge( ){ const int nCrisis = p->pConfig->nCrisisMerge; Fts5Structure *pStruct = *ppStruct; - int iLvl = 0; - - assert( p->rc!=SQLITE_OK || pStruct->nLevel>0 ); - while( p->rc==SQLITE_OK && pStruct->aLevel[iLvl].nSeg>=nCrisis ){ - fts5IndexMergeLevel(p, &pStruct, iLvl, 0); - assert( p->rc!=SQLITE_OK || pStruct->nLevel>(iLvl+1) ); - fts5StructurePromote(p, iLvl+1, pStruct); - iLvl++; + if( pStruct && pStruct->nLevel>0 ){ + int iLvl = 0; + while( p->rc==SQLITE_OK && pStruct->aLevel[iLvl].nSeg>=nCrisis ){ + fts5IndexMergeLevel(p, &pStruct, iLvl, 0); + assert( p->rc!=SQLITE_OK || pStruct->nLevel>(iLvl+1) ); + fts5StructurePromote(p, iLvl+1, pStruct); + iLvl++; + } + *ppStruct = pStruct; } - *ppStruct = pStruct; } static int fts5IndexReturn(Fts5Index *p){ @@ -4277,8 +5058,16 @@ static int fts5IndexReturn(Fts5Index *p){ return rc; } -typedef struct Fts5FlushCtx Fts5FlushCtx; -struct Fts5FlushCtx { +/* +** Close the read-only blob handle, if it is open. +*/ +void sqlite3Fts5IndexCloseReader(Fts5Index *p){ + fts5IndexCloseReader(p); + fts5IndexReturn(p); +} + +typedef struct Fts5FlushCtx Fts5FlushCtx; +struct Fts5FlushCtx { Fts5Index *pIdx; Fts5SegWriter writer; }; @@ -4302,6 +5091,491 @@ static int fts5PoslistPrefix(const u8 *aBuf, int nMax){ return ret; } +/* +** Execute the SQL statement: +** +** DELETE FROM %_idx WHERE (segid, (pgno/2)) = ($iSegid, $iPgno); +** +** This is used when a secure-delete operation removes the last term +** from a segment leaf page. In that case the %_idx entry is removed +** too. This is done to ensure that if all instances of a token are +** removed from an fts5 database in secure-delete mode, no trace of +** the token itself remains in the database. +*/ +static void fts5SecureDeleteIdxEntry( + Fts5Index *p, /* FTS5 backend object */ + int iSegid, /* Id of segment to delete entry for */ + int iPgno /* Page number within segment */ +){ + if( iPgno!=1 ){ + assert( p->pConfig->iVersion==FTS5_CURRENT_VERSION_SECUREDELETE ); + if( p->pDeleteFromIdx==0 ){ + fts5IndexPrepareStmt(p, &p->pDeleteFromIdx, sqlite3_mprintf( + "DELETE FROM '%q'.'%q_idx' WHERE (segid, (pgno/2)) = (?1, ?2)", + p->pConfig->zDb, p->pConfig->zName + )); + } + if( p->rc==SQLITE_OK ){ + sqlite3_bind_int(p->pDeleteFromIdx, 1, iSegid); + sqlite3_bind_int(p->pDeleteFromIdx, 2, iPgno); + sqlite3_step(p->pDeleteFromIdx); + p->rc = sqlite3_reset(p->pDeleteFromIdx); + } + } +} + +/* +** This is called when a secure-delete operation removes a position-list +** that overflows onto segment page iPgno of segment pSeg. This function +** rewrites node iPgno, and possibly one or more of its right-hand peers, +** to remove this portion of the position list. +** +** Output variable (*pbLastInDoclist) is set to true if the position-list +** removed is followed by a new term or the end-of-segment, or false if +** it is followed by another rowid/position list. +*/ +static void fts5SecureDeleteOverflow( + Fts5Index *p, + Fts5StructureSegment *pSeg, + int iPgno, + int *pbLastInDoclist +){ + const int bDetailNone = (p->pConfig->eDetail==FTS5_DETAIL_NONE); + int pgno; + Fts5Data *pLeaf = 0; + assert( iPgno!=1 ); + + *pbLastInDoclist = 1; + for(pgno=iPgno; p->rc==SQLITE_OK && pgno<=pSeg->pgnoLast; pgno++){ + i64 iRowid = FTS5_SEGMENT_ROWID(pSeg->iSegid, pgno); + int iNext = 0; + u8 *aPg = 0; + + pLeaf = fts5DataRead(p, iRowid); + if( pLeaf==0 ) break; + aPg = pLeaf->p; + + iNext = fts5GetU16(&aPg[0]); + if( iNext!=0 ){ + *pbLastInDoclist = 0; + } + if( iNext==0 && pLeaf->szLeaf!=pLeaf->nn ){ + fts5GetVarint32(&aPg[pLeaf->szLeaf], iNext); + } + + if( iNext==0 ){ + /* The page contains no terms or rowids. Replace it with an empty + ** page and move on to the right-hand peer. */ + const u8 aEmpty[] = {0x00, 0x00, 0x00, 0x04}; + assert_nc( bDetailNone==0 || pLeaf->nn==4 ); + if( bDetailNone==0 ) fts5DataWrite(p, iRowid, aEmpty, sizeof(aEmpty)); + fts5DataRelease(pLeaf); + pLeaf = 0; + }else if( bDetailNone ){ + break; + }else if( iNext>=pLeaf->szLeaf || pLeaf->nnszLeaf || iNext<4 ){ + FTS5_CORRUPT_ROWID(p, iRowid); + break; + }else{ + int nShift = iNext - 4; + int nPg; + + int nIdx = 0; + u8 *aIdx = 0; + + /* Unless the current page footer is 0 bytes in size (in which case + ** the new page footer will be as well), allocate and populate a + ** buffer containing the new page footer. Set stack variables aIdx + ** and nIdx accordingly. */ + if( pLeaf->nn>pLeaf->szLeaf ){ + int iFirst = 0; + int i1 = pLeaf->szLeaf; + int i2 = 0; + + i1 += fts5GetVarint32(&aPg[i1], iFirst); + if( iFirstrc, (pLeaf->nn-pLeaf->szLeaf)+2); + if( aIdx==0 ) break; + i2 = sqlite3Fts5PutVarint(aIdx, iFirst-nShift); + if( i1nn ){ + memcpy(&aIdx[i2], &aPg[i1], pLeaf->nn-i1); + i2 += (pLeaf->nn-i1); + } + nIdx = i2; + } + + /* Modify the contents of buffer aPg[]. Set nPg to the new size + ** in bytes. The new page is always smaller than the old. */ + nPg = pLeaf->szLeaf - nShift; + memmove(&aPg[4], &aPg[4+nShift], nPg-4); + fts5PutU16(&aPg[2], nPg); + if( fts5GetU16(&aPg[0]) ) fts5PutU16(&aPg[0], 4); + if( nIdx>0 ){ + memcpy(&aPg[nPg], aIdx, nIdx); + nPg += nIdx; + } + sqlite3_free(aIdx); + + /* Write the new page to disk and exit the loop */ + assert( nPg>4 || fts5GetU16(aPg)==0 ); + fts5DataWrite(p, iRowid, aPg, nPg); + break; + } + } + fts5DataRelease(pLeaf); +} + +/* +** Completely remove the entry that pSeg currently points to from +** the database. +*/ +static void fts5DoSecureDelete( + Fts5Index *p, + Fts5SegIter *pSeg +){ + const int bDetailNone = (p->pConfig->eDetail==FTS5_DETAIL_NONE); + int iSegid = pSeg->pSeg->iSegid; + u8 *aPg = pSeg->pLeaf->p; + int nPg = pSeg->pLeaf->nn; + int iPgIdx = pSeg->pLeaf->szLeaf; + + u64 iDelta = 0; + int iNextOff = 0; + int iOff = 0; + int nIdx = 0; + u8 *aIdx = 0; + int bLastInDoclist = 0; + int iIdx = 0; + int iStart = 0; + int iDelKeyOff = 0; /* Offset of deleted key, if any */ + + nIdx = nPg-iPgIdx; + aIdx = sqlite3Fts5MallocZero(&p->rc, ((i64)nIdx)+16); + if( p->rc ) return; + memcpy(aIdx, &aPg[iPgIdx], nIdx); + + /* At this point segment iterator pSeg points to the entry + ** this function should remove from the b-tree segment. + ** + ** In detail=full or detail=column mode, pSeg->iLeafOffset is the + ** offset of the first byte in the position-list for the entry to + ** remove. Immediately before this comes two varints that will also + ** need to be removed: + ** + ** + the rowid or delta rowid value for the entry, and + ** + the size of the position list in bytes. + ** + ** Or, in detail=none mode, there is a single varint prior to + ** pSeg->iLeafOffset - the rowid or delta rowid value. + ** + ** This block sets the following variables: + ** + ** iStart: + ** The offset of the first byte of the rowid or delta-rowid + ** value for the doclist entry being removed. + ** + ** iDelta: + ** The value of the rowid or delta-rowid value for the doclist + ** entry being removed. + ** + ** iNextOff: + ** The offset of the next entry following the position list + ** for the one being removed. If the position list for this + ** entry overflows onto the next leaf page, this value will be + ** greater than pLeaf->szLeaf. + */ + { + int iSOP; /* Start-Of-Position-list */ + if( pSeg->iLeafPgno==pSeg->iTermLeafPgno ){ + iStart = pSeg->iTermLeafOffset; + }else{ + iStart = fts5GetU16(&aPg[0]); + } + + iSOP = iStart + fts5GetVarint(&aPg[iStart], &iDelta); + assert_nc( iSOP<=pSeg->iLeafOffset ); + + if( bDetailNone ){ + while( iSOPiLeafOffset ){ + if( aPg[iSOP]==0x00 ) iSOP++; + if( aPg[iSOP]==0x00 ) iSOP++; + iStart = iSOP; + iSOP = iStart + fts5GetVarint(&aPg[iStart], &iDelta); + } + + iNextOff = iSOP; + if( iNextOffiEndofDoclist && aPg[iNextOff]==0x00 ) iNextOff++; + if( iNextOffiEndofDoclist && aPg[iNextOff]==0x00 ) iNextOff++; + + }else{ + int nPos = 0; + iSOP += fts5GetVarint32(&aPg[iSOP], nPos); + while( iSOPiLeafOffset ){ + iStart = iSOP + (nPos/2); + iSOP = iStart + fts5GetVarint(&aPg[iStart], &iDelta); + iSOP += fts5GetVarint32(&aPg[iSOP], nPos); + } + assert_nc( iSOP==pSeg->iLeafOffset ); + iNextOff = pSeg->iLeafOffset + pSeg->nPos; + } + } + + iOff = iStart; + + /* If the position-list for the entry being removed flows over past + ** the end of this page, delete the portion of the position-list on the + ** next page and beyond. + ** + ** Set variable bLastInDoclist to true if this entry happens + ** to be the last rowid in the doclist for its term. */ + if( iNextOff>=iPgIdx ){ + int pgno = pSeg->iLeafPgno+1; + fts5SecureDeleteOverflow(p, pSeg->pSeg, pgno, &bLastInDoclist); + iNextOff = iPgIdx; + } + + if( pSeg->bDel==0 ){ + if( iNextOff!=iPgIdx ){ + /* Loop through the page-footer. If iNextOff (offset of the + ** entry following the one we are removing) is equal to the + ** offset of a key on this page, then the entry is the last + ** in its doclist. */ + int iKeyOff = 0; + for(iIdx=0; iIdxbDel ){ + iOff += sqlite3Fts5PutVarint(&aPg[iOff], iDelta); + aPg[iOff++] = 0x01; + }else if( bLastInDoclist==0 ){ + if( iNextOff!=iPgIdx ){ + u64 iNextDelta = 0; + iNextOff += fts5GetVarint(&aPg[iNextOff], &iNextDelta); + iOff += sqlite3Fts5PutVarint(&aPg[iOff], iDelta + iNextDelta); + } + }else if( + pSeg->iLeafPgno==pSeg->iTermLeafPgno + && iStart==pSeg->iTermLeafOffset + ){ + /* The entry being removed was the only position list in its + ** doclist. Therefore the term needs to be removed as well. */ + int iKey = 0; + int iKeyOff = 0; + + /* Set iKeyOff to the offset of the term that will be removed - the + ** last offset in the footer that is not greater than iStart. */ + for(iIdx=0; iIdx(u32)iStart ) break; + iKeyOff += iVal; + } + assert_nc( iKey>=1 ); + + /* Set iDelKeyOff to the value of the footer entry to remove from + ** the page. */ + iDelKeyOff = iOff = iKeyOff; + + if( iNextOff!=iPgIdx ){ + /* This is the only position-list associated with the term, and there + ** is another term following it on this page. So the subsequent term + ** needs to be moved to replace the term associated with the entry + ** being removed. */ + int nPrefix = 0; + int nSuffix = 0; + int nPrefix2 = 0; + int nSuffix2 = 0; + + iDelKeyOff = iNextOff; + iNextOff += fts5GetVarint32(&aPg[iNextOff], nPrefix2); + iNextOff += fts5GetVarint32(&aPg[iNextOff], nSuffix2); + + if( iKey!=1 ){ + iKeyOff += fts5GetVarint32(&aPg[iKeyOff], nPrefix); + } + iKeyOff += fts5GetVarint32(&aPg[iKeyOff], nSuffix); + + nPrefix = MIN(nPrefix, nPrefix2); + nSuffix = (nPrefix2 + nSuffix2) - nPrefix; + + if( (iKeyOff+nSuffix)>iPgIdx || (iNextOff+nSuffix2)>iPgIdx ){ + FTS5_CORRUPT_IDX(p); + }else{ + if( iKey!=1 ){ + iOff += sqlite3Fts5PutVarint(&aPg[iOff], nPrefix); + } + iOff += sqlite3Fts5PutVarint(&aPg[iOff], nSuffix); + if( nPrefix2>pSeg->term.n ){ + FTS5_CORRUPT_IDX(p); + }else if( nPrefix2>nPrefix ){ + memcpy(&aPg[iOff], &pSeg->term.p[nPrefix], nPrefix2-nPrefix); + iOff += (nPrefix2-nPrefix); + } + memmove(&aPg[iOff], &aPg[iNextOff], nSuffix2); + iOff += nSuffix2; + iNextOff += nSuffix2; + } + } + }else if( iStart==4 ){ + int iPgno; + + assert_nc( pSeg->iLeafPgno>pSeg->iTermLeafPgno ); + /* The entry being removed may be the only position list in + ** its doclist. */ + for(iPgno=pSeg->iLeafPgno-1; iPgno>pSeg->iTermLeafPgno; iPgno-- ){ + Fts5Data *pPg = fts5DataRead(p, FTS5_SEGMENT_ROWID(iSegid, iPgno)); + int bEmpty = (pPg && pPg->nn==4); + fts5DataRelease(pPg); + if( bEmpty==0 ) break; + } + + if( iPgno==pSeg->iTermLeafPgno ){ + i64 iId = FTS5_SEGMENT_ROWID(iSegid, pSeg->iTermLeafPgno); + Fts5Data *pTerm = fts5DataRead(p, iId); + if( pTerm && pTerm->szLeaf==pSeg->iTermLeafOffset ){ + u8 *aTermIdx = &pTerm->p[pTerm->szLeaf]; + int nTermIdx = pTerm->nn - pTerm->szLeaf; + int iTermIdx = 0; + int iTermOff = 0; + + while( 1 ){ + u32 iVal = 0; + int nByte = fts5GetVarint32(&aTermIdx[iTermIdx], iVal); + iTermOff += iVal; + if( (iTermIdx+nByte)>=nTermIdx ) break; + iTermIdx += nByte; + } + nTermIdx = iTermIdx; + + memmove(&pTerm->p[iTermOff], &pTerm->p[pTerm->szLeaf], nTermIdx); + fts5PutU16(&pTerm->p[2], iTermOff); + + fts5DataWrite(p, iId, pTerm->p, iTermOff+nTermIdx); + if( nTermIdx==0 ){ + fts5SecureDeleteIdxEntry(p, iSegid, pSeg->iTermLeafPgno); + } + } + fts5DataRelease(pTerm); + } + } + + /* Assuming no error has occurred, this block does final edits to the + ** leaf page before writing it back to disk. Input variables are: + ** + ** nPg: Total initial size of leaf page. + ** iPgIdx: Initial offset of page footer. + ** + ** iOff: Offset to move data to + ** iNextOff: Offset to move data from + */ + if( p->rc==SQLITE_OK ){ + const int nMove = nPg - iNextOff; /* Number of bytes to move */ + int nShift = iNextOff - iOff; /* Distance to move them */ + + int iPrevKeyOut = 0; + int iKeyIn = 0; + + memmove(&aPg[iOff], &aPg[iNextOff], nMove); + iPgIdx -= nShift; + nPg = iPgIdx; + fts5PutU16(&aPg[2], iPgIdx); + + for(iIdx=0; iIdxiOff ? nShift : 0)); + nPg += sqlite3Fts5PutVarint(&aPg[nPg], iKeyOut - iPrevKeyOut); + iPrevKeyOut = iKeyOut; + } + } + + if( iPgIdx==nPg && nIdx>0 && pSeg->iLeafPgno!=1 ){ + fts5SecureDeleteIdxEntry(p, iSegid, pSeg->iLeafPgno); + } + + assert_nc( nPg>4 || fts5GetU16(aPg)==0 ); + fts5DataWrite(p, FTS5_SEGMENT_ROWID(iSegid,pSeg->iLeafPgno), aPg, nPg); + } + sqlite3_free(aIdx); +} + +/* +** This is called as part of flushing a delete to disk in 'secure-delete' +** mode. It edits the segments within the database described by argument +** pStruct to remove the entries for term zTerm, rowid iRowid. +** +** Return SQLITE_OK if successful, or an SQLite error code if an error +** has occurred. Any error code is also stored in the Fts5Index handle. +*/ +static int fts5FlushSecureDelete( + Fts5Index *p, + Fts5Structure *pStruct, + const char *zTerm, + int nTerm, + i64 iRowid +){ + const int f = FTS5INDEX_QUERY_SKIPHASH; + Fts5Iter *pIter = 0; /* Used to find term instance */ + + /* If the version number has not been set to SECUREDELETE, do so now. */ + if( p->pConfig->iVersion!=FTS5_CURRENT_VERSION_SECUREDELETE ){ + Fts5Config *pConfig = p->pConfig; + sqlite3_stmt *pStmt = 0; + fts5IndexPrepareStmt(p, &pStmt, sqlite3_mprintf( + "REPLACE INTO %Q.'%q_config' VALUES ('version', %d)", + pConfig->zDb, pConfig->zName, FTS5_CURRENT_VERSION_SECUREDELETE + )); + if( p->rc==SQLITE_OK ){ + int rc; + sqlite3_step(pStmt); + rc = sqlite3_finalize(pStmt); + if( p->rc==SQLITE_OK ) p->rc = rc; + pConfig->iCookie++; + pConfig->iVersion = FTS5_CURRENT_VERSION_SECUREDELETE; + } + } + + fts5MultiIterNew(p, pStruct, f, 0, (const u8*)zTerm, nTerm, -1, 0, &pIter); + if( fts5MultiIterEof(p, pIter)==0 ){ + i64 iThis = fts5MultiIterRowid(pIter); + if( iThisrc==SQLITE_OK + && fts5MultiIterEof(p, pIter)==0 + && iRowid==fts5MultiIterRowid(pIter) + ){ + Fts5SegIter *pSeg = &pIter->aSeg[pIter->aFirst[1].iFirst]; + fts5DoSecureDelete(p, pSeg); + } + } + + fts5MultiIterFree(pIter); + return p->rc; +} + + /* ** Flush the contents of in-memory hash table iHash to a new level-0 ** segment on disk. Also update the corresponding structure record. @@ -4318,140 +5592,199 @@ static void fts5FlushOneHash(Fts5Index *p){ /* Obtain a reference to the index structure and allocate a new segment-id ** for the new level-0 segment. */ pStruct = fts5StructureRead(p); - iSegid = fts5AllocateSegid(p, pStruct); - - if( iSegid ){ - const int pgsz = p->pConfig->pgsz; - int eDetail = p->pConfig->eDetail; - Fts5StructureSegment *pSeg; /* New segment within pStruct */ - Fts5Buffer *pBuf; /* Buffer in which to assemble leaf page */ - Fts5Buffer *pPgidx; /* Buffer in which to assemble pgidx */ - - Fts5SegWriter writer; - fts5WriteInit(p, &writer, iSegid); - - pBuf = &writer.writer.buf; - pPgidx = &writer.writer.pgidx; - - /* fts5WriteInit() should have initialized the buffers to (most likely) - ** the maximum space required. */ - assert( p->rc || pBuf->nSpace>=(pgsz + FTS5_DATA_PADDING) ); - assert( p->rc || pPgidx->nSpace>=(pgsz + FTS5_DATA_PADDING) ); - - /* Begin scanning through hash table entries. This loop runs once for each - ** term/doclist currently stored within the hash table. */ - if( p->rc==SQLITE_OK ){ - p->rc = sqlite3Fts5HashScanInit(pHash, 0, 0); - } - while( p->rc==SQLITE_OK && 0==sqlite3Fts5HashScanEof(pHash) ){ - const char *zTerm; /* Buffer containing term */ - const u8 *pDoclist; /* Pointer to doclist for this term */ - int nDoclist; /* Size of doclist in bytes */ - - /* Write the term for this entry to disk. */ - sqlite3Fts5HashScanEntry(pHash, &zTerm, &pDoclist, &nDoclist); - fts5WriteAppendTerm(p, &writer, (int)strlen(zTerm), (const u8*)zTerm); - - assert( writer.bFirstRowidInPage==0 ); - if( pgsz>=(pBuf->n + pPgidx->n + nDoclist + 1) ){ - /* The entire doclist will fit on the current leaf. */ - fts5BufferSafeAppendBlob(pBuf, pDoclist, nDoclist); - }else{ - i64 iRowid = 0; - i64 iDelta = 0; - int iOff = 0; - - /* The entire doclist will not fit on this leaf. The following - ** loop iterates through the poslists that make up the current - ** doclist. */ - while( p->rc==SQLITE_OK && iOffp[0], (u16)pBuf->n); /* first rowid on page */ - pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], iRowid); - writer.bFirstRowidInPage = 0; - fts5WriteDlidxAppend(p, &writer, iRowid); - }else{ - pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], iDelta); - } - assert( pBuf->n<=pBuf->nSpace ); - - if( eDetail==FTS5_DETAIL_NONE ){ - if( iOffp[pBuf->n++] = 0; - iOff++; - if( iOffp[pBuf->n++] = 0; - iOff++; + fts5StructureInvalidate(p); + + if( sqlite3Fts5HashIsEmpty(pHash)==0 ){ + iSegid = fts5AllocateSegid(p, pStruct); + if( iSegid ){ + const int pgsz = p->pConfig->pgsz; + int eDetail = p->pConfig->eDetail; + int bSecureDelete = p->pConfig->bSecureDelete; + Fts5StructureSegment *pSeg; /* New segment within pStruct */ + Fts5Buffer *pBuf; /* Buffer in which to assemble leaf page */ + Fts5Buffer *pPgidx; /* Buffer in which to assemble pgidx */ + + Fts5SegWriter writer; + fts5WriteInit(p, &writer, iSegid); + + pBuf = &writer.writer.buf; + pPgidx = &writer.writer.pgidx; + + /* fts5WriteInit() should have initialized the buffers to (most likely) + ** the maximum space required. */ + assert( p->rc || pBuf->nSpace>=(pgsz + FTS5_DATA_PADDING) ); + assert( p->rc || pPgidx->nSpace>=(pgsz + FTS5_DATA_PADDING) ); + + /* Begin scanning through hash table entries. This loop runs once for each + ** term/doclist currently stored within the hash table. */ + if( p->rc==SQLITE_OK ){ + p->rc = sqlite3Fts5HashScanInit(pHash, 0, 0); + } + while( p->rc==SQLITE_OK && 0==sqlite3Fts5HashScanEof(pHash) ){ + const char *zTerm; /* Buffer containing term */ + int nTerm; /* Size of zTerm in bytes */ + const u8 *pDoclist; /* Pointer to doclist for this term */ + int nDoclist; /* Size of doclist in bytes */ + + /* Get the term and doclist for this entry. */ + sqlite3Fts5HashScanEntry(pHash, &zTerm, &nTerm, &pDoclist, &nDoclist); + if( bSecureDelete==0 ){ + fts5WriteAppendTerm(p, &writer, nTerm, (const u8*)zTerm); + if( p->rc!=SQLITE_OK ) break; + assert( writer.bFirstRowidInPage==0 ); + } + + if( !bSecureDelete && pgsz>=(pBuf->n + pPgidx->n + nDoclist + 1) ){ + /* The entire doclist will fit on the current leaf. */ + fts5BufferSafeAppendBlob(pBuf, pDoclist, nDoclist); + }else{ + int bTermWritten = !bSecureDelete; + i64 iRowid = 0; + i64 iPrev = 0; + int iOff = 0; + + /* The entire doclist will not fit on this leaf. The following + ** loop iterates through the poslists that make up the current + ** doclist. */ + while( p->rc==SQLITE_OK && iOffrc!=SQLITE_OK || pDoclist[iOff]==0x01 ){ + iOff++; + continue; + } } } - if( (pBuf->n + pPgidx->n)>=pgsz ){ - fts5WriteFlushLeaf(p, &writer); + + if( p->rc==SQLITE_OK && bTermWritten==0 ){ + fts5WriteAppendTerm(p, &writer, nTerm, (const u8*)zTerm); + bTermWritten = 1; + assert( p->rc!=SQLITE_OK || writer.bFirstRowidInPage==0 ); } - }else{ - int bDummy; - int nPos; - int nCopy = fts5GetPoslistSize(&pDoclist[iOff], &nPos, &bDummy); - nCopy += nPos; - if( (pBuf->n + pPgidx->n + nCopy) <= pgsz ){ - /* The entire poslist will fit on the current leaf. So copy - ** it in one go. */ - fts5BufferSafeAppendBlob(pBuf, &pDoclist[iOff], nCopy); + + if( writer.bFirstRowidInPage ){ + fts5PutU16(&pBuf->p[0], (u16)pBuf->n); /* first rowid on page */ + pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], iRowid); + writer.bFirstRowidInPage = 0; + fts5WriteDlidxAppend(p, &writer, iRowid); }else{ - /* The entire poslist will not fit on this leaf. So it needs - ** to be broken into sections. The only qualification being - ** that each varint must be stored contiguously. */ - const u8 *pPoslist = &pDoclist[iOff]; - int iPos = 0; - while( p->rc==SQLITE_OK ){ - int nSpace = pgsz - pBuf->n - pPgidx->n; - int n = 0; - if( (nCopy - iPos)<=nSpace ){ - n = nCopy - iPos; - }else{ - n = fts5PoslistPrefix(&pPoslist[iPos], nSpace); + u64 iRowidDelta = (u64)iRowid - (u64)iPrev; + pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], iRowidDelta); + } + if( p->rc!=SQLITE_OK ) break; + assert( pBuf->n<=pBuf->nSpace ); + iPrev = iRowid; + + if( eDetail==FTS5_DETAIL_NONE ){ + if( iOffp[pBuf->n++] = 0; + iOff++; + if( iOffp[pBuf->n++] = 0; + iOff++; } - assert( n>0 ); - fts5BufferSafeAppendBlob(pBuf, &pPoslist[iPos], n); - iPos += n; - if( (pBuf->n + pPgidx->n)>=pgsz ){ - fts5WriteFlushLeaf(p, &writer); + } + if( (pBuf->n + pPgidx->n)>=pgsz ){ + fts5WriteFlushLeaf(p, &writer); + } + }else{ + int bDel = 0; + int nPos = 0; + int nCopy = fts5GetPoslistSize(&pDoclist[iOff], &nPos, &bDel); + if( bDel && bSecureDelete ){ + fts5BufferAppendVarint(&p->rc, pBuf, nPos*2); + iOff += nCopy; + nCopy = nPos; + }else{ + nCopy += nPos; + } + if( (pBuf->n + pPgidx->n + nCopy) <= pgsz ){ + /* The entire poslist will fit on the current leaf. So copy + ** it in one go. */ + fts5BufferSafeAppendBlob(pBuf, &pDoclist[iOff], nCopy); + }else{ + /* The entire poslist will not fit on this leaf. So it needs + ** to be broken into sections. The only qualification being + ** that each varint must be stored contiguously. */ + const u8 *pPoslist = &pDoclist[iOff]; + int iPos = 0; + while( p->rc==SQLITE_OK ){ + int nSpace = pgsz - pBuf->n - pPgidx->n; + int n = 0; + if( (nCopy - iPos)<=nSpace ){ + n = nCopy - iPos; + }else{ + n = fts5PoslistPrefix(&pPoslist[iPos], nSpace); + } + assert( n>0 ); + fts5BufferSafeAppendBlob(pBuf, &pPoslist[iPos], n); + iPos += n; + if( (pBuf->n + pPgidx->n)>=pgsz ){ + fts5WriteFlushLeaf(p, &writer); + } + if( iPos>=nCopy ) break; } - if( iPos>=nCopy ) break; } + iOff += nCopy; } - iOff += nCopy; } } + + /* TODO2: Doclist terminator written here. */ + /* pBuf->p[pBuf->n++] = '\0'; */ + assert( pBuf->n<=pBuf->nSpace ); + if( p->rc==SQLITE_OK ) sqlite3Fts5HashScanNext(pHash); + } + fts5WriteFinish(p, &writer, &pgnoLast); + + assert( p->rc!=SQLITE_OK || bSecureDelete || pgnoLast>0 ); + if( pgnoLast>0 ){ + /* Update the Fts5Structure. It is written back to the database by the + ** fts5StructureRelease() call below. */ + if( pStruct->nLevel==0 ){ + fts5StructureAddLevel(&p->rc, &pStruct); + } + fts5StructureExtendLevel(&p->rc, pStruct, 0, 1, 0); + if( p->rc==SQLITE_OK ){ + pSeg = &pStruct->aLevel[0].aSeg[ pStruct->aLevel[0].nSeg++ ]; + pSeg->iSegid = iSegid; + pSeg->pgnoFirst = 1; + pSeg->pgnoLast = pgnoLast; + if( pStruct->nOriginCntr>0 ){ + pSeg->iOrigin1 = pStruct->nOriginCntr; + pSeg->iOrigin2 = pStruct->nOriginCntr; + pSeg->nEntry = p->nPendingRow; + pStruct->nOriginCntr++; + } + pStruct->nSegment++; + } + fts5StructurePromote(p, 0, pStruct); } - - /* TODO2: Doclist terminator written here. */ - /* pBuf->p[pBuf->n++] = '\0'; */ - assert( pBuf->n<=pBuf->nSpace ); - sqlite3Fts5HashScanNext(pHash); - } - sqlite3Fts5HashClear(pHash); - fts5WriteFinish(p, &writer, &pgnoLast); - - /* Update the Fts5Structure. It is written back to the database by the - ** fts5StructureRelease() call below. */ - if( pStruct->nLevel==0 ){ - fts5StructureAddLevel(&p->rc, &pStruct); - } - fts5StructureExtendLevel(&p->rc, pStruct, 0, 1, 0); - if( p->rc==SQLITE_OK ){ - pSeg = &pStruct->aLevel[0].aSeg[ pStruct->aLevel[0].nSeg++ ]; - pSeg->iSegid = iSegid; - pSeg->pgnoFirst = 1; - pSeg->pgnoLast = pgnoLast; - pStruct->nSegment++; } - fts5StructurePromote(p, 0, pStruct); } - fts5IndexAutomerge(p, &pStruct, pgnoLast); + fts5IndexAutomerge(p, &pStruct, pgnoLast + p->nContentlessDelete); fts5IndexCrisismerge(p, &pStruct); fts5StructureWrite(p, pStruct); fts5StructureRelease(pStruct); @@ -4462,44 +5795,78 @@ static void fts5FlushOneHash(Fts5Index *p){ */ static void fts5IndexFlush(Fts5Index *p){ /* Unless it is empty, flush the hash table to disk */ - if( p->nPendingData ){ + if( p->flushRc ){ + p->rc = p->flushRc; + return; + } + if( p->nPendingData || p->nContentlessDelete ){ assert( p->pHash ); - p->nPendingData = 0; fts5FlushOneHash(p); + if( p->rc==SQLITE_OK ){ + sqlite3Fts5HashClear(p->pHash); + p->nPendingData = 0; + p->nPendingRow = 0; + p->nContentlessDelete = 0; + }else if( p->nPendingData || p->nContentlessDelete ){ + p->flushRc = p->rc; + } } } - -int sqlite3Fts5IndexOptimize(Fts5Index *p){ - Fts5Structure *pStruct; +static Fts5Structure *fts5IndexOptimizeStruct( + Fts5Index *p, + Fts5Structure *pStruct +){ Fts5Structure *pNew = 0; - int nSeg = 0; - - assert( p->rc==SQLITE_OK ); - fts5IndexFlush(p); - pStruct = fts5StructureRead(p); + sqlite3_int64 nByte = SZ_FTS5STRUCTURE(1); + int nSeg = pStruct->nSegment; + int i; - if( pStruct ){ - assert( pStruct->nSegment==fts5StructureCountSegments(pStruct) ); - nSeg = pStruct->nSegment; - if( nSeg>1 ){ - int nByte = sizeof(Fts5Structure); - nByte += (pStruct->nLevel+1) * sizeof(Fts5StructureLevel); - pNew = (Fts5Structure*)sqlite3Fts5MallocZero(&p->rc, nByte); + /* Figure out if this structure requires optimization. A structure does + ** not require optimization if either: + ** + ** 1. it consists of fewer than two segments, or + ** 2. all segments are on the same level, or + ** 3. all segments except one are currently inputs to a merge operation. + ** + ** In the first case, if there are no tombstone hash pages, return NULL. In + ** the second, increment the ref-count on *pStruct and return a copy of the + ** pointer to it. + */ + if( nSeg==0 ) return 0; + for(i=0; inLevel; i++){ + int nThis = pStruct->aLevel[i].nSeg; + int nMerge = pStruct->aLevel[i].nMerge; + if( nThis>0 && (nThis==nSeg || (nThis==nSeg-1 && nMerge==nThis)) ){ + if( nSeg==1 && nThis==1 && pStruct->aLevel[i].aSeg[0].nPgTombstone==0 ){ + return 0; + } + fts5StructureRef(pStruct); + return pStruct; } + assert( pStruct->aLevel[i].nMerge<=nThis ); } + + nByte += (((i64)pStruct->nLevel)+1) * sizeof(Fts5StructureLevel); + assert( nByte==(i64)SZ_FTS5STRUCTURE(pStruct->nLevel+2) ); + pNew = (Fts5Structure*)sqlite3Fts5MallocZero(&p->rc, nByte); + if( pNew ){ Fts5StructureLevel *pLvl; - int nByte = nSeg * sizeof(Fts5StructureSegment); - pNew->nLevel = pStruct->nLevel+1; + nByte = nSeg * sizeof(Fts5StructureSegment); + pNew->nLevel = MIN(pStruct->nLevel+1, FTS5_MAX_LEVEL); pNew->nRef = 1; pNew->nWriteCounter = pStruct->nWriteCounter; - pLvl = &pNew->aLevel[pStruct->nLevel]; + pNew->nOriginCntr = pStruct->nOriginCntr; + pLvl = &pNew->aLevel[pNew->nLevel-1]; pLvl->aSeg = (Fts5StructureSegment*)sqlite3Fts5MallocZero(&p->rc, nByte); if( pLvl->aSeg ){ int iLvl, iSeg; int iSegOut = 0; - for(iLvl=0; iLvlnLevel; iLvl++){ + /* Iterate through all segments, from oldest to newest. Add them to + ** the new Fts5Level object so that pLvl->aSeg[0] is the oldest + ** segment in the data structure. */ + for(iLvl=pStruct->nLevel-1; iLvl>=0; iLvl--){ for(iSeg=0; iSegaLevel[iLvl].nSeg; iSeg++){ pLvl->aSeg[iSegOut] = pStruct->aLevel[iLvl].aSeg[iSeg]; iSegOut++; @@ -4512,8 +5879,29 @@ int sqlite3Fts5IndexOptimize(Fts5Index *p){ } } + return pNew; +} + +int sqlite3Fts5IndexOptimize(Fts5Index *p){ + Fts5Structure *pStruct; + Fts5Structure *pNew = 0; + + assert( p->rc==SQLITE_OK ); + fts5IndexFlush(p); + assert( p->rc!=SQLITE_OK || p->nContentlessDelete==0 ); + pStruct = fts5StructureRead(p); + assert( p->rc!=SQLITE_OK || pStruct!=0 ); + fts5StructureInvalidate(p); + + if( pStruct ){ + pNew = fts5IndexOptimizeStruct(p, pStruct); + } + fts5StructureRelease(pStruct); + + assert( pNew==0 || pNew->nSegment>0 ); if( pNew ){ - int iLvl = pNew->nLevel-1; + int iLvl; + for(iLvl=0; pNew->aLevel[iLvl].nSeg==0; iLvl++){} while( p->rc==SQLITE_OK && pNew->aLevel[iLvl].nSeg>0 ){ int nRem = FTS5_OPT_WORK_UNIT; fts5IndexMergeLevel(p, &pNew, iLvl, &nRem); @@ -4523,26 +5911,41 @@ int sqlite3Fts5IndexOptimize(Fts5Index *p){ fts5StructureRelease(pNew); } - fts5StructureRelease(pStruct); return fts5IndexReturn(p); } +/* +** This is called to implement the special "VALUES('merge', $nMerge)" +** INSERT command. +*/ int sqlite3Fts5IndexMerge(Fts5Index *p, int nMerge){ - Fts5Structure *pStruct; + Fts5Structure *pStruct = 0; + fts5IndexFlush(p); pStruct = fts5StructureRead(p); - if( pStruct && pStruct->nLevel ){ - fts5IndexMerge(p, &pStruct, nMerge); - fts5StructureWrite(p, pStruct); + if( pStruct ){ + int nMin = p->pConfig->nUsermerge; + fts5StructureInvalidate(p); + if( nMerge<0 ){ + Fts5Structure *pNew = fts5IndexOptimizeStruct(p, pStruct); + fts5StructureRelease(pStruct); + pStruct = pNew; + nMin = 1; + nMerge = (nMerge==SMALLEST_INT32 ? LARGEST_INT32 : (nMerge*-1)); + } + if( pStruct && pStruct->nLevel ){ + if( fts5IndexMerge(p, &pStruct, nMerge, nMin) ){ + fts5StructureWrite(p, pStruct); + } + } + fts5StructureRelease(pStruct); } - fts5StructureRelease(pStruct); - return fts5IndexReturn(p); } static void fts5AppendRowid( Fts5Index *p, - i64 iDelta, + u64 iDelta, Fts5Iter *pUnused, Fts5Buffer *pBuf ){ @@ -4552,16 +5955,18 @@ static void fts5AppendRowid( static void fts5AppendPoslist( Fts5Index *p, - i64 iDelta, + u64 iDelta, Fts5Iter *pMulti, Fts5Buffer *pBuf ){ int nData = pMulti->base.nData; + int nByte = nData + 9 + 9 + FTS5_DATA_ZERO_PADDING; assert( nData>0 ); - if( p->rc==SQLITE_OK && 0==fts5BufferGrow(&p->rc, pBuf, nData+9+9) ){ + if( p->rc==SQLITE_OK && 0==fts5BufferGrow(&p->rc, pBuf, nByte) ){ fts5BufferSafeAppendVarint(pBuf, iDelta); fts5BufferSafeAppendVarint(pBuf, nData*2); fts5BufferSafeAppendBlob(pBuf, pMulti->base.pData, nData); + memset(&pBuf->p[pBuf->n], 0, FTS5_DATA_ZERO_PADDING); } } @@ -4569,7 +5974,7 @@ static void fts5AppendPoslist( static void fts5DoclistIterNext(Fts5DoclistIter *pIter){ u8 *p = pIter->aPoslist + pIter->nSize + pIter->nPoslist; - assert( pIter->aPoslist ); + assert( pIter->aPoslist || (p==0 && pIter->aPoslist==0) ); if( p>=pIter->aEof ){ pIter->aPoslist = 0; }else{ @@ -4589,6 +5994,9 @@ static void fts5DoclistIterNext(Fts5DoclistIter *pIter){ } pIter->aPoslist = p; + if( &pIter->aPoslist[pIter->nPoslist]>pIter->aEof ){ + pIter->aPoslist = 0; + } } } @@ -4597,9 +6005,11 @@ static void fts5DoclistIterInit( Fts5DoclistIter *pIter ){ memset(pIter, 0, sizeof(*pIter)); - pIter->aPoslist = pBuf->p; - pIter->aEof = &pBuf->p[pBuf->n]; - fts5DoclistIterNext(pIter); + if( pBuf->n>0 ){ + pIter->aPoslist = pBuf->p; + pIter->aEof = &pBuf->p[pBuf->n]; + fts5DoclistIterNext(pIter); + } } #if 0 @@ -4620,10 +6030,10 @@ static void fts5MergeAppendDocid( } #endif -#define fts5MergeAppendDocid(pBuf, iLastRowid, iRowid) { \ - assert( (pBuf)->n!=0 || (iLastRowid)==0 ); \ - fts5BufferSafeAppendVarint((pBuf), (iRowid) - (iLastRowid)); \ - (iLastRowid) = (iRowid); \ +#define fts5MergeAppendDocid(pBuf, iLastRowid, iRowid) { \ + assert( (pBuf)->n!=0 || (iLastRowid)==0 ); \ + fts5BufferSafeAppendVarint((pBuf), (u64)(iRowid) - (u64)(iLastRowid)); \ + (iLastRowid) = (iRowid); \ } /* @@ -4653,16 +6063,20 @@ static void fts5NextRowid(Fts5Buffer *pBuf, int *piOff, i64 *piRowid){ static void fts5MergeRowidLists( Fts5Index *p, /* FTS5 backend object */ Fts5Buffer *p1, /* First list to merge */ - Fts5Buffer *p2 /* Second list to merge */ + int nBuf, /* Number of entries in apBuf[] */ + Fts5Buffer *aBuf /* Array of other lists to merge into p1 */ ){ int i1 = 0; int i2 = 0; i64 iRowid1 = 0; i64 iRowid2 = 0; i64 iOut = 0; - + Fts5Buffer *p2 = &aBuf[0]; Fts5Buffer out; + + (void)nBuf; memset(&out, 0, sizeof(out)); + assert( nBuf==1 ); sqlite3Fts5BufferSize(&p->rc, &out, p1->n + p2->n); if( p->rc ) return; @@ -4689,220 +6103,665 @@ static void fts5MergeRowidLists( fts5BufferFree(&out); } -/* -** Buffers p1 and p2 contain doclists. This function merges the content -** of the two doclists together and sets buffer p1 to the result before -** returning. -** -** If an error occurs, an error code is left in p->rc. If an error has -** already occurred, this function is a no-op. -*/ -static void fts5MergePrefixLists( - Fts5Index *p, /* FTS5 backend object */ - Fts5Buffer *p1, /* First list to merge */ - Fts5Buffer *p2 /* Second list to merge */ +typedef struct PrefixMerger PrefixMerger; +struct PrefixMerger { + Fts5DoclistIter iter; /* Doclist iterator */ + i64 iPos; /* For iterating through a position list */ + int iOff; + u8 *aPos; + PrefixMerger *pNext; /* Next in docid/poslist order */ +}; + +static void fts5PrefixMergerInsertByRowid( + PrefixMerger **ppHead, + PrefixMerger *p ){ - if( p2->n ){ - i64 iLastRowid = 0; - Fts5DoclistIter i1; - Fts5DoclistIter i2; - Fts5Buffer out = {0, 0, 0}; - Fts5Buffer tmp = {0, 0, 0}; + if( p->iter.aPoslist ){ + PrefixMerger **pp = ppHead; + while( *pp && p->iter.iRowid>(*pp)->iter.iRowid ){ + pp = &(*pp)->pNext; + } + p->pNext = *pp; + *pp = p; + } +} - if( sqlite3Fts5BufferSize(&p->rc, &out, p1->n + p2->n) ) return; - fts5DoclistIterInit(p1, &i1); - fts5DoclistIterInit(p2, &i2); +static void fts5PrefixMergerInsertByPosition( + PrefixMerger **ppHead, + PrefixMerger *p +){ + if( p->iPos>=0 ){ + PrefixMerger **pp = ppHead; + while( *pp && p->iPos>(*pp)->iPos ){ + pp = &(*pp)->pNext; + } + p->pNext = *pp; + *pp = p; + } +} - while( 1 ){ - if( i1.iRowidrc, &tmp, i1.nPoslist + i2.nPoslist); - if( p->rc ) break; - sqlite3Fts5PoslistNext64(a1, i1.nPoslist, &iOff1, &iPos1); - sqlite3Fts5PoslistNext64(a2, i2.nPoslist, &iOff2, &iPos2); - assert( iPos1>=0 && iPos2>=0 ); +/* +** Array aBuf[] contains nBuf doclists. These are all merged in with the +** doclist in buffer p1. +*/ +static void fts5MergePrefixLists( + Fts5Index *p, /* FTS5 backend object */ + Fts5Buffer *p1, /* First list to merge */ + int nBuf, /* Number of buffers in array aBuf[] */ + Fts5Buffer *aBuf /* Other lists to merge in */ +){ +#define fts5PrefixMergerNextPosition(p) \ + sqlite3Fts5PoslistNext64((p)->aPos,(p)->iter.nPoslist,&(p)->iOff,&(p)->iPos) +#define FTS5_MERGE_NLIST 16 + PrefixMerger aMerger[FTS5_MERGE_NLIST]; + PrefixMerger *pHead = 0; + int i; + int nOut = 0; + Fts5Buffer out = {0, 0, 0}; + Fts5Buffer tmp = {0, 0, 0}; + i64 iLastRowid = 0; + + /* Initialize a doclist-iterator for each input buffer. Arrange them in + ** a linked-list starting at pHead in ascending order of rowid. Avoid + ** linking any iterators already at EOF into the linked list at all. */ + assert( nBuf+1<=(int)(sizeof(aMerger)/sizeof(aMerger[0])) ); + memset(aMerger, 0, sizeof(PrefixMerger)*(nBuf+1)); + pHead = &aMerger[nBuf]; + fts5DoclistIterInit(p1, &pHead->iter); + for(i=0; in + 9 + 10*nBuf; + + /* The maximum size of the output is equal to the sum of the + ** input sizes + 1 varint (9 bytes). The extra varint is because if the + ** first rowid in one input is a large negative number, and the first in + ** the other a non-negative number, the delta for the non-negative + ** number will be larger on disk than the literal integer value + ** was. + ** + ** Or, if the input position-lists are corrupt, then the output might + ** include up to (nBuf+1) extra 10-byte positions created by interpreting -1 + ** (the value PoslistNext64() uses for EOF) as a position and appending + ** it to the output. This can happen at most once for each input + ** position-list, hence (nBuf+1) 10 byte paddings. */ + if( sqlite3Fts5BufferSize(&p->rc, &out, nOut) ) return; + + while( pHead ){ + fts5MergeAppendDocid(&out, iLastRowid, pHead->iter.iRowid); + + if( pHead->pNext && iLastRowid==pHead->pNext->iter.iRowid ){ + /* Merge data from two or more poslists */ + i64 iPrev = 0; + int nTmp = FTS5_DATA_ZERO_PADDING; + int nMerge = 0; + PrefixMerger *pSave = pHead; + PrefixMerger *pThis = 0; + int nTail = 0; + + pHead = 0; + while( pSave && pSave->iter.iRowid==iLastRowid ){ + PrefixMerger *pNext = pSave->pNext; + pSave->iOff = 0; + pSave->iPos = 0; + pSave->aPos = &pSave->iter.aPoslist[pSave->iter.nSize]; + fts5PrefixMergerNextPosition(pSave); + nTmp += pSave->iter.nPoslist + 10; + nMerge++; + fts5PrefixMergerInsertByPosition(&pHead, pSave); + pSave = pNext; + } - if( iPos1pNext==0 ){ + FTS5_CORRUPT_IDX(p); + break; + } - if( iPos1>=0 && iPos2>=0 ){ - while( 1 ){ - if( iPos1rc, &tmp, nTmp+nMerge*10) ){ + break; + } + fts5BufferZero(&tmp); + + pThis = pHead; + pHead = pThis->pNext; + sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, pThis->iPos); + fts5PrefixMergerNextPosition(pThis); + fts5PrefixMergerInsertByPosition(&pHead, pThis); + + while( pHead->pNext ){ + pThis = pHead; + if( pThis->iPos!=iPrev ){ + sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, pThis->iPos); } + fts5PrefixMergerNextPosition(pThis); + pHead = pThis->pNext; + fts5PrefixMergerInsertByPosition(&pHead, pThis); + } - if( iPos1>=0 ){ - if( iPos1!=iPrev ){ - sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos1); - } - fts5BufferSafeAppendBlob(&tmp, &a1[iOff1], i1.nPoslist-iOff1); - }else{ - assert( iPos2>=0 && iPos2!=iPrev ); - sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos2); - fts5BufferSafeAppendBlob(&tmp, &a2[iOff2], i2.nPoslist-iOff2); + if( pHead->iPos!=iPrev ){ + sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, pHead->iPos); + } + nTail = pHead->iter.nPoslist - pHead->iOff; + + /* WRITEPOSLISTSIZE */ + assert_nc( tmp.n+nTail<=nTmp ); + assert( tmp.n+nTail<=nTmp+nMerge*10 ); + if( tmp.n+nTail>nTmp-FTS5_DATA_ZERO_PADDING ){ + if( p->rc==SQLITE_OK ) FTS5_CORRUPT_IDX(p); + break; + } + fts5BufferSafeAppendVarint(&out, (tmp.n+nTail) * 2); + fts5BufferSafeAppendBlob(&out, tmp.p, tmp.n); + if( nTail>0 ){ + fts5BufferSafeAppendBlob(&out, &pHead->aPos[pHead->iOff], nTail); + } + + pHead = pSave; + for(i=0; iiter.aPoslist && pX->iter.iRowid==iLastRowid ){ + fts5DoclistIterNext(&pX->iter); + fts5PrefixMergerInsertByRowid(&pHead, pX); } + } + + }else{ + /* Copy poslist from pHead to output */ + PrefixMerger *pThis = pHead; + Fts5DoclistIter *pI = &pThis->iter; + fts5BufferSafeAppendBlob(&out, pI->aPoslist, pI->nPoslist+pI->nSize); + fts5DoclistIterNext(pI); + pHead = pThis->pNext; + fts5PrefixMergerInsertByRowid(&pHead, pThis); + } + } + + fts5BufferFree(p1); + fts5BufferFree(&tmp); + memset(&out.p[out.n], 0, FTS5_DATA_ZERO_PADDING); + *p1 = out; +} + + +/* +** Iterate through a range of entries in the FTS index, invoking the xVisit +** callback for each of them. +** +** Parameter pToken points to an nToken buffer containing an FTS index term +** (i.e. a document term with the preceding 1 byte index identifier - +** FTS5_MAIN_PREFIX or similar). If bPrefix is true, then the call visits +** all entries for terms that have pToken/nToken as a prefix. If bPrefix +** is false, then only entries with pToken/nToken as the entire key are +** visited. +** +** If the current table is a tokendata=1 table, then if bPrefix is true then +** each index term is treated separately. However, if bPrefix is false, then +** all index terms corresponding to pToken/nToken are collapsed into a single +** term before the callback is invoked. +** +** The callback invoked for each entry visited is specified by paramter xVisit. +** Each time it is invoked, it is passed a pointer to the Fts5Index object, +** a copy of the 7th paramter to this function (pCtx) and a pointer to the +** iterator that indicates the current entry. If the current entry is the +** first with a new term (i.e. different from that of the previous entry, +** including the very first term), then the final two parameters are passed +** a pointer to the term and its size in bytes, respectively. If the current +** entry is not the first associated with its term, these two parameters +** are passed 0. +** +** If parameter pColset is not NULL, then it is used to filter entries before +** the callback is invoked. +*/ +static int fts5VisitEntries( + Fts5Index *p, /* Fts5 index object */ + Fts5Colset *pColset, /* Columns filter to apply, or NULL */ + u8 *pToken, /* Buffer containing token */ + int nToken, /* Size of buffer pToken in bytes */ + int bPrefix, /* True for a prefix scan */ + void (*xVisit)(Fts5Index*, void *pCtx, Fts5Iter *pIter, const u8*, int), + void *pCtx /* Passed as second argument to xVisit() */ +){ + const int flags = (bPrefix ? FTS5INDEX_QUERY_SCAN : 0) + | FTS5INDEX_QUERY_SKIPEMPTY + | FTS5INDEX_QUERY_NOOUTPUT; + Fts5Iter *p1 = 0; /* Iterator used to gather data from index */ + int bNewTerm = 1; + Fts5Structure *pStruct = fts5StructureRead(p); + + fts5MultiIterNew(p, pStruct, flags, pColset, pToken, nToken, -1, 0, &p1); + fts5IterSetOutputCb(&p->rc, p1); + for( /* no-op */ ; + fts5MultiIterEof(p, p1)==0; + fts5MultiIterNext2(p, p1, &bNewTerm) + ){ + Fts5SegIter *pSeg = &p1->aSeg[ p1->aFirst[1].iFirst ]; + int nNew = 0; + const u8 *pNew = 0; + + p1->xSetOutputs(p1, pSeg); + if( p->rc ) break; + + if( bNewTerm ){ + nNew = pSeg->term.n; + pNew = pSeg->term.p; + if( nNewrc; +} - /* WRITEPOSLISTSIZE */ - fts5BufferSafeAppendVarint(&out, tmp.n * 2); - fts5BufferSafeAppendBlob(&out, tmp.p, tmp.n); - fts5DoclistIterNext(&i1); - fts5DoclistIterNext(&i2); - if( i1.aPoslist==0 || i2.aPoslist==0 ) break; + +/* +** Usually, a tokendata=1 iterator (struct Fts5TokenDataIter) accumulates an +** array of these for each row it visits (so all iRowid fields are the same). +** Or, for an iterator used by an "ORDER BY rank" query, it accumulates an +** array of these for the entire query (in which case iRowid fields may take +** a variety of values). +** +** Each instance in the array indicates the iterator (and therefore term) +** associated with position iPos of rowid iRowid. This is used by the +** xInstToken() API. +** +** iRowid: +** Rowid for the current entry. +** +** iPos: +** Position of current entry within row. In the usual ((iCol<<32)+iOff) +** format (e.g. see macros FTS5_POS2COLUMN() and FTS5_POS2OFFSET()). +** +** iIter: +** If the Fts5TokenDataIter iterator that the entry is part of is +** actually an iterator (i.e. with nIter>0, not just a container for +** Fts5TokenDataMap structures), then this variable is an index into +** the apIter[] array. The corresponding term is that which the iterator +** at apIter[iIter] currently points to. +** +** Or, if the Fts5TokenDataIter iterator is just a container object +** (nIter==0), then iIter is an index into the term.p[] buffer where +** the term is stored. +** +** nByte: +** In the case where iIter is an index into term.p[], this variable +** is the size of the term in bytes. If iIter is an index into apIter[], +** this variable is unused. +*/ +struct Fts5TokenDataMap { + i64 iRowid; /* Row this token is located in */ + i64 iPos; /* Position of token */ + int iIter; /* Iterator token was read from */ + int nByte; /* Length of token in bytes (or 0) */ +}; + +/* +** An object used to supplement Fts5Iter for tokendata=1 iterators. +** +** This object serves two purposes. The first is as a container for an array +** of Fts5TokenDataMap structures, which are used to find the token required +** when the xInstToken() API is used. This is done by the nMapAlloc, nMap and +** aMap[] variables. +*/ +struct Fts5TokenDataIter { + int nMapAlloc; /* Allocated size of aMap[] in entries */ + int nMap; /* Number of valid entries in aMap[] */ + Fts5TokenDataMap *aMap; /* Array of (rowid+pos -> token) mappings */ + + /* The following are used for prefix-queries only. */ + Fts5Buffer terms; + + /* The following are used for other full-token tokendata queries only. */ + int nIter; + int nIterAlloc; + Fts5PoslistReader *aPoslistReader; + int *aPoslistToIter; + Fts5Iter *apIter[FLEXARRAY]; +}; + +/* Size in bytes of an Fts5TokenDataIter object holding up to N iterators */ +#define SZ_FTS5TOKENDATAITER(N) \ + (offsetof(Fts5TokenDataIter,apIter) + (N)*sizeof(Fts5Iter)) + +/* +** The two input arrays - a1[] and a2[] - are in sorted order. This function +** merges the two arrays together and writes the result to output array +** aOut[]. aOut[] is guaranteed to be large enough to hold the result. +** +** Duplicate entries are copied into the output. So the size of the output +** array is always (n1+n2) entries. +*/ +static void fts5TokendataMerge( + Fts5TokenDataMap *a1, int n1, /* Input array 1 */ + Fts5TokenDataMap *a2, int n2, /* Input array 2 */ + Fts5TokenDataMap *aOut /* Output array */ +){ + int i1 = 0; + int i2 = 0; + + assert( n1>=0 && n2>=0 ); + while( i1=n2 || (i1rc==SQLITE_OK ){ + if( pT->nMap==pT->nMapAlloc ){ + int nNew = pT->nMapAlloc ? pT->nMapAlloc*2 : 64; + int nAlloc = nNew * sizeof(Fts5TokenDataMap); + Fts5TokenDataMap *aNew; + + aNew = (Fts5TokenDataMap*)sqlite3_realloc(pT->aMap, nAlloc); + if( aNew==0 ){ + p->rc = SQLITE_NOMEM; + return; + } + + pT->aMap = aNew; + pT->nMapAlloc = nNew; + } + + pT->aMap[pT->nMap].iRowid = iRowid; + pT->aMap[pT->nMap].iPos = iPos; + pT->aMap[pT->nMap].iIter = iIter; + pT->aMap[pT->nMap].nByte = nByte; + pT->nMap++; + } +} + +/* +** Sort the contents of the pT->aMap[] array. +** +** The sorting algorithm requires a malloc(). If this fails, an error code +** is left in Fts5Index.rc before returning. +*/ +static void fts5TokendataIterSortMap(Fts5Index *p, Fts5TokenDataIter *pT){ + Fts5TokenDataMap *aTmp = 0; + int nByte = pT->nMap * sizeof(Fts5TokenDataMap); + + aTmp = (Fts5TokenDataMap*)sqlite3Fts5MallocZero(&p->rc, nByte); + if( aTmp ){ + Fts5TokenDataMap *a1 = pT->aMap; + Fts5TokenDataMap *a2 = aTmp; + i64 nHalf; + + for(nHalf=1; nHalfnMap; nHalf=nHalf*2){ + int i1; + for(i1=0; i1nMap; i1+=(nHalf*2)){ + int n1 = MIN(nHalf, pT->nMap-i1); + int n2 = MIN(nHalf, pT->nMap-i1-n1); + fts5TokendataMerge(&a1[i1], n1, &a1[i1+n1], n2, &a2[i1]); + } + SWAPVAL(Fts5TokenDataMap*, a1, a2); + } + + if( a1!=pT->aMap ){ + memcpy(pT->aMap, a1, pT->nMap*sizeof(Fts5TokenDataMap)); + } + sqlite3_free(aTmp); + +#ifdef SQLITE_DEBUG + { + int ii; + for(ii=1; iinMap; ii++){ + Fts5TokenDataMap *p1 = &pT->aMap[ii-1]; + Fts5TokenDataMap *p2 = &pT->aMap[ii]; + assert( p1->iRowidiRowid + || (p1->iRowid==p2->iRowid && p1->iPos<=p2->iPos) + ); } } +#endif + } +} - if( i1.aPoslist ){ - fts5MergeAppendDocid(&out, iLastRowid, i1.iRowid); - fts5BufferSafeAppendBlob(&out, i1.aPoslist, i1.aEof - i1.aPoslist); +/* +** Delete an Fts5TokenDataIter structure and its contents. +*/ +static void fts5TokendataIterDelete(Fts5TokenDataIter *pSet){ + if( pSet ){ + int ii; + for(ii=0; iinIter; ii++){ + fts5MultiIterFree(pSet->apIter[ii]); } - else if( i2.aPoslist ){ - fts5MergeAppendDocid(&out, iLastRowid, i2.iRowid); - fts5BufferSafeAppendBlob(&out, i2.aPoslist, i2.aEof - i2.aPoslist); + fts5BufferFree(&pSet->terms); + sqlite3_free(pSet->aPoslistReader); + sqlite3_free(pSet->aMap); + sqlite3_free(pSet); + } +} + + +/* +** fts5VisitEntries() context object used by fts5SetupPrefixIterTokendata() +** to pass data to prefixIterSetupTokendataCb(). +*/ +typedef struct TokendataSetupCtx TokendataSetupCtx; +struct TokendataSetupCtx { + Fts5TokenDataIter *pT; /* Object being populated with mappings */ + int iTermOff; /* Offset of current term in terms.p[] */ + int nTermByte; /* Size of current term in bytes */ +}; + +/* +** fts5VisitEntries() callback used by fts5SetupPrefixIterTokendata(). This +** callback adds an entry to the Fts5TokenDataIter.aMap[] array for each +** position in the current position-list. It doesn't matter that some of +** these may be out of order - they will be sorted later. +*/ +static void prefixIterSetupTokendataCb( + Fts5Index *p, + void *pCtx, + Fts5Iter *p1, + const u8 *pNew, + int nNew +){ + TokendataSetupCtx *pSetup = (TokendataSetupCtx*)pCtx; + int iPosOff = 0; + i64 iPos = 0; + + if( pNew ){ + pSetup->nTermByte = nNew-1; + pSetup->iTermOff = pSetup->pT->terms.n; + fts5BufferAppendBlob(&p->rc, &pSetup->pT->terms, nNew-1, pNew+1); + } + + while( 0==sqlite3Fts5PoslistNext64( + p1->base.pData, p1->base.nData, &iPosOff, &iPos + ) ){ + fts5TokendataIterAppendMap(p, + pSetup->pT, pSetup->iTermOff, pSetup->nTermByte, p1->base.iRowid, iPos + ); + } +} + + +/* +** Context object passed by fts5SetupPrefixIter() to fts5VisitEntries(). +*/ +typedef struct PrefixSetupCtx PrefixSetupCtx; +struct PrefixSetupCtx { + void (*xMerge)(Fts5Index*, Fts5Buffer*, int, Fts5Buffer*); + void (*xAppend)(Fts5Index*, u64, Fts5Iter*, Fts5Buffer*); + i64 iLastRowid; + int nMerge; + Fts5Buffer *aBuf; + int nBuf; + Fts5Buffer doclist; + TokendataSetupCtx *pTokendata; +}; + +/* +** fts5VisitEntries() callback used by fts5SetupPrefixIter() +*/ +static void prefixIterSetupCb( + Fts5Index *p, + void *pCtx, + Fts5Iter *p1, + const u8 *pNew, + int nNew +){ + PrefixSetupCtx *pSetup = (PrefixSetupCtx*)pCtx; + const int nMerge = pSetup->nMerge; + + if( p1->base.nData>0 ){ + if( p1->base.iRowid<=pSetup->iLastRowid && pSetup->doclist.n>0 ){ + int i; + for(i=0; p->rc==SQLITE_OK && pSetup->doclist.n; i++){ + int i1 = i*nMerge; + int iStore; + assert( i1+nMerge<=pSetup->nBuf ); + for(iStore=i1; iStoreaBuf[iStore].n==0 ){ + fts5BufferSwap(&pSetup->doclist, &pSetup->aBuf[iStore]); + fts5BufferZero(&pSetup->doclist); + break; + } + } + if( iStore==i1+nMerge ){ + pSetup->xMerge(p, &pSetup->doclist, nMerge, &pSetup->aBuf[i1]); + for(iStore=i1; iStoreaBuf[iStore]); + } + } + } + pSetup->iLastRowid = 0; } - fts5BufferSet(&p->rc, p1, out.n, out.p); - fts5BufferFree(&tmp); - fts5BufferFree(&out); + pSetup->xAppend( + p, (u64)p1->base.iRowid-(u64)pSetup->iLastRowid, p1, &pSetup->doclist + ); + pSetup->iLastRowid = p1->base.iRowid; + } + + if( pSetup->pTokendata ){ + prefixIterSetupTokendataCb(p, (void*)pSetup->pTokendata, p1, pNew, nNew); } } static void fts5SetupPrefixIter( Fts5Index *p, /* Index to read from */ int bDesc, /* True for "ORDER BY rowid DESC" */ - const u8 *pToken, /* Buffer containing prefix to match */ + int iIdx, /* Index to scan for data */ + u8 *pToken, /* Buffer containing prefix to match */ int nToken, /* Size of buffer pToken in bytes */ Fts5Colset *pColset, /* Restrict matches to these columns */ - Fts5Iter **ppIter /* OUT: New iterator */ + Fts5Iter **ppIter /* OUT: New iterator */ ){ Fts5Structure *pStruct; - Fts5Buffer *aBuf; - const int nBuf = 32; + PrefixSetupCtx s; + TokendataSetupCtx s2; + + memset(&s, 0, sizeof(s)); + memset(&s2, 0, sizeof(s2)); + + s.nMerge = 1; + s.iLastRowid = 0; + s.nBuf = 32; + if( iIdx==0 + && p->pConfig->eDetail==FTS5_DETAIL_FULL + && p->pConfig->bPrefixInsttoken + ){ + s.pTokendata = &s2; + s2.pT = (Fts5TokenDataIter*)fts5IdxMalloc(p, SZ_FTS5TOKENDATAITER(1)); + } - void (*xMerge)(Fts5Index*, Fts5Buffer*, Fts5Buffer*); - void (*xAppend)(Fts5Index*, i64, Fts5Iter*, Fts5Buffer*); if( p->pConfig->eDetail==FTS5_DETAIL_NONE ){ - xMerge = fts5MergeRowidLists; - xAppend = fts5AppendRowid; + s.xMerge = fts5MergeRowidLists; + s.xAppend = fts5AppendRowid; }else{ - xMerge = fts5MergePrefixLists; - xAppend = fts5AppendPoslist; + s.nMerge = FTS5_MERGE_NLIST-1; + s.nBuf = s.nMerge*8; /* Sufficient to merge (16^8)==(2^32) lists */ + s.xMerge = fts5MergePrefixLists; + s.xAppend = fts5AppendPoslist; } - aBuf = (Fts5Buffer*)fts5IdxMalloc(p, sizeof(Fts5Buffer)*nBuf); + s.aBuf = (Fts5Buffer*)fts5IdxMalloc(p, sizeof(Fts5Buffer)*s.nBuf); pStruct = fts5StructureRead(p); + assert( p->rc!=SQLITE_OK || (s.aBuf && pStruct) ); - if( aBuf && pStruct ){ - const int flags = FTS5INDEX_QUERY_SCAN - | FTS5INDEX_QUERY_SKIPEMPTY - | FTS5INDEX_QUERY_NOOUTPUT; + if( p->rc==SQLITE_OK ){ + void *pCtx = (void*)&s; int i; - i64 iLastRowid = 0; - Fts5Iter *p1 = 0; /* Iterator used to gather data from index */ Fts5Data *pData; - Fts5Buffer doclist; - int bNewTerm = 1; - - memset(&doclist, 0, sizeof(doclist)); - fts5MultiIterNew(p, pStruct, flags, pColset, pToken, nToken, -1, 0, &p1); - fts5IterSetOutputCb(&p->rc, p1); - for( /* no-op */ ; - fts5MultiIterEof(p, p1)==0; - fts5MultiIterNext2(p, p1, &bNewTerm) - ){ - Fts5SegIter *pSeg = &p1->aSeg[ p1->aFirst[1].iFirst ]; - int nTerm = pSeg->term.n; - const u8 *pTerm = pSeg->term.p; - p1->xSetOutputs(p1, pSeg); - - assert_nc( memcmp(pToken, pTerm, MIN(nToken, nTerm))<=0 ); - if( bNewTerm ){ - if( nTermbase.nData==0 ) continue; - - if( p1->base.iRowid<=iLastRowid && doclist.n>0 ){ - for(i=0; p->rc==SQLITE_OK && doclist.n; i++){ - assert( ibase.iRowid-iLastRowid, p1, &doclist); - iLastRowid = p1->base.iRowid; + /* If iIdx is non-zero, then it is the number of a prefix-index for + ** prefixes 1 character longer than the prefix being queried for. That + ** index contains all the doclists required, except for the one + ** corresponding to the prefix itself. That one is extracted from the + ** main term index here. */ + if( iIdx!=0 ){ + pToken[0] = FTS5_MAIN_PREFIX; + fts5VisitEntries(p, pColset, pToken, nToken, 0, prefixIterSetupCb, pCtx); } - for(i=0; irc==SQLITE_OK ){ - xMerge(p, &doclist, &aBuf[i]); + s.xMerge(p, &s.doclist, s.nMerge, &s.aBuf[i]); + } + for(iFree=i; iFreerc!=SQLITE_OK ); if( pData ){ pData->p = (u8*)&pData[1]; - pData->nn = pData->szLeaf = doclist.n; - memcpy(pData->p, doclist.p, doclist.n); + pData->nn = pData->szLeaf = s.doclist.n; + if( s.doclist.n ) memcpy(pData->p, s.doclist.p, s.doclist.n); fts5MultiIterNew2(p, pData, bDesc, ppIter); } - fts5BufferFree(&doclist); + + assert( (*ppIter)!=0 || p->rc!=SQLITE_OK ); + if( p->rc==SQLITE_OK && s.pTokendata ){ + fts5TokendataIterSortMap(p, s2.pT); + (*ppIter)->pTokenDataIter = s2.pT; + s2.pT = 0; + } } + fts5TokendataIterDelete(s2.pT); + fts5BufferFree(&s.doclist); fts5StructureRelease(pStruct); - sqlite3_free(aBuf); + sqlite3_free(s.aBuf); } @@ -4921,23 +6780,26 @@ int sqlite3Fts5IndexBeginWrite(Fts5Index *p, int bDelete, i64 iRowid){ /* Flush the hash table to disk if required */ if( iRowidiWriteRowid || (iRowid==p->iWriteRowid && p->bDelete==0) - || (p->nPendingData > p->pConfig->nHashSize) + || (p->nPendingData > p->pConfig->nHashSize) ){ fts5IndexFlush(p); } p->iWriteRowid = iRowid; p->bDelete = bDelete; + if( bDelete==0 ){ + p->nPendingRow++; + } return fts5IndexReturn(p); } /* ** Commit data to disk. */ -int sqlite3Fts5IndexSync(Fts5Index *p, int bCommit){ +int sqlite3Fts5IndexSync(Fts5Index *p){ assert( p->rc==SQLITE_OK ); fts5IndexFlush(p); - if( bCommit ) fts5CloseReader(p); + fts5IndexCloseReader(p); return fts5IndexReturn(p); } @@ -4948,10 +6810,10 @@ int sqlite3Fts5IndexSync(Fts5Index *p, int bCommit){ ** records must be invalidated. */ int sqlite3Fts5IndexRollback(Fts5Index *p){ - fts5CloseReader(p); + fts5IndexCloseReader(p); fts5IndexDiscardData(p); - /* assert( p->rc==SQLITE_OK ); */ - return SQLITE_OK; + fts5StructureInvalidate(p); + return fts5IndexReturn(p); } /* @@ -4960,10 +6822,20 @@ int sqlite3Fts5IndexRollback(Fts5Index *p){ ** and the initial version of the "averages" record (a zero-byte blob). */ int sqlite3Fts5IndexReinit(Fts5Index *p){ - Fts5Structure s; - memset(&s, 0, sizeof(Fts5Structure)); + Fts5Structure *pTmp; + union { + Fts5Structure sFts; + u8 tmpSpace[SZ_FTS5STRUCTURE(1)]; + } uFts; + fts5StructureInvalidate(p); + fts5IndexDiscardData(p); + pTmp = &uFts.sFts; + memset(uFts.tmpSpace, 0, sizeof(uFts.tmpSpace)); + if( p->pConfig->bContentlessDelete ){ + pTmp->nOriginCntr = 1; + } fts5DataWrite(p, FTS5_AVERAGES_ROWID, (const u8*)"", 0); - fts5StructureWrite(p, &s); + fts5StructureWrite(p, pTmp); return fts5IndexReturn(p); } @@ -5019,11 +6891,15 @@ int sqlite3Fts5IndexClose(Fts5Index *p){ int rc = SQLITE_OK; if( p ){ assert( p->pReader==0 ); + fts5StructureInvalidate(p); sqlite3_finalize(p->pWriter); sqlite3_finalize(p->pDeleter); sqlite3_finalize(p->pIdxWriter); sqlite3_finalize(p->pIdxDeleter); sqlite3_finalize(p->pIdxSelect); + sqlite3_finalize(p->pIdxNextSelect); + sqlite3_finalize(p->pDataVersion); + sqlite3_finalize(p->pDeleteFromIdx); sqlite3Fts5HashFree(p->pHash); sqlite3_free(p->zDataTbl); sqlite3_free(p); @@ -5046,7 +6922,14 @@ int sqlite3Fts5IndexCharlenToBytelen( for(i=0; i=nByte ) return 0; /* Input contains fewer than nChar chars */ if( (unsigned char)p[n++]>=0xc0 ){ - while( (p[n] & 0xc0)==0x80 ) n++; + if( n>=nByte ) return 0; + while( (p[n] & 0xc0)==0x80 ){ + n++; + if( n>=nByte ){ + if( i+1==nChar ) break; + return 0; + } + } } } return n; @@ -5111,52 +6994,450 @@ int sqlite3Fts5IndexWrite( } /* -** Open a new iterator to iterate though all rowid that match the -** specified token or token prefix. +** pToken points to a buffer of size nToken bytes containing a search +** term, including the index number at the start, used on a tokendata=1 +** table. This function returns true if the term in buffer pBuf matches +** token pToken/nToken. */ -int sqlite3Fts5IndexQuery( - Fts5Index *p, /* FTS index to query */ - const char *pToken, int nToken, /* Token (or prefix) to query for */ - int flags, /* Mask of FTS5INDEX_QUERY_X flags */ - Fts5Colset *pColset, /* Match these columns only */ - Fts5IndexIter **ppIter /* OUT: New iterator object */ +static int fts5IsTokendataPrefix( + Fts5Buffer *pBuf, + const u8 *pToken, + int nToken ){ - Fts5Config *pConfig = p->pConfig; - Fts5Iter *pRet = 0; - Fts5Buffer buf = {0, 0, 0}; + return ( + pBuf->n>=nToken + && 0==memcmp(pBuf->p, pToken, nToken) + && (pBuf->n==nToken || pBuf->p[nToken]==0x00) + ); +} - /* If the QUERY_SCAN flag is set, all other flags must be clear. */ - assert( (flags & FTS5INDEX_QUERY_SCAN)==0 || flags==FTS5INDEX_QUERY_SCAN ); +/* +** Ensure the segment-iterator passed as the only argument points to EOF. +*/ +static void fts5SegIterSetEOF(Fts5SegIter *pSeg){ + fts5DataRelease(pSeg->pLeaf); + pSeg->pLeaf = 0; +} - if( sqlite3Fts5BufferSize(&p->rc, &buf, nToken+1)==0 ){ - int iIdx = 0; /* Index to search */ - memcpy(&buf.p[1], pToken, nToken); +static void fts5IterClose(Fts5IndexIter *pIndexIter){ + if( pIndexIter ){ + Fts5Iter *pIter = (Fts5Iter*)pIndexIter; + Fts5Index *pIndex = pIter->pIndex; + fts5TokendataIterDelete(pIter->pTokenDataIter); + fts5MultiIterFree(pIter); + fts5IndexCloseReader(pIndex); + } +} - /* Figure out which index to search and set iIdx accordingly. If this - ** is a prefix query for which there is no prefix index, set iIdx to - ** greater than pConfig->nPrefix to indicate that the query will be - ** satisfied by scanning multiple terms in the main index. - ** - ** If the QUERY_TEST_NOIDX flag was specified, then this must be a - ** prefix-query. Instead of using a prefix-index (if one exists), - ** evaluate the prefix query using the main FTS index. This is used - ** for internal sanity checking by the integrity-check in debug - ** mode only. */ -#ifdef SQLITE_DEBUG - if( pConfig->bPrefixIndex==0 || (flags & FTS5INDEX_QUERY_TEST_NOIDX) ){ - assert( flags & FTS5INDEX_QUERY_PREFIX ); - iIdx = 1+pConfig->nPrefix; - }else -#endif - if( flags & FTS5INDEX_QUERY_PREFIX ){ - int nChar = fts5IndexCharlen(pToken, nToken); - for(iIdx=1; iIdx<=pConfig->nPrefix; iIdx++){ - if( pConfig->aPrefix[iIdx-1]==nChar ) break; +/* +** This function appends iterator pAppend to Fts5TokenDataIter pIn and +** returns the result. +*/ +static Fts5TokenDataIter *fts5AppendTokendataIter( + Fts5Index *p, /* Index object (for error code) */ + Fts5TokenDataIter *pIn, /* Current Fts5TokenDataIter struct */ + Fts5Iter *pAppend /* Append this iterator */ +){ + Fts5TokenDataIter *pRet = pIn; + + if( p->rc==SQLITE_OK ){ + if( pIn==0 || pIn->nIter==pIn->nIterAlloc ){ + int nAlloc = pIn ? pIn->nIterAlloc*2 : 16; + int nByte = SZ_FTS5TOKENDATAITER(nAlloc+1); + Fts5TokenDataIter *pNew = (Fts5TokenDataIter*)sqlite3_realloc(pIn, nByte); + + if( pNew==0 ){ + p->rc = SQLITE_NOMEM; + }else{ + if( pIn==0 ) memset(pNew, 0, nByte); + pRet = pNew; + pNew->nIterAlloc = nAlloc; } } + } + if( p->rc ){ + fts5IterClose((Fts5IndexIter*)pAppend); + }else{ + pRet->apIter[pRet->nIter++] = pAppend; + } + assert( pRet==0 || pRet->nIter<=pRet->nIterAlloc ); - if( iIdx<=pConfig->nPrefix ){ - /* Straight index lookup */ + return pRet; +} + +/* +** The iterator passed as the only argument must be a tokendata=1 iterator +** (pIter->pTokenDataIter!=0). This function sets the iterator output +** variables (pIter->base.*) according to the contents of the current +** row. +*/ +static void fts5IterSetOutputsTokendata(Fts5Iter *pIter){ + int ii; + int nHit = 0; + i64 iRowid = SMALLEST_INT64; + int iMin = 0; + + Fts5TokenDataIter *pT = pIter->pTokenDataIter; + + pIter->base.nData = 0; + pIter->base.pData = 0; + + for(ii=0; iinIter; ii++){ + Fts5Iter *p = pT->apIter[ii]; + if( p->base.bEof==0 ){ + if( nHit==0 || p->base.iRowidbase.iRowid; + nHit = 1; + pIter->base.pData = p->base.pData; + pIter->base.nData = p->base.nData; + iMin = ii; + }else if( p->base.iRowid==iRowid ){ + nHit++; + } + } + } + + if( nHit==0 ){ + pIter->base.bEof = 1; + }else{ + int eDetail = pIter->pIndex->pConfig->eDetail; + pIter->base.bEof = 0; + pIter->base.iRowid = iRowid; + + if( nHit==1 && eDetail==FTS5_DETAIL_FULL ){ + fts5TokendataIterAppendMap(pIter->pIndex, pT, iMin, 0, iRowid, -1); + }else + if( nHit>1 && eDetail!=FTS5_DETAIL_NONE ){ + int nReader = 0; + int nByte = 0; + i64 iPrev = 0; + + /* Allocate array of iterators if they are not already allocated. */ + if( pT->aPoslistReader==0 ){ + pT->aPoslistReader = (Fts5PoslistReader*)sqlite3Fts5MallocZero( + &pIter->pIndex->rc, + pT->nIter * (sizeof(Fts5PoslistReader) + sizeof(int)) + ); + if( pT->aPoslistReader==0 ) return; + pT->aPoslistToIter = (int*)&pT->aPoslistReader[pT->nIter]; + } + + /* Populate an iterator for each poslist that will be merged */ + for(ii=0; iinIter; ii++){ + Fts5Iter *p = pT->apIter[ii]; + if( iRowid==p->base.iRowid ){ + pT->aPoslistToIter[nReader] = ii; + sqlite3Fts5PoslistReaderInit( + p->base.pData, p->base.nData, &pT->aPoslistReader[nReader++] + ); + nByte += p->base.nData; + } + } + + /* Ensure the output buffer is large enough */ + if( fts5BufferGrow(&pIter->pIndex->rc, &pIter->poslist, nByte+nHit*10) ){ + return; + } + + /* Ensure the token-mapping is large enough */ + if( eDetail==FTS5_DETAIL_FULL && pT->nMapAlloc<(pT->nMap + nByte) ){ + int nNew = (pT->nMapAlloc + nByte) * 2; + Fts5TokenDataMap *aNew = (Fts5TokenDataMap*)sqlite3_realloc( + pT->aMap, nNew*sizeof(Fts5TokenDataMap) + ); + if( aNew==0 ){ + pIter->pIndex->rc = SQLITE_NOMEM; + return; + } + pT->aMap = aNew; + pT->nMapAlloc = nNew; + } + + pIter->poslist.n = 0; + + while( 1 ){ + i64 iMinPos = LARGEST_INT64; + + /* Find smallest position */ + iMin = 0; + for(ii=0; iiaPoslistReader[ii]; + if( pReader->bEof==0 ){ + if( pReader->iPosiPos; + iMin = ii; + } + } + } + + /* If all readers were at EOF, break out of the loop. */ + if( iMinPos==LARGEST_INT64 ) break; + + sqlite3Fts5PoslistSafeAppend(&pIter->poslist, &iPrev, iMinPos); + sqlite3Fts5PoslistReaderNext(&pT->aPoslistReader[iMin]); + + if( eDetail==FTS5_DETAIL_FULL ){ + pT->aMap[pT->nMap].iPos = iMinPos; + pT->aMap[pT->nMap].iIter = pT->aPoslistToIter[iMin]; + pT->aMap[pT->nMap].iRowid = iRowid; + pT->nMap++; + } + } + + pIter->base.pData = pIter->poslist.p; + pIter->base.nData = pIter->poslist.n; + } + } +} + +/* +** The iterator passed as the only argument must be a tokendata=1 iterator +** (pIter->pTokenDataIter!=0). This function advances the iterator. If +** argument bFrom is false, then the iterator is advanced to the next +** entry. Or, if bFrom is true, it is advanced to the first entry with +** a rowid of iFrom or greater. +*/ +static void fts5TokendataIterNext(Fts5Iter *pIter, int bFrom, i64 iFrom){ + int ii; + Fts5TokenDataIter *pT = pIter->pTokenDataIter; + Fts5Index *pIndex = pIter->pIndex; + + for(ii=0; iinIter; ii++){ + Fts5Iter *p = pT->apIter[ii]; + if( p->base.bEof==0 + && (p->base.iRowid==pIter->base.iRowid || (bFrom && p->base.iRowidbase.bEof==0 + && p->base.iRowidrc==SQLITE_OK + ){ + fts5MultiIterNext(pIndex, p, 0, 0); + } + } + } + + if( pIndex->rc==SQLITE_OK ){ + fts5IterSetOutputsTokendata(pIter); + } +} + +/* +** If the segment-iterator passed as the first argument is at EOF, then +** set pIter->term to a copy of buffer pTerm. +*/ +static void fts5TokendataSetTermIfEof(Fts5Iter *pIter, Fts5Buffer *pTerm){ + if( pIter && pIter->aSeg[0].pLeaf==0 ){ + fts5BufferSet(&pIter->pIndex->rc, &pIter->aSeg[0].term, pTerm->n, pTerm->p); + } +} + +/* +** This function sets up an iterator to use for a non-prefix query on a +** tokendata=1 table. +*/ +static Fts5Iter *fts5SetupTokendataIter( + Fts5Index *p, /* FTS index to query */ + const u8 *pToken, /* Buffer containing query term */ + int nToken, /* Size of buffer pToken in bytes */ + Fts5Colset *pColset /* Colset to filter on */ +){ + Fts5Iter *pRet = 0; + Fts5TokenDataIter *pSet = 0; + Fts5Structure *pStruct = 0; + const int flags = FTS5INDEX_QUERY_SCANONETERM | FTS5INDEX_QUERY_SCAN; + + Fts5Buffer bSeek = {0, 0, 0}; + Fts5Buffer *pSmall = 0; + + fts5IndexFlush(p); + pStruct = fts5StructureRead(p); + + while( p->rc==SQLITE_OK ){ + Fts5Iter *pPrev = pSet ? pSet->apIter[pSet->nIter-1] : 0; + Fts5Iter *pNew = 0; + Fts5SegIter *pNewIter = 0; + Fts5SegIter *pPrevIter = 0; + + int iLvl, iSeg, ii; + + pNew = fts5MultiIterAlloc(p, pStruct->nSegment); + if( pSmall ){ + fts5BufferSet(&p->rc, &bSeek, pSmall->n, pSmall->p); + fts5BufferAppendBlob(&p->rc, &bSeek, 1, (const u8*)"\0"); + }else{ + fts5BufferSet(&p->rc, &bSeek, nToken, pToken); + } + if( p->rc ){ + fts5IterClose((Fts5IndexIter*)pNew); + break; + } + + pNewIter = &pNew->aSeg[0]; + pPrevIter = (pPrev ? &pPrev->aSeg[0] : 0); + for(iLvl=0; iLvlnLevel; iLvl++){ + for(iSeg=pStruct->aLevel[iLvl].nSeg-1; iSeg>=0; iSeg--){ + Fts5StructureSegment *pSeg = &pStruct->aLevel[iLvl].aSeg[iSeg]; + int bDone = 0; + + if( pPrevIter ){ + if( fts5BufferCompare(pSmall, &pPrevIter->term) ){ + memcpy(pNewIter, pPrevIter, sizeof(Fts5SegIter)); + memset(pPrevIter, 0, sizeof(Fts5SegIter)); + bDone = 1; + }else if( pPrevIter->iEndofDoclist>pPrevIter->pLeaf->szLeaf ){ + fts5SegIterNextInit(p,(const char*)bSeek.p,bSeek.n-1,pSeg,pNewIter); + bDone = 1; + } + } + + if( bDone==0 ){ + fts5SegIterSeekInit(p, bSeek.p, bSeek.n, flags, pSeg, pNewIter); + } + + if( pPrevIter ){ + if( pPrevIter->pTombArray ){ + pNewIter->pTombArray = pPrevIter->pTombArray; + pNewIter->pTombArray->nRef++; + } + }else{ + fts5SegIterAllocTombstone(p, pNewIter); + } + + pNewIter++; + if( pPrevIter ) pPrevIter++; + if( p->rc ) break; + } + } + fts5TokendataSetTermIfEof(pPrev, pSmall); + + pNew->bSkipEmpty = 1; + pNew->pColset = pColset; + fts5IterSetOutputCb(&p->rc, pNew); + + /* Loop through all segments in the new iterator. Find the smallest + ** term that any segment-iterator points to. Iterator pNew will be + ** used for this term. Also, set any iterator that points to a term that + ** does not match pToken/nToken to point to EOF */ + pSmall = 0; + for(ii=0; iinSeg; ii++){ + Fts5SegIter *pII = &pNew->aSeg[ii]; + if( 0==fts5IsTokendataPrefix(&pII->term, pToken, nToken) ){ + fts5SegIterSetEOF(pII); + } + if( pII->pLeaf && (!pSmall || fts5BufferCompare(pSmall, &pII->term)>0) ){ + pSmall = &pII->term; + } + } + + /* If pSmall is still NULL at this point, then the new iterator does + ** not point to any terms that match the query. So delete it and break + ** out of the loop - all required iterators have been collected. */ + if( pSmall==0 ){ + fts5IterClose((Fts5IndexIter*)pNew); + break; + } + + /* Append this iterator to the set and continue. */ + pSet = fts5AppendTokendataIter(p, pSet, pNew); + } + + if( p->rc==SQLITE_OK && pSet ){ + int ii; + for(ii=0; iinIter; ii++){ + Fts5Iter *pIter = pSet->apIter[ii]; + int iSeg; + for(iSeg=0; iSegnSeg; iSeg++){ + pIter->aSeg[iSeg].flags |= FTS5_SEGITER_ONETERM; + } + fts5MultiIterFinishSetup(p, pIter); + } + } + + if( p->rc==SQLITE_OK ){ + pRet = fts5MultiIterAlloc(p, 0); + } + if( pRet ){ + pRet->nSeg = 0; + pRet->pTokenDataIter = pSet; + if( pSet ){ + fts5IterSetOutputsTokendata(pRet); + }else{ + pRet->base.bEof = 1; + } + }else{ + fts5TokendataIterDelete(pSet); + } + + fts5StructureRelease(pStruct); + fts5BufferFree(&bSeek); + return pRet; +} + +/* +** Open a new iterator to iterate though all rowid that match the +** specified token or token prefix. +*/ +int sqlite3Fts5IndexQuery( + Fts5Index *p, /* FTS index to query */ + const char *pToken, int nToken, /* Token (or prefix) to query for */ + int flags, /* Mask of FTS5INDEX_QUERY_X flags */ + Fts5Colset *pColset, /* Match these columns only */ + Fts5IndexIter **ppIter /* OUT: New iterator object */ +){ + Fts5Config *pConfig = p->pConfig; + Fts5Iter *pRet = 0; + Fts5Buffer buf = {0, 0, 0}; + + /* If the QUERY_SCAN flag is set, all other flags must be clear. */ + assert( (flags & FTS5INDEX_QUERY_SCAN)==0 || flags==FTS5INDEX_QUERY_SCAN ); + + if( sqlite3Fts5BufferSize(&p->rc, &buf, nToken+1)==0 ){ + int iIdx = 0; /* Index to search */ + int iPrefixIdx = 0; /* +1 prefix index */ + int bTokendata = pConfig->bTokendata; + assert( buf.p!=0 ); + if( nToken>0 ) memcpy(&buf.p[1], pToken, nToken); + + /* The NOTOKENDATA flag is set when each token in a tokendata=1 table + ** should be treated individually, instead of merging all those with + ** a common prefix into a single entry. This is used, for example, by + ** queries performed as part of an integrity-check, or by the fts5vocab + ** module. */ + if( flags & (FTS5INDEX_QUERY_NOTOKENDATA|FTS5INDEX_QUERY_SCAN) ){ + bTokendata = 0; + } + + /* Figure out which index to search and set iIdx accordingly. If this + ** is a prefix query for which there is no prefix index, set iIdx to + ** greater than pConfig->nPrefix to indicate that the query will be + ** satisfied by scanning multiple terms in the main index. + ** + ** If the QUERY_TEST_NOIDX flag was specified, then this must be a + ** prefix-query. Instead of using a prefix-index (if one exists), + ** evaluate the prefix query using the main FTS index. This is used + ** for internal sanity checking by the integrity-check in debug + ** mode only. */ +#ifdef SQLITE_DEBUG + if( pConfig->bPrefixIndex==0 || (flags & FTS5INDEX_QUERY_TEST_NOIDX) ){ + assert( flags & FTS5INDEX_QUERY_PREFIX ); + iIdx = 1+pConfig->nPrefix; + }else +#endif + if( flags & FTS5INDEX_QUERY_PREFIX ){ + int nChar = fts5IndexCharlen(pToken, nToken); + for(iIdx=1; iIdx<=pConfig->nPrefix; iIdx++){ + int nIdxChar = pConfig->aPrefix[iIdx-1]; + if( nIdxChar==nChar ) break; + if( nIdxChar==nChar+1 ) iPrefixIdx = iIdx; + } + } + + if( bTokendata && iIdx==0 ){ + buf.p[0] = FTS5_MAIN_PREFIX; + pRet = fts5SetupTokendataIter(p, buf.p, nToken+1, pColset); + }else if( iIdx<=pConfig->nPrefix ){ + /* Straight index lookup */ Fts5Structure *pStruct = fts5StructureRead(p); buf.p[0] = (u8)(FTS5_MAIN_PREFIX + iIdx); if( pStruct ){ @@ -5166,25 +7447,28 @@ int sqlite3Fts5IndexQuery( fts5StructureRelease(pStruct); } }else{ - /* Scan multiple terms in the main index */ + /* Scan multiple terms in the main index for a prefix query. */ int bDesc = (flags & FTS5INDEX_QUERY_DESC)!=0; - buf.p[0] = FTS5_MAIN_PREFIX; - fts5SetupPrefixIter(p, bDesc, buf.p, nToken+1, pColset, &pRet); - assert( p->rc!=SQLITE_OK || pRet->pColset==0 ); - fts5IterSetOutputCb(&p->rc, pRet); - if( p->rc==SQLITE_OK ){ - Fts5SegIter *pSeg = &pRet->aSeg[pRet->aFirst[1].iFirst]; - if( pSeg->pLeaf ) pRet->xSetOutputs(pRet, pSeg); + fts5SetupPrefixIter(p, bDesc, iPrefixIdx, buf.p, nToken+1, pColset,&pRet); + if( pRet==0 ){ + assert( p->rc!=SQLITE_OK ); + }else{ + assert( pRet->pColset==0 ); + fts5IterSetOutputCb(&p->rc, pRet); + if( p->rc==SQLITE_OK ){ + Fts5SegIter *pSeg = &pRet->aSeg[pRet->aFirst[1].iFirst]; + if( pSeg->pLeaf ) pRet->xSetOutputs(pRet, pSeg); + } } } if( p->rc ){ - sqlite3Fts5IterClose(&pRet->base); + fts5IterClose((Fts5IndexIter*)pRet); pRet = 0; - fts5CloseReader(p); + fts5IndexCloseReader(p); } - *ppIter = &pRet->base; + *ppIter = (Fts5IndexIter*)pRet; sqlite3Fts5BufferFree(&buf); } return fts5IndexReturn(p); @@ -5199,7 +7483,12 @@ int sqlite3Fts5IndexQuery( int sqlite3Fts5IterNext(Fts5IndexIter *pIndexIter){ Fts5Iter *pIter = (Fts5Iter*)pIndexIter; assert( pIter->pIndex->rc==SQLITE_OK ); - fts5MultiIterNext(pIter->pIndex, pIter, 0, 0); + if( pIter->nSeg==0 ){ + assert( pIter->pTokenDataIter ); + fts5TokendataIterNext(pIter, 0, 0); + }else{ + fts5MultiIterNext(pIter->pIndex, pIter, 0, 0); + } return fts5IndexReturn(pIter->pIndex); } @@ -5232,7 +7521,12 @@ int sqlite3Fts5IterNextScan(Fts5IndexIter *pIndexIter){ */ int sqlite3Fts5IterNextFrom(Fts5IndexIter *pIndexIter, i64 iMatch){ Fts5Iter *pIter = (Fts5Iter*)pIndexIter; - fts5MultiIterNextFrom(pIter->pIndex, pIter, iMatch); + if( pIter->nSeg==0 ){ + assert( pIter->pTokenDataIter ); + fts5TokendataIterNext(pIter, 1, iMatch); + }else{ + fts5MultiIterNextFrom(pIter->pIndex, pIter, iMatch); + } return fts5IndexReturn(pIter->pIndex); } @@ -5242,8 +7536,180 @@ int sqlite3Fts5IterNextFrom(Fts5IndexIter *pIndexIter, i64 iMatch){ const char *sqlite3Fts5IterTerm(Fts5IndexIter *pIndexIter, int *pn){ int n; const char *z = (const char*)fts5MultiIterTerm((Fts5Iter*)pIndexIter, &n); + assert_nc( z || n<=1 ); *pn = n-1; - return &z[1]; + return (z ? &z[1] : 0); +} + +/* +** pIter is a prefix query. This function populates pIter->pTokenDataIter +** with an Fts5TokenDataIter object containing mappings for all rows +** matched by the query. +*/ +static int fts5SetupPrefixIterTokendata( + Fts5Iter *pIter, + const char *pToken, /* Token prefix to search for */ + int nToken /* Size of pToken in bytes */ +){ + Fts5Index *p = pIter->pIndex; + Fts5Buffer token = {0, 0, 0}; + TokendataSetupCtx ctx; + + memset(&ctx, 0, sizeof(ctx)); + + fts5BufferGrow(&p->rc, &token, nToken+1); + assert( token.p!=0 || p->rc!=SQLITE_OK ); + ctx.pT = (Fts5TokenDataIter*)sqlite3Fts5MallocZero(&p->rc, + SZ_FTS5TOKENDATAITER(1)); + + if( p->rc==SQLITE_OK ){ + + /* Fill in the token prefix to search for */ + token.p[0] = FTS5_MAIN_PREFIX; + memcpy(&token.p[1], pToken, nToken); + token.n = nToken+1; + + fts5VisitEntries( + p, 0, token.p, token.n, 1, prefixIterSetupTokendataCb, (void*)&ctx + ); + + fts5TokendataIterSortMap(p, ctx.pT); + } + + if( p->rc==SQLITE_OK ){ + pIter->pTokenDataIter = ctx.pT; + }else{ + fts5TokendataIterDelete(ctx.pT); + } + fts5BufferFree(&token); + + return fts5IndexReturn(p); +} + +/* +** This is used by xInstToken() to access the token at offset iOff, column +** iCol of row iRowid. The token is returned via output variables *ppOut +** and *pnOut. The iterator passed as the first argument must be a tokendata=1 +** iterator (pIter->pTokenDataIter!=0). +** +** pToken/nToken: +*/ +int sqlite3Fts5IterToken( + Fts5IndexIter *pIndexIter, + const char *pToken, int nToken, + i64 iRowid, + int iCol, + int iOff, + const char **ppOut, int *pnOut +){ + Fts5Iter *pIter = (Fts5Iter*)pIndexIter; + Fts5TokenDataIter *pT = pIter->pTokenDataIter; + i64 iPos = (((i64)iCol)<<32) + iOff; + Fts5TokenDataMap *aMap = 0; + int i1 = 0; + int i2 = 0; + int iTest = 0; + + assert( pT || (pToken && pIter->nSeg>0) ); + if( pT==0 ){ + int rc = fts5SetupPrefixIterTokendata(pIter, pToken, nToken); + if( rc!=SQLITE_OK ) return rc; + pT = pIter->pTokenDataIter; + } + + i2 = pT->nMap; + aMap = pT->aMap; + + while( i2>i1 ){ + iTest = (i1 + i2) / 2; + + if( aMap[iTest].iRowidiRowid ){ + i2 = iTest; + }else{ + if( aMap[iTest].iPosiPos ){ + i2 = iTest; + }else{ + break; + } + } + } + + if( i2>i1 ){ + if( pIter->nSeg==0 ){ + Fts5Iter *pMap = pT->apIter[aMap[iTest].iIter]; + *ppOut = (const char*)pMap->aSeg[0].term.p+1; + *pnOut = pMap->aSeg[0].term.n-1; + }else{ + Fts5TokenDataMap *p = &aMap[iTest]; + *ppOut = (const char*)&pT->terms.p[p->iIter]; + *pnOut = aMap[iTest].nByte; + } + } + + return SQLITE_OK; +} + +/* +** Clear any existing entries from the token-map associated with the +** iterator passed as the only argument. +*/ +void sqlite3Fts5IndexIterClearTokendata(Fts5IndexIter *pIndexIter){ + Fts5Iter *pIter = (Fts5Iter*)pIndexIter; + if( pIter && pIter->pTokenDataIter + && (pIter->nSeg==0 || pIter->pIndex->pConfig->eDetail!=FTS5_DETAIL_FULL) + ){ + pIter->pTokenDataIter->nMap = 0; + } +} + +/* +** Set a token-mapping for the iterator passed as the first argument. This +** is used in detail=column or detail=none mode when a token is requested +** using the xInstToken() API. In this case the caller tokenizers the +** current row and configures the token-mapping via multiple calls to this +** function. +*/ +int sqlite3Fts5IndexIterWriteTokendata( + Fts5IndexIter *pIndexIter, + const char *pToken, int nToken, + i64 iRowid, int iCol, int iOff +){ + Fts5Iter *pIter = (Fts5Iter*)pIndexIter; + Fts5TokenDataIter *pT = pIter->pTokenDataIter; + Fts5Index *p = pIter->pIndex; + i64 iPos = (((i64)iCol)<<32) + iOff; + + assert( p->pConfig->eDetail!=FTS5_DETAIL_FULL ); + assert( pIter->pTokenDataIter || pIter->nSeg>0 ); + if( pIter->nSeg>0 ){ + /* This is a prefix term iterator. */ + if( pT==0 ){ + pT = (Fts5TokenDataIter*)sqlite3Fts5MallocZero(&p->rc, + SZ_FTS5TOKENDATAITER(1)); + pIter->pTokenDataIter = pT; + } + if( pT ){ + fts5TokendataIterAppendMap(p, pT, pT->terms.n, nToken, iRowid, iPos); + fts5BufferAppendBlob(&p->rc, &pT->terms, nToken, (const u8*)pToken); + } + }else{ + int ii; + for(ii=0; iinIter; ii++){ + Fts5Buffer *pTerm = &pT->apIter[ii]->aSeg[0].term; + if( nToken==pTerm->n-1 && memcmp(pToken, pTerm->p+1, nToken)==0 ) break; + } + if( iinIter ){ + fts5TokendataIterAppendMap(p, pT, ii, 0, iRowid, iPos); + } + } + return fts5IndexReturn(p); } /* @@ -5251,10 +7717,9 @@ const char *sqlite3Fts5IterTerm(Fts5IndexIter *pIndexIter, int *pn){ */ void sqlite3Fts5IterClose(Fts5IndexIter *pIndexIter){ if( pIndexIter ){ - Fts5Iter *pIter = (Fts5Iter*)pIndexIter; - Fts5Index *pIndex = pIter->pIndex; - fts5MultiIterFree(pIter); - fts5CloseReader(pIndex); + Fts5Index *pIndex = ((Fts5Iter*)pIndexIter)->pIndex; + fts5IterClose(pIndexIter); + fts5IndexReturn(pIndex); } } @@ -5279,64 +7744,405 @@ int sqlite3Fts5IndexGetAverages(Fts5Index *p, i64 *pnRow, i64 *anSize){ i += fts5GetVarint(&pData->p[i], (u64*)&anSize[iCol]); } } - - fts5DataRelease(pData); - return fts5IndexReturn(p); + + fts5DataRelease(pData); + return fts5IndexReturn(p); +} + +/* +** Replace the current "averages" record with the contents of the buffer +** supplied as the second argument. +*/ +int sqlite3Fts5IndexSetAverages(Fts5Index *p, const u8 *pData, int nData){ + assert( p->rc==SQLITE_OK ); + fts5DataWrite(p, FTS5_AVERAGES_ROWID, pData, nData); + return fts5IndexReturn(p); +} + +/* +** Return the total number of blocks this module has read from the %_data +** table since it was created. +*/ +int sqlite3Fts5IndexReads(Fts5Index *p){ + return p->nRead; +} + +/* +** Set the 32-bit cookie value stored at the start of all structure +** records to the value passed as the second argument. +** +** Return SQLITE_OK if successful, or an SQLite error code if an error +** occurs. +*/ +int sqlite3Fts5IndexSetCookie(Fts5Index *p, int iNew){ + int rc; /* Return code */ + Fts5Config *pConfig = p->pConfig; /* Configuration object */ + u8 aCookie[4]; /* Binary representation of iNew */ + sqlite3_blob *pBlob = 0; + + assert( p->rc==SQLITE_OK ); + sqlite3Fts5Put32(aCookie, iNew); + + rc = sqlite3_blob_open(pConfig->db, pConfig->zDb, p->zDataTbl, + "block", FTS5_STRUCTURE_ROWID, 1, &pBlob + ); + if( rc==SQLITE_OK ){ + sqlite3_blob_write(pBlob, aCookie, 4, 0); + rc = sqlite3_blob_close(pBlob); + } + + return rc; +} + +int sqlite3Fts5IndexLoadConfig(Fts5Index *p){ + Fts5Structure *pStruct; + pStruct = fts5StructureRead(p); + fts5StructureRelease(pStruct); + return fts5IndexReturn(p); +} + +/* +** Retrieve the origin value that will be used for the segment currently +** being accumulated in the in-memory hash table when it is flushed to +** disk. If successful, SQLITE_OK is returned and (*piOrigin) set to +** the queried value. Or, if an error occurs, an error code is returned +** and the final value of (*piOrigin) is undefined. +*/ +int sqlite3Fts5IndexGetOrigin(Fts5Index *p, i64 *piOrigin){ + Fts5Structure *pStruct; + pStruct = fts5StructureRead(p); + if( pStruct ){ + *piOrigin = pStruct->nOriginCntr; + fts5StructureRelease(pStruct); + } + return fts5IndexReturn(p); +} + +/* +** Buffer pPg contains a page of a tombstone hash table - one of nPg pages +** associated with the same segment. This function adds rowid iRowid to +** the hash table. The caller is required to guarantee that there is at +** least one free slot on the page. +** +** If parameter bForce is false and the hash table is deemed to be full +** (more than half of the slots are occupied), then non-zero is returned +** and iRowid not inserted. Or, if bForce is true or if the hash table page +** is not full, iRowid is inserted and zero returned. +*/ +static int fts5IndexTombstoneAddToPage( + Fts5Data *pPg, + int bForce, + int nPg, + u64 iRowid +){ + const int szKey = TOMBSTONE_KEYSIZE(pPg); + const int nSlot = TOMBSTONE_NSLOT(pPg); + const int nElem = fts5GetU32(&pPg->p[4]); + int iSlot = (iRowid / nPg) % nSlot; + int nCollide = nSlot; + + if( szKey==4 && iRowid>0xFFFFFFFF ) return 2; + if( iRowid==0 ){ + pPg->p[1] = 0x01; + return 0; + } + + if( bForce==0 && nElem>=(nSlot/2) ){ + return 1; + } + + fts5PutU32(&pPg->p[4], nElem+1); + if( szKey==4 ){ + u32 *aSlot = (u32*)&pPg->p[8]; + while( aSlot[iSlot] ){ + iSlot = (iSlot + 1) % nSlot; + if( nCollide--==0 ) return 0; + } + fts5PutU32((u8*)&aSlot[iSlot], (u32)iRowid); + }else{ + u64 *aSlot = (u64*)&pPg->p[8]; + while( aSlot[iSlot] ){ + iSlot = (iSlot + 1) % nSlot; + if( nCollide--==0 ) return 0; + } + fts5PutU64((u8*)&aSlot[iSlot], iRowid); + } + + return 0; +} + +/* +** This function attempts to build a new hash containing all the keys +** currently in the tombstone hash table for segment pSeg. The new +** hash will be stored in the nOut buffers passed in array apOut[]. +** All pages of the new hash use key-size szKey (4 or 8). +** +** Return 0 if the hash is successfully rebuilt into the nOut pages. +** Or non-zero if it is not (because one page became overfull). In this +** case the caller should retry with a larger nOut parameter. +** +** Parameter pData1 is page iPg1 of the hash table being rebuilt. +*/ +static int fts5IndexTombstoneRehash( + Fts5Index *p, + Fts5StructureSegment *pSeg, /* Segment to rebuild hash of */ + Fts5Data *pData1, /* One page of current hash - or NULL */ + int iPg1, /* Which page of the current hash is pData1 */ + int szKey, /* 4 or 8, the keysize */ + int nOut, /* Number of output pages */ + Fts5Data **apOut /* Array of output hash pages */ +){ + int ii; + int res = 0; + + /* Initialize the headers of all the output pages */ + for(ii=0; iip[0] = szKey; + fts5PutU32(&apOut[ii]->p[4], 0); + } + + /* Loop through the current pages of the hash table. */ + for(ii=0; res==0 && iinPgTombstone; ii++){ + Fts5Data *pData = 0; /* Page ii of the current hash table */ + Fts5Data *pFree = 0; /* Free this at the end of the loop */ + + if( iPg1==ii ){ + pData = pData1; + }else{ + pFree = pData = fts5DataRead(p, FTS5_TOMBSTONE_ROWID(pSeg->iSegid, ii)); + } + + if( pData ){ + int szKeyIn = TOMBSTONE_KEYSIZE(pData); + int nSlotIn = (pData->nn - 8) / szKeyIn; + int iIn; + for(iIn=0; iInp[8]; + if( aSlot[iIn] ) iVal = fts5GetU32((u8*)&aSlot[iIn]); + }else{ + u64 *aSlot = (u64*)&pData->p[8]; + if( aSlot[iIn] ) iVal = fts5GetU64((u8*)&aSlot[iIn]); + } + + /* If iVal is not 0 at this point, insert it into the new hash table */ + if( iVal ){ + Fts5Data *pPg = apOut[(iVal % nOut)]; + res = fts5IndexTombstoneAddToPage(pPg, 0, nOut, iVal); + if( res ) break; + } + } + + /* If this is page 0 of the old hash, copy the rowid-0-flag from the + ** old hash to the new. */ + if( ii==0 ){ + apOut[0]->p[1] = pData->p[1]; + } + } + fts5DataRelease(pFree); + } + + return res; +} + +/* +** This is called to rebuild the hash table belonging to segment pSeg. +** If parameter pData1 is not NULL, then one page of the existing hash table +** has already been loaded - pData1, which is page iPg1. The key-size for +** the new hash table is szKey (4 or 8). +** +** If successful, the new hash table is not written to disk. Instead, +** output parameter (*pnOut) is set to the number of pages in the new +** hash table, and (*papOut) to point to an array of buffers containing +** the new page data. +** +** If an error occurs, an error code is left in the Fts5Index object and +** both output parameters set to 0 before returning. +*/ +static void fts5IndexTombstoneRebuild( + Fts5Index *p, + Fts5StructureSegment *pSeg, /* Segment to rebuild hash of */ + Fts5Data *pData1, /* One page of current hash - or NULL */ + int iPg1, /* Which page of the current hash is pData1 */ + int szKey, /* 4 or 8, the keysize */ + int *pnOut, /* OUT: Number of output pages */ + Fts5Data ***papOut /* OUT: Output hash pages */ +){ + const int MINSLOT = 32; + int nSlotPerPage = MAX(MINSLOT, (p->pConfig->pgsz - 8) / szKey); + int nSlot = 0; /* Number of slots in each output page */ + int nOut = 0; + + /* Figure out how many output pages (nOut) and how many slots per + ** page (nSlot). There are three possibilities: + ** + ** 1. The hash table does not yet exist. In this case the new hash + ** table will consist of a single page with MINSLOT slots. + ** + ** 2. The hash table exists but is currently a single page. In this + ** case an attempt is made to grow the page to accommodate the new + ** entry. The page is allowed to grow up to nSlotPerPage (see above) + ** slots. + ** + ** 3. The hash table already consists of more than one page, or of + ** a single page already so large that it cannot be grown. In this + ** case the new hash consists of (nPg*2+1) pages of nSlotPerPage + ** slots each, where nPg is the current number of pages in the + ** hash table. + */ + if( pSeg->nPgTombstone==0 ){ + /* Case 1. */ + nOut = 1; + nSlot = MINSLOT; + }else if( pSeg->nPgTombstone==1 ){ + /* Case 2. */ + int nElem = (int)fts5GetU32(&pData1->p[4]); + assert( pData1 && iPg1==0 ); + nOut = 1; + nSlot = MAX(nElem*4, MINSLOT); + if( nSlot>nSlotPerPage ) nOut = 0; + } + if( nOut==0 ){ + /* Case 3. */ + nOut = (pSeg->nPgTombstone * 2 + 1); + nSlot = nSlotPerPage; + } + + /* Allocate the required array and output pages */ + while( 1 ){ + int res = 0; + int ii = 0; + int szPage = 0; + Fts5Data **apOut = 0; + + /* Allocate space for the new hash table */ + assert( nSlot>=MINSLOT ); + apOut = (Fts5Data**)sqlite3Fts5MallocZero(&p->rc, sizeof(Fts5Data*) * nOut); + szPage = 8 + nSlot*szKey; + for(ii=0; iirc, + sizeof(Fts5Data)+szPage + ); + if( pNew ){ + pNew->nn = szPage; + pNew->p = (u8*)&pNew[1]; + apOut[ii] = pNew; + } + } + + /* Rebuild the hash table. */ + if( p->rc==SQLITE_OK ){ + res = fts5IndexTombstoneRehash(p, pSeg, pData1, iPg1, szKey, nOut, apOut); + } + if( res==0 ){ + if( p->rc ){ + fts5IndexFreeArray(apOut, nOut); + apOut = 0; + nOut = 0; + } + *pnOut = nOut; + *papOut = apOut; + break; + } + + /* If control flows to here, it was not possible to rebuild the hash + ** table. Free all buffers and then try again with more pages. */ + assert( p->rc==SQLITE_OK ); + fts5IndexFreeArray(apOut, nOut); + nSlot = nSlotPerPage; + nOut = nOut*2 + 1; + } } -/* -** Replace the current "averages" record with the contents of the buffer -** supplied as the second argument. -*/ -int sqlite3Fts5IndexSetAverages(Fts5Index *p, const u8 *pData, int nData){ - assert( p->rc==SQLITE_OK ); - fts5DataWrite(p, FTS5_AVERAGES_ROWID, pData, nData); - return fts5IndexReturn(p); -} /* -** Return the total number of blocks this module has read from the %_data -** table since it was created. +** Add a tombstone for rowid iRowid to segment pSeg. */ -int sqlite3Fts5IndexReads(Fts5Index *p){ - return p->nRead; -} +static void fts5IndexTombstoneAdd( + Fts5Index *p, + Fts5StructureSegment *pSeg, + u64 iRowid +){ + Fts5Data *pPg = 0; + int iPg = -1; + int szKey = 0; + int nHash = 0; + Fts5Data **apHash = 0; + + p->nContentlessDelete++; + + if( pSeg->nPgTombstone>0 ){ + iPg = iRowid % pSeg->nPgTombstone; + pPg = fts5DataRead(p, FTS5_TOMBSTONE_ROWID(pSeg->iSegid,iPg)); + if( pPg==0 ){ + assert( p->rc!=SQLITE_OK ); + return; + } -/* -** Set the 32-bit cookie value stored at the start of all structure -** records to the value passed as the second argument. -** -** Return SQLITE_OK if successful, or an SQLite error code if an error -** occurs. -*/ -int sqlite3Fts5IndexSetCookie(Fts5Index *p, int iNew){ - int rc; /* Return code */ - Fts5Config *pConfig = p->pConfig; /* Configuration object */ - u8 aCookie[4]; /* Binary representation of iNew */ - sqlite3_blob *pBlob = 0; + if( 0==fts5IndexTombstoneAddToPage(pPg, 0, pSeg->nPgTombstone, iRowid) ){ + fts5DataWrite(p, FTS5_TOMBSTONE_ROWID(pSeg->iSegid,iPg), pPg->p, pPg->nn); + fts5DataRelease(pPg); + return; + } + } - assert( p->rc==SQLITE_OK ); - sqlite3Fts5Put32(aCookie, iNew); + /* Have to rebuild the hash table. First figure out the key-size (4 or 8). */ + szKey = pPg ? TOMBSTONE_KEYSIZE(pPg) : 4; + if( iRowid>0xFFFFFFFF ) szKey = 8; - rc = sqlite3_blob_open(pConfig->db, pConfig->zDb, p->zDataTbl, - "block", FTS5_STRUCTURE_ROWID, 1, &pBlob - ); - if( rc==SQLITE_OK ){ - sqlite3_blob_write(pBlob, aCookie, 4, 0); - rc = sqlite3_blob_close(pBlob); + /* Rebuild the hash table */ + fts5IndexTombstoneRebuild(p, pSeg, pPg, iPg, szKey, &nHash, &apHash); + assert( p->rc==SQLITE_OK || (nHash==0 && apHash==0) ); + + /* If all has succeeded, write the new rowid into one of the new hash + ** table pages, then write them all out to disk. */ + if( nHash ){ + int ii = 0; + fts5IndexTombstoneAddToPage(apHash[iRowid % nHash], 1, nHash, iRowid); + for(ii=0; iiiSegid, ii); + fts5DataWrite(p, iTombstoneRowid, apHash[ii]->p, apHash[ii]->nn); + } + pSeg->nPgTombstone = nHash; + fts5StructureWrite(p, p->pStruct); } - return rc; + fts5DataRelease(pPg); + fts5IndexFreeArray(apHash, nHash); } -int sqlite3Fts5IndexLoadConfig(Fts5Index *p){ +/* +** Add iRowid to the tombstone list of the segment or segments that contain +** rows from origin iOrigin. Return SQLITE_OK if successful, or an SQLite +** error code otherwise. +*/ +int sqlite3Fts5IndexContentlessDelete(Fts5Index *p, i64 iOrigin, i64 iRowid){ Fts5Structure *pStruct; pStruct = fts5StructureRead(p); - fts5StructureRelease(pStruct); + if( pStruct ){ + int bFound = 0; /* True after pSeg->nEntryTombstone incr. */ + int iLvl; + for(iLvl=pStruct->nLevel-1; iLvl>=0; iLvl--){ + int iSeg; + for(iSeg=pStruct->aLevel[iLvl].nSeg-1; iSeg>=0; iSeg--){ + Fts5StructureSegment *pSeg = &pStruct->aLevel[iLvl].aSeg[iSeg]; + if( pSeg->iOrigin1<=(u64)iOrigin && pSeg->iOrigin2>=(u64)iOrigin ){ + if( bFound==0 ){ + pSeg->nEntryTombstone++; + bFound = 1; + } + fts5IndexTombstoneAdd(p, pSeg, iRowid); + } + } + } + fts5StructureRelease(pStruct); + } return fts5IndexReturn(p); } - /************************************************************************* ************************************************************************** ** Below this point is the implementation of the integrity-check @@ -5419,9 +8225,11 @@ static int fts5QueryCksum( int eDetail = p->pConfig->eDetail; u64 cksum = *pCksum; Fts5IndexIter *pIter = 0; - int rc = sqlite3Fts5IndexQuery(p, z, n, flags, 0, &pIter); + int rc = sqlite3Fts5IndexQuery( + p, z, n, (flags | FTS5INDEX_QUERY_NOTOKENDATA), 0, &pIter + ); - while( rc==SQLITE_OK && 0==sqlite3Fts5IterEof(pIter) ){ + while( rc==SQLITE_OK && ALWAYS(pIter!=0) && 0==sqlite3Fts5IterEof(pIter) ){ i64 rowid = pIter->iRowid; if( eDetail==FTS5_DETAIL_NONE ){ @@ -5441,29 +8249,68 @@ static int fts5QueryCksum( rc = sqlite3Fts5IterNext(pIter); } } - sqlite3Fts5IterClose(pIter); + fts5IterClose(pIter); *pCksum = cksum; return rc; } +/* +** Check if buffer z[], size n bytes, contains as series of valid utf-8 +** encoded codepoints. If so, return 0. Otherwise, if the buffer does not +** contain valid utf-8, return non-zero. +*/ +static int fts5TestUtf8(const char *z, int n){ + int i = 0; + assert_nc( n>0 ); + while( i=n || (z[i+1] & 0xC0)!=0x80 ) return 1; + i += 2; + }else + if( (z[i] & 0xF0)==0xE0 ){ + if( i+2>=n || (z[i+1] & 0xC0)!=0x80 || (z[i+2] & 0xC0)!=0x80 ) return 1; + i += 3; + }else + if( (z[i] & 0xF8)==0xF0 ){ + if( i+3>=n || (z[i+1] & 0xC0)!=0x80 || (z[i+2] & 0xC0)!=0x80 ) return 1; + if( (z[i+2] & 0xC0)!=0x80 ) return 1; + i += 3; + }else{ + return 1; + } + } + + return 0; +} /* ** This function is also purely an internal test. It does not contribute to ** FTS functionality, or even the integrity-check, in any way. +** +** This function sets output variable (*pbFail) to true if the test fails. Or +** leaves it unchanged if the test succeeds. */ static void fts5TestTerm( Fts5Index *p, Fts5Buffer *pPrev, /* Previous term */ const char *z, int n, /* Possibly new term to test */ u64 expected, - u64 *pCksum + u64 *pCksum, + int *pbFail ){ int rc = p->rc; if( pPrev->n==0 ){ fts5BufferSet(&rc, pPrev, n, (const u8*)z); }else - if( rc==SQLITE_OK && (pPrev->n!=n || memcmp(pPrev->p, z, n)) ){ + if( *pbFail==0 + && rc==SQLITE_OK + && (pPrev->n!=n || memcmp(pPrev->p, z, n)) + && (p->pHash==0 || p->pHash->nEntry==0) + ){ u64 cksum3 = *pCksum; const char *zTerm = (const char*)&pPrev->p[1]; /* term sans prefix-byte */ int nTerm = pPrev->n-1; /* Size of zTerm in bytes */ @@ -5487,8 +8334,14 @@ static void fts5TestTerm( ** This check may only be performed if the hash table is empty. This ** is because the hash table only supports a single scan query at ** a time, and the multi-iter loop from which this function is called - ** is already performing such a scan. */ - if( p->nPendingData==0 ){ + ** is already performing such a scan. + ** + ** Also only do this if buffer zTerm contains nTerm bytes of valid + ** utf-8. Otherwise, the last part of the buffer contents might contain + ** a non-utf-8 sequence that happens to be a prefix of a valid utf-8 + ** character stored in the main fts index, which will cause the + ** test to fail. */ + if( p->nPendingData==0 && 0==fts5TestUtf8(zTerm, nTerm) ){ if( iIdx>0 && rc==SQLITE_OK ){ int f = flags|FTS5INDEX_QUERY_TEST_NOIDX; ck2 = 0; @@ -5507,7 +8360,7 @@ static void fts5TestTerm( fts5BufferSet(&rc, pPrev, n, (const u8*)z); if( rc==SQLITE_OK && cksum3!=expected ){ - rc = FTS5_CORRUPT; + *pbFail = 1; } *pCksum = cksum3; } @@ -5516,7 +8369,7 @@ static void fts5TestTerm( #else # define fts5TestDlidxReverse(x,y,z) -# define fts5TestTerm(u,v,w,x,y,z) +# define fts5TestTerm(t,u,v,w,x,y,z) #endif /* @@ -5541,15 +8394,18 @@ static void fts5IndexIntegrityCheckEmpty( for(i=iFirst; p->rc==SQLITE_OK && i<=iLast; i++){ Fts5Data *pLeaf = fts5DataRead(p, FTS5_SEGMENT_ROWID(pSeg->iSegid, i)); if( pLeaf ){ - if( !fts5LeafIsTermless(pLeaf) ) p->rc = FTS5_CORRUPT; - if( i>=iNoRowid && 0!=fts5LeafFirstRowidOff(pLeaf) ) p->rc = FTS5_CORRUPT; + if( !fts5LeafIsTermless(pLeaf) + || (i>=iNoRowid && 0!=fts5LeafFirstRowidOff(pLeaf)) + ){ + FTS5_CORRUPT_ROWID(p, FTS5_SEGMENT_ROWID(pSeg->iSegid, i)); + } } fts5DataRelease(pLeaf); } } -static void fts5IntegrityCheckPgidx(Fts5Index *p, Fts5Data *pLeaf){ - int iTermOff = 0; +static void fts5IntegrityCheckPgidx(Fts5Index *p, i64 iRowid, Fts5Data *pLeaf){ + i64 iTermOff = 0; int ii; Fts5Buffer buf1 = {0,0,0}; @@ -5558,7 +8414,7 @@ static void fts5IntegrityCheckPgidx(Fts5Index *p, Fts5Data *pLeaf){ ii = pLeaf->szLeaf; while( iinn && p->rc==SQLITE_OK ){ int res; - int iOff; + i64 iOff; int nIncr; ii += fts5GetVarint32(&pLeaf->p[ii], nIncr); @@ -5566,12 +8422,12 @@ static void fts5IntegrityCheckPgidx(Fts5Index *p, Fts5Data *pLeaf){ iOff = iTermOff; if( iOff>=pLeaf->szLeaf ){ - p->rc = FTS5_CORRUPT; + FTS5_CORRUPT_ROWID(p, iRowid); }else if( iTermOff==nIncr ){ int nByte; iOff += fts5GetVarint32(&pLeaf->p[iOff], nByte); if( (iOff+nByte)>pLeaf->szLeaf ){ - p->rc = FTS5_CORRUPT; + FTS5_CORRUPT_ROWID(p, iRowid); }else{ fts5BufferSet(&p->rc, &buf1, nByte, &pLeaf->p[iOff]); } @@ -5580,7 +8436,7 @@ static void fts5IntegrityCheckPgidx(Fts5Index *p, Fts5Data *pLeaf){ iOff += fts5GetVarint32(&pLeaf->p[iOff], nKeep); iOff += fts5GetVarint32(&pLeaf->p[iOff], nByte); if( nKeep>buf1.n || (iOff+nByte)>pLeaf->szLeaf ){ - p->rc = FTS5_CORRUPT; + FTS5_CORRUPT_ROWID(p, iRowid); }else{ buf1.n = nKeep; fts5BufferAppendBlob(&p->rc, &buf1, nByte, &pLeaf->p[iOff]); @@ -5588,7 +8444,7 @@ static void fts5IntegrityCheckPgidx(Fts5Index *p, Fts5Data *pLeaf){ if( p->rc==SQLITE_OK ){ res = fts5BufferCompare(&buf1, &buf2); - if( res<=0 ) p->rc = FTS5_CORRUPT; + if( res<=0 ) FTS5_CORRUPT_ROWID(p, iRowid); } } fts5BufferSet(&p->rc, &buf2, buf1.n, buf1.p); @@ -5603,6 +8459,7 @@ static void fts5IndexIntegrityCheckSegment( Fts5StructureSegment *pSeg /* Segment to check internal consistency */ ){ Fts5Config *pConfig = p->pConfig; + int bSecureDelete = (pConfig->iVersion==FTS5_CURRENT_VERSION_SECUREDELETE); sqlite3_stmt *pStmt = 0; int rc2; int iIdxPrevLeaf = pSeg->pgnoFirst-1; @@ -5611,7 +8468,8 @@ static void fts5IndexIntegrityCheckSegment( if( pSeg->pgnoFirst==0 ) return; fts5IndexPrepareStmt(p, &pStmt, sqlite3_mprintf( - "SELECT segid, term, (pgno>>1), (pgno&1) FROM %Q.'%q_idx' WHERE segid=%d", + "SELECT segid, term, (pgno>>1), (pgno&1) FROM %Q.'%q_idx' WHERE segid=%d " + "ORDER BY 1, 2", pConfig->zDb, pConfig->zName, pSeg->iSegid )); @@ -5620,8 +8478,8 @@ static void fts5IndexIntegrityCheckSegment( i64 iRow; /* Rowid for this leaf */ Fts5Data *pLeaf; /* Data for this leaf */ + const char *zIdxTerm = (const char*)sqlite3_column_blob(pStmt, 1); int nIdxTerm = sqlite3_column_bytes(pStmt, 1); - const char *zIdxTerm = (const char*)sqlite3_column_text(pStmt, 1); int iIdxLeaf = sqlite3_column_int(pStmt, 2); int bIdxDlidx = sqlite3_column_int(pStmt, 3); @@ -5629,7 +8487,7 @@ static void fts5IndexIntegrityCheckSegment( ** ignore this b-tree entry. Otherwise, load it into memory. */ if( iIdxLeafpgnoFirst ) continue; iRow = FTS5_SEGMENT_ROWID(pSeg->iSegid, iIdxLeaf); - pLeaf = fts5DataRead(p, iRow); + pLeaf = fts5LeafRead(p, iRow); if( pLeaf==0 ) break; /* Check that the leaf contains at least one term, and that it is equal @@ -5637,7 +8495,19 @@ static void fts5IndexIntegrityCheckSegment( ** is also a rowid pointer within the leaf page header, it points to a ** location before the term. */ if( pLeaf->nn<=pLeaf->szLeaf ){ - p->rc = FTS5_CORRUPT; + + if( nIdxTerm==0 + && pConfig->iVersion==FTS5_CURRENT_VERSION_SECUREDELETE + && pLeaf->nn==pLeaf->szLeaf + && pLeaf->nn==4 + ){ + /* special case - the very first page in a segment keeps its %_idx + ** entry even if all the terms are removed from it by secure-delete + ** operations. */ + }else{ + FTS5_CORRUPT_ROWID(p, iRow); + } + }else{ int iOff; /* Offset of first term on leaf */ int iRowidOff; /* Offset of first rowid on leaf */ @@ -5646,16 +8516,16 @@ static void fts5IndexIntegrityCheckSegment( iOff = fts5LeafFirstTermOff(pLeaf); iRowidOff = fts5LeafFirstRowidOff(pLeaf); - if( iRowidOff>=iOff ){ - p->rc = FTS5_CORRUPT; + if( iRowidOff>=iOff || iOff>=pLeaf->szLeaf ){ + FTS5_CORRUPT_ROWID(p, iRow); }else{ iOff += fts5GetVarint32(&pLeaf->p[iOff], nTerm); - res = memcmp(&pLeaf->p[iOff], zIdxTerm, MIN(nTerm, nIdxTerm)); + res = fts5Memcmp(&pLeaf->p[iOff], zIdxTerm, MIN(nTerm, nIdxTerm)); if( res==0 ) res = nTerm - nIdxTerm; - if( res<0 ) p->rc = FTS5_CORRUPT; + if( res<0 ) FTS5_CORRUPT_ROWID(p, iRow); } - fts5IntegrityCheckPgidx(p, pLeaf); + fts5IntegrityCheckPgidx(p, iRow, pLeaf); } fts5DataRelease(pLeaf); if( p->rc ) break; @@ -5685,7 +8555,7 @@ static void fts5IndexIntegrityCheckSegment( iKey = FTS5_SEGMENT_ROWID(iSegid, iPg); pLeaf = fts5DataRead(p, iKey); if( pLeaf ){ - if( fts5LeafFirstRowidOff(pLeaf)!=0 ) p->rc = FTS5_CORRUPT; + if( fts5LeafFirstRowidOff(pLeaf)!=0 ) FTS5_CORRUPT_ROWID(p, iKey); fts5DataRelease(pLeaf); } } @@ -5700,10 +8570,13 @@ static void fts5IndexIntegrityCheckSegment( int iRowidOff = fts5LeafFirstRowidOff(pLeaf); ASSERT_SZLEAF_OK(pLeaf); if( iRowidOff>=pLeaf->szLeaf ){ - p->rc = FTS5_CORRUPT; - }else{ + FTS5_CORRUPT_ROWID(p, iKey); + }else if( bSecureDelete==0 || iRowidOff>0 ){ + i64 iDlRowid = fts5DlidxIterRowid(pDlidx); fts5GetVarint(&pLeaf->p[iRowidOff], (u64*)&iRowid); - if( iRowid!=fts5DlidxIterRowid(pDlidx) ) p->rc = FTS5_CORRUPT; + if( iRowidpConfig->eDetail; u64 cksum2 = 0; /* Checksum based on contents of indexes */ Fts5Buffer poslist = {0,0,0}; /* Buffer used to hold a poslist */ Fts5Iter *pIter; /* Used to iterate through entire index */ Fts5Structure *pStruct; /* Index structure */ + int iLvl, iSeg; #ifdef SQLITE_DEBUG /* Used by extra internal tests only run if NDEBUG is not defined */ u64 cksum3 = 0; /* Checksum based on contents of indexes */ Fts5Buffer term = {0,0,0}; /* Buffer used to hold most recent term */ + int bTestFail = 0; #endif const int flags = FTS5INDEX_QUERY_NOOUTPUT; /* Load the FTS index structure */ pStruct = fts5StructureRead(p); + if( pStruct==0 ){ + assert( p->rc!=SQLITE_OK ); + return fts5IndexReturn(p); + } /* Check that the internal nodes of each segment match the leaves */ - if( pStruct ){ - int iLvl, iSeg; - for(iLvl=0; iLvlnLevel; iLvl++){ - for(iSeg=0; iSegaLevel[iLvl].nSeg; iSeg++){ - Fts5StructureSegment *pSeg = &pStruct->aLevel[iLvl].aSeg[iSeg]; - fts5IndexIntegrityCheckSegment(p, pSeg); - } + for(iLvl=0; iLvlnLevel; iLvl++){ + for(iSeg=0; iSegaLevel[iLvl].nSeg; iSeg++){ + Fts5StructureSegment *pSeg = &pStruct->aLevel[iLvl].aSeg[iSeg]; + fts5IndexIntegrityCheckSegment(p, pSeg); } } @@ -5794,7 +8670,8 @@ int sqlite3Fts5IndexIntegrityCheck(Fts5Index *p, u64 cksum){ char *z = (char*)fts5MultiIterTerm(pIter, &n); /* If this is a new term, query for it. Update cksum3 with the results. */ - fts5TestTerm(p, &term, z, n, cksum2, &cksum3); + fts5TestTerm(p, &term, z, n, cksum2, &cksum3, &bTestFail); + if( p->rc ) break; if( eDetail==FTS5_DETAIL_NONE ){ if( 0==fts5MultiIterIsEmpty(p, pIter) ){ @@ -5803,6 +8680,7 @@ int sqlite3Fts5IndexIntegrityCheck(Fts5Index *p, u64 cksum){ }else{ poslist.n = 0; fts5SegiterPoslist(p, &pIter->aSeg[pIter->aFirst[1].iFirst], 0, &poslist); + fts5BufferAppendBlob(&p->rc, &poslist, 4, (const u8*)"\0\0\0\0"); while( 0==sqlite3Fts5PoslistNext64(poslist.p, poslist.n, &iOff, &iPos) ){ int iCol = FTS5_POS2COLUMN(iPos); int iTokOff = FTS5_POS2OFFSET(iPos); @@ -5810,15 +8688,26 @@ int sqlite3Fts5IndexIntegrityCheck(Fts5Index *p, u64 cksum){ } } } - fts5TestTerm(p, &term, 0, 0, cksum2, &cksum3); + fts5TestTerm(p, &term, 0, 0, cksum2, &cksum3, &bTestFail); fts5MultiIterFree(pIter); - if( p->rc==SQLITE_OK && cksum!=cksum2 ) p->rc = FTS5_CORRUPT; - - fts5StructureRelease(pStruct); + if( p->rc==SQLITE_OK && bUseCksum && cksum!=cksum2 ){ + p->rc = FTS5_CORRUPT; + sqlite3Fts5ConfigErrmsg(p->pConfig, + "fts5: checksum mismatch for table \"%s\"", p->pConfig->zName + ); + } #ifdef SQLITE_DEBUG + /* In SQLITE_DEBUG builds, expensive extra checks were run as part of + ** the integrity-check above. If no other errors were detected, but one + ** of these tests failed, set the result to SQLITE_CORRUPT_VTAB here. */ + if( p->rc==SQLITE_OK && bTestFail ){ + p->rc = FTS5_CORRUPT; + } fts5BufferFree(&term); #endif + + fts5StructureRelease(pStruct); fts5BufferFree(&poslist); return fts5IndexReturn(p); } @@ -5829,12 +8718,14 @@ int sqlite3Fts5IndexIntegrityCheck(Fts5Index *p, u64 cksum){ ** function only. */ +#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG) /* ** Decode a segment-data rowid from the %_data table. This function is ** the opposite of macro FTS5_SEGMENT_ROWID(). */ static void fts5DecodeRowid( i64 iRowid, /* Rowid from %_data table */ + int *pbTombstone, /* OUT: Tombstone hash flag */ int *piSegid, /* OUT: Segment id */ int *pbDlidx, /* OUT: Dlidx flag */ int *piHeight, /* OUT: Height */ @@ -5850,11 +8741,16 @@ static void fts5DecodeRowid( iRowid >>= FTS5_DATA_DLI_B; *piSegid = (int)(iRowid & (((i64)1 << FTS5_DATA_ID_B) - 1)); + iRowid >>= FTS5_DATA_ID_B; + + *pbTombstone = (int)(iRowid & 0x0001); } +#endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */ +#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG) static void fts5DebugRowid(int *pRc, Fts5Buffer *pBuf, i64 iKey){ - int iSegid, iHeight, iPgno, bDlidx; /* Rowid compenents */ - fts5DecodeRowid(iKey, &iSegid, &bDlidx, &iHeight, &iPgno); + int iSegid, iHeight, iPgno, bDlidx, bTomb; /* Rowid components */ + fts5DecodeRowid(iKey, &bTomb, &iSegid, &bDlidx, &iHeight, &iPgno); if( iSegid==0 ){ if( iKey==FTS5_AVERAGES_ROWID ){ @@ -5864,12 +8760,16 @@ static void fts5DebugRowid(int *pRc, Fts5Buffer *pBuf, i64 iKey){ } } else{ - sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "{%ssegid=%d h=%d pgno=%d}", - bDlidx ? "dlidx " : "", iSegid, iHeight, iPgno + sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "{%s%ssegid=%d h=%d pgno=%d}", + bDlidx ? "dlidx " : "", + bTomb ? "tombstone " : "", + iSegid, iHeight, iPgno ); } } +#endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */ +#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG) static void fts5DebugStructure( int *pRc, /* IN/OUT: error code */ Fts5Buffer *pBuf, @@ -5884,14 +8784,22 @@ static void fts5DebugStructure( ); for(iSeg=0; iSegnSeg; iSeg++){ Fts5StructureSegment *pSeg = &pLvl->aSeg[iSeg]; - sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " {id=%d leaves=%d..%d}", + sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " {id=%d leaves=%d..%d", pSeg->iSegid, pSeg->pgnoFirst, pSeg->pgnoLast ); + if( pSeg->iOrigin1>0 ){ + sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " origin=%lld..%lld", + pSeg->iOrigin1, pSeg->iOrigin2 + ); + } + sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "}"); } sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "}"); } } +#endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */ +#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG) /* ** This is part of the fts5_decode() debugging aid. ** @@ -5916,7 +8824,9 @@ static void fts5DecodeStructure( fts5DebugStructure(pRc, pBuf, p); fts5StructureRelease(p); } +#endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */ +#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG) /* ** This is part of the fts5_decode() debugging aid. ** @@ -5939,7 +8849,9 @@ static void fts5DecodeAverages( zSpace = " "; } } +#endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */ +#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG) /* ** Buffer (a/n) is assumed to contain a list of serialized varints. Read ** each varint and append its string representation to buffer pBuf. Return @@ -5956,7 +8868,9 @@ static int fts5DecodePoslist(int *pRc, Fts5Buffer *pBuf, const u8 *a, int n){ } return iOff; } +#endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */ +#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG) /* ** The start of buffer (a/n) contains the start of a doclist. The doclist ** may or may not finish within the buffer. This function appends a text @@ -5989,7 +8903,9 @@ static int fts5DecodeDoclist(int *pRc, Fts5Buffer *pBuf, const u8 *a, int n){ return iOff; } +#endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */ +#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG) /* ** This function is part of the fts5_decode() debugging function. It is ** only ever used with detail=none tables. @@ -6030,7 +8946,27 @@ static void fts5DecodeRowidList( sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " %lld%s", iRowid, zApp); } } +#endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */ +#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG) +static void fts5BufferAppendTerm(int *pRc, Fts5Buffer *pBuf, Fts5Buffer *pTerm){ + int ii; + fts5BufferGrow(pRc, pBuf, pTerm->n*2 + 1); + if( *pRc==SQLITE_OK ){ + for(ii=0; iin; ii++){ + if( pTerm->p[ii]==0x00 ){ + pBuf->p[pBuf->n++] = '\\'; + pBuf->p[pBuf->n++] = '0'; + }else{ + pBuf->p[pBuf->n++] = pTerm->p[ii]; + } + } + pBuf->p[pBuf->n] = 0x00; + } +} +#endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */ + +#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG) /* ** The implementation of user-defined scalar function fts5_decode(). */ @@ -6041,11 +8977,12 @@ static void fts5DecodeFunction( ){ i64 iRowid; /* Rowid for record being decoded */ int iSegid,iHeight,iPgno,bDlidx;/* Rowid components */ + int bTomb; const u8 *aBlob; int n; /* Record to decode */ u8 *a = 0; Fts5Buffer s; /* Build up text to return here */ int rc = SQLITE_OK; /* Return code */ - int nSpace = 0; + sqlite3_int64 nSpace = 0; int eDetailNone = (sqlite3_user_data(pCtx)!=0); assert( nArg==2 ); @@ -6058,13 +8995,12 @@ static void fts5DecodeFunction( ** buffer overreads even if the record is corrupt. */ n = sqlite3_value_bytes(apVal[1]); aBlob = sqlite3_value_blob(apVal[1]); - nSpace = n + FTS5_DATA_ZERO_PADDING; + nSpace = ((i64)n) + FTS5_DATA_ZERO_PADDING; a = (u8*)sqlite3Fts5MallocZero(&rc, nSpace); if( a==0 ) goto decode_out; - memcpy(a, aBlob, n); + if( n>0 ) memcpy(a, aBlob, n); - - fts5DecodeRowid(iRowid, &iSegid, &bDlidx, &iHeight, &iPgno); + fts5DecodeRowid(iRowid, &bTomb, &iSegid, &bDlidx, &iHeight, &iPgno); fts5DebugRowid(&rc, &s, iRowid); if( bDlidx ){ @@ -6083,6 +9019,28 @@ static void fts5DecodeFunction( " %d(%lld)", lvl.iLeafPgno, lvl.iRowid ); } + }else if( bTomb ){ + u32 nElem = fts5GetU32(&a[4]); + int szKey = (aBlob[0]==4 || aBlob[0]==8) ? aBlob[0] : 8; + int nSlot = (n - 8) / szKey; + int ii; + sqlite3Fts5BufferAppendPrintf(&rc, &s, " nElem=%d", (int)nElem); + if( aBlob[1] ){ + sqlite3Fts5BufferAppendPrintf(&rc, &s, " 0"); + } + for(ii=0; iiestimatedCost = (double)100; + pIdxInfo->estimatedRows = 100; + pIdxInfo->idxNum = 0; + for(i=0, p=pIdxInfo->aConstraint; inConstraint; i++, p++){ + if( p->usable==0 ) continue; + if( p->op==SQLITE_INDEX_CONSTRAINT_EQ && p->iColumn==11 ){ + rc = SQLITE_OK; + pIdxInfo->aConstraintUsage[i].omit = 1; + pIdxInfo->aConstraintUsage[i].argvIndex = 1; + break; + } + } + return rc; +} + +/* +** This method is the destructor for bytecodevtab objects. +*/ +static int fts5structDisconnectMethod(sqlite3_vtab *pVtab){ + Fts5StructVtab *p = (Fts5StructVtab*)pVtab; + sqlite3_free(p); + return SQLITE_OK; +} + +/* +** Constructor for a new bytecodevtab_cursor object. +*/ +static int fts5structOpenMethod(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCsr){ + int rc = SQLITE_OK; + Fts5StructVcsr *pNew = 0; + + pNew = sqlite3Fts5MallocZero(&rc, sizeof(*pNew)); + *ppCsr = (sqlite3_vtab_cursor*)pNew; + + return SQLITE_OK; +} + +/* +** Destructor for a bytecodevtab_cursor. +*/ +static int fts5structCloseMethod(sqlite3_vtab_cursor *cur){ + Fts5StructVcsr *pCsr = (Fts5StructVcsr*)cur; + fts5StructureRelease(pCsr->pStruct); + sqlite3_free(pCsr); + return SQLITE_OK; +} + + +/* +** Advance a bytecodevtab_cursor to its next row of output. +*/ +static int fts5structNextMethod(sqlite3_vtab_cursor *cur){ + Fts5StructVcsr *pCsr = (Fts5StructVcsr*)cur; + Fts5Structure *p = pCsr->pStruct; + + assert( pCsr->pStruct ); + pCsr->iSeg++; + pCsr->iRowid++; + while( pCsr->iLevelnLevel && pCsr->iSeg>=p->aLevel[pCsr->iLevel].nSeg ){ + pCsr->iLevel++; + pCsr->iSeg = 0; + } + if( pCsr->iLevel>=p->nLevel ){ + fts5StructureRelease(pCsr->pStruct); + pCsr->pStruct = 0; + } + return SQLITE_OK; +} + +/* +** Return TRUE if the cursor has been moved off of the last +** row of output. +*/ +static int fts5structEofMethod(sqlite3_vtab_cursor *cur){ + Fts5StructVcsr *pCsr = (Fts5StructVcsr*)cur; + return pCsr->pStruct==0; +} + +static int fts5structRowidMethod( + sqlite3_vtab_cursor *cur, + sqlite_int64 *piRowid +){ + Fts5StructVcsr *pCsr = (Fts5StructVcsr*)cur; + *piRowid = pCsr->iRowid; + return SQLITE_OK; +} + +/* +** Return values of columns for the row at which the bytecodevtab_cursor +** is currently pointing. +*/ +static int fts5structColumnMethod( + sqlite3_vtab_cursor *cur, /* The cursor */ + sqlite3_context *ctx, /* First argument to sqlite3_result_...() */ + int i /* Which column to return */ +){ + Fts5StructVcsr *pCsr = (Fts5StructVcsr*)cur; + Fts5Structure *p = pCsr->pStruct; + Fts5StructureSegment *pSeg = &p->aLevel[pCsr->iLevel].aSeg[pCsr->iSeg]; + + switch( i ){ + case 0: /* level */ + sqlite3_result_int(ctx, pCsr->iLevel); + break; + case 1: /* segment */ + sqlite3_result_int(ctx, pCsr->iSeg); + break; + case 2: /* merge */ + sqlite3_result_int(ctx, pCsr->iSeg < p->aLevel[pCsr->iLevel].nMerge); + break; + case 3: /* segid */ + sqlite3_result_int(ctx, pSeg->iSegid); + break; + case 4: /* leaf1 */ + sqlite3_result_int(ctx, pSeg->pgnoFirst); + break; + case 5: /* leaf2 */ + sqlite3_result_int(ctx, pSeg->pgnoLast); + break; + case 6: /* origin1 */ + sqlite3_result_int64(ctx, pSeg->iOrigin1); + break; + case 7: /* origin2 */ + sqlite3_result_int64(ctx, pSeg->iOrigin2); + break; + case 8: /* npgtombstone */ + sqlite3_result_int(ctx, pSeg->nPgTombstone); + break; + case 9: /* nentrytombstone */ + sqlite3_result_int64(ctx, pSeg->nEntryTombstone); + break; + case 10: /* nentry */ + sqlite3_result_int64(ctx, pSeg->nEntry); + break; + } + return SQLITE_OK; +} + +/* +** Initialize a cursor. +** +** idxNum==0 means show all subprograms +** idxNum==1 means show only the main bytecode and omit subprograms. +*/ +static int fts5structFilterMethod( + sqlite3_vtab_cursor *pVtabCursor, + int idxNum, const char *idxStr, + int argc, sqlite3_value **argv +){ + Fts5StructVcsr *pCsr = (Fts5StructVcsr *)pVtabCursor; + int rc = SQLITE_OK; + + const u8 *aBlob = 0; + int nBlob = 0; + + assert( argc==1 ); + fts5StructureRelease(pCsr->pStruct); + pCsr->pStruct = 0; + + nBlob = sqlite3_value_bytes(argv[0]); + aBlob = (const u8*)sqlite3_value_blob(argv[0]); + rc = fts5StructureDecode(aBlob, nBlob, 0, &pCsr->pStruct); + if( rc==SQLITE_OK ){ + pCsr->iLevel = 0; + pCsr->iRowid = 0; + pCsr->iSeg = -1; + rc = fts5structNextMethod(pVtabCursor); + } + + return rc; +} + +#endif /* SQLITE_TEST || SQLITE_FTS5_DEBUG */ /* ** This is called as part of registering the FTS5 module with database @@ -6261,6 +9474,7 @@ static void fts5RowidFunction( ** SQLite error code is returned instead. */ int sqlite3Fts5IndexInit(sqlite3 *db){ +#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG) int rc = sqlite3_create_function( db, "fts5_decode", 2, SQLITE_UTF8, 0, fts5DecodeFunction, 0, 0 ); @@ -6277,5 +9491,49 @@ int sqlite3Fts5IndexInit(sqlite3 *db){ db, "fts5_rowid", -1, SQLITE_UTF8, 0, fts5RowidFunction, 0, 0 ); } + + if( rc==SQLITE_OK ){ + static const sqlite3_module fts5structure_module = { + 0, /* iVersion */ + 0, /* xCreate */ + fts5structConnectMethod, /* xConnect */ + fts5structBestIndexMethod, /* xBestIndex */ + fts5structDisconnectMethod, /* xDisconnect */ + 0, /* xDestroy */ + fts5structOpenMethod, /* xOpen */ + fts5structCloseMethod, /* xClose */ + fts5structFilterMethod, /* xFilter */ + fts5structNextMethod, /* xNext */ + fts5structEofMethod, /* xEof */ + fts5structColumnMethod, /* xColumn */ + fts5structRowidMethod, /* xRowid */ + 0, /* xUpdate */ + 0, /* xBegin */ + 0, /* xSync */ + 0, /* xCommit */ + 0, /* xRollback */ + 0, /* xFindFunction */ + 0, /* xRename */ + 0, /* xSavepoint */ + 0, /* xRelease */ + 0, /* xRollbackTo */ + 0, /* xShadowName */ + 0 /* xIntegrity */ + }; + rc = sqlite3_create_module(db, "fts5_structure", &fts5structure_module, 0); + } return rc; +#else + return SQLITE_OK; + UNUSED_PARAM(db); +#endif +} + + +int sqlite3Fts5IndexReset(Fts5Index *p){ + assert( p->pStruct==0 || p->iStructVersion!=0 ); + if( fts5IndexDataVersion(p)!=p->iStructVersion ){ + fts5StructureInvalidate(p); + } + return fts5IndexReturn(p); } diff --git a/ext/fts5/fts5_main.c b/ext/fts5/fts5_main.c index fe1d72e2b0..f45b9ef906 100644 --- a/ext/fts5/fts5_main.c +++ b/ext/fts5/fts5_main.c @@ -22,14 +22,16 @@ ** assert() conditions in the fts5 code are activated - conditions that are ** only true if it is guaranteed that the fts5 database is not corrupt. */ +#ifdef SQLITE_DEBUG int sqlite3_fts5_may_be_corrupt = 1; +#endif typedef struct Fts5Auxdata Fts5Auxdata; typedef struct Fts5Auxiliary Fts5Auxiliary; typedef struct Fts5Cursor Fts5Cursor; +typedef struct Fts5FullTable Fts5FullTable; typedef struct Fts5Sorter Fts5Sorter; -typedef struct Fts5Table Fts5Table; typedef struct Fts5TokenizerModule Fts5TokenizerModule; /* @@ -81,8 +83,18 @@ struct Fts5Global { Fts5TokenizerModule *pTok; /* First in list of all tokenizer modules */ Fts5TokenizerModule *pDfltTok; /* Default tokenizer module */ Fts5Cursor *pCsr; /* First in list of all open cursors */ + u32 aLocaleHdr[4]; }; +/* +** Size of header on fts5_locale() values. And macro to access a buffer +** containing a copy of the header from an Fts5Config pointer. +*/ +#define FTS5_LOCALE_HDR_SIZE ((int)sizeof( ((Fts5Global*)0)->aLocaleHdr )) +#define FTS5_LOCALE_HDR(pConfig) ((const u8*)(pConfig->pGlobal->aLocaleHdr)) + +#define FTS5_INSTTOKEN_SUBTYPE 73 + /* ** Each auxiliary function registered with the FTS5 module is represented ** by an object of the following type. All such objects are stored as part @@ -101,25 +113,39 @@ struct Fts5Auxiliary { ** Each tokenizer module registered with the FTS5 module is represented ** by an object of the following type. All such objects are stored as part ** of the Fts5Global.pTok list. +** +** bV2Native: +** True if the tokenizer was registered using xCreateTokenizer_v2(), false +** for xCreateTokenizer(). If this variable is true, then x2 is populated +** with the routines as supplied by the caller and x1 contains synthesized +** wrapper routines. In this case the user-data pointer passed to +** x1.xCreate should be a pointer to the Fts5TokenizerModule structure, +** not a copy of pUserData. +** +** Of course, if bV2Native is false, then x1 contains the real routines and +** x2 the synthesized ones. In this case a pointer to the Fts5TokenizerModule +** object should be passed to x2.xCreate. +** +** The synthesized wrapper routines are necessary for xFindTokenizer(_v2) +** calls. */ struct Fts5TokenizerModule { char *zName; /* Name of tokenizer */ void *pUserData; /* User pointer passed to xCreate() */ - fts5_tokenizer x; /* Tokenizer functions */ + int bV2Native; /* True if v2 native tokenizer */ + fts5_tokenizer x1; /* Tokenizer functions */ + fts5_tokenizer_v2 x2; /* V2 tokenizer functions */ void (*xDestroy)(void*); /* Destructor function */ Fts5TokenizerModule *pNext; /* Next registered tokenizer module */ }; -/* -** Virtual-table object. -*/ -struct Fts5Table { - sqlite3_vtab base; /* Base class used by SQLite core */ - Fts5Config *pConfig; /* Virtual table configuration */ - Fts5Index *pIndex; /* Full-text index */ +struct Fts5FullTable { + Fts5Table p; /* Public class members from fts5Int.h */ Fts5Storage *pStorage; /* Document store */ Fts5Global *pGlobal; /* Global (connection wide) data */ Fts5Cursor *pSortCsr; /* Sort data from this cursor */ + int iSavepoint; /* Successful xSavepoint()+1 */ + #ifdef SQLITE_DEBUG struct Fts5TransactionState ts; #endif @@ -144,9 +170,11 @@ struct Fts5Sorter { i64 iRowid; /* Current rowid */ const u8 *aPoslist; /* Position lists for current row */ int nIdx; /* Number of entries in aIdx[] */ - int aIdx[1]; /* Offsets into aPoslist for current row */ + int aIdx[FLEXARRAY]; /* Offsets into aPoslist for current row */ }; +/* Size (int bytes) of an Fts5Sorter object with N indexes */ +#define SZ_FTS5SORTER(N) (offsetof(Fts5Sorter,nIdx)+((N+2)/2)*sizeof(i64)) /* ** Virtual-table cursor object. @@ -196,7 +224,7 @@ struct Fts5Cursor { Fts5Auxiliary *pAux; /* Currently executing extension function */ Fts5Auxdata *pAuxdata; /* First in linked list of saved aux-data */ - /* Cache used by auxiliary functions xInst() and xInstCount() */ + /* Cache used by auxiliary API functions xInst() and xInstCount() */ Fts5PoslistReader *aInstIter; /* One for each phrase */ int nInstAlloc; /* Size of aInst[] array (entries / 3) */ int nInstCount; /* Number of phrase instances */ @@ -254,7 +282,7 @@ struct Fts5Auxdata { #define FTS5_SAVEPOINT 5 #define FTS5_RELEASE 6 #define FTS5_ROLLBACKTO 7 -static void fts5CheckTransactionState(Fts5Table *p, int op, int iSavepoint){ +static void fts5CheckTransactionState(Fts5FullTable *p, int op, int iSavepoint){ switch( op ){ case FTS5_BEGIN: assert( p->ts.eState==0 ); @@ -263,7 +291,7 @@ static void fts5CheckTransactionState(Fts5Table *p, int op, int iSavepoint){ break; case FTS5_SYNC: - assert( p->ts.eState==1 ); + assert( p->ts.eState==1 || p->ts.eState==2 ); p->ts.eState = 2; break; @@ -278,23 +306,26 @@ static void fts5CheckTransactionState(Fts5Table *p, int op, int iSavepoint){ break; case FTS5_SAVEPOINT: - assert( p->ts.eState==1 ); + assert( p->ts.eState>=1 ); assert( iSavepoint>=0 ); - assert( iSavepoint>p->ts.iSavepoint ); + assert( iSavepoint>=p->ts.iSavepoint ); p->ts.iSavepoint = iSavepoint; break; case FTS5_RELEASE: - assert( p->ts.eState==1 ); + assert( p->ts.eState>=1 ); assert( iSavepoint>=0 ); assert( iSavepoint<=p->ts.iSavepoint ); p->ts.iSavepoint = iSavepoint-1; break; case FTS5_ROLLBACKTO: - assert( p->ts.eState==1 ); - assert( iSavepoint>=0 ); - assert( iSavepoint<=p->ts.iSavepoint ); + assert( p->ts.eState>=1 ); + assert( iSavepoint>=-1 ); + /* The following assert() can fail if another vtab strikes an error + ** within an xSavepoint() call then SQLite calls xRollbackTo() - without + ** having called xSavepoint() on this vtab. */ + /* assert( iSavepoint<=p->ts.iSavepoint ); */ p->ts.iSavepoint = iSavepoint; break; } @@ -304,20 +335,26 @@ static void fts5CheckTransactionState(Fts5Table *p, int op, int iSavepoint){ #endif /* -** Return true if pTab is a contentless table. +** Return true if pTab is a contentless table. If parameter bIncludeUnindexed +** is true, this includes contentless tables that store UNINDEXED columns +** only. */ -static int fts5IsContentless(Fts5Table *pTab){ - return pTab->pConfig->eContent==FTS5_CONTENT_NONE; +static int fts5IsContentless(Fts5FullTable *pTab, int bIncludeUnindexed){ + int eContent = pTab->p.pConfig->eContent; + return ( + eContent==FTS5_CONTENT_NONE + || (bIncludeUnindexed && eContent==FTS5_CONTENT_UNINDEXED) + ); } /* ** Delete a virtual table handle allocated by fts5InitVtab(). */ -static void fts5FreeVtab(Fts5Table *pTab){ +static void fts5FreeVtab(Fts5FullTable *pTab){ if( pTab ){ - sqlite3Fts5IndexClose(pTab->pIndex); + sqlite3Fts5IndexClose(pTab->p.pIndex); sqlite3Fts5StorageClose(pTab->pStorage); - sqlite3Fts5ConfigFree(pTab->pConfig); + sqlite3Fts5ConfigFree(pTab->p.pConfig); sqlite3_free(pTab); } } @@ -326,7 +363,7 @@ static void fts5FreeVtab(Fts5Table *pTab){ ** The xDisconnect() virtual table method. */ static int fts5DisconnectMethod(sqlite3_vtab *pVtab){ - fts5FreeVtab((Fts5Table*)pVtab); + fts5FreeVtab((Fts5FullTable*)pVtab); return SQLITE_OK; } @@ -337,7 +374,7 @@ static int fts5DestroyMethod(sqlite3_vtab *pVtab){ Fts5Table *pTab = (Fts5Table*)pVtab; int rc = sqlite3Fts5DropAll(pTab->pConfig); if( rc==SQLITE_OK ){ - fts5FreeVtab((Fts5Table*)pVtab); + fts5FreeVtab((Fts5FullTable*)pVtab); } return rc; } @@ -366,28 +403,32 @@ static int fts5InitVtab( const char **azConfig = (const char**)argv; int rc = SQLITE_OK; /* Return code */ Fts5Config *pConfig = 0; /* Results of parsing argc/argv */ - Fts5Table *pTab = 0; /* New virtual table object */ + Fts5FullTable *pTab = 0; /* New virtual table object */ /* Allocate the new vtab object and parse the configuration */ - pTab = (Fts5Table*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5Table)); + pTab = (Fts5FullTable*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5FullTable)); if( rc==SQLITE_OK ){ rc = sqlite3Fts5ConfigParse(pGlobal, db, argc, azConfig, &pConfig, pzErr); assert( (rc==SQLITE_OK && *pzErr==0) || pConfig==0 ); } if( rc==SQLITE_OK ){ - pTab->pConfig = pConfig; + pConfig->pzErrmsg = pzErr; + pTab->p.pConfig = pConfig; pTab->pGlobal = pGlobal; + if( bCreate || sqlite3Fts5TokenizerPreload(&pConfig->t) ){ + rc = sqlite3Fts5LoadTokenizer(pConfig); + } } /* Open the index sub-system */ if( rc==SQLITE_OK ){ - rc = sqlite3Fts5IndexOpen(pConfig, bCreate, &pTab->pIndex, pzErr); + rc = sqlite3Fts5IndexOpen(pConfig, bCreate, &pTab->p.pIndex, pzErr); } /* Open the storage sub-system */ if( rc==SQLITE_OK ){ rc = sqlite3Fts5StorageOpen( - pConfig, pTab->pIndex, bCreate, &pTab->pStorage, pzErr + pConfig, pTab->p.pIndex, bCreate, &pTab->pStorage, pzErr ); } @@ -398,13 +439,17 @@ static int fts5InitVtab( /* Load the initial configuration */ if( rc==SQLITE_OK ){ - assert( pConfig->pzErrmsg==0 ); - pConfig->pzErrmsg = pzErr; - rc = sqlite3Fts5IndexLoadConfig(pTab->pIndex); - sqlite3Fts5IndexRollback(pTab->pIndex); - pConfig->pzErrmsg = 0; + rc = sqlite3Fts5ConfigLoad(pTab->p.pConfig, pTab->p.pConfig->iCookie-1); + } + + if( rc==SQLITE_OK && pConfig->eContent==FTS5_CONTENT_NORMAL ){ + rc = sqlite3_vtab_config(db, SQLITE_VTAB_CONSTRAINT_SUPPORT, (int)1); + } + if( rc==SQLITE_OK ){ + rc = sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS); } + if( pConfig ) pConfig->pzErrmsg = 0; if( rc!=SQLITE_OK ){ fts5FreeVtab(pTab); pTab = 0; @@ -466,25 +511,77 @@ static void fts5SetUniqueFlag(sqlite3_index_info *pIdxInfo){ #endif } +static void fts5SetEstimatedRows(sqlite3_index_info *pIdxInfo, i64 nRow){ +#if SQLITE_VERSION_NUMBER>=3008002 +#ifndef SQLITE_CORE + if( sqlite3_libversion_number()>=3008002 ) +#endif + { + pIdxInfo->estimatedRows = nRow; + } +#endif +} + +static int fts5UsePatternMatch( + Fts5Config *pConfig, + struct sqlite3_index_constraint *p +){ + assert( FTS5_PATTERN_GLOB==SQLITE_INDEX_CONSTRAINT_GLOB ); + assert( FTS5_PATTERN_LIKE==SQLITE_INDEX_CONSTRAINT_LIKE ); + if( pConfig->t.ePattern==FTS5_PATTERN_GLOB && p->op==FTS5_PATTERN_GLOB ){ + return 1; + } + if( pConfig->t.ePattern==FTS5_PATTERN_LIKE + && (p->op==FTS5_PATTERN_LIKE || p->op==FTS5_PATTERN_GLOB) + ){ + return 1; + } + return 0; +} + /* ** Implementation of the xBestIndex method for FTS5 tables. Within the ** WHERE constraint, it searches for the following: ** -** 1. A MATCH constraint against the special column. +** 1. A MATCH constraint against the table column. ** 2. A MATCH constraint against the "rank" column. -** 3. An == constraint against the rowid column. -** 4. A < or <= constraint against the rowid column. -** 5. A > or >= constraint against the rowid column. +** 3. A MATCH constraint against some other column. +** 4. An == constraint against the rowid column. +** 5. A < or <= constraint against the rowid column. +** 6. A > or >= constraint against the rowid column. ** -** Within the ORDER BY, either: +** Within the ORDER BY, the following are supported: ** ** 5. ORDER BY rank [ASC|DESC] ** 6. ORDER BY rowid [ASC|DESC] ** -** Costs are assigned as follows: +** Information for the xFilter call is passed via both the idxNum and +** idxStr variables. Specifically, idxNum is a bitmask of the following +** flags used to encode the ORDER BY clause: ** -** a) If an unusable MATCH operator is present in the WHERE clause, the -** cost is unconditionally set to 1e50 (a really big number). +** FTS5_BI_ORDER_RANK +** FTS5_BI_ORDER_ROWID +** FTS5_BI_ORDER_DESC +** +** idxStr is used to encode data from the WHERE clause. For each argument +** passed to the xFilter method, the following is appended to idxStr: +** +** Match against table column: "m" +** Match against rank column: "r" +** Match against other column: "M" +** LIKE against other column: "L" +** GLOB against other column: "G" +** Equality constraint against the rowid: "=" +** A < or <= against the rowid: "<" +** A > or >= against the rowid: ">" +** +** This function ensures that there is at most one "r" or "=". And that if +** there exists an "=" then there is no "<" or ">". +** +** If an unusable MATCH operator is present in the WHERE clause, then +** SQLITE_CONSTRAINT is returned. +** +** Costs are assigned as follows: ** ** a) If a MATCH operator is present, the cost depends on the other ** constraints also present. As follows: @@ -506,60 +603,115 @@ static void fts5SetUniqueFlag(sqlite3_index_info *pIdxInfo){ static int fts5BestIndexMethod(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){ Fts5Table *pTab = (Fts5Table*)pVTab; Fts5Config *pConfig = pTab->pConfig; + const int nCol = pConfig->nCol; int idxFlags = 0; /* Parameter passed through to xFilter() */ - int bHasMatch; - int iNext; int i; - struct Constraint { - int op; /* Mask against sqlite3_index_constraint.op */ - int fts5op; /* FTS5 mask for idxFlags */ - int iCol; /* 0==rowid, 1==tbl, 2==rank */ - int omit; /* True to omit this if found */ - int iConsIndex; /* Index in pInfo->aConstraint[] */ - } aConstraint[] = { - {SQLITE_INDEX_CONSTRAINT_MATCH|SQLITE_INDEX_CONSTRAINT_EQ, - FTS5_BI_MATCH, 1, 1, -1}, - {SQLITE_INDEX_CONSTRAINT_MATCH|SQLITE_INDEX_CONSTRAINT_EQ, - FTS5_BI_RANK, 2, 1, -1}, - {SQLITE_INDEX_CONSTRAINT_EQ, FTS5_BI_ROWID_EQ, 0, 0, -1}, - {SQLITE_INDEX_CONSTRAINT_LT|SQLITE_INDEX_CONSTRAINT_LE, - FTS5_BI_ROWID_LE, 0, 0, -1}, - {SQLITE_INDEX_CONSTRAINT_GT|SQLITE_INDEX_CONSTRAINT_GE, - FTS5_BI_ROWID_GE, 0, 0, -1}, - }; + char *idxStr; + int iIdxStr = 0; + int iCons = 0; + + int bSeenEq = 0; + int bSeenGt = 0; + int bSeenLt = 0; + int nSeenMatch = 0; + int bSeenRank = 0; - int aColMap[3]; - aColMap[0] = -1; - aColMap[1] = pConfig->nCol; - aColMap[2] = pConfig->nCol+1; - /* Set idxFlags flags for all WHERE clause terms that will be used. */ + assert( SQLITE_INDEX_CONSTRAINT_EQbLock ){ + pTab->base.zErrMsg = sqlite3_mprintf( + "recursively defined fts5 content table" + ); + return SQLITE_ERROR; + } + + idxStr = (char*)sqlite3_malloc(pInfo->nConstraint * 8 + 1); + if( idxStr==0 ) return SQLITE_NOMEM; + pInfo->idxStr = idxStr; + pInfo->needToFreeIdxStr = 1; + for(i=0; inConstraint; i++){ struct sqlite3_index_constraint *p = &pInfo->aConstraint[i]; - int j; - for(j=0; jiColumn==aColMap[pC->iCol] && p->op & pC->op ){ - if( p->usable ){ - pC->iConsIndex = i; - idxFlags |= pC->fts5op; - }else if( j==0 ){ - /* As there exists an unusable MATCH constraint this is an - ** unusable plan. Set a prohibitively high cost. */ - pInfo->estimatedCost = 1e50; - return SQLITE_OK; + int iCol = p->iColumn; + if( p->op==SQLITE_INDEX_CONSTRAINT_MATCH + || (p->op==SQLITE_INDEX_CONSTRAINT_EQ && iCol>=nCol) + ){ + /* A MATCH operator or equivalent */ + if( p->usable==0 || iCol<0 ){ + /* As there exists an unusable MATCH constraint this is an + ** unusable plan. Return SQLITE_CONSTRAINT. */ + idxStr[iIdxStr] = 0; + return SQLITE_CONSTRAINT; + }else{ + if( iCol==nCol+1 ){ + if( bSeenRank ) continue; + idxStr[iIdxStr++] = 'r'; + bSeenRank = 1; + }else{ + nSeenMatch++; + idxStr[iIdxStr++] = 'M'; + sqlite3_snprintf(6, &idxStr[iIdxStr], "%d", iCol); + iIdxStr += (int)strlen(&idxStr[iIdxStr]); + assert( idxStr[iIdxStr]=='\0' ); + } + pInfo->aConstraintUsage[i].argvIndex = ++iCons; + pInfo->aConstraintUsage[i].omit = 1; + } + }else if( p->usable ){ + if( iCol>=0 && iColop==FTS5_PATTERN_LIKE || p->op==FTS5_PATTERN_GLOB ); + idxStr[iIdxStr++] = p->op==FTS5_PATTERN_LIKE ? 'L' : 'G'; + sqlite3_snprintf(6, &idxStr[iIdxStr], "%d", iCol); + idxStr += strlen(&idxStr[iIdxStr]); + pInfo->aConstraintUsage[i].argvIndex = ++iCons; + assert( idxStr[iIdxStr]=='\0' ); + nSeenMatch++; + }else if( bSeenEq==0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ && iCol<0 ){ + idxStr[iIdxStr++] = '='; + bSeenEq = 1; + pInfo->aConstraintUsage[i].argvIndex = ++iCons; + pInfo->aConstraintUsage[i].omit = 1; + } + } + } + + if( bSeenEq==0 ){ + for(i=0; inConstraint; i++){ + struct sqlite3_index_constraint *p = &pInfo->aConstraint[i]; + if( p->iColumn<0 && p->usable ){ + int op = p->op; + if( op==SQLITE_INDEX_CONSTRAINT_LT || op==SQLITE_INDEX_CONSTRAINT_LE ){ + if( bSeenLt ) continue; + idxStr[iIdxStr++] = '<'; + pInfo->aConstraintUsage[i].argvIndex = ++iCons; + bSeenLt = 1; + }else + if( op==SQLITE_INDEX_CONSTRAINT_GT || op==SQLITE_INDEX_CONSTRAINT_GE ){ + if( bSeenGt ) continue; + idxStr[iIdxStr++] = '>'; + pInfo->aConstraintUsage[i].argvIndex = ++iCons; + bSeenGt = 1; } } } } + idxStr[iIdxStr] = '\0'; - /* Set idxFlags flags for the ORDER BY clause */ + /* Set idxFlags flags for the ORDER BY clause + ** + ** Note that tokendata=1 tables cannot currently handle "ORDER BY rowid DESC". + */ if( pInfo->nOrderBy==1 ){ int iSort = pInfo->aOrderBy[0].iColumn; - if( iSort==(pConfig->nCol+1) && BitFlagTest(idxFlags, FTS5_BI_MATCH) ){ + if( iSort==(pConfig->nCol+1) && nSeenMatch>0 ){ idxFlags |= FTS5_BI_ORDER_RANK; - }else if( iSort==-1 ){ + }else if( iSort==-1 && (!pInfo->aOrderBy[0].desc || !pConfig->bTokendata) ){ idxFlags |= FTS5_BI_ORDER_ROWID; } if( BitFlagTest(idxFlags, FTS5_BI_ORDER_RANK|FTS5_BI_ORDER_ROWID) ){ @@ -571,53 +723,60 @@ static int fts5BestIndexMethod(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){ } /* Calculate the estimated cost based on the flags set in idxFlags. */ - bHasMatch = BitFlagTest(idxFlags, FTS5_BI_MATCH); - if( BitFlagTest(idxFlags, FTS5_BI_ROWID_EQ) ){ - pInfo->estimatedCost = bHasMatch ? 100.0 : 10.0; - if( bHasMatch==0 ) fts5SetUniqueFlag(pInfo); - }else if( BitFlagAllTest(idxFlags, FTS5_BI_ROWID_LE|FTS5_BI_ROWID_GE) ){ - pInfo->estimatedCost = bHasMatch ? 500.0 : 250000.0; - }else if( BitFlagTest(idxFlags, FTS5_BI_ROWID_LE|FTS5_BI_ROWID_GE) ){ - pInfo->estimatedCost = bHasMatch ? 750.0 : 750000.0; + if( bSeenEq ){ + pInfo->estimatedCost = nSeenMatch ? 1000.0 : 25.0; + fts5SetUniqueFlag(pInfo); + fts5SetEstimatedRows(pInfo, 1); }else{ - pInfo->estimatedCost = bHasMatch ? 1000.0 : 1000000.0; - } - - /* Assign argvIndex values to each constraint in use. */ - iNext = 1; - for(i=0; iiConsIndex>=0 ){ - pInfo->aConstraintUsage[pC->iConsIndex].argvIndex = iNext++; - pInfo->aConstraintUsage[pC->iConsIndex].omit = (unsigned char)pC->omit; + if( bSeenLt && bSeenGt ){ + pInfo->estimatedCost = nSeenMatch ? 5000.0 : 750000.0; + }else if( bSeenLt || bSeenGt ){ + pInfo->estimatedCost = nSeenMatch ? 7500.0 : 2250000.0; + }else{ + pInfo->estimatedCost = nSeenMatch ? 10000.0 : 3000000.0; } + for(i=1; iestimatedCost *= 0.4; + } + fts5SetEstimatedRows(pInfo, (i64)(pInfo->estimatedCost / 4.0)); } pInfo->idxNum = idxFlags; return SQLITE_OK; } +static int fts5NewTransaction(Fts5FullTable *pTab){ + Fts5Cursor *pCsr; + for(pCsr=pTab->pGlobal->pCsr; pCsr; pCsr=pCsr->pNext){ + if( pCsr->base.pVtab==(sqlite3_vtab*)pTab ) return SQLITE_OK; + } + return sqlite3Fts5StorageReset(pTab->pStorage); +} + /* ** Implementation of xOpen method. */ static int fts5OpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){ - Fts5Table *pTab = (Fts5Table*)pVTab; - Fts5Config *pConfig = pTab->pConfig; - Fts5Cursor *pCsr; /* New cursor object */ - int nByte; /* Bytes of space to allocate */ - int rc = SQLITE_OK; /* Return code */ + Fts5FullTable *pTab = (Fts5FullTable*)pVTab; + Fts5Config *pConfig = pTab->p.pConfig; + Fts5Cursor *pCsr = 0; /* New cursor object */ + sqlite3_int64 nByte; /* Bytes of space to allocate */ + int rc; /* Return code */ - nByte = sizeof(Fts5Cursor) + pConfig->nCol * sizeof(int); - pCsr = (Fts5Cursor*)sqlite3_malloc(nByte); - if( pCsr ){ - Fts5Global *pGlobal = pTab->pGlobal; - memset(pCsr, 0, nByte); - pCsr->aColumnSize = (int*)&pCsr[1]; - pCsr->pNext = pGlobal->pCsr; - pGlobal->pCsr = pCsr; - pCsr->iCsrId = ++pGlobal->iNextId; - }else{ - rc = SQLITE_NOMEM; + rc = fts5NewTransaction(pTab); + if( rc==SQLITE_OK ){ + nByte = sizeof(Fts5Cursor) + pConfig->nCol * sizeof(int); + pCsr = (Fts5Cursor*)sqlite3_malloc64(nByte); + if( pCsr ){ + Fts5Global *pGlobal = pTab->pGlobal; + memset(pCsr, 0, (size_t)nByte); + pCsr->aColumnSize = (int*)&pCsr[1]; + pCsr->pNext = pGlobal->pCsr; + pGlobal->pCsr = pCsr; + pCsr->iCsrId = ++pGlobal->iNextId; + }else{ + rc = SQLITE_NOMEM; + } } *ppCsr = (sqlite3_vtab_cursor*)pCsr; return rc; @@ -645,7 +804,7 @@ static void fts5CsrNewrow(Fts5Cursor *pCsr){ } static void fts5FreeCursorComponents(Fts5Cursor *pCsr){ - Fts5Table *pTab = (Fts5Table*)(pCsr->base.pVtab); + Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab); Fts5Auxdata *pData; Fts5Auxdata *pNext; @@ -679,6 +838,7 @@ static void fts5FreeCursorComponents(Fts5Cursor *pCsr){ sqlite3_free(pCsr->zRankArgs); } + sqlite3Fts5IndexCloseReader(pTab->p.pIndex); memset(&pCsr->ePlan, 0, sizeof(Fts5Cursor) - ((u8*)&pCsr->ePlan - (u8*)pCsr)); } @@ -689,7 +849,7 @@ static void fts5FreeCursorComponents(Fts5Cursor *pCsr){ */ static int fts5CloseMethod(sqlite3_vtab_cursor *pCursor){ if( pCursor ){ - Fts5Table *pTab = (Fts5Table*)(pCursor->pVtab); + Fts5FullTable *pTab = (Fts5FullTable*)(pCursor->pVtab); Fts5Cursor *pCsr = (Fts5Cursor*)pCursor; Fts5Cursor **pp; @@ -710,7 +870,7 @@ static int fts5SorterNext(Fts5Cursor *pCsr){ rc = sqlite3_step(pSorter->pStmt); if( rc==SQLITE_DONE ){ rc = SQLITE_OK; - CsrFlagSet(pCsr, FTS5CSR_EOF); + CsrFlagSet(pCsr, FTS5CSR_EOF|FTS5CSR_REQUIRE_CONTENT); }else if( rc==SQLITE_ROW ){ const u8 *a; const u8 *aBlob; @@ -746,7 +906,7 @@ static int fts5SorterNext(Fts5Cursor *pCsr){ ** Set the FTS5CSR_REQUIRE_RESEEK flag on all FTS5_PLAN_MATCH cursors ** open on table pTab. */ -static void fts5TripCursors(Fts5Table *pTab){ +static void fts5TripCursors(Fts5FullTable *pTab){ Fts5Cursor *pCsr; for(pCsr=pTab->pGlobal->pCsr; pCsr; pCsr=pCsr->pNext){ if( pCsr->ePlan==FTS5_PLAN_MATCH @@ -773,11 +933,13 @@ static int fts5CursorReseek(Fts5Cursor *pCsr, int *pbSkip){ int rc = SQLITE_OK; assert( *pbSkip==0 ); if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_RESEEK) ){ - Fts5Table *pTab = (Fts5Table*)(pCsr->base.pVtab); + Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab); int bDesc = pCsr->bDesc; i64 iRowid = sqlite3Fts5ExprRowid(pCsr->pExpr); - rc = sqlite3Fts5ExprFirst(pCsr->pExpr, pTab->pIndex, iRowid, bDesc); + rc = sqlite3Fts5ExprFirst( + pCsr->pExpr, pTab->p.pIndex, iRowid, pCsr->iLastRowid, bDesc + ); if( rc==SQLITE_OK && iRowid!=sqlite3Fts5ExprRowid(pCsr->pExpr) ){ *pbSkip = 1; } @@ -810,6 +972,16 @@ static int fts5NextMethod(sqlite3_vtab_cursor *pCursor){ ); assert( !CsrFlagTest(pCsr, FTS5CSR_EOF) ); + /* If this cursor uses FTS5_PLAN_MATCH and this is a tokendata=1 table, + ** clear any token mappings accumulated at the fts5_index.c level. In + ** other cases, specifically FTS5_PLAN_SOURCE and FTS5_PLAN_SORTED_MATCH, + ** we need to retain the mappings for the entire query. */ + if( pCsr->ePlan==FTS5_PLAN_MATCH + && ((Fts5Table*)pCursor->pVtab)->pConfig->bTokendata + ){ + sqlite3Fts5ExprClearTokens(pCsr->pExpr); + } + if( pCsr->ePlan<3 ){ int bSkip = 0; if( (rc = fts5CursorReseek(pCsr, &bSkip)) || bSkip ) return rc; @@ -829,15 +1001,25 @@ static int fts5NextMethod(sqlite3_vtab_cursor *pCursor){ break; } - default: + default: { + Fts5Config *pConfig = ((Fts5Table*)pCursor->pVtab)->pConfig; + pConfig->bLock++; rc = sqlite3_step(pCsr->pStmt); + pConfig->bLock--; if( rc!=SQLITE_ROW ){ CsrFlagSet(pCsr, FTS5CSR_EOF); rc = sqlite3_reset(pCsr->pStmt); + if( rc!=SQLITE_OK ){ + pCursor->pVtab->zErrMsg = sqlite3_mprintf( + "%s", sqlite3_errmsg(pConfig->db) + ); + } }else{ rc = SQLITE_OK; + CsrFlagSet(pCsr, FTS5CSR_REQUIRE_DOCSIZE); } break; + } } } @@ -861,9 +1043,10 @@ static int fts5PrepareStatement( if( zSql==0 ){ rc = SQLITE_NOMEM; }else{ - rc = sqlite3_prepare_v2(pConfig->db, zSql, -1, &pRet, 0); + rc = sqlite3_prepare_v3(pConfig->db, zSql, -1, + SQLITE_PREPARE_PERSISTENT, &pRet, 0); if( rc!=SQLITE_OK ){ - *pConfig->pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(pConfig->db)); + sqlite3Fts5ConfigErrmsg(pConfig, "%s", sqlite3_errmsg(pConfig->db)); } sqlite3_free(zSql); } @@ -873,20 +1056,24 @@ static int fts5PrepareStatement( return rc; } -static int fts5CursorFirstSorted(Fts5Table *pTab, Fts5Cursor *pCsr, int bDesc){ - Fts5Config *pConfig = pTab->pConfig; +static int fts5CursorFirstSorted( + Fts5FullTable *pTab, + Fts5Cursor *pCsr, + int bDesc +){ + Fts5Config *pConfig = pTab->p.pConfig; Fts5Sorter *pSorter; int nPhrase; - int nByte; + sqlite3_int64 nByte; int rc; const char *zRank = pCsr->zRank; const char *zRankArgs = pCsr->zRankArgs; nPhrase = sqlite3Fts5ExprPhraseCount(pCsr->pExpr); - nByte = sizeof(Fts5Sorter) + sizeof(int) * (nPhrase-1); - pSorter = (Fts5Sorter*)sqlite3_malloc(nByte); + nByte = SZ_FTS5SORTER(nPhrase); + pSorter = (Fts5Sorter*)sqlite3_malloc64(nByte); if( pSorter==0 ) return SQLITE_NOMEM; - memset(pSorter, 0, nByte); + memset(pSorter, 0, (size_t)nByte); pSorter->nIdx = nPhrase; /* TODO: It would be better to have some system for reusing statement @@ -897,7 +1084,7 @@ static int fts5CursorFirstSorted(Fts5Table *pTab, Fts5Cursor *pCsr, int bDesc){ ** ** If SQLite a built-in statement cache, this wouldn't be a problem. */ rc = fts5PrepareStatement(&pSorter->pStmt, pConfig, - "SELECT rowid, rank FROM %Q.%Q ORDER BY %s(%s%s%s) %s", + "SELECT rowid, rank FROM %Q.%Q ORDER BY %s(\"%w\"%s%s) %s", pConfig->zDb, pConfig->zName, zRank, pConfig->zName, (zRankArgs ? ", " : ""), (zRankArgs ? zRankArgs : ""), @@ -921,10 +1108,12 @@ static int fts5CursorFirstSorted(Fts5Table *pTab, Fts5Cursor *pCsr, int bDesc){ return rc; } -static int fts5CursorFirst(Fts5Table *pTab, Fts5Cursor *pCsr, int bDesc){ +static int fts5CursorFirst(Fts5FullTable *pTab, Fts5Cursor *pCsr, int bDesc){ int rc; Fts5Expr *pExpr = pCsr->pExpr; - rc = sqlite3Fts5ExprFirst(pExpr, pTab->pIndex, pCsr->iFirstRowid, bDesc); + rc = sqlite3Fts5ExprFirst( + pExpr, pTab->p.pIndex, pCsr->iFirstRowid, pCsr->iLastRowid, bDesc + ); if( sqlite3Fts5ExprEof(pExpr) ){ CsrFlagSet(pCsr, FTS5CSR_EOF); } @@ -939,7 +1128,7 @@ static int fts5CursorFirst(Fts5Table *pTab, Fts5Cursor *pCsr, int bDesc){ ** parameters. */ static int fts5SpecialMatch( - Fts5Table *pTab, + Fts5FullTable *pTab, Fts5Cursor *pCsr, const char *zQuery ){ @@ -950,18 +1139,18 @@ static int fts5SpecialMatch( while( z[0]==' ' ) z++; for(n=0; z[n] && z[n]!=' '; n++); - assert( pTab->base.zErrMsg==0 ); + assert( pTab->p.base.zErrMsg==0 ); pCsr->ePlan = FTS5_PLAN_SPECIAL; - if( 0==sqlite3_strnicmp("reads", z, n) ){ - pCsr->iSpecial = sqlite3Fts5IndexReads(pTab->pIndex); + if( n==5 && 0==sqlite3_strnicmp("reads", z, n) ){ + pCsr->iSpecial = sqlite3Fts5IndexReads(pTab->p.pIndex); } - else if( 0==sqlite3_strnicmp("id", z, n) ){ + else if( n==2 && 0==sqlite3_strnicmp("id", z, n) ){ pCsr->iSpecial = pCsr->iCsrId; } else{ /* An unrecognized directive. Return an error message. */ - pTab->base.zErrMsg = sqlite3_mprintf("unknown special query: %.*s", n, z); + pTab->p.base.zErrMsg = sqlite3_mprintf("unknown special query: %.*s", n, z); rc = SQLITE_ERROR; } @@ -973,7 +1162,7 @@ static int fts5SpecialMatch( ** pTab. If one is found, return a pointer to the corresponding Fts5Auxiliary ** structure. Otherwise, if no such function exists, return NULL. */ -static Fts5Auxiliary *fts5FindAuxiliary(Fts5Table *pTab, const char *zName){ +static Fts5Auxiliary *fts5FindAuxiliary(Fts5FullTable *pTab, const char *zName){ Fts5Auxiliary *pAux; for(pAux=pTab->pGlobal->pAux; pAux; pAux=pAux->pNext){ @@ -986,8 +1175,8 @@ static Fts5Auxiliary *fts5FindAuxiliary(Fts5Table *pTab, const char *zName){ static int fts5FindRankFunction(Fts5Cursor *pCsr){ - Fts5Table *pTab = (Fts5Table*)(pCsr->base.pVtab); - Fts5Config *pConfig = pTab->pConfig; + Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab); + Fts5Config *pConfig = pTab->p.pConfig; int rc = SQLITE_OK; Fts5Auxiliary *pAux = 0; const char *zRank = pCsr->zRank; @@ -997,12 +1186,13 @@ static int fts5FindRankFunction(Fts5Cursor *pCsr){ char *zSql = sqlite3Fts5Mprintf(&rc, "SELECT %s", zRankArgs); if( zSql ){ sqlite3_stmt *pStmt = 0; - rc = sqlite3_prepare_v2(pConfig->db, zSql, -1, &pStmt, 0); + rc = sqlite3_prepare_v3(pConfig->db, zSql, -1, + SQLITE_PREPARE_PERSISTENT, &pStmt, 0); sqlite3_free(zSql); assert( rc==SQLITE_OK || pCsr->pRankArgStmt==0 ); if( rc==SQLITE_OK ){ if( SQLITE_ROW==sqlite3_step(pStmt) ){ - int nByte; + sqlite3_int64 nByte; pCsr->nRankArg = sqlite3_column_count(pStmt); nByte = sizeof(sqlite3_value*)*pCsr->nRankArg; pCsr->apRankArg = (sqlite3_value**)sqlite3Fts5MallocZero(&rc, nByte); @@ -1024,8 +1214,8 @@ static int fts5FindRankFunction(Fts5Cursor *pCsr){ if( rc==SQLITE_OK ){ pAux = fts5FindAuxiliary(pTab, zRank); if( pAux==0 ){ - assert( pTab->base.zErrMsg==0 ); - pTab->base.zErrMsg = sqlite3_mprintf("no such function: %s", zRank); + assert( pTab->p.base.zErrMsg==0 ); + pTab->p.base.zErrMsg = sqlite3_mprintf("no such function: %s", zRank); rc = SQLITE_ERROR; } } @@ -1082,6 +1272,145 @@ static i64 fts5GetRowidLimit(sqlite3_value *pVal, i64 iDefault){ return iDefault; } +/* +** Set the error message on the virtual table passed as the first argument. +*/ +static void fts5SetVtabError(Fts5FullTable *p, const char *zFormat, ...){ + va_list ap; /* ... printf arguments */ + va_start(ap, zFormat); + sqlite3_free(p->p.base.zErrMsg); + p->p.base.zErrMsg = sqlite3_vmprintf(zFormat, ap); + va_end(ap); +} + +/* +** Arrange for subsequent calls to sqlite3Fts5Tokenize() to use the locale +** specified by pLocale/nLocale. The buffer indicated by pLocale must remain +** valid until after the final call to sqlite3Fts5Tokenize() that will use +** the locale. +*/ +static void sqlite3Fts5SetLocale( + Fts5Config *pConfig, + const char *zLocale, + int nLocale +){ + Fts5TokenizerConfig *pT = &pConfig->t; + pT->pLocale = zLocale; + pT->nLocale = nLocale; +} + +/* +** Clear any locale configured by an earlier call to sqlite3Fts5SetLocale(). +*/ +void sqlite3Fts5ClearLocale(Fts5Config *pConfig){ + sqlite3Fts5SetLocale(pConfig, 0, 0); +} + +/* +** Return true if the value passed as the only argument is an +** fts5_locale() value. +*/ +int sqlite3Fts5IsLocaleValue(Fts5Config *pConfig, sqlite3_value *pVal){ + int ret = 0; + if( sqlite3_value_type(pVal)==SQLITE_BLOB ){ + /* Call sqlite3_value_bytes() after sqlite3_value_blob() in this case. + ** If the blob was created using zeroblob(), then sqlite3_value_blob() + ** may call malloc(). If this malloc() fails, then the values returned + ** by both value_blob() and value_bytes() will be 0. If value_bytes() were + ** called first, then the NULL pointer returned by value_blob() might + ** be dereferenced. */ + const u8 *pBlob = sqlite3_value_blob(pVal); + int nBlob = sqlite3_value_bytes(pVal); + if( nBlob>FTS5_LOCALE_HDR_SIZE + && 0==memcmp(pBlob, FTS5_LOCALE_HDR(pConfig), FTS5_LOCALE_HDR_SIZE) + ){ + ret = 1; + } + } + return ret; +} + +/* +** Value pVal is guaranteed to be an fts5_locale() value, according to +** sqlite3Fts5IsLocaleValue(). This function extracts the text and locale +** from the value and returns them separately. +** +** If successful, SQLITE_OK is returned and (*ppText) and (*ppLoc) set +** to point to buffers containing the text and locale, as utf-8, +** respectively. In this case output parameters (*pnText) and (*pnLoc) are +** set to the sizes in bytes of these two buffers. +** +** Or, if an error occurs, then an SQLite error code is returned. The final +** value of the four output parameters is undefined in this case. +*/ +int sqlite3Fts5DecodeLocaleValue( + sqlite3_value *pVal, + const char **ppText, + int *pnText, + const char **ppLoc, + int *pnLoc +){ + const char *p = sqlite3_value_blob(pVal); + int n = sqlite3_value_bytes(pVal); + int nLoc = 0; + + assert( sqlite3_value_type(pVal)==SQLITE_BLOB ); + assert( n>FTS5_LOCALE_HDR_SIZE ); + + for(nLoc=FTS5_LOCALE_HDR_SIZE; p[nLoc]; nLoc++){ + if( nLoc==(n-1) ){ + return SQLITE_MISMATCH; + } + } + *ppLoc = &p[FTS5_LOCALE_HDR_SIZE]; + *pnLoc = nLoc - FTS5_LOCALE_HDR_SIZE; + + *ppText = &p[nLoc+1]; + *pnText = n - nLoc - 1; + return SQLITE_OK; +} + +/* +** Argument pVal is the text of a full-text search expression. It may or +** may not have been wrapped by fts5_locale(). This function extracts +** the text of the expression, and sets output variable (*pzText) to +** point to a nul-terminated buffer containing the expression. +** +** If pVal was an fts5_locale() value, then sqlite3Fts5SetLocale() is called +** to set the tokenizer to use the specified locale. +** +** If output variable (*pbFreeAndReset) is set to true, then the caller +** is required to (a) call sqlite3Fts5ClearLocale() to reset the tokenizer +** locale, and (b) call sqlite3_free() to free (*pzText). +*/ +static int fts5ExtractExprText( + Fts5Config *pConfig, /* Fts5 configuration */ + sqlite3_value *pVal, /* Value to extract expression text from */ + char **pzText, /* OUT: nul-terminated buffer of text */ + int *pbFreeAndReset /* OUT: Free (*pzText) and clear locale */ +){ + int rc = SQLITE_OK; + + if( sqlite3Fts5IsLocaleValue(pConfig, pVal) ){ + const char *pText = 0; + int nText = 0; + const char *pLoc = 0; + int nLoc = 0; + rc = sqlite3Fts5DecodeLocaleValue(pVal, &pText, &nText, &pLoc, &nLoc); + *pzText = sqlite3Fts5Mprintf(&rc, "%.*s", nText, pText); + if( rc==SQLITE_OK ){ + sqlite3Fts5SetLocale(pConfig, pLoc, nLoc); + } + *pbFreeAndReset = 1; + }else{ + *pzText = (char*)sqlite3_value_text(pVal); + *pbFreeAndReset = 0; + } + + return rc; +} + + /* ** This is the xFilter interface for the virtual table. See ** the virtual table xFilter method documentation for additional @@ -1096,27 +1425,28 @@ static i64 fts5GetRowidLimit(sqlite3_value *pVal, i64 iDefault){ static int fts5FilterMethod( sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */ int idxNum, /* Strategy index */ - const char *zUnused, /* Unused */ + const char *idxStr, /* Unused */ int nVal, /* Number of elements in apVal */ sqlite3_value **apVal /* Arguments for the indexing scheme */ ){ - Fts5Table *pTab = (Fts5Table*)(pCursor->pVtab); - Fts5Config *pConfig = pTab->pConfig; + Fts5FullTable *pTab = (Fts5FullTable*)(pCursor->pVtab); + Fts5Config *pConfig = pTab->p.pConfig; Fts5Cursor *pCsr = (Fts5Cursor*)pCursor; int rc = SQLITE_OK; /* Error code */ - int iVal = 0; /* Counter for apVal[] */ int bDesc; /* True if ORDER BY [rank|rowid] DESC */ int bOrderByRank; /* True if ORDER BY rank */ - sqlite3_value *pMatch = 0; /* MATCH ? expression (or NULL) */ sqlite3_value *pRank = 0; /* rank MATCH ? expression (or NULL) */ sqlite3_value *pRowidEq = 0; /* rowid = ? expression (or NULL) */ sqlite3_value *pRowidLe = 0; /* rowid <= ? expression (or NULL) */ sqlite3_value *pRowidGe = 0; /* rowid >= ? expression (or NULL) */ + int iCol; /* Column on LHS of MATCH operator */ char **pzErrmsg = pConfig->pzErrmsg; + int bPrefixInsttoken = pConfig->bPrefixInsttoken; + int i; + int iIdxStr = 0; + Fts5Expr *pExpr = 0; - UNUSED_PARAM(zUnused); - UNUSED_PARAM(nVal); - + assert( pConfig->bLock==0 ); if( pCsr->ePlan ){ fts5FreeCursorComponents(pCsr); memset(&pCsr->ePlan, 0, sizeof(Fts5Cursor) - ((u8*)&pCsr->ePlan-(u8*)pCsr)); @@ -1128,21 +1458,89 @@ static int fts5FilterMethod( assert( pCsr->pRank==0 ); assert( pCsr->zRank==0 ); assert( pCsr->zRankArgs==0 ); + assert( pTab->pSortCsr==0 || nVal==0 ); - assert( pzErrmsg==0 || pzErrmsg==&pTab->base.zErrMsg ); - pConfig->pzErrmsg = &pTab->base.zErrMsg; + assert( pzErrmsg==0 || pzErrmsg==&pTab->p.base.zErrMsg ); + pConfig->pzErrmsg = &pTab->p.base.zErrMsg; - /* Decode the arguments passed through to this function. - ** - ** Note: The following set of if(...) statements must be in the same - ** order as the corresponding entries in the struct at the top of - ** fts5BestIndexMethod(). */ - if( BitFlagTest(idxNum, FTS5_BI_MATCH) ) pMatch = apVal[iVal++]; - if( BitFlagTest(idxNum, FTS5_BI_RANK) ) pRank = apVal[iVal++]; - if( BitFlagTest(idxNum, FTS5_BI_ROWID_EQ) ) pRowidEq = apVal[iVal++]; - if( BitFlagTest(idxNum, FTS5_BI_ROWID_LE) ) pRowidLe = apVal[iVal++]; - if( BitFlagTest(idxNum, FTS5_BI_ROWID_GE) ) pRowidGe = apVal[iVal++]; - assert( iVal==nVal ); + /* Decode the arguments passed through to this function. */ + for(i=0; ibPrefixInsttoken = 1; + } + + iCol = 0; + do{ + iCol = iCol*10 + (idxStr[iIdxStr]-'0'); + iIdxStr++; + }while( idxStr[iIdxStr]>='0' && idxStr[iIdxStr]<='9' ); + + if( zText[0]=='*' ){ + /* The user has issued a query of the form "MATCH '*...'". This + ** indicates that the MATCH expression is not a full text query, + ** but a request for an internal parameter. */ + rc = fts5SpecialMatch(pTab, pCsr, &zText[1]); + bInternal = 1; + }else{ + char **pzErr = &pTab->p.base.zErrMsg; + rc = sqlite3Fts5ExprNew(pConfig, 0, iCol, zText, &pExpr, pzErr); + if( rc==SQLITE_OK ){ + rc = sqlite3Fts5ExprAnd(&pCsr->pExpr, pExpr); + pExpr = 0; + } + } + + if( bFreeAndReset ){ + sqlite3_free(zText); + sqlite3Fts5ClearLocale(pConfig); + } + + if( bInternal || rc!=SQLITE_OK ) goto filter_out; + + break; + } + case 'L': + case 'G': { + int bGlob = (idxStr[iIdxStr-1]=='G'); + const char *zText = (const char*)sqlite3_value_text(apVal[i]); + iCol = 0; + do{ + iCol = iCol*10 + (idxStr[iIdxStr]-'0'); + iIdxStr++; + }while( idxStr[iIdxStr]>='0' && idxStr[iIdxStr]<='9' ); + if( zText ){ + rc = sqlite3Fts5ExprPattern(pConfig, bGlob, iCol, zText, &pExpr); + } + if( rc==SQLITE_OK ){ + rc = sqlite3Fts5ExprAnd(&pCsr->pExpr, pExpr); + pExpr = 0; + } + if( rc!=SQLITE_OK ) goto filter_out; + break; + } + case '=': + pRowidEq = apVal[i]; + break; + case '<': + pRowidLe = apVal[i]; + break; + default: assert( idxStr[iIdxStr-1]=='>' ); + pRowidGe = apVal[i]; + break; + } + } bOrderByRank = ((idxNum & FTS5_BI_ORDER_RANK) ? 1 : 0); pCsr->bDesc = bDesc = ((idxNum & FTS5_BI_ORDER_DESC) ? 1 : 0); @@ -1161,6 +1559,9 @@ static int fts5FilterMethod( pCsr->iFirstRowid = fts5GetRowidLimit(pRowidGe, SMALLEST_INT64); } + rc = sqlite3Fts5IndexLoadConfig(pTab->p.pIndex); + if( rc!=SQLITE_OK ) goto filter_out; + if( pTab->pSortCsr ){ /* If pSortCsr is non-NULL, then this call is being made as part of ** processing for a "... MATCH ORDER BY rank" query (ePlan is @@ -1169,53 +1570,45 @@ static int fts5FilterMethod( ** (pCursor) is used to execute the query issued by function ** fts5CursorFirstSorted() above. */ assert( pRowidEq==0 && pRowidLe==0 && pRowidGe==0 && pRank==0 ); - assert( nVal==0 && pMatch==0 && bOrderByRank==0 && bDesc==0 ); + assert( nVal==0 && bOrderByRank==0 && bDesc==0 ); assert( pCsr->iLastRowid==LARGEST_INT64 ); assert( pCsr->iFirstRowid==SMALLEST_INT64 ); + if( pTab->pSortCsr->bDesc ){ + pCsr->iLastRowid = pTab->pSortCsr->iFirstRowid; + pCsr->iFirstRowid = pTab->pSortCsr->iLastRowid; + }else{ + pCsr->iLastRowid = pTab->pSortCsr->iLastRowid; + pCsr->iFirstRowid = pTab->pSortCsr->iFirstRowid; + } pCsr->ePlan = FTS5_PLAN_SOURCE; pCsr->pExpr = pTab->pSortCsr->pExpr; rc = fts5CursorFirst(pTab, pCsr, bDesc); - sqlite3Fts5ExprClearEof(pCsr->pExpr); - }else if( pMatch ){ - const char *zExpr = (const char*)sqlite3_value_text(apVal[0]); - if( zExpr==0 ) zExpr = ""; - + }else if( pCsr->pExpr ){ + assert( rc==SQLITE_OK ); rc = fts5CursorParseRank(pConfig, pCsr, pRank); if( rc==SQLITE_OK ){ - if( zExpr[0]=='*' ){ - /* The user has issued a query of the form "MATCH '*...'". This - ** indicates that the MATCH expression is not a full text query, - ** but a request for an internal parameter. */ - rc = fts5SpecialMatch(pTab, pCsr, &zExpr[1]); + if( bOrderByRank ){ + pCsr->ePlan = FTS5_PLAN_SORTED_MATCH; + rc = fts5CursorFirstSorted(pTab, pCsr, bDesc); }else{ - char **pzErr = &pTab->base.zErrMsg; - rc = sqlite3Fts5ExprNew(pConfig, zExpr, &pCsr->pExpr, pzErr); - if( rc==SQLITE_OK ){ - if( bOrderByRank ){ - pCsr->ePlan = FTS5_PLAN_SORTED_MATCH; - rc = fts5CursorFirstSorted(pTab, pCsr, bDesc); - }else{ - pCsr->ePlan = FTS5_PLAN_MATCH; - rc = fts5CursorFirst(pTab, pCsr, bDesc); - } - } + pCsr->ePlan = FTS5_PLAN_MATCH; + rc = fts5CursorFirst(pTab, pCsr, bDesc); } } }else if( pConfig->zContent==0 ){ - *pConfig->pzErrmsg = sqlite3_mprintf( - "%s: table does not support scanning", pConfig->zName - ); + fts5SetVtabError(pTab,"%s: table does not support scanning",pConfig->zName); rc = SQLITE_ERROR; }else{ /* This is either a full-table scan (ePlan==FTS5_PLAN_SCAN) or a lookup ** by rowid (ePlan==FTS5_PLAN_ROWID). */ pCsr->ePlan = (pRowidEq ? FTS5_PLAN_ROWID : FTS5_PLAN_SCAN); rc = sqlite3Fts5StorageStmt( - pTab->pStorage, fts5StmtType(pCsr), &pCsr->pStmt, &pTab->base.zErrMsg + pTab->pStorage, fts5StmtType(pCsr), &pCsr->pStmt, &pTab->p.base.zErrMsg ); if( rc==SQLITE_OK ){ - if( pCsr->ePlan==FTS5_PLAN_ROWID ){ - sqlite3_bind_value(pCsr->pStmt, 1, apVal[0]); + if( pRowidEq!=0 ){ + assert( pCsr->ePlan==FTS5_PLAN_ROWID ); + sqlite3_bind_value(pCsr->pStmt, 1, pRowidEq); }else{ sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iFirstRowid); sqlite3_bind_int64(pCsr->pStmt, 2, pCsr->iLastRowid); @@ -1224,7 +1617,10 @@ static int fts5FilterMethod( } } + filter_out: + sqlite3Fts5ExprFree(pExpr); pConfig->pzErrmsg = pzErrmsg; + pConfig->bPrefixInsttoken = bPrefixInsttoken; return rc; } @@ -1244,9 +1640,13 @@ static i64 fts5CursorRowid(Fts5Cursor *pCsr){ assert( pCsr->ePlan==FTS5_PLAN_MATCH || pCsr->ePlan==FTS5_PLAN_SORTED_MATCH || pCsr->ePlan==FTS5_PLAN_SOURCE + || pCsr->ePlan==FTS5_PLAN_SCAN + || pCsr->ePlan==FTS5_PLAN_ROWID ); if( pCsr->pSorter ){ return pCsr->pSorter->iRowid; + }else if( pCsr->ePlan>=FTS5_PLAN_SCAN ){ + return sqlite3_column_int64(pCsr->pStmt, 0); }else{ return sqlite3Fts5ExprRowid(pCsr->pExpr); } @@ -1263,25 +1663,16 @@ static int fts5RowidMethod(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){ int ePlan = pCsr->ePlan; assert( CsrFlagTest(pCsr, FTS5CSR_EOF)==0 ); - switch( ePlan ){ - case FTS5_PLAN_SPECIAL: - *pRowid = 0; - break; - - case FTS5_PLAN_SOURCE: - case FTS5_PLAN_MATCH: - case FTS5_PLAN_SORTED_MATCH: - *pRowid = fts5CursorRowid(pCsr); - break; - - default: - *pRowid = sqlite3_column_int64(pCsr->pStmt, 0); - break; + if( ePlan==FTS5_PLAN_SPECIAL ){ + *pRowid = 0; + }else{ + *pRowid = fts5CursorRowid(pCsr); } return SQLITE_OK; } + /* ** If the cursor requires seeking (bSeekRequired flag is set), seek it. ** Return SQLITE_OK if no error occurs, or an SQLite error code otherwise. @@ -1294,20 +1685,23 @@ static int fts5SeekCursor(Fts5Cursor *pCsr, int bErrormsg){ /* If the cursor does not yet have a statement handle, obtain one now. */ if( pCsr->pStmt==0 ){ - Fts5Table *pTab = (Fts5Table*)(pCsr->base.pVtab); + Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab); int eStmt = fts5StmtType(pCsr); rc = sqlite3Fts5StorageStmt( - pTab->pStorage, eStmt, &pCsr->pStmt, (bErrormsg?&pTab->base.zErrMsg:0) + pTab->pStorage, eStmt, &pCsr->pStmt, (bErrormsg?&pTab->p.base.zErrMsg:0) ); - assert( rc!=SQLITE_OK || pTab->base.zErrMsg==0 ); + assert( rc!=SQLITE_OK || pTab->p.base.zErrMsg==0 ); assert( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_CONTENT) ); } if( rc==SQLITE_OK && CsrFlagTest(pCsr, FTS5CSR_REQUIRE_CONTENT) ){ + Fts5Table *pTab = (Fts5Table*)(pCsr->base.pVtab); assert( pCsr->pExpr ); sqlite3_reset(pCsr->pStmt); sqlite3_bind_int64(pCsr->pStmt, 1, fts5CursorRowid(pCsr)); + pTab->pConfig->bLock++; rc = sqlite3_step(pCsr->pStmt); + pTab->pConfig->bLock--; if( rc==SQLITE_ROW ){ rc = SQLITE_OK; CsrFlagClear(pCsr, FTS5CSR_REQUIRE_CONTENT); @@ -1315,20 +1709,21 @@ static int fts5SeekCursor(Fts5Cursor *pCsr, int bErrormsg){ rc = sqlite3_reset(pCsr->pStmt); if( rc==SQLITE_OK ){ rc = FTS5_CORRUPT; + fts5SetVtabError((Fts5FullTable*)pTab, + "fts5: missing row %lld from content table %s", + fts5CursorRowid(pCsr), + pTab->pConfig->zContent + ); + }else if( pTab->pConfig->pzErrmsg ){ + fts5SetVtabError((Fts5FullTable*)pTab, + "%s", sqlite3_errmsg(pTab->pConfig->db) + ); } } } return rc; } -static void fts5SetVtabError(Fts5Table *p, const char *zFormat, ...){ - va_list ap; /* ... printf arguments */ - va_start(ap, zFormat); - assert( p->base.zErrMsg==0 ); - p->base.zErrMsg = sqlite3_vmprintf(zFormat, ap); - va_end(ap); -} - /* ** This function is called to handle an FTS INSERT command. In other words, ** an INSERT statement of the form: @@ -1345,13 +1740,14 @@ static void fts5SetVtabError(Fts5Table *p, const char *zFormat, ...){ ** more commands are added to this function. */ static int fts5SpecialInsert( - Fts5Table *pTab, /* Fts5 table object */ + Fts5FullTable *pTab, /* Fts5 table object */ const char *zCmd, /* Text inserted into table-name column */ sqlite3_value *pVal /* Value inserted into rank column */ ){ - Fts5Config *pConfig = pTab->pConfig; + Fts5Config *pConfig = pTab->p.pConfig; int rc = SQLITE_OK; int bError = 0; + int bLoadConfig = 0; if( 0==sqlite3_stricmp("delete-all", zCmd) ){ if( pConfig->eContent==FTS5_CONTENT_NORMAL ){ @@ -1363,8 +1759,9 @@ static int fts5SpecialInsert( }else{ rc = sqlite3Fts5StorageDeleteAll(pTab->pStorage); } + bLoadConfig = 1; }else if( 0==sqlite3_stricmp("rebuild", zCmd) ){ - if( pConfig->eContent==FTS5_CONTENT_NONE ){ + if( fts5IsContentless(pTab, 1) ){ fts5SetVtabError(pTab, "'rebuild' may not be used with a contentless fts5 table" ); @@ -1372,21 +1769,28 @@ static int fts5SpecialInsert( }else{ rc = sqlite3Fts5StorageRebuild(pTab->pStorage); } + bLoadConfig = 1; }else if( 0==sqlite3_stricmp("optimize", zCmd) ){ rc = sqlite3Fts5StorageOptimize(pTab->pStorage); }else if( 0==sqlite3_stricmp("merge", zCmd) ){ int nMerge = sqlite3_value_int(pVal); rc = sqlite3Fts5StorageMerge(pTab->pStorage, nMerge); }else if( 0==sqlite3_stricmp("integrity-check", zCmd) ){ - rc = sqlite3Fts5StorageIntegrity(pTab->pStorage); + int iArg = sqlite3_value_int(pVal); + rc = sqlite3Fts5StorageIntegrity(pTab->pStorage, iArg); #ifdef SQLITE_DEBUG }else if( 0==sqlite3_stricmp("prefix-index", zCmd) ){ pConfig->bPrefixIndex = sqlite3_value_int(pVal); #endif + }else if( 0==sqlite3_stricmp("flush", zCmd) ){ + rc = sqlite3Fts5FlushToDisk(&pTab->p); }else{ - rc = sqlite3Fts5IndexLoadConfig(pTab->pIndex); + rc = sqlite3Fts5FlushToDisk(&pTab->p); + if( rc==SQLITE_OK ){ + rc = sqlite3Fts5IndexLoadConfig(pTab->p.pIndex); + } if( rc==SQLITE_OK ){ - rc = sqlite3Fts5ConfigSetValue(pTab->pConfig, zCmd, pVal, &bError); + rc = sqlite3Fts5ConfigSetValue(pTab->p.pConfig, zCmd, pVal, &bError); } if( rc==SQLITE_OK ){ if( bError ){ @@ -1396,31 +1800,37 @@ static int fts5SpecialInsert( } } } + + if( rc==SQLITE_OK && bLoadConfig ){ + pTab->p.pConfig->iCookie--; + rc = sqlite3Fts5IndexLoadConfig(pTab->p.pIndex); + } + return rc; } static int fts5SpecialDelete( - Fts5Table *pTab, + Fts5FullTable *pTab, sqlite3_value **apVal ){ int rc = SQLITE_OK; int eType1 = sqlite3_value_type(apVal[1]); if( eType1==SQLITE_INTEGER ){ sqlite3_int64 iDel = sqlite3_value_int64(apVal[1]); - rc = sqlite3Fts5StorageDelete(pTab->pStorage, iDel, &apVal[2]); + rc = sqlite3Fts5StorageDelete(pTab->pStorage, iDel, &apVal[2], 0); } return rc; } static void fts5StorageInsert( int *pRc, - Fts5Table *pTab, + Fts5FullTable *pTab, sqlite3_value **apVal, i64 *piRowid ){ int rc = *pRc; if( rc==SQLITE_OK ){ - rc = sqlite3Fts5StorageContentInsert(pTab->pStorage, apVal, piRowid); + rc = sqlite3Fts5StorageContentInsert(pTab->pStorage, 0, apVal, piRowid); } if( rc==SQLITE_OK ){ rc = sqlite3Fts5StorageIndexInsert(pTab->pStorage, apVal, *piRowid); @@ -1428,6 +1838,67 @@ static void fts5StorageInsert( *pRc = rc; } +/* +** +** This function is called when the user attempts an UPDATE on a contentless +** table. Parameter bRowidModified is true if the UPDATE statement modifies +** the rowid value. Parameter apVal[] contains the new values for each user +** defined column of the fts5 table. pConfig is the configuration object of the +** table being updated (guaranteed to be contentless). The contentless_delete=1 +** and contentless_unindexed=1 options may or may not be set. +** +** This function returns SQLITE_OK if the UPDATE can go ahead, or an SQLite +** error code if it cannot. In this case an error message is also loaded into +** pConfig. Output parameter (*pbContent) is set to true if the caller should +** update the %_content table only - not the FTS index or any other shadow +** table. This occurs when an UPDATE modifies only UNINDEXED columns of the +** table. +** +** An UPDATE may proceed if: +** +** * The only columns modified are UNINDEXED columns, or +** +** * The contentless_delete=1 option was specified and all of the indexed +** columns (not a subset) have been modified. +*/ +static int fts5ContentlessUpdate( + Fts5Config *pConfig, + sqlite3_value **apVal, + int bRowidModified, + int *pbContent +){ + int ii; + int bSeenIndex = 0; /* Have seen modified indexed column */ + int bSeenIndexNC = 0; /* Have seen unmodified indexed column */ + int rc = SQLITE_OK; + + for(ii=0; iinCol; ii++){ + if( pConfig->abUnindexed[ii]==0 ){ + if( sqlite3_value_nochange(apVal[ii]) ){ + bSeenIndexNC++; + }else{ + bSeenIndex++; + } + } + } + + if( bSeenIndex==0 && bRowidModified==0 ){ + *pbContent = 1; + }else{ + if( bSeenIndexNC || pConfig->bContentlessDelete==0 ){ + rc = SQLITE_ERROR; + sqlite3Fts5ConfigErrmsg(pConfig, + (pConfig->bContentlessDelete ? + "%s a subset of columns on fts5 contentless-delete table: %s" : + "%s contentless fts5 table: %s") + , "cannot UPDATE", pConfig->zName + ); + } + } + + return rc; +} + /* ** This function is the implementation of the xUpdate callback used by ** FTS3 virtual tables. It is invoked by SQLite each time a row is to be @@ -1448,22 +1919,26 @@ static int fts5UpdateMethod( sqlite3_value **apVal, /* Array of arguments */ sqlite_int64 *pRowid /* OUT: The affected (or effected) rowid */ ){ - Fts5Table *pTab = (Fts5Table*)pVtab; - Fts5Config *pConfig = pTab->pConfig; + Fts5FullTable *pTab = (Fts5FullTable*)pVtab; + Fts5Config *pConfig = pTab->p.pConfig; int eType0; /* value_type() of apVal[0] */ int rc = SQLITE_OK; /* Return code */ /* A transaction must be open when this is called. */ - assert( pTab->ts.eState==1 ); + assert( pTab->ts.eState==1 || pTab->ts.eState==2 ); assert( pVtab->zErrMsg==0 ); assert( nArg==1 || nArg==(2+pConfig->nCol+2) ); - assert( nArg==1 - || sqlite3_value_type(apVal[1])==SQLITE_INTEGER - || sqlite3_value_type(apVal[1])==SQLITE_NULL + assert( sqlite3_value_type(apVal[0])==SQLITE_INTEGER + || sqlite3_value_type(apVal[0])==SQLITE_NULL ); - assert( pTab->pConfig->pzErrmsg==0 ); - pTab->pConfig->pzErrmsg = &pTab->base.zErrMsg; + assert( pTab->p.pConfig->pzErrmsg==0 ); + if( pConfig->pgsz==0 ){ + rc = sqlite3Fts5ConfigLoad(pTab->p.pConfig, pTab->p.pConfig->iCookie); + if( rc!=SQLITE_OK ) return rc; + } + + pTab->p.pConfig->pzErrmsg = &pTab->p.base.zErrMsg; /* Put any active cursors into REQUIRE_SEEK state. */ fts5TripCursors(pTab); @@ -1477,7 +1952,14 @@ static int fts5UpdateMethod( if( pConfig->eContent!=FTS5_CONTENT_NORMAL && 0==sqlite3_stricmp("delete", z) ){ - rc = fts5SpecialDelete(pTab, apVal); + if( pConfig->bContentlessDelete ){ + fts5SetVtabError(pTab, + "'delete' may not be used with a contentless_delete=1 table" + ); + rc = SQLITE_ERROR; + }else{ + rc = fts5SpecialDelete(pTab, apVal); + } }else{ rc = fts5SpecialInsert(pTab, z, apVal[2 + pConfig->nCol + 1]); } @@ -1494,69 +1976,112 @@ static int fts5UpdateMethod( ** Cases 3 and 4 may violate the rowid constraint. */ int eConflict = SQLITE_ABORT; - if( pConfig->eContent==FTS5_CONTENT_NORMAL ){ + if( pConfig->eContent==FTS5_CONTENT_NORMAL || pConfig->bContentlessDelete ){ eConflict = sqlite3_vtab_on_conflict(pConfig->db); } assert( eType0==SQLITE_INTEGER || eType0==SQLITE_NULL ); assert( nArg!=1 || eType0==SQLITE_INTEGER ); - /* Filter out attempts to run UPDATE or DELETE on contentless tables. - ** This is not suported. */ - if( eType0==SQLITE_INTEGER && fts5IsContentless(pTab) ){ - pTab->base.zErrMsg = sqlite3_mprintf( - "cannot %s contentless fts5 table: %s", - (nArg>1 ? "UPDATE" : "DELETE from"), pConfig->zName - ); - rc = SQLITE_ERROR; - } - - /* Case 1: DELETE */ - else if( nArg==1 ){ - i64 iDel = sqlite3_value_int64(apVal[0]); /* Rowid to delete */ - rc = sqlite3Fts5StorageDelete(pTab->pStorage, iDel, 0); - } - - /* Case 2: INSERT */ - else if( eType0!=SQLITE_INTEGER ){ - /* If this is a REPLACE, first remove the current entry (if any) */ - if( eConflict==SQLITE_REPLACE - && sqlite3_value_type(apVal[1])==SQLITE_INTEGER - ){ - i64 iNew = sqlite3_value_int64(apVal[1]); /* Rowid to delete */ - rc = sqlite3Fts5StorageDelete(pTab->pStorage, iNew, 0); + /* DELETE */ + if( nArg==1 ){ + /* It is only possible to DELETE from a contentless table if the + ** contentless_delete=1 flag is set. */ + if( fts5IsContentless(pTab, 1) && pConfig->bContentlessDelete==0 ){ + fts5SetVtabError(pTab, + "cannot DELETE from contentless fts5 table: %s", pConfig->zName + ); + rc = SQLITE_ERROR; + }else{ + i64 iDel = sqlite3_value_int64(apVal[0]); /* Rowid to delete */ + rc = sqlite3Fts5StorageDelete(pTab->pStorage, iDel, 0, 0); } - fts5StorageInsert(&rc, pTab, apVal, pRowid); } - /* Case 2: UPDATE */ + /* INSERT or UPDATE */ else{ - i64 iOld = sqlite3_value_int64(apVal[0]); /* Old rowid */ - i64 iNew = sqlite3_value_int64(apVal[1]); /* New rowid */ - if( iOld!=iNew ){ - if( eConflict==SQLITE_REPLACE ){ - rc = sqlite3Fts5StorageDelete(pTab->pStorage, iOld, 0); - if( rc==SQLITE_OK ){ - rc = sqlite3Fts5StorageDelete(pTab->pStorage, iNew, 0); - } - fts5StorageInsert(&rc, pTab, apVal, pRowid); - }else{ - rc = sqlite3Fts5StorageContentInsert(pTab->pStorage, apVal, pRowid); - if( rc==SQLITE_OK ){ - rc = sqlite3Fts5StorageDelete(pTab->pStorage, iOld, 0); - } - if( rc==SQLITE_OK ){ - rc = sqlite3Fts5StorageIndexInsert(pTab->pStorage, apVal, *pRowid); + int eType1 = sqlite3_value_numeric_type(apVal[1]); + + /* It is an error to write an fts5_locale() value to a table without + ** the locale=1 option. */ + if( pConfig->bLocale==0 ){ + int ii; + for(ii=0; iinCol; ii++){ + sqlite3_value *pVal = apVal[ii+2]; + if( sqlite3Fts5IsLocaleValue(pConfig, pVal) ){ + fts5SetVtabError(pTab, "fts5_locale() requires locale=1"); + rc = SQLITE_MISMATCH; + goto update_out; } } - }else{ - rc = sqlite3Fts5StorageDelete(pTab->pStorage, iOld, 0); + } + + if( eType0!=SQLITE_INTEGER ){ + /* An INSERT statement. If the conflict-mode is REPLACE, first remove + ** the current entry (if any). */ + if( eConflict==SQLITE_REPLACE && eType1==SQLITE_INTEGER ){ + i64 iNew = sqlite3_value_int64(apVal[1]); /* Rowid to delete */ + rc = sqlite3Fts5StorageDelete(pTab->pStorage, iNew, 0, 0); + } fts5StorageInsert(&rc, pTab, apVal, pRowid); } + + /* UPDATE */ + else{ + Fts5Storage *pStorage = pTab->pStorage; + i64 iOld = sqlite3_value_int64(apVal[0]); /* Old rowid */ + i64 iNew = sqlite3_value_int64(apVal[1]); /* New rowid */ + int bContent = 0; /* Content only update */ + + /* If this is a contentless table (including contentless_unindexed=1 + ** tables), check if the UPDATE may proceed. */ + if( fts5IsContentless(pTab, 1) ){ + rc = fts5ContentlessUpdate(pConfig, &apVal[2], iOld!=iNew, &bContent); + if( rc!=SQLITE_OK ) goto update_out; + } + + if( eType1!=SQLITE_INTEGER ){ + rc = SQLITE_MISMATCH; + }else if( iOld!=iNew ){ + assert( bContent==0 ); + if( eConflict==SQLITE_REPLACE ){ + rc = sqlite3Fts5StorageDelete(pStorage, iOld, 0, 1); + if( rc==SQLITE_OK ){ + rc = sqlite3Fts5StorageDelete(pStorage, iNew, 0, 0); + } + fts5StorageInsert(&rc, pTab, apVal, pRowid); + }else{ + rc = sqlite3Fts5StorageFindDeleteRow(pStorage, iOld); + if( rc==SQLITE_OK ){ + rc = sqlite3Fts5StorageContentInsert(pStorage, 0, apVal, pRowid); + } + if( rc==SQLITE_OK ){ + rc = sqlite3Fts5StorageDelete(pStorage, iOld, 0, 0); + } + if( rc==SQLITE_OK ){ + rc = sqlite3Fts5StorageIndexInsert(pStorage, apVal, *pRowid); + } + } + }else if( bContent ){ + /* This occurs when an UPDATE on a contentless table affects *only* + ** UNINDEXED columns. This is a no-op for contentless_unindexed=0 + ** tables, or a write to the %_content table only for =1 tables. */ + assert( fts5IsContentless(pTab, 1) ); + rc = sqlite3Fts5StorageFindDeleteRow(pStorage, iOld); + if( rc==SQLITE_OK ){ + rc = sqlite3Fts5StorageContentInsert(pStorage, 1, apVal, pRowid); + } + }else{ + rc = sqlite3Fts5StorageDelete(pStorage, iOld, 0, 1); + fts5StorageInsert(&rc, pTab, apVal, pRowid); + } + sqlite3Fts5StorageReleaseDeleteRow(pStorage); + } } } - pTab->pConfig->pzErrmsg = 0; + update_out: + pTab->p.pConfig->pzErrmsg = 0; return rc; } @@ -1565,12 +2090,11 @@ static int fts5UpdateMethod( */ static int fts5SyncMethod(sqlite3_vtab *pVtab){ int rc; - Fts5Table *pTab = (Fts5Table*)pVtab; + Fts5FullTable *pTab = (Fts5FullTable*)pVtab; fts5CheckTransactionState(pTab, FTS5_SYNC, 0); - pTab->pConfig->pzErrmsg = &pTab->base.zErrMsg; - fts5TripCursors(pTab); - rc = sqlite3Fts5StorageSync(pTab->pStorage, 1); - pTab->pConfig->pzErrmsg = 0; + pTab->p.pConfig->pzErrmsg = &pTab->p.base.zErrMsg; + rc = sqlite3Fts5FlushToDisk(&pTab->p); + pTab->p.pConfig->pzErrmsg = 0; return rc; } @@ -1578,9 +2102,11 @@ static int fts5SyncMethod(sqlite3_vtab *pVtab){ ** Implementation of xBegin() method. */ static int fts5BeginMethod(sqlite3_vtab *pVtab){ - UNUSED_PARAM(pVtab); /* Call below is a no-op for NDEBUG builds */ - fts5CheckTransactionState((Fts5Table*)pVtab, FTS5_BEGIN, 0); - return SQLITE_OK; + int rc = fts5NewTransaction((Fts5FullTable*)pVtab); + if( rc==SQLITE_OK ){ + fts5CheckTransactionState((Fts5FullTable*)pVtab, FTS5_BEGIN, 0); + } + return rc; } /* @@ -1590,7 +2116,7 @@ static int fts5BeginMethod(sqlite3_vtab *pVtab){ */ static int fts5CommitMethod(sqlite3_vtab *pVtab){ UNUSED_PARAM(pVtab); /* Call below is a no-op for NDEBUG builds */ - fts5CheckTransactionState((Fts5Table*)pVtab, FTS5_COMMIT, 0); + fts5CheckTransactionState((Fts5FullTable*)pVtab, FTS5_COMMIT, 0); return SQLITE_OK; } @@ -1600,9 +2126,10 @@ static int fts5CommitMethod(sqlite3_vtab *pVtab){ */ static int fts5RollbackMethod(sqlite3_vtab *pVtab){ int rc; - Fts5Table *pTab = (Fts5Table*)pVtab; + Fts5FullTable *pTab = (Fts5FullTable*)pVtab; fts5CheckTransactionState(pTab, FTS5_ROLLBACK, 0); rc = sqlite3Fts5StorageRollback(pTab->pStorage); + pTab->p.pConfig->pgsz = 0; return rc; } @@ -1624,27 +2151,50 @@ static int fts5ApiColumnTotalSize( sqlite3_int64 *pnToken ){ Fts5Cursor *pCsr = (Fts5Cursor*)pCtx; - Fts5Table *pTab = (Fts5Table*)(pCsr->base.pVtab); + Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab); return sqlite3Fts5StorageSize(pTab->pStorage, iCol, pnToken); } static int fts5ApiRowCount(Fts5Context *pCtx, i64 *pnRow){ Fts5Cursor *pCsr = (Fts5Cursor*)pCtx; - Fts5Table *pTab = (Fts5Table*)(pCsr->base.pVtab); + Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab); return sqlite3Fts5StorageRowCount(pTab->pStorage, pnRow); } -static int fts5ApiTokenize( +/* +** Implementation of xTokenize_v2() API. +*/ +static int fts5ApiTokenize_v2( Fts5Context *pCtx, const char *pText, int nText, + const char *pLoc, int nLoc, void *pUserData, int (*xToken)(void*, int, const char*, int, int, int) ){ Fts5Cursor *pCsr = (Fts5Cursor*)pCtx; Fts5Table *pTab = (Fts5Table*)(pCsr->base.pVtab); - return sqlite3Fts5Tokenize( - pTab->pConfig, FTS5_TOKENIZE_AUX, pText, nText, pUserData, xToken + int rc = SQLITE_OK; + + sqlite3Fts5SetLocale(pTab->pConfig, pLoc, nLoc); + rc = sqlite3Fts5Tokenize(pTab->pConfig, + FTS5_TOKENIZE_AUX, pText, nText, pUserData, xToken ); + sqlite3Fts5SetLocale(pTab->pConfig, 0, 0); + + return rc; +} + +/* +** Implementation of xTokenize() API. This is just xTokenize_v2() with NULL/0 +** passed as the locale. +*/ +static int fts5ApiTokenize( + Fts5Context *pCtx, + const char *pText, int nText, + void *pUserData, + int (*xToken)(void*, int, const char*, int, int, int) +){ + return fts5ApiTokenize_v2(pCtx, pText, nText, 0, 0, pUserData, xToken); } static int fts5ApiPhraseCount(Fts5Context *pCtx){ @@ -1657,6 +2207,49 @@ static int fts5ApiPhraseSize(Fts5Context *pCtx, int iPhrase){ return sqlite3Fts5ExprPhraseSize(pCsr->pExpr, iPhrase); } +/* +** Argument pStmt is an SQL statement of the type used by Fts5Cursor. This +** function extracts the text value of column iCol of the current row. +** Additionally, if there is an associated locale, it invokes +** sqlite3Fts5SetLocale() to configure the tokenizer. In all cases the caller +** should invoke sqlite3Fts5ClearLocale() to clear the locale at some point +** after this function returns. +** +** If successful, (*ppText) is set to point to a buffer containing the text +** value as utf-8 and SQLITE_OK returned. (*pnText) is set to the size of that +** buffer in bytes. It is not guaranteed to be nul-terminated. If an error +** occurs, an SQLite error code is returned. The final values of the two +** output parameters are undefined in this case. +*/ +static int fts5TextFromStmt( + Fts5Config *pConfig, + sqlite3_stmt *pStmt, + int iCol, + const char **ppText, + int *pnText +){ + sqlite3_value *pVal = sqlite3_column_value(pStmt, iCol+1); + const char *pLoc = 0; + int nLoc = 0; + int rc = SQLITE_OK; + + if( pConfig->bLocale + && pConfig->eContent==FTS5_CONTENT_EXTERNAL + && sqlite3Fts5IsLocaleValue(pConfig, pVal) + ){ + rc = sqlite3Fts5DecodeLocaleValue(pVal, ppText, pnText, &pLoc, &nLoc); + }else{ + *ppText = (const char*)sqlite3_value_text(pVal); + *pnText = sqlite3_value_bytes(pVal); + if( pConfig->bLocale && pConfig->eContent==FTS5_CONTENT_NORMAL ){ + pLoc = (const char*)sqlite3_column_text(pStmt, iCol+1+pConfig->nCol); + nLoc = sqlite3_column_bytes(pStmt, iCol+1+pConfig->nCol); + } + } + sqlite3Fts5SetLocale(pConfig, pLoc, nLoc); + return rc; +} + static int fts5ApiColumnText( Fts5Context *pCtx, int iCol, @@ -1665,44 +2258,69 @@ static int fts5ApiColumnText( ){ int rc = SQLITE_OK; Fts5Cursor *pCsr = (Fts5Cursor*)pCtx; - if( fts5IsContentless((Fts5Table*)(pCsr->base.pVtab)) ){ + Fts5Table *pTab = (Fts5Table*)(pCsr->base.pVtab); + + assert( pCsr->ePlan!=FTS5_PLAN_SPECIAL ); + if( iCol<0 || iCol>=pTab->pConfig->nCol ){ + rc = SQLITE_RANGE; + }else if( fts5IsContentless((Fts5FullTable*)(pCsr->base.pVtab), 0) ){ *pz = 0; *pn = 0; }else{ rc = fts5SeekCursor(pCsr, 0); if( rc==SQLITE_OK ){ - *pz = (const char*)sqlite3_column_text(pCsr->pStmt, iCol+1); - *pn = sqlite3_column_bytes(pCsr->pStmt, iCol+1); + rc = fts5TextFromStmt(pTab->pConfig, pCsr->pStmt, iCol, pz, pn); + sqlite3Fts5ClearLocale(pTab->pConfig); } } return rc; } +/* +** This is called by various API functions - xInst, xPhraseFirst, +** xPhraseFirstColumn etc. - to obtain the position list for phrase iPhrase +** of the current row. This function works for both detail=full tables (in +** which case the position-list was read from the fts index) or for other +** detail= modes if the row content is available. +*/ static int fts5CsrPoslist( - Fts5Cursor *pCsr, - int iPhrase, - const u8 **pa, - int *pn + Fts5Cursor *pCsr, /* Fts5 cursor object */ + int iPhrase, /* Phrase to find position list for */ + const u8 **pa, /* OUT: Pointer to position list buffer */ + int *pn /* OUT: Size of (*pa) in bytes */ ){ Fts5Config *pConfig = ((Fts5Table*)(pCsr->base.pVtab))->pConfig; int rc = SQLITE_OK; int bLive = (pCsr->pSorter==0); - if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_POSLIST) ){ - + if( iPhrase<0 || iPhrase>=sqlite3Fts5ExprPhraseCount(pCsr->pExpr) ){ + rc = SQLITE_RANGE; + }else if( pConfig->eDetail!=FTS5_DETAIL_FULL + && fts5IsContentless((Fts5FullTable*)pCsr->base.pVtab, 1) + ){ + *pa = 0; + *pn = 0; + return SQLITE_OK; + }else if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_POSLIST) ){ if( pConfig->eDetail!=FTS5_DETAIL_FULL ){ Fts5PoslistPopulator *aPopulator; int i; + aPopulator = sqlite3Fts5ExprClearPoslists(pCsr->pExpr, bLive); if( aPopulator==0 ) rc = SQLITE_NOMEM; + if( rc==SQLITE_OK ){ + rc = fts5SeekCursor(pCsr, 0); + } for(i=0; inCol && rc==SQLITE_OK; i++){ - int n; const char *z; - rc = fts5ApiColumnText((Fts5Context*)pCsr, i, &z, &n); + const char *z = 0; + int n = 0; + rc = fts5TextFromStmt(pConfig, pCsr->pStmt, i, &z, &n); if( rc==SQLITE_OK ){ rc = sqlite3Fts5ExprPopulatePoslists( pConfig, pCsr->pExpr, aPopulator, i, z, n ); } + sqlite3Fts5ClearLocale(pConfig); } sqlite3_free(aPopulator); @@ -1713,13 +2331,18 @@ static int fts5CsrPoslist( CsrFlagClear(pCsr, FTS5CSR_REQUIRE_POSLIST); } - if( pCsr->pSorter && pConfig->eDetail==FTS5_DETAIL_FULL ){ - Fts5Sorter *pSorter = pCsr->pSorter; - int i1 = (iPhrase==0 ? 0 : pSorter->aIdx[iPhrase-1]); - *pn = pSorter->aIdx[iPhrase] - i1; - *pa = &pSorter->aPoslist[i1]; + if( rc==SQLITE_OK ){ + if( pCsr->pSorter && pConfig->eDetail==FTS5_DETAIL_FULL ){ + Fts5Sorter *pSorter = pCsr->pSorter; + int i1 = (iPhrase==0 ? 0 : pSorter->aIdx[iPhrase-1]); + *pn = pSorter->aIdx[iPhrase] - i1; + *pa = &pSorter->aPoslist[i1]; + }else{ + *pn = sqlite3Fts5ExprPoslist(pCsr->pExpr, iPhrase, pa); + } }else{ - *pn = sqlite3Fts5ExprPoslist(pCsr->pExpr, iPhrase, pa); + *pa = 0; + *pn = 0; } return rc; @@ -1734,10 +2357,11 @@ static int fts5CacheInstArray(Fts5Cursor *pCsr){ int rc = SQLITE_OK; Fts5PoslistReader *aIter; /* One iterator for each phrase */ int nIter; /* Number of iterators/phrases */ + int nCol = ((Fts5Table*)pCsr->base.pVtab)->pConfig->nCol; nIter = sqlite3Fts5ExprPhraseCount(pCsr->pExpr); if( pCsr->aInstIter==0 ){ - int nByte = sizeof(Fts5PoslistReader) * nIter; + sqlite3_int64 nByte = sizeof(Fts5PoslistReader) * nIter; pCsr->aInstIter = (Fts5PoslistReader*)sqlite3Fts5MallocZero(&rc, nByte); } aIter = pCsr->aInstIter; @@ -1771,13 +2395,15 @@ static int fts5CacheInstArray(Fts5Cursor *pCsr){ nInst++; if( nInst>=pCsr->nInstAlloc ){ - pCsr->nInstAlloc = pCsr->nInstAlloc ? pCsr->nInstAlloc*2 : 32; - aInst = (int*)sqlite3_realloc( - pCsr->aInst, pCsr->nInstAlloc*sizeof(int)*3 + int nNewSize = pCsr->nInstAlloc ? pCsr->nInstAlloc*2 : 32; + aInst = (int*)sqlite3_realloc64( + pCsr->aInst, nNewSize*sizeof(int)*3 ); if( aInst ){ pCsr->aInst = aInst; + pCsr->nInstAlloc = nNewSize; }else{ + nInst--; rc = SQLITE_NOMEM; break; } @@ -1787,6 +2413,11 @@ static int fts5CacheInstArray(Fts5Cursor *pCsr){ aInst[0] = iBest; aInst[1] = FTS5_POS2COLUMN(aIter[iBest].iPos); aInst[2] = FTS5_POS2OFFSET(aIter[iBest].iPos); + assert( aInst[1]>=0 ); + if( aInst[1]>=nCol ){ + rc = FTS5_CORRUPT; + break; + } sqlite3Fts5PoslistReaderNext(&aIter[iBest]); } } @@ -1821,12 +2452,6 @@ static int fts5ApiInst( ){ if( iIdx<0 || iIdx>=pCsr->nInstCount ){ rc = SQLITE_RANGE; -#if 0 - }else if( fts5IsOffsetless((Fts5Table*)pCsr->base.pVtab) ){ - *piPhrase = pCsr->aInst[iIdx*3]; - *piCol = pCsr->aInst[iIdx*3 + 2]; - *piOff = -1; -#endif }else{ *piPhrase = pCsr->aInst[iIdx*3]; *piCol = pCsr->aInst[iIdx*3 + 1]; @@ -1859,15 +2484,15 @@ static int fts5ColumnSizeCb( static int fts5ApiColumnSize(Fts5Context *pCtx, int iCol, int *pnToken){ Fts5Cursor *pCsr = (Fts5Cursor*)pCtx; - Fts5Table *pTab = (Fts5Table*)(pCsr->base.pVtab); - Fts5Config *pConfig = pTab->pConfig; + Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab); + Fts5Config *pConfig = pTab->p.pConfig; int rc = SQLITE_OK; if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_DOCSIZE) ){ if( pConfig->bColumnsize ){ i64 iRowid = fts5CursorRowid(pCsr); rc = sqlite3Fts5StorageDocsize(pTab->pStorage, iRowid, pCsr->aColumnSize); - }else if( pConfig->zContent==0 ){ + }else if( !pConfig->zContent || pConfig->eContent==FTS5_CONTENT_UNINDEXED ){ int i; for(i=0; inCol; i++){ if( pConfig->abUnindexed[i]==0 ){ @@ -1876,17 +2501,19 @@ static int fts5ApiColumnSize(Fts5Context *pCtx, int iCol, int *pnToken){ } }else{ int i; + rc = fts5SeekCursor(pCsr, 0); for(i=0; rc==SQLITE_OK && inCol; i++){ if( pConfig->abUnindexed[i]==0 ){ - const char *z; int n; - void *p = (void*)(&pCsr->aColumnSize[i]); + const char *z = 0; + int n = 0; pCsr->aColumnSize[i] = 0; - rc = fts5ApiColumnText(pCtx, i, &z, &n); + rc = fts5TextFromStmt(pConfig, pCsr->pStmt, i, &z, &n); if( rc==SQLITE_OK ){ - rc = sqlite3Fts5Tokenize( - pConfig, FTS5_TOKENIZE_AUX, z, n, p, fts5ColumnSizeCb + rc = sqlite3Fts5Tokenize(pConfig, FTS5_TOKENIZE_AUX, + z, n, (void*)&pCsr->aColumnSize[i], fts5ColumnSizeCb ); } + sqlite3Fts5ClearLocale(pConfig); } } } @@ -1966,11 +2593,10 @@ static void *fts5ApiGetAuxdata(Fts5Context *pCtx, int bClear){ } static void fts5ApiPhraseNext( - Fts5Context *pUnused, + Fts5Context *pCtx, Fts5PhraseIter *pIter, int *piCol, int *piOff ){ - UNUSED_PARAM(pUnused); if( pIter->a>=pIter->b ){ *piCol = -1; *piOff = -1; @@ -1978,8 +2604,12 @@ static void fts5ApiPhraseNext( int iVal; pIter->a += fts5GetVarint32(pIter->a, iVal); if( iVal==1 ){ + /* Avoid returning a (*piCol) value that is too large for the table, + ** even if the position-list is corrupt. The caller might not be + ** expecting it. */ + int nCol = ((Fts5Table*)(((Fts5Cursor*)pCtx)->base.pVtab))->pConfig->nCol; pIter->a += fts5GetVarint32(pIter->a, iVal); - *piCol = iVal; + *piCol = (iVal>=nCol ? nCol-1 : iVal); *piOff = 0; pIter->a += fts5GetVarint32(pIter->a, iVal); } @@ -1997,7 +2627,8 @@ static int fts5ApiPhraseFirst( int n; int rc = fts5CsrPoslist(pCsr, iPhrase, &pIter->a, &n); if( rc==SQLITE_OK ){ - pIter->b = &pIter->a[n]; + assert( pIter->a || n==0 ); + pIter->b = (pIter->a ? &pIter->a[n] : 0); *piCol = 0; *piOff = 0; fts5ApiPhraseNext(pCtx, pIter, piCol, piOff); @@ -2056,7 +2687,8 @@ static int fts5ApiPhraseFirstColumn( rc = sqlite3Fts5ExprPhraseCollist(pCsr->pExpr, iPhrase, &pIter->a, &n); } if( rc==SQLITE_OK ){ - pIter->b = &pIter->a[n]; + assert( pIter->a || n==0 ); + pIter->b = (pIter->a ? &pIter->a[n] : 0); *piCol = 0; fts5ApiPhraseNextColumn(pCtx, pIter, piCol); } @@ -2064,7 +2696,8 @@ static int fts5ApiPhraseFirstColumn( int n; rc = fts5CsrPoslist(pCsr, iPhrase, &pIter->a, &n); if( rc==SQLITE_OK ){ - pIter->b = &pIter->a[n]; + assert( pIter->a || n==0 ); + pIter->b = (pIter->a ? &pIter->a[n] : 0); if( n<=0 ){ *piCol = -1; }else if( pIter->a[0]==0x01 ){ @@ -2078,13 +2711,96 @@ static int fts5ApiPhraseFirstColumn( return rc; } +/* +** xQueryToken() API implemenetation. +*/ +static int fts5ApiQueryToken( + Fts5Context* pCtx, + int iPhrase, + int iToken, + const char **ppOut, + int *pnOut +){ + Fts5Cursor *pCsr = (Fts5Cursor*)pCtx; + return sqlite3Fts5ExprQueryToken(pCsr->pExpr, iPhrase, iToken, ppOut, pnOut); +} + +/* +** xInstToken() API implemenetation. +*/ +static int fts5ApiInstToken( + Fts5Context *pCtx, + int iIdx, + int iToken, + const char **ppOut, int *pnOut +){ + Fts5Cursor *pCsr = (Fts5Cursor*)pCtx; + int rc = SQLITE_OK; + if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_INST)==0 + || SQLITE_OK==(rc = fts5CacheInstArray(pCsr)) + ){ + if( iIdx<0 || iIdx>=pCsr->nInstCount ){ + rc = SQLITE_RANGE; + }else{ + int iPhrase = pCsr->aInst[iIdx*3]; + int iCol = pCsr->aInst[iIdx*3 + 1]; + int iOff = pCsr->aInst[iIdx*3 + 2]; + i64 iRowid = fts5CursorRowid(pCsr); + rc = sqlite3Fts5ExprInstToken( + pCsr->pExpr, iRowid, iPhrase, iCol, iOff, iToken, ppOut, pnOut + ); + } + } + return rc; +} + static int fts5ApiQueryPhrase(Fts5Context*, int, void*, int(*)(const Fts5ExtensionApi*, Fts5Context*, void*) ); +/* +** The xColumnLocale() API. +*/ +static int fts5ApiColumnLocale( + Fts5Context *pCtx, + int iCol, + const char **pzLocale, + int *pnLocale +){ + int rc = SQLITE_OK; + Fts5Cursor *pCsr = (Fts5Cursor*)pCtx; + Fts5Config *pConfig = ((Fts5Table*)(pCsr->base.pVtab))->pConfig; + + *pzLocale = 0; + *pnLocale = 0; + + assert( pCsr->ePlan!=FTS5_PLAN_SPECIAL ); + if( iCol<0 || iCol>=pConfig->nCol ){ + rc = SQLITE_RANGE; + }else if( + pConfig->abUnindexed[iCol]==0 + && 0==fts5IsContentless((Fts5FullTable*)pCsr->base.pVtab, 1) + && pConfig->bLocale + ){ + rc = fts5SeekCursor(pCsr, 0); + if( rc==SQLITE_OK ){ + const char *zDummy = 0; + int nDummy = 0; + rc = fts5TextFromStmt(pConfig, pCsr->pStmt, iCol, &zDummy, &nDummy); + if( rc==SQLITE_OK ){ + *pzLocale = pConfig->t.pLocale; + *pnLocale = pConfig->t.nLocale; + } + sqlite3Fts5ClearLocale(pConfig); + } + } + + return rc; +} + static const Fts5ExtensionApi sFts5Api = { - 2, /* iVersion */ + 4, /* iVersion */ fts5ApiUserData, fts5ApiColumnCount, fts5ApiRowCount, @@ -2104,6 +2820,10 @@ static const Fts5ExtensionApi sFts5Api = { fts5ApiPhraseNext, fts5ApiPhraseFirstColumn, fts5ApiPhraseNextColumn, + fts5ApiQueryToken, + fts5ApiInstToken, + fts5ApiColumnLocale, + fts5ApiTokenize_v2 }; /* @@ -2116,7 +2836,7 @@ static int fts5ApiQueryPhrase( int(*xCallback)(const Fts5ExtensionApi*, Fts5Context*, void*) ){ Fts5Cursor *pCsr = (Fts5Cursor*)pCtx; - Fts5Table *pTab = (Fts5Table*)(pCsr->base.pVtab); + Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab); int rc; Fts5Cursor *pNew = 0; @@ -2154,6 +2874,7 @@ static void fts5ApiInvoke( sqlite3_value **argv ){ assert( pCsr->pAux==0 ); + assert( pCsr->ePlan!=FTS5_PLAN_SPECIAL ); pCsr->pAux = pAux; pAux->xFunc(&sFts5Api, (Fts5Context*)pCsr, context, argc, argv); pCsr->pAux = 0; @@ -2167,6 +2888,21 @@ static Fts5Cursor *fts5CursorFromCsrid(Fts5Global *pGlobal, i64 iCsrId){ return pCsr; } +/* +** Parameter zFmt is a printf() style formatting string. This function +** formats it using the trailing arguments and returns the result as +** an error message to the context passed as the first argument. +*/ +static void fts5ResultError(sqlite3_context *pCtx, const char *zFmt, ...){ + char *zErr = 0; + va_list ap; + va_start(ap, zFmt); + zErr = sqlite3_vmprintf(zFmt, ap); + sqlite3_result_error(pCtx, zErr, -1); + sqlite3_free(zErr); + va_end(ap); +} + static void fts5ApiCallback( sqlite3_context *context, int argc, @@ -2182,36 +2918,31 @@ static void fts5ApiCallback( iCsrId = sqlite3_value_int64(argv[0]); pCsr = fts5CursorFromCsrid(pAux->pGlobal, iCsrId); - if( pCsr==0 ){ - char *zErr = sqlite3_mprintf("no such cursor: %lld", iCsrId); - sqlite3_result_error(context, zErr, -1); - sqlite3_free(zErr); + if( pCsr==0 || (pCsr->ePlan==0 || pCsr->ePlan==FTS5_PLAN_SPECIAL) ){ + fts5ResultError(context, "no such cursor: %lld", iCsrId); }else{ + sqlite3_vtab *pTab = pCsr->base.pVtab; fts5ApiInvoke(pAux, pCsr, context, argc-1, &argv[1]); + sqlite3_free(pTab->zErrMsg); + pTab->zErrMsg = 0; } } /* -** Given cursor id iId, return a pointer to the corresponding Fts5Index +** Given cursor id iId, return a pointer to the corresponding Fts5Table ** object. Or NULL If the cursor id does not exist. -** -** If successful, set *ppConfig to point to the associated config object -** before returning. */ -Fts5Index *sqlite3Fts5IndexFromCsrid( +Fts5Table *sqlite3Fts5TableFromCsrid( Fts5Global *pGlobal, /* FTS5 global context for db handle */ - i64 iCsrId, /* Id of cursor to find */ - Fts5Config **ppConfig /* OUT: Configuration object */ + i64 iCsrId /* Id of cursor to find */ ){ Fts5Cursor *pCsr; - Fts5Table *pTab; - pCsr = fts5CursorFromCsrid(pGlobal, iCsrId); - pTab = (Fts5Table*)pCsr->base.pVtab; - *ppConfig = pTab->pConfig; - - return pTab->pIndex; + if( pCsr ){ + return (Fts5Table*)pCsr->base.pVtab; + } + return 0; } /* @@ -2291,8 +3022,8 @@ static int fts5ColumnMethod( sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */ int iCol /* Index of column to read value from */ ){ - Fts5Table *pTab = (Fts5Table*)(pCursor->pVtab); - Fts5Config *pConfig = pTab->pConfig; + Fts5FullTable *pTab = (Fts5FullTable*)(pCursor->pVtab); + Fts5Config *pConfig = pTab->p.pConfig; Fts5Cursor *pCsr = (Fts5Cursor*)pCursor; int rc = SQLITE_OK; @@ -2311,8 +3042,8 @@ static int fts5ColumnMethod( ** auxiliary function. */ sqlite3_result_int64(pCtx, pCsr->iCsrId); }else if( iCol==pConfig->nCol+1 ){ - /* The value of the "rank" column. */ + if( pCsr->ePlan==FTS5_PLAN_SOURCE ){ fts5PoslistBlob(pCtx, pCsr); }else if( @@ -2323,12 +3054,32 @@ static int fts5ColumnMethod( fts5ApiInvoke(pCsr->pRank, pCsr, pCtx, pCsr->nRankArg, pCsr->apRankArg); } } - }else if( !fts5IsContentless(pTab) ){ - rc = fts5SeekCursor(pCsr, 1); - if( rc==SQLITE_OK ){ - sqlite3_result_value(pCtx, sqlite3_column_value(pCsr->pStmt, iCol+1)); + }else{ + if( !sqlite3_vtab_nochange(pCtx) && pConfig->eContent!=FTS5_CONTENT_NONE ){ + pConfig->pzErrmsg = &pTab->p.base.zErrMsg; + rc = fts5SeekCursor(pCsr, 1); + if( rc==SQLITE_OK ){ + sqlite3_value *pVal = sqlite3_column_value(pCsr->pStmt, iCol+1); + if( pConfig->bLocale + && pConfig->eContent==FTS5_CONTENT_EXTERNAL + && sqlite3Fts5IsLocaleValue(pConfig, pVal) + ){ + const char *z = 0; + int n = 0; + rc = fts5TextFromStmt(pConfig, pCsr->pStmt, iCol, &z, &n); + if( rc==SQLITE_OK ){ + sqlite3_result_text(pCtx, z, n, SQLITE_TRANSIENT); + } + sqlite3Fts5ClearLocale(pConfig); + }else{ + sqlite3_result_value(pCtx, pVal); + } + } + + pConfig->pzErrmsg = 0; } } + return rc; } @@ -2344,7 +3095,7 @@ static int fts5FindFunctionMethod( void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */ void **ppArg /* OUT: User data for *pxFunc */ ){ - Fts5Table *pTab = (Fts5Table*)pVtab; + Fts5FullTable *pTab = (Fts5FullTable*)pVtab; Fts5Auxiliary *pAux; UNUSED_PARAM(nUnused); @@ -2366,8 +3117,15 @@ static int fts5RenameMethod( sqlite3_vtab *pVtab, /* Virtual table handle */ const char *zName /* New name of table */ ){ - Fts5Table *pTab = (Fts5Table*)pVtab; - return sqlite3Fts5StorageRename(pTab->pStorage, zName); + int rc; + Fts5FullTable *pTab = (Fts5FullTable*)pVtab; + rc = sqlite3Fts5StorageRename(pTab->pStorage, zName); + return rc; +} + +int sqlite3Fts5FlushToDisk(Fts5Table *pTab){ + fts5TripCursors((Fts5FullTable*)pTab); + return sqlite3Fts5StorageSync(((Fts5FullTable*)pTab)->pStorage); } /* @@ -2376,11 +3134,15 @@ static int fts5RenameMethod( ** Flush the contents of the pending-terms table to disk. */ static int fts5SavepointMethod(sqlite3_vtab *pVtab, int iSavepoint){ - Fts5Table *pTab = (Fts5Table*)pVtab; - UNUSED_PARAM(iSavepoint); /* Call below is a no-op for NDEBUG builds */ + Fts5FullTable *pTab = (Fts5FullTable*)pVtab; + int rc = SQLITE_OK; + fts5CheckTransactionState(pTab, FTS5_SAVEPOINT, iSavepoint); - fts5TripCursors(pTab); - return sqlite3Fts5StorageSync(pTab->pStorage, 0); + rc = sqlite3Fts5FlushToDisk((Fts5Table*)pVtab); + if( rc==SQLITE_OK ){ + pTab->iSavepoint = iSavepoint+1; + } + return rc; } /* @@ -2389,11 +3151,16 @@ static int fts5SavepointMethod(sqlite3_vtab *pVtab, int iSavepoint){ ** This is a no-op. */ static int fts5ReleaseMethod(sqlite3_vtab *pVtab, int iSavepoint){ - Fts5Table *pTab = (Fts5Table*)pVtab; - UNUSED_PARAM(iSavepoint); /* Call below is a no-op for NDEBUG builds */ + Fts5FullTable *pTab = (Fts5FullTable*)pVtab; + int rc = SQLITE_OK; fts5CheckTransactionState(pTab, FTS5_RELEASE, iSavepoint); - fts5TripCursors(pTab); - return sqlite3Fts5StorageSync(pTab->pStorage, 0); + if( (iSavepoint+1)iSavepoint ){ + rc = sqlite3Fts5FlushToDisk(&pTab->p); + if( rc==SQLITE_OK ){ + pTab->iSavepoint = iSavepoint; + } + } + return rc; } /* @@ -2402,11 +3169,15 @@ static int fts5ReleaseMethod(sqlite3_vtab *pVtab, int iSavepoint){ ** Discard the contents of the pending terms table. */ static int fts5RollbackToMethod(sqlite3_vtab *pVtab, int iSavepoint){ - Fts5Table *pTab = (Fts5Table*)pVtab; - UNUSED_PARAM(iSavepoint); /* Call below is a no-op for NDEBUG builds */ + Fts5FullTable *pTab = (Fts5FullTable*)pVtab; + int rc = SQLITE_OK; fts5CheckTransactionState(pTab, FTS5_ROLLBACKTO, iSavepoint); fts5TripCursors(pTab); - return sqlite3Fts5StorageRollback(pTab->pStorage); + if( (iSavepoint+1)<=pTab->iSavepoint ){ + pTab->p.pConfig->pgsz = 0; + rc = sqlite3Fts5StorageRollback(pTab->pStorage); + } + return rc; } /* @@ -2423,14 +3194,14 @@ static int fts5CreateAux( int rc = sqlite3_overload_function(pGlobal->db, zName, -1); if( rc==SQLITE_OK ){ Fts5Auxiliary *pAux; - int nName; /* Size of zName in bytes, including \0 */ - int nByte; /* Bytes of space to allocate */ + sqlite3_int64 nName; /* Size of zName in bytes, including \0 */ + sqlite3_int64 nByte; /* Bytes of space to allocate */ - nName = (int)strlen(zName) + 1; + nName = strlen(zName) + 1; nByte = sizeof(Fts5Auxiliary) + nName; - pAux = (Fts5Auxiliary*)sqlite3_malloc(nByte); + pAux = (Fts5Auxiliary*)sqlite3_malloc64(nByte); if( pAux ){ - memset(pAux, 0, nByte); + memset(pAux, 0, (size_t)nByte); pAux->zFunc = (char*)&pAux[1]; memcpy(pAux->zFunc, zName, nName); pAux->pGlobal = pGlobal; @@ -2448,47 +3219,210 @@ static int fts5CreateAux( } /* -** Register a new tokenizer. This is the implementation of the -** fts5_api.xCreateTokenizer() method. +** This function is used by xCreateTokenizer_v2() and xCreateTokenizer(). +** It allocates and partially populates a new Fts5TokenizerModule object. +** The new object is already linked into the Fts5Global context before +** returning. +** +** If successful, SQLITE_OK is returned and a pointer to the new +** Fts5TokenizerModule object returned via output parameter (*ppNew). All +** that is required is for the caller to fill in the methods in +** Fts5TokenizerModule.x1 and x2, and to set Fts5TokenizerModule.bV2Native +** as appropriate. +** +** If an error occurs, an SQLite error code is returned and the final value +** of (*ppNew) undefined. */ -static int fts5CreateTokenizer( - fts5_api *pApi, /* Global context (one per db handle) */ +static int fts5NewTokenizerModule( + Fts5Global *pGlobal, /* Global context (one per db handle) */ const char *zName, /* Name of new function */ void *pUserData, /* User data for aux. function */ - fts5_tokenizer *pTokenizer, /* Tokenizer implementation */ - void(*xDestroy)(void*) /* Destructor for pUserData */ + void(*xDestroy)(void*), /* Destructor for pUserData */ + Fts5TokenizerModule **ppNew ){ - Fts5Global *pGlobal = (Fts5Global*)pApi; - Fts5TokenizerModule *pNew; - int nName; /* Size of zName and its \0 terminator */ - int nByte; /* Bytes of space to allocate */ int rc = SQLITE_OK; + Fts5TokenizerModule *pNew; + sqlite3_int64 nName; /* Size of zName and its \0 terminator */ + sqlite3_int64 nByte; /* Bytes of space to allocate */ - nName = (int)strlen(zName) + 1; + nName = strlen(zName) + 1; nByte = sizeof(Fts5TokenizerModule) + nName; - pNew = (Fts5TokenizerModule*)sqlite3_malloc(nByte); + *ppNew = pNew = (Fts5TokenizerModule*)sqlite3Fts5MallocZero(&rc, nByte); if( pNew ){ - memset(pNew, 0, nByte); pNew->zName = (char*)&pNew[1]; memcpy(pNew->zName, zName, nName); pNew->pUserData = pUserData; - pNew->x = *pTokenizer; pNew->xDestroy = xDestroy; pNew->pNext = pGlobal->pTok; pGlobal->pTok = pNew; if( pNew->pNext==0 ){ pGlobal->pDfltTok = pNew; } + } + + return rc; +} + +/* +** An instance of this type is used as the Fts5Tokenizer object for +** wrapper tokenizers - those that provide access to a v1 tokenizer via +** the fts5_tokenizer_v2 API, and those that provide access to a v2 tokenizer +** via the fts5_tokenizer API. +*/ +typedef struct Fts5VtoVTokenizer Fts5VtoVTokenizer; +struct Fts5VtoVTokenizer { + int bV2Native; /* True if v2 native tokenizer */ + fts5_tokenizer x1; /* Tokenizer functions */ + fts5_tokenizer_v2 x2; /* V2 tokenizer functions */ + Fts5Tokenizer *pReal; +}; + +/* +** Create a wrapper tokenizer. The context argument pCtx points to the +** Fts5TokenizerModule object. +*/ +static int fts5VtoVCreate( + void *pCtx, + const char **azArg, + int nArg, + Fts5Tokenizer **ppOut +){ + Fts5TokenizerModule *pMod = (Fts5TokenizerModule*)pCtx; + Fts5VtoVTokenizer *pNew = 0; + int rc = SQLITE_OK; + + pNew = (Fts5VtoVTokenizer*)sqlite3Fts5MallocZero(&rc, sizeof(*pNew)); + if( rc==SQLITE_OK ){ + pNew->x1 = pMod->x1; + pNew->x2 = pMod->x2; + pNew->bV2Native = pMod->bV2Native; + if( pMod->bV2Native ){ + rc = pMod->x2.xCreate(pMod->pUserData, azArg, nArg, &pNew->pReal); + }else{ + rc = pMod->x1.xCreate(pMod->pUserData, azArg, nArg, &pNew->pReal); + } + if( rc!=SQLITE_OK ){ + sqlite3_free(pNew); + pNew = 0; + } + } + + *ppOut = (Fts5Tokenizer*)pNew; + return rc; +} + +/* +** Delete an Fts5VtoVTokenizer wrapper tokenizer. +*/ +static void fts5VtoVDelete(Fts5Tokenizer *pTok){ + Fts5VtoVTokenizer *p = (Fts5VtoVTokenizer*)pTok; + if( p ){ + if( p->bV2Native ){ + p->x2.xDelete(p->pReal); + }else{ + p->x1.xDelete(p->pReal); + } + sqlite3_free(p); + } +} + + +/* +** xTokenizer method for a wrapper tokenizer that offers the v1 interface +** (no support for locales). +*/ +static int fts5V1toV2Tokenize( + Fts5Tokenizer *pTok, + void *pCtx, int flags, + const char *pText, int nText, + int (*xToken)(void*, int, const char*, int, int, int) +){ + Fts5VtoVTokenizer *p = (Fts5VtoVTokenizer*)pTok; + assert( p->bV2Native ); + return p->x2.xTokenize(p->pReal, pCtx, flags, pText, nText, 0, 0, xToken); +} + +/* +** xTokenizer method for a wrapper tokenizer that offers the v2 interface +** (with locale support). +*/ +static int fts5V2toV1Tokenize( + Fts5Tokenizer *pTok, + void *pCtx, int flags, + const char *pText, int nText, + const char *pLocale, int nLocale, + int (*xToken)(void*, int, const char*, int, int, int) +){ + Fts5VtoVTokenizer *p = (Fts5VtoVTokenizer*)pTok; + assert( p->bV2Native==0 ); + UNUSED_PARAM2(pLocale,nLocale); + return p->x1.xTokenize(p->pReal, pCtx, flags, pText, nText, xToken); +} + +/* +** Register a new tokenizer. This is the implementation of the +** fts5_api.xCreateTokenizer_v2() method. +*/ +static int fts5CreateTokenizer_v2( + fts5_api *pApi, /* Global context (one per db handle) */ + const char *zName, /* Name of new function */ + void *pUserData, /* User data for aux. function */ + fts5_tokenizer_v2 *pTokenizer, /* Tokenizer implementation */ + void(*xDestroy)(void*) /* Destructor for pUserData */ +){ + Fts5Global *pGlobal = (Fts5Global*)pApi; + int rc = SQLITE_OK; + + if( pTokenizer->iVersion>2 ){ + rc = SQLITE_ERROR; }else{ - rc = SQLITE_NOMEM; + Fts5TokenizerModule *pNew = 0; + rc = fts5NewTokenizerModule(pGlobal, zName, pUserData, xDestroy, &pNew); + if( pNew ){ + pNew->x2 = *pTokenizer; + pNew->bV2Native = 1; + pNew->x1.xCreate = fts5VtoVCreate; + pNew->x1.xTokenize = fts5V1toV2Tokenize; + pNew->x1.xDelete = fts5VtoVDelete; + } } return rc; } +/* +** The fts5_api.xCreateTokenizer() method. +*/ +static int fts5CreateTokenizer( + fts5_api *pApi, /* Global context (one per db handle) */ + const char *zName, /* Name of new function */ + void *pUserData, /* User data for aux. function */ + fts5_tokenizer *pTokenizer, /* Tokenizer implementation */ + void(*xDestroy)(void*) /* Destructor for pUserData */ +){ + Fts5TokenizerModule *pNew = 0; + int rc = SQLITE_OK; + + rc = fts5NewTokenizerModule( + (Fts5Global*)pApi, zName, pUserData, xDestroy, &pNew + ); + if( pNew ){ + pNew->x1 = *pTokenizer; + pNew->x2.xCreate = fts5VtoVCreate; + pNew->x2.xTokenize = fts5V2toV1Tokenize; + pNew->x2.xDelete = fts5VtoVDelete; + } + return rc; +} + +/* +** Search the global context passed as the first argument for a tokenizer +** module named zName. If found, return a pointer to the Fts5TokenizerModule +** object. Otherwise, return NULL. +*/ static Fts5TokenizerModule *fts5LocateTokenizer( - Fts5Global *pGlobal, - const char *zName + Fts5Global *pGlobal, /* Global (one per db handle) object */ + const char *zName /* Name of tokenizer module to find */ ){ Fts5TokenizerModule *pMod = 0; @@ -2503,6 +3437,36 @@ static Fts5TokenizerModule *fts5LocateTokenizer( return pMod; } +/* +** Find a tokenizer. This is the implementation of the +** fts5_api.xFindTokenizer_v2() method. +*/ +static int fts5FindTokenizer_v2( + fts5_api *pApi, /* Global context (one per db handle) */ + const char *zName, /* Name of tokenizer */ + void **ppUserData, + fts5_tokenizer_v2 **ppTokenizer /* Populate this object */ +){ + int rc = SQLITE_OK; + Fts5TokenizerModule *pMod; + + pMod = fts5LocateTokenizer((Fts5Global*)pApi, zName); + if( pMod ){ + if( pMod->bV2Native ){ + *ppUserData = pMod->pUserData; + }else{ + *ppUserData = (void*)pMod; + } + *ppTokenizer = &pMod->x2; + }else{ + *ppTokenizer = 0; + *ppUserData = 0; + rc = SQLITE_ERROR; + } + + return rc; +} + /* ** Find a tokenizer. This is the implementation of the ** fts5_api.xFindTokenizer() method. @@ -2518,48 +3482,75 @@ static int fts5FindTokenizer( pMod = fts5LocateTokenizer((Fts5Global*)pApi, zName); if( pMod ){ - *pTokenizer = pMod->x; - *ppUserData = pMod->pUserData; + if( pMod->bV2Native==0 ){ + *ppUserData = pMod->pUserData; + }else{ + *ppUserData = (void*)pMod; + } + *pTokenizer = pMod->x1; }else{ - memset(pTokenizer, 0, sizeof(fts5_tokenizer)); + memset(pTokenizer, 0, sizeof(*pTokenizer)); + *ppUserData = 0; rc = SQLITE_ERROR; } return rc; } -int sqlite3Fts5GetTokenizer( - Fts5Global *pGlobal, - const char **azArg, - int nArg, - Fts5Tokenizer **ppTok, - fts5_tokenizer **ppTokApi, - char **pzErr -){ - Fts5TokenizerModule *pMod; +/* +** Attempt to instantiate the tokenizer. +*/ +int sqlite3Fts5LoadTokenizer(Fts5Config *pConfig){ + const char **azArg = pConfig->t.azArg; + const int nArg = pConfig->t.nArg; + Fts5TokenizerModule *pMod = 0; int rc = SQLITE_OK; - pMod = fts5LocateTokenizer(pGlobal, nArg==0 ? 0 : azArg[0]); + pMod = fts5LocateTokenizer(pConfig->pGlobal, nArg==0 ? 0 : azArg[0]); if( pMod==0 ){ assert( nArg>0 ); rc = SQLITE_ERROR; - *pzErr = sqlite3_mprintf("no such tokenizer: %s", azArg[0]); + sqlite3Fts5ConfigErrmsg(pConfig, "no such tokenizer: %s", azArg[0]); }else{ - rc = pMod->x.xCreate(pMod->pUserData, &azArg[1], (nArg?nArg-1:0), ppTok); - *ppTokApi = &pMod->x; - if( rc!=SQLITE_OK && pzErr ){ - *pzErr = sqlite3_mprintf("error in tokenizer constructor"); + int (*xCreate)(void*, const char**, int, Fts5Tokenizer**) = 0; + if( pMod->bV2Native ){ + xCreate = pMod->x2.xCreate; + pConfig->t.pApi2 = &pMod->x2; + }else{ + pConfig->t.pApi1 = &pMod->x1; + xCreate = pMod->x1.xCreate; + } + + rc = xCreate(pMod->pUserData, + (azArg?&azArg[1]:0), (nArg?nArg-1:0), &pConfig->t.pTok + ); + + if( rc!=SQLITE_OK ){ + if( rc!=SQLITE_NOMEM ){ + sqlite3Fts5ConfigErrmsg(pConfig, "error in tokenizer constructor"); + } + }else if( pMod->bV2Native==0 ){ + pConfig->t.ePattern = sqlite3Fts5TokenizerPattern( + pMod->x1.xCreate, pConfig->t.pTok + ); } } if( rc!=SQLITE_OK ){ - *ppTokApi = 0; - *ppTok = 0; + pConfig->t.pApi1 = 0; + pConfig->t.pApi2 = 0; + pConfig->t.pTok = 0; } return rc; } + +/* +** xDestroy callback passed to sqlite3_create_module(). This is invoked +** when the db handle is being closed. Free memory associated with +** tokenizers and aux functions registered with this db handle. +*/ static void fts5ModuleDestroy(void *pCtx){ Fts5TokenizerModule *pTok, *pNextTok; Fts5Auxiliary *pAux, *pNextAux; @@ -2580,18 +3571,21 @@ static void fts5ModuleDestroy(void *pCtx){ sqlite3_free(pGlobal); } +/* +** Implementation of the fts5() function used by clients to obtain the +** API pointer. +*/ static void fts5Fts5Func( sqlite3_context *pCtx, /* Function call context */ int nArg, /* Number of args */ - sqlite3_value **apUnused /* Function arguments */ + sqlite3_value **apArg /* Function arguments */ ){ Fts5Global *pGlobal = (Fts5Global*)sqlite3_user_data(pCtx); - char buf[8]; - UNUSED_PARAM2(nArg, apUnused); - assert( nArg==0 ); - assert( sizeof(buf)>=sizeof(pGlobal) ); - memcpy(buf, (void*)&pGlobal, sizeof(pGlobal)); - sqlite3_result_blob(pCtx, buf, sizeof(pGlobal), SQLITE_TRANSIENT); + fts5_api **ppApi; + UNUSED_PARAM(nArg); + assert( nArg==1 ); + ppApi = (fts5_api**)sqlite3_value_pointer(apArg[0], "fts5_api_ptr"); + if( ppApi ) *ppApi = &pGlobal->api; } /* @@ -2607,9 +3601,138 @@ static void fts5SourceIdFunc( sqlite3_result_text(pCtx, "--FTS5-SOURCE-ID--", -1, SQLITE_TRANSIENT); } +/* +** Implementation of fts5_locale(LOCALE, TEXT) function. +** +** If parameter LOCALE is NULL, or a zero-length string, then a copy of +** TEXT is returned. Otherwise, both LOCALE and TEXT are interpreted as +** text, and the value returned is a blob consisting of: +** +** * The 4 bytes 0x00, 0xE0, 0xB2, 0xEb (FTS5_LOCALE_HEADER). +** * The LOCALE, as utf-8 text, followed by +** * 0x00, followed by +** * The TEXT, as utf-8 text. +** +** There is no final nul-terminator following the TEXT value. +*/ +static void fts5LocaleFunc( + sqlite3_context *pCtx, /* Function call context */ + int nArg, /* Number of args */ + sqlite3_value **apArg /* Function arguments */ +){ + const char *zLocale = 0; + i64 nLocale = 0; + const char *zText = 0; + i64 nText = 0; + + assert( nArg==2 ); + UNUSED_PARAM(nArg); + + zLocale = (const char*)sqlite3_value_text(apArg[0]); + nLocale = sqlite3_value_bytes(apArg[0]); + + zText = (const char*)sqlite3_value_text(apArg[1]); + nText = sqlite3_value_bytes(apArg[1]); + + if( zLocale==0 || zLocale[0]=='\0' ){ + sqlite3_result_text(pCtx, zText, nText, SQLITE_TRANSIENT); + }else{ + Fts5Global *p = (Fts5Global*)sqlite3_user_data(pCtx); + u8 *pBlob = 0; + u8 *pCsr = 0; + i64 nBlob = 0; + + nBlob = FTS5_LOCALE_HDR_SIZE + nLocale + 1 + nText; + pBlob = (u8*)sqlite3_malloc64(nBlob); + if( pBlob==0 ){ + sqlite3_result_error_nomem(pCtx); + return; + } + + pCsr = pBlob; + memcpy(pCsr, (const u8*)p->aLocaleHdr, FTS5_LOCALE_HDR_SIZE); + pCsr += FTS5_LOCALE_HDR_SIZE; + memcpy(pCsr, zLocale, nLocale); + pCsr += nLocale; + (*pCsr++) = 0x00; + if( zText ) memcpy(pCsr, zText, nText); + assert( &pCsr[nText]==&pBlob[nBlob] ); + + sqlite3_result_blob(pCtx, pBlob, nBlob, sqlite3_free); + } +} + +/* +** Implementation of fts5_insttoken() function. +*/ +static void fts5InsttokenFunc( + sqlite3_context *pCtx, /* Function call context */ + int nArg, /* Number of args */ + sqlite3_value **apArg /* Function arguments */ +){ + assert( nArg==1 ); + (void)nArg; + sqlite3_result_value(pCtx, apArg[0]); + sqlite3_result_subtype(pCtx, FTS5_INSTTOKEN_SUBTYPE); +} + +/* +** Return true if zName is the extension on one of the shadow tables used +** by this module. +*/ +static int fts5ShadowName(const char *zName){ + static const char *azName[] = { + "config", "content", "data", "docsize", "idx" + }; + unsigned int i; + for(i=0; ip.pConfig->pzErrmsg==0 ); + pTab->p.pConfig->pzErrmsg = pzErr; + rc = sqlite3Fts5StorageIntegrity(pTab->pStorage, 0); + if( *pzErr==0 && rc!=SQLITE_OK ){ + if( (rc&0xff)==SQLITE_CORRUPT ){ + *pzErr = sqlite3_mprintf("malformed inverted index for FTS5 table %s.%s", + zSchema, zTabname); + rc = (*pzErr) ? SQLITE_OK : SQLITE_NOMEM; + }else{ + *pzErr = sqlite3_mprintf("unable to validate the inverted index for" + " FTS5 table %s.%s: %s", + zSchema, zTabname, sqlite3_errstr(rc)); + } + }else if( (rc&0xff)==SQLITE_CORRUPT ){ + rc = SQLITE_OK; + } + sqlite3Fts5IndexCloseReader(pTab->p.pIndex); + pTab->p.pConfig->pzErrmsg = 0; + + return rc; +} + static int fts5Init(sqlite3 *db){ static const sqlite3_module fts5Mod = { - /* iVersion */ 2, + /* iVersion */ 4, /* xCreate */ fts5CreateMethod, /* xConnect */ fts5ConnectMethod, /* xBestIndex */ fts5BestIndexMethod, @@ -2632,6 +3755,8 @@ static int fts5Init(sqlite3 *db){ /* xSavepoint */ fts5SavepointMethod, /* xRelease */ fts5ReleaseMethod, /* xRollbackTo */ fts5RollbackToMethod, + /* xShadowName */ fts5ShadowName, + /* xIntegrity */ fts5IntegrityMethod }; int rc; @@ -2644,10 +3769,22 @@ static int fts5Init(sqlite3 *db){ void *p = (void*)pGlobal; memset(pGlobal, 0, sizeof(Fts5Global)); pGlobal->db = db; - pGlobal->api.iVersion = 2; + pGlobal->api.iVersion = 3; pGlobal->api.xCreateFunction = fts5CreateAux; pGlobal->api.xCreateTokenizer = fts5CreateTokenizer; pGlobal->api.xFindTokenizer = fts5FindTokenizer; + pGlobal->api.xCreateTokenizer_v2 = fts5CreateTokenizer_v2; + pGlobal->api.xFindTokenizer_v2 = fts5FindTokenizer_v2; + + /* Initialize pGlobal->aLocaleHdr[] to a 128-bit pseudo-random vector. + ** The constants below were generated randomly. */ + sqlite3_randomness(sizeof(pGlobal->aLocaleHdr), pGlobal->aLocaleHdr); + pGlobal->aLocaleHdr[0] ^= 0xF924976D; + pGlobal->aLocaleHdr[1] ^= 0x16596E13; + pGlobal->aLocaleHdr[2] ^= 0x7C80BEAA; + pGlobal->aLocaleHdr[3] ^= 0x9B03A67F; + assert( sizeof(pGlobal->aLocaleHdr)==16 ); + rc = sqlite3_create_module_v2(db, "fts5", &fts5Mod, p, fts5ModuleDestroy); if( rc==SQLITE_OK ) rc = sqlite3Fts5IndexInit(db); if( rc==SQLITE_OK ) rc = sqlite3Fts5ExprInit(pGlobal, db); @@ -2656,15 +3793,42 @@ static int fts5Init(sqlite3 *db){ if( rc==SQLITE_OK ) rc = sqlite3Fts5VocabInit(pGlobal, db); if( rc==SQLITE_OK ){ rc = sqlite3_create_function( - db, "fts5", 0, SQLITE_UTF8, p, fts5Fts5Func, 0, 0 + db, "fts5", 1, SQLITE_UTF8, p, fts5Fts5Func, 0, 0 ); } if( rc==SQLITE_OK ){ rc = sqlite3_create_function( - db, "fts5_source_id", 0, SQLITE_UTF8, p, fts5SourceIdFunc, 0, 0 + db, "fts5_source_id", 0, + SQLITE_UTF8|SQLITE_DETERMINISTIC|SQLITE_INNOCUOUS, + p, fts5SourceIdFunc, 0, 0 + ); + } + if( rc==SQLITE_OK ){ + rc = sqlite3_create_function( + db, "fts5_locale", 2, + SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_RESULT_SUBTYPE|SQLITE_SUBTYPE, + p, fts5LocaleFunc, 0, 0 + ); + } + if( rc==SQLITE_OK ){ + rc = sqlite3_create_function( + db, "fts5_insttoken", 1, + SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_RESULT_SUBTYPE, + p, fts5InsttokenFunc, 0, 0 ); } } + + /* If SQLITE_FTS5_ENABLE_TEST_MI is defined, assume that the file + ** fts5_test_mi.c is compiled and linked into the executable. And call + ** its entry point to enable the matchinfo() demo. */ +#ifdef SQLITE_FTS5_ENABLE_TEST_MI + if( rc==SQLITE_OK ){ + extern int sqlite3Fts5TestRegisterMatchinfoAPI(fts5_api*); + rc = sqlite3Fts5TestRegisterMatchinfoAPI(&pGlobal->api); + } +#endif + return rc; } diff --git a/ext/fts5/fts5_storage.c b/ext/fts5/fts5_storage.c index e4f5dd8f51..76820e85b3 100644 --- a/ext/fts5/fts5_storage.c +++ b/ext/fts5/fts5_storage.c @@ -16,13 +16,40 @@ #include "fts5Int.h" +/* +** pSavedRow: +** SQL statement FTS5_STMT_LOOKUP2 is a copy of FTS5_STMT_LOOKUP, it +** does a by-rowid lookup to retrieve a single row from the %_content +** table or equivalent external-content table/view. +** +** However, FTS5_STMT_LOOKUP2 is only used when retrieving the original +** values for a row being UPDATEd. In that case, the SQL statement is +** not reset and pSavedRow is set to point at it. This is so that the +** insert operation that follows the delete may access the original +** row values for any new values for which sqlite3_value_nochange() returns +** true. i.e. if the user executes: +** +** CREATE VIRTUAL TABLE ft USING fts5(a, b, c, locale=1); +** ... +** UPDATE fts SET a=?, b=? WHERE rowid=?; +** +** then the value passed to the xUpdate() method of this table as the +** new.c value is an sqlite3_value_nochange() value. So in this case it +** must be read from the saved row stored in Fts5Storage.pSavedRow. +** +** This is necessary - using sqlite3_value_nochange() instead of just having +** SQLite pass the original value back via xUpdate() - so as not to discard +** any locale information associated with such values. +** +*/ struct Fts5Storage { Fts5Config *pConfig; Fts5Index *pIndex; int bTotalsValid; /* True if nTotalRow/aTotalSize[] are valid */ i64 nTotalRow; /* Total number of rows in FTS table */ i64 *aTotalSize; /* Total sizes of each column */ - sqlite3_stmt *aStmt[11]; + sqlite3_stmt *pSavedRow; + sqlite3_stmt *aStmt[12]; }; @@ -36,14 +63,15 @@ struct Fts5Storage { # error "FTS5_STMT_LOOKUP mismatch" #endif -#define FTS5_STMT_INSERT_CONTENT 3 -#define FTS5_STMT_REPLACE_CONTENT 4 -#define FTS5_STMT_DELETE_CONTENT 5 -#define FTS5_STMT_REPLACE_DOCSIZE 6 -#define FTS5_STMT_DELETE_DOCSIZE 7 -#define FTS5_STMT_LOOKUP_DOCSIZE 8 -#define FTS5_STMT_REPLACE_CONFIG 9 -#define FTS5_STMT_SCAN 10 +#define FTS5_STMT_LOOKUP2 3 +#define FTS5_STMT_INSERT_CONTENT 4 +#define FTS5_STMT_REPLACE_CONTENT 5 +#define FTS5_STMT_DELETE_CONTENT 6 +#define FTS5_STMT_REPLACE_DOCSIZE 7 +#define FTS5_STMT_DELETE_DOCSIZE 8 +#define FTS5_STMT_LOOKUP_DOCSIZE 9 +#define FTS5_STMT_REPLACE_CONFIG 10 +#define FTS5_STMT_SCAN 11 /* ** Prepare the two insert statements - Fts5Storage.pInsertContent and @@ -73,14 +101,15 @@ static int fts5StorageGetStmt( "SELECT %s FROM %s T WHERE T.%Q >= ? AND T.%Q <= ? ORDER BY T.%Q ASC", "SELECT %s FROM %s T WHERE T.%Q <= ? AND T.%Q >= ? ORDER BY T.%Q DESC", "SELECT %s FROM %s T WHERE T.%Q=?", /* LOOKUP */ + "SELECT %s FROM %s T WHERE T.%Q=?", /* LOOKUP2 */ "INSERT INTO %Q.'%q_content' VALUES(%s)", /* INSERT_CONTENT */ "REPLACE INTO %Q.'%q_content' VALUES(%s)", /* REPLACE_CONTENT */ "DELETE FROM %Q.'%q_content' WHERE id=?", /* DELETE_CONTENT */ - "REPLACE INTO %Q.'%q_docsize' VALUES(?,?)", /* REPLACE_DOCSIZE */ + "REPLACE INTO %Q.'%q_docsize' VALUES(?,?%s)", /* REPLACE_DOCSIZE */ "DELETE FROM %Q.'%q_docsize' WHERE id=?", /* DELETE_DOCSIZE */ - "SELECT sz FROM %Q.'%q_docsize' WHERE id=?", /* LOOKUP_DOCSIZE */ + "SELECT sz%s FROM %Q.'%q_docsize' WHERE id=?", /* LOOKUP_DOCSIZE */ "REPLACE INTO %Q.'%q_config' VALUES(?,?)", /* REPLACE_CONFIG */ "SELECT %s FROM %s AS T", /* SCAN */ @@ -88,6 +117,8 @@ static int fts5StorageGetStmt( Fts5Config *pC = p->pConfig; char *zSql = 0; + assert( ArraySize(azStmt)==ArraySize(p->aStmt) ); + switch( eStmt ){ case FTS5_STMT_SCAN: zSql = sqlite3_mprintf(azStmt[eStmt], @@ -104,6 +135,7 @@ static int fts5StorageGetStmt( break; case FTS5_STMT_LOOKUP: + case FTS5_STMT_LOOKUP2: zSql = sqlite3_mprintf(azStmt[eStmt], pC->zContentExprlist, pC->zContent, pC->zContentRowid ); @@ -111,23 +143,51 @@ static int fts5StorageGetStmt( case FTS5_STMT_INSERT_CONTENT: case FTS5_STMT_REPLACE_CONTENT: { - int nCol = pC->nCol + 1; - char *zBind; + char *zBind = 0; int i; - zBind = sqlite3_malloc(1 + nCol*2); - if( zBind ){ - for(i=0; ieContent==FTS5_CONTENT_NORMAL + || pC->eContent==FTS5_CONTENT_UNINDEXED + ); + + /* Add bindings for the "c*" columns - those that store the actual + ** table content. If eContent==NORMAL, then there is one binding + ** for each column. Or, if eContent==UNINDEXED, then there are only + ** bindings for the UNINDEXED columns. */ + for(i=0; rc==SQLITE_OK && i<(pC->nCol+1); i++){ + if( !i || pC->eContent==FTS5_CONTENT_NORMAL || pC->abUnindexed[i-1] ){ + zBind = sqlite3Fts5Mprintf(&rc, "%z%s?%d", zBind, zBind?",":"",i+1); + } + } + + /* Add bindings for any "l*" columns. Only non-UNINDEXED columns + ** require these. */ + if( pC->bLocale && pC->eContent==FTS5_CONTENT_NORMAL ){ + for(i=0; rc==SQLITE_OK && inCol; i++){ + if( pC->abUnindexed[i]==0 ){ + zBind = sqlite3Fts5Mprintf(&rc, "%z,?%d", zBind, pC->nCol+i+2); + } } - zBind[i*2-1] = '\0'; - zSql = sqlite3_mprintf(azStmt[eStmt], pC->zDb, pC->zName, zBind); - sqlite3_free(zBind); } + + zSql = sqlite3Fts5Mprintf(&rc, azStmt[eStmt], pC->zDb, pC->zName,zBind); + sqlite3_free(zBind); break; } + case FTS5_STMT_REPLACE_DOCSIZE: + zSql = sqlite3_mprintf(azStmt[eStmt], pC->zDb, pC->zName, + (pC->bContentlessDelete ? ",?" : "") + ); + break; + + case FTS5_STMT_LOOKUP_DOCSIZE: + zSql = sqlite3_mprintf(azStmt[eStmt], + (pC->bContentlessDelete ? ",origin" : ""), + pC->zDb, pC->zName + ); + break; + default: zSql = sqlite3_mprintf(azStmt[eStmt], pC->zDb, pC->zName); break; @@ -136,15 +196,25 @@ static int fts5StorageGetStmt( if( zSql==0 ){ rc = SQLITE_NOMEM; }else{ - rc = sqlite3_prepare_v2(pC->db, zSql, -1, &p->aStmt[eStmt], 0); + int f = SQLITE_PREPARE_PERSISTENT; + if( eStmt>FTS5_STMT_LOOKUP2 ) f |= SQLITE_PREPARE_NO_VTAB; + p->pConfig->bLock++; + rc = sqlite3_prepare_v3(pC->db, zSql, -1, f, &p->aStmt[eStmt], 0); + p->pConfig->bLock--; sqlite3_free(zSql); if( rc!=SQLITE_OK && pzErrMsg ){ *pzErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pC->db)); } + if( rc==SQLITE_ERROR && eStmt>FTS5_STMT_LOOKUP2 && eStmtaStmt[eStmt]; + sqlite3_reset(*ppStmt); return rc; } @@ -217,7 +287,7 @@ static void fts5StorageRenameOne( int sqlite3Fts5StorageRename(Fts5Storage *pStorage, const char *zName){ Fts5Config *pConfig = pStorage->pConfig; - int rc = sqlite3Fts5StorageSync(pStorage, 1); + int rc = sqlite3Fts5StorageSync(pStorage); fts5StorageRenameOne(pConfig, &rc, "data", zName); fts5StorageRenameOne(pConfig, &rc, "idx", zName); @@ -246,7 +316,11 @@ int sqlite3Fts5CreateTable( char *zErr = 0; rc = fts5ExecPrintf(pConfig->db, &zErr, "CREATE TABLE %Q.'%q_%q'(%s)%s", - pConfig->zDb, pConfig->zName, zPost, zDefn, bWithout?" WITHOUT ROWID":"" + pConfig->zDb, pConfig->zName, zPost, zDefn, +#ifndef SQLITE_FTS5_NO_WITHOUT_ROWID + bWithout?" WITHOUT ROWID": +#endif + "" ); if( zErr ){ *pzErr = sqlite3_mprintf( @@ -275,22 +349,24 @@ int sqlite3Fts5StorageOpen( ){ int rc = SQLITE_OK; Fts5Storage *p; /* New object */ - int nByte; /* Bytes of space to allocate */ + sqlite3_int64 nByte; /* Bytes of space to allocate */ nByte = sizeof(Fts5Storage) /* Fts5Storage object */ + pConfig->nCol * sizeof(i64); /* Fts5Storage.aTotalSize[] */ - *pp = p = (Fts5Storage*)sqlite3_malloc(nByte); + *pp = p = (Fts5Storage*)sqlite3_malloc64(nByte); if( !p ) return SQLITE_NOMEM; - memset(p, 0, nByte); + memset(p, 0, (size_t)nByte); p->aTotalSize = (i64*)&p[1]; p->pConfig = pConfig; p->pIndex = pIndex; if( bCreate ){ - if( pConfig->eContent==FTS5_CONTENT_NORMAL ){ + if( pConfig->eContent==FTS5_CONTENT_NORMAL + || pConfig->eContent==FTS5_CONTENT_UNINDEXED + ){ int nDefn = 32 + pConfig->nCol*10; - char *zDefn = sqlite3_malloc(32 + pConfig->nCol * 10); + char *zDefn = sqlite3_malloc64(32 + (sqlite3_int64)pConfig->nCol * 20); if( zDefn==0 ){ rc = SQLITE_NOMEM; }else{ @@ -299,8 +375,20 @@ int sqlite3Fts5StorageOpen( sqlite3_snprintf(nDefn, zDefn, "id INTEGER PRIMARY KEY"); iOff = (int)strlen(zDefn); for(i=0; inCol; i++){ - sqlite3_snprintf(nDefn-iOff, &zDefn[iOff], ", c%d", i); - iOff += (int)strlen(&zDefn[iOff]); + if( pConfig->eContent==FTS5_CONTENT_NORMAL + || pConfig->abUnindexed[i] + ){ + sqlite3_snprintf(nDefn-iOff, &zDefn[iOff], ", c%d", i); + iOff += (int)strlen(&zDefn[iOff]); + } + } + if( pConfig->bLocale ){ + for(i=0; inCol; i++){ + if( pConfig->abUnindexed[i]==0 ){ + sqlite3_snprintf(nDefn-iOff, &zDefn[iOff], ", l%d", i); + iOff += (int)strlen(&zDefn[iOff]); + } + } } rc = sqlite3Fts5CreateTable(pConfig, "content", zDefn, 0, pzErr); } @@ -308,9 +396,11 @@ int sqlite3Fts5StorageOpen( } if( rc==SQLITE_OK && pConfig->bColumnsize ){ - rc = sqlite3Fts5CreateTable( - pConfig, "docsize", "id INTEGER PRIMARY KEY, sz BLOB", 0, pzErr - ); + const char *zCols = "id INTEGER PRIMARY KEY, sz BLOB"; + if( pConfig->bContentlessDelete ){ + zCols = "id INTEGER PRIMARY KEY, sz BLOB, origin INTEGER"; + } + rc = sqlite3Fts5CreateTable(pConfig, "docsize", zCols, 0, pzErr); } if( rc==SQLITE_OK ){ rc = sqlite3Fts5CreateTable( @@ -368,66 +458,200 @@ static int fts5StorageInsertCallback( Fts5InsertCtx *pCtx = (Fts5InsertCtx*)pContext; Fts5Index *pIdx = pCtx->pStorage->pIndex; UNUSED_PARAM2(iUnused1, iUnused2); + if( nToken>FTS5_MAX_TOKEN_SIZE ) nToken = FTS5_MAX_TOKEN_SIZE; if( (tflags & FTS5_TOKEN_COLOCATED)==0 || pCtx->szCol==0 ){ pCtx->szCol++; } return sqlite3Fts5IndexWrite(pIdx, pCtx->iCol, pCtx->szCol-1, pToken, nToken); } +/* +** This function is used as part of an UPDATE statement that modifies the +** rowid of a row. In that case, this function is called first to set +** Fts5Storage.pSavedRow to point to a statement that may be used to +** access the original values of the row being deleted - iDel. +** +** SQLITE_OK is returned if successful, or an SQLite error code otherwise. +** It is not considered an error if row iDel does not exist. In this case +** pSavedRow is not set and SQLITE_OK returned. +*/ +int sqlite3Fts5StorageFindDeleteRow(Fts5Storage *p, i64 iDel){ + int rc = SQLITE_OK; + sqlite3_stmt *pSeek = 0; + + assert( p->pSavedRow==0 ); + rc = fts5StorageGetStmt(p, FTS5_STMT_LOOKUP+1, &pSeek, 0); + if( rc==SQLITE_OK ){ + sqlite3_bind_int64(pSeek, 1, iDel); + if( sqlite3_step(pSeek)!=SQLITE_ROW ){ + rc = sqlite3_reset(pSeek); + }else{ + p->pSavedRow = pSeek; + } + } + + return rc; +} + /* ** If a row with rowid iDel is present in the %_content table, add the ** delete-markers to the FTS index necessary to delete it. Do not actually ** remove the %_content row at this time though. +** +** If parameter bSaveRow is true, then Fts5Storage.pSavedRow is left +** pointing to a statement (FTS5_STMT_LOOKUP2) that may be used to access +** the original values of the row being deleted. This is used by UPDATE +** statements. */ static int fts5StorageDeleteFromIndex( Fts5Storage *p, i64 iDel, - sqlite3_value **apVal + sqlite3_value **apVal, + int bSaveRow /* True to set pSavedRow */ ){ Fts5Config *pConfig = p->pConfig; sqlite3_stmt *pSeek = 0; /* SELECT to read row iDel from %_data */ - int rc; /* Return code */ + int rc = SQLITE_OK; /* Return code */ int rc2; /* sqlite3_reset() return code */ int iCol; Fts5InsertCtx ctx; + assert( bSaveRow==0 || apVal==0 ); + assert( bSaveRow==0 || bSaveRow==1 ); + assert( FTS5_STMT_LOOKUP2==FTS5_STMT_LOOKUP+1 ); + if( apVal==0 ){ - rc = fts5StorageGetStmt(p, FTS5_STMT_LOOKUP, &pSeek, 0); - if( rc!=SQLITE_OK ) return rc; - sqlite3_bind_int64(pSeek, 1, iDel); - if( sqlite3_step(pSeek)!=SQLITE_ROW ){ - return sqlite3_reset(pSeek); + if( p->pSavedRow && bSaveRow ){ + pSeek = p->pSavedRow; + p->pSavedRow = 0; + }else{ + rc = fts5StorageGetStmt(p, FTS5_STMT_LOOKUP+bSaveRow, &pSeek, 0); + if( rc!=SQLITE_OK ) return rc; + sqlite3_bind_int64(pSeek, 1, iDel); + if( sqlite3_step(pSeek)!=SQLITE_ROW ){ + return sqlite3_reset(pSeek); + } } } ctx.pStorage = p; ctx.iCol = -1; - rc = sqlite3Fts5IndexBeginWrite(p->pIndex, 1, iDel); for(iCol=1; rc==SQLITE_OK && iCol<=pConfig->nCol; iCol++){ if( pConfig->abUnindexed[iCol-1]==0 ){ - const char *zText; - int nText; + sqlite3_value *pVal = 0; + sqlite3_value *pFree = 0; + const char *pText = 0; + int nText = 0; + const char *pLoc = 0; + int nLoc = 0; + + assert( pSeek==0 || apVal==0 ); + assert( pSeek!=0 || apVal!=0 ); if( pSeek ){ - zText = (const char*)sqlite3_column_text(pSeek, iCol); - nText = sqlite3_column_bytes(pSeek, iCol); + pVal = sqlite3_column_value(pSeek, iCol); }else{ - zText = (const char*)sqlite3_value_text(apVal[iCol-1]); - nText = sqlite3_value_bytes(apVal[iCol-1]); + pVal = apVal[iCol-1]; } - ctx.szCol = 0; - rc = sqlite3Fts5Tokenize(pConfig, FTS5_TOKENIZE_DOCUMENT, - zText, nText, (void*)&ctx, fts5StorageInsertCallback - ); - p->aTotalSize[iCol-1] -= (i64)ctx.szCol; + + if( pConfig->bLocale && sqlite3Fts5IsLocaleValue(pConfig, pVal) ){ + rc = sqlite3Fts5DecodeLocaleValue(pVal, &pText, &nText, &pLoc, &nLoc); + }else{ + if( sqlite3_value_type(pVal)!=SQLITE_TEXT ){ + /* Make a copy of the value to work with. This is because the call + ** to sqlite3_value_text() below forces the type of the value to + ** SQLITE_TEXT, and we may need to use it again later. */ + pFree = pVal = sqlite3_value_dup(pVal); + if( pVal==0 ){ + rc = SQLITE_NOMEM; + } + } + if( rc==SQLITE_OK ){ + pText = (const char*)sqlite3_value_text(pVal); + nText = sqlite3_value_bytes(pVal); + if( pConfig->bLocale && pSeek ){ + pLoc = (const char*)sqlite3_column_text(pSeek, iCol+pConfig->nCol); + nLoc = sqlite3_column_bytes(pSeek, iCol + pConfig->nCol); + } + } + } + + if( rc==SQLITE_OK ){ + sqlite3Fts5SetLocale(pConfig, pLoc, nLoc); + ctx.szCol = 0; + rc = sqlite3Fts5Tokenize(pConfig, FTS5_TOKENIZE_DOCUMENT, + pText, nText, (void*)&ctx, fts5StorageInsertCallback + ); + p->aTotalSize[iCol-1] -= (i64)ctx.szCol; + if( rc==SQLITE_OK && p->aTotalSize[iCol-1]<0 ){ + rc = FTS5_CORRUPT; + } + sqlite3Fts5ClearLocale(pConfig); + } + sqlite3_value_free(pFree); } } - p->nTotalRow--; + if( rc==SQLITE_OK && p->nTotalRow<1 ){ + rc = FTS5_CORRUPT; + }else{ + p->nTotalRow--; + } - rc2 = sqlite3_reset(pSeek); - if( rc==SQLITE_OK ) rc = rc2; + if( rc==SQLITE_OK && bSaveRow ){ + assert( p->pSavedRow==0 ); + p->pSavedRow = pSeek; + }else{ + rc2 = sqlite3_reset(pSeek); + if( rc==SQLITE_OK ) rc = rc2; + } return rc; } +/* +** Reset any saved statement pSavedRow. Zero pSavedRow as well. This +** should be called by the xUpdate() method of the fts5 table before +** returning from any operation that may have set Fts5Storage.pSavedRow. +*/ +void sqlite3Fts5StorageReleaseDeleteRow(Fts5Storage *pStorage){ + assert( pStorage->pSavedRow==0 + || pStorage->pSavedRow==pStorage->aStmt[FTS5_STMT_LOOKUP2] + ); + sqlite3_reset(pStorage->pSavedRow); + pStorage->pSavedRow = 0; +} + +/* +** This function is called to process a DELETE on a contentless_delete=1 +** table. It adds the tombstone required to delete the entry with rowid +** iDel. If successful, SQLITE_OK is returned. Or, if an error occurs, +** an SQLite error code. +*/ +static int fts5StorageContentlessDelete(Fts5Storage *p, i64 iDel){ + i64 iOrigin = 0; + sqlite3_stmt *pLookup = 0; + int rc = SQLITE_OK; + + assert( p->pConfig->bContentlessDelete ); + assert( p->pConfig->eContent==FTS5_CONTENT_NONE + || p->pConfig->eContent==FTS5_CONTENT_UNINDEXED + ); + + /* Look up the origin of the document in the %_docsize table. Store + ** this in stack variable iOrigin. */ + rc = fts5StorageGetStmt(p, FTS5_STMT_LOOKUP_DOCSIZE, &pLookup, 0); + if( rc==SQLITE_OK ){ + sqlite3_bind_int64(pLookup, 1, iDel); + if( SQLITE_ROW==sqlite3_step(pLookup) ){ + iOrigin = sqlite3_column_int64(pLookup, 1); + } + rc = sqlite3_reset(pLookup); + } + + if( rc==SQLITE_OK && iOrigin!=0 ){ + rc = sqlite3Fts5IndexContentlessDelete(p->pIndex, iOrigin, iDel); + } + + return rc; +} /* ** Insert a record into the %_docsize table. Specifically, do: @@ -448,9 +672,17 @@ static int fts5StorageInsertDocsize( rc = fts5StorageGetStmt(p, FTS5_STMT_REPLACE_DOCSIZE, &pReplace, 0); if( rc==SQLITE_OK ){ sqlite3_bind_int64(pReplace, 1, iRowid); + if( p->pConfig->bContentlessDelete ){ + i64 iOrigin = 0; + rc = sqlite3Fts5IndexGetOrigin(p->pIndex, &iOrigin); + sqlite3_bind_int64(pReplace, 3, iOrigin); + } + } + if( rc==SQLITE_OK ){ sqlite3_bind_blob(pReplace, 2, pBuf->p, pBuf->n, SQLITE_STATIC); sqlite3_step(pReplace); rc = sqlite3_reset(pReplace); + sqlite3_bind_null(pReplace, 2); } } return rc; @@ -504,7 +736,12 @@ static int fts5StorageSaveTotals(Fts5Storage *p){ /* ** Remove a row from the FTS table. */ -int sqlite3Fts5StorageDelete(Fts5Storage *p, i64 iDel, sqlite3_value **apVal){ +int sqlite3Fts5StorageDelete( + Fts5Storage *p, /* Storage object */ + i64 iDel, /* Rowid to delete from table */ + sqlite3_value **apVal, /* Optional - values to remove from index */ + int bSaveRow /* If true, set pSavedRow for deleted row */ +){ Fts5Config *pConfig = p->pConfig; int rc; sqlite3_stmt *pDel = 0; @@ -514,7 +751,21 @@ int sqlite3Fts5StorageDelete(Fts5Storage *p, i64 iDel, sqlite3_value **apVal){ /* Delete the index records */ if( rc==SQLITE_OK ){ - rc = fts5StorageDeleteFromIndex(p, iDel, apVal); + rc = sqlite3Fts5IndexBeginWrite(p->pIndex, 1, iDel); + } + + if( rc==SQLITE_OK ){ + if( p->pConfig->bContentlessDelete ){ + rc = fts5StorageContentlessDelete(p, iDel); + if( rc==SQLITE_OK + && bSaveRow + && p->pConfig->eContent==FTS5_CONTENT_UNINDEXED + ){ + rc = sqlite3Fts5StorageFindDeleteRow(p, iDel); + } + }else{ + rc = fts5StorageDeleteFromIndex(p, iDel, apVal, bSaveRow); + } } /* Delete the %_docsize record */ @@ -528,7 +779,9 @@ int sqlite3Fts5StorageDelete(Fts5Storage *p, i64 iDel, sqlite3_value **apVal){ } /* Delete the %_content record */ - if( pConfig->eContent==FTS5_CONTENT_NORMAL ){ + if( pConfig->eContent==FTS5_CONTENT_NORMAL + || pConfig->eContent==FTS5_CONTENT_UNINDEXED + ){ if( rc==SQLITE_OK ){ rc = fts5StorageGetStmt(p, FTS5_STMT_DELETE_CONTENT, &pDel, 0); } @@ -539,11 +792,6 @@ int sqlite3Fts5StorageDelete(Fts5Storage *p, i64 iDel, sqlite3_value **apVal){ } } - /* Write the averages record */ - if( rc==SQLITE_OK ){ - rc = fts5StorageSaveTotals(p); - } - return rc; } @@ -554,6 +802,8 @@ int sqlite3Fts5StorageDeleteAll(Fts5Storage *p){ Fts5Config *pConfig = p->pConfig; int rc; + p->bTotalsValid = 0; + /* Delete the contents of the %_data and %_docsize tables. */ rc = fts5ExecPrintf(pConfig->db, 0, "DELETE FROM %Q.'%q_data';" @@ -563,8 +813,13 @@ int sqlite3Fts5StorageDeleteAll(Fts5Storage *p){ ); if( rc==SQLITE_OK && pConfig->bColumnsize ){ rc = fts5ExecPrintf(pConfig->db, 0, - "DELETE FROM %Q.'%q_docsize';", - pConfig->zDb, pConfig->zName + "DELETE FROM %Q.'%q_docsize';", pConfig->zDb, pConfig->zName + ); + } + + if( rc==SQLITE_OK && pConfig->eContent==FTS5_CONTENT_UNINDEXED ){ + rc = fts5ExecPrintf(pConfig->db, 0, + "DELETE FROM %Q.'%q_content';", pConfig->zDb, pConfig->zName ); } @@ -584,7 +839,7 @@ int sqlite3Fts5StorageRebuild(Fts5Storage *p){ Fts5Config *pConfig = p->pConfig; sqlite3_stmt *pScan = 0; Fts5InsertCtx ctx; - int rc; + int rc, rc2; memset(&ctx, 0, sizeof(Fts5InsertCtx)); ctx.pStorage = p; @@ -594,7 +849,7 @@ int sqlite3Fts5StorageRebuild(Fts5Storage *p){ } if( rc==SQLITE_OK ){ - rc = fts5StorageGetStmt(p, FTS5_STMT_SCAN, &pScan, 0); + rc = fts5StorageGetStmt(p, FTS5_STMT_SCAN, &pScan, pConfig->pzErrmsg); } while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pScan) ){ @@ -605,13 +860,36 @@ int sqlite3Fts5StorageRebuild(Fts5Storage *p){ for(ctx.iCol=0; rc==SQLITE_OK && ctx.iColnCol; ctx.iCol++){ ctx.szCol = 0; if( pConfig->abUnindexed[ctx.iCol]==0 ){ - rc = sqlite3Fts5Tokenize(pConfig, - FTS5_TOKENIZE_DOCUMENT, - (const char*)sqlite3_column_text(pScan, ctx.iCol+1), - sqlite3_column_bytes(pScan, ctx.iCol+1), - (void*)&ctx, - fts5StorageInsertCallback - ); + int nText = 0; /* Size of pText in bytes */ + const char *pText = 0; /* Pointer to buffer containing text value */ + int nLoc = 0; /* Size of pLoc in bytes */ + const char *pLoc = 0; /* Pointer to buffer containing text value */ + + sqlite3_value *pVal = sqlite3_column_value(pScan, ctx.iCol+1); + if( pConfig->eContent==FTS5_CONTENT_EXTERNAL + && sqlite3Fts5IsLocaleValue(pConfig, pVal) + ){ + rc = sqlite3Fts5DecodeLocaleValue(pVal, &pText, &nText, &pLoc, &nLoc); + }else{ + pText = (const char*)sqlite3_value_text(pVal); + nText = sqlite3_value_bytes(pVal); + if( pConfig->bLocale ){ + int iCol = ctx.iCol + 1 + pConfig->nCol; + pLoc = (const char*)sqlite3_column_text(pScan, iCol); + nLoc = sqlite3_column_bytes(pScan, iCol); + } + } + + if( rc==SQLITE_OK ){ + sqlite3Fts5SetLocale(pConfig, pLoc, nLoc); + rc = sqlite3Fts5Tokenize(pConfig, + FTS5_TOKENIZE_DOCUMENT, + pText, nText, + (void*)&ctx, + fts5StorageInsertCallback + ); + sqlite3Fts5ClearLocale(pConfig); + } } sqlite3Fts5BufferAppendVarint(&rc, &buf, ctx.szCol); p->aTotalSize[ctx.iCol] += (i64)ctx.szCol; @@ -623,6 +901,8 @@ int sqlite3Fts5StorageRebuild(Fts5Storage *p){ } } sqlite3_free(buf.p); + rc2 = sqlite3_reset(pScan); + if( rc==SQLITE_OK ) rc = rc2; /* Write the averages record */ if( rc==SQLITE_OK ){ @@ -639,6 +919,10 @@ int sqlite3Fts5StorageMerge(Fts5Storage *p, int nMerge){ return sqlite3Fts5IndexMerge(p->pIndex, nMerge); } +int sqlite3Fts5StorageReset(Fts5Storage *p){ + return sqlite3Fts5IndexReset(p->pIndex); +} + /* ** Allocate a new rowid. This is used for "external content" tables when ** a NULL value is inserted into the rowid column. The new rowid is allocated @@ -671,6 +955,7 @@ static int fts5StorageNewRowid(Fts5Storage *p, i64 *piRowid){ */ int sqlite3Fts5StorageContentInsert( Fts5Storage *p, + int bReplace, /* True to use REPLACE instead of INSERT */ sqlite3_value **apVal, i64 *piRowid ){ @@ -678,7 +963,9 @@ int sqlite3Fts5StorageContentInsert( int rc = SQLITE_OK; /* Insert the new row into the %_content table. */ - if( pConfig->eContent!=FTS5_CONTENT_NORMAL ){ + if( pConfig->eContent!=FTS5_CONTENT_NORMAL + && pConfig->eContent!=FTS5_CONTENT_UNINDEXED + ){ if( sqlite3_value_type(apVal[1])==SQLITE_INTEGER ){ *piRowid = sqlite3_value_int64(apVal[1]); }else{ @@ -687,9 +974,52 @@ int sqlite3Fts5StorageContentInsert( }else{ sqlite3_stmt *pInsert = 0; /* Statement to write %_content table */ int i; /* Counter variable */ - rc = fts5StorageGetStmt(p, FTS5_STMT_INSERT_CONTENT, &pInsert, 0); - for(i=1; rc==SQLITE_OK && i<=pConfig->nCol+1; i++){ - rc = sqlite3_bind_value(pInsert, i, apVal[i]); + + assert( FTS5_STMT_INSERT_CONTENT+1==FTS5_STMT_REPLACE_CONTENT ); + assert( bReplace==0 || bReplace==1 ); + rc = fts5StorageGetStmt(p, FTS5_STMT_INSERT_CONTENT+bReplace, &pInsert, 0); + if( pInsert ) sqlite3_clear_bindings(pInsert); + + /* Bind the rowid value */ + sqlite3_bind_value(pInsert, 1, apVal[1]); + + /* Loop through values for user-defined columns. i=2 is the leftmost + ** user-defined column. As is column 1 of pSavedRow. */ + for(i=2; rc==SQLITE_OK && i<=pConfig->nCol+1; i++){ + int bUnindexed = pConfig->abUnindexed[i-2]; + if( pConfig->eContent==FTS5_CONTENT_NORMAL || bUnindexed ){ + sqlite3_value *pVal = apVal[i]; + + if( sqlite3_value_nochange(pVal) && p->pSavedRow ){ + /* This is an UPDATE statement, and user-defined column (i-2) was not + ** modified. Retrieve the value from Fts5Storage.pSavedRow. */ + pVal = sqlite3_column_value(p->pSavedRow, i-1); + if( pConfig->bLocale && bUnindexed==0 ){ + sqlite3_bind_value(pInsert, pConfig->nCol + i, + sqlite3_column_value(p->pSavedRow, pConfig->nCol + i - 1) + ); + } + }else if( sqlite3Fts5IsLocaleValue(pConfig, pVal) ){ + const char *pText = 0; + const char *pLoc = 0; + int nText = 0; + int nLoc = 0; + assert( pConfig->bLocale ); + + rc = sqlite3Fts5DecodeLocaleValue(pVal, &pText, &nText, &pLoc, &nLoc); + if( rc==SQLITE_OK ){ + sqlite3_bind_text(pInsert, i, pText, nText, SQLITE_TRANSIENT); + if( bUnindexed==0 ){ + int iLoc = pConfig->nCol + i; + sqlite3_bind_text(pInsert, iLoc, pLoc, nLoc, SQLITE_TRANSIENT); + } + } + + continue; + } + + rc = sqlite3_bind_value(pInsert, i, pVal); + } } if( rc==SQLITE_OK ){ sqlite3_step(pInsert); @@ -724,13 +1054,38 @@ int sqlite3Fts5StorageIndexInsert( for(ctx.iCol=0; rc==SQLITE_OK && ctx.iColnCol; ctx.iCol++){ ctx.szCol = 0; if( pConfig->abUnindexed[ctx.iCol]==0 ){ - rc = sqlite3Fts5Tokenize(pConfig, - FTS5_TOKENIZE_DOCUMENT, - (const char*)sqlite3_value_text(apVal[ctx.iCol+2]), - sqlite3_value_bytes(apVal[ctx.iCol+2]), - (void*)&ctx, - fts5StorageInsertCallback - ); + int nText = 0; /* Size of pText in bytes */ + const char *pText = 0; /* Pointer to buffer containing text value */ + int nLoc = 0; /* Size of pText in bytes */ + const char *pLoc = 0; /* Pointer to buffer containing text value */ + + sqlite3_value *pVal = apVal[ctx.iCol+2]; + if( p->pSavedRow && sqlite3_value_nochange(pVal) ){ + pVal = sqlite3_column_value(p->pSavedRow, ctx.iCol+1); + if( pConfig->eContent==FTS5_CONTENT_NORMAL && pConfig->bLocale ){ + int iCol = ctx.iCol + 1 + pConfig->nCol; + pLoc = (const char*)sqlite3_column_text(p->pSavedRow, iCol); + nLoc = sqlite3_column_bytes(p->pSavedRow, iCol); + } + }else{ + pVal = apVal[ctx.iCol+2]; + } + + if( pConfig->bLocale && sqlite3Fts5IsLocaleValue(pConfig, pVal) ){ + rc = sqlite3Fts5DecodeLocaleValue(pVal, &pText, &nText, &pLoc, &nLoc); + }else{ + pText = (const char*)sqlite3_value_text(pVal); + nText = sqlite3_value_bytes(pVal); + } + + if( rc==SQLITE_OK ){ + sqlite3Fts5SetLocale(pConfig, pLoc, nLoc); + rc = sqlite3Fts5Tokenize(pConfig, + FTS5_TOKENIZE_DOCUMENT, pText, nText, (void*)&ctx, + fts5StorageInsertCallback + ); + sqlite3Fts5ClearLocale(pConfig); + } } sqlite3Fts5BufferAppendVarint(&rc, &buf, ctx.szCol); p->aTotalSize[ctx.iCol] += (i64)ctx.szCol; @@ -743,11 +1098,6 @@ int sqlite3Fts5StorageIndexInsert( } sqlite3_free(buf.p); - /* Write the averages record */ - if( rc==SQLITE_OK ){ - rc = fts5StorageSaveTotals(p); - } - return rc; } @@ -810,6 +1160,7 @@ static int fts5StorageIntegrityCallback( int iCol; UNUSED_PARAM2(iUnused1, iUnused2); + if( nToken>FTS5_MAX_TOKEN_SIZE ) nToken = FTS5_MAX_TOKEN_SIZE; if( (tflags & FTS5_TOKEN_COLOCATED)==0 || pCtx->szCol==0 ){ pCtx->szCol++; @@ -862,97 +1213,136 @@ static int fts5StorageIntegrityCallback( ** some other SQLite error code if an error occurs while attempting to ** determine this. */ -int sqlite3Fts5StorageIntegrity(Fts5Storage *p){ +int sqlite3Fts5StorageIntegrity(Fts5Storage *p, int iArg){ Fts5Config *pConfig = p->pConfig; - int rc; /* Return code */ + int rc = SQLITE_OK; /* Return code */ int *aColSize; /* Array of size pConfig->nCol */ i64 *aTotalSize; /* Array of size pConfig->nCol */ Fts5IntegrityCtx ctx; sqlite3_stmt *pScan; + int bUseCksum; memset(&ctx, 0, sizeof(Fts5IntegrityCtx)); ctx.pConfig = p->pConfig; - aTotalSize = (i64*)sqlite3_malloc(pConfig->nCol * (sizeof(int)+sizeof(i64))); + aTotalSize = (i64*)sqlite3_malloc64(pConfig->nCol*(sizeof(int)+sizeof(i64))); if( !aTotalSize ) return SQLITE_NOMEM; aColSize = (int*)&aTotalSize[pConfig->nCol]; memset(aTotalSize, 0, sizeof(i64) * pConfig->nCol); - /* Generate the expected index checksum based on the contents of the - ** %_content table. This block stores the checksum in ctx.cksum. */ - rc = fts5StorageGetStmt(p, FTS5_STMT_SCAN, &pScan, 0); - if( rc==SQLITE_OK ){ - int rc2; - while( SQLITE_ROW==sqlite3_step(pScan) ){ - int i; - ctx.iRowid = sqlite3_column_int64(pScan, 0); - ctx.szCol = 0; - if( pConfig->bColumnsize ){ - rc = sqlite3Fts5StorageDocsize(p, ctx.iRowid, aColSize); - } - if( rc==SQLITE_OK && pConfig->eDetail==FTS5_DETAIL_NONE ){ - rc = sqlite3Fts5TermsetNew(&ctx.pTermset); - } - for(i=0; rc==SQLITE_OK && inCol; i++){ - if( pConfig->abUnindexed[i] ) continue; - ctx.iCol = i; + bUseCksum = (pConfig->eContent==FTS5_CONTENT_NORMAL + || (pConfig->eContent==FTS5_CONTENT_EXTERNAL && iArg) + ); + if( bUseCksum ){ + /* Generate the expected index checksum based on the contents of the + ** %_content table. This block stores the checksum in ctx.cksum. */ + rc = fts5StorageGetStmt(p, FTS5_STMT_SCAN, &pScan, 0); + if( rc==SQLITE_OK ){ + int rc2; + while( SQLITE_ROW==sqlite3_step(pScan) ){ + int i; + ctx.iRowid = sqlite3_column_int64(pScan, 0); ctx.szCol = 0; - if( pConfig->eDetail==FTS5_DETAIL_COLUMNS ){ - rc = sqlite3Fts5TermsetNew(&ctx.pTermset); + if( pConfig->bColumnsize ){ + rc = sqlite3Fts5StorageDocsize(p, ctx.iRowid, aColSize); } - if( rc==SQLITE_OK ){ - rc = sqlite3Fts5Tokenize(pConfig, - FTS5_TOKENIZE_DOCUMENT, - (const char*)sqlite3_column_text(pScan, i+1), - sqlite3_column_bytes(pScan, i+1), - (void*)&ctx, - fts5StorageIntegrityCallback - ); - } - if( rc==SQLITE_OK && pConfig->bColumnsize && ctx.szCol!=aColSize[i] ){ - rc = FTS5_CORRUPT; + if( rc==SQLITE_OK && pConfig->eDetail==FTS5_DETAIL_NONE ){ + rc = sqlite3Fts5TermsetNew(&ctx.pTermset); } - aTotalSize[i] += ctx.szCol; - if( pConfig->eDetail==FTS5_DETAIL_COLUMNS ){ - sqlite3Fts5TermsetFree(ctx.pTermset); - ctx.pTermset = 0; + for(i=0; rc==SQLITE_OK && inCol; i++){ + if( pConfig->abUnindexed[i]==0 ){ + const char *pText = 0; + int nText = 0; + const char *pLoc = 0; + int nLoc = 0; + sqlite3_value *pVal = sqlite3_column_value(pScan, i+1); + + if( pConfig->eContent==FTS5_CONTENT_EXTERNAL + && sqlite3Fts5IsLocaleValue(pConfig, pVal) + ){ + rc = sqlite3Fts5DecodeLocaleValue( + pVal, &pText, &nText, &pLoc, &nLoc + ); + }else{ + if( pConfig->eContent==FTS5_CONTENT_NORMAL && pConfig->bLocale ){ + int iCol = i + 1 + pConfig->nCol; + pLoc = (const char*)sqlite3_column_text(pScan, iCol); + nLoc = sqlite3_column_bytes(pScan, iCol); + } + pText = (const char*)sqlite3_value_text(pVal); + nText = sqlite3_value_bytes(pVal); + } + + ctx.iCol = i; + ctx.szCol = 0; + + if( rc==SQLITE_OK && pConfig->eDetail==FTS5_DETAIL_COLUMNS ){ + rc = sqlite3Fts5TermsetNew(&ctx.pTermset); + } + + if( rc==SQLITE_OK ){ + sqlite3Fts5SetLocale(pConfig, pLoc, nLoc); + rc = sqlite3Fts5Tokenize(pConfig, + FTS5_TOKENIZE_DOCUMENT, + pText, nText, + (void*)&ctx, + fts5StorageIntegrityCallback + ); + sqlite3Fts5ClearLocale(pConfig); + } + + /* If this is not a columnsize=0 database, check that the number + ** of tokens in the value matches the aColSize[] value read from + ** the %_docsize table. */ + if( rc==SQLITE_OK + && pConfig->bColumnsize + && ctx.szCol!=aColSize[i] + ){ + rc = FTS5_CORRUPT; + } + aTotalSize[i] += ctx.szCol; + if( pConfig->eDetail==FTS5_DETAIL_COLUMNS ){ + sqlite3Fts5TermsetFree(ctx.pTermset); + ctx.pTermset = 0; + } + } } + sqlite3Fts5TermsetFree(ctx.pTermset); + ctx.pTermset = 0; + + if( rc!=SQLITE_OK ) break; } - sqlite3Fts5TermsetFree(ctx.pTermset); - ctx.pTermset = 0; - - if( rc!=SQLITE_OK ) break; + rc2 = sqlite3_reset(pScan); + if( rc==SQLITE_OK ) rc = rc2; } - rc2 = sqlite3_reset(pScan); - if( rc==SQLITE_OK ) rc = rc2; - } - /* Test that the "totals" (sometimes called "averages") record looks Ok */ - if( rc==SQLITE_OK ){ - int i; - rc = fts5StorageLoadTotals(p, 0); - for(i=0; rc==SQLITE_OK && inCol; i++){ - if( p->aTotalSize[i]!=aTotalSize[i] ) rc = FTS5_CORRUPT; + /* Test that the "totals" (sometimes called "averages") record looks Ok */ + if( rc==SQLITE_OK ){ + int i; + rc = fts5StorageLoadTotals(p, 0); + for(i=0; rc==SQLITE_OK && inCol; i++){ + if( p->aTotalSize[i]!=aTotalSize[i] ) rc = FTS5_CORRUPT; + } } - } - /* Check that the %_docsize and %_content tables contain the expected - ** number of rows. */ - if( rc==SQLITE_OK && pConfig->eContent==FTS5_CONTENT_NORMAL ){ - i64 nRow = 0; - rc = fts5StorageCount(p, "content", &nRow); - if( rc==SQLITE_OK && nRow!=p->nTotalRow ) rc = FTS5_CORRUPT; - } - if( rc==SQLITE_OK && pConfig->bColumnsize ){ - i64 nRow = 0; - rc = fts5StorageCount(p, "docsize", &nRow); - if( rc==SQLITE_OK && nRow!=p->nTotalRow ) rc = FTS5_CORRUPT; + /* Check that the %_docsize and %_content tables contain the expected + ** number of rows. */ + if( rc==SQLITE_OK && pConfig->eContent==FTS5_CONTENT_NORMAL ){ + i64 nRow = 0; + rc = fts5StorageCount(p, "content", &nRow); + if( rc==SQLITE_OK && nRow!=p->nTotalRow ) rc = FTS5_CORRUPT; + } + if( rc==SQLITE_OK && pConfig->bColumnsize ){ + i64 nRow = 0; + rc = fts5StorageCount(p, "docsize", &nRow); + if( rc==SQLITE_OK && nRow!=p->nTotalRow ) rc = FTS5_CORRUPT; + } } /* Pass the expected checksum down to the FTS index module. It will ** verify, amongst other things, that it matches the checksum generated by ** inspecting the index itself. */ if( rc==SQLITE_OK ){ - rc = sqlite3Fts5IndexIntegrityCheck(p->pIndex, ctx.cksum); + rc = sqlite3Fts5IndexIntegrityCheck(p->pIndex, ctx.cksum, bUseCksum); } sqlite3_free(aTotalSize); @@ -1032,8 +1422,9 @@ int sqlite3Fts5StorageDocsize(Fts5Storage *p, i64 iRowid, int *aCol){ assert( p->pConfig->bColumnsize ); rc = fts5StorageGetStmt(p, FTS5_STMT_LOOKUP_DOCSIZE, &pLookup, 0); - if( rc==SQLITE_OK ){ + if( pLookup ){ int bCorrupt = 1; + assert( rc==SQLITE_OK ); sqlite3_bind_int64(pLookup, 1, iRowid); if( SQLITE_ROW==sqlite3_step(pLookup) ){ const u8 *aBlob = sqlite3_column_blob(pLookup, 0); @@ -1046,6 +1437,8 @@ int sqlite3Fts5StorageDocsize(Fts5Storage *p, i64 iRowid, int *aCol){ if( bCorrupt && rc==SQLITE_OK ){ rc = FTS5_CORRUPT; } + }else{ + assert( rc!=SQLITE_OK ); } return rc; @@ -1072,7 +1465,13 @@ int sqlite3Fts5StorageSize(Fts5Storage *p, int iCol, i64 *pnToken){ int sqlite3Fts5StorageRowCount(Fts5Storage *p, i64 *pnRow){ int rc = fts5StorageLoadTotals(p, 0); if( rc==SQLITE_OK ){ + /* nTotalRow being zero does not necessarily indicate a corrupt + ** database - it might be that the FTS5 table really does contain zero + ** rows. However this function is only called from the xRowCount() API, + ** and there is no way for that API to be invoked if the table contains + ** no rows. Hence the FTS5_CORRUPT return. */ *pnRow = p->nTotalRow; + if( p->nTotalRow<=0 ) rc = FTS5_CORRUPT; } return rc; } @@ -1080,13 +1479,20 @@ int sqlite3Fts5StorageRowCount(Fts5Storage *p, i64 *pnRow){ /* ** Flush any data currently held in-memory to disk. */ -int sqlite3Fts5StorageSync(Fts5Storage *p, int bCommit){ - if( bCommit && p->bTotalsValid ){ - int rc = fts5StorageSaveTotals(p); - p->bTotalsValid = 0; - if( rc!=SQLITE_OK ) return rc; +int sqlite3Fts5StorageSync(Fts5Storage *p){ + int rc = SQLITE_OK; + i64 iLastRowid = sqlite3_last_insert_rowid(p->pConfig->db); + if( p->bTotalsValid ){ + rc = fts5StorageSaveTotals(p); + if( rc==SQLITE_OK ){ + p->bTotalsValid = 0; + } + } + if( rc==SQLITE_OK ){ + rc = sqlite3Fts5IndexSync(p->pIndex); } - return sqlite3Fts5IndexSync(p->pIndex, bCommit); + sqlite3_set_last_insert_rowid(p->pConfig->db, iLastRowid); + return rc; } int sqlite3Fts5StorageRollback(Fts5Storage *p){ @@ -1111,6 +1517,7 @@ int sqlite3Fts5StorageConfigValue( } sqlite3_step(pReplace); rc = sqlite3_reset(pReplace); + sqlite3_bind_null(pReplace, 1); } if( rc==SQLITE_OK && pVal ){ int iNew = p->pConfig->iCookie + 1; @@ -1121,5 +1528,3 @@ int sqlite3Fts5StorageConfigValue( } return rc; } - - diff --git a/ext/fts5/fts5_tcl.c b/ext/fts5/fts5_tcl.c index 72db65777f..25cd5c0633 100644 --- a/ext/fts5/fts5_tcl.c +++ b/ext/fts5/fts5_tcl.c @@ -14,15 +14,18 @@ #ifdef SQLITE_TEST -#include +#include "tclsqlite.h" #ifdef SQLITE_ENABLE_FTS5 #include "fts5.h" #include #include +#include +#ifdef SQLITE_DEBUG extern int sqlite3_fts5_may_be_corrupt; +#endif extern int sqlite3Fts5TestRegisterMatchinfo(sqlite3*); extern int sqlite3Fts5TestRegisterTok(sqlite3*, fts5_api*); @@ -78,7 +81,7 @@ static int f5tResultToErrorCode(const char *zRes){ return SQLITE_ERROR; } -static int f5tDbAndApi( +static int SQLITE_TCLAPI f5tDbAndApi( Tcl_Interp *interp, Tcl_Obj *pObj, sqlite3 **ppDb, @@ -92,19 +95,16 @@ static int f5tDbAndApi( sqlite3_stmt *pStmt = 0; fts5_api *pApi = 0; - rc = sqlite3_prepare_v2(db, "SELECT fts5()", -1, &pStmt, 0); + rc = sqlite3_prepare_v2(db, "SELECT fts5(?1)", -1, &pStmt, 0); if( rc!=SQLITE_OK ){ - Tcl_AppendResult(interp, "error: ", sqlite3_errmsg(db), 0); + Tcl_AppendResult(interp, "error: ", sqlite3_errmsg(db), (char*)0); return TCL_ERROR; } - - if( SQLITE_ROW==sqlite3_step(pStmt) ){ - const void *pPtr = sqlite3_column_blob(pStmt, 0); - memcpy((void*)&pApi, pPtr, sizeof(pApi)); - } + sqlite3_bind_pointer(pStmt, 1, (void*)&pApi, "fts5_api_ptr", 0); + sqlite3_step(pStmt); if( sqlite3_finalize(pStmt)!=SQLITE_OK ){ - Tcl_AppendResult(interp, "error: ", sqlite3_errmsg(db), 0); + Tcl_AppendResult(interp, "error: ", sqlite3_errmsg(db), (char*)0); return TCL_ERROR; } @@ -164,7 +164,7 @@ static int xTokenizeCb( return rc; } -static int xF5tApi(void*, Tcl_Interp*, int, Tcl_Obj *CONST []); +static int SQLITE_TCLAPI xF5tApi(void*, Tcl_Interp*, int, Tcl_Obj *CONST []); static int xQueryPhraseCb( const Fts5ExtensionApi *pApi, @@ -209,7 +209,7 @@ static void xSetAuxdataDestructor(void *p){ ** ** Description... */ -static int xF5tApi( +static int SQLITE_TCLAPI xF5tApi( void * clientData, Tcl_Interp *interp, int objc, @@ -238,6 +238,10 @@ static int xF5tApi( { "xGetAuxdataInt", 1, "CLEAR" }, /* 15 */ { "xPhraseForeach", 4, "IPHRASE COLVAR OFFVAR SCRIPT" }, /* 16 */ { "xPhraseColumnForeach", 3, "IPHRASE COLVAR SCRIPT" }, /* 17 */ + + { "xQueryToken", 2, "IPHRASE ITERM" }, /* 18 */ + { "xInstToken", 2, "IDX ITERM" }, /* 19 */ + { "xColumnLocale", 1, "COL" }, /* 20 */ { 0, 0, 0} }; @@ -288,12 +292,12 @@ static int xF5tApi( break; } CASE(3, "xTokenize") { - int nText; + Tcl_Size nText; char *zText = Tcl_GetStringFromObj(objv[2], &nText); F5tFunction ctx; ctx.interp = interp; ctx.pScript = objv[3]; - rc = p->pApi->xTokenize(p->pFts, zText, nText, &ctx, xTokenizeCb); + rc = p->pApi->xTokenize(p->pFts, zText, (int)nText, &ctx, xTokenizeCb); if( rc==SQLITE_OK ){ Tcl_ResetResult(interp); } @@ -389,7 +393,7 @@ static int xF5tApi( CASE(12, "xSetAuxdata") { F5tAuxData *pData = (F5tAuxData*)sqlite3_malloc(sizeof(F5tAuxData)); if( pData==0 ){ - Tcl_AppendResult(interp, "out of memory", 0); + Tcl_AppendResult(interp, "out of memory", (char*)0); return TCL_ERROR; } pData->pObj = objv[2]; @@ -429,7 +433,7 @@ static int xF5tApi( int iVal; int bClear; if( Tcl_GetBooleanFromObj(interp, objv[2], &bClear) ) return TCL_ERROR; - iVal = ((char*)p->pApi->xGetAuxdata(p->pFts, bClear) - (char*)0); + iVal = (int)((char*)p->pApi->xGetAuxdata(p->pFts, bClear) - (char*)0); Tcl_SetObjResult(interp, Tcl_NewIntObj(iVal)); break; } @@ -449,7 +453,7 @@ static int xF5tApi( rc = p->pApi->xPhraseFirst(p->pFts, iPhrase, &iter, &iCol, &iOff); if( rc!=SQLITE_OK ){ - Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); + Tcl_AppendResult(interp, sqlite3ErrName(rc), (char*)0); return TCL_ERROR; } for( ;iCol>=0; p->pApi->xPhraseNext(p->pFts, &iter, &iCol, &iOff) ){ @@ -478,7 +482,7 @@ static int xF5tApi( rc = p->pApi->xPhraseFirstColumn(p->pFts, iPhrase, &iter, &iCol); if( rc!=SQLITE_OK ){ - Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); + Tcl_SetResult(interp, (char*)sqlite3ErrName(rc), TCL_VOLATILE); return TCL_ERROR; } for( ; iCol>=0; p->pApi->xPhraseNextColumn(p->pFts, &iter, &iCol)){ @@ -494,6 +498,52 @@ static int xF5tApi( break; } + CASE(18, "xQueryToken") { + const char *pTerm = 0; + int nTerm = 0; + int iPhrase = 0; + int iTerm = 0; + + if( Tcl_GetIntFromObj(interp, objv[2], &iPhrase) ) return TCL_ERROR; + if( Tcl_GetIntFromObj(interp, objv[3], &iTerm) ) return TCL_ERROR; + rc = p->pApi->xQueryToken(p->pFts, iPhrase, iTerm, &pTerm, &nTerm); + if( rc==SQLITE_OK ){ + Tcl_SetObjResult(interp, Tcl_NewStringObj(pTerm, nTerm)); + } + + break; + } + + CASE(19, "xInstToken") { + const char *pTerm = 0; + int nTerm = 0; + int iIdx = 0; + int iTerm = 0; + + if( Tcl_GetIntFromObj(interp, objv[2], &iIdx) ) return TCL_ERROR; + if( Tcl_GetIntFromObj(interp, objv[3], &iTerm) ) return TCL_ERROR; + rc = p->pApi->xInstToken(p->pFts, iIdx, iTerm, &pTerm, &nTerm); + if( rc==SQLITE_OK ){ + Tcl_SetObjResult(interp, Tcl_NewStringObj(pTerm, nTerm)); + } + + break; + } + + CASE(20, "xColumnLocale") { + const char *z = 0; + int n = 0; + int iCol; + if( Tcl_GetIntFromObj(interp, objv[2], &iCol) ){ + return TCL_ERROR; + } + rc = p->pApi->xColumnLocale(p->pFts, iCol, &z, &n); + if( rc==SQLITE_OK && z ){ + Tcl_SetObjResult(interp, Tcl_NewStringObj(z, n)); + } + break; + } + default: assert( 0 ); break; @@ -564,15 +614,16 @@ static void xF5tFunction( sqlite3_result_error(pCtx, Tcl_GetStringResult(p->interp), -1); }else{ Tcl_Obj *pVar = Tcl_GetObjResult(p->interp); - int n; const char *zType = (pVar->typePtr ? pVar->typePtr->name : ""); char c = zType[0]; if( c=='b' && strcmp(zType,"bytearray")==0 && pVar->bytes==0 ){ /* Only return a BLOB type if the Tcl variable is a bytearray and ** has no string representation. */ - unsigned char *data = Tcl_GetByteArrayFromObj(pVar, &n); - sqlite3_result_blob(pCtx, data, n, SQLITE_TRANSIENT); + Tcl_Size nn; + unsigned char *data = Tcl_GetByteArrayFromObj(pVar, &nn); + sqlite3_result_blob(pCtx, data, (int)nn, SQLITE_TRANSIENT); }else if( c=='b' && strcmp(zType,"boolean")==0 ){ + int n; Tcl_GetIntFromObj(0, pVar, &n); sqlite3_result_int(pCtx, n); }else if( c=='d' && strcmp(zType,"double")==0 ){ @@ -585,8 +636,9 @@ static void xF5tFunction( Tcl_GetWideIntFromObj(0, pVar, &v); sqlite3_result_int64(pCtx, v); }else{ - unsigned char *data = (unsigned char *)Tcl_GetStringFromObj(pVar, &n); - sqlite3_result_text(pCtx, (char *)data, n, SQLITE_TRANSIENT); + Tcl_Size nn; + unsigned char *data = (unsigned char *)Tcl_GetStringFromObj(pVar, &nn); + sqlite3_result_text(pCtx, (char*)data, (int)nn, SQLITE_TRANSIENT); } } } @@ -602,7 +654,7 @@ static void xF5tDestroy(void *pCtx){ ** ** Description... */ -static int f5tCreateFunction( +static int SQLITE_TCLAPI f5tCreateFunction( void * clientData, Tcl_Interp *interp, int objc, @@ -632,7 +684,7 @@ static int f5tCreateFunction( pApi, zName, (void*)pCtx, xF5tFunction, xF5tDestroy ); if( rc!=SQLITE_OK ){ - Tcl_AppendResult(interp, "error: ", sqlite3_errmsg(db), 0); + Tcl_AppendResult(interp, "error: ", sqlite3_errmsg(db), (char*)0); return TCL_ERROR; } @@ -672,14 +724,15 @@ static int xTokenizeCb2( ** ** Description... */ -static int f5tTokenize( +static int SQLITE_TCLAPI f5tTokenize( void * clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[] ){ - char *zText; - int nText; + char *pCopy = 0; + char *zText = 0; + Tcl_Size nText = 0; sqlite3 *db = 0; fts5_api *pApi = 0; Fts5Tokenizer *pTok = 0; @@ -688,7 +741,7 @@ static int f5tTokenize( void *pUserdata; int rc; - int nArg; + Tcl_Size nArg; const char **azArg; F5tTokenizeCtx ctx; @@ -699,7 +752,7 @@ static int f5tTokenize( if( objc==5 ){ char *zOpt = Tcl_GetString(objv[1]); if( strcmp("-subst", zOpt) ){ - Tcl_AppendResult(interp, "unrecognized option: ", zOpt, 0); + Tcl_AppendResult(interp, "unrecognized option: ", zOpt, (char*)0); return TCL_ERROR; } } @@ -708,7 +761,7 @@ static int f5tTokenize( return TCL_ERROR; } if( nArg==0 ){ - Tcl_AppendResult(interp, "no such tokenizer: ", 0); + Tcl_AppendResult(interp, "no such tokenizer: ", (char*)0); Tcl_Free((void*)azArg); return TCL_ERROR; } @@ -716,32 +769,43 @@ static int f5tTokenize( rc = pApi->xFindTokenizer(pApi, azArg[0], &pUserdata, &tokenizer); if( rc!=SQLITE_OK ){ - Tcl_AppendResult(interp, "no such tokenizer: ", azArg[0], 0); + Tcl_AppendResult(interp, "no such tokenizer: ", azArg[0], (char*)0); return TCL_ERROR; } - rc = tokenizer.xCreate(pUserdata, &azArg[1], nArg-1, &pTok); + rc = tokenizer.xCreate(pUserdata, &azArg[1], (int)(nArg-1), &pTok); if( rc!=SQLITE_OK ){ - Tcl_AppendResult(interp, "error in tokenizer.xCreate()", 0); + Tcl_AppendResult(interp, "error in tokenizer.xCreate()", (char*)0); return TCL_ERROR; } + if( nText>0 ){ + pCopy = sqlite3_malloc(nText); + if( pCopy==0 ){ + tokenizer.xDelete(pTok); + Tcl_AppendResult(interp, "error in sqlite3_malloc()", (char*)0); + return TCL_ERROR; + }else{ + memcpy(pCopy, zText, nText); + } + } + pRet = Tcl_NewObj(); Tcl_IncrRefCount(pRet); ctx.bSubst = (objc==5); ctx.pRet = pRet; - ctx.zInput = zText; + ctx.zInput = pCopy; rc = tokenizer.xTokenize( - pTok, (void*)&ctx, FTS5_TOKENIZE_DOCUMENT, zText, nText, xTokenizeCb2 + pTok, (void*)&ctx, FTS5_TOKENIZE_DOCUMENT, pCopy,(int)nText, xTokenizeCb2 ); tokenizer.xDelete(pTok); + sqlite3_free(pCopy); if( rc!=SQLITE_OK ){ - Tcl_AppendResult(interp, "error in tokenizer.xTokenize()", 0); + Tcl_AppendResult(interp, "error in tokenizer.xTokenize()", (char*)0); Tcl_DecrRefCount(pRet); return TCL_ERROR; } - Tcl_Free((void*)azArg); Tcl_SetObjResult(interp, pRet); Tcl_DecrRefCount(pRet); @@ -760,18 +824,32 @@ typedef struct F5tTokenizerInstance F5tTokenizerInstance; struct F5tTokenizerContext { void *pCtx; int (*xToken)(void*, int, const char*, int, int, int); + F5tTokenizerInstance *pInst; }; struct F5tTokenizerModule { Tcl_Interp *interp; Tcl_Obj *pScript; + void *pParentCtx; + fts5_tokenizer_v2 parent_v2; + fts5_tokenizer parent; F5tTokenizerContext *pContext; }; +/* +** zLocale: +** Within a call to xTokenize_v2(), pLocale/nLocale store the locale +** passed to the call by fts5. This can be retrieved by a Tcl tokenize +** script using [sqlite3_fts5_locale]. +*/ struct F5tTokenizerInstance { Tcl_Interp *interp; Tcl_Obj *pScript; + F5tTokenizerModule *pModule; + Fts5Tokenizer *pParent; F5tTokenizerContext *pContext; + const char *pLocale; + int nLocale; }; static int f5tTokenizerCreate( @@ -780,11 +858,20 @@ static int f5tTokenizerCreate( int nArg, Fts5Tokenizer **ppOut ){ + Fts5Tokenizer *pParent = 0; F5tTokenizerModule *pMod = (F5tTokenizerModule*)pCtx; Tcl_Obj *pEval; int rc = TCL_OK; int i; + assert( pMod->parent_v2.xCreate==0 || pMod->parent.xCreate==0 ); + if( pMod->parent_v2.xCreate ){ + rc = pMod->parent_v2.xCreate(pMod->pParentCtx, 0, 0, &pParent); + } + if( pMod->parent.xCreate ){ + rc = pMod->parent.xCreate(pMod->pParentCtx, 0, 0, &pParent); + } + pEval = Tcl_DuplicateObj(pMod->pScript); Tcl_IncrRefCount(pEval); for(i=0; rc==TCL_OK && iinterp = pMod->interp; pInst->pScript = Tcl_GetObjResult(pMod->interp); pInst->pContext = pMod->pContext; + pInst->pParent = pParent; + pInst->pModule = pMod; Tcl_IncrRefCount(pInst->pScript); *ppOut = (Fts5Tokenizer*)pInst; } @@ -814,11 +903,21 @@ static int f5tTokenizerCreate( static void f5tTokenizerDelete(Fts5Tokenizer *p){ F5tTokenizerInstance *pInst = (F5tTokenizerInstance*)p; - Tcl_DecrRefCount(pInst->pScript); - ckfree((char *)pInst); + if( pInst ){ + if( pInst->pParent ){ + if( pInst->pModule->parent_v2.xDelete ){ + pInst->pModule->parent_v2.xDelete(pInst->pParent); + }else{ + pInst->pModule->parent.xDelete(pInst->pParent); + } + } + Tcl_DecrRefCount(pInst->pScript); + ckfree((char *)pInst); + } } -static int f5tTokenizerTokenize( + +static int f5tTokenizerReallyTokenize( Fts5Tokenizer *p, void *pCtx, int flags, @@ -826,6 +925,7 @@ static int f5tTokenizerTokenize( int (*xToken)(void*, int, const char*, int, int, int) ){ F5tTokenizerInstance *pInst = (F5tTokenizerInstance*)p; + F5tTokenizerInstance *pOldInst = 0; void *pOldCtx; int (*xOldToken)(void*, int, const char*, int, int, int); Tcl_Obj *pEval; @@ -834,9 +934,11 @@ static int f5tTokenizerTokenize( pOldCtx = pInst->pContext->pCtx; xOldToken = pInst->pContext->xToken; + pOldInst = pInst->pContext->pInst; pInst->pContext->pCtx = pCtx; pInst->pContext->xToken = xToken; + pInst->pContext->pInst = pInst; assert( flags==FTS5_TOKENIZE_DOCUMENT @@ -872,13 +974,109 @@ static int f5tTokenizerTokenize( pInst->pContext->pCtx = pOldCtx; pInst->pContext->xToken = xOldToken; + pInst->pContext->pInst = pOldInst; + return rc; +} + +typedef struct CallbackCtx CallbackCtx; +struct CallbackCtx { + Fts5Tokenizer *p; + void *pCtx; + int flags; + int (*xToken)(void*, int, const char*, int, int, int); +}; + +static int f5tTokenizeCallback( + void *pCtx, + int tflags, + const char *z, int n, + int iStart, int iEnd +){ + CallbackCtx *p = (CallbackCtx*)pCtx; + return f5tTokenizerReallyTokenize(p->p, p->pCtx, p->flags, z, n, p->xToken); +} + +static int f5tTokenizerTokenize_v2( + Fts5Tokenizer *p, + void *pCtx, + int flags, + const char *pText, int nText, + const char *pLoc, int nLoc, + int (*xToken)(void*, int, const char*, int, int, int) +){ + int rc = SQLITE_OK; + F5tTokenizerInstance *pInst = (F5tTokenizerInstance*)p; + + pInst->pLocale = pLoc; + pInst->nLocale = nLoc; + + if( pInst->pParent ){ + CallbackCtx ctx; + ctx.p = p; + ctx.pCtx = pCtx; + ctx.flags = flags; + ctx.xToken = xToken; + if( pInst->pModule->parent_v2.xTokenize ){ + rc = pInst->pModule->parent_v2.xTokenize( + pInst->pParent, (void*)&ctx, flags, pText, nText, + pLoc, nLoc, f5tTokenizeCallback + ); + }else{ + rc = pInst->pModule->parent.xTokenize( + pInst->pParent, (void*)&ctx, flags, pText, nText, f5tTokenizeCallback + ); + } + }else{ + rc = f5tTokenizerReallyTokenize(p, pCtx, flags, pText, nText, xToken); + } + + pInst->pLocale = 0; + pInst->nLocale = 0; return rc; } +static int f5tTokenizerTokenize( + Fts5Tokenizer *p, + void *pCtx, + int flags, + const char *pText, int nText, + int (*xToken)(void*, int, const char*, int, int, int) +){ + return f5tTokenizerTokenize_v2(p, pCtx, flags, pText, nText, 0, 0, xToken); +} + +/* +** sqlite3_fts5_locale +*/ +static int SQLITE_TCLAPI f5tTokenizerLocale( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + F5tTokenizerContext *p = (F5tTokenizerContext*)clientData; + + if( objc!=1 ){ + Tcl_WrongNumArgs(interp, 1, objv, ""); + return TCL_ERROR; + } + + if( p->xToken==0 ){ + Tcl_AppendResult(interp, + "sqlite3_fts5_locale may only be used by tokenizer callback", (char*)0 + ); + return TCL_ERROR; + } + + Tcl_SetObjResult(interp, + Tcl_NewStringObj(p->pInst->pLocale, p->pInst->nLocale) + ); + return TCL_OK; +} /* ** sqlite3_fts5_token ?-colocated? TEXT START END */ -static int f5tTokenizerReturn( +static int SQLITE_TCLAPI f5tTokenizerReturn( void * clientData, Tcl_Interp *interp, int objc, @@ -887,13 +1085,13 @@ static int f5tTokenizerReturn( F5tTokenizerContext *p = (F5tTokenizerContext*)clientData; int iStart; int iEnd; - int nToken; + Tcl_Size nToken; int tflags = 0; char *zToken; int rc; if( objc==5 ){ - int nArg; + Tcl_Size nArg; char *zArg = Tcl_GetStringFromObj(objv[1], &nArg); if( nArg<=10 && nArg>=2 && memcmp("-colocated", zArg, nArg)==0 ){ tflags |= FTS5_TOKEN_COLOCATED; @@ -913,14 +1111,14 @@ static int f5tTokenizerReturn( if( p->xToken==0 ){ Tcl_AppendResult(interp, - "sqlite3_fts5_token may only be used by tokenizer callback", 0 + "sqlite3_fts5_token may only be used by tokenizer callback", (char*)0 ); return TCL_ERROR; } - rc = p->xToken(p->pCtx, tflags, zToken, nToken, iStart, iEnd); + rc = p->xToken(p->pCtx, tflags, zToken, (int)nToken, iStart, iEnd); Tcl_SetResult(interp, (char*)sqlite3ErrName(rc), TCL_VOLATILE); - return TCL_OK; + return rc==SQLITE_OK ? TCL_OK : TCL_ERROR; usage: Tcl_WrongNumArgs(interp, 1, objv, "?-colocated? TEXT START END"); @@ -949,7 +1147,7 @@ static void f5tDelTokenizer(void *pCtx){ ** SCRIPT2 should invoke the [sqlite3_fts5_token] command once for each ** token within the tokenized text. */ -static int f5tCreateTokenizer( +static int SQLITE_TCLAPI f5tCreateTokenizer( ClientData clientData, Tcl_Interp *interp, int objc, @@ -960,39 +1158,119 @@ static int f5tCreateTokenizer( fts5_api *pApi; char *zName; Tcl_Obj *pScript; - fts5_tokenizer t; F5tTokenizerModule *pMod; - int rc; - - if( objc!=4 ){ - Tcl_WrongNumArgs(interp, 1, objv, "DB NAME SCRIPT"); + int rc = SQLITE_OK; + int bV2 = 0; /* True to use _v2 API */ + int iVersion = 2; /* Value for _v2.iVersion */ + const char *zParent = 0; /* Name of parent tokenizer, if any */ + int ii = 0; + + if( objc<4 ){ + Tcl_WrongNumArgs(interp, 1, objv, "?OPTIONS? DB NAME SCRIPT"); return TCL_ERROR; } - if( f5tDbAndApi(interp, objv[1], &db, &pApi) ){ - return TCL_ERROR; + + /* Parse any options. Set stack variables bV2 and zParent. */ + for(ii=1; iiinterp = interp; pMod->pScript = pScript; - pMod->pContext = pContext; Tcl_IncrRefCount(pScript); - rc = pApi->xCreateTokenizer(pApi, zName, (void*)pMod, &t, f5tDelTokenizer); + pMod->pContext = pContext; + if( zParent ){ + if( bV2 ){ + fts5_tokenizer_v2 *pParent = 0; + rc = pApi->xFindTokenizer_v2(pApi, zParent, &pMod->pParentCtx, &pParent); + if( rc==SQLITE_OK ){ + memcpy(&pMod->parent_v2, pParent, sizeof(fts5_tokenizer_v2)); + pMod->parent_v2.xDelete(0); + } + }else{ + rc = pApi->xFindTokenizer(pApi, zParent, &pMod->pParentCtx,&pMod->parent); + if( rc==SQLITE_OK ){ + pMod->parent.xDelete(0); + } + } + } + + if( rc==SQLITE_OK ){ + void *pModCtx = (void*)pMod; + if( bV2==0 ){ + fts5_tokenizer t; + t.xCreate = f5tTokenizerCreate; + t.xTokenize = f5tTokenizerTokenize; + t.xDelete = f5tTokenizerDelete; + rc = pApi->xCreateTokenizer(pApi, zName, pModCtx, &t, f5tDelTokenizer); + }else{ + fts5_tokenizer_v2 t2; + memset(&t2, 0, sizeof(t2)); + t2.iVersion = iVersion; + t2.xCreate = f5tTokenizerCreate; + t2.xTokenize = f5tTokenizerTokenize_v2; + t2.xDelete = f5tTokenizerDelete; + rc = pApi->xCreateTokenizer_v2(pApi, zName, pModCtx, &t2,f5tDelTokenizer); + } + } + if( rc!=SQLITE_OK ){ - Tcl_AppendResult(interp, "error in fts5_api.xCreateTokenizer()", 0); + Tcl_AppendResult(interp, ( + bV2 ? "error in fts5_api.xCreateTokenizer_v2()" + : "error in fts5_api.xCreateTokenizer()" + ), (char*)0); return TCL_ERROR; } return TCL_OK; } -static void xF5tFree(ClientData clientData){ +static void SQLITE_TCLAPI xF5tFree(ClientData clientData){ ckfree(clientData); } @@ -1001,12 +1279,13 @@ static void xF5tFree(ClientData clientData){ ** ** Set or clear the global "may-be-corrupt" flag. Return the old value. */ -static int f5tMayBeCorrupt( +static int SQLITE_TCLAPI f5tMayBeCorrupt( void * clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[] ){ +#ifdef SQLITE_DEBUG int bOld = sqlite3_fts5_may_be_corrupt; if( objc!=2 && objc!=1 ){ @@ -1020,6 +1299,7 @@ static int f5tMayBeCorrupt( } Tcl_SetObjResult(interp, Tcl_NewIntObj(bOld)); +#endif return TCL_OK; } @@ -1033,14 +1313,14 @@ static unsigned int f5t_fts5HashKey(int nSlot, const char *p, int n){ return (h % nSlot); } -static int f5tTokenHash( +static int SQLITE_TCLAPI f5tTokenHash( void * clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[] ){ char *z; - int n; + Tcl_Size n; unsigned int iVal; int nSlot; @@ -1053,12 +1333,12 @@ static int f5tTokenHash( } z = Tcl_GetStringFromObj(objv[2], &n); - iVal = f5t_fts5HashKey(nSlot, z, n); + iVal = f5t_fts5HashKey(nSlot, z, (int)n); Tcl_SetObjResult(interp, Tcl_NewIntObj(iVal)); return TCL_OK; } -static int f5tRegisterMatchinfo( +static int SQLITE_TCLAPI f5tRegisterMatchinfo( void * clientData, Tcl_Interp *interp, int objc, @@ -1083,7 +1363,7 @@ static int f5tRegisterMatchinfo( return TCL_OK; } -static int f5tRegisterTok( +static int SQLITE_TCLAPI f5tRegisterTok( void * clientData, Tcl_Interp *interp, int objc, @@ -1109,6 +1389,313 @@ static int f5tRegisterTok( return TCL_OK; } +typedef struct OriginTextCtx OriginTextCtx; +struct OriginTextCtx { + sqlite3 *db; + fts5_api *pApi; +}; + +typedef struct OriginTextTokenizer OriginTextTokenizer; +struct OriginTextTokenizer { + Fts5Tokenizer *pTok; /* Underlying tokenizer object */ + fts5_tokenizer tokapi; /* API implementation for pTok */ +}; + +/* +** Delete the OriginTextCtx object indicated by the only argument. +*/ +static void f5tOrigintextTokenizerDelete(void *pCtx){ + OriginTextCtx *p = (OriginTextCtx*)pCtx; + ckfree((char*)p); +} + +static int f5tOrigintextCreate( + void *pCtx, + const char **azArg, + int nArg, + Fts5Tokenizer **ppOut +){ + OriginTextCtx *p = (OriginTextCtx*)pCtx; + OriginTextTokenizer *pTok = 0; + void *pTokCtx = 0; + int rc = SQLITE_OK; + + pTok = (OriginTextTokenizer*)sqlite3_malloc(sizeof(OriginTextTokenizer)); + if( pTok==0 ){ + rc = SQLITE_NOMEM; + }else if( nArg<1 ){ + rc = SQLITE_ERROR; + }else{ + /* Locate the underlying tokenizer */ + rc = p->pApi->xFindTokenizer(p->pApi, azArg[0], &pTokCtx, &pTok->tokapi); + } + + /* Create the new tokenizer instance */ + if( rc==SQLITE_OK ){ + rc = pTok->tokapi.xCreate(pTokCtx, &azArg[1], nArg-1, &pTok->pTok); + } + + if( rc!=SQLITE_OK ){ + sqlite3_free(pTok); + pTok = 0; + } + *ppOut = (Fts5Tokenizer*)pTok; + return rc; +} + +static void f5tOrigintextDelete(Fts5Tokenizer *pTokenizer){ + OriginTextTokenizer *p = (OriginTextTokenizer*)pTokenizer; + if( p->pTok ){ + p->tokapi.xDelete(p->pTok); + } + sqlite3_free(p); +} + +typedef struct OriginTextCb OriginTextCb; +struct OriginTextCb { + void *pCtx; + const char *pText; + int nText; + int (*xToken)(void *, int, const char *, int, int, int); + + char *aBuf; /* Buffer to use */ + int nBuf; /* Allocated size of aBuf[] */ +}; + +static int xOriginToken( + void *pCtx, /* Copy of 2nd argument to xTokenize() */ + int tflags, /* Mask of FTS5_TOKEN_* flags */ + const char *pToken, /* Pointer to buffer containing token */ + int nToken, /* Size of token in bytes */ + int iStart, /* Byte offset of token within input text */ + int iEnd /* Byte offset of end of token within input */ +){ + OriginTextCb *p = (OriginTextCb*)pCtx; + int ret = 0; + + if( nToken==(iEnd-iStart) && 0==memcmp(pToken, &p->pText[iStart], nToken) ){ + /* Token exactly matches document text. Pass it through as is. */ + ret = p->xToken(p->pCtx, tflags, pToken, nToken, iStart, iEnd); + }else{ + int nReq = nToken + 1 + (iEnd-iStart); + if( nReq>p->nBuf ){ + sqlite3_free(p->aBuf); + p->aBuf = sqlite3_malloc(nReq*2); + if( p->aBuf==0 ) return SQLITE_NOMEM; + p->nBuf = nReq*2; + } + + memcpy(p->aBuf, pToken, nToken); + p->aBuf[nToken] = '\0'; + memcpy(&p->aBuf[nToken+1], &p->pText[iStart], iEnd-iStart); + ret = p->xToken(p->pCtx, tflags, p->aBuf, nReq, iStart, iEnd); + } + + return ret; +} + + +static int f5tOrigintextTokenize( + Fts5Tokenizer *pTokenizer, + void *pCtx, + int flags, /* Mask of FTS5_TOKENIZE_* flags */ + const char *pText, int nText, + int (*xToken)(void *, int, const char *, int, int, int) +){ + OriginTextTokenizer *p = (OriginTextTokenizer*)pTokenizer; + OriginTextCb cb; + int ret; + + memset(&cb, 0, sizeof(cb)); + cb.pCtx = pCtx; + cb.pText = pText; + cb.nText = nText; + cb.xToken = xToken; + + ret = p->tokapi.xTokenize(p->pTok,(void*)&cb,flags,pText,nText,xOriginToken); + sqlite3_free(cb.aBuf); + return ret; +} + +/* +** sqlite3_fts5_register_origintext DB +** +** Description... +*/ +static int SQLITE_TCLAPI f5tRegisterOriginText( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + sqlite3 *db = 0; + fts5_api *pApi = 0; + int rc; + fts5_tokenizer tok = {0, 0, 0}; + OriginTextCtx *pCtx = 0; + + if( objc!=2 ){ + Tcl_WrongNumArgs(interp, 1, objv, "DB"); + return TCL_ERROR; + } + if( f5tDbAndApi(interp, objv[1], &db, &pApi) ) return TCL_ERROR; + + pCtx = (OriginTextCtx*)ckalloc(sizeof(OriginTextCtx)); + pCtx->db = db; + pCtx->pApi = pApi; + + tok.xCreate = f5tOrigintextCreate; + tok.xDelete = f5tOrigintextDelete; + tok.xTokenize = f5tOrigintextTokenize; + rc = pApi->xCreateTokenizer( + pApi, "origintext", (void*)pCtx, &tok, f5tOrigintextTokenizerDelete + ); + + Tcl_ResetResult(interp); + if( rc!=SQLITE_OK ){ + Tcl_AppendResult(interp, "error: ", sqlite3_errmsg(db), (void*)0); + return TCL_ERROR; + } + return TCL_OK; +} + +/* +** This function is used to DROP an fts5 table. It works even if the data +** structures fts5 stores within the database are corrupt, which sometimes +** prevents a straight "DROP TABLE" command from succeeding. +** +** The first parameter is the database handle to use for the DROP TABLE +** operation. The second is the name of the database to drop the fts5 table +** from (i.e. "main", "temp" or the name of an attached database). The +** third parameter is the name of the fts5 table to drop. +** +** SQLITE_OK is returned if the table is successfully dropped. Or, if an +** error occurs, an SQLite error code. +*/ +static int sqlite3_fts5_drop_corrupt_table( + sqlite3 *db, /* Database handle */ + const char *zDb, /* Database name ("main", "temp" etc.) */ + const char *zTab /* Name of fts5 table to drop */ +){ + int rc = SQLITE_OK; + int bDef = 0; + + rc = sqlite3_db_config(db, SQLITE_DBCONFIG_DEFENSIVE, -1, &bDef); + if( rc==SQLITE_OK ){ + char *zScript = sqlite3_mprintf( + "DELETE FROM %Q.'%q_data';" + "DELETE FROM %Q.'%q_config';" + "INSERT INTO %Q.'%q_data' VALUES(10, X'0000000000');" + "INSERT INTO %Q.'%q_config' VALUES('version', 4);" + "DROP TABLE %Q.'%q';", + zDb, zTab, zDb, zTab, zDb, zTab, zDb, zTab, zDb, zTab + ); + + if( zScript==0 ){ + rc = SQLITE_NOMEM; + }else{ + if( bDef ) sqlite3_db_config(db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0); + rc = sqlite3_exec(db, zScript, 0, 0, 0); + if( bDef ) sqlite3_db_config(db, SQLITE_DBCONFIG_DEFENSIVE, 1, 0); + sqlite3_free(zScript); + } + } + + return rc; +} + +/* +** sqlite3_fts5_drop_corrupt_table DB DATABASE TABLE +** +** Description... +*/ +static int SQLITE_TCLAPI f5tDropCorruptTable( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + sqlite3 *db = 0; + const char *zDb = 0; + const char *zTab = 0; + int rc = SQLITE_OK; + + if( objc!=4 ){ + Tcl_WrongNumArgs(interp, 1, objv, "DB DATABASE TABLE"); + return TCL_ERROR; + } + if( f5tDbPointer(interp, objv[1], &db) ){ + return TCL_ERROR; + } + zDb = Tcl_GetString(objv[2]); + zTab = Tcl_GetString(objv[3]); + + rc = sqlite3_fts5_drop_corrupt_table(db, zDb, zTab); + if( rc!=SQLITE_OK ){ + Tcl_AppendResult(interp, "error: ", sqlite3_errmsg(db), (void*)0); + return TCL_ERROR; + } + + return TCL_OK; +} + +/* +** Free a buffer returned to SQLite by the str() function. +*/ +void f5tFree(void *p){ + char *x = (char *)p; + ckfree(&x[-8]); +} + +/* +** Implementation of str(). +*/ +void f5tStrFunc(sqlite3_context *pCtx, int nArg, sqlite3_value **apArg){ + const char *zText = 0; + assert( nArg==1 ); + + zText = (const char*)sqlite3_value_text(apArg[0]); + if( zText ){ + sqlite3_int64 nText = strlen(zText); + char *zCopy = (char*)ckalloc(nText+8); + if( zCopy==0 ){ + sqlite3_result_error_nomem(pCtx); + }else{ + zCopy += 8; + memcpy(zCopy, zText, nText); + sqlite3_result_text64(pCtx, zCopy, nText, f5tFree, SQLITE_UTF8); + } + } +} + +/* +** sqlite3_fts5_register_str DB +** +** Register the str() function with database handle DB. str() interprets +** its only argument as text and returns a copy of the value in a +** non-nul-terminated buffer. +*/ +static int SQLITE_TCLAPI f5tRegisterStr( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + sqlite3 *db = 0; + + if( objc!=2 ){ + Tcl_WrongNumArgs(interp, 1, objv, "DB"); + return TCL_ERROR; + } + if( f5tDbPointer(interp, objv[1], &db) ){ + return TCL_ERROR; + } + + sqlite3_create_function(db, "str", 1, SQLITE_UTF8, 0, f5tStrFunc, 0, 0); + + return TCL_OK; +} + /* ** Entry point. */ @@ -1120,12 +1707,16 @@ int Fts5tcl_Init(Tcl_Interp *interp){ } aCmd[] = { { "sqlite3_fts5_create_tokenizer", f5tCreateTokenizer, 1 }, { "sqlite3_fts5_token", f5tTokenizerReturn, 1 }, + { "sqlite3_fts5_locale", f5tTokenizerLocale, 1 }, { "sqlite3_fts5_tokenize", f5tTokenize, 0 }, { "sqlite3_fts5_create_function", f5tCreateFunction, 0 }, { "sqlite3_fts5_may_be_corrupt", f5tMayBeCorrupt, 0 }, { "sqlite3_fts5_token_hash", f5tTokenHash, 0 }, { "sqlite3_fts5_register_matchinfo", f5tRegisterMatchinfo, 0 }, - { "sqlite3_fts5_register_fts5tokenize", f5tRegisterTok, 0 } + { "sqlite3_fts5_register_fts5tokenize", f5tRegisterTok, 0 }, + { "sqlite3_fts5_register_origintext",f5tRegisterOriginText, 0 }, + { "sqlite3_fts5_drop_corrupt_table", f5tDropCorruptTable, 0 }, + { "sqlite3_fts5_register_str", f5tRegisterStr, 0 } }; int i; F5tTokenizerContext *pContext; diff --git a/ext/fts5/fts5_test_mi.c b/ext/fts5/fts5_test_mi.c index 28331773c0..e8648a4d22 100644 --- a/ext/fts5/fts5_test_mi.c +++ b/ext/fts5/fts5_test_mi.c @@ -14,7 +14,7 @@ ** versions of FTS5. It contains the implementation of an FTS5 auxiliary ** function very similar to the FTS4 function matchinfo(): ** -** https://www.sqlite.org/fts3.html#matchinfo +** https://sqlite.org/fts3.html#matchinfo ** ** Known differences are that: ** @@ -41,16 +41,17 @@ */ -#ifdef SQLITE_TEST #ifdef SQLITE_ENABLE_FTS5 #include "fts5.h" -#include #include #include typedef struct Fts5MatchinfoCtx Fts5MatchinfoCtx; + +#ifndef SQLITE_AMALGAMATION typedef unsigned int u32; +#endif struct Fts5MatchinfoCtx { int nCol; /* Number of cols in FTS5 table */ @@ -67,18 +68,19 @@ struct Fts5MatchinfoCtx { ** If an error occurs, return NULL and leave an error in the database ** handle (accessible using sqlite3_errcode()/errmsg()). */ -static fts5_api *fts5_api_from_db(sqlite3 *db){ - fts5_api *pRet = 0; +static int fts5_api_from_db(sqlite3 *db, fts5_api **ppApi){ sqlite3_stmt *pStmt = 0; + int rc; - if( SQLITE_OK==sqlite3_prepare(db, "SELECT fts5()", -1, &pStmt, 0) - && SQLITE_ROW==sqlite3_step(pStmt) - && sizeof(pRet)==sqlite3_column_bytes(pStmt, 0) - ){ - memcpy(&pRet, sqlite3_column_blob(pStmt, 0), sizeof(pRet)); + *ppApi = 0; + rc = sqlite3_prepare(db, "SELECT fts5(?1)", -1, &pStmt, 0); + if( rc==SQLITE_OK ){ + sqlite3_bind_pointer(pStmt, 1, (void*)ppApi, "fts5_api_ptr", 0); + (void)sqlite3_step(pStmt); + rc = sqlite3_finalize(pStmt); } - sqlite3_finalize(pStmt); - return pRet; + + return rc; } @@ -244,11 +246,7 @@ static int fts5MatchinfoLocalCb( iOff>=0; pApi->xPhraseNext(pFts, &iter, &iCol, &iOff) ){ - if( f=='b' ){ - aOut[iPhrase * ((p->nCol+31)/32) + iCol/32] |= ((u32)1 << iCol%32); - }else{ - aOut[nMul * (iCol + iPhrase * p->nCol)]++; - } + aOut[nMul * (iCol + iPhrase * p->nCol)]++; } } @@ -311,7 +309,7 @@ static Fts5MatchinfoCtx *fts5MatchinfoNew( int nPhrase; int i; int nInt; - int nByte; + sqlite3_int64 nByte; int rc; nCol = pApi->xColumnCount(pFts); @@ -332,7 +330,7 @@ static Fts5MatchinfoCtx *fts5MatchinfoNew( nByte = sizeof(Fts5MatchinfoCtx) /* The struct itself */ + sizeof(u32) * nInt /* The p->aRet[] array */ + (i+1); /* The p->zArg string */ - p = (Fts5MatchinfoCtx*)sqlite3_malloc(nByte); + p = (Fts5MatchinfoCtx*)sqlite3_malloc64(nByte); if( p==0 ){ sqlite3_result_error_nomem(pCtx); return 0; @@ -395,21 +393,18 @@ static void fts5MatchinfoFunc( } } -int sqlite3Fts5TestRegisterMatchinfo(sqlite3 *db){ - int rc; /* Return code */ - fts5_api *pApi; /* FTS5 API functions */ - - /* Extract the FTS5 API pointer from the database handle. The - ** fts5_api_from_db() function above is copied verbatim from the - ** FTS5 documentation. Refer there for details. */ - pApi = fts5_api_from_db(db); +/* +** Register "matchinfo" with global API object pApi. +*/ +int sqlite3Fts5TestRegisterMatchinfoAPI(fts5_api *pApi){ + int rc; - /* If fts5_api_from_db() returns NULL, then either FTS5 is not registered + /* If fts5_api_from_db() returned NULL, then either FTS5 is not registered ** with this database handle, or an error (OOM perhaps?) has occurred. ** ** Also check that the fts5_api object is version 2 or newer. */ - if( pApi==0 || pApi->iVersion<1 ){ + if( pApi==0 || pApi->iVersion<2 ){ return SQLITE_ERROR; } @@ -419,6 +414,20 @@ int sqlite3Fts5TestRegisterMatchinfo(sqlite3 *db){ return rc; } -#endif /* SQLITE_ENABLE_FTS5 */ -#endif /* SQLITE_TEST */ +/* +** Register "matchinfo" with database handle db. +*/ +int sqlite3Fts5TestRegisterMatchinfo(sqlite3 *db){ + int rc; /* Return code */ + fts5_api *pApi; /* FTS5 API functions */ + /* Extract the FTS5 API pointer from the database handle. The + ** fts5_api_from_db() function above is copied verbatim from the + ** FTS5 documentation. Refer there for details. */ + rc = fts5_api_from_db(db, &pApi); + if( rc!=SQLITE_OK ) return rc; + + return sqlite3Fts5TestRegisterMatchinfoAPI(pApi); +} + +#endif /* SQLITE_ENABLE_FTS5 */ diff --git a/ext/fts5/fts5_test_tok.c b/ext/fts5/fts5_test_tok.c index 10af126c10..994d304dc6 100644 --- a/ext/fts5/fts5_test_tok.c +++ b/ext/fts5/fts5_test_tok.c @@ -40,7 +40,7 @@ */ #if defined(SQLITE_TEST) && defined(SQLITE_ENABLE_FTS5) -#include +#include "fts5.h" #include #include @@ -137,7 +137,7 @@ static int fts5tokDequoteArray( nByte += (int)(strlen(argv[i]) + 1); } - *pazDequote = azDequote = sqlite3_malloc(sizeof(char *)*argc + nByte); + *pazDequote = azDequote = sqlite3_malloc64(sizeof(char *)*argc + nByte); if( azDequote==0 ){ rc = SQLITE_NOMEM; }else{ @@ -182,7 +182,7 @@ static int fts5tokConnectMethod( Fts5tokTable *pTab = 0; int rc; char **azDequote = 0; - int nDequote; + int nDequote = 0; rc = sqlite3_declare_vtab(db, "CREATE TABLE x(input HIDDEN, token, start, end, position)" @@ -211,7 +211,7 @@ static int fts5tokConnectMethod( rc = pApi->xFindTokenizer(pApi, zModule, &pTokCtx, &pTab->tok); if( rc==SQLITE_OK ){ - const char **azArg = (const char **)&azDequote[1]; + const char **azArg = (nDequote>1 ? (const char **)&azDequote[1] : 0); int nArg = nDequote>0 ? nDequote-1 : 0; rc = pTab->tok.xCreate(pTokCtx, azArg, nArg, &pTab->pTok); } @@ -335,7 +335,7 @@ static int fts5tokCb( if( (pCsr->nRow & (pCsr->nRow-1))==0 ){ int nNew = pCsr->nRow ? pCsr->nRow*2 : 32; Fts5tokRow *aNew; - aNew = (Fts5tokRow*)sqlite3_realloc(pCsr->aRow, nNew*sizeof(Fts5tokRow)); + aNew = (Fts5tokRow*)sqlite3_realloc64(pCsr->aRow, nNew*sizeof(Fts5tokRow)); if( aNew==0 ) return SQLITE_NOMEM; memset(&aNew[pCsr->nRow], 0, sizeof(Fts5tokRow)*(nNew-pCsr->nRow)); pCsr->aRow = aNew; @@ -378,7 +378,7 @@ static int fts5tokFilterMethod( if( pCsr->zInput==0 ){ rc = SQLITE_NOMEM; }else{ - memcpy(pCsr->zInput, zByte, nByte); + if( nByte>0 ) memcpy(pCsr->zInput, zByte, nByte); pCsr->zInput[nByte] = 0; rc = pTab->tok.xTokenize( pTab->pTok, (void*)pCsr, 0, zByte, nByte, fts5tokCb @@ -471,7 +471,9 @@ int sqlite3Fts5TestRegisterTok(sqlite3 *db, fts5_api *pApi){ 0, /* xRename */ 0, /* xSavepoint */ 0, /* xRelease */ - 0 /* xRollbackTo */ + 0, /* xRollbackTo */ + 0, /* xShadowName */ + 0 /* xIntegrity */ }; int rc; /* Return code */ diff --git a/ext/fts5/fts5_tokenize.c b/ext/fts5/fts5_tokenize.c index b72a0c24ab..b8a1136465 100644 --- a/ext/fts5/fts5_tokenize.c +++ b/ext/fts5/fts5_tokenize.c @@ -152,7 +152,7 @@ static int fts5AsciiTokenize( nByte = ie-is; if( nByte>nFold ){ if( pFold!=aFold ) sqlite3_free(pFold); - pFold = sqlite3_malloc(nByte*2); + pFold = sqlite3_malloc64((sqlite3_int64)nByte*2); if( pFold==0 ){ rc = SQLITE_NOMEM; break; @@ -198,7 +198,7 @@ static const unsigned char sqlite3Utf8Trans1[] = { c = *(zIn++); \ if( c>=0xc0 ){ \ c = sqlite3Utf8Trans1[c-0xc0]; \ - while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){ \ + while( zIn=0xc0 ){ \ + while( (((unsigned char)*zIn) & 0xc0)==0x80 ){ zIn++; } \ + } \ +} + typedef struct Unicode61Tokenizer Unicode61Tokenizer; struct Unicode61Tokenizer { unsigned char aTokenChar[128]; /* ASCII range token characters */ char *aFold; /* Buffer to fold text into */ int nFold; /* Size of aFold[] in bytes */ - int bRemoveDiacritic; /* True if remove_diacritics=1 is set */ + int eRemoveDiacritic; /* True if remove_diacritics=1 is set */ int nException; int *aiException; + + unsigned char aCategory[32]; /* True for token char categories */ }; +/* Values for eRemoveDiacritic (must match internals of fts5_unicode2.c) */ +#define FTS5_REMOVE_DIACRITICS_NONE 0 +#define FTS5_REMOVE_DIACRITICS_SIMPLE 1 +#define FTS5_REMOVE_DIACRITICS_COMPLEX 2 + static int fts5UnicodeAddExceptions( Unicode61Tokenizer *p, /* Tokenizer object */ const char *z, /* Characters to treat as exceptions */ @@ -249,25 +262,26 @@ static int fts5UnicodeAddExceptions( int *aNew; if( n>0 ){ - aNew = (int*)sqlite3_realloc(p->aiException, (n+p->nException)*sizeof(int)); + aNew = (int*)sqlite3_realloc64(p->aiException, + (n+p->nException)*sizeof(int)); if( aNew ){ int nNew = p->nException; const unsigned char *zCsr = (const unsigned char*)z; const unsigned char *zTerm = (const unsigned char*)&z[n]; while( zCsraTokenChar[iCode] = (unsigned char)bTokenChars; }else{ - bToken = sqlite3Fts5UnicodeIsalnum(iCode); + bToken = p->aCategory[sqlite3Fts5UnicodeCategory(iCode)]; assert( (bToken==0 || bToken==1) ); assert( (bTokenChars==0 || bTokenChars==1) ); if( bToken!=bTokenChars && sqlite3Fts5UnicodeIsdiacritic(iCode)==0 ){ int i; for(i=0; iiCode ) break; + if( (u32)aNew[i]>iCode ) break; } memmove(&aNew[i+1], &aNew[i], (nNew-i)*sizeof(int)); aNew[i] = iCode; @@ -322,6 +336,21 @@ static void fts5UnicodeDelete(Fts5Tokenizer *pTok){ return; } +static int unicodeSetCategories(Unicode61Tokenizer *p, const char *zCat){ + const char *z = zCat; + + while( *z ){ + while( *z==' ' || *z=='\t' ) z++; + if( *z && sqlite3Fts5UnicodeCatParse(z, p->aCategory) ){ + return SQLITE_ERROR; + } + while( *z!=' ' && *z!='\t' && *z!='\0' ) z++; + } + + sqlite3Fts5UnicodeAscii(p->aCategory, p->aTokenChar); + return SQLITE_OK; +} + /* ** Create a "unicode61" tokenizer. */ @@ -340,28 +369,48 @@ static int fts5UnicodeCreate( }else{ p = (Unicode61Tokenizer*)sqlite3_malloc(sizeof(Unicode61Tokenizer)); if( p ){ + const char *zCat = "L* N* Co"; int i; memset(p, 0, sizeof(Unicode61Tokenizer)); - memcpy(p->aTokenChar, aAsciiTokenChar, sizeof(aAsciiTokenChar)); - p->bRemoveDiacritic = 1; + + p->eRemoveDiacritic = FTS5_REMOVE_DIACRITICS_SIMPLE; p->nFold = 64; - p->aFold = sqlite3_malloc(p->nFold * sizeof(char)); + p->aFold = sqlite3_malloc64(p->nFold * sizeof(char)); if( p->aFold==0 ){ rc = SQLITE_NOMEM; } + + /* Search for a "categories" argument */ + for(i=0; rc==SQLITE_OK && ieRemoveDiacritic = (zArg[0] - '0'); + assert( p->eRemoveDiacritic==FTS5_REMOVE_DIACRITICS_NONE + || p->eRemoveDiacritic==FTS5_REMOVE_DIACRITICS_SIMPLE + || p->eRemoveDiacritic==FTS5_REMOVE_DIACRITICS_COMPLEX + ); } - p->bRemoveDiacritic = (zArg[0]=='1'); }else if( 0==sqlite3_stricmp(azArg[i], "tokenchars") ){ rc = fts5UnicodeAddExceptions(p, zArg, 1); }else if( 0==sqlite3_stricmp(azArg[i], "separators") ){ rc = fts5UnicodeAddExceptions(p, zArg, 0); + }else + if( 0==sqlite3_stricmp(azArg[i], "categories") ){ + /* no-op */ }else{ rc = SQLITE_ERROR; } @@ -384,8 +433,10 @@ static int fts5UnicodeCreate( ** character (not a separator). */ static int fts5UnicodeIsAlnum(Unicode61Tokenizer *p, int iCode){ - assert( (sqlite3Fts5UnicodeIsalnum(iCode) & 0xFFFFFFFE)==0 ); - return sqlite3Fts5UnicodeIsalnum(iCode) ^ fts5UnicodeIsException(p, iCode); + return ( + p->aCategory[sqlite3Fts5UnicodeCategory((u32)iCode)] + ^ fts5UnicodeIsException(p, iCode) + ); } static int fts5UnicodeTokenize( @@ -412,7 +463,7 @@ static int fts5UnicodeTokenize( /* Each iteration of this loop gobbles up a contiguous run of separators, ** then the next token. */ while( rc==SQLITE_OK ){ - int iCode; /* non-ASCII codepoint read from input */ + u32 iCode; /* non-ASCII codepoint read from input */ char *zOut = aFold; int is; int ie; @@ -444,7 +495,7 @@ static int fts5UnicodeTokenize( /* Grow the output buffer so that there is sufficient space to fit the ** largest possible utf-8 character. */ if( zOut>pEnd ){ - aFold = sqlite3_malloc(nFold*2); + aFold = sqlite3_malloc64((sqlite3_int64)nFold*2); if( aFold==0 ){ rc = SQLITE_NOMEM; goto tokenize_done; @@ -463,7 +514,7 @@ static int fts5UnicodeTokenize( READ_UTF8(zCsr, zTerm, iCode); if( fts5UnicodeIsAlnum(p,iCode)||sqlite3Fts5UnicodeIsdiacritic(iCode) ){ non_ascii_tokenchar: - iCode = sqlite3Fts5UnicodeFold(iCode, p->bRemoveDiacritic); + iCode = sqlite3Fts5UnicodeFold(iCode, p->eRemoveDiacritic); if( iCode ) WRITE_UTF8(zOut, iCode); }else{ break; @@ -502,7 +553,7 @@ static int fts5UnicodeTokenize( typedef struct PorterTokenizer PorterTokenizer; struct PorterTokenizer { - fts5_tokenizer tokenizer; /* Parent tokenizer module */ + fts5_tokenizer_v2 tokenizer_v2; /* Parent tokenizer module */ Fts5Tokenizer *pTokenizer; /* Parent tokenizer instance */ char aBuf[FTS5_PORTER_MAX_TOKEN + 64]; }; @@ -514,7 +565,7 @@ static void fts5PorterDelete(Fts5Tokenizer *pTok){ if( pTok ){ PorterTokenizer *p = (PorterTokenizer*)pTok; if( p->pTokenizer ){ - p->tokenizer.xDelete(p->pTokenizer); + p->tokenizer_v2.xDelete(p->pTokenizer); } sqlite3_free(p); } @@ -533,6 +584,7 @@ static int fts5PorterCreate( PorterTokenizer *pRet; void *pUserdata = 0; const char *zBase = "unicode61"; + fts5_tokenizer_v2 *pV2 = 0; if( nArg>0 ){ zBase = azArg[0]; @@ -541,14 +593,15 @@ static int fts5PorterCreate( pRet = (PorterTokenizer*)sqlite3_malloc(sizeof(PorterTokenizer)); if( pRet ){ memset(pRet, 0, sizeof(PorterTokenizer)); - rc = pApi->xFindTokenizer(pApi, zBase, &pUserdata, &pRet->tokenizer); + rc = pApi->xFindTokenizer_v2(pApi, zBase, &pUserdata, &pV2); }else{ rc = SQLITE_NOMEM; } if( rc==SQLITE_OK ){ int nArg2 = (nArg>0 ? nArg-1 : 0); - const char **azArg2 = (nArg2 ? &azArg[1] : 0); - rc = pRet->tokenizer.xCreate(pUserdata, azArg2, nArg2, &pRet->pTokenizer); + const char **az2 = (nArg2 ? &azArg[1] : 0); + memcpy(&pRet->tokenizer_v2, pV2, sizeof(fts5_tokenizer_v2)); + rc = pRet->tokenizer_v2.xCreate(pUserdata, az2, nArg2, &pRet->pTokenizer); } if( rc!=SQLITE_OK ){ @@ -1199,6 +1252,7 @@ static int fts5PorterTokenize( void *pCtx, int flags, const char *pText, int nText, + const char *pLoc, int nLoc, int (*xToken)(void*, int, const char*, int nToken, int iStart, int iEnd) ){ PorterTokenizer *p = (PorterTokenizer*)pTokenizer; @@ -1206,11 +1260,194 @@ static int fts5PorterTokenize( sCtx.xToken = xToken; sCtx.pCtx = pCtx; sCtx.aBuf = p->aBuf; - return p->tokenizer.xTokenize( - p->pTokenizer, (void*)&sCtx, flags, pText, nText, fts5PorterCb + return p->tokenizer_v2.xTokenize( + p->pTokenizer, (void*)&sCtx, flags, pText, nText, pLoc, nLoc, fts5PorterCb ); } +/************************************************************************** +** Start of trigram implementation. +*/ +typedef struct TrigramTokenizer TrigramTokenizer; +struct TrigramTokenizer { + int bFold; /* True to fold to lower-case */ + int iFoldParam; /* Parameter to pass to Fts5UnicodeFold() */ +}; + +/* +** Free a trigram tokenizer. +*/ +static void fts5TriDelete(Fts5Tokenizer *p){ + sqlite3_free(p); +} + +/* +** Allocate a trigram tokenizer. +*/ +static int fts5TriCreate( + void *pUnused, + const char **azArg, + int nArg, + Fts5Tokenizer **ppOut +){ + int rc = SQLITE_OK; + TrigramTokenizer *pNew = 0; + UNUSED_PARAM(pUnused); + if( nArg%2 ){ + rc = SQLITE_ERROR; + }else{ + int i; + pNew = (TrigramTokenizer*)sqlite3_malloc(sizeof(*pNew)); + if( pNew==0 ){ + rc = SQLITE_NOMEM; + }else{ + pNew->bFold = 1; + pNew->iFoldParam = 0; + + for(i=0; rc==SQLITE_OK && ibFold = (zArg[0]=='0'); + } + }else if( 0==sqlite3_stricmp(azArg[i], "remove_diacritics") ){ + if( (zArg[0]!='0' && zArg[0]!='1' && zArg[0]!='2') || zArg[1] ){ + rc = SQLITE_ERROR; + }else{ + pNew->iFoldParam = (zArg[0]!='0') ? 2 : 0; + } + }else{ + rc = SQLITE_ERROR; + } + } + + if( pNew->iFoldParam!=0 && pNew->bFold==0 ){ + rc = SQLITE_ERROR; + } + + if( rc!=SQLITE_OK ){ + fts5TriDelete((Fts5Tokenizer*)pNew); + pNew = 0; + } + } + } + *ppOut = (Fts5Tokenizer*)pNew; + return rc; +} + +/* +** Trigram tokenizer tokenize routine. +*/ +static int fts5TriTokenize( + Fts5Tokenizer *pTok, + void *pCtx, + int unusedFlags, + const char *pText, int nText, + int (*xToken)(void*, int, const char*, int, int, int) +){ + TrigramTokenizer *p = (TrigramTokenizer*)pTok; + int rc = SQLITE_OK; + char aBuf[32]; + char *zOut = aBuf; + int ii; + const unsigned char *zIn = (const unsigned char*)pText; + const unsigned char *zEof = (zIn ? &zIn[nText] : 0); + u32 iCode = 0; + int aStart[3]; /* Input offset of each character in aBuf[] */ + + UNUSED_PARAM(unusedFlags); + + /* Populate aBuf[] with the characters for the first trigram. */ + for(ii=0; ii<3; ii++){ + do { + aStart[ii] = zIn - (const unsigned char*)pText; + if( zIn>=zEof ) return SQLITE_OK; + READ_UTF8(zIn, zEof, iCode); + if( p->bFold ) iCode = sqlite3Fts5UnicodeFold(iCode, p->iFoldParam); + }while( iCode==0 ); + WRITE_UTF8(zOut, iCode); + } + + /* At the start of each iteration of this loop: + ** + ** aBuf: Contains 3 characters. The 3 characters of the next trigram. + ** zOut: Points to the byte following the last character in aBuf. + ** aStart[3]: Contains the byte offset in the input text corresponding + ** to the start of each of the three characters in the buffer. + */ + assert( zIn<=zEof ); + while( 1 ){ + int iNext; /* Start of character following current tri */ + const char *z1; + + /* Read characters from the input up until the first non-diacritic */ + do { + iNext = zIn - (const unsigned char*)pText; + if( zIn>=zEof ){ + iCode = 0; + break; + } + READ_UTF8(zIn, zEof, iCode); + if( p->bFold ) iCode = sqlite3Fts5UnicodeFold(iCode, p->iFoldParam); + }while( iCode==0 ); + + /* Pass the current trigram back to fts5 */ + rc = xToken(pCtx, 0, aBuf, zOut-aBuf, aStart[0], iNext); + if( iCode==0 || rc!=SQLITE_OK ) break; + + /* Remove the first character from buffer aBuf[]. Append the character + ** with codepoint iCode. */ + z1 = aBuf; + FTS5_SKIP_UTF8(z1); + memmove(aBuf, z1, zOut - z1); + zOut -= (z1 - aBuf); + WRITE_UTF8(zOut, iCode); + + /* Update the aStart[] array */ + aStart[0] = aStart[1]; + aStart[1] = aStart[2]; + aStart[2] = iNext; + } + + return rc; +} + +/* +** Argument xCreate is a pointer to a constructor function for a tokenizer. +** pTok is a tokenizer previously created using the same method. This function +** returns one of FTS5_PATTERN_NONE, FTS5_PATTERN_LIKE or FTS5_PATTERN_GLOB +** indicating the style of pattern matching that the tokenizer can support. +** In practice, this is: +** +** "trigram" tokenizer, case_sensitive=1 - FTS5_PATTERN_GLOB +** "trigram" tokenizer, case_sensitive=0 (the default) - FTS5_PATTERN_LIKE +** all other tokenizers - FTS5_PATTERN_NONE +*/ +int sqlite3Fts5TokenizerPattern( + int (*xCreate)(void*, const char**, int, Fts5Tokenizer**), + Fts5Tokenizer *pTok +){ + if( xCreate==fts5TriCreate ){ + TrigramTokenizer *p = (TrigramTokenizer*)pTok; + if( p->iFoldParam==0 ){ + return p->bFold ? FTS5_PATTERN_LIKE : FTS5_PATTERN_GLOB; + } + } + return FTS5_PATTERN_NONE; +} + +/* +** Return true if the tokenizer described by p->azArg[] is the trigram +** tokenizer. This tokenizer needs to be loaded before xBestIndex is +** called for the first time in order to correctly handle LIKE/GLOB. +*/ +int sqlite3Fts5TokenizerPreload(Fts5TokenizerConfig *p){ + return (p->nArg>=1 && 0==sqlite3_stricmp(p->azArg[0], "trigram")); +} + + /* ** Register all built-in tokenizers with FTS5. */ @@ -1221,7 +1458,7 @@ int sqlite3Fts5TokenizerInit(fts5_api *pApi){ } aBuiltin[] = { { "unicode61", {fts5UnicodeCreate, fts5UnicodeDelete, fts5UnicodeTokenize}}, { "ascii", {fts5AsciiCreate, fts5AsciiDelete, fts5AsciiTokenize }}, - { "porter", {fts5PorterCreate, fts5PorterDelete, fts5PorterTokenize }}, + { "trigram", {fts5TriCreate, fts5TriDelete, fts5TriTokenize}}, }; int rc = SQLITE_OK; /* Return code */ @@ -1235,8 +1472,19 @@ int sqlite3Fts5TokenizerInit(fts5_api *pApi){ 0 ); } - + if( rc==SQLITE_OK ){ + fts5_tokenizer_v2 sPorter = { + 2, + fts5PorterCreate, + fts5PorterDelete, + fts5PorterTokenize + }; + rc = pApi->xCreateTokenizer_v2(pApi, + "porter", + (void*)pApi, + &sPorter, + 0 + ); + } return rc; } - - diff --git a/ext/fts5/fts5_unicode2.c b/ext/fts5/fts5_unicode2.c index 1ef56f6156..2133d5d5b8 100644 --- a/ext/fts5/fts5_unicode2.c +++ b/ext/fts5/fts5_unicode2.c @@ -1,5 +1,5 @@ /* -** 2012 May 25 +** 2012-05-25 ** ** The author disclaims copyright to this source code. In place of ** a legal notice, here is a blessing: @@ -18,135 +18,6 @@ #include -/* -** Return true if the argument corresponds to a unicode codepoint -** classified as either a letter or a number. Otherwise false. -** -** The results are undefined if the value passed to this function -** is less than zero. -*/ -int sqlite3Fts5UnicodeIsalnum(int c){ - /* Each unsigned integer in the following array corresponds to a contiguous - ** range of unicode codepoints that are not either letters or numbers (i.e. - ** codepoints for which this function should return 0). - ** - ** The most significant 22 bits in each 32-bit value contain the first - ** codepoint in the range. The least significant 10 bits are used to store - ** the size of the range (always at least 1). In other words, the value - ** ((C<<22) + N) represents a range of N codepoints starting with codepoint - ** C. It is not possible to represent a range larger than 1023 codepoints - ** using this format. - */ - static const unsigned int aEntry[] = { - 0x00000030, 0x0000E807, 0x00016C06, 0x0001EC2F, 0x0002AC07, - 0x0002D001, 0x0002D803, 0x0002EC01, 0x0002FC01, 0x00035C01, - 0x0003DC01, 0x000B0804, 0x000B480E, 0x000B9407, 0x000BB401, - 0x000BBC81, 0x000DD401, 0x000DF801, 0x000E1002, 0x000E1C01, - 0x000FD801, 0x00120808, 0x00156806, 0x00162402, 0x00163C01, - 0x00164437, 0x0017CC02, 0x00180005, 0x00181816, 0x00187802, - 0x00192C15, 0x0019A804, 0x0019C001, 0x001B5001, 0x001B580F, - 0x001B9C07, 0x001BF402, 0x001C000E, 0x001C3C01, 0x001C4401, - 0x001CC01B, 0x001E980B, 0x001FAC09, 0x001FD804, 0x00205804, - 0x00206C09, 0x00209403, 0x0020A405, 0x0020C00F, 0x00216403, - 0x00217801, 0x0023901B, 0x00240004, 0x0024E803, 0x0024F812, - 0x00254407, 0x00258804, 0x0025C001, 0x00260403, 0x0026F001, - 0x0026F807, 0x00271C02, 0x00272C03, 0x00275C01, 0x00278802, - 0x0027C802, 0x0027E802, 0x00280403, 0x0028F001, 0x0028F805, - 0x00291C02, 0x00292C03, 0x00294401, 0x0029C002, 0x0029D401, - 0x002A0403, 0x002AF001, 0x002AF808, 0x002B1C03, 0x002B2C03, - 0x002B8802, 0x002BC002, 0x002C0403, 0x002CF001, 0x002CF807, - 0x002D1C02, 0x002D2C03, 0x002D5802, 0x002D8802, 0x002DC001, - 0x002E0801, 0x002EF805, 0x002F1803, 0x002F2804, 0x002F5C01, - 0x002FCC08, 0x00300403, 0x0030F807, 0x00311803, 0x00312804, - 0x00315402, 0x00318802, 0x0031FC01, 0x00320802, 0x0032F001, - 0x0032F807, 0x00331803, 0x00332804, 0x00335402, 0x00338802, - 0x00340802, 0x0034F807, 0x00351803, 0x00352804, 0x00355C01, - 0x00358802, 0x0035E401, 0x00360802, 0x00372801, 0x00373C06, - 0x00375801, 0x00376008, 0x0037C803, 0x0038C401, 0x0038D007, - 0x0038FC01, 0x00391C09, 0x00396802, 0x003AC401, 0x003AD006, - 0x003AEC02, 0x003B2006, 0x003C041F, 0x003CD00C, 0x003DC417, - 0x003E340B, 0x003E6424, 0x003EF80F, 0x003F380D, 0x0040AC14, - 0x00412806, 0x00415804, 0x00417803, 0x00418803, 0x00419C07, - 0x0041C404, 0x0042080C, 0x00423C01, 0x00426806, 0x0043EC01, - 0x004D740C, 0x004E400A, 0x00500001, 0x0059B402, 0x005A0001, - 0x005A6C02, 0x005BAC03, 0x005C4803, 0x005CC805, 0x005D4802, - 0x005DC802, 0x005ED023, 0x005F6004, 0x005F7401, 0x0060000F, - 0x0062A401, 0x0064800C, 0x0064C00C, 0x00650001, 0x00651002, - 0x0066C011, 0x00672002, 0x00677822, 0x00685C05, 0x00687802, - 0x0069540A, 0x0069801D, 0x0069FC01, 0x006A8007, 0x006AA006, - 0x006C0005, 0x006CD011, 0x006D6823, 0x006E0003, 0x006E840D, - 0x006F980E, 0x006FF004, 0x00709014, 0x0070EC05, 0x0071F802, - 0x00730008, 0x00734019, 0x0073B401, 0x0073C803, 0x00770027, - 0x0077F004, 0x007EF401, 0x007EFC03, 0x007F3403, 0x007F7403, - 0x007FB403, 0x007FF402, 0x00800065, 0x0081A806, 0x0081E805, - 0x00822805, 0x0082801A, 0x00834021, 0x00840002, 0x00840C04, - 0x00842002, 0x00845001, 0x00845803, 0x00847806, 0x00849401, - 0x00849C01, 0x0084A401, 0x0084B801, 0x0084E802, 0x00850005, - 0x00852804, 0x00853C01, 0x00864264, 0x00900027, 0x0091000B, - 0x0092704E, 0x00940200, 0x009C0475, 0x009E53B9, 0x00AD400A, - 0x00B39406, 0x00B3BC03, 0x00B3E404, 0x00B3F802, 0x00B5C001, - 0x00B5FC01, 0x00B7804F, 0x00B8C00C, 0x00BA001A, 0x00BA6C59, - 0x00BC00D6, 0x00BFC00C, 0x00C00005, 0x00C02019, 0x00C0A807, - 0x00C0D802, 0x00C0F403, 0x00C26404, 0x00C28001, 0x00C3EC01, - 0x00C64002, 0x00C6580A, 0x00C70024, 0x00C8001F, 0x00C8A81E, - 0x00C94001, 0x00C98020, 0x00CA2827, 0x00CB003F, 0x00CC0100, - 0x01370040, 0x02924037, 0x0293F802, 0x02983403, 0x0299BC10, - 0x029A7C01, 0x029BC008, 0x029C0017, 0x029C8002, 0x029E2402, - 0x02A00801, 0x02A01801, 0x02A02C01, 0x02A08C09, 0x02A0D804, - 0x02A1D004, 0x02A20002, 0x02A2D011, 0x02A33802, 0x02A38012, - 0x02A3E003, 0x02A4980A, 0x02A51C0D, 0x02A57C01, 0x02A60004, - 0x02A6CC1B, 0x02A77802, 0x02A8A40E, 0x02A90C01, 0x02A93002, - 0x02A97004, 0x02A9DC03, 0x02A9EC01, 0x02AAC001, 0x02AAC803, - 0x02AADC02, 0x02AAF802, 0x02AB0401, 0x02AB7802, 0x02ABAC07, - 0x02ABD402, 0x02AF8C0B, 0x03600001, 0x036DFC02, 0x036FFC02, - 0x037FFC01, 0x03EC7801, 0x03ECA401, 0x03EEC810, 0x03F4F802, - 0x03F7F002, 0x03F8001A, 0x03F88007, 0x03F8C023, 0x03F95013, - 0x03F9A004, 0x03FBFC01, 0x03FC040F, 0x03FC6807, 0x03FCEC06, - 0x03FD6C0B, 0x03FF8007, 0x03FFA007, 0x03FFE405, 0x04040003, - 0x0404DC09, 0x0405E411, 0x0406400C, 0x0407402E, 0x040E7C01, - 0x040F4001, 0x04215C01, 0x04247C01, 0x0424FC01, 0x04280403, - 0x04281402, 0x04283004, 0x0428E003, 0x0428FC01, 0x04294009, - 0x0429FC01, 0x042CE407, 0x04400003, 0x0440E016, 0x04420003, - 0x0442C012, 0x04440003, 0x04449C0E, 0x04450004, 0x04460003, - 0x0446CC0E, 0x04471404, 0x045AAC0D, 0x0491C004, 0x05BD442E, - 0x05BE3C04, 0x074000F6, 0x07440027, 0x0744A4B5, 0x07480046, - 0x074C0057, 0x075B0401, 0x075B6C01, 0x075BEC01, 0x075C5401, - 0x075CD401, 0x075D3C01, 0x075DBC01, 0x075E2401, 0x075EA401, - 0x075F0C01, 0x07BBC002, 0x07C0002C, 0x07C0C064, 0x07C2800F, - 0x07C2C40E, 0x07C3040F, 0x07C3440F, 0x07C4401F, 0x07C4C03C, - 0x07C5C02B, 0x07C7981D, 0x07C8402B, 0x07C90009, 0x07C94002, - 0x07CC0021, 0x07CCC006, 0x07CCDC46, 0x07CE0014, 0x07CE8025, - 0x07CF1805, 0x07CF8011, 0x07D0003F, 0x07D10001, 0x07D108B6, - 0x07D3E404, 0x07D4003E, 0x07D50004, 0x07D54018, 0x07D7EC46, - 0x07D9140B, 0x07DA0046, 0x07DC0074, 0x38000401, 0x38008060, - 0x380400F0, - }; - static const unsigned int aAscii[4] = { - 0xFFFFFFFF, 0xFC00FFFF, 0xF8000001, 0xF8000001, - }; - - if( (unsigned int)c<128 ){ - return ( (aAscii[c >> 5] & (1 << (c & 0x001F)))==0 ); - }else if( (unsigned int)c<(1<<22) ){ - unsigned int key = (((unsigned int)c)<<10) | 0x000003FF; - int iRes = 0; - int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1; - int iLo = 0; - while( iHi>=iLo ){ - int iTest = (iHi + iLo) / 2; - if( key >= aEntry[iTest] ){ - iRes = iTest; - iLo = iTest+1; - }else{ - iHi = iTest-1; - } - } - assert( aEntry[0]=aEntry[iRes] ); - return (((unsigned int)c) >= ((aEntry[iRes]>>10) + (aEntry[iRes]&0x3FF))); - } - return 1; -} /* @@ -157,32 +28,48 @@ int sqlite3Fts5UnicodeIsalnum(int c){ ** E"). The resuls of passing a codepoint that corresponds to an ** uppercase letter are undefined. */ -static int fts5_remove_diacritic(int c){ +static int fts5_remove_diacritic(int c, int bComplex){ unsigned short aDia[] = { 0, 1797, 1848, 1859, 1891, 1928, 1940, 1995, 2024, 2040, 2060, 2110, 2168, 2206, 2264, 2286, 2344, 2383, 2472, 2488, 2516, 2596, 2668, 2732, 2782, 2842, 2894, 2954, 2984, 3000, 3028, 3336, - 3456, 3696, 3712, 3728, 3744, 3896, 3912, 3928, - 3968, 4008, 4040, 4106, 4138, 4170, 4202, 4234, - 4266, 4296, 4312, 4344, 4408, 4424, 4472, 4504, - 6148, 6198, 6264, 6280, 6360, 6429, 6505, 6529, - 61448, 61468, 61534, 61592, 61642, 61688, 61704, 61726, - 61784, 61800, 61836, 61880, 61914, 61948, 61998, 62122, - 62154, 62200, 62218, 62302, 62364, 62442, 62478, 62536, - 62554, 62584, 62604, 62640, 62648, 62656, 62664, 62730, - 62924, 63050, 63082, 63274, 63390, + 3456, 3696, 3712, 3728, 3744, 3766, 3832, 3896, + 3912, 3928, 3944, 3968, 4008, 4040, 4056, 4106, + 4138, 4170, 4202, 4234, 4266, 4296, 4312, 4344, + 4408, 4424, 4442, 4472, 4488, 4504, 6148, 6198, + 6264, 6280, 6360, 6429, 6505, 6529, 61448, 61468, + 61512, 61534, 61592, 61610, 61642, 61672, 61688, 61704, + 61726, 61784, 61800, 61816, 61836, 61880, 61896, 61914, + 61948, 61998, 62062, 62122, 62154, 62184, 62200, 62218, + 62252, 62302, 62364, 62410, 62442, 62478, 62536, 62554, + 62584, 62604, 62640, 62648, 62656, 62664, 62730, 62766, + 62830, 62890, 62924, 62974, 63032, 63050, 63082, 63118, + 63182, 63242, 63274, 63310, 63368, 63390, }; - char aChar[] = { - '\0', 'a', 'c', 'e', 'i', 'n', 'o', 'u', 'y', 'y', 'a', 'c', - 'd', 'e', 'e', 'g', 'h', 'i', 'j', 'k', 'l', 'n', 'o', 'r', - 's', 't', 'u', 'u', 'w', 'y', 'z', 'o', 'u', 'a', 'i', 'o', - 'u', 'g', 'k', 'o', 'j', 'g', 'n', 'a', 'e', 'i', 'o', 'r', - 'u', 's', 't', 'h', 'a', 'e', 'o', 'y', '\0', '\0', '\0', '\0', - '\0', '\0', '\0', '\0', 'a', 'b', 'd', 'd', 'e', 'f', 'g', 'h', - 'h', 'i', 'k', 'l', 'l', 'm', 'n', 'p', 'r', 'r', 's', 't', - 'u', 'v', 'w', 'w', 'x', 'y', 'z', 'h', 't', 'w', 'y', 'a', - 'e', 'i', 'o', 'u', 'y', +#define HIBIT ((unsigned char)0x80) + unsigned char aChar[] = { + '\0', 'a', 'c', 'e', 'i', 'n', + 'o', 'u', 'y', 'y', 'a', 'c', + 'd', 'e', 'e', 'g', 'h', 'i', + 'j', 'k', 'l', 'n', 'o', 'r', + 's', 't', 'u', 'u', 'w', 'y', + 'z', 'o', 'u', 'a', 'i', 'o', + 'u', 'u'|HIBIT, 'a'|HIBIT, 'g', 'k', 'o', + 'o'|HIBIT, 'j', 'g', 'n', 'a'|HIBIT, 'a', + 'e', 'i', 'o', 'r', 'u', 's', + 't', 'h', 'a', 'e', 'o'|HIBIT, 'o', + 'o'|HIBIT, 'y', '\0', '\0', '\0', '\0', + '\0', '\0', '\0', '\0', 'a', 'b', + 'c'|HIBIT, 'd', 'd', 'e'|HIBIT, 'e', 'e'|HIBIT, + 'f', 'g', 'h', 'h', 'i', 'i'|HIBIT, + 'k', 'l', 'l'|HIBIT, 'l', 'm', 'n', + 'o'|HIBIT, 'p', 'r', 'r'|HIBIT, 'r', 's', + 's'|HIBIT, 't', 'u', 'u'|HIBIT, 'v', 'w', + 'w', 'x', 'y', 'z', 'h', 't', + 'w', 'y', 'a', 'a'|HIBIT, 'a'|HIBIT, 'a'|HIBIT, + 'e', 'e'|HIBIT, 'e'|HIBIT, 'i', 'o', 'o'|HIBIT, + 'o'|HIBIT, 'o'|HIBIT, 'u', 'u'|HIBIT, 'u'|HIBIT, 'y', }; unsigned int key = (((unsigned int)c)<<3) | 0x00000007; @@ -199,7 +86,8 @@ static int fts5_remove_diacritic(int c){ } } assert( key>=aDia[iRes] ); - return ((c > (aDia[iRes]>>3) + (aDia[iRes]&0x07)) ? c : (int)aChar[iRes]); + if( bComplex==0 && (aChar[iRes] & 0x80) ) return c; + return (c > (aDia[iRes]>>3) + (aDia[iRes]&0x07)) ? c : ((int)aChar[iRes] & 0x7F); } @@ -212,8 +100,8 @@ int sqlite3Fts5UnicodeIsdiacritic(int c){ unsigned int mask1 = 0x000361F8; if( c<768 || c>817 ) return 0; return (c < 768+32) ? - (mask0 & (1 << (c-768))) : - (mask1 & (1 << (c-768-32))); + (mask0 & ((unsigned int)1 << (c-768))) : + (mask1 & ((unsigned int)1 << (c-768-32))); } @@ -226,7 +114,7 @@ int sqlite3Fts5UnicodeIsdiacritic(int c){ ** The results are undefined if the value passed to this function ** is less than zero. */ -int sqlite3Fts5UnicodeFold(int c, int bRemoveDiacritic){ +int sqlite3Fts5UnicodeFold(int c, int eRemoveDiacritic){ /* Each entry in the following array defines a rule for folding a range ** of codepoints to lower case. The rule applies to a range of nRange ** codepoints starting at codepoint iCode. @@ -349,7 +237,9 @@ int sqlite3Fts5UnicodeFold(int c, int bRemoveDiacritic){ assert( ret>0 ); } - if( bRemoveDiacritic ) ret = fts5_remove_diacritic(ret); + if( eRemoveDiacritic ){ + ret = fts5_remove_diacritic(ret, eRemoveDiacritic==2); + } } else if( c>=66560 && c<66600 ){ @@ -358,3 +248,533 @@ int sqlite3Fts5UnicodeFold(int c, int bRemoveDiacritic){ return ret; } + + +int sqlite3Fts5UnicodeCatParse(const char *zCat, u8 *aArray){ + aArray[0] = 1; + switch( zCat[0] ){ + case 'C': + switch( zCat[1] ){ + case 'c': aArray[1] = 1; break; + case 'f': aArray[2] = 1; break; + case 'n': aArray[3] = 1; break; + case 's': aArray[4] = 1; break; + case 'o': aArray[31] = 1; break; + case '*': + aArray[1] = 1; + aArray[2] = 1; + aArray[3] = 1; + aArray[4] = 1; + aArray[31] = 1; + break; + default: return 1; } + break; + + case 'L': + switch( zCat[1] ){ + case 'l': aArray[5] = 1; break; + case 'm': aArray[6] = 1; break; + case 'o': aArray[7] = 1; break; + case 't': aArray[8] = 1; break; + case 'u': aArray[9] = 1; break; + case 'C': aArray[30] = 1; break; + case '*': + aArray[5] = 1; + aArray[6] = 1; + aArray[7] = 1; + aArray[8] = 1; + aArray[9] = 1; + aArray[30] = 1; + break; + default: return 1; } + break; + + case 'M': + switch( zCat[1] ){ + case 'c': aArray[10] = 1; break; + case 'e': aArray[11] = 1; break; + case 'n': aArray[12] = 1; break; + case '*': + aArray[10] = 1; + aArray[11] = 1; + aArray[12] = 1; + break; + default: return 1; } + break; + + case 'N': + switch( zCat[1] ){ + case 'd': aArray[13] = 1; break; + case 'l': aArray[14] = 1; break; + case 'o': aArray[15] = 1; break; + case '*': + aArray[13] = 1; + aArray[14] = 1; + aArray[15] = 1; + break; + default: return 1; } + break; + + case 'P': + switch( zCat[1] ){ + case 'c': aArray[16] = 1; break; + case 'd': aArray[17] = 1; break; + case 'e': aArray[18] = 1; break; + case 'f': aArray[19] = 1; break; + case 'i': aArray[20] = 1; break; + case 'o': aArray[21] = 1; break; + case 's': aArray[22] = 1; break; + case '*': + aArray[16] = 1; + aArray[17] = 1; + aArray[18] = 1; + aArray[19] = 1; + aArray[20] = 1; + aArray[21] = 1; + aArray[22] = 1; + break; + default: return 1; } + break; + + case 'S': + switch( zCat[1] ){ + case 'c': aArray[23] = 1; break; + case 'k': aArray[24] = 1; break; + case 'm': aArray[25] = 1; break; + case 'o': aArray[26] = 1; break; + case '*': + aArray[23] = 1; + aArray[24] = 1; + aArray[25] = 1; + aArray[26] = 1; + break; + default: return 1; } + break; + + case 'Z': + switch( zCat[1] ){ + case 'l': aArray[27] = 1; break; + case 'p': aArray[28] = 1; break; + case 's': aArray[29] = 1; break; + case '*': + aArray[27] = 1; + aArray[28] = 1; + aArray[29] = 1; + break; + default: return 1; } + break; + + + default: + return 1; + } + return 0; +} + +static u16 aFts5UnicodeBlock[] = { + 0, 1471, 1753, 1760, 1760, 1760, 1760, 1760, 1760, 1760, + 1760, 1760, 1760, 1760, 1760, 1763, 1765, + }; +static u16 aFts5UnicodeMap[] = { + 0, 32, 33, 36, 37, 40, 41, 42, 43, 44, + 45, 46, 48, 58, 60, 63, 65, 91, 92, 93, + 94, 95, 96, 97, 123, 124, 125, 126, 127, 160, + 161, 162, 166, 167, 168, 169, 170, 171, 172, 173, + 174, 175, 176, 177, 178, 180, 181, 182, 184, 185, + 186, 187, 188, 191, 192, 215, 216, 223, 247, 248, + 256, 312, 313, 329, 330, 377, 383, 385, 387, 388, + 391, 394, 396, 398, 402, 403, 405, 406, 409, 412, + 414, 415, 417, 418, 423, 427, 428, 431, 434, 436, + 437, 440, 442, 443, 444, 446, 448, 452, 453, 454, + 455, 456, 457, 458, 459, 460, 461, 477, 478, 496, + 497, 498, 499, 500, 503, 505, 506, 564, 570, 572, + 573, 575, 577, 580, 583, 584, 592, 660, 661, 688, + 706, 710, 722, 736, 741, 748, 749, 750, 751, 768, + 880, 884, 885, 886, 890, 891, 894, 900, 902, 903, + 904, 908, 910, 912, 913, 931, 940, 975, 977, 978, + 981, 984, 1008, 1012, 1014, 1015, 1018, 1020, 1021, 1072, + 1120, 1154, 1155, 1160, 1162, 1217, 1231, 1232, 1329, 1369, + 1370, 1377, 1417, 1418, 1423, 1425, 1470, 1471, 1472, 1473, + 1475, 1476, 1478, 1479, 1488, 1520, 1523, 1536, 1542, 1545, + 1547, 1548, 1550, 1552, 1563, 1566, 1568, 1600, 1601, 1611, + 1632, 1642, 1646, 1648, 1649, 1748, 1749, 1750, 1757, 1758, + 1759, 1765, 1767, 1769, 1770, 1774, 1776, 1786, 1789, 1791, + 1792, 1807, 1808, 1809, 1810, 1840, 1869, 1958, 1969, 1984, + 1994, 2027, 2036, 2038, 2039, 2042, 2048, 2070, 2074, 2075, + 2084, 2085, 2088, 2089, 2096, 2112, 2137, 2142, 2208, 2210, + 2276, 2304, 2307, 2308, 2362, 2363, 2364, 2365, 2366, 2369, + 2377, 2381, 2382, 2384, 2385, 2392, 2402, 2404, 2406, 2416, + 2417, 2418, 2425, 2433, 2434, 2437, 2447, 2451, 2474, 2482, + 2486, 2492, 2493, 2494, 2497, 2503, 2507, 2509, 2510, 2519, + 2524, 2527, 2530, 2534, 2544, 2546, 2548, 2554, 2555, 2561, + 2563, 2565, 2575, 2579, 2602, 2610, 2613, 2616, 2620, 2622, + 2625, 2631, 2635, 2641, 2649, 2654, 2662, 2672, 2674, 2677, + 2689, 2691, 2693, 2703, 2707, 2730, 2738, 2741, 2748, 2749, + 2750, 2753, 2759, 2761, 2763, 2765, 2768, 2784, 2786, 2790, + 2800, 2801, 2817, 2818, 2821, 2831, 2835, 2858, 2866, 2869, + 2876, 2877, 2878, 2879, 2880, 2881, 2887, 2891, 2893, 2902, + 2903, 2908, 2911, 2914, 2918, 2928, 2929, 2930, 2946, 2947, + 2949, 2958, 2962, 2969, 2972, 2974, 2979, 2984, 2990, 3006, + 3008, 3009, 3014, 3018, 3021, 3024, 3031, 3046, 3056, 3059, + 3065, 3066, 3073, 3077, 3086, 3090, 3114, 3125, 3133, 3134, + 3137, 3142, 3146, 3157, 3160, 3168, 3170, 3174, 3192, 3199, + 3202, 3205, 3214, 3218, 3242, 3253, 3260, 3261, 3262, 3263, + 3264, 3270, 3271, 3274, 3276, 3285, 3294, 3296, 3298, 3302, + 3313, 3330, 3333, 3342, 3346, 3389, 3390, 3393, 3398, 3402, + 3405, 3406, 3415, 3424, 3426, 3430, 3440, 3449, 3450, 3458, + 3461, 3482, 3507, 3517, 3520, 3530, 3535, 3538, 3542, 3544, + 3570, 3572, 3585, 3633, 3634, 3636, 3647, 3648, 3654, 3655, + 3663, 3664, 3674, 3713, 3716, 3719, 3722, 3725, 3732, 3737, + 3745, 3749, 3751, 3754, 3757, 3761, 3762, 3764, 3771, 3773, + 3776, 3782, 3784, 3792, 3804, 3840, 3841, 3844, 3859, 3860, + 3861, 3864, 3866, 3872, 3882, 3892, 3893, 3894, 3895, 3896, + 3897, 3898, 3899, 3900, 3901, 3902, 3904, 3913, 3953, 3967, + 3968, 3973, 3974, 3976, 3981, 3993, 4030, 4038, 4039, 4046, + 4048, 4053, 4057, 4096, 4139, 4141, 4145, 4146, 4152, 4153, + 4155, 4157, 4159, 4160, 4170, 4176, 4182, 4184, 4186, 4190, + 4193, 4194, 4197, 4199, 4206, 4209, 4213, 4226, 4227, 4229, + 4231, 4237, 4238, 4239, 4240, 4250, 4253, 4254, 4256, 4295, + 4301, 4304, 4347, 4348, 4349, 4682, 4688, 4696, 4698, 4704, + 4746, 4752, 4786, 4792, 4800, 4802, 4808, 4824, 4882, 4888, + 4957, 4960, 4969, 4992, 5008, 5024, 5120, 5121, 5741, 5743, + 5760, 5761, 5787, 5788, 5792, 5867, 5870, 5888, 5902, 5906, + 5920, 5938, 5941, 5952, 5970, 5984, 5998, 6002, 6016, 6068, + 6070, 6071, 6078, 6086, 6087, 6089, 6100, 6103, 6104, 6107, + 6108, 6109, 6112, 6128, 6144, 6150, 6151, 6155, 6158, 6160, + 6176, 6211, 6212, 6272, 6313, 6314, 6320, 6400, 6432, 6435, + 6439, 6441, 6448, 6450, 6451, 6457, 6464, 6468, 6470, 6480, + 6512, 6528, 6576, 6593, 6600, 6608, 6618, 6622, 6656, 6679, + 6681, 6686, 6688, 6741, 6742, 6743, 6744, 6752, 6753, 6754, + 6755, 6757, 6765, 6771, 6783, 6784, 6800, 6816, 6823, 6824, + 6912, 6916, 6917, 6964, 6965, 6966, 6971, 6972, 6973, 6978, + 6979, 6981, 6992, 7002, 7009, 7019, 7028, 7040, 7042, 7043, + 7073, 7074, 7078, 7080, 7082, 7083, 7084, 7086, 7088, 7098, + 7142, 7143, 7144, 7146, 7149, 7150, 7151, 7154, 7164, 7168, + 7204, 7212, 7220, 7222, 7227, 7232, 7245, 7248, 7258, 7288, + 7294, 7360, 7376, 7379, 7380, 7393, 7394, 7401, 7405, 7406, + 7410, 7412, 7413, 7424, 7468, 7531, 7544, 7545, 7579, 7616, + 7676, 7680, 7830, 7838, 7936, 7944, 7952, 7960, 7968, 7976, + 7984, 7992, 8000, 8008, 8016, 8025, 8027, 8029, 8031, 8033, + 8040, 8048, 8064, 8072, 8080, 8088, 8096, 8104, 8112, 8118, + 8120, 8124, 8125, 8126, 8127, 8130, 8134, 8136, 8140, 8141, + 8144, 8150, 8152, 8157, 8160, 8168, 8173, 8178, 8182, 8184, + 8188, 8189, 8192, 8203, 8208, 8214, 8216, 8217, 8218, 8219, + 8221, 8222, 8223, 8224, 8232, 8233, 8234, 8239, 8240, 8249, + 8250, 8251, 8255, 8257, 8260, 8261, 8262, 8263, 8274, 8275, + 8276, 8277, 8287, 8288, 8298, 8304, 8305, 8308, 8314, 8317, + 8318, 8319, 8320, 8330, 8333, 8334, 8336, 8352, 8400, 8413, + 8417, 8418, 8421, 8448, 8450, 8451, 8455, 8456, 8458, 8459, + 8462, 8464, 8467, 8468, 8469, 8470, 8472, 8473, 8478, 8484, + 8485, 8486, 8487, 8488, 8489, 8490, 8494, 8495, 8496, 8500, + 8501, 8505, 8506, 8508, 8510, 8512, 8517, 8519, 8522, 8523, + 8524, 8526, 8527, 8528, 8544, 8579, 8581, 8585, 8592, 8597, + 8602, 8604, 8608, 8609, 8611, 8612, 8614, 8615, 8622, 8623, + 8654, 8656, 8658, 8659, 8660, 8661, 8692, 8960, 8968, 8972, + 8992, 8994, 9001, 9002, 9003, 9084, 9085, 9115, 9140, 9180, + 9186, 9216, 9280, 9312, 9372, 9450, 9472, 9655, 9656, 9665, + 9666, 9720, 9728, 9839, 9840, 9985, 10088, 10089, 10090, 10091, + 10092, 10093, 10094, 10095, 10096, 10097, 10098, 10099, 10100, 10101, + 10102, 10132, 10176, 10181, 10182, 10183, 10214, 10215, 10216, 10217, + 10218, 10219, 10220, 10221, 10222, 10223, 10224, 10240, 10496, 10627, + 10628, 10629, 10630, 10631, 10632, 10633, 10634, 10635, 10636, 10637, + 10638, 10639, 10640, 10641, 10642, 10643, 10644, 10645, 10646, 10647, + 10648, 10649, 10712, 10713, 10714, 10715, 10716, 10748, 10749, 10750, + 11008, 11056, 11077, 11079, 11088, 11264, 11312, 11360, 11363, 11365, + 11367, 11374, 11377, 11378, 11380, 11381, 11383, 11388, 11390, 11393, + 11394, 11492, 11493, 11499, 11503, 11506, 11513, 11517, 11518, 11520, + 11559, 11565, 11568, 11631, 11632, 11647, 11648, 11680, 11688, 11696, + 11704, 11712, 11720, 11728, 11736, 11744, 11776, 11778, 11779, 11780, + 11781, 11782, 11785, 11786, 11787, 11788, 11789, 11790, 11799, 11800, + 11802, 11803, 11804, 11805, 11806, 11808, 11809, 11810, 11811, 11812, + 11813, 11814, 11815, 11816, 11817, 11818, 11823, 11824, 11834, 11904, + 11931, 12032, 12272, 12288, 12289, 12292, 12293, 12294, 12295, 12296, + 12297, 12298, 12299, 12300, 12301, 12302, 12303, 12304, 12305, 12306, + 12308, 12309, 12310, 12311, 12312, 12313, 12314, 12315, 12316, 12317, + 12318, 12320, 12321, 12330, 12334, 12336, 12337, 12342, 12344, 12347, + 12348, 12349, 12350, 12353, 12441, 12443, 12445, 12447, 12448, 12449, + 12539, 12540, 12543, 12549, 12593, 12688, 12690, 12694, 12704, 12736, + 12784, 12800, 12832, 12842, 12872, 12880, 12881, 12896, 12928, 12938, + 12977, 12992, 13056, 13312, 19893, 19904, 19968, 40908, 40960, 40981, + 40982, 42128, 42192, 42232, 42238, 42240, 42508, 42509, 42512, 42528, + 42538, 42560, 42606, 42607, 42608, 42611, 42612, 42622, 42623, 42624, + 42655, 42656, 42726, 42736, 42738, 42752, 42775, 42784, 42786, 42800, + 42802, 42864, 42865, 42873, 42878, 42888, 42889, 42891, 42896, 42912, + 43000, 43002, 43003, 43010, 43011, 43014, 43015, 43019, 43020, 43043, + 43045, 43047, 43048, 43056, 43062, 43064, 43065, 43072, 43124, 43136, + 43138, 43188, 43204, 43214, 43216, 43232, 43250, 43256, 43259, 43264, + 43274, 43302, 43310, 43312, 43335, 43346, 43359, 43360, 43392, 43395, + 43396, 43443, 43444, 43446, 43450, 43452, 43453, 43457, 43471, 43472, + 43486, 43520, 43561, 43567, 43569, 43571, 43573, 43584, 43587, 43588, + 43596, 43597, 43600, 43612, 43616, 43632, 43633, 43639, 43642, 43643, + 43648, 43696, 43697, 43698, 43701, 43703, 43705, 43710, 43712, 43713, + 43714, 43739, 43741, 43742, 43744, 43755, 43756, 43758, 43760, 43762, + 43763, 43765, 43766, 43777, 43785, 43793, 43808, 43816, 43968, 44003, + 44005, 44006, 44008, 44009, 44011, 44012, 44013, 44016, 44032, 55203, + 55216, 55243, 55296, 56191, 56319, 57343, 57344, 63743, 63744, 64112, + 64256, 64275, 64285, 64286, 64287, 64297, 64298, 64312, 64318, 64320, + 64323, 64326, 64434, 64467, 64830, 64831, 64848, 64914, 65008, 65020, + 65021, 65024, 65040, 65047, 65048, 65049, 65056, 65072, 65073, 65075, + 65077, 65078, 65079, 65080, 65081, 65082, 65083, 65084, 65085, 65086, + 65087, 65088, 65089, 65090, 65091, 65092, 65093, 65095, 65096, 65097, + 65101, 65104, 65108, 65112, 65113, 65114, 65115, 65116, 65117, 65118, + 65119, 65122, 65123, 65124, 65128, 65129, 65130, 65136, 65142, 65279, + 65281, 65284, 65285, 65288, 65289, 65290, 65291, 65292, 65293, 65294, + 65296, 65306, 65308, 65311, 65313, 65339, 65340, 65341, 65342, 65343, + 65344, 65345, 65371, 65372, 65373, 65374, 65375, 65376, 65377, 65378, + 65379, 65380, 65382, 65392, 65393, 65438, 65440, 65474, 65482, 65490, + 65498, 65504, 65506, 65507, 65508, 65509, 65512, 65513, 65517, 65529, + 65532, 0, 13, 40, 60, 63, 80, 128, 256, 263, + 311, 320, 373, 377, 394, 400, 464, 509, 640, 672, + 768, 800, 816, 833, 834, 842, 896, 927, 928, 968, + 976, 977, 1024, 1064, 1104, 1184, 2048, 2056, 2058, 2103, + 2108, 2111, 2135, 2136, 2304, 2326, 2335, 2336, 2367, 2432, + 2494, 2560, 2561, 2565, 2572, 2576, 2581, 2585, 2616, 2623, + 2624, 2640, 2656, 2685, 2687, 2816, 2873, 2880, 2904, 2912, + 2936, 3072, 3680, 4096, 4097, 4098, 4099, 4152, 4167, 4178, + 4198, 4224, 4226, 4227, 4272, 4275, 4279, 4281, 4283, 4285, + 4286, 4304, 4336, 4352, 4355, 4391, 4396, 4397, 4406, 4416, + 4480, 4482, 4483, 4531, 4534, 4543, 4545, 4549, 4560, 5760, + 5803, 5804, 5805, 5806, 5808, 5814, 5815, 5824, 8192, 9216, + 9328, 12288, 26624, 28416, 28496, 28497, 28559, 28563, 45056, 53248, + 53504, 53545, 53605, 53607, 53610, 53613, 53619, 53627, 53635, 53637, + 53644, 53674, 53678, 53760, 53826, 53829, 54016, 54112, 54272, 54298, + 54324, 54350, 54358, 54376, 54402, 54428, 54430, 54434, 54437, 54441, + 54446, 54454, 54459, 54461, 54469, 54480, 54506, 54532, 54535, 54541, + 54550, 54558, 54584, 54587, 54592, 54598, 54602, 54610, 54636, 54662, + 54688, 54714, 54740, 54766, 54792, 54818, 54844, 54870, 54896, 54922, + 54952, 54977, 54978, 55003, 55004, 55010, 55035, 55036, 55061, 55062, + 55068, 55093, 55094, 55119, 55120, 55126, 55151, 55152, 55177, 55178, + 55184, 55209, 55210, 55235, 55236, 55242, 55246, 60928, 60933, 60961, + 60964, 60967, 60969, 60980, 60985, 60987, 60994, 60999, 61001, 61003, + 61005, 61009, 61012, 61015, 61017, 61019, 61021, 61023, 61025, 61028, + 61031, 61036, 61044, 61049, 61054, 61056, 61067, 61089, 61093, 61099, + 61168, 61440, 61488, 61600, 61617, 61633, 61649, 61696, 61712, 61744, + 61808, 61926, 61968, 62016, 62032, 62208, 62256, 62263, 62336, 62368, + 62406, 62432, 62464, 62528, 62530, 62713, 62720, 62784, 62800, 62971, + 63045, 63104, 63232, 0, 42710, 42752, 46900, 46912, 47133, 63488, + 1, 32, 256, 0, 65533, + }; +static u16 aFts5UnicodeData[] = { + 1025, 61, 117, 55, 117, 54, 50, 53, 57, 53, + 49, 85, 333, 85, 121, 85, 841, 54, 53, 50, + 56, 48, 56, 837, 54, 57, 50, 57, 1057, 61, + 53, 151, 58, 53, 56, 58, 39, 52, 57, 34, + 58, 56, 58, 57, 79, 56, 37, 85, 56, 47, + 39, 51, 111, 53, 745, 57, 233, 773, 57, 261, + 1822, 37, 542, 37, 1534, 222, 69, 73, 37, 126, + 126, 73, 69, 137, 37, 73, 37, 105, 101, 73, + 37, 73, 37, 190, 158, 37, 126, 126, 73, 37, + 126, 94, 37, 39, 94, 69, 135, 41, 40, 37, + 41, 40, 37, 41, 40, 37, 542, 37, 606, 37, + 41, 40, 37, 126, 73, 37, 1886, 197, 73, 37, + 73, 69, 126, 105, 37, 286, 2181, 39, 869, 582, + 152, 390, 472, 166, 248, 38, 56, 38, 568, 3596, + 158, 38, 56, 94, 38, 101, 53, 88, 41, 53, + 105, 41, 73, 37, 553, 297, 1125, 94, 37, 105, + 101, 798, 133, 94, 57, 126, 94, 37, 1641, 1541, + 1118, 58, 172, 75, 1790, 478, 37, 2846, 1225, 38, + 213, 1253, 53, 49, 55, 1452, 49, 44, 53, 76, + 53, 76, 53, 44, 871, 103, 85, 162, 121, 85, + 55, 85, 90, 364, 53, 85, 1031, 38, 327, 684, + 333, 149, 71, 44, 3175, 53, 39, 236, 34, 58, + 204, 70, 76, 58, 140, 71, 333, 103, 90, 39, + 469, 34, 39, 44, 967, 876, 2855, 364, 39, 333, + 1063, 300, 70, 58, 117, 38, 711, 140, 38, 300, + 38, 108, 38, 172, 501, 807, 108, 53, 39, 359, + 876, 108, 42, 1735, 44, 42, 44, 39, 106, 268, + 138, 44, 74, 39, 236, 327, 76, 85, 333, 53, + 38, 199, 231, 44, 74, 263, 71, 711, 231, 39, + 135, 44, 39, 106, 140, 74, 74, 44, 39, 42, + 71, 103, 76, 333, 71, 87, 207, 58, 55, 76, + 42, 199, 71, 711, 231, 71, 71, 71, 44, 106, + 76, 76, 108, 44, 135, 39, 333, 76, 103, 44, + 76, 42, 295, 103, 711, 231, 71, 167, 44, 39, + 106, 172, 76, 42, 74, 44, 39, 71, 76, 333, + 53, 55, 44, 74, 263, 71, 711, 231, 71, 167, + 44, 39, 42, 44, 42, 140, 74, 74, 44, 44, + 42, 71, 103, 76, 333, 58, 39, 207, 44, 39, + 199, 103, 135, 71, 39, 71, 71, 103, 391, 74, + 44, 74, 106, 106, 44, 39, 42, 333, 111, 218, + 55, 58, 106, 263, 103, 743, 327, 167, 39, 108, + 138, 108, 140, 76, 71, 71, 76, 333, 239, 58, + 74, 263, 103, 743, 327, 167, 44, 39, 42, 44, + 170, 44, 74, 74, 76, 74, 39, 71, 76, 333, + 71, 74, 263, 103, 1319, 39, 106, 140, 106, 106, + 44, 39, 42, 71, 76, 333, 207, 58, 199, 74, + 583, 775, 295, 39, 231, 44, 106, 108, 44, 266, + 74, 53, 1543, 44, 71, 236, 55, 199, 38, 268, + 53, 333, 85, 71, 39, 71, 39, 39, 135, 231, + 103, 39, 39, 71, 135, 44, 71, 204, 76, 39, + 167, 38, 204, 333, 135, 39, 122, 501, 58, 53, + 122, 76, 218, 333, 335, 58, 44, 58, 44, 58, + 44, 54, 50, 54, 50, 74, 263, 1159, 460, 42, + 172, 53, 76, 167, 364, 1164, 282, 44, 218, 90, + 181, 154, 85, 1383, 74, 140, 42, 204, 42, 76, + 74, 76, 39, 333, 213, 199, 74, 76, 135, 108, + 39, 106, 71, 234, 103, 140, 423, 44, 74, 76, + 202, 44, 39, 42, 333, 106, 44, 90, 1225, 41, + 41, 1383, 53, 38, 10631, 135, 231, 39, 135, 1319, + 135, 1063, 135, 231, 39, 135, 487, 1831, 135, 2151, + 108, 309, 655, 519, 346, 2727, 49, 19847, 85, 551, + 61, 839, 54, 50, 2407, 117, 110, 423, 135, 108, + 583, 108, 85, 583, 76, 423, 103, 76, 1671, 76, + 42, 236, 266, 44, 74, 364, 117, 38, 117, 55, + 39, 44, 333, 335, 213, 49, 149, 108, 61, 333, + 1127, 38, 1671, 1319, 44, 39, 2247, 935, 108, 138, + 76, 106, 74, 44, 202, 108, 58, 85, 333, 967, + 167, 1415, 554, 231, 74, 333, 47, 1114, 743, 76, + 106, 85, 1703, 42, 44, 42, 236, 44, 42, 44, + 74, 268, 202, 332, 44, 333, 333, 245, 38, 213, + 140, 42, 1511, 44, 42, 172, 42, 44, 170, 44, + 74, 231, 333, 245, 346, 300, 314, 76, 42, 967, + 42, 140, 74, 76, 42, 44, 74, 71, 333, 1415, + 44, 42, 76, 106, 44, 42, 108, 74, 149, 1159, + 266, 268, 74, 76, 181, 333, 103, 333, 967, 198, + 85, 277, 108, 53, 428, 42, 236, 135, 44, 135, + 74, 44, 71, 1413, 2022, 421, 38, 1093, 1190, 1260, + 140, 4830, 261, 3166, 261, 265, 197, 201, 261, 265, + 261, 265, 197, 201, 261, 41, 41, 41, 94, 229, + 265, 453, 261, 264, 261, 264, 261, 264, 165, 69, + 137, 40, 56, 37, 120, 101, 69, 137, 40, 120, + 133, 69, 137, 120, 261, 169, 120, 101, 69, 137, + 40, 88, 381, 162, 209, 85, 52, 51, 54, 84, + 51, 54, 52, 277, 59, 60, 162, 61, 309, 52, + 51, 149, 80, 117, 57, 54, 50, 373, 57, 53, + 48, 341, 61, 162, 194, 47, 38, 207, 121, 54, + 50, 38, 335, 121, 54, 50, 422, 855, 428, 139, + 44, 107, 396, 90, 41, 154, 41, 90, 37, 105, + 69, 105, 37, 58, 41, 90, 57, 169, 218, 41, + 58, 41, 58, 41, 58, 137, 58, 37, 137, 37, + 135, 37, 90, 69, 73, 185, 94, 101, 58, 57, + 90, 37, 58, 527, 1134, 94, 142, 47, 185, 186, + 89, 154, 57, 90, 57, 90, 57, 250, 57, 1018, + 89, 90, 57, 58, 57, 1018, 8601, 282, 153, 666, + 89, 250, 54, 50, 2618, 57, 986, 825, 1306, 217, + 602, 1274, 378, 1935, 2522, 719, 5882, 57, 314, 57, + 1754, 281, 3578, 57, 4634, 3322, 54, 50, 54, 50, + 54, 50, 54, 50, 54, 50, 54, 50, 54, 50, + 975, 1434, 185, 54, 50, 1017, 54, 50, 54, 50, + 54, 50, 54, 50, 54, 50, 537, 8218, 4217, 54, + 50, 54, 50, 54, 50, 54, 50, 54, 50, 54, + 50, 54, 50, 54, 50, 54, 50, 54, 50, 54, + 50, 2041, 54, 50, 54, 50, 1049, 54, 50, 8281, + 1562, 697, 90, 217, 346, 1513, 1509, 126, 73, 69, + 254, 105, 37, 94, 37, 94, 165, 70, 105, 37, + 3166, 37, 218, 158, 108, 94, 149, 47, 85, 1221, + 37, 37, 1799, 38, 53, 44, 743, 231, 231, 231, + 231, 231, 231, 231, 231, 1036, 85, 52, 51, 52, + 51, 117, 52, 51, 53, 52, 51, 309, 49, 85, + 49, 53, 52, 51, 85, 52, 51, 54, 50, 54, + 50, 54, 50, 54, 50, 181, 38, 341, 81, 858, + 2874, 6874, 410, 61, 117, 58, 38, 39, 46, 54, + 50, 54, 50, 54, 50, 54, 50, 54, 50, 90, + 54, 50, 54, 50, 54, 50, 54, 50, 49, 54, + 82, 58, 302, 140, 74, 49, 166, 90, 110, 38, + 39, 53, 90, 2759, 76, 88, 70, 39, 49, 2887, + 53, 102, 39, 1319, 3015, 90, 143, 346, 871, 1178, + 519, 1018, 335, 986, 271, 58, 495, 1050, 335, 1274, + 495, 2042, 8218, 39, 39, 2074, 39, 39, 679, 38, + 36583, 1786, 1287, 198, 85, 8583, 38, 117, 519, 333, + 71, 1502, 39, 44, 107, 53, 332, 53, 38, 798, + 44, 2247, 334, 76, 213, 760, 294, 88, 478, 69, + 2014, 38, 261, 190, 350, 38, 88, 158, 158, 382, + 70, 37, 231, 44, 103, 44, 135, 44, 743, 74, + 76, 42, 154, 207, 90, 55, 58, 1671, 149, 74, + 1607, 522, 44, 85, 333, 588, 199, 117, 39, 333, + 903, 268, 85, 743, 364, 74, 53, 935, 108, 42, + 1511, 44, 74, 140, 74, 44, 138, 437, 38, 333, + 85, 1319, 204, 74, 76, 74, 76, 103, 44, 263, + 44, 42, 333, 149, 519, 38, 199, 122, 39, 42, + 1543, 44, 39, 108, 71, 76, 167, 76, 39, 44, + 39, 71, 38, 85, 359, 42, 76, 74, 85, 39, + 70, 42, 44, 199, 199, 199, 231, 231, 1127, 74, + 44, 74, 44, 74, 53, 42, 44, 333, 39, 39, + 743, 1575, 36, 68, 68, 36, 63, 63, 11719, 3399, + 229, 165, 39, 44, 327, 57, 423, 167, 39, 71, + 71, 3463, 536, 11623, 54, 50, 2055, 1735, 391, 55, + 58, 524, 245, 54, 50, 53, 236, 53, 81, 80, + 54, 50, 54, 50, 54, 50, 54, 50, 54, 50, + 54, 50, 54, 50, 54, 50, 85, 54, 50, 149, + 112, 117, 149, 49, 54, 50, 54, 50, 54, 50, + 117, 57, 49, 121, 53, 55, 85, 167, 4327, 34, + 117, 55, 117, 54, 50, 53, 57, 53, 49, 85, + 333, 85, 121, 85, 841, 54, 53, 50, 56, 48, + 56, 837, 54, 57, 50, 57, 54, 50, 53, 54, + 50, 85, 327, 38, 1447, 70, 999, 199, 199, 199, + 103, 87, 57, 56, 58, 87, 58, 153, 90, 98, + 90, 391, 839, 615, 71, 487, 455, 3943, 117, 1455, + 314, 1710, 143, 570, 47, 410, 1466, 44, 935, 1575, + 999, 143, 551, 46, 263, 46, 967, 53, 1159, 263, + 53, 174, 1289, 1285, 2503, 333, 199, 39, 1415, 71, + 39, 743, 53, 271, 711, 207, 53, 839, 53, 1799, + 71, 39, 108, 76, 140, 135, 103, 871, 108, 44, + 271, 309, 935, 79, 53, 1735, 245, 711, 271, 615, + 271, 2343, 1007, 42, 44, 42, 1703, 492, 245, 655, + 333, 76, 42, 1447, 106, 140, 74, 76, 85, 34, + 149, 807, 333, 108, 1159, 172, 42, 268, 333, 149, + 76, 42, 1543, 106, 300, 74, 135, 149, 333, 1383, + 44, 42, 44, 74, 204, 42, 44, 333, 28135, 3182, + 149, 34279, 18215, 2215, 39, 1482, 140, 422, 71, 7898, + 1274, 1946, 74, 108, 122, 202, 258, 268, 90, 236, + 986, 140, 1562, 2138, 108, 58, 2810, 591, 841, 837, + 841, 229, 581, 841, 837, 41, 73, 41, 73, 137, + 265, 133, 37, 229, 357, 841, 837, 73, 137, 265, + 233, 837, 73, 137, 169, 41, 233, 837, 841, 837, + 841, 837, 841, 837, 841, 837, 841, 837, 841, 901, + 809, 57, 805, 57, 197, 809, 57, 805, 57, 197, + 809, 57, 805, 57, 197, 809, 57, 805, 57, 197, + 809, 57, 805, 57, 197, 94, 1613, 135, 871, 71, + 39, 39, 327, 135, 39, 39, 39, 39, 39, 39, + 103, 71, 39, 39, 39, 39, 39, 39, 71, 39, + 135, 231, 135, 135, 39, 327, 551, 103, 167, 551, + 89, 1434, 3226, 506, 474, 506, 506, 367, 1018, 1946, + 1402, 954, 1402, 314, 90, 1082, 218, 2266, 666, 1210, + 186, 570, 2042, 58, 5850, 154, 2010, 154, 794, 2266, + 378, 2266, 3738, 39, 39, 39, 39, 39, 39, 17351, + 34, 3074, 7692, 63, 63, + }; + +int sqlite3Fts5UnicodeCategory(u32 iCode) { + int iRes = -1; + int iHi; + int iLo; + int ret; + u16 iKey; + + if( iCode>=(1<<20) ){ + return 0; + } + iLo = aFts5UnicodeBlock[(iCode>>16)]; + iHi = aFts5UnicodeBlock[1+(iCode>>16)]; + iKey = (iCode & 0xFFFF); + while( iHi>iLo ){ + int iTest = (iHi + iLo) / 2; + assert( iTest>=iLo && iTest=aFts5UnicodeMap[iTest] ){ + iRes = iTest; + iLo = iTest+1; + }else{ + iHi = iTest; + } + } + + if( iRes<0 ) return 0; + if( iKey>=(aFts5UnicodeMap[iRes]+(aFts5UnicodeData[iRes]>>5)) ) return 0; + ret = aFts5UnicodeData[iRes] & 0x1F; + if( ret!=30 ) return ret; + return ((iKey - aFts5UnicodeMap[iRes]) & 0x01) ? 5 : 9; +} + +void sqlite3Fts5UnicodeAscii(u8 *aArray, u8 *aAscii){ + int i = 0; + int iTbl = 0; + while( i<128 ){ + int bToken = aArray[ aFts5UnicodeData[iTbl] & 0x1F ]; + int n = (aFts5UnicodeData[iTbl] >> 5) + i; + for(; i<128 && i3 && n<=9 ); return n; } @@ -342,4 +342,3 @@ int sqlite3Fts5GetVarintLen(u32 iVal){ if( iVal<(1 << 28) ) return 4; return 5; } - diff --git a/ext/fts5/fts5_vocab.c b/ext/fts5/fts5_vocab.c index f3a2381cc1..3a6a968f7c 100644 --- a/ext/fts5/fts5_vocab.c +++ b/ext/fts5/fts5_vocab.c @@ -29,6 +29,11 @@ ** the number of fts5 rows that contain at least one instance of term ** $term. Field $cnt is set to the total number of instances of term ** $term in the database. +** +** instance: +** CREATE TABLE vocab(term, doc, col, offset, PRIMARY KEY()); +** +** One row for each term instance in the database. */ @@ -44,43 +49,53 @@ struct Fts5VocabTable { char *zFts5Db; /* Db containing fts5 table */ sqlite3 *db; /* Database handle */ Fts5Global *pGlobal; /* FTS5 global object for this database */ - int eType; /* FTS5_VOCAB_COL or ROW */ + int eType; /* FTS5_VOCAB_COL, ROW or INSTANCE */ + unsigned bBusy; /* True if busy */ }; struct Fts5VocabCursor { sqlite3_vtab_cursor base; sqlite3_stmt *pStmt; /* Statement holding lock on pIndex */ - Fts5Index *pIndex; /* Associated FTS5 index */ + Fts5Table *pFts5; /* Associated FTS5 table */ int bEof; /* True if this cursor is at EOF */ Fts5IndexIter *pIter; /* Term/rowid iterator object */ + void *pStruct; /* From sqlite3Fts5StructureRef() */ int nLeTerm; /* Size of zLeTerm in bytes */ char *zLeTerm; /* (term <= $zLeTerm) paramater, or NULL */ + int colUsed; /* Copy of sqlite3_index_info.colUsed */ /* These are used by 'col' tables only */ - Fts5Config *pConfig; /* Fts5 table configuration */ int iCol; i64 *aCnt; i64 *aDoc; - /* Output values used by 'row' and 'col' tables */ + /* Output values used by all tables. */ i64 rowid; /* This table's current rowid value */ Fts5Buffer term; /* Current value of 'term' column */ + + /* Output values Used by 'instance' tables only */ + i64 iInstPos; + int iInstOff; }; -#define FTS5_VOCAB_COL 0 -#define FTS5_VOCAB_ROW 1 +#define FTS5_VOCAB_COL 0 +#define FTS5_VOCAB_ROW 1 +#define FTS5_VOCAB_INSTANCE 2 #define FTS5_VOCAB_COL_SCHEMA "term, col, doc, cnt" #define FTS5_VOCAB_ROW_SCHEMA "term, doc, cnt" +#define FTS5_VOCAB_INST_SCHEMA "term, doc, col, offset" /* ** Bits for the mask used as the idxNum value by xBestIndex/xFilter. */ -#define FTS5_VOCAB_TERM_EQ 0x01 -#define FTS5_VOCAB_TERM_GE 0x02 -#define FTS5_VOCAB_TERM_LE 0x04 +#define FTS5_VOCAB_TERM_EQ 0x0100 +#define FTS5_VOCAB_TERM_GE 0x0200 +#define FTS5_VOCAB_TERM_LE 0x0400 + +#define FTS5_VOCAB_COLUSED_MASK 0xFF /* @@ -101,6 +116,9 @@ static int fts5VocabTableType(const char *zType, char **pzErr, int *peType){ if( sqlite3_stricmp(zCopy, "row")==0 ){ *peType = FTS5_VOCAB_ROW; }else + if( sqlite3_stricmp(zCopy, "instance")==0 ){ + *peType = FTS5_VOCAB_INSTANCE; + }else { *pzErr = sqlite3_mprintf("fts5vocab: unknown table type: %Q", zCopy); rc = SQLITE_ERROR; @@ -161,7 +179,8 @@ static int fts5VocabInitVtab( ){ const char *azSchema[] = { "CREATE TABlE vocab(" FTS5_VOCAB_COL_SCHEMA ")", - "CREATE TABlE vocab(" FTS5_VOCAB_ROW_SCHEMA ")" + "CREATE TABlE vocab(" FTS5_VOCAB_ROW_SCHEMA ")", + "CREATE TABlE vocab(" FTS5_VOCAB_INST_SCHEMA ")" }; Fts5VocabTable *pRet = 0; @@ -174,12 +193,12 @@ static int fts5VocabInitVtab( *pzErr = sqlite3_mprintf("wrong number of vtable arguments"); rc = SQLITE_ERROR; }else{ - int nByte; /* Bytes of space to allocate */ + i64 nByte; /* Bytes of space to allocate */ const char *zDb = bDb ? argv[3] : argv[1]; const char *zTab = bDb ? argv[4] : argv[3]; const char *zType = bDb ? argv[5] : argv[4]; - int nDb = (int)strlen(zDb)+1; - int nTab = (int)strlen(zTab)+1; + i64 nDb = strlen(zDb)+1; + i64 nTab = strlen(zTab)+1; int eType = 0; rc = fts5VocabTableType(zType, pzErr, &eType); @@ -235,6 +254,15 @@ static int fts5VocabCreateMethod( /* ** Implementation of the xBestIndex method. +** +** Only constraints of the form: +** +** term <= ? +** term == ? +** term >= ? +** +** are interpreted. Less-than and less-than-or-equal are treated +** identically, as are greater-than and greater-than-or-equal. */ static int fts5VocabBestIndexMethod( sqlite3_vtab *pUnused, @@ -244,11 +272,13 @@ static int fts5VocabBestIndexMethod( int iTermEq = -1; int iTermGe = -1; int iTermLe = -1; - int idxNum = 0; + int idxNum = (int)pInfo->colUsed; int nArg = 0; UNUSED_PARAM(pUnused); + assert( (pInfo->colUsed & FTS5_VOCAB_COLUSED_MASK)==pInfo->colUsed ); + for(i=0; inConstraint; i++){ struct sqlite3_index_constraint *p = &pInfo->aConstraint[i]; if( p->usable==0 ) continue; @@ -279,8 +309,19 @@ static int fts5VocabBestIndexMethod( } } - pInfo->idxNum = idxNum; + /* This virtual table always delivers results in ascending order of + ** the "term" column (column 0). So if the user has requested this + ** specifically - "ORDER BY term" or "ORDER BY term ASC" - set the + ** sqlite3_index_info.orderByConsumed flag to tell the core the results + ** are already in sorted order. */ + if( pInfo->nOrderBy==1 + && pInfo->aOrderBy[0].iColumn==0 + && pInfo->aOrderBy[0].desc==0 + ){ + pInfo->orderByConsumed = 1; + } + pInfo->idxNum = idxNum; return SQLITE_OK; } @@ -292,13 +333,18 @@ static int fts5VocabOpenMethod( sqlite3_vtab_cursor **ppCsr ){ Fts5VocabTable *pTab = (Fts5VocabTable*)pVTab; - Fts5Index *pIndex = 0; - Fts5Config *pConfig = 0; + Fts5Table *pFts5 = 0; Fts5VocabCursor *pCsr = 0; int rc = SQLITE_OK; sqlite3_stmt *pStmt = 0; char *zSql = 0; + if( pTab->bBusy ){ + pVTab->zErrMsg = sqlite3_mprintf( + "recursive definition for %s.%s", pTab->zFts5Db, pTab->zFts5Tbl + ); + return SQLITE_ERROR; + } zSql = sqlite3Fts5Mprintf(&rc, "SELECT t.%Q FROM %Q.%Q AS t WHERE t.%Q MATCH '*id'", pTab->zFts5Tbl, pTab->zFts5Db, pTab->zFts5Tbl, pTab->zFts5Tbl @@ -310,33 +356,38 @@ static int fts5VocabOpenMethod( assert( rc==SQLITE_OK || pStmt==0 ); if( rc==SQLITE_ERROR ) rc = SQLITE_OK; + pTab->bBusy = 1; if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){ i64 iId = sqlite3_column_int64(pStmt, 0); - pIndex = sqlite3Fts5IndexFromCsrid(pTab->pGlobal, iId, &pConfig); + pFts5 = sqlite3Fts5TableFromCsrid(pTab->pGlobal, iId); } + pTab->bBusy = 0; - if( rc==SQLITE_OK && pIndex==0 ){ - rc = sqlite3_finalize(pStmt); - pStmt = 0; - if( rc==SQLITE_OK ){ - pVTab->zErrMsg = sqlite3_mprintf( - "no such fts5 table: %s.%s", pTab->zFts5Db, pTab->zFts5Tbl - ); - rc = SQLITE_ERROR; + if( rc==SQLITE_OK ){ + if( pFts5==0 ){ + rc = sqlite3_finalize(pStmt); + pStmt = 0; + if( rc==SQLITE_OK ){ + pVTab->zErrMsg = sqlite3_mprintf( + "no such fts5 table: %s.%s", pTab->zFts5Db, pTab->zFts5Tbl + ); + rc = SQLITE_ERROR; + } + }else{ + rc = sqlite3Fts5FlushToDisk(pFts5); } } if( rc==SQLITE_OK ){ - int nByte = pConfig->nCol * sizeof(i64) * 2 + sizeof(Fts5VocabCursor); + i64 nByte = pFts5->pConfig->nCol * sizeof(i64)*2 + sizeof(Fts5VocabCursor); pCsr = (Fts5VocabCursor*)sqlite3Fts5MallocZero(&rc, nByte); } if( pCsr ){ - pCsr->pIndex = pIndex; + pCsr->pFts5 = pFts5; pCsr->pStmt = pStmt; - pCsr->pConfig = pConfig; pCsr->aCnt = (i64*)&pCsr[1]; - pCsr->aDoc = &pCsr->aCnt[pConfig->nCol]; + pCsr->aDoc = &pCsr->aCnt[pFts5->pConfig->nCol]; }else{ sqlite3_finalize(pStmt); } @@ -345,13 +396,27 @@ static int fts5VocabOpenMethod( return rc; } +/* +** Restore cursor pCsr to the state it was in immediately after being +** created by the xOpen() method. +*/ static void fts5VocabResetCursor(Fts5VocabCursor *pCsr){ + int nCol = pCsr->pFts5->pConfig->nCol; pCsr->rowid = 0; sqlite3Fts5IterClose(pCsr->pIter); + sqlite3Fts5StructureRelease(pCsr->pStruct); + pCsr->pStruct = 0; pCsr->pIter = 0; sqlite3_free(pCsr->zLeTerm); pCsr->nLeTerm = -1; pCsr->zLeTerm = 0; + pCsr->bEof = 0; + pCsr->iCol = 0; + pCsr->iInstPos = 0; + pCsr->iInstOff = 0; + pCsr->colUsed = 0; + memset(pCsr->aCnt, 0, sizeof(i64)*nCol); + memset(pCsr->aDoc, 0, sizeof(i64)*nCol); } /* @@ -367,6 +432,56 @@ static int fts5VocabCloseMethod(sqlite3_vtab_cursor *pCursor){ return SQLITE_OK; } +static int fts5VocabInstanceNewTerm(Fts5VocabCursor *pCsr){ + int rc = SQLITE_OK; + + if( sqlite3Fts5IterEof(pCsr->pIter) ){ + pCsr->bEof = 1; + }else{ + const char *zTerm; + int nTerm; + zTerm = sqlite3Fts5IterTerm(pCsr->pIter, &nTerm); + if( pCsr->nLeTerm>=0 ){ + int nCmp = MIN(nTerm, pCsr->nLeTerm); + int bCmp = memcmp(pCsr->zLeTerm, zTerm, nCmp); + if( bCmp<0 || (bCmp==0 && pCsr->nLeTermbEof = 1; + } + } + + sqlite3Fts5BufferSet(&rc, &pCsr->term, nTerm, (const u8*)zTerm); + } + return rc; +} + +static int fts5VocabInstanceNext(Fts5VocabCursor *pCsr){ + int eDetail = pCsr->pFts5->pConfig->eDetail; + int rc = SQLITE_OK; + Fts5IndexIter *pIter = pCsr->pIter; + i64 *pp = &pCsr->iInstPos; + int *po = &pCsr->iInstOff; + + assert( sqlite3Fts5IterEof(pIter)==0 ); + assert( pCsr->bEof==0 ); + while( eDetail==FTS5_DETAIL_NONE + || sqlite3Fts5PoslistNext64(pIter->pData, pIter->nData, po, pp) + ){ + pCsr->iInstPos = 0; + pCsr->iInstOff = 0; + + rc = sqlite3Fts5IterNextScan(pCsr->pIter); + if( rc==SQLITE_OK ){ + rc = fts5VocabInstanceNewTerm(pCsr); + if( pCsr->bEof || eDetail==FTS5_DETAIL_NONE ) break; + } + if( rc ){ + pCsr->bEof = 1; + break; + } + } + + return rc; +} /* ** Advance the cursor to the next row in the table. @@ -374,18 +489,24 @@ static int fts5VocabCloseMethod(sqlite3_vtab_cursor *pCursor){ static int fts5VocabNextMethod(sqlite3_vtab_cursor *pCursor){ Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor; Fts5VocabTable *pTab = (Fts5VocabTable*)pCursor->pVtab; - int rc = SQLITE_OK; - int nCol = pCsr->pConfig->nCol; + int nCol = pCsr->pFts5->pConfig->nCol; + int rc; + rc = sqlite3Fts5StructureTest(pCsr->pFts5->pIndex, pCsr->pStruct); + if( rc!=SQLITE_OK ) return rc; pCsr->rowid++; + if( pTab->eType==FTS5_VOCAB_INSTANCE ){ + return fts5VocabInstanceNext(pCsr); + } + if( pTab->eType==FTS5_VOCAB_COL ){ for(pCsr->iCol++; pCsr->iColiCol++){ if( pCsr->aDoc[pCsr->iCol] ) break; } } - if( pTab->eType==FTS5_VOCAB_ROW || pCsr->iCol>=nCol ){ + if( pTab->eType!=FTS5_VOCAB_COL || pCsr->iCol>=nCol ){ if( sqlite3Fts5IterEof(pCsr->pIter) ){ pCsr->bEof = 1; }else{ @@ -393,6 +514,7 @@ static int fts5VocabNextMethod(sqlite3_vtab_cursor *pCursor){ int nTerm; zTerm = sqlite3Fts5IterTerm(pCsr->pIter, &nTerm); + assert( nTerm>=0 ); if( pCsr->nLeTerm>=0 ){ int nCmp = MIN(nTerm, pCsr->nLeTerm); int bCmp = memcmp(pCsr->zLeTerm, zTerm, nCmp); @@ -409,26 +531,39 @@ static int fts5VocabNextMethod(sqlite3_vtab_cursor *pCursor){ assert( pTab->eType==FTS5_VOCAB_COL || pTab->eType==FTS5_VOCAB_ROW ); while( rc==SQLITE_OK ){ + int eDetail = pCsr->pFts5->pConfig->eDetail; const u8 *pPos; int nPos; /* Position list */ i64 iPos = 0; /* 64-bit position read from poslist */ int iOff = 0; /* Current offset within position list */ pPos = pCsr->pIter->pData; nPos = pCsr->pIter->nData; - switch( pCsr->pConfig->eDetail ){ - case FTS5_DETAIL_FULL: - pPos = pCsr->pIter->pData; - nPos = pCsr->pIter->nData; - if( pTab->eType==FTS5_VOCAB_ROW ){ - while( 0==sqlite3Fts5PoslistNext64(pPos, nPos, &iOff, &iPos) ){ - pCsr->aCnt[0]++; + + switch( pTab->eType ){ + case FTS5_VOCAB_ROW: + /* Do not bother counting the number of instances if the "cnt" + ** column is not being read (according to colUsed). */ + if( eDetail==FTS5_DETAIL_FULL && (pCsr->colUsed & 0x04) ){ + while( iPosaCnt[] */ + pCsr->aCnt[0]++; + } } - pCsr->aDoc[0]++; - }else{ + } + pCsr->aDoc[0]++; + break; + + case FTS5_VOCAB_COL: + if( eDetail==FTS5_DETAIL_FULL ){ int iCol = -1; while( 0==sqlite3Fts5PoslistNext64(pPos, nPos, &iOff, &iPos) ){ int ii = FTS5_POS2COLUMN(iPos); - pCsr->aCnt[ii]++; if( iCol!=ii ){ if( ii>=nCol ){ rc = FTS5_CORRUPT; @@ -437,14 +572,9 @@ static int fts5VocabNextMethod(sqlite3_vtab_cursor *pCursor){ pCsr->aDoc[ii]++; iCol = ii; } + pCsr->aCnt[ii]++; } - } - break; - - case FTS5_DETAIL_COLUMNS: - if( pTab->eType==FTS5_VOCAB_ROW ){ - pCsr->aDoc[0]++; - }else{ + }else if( eDetail==FTS5_DETAIL_COLUMNS ){ while( 0==sqlite3Fts5PoslistNext64(pPos, nPos, &iOff,&iPos) ){ assert_nc( iPos>=0 && iPos=nCol ){ @@ -453,22 +583,27 @@ static int fts5VocabNextMethod(sqlite3_vtab_cursor *pCursor){ } pCsr->aDoc[iPos]++; } + }else{ + assert( eDetail==FTS5_DETAIL_NONE ); + pCsr->aDoc[0]++; } break; - default: - assert( pCsr->pConfig->eDetail==FTS5_DETAIL_NONE ); - pCsr->aDoc[0]++; + default: + assert( pTab->eType==FTS5_VOCAB_INSTANCE ); break; } if( rc==SQLITE_OK ){ rc = sqlite3Fts5IterNextScan(pCsr->pIter); } + if( pTab->eType==FTS5_VOCAB_INSTANCE ) break; if( rc==SQLITE_OK ){ zTerm = sqlite3Fts5IterTerm(pCsr->pIter, &nTerm); - if( nTerm!=pCsr->term.n || memcmp(zTerm, pCsr->term.p, nTerm) ){ + if( nTerm!=pCsr->term.n + || (nTerm>0 && memcmp(zTerm, pCsr->term.p, nTerm)) + ){ break; } if( sqlite3Fts5IterEof(pCsr->pIter) ) break; @@ -478,8 +613,10 @@ static int fts5VocabNextMethod(sqlite3_vtab_cursor *pCursor){ } if( rc==SQLITE_OK && pCsr->bEof==0 && pTab->eType==FTS5_VOCAB_COL ){ - while( pCsr->aDoc[pCsr->iCol]==0 ) pCsr->iCol++; - assert( pCsr->iColpConfig->nCol ); + for(/* noop */; pCsr->iColaDoc[pCsr->iCol]==0; pCsr->iCol++); + if( pCsr->iCol==nCol ){ + rc = FTS5_CORRUPT; + } } return rc; } @@ -494,7 +631,9 @@ static int fts5VocabFilterMethod( int nUnused, /* Number of elements in apVal */ sqlite3_value **apVal /* Arguments for the indexing scheme */ ){ + Fts5VocabTable *pTab = (Fts5VocabTable*)pCursor->pVtab; Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor; + int eType = pTab->eType; int rc = SQLITE_OK; int iVal = 0; @@ -512,11 +651,12 @@ static int fts5VocabFilterMethod( if( idxNum & FTS5_VOCAB_TERM_EQ ) pEq = apVal[iVal++]; if( idxNum & FTS5_VOCAB_TERM_GE ) pGe = apVal[iVal++]; if( idxNum & FTS5_VOCAB_TERM_LE ) pLe = apVal[iVal++]; + pCsr->colUsed = (idxNum & FTS5_VOCAB_COLUSED_MASK); if( pEq ){ zTerm = (const char *)sqlite3_value_text(pEq); nTerm = sqlite3_value_bytes(pEq); - f = 0; + f = FTS5INDEX_QUERY_NOTOKENDATA; }else{ if( pGe ){ zTerm = (const char *)sqlite3_value_text(pGe); @@ -524,6 +664,7 @@ static int fts5VocabFilterMethod( } if( pLe ){ const char *zCopy = (const char *)sqlite3_value_text(pLe); + if( zCopy==0 ) zCopy = ""; pCsr->nLeTerm = sqlite3_value_bytes(pLe); pCsr->zLeTerm = sqlite3_malloc(pCsr->nLeTerm+1); if( pCsr->zLeTerm==0 ){ @@ -534,11 +675,20 @@ static int fts5VocabFilterMethod( } } - if( rc==SQLITE_OK ){ - rc = sqlite3Fts5IndexQuery(pCsr->pIndex, zTerm, nTerm, f, 0, &pCsr->pIter); + Fts5Index *pIndex = pCsr->pFts5->pIndex; + rc = sqlite3Fts5IndexQuery(pIndex, zTerm, nTerm, f, 0, &pCsr->pIter); + if( rc==SQLITE_OK ){ + pCsr->pStruct = sqlite3Fts5StructureRef(pIndex); + } } - if( rc==SQLITE_OK ){ + if( rc==SQLITE_OK && eType==FTS5_VOCAB_INSTANCE ){ + rc = fts5VocabInstanceNewTerm(pCsr); + } + if( rc==SQLITE_OK && !pCsr->bEof + && (eType!=FTS5_VOCAB_INSTANCE + || pCsr->pFts5->pConfig->eDetail!=FTS5_DETAIL_NONE) + ){ rc = fts5VocabNextMethod(pCursor); } @@ -560,7 +710,7 @@ static int fts5VocabColumnMethod( int iCol /* Index of column to read value from */ ){ Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor; - int eDetail = pCsr->pConfig->eDetail; + int eDetail = pCsr->pFts5->pConfig->eDetail; int eType = ((Fts5VocabTable*)(pCursor->pVtab))->eType; i64 iVal = 0; @@ -572,7 +722,7 @@ static int fts5VocabColumnMethod( assert( iCol==1 || iCol==2 || iCol==3 ); if( iCol==1 ){ if( eDetail!=FTS5_DETAIL_NONE ){ - const char *z = pCsr->pConfig->azCol[pCsr->iCol]; + const char *z = pCsr->pFts5->pConfig->azCol[pCsr->iCol]; sqlite3_result_text(pCtx, z, -1, SQLITE_STATIC); } }else if( iCol==2 ){ @@ -580,13 +730,41 @@ static int fts5VocabColumnMethod( }else{ iVal = pCsr->aCnt[pCsr->iCol]; } - }else{ + }else if( eType==FTS5_VOCAB_ROW ){ assert( iCol==1 || iCol==2 ); if( iCol==1 ){ iVal = pCsr->aDoc[0]; }else{ iVal = pCsr->aCnt[0]; } + }else{ + assert( eType==FTS5_VOCAB_INSTANCE ); + switch( iCol ){ + case 1: + sqlite3_result_int64(pCtx, pCsr->pIter->iRowid); + break; + case 2: { + int ii = -1; + if( eDetail==FTS5_DETAIL_FULL ){ + ii = FTS5_POS2COLUMN(pCsr->iInstPos); + }else if( eDetail==FTS5_DETAIL_COLUMNS ){ + ii = (int)pCsr->iInstPos; + } + if( ii>=0 && iipFts5->pConfig->nCol ){ + const char *z = pCsr->pFts5->pConfig->azCol[ii]; + sqlite3_result_text(pCtx, z, -1, SQLITE_STATIC); + } + break; + } + default: { + assert( iCol==3 ); + if( eDetail==FTS5_DETAIL_FULL ){ + int ii = FTS5_POS2OFFSET(pCsr->iInstPos); + sqlite3_result_int(pCtx, ii); + } + break; + } + } } if( iVal>0 ) sqlite3_result_int64(pCtx, iVal); @@ -632,10 +810,10 @@ int sqlite3Fts5VocabInit(Fts5Global *pGlobal, sqlite3 *db){ /* xSavepoint */ 0, /* xRelease */ 0, /* xRollbackTo */ 0, + /* xShadowName */ 0, + /* xIntegrity */ 0 }; void *p = (void*)pGlobal; return sqlite3_create_module_v2(db, "fts5vocab", &fts5Vocab, p, 0); } - - diff --git a/ext/fts5/fts5parse.y b/ext/fts5/fts5parse.y index 43936767b7..134125db1f 100644 --- a/ext/fts5/fts5parse.y +++ b/ext/fts5/fts5parse.y @@ -34,7 +34,6 @@ ); } %stack_overflow { - UNUSED_PARAM(yypMinor); /* Silence a compiler warning */ sqlite3Fts5ParseError(pParse, "fts5: parser stack overflow"); } @@ -90,6 +89,29 @@ input ::= expr(X). { sqlite3Fts5ParseFinished(pParse, X); } %destructor expr { sqlite3Fts5ParseNodeFree($$); } %destructor exprlist { sqlite3Fts5ParseNodeFree($$); } +%type colset {Fts5Colset*} +%destructor colset { sqlite3_free($$); } +%type colsetlist {Fts5Colset*} +%destructor colsetlist { sqlite3_free($$); } + +colset(A) ::= MINUS LCP colsetlist(X) RCP. { + A = sqlite3Fts5ParseColsetInvert(pParse, X); +} +colset(A) ::= LCP colsetlist(X) RCP. { A = X; } +colset(A) ::= STRING(X). { + A = sqlite3Fts5ParseColset(pParse, 0, &X); +} +colset(A) ::= MINUS STRING(X). { + A = sqlite3Fts5ParseColset(pParse, 0, &X); + A = sqlite3Fts5ParseColsetInvert(pParse, A); +} + +colsetlist(A) ::= colsetlist(Y) STRING(X). { + A = sqlite3Fts5ParseColset(pParse, Y, &X); } +colsetlist(A) ::= STRING(X). { + A = sqlite3Fts5ParseColset(pParse, 0, &X); +} + expr(A) ::= expr(X) AND expr(Y). { A = sqlite3Fts5ParseNode(pParse, FTS5_AND, X, Y, 0); } @@ -100,36 +122,24 @@ expr(A) ::= expr(X) NOT expr(Y). { A = sqlite3Fts5ParseNode(pParse, FTS5_NOT, X, Y, 0); } +expr(A) ::= colset(X) COLON LP expr(Y) RP. { + sqlite3Fts5ParseSetColset(pParse, Y, X); + A = Y; +} expr(A) ::= LP expr(X) RP. {A = X;} expr(A) ::= exprlist(X). {A = X;} exprlist(A) ::= cnearset(X). {A = X;} exprlist(A) ::= exprlist(X) cnearset(Y). { - A = sqlite3Fts5ParseNode(pParse, FTS5_AND, X, Y, 0); + A = sqlite3Fts5ParseImplicitAnd(pParse, X, Y); } cnearset(A) ::= nearset(X). { A = sqlite3Fts5ParseNode(pParse, FTS5_STRING, 0, 0, X); } cnearset(A) ::= colset(X) COLON nearset(Y). { - sqlite3Fts5ParseSetColset(pParse, Y, X); A = sqlite3Fts5ParseNode(pParse, FTS5_STRING, 0, 0, Y); -} - -%type colset {Fts5Colset*} -%destructor colset { sqlite3_free($$); } -%type colsetlist {Fts5Colset*} -%destructor colsetlist { sqlite3_free($$); } - -colset(A) ::= LCP colsetlist(X) RCP. { A = X; } -colset(A) ::= STRING(X). { - A = sqlite3Fts5ParseColset(pParse, 0, &X); -} - -colsetlist(A) ::= colsetlist(Y) STRING(X). { - A = sqlite3Fts5ParseColset(pParse, Y, &X); } -colsetlist(A) ::= STRING(X). { - A = sqlite3Fts5ParseColset(pParse, 0, &X); + sqlite3Fts5ParseSetColset(pParse, A, X); } @@ -138,7 +148,11 @@ colsetlist(A) ::= STRING(X). { %destructor nearset { sqlite3Fts5ParseNearsetFree($$); } %destructor nearphrases { sqlite3Fts5ParseNearsetFree($$); } -nearset(A) ::= phrase(X). { A = sqlite3Fts5ParseNearset(pParse, 0, X); } +nearset(A) ::= phrase(Y). { A = sqlite3Fts5ParseNearset(pParse, 0, Y); } +nearset(A) ::= CARET phrase(Y). { + sqlite3Fts5ParseSetCaret(Y); + A = sqlite3Fts5ParseNearset(pParse, 0, Y); +} nearset(A) ::= STRING(X) LP nearphrases(Y) neardist_opt(Z) RP. { sqlite3Fts5ParseNear(pParse, &X); sqlite3Fts5ParseSetDistance(pParse, Y, &Z); @@ -179,6 +193,5 @@ phrase(A) ::= STRING(Y) star_opt(Z). { ** Optional "*" character. */ %type star_opt {int} - star_opt(A) ::= STAR. { A = 1; } star_opt(A) ::= . { A = 0; } diff --git a/ext/fts5/test/fts5_common.tcl b/ext/fts5/test/fts5_common.tcl index cf688dcc0d..8ea87dbdd1 100644 --- a/ext/fts5/test/fts5_common.tcl +++ b/ext/fts5/test/fts5_common.tcl @@ -16,8 +16,13 @@ if {![info exists testdir]} { source $testdir/tester.tcl ifcapable !fts5 { - finish_test + proc return_if_no_fts5 {} { + finish_test + return -code return + } return +} else { + proc return_if_no_fts5 {} {} } catch { @@ -25,12 +30,6 @@ catch { reset_db } -# If SQLITE_ENABLE_FTS5 is not defined, skip this test. -ifcapable !fts5 { - finish_test - return -} - proc fts5_test_poslist {cmd} { set res [list] for {set i 0} {$i < [$cmd xInstCount]} {incr i} { @@ -52,6 +51,10 @@ proc fts5_test_poslist2 {cmd} { sort_poslist $res } +proc fts5_test_insttoken {cmd iInst iToken} { + $cmd xInstToken $iInst $iToken +} + proc fts5_test_collist {cmd} { set res [list] @@ -62,6 +65,12 @@ proc fts5_test_collist {cmd} { set res } +proc fts5_collist {cmd iPhrase} { + set res [list] + $cmd xPhraseColumnForeach $iPhrase c { lappend res $c } + set res +} + proc fts5_test_columnsize {cmd} { set res [list] for {set i 0} {$i < [$cmd xColumnCount]} {incr i} { @@ -70,6 +79,13 @@ proc fts5_test_columnsize {cmd} { set res } +proc fts5_columntext {cmd iCol} { + $cmd xColumnText $iCol +} +proc fts5_columnlocale {cmd iCol} { + $cmd xColumnLocale $iCol +} + proc fts5_test_columntext {cmd} { set res [list] for {set i 0} {$i < [$cmd xColumnCount]} {incr i} { @@ -78,6 +94,14 @@ proc fts5_test_columntext {cmd} { set res } +proc fts5_test_columnlocale {cmd} { + set res [list] + for {set i 0} {$i < [$cmd xColumnCount]} {incr i} { + lappend res [$cmd xColumnLocale $i] + } + set res +} + proc fts5_test_columntotalsize {cmd} { set res [list] for {set i 0} {$i < [$cmd xColumnCount]} {incr i} { @@ -105,6 +129,10 @@ proc fts5_test_rowcount {cmd} { $cmd xRowCount } +proc fts5_test_rowid {cmd} { + $cmd xRowid +} + proc test_queryphrase_cb {cnt cmd} { upvar $cnt L for {set i 0} {$i < [$cmd xInstCount]} {incr i} { @@ -126,6 +154,13 @@ proc fts5_test_queryphrase {cmd} { set res } +proc fts5_queryphrase {cmd iPhrase} { + set cnt [list] + for {set j 0} {$j < [$cmd xColumnCount]} {incr j} { lappend cnt 0 } + $cmd xQueryPhrase $iPhrase [list test_queryphrase_cb cnt] + set cnt +} + proc fts5_test_phrasecount {cmd} { $cmd xPhraseCount } @@ -145,21 +180,34 @@ proc fts5_aux_test_functions {db} { foreach f { fts5_test_columnsize fts5_test_columntext + fts5_test_columnlocale fts5_test_columntotalsize fts5_test_poslist fts5_test_poslist2 fts5_test_collist + fts5_test_insttoken fts5_test_tokenize fts5_test_rowcount + fts5_test_rowid fts5_test_all fts5_test_queryphrase fts5_test_phrasecount + fts5_columntext + fts5_columnlocale + fts5_queryphrase + fts5_collist } { sqlite3_fts5_create_function $db $f $f } } +proc fts5_segcount {tbl} { + set N 0 + foreach n [fts5_level_segs $tbl] { incr N $n } + set N +} + proc fts5_level_segs {tbl} { set sql "SELECT fts5_decode(rowid,block) aS r FROM ${tbl}_data WHERE rowid=10" set ret [list] @@ -433,6 +481,20 @@ proc detail_is_none {} { detail_check ; expr {$::detail == "none"} } proc detail_is_col {} { detail_check ; expr {$::detail == "col" } } proc detail_is_full {} { detail_check ; expr {$::detail == "full"} } +proc foreach_tokenizer_mode {prefix script} { + set saved $::testprefix + foreach {d mapping} { + "" {} + "-origintext" {, tokenize="origintext unicode61", tokendata=1} + } { + set s [string map [list %TOKENIZER% $mapping] $script] + set ::testprefix "$prefix$d" + reset_db + sqlite3_fts5_register_origintext db + uplevel $s + } + set ::testprefix $saved +} #------------------------------------------------------------------------- # Convert a poslist of the type returned by fts5_test_poslist() to a @@ -589,6 +651,10 @@ proc nearset_rc {aCol args} { list } +proc dump {tname} { + execsql_pp "SELECT * FROM ${tname}_idx" + execsql_pp "SELECT id, quote(block), fts5_decode(id,block) FROM ${tname}_data" +} #------------------------------------------------------------------------- # Code for a simple Tcl tokenizer that supports synonyms at query time. diff --git a/ext/fts5/test/fts5aa.test b/ext/fts5/test/fts5aa.test index fdcf08398d..184cb77b84 100644 --- a/ext/fts5/test/fts5aa.test +++ b/ext/fts5/test/fts5aa.test @@ -22,6 +22,7 @@ ifcapable !fts5 { } foreach_detail_mode $::testprefix { +foreach_tokenizer_mode $::testprefix { do_execsql_test 1.0 { CREATE VIRTUAL TABLE t1 USING fts5(a, b, c); @@ -38,14 +39,13 @@ do_execsql_test 1.0 { do_execsql_test 1.1 { DROP TABLE t1; SELECT name, sql FROM sqlite_master; -} { -} +} {} #------------------------------------------------------------------------- # do_execsql_test 2.0 { - CREATE VIRTUAL TABLE t1 USING fts5(x, y, detail=%DETAIL%); + CREATE VIRTUAL TABLE t1 USING fts5(x, y, detail=%DETAIL% %TOKENIZER%); } do_execsql_test 2.1 { INSERT INTO t1 VALUES('a b c', 'd e f'); @@ -66,14 +66,17 @@ foreach w {a b c d e f} { do_execsql_test 2.4 { INSERT INTO t1(t1) VALUES('integrity-check'); -} + PRAGMA integrity_check; + PRAGMA integrity_check(t1); +} {ok ok} #------------------------------------------------------------------------- # reset_db +sqlite3_fts5_register_origintext db do_execsql_test 3.0 { - CREATE VIRTUAL TABLE t1 USING fts5(x,y, detail=%DETAIL%); + CREATE VIRTUAL TABLE t1 USING fts5(x,y, detail=%DETAIL% %TOKENIZER%); } foreach {i x y} { 1 {g f d b f} {h h e i a} @@ -89,14 +92,16 @@ foreach {i x y} { } { do_execsql_test 3.$i.1 { INSERT INTO t1 VALUES($x, $y) } do_execsql_test 3.$i.2 { INSERT INTO t1(t1) VALUES('integrity-check') } + do_execsql_test 3.$i.3 { PRAGMA integrity_check(t1) } ok if {[set_test_counter errors]} break } #------------------------------------------------------------------------- # reset_db +sqlite3_fts5_register_origintext db do_execsql_test 4.0 { - CREATE VIRTUAL TABLE t1 USING fts5(x,y, detail=%DETAIL%); + CREATE VIRTUAL TABLE t1 USING fts5(x,y, detail=%DETAIL% %TOKENIZER%); INSERT INTO t1(t1, rank) VALUES('pgsz', 32); } foreach {i x y} { @@ -119,8 +124,9 @@ foreach {i x y} { #------------------------------------------------------------------------- # reset_db +sqlite3_fts5_register_origintext db do_execsql_test 5.0 { - CREATE VIRTUAL TABLE t1 USING fts5(x,y, detail=%DETAIL%); + CREATE VIRTUAL TABLE t1 USING fts5(x,y, detail=%DETAIL% %TOKENIZER%); INSERT INTO t1(t1, rank) VALUES('pgsz', 32); } foreach {i x y} { @@ -136,15 +142,16 @@ foreach {i x y} { 10 {ddd abcde dddd dd c} {dddd c c d abcde} } { do_execsql_test 5.$i.1 { INSERT INTO t1 VALUES($x, $y) } - do_execsql_test 5.$i.2 { INSERT INTO t1(t1) VALUES('integrity-check') } + do_execsql_test 5.$i.2 { PRAGMA integrity_check(t1) } ok if {[set_test_counter errors]} break } #------------------------------------------------------------------------- # reset_db +sqlite3_fts5_register_origintext db do_execsql_test 6.0 { - CREATE VIRTUAL TABLE t1 USING fts5(x,y, detail=%DETAIL%); + CREATE VIRTUAL TABLE t1 USING fts5(x,y, detail=%DETAIL% %TOKENIZER%); INSERT INTO t1(t1, rank) VALUES('pgsz', 32); } @@ -179,6 +186,7 @@ do_execsql_test 6.6 { #------------------------------------------------------------------------- # reset_db +sqlite3_fts5_register_origintext db expr srand(0) do_execsql_test 7.0 { CREATE VIRTUAL TABLE t1 USING fts5(x,y,z); @@ -220,6 +228,7 @@ for {set i 1} {$i <= 10} {incr i} { #------------------------------------------------------------------------- # reset_db +sqlite3_fts5_register_origintext db do_execsql_test 8.0 { CREATE VIRTUAL TABLE t1 USING fts5(x, prefix="1,2,3"); INSERT INTO t1(t1, rank) VALUES('pgsz', 32); @@ -234,6 +243,7 @@ do_execsql_test 8.1 { #------------------------------------------------------------------------- # reset_db +sqlite3_fts5_register_origintext db expr srand(0) @@ -278,8 +288,9 @@ for {set i 1} {$i <= 10} {incr i} { #------------------------------------------------------------------------- # reset_db +sqlite3_fts5_register_origintext db do_execsql_test 10.0 { - CREATE VIRTUAL TABLE t1 USING fts5(x,y, detail=%DETAIL%); + CREATE VIRTUAL TABLE t1 USING fts5(x,y, detail=%DETAIL% %TOKENIZER%); } set d10 { 1 {g f d b f} {h h e i a} @@ -312,19 +323,19 @@ do_execsql_test 10.4.2 { INSERT INTO t1(t1) VALUES('integrity-check') } #------------------------------------------------------------------------- # do_catchsql_test 11.1 { - CREATE VIRTUAL TABLE t2 USING fts5(a, b, c, rank, detail=%DETAIL%); + CREATE VIRTUAL TABLE t2 USING fts5(a, b, c, rank, detail=%DETAIL% %TOKENIZER%); } {1 {reserved fts5 column name: rank}} do_catchsql_test 11.2 { - CREATE VIRTUAL TABLE rank USING fts5(a, b, c, detail=%DETAIL%); + CREATE VIRTUAL TABLE rank USING fts5(a, b, c, detail=%DETAIL% %TOKENIZER%); } {1 {reserved fts5 table name: rank}} do_catchsql_test 11.3 { - CREATE VIRTUAL TABLE t2 USING fts5(a, b, c, rowid, detail=%DETAIL%); + CREATE VIRTUAL TABLE t2 USING fts5(a, b, c, rowid, detail=%DETAIL% %TOKENIZER%); } {1 {reserved fts5 column name: rowid}} #------------------------------------------------------------------------- # do_execsql_test 12.1 { - CREATE VIRTUAL TABLE t2 USING fts5(x,y, detail=%DETAIL%); + CREATE VIRTUAL TABLE t2 USING fts5(x,y, detail=%DETAIL% %TOKENIZER%); } {} do_catchsql_test 12.2 { @@ -339,8 +350,9 @@ do_test 12.3 { #------------------------------------------------------------------------- # reset_db +sqlite3_fts5_register_origintext db do_execsql_test 13.1 { - CREATE VIRTUAL TABLE t1 USING fts5(x, detail=%DETAIL%); + CREATE VIRTUAL TABLE t1 USING fts5(x, detail=%DETAIL% %TOKENIZER%); INSERT INTO t1(rowid, x) VALUES(1, 'o n e'), (2, 't w o'); } {} @@ -363,8 +375,9 @@ do_execsql_test 13.6 { #------------------------------------------------------------------------- # reset_db +sqlite3_fts5_register_origintext db do_execsql_test 14.1 { - CREATE VIRTUAL TABLE t1 USING fts5(x, y, detail=%DETAIL%); + CREATE VIRTUAL TABLE t1 USING fts5(x, y, detail=%DETAIL% %TOKENIZER%); INSERT INTO t1(t1, rank) VALUES('pgsz', 32); WITH d(x,y) AS ( SELECT NULL, 'xyz xyz xyz xyz xyz xyz' @@ -409,12 +422,13 @@ do_test 14.3 { do_execsql_test 15.0 { INSERT INTO t1(t1) VALUES('integrity-check'); } +sqlite3_db_config db DEFENSIVE 0 do_execsql_test 15.1 { UPDATE t1_content SET c1 = 'xyz xyz xyz xyz xyz abc' WHERE rowid = 1; } do_catchsql_test 15.2 { INSERT INTO t1(t1) VALUES('integrity-check'); -} {1 {database disk image is malformed}} +} {1 {fts5: checksum mismatch for table "t1"}} #------------------------------------------------------------------------- # @@ -426,21 +440,29 @@ do_execsql_test 16.1 { proc funk {} { db eval { UPDATE n1_config SET v=50 WHERE k='version' } set fd [db incrblob main n1_data block 10] - fconfigure $fd -encoding binary -translation binary - puts -nonewline $fd "\x44\x45" + fconfigure $fd -translation binary +# puts -nonewline $fd "\x44\x45" close $fd } db func funk funk +# This test case corrupts the structure record within the first invocation +# of function funk(). Which used to cause the bm25() function to throw an +# exception. But since bm25() can now used the cached structure record, +# it never sees the corruption introduced by funk() and so the following +# statement no longer fails. +# do_catchsql_test 16.2 { - SELECT funk(), bm25(n1), funk() FROM n1 WHERE n1 MATCH 'a+b+c+d' -} {1 {SQL logic error or missing database}} + SELECT funk(), format('%g',bm25(n1)), funk() FROM n1 WHERE n1 MATCH 'a+b+c+d' +} {0 {{} -1e-06 {}}} +# {1 {SQL logic error}} #------------------------------------------------------------------------- # reset_db +sqlite3_fts5_register_origintext db do_execsql_test 17.1 { - CREATE VIRTUAL TABLE b2 USING fts5(x, detail=%DETAIL%); + CREATE VIRTUAL TABLE b2 USING fts5(x, detail=%DETAIL% %TOKENIZER%); INSERT INTO b2 VALUES('a'); INSERT INTO b2 VALUES('b'); INSERT INTO b2 VALUES('c'); @@ -456,8 +478,9 @@ do_test 17.2 { if {[string match n* %DETAIL%]==0} { reset_db + sqlite3_fts5_register_origintext db do_execsql_test 17.3 { - CREATE VIRTUAL TABLE c2 USING fts5(x, y, detail=%DETAIL%); + CREATE VIRTUAL TABLE c2 USING fts5(x, y, detail=%DETAIL% %TOKENIZER%); INSERT INTO c2 VALUES('x x x', 'x x x'); SELECT rowid FROM c2 WHERE c2 MATCH 'y:x'; } {1} @@ -466,8 +489,9 @@ if {[string match n* %DETAIL%]==0} { #------------------------------------------------------------------------- # reset_db +sqlite3_fts5_register_origintext db do_execsql_test 17.1 { - CREATE VIRTUAL TABLE uio USING fts5(ttt, detail=%DETAIL%); + CREATE VIRTUAL TABLE uio USING fts5(ttt, detail=%DETAIL% %TOKENIZER%); INSERT INTO uio VALUES(NULL); INSERT INTO uio SELECT NULL FROM uio; INSERT INTO uio SELECT NULL FROM uio; @@ -514,8 +538,8 @@ do_execsql_test 17.9 { #-------------------------------------------------------------------- # do_execsql_test 18.1 { - CREATE VIRTUAL TABLE t1 USING fts5(a, b, detail=%DETAIL%); - CREATE VIRTUAL TABLE t2 USING fts5(c, d, detail=%DETAIL%); + CREATE VIRTUAL TABLE t1 USING fts5(a, b, detail=%DETAIL% %TOKENIZER%); + CREATE VIRTUAL TABLE t2 USING fts5(c, d, detail=%DETAIL% %TOKENIZER%); INSERT INTO t1 VALUES('abc*', NULL); INSERT INTO t2 VALUES(1, 'abcdefg'); } @@ -530,8 +554,9 @@ do_execsql_test 18.3 { # fts5 table in the temp schema. # reset_db +sqlite3_fts5_register_origintext db do_execsql_test 19.0 { - CREATE VIRTUAL TABLE temp.t1 USING fts5(x, detail=%DETAIL%); + CREATE VIRTUAL TABLE temp.t1 USING fts5(x, detail=%DETAIL% %TOKENIZER%); INSERT INTO t1 VALUES('x y z'); INSERT INTO t1 VALUES('w x 1'); SELECT rowid FROM t1 WHERE t1 MATCH 'x'; @@ -541,8 +566,9 @@ do_execsql_test 19.0 { # Test that 6 and 7 byte varints can be read. # reset_db +sqlite3_fts5_register_origintext db do_execsql_test 20.0 { - CREATE VIRTUAL TABLE temp.tmp USING fts5(x, detail=%DETAIL%); + CREATE VIRTUAL TABLE temp.tmp USING fts5(x, detail=%DETAIL% %TOKENIZER%); } set ::ids [list \ 0 [expr 1<<36] [expr 2<<36] [expr 1<<43] [expr 2<<43] @@ -554,9 +580,85 @@ do_test 20.1 { execsql { SELECT rowid FROM tmp WHERE tmp MATCH 'y' } } $::ids +#-------------------------------------------------------------------- +# Test that a DROP TABLE may be executed within a transaction that +# writes to an FTS5 table. +# +do_execsql_test 21.0 { + CREATE TEMP TABLE t8(a, b); + CREATE VIRTUAL TABLE ft USING fts5(x, detail=%DETAIL% %TOKENIZER%); +} + +do_execsql_test 21.1 { + BEGIN; + INSERT INTO ft VALUES('a b c'); + DROP TABLE t8; + COMMIT; } +do_execsql_test 22.0 { + CREATE VIRTUAL TABLE t9 USING fts5(x, detail=%DETAIL% %TOKENIZER%); + INSERT INTO t9(rowid, x) VALUES(2, 'bbb'); + BEGIN; + INSERT INTO t9(rowid, x) VALUES(1, 'aaa'); + DELETE FROM t9 WHERE rowid = 2; + INSERT INTO t9(rowid, x) VALUES(3, 'bbb'); + COMMIT; +} -finish_test +do_execsql_test 22.1 { + SELECT rowid FROM t9('a*') +} {1} + +#------------------------------------------------------------------------- +do_execsql_test 23.0 { + CREATE VIRTUAL TABLE t10 USING fts5(x, detail=%DETAIL% %TOKENIZER%); + CREATE TABLE t11(x); +} +do_execsql_test 23.1 { + SELECT * FROM t11, t10 WHERE t11.x = t10.x AND t10.rowid IS NULL; +} +do_execsql_test 23.2 { + SELECT * FROM t11, t10 WHERE t10.rowid IS NULL; +} + +#------------------------------------------------------------------------- +do_execsql_test 24.0 { + CREATE VIRTUAL TABLE t12 USING fts5(x, detail=%DETAIL% %TOKENIZER%); + INSERT INTO t12 VALUES('aaaa'); +} +do_execsql_test 24.1 { + BEGIN; + DELETE FROM t12 WHERE rowid=1; + SELECT * FROM t12('aaaa'); + INSERT INTO t12 VALUES('aaaa'); + END; +} +execsql_pp { + SELECT rowid, hex(block) FROM t12_data +} +do_execsql_test 24.2 { + INSERT INTO t12(t12) VALUES('integrity-check'); +} +do_execsql_test 24.3 { + SELECT * FROM t12('aaaa'); +} {aaaa} + +#------------------------------------------------------------------------- +do_execsql_test 25.0 { + CREATE VIRTUAL TABLE t13 USING fts5(x, detail=%DETAIL% %TOKENIZER%); +} +do_execsql_test 25.1 { + BEGIN; + INSERT INTO t13 VALUES('AAAA'); +SELECT * FROM t13('BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB*'); + + END; +} +} +} + +expand_all_sql db +finish_test diff --git a/ext/fts5/test/fts5ab.test b/ext/fts5/test/fts5ab.test index 95da2cd2eb..a74c0f8884 100644 --- a/ext/fts5/test/fts5ab.test +++ b/ext/fts5/test/fts5ab.test @@ -16,7 +16,7 @@ source [file join [file dirname [info script]] fts5_common.tcl] set testprefix fts5ab -# If SQLITE_ENABLE_FTS5 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return @@ -180,7 +180,11 @@ if {[detail_is_full]} { } {1 2} } -do_execsql_test 4.5 { +do_execsql_test 4.5.1 { + SELECT rowid FROM s1 WHERE s1 MATCH 'a AND x' +} {1 2} + +do_execsql_test 4.5.2 { SELECT rowid FROM s1 WHERE s1 MATCH 'a x' } {1 2} @@ -290,8 +294,40 @@ do_execsql_test 7.0 { INSERT INTO x1 VALUES($doc); } +#------------------------------------------------------------------------- +# Forum post: https://sqlite.org/forum/forumpost/ea4d8c9acb +# +reset_db +do_execsql_test 8.0 { + PRAGMA encoding = 'UTF-16le'; + CREATE VIRTUAL TABLE vt0 USING fts5(c0); +} +set v [db one {SELECT x'2a12'}] +do_execsql_test 8.1 { + INSERT INTO vt0 VALUES ($v); +} +do_execsql_test 8.2 { + SELECT quote(c0) FROM vt0 +} {X'2A12'} +do_execsql_test 8.3 { + INSERT INTO vt0(vt0) VALUES('integrity-check'); +} {} +reset_db +do_execsql_test 8.4 { + PRAGMA encoding = 'UTF-16le'; + CREATE VIRTUAL TABLE vt0 USING fts5(c0); +} +do_execsql_test 8.5 { + INSERT INTO vt0 VALUES (x'2a12'); +} +do_execsql_test 8.6 { + SELECT quote(c0) FROM vt0 +} {X'2A12'} +do_execsql_test 8.7 { + INSERT INTO vt0(vt0) VALUES('integrity-check'); +} {} + } ;# foreach_detail_mode... finish_test - diff --git a/ext/fts5/test/fts5ac.test b/ext/fts5/test/fts5ac.test index 61b3230772..4628e909c1 100644 --- a/ext/fts5/test/fts5ac.test +++ b/ext/fts5/test/fts5ac.test @@ -16,7 +16,7 @@ source [file join [file dirname [info script]] fts5_common.tcl] set testprefix fts5ac -# If SQLITE_ENABLE_FTS5 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return @@ -276,4 +276,3 @@ foreach {tn expr tclexpr} { } finish_test - diff --git a/ext/fts5/test/fts5ad.test b/ext/fts5/test/fts5ad.test index 974aa781aa..27806a4c0c 100644 --- a/ext/fts5/test/fts5ad.test +++ b/ext/fts5/test/fts5ad.test @@ -18,7 +18,7 @@ source [file join [file dirname [info script]] fts5_common.tcl] set testprefix fts5ad -# If SQLITE_ENABLE_FTS5 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return @@ -231,7 +231,6 @@ foreach {T create} { set res [lsort -integer -increasing $res] } set n [llength $res] - if {$T==5} breakpoint do_execsql_test $T.$bAsc.$tn.$n $sql $res } } @@ -242,4 +241,3 @@ foreach {T create} { } finish_test - diff --git a/ext/fts5/test/fts5ae.test b/ext/fts5/test/fts5ae.test index 5153306d19..205a59a69f 100644 --- a/ext/fts5/test/fts5ae.test +++ b/ext/fts5/test/fts5ae.test @@ -16,7 +16,7 @@ source [file join [file dirname [info script]] fts5_common.tcl] set testprefix fts5ae -# If SQLITE_ENABLE_FTS5 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return @@ -309,4 +309,3 @@ foreach {tn q cnt} { } finish_test - diff --git a/ext/fts5/test/fts5af.test b/ext/fts5/test/fts5af.test index d6b2241568..9c95ef2daa 100644 --- a/ext/fts5/test/fts5af.test +++ b/ext/fts5/test/fts5af.test @@ -18,7 +18,7 @@ source [file join [file dirname [info script]] fts5_common.tcl] set testprefix fts5af -# If SQLITE_ENABLE_FTS5 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return @@ -72,42 +72,56 @@ foreach {tn doc res} { 2.2 {o X o o o o o o} {o [X] o o o o o...} 2.3 {o o X o o o o o} {o o [X] o o o o...} 2.4 {o o o X o o o o} {o o o [X] o o o...} - 2.5 {o o o o X o o o} {...o o o [X] o o o} - 2.6 {o o o o o X o o} {...o o o o [X] o o} - 2.7 {o o o o o o X o} {...o o o o o [X] o} + 2.5 {o o o o X o o o} {o o o o [X] o o...} + 2.6 {o o o o o X o o} {o o o o o [X] o...} + 2.7 {o o o o o o X o} {o o o o o o [X]...} 2.8 {o o o o o o o X} {...o o o o o o [X]} + 2.9 {o o o o o o o X o} {...o o o o o [X] o} + 2.10 {o o o o o o o X o o} {...o o o o [X] o o} + 2.11 {o o o o o o o X o o o} {...o o o [X] o o o} + 2.12 {o o o o o o o X o o o o} {...o o o [X] o o o...} + + 3.1 {X o o o o o o o o} {[X] o o o o o o...} 3.2 {o X o o o o o o o} {o [X] o o o o o...} 3.3 {o o X o o o o o o} {o o [X] o o o o...} 3.4 {o o o X o o o o o} {o o o [X] o o o...} - 3.5 {o o o o X o o o o} {...o o o [X] o o o...} - 3.6 {o o o o o X o o o} {...o o o [X] o o o} - 3.7 {o o o o o o X o o} {...o o o o [X] o o} - 3.8 {o o o o o o o X o} {...o o o o o [X] o} - 3.9 {o o o o o o o o X} {...o o o o o o [X]} + + 3.5 {o o o o o o o X o o o o} {...o o o [X] o o o...} + 3.6 {o o o o o o o o X o o o} {...o o o [X] o o o} + 3.7 {o o o o o o o o o X o o} {...o o o o [X] o o} + 3.8 {o o o o o o o o o o X o} {...o o o o o [X] o} + 3.9 {o o o o o o o o o o o X} {...o o o o o o [X]} 4.1 {X o o o o o X o o} {[X] o o o o o [X]...} - 4.2 {o X o o o o o X o} {...[X] o o o o o [X]...} - 4.3 {o o X o o o o o X} {...[X] o o o o o [X]} + 4.2 {o o o o o o o X o o o o o X o} {...[X] o o o o o [X]...} + 4.3 {o o o o o o o o X o o o o o X} {...[X] o o o o o [X]} 5.1 {X o o o o X o o o} {[X] o o o o [X] o...} - 5.2 {o X o o o o X o o} {...[X] o o o o [X] o...} - 5.3 {o o X o o o o X o} {...[X] o o o o [X] o} - 5.4 {o o o X o o o o X} {...o [X] o o o o [X]} + 5.2 {o o o o o o o X o o o o X o o} {...[X] o o o o [X] o...} + 5.3 {o o o o o o o o X o o o o X o} {...[X] o o o o [X] o} + 5.4 {o o o o o o o o o X o o o o X} {...o [X] o o o o [X]} 6.1 {X o o o X o o o} {[X] o o o [X] o o...} 6.2 {o X o o o X o o o} {o [X] o o o [X] o...} - 6.3 {o o X o o o X o o} {...o [X] o o o [X] o...} - 6.4 {o o o X o o o X o} {...o [X] o o o [X] o} - 6.5 {o o o o X o o o X} {...o o [X] o o o [X]} + 6.3 {o o o o o o o X o o o X o o} {...o [X] o o o [X] o...} + 6.4 {o o o o o o o o X o o o X o} {...o [X] o o o [X] o} + 6.5 {o o o o o o o o o X o o o X} {...o o [X] o o o [X]} 7.1 {X o o X o o o o o} {[X] o o [X] o o o...} 7.2 {o X o o X o o o o} {o [X] o o [X] o o...} - 7.3 {o o X o o X o o o} {...o [X] o o [X] o o...} - 7.4 {o o o X o o X o o} {...o [X] o o [X] o o} - 7.5 {o o o o X o o X o} {...o o [X] o o [X] o} - 7.6 {o o o o o X o o X} {...o o o [X] o o [X]} + 7.3 {o o o o o o o X o o X o o o} {...o [X] o o [X] o o...} + 7.4 {o o o o o o o o X o o X o o} {...o [X] o o [X] o o} + 7.5 {o o o o o o o o o X o o X o} {...o o [X] o o [X] o} + 7.6 {o o o o o o o o o o X o o X} {...o o o [X] o o [X]} + + 8.1 {o o o o o o o o o X o o o o o o o o o o o o o o o o X X X o o o} + {...o o [X] [X] [X] o o...} + 8.2 {o o o o o o o. o o X o o o o o o o o o o o o o o o o X X X o o o} + {...o o [X] o o o o...} + 8.3 {o o o o X o o o o o o o o o o o o o o o o o o o o o X X X o o o} + {o o o o [X] o o...} } { do_snippet_test 1.$tn $doc X $res } @@ -124,25 +138,89 @@ if {[detail_is_full]} { 2.1 {X Y o o o o o o} {[X Y] o o o o o...} 2.2 {o X Y o o o o o} {o [X Y] o o o o...} 2.3 {o o X Y o o o o} {o o [X Y] o o o...} - 2.4 {o o o X Y o o o} {...o o [X Y] o o o} - 2.5 {o o o o X Y o o} {...o o o [X Y] o o} - 2.6 {o o o o o X Y o} {...o o o o [X Y] o} - 2.7 {o o o o o o X Y} {...o o o o o [X Y]} + 2.4 {o o o o o o o X Y o o o} {...o o [X Y] o o o} + 2.5 {o o o o o o o o X Y o o} {...o o o [X Y] o o} + 2.6 {o o o o o o o o o X Y o} {...o o o o [X Y] o} + 2.7 {o o o o o o o o o o X Y} {...o o o o o [X Y]} 3.1 {X Y o o o o o o o} {[X Y] o o o o o...} 3.2 {o X Y o o o o o o} {o [X Y] o o o o...} 3.3 {o o X Y o o o o o} {o o [X Y] o o o...} - 3.4 {o o o X Y o o o o} {...o o [X Y] o o o...} - 3.5 {o o o o X Y o o o} {...o o [X Y] o o o} - 3.6 {o o o o o X Y o o} {...o o o [X Y] o o} - 3.7 {o o o o o o X Y o} {...o o o o [X Y] o} - 3.8 {o o o o o o o X Y} {...o o o o o [X Y]} + 3.4 {o o o o o o o X Y o o o o} {...o o [X Y] o o o...} + 3.5 {o o o o o o o o X Y o o o} {...o o [X Y] o o o} + 3.6 {o o o o o o o o o X Y o o} {...o o o [X Y] o o} + 3.7 {o o o o o o o o o o X Y o} {...o o o o [X Y] o} + 3.8 {o o o o o o o o o o o X Y} {...o o o o o [X Y]} } { do_snippet_test 2.$tn $doc "X + Y" $res } } +do_execsql_test 4.0 { + CREATE VIRTUAL TABLE x1 USING fts5(a, b); + INSERT INTO x1 VALUES('xyz', '1 2 3 4 5 6 7 8 9 10 11 12 13'); + SELECT snippet(x1, 1, '[', ']', '...', 5) FROM x1('xyz'); +} { + {1 2 3 4 5...} +} + +do_execsql_test 5.0 { + CREATE VIRTUAL TABLE p1 USING fts5(a, b, detail=%DETAIL%); + INSERT INTO p1 VALUES( + 'x a a a a a a a a a a', + 'a a a a a a a a a a a a a a a a a a a x' + ); +} +do_execsql_test 5.1 { + SELECT snippet(p1, 0, '[', ']', '...', 6) FROM p1('x'); +} {{[x] a a a a a...}} + +do_execsql_test 5.2 { + SELECT snippet(p1, 0, '[', ']', NULL, 6) FROM p1('x'); +} {{[x] a a a a a}} +do_execsql_test 5.3 { + SELECT snippet(p1, 0, NULL, ']', '...', 6) FROM p1('x'); +} {{x] a a a a a...}} +do_execsql_test 5.4 { + SELECT snippet(p1, 0, '[', NULL, '...', 6) FROM p1('x'); +} {{[x a a a a a...}} +do_execsql_test 5.5 { + SELECT snippet(p1, 0, '[', NULL, '...', 6) FROM p1('x OR ""'); +} {{[x a a a a a...}} +do_execsql_test 5.6 { + SELECT snippet(p1, 0, '[', NULL, '...', 6) FROM p1('x OR ' || x'DB'); +} {{[x a a a a a...}} + } ;# foreach_detail_mode -finish_test +reset_db +do_execsql_test 6.0 { + CREATE VIRTUAL TABLE t1 USING fts5(colA, colB); + INSERT INTO t1 VALUES('A B C', 'D E F'); +} + +do_execsql_test 6.1 { + SELECT colA, colB, snippet(t1,0,'[', ']','...',1) FROM t1 WHERE t1 MATCH 'B'; +} {{A B C} {D E F} ...[B]...} +breakpoint +do_execsql_test 6.2 { + SELECT colA, colB, snippet(t1, 1,'[',']','...',2) FROM t1 WHERE t1 MATCH 'B'; +} {{A B C} {D E F} {D E...}} +do_execsql_test 6.3 { + SELECT colA, colB, snippet(t1, 1,'[',']','...',1) FROM t1 WHERE t1 MATCH 'B'; +} {{A B C} {D E F} {D...}} + +do_execsql_test 6.1 { + SELECT colA, colB, snippet(t1,0,'[', ']','...',1) FROM t1 WHERE t1 MATCH 'A'; +} {{A B C} {D E F} [A]...} +breakpoint +do_execsql_test 6.2 { + SELECT colA, colB, snippet(t1, 1,'[',']','...',2) FROM t1 WHERE t1 MATCH 'A'; +} {{A B C} {D E F} {D E...}} +do_execsql_test 6.3 { + SELECT colA, colB, snippet(t1, 1,'[',']','...',1) FROM t1 WHERE t1 MATCH 'A'; +} {{A B C} {D E F} {D...}} + + +finish_test diff --git a/ext/fts5/test/fts5ag.test b/ext/fts5/test/fts5ag.test index de126a25f0..42cd913784 100644 --- a/ext/fts5/test/fts5ag.test +++ b/ext/fts5/test/fts5ag.test @@ -15,7 +15,7 @@ source [file join [file dirname [info script]] fts5_common.tcl] set testprefix fts5ag -# If SQLITE_ENABLE_FTS5 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return @@ -142,4 +142,3 @@ if {[detail_is_full]} { finish_test - diff --git a/ext/fts5/test/fts5ah.test b/ext/fts5/test/fts5ah.test index b7beb5655b..bf9c9e9dbc 100644 --- a/ext/fts5/test/fts5ah.test +++ b/ext/fts5/test/fts5ah.test @@ -11,11 +11,12 @@ # This file implements regression tests for SQLite library. The # focus of this script is testing the FTS5 module. # +# TESTRUNNER: slow source [file join [file dirname [info script]] fts5_common.tcl] set testprefix fts5ah -# If SQLITE_ENABLE_FTS5 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return @@ -162,9 +163,19 @@ do_execsql_test 1.8.2 { SELECT count(*) FROM t1 WHERE t1 MATCH 'x' AND rowid < 'text'; } {10000} +do_execsql_test 1.8.3 { + SELECT count(*) FROM t1 WHERE t1 MATCH 'x' AND rowid<5000 AND rowid < 'text'; +} {4999} +do_execsql_test 1.8.4 { + SELECT count(*) FROM t1 WHERE t1 MATCH 'x' AND rowid>5000 AND rowid > 'text'; +} {0} + +do_catchsql_test 1.9 { + SELECT * FROM t1('*xy'); +} {1 {unknown special query: xy}} + } ;# foreach_detail_mode #db eval {SELECT rowid, fts5_decode(rowid, block) aS r FROM t1_data} {puts $r} finish_test - diff --git a/ext/fts5/test/fts5ai.test b/ext/fts5/test/fts5ai.test index e32c806c46..a6576d3afc 100644 --- a/ext/fts5/test/fts5ai.test +++ b/ext/fts5/test/fts5ai.test @@ -17,7 +17,7 @@ source [file join [file dirname [info script]] fts5_common.tcl] set testprefix fts5ai -# If SQLITE_ENABLE_FTS5 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return @@ -51,8 +51,13 @@ do_execsql_test 1.1 { do_execsql_test 1.2 { INSERT INTO t1(t1) VALUES('integrity-check'); } + +do_execsql_test 1.3 { + SAVEPOINT one; + INSERT INTO t1 VALUES('v w x'); + ROLLBACK TO one; +} } finish_test - diff --git a/ext/fts5/test/fts5aj.test b/ext/fts5/test/fts5aj.test index 6b9dddd8b0..e802306b38 100644 --- a/ext/fts5/test/fts5aj.test +++ b/ext/fts5/test/fts5aj.test @@ -19,7 +19,7 @@ source [file join [file dirname [info script]] fts5_common.tcl] set testprefix fts5aj -# If SQLITE_ENABLE_FTS5 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return @@ -66,4 +66,3 @@ do_execsql_test 2.0 { INSERT INTO t1(t1) VALUES('integrity-check') } finish_test - diff --git a/ext/fts5/test/fts5ak.test b/ext/fts5/test/fts5ak.test index 0f699a601f..253f14fc79 100644 --- a/ext/fts5/test/fts5ak.test +++ b/ext/fts5/test/fts5ak.test @@ -17,7 +17,7 @@ source [file join [file dirname [info script]] fts5_common.tcl] set testprefix fts5ak -# If SQLITE_ENABLE_FTS5 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return @@ -144,7 +144,40 @@ do_execsql_test 3.1 { {[a b c d e]} } +do_execsql_test 3.2 { + SELECT highlight(ft, 0, NULL, NULL) FROM ft WHERE ft MATCH 'a+b+c AND c+d+e'; +} { + {a b c x c d e} + {a b c c d e} + {a b c d e} } -finish_test +} + +# 2023-04-06 https://sqlite.org/forum/forumpost/cae4367d9b +# +# This is not a test of FTS5, but rather a test of the of what happens to +# prepared statements that encounter SQLITE_SCHEMA while other prepared +# statements are running. The original problem POC used FTS5, and so +# is seems reasonable to put the test here. +# +# The vdbeaux24.test module in TH3 also tests this same behavior but +# without requiring FTS5 or an other extension. +# +reset_db +db null NULL +do_execsql_test 4.0 { + CREATE TABLE t5(a PRIMARY KEY); + INSERT INTO t5 VALUES(0); + CREATE VIRTUAL TABLE t6 USING fts5(0); + DELETE FROM t6; + CREATE TABLE t7(x); + WITH cte(a) AS ( + SELECT a FROM t5 + WHERE ((0,0) IN (SELECT 0, LAG(0) OVER (PARTITION BY 0) FROM t6), 0) + < (a,0) + ) + SELECT max(a) FROM cte; +} NULL +finish_test diff --git a/ext/fts5/test/fts5al.test b/ext/fts5/test/fts5al.test index c0dd2117dd..7187ad67c7 100644 --- a/ext/fts5/test/fts5al.test +++ b/ext/fts5/test/fts5al.test @@ -17,7 +17,7 @@ source [file join [file dirname [info script]] fts5_common.tcl] set testprefix fts5al -# If SQLITE_ENABLE_FTS5 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return @@ -77,7 +77,7 @@ foreach {tn defn} { } { do_test 2.2.$tn { catchsql { INSERT INTO ft1(ft1, rank) VALUES('rank', $defn) } - } {1 {SQL logic error or missing database}} + } {1 {SQL logic error}} } #------------------------------------------------------------------------- @@ -293,8 +293,17 @@ do_catchsql_test 4.4.4 { SELECT *, rank FROM t3 WHERE t3 MATCH 'a' AND rank MATCH NULL } {1 {parse error in rank function: }} +# Check that the second and subsequent rank= constraints are ignored. +# +do_catchsql_test 4.3.3 { + SELECT *, rank FROM t3 + WHERE t3 MATCH 'a' AND + rank MATCH 'nosuch()' AND + rank MATCH 'rowidmod(3)' + ORDER BY rank ASC +} {1 {unable to use function MATCH in the requested context}} + } ;# foreach_detail_mode finish_test - diff --git a/ext/fts5/test/fts5alter.test b/ext/fts5/test/fts5alter.test index eae01b7386..bb5f78dc86 100644 --- a/ext/fts5/test/fts5alter.test +++ b/ext/fts5/test/fts5alter.test @@ -16,7 +16,7 @@ source [file join [file dirname [info script]] fts5_common.tcl] set testprefix fts5alter -# If SQLITE_ENABLE_FTS5 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return @@ -89,7 +89,6 @@ do_execsql_test 3.1 { BEGIN; INSERT INTO abc(rowid, a) VALUES(2, 'a'); } -breakpoint do_execsql_test 3.2 { SELECT rowid FROM abc WHERE abc MATCH 'a'; } {1 2} @@ -100,4 +99,3 @@ do_execsql_test 3.3 { } {1 2} finish_test - diff --git a/ext/fts5/test/fts5auto.test b/ext/fts5/test/fts5auto.test index 218b3f4862..b771af912e 100644 --- a/ext/fts5/test/fts5auto.test +++ b/ext/fts5/test/fts5auto.test @@ -16,7 +16,7 @@ source [file join [file dirname [info script]] fts5_common.tcl] set testprefix fts5auto -# If SQLITE_ENABLE_FTS5 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return @@ -342,4 +342,3 @@ foreach {tn expr} { finish_test - diff --git a/ext/fts5/test/fts5aux.test b/ext/fts5/test/fts5aux.test index 995fe85784..960dbc5117 100644 --- a/ext/fts5/test/fts5aux.test +++ b/ext/fts5/test/fts5aux.test @@ -15,7 +15,7 @@ source [file join [file dirname [info script]] fts5_common.tcl] set testprefix fts5aux -# If SQLITE_ENABLE_FTS5 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return @@ -240,11 +240,165 @@ foreach {tn lRow res} { } { execsql { DELETE FROM x1 } foreach row $lRow { execsql { INSERT INTO x1 VALUES($row) } } - breakpoint do_execsql_test 8.$tn { SELECT highlight(x1, 0, '[', ']') FROM x1 WHERE x1 MATCH 'a OR (b AND d)'; } $res } -finish_test +#------------------------------------------------------------------------- +# Test the built-in bm25() demo. +# +reset_db +do_execsql_test 9.1 { + CREATE VIRTUAL TABLE t1 USING fts5(a, b); + INSERT INTO t1 VALUES('a', NULL); -- 1 + INSERT INTO t1 VALUES('a', NULL); -- 2 + INSERT INTO t1 VALUES('a', NULL); -- 3 + INSERT INTO t1 VALUES('a', NULL); -- 4 + INSERT INTO t1 VALUES('a', NULL); -- 5 + INSERT INTO t1 VALUES('a', NULL); -- 6 + INSERT INTO t1 VALUES('a', NULL); -- 7 + INSERT INTO t1 VALUES('a', NULL); -- 8 + INSERT INTO t1 VALUES(NULL, 'a a b'); -- 9 + INSERT INTO t1 VALUES(NULL, 'b b a'); -- 10 +} + +do_execsql_test 9.2 { + SELECT rowid FROM t1('a AND b') ORDER BY rank; +} { + 10 9 +} + +do_execsql_test 9.3 { + SELECT rowid FROM t1('b:a AND b:b') ORDER BY rank; +} { + 9 10 +} + +#------------------------------------------------------------------------- +# Test that aux. functions may not be used in aggregate queries. +# +reset_db +do_execsql_test 10.0 { + CREATE VIRTUAL TABLE t1 USING fts5(x, y, z); + INSERT INTO t1 VALUES('a', 'one two', 1); + INSERT INTO t1 VALUES('b', 'two three', 2); + INSERT INTO t1 VALUES('c', 'three four', 1); + INSERT INTO t1 VALUES('d', 'four five', 2); + INSERT INTO t1 VALUES('e', 'five six', 1); + INSERT INTO t1 VALUES('f', 'six seven', 2); +} + +proc firstcol {cmd} { $cmd xColumnText 0 } +sqlite3_fts5_create_function db firstcol firstcol + +do_execsql_test 10.1.1 { + SELECT firstcol(t1) FROM t1 +} {a b c d e f} +do_execsql_test 10.1.2 { + SELECT group_concat(x, '.') FROM t1 +} {a.b.c.d.e.f} + +do_catchsql_test 10.1.3 { + SELECT group_concat(firstcol(t1), '.') FROM t1 +} {1 {unable to use function firstcol in the requested context}} + +do_catchsql_test 10.1.4 { + SELECT group_concat(firstcol(t1), '.') FROM t1 GROUP BY rowid +} {1 {unable to use function firstcol in the requested context}} + +#------------------------------------------------------------------------- +# Test that xInstCount() works from within an xPhraseQuery() callback. +# +reset_db + +proc xCallback {cmd} { + incr ::hitcount [$cmd xInstCount] + return SQLITE_OK +} +proc fts5_hitcount {cmd} { + set ::hitcount 0 + $cmd xQueryPhrase 0 xCallback + return $::hitcount +} +sqlite3_fts5_create_function db fts5_hitcount fts5_hitcount + +do_execsql_test 11.1 { + CREATE VIRTUAL TABLE x1 USING fts5(z); + INSERT INTO x1 VALUES('one two three'); + INSERT INTO x1 VALUES('one two one three one'); + INSERT INTO x1 VALUES('one two three'); +} + +do_execsql_test 11.2 { + SELECT fts5_hitcount(x1) FROM x1('one') LIMIT 1; +} {5} + +#------------------------------------------------------------------------- +# Test that xColumnText returns SQLITE_RANGE when it should. +# +reset_db +fts5_aux_test_functions db +do_execsql_test 12.0 { + CREATE VIRTUAL TABLE t1 USING fts5(a, b, c); + INSERT INTO t1 VALUES('one', 'two', 'three'); + INSERT INTO t1 VALUES('one', 'one', 'one'); + INSERT INTO t1 VALUES('two', 'two', 'two'); + INSERT INTO t1 VALUES('three', 'three', 'three'); +} + +do_catchsql_test 12.1.1 { + SELECT fts5_columntext(t1, -1) FROM t1('two'); +} {1 SQLITE_RANGE} +do_catchsql_test 12.1.2 { + SELECT fts5_columntext(t1, 3) FROM t1('two'); +} {1 SQLITE_RANGE} +do_catchsql_test 12.1.2 { + SELECT fts5_columntext(t1, 1) FROM t1('one AND two'); +} {0 two} +do_catchsql_test 12.2.1 { + SELECT fts5_queryphrase(t1, -1) FROM t1('one AND two'); +} {1 SQLITE_RANGE} +do_catchsql_test 12.2.2 { + SELECT fts5_queryphrase(t1, 2) FROM t1('one AND two'); +} {1 SQLITE_RANGE} +do_catchsql_test 12.2.3 { + SELECT fts5_queryphrase(t1, 1) FROM t1('one AND two'); +} {0 {{1 2 1}}} + +do_catchsql_test 12.3.1 { + SELECT fts5_collist(t1, -1) FROM t1('one AND two'); +} {1 SQLITE_RANGE} +do_catchsql_test 12.3.2 { + SELECT fts5_collist(t1, 2) FROM t1('one AND two'); +} {1 SQLITE_RANGE} +do_catchsql_test 12.3.3 { + SELECT fts5_collist(t1, 1) FROM t1('one AND two'); +} {0 1} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 13.1 { + CREATE VIRTUAL TABLE t1 USING fts5(a, tokenize=ascii); + INSERT INTO t1 VALUES('a b c'), ('d e f'); + PRAGMA integrity_check; +} {ok} + +do_catchsql_test 13.2 { + SELECT highlight(t1, 0, '[', ']') FROM t1 +} {0 {{a b c} {d e f}}} + +do_execsql_test 13.3 { + PRAGMA writable_schema = 1; + UPDATE sqlite_schema SET sql = 'CREATE VIRTUAL TABLE t1 USING fts5(a, tokenize=blah)' + WHERE name = 't1'; +} + +db close +sqlite3 db test.db +do_catchsql_test 13.4 { + SELECT highlight(t1, 0, '[', ']') FROM t1 +} {1 {SQL logic error}} + +finish_test diff --git a/ext/fts5/test/fts5aux2.test b/ext/fts5/test/fts5aux2.test new file mode 100644 index 0000000000..2352970ec7 --- /dev/null +++ b/ext/fts5/test/fts5aux2.test @@ -0,0 +1,71 @@ +# 2024 June 24 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# Tests focusing on the auxiliary function APIs. +# + +source [file join [file dirname [info script]] fts5_common.tcl] +set testprefix fts5aux + +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. +ifcapable !fts5 { + finish_test + return +} + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE x1 USING fts5(a, b); + INSERT INTO x1 VALUES('a b', 'c d'); + INSERT INTO x1 VALUES('d e', 'a b'); + INSERT INTO x1 VALUES('a b', 'e f'); + INSERT INTO x1 VALUES('d e', 'c d'); +} + +fts5_aux_test_functions db +do_execsql_test 1.1 { + SELECT fts5_test_all(x1) FROM x1 WHERE rowid=2 +} [list [list {*}{ + columnsize {2 2} + columntext {{d e} {a b}} + columntotalsize {8 8} + poslist {} + tokenize {{d e} {a b}} + rowcount 4 +}]] + +do_execsql_test 1.2 { + SELECT fts5_test_columntext(x1) FROM x1 +} { + {{a b} {c d}} + {{d e} {a b}} + {{a b} {e f}} + {{d e} {c d}} +} + +do_execsql_test 1.3 { + SELECT fts5_test_rowid(x1) FROM x1 +} { + 1 2 3 4 +} +do_execsql_test 1.4 { + SELECT fts5_test_phrasecount(x1) FROM x1 +} { + 0 0 0 0 +} +do_catchsql_test 1.5 { + SELECT fts5_queryphrase(x1, 0) FROM x1 +} {1 SQLITE_RANGE} +do_execsql_test 1.6 { + SELECT fts5_test_rowcount(x1) FROM x1 +} {4 4 4 4} + + +finish_test diff --git a/ext/fts5/test/fts5auxdata.test b/ext/fts5/test/fts5auxdata.test index dbbb1dba78..7f99fed316 100644 --- a/ext/fts5/test/fts5auxdata.test +++ b/ext/fts5/test/fts5auxdata.test @@ -15,7 +15,7 @@ source [file join [file dirname [info script]] fts5_common.tcl] set testprefix fts5auxdata -# If SQLITE_ENABLE_FTS5 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return @@ -112,4 +112,3 @@ db eval { } finish_test - diff --git a/ext/fts5/test/fts5bigid.test b/ext/fts5/test/fts5bigid.test new file mode 100644 index 0000000000..ae20ec641e --- /dev/null +++ b/ext/fts5/test/fts5bigid.test @@ -0,0 +1,62 @@ +# 2023 May 28 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# + +source [file join [file dirname [info script]] fts5_common.tcl] +set testprefix fts5bigid + +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. +ifcapable !fts5 { + finish_test + return +} + +set nRow 20000 + +proc do_ascdesc_test {tn query} { + set ::lAsc [db eval { SELECT rowid FROM x1($query) }] + set ::lDesc [db eval { SELECT rowid FROM x1($query) ORDER BY rowid DESC }] + do_test $tn.1 { lsort -integer $::lAsc } $::lAsc + do_test $tn.2 { lsort -integer -decr $::lDesc } $::lDesc + do_test $tn.3 { lsort -integer $::lDesc } $::lAsc +} + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE x1 USING fts5(a); +} + +do_test 1.1 { + for {set ii 0} {$ii < $nRow} {incr ii} { + db eval { + REPLACE INTO x1(rowid, a) VALUES(random(), 'movement at the station'); + } + } +} {} + +do_ascdesc_test 1.2 "the" + +do_execsql_test 1.3 { + DELETE FROM x1 +} + +do_test 1.4 { + for {set ii 0} {$ii < $nRow} {incr ii} { + db eval { + INSERT INTO x1(rowid, a) VALUES( + $ii + 0x6FFFFFFFFFFFFFFF, 'movement at the station' + ); + } + } +} {} + +do_ascdesc_test 1.5 "movement" + +finish_test diff --git a/ext/fts5/test/fts5bigpl.test b/ext/fts5/test/fts5bigpl.test index 85f74606c6..9e3d86c0e6 100644 --- a/ext/fts5/test/fts5bigpl.test +++ b/ext/fts5/test/fts5bigpl.test @@ -17,7 +17,7 @@ source [file join [file dirname [info script]] fts5_common.tcl] set testprefix fts5bigpl -# If SQLITE_ENABLE_FTS5 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return @@ -61,4 +61,3 @@ do_test 2.1...slow { } {} finish_test - diff --git a/ext/fts5/test/fts5bigtok.test b/ext/fts5/test/fts5bigtok.test index 2267be5058..f74ec8f665 100644 --- a/ext/fts5/test/fts5bigtok.test +++ b/ext/fts5/test/fts5bigtok.test @@ -14,6 +14,7 @@ source [file join [file dirname [info script]] fts5_common.tcl] set testprefix fts5bigtok +return_if_no_fts5 proc rndterm {} { set L [list a b c d e f g h i j k l m n o p q r s t u v w x y z] @@ -63,5 +64,3 @@ foreach_detail_mode $::testprefix { } finish_test - - diff --git a/ext/fts5/test/fts5blob.test b/ext/fts5/test/fts5blob.test new file mode 100644 index 0000000000..9348554104 --- /dev/null +++ b/ext/fts5/test/fts5blob.test @@ -0,0 +1,166 @@ +# 2024 July 30 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# This file verifies that: +# +# * blob values may be written to locale=0 tables. +# +# * blob values - other than fts5_locale() values - may not be written +# to locale=0 tables. This is an SQLITE_MISMATCH error +# +# * blob values may be returned by queries on the external-content table +# of a locale=0 table. +# +# * blob values not may be returned by queries on the external-content +# table of a locale=1 table, apart from fts5_locale() blobs. This is an +# SQLITE_MISMATCH error. +# + +source [file join [file dirname [info script]] fts5_common.tcl] +set testprefix fts5blob + +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. +ifcapable !fts5 { + finish_test + return +} + +# Test that blobs may be stored in normal locale=0 tables. +# +foreach {tn enc} { + 1 utf8 + 2 utf16 +} { + reset_db + fts5_aux_test_functions db + + execsql "PRAGMA encoding = $enc" + + execsql " + CREATE VIRTUAL TABLE t1 USING fts5(x, y); + " + do_execsql_test 1.$tn.0 { + CREATE VIRTUAL TABLE tt USING fts5vocab('t1', 'instance'); + INSERT INTO t1(rowid, x, y) VALUES(1, 555, X'0000000041424320444546'); + INSERT INTO t1(rowid, x, y) VALUES(2, 666, X'41424300444546'); + INSERT INTO t1(rowid, x, y) VALUES(3, 777, 'xyz'); + } + + do_execsql_test 1.$tn.1 { + SELECT rowid, quote(x), quote(y) FROM t1 + } { + 1 555 X'0000000041424320444546' + 2 666 X'41424300444546' + 3 777 'xyz' + } + + do_execsql_test 1.$tn.2 { + DELETE FROM t1 WHERE rowid=2; + DELETE FROM t1 WHERE rowid=1; + } + + do_execsql_test 1.$tn.3 { + PRAGMA integrity_check; + } {ok} +} + +#-------------------------------------------------------------------------- +# Test that a blob may be stored and retrieved in an unindexed column of +# a regular table with locale=1. +# +reset_db +do_execsql_test 2.0 { + CREATE VIRTUAL TABLE t1 USING fts5(x, y UNINDEXED, locale=1); + INSERT INTO t1(rowid, x, y) VALUES(12, 'twelve', X'0000000041424320444546'); +} + +do_execsql_test 2.1 { + select rowid, x, quote(y) FROM t1 +} { + 12 twelve X'0000000041424320444546' +} + +#-------------------------------------------------------------------------- +# Test that blobs may not be written to any type of table with locale=1 +# set. Except, they may be written to UNINDEXED columns. +# +reset_db +do_execsql_test 3.0 { + CREATE TABLE t1(a, b); + + CREATE VIRTUAL TABLE x1 USING fts5(a, b, locale=1); + CREATE VIRTUAL TABLE x2 USING fts5(a, b, locale=1, content=t2); + CREATE VIRTUAL TABLE x3 USING fts5(a, b, locale=1, content=); +} + +do_catchsql_test 3.1 { + INSERT INTO x1(rowid, a, b) VALUES(113, 'hello world', X'123456'); +} {0 {}} +do_catchsql_test 3.2 { + INSERT INTO x2(rowid, a, b) VALUES(113, 'hello world', X'123456'); +} {0 {}} +do_catchsql_test 3.3 { + INSERT INTO x3(rowid, a, b) VALUES(113, 'hello world', X'123456'); +} {0 {}} + + +#-------------------------------------------------------------------------- +# Test that fts5_locale() values may not be written to any type of table +# without locale=1 set. Even to an UNINDEXED column. +# +reset_db +do_execsql_test 3.0 { + CREATE TABLE t1(a, b); + + CREATE VIRTUAL TABLE x1 USING fts5(a, b); + CREATE VIRTUAL TABLE x2 USING fts5(a, b, content=t2); + CREATE VIRTUAL TABLE x3 USING fts5(a, b, content=); + + CREATE VIRTUAL TABLE x4 USING fts5(a, b, c UNINDEXED); +} + +do_catchsql_test 3.1 { + INSERT INTO x1(rowid, a, b) + VALUES(113, 'hello world', fts5_locale('en_AU', 'abc')); +} {1 {fts5_locale() requires locale=1}} +do_catchsql_test 3.2 { + INSERT INTO x2(rowid, a, b) + VALUES(113, 'hello world', fts5_locale('en_AU', 'abc')); +} {1 {fts5_locale() requires locale=1}} +do_catchsql_test 3.3 { + INSERT INTO x3(rowid, a, b) + VALUES(113, 'hello world', fts5_locale('en_AU', 'abc')); +} {1 {fts5_locale() requires locale=1}} +do_catchsql_test 3.4 { + INSERT INTO x4(rowid, a, b, c) + VALUES(113, 'hello world', 'yesno', fts5_locale('en_AU', 'abc')); +} {1 {fts5_locale() requires locale=1}} + + +#------------------------------------------------------------------------- +# +reset_db +do_execsql_test 4.0 { + CREATE VIRTUAL TABLE x1 USING fts5(x); +} + +foreach {tn sql} { + 1 { INSERT INTO x1(rowid, x) VALUES(4.5, 'abcd') } + 2 { INSERT INTO x1(rowid, x) VALUES('xyz', 'abcd') } + 3 { INSERT INTO x1(rowid, x) VALUES(X'001122', 'abcd') } +} { + do_catchsql_test 4.1.$tn $sql {1 {datatype mismatch}} +} + + +finish_test + + diff --git a/ext/fts5/test/fts5cat.test b/ext/fts5/test/fts5cat.test new file mode 100644 index 0000000000..71e2abe3ae --- /dev/null +++ b/ext/fts5/test/fts5cat.test @@ -0,0 +1,76 @@ +# 2016 Jan 15 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# + +source [file join [file dirname [info script]] fts5_common.tcl] +ifcapable !fts5 { finish_test ; return } +set ::testprefix fts5cat + + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE t1 USING fts5(x, tokenize="unicode61 categories 'L*'"); + INSERT INTO t1 VALUES ('Unlike1option2values3and4column5names'); +} + +do_execsql_test 1.1 { + SELECT rowid FROM t1('option'); +} {1} + +do_execsql_test 1.2 { + CREATE VIRTUAL TABLE t2 USING fts5(x); + CREATE VIRTUAL TABLE t2t USING fts5vocab(t2, row); + + CREATE VIRTUAL TABLE t3 USING fts5( + x, tokenize="unicode61 categories 'L* N* Co Mn'" + ); + CREATE VIRTUAL TABLE t3t USING fts5vocab(t3, row); + + CREATE VIRTUAL TABLE t4 USING fts5( + x, tokenize="unicode61 categories 'L* N* Co M*'" + ); + CREATE VIRTUAL TABLE t4t USING fts5vocab(t4, row); + + INSERT INTO t2 VALUES ('สนามกีฬา'); + INSERT INTO t3 VALUES ('สนามกีฬา'); + INSERT INTO t4 VALUES ('สนามกีฬา'); +} + +do_execsql_test 1.3 { + SELECT * FROM t2t +} {สนามก 1 1 ฬา 1 1} + +do_execsql_test 1.4 { + SELECT * FROM t3t +} {สนามกีฬา 1 1} + +do_execsql_test 1.5 { + SELECT * FROM t4t +} {สนามกีฬา 1 1} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 2.0 " + CREATE VIRTUAL TABLE x1 USING fts5(c, + tokenize=\"unicode61 categories ' \t'\"); +" + +do_catchsql_test 2.1 " + CREATE VIRTUAL TABLE x2 USING fts5(c, + tokenize=\"unicode61 categories 'N*\t\tMYZ'\"); +" {1 {error in tokenizer constructor}} + +do_catchsql_test 2.2 " + CREATE VIRTUAL TABLE x2 USING fts5(c, + tokenize=\"unicode61 categories 'N*\t\tXYZ'\"); +" {1 {error in tokenizer constructor}} + + +finish_test diff --git a/ext/fts5/test/fts5circref.test b/ext/fts5/test/fts5circref.test new file mode 100644 index 0000000000..8732fa17dd --- /dev/null +++ b/ext/fts5/test/fts5circref.test @@ -0,0 +1,80 @@ +# 2018 Dec 22 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# This file implements regression tests for SQLite library. The +# focus of this script is testing the FTS5 module. +# + +source [file join [file dirname [info script]] fts5_common.tcl] +set testprefix fts5circref + +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. +ifcapable !fts5 { + finish_test + return +} + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE tt USING fts5(a); + SELECT name FROM sqlite_master ORDER BY 1; +} { + tt tt_config tt_content tt_data tt_docsize tt_idx +} +db_save_and_close + +foreach {tn schema sql} { + 1 { + CREATE TRIGGER tr1 AFTER INSERT ON tt_config BEGIN + SELECT * FROM tt; + END; + } { + INSERT INTO tt(tt, rank) VALUES('usermerge', 4); + } + + 2 { + CREATE TRIGGER tr1 AFTER INSERT ON tt_docsize BEGIN + SELECT * FROM tt; + END; + } { + INSERT INTO tt(a) VALUES('one two three'); + } + + 3 { + CREATE TRIGGER tr1 AFTER INSERT ON tt_content BEGIN + SELECT * FROM tt; + END; + } { + INSERT INTO tt(a) VALUES('one two three'); + } + + 4 { + CREATE TRIGGER tr1 AFTER INSERT ON tt_data BEGIN + SELECT * FROM tt; + END; + } { + INSERT INTO tt(a) VALUES('one two three'); + } + + 5 { + CREATE TRIGGER tr1 AFTER INSERT ON tt_idx BEGIN + SELECT * FROM tt; + END; + } { + INSERT INTO tt(a) VALUES('one two three'); + } +} { + db_restore_and_reopen + do_execsql_test 1.1.$tn.1 $schema + do_catchsql_test 1.1.$tn.2 $sql {1 {database disk image is malformed}} + db close +} + + +finish_test diff --git a/ext/fts5/test/fts5colset.test b/ext/fts5/test/fts5colset.test new file mode 100644 index 0000000000..e5429572c5 --- /dev/null +++ b/ext/fts5/test/fts5colset.test @@ -0,0 +1,105 @@ +# 2016 August 10 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# This file implements regression tests for SQLite library. The +# focus of this script is testing the FTS5 module. +# + +source [file join [file dirname [info script]] fts5_common.tcl] +set testprefix fts5colset + +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. +ifcapable !fts5 { + finish_test + return +} + +foreach_detail_mode $::testprefix { + if {[detail_is_none]} continue + + do_execsql_test 1.0 { + CREATE VIRTUAL TABLE t1 USING fts5(a, b, c, d, detail=%DETAIL%); + INSERT INTO t1 VALUES('a', 'b', 'c', 'd'); -- 1 + INSERT INTO t1 VALUES('d', 'a', 'b', 'c'); -- 2 + INSERT INTO t1 VALUES('c', 'd', 'a', 'b'); -- 3 + INSERT INTO t1 VALUES('b', 'c', 'd', 'a'); -- 4 + } + + foreach {tn q res} { + 1 "a" {1 2 3 4} + 2 "{a} : a" {1} + 3 "-{a} : a" {2 3 4} + 4 "- {a c} : a" {2 4} + 5 " - {d d c} : a" {1 2} + 6 "- {d c b a} : a" {} + 7 "-{\"a\"} : b" {1 2 3} + 8 "- c : a" {1 2 4} + 9 "-c : a" {1 2 4} + 10 "-\"c\" : a" {1 2 4} + } { + do_execsql_test 1.$tn { + SELECT rowid FROM t1($q) + } $res + } + + foreach {tn q res} { + 0 {{a} : (a AND ":")} {} + 1 "{a b c} : (a AND d)" {2 3} + 2 "{a b c} : (a AND b:d)" {3} + 3 "{a b c} : (a AND d:d)" {} + 4 "{b} : ( {b a} : ( {c b a} : ( {d b c a} : ( d OR c ) ) ) )" {3 4} + 5 "{a} : ( {b a} : ( {c b a} : ( {d b c a} : ( d OR c ) ) ) )" {2 3} + 6 "{a} : ( {b a} : ( {c b} : ( {d b c a} : ( d OR c ) ) ) )" {} + 7 "{a b c} : (b:a AND c:b)" {2} + } { + do_execsql_test 2.$tn { + SELECT rowid FROM t1($q) + } $res + } + + foreach {tn w res} { + 0 "a MATCH 'a'" {1} + 1 "b MATCH 'a'" {2} + 2 "b MATCH '{a b c} : a'" {2} + 3 "b MATCH 'a OR b'" {1 2} + 4 "b MATCH 'a OR a:b'" {2} + 5 "b MATCH 'a OR b:b'" {1 2} + } { + do_execsql_test 3.$tn " + SELECT rowid FROM t1 WHERE $w + " $res + } + + do_catchsql_test 4.1 { + SELECT * FROM t1 WHERE rowid MATCH 'a' + } {1 {no query solution}} +} + +#------------------------------------------------------------------------- +# Confirm that the expression parser creates the same expression tree +# for: +# +# {a b} : (abc AND def) +# -{c d} : (abc AND def) +# +# Assuming that the table columns are (a, b, c, d). +# +do_execsql_test 5.1 { + SELECT fts5_expr('abcd AND cdef'); +} {{"abcd" AND "cdef"}} +do_execsql_test 5.2 { + SELECT fts5_expr('{a b} : (abcd AND cdef)', 'a', 'b', 'c', 'd'); +} {{{a b} : "abcd" AND {a b} : "cdef"}} +do_execsql_test 5.3 { + SELECT fts5_expr('-{c d} : (abcd AND cdef)', 'a', 'b', 'c', 'd'); +} {{{a b} : "abcd" AND {a b} : "cdef"}} + + +finish_test diff --git a/ext/fts5/test/fts5columnsize.test b/ext/fts5/test/fts5columnsize.test index dec9b58d3d..7af49184b8 100644 --- a/ext/fts5/test/fts5columnsize.test +++ b/ext/fts5/test/fts5columnsize.test @@ -15,7 +15,7 @@ source [file join [file dirname [info script]] fts5_common.tcl] set testprefix fts5columnsize -# If SQLITE_ENABLE_FTS5 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return @@ -143,7 +143,6 @@ do_execsql_test 4.1.1 { INSERT INTO t5 VALUES('2 4 6 8'); } -breakpoint do_execsql_test 4.1.2 { INSERT INTO t5(t5) VALUES('integrity-check'); } diff --git a/ext/fts5/test/fts5config.test b/ext/fts5/test/fts5config.test index c30a597242..28f3146ea3 100644 --- a/ext/fts5/test/fts5config.test +++ b/ext/fts5/test/fts5config.test @@ -16,7 +16,7 @@ source [file join [file dirname [info script]] fts5_common.tcl] set testprefix fts5config -# If SQLITE_ENABLE_FTS5 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return @@ -66,7 +66,7 @@ foreach {tn val} { } { do_catchsql_test 3.$tn { INSERT INTO t1(t1, rank) VALUES('rank', $val); - } {1 {SQL logic error or missing database}} + } {1 {SQL logic error}} } #------------------------------------------------------------------------- @@ -110,7 +110,6 @@ do_catchsql_test 5.1 { CREATE VIRTUAL TABLE xx USING fts5(x, tokenize="porter 'ascii"); } {1 {parse error in tokenize directive}} -breakpoint do_catchsql_test 5.2 { CREATE VIRTUAL TABLE xx USING fts5(x, [y[]); } {0 {}} @@ -169,33 +168,33 @@ do_execsql_test 9.0 { } {} do_catchsql_test 9.1.1 { INSERT INTO abc(abc, rank) VALUES('pgsz', -5); -} {1 {SQL logic error or missing database}} +} {1 {SQL logic error}} do_catchsql_test 9.1.2 { INSERT INTO abc(abc, rank) VALUES('pgsz', 50000000); -} {1 {SQL logic error or missing database}} +} {1 {SQL logic error}} do_catchsql_test 9.1.3 { INSERT INTO abc(abc, rank) VALUES('pgsz', 66.67); -} {1 {SQL logic error or missing database}} +} {1 {SQL logic error}} do_catchsql_test 9.2.1 { INSERT INTO abc(abc, rank) VALUES('automerge', -5); -} {1 {SQL logic error or missing database}} +} {1 {SQL logic error}} do_catchsql_test 9.2.2 { INSERT INTO abc(abc, rank) VALUES('automerge', 50000000); -} {1 {SQL logic error or missing database}} +} {1 {SQL logic error}} do_catchsql_test 9.2.3 { INSERT INTO abc(abc, rank) VALUES('automerge', 66.67); -} {1 {SQL logic error or missing database}} +} {1 {SQL logic error}} do_execsql_test 9.2.4 { INSERT INTO abc(abc, rank) VALUES('automerge', 1); } {} do_catchsql_test 9.3.1 { INSERT INTO abc(abc, rank) VALUES('crisismerge', -5); -} {1 {SQL logic error or missing database}} +} {1 {SQL logic error}} do_catchsql_test 9.3.2 { INSERT INTO abc(abc, rank) VALUES('crisismerge', 66.67); -} {1 {SQL logic error or missing database}} +} {1 {SQL logic error}} do_execsql_test 9.3.3 { INSERT INTO abc(abc, rank) VALUES('crisismerge', 1); } {} @@ -205,14 +204,14 @@ do_execsql_test 9.3.4 { do_catchsql_test 9.4.1 { INSERT INTO abc(abc, rank) VALUES('nosuchoption', 1); -} {1 {SQL logic error or missing database}} +} {1 {SQL logic error}} do_catchsql_test 9.5.1 { INSERT INTO abc(abc, rank) VALUES('hashsize', 'not an integer'); -} {1 {SQL logic error or missing database}} +} {1 {SQL logic error}} do_catchsql_test 9.5.2 { INSERT INTO abc(abc, rank) VALUES('hashsize', -500000); -} {1 {SQL logic error or missing database}} +} {1 {SQL logic error}} do_catchsql_test 9.5.3 { INSERT INTO abc(abc, rank) VALUES('hashsize', 500000); } {0 {}} @@ -245,7 +244,22 @@ foreach {tn opt} { do_catchsql_test 12.1 { INSERT INTO t1(t1, rank) VALUES('rank', NULL);; -} {1 {SQL logic error or missing database}} +} {1 {SQL logic error}} -finish_test +#------------------------------------------------------------------------- +# errors in the 'usermerge' option +# +do_execsql_test 13.0 { + CREATE VIRTUAL TABLE tt USING fts5(ttt); +} +foreach {tn val} { + 1 -1 + 2 4.2 + 3 17 + 4 1 +} { + set sql "INSERT INTO tt(tt, rank) VALUES('usermerge', $val)" + do_catchsql_test 13.$tn $sql {1 {SQL logic error}} +} +finish_test diff --git a/ext/fts5/test/fts5conflict.test b/ext/fts5/test/fts5conflict.test index 5c1e593249..b5bf0a1160 100644 --- a/ext/fts5/test/fts5conflict.test +++ b/ext/fts5/test/fts5conflict.test @@ -65,6 +65,44 @@ do_execsql_test 2.1 { INSERT INTO fts_idx(fts_idx) VALUES('integrity-check'); } -finish_test +#------------------------------------------------------------------------- +# Tests for OR IGNORE conflict handling. +# +reset_db +foreach_detail_mode $::testprefix { + + do_execsql_test 3.0 { + CREATE VIRTUAL TABLE t1 USING fts5(xyz, detail=%DETAIL%); + + BEGIN; + INSERT INTO t1(rowid, xyz) VALUES(13, 'thirteen documents'); + INSERT INTO t1(rowid, xyz) VALUES(14, 'fourteen documents'); + INSERT INTO t1(rowid, xyz) VALUES(15, 'fifteen documents'); + COMMIT; + } + set db_cksum [cksum] + foreach {tn sql} { + 1 { + INSERT OR IGNORE INTO t1(rowid, xyz) VALUES(14, 'new text'); + } + 2 { + UPDATE OR IGNORE t1 SET rowid=13 WHERE rowid=15; + } + 3 { + INSERT OR IGNORE INTO t1(rowid, xyz) + SELECT 13, 'some text' + UNION ALL + SELECT 14, 'some text' + UNION ALL + SELECT 15, 'some text' + } + } { + do_execsql_test 3.1.$tn.1 $sql + do_test 3.1.$tn.2 { cksum } $db_cksum + } +} + + +finish_test diff --git a/ext/fts5/test/fts5connect.test b/ext/fts5/test/fts5connect.test new file mode 100644 index 0000000000..46077340ca --- /dev/null +++ b/ext/fts5/test/fts5connect.test @@ -0,0 +1,246 @@ +# 2017 August 17 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# + + + +source [file join [file dirname [info script]] fts5_common.tcl] +set testprefix fts5connect + +ifcapable !fts5 { + finish_test + return +} + +#------------------------------------------------------------------------- +# The tests in this file test the outcome of a schema-reset happening +# within the xConnect() method of an FTS5 table. At one point this +# was causing a problem in SQLite. Each test proceeds as follows: +# +# 1. Connection [db] opens the db and reads from some unrelated, non-FTS5 +# table causing SQLite to load the db schema into memory. +# +# 2. Connection [db2] opens the db and modifies the db schema. +# +# 3. Connection [db] reads or writes an existing fts5 table. That the +# schema has been modified is detected inside the fts5 xConnect() +# callback that is invoked by sqlite3_prepare(). +# +# 4. Verify that the statement in 3 has worked. SQLite should detect +# that the schema has changed and successfully prepare the +# statement against the new schema. +# +# Test plan: +# +# 1.*: Trigger the xConnect()/schema-reset using statements executed +# directly against an FTS5 table. +# +# 2.*: Using various statements executed by various BEFORE triggers. +# +# 3.*: Using various statements executed by various AFTER triggers. +# +# 4.*: Using various statements executed by various INSTEAD OF triggers. +# + + + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE ft1 USING fts5(a, b); + CREATE TABLE abc(x INTEGER PRIMARY KEY); + CREATE TABLE t1(i INTEGER PRIMARY KEY, a, b); + + INSERT INTO ft1 VALUES('one', 'two'); + INSERT INTO ft1 VALUES('three', 'four'); +} + +foreach {tn sql res} { + 1 "SELECT * FROM ft1" {one two three four} + 2 "REPLACE INTO ft1(rowid, a, b) VALUES(1, 'five', 'six')" {} + 3 "SELECT * FROM ft1" {five six three four} + 4 "INSERT INTO ft1 VALUES('seven', 'eight')" {} + 5 "SELECT * FROM ft1" {five six three four seven eight} + 6 "DELETE FROM ft1 WHERE rowid=2" {} + 7 "UPDATE ft1 SET b='nine' WHERE rowid=1" {} + 8 "SELECT * FROM ft1" {five nine seven eight} +} { + + catch { db close } + catch { db2 close } + sqlite3 db test.db + sqlite3 db2 test.db + + do_test 1.$tn.1 { + db eval { INSERT INTO abc DEFAULT VALUES } + db2 eval { CREATE TABLE newtable(x,y); DROP TABLE newtable } + } {} + + do_execsql_test 1.$tn.2 $sql $res + + do_execsql_test 1.$tn.3 { + INSERT INTO ft1(ft1) VALUES('integrity-check'); + } +} + +do_execsql_test 2.0 { + CREATE VIRTUAL TABLE ft2 USING fts5(a, b); + CREATE TABLE t2(a, b); + CREATE TABLE log(txt); + + CREATE TRIGGER t2_ai AFTER INSERT ON t2 BEGIN + INSERT INTO ft2(rowid, a, b) VALUES(new.rowid, new.a, new.b); + INSERT INTO log VALUES('insert'); + END; + + CREATE TRIGGER t2_ad AFTER DELETE ON t2 BEGIN + DELETE FROM ft2 WHERE rowid = old.rowid; + INSERT INTO log VALUES('delete'); + END; + + CREATE TRIGGER t2_au AFTER UPDATE ON t2 BEGIN + UPDATE ft2 SET a=new.a, b=new.b WHERE rowid=new.rowid; + INSERT INTO log VALUES('update'); + END; + + INSERT INTO t2 VALUES('one', 'two'); + INSERT INTO t2 VALUES('three', 'four'); +} + +foreach {tn sql res} { + 1 "SELECT * FROM t2" {one two three four} + 2 "REPLACE INTO t2(rowid, a, b) VALUES(1, 'five', 'six')" {} + 3 "SELECT * FROM ft2" {five six three four} + 4 "INSERT INTO t2 VALUES('seven', 'eight')" {} + 5 "SELECT * FROM ft2" {five six three four seven eight} + 6 "DELETE FROM t2 WHERE rowid=2" {} + 7 "UPDATE t2 SET b='nine' WHERE rowid=1" {} + 8 "SELECT * FROM ft2" {five nine seven eight} +} { + + catch { db close } + catch { db2 close } + sqlite3 db test.db + sqlite3 db2 test.db + + do_test 2.$tn.1 { + db eval { INSERT INTO abc DEFAULT VALUES } + db2 eval { CREATE TABLE newtable(x,y); DROP TABLE newtable } + } {} + + do_execsql_test 2.$tn.2 $sql $res + + do_execsql_test 2.$tn.3 { + INSERT INTO ft2(ft2) VALUES('integrity-check'); + } +} + +do_execsql_test 3.0 { + CREATE VIRTUAL TABLE ft3 USING fts5(a, b); + CREATE TABLE t3(a, b); + + CREATE TRIGGER t3_ai BEFORE INSERT ON t3 BEGIN + INSERT INTO ft3(rowid, a, b) VALUES(new.rowid, new.a, new.b); + INSERT INTO log VALUES('insert'); + END; + + CREATE TRIGGER t3_ad BEFORE DELETE ON t3 BEGIN + DELETE FROM ft3 WHERE rowid = old.rowid; + INSERT INTO log VALUES('delete'); + END; + + CREATE TRIGGER t3_au BEFORE UPDATE ON t3 BEGIN + UPDATE ft3 SET a=new.a, b=new.b WHERE rowid=new.rowid; + INSERT INTO log VALUES('update'); + END; + + INSERT INTO t3(rowid, a, b) VALUES(1, 'one', 'two'); + INSERT INTO t3(rowid, a, b) VALUES(2, 'three', 'four'); +} + +foreach {tn sql res} { + 1 "SELECT * FROM t3" {one two three four} + 2 "REPLACE INTO t3(rowid, a, b) VALUES(1, 'five', 'six')" {} + 3 "SELECT * FROM ft3" {five six three four} + 4 "INSERT INTO t3(rowid, a, b) VALUES(3, 'seven', 'eight')" {} + 5 "SELECT * FROM ft3" {five six three four seven eight} + 6 "DELETE FROM t3 WHERE rowid=2" {} + 7 "UPDATE t3 SET b='nine' WHERE rowid=1" {} + 8 "SELECT * FROM ft3" {five nine seven eight} +} { + + catch { db close } + catch { db2 close } + sqlite3 db test.db + sqlite3 db2 test.db + + do_test 3.$tn.1 { + db eval { INSERT INTO abc DEFAULT VALUES } + db2 eval { CREATE TABLE newtable(x,y); DROP TABLE newtable } + } {} + + do_execsql_test 3.$tn.2 $sql $res + + do_execsql_test 3.$tn.3 { + INSERT INTO ft3(ft3) VALUES('integrity-check'); + } +} + +do_execsql_test 4.0 { + CREATE VIRTUAL TABLE ft4 USING fts5(a, b); + CREATE VIEW v4 AS SELECT rowid, * FROM ft4; + + CREATE TRIGGER t4_ai INSTEAD OF INSERT ON v4 BEGIN + INSERT INTO ft4(rowid, a, b) VALUES(new.rowid, new.a, new.b); + INSERT INTO log VALUES('insert'); + END; + + CREATE TRIGGER t4_ad INSTEAD OF DELETE ON v4 BEGIN + DELETE FROM ft4 WHERE rowid = old.rowid; + INSERT INTO log VALUES('delete'); + END; + + CREATE TRIGGER t4_au INSTEAD OF UPDATE ON v4 BEGIN + UPDATE ft4 SET a=new.a, b=new.b WHERE rowid=new.rowid; + INSERT INTO log VALUES('update'); + END; + + INSERT INTO ft4(rowid, a, b) VALUES(1, 'one', 'two'); + INSERT INTO ft4(rowid, a, b) VALUES(2, 'three', 'four'); +} + +foreach {tn sql res} { + 1 "SELECT * FROM ft4" {one two three four} + 2 "REPLACE INTO v4(rowid, a, b) VALUES(1, 'five', 'six')" {} + 3 "SELECT * FROM ft4" {five six three four} + 4 "INSERT INTO v4(rowid, a, b) VALUES(3, 'seven', 'eight')" {} + 5 "SELECT * FROM ft4" {five six three four seven eight} + 6 "DELETE FROM v4 WHERE rowid=2" {} + 7 "UPDATE v4 SET b='nine' WHERE rowid=1" {} + 8 "SELECT * FROM ft4" {five nine seven eight} +} { + + catch { db close } + catch { db2 close } + sqlite3 db test.db + sqlite3 db2 test.db + + do_test 4.$tn.1 { + db eval { INSERT INTO abc DEFAULT VALUES } + db2 eval { CREATE TABLE newtable(x,y); DROP TABLE newtable } + } {} + + do_execsql_test 4.$tn.2 $sql $res + + do_execsql_test 4.$tn.3 { + INSERT INTO ft3(ft3) VALUES('integrity-check'); + } +} + +finish_test diff --git a/ext/fts5/test/fts5content.test b/ext/fts5/test/fts5content.test index 69e66a54f8..05b5cc6113 100644 --- a/ext/fts5/test/fts5content.test +++ b/ext/fts5/test/fts5content.test @@ -15,7 +15,7 @@ source [file join [file dirname [info script]] fts5_common.tcl] set testprefix fts5content -# If SQLITE_ENABLE_FTS5 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return @@ -253,6 +253,116 @@ do_execsql_test 6.2 { SELECT name FROM sqlite_master; } {} +#--------------------------------------------------------------------------- +# Check that an fts5 table cannot be its own content table. +# +reset_db +do_execsql_test 7.1.1 { + CREATE VIRTUAL TABLE t1 USING fts5(a, c=t1 ); + INSERT INTO t1( a ) VALUES('abc'); +} +do_catchsql_test 7.1.2 { + SELECT * FROM t1; +} {1 {recursively defined fts5 content table}} +do_catchsql_test 7.1.3 { + SELECT * FROM t1('abc'); +} {1 {recursively defined fts5 content table}} +do_catchsql_test 7.1.4 { + SELECT count(*) FROM t1; +} {1 {recursively defined fts5 content table}} +do_catchsql_test 7.1.5 { + SELECT * FROM t1('abc') ORDER BY rank; +} {1 {recursively defined fts5 content table}} + +reset_db +do_execsql_test 7.2.1 { + CREATE VIRTUAL TABLE t1 USING fts5(a, c=t2 ); + CREATE VIRTUAL TABLE t2 USING fts5(a, c=t1 ); + INSERT INTO t1( a ) VALUES('abc'); +} +do_catchsql_test 7.2.2 { + SELECT * FROM t1; +} {1 {recursively defined fts5 content table}} +do_catchsql_test 7.2.3 { + SELECT * FROM t1('abc'); +} {1 {recursively defined fts5 content table}} +do_catchsql_test 7.2.4 { + SELECT count(*) FROM t1; +} {1 {recursively defined fts5 content table}} +do_catchsql_test 7.2.5 { + SELECT * FROM t1('abc') ORDER BY rank; +} {1 {recursively defined fts5 content table}} + +#--------------------------------------------------------------------------- +# Check that if the content table is a view, and that view contains an +# error, a reasonable error message is returned if the user tries to +# read from the view via the fts5 table. +# +reset_db +do_execsql_test 8.1 { + CREATE VIEW a1 AS + SELECT 1 AS r, text_value(1) AS t + UNION ALL + SELECT 2 AS r, text_value(2) AS t; + + CREATE VIRTUAL TABLE t1 USING fts5(t, content='a1', content_rowid='r'); +} + +foreach {tn sql} { + 1 "SELECT * FROM t1" + 2 "INSERT INTO t1(t1) VALUES('rebuild')" + 3 "SELECT * FROM t1 WHERE rowid=1" +} { + do_catchsql_test 8.2.$tn $sql {1 {no such function: text_value}} +} + +proc text_value {i} { + if {$i==1} { return "one" } + if {$i==2} { return "two" } + return "many" +} +db func text_value text_value + +do_execsql_test 8.3.1 { SELECT * FROM t1 } {one two} +do_execsql_test 8.3.2 { INSERT INTO t1(t1) VALUES('rebuild') } +do_execsql_test 8.3.3 { SELECT * FROM t1 WHERE rowid=1 } {one} +do_execsql_test 8.3.4 { SELECT rowid FROM t1('two') } {2} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 9.1 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b); + INSERT INTO t1 VALUES(1, 'one two three'); + INSERT INTO t1 VALUES(2, 'one two three'); + + CREATE VIRTUAL TABLE ft USING fts5(b, content=t1, content_rowid=a); + INSERT INTO ft(ft) VALUES('rebuild'); +} + +do_execsql_test 9.2 { + SELECT rowid, b FROM ft('two'); +} { + 1 {one two three} + 2 {one two three} +} + +do_execsql_test 9.3 { + DELETE FROM t1 WHERE a=2; +} + +do_catchsql_test 9.4 { + SELECT rowid FROM ft('two'); +} {0 {1 2}} + +do_catchsql_test 9.5 { + SELECT * FROM ft('two'); +} {1 {fts5: missing row 2 from content table 'main'.'t1'}} + +fts5_aux_test_functions db + +do_catchsql_test 9.6 { + SELECT rowid, fts5_columntext(ft, 0) FROM ft('two'); +} {1 SQLITE_CORRUPT_VTAB} finish_test diff --git a/ext/fts5/test/fts5contentless.test b/ext/fts5/test/fts5contentless.test new file mode 100644 index 0000000000..991e9888fc --- /dev/null +++ b/ext/fts5/test/fts5contentless.test @@ -0,0 +1,290 @@ +# 2014 Dec 20 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# This file contains tests for the content= and content_rowid= options. +# + +source [file join [file dirname [info script]] fts5_common.tcl] +set testprefix fts5contentless + +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. +ifcapable !fts5 { + finish_test + return +} + +# Check that it is not possible to specify "contentless_delete=1" for +# anything other than a contentless table. +# +set res(0) {0 {}} +set res(1) {1 {contentless_delete=1 requires a contentless table}} +foreach {tn sql bError} { + 1 "(a, b, contentless_delete=1)" 1 + 2 "(a, b, contentless_delete=1, content=abc)" 1 + 3 "(a, b, contentless_delete=1, content=)" 0 + 4 "(content=, contentless_delete=1, a)" 0 + 5 "(content='', contentless_delete=1, hello)" 0 +} { + execsql { BEGIN } + do_catchsql_test 1.$tn "CREATE VIRTUAL TABLE t1 USING fts5 $sql" $res($bError) + execsql { ROLLBACK } +} + +# Check that it is not possible to specify "contentless_delete=1" +# along with columnsize=1. +# +set res(0) {0 {}} +set res(1) {1 {contentless_delete=1 is incompatible with columnsize=0}} +foreach {tn sql bError} { + 2 "(a, b, content='', contentless_delete=1, columnsize=0)" 1 +} { + execsql { BEGIN } + do_catchsql_test 1.$tn "CREATE VIRTUAL TABLE t1 USING fts5 $sql" $res($bError) + execsql { ROLLBACK } +} + +# Check that if contentless_delete=1 is specified, then the "origin" +# column is added to the %_docsize table. +reset_db +do_execsql_test 3.0 { + CREATE VIRTUAL TABLE x1 USING fts5(c, content=''); + CREATE VIRTUAL TABLE x2 USING fts5(c, content='', contentless_delete=1); +} +do_execsql_test 3.1 { + SELECT sql FROM sqlite_schema WHERE name IN ('x1_docsize', 'x2_docsize'); +} { + {CREATE TABLE 'x1_docsize'(id INTEGER PRIMARY KEY, sz BLOB)} + {CREATE TABLE 'x2_docsize'(id INTEGER PRIMARY KEY, sz BLOB, origin INTEGER)} +} + +do_execsql_test 3.2.1 { + SELECT hex(block) FROM x1_data WHERE id=10 +} {00000000000000} +do_execsql_test 3.2.2 { + SELECT hex(block) FROM x2_data WHERE id=10 +} {00000000FF000001000000} + +do_execsql_test 3.3 { + INSERT INTO x2 VALUES('first text'); + INSERT INTO x2 VALUES('second text'); +} +do_execsql_test 3.4 { + SELECT id, origin FROM x2_docsize +} {1 1 2 2} +do_execsql_test 3.5 { + SELECT level, segment, loc1, loc2 FROM fts5_structure( + (SELECT block FROM x2_data WHERE id=10) + ) +} { + 0 0 1 1 + 0 1 2 2 +} +do_execsql_test 3.6 { + INSERT INTO x2(x2) VALUES('optimize'); +} +do_execsql_test 3.7 { + SELECT level, segment, loc1, loc2 FROM fts5_structure( + (SELECT block FROM x2_data WHERE id=10) + ) +} { + 1 0 1 2 +} + +do_execsql_test 3.8 { + DELETE FROM x2 WHERE rowid=2; +} + +do_execsql_test 3.9 { + SELECT rowid FROM x2('text') +} {1} + +#-------------------------------------------------------------------------- +reset_db +proc document {n} { + set vocab [list A B C D E F G H I J K L M N O P Q R S T U V W X Y Z] + set ret [list] + for {set ii 0} {$ii < $n} {incr ii} { + lappend ret [lindex $vocab [expr int(rand()*[llength $vocab])]] + } + set ret +} + +set nRow 1000 + +do_execsql_test 4.0 { + CREATE TABLE t1(x); + CREATE VIRTUAL TABLE ft USING fts5(x, content='', contentless_delete=1); + INSERT INTO ft(ft, rank) VALUES('pgsz', 100); +} +do_test 4.1 { + for {set ii 0} {$ii < $nRow} {incr ii} { + set doc [document 6] + execsql { + INSERT INTO t1 VALUES($doc); + INSERT INTO ft VALUES($doc); + } + } +} {} + +foreach v {A B C D E F G H I J K L M N O P Q R S T U V W X Y Z} { + set L1 [execsql {SELECT rowid FROM t1 WHERE x LIKE '%'||$v||'%'}] + set L2 [execsql {SELECT rowid FROM ft($v)}] + do_test 4.2.$v { set L1 } $L2 +} + +do_test 4.3 { + for {set ii 1} {$ii < $nRow} {incr ii 2} { + execsql { + DELETE FROM ft WHERE rowid=$ii; + DELETE FROM t1 WHERE rowid=$ii; + } + } +} {} + +foreach v {A B C D E F G H I J K L M N O P Q R S T U V W X Y Z} { + set L1 [execsql {SELECT rowid FROM t1 WHERE x LIKE '%'||$v||'%'}] + set L2 [execsql {SELECT rowid FROM ft($v)}] + do_test 4.4.$v { set L1 } $L2 +} + +do_execsql_test 4.5 { + INSERT INTO ft(ft) VALUES('optimize'); +} {} + +foreach v {A B C D E F G H I J K L M N O P Q R S T U V W X Y Z} { + set L1 [execsql {SELECT rowid FROM t1 WHERE x LIKE '%'||$v||'%'}] + set L2 [execsql {SELECT rowid FROM ft($v)}] + do_test 4.6.$v { set L1 } $L2 +} + +#execsql_pp { SELECT fts5_decode(id, block) FROM ft_data } + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 5.0 { + CREATE VIRTUAL TABLE ft USING fts5(x, content='', contentless_delete=1); + INSERT INTO ft(rowid, x) VALUES(1, 'one two three'); + INSERT INTO ft(rowid, x) VALUES(2, 'one two four'); + INSERT INTO ft(rowid, x) VALUES(3, 'one two five'); + INSERT INTO ft(rowid, x) VALUES(4, 'one two seven'); + INSERT INTO ft(rowid, x) VALUES(5, 'one two eight'); +} + +do_execsql_test 5.1 { + DELETE FROM ft WHERE rowid=2 +} + +do_execsql_test 5.2 { + SELECT rowid FROM ft +} {1 3 4 5} + +do_catchsql_test 5.3 { + UPDATE ft SET x='four six' WHERE rowid=3 +} {0 {}} + +do_execsql_test 5.4 { + SELECT rowid FROM ft('one'); +} {1 4 5} + +do_execsql_test 5.5 { + REPLACE INTO ft(rowid, x) VALUES(3, 'four six'); + SELECT rowid FROM ft('one'); +} {1 4 5} + +do_execsql_test 5.6 { + REPLACE INTO ft(rowid, x) VALUES(6, 'one two eleven'); + SELECT rowid FROM ft('one'); +} {1 4 5 6} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 6.0 { + CREATE VIRTUAL TABLE ft USING fts5(x, content='', contentless_delete=1); + INSERT INTO ft(rowid, x) VALUES(1, 'one two three'); + INSERT INTO ft(rowid, x) VALUES(2, 'one two four'); +} + +do_test 6.1 { + db eval { SELECT rowid FROM ft('one two') } { + if {$rowid==1} { + db eval { INSERT INTO ft(rowid, x) VALUES(3, 'one two four') } + } + } +} {} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 7.0 { + CREATE VIRTUAL TABLE ft USING fts5(x, content='', contentless_delete=1); +} + +set lRowid [list -450 0 1 2 42] + +do_test 7.1 { + execsql BEGIN + foreach r $lRowid { + execsql { INSERT INTO ft(rowid, x) VALUES($r, 'one one one'); } + } + execsql COMMIT +} {} + +do_test 7.2 { + execsql BEGIN + foreach r $lRowid { + execsql { REPLACE INTO ft(rowid, x) VALUES($r, 'two two two'); } + } + execsql COMMIT +} {} + +do_execsql_test 7.3 { SELECT rowid FROM ft('one'); } {} +do_execsql_test 7.4 { SELECT rowid FROM ft('two'); } $lRowid + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 8.0 { + CREATE VIRTUAL TABLE ft USING fts5(x, content='', contentless_delete=1); + INSERT INTO ft VALUES('hello world'); + INSERT INTO ft VALUES('one two three'); +} + +do_catchsql_test 8.1 { + INSERT INTO ft(ft, rowid, x) VALUES('delete', 1, 'hello world'); +} {1 {'delete' may not be used with a contentless_delete=1 table}} + +do_execsql_test 8.2 { + BEGIN; + INSERT INTO ft(rowid, x) VALUES(3, 'four four four'); + DELETE FROM ft WHERE rowid=3; + COMMIT; + SELECT rowid FROM ft('four'); +} {} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 9.0 { + CREATE VIRTUAL TABLE ft USING fts5(x, content='', contentless_delete=0); + INSERT INTO ft VALUES('hello world'); + INSERT INTO ft VALUES('one two three'); +} + +do_catchsql_test 9.1 { + INSERT INTO ft(ft, rowid, x) VALUES('delete', 1, 'hello world'); +} {0 {}} + +do_catchsql_test 9.2 { + CREATE VIRTUAL TABLE ft2 USING fts5(x, content='', contentless_delete=2); +} {1 {malformed contentless_delete=... directive}} + +do_catchsql_test 9.3 { + CREATE VIRTUAL TABLE ft2 USING fts5(x, content='', contentless_delete=11); +} {1 {malformed contentless_delete=... directive}} + +finish_test diff --git a/ext/fts5/test/fts5contentless2.test b/ext/fts5/test/fts5contentless2.test new file mode 100644 index 0000000000..248534bce4 --- /dev/null +++ b/ext/fts5/test/fts5contentless2.test @@ -0,0 +1,207 @@ +# 2023 July 19 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# This file contains tests for the content= and content_rowid= options. +# + +source [file join [file dirname [info script]] fts5_common.tcl] +set testprefix fts5contentless2 + +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. +ifcapable !fts5 { + finish_test + return +} + +proc vocab {} { + list aaa bbb ccc ddd eee fff ggg hhh iii jjj kkk lll mmm nnn ooo ppp +} + +proc document {nToken} { + set doc [list] + set vocab [vocab] + for {set ii 0} {$ii < $nToken} {incr ii} { + lappend doc [lindex $vocab [expr int(rand()*[llength $vocab])]] + } + set doc +} +db func document document + +proc contains {doc token} { + expr {[lsearch $doc $token]>=0} +} +db func contains contains + +proc do_compare_tables_test {tn} { + uplevel [list do_test $tn { + foreach v [vocab] { + set l1 [execsql { SELECT rowid FROM t1 WHERE contains(doc, $v) }] + set l2 [execsql { SELECT rowid FROM t2($v) }] + if {$l1!=$l2} { error "1: query mismatch ($l1) ($l2)" } + + set w "[string range $v 0 1]*" + set l1 [execsql { SELECT rowid FROM t1 WHERE contains(doc, $w) }] + set l2 [execsql { SELECT rowid FROM t2($w) }] + if {$l1!=$l2} { error "2: query mismatch ($l1) ($l2)" } + + set w "[string range $v 0 0]*" + set l1 [execsql { SELECT rowid FROM t1 WHERE contains(doc, $w) }] + set l2 [execsql { SELECT rowid FROM t2($w) }] + if {$l1!=$l2} { error "2: query mismatch ($l1) ($l2)" } + + set l1 [execsql { + SELECT rowid FROM t1 WHERE contains(doc, $v) ORDER BY rowid DESC + }] + set l2 [execsql { SELECT rowid FROM t2($v) ORDER BY rowid DESC }] + if {$l1!=$l2} { error "1: query mismatch ($l1) ($l2)" } + } + set {} {} + } {}] +} + +proc lshuffle {in} { + set L [list] + set ret [list] + foreach elem $in { lappend L [list [expr rand()] $elem] } + foreach pair [lsort -index 0 $L] { lappend ret [lindex $pair 1] } + set ret +} + +expr srand(0) + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE t2 USING fts5( + doc, prefix=2, content=, contentless_delete=1 + ); + + CREATE TABLE t1(doc); + CREATE TRIGGER tr1 AFTER DELETE ON t1 BEGIN + DELETE FROM t2 WHERE rowid = old.rowid; + END; +} + +set SMALLEST64 -9223372036854775808 +set LARGEST64 9223372036854775807 + +foreach {tn r1 r2} { + 1 0 50 + 2 $SMALLEST64 $SMALLEST64+50 + 3 $LARGEST64-50 $LARGEST64 + 4 -50 -1 +} { + set r1 [expr $r1] + set r2 [expr $r2] + + do_test 1.1.$tn { + execsql BEGIN + for {set ii $r1} {$ii <= $r2} {incr ii} { + execsql { INSERT INTO t1(rowid, doc) VALUES ($ii, document(8)); } + } + execsql COMMIT + } {} +} +do_test 1.2 { + db eval { SELECT rowid, doc FROM t1 } { + execsql { INSERT INTO t2(rowid, doc) VALUES($rowid, $doc) } + } +} {} + +foreach {tn rowid} { + 1 $SMALLEST64 + 2 0 + 3 -5 + 4 -30 + 5 $LARGEST64 + 6 $LARGEST64-1 +} { + set rowid [expr $rowid] + do_execsql_test 1.3.$tn.1 { + DELETE FROM t1 WHERE rowid=$rowid + } + do_compare_tables_test 1.3.$tn.2 +} + +set iTest 1 +foreach r [lshuffle [execsql {SELECT rowid FROM t1}]] { + if {($iTest % 50)==0} { + execsql { INSERT INTO t2(t2) VALUES('optimize') } + } + if {($iTest % 5)==0} { + execsql { INSERT INTO t2(t2, rank) VALUES('merge', 5) } + } + do_execsql_test 1.4.$iTest.1($r) { + DELETE FROM t1 WHERE rowid=$r + } + do_compare_tables_test 1.4.$iTest.2 + incr iTest +} + +do_execsql_test 1.5 { + SELECT * FROM t1 +} {} + +#------------------------------------------------------------------------- +reset_db +db func document document + +do_execsql_test 2.0 { + CREATE VIRTUAL TABLE t2 USING fts5(doc, content=, contentless_delete=1); + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<1000 + ) + INSERT INTO t2(rowid, doc) SELECT i, i || ' ' || i FROM s; +} + +do_execsql_test 2.1 { + BEGIN; + DELETE FROM t2 WHERE rowid=32; + DELETE FROM t2 WHERE rowid=64; + DELETE FROM t2 WHERE rowid=96; + DELETE FROM t2 WHERE rowid=128; + DELETE FROM t2 WHERE rowid=160; + DELETE FROM t2 WHERE rowid=192; + COMMIT; +} + +do_execsql_test 2.2 { + SELECT * FROM t2('128'); +} {} + +#------------------------------------------------------------------------- + +foreach {tn step} { + 1 3 + 2 7 + 3 15 +} { + set step [expr $step] + + reset_db + db func document document + do_execsql_test 3.$tn.0 { + CREATE VIRTUAL TABLE t2 USING fts5(doc, content=, contentless_delete=1); + INSERT INTO t2(t2, rank) VALUES('pgsz', 100); + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<1000 + ) + INSERT INTO t2(rowid, doc) SELECT i, i || ' ' || i FROM s; + } + do_execsql_test 3.$tn.1 { + DELETE FROM t2 WHERE (rowid % $step)==0 + } + do_execsql_test 3.$tn.2 { + SELECT * FROM t2( $step * 5 ) + } {} +} + + + +finish_test diff --git a/ext/fts5/test/fts5contentless3.test b/ext/fts5/test/fts5contentless3.test new file mode 100644 index 0000000000..693840da82 --- /dev/null +++ b/ext/fts5/test/fts5contentless3.test @@ -0,0 +1,195 @@ +# 2023 July 21 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# This file contains tests for the content= and content_rowid= options. +# + +source [file join [file dirname [info script]] fts5_common.tcl] +set testprefix fts5contentless3 + +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. +ifcapable !fts5 { + finish_test + return +} + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE ft USING fts5(x, content=, contentless_delete=1); + BEGIN; + INSERT INTO ft VALUES('one one one'); + INSERT INTO ft VALUES('two two two'); + INSERT INTO ft VALUES('three three three'); + INSERT INTO ft VALUES('four four four'); + INSERT INTO ft VALUES('five five five'); + INSERT INTO ft VALUES('six six six'); + INSERT INTO ft VALUES('seven seven seven'); + INSERT INTO ft VALUES('eight eight eight'); + INSERT INTO ft VALUES('nine nine nine'); + COMMIT; + + DELETE FROM ft WHERE rowid=3; +} + +proc myhex {hex} { binary decode hex $hex } +db func myhex myhex + +do_execsql_test 1.1 { + UPDATE ft_data SET block = + myhex('04000000 00000001' || + '01020304 01020304 01020304 01020304' || + '01020304 01020304 01020304 01020304' + ) + WHERE id = (SELECT max(id) FROM ft_data); +} + +do_execsql_test 1.2 { + DELETE FROM ft WHERE rowid=1 +} + +do_execsql_test 1.3 { + SELECT rowid FROM ft('two'); +} {2} + +do_execsql_test 1.3 { + UPDATE ft_data SET block = + myhex('08000000 00000001' || + '0000000001020304 0000000001020304 0000000001020304 0000000001020304' || + '0000000001020304 0000000001020304 0000000001020304 0000000001020304' + ) + WHERE id = (SELECT max(id) FROM ft_data); +} + +do_execsql_test 1.4 { + SELECT rowid FROM ft('two'); +} {2} + +do_execsql_test 1.5 { + DELETE FROM ft WHERE rowid=4 +} + +do_execsql_test 1.6 { + UPDATE ft_data SET block = myhex('04000000 00000000') + WHERE id = (SELECT max(id) FROM ft_data); +} +do_execsql_test 1.7 { + SELECT rowid FROM ft('two'); +} {2} + +do_execsql_test 1.8 { + UPDATE ft_data SET block = myhex('04000000 00000000') + WHERE id = (SELECT max(id) FROM ft_data); +} +do_execsql_test 1.9 { + DELETE FROM ft WHERE rowid=8 +} {} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 2.0 { + CREATE VIRTUAL TABLE ft USING fts5(x, content=, contentless_delete=1); + INSERT INTO ft VALUES('one one one'); + INSERT INTO ft VALUES('two two two'); + INSERT INTO ft VALUES('three three three'); + INSERT INTO ft VALUES('four four four'); + INSERT INTO ft VALUES('five five five'); + INSERT INTO ft VALUES('six six six'); + INSERT INTO ft VALUES('seven seven seven'); + INSERT INTO ft VALUES('eight eight eight'); + INSERT INTO ft VALUES('nine nine nine'); +} + +do_execsql_test 2.1 { + INSERT INTO ft(ft) VALUES('optimize'); +} +do_execsql_test 2.2 { + SELECT count(*) FROM ft_data +} {3} +do_execsql_test 2.3 { + DELETE FROM ft WHERE rowid=5 +} +do_execsql_test 2.4 { + SELECT count(*) FROM ft_data +} {4} + +# Check that an 'optimize' works (rewrites the index) if there is a single +# segment with one or more tombstone hash pages. +do_execsql_test 2.5 { + INSERT INTO ft(ft) VALUES('optimize'); +} +do_execsql_test 2.6 { + SELECT count(*) FROM ft_data +} {3} + +# Check that an 'optimize' is a no-op if there is a single segment +# and no tombstone hash pages. +do_execsql_test 2.7 { + INSERT INTO ft(ft) VALUES('optimize'); + SELECT rowid FROM ft_data; +} [db eval {SELECT rowid FROM ft_data}] + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 3.0 { + CREATE VIRTUAL TABLE ft USING fts5(x, content=, contentless_delete=1); + INSERT INTO ft(ft, rank) VALUES('pgsz', 64); + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<1000 + ) + INSERT INTO ft(rowid, x) SELECT i, i||' '||i||' '||i||' '||i FROM s; + INSERT INTO ft(ft) VALUES('optimize'); +} + +do_execsql_test 3.1 { + SELECT count(*) FROM ft_data +} {200} + +do_execsql_test 3.2 { + DELETE FROM ft WHERE (rowid % 50)==0; + SELECT count(*) FROM ft_data; +} {203} + +do_execsql_test 3.3 { + INSERT INTO ft(ft, rank) VALUES('merge', 500); + SELECT rowid FROM ft_data; +} [db eval {SELECT rowid FROM ft_data}] + +do_execsql_test 3.4 { + INSERT INTO ft(ft, rank) VALUES('merge', -1000); + SELECT count(*) FROM ft_data; +} {197} + +do_execsql_test 3.5 { + DELETE FROM ft WHERE (rowid % 50)==1; + SELECT count(*) FROM ft_data; +} {200} + +do_execsql_test 3.6 { + SELECT level, segment, npgtombstone FROM fts5_structure( + (SELECT block FROM ft_data WHERE id=10) + ) +} {1 0 3} + +do_test 3.6 { + while 1 { + set nChange [db total_changes] + execsql { INSERT INTO ft(ft, rank) VALUES('merge', -5) } + if {([db total_changes] - $nChange)<2} break + } +} {} + +do_execsql_test 3.7 { + SELECT level, segment, npgtombstone FROM fts5_structure( + (SELECT block FROM ft_data WHERE id=10) + ) +} {2 0 0} + + +finish_test diff --git a/ext/fts5/test/fts5contentless4.test b/ext/fts5/test/fts5contentless4.test new file mode 100644 index 0000000000..7fdf8c4b01 --- /dev/null +++ b/ext/fts5/test/fts5contentless4.test @@ -0,0 +1,247 @@ +# 2023 July 21 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# This file contains tests for the content= and content_rowid= options. +# + +source [file join [file dirname [info script]] fts5_common.tcl] +set testprefix fts5contentless4 + +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. +ifcapable !fts5 { + finish_test + return +} + +proc document {n} { + set vocab [list A B C D E F G H I J K L M N O P Q R S T U V W X Y Z] + set ret [list] + for {set ii 0} {$ii < $n} {incr ii} { + lappend ret [lindex $vocab [expr int(rand()*[llength $vocab])]] + } + set ret +} +db func document document + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE ft USING fts5(x, content='', contentless_delete=1); + INSERT INTO ft(ft, rank) VALUES('pgsz', 240); + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<1000 + ) + INSERT INTO ft SELECT document(12) FROM s; +} + +do_execsql_test 1.1 { + INSERT INTO ft(ft) VALUES('optimize'); +} + +do_execsql_test 1.2 { + SELECT level, segment, nentry, nentrytombstone FROM fts5_structure(( + SELECT block FROM ft_data WHERE id=10 + )) +} {0 0 1000 0} + +do_execsql_test 1.3 { + DELETE FROM ft WHERE rowid < 50 +} + +do_execsql_test 1.4 { + SELECT level, segment, nentry, nentrytombstone FROM fts5_structure(( + SELECT block FROM ft_data WHERE id=10 + )) +} {0 0 1000 49} + +do_execsql_test 1.5 { + DELETE FROM ft WHERE rowid < 1000 +} + +do_execsql_test 1.6 { + SELECT level, segment, nentry, nentrytombstone FROM fts5_structure(( + SELECT block FROM ft_data WHERE id=10 + )) +} {1 0 1 0} + +#-------------------------------------------------------------------------- +reset_db +db func document document + +do_execsql_test 2.0 { + CREATE VIRTUAL TABLE ft USING fts5(x, content='', contentless_delete=1); +} + +do_test 2.1 { + for {set ii 0} {$ii < 5000} {incr ii} { + execsql { INSERT INTO ft VALUES( document(12) ) } + } +} {} + +do_execsql_test 2.2 { + SELECT sum(nentry) - sum(nentrytombstone) FROM fts5_structure(( + SELECT block FROM ft_data WHERE id=10 + )) +} {5000} + +for {set ii 5000} {$ii >= 0} {incr ii -100} { + do_execsql_test 2.3.$ii { + DELETE FROM ft WHERE rowid > $ii + } + do_execsql_test 2.3.$ii.2 { + SELECT + CAST((total(nentry) - total(nentrytombstone)) AS integer) + FROM + fts5_structure( (SELECT block FROM ft_data WHERE id=10) ) + } $ii +} + +execsql_pp { + SELECT * FROM fts5_structure(( + SELECT block FROM ft_data WHERE id=10 + )) +} + +do_test 2.4 { + for {set ii 0} {$ii < 5000} {incr ii} { + execsql { INSERT INTO ft VALUES( document(12) ) } + } +} {} + +for {set ii 1} {$ii <= 5000} {incr ii 10} { + do_execsql_test 2.3.$ii { + DELETE FROM ft WHERE rowid = $ii; + INSERT INTO ft VALUES( document(12) ); + INSERT INTO ft(ft, rank) VALUES('merge', -10); + } + + do_execsql_test 2.3.$ii.2 { + SELECT + CAST((total(nentry) - total(nentrytombstone)) AS integer) + FROM + fts5_structure( (SELECT block FROM ft_data WHERE id=10) ) + } 5000 +} + +#------------------------------------------------------------------------- +reset_db +db func document document +do_execsql_test 3.0 { + CREATE VIRTUAL TABLE ft USING fts5(x, content='', contentless_delete=1); + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<100 + ) + INSERT INTO ft SELECT document(12) FROM s; +} + +do_catchsql_test 3.1 { + INSERT INTO ft(ft, rank) VALUES('deletemerge', 'text'); +} {1 {SQL logic error}} +do_catchsql_test 3.2 { + INSERT INTO ft(ft, rank) VALUES('deletemerge', 50); +} {0 {}} +do_execsql_test 3.3 { + SELECT * FROM ft_config WHERE k='deletemerge' +} {deletemerge 50} +do_catchsql_test 3.4 { + INSERT INTO ft(ft, rank) VALUES('deletemerge', 101); +} {0 {}} +do_execsql_test 3.5 { + SELECT * FROM ft_config WHERE k='deletemerge' +} {deletemerge 101} + +do_execsql_test 3.6 { + DELETE FROM ft WHERE rowid<95 +} + +do_execsql_test 3.7 { + SELECT nentrytombstone, nentry FROM fts5_structure(( + SELECT block FROM ft_data WHERE id=10 + )) +} {94 100} + +do_execsql_test 3.8 { + DELETE FROM ft WHERE rowid=95 +} + +do_execsql_test 3.9 { + SELECT nentrytombstone, nentry FROM fts5_structure(( + SELECT block FROM ft_data WHERE id=10 + )) +} {95 100} + +do_execsql_test 3.10 { + DELETE FROM ft; + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<100 + ) + INSERT INTO ft SELECT document(12) FROM s; + INSERT INTO ft(ft, rank) VALUES('deletemerge', 50); +} + +do_execsql_test 3.11 { + DELETE FROM ft WHERE rowid<95 +} + +do_execsql_test 3.12 { + SELECT nentrytombstone, nentry FROM fts5_structure(( + SELECT block FROM ft_data WHERE id=10 + )) +} {0 6} + +#------------------------------------------------------------------------- +reset_db +db func document document +do_execsql_test 4.0 { + CREATE VIRTUAL TABLE x1 USING fts5(x, content='', contentless_delete=1); + INSERT INTO x1(x1, rank) VALUES('usermerge', 16); + INSERT INTO x1(x1, rank) VALUES('deletemerge', 40); + INSERT INTO x1 VALUES('one'); + INSERT INTO x1 VALUES('two'); + INSERT INTO x1 VALUES('three'); + INSERT INTO x1 VALUES('four'); + INSERT INTO x1 VALUES('five'); + INSERT INTO x1 VALUES('six'); + INSERT INTO x1 VALUES('seven'); + INSERT INTO x1 VALUES('eight'); + INSERT INTO x1 VALUES('nine'); + INSERT INTO x1 VALUES('ten'); +} + +do_execsql_test 4.1 { + SELECT level, segment FROM fts5_structure(( + SELECT block FROM x1_data WHERE id=10 + )) +} { + 0 0 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 +} + +for {set ii 1} {$ii < 4} {incr ii} { + do_execsql_test 4.2.$ii { + DELETE FROM x1 WHERE rowid = $ii; + INSERT INTO x1(x1, rank) VALUES('merge', 5); + SELECT level, segment FROM fts5_structure(( + SELECT block FROM x1_data WHERE id=10 + )) + } { + 0 0 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 + } +} + +do_execsql_test 4.3 { + DELETE FROM x1 WHERE rowid = $ii; + INSERT INTO x1(x1, rank) VALUES('merge', 5); + SELECT level, segment, nentry FROM fts5_structure(( + SELECT block FROM x1_data WHERE id=10 + )) +} { + 1 0 6 +} + +finish_test diff --git a/ext/fts5/test/fts5contentless5.test b/ext/fts5/test/fts5contentless5.test new file mode 100644 index 0000000000..86d0753286 --- /dev/null +++ b/ext/fts5/test/fts5contentless5.test @@ -0,0 +1,111 @@ +# 2023 August 7 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# This file contains tests for the content= and content_rowid= options. +# + +source [file join [file dirname [info script]] fts5_common.tcl] +set testprefix fts5contentless5 + +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. +ifcapable !fts5 { + finish_test + return +} +unset -nocomplain res + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE t1 USING fts5(a, b, c, content='', contentless_delete=1); + INSERT INTO t1 VALUES('A', 'B', 'C'); + INSERT INTO t1 VALUES('D', 'E', 'F'); + INSERT INTO t1 VALUES('G', 'H', 'I'); +} + +do_execsql_test 1.01 { + CREATE TABLE t2(x, y); + INSERT INTO t2 VALUES('x', 'y'); +} + +# explain_i "UPDATE t1 SET a='a' WHERE t1.rowid=1" +#breakpoint +#explain_i "UPDATE t1 SET a='a' FROM t2 WHERE t1.rowid=1 AND b IS NULL" + +#breakpoint +#explain_i "UPDATE t1 SET a='a' WHERE b IS NULL AND rowid=?" + +foreach {tn up err} { + 1 "UPDATE t1 SET a='a', b='b', c='c' WHERE rowid=1" 0 + 2 "UPDATE t1 SET a='a', b='b' WHERE rowid=1" 1 + 3 "UPDATE t1 SET b='b', c='c' WHERE rowid=1" 1 + 4 "UPDATE t1 SET a='a', c='c' WHERE rowid=1" 1 + 5 "UPDATE t1 SET a='a', c='c' WHERE t1.rowid=1 AND b IS NULL" 1 + 6 "UPDATE t1 SET a='a' FROM t2 WHERE t1.rowid=1" 1 + 7 "UPDATE t1 SET a='a', b='b', c='c' FROM t2 WHERE t1.rowid=1" 0 +} { + + set res(0) {0 {}} + set res(1) {1 {cannot UPDATE a subset of columns on fts5 contentless-delete table: t1}} + do_catchsql_test 1.$tn $up $res($err) +} + +#------------------------------------------------------------------------- +reset_db + +proc random {n} { expr {abs(int(rand()*$n))} } +proc select_one {list} { + set n [llength $list] + lindex $list [random $n] +} +proc vocab {} { + list abc def ghi jkl mno pqr stu vwx yza +} +proc term {} { + select_one [vocab] +} +proc document {} { + set nTerm [expr [random 3] + 7] + set doc "" + for {set ii 0} {$ii < $nTerm} {incr ii} { + lappend doc [term] + } + set doc +} +db func document document + +do_execsql_test 2.0 { + CREATE VIRTUAL TABLE ft USING fts5(a, contentless_delete=1, content=''); + INSERT INTO ft(ft, rank) VALUES('pgsz', 64); +} + +do_test 2.1 { + for {set ii 1} {$ii < 12} {incr ii} { + db transaction { + for {set jj 0} {$jj < 10} {incr jj} { + set doc [document] + execsql { INSERT INTO ft VALUES($doc); } + } + } + } +} {} + +do_test 2.2 { + foreach r [db eval {SELECT rowid FROM ft}] { + execsql { DELETE FROM ft WHERE rowid=$r } + } +} {} + +set doc [document] +do_execsql_test 2.3 { + INSERT INTO ft VALUES($doc) +} + + +finish_test diff --git a/ext/fts5/test/fts5corrupt.test b/ext/fts5/test/fts5corrupt.test index edaafb2379..8788bc2ed6 100644 --- a/ext/fts5/test/fts5corrupt.test +++ b/ext/fts5/test/fts5corrupt.test @@ -16,7 +16,7 @@ source [file join [file dirname [info script]] fts5_common.tcl] set testprefix fts5corrupt -# If SQLITE_ENABLE_FTS5 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return @@ -41,21 +41,26 @@ db_save do_execsql_test 1.2 { INSERT INTO t1(t1) VALUES('integrity-check') } set segid [lindex [fts5_level_segids t1] 0] +sqlite3_db_config db DEFENSIVE 0 do_test 1.3 { execsql { DELETE FROM t1_data WHERE rowid = fts5_rowid('segment', $segid, 4); } catchsql { INSERT INTO t1(t1) VALUES('integrity-check') } -} {1 {database disk image is malformed}} +} {1 {fts5: corruption found reading blob 137438953476 from table "t1"}} +do_execsql_test 1.3b { + PRAGMA integrity_check(t1); +} {{fts5: corruption found reading blob 137438953476 from table "t1"}} do_test 1.4 { db_restore_and_reopen + sqlite3_db_config db DEFENSIVE 0 execsql { UPDATE t1_data set block = X'00000000' || substr(block, 5) WHERE rowid = fts5_rowid('segment', $segid, 4); } catchsql { INSERT INTO t1(t1) VALUES('integrity-check') } -} {1 {database disk image is malformed}} +} {1 {fts5: corruption found reading blob 137438953476 from table "t1"}} db_restore_and_reopen #db eval {SELECT rowid, fts5_decode(rowid, block) aS r FROM t1_data} {puts $r} @@ -89,11 +94,31 @@ do_execsql_test 3.0 { do_execsql_test 3.1 { SELECT * FROM t3 WHERE t3 MATCH 'o' } {{one o} {three o} {five o}} - +sqlite3_db_config db DEFENSIVE 0 do_catchsql_test 3.1 { DELETE FROM t3_content WHERE rowid = 3; SELECT * FROM t3 WHERE t3 MATCH 'o'; +} {1 {fts5: missing row 3 from content table 'main'.'t3_content'}} + +#-------------------------------------------------------------------- +# +reset_db +do_execsql_test 4.0 { + CREATE VIRTUAL TABLE t2 USING fts5(x); + INSERT INTO t2 VALUES('one two three'); + INSERT INTO t2 VALUES('four five six'); + INSERT INTO t2 VALUES('seven eight nine'); + INSERT INTO t2 VALUES('ten eleven twelve'); +} +do_execsql_test 4.1 { + SELECT hex(block) FROM t2_data WHERE id=1; +} {040C} +do_execsql_test 4.2 { + UPDATE t2_data SET block = X'0402' WHERE id=1 +} +breakpoint +do_catchsql_test 4.3 { + DELETE FROM t2 WHERE rowid=3 } {1 {database disk image is malformed}} finish_test - diff --git a/ext/fts5/test/fts5corrupt2.test b/ext/fts5/test/fts5corrupt2.test index 3a4fcfaaed..fd2a841c7e 100644 --- a/ext/fts5/test/fts5corrupt2.test +++ b/ext/fts5/test/fts5corrupt2.test @@ -17,7 +17,7 @@ source [file join [file dirname [info script]] fts5_common.tcl] set testprefix fts5corrupt2 -# If SQLITE_ENABLE_FTS5 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return @@ -37,7 +37,7 @@ do_execsql_test 1.0 { } set mask [expr 31 << 31] -if 1 { +if 0 { # Test 1: # @@ -84,6 +84,8 @@ foreach {tno stmt} { } } +} + # Using the same database as the 1.* tests. # # Run N-1 tests, where N is the number of bytes in the rightmost leaf page @@ -97,6 +99,8 @@ foreach {tno stmt} { set lrowid [db one {SELECT max(rowid) FROM t1_data WHERE (rowid & $mask)=0}] set nbyte [db one {SELECT length(block) FROM t1_data WHERE rowid=$lrowid}] set all [db eval {SELECT rowid FROM t1}] +sqlite3_db_config db DEFENSIVE 0 +unset -nocomplain res for {set i [expr $nbyte-2]} {$i>=0} {incr i -1} { do_execsql_test 2.$i.1 { BEGIN; @@ -105,12 +109,12 @@ for {set i [expr $nbyte-2]} {$i>=0} {incr i -1} { do_catchsql_test 2.$i.2 { INSERT INTO t1(t1) VALUES('integrity-check'); - } {1 {database disk image is malformed}} + } {/1.*fts5: corruption.*/} do_test 2.$i.3 { set res [catchsql {SELECT rowid FROM t1 WHERE t1 MATCH 'x*'}] expr { - $res=="1 {database disk image is malformed}" + [string match {*fts5: corruption*} $res] || $res=="0 {$all}" } } 1 @@ -149,21 +153,24 @@ foreach {tn hdr} { execsql BEGIN set fd [db incrblob main x3_data block $rowid] - fconfigure $fd -encoding binary -translation binary + fconfigure $fd -translation binary set existing [read $fd [string length $hdr]] seek $fd 0 puts -nonewline $fd $hdr close $fd set res [catchsql {SELECT rowid FROM x3 WHERE x3 MATCH 'x AND a'}] - if {$res == "1 {database disk image is malformed}"} {incr nCorrupt} + if {[string match {*fts5: corruption*} $res]} {incr nCorrupt} set {} 1 } {1} if {($tn2 % 10)==0 && $existing != $hdr} { do_test 3.$tn.$tn2.2 { catchsql { INSERT INTO x3(x3) VALUES('integrity-check') } - } {1 {database disk image is malformed}} + } {/.*fts5: corruption.*/} + do_execsql_test 3.$tn.$tn2.3 { + PRAGMA integrity_check(x3); + } {/.*fts5: corruption.*/} } execsql ROLLBACK @@ -202,7 +209,7 @@ foreach {tn nCut} { set res [catchsql { SELECT rowid FROM x4 WHERE x4 MATCH 'a' ORDER BY 1 DESC }] - if {$res == "1 {database disk image is malformed}"} {incr nCorrupt} + if {[string match {*fts5: corruption*} $res]} {incr nCorrupt} set {} 1 } {1} @@ -212,8 +219,6 @@ foreach {tn nCut} { # do_test 4.$tn.x { expr $nCorrupt>0 } 1 } -} - set doc [string repeat "A B C " 1000] do_execsql_test 5.0 { CREATE VIRTUAL TABLE x5 USING fts5(tt); @@ -234,7 +239,7 @@ foreach {tn hdr} { execsql BEGIN set fd [db incrblob main x5_data block $rowid] - fconfigure $fd -encoding binary -translation binary + fconfigure $fd -translation binary puts -nonewline $fd $hdr close $fd @@ -248,6 +253,7 @@ foreach {tn hdr} { #-------------------------------------------------------------------- reset_db +sqlite3_db_config db DEFENSIVE 0 do_execsql_test 6.1 { CREATE VIRTUAL TABLE x5 USING fts5(tt); INSERT INTO x5 VALUES('a'); @@ -269,4 +275,3 @@ do_catchsql_test 6.2 { sqlite3_fts5_may_be_corrupt 0 finish_test - diff --git a/ext/fts5/test/fts5corrupt3.test b/ext/fts5/test/fts5corrupt3.test index 7a8cb5c465..20be7c45cf 100644 --- a/ext/fts5/test/fts5corrupt3.test +++ b/ext/fts5/test/fts5corrupt3.test @@ -17,12 +17,13 @@ source [file join [file dirname [info script]] fts5_common.tcl] set testprefix fts5corrupt3 -# If SQLITE_ENABLE_FTS5 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return } sqlite3_fts5_may_be_corrupt 1 +database_may_be_corrupt proc create_t1 {} { expr srand(0) @@ -35,8 +36,6 @@ proc create_t1 {} { } } -if 1 { - # Create a simple FTS5 table containing 100 documents. Each document # contains 10 terms, each of which start with the character "x". # @@ -51,6 +50,7 @@ do_test 1.1 { set {} {} } {} +sqlite3_db_config db DEFENSIVE 0 for {set i 0} {$i < $L} {incr i} { do_test 1.2.$i { catchsql { @@ -86,6 +86,7 @@ do_execsql_test 2.2 { # reset_db do_test 3.0 { create_t1 } {} +sqlite3_db_config db DEFENSIVE 0 do_execsql_test 3.1 { SELECT count(*) FROM t1_data; @@ -101,7 +102,7 @@ proc do_3_test {tn} { list [ catch { db eval {SELECT rowid FROM t1 WHERE t1 MATCH 'x*'} } msg ] $msg - } {1 {database disk image is malformed}} + } {/.*fts5: corruption.*/} catch { db eval ROLLBACK } } } @@ -158,6 +159,7 @@ do_3_test 3.10 # Test that segments that end unexpectedly are identified as corruption. # reset_db +sqlite3_db_config db DEFENSIVE 0 do_test 4.0 { execsql { CREATE VIRTUAL TABLE t1 USING fts5(x); @@ -179,6 +181,11 @@ for {set i 1} {1} {incr i} { if {$end<=$i} break lset var end [expr $end - $i] set struct [binary format c* $var] + + db close + sqlite3 db test.db + sqlite3_db_config db DEFENSIVE 0 + db eval { BEGIN; UPDATE t1_data SET block = $struct WHERE id=10; @@ -253,6 +260,7 @@ foreach rowid [db eval {SELECT rowid FROM x1_data WHERE rowid>100}] { #------------------------------------------------------------------------ # reset_db +sqlite3_db_config db DEFENSIVE 0 do_execsql_test 6.1.0 { CREATE VIRTUAL TABLE t1 USING fts5(a); INSERT INTO t1 VALUES('bbbbb ccccc'); @@ -265,10 +273,11 @@ do_execsql_test 6.1.1 { } do_catchsql_test 6.1.2 { INSERT INTO t1(t1) VALUES('integrity-check'); -} {1 {database disk image is malformed}} +} {/.*fts5: corruption.*/} #------- reset_db +sqlite3_db_config db DEFENSIVE 0 do_execsql_test 6.2.0 { CREATE VIRTUAL TABLE t1 USING fts5(a); INSERT INTO t1(t1, rank) VALUES('pgsz', 32); @@ -280,10 +289,11 @@ do_execsql_test 6.2.1 { } do_catchsql_test 6.2.2 { INSERT INTO t1(t1) VALUES('integrity-check'); -} {1 {database disk image is malformed}} +} {/.*fts5: corruption.*/} #------- reset_db +sqlite3_db_config db DEFENSIVE 0 do_execsql_test 6.3.0 { CREATE VIRTUAL TABLE t1 USING fts5(a); INSERT INTO t1 VALUES('abc abcdef abcdefghi'); @@ -298,7 +308,7 @@ do_execsql_test 6.3.1 { } do_catchsql_test 6.3.2 { INSERT INTO t1(t1) VALUES('integrity-check'); -} {1 {database disk image is malformed}} +} {/.*fts5: corruption.*/} do_execsql_test 6.3.3 { ROLLBACK; BEGIN; @@ -309,7 +319,7 @@ do_execsql_test 6.3.3 { } do_catchsql_test 6.3.3 { INSERT INTO t1(t1) VALUES('integrity-check'); -} {1 {database disk image is malformed}} +} {/.*fts5: corruption.*/} do_execsql_test 6.3.4 { ROLLBACK; BEGIN; @@ -320,7 +330,7 @@ do_execsql_test 6.3.4 { } do_catchsql_test 6.3.5 { INSERT INTO t1(t1) VALUES('integrity-check'); -} {1 {database disk image is malformed}} +} {/.*fts5: corruption.*/} do_execsql_test 6.3.6 { ROLLBACK; BEGIN; @@ -331,7 +341,7 @@ do_execsql_test 6.3.6 { } do_catchsql_test 6.3.5 { INSERT INTO t1(t1) VALUES('integrity-check'); -} {1 {database disk image is malformed}} +} {/.*fts5: corruption.*/} #------------------------------------------------------------------------ @@ -358,18 +368,17 @@ do_test 7.0 { } } {} +sqlite3_db_config db DEFENSIVE 0 do_test 7.1 { foreach i [db eval { SELECT rowid FROM t5_data WHERE rowid>100 }] { db eval BEGIN db eval {DELETE FROM t5_data WHERE rowid = $i} set r [catchsql { INSERT INTO t5(t5) VALUES('integrity-check')} ] - if {$r != "1 {database disk image is malformed}"} { error $r } + if {![string match {*fts5: corruption*} $r]} { error $r } db eval ROLLBACK } } {} -} - #------------------------------------------------------------------------ # Corruption within the structure record. # @@ -379,6 +388,7 @@ do_execsql_test 8.1 { INSERT INTO t1 VALUES('one', 'two'); } +sqlite3_db_config db DEFENSIVE 0 do_test 9.1.1 { set blob "12345678" ;# cookie append blob "0105" ;# 1 level, total of 5 segments @@ -389,7 +399,7 @@ do_test 9.1.1 { } {} do_catchsql_test 9.1.2 { SELECT * FROM t1('one AND two'); -} {1 {database disk image is malformed}} +} {/.*fts5: corrupt.*/} do_test 9.2.1 { set blob "12345678" ;# cookie @@ -401,8 +411,16080 @@ do_test 9.2.1 { } {} do_catchsql_test 9.2.2 { SELECT * FROM t1('one AND two'); +} {/.*fts5: corrupt.*/} + +#------------------------------------------------------------------------- +reset_db +do_test 10.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 32768 pagesize 4096 filename c9.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 07 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 07 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 96: 00 00 00 00 0d 00 00 00 07 0d d2 00 0f c4 0f 6d ...............m +| 112: 0f 02 0e ab 0e 4e 0d f6 0d d2 00 00 00 00 00 00 .....N.......... +| 3536: 00 00 22 07 06 17 11 11 01 31 74 61 62 6c 65 74 .........1tablet +| 3552: 32 74 32 07 43 52 45 41 54 45 20 54 41 42 4c 45 2t2.CREATE TABLE +| 3568: 20 74 32 28 78 29 56 06 06 17 1f 1f 01 7d 74 61 t2(x)V.......ta +| 3584: 62 6c 65 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 blet1_configt1_c +| 3600: 6f 6e 66 69 67 06 43 52 45 41 54 45 20 54 41 42 onfig.CREATE TAB +| 3616: 4c 45 20 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b LE 't1_config'(k +| 3632: 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 PRIMARY KEY, v) +| 3648: 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 5b 05 WITHOUT ROWID[. +| 3664: 07 17 21 21 01 81 01 74 61 62 6c 65 74 31 5f 64 ..!!...tablet1_d +| 3680: 6f 63 73 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 ocsizet1_docsize +| 3696: 05 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 .CREATE TABLE 't +| 3712: 31 5f 64 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 1_docsize'(id IN +| 3728: 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE +| 3744: 59 2c 20 73 7a 20 42 4c 4f 42 29 55 04 06 17 21 Y, sz BLOB)U...! +| 3760: 21 01 77 74 61 62 6c 65 74 31 5f 63 6f 6e 74 65 !.wtablet1_conte +| 3776: 6e 74 74 31 5f 63 6f 6e 74 65 6e 74 04 43 52 45 ntt1_content.CRE +| 3792: 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 6f ATE TABLE 't1_co +| 3808: 6e 74 65 6e 74 27 28 69 64 20 49 4e 54 45 47 45 ntent'(id INTEGE +| 3824: 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 63 R PRIMARY KEY, c +| 3840: 30 29 69 03 07 17 19 19 01 81 2d 74 61 62 6c 65 0)i.......-table +| 3856: 74 31 5f 69 64 78 74 31 5f 69 64 78 03 43 52 45 t1_idxt1_idx.CRE +| 3872: 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 69 64 ATE TABLE 't1_id +| 3888: 78 27 28 73 65 67 69 64 2c 20 74 65 72 6d 2c 20 x'(segid, term, +| 3904: 70 67 6e 6f 2c 20 50 52 49 4d 41 52 59 20 4b 45 pgno, PRIMARY KE +| 3920: 59 28 73 65 67 69 64 2c 20 74 65 72 6d 29 29 20 Y(segid, term)) +| 3936: 57 49 54 48 4f 55 54 20 52 4f 57 49 44 55 02 07 WITHOUT ROWIDU.. +| 3952: 17 1b 1b 01 81 01 74 61 62 6c 65 74 31 5f 64 61 ......tablet1_da +| 3968: 74 61 74 31 5f 64 61 74 61 02 43 52 45 41 54 45 tat1_data.CREATE +| 3984: 20 54 41 42 4c 45 20 27 74 31 5f 64 61 74 61 27 TABLE 't1_data' +| 4000: 28 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d (id INTEGER PRIM +| 4016: 41 52 59 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 ARY KEY, block B +| 4032: 4c 4f 42 29 3a 01 06 17 11 11 08 63 74 61 62 6c LOB):......ctabl +| 4048: 65 74 31 74 31 43 52 45 41 54 45 20 56 49 52 54 et1t1CREATE VIRT +| 4064: 55 41 4c 20 54 41 42 4c 45 20 74 31 20 55 53 49 UAL TABLE t1 USI +| 4080: 4e 47 20 66 74 73 35 28 63 6f 6e 74 65 6e 74 29 NG fts5(content) +| page 2 offset 4096 +| 0: 0d 00 00 00 03 0f bd 00 0f e8 0f ef 0f bd 00 00 ................ +| 4016: 00 00 00 00 00 00 00 00 00 00 00 00 00 24 84 80 .............$.. +| 4032: 80 80 80 01 03 00 4e 00 00 00 1e 06 30 61 62 61 ......N.....0aba +| 4048: 63 6b 01 02 02 04 02 66 74 02 06 36 b0 a0 10 21 ck.....ft..6...! +| 4064: d6 f7 07 46 96 d6 97 a6 05 01 03 00 10 03 03 0f ...F............ +| 4080: 0a 03 00 24 00 00 00 00 01 01 01 00 01 01 01 01 ...$............ +| page 3 offset 8192 +| 0: 0a 00 00 00 01 0f fa 00 0f fa 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 05 04 09 0c 01 02 ................ +| page 4 offset 12288 +| 0: 0d 00 00 00 03 0f e0 00 0f f6 0f ec 0f e0 00 00 ................ +| 4064: 0a 03 03 00 1b 61 62 61 6e 64 6f 6e 08 02 03 00 .....abandon.... +| 4080: 17 61 62 61 66 74 08 01 03 00 17 61 62 61 63 6b .abaft.....aback +| page 5 offset 16384 +| 0: 0d 00 00 00 03 0f ee 00 0f fa 0f f4 0f ee 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 04 03 ................ +| 4080: 03 00 0e 01 04 02 03 00 0e 01 04 01 03 00 0e 01 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| page 7 offset 24576 +| 0: 0d 00 00 00 03 0f d6 00 0f f4 0f e1 0f d6 00 00 ................ +| 4048: 00 00 00 00 00 00 09 03 02 1b 72 65 62 75 69 6c ..........rebuil +| 4064: 64 11 02 02 2b 69 6e 74 65 67 72 69 74 79 2d 63 d...+integrity-c +| 4080: 68 65 62 6c 65 74 31 74 31 43 52 45 41 54 45 20 heblet1t1CREATE +| page 8 offset 28672 +| 0: 56 49 52 54 55 41 4c 20 54 41 42 4c 45 20 74 31 VIRTUAL TABLE t1 +| 16: 20 55 53 49 4e 47 20 66 74 73 35 28 63 6f 6e 74 USING fts5(cont +| 32: 65 6e 74 29 0d 00 00 00 03 0f bd 00 0f e8 0f ef ent)............ +| 48: 0f bd 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| end c9.db + }] +} {} +do_catchsql_test 10.1 { + SELECT * FROM t1 WHERE t1 MATCH 'abandon'; +} {/.*fts5: corrupt.*/} + +#------------------------------------------------------------------------- +# +reset_db +do_test 11.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 28672 pagesize 4096 filename c10b.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 01 00 00 00 07 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 48: 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 ................ +| 80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 ................ +| 96: 00 2e 30 38 0d 00 00 00 07 0d d2 00 0f c4 0f 6d ..08...........m +| 112: 0f 02 0e ab 0e 4e 0d f6 0d d2 00 00 00 00 00 00 .....N.......... +| 3536: 00 00 22 07 06 17 11 11 01 31 74 61 62 6c 65 74 .........1tablet +| 3552: 32 74 32 07 43 52 45 41 54 45 20 54 41 42 4c 45 2t2.CREATE TABLE +| 3568: 20 74 32 28 78 29 56 06 06 17 1f 1f 01 7d 74 61 t2(x)V.......ta +| 3584: 62 6c 65 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 blet1_configt1_c +| 3600: 6f 6e 66 69 67 06 43 52 45 41 54 45 20 54 41 42 onfig.CREATE TAB +| 3616: 4c 45 20 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b LE 't1_config'(k +| 3632: 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 PRIMARY KEY, v) +| 3648: 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 5b 05 WITHOUT ROWID[. +| 3664: 07 17 21 21 01 81 01 74 61 62 6c 65 74 31 5f 64 ..!!...tablet1_d +| 3680: 6f 63 73 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 ocsizet1_docsize +| 3696: 05 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 .CREATE TABLE 't +| 3712: 31 5f 64 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 1_docsize'(id IN +| 3728: 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE +| 3744: 59 2c 20 73 7a 20 42 4c 4f 42 29 55 04 06 17 21 Y, sz BLOB)U...! +| 3760: 21 01 77 74 61 62 6c 65 74 31 5f 63 6f 6e 74 65 !.wtablet1_conte +| 3776: 6e 74 74 31 5f 63 6f 6e 74 65 6e 74 04 43 52 45 ntt1_content.CRE +| 3792: 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 6f ATE TABLE 't1_co +| 3808: 6e 74 65 6e 74 27 28 69 64 20 49 4e 54 45 47 45 ntent'(id INTEGE +| 3824: 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 63 R PRIMARY KEY, c +| 3840: 30 29 69 03 07 17 19 19 01 81 2d 74 61 62 6c 65 0)i.......-table +| 3856: 74 31 5f 69 64 78 74 31 5f 69 64 78 03 43 52 45 t1_idxt1_idx.CRE +| 3872: 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 69 64 ATE TABLE 't1_id +| 3888: 78 27 28 73 65 67 69 64 2c 20 74 65 72 6d 2c 20 x'(segid, term, +| 3904: 70 67 6e 6f 2c 20 50 52 49 4d 41 52 59 20 4b 45 pgno, PRIMARY KE +| 3920: 59 28 73 65 67 69 64 2c 20 74 65 72 6d 29 29 20 Y(segid, term)) +| 3936: 57 49 54 48 4f 55 54 20 52 4f 57 49 44 55 02 07 WITHOUT ROWIDU.. +| 3952: 17 1b 1b 01 81 01 74 61 62 6c 65 74 31 5f 64 61 ......tablet1_da +| 3968: 74 61 74 31 5f 64 61 74 61 02 43 52 45 41 54 45 tat1_data.CREATE +| 3984: 20 54 41 42 4c 45 20 27 74 31 5f 64 61 74 61 27 TABLE 't1_data' +| 4000: 28 69 64 20 49 4e 54 45 47 45 52 20 50 52 44 d9 (id INTEGER PRD. +| 4016: 41 52 59 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 ARY KEY, block B +| 4032: 4c 4f 42 29 3a 01 06 17 11 11 08 63 74 61 62 6c LOB):......ctabl +| 4048: 65 74 31 74 31 43 52 45 41 54 45 20 56 49 52 54 et1t1CREATE VIRT +| 4064: 55 41 4c 20 54 41 42 4c 45 20 74 31 20 55 53 49 UAL TABLE t1 USI +| 4080: 4e 47 20 66 74 73 35 28 63 6f 6e 74 65 6e 74 29 NG fts5(content) +| page 2 offset 4096 +| 0: 0d 00 00 00 06 0f 59 00 0f e8 0f ef 0f bd 0f b0 ......Y......... +| 16: 0f 73 0f 59 00 00 00 00 00 00 00 00 00 00 00 00 .s.Y............ +| 3920: 00 00 00 00 00 00 00 00 00 13 84 80 80 80 80 04 ................ +| 3936: 03 01 2a 0a 00 00 00 00 01 02 02 00 02 01 01 01 ..*............. +| 3952: 02 01 01 36 84 80 80 80 80 03 03 05 66 00 40 00 ...6........f.@. +| 3968: 00 00 01 00 00 00 29 07 30 61 63 74 69 76 65 04 ......).0active. +| 3984: 02 02 02 03 74 6f 6d 06 02 02 05 02 69 63 07 02 ....tom.....ic.. +| 4000: 02 01 06 62 6f 6f 6d 65 72 05 02 02 04 0b 08 07 ...boomer....... +| 4016: 06 84 80 80 80 80 02 03 01 10 01 07 07 24 84 80 .............$.. +| 4032: 80 80 80 01 03 00 4e 00 00 00 1e 06 30 61 62 61 ......N.....0aba +| 4048: 63 6b 01 02 02 04 02 66 74 02 02 02 04 04 6e 64 ck.....ft.....nd +| 4064: 6f 6e 03 02 02 04 0a 07 05 01 03 00 10 03 03 0f on.............. +| 4080: 0a 03 00 24 00 00 00 00 01 01 01 00 01 01 01 01 ...$............ +| page 3 offset 8192 +| 0: 0a 00 00 00 02 0f f3 00 0f fa 0f f3 00 00 00 00 ................ +| 4080: 00 00 00 06 04 01 0c 01 02 02 05 04 09 0c 01 02 ................ +| page 4 offset 12288 +| 0: 0d 00 00 00 07 0f b6 00 0f f6 0f ec 0f e0 0f d5 ................ +| 16: 0f ca 0f c1 0f b6 00 00 00 00 00 00 00 00 00 00 ................ +| 4016: 00 00 00 00 00 00 09 07 03 00 19 61 74 6f 6d 69 ...........atomi +| 4032: 63 07 06 03 00 15 61 74 6f 6d 09 05 03 00 19 62 c.....atom.....b +| 4048: 6f 6f 6d 65 72 09 04 03 00 19 61 63 74 69 76 65 oomer.....active +| 4064: 0a 03 03 00 1b 61 62 61 6e 64 6f 6e 08 02 03 00 .....abandon.... +| 4080: 17 61 62 61 66 74 08 01 03 00 17 61 62 61 63 6b .abaft.....aback +| page 5 offset 16384 +| 0: 0d 00 00 00 07 0f d6 00 0f fa 0f f4 0f ee 0f e8 ................ +| 16: 0f e2 0f dc 0f d6 00 00 00 00 00 00 00 00 00 00 ................ +| 4048: 00 00 00 00 00 00 04 07 03 00 0e 01 04 06 03 00 ................ +| 4064: 0e 01 04 05 03 00 0e 01 04 04 03 00 0e 01 04 03 ................ +| 4080: 03 00 0e 01 04 02 03 00 0e 01 04 01 03 00 0e 01 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| page 7 offset 24576 +| 0: 0d 00 00 00 03 0f d6 00 0f f4 0f e1 0f d6 00 00 ................ +| 4048: 00 00 00 00 00 00 09 03 02 1b 72 65 62 75 69 6c ..........rebuil +| 4064: 64 11 02 02 2b 69 6e 74 65 67 72 69 74 79 2d 63 d...+integrity-c +| 4080: 68 65 63 6b 0a 01 02 1d 6f 70 74 69 6d 69 7a 65 heck....optimize +| end c10b.db +}]} {} + +# This returns SQLITE_CONSTRAINT instead of SQLITE_CORRUPT. The problem is +# that the corrupted structure-record leads fts5 to try to use a segment-id +# that is already in use. This is caught by the PRIMARY KEY constraint on +# the %_idx table. +# +do_catchsql_test 11.1 { + UPDATE t1 SET content='abc' WHERE content='boomer'; +} {1 {constraint failed}} + +#------------------------------------------------------------------------- +# +reset_db +do_test 12.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 28672 pagesize 4096 filename c2.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 07 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 07 00 00 00 00 ................ +| 48: 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 ................ +| 96: 00 00 00 00 0d 00 00 00 07 0d d2 00 0f c4 0f 6d ...............m +| 112: 0f 02 0e ab 0e 4e 0d f6 0d d2 00 00 00 00 00 00 .....N.......... +| 3536: 00 00 22 07 06 17 11 11 01 31 74 61 62 6c 65 74 .........1tablet +| 3552: 32 74 32 07 43 52 45 41 54 45 20 54 41 42 4c 45 2t2.CREATE TABLE +| 3568: 20 74 32 28 78 29 56 06 06 17 1f 1f 01 7d 74 61 t2(x)V.......ta +| 3584: 62 6c 65 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 blet1_configt1_c +| 3600: 6f 6e 66 69 67 06 43 52 45 41 54 45 20 54 41 42 onfig.CREATE TAB +| 3616: 4c 45 20 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b LE 't1_config'(k +| 3632: 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 PRIMARY KEY, v) +| 3648: 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 5b 05 WITHOUT ROWID[. +| 3664: 07 17 21 21 01 81 01 74 61 62 6c 65 74 31 5f 64 ..!!...tablet1_d +| 3680: 6f 63 73 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 ocsizet1_docsize +| 3696: 05 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 .CREATE TABLE 't +| 3712: 31 5f 64 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 1_docsize'(id IN +| 3728: 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE +| 3744: 59 2c 20 73 7a 20 42 4c 4f 42 29 55 04 06 17 21 Y, sz BLOB)U...! +| 3760: 21 01 77 74 61 62 6c 65 74 31 5f 63 6f 6e 74 65 !.wtablet1_conte +| 3776: 6e 74 74 31 5f 63 6f 6e 74 65 6e 74 04 43 52 45 ntt1_content.CRE +| 3792: 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 6f ATE TABLE 't1_co +| 3808: 6e 74 65 6e 74 27 28 69 64 20 49 4e 54 45 47 45 ntent'(id INTEGE +| 3824: 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 63 R PRIMARY KEY, c +| 3840: 30 29 69 03 07 17 19 19 01 81 2d 74 61 62 6c 65 0)i.......-table +| 3856: 74 31 5f 69 64 78 74 31 5f 69 64 78 03 43 52 45 t1_idxt1_idx.CRE +| 3872: 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 69 64 ATE TABLE 't1_id +| 3888: 78 27 28 73 65 67 69 64 2c 20 74 65 72 6d 2c 20 x'(segid, term, +| 3904: 70 67 6e 6f 2c 20 50 52 49 4d 41 52 59 20 4b 45 pgno, PRIMARY KE +| 3920: 59 28 73 65 67 69 64 2c 20 74 65 72 6d 29 29 20 Y(segid, term)) +| 3936: 57 49 54 48 4f 55 54 20 52 4f 57 49 44 55 02 07 WITHOUT ROWIDU.. +| 3952: 17 1b 1b 01 81 01 74 61 62 6c 65 74 31 5f 64 61 ......tablet1_da +| 3968: 74 61 74 31 5f 64 61 74 61 02 43 52 45 41 54 45 tat1_data.CREATE +| 3984: 20 54 41 42 4c 45 20 27 74 31 5f 64 61 74 61 27 TABLE 't1_data' +| 4000: 28 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d (id INTEGER PRIM +| 4016: 41 52 59 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 ARY KEY, block B +| 4032: 4c 4f 42 29 3a 01 06 17 11 11 08 63 74 61 62 6c LOB):......ctabl +| 4048: 65 74 31 74 31 43 52 45 41 54 45 20 56 49 52 54 et1t1CREATE VIRT +| 4064: 55 41 4c 20 54 41 42 4c 45 20 74 31 20 55 53 49 UAL TABLE t1 USI +| 4080: 4e 47 20 66 74 73 35 28 63 6f 6e 74 65 6e 74 29 NG fts5(content) +| page 2 offset 4096 +| 0: 0d 00 00 00 03 0f bd 00 0f d8 0f ef 0f bd 00 00 ................ +| 4016: 00 00 00 00 00 00 00 00 00 00 00 00 00 24 84 80 .............$.. +| 4032: 80 80 80 01 03 00 4e 00 00 00 1e 06 30 61 62 61 ......N.....0aba +| 4048: 63 6b 01 02 02 04 02 66 74 02 02 02 04 04 6e 64 ck.....ft.....nd +| 4064: 6f 6e 03 02 02 04 0a 07 05 01 03 00 10 03 03 0f on.............. +| 4080: 0a 03 00 24 00 00 00 00 01 01 01 20 01 01 01 01 ...$....... .... +| page 3 offset 8192 +| 0: 0a 00 00 00 01 0f fa 00 0f fa 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 05 04 09 0c 01 02 ................ +| page 4 offset 12288 +| 0: 0d 00 00 00 03 0f e0 00 0f f6 0f ec 0f e0 3f e0 ..............?. +| 16: a0 30 30 01 b6 16 26 16 e6 46 f6 e0 80 20 30 01 .00...&..F... 0. +| 32: 76 16 26 16 67 40 80 10 30 01 76 16 26 16 36 b0 v.&.g@..0.v.&.6. +| 48: d0 00 00 00 30 fe e0 00 ff a0 ff 40 fe 00 00 00 ....0......@.... +| page 5 offset 16384 +| 4064: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 04 03 ................ +| 4080: 03 00 0e 01 04 02 03 00 0e 01 04 01 03 00 0e 01 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| page 7 offset 24576 +| 0: 0d 00 00 00 03 0f d6 00 0f f4 0f e1 0f d6 00 00 ................ +| 4048: 00 00 00 00 00 00 09 03 02 1b 72 65 62 75 69 6c ..........rebuil +| 4064: 64 11 02 02 2b 69 6e 74 65 67 72 69 74 79 2d 63 d...+integrity-c +| 4080: 68 65 63 6b 0a 01 02 1d 6f 70 74 69 6d 69 7a 65 heck....optimize +| end c2.db +}]} {} + +do_catchsql_test 12.1 { + SELECT * FROM t1 WHERE t1 MATCH 'abandon'; +} {/.*fts5: corrupt.*/} + +do_catchsql_test 12.2 { + INSERT INTO t1(t1, rank) VALUES('merge', 500); +} {/.*fts5: corrupt.*/} + +#------------------------------------------------------------------------- +# +reset_db +do_test 13.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 28672 pagesize 4096 filename c13.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 07 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 07 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 96: 00 00 00 00 0d 00 00 00 07 0d d2 00 0f c4 0f 6d ...............m +| 112: 0f 02 0e ab 0e 4e 0d f6 0d d2 00 00 00 00 00 00 .....N.......... +| 3536: 00 00 22 07 06 17 11 11 01 31 74 61 62 6c 65 74 .........1tablet +| 3552: 32 74 32 07 43 52 45 41 54 45 20 54 41 42 4c 45 2t2.CREATE TABLE +| 3568: 20 74 32 28 78 29 56 06 06 17 1f 1f 01 7d 74 61 t2(x)V.......ta +| 3584: 62 6c 65 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 blet1_configt1_c +| 3600: 6f 6e 66 69 67 06 43 52 45 41 54 45 20 54 41 42 onfig.CREATE TAB +| 3616: 4c 45 20 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b LE 't1_config'(k +| 3632: 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 PRIMARY KEY, v) +| 3648: 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 5b 05 WITHOUT ROWID[. +| 3664: 07 17 21 21 01 81 01 74 61 62 6c 65 74 31 5f 64 ..!!...tablet1_d +| 3680: 6f 63 73 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 ocsizet1_docsize +| 3696: 05 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 .CREATE TABLE 't +| 3712: 31 5f 64 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 1_docsize'(id IN +| 3728: 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE +| 3744: 59 2c 20 73 7a 20 42 4c 4f 42 29 55 04 06 17 21 Y, sz BLOB)U...! +| 3760: 21 01 77 74 61 62 6c 65 74 31 5f 63 6f 6e 74 65 !.wtablet1_conte +| 3776: 6e 74 74 31 5f 63 6f 6e 74 65 6e 74 04 43 52 45 ntt1_content.CRE +| 3792: 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 6f ATE TABLE 't1_co +| 3808: 6e 74 65 6e 74 27 28 69 64 20 49 4e 54 45 47 45 ntent'(id INTEGE +| 3824: 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 63 R PRIMARY KEY, c +| 3840: 30 29 69 03 07 17 19 19 01 81 2d 74 61 62 6c 65 0)i.......-table +| 3856: 74 31 5f 69 64 78 74 31 5f 69 64 78 03 43 52 45 t1_idxt1_idx.CRE +| 3872: 41 54 45 20 54 41 42 4c 45 20 27 74 31 4f 69 64 ATE TABLE 't1Oid +| 3888: 78 27 28 73 65 67 69 64 2c 20 74 65 72 6d 2c 20 x'(segid, term, +| 3904: 70 67 6e 6f 2c 20 50 52 49 4d 41 52 59 20 4b 45 pgno, PRIMARY KE +| 3920: 59 28 73 65 67 69 64 2c 20 74 65 72 6d 29 29 20 Y(segid, term)) +| 3936: 57 49 54 48 4f 55 54 20 52 4f 57 49 44 55 02 07 WITHOUT ROWIDU.. +| 3952: 17 1b 1b 01 81 01 74 61 62 6c 65 74 31 5f 64 61 ......tablet1_da +| 3968: 74 61 74 31 5f 64 61 74 61 02 43 52 45 41 54 45 tat1_data.CREATE +| 3984: 20 54 41 42 4c 45 20 27 74 31 5f 64 61 74 61 27 TABLE 't1_data' +| 4000: 28 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d (id INTEGER PRIM +| 4016: 41 52 59 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 ARY KEY, block B +| 4032: 4c 4f 42 29 3a 01 06 17 11 11 08 63 74 61 62 6c LOB):......ctabl +| 4048: 65 74 31 74 31 43 52 45 41 54 45 20 56 49 52 54 et1t1CREATE VIRT +| 4064: 55 41 4c 20 54 41 42 4c 45 20 74 31 20 55 53 49 UAL TABLE t1 USI +| 4080: 4e 47 20 66 74 73 35 28 63 6f 6e 74 65 6e 74 29 NG fts5(content) +| page 2 offset 4096 +| 0: 0d 00 00 00 03 0f bd 00 0f e8 0f ef 0f bd 00 00 ................ +| 4016: 00 00 00 00 00 00 00 00 00 00 00 00 00 24 84 80 .............$.. +| 4032: 80 80 80 01 03 00 4e 00 00 00 1e 06 30 61 62 61 ......N.....0aba +| 4048: 63 6b 01 02 02 04 02 66 74 02 02 02 04 04 6e 64 ck.....ft.....nd +| 4064: 6f 6e 03 02 02 04 0a 07 05 01 03 00 10 03 03 0f on.............. +| 4080: 0a 03 00 24 00 eb 00 00 00 01 01 01 00 01 01 01 ...$............ +| page 3 offset 8192 +| 0: 01 0a 00 00 00 01 0f fa 00 0f fa 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 05 04 09 0c 01 02 ................ +| page 4 offset 12288 +| 0: 0d 00 00 00 03 0f e0 00 0f f6 0f ec 0f e0 00 00 ................ +| 4064: 0a 03 03 00 1b 61 62 61 6e 64 6f 6e 08 02 03 00 .....abandon.... +| 4080: 17 61 62 61 66 74 08 01 03 00 17 61 62 61 63 6b .abaft.....aback +| page 5 offset 16384 +| 0: 0d 00 00 00 03 0f ee 00 0f fa 0f f2 0f ee 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 04 03 ................ +| 4080: 03 00 0e 01 04 02 03 00 0e 01 04 01 03 00 0e 01 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| page 7 offset 24576 +| 0: 0d 00 00 00 03 0f d6 00 0f f4 0f e1 0f d6 00 00 ................ +| 4048: 00 00 00 00 00 00 09 03 02 1b 72 65 62 75 69 6c ..........rebuil +| 4064: 64 11 02 02 2b 69 6e 74 65 67 72 69 74 79 2d 63 d...+integrity-c +| 4080: 68 65 63 6b 0a 01 02 1d 6f 70 74 69 6d 69 7a 65 heck....optimize +| end c13.db +SELECT * FROM t1 WHERE t1 MATCH 'abandon'; +}]} {} + +do_catchsql_test 13.1 { + SELECT * FROM t1 WHERE t1 MATCH 'abandon'; +} {/*malformed database schema*/} + +#------------------------------------------------------------------------- +reset_db +do_test 14.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 28672 pagesize 4096 filename c14b.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 01 00 00 00 07 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 48: 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 ................ +| 80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 ................ +| 96: 00 2e 30 38 0d 00 00 00 07 0d d2 00 0f c4 0f 6d ..08...........m +| 112: 0f 02 0e ab 0e 4e 0d f6 0d d2 00 00 00 00 00 00 .....N.......... +| 3536: 00 00 22 07 06 17 11 11 01 31 74 61 62 6c 65 74 .........1tablet +| 3552: 32 74 32 07 43 52 45 41 54 45 20 54 41 42 4c 45 2t2.CREATE TABLE +| 3568: 20 74 32 28 78 29 56 06 06 17 1f 1f 01 7d 74 61 t2(x)V.......ta +| 3584: 62 6c 65 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 blet1_configt1_c +| 3600: 6f 6e 66 69 67 06 43 52 45 41 54 45 20 54 41 42 onfig.CREATE TAB +| 3616: 4c 45 20 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b LE 't1_config'(k +| 3632: 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 PRIMARY KEY, v) +| 3648: 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 5b 05 WITHOUT ROWID[. +| 3664: 07 17 21 21 01 81 01 74 61 62 6c 65 74 31 5f 64 ..!!...tablet1_d +| 3680: 6f 63 73 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 ocsizet1_docsize +| 3696: 05 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 .CREATE TABLE 't +| 3712: 31 5f 64 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 1_docsize'(id IN +| 3728: 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE +| 3744: 59 2c 20 73 7a 20 42 4c 4f 42 29 55 04 06 17 21 Y, sz BLOB)U...! +| 3760: 21 01 77 74 61 62 6c 65 74 31 5f 63 6f 6e 74 65 !.wtablet1_conte +| 3776: 6e 74 74 31 5f 63 6f 6e 74 65 6e 74 04 43 52 45 ntt1_content.CRE +| 3792: 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 6f ATE TABLE 't1_co +| 3808: 6e 74 65 6e 74 27 28 69 64 20 49 4e 54 45 47 45 ntent'(id INTEGE +| 3824: 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 63 R PRIMARY KEY, c +| 3840: 30 29 69 03 07 17 19 19 01 81 2d 74 61 62 6c 65 0)i.......-table +| 3856: 74 31 5f 69 64 78 74 31 5f 69 64 78 03 43 52 45 t1_idxt1_idx.CRE +| 3872: 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 69 64 ATE TABLE 't1_id +| 3888: 78 27 28 73 65 67 69 64 2c 20 74 65 72 6d 2c 20 x'(segid, term, +| 3904: 70 67 6e 6f 2c 20 50 52 49 4d 41 52 59 20 4b 45 pgno, PRIMARY KE +| 3920: 59 28 73 65 67 69 64 2c 20 74 65 72 6d 29 29 20 Y(segid, term)) +| 3936: 57 49 54 48 4f 55 54 20 52 4f 57 49 44 55 02 07 WITHOUT ROWIDU.. +| 3952: 17 1b 1b 01 81 01 74 61 62 6c 65 74 31 5f 64 61 ......tablet1_da +| 3968: 74 61 74 31 5f 64 61 74 61 02 43 52 45 41 54 45 tat1_data.CREATE +| 3984: 20 54 41 42 4c 45 20 27 74 31 5f 64 61 74 61 27 TABLE 't1_data' +| 4000: 28 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d (id INTEGER PRIM +| 4016: 41 52 59 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 ARY KEY, block B +| 4032: 4c 4f 42 29 3a 01 06 17 11 11 08 63 74 61 62 6c LOB):......ctabl +| 4048: 65 74 31 74 31 43 52 45 41 54 45 20 56 49 52 54 et1t1CREATE VIRT +| 4064: 55 41 4c 20 54 41 42 4c 45 20 74 31 20 55 53 49 UAL TABLE t1 USI +| 4080: 4e 47 20 66 74 73 35 28 63 6f 6e 74 65 6e 74 29 NG fts5(content) +| page 2 offset 4096 +| 0: 0d 0f ef 00 04 0f 18 00 0f e8 0f 18 0f bd 0f 2c ..............., +| 3856: 00 00 00 00 00 00 00 00 12 0a 03 00 2a 00 00 00 ............*... +| 3872: 00 01 02 02 00 02 01 01 01 02 01 01 81 09 88 80 ................ +| 3888: 80 80 80 01 04 00 82 16 00 00 00 79 06 30 61 62 ...........y.0ab +| 3904: 61 63 6b 08 02 07 04 04 6e 64 6f 6e 08 02 05 02 ack.....ndon.... +| 3920: 05 63 74 69 76 65 04 02 02 04 02 0b 02 04 6c 70 .ctive........lp +| 3936: 68 61 08 04 02 0a 02 03 74 6b 6d 06 02 02 03 02 ha......tkm..... +| 3952: 6f 6d 08 02 09 05 02 69 63 07 02 02 01 06 62 61 om.....ic.....ba +| 3968: 63 6b 75 70 08 02 04 02 05 6f 6f 6d 65 72 05 02 ckup.....oomer.. +| 3984: 02 01 0c 63 68 61 6e 6e 65 62 6f 6f 6d 65 72 08 ...channeboomer. +| 4000: 02 08 07 01 6c 08 02 03 01 04 74 65 73 74 08 02 ....l.....test.. +| 4016: 06 04 0a 09 0d 0a 08 07 07 0b 0a 11 06 24 84 80 .............$.. +| 4032: 80 80 80 01 03 00 4e 00 00 00 1e 06 30 61 62 61 ......N.....0aba +| 4048: 63 6b 01 02 02 04 02 66 74 02 02 02 04 04 6e 64 ck.....ft.....nd +| 4064: 6f 6e 03 02 02 03 9a 07 05 01 03 00 10 08 11 00 on.............. +| 4080: 00 00 11 24 00 00 00 00 01 01 01 00 01 01 01 01 ...$............ +| page 3 offset 8192 +| 0: 0a 00 00 00 02 0f f3 00 0f fa 0f f3 00 00 00 00 ................ +| 4080: 00 00 00 06 04 01 0c 01 02 02 05 04 09 0c 01 02 ................ +| page 4 offset 12288 +| 0: 0d 00 00 00 08 0f 6a 00 0f f6 0f ec 0f e0 0f d5 ......j......... +| 16: 0f ca 0f c1 0f b6 0f 6a 00 00 00 00 00 00 00 00 .......j........ +| 3936: 00 00 00 00 00 00 00 00 00 00 4a 08 04 00 81 19 ..........J..... +| 3952: 61 6c 70 68 61 20 63 68 61 6e 6e 65 6c 20 62 61 alpha channel ba +| 3968: 63 6b 75 70 20 61 62 61 6e 64 6f 6e 20 74 65 73 ckup abandon tes +| 3984: 74 20 61 62 61 63 6b 20 63 68 61 6e 6e 65 62 6f t aback channebo +| 4000: 6f 6d 65 72 20 61 74 6f 6d 20 61 6c 70 68 61 20 omer atom alpha +| 4016: 61 63 74 69 76 65 09 07 03 00 19 61 74 6f 6d 69 active.....atomi +| 4032: 63 07 06 03 00 15 61 74 6b 6d 09 05 03 00 19 62 c.....atkm.....b +| 4048: 6f 6f 6d 65 72 09 04 03 00 19 61 63 74 69 76 65 oomer.....active +| 4064: 0a 03 03 00 1b 61 62 61 6e 64 6f 6e 08 02 03 00 .....abandon.... +| 4080: 17 61 62 61 66 74 08 01 03 00 17 61 62 61 63 6b .abaft.....aback +| page 5 offset 16384 +| 0: 0d 00 00 00 08 0f d0 00 0f fa 0f f4 0f ee 0f e8 ................ +| 16: 0f e2 0f dc 0f d6 0f d0 00 00 00 00 00 00 00 00 ................ +| 4048: 04 08 03 00 0e 0a 04 07 03 00 0e 01 04 06 03 00 ................ +| 4064: 0e 01 04 05 03 00 0e 01 04 04 03 00 0e 01 04 03 ................ +| 4080: 03 00 0e 01 04 02 03 00 0e 01 04 01 03 00 0e 01 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| page 7 offset 24576 +| 0: 0d 00 00 00 03 0f d6 00 0f f4 0f e1 0f d6 00 00 ................ +| 4048: 00 00 00 00 00 00 09 03 02 1b 72 65 62 75 69 6c ..........rebuil +| 4064: 64 11 02 02 2b 69 6e 74 65 67 72 69 74 79 2d 63 d...+integrity-c +| 4080: 68 65 63 6b 0a 01 02 1d 6f 70 74 69 6d 69 7a 65 heck....optimize +| end c14b.db +}]} {} + +do_catchsql_test 14.1 { + INSERT INTO t1(t1) VALUES('optimize'); +} {/.*fts5: corrupt.*/} + +#--------------------------------------------------------------------------- +# +reset_db +do_test 15.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 32768 pagesize 4096 filename c16.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 07 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 07 00 00 00 00 ................ +| 48: 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 ................ +| 96: 00 00 00 00 0d 00 00 00 07 0d d2 00 0f c4 0f 6d ...............m +| 112: 0f 02 0e ab 0e 4e 0d f6 0d d2 00 00 00 00 00 00 .....N.......... +| 3536: 00 00 22 07 06 17 11 11 01 31 74 61 62 6c 65 74 .........1tablet +| 3552: 32 74 32 07 43 52 45 41 54 45 20 54 41 42 4c 45 2t2.CREATE TABLE +| 3568: 20 74 32 28 78 29 56 06 06 17 1f 1f 01 7d 74 61 t2(x)V.......ta +| 3584: 62 6c 65 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 blet1_configt1_c +| 3600: 6f 6e 66 69 67 06 43 52 45 41 54 45 20 54 41 42 onfig.CREATE TAB +| 3616: 4c 45 20 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b LE 't1_config'(k +| 3632: 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 PRIMARY KEY, v) +| 3648: 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 5b 05 WITHOUT ROWID[. +| 3664: 07 17 21 21 01 81 01 74 61 62 6c 00 0f f6 0f ec ..!!...tabl..... +| 3680: 0f e0 73 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 ..sizet1_docsize +| 3696: 05 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 .CREATE TABLE 't +| 3712: 31 5f 64 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 1_docsize'(id IN +| 3728: 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE +| 3744: 59 2c 20 73 7a 20 42 4c 4f 42 29 55 04 06 17 21 Y, sz BLOB)U...! +| 3760: 21 01 77 74 61 62 6c 65 74 31 5f 63 6f 6e 74 65 !.wtablet1_conte +| 3776: 6e 74 74 31 5f 63 6f 6e 74 65 6e 74 04 43 52 45 ntt1_content.CRE +| 3792: 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 6f ATE TABLE 't1_co +| 3808: 6e 74 65 6e 74 27 28 69 64 20 49 4e 54 45 47 45 ntent'(id INTEGE +| 3824: 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 63 R PRIMARY KEY, c +| 3840: 30 29 69 03 07 17 19 19 01 81 2d 74 61 62 6c 65 0)i.......-table +| 3856: 74 31 5f 69 64 78 74 31 5f 69 64 78 03 43 52 45 t1_idxt1_idx.CRE +| 3872: 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 69 64 ATE TABLE 't1_id +| 3888: 78 27 28 73 65 67 69 64 2c 20 74 65 72 6d 2c 20 x'(segid, term, +| 3904: 70 67 6e 6f 2c 20 50 52 49 4d 41 52 59 20 4b 45 pgno, PRIMARY KE +| 3920: 59 28 73 65 67 69 64 2c 20 74 65 72 6d 29 29 20 Y(segid, term)) +| 3936: 57 49 54 48 4f 55 54 20 52 4f 57 49 44 55 02 07 WITHOUT ROWIDU.. +| 3952: 17 1b 1b 01 81 01 74 61 62 6c 65 74 31 5f 64 61 ......tablet1_da +| 3968: 74 61 74 31 5f 64 61 74 61 02 43 52 45 41 54 45 tat1_data.CREATE +| 3984: 20 54 41 42 4c 45 20 27 74 31 5f 64 61 74 61 27 TABLE 't1_data' +| 4000: 28 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d (id INTEGER PRIM +| 4016: 41 52 59 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 ARY KEY, block B +| 4032: 4c 4f 42 29 3a 01 06 17 11 11 08 63 74 61 62 6c LOB):......ctabl +| 4048: 65 74 31 74 31 43 52 45 41 54 45 20 56 49 52 54 et1t1CREATE VIRT +| 4064: 55 41 4c 20 54 41 42 4c 45 20 74 31 20 55 53 49 UAL TABLE t1 USI +| 4080: 4e 47 20 66 74 73 35 28 63 6f 6e 74 65 6e 74 29 NG fts5(content) +| page 2 offset 4096 +| 0: 0d 00 00 00 03 0f bd 00 0f e8 0f ef 0f bd 00 00 ................ +| 4016: 00 00 00 00 00 00 00 00 00 00 00 00 00 24 84 80 .............$.. +| 4032: 80 80 80 01 03 00 4e 00 00 00 1e 06 30 61 62 61 ......N.....0aba +| 4048: 63 6b 01 02 02 04 02 66 74 00 02 22 04 04 6e 64 ck.....ft.....nd +| 4064: 6f 6e 04 67 90 38 2a 07 05 01 03 00 10 03 03 0f on.g.8*......... +| 4080: 0a 03 00 24 00 00 00 00 01 01 01 00 01 01 01 01 ...$............ +| page 3 offset 8192 +| 0: 0a 00 00 00 01 0f fa 00 0f fa 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 05 04 09 0c 01 02 ................ +| page 4 offset 12288 +| 0: 0d 00 00 00 03 0f e0 00 0f f6 0f ec 0f e0 00 00 ................ +| 4064: 0a 03 03 00 1b 61 62 61 6e 64 6f 6e 08 02 03 00 .....abandon.... +| 4080: 17 61 62 61 66 74 08 01 03 00 17 61 62 61 63 6b .abaft.....aback +| page 5 offset 16384 +| 0: 0d 00 00 00 03 0f ee 00 0f fa 0f f4 0f ee 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 04 03 ................ +| 4080: 03 00 0e 01 04 02 03 00 0e 01 04 01 03 00 0e 01 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| page 7 offset 24576 +| 0: 0d 00 00 00 03 0f d6 00 0f f4 0f e1 0f d6 00 00 ................ +| 4048: 00 00 00 00 00 00 09 03 02 1b 72 65 62 75 69 6c ..........rebuil +| 4064: 64 11 02 02 2b 69 6e 74 65 67 72 69 74 79 2d 63 d...+integrity-c +| 4080: 68 65 63 6b 0a 01 02 1d 6f 70 74 69 6d 69 7a 65 heck....optimize +| page 8 offset 28672 +| 0: 03 07 17 19 19 01 81 2d 74 61 62 6c 65 74 31 5f .......-tablet1_ +| 16: 69 64 78 74 31 5f 69 64 78 03 43 52 45 41 54 45 idxt1_idx.CREATE +| 32: 20 54 41 42 4c 45 20 27 74 31 5f 66 17 42 03 30 TABLE 't1_f.B.0 +| 48: 01 00 00 10 10 04 02 02 00 00 00 00 00 00 00 00 ................ +| 64: 70 00 00 00 00 00 00 00 00 00 00 00 70 00 00 00 p...........p... +| end c16.db +}]} {} + +do_catchsql_test 15.1 { + INSERT INTO t1(t1) VALUES('integrity-check'); +} {/*malformed database schema*/} + +#--------------------------------------------------------------------------- +# +reset_db +do_test 16.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 28672 pagesize 4096 filename c17.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 07 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 07 00 00 00 00 ................ +| 48: 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 ................ +| 96: 00 00 00 00 0d 00 00 00 07 0d d2 00 0f c4 0f 6d ...............m +| 112: 0f 02 0e ab 0e 4e 0d f6 0d d2 00 00 00 00 00 00 .....N.......... +| 3536: 00 00 22 07 06 17 11 11 01 31 74 61 62 6c 65 74 .........1tablet +| 3552: 32 74 32 07 43 52 45 41 54 45 20 54 41 42 4c 45 2t2.CREATE TABLE +| 3568: 20 74 32 28 78 29 56 06 06 17 1f 1f 01 7d 74 61 t2(x)V.......ta +| 3584: 62 6c 65 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 blet1_configt1_c +| 3600: 6f 6e 66 69 67 06 43 52 45 41 54 45 20 54 41 42 onfig.CREATE TAB +| 3616: 4c 45 20 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b LE 't1_config'(k +| 3632: 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 PRIMARY KEY, v) +| 3648: 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 5b 05 WITHOUT ROWID[. +| 3664: 07 17 21 21 01 81 01 74 61 62 6c 65 74 31 5f 64 ..!!...tablet1_d +| 3680: 6f 63 73 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 ocsizet1_docsize +| 3696: 05 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 .CREATE TABLE 't +| 3712: 31 5f 64 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 1_docsize'(id IN +| 3728: 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE +| 3744: 59 2c 20 73 7a 20 42 4c 4f 42 29 55 04 06 17 21 Y, sz BLOB)U...! +| 3760: 21 01 77 74 61 62 6c 65 74 31 5f 63 6f 6e 74 65 !.wtablet1_conte +| 3776: 6e 74 74 31 5f 63 6f 6e 74 65 6e 74 04 43 52 45 ntt1_content.CRE +| 3792: 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 6f ATE TABLE 't1_co +| 3808: 6e 74 65 6e 74 27 28 69 64 20 49 4e 54 45 47 45 ntent'(id INTEGE +| 3824: 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 63 R PRIMARY KEY, c +| 3840: 30 29 69 03 07 17 19 19 01 81 2d 74 61 62 6c 65 0)i.......-table +| 3856: 74 31 5f 69 64 78 74 31 5f 69 64 78 03 43 52 45 t1_idxt1_idx.CRE +| 3872: 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 69 64 ATE TABLE 't1_id +| 3888: 78 27 28 73 65 67 69 64 2c 20 74 65 72 6d 2c 20 x'(segid, term, +| 3904: 70 67 6e 6f 2c 20 50 52 49 4d 41 52 59 20 4b 45 pgno, PRIMARY KE +| 3920: 59 28 73 65 67 69 64 2c 20 74 65 72 6d 29 29 20 Y(segid, term)) +| 3936: 57 49 54 48 4f 55 54 20 52 4f 57 49 44 55 02 07 WITHOUT ROWIDU.. +| 3952: 17 1b 1b 01 81 01 74 61 62 6c 65 74 31 5f 64 61 ......tablet1_da +| 3968: 74 61 74 31 5f 64 61 74 61 02 43 52 45 41 54 45 tat1_data.CREATE +| 3984: 20 54 41 42 4c 45 20 27 74 31 5f 64 61 74 61 27 TABLE 't1_data' +| 4000: 28 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d (id INTEGER PRIM +| 4016: 41 52 59 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 ARY KEY, block B +| 4032: 4c 4f 42 29 3a 01 06 17 11 11 08 63 74 61 62 6c LOB):......ctabl +| 4048: 65 74 31 74 31 43 52 45 41 54 45 20 56 49 52 54 et1t1CREATE VIRT +| 4064: 55 41 4c 20 54 41 42 4c 45 20 74 31 20 55 53 49 UAL TABLE t1 USI +| 4080: 4e 47 20 66 74 73 35 28 63 6f 6e 74 65 6e 74 29 NG fts5(content) +| page 2 offset 4096 +| 0: 0d 00 00 00 03 0f bd 00 0f e8 0f ef 0f bd 00 00 ................ +| 4016: 00 00 00 00 00 00 00 00 00 00 00 00 00 24 84 80 .............$.. +| 4032: 80 80 80 01 03 00 4e 00 00 00 1e 06 30 61 62 61 ......N.....0aba +| 4048: 63 6b 01 02 02 04 02 66 74 00 02 22 04 04 6e 64 ck.....ft.....nd +| 4064: 6f 6e 03 02 02 04 0a 07 05 01 03 00 10 03 03 0f on.............. +| 4080: 0a 03 00 24 00 00 00 00 01 01 01 00 01 01 41 01 ...$..........A. +| page 3 offset 8192 +| 0: 0a 00 00 00 01 0f fa 00 0f fa 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 05 04 09 0c 01 02 ................ +| page 4 offset 12288 +| 0: 0d 00 00 00 03 0f e0 00 0f f6 0f ec 0f e0 00 00 ................ +| 4064: 0a 03 03 00 1b 61 62 61 6e 64 6f 6e 08 02 03 00 .....abandon.... +| 4080: 17 61 62 61 66 74 08 01 03 00 17 61 62 61 63 6b .abaft.....aback +| page 5 offset 16384 +| 0: 0d 00 00 00 03 0f ee 00 0f fa 0f f4 0f ee 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 04 03 ................ +| 4080: 03 00 0e 01 04 02 03 00 0e 01 04 01 03 00 0e 01 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| page 7 offset 24576 +| 0: 0d 00 00 00 03 0f d6 00 0f f4 0f e1 0f d6 00 00 ................ +| 4048: 00 00 00 00 00 00 09 03 02 1b 72 65 62 75 69 6c ..........rebuil +| 4064: 64 11 02 02 2b 69 6e 74 65 67 72 69 74 79 2d 63 d...+integrity-c +| 4080: 68 65 63 6b 0a 01 02 1d 6f 70 74 69 6d 69 7a 65 heck....optimize +| end c17.db +}]} {} + +do_catchsql_test 16.1 { +INSERT INTO t1(t1) VALUES('integrity-check'); +} {/.*fts5: corrupt.*/} + +#-------------------------------------------------------------------------- +reset_db +do_test 17.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 28672 pagesize 4096 filename c18.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 07 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 07 00 00 00 00 ................ +| 48: 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 ................ +| 96: 00 00 00 00 0d 00 00 00 07 0d d2 00 0f c4 0f 6d ...............m +| 112: 0f 02 0e ab 0e 4e 0d f6 0d d2 00 00 00 00 00 00 .....N.......... +| 3536: 00 00 22 07 06 17 11 11 01 31 74 61 62 6c 65 74 .........1tablet +| 3552: 32 74 32 07 43 52 45 41 54 45 20 54 41 42 4c 45 2t2.CREATE TABLE +| 3568: 20 74 32 28 78 29 56 06 06 17 1f 1f 01 7d 74 61 t2(x)V.......ta +| 3584: 62 6c 65 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 blet1_configt1_c +| 3600: 6f 6e 66 69 67 06 43 52 45 41 54 45 20 54 41 42 onfig.CREATE TAB +| 3616: 4c 45 20 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b LE 't1_config'(k +| 3632: 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 PRIMARY KEY, v) +| 3648: 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 5b 05 WITHOUT ROWID[. +| 3664: 07 17 21 21 01 81 01 74 61 62 6c 65 74 31 5f 64 ..!!...tablet1_d +| 3680: 6f 63 73 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 ocsizet1_docsize +| 3696: 05 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 .CREATE TABLE 't +| 3712: 31 5f 64 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 1_docsize'(id IN +| 3728: 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE +| 3744: 59 2c 20 73 7a 20 42 4c 4f 42 29 55 04 06 17 21 Y, sz BLOB)U...! +| 3760: 21 01 77 74 61 62 6c 65 74 31 5f 63 6f 6e 74 65 !.wtablet1_conte +| 3776: 6e 74 74 31 5f 63 6f 6e 74 65 6e 74 04 43 52 45 ntt1_content.CRE +| 3792: 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 6f ATE TABLE 't1_co +| 3808: 6e 74 65 6e 74 27 28 69 64 20 49 4e 54 45 47 45 ntent'(id INTEGE +| 3824: 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 63 R PRIMARY KEY, c +| 3840: 30 29 69 03 07 17 19 19 01 81 2d 74 61 62 6c 65 0)i.......-table +| 3856: 74 31 5f 69 64 78 74 31 5f 69 64 78 03 43 52 45 t1_idxt1_idx.CRE +| 3872: 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 69 64 ATE TABLE 't1_id +| 3888: 78 27 28 73 65 67 69 64 2c 20 74 65 72 6d 2c 20 x'(segid, term, +| 3904: 70 67 6e 6f 2c 20 50 52 49 4d 41 52 59 20 4b 45 pgno, PRIMARY KE +| 3920: 59 28 73 65 67 69 64 2c 20 74 65 72 6d 29 29 20 Y(segid, term)) +| 3936: 57 49 54 48 4f 55 54 20 52 4f 57 49 44 55 02 07 WITHOUT ROWIDU.. +| 3952: 17 1b 1b 01 81 01 74 61 62 6c 65 74 31 5f 64 61 ......tablet1_da +| 3968: 74 61 74 31 5f 64 61 74 61 02 43 52 45 41 54 45 tat1_data.CREATE +| 3984: 20 54 41 42 4c 45 20 27 74 31 5f 64 61 74 61 27 TABLE 't1_data' +| 4000: 28 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d (id INTEGER PRIM +| 4016: 41 52 59 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 ARY KEY, block B +| 4032: 4c 4f 42 29 3a 01 06 17 11 11 08 63 74 61 62 6c LOB):......ctabl +| 4048: 65 74 31 74 31 43 52 45 41 54 45 20 56 49 52 54 et1t1CREATE VIRT +| 4064: 55 41 4c 20 54 41 42 4c 45 20 74 31 20 55 53 49 UAL TABLE t1 USI +| 4080: 4e 47 20 66 74 73 35 28 63 6f 6e 74 65 6e 74 29 NG fts5(content) +| page 2 offset 4096 +| 0: 0d 00 00 00 03 0f bd 00 0f e8 0f ef 0f bd 00 00 ................ +| 4016: 00 00 00 00 00 00 00 00 00 00 00 00 00 24 84 80 .............$.. +| 4032: 80 80 80 01 03 00 4e 00 00 00 1e 06 30 61 62 61 ......N.....0aba +| 4048: 63 6b 01 02 02 04 02 66 74 00 02 22 04 04 6e 64 ck.....ft.....nd +| 4064: 6f 6e 03 02 02 04 0a 07 05 01 03 00 10 03 03 0f on.............. +| 4080: 0a 03 00 24 00 00 0a aa aa aa aa aa aa aa aa aa ...$............ +| page 3 offset 8192 +| 0: aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa ................ +| 16: aa aa aa aa aa aa aa aa 00 00 10 10 10 00 10 10 ................ +| 32: 10 10 a0 00 00 00 10 ff a0 00 ff 52 05 64 95 25 ...........R.d.% +| 48: 45 54 14 c2 05 44 14 24 c4 52 07 43 12 05 55 34 ET...D.$.R.C..U4 +| 64: 94 e4 72 06 67 47 33 52 86 36 f6 e7 46 56 e7 42 ..r.gG3R.6..FV.B +| 80: 90 d0 00 00 00 30 fb d0 00 fe 80 fe f0 fb 00 00 .....0.......... +| 4080: 00 00 00 00 00 00 00 00 00 00 05 04 09 0c 01 02 ................ +| page 4 offset 12288 +| 0: 0d 00 00 00 03 0f e0 00 0f f6 0f ec 0f e0 00 00 ................ +| 4064: 0a 03 03 00 1b 61 62 61 6e 64 6f 6e 08 02 03 00 .....abandon.... +| 4080: 17 61 62 61 66 74 08 01 03 00 17 61 62 61 63 6b .abaft.....aback +| page 5 offset 16384 +| 0: 0d 00 00 00 03 0f ee 00 0f fa 0f f4 0f ee 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 04 03 ................ +| 4080: 03 00 0e 01 04 02 03 00 0e 01 04 01 03 00 0e 01 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| page 7 offset 24576 +| 0: 0d 00 00 00 03 0f d6 00 0f f4 0f e1 0f d6 00 00 ................ +| 4048: 00 00 00 00 00 00 09 03 02 1b 72 65 62 75 69 6c ..........rebuil +| 4064: 64 11 02 02 2b 69 6e 74 65 67 72 69 74 79 2d 63 d...+integrity-c +| 4080: 68 65 63 6b 0a 01 02 1d 6f 70 74 69 6d 69 7a 65 heck....optimize +| end c18.db +}]} {} + +do_catchsql_test 17.1 { + SELECT * FROM t1 WHERE t1 MATCH 'abandon'; +} {/.*fts5: corrupt.*/} + +#-------------------------------------------------------------------------- +reset_db +do_test 18.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 28672 pagesize 4096 filename c19b.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 01 00 00 00 07 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 48: 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 ................ +| 80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 ................ +| 96: 00 2e 30 38 0d 00 00 00 07 0d d2 00 0f c4 0f 6d ..08...........m +| 112: 0f 02 0e ab 0e 4e 0d f6 0d d2 00 00 00 00 00 00 .....N.......... +| 3536: 00 00 22 07 06 17 11 11 01 31 74 61 62 6c 65 74 .........1tablet +| 3552: 32 74 32 07 43 52 45 41 54 45 20 54 41 42 4c 45 2t2.CREATE TABLE +| 3568: 20 74 32 28 78 29 56 06 06 17 1f 1f 01 7d 74 61 t2(x)V.......ta +| 3584: 62 6c 65 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 blet1_configt1_c +| 3600: 6f 6e 66 69 67 06 43 52 45 41 54 45 20 54 41 42 onfig.CREATE TAB +| 3616: 4c 45 20 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b LE 't1_config'(k +| 3632: 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 PRIMARY KEY, v) +| 3648: 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 5b 05 WITHOUT ROWID[. +| 3664: 07 17 21 21 01 81 01 74 61 62 6c 65 74 31 5f 64 ..!!...tablet1_d +| 3680: 6f 63 73 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 ocsizet1_docsize +| 3696: 05 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 .CREATE TABLE 't +| 3712: 31 5f 64 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 1_docsize'(id IN +| 3728: 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE +| 3744: 59 2c 20 73 7a 20 42 4c 4f 42 29 55 04 06 17 21 Y, sz BLOB)U...! +| 3760: 21 01 77 74 61 62 6c 65 74 31 5f 63 6f 6e 74 65 !.wtablet1_conte +| 3776: 6e 74 74 31 5f 63 6f 6e 74 65 6e 74 04 43 52 45 ntt1_content.CRE +| 3792: 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 6f ATE TABLE 't1_co +| 3808: 6e 74 65 6e 74 27 28 69 64 20 49 4e 54 45 47 45 ntent'(id INTEGE +| 3824: 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 63 R PRIMARY KEY, c +| 3840: 30 29 69 03 07 17 19 19 01 81 2d 74 61 62 6c 65 0)i.......-table +| 3856: 74 31 5f 69 64 78 74 31 5f 69 64 78 03 43 52 45 t1_idxt1_idx.CRE +| 3872: 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 69 64 ATE TABLE 't1_id +| 3888: 78 27 28 73 65 67 69 64 2c 20 74 65 72 6d 2c 20 x'(segid, term, +| 3904: 70 67 6e 6f 2c 20 50 52 49 4d 41 52 59 20 4b 45 pgno, PRIMARY KE +| 3920: 59 28 73 65 67 69 64 2c 20 74 65 72 6d 29 29 20 Y(segid, term)) +| 3936: 57 49 54 48 4f 55 54 20 52 4f 57 49 44 55 02 07 WITHOUT ROWIDU.. +| 3952: 17 1b 1b 01 81 01 74 61 62 6c 65 74 31 5f 64 61 ......tablet1_da +| 3968: 74 61 74 31 5f 64 61 74 61 02 43 52 45 41 54 45 tat1_data.CREATE +| 3984: 20 54 41 42 4c 45 20 27 74 31 5f 64 61 74 61 27 TABLE 't1_data' +| 4000: 28 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d (id INTEGER PRIM +| 4016: 41 52 59 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 ARY KEY, block B +| 4032: 4c 4f 42 29 3a 01 06 17 11 11 08 63 74 61 62 6c LOB):......ctabl +| 4048: 65 74 31 74 31 43 52 45 41 54 45 20 56 49 52 54 et1t1CREATE VIRT +| 4064: 55 41 4c 20 54 41 42 4c 45 20 74 31 20 55 53 49 UAL TABLE t1 USI +| 4080: 4e 47 20 66 74 73 35 28 63 6f 6e 74 65 6e 74 29 NG fts5(content) +| page 2 offset 4096 +| 0: 0d 09 a6 00 06 09 22 00 0f e8 09 22 0f bd 0f 2c ..............., +| 16: 09 bd 09 3c 00 00 00 00 00 00 00 00 00 00 00 00 ...<............ +| 2336: 00 00 18 0a 03 00 36 00 00 00 00 01 04 04 00 04 ......6......... +| 2352: 01 01 01 02 01 01 03 01 01 04 01 01 63 90 80 80 ............c... +| 2368: 80 80 01 04 00 81 4a 00 00 00 56 06 30 61 62 61 ......J...V.0aba +| 2384: 63 6b 08 01 04 04 6e 64 6f 6e 03 01 05 01 02 05 ck....ndon...... +| 2400: 63 74 69 76 65 08 01 02 04 6c 70 68 61 08 01 02 ctive....lpha... +| 2416: 03 74 6f 6d 08 01 01 06 62 61 63 6b 75 70 08 01 .tom....backup.. +| 2432: 01 0c 63 68 61 6e 6e 65 62 6f 6f 6d 65 72 08 01 ..channeboomer.. +| 2448: 07 01 6c 08 01 01 04 74 65 73 74 08 01 04 09 0a ..l....test..... +| 2464: 09 08 07 0a 10 05 0f 18 00 17 30 00 00 00 00 01 ..........0..... +| 2480: 03 03 00 03 01 01 01 02 01 01 03 01 01 8a 53 8c ..............S. +| 2496: 80 80 80 80 01 04 00 95 2a 00 00 05 35 0d 30 30 ........*...5.00 +| 2512: 31 30 66 66 61 30 30 30 66 66 61 05 02 1c 02 49 10ffa000ffa....I +| 2528: 33 34 33 35 32 34 35 34 31 35 34 34 35 32 30 35 3435245415445205 +| 2544: 34 34 31 34 32 34 63 34 35 32 30 32 37 37 34 33 441424c452027743 +| 2560: 31 35 66 36 39 36 34 37 38 32 37 32 38 37 33 36 15f6964782728736 +| 2576: 35 36 37 36 39 36 34 32 63 32 30 37 34 36 35 37 56769642c2074657 +| 2592: 32 36 64 32 63 32 30 37 30 05 02 12 02 3b 36 31 26d2c2070....;61 +| 2608: 37 32 31 32 31 30 31 37 37 37 34 36 31 36 32 35 7212101777461625 +| 2624: 63 36 35 37 34 33 31 35 66 36 33 36 66 36 65 37 c6574315f636f6e7 +| 2640: 34 36 35 36 65 37 34 37 34 33 31 35 66 36 33 36 4656e7474315f636 +| 2656: 66 36 65 37 34 36 35 36 65 05 04 07 07 01 04 31 f6e74656e......1 +| 2672: 66 62 64 05 02 19 01 44 32 34 38 34 38 30 38 30 fbd....D24848080 +| 2688: 38 30 38 30 30 31 30 33 30 30 34 65 30 30 30 30 80800103004e0000 +| 2704: 30 30 31 65 30 36 33 30 36 31 36 32 36 31 36 33 001e063061626163 +| 2720: 36 62 30 31 30 32 30 32 30 34 30 32 36 36 37 34 6b01020204026674 +| 2736: 30 32 30 32 30 32 30 34 30 34 36 65 05 02 1a 02 02020204046e.... +| 2752: 03 66 65 72 05 02 1d 01 28 33 65 37 34 36 35 36 .fer....(3e74656 +| 2768: 65 37 34 32 39 30 64 30 30 30 30 30 30 30 33 30 e74290d000000030 +| 2784: 66 62 64 30 30 30 66 65 38 30 66 65 66 30 66 62 fbd000fe80fef0fb +| 2800: 64 05 02 18 01 4a 34 31 35 32 35 39 32 30 34 62 d....J415259204b +| 2816: 34 35 35 39 32 63 32 30 36 32 36 63 36 66 36 33 45592c20626c6f63 +| 2832: 36 62 32 30 34 32 34 63 34 66 34 32 32 39 33 61 6b20424c4f42293a +| 2848: 30 31 30 36 31 37 31 31 31 31 30 38 36 33 37 34 0106171111086374 +| 2864: 36 31 36 32 36 63 36 35 37 34 33 31 37 34 33 31 61626c6574317431 +| 2880: 05 02 16 02 49 33 35 32 34 35 34 31 35 34 34 35 ....I35245415445 +| 2896: 32 30 35 36 34 39 35 32 35 34 35 35 34 31 34 63 205649525455414c +| 2912: 32 30 35 34 34 31 34 32 34 63 34 35 32 30 37 34 205441424c452074 +| 2928: 33 31 6f 30 35 35 35 33 34 39 34 65 34 37 32 30 31o05553494e4720 +| 2944: 36 36 37 34 37 33 33 35 32 38 36 33 36 66 05 02 6674733528636f.. +| 2960: 17 02 49 35 32 30 35 34 34 31 34 32 34 63 34 35 ..I5205441424c45 +| 2976: 32 30 32 37 37 34 33 31 35 66 36 33 36 66 36 65 202774315f636f6e +| 2992: 37 34 36 35 36 65 37 34 32 37 32 38 36 39 36 34 74656e7427286964 +| 3008: 32 30 34 39 34 65 35 34 34 35 34 37 34 35 35 32 20494e5445474552 +| 3024: 32 30 35 30 35 32 34 39 34 64 34 31 05 02 0e 44 205052494d41...D +| 3040: 29 62 30 35 30 37 31 37 32 31 32 31 30 31 38 31 )b05071721210181 +| 3056: 30 31 37 34 36 31 36 32 36 63 36 35 37 34 33 31 017461626c657431 +| 3072: 35 66 36 34 36 66 36 33 37 33 05 02 09 01 4a 35 5f646f6373....J5 +| 3088: 32 34 35 34 31 35 34 34 35 32 30 35 34 34 31 34 2454154452054414 +| 3104: 32 34 63 34 35 32 30 32 37 37 34 33 31 35 66 36 24c45202774315f6 +| 3120: 34 36 31 37 34 36 31 32 37 32 38 36 39 36 34 32 4617461272869642 +| 3136: 30 34 39 34 65 35 34 34 35 34 37 34 35 35 32 32 0494e54454745522 +| 3152: 30 35 30 35 32 34 39 34 64 05 02 15 03 3a 35 39 05052494d....:59 +| 3168: 32 30 34 62 34 35 35 39 32 63 32 30 36 33 33 30 204b45592c206330 +| 3184: 32 39 36 39 30 33 30 37 31 37 31 39 31 39 30 31 2969030717191901 +| 3200: 38 31 32 64 37 34 36 31 36 32 36 63 36 35 37 34 812d7461626c6574 +| 3216: 33 31 35 66 36 39 79 79 05 02 0f 02 49 34 32 30 315f69yy....I420 +| 3232: 35 32 34 66 35 37 34 39 34 34 35 35 30 32 30 37 524f574944550207 +| 3248: 31 37 31 62 31 62 30 31 38 31 30 31 37 34 36 31 171b1b0181017461 +| 3264: 36 32 36 63 36 37 37 34 33 31 35 66 36 34 36 31 626c6774315f6461 +| 3280: 37 34 36 31 37 34 33 31 35 66 36 34 36 31 37 34 746174315f646174 +| 3296: 36 31 30 32 34 33 05 02 14 02 07 66 36 39 36 34 610243.....f6964 +| 3312: 37 38 05 02 11 01 4a 36 34 36 66 36 65 30 33 30 78....J646f6e030 +| 3328: 32 30 32 30 34 30 61 30 37 30 35 30 31 30 33 30 202040a070501030 +| 3344: 30 31 30 30 33 30 33 30 66 30 61 30 33 30 30 32 01003030f0a03002 +| 3360: 34 30 30 30 30 30 30 30 30 30 31 30 31 30 31 30 4000000000101010 +| 3376: 30 30 31 30 31 30 31 30 31 30 61 30 30 30 30 30 0010101010a00000 +| 3392: 30 05 02 1b 02 49 35 32 37 32 38 36 39 36 34 32 0....I5272869642 +| 3408: 30 34 39 34 65 35 34 34 35 34 37 34 35 35 32 32 0494e54454745522 +| 3424: 30 35 30 35 32 34 39 34 64 34 31 35 32 35 39 32 05052494d4152592 +| 3440: 30 34 62 34 35 35 39 32 63 32 30 37 33 37 61 32 04b45592c20737a2 +| 3456: 30 34 32 34 63 34 66 34 32 32 39 35 35 30 34 05 0424c4f42295504. +| 3472: 04 06 07 02 49 37 36 65 36 66 32 63 32 30 35 30 ....I76e6f2c2050 +| 3488: 35 32 34 39 34 64 34 31 35 32 35 39 32 30 34 62 52494d415259204b +| 3504: 34 35 35 39 32 38 37 33 36 35 36 37 36 39 36 34 4559287365676964 +| 3520: 32 63 32 30 37 34 36 35 37 32 36 64 32 39 32 39 2c207465726d2929 +| 3536: 32 30 35 37 34 39 35 34 34 38 34 66 35 35 05 02 20574954484f55.. +| 3552: 13 02 49 39 37 61 36 35 37 34 33 31 35 66 36 34 ..I97a6574315f64 +| 3568: 36 66 36 33 37 33 36 39 37 61 36 35 30 35 34 33 6f6373697a650543 +| 3584: 35 32 34 35 34 31 35 34 34 35 32 30 35 34 34 31 5245415445205441 +| 3600: 34 32 34 63 34 35 32 30 32 37 37 34 33 31 35 66 424c45202774315f +| 3616: 36 34 36 66 36 33 37 33 36 39 37 61 05 04 05 07 646f6373697a.... +| 3632: 01 0e 37 34 30 34 34 33 35 32 34 35 34 31 35 34 ..74044352454154 +| 3648: 05 04 08 07 02 49 36 32 39 32 30 35 37 34 39 35 .....I6292057495 +| 3664: 34 34 38 34 66 35 35 35 34 32 30 35 32 34 66 35 4484f555420524f5 +| 3680: 37 34 39 34 34 35 62 30 35 30 37 31 37 32 31 32 749445b050717212 +| 3696: 31 30 31 38 31 30 31 37 34 36 31 36 32 36 63 36 10181017461626c6 +| 3712: 35 37 34 33 31 35 66 36 34 36 66 36 33 37 33 05 574315f646f6373. +| 3728: 02 04 01 06 62 61 63 6b 75 70 05 02 1e 02 05 65 ....backup.....e +| 3744: 61 6d 65 72 05 02 02 02 05 6f 6f 6d 65 72 05 01 amer.....oomer.. +| 3760: 02 40 75 6d 6d 32 34 63 34 35 32 30 32 37 37 34 .@umm24c45202774 +| 3776: 33 31 35 66 36 33 36 66 36 65 36 36 36 39 36 37 315f636f6e666967 +| 3792: 32 37 32 38 36 62 32 30 35 30 35 32 34 39 34 64 27286b205052494d +| 3808: 34 31 35 32 35 39 32 30 34 62 34 35 35 39 32 63 415259204b45592c +| 3824: 32 30 05 02 03 01 04 79 65 6b 72 05 02 10 04 11 20.....yekr..... +| 3840: 4e 41 09 49 08 2d 4f 4e 4e 2e 4f 3f 4e 0c 4f 4f NA.I.-ONN.O?N.OO +| 3856: 4e 4f 14 4e 0b 0a 09 45 0f ef 00 14 2a 00 00 00 NO.N...E....*... +| 3872: 00 01 02 02 00 02 01 01 01 02 01 01 81 09 88 80 ................ +| 3888: 80 80 80 01 04 00 82 16 00 00 00 79 06 30 61 62 ...........y.0ab +| 3904: 61 63 6b 08 02 07 04 04 6e 64 6f 6e 08 02 05 02 ack.....ndon.... +| 3920: 05 63 74 69 76 65 04 02 02 04 02 0b 02 04 6c 70 .ctive........lp +| 3936: 68 61 08 04 02 0a 02 03 74 6b 6d 06 02 02 03 02 ha......tkm..... +| 3952: 6f 6d 08 02 09 05 02 69 63 07 02 02 01 06 62 61 om.....ic.....ba +| 3968: 63 6b 75 70 08 02 04 02 05 6f 6f 6d 65 72 05 02 ckup.....oomer.. +| 3984: 02 01 0c 63 68 61 6e 6e 65 62 6f 6f 6d 65 72 08 ...channeboomer. +| 4000: 02 08 07 01 6c 08 02 03 01 04 74 65 73 74 08 02 ....l.....test.. +| 4016: 06 04 0a 09 0d 0a 08 07 07 0b 0a 11 06 24 84 80 .............$.. +| 4032: 80 80 80 01 03 00 4e 00 00 00 1e 06 30 61 62 61 ......N.....0aba +| 4048: 63 6b 01 08 02 04 02 66 74 00 02 22 04 04 6e 64 ck.....ft.....nd +| 4064: 6f 6e 03 02 02 04 0a 07 05 01 03 00 10 06 22 00 on.............. +| 4080: 00 00 11 24 00 00 00 00 01 01 01 00 01 01 01 01 ...$............ +| page 3 offset 8192 +| 0: 0a 00 00 00 04 0f e5 00 0f fa 0f f3 0f ec 0f e5 ................ +| 4064: 00 00 00 00 00 06 04 01 0c 01 04 02 06 04 01 0c ................ +| 4080: 01 03 02 06 04 01 0c 01 02 02 05 04 09 0c 01 02 ................ +| page 4 offset 12288 +| 0: 0d 0f 68 00 06 08 98 00 0f f6 0f ec 0f d5 08 98 ..h............. +| 16: 0f c1 0f b6 0f 68 0f 68 00 00 00 00 00 00 00 00 .....h.h........ +| 2192: 00 00 00 00 00 00 00 00 8d 4d 05 04 00 9b 1f 62 .........M.....b +| 2208: 65 61 6d 65 72 20 62 75 6d 6d 32 34 63 34 35 32 eamer bumm24c452 +| 2224: 30 32 37 37 34 33 31 35 66 36 33 36 66 36 65 36 02774315f636f6e6 +| 2240: 36 36 39 36 37 32 37 32 38 36 62 32 30 35 30 35 6696727286b20505 +| 2256: 32 34 39 34 64 34 31 35 32 35 39 32 30 34 62 34 2494d415259204b4 +| 2272: 35 35 39 32 63 32 30 0a 37 36 32 39 32 30 35 37 5592c20.76292057 +| 2288: 34 39 35 34 34 38 34 66 35 35 35 34 32 30 35 32 4954484f55542052 +| 2304: 34 66 35 37 34 39 34 34 35 62 30 35 30 37 31 37 4f5749445b050717 +| 2320: 32 31 32 31 30 31 38 31 30 31 37 34 36 31 36 32 2121018101746162 +| 2336: 36 63 36 35 37 34 33 31 35 66 36 34 36 66 36 33 6c6574315f646f63 +| 2352: 37 33 0a 36 39 37 61 36 35 37 34 33 31 35 66 36 73.697a6574315f6 +| 2368: 34 36 66 36 33 37 33 36 39 37 61 36 35 30 35 34 46f6373697a65054 +| 2384: 33 35 32 34 35 34 31 35 34 34 35 32 30 35 34 34 3524541544520544 +| 2400: 31 34 32 34 63 34 35 32 30 32 37 37 34 33 31 35 1424c45202774315 +| 2416: 66 36 34 36 66 36 33 37 33 36 39 37 61 0a 36 35 f646f6373697a.65 +| 2432: 32 37 32 38 36 39 36 34 32 30 34 39 34 65 35 34 2728696420494e54 +| 2448: 34 35 34 37 34 35 35 32 32 30 35 30 35 32 34 39 4547455220505249 +| 2464: 34 64 34 31 35 32 35 39 32 30 34 62 34 35 35 39 4d415259204b4559 +| 2480: 32 63 32 30 37 33 37 61 32 30 34 32 34 63 34 66 2c20737a20424c4f +| 2496: 34 32 32 39 35 35 30 34 0a 30 36 31 37 32 31 32 42295504.0617212 +| 2512: 31 30 31 37 37 37 34 36 31 36 32 35 63 36 35 37 101777461625c657 +| 2528: 34 33 31 35 66 36 33 36 66 36 65 37 34 36 35 36 4315f636f6e74656 +| 2544: 65 37 34 37 34 33 31 35 66 36 33 36 66 36 65 37 e7474315f636f6e7 +| 2560: 34 36 35 36 65 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 4656e........... +| 2576: 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b ................ +| 2592: 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b ................ +| 2608: 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b ................ +| 2624: 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b ................ +| 2640: 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b ................ +| 2656: 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 37 34 30 .............740 +| 2672: 34 34 33 35 32 34 35 34 31 35 34 0a 34 35 32 30 44352454154.4520 +| 2688: 35 34 34 31 34 32 34 63 34 35 32 30 32 37 37 34 5441424c45202774 +| 2704: 33 31 35 66 36 33 36 66 36 65 37 34 36 35 36 65 315f636f6e74656e +| 2720: 37 34 32 37 32 38 36 39 36 34 32 30 34 39 34 65 742728696420494e +| 2736: 35 34 34 35 34 37 34 35 35 32 32 30 35 30 35 62 544547455220505b +| 2752: 30 35 30 37 31 37 32 31 32 31 30 31 38 31 30 31 0507172121018101 +| 2768: 37 34 36 31 36 32 36 63 36 35 37 34 33 31 35 66 7461626c6574315f +| 2784: 36 34 36 66 36 33 37 33 0a 36 39 37 61 36 35 37 646f6373.697a657 +| 2800: 34 33 31 35 66 36 34 36 66 36 33 37 33 36 39 37 4315f646f6373697 +| 2816: 61 36 35 30 35 34 33 35 32 34 35 34 31 35 34 34 a650543524541544 +| 2832: 35 32 30 35 34 34 31 34 32 34 63 34 35 32 30 32 5205441424c45202 +| 2848: 37 37 34 33 31 35 66 36 34 36 66 36 33 37 33 36 774315f646f63736 +| 2864: 39 37 61 0a 36 35 32 37 32 38 36 39 36 34 32 30 97a.652728696420 +| 2880: 34 39 34 65 35 34 34 35 34 37 34 35 35 32 32 30 494e544547455220 +| 2896: 35 30 35 32 34 39 34 64 34 31 35 32 35 39 32 30 5052494d41525920 +| 2912: 34 62 34 35 35 39 32 63 32 30 37 33 37 61 32 30 4b45592c20737a20 +| 2928: 34 32 34 63 34 66 34 32 32 39 35 35 30 34 0a 30 424c4f42295504.0 +| 2944: 36 31 37 32 31 32 31 30 31 37 37 37 34 36 31 36 6172121017774616 +| 2960: 32 35 63 36 35 37 34 33 31 35 66 36 33 36 66 36 25c6574315f636f6 +| 2976: 65 37 34 36 35 36 65 37 34 37 34 33 31 35 66 36 e74656e7474315f6 +| 2992: 33 36 66 36 65 37 34 36 35 36 65 0b 0b 0b 0b 0b 36f6e74656e..... +| 3008: 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b ................ +| 3024: 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b ................ +| 3040: 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b ................ +| 3056: 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b ................ +| 3072: 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b ................ +| 3088: 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b ................ +| 3104: 0b 0b 0b 37 34 30 34 34 33 35 32 34 35 34 31 35 ...7404435245415 +| 3120: 34 0a 34 35 32 30 35 34 34 31 34 32 34 63 34 35 4.45205441424c45 +| 3136: 32 30 32 37 37 34 33 31 35 66 36 33 36 66 36 65 202774315f636f6e +| 3152: 37 34 36 35 36 65 37 34 32 37 32 38 36 39 36 34 74656e7427286964 +| 3168: 32 30 34 39 34 65 35 34 34 35 34 37 34 35 35 32 20494e5445474552 +| 3184: 32 30 35 30 35 32 34 39 34 64 34 31 0a 35 32 35 205052494d41.525 +| 3200: 39 32 30 34 62 34 35 35 39 32 63 32 30 36 33 33 9204b45592c20633 +| 3216: 30 32 39 36 39 30 33 30 37 31 37 31 39 31 39 30 0296903071719190 +| 3232: 31 38 31 32 64 37 34 36 31 36 32 36 63 36 35 37 1812d7461626c657 +| 3248: 34 33 31 35 66 36 39 c3 bf c3 bf 7e c3 bf c3 89 4315f69....~.... +| 3264: 4b 52 c2 81 35 66 36 39 36 34 37 38 0a 30 33 34 KR..5f696478.034 +| 3280: 33 35 32 34 35 34 31 35 34 34 35 32 30 35 34 34 3524541544520544 +| 3296: 31 34 32 34 63 34 35 32 30 32 37 37 34 33 31 35 1424c45202774315 +| 3312: 66 36 39 36 34 37 38 32 37 32 38 37 33 36 35 36 f696478272873656 +| 3328: 37 36 39 36 34 32 63 32 30 37 34 36 35 37 32 36 769642c207465726 +| 3344: 64 32 63 32 30 37 30 0a 36 37 36 65 36 66 32 63 d2c2070.676e6f2c +| 3360: 32 30 35 30 35 32 34 39 34 64 34 31 35 32 35 39 205052494d415259 +| 3376: 32 30 34 62 34 35 35 39 32 38 37 33 36 35 36 37 204b455928736567 +| 3392: 36 39 36 34 32 63 32 30 37 34 36 35 37 32 36 64 69642c207465726d +| 3408: 32 39 32 39 32 30 35 37 34 39 35 34 34 38 34 66 292920574954484f +| 3424: 35 35 0a 35 34 32 30 35 32 34 66 35 37 34 39 34 55.5420524f57494 +| 3440: 34 35 35 30 32 30 37 31 37 31 62 31 62 30 31 38 4550207171b1b018 +| 3456: 31 30 31 37 34 36 31 36 32 36 63 36 37 37 34 33 1017461626c67743 +| 3472: 31 35 66 36 34 36 31 37 34 36 31 37 34 33 31 35 15f6461746174315 +| 3488: 66 36 34 36 31 37 34 36 31 30 32 34 33 0a 35 32 f646174610243.52 +| 3504: 34 35 34 31 35 34 34 35 32 30 35 34 34 31 34 32 4541544520544142 +| 3520: 34 63 34 35 32 30 32 37 37 34 33 31 35 66 36 34 4c45202774315f64 +| 3536: 36 31 37 34 36 31 32 37 32 38 36 39 36 34 32 30 6174612728696420 +| 3552: 34 39 34 65 35 34 34 35 34 37 34 35 35 32 32 30 494e544547455220 +| 3568: 35 30 35 32 34 39 34 64 0a 34 31 35 32 35 39 32 5052494d.4152592 +| 3584: 30 34 62 34 35 35 39 32 63 32 30 36 32 36 63 36 04b45592c20626c6 +| 3600: 66 36 33 36 62 32 30 34 32 34 63 34 66 34 32 32 f636b20424c4f422 +| 3616: 39 33 61 30 31 30 36 31 37 31 31 31 31 30 38 36 93a0106171111086 +| 3632: 33 37 34 36 31 36 32 36 63 36 35 37 34 33 31 37 37461626c6574317 +| 3648: 34 33 31 0a 34 33 35 32 34 35 34 31 35 34 34 35 431.435245415445 +| 3664: 32 30 35 36 34 39 35 32 35 34 35 35 34 31 34 63 205649525455414c +| 3680: 32 30 35 34 34 31 34 32 34 63 34 35 32 30 37 34 205441424c452074 +| 3696: 33 31 c3 94 30 35 35 35 33 34 39 34 65 34 37 32 31..05553494e472 +| 3712: 30 36 36 37 34 37 33 33 35 32 38 36 33 36 66 0a 06674733528636f. +| 3728: 33 65 37 34 36 35 36 65 37 34 32 39 30 64 30 30 3e74656e74290d00 +| 3744: 30 30 30 30 30 33 30 66 62 64 30 30 30 66 65 38 0000030fbd000fe8 +| 3760: 30 66 65 66 30 66 62 64 0a 5b 31 66 62 64 5d 32 0fef0fbd.[1fbd]2 +| 3776: 34 38 34 38 30 38 30 38 30 38 30 30 31 30 33 30 4848080808001030 +| 3792: 30 34 65 30 30 30 30 30 30 31 65 30 36 33 30 36 04e0000001e06306 +| 3808: 31 36 32 36 31 36 33 36 62 30 31 30 32 30 32 30 16261636b0102020 +| 3824: 34 30 32 36 36 37 34 30 32 30 32 30 32 30 34 30 4026674020202040 +| 3840: 34 36 65 0a 36 34 36 66 36 65 30 33 30 32 30 32 46e.646f6e030202 +| 3856: 30 34 30 61 30 37 30 35 30 31 30 33 30 30 31 30 040a070501030010 +| 3872: 30 33 30 33 30 66 30 61 30 33 30 30 32 34 30 30 03030f0a03002400 +| 3888: 30 30 30 30 30 30 30 31 30 31 30 31 30 30 30 31 0000000101010001 +| 3904: 30 31 30 31 30 31 30 61 30 30 30 30 30 30 0a 30 0101010a000000.0 +| 3920: 31 30 66 66 61 30 30 30 66 66 61 0a 5b 32 66 65 10ffa000ffa.[2fe +| 3936: 72 20 62 61 63 6b 75 70 0f ca 00 4e 81 1d 61 6c r backup...N..al +| 3952: 70 68 61 20 63 68 61 6e 6e 65 6c 20 c2 af 62 61 pha channel ..ba +| 3968: 63 6b 75 70 20 61 62 61 6e 64 6f 6e 20 74 65 73 ckup abandon tes +| 3984: 74 20 61 62 61 63 6b 20 63 68 61 6e 6e 65 62 6f t aback channebo +| 4000: 6f 6d 65 72 20 61 74 6f 6d 20 61 6c 70 68 61 20 omer atom alpha +| 4016: 61 63 74 69 76 65 09 07 03 00 19 61 74 6f 6d 69 active.....atomi +| 4032: 63 07 06 03 00 15 61 74 6b 6d 0f e0 00 0b 19 62 c.....atkm.....b +| 4048: 6f 6f 6d 65 72 09 04 03 00 19 61 63 74 69 76 65 oomer.....active +| 4064: 00 00 00 0c 1b 61 62 61 6e 64 6f 6e 08 02 03 00 .....abandon.... +| 4080: 17 61 66 21 66 74 08 01 03 00 17 61 62 61 63 6b .af!ft.....aback +| page 5 offset 16384 +| 0: 0d 0f ee 00 06 0f d6 00 0f fa 0f f4 0f e8 0f e2 ................ +| 16: 0f dc 0f d6 0f d0 0f d0 00 00 00 00 00 00 00 00 ................ +| 4048: 0f ee 00 06 0e 0a 04 07 03 00 0e 01 04 06 03 00 ................ +| 4064: 0e 01 04 05 03 00 0e 1d 04 04 03 00 0e 01 00 00 ................ +| 4080: 00 06 0e 01 04 02 03 00 0e 01 04 01 03 00 0e 01 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| page 7 offset 24576 +| 0: 0d 00 00 00 03 0f d6 00 0f f4 0f e1 0f d6 00 00 ................ +| 4048: 00 00 00 00 00 00 09 03 02 1b 72 65 62 75 69 6c ..........rebuil +| 4064: 64 11 02 02 2b 69 6e 74 65 67 72 69 74 79 2d 63 d...+integrity-c +| 4080: 68 65 63 6b 0a 01 02 1d 6f 70 74 69 6d 69 7a 65 heck....optimize +| end c19b.db +}]} {} + +do_catchsql_test 18.1 { + INSERT INTO t1(t1) VALUES('optimize'); +} {/.*fts5: corrupt.*/} + +#-------------------------------------------------------------------------- +reset_db +do_test 19.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 28672 pagesize 4096 filename c20b.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 01 00 00 00 07 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 48: 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 ................ +| 80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 ................ +| 96: 00 2e 30 38 0d 00 00 00 07 0d d2 00 0f c4 0f 6d ..08...........m +| 112: 0f 02 0e ab 0e 4e 0d f6 0d d2 00 00 00 00 00 00 .....N.......... +| 3536: 00 00 22 07 06 17 11 11 01 31 74 61 62 6c 65 74 .........1tablet +| 3552: 32 74 32 07 43 52 45 41 54 45 20 54 41 42 4c 45 2t2.CREATE TABLE +| 3568: 20 74 32 28 78 29 56 06 06 17 1f 1f 01 7d 74 61 t2(x)V.......ta +| 3584: 62 6c 65 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 blet1_configt1_c +| 3600: 6f 6e 66 69 67 06 43 52 45 41 54 45 20 54 41 42 onfig.CREATE TAB +| 3616: 4c 45 20 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b LE 't1_config'(k +| 3632: 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 PRIMARY KEY, v) +| 3648: 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 5b 05 WITHOUT ROWID[. +| 3664: 07 17 21 21 01 81 01 74 61 62 6c 65 74 31 5f 64 ..!!...tablet1_d +| 3680: 6f 63 73 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 ocsizet1_docsize +| 3696: 05 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 .CREATE TABLE 't +| 3712: 31 5f 64 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 1_docsize'(id IN +| 3728: 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE +| 3744: 59 2c 20 73 7a 20 42 4c 4f 42 29 55 04 06 17 21 Y, sz BLOB)U...! +| 3760: 21 01 77 74 61 62 6c 65 74 31 5f 63 6f 6e 74 65 !.wtablet1_conte +| 3776: 6e 74 74 31 5f 63 6f 6e 74 65 6e 74 04 43 52 45 ntt1_content.CRE +| 3792: 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 6f ATE TABLE 't1_co +| 3808: 6e 74 65 6e 74 27 28 69 64 20 49 4e 54 45 47 45 ntent'(id INTEGE +| 3824: 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 63 R PRIMARY KEY, c +| 3840: 30 29 69 03 07 17 19 19 01 81 2d 74 61 62 6c 65 0)i.......-table +| 3856: 74 31 5f 69 64 78 74 31 5f 69 64 78 03 43 52 45 t1_idxt1_idx.CRE +| 3872: 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 69 64 ATE TABLE 't1_id +| 3888: 78 27 28 73 65 67 69 64 2c 20 74 65 72 6d 2c 20 x'(segid, term, +| 3904: 70 67 6e 6f 2c 20 50 52 49 4d 41 52 59 20 4b 45 pgno, PRIMARY KE +| 3920: 59 28 73 65 67 69 64 2c 20 74 65 72 6d 29 29 20 Y(segid, term)) +| 3936: 57 49 54 48 4f 55 54 20 52 4f 57 49 44 55 02 07 WITHOUT ROWIDU.. +| 3952: 17 1b 1b 01 81 01 74 61 62 6c 65 74 31 5f 64 61 ......tablet1_da +| 3968: 74 61 74 31 5f 64 61 74 61 02 43 52 45 41 54 45 tat1_data.CREATE +| 3984: 20 54 41 42 4c 45 20 27 74 31 5f 64 61 74 61 27 TABLE 't1_data' +| 4000: 28 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d (id INTEGER PRIM +| 4016: 41 52 59 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 ARY KEY, block B +| 4032: 4c 4f 42 29 3a 01 06 17 11 11 08 63 74 61 62 6c LOB):......ctabl +| 4048: 65 74 31 74 31 43 52 45 41 54 45 20 56 49 52 54 et1t1CREATE VIRT +| 4064: 55 41 4c 20 54 41 42 4c 45 20 74 31 20 55 53 49 UAL TABLE t1 USI +| 4080: 4e 47 20 66 74 73 35 28 63 6f 6e 74 65 6e 74 29 NG fts5(content) +| page 2 offset 4096 +| 0: 0d 0f 20 00 05 0e a0 00 0f e8 0e a0 0f bd 0f 34 .. ............4 +| 16: 0e b7 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 3744: 15 0a 03 00 30 00 00 00 00 01 03 03 00 03 01 01 ....0........... +| 3760: 01 02 01 01 03 01 01 62 8c 80 80 80 80 01 04 00 .......b........ +| 3776: 81 48 00 00 00 55 06 30 61 62 61 63 6b 08 01 04 .H...U.0aback... +| 3792: 04 6e 64 6f 6e 03 01 05 01 02 05 63 74 69 76 65 .ndon......ctive +| 3808: 08 01 02 04 6c 70 68 61 08 01 02 03 74 6f 6d 08 ....lpha....tom. +| 3824: 01 01 06 62 61 63 6b 75 70 08 01 02 05 6f 6f 6d ...backup....oom +| 3840: 65 72 08 01 01 07 63 68 61 6e 6e 65 6c 08 01 01 er....channel... +| 3856: 04 74 65 73 74 08 01 04 09 0a 09 08 07 0a 09 0b .test........... +| 3872: 0f ef 00 14 2a 00 00 00 00 01 02 02 00 02 01 01 ....*........... +| 3888: 01 02 01 01 81 01 88 80 80 80 80 01 04 00 82 06 ................ +| 3904: 00 00 00 72 06 30 61 62 61 63 6b 08 02 07 04 04 ...r.0aback..... +| 3920: 6e 64 6f 6e 08 02 05 02 05 63 74 69 76 65 04 02 ndon.....ctive.. +| 3936: 02 04 02 0b 02 04 6c 70 68 61 08 04 02 0a 02 03 ......lpha...... +| 3952: 74 6f 6d 06 02 02 02 02 09 05 02 69 63 07 02 02 tom........ic... +| 3968: 01 06 62 61 63 6b 75 70 08 02 04 02 05 6f 6f 66 ..backup.....oof +| 3984: 65 72 05 02 02 04 03 6d 65 72 08 02 08 01 07 63 er.....mer.....c +| 4000: 68 61 6e 6e 65 6c 08 02 03 01 04 74 65 73 74 08 hannel.....test. +| 4016: 02 06 04 0a 09 0d 0a 0b 07 0b 0a 08 0c 24 84 80 .............$.. +| 4032: 80 80 80 01 03 00 4e 00 00 00 1e 06 30 61 62 61 ......N.....0aba +| 4048: 63 6b 01 02 66 04 00 22 74 00 02 22 04 04 6e 64 ck..f...t.....nd +| 4064: 6f 6e 03 02 02 04 0a 07 05 01 03 00 10 06 06 00 on.............. +| 4080: 00 00 11 24 00 00 00 00 01 01 01 00 01 01 01 01 ...$............ +| page 3 offset 8192 +| 0: 0a 00 00 00 03 0f ec 00 0f fa 0f f3 0f ec 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 00 06 04 01 0c ................ +| 4080: 01 03 02 06 04 01 0c 01 02 02 05 04 09 0c 01 02 ................ +| page 4 offset 12288 +| 0: 0d 0f e0 00 06 0f b6 00 0f f6 0f ec 0f d5 0f ca ................ +| 16: 0f c1 0f b6 0f 70 0f 70 00 00 00 00 00 00 00 00 .....p.p........ +| 3952: 0f e0 00 46 81 0d 61 6c 70 68 61 20 63 68 61 6e ...F..alpha chan +| 3968: 6e 65 6c 20 62 61 63 6b 75 70 20 61 62 61 6e 64 nel backup aband +| 3984: 6f 6e 20 74 65 73 74 20 61 62 61 63 6b 20 62 6f on test aback bo +| 4000: 6f 6d 65 72 20 61 74 6f 6d 20 61 6c 70 68 61 20 omer atom alpha +| 4016: 61 63 74 69 76 65 09 07 03 00 19 61 74 6f 6d 69 active.....atomi +| 4032: 63 07 06 03 00 15 61 74 6f 6d 09 05 03 00 19 62 c.....atom.....b +| 4048: 6f 6f 66 65 72 09 04 03 00 19 61 63 74 69 76 65 oofer.....active +| 4064: 00 00 00 0c 1b 61 62 61 6e 64 6f 6e 08 02 03 00 .....abandon.... +| 4080: 17 61 62 61 66 74 08 01 03 00 17 61 62 61 63 6b .abaft.....aback +| page 5 offset 16384 +| 0: 0d 0f ee 00 06 0f d6 00 0f fa 0f f4 0f e8 0f e2 ................ +| 16: 0f dc 0f d6 0f d0 0f d0 00 00 00 00 00 00 00 00 ................ +| 4048: 0f ee 00 06 0e 0a 04 07 03 00 0e 01 04 06 03 00 ................ +| 4064: 0e 01 04 05 03 00 0e 01 04 04 03 00 0e 01 00 00 ................ +| 4080: 00 06 0e 01 04 02 03 00 0e 01 04 01 03 00 0e 01 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| page 7 offset 24576 +| 0: 0d 00 00 00 03 0f d6 00 0f f4 0f e1 0f d6 00 00 ................ +| 4048: 00 00 00 00 00 00 09 03 02 1b 72 65 62 75 69 6c ..........rebuil +| 4064: 64 11 02 02 2b 69 6e 74 65 67 72 69 74 79 2d 63 d...+integrity-c +| 4080: 86 65 63 6b 0a 01 02 1d 6f 70 74 69 6d 69 7a 65 .eck....optimize +| end c20b.db +}]} {} + +do_catchsql_test 19.1 { + INSERT INTO t1(t1) VALUES('optimize'); +} {/.*fts5: corrupt.*/} + +#-------------------------------------------------------------------------- +reset_db +do_test 20.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 28672 pagesize 4096 filename crash-cf347c523f793c.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 07 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 07 00 00 00 00 ................ +| 48: 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 ................ +| 96: 00 00 00 00 0d 00 00 00 07 0d d2 00 0f c4 0f 6d ...............m +| 112: 0f 02 0e ab 0e 4e 0d f6 0d d2 00 00 00 00 00 00 .....N.......... +| 3536: 00 00 22 07 06 17 11 11 01 31 74 61 62 6c 65 74 .........1tablet +| 3552: 32 74 32 07 43 52 45 41 54 45 20 54 41 42 4c 45 2t2.CREATE TABLE +| 3568: 20 74 32 28 78 29 56 06 06 17 1f 1f 01 7d 74 61 t2(x)V.......ta +| 3584: 62 6c 65 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 blet1_configt1_c +| 3600: 6f 6e 66 69 67 06 43 52 45 41 54 45 20 54 41 42 onfig.CREATE TAB +| 3616: 4c 45 20 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b LE 't1_config'(k +| 3632: 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 PRIMARY KEY, v) +| 3648: 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 5b 05 WITHOUT ROWID[. +| 3664: 07 17 21 21 01 81 01 74 61 62 6c 65 74 31 5f 64 ..!!...tablet1_d +| 3680: 6f 63 73 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 ocsizet1_docsize +| 3696: 05 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 .CREATE TABLE 't +| 3712: 31 5f 64 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 1_docsize'(id IN +| 3728: 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE +| 3744: 59 2c 20 73 7a 20 42 4c 4f 42 29 55 04 06 17 21 Y, sz BLOB)U...! +| 3760: 21 01 77 74 61 62 6c 65 74 31 5f 63 6f 6e 74 65 !.wtablet1_conte +| 3776: 6e 74 74 31 5f 63 6f 6e 74 65 6e 74 04 43 52 45 ntt1_content.CRE +| 3792: 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 6f ATE TABLE 't1_co +| 3808: 6e 74 65 6e 74 27 28 69 64 20 49 4e 54 45 47 45 ntent'(id INTEGE +| 3824: 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 63 R PRIMARY KEY, c +| 3840: 30 29 69 03 07 17 19 19 01 81 2d 74 61 62 6c 65 0)i.......-table +| 3856: 74 31 5f 69 64 78 74 31 5f 69 64 78 03 43 52 45 t1_idxt1_idx.CRE +| 3872: 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 69 64 ATE TABLE 't1_id +| 3888: 78 27 28 73 65 67 69 64 2c 20 74 65 72 6d 2c 20 x'(segid, term, +| 3904: 70 67 6e 6f 2c 20 50 52 49 4d 41 52 59 20 4b 45 pgno, PRIMARY KE +| 3920: 59 28 73 65 67 69 64 2c 20 74 65 72 6d 29 29 20 Y(segid, term)) +| 3936: 57 49 54 48 4f 55 54 20 52 4f 57 49 44 55 02 07 WITHOUT ROWIDU.. +| 3952: 17 1b 1b 01 81 01 74 61 62 6c 65 74 31 5f 64 61 ......tablet1_da +| 3968: 74 61 74 31 5f 64 61 74 61 02 43 52 45 41 54 45 tat1_data.CREATE +| 3984: 20 54 41 42 4c 45 20 27 74 31 5f 64 61 74 61 27 TABLE 't1_data' +| 4000: 28 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d (id INTEGER PRIM +| 4016: 41 52 59 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 ARY KEY, block B +| 4032: 4c 4f 42 29 3a 01 06 17 11 11 08 63 74 61 62 6c LOB):......ctabl +| 4048: 65 74 31 74 31 43 52 45 41 54 45 20 56 49 52 54 et1t1CREATE VIRT +| 4064: 55 41 4c 20 54 41 42 4c 45 20 74 31 20 55 53 49 UAL TABLE t1 USI +| 4080: 4e 47 20 66 74 73 35 28 63 6f 6e 74 65 6e 74 29 NG fts5(content) +| page 2 offset 4096 +| 0: 0d 00 00 00 03 0f bd 00 0f e8 0f ef 0f bd 00 00 ................ +| 4016: 00 00 00 00 00 00 00 00 00 00 00 00 00 24 84 80 .............$.. +| 4032: 80 80 80 01 03 00 4e 00 00 00 1e 06 30 61 62 61 ......N.....0aba +| 4048: 63 6b 01 02 02 04 02 66 74 02 02 02 04 04 6e 64 ck.....ft.....nd +| 4064: 6f 6e 03 02 02 04 0a 07 05 01 03 00 10 03 03 0f on.............. +| 4080: 0a 03 00 24 00 00 00 0e ee ee ee ee ee ee ee ee ...$............ +| page 3 offset 8192 +| 0: ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ................ +| 16: ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ................ +| 32: ee ee ee ee ee ee ee ee ee ee ee ee 00 10 10 10 ................ +| 48: 00 10 10 10 10 a0 00 00 00 10 ff a0 00 ff 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 05 04 09 0c 01 02 ................ +| page 4 offset 12288 +| 0: 0d 00 00 00 03 0f e0 00 0f f6 0f ec 0f e0 00 00 ................ +| 4064: 0a 03 03 00 1b 61 62 61 6e 64 6f 6e 08 02 03 00 .....abandon.... +| 4080: 17 61 62 61 66 74 08 01 03 00 17 61 62 61 63 6b .abaft.....aback +| page 5 offset 16384 +| 0: 0d 00 00 00 03 0f ee 00 0f fa 0f f4 0f ee 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 04 03 ................ +| 4080: 03 00 0e 01 04 02 03 00 0e 01 04 01 03 00 0e 01 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| page 7 offset 24576 +| 0: 0d 00 00 00 03 0f d6 00 0f f4 0f e1 0f d6 00 00 ................ +| 4048: 00 00 00 00 00 00 09 03 02 1b 72 65 62 75 69 6c ..........rebuil +| 4064: 64 11 02 02 2b 69 6e 74 65 67 72 69 74 79 2d 63 d...+integrity-c +| 4080: 68 65 63 6b 0a 01 02 1d 6f 70 74 69 6d 69 7a 65 heck....optimize +| end crash-cf347c523f793c.db +}]} {} + +do_catchsql_test 20.1 { + SELECT * FROM t1 WHERE t1 MATCH 'abandon'; +} {/.*fts5: corrupt.*/} + +#------------------------------------------------------------------------- +reset_db +do_test 21.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 28672 pagesize 4096 filename c22b.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 01 00 00 00 07 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 48: 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 ................ +| 80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 ................ +| 96: 00 2e 30 38 0d 00 00 00 07 0d d2 00 0f c4 0f 6d ..08...........m +| 112: 0f 02 0e ab 0e 4e 0d f6 0d d2 00 00 00 00 00 00 .....N.......... +| 3536: 00 00 22 07 06 17 11 11 01 31 74 61 62 6c 65 74 .........1tablet +| 3552: 32 74 32 07 43 52 45 41 54 45 20 54 41 42 4c 45 2t2.CREATE TABLE +| 3568: 20 74 32 28 78 29 56 06 06 17 1f 1f 01 7d 74 61 t2(x)V.......ta +| 3584: 62 6c 65 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 blet1_configt1_c +| 3600: 6f 6e 66 69 67 06 43 52 45 41 54 45 20 54 41 42 onfig.CREATE TAB +| 3616: 4c 45 20 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b LE 't1_config'(k +| 3632: 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 PRIMARY KEY, v) +| 3648: 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 5b 05 WITHOUT ROWID[. +| 3664: 07 17 21 21 01 81 01 74 61 62 6c 65 74 31 5f 64 ..!!...tablet1_d +| 3680: 6f 63 73 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 ocsizet1_docsize +| 3696: 05 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 .CREATE TABLE 't +| 3712: 31 5f 64 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 1_docsize'(id IN +| 3728: 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE +| 3744: 59 2c 20 73 7a 20 42 4c 4f 42 29 55 04 06 17 21 Y, sz BLOB)U...! +| 3760: 21 01 77 74 61 62 6c 65 74 31 5f 63 6f 6e 74 65 !.wtablet1_conte +| 3776: 6e 74 74 31 5f 63 6f 6e 74 65 6e 74 04 43 52 45 ntt1_content.CRE +| 3792: 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 6f ATE TABLE 't1_co +| 3808: 6e 74 65 6e 74 27 28 69 64 20 49 4e 54 45 47 45 ntent'(id INTEGE +| 3824: 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 63 R PRIMARY KEY, c +| 3840: 30 29 69 03 07 17 19 19 01 81 2d 74 61 62 6c 65 0)i.......-table +| 3856: 74 31 5f 69 64 78 74 31 5f 69 64 78 03 43 52 45 t1_idxt1_idx.CRE +| 3872: 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 69 64 ATE TABLE 't1_id +| 3888: 78 27 28 73 65 67 69 64 2c 20 74 65 72 6d 2c 20 x'(segid, term, +| 3904: 70 67 6e 6f 2c 20 50 52 49 4d 41 52 59 20 4b 45 pgno, PRIMARY KE +| 3920: 59 28 73 65 67 69 64 2c 20 74 65 72 6d 29 29 20 Y(segid, term)) +| 3936: 57 49 54 48 4f 55 54 20 52 4f 57 49 44 55 02 07 WITHOUT ROWIDU.. +| 3952: 17 1b 1b 01 81 01 74 61 62 6c 65 74 31 5f 64 61 ......tablet1_da +| 3968: 74 61 74 31 5f 64 61 74 61 02 43 52 45 41 54 45 tat1_data.CREATE +| 3984: 20 54 41 42 4c 45 20 27 74 31 5f 64 61 74 61 27 TABLE 't1_data' +| 4000: 28 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d (id INTEGER PRIM +| 4016: 41 52 59 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 ARY KEY, block B +| 4032: 4c 4f 42 29 3a 01 06 17 11 11 08 63 74 61 62 6c LOB):......ctabl +| 4048: 65 74 31 74 31 43 52 45 41 54 45 20 56 49 52 54 et1t1CREATE VIRT +| 4064: 55 41 4c 20 54 41 42 4c 45 20 74 31 20 55 53 49 UAL TABLE t1 USI +| 4080: 4e 47 20 66 74 73 35 28 63 6f 6e 74 65 6e 74 29 NG fts5(content) +| page 2 offset 4096 +| 0: 0d 0e 8e 00 06 0e 2f 00 0f e8 0e 2f 0f bd 0f 3b ....../..../...; +| 16: 0e a5 0e 49 00 00 00 00 00 00 00 00 00 00 00 00 ...I............ +| 3616: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 18 ................ +| 3632: 0a 03 00 36 00 00 00 00 01 04 04 00 04 01 01 01 ...6............ +| 3648: 02 01 01 03 01 01 04 01 01 3e 90 80 80 80 80 01 .........>...... +| 3664: 04 00 81 00 00 00 00 36 06 30 62 61 63 6b 75 05 .......6.0backu. +| 3680: 02 04 05 02 04 02 05 65 61 6d 65 72 05 02 02 05 .......eamer.... +| 3696: 02 02 02 05 6f 6f 6d 65 72 05 01 05 01 02 05 75 ....oomer......u +| 3712: 6d 6d 65 72 05 02 03 05 02 03 04 0d 0d 0b 0f 27 mmer...........' +| 3728: 00 17 30 00 00 00 00 01 03 03 00 03 01 01 01 02 ..0............. +| 3744: 01 01 03 01 01 7b 8c 80 80 80 80 01 04 00 81 7a ...............z +| 3760: 00 00 00 6d 06 30 61 62 61 63 6b 0d 02 07 04 04 ...m.0aback..... +| 3776: 6e 64 6f 6e 0d 02 05 02 05 63 74 69 76 65 09 02 ndon.....ctive.. +| 3792: 02 04 02 0b 02 04 6c 70 68 61 0d 04 02 0a 02 03 ......lpha...... +| 3808: 74 6f 6d 0b 02 02 02 02 09 05 02 69 63 0c 02 02 tom........ic... +| 3824: 01 06 62 61 63 6b 75 70 0d 02 04 02 05 6f 6f 6d ..backup.....oom +| 3840: 65 72 0a 02 02 03 02 08 01 07 63 68 61 6e 6e 65 er........channe +| 3856: 6c 0d 02 03 01 04 74 65 73 74 0d 02 06 04 0a 09 l.....test...... +| 3872: 0d 0a 0b 07 0b 0d 0c 0f ef 00 14 2a 00 00 00 00 ...........*.... +| 3888: 01 02 02 00 02 01 01 01 02 01 01 7b 88 80 80 80 ................ +| 3904: 80 01 04 00 81 7a 00 00 00 6d 06 30 61 62 61 63 .....z...m.0abac +| 3920: 6b 08 02 07 04 04 6e 64 6f 6e 08 02 05 02 05 63 k.....ndon.....c +| 3936: 74 69 76 65 04 02 02 04 02 0b 02 04 6c 70 68 61 tive........lpha +| 3952: 08 04 02 0a 02 03 74 6f 6d 06 02 02 02 02 09 05 ......tom....... +| 3968: 02 69 63 07 02 02 01 06 62 61 63 6b 75 70 08 02 .ic.....backup.. +| 3984: 04 02 05 6f 6f 6d 65 72 05 02 02 03 02 08 01 07 ...oomer........ +| 4000: 63 68 61 6e 6e 65 6c 08 02 03 01 04 74 65 73 74 channel.....test +| 4016: 08 02 06 04 0a 09 0d 0a 0b 07 0b 0d 0c 24 84 80 .............$.. +| 4032: 80 80 80 01 03 00 4e 00 00 00 1e 06 30 61 62 61 ......N.....0aba +| 4048: 63 6b 01 02 02 04 02 66 74 00 02 22 04 04 6e 64 ck.....ft.....nd +| 4064: 6f 6e 03 02 02 08 0a 07 05 01 03 00 10 0d 23 00 on............#. +| 4080: 00 00 11 24 00 00 00 00 01 01 01 00 01 01 01 01 ...$............ +| page 3 offset 8192 +| 0: 0a 00 00 00 04 0f e5 00 0f fa 0f f3 0f ec 0f e5 ................ +| 4064: 00 00 00 00 00 06 04 01 0c 01 04 02 06 04 01 0c ................ +| 4080: 01 03 02 06 04 01 0c 01 02 02 05 04 09 0c 01 02 ................ +| page 4 offset 12288 +| 0: 0d 0f 5a 00 0d 0e ce 00 0f f6 0f ec 0f e0 0f d5 ..Z............. +| 16: 0e e7 0f c1 0f b6 0f 70 0f 65 0e ce 0f 51 0f 46 .......p.e...Q.F +| 32: 0f 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 3776: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 17 0a ................ +| 3792: 03 00 35 62 65 61 6d 65 72 20 62 75 6d 6d 65 72 ..5beamer bummer +| 3808: 20 62 61 63 6b 75 29 17 05 03 00 35 62 65 61 6d backu)....5beam +| 3824: 65 72 20 62 75 6d 6d 65 72 20 62 61 63 6b 75 29 er bummer backu) +| 3840: 44 0d 04 00 81 0d 61 6c 70 68 61 20 63 68 61 6e D.....alpha chan +| 3856: 6e 65 6c 20 62 61 63 6b 75 70 20 61 62 61 6e 64 nel backup aband +| 3872: 6f 6e 20 74 65 73 74 20 61 62 61 63 6b 20 62 6f on test aback bo +| 3888: 6f 6d 65 72 20 61 74 6f 6d 20 61 6c 70 68 61 20 omer atom alpha +| 3904: 61 63 74 69 76 65 09 0c 03 00 19 61 74 6f 6d 69 active.....atomi +| 3920: 63 07 0b 03 00 15 61 74 6f 6d 0f ca 00 0b 19 62 c.....atom.....b +| 3936: 6f 6f 6d 65 72 09 09 03 00 19 61 63 74 69 76 65 oomer.....active +| 3952: 44 08 04 00 81 0d 61 6c 70 68 61 20 63 68 61 6e D.....alpha chan +| 3968: 6e 65 6c 20 62 61 63 6b 75 70 20 61 62 61 6e 64 nel backup aband +| 3984: 6f 6e 20 74 65 73 74 20 61 62 61 63 6b 20 62 6f on test aback bo +| 4000: 6f 6d 65 72 20 61 74 6f 6d 20 61 6c 70 68 61 20 omer atom alpha +| 4016: 61 63 74 69 76 65 09 07 03 00 19 61 74 6f 6d 69 active.....atomi +| 4032: 63 07 06 03 00 15 61 74 6f 6d 00 00 00 0b 19 62 c.....atom.....b +| 4048: 6f 6f 6d 65 72 09 04 03 00 19 61 63 74 69 76 65 oomer.....active +| 4064: 0a 03 03 00 1b 61 62 61 6e 64 6f 6e 08 02 03 00 .....abandon.... +| 4080: 17 61 62 61 66 74 08 01 03 00 17 61 62 61 63 6b .abaft.....aback +| page 5 offset 16384 +| 0: 0d 00 00 00 0d 0f b2 00 0f fa 0f f4 0f ee 0f e8 ................ +| 16: 0f e2 0f dc 0f d6 0f d0 0f ca 0f c4 0f be 0f b8 ................ +| 32: 0f b2 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 4016: 00 00 04 0d 03 00 0e 0a 04 0c 03 00 0e 01 04 0b ................ +| 4032: 03 00 0e 01 04 0a 03 00 0e 03 04 09 03 00 0e 01 ................ +| 4048: 04 08 03 00 0e 0a 04 07 03 00 0e 01 04 06 03 00 ................ +| 4064: 0e 01 04 05 03 00 0e 03 04 04 03 00 0e 01 04 03 ................ +| 4080: 03 00 0e 01 04 02 03 00 0e 01 04 01 03 00 0e 01 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| page 7 offset 24576 +| 0: 0d 00 00 00 03 0f d6 00 0f f4 0f e1 0f d6 00 00 ................ +| 4048: 00 00 00 00 00 00 09 03 02 1b 72 65 62 75 69 6c ..........rebuil +| 4064: 64 11 02 02 2b 69 6e 74 65 67 72 69 74 79 2d 63 d...+integrity-c +| 4080: 68 65 63 6b 0a 01 02 1d 6f 70 74 69 6d 69 7a 65 heck....optimize +| end c22b.db +}]} {} + +do_catchsql_test 21.1 { + DELETE FROM t1 WHERE t1 MATCH 'ab*ndon'; +} {/.*fts5: corrupt.*/} + +#------------------------------------------------------------------------- +# +reset_db +do_test 22.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 28672 pagesize 4096 filename c22b.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 02 00 00 00 07 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 02 00 00 00 00 ................ +| 48: 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 ................ +| 80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 02 ................ +| 96: 00 2e 30 38 0d 00 00 00 07 0d d2 00 0f c4 0f 6d ..08...........m +| 112: 0f 02 0e ab 0e 4e 0d f6 0d d2 00 00 00 00 00 00 .....N.......... +| 3536: 00 00 22 07 06 17 11 11 01 31 74 61 62 6c 65 74 .........1tablet +| 3552: 32 74 32 07 43 52 45 41 54 45 20 54 41 42 4c 45 2t2.CREATE TABLE +| 3568: 20 74 32 28 78 29 56 06 06 17 1f 1f 01 7d 74 61 t2(x)V.......ta +| 3584: 62 6c 65 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 blet1_configt1_c +| 3600: 6f 6e 66 69 67 06 43 52 45 41 54 45 20 54 41 42 onfig.CREATE TAB +| 3616: 4c 45 20 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b LE 't1_config'(k +| 3632: 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 PRIMARY KEY, v) +| 3648: 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 5b 05 WITHOUT ROWID[. +| 3664: 07 17 21 21 01 81 01 74 41 62 6c 65 74 31 5f 64 ..!!...tAblet1_d +| 3680: 6f 63 73 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 ocsizet1_docsize +| 3696: 05 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 .CREATE TABLE 't +| 3712: 31 5f 64 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 1_docsize'(id IN +| 3728: 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE +| 3744: 59 2c 20 73 7a 20 42 4c 4f 42 29 55 04 06 17 21 Y, sz BLOB)U...! +| 3760: 21 01 77 74 61 62 6c 65 74 31 5f 63 6f 6e 74 65 !.wtablet1_conte +| 3776: 6e 74 74 31 5f 63 6f 6e 74 65 6e 74 04 43 52 45 ntt1_content.CRE +| 3792: 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 6f ATE TABLE 't1_co +| 3808: 6e 74 65 6e 74 27 28 69 64 20 49 4e 54 45 47 45 ntent'(id INTEGE +| 3824: 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 63 R PRIMARY KEY, c +| 3840: 30 29 69 03 07 17 19 19 01 81 2d 74 61 62 6c 65 0)i.......-table +| 3856: 74 31 5f 69 64 78 74 31 5f 69 64 78 03 43 52 45 t1_idxt1_idx.CRE +| 3872: 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 69 64 ATE TABLE 't1_id +| 3888: 78 27 28 73 65 67 69 64 2c 20 74 65 72 6d 2c 20 x'(segid, term, +| 3904: 70 67 6e 6f 2c 20 50 52 49 4d 41 52 59 20 4b 45 pgno, PRIMARY KE +| 3920: 59 28 73 65 67 69 64 2c 20 74 65 72 6d 29 29 20 Y(segid, term)) +| 3936: 57 49 54 48 4f 55 54 20 52 4f 57 49 44 55 02 07 WITHOUT ROWIDU.. +| 3952: 17 1b 1b 01 81 01 74 61 62 6c 65 74 31 5f 64 61 ......tablet1_da +| 3968: 74 61 74 31 5f 64 61 74 61 02 43 52 45 41 54 45 tat1_data.CREATE +| 3984: 20 54 41 42 4c 45 20 27 74 31 5f 64 61 74 61 27 TABLE 't1_data' +| 4000: 28 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d (id INTEGER PRIM +| 4016: 41 52 59 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 ARY KEY, block B +| 4032: 4c 4f 42 29 3a 01 06 17 11 11 08 63 74 61 62 6c LOB):......ctabl +| 4048: 65 74 31 74 31 43 52 45 41 54 45 20 56 49 52 54 et1t1CREATE VIRT +| 4064: 75 41 4c 20 54 41 42 4c 45 20 74 31 20 55 53 49 uAL TABLE t1 USI +| 4080: 4e 47 20 66 74 73 35 28 63 6f 6e 74 65 6e 74 29 NG fts5(content) +| page 2 offset 4096 +| 0: 0d 0f 15 00 05 08 4e 00 0f e8 08 4e 0f bd 0f 29 ......N....N...) +| 16: 08 65 00 00 00 00 00 00 00 00 00 00 00 00 00 00 .e.............. +| 2112: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 15 0a ................ +| 2128: 03 00 30 00 00 00 00 01 03 02 01 03 01 01 01 02 ..0............. +| 2144: 01 01 03 01 01 8d 28 8c 80 80 80 80 01 04 00 9a ......(......... +| 2160: 54 00 00 06 77 19 30 30 30 30 30 30 33 30 66 65 T...w.00000030fe +| 2176: 65 30 30 30 66 66 61 30 66 66 34 30 66 65 65 05 e000ffa0ff40fee. +| 2192: 02 19 02 0b 31 30 66 66 61 30 30 30 66 66 61 05 ....10ffa000ffa. +| 2208: 06 13 18 09 02 25 33 34 33 35 32 34 35 34 31 35 .....%3435245415 +| 2224: 34 34 35 32 30 35 34 34 31 34 32 34 63 34 35 32 445205441424c452 +| 2240: 30 32 37 37 34 33 31 35 66 36 39 05 02 07 02 37 02774315f69....7 +| 2256: 34 30 33 30 33 30 30 30 65 30 31 30 34 30 32 30 40303000e0104020 +| 2272: 33 30 30 30 65 30 31 30 34 30 31 30 33 30 30 30 3000e01040103000 +| 2288: 65 30 31 30 61 30 30 30 30 30 30 30 31 30 66 66 e010a000000010ff +| 2304: 34 30 30 30 66 66 34 05 02 1b 02 27 35 30 34 30 4000ff4....'5040 +| 2320: 39 30 63 30 31 30 32 30 64 30 30 30 30 30 30 30 90c01020d0000000 +| 2336: 33 30 66 65 30 30 30 30 66 66 36 30 66 65 63 30 30fe0000ff60fec0 +| 2352: 66 65 30 05 02 2c 27 02 66 30 05 02 16 02 1d 36 fe0..,'.f0.....6 +| 2368: 31 37 31 31 31 31 30 38 36 33 37 34 36 31 36 32 1711110863746162 +| 2384: 36 63 36 35 37 34 33 31 37 34 33 31 05 02 0d 02 6c6574317431.... +| 2400: 29 39 30 33 30 32 31 62 37 32 36 35 36 32 37 35 )903021b72656275 +| 2416: 36 34 36 31 37 34 36 31 37 34 33 31 35 66 36 34 6461746174315f64 +| 2432: 36 31 37 34 36 31 30 32 34 33 05 02 1f 02 43 61 6174610243....Ca +| 2448: 30 33 30 33 30 30 31 62 36 31 36 32 36 31 36 65 0303001b6162616e +| 2464: 36 34 36 66 36 65 30 38 30 32 30 33 30 30 31 37 646f6e0802030017 +| 2480: 36 31 36 32 36 31 36 36 37 34 30 38 30 31 30 33 6162616674080103 +| 2496: 30 30 31 37 36 31 36 32 36 31 36 33 36 62 30 64 0017616261636b0d +| 2512: 30 30 05 02 18 34 1d 33 36 62 30 31 30 32 30 32 00...4.36b010202 +| 2528: 30 34 30 32 36 36 37 34 30 32 30 32 30 32 30 34 0402667402020204 +| 2544: 30 34 36 65 05 02 2e 02 33 62 30 33 31 62 30 31 046e....3b031b01 +| 2560: 37 36 36 35 37 32 37 33 36 39 36 66 36 65 30 34 76657273696f6e04 +| 2576: 30 64 30 30 30 30 30 30 30 33 30 66 64 36 30 30 0d000000030fd600 +| 2592: 30 66 66 34 30 66 65 31 30 66 64 36 05 02 1d 01 0ff40fe10fd6.... +| 2608: 04 31 66 62 64 05 04 10 18 01 01 32 05 02 14 02 .1fbd......2.... +| 2624: 43 34 38 34 38 30 38 30 38 30 38 30 30 31 30 33 C484808080800103 +| 2640: 30 30 34 65 30 30 30 30 30 30 31 65 30 36 33 30 004e0000001e0630 +| 2656: 36 31 36 32 36 31 36 33 36 62 30 31 30 32 30 32 616261636b010202 +| 2672: 30 34 30 32 36 36 37 34 30 32 30 32 30 32 30 34 0402667402020204 +| 2688: 30 34 36 65 05 04 11 18 02 01 66 05 02 2a 03 02 046e......f..*.. +| 2704: 65 72 05 02 31 01 08 33 35 32 38 36 33 36 66 05 er..1..3528636f. +| 2720: 02 24 02 03 66 65 30 05 04 17 18 01 2b 34 31 35 .$..fe0.....+415 +| 2736: 32 35 39 32 30 34 62 34 35 35 39 32 63 32 30 36 259204b45592c206 +| 2752: 32 36 63 36 66 36 33 36 62 32 30 34 32 34 63 34 26c6f636b20424c4 +| 2768: 66 34 32 32 39 33 61 30 05 02 0c 2c 1f 31 30 36 f42293a0...,.106 +| 2784: 31 37 31 31 31 31 30 38 36 33 37 34 36 31 36 32 1711110863746162 +| 2800: 36 63 36 35 37 34 33 31 37 34 33 31 05 02 22 02 6c6574317431.... +| 2816: 40 33 35 32 34 35 34 31 35 34 34 35 32 30 35 36 @352454154452056 +| 2832: 34 39 35 32 35 34 37 35 34 31 34 63 32 30 35 34 49525475414c2054 +| 2848: 34 31 34 32 34 63 34 35 32 30 37 34 33 31 32 30 41424c4520743120 +| 2864: 35 35 35 33 34 39 34 65 34 37 32 30 36 36 37 34 5553494e47206674 +| 2880: 37 05 02 23 42 09 33 33 35 32 38 36 33 36 66 05 7..#B.33528636f. +| 2896: 02 0e 02 03 66 65 65 05 02 1a 01 34 35 32 34 35 ....fee....45245 +| 2912: 34 31 35 34 34 35 32 30 35 34 34 31 34 32 34 63 415445205441424c +| 2928: 34 35 32 30 32 37 37 34 33 31 35 66 36 34 36 31 45202774315f6461 +| 2944: 37 34 36 31 32 37 32 38 36 39 36 34 32 30 34 39 7461272869642049 +| 2960: 05 02 20 16 37 c3 b0 35 32 30 32 37 37 34 33 31 .. .7..520277431 +| 2976: 35 66 36 34 36 31 37 34 36 31 32 37 32 38 36 39 5f64617461272869 +| 2992: 36 34 32 30 34 39 34 65 35 34 34 35 34 37 34 35 6420494e54454745 +| 3008: 35 32 32 30 35 30 35 32 34 39 34 64 05 02 0b 03 52205052494d.... +| 3024: 48 35 39 32 30 34 62 34 35 35 39 32 63 32 30 36 H59204b45592c206 +| 3040: 33 33 30 32 39 36 39 30 33 30 30 30 31 35 37 35 3302969030001575 +| 3056: 33 31 31 39 32 36 64 37 34 36 31 36 32 36 63 36 311926d7461626c6 +| 3072: 35 37 34 33 31 35 66 36 39 36 34 37 38 37 34 33 574315f696478743 +| 3088: 31 35 66 36 39 36 34 37 38 05 02 06 02 38 34 32 15f696478....842 +| 3104: 30 35 32 34 66 35 37 34 39 34 34 35 35 30 32 30 0524f57494455020 +| 3120: 37 31 37 31 62 31 62 30 31 38 31 30 31 37 34 36 7171b1b018101746 +| 3136: 31 36 32 36 63 36 35 37 34 33 31 35 66 31 37 34 1626c6574315f174 +| 3152: 36 31 30 32 34 33 05 02 0a 02 03 66 66 34 05 02 610243.....ff4.. +| 3168: 1c 01 4a 36 34 36 66 36 65 30 33 30 32 30 32 30 ..J646f6e0302020 +| 3184: 34 30 61 30 37 30 35 30 31 30 33 30 30 31 30 30 40a0705010300100 +| 3200: 33 30 33 30 66 30 61 30 33 30 30 32 34 30 30 30 3030f0a030024000 +| 3216: 30 30 30 30 30 30 31 30 31 30 31 30 30 30 31 30 0000001010100010 +| 3232: 31 30 31 30 30 39 61 30 30 30 30 30 30 05 02 2f 101009a000000../ +| 3248: 42 09 31 30 61 30 30 30 30 30 30 05 02 28 08 43 B.10a000000..(.C +| 3264: 74 30 32 30 32 30 34 30 61 30 37 30 35 30 31 30 t0202040a0705010 +| 3280: 33 30 30 31 30 30 33 30 33 30 66 30 61 30 33 30 3001003030f0a030 +| 3296: 30 32 34 30 30 30 30 30 30 30 30 30 31 30 31 30 0240000000001010 +| 3312: 31 30 30 30 31 30 31 30 31 30 31 30 61 30 30 30 100010101010a000 +| 3328: 30 30 30 05 02 12 02 49 37 36 65 36 66 32 63 32 000....I76e6f2c2 +| 3344: 30 35 30 35 32 34 39 34 64 34 31 35 32 35 39 32 05052494d4152592 +| 3360: 30 34 62 34 35 35 39 32 38 37 33 36 35 36 37 36 04b4559287365676 +| 3376: 39 36 34 32 63 32 30 37 34 36 35 37 32 36 64 32 9642c207465726d2 +| 3392: 39 32 39 32 30 35 37 34 39 35 34 34 38 34 66 35 92920574954484f5 +| 3408: 35 05 02 09 02 4c 39 37 61 36 35 37 34 33 31 35 5....L97a6574315 +| 3424: 66 36 34 36 66 36 33 37 33 36 39 37 61 36 35 30 f646f6373697a650 +| 3440: 35 34 33 35 32 34 35 34 31 35 34 34 35 32 30 35 5435245415445205 +| 3456: 34 38 36 39 36 34 32 30 34 39 34 65 35 34 34 35 48696420494e5445 +| 3472: 34 37 34 35 35 32 32 30 35 30 35 32 34 39 34 34 4745522050524944 +| 3488: 64 31 05 02 05 02 27 65 37 34 36 35 36 65 37 34 d1....'e74656e74 +| 3504: 32 39 30 64 30 30 30 30 30 30 30 33 30 66 62 64 290d000000030fbd +| 3520: 30 30 30 62 65 38 30 66 65 66 30 66 62 64 05 02 000be80fef0fbd.. +| 3536: 0f 1e 0b 66 65 38 30 66 65 66 30 66 62 64 05 02 ...fe80fef0fbd.. +| 3552: 25 02 03 66 64 36 05 02 1e 01 4a 37 36 32 39 32 %..fd6....J76292 +| 3568: 30 35 37 34 39 35 34 34 38 34 66 35 35 35 34 32 0574954484f55542 +| 3584: 30 35 32 34 66 35 37 34 39 34 34 35 62 30 35 30 0524f5749445b050 +| 3600: 37 31 37 32 31 32 31 30 31 38 31 30 31 37 34 36 7172121018101746 +| 3616: 31 36 32 36 63 36 35 37 34 33 31 35 66 36 34 36 1626c6574315f646 +| 3632: 66 36 33 37 33 05 02 04 02 21 38 32 37 32 38 37 f6373....!827287 +| 3648: 33 36 35 36 37 36 39 36 34 32 63 32 30 37 34 36 3656769642c20746 +| 3664: 35 37 32 36 64 32 63 32 30 37 30 05 02 08 01 01 5726d2c2070..... +| 3680: 61 05 02 2b 01 06 62 61 63 6b 75 70 05 02 32 02 a..+..backup..2. +| 3696: 05 65 61 6d 65 72 05 02 02 02 05 6f 6f 6d 65 72 .eamer.....oomer +| 3712: 05 01 02 40 75 6d 6d 32 34 63 34 35 32 30 32 37 ...@umm24c452027 +| 3728: 37 34 33 31 35 66 36 33 36 66 36 65 36 36 36 39 74315f636f6e6669 +| 3744: 36 37 32 37 32 38 36 62 32 30 35 30 35 32 34 39 6727286b20505249 +| 3760: 34 64 34 31 35 32 35 39 32 30 34 62 34 35 35 39 4d415259204b4559 +| 3776: 32 63 32 30 05 02 03 01 15 65 35 34 34 35 34 37 2c20.....e544547 +| 3792: 34 35 35 32 32 30 35 30 35 32 34 39 34 64 05 02 4552205052494d.. +| 3808: 21 01 02 66 61 05 02 15 04 1d 12 2a 3c 2c 07 22 !..fa......*<,.. +| 3824: 2e 48 22 38 0a 06 49 06 07 0d 09 30 24 45 0e 08 .H.8..I....0$E.. +| 3840: 39 3c 4d 3d 08 4f 0e 48 4e 51 2c 10 08 4f 26 06 9.$.. +| 3072: 00 00 00 00 00 00 00 00 00 00 18 24 05 00 25 0f ...........$..%. +| 3088: 19 54 48 52 45 41 44 53 41 46 45 3d 30 58 42 49 .THREADSAFE=0XBI +| 3104: 4e 41 52 59 18 23 05 00 25 0f 19 54 48 52 45 41 NARY.#..%..THREA +| 3120: 44 53 41 46 45 3d 30 58 4e 4f 43 41 53 45 17 22 DSAFE=0XNOCASE.. +| 3136: 05 00 25 0f 17 54 48 52 45 41 44 53 41 46 45 3d ..%..THREADSAFE= +| 3152: 30 58 52 54 52 49 4d 1f 21 05 00 33 0f 19 4f 4d 0XRTRIM.!..3..OM +| 3168: 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 4f IT LOAD EXTENSIO +| 3184: 4e 58 42 49 4e 41 52 59 1f 20 05 00 33 0f 19 4f NXBINARY. ..3..O +| 3200: 4d 49 54 20 4c 4f 41 44 b2 04 55 85 44 54 e5 34 MIT LOAD..U.DT.4 +| 3216: 94 f4 e5 84 e4 f4 34 15 34 51 e1 f0 50 03 30 f1 ......4.4Q..P.0. +| 3232: 74 f4 d4 95 42 04 c4 f4 14 42 04 55 85 44 54 e5 t...B....B.U.DT. +| 3248: 34 94 f4 e5 85 25 45 24 94 d1 f1 e0 50 03 30 f1 4....%E$....P.0. +| 3264: 94 d4 15 82 04 d4 54 d4 f5 25 93 d3 53 03 03 03 ......T..%..S... +| 3280: 03 03 03 05 84 24 94 e4 15 25 91 f1 d0 50 03 30 .....$...%...P.0 +| 3296: f1 94 d4 15 82 04 d4 54 d4 f5 25 93 d3 53 03 03 .......T..%..S.. +| 3312: 03 03 03 03 05 84 e4 f4 34 15 34 51 e1 c0 50 03 ........4.4Q..P. +| 3328: 30 f1 74 d4 15 82 04 d4 54 d4 f5 25 93 d3 53 03 0.t.....T..%..S. +| 3344: 03 03 03 03 03 05 85 25 45 24 94 d1 81 b0 50 02 .......%E$....P. +| 3360: 50 f1 94 54 e4 14 24 c4 52 05 25 45 24 54 55 84 P..T..$.R.%E$TU. +| 3376: 24 94 e4 15 25 91 81 a0 50 02 50 f1 94 54 e4 14 $...%...P.P..T.. +| 3392: 24 c4 52 05 25 45 24 54 55 84 e4 f4 34 15 34 51 $.R.%E$TU...4.4Q +| 3408: 71 90 50 02 50 f1 74 54 e4 14 24 c4 52 05 25 45 q.P.P.tT..$.R.%E +| 3424: 24 54 55 85 25 45 24 94 d1 a1 80 50 02 90 f1 94 $TU.%E$....P.... +| 3440: 54 e4 14 24 c4 52 04 d4 54 d5 35 95 33 55 84 24 T..$.R..T.5.3U.$ +| 3456: 94 e4 15 25 91 a1 70 50 02 90 f1 94 54 e4 14 24 ...%..pP....T..$ +| 3472: c4 52 04 d4 54 d5 35 95 33 55 84 e4 f4 34 15 34 .R..T.5.3U...4.4 +| 3488: 51 91 60 50 02 90 f1 74 54 e4 14 24 c4 52 04 d4 Q.`P...tT..$.R.. +| 3504: 54 d5 35 95 33 55 85 25 45 24 94 d1 81 50 50 02 T.5.3U.%E$...PP. +| 3520: 50 f1 94 54 e4 14 24 c4 52 04 a5 34 f4 e3 15 84 P..T..$.R..4.... +| 3536: 24 94 e4 15 25 91 81 40 50 02 50 f1 94 54 e4 14 $...%..@P.P..T.. +| 3552: 24 c4 52 04 a5 34 f4 e3 15 84 e4 f4 34 15 34 51 $.R..4......4.4Q +| 3568: 71 30 50 02 50 f1 74 54 e4 14 24 c4 52 04 a5 34 q0P.P.tT..$.R..4 +| 3584: f4 e3 15 85 25 45 24 94 d1 a1 20 50 02 90 f1 94 ....%E$... P.... +| 3600: 54 e4 14 24 c4 52 04 74 54 f5 04 f4 c5 95 84 24 T..$.R.tT......$ +| 3616: 94 e4 15 25 91 a1 10 50 02 90 f1 94 54 e4 14 24 ...%...P....T..$ +| 3632: c4 52 04 74 54 f5 04 f4 c5 95 84 e4 f4 34 15 34 .R.tT........4.4 +| 3648: 51 91 00 50 02 90 f1 74 54 e4 14 24 c4 52 04 74 Q..P...tT..$.R.t +| 3664: 54 f5 04 f4 c5 95 85 25 45 24 94 d1 70 f0 50 02 T......%E$..p.P. +| 3680: 30 f1 94 54 e4 14 24 c5 20 46 54 53 35 58 42 49 0..T..$. FTS5XBI +| 3696: 4e 41 52 59 17 0e 05 00 23 0f 19 45 4e 41 42 4c NARY....#..ENABL +| 3712: 45 20 46 54 53 35 58 4e 4f 43 41 53 45 16 0d 05 E FTS5XNOCASE... +| 3728: 00 23 0f 17 45 4e 41 42 4c 45 20 46 54 53 35 58 .#..ENABLE FTS5X +| 3744: 52 54 52 49 4d 17 0c 05 00 23 0f 19 45 4e 41 42 RTRIM....#..ENAB +| 3760: 4c 45 20 46 54 53 34 58 42 49 4e 41 52 59 97 0b LE FTS4XBINARY.. +| 3776: 05 00 23 0f 19 45 4e 41 42 4c 45 20 46 54 53 34 ..#..ENABLE FTS4 +| 3792: 58 4e 4f 43 41 53 45 16 0a 05 00 23 0f 17 45 4e XNOCASE....#..EN +| 3808: 41 42 4c 45 20 46 54 53 34 58 52 54 52 49 4d 1e ABLE FTS4XRTRIM. +| 3824: 09 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3840: 54 41 54 20 56 54 41 42 58 42 49 4e 41 52 59 1e TAT VTABXBINARY. +| 3856: 08 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3872: 54 41 54 20 56 54 41 42 58 4e 4f 43 41 53 45 1d TAT VTABXNOCASE. +| 3888: 07 05 00 31 0f 17 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3904: 54 41 54 20 56 54 41 42 58 52 54 52 49 4d 11 06 TAT VTABXRTRIM.. +| 3920: 05 00 17 0f 19 44 45 42 55 47 58 42 49 4e 41 52 .....DEBUGXBINAR +| 3936: 59 11 05 05 00 17 0f 19 44 45 42 55 47 58 4e 4f Y.......DEBUGXNO +| 3952: 43 41 53 45 10 02 02 50 08 5f 17 44 45 42 55 47 CASE...P._.DEBUG +| 3968: 58 52 54 52 49 4d 27 03 05 00 43 0f 19 43 4f 4d XRTRIM'...C..COM +| 3984: 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e 30 20 PILER=gcc-5.4.0 +| 4000: 32 30 31 36 30 36 30 39 58 42 49 4e 41 52 59 27 20160609XBINARY' +| 4016: 02 05 00 43 0f 19 43 4f 4d 50 49 4c 45 52 3d 67 ...C..COMPILER=g +| 4032: 63 63 2d 35 2e 34 2e 30 20 32 30 31 36 30 36 30 cc-5.4.0 2016060 +| 4048: 39 58 4e 4f 43 41 53 45 26 01 05 00 43 0f 17 43 9XNOCASE&...C..C +| 4064: 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e OMPILER=gcc-5.4. +| 4080: 30 20 32 30 31 36 30 36 30 39 58 52 54 52 49 4d 0 20160609XRTRIM +| page 6 offset 20480 +| 0: 0d 00 00 00 24 0e e0 00 0f f8 0f f0 0f e8 0f e0 ....$........... +| 16: 0f d8 0f d0 0f c8 0f c0 0f b8 0f b0 0f a8 0f a0 ................ +| 32: 0f 98 0f 90 0f 88 0f 80 0f 78 0f 70 0f 68 0f 60 .........x.p.h.` +| 48: 0f 58 0f 50 0f 48 0f 40 0f 38 0f 30 0f 28 0f 20 .X.P.H.@.8.0.(. +| 64: 0f 18 0f 10 0f 08 0f 00 0e f8 0e f0 0e e8 0e e0 ................ +| 3808: 06 24 03 00 12 02 01 01 06 23 03 00 12 02 01 01 .$.......#...... +| 3824: 06 22 03 00 12 02 01 01 06 21 03 00 12 03 01 01 .........!...... +| 3840: 06 20 03 00 12 03 01 01 06 1f 03 00 12 03 01 01 . .............. +| 3856: 06 1e 03 00 12 03 01 01 06 1d 03 00 12 03 01 01 ................ +| 3872: 06 1c 03 00 12 03 01 01 06 1b 03 00 12 02 01 01 ................ +| 3888: 06 1a 03 00 12 02 01 01 06 19 03 00 12 02 01 01 ................ +| 3904: 06 18 03 00 12 02 01 01 06 17 03 00 12 02 01 01 ................ +| 3920: 06 16 03 00 12 02 01 01 06 15 03 00 12 02 01 01 ................ +| 3936: 06 14 03 00 12 02 01 01 06 13 03 00 12 02 01 01 ................ +| 3952: 06 12 03 00 12 02 01 01 06 11 03 00 12 02 01 01 ................ +| 3968: 06 10 03 00 12 02 01 01 06 0f 03 00 12 02 01 01 ................ +| 3984: 06 0e 03 00 12 02 01 01 06 0d 03 00 12 02 01 01 ................ +| 4000: 06 0c 03 00 12 02 01 01 06 0b 03 00 12 02 01 01 ................ +| 4016: 06 0a 03 00 12 02 01 01 06 09 03 00 12 03 01 01 ................ +| 4032: 06 08 03 00 12 03 01 01 06 07 03 00 12 03 01 01 ................ +| 4048: 06 06 03 00 12 01 01 01 06 05 03 00 12 01 01 01 ................ +| 4064: 06 04 03 00 12 01 01 01 06 03 03 00 12 06 01 01 ................ +| 4080: 06 02 03 00 12 06 01 01 06 01 03 00 12 06 01 01 ................ +| page 7 offset 24576 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| page 8 offset 28672 +| 0: 0d 00 00 00 03 0f d6 00 0f f4 0f e9 0f d6 00 00 ................ +| 4048: 00 00 00 00 00 00 11 03 02 2b 69 6e 74 65 67 72 .........+integr +| 4064: 69 74 79 2d 63 68 65 63 6b 09 02 02 1b 72 65 62 ity-check....reb +| 4080: 75 69 6c 64 0a 01 02 1d 6f 70 74 69 6d 69 7a 65 uild....optimize +| end crash-b87dfef02880fe.db +}]} {} + +do_catchsql_test 24.1 { + UPDATE t1 SET b=quote(zeroblob(200)) WHERE a MATCH 'thread*'; +} {/.*fts5: corrupt.*/} + +do_catchsql_test 24.2 { + INSERT INTO t1(t1) VALUES('integrity-check'); +} {1 {database disk image is malformed}} + +#-------------------------------------------------------------------------- +reset_db +do_test 25.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 28672 pagesize 4096 filename crash-e3b1b19e4d4bcc.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 06 00 00 00 00 .....@ ........ +| 32: 00 00 00 00 00 00 00 06 00 00 00 04 00 00 00 00 ................ +| 48: 03 20 54 35 24 54 15 44 52 04 94 e4 44 55 82 07 . T5$T.DR...DU.. +| 64: 43 27 a2 04 f4 e2 07 43 22 87 a2 95 30 30 71 71 C'.....C....00qq +| 80: 11 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 .SQLite format 3 +| 96: 00 10 00 01 0d 00 00 00 07 0d d2 00 0f c4 0f 6d ...............m +| 112: 0f 02 0e ab 0e 4e 0d f6 0d d2 03 30 01 00 00 10 .....N.....0.... +| 128: 10 04 02 02 00 00 00 00 00 00 00 00 30 00 00 00 ............0... +| 144: 00 00 00 00 00 00 00 00 20 00 00 00 40 00 00 00 ........ ...@... +| 3536: 00 00 22 07 06 17 11 11 01 31 74 61 62 6c 65 74 .........1tablet +| 3552: 32 74 32 07 43 52 45 41 54 45 20 54 41 42 4c 45 2t2.CREATE TABLE +| 3568: 20 74 32 28 78 29 56 06 06 17 1f 1f 01 7d 74 61 t2(x)V.......ta +| 3584: 62 6c 65 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 blet1_configt1_c +| 3600: 6f 6e 66 69 67 06 43 52 45 41 54 45 20 54 41 42 onfig.CREATE TAB +| 3616: 4c 45 20 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b LE 't1_config'(k +| 3632: 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 PRIMARY KEY, v) +| 3648: 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 5b 05 WITHOUT ROWID[. +| 3664: 07 17 21 21 01 81 01 74 61 62 6c 65 74 31 5f 64 ..!!...tablet1_d +| 3680: 6f 63 73 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 ocsizet1_docsize +| 3696: 05 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 .CREATE TABLE 't +| 3712: 31 5f 64 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 1_docsize'(id IN +| 3728: 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE +| 3744: 59 2c 20 73 7a 20 42 4c 4f 42 29 55 04 06 17 21 Y, sz BLOB)U...! +| 3760: 21 01 77 74 61 62 6c 65 74 31 5f 63 6f 6e 74 65 !.wtablet1_conte +| 3776: 6e 74 74 31 5f 63 6f 6e 74 65 6e 74 04 43 52 45 ntt1_content.CRE +| 3792: 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 6f ATE TABLE 't1_co +| 3808: 6e 74 65 6e 74 27 28 69 64 20 49 4e 54 45 47 45 ntent'(id INTEGE +| 3824: 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 63 R PRIMARY KEY, c +| 3840: 30 29 69 03 07 17 19 19 01 81 2d 74 61 62 6c 65 0)i.......-table +| 3856: 74 31 5f 69 64 78 74 31 5f 69 64 78 03 43 52 45 t1_idxt1_idx.CRE +| 3872: 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 69 64 ATE TABLE 't1_id +| 3888: 78 27 28 73 65 67 69 64 2c 20 74 65 72 6d 2c 20 x'(segid, term, +| 3904: 70 67 6e 6f 2c 20 50 52 49 4d 41 52 59 20 4b 45 pgno, PRIMARY KE +| 3920: 59 28 73 65 67 69 64 2c 20 74 65 72 6d 29 29 20 Y(segid, term)) +| 3936: 57 49 54 48 4f 55 54 20 52 4f 57 49 44 55 02 07 WITHOUT ROWIDU.. +| 3952: 17 1b 1b 01 81 01 74 61 62 6c 65 74 31 5f 64 61 ......tablet1_da +| 3968: 74 61 74 31 5f 64 61 74 61 02 43 52 45 41 54 45 tat1_data.CREATE +| 3984: 20 54 41 42 4c 45 20 27 74 31 5f 64 61 74 61 27 TABLE 't1_data' +| 4000: 28 69 64 20 49 4e 54 45 47 45 52 20 50 52 d9 44 (id INTEGER PR.D +| 4016: 41 52 59 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 ARY KEY, block B +| 4032: 4c 4f 42 29 3a 01 06 17 11 11 08 63 74 61 62 6c LOB):......ctabl +| 4048: 65 74 31 74 31 43 52 45 41 54 45 20 56 49 52 54 et1t1CREATE VIRT +| 4064: 55 41 4c 20 54 41 42 4c 45 20 74 31 20 55 53 49 UAL TABLE t1 USI +| 4080: 4e 47 20 66 74 73 35 28 63 6f 6e 74 65 6e 74 29 NG fts5(content) +| page 2 offset 4096 +| 0: 0d 00 00 00 03 0f bd 00 0f e8 0f ef 0f bd 00 00 ................ +| 4016: 00 00 00 00 00 00 00 00 00 00 00 00 00 24 84 80 .............$.. +| 4032: 80 80 80 01 03 00 4e 00 00 00 1e 06 30 61 62 61 ......N.....0aba +| 4048: 63 6b 01 02 02 04 02 66 74 02 02 02 04 04 6e 64 ck.....ft.....nd +| 4064: 6f 6e 03 02 02 04 0a 07 05 01 03 00 10 03 03 0f on.............. +| 4080: 0a 03 00 24 00 00 00 00 01 01 01 00 01 01 01 01 ...$............ +| page 3 offset 8192 +| 0: 0a 00 00 00 01 0f fa 00 0f fa 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 05 04 09 0c 01 02 ................ +| page 4 offset 12288 +| 0: 0d 00 00 00 03 0f e0 00 0f f6 0f e0 fe 00 00 00 ................ +| 4064: 0a 03 03 00 1b 61 62 61 6e 64 6f 6e 08 02 00 03 .....abandon.... +| 4080: 17 61 62 61 66 74 08 01 03 00 17 61 62 61 63 6b .abaft.....aback +| page 5 offset 16384 +| 0: 0d 00 00 00 03 0f ee 00 0f fa 0f f4 0f ee 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 04 03 ................ +| 4080: 03 00 0e 01 04 02 03 00 0e 01 04 01 03 00 0e 01 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| page 7 offset 24576 +| 0: 0d 00 00 00 03 0f d6 00 0f f4 01 fe 0f d6 00 00 ................ +| 4048: 00 00 00 00 00 00 09 03 02 1b 72 65 62 75 69 6c ..........rebuil +| 4064: 64 11 02 02 2b 69 6e 74 65 67 72 69 74 79 2d 63 d...+integrity-c +| 4080: 68 65 63 6b 0a 01 02 1d 6f 70 74 69 6d 69 7a 65 heck....optimize +| end crash-e3b1b19e4d4bcc.db +}]} {} + +do_catchsql_test 25.1 { + INSERT INTO t1(t1) VALUES('rebuild'); +} {/.*fts5: corrupt.*/} + +do_execsql_test 25.2 { + PRAGMA page_size=512; +} + +#-------------------------------------------------------------------------- +reset_db +do_test 26.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 32768 pagesize 4096 filename c30b.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 01 00 00 00 08 .....@ ........ +| 32: 00 00 00 02 00 00 00 01 00 00 00 01 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 ................ +| 96: 00 2e 30 38 0d 0f c7 00 07 0d 92 00 0f 8d 0f 36 ..08...........6 +| 112: 0e cb 0e 6b 0e 0e 0d b6 0d 92 0d 92 00 00 00 00 ...k............ +| 3472: 00 00 22 08 06 17 11 11 01 31 74 61 62 6c 65 74 .........1tablet +| 3488: 32 74 32 08 43 52 45 41 54 45 20 54 41 42 4c 45 2t2.CREATE TABLE +| 3504: 20 74 32 28 78 29 56 07 06 17 1f 1f 01 7d 74 61 t2(x)V.......ta +| 3520: 62 6c 65 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 blet1_configt1_c +| 3536: 6f 6e 66 69 67 07 43 52 45 41 54 45 20 54 41 42 onfig.CREATE TAB +| 3552: 4c 45 20 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b LE 't1_config'(k +| 3568: 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 PRIMARY KEY, v) +| 3584: 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 5b 06 WITHOUT ROWID[. +| 3600: 07 17 21 21 01 81 01 74 61 62 6c 65 74 31 5f 64 ..!!...tablet1_d +| 3616: 6f 63 73 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 ocsizet1_docsize +| 3632: 06 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 .CREATE TABLE 't +| 3648: 31 5f 64 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 1_docsize'(id IN +| 3664: 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE +| 3680: 59 2c 20 73 7a 20 42 4c 4f 42 29 5e 05 07 17 21 Y, sz BLOB)^...! +| 3696: 21 01 81 07 74 61 62 6c 65 74 31 5f 63 6f 6e 74 !...tablet1_cont +| 3712: 65 6e 74 74 31 5f 63 6f 6e 74 65 6e 74 05 43 52 entt1_content.CR +| 3728: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 EATE TABLE 't1_c +| 3744: 6f 6e 74 65 6e 74 27 28 69 64 20 49 4e 54 45 47 ontent'(id INTEG +| 3760: 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 ER PRIMARY KEY, +| 3776: 63 30 2c 20 63 31 2c 20 63 32 29 69 04 07 17 19 c0, c1, c2)i.... +| 3792: 19 01 81 2d 74 61 62 6c 65 74 31 5f 69 64 78 74 ...-tablet1_idxt +| 3808: 31 5f 69 64 78 04 43 52 45 41 54 45 20 54 41 42 1_idx.CREATE TAB +| 3824: 4c 45 20 27 74 31 5f 69 64 78 27 28 73 65 67 69 LE 't1_idx'(segi +| 3840: 64 2c 20 74 65 72 6d 2c 20 70 67 6e 6f 2c 20 50 d, term, pgno, P +| 3856: 52 49 4d 41 52 59 20 4b 45 59 28 73 65 67 69 64 RIMARY KEY(segid +| 3872: 2c 20 74 65 72 6d 29 29 20 57 49 54 48 4f 55 54 , term)) WITHOUT +| 3888: 20 52 4f 57 49 44 55 03 07 17 1b 1b 01 81 01 74 ROWIDU........t +| 3904: 61 62 6c 65 74 31 5f 64 61 74 61 74 31 5f 64 61 ablet1_datat1_da +| 3920: 74 61 03 43 52 45 41 54 45 20 54 41 42 4c 45 20 ta.CREATE TABLE +| 3936: 27 74 31 5f 64 61 74 61 27 28 69 64 20 49 4e 54 't1_data'(id INT +| 3952: 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 EGER PRIMARY KEY +| 3968: 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 29 38 02 06 , block BLOB)8.. +| 3984: 17 11 11 08 5f 74 61 62 6c 65 74 31 74 31 43 52 ...._tablet1t1CR +| 4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 35 LE t1 USING fts5 +| 4032: 28 61 2c 62 2c 63 29 00 00 00 39 00 00 00 00 00 (a,b,c)...9..... +| page 3 offset 8192 +| 0: 0d 0c 0f 00 05 09 fe 00 0f e6 09 fe 0c 94 0c 23 ...............# +| 16: 0a 15 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 2544: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 15 0a ................ +| 2560: 03 00 30 00 00 00 00 01 03 03 00 03 01 01 01 02 ..0............. +| 2576: 01 01 03 01 01 83 72 8c 80 80 80 80 01 04 00 87 ......r......... +| 2592: 68 00 00 01 e4 02 30 30 03 03 06 02 83 0f 30 30 h.....00......00 +| 2608: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 2624: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 2640: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 2656: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 2672: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 2688: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 2704: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 2720: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 2736: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 2752: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 2768: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 2784: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 2800: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 2816: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 2832: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 2848: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 2864: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 2880: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 2896: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 2912: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 2928: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 2944: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 2960: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 2976: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 2992: 30 30 30 30 30 30 30 30 30 30 30 30 30 03 06 01 0000000000000... +| 3008: 01 03 01 08 32 30 31 36 30 36 30 39 03 03 07 01 ....20160609.... +| 3024: 01 34 03 03 05 01 01 35 03 03 04 01 06 62 69 6e .4.....5.....bin +| 3040: 61 72 79 03 07 01 02 02 01 08 63 6f 6d 70 69 6c ary.......compil +| 3056: 65 72 03 03 02 01 03 67 63 63 03 03 03 01 01 78 er.....gcc.....x +| 3072: 03 07 01 01 02 04 06 83 17 0d 06 06 0d 0d 08 0f ................ +| 3088: ef 00 14 2a 00 00 00 00 01 02 02 00 02 01 01 01 ...*............ +| 3104: 02 01 01 6a 88 80 80 80 80 01 04 00 81 58 00 00 ...j.........X.. +| 3120: 00 5f 07 30 62 69 6e 61 72 79 0c 01 03 01 01 06 ._.0binary...... +| 3136: 65 6e 61 62 6c 65 0a 01 01 01 01 01 01 01 01 01 enable.......... +| 3152: 01 01 01 04 66 74 73 34 0a 01 01 01 01 01 04 01 ....fts4........ +| 3168: 35 0d 01 01 01 01 01 01 06 6e 6f 63 61 73 65 0b 5........nocase. +| 3184: 01 03 01 01 05 72 74 72 69 6d 0a 01 03 01 01 01 .....rtrim...... +| 3200: 78 0a 01 01 01 01 01 01 01 01 01 01 01 04 0c 14 x............... +| 3216: 0c 09 0c 0b 86 4a 84 80 80 80 80 01 04 00 8d 18 .....J.......... +| 3232: 00 00 03 2b 02 30 30 01 02 06 01 02 06 01 02 06 ...+.00......... +| 3248: 1f 02 03 01 02 03 01 02 03 01 08 32 30 31 36 30 ...........20160 +| 3264: 36 30 39 01 02 07 01 02 07 01 02 07 01 01 34 01 609...........4. +| 3280: 02 05 01 02 05 01 02 05 01 01 35 01 02 04 01 02 ..........5..... +| 3296: 04 01 02 04 02 07 30 30 30 30 30 30 30 1c 02 04 ......0000000... +| 3312: 01 02 04 01 02 04 01 06 62 69 6e 61 72 79 03 06 ........binary.. +| 3328: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3344: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3360: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 ................ +| 3376: 03 06 01 02 02 03 06 01 02 02 01 08 63 6f 6d 70 ............comp +| 3392: 69 6c 65 72 01 02 02 01 02 02 01 02 02 01 06 64 iler...........d +| 3408: 62 73 74 61 74 07 02 03 01 02 03 01 02 03 02 04 bstat........... +| 3424: 65 62 75 67 04 02 02 01 02 02 01 02 02 01 06 65 ebug...........e +| 3440: 6e 61 62 6c 65 07 02 02 01 02 02 01 02 02 01 02 nable........... +| 3456: 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 ................ +| 3472: 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 ................ +| 3488: 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 ................ +| 3504: 02 01 02 02 02 08 78 74 65 6e 73 69 6f 6e 1f 02 ......xtension.. +| 3520: 04 01 02 04 01 02 04 01 04 66 74 73 34 0a 02 03 .........fts4... +| 3536: 01 02 03 01 02 03 04 01 35 0d 02 03 01 02 03 01 ........5....... +| 3552: 02 03 01 03 67 63 63 01 02 03 01 02 03 01 02 03 ....gcc......... +| 3568: 02 06 65 6f 70 6f 6c 79 10 02 03 01 02 03 01 02 ..eopoly........ +| 3584: 03 01 05 6a 73 6f 6e 31 13 02 03 01 02 03 01 02 ...json1........ +| 3600: 03 01 04 6c 6f 61 64 1f 02 03 01 02 03 01 02 03 ...load......... +| 3616: 01 03 6d 61 78 1c 02 02 01 02 02 01 02 02 02 05 ..max........... +| 3632: 65 6d 6f 72 79 1c 02 03 01 02 03 01 02 03 04 04 emory........... +| 3648: 73 79 73 35 16 02 03 01 02 03 01 02 03 01 06 6e sys5...........n +| 3664: 6f 63 61 73 65 02 06 01 02 02 03 06 01 02 02 03 ocase........... +| 3680: 06 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 ................ +| 3696: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3712: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3728: 02 01 04 6f 6d 69 74 1f 02 02 01 02 02 01 02 02 ...omit......... +| 3744: 01 05 72 74 72 65 65 19 02 03 01 02 03 01 02 03 ..rtree......... +| 3760: 04 02 69 6d 01 06 01 02 02 03 06 01 02 02 03 06 ..im............ +| 3776: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3792: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3808: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 ................ +| 3824: 01 0a 74 68 72 65 61 64 73 61 66 65 03 57 34 56 ..threadsafe.W4V +| 3840: 94 64 91 46 85 84 04 76 74 61 62 07 02 04 01 02 .d.F...vtab..... +| 3856: 04 01 02 04 01 01 78 01 06 01 01 02 01 06 01 01 ......x......... +| 3872: 02 01 06 01 01 02 01 06 01 01 02 01 06 01 10 02 ................ +| 3888: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 3904: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 3920: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 ................ +| 3936: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 ................ +| 3952: 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 ................ +| 3968: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 3984: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 4000: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 ................ +| 4016: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 ................ +| 4032: 02 01 06 01 01 02 01 06 01 01 02 04 15 13 0c 0c ................ +| 4048: 12 44 13 11 0f 47 13 0f 0c 0e 11 10 0f 0e 10 0f .D...G.......... +| 4064: 44 0f 10 40 15 0f 07 01 03 00 14 1e 4e 1f 1e 00 D..@........N... +| 4080: 00 00 11 24 00 00 00 00 01 01 01 00 01 01 01 01 ...$............ +| page 4 offset 12288 +| 0: 0a 00 00 00 03 0f ec 00 0f fa 0f f3 0f ec 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 00 06 04 01 0c ................ +| 4080: 01 03 02 06 04 01 0c 01 02 02 05 04 09 0c 01 02 ................ +| page 5 offset 16384 +| 0: 0d 0e 5b 00 1e 0a 4d 00 0f d8 0f af 0a 4d 0f 74 ..[...M......M.t +| 16: 0f 61 0f 4e 0f 2f 0f 0f 0e ef 0e 40 0e 24 0e 08 .a.N./.....@.$.. +| 32: 0d ef 0d d5 0d bb 0d a0 0d 84 0d 68 0d 4f 0d 35 ...........h.O.5 +| 48: 0d 1b 0c fb 0c da 0c b9 0c 99 0c 78 0c 57 0c 3e ...........x.W.> +| 64: 0c 24 0c 0a 0c 0a 0c 0a 0c 0a 0c 0a 0c 0a 0c 0a .$.............. +| 2624: 00 00 00 00 00 00 00 00 00 00 00 00 00 83 3a 03 ..............:. +| 2640: 06 00 43 86 33 19 43 4f 4d 50 49 4c 45 52 3d 67 ..C.3.COMPILER=g +| 2656: 63 63 2d 35 2e 34 2e 30 20 32 30 31 36 30 36 30 cc-5.4.0 2016060 +| 2672: 39 58 27 30 30 30 30 30 30 30 30 30 30 30 30 30 9X'0000000000000 +| 2688: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 2704: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 2720: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 2736: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 2752: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 2768: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 2784: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 2800: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 2816: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 2832: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 2848: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 2864: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 2880: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 2896: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 2912: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 2928: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 2944: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 2960: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 2976: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 2992: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 3008: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 3024: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 3040: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 3056: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 3072: 30 30 30 27 42 49 4e 41 52 59 18 24 05 00 25 0f 000'BINARY.$..%. +| 3088: 19 54 48 52 45 41 44 53 41 46 45 3d 30 58 42 49 .THREADSAFE=0XBI +| 3104: 4e 41 52 59 18 23 05 00 25 0f 19 54 48 52 45 41 NARY.#..%..THREA +| 3120: 44 53 41 46 45 3d 30 58 4e 4f 43 41 53 45 17 22 DSAFE=0XNOCASE.. +| 3136: 05 00 25 0f 17 54 48 52 45 41 44 53 41 46 45 3d ..%..THREADSAFE= +| 3152: 30 58 52 54 52 49 4d 1f 21 05 00 33 0f 19 4f 4d 0XRTRIM.!..3..OM +| 3168: 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 4f IT LOAD EXTENSIO +| 3184: 4e 58 42 49 4e 41 52 59 1f 20 05 00 33 0f 19 4f NXBINARY. ..3..O +| 3200: 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 MIT LOAD EXTENSI +| 3216: 4f 4e 58 4e 4f 43 41 53 45 1e 1f 05 00 33 0f 17 ONXNOCASE....3.. +| 3232: 4f 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 OMIT LOAD EXTENS +| 3248: 49 4f 4e 58 52 54 52 49 4d 1f 1e 05 00 33 0f 19 IONXRTRIM....3.. +| 3264: 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 30 MAX MEMORY=50000 +| 3280: 30 30 30 58 42 49 4e 41 52 59 1f 1d 05 00 33 0f 000XBINARY....3. +| 3296: 19 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 .MAX MEMORY=5000 +| 3312: 30 30 30 30 58 4e 4f 43 41 53 45 1e 1c 05 00 33 0000XNOCASE....3 +| 3328: 0f 17 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 ..MAX MEMORY=500 +| 3344: 30 30 30 30 30 58 52 54 52 49 4d 18 1b 05 00 25 00000XRTRIM....% +| 3360: 0f 19 45 4e 41 42 4c 45 20 52 54 52 45 45 58 42 ..ENABLE RTREEXB +| 3376: 49 4e 41 52 59 18 1a 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3392: 4c 45 20 52 54 52 45 45 58 4e 4f 43 41 53 45 17 LE RTREEXNOCASE. +| 3408: 19 05 00 25 0f 17 45 4e 41 42 4c 45 20 52 54 52 ...%..ENABLE RTR +| 3424: 45 45 58 52 54 52 49 4d 1a 18 05 00 29 0f 19 45 EEXRTRIM....)..E +| 3440: 4e 41 42 4c 45 20 4d 45 4d 53 59 53 35 58 42 49 NABLE MEMSYS5XBI +| 3456: 4e 41 52 59 1a 17 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3472: 42 60 2d 45 4d 53 59 53 35 58 4e 4f 43 41 53 45 B`-EMSYS5XNOCASE +| 3488: 19 16 05 00 29 0f 17 45 4e 41 42 4c 45 20 4d 45 ....)..ENABLE ME +| 3504: 4d 53 59 53 35 58 52 54 52 49 4d 18 15 05 00 25 MSYS5XRTRIM....% +| 3520: 0f 19 45 4e 41 42 4c 45 20 4a 53 4f 4e 31 58 42 ..ENABLE JSON1XB +| 3536: 49 4e 41 52 59 18 14 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3552: 4c 45 20 4a 53 4f 4e 31 58 4e 4f 43 41 53 45 17 LE JSON1XNOCASE. +| 3568: 13 05 00 25 0f 17 45 4e 41 42 4c 45 20 4a 53 4f ...%..ENABLE JSO +| 3584: 4e 31 58 52 54 52 49 4d 1a 12 05 00 29 0f 19 45 N1XRTRIM....)..E +| 3600: 4e 41 42 4c 45 20 47 45 4f 50 4f 4c 59 58 42 49 NABLE GEOPOLYXBI +| 3616: 4e 41 52 59 1a 11 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3632: 45 20 47 45 4f 50 4f 4c 59 58 4e 4f 43 41 53 45 E GEOPOLYXNOCASE +| 3648: 19 10 05 00 29 0f 17 45 4e 41 42 4c 45 20 47 45 ....)..ENABLE GE +| 3664: 4f 50 4f 4c 59 58 52 54 52 49 4d 0f 86 00 94 23 OPOLYXRTRIM....# +| 3680: 0f 19 45 4e 41 42 4c 45 20 46 54 53 35 58 42 49 ..ENABLE FTS5XBI +| 3696: 4e 41 52 59 00 00 00 7b 23 0f 19 45 4e 41 42 4c NARY....#..ENABL +| 3712: 45 20 46 54 53 35 58 4e 4f 43 41 53 45 00 00 00 E FTS5XNOCASE... +| 3728: 62 23 0f 17 45 4e 41 42 4c 45 20 46 54 53 35 58 b#..ENABLE FTS5X +| 3744: 52 54 52 49 4d 00 00 00 4a 23 0f 19 45 4e 41 42 RTRIM...J#..ENAB +| 3760: 4c 45 20 46 54 53 34 58 42 49 4e 41 52 59 00 00 LE FTS4XBINARY.. +| 3776: 00 31 23 0f 19 45 4e 41 42 4c 45 20 46 54 53 34 .1#..ENABLE FTS4 +| 3792: 58 4e 4f 43 41 53 45 00 00 00 18 23 0f 17 45 4e XNOCASE....#..EN +| 3808: 41 42 4c 45 20 46 54 53 34 58 52 54 52 49 4d 1e ABLE FTS4XRTRIM. +| 3824: 09 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3840: 54 41 54 20 56 54 41 42 58 42 49 4e 41 52 59 1e TAT VTABXBINARY. +| 3856: 08 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3872: 54 41 54 20 56 54 24 15 48 4e 4f 43 41 53 45 1d TAT VT$.HNOCASE. +| 3888: 07 05 00 31 0f 17 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3904: 54 41 54 20 56 54 41 42 58 52 54 52 49 4d 11 06 TAT VTABXRTRIM.. +| 3920: 05 00 17 0f 19 44 45 42 55 47 58 42 49 4e 41 52 .....DEBUGXBINAR +| 3936: 59 11 05 05 00 17 0f 19 44 45 42 55 47 58 4e 4f Y.......DEBUGXNO +| 3952: 43 41 53 45 10 04 05 00 17 0f 17 44 45 42 55 47 CASE.......DEBUG +| 3968: 58 52 54 52 49 4d 00 00 00 29 43 0f 19 43 4f 4d XRTRIM...)C..COM +| 3984: 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e 30 20 PILER=gcc-5.4.0 +| 4000: 32 30 31 36 30 36 30 39 58 42 49 4e 41 52 59 27 20160609XBINARY' +| 4016: 02 05 00 43 0f 19 43 4f 4d 50 49 4c 45 52 3d 67 ...C..COMPILER=g +| 4032: 63 63 2d 35 2e 34 2e 30 20 32 30 31 36 30 36 30 cc-5.4.0 2016060 +| 4048: 39 58 4e 4f 43 41 53 45 26 01 05 00 43 0f 17 43 9XNOCASE&...C..C +| 4064: 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e OMPILER=gcc-5.4. +| 4080: 30 20 32 30 31 36 30 36 30 39 58 52 54 52 49 4d 0 20160609XRTRIM +| page 6 offset 20480 +| 0: 0d 0f 88 00 1e 0e e0 00 0f f8 0f f0 0f b0 0f e0 ................ +| 16: 0f d8 0f d0 0f c8 0f c0 0f b8 0f 80 0f 78 0f 70 .............x.p +| 32: 0f 68 0f 60 0f 58 0f 50 0f 48 0f 40 0f 38 0f 30 .h.`.X.P.H.@.8.0 +| 48: 0f 28 0f 20 0f 18 0f 10 0f 08 0f 00 0e f8 0e f0 .(. ............ +| 64: 0e e8 0e e0 0e e0 0e e0 0e e0 0e e0 0e e0 0e e0 ................ +| 3808: 06 24 03 00 12 02 01 01 06 23 03 00 12 02 01 01 .$.......#...... +| 3824: 06 22 03 00 12 02 01 01 06 21 03 00 12 03 01 01 .........!...... +| 3840: 06 20 03 00 12 03 01 01 06 1f 03 00 12 03 01 01 . .............. +| 3856: 06 1e 03 00 12 03 01 01 06 1d 03 00 12 03 01 01 ................ +| 3872: 06 1c 03 00 12 03 01 01 06 1b 03 00 12 02 01 01 ................ +| 3888: 06 1a 03 00 12 02 01 01 06 19 03 00 12 02 01 01 ................ +| 3904: 06 18 03 00 12 02 01 01 06 17 03 00 12 02 01 01 ................ +| 3920: 06 16 03 00 12 02 01 01 06 15 03 00 12 02 01 01 ................ +| 3936: 06 14 03 00 12 02 01 01 06 13 03 00 12 02 01 01 ................ +| 3952: 06 12 03 00 12 02 01 01 06 11 03 00 12 02 01 01 ................ +| 3968: 06 10 03 00 12 02 01 01 0f e8 00 28 12 02 01 01 ...........(.... +| 3984: 00 00 00 28 12 02 01 01 00 00 00 20 12 02 01 01 ...(....... .... +| 4000: 00 00 00 18 12 02 01 01 00 00 00 10 12 02 01 01 ................ +| 4016: 06 03 03 00 12 06 02 01 06 09 03 00 12 03 01 01 ................ +| 4032: 06 08 03 00 12 03 01 01 06 07 03 00 12 03 01 01 ................ +| 4048: 06 06 03 00 12 01 01 01 06 05 03 00 12 01 01 01 ................ +| 4064: 06 04 03 00 12 01 01 01 00 00 00 08 12 06 01 01 ................ +| 4080: 06 02 03 00 12 06 01 01 06 01 03 00 12 06 01 01 ................ +| page 7 offset 24576 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| page 8 offset 28672 +| 0: 0d 00 00 00 03 0f d6 00 0f f4 0f e9 0f d6 00 00 ................ +| 4048: 00 00 00 00 00 00 11 03 02 2b 69 6e 74 65 67 72 .........+integr +| 4064: 69 74 79 2d 63 68 65 63 6b 09 02 02 1b 72 65 62 ity-check....reb +| 4080: 75 69 6c 64 0a 01 02 1d 6f 70 74 69 6d 69 7a 65 uild....optimize +| end c30b.db +}]} {} + +do_catchsql_test 26.1 { + BEGIN; + INSERT INTO t1(t1) VALUES('rebuild'); + INSERT INTO t1(t1) VALUES('integrity-check'); + COMMIT; +} {0 {}} + +#-------------------------------------------------------------------------- +reset_db +do_test 27.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 32768 pagesize 4096 filename timeout-2ca5b0658c98.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 08 .....@ ........ +| 32: 00 00 00 02 00 00 00 01 00 00 00 09 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 96: 00 00 00 00 0d 0f c7 00 07 0d 92 00 0f 8d 0f 36 ...............6 +| 112: 0e cb 0e 6b 0e 0e 0d b6 0d 92 0d 92 00 00 00 00 ...k............ +| 3472: 00 00 22 08 06 17 11 11 01 31 74 61 62 6c 65 74 .........1tablet +| 3488: 32 74 32 08 43 52 45 41 54 45 20 54 41 42 4c 45 2t2.CREATE TABLE +| 3504: 20 74 32 28 78 29 56 07 06 17 1f 1f 01 7d 74 61 t2(x)V.......ta +| 3520: 62 6c 65 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 blet1_configt1_c +| 3536: 6f 6e 66 69 67 07 43 52 45 41 54 45 20 54 41 42 onfig.CREATE TAB +| 3552: 4c 45 20 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b LE 't1_config'(k +| 3568: 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 PRIMARY KEY, v) +| 3584: 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 5b 06 WITHOUT ROWID[. +| 3600: 07 17 21 21 01 81 01 74 61 62 6c 65 74 31 5f 64 ..!!...tablet1_d +| 3616: 6f 63 73 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 ocsizet1_docsize +| 3632: 06 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 .CREATE TABLE 't +| 3648: 31 5f 64 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 1_docsize'(id IN +| 3664: 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE +| 3680: 59 2c 20 73 7a 20 42 4c 4f 42 29 5e 05 07 17 21 Y, sz BLOB)^...! +| 3696: 21 01 81 07 74 61 62 6c 65 74 31 5f 63 6f 6e 74 !...tablet1_cont +| 3712: 65 6e 74 74 31 5f 63 6f 6e 74 65 6e 74 05 43 52 entt1_content.CR +| 3728: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 EATE TABLE 't1_c +| 3744: 6f 6e 74 65 6e 74 27 28 69 64 20 49 4e 54 45 47 ontent'(id INTEG +| 3760: 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 ER PRIMARY KEY, +| 3776: 63 30 2c 20 63 31 2c 20 63 32 29 69 04 07 17 19 c0, c1, c2)i.... +| 3792: 19 01 81 2d 74 61 62 6c 65 74 31 5f 69 64 78 74 ...-tablet1_idxt +| 3808: 31 5f 69 64 78 04 43 52 45 41 54 45 20 54 41 42 1_idx.CREATE TAB +| 3824: 4c 45 20 27 74 31 5f 69 64 78 27 28 73 65 67 69 LE 't1_idx'(segi +| 3840: 64 2c 20 74 65 72 6d 2c 20 70 67 6e 6f 2c 20 50 d, term, pgno, P +| 3856: 52 49 4d 41 52 59 20 4b 45 59 28 73 65 67 69 64 RIMARY KEY(segid +| 3872: 2c 20 74 65 72 6d 29 29 20 57 49 54 48 4f 55 54 , term)) WITHOUT +| 3888: 20 52 4f 57 49 44 55 03 07 17 1b 1b 01 81 01 74 ROWIDU........t +| 3904: 61 62 6c 65 74 31 5f 64 61 74 61 74 31 5f 64 61 ablet1_datat1_da +| 3920: 74 61 03 43 52 45 41 54 45 20 54 41 42 4c 45 20 ta.CREATE TABLE +| 3936: 27 74 31 5f 64 61 74 61 27 28 69 64 20 49 4e 54 't1_data'(id INT +| 3952: 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 EGER PRIMARY KEY +| 3968: 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 29 38 02 06 , block BLOB)8.. +| 3984: 17 11 11 08 5f 74 61 62 6c 65 74 31 74 31 43 52 ...._tablet1t1CR +| 4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 35 LE t1 USING fts5 +| 4032: 28 61 2c 62 2c 63 29 00 00 00 39 00 00 00 00 00 (a,b,c)...9..... +| page 3 offset 8192 +| 0: 0d 00 00 00 03 0c 94 00 0f e6 0f ef 0c 94 00 00 ................ +| 3216: 00 00 00 00 86 4a 84 80 80 80 80 01 04 00 8d 18 .....J.......... +| 3232: 00 00 03 2b 02 30 30 01 02 06 01 02 06 01 02 06 ...+.00......... +| 3248: 1f 02 03 01 02 03 01 02 03 01 08 32 30 31 36 30 ...........20160 +| 3264: 36 30 39 01 02 07 01 02 07 01 02 07 01 01 34 01 609...........4. +| 3280: 02 05 01 02 05 01 02 05 01 01 35 01 02 04 01 02 ..........5..... +| 3296: 04 01 02 04 02 07 30 30 30 30 30 30 30 1c 02 04 ......0000000... +| 3312: 01 02 04 01 02 04 01 06 62 69 6e 61 72 79 03 06 ........binary.. +| 3328: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3344: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3360: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 ................ +| 3376: 03 06 01 02 02 03 06 01 02 02 01 08 63 6f 6d 70 ............comp +| 3392: 69 6c 65 72 01 20 01 02 02 02 01 02 02 01 06 64 iler. .........d +| 3408: 62 73 7c cc cc cc cc cc cc cc cc cc cc cc cc cc bs|............. +| 3424: cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc ................ +| 3440: cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc ................ +| 3456: cc cc cc cc cc cc c4 61 74 07 02 03 01 02 03 01 .......at....... +| 3472: 02 03 02 04 65 62 75 67 04 02 02 01 02 02 01 02 ....ebug........ +| 3488: 02 01 06 65 6e 61 62 6c 65 07 02 02 01 02 02 01 ...enable....... +| 3504: 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 ................ +| 3520: 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 ................ +| 3536: 01 02 02 01 02 02 01 02 02 01 02 02 01 02 05 23 ...............# +| 3552: d6 76 36 32 d3 52 e3 42 e3 02 03 23 03 13 63 03 .v62.R.B...#..c. +| 3568: 63 03 95 84 e4 f4 34 15 34 52 60 10 50 04 30 f1 c.....4.4R`.P.0. +| 3584: 74 34 f4 d5 04 94 c4 55 23 d6 76 36 32 d3 52 e3 t4.....U#.v62.R. +| 3600: 42 e3 02 03 23 03 13 63 03 63 03 95 85 25 45 24 B...#..c.c...%E$ +| 3616: 94 d0 d0 00 00 02 40 ee 00 00 ff 80 ff 00 fe 80 ......@......... +| 3632: fe 00 fd 80 fd 00 fc 80 fc 00 fb 80 fb 00 fa 80 ................ +| 3648: fa 00 f9 80 f9 00 f8 80 f8 00 f7 80 f7 00 f6 80 ................ +| 3664: f6 00 f5 80 f5 00 f4 80 f4 00 f8 0f 30 0f 28 0f ............0.(. +| 3680: 20 0f 18 0f 10 0f 08 0f 00 0e f8 0e f0 0e e8 0e ............... +| 3696: e0 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3712: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3728: 02 01 04 6f 6d 69 74 1f 02 02 01 02 02 01 02 02 ...omit......... +| 3744: 01 05 72 74 72 65 65 19 02 03 01 02 03 01 02 03 ..rtree......... +| 3760: 04 02 69 6d 01 06 01 02 02 03 06 01 02 02 03 06 ..im............ +| 3776: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3792: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3808: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 ................ +| 3824: 01 0a 74 68 72 65 61 64 73 61 66 65 22 02 02 01 ..threadsafe.... +| 3840: 02 02 01 02 02 01 04 76 74 61 62 07 02 04 01 02 .......vtab..... +| 3856: 04 01 02 04 01 01 78 01 06 01 01 02 01 06 01 01 ......x......... +| 3872: 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 ................ +| 3888: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 3904: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 3920: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 ................ +| 3936: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 ................ +| 3952: 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 ................ +| 3968: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 3984: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 4000: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 ................ +| 4016: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 ................ +| 4032: 02 01 06 01 01 02 01 06 01 01 02 04 15 13 0c 0c ................ +| 4048: 12 44 13 11 0f 47 13 0f 0c 0e 11 10 0f 0e 10 0f .D...G.......... +| 4064: 44 0f 10 40 15 0f 07 01 03 00 14 24 5a 24 24 0f D..@.......$Z$$. +| 4080: 0a 03 00 24 00 00 00 00 01 01 01 00 01 01 01 01 ...$............ +| page 4 offset 12288 +| 0: 0a 00 00 00 01 0f fa 00 0f fa 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 05 04 09 0c 01 02 ................ +| page 5 offset 16384 +| 0: 0d 00 00 00 24 0c 0a 00 0f d8 0f af 0f 86 0f 74 ....$..........t +| 16: 0f 61 0f 4e 0f 2f 0f 0f 0e ef 0e d7 0e be 0e a5 .a.N./.......... +| 32: 0e 8d 0e 74 0e 5b 0e 40 0e 24 0e 08 0d ef 0d d5 ...t.[.@.$...... +| 48: 0d bb 0d a0 0d 84 0d 68 0d 4f 0d 35 0d 1b 0c fb .......h.O.5.... +| 64: 0c da 0c b9 0c 99 0c 78 0c 57 0c 3e 0c 24 0c 0a .......x.W.>.$.. +| 3072: 00 00 00 00 00 00 00 00 00 00 18 24 05 00 25 0f ...........$..%. +| 3088: 19 54 48 52 45 41 44 53 41 46 45 3d 30 58 42 49 .THREADSAFE=0XBI +| 3104: 4e 41 52 59 18 23 05 00 25 0f 19 54 48 52 45 41 NARY.#..%..THREA +| 3120: 44 53 41 46 45 3d 30 58 4e 4f 43 41 53 45 17 22 DSAFE=0XNOCASE.. +| 3136: 05 00 25 0f 17 54 48 52 45 41 44 53 41 46 45 3d ..%..THREADSAFE= +| 3152: 30 58 52 54 52 49 4d 1f 21 05 00 33 0f 19 4f 4d 0XRTRIM.!..3..OM +| 3168: 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 4f IT LOAD EXTENSIO +| 3184: 4e 58 42 49 4e 41 52 59 1f 20 05 00 33 0f 19 4f NXBINARY. ..3..O +| 3200: 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 MIT LOAD EXTENSI +| 3216: 4f 4e 58 4e 4f 43 41 53 45 1e 1f 05 00 33 0f 17 ONXNOCASE....3.. +| 3232: 4f 4d 49 54 20 4c 4f 41 44 20 45 5f e8 54 45 4e OMIT LOAD E_.TEN +| 3248: 53 49 4f 4e 58 52 54 52 49 4d 1f 1e 05 00 33 0f SIONXRTRIM....3. +| 3264: 19 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 31 81 .MAX MEMORY=501. +| 3280: 40 50 02 50 f1 94 54 e4 14 24 c4 52 04 a5 35 f4 @P.P..T..$.R..5. +| 3296: e3 15 84 e4 f4 34 15 34 51 71 30 50 02 50 f1 74 .....4.4Qq0P.P.t +| 3312: 54 e4 14 24 c4 52 04 a5 34 f4 e3 15 85 25 45 24 T..$.R..4....%E$ +| 3328: 94 d1 a1 20 50 02 90 f1 94 54 e4 14 24 c4 52 04 ... P....T..$.R. +| 3344: 74 54 f5 04 f4 c5 95 84 24 94 e4 15 25 91 a1 10 tT......$...%... +| 3360: 50 02 90 f1 94 54 e4 14 24 c4 52 04 74 54 f5 04 P....T..$.R.tT.. +| 3376: f4 c5 95 84 e4 f4 34 15 34 51 91 00 50 02 90 f1 ......4.4Q..P... +| 3392: 74 54 e4 14 24 c4 52 04 74 54 f5 04 f4 c5 95 85 tT..$.R.tT...... +| 3408: 25 45 24 94 d1 70 f0 50 02 30 f1 94 54 e4 14 24 %E$..p.P.0..T..$ +| 3424: c4 52 04 65 45 33 55 84 24 94 e4 15 25 91 70 e0 .R.eE3U.$...%.p. +| 3440: 50 02 30 f1 94 54 e4 14 24 c4 52 04 65 45 33 55 P.0..T..$.R.eE3U +| 3456: 84 e4 f4 34 15 34 51 60 d0 50 02 30 f1 74 54 e4 ...4.4Q`.P.0.tT. +| 3472: 14 24 c4 52 04 65 45 33 55 85 25 45 24 94 d1 70 .$.R.eE3U.%E$..p +| 3488: c0 50 02 30 f1 94 54 e4 14 24 c4 52 04 65 45 33 .P.0..T..$.R.eE3 +| 3504: 45 84 24 94 e4 15 25 91 70 b0 50 02 30 f1 94 54 E.$...%.p.P.0..T +| 3520: e4 14 24 c4 52 04 65 45 33 45 84 e4 f4 34 15 34 ..$.R.eE3E...4.4 +| 3536: 51 60 a0 50 02 30 f1 74 54 e4 14 24 c4 52 04 65 Q`.P.0.tT..$.R.e +| 3552: 45 33 45 85 25 45 24 94 d1 e0 90 50 03 10 f1 94 E3E.%E$....P.... +| 3568: 54 e4 42 4c 45 20 44 42 53 54 41 54 20 56 54 41 T.BLE DBSTAT VTA +| 3584: 42 58 42 49 4e 41 52 59 1e 08 05 00 31 0f 19 45 BXBINARY....1..E +| 3600: e4 14 24 c4 52 04 44 25 35 44 15 42 05 65 42 41 ..$.R.D%5D.B.eBA +| 3616: 54 84 e4 f4 34 15 34 51 d0 70 50 03 10 f1 74 54 T...4.4Q.pP...tT +| 3632: e4 14 24 c4 52 04 44 25 35 44 15 42 05 65 44 14 ..$.R.D%5D.B.eD. +| 3648: 25 85 25 45 24 94 d1 10 60 50 01 70 f1 94 44 54 %.%E$...`P.p..DT +| 3664: 25 54 75 84 24 94 e4 15 25 91 10 50 50 01 70 f1 %Tu.$...%..PP.p. +| 3680: 94 44 54 25 54 75 84 e4 f4 34 15 34 51 00 40 50 .DT%Tu...4.4Q.@P +| 3696: 01 70 f1 74 44 54 25 54 75 85 25 45 24 94 d2 70 .p.tDT%Tu.%E$..p +| 3712: 30 50 04 30 f1 94 34 f4 d5 04 94 c4 55 23 d6 76 0P.0..4.....U#.v +| 3728: 36 32 d3 52 e3 42 e3 02 03 23 03 13 63 03 63 03 62.R.B...#..c.c. +| 3744: 95 84 24 94 e4 15 25 92 70 20 50 04 30 f1 94 34 ..$...%.p P.0..4 +| 3760: f4 d5 04 94 c4 53 30 01 00 00 10 10 04 02 02 00 .....S0......... +| 3776: 00 00 00 00 00 00 00 80 00 00 00 20 00 00 00 10 ........... .... +| 3792: 00 00 00 90 00 00 00 40 00 00 00 00 00 00 00 00 .......@........ +| page 6 offset 20480 +| 3808: 06 24 03 00 12 02 01 01 06 23 03 00 12 02 01 01 .$.......#...... +| 3824: 06 22 03 00 12 02 01 01 06 21 03 00 12 03 01 01 .........!...... +| 3840: 06 20 03 00 12 03 01 01 06 1f 03 00 12 03 01 01 . .............. +| 3856: 06 1e 03 00 12 03 01 01 06 1d 03 00 12 03 01 01 ................ +| 3872: 06 1c 03 00 12 03 01 01 06 1b 03 00 12 02 01 01 ................ +| 3888: 06 1a 03 00 12 02 01 01 06 19 03 00 12 02 01 01 ................ +| 3904: 06 18 03 00 12 02 01 01 06 17 03 00 12 02 01 01 ................ +| 3920: 06 16 03 00 12 02 01 01 06 15 03 00 12 02 01 01 ................ +| 3936: 06 14 03 00 12 02 01 01 06 13 03 00 12 02 01 01 ................ +| 3952: 06 12 03 00 12 02 01 01 06 11 03 00 12 02 01 01 ................ +| 3968: 06 10 03 00 12 02 01 01 06 0f 03 00 12 02 01 01 ................ +| 3984: 06 0e 03 00 12 02 01 01 06 0d 03 00 12 02 01 01 ................ +| 4000: 06 0c 03 00 12 02 01 01 06 0b 03 00 12 02 01 01 ................ +| 4016: 06 0a 03 00 12 02 01 01 06 09 03 00 12 03 01 01 ................ +| 4032: 06 08 03 00 12 03 01 01 06 07 03 00 12 03 01 01 ................ +| 4048: 06 06 03 00 12 01 01 01 06 05 03 00 12 01 01 01 ................ +| 4064: 06 04 03 00 12 01 01 01 06 03 03 00 12 06 01 01 ................ +| 4080: 06 02 03 00 12 06 01 01 06 01 03 00 12 06 01 01 ................ +| page 7 offset 24576 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| page 8 offset 28672 +| 0: 0d 00 00 00 03 0f d6 00 0f f4 0f e9 0f d6 00 00 ................ +| 4048: 00 00 00 00 00 00 11 03 02 2b 69 6e 74 65 67 72 .........+integr +| 4064: 69 74 79 2d 63 68 65 63 6b 09 02 02 1b 72 65 62 ity-check....reb +| 4080: 75 69 6c 64 0a 01 02 1d 6f 70 74 69 6d 69 7a 65 uild....optimize +| end timeout-2ca5b0658c98.db +}]} {} + +do_catchsql_test 27.1 { + DELETE FROM t1 WHERE a MATCH 'fts*'; +} {/.*fts5: corrupt.*/} + +#------------------------------------------------------------------------- +reset_db +do_test 28.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 40960 pagesize 4096 filename crash-e2d47e0624a42c.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 0a .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 0d 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 96: 00 00 00 00 0d 00 00 00 0d 0b 6e 00 0f a3 0f 4c ..........n....L +| 112: 0e e1 0e 81 0e 24 0d cc 0d 72 0d 1b 0c b0 0c 50 .....$...r.....P +| 128: 0b f8 0b b3 0b 6e 00 00 00 00 00 00 00 00 00 00 .....n.......... +| 2912: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 43 0d ..............C. +| 2928: 06 17 11 11 08 75 74 61 62 6c 65 74 34 74 34 43 .....utablet4t4C +| 2944: 52 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 REATE VIRTUAL TA +| 2960: 42 4c 45 20 74 34 20 55 53 49 4e 47 20 66 74 73 BLE t4 USING fts +| 2976: 35 76 6f 63 61 62 28 27 74 32 27 2c 20 27 72 6f 5vocab('t2', 'ro +| 2992: 77 27 29 43 0c 06 17 11 11 08 75 74 61 62 6c 65 w')C......utable +| 3008: 74 33 74 33 43 52 45 41 54 45 20 56 49 52 54 55 t3t3CREATE VIRTU +| 3024: 41 4c 20 54 41 42 4c 45 20 74 33 20 55 53 49 4e AL TABLE t3 USIN +| 3040: 47 20 66 74 73 35 76 6f 63 61 62 28 27 74 31 27 G fts5vocab('t1' +| 3056: 2c 20 27 72 6f 77 27 29 56 0b 06 17 1f 1f 01 7d , 'row')V....... +| 3072: 74 61 62 6c 65 74 32 5f 63 6f 6e 66 69 67 74 32 tablet2_configt2 +| 3088: 5f 63 6f 6e 66 69 67 0a 43 52 45 41 54 45 20 54 _config.CREATE T +| 3104: 41 42 4c 45 20 27 74 32 5f 63 6f 6e 66 69 67 27 ABLE 't2_config' +| 3120: 28 6b 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 (k PRIMARY KEY, +| 3136: 76 29 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 v) WITHOUT ROWID +| 3152: 5e 0a 07 17 21 21 01 81 07 74 61 62 6c 65 74 32 ^...!!...tablet2 +| 3168: 5f 63 6f 6e 74 65 6e 74 74 32 5f 63 6f 6e 74 65 _contentt2_conte +| 3184: 6e 74 09 43 52 45 41 54 45 20 54 41 42 4c 45 20 nt.CREATE TABLE +| 3200: 27 74 32 5f 63 6f 6e 74 65 6e 74 27 28 69 64 20 't2_content'(id +| 3216: 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 INTEGER PRIMARY +| 3232: 4b 45 59 2c 20 63 30 2c 20 63 31 2c 20 63 32 29 KEY, c0, c1, c2) +| 3248: 69 09 07 17 19 19 01 81 2d 74 61 62 6c 65 74 32 i.......-tablet2 +| 3264: 5f 69 64 78 74 32 5f 69 64 78 08 43 52 45 41 54 _idxt2_idx.CREAT +| 3280: 45 20 54 41 42 4c 45 20 27 74 32 5f 69 64 78 27 E TABLE 't2_idx' +| 3296: 28 73 65 67 69 64 2c 20 74 65 72 6d 2c 20 70 67 (segid, term, pg +| 3312: 6e 6f 2c 20 50 52 49 4d 41 52 59 20 4b 45 59 28 no, PRIMARY KEY( +| 3328: 73 65 67 69 64 2c 20 74 65 72 6d 29 29 20 57 49 segid, term)) WI +| 3344: 54 48 4f 55 54 20 52 4f 57 49 44 55 08 07 17 1b THOUT ROWIDU.... +| 3360: 1b 01 81 01 74 61 62 6c 65 74 32 5f 64 61 74 61 ....tablet2_data +| 3376: 74 32 5f 64 61 74 61 07 43 52 45 41 54 45 20 54 t2_data.CREATE T +| 3392: 41 42 4c 45 20 27 74 32 5f 64 61 74 61 27 28 69 ABLE 't2_data'(i +| 3408: 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 d INTEGER PRIMAR +| 3424: 59 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 4c 4f Y KEY, block BLO +| 3440: 42 29 58 07 07 17 11 11 08 81 1d 74 61 62 6c 65 B)X........table +| 3456: 74 32 74 32 43 52 45 41 54 45 20 56 49 52 54 55 t2t2CREATE VIRTU +| 3472: 41 4c 20 54 41 42 4c 45 20 74 32 20 55 53 49 4e AL TABLE t2 USIN +| 3488: 47 20 66 74 73 35 28 27 61 27 2c 5b 62 5d 2c 22 G fts5('a',[b],. +| 3504: 63 22 2c 64 65 74 61 69 6c 3d 6e 6f 6e 65 2c 63 c.,detail=none,c +| 3520: 6f 6c 75 6d 6e 73 69 7a 65 3d 30 29 56 06 06 17 olumnsize=0)V... +| 3536: 1f 1f 01 7d 74 61 62 6c 65 74 31 5f 63 6f 6e 66 ....tablet1_conf +| 3552: 69 67 74 31 5f 63 6f 6e 66 69 67 06 43 52 45 41 igt1_config.CREA +| 3568: 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 6f 6e TE TABLE 't1_con +| 3584: 66 69 67 27 28 6b 20 50 52 49 4d 41 52 59 20 4b fig'(k PRIMARY K +| 3600: 45 59 2c 20 76 29 20 57 49 54 48 4f 55 54 20 52 EY, v) WITHOUT R +| 3616: 4f 57 49 44 5b 05 07 17 21 21 01 81 01 74 61 62 OWID[...!!...tab +| 3632: 6c 65 74 31 5f 64 6f 63 73 69 7a 65 74 31 5f 64 let1_docsizet1_d +| 3648: 6f 63 73 69 7a 65 05 43 52 45 41 54 45 20 54 41 ocsize.CREATE TA +| 3664: 42 4c 45 20 27 74 31 5f 64 6f 63 73 69 7a 65 27 BLE 't1_docsize' +| 3680: 28 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d (id INTEGER PRIM +| 3696: 41 52 59 20 4b 45 59 2c 20 73 7a 20 42 4c 4f 42 ARY KEY, sz BLOB +| 3712: 29 5e 04 07 17 21 21 01 81 07 74 61 62 6c 65 74 )^...!!...tablet +| 3728: 31 5f 63 6f 6e 74 65 6e 74 74 31 5f 63 6f 6e 74 1_contentt1_cont +| 3744: 65 6e 74 04 43 52 45 41 54 45 20 54 41 42 4c 45 ent.CREATE TABLE +| 3760: 20 27 74 31 5f 63 6f 6e 74 65 6e 74 27 28 69 64 't1_content'(id +| 3776: 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 INTEGER PRIMARY +| 3792: 20 4b 45 59 2c 20 63 30 2c 20 63 31 2c 20 63 32 KEY, c0, c1, c2 +| 3808: 29 69 03 07 17 19 19 01 81 2d 74 61 62 6c 65 74 )i.......-tablet +| 3824: 31 5f 69 64 78 74 31 5f 69 64 78 03 43 52 45 41 1_idxt1_idx.CREA +| 3840: 54 45 20 54 41 42 4c 45 20 27 74 31 5f 69 64 78 TE TABLE 't1_idx +| 3856: 27 28 73 65 67 69 64 2c 20 74 65 72 6d 2c 20 70 '(segid, term, p +| 3872: 67 6e 6f 2c 20 50 52 49 4d 41 52 59 20 4b 45 59 gno, PRIMARY KEY +| 3888: 28 73 65 67 69 64 2c 20 74 65 72 6d 29 29 20 57 (segid, term)) W +| 3904: 49 54 48 4f 55 54 20 52 4f 57 49 44 55 02 07 17 ITHOUT ROWIDU... +| 3920: 1b 1b 01 81 01 74 61 62 6c 65 74 31 5f 64 61 74 .....tablet1_dat +| 3936: 61 74 31 5f 64 61 74 61 02 43 52 45 41 54 45 20 at1_data.CREATE +| 3952: 54 41 42 4c 45 20 27 74 31 5f 64 61 74 61 27 28 TABLE 't1_data'( +| 3968: 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 id INTEGER PRIMA +| 3984: 52 59 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 4c RY KEY, block BL +| 4000: 4f 42 29 5b 01 07 17 11 11 08 81 23 74 61 62 6c OB)[.......#tabl +| 4016: 65 74 31 74 31 43 52 45 41 54 45 20 56 49 52 54 et1t1CREATE VIRT +| 4032: 55 41 4c 20 54 41 42 4c 45 20 74 31 20 55 53 49 UAL TABLE t1 USI +| 4048: 4e 47 20 66 74 73 35 28 61 2c 62 20 75 6e 69 6e NG fts5(a,b unin +| 4064: 64 65 78 65 64 2c 63 2c 74 6f 6b 65 6e 69 7a 65 dexed,c,tokenize +| 4080: 3d 22 70 6f 72 74 65 72 20 61 73 63 69 69 22 29 =.porter ascii.) +| page 2 offset 4096 +| 0: 0d 0f 68 00 05 0f 13 00 0f e6 0f 13 0f a8 0f 7c ..h............| +| 16: 0f 2a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 .*.............. +| 3856: 00 00 00 15 0a 03 00 30 00 00 00 00 01 03 03 00 .......0........ +| 3872: 03 01 01 01 02 01 01 03 01 01 37 8c 80 80 80 80 ..........7..... +| 3888: 01 03 00 74 00 00 00 2e 02 30 61 03 02 02 01 01 ...t.....0a..... +| 3904: 62 03 02 03 01 01 63 03 02 04 01 01 67 03 06 01 b.....c.....g... +| 3920: 02 02 01 01 68 03 06 01 02 03 01 01 69 03 06 01 ....h.......i... +| 3936: 02 04 04 06 06 06 08 08 0f ef 00 14 2a 00 00 00 ............*... +| 3952: 00 01 02 02 00 02 01 01 01 02 01 01 25 88 80 80 ............%... +| 3968: 80 80 01 03 00 50 00 00 00 1f 02 30 67 02 08 02 .....P.....0g... +| 3984: 01 02 02 01 01 68 02 08 03 01 02 03 01 01 69 02 .....h........i. +| 4000: 08 04 01 02 04 04 09 09 37 84 80 80 80 80 01 03 ........7....... +| 4016: 00 74 00 00 00 2e 02 30 61 01 02 02 01 01 62 01 .t.....0a.....b. +| 4032: 02 03 01 01 63 01 02 04 01 01 67 01 06 01 02 20 ....c.....g.... +| 4048: 10 16 80 10 60 10 20 30 10 16 90 10 60 10 20 40 ....`. 0....`. @ +| 4064: 40 60 60 60 80 80 70 10 30 01 40 30 90 00 90 00 @```..p.0.@0.... +| 4080: 00 01 12 40 00 00 00 00 10 10 10 00 10 10 10 10 ...@............ +| page 3 offset 8192 +| 0: a0 00 00 00 30 fe c0 00 ff a0 ff 30 fe 00 00 00 ....0......0.... +| 4064: 00 00 00 00 00 00 00 00 00 00 00 00 06 04 01 0c ................ +| 4080: 01 03 02 06 04 01 0c 01 02 02 05 04 09 0c 01 02 ................ +| page 4 offset 12288 +| 0: 0d 00 00 00 03 0f be 00 0f ea 0f d4 0f be 00 00 ................ +| 4016: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 14 03 ................ +| 4032: 05 00 17 17 17 61 20 62 20 63 67 20 68 20 69 67 .....a b cg h ig +| 4048: 20 68 20 69 14 02 05 00 17 17 17 67 20 68 20 69 h i.......g h i +| 4064: 61 20 62 20 63 67 20 68 20 69 14 01 05 00 17 17 a b cg h i...... +| 4080: 17 61 20 62 20 63 64 20 65 20 66 67 20 68 20 69 .a b cd e fg h i +| page 5 offset 16384 +| 0: 0d 00 00 00 03 0f e8 00 0f f8 0f f0 0f e8 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 06 03 03 00 12 03 00 03 ................ +| 4080: 06 02 03 00 12 03 00 03 06 01 03 00 12 03 00 03 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| page 7 offset 24576 +| 0: 0d 00 00 00 03 0f 9e 00 0f e6 0f ef 0f 9e 00 00 ................ +| 3984: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 41 84 ..............A. +| 4000: 80 80 80 80 01 04 00 81 06 00 00 00 34 02 30 61 ............4.0a +| 4016: 01 01 01 01 01 62 01 01 01 01 01 63 01 01 01 01 .....b.....c.... +| 4032: 01 64 01 01 01 65 01 01 01 66 01 01 01 01 01 68 .d...e...f.....h +| 4048: 01 01 01 01 01 69 01 01 01 04 06 06 06 04 04 04 .....i.......... +| 4064: 06 06 07 01 03 00 14 03 09 09 09 0f 0a 03 00 24 ...............$ +| 4080: 00 00 00 00 01 01 01 00 01 01 01 01 0a 00 00 00 ................ +| page 8 offset 28672 +| 0: 01 0f fa 00 0f fa 00 00 00 00 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 05 04 09 0c 01 02 ................ +| page 9 offset 32768 +| 0: 0d 00 00 00 03 0f be 00 0f ea 0f d4 1f be 00 00 ................ +| 4016: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 14 03 ................ +| 4032: 05 00 17 17 17 61 20 62 20 63 67 20 68 20 69 67 .....a b cg h ig +| 4048: 20 68 20 69 14 02 05 00 17 17 17 67 20 68 20 69 h i.......g h i +| 4064: 61 20 62 20 63 67 20 68 20 69 14 01 05 00 17 17 a b cg h i...... +| 4080: 17 61 20 62 20 63 64 20 65 20 66 67 20 68 20 69 .a b cd e fg h i +| page 10 offset 36864 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| end crash-e2d47e0624a42c.db +}]} {} + +do_catchsql_test 28.1 { + SELECT count( fts5_decode(id, block) ) FROM t2_data; } {1 {database disk image is malformed}} +#------------------------------------------------------------------------- +reset_db +do_test 29.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 28672 pagesize 4096 filename crash-e114c036e13dde.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 07 .....@ ........ +| 32: 00 00 00 02 00 00 00 01 00 00 00 08 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 96: 00 00 00 00 0d 0f c7 00 06 0d b6 00 0f 8d 0f 36 ...............6 +| 112: 0e cb 0e 6b 0e 0e 0d b6 0d b6 00 00 00 00 00 00 ...k............ +| 3504: 00 00 00 00 00 00 56 07 06 17 1f 1f 01 7d 74 61 ......V.......ta +| 3520: 62 6c 65 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 blet1_configt1_c +| 3536: 6f 6e 66 69 67 07 43 52 45 41 54 45 20 54 41 42 onfig.CREATE TAB +| 3552: 4c 45 20 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b LE 't1_config'(k +| 3568: 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 PRIMARY KEY, v) +| 3584: 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 5b 06 WITHOUT ROWID[. +| 3600: 07 17 21 21 01 81 01 74 61 62 6c 65 74 31 5f 64 ..!!...tablet1_d +| 3616: 6f 63 73 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 ocsizet1_docsize +| 3632: 06 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 .CREATE TABLE 't +| 3648: 31 5f 64 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 1_docsize'(id IN +| 3664: 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE +| 3680: 59 2c 20 73 7a 20 42 4c 4f 42 29 5e 05 07 17 21 Y, sz BLOB)^...! +| 3696: 21 01 81 07 74 61 62 6c 65 74 31 5f 63 6f 6e 74 !...tablet1_cont +| 3712: 65 6e 74 74 31 5f 63 6f 6e 74 65 6e 74 05 43 52 entt1_content.CR +| 3728: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 EATE TABLE 't1_c +| 3744: 6f 6e 74 65 6e 74 27 28 69 64 20 49 4e 54 45 47 ontent'(id INTEG +| 3760: 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 ER PRIMARY KEY, +| 3776: 63 30 2c 20 63 31 2c 20 63 32 29 69 04 07 17 19 c0, c1, c2)i.... +| 3792: 19 01 81 2d 74 61 62 6c 65 74 31 5f 69 64 78 74 ...-tablet1_idxt +| 3808: 31 5f 69 64 78 04 43 52 45 41 54 45 20 54 41 42 1_idx.CREATE TAB +| 3824: 4c 45 20 27 74 31 5f 69 64 78 27 28 73 65 67 69 LE 't1_idx'(segi +| 3840: 64 2c 20 74 65 72 6d 2c 20 70 67 6e 6f 2c 20 50 d, term, pgno, P +| 3856: 52 49 4d 41 52 59 20 4b 45 59 28 73 65 67 69 64 RIMARY KEY(segid +| 3872: 2c 20 74 65 72 6d 29 29 20 57 49 54 48 4f 55 54 , term)) WITHOUT +| 3888: 20 52 4f 57 49 44 55 03 07 17 1b 1b 01 81 01 74 ROWIDU........t +| 3904: 61 62 6c 65 74 31 5f 64 61 74 61 74 31 5f 64 61 ablet1_datat1_da +| 3920: 74 61 03 43 52 45 41 54 45 20 54 41 42 4c 45 20 ta.CREATE TABLE +| 3936: 27 74 31 5f 64 61 74 61 27 28 69 64 20 49 4e 54 't1_data'(id INT +| 3952: 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 EGER PRIMARY KEY +| 3968: 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 29 38 02 06 , block BLOB)8.. +| 3984: 17 11 11 08 5f 74 61 62 6c 65 74 31 74 31 43 52 ...._tablet1t1CR +| 4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 35 LE t1 USING fts5 +| 4032: 28 61 2c 62 2c 63 29 00 00 00 39 00 00 00 00 00 (a,b,c)...9..... +| page 3 offset 8192 +| 0: 0d 00 00 00 03 0c af 00 0f e6 0f ef 0c af 00 00 ................ +| 3232: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 86 ................ +| 3248: 2f 84 80 80 80 80 01 04 00 8c 62 00 00 03 12 02 /.........b..... +| 3264: 30 30 01 04 05 03 01 04 05 03 01 04 05 03 1f 02 00.............. +| 3280: 03 01 02 03 01 02 03 01 08 35 30 30 30 30 30 30 .........5000000 +| 3296: 30 1c 02 04 01 02 04 01 02 04 01 01 36 01 02 04 0...........6... +| 3312: 01 02 04 01 02 04 01 06 62 69 6e 61 72 79 03 06 ........binary.. +| 3328: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3344: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3360: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 ................ +| 3376: 03 06 01 02 02 03 06 01 02 02 01 05 63 6c 61 6e ............clan +| 3392: 67 01 02 03 01 02 03 01 02 03 02 07 6f 6d 70 69 g...........ompi +| 3408: 6c 65 72 01 02 02 01 02 02 01 02 02 01 06 64 62 ler...........db +| 3424: 73 74 61 74 07 02 03 01 02 03 01 02 03 02 04 65 stat...........e +| 3440: 62 75 67 04 02 02 01 02 02 01 02 02 01 06 65 6e bug...........en +| 3456: 61 62 6c 65 07 02 02 01 02 02 01 02 02 01 02 02 able............ +| 3472: 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 ................ +| 3488: 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 ................ +| 3504: 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 ................ +| 3520: 01 02 02 02 08 78 74 65 6e 73 69 6f 6e 1f 02 04 .....xtension... +| 3536: 01 02 04 01 02 04 01 04 66 74 73 34 0a 02 03 03 ........fts4.... +| 3552: 01 02 01 02 03 04 01 35 0d 02 03 01 02 03 01 02 .......5........ +| 3568: 03 01 07 67 65 6f 70 6f 6c 79 10 02 03 01 02 03 ...geopoly...... +| 3584: 01 02 03 01 05 6a 73 6f 6e 31 13 02 03 01 02 03 .....json1...... +| 3600: 01 02 03 01 04 6c 6f 61 64 1f 02 03 01 02 03 01 .....load....... +| 3616: 02 03 01 03 6d 61 78 1c 02 02 01 02 02 01 02 02 ....max......... +| 3632: 02 05 65 6d 6f 72 79 1c 02 03 01 02 03 01 02 03 ..emory......... +| 3648: 04 04 73 79 73 35 16 02 03 01 02 03 01 02 03 01 ..sys5.......... +| 3664: 06 6e 6f 63 61 73 65 02 06 01 02 02 03 06 01 02 .nocase......... +| 3680: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 ................ +| 3696: 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 03 ................ +| 3712: 06 01 02 02 03 06 01 02 02 03 06 01 12 02 03 06 ................ +| 3728: 01 02 02 01 04 6f 6d 69 74 1f 02 02 01 02 02 01 .....omit....... +| 3744: 02 02 01 05 72 74 72 65 65 19 02 03 01 02 03 01 ....rtree....... +| 3760: 02 03 04 02 69 6d 01 06 01 02 02 03 06 01 02 02 ....im.......... +| 3776: 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 03 ................ +| 3792: 06 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 ................ +| 3808: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3824: 02 02 01 0a 74 68 72 65 61 64 73 61 66 65 22 02 ....threadsafe.. +| 3840: 02 01 02 02 01 02 02 01 04 76 74 61 62 07 02 04 .........vtab... +| 3856: 01 02 04 01 02 04 01 01 78 01 06 01 01 02 01 06 ........x....... +| 3872: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 ................ +| 3888: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 ................ +| 3904: 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 ................ +| 3920: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 3936: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 3952: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 ................ +| 3968: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 ................ +| 3984: 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 ................ +| 4000: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 4016: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 4032: 01 01 02 01 06 01 01 02 01 06 01 01 02 04 18 13 ................ +| 4048: 0c 44 10 12 11 0f 47 13 0f 0c 12 10 0f 0e 10 0f .D....G......... +| 4064: 44 0f 10 40 15 0f 07 01 03 00 14 24 57 24 24 0f D..@.......$W$$. +| 4080: 0a 03 00 24 00 00 00 00 01 01 01 00 01 01 01 01 ...$............ +| page 4 offset 12288 +| 0: 0a 00 00 00 01 0f fa 00 0f fa 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 05 04 09 0c 01 02 ................ +| page 5 offset 16384 +| 0: 0d 00 00 00 24 0c 1f 00 0f df 0f bd 0f 9b 0f 89 ....$........... +| 16: 0f 76 0f 63 0f 44 0f 24 0f 04 0e ec 0e d3 0e ba .v.c.D.$........ +| 32: 0e a2 0e 89 0e 70 e5 50 e3 90 e1 d0 e0 40 de a0 .....p.P.....@.. +| 48: dd 00 db 50 d9 90 d7 ea ca ea be d0 d6 40 d4 a0 ...P.........@.. +| 64: d3 00 d1 00 ce f0 cc e0 ca e0 c8 d0 c6 c0 c5 30 ...............0 +| 80: c3 90 c1 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 3088: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 18 ................ +| 3104: 24 05 00 25 0f 19 54 48 52 45 41 44 53 41 46 45 $..%..THREADSAFE +| 3120: 3d 30 58 42 49 4e 41 52 59 18 23 05 00 25 0f 19 =0XBINARY.#..%.. +| 3136: 54 48 52 45 41 44 53 41 46 45 3d 30 58 4e 4f 43 THREADSAFE=0XNOC +| 3152: 41 53 45 17 22 05 00 25 0f 17 54 48 52 45 41 44 ASE....%..THREAD +| 3168: 53 41 46 45 3d 30 05 00 33 0f 19 4f 4d 49 54 20 SAFE=0..3..OMIT +| 3184: 4c 4f 41 44 20 45 58 54 45 4e 53 49 4f 4e 58 42 LOAD EXTENSIONXB +| 3200: 49 4e 41 52 59 1f 20 05 00 33 0f 19 4f 4d 49 54 INARY. ..3..OMIT +| 3216: 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 4f 4e 58 LOAD EXTENSIONX +| 3232: 4e 4f 43 41 53 45 1e 1f 05 00 33 0f 17 4f 4d 49 NOCASE....3..OMI +| 3248: 54 20 4c f4 14 42 04 55 85 44 54 e5 34 94 f4 e5 T L..B.U.DT.4... +| 3264: 85 25 45 24 94 d1 f1 e0 50 03 30 f1 94 d4 15 82 .%E$....P.0..... +| 3280: 04 d4 54 d4 f5 25 93 d3 53 03 03 03 03 03 03 05 ..T..%..S....... +| 3296: 84 24 94 e4 15 25 91 f1 d0 50 03 30 f1 94 d4 15 .$...%...P.0.... +| 3312: 82 04 d4 54 d4 f5 25 93 d3 53 03 03 03 03 03 03 ...T..%..S...... +| 3328: 05 84 e4 f4 34 15 34 51 e1 c0 50 03 30 f1 74 d4 ....4.4Q..P.0.t. +| 3344: 15 82 04 d4 54 d4 f2 90 f1 74 54 e4 14 24 c4 52 ....T....tT..$.R +| 3360: 04 74 54 f5 04 f4 c5 95 85 25 45 24 94 d1 70 f0 .tT......%E$..p. +| 3376: 50 02 30 f1 94 54 e4 14 24 c4 52 04 65 45 33 55 P.0..T..$.R.eE3U +| 3392: 84 24 94 e4 15 25 91 70 e0 50 02 30 f1 94 54 e4 .$...%.p.P.0..T. +| 3408: 14 24 c4 52 04 65 45 33 55 84 e4 f4 34 15 34 51 .$.R.eE3U...4.4Q +| 3424: 60 d0 50 02 30 f1 74 54 e4 14 24 c4 52 04 65 45 `.P.0.tT..$.R.eE +| 3440: 33 55 85 25 45 24 94 d1 70 c0 50 02 30 f1 94 54 3U.%E$..p.P.0..T +| 3456: e4 14 24 c4 52 04 65 45 33 45 84 24 94 e4 15 25 ..$.R.eE3E.$...% +| 3472: 91 70 b0 50 02 30 f1 94 54 e4 14 24 c4 52 04 65 .p.P.0..T..$.R.e +| 3488: 45 33 45 84 e4 f4 34 15 34 51 60 a0 74 54 e4 14 E3E...4.4Q`.tT.. +| 3504: 24 c4 52 04 65 45 33 45 85 25 45 24 94 d1 e0 90 $.R.eE3E.%E$.... +| 3520: 50 03 10 f1 94 54 e4 14 24 c4 52 04 44 25 35 44 P....T..$.R.D%5D +| 3536: 15 42 05 65 44 14 25 84 24 94 e4 15 25 91 e0 80 .B.eD.%.$...%... +| 3552: 50 03 10 f1 94 54 e4 14 24 c4 52 04 44 25 35 44 P....T..$.R.D%5D +| 3568: 15 42 05 65 44 14 25 84 e4 f4 34 15 34 51 d0 70 .B.eD.%...4.4Q.p +| 3584: 50 03 10 f1 74 54 e4 14 24 c4 52 04 44 25 35 44 P...tT..$.R.D%5D +| 3600: 15 42 05 65 44 14 25 85 25 45 24 94 d1 10 60 50 .B.eD.%.%E$...`P +| 3616: 01 70 f1 94 44 54 25 54 75 84 24 94 e4 15 25 91 .p..DT%Tu.$...%. +| 3632: 10 50 50 01 70 f1 94 44 54 25 54 75 84 e4 f4 34 .PP.p..DT%Tu...4 +| 3648: 15 34 51 00 40 50 01 70 f1 74 44 54 25 54 75 85 .4Q.@P.p.tDT%Tu. +| 3664: 25 45 24 94 d2 00 30 50 03 50 f1 94 34 f4 d5 04 %E$...0P.P..4... +| 3680: 94 c4 55 23 d6 36 c6 16 e6 72 d3 62 e3 02 e3 05 ..U#.6...r.b.... +| 3696: 84 24 94 e4 15 25 92 00 20 50 03 50 f1 94 34 f4 .$...%.. P.P..4. +| 3712: d5 04 94 c4 55 23 d6 36 c6 16 e6 72 d3 62 e3 02 ....U#.6...r.b.. +| 3728: e3 05 84 e4 f4 34 15 34 51 f0 10 50 03 50 f1 74 .....4.4Q..P.P.t +| 3744: 34 f4 d5 04 94 c4 55 23 d6 36 c6 16 e6 72 d3 62 4.....U#.6...r.b +| 3760: e3 02 e3 05 85 25 45 24 94 d0 d0 00 00 02 40 ee .....%E$......@. +| 3776: 00 00 ff 80 ff 00 fe 80 fe 00 fd 80 fd 00 fc 80 ................ +| 3792: fc 00 fb 80 fb 00 fa 80 fa 00 f9 80 f9 00 f8 80 ................ +| 3808: f8 00 f7 80 f7 00 f6 80 f6 00 f5 80 f5 00 f4 80 ................ +| 3824: f4 00 f3 80 f3 00 f2 80 f2 00 f1 80 f1 00 f0 80 ................ +| 3840: f0 00 ef 80 ef 00 ee 80 ee 00 00 00 00 00 00 00 ................ +| page 6 offset 20480 +| 3808: 06 24 03 00 12 02 01 01 06 23 03 00 12 02 01 01 .$.......#...... +| 3824: 06 22 03 00 12 02 01 01 06 21 03 00 12 03 01 01 .........!...... +| 3840: 06 20 03 00 12 03 01 01 06 1f 03 00 12 03 01 01 . .............. +| 3856: 06 1e 03 00 12 03 01 01 06 1d 03 00 12 03 01 01 ................ +| 3872: 06 1c 03 00 12 03 01 01 06 1b 03 00 12 02 01 01 ................ +| 3888: 06 1a 03 00 12 02 01 01 06 19 03 00 12 02 01 01 ................ +| 3904: 06 18 03 00 12 02 01 01 06 17 03 00 12 02 01 01 ................ +| 3920: 06 16 03 00 12 02 01 01 06 15 03 00 12 02 01 01 ................ +| 3936: 06 14 03 00 12 02 01 01 06 13 03 00 12 02 01 01 ................ +| 3952: 06 12 03 00 12 02 01 01 06 11 03 00 12 02 01 01 ................ +| 3968: 06 10 03 00 12 02 01 01 06 0f 03 00 12 02 01 01 ................ +| 3984: 06 0e 03 00 12 02 01 01 06 0d 03 00 12 02 01 01 ................ +| 4000: 06 0c 03 00 12 02 01 01 06 0b 03 00 12 02 01 01 ................ +| 4016: 06 0a 03 00 12 02 01 01 06 09 03 00 12 03 01 01 ................ +| 4032: 06 08 03 00 12 03 01 01 06 07 03 00 12 03 01 01 ................ +| 4048: 06 06 03 00 12 01 01 01 06 05 03 00 12 01 01 01 ................ +| 4064: 06 04 03 00 12 01 01 01 06 03 03 00 12 05 01 01 ................ +| 4080: 06 02 03 00 12 05 01 01 06 01 03 00 12 05 01 01 ................ +| page 7 offset 24576 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| end crash-e114c036e13dde.db +}]} {} + +do_catchsql_test 29.1 { + CREATE VIRTUAL TABLE t3 USING fts5vocab('t1','col'); +} {0 {}} +do_catchsql_test 29.2 { + SELECT rowid, quote(term), * FROM t3 WHERE term=='nocase'; +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +reset_db +do_test 30.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 40960 pagesize 4096 filename crash-eef41e30b388a0.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 0a .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 0d 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 96: 00 00 00 00 0d 00 00 00 0d 0b 6e 00 0f a3 0f 4c ..........n....L +| 112: 0e e1 0e 81 0e 24 0d cc 0d 72 0d 1b 0c b0 0c 50 .....$...r.....P +| 128: 0b f8 0b b3 0b 6e 00 00 00 00 00 00 00 00 00 00 .....n.......... +| 2912: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 43 0d ..............C. +| 2928: 06 17 11 11 08 75 74 61 62 6c 65 74 34 74 34 43 .....utablet4t4C +| 2944: 52 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 REATE VIRTUAL TA +| 2960: 42 4c 45 20 74 34 20 55 53 49 4e 47 20 66 74 73 BLE t4 USING fts +| 2976: 35 76 6f 63 61 62 28 27 74 32 27 2c 20 27 72 6f 5vocab('t2', 'ro +| 2992: 77 27 29 43 0c 06 17 11 11 08 75 74 61 62 6c 65 w')C......utable +| 3008: 74 33 74 33 43 52 45 41 54 45 20 56 49 52 54 55 t3t3CREATE VIRTU +| 3024: 41 4c 20 54 41 42 4c 45 20 74 33 20 55 53 49 4e AL TABLE t3 USIN +| 3040: 47 20 66 74 73 35 76 6f 63 61 62 28 27 74 31 27 G fts5vocab('t1' +| 3056: 2c 20 27 72 6f 77 27 29 56 0b 06 17 1f 1f 01 7d , 'row')V....... +| 3072: 74 61 62 6c 65 74 32 5f 63 6f 6e 66 69 67 74 32 tablet2_configt2 +| 3088: 5f 63 6f 6e 66 69 67 0a 43 52 45 41 54 45 20 54 _config.CREATE T +| 3104: 41 42 4c 45 20 27 74 32 5f 63 6f 6e 66 69 67 27 ABLE 't2_config' +| 3120: 28 6b 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 (k PRIMARY KEY, +| 3136: 76 29 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 v) WITHOUT ROWID +| 3152: 5e 0a 07 17 21 21 01 81 07 74 61 62 6c 65 74 32 ^...!!...tablet2 +| 3168: 5f 63 6f 6e 74 65 6e 74 74 32 5f 63 6f 6e 74 65 _contentt2_conte +| 3184: 6e 74 09 43 52 45 41 54 45 20 54 41 42 4c 45 20 nt.CREATE TABLE +| 3200: 27 74 32 5f 63 6f 6e 74 65 6e 74 27 28 69 64 20 't2_content'(id +| 3216: 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 INTEGER PRIMARY +| 3232: 4b 45 59 2c 20 63 30 2c 20 63 31 2c 20 63 32 29 KEY, c0, c1, c2) +| 3248: 69 09 07 17 19 19 01 81 2d 74 61 62 6c 65 74 32 i.......-tablet2 +| 3264: 5f 69 64 78 74 32 5f 69 64 78 08 43 52 45 41 54 _idxt2_idx.CREAT +| 3280: 45 20 54 41 42 4c 45 20 27 74 32 5f 69 64 78 27 E TABLE 't2_idx' +| 3296: 28 73 65 67 69 64 2c 20 74 65 72 6d 2c 20 70 67 (segid, term, pg +| 3312: 6e 6f 2c 20 50 52 49 4d 41 52 59 20 4b 45 59 28 no, PRIMARY KEY( +| 3328: 73 65 67 69 64 2c 20 74 65 72 6d 29 29 20 57 49 segid, term)) WI +| 3344: 54 48 4f 55 54 20 52 4f 57 49 44 55 08 07 17 1b THOUT ROWIDU.... +| 3360: 1b 01 81 01 74 61 62 6c 65 74 32 5f 64 61 74 61 ....tablet2_data +| 3376: 74 32 5f 64 61 74 61 07 43 52 45 41 54 45 20 54 t2_data.CREATE T +| 3392: 41 42 4c 45 20 27 74 32 5f 64 61 74 61 27 28 69 ABLE 't2_data'(i +| 3408: 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 d INTEGER PRIMAR +| 3424: 59 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 4c 4f Y KEY, block BLO +| 3440: 42 29 58 07 07 17 11 11 08 81 1d 74 61 62 6c 65 B)X........table +| 3456: 74 32 74 32 43 52 45 41 54 45 20 56 49 52 54 55 t2t2CREATE VIRTU +| 3472: 41 4c 20 54 41 42 4c 45 20 74 32 20 55 53 49 4e AL TABLE t2 USIN +| 3488: 47 20 66 74 73 35 28 27 61 27 2c 5b 62 5d 2c 22 G fts5('a',[b],. +| 3504: 63 22 2c 64 65 74 61 69 6c 3d 6e 6f 6e 65 2c 63 c.,detail=none,c +| 3520: 6f 6c 75 6d 6e 73 69 7a 65 3d 30 29 56 06 06 17 olumnsize=0)V... +| 3536: 1f 1f 01 7d 74 61 62 6c 65 74 31 5f 63 6f 6e 66 ....tablet1_conf +| 3552: 69 67 74 31 5f 63 6f 6e 66 69 67 06 43 52 45 41 igt1_config.CREA +| 3568: 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 6f 6e TE TABLE 't1_con +| 3584: 66 69 67 27 28 6b 20 50 52 49 4d 41 52 59 20 4b fig'(k PRIMARY K +| 3600: 45 59 2c 20 76 29 20 57 49 54 48 4f 55 54 20 52 EY, v) WITHOUT R +| 3616: 4f 57 49 44 5b 05 07 17 21 21 01 81 01 74 61 62 OWID[...!!...tab +| 3632: 6c 65 74 31 5f 64 6f 63 73 69 7a 65 74 31 5f 64 let1_docsizet1_d +| 3648: 6f 63 73 69 7a 65 05 43 52 45 41 54 45 20 54 41 ocsize.CREATE TA +| 3664: 42 4c 45 20 27 74 31 5f 64 6f 63 73 69 7a 65 27 BLE 't1_docsize' +| 3680: 28 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d (id INTEGER PRIM +| 3696: 41 52 59 20 4b 45 59 2c 20 73 7a 20 42 4c 4f 42 ARY KEY, sz BLOB +| 3712: 29 5e 04 07 17 21 21 01 81 07 74 61 62 6c 65 74 )^...!!...tablet +| 3728: 31 5f 63 6f 6e 74 65 6e 74 74 31 5f 63 6f 6e 74 1_contentt1_cont +| 3744: 65 6e 74 04 43 52 45 41 54 45 20 54 41 42 4c 45 ent.CREATE TABLE +| 3760: 20 27 74 31 5f 63 6f 6e 74 65 6e 74 27 28 69 64 't1_content'(id +| 3776: 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 INTEGER PRIMARY +| 3792: 20 4b 45 59 2c 20 63 30 2c 20 63 31 2c 20 63 32 KEY, c0, c1, c2 +| 3808: 29 69 03 07 17 19 19 01 81 2d 74 61 62 6c 65 74 )i.......-tablet +| 3824: 31 5f 69 64 78 74 31 5f 69 64 78 03 43 52 45 41 1_idxt1_idx.CREA +| 3840: 54 45 20 54 41 42 4c 45 20 27 74 31 5f 69 64 78 TE TABLE 't1_idx +| 3856: 27 28 73 65 67 69 64 2c 20 74 65 72 6d 2c 20 70 '(segid, term, p +| 3872: 67 6e 6f 2c 20 50 52 49 4d 41 52 59 20 4b 45 59 gno, PRIMARY KEY +| 3888: 28 73 65 67 69 64 2c 20 74 65 72 6d 29 29 20 57 (segid, term)) W +| 3904: 49 54 48 4f 55 54 20 52 4f 57 49 44 55 02 07 17 ITHOUT ROWIDU... +| 3920: 1b 1b 01 81 01 74 61 62 6c 65 74 31 5f 64 61 74 .....tablet1_dat +| 3936: 61 74 31 5f 64 61 74 61 02 43 52 45 41 54 45 20 at1_data.CREATE +| 3952: 54 41 42 4c 45 20 27 74 31 5f 64 61 74 61 27 28 TABLE 't1_data'( +| 3968: 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 id INTEGER PRIMA +| 3984: 52 59 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 4c RY KEY, block BL +| 4000: 4f 42 29 5b 01 07 17 11 11 08 81 23 74 61 62 6c OB)[.......#tabl +| 4016: 65 74 31 74 31 43 52 45 41 54 45 20 56 49 52 54 et1t1CREATE VIRT +| 4032: 55 41 4c 20 54 41 42 4c 45 20 74 31 20 55 53 49 UAL TABLE t1 USI +| 4048: 4e 47 20 66 74 73 35 28 61 2c 62 20 75 6e 69 6e NG fts5(a,b unin +| 4064: 64 65 78 65 64 2c 63 2c 74 6f 6b 65 6e 69 7a 65 dexed,c,tokenize +| 4080: 3d 22 70 6f 72 74 65 72 20 61 73 63 69 69 22 29 =.porter ascii.) +| page 2 offset 4096 +| 0: 0d 0f 68 00 05 0f 13 00 0f e6 0f 13 0f a8 0f 7c ..h............| +| 16: 0f 2a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 .*.............. +| 3856: 00 00 00 15 0a 03 00 30 00 00 00 00 01 03 03 00 .......0........ +| 3872: 03 01 01 01 02 01 01 03 01 01 37 8c 80 80 80 80 ..........7..... +| 3888: 01 03 00 74 00 00 00 2e 02 30 61 03 02 02 01 01 ...t.....0a..... +| 3904: 62 03 02 03 01 01 63 03 02 04 01 01 67 03 06 01 b.....c.....g... +| 3920: 02 02 01 01 68 03 06 01 02 03 01 01 69 03 06 01 ....h.......i... +| 3936: 02 04 00 00 66 46 08 08 0f ef 00 14 2a 00 00 00 ....fF......*... +| 3952: 00 01 02 02 00 02 01 01 01 02 01 01 25 88 80 80 ............%... +| 3968: 80 80 01 03 00 50 00 00 00 1f 02 30 67 02 08 02 .....P.....0g... +| 3984: 01 02 02 01 01 68 02 08 03 01 02 03 01 01 69 02 .....h........i. +| 4000: 08 04 01 02 04 04 09 09 37 84 80 80 80 80 01 03 ........7....... +| 4016: 00 74 00 00 00 2e 02 30 61 01 02 02 01 01 62 01 .t.....0a.....b. +| 4032: 02 03 01 01 63 01 02 04 01 01 67 01 06 01 02 02 ....c.....g..... +| 4048: 01 01 68 01 06 01 02 03 01 01 69 01 06 01 02 04 ..h.......i..... +| 4064: 04 06 06 06 08 08 07 01 03 00 14 03 09 00 09 00 ................ +| 4080: 00 00 11 24 00 00 00 00 01 01 01 00 01 01 01 01 ...$............ +| page 3 offset 8192 +| 0: 0a 00 00 00 03 0f ec 00 0f fa 0f f3 0f ec 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 00 06 04 01 0c ................ +| 4080: 01 03 02 06 04 01 0c 01 02 02 05 04 09 0c 01 02 ................ +| page 4 offset 12288 +| 0: 0d 00 00 00 03 0f be 00 0f ea 0f d4 0f be 00 00 ................ +| 4016: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 14 03 ................ +| 4032: 05 00 17 17 17 61 20 62 20 63 67 20 68 20 69 67 .....a b cg h ig +| 4048: 20 68 20 69 14 02 05 00 17 17 17 67 20 68 20 69 h i.......g h i +| 4064: 61 20 62 20 63 67 20 68 20 69 14 01 05 00 17 17 a b cg h i...... +| 4080: 17 61 20 62 20 63 64 20 52 06 66 72 06 82 06 90 .a b cd R.fr.... +| page 5 offset 16384 +| 0: d0 00 00 00 30 fe 80 00 ff 80 ff 00 fe 00 00 00 ....0........... +| 4064: 00 00 00 00 00 00 00 00 06 03 03 00 12 03 00 03 ................ +| 4080: 06 02 03 00 12 03 00 03 06 01 03 00 12 03 00 03 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| page 7 offset 24576 +| 0: 0d 00 00 00 03 0f 9e 00 0f e6 0f ef 0f 9e 00 00 ................ +| 3984: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 41 84 ..............A. +| 4000: 80 80 80 80 01 04 00 81 06 00 00 00 34 02 30 61 ............4.0a +| 4016: 01 01 01 01 01 62 01 01 01 01 01 63 01 01 01 01 .....b.....c.... +| 4032: 01 64 01 01 01 65 01 01 01 66 01 01 01 67 01 01 .d...e...f...g.. +| 4048: 01 01 01 68 01 01 01 01 01 69 01 01 01 04 06 06 ...h.....i...... +| 4064: 06 04 04 04 06 06 07 01 03 00 14 03 09 09 09 0f ................ +| 4080: 0a 03 00 24 00 00 00 00 01 01 01 00 01 01 01 01 ...$............ +| page 8 offset 28672 +| 0: 0a 00 00 00 01 0f fa 00 0f fa 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 05 04 09 0c 01 02 ................ +| page 9 offset 32768 +| 0: 0d 00 00 00 03 0f be 00 0f ea 0f d4 0f be 00 00 ................ +| 4016: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 14 03 ................ +| 4032: 05 00 17 17 17 61 20 62 20 63 67 20 68 20 69 67 .....a b cg h ig +| 4048: 20 68 20 69 14 02 05 00 17 17 17 67 20 68 20 69 h i.......g h i +| 4064: 61 20 62 20 63 67 20 68 20 69 14 01 05 00 17 17 a b cg h i...... +| 4080: 17 61 20 62 20 63 64 20 65 20 66 67 20 68 20 69 .a b cd e fg h i +| page 10 offset 36864 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| end crash-eef41e30b388a0.db +}]} {} + +do_catchsql_test 30.1 { + SELECT fts5_decode(id, block) FROM t1_data; +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +reset_db +do_test 31.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 8192 pagesize 4096 filename crash-7629f35f11d48e.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 02 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 96: 00 00 00 00 0d 00 00 00 01 0f c7 00 0f c7 00 00 ................ +| 4032: 00 00 00 00 00 00 00 37 01 06 17 15 15 01 53 74 .......7......St +| 4048: 61 62 6c 65 64 75 61 6c 64 75 61 6c 02 43 52 45 abledualdual.CRE +| 4064: 41 54 45 20 54 41 42 4c 45 20 64 75 61 6c 28 64 ATE TABLE dual(d +| 4080: 75 6d 6d 79 20 76 61 72 28 31 29 29 0d 00 00 00 ummy var(1)).... +| page 2 offset 4096 +| 0: 01 0f fb 00 0f fb 00 00 00 00 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 00 03 01 02 0f 58 ...............X +| end crash-7629f35f11d48e.db +}]} {} + +do_execsql_test 31.1 { + CREATE VIRTUAL TABLE t1 USING fts5(a,b,c); + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<72) + INSERT INTO t1(a) SELECT randomblob(2829) FROM c; + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<10) + INSERT INTO t1(a) SELECT randomblob(3000) FROM c; +} + +do_catchsql_test 31.2 { + DELETE FROM t1 WHERE a MATCH X'6620e574f32a'; +} {0 {}} + +#------------------------------------------------------------------------- +reset_db +do_test 32.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 40960 pagesize 4096 filename crash-e2d47e0624a42c.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 0a .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 0d 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 96: 00 00 00 00 0d 00 00 00 0d 0b 6e 00 0f a3 0f 4c ..........n....L +| 112: 0e e1 0e 81 0e 24 0d cc 0d 72 0d 1b 0c b0 0c 50 .....$...r.....P +| 128: 0b f8 0b b3 0b 6e 00 00 00 00 00 00 00 00 00 00 .....n.......... +| 2912: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 43 0d ..............C. +| 2928: 06 17 11 11 08 75 74 61 62 6c 65 74 34 74 34 43 .....utablet4t4C +| 2944: 52 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 REATE VIRTUAL TA +| 2960: 42 4c 45 20 74 34 20 55 53 49 4e 47 20 66 74 73 BLE t4 USING fts +| 2976: 35 76 6f 63 61 62 28 27 74 32 27 2c 20 27 72 6f 5vocab('t2', 'ro +| 2992: 77 27 29 43 0c 06 17 11 11 08 75 74 61 62 6c 65 w')C......utable +| 3008: 74 33 74 33 43 52 45 41 54 45 20 56 49 52 54 55 t3t3CREATE VIRTU +| 3024: 41 4c 20 54 41 42 4c 45 20 74 33 20 55 53 49 4e AL TABLE t3 USIN +| 3040: 47 20 66 74 73 35 76 6f 63 61 62 28 27 74 31 27 G fts5vocab('t1' +| 3056: 2c 20 27 72 6f 77 27 29 56 0b 06 17 1f 1f 01 7d , 'row')V....... +| 3072: 74 61 62 6c 65 74 32 5f 63 6f 6e 66 69 67 74 32 tablet2_configt2 +| 3088: 5f 63 6f 6e 66 69 67 0a 43 52 45 41 54 45 20 54 _config.CREATE T +| 3104: 41 42 4c 45 20 27 74 32 5f 63 6f 6e 66 69 67 27 ABLE 't2_config' +| 3120: 28 6b 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 (k PRIMARY KEY, +| 3136: 76 29 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 v) WITHOUT ROWID +| 3152: 5e 0a 07 17 21 21 01 81 07 74 61 62 6c 65 74 32 ^...!!...tablet2 +| 3168: 5f 63 6f 6e 74 65 6e 74 74 32 5f 63 6f 6e 74 65 _contentt2_conte +| 3184: 6e 74 09 43 52 45 41 54 45 20 54 41 42 4c 45 20 nt.CREATE TABLE +| 3200: 27 74 32 5f 63 6f 6e 74 65 6e 74 27 28 69 64 20 't2_content'(id +| 3216: 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 INTEGER PRIMARY +| 3232: 4b 45 59 2c 20 63 30 2c 20 63 31 2c 20 63 32 29 KEY, c0, c1, c2) +| 3248: 69 09 07 17 19 19 01 81 2d 74 61 62 6c 65 74 32 i.......-tablet2 +| 3264: 5f 69 64 78 74 32 5f 69 64 78 08 43 52 45 41 54 _idxt2_idx.CREAT +| 3280: 45 20 54 41 42 4c 45 20 27 74 32 5f 69 64 78 27 E TABLE 't2_idx' +| 3296: 28 73 65 67 69 64 2c 20 74 65 72 6d 2c 20 70 67 (segid, term, pg +| 3312: 6e 6f 2c 20 50 52 49 4d 41 52 59 20 4b 45 59 28 no, PRIMARY KEY( +| 3328: 73 65 67 69 64 2c 20 74 65 72 6d 29 29 20 57 49 segid, term)) WI +| 3344: 54 48 4f 55 54 20 52 4f 57 49 44 55 08 07 17 1b THOUT ROWIDU.... +| 3360: 1b 01 81 01 74 61 62 6c 65 74 32 5f 64 61 74 61 ....tablet2_data +| 3376: 74 32 5f 64 61 74 61 07 43 52 45 41 54 45 20 54 t2_data.CREATE T +| 3392: 41 42 4c 45 20 27 74 32 5f 64 61 74 61 27 28 69 ABLE 't2_data'(i +| 3408: 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 d INTEGER PRIMAR +| 3424: 59 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 4c 4f Y KEY, block BLO +| 3440: 42 29 58 07 07 17 11 11 08 81 1d 74 61 62 6c 65 B)X........table +| 3456: 74 32 74 32 43 52 45 41 54 45 20 56 49 52 54 55 t2t2CREATE VIRTU +| 3472: 41 4c 20 54 41 42 4c 45 20 74 32 20 55 53 49 4e AL TABLE t2 USIN +| 3488: 47 20 66 74 73 35 28 27 61 27 2c 5b 62 5d 2c 22 G fts5('a',[b],. +| 3504: 63 22 2c 64 65 74 61 69 6c 3d 6e 6f 6e 65 2c 63 c.,detail=none,c +| 3520: 6f 6c 75 6d 6e 73 69 7a 65 3d 30 29 56 06 06 17 olumnsize=0)V... +| 3536: 1f 1f 01 7d 74 61 62 6c 65 74 31 5f 63 6f 6e 66 ....tablet1_conf +| 3552: 69 67 74 31 5f 63 6f 6e 66 69 67 06 43 52 45 41 igt1_config.CREA +| 3568: 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 6f 6e TE TABLE 't1_con +| 3584: 66 69 67 27 28 6b 20 50 52 49 4d 41 52 59 20 4b fig'(k PRIMARY K +| 3600: 45 59 2c 20 76 29 20 57 49 54 48 4f 55 54 20 52 EY, v) WITHOUT R +| 3616: 4f 57 49 44 5b 05 07 17 21 21 01 81 01 74 61 62 OWID[...!!...tab +| 3632: 6c 65 74 31 5f 64 6f 63 73 69 7a 65 74 31 5f 64 let1_docsizet1_d +| 3648: 6f 63 73 69 7a 65 05 43 52 45 41 54 45 20 54 41 ocsize.CREATE TA +| 3664: 42 4c 45 20 27 74 31 5f 64 6f 63 73 69 7a 65 27 BLE 't1_docsize' +| 3680: 28 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d (id INTEGER PRIM +| 3696: 41 52 59 20 4b 45 59 2c 20 73 7a 20 42 4c 4f 42 ARY KEY, sz BLOB +| 3712: 29 5e 04 07 17 21 21 01 81 07 74 61 62 6c 65 74 )^...!!...tablet +| 3728: 31 5f 63 6f 6e 74 65 6e 74 74 31 5f 63 6f 6e 74 1_contentt1_cont +| 3744: 65 6e 74 04 43 52 45 41 54 45 20 54 41 42 4c 45 ent.CREATE TABLE +| 3760: 20 27 74 31 5f 63 6f 6e 74 65 6e 74 27 28 69 64 't1_content'(id +| 3776: 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 INTEGER PRIMARY +| 3792: 20 4b 45 59 2c 20 63 30 2c 20 63 31 2c 20 63 32 KEY, c0, c1, c2 +| 3808: 29 69 03 07 17 19 19 01 81 2d 74 61 62 6c 65 74 )i.......-tablet +| 3824: 31 5f 69 64 78 74 31 5f 69 64 78 03 43 52 45 41 1_idxt1_idx.CREA +| 3840: 54 45 20 54 41 42 4c 45 20 27 74 31 5f 69 64 78 TE TABLE 't1_idx +| 3856: 27 28 73 65 67 69 64 2c 20 74 65 72 6d 2c 20 70 '(segid, term, p +| 3872: 67 6e 6f 2c 20 50 52 49 4d 41 52 59 20 4b 45 59 gno, PRIMARY KEY +| 3888: 28 73 65 67 69 64 2c 20 74 65 72 6d 29 29 20 57 (segid, term)) W +| 3904: 49 54 48 4f 55 54 20 52 4f 57 49 44 55 02 07 17 ITHOUT ROWIDU... +| 3920: 1b 1b 01 81 01 74 61 62 6c 65 74 31 5f 64 61 74 .....tablet1_dat +| 3936: 61 74 31 5f 64 61 74 61 02 43 52 45 41 54 45 20 at1_data.CREATE +| 3952: 54 41 42 4c 45 20 27 74 31 5f 64 61 74 61 27 28 TABLE 't1_data'( +| 3968: 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 id INTEGER PRIMA +| 3984: 52 59 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 4c RY KEY, block BL +| 4000: 4f 42 29 5b 01 07 17 11 11 08 81 23 74 61 62 6c OB)[.......#tabl +| 4016: 65 74 31 74 31 43 52 45 41 54 45 20 56 49 52 54 et1t1CREATE VIRT +| 4032: 55 41 4c 20 54 41 42 4c 45 20 74 31 20 55 53 49 UAL TABLE t1 USI +| 4048: 4e 47 20 66 74 73 35 28 61 2c 62 20 75 6e 69 6e NG fts5(a,b unin +| 4064: 64 65 78 65 64 2c 63 2c 74 6f 6b 65 6e 69 7a 65 dexed,c,tokenize +| 4080: 3d 22 70 6f 72 74 65 72 20 61 73 63 69 69 22 29 =.porter ascii.) +| page 2 offset 4096 +| 0: 0d 0f 68 00 05 0f 13 00 0f e6 0f 13 0f a8 0f 7c ..h............| +| 16: 0f 2a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 .*.............. +| 3856: 00 00 00 15 0a 03 00 30 00 00 00 00 01 03 03 00 .......0........ +| 3872: 03 01 01 01 02 01 01 03 01 01 37 8c 80 80 80 80 ..........7..... +| 3888: 01 03 00 74 00 00 00 2e 02 30 61 03 02 02 01 01 ...t.....0a..... +| 3904: 62 03 02 03 01 01 63 03 02 04 01 01 67 03 06 01 b.....c.....g... +| 3920: 02 02 01 01 68 03 06 01 02 03 01 01 69 03 06 01 ....h.......i... +| 3936: 02 04 04 06 06 06 08 08 0f ef 00 14 2a 00 00 00 ............*... +| 3952: 00 01 02 02 00 02 01 01 01 02 01 01 25 88 80 80 ............%... +| 3968: 80 80 01 03 00 50 00 00 00 1f 02 30 67 02 08 02 .....P.....0g... +| 3984: 01 02 02 01 01 68 02 08 03 01 02 03 01 01 69 02 .....h........i. +| 4000: 08 04 01 02 04 04 09 09 37 84 80 80 80 80 01 03 ........7....... +| 4016: 00 74 00 00 00 2e 02 30 61 01 02 02 01 01 62 01 .t.....0a.....b. +| 4032: 02 03 01 01 63 01 02 04 01 01 67 01 06 01 02 20 ....c.....g.... +| 4048: 10 16 80 10 60 10 20 30 10 16 90 10 60 10 20 40 ....`. 0....`. @ +| 4064: 40 60 60 60 80 80 70 10 30 01 40 30 90 00 90 00 @```..p.0.@0.... +| 4080: 00 01 12 40 00 00 00 00 10 10 10 00 10 10 10 10 ...@............ +| page 3 offset 8192 +| 0: a0 00 00 00 30 fe c0 00 ff a0 ff 30 fe 00 00 00 ....0......0.... +| 4064: 00 00 00 00 00 00 00 00 00 00 00 00 06 04 01 0c ................ +| 4080: 01 03 02 06 04 01 0c 01 02 02 05 04 09 0c 01 02 ................ +| page 4 offset 12288 +| 0: 0d 00 00 00 03 0f be 00 0f ea 0f d4 0f be 00 00 ................ +| 4016: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 14 03 ................ +| 4032: 05 00 17 17 17 61 20 62 20 63 67 20 68 20 69 67 .....a b cg h ig +| 4048: 20 68 20 69 14 02 05 00 17 17 17 67 20 68 20 69 h i.......g h i +| 4064: 61 20 62 20 63 67 20 68 20 69 14 01 05 00 17 17 a b cg h i...... +| 4080: 17 61 20 62 20 63 64 20 65 20 66 67 20 68 20 69 .a b cd e fg h i +| page 5 offset 16384 +| 0: 0d 00 00 00 03 0f e8 00 0f f8 0f f0 0f e8 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 06 03 03 00 12 03 00 03 ................ +| 4080: 06 02 03 00 12 03 00 03 06 01 03 00 12 03 00 03 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| page 7 offset 24576 +| 0: 0d 00 00 00 03 0f 9e 00 0f e6 0f ef 0f 9e 00 00 ................ +| 3984: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 41 84 ..............A. +| 4000: 80 80 80 80 01 04 00 81 06 00 00 00 34 02 30 61 ............4.0a +| 4016: 01 01 01 01 01 62 01 01 01 01 01 63 01 01 01 01 .....b.....c.... +| 4032: 01 64 01 01 01 65 01 01 01 66 01 01 01 01 01 68 .d...e...f.....h +| 4048: 01 01 01 01 01 69 01 01 01 04 06 06 06 04 04 04 .....i.......... +| 4064: 06 06 07 01 03 00 14 03 09 09 09 0f 0a 03 00 24 ...............$ +| 4080: 00 00 00 00 01 01 01 00 01 01 01 01 0a 00 00 00 ................ +| page 8 offset 28672 +| 0: 01 0f fa 00 0f fa 00 00 00 00 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 05 04 09 0c 01 02 ................ +| page 9 offset 32768 +| 0: 0d 00 00 00 03 0f be 00 0f ea 0f d4 1f be 00 00 ................ +| 4016: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 14 03 ................ +| 4032: 05 00 17 17 17 61 20 62 20 63 67 20 68 20 69 67 .....a b cg h ig +| 4048: 20 68 20 69 14 02 05 00 17 17 17 67 20 68 20 69 h i.......g h i +| 4064: 61 20 62 20 63 67 20 68 20 69 14 01 05 00 17 17 a b cg h i...... +| 4080: 17 61 20 62 20 63 64 20 65 20 66 67 20 68 20 69 .a b cd e fg h i +| page 10 offset 36864 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| end crash-e2d47e0624a42c.db +}]} {} + +do_catchsql_test 32.1 { + SELECT snippet(t1, -1, '.', '..', '[', ']'), + highlight(t1, 2, '[', ']') + FROM t1('g + h') + WHERE rank MATCH 'bm25(1.0, 1.0)' ORDER BY rank; +} {/.*fts5: corrupt.*/} + +do_catchsql_test 32.2 { + SELECT * FROM t3; +} {1 {database disk image is malformed}} + +do_catchsql_test 32.3 { + SELECT * FROM t4; +} {1 {database disk image is malformed}} + +do_catchsql_test 32.4 { + SELECT fts5_decode(id, block) FROM t1_data; +} {1 {database disk image is malformed}} + +do_catchsql_test 32.5 { + SELECT fts5_decode(id, block) FROM t2_data; +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +reset_db +do_test 33.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 28672 pagesize 4096 filename crash-fed6e90021ba5d.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 07 .....@ ........ +| 32: 00 00 00 02 00 00 00 01 00 00 00 08 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 96: 00 00 00 00 0d 0f c7 00 06 0d b6 00 0f 8d 0f 36 ...............6 +| 112: 0e cb 0e 6b 0e 0e 0d b6 0d b6 00 00 00 00 00 00 ...k............ +| 3504: 00 00 00 00 00 00 56 07 06 17 1f 1f 01 7d 74 61 ......V.......ta +| 3520: 62 6c 65 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 blet1_configt1_c +| 3536: 6f 6e 66 69 67 07 43 52 45 41 54 45 20 54 41 42 onfig.CREATE TAB +| 3552: 4c 45 20 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b LE 't1_config'(k +| 3568: 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 PRIMARY KEY, v) +| 3584: 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 5b 06 WITHOUT ROWID[. +| 3600: 07 17 21 21 01 81 01 74 61 62 6c 65 74 31 5f 64 ..!!...tablet1_d +| 3616: 6f 63 73 69 8a 65 74 31 5f 64 6f 63 73 69 7a 65 ocsi.et1_docsize +| 3632: 06 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 .CREATE TABLE 't +| 3648: 31 5f 64 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 1_docsize'(id IN +| 3664: 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE +| 3680: 59 2c 20 73 7a 20 42 4c 4f 42 29 5e 05 07 17 21 Y, sz BLOB)^...! +| 3696: 21 01 81 07 74 61 62 6c 65 74 31 5f 63 6f 6e 74 !...tablet1_cont +| 3712: 65 6e 74 74 31 5f 63 6f 6e 74 65 6e 74 05 43 52 entt1_content.CR +| 3728: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 EATE TABLE 't1_c +| 3744: 6f 6e 74 65 6e 74 27 28 69 64 20 49 4e 54 45 47 ontent'(id INTEG +| 3760: 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 ER PRIMARY KEY, +| 3776: 63 30 2c 20 63 31 2c 20 63 32 29 69 04 07 17 19 c0, c1, c2)i.... +| 3792: 19 01 81 2d 74 61 62 6c 65 74 31 5f 69 64 78 74 ...-tablet1_idxt +| 3808: 31 5f 69 64 78 04 43 52 45 41 54 45 20 54 41 42 1_idx.CREATE TAB +| 3824: 4c 45 20 27 74 31 5f 69 64 78 27 28 73 65 67 69 LE 't1_idx'(segi +| 3840: 64 2c 20 74 65 72 6d 2c 20 70 67 6e 6f 2c 20 50 d, term, pgno, P +| 3856: 52 49 4d 41 52 59 20 4b 45 59 28 73 65 67 69 64 RIMARY KEY(segid +| 3872: 2c 20 74 65 72 6d 29 29 20 57 49 54 48 4f 55 54 , term)) WITHOUT +| 3888: 20 52 4f 57 49 44 55 03 07 17 1b 1b 01 81 01 74 ROWIDU........t +| 3904: 61 62 6c 65 74 31 5f 64 61 74 61 74 31 5f 64 61 ablet1_datat1_da +| 3920: 74 61 03 43 52 45 41 54 45 20 54 41 42 4c 45 20 ta.CREATE TABLE +| 3936: 27 74 31 5f 64 61 74 61 27 28 69 64 20 49 4e 54 't1_data'(id INT +| 3952: 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 EGER PRIMARY KEY +| 3968: 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 29 38 02 06 , block BLOB)8.. +| 3984: 17 11 11 08 5f 74 61 62 6c 65 74 31 74 31 43 52 ...._tablet1t1CR +| 4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 35 LE t1 USING fts5 +| 4032: 28 61 2c 62 2c 63 29 00 00 00 39 00 00 00 00 00 (a,b,c)...9..... +| page 3 offset 8192 +| 0: 0d 00 00 00 03 0c af 00 0f e6 0f ef 0c af 00 00 ................ +| 3232: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 86 ................ +| 3248: 2f 84 80 80 80 80 01 04 00 8c 62 00 00 03 12 02 /.........b..... +| 3264: 30 30 01 04 05 03 01 04 05 03 01 04 05 03 1f 02 00.............. +| 3280: 03 01 02 03 01 02 03 01 08 35 30 30 30 30 30 30 .........5000000 +| 3296: 30 1c 02 04 01 0e ee ca ec ea ea ab e4 f5 ca b1 0............... +| 3312: ac ee ec de ef 3e ee ca ee ec f2 f8 0f f0 0f e8 .....>.......... +| 3328: 0f e0 0f d8 0f d0 0f c8 0f c0 0f b8 0f b0 0f a8 ................ +| 3344: 0f a0 0f 98 0f 90 0f 88 0f 80 0f 78 0f 70 0f 68 ...........x.p.h +| 3360: 0f 60 0f 58 0f 50 0f 48 0f 40 0f 38 0f 30 0f 28 .`.X.P.H.@.8.0.( +| 3376: 0f 20 0f 18 0f 10 0f 08 0f 00 0e f8 0e f0 0e e8 . .............. +| 3392: 0e e0 02 03 01 02 03 01 02 03 02 07 6f 6d 70 69 ............ompi +| 3408: 6c 65 72 01 02 02 01 02 02 01 02 02 01 06 64 62 ler...........db +| 3424: 73 74 61 74 07 02 03 01 02 03 01 02 03 02 04 65 stat...........e +| 3440: 62 75 67 04 02 02 01 02 02 01 02 02 01 06 65 6e bug...........en +| 3456: 61 62 6c 65 07 02 02 01 02 02 01 02 02 01 02 02 able............ +| 3472: 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 ................ +| 3488: 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 ................ +| 3504: 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 ................ +| 3520: 01 02 02 02 08 78 74 65 6e 73 69 6f 6e 1f 02 04 .....xtension... +| 3536: 01 02 04 01 02 04 01 04 66 74 73 34 0a 02 03 03 ........fts4.... +| 3552: 01 02 01 02 03 04 01 35 0d 02 03 01 02 03 01 02 .......5........ +| 3568: 03 01 07 67 65 6f 70 6f 6c 79 10 02 03 01 02 03 ...geopoly...... +| 3584: 01 02 03 01 05 6a 73 6f 6e 31 13 02 03 01 02 03 .....json1...... +| 3600: 01 02 03 01 04 6c 6f 61 64 1f 02 03 01 02 03 01 .....load....... +| 3616: 02 03 01 03 6d 61 78 1c 02 02 01 02 02 01 02 02 ....max......... +| 3632: 02 05 65 6d 6f 72 79 1c 02 03 01 02 03 01 02 03 ..emory......... +| 3648: 04 04 73 79 73 35 16 02 03 01 02 03 01 02 03 01 ..sys5.......... +| 3664: 06 6e 6f 63 61 73 65 02 06 01 02 02 03 06 01 02 .nocase......... +| 3680: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 ................ +| 3696: 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 03 ................ +| 3712: 06 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 ................ +| 3728: 01 02 02 01 04 6f 6d 69 74 1f 02 02 01 02 02 01 .....omit....... +| 3744: 02 02 01 05 72 74 72 65 65 19 02 03 01 02 03 01 ....rtree....... +| 3760: 02 03 04 02 69 6d 01 06 01 02 02 03 06 01 02 02 ....im.......... +| 3776: 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 03 ................ +| 3792: 06 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 ................ +| 3808: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3824: 02 02 01 0a 74 68 72 65 61 64 73 61 66 65 22 02 ....threadsafe.. +| 3840: 02 01 02 02 01 02 02 01 04 76 74 61 62 07 02 04 .........vtab... +| 3856: 01 02 04 01 02 04 01 01 78 01 06 01 01 02 01 06 ........x....... +| 3872: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 ................ +| 3888: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 ................ +| 3904: 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 ................ +| 3920: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 3936: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 3952: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 ................ +| 3968: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 ................ +| 3984: 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 ................ +| 4000: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 4016: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 4032: 01 01 02 01 06 01 01 02 01 06 01 01 02 04 18 13 ................ +| 4048: 0c 44 10 12 11 0f 47 13 0f 0c 12 10 0f 0e 10 0f .D....G......... +| 4064: 44 0f 10 40 15 0f 07 01 03 00 14 24 57 24 24 0f D..@.......$W$$. +| 4080: 0a 03 00 24 00 00 00 00 01 01 01 00 01 01 01 01 ...$............ +| page 4 offset 12288 +| 0: 0a 00 00 00 01 0f fa 00 0f fa 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 05 04 09 0c 01 02 ................ +| page 5 offset 16384 +| 0: 0d 00 00 00 24 0c 1f 00 0f df 0f bd 0f 9b 0f 89 ....$........... +| 16: 0f 76 0f 63 0f 44 0f 24 0f 04 0e ec 0e d3 0e ba .v.c.D.$........ +| 32: 0e a2 0e 89 0e 70 0e 55 0e 39 0e 1d 0e 04 0d ea .....p.U.9...... +| 48: 0d d0 0d b5 0d 99 0d 7d 0d 64 0d 4a 0d 30 0d 10 .........d.J.0.. +| 64: 0c ef 0c ce 0c ae 54 d5 35 95 33 55 84 24 94 e4 ......T.5.3U.$.. +| 80: 15 25 91 a1 70 50 02 90 f1 94 54 e4 14 24 c4 52 .%..pP....T..$.R +| 96: 04 d4 54 d5 35 95 33 55 84 e4 f4 34 15 34 51 91 ..T.5.3U...4.4Q. +| 112: 60 50 02 90 f1 74 54 e4 14 24 c4 52 04 d4 54 d5 `P...tT..$.R..T. +| 128: 35 95 33 55 85 25 45 24 94 d1 81 50 50 02 50 f1 5.3U.%E$...PP.P. +| 144: 94 54 e4 14 24 c4 52 04 a5 34 f4 e3 15 84 24 94 .T..$.R..4....$. +| 160: e4 15 25 91 81 40 50 02 50 f1 94 54 e4 14 24 c4 ..%..@P.P..T..$. +| 176: 52 04 a5 34 f4 e3 15 84 e4 f4 34 15 34 51 71 30 R..4......4.4Qq0 +| 192: 50 02 50 f1 74 54 e4 14 24 c4 52 04 ae 4f 41 33 P.P.tT..$.R..OA3 +| 208: 55 85 25 45 24 94 d1 a1 20 50 02 90 f1 94 54 e4 U.%E$... P....T. +| 224: 14 24 c4 52 04 74 54 f5 04 f4 c5 95 84 24 94 e4 .$.R.tT......$.. +| 240: 15 25 91 a1 10 50 02 90 f1 94 54 e4 14 24 c4 52 .%...P....T..$.R +| 256: 04 74 54 f5 04 f4 c5 95 84 e4 f4 34 15 34 51 91 .tT........4.4Q. +| 272: 00 50 02 90 f1 74 54 e4 14 24 c4 52 04 74 54 f5 .P...tT..$.R.tT. +| 288: 04 f4 c5 95 85 25 45 24 94 d1 70 f0 50 02 30 f1 .....%E$..p.P.0. +| 304: 94 54 e4 14 24 c4 52 04 65 45 33 55 84 24 94 e4 .T..$.R.eE3U.$.. +| 320: 15 25 91 70 e0 50 02 30 f1 94 54 e4 40 0f 38 0f .%.p.P.0..T.@.8. +| 336: 30 0f 28 0f 20 0f 18 0f 10 0f 08 0f 00 0e f8 0e 0.(. ........... +| 352: f0 0e e8 0e e0 00 00 00 00 00 00 00 00 00 00 00 ................ +| page 6 offset 20480 +| 3808: 06 24 03 00 12 02 01 01 06 23 03 00 12 02 01 01 .$.......#...... +| 3824: 06 22 03 00 12 02 01 01 06 21 03 00 12 03 01 01 .........!...... +| 3840: 06 20 03 00 12 03 01 01 06 1f 03 00 12 03 01 01 . .............. +| 3856: 06 1e 03 00 12 03 01 01 06 1d 03 00 12 03 01 01 ................ +| 3872: 06 1c 03 00 12 03 01 01 06 1b 03 00 12 02 01 01 ................ +| 3888: 06 1a 03 00 12 02 01 01 06 19 03 00 12 02 01 01 ................ +| 3904: 06 18 03 00 12 02 01 01 06 17 03 00 12 02 01 01 ................ +| 3920: 06 16 03 00 12 02 01 01 06 15 03 00 12 02 01 01 ................ +| 3936: 06 14 03 00 12 02 01 01 06 13 03 00 12 02 01 01 ................ +| 3952: 06 12 03 00 12 02 01 01 06 11 03 00 12 02 01 01 ................ +| 3968: 06 10 03 00 12 02 01 01 06 0f 03 00 12 02 01 01 ................ +| 3984: 06 0e 03 00 12 02 01 01 06 0d 03 00 12 02 01 01 ................ +| 4000: 06 0c 03 00 12 02 01 01 06 0b 03 00 12 02 01 01 ................ +| 4016: 06 0a 03 00 12 02 01 01 06 09 03 00 12 03 01 01 ................ +| 4032: 06 08 03 00 12 03 01 01 06 07 03 00 12 03 01 01 ................ +| 4048: 06 06 03 00 12 01 01 01 06 05 03 00 12 01 01 01 ................ +| 4064: 06 04 03 00 12 01 01 01 06 03 03 00 12 05 01 01 ................ +| 4080: 06 02 03 00 12 05 01 01 06 01 03 00 12 05 01 01 ................ +| page 7 offset 24576 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 52 59 11 05 05 00 ..........RY.... +| 16: 17 0f 19 44 45 42 55 47 58 4e 4f 43 41 53 45 10 ...DEBUGXNOCASE. +| 32: 04 05 00 17 0f 17 44 45 42 55 47 58 52 54 52 49 ......DEBUGXRTRI +| 48: 4d 20 03 05 00 35 0f 19 43 4f 4d 50 49 4c 45 52 M ...5..COMPILER +| 64: 3d 63 6c 61 6e 67 2d 36 2e 30 2e 30 58 42 49 4e =clang-6.0.0XBIN +| 80: 41 52 59 20 02 05 00 35 0f 19 43 4f 4d 50 49 4c ARY ...5..COMPIL +| 96: 45 52 3d 63 6c 61 6e 67 2d 36 2e 30 2e 30 58 4e ER=clang-6.0.0XN +| 112: 4f 43 41 53 45 1f 01 05 00 35 0f 17 43 4f 4d 50 OCASE....5..COMP +| 128: 49 4c 45 52 3d 63 6c 61 6e 67 2d 36 2e 30 2e 30 ILER=clang-6.0.0 +| 144: 58 52 54 52 49 4d 0d 00 00 00 24 0e e0 00 0f 6f XRTRIM....$....o +| 160: 6e 74 65 6e 74 05 43 52 45 41 54 45 20 54 41 42 ntent.CREATE TAB +| 176: 4c 45 20 27 74 31 5f 63 6f 6e 74 65 6e 74 27 28 LE 't1_content'( +| 192: 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 id INTEGER PRIMA +| 208: 52 59 20 4b 45 59 2c 20 63 30 2c 20 63 31 2c 20 RY KEY, c0, c1, +| 224: 63 32 29 69 04 07 17 19 19 01 81 2d 74 61 62 6c c2)i.......-tabl +| 240: 65 74 31 5f 69 64 78 74 31 5f 69 64 78 04 43 52 et1_idxt1_idx.CR +| 256: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 69 EATE TABLE 't1_i +| 272: 64 78 27 28 73 65 67 69 64 2c 20 74 65 72 6d 2c dx'(segid, term, +| 288: 20 70 67 6e 6f 2c 20 50 52 49 4d 41 52 59 20 4b pgno, PRIMARY K +| 304: 45 59 28 73 65 67 69 64 2c 20 74 65 72 6d 29 29 EY(segid, term)) +| 320: 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 55 03 WITHOUT ROWIDU. +| 336: 07 17 1b 1b 01 81 01 74 61 62 6c 65 74 31 5f 64 .......tablet1_d +| 352: 61 74 61 74 31 5f 64 61 74 61 03 43 52 45 41 54 atat1_data.CREAT +| 368: 45 20 54 41 42 4c 45 20 27 74 31 5f 64 61 74 61 E TABLE 't1_data +| 384: 27 28 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 '(id INTEGER PRI +| 400: 4d 41 52 59 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 MARY KEY, block +| 416: 42 4c 4f 42 29 38 02 06 17 11 11 08 5f 74 61 62 BLOB)8......_tab +| 432: 6c 65 74 31 74 31 43 52 45 41 54 45 20 56 49 52 let1t1CREATE VIR +| 448: 54 55 41 4c 20 54 41 42 4c 45 20 74 31 20 55 53 TUAL TABLE t1 US +| 464: 49 4e 47 20 66 74 73 35 28 61 2c 62 2c 63 29 00 ING fts5(a,b,c). +| 480: 00 00 39 00 00 00 00 00 00 00 00 00 00 00 00 00 ..9............. +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| end crash-fed6e90021ba5d.db +}]} {} + +do_catchsql_test 33.1 { + CREATE VIRTUAL TABLE t2 USING fts5vocab('t1','row'); + CREATE VIRTUAL TABLE t3 USING fts5vocab('t1','col'); + CREATE VIRTUAL TABLE t4 USING fts5vocab('t1','instance'); +} {/*malformed database schema*/} + +do_catchsql_test 33.2 { + SELECT * FROM t2; +} {/*malformed database schema*/} + +do_catchsql_test 33.3 { + SELECT * FROM t2, t3, t4 WHERE t2.term=t3.term AND t3.term=t4.term; +} {/*malformed database schema*/} + +#------------------------------------------------------------------------- +reset_db +do_test 34.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 40960 pagesize 4096 filename crash-a60a9da4c8932f.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 0a .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 0d 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 96: 00 00 00 00 0d 00 00 00 0d 0b 6e 00 0f a3 0f 4c ..........n....L +| 112: 0e e1 0e 81 0e 24 0d cc 0d 72 0d 1b 0c b0 0c 50 .....$...r.....P +| 128: 0b f8 0b b3 0b 6e 00 00 00 00 00 00 00 00 00 00 .....n.......... +| 2912: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 43 0d ..............C. +| 2928: 06 17 11 11 08 75 74 61 62 6c 65 74 34 74 34 43 .....utablet4t4C +| 2944: 52 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 REATE VIRTUAL TA +| 2960: 42 4c 45 20 74 34 20 55 53 49 4e 47 20 66 74 73 BLE t4 USING fts +| 2976: 35 76 6f 63 61 62 28 27 74 32 27 2c 20 27 72 6f 5vocab('t2', 'ro +| 2992: 77 27 29 43 0c 06 17 11 11 08 75 74 61 62 6c 65 w')C......utable +| 3008: 74 33 74 33 43 52 45 41 54 45 20 56 49 52 54 55 t3t3CREATE VIRTU +| 3024: 41 4c 20 54 41 42 4c 45 20 74 33 20 55 53 49 4e AL TABLE t3 USIN +| 3040: 47 20 66 74 73 35 76 6f 63 61 62 28 27 74 31 27 G fts5vocab('t1' +| 3056: 2c 20 27 72 6f 77 27 29 56 0b 06 17 1f 1f 01 7d , 'row')V....... +| 3072: 74 61 62 6c 65 74 32 5f 63 6f 6e 66 69 67 74 32 tablet2_configt2 +| 3088: 5f 63 6f 6e 66 69 67 0a 43 52 45 41 54 45 20 54 _config.CREATE T +| 3104: 41 42 4c 45 20 27 74 32 5f 63 6f 6e 66 69 67 27 ABLE 't2_config' +| 3120: 28 6b 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 (k PRIMARY KEY, +| 3136: 76 29 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 v) WITHOUT ROWID +| 3152: 5e 0a 07 17 21 21 01 81 07 74 61 62 6c 65 74 32 ^...!!...tablet2 +| 3168: 5f 63 6f 6e 74 65 6e 74 74 32 5f 63 6f 6e 74 65 _contentt2_conte +| 3184: 6e 74 09 43 52 45 41 54 45 20 54 41 42 4c 45 20 nt.CREATE TABLE +| 3200: 27 74 32 5f 63 6f 6e 74 65 6e 74 27 28 69 64 20 't2_content'(id +| 3216: 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 INTEGER PRIMARY +| 3232: 4b 45 59 2c 20 63 30 2c 20 63 31 2c 20 63 32 29 KEY, c0, c1, c2) +| 3248: 69 09 07 17 19 19 01 81 2d 74 61 62 6c 65 74 32 i.......-tablet2 +| 3264: 5f 69 64 78 74 32 5f 69 64 78 08 43 52 45 41 54 _idxt2_idx.CREAT +| 3280: 45 20 54 41 42 4c 45 20 27 74 32 5f 69 64 78 27 E TABLE 't2_idx' +| 3296: 28 73 65 67 69 64 2c 20 74 65 72 6d 2c 20 70 67 (segid, term, pg +| 3312: 6e 6f 2c 20 50 52 49 4d 41 52 59 20 4b 45 59 28 no, PRIMARY KEY( +| 3328: 73 65 67 69 64 2c 20 74 65 72 6d 29 29 20 57 49 segid, term)) WI +| 3344: 54 48 4f 55 54 20 52 4f 57 49 44 55 08 07 17 1b THOUT ROWIDU.... +| 3360: 1b 01 81 01 74 61 62 6c 65 74 32 5f 64 61 74 61 ....tablet2_data +| 3376: 74 32 5f 64 61 74 61 07 43 52 45 41 54 45 20 54 t2_data.CREATE T +| 3392: 41 42 4c 45 20 27 74 32 5f 64 61 74 61 27 28 69 ABLE 't2_data'(i +| 3408: 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 d INTEGER PRIMAR +| 3424: 59 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 4c 4f Y KEY, block BLO +| 3440: 42 29 58 07 07 17 11 11 08 81 1d 74 61 62 6c 65 B)X........table +| 3456: 74 32 74 32 43 52 45 41 54 45 20 56 49 52 54 55 t2t2CREATE VIRTU +| 3472: 41 4c 20 54 41 42 4c 45 20 74 32 20 55 53 49 4e AL TABLE t2 USIN +| 3488: 47 20 66 74 73 35 28 27 61 27 2c 5b 62 5d 2c 22 G fts5('a',[b],. +| 3504: 63 22 2c 64 65 74 61 69 6c 3d 6e 6f 6e 65 2c 63 c.,detail=none,c +| 3520: 6f 6c 75 6d 6e 73 69 7a 65 3d 30 29 56 06 06 17 olumnsize=0)V... +| 3536: 1f 1f 01 7d 74 61 62 6c 65 74 31 5f 63 6f 6e 66 ....tablet1_conf +| 3552: 69 67 74 31 5f 63 6f 6e 66 69 67 06 43 52 45 41 igt1_config.CREA +| 3568: 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 6f 6e TE TABLE 't1_con +| 3584: 66 69 67 27 28 6b 20 50 52 49 4d 41 52 59 20 4b fig'(k PRIMARY K +| 3600: 45 59 2c 20 76 29 20 57 49 54 48 4f 55 54 20 52 EY, v) WITHOUT R +| 3616: 4f 57 49 44 5b 05 07 17 21 21 01 81 01 74 61 62 OWID[...!!...tab +| 3632: 6c 65 74 31 5f 64 6f 63 73 69 7a 65 74 31 5f 64 let1_docsizet1_d +| 3648: 6f 63 73 69 7a 65 05 43 52 45 41 54 45 20 54 41 ocsize.CREATE TA +| 3664: 42 4c 45 20 27 74 31 5f 64 6f 63 73 69 7a 65 27 BLE 't1_docsize' +| 3680: 28 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d (id INTEGER PRIM +| 3696: 41 52 59 20 4b 45 59 2c 20 73 7a 20 42 4c 4f 42 ARY KEY, sz BLOB +| 3712: 29 5e 04 07 17 21 21 01 81 07 74 61 62 6c 65 74 )^...!!...tablet +| 3728: 31 5f 63 6f 6e 74 65 6e 74 74 31 5f 63 6f 6e 74 1_contentt1_cont +| 3744: 65 6e 74 04 43 52 45 41 54 45 20 54 41 42 4c 45 ent.CREATE TABLE +| 3760: 20 27 74 31 5f 63 6f 6e 74 65 6e 74 27 28 69 64 't1_content'(id +| 3776: 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 INTEGER PRIMARY +| 3792: 20 4b 45 59 2c 20 63 30 2c 20 63 31 2c 20 63 32 KEY, c0, c1, c2 +| 3808: 29 69 03 07 17 19 19 01 81 2d 74 61 62 6c 65 74 )i.......-tablet +| 3824: 31 5f 69 64 78 74 31 5f 69 64 78 03 43 52 45 41 1_idxt1_idx.CREA +| 3840: 54 45 20 54 41 42 4c 45 20 27 74 31 5f 69 64 78 TE TABLE 't1_idx +| 3856: 27 28 73 65 67 69 64 2c 20 74 65 72 6d 2c 20 70 '(segid, term, p +| 3872: 67 6e 6f 2c 20 50 52 49 4d 41 52 59 20 4b 45 59 gno, PRIMARY KEY +| 3888: 28 73 65 67 69 64 2c 20 74 65 72 6d 29 29 20 57 (segid, term)) W +| 3904: 49 54 48 4f 55 54 20 52 4f 57 49 44 55 02 07 17 ITHOUT ROWIDU... +| 3920: 1b 1b 01 81 01 74 61 62 6c 65 74 31 5f 64 61 74 .....tablet1_dat +| 3936: 61 74 31 5f 64 61 74 61 02 43 52 45 41 54 45 20 at1_data.CREATE +| 3952: 54 41 42 4c 45 20 27 74 31 5f 64 61 74 61 27 28 TABLE 't1_data'( +| 3968: 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 id INTEGER PRIMA +| 3984: 52 59 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 4c RY KEY, block BL +| 4000: 4f 42 29 5b 01 07 17 11 11 08 81 23 74 61 62 6c OB)[.......#tabl +| 4016: 65 74 31 74 31 43 52 45 41 54 45 20 56 49 52 54 et1t1CREATE VIRT +| 4032: 55 41 4c 20 54 41 42 4c 45 20 74 31 20 55 53 49 UAL TABLE t1 USI +| 4048: 4e 47 20 66 74 73 35 28 61 2c 62 20 75 6e 69 6e NG fts5(a,b unin +| 4064: 64 65 78 65 64 2c 63 2c 74 6f 6b 65 6e 69 7a 65 dexed,c,tokenize +| 4080: 3d 22 70 6f 72 74 65 72 20 61 73 63 69 69 22 29 =.porter ascii.) +| page 2 offset 4096 +| 0: 0d 0f 68 00 05 0f 13 00 0f e6 0f 13 0f a8 0f 7c ..h............| +| 16: 0f 2a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 .*.............. +| 3856: 00 00 00 15 0a 03 00 30 00 00 00 00 01 03 03 00 .......0........ +| 3872: 03 01 01 01 02 01 01 03 01 01 37 8c 80 80 80 80 ..........7..... +| 3888: 01 03 00 74 00 20 68 20 69 0d 00 00 00 03 0f e8 ...t. h i....... +| 3904: 00 0f f8 0f f0 0f e8 00 00 00 00 00 00 00 00 00 ................ +| page 5 offset 16384 +| 4064: 00 00 00 00 00 00 00 00 06 03 03 00 12 03 00 00 ................ +| 4080: 60 20 30 01 20 30 00 30 60 10 30 01 20 30 00 30 ` 0. 0.0`.0. 0.0 +| page 6 offset 20480 +| 0: a0 00 00 00 10 ff 40 00 ff 00 00 00 00 00 00 00 ......@......... +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| page 7 offset 24576 +| 0: 0d 00 00 00 03 0f 9e 00 0f e6 0f ef 0f 9e 00 00 ................ +| 3984: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 41 84 ..............A. +| 4000: 80 80 80 80 01 04 00 81 06 00 00 00 34 02 30 61 ............4.0a +| 4016: 01 01 00 00 00 00 00 00 00 00 00 11 87 89 06 26 ...............& +| 4032: 01 64 01 01 01 65 01 01 01 66 01 01 01 67 01 01 .d...e...f...g.. +| 4048: 01 01 01 68 01 01 01 01 01 69 01 01 01 04 06 06 ...h.....i...... +| 4064: 06 04 44 00 06 06 07 01 03 00 14 03 09 09 09 0f ..D............. +| 4080: 0a 03 00 24 00 00 00 00 01 01 01 00 01 01 01 01 ...$............ +| page 8 offset 28672 +| 0: 0a 00 00 00 01 0f fa 00 0f fa 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 05 04 09 0c 01 02 ................ +| page 9 offset 32768 +| 0: 0d 00 00 00 03 0f be 00 0f ea 0f d4 0f be 00 00 ................ +| 4016: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 14 03 ................ +| 4032: 05 00 17 17 17 61 20 62 20 63 67 20 68 20 69 67 .....a b cg h ig +| 4048: 20 68 20 69 14 02 05 00 17 17 17 67 20 68 20 69 h i.......g h i +| 4064: 61 20 62 20 63 67 20 68 20 69 14 01 05 00 17 17 a b cg h i...... +| 4080: 17 61 20 62 20 63 64 20 65 20 66 67 20 68 20 69 .a b cd e fg h i +| page 10 offset 36864 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| end crash-a60a9da4c8932f.db +}]} {} + +do_catchsql_test 34.1 { + SELECT fts5_decode(id, block) FROM t1_data; +} {1 {database disk image is malformed}} + +do_catchsql_test 34.2 { + SELECT fts5_decode(id, block) FROM t2_data; +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +reset_db +do_test 35.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 32768 pagesize 4096 filename crash-ae135cb10977c7.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 08 .....@ ........ +| 32: 00 00 00 02 00 00 00 01 00 00 00 09 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 96: 00 00 00 00 0d 0f c7 00 07 0d 92 00 0f 8d 0f 36 ...............6 +| 112: 0e cb 0e 6b 0e 0e 0d b6 0d 92 0d 92 00 00 00 00 ...k............ +| 3472: 00 00 22 08 06 17 11 11 01 31 74 61 62 6c 65 74 .........1tablet +| 3488: 32 74 32 08 43 52 45 41 54 45 20 54 41 42 4c 45 2t2.CREATE TABLE +| 3504: 20 74 32 28 78 29 56 07 06 17 1f 1f 01 7d 74 61 t2(x)V.......ta +| 3520: 62 6c 65 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 blet1_configt1_c +| 3536: 6f 6e 66 69 67 07 43 52 45 41 54 45 20 54 41 42 onfig.CREATE TAB +| 3552: 4c 45 20 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b LE 't1_config'(k +| 3568: 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 PRIMARY KEY, v) +| 3584: 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 5b 06 WITHOUT ROWID[. +| 3600: 07 17 21 21 01 81 01 74 61 62 6c 65 74 31 5f 64 ..!!...tablet1_d +| 3616: 6f 63 73 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 ocsizet1_docsize +| 3632: 06 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 .CREATE TABLE 't +| 3648: 31 5f 64 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 1_docsize'(id IN +| 3664: 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE +| 3680: 59 2c 20 73 7a 20 42 4c 4f 42 29 5e 05 07 17 21 Y, sz BLOB)^...! +| 3696: 21 01 81 07 74 61 62 6c 65 74 31 5f 63 6f 6e 74 !...tablet1_cont +| 3712: 65 6e 74 74 31 5f 63 6f 6e 74 65 6e 74 05 43 52 entt1_content.CR +| 3728: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 EATE TABLE 't1_c +| 3744: 6f 6e 74 65 6e 74 27 28 69 64 20 49 4e 54 45 47 ontent'(id INTEG +| 3760: 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 ER PRIMARY KEY, +| 3776: 63 30 2c 20 63 31 2c 20 63 32 29 69 04 07 17 19 c0, c1, c2)i.... +| 3792: 19 01 81 2d 74 61 62 6c 65 74 31 5f 69 64 78 74 ...-tablet1_idxt +| 3808: 31 5f 69 64 78 04 43 52 45 41 54 45 20 54 41 42 1_idx.CREATE TAB +| 3824: 4c 45 20 27 74 31 5f 69 64 78 27 28 73 65 67 69 LE 't1_idx'(segi +| 3840: 64 2c 20 74 65 72 6d 2c 20 70 67 6e 6f 2c 20 50 d, term, pgno, P +| 3856: 52 49 4d 41 52 59 20 4b 45 59 28 73 65 67 69 64 RIMARY KEY(segid +| 3872: 2c 20 74 65 72 6d 29 29 20 57 49 54 48 4f 55 54 , term)) WITHOUT +| 3888: 20 52 4f 57 49 44 55 03 07 17 1b 1b 01 81 01 74 ROWIDU........t +| 3904: 61 62 6c 65 74 31 5f 64 61 74 61 74 31 5f 64 61 ablet1_datat1_da +| 3920: 74 61 03 43 52 45 41 54 45 20 54 41 42 4c 45 20 ta.CREATE TABLE +| 3936: 27 74 31 5f 64 61 74 61 27 28 69 64 20 49 4e 54 't1_data'(id INT +| 3952: 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 EGER PRIMARY KEY +| 3968: 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 29 38 02 06 , block BLOB)8.. +| 3984: 17 11 11 08 5f 74 61 62 6c 65 74 31 74 31 43 52 ...._tablet1t1CR +| 4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 35 LE t1 USING fts5 +| 4032: 28 61 2c 62 2c 63 29 00 00 00 39 00 00 00 00 00 (a,b,c)...9..... +| page 3 offset 8192 +| 0: 0d 00 00 00 03 0c 94 00 0f e6 0f ef 0c 94 00 00 ................ +| 3216: 00 00 00 00 86 4a 84 80 80 80 80 01 04 00 8d 18 .....J.......... +| 3232: 00 00 03 2b 02 30 30 01 02 06 01 02 06 01 02 06 ...+.00......... +| 3248: 1f 02 03 01 02 03 01 02 03 01 08 32 30 31 36 30 ...........20160 +| 3264: 36 30 39 01 02 07 01 02 07 01 02 07 01 01 34 01 609...........4. +| 3280: 02 05 01 02 05 01 02 05 01 01 35 01 02 04 01 02 ..........5..... +| 3296: 04 01 02 04 02 07 30 30 30 30 30 30 30 1c 02 3d ......0000000..= +| 3312: 01 02 04 01 02 04 01 06 62 69 6e 61 72 79 03 06 ........binary.. +| 3328: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3344: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3360: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 ................ +| 3376: 03 06 01 02 02 03 06 01 02 02 01 08 63 6f 6d 70 ............comp +| 3392: 69 6c 65 72 01 02 02 01 02 02 01 02 02 01 06 64 iler...........d +| 3408: 62 73 74 61 74 07 02 03 01 02 03 01 02 03 02 04 bstat........... +| 3424: 65 62 75 67 04 02 02 01 02 02 01 02 02 01 07 65 ebug...........e +| 3440: 6e 61 62 6c 65 07 02 02 01 02 02 01 02 02 01 02 nable........... +| 3456: 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 ................ +| 3472: 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 ................ +| 3488: 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 ................ +| 3504: 02 01 02 02 02 08 78 74 65 6e 73 69 6f 6e 1f 02 ......xtension.. +| 3520: 04 01 02 04 01 02 04 01 04 66 74 73 34 0a 02 03 .........fts4... +| 3536: 01 02 03 01 02 03 04 01 35 0d 02 03 01 02 03 01 ........5....... +| 3552: 02 03 01 03 67 63 63 01 02 03 01 02 03 01 02 03 ....gcc......... +| 3568: 02 06 65 6f 70 6f 6c 79 10 02 03 01 02 03 01 02 ..eopoly........ +| 3584: 03 01 05 6a 73 6f 6e 31 13 02 03 01 02 03 01 02 ...json1........ +| 3600: 03 01 04 6c 6f 61 64 1f 02 03 01 02 03 01 02 03 ...load......... +| 3616: 01 03 6d 61 78 1c 02 02 01 02 02 01 02 02 02 05 ..max........... +| 3632: 65 6d 6f 72 79 1c 02 03 01 02 03 01 02 03 04 04 emory........... +| 3648: 73 79 73 35 16 02 03 01 02 03 01 02 03 01 06 6e sys5...........n +| 3664: 6f 63 61 73 65 02 06 01 02 02 03 06 01 02 02 03 ocase........... +| 3680: 06 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 ................ +| 3696: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3712: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3728: 02 01 04 6f 6d 69 74 1f 02 02 01 02 02 01 02 02 ...omit......... +| 3744: 01 05 72 74 72 65 65 19 02 03 01 02 03 01 02 03 ..rtree......... +| 3760: 04 02 69 6d 01 06 01 02 02 03 06 01 02 02 03 06 ..im............ +| 3776: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3792: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3808: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 ................ +| 3824: 01 0a 74 68 72 65 61 64 73 61 66 65 22 02 02 01 ..threadsafe.... +| 3840: 02 02 01 02 02 01 04 76 74 61 62 07 02 04 01 02 .......vtab..... +| 3856: 04 01 02 04 01 01 78 01 06 01 01 02 01 06 01 01 ......x......... +| 3872: 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 ................ +| 3888: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 3904: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 3920: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 ................ +| 3936: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 ................ +| 3952: 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 ................ +| 3968: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 3984: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 4000: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 ................ +| 4016: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 ................ +| 4032: 02 01 06 01 01 02 01 06 01 01 02 04 15 13 0c 0c ................ +| 4048: 12 44 13 11 0f 47 13 0e fc 0e 11 10 0f 0e 10 0f .D...G.......... +| 4064: 44 0f 10 40 15 0f 07 01 03 00 14 24 5a 24 24 0f D..@.......$Z$$. +| 4080: 0a 03 00 24 00 00 00 00 01 01 01 00 01 01 01 01 ...$............ +| page 4 offset 12288 +| 0: 0a 00 00 00 01 0f fa 00 0f fa 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 05 04 09 0c 01 02 ................ +| page 5 offset 16384 +| 0: 0d 00 00 00 24 0c 0a 00 0f d8 0f af 0f 86 0f 74 ....$..........t +| 16: 0f 61 0f 4e 0f 2f 0f 0f 0e ef 0e d7 0e be 0e a5 .a.N./.......... +| 32: 0e 8d 0e 74 0e 5b 0e 40 0e 24 0e 08 01 2f 0d d5 ...t.[.@.$.../.. +| 48: 0d bb 0d a0 0d 84 0d 68 0d 4f 0d 35 0d 1b 0c fb .......h.O.5.... +| 64: 0c da 0c b9 0c 99 0c 78 0c 57 0c 3e 0c 24 0c 0a .......x.W.>.$.. +| 3072: 00 00 00 00 00 00 00 00 00 00 18 24 05 00 25 0f ...........$..%. +| 3088: 19 54 48 52 45 41 44 53 41 46 45 3d 30 58 42 49 .THREADSAFE=0XBI +| 3104: 4e 41 52 59 18 23 05 00 25 0f 19 54 48 52 45 41 NARY.#..%..THREA +| 3120: 44 53 41 46 45 3d 30 58 4e 4f 43 41 53 45 17 8f DSAFE=0XNOCASE.. +| 3136: 05 00 25 0f 17 54 48 52 45 41 44 53 41 46 45 3d ..%..THREADSAFE= +| 3152: 30 58 52 54 52 49 4d 1f 21 05 00 33 0f 19 4f 4d 0XRTRIM.!..3..OM +| 3168: 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 4f IT LOAD EXTENSIO +| 3184: 4e 58 42 49 4e 41 52 59 1f 20 05 00 33 0f 19 4f NXBINARY. ..3..O +| 3200: 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 MIT LOAD EXTENSI +| 3216: 4f 4e 58 4e 4f 43 41 53 45 1e 1f 05 00 33 0f 17 ONXNOCASE....3.. +| 3232: 4f 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 OMIT LOAD EXTENS +| 3248: 49 4f 4e 58 52 54 52 49 4d 1f 1e 05 00 33 0f 19 IONXRTRIM....3.. +| 3264: 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 30 MAX MEMORY=50000 +| 3280: 30 30 30 57 42 49 4e 41 52 59 1f 1d 05 00 33 0f 000WBINARY....3. +| 3296: 19 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 .MAX MEMORY=5000 +| 3312: 30 30 30 30 58 4e 4f 43 41 53 45 1e 1c 05 00 33 0000XNOCASE....3 +| 3328: 0f 17 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 ..MAX MEMORY=500 +| 3344: 30 30 30 30 30 58 52 54 52 49 4d 18 1b 05 00 25 00000XRTRIM....% +| 3360: 0f 19 45 4e 41 42 4c 45 20 52 54 52 45 45 58 42 ..ENABLE RTREEXB +| 3376: 49 4e 41 52 59 18 1a 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3392: 4c 45 20 52 54 52 45 45 59 4e 4f 43 41 53 45 17 LE RTREEYNOCASE. +| 3408: 19 05 00 25 0f 17 45 4e 41 42 4c 45 20 52 54 52 ...%..ENABLE RTR +| 3424: 45 45 58 52 54 52 49 4d 1a 18 05 00 29 0f 19 45 EEXRTRIM....)..E +| 3440: 4e 41 42 4c 45 20 4d 45 4d 53 59 53 35 58 42 49 NABLE MEMSYS5XBI +| 3456: 4e 41 52 59 1a 17 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3472: 45 20 4d 45 4d 53 59 53 35 58 4e 4f 43 41 53 45 E MEMSYS5XNOCASE +| 3488: 19 16 05 00 29 0f 17 45 4e 41 42 4c 45 20 4d 45 ....)..ENABLE ME +| 3504: 4d 53 59 53 35 58 52 54 52 49 4d 18 15 05 00 25 MSYS5XRTRIM....% +| 3520: 0f 19 45 4e 41 42 4c 45 20 4a 53 4f 4e 31 58 42 ..ENABLE JSON1XB +| 3536: 49 4e 41 52 59 18 14 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3552: 4c 45 20 4a 53 4f 4e 31 58 4e 4f 43 41 53 45 17 LE JSON1XNOCASE. +| 3568: 13 05 00 25 0f 17 45 4e 41 42 4c 45 20 4a 53 4f ...%..ENABLE JSO +| 3584: 4e 31 58 52 54 52 49 4d 1a 12 05 00 29 0f 19 45 N1XRTRIM....)..E +| 3600: 4e 41 42 4c 45 20 47 45 4f 50 4f 4c 59 58 42 49 NABLE GEOPOLYXBI +| 3616: 4e 41 52 59 1a 11 05 00 29 0f 19 45 4f 41 42 4c NARY....)..EOABL +| 3632: 45 20 47 45 4f 50 4f 4c 59 58 4e 4f 43 51 53 45 E GEOPOLYXNOCQSE +| 3648: 19 10 05 00 29 0f 17 45 4e 41 42 4c 45 20 47 45 ....)..ENABLE GE +| 3664: 4f 50 4f 4c 59 58 52 54 52 49 4d 17 0f 05 00 23 OPOLYXRTRIM....# +| 3680: 0f 19 45 4e 41 42 4c 45 20 46 54 53 35 58 42 49 ..ENABLE FTS5XBI +| 3696: 4e 41 52 59 17 0e 05 00 23 0f 19 45 4e 41 42 4c NARY....#..ENABL +| 3712: 45 20 46 54 53 35 58 4e 4f 43 41 53 45 16 1d 05 E FTS5XNOCASE... +| 3728: 00 23 0f a4 45 4e 41 42 4c 45 20 46 54 53 35 58 .#..ENABLE FTS5X +| 3744: 52 54 52 49 4d 17 0c 05 00 23 0f 19 45 4e 41 42 RTRIM....#..ENAB +| 3760: 4c 45 20 46 55 53 34 58 42 49 4e 41 52 59 17 0b LE FUS4XBINARY.. +| 3776: 05 00 23 0f 19 45 4e 41 42 4c 45 20 46 54 53 34 ..#..ENABLE FTS4 +| 3792: 58 4e 4f 43 41 53 45 16 0a 05 00 23 0f 17 45 4e XNOCASE....#..EN +| 3808: 41 42 4c 45 20 46 54 53 34 58 52 54 52 49 4d 1e ABLE FTS4XRTRIM. +| 3824: 09 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3840: 54 41 54 20 56 54 41 42 58 42 49 4e 41 52 59 1e TAT VTABXBINARY. +| 3856: 08 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3872: 54 41 54 20 56 54 41 42 58 4e 4f 43 41 53 45 1d TAT VTABXNOCASE. +| 3888: 07 05 00 31 0f 17 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3904: 54 41 54 20 56 54 41 42 58 52 54 52 49 4d 11 06 TAT VTABXRTRIM.. +| 3920: 05 00 17 0f 19 44 45 42 55 47 58 42 49 4e 41 52 .....DEBUGXBINAR +| 3936: 59 11 05 05 00 17 0f 19 44 45 42 55 47 58 4e 4f Y.......DEBUGXNO +| 3952: 43 41 53 45 10 04 05 00 17 0f 17 44 45 42 55 47 CASE.......DEBUG +| 3968: 58 52 54 52 49 4d 27 03 05 00 43 0f 19 43 4f 4d XRTRIM'...C..COM +| 3984: 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e 30 20 PILER=gcc-5.4.0 +| 4000: 32 30 31 36 30 36 30 39 58 42 49 4e 41 52 59 27 20160609XBINARY' +| 4016: 02 05 00 43 0f 19 43 4f 4d 50 49 4c 45 52 3d 67 ...C..COMPILER=g +| 4032: 63 63 2d 35 2e 34 2e 30 20 32 30 31 36 30 36 30 cc-5.4.0 2016060 +| 4048: 39 58 4e 4f 43 41 53 45 26 01 05 00 43 0f 17 43 9XNOCASE&...C..C +| 4064: 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e OMPILER=gcc-5.4. +| 4080: 30 20 32 30 31 36 30 36 30 39 58 52 54 52 49 4d 0 20160609XRTRIM +| page 6 offset 20480 +| 0: 0d 00 00 00 24 0e e0 00 0f f8 0f f0 0f e8 0f e0 ....$........... +| 16: 0f d8 0f d0 0f c8 0f c0 0f b8 0f b0 0f a8 0f a0 ................ +| 32: 0f 98 0f 90 0f 88 0f 80 0f 78 0f 70 0f 68 0f 60 .........x.p.h.` +| 48: 0f 58 0f 50 0f 48 0f 40 0f 38 0f 30 0f 28 0f 20 .X.P.H.@.8.0.(. +| 64: 0f 18 0f 10 0f 08 0f 00 0e f8 0e f0 0e e8 0e e0 ................ +| 3808: 06 24 03 00 12 02 01 01 06 23 03 00 12 02 01 01 .$.......#...... +| 3824: 06 22 03 00 12 02 01 01 06 21 03 00 12 03 01 01 .........!...... +| 3840: 06 20 03 00 12 03 01 01 06 1f 03 00 12 03 01 01 . .............. +| 3856: 06 1e 03 00 12 03 01 01 06 1d 03 00 12 03 01 01 ................ +| 3872: 06 1c 03 00 12 03 01 01 06 1b 03 00 12 02 01 01 ................ +| 3888: 06 1a 03 00 12 02 01 01 06 19 03 00 12 02 01 01 ................ +| 3904: 06 18 03 00 12 02 01 01 06 17 03 00 12 02 01 01 ................ +| 3920: 06 16 03 00 12 02 01 01 06 15 03 00 12 02 01 01 ................ +| 3936: 06 14 03 00 12 02 01 01 06 13 03 00 12 02 01 01 ................ +| 3952: 06 12 03 00 12 02 01 01 06 11 03 00 12 02 01 01 ................ +| 3968: 06 10 03 00 12 02 01 01 06 0f 03 00 12 02 01 01 ................ +| 3984: 06 0e 03 00 12 02 01 01 06 0d 03 00 12 02 01 01 ................ +| 4000: 06 0c 03 00 12 02 01 01 06 0b 03 00 12 02 01 01 ................ +| 4016: 06 0a 03 00 12 02 01 01 06 09 03 00 12 03 01 01 ................ +| 4032: 06 08 03 00 12 03 01 01 06 07 03 00 12 03 01 01 ................ +| 4048: 06 06 03 00 12 01 01 01 06 05 03 00 12 01 01 01 ................ +| 4064: 06 04 03 00 12 01 01 01 06 03 03 00 12 06 01 01 ................ +| 4080: 06 02 03 00 12 06 01 01 06 01 03 00 12 06 01 01 ................ +| page 7 offset 24576 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| page 8 offset 28672 +| 0: 0d 00 00 00 03 0f d6 00 0f f4 0f e9 0f d6 00 00 ................ +| 4048: 00 00 00 00 00 00 11 03 02 2b 69 6e 74 65 67 72 .........+integr +| 4064: 69 74 79 2d 63 68 65 63 6b 09 02 02 1b 72 65 62 ity-check....reb +| 4080: 75 69 6c 64 0a 01 02 1d 6f 70 74 69 6d 69 7a 65 uild....optimize +| end crash-ae135cb10977c7.db +}]} {} + +do_catchsql_test 35.1 { + SELECT * FROM t1 WHERE t1 MATCH 'e*'; +} {1 {fts5: missing row 14 from content table 'main'.'t1_content'}} + +#------------------------------------------------------------------------- +reset_db +do_test 36.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 24576 pagesize 4096 filename crash-a6651222df1bd1.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 06 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 06 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 96: 00 00 00 00 0d 00 00 00 06 0e 0f 00 0f aa 0f 53 ...............S +| 112: 0e e8 0e 8b 0e 33 0e 0f 00 00 00 00 00 00 00 00 .....3.......... +| 3584: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 22 ................ +| 3600: 06 06 17 11 11 01 31 74 61 62 6c 65 62 62 62 62 ......1tablebbbb +| 3616: 06 43 52 45 41 54 45 20 54 41 42 4c 45 20 62 62 .CREATE TABLE bb +| 3632: 28 61 29 56 05 06 17 1f 1f 01 7d 74 61 62 6c 65 (a)V.......table +| 3648: 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 6f 6e 66 t1_configt1_conf +| 3664: 69 67 05 43 52 45 41 54 45 20 54 41 42 4c 45 20 ig.CREATE TABLE +| 3680: 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b 20 50 52 't1_config'(k PR +| 3696: 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 20 57 49 IMARY KEY, v) WI +| 3712: 54 48 4f 55 54 20 52 4f 57 49 44 5b 04 07 17 21 THOUT ROWID[...! +| 3728: 21 01 81 01 74 61 62 6c 65 74 31 5f 64 6f 63 73 !...tablet1_docs +| 3744: 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 04 43 52 izet1_docsize.CR +| 3760: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 64 EATE TABLE 't1_d +| 3776: 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 54 45 47 ocsize'(id INTEG +| 3792: 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 ER PRIMARY KEY, +| 3808: 73 7a 20 42 4c 4f 42 29 69 03 07 17 19 19 01 81 sz BLOB)i....... +| 3824: 2d 74 61 62 6c 65 74 31 5f 69 64 78 74 31 5f 69 -tablet1_idxt1_i +| 3840: 64 78 03 43 52 45 41 54 45 20 54 41 42 4c 45 20 dx.CREATE TABLE +| 3856: 27 74 31 5f 69 64 78 27 28 73 65 67 69 64 2c 20 't1_idx'(segid, +| 3872: 74 65 72 6d 2c 20 70 67 6e 6f 2c 20 50 52 49 4d term, pgno, PRIM +| 3888: 41 52 59 20 4b 45 59 28 73 65 67 69 64 2c 20 74 ARY KEY(segid, t +| 3904: 65 72 6d 29 29 20 57 49 54 48 4f 55 54 20 52 4f erm)) WITHOUT RO +| 3920: 57 49 44 55 02 07 17 1b 1b 01 81 01 74 61 62 6c WIDU........tabl +| 3936: 65 74 31 5f 64 61 74 61 74 31 5f 64 61 74 61 02 et1_datat1_data. +| 3952: 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 CREATE TABLE 't1 +| 3968: 5f 64 61 74 61 27 28 69 64 20 49 4e 54 45 47 45 _data'(id INTEGE +| 3984: 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 62 R PRIMARY KEY, b +| 4000: 6c 6f 63 6b 20 42 4c 4f 42 29 54 01 07 17 11 11 lock BLOB)T..... +| 4016: 08 81 15 74 61 62 6c 65 74 31 74 31 43 52 45 41 ...tablet1t1CREA +| 4032: 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45 TE VIRTUAL TABLE +| 4048: 20 74 31 20 55 53 49 4e 47 20 66 74 73 35 28 61 t1 USING fts5(a +| 4064: 2c 62 2c 70 72 65 66 69 78 3d 22 31 2c 32 2c 33 ,b,prefix=.1,2,3 +| 4080: 2c 34 22 2c 20 63 6f 6e 74 65 6e 74 3d 22 22 29 ,4., content=..) +| page 2 offset 4096 +| 0: 0d 0b 6a 00 37 09 4c 02 0f e7 09 4c 0f c6 0f a4 ..j.7.L....L.... +| 16: 0f 88 0f 6d 0f 4b 0f 2c 0f 0e 0e ec 0e cd 0e ad ...m.K.,........ +| 32: 0e 8e 0e 6c 0e 4b 0e 29 0e 08 0d e6 0d c4 0d b5 ...l.K.)........ +| 48: 0d 97 0d 76 0d 54 0d 31 0d 15 0c f3 0c d3 0c b5 ...v.T.1........ +| 64: 0c 95 0c 73 0c 54 0c 32 0c 10 0b ee 0b cc 0b b0 ...s.T.2........ +| 80: 0b 8d 0b 7e 0b 48 0b 2e 0b 0b 0a ef 0a cc 0a ad ...~.H.......... +| 96: 0a 8c 0a 6d 0a 4d 0a 2b 0a 0c 09 ec 09 ca 09 a8 ...m.M.+........ +| 112: 09 86 09 63 0f f1 00 00 00 00 00 00 00 00 00 00 ...c............ +| 2368: 00 00 00 00 00 00 00 00 00 00 00 00 15 0a 03 00 ................ +| 2384: 30 00 00 00 01 01 03 35 00 03 01 11 12 02 01 12 0......5........ +| 2400: 03 01 11 1c 8c 80 80 80 80 10 03 00 3e 00 00 00 ............>... +| 2416: 17 01 05 05 34 74 61 62 6c 03 02 03 01 04 77 68 ....4tabl.....wh +| 2432: 65 72 03 02 06 09 1b 8c 80 80 80 80 0f 03 00 3c er.............< +| 2448: 00 00 00 16 05 34 66 74 73 34 03 02 02 01 04 6e .....4fts4.....n +| 2464: 75 6d 62 03 06 01 04 09 1b 8c 80 80 80 80 0e 03 umb............. +| 2480: 00 3c 00 00 00 16 04 33 74 68 65 03 06 01 01 04 .<.....3the..... +| 2496: 01 03 77 68 65 03 02 04 04 0a 1b 8c 80 80 80 80 ..whe........... +| 2512: 0d 03 00 3c 00 00 00 16 04 33 6e 75 6d 03 06 01 ...<.....3num... +| 2528: 01 05 01 03 74 61 62 03 02 03 04 0a 19 8c 80 80 ....tab......... +| 2544: 80 80 0c 03 00 38 00 00 00 14 03 32 77 68 03 02 .....8.....2wh.. +| 2560: 04 00 04 33 66 74 73 03 02 02 04 07 18 8c 80 80 ...3fts......... +| 2576: 80 80 0b 03 00 36 00 00 00 13 03 32 74 61 03 02 .....6.....2ta.. +| 2592: 03 02 01 68 03 06 01 01 04 04 07 1b 8c 80 80 80 ...h............ +| 2608: 80 0a 03 00 3c 00 00 00 16 03 32 6e 75 03 06 01 ....<.....2nu... +| 2624: 01 05 01 02 6f 66 03 06 01 01 06 04 09 19 8c 80 ....of.......... +| 2640: 80 80 80 09 03 00 38 00 00 00 14 03 32 66 74 03 ......8.....2ft. +| 2656: 02 02 01 02 69 73 03 06 01 01 03 04 07 18 8c 80 ....is.......... +| 2672: 80 80 80 08 03 00 36 00 00 00 13 02 31 74 03 08 ......6.....1t.. +| 2688: 03 01 01 04 01 01 77 03 02 04 04 09 1a 8c 80 80 ......w......... +| 2704: 80 80 07 03 00 3a 00 00 00 15 02 31 6e 03 08 01 .....:.....1n... +| 2720: 01 02 05 01 00 6f 03 06 01 01 06 04 09 18 8c 80 .....o.......... +| 2736: 80 80 80 06 03 00 36 00 00 00 13 04 02 31 66 03 ......6......1f. +| 2752: 02 02 01 01 69 03 06 01 01 03 05 06 1c 8c 80 80 ....i........... +| 2768: 80 80 05 03 00 3e 00 00 00 17 04 30 74 68 65 03 .....>.....0the. +| 2784: 06 01 01 04 01 05 77 68 65 72 65 03 02 04 0a 15 ......where..... +| 2800: 8c 80 80 80 80 04 03 00 30 00 00 00 11 01 01 06 ........0....... +| 2816: 06 30 74 61 62 6c 65 03 02 03 07 1c 8c 80 80 80 .0table......... +| 2832: 80 03 03 00 3e 00 00 00 17 07 30 6e 75 6d 62 65 ....>.....0numbe +| 2848: 72 03 06 01 01 05 01 02 6f 66 03 06 04 0d 13 8c r.......of...... +| 2864: 80 80 80 80 02 03 00 2c 00 00 00 0f 01 01 03 02 .......,........ +| 2880: 30 6e 03 06 01 01 02 07 1b 8c 80 80 80 80 01 03 0n.............. +| 2896: 00 3c 00 00 00 16 08 30 66 74 73 34 61 75 78 03 .<.....0fts4aux. +| 2912: 02 02 01 02 69 73 03 06 04 0c 00 00 00 14 2a 00 ....is........*. +| 2928: 00 00 01 01 02 24 00 02 01 01 12 02 01 12 08 88 .....$.......... +| 2944: 80 80 80 80 12 03 00 16 00 00 00 05 02 1c 88 80 ................ +| 2960: 80 80 80 11 03 00 3e 00 00 00 17 05 34 72 6f 77 ......>.....4row +| 2976: 73 02 06 01 01 05 01 04 74 68 65 72 02 02 04 0b s.......ther.... +| 2992: 15 88 80 80 80 80 10 03 00 30 00 00 00 11 02 01 .........0...... +| 3008: 01 07 05 34 62 65 74 77 02 02 04 08 1b 88 80 80 ...4betw........ +| 3024: 80 80 0f 03 00 3c 00 00 00 16 04 04 33 72 6f 77 .....<......3row +| 3040: 02 06 01 01 05 01 03 74 68 65 02 08 05 0a 1b 88 .......the...... +| 3056: 80 80 80 80 0e 03 00 3c 00 00 00 16 01 01 02 04 .......<........ +| 3072: 33 61 72 65 02 02 03 01 03 62 65 74 02 02 07 08 3are.....bet.... +| 3088: 1b 88 80 80 80 80 0d 03 00 3c 00 00 00 16 13 32 .........<.....2 +| 3104: 74 68 02 08 02 01 01 07 00 04 33 61 6e 64 02 06 th........3and.. +| 3120: 04 0a 1b 88 80 80 80 80 0c 03 00 3c 00 00 00 16 ...........<.... +| 3136: 03 32 69 6e 02 06 01 01 06 01 02 72 6f 02 06 01 .2in.......ro... +| 3152: 01 05 04 09 18 88 80 80 80 80 0b 03 00 36 00 00 .............6.. +| 3168: 00 13 02 03 32 61 72 02 02 03 01 02 62 65 02 02 ....2ar.....be.. +| 3184: 04 05 07 1b 88 80 80 80 80 0a 03 00 3c 00 00 00 ............<... +| 3200: 16 02 31 74 02 08 02 01 01 07 00 03 32 61 6e 02 ..1t........2an. +| 3216: 06 01 01 04 09 19 88 80 80 80 80 09 03 00 38 00 ..............8. +| 3232: 00 00 14 02 31 6e 02 06 01 01 03 01 01 72 02 06 ....1n.......r.. +| 3248: 01 01 05 04 08 17 88 80 80 80 80 08 03 00 34 00 ..............4. +| 3264: 00 00 12 02 31 62 02 02 04 01 01 69 02 06 01 01 ....1b.....i.... +| 3280: 06 04 06 19 88 80 80 80 80 07 03 00 38 00 00 00 ............8... +| 3296: 14 04 02 31 32 02 02 05 01 01 61 02 08 03 01 01 ...12.....a..... +| 3312: 02 05 06 1b 88 80 80 80 80 06 03 00 3c 00 00 00 ............<... +| 3328: 16 06 30 74 68 65 72 65 02 02 02 00 02 31 31 02 ..0there.....11. +| 3344: 06 01 01 04 0a 15 88 80 80 80 80 05 03 00 30 00 ..............0. +| 3360: 00 00 11 01 01 05 04 30 74 68 65 02 06 01 01 07 .......0the..... +| 3376: 07 1c 88 80 80 80 80 04 03 00 3e 00 00 00 17 01 ..........>..... +| 3392: 01 06 02 30 6e 02 06 01 01 03 01 04 72 6f 77 73 ...0n.......rows +| 3408: 02 06 07 08 1b 88 80 80 80 80 03 03 00 3c 00 00 .............<.. +| 3424: 00 16 08 30 62 65 74 77 65 65 6e 02 02 04 01 02 ...0between..... +| 3440: 69 6e 02 06 04 0c 1a 88 80 80 80 80 02 03 00 3a in.............: +| 3456: 00 00 00 15 04 30 61 6e 64 02 06 01 01 02 02 02 .....0and....... +| 3472: 72 65 02 02 03 04 0a 17 88 80 80 80 80 01 03 00 re.............. +| 3488: 34 00 00 00 12 02 30 31 02 06 01 01 04 01 01 32 4.....01.......2 +| 3504: 02 02 05 04 08 08 84 80 80 80 80 12 03 00 16 00 ................ +| 3520: 00 00 05 04 1b 84 80 80 80 80 11 03 00 3c 00 00 .............<.. +| 3536: 00 16 05 34 74 61 62 6c 01 06 01 01 05 02 03 65 ...4tabl.......e +| 3552: 72 6d 01 02 04 0b 1b 84 80 80 80 80 10 03 00 3c rm.............< +| 3568: 00 00 00 16 05 34 65 61 63 68 01 02 03 01 04 70 .....4each.....p +| 3584: 72 65 73 01 02 05 04 09 1a 84 80 80 80 80 0f 03 res............. +| 3600: 00 3a 00 00 00 15 04 33 74 65 72 01 02 04 02 02 .:.....3ter..... +| 3616: 68 65 01 06 01 01 03 04 08 1b 84 80 80 80 80 0e he.............. +| 3632: 03 00 3c 00 00 00 16 04 33 70 72 65 01 02 05 01 ..<.....3pre.... +| 3648: 03 74 61 62 01 06 01 01 05 04 08 1a 84 80 80 80 .tab............ +| 3664: 80 0d 03 00 3a 00 00 00 15 04 33 66 6f 72 01 02 ....:.....3for.. +| 3680: 02 02 02 74 73 01 06 01 01 04 04 08 1b 84 80 80 ...ts........... +| 3696: 80 80 0c 03 00 3c 00 00 00 16 03 32 74 68 01 06 .....<.....2th.. +| 3712: 01 01 03 00 04 33 65 61 63 01 02 03 04 09 18 84 .....3eac....... +| 3728: 80 80 80 80 0b 03 00 36 00 00 00 13 03 32 74 61 .......6.....2ta +| 3744: 01 06 01 01 05 02 01 65 01 02 04 04 09 19 84 80 .......e........ +| 3760: 80 80 80 0a 03 00 38 00 00 00 14 03 32 69 6e 01 ......8.....2in. +| 3776: 06 01 01 02 01 02 70 72 01 02 05 04 09 18 84 80 ......pr........ +| 3792: 80 80 80 09 03 00 36 00 00 00 13 03 32 66 6f 01 ......6.....2fo. +| 3808: 02 02 02 01 74 01 06 01 01 04 04 07 1b 84 80 80 ....t........... +| 3824: 80 80 08 03 00 3c 00 00 00 16 02 31 74 01 0a 04 .....<.....1t... +| 3840: 01 01 03 04 00 03 32 65 61 01 02 03 04 0a 17 84 ......2ea....... +| 3856: 80 80 80 80 07 03 00 34 00 00 00 12 02 31 69 01 .......4.....1i. +| 3872: 06 01 01 02 01 01 70 01 02 05 04 08 18 84 80 80 ......p......... +| 3888: 80 80 06 03 00 36 00 00 00 13 02 31 65 01 02 03 .....6.....1e... +| 3904: 01 01 66 01 08 02 01 01 04 04 06 1b 84 80 80 80 ..f............. +| 3920: 80 05 03 00 3c 00 00 00 16 05 30 74 65 72 6d 01 ....<.....0term. +| 3936: 02 04 02 02 68 65 01 06 01 01 03 04 09 14 84 80 ....he.......... +| 3952: 80 80 80 04 03 00 2e 00 00 00 10 06 30 64 61 62 ............0dab +| 3968: 6c 65 01 06 01 01 05 04 15 84 80 80 80 80 03 03 le.............. +| 3984: 00 30 00 00 00 11 02 08 30 70 72 65 73 65 6e 74 .0......0present +| 4000: 01 02 05 05 1b 84 80 80 80 80 02 03 00 3c 00 00 .............<.. +| 4016: 00 16 04 30 66 74 73 01 06 01 01 04 01 02 69 6e ...0fts.......in +| 4032: 01 06 01 01 04 0a 1a 84 80 80 80 80 01 03 00 3a ...............: +| 4048: 00 00 00 15 05 30 65 61 63 68 01 02 03 01 03 66 .....0each.....f +| 4064: 6f 72 01 02 02 04 09 06 01 03 00 12 03 0b 0f 00 or.............. +| 4080: 00 08 8c 80 80 80 80 11 03 00 16 00 00 00 05 04 ................ +| page 3 offset 8192 +| 0: 0a 00 00 00 32 0e 4f 00 0f fa 0f f1 0f e9 0f e1 ....2.O......... +| 16: 0f d8 0f d1 0f c9 0f c1 0f b9 0f b1 0f a9 0f a0 ................ +| 32: 0f 98 0f 90 0f 87 0f 80 0f 78 0f 71 0f 68 0f 5f .........x.q.h._ +| 48: 0f 56 0f 4d 0f 41 0f 38 0f 2f 0f 26 0f 1d 0f 13 .V.M.A.8./.&.... +| 64: 0f 0a 0f 01 0e f7 0e ee 0e e6 0e dd 0e d6 0e cd ................ +| 80: 0e c3 0e ba 0e b0 0e a8 0e 9f 0e 96 0e 8e 0e 85 ................ +| 96: 0e 7c 0e 73 0e 6a 0e 60 0e 58 0e 4f 00 00 00 00 .|.s.j.`.X.O.... +| 3648: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 08 ................ +| 3664: 04 01 10 01 03 34 74 20 07 04 01 0e 01 03 34 1e .....4t ......4. +| 3680: 09 04 01 12 01 03 33 74 68 1c 08 04 01 10 01 03 ......3th....... +| 3696: 33 6e 1a 08 04 01 10 01 03 32 77 18 08 04 01 10 3n.......2w..... +| 3712: 01 03 32 74 16 08 04 01 10 01 03 32 6e 14 07 04 ..2t.......2n... +| 3728: 01 0e 01 03 32 12 08 04 01 10 01 03 31 74 10 08 ....2.......1t.. +| 3744: 04 01 10 01 03 31 6e 0e 07 04 01 0e 01 03 31 0c .....1n.......1. +| 3760: 09 04 01 12 01 03 30 74 68 0a 08 04 01 10 01 03 ......0th....... +| 3776: 30 74 08 09 04 01 12 01 03 30 6e 75 06 08 04 01 0t.......0nu.... +| 3792: 10 01 03 30 6e 04 06 04 01 0c 01 03 02 08 04 01 ...0n........... +| 3808: 10 01 02 34 72 22 07 04 01 0e 01 02 34 20 08 04 ...4r.......4 .. +| 3824: 01 10 01 02 33 72 1e 09 04 01 12 01 02 33 61 72 ....3r.......3ar +| 3840: 1c 08 04 01 10 01 02 32 74 1a 08 04 01 10 01 02 .......2t....... +| 3856: 32 69 18 09 04 01 12 01 02 32 61 72 16 08 04 01 2i.......2ar.... +| 3872: 10 01 02 31 74 14 08 04 01 10 01 02 31 6e 12 08 ...1t.......1n.. +| 3888: 04 01 10 01 02 31 62 10 08 04 01 10 01 02 31 32 .....1b.......12 +| 3904: 0e 0b 04 01 16 01 02 30 74 68 65 72 0c 08 04 01 .......0ther.... +| 3920: 10 01 02 30 74 0a 08 04 01 10 01 02 30 6e 08 08 ...0t.......0n.. +| 3936: 14 01 10 01 02 30 62 06 08 04 01 10 01 02 30 61 .....0b.......0a +| 3952: 04 06 04 01 0c 01 02 02 07 04 09 10 01 34 74 22 .............4t. +| 3968: 06 04 09 0e 01 34 20 08 04 09 12 01 33 74 65 1e .....4 .....3te. +| 3984: 07 04 09 10 01 33 70 1c 07 04 09 10 01 33 66 1a .....3p......3f. +| 4000: 08 04 09 12 01 32 74 68 18 07 04 09 10 01 32 74 .....2th......2t +| 4016: 16 07 04 09 10 01 32 69 14 07 04 09 10 01 32 66 ......2i......2f +| 4032: 12 07 04 09 10 01 31 74 10 07 04 09 10 01 31 69 ......1t......1i +| 4048: 0e 06 04 09 0e 01 31 0c 08 04 09 12 01 30 74 65 ......1......0te +| 4064: 0a 07 04 09 10 01 30 74 08 07 04 09 10 01 30 70 ......0t......0p +| 4080: 06 08 04 09 12 01 30 66 74 04 05 04 09 0c 01 02 ......0ft....... +| page 4 offset 12288 +| 0: 0d 00 00 00 03 0f eb 00 0f f9 0f f2 0f eb 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 05 03 03 00 10 ................ +| 4080: 03 05 05 02 03 00 10 04 06 05 01 03 00 10 04 04 ................ +| page 5 offset 16384 +| 0: 0a 00 00 00 02 0f eb 00 0f eb 0f f4 00 00 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 08 03 15 01 70 ...............p +| 4080: 67 73 7a 18 0b 03 1b 01 76 65 72 73 69 6f 6e 04 gsz.....version. +| page 6 offset 20480 +| 0: 0d 00 00 00 03 0f f2 00 0f fc 0f f7 0f f2 00 00 ................ +| 4080: 00 00 03 03 02 01 03 03 02 02 01 02 02 01 0c e9 ................ +| end crash-a6651222df1bd1.db +}]} {} + +do_catchsql_test 36.1 { + INSERT INTO t1(b) VALUES( + x'78de3fa24af3733ca8769291a0fee3669f9fddefc5cba913e4225d4b6ce2b04f26b87fad3ee6f9b7d90a1ea62a169bf41e5d32707a6ca5c3d05e4bde05c9d89eaaa8c50e74333d2e9fcd7dfe95528a3a016aac1102d825c5cd70cf99d8a88e0ea7f798d4334386518b7ad359beb168b93aba059a2a3bd93112d65b44c12b9904ea786b204d80531cdf0504bf9b203dbe927061974caf7b9f30cbc3397b61f802e732012a6663d41c3607d6f1c0dbcfd489adac05ca500c0b04439d894cd93a840159225ef73b627e178b9f84b3ffe66cf22a963a8368813ff7961fc47f573211ccec95e0220dcbb3bf429f4a50ba54d7a53784ac51bfef346e6a'); +} {0 {}} + +#------------------------------------------------------------------------- +reset_db +do_test 37.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 40960 pagesize 4096 filename null-memcmp-param-1..db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 0a .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 0d 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 96: 00 00 00 00 0d 00 00 00 0d 0b 6e 00 0f a3 0f 4c ..........n....L +| 112: 0e e1 0e 81 0e 24 0d cc 0d 72 0d 1b 0c b0 0c 50 .....$...r.....P +| 128: 0b f8 0b b3 0b 6e 00 00 00 00 00 00 00 00 00 00 .....n.......... +| 2912: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 43 0d ..............C. +| 2928: 06 17 11 11 08 75 74 61 62 6c 66 74 34 74 34 43 .....utablft4t4C +| 2944: 52 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 REATE VIRTUAL TA +| 2960: 42 4c 45 20 74 34 20 55 53 49 4e 47 20 66 74 73 BLE t4 USING fts +| 2976: 35 76 6f 63 61 62 28 27 74 32 27 2c 20 27 72 6f 5vocab('t2', 'ro +| 2992: 77 27 29 43 0c 06 17 11 11 08 75 74 61 62 6c 65 w')C......utable +| 3008: 74 33 74 33 43 52 45 41 54 45 20 56 49 52 54 55 t3t3CREATE VIRTU +| 3024: 41 4c 20 54 41 42 4c 45 20 74 33 20 55 53 49 4e AL TABLE t3 USIN +| 3040: 47 20 66 74 73 35 76 6f 63 61 62 28 27 74 31 27 G fts5vocab('t1' +| 3056: 2c 20 27 72 6f 77 27 29 56 0b 06 17 1f 1f 01 7d , 'row')V....... +| 3072: 74 61 62 6c 65 74 32 5f 63 6f 6e 66 69 67 74 32 tablet2_configt2 +| 3088: 5f 63 6f 6e 66 69 67 0a 43 52 45 41 54 45 20 54 _config.CREATE T +| 3104: 41 42 4c 45 20 27 74 32 5f 63 6f 6e 66 69 67 27 ABLE 't2_config' +| 3120: 28 6b 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 (k PRIMARY KEY, +| 3136: 76 29 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 v) WITHOUT ROWID +| 3152: 5e 0a 07 17 21 21 01 81 07 74 61 62 6c 65 74 32 ^...!!...tablet2 +| 3168: 5f 63 6f 6e 74 65 6e 74 74 32 5f 63 6f 6e 74 65 _contentt2_conte +| 3184: 6e 74 09 43 52 45 41 54 45 20 54 41 42 4c 45 20 nt.CREATE TABLE +| 3200: 27 74 32 5f 63 6f 6e 74 65 6e 74 27 28 69 64 20 't2_content'(id +| 3216: 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 INTEGER PRIMARY +| 3232: 4b 45 59 2c 20 63 30 2c 20 63 31 2c 20 63 32 29 KEY, c0, c1, c2) +| 3248: 69 09 07 17 19 19 01 81 2d 74 61 62 6c 65 74 32 i.......-tablet2 +| 3264: 5f 69 64 78 74 32 5f 69 64 78 08 43 52 45 41 54 _idxt2_idx.CREAT +| 3280: 45 20 54 41 42 4c 45 20 27 74 32 5f 69 64 78 27 E TABLE 't2_idx' +| 3296: 28 73 65 67 69 64 2c 20 74 65 72 6d 2c 20 70 67 (segid, term, pg +| 3312: 6e 6f 2c 20 50 52 49 4d 41 52 59 20 4b 45 59 28 no, PRIMARY KEY( +| 3328: 73 65 67 69 64 2c 20 74 65 72 6d 29 29 20 57 49 segid, term)) WI +| 3344: 54 48 4f 55 54 20 52 4f 57 49 44 55 08 07 17 1b THOUT ROWIDU.... +| 3360: 1b 01 81 01 74 61 62 6c 65 74 32 5f 64 61 74 61 ....tablet2_data +| 3376: 74 32 5f 64 61 74 61 07 43 52 45 41 54 45 20 54 t2_data.CREATE T +| 3392: 41 42 4c 45 20 27 74 32 5f 64 61 74 61 27 28 69 ABLE 't2_data'(i +| 3408: 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 d INTEGER PRIMAR +| 3424: 59 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 4c 4f Y KEY, block BLO +| 3440: 42 29 58 07 07 17 11 11 08 81 1d 74 61 62 6c 65 B)X........table +| 3456: 74 32 74 32 43 52 45 41 54 45 20 56 49 52 54 55 t2t2CREATE VIRTU +| 3472: 41 4c 20 54 41 42 4c 45 20 74 32 20 55 53 49 4e AL TABLE t2 USIN +| 3488: 47 20 64 44 73 35 28 27 61 27 2c 5b 62 5d 2c 22 G dDs5('a',[b],. +| 3504: 63 22 2c 64 65 74 61 69 6c 3d 6e 6f 6e 65 2c 63 c.,detail=none,c +| 3520: 6f 6c 75 6d 6e 73 69 7a 65 3d 30 29 56 06 06 17 olumnsize=0)V... +| 3536: 1f 1f 01 7d 74 61 62 6c 65 74 31 5f 63 6f 6e 66 ....tablet1_conf +| 3552: 69 67 74 31 5f 63 6f 6e 66 69 67 06 43 52 45 41 igt1_config.CREA +| 3568: 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 6f 6e TE TABLE 't1_con +| 3584: 66 69 67 27 28 6b 20 50 52 49 4d 41 52 59 20 4b fig'(k PRIMARY K +| 3600: 45 59 2c 20 76 29 20 57 49 54 48 4f 55 54 20 52 EY, v) WITHOUT R +| 3616: 4f 57 49 44 5b 05 07 17 21 21 01 81 01 74 61 62 OWID[...!!...tab +| 3632: 6c 65 74 31 5f 64 6f 63 73 69 7a 65 74 31 5f 64 let1_docsizet1_d +| 3648: 6f 63 73 69 7a 65 05 43 52 45 41 54 45 20 54 41 ocsize.CREATE TA +| 3664: 42 4c 45 20 27 74 31 5f 64 6f 63 73 69 7a 65 27 BLE 't1_docsize' +| 3680: 28 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d (id INTEGER PRIM +| 3696: 41 52 59 20 4b 45 59 2c 20 73 7a 20 42 4c 4f 42 ARY KEY, sz BLOB +| 3712: 29 5e 04 07 17 21 21 01 81 07 74 61 62 6c 65 74 )^...!!...tablet +| 3728: 31 5f 63 6f 6e 74 65 6e 74 74 31 5f 63 6f 6e 74 1_contentt1_cont +| 3744: 65 6e 74 04 43 52 45 41 54 45 20 54 41 42 4c 45 ent.CREATE TABLE +| 3760: 20 27 74 31 5f 63 6f 6e 74 65 6e 74 27 28 69 64 't1_content'(id +| 3776: 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 INTEGER PRIMARY +| 3792: 20 4b 45 59 2c 20 63 30 2c 20 63 31 2c 20 63 32 KEY, c0, c1, c2 +| 3808: 29 69 03 07 17 19 19 01 81 2d 74 61 62 6c 65 74 )i.......-tablet +| 3824: 31 5f 69 64 78 74 31 5f 69 64 78 03 43 52 45 41 1_idxt1_idx.CREA +| 3840: 54 45 20 54 41 42 4c 45 20 27 74 31 5f 69 64 78 TE TABLE 't1_idx +| 3856: 27 28 73 65 67 69 64 2c 20 74 65 72 6d 2c 20 70 '(segid, term, p +| 3872: 67 6e 6f 2c 20 50 52 49 4d 41 52 59 20 4b 45 59 gno, PRIMARY KEY +| 3888: 28 73 65 67 69 64 2c 20 74 65 72 6d 29 29 20 57 (segid, term)) W +| 3904: 49 54 48 4f 55 54 20 52 4f 57 49 44 55 02 07 17 ITHOUT ROWIDU... +| 3920: 1b 1b 01 81 01 74 61 62 6c 65 74 31 5f 64 61 74 .....tablet1_dat +| 3936: 61 74 31 5f 64 61 74 61 02 43 52 45 41 54 45 20 at1_data.CREATE +| 3952: 54 41 42 4c 45 20 27 74 31 5f 64 61 74 61 27 28 TABLE 't1_data'( +| 3968: 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 id INTEGER PRIMA +| 3984: 52 59 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 4c RY KEY, block BL +| 4000: 4f 42 29 5b 01 07 17 11 11 08 81 23 74 61 62 6c OB)[.......#tabl +| 4016: 65 74 31 74 31 43 52 45 41 54 45 20 56 49 52 54 et1t1CREATE VIRT +| 4032: 55 41 4c 20 54 41 42 4c 45 20 74 31 20 55 53 49 UAL TABLE t1 USI +| 4048: 4e 47 20 66 74 73 35 28 61 2c 62 20 75 6e 69 6e NG fts5(a,b unin +| 4064: 64 65 78 65 64 2c 63 2c 74 6f 6b 65 6e 69 7a 65 dexed,c,tokenize +| 4080: 3d 22 70 6f 72 74 65 72 20 61 73 63 69 69 22 29 =.porter ascii.) +| page 2 offset 4096 +| 0: 0d 0f 68 00 05 0f 13 00 0f e6 0f 13 0f a8 0f 7c ..h............| +| 16: 0f 2a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 .*.............. +| 3856: 00 00 00 15 0a 03 00 30 00 00 00 00 01 03 03 00 .......0........ +| 3872: 03 01 01 01 02 01 01 03 01 01 37 8c 80 80 80 80 ..........7..... +| 3888: 01 03 00 74 00 00 00 2e 02 30 61 03 02 02 01 01 ...t.....0a..... +| 3904: 62 03 02 03 01 01 63 03 02 04 01 01 67 03 06 01 b.....c.....g... +| 3920: 02 02 01 01 68 03 06 01 02 03 01 01 69 03 06 01 ....h.......i... +| 3936: 02 04 04 06 06 06 08 08 0f ef 00 14 2a 00 00 00 ............*... +| 3952: 00 01 02 02 00 02 01 01 01 02 01 01 25 88 80 80 ............%... +| 3968: 80 80 01 03 00 50 00 00 00 1f 01 30 67 02 08 02 .....P.....0g... +| 3984: 01 02 02 01 01 68 02 08 03 01 02 03 01 01 69 02 .....h........i. +| 4000: 08 04 01 02 04 04 09 09 37 84 80 80 80 80 01 03 ........7....... +| 4016: 00 74 00 00 00 2e 02 30 61 01 5a 02 01 01 62 01 .t.....0a.Z...b. +| 4032: 02 03 01 01 63 01 02 04 01 01 67 01 06 01 02 02 ....c.....g..... +| 4048: 01 01 68 01 06 01 02 03 01 01 69 01 06 01 02 04 ..h.......i..... +| 4064: 04 06 06 06 08 08 07 01 03 00 14 03 09 00 09 00 ................ +| 4080: 00 00 11 24 00 00 00 00 01 01 01 00 01 01 01 01 ...$............ +| page 3 offset 8192 +| 0: 0a 00 00 00 03 0f ec 00 0f fa 0f f3 0f ec 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 00 06 04 01 0c ................ +| 4080: 01 03 02 06 04 01 0c 01 02 02 05 04 09 0c 01 02 ................ +| page 4 offset 12288 +| 0: 0d 00 00 00 03 0f be 00 0f ea 0f d4 0f be 00 00 ................ +| 4016: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 14 03 ................ +| 4032: 05 00 17 17 17 61 20 62 20 63 67 20 68 20 69 67 .....a b cg h ig +| 4048: 20 68 20 69 14 02 05 00 17 17 17 67 20 68 20 69 h i.......g h i +| 4064: 61 20 62 20 63 67 20 68 20 69 14 01 05 00 17 17 a b cg h i...... +| 4080: 17 61 20 62 20 63 64 20 65 20 66 67 20 68 20 69 .a b cd e fg h i +| page 5 offset 16384 +| 0: 0d 00 00 00 03 0f e8 00 0f f8 0f f0 0f e8 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 06 03 03 00 12 03 00 03 ................ +| 4080: 06 02 03 00 12 03 00 03 06 01 03 00 12 03 00 03 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| page 7 offset 24576 +| 0: 0d 00 00 00 03 0f 9e 00 0f e6 0f ef 0f 9e 00 00 ................ +| 3984: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 41 84 ..............A. +| 4000: 80 80 80 80 01 04 00 81 06 00 00 00 34 02 30 61 ............4.0a +| 4016: 01 01 01 01 01 62 01 01 01 01 01 63 01 01 01 01 .....b.....c.... +| 4032: 01 64 01 01 01 65 01 01 01 66 01 01 01 67 01 01 .d...e...f...g.. +| 4048: 01 01 01 68 01 01 01 01 01 69 01 01 01 04 06 06 ...h.....i...... +| 4064: 06 04 04 04 06 06 07 01 03 00 14 03 09 09 09 0f ................ +| 4080: 0a 03 00 24 00 00 00 00 01 01 01 00 01 01 01 01 ...$............ +| page 8 offset 28672 +| 0: 0a 00 00 00 01 0f fa 00 0f fa 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 05 04 09 0c 01 02 ................ +| page 9 offset 32768 +| 0: 0d 00 00 00 03 0f be 00 0f ea 0f d4 0f be 00 00 ................ +| 4016: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 14 03 ................ +| 4032: 05 00 17 17 17 61 20 62 20 63 67 20 68 20 69 67 .....a b cg h ig +| 4048: 20 68 20 69 14 02 05 00 17 17 17 67 20 68 20 69 h i.......g h i +| 4064: 61 20 62 20 63 67 20 68 20 69 14 01 05 00 17 17 a b cg h i...... +| 4080: 17 61 20 62 20 63 64 20 65 20 66 67 20 68 30 69 .a b cd e fg h0i +| page 10 offset 36864 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| end null-memcmp-param-1..db +}]} {} + +do_catchsql_test 37.1 { + SELECT * FROM t3; +} {/*malformed database schema*/} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 37a.0 { + CREATE VIRTUAL TABLE t1 USING fts5(b, c); + INSERT INTO t1 VALUES('a', 'b'); + SELECT quote(block) FROM t1_data WHERE rowid=10; +} {X'000000000101010001010101'} + +do_execsql_test 37a.1 { + UPDATE t1_data SET block = X'FFFFFFFF0101010001010101' WHERE rowid = 10; + SELECT rowid FROM t1('a'); +} {1} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 38.0 { + CREATE VIRTUAL TABLE t1 USING fts5(b, c); + INSERT INTO t1 VALUES('a', 'b'); + INSERT INTO t1 VALUES('a', 'b'); + SELECT quote(block) FROM t1_data WHERE rowid=1; +} {X'020202'} + +do_execsql_test 38.1 { + SELECT * FROM t1('a b') ORDER BY rank; +} {a b a b} + +do_execsql_test 38.2 { + UPDATE t1_data SET block = X'000202' WHERE rowid=1; +} +do_catchsql_test 38.3 { + SELECT * FROM t1('a b') ORDER BY rank; +} {1 {database disk image is malformed}} + +db close +sqlite3 db test.db +do_catchsql_test 38.4 { + SELECT * FROM t1('a b') ORDER BY rank; +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +reset_db +do_test 38.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 32768 pagesize 4096 filename crash-fd2a1313e5b5e9.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 08 .....@ ........ +| 32: 00 00 00 02 00 00 00 01 00 00 00 09 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 96: 00 00 00 00 0d 0f c7 00 07 0d 92 00 0f 8d 0f 36 ...............6 +| 112: 0e cb 0e 6b 0e 0e 0d b6 0d 92 0d 92 00 00 00 00 ...k............ +| 3472: 00 00 22 08 06 17 11 11 01 31 74 61 62 6c 65 74 .........1tablet +| 3488: 32 74 32 08 43 52 45 41 54 45 20 54 41 42 4c 45 2t2.CREATE TABLE +| 3504: 20 74 32 28 78 29 56 07 06 17 1f 1f 01 7d 74 61 t2(x)V.......ta +| 3520: 62 6c 65 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 blet1_configt1_c +| 3536: 6f 6e 66 69 67 07 43 52 45 41 54 45 20 54 41 42 onfig.CREATE TAB +| 3552: 4c 45 20 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b LE 't1_config'(k +| 3568: 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 PRIMARY KEY, v) +| 3584: 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 5b 06 WITHOUT ROWID[. +| 3600: 07 17 21 21 01 81 01 74 61 62 6c 65 74 31 5f 64 ..!!...tablet1_d +| 3616: 6f 63 73 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 ocsizet1_docsize +| 3632: 06 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 .CREATE TABLE 't +| 3648: 31 5f 64 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 1_docsize'(id IN +| 3664: 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE +| 3680: 59 2c 20 73 7a 20 42 4c 4f 42 29 5e 05 07 17 21 Y, sz BLOB)^...! +| 3696: 21 01 81 07 74 61 62 6c 65 b8 31 5f 63 6f 6e 74 !...table.1_cont +| 3712: 65 6e 74 74 31 5f 63 6f 6e 74 65 6e 74 05 43 52 entt1_content.CR +| 3728: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 EATE TABLE 't1_c +| 3744: 6f 6e 74 65 6e 74 27 28 69 64 20 49 4e 54 45 47 ontent'(id INTEG +| 3760: 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 ER PRIMARY KEY, +| 3776: 63 30 2c 20 63 31 2c 20 63 32 29 69 04 07 17 19 c0, c1, c2)i.... +| 3792: 19 01 81 2d 74 61 62 6c 65 74 31 5f 69 64 78 74 ...-tablet1_idxt +| 3808: 31 5f 69 64 78 04 43 52 45 41 54 45 20 54 41 42 1_idx.CREATE TAB +| 3824: 4c 45 20 27 74 31 5f 69 64 78 27 28 73 65 67 69 LE 't1_idx'(segi +| 3840: 64 2c 20 74 65 72 6d 2c 20 70 67 6e 6f 2c 20 50 d, term, pgno, P +| 3856: 52 49 4d 41 52 59 20 4b 45 59 28 73 65 67 69 64 RIMARY KEY(segid +| 3872: 2c 20 74 65 72 6d 29 29 20 57 49 54 48 4f 55 54 , term)) WITHOUT +| 3888: 20 52 4f 57 49 44 55 03 07 17 1b 1b 01 81 01 74 ROWIDU........t +| 3904: 61 62 6c 65 74 31 5f 64 61 74 61 74 31 5f 64 61 ablet1_datat1_da +| 3920: 74 61 03 43 52 45 41 54 45 20 54 41 42 4c 45 20 ta.CREATE TABLE +| 3936: 27 74 31 5f 64 61 74 61 27 28 69 64 20 49 4e 54 't1_data'(id INT +| 3952: 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 EGER PRIMARY KEY +| 3968: 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 29 38 02 06 , block BLOB)8.. +| 3984: 17 11 11 08 5f 74 61 62 6c 65 74 31 74 31 43 52 ...._tablet1t1CR +| 4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 35 LE t1 USING fts5 +| 4032: 28 61 2c 62 2c 63 29 00 00 00 39 00 00 00 00 00 (a,b,c)...9..... +| page 3 offset 8192 +| 0: 0d 00 00 00 03 0c 94 00 0f e6 0f ef 0c 94 00 00 ................ +| 3216: 00 00 00 00 86 4a 84 80 80 80 80 01 04 00 8d 18 .....J.......... +| 3232: 00 00 03 2b 02 30 30 01 02 06 01 02 06 01 02 06 ...+.00......... +| 3248: 1f 02 03 01 02 03 01 02 03 01 08 32 30 31 36 30 ...........20160 +| 3264: 36 30 39 01 02 07 01 02 07 01 02 07 01 01 34 01 609...........4. +| 3280: 02 05 01 02 05 01 02 05 01 01 35 01 02 04 01 02 ..........5..... +| 3296: 04 01 02 04 02 07 30 30 30 30 30 30 30 1c 02 04 ......0000000... +| 3312: 01 02 04 01 02 04 01 06 62 69 6e 61 72 79 03 06 ........binary.. +| 3328: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3344: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3360: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 ................ +| 3376: 03 06 01 02 02 03 06 01 02 02 01 08 63 6f 6d 70 ............comp +| 3392: 69 6c 65 72 01 02 02 01 02 02 01 02 02 01 06 64 iler...........d +| 3408: 62 73 74 61 74 07 02 03 01 02 03 01 02 03 02 04 bstat........... +| 3424: 65 62 75 67 04 02 02 01 02 02 01 02 02 01 06 65 ebug...........e +| 3440: 6e 61 62 6c 65 07 02 02 01 02 02 01 02 02 01 02 nable........... +| 3456: 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 ................ +| 3472: 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 ................ +| 3488: 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 ................ +| 3504: 02 01 02 02 02 08 78 74 65 6e 73 69 6f 6e 1f 02 ......xtension.. +| 3520: 04 01 02 04 01 02 04 01 04 66 74 73 34 0a 02 03 .........fts4... +| 3536: 01 02 03 01 02 03 04 01 35 0d 02 03 01 02 03 01 ........5....... +| 3552: 02 03 01 03 67 63 63 01 02 03 01 02 03 01 02 03 ....gcc......... +| 3568: 02 06 65 6f 70 6f 6c 79 10 02 03 01 02 03 01 02 ..eopoly........ +| 3584: 03 01 05 6a 73 6f 6e 31 13 02 03 01 02 03 01 02 ...json1........ +| 3600: 03 01 04 6c 6f 61 64 1f 02 03 01 02 03 01 02 03 ...load......... +| 3616: 01 03 6d 61 78 1c 02 02 01 02 02 01 02 02 02 05 ..max........... +| 3632: 65 6d 6f 72 79 1c 02 03 01 02 03 01 02 03 04 04 emory........... +| 3648: 73 79 73 35 16 02 03 01 02 03 01 02 03 01 06 6e sys5...........n +| 3664: 6f 63 61 73 65 02 06 01 02 02 03 06 01 02 02 03 ocase........... +| 3680: 06 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 ................ +| 3696: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3712: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3728: 02 01 04 6f 6d 69 74 1f 02 02 01 02 02 01 02 02 ...omit......... +| 3744: 01 05 72 74 72 65 65 19 02 03 01 02 03 01 02 03 ..rtree......... +| 3760: 04 02 69 6d 01 06 01 02 02 03 06 01 02 02 03 06 ..im............ +| 3776: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3792: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3808: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 ................ +| 3824: 01 0a 74 68 72 65 61 64 73 61 66 65 22 02 02 01 ..threadsafe.... +| 3840: 02 02 01 02 01 01 04 76 74 61 62 07 02 04 01 02 .......vtab..... +| 3856: 04 01 02 04 01 01 78 01 06 01 01 02 01 06 01 01 ......x......... +| 3872: 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 ................ +| 3888: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 3904: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 3920: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 ................ +| 3936: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 ................ +| 3952: 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 ................ +| 3968: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 3984: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 4000: 01 01 02 01 06 01 01 02 01 06 01 ec 02 01 06 01 ................ +| 4016: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 ................ +| 4032: 02 01 06 01 01 02 01 06 01 01 01 04 15 13 0c 0c ................ +| 4048: 12 44 13 11 0f 47 13 0f 0c 0e 11 10 0f 0e 10 0f .D...G.......... +| 4064: 44 0f 10 40 15 0f 07 01 03 00 14 24 5a 24 24 0f D..@.......$Z$$. +| 4080: 0a 03 00 24 00 00 00 00 01 01 01 00 01 01 01 01 ...$............ +| page 4 offset 12288 +| 0: 0a 00 00 00 01 0f fa 00 0f fa 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 05 04 09 0c 01 02 ................ +| page 5 offset 16384 +| 0: 0d 00 00 00 24 0c 0a 00 0f d8 0f af 0f 86 0f 74 ....$..........t +| 16: 0f 61 0f 4e 0f 2f 0f 0f 0e ef 0e d7 0e be 0e a5 .a.N./.......... +| 32: 0e 8d 0e 74 0e 5b 0e 40 0e 24 0e 08 0d ef 0d d5 ...t.[.@.$...... +| 48: 0d bb 0d a0 0d 84 0d 68 0d 4f 0d 35 0d 1b 0c fb .......h.O.5.... +| 64: 0c da 0c b9 0c 99 0c 78 0c 57 0c 3e 0c 24 0c 0a .......x.W.>.$.. +| 3072: 00 00 00 00 00 00 00 00 00 00 18 24 05 00 25 0f ...........$..%. +| 3088: 19 54 48 52 45 41 44 53 41 46 45 3d 30 58 42 49 .THREADSAFE=0XBI +| 3104: 4e 41 52 59 18 23 05 00 25 0f 19 54 48 52 45 41 NARY.#..%..THREA +| 3120: 44 53 41 46 45 3d 30 58 4e 4f 43 41 53 45 17 22 DSAFE=0XNOCASE.. +| 3136: 05 00 25 0f 17 54 48 52 45 41 44 53 41 46 45 3d ..%..THREADSAFE= +| 3152: 30 58 52 54 52 49 4d 1f 21 05 00 33 0f 19 4f 4d 0XRTRIM.!..3..OM +| 3168: 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 4f IT LOAD EXTENSIO +| 3184: 4e 58 42 49 4e 41 52 59 1f 20 05 00 33 0f 19 4f NXBINARY. ..3..O +| 3200: 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 MIT LOAD EXTENSI +| 3216: 4f 4e 58 4e 4f 43 41 53 45 1e 1f 05 00 33 0f 17 ONXNOCASE....3.. +| 3232: 4f 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 OMIT LOAD EXTENS +| 3248: 49 4f 4e 58 52 54 52 49 4d 1f 1e 05 00 33 0f 19 IONXRTRIM....3.. +| 3264: 4d 41 58 20 4d 45 4d 4f 52 59 60 35 30 30 30 30 MAX MEMORY`50000 +| 3280: 30 30 30 58 42 49 4e 41 52 59 1f 1d 05 00 33 0f 000XBINARY....3. +| 3296: 19 4d 41 58 20 4d 44 4d 4f 52 59 3d 35 30 30 30 .MAX MDMORY=5000 +| 3312: 30 30 30 30 58 4e 4f 43 41 53 45 1e 1c 05 00 33 0000XNOCASE....3 +| 3328: 0f 17 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 ..MAX MEMORY=500 +| 3344: 30 30 30 30 30 58 52 54 52 49 4d 18 1b 05 00 25 00000XRTRIM....% +| 3360: 0f 19 45 4e 41 42 4c 45 20 52 54 52 45 45 58 42 ..ENABLE RTREEXB +| 3376: 49 4e 41 52 59 18 1a 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3392: 4c 45 20 52 54 52 45 45 58 4e 4f 43 41 53 45 17 LE RTREEXNOCASE. +| 3408: 19 05 00 25 0f 17 45 4e 41 42 4c 45 20 52 54 52 ...%..ENABLE RTR +| 3424: 45 45 58 52 54 52 49 4d 1a 18 05 00 29 0f 19 45 EEXRTRIM....)..E +| 3440: 4e 41 42 4c 45 20 4d 45 4d 53 59 53 35 58 42 49 NABLE MEMSYS5XBI +| 3456: 4e 41 52 59 1a 17 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3472: 45 20 4d 45 4d 53 59 53 35 58 4e 4f 43 41 53 45 E MEMSYS5XNOCASE +| 3488: 19 16 05 00 29 0f 17 45 4e 41 42 4c 45 20 4d 45 ....)..ENABLE ME +| 3504: 4d 53 59 53 35 58 52 54 52 49 4d 18 15 05 00 25 MSYS5XRTRIM....% +| 3520: 0f 19 45 4e 41 42 4c 45 20 4a 53 4f 4e 31 58 42 ..ENABLE JSON1XB +| 3536: 49 4e 41 52 59 18 14 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3552: 4c 45 20 4a 53 4f 4e 31 58 4e 4f 43 41 53 45 17 LE JSON1XNOCASE. +| 3568: 13 05 00 25 0f 17 45 4e 41 42 4c 45 20 4a 53 4f ...%..ENABLE JSO +| 3584: 4e 31 58 52 54 52 49 4d 1a 12 05 00 29 0f 19 45 N1XRTRIM....)..E +| 3600: 4e 41 42 4c 45 20 47 45 4f 50 4f 4c 59 58 42 49 NABLE GEOPOLYXBI +| 3616: 4e 41 52 59 1a 11 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3632: 45 20 47 45 4f 50 4f 4c 59 58 4e 4f 43 41 53 45 E GEOPOLYXNOCASE +| 3648: 19 10 05 00 29 0f 17 45 4e 41 42 4c 45 20 47 45 ....)..ENABLE GE +| 3664: 4f 50 4f 4c 59 58 52 54 52 49 4d 17 0f 05 00 23 OPOLYXRTRIM....# +| 3680: 0f 19 45 4e 41 42 4c 45 20 46 54 53 35 58 42 49 ..ENABLE FTS5XBI +| 3696: 4e 41 52 59 17 0e 05 00 23 0f 19 45 4e 41 42 4c NARY....#..ENABL +| 3712: 45 20 46 54 53 35 58 4e 4f 43 41 53 45 16 0d 05 E FTS5XNOCASE... +| 3728: 00 23 0f 17 45 4e 41 42 4c 45 20 46 54 53 35 58 .#..ENABLE FTS5X +| 3744: 52 54 52 49 4d 17 0c 05 00 23 0f 19 45 4e 41 42 RTRIM....#..ENAB +| 3760: 4c 45 20 46 54 53 34 58 42 49 4e 41 52 59 17 0b LE FTS4XBINARY.. +| 3776: 05 00 23 0f 19 45 4e 41 42 4c 45 20 46 54 53 34 ..#..ENABLE FTS4 +| 3792: 58 4e 4f 43 41 53 45 16 0a 05 00 23 0f 17 45 4e XNOCASE....#..EN +| 3808: 41 42 4c 45 20 46 54 53 34 58 52 54 52 49 4d 1e ABLE FTS4XRTRIM. +| 3824: 09 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3840: 54 41 54 20 56 54 41 42 58 42 49 4e 41 52 59 1e TAT VTABXBINARY. +| 3856: 08 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3872: 54 41 54 20 56 54 41 42 58 4e 4f 43 41 53 45 1d TAT VTABXNOCASE. +| 3888: 07 05 00 31 0f 17 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3904: 54 41 54 20 56 54 41 42 58 52 54 52 49 4d 11 06 TAT VTABXRTRIM.. +| 3920: 05 00 17 0f 19 44 45 42 55 47 58 42 49 4e 41 52 .....DEBUGXBINAR +| 3936: 59 11 05 05 00 17 0f 19 44 45 42 55 47 58 4e 4f Y.......DEBUGXNO +| 3952: 43 41 53 45 10 04 05 00 17 0f 17 44 45 42 55 47 CASE.......DEBUG +| 3968: 58 52 54 52 49 4d 27 03 05 00 43 0f 19 43 4f 4d XRTRIM'...C..COM +| 3984: 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e 30 20 PILER=gcc-5.4.0 +| 4000: 32 30 31 36 30 36 30 39 58 42 49 4e 41 52 59 27 20160609XBINARY' +| 4016: 02 05 00 43 0f 19 43 4f 4d 50 49 4c 45 52 3d 67 ...C..COMPILER=g +| 4032: 63 63 2d 35 2e 34 2e 30 20 32 30 31 36 30 36 30 cc-5.4.0 2016060 +| 4048: 39 58 4e 4f 43 41 53 45 26 01 05 00 43 0f 17 43 9XNOCASE&...C..C +| 4064: 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e OMPILER=gcc-5.4. +| 4080: 30 20 32 30 31 36 30 36 30 39 58 52 54 52 49 4d 0 20160609XRTRIM +| page 6 offset 20480 +| 0: 0d 00 00 00 24 0e e0 00 0f f8 0f f0 0f e8 0f e0 ....$........... +| 16: 0f d8 0f d0 0f c8 0f c0 0f b8 0f b0 0f a8 0f a0 ................ +| 32: 0f 98 0f 90 0f 88 0f 80 0f 78 0f 70 0f 68 0f 60 .........x.p.h.` +| 48: 0f 58 0f 50 0f 48 0f 40 0f 38 0f 30 0f 28 0f 20 .X.P.H.@.8.0.(. +| 64: 0f 18 0f 10 0f 08 0f 00 0e f8 0e f0 0e e8 0e e0 ................ +| 3808: 06 24 03 00 12 02 01 01 06 23 03 00 12 02 01 01 .$.......#...... +| 3824: 06 22 03 00 12 02 01 01 06 21 03 00 12 03 01 01 .........!...... +| 3840: 06 20 03 00 12 03 01 01 06 1f 03 00 12 03 01 01 . .............. +| 3856: 06 1e 03 00 12 03 01 01 06 1d 03 00 12 03 01 01 ................ +| 3872: 06 1c 03 00 12 03 01 01 16 1b 03 00 12 02 01 01 ................ +| 3888: 06 1a 03 00 12 02 01 01 06 19 03 00 12 02 01 01 ................ +| 3904: 06 18 03 00 12 02 01 01 06 17 03 00 12 02 01 01 ................ +| 3920: 06 16 03 00 12 02 01 01 06 15 03 00 12 02 01 01 ................ +| 3936: 06 14 03 00 12 02 01 01 06 13 03 00 12 02 01 01 ................ +| 3952: 06 12 03 00 12 02 01 01 06 11 03 00 12 02 01 01 ................ +| 3968: 06 10 03 00 12 02 01 01 06 0f 03 00 12 02 01 01 ................ +| 3984: 06 0e 03 00 12 02 01 01 06 0d 03 00 12 02 01 01 ................ +| 4000: 06 0c 03 00 12 02 01 01 06 0b 03 00 12 02 01 01 ................ +| 4016: 06 0a 03 00 12 02 01 01 05 09 03 00 12 03 01 01 ................ +| 4032: 06 08 03 00 12 03 01 01 06 07 03 10 12 03 01 01 ................ +| 4048: 06 06 03 00 12 01 01 01 06 05 03 00 12 01 01 01 ................ +| 4064: 06 04 03 00 12 01 01 01 06 03 03 00 12 05 f1 01 ................ +| 4080: 06 02 03 00 12 06 01 01 06 01 03 00 12 06 01 01 ................ +| page 7 offset 24576 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| page 8 offset 28672 +| 0: 0d 00 00 00 03 0f d6 00 0f f4 0f e9 0f d6 00 00 ................ +| 4048: 00 00 00 00 00 00 11 03 02 2b 69 6e 74 65 67 72 .........+integr +| 4064: 69 74 79 2d 63 68 65 63 6b 09 02 02 1b 72 65 62 ity-check....reb +| 4080: 75 69 6c 64 0a 01 02 1d 6f 70 74 69 6d 69 7a 65 uild....optimize +| end crash-fd2a1313e5b5e9.db +}]} {} + +do_catchsql_test 38.1 { + UPDATE t1 SET b=quote(zeroblob(200)) WHERE t1 MATCH 'thread*'; +} {/*malformed database schema*/} + +#------------------------------------------------------------------------- +reset_db +do_test 39.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 24576 pagesize 4096 filename crash-e650fe95502908.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 06 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 06 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 96: 00 00 00 00 0d 00 00 00 06 0e 0f 00 0f aa 0f 53 ...............S +| 112: 0e e8 0e 8b 0e 33 0e 0f 00 00 00 00 00 00 00 00 .....3.......... +| 3584: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 22 ................ +| 3600: 06 06 17 11 11 01 31 74 61 62 6c 65 62 62 62 62 ......1tablebbbb +| 3616: 06 43 52 45 41 54 45 20 54 41 42 4c 45 20 62 62 .CREATE TABLE bb +| 3632: 28 61 29 56 05 06 17 1f 1f 01 7d 74 61 62 6c 65 (a)V.......table +| 3648: 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 6f 6e 66 t1_configt1_conf +| 3664: 69 67 05 43 52 45 41 54 45 20 54 41 42 4c 45 20 ig.CREATE TABLE +| 3680: 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b 20 50 52 't1_config'(k PR +| 3696: 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 20 57 49 IMARY KEY, v) WI +| 3712: 54 48 4f 55 54 20 52 4f 57 49 44 5b 04 07 17 21 THOUT ROWID[...! +| 3728: 21 01 81 01 74 61 62 6c 65 74 31 5f 64 6f 63 73 !...tablet1_docs +| 3744: 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 04 43 52 izet1_docsize.CR +| 3760: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 64 EATE TABLE 't1_d +| 3776: 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 54 45 47 ocsize'(id INTEG +| 3792: 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 ER PRIMARY KEY, +| 3808: 73 7a 20 42 4c 4f 42 29 69 03 07 17 19 19 01 81 sz BLOB)i....... +| 3824: 2d 74 61 62 6c 65 74 31 5f 69 64 78 74 31 5f 69 -tablet1_idxt1_i +| 3840: 64 78 03 43 52 45 41 54 45 20 54 41 42 4c 45 20 dx.CREATE TABLE +| 3856: 27 74 31 5f 69 64 78 27 28 73 65 67 69 64 2c 20 't1_idx'(segid, +| 3872: 74 65 72 6d 2c 20 70 67 6e 6f 2c 20 50 52 49 4d term, pgno, PRIM +| 3888: 41 52 59 20 4b 45 59 28 73 65 67 69 64 2c 20 74 ARY KEY(segid, t +| 3904: 65 72 6d 29 29 20 57 49 54 48 4f 55 54 20 52 4f erm)) WITHOUT RO +| 3920: 57 49 44 55 02 07 17 1b 1b 01 81 01 74 61 62 6c WIDU........tabl +| 3936: 65 74 31 5f 64 61 74 61 74 31 5f 64 61 74 61 02 et1_datat1_data. +| 3952: 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 CREATE TABLE 't1 +| 3968: 5f 64 61 74 61 27 28 69 64 20 49 4e 54 45 47 45 _data'(id INTEGE +| 3984: 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 62 R PRIMARY KEY, b +| 4000: 6c 6f 63 6b 20 42 4c 4f 42 29 54 01 07 17 11 11 lock BLOB)T..... +| 4016: 08 81 15 74 61 62 6c 65 74 31 74 31 43 52 45 41 ...tablet1t1CREA +| 4032: 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45 TE VIRTUAL TABLE +| 4048: 20 74 31 20 55 53 49 4e 47 20 66 74 73 35 28 61 t1 USING fts5(a +| 4064: 2c 62 2c 70 72 65 66 69 78 3d 22 32 2c 32 2c 33 ,b,prefix=.2,2,3 +| 4080: 2c 34 22 2c 20 63 6f 6e 74 65 6e 74 3d 22 22 29 ,4., content=..) +| page 2 offset 4096 +| 0: 0d 0b 6a 00 37 09 4c 02 0f e7 09 4c 0f c6 0f a4 ..j.7.L....L.... +| 16: 0f 88 0f 6d 0f 4b 0f 2c 0f 0e 0e ec 0e cd 0e ad ...m.K.,........ +| 32: 0e 8e 0e 6c 0e 4b 0e 29 0e 08 0d e6 0d c4 0d b5 ...l.K.)........ +| 48: 0d 97 0d 76 0d 54 0d 31 0d 15 0c f3 0c d3 0c b5 ...v.T.1........ +| 64: 0c 95 0c 73 0c 54 0c 32 0c 10 0b ee 0b cc 0b b0 ...s.T.2........ +| 80: 0b 8d 0b 7e 0b 48 0b 2e 0b 0b 0a ef 0a cc 0a ad ...~.H.......... +| 96: 0a 8c 0a 6d 0a 4d 0a 2b 0a 0c 09 ec 09 ca 09 a8 ...m.M.+........ +| 112: 09 86 09 63 0f f1 00 00 00 00 00 00 00 00 00 00 ...c............ +| 2368: 00 00 00 00 00 00 00 00 00 00 00 00 15 0a 03 00 ................ +| 2384: 30 00 00 00 01 01 03 35 00 03 01 01 12 02 01 12 0......5........ +| 2400: 03 01 11 1c 8c 80 80 80 80 10 03 00 3e 00 00 00 ............>... +| 2416: 17 01 05 05 34 74 61 62 6c 03 02 03 01 04 77 68 ....4tabl.....wh +| 2432: 65 72 03 02 06 09 1b 8c 80 80 80 80 0f 03 00 3c er.............< +| 2448: 00 00 00 16 05 34 66 74 73 34 03 02 02 01 04 6e .....4fts4.....n +| 2464: 75 6d 62 03 06 01 04 09 1b 8c 80 80 80 80 0e 03 umb............. +| 2480: 00 3c 00 00 00 16 04 33 74 68 65 03 06 01 01 04 .<.....3the..... +| 2496: 01 03 77 68 65 03 02 04 04 0a 1b 8c 80 80 80 80 ..whe........... +| 2512: 0d 03 00 3c 00 00 00 16 04 33 6e 75 6d 03 06 01 ...<.....3num... +| 2528: 01 05 01 03 74 61 62 03 02 03 04 0a 19 8c 80 80 ....tab......... +| 2544: 80 80 0c 03 00 38 00 00 00 14 03 32 77 68 03 02 .....8.....2wh.. +| 2560: 04 00 04 33 66 74 73 03 02 02 04 07 18 8c 80 80 ...3fts......... +| 2576: 80 80 0b 03 00 36 00 00 00 13 03 32 74 61 03 02 .....6.....2ta.. +| 2592: 03 02 01 68 03 06 01 01 04 04 07 1b 8c 80 80 80 ...h............ +| 2608: 80 0a 03 00 3c 00 00 00 16 03 32 6e 75 03 06 01 ....<.....2nu... +| 2624: 01 05 01 02 6f 66 03 06 01 01 06 04 09 19 8c 80 ....of.......... +| 2640: 80 80 80 09 03 00 38 00 00 00 14 03 32 66 74 03 ......8.....2ft. +| 2656: 02 02 01 02 69 73 03 06 01 01 03 04 07 18 8c 80 ....is.......... +| 2672: 80 80 80 08 03 00 36 00 00 00 13 02 31 74 03 08 ......6.....1t.. +| 2688: 03 01 01 04 01 01 77 03 02 04 04 09 1a 8c 80 80 ......w......... +| 2704: 80 80 07 03 00 3a ff 00 00 15 02 31 6e 03 08 01 .....:.....1n... +| 2720: 01 02 05 01 01 6f 03 06 01 01 06 04 09 18 8c 80 .....o.......... +| 2736: 80 80 80 06 03 00 36 00 00 00 13 04 02 31 66 03 ......6......1f. +| 2752: 02 02 01 01 69 03 06 01 01 03 05 06 1c 8c 80 80 ....i........... +| 2768: 80 80 05 03 00 3e 00 00 00 17 04 30 74 68 65 03 .....>.....0the. +| 2784: 06 00 f1 04 01 05 77 68 65 72 65 03 02 04 0a 15 ......where..... +| 2800: 8c 80 80 80 80 04 03 00 30 00 00 00 11 01 01 06 ........0....... +| 2816: 06 30 74 61 62 6c 65 03 02 03 07 1c 8c 80 80 80 .0table......... +| 2832: 80 03 03 00 3e 00 00 00 17 07 30 6e 75 6d 62 65 ....>.....0numbe +| 2848: 72 03 06 01 01 05 01 02 6f 66 03 06 04 0d 13 8c r.......of...... +| 2864: 80 80 80 80 02 03 00 2c 00 00 00 0f 01 01 03 02 .......,........ +| 2880: 30 6e 03 06 01 01 02 07 1b 8c 80 80 80 80 01 03 0n.............. +| 2896: 00 3c 00 00 00 16 08 30 66 74 73 34 61 75 78 03 .<.....0fts4aux. +| 2912: 02 02 01 02 69 73 03 06 04 0c 00 00 00 14 2a 00 ....is........*. +| 2928: 00 00 01 01 02 24 00 02 01 01 12 02 01 12 08 88 .....$.......... +| 2944: 80 80 80 80 12 03 00 16 00 00 00 05 02 1c 88 80 ................ +| 2960: 80 80 80 11 03 00 3e 00 00 00 17 05 34 72 6f 77 ......>.....4row +| 2976: 73 02 06 01 01 05 01 04 74 68 65 72 02 02 04 0b s.......ther.... +| 2992: 15 88 80 80 80 80 10 03 00 30 00 00 00 11 02 01 .........0...... +| 3008: 01 07 05 34 62 65 74 77 02 02 04 08 1b 88 80 80 ...4betw........ +| 3024: 80 80 0f 03 00 3c 00 00 00 16 04 04 33 72 6f 77 .....<......3row +| 3040: 02 06 01 01 05 01 03 74 68 65 02 08 05 0a 1b 88 .......the...... +| 3056: 80 80 80 80 0e 03 00 3c 00 00 00 16 01 01 02 04 .......<........ +| 3072: 33 61 72 65 02 02 03 01 03 62 65 74 02 02 07 08 3are.....bet.... +| 3088: 1b 88 80 80 80 80 0d 03 00 3c 00 00 00 16 03 32 .........<.....2 +| 3104: 74 68 02 08 02 01 01 07 00 04 33 61 6e 64 02 06 th........3and.. +| 3120: 04 0a 1b 88 80 80 80 80 0c 03 00 3c 00 00 00 16 ...........<.... +| 3136: 03 32 69 6e 02 06 01 01 06 01 02 72 6f 02 06 01 .2in.......ro... +| 3152: 01 05 04 09 18 88 80 80 80 80 0b 03 00 36 00 00 .............6.. +| 3168: 00 13 02 03 32 61 72 02 02 03 01 02 62 65 02 02 ....2ar.....be.. +| 3184: 04 05 07 1b 88 80 80 80 80 0a 03 00 3c 00 00 00 ............<... +| 3200: 16 02 31 74 02 08 02 01 01 07 00 03 32 61 6e 02 ..1t........2an. +| 3216: 06 01 01 04 09 19 88 80 80 80 80 09 03 00 38 00 ..............8. +| 3232: 00 00 14 02 31 6e 02 06 01 01 03 01 01 72 02 06 ....1n.......r.. +| 3248: 01 01 05 04 08 17 88 80 80 80 80 08 03 00 34 00 ..............4. +| 3264: 00 00 12 02 31 62 02 02 04 01 01 69 02 06 01 01 ....1b.....i.... +| 3280: 06 04 06 19 88 80 80 80 80 07 03 00 38 00 00 00 ............8... +| 3296: 14 04 02 31 32 02 02 05 01 01 61 02 08 03 01 01 ...12.....a..... +| 3312: 02 05 06 1b 88 80 80 80 80 06 03 00 3c 00 00 00 ............<... +| 3328: 16 06 30 74 68 65 72 65 02 02 02 00 02 31 31 02 ..0there.....11. +| 3344: 06 01 01 04 0a 15 88 80 80 80 80 05 03 00 30 00 ..............0. +| 3360: 00 00 11 01 01 05 04 30 74 68 65 02 06 01 01 07 .......0the..... +| 3376: 07 1c 88 80 80 80 80 04 03 00 3e 00 00 00 17 01 ..........>..... +| 3392: 01 06 02 30 6e 02 06 01 01 03 01 04 72 6f 77 73 ...0n.......rows +| 3408: 02 06 07 08 1b 88 80 80 80 80 03 03 00 3c 00 00 .............<.. +| 3424: 00 16 08 30 62 65 74 77 65 65 6e 02 02 04 01 02 ...0between..... +| 3440: 69 6e 02 06 04 0c 1a 88 80 80 80 80 02 03 00 3a in.............: +| 3456: 00 00 00 15 04 30 61 6e 64 02 06 01 01 02 02 02 .....0and....... +| 3472: 72 65 02 02 03 04 0a 17 88 80 80 80 80 01 03 00 re.............. +| 3488: 34 00 00 00 12 02 30 31 02 06 01 01 04 01 01 32 4.....01.......2 +| 3504: 02 02 05 04 08 08 84 80 80 80 81 12 03 00 16 00 ................ +| 3520: 00 00 05 04 1b 84 61 80 80 80 11 03 00 3c 00 00 ......a......<.. +| 3536: 00 16 05 34 74 61 62 6c 01 06 01 01 05 02 03 65 ...4tabl.......e +| 3552: 72 6d 01 02 04 0b 1b 84 80 80 80 80 10 03 00 3c rm.............< +| 3568: 00 00 00 16 05 34 65 61 63 68 01 02 03 01 04 70 .....4each.....p +| 3584: 72 65 73 01 02 05 04 09 1a 84 80 80 80 80 0f 03 res............. +| 3600: 00 3a 00 00 00 15 04 33 74 65 72 01 02 04 02 02 .:.....3ter..... +| 3616: 68 65 01 06 01 01 03 04 08 1b 84 80 80 80 80 0e he.............. +| 3632: 03 00 3c 00 00 00 16 04 33 70 72 65 01 02 05 01 ..<.....3pre.... +| 3648: 03 74 61 62 01 06 01 01 05 04 08 1a 84 80 80 80 .tab............ +| 3664: 80 0d 03 00 3a 00 00 00 15 04 33 66 6f 72 01 02 ....:.....3for.. +| 3680: 02 02 02 74 73 01 06 01 01 04 04 08 1b 84 80 80 ...ts........... +| 3696: 80 80 0c 03 00 3c 00 00 00 16 03 32 74 68 01 06 .....<.....2th.. +| 3712: 01 01 03 00 04 33 65 61 63 01 02 03 04 09 18 84 .....3eac....... +| 3728: 80 80 80 80 0b 03 00 36 00 00 00 13 03 32 74 71 .......6.....2tq +| 3744: 01 06 01 01 05 02 01 65 01 02 04 04 09 19 84 80 .......e........ +| 3760: 80 80 80 0a 03 00 38 00 00 00 14 03 32 69 6e 01 ......8.....2in. +| 3776: 06 01 01 02 01 02 70 72 01 02 05 0b 89 18 84 80 ......pr........ +| 3792: 80 80 80 09 03 00 36 00 00 00 13 03 32 66 6f 01 ......6.....2fo. +| 3808: 02 02 02 01 74 01 06 01 01 04 04 07 1b 84 80 80 ....t........... +| 3824: 80 80 08 03 00 3c 00 00 00 16 02 31 74 01 0a 04 .....<.....1t... +| 3840: 01 01 03 04 00 03 32 65 61 01 02 03 04 0a 17 84 ......2ea....... +| 3856: 80 80 80 80 07 03 00 34 00 00 00 12 02 31 69 01 .......4.....1i. +| 3872: 06 01 01 02 01 01 70 01 02 05 04 08 18 84 80 80 ......p......... +| 3888: 80 80 06 03 00 36 00 00 00 13 02 31 65 01 02 03 .....6.....1e... +| 3904: 01 01 66 01 08 02 01 01 04 04 06 1b 84 80 80 80 ..f............. +| 3920: 80 05 03 00 3c 00 00 00 16 05 30 74 65 72 6d 01 ....<.....0term. +| 3936: 02 04 02 02 68 65 01 06 01 01 03 04 09 14 84 80 ....he.......... +| 3952: 80 80 80 04 03 00 2e 00 00 00 10 06 30 74 61 62 ............0tab +| 3968: 6c 65 01 06 01 01 05 04 15 84 80 80 80 80 03 03 le.............. +| 3984: 00 30 00 00 00 11 02 08 30 70 72 65 73 65 6e 74 .0......0present +| 4000: 01 02 05 05 1b 84 80 80 80 80 02 03 00 3c 00 00 .............<.. +| 4016: 00 16 04 30 66 74 73 01 06 01 01 04 01 02 69 6e ...0fts.......in +| 4032: 01 06 01 01 04 0a 1a 84 80 80 80 80 01 03 00 3a ...............: +| 4048: 00 00 00 15 05 30 65 61 63 f4 01 02 03 01 03 66 .....0eac......f +| 4064: 6f 72 01 02 02 04 09 06 01 03 00 12 03 0b 0f 00 or.............. +| 4080: 00 08 8c 80 80 80 80 11 03 00 16 00 00 00 05 04 ................ +| page 3 offset 8192 +| 0: 0a 00 00 00 32 0e 4f 00 0f fa 0f f1 0f e9 0f e1 ....2.O......... +| 16: 0f d8 0f d1 0f c9 0f c1 0f b9 0f b1 0f a9 0f a0 ................ +| 32: 0f 98 0f 90 0f 87 0f 80 0f 78 0f 71 0f 68 0f 5f .........x.q.h._ +| 48: 0f 56 0f 4d 0f 41 0f 38 0f 2f 0f 26 0f 1d 0f 13 .V.M.A.8./.&.... +| 64: 0f 0a 0f 01 0e f7 0e ee 0e e6 0e dd 0e d6 0e cd ................ +| 80: 0e c3 0e ba 0e b0 0e a8 0e 9f 0e 96 0e 8e 0e 85 ................ +| 96: 0e 7c 0e 73 0e 6a 0e 60 0e 58 0e 4f 00 00 00 00 .|.s.j.`.X.O.... +| 3648: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 08 ................ +| 3664: 04 01 10 01 03 34 74 20 07 04 01 0e 01 03 34 1e .....4t ......4. +| 3680: 09 04 01 12 01 03 33 74 68 1c 08 04 01 10 01 03 ......3th....... +| 3696: 33 6e 1a 08 04 01 10 01 03 32 77 18 08 04 01 10 3n.......2w..... +| 3712: 01 03 32 74 16 08 04 01 10 01 03 32 6e 14 07 04 ..2t.......2n... +| 3728: 01 0e 01 03 32 12 08 04 01 10 01 03 31 74 10 08 ....2.......1t.. +| 3744: 04 01 10 01 03 31 6e 0e 07 04 01 0e 01 03 31 0c .....1n.......1. +| 3760: 09 04 01 12 01 03 30 74 68 0a 08 04 01 10 01 03 ......0th....... +| 3776: 30 74 08 09 04 01 12 01 03 30 6e 75 06 08 04 01 0t.......0nu.... +| 3792: 10 01 03 30 6e 04 06 04 01 0c 01 03 02 08 04 01 ...0n........... +| 3808: 10 01 02 34 72 22 07 04 01 0e 01 02 34 20 08 04 ...4r.......4 .. +| 3824: 01 10 01 02 33 72 1e 09 04 01 12 01 02 33 61 72 ....3r.......3ar +| 3840: 1c 08 04 01 10 01 02 32 74 1a 08 04 01 10 b3 02 .......2t....... +| 3856: 32 69 18 09 04 01 12 01 02 32 61 72 16 08 04 01 2i.......2ar.... +| 3872: 10 01 02 31 74 14 08 04 01 10 01 02 31 6e 12 08 ...1t.......1n.. +| 3888: 04 01 10 01 02 31 62 10 08 04 01 10 01 02 31 32 .....1b.......12 +| 3904: 0e 0b 04 01 16 01 02 30 74 68 65 72 0c 08 04 01 .......0ther.... +| 3920: 10 01 02 30 74 0a 08 04 01 10 01 02 30 6e 08 08 ...0t.......0n.. +| 3936: 04 01 10 01 02 30 62 06 08 04 01 10 01 02 30 61 .....0b.......0a +| 3952: 05 06 04 01 0c 01 02 02 07 04 09 10 01 34 74 22 .............4t. +| 3968: 06 04 09 0e 01 34 20 08 04 09 12 01 33 74 65 1e .....4 .....3te. +| 3984: 07 04 09 10 01 33 70 1c 07 04 09 10 01 33 66 1a .....3p......3f. +| 4000: 08 04 09 12 01 32 74 68 18 07 04 09 10 01 32 2d .....2th......2- +| 4016: 16 07 04 09 10 01 32 69 14 07 04 09 10 01 32 66 ......2i......2f +| 4032: 12 07 04 09 10 01 31 74 10 07 04 09 10 01 31 69 ......1t......1i +| 4048: 0e 06 04 09 0e 01 31 0c 08 04 09 12 01 30 74 65 ......1......0te +| 4064: 0a 07 04 09 10 01 30 74 08 07 04 09 10 01 30 70 ......0t......0p +| 4080: 06 08 04 09 12 01 30 66 74 04 05 04 09 0c 01 02 ......0ft....... +| page 4 offset 12288 +| 0: 0d 00 00 00 03 0f eb 00 0f f9 0f f2 0f eb 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 05 03 03 00 10 ................ +| 4080: 03 05 05 02 03 00 10 04 06 05 01 03 00 10 04 03 ................ +| page 5 offset 16384 +| 0: 0a 00 00 00 02 0f eb 00 0f eb 0f f4 00 00 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 08 03 15 01 70 ...............p +| 4080: 67 73 7a 18 0b 03 1b 01 76 65 72 73 69 6f 6e 04 gsz.....version. +| page 6 offset 20480 +| 0: 0d 00 00 00 03 0f f2 00 0f fc 0f f7 0f f2 00 00 ................ +| 4080: 00 00 03 03 02 01 03 03 02 02 01 02 02 01 02 09 ................ +| end crash-e650fe95502908.db +}]} {} + +do_execsql_test 39.1 { + SELECT rowid FROM t1('t*'); +} {1 2 3} + +#------------------------------------------------------------------------- +reset_db +do_test 40.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 24576 pagesize 4096 filename crash2.txt.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 06 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 06 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 96: 00 00 00 00 0d 00 00 00 06 0e 0f 00 0f aa 0f 53 ...............S +| 112: 0e e8 0e 8b 0e 33 0e 0f 00 00 00 00 00 00 00 00 .....3.......... +| 3584: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 22 ................ +| 3600: 06 06 17 11 11 01 31 74 61 62 6c 65 62 62 62 62 ......1tablebbbb +| 3616: 06 43 52 45 41 54 45 20 54 41 42 4c 45 20 62 62 .CREATE TABLE bb +| 3632: 28 61 29 56 05 06 17 1f 1f 01 7d 74 61 52 6c 65 (a)V.......taRle +| 3648: 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 6f 6e 66 t1_configt1_conf +| 3664: 69 67 05 43 52 45 41 54 45 20 54 41 42 4c 45 20 ig.CREATE TABLE +| 3680: 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b 20 50 52 't1_config'(k PR +| 3696: 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 20 57 49 IMARY KEY, v) WI +| 3712: 54 48 4f 55 54 20 52 4f 57 49 44 5b 04 07 17 21 THOUT ROWID[...! +| 3728: 21 01 81 01 74 61 62 6c 65 74 31 5f 64 6f 73 73 !...tablet1_doss +| 3744: 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 04 43 52 izet1_docsize.CR +| 3760: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 64 EATE TABLE 't1_d +| 3776: 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 54 45 47 ocsize'(id INTEG +| 3792: 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 ER PRIMARY KEY, +| 3808: 73 7a 20 42 4c 4f 42 29 69 03 07 17 19 19 01 81 sz BLOB)i....... +| 3824: 2d 74 61 62 6c 65 74 31 5f 69 64 78 74 31 5f 69 -tablet1_idxt1_i +| 3840: 64 78 03 43 52 45 41 54 45 20 54 41 42 4c 45 20 dx.CREATE TABLE +| 3856: 27 74 31 5f 69 64 78 27 28 73 65 67 69 64 2c 20 't1_idx'(segid, +| 3872: 74 65 72 6d 2c 20 70 67 6e 6f 2c 20 50 52 49 4d term, pgno, PRIM +| 3888: 41 52 59 20 4b 45 59 28 73 65 67 69 64 2c 20 74 ARY KEY(segid, t +| 3904: 65 72 6d 29 29 20 57 49 54 48 4f 55 54 20 52 4f erm)) WITHOUT RO +| 3920: 57 49 44 55 02 07 17 1b 1b 01 81 01 74 61 62 6c WIDU........tabl +| 3936: 65 74 31 5f 64 61 74 61 74 31 5f 64 61 74 61 02 et1_datat1_data. +| 3952: 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 CREATE TABLE 't1 +| 3968: 5f 64 61 74 61 27 28 69 64 20 49 4e 54 45 47 45 _data'(id INTEGE +| 3984: 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 62 R PRIMARY KEY, b +| 4000: 6c 6f 63 6b 20 42 4c 4f 42 29 54 01 07 17 11 11 lock BLOB)T..... +| 4016: 08 81 15 74 61 62 6c 65 74 31 74 31 43 52 45 41 ...tablet1t1CREA +| 4032: 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45 TE VIRTUAL TABLE +| 4048: 20 74 31 20 55 53 49 4e 47 20 66 74 73 35 28 61 t1 USING fts5(a +| 4064: 2c 62 2c 70 72 65 66 69 78 3d 22 31 2c 32 2c 33 ,b,prefix=.1,2,3 +| 4080: 2c 34 22 2c 20 63 6f 6e 74 65 6e 74 3d 22 22 29 ,4., content=..) +| page 2 offset 4096 +| 0: 0d 0b 6a 00 37 09 4c 02 0f e7 09 4c 0f c6 0f a4 ..j.7.L....L.... +| 16: 0f 88 0f 6d 0f 4b 0f 2c 0f 0e 0e ec 0e cd 0e ad ...m.K.,........ +| 32: 0e 8e 0e 6c 0e 4b 0e 29 0e 08 0d e6 0d c4 0d b5 ...l.K.)........ +| 48: 0d 97 0d 76 0d 54 0d 31 0d 15 0c f3 0c d3 0c b5 ...v.T.1........ +| 64: 0c 95 0c 73 0c 54 0c 32 0c 10 0b ee 0b cc 0b b0 ...s.T.2........ +| 80: 0b 8d 0b 7e 0b 48 0b 2e 0b 0b 0a ef 0a cc 0a ad ...~.H.......... +| 96: 0a 8c 0a 6d 0a 4d 0a 2b 0a 0c 09 ec 09 ca 09 a8 ...m.M.+........ +| 112: 09 86 09 63 0f f1 00 00 00 00 00 00 00 00 00 00 ...c............ +| 2368: 00 00 00 00 00 00 00 00 00 00 00 00 15 0a 03 00 ................ +| 2384: 30 00 00 00 01 01 03 35 00 03 01 01 12 02 01 12 0......5........ +| 2400: 03 01 11 1c 8c 80 80 80 80 10 03 00 3e 00 00 00 ............>... +| 2416: 17 01 05 05 34 74 61 62 6c 03 02 03 01 04 77 68 ....4tabl.....wh +| 2432: 65 72 03 02 06 09 1b 8c 80 80 80 80 0f 03 00 3c er.............< +| 2448: 00 00 00 16 05 34 66 75 73 34 03 02 02 01 04 6e .....4fus4.....n +| 2464: 75 6d 62 03 06 01 04 09 1b 8c 80 80 80 80 0e 03 umb............. +| 2480: 00 3c 00 00 00 16 04 33 74 68 65 03 06 01 01 04 .<.....3the..... +| 2496: 01 03 77 68 65 03 02 04 04 0a 1b 8c 80 80 80 80 ..whe........... +| 2512: 0d 03 00 3c 00 00 00 16 04 33 6e 75 6d 03 06 01 ...<.....3num... +| 2528: 01 05 01 03 74 61 62 05 62 03 04 0a 19 8c 80 80 ....tab.b....... +| 2544: 80 80 0c 03 00 38 00 00 00 14 03 32 77 68 03 02 .....8.....2wh.. +| 2560: 04 00 04 33 66 74 73 03 02 02 04 07 18 8c 80 80 ...3fts......... +| 2576: 80 80 0b 03 00 36 00 00 00 13 03 32 74 61 03 02 .....6.....2ta.. +| 2592: 03 02 01 68 03 06 01 01 04 04 07 1b 8c 80 80 80 ...h............ +| 2608: 80 0a 03 00 3c 00 00 00 16 03 32 6e 75 03 06 01 ....<.....2nu... +| 2624: 01 05 01 02 6f 66 03 06 01 01 06 04 09 19 8c 80 ....of.......... +| 2640: 80 80 80 09 03 00 38 00 00 00 14 03 32 66 74 03 ......8.....2ft. +| 2656: 02 02 01 02 69 73 03 06 01 01 03 04 07 18 8c 80 ....is.......... +| 2672: 80 80 80 08 03 00 36 00 00 00 13 02 31 74 03 08 ......6.....1t.. +| 2688: 03 01 01 04 01 01 77 03 02 04 04 09 1a 8c 80 80 ......w......... +| 2704: 80 80 07 03 00 3a ff 00 00 15 02 31 6e 03 08 01 .....:.....1n... +| 2720: 01 02 05 01 01 6f 03 06 01 01 06 04 09 18 8c 80 .....o.......... +| 2736: 80 80 80 06 03 00 36 00 00 00 13 04 02 31 66 03 ......6......1f. +| 2752: 02 02 01 01 69 03 06 01 01 03 05 06 1c 8c 80 80 ....i........... +| 2768: 80 80 05 03 00 3e 00 00 00 17 04 30 74 68 65 03 .....>.....0the. +| 2784: 06 01 01 04 01 05 77 68 65 72 65 03 02 04 0a 15 ......where..... +| 2800: 8c 80 80 80 80 04 03 00 30 00 00 00 11 01 01 06 ........0....... +| 2816: 06 30 74 61 62 6c 65 03 02 03 07 1c 8c 80 80 80 .0table......... +| 2832: 80 03 03 00 3e 00 00 00 17 07 30 6e 75 6d 62 65 ....>.....0numbe +| 2848: 72 03 06 01 01 05 01 02 6f 66 03 06 04 0d 13 8c r.......of...... +| 2864: 80 80 80 80 02 03 00 2c 00 00 00 0f 01 01 03 02 .......,........ +| 2880: 30 6e 03 06 01 01 02 07 1b 8c 80 80 80 80 01 03 0n.............. +| 2896: 00 3c 00 00 00 16 08 30 66 74 73 34 61 75 78 03 .<.....0fts4aux. +| 2912: 02 02 01 02 69 73 03 06 04 0c 00 00 00 14 2a 00 ....is........*. +| 2928: 00 00 01 01 02 24 00 02 01 01 12 02 01 12 08 88 .....$.......... +| 2944: 80 80 80 80 11 03 00 16 00 00 00 05 02 1c 88 80 ................ +| 2960: 80 80 80 11 03 00 3e 00 00 00 17 05 34 72 6f 77 ......>.....4row +| 2976: 73 02 06 01 01 05 01 04 74 68 65 72 02 02 04 0b s.......ther.... +| 2992: 15 88 80 80 80 80 10 03 00 30 00 00 00 11 02 01 .........0...... +| 3008: 01 07 05 34 62 65 74 77 02 02 04 08 1b 88 80 80 ...4betw........ +| 3024: 80 80 0f 03 00 3c 00 00 00 16 04 04 33 72 6f 77 .....<......3row +| 3040: 02 06 01 01 05 01 03 74 68 65 02 08 05 0a 1b 88 .......the...... +| 3056: 80 80 80 80 0e 03 00 3c 00 00 00 16 02 01 02 04 .......<........ +| 3072: 33 61 72 65 02 02 03 01 03 62 65 74 02 02 07 08 3are.....bet.... +| 3088: 1b 88 80 80 80 80 0d 03 00 3c 00 00 00 16 03 32 .........<.....2 +| 3104: 74 68 02 08 02 01 01 07 00 04 33 61 6e 64 02 06 th........3and.. +| 3120: 04 0a 1b 88 80 80 80 80 0c 03 00 3c 00 00 00 16 ...........<.... +| 3136: 03 32 69 6e 02 06 01 01 06 01 02 72 6f 02 06 01 .2in.......ro... +| 3152: 01 05 04 09 18 88 80 80 80 80 0b 03 00 36 00 00 .............6.. +| 3168: 00 13 02 03 32 61 72 02 02 03 01 02 62 65 02 02 ....2ar.....be.. +| 3184: 04 05 07 1b 88 80 80 80 80 0a 03 00 3c 00 00 00 ............<... +| 3200: 16 02 31 74 02 08 02 01 01 07 00 03 32 61 6e 02 ..1t........2an. +| 3216: 06 01 01 04 09 19 88 80 80 80 80 09 03 00 38 00 ..............8. +| 3232: 00 00 14 02 31 6e 02 06 01 01 03 01 01 72 02 06 ....1n.......r.. +| 3248: 01 01 05 04 08 17 88 80 80 80 80 08 03 00 34 00 ..............4. +| 3264: 00 00 12 02 31 62 02 02 04 01 01 69 02 06 01 01 ....1b.....i.... +| 3280: 06 04 06 19 88 80 80 80 80 07 03 00 38 00 00 00 ............8... +| 3296: 14 04 02 31 32 02 02 05 01 01 61 02 08 03 01 01 ...12.....a..... +| 3312: 02 05 06 1b 88 80 80 80 80 06 03 00 3c 00 00 00 ............<... +| 3328: 16 06 30 74 68 65 72 65 02 02 02 00 02 31 31 02 ..0there.....11. +| 3344: 06 01 01 04 0a 15 88 80 80 80 80 05 03 00 30 00 ..............0. +| 3360: 00 00 11 01 01 05 04 30 74 68 65 02 06 01 01 07 .......0the..... +| 3376: 07 1c 88 80 80 80 80 04 03 00 3e 00 00 00 17 01 ..........>..... +| 3392: 01 06 02 30 6e 02 06 01 01 03 01 04 72 6f 77 73 ...0n.......rows +| 3408: 02 06 07 08 1b 88 80 80 80 80 03 03 00 3c 00 00 .............<.. +| 3424: 00 16 08 30 62 65 74 77 65 65 6e 02 02 04 01 02 ...0between..... +| 3440: 69 6e 02 06 04 0c 1a 88 80 80 80 80 02 03 00 3a in.............: +| 3456: 00 00 00 15 04 30 61 6e 64 02 06 01 01 02 02 02 .....0and....... +| 3472: 72 65 02 02 03 04 0a 17 88 80 80 80 80 01 03 00 re.............. +| 3488: 34 00 00 00 12 02 30 31 02 06 01 01 04 01 01 32 4.....01.......2 +| 3504: 02 02 06 04 08 08 84 80 80 80 80 12 03 00 16 00 ................ +| 3520: 00 00 05 04 1b 84 80 80 80 80 11 03 00 3c 00 00 .............<.. +| 3536: 00 16 05 34 74 61 62 6c 01 06 01 01 05 02 03 65 ...4tabl.......e +| 3552: 72 6d 01 02 04 0b 1b 84 80 80 80 80 10 03 00 3c rm.............< +| 3568: 00 00 00 16 05 34 65 61 63 68 01 02 03 01 04 70 .....4each.....p +| 3584: 72 65 73 01 02 05 04 09 1a 84 80 80 80 80 0f 03 res............. +| 3600: 00 3a 00 00 00 15 04 33 74 65 72 01 02 04 02 02 .:.....3ter..... +| 3616: 68 65 01 06 01 01 03 04 08 1b 84 80 80 80 80 0e he.............. +| 3632: 03 00 3c 00 00 00 16 04 33 70 72 65 01 02 05 01 ..<.....3pre.... +| 3648: 03 74 61 62 01 06 01 01 05 04 08 1a 84 80 80 80 .tab............ +| 3664: 80 0d 03 00 3a 00 00 00 15 04 33 66 6f 72 01 02 ....:.....3for.. +| 3680: 02 02 02 74 73 01 06 01 01 04 04 08 1b 84 80 80 ...ts........... +| 3696: 80 80 0c 03 00 3c 00 00 00 16 03 32 74 68 01 06 .....<.....2th.. +| 3712: 01 01 03 00 04 33 65 61 63 01 02 03 04 09 18 84 .....3eac....... +| 3728: 80 80 80 80 0b 03 00 36 00 00 00 13 03 32 74 61 .......6.....2ta +| 3744: 01 06 01 01 05 02 01 65 01 02 04 04 09 19 84 80 .......e........ +| 3760: 80 80 80 0a 03 00 38 00 00 00 14 03 32 69 6e 01 ......8.....2in. +| 3776: 06 01 01 02 01 02 70 72 01 02 05 04 09 18 84 80 ......pr........ +| 3792: 80 80 80 09 03 00 36 00 00 00 13 03 32 66 6f 01 ......6.....2fo. +| 3808: 02 02 02 01 74 01 06 01 01 04 04 07 1b 84 80 80 ....t........... +| 3824: 80 80 08 03 00 3c 00 00 00 16 02 31 74 01 0a 04 .....<.....1t... +| 3840: 01 01 03 04 00 03 32 65 61 01 02 03 04 0a 17 84 ......2ea....... +| 3856: 80 80 80 80 07 03 00 34 00 00 00 12 02 31 69 01 .......4.....1i. +| 3872: 06 01 01 02 01 01 70 01 02 05 04 08 18 84 80 80 ......p......... +| 3888: 80 80 06 03 00 36 00 00 00 13 02 31 65 01 02 03 .....6.....1e... +| 3904: 01 01 66 01 08 02 01 01 04 04 06 1b 84 80 80 80 ..f............. +| 3920: 80 05 03 00 3c 00 00 00 16 05 30 74 65 72 6d 01 ....<.....0term. +| 3936: 02 04 02 02 68 65 01 06 01 01 03 04 09 14 84 80 ....he.......... +| 3952: 80 80 80 04 03 00 2e 00 00 00 10 06 30 74 61 62 ............0tab +| 3968: 6c 65 01 06 00 01 05 04 15 84 80 80 80 80 03 03 le.............. +| 3984: 00 30 00 00 00 11 02 08 30 70 72 65 73 65 6e 74 .0......0present +| 4000: 01 02 05 05 1b 84 80 80 80 80 02 03 00 3c 00 00 .............<.. +| 4016: 00 16 04 30 66 74 73 01 06 01 01 04 01 02 69 6e ...0fts.......in +| 4032: 01 06 01 01 04 0a 1a 84 80 80 80 80 01 03 00 3a ...............: +| 4048: 00 00 00 15 05 30 65 61 63 68 01 02 03 01 03 66 .....0each.....f +| 4064: 6f 72 01 02 02 04 09 06 01 03 00 12 03 0b 0f 00 or.............. +| 4080: 00 08 8c 80 80 80 80 11 03 00 16 00 00 00 05 04 ................ +| page 3 offset 8192 +| 0: 0a 00 00 00 32 0e 4f 00 0f fa 10 f1 0f e9 0f e1 ....2.O......... +| 16: 0f d8 0f d1 0f c9 0f c1 0f b9 0f b1 0f a9 0f a0 ................ +| 32: 0f 98 0f 90 0f 87 0f 80 0f 78 0f 71 0f 68 0f 5f .........x.q.h._ +| 48: bd 56 0f 4d 0f 41 0f 38 0f 2f 0f 26 0f 1d 0f 13 .V.M.A.8./.&.... +| 64: 0f 0a 0f 01 0e f7 0e ee 0e e6 0e dd 0e d6 0e cd ................ +| 80: 0e c3 0e ba 0e b0 0e a8 0e 9f 0e 96 0e 8e 0e 85 ................ +| 96: 0e 7c 0e 73 0e 6a 0e 60 0e 58 0e 4f 00 00 00 00 .|.s.j.`.X.O.... +| 3648: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 08 ................ +| 3664: 04 01 10 01 03 34 74 20 07 04 01 0e 01 03 34 1e .....4t ......4. +| 3680: 09 04 01 12 01 03 33 74 68 1c 08 04 01 10 01 03 ......3th....... +| 3696: 33 6e 1a 08 04 01 10 01 03 32 77 18 08 04 01 10 3n.......2w..... +| 3712: 01 03 32 74 16 08 04 01 10 01 03 32 6e 14 07 04 ..2t.......2n... +| 3728: 01 0e 01 03 32 12 08 04 01 10 01 03 31 74 10 08 ....2.......1t.. +| 3744: 04 01 10 01 03 31 6e 0e 07 04 01 0e 01 03 31 0c .....1n.......1. +| 3760: 09 04 01 12 01 03 30 74 68 0a 08 04 01 10 01 03 ......0th....... +| 3776: 30 74 08 09 04 01 12 01 03 30 6e 75 06 08 04 01 0t.......0nu.... +| 3792: 10 01 03 30 6e 04 06 04 01 0c 01 05 52 08 04 01 ...0n.......R... +| 3808: 10 01 02 34 72 22 07 04 01 0e 01 02 34 20 08 04 ...4r.......4 .. +| 3824: 01 10 01 02 33 72 1e 09 04 01 12 01 02 33 61 72 ....3r.......3ar +| 3840: 1c 08 04 01 10 01 02 32 74 1a 08 04 01 10 b3 02 .......2t....... +| 3856: 32 69 18 09 04 01 12 01 02 32 61 72 16 08 04 01 2i.......2ar.... +| 3872: 10 01 02 31 74 14 08 04 01 10 01 02 31 6e 12 08 ...1t.......1n.. +| 3888: 04 01 10 01 02 31 62 10 08 04 01 10 01 02 31 32 .....1b.......12 +| 3904: 0e 0b 04 01 16 01 02 30 74 68 65 72 0c 08 04 01 .......0ther.... +| 3920: 10 01 02 30 74 0a 08 04 01 10 01 02 30 6e 08 08 ...0t.......0n.. +| 3936: 04 01 10 01 02 30 62 06 08 04 01 10 01 02 30 61 .....0b.......0a +| 3952: 04 06 04 01 0c 01 02 02 07 04 09 10 01 34 74 22 .............4t. +| 3968: 06 04 09 0e 01 34 20 08 04 09 12 01 33 74 65 1e .....4 .....3te. +| 3984: 07 04 09 10 01 33 70 1c 07 04 09 10 01 33 66 1a .....3p......3f. +| 4000: 08 04 09 12 01 32 74 68 18 07 04 09 10 01 32 74 .....2th......2t +| 4016: 16 07 04 09 10 01 32 69 14 07 04 09 10 01 32 66 ......2i......2f +| 4032: 12 07 04 09 10 01 31 74 10 07 04 09 10 01 31 69 ......1t......1i +| 4048: 0e 06 04 09 0e 01 31 0c 08 04 09 12 01 30 74 65 ......1......0te +| 4064: 0a 07 04 09 10 01 30 74 08 07 04 09 10 01 30 70 ......0t......0p +| 4080: 06 08 04 09 12 01 30 66 74 04 05 04 09 0c 01 02 ......0ft....... +| page 4 offset 12288 +| 0: 0d 00 00 00 03 0f eb 00 0f f9 0f f2 0f eb 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 05 03 03 00 10 ................ +| 4080: 03 05 05 02 03 00 10 04 06 05 01 03 00 10 04 04 ................ +| page 5 offset 16384 +| 0: 0a 00 00 00 02 0f eb 00 0f eb 0f f4 00 00 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 08 03 15 01 70 ...............p +| 4080: 67 73 7a 18 0b 03 1b 01 76 65 72 73 69 6f 6e 04 gsz.....version. +| page 6 offset 20480 +| 0: 0d 00 00 00 03 0f f2 00 0f fc 0f f7 0f f2 00 00 ................ +| 4080: 00 00 03 03 02 01 03 03 02 02 01 02 02 01 02 09 ................ +| end crash2.txt.db +}]} {} + +do_catchsql_test 40.1 { + BEGIN; + INSERT INTO t1(b) VALUES(X'819192e578de3fa24af3733ca8769291a0fee3669f9fddefc5cba913e4225d4b6ce2b04f26b87fad3ee6f9b7d90a1ea62a169bf41e5d32707a6ca5c3d05e4bde05c9d89eaaa8c50e74333d2e9fcd7dfe95528a3a016aac1102d825c5cd70cf99d8a88e0ea7f798d4334386518b7ad359beb168b93aba059a2a3bd93112d65b44c12b9904ea786b204d80531cdf0504bf9b203dbe927061974caf7b9f30cbc3397b61f802e732012a6663d41c3607d6f1c0dbcfd489adac05ca500c0b04439d894cd93a840159225ef73b627e178b9f84b3ffe66cf22a963a8368813ff7961fc47f573211ccec95e0220dcbb3bf429f4a50ba54d7a53784ac51bf'); + INSERT INTO t1(b) VALUES(X'c8ae0d0e7c3175946e62ba2b449511d4eb504079984a20f77969f62206c9f3d7ea25358ab705e6978627290b6d48db9032f815a06a79a4f4b809841a0942eed12954ed166f666111812a508abc3bec87958846edaec0a6fe14564bc0a4b78f1c35ebcacca6bae29cc37ae9b59d8a2d7593af1e47dda0ece2268a98d20febafad037964f139851f9a57f48b3706b01721769071991412044cd6006f1d72eb6eb4aa5ad77e378176db8c15575fbeee47165e38a7c6c5a557ac2dfe11813976eaf6741cf593a9e457053a3c34cddfbe605a6e25419f993de8374fafcd3636509d8416a51dc7bcc14cfca322ae343078f47e23522431c17d0da0c033'); + INSERT INTO t1(b) VALUES(X'dc29a94e873a45a4243fce9b912aaefbadf1d0423e0345793874b356eeb500b92fb05284c1601fe9bad3143f72162f10242cec27c44ebf764c8fc9fb0824e32c4161472a4f914f579e0e8274f08ca1a02e59b9d8eec1f31061f9ccb9ed97a6f06534e991f7992c761489e6a7724f6e9c2b581e77487ded3a986d53c4419bbd3e9747cee300e670dd7294874c77e2ed48da68eaa6c3ec954a09ac410493d98e34d6686e54fbbe80696705f10e040c66093efb40746b33600685c94c664c7942835a9e954866121d5dcfb2cb12e92521ea3df175ee17072502dad9b9c1565f801b2179799011eb7418bfa00323e3157589e648ff7378be233c79b7'); +} {/*malformed database schema*/} + +do_catchsql_test 40.2 { + INSERT INTO t1(a,b) VALUES(1,11),(2,22),(3, true ),(4,44); +} {/*malformed database schema*/} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 41.0 { + CREATE VIRTUAL TABLE t1 USING fts5(a,b,c); + REPLACE INTO t1_data VALUES(1,X'255a5824'); + REPLACE INTO t1_data VALUES(10,X'0a1000000102020002010101020101'); + INSERT INTO t1_data VALUES(137438953473,X'0000032b0030300102060102060102061f0203010203010203010832303136303630390102070102070102070101340102050102050102050101350102040102040102040207303030303030301c0204010204010204010662696e6272790306010202030601020203060102020306010202030601020203060102020306010202030601020203060102020306010202030601020203060102020108636f6d70696c657201020201020201020201066462737461740702030102030102030424656275670402020102020102020106656e61626c6507020201020201020201020201020201020201020201020201020201020201020201020201020201020201020201020201020201020201020201020201020202087874656e73696f6e1f02040102040102040104667473340a02030102030102030401350d020301020301020301036763630102030102030102030206656f706f6c7910020301020301020301056a736f6e3113020301020301020301046c6f61641f020301020301020301036d61781c02020102020102020205656d6f72791c020301020301020304047379733516020301020301020301066e6f6361736502060102020306010202030601020203060102020306010202030601020203060102020306010202030601020203060102020301b10202030601020201046f6d69741f0202010202010202010572747265651902030102030102030402696d010601020203060102020306010202030601020203060102020306010202030601020203060102020306010202030601020203060102020306010202010a746872656164736166652202020102020102020104767461620702040102040102040101780106010102010601010201060101020106010102010601010201060101020106010102010601010201060101020106010102010601010201060101020106010102010601010201060101020106010102010601010201060101020106010102010601010201060101020106010102010601010201060101020106010102010601010201060101020106010102010601010201060101020106010102010601010201060101020106010102010601010201060101020415130c0c124413110f47130f0c0e11100f0e100f440f1040150f'); + INSERT INTO t1_data VALUES(274877906945,X'00000183023030250601011d010331c2ba250601010d0101342506010121010135250601011e02036ec2ba250601012b0101382506010112010161250a0101021a1d02016f2506010111010162250601013201026377250601012f010166250801011f0c010167250601012701026863250601010f02026473250601013002016b2506010133020175250601010e010169250601012c0204386ec2be250601012001016a250601010401056bc2b2cebc250601010901016c25060101150203cebc71250601011301036dd18a250601010c01016f25060101260102706425060101240101712506010122010173250a010116040d02016f2506010134010175250801011b14020161250601010b010376c2aa25060101100202d7ac250601010601017725060101030201752506010114010179250a0101190e050202357a250601010701017a250601012e0102c2aa250801011c100201b3250601010a0202ba6225060101310203be656625060101080103c5a77425060101050102de8e250601011704080a08080a080a080809090809090808080b080c080a0a0809080a0809080a0908080a09080a08090a0a'); + INSERT INTO t1_idx VALUES(1,X'',2); + INSERT INTO t1_idx VALUES(2,X'',2); +} + +do_catchsql_test 41.1 { + INSERT INTO t1(t1) VALUES('optimize'); +} {/.*fts5: corrupt.*/} + +do_catchsql_test 41.2 { + INSERT INTO t1(t1) VALUES('integrity-check'); +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +reset_db +do_test 42.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 32768 pagesize 4096 filename 8cfba7fbb67e48de92c6.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 08 .....@ ........ +| 32: 00 00 00 02 00 00 00 01 00 00 00 09 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 96: 00 00 00 00 0d 0f c7 00 07 0d 92 00 0f 8d 0f 36 ...............6 +| 112: 0e cb 0e 6b 0e 0e 0d b6 0d 92 0d 92 00 00 00 00 ...k............ +| 3472: 00 00 22 08 06 17 11 11 01 31 74 61 62 6c 65 74 .........1tablet +| 3488: 32 74 32 08 43 52 45 41 54 45 20 54 41 42 4c 45 2t2.CREATE TABLE +| 3504: 20 74 32 28 78 29 56 07 06 17 1f 1f 01 7d 74 61 t2(x)V.......ta +| 3520: 62 6c 65 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 blet1_configt1_c +| 3536: 6f 6e 66 69 67 07 43 52 45 41 54 45 20 54 41 42 onfig.CREATE TAB +| 3552: 4c 45 20 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b LE 't1_config'(k +| 3568: 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 PRIMARY KEY, v) +| 3584: 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 5b 06 WITHOUT ROWID[. +| 3600: 07 17 21 21 01 81 01 74 61 62 6c 65 74 31 5f 64 ..!!...tablet1_d +| 3616: 6f 63 73 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 ocsizet1_docsize +| 3632: 06 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 .CREATE TABLE 't +| 3648: 31 5f 64 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 1_docsize'(id IN +| 3664: 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE +| 3680: 59 2c 20 73 7a 20 42 4c 4f 42 29 5e 05 07 17 21 Y, sz BLOB)^...! +| 3696: 21 01 81 07 74 61 62 6c 65 74 31 5f 63 6f 6e 74 !...tablet1_cont +| 3712: 65 6e 74 74 31 5f 63 6f 6e 74 65 6e 74 05 43 52 entt1_content.CR +| 3728: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 EATE TABLE 't1_c +| 3744: 6f 6e 74 65 6e 74 27 28 69 64 20 49 4e 54 45 47 ontent'(id INTEG +| 3760: 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 ER PRIMARY KEY, +| 3776: 63 30 2c 20 63 31 2c 20 63 32 29 69 04 07 17 19 c0, c1, c2)i.... +| 3792: 19 01 81 2d 74 61 62 6c 65 74 31 5f 69 64 78 74 ...-tablet1_idxt +| 3808: 31 5f 69 64 78 04 43 52 45 41 54 45 20 54 41 42 1_idx.CREATE TAB +| 3824: 4c 45 20 27 74 31 5f 69 64 78 27 28 73 65 67 69 LE 't1_idx'(segi +| 3840: 64 2c 20 74 65 72 6d 2c 20 70 67 6e 6f 2c 20 50 d, term, pgno, P +| 3856: 52 49 4d 41 52 59 20 4b 45 59 28 73 65 67 69 64 RIMARY KEY(segid +| 3872: 2c 20 74 65 72 6d 29 29 20 57 49 54 48 4f 55 54 , term)) WITHOUT +| 3888: 20 52 4f 57 49 44 55 03 07 17 1b 1b 01 81 01 74 ROWIDU........t +| 3904: 61 62 6c 65 74 31 5f 64 61 74 61 74 31 5f 64 61 ablet1_datat1_da +| 3920: 74 61 03 43 52 45 41 54 45 20 54 41 42 4c 45 20 ta.CREATE TABLE +| 3936: 27 74 31 5f 64 61 74 61 27 28 69 64 20 49 4e 54 't1_data'(id INT +| 3952: 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 EGER PRIMARY KEY +| 3968: 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 29 38 02 06 , block BLOB)8.. +| 3984: 17 11 11 08 5f 74 61 62 6c 65 74 31 74 31 43 52 ...._tablet1t1CR +| 4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 35 LE t1 USING fts5 +| 4032: 28 61 2c 62 2c 63 29 00 00 00 39 00 00 00 00 00 (a,b,c)...9..... +| page 3 offset 8192 +| 0: 0d 00 00 00 03 0c 94 00 0f e6 0f ef 0c 94 00 00 ................ +| 3216: 00 00 00 00 86 4a 84 80 80 80 80 01 04 00 8d 18 .....J.......... +| 3232: 00 00 03 2b 02 30 30 01 02 06 01 02 06 01 02 06 ...+.00......... +| 3248: 1f 02 03 01 02 03 01 02 03 01 08 32 30 31 36 30 ...........20160 +| 3264: 36 30 39 01 02 07 01 02 07 01 02 07 01 01 34 01 609...........4. +| 3280: 02 05 01 02 05 01 02 05 01 01 35 01 02 04 01 02 ..........5..... +| 3296: 04 01 02 04 02 07 30 30 30 30 30 30 30 1c 02 04 ......0000000... +| 3312: 01 02 04 01 02 04 01 06 62 69 6e 62 72 79 03 06 ........binbry.. +| 3328: 01 02 02 03 06 01 02 02 03 06 01 02 01 03 06 01 ................ +| 3344: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3360: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 ................ +| 3376: 03 06 01 02 02 03 06 01 02 02 01 08 63 6f 6d 70 ............comp +| 3392: 69 6c 65 72 01 02 02 01 02 02 01 02 02 01 06 64 iler...........d +| 3408: 62 73 74 61 74 07 02 03 01 02 03 01 02 03 02 04 bstat........... +| 3424: 65 62 75 67 04 02 02 01 02 02 01 02 02 01 06 65 ebug...........e +| 3440: 6e 61 62 6c 65 07 02 02 01 02 02 01 02 02 01 02 nable........... +| 3456: 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 ................ +| 3472: 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 ................ +| 3488: 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 ................ +| 3504: 02 01 02 02 02 08 78 74 65 6e 73 69 6f 6e 1f 02 ......xtension.. +| 3520: 04 01 02 04 01 02 04 01 04 66 74 73 34 0a 02 03 .........fts4... +| 3536: 01 02 03 01 02 03 04 01 35 0d 02 03 01 02 03 01 ........5....... +| 3552: 02 03 01 03 67 63 63 01 02 03 01 02 03 01 02 03 ....gcc......... +| 3568: 02 06 65 6f 70 6f 6c 79 10 02 03 01 02 03 01 02 ..eopoly........ +| 3584: 03 01 05 6a 73 6f 6e 31 13 02 03 01 02 03 01 02 ...json1........ +| 3600: 03 01 04 6c 6f 61 64 1f 02 03 01 02 03 01 02 03 ...load......... +| 3616: 01 03 6d 61 78 1c 02 02 01 02 02 01 02 02 02 05 ..max........... +| 3632: 65 6d 6f 72 79 1c 02 03 01 02 03 01 02 03 04 04 emory........... +| 3648: 73 79 73 35 16 02 03 01 02 03 01 02 03 01 06 6e sys5...........n +| 3664: 6f 63 61 73 65 02 06 01 02 02 03 06 01 02 02 03 ocase........... +| 3680: 06 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 ................ +| 3696: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3712: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3728: 02 01 04 6f 6d 69 74 1f 02 02 01 02 02 01 02 02 ...omit......... +| 3744: 01 05 72 74 72 65 65 19 02 03 01 02 03 01 02 03 ..rtree......... +| 3760: 04 02 69 6d 01 06 01 02 02 03 06 01 02 02 03 06 ..im............ +| 3776: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3792: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3808: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 ................ +| 3824: 01 0a 74 68 72 65 61 64 73 61 66 65 22 02 02 01 ..threadsafe.... +| 3840: 02 02 01 02 02 01 04 76 74 61 62 07 02 04 01 02 .......vtab..... +| 3856: 04 01 02 04 01 01 78 01 06 01 01 02 01 06 01 01 ......x......... +| 3872: 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 ................ +| 3888: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 3904: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 3920: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 ................ +| 3936: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 ................ +| 3952: 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 ................ +| 3968: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 3984: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 4000: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 ................ +| 4016: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 ................ +| 4032: 02 01 06 01 01 02 01 06 01 01 02 04 15 13 0c 0c ................ +| 4048: 12 44 13 11 0f 47 13 0f 0c 0e 11 10 0f 0e 10 0f .D...G.......... +| 4064: 44 0f 10 40 15 0f 07 01 03 00 14 24 5a 24 24 0f D..@.......$Z$$. +| 4080: 0a 03 00 24 00 00 00 00 01 01 01 00 01 01 01 01 ...$............ +| page 4 offset 12288 +| 0: 0a 00 00 00 01 0f fa 00 0f fa 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 05 04 09 0b 01 02 ................ +| page 5 offset 16384 +| 0: 0d 00 00 00 24 0c 0a 00 0f d8 0f af 0f 86 0f 74 ....$..........t +| 16: 0f 61 0f 4e 0f 2f 0f 0f 0e ef 0e d7 0e be 0e a5 .a.N./.......... +| 32: 0e 8d 0e 74 0e 5b 0e 40 0e 24 0e 08 0d ef 0d d5 ...t.[.@.$...... +| 48: 0d bb 0d a0 0d 84 0d 68 0d 4f 0d 35 0d 1b 0c fb .......h.O.5.... +| 64: 0c da 0c b9 0c 99 0c 78 0c 57 0c 3e 0c 24 0c 0a .......x.W.>.$.. +| 3072: 00 00 00 00 00 00 00 00 00 00 18 24 05 00 25 0f ...........$..%. +| 3088: 19 54 48 52 45 41 44 53 41 46 45 3d 30 58 42 49 .THREADSAFE=0XBI +| 3104: 4f 41 52 59 18 23 05 00 25 0f 19 54 48 52 45 41 OARY.#..%..THREA +| 3120: 44 53 41 46 45 3d 30 58 4e 4f 43 41 53 45 17 22 DSAFE=0XNOCASE.. +| 3136: 05 00 25 0f 17 54 48 52 45 41 44 53 41 46 45 3d ..%..THREADSAFE= +| 3152: 30 58 52 54 52 49 4d 1f 21 05 00 33 0f 19 4f 4d 0XRTRIM.!..3..OM +| 3168: 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 4f IT LOAD EXTENSIO +| 3184: 4e 58 42 49 4e 41 52 59 1f 20 05 00 33 0f 19 4f NXBINARY. ..3..O +| 3200: 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 MIT LOAD EXTENSI +| 3216: 4f 4e 58 4e 4f 43 41 53 45 1e 1f 05 00 33 0f 17 ONXNOCASE....3.. +| 3232: 4f 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 OMIT LOAD EXTENS +| 3248: 49 4f 4e 58 52 54 52 49 4d 1f 1e 05 00 33 0f 19 IONXRTRIM....3.. +| 3264: 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 30 MAX MEMORY=50000 +| 3280: 30 30 30 58 42 49 4e 41 52 59 1f 1d 05 00 33 0f 000XBINARY....3. +| 3296: 19 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 .MAX MEMORY=5000 +| 3312: 30 30 30 30 58 4e 4f 43 41 53 45 1e 1c 05 00 33 0000XNOCASE....3 +| 3328: 0f 17 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 ..MAX MEMORY=500 +| 3344: 30 30 30 30 30 58 52 54 52 49 4d 18 1b 05 00 25 00000XRTRIM....% +| 3360: 0f 19 45 4e 41 42 4c 45 20 52 54 52 45 45 58 42 ..ENABLE RTREEXB +| 3376: 49 4e 41 52 59 18 1a 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3392: 4c 45 20 52 54 52 45 45 58 4e 4f 43 41 53 45 17 LE RTREEXNOCASE. +| 3408: 19 05 00 25 0f 17 45 4e 41 42 4c 45 20 52 54 52 ...%..ENABLE RTR +| 3424: 45 45 58 52 54 52 49 4d 1a 18 05 00 29 0f 19 45 EEXRTRIM....)..E +| 3440: 4e 41 42 4c 45 20 4d 45 4d 53 59 53 35 58 42 49 NABLE MEMSYS5XBI +| 3456: 4e 41 52 59 1a 17 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3472: 45 20 4d 45 4d 53 59 53 35 58 4e 4f 43 41 53 45 E MEMSYS5XNOCASE +| 3488: 19 16 05 00 29 0f 17 45 4e 41 42 4c 45 20 4d 45 ....)..ENABLE ME +| 3504: 4d 53 59 53 35 58 52 54 52 49 4d 18 15 05 00 25 MSYS5XRTRIM....% +| 3520: 0f 19 45 4e 41 42 4c 45 20 4a 53 4f 4e 31 58 42 ..ENABLE JSON1XB +| 3536: 49 4e 41 52 59 18 14 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3552: 4c 45 20 4a 53 4f 4e 31 58 4e 4f 43 41 53 45 17 LE JSON1XNOCASE. +| 3568: 13 05 00 25 0f 17 45 4e 41 42 4c 45 20 4a 53 4f ...%..ENABLE JSO +| 3584: 4e 31 58 52 54 52 49 4d 1a 12 05 00 29 0f 19 45 N1XRTRIM....)..E +| 3600: 4e 41 42 4c 45 20 47 45 4f 50 4f 4c 59 57 42 49 NABLE GEOPOLYWBI +| 3616: 4e 41 52 59 1a 11 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3632: 45 20 47 45 4f 50 4f 4c 59 58 4e 4f 42 41 53 45 E GEOPOLYXNOBASE +| 3648: 19 10 05 00 29 0f 17 45 4e 41 42 4c 45 20 47 45 ....)..ENABLE GE +| 3664: 4f 50 4f 4c 59 58 52 54 52 49 4d 17 0f 05 00 23 OPOLYXRTRIM....# +| 3680: 0f 19 45 4e 41 42 4c 45 20 46 54 53 35 58 42 49 ..ENABLE FTS5XBI +| 3696: 4e 41 52 59 17 0e 05 00 23 0f 19 45 4e 41 42 4c NARY....#..ENABL +| 3712: 45 20 46 54 53 35 58 4e 4f 43 41 53 45 16 0d 05 E FTS5XNOCASE... +| 3728: 00 23 0f 17 45 4e 41 42 4c 45 20 46 54 53 35 58 .#..ENABLE FTS5X +| 3744: 52 54 52 49 4d 17 0c 05 00 23 0f 19 45 4e 41 42 RTRIM....#..ENAB +| 3760: 4c 45 20 46 54 53 34 58 42 49 4e 41 52 59 17 0b LE FTS4XBINARY.. +| 3776: 05 00 23 0f 19 45 4e 41 42 4c 45 20 46 54 53 34 ..#..ENABLE FTS4 +| 3792: 58 4e 4f 43 41 53 45 16 0a 05 00 23 0f 17 45 4e XNOCASE....#..EN +| 3808: 41 42 4c 45 20 46 54 53 34 58 52 54 52 49 4d 1e ABLE FTS4XRTRIM. +| 3824: 09 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3840: 54 41 54 20 56 54 41 42 58 42 49 4e 41 52 59 1e TAT VTABXBINARY. +| 3856: 08 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3872: 54 41 54 20 56 54 41 42 58 4e 4f 43 41 53 45 1d TAT VTABXNOCASE. +| 3888: 07 05 00 31 0f 17 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3904: 54 41 54 20 56 54 41 42 58 52 54 52 49 4d 11 06 TAT VTABXRTRIM.. +| 3920: 05 00 17 0f 19 44 45 42 55 47 58 42 49 4e 41 52 .....DEBUGXBINAR +| 3936: 59 11 05 05 00 17 0f 19 44 45 42 55 47 58 4e 4f Y.......DEBUGXNO +| 3952: 43 41 53 45 10 04 05 00 17 0f 17 44 45 42 55 47 CASE.......DEBUG +| 3968: 58 52 54 52 49 4d 27 03 05 00 43 0f 19 43 4f 4d XRTRIM'...C..COM +| 3984: 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e 30 20 PILER=gcc-5.4.0 +| 4000: 32 30 31 36 30 36 30 39 52 02 49 4e 41 52 59 27 20160609R.INARY' +| 4016: 02 05 00 43 0f 19 43 4f 4d 50 49 4c 45 52 3d 67 ...C..COMPILER=g +| 4032: 63 63 2d 35 2e 34 2e 30 20 32 30 31 36 30 36 30 cc-5.4.0 2016060 +| 4048: 39 58 4e 4f 43 41 53 45 26 01 05 00 43 0f 17 43 9XNOCASE&...C..C +| 4064: 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e OMPILER=gcc-5.4. +| 4080: 30 20 32 30 31 36 30 36 30 39 58 52 54 52 49 4d 0 20160609XRTRIM +| page 6 offset 20480 +| 0: 0d 00 00 00 24 0e e0 00 0f f8 0f f0 0f e8 0f e0 ....$........... +| 16: 0f d8 0f d0 0f c8 0f c0 0f b8 0f b0 0f a8 0f a0 ................ +| 32: 0f 98 0f 90 0f 88 0f 80 0f 78 0f 70 0f 68 0f 60 .........x.p.h.` +| 48: 0f 58 0f 50 0f 48 0f 40 0f 38 0f 30 0f 28 0f 20 .X.P.H.@.8.0.(. +| 64: 0f 18 0f 10 0f 08 0f 00 0e f8 0e f0 0e e8 0e e0 ................ +| 3808: 06 24 03 00 12 02 01 01 06 23 03 00 12 02 01 01 .$.......#...... +| 3824: 06 22 03 00 12 02 01 01 06 21 03 00 12 03 01 01 .........!...... +| 3840: 06 20 03 00 12 03 01 01 06 1f 03 00 12 03 01 01 . .............. +| 3856: 06 1e 03 00 12 03 01 01 06 1d 03 00 12 03 01 01 ................ +| 3872: 06 1c 03 00 12 03 01 01 06 1b 03 00 12 02 01 01 ................ +| 3888: 06 1a 03 00 12 02 01 01 06 19 03 00 12 02 01 01 ................ +| 3904: 06 18 03 00 12 02 01 01 06 17 03 00 12 02 01 01 ................ +| 3920: 06 16 03 00 12 02 01 01 06 15 03 00 12 02 01 01 ................ +| 3936: 06 14 03 00 12 02 01 01 06 13 03 00 12 02 01 01 ................ +| 3952: 06 12 03 00 12 02 01 01 06 11 03 00 12 02 01 01 ................ +| 3968: 06 10 03 00 12 02 01 01 06 0f 03 00 12 02 01 01 ................ +| 3984: 06 0e 03 00 12 02 01 01 06 0d 03 00 12 02 01 01 ................ +| 4000: 06 0c 03 00 12 02 01 01 06 0b 03 00 12 02 01 01 ................ +| 4016: 06 0a 03 00 12 02 01 01 06 09 03 00 12 03 01 01 ................ +| 4032: 06 08 03 00 12 03 01 01 06 07 03 00 12 03 01 01 ................ +| 4048: 06 06 03 00 12 01 01 01 06 05 03 00 12 01 01 01 ................ +| 4064: 06 04 03 00 12 01 01 01 06 03 03 00 12 06 01 01 ................ +| 4080: 06 02 03 00 12 06 01 01 06 01 03 00 12 06 01 01 ................ +| page 7 offset 24576 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| page 8 offset 28672 +| 0: 0d 00 00 00 03 0f d6 00 0f f4 0f e9 0f d6 00 00 ................ +| 4048: 00 00 00 00 00 00 11 03 02 2b 69 6e 74 65 67 72 .........+integr +| 4064: 69 74 79 2d 63 68 65 63 6b 09 02 02 1b 72 65 62 ity-check....reb +| 4080: 75 69 6c 64 0a 01 02 1d 6f 70 74 69 5d 69 7a 65 uild....opti]ize +| end 8cfba7fbb67e48de92c6.db +}]} {} + +do_catchsql_test 42.1 { + INSERT INTO t1(t1) VALUES('integrity-check'); +} {1 {fts5: checksum mismatch for table "t1"}} + +#------------------------------------------------------------------------- +reset_db +do_test 43.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 24576 pagesize 4096 filename 89028ffd2c29b679e250.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 06 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 06 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 96: 00 00 00 00 0d 00 00 00 06 0e 0f 00 0f aa 0f 53 ...............S +| 112: 0e e8 0e 8b 0e 33 0e 0f 00 00 00 00 00 00 00 00 .....3.......... +| 3584: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 22 ................ +| 3600: 06 06 17 11 11 01 31 74 61 62 6c 65 62 62 62 62 ......1tablebbbb +| 3616: 06 43 52 45 41 54 45 20 54 41 42 4c 45 20 62 62 .CREATE TABLE bb +| 3632: 28 61 29 56 05 06 17 1f 1f 01 7d 74 61 62 6c 65 (a)V.......table +| 3648: 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 6f 6e 66 t1_configt1_conf +| 3664: 69 67 05 43 52 45 41 54 45 20 54 41 42 4c 45 20 ig.CREATE TABLE +| 3680: 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b 20 50 52 't1_config'(k PR +| 3696: 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 20 57 49 IMARY KEY, v) WI +| 3712: 54 48 4f 55 54 20 52 4f 57 49 44 5b 04 07 17 21 THOUT ROWID[...! +| 3728: 21 01 81 01 74 61 62 6c 65 74 31 5f 64 6f 63 73 !...tablet1_docs +| 3744: 69 7a 65 74 31 5f 12 6f 63 73 69 7a 65 04 43 52 izet1_.ocsize.CR +| 3760: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 64 EATE TABLE 't1_d +| 3776: 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 54 45 47 ocsize'(id INTEG +| 3792: 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 ER PRIMARY KEY, +| 3808: 73 7a 20 42 4c 4f 42 29 69 03 07 17 19 19 01 81 sz BLOB)i....... +| 3824: 2d 74 61 62 6c 65 74 31 5f 69 64 78 74 31 5f 69 -tablet1_idxt1_i +| 3840: 64 78 03 43 52 45 41 54 45 20 54 41 42 4c 45 20 dx.CREATE TABLE +| 3856: 27 74 31 5f 69 64 78 27 28 73 65 67 69 64 2c 20 't1_idx'(segid, +| 3872: 74 65 72 6d 2c 20 70 67 6e 6f 2c 20 50 52 49 4d term, pgno, PRIM +| 3888: 41 52 59 20 4b 45 59 28 73 65 67 69 64 2c 20 74 ARY KEY(segid, t +| 3904: 65 72 6d 29 29 20 57 49 54 48 4f 55 54 20 52 4f erm)) WITHOUT RO +| 3920: 57 49 44 55 02 07 17 1b 1b 01 81 01 74 61 62 6c WIDU........tabl +| 3936: 65 74 31 5f 64 61 74 61 74 31 5e 64 61 74 61 02 et1_datat1^data. +| 3952: 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 CREATE TABLE 't1 +| 3968: 5f 64 61 74 61 27 28 69 64 20 49 4e 54 45 47 45 _data'(id INTEGE +| 3984: 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 62 R PRIMARY KEY, b +| 4000: 6c 6f 63 6b 20 42 4c 4f 42 29 54 01 07 17 11 11 lock BLOB)T..... +| 4016: 08 81 15 74 61 62 6c 65 74 31 74 31 43 52 45 41 ...tablet1t1CREA +| 4032: 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45 TE VIRTUAL TABLE +| 4048: 20 74 31 20 55 53 49 4e 47 20 66 74 73 35 28 61 t1 USING fts5(a +| 4064: 2c 62 2c 70 72 65 66 69 78 3d 22 31 2c 32 2c 33 ,b,prefix=.1,2,3 +| 4080: 2c 34 22 2c 20 63 6f 6e 74 65 6e 74 3d 22 22 29 ,4., content=..) +| page 2 offset 4096 +| 0: 0d 0b 6a 00 37 09 4c 02 0f e8 09 4c 0f c6 0f a4 ..j.7.L....L.... +| 16: 0f 88 0f 6d 0f 4b 0f 2c 0f 0e 0e ec 0e cd 0e ad ...m.K.,........ +| 32: 0e 8e 0e 6c 0e 4b 0e 29 0e 08 0d e6 0d c4 0d b5 ...l.K.)........ +| 48: 0d 97 0d 76 0d 54 0d 31 0d 15 0c f3 0c d3 0c b5 ...v.T.1........ +| 64: 0c 95 0c 73 0c 54 0c 32 0c 10 0b ee 0b cc 0b b0 ...s.T.2........ +| 80: 0b 8d 0b 7e 0b 48 0b 2e 0b 0b 0a ef 0a cc 0a ad ...~.H.......... +| 96: 0a 8c 0a 6d 0a 4d 0a 2b 0a 0c 09 ec 09 ca 09 a8 ...m.M.+........ +| 112: 09 86 09 63 0f f1 00 00 00 00 00 00 00 00 00 00 ...c............ +| 2368: 00 00 00 00 00 00 00 00 00 00 00 00 15 0a 03 00 ................ +| 2384: 30 00 00 00 01 01 03 35 00 03 00 01 12 02 01 12 0......5........ +| 2400: 03 01 11 1c 8c 80 80 80 80 10 03 00 3e 00 00 00 ............>... +| 2416: 17 01 05 05 34 74 61 62 6c 03 02 03 01 04 77 68 ....4tabl.....wh +| 2432: 65 72 03 02 06 09 1b 8c 80 80 80 80 0f 03 00 3c er.............< +| 2448: 00 00 00 16 05 34 66 74 73 34 03 02 02 01 04 6e .....4fts4.....n +| 2464: 75 6d 62 03 06 01 04 09 1b 8c 80 80 80 80 0e 03 umb............. +| 2480: 00 3c 00 00 00 16 04 33 74 68 65 03 06 01 01 04 .<.....3the..... +| 2496: 01 03 77 68 65 03 02 04 04 0a 1b 8c 80 80 80 80 ..whe........... +| 2512: 0d 03 00 3c 00 00 00 16 04 33 6e 75 6d 03 06 01 ...<.....3num... +| 2528: 01 05 01 03 74 61 62 03 02 03 04 0a 19 8c 80 80 ....tab......... +| 2544: 80 80 0c 03 00 38 00 00 00 14 03 32 77 68 03 02 .....8.....2wh.. +| 2560: 04 00 04 33 66 74 73 03 02 02 04 07 18 8c 80 80 ...3fts......... +| 2576: 80 80 0b 03 00 36 00 00 00 13 03 32 74 61 03 02 .....6.....2ta.. +| 2592: 03 02 01 68 03 06 01 01 04 04 07 1b 8c 80 80 80 ...h............ +| 2608: 80 0a 03 00 3c 00 00 00 16 03 32 6e 75 03 06 01 ....<.....2nu... +| 2624: 01 05 01 02 6f 66 03 06 01 01 06 04 09 19 8c 80 ....of.......... +| 2640: 80 80 80 09 03 00 38 00 00 00 14 03 32 66 74 03 ......8.....2ft. +| 2656: 02 02 01 02 69 73 03 06 01 01 03 04 07 18 8c 80 ....is.......... +| 2672: 80 80 80 08 03 00 36 94 00 00 13 02 31 74 03 08 ......6.....1t.. +| 2688: 03 01 01 04 01 01 77 03 02 04 04 09 1a 8c 80 80 ......w......... +| 2704: 80 80 07 03 00 3a 00 00 00 15 02 31 6e 03 08 01 .....:.....1n... +| 2720: 01 02 05 01 01 6f 03 06 01 01 06 04 09 18 8c 80 .....o.......... +| 2736: 80 80 80 06 03 00 36 00 00 00 13 04 02 31 66 03 ......6......1f. +| 2752: 02 02 01 01 69 03 06 01 01 03 05 06 1c 8c 80 80 ....i........... +| 2768: 80 80 05 03 00 3e 00 00 00 17 04 30 74 68 65 03 .....>.....0the. +| 2784: 06 01 01 04 01 05 77 68 65 72 65 03 02 04 0a 15 ......where..... +| 2800: 8c 80 80 80 80 04 03 00 30 00 00 00 11 01 01 06 ........0....... +| 2816: 06 30 74 61 62 6c 65 03 02 03 07 1c 8c 80 80 80 .0table......... +| 2832: 80 03 03 00 3e 00 00 00 17 07 30 6e 75 6d 62 65 ....>.....0numbe +| 2848: 72 03 06 01 01 05 01 02 6f 66 03 06 04 0d 13 8c r.......of...... +| 2864: 80 80 80 80 02 03 00 2c 00 00 00 0f 01 01 03 02 .......,........ +| 2880: 30 6e 03 06 01 01 02 07 1b 8c 80 80 80 80 01 03 0n.............. +| 2896: 00 3c 00 00 00 16 08 30 66 74 73 34 61 75 78 03 .<.....0fts4aux. +| 2912: 02 02 01 02 69 73 04 06 04 0c 00 00 00 14 2a 00 ....is........*. +| 2928: 00 00 01 01 02 24 00 02 01 01 12 02 01 12 08 88 .....$.......... +| 2944: 80 80 80 80 12 03 00 16 00 00 00 05 02 1c 88 80 ................ +| 2960: 80 80 80 11 03 00 3e 00 00 00 17 05 34 72 6f 77 ......>.....4row +| 2976: 73 02 06 01 01 05 01 04 74 68 65 72 02 02 04 0b s.......ther.... +| 2992: 15 88 80 80 80 80 10 03 00 30 00 00 00 11 02 01 .........0...... +| 3008: 01 07 05 34 62 65 74 77 02 02 04 08 1b 88 80 80 ...4betw........ +| 3024: 80 80 0f 03 00 3c 00 00 00 16 04 04 33 72 6f 77 .....<......3row +| 3040: 02 06 01 01 05 01 03 74 68 65 02 08 05 0a 1b 88 .......the...... +| 3056: 80 80 80 80 0e 03 00 3c 00 00 00 16 01 01 02 04 .......<........ +| 3072: 33 61 72 65 02 02 03 01 03 62 65 74 02 02 07 08 3are.....bet.... +| 3088: 1b 88 80 80 80 80 0d 03 00 3c 00 00 00 16 03 32 .........<.....2 +| 3104: 74 68 02 08 02 01 01 07 00 04 33 61 6e 64 02 06 th........3and.. +| 3120: 04 0a 1b 88 80 80 80 80 0c 03 00 3c 00 00 00 16 ...........<.... +| 3136: 03 32 69 6e 02 06 01 01 06 01 02 72 6f 02 06 01 .2in.......ro... +| 3152: 01 05 04 09 18 88 80 80 80 80 0b 03 00 36 00 00 .............6.. +| 3168: 00 13 02 03 32 61 72 02 02 03 01 02 62 65 02 02 ....2ar.....be.. +| 3184: 04 05 07 1b 88 80 80 80 80 0a 03 00 3c 00 00 00 ............<... +| 3200: 16 02 31 74 02 08 02 01 01 07 00 03 32 61 6e 02 ..1t........2an. +| 3216: 06 01 01 04 09 19 88 80 80 80 80 09 03 00 38 00 ..............8. +| 3232: 00 00 14 02 31 6e 02 06 01 01 03 01 01 72 02 06 ....1n.......r.. +| 3248: 01 01 05 04 08 17 88 80 80 80 80 08 03 00 34 00 ..............4. +| 3264: 00 00 12 02 31 62 02 02 04 01 01 69 02 06 01 01 ....1b.....i.... +| 3280: 06 04 06 19 88 80 80 80 80 07 03 00 38 00 00 00 ............8... +| 3296: 14 04 02 31 32 02 02 05 01 01 61 02 08 03 01 01 ...12.....a..... +| 3312: 02 05 06 1b 88 80 80 80 80 06 03 00 3c 00 00 00 ............<... +| 3328: 16 06 30 74 68 65 72 65 02 02 02 00 02 31 31 02 ..0there.....11. +| 3344: 06 01 01 04 0a 15 88 80 80 80 80 05 00 90 30 00 ..............0. +| 3360: 00 00 11 01 01 05 04 30 74 68 65 02 06 01 01 07 .......0the..... +| 3376: 07 1c 88 80 80 80 80 04 03 00 3e 00 00 00 17 01 ..........>..... +| 3392: 01 06 02 30 6e 02 06 01 01 03 01 04 72 6f 77 73 ...0n.......rows +| 3408: 02 06 07 08 1b 88 80 80 80 80 03 03 00 3c 00 00 .............<.. +| 3424: 00 16 07 30 62 65 74 77 65 65 6e 02 02 04 01 02 ...0between..... +| 3440: 69 6e 02 06 04 0c 1a 88 80 80 80 80 02 03 00 3a in.............: +| 3456: 00 00 00 15 04 30 61 6e 64 02 06 01 01 02 02 02 .....0and....... +| 3472: 72 65 02 02 03 04 0a 17 88 80 80 80 80 01 03 00 re.............. +| 3488: 34 00 00 00 12 02 30 31 02 06 01 01 04 01 01 32 4.....01.......2 +| 3504: 02 02 05 04 08 08 84 80 80 80 80 12 03 00 16 00 ................ +| 3520: 00 00 05 04 1b 84 80 80 80 80 11 03 00 3c 00 00 .............<.. +| 3536: 00 16 05 34 74 61 62 6c 01 06 01 01 05 02 03 65 ...4tabl.......e +| 3552: 72 6d 01 02 04 0b 1b 84 80 80 80 80 10 03 00 3c rm.............< +| 3568: 00 00 00 16 05 34 65 61 63 68 01 02 03 01 04 70 .....4each.....p +| 3584: 72 65 73 01 02 05 04 09 1a 84 80 80 80 80 0f 03 res............. +| 3600: 00 3a 00 00 00 15 04 33 74 65 72 01 02 04 02 02 .:.....3ter..... +| 3616: 68 65 01 06 01 01 03 04 08 1b 84 80 80 80 80 0e he.............. +| 3632: 03 00 3c 00 00 00 16 04 33 70 72 65 01 02 05 01 ..<.....3pre.... +| 3648: 03 74 61 62 01 06 01 01 05 04 08 1a 84 80 80 80 .tab............ +| 3664: 80 0d 03 00 3a 00 00 00 15 04 33 66 6f 72 01 02 ....:.....3for.. +| 3680: 02 02 02 74 73 01 06 01 01 04 04 08 1b 84 80 80 ...ts........... +| 3696: 80 80 0c 03 00 3c 00 00 00 16 03 32 74 68 01 06 .....<.....2th.. +| 3712: 01 01 03 00 04 33 65 61 63 01 02 03 04 09 18 84 .....3eac....... +| 3728: 80 80 80 80 0b 03 00 36 00 00 00 13 03 32 74 61 .......6.....2ta +| 3744: 01 06 01 01 05 02 01 65 01 02 04 04 09 19 84 80 .......e........ +| 3760: 80 80 80 0a 03 00 38 00 00 00 14 03 32 69 6e 01 ......8.....2in. +| 3776: 06 01 01 02 01 02 70 72 01 02 05 04 09 18 84 80 ......pr........ +| 3792: 80 80 80 09 03 00 36 00 00 00 13 03 32 66 6f 01 ......6.....2fo. +| 3808: 02 02 02 01 74 01 06 01 01 04 04 07 1b 84 80 80 ....t........... +| 3824: 80 80 08 03 00 3c 00 00 00 16 02 31 74 01 0a 04 .....<.....1t... +| 3840: 01 01 03 04 00 03 32 65 61 01 02 03 04 0a 17 84 ......2ea....... +| 3856: 80 80 80 80 07 03 00 34 00 00 00 12 02 31 69 01 .......4.....1i. +| 3872: 06 01 01 02 01 01 71 01 02 05 04 08 18 84 80 80 ......q......... +| 3888: 80 80 06 03 00 36 00 00 00 13 02 31 65 01 02 03 .....6.....1e... +| 3904: 01 01 66 01 08 02 01 01 04 04 06 1b 84 80 80 80 ..f............. +| 3920: 80 05 03 00 3c 00 00 00 16 05 30 74 65 72 6d 01 ....<.....0term. +| 3936: 02 04 02 02 68 65 01 06 01 01 03 04 09 14 84 80 ....he.......... +| 3952: 80 80 80 04 03 00 2e 00 00 00 10 06 30 74 61 62 ............0tab +| 3968: 6c 65 01 06 01 01 05 04 15 84 80 80 80 80 03 03 le.............. +| 3984: 00 30 00 00 00 11 02 08 30 70 72 65 73 65 6e 74 .0......0present +| 4000: 01 02 05 05 1b 84 80 80 80 80 02 03 00 3c 00 00 .............<.. +| 4016: 00 16 04 30 66 74 73 01 06 01 01 04 01 02 69 6e ...0fts.......in +| 4032: 01 06 01 01 04 0a 1a 84 80 80 80 80 01 03 00 3a ...............: +| 4048: 00 00 00 15 05 30 65 61 63 68 01 02 03 01 03 66 .....0each.....f +| 4064: 6f 72 01 02 02 04 09 06 01 03 00 12 03 0b 0f 00 or.............. +| 4080: 00 08 8c 80 80 80 80 11 03 00 16 00 00 00 05 04 ................ +| page 3 offset 8192 +| 0: 0a 00 00 00 32 0e 4f 00 0f fa 0f f1 0f e9 0f e1 ....2.O......... +| 16: 0f d8 0f d1 0f c9 0f c1 0f b9 0f b1 0f a9 0f a0 ................ +| 32: 0f 98 0f 91 0f 87 0f 80 0f 78 0f 71 0f 68 0f 5f .........x.q.h._ +| 48: 0f 56 0f 4d 0f 41 0f 38 0f 2f 0f 26 0f 1d 0f 13 .V.M.A.8./.&.... +| 64: 0f 0a 0f 01 0e f7 0e ee 0e e6 0e dd 0e d6 0e cd ................ +| 80: 0e c3 0e ba 0e b0 0e a8 0e 9f 0e 96 0e 8e 0e 85 ................ +| 96: 0e 7c 0e 73 0e 6a 0e 60 0e 58 0e 4f 00 00 00 00 .|.s.j.`.X.O.... +| 3648: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 08 ................ +| 3664: 04 01 10 01 03 34 74 20 07 04 01 0e 01 03 34 1e .....4t ......4. +| 3680: 09 05 01 12 01 03 33 74 68 1c 08 04 01 10 01 03 ......3th....... +| 3696: 33 6e 1a 08 04 01 10 01 03 32 77 18 08 04 01 10 3n.......2w..... +| 3712: 01 03 32 74 16 08 04 01 10 01 03 32 6e 14 07 04 ..2t.......2n... +| 3728: 01 0e 01 03 32 12 08 04 01 10 01 03 31 74 10 08 ....2.......1t.. +| 3744: 04 01 10 01 03 31 6e 0e 07 04 01 0e 01 03 31 0c .....1n.......1. +| 3760: 09 04 01 12 01 03 30 74 68 0a 08 04 01 10 01 03 ......0th....... +| 3776: 30 74 08 09 04 01 12 01 03 30 6e 75 06 08 04 01 0t.......0nu.... +| 3792: 10 01 03 30 6e 04 06 04 01 0c 01 03 02 08 04 01 ...0n........... +| 3808: 10 01 02 34 72 22 07 04 01 0e 01 02 34 20 08 04 ...4r.......4 .. +| 3824: 01 10 01 02 33 72 1e 09 04 01 12 01 02 33 61 72 ....3r.......3ar +| 3840: 1c 08 04 01 10 01 02 32 74 1a 08 04 01 10 02 02 .......2t....... +| 3856: 32 69 18 09 04 01 12 01 02 32 61 72 16 08 04 01 2i.......2ar.... +| 3872: 10 01 02 31 74 14 08 04 01 10 01 02 31 6e 12 08 ...1t.......1n.. +| 3888: 04 01 10 01 02 31 62 10 08 04 01 10 01 02 31 32 .....1b.......12 +| 3904: 0e 0b 04 01 16 01 02 30 74 68 65 72 0c 08 04 01 .......0ther.... +| 3920: 10 01 02 30 74 0a 08 04 01 10 01 02 30 6e 08 08 ...0t.......0n.. +| 3936: 04 01 10 01 02 30 62 06 08 04 01 10 01 02 30 61 .....0b.......0a +| 3952: 04 06 04 01 0c 01 02 02 07 04 09 10 01 34 74 22 .............4t. +| 3968: 06 04 09 0e 01 34 20 08 04 09 12 01 33 74 65 1e .....4 .....3te. +| 3984: 07 04 09 10 01 33 70 1c 07 04 09 10 01 33 66 1a .....3p......3f. +| 4000: 08 04 09 12 01 32 74 68 18 07 04 09 10 01 32 74 .....2th......2t +| 4016: 16 07 04 09 10 01 32 69 14 07 04 09 10 01 32 66 ......2i......2f +| 4032: 12 07 04 09 10 01 31 74 10 07 04 09 10 01 31 69 ......1t......1i +| 4048: 0e 06 04 09 0e 01 31 0c 08 04 09 12 01 30 74 65 ......1......0te +| 4064: 0a 07 04 09 10 01 30 74 08 07 04 09 10 01 30 70 ......0t......0p +| 4080: 06 08 04 09 12 01 30 66 74 04 05 04 09 0c 01 02 ......0ft....... +| page 4 offset 12288 +| 0: 0d 00 00 00 03 0f eb 00 0f f9 0f f2 0f eb 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 05 03 03 00 10 ................ +| 4080: 03 05 05 02 03 00 10 04 06 05 01 03 00 10 04 04 ................ +| page 5 offset 16384 +| 0: 0a 00 00 00 02 0f eb 00 0f eb 0f f4 00 00 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 08 03 15 01 70 ...............p +| 4080: 67 73 7a 18 0b 03 1b 01 76 65 72 73 69 6f 6e 04 gsz.....version. +| page 6 offset 20480 +| 0: 0d 00 00 00 03 0f f2 00 0f fc 0f f7 0f f2 00 00 ................ +| 4080: 00 00 03 03 02 01 03 03 02 02 01 02 02 01 02 09 ................ +| end 89028ffd2c29b679e250.db +}]} {} + +do_catchsql_test 43.1 { + INSERT INTO t1(t1) VALUES('optimize'); +} {/*malformed database schema*/} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 44.1 { + CREATE VIRTUAL TABLE t1 USING fts5(a,b unindexed,c,tokenize="porter ascii"); + REPLACE INTO t1_data VALUES(1,X'03090009'); + REPLACE INTO t1_data VALUES(10,X'000000000103030003010101020101030101'); + INSERT INTO t1_data VALUES(137438953473,X'0000002e023061011202010162010203010163010204010167010601020201016801050102030101690106010204040606060808'); + INSERT INTO t1_data VALUES(274877906945,X'0000001f02306702080201020201016802080301020301016a420804010204040909'); + INSERT INTO t1_data VALUES(412316860417,X'0000002e023061030202010162030203010163030204010167030601020201016803060102030101690306010204040606060808'); + INSERT INTO t1_idx VALUES(1,X'',2); + INSERT INTO t1_idx VALUES(2,X'',2); + INSERT INTO t1_idx VALUES(3,X'',2); + INSERT INTO t1_content VALUES(1,'a b c','d e f','g h i'); + INSERT INTO t1_content VALUES(2,'g h i','a b c','g h i'); + INSERT INTO t1_content VALUES(3,'a b c','g h i','g h i'); + INSERT INTO t1_docsize VALUES(1,X'030003'); + INSERT INTO t1_docsize VALUES(2,X'030003'); + INSERT INTO t1_docsize VALUES(3,X'030003'); +} {} + +do_catchsql_test 44.2 { + INSERT INTO t1(t1) VALUES('integrity-check'); +} {/.*fts5: corrupt.*/} + +do_catchsql_test 44.3 { + SELECT snippet(t1, -1, '.', '..', '', 2 ) FROM t1('g h') ORDER BY rank; +} {0 {{.g.. .h..} {.g.. h} {.g.. .h..}}} + +#-------------------------------------------------------------------------- +reset_db +do_test 45.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 24576 pagesize 4096 filename crash-0b162c9e69b999.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 06 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 06 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 96: 00 00 00 00 0d 00 00 00 06 0e 0f 00 0f aa 0f 53 ...............S +| 112: 0e e8 0e 8b 0e 33 0e 0f 00 00 00 00 00 00 00 00 .....3.......... +| 3584: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 22 ................ +| 3600: 06 06 17 11 11 01 31 74 61 62 6c 65 62 62 62 62 ......1tablebbbb +| 3616: 06 43 52 45 41 54 45 20 54 41 42 4c 45 20 62 62 .CREATE TABLE bb +| 3632: 28 61 29 56 05 06 17 1f 1f 01 7d 74 61 52 6c 65 (a)V.......taRle +| 3648: 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 6f 6e 66 t1_configt1_conf +| 3664: 69 67 05 43 52 45 41 54 45 20 54 41 42 4c 45 20 ig.CREATE TABLE +| 3680: 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b 20 50 52 't1_config'(k PR +| 3696: 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 20 57 49 IMARY KEY, v) WI +| 3712: 54 48 4f 55 54 20 52 4f 57 49 44 5b 04 07 17 21 THOUT ROWID[...! +| 3728: 21 01 81 01 74 61 62 6c 65 74 31 5f 64 6f 73 73 !...tablet1_doss +| 3744: 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 04 43 52 izet1_docsize.CR +| 3760: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 64 EATE TABLE 't1_d +| 3776: 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 54 45 47 ocsize'(id INTEG +| 3792: 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 ER PRIMARY KEY, +| 3808: 73 7a 20 42 4c 4f 42 29 69 03 07 17 19 19 01 81 sz BLOB)i....... +| 3824: 2d 74 61 62 6c 65 74 31 5f 69 64 78 74 31 5f 69 -tablet1_idxt1_i +| 3840: 64 78 03 43 52 45 41 54 45 20 54 41 42 4c 45 20 dx.CREATE TABLE +| 3856: 27 74 31 5f 69 64 78 27 28 73 65 67 69 64 2c 20 't1_idx'(segid, +| 3872: 74 65 72 6d 2c 20 70 67 6e 6f 2c 20 50 52 49 4d term, pgno, PRIM +| 3888: 41 52 59 20 4b 45 59 28 73 65 67 69 64 2c 20 74 ARY KEY(segid, t +| 3904: 65 72 6d 29 29 20 57 49 54 48 4f 55 54 20 52 4f erm)) WITHOUT RO +| 3920: 57 49 44 55 02 07 17 1b 1b 01 81 01 74 61 62 6c WIDU........tabl +| 3936: 65 74 31 5f 64 61 74 61 74 31 5f 64 61 74 61 02 et1_datat1_data. +| 3952: 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 CREATE TABLE 't1 +| 3968: 5f 64 61 74 61 27 28 69 64 20 49 4e 54 45 47 45 _data'(id INTEGE +| 3984: 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 62 R PRIMARY KEY, b +| 4000: 6c 6f 63 6b 20 42 4c 4f 42 29 54 01 07 17 11 11 lock BLOB)T..... +| 4016: 08 81 15 74 61 62 6c 65 74 31 74 31 43 52 45 41 ...tablet1t1CREA +| 4032: 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45 TE VIRTUAL TABLE +| 4048: 20 74 31 20 55 53 49 4e 47 20 66 74 73 35 28 61 t1 USING fts5(a +| 4064: 2c 62 2c 70 72 65 66 69 78 3d 22 31 2c 32 2c 33 ,b,prefix=.1,2,3 +| 4080: 2c 34 22 2c 20 63 6f 6e 74 65 6e 74 3d 22 22 29 ,4., content=..) +| page 2 offset 4096 +| 0: 0d 0b 6a 00 37 09 4c 02 0f e7 09 4c 0f c6 0f a4 ..j.7.L....L.... +| 16: 0f 88 0f 6d 0f 4b 0f 2c 0f 0e 0e ec 0e cd 0e ad ...m.K.,........ +| 32: 0e 8e 0e 6c 0e 4b 0e 29 0e 08 0d e6 0d c4 0d b5 ...l.K.)........ +| 48: 0d 97 0d 76 0d 54 0d 31 0d 15 0c f3 0c d3 0c b5 ...v.T.1........ +| 64: 0c 95 0c 73 0c 54 0c 32 0c 10 0b ee 0b cc 0b b0 ...s.T.2........ +| 80: 0b 8d 0b 7e 0b 48 0b 2e 0b 0b 0a ef 0a cc 0a ad ...~.H.......... +| 96: 0a 8c 0a 6d 0a 4d 0a 2b 0a 0c 09 ec 09 ca 09 a8 ...m.M.+........ +| 112: 09 86 09 63 0f f1 00 00 00 00 00 00 00 00 00 00 ...c............ +| 2368: 00 00 00 00 00 00 00 00 00 00 00 00 15 0a 03 00 ................ +| 2384: 30 00 00 00 01 01 03 35 00 03 01 01 12 02 01 12 0......5........ +| 2400: 03 01 11 1c 8c 80 80 80 80 10 03 00 3e 00 00 00 ............>... +| 2416: 17 01 05 05 34 74 61 62 6c 03 02 03 01 04 77 68 ....4tabl.....wh +| 2432: 65 72 03 02 06 09 1b 8c 80 80 80 80 0f 03 00 3c er.............< +| 2448: 00 00 00 16 05 34 66 75 73 34 03 02 02 01 04 6e .....4fus4.....n +| 2464: 75 6d 62 03 06 01 04 09 1b 8c 80 80 80 80 0e 03 umb............. +| 2480: 00 3c 00 00 00 16 04 33 74 68 65 03 06 01 01 04 .<.....3the..... +| 2496: 01 03 77 68 65 03 02 04 04 0a 1b 8c 80 80 80 80 ..whe........... +| 2512: 0d 03 00 3c 00 00 00 16 04 33 6e 74 6d 03 06 01 ...<.....3ntm... +| 2528: 01 05 01 03 74 61 62 05 62 03 04 0a 19 8c 80 80 ....tab.b....... +| 2544: 80 80 0c 03 00 38 00 00 00 14 03 32 77 68 03 02 .....8.....2wh.. +| 2560: 04 00 04 33 66 74 73 03 02 02 04 07 18 8c 80 80 ...3fts......... +| 2576: 80 80 0b 03 00 36 00 00 00 13 03 32 74 61 03 02 .....6.....2ta.. +| 2592: 03 02 01 68 03 06 01 01 04 04 07 1b 8c 80 80 80 ...h............ +| 2608: 80 0a 03 00 3c 00 00 00 16 03 32 6e 75 03 06 01 ....<.....2nu... +| 2624: 01 05 01 02 6f 66 03 06 01 01 06 04 09 19 8c 80 ....of.......... +| 2640: 80 80 80 09 03 00 38 00 00 00 14 03 32 66 74 03 ......8.....2ft. +| 2656: 02 02 01 02 69 73 03 06 01 01 03 04 07 18 8c 80 ....is.......... +| 2672: 80 80 80 08 03 00 36 00 00 00 13 02 31 74 03 08 ......6.....1t.. +| 2688: 03 01 01 04 01 01 77 03 02 04 04 09 1a 8c 80 80 ......w......... +| 2704: 80 80 07 03 00 3a ff 00 00 15 02 31 6e 03 08 01 .....:.....1n... +| 2720: 01 02 05 01 01 6f 03 06 01 01 06 04 09 18 8c 80 .....o.......... +| 2736: 80 80 80 06 03 00 36 00 00 00 13 04 02 31 66 03 ......6......1f. +| 2752: 02 02 01 01 69 03 06 01 01 03 05 06 1c 8c 80 80 ....i........... +| 2768: 80 80 05 03 00 3e 00 00 00 17 04 30 74 68 65 03 .....>.....0the. +| 2784: 06 01 01 04 01 05 77 68 65 72 65 03 02 04 0a 15 ......where..... +| 2800: 8c 80 80 80 80 04 03 00 30 00 00 00 11 01 01 06 ........0....... +| 2816: 06 30 74 61 62 6c 65 03 02 03 07 1c 8c 80 80 80 .0table......... +| 2832: 80 03 03 00 3e 00 00 00 17 07 30 6e 75 6d 62 65 ....>.....0numbe +| 2848: 72 03 06 01 01 05 01 02 6f 66 03 06 04 0d 13 8c r.......of...... +| 2864: 80 80 80 80 02 03 00 2c 00 00 00 0f 01 01 03 02 .......,........ +| 2880: 30 6e 03 06 01 01 02 07 1b 8c 80 80 80 80 01 03 0n.............. +| 2896: 00 3c 00 00 00 16 08 30 66 74 73 34 61 75 78 03 .<.....0fts4aux. +| 2912: 02 02 01 02 69 73 03 06 04 0c 00 00 00 14 2a 00 ....is........*. +| 2928: 00 00 01 01 02 24 00 02 01 01 12 02 01 12 08 88 .....$.......... +| 2944: 80 80 80 80 11 03 00 16 00 00 01 05 02 1c 88 80 ................ +| 2960: 80 80 80 11 03 00 3e 00 00 00 17 05 34 72 6f 77 ......>.....4row +| 2976: 73 02 06 01 01 05 01 04 74 68 65 72 02 02 04 0b s.......ther.... +| 2992: 15 88 80 80 80 80 10 03 00 30 00 00 00 11 02 01 .........0...... +| 3008: 01 07 05 34 62 65 74 77 02 02 04 08 1b 88 80 80 ...4betw........ +| 3024: 80 80 0f 03 00 3c 00 00 00 16 04 04 33 72 6f 77 .....<......3row +| 3040: 02 06 01 01 05 01 03 74 68 65 02 08 05 0a 1b 88 .......the...... +| 3056: 80 80 80 80 0e 03 00 3c 00 00 00 16 02 01 02 04 .......<........ +| 3072: 33 61 72 65 02 02 03 01 03 62 65 74 02 02 07 08 3are.....bet.... +| 3088: 1b 88 80 80 80 80 0d 03 00 3c 00 00 00 16 03 32 .........<.....2 +| 3104: 74 68 02 08 02 01 01 07 00 04 33 61 6e 64 02 06 th........3and.. +| 3120: 04 0a 1b 88 80 80 80 80 0c 03 00 3c 00 00 00 16 ...........<.... +| 3136: 03 32 69 6e 02 06 01 01 06 01 02 72 6f 02 06 01 .2in.......ro... +| 3152: 01 05 04 09 18 88 80 80 80 80 0b 03 00 36 00 00 .............6.. +| 3168: 00 13 02 03 32 61 72 02 02 03 01 02 62 65 02 02 ....2ar.....be.. +| 3184: 04 05 07 1b 88 80 80 80 80 0a 03 00 3c 00 00 00 ............<... +| 3200: 16 02 31 74 02 08 02 01 01 07 00 03 32 61 6e 02 ..1t........2an. +| 3216: 06 01 01 04 09 19 88 80 80 80 80 09 03 00 38 00 ..............8. +| 3232: 00 00 14 02 31 6e 02 06 01 01 03 01 01 72 02 06 ....1n.......r.. +| 3248: 01 01 05 04 08 17 88 80 80 80 80 08 03 00 34 00 ..............4. +| 3264: 00 00 12 02 31 62 02 02 04 01 01 69 02 06 01 01 ....1b.....i.... +| 3280: 06 04 06 19 88 80 80 80 80 07 03 00 38 00 00 00 ............8... +| 3296: 14 04 02 31 32 02 02 05 01 01 61 02 08 03 01 01 ...12.....a..... +| 3312: 02 05 06 1b 88 80 80 80 80 06 03 00 3c 00 00 00 ............<... +| 3328: 16 06 30 74 68 65 72 65 02 02 02 00 02 31 31 02 ..0there.....11. +| 3344: 06 01 01 04 0a 15 88 80 80 80 80 05 03 00 30 00 ..............0. +| 3360: 00 00 11 01 01 05 04 30 74 68 65 02 06 01 01 07 .......0the..... +| 3376: 07 1c 88 80 80 80 80 04 03 00 3e 00 00 00 17 01 ..........>..... +| 3392: 01 06 02 30 6e 02 06 01 01 03 01 04 72 6f 77 73 ...0n.......rows +| 3408: 02 06 07 08 1b 88 80 80 80 80 03 03 00 3c 00 00 .............<.. +| 3424: 00 16 08 30 62 65 74 77 65 65 6e 02 02 04 01 02 ...0between..... +| 3440: 69 6e 02 06 04 0c 1a 88 80 80 80 80 02 03 00 3a in.............: +| 3456: 00 00 00 15 04 30 61 6e 64 02 06 01 01 02 02 02 .....0and....... +| 3472: 72 65 02 02 03 04 0a 17 88 80 80 80 80 01 03 00 re.............. +| 3488: 34 00 00 00 12 02 30 31 02 06 01 01 04 01 01 32 4.....01.......2 +| 3504: 02 02 06 04 08 08 84 80 80 80 80 12 03 00 16 00 ................ +| 3520: 00 00 05 04 1b 84 80 80 80 80 11 03 00 3c 00 00 .............<.. +| 3536: 00 16 05 34 74 61 62 6c 01 06 01 01 05 02 03 65 ...4tabl.......e +| 3552: 72 6d 01 02 04 0b 1b 84 80 80 80 80 10 03 00 3c rm.............< +| 3568: 00 00 00 16 05 34 65 61 63 68 01 02 03 01 04 70 .....4each.....p +| 3584: 72 65 73 01 02 05 04 09 1a 84 80 80 80 80 0f 03 res............. +| 3600: 00 3a 00 00 00 15 04 33 74 65 72 01 02 04 02 02 .:.....3ter..... +| 3616: 68 65 01 06 01 01 03 04 08 1b 84 80 80 80 80 0e he.............. +| 3632: 03 00 3c 00 00 00 16 04 33 70 72 65 01 02 05 01 ..<.....3pre.... +| 3648: 03 74 61 62 01 06 01 01 05 04 08 1a 84 80 80 80 .tab............ +| 3664: 80 0d 03 00 3a 00 00 00 15 04 33 66 6f 72 01 02 ....:.....3for.. +| 3680: 02 02 02 74 73 01 06 01 01 04 04 08 1b 84 80 80 ...ts........... +| 3696: 80 80 0c 03 00 3c 00 00 00 16 03 32 74 68 01 06 .....<.....2th.. +| 3712: 01 01 03 00 04 33 65 61 63 01 02 03 04 09 18 84 .....3eac....... +| 3728: 80 80 80 80 0b 03 00 36 00 00 00 13 03 32 74 61 .......6.....2ta +| 3744: 01 06 01 01 05 02 01 65 01 02 04 04 09 19 84 80 .......e........ +| 3760: 80 80 80 0a 03 00 38 00 00 00 14 03 32 69 6e 01 ......8.....2in. +| 3776: 06 01 01 02 01 02 70 72 01 02 05 04 09 18 84 80 ......pr........ +| 3792: 80 80 80 09 03 00 36 00 00 00 13 03 32 66 6f 01 ......6.....2fo. +| 3808: 02 02 02 01 74 01 06 01 01 04 04 07 1b 84 80 80 ....t........... +| 3824: 80 80 08 03 00 3c 00 00 00 16 02 31 74 01 0a 04 .....<.....1t... +| 3840: 01 01 03 04 00 03 32 65 61 01 02 03 04 0a 17 84 ......2ea....... +| 3856: 80 80 80 80 07 03 00 34 00 00 00 12 02 31 69 01 .......4.....1i. +| 3872: 06 01 01 02 01 01 70 01 02 05 04 08 18 84 80 80 ......p......... +| 3888: 80 80 06 03 00 36 00 00 00 13 02 31 65 01 02 03 .....6.....1e... +| 3904: 01 01 66 01 08 02 01 01 04 04 06 1b 84 80 80 80 ..f............. +| 3920: 80 05 03 00 3c 00 00 00 16 05 30 74 65 72 6d 01 ....<.....0term. +| 3936: 02 04 02 02 68 65 01 06 01 01 03 04 09 14 84 80 ....he.......... +| 3952: 80 80 80 04 03 00 2e 00 00 00 10 06 30 74 61 62 ............0tab +| 3968: 6c 65 01 06 00 01 05 04 15 84 80 80 80 80 03 03 le.............. +| 3984: 00 30 00 00 00 11 02 08 30 70 72 65 73 65 6e 74 .0......0present +| 4000: 01 02 05 05 1b 84 80 80 80 80 02 03 00 3c 00 00 .............<.. +| 4016: 00 16 04 30 66 74 73 01 06 01 01 04 01 02 69 6e ...0fts.......in +| 4032: 01 06 01 01 04 0a 1a 84 80 80 80 80 01 03 00 3a ...............: +| 4048: 00 00 00 15 05 30 65 61 63 68 01 02 03 01 03 66 .....0each.....f +| 4064: 6f 72 01 02 02 04 09 06 01 03 00 12 03 0b 0f 00 or.............. +| 4080: 00 08 8c 80 80 80 80 11 03 00 16 00 00 00 05 04 ................ +| page 3 offset 8192 +| 0: 0a 00 00 00 32 0e 4f 00 0f fa 10 f1 0f e9 0f e1 ....2.O......... +| 16: 0f d8 0f d1 0f c9 0f c1 0f b9 0f b1 0f a9 0f a0 ................ +| 32: 0f 98 0f 90 0f 87 0f 80 0f 78 0f 71 0f 68 0f 5f .........x.q.h._ +| 48: bd 56 0f 4d 0f 41 0f 38 0f 2f 0f 26 0f 1d 0f 13 .V.M.A.8./.&.... +| 64: 0f 0a 0f 01 0e f7 0e ee 0e e6 0e dd 0e d6 0e cd ................ +| 80: 0e c3 0e ba 0e b0 0e a8 0e 9f 0e 96 0e 8e 0e 85 ................ +| 96: 0e 7c 0e 73 0e 6a 0e 60 0e 58 0e 4f 00 00 00 00 .|.s.j.`.X.O.... +| 3648: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 08 ................ +| 3664: 04 01 10 2f 03 34 74 20 07 04 01 0e 01 03 34 1e .../.4t ......4. +| 3680: 09 04 01 12 01 03 33 74 68 1c 08 04 01 10 01 03 ......3th....... +| 3696: 33 6e 1a 08 04 01 10 01 03 32 77 18 08 04 01 10 3n.......2w..... +| 3712: 01 03 32 74 16 08 04 01 10 01 03 32 6e 14 07 04 ..2t.......2n... +| 3728: 01 0e 01 03 32 12 08 04 01 10 01 03 31 74 10 08 ....2.......1t.. +| 3744: 04 01 10 01 03 31 6e 0e 07 04 01 0e 02 03 31 0c .....1n.......1. +| 3760: 09 04 01 12 01 03 30 74 68 0a 08 04 01 10 01 03 ......0th....... +| 3776: 30 74 08 09 04 01 12 01 03 30 6e 75 06 08 04 01 0t.......0nu.... +| 3792: 10 01 03 30 6e 04 06 04 01 0c 01 05 52 08 04 01 ...0n.......R... +| 3808: 10 01 02 34 72 22 07 04 01 0e 01 02 34 20 08 04 ...4r.......4 .. +| 3824: 01 10 01 02 33 72 1e 09 04 01 12 01 02 33 61 72 ....3r.......3ar +| 3840: 1c 08 04 01 10 01 02 32 74 1a 08 04 01 10 b3 02 .......2t....... +| 3856: 32 69 18 09 04 01 12 01 02 32 61 72 16 08 04 01 2i.......2ar.... +| 3872: 10 01 02 31 74 14 08 04 01 10 01 02 31 6e 12 08 ...1t.......1n.. +| 3888: 04 01 10 01 02 31 62 10 08 04 01 10 01 02 31 32 .....1b.......12 +| 3904: 0e 0b 04 01 16 01 02 30 74 68 65 72 0c 08 04 01 .......0ther.... +| 3920: 10 01 02 30 74 0a 08 04 01 10 01 02 30 6e 08 08 ...0t.......0n.. +| 3936: 04 01 10 01 02 30 62 06 08 04 01 10 01 02 30 61 .....0b.......0a +| 3952: 04 06 04 01 0c 01 02 02 07 04 09 10 01 34 74 22 .............4t. +| 3968: 06 04 09 0e 01 34 20 08 04 09 12 01 33 74 65 1e .....4 .....3te. +| 3984: 07 04 09 10 01 33 70 1c 07 04 09 10 01 33 66 1a .....3p......3f. +| 4000: 08 04 09 12 01 32 74 68 18 07 04 09 10 01 32 74 .....2th......2t +| 4016: 16 07 04 09 10 01 32 69 14 07 04 09 10 01 32 66 ......2i......2f +| 4032: 12 07 04 09 10 01 31 74 10 07 04 09 10 01 31 69 ......1t......1i +| 4048: 0e 06 04 09 0e 01 31 0c 08 04 09 12 01 30 74 65 ......1......0te +| 4064: 0a 07 04 09 10 01 30 74 08 07 04 09 10 01 30 70 ......0t......0p +| 4080: 06 08 04 09 12 01 30 66 74 04 05 04 09 0c 01 02 ......0ft....... +| page 4 offset 12288 +| 0: 0d 00 00 00 03 0f eb 00 0f f9 0f f2 0f eb 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 05 03 03 00 10 ................ +| 4080: 03 05 05 02 03 00 10 04 06 05 01 03 00 10 04 04 ................ +| page 5 offset 16384 +| 0: 0a 00 00 00 02 0f eb 00 0f eb 0f f4 00 00 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 08 03 15 01 70 ...............p +| 4080: 67 73 7a 18 0b 03 1b 01 76 65 72 73 69 6f 6e 04 gsz.....version. +| page 6 offset 20480 +| 0: 0d 00 00 00 03 0f f2 00 0f fc 0f f7 0f f2 00 00 ................ +| 4080: 00 00 03 03 02 01 03 03 02 02 01 02 02 01 02 09 ................ +}]} {} + +do_catchsql_test 45.2 { + INSERT INTO t1(t1, rank) VALUES('merge', 5); + INSERT INTO t1(t1, rank) VALUES('merge', 5); + INSERT INTO t1(t1, rank) VALUES('merge', 5); + INSERT INTO t1(t1, rank) VALUES('merge', 5); + INSERT INTO t1(t1, rank) VALUES('merge', 5); + INSERT INTO t1(t1, rank) VALUES('merge', 5); + INSERT INTO t1(t1, rank) VALUES('merge', 5); + INSERT INTO t1(t1, rank) VALUES('merge', 5); + INSERT INTO t1(t1, rank) VALUES('merge', 5); + INSERT INTO t1(t1, rank) VALUES('merge', 5); + INSERT INTO t1(t1, rank) VALUES('merge', 5); + INSERT INTO t1(t1, rank) VALUES('merge', 5); + INSERT INTO t1(t1, rank) VALUES('merge', 5); + INSERT INTO t1(t1, rank) VALUES('merge', 5); + INSERT INTO t1(t1, rank) VALUES('merge', 5); + INSERT INTO t1(t1, rank) VALUES('merge', 5); +} {/*malformed database schema*/} + +#-------------------------------------------------------------------------- +reset_db +do_test 46.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 32768 pagesize 4096 filename crash-1ee8bd451dd1ad.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 08 .....@ ........ +| 32: 00 00 00 02 00 00 00 01 00 00 00 09 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 96: 00 00 00 00 0d 0f c7 00 07 0d 92 00 0f 8d 0f 36 ...............6 +| 112: 0e cb 0e 6b 0e 0e 0d b6 0d 92 0d 92 00 00 00 00 ...k............ +| 3472: 00 00 22 08 06 17 11 11 01 31 74 61 62 6c 65 74 .........1tablet +| 3488: 32 74 32 08 43 52 45 41 54 45 20 54 41 42 4c 45 2t2.CREATE TABLE +| 3504: 20 74 32 28 78 29 56 07 06 17 1f 1f 01 7d 74 60 t2(x)V.......t` +| 3520: 62 6c 65 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 blet1_configt1_c +| 3536: 6f 6e 66 69 67 07 43 52 45 41 54 45 20 54 41 42 onfig.CREATE TAB +| 3552: 4c 45 20 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b LE 't1_config'(k +| 3568: 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 PRIMARY KEY, v) +| 3584: 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 5b 06 WITHOUT ROWID[. +| 3600: 07 17 21 21 01 81 01 74 61 62 6c 65 74 31 5f 64 ..!!...tablet1_d +| 3616: 6f 63 73 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 ocsizet1_docsize +| 3632: 06 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 .CREATE TABLE 't +| 3648: 31 5f 64 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 1_docsize'(id IN +| 3664: 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE +| 3680: 59 2c 20 73 7a 20 42 4c 4f 42 29 5e 05 07 17 21 Y, sz BLOB)^...! +| 3696: 21 01 81 07 74 61 62 6c 65 74 31 5f 63 6f 6e 74 !...tablet1_cont +| 3712: 65 6e 74 74 31 5f 63 6f 6e 74 65 6e 74 05 43 52 entt1_content.CR +| 3728: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 EATE TABLE 't1_c +| 3744: 6f 6e 74 65 6e 74 27 28 69 64 20 49 4e 54 45 47 ontent'(id INTEG +| 3760: 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 ER PRIMARY KEY, +| 3776: 63 30 2c 20 63 31 2c 20 63 32 29 69 04 07 17 19 c0, c1, c2)i.... +| 3792: 19 01 81 2d 74 61 62 6c 65 74 31 5f 69 64 78 74 ...-tablet1_idxt +| 3808: 31 5f 69 64 78 04 43 52 45 41 54 45 20 54 41 42 1_idx.CREATE TAB +| 3824: 4c 45 20 27 74 31 5f 69 64 78 27 28 73 65 67 69 LE 't1_idx'(segi +| 3840: 64 2c 20 74 65 72 6d 2c 20 70 67 6e 6f 2c 20 50 d, term, pgno, P +| 3856: 52 49 4d 41 52 59 20 4b 45 59 28 73 65 67 69 64 RIMARY KEY(segid +| 3872: 2c 20 74 65 72 6d 29 29 20 57 49 54 48 4f 55 54 , term)) WITHOUT +| 3888: 20 52 4f 57 49 44 55 03 07 17 1b 1b 01 81 01 74 ROWIDU........t +| 3904: 61 62 6c 65 74 31 5f 64 61 74 61 74 31 5f 64 61 ablet1_datat1_da +| 3920: 74 61 03 43 52 45 41 54 45 20 54 41 42 4c 45 20 ta.CREATE TABLE +| 3936: 27 74 31 5f 64 61 74 61 27 28 69 64 20 49 4e 54 't1_data'(id INT +| 3952: 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 EGER PRIMARY KEY +| 3968: 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 29 38 02 06 , block BLOB)8.. +| 3984: 17 11 11 08 5f 74 61 62 6c 65 74 31 74 31 43 52 ...._tablet1t1CR +| 4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 35 LE t1 USING fts5 +| 4032: 28 61 2c 62 2c 63 29 00 00 00 39 00 00 00 00 00 (a,b,c)...9..... +| page 3 offset 8192 +| 0: 0d 00 00 00 03 0c 94 00 0f e6 0f ef 0c 94 00 00 ................ +| 3216: 00 00 00 00 86 4a 84 80 80 80 80 01 04 00 8d 18 .....J.......... +| 3232: 00 00 03 2b 02 30 30 01 02 06 01 02 06 01 02 06 ...+.00......... +| 3248: 1f 02 03 01 02 03 01 02 03 01 08 32 30 31 36 30 ...........20160 +| 3264: 36 30 39 01 02 07 01 02 07 01 02 07 01 01 34 01 609...........4. +| 3280: 02 05 01 02 05 01 02 05 01 01 35 01 02 04 01 02 ..........5..... +| 3296: 04 01 02 04 02 07 30 30 30 30 30 30 30 1c 02 3d ......0000000..= +| 3312: 01 02 04 01 02 04 01 06 62 69 6e 61 72 79 03 06 ........binary.. +| 3328: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3344: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3360: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 ................ +| 3376: 03 06 01 02 02 03 06 01 02 02 01 08 63 6f 6d 70 ............comp +| 3392: 69 6c 65 72 01 02 02 01 02 02 01 02 02 01 06 64 iler...........d +| 3408: 62 73 74 61 74 07 02 03 01 02 03 01 02 03 02 04 bstat........... +| 3424: 65 62 75 67 04 02 02 01 02 02 01 02 02 01 06 65 ebug...........e +| 3440: 6e 61 62 6c 65 07 02 02 01 02 02 01 02 02 01 02 nable........... +| 3456: 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 ................ +| 3472: 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 ................ +| 3488: 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 ................ +| 3504: 02 01 02 02 02 08 78 74 65 6e 73 69 6f 6e 1f 02 ......xtension.. +| 3520: 04 01 02 04 01 02 04 01 04 66 74 73 34 0a 02 03 .........fts4... +| 3536: 01 02 03 01 02 03 04 01 35 0d 02 03 01 02 03 01 ........5....... +| 3552: 02 03 01 03 67 63 63 01 02 03 01 02 03 01 02 03 ....gcc......... +| 3568: 02 06 65 6f 70 6f 6c 79 10 02 03 01 02 03 01 02 ..eopoly........ +| 3584: 03 01 05 6a 73 6f 6e 31 13 02 03 01 02 03 01 02 ...json1........ +| 3600: 03 01 04 6c 6f 61 64 1f 02 03 01 02 03 01 02 03 ...load......... +| 3616: 01 03 6d 61 78 1c 02 02 01 02 02 01 02 02 02 05 ..max........... +| 3632: 65 6d 6f 72 79 1c 02 03 01 02 03 01 02 03 04 04 emory........... +| 3648: 73 79 73 35 16 02 03 01 02 03 01 02 03 01 06 6e sys5...........n +| 3664: 6f 63 61 73 65 02 06 01 02 02 03 06 01 02 02 03 ocase........... +| 3680: 06 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 ................ +| 3696: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3712: 02 02 03 07 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3728: 02 01 04 6f 6d 69 74 1f 02 02 01 02 02 01 02 02 ...omit......... +| 3744: 01 05 72 74 72 65 65 19 02 03 01 02 03 01 02 03 ..rtree......... +| 3760: 04 02 69 6d 01 06 01 02 02 03 06 01 02 02 03 06 ..im............ +| 3776: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3792: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3808: 02 03 06 01 02 02 03 06 06 e2 02 03 06 01 02 02 ................ +| 3824: 01 0a 74 68 72 65 61 64 73 61 66 65 22 02 02 01 ..threadsafe.... +| 3840: 02 02 01 02 02 01 04 76 74 61 62 07 02 04 01 02 .......vtab..... +| 3856: 04 01 02 04 01 01 78 01 06 01 01 02 01 06 01 01 ......x......... +| 3872: 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 ................ +| 3888: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 3904: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 3920: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 ................ +| 3936: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 ................ +| 3952: 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 ................ +| 3968: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 3984: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 4000: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 ................ +| 4016: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 ................ +| 4032: 02 01 06 01 01 02 01 06 01 01 02 04 15 13 0c 0c ................ +| 4048: 12 44 13 11 0f 47 13 0f 0c 0e 11 10 0f 0e 10 0f .D...G.......... +| 4064: 44 0f 10 40 15 0f 07 01 03 00 14 24 5a 24 24 0f D..@.......$Z$$. +| 4080: 0a 03 00 24 00 00 00 00 01 01 01 00 01 01 01 01 ...$............ +| page 4 offset 12288 +| 0: 0a 00 00 00 01 0f fa 00 0f fa 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 05 04 09 0c 01 02 ................ +| page 5 offset 16384 +| 0: 0d 00 00 00 24 0c 0a 00 0f d8 0f af 0f 86 0f 74 ....$..........t +| 16: 0f 61 0f 4e 0f 2f 0f 0f 0e ef 0e d7 0e be 0e a5 .a.N./.......... +| 32: 0e 8d 0e 74 0e 5b 0e 40 0e 24 0e 08 0d ef 0d d5 ...t.[.@.$...... +| 48: 0d bb 0d a0 0d 84 0d 68 0d 4f 0d 35 0d 1b 0c fb .......h.O.5.... +| 64: 0c da 0c b9 0c 99 0c 78 0c 57 0c 3e 0c 24 0c 0a .......x.W.>.$.. +| 3072: 00 00 00 00 00 00 00 00 00 00 18 24 05 00 25 0f ...........$..%. +| 3088: 19 54 48 52 45 41 44 53 41 46 45 3d 30 58 42 49 .THREADSAFE=0XBI +| 3104: 4e 41 52 59 18 23 05 00 25 0f 19 54 48 52 45 41 NARY.#..%..THREA +| 3120: 44 53 31 46 45 3d 30 58 4e 4f 43 41 53 45 17 22 DS1FE=0XNOCASE.. +| 3136: 05 00 25 0f 17 54 48 52 45 41 44 53 41 46 45 3d ..%..THREADSAFE= +| 3152: 30 58 52 54 52 49 4d 1f 21 05 00 33 0f 19 4f 4d 0XRTRIM.!..3..OM +| 3168: 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 4f IT LOAD EXTENSIO +| 3184: 4e 58 42 49 4e 41 52 59 1f 20 05 00 33 0f 19 4f NXBINARY. ..3..O +| 3200: 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 MIT LOAD EXTENSI +| 3216: 4f 4e 58 4e 4f 43 41 53 45 1e 1f 05 00 33 0f 17 ONXNOCASE....3.. +| 3232: 4f 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 OMIT LOAD EXTENS +| 3248: 49 4f 4e 58 52 54 52 49 4d 1f 1e 05 00 33 0f 19 IONXRTRIM....3.. +| 3264: 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 30 MAX MEMORY=50000 +| 3280: 30 30 30 58 42 49 4e 41 52 59 1f 1d 05 00 33 0f 000XBINARY....3. +| 3296: 19 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 .MAX MEMORY=5000 +| 3312: 30 30 30 30 58 4e 4f 43 41 53 45 1e 1c 05 00 33 0000XNOCASE....3 +| 3328: 0f 17 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 ..MAX MEMORY=500 +| 3344: 30 30 30 30 30 58 52 54 52 49 4d 18 1b 05 00 25 00000XRTRIM....% +| 3360: 0f 19 45 4e 41 42 4c 45 20 52 54 52 45 45 58 42 ..ENABLE RTREEXB +| 3376: 49 4e 41 52 59 18 1a 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3392: 4c 45 20 52 54 52 45 45 58 4e 4f 43 41 53 45 17 LE RTREEXNOCASE. +| 3408: 19 05 00 25 0f 17 45 4e 40 42 4c 45 20 52 54 52 ...%..EN@BLE RTR +| 3424: 45 45 58 52 54 52 49 4d 1a 18 05 00 29 0f 19 45 EEXRTRIM....)..E +| 3440: 4e 41 42 4c 45 20 4d 45 4d 53 59 53 35 58 42 49 NABLE MEMSYS5XBI +| 3456: 4e 41 52 59 1a 17 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3472: 45 20 4d 45 4d 53 59 53 35 58 4e 4f 43 41 53 45 E MEMSYS5XNOCASE +| 3488: 19 16 05 00 29 0f 17 45 4e 41 42 4c 45 20 4d 45 ....)..ENABLE ME +| 3504: 4d 53 59 53 35 58 52 54 52 49 4d 18 15 05 00 25 MSYS5XRTRIM....% +| 3520: 0f 19 45 4e 41 42 4c 45 20 4a 53 4f 4e 31 59 42 ..ENABLE JSON1YB +| 3536: 49 4e 41 52 59 18 14 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3552: 4c 45 20 4a 53 4f 4e 31 58 4e 4f 43 41 53 45 17 LE JSON1XNOCASE. +| 3568: 13 05 00 25 0f 17 45 4e 41 42 4c 45 20 4a 53 4f ...%..ENABLE JSO +| 3584: 4e 31 58 52 54 52 49 4d 1a 12 05 00 29 0f 19 45 N1XRTRIM....)..E +| 3600: 4e 41 42 4c 45 20 47 45 4f 50 4f 4c 59 58 42 49 NABLE GEOPOLYXBI +| 3616: 4e 41 52 59 1a 11 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3632: 45 20 47 45 4f 50 4f 4c 59 58 4e 4f 43 41 53 45 E GEOPOLYXNOCASE +| 3648: 19 10 05 00 29 0f 17 45 4e 41 42 4c 45 20 47 45 ....)..ENABLE GE +| 3664: 4f 50 4f 4c 59 58 52 54 52 49 4d 17 0f 15 00 23 OPOLYXRTRIM....# +| 3680: 0f 19 45 4e 41 42 4c 45 20 46 54 53 35 58 42 49 ..ENABLE FTS5XBI +| 3696: 4e 41 52 59 17 0e 05 00 23 0f 19 45 4e 41 42 4c NARY....#..ENABL +| 3712: 45 20 46 54 53 35 58 4e 4f 43 41 53 45 16 0d 05 E FTS5XNOCASE... +| 3728: 00 23 0f a4 45 4e 41 42 4c 45 20 46 54 53 35 58 .#..ENABLE FTS5X +| 3744: 52 54 52 49 4d 17 0c 05 00 23 0f 19 45 4e 41 42 RTRIM....#..ENAB +| 3760: 4c 45 20 46 54 53 34 58 42 49 4e 41 52 59 17 0b LE FTS4XBINARY.. +| 3776: 05 00 23 0f 19 45 4e 41 42 4c 45 20 46 54 53 34 ..#..ENABLE FTS4 +| 3792: 58 4e 4f 43 41 53 45 16 0a 05 00 23 0f 17 45 4e XNOCASE....#..EN +| 3808: 41 42 4c 45 20 46 54 53 34 58 52 54 52 49 4d 1e ABLE FTS4XRTRIM. +| 3824: 09 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3840: 54 41 54 20 56 54 41 42 58 42 49 4e 41 52 59 1e TAT VTABXBINARY. +| 3856: 08 04 f0 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3872: 54 41 54 20 56 54 41 42 58 4e 4f 43 41 53 45 1d TAT VTABXNOCASE. +| 3888: 07 05 00 31 0f 17 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3904: 54 41 54 20 56 54 41 42 58 52 54 52 49 4d 11 06 TAT VTABXRTRIM.. +| 3920: 05 00 17 0f 19 44 45 42 55 47 58 42 49 4e 41 52 .....DEBUGXBINAR +| 3936: 59 11 05 05 00 17 0f 19 44 45 42 55 47 58 4e 4f Y.......DEBUGXNO +| 3952: 43 41 53 45 10 04 05 00 17 0f 17 44 45 42 55 47 CASE.......DEBUG +| 3968: 58 52 54 52 49 4d 27 03 05 00 43 0f 19 43 4f 4d XRTRIM'...C..COM +| 3984: 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e 30 20 PILER=gcc-5.4.0 +| 4000: 32 30 31 36 30 36 30 39 58 42 49 4e 41 52 59 27 20160609XBINARY' +| 4016: 02 05 00 43 0f 19 43 4f 4d 50 49 4c 45 52 3d 67 ...C..COMPILER=g +| 4032: 63 63 2d 35 2e 34 2e 30 20 32 30 31 36 30 36 30 cc-5.4.0 2016060 +| 4048: 39 58 4e 4f 43 41 53 45 26 01 05 00 43 0f 17 43 9XNOCASE&...C..C +| 4064: 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e OMPILER=gcc-5.4. +| 4080: 30 20 32 30 31 36 30 36 30 39 58 52 54 52 49 4d 0 20160609XRTRIM +| page 6 offset 20480 +| 0: 0d 00 00 00 24 0e e0 00 0f f8 0f f0 0f e8 0f e0 ....$........... +| 16: 0f d8 0f d0 0f c8 0f c0 0f b8 0f b0 0f a8 0f a0 ................ +| 32: 0f 98 0f 90 0f 88 0f 80 0f 78 0f 70 0f 68 0f 60 .........x.p.h.` +| 48: 0f 58 0f 50 0f 48 0f 40 0f 38 0f 30 0f 28 0f 20 .X.P.H.@.8.0.(. +| 64: 0f 18 0f 10 0f 08 0f 00 0e f8 0e f0 0e e8 0e e0 ................ +| 3808: 06 24 03 00 12 02 01 01 06 23 03 00 12 02 01 01 .$.......#...... +| 3824: 06 22 03 00 12 02 01 01 06 21 03 00 12 03 01 01 .........!...... +| 3840: 06 20 03 00 22 03 01 01 06 1f 03 00 12 03 01 01 . .............. +| 3856: 06 1e 03 00 12 03 01 01 06 1d 03 00 12 03 01 01 ................ +| 3872: 06 1c 03 00 12 03 01 01 06 1b 03 00 12 02 01 01 ................ +| 3888: 06 1a 03 00 12 02 01 01 06 19 03 00 12 02 01 01 ................ +| 3904: 06 18 03 00 12 02 01 01 06 17 03 00 12 02 01 01 ................ +| 3920: 06 16 03 00 12 02 01 01 06 15 03 00 12 02 01 01 ................ +| 3936: 06 14 03 00 12 02 01 01 06 13 03 00 12 02 01 01 ................ +| 3952: 06 12 03 00 12 02 01 01 06 11 03 00 12 02 01 01 ................ +| 3968: 06 10 03 00 12 02 01 01 06 0f 03 00 12 02 01 01 ................ +| 3984: 06 0e 03 00 12 02 01 01 06 0d 03 00 12 02 01 01 ................ +| 4000: 06 0c 03 00 12 02 01 01 06 0b 03 00 12 02 01 01 ................ +| 4016: 06 0a 03 00 12 02 01 01 06 09 03 00 12 03 01 01 ................ +| 4032: 06 08 03 00 12 03 01 01 06 07 03 00 12 03 01 01 ................ +| 4048: 06 a2 03 00 12 01 01 01 06 05 03 00 12 01 01 01 ................ +| 4064: 06 04 03 00 12 01 01 01 06 03 03 00 12 06 01 01 ................ +| 4080: 06 02 03 00 12 06 01 01 06 01 03 00 12 06 01 01 ................ +| page 7 offset 24576 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| page 8 offset 28672 +| 0: 0d 00 00 00 03 0f d6 00 0f f4 0f e9 0f d6 00 00 ................ +| 4048: 00 00 00 00 00 00 11 03 02 2b 69 6e 74 65 67 72 .........+integr +| 4064: 69 74 79 2d 63 68 65 63 6b 09 02 02 1b 72 65 62 ity-check....reb +| 4080: 75 69 6c 64 0a 01 02 1d 6f 70 74 69 6d 69 7a 65 uild....optimize +| end crash-1ee8bd451dd1ad.db +}]} {} + +do_catchsql_test 46.1 { + SELECT snippet(t1,'[','', '--',-1,10) FROM t1('*'); +} {/*malformed database schema*/} + +#-------------------------------------------------------------------------- +reset_db +do_test 47.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 40960 pagesize 4096 filename 4b6fc659283f2735616c.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 0a .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 0d 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 96: 00 00 00 00 0d 00 00 00 0d 0b 6e 00 0f a3 0f 4c ..........n....L +| 112: 0e e1 0e 81 0e 24 0d cc 0d 72 0d 1b 0c b0 0c 50 .....$...r.....P +| 128: 0b f8 0b b3 0b 6e 00 00 00 00 00 00 00 00 00 00 .....n.......... +| 2912: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 43 0d ..............C. +| 2928: 06 17 11 11 08 75 74 61 62 6c 65 74 34 74 34 43 .....utablet4t4C +| 2944: 52 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 REATE VIRTUAL TA +| 2960: 42 4c 45 20 74 34 20 55 53 49 4e 47 20 66 74 73 BLE t4 USING fts +| 2976: 35 76 6f 63 61 62 28 27 74 32 27 2c 20 27 72 6f 5vocab('t2', 'ro +| 2992: 77 27 29 43 0c 06 17 11 11 08 75 74 61 62 6c 65 w')C......utable +| 3008: 74 33 74 33 43 52 45 41 54 45 20 56 49 52 54 55 t3t3CREATE VIRTU +| 3024: 41 4c 20 54 41 42 4c 45 20 74 33 20 55 53 49 4e AL TABLE t3 USIN +| 3040: 47 20 66 74 73 35 76 6f 63 61 62 28 27 74 31 27 G fts5vocab('t1' +| 3056: 2c 20 27 72 6f 77 27 29 56 0b 06 17 1f 1f 01 7d , 'row')V....... +| 3072: 74 61 62 6c 65 7d 42 5f 63 6f 6e 66 69 67 74 32 table.B_configt2 +| 3088: 5f 63 6f 6e 66 69 67 0a 43 52 45 41 54 45 20 54 _config.CREATE T +| 3104: 41 42 4c 45 20 27 74 32 5f 63 6f 6e 66 69 67 27 ABLE 't2_config' +| 3120: 28 6b 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 (k PRIMARY KEY, +| 3136: 76 29 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 v) WITHOUT ROWID +| 3152: 5e 0a 07 17 21 21 01 81 07 74 61 62 6c 65 74 32 ^...!!...tablet2 +| 3168: 5f 63 6f 6e 74 65 6e 74 74 32 5f 63 6f 6e 74 65 _contentt2_conte +| 3184: 6e 74 09 43 52 45 41 54 45 20 54 41 42 4c 45 20 nt.CREATE TABLE +| 3200: 27 74 32 5f 63 6f 6e 74 65 6e 74 27 28 69 64 20 't2_content'(id +| 3216: 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 INTEGER PRIMARY +| 3232: 4b 45 59 2c 20 63 30 2c 20 63 31 2c 20 63 32 29 KEY, c0, c1, c2) +| 3248: 69 09 07 17 19 19 01 81 2d 74 61 62 6c 65 74 32 i.......-tablet2 +| 3264: 5f 69 64 78 74 32 5f 69 64 78 08 43 52 45 41 54 _idxt2_idx.CREAT +| 3280: 45 20 54 41 42 4c 45 20 27 74 32 5f 69 64 78 27 E TABLE 't2_idx' +| 3296: 28 73 65 67 69 64 2c 20 74 65 72 6d 2c 20 70 67 (segid, term, pg +| 3312: 6e 6f 2c 20 50 52 49 4d 41 52 59 20 4b 45 59 28 no, PRIMARY KEY( +| 3328: 73 65 67 69 64 2c 20 74 65 72 6d 29 29 20 57 49 segid, term)) WI +| 3344: 54 48 4f 55 54 20 52 4f 57 49 44 55 08 07 17 1b THOUT ROWIDU.... +| 3360: 1b 01 81 01 74 61 62 6c 65 74 32 5f 64 61 74 61 ....tablet2_data +| 3376: 74 32 5f 64 61 74 61 07 43 52 45 41 54 45 20 54 t2_data.CREATE T +| 3392: 41 42 4c 45 20 27 74 32 5f 64 61 74 61 27 28 69 ABLE 't2_data'(i +| 3408: 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 d INTEGER PRIMAR +| 3424: 59 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 4c 4f Y KEY, block BLO +| 3440: 42 29 58 07 07 17 11 11 08 81 1d 74 61 62 6c 65 B)X........table +| 3456: 74 32 74 32 43 52 45 41 54 45 20 56 49 52 54 55 t2t2CREATE VIRTU +| 3472: 41 4c 20 54 41 42 4c 45 20 74 32 20 55 53 49 4e AL TABLE t2 USIN +| 3488: 47 20 66 74 73 35 28 27 61 27 2c 5b 62 5d 2c 22 G fts5('a',[b],. +| 3504: 63 22 2c 64 65 74 61 69 6c 3d 6e 6f 6e 65 2c 63 c.,detail=none,c +| 3520: 6f 6c 75 6d 6e 73 69 7a 65 3d 30 29 56 06 06 17 olumnsize=0)V... +| 3536: 1f 1f 01 7d 74 61 62 6c 65 74 31 5f 63 6f 6e 66 ....tablet1_conf +| 3552: 69 67 74 31 5f 63 6f 6e 66 69 67 06 43 52 45 41 igt1_config.CREA +| 3568: 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 6f 6e TE TABLE 't1_con +| 3584: 66 69 67 27 28 6b 20 50 52 49 4d 41 52 59 20 4b fig'(k PRIMARY K +| 3600: 45 59 2c 20 76 29 20 57 49 54 48 4f 55 54 20 52 EY, v) WITHOUT R +| 3616: 4f 57 49 44 5b 05 07 17 21 21 01 81 01 74 61 62 OWID[...!!...tab +| 3632: 6c 65 74 31 5f 64 6f 63 73 69 7a 65 74 31 5f 64 let1_docsizet1_d +| 3648: 6f 63 73 69 7a 65 05 43 52 45 41 54 45 20 54 41 ocsize.CREATE TA +| 3664: 42 4c 45 20 27 74 31 5f 64 6f 63 73 69 7a 65 27 BLE 't1_docsize' +| 3680: 28 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d (id INTEGER PRIM +| 3696: 41 52 59 20 4b 45 59 2c 20 73 7a 20 42 4c 4f 42 ARY KEY, sz BLOB +| 3712: 29 5e 04 07 17 21 21 01 81 07 74 61 62 6c 65 74 )^...!!...tablet +| 3728: 31 5f 63 6f 6e 74 65 6f 74 74 31 5f 63 6f 6e 74 1_conteott1_cont +| 3744: 65 6e 74 04 43 52 45 41 54 45 20 54 41 42 4c 45 ent.CREATE TABLE +| 3760: 20 27 74 31 5f 63 6f 6e 74 65 6e 74 27 28 69 64 't1_content'(id +| 3776: 20 49 4e 54 45 47 46 52 20 50 52 49 4d 41 52 59 INTEGFR PRIMARY +| 3792: 20 4b 45 59 2c 20 63 30 2c 20 63 31 2c 20 63 32 KEY, c0, c1, c2 +| 3808: 29 69 03 07 17 19 19 01 81 2d 74 61 62 6c 65 74 )i.......-tablet +| 3824: 31 5f 69 64 78 74 31 5f 69 64 78 03 43 52 45 41 1_idxt1_idx.CREA +| 3840: 54 45 20 54 41 42 4c 45 20 27 74 31 5f 69 64 78 TE TABLE 't1_idx +| 3856: 27 28 73 65 67 69 64 2c 20 74 65 72 6d 2c 20 70 '(segid, term, p +| 3872: 67 6e 6f 2c 20 50 52 49 4d 41 52 59 20 4b 45 59 gno, PRIMARY KEY +| 3888: 28 73 65 67 69 64 2c 20 74 65 72 6d 29 29 20 57 (segid, term)) W +| 3904: 49 54 48 4f 55 54 20 52 4f 57 49 44 55 02 07 17 ITHOUT ROWIDU... +| 3920: 1b 1b 01 81 01 74 61 62 6c 65 74 31 5f 64 61 74 .....tablet1_dat +| 3936: 61 74 31 5f 64 61 74 61 02 43 52 45 41 54 45 20 at1_data.CREATE +| 3952: 54 41 42 4c 45 20 27 74 31 5f 64 61 74 61 27 28 TABLE 't1_data'( +| 3968: 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 id INTEGER PRIMA +| 3984: 52 59 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 4c RY KEY, block BL +| 4000: 4f 42 29 5b 01 07 17 11 11 08 81 23 74 61 62 6c OB)[.......#tabl +| 4016: 65 74 31 74 31 43 52 45 41 54 45 20 56 49 52 54 et1t1CREATE VIRT +| 4032: 55 41 4c 20 54 41 42 4c 45 20 74 31 20 55 53 49 UAL TABLE t1 USI +| 4048: 4e 47 20 66 74 73 35 28 61 2c 62 20 75 6e 69 6e NG fts5(a,b unin +| 4064: 64 65 78 65 64 2c 63 2c 74 6f 6b 65 6e 69 7a 65 dexed,c,tokenize +| 4080: 3d 22 70 6f 72 74 65 72 20 61 73 63 69 69 22 29 =.porter ascii.) +| page 2 offset 4096 +| 0: 0d 0f 68 00 05 0f 13 00 0f e6 0f 13 0f a8 0f 7c ..h............| +| 16: 0f 2a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 .*.............. +| 3856: 00 00 00 15 0a 03 00 30 00 00 00 00 01 03 03 00 .......0........ +| 3872: 03 01 01 01 02 01 01 03 01 01 37 8c 80 80 80 80 ..........7..... +| 3888: 01 03 00 74 00 00 00 2e 02 30 61 03 02 02 01 01 ...t.....0a..... +| 3904: 62 03 02 03 01 01 63 03 02 04 01 01 67 03 06 01 b.....c.....g... +| 3920: 02 02 01 01 68 03 06 01 02 03 01 01 69 03 06 01 ....h.......i... +| 3936: 02 04 04 06 06 06 08 08 0f ef 00 14 2a 00 00 00 ............*... +| 3952: 00 01 02 02 00 02 01 01 01 02 01 01 25 88 80 80 ............%... +| 3968: 80 80 01 03 00 50 00 00 00 1f 02 30 67 02 08 02 .....P.....0g... +| 3984: 01 02 02 01 01 68 02 08 03 01 02 03 01 01 69 02 .....h........i. +| 4000: 08 04 01 02 04 04 09 09 37 84 80 80 80 80 01 03 ........7....... +| 4016: 00 74 00 00 00 2e 02 30 61 01 02 02 01 01 62 01 .t.....0a.....b. +| 4032: 02 03 01 01 63 01 02 04 01 01 67 01 06 01 02 01 ....c.....g..... +| 4048: 01 01 68 01 06 01 02 03 01 01 69 01 06 01 02 04 ..h.......i..... +| 4064: 04 06 06 06 08 08 07 01 03 00 14 03 09 00 09 00 ................ +| 4080: 00 00 11 24 00 00 00 00 01 01 01 00 01 01 01 01 ...$............ +| page 3 offset 8192 +| 0: 0a 00 00 00 03 0f ec 00 0f fa 0f f3 0f ec 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 00 06 04 01 0c ................ +| 4080: 01 03 02 06 04 01 0c 01 02 02 05 04 09 0c 01 02 ................ +| page 4 offset 12288 +| 0: 0d 00 00 00 03 0f be 00 0f ea 0f d4 0f be 00 00 ................ +| 4016: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 14 03 ................ +| 4032: 05 00 17 17 17 61 20 62 20 63 67 20 68 20 69 67 .....a b cg h ig +| 4048: 20 68 20 69 14 02 05 00 17 17 17 67 20 68 20 69 h i.......g h i +| 4064: 61 20 62 20 63 67 20 68 20 69 14 01 05 00 17 17 a b cg h i...... +| 4080: 17 61 20 62 20 63 64 20 65 20 66 67 20 68 20 69 .a b cd e fg h i +| page 5 offset 16384 +| 0: 0d 00 00 00 03 0f e8 00 0f f8 0f f0 0f e8 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 06 03 03 00 12 03 00 03 ................ +| 4080: 06 02 03 00 12 03 00 03 06 01 03 00 12 03 00 03 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| page 7 offset 24576 +| 0: 0d 00 00 00 03 0f 9e 00 0f e6 0f ef 0f 9e 00 00 ................ +| 3984: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 41 84 ..............A. +| 4000: 80 80 80 80 01 04 00 81 06 00 00 00 34 02 30 61 ............4.0a +| 4016: 01 01 01 01 01 62 01 01 01 01 01 63 01 01 01 01 .....b.....c.... +| 4032: 01 64 01 01 01 65 01 01 01 66 01 01 01 67 01 01 .d...e...f...g.. +| 4048: 01 01 01 68 01 01 01 01 01 69 01 01 01 04 06 06 ...h.....i...... +| 4064: 06 04 04 04 06 06 07 01 03 00 14 03 09 09 09 0f ................ +| 4080: 0a 03 00 24 00 00 00 00 01 01 01 00 01 01 01 01 ...$............ +| page 8 offset 28672 +| 0: 0a 00 00 00 01 0f fa 00 0f fa 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 05 04 09 0c 01 02 ................ +| page 9 offset 32768 +| 0: 0d 00 00 00 03 0f be 00 0f ea 0f d4 0f be 00 00 ................ +| 4016: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 14 03 ................ +| 4032: 05 00 17 17 17 61 20 62 20 63 67 20 68 20 69 67 .....a b cg h ig +| 4048: 20 68 20 69 14 02 05 00 17 17 17 67 20 68 20 69 h i.......g h i +| 4064: 61 20 62 20 63 67 20 68 20 69 14 01 05 00 17 17 a b cg h i...... +| 4080: 17 61 20 62 20 63 64 20 65 20 66 67 20 68 20 69 .a b cd e fg h i +| page 10 offset 36864 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| end 4b6fc659283f2735616c.db +}]} {} + +do_catchsql_test 47.1 { + INSERT INTO t1(t1) VALUES('integrity-check'); +} {/*malformed database schema*/} + +do_catchsql_test 47.2 { + SELECT count(*) FROM ( + SELECT snippet(t1, -1, '.', '..', '[', 50), + highlight(t1, 2, '[', ']') FROM t1('g h') + WHERE rank MATCH 'bm25(1.0, 1.0)' ORDER BY rank + ) +} {/*malformed database schema*/} + +#-------------------------------------------------------------------------- +reset_db +do_test 48.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 32768 pagesize 4096 filename crash-44a8305b4bd86f.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 08 .....@ ........ +| 32: 00 00 00 02 00 00 00 01 00 00 00 09 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 96: 00 00 00 00 0d 0f c7 00 07 0d 92 00 0f 8d 0f 36 ...............6 +| 112: 0e cb 0e 6b 0e 0e 0d b6 0d 92 0d 92 00 00 00 00 ...k............ +| 3472: 00 00 22 08 06 17 11 11 01 31 74 61 62 6c 65 74 .........1tablet +| 3488: 32 74 32 08 43 52 45 41 54 45 20 54 41 42 4c 45 2t2.CREATE TABLE +| 3504: 20 74 32 28 78 29 56 07 06 17 1f 1f 01 7d 74 61 t2(x)V.......ta +| 3520: 62 6c 65 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 blet1_configt1_c +| 3536: 6f 6e 66 69 67 07 43 52 45 41 54 45 20 54 41 42 onfig.CREATE TAB +| 3552: 4c 45 20 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b LE 't1_config'(k +| 3568: 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 PRIMARY KEY, v) +| 3584: 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 5b 06 WITHOUT ROWID[. +| 3600: 07 17 21 21 01 81 01 74 61 62 6c 65 74 31 5f 64 ..!!...tablet1_d +| 3616: 6f 63 73 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 ocsizet1_docsize +| 3632: 06 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 .CREATE TABLE 't +| 3648: 31 5f 64 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 1_docsize'(id IN +| 3664: 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE +| 3680: 59 2c 20 73 7a 20 42 4c 4f 42 29 5e 05 07 17 21 Y, sz BLOB)^...! +| 3696: 21 01 81 07 74 61 62 6c 65 74 31 5f 63 6f 6e 74 !...tablet1_cont +| 3712: 65 6e 74 74 31 5f 63 6f 6e 74 65 6e 74 05 43 52 entt1_content.CR +| 3728: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 EATE TABLE 't1_c +| 3744: 6f 6e 74 65 6e 74 27 28 69 64 20 49 4e 54 45 47 ontent'(id INTEG +| 3760: 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 ER PRIMARY KEY, +| 3776: 63 30 2c 20 63 31 2c 20 63 32 29 69 04 07 17 19 c0, c1, c2)i.... +| 3792: 19 01 81 2d 74 61 62 6c 65 74 31 5f 69 64 78 74 ...-tablet1_idxt +| 3808: 31 5f 69 64 78 04 43 52 45 41 54 45 20 54 41 42 1_idx.CREATE TAB +| 3824: 4c 45 20 27 74 31 5f 69 64 78 27 28 73 65 67 69 LE 't1_idx'(segi +| 3840: 64 2c 20 74 65 72 6d 2c 20 70 67 6e 6f 2c 20 50 d, term, pgno, P +| 3856: 52 49 4d 41 52 59 20 4b 45 59 28 73 65 67 69 64 RIMARY KEY(segid +| 3872: 2c 20 74 65 72 6d 29 29 20 57 49 54 48 4f 55 54 , term)) WITHOUT +| 3888: 20 52 4f 57 49 44 55 03 07 17 1b 1b 01 81 01 74 ROWIDU........t +| 3904: 61 62 6c 65 74 31 5f 64 61 74 61 74 31 5f 64 61 ablet1_datat1_da +| 3920: 74 61 03 43 52 45 41 54 45 20 54 41 42 4c 45 20 ta.CREATE TABLE +| 3936: 27 74 31 5f 64 61 74 61 27 28 69 64 20 49 4e 54 't1_data'(id INT +| 3952: 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 EGER PRIMARY KEY +| 3968: 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 29 38 02 06 , block BLOB)8.. +| 3984: 17 11 11 08 5f 74 61 62 6c 65 74 31 74 31 43 52 ...._tablet1t1CR +| 4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 35 LE t1 USING fts5 +| 4032: 28 61 2c 62 2c 63 29 00 00 00 39 00 00 00 00 00 (a,b,c)...9..... +| page 3 offset 8192 +| 0: 0d 00 00 00 03 0c 94 00 0f e6 0f ef 0c 94 00 00 ................ +| 3216: 00 00 00 00 86 4a 84 80 80 80 80 01 04 00 8d 18 .....J.......... +| 3232: 00 00 03 2b 02 30 30 01 02 06 01 02 06 01 02 06 ...+.00......... +| 3248: 1f 02 03 01 02 03 01 02 03 01 08 32 30 31 36 30 ...........20160 +| 3264: 36 30 39 01 02 07 01 02 07 01 02 07 01 01 34 01 609...........4. +| 3280: 02 05 01 02 05 01 02 05 01 01 35 01 02 04 01 02 ..........5..... +| 3296: 04 01 02 04 02 07 30 30 30 30 30 30 30 1c 02 04 ......0000000... +| 3312: 01 02 04 01 02 04 01 06 62 69 6e 62 72 79 03 06 ........binbry.. +| 3328: 01 02 02 03 06 01 02 02 03 06 01 02 01 03 16 01 ................ +| 3344: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3360: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 ................ +| 3376: 03 04 71 02 02 03 06 01 02 02 01 08 63 6f 6d 70 ..q.........comp +| 3392: 69 6c 65 72 01 02 02 01 02 02 01 02 02 01 06 64 iler...........d +| 3408: 62 73 74 61 74 07 02 03 01 02 03 01 02 03 02 04 bstat........... +| 3424: 65 62 75 67 04 02 02 01 02 02 01 02 02 01 06 65 ebug...........e +| 3440: 6e 61 62 6c 65 07 02 02 01 02 02 01 02 02 01 02 nable........... +| 3456: 02 01 02 02 01 02 02 01 02 02 01 02 02 01 03 02 ................ +| 3472: 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 ................ +| 3488: 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 ................ +| 3504: 02 01 02 02 02 08 78 74 65 6e 73 69 6f 6e 1f 02 ......xtension.. +| 3520: 04 01 02 04 01 02 04 01 04 66 74 73 34 0a 02 03 .........fts4... +| 3536: 01 02 03 01 02 03 04 01 35 0d 02 03 01 02 03 01 ........5....... +| 3552: 02 03 01 03 67 63 63 01 02 03 01 02 03 01 02 03 ....gcc......... +| 3568: 02 06 65 6f 70 6f 6c 79 10 02 03 01 02 03 01 02 ..eopoly........ +| 3584: 03 01 05 6a 73 6f 6e 31 13 02 03 01 02 03 01 02 ...json1........ +| 3600: 03 01 04 6c 6f 61 64 1f 02 03 01 02 03 01 02 03 ...load......... +| 3616: 01 03 6d 61 78 1c 02 02 01 02 02 01 02 02 02 05 ..max........... +| 3632: 65 6d 6f 72 79 1c 02 03 01 02 03 01 02 03 04 04 emory........... +| 3648: 73 79 73 35 16 02 03 01 02 03 01 02 03 01 06 6e sys5...........n +| 3664: 6f 63 61 73 65 02 06 01 02 02 03 06 01 02 02 03 ocase........... +| 3680: 06 01 02 02 03 06 01 02 02 03 06 01 02 02 13 06 ................ +| 3696: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3712: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3728: 02 01 04 6f 6d 69 74 1f 02 02 01 02 02 01 02 02 ...omit......... +| 3744: 01 05 72 74 72 65 65 19 02 03 01 02 03 01 02 03 ..rtree......... +| 3760: 04 02 69 6d 01 06 01 02 02 03 06 01 02 02 03 06 ..im............ +| 3776: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3792: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3808: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 ................ +| 3824: 01 0a 74 68 72 65 61 64 73 61 66 65 22 02 02 01 ..threadsafe.... +| 3840: 02 02 01 02 02 01 04 76 74 61 62 07 02 04 01 02 .......vtab..... +| 3856: 04 01 02 04 01 01 78 01 06 01 01 02 01 06 01 01 ......x......... +| 3872: 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 ................ +| 3888: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 3904: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 3920: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 ................ +| 3936: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 ................ +| 3952: 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 ................ +| 3968: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 3984: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 4000: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 ................ +| 4016: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 ................ +| 4032: 02 01 06 01 01 02 01 06 01 01 02 04 15 13 0c 0c ................ +| 4048: 12 44 13 11 0f 47 13 0f 0c 0e 11 10 0f 0e 10 0f .D...G.......... +| 4064: 44 0f 10 40 15 0f 07 01 03 00 14 24 5a 24 24 0f D..@.......$Z$$. +| 4080: 0a 03 00 24 00 00 00 00 01 01 01 00 01 01 01 01 ...$............ +| page 4 offset 12288 +| 0: 0a 00 00 00 01 0f fa 00 0f fa 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 05 04 09 0b 01 02 ................ +| page 5 offset 16384 +| 0: 0d 00 00 00 24 0c 0a 00 0f d8 0f af 0f 86 0f 74 ....$..........t +| 16: 0f 61 0f 4e 0f 2f 0f 0f 0e ef 0e d7 0e be 0e a5 .a.N./.......... +| 32: 0e 8d 0e 74 0e 5b 0e 40 0e 24 0e 08 0d ef 0d d5 ...t.[.@.$...... +| 48: 0d bb 0d a0 0d 84 0d 68 0d 4f 0d 35 0d 1b 0c fb .......h.O.5.... +| 64: 0c da 0c b9 0c 99 0c 78 0c 57 0c 3e 0c 24 0c 0a .......x.W.>.$.. +| 3072: 00 00 00 00 00 00 00 00 00 00 18 24 05 00 25 0f ...........$..%. +| 3088: 19 54 48 52 45 41 44 53 41 46 45 3d 30 58 42 49 .THREADSAFE=0XBI +| 3104: 4f 41 52 59 18 23 05 00 25 0f 19 54 48 52 45 41 OARY.#..%..THREA +| 3120: 44 53 41 46 45 3d 30 58 4e 4f 43 41 53 45 17 22 DSAFE=0XNOCASE.. +| 3136: 05 00 25 0f 17 54 48 52 45 41 44 53 41 46 45 3d ..%..THREADSAFE= +| 3152: 30 58 52 54 52 49 4d 1f 21 05 00 33 0f 19 4f 4d 0XRTRIM.!..3..OM +| 3168: 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 4f IT LOAD EXTENSIO +| 3184: 4e 58 42 49 4e 41 52 59 1f 20 05 00 33 0f 19 4f NXBINARY. ..3..O +| 3200: 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 MIT LOAD EXTENSI +| 3216: 4f 4e 58 4e 4f 43 41 53 45 1e 1f 05 00 33 0f 17 ONXNOCASE....3.. +| 3232: 4f 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 OMIT LOAD EXTENS +| 3248: 49 4f 4e 58 52 54 52 49 4d 1f 1e 05 00 33 0f 19 IONXRTRIM....3.. +| 3264: 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 30 MAX MEMORY=50000 +| 3280: 30 30 30 58 42 49 4e 41 52 59 1f 1d 05 00 33 0f 000XBINARY....3. +| 3296: 19 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 .MAX MEMORY=5000 +| 3312: 30 30 30 30 58 4e 4f 43 41 53 45 1e 1c 05 00 33 0000XNOCASE....3 +| 3328: 0f 17 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 ..MAX MEMORY=500 +| 3344: 30 30 30 30 30 58 52 54 52 49 4d 18 1b 05 00 25 00000XRTRIM....% +| 3360: 0f 19 45 4e 41 42 4c 45 20 52 54 52 45 45 58 42 ..ENABLE RTREEXB +| 3376: 49 4e 41 52 59 18 1a 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3392: 4c 45 20 52 54 52 46 45 58 4e 4f 43 41 53 45 17 LE RTRFEXNOCASE. +| 3408: 19 05 00 25 0f 17 45 4e 41 42 4c 45 20 52 54 52 ...%..ENABLE RTR +| 3424: 45 45 58 52 54 52 49 4d 1a 18 05 00 29 0f 19 45 EEXRTRIM....)..E +| 3440: 4e 41 42 4c 45 20 4d 45 4d 53 59 53 35 58 42 49 NABLE MEMSYS5XBI +| 3456: 4e 41 52 59 1a 17 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3472: 45 20 4d 45 4d 53 59 53 35 58 4e 4f 43 41 53 45 E MEMSYS5XNOCASE +| 3488: 19 16 05 00 29 0f 17 45 4e 41 42 4c 45 20 4d 45 ....)..ENABLE ME +| 3504: 4d 53 59 53 35 58 52 54 52 49 4d 18 15 05 00 25 MSYS5XRTRIM....% +| 3520: 0f 19 45 4e 41 42 4c 45 20 4a 53 4f 4e 31 58 42 ..ENABLE JSON1XB +| 3536: 49 4e 41 52 59 18 14 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3552: 4c 45 20 4a 53 4f 4e 31 58 4e 4f 43 41 53 45 17 LE JSON1XNOCASE. +| 3568: 13 05 00 25 0f 17 45 4e 41 42 4c 45 20 4a 53 4f ...%..ENABLE JSO +| 3584: 4e 31 58 52 54 52 49 4d 1a 12 05 00 29 0f 19 45 N1XRTRIM....)..E +| 3600: 4e 41 42 4c 45 20 47 45 4f 50 4f 4c 59 57 42 49 NABLE GEOPOLYWBI +| 3616: 4e 41 52 59 1a 11 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3632: 45 20 47 45 4f 50 4f 4c 59 58 4e 4f 42 41 53 45 E GEOPOLYXNOBASE +| 3648: 19 10 05 00 29 0f 17 45 4e 41 42 4c 45 20 47 45 ....)..ENABLE GE +| 3664: 4f 50 4f 4c 59 58 52 54 52 49 4d 17 0f 05 00 23 OPOLYXRTRIM....# +| 3680: 0f 19 45 4e 41 42 4c 45 20 46 54 53 35 58 42 49 ..ENABLE FTS5XBI +| 3696: 4e 41 52 59 17 0e 05 00 23 0f 19 45 4e 41 42 4c NARY....#..ENABL +| 3712: 45 20 46 54 53 35 58 4e 4f 43 41 53 45 16 0d 05 E FTS5XNOCASE... +| 3728: 00 23 0f 17 45 4e 41 42 4c 45 20 46 54 53 35 58 .#..ENABLE FTS5X +| 3744: 52 54 52 49 4d 17 0c 05 00 23 0f 19 45 4e 41 42 RTRIM....#..ENAB +| 3760: 4c 45 20 46 54 53 34 58 42 49 4e 41 52 59 17 0b LE FTS4XBINARY.. +| 3776: 05 00 23 0f 19 45 4e 41 42 4c 45 20 46 54 53 34 ..#..ENABLE FTS4 +| 3792: 58 4e 4f 43 41 53 45 16 0a 05 00 23 0f 17 45 4e XNOCASE....#..EN +| 3808: 41 42 4c 45 20 46 54 53 34 58 52 54 52 49 4d 1e ABLE FTS4XRTRIM. +| 3824: 09 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3840: 54 41 54 20 56 54 41 42 58 42 49 4e 41 52 59 1e TAT VTABXBINARY. +| 3856: 08 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3872: 54 41 54 20 56 54 41 42 58 4e 4f 43 41 53 45 1d TAT VTABXNOCASE. +| 3888: 07 05 00 31 0f 17 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3904: 54 41 54 20 66 54 41 42 58 52 54 52 49 4d 11 06 TAT fTABXRTRIM.. +| 3920: 05 00 17 0f 19 44 45 42 55 47 58 42 49 4e 41 52 .....DEBUGXBINAR +| 3936: 59 11 05 05 00 17 0f 19 44 45 42 55 47 58 4e 4f Y.......DEBUGXNO +| 3952: 43 41 53 45 10 04 05 00 17 0f 17 44 45 42 55 47 CASE.......DEBUG +| 3968: 58 62 54 52 49 4d 27 03 05 00 43 0f 19 43 4f 4d XbTRIM'...C..COM +| 3984: 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e 30 20 PILER=gcc-5.4.0 +| 4000: 32 30 31 36 30 36 30 39 52 02 49 4e 41 52 59 27 20160609R.INARY' +| 4016: 02 05 00 43 0f 19 43 4f 4d 50 49 4c 45 52 3d 67 ...C..COMPILER=g +| 4032: 63 63 2d 35 2e 34 2e 30 20 32 30 31 36 30 36 30 cc-5.4.0 2016060 +| 4048: 39 58 4e 4f 43 41 53 45 26 01 05 00 43 0f 17 43 9XNOCASE&...C..C +| 4064: 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e OMPILER=gcc-5.4. +| 4080: 30 20 32 30 31 36 30 36 30 39 58 52 54 52 49 4d 0 20160609XRTRIM +| page 6 offset 20480 +| 0: 0d 00 00 00 24 0e e0 00 0f f8 0f f0 0f e8 0f e0 ....$........... +| 16: 0f d8 0f d0 0f c8 0f c0 0f b8 0f b0 0f a8 0f a0 ................ +| 32: 0f 98 0f 90 0f 88 0f 80 0f 78 0f 70 0f 68 0f 60 .........x.p.h.` +| 48: 0f 58 0f 50 0f 48 0f 40 0f 38 0f 30 0f 28 0f 20 .X.P.H.@.8.0.(. +| 64: 0f 18 0f 10 0f 08 0f 00 0e f8 0e f0 0e e8 0e e0 ................ +| 3808: 06 24 03 00 12 02 01 01 06 23 03 00 12 02 01 01 .$.......#...... +| 3824: 06 22 03 00 12 02 01 01 06 21 03 00 12 03 01 01 .........!...... +| 3840: 06 20 03 00 12 03 01 01 06 1f 03 00 12 03 01 01 . .............. +| 3856: 06 1e 03 00 12 03 01 01 06 1d 03 00 12 03 01 01 ................ +| 3872: 06 1c 03 00 12 03 01 01 06 1b 03 00 12 02 01 01 ................ +| 3888: 06 1a 03 00 12 02 01 01 06 19 03 00 12 02 01 01 ................ +| 3904: 06 18 03 00 12 02 01 01 06 17 03 00 12 02 01 01 ................ +| 3920: 06 16 03 00 12 02 01 01 06 15 03 00 12 02 01 01 ................ +| 3936: 06 14 03 00 12 02 01 01 06 13 03 00 12 02 01 01 ................ +| 3952: 06 12 03 00 12 02 01 01 06 11 03 00 12 02 01 01 ................ +| 3968: 06 10 03 00 12 02 01 01 06 0f 03 00 12 02 01 01 ................ +| 3984: 06 0e 03 00 12 02 01 01 06 0d 03 00 12 02 01 01 ................ +| 4000: 06 0c 03 00 12 02 01 01 06 0b 03 00 12 02 01 01 ................ +| 4016: 06 0a 03 00 12 02 01 01 06 09 03 00 12 03 01 01 ................ +| 4032: 06 08 03 00 12 03 01 01 06 07 03 00 12 03 01 01 ................ +| 4048: 06 06 03 00 12 01 01 01 06 05 03 00 12 01 01 01 ................ +| 4064: 06 04 03 00 12 01 01 01 06 03 03 00 12 06 01 01 ................ +| 4080: 06 02 03 00 12 06 01 01 06 01 03 00 12 06 01 01 ................ +| page 7 offset 24576 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| page 8 offset 28672 +| 0: 0d 00 00 00 03 0f d6 00 0f f4 0f e9 0f d6 00 00 ................ +| 4048: 00 00 00 00 00 00 11 03 02 2b 69 6e 74 65 67 72 .........+integr +| 4064: 69 74 79 2d 63 68 65 63 6b 09 02 02 1b 72 65 62 ity-check....reb +| 4080: 75 69 6c 64 0a 01 02 1d 6f 70 74 69 5d 69 71 a5 uild....opti]iq. +| end crash-44a8305b4bd86f.db +}]} {} + +do_catchsql_test 48.1 { + INSERT INTO t1(t1) VALUES('integrity-check'); +} {1 {fts5: corruption on page 1, segment 1, table "t1"}} + +#-------------------------------------------------------------------------- +reset_db +do_test 49.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 40960 pagesize 4096 filename crash-fd87385402ecf5.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 0a .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 0d 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 96: 00 00 00 00 0d 00 00 00 0d 0b 6e 00 0f a3 0f 4c ..........n....L +| 112: 0e e1 0e 81 0e 24 0d cc 0d 72 0d 1b 0c b0 0c 50 .....$...r.....P +| 128: 0b f8 0b b3 0b 6e 00 00 00 00 00 00 00 00 00 00 .....n.......... +| 2912: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 43 0d ..............C. +| 2928: 06 17 11 11 08 75 74 61 62 6c 65 74 34 74 34 43 .....utablet4t4C +| 2944: 52 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 REATE VIRTUAL TA +| 2960: 42 4c 45 20 74 34 20 55 53 49 4e 47 20 66 74 73 BLE t4 USING fts +| 2976: 35 76 6f 63 61 62 28 27 74 32 27 2c 20 27 72 6f 5vocab('t2', 'ro +| 2992: 77 27 29 43 0c 06 17 11 11 08 75 74 61 62 6c 65 w')C......utable +| 3008: 74 33 74 33 43 52 45 41 54 45 20 56 49 52 54 55 t3t3CREATE VIRTU +| 3024: 41 4c 20 54 41 42 4c 45 20 74 33 20 55 53 49 4e AL TABLE t3 USIN +| 3040: 47 20 66 74 73 35 76 6f 63 61 62 28 27 74 31 27 G fts5vocab('t1' +| 3056: 2c 20 27 72 6f 77 27 29 56 0b 06 17 1f 1f 01 7d , 'row')V....... +| 3072: 74 61 62 6c 65 74 32 5f 63 6f 6e 66 69 67 74 32 tablet2_configt2 +| 3088: 5f 63 6f 6e 66 69 67 0a 43 52 45 41 54 45 20 54 _config.CREATE T +| 3104: 41 42 4c 45 20 27 74 32 5f 63 6f 6e 66 69 67 27 ABLE 't2_config' +| 3120: 28 6b 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 (k PRIMARY KEY, +| 3136: 76 29 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 v) WITHOUT ROWID +| 3152: 5e 0a 07 17 21 21 01 81 07 74 61 62 6c 65 74 32 ^...!!...tablet2 +| 3168: 5f 63 6f 6e 74 65 6e 74 74 32 5f 63 6f 6e 74 65 _contentt2_conte +| 3184: 6e 74 09 43 52 45 41 54 45 20 54 41 42 4c 45 20 nt.CREATE TABLE +| 3200: 27 74 32 5f 63 6f 6e 74 65 6e 74 27 28 69 64 20 't2_content'(id +| 3216: 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 INTEGER PRIMARY +| 3232: 4b 45 59 2c 20 63 30 2c 20 63 31 2c 20 63 32 29 KEY, c0, c1, c2) +| 3248: 69 09 07 17 19 19 01 81 2d 74 61 62 6c 65 74 32 i.......-tablet2 +| 3264: 5f 69 64 78 74 32 5f 69 64 78 08 43 52 45 41 54 _idxt2_idx.CREAT +| 3280: 45 20 54 41 42 4c 45 20 27 74 32 5f 69 64 78 27 E TABLE 't2_idx' +| 3296: 28 73 65 67 69 64 2c 20 74 65 72 6d 2c 20 70 67 (segid, term, pg +| 3312: 6e 6f 2c 20 50 52 49 4d 41 52 59 20 4b 45 59 28 no, PRIMARY KEY( +| 3328: 73 65 67 69 64 2c 20 74 65 72 6d 29 29 20 57 49 segid, term)) WI +| 3344: 54 48 4f 55 54 20 52 4f 57 49 44 55 08 07 17 1b THOUT ROWIDU.... +| 3360: 1b 01 81 01 74 61 62 6c 65 74 32 5f 64 61 74 61 ....tablet2_data +| 3376: 74 32 5f 64 61 74 61 07 43 52 45 41 54 45 20 54 t2_data.CREATE T +| 3392: 41 42 4c 45 20 27 74 32 5f 64 61 74 61 27 28 69 ABLE 't2_data'(i +| 3408: 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 d INTEGER PRIMAR +| 3424: 59 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 4c 4f Y KEY, block BLO +| 3440: 42 29 58 07 07 17 11 11 08 81 1d 74 61 62 6c 65 B)X........table +| 3456: 74 32 74 32 43 52 45 41 54 45 20 56 49 52 54 55 t2t2CREATE VIRTU +| 3472: 41 4c 20 54 41 42 4c 45 20 74 32 20 55 53 49 4e AL TABLE t2 USIN +| 3488: 47 20 66 74 73 35 28 27 61 27 2c 5b 62 5d 2c 22 G fts5('a',[b],. +| 3504: 63 22 2c 64 65 74 61 69 6c 3d 6e 6f 6e 65 2c 63 c.,detail=none,c +| 3520: 6f 6c 75 6d 6e 73 69 7a 65 3d 30 29 56 06 06 17 olumnsize=0)V... +| 3536: 1f 1f 01 7d 74 61 62 6c 65 74 31 5f 63 6f 6e 66 ....tablet1_conf +| 3552: 69 67 74 31 5f 63 6f 6e 66 69 67 06 43 52 45 41 igt1_config.CREA +| 3568: 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 6f 6e TE TABLE 't1_con +| 3584: 66 69 67 27 28 6b 20 50 52 49 4d 41 52 59 20 4b fig'(k PRIMARY K +| 3600: 45 59 2c 20 76 29 20 57 49 54 48 4f 55 54 20 52 EY, v) WITHOUT R +| 3616: 4f 57 49 44 5b 05 07 17 21 21 01 81 01 74 61 62 OWID[...!!...tab +| 3632: 6c 65 74 31 5f 64 6f 63 73 69 7a 65 74 31 5f 64 let1_docsizet1_d +| 3648: 6f 63 73 69 7a 65 05 43 52 45 41 54 45 20 54 41 ocsize.CREATE TA +| 3664: 42 4c 45 20 27 74 31 5f 64 6f 63 73 69 7a 65 27 BLE 't1_docsize' +| 3680: 28 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d (id INTEGER PRIM +| 3696: 41 52 59 20 4b 45 59 2c 20 73 7a 20 42 4c 4f 42 ARY KEY, sz BLOB +| 3712: 29 5e 04 07 17 21 21 01 81 07 74 61 62 6c 65 74 )^...!!...tablet +| 3728: 31 5f 63 6f 6e 74 65 6e 74 74 31 5f 63 6f 6e 74 1_contentt1_cont +| 3744: 65 6e 74 04 43 52 45 41 54 45 20 54 41 42 4c 45 ent.CREATE TABLE +| 3760: 20 27 74 31 5f 63 6f 6e 74 65 6e 74 27 28 69 64 't1_content'(id +| 3776: 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 INTEGER PRIMARY +| 3792: 20 4b 45 59 2c 20 63 30 2c 20 63 31 2c 20 63 32 KEY, c0, c1, c2 +| 3808: 29 69 03 07 17 19 19 01 81 2d 74 61 62 6c 65 74 )i.......-tablet +| 3824: 31 5f 69 64 78 74 31 5f 69 64 78 03 43 52 45 41 1_idxt1_idx.CREA +| 3840: 54 45 20 54 41 42 4c 45 20 27 74 31 5f 69 64 78 TE TABLE 't1_idx +| 3856: 27 28 73 65 67 69 64 2c 20 74 65 72 6d 2c 20 70 '(segid, term, p +| 3872: 67 6e 6f 2c 20 50 52 49 4d 41 52 59 20 4b 45 59 gno, PRIMARY KEY +| 3888: 28 73 65 67 69 64 2c 20 74 65 72 6d 29 29 20 57 (segid, term)) W +| 3904: 49 54 48 4f 55 54 20 52 4f 57 49 44 55 02 07 17 ITHOUT ROWIDU... +| 3920: 1b 1b 01 81 01 74 61 62 6c 65 74 31 5f 64 61 74 .....tablet1_dat +| 3936: 61 74 31 5f 64 61 74 61 02 43 52 45 41 54 45 20 at1_data.CREATE +| 3952: 54 41 42 4c 45 20 27 74 31 5f 64 61 74 61 27 28 TABLE 't1_data'( +| 3968: 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 id INTEGER PRIMA +| 3984: 52 59 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 4c RY KEY, block BL +| 4000: 4f 42 29 5b 01 07 17 11 11 08 81 23 74 61 62 6c OB)[.......#tabl +| 4016: 65 74 31 74 31 43 52 45 41 54 45 20 56 49 52 54 et1t1CREATE VIRT +| 4032: 55 41 4c 20 54 41 42 4c 45 20 74 31 20 55 53 49 UAL TABLE t1 USI +| 4048: 4e 47 20 66 74 73 35 28 61 2c 62 20 75 6e 69 6e NG fts5(a,b unin +| 4064: 64 65 78 65 64 2c 63 2c 74 6f 6b 65 6e 69 7a 65 dexed,c,tokenize +| 4080: 3d 22 70 6f 72 74 65 72 20 61 73 63 69 69 22 29 =.porter ascii.) +| page 2 offset 4096 +| 0: 0d 0f 68 00 05 0f 13 00 0f e6 0f 13 0f a8 0f 7c ..h............| +| 16: 0f 2a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 .*.............. +| 3856: 00 00 00 15 0a 03 00 30 00 00 00 00 01 03 03 00 .......0........ +| 3872: 03 01 01 01 02 01 01 03 01 01 37 8c 80 80 80 80 ..........7..... +| 3888: 01 03 00 74 00 20 68 20 69 0d 00 00 00 03 0f e8 ...t. h i....... +| 3904: 00 0f f8 0f f0 0f e8 00 00 00 00 00 00 00 00 00 ................ +| page 5 offset 16384 +| 4064: 00 00 00 00 00 00 00 00 06 03 03 00 12 03 00 00 ................ +| 4080: 60 20 30 d6 20 30 00 30 60 10 30 01 20 30 00 30 ` 0. 0.0`.0. 0.0 +| page 6 offset 20480 +| 0: a0 00 00 00 10 ff 40 00 ff 00 00 00 00 00 00 00 ......@......... +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| page 7 offset 24576 +| 0: 0d 00 00 00 03 0f 9e 00 0f e6 0f ef 0f 9e 00 00 ................ +| 3984: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 41 84 ..............A. +| 4000: 80 80 80 80 01 04 00 81 06 00 00 00 34 02 30 61 ............4.0a +| 4016: 01 01 00 00 00 00 00 00 00 00 00 11 87 89 06 26 ...............& +| 4032: 01 64 01 01 01 65 01 01 01 66 01 01 01 67 01 01 .d...e...f...g.. +| 4048: 01 01 01 68 01 01 01 01 01 69 01 01 01 04 06 06 ...h.....i...... +| 4064: 06 04 44 00 06 06 07 01 03 00 14 03 09 09 09 0f ..D............. +| 4080: 0a 03 00 24 00 00 00 00 01 01 01 00 01 01 01 01 ...$............ +| page 8 offset 28672 +| 0: 0a 00 00 00 01 0f fa 00 0f fa 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 05 04 09 0c 01 02 ................ +| page 9 offset 32768 +| 0: 0d 00 00 00 9d 0f be 00 0f ea 0f d4 0f be 00 00 ................ +| 4016: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 14 03 ................ +| 4032: 05 00 17 17 17 61 20 62 20 63 67 20 68 20 69 67 .....a b cg h ig +| 4048: 20 68 20 69 14 02 05 00 17 17 17 67 20 68 20 69 h i.......g h i +| 4064: 61 20 62 20 63 67 20 68 20 69 14 01 05 00 17 17 a b cg h i...... +| 4080: 17 61 20 62 20 63 64 20 65 20 66 67 20 68 20 69 .a b cd e fg h i +| page 10 offset 36864 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| end crash-fd87385402ecf5.db +}]} {} + +do_catchsql_test 49.1 { + SELECT term FROM t4 WHERE term LIKE 'oase'; +} {1 {database disk image is malformed}} + +#-------------------------------------------------------------------------- +reset_db +do_test 50.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 40960 pagesize 4096 filename crash-695bce8a3e107c.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 0a .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 0d 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 96: 00 00 00 00 0d 00 00 00 0d 0b 6e 00 0f a3 0f 4c ..........n....L +| 112: 0e e1 0e 81 0e 24 0d cc 0d 72 0d 1b 0c b0 0c 50 .....$...r.....P +| 128: 0b f8 0b b3 0b 6e 00 00 00 00 00 00 00 00 00 00 .....n.......... +| 2912: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 43 0d ..............C. +| 2928: 06 17 11 11 08 75 74 61 62 6c 65 74 34 74 34 43 .....utablet4t4C +| 2944: 52 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 REATE VIRTUAL TA +| 2960: 42 4c 45 20 74 34 20 55 53 49 4e 47 20 66 74 73 BLE t4 USING fts +| 2976: 35 76 6f 63 61 62 28 27 74 32 27 2c 20 27 72 6f 5vocab('t2', 'ro +| 2992: 77 27 29 43 0c 06 17 11 11 08 75 74 61 62 6c 65 w')C......utable +| 3008: 74 33 74 33 43 52 45 41 54 45 20 56 49 52 54 55 t3t3CREATE VIRTU +| 3024: 41 4c 20 54 41 42 4c 45 20 74 33 20 55 53 49 4e AL TABLE t3 USIN +| 3040: 47 20 66 74 73 35 76 6f 63 61 62 28 27 74 31 27 G fts5vocab('t1' +| 3056: 2c 20 27 72 6f 77 27 29 56 0b 06 17 1f 1f 01 7d , 'row')V....... +| 3072: 74 61 62 6c 65 74 32 5f 63 6f 6e 66 69 67 74 32 tablet2_configt2 +| 3088: 5f 63 6f 6e 66 69 67 0a 43 52 45 41 54 45 20 54 _config.CREATE T +| 3104: 41 42 4c 45 20 27 74 32 5f 63 6f 6e 66 69 67 27 ABLE 't2_config' +| 3120: 28 6b 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 (k PRIMARY KEY, +| 3136: 76 29 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 v) WITHOUT ROWID +| 3152: 5e 0a 07 17 21 21 01 81 07 74 61 62 6c 65 74 32 ^...!!...tablet2 +| 3168: 5f 63 6f 6e 74 65 6e 74 74 32 5f 63 6f 6e 74 65 _contentt2_conte +| 3184: 6e 74 09 43 52 45 41 54 45 20 54 41 42 4c 45 20 nt.CREATE TABLE +| 3200: 27 74 32 5f 63 6f 6e 74 65 6e 74 27 28 69 64 20 't2_content'(id +| 3216: 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 INTEGER PRIMARY +| 3232: 4b 45 59 2c 20 63 30 2c 20 63 31 2c 20 63 32 29 KEY, c0, c1, c2) +| 3248: 69 09 07 17 19 19 01 81 2d 74 61 62 6c 65 74 32 i.......-tablet2 +| 3264: 5f 69 64 78 74 32 5f 69 64 78 08 43 52 45 41 54 _idxt2_idx.CREAT +| 3280: 45 20 54 41 42 4c 45 20 27 74 32 5f 69 64 78 27 E TABLE 't2_idx' +| 3296: 28 73 65 67 69 64 2c 20 74 65 72 6d 2c 20 70 67 (segid, term, pg +| 3312: 6e 6f 2c 20 50 52 49 4d 41 52 59 20 4b 45 59 28 no, PRIMARY KEY( +| 3328: 73 65 67 69 64 2c 20 74 65 72 6d 29 29 20 57 49 segid, term)) WI +| 3344: 54 48 4f 55 54 20 52 4f 57 49 44 55 08 07 17 1b THOUT ROWIDU.... +| 3360: 1b 01 81 01 74 61 62 6c 65 74 32 5f 64 61 74 61 ....tablet2_data +| 3376: 74 32 5f 64 61 74 61 07 43 52 45 41 54 45 20 54 t2_data.CREATE T +| 3392: 41 42 4c 45 20 27 74 32 5f 64 61 74 61 27 28 69 ABLE 't2_data'(i +| 3408: 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 d INTEGER PRIMAR +| 3424: 59 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 4c 4f Y KEY, block BLO +| 3440: 42 29 58 07 07 17 11 11 08 81 1d 74 61 62 6c 65 B)X........table +| 3456: 74 32 74 32 43 52 45 41 54 45 20 56 49 52 54 55 t2t2CREATE VIRTU +| 3472: 41 4c 20 54 41 42 4c 45 20 74 32 20 55 53 49 4e AL TABLE t2 USIN +| 3488: 47 20 66 74 73 35 28 27 61 27 2c 5b 62 5d 2c 22 G fts5('a',[b],. +| 3504: 63 22 2c 64 65 74 61 69 6c 3d 6e 6f 6e 65 2c 63 c.,detail=none,c +| 3520: 6f 6c 75 6d 6e 73 69 7a 65 3d 30 29 56 06 06 17 olumnsize=0)V... +| 3536: 1f 1f 01 7d 74 61 62 6c 65 74 31 5f 63 6f 6e 66 ....tablet1_conf +| 3552: 69 67 74 31 5f 63 6f 6e 66 69 67 06 43 52 45 41 igt1_config.CREA +| 3568: 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 6f 6e TE TABLE 't1_con +| 3584: 66 69 67 27 28 6b 20 50 52 49 4d 41 52 59 20 4b fig'(k PRIMARY K +| 3600: 45 59 2c 20 76 29 20 57 49 54 48 4f 55 54 20 52 EY, v) WITHOUT R +| 3616: 4f 57 49 44 5b 05 07 17 21 21 01 81 01 74 61 62 OWID[...!!...tab +| 3632: 6c 65 74 31 5f 64 6f 63 73 69 7a 65 74 31 5f 64 let1_docsizet1_d +| 3648: 6f 63 73 69 7a 65 05 43 52 45 41 54 45 20 54 41 ocsize.CREATE TA +| 3664: 42 4c 45 20 27 74 31 5f 64 6f 63 73 69 7a 65 27 BLE 't1_docsize' +| 3680: 28 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d (id INTEGER PRIM +| 3696: 41 52 59 20 4b 45 59 2c 20 73 7a 20 42 4c 4f 42 ARY KEY, sz BLOB +| 3712: 29 5e 04 07 17 21 21 01 81 07 74 61 62 6c 65 74 )^...!!...tablet +| 3728: 31 5f 63 6f 6e 74 65 6e 74 74 31 5f 63 6f 6e 74 1_contentt1_cont +| 3744: 65 6e 74 04 43 52 45 41 54 45 20 54 41 42 4c 45 ent.CREATE TABLE +| 3760: 20 27 74 31 5f 63 6f 6e 74 65 6e 74 27 28 69 64 't1_content'(id +| 3776: 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 INTEGER PRIMARY +| 3792: 20 4b 45 59 2c 20 63 30 2c 20 63 31 2c 20 63 32 KEY, c0, c1, c2 +| 3808: 29 69 03 07 17 19 19 01 81 2d 74 61 62 6c 65 74 )i.......-tablet +| 3824: 31 5f 69 64 78 74 31 5f 69 64 78 03 43 52 45 41 1_idxt1_idx.CREA +| 3840: 54 45 20 54 41 42 4c 45 20 27 74 31 5f 69 64 78 TE TABLE 't1_idx +| 3856: 27 28 73 65 67 69 64 2c 20 74 65 72 6d 2c 20 70 '(segid, term, p +| 3872: 67 6e 6f 2c 20 50 52 49 4d 41 52 59 20 4b 45 59 gno, PRIMARY KEY +| 3888: 28 73 65 67 69 64 2c 20 74 65 72 6d 29 29 20 57 (segid, term)) W +| 3904: 49 54 48 4f 55 54 20 52 4f 57 49 44 55 02 07 17 ITHOUT ROWIDU... +| 3920: 1b 1b 01 81 01 74 61 62 6c 65 74 31 5f 64 61 74 .....tablet1_dat +| 3936: 61 74 31 5f 64 61 74 61 02 43 52 45 41 54 45 20 at1_data.CREATE +| 3952: 54 41 42 4c 45 20 27 74 31 5f 64 61 74 61 27 28 TABLE 't1_data'( +| 3968: 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 id INTEGER PRIMA +| 3984: 52 59 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 4c RY KEY, block BL +| 4000: 4f 42 29 5b 01 07 17 11 11 08 81 23 74 61 62 6c OB)[.......#tabl +| 4016: 65 74 31 74 31 43 52 45 41 54 45 20 56 49 52 54 et1t1CREATE VIRT +| 4032: 55 41 4c 20 54 41 42 4c 45 20 74 31 20 55 53 49 UAL TABLE t1 USI +| 4048: 4e 47 20 66 74 73 35 28 61 2c 62 20 75 6e 69 6e NG fts5(a,b unin +| 4064: 64 65 78 65 64 2c 63 2c 74 6f 6b 65 6e 69 7a 65 dexed,c,tokenize +| 4080: 3d 22 70 6f 72 74 65 72 20 61 73 63 69 69 22 29 =.porter ascii.) +| page 2 offset 4096 +| 0: 0d 0f 68 00 05 0f 13 00 0f e6 0f 13 0f a8 0f 7c ..h............| +| 16: 0f 2a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 .*.............. +| 3856: 00 00 00 15 0a 03 00 30 00 00 00 00 01 03 03 00 .......0........ +| 3872: 03 01 01 01 02 01 01 03 01 01 37 8c 80 80 80 80 ..........7..... +| 3888: 01 03 00 74 00 20 68 20 69 0d 00 00 00 03 0f e8 ...t. h i....... +| 3904: 00 0f f8 0f f0 0f e8 00 00 00 00 00 00 00 00 00 ................ +| page 5 offset 16384 +| 4064: 00 00 00 00 00 00 00 00 06 03 03 00 12 03 00 00 ................ +| 4080: 60 20 30 d6 20 30 00 30 60 10 30 01 20 30 00 30 ` 0. 0.0`.0. 0.0 +| page 6 offset 20480 +| 0: a0 00 00 00 10 ff 40 00 ff 00 00 00 00 00 00 00 ......@......... +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| page 7 offset 24576 +| 0: 0d 00 00 00 03 0f 9e 00 0f e6 0f ef 0f 9e 00 00 ................ +| 3984: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 41 84 ..............A. +| 4000: 80 80 80 80 01 04 00 81 06 00 00 00 34 02 30 61 ............4.0a +| 4016: 01 01 00 00 00 00 00 00 00 00 00 11 87 89 06 26 ...............& +| 4032: 01 64 01 01 01 65 01 01 01 66 01 01 01 67 01 01 .d...e...f...g.. +| 4048: 01 01 01 68 01 01 01 01 01 69 01 01 01 04 06 06 ...h.....i...... +| 4064: 06 04 44 00 06 06 07 01 03 00 14 03 09 09 09 0f ..D............. +| 4080: 0a 03 00 24 00 00 00 00 01 01 01 00 01 01 01 01 ...$............ +| page 8 offset 28672 +| 0: 0a 00 00 00 01 0f fa 00 0f fa 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 05 04 09 0c 01 02 ................ +| page 9 offset 32768 +| 0: 0d 00 00 00 9d 0f be 00 0f ea 0f d4 0f be 00 00 ................ +| 4016: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 14 03 ................ +| 4032: 05 00 17 17 17 61 20 62 20 63 67 20 68 20 69 67 .....a b cg h ig +| 4048: 20 68 20 69 14 02 05 00 17 17 17 67 20 68 20 69 h i.......g h i +| 4064: 61 20 62 20 63 67 20 68 20 69 14 01 05 00 17 17 a b cg h i...... +| 4080: 17 61 20 62 20 63 64 20 65 20 66 67 20 68 20 69 .a b cd e fg h i +| page 10 offset 36864 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| end crash-695bce8a3e107c.db +}]} {} + +do_catchsql_test 50.1 { + SELECT term FROM t4 WHERE term LIKE '»as'; +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 51.0 { +BEGIN TRANSACTION; +PRAGMA writable_schema=ON; +CREATE VIRTUAL TABLE t1 USING fts5(a,b,c); +CREATE TABLE IF NOT EXISTS 't1_data'(id INTEGER PRIMARY KEY, block BLOB); +REPLACE INTO t1_data VALUES(1,X'2eb1182424'); +REPLACE INTO t1_data VALUES(10,X'000000000102080002010101020107'); +INSERT INTO t1_data VALUES(137438953473,X'0000032b0230300102060102060102061f0203010203010203010832303136303630390102070102070102070101340102050102050102050101350102040102040102040207303030303030301c023d010204010204010662696e6172790306010202030601020203060102020306010202030601020203060102020306010202030601020203060102020306010202030601020203060102020108636f6d70696c657201020201020201020201066462737461740702030102030102030204656275670402020102020102020107656e61626c6507020201020201020201020201020201020201020201020201020201020201020201020201020201020201020201020201020201020201020201020201020202087874656e73696f6e1f02040102040102040104667473340a02030102030102030401350d020301020301020301036763630102030102030102030206656f706f6c7910020301020301020301056a736f6e3113020301020301020301046c6f61641f020301020301020301036d61781c02020102020102020205656d6f72791c020301020301020304047379733516020301020301020301066e6f6361736502060102020306010202030601020213060102020306010202030601020203060102020306010202030601020203060102020306010202030601020201046f6d69741f0202010202010202010572747265651902030102030102030402696d010601020203060102020306010202030601020203060102020306010202030601020203060102020306010202030601020203060102020306010202010a7468726561647361666522020201020201020201047674616207020401020401020401017801060101020106010102010601010201060101020106010102010601010201060101020106010102010601010201060101020106010102010601010201060101020106010102010601010201060101020106010102010601010201060101020106010102ad060101020106010102010601010201060101020106010101010601010201060101020106010102010601010201060101020106010102010601010201060101020106010102010601010201060101020415130c0c124413110f47130efc0e11100f0e100f440f1040150f'); +INSERT INTO t1_data VALUES(274877906945,X'00000e96023030011a042319320d3b123d812b5a31120110446e581b66814a05010a4537814274010e8102815c810f3d0104846d01081581204401103741043c59416b44010a404655265301103f73811a11114213010a821235820f020135030484320201360104816a020162020484550302390301710a04824a020166030483690201670704837d0201690404822602016a0504825c02026b620504817502016f0904810d0303e79c88060482760201700a04826302017204048155020373c2be050481130201770204846202027962050482710202c2ba010482140203e58496070483330204e8b2b879010483710101310110545c0c814b0e3a6501082c815d5b011a2a0e2f0d765c3d686014061d0d0112810733112c2e82141101048313010e5c6f632e813e42010c811882370548010e19158146822f1f01104d364a708146135a010a237b0a55210201610904841703027678090481270201620304810002026374060484660202657a0704827602016601048351090483540301660704814b02016b03025f0304c582caba0204816602016c01025f0302cebc0904843e02016e0804821802016f0a04817503016f070483100201720304822c020484380201740404842e0102460201770104812f0204836c02027a6f040483110202cebc02048267040161020484650205d5bd62cebc0604845b0204f2a580880204842a0206f38184a179670502750204f696a3aa0a04814601013201063330390110812281378114600d010c03716c5e822d010e81226b542a814d010a72740f83000108813a1e0b010c5681046f812c010c07814a777328011664244219531b1a2f811e4a010c4d81557c7f1b0201300704810702013307048230010484050202367807048175020239710804832502016204026b0204814d020363306108048262020265650504817602026667070483150201690704832f0301360a04814d02016c08024702016d0304843e0303cfb2630204814002016e0804837503016203048416030370c2be0304821b02016f0604834b0201700504816b030175070210020273660604841c020676c2bac2b2640604830a02017704027d02017808048141010482700201790504811d0202c2ba0502470206ca8d73ecbab9010483340204f09e9ab504048367010133010c3e04814f82250114812e814b2e0411811305010c811337811e6e010c82085e2b0e5d010c61812054811c01148122451b0781050c813d010c17823762643e011e080c1720814a10364306143b0d33260112810f0c2a810c816b13010a8163810e470201370404811102056176c2aa36050481530202646e0904846202026a730404827402016b0a020c02036f616b0404830a0201750504820d02017605025e0201770a04820702027a73050482460202c2ba0604824a020483330203cba434040483200203cebc790304847e01013401124181442c1d091f81580108601d8336011081320a2b8125820001123b0b81158116811f070110078112817a817308010e6682410d810e2601122d0d6413378147351e01105081021d3525812d01128246510a622204054101105c1b620e81302b05020130020483480104822702013102027e020132030483270201350304844802023770030207020261710604823f0201640802570204830a020265770304831f020168070483210201690204825b02016f020481280704835402057037e18b8d0904810802017304048439030172020481440303c2bd6b0a02630201760202490804815e0201770304816e050483550201780704816902017a070483280207c2b2f093aabc780a0482240301b303024e0301bc0604837a0202cb800204834a0202cebc04048201040484410203d3ad770a04814f0202d5a508048371010135011630817e0f81040d2c041552010c813d3b7e8115010c40692182693a01121f810d810d0a32814701101d1d1f642281742e01068229240110811231810a387f4c01100f50810f8165810d0114811f26443152593c104a010e641e1a3357820e020132030481540201340204815402023778040207020163060481020204815b020164020483010201670a0481540304f0948f870904811a0201690704835502016d0604832203027b01022803017a0a025102016e020484260404816002026f690502680301720104834e02017208024f02021d020475e69c8e0504814c0202767602025f0302de870a04837b020178090483200104835003026a72090484400201790504837204022302017a0104836b0202c2aa0a0481070303bcc2b3090484370203c7866403027501013601087c158303011212814305813e7b0e090118141a1c49713a211e0c74630f010a59826d8113011203328166037781561a01101d7f1d2a1f822533010e820e070f7b40160110811f40292c813226010a2d20824a32010a81418158670201300304840a0201660404817d02016d0804826902016e080626817f0104820b02016f0404825a02017003023b020272750804840c0301760a04844d02017403025b020175080484200202766e060482360303c2ba6c030482220202c2aa0204810e0301b20204835703048421040484240301bc0802410202d2a1010482630204e1b18f3704048354010137010c08816337812f01101382211532424d39010881248123010e7724810267815f011081236029813e273301101b7b29812a5b813b01128150810324814b220b01060c8417010e165b6c81708117010a1782346f6c0201380804816803016b0604840102013908023f0201610204816c0201630302760201640304832604023e0204833902016502048203020266770804821a0201670a04830002026964080484500304f29e9eb70802250201700204811b0201710904832d0201730304826b05048403020174010481000205776a62c2b2050482630201790202260206c2b2eaaeb464050485020301b3080482480303bac7af0a0484550203c695650804822a0202ceb90a0481170202dbae05023f010138011819814a2703390a61090c6912011a21181304812523811b5f164e050114110f35128123423f810c010c817573817c03010e7182590c812b0401142503597e6e0e2f3a3759011252813a811a2b75091a010882162a31010e17450a81048279010858658208020231670a048205020361c2b906023b02016301048236020164050482520201650904833d0201670904811b0201690604825002026a7a0604837c02016e0204832002017002026d0302c789010481020201720504835e0201740604810002017502020a0702630302676306025f0303e4a0a70102640203786a75010484440201790104841402047ac2be72070481340207c2ba3766673576090482790301bd07048142010481600202c7b309027a0401740604823b0202d2bf0304830f0204e989a6300a02600204f4bd91b60702120101390106518369010e19254641823711010c258267288121010e817c810d2b17250110810a578133812f4c0110415681067b288121010e0881208119347101140b8131543c8100343d1101088203813e01100d742e3230820f3802013006048107020134080481440202356501021a02013808048147020162020482230201630304833c030162050483390206656cf093b5bd070484140202677303048502020769e3ad9669c2b90304847402016b0804836402016c0404841f02016d010481250904825202016e06025402016f0704842a0201700a04834a0201720a0483530201750304822e020676f097b18374030482140201780604833d0202c2b9010481550301be070483720204826f0202ca80060481630202d5a80504833f0101610114551047810e130a78660c011a364611206c0b13080705733d5501240f08070c090b0c20813d1471042e4351131e011204412d814f0913104201263036110d060b1f811a301b0f4e1a29092f181c012808071e221a2a81075b320503065a0f140c1a0a26011c07231d0e6f3715063b760c6b091501121111303e3a71566d6d010e0867814d816a0c01181e18240d41724d221b3f384b0201300204830b060483080302c2b30204837f0201310a026e020134050481560301730902690201360406827c0f0201370204825d06020a0201390904815b0304f3bfb2a70a04822c0201610404810e020262660604841a03017608025b02016309088112825f020164090482310201650a0484480302396f01025d02026774010482090302df9b06048321020168070661813303016f03048248020169030483610504814001048401010483460301300704824203016a0204824402016a0504813303021d03017209048412030277380804824502016c0104814c02036ec2ba0804835702016f0204811d030176020238020270360a04840d02017107048201030469ca99690602350201720304812d0104845e02017304022c02017409022b020175050484140302caaf0402410201760404831f020177010484180704845203026f6d04024b02017a030482660202c2aa0204810a0301b30a04817b0302bd76020483780302be6a0302440202c58207025e0202c69901027a0302ad77010483200206c993f099b183070484270203caa1660204841a0204f29788ac0804831d0204f4ba9f950504843c010162011c0e33810216341c2413042130780501184d373e53131f2f052907423e010c830e3781390e011c1320461f81041b811b041e15243d011e241b10816c310b130c3133033b0741011a11816d3139100c13140b395848011c580e411a06304306810a3138330d011a441707092c70140c1643813920010c73653581374f010c826c81210f0402013803024f07048172020161020481650204810c020162030483470301370404813602016307048379020483280201640304815501048176010481060201650104812a0104841f0201660504821f0201670302700201680804846403016b0a04831d0301700904845502016908025c02016a010483560904827602026b6306023402016c0404832602016d040484410204825702016e0504831603027831020482160302c2be0504827d02026f6a05048121020171030232020483220304845402023a0201720204845c0304846e03013607048224020174060484480201780704844303016f0604814f045807070a0707070707080709070709070808090a5c07080708080b07060a06080707070b0a0b0808070b0a0b0a55070b08080a0908080707060709070709070706080c060b07070c0a66070b08080609070607080c0909660b06070707080a0807070b0b0707080a0b07070d0607080c0908630707070b07070a070d060b0707090a07080b080a070809085f0707070c0706080706070809080f06080a5807070607060e070807080907070b070b060c0709090807690808070707070708070608070709070809070a0d0b07070809095b070707070707070c080d07070b06070707070c07080b0808811a0b08060706080a070a07080609070707080808071307070a0708070907060807090b06060707070b0707080708070707080c090a0a81080a0b07070b0f0b0706070707060b07070b07080808110b070707'); +INSERT INTO t1_data VALUES(274877906947,X'00080e7f073c23110a1a18392f66090524183704276d6703306a320404824e030164080483520305c2bd7ac2bc0604815a0201360704833202016106021f020482400201630304822a0708817e8204030173040483500201640404824803016d0804824a03017709023002016606048367020268680a04815802016902088339811804027f0302656e0704834e0303d5a5370604816702036a3366090484470303c2ba660904826e02016b0904837c02016c07021403026c610604835802016d0204816802016e0104831202016f0104822f020270720602060201710704822202017206048174020273690204824602017409020c020175090482140201760a0482720301660404824403016a090484290201790404845703025d0203c2bc33040484620301bd0304824c020484540202c78607022403019a010482380202ca87070484390202d39d030485050203e184940404831b0203e6a881060483480203e8b18c0a04816d0203ee8d850104814801016b0110467257393c81272c011a053e815d3b190517064524521f011c3823590a8115372004313b1f3216011a5a20780b102d0804426916112c011a182f810781082d12137026161501221a180516811611051c131207811515173501180320112581062e05621c1407011c2d0e0617811522062208065a21520114582841621e6c203f1e2001161647411a272533815b1c2602013009048309020232630104835a0301720104817f0201330604836f0302ddb5080482560202347a07048102020135020483460104827b02043678ca800a04835f0201370404814b0104846002016103048246010482220301700204833f0201620404824d060481150201650304824f02016606088110834c0201670604821d0303c2be66010481790201680404843b030176050482270201690a04830e02016e0904844202016f040481010301630304822f020270640204822f03016e0704845802027177090482710206736ec2b2796a0104832e0306dab1d485377004048304020174050481700201750a0212020378627604048164030173080483190201790704833d0204823a02017a0506820e67030178070484530202c2b2060481500104823f020483030301b3020484310301bc04027e0402caaf0a026a0301be040482590204842e0202540202c79f0804824d0202cfa30804815a0204f29a92970204823301016c01140f63351a0a653b650d22011c09117a3e1538123537046a15043101141310082f49052f772b0c011c11121781583c2a5010133228241301287f3e0a2b1244080503060a100f413b4f0d070e2a01103e4e1f04814e7b1601183d0404052877111f230f811d01123a100f053e5c076910011a031732102381243d1b1727507301180e5d273e810803812e0f192a02013301048271070481330204821d020134080263020135060481280201610104830a0201640604826d020165060483050201670204841c0504841c0304841b0201680a04845602016a0104811c01024f030481080204813102016b0204837008024502016d0404836c04068207780301670704842302016f0404821203016d040484490301720404837e0201700104821d03048407030165050483050201720a04811602017307023502017407020503016f080484240302c2b90504821b020175090484090201770a088119822503026d6905048300020178030482680604812a0201790104830c0204833a0303d9a06806022002017a060482600203c2aa33030481560301b904020a0301bd0504820f0202d0b90904817c0202d3820202200202daa9080482030203de966e02024b0202df9d080484350204f098b0a20604845e01016d01220304456608322258060a031d4c38340f090112310c070e4238626e6601124a318109030513812f0118240d561e533742188113101b01160b24444b224d44814d4806011c05774e483410330d23541b28090401141f29062581131e221b6d011e81053a037a03320b0e4c24360d2310011a0e321d3c141825111d54637a1c0114093d3c2e58571a35293a0201350104840a03017701048330020136060217020138030483370201610a0482650201620504815e0201630704827701048201020164080483690201660804846703016904048113020167070483080201680504837d05022c0302cdb10a04815f0201690104833a0404824302026a360a04823b02016b0a04813502016d0504831a0204833803021a02026e360404825e02016f080484140201720304844b0404816603056ff09d899b0304823f020275390204816e0301780a04824202017604088308812703027902027770050482040201790104827e040482750204812902017a060483030304c2bdc2b30104836f0203c2aa62040484040301b903021c0302bd6b090484300301be0704814d0202c99402025603049a65656b090484020202ca92090482060203d19a730504844a0203d49f690804836e0202dfa8020482710204f09180860704822901016e011a0c0b8104243647521f43231f36011a2e1b33432c3d0b414905054d17011010573a6c0a816c1801160e063582340a5239050b06011a4481063d1b67250f2044200839012044591d1857291214135814101a1b361d011225067e8147111a4a4301166b13362e17195f3812186f01141c465b032b290406373301182a152a2281300f8107054e3f02023274080481770305c2bacf8168020481450201340604832f0201350704842e02013605020e0201380404841d0201610404810d020483750201620304812b020484230301610804834503026c6a0304816d0201630102380305613577337405048359020165040482720201660904826202066736f094b0af0a0482250201680104811f02016b0304847202016c0404822403016f0904822c0302c2b301025002016d0504817b01023f02016e020483090802040303e7bda10804832d02036f6b740404811402017005048419020484220202716506026303026b760904830a020172080482430304706c73620504825f02017308048413020174080481070201760104827f0204836e020477e7b89a0104840e02017a030483700206c2aa35657065050482740301b30804842b04046cc2be78090481040301b903020d0301bc010484260904813f0203c7a5620302330203c99f36050481010301a30704815b0202ca8b090483250202cdbb0604820a0202cebc0102170401380304842b0207eca2a6f29c87950904824001016f01221d17052b58101241060e3a201f1021633a0114816919811c142443100801280426080e2620042a812c531a490e121707131710011273432e493347811a340112195f671f46721c325e0118380c052b812822478107600b0116021c21821b2019263433040126021b05351b2a286b05181f071b5628111a330a012014533e073d0c0e5469141d1e2734050901220318051b44412803632e0642370e0a3a2b020131070481770201320a04812f0202346e04022b020136030483590304f09a81b60404834702016105048210020162030205030167010268020163010481540604820202048300020264310902420201650804834b02016703048247030365c6b602048205030573f098b890030481450201690204832802016a0a04826703016c0104825e02016b0604815e03016c03048334030677c2aa74c2bc09023d02016c0304823903027777060484540303df866c0104815b02016d0204811f0303796f7704020302016e0204814d07024a02016f0902680201700604840a0104831a0204835a020172070484440201730a023703026b660604830e030278790304815b0201750904822402017704025b020178030482350307f4b2a3896a343407026b0201790902720302633409020402017a020484590302dea004025f0202c2b20204816b050481200202de900402160204ee85a5770204822c0101700114143a0d391a60812d4e09011a2f313104201c372c3a3411321b011a268140144226334145050d1c4d01164e081f20671f088107237901186b123c1f6d07261e2b732e210116511116342a3d32376e083001106882257a0a17141101163039192b0c05812d735f3b01262a3e0841030b17181411051e0a18530e272b6d01182f322b260e24581d5381050f02013104048353020132050482370301690204843e02013302020d0201340104841f0201350a048139020137050482770201380204833a02016105025a0504832f0201620304836d020163050484100304832003016c030481290201640304837e020482490304822b010482290201660a04827002046964dc960204833102016c0704814502016d010482000201700104817e02037176760904821f020473eb91a708048152020174090482770201750404831e01063a825d03017a0904826a020276730a0254020177080260030277630104815c020178070481220202020301720804841202017a0204834c0202c2aa040484010301ba080482580202c6a3020481320203cdbf690502790202ce90070483140301bc030481470205d1a371cebc060481590203d2976a0404830c0203dfba6e0604814b01017101163732393b8120422f054b0e010e030b211d815d1c01165641757c080d81311d090e0112816581542d2313054301224e07121706516606080e39102d231c4b39010a2d81402d5e011a132527428114080d6e1111721c011a814a1a341538251023100d1c4c011e22182622623712411e38162a182d3b01142b67611981470f1f1f250201310a04824e0202336207048217020238730204815a0303cf886d06025a02033962620404833803016f0a04814e020161060483140302726d050483450301790904810c02036376690504811c02016403024d020165010483280802550301650904827f0304ebb8b561070482340201670302670301660804810e020168040482340201690704844d0302616404020f02016a030481060301700704827802016b070481240104814b0302c2bd0504816102016c0604837f03017a0404837902036dc2b90804810002016e0904821602016f0304812c03016401024b02017103048233060483600303e5848e04023a020172040481050305f3978aa06c070481151708070b070a0d0707070607080c080909090706080707070707070806070707070a090b070708080909090981140708070708080b0a0b0b070b070907090707070707070807080c0c070609070b0807100706070e08080a81110f06070707070f07120a0c070707070b0707060607080709080b0b080709060708070808080a810f0707060707070b070707070a080b08070e08070b0b08070c080f070a09060807070a080909080a810b080b070706070b0b0708060b07070c07070707070a0a09090b0708070a07070b0a070c070a060b080907080807070d8123070707070a0706060f070707090b07070707070b07080907080a060f070608080706070c060707070c070a810f07070706070707070a070b0713070a070707090a070c070706080a07070807080808070b0909810607080808090707080709060a070a060707070707070b080707090707060b0807'); +INSERT INTO t1_data VALUES(274877906948,X'00000e8a0330717304048359030134050481100203756371070482190201760704817b0301770804821d0201770204844d0201780204836c0404826103017504026a0202c2aa020482420301b305022c040267390602570301b9080481540301bc0102290206c99cf6b5aa80080481430202cebc02026e02048120010172011a1c2f15158108048125463f251d010811811539011412423105812181171549011847284a30234e5b33042632120118351e8113817d0f2b220d111901264f104a211004061d0a2a0b35121a0a2118341f011c81160a1b030d2a0610243e445f0c011c6f0c1e3b1768141e322717500b140110537f810169811625011a492847203e210f532c16480627020135020481780302caae0104811a020261330904846c0201620804812d0201630404814a0201650704837503017301048276020168050213020169010484540604842b0302796f0504833302016a0304831b02016b0604701302016d0604815302016e070483630204815202027071020481520201710104835b02037266700704843002027362040481490201740904817d020175020481040304f59e9c9407048218020176060484060204776dc2aa020483070201780504812503016601048159020279790704840502027a730904826c030178030482740202c2aa04023d010483540301bd06026b0203cab877010483290202cdbf060482410202cebc04025c0401690204827f04016c0904840e0202cf880104835d0203dfbe6c01025b0204f0aeb7b2030481680205f1a7b5bb390504826a010173011a22810d12415003071f81181839011a3220221511546d810012052b57011a0c4274300d154e81111f041e10011e293f4213051b2276560817312811170120092136122418370e4e782b3912080f3201262b3b340f222b0c09142a0822116a135c1c130c0114320c4e385a0d0415075f01163543340f06362381133c0c012224180981742048191d110e180e180d310f011a20632450281f043027114b034e0203316a61030482160201360104826e0201380a0484570201610104844d0504814a0201620904820f020164010481630304810403027761040258020165080481550302c9b70204827a0204677367690102660201680104831b020169020483760301720a04814502016a04020c02016b04023405023c030269630504840502016c08048463020170030482670202716d080482000201720304835c03016507024d020174030482520504821503016505048207020175040208030137060481710201760a04833d0201780404832302027a6f080481200202c2ba0204812c0301bd0404821e0205c3a66865730702540202c7890804816f0205cebcc7af730504815c0202d2930202540202db89020481160203e8b8a00304825d0205f0958db331070481620305989b8569780a04813d0204f69299a5020210010174011a7b3829100a4e511f1a281c17140114812626032c372634234c01140a520e815a810815200501123e4f3531042d57615b011a041f3e64070f1f1913274a20770114811d0f5d743e0634161c01162c2782130c1b810520280d01164a513110480b402b810d13011e522d08042c1146137012201e810512011a290903182c05301e5d811944290201390a04836d0301610104836e02016208027803036cc2bc07048261020263640a0481480301730704813602016408022b020165050484310301720702260301780402500203666d73070484470201670104825b02016a040481590304836702016c0304835601025e02016d060484110301340204813702016f04021802017006068336280201710304813507025902017201026508022903016d0604812f0201740304827e03016a010482440201750404834b02017604021a010484150504836a02057773c2b2380602520201780204823e0302cebc080484040303d2956403048171020179020484240204813c0202c2b303048307020484410301be0204816f030484250203c798680104843d03019c030482570204e19ea86a020482350302a0950a0482280204e5a4bc780304810e0101750126090a35030a03220a1731630f31252f0c4b1e31011e39200e3715282a03103b56090f6b1501121d4916246e6d460d6501162609380406361e816d203f011a22166008124f58202e182025150114390f3a25713f0e3f715c011a5a11191123466025710c313312011e3c191326811c1444055f1f5109051201143b106f1181000d068155012043381381020d81080d0603171824260a0201300404844a02013207024203026b390204833803066eeebabb35660604842102013308020302023f02013503048243020436716b66040481440201380a04843d020261690704836002016203048209030484670201630a04841d020264790104822b0201650502340302c2bd060484300201670202620201680704810d020169010484430104843402026b67040481540306eea3ad77c2aa0a04836702016c020258040482270104830a02016d0404824102036e716a0604843e02016f0104832a04020c0204836803036530650a04817402017005025b0301630604843d020171070275020273720404827002017504048133010484120301370102270201760104814f03026203016d0904844802017701048375080481220301660202330301700a04827f020378c2b2030484710202796806023c0301730704813d02017a0404815f0104817202048407010483390202c2b204026802048254040266640204831e0301b3090482230302b963060482010401750a0483290306ba35f2999dac09020f0203ca926e090483350203cdb4780302350202cebc0a020f0204d7a7696d010483100206f097bc996d71040481480101760114185b2258291610821c0e01160272173107154f5b813722011a81020c200e1826250d39811f07011a7911152a2a45131504422c81070120050d3f5b23342e3e4139032a3813042d0116592d1c15630c0c0a814649011c1a362f5c4a35511f0804033e372b01102981262a352e8205010e0b4b6282388106011e26810a2d125f361a12170d1721311e0201300204832b0201320104811b050483790201350204832202016404024c0202657a090483710202666307022703016c07048362030277750604842f0201670304843d0104844c0203686f7a07027202016a0102430204847502046ce0a2b20704810302016d0204827b02026e330804826c02016f0104835502017102027606026c020172060483490202736505048371020174030484130302387602025d02017502048345020376346b0904825c0201770904814b0303c2bd720804812b020178040483600201790804816402017a08026e0202c2b90704811c0301bd0504821e0204cdbcc7a108021a0202cfb8080481490204f09f96a50204842101017701180207232d37812d0c045c4a0a011a06163b3408171c52213a26592201206d08581605811a171e0c0a1347104914011282181324082b73320f01122f6e811d2c3d410a44011e551414206a092f133f333d150a3e0f011e235b170e37060627471b13373b3e27011a0e1e816b270c10102d53381045011a060e1e254d044932651234691e011a158138300a04810c0a8121071802013003027707048214020131080481370201320a04835502013304048271020538ceb369650802310201390a0481440202616c0502570302c7a104026c0201620804811c02016301048168020241020164040481560104820b020165080484460102400202686c060232020269670604827302016a0108810c826e0704824e02026b6c0604816902016c0304831c02016d0304811903016407022b02016e01022e0604845a030237780702040301710804815102016f0104842903017a0704826902017003048445020482080202713002024e0201720804822003016202022302017303048111010482790204812d03067479c39f66700a0210020174020229020175090484430301690604820402017606020202017a01021b0104843a0202c2b90604842b0301bd030484570202c69b0504815d0202c8a30a02240202ceb8090484690301bc0404832f0202df85020481230101780116812b0a16810e4b045a3b2a01205b1305811134092f62072343100f0f05011e5734152612030b4c4134123009361601121781653207780a6a0d01164a25210824138107738139011481341f088158060c8133010e5920193a4c2331011a0510358101231a1b3609702732011a2f07631610033436810256174c011a1342040a58110721378139101602033067750802720201320604834303017109048244020133090481700203366f79010483520201390404810902016301048411050483420201640a048432020265310704832b02026774040481000302d5b2010259020369756c0504832902016c07048365030233700904824102016d010482670404834c0504830c02016e0104831604048120030169090481750303eba6990104835f02016f0604834c030379c2ba0a0481560201700404815e06048256020174040482370201760a04820d0201770604811c02017a0404812e06020b0202c2b9030481660301bd0a04816c0301be030483620206c99b6d7777750304835b0206ceb0646b66610402490202d8bf030482250206e8bfbc626964080482510204f09c9a9e0404830f010179010e2081335661371c01220e4e2718124f0d0649812b0b0a063b040b011402741d1235810805211a011409161d732b8106325f6a01182e330325068107703728302b011e3723081c0d0a3f810c183e061b067f0106834a12011a044030185a1e810704220a0541011245602b0e421441817801144b03811a1a29614e224b02013003048139020132050485050304f09caba6010482230201350104822708048413020138020483430201610402230201620902440108812181070301300304815e020263710404831002016403048226010483660604823c020165010484510201670104816407048418030334c2b20a0481120201690404814c02016a010481500904810f02036b75610304836402016d01027902016f0304817b03056f6373cebc0a0483010201700104817d020171050482680104843b0302383203048128040807090707070b0608060707060c0b810e07080807070707060b080707070b0807090807070a070a07070808070b06090807070708080a0b81230907070b070b0707080907070706090807070807060b07060707070808070a080b0708090b0b09810a0707060908070607060609070b0a070706080a09070707070e0a0708090b0c0b09070a080a811a0706080c09070a07080b0708060806070b080c0e07090e09060706080b060a070b0607090707130b080708070b0908070a0c810d070b0706080707080b080a0a070807090708070707090709070706080709080a81170a0707070a070707070a0b0a07080d080707060a070707070b0707060f0b060707060a08070807080708810d0807070709070b070808070907080f0b070907090b0707070a0807070c0b080c0a810107070a0b07060c07080f070b09070b0906070b070b'); +INSERT INTO t1_data VALUES(274877906949,X'00000e5c033079720404826c0404833002021b03026f6b020482100201740904842e010484150303c2b36f0204840e0201760704826b02017708048273020578f48ba5b50a0481400201790904845902017a050483280304c2bac2bd0404841e0203c2aa680904843a0301bc070481100301be0304820304016a050481630202ca8103027b0202cb860604840d0204ceb56e370204832e0302bc740202520202cf8d07027e0205d8ae39c2aa0104813c0204e887b3770404816a0204f1bfb0970204832f01017a0118101e282f07045961813a193e011e69162f0d2b051c060f084460063053011e06810c20330d0733815c220515220c011210290a7e07810c3a18011a1f2f064a19155212472781047c010a123e45825501166c6062182718167131092f0112331b812c0b6e81470b01184f3a230d45261e271c36111701140a8128456b291248391a0201300104840d0204812502013401048318020137070483070201380904823b03016704026402013907023403016e060481380201610204812f0404824504045243020162050481150104824003016a0804827a020163070484590204817903017a02048209020164020484200203653077030482610302737601048424020166070481730201670304841e0504840e03016b0504825302016901048235010484400404841002016a010483680404845f02046bc2b97407023302026c7a0704812d02027161070209030378cebc0a021f020172040483750404815f0201730304813c030131020482040201740704824d0302793901027502017604048423020177040620813103048313020179030484540204833c04022503017a0904826302017a050483530203c2b2660404840f0307ba6272f397bd92010483070301be020482760202c6b9050482410203c9a3650604812c0202cbae020482180202d38c0704812f0204f096adb7070483490204f6a69c8b030484390102c2aa010c815e81147969010c811c827f0417010e81077a4c03815f010a2e8100820c010a810d148140010c0a7481201a13010c0b831525323d010a8206358129010c21637a33812701083c8340390301300504820a0304326939770404841c03016107048334030165030484150302696c0704810103016c0704812a03027178090481620301730504830e0301750504846303017802021c0301790304836803017a0604834e040135030482650302cdbf010483330304ceb23169070484090303e4849a0504817d0201b201088219744601082210832c010629846001121c8137182211816232010a81668122690108817281080110816a433581102f3e010e7481001b3481190106814e1b010e3646823a810e070301310504840b030161010481720301660604822f0301680804821003016a0704844803016b0104845b03016c0504823403026f700802340301710804826004026d3106024a03017509068458070301770a04836c04016b01021f0301790a0482790302c2b20a02270401b3040482130303ceae6e010484330302df9e040482100201b3010881656e750106820b50010e81434a27048153010a812b068122010843810c0401048211010a50817d812e010c811a8163810801061e832c010c811f81418101030163030481210301640504831102048104030482440301680a048202040271300404841303066b6576cebc6a0104843603026f7a08026d0303756a7808023e030176040483610301770704814403027973020484360302c2b906020c0401bc010484490401bd0804835c0302d38c0304846f0304eaae96750a0484020201b9010c3b824018322d010e0468315a5c817901067d583401060b810e010865118169010c0e07826b813a01066f8265010c3b8239633852010e61161e7030821b01068241210301350804843d0301620302480301690402610403e7a1910704812103016d080269030673c2bac2b36c090483070301760504821d02048454030278350702620302c2ba010483320304ca897a750804813b0401b5070482350303cebc6d050483120302d199020483020402ad660604840c0302d2a1010482550302daa30202340201ba01026d01085119826a010c2f5e82008110010882028221010481090108821b812d01068128460208810b8264010c810971812d440301310304820e0301320504825e05048221030333756d010482720301340502700301640604813d03016607020c0301690204823a03016b0104812203016c0704842103016d0304835f03016e0a04813c03016f0304834d030173060482660401710604810f0301740104843c0303d48174040482690201bc010482690108813c83290114090f816045242e148111010a810616822701048456010e2c81167a638115010a3a3b83204001067a8367010c8114127a2265010a824f7f5c230301300604833e0302616f020483560301640104827d090481280301650502150301660804841a03016804048221030169030481120301720104827803017606023d04016e020481700302c2aa0904836c0401ba090484100201bd01088240224f010843813674020a81185068620106822a44010e14154a8101825e010c19814a1b826c010c81221f81651b010a815a4d812c01082d7281160301300804843c030231300304836703013509048353030365c2bc0304814d0302667504027a0301670604831503016c09025d03016d03048178030172020483620301730104816b0402c2b30404823203017401027204016a08048451030175090237040177090481690301770302190301780a04823d03027a350a0481260302c2aa060481620401b3090481090304f098a1a30a04825d0201be010a0d730b816f010c821d81236c2b010c6f8208358119010681236801087d4e831e0108823c8235010667794b010683165e010e05812e3d3c820d010c81148123817403043531c2ba0204824c03083875c2bceba7957a03020803016302020804036dcebc0304814e0301640304843704033379790504823203016b08023603016d070481090304825804013707027a03016f080484530301710a04842203017809025e0304c2aa6135010481460401b90a04836b0401bc0704814f0306c6ab66e3afb9080482660103c39f350304821f0201b0050481110301680602030201b8040481310604810403016c0a024803017108024b0201be06048323030482650301300504812d0304f48990ae060482740103c491680a0483700301760a0481290201a7010484090202b177020482380201b3030482280102c5800602090301360104826a02018202048173040483780301370104814102018b090482730201930504810f0102c680020483790404830c020183020228020185030484400704820b02028d7a0a04821c0201920404835e02019507048437010484070301760204836102019a020214080483100303d795680504820602019e030481050201a30404820a05048436030177070484290201a50704843b0201a8060482690304816f03027735020483630202ab79050484430201b6060481050201b904024003016d070481130201bd090484350201bf06048361030163020481580102c781040484560201830604833a020286650404842a0301750a04821a020189070483370303e0b9b30904836602018c0104816c04020602021d030171050484570201960a04842d03016b0204821903016c04021e03016e0204845002019a0a04844402019d0704835103013103021702039f77750704827e0201a1090483340201a301022b0404831d0304616363750502520302cfb2040483720201a50a0481530202ad760902310201af0104811d0908816281390301640404843c0201b3080483240201bb030483480201bd040484290304840a0203bf646a0404843f03017a040482450102c89d05020f0201a1090483550201a3060483550201ab0904813203017a04024a0201ad030482210201b10204824302025d0303656577030483600202b46e010482240201b6090484410204bce39f9d020484570102c9870604813202018b0804843603017a06027102018d080481130204840c02018f0104836309026202029164020484430201930504816d0201970304843402039c6c73070483700201a0070483470201a203048262030364d7960a0484010201a80304832a060484390202a96a080481170303c69532090481440201ac040214020481180202ae770604832c0201b0090482300201b40804845a0201b5070482110302716f010482160201ba0504837b0201bb02048149030165050483250201bc07088105833002067f824d0302d19f0402330201bd04024d03017a0904814f0102ca800a048321020181040481580201820604821e020383693707048417020287630704840002018a0904834c0104835c02018b06022f02018c0704814702018d0704810a0104831a020490e7b38308048423020191020482510201920304832f030135030483710201950304833207026802019902026b02019b0904832a02019d09024c02019f0802210201a4010484080202a5710a0481680203a777670804831e0301790902100201ae0402700104834e0201af040484650202b673080484190201b705048221010483520201b808048465030167010484400104cb81cab90104834802018602048317020689777568cf920804842602018b0404817b020191050484200202a0720304833e0201a3040625827f0201a4030485080102cdb1070881408205020483700201b30404840e0202b735080482640201b80504815a0201b90a0482370206bb31cebc6c730304842f030133070483010103ce80370304826d0201900202770201ae0504844e0201b1050484080201b20904815f0201b3040483430201b40702600201b50804823b0201b90304846e0302c9a1070483300201ba0404844d0104847e0201bb050482780201bc010882378223010e811105814d8134010a8142823f2d0106811c52010a6e1814816b02088168824b010a438137812d011019060c6b812f811c010a81314e811b03033366660404825503013502048263030238620904820703016303048120040f080b0907070b07070a0907070707080a07070b0a0a81060b0707070606070f0b070b07070908070b070f0b090807080b07070707070c0e0707090d07080908080a0a50070a0707080708070706070707080a094d07070707070707070707080706070707090844070f07080c0708070708070707080a4707060609060c0b07080a07090808080737070b09060706070707070707070707094807080b0607070707060708074107080709070706070707080607060706070808070a460a0d06090709060b060707060a07070c0907060b06060b070a090707080707070b0707070c060b08070b070a09070b07070b08080706070707070807080707090d070707060707070609070a090807070d0707070b09070707070706070a0908070a0807060b0a080707090707090b08090a08070707080707070e07060708070709080b06070b0a0707070a06070606070809060a07080b07070a070c07070808070e070807070c07090607070707060707080b0743090708'); +INSERT INTO t1_data VALUES(274877906950,X'00040e1807020e0830cebc66f09f97b60604840703016b06021003016c0804835103016d0a0484050301710602560301730304822d030275770904834e0301770204824d0302797707022e0302c2b2070482640402bc6d0a025e0303c3a6370602530305cebc7768690a04823f0201be0504816f0301780a0482780201bf0a04833a0305f09e919a790a0483440103cf80340604827e0201810404823e0201830304843a0504837f0301780704834c02018407048139020186030881038372030178010231020187010481100201880204835b0204845a0104825a0201890204825c0604831802018a0704841502028c340304845e02018e0602180301390204833c0203936e770104820f0201940704823702039765630304847b020199080484110301710704821402029b650704827102019d0304845802019f0504845c0303e7ba880502690202a163090484160201a3060483020301680102470201a90a0482480201ab0104837f030482570201af050482190201b20a0481020201b30504842903026f64040483650203b873780704826d0201bb0704831c030177020481410102d0b0070482560202b17a050484460201b2040483400201b40402390202b671020484220202b777090483510201ba030481750202bb6301021e0202bc620a0484080201bd0204815d0201be060482470201bf040483340102d1800904825002028470050482290202876f06022202048872c5820104826202028b680504845d02018c0a027302018d0a0205020197090483210202986b0a02780201a3030481580201a70604835e020483670201a901048379060483780201ad0a04816b0201af010482310201b1080482020201b3060484610202b5640804814c03026e690304847c0201bb080482710201bd020482020201bf06048435010482590107d28b37c2ba61640104831702018d0704810b020393c5890204836e0201950602070305f6a5989a74040484450201970104813702029f790a04843f0202a57407027f0201a90504835b0202ab750502490202b9760a04827d0301780304826e0202bb6f060481780203bd7130050483630103d3866f09023602028a6c0604825302018c0304845302028e650404833a02028f6f0104821302039d356d090484320201a5020481130404813f0201ab0704835b0201b90a0482380201bd0904833c0201bf060484450302307604027f0102d491050483300201930a024603036bd291070483060201970704826702079b72c2b3356f730904844f02019d010482610201a10404814d0202a36b0502250203a5c7b3080483780201ab02023d0103d5a1640102260201a206026a0301660804813c0201a5030481180201a7050483360201a9040483510201aa060482020201ab0504811e0201bb090481680202bd740702160202be700304846d0102d6840a027d02068534f0988d910a0483730102d78f0404832804048462020390766601027802019202027405024b0201940a04834d020298770904815702039a6c6b0304846802049c73616e080483420201b0010481050202b565050481290206b67376c2aa640a0483280201b80704830e0102d89d0202150802720201a60602480202b073090481480201ba0204836f0102d9a50202670201b40a04822a030337c2b20a04820f0102da800604831c0201840604842802038636380804811e020188090484190201920504815402019a0304830c0201a301023d0201a6040481460201a8020484100201af0104842a0204825f0201b50304827c0201bb0804830f0105dbb878c2bc0502580107dc97e69eaf6764020259020299610104812902029d680502110202a0690a0483270203a56372030483540202a96a0204842c0205acf094abac0704821c0102dd92020483410203946d790304814f020395d7ac090228020296300404830302039b397809020a02029c770504833d0202a2700504821a0201ab010483400202b0760a04813a0201b2040483130203b3c7a10104833d0204b4e8a5a40604816f0201b90604830d0201ba090483630102de800904810a020281740a02380201880704826802018f030481670201900202790203916e710404833502019206048418020399ddba080481240202a3630a02250202b86e070481670204830d0201b90604811f0201bc070481530102df820102390201830304830102038e367901024f02039134620a0482080201920104814b020198030484790202a06b060481240201ba0802090201be08022f03023670010482170202bf6f01022d0103e0a1960304831d0302bb75060483700202a2900804817c0202a49e050485010202a6b2040482030202aa80040481720202abb30304843c0301ba0204833b0203aca677010481120206ae9f613566730504841e0202afa001020a0302a979080484060202b1a1010483730203b2a46a070482460301ba020484580203b39b610804834a0202b6a60304813b0202bfae0404837c0103e185930304845f02038889780a04826802028a85070484620301b90304826c020298a3070483270203af8962050484620202b5b7070481170202b8970602190202bd8f0a0482620103e2848f04021b0203afa861020484010301b70704824a0103e3828f05048214020284a50304812f0202909e0904824d020292b907026302049ba0336604020602029c8e0204840d0202ad860202750302a16a0802180202ae880602040202b480010483150202b7b1090483780202bdbf070482050103e480ab06020f0202889605024f020293b9030483310204968fdc93080483110202a295010484150202a69a0804844d0202a78e0a02080202aa9b0a04825f0203b2b8670402720203b5b764060482550204babacf87070481270202bd9b010481780206be9a6878696805021e0103e58bba0304821202049eab6a630a04827702029faf08024d0202a2b50a02400202a3af0704827c0203a4bd6f0504840c0202ad9d030485010202b1be040483780202b5b9060484500103e689b10404844602028abc0704811902038e98690802320203968b35090482680203979971010483060202b6a4030484050301be0a04833e0103e780a601048171020282be0304824d020286b105021b02028f960a048266020291be0804831c020299900204815502029ea70702400202a0ba040481410204a69e3870030481220202a9800602310202afb8020482170203b1af6d0802140203b5a06a0702740104e88c9a6c06048426020295bc05048326020296af0304846b02059cad72d4aa060484780205baa97364680404837b0103e983840104843902029a8a010481490203a1ac770504812c0301ba050483410202af8c0302620202b1af0302500103ea8fa60a023e020297b50904812e020298a90704825702039ea76b0304847f0202a59e080481460202a68e080482590204a9b1c6a8010481350203b38d75040482750203b89e6f0a0484030103eb828f0704824e020384837109048178020397b6690104821a02039e9e330704837f0203a68b73030484440203b980320804822c0103ec8fa80204814302039588760a04826902029bb80a024e0203afb6630802710203b4a268060483000103ed8cb50104811602029982060481550104ee8085760304834502028a870904845702028f8b04027602039da470030483040202a1a20504813c0202a8ae0504837f0202af9d0402570205b1b773d8b4080482120202bd880904832c0202be930104827b0104ef88ae680704835d0205a8ab63c2be06022e0203aaa16c080481090204adb779660804823f0202b8ae080484220203bb9165030481340106f08798b1636a0604824f0203908ea80404826203029f880402530303b4ad740a04833302039184b6090484650302acb3080483340302b1b703048272020a9281a23578f098b1893001048210030291a50404816803029d8f070483200302afa5080484440302b49f020481010303bd833603026a020393839a07024c0304a3ad6e300504842d0303abb16d0502660302adae0904830e02039481a406048164030386a16c070483530302b2a8050484320302bfb9080483170204958eb8720304832b0302b1ba090481200302babe05048209020496888b6708048358030293b4050483400304b4927a6f050483770204978b93320104810a03048fb9c7890704822803029ba702022a0302a39d060484490302a4be0a0484530302adaa0a026b0302af92040483700302b9bc0404821b02049884b56404048352030486b1716f0904827a030296b80904811d03049d9e797a060483390302a5a00a04844a0303b5856d0a04822e0302bcbd040483310303bd91740704845102039983a20a04816503039d8a330a02140303b9826b0404814e02069a8b8438687403026b0302a599010481740302a6ac0704840f0303b6896d0404831a0302b785010484370302bfac08027d02059b86b770610404845203028fbd020483550303a0be75090483620302a295040482390302b6970404813002039c97bb0a04841103059d8976c2be0604824e0302afbc05023c02039d87b40504825903028daf03048172030292be040482310302a0ab0502290303b2a16b03025d02039e929d050483040302a69f060484150302aa8607025502049f9f8b66020483160302a3be060481120203a8afad0904835e0204acb1af640204845b0204b282a47a040481610203baa68a010482490204bb9f8366060483130203bfa48f0704817c0104f18191920802220204828cad360a0482730203859c9a050481670204888eb23408025f020492ba9c3202026502049bb0bf7008021f02039f94a60104835c0203a6b496030482760203a98c960504824e0203acb7b8030483240203afa0b703023d0206b3a38c683972060483340203bbaaad0902070105f282848f790804833a020384b79f0a048170020590a98c616a0804843b02049a92be730904822f02039da18b080482530203a28f8f06048309070d06070706070807070807080b0707070b09070b07070906070f0b07080607090709070708070708080706070b070707080907070808070608080707080707070808070a0806060707070b0b07070707080807070b0d0709060b0708070707080708090808070808090b0707070707080609070d07070709060806070707070707070708070c0c08090708090a07080c070a0608070707090807090707070607070b07070a0c08070809080b08090808080808070807090a07070807070706090709070c070707070809070708060608070908080808080807090c07080809070908080909080708090807080809070908080709080707070808080807080a0808070808090a080b090a070708090808080908080909080709080708080807080a070808080a08080b0b0908090707070808080908080a0909090909090909090907080909080a0807090808070b08080a0a090a08090c090709090808100808080808080a0808090908080a08080a080a0a0a0708080708080a0a080a080908090908090b08080908070b08090808090b0709080807080908070a08090a0a090a09090a0909090909090909080c080b090b0a09'); +INSERT INTO t1_data VALUES(274877906951,X'000002020630f2abab907a060481130204b381af77040481790203bea6a6070484250108f380b5a33633737701048229020383be9f0404837102048495bf73070481060204938c9a6e0904823502039eb39201022302049fbb9f6c0a0481500204a3909e620404837d0204a9ac9a75020483110203bd8c850a048111030293ad0a048247030294b9040483550203bea4ac0702220105f483b5ab66030482440302bda306048141020584b2bb643804048142020586b1a8677a0204831d0205878c916c770804821f02038da1bf0704824302038f81bd0304825102039e91b20302420203b29b9403021f0203b3a1b10704825a0203b5afb4040481210204b98da0780a02060104f582b1b00404811002038f9fbc010482200302b8a406048419020394b7b80404834c02039bb5b005022b0203aa88b9060481390203aba48f0604842a0205af969438680602360203b88d8903026c0104f68386ad0904846702049aae8f79030483520203a8989a0604836c0203b18cb4070482260203b9a98d050483730203bebc870104824c0104f78283840804816703028db20302600303a8b46106020b020389b2a80104835002048e809339010481190205929193d0b409048253020393aebc0204816202059787b96f690704817202079b949dc78934620404817f02039db28b020483750205a288a7c7a3080482780203a5948d0204811a040b0a090e090a0a080a0a0a090808080b080b0b0b090908080909090a0908090809090a080a0a090909090a0708090a0b090b0d090b'); +CREATE TABLE IF NOT EXISTS 't1_idx'(segid, term, pgno, PRIMARY KEY(segid, term)) WITHOUT ROWID; +INSERT INTO t1_idx VALUES(2,X'',2); +INSERT INTO t1_idx VALUES(2,X'30627a',4); +INSERT INTO t1_idx VALUES(2,X'306a32',6); +INSERT INTO t1_idx VALUES(2,X'307173',8); +INSERT INTO t1_idx VALUES(2,X'307972',10); +INSERT INTO t1_idx VALUES(2,X'30cebc66',12); +INSERT INTO t1_idx VALUES(2,X'30f2ab',14); +CREATE TABLE IF NOT EXISTS 't1_content'(id INTEGER PRIMARY KEY, c0, c1,Öc2); +INSERT INTO t1_content VALUES(1,'COMPILER=gcc-5.4.0 20160609','X','RTRIM'); +INSERT INTO t1_content VALUES(2,'COMPILER=gcc-5.4.0 20160609','X','NOCASE'); +INSERT INTO t1_content VALUES(3,'COMPILER=gcc-5.4.0 20160609','X','BINARY'); +INSERT INTO t1_content VALUES(4,'DEBUG','X','RTRIM'); +INSERT INTO t1_content VALUES(5,'DEBUG','X','NOCASE'); +INSERT INTO t1_content VALUES(6,'DEBUG','X','BINARY'); +INSERT INTO t1_content VALUES(7,'ENABLE DBSTAT VTAB','X','RTRIM'); +INSERT INTO t1_content VALUES(8,'ENABLE DBSTAT VTAB','X','NOCASE'); +INSERT INTO t1_content VALUES(9,'ENABLE DBSTAT VTAB','X','BINARY'); +INSERT INTO t1_content VALUES(10,'ENABLE FTS4','X','RTRIM'); +INSERT INTO t1_content VALUES(11,'ENABLE FTS4','X','NOCASE'); +INSERT INTO t1_content VALUES(12,'ENABLE FUS4','X','BINARY'); +INSERT INTO t1_content VALUES(0,NULL,NULL,NULL); +INSERT INTO t1_content VALUES(33,'OMIT LOAD EXTENSION','X','BINARY'); +INSERT INTO t1_content VALUES(32,'OMIT LOAD EXTENSION','X','NOCASE'); +INSERT INTO t1_content VALUES(31,'OMYT LOAD EXTENSION','X','RTRIM'); +INSERT INTO t1_content VALUES(30,'MAX MEMORY=50000000','W','BINARY'); +INSERT INTO t1_content VALUES(29,'MAX MEMORY=50000000','X','NOCASE'); +INSERT INTO t1_content VALUES(28,'MAX MEMORY=50000000','X','RTRIM'); +INSERT INTO t1_content VALUES(27,'ENABLE RTREE','X','BINARY'); +INSERT INTO t1_content VALUES(26,'ENABLE RTREE','Y','NOCASE'); +INSERT INTO t1_content VALUES(25,'ENABLE RTREE','X','RTRIM'); +INSERT INTO t1_content VALUES(24,'ENABLE MEMSYS5','X','BINARY'); +INSERT INTO t1_content VALUES(23,'ENABLE MEMSYS5','X','NOCASE'); +INSERT INTO t1_content VALUES(22,'ENABLE MEMSYS5','X','RTRIM'); +INSERT INTO t1_content VALUES(21,'ENABLE JSON1','X','BINARY'); +INSERT INTO t1_content VALUES(20,'ENABLE JSON1','X','NOCASE'); +INSERT INTO t1_content VALUES(18,'ENABLE GEOPOLY','X','BINARY'); +INSERT INTO t1_content VALUES(17,'EOABLE GEOPOLY','X','NOCQSE'); +INSERT INTO t1_content VALUES(16,'ENABLE GEOPOLY','X','RTRIM'); +INSERT INTO t1_content VALUES(15,'ENABLE FTS5','X','BINARY'); +INSERT INTO t1_content VALUES(14,'ENABLE FTS5','X','NOCASE'); +CREATE TABLE IF NOT EXISTS 't1_docsize'(id INTEGER PRIMARY KEY, sz BLOB); +CREATE TABLE IF NOT EXISTS 't1_config'(k PRIMARY KEY, v) WITHOUT ROWID; +CREATE TABLE t2(x); +INSERT INTO t2 VALUES('optimize'); +INSERT INTO t2 VALUES('rebuild'); +INSERT INTO t2 VALUES('integrity-check'); +PRAGMA writable_schema=OFF; +COMMIT; +} {} + +do_catchsql_test 51.1 { + SELECT max(rowid)==0 FROM t1('e*'); +} {/.*fts5: corrupt.*/} + +#-------------------------------------------------------------------------- +reset_db +do_test 52.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 40960 pagesize 4096 filename crash-2b92f77ddfe191.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 0a .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 0d 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 96: 00 00 00 00 0d 00 00 00 0d 0b 6e 00 0f a3 0f 4c ..........n....L +| 112: 0e e1 0e 81 0e 24 0d cc 0d 72 0d 1b 0c b0 0c 50 .....$...r.....P +| 128: 0b f8 0b b3 0b 6e 00 00 00 00 00 00 00 00 00 00 .....n.......... +| 2912: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 43 0d ..............C. +| 2928: 06 17 11 11 08 75 74 61 62 6c 65 74 34 74 34 43 .....utablet4t4C +| 2944: 52 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 REATE VIRTUAL TA +| 2960: 42 4c 45 20 74 34 20 55 53 49 4e 47 20 66 74 73 BLE t4 USING fts +| 2976: 35 76 6f 63 61 62 28 27 74 32 27 2c 20 27 72 6f 5vocab('t2', 'ro +| 2992: 77 27 29 43 0c 06 17 11 11 08 75 74 61 62 6c 65 w')C......utable +| 3008: 74 33 74 33 43 52 45 41 54 45 20 56 49 52 54 55 t3t3CREATE VIRTU +| 3024: 41 4c 20 54 41 42 4c 45 20 74 33 20 55 53 49 4e AL TABLE t3 USIN +| 3040: 47 20 66 74 73 35 76 6f 63 61 62 28 27 74 31 27 G fts5vocab('t1' +| 3056: 2c 20 27 72 6f 77 27 29 56 0b 06 17 1f 1f 01 7d , 'row')V....... +| 3072: 74 61 62 6c 75 74 32 5f 63 6f 6e 66 69 67 74 32 tablut2_configt2 +| 3088: 5f 63 6f 6e 66 69 67 0a 43 52 45 41 54 45 20 54 _config.CREATE T +| 3104: 41 42 4c 45 20 27 74 32 5f 63 6f 6e 66 69 67 27 ABLE 't2_config' +| 3120: 28 6b 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 (k PRIMARY KEY, +| 3136: 76 29 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 v) WITHOUT ROWID +| 3152: 5e 0a 07 17 21 21 01 81 07 74 61 62 6c 65 74 32 ^...!!...tablet2 +| 3168: 5f 63 6f 6e 74 65 6e 74 74 32 5f 63 6f 6e 74 65 _contentt2_conte +| 3184: 6e 74 09 43 52 45 41 54 45 20 54 41 42 4c 45 20 nt.CREATE TABLE +| 3200: 27 74 32 5f 63 6f 6e 74 65 6e 74 27 28 69 64 20 't2_content'(id +| 3216: 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 INTEGER PRIMARY +| 3232: 4b 45 59 2c 20 63 30 2c 20 63 31 2c 20 63 32 29 KEY, c0, c1, c2) +| 3248: 69 09 07 17 19 19 01 81 2d 74 61 62 6c 65 74 32 i.......-tablet2 +| 3264: 5f 69 64 78 74 32 5f 69 64 78 08 43 52 45 41 54 _idxt2_idx.CREAT +| 3280: 45 20 54 41 42 4c 45 20 27 74 32 5f 69 64 78 27 E TABLE 't2_idx' +| 3296: 28 73 65 67 69 64 2c 20 74 65 72 6d 2c 20 70 67 (segid, term, pg +| 3312: 6e 6f 2c 20 50 52 49 4d 41 52 59 20 4b 45 59 28 no, PRIMARY KEY( +| 3328: 73 65 67 69 64 2c 20 74 65 72 6d 29 29 20 57 49 segid, term)) WI +| 3344: 54 48 4f 55 54 20 52 4f 57 49 44 55 08 07 17 1b THOUT ROWIDU.... +| 3360: 1b 01 81 01 74 61 62 6c 65 74 32 5f 64 61 74 61 ....tablet2_data +| 3376: 74 32 5f 64 61 74 61 07 43 52 45 41 54 45 20 54 t2_data.CREATE T +| 3392: 41 42 4c 45 20 27 74 32 5f 64 61 74 61 27 28 69 ABLE 't2_data'(i +| 3408: 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 d INTEGER PRIMAR +| 3424: 59 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 4c 4f Y KEY, block BLO +| 3440: 42 29 58 07 07 17 11 11 08 81 1d 74 61 62 6c 65 B)X........table +| 3456: 74 32 74 32 43 52 45 41 54 45 20 56 49 52 54 55 t2t2CREATE VIRTU +| 3472: 41 4c 20 54 41 42 4c 45 20 74 32 20 55 53 49 4e AL TABLE t2 USIN +| 3488: 47 20 66 74 73 35 28 27 61 27 2c 5b 62 5d 2c 22 G fts5('a',[b],. +| 3504: 63 22 2c 64 65 74 61 69 6c 3d 6e 6f 6e 65 2c 63 c.,detail=none,c +| 3520: 6f 6c 75 6d 6e 73 69 7a 65 3d 30 29 56 06 06 17 olumnsize=0)V... +| 3536: 1f 1f 01 7d 74 61 62 6c 65 74 31 5f 63 6f 6e 66 ....tablet1_conf +| 3552: 69 67 74 31 5f 63 6f 6e 66 69 67 06 43 52 45 41 igt1_config.CREA +| 3568: 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 6f 6e TE TABLE 't1_con +| 3584: 66 69 67 27 28 6b 20 50 52 49 4d 41 52 59 20 4b fig'(k PRIMARY K +| 3600: 45 59 2c 20 76 29 20 57 49 54 48 4f 55 54 20 52 EY, v) WITHOUT R +| 3616: 4f 57 49 44 5b 05 07 17 21 21 01 81 01 74 61 62 OWID[...!!...tab +| 3632: 6c 65 74 31 5f 64 6f 63 73 69 7a 65 74 31 5f 64 let1_docsizet1_d +| 3648: e8 63 73 69 7a 65 05 43 52 45 41 54 45 20 54 41 .csize.CREATE TA +| 3664: 42 4c 45 20 27 74 31 5f 64 6f 63 73 69 7a 65 27 BLE 't1_docsize' +| 3680: 28 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d (id INTEGER PRIM +| 3696: 41 52 59 20 4b 45 59 2c 20 73 7a 20 42 4c 4f 42 ARY KEY, sz BLOB +| 3712: 29 5e 04 07 17 21 21 01 81 07 74 61 62 6c 65 74 )^...!!...tablet +| 3728: 31 5f 63 6f 6e 74 65 6f 74 74 31 5f 63 6f 6e 74 1_conteott1_cont +| 3744: 65 6e 74 04 43 52 45 41 54 45 20 54 41 42 4c 45 ent.CREATE TABLE +| 3760: 20 27 74 31 5f 63 6f 6e 74 65 6e 74 27 28 69 64 't1_content'(id +| 3776: 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 INTEGER PRIMARY +| 3792: 20 4b 45 59 2c 20 63 30 2c 20 63 31 2c 20 63 32 KEY, c0, c1, c2 +| 3808: 29 69 03 07 17 19 19 01 81 2d 74 61 62 6c 65 74 )i.......-tablet +| 3824: 31 5f 69 64 78 74 31 5f 69 64 78 03 43 52 45 41 1_idxt1_idx.CREA +| 3840: 54 45 20 54 41 42 4c 45 20 27 74 31 5f 69 64 78 TE TABLE 't1_idx +| 3856: 27 28 73 65 67 69 64 2c 20 74 65 72 6d 2c 20 70 '(segid, term, p +| 3872: 67 6e 6f 2c 20 50 52 49 4d 41 52 59 20 4b 45 59 gno, PRIMARY KEY +| 3888: 28 73 65 67 69 64 2c 20 74 65 72 6d 29 29 20 57 (segid, term)) W +| 3904: 49 54 48 4f 55 54 20 52 4f 57 49 44 55 02 07 17 ITHOUT ROWIDU... +| 3920: 1b 1b 01 81 01 74 61 62 6c 65 74 31 5f 64 61 74 .....tablet1_dat +| 3936: 61 74 31 5f 64 61 74 61 02 43 52 45 41 54 45 20 at1_data.CREATE +| 3952: 54 41 42 4c 45 20 27 74 31 5f 64 61 74 61 27 28 TABLE 't1_data'( +| 3968: 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 id INTEGER PRIMA +| 3984: 52 59 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 4c RY KEY, block BL +| 4000: 4f 42 29 5b 01 07 17 11 11 08 81 23 74 61 62 6c OB)[.......#tabl +| 4016: 65 74 31 74 31 43 52 45 41 54 45 20 56 49 52 54 et1t1CREATE VIRT +| 4032: 55 41 4c 20 54 41 42 4c 45 20 74 31 20 55 53 49 UAL TABLE t1 USI +| 4048: 4e 47 20 66 74 73 35 28 61 2c 62 20 75 6e 69 6e NG fts5(a,b unin +| 4064: 64 65 78 65 64 2c 63 2c 74 6f 6b 65 6e 69 7a 65 dexed,c,tokenize +| 4080: 3d 22 70 6f 72 74 65 72 20 61 73 63 69 69 22 29 =.porter ascii.) +| page 2 offset 4096 +| 0: 0d 0f 68 00 05 0f 13 00 0f e6 0f 13 0f a8 0f 7c ..h............| +| 16: 0f 2a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 .*.............. +| 3856: 00 00 00 15 0a 03 00 30 00 00 00 00 01 03 03 00 .......0........ +| 3872: 03 01 01 01 02 01 01 03 01 01 37 8c 80 80 80 80 ..........7..... +| 3888: 01 03 00 74 00 00 00 2e 02 30 61 03 02 02 01 01 ...t.....0a..... +| 3904: 62 03 02 03 01 01 63 03 02 04 01 01 67 03 08 c1 b.....c.....g... +| 3920: 02 02 01 01 68 03 06 01 02 03 01 01 69 03 06 01 ....h.......i... +| 3936: 02 04 04 06 06 06 08 08 0f ef 00 14 2a 00 00 00 ............*... +| 3952: 00 01 02 02 00 02 01 01 01 02 01 01 25 88 80 80 ............%... +| 3968: 80 80 01 03 00 50 00 00 00 1f 02 30 67 02 08 02 .....P.....0g... +| 3984: 01 02 02 01 01 68 02 08 03 01 02 03 01 01 69 02 .....h........i. +| 4000: 08 04 01 02 04 04 09 09 37 84 80 80 80 80 01 03 ........7....... +| 4016: 00 74 00 14 00 2e 02 30 61 01 02 02 01 01 62 01 .t.....0a.....b. +| 4032: 02 03 01 01 63 01 02 04 01 01 67 01 06 01 02 02 ....c.....g..... +| 4048: 01 01 68 01 06 01 02 03 01 01 69 01 06 01 02 03 ..h.......i..... +| 4064: f4 06 06 06 08 08 07 01 03 00 14 03 09 00 09 00 ................ +| 4080: 00 00 11 24 00 00 00 00 01 01 01 00 01 00 01 01 ...$............ +| page 3 offset 8192 +| 0: 0a 00 00 00 03 0f ec 00 0f fa 0f f3 0f ec 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 00 06 04 01 0c ................ +| 4080: 01 03 02 06 04 01 0c 01 02 02 05 04 09 0c 01 02 ................ +| page 4 offset 12288 +| 0: 0d 00 00 00 03 0f be 00 0f ea 0f d4 0f be 00 00 ................ +| 16: 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 ................ +| 4016: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 14 03 ................ +| 4032: 05 00 17 17 17 61 20 62 20 63 67 20 68 20 69 67 .....a b cg h ig +| 4048: 20 68 20 69 14 02 05 00 17 17 17 67 20 68 20 69 h i.......g h i +| 4064: 61 20 62 20 63 67 20 68 20 69 14 01 05 00 17 17 a b cg h i...... +| 4080: 17 61 20 62 20 63 64 20 65 20 66 67 20 68 20 69 .a b cd e fg h i +| page 5 offset 16384 +| 0: 0d 00 00 00 03 0f e8 00 0f f8 0f f0 0f e8 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 06 03 03 00 12 03 00 03 ................ +| 4080: 06 02 03 00 12 03 00 03 06 01 04 00 12 03 00 03 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| page 7 offset 24576 +| 0: 0d 00 00 00 03 0f 9e 00 0f e6 0f ef 0f 9e 00 00 ................ +| 3968: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 41 84 ..............A. +| 3984: 80 80 80 80 01 04 00 81 06 00 00 00 34 02 30 61 ............4.0a +| 4000: 01 01 01 01 01 62 01 01 01 01 01 63 01 01 01 01 .....b.....c.... +| 4016: 01 64 01 01 01 65 01 01 01 66 01 01 01 67 01 01 .d...e...f...g.. +| 4032: 01 01 01 68 01 01 01 01 01 69 01 01 01 04 06 06 ...h.....i...... +| 4048: 06 04 04 04 06 06 07 01 03 00 14 03 09 09 09 0f ................ +| 4064: 0a 03 00 24 00 00 00 00 01 01 01 00 01 01 01 01 ...$............ +| 4080: 0a 00 00 00 01 0f fa 00 0f fa 00 00 00 00 00 00 ................ +| page 8 offset 28672 +| 4080: 00 00 00 00 00 00 00 00 00 00 05 04 09 0c 01 02 ................ +| page 9 offset 32768 +| 0: 0d 00 00 00 03 0f be 00 0f ea 0f d4 0f be 00 00 ................ +| 4016: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 14 03 ................ +| 4032: 05 00 17 17 17 61 20 62 20 63 67 20 68 20 69 67 .....a b cg h ig +| 4048: 20 68 20 69 14 02 05 00 17 17 17 67 20 68 20 69 h i.......g h i +| 4064: 61 20 62 20 63 67 20 68 20 69 14 01 05 00 17 17 a b cg h i...... +| 4080: 17 61 20 62 20 63 64 20 65 20 66 67 20 68 20 69 .a b cd e fg h i +| page 10 offset 36864 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| end crash-2b92f77ddfe191.db +}]} {} + +do_catchsql_test 52.1 { + SELECT fts5_decode(id, block) FROM t1_data; +} {/*malformed database schema*/} + +#------------------------------------------------------------------------- +reset_db +do_test 53.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 24576 pagesize 4096 filename crash-dbe9b7614da103.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 06 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 06 00 00 00 04 ................ +| 96: 00 00 00 00 0d 00 00 00 06 0e 0f 00 0f aa 0f 53 ...............S +| 112: 0e e8 0e 8b 0e 33 0e 0f 00 00 00 00 00 00 00 00 .....3.......... +| 3584: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 22 ................ +| 3600: 06 06 17 11 11 01 31 74 61 62 6c 65 62 62 62 62 ......1tablebbbb +| 3616: 06 43 52 45 41 54 45 20 54 41 42 4c 45 20 62 62 .CREATE TABLE bb +| 3632: 28 61 29 56 05 06 17 1f 1f 01 7d 74 61 62 6c 65 (a)V.......table +| 3648: 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 6f 6e 66 t1_configt1_conf +| 3664: 69 67 05 43 52 45 41 54 45 20 54 41 42 4c 45 20 ig.CREATE TABLE +| 3680: 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b 20 50 52 't1_config'(k PR +| 3696: 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 20 57 49 IMARY KEY, v) WI +| 3712: 54 48 4f 55 54 20 52 4f 57 49 44 5b 04 07 17 21 THOUT ROWID[...! +| 3728: 21 01 81 01 74 61 62 6c 65 74 31 5f 64 6f 63 73 !...tablet1_docs +| 3744: 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 04 43 52 izet1_docsize.CR +| 3760: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 64 EATE TABLE 't1_d +| 3776: 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 54 45 47 ocsize'(id INTEG +| 3792: 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 ER PRIMARY KEY, +| 3808: 73 7a 20 42 4c 4f 42 29 69 03 07 17 19 19 01 81 sz BLOB)i....... +| 3824: 2d 74 61 62 6c 65 74 31 5f 69 64 78 74 31 5f 69 -tablet1_idxt1_i +| 3840: 64 78 03 43 52 45 41 54 45 20 54 41 42 4c 45 20 dx.CREATE TABLE +| 3856: 27 74 31 5f 69 64 78 27 28 73 65 67 69 64 2c 20 't1_idx'(segid, +| 3872: 74 65 72 6d 2c 20 70 67 6e 6f 2c 20 50 52 49 4d term, pgno, PRIM +| 3888: 41 52 59 20 4b 45 59 28 73 65 67 69 64 2c 20 74 ARY KEY(segid, t +| 3904: 65 72 6d 29 29 20 57 49 54 48 4f 55 54 20 52 4f erm)) WITHOUT RO +| 3920: 57 49 44 55 02 07 17 1b 1b 01 81 01 74 61 62 6b WIDU........tabk +| 3936: 65 74 31 5f 64 61 74 61 74 31 5f 64 61 74 61 02 et1_datat1_data. +| 3952: 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 CREATE TABLE 't1 +| 3968: 5f 64 61 74 61 27 28 69 64 20 49 4e 54 45 47 45 _data'(id INTEGE +| 3984: 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 62 R PRIMARY KEY, b +| 4000: 6c 6f 63 6b 20 42 4c 4f 42 29 54 01 07 17 10 11 lock BLOB)T..... +| 4016: 08 81 15 74 61 62 6c 65 74 31 74 31 43 52 45 41 ...tablet1t1CREA +| 4032: 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45 TE VIRTUAL TABLE +| 4048: 20 74 31 20 55 53 49 4e 47 20 66 74 73 35 28 61 t1 USING fts5(a +| 4064: 2c 62 2c 70 72 65 66 69 78 3d 22 31 2c 32 2c 33 ,b,prefix=.1,2,3 +| 4080: 2c 34 22 2c 20 63 6f 6e 74 65 6e 74 3d 22 22 29 ,4., content=..) +| page 2 offset 4096 +| 0: 0d 0b 6a 00 37 09 4c 02 0f e7 09 4c 0f c6 0f a4 ..j.7.L....L.... +| 16: 0f 88 0f 6d 0f 4b 0f 2c 0f 0e 0e ec 0e cd 0e 2a ...m.K.,.......* +| 32: 0e 8e 0e 6c 0e 4b 0e 29 0e 08 0d e6 0d c4 0d b5 ...l.K.)........ +| 48: 0d 97 0d 76 0d 54 0e 31 0d 15 0c f3 0c d3 0c b5 ...v.T.1........ +| 64: 0c 95 0c 73 0c 54 0c 32 0c 10 0b ee 0b cc 0b b0 ...s.T.2........ +| 80: 0b 8d 0b 7e 0b 48 0b 2e 0b 0b 0a ef 0a cc 0a ad ...~.H.......... +| 96: 0a 8c 0a 6d 0a 4d 0a 2b 0a 0c 09 ec 09 ca 09 a8 ...m.M.+........ +| 112: 09 86 09 63 0f f1 00 00 00 00 00 00 00 00 00 00 ...c............ +| 2368: 00 00 00 00 00 00 00 00 00 00 00 00 15 0a 03 00 ................ +| 2384: 30 00 00 00 01 01 03 35 00 03 01 01 12 02 01 12 0......5........ +| 2400: 03 01 11 1c 8c 80 80 80 80 10 03 00 3e 00 00 00 ............>... +| 2416: 17 01 05 05 34 74 61 62 6c 03 02 03 01 04 77 68 ....4tabl.....wh +| 2432: 65 72 03 02 06 09 1b 8c 80 80 80 80 0f 03 00 3c er.............< +| 2448: 00 00 00 16 05 34 66 74 73 34 03 02 02 01 04 6e .....4fts4.....n +| 2464: 75 6d 62 03 06 01 04 09 1b 8c 80 80 80 80 0e 03 umb............. +| 2480: 00 3c 00 00 00 16 04 33 74 68 65 03 06 01 01 04 .<.....3the..... +| 2496: 01 03 77 68 65 03 02 04 04 0a 1b 8c 80 80 80 80 ..whe........... +| 2512: 0d 03 00 3c 00 00 00 16 04 33 6e 75 6d 03 06 01 ...<.....3num... +| 2528: 01 05 01 03 74 61 62 03 02 03 04 0a 19 8c 80 80 ....tab......... +| 2544: 80 80 0c 03 00 38 00 00 00 14 03 32 77 68 03 02 .....8.....2wh.. +| 2560: 04 00 04 33 66 74 73 03 02 02 04 07 18 8c 80 80 ...3fts......... +| 2576: 80 80 0b 03 00 36 00 00 00 13 03 32 74 61 03 02 .....6.....2ta.. +| 2592: 03 02 01 68 03 06 01 01 04 04 07 1b 8c 80 80 80 ...h............ +| 2608: 80 0a 03 00 3c 00 00 00 16 03 32 6e 75 03 06 01 ....<.....2nu... +| 2624: 01 05 01 02 6f 66 03 06 01 01 06 04 09 19 8c 80 ....of.......... +| 2640: 80 80 80 09 03 00 38 00 00 00 14 03 32 66 74 03 ......8.....2ft. +| 2656: 02 02 01 02 69 73 03 06 01 01 03 04 07 18 8c 80 ....is.......... +| 2672: 80 80 80 08 03 00 36 00 00 00 13 02 31 74 03 08 ......6.....1t.. +| 2688: 03 01 01 04 01 01 77 03 02 04 04 09 1a 8c 80 80 ......w......... +| 2704: 80 80 07 03 00 3a 00 00 00 15 02 31 6e 03 08 01 .....:.....1n... +| 2720: 01 02 05 01 01 6f 03 06 01 01 06 04 09 18 8c 80 .....o.......... +| 2736: 80 80 80 06 03 00 36 00 00 00 13 04 02 31 66 03 ......6......1f. +| 2752: 02 02 01 01 69 03 06 01 01 03 05 06 1c 8c 80 80 ....i........... +| 2768: 80 80 05 03 00 3e 00 00 00 17 04 30 74 68 65 03 .....>.....0the. +| 2784: 06 01 01 04 01 05 77 68 65 72 65 03 02 04 0a 15 ......where..... +| 2800: 8c 80 80 80 80 04 03 00 30 00 00 00 11 01 01 06 ........0....... +| 2816: 06 30 74 61 62 6c 65 03 02 03 07 1c 8c 80 80 80 .0table......... +| 2832: 80 03 03 00 3e 00 00 00 17 07 30 6e 75 6d 62 65 ....>.....0numbe +| 2848: 72 03 06 01 01 05 01 02 6f 66 03 06 04 0d 13 8c r.......of...... +| 2864: 80 80 80 80 02 03 00 2c 00 00 00 0f 01 01 03 02 .......,........ +| 2880: 30 6e 03 06 01 01 02 07 1b 8c 80 80 80 80 01 03 0n.............. +| 2896: 00 3c 00 00 00 16 08 30 66 74 73 34 61 75 78 03 .<.....0fts4aux. +| 2912: 02 02 01 02 69 73 03 06 04 0c 00 00 00 14 2a 00 ....is........*. +| 2928: 00 00 01 01 02 24 00 02 01 01 12 02 01 12 08 88 .....$.......... +| 2944: 80 80 80 80 12 03 00 16 00 00 00 05 02 1c 88 80 ................ +| 2960: 80 80 80 11 03 00 3e 00 00 00 17 05 34 72 6f 77 ......>.....4row +| 2976: 73 02 06 01 01 05 01 04 74 68 65 72 02 02 04 0b s.......ther.... +| 2992: 15 88 80 80 80 80 10 03 00 30 00 00 00 11 02 01 .........0...... +| 3008: 01 07 05 34 62 65 74 77 02 02 04 08 1b 88 80 80 ...4betw........ +| 3024: 80 80 0f 03 00 3c 00 00 00 16 04 04 33 72 6f 77 .....<......3row +| 3040: 02 06 01 01 05 01 03 74 68 65 02 08 05 0a 1b 88 .......the...... +| 3056: 80 80 80 80 0e 03 00 3c 00 00 00 16 01 01 02 04 .......<........ +| 3072: 33 61 72 65 02 02 03 01 03 62 65 74 02 02 07 08 3are.....bet.... +| 3088: 1b 88 80 80 80 80 0d 03 00 3c 00 00 00 16 03 32 .........<.....2 +| 3104: 74 68 02 08 02 01 01 07 00 04 33 61 6e 64 02 06 th........3and.. +| 3120: 04 0a 1b 89 80 80 80 80 0c 03 00 3c 00 00 00 16 ...........<.... +| 3136: 03 32 69 6e 02 06 01 01 06 01 02 72 6f 02 06 01 .2in.......ro... +| 3152: 01 05 04 09 18 88 80 80 80 80 0b 03 00 36 00 00 .............6.. +| 3168: 00 13 02 03 32 61 72 02 02 03 01 02 62 65 02 02 ....2ar.....be.. +| 3184: 04 05 07 1b 88 80 80 80 80 0a 03 00 3c 00 00 00 ............<... +| 3200: 16 02 31 74 02 08 02 01 01 07 00 03 32 61 6e 02 ..1t........2an. +| 3216: 06 01 01 04 09 19 88 80 80 80 80 09 03 00 38 00 ..............8. +| 3232: 00 00 14 02 31 6e 02 06 01 01 03 01 01 72 02 06 ....1n.......r.. +| 3248: 01 01 05 04 08 17 88 80 80 80 80 08 03 00 34 00 ..............4. +| 3264: 00 00 12 02 31 62 02 02 04 01 01 69 02 06 01 01 ....1b.....i.... +| 3280: 06 04 06 19 88 80 80 80 80 07 03 00 38 00 00 00 ............8... +| 3296: 14 04 02 31 32 02 02 05 01 01 61 02 08 03 01 01 ...12.....a..... +| 3312: 02 05 06 1b 88 80 80 80 80 06 03 00 3c 00 00 00 ............<... +| 3328: 16 06 30 74 68 65 72 65 02 02 02 00 02 31 31 02 ..0there.....11. +| 3344: 06 01 01 04 0a 15 88 80 80 80 80 05 03 00 30 00 ..............0. +| 3360: 00 00 11 01 01 05 04 30 74 68 65 02 06 01 01 07 .......0the..... +| 3376: 07 1c 88 80 80 80 80 04 03 00 3e 00 00 00 17 01 ..........>..... +| 3392: 01 06 02 30 6e 02 06 01 01 03 01 04 72 6f 77 73 ...0n.......rows +| 3408: 02 06 07 08 1b 88 80 80 80 80 03 03 00 3c 00 00 .............<.. +| 3424: 00 16 08 30 62 65 74 77 65 65 6e 02 02 04 01 02 ...0between..... +| 3440: 69 6e 02 06 04 0c 1a 88 80 80 80 80 02 03 00 3a in.............: +| 3456: 00 00 00 15 04 30 61 6e 64 02 06 01 01 02 02 02 .....0and....... +| 3472: 72 65 02 02 03 04 0a 16 88 80 80 80 80 01 03 00 re.............. +| 3488: 34 00 00 00 12 02 30 31 02 06 01 01 04 01 01 32 4.....01.......2 +| 3504: 02 02 05 04 08 08 84 80 80 80 80 12 03 00 16 00 ................ +| 3520: 00 00 05 04 1b 84 80 80 80 80 11 03 00 3c 00 00 .............<.. +| 3536: 00 16 05 34 74 61 62 6c 01 06 01 01 05 02 03 65 ...4tabl.......e +| 3552: 72 6d 01 02 04 0b 1b 84 80 80 80 80 10 03 00 3c rm.............< +| 3568: 00 00 00 16 05 34 65 61 63 68 01 02 03 01 04 70 .....4each.....p +| 3584: 72 65 73 01 02 05 04 09 1a 84 80 80 80 80 0f 03 res............. +| 3600: 00 3a 00 00 00 15 04 33 74 65 72 01 02 04 02 02 .:.....3ter..... +| 3616: 68 65 01 06 01 01 03 04 08 1b 84 80 80 80 80 0e he.............. +| 3632: 03 00 3c 00 00 00 16 04 33 70 72 65 01 02 05 01 ..<.....3pre.... +| 3648: 03 74 61 62 01 06 01 01 05 04 08 1a 84 80 80 80 .tab............ +| 3664: 80 0d 03 00 3a 00 00 00 15 04 33 66 6f 72 01 02 ....:.....3for.. +| 3680: 02 02 02 74 73 01 06 01 01 04 04 08 1b 84 80 80 ...ts........... +| 3696: 80 80 0c 03 00 3c 00 00 00 16 03 32 74 68 01 06 .....<.....2th.. +| 3712: 01 01 03 00 04 33 65 61 63 01 02 03 04 09 18 84 .....3eac....... +| 3728: 80 80 80 80 0b 03 00 36 00 00 00 13 03 32 74 61 .......6.....2ta +| 3744: 01 06 01 01 05 02 01 65 01 02 04 04 09 19 84 70 .......e.......p +| 3760: 80 80 80 0a 03 00 38 00 00 00 14 03 32 69 6e 01 ......8.....2in. +| 3776: 06 01 01 02 01 02 70 72 01 02 05 04 09 18 84 80 ......pr........ +| 3792: 80 80 80 09 03 00 36 00 00 00 13 03 32 66 6f 01 ......6.....2fo. +| 3808: 02 02 02 01 74 01 06 01 01 04 04 07 1b 84 80 80 ....t........... +| 3824: 80 80 08 03 00 3c 00 00 00 16 02 31 74 01 0a 04 .....<.....1t... +| 3840: 01 01 03 04 00 03 32 65 61 01 02 03 04 0a 17 84 ......2ea....... +| 3856: 80 80 80 80 07 03 00 34 00 00 00 12 02 31 69 01 .......4.....1i. +| 3872: 06 01 01 02 01 01 70 01 02 05 04 08 18 84 80 80 ......p......... +| 3888: 80 80 06 03 00 36 00 00 00 03 02 31 65 01 02 03 .....6.....1e... +| 3904: 01 01 66 02 08 02 01 01 04 04 06 1b 84 80 80 80 ..f............. +| 3920: 80 05 03 00 3c 00 00 00 16 05 30 74 65 72 6d 01 ....<.....0term. +| 3936: 02 04 02 02 68 65 01 06 01 01 03 04 09 14 84 80 ....he.......... +| 3952: 80 80 80 04 03 00 2e 00 00 00 10 06 30 74 61 62 ............0tab +| 3968: 6c 65 01 06 01 01 05 04 15 84 80 80 80 80 03 03 le.............. +| 3984: 00 30 00 00 00 11 02 08 30 70 72 65 73 65 6e 74 .0......0present +| 4000: 01 02 05 05 1b 84 80 80 80 80 02 03 00 3c 00 00 .............<.. +| 4016: 00 16 04 30 66 74 73 01 06 01 01 04 01 02 69 6e ...0fts.......in +| 4032: 01 06 01 01 04 0a 1a 84 80 80 80 80 01 03 00 3a ...............: +| 4048: 00 00 00 15 05 30 65 61 63 68 01 02 03 01 03 66 .....0each.....f +| 4064: 6f 72 01 02 02 04 09 06 01 03 00 12 03 0b 0f 00 or.............. +| 4080: 00 08 8c 80 80 80 80 11 03 00 16 00 00 00 05 04 ................ +| page 3 offset 8192 +| 0: 0a 00 00 00 32 0e 4f 00 0f fa 0f f1 0f e9 0f e1 ....2.O......... +| 16: 0f d8 0f d1 0f c9 0f c1 0f b9 0f b1 0f a9 0f a0 ................ +| 32: 0f 98 0f 90 0f 87 0f 80 0f 78 0f 71 0f 68 0f 5f .........x.q.h._ +| 48: 0f 56 0f 4d 0f 41 0f 38 0f 2f 0f 26 0f 1d 0f 13 .V.M.A.8./.&.... +| 64: 0f 0a 0f 01 0e f7 0e ee 0e e6 0e dd 0e d6 0e cd ................ +| 80: 0e c3 0e ba 0e b0 0e a8 0e 9f 0e 86 0e 8e 0e 85 ................ +| 96: 0e 7c 0e 73 0e 6a 0e 60 0e 58 0e 4f 00 00 00 00 .|.s.j.`.X.O.... +| 3648: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 08 ................ +| 3664: 04 01 10 01 03 34 74 20 07 04 01 0e 01 03 34 1e .....4t ......4. +| 3680: 09 04 01 12 01 03 33 74 68 1c 08 04 01 10 01 03 ......3th....... +| 3696: 33 6e 1a 08 04 01 10 01 03 32 77 18 08 04 01 10 3n.......2w..... +| 3712: 01 03 32 74 16 08 04 01 10 01 03 32 6e 14 07 04 ..2t.......2n... +| 3728: 01 0e 01 03 32 12 08 04 01 10 01 03 31 74 10 07 ....2.......1t.. +| 3744: f4 01 10 01 03 31 6e 0e 07 04 01 0e 01 03 31 0c .....1n.......1. +| 3760: 09 04 01 12 01 03 30 74 68 0a 08 04 01 10 01 03 ......0th....... +| 3776: 30 74 08 09 04 01 12 01 03 30 6e 75 06 08 04 01 0t.......0nu.... +| 3792: 10 01 03 30 6e 04 06 04 01 0c 01 03 02 08 04 01 ...0n........... +| 3808: 10 01 02 34 72 22 07 04 01 0e 01 02 34 20 08 04 ...4r.......4 .. +| 3824: 01 10 01 02 33 72 1e 09 04 01 12 01 02 33 61 72 ....3r.......3ar +| 3840: 1c 08 04 01 10 01 02 32 74 1a 08 04 01 10 01 02 .......2t....... +| 3856: 32 69 18 09 04 01 12 01 02 32 61 72 16 08 04 01 2i.......2ar.... +| 3872: 10 01 02 31 74 14 08 04 01 10 01 02 31 6e 12 08 ...1t.......1n.. +| 3888: 04 01 10 01 02 31 62 10 08 04 01 10 01 02 31 32 .....1b.......12 +| 3904: 0e 0b 04 01 16 01 02 30 74 68 65 72 0c 08 04 01 .......0ther.... +| 3920: 10 01 02 30 74 0a 08 04 01 10 01 02 30 6e 08 08 ...0t.......0n.. +| 3936: 04 01 10 01 02 30 62 06 08 04 01 10 01 02 30 61 .....0b.......0a +| 3952: 04 06 04 01 0c 01 02 02 07 04 09 10 01 34 74 22 .............4t. +| 3968: 06 04 09 0e 01 34 20 08 04 09 12 01 33 74 65 1e .....4 .....3te. +| 3984: 07 04 09 10 01 33 70 1c 07 04 09 10 01 33 66 1a .....3p......3f. +| 4000: 08 04 09 12 01 32 74 68 18 07 04 09 10 01 32 74 .....2th......2t +| 4016: 16 07 04 09 0f 01 32 69 14 07 04 09 10 01 32 66 ......2i......2f +| 4032: 12 07 04 09 00 01 31 74 10 07 04 09 10 01 31 69 ......1t......1i +| 4048: 0e 06 04 09 0e 01 31 0c 08 04 09 12 01 33 44 65 ......1......3De +| 4064: 0a 07 04 09 10 01 30 74 08 07 04 09 10 01 30 75 ......0t......0u +| 4080: 26 08 04 09 12 01 30 66 74 04 05 04 09 0c 01 02 &.....0ft....... +| page 4 offset 12288 +| 0: 0d 00 00 00 03 0f eb 00 0f f9 0f f2 0f eb 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 05 03 03 00 10 ................ +| 4080: 03 05 05 02 03 00 10 04 06 05 01 03 00 10 04 04 ................ +| page 5 offset 16384 +| 0: 0a 00 00 00 02 0f eb 00 0f eb 0f f4 00 00 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 08 03 15 01 70 ...............p +| 4080: 67 73 7a 08 0b 03 1b 01 76 65 72 73 69 6f 6e 04 gsz.....version. +| page 6 offset 20480 +| 0: 0d 00 00 00 03 0f f2 00 0f 00 00 00 00 00 00 00 ................ +| 4080: 00 00 03 03 02 01 03 03 02 02 01 02 02 01 0c e9 ................ +| end crash-dbe9b7614da103.db +}]} {} + +do_catchsql_test 53.1 { + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x<>1 FROM c WHERE x<10) + INSERT INTO t1(a) SELECT randomblob(3000) FROM c; +} {/*malformed database schema*/} + +#------------------------------------------------------------------------- +reset_db +do_test 54.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 24576 pagesize 4096 filename crash-03a1855566d9ae.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 06 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 06 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 96: 00 00 00 00 0d 00 00 00 06 0e 0f 00 0f aa 0f 53 ...............S +| 112: 0e e8 0e 8b 0e 33 0e 0f 00 00 00 00 00 00 00 00 .....3.......... +| 3584: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 22 ................ +| 3600: 06 06 17 11 11 01 31 74 61 62 6c 65 62 62 62 62 ......1tablebbbb +| 3616: 06 43 52 45 41 54 45 20 54 41 42 4c 45 20 62 62 .CREATE TABLE bb +| 3632: 28 61 29 56 05 06 17 1f 1f 01 7d 74 61 62 6c 65 (a)V.......table +| 3648: 74 31 5f 63 2a 6e 66 69 67 74 31 5f 63 6f 6e 66 t1_c*nfigt1_conf +| 3664: 69 67 05 43 52 45 41 54 45 20 54 41 42 4c 45 20 ig.CREATE TABLE +| 3680: 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b 20 50 52 't1_config'(k PR +| 3696: 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 20 57 49 IMARY KEY, v) WI +| 3712: 54 48 4f 55 54 20 52 4f 57 49 44 5b 04 07 17 21 THOUT ROWID[...! +| 3728: 21 01 81 01 74 61 62 6c 65 74 31 5f 64 6f 73 73 !...tablet1_doss +| 3744: 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 04 43 52 izet1_docsize.CR +| 3760: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 64 EATE TABLE 't1_d +| 3776: 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 54 45 47 ocsize'(id INTEG +| 3792: 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 ER PRIMARY KEY, +| 3808: 73 7a 20 42 4c 4f 42 29 69 03 07 17 19 19 01 81 sz BLOB)i....... +| 3824: 2d 74 61 62 6c 65 74 31 5f 69 64 78 74 31 5f 69 -tablet1_idxt1_i +| 3840: 64 78 03 43 52 45 41 54 45 20 54 41 42 4c 45 20 dx.CREATE TABLE +| 3856: 27 74 31 5f 69 64 78 27 28 73 65 67 69 64 2c 20 't1_idx'(segid, +| 3872: 74 65 72 6d 2c 20 70 67 6e 6f 2c 20 50 52 49 4d term, pgno, PRIM +| 3888: 41 52 59 20 4b 45 59 28 73 65 67 69 64 2c 20 74 ARY KEY(segid, t +| 3904: 65 72 6d 29 29 20 57 49 54 48 4f 55 54 20 52 4f erm)) WITHOUT RO +| 3920: 57 49 44 55 02 07 17 1b 1b 01 81 01 74 61 62 6c WIDU........tabl +| 3936: 65 74 31 5f 64 61 74 61 74 31 5f 64 61 74 61 02 et1_datat1_data. +| 3952: 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 CREATE TABLE 't1 +| 3968: 5f 64 61 74 61 27 28 69 64 20 49 4e 54 45 47 45 _data'(id INTEGE +| 3984: 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 62 R PRIMARY KEY, b +| 4000: 6c 6f 63 6b 20 42 4c 4f 42 29 54 01 07 17 11 11 lock BLOB)T..... +| 4016: 08 81 15 74 61 62 6c 65 74 31 74 31 43 52 45 41 ...tablet1t1CREA +| 4032: 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45 TE VIRTUAL TABLE +| 4048: 20 74 31 20 55 53 49 4e 47 20 66 74 73 35 28 61 t1 USING fts5(a +| 4064: 2c 62 2c 70 72 65 66 69 78 3d 22 32 2c 32 2c 33 ,b,prefix=.2,2,3 +| 4080: 2c 34 22 2c 20 63 6f 6e 74 65 6e 74 3d 22 22 29 ,4., content=..) +| page 2 offset 4096 +| 0: 0d 0b 6a 00 37 09 4c 02 0f e7 09 4c 0f c6 0f a4 ..j.7.L....L.... +| 16: 0f 88 0f 6d 0f 4b 0f 2c 0f 0e 0e ec 0e cd 0e ad ...m.K.,........ +| 32: 0e 8e 0e 6c 0e 4b 0e 29 0e 08 0d e6 0d c4 0d b5 ...l.K.)........ +| 48: 0d 97 0d 76 0d 54 0d 31 0d 15 0c f3 0c d3 0c b5 ...v.T.1........ +| 64: 0c 95 0c 73 0c 54 0c 32 0c 10 0b ee 0b cd 0b b0 ...s.T.2........ +| 80: 0b 8d 0b 7e 0b 48 0b 2e 0b 0b 0a ef 0a cc 0a ad ...~.H.......... +| 96: 0a 8c 0a 6d 0a 4d 0a 2b 0a 0c 09 ec 09 ca 09 a8 ...m.M.+........ +| 112: 09 86 09 63 0f f1 00 00 00 00 00 00 00 00 00 00 ...c............ +| 2368: 00 00 00 00 00 00 00 00 00 00 00 00 15 0a 03 00 ................ +| 2384: 30 00 00 00 01 01 03 35 00 03 01 01 12 02 01 12 0......5........ +| 2400: 03 01 11 1c 8c 80 80 80 80 10 03 00 3e 00 00 00 ............>... +| 2416: 17 01 05 05 34 74 61 62 6c 03 02 03 01 04 77 68 ....4tabl.....wh +| 2432: 65 72 03 02 06 09 1b 8c 80 80 80 80 0f 03 00 3c er.............< +| 2448: 00 00 00 16 05 34 66 74 73 34 03 02 02 01 04 6e .....4fts4.....n +| 2464: 75 6d 62 03 06 01 04 09 1b 8c 80 80 80 80 0e 03 umb............. +| 2480: 00 3c 00 00 00 16 04 33 74 68 65 03 06 01 01 04 .<.....3the..... +| 2496: 01 03 77 68 65 03 02 04 04 0a 1b 8c 80 80 80 80 ..whe........... +| 2512: 0d 03 00 3c 00 00 00 16 04 33 6e 75 6d 03 06 01 ...<.....3num... +| 2528: 01 05 01 03 74 61 62 05 62 03 04 0a 19 8c 80 80 ....tab.b....... +| 2544: 80 80 0c 03 00 38 00 00 00 14 03 39 a7 68 03 02 .....8.....9.h.. +| 2560: 04 10 04 33 66 74 73 03 02 02 04 07 18 8c 80 80 ...3fts......... +| 2576: 80 80 0b 03 00 36 00 00 00 13 03 32 74 61 03 02 .....6.....2ta.. +| 2592: 03 02 01 68 03 06 01 01 04 04 07 1b 8c 80 80 80 ...h............ +| 2608: 80 0a 03 00 3c 00 00 00 16 03 32 6e 75 03 06 01 ....<.....2nu... +| 2624: 01 05 01 02 6f 66 03 06 01 01 06 04 09 19 8c 80 ....of.......... +| 2640: 80 80 80 09 03 00 38 00 00 00 14 03 32 66 74 03 ......8.....2ft. +| 2656: 02 02 01 02 69 73 03 06 01 01 03 04 07 18 8c 80 ....is.......... +| 2672: 80 80 80 08 03 00 36 00 00 00 13 02 31 74 03 08 ......6.....1t.. +| 2688: 03 01 01 04 01 01 77 03 02 04 04 09 1a 8c 80 80 ......w......... +| 2704: 80 80 07 03 00 3a ff 00 00 15 02 31 6e 03 08 01 .....:.....1n... +| 2720: 01 02 05 01 01 6f 03 06 01 01 06 04 09 18 8c 80 .....o.......... +| 2736: 80 80 80 06 03 00 36 00 00 00 13 04 02 31 66 03 ......6......1f. +| 2752: 02 02 01 01 69 03 06 01 01 03 05 06 1c 8c 80 80 ....i........... +| 2768: 80 80 05 03 00 3e 00 00 00 17 04 30 74 68 65 03 .....>.....0the. +| 2784: 06 01 01 04 01 05 77 68 65 72 65 03 02 04 0a 15 ......where..... +| 2800: 8c 80 80 80 80 04 03 00 30 00 00 00 11 01 01 06 ........0....... +| 2816: 06 30 74 61 62 6c cc 03 02 03 07 1c 8c 80 80 80 .0tabl.......... +| 2832: 80 03 03 00 3e 00 00 00 17 07 30 6e 75 6d 62 65 ....>.....0numbe +| 2848: 72 03 06 01 01 05 01 02 6f 66 03 06 04 0d 13 8c r.......of...... +| 2864: 80 80 80 80 02 03 00 2c 00 00 00 0f 01 01 03 02 .......,........ +| 2880: 30 6e 03 06 01 01 02 07 1b 8c 80 80 80 80 01 03 0n.............. +| 2896: 00 3c 00 00 00 16 08 30 66 74 73 34 61 75 78 03 .<.....0fts4aux. +| 2912: 02 02 01 02 69 73 03 06 04 0c 00 00 00 14 2a 00 ....is........*. +| 2928: 00 00 01 01 02 24 00 02 01 01 12 02 01 12 08 88 .....$.......... +| 2944: 80 80 80 80 12 03 00 16 00 00 00 05 02 1c 88 80 ................ +| 2960: 80 80 80 11 03 00 3e 00 00 00 17 05 34 72 6f 77 ......>.....4row +| 2976: 73 02 06 01 01 05 01 04 74 68 65 72 02 02 04 0b s.......ther.... +| 2992: 15 88 80 80 80 80 10 03 00 30 00 00 00 11 02 01 .........0...... +| 3008: 01 07 05 34 62 65 74 77 02 02 04 08 1b 88 80 80 ...4betw........ +| 3024: 80 80 0f 03 00 3c 00 00 00 16 04 04 33 72 6f 77 .....<......3row +| 3040: 02 06 01 01 05 01 03 74 68 65 02 08 05 0a 1b 88 .......the...... +| 3056: 80 80 80 80 0e 03 05 0c 00 00 00 16 01 01 02 04 ................ +| 3072: 33 61 72 65 02 02 03 01 03 62 65 74 02 02 07 08 3are.....bet.... +| 3088: 1b 88 80 80 80 80 0d 03 00 3c 00 00 00 16 03 32 .........<.....2 +| 3104: 84 68 02 08 02 01 01 07 00 04 33 61 6e 64 02 06 .h........3and.. +| 3120: 04 0a 1b 88 80 80 80 80 0c 03 00 3c 00 00 00 16 ...........<.... +| 3136: 03 32 69 6e 02 06 01 01 06 01 02 72 6f 02 06 01 .2in.......ro... +| 3152: 01 05 04 09 18 88 80 80 80 80 0b 03 00 36 00 00 .............6.. +| 3168: 00 13 02 03 32 61 72 02 02 03 01 02 62 65 02 02 ....2ar.....be.. +| 3184: 04 05 07 1b 88 80 80 80 80 0a 03 00 3c 00 00 00 ............<... +| 3200: 16 02 31 74 02 08 02 01 01 07 00 03 32 61 6e 02 ..1t........2an. +| 3216: 06 01 01 04 09 19 88 80 80 80 80 09 03 00 38 00 ..............8. +| 3232: 00 00 14 02 31 6e 02 06 01 01 03 01 01 72 02 06 ....1n.......r.. +| 3248: 01 01 05 04 08 17 88 80 80 80 80 08 03 00 34 00 ..............4. +| 3264: 00 00 12 02 31 62 02 02 04 01 01 69 02 06 01 01 ....1b.....i.... +| 3280: 06 04 06 19 88 80 80 80 80 07 03 00 38 00 00 00 ............8... +| 3296: 14 04 02 31 32 02 02 05 01 01 61 02 08 03 01 01 ...12.....a..... +| 3312: 02 05 06 1b 88 80 80 80 80 06 03 00 3c 00 00 00 ............<... +| 3328: 16 06 30 74 68 65 72 65 02 12 02 00 02 31 31 02 ..0there.....11. +| 3344: 06 11 01 04 0a 15 88 80 80 80 80 05 03 00 30 00 ..............0. +| 3360: 00 00 11 01 01 05 04 30 74 68 65 02 06 01 01 07 .......0the..... +| 3376: 07 1c 88 80 80 80 80 04 03 00 3e 00 00 00 17 01 ..........>..... +| 3392: 01 06 02 30 6e 02 06 01 01 03 01 04 72 6f 77 73 ...0n.......rows +| 3408: 02 06 07 08 1b 88 80 80 80 80 03 03 00 3c 00 00 .............<.. +| 3424: 00 16 08 30 62 65 74 77 65 65 6e 02 02 04 01 02 ...0between..... +| 3440: 69 6e 02 06 04 0c 1a 88 80 80 80 80 02 03 00 3a in.............: +| 3456: 08 f0 00 15 04 30 61 6e 64 02 06 01 01 02 02 02 .....0and....... +| 3472: 72 65 02 02 03 04 0a 17 88 80 80 80 80 01 03 00 re.............. +| 3488: 34 00 00 00 12 02 30 31 02 06 01 01 04 01 01 32 4.....01.......2 +| 3504: 02 02 06 04 08 08 84 80 80 80 80 12 03 00 16 00 ................ +| 3520: 00 00 05 04 1b 84 80 80 80 80 11 03 00 3c 00 00 .............<.. +| 3536: 00 16 05 34 74 61 62 6c 01 06 01 01 05 02 03 65 ...4tabl.......e +| 3552: 72 6d 01 02 04 0b 1b 84 80 80 80 80 10 03 00 3c rm.............< +| 3568: 00 00 00 16 05 34 65 61 63 68 01 02 03 01 04 70 .....4each.....p +| 3584: 72 65 73 01 02 05 04 09 1a 84 80 80 80 80 0f 03 res............. +| 3600: 00 3a 00 00 00 15 04 33 74 65 72 01 02 04 02 02 .:.....3ter..... +| 3616: 68 65 01 06 01 01 03 04 08 1b 84 80 80 80 80 0e he.............. +| 3632: 03 00 3c 00 00 00 16 04 33 70 72 65 01 02 05 01 ..<.....3pre.... +| 3648: 03 74 61 62 01 06 01 01 05 04 08 1a 84 80 80 80 .tab............ +| 3664: 80 0d 03 00 3a 00 00 00 15 04 33 66 6f 72 01 02 ....:.....3for.. +| 3680: 02 02 02 74 73 01 06 01 01 04 04 08 1b 84 80 80 ...ts........... +| 3696: 80 80 0c 03 00 3c 00 00 00 16 03 32 74 68 01 06 .....<.....2th.. +| 3712: 01 01 03 00 04 33 65 61 63 01 02 03 04 09 18 84 .....3eac....... +| 3728: 80 80 80 80 0b 03 00 36 00 00 00 13 03 32 74 61 .......6.....2ta +| 3744: 01 06 01 01 05 02 01 65 00 02 04 04 09 19 84 80 .......e........ +| 3760: 80 80 80 0a 03 00 38 00 00 00 14 03 32 69 6e 01 ......8.....2in. +| 3776: 06 01 01 02 01 02 70 72 01 02 05 04 09 18 84 80 ......pr........ +| 3792: 80 80 80 09 03 00 36 00 00 00 13 03 32 66 6f 01 ......6.....2fo. +| 3808: 02 02 02 01 74 01 06 01 01 04 04 07 1b 84 80 80 ....t........... +| 3824: 80 80 08 03 00 3c 00 00 00 16 02 31 74 01 0a 04 .....<.....1t... +| 3840: 01 01 03 04 00 03 32 65 61 01 02 03 04 0a 17 84 ......2ea....... +| 3856: 80 80 80 80 07 03 00 34 00 00 00 12 02 31 69 01 .......4.....1i. +| 3872: 06 01 01 02 de 01 70 01 02 05 04 08 18 84 80 80 ......p......... +| 3888: 80 80 06 03 00 36 00 00 00 13 02 31 65 01 02 03 .....6.....1e... +| 3904: 01 01 66 01 08 02 01 01 04 04 06 1b 84 80 80 80 ..f............. +| 3920: 80 05 03 00 3c 00 00 00 16 05 30 74 65 72 6d 01 ....<.....0term. +| 3936: 02 04 02 02 68 65 01 06 01 01 03 04 09 14 84 80 ....he.......... +| 3952: 80 80 80 04 03 00 2e 00 00 00 10 06 30 74 61 62 ............0tab +| 3968: 6c 65 01 06 01 01 05 04 15 84 80 80 80 80 03 03 le.............. +| 3984: 00 30 00 00 00 11 02 08 30 70 72 65 73 65 6e 74 .0......0present +| 4000: 01 02 05 05 1b 84 80 80 80 80 02 03 00 3c 00 00 .............<.. +| 4016: 00 16 04 30 66 74 73 01 06 01 01 04 01 02 69 6e ...0fts.......in +| 4032: 01 06 01 01 04 0a 1a 84 80 80 80 80 01 03 00 3a ...............: +| 4048: 00 00 00 15 05 30 65 61 63 68 00 f2 03 01 03 66 .....0each.....f +| 4064: 6f 72 01 02 02 04 09 06 01 03 00 12 03 0b 0f 00 or.............. +| 4080: 00 08 8c 80 80 80 80 11 03 00 16 00 00 00 05 04 ................ +| page 3 offset 8192 +| 0: 0a 00 00 00 32 0e 4f 00 0f fa 0f f1 0f e9 0f e1 ....2.O......... +| 16: 0f d8 0f d1 0f c9 0f c1 0f b9 0f b1 0f a9 0f a0 ................ +| 32: 0f 98 0f 90 0f 87 0f 80 0f 78 0f 71 0f 68 0f 5f .........x.q.h._ +| 48: 0f 56 0f 4d 0f 41 0f 38 0f 2f 0f 26 0f 1d 0f 13 .V.M.A.8./.&.... +| 64: 0f 0a 0f 01 0e f7 0e ee 0e e6 0e dd 0e d6 0e cd ................ +| 80: 0e c3 0e ba 0e b0 0e a8 0e 9f 0e 96 0e 8e 0e 85 ................ +| 96: 0e 7c 0e 73 0e 6a 0e 60 0e 58 0e 4f 00 00 00 00 .|.s.j.`.X.O.... +| 3648: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 08 ................ +| 3664: 04 01 10 01 03 34 74 20 07 04 01 0e 01 03 34 1e .....4t ......4. +| 3680: 09 04 01 12 01 03 33 74 68 1c 08 04 01 10 01 03 ......3th....... +| 3696: 33 6e 1a 08 04 01 10 01 03 32 77 18 08 04 01 10 3n.......2w..... +| 3712: 01 03 32 74 16 08 04 01 10 01 03 32 6e 14 07 04 ..2t.......2n... +| 3728: 01 0e 01 03 32 12 08 04 01 10 01 03 31 74 10 08 ....2.......1t.. +| 3744: 04 01 10 01 03 31 6e 0e 07 04 01 0e 01 03 31 0c .....1n.......1. +| 3760: 09 04 01 12 01 03 30 74 68 0a 08 04 01 10 01 03 ......0th....... +| 3776: 30 74 08 09 04 01 12 01 03 30 6e 75 06 08 04 01 0t.......0nu.... +| 3792: 10 01 03 30 6e 04 06 04 01 0c 01 05 52 08 04 01 ...0n.......R... +| 3808: 10 01 02 34 72 22 07 04 01 0e 01 02 34 20 08 04 ...4r.......4 .. +| 3824: 01 10 01 02 33 72 1e 09 04 01 12 01 02 33 61 72 ....3r.......3ar +| 3840: 1c 08 04 01 10 01 02 32 74 1a 08 04 01 10 b3 02 .......2t....... +| 3856: 32 69 18 09 04 01 12 01 02 32 61 72 16 08 04 01 2i.......2ar.... +| 3872: 10 01 02 31 74 14 08 04 01 10 01 02 31 6e 12 08 ...1t.......1n.. +| 3888: 04 01 10 01 02 31 62 10 08 04 01 10 01 02 31 32 .....1b.......12 +| 3904: 0e 0b 04 01 16 01 02 30 74 68 65 72 0c 08 04 01 .......0ther.... +| 3920: 10 01 02 30 74 0a 08 04 01 10 01 02 30 6e 08 08 ...0t.......0n.. +| 3936: 04 01 10 01 02 30 62 06 08 04 01 10 01 02 30 61 .....0b.......0a +| 3952: 04 06 04 01 0c 01 02 02 07 04 09 10 01 34 74 22 .............4t. +| 3968: 06 04 09 0e 01 34 20 08 04 09 12 01 33 74 65 1e .....4 .....3te. +| 3984: 07 04 09 10 01 33 70 1c 07 f4 09 11 01 33 66 1a .....3p......3f. +| 4000: 08 04 09 12 01 32 74 68 18 07 04 09 10 01 32 e4 .....2th......2. +| 4016: 16 07 04 09 10 01 32 69 14 07 04 09 10 01 32 66 ......2i......2f +| 4032: 12 07 04 09 10 01 31 74 10 07 04 09 10 01 31 69 ......1t......1i +| 4048: 0e 06 04 09 0e 01 31 0c 08 04 09 12 01 30 74 65 ......1......0te +| 4064: 0a 07 04 09 10 01 30 74 08 07 04 09 10 01 30 70 ......0t......0p +| 4080: 06 08 04 09 12 01 30 66 74 04 05 04 09 0c 01 02 ......0ft....... +| page 4 offset 12288 +| 0: 0d 00 00 00 03 0f eb 00 0f f9 0f f2 0f eb 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 05 03 03 00 10 ................ +| 4080: 03 05 05 02 03 00 10 04 06 05 01 03 00 10 04 04 ................ +| page 5 offset 16384 +| 0: 0a 00 00 00 02 0f eb 00 0f eb 0f f4 00 00 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 08 03 15 01 70 ...............p +| 4080: 67 73 7a 18 0b 03 1b 01 76 65 72 73 69 6f 6e 04 gsz.....version. +| page 6 offset 20480 +| 0: 0d 00 00 00 03 0f f2 00 0f fc 0f f7 0f f2 00 00 ................ +| 4080: 00 00 23 03 02 01 03 03 02 02 01 02 02 00 f2 09 ..#............. +| end crash-03a1855566d9ae.db +}]} {} + +do_catchsql_test 54.1 { + SELECT rowid==-1 FROM t1('t*'); +} {/*malformed database schema*/} + +#------------------------------------------------------------------------- +reset_db +do_test 55.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 32768 pagesize 4096 filename crash-b366b5ac0d3887.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 08 .....@ ........ +| 32: 00 00 00 02 00 00 00 05 90 00 00 09 00 00 00 04 ................ +| 96: 00 00 00 00 0d 0f c7 00 07 0d 92 00 0f 8d 0f 36 ...............6 +| 112: 0e cb 0e 6b 0e 0e 0d b6 0d 92 0d 92 00 00 00 00 ...k............ +| 3472: 00 00 22 08 06 17 11 11 01 31 74 61 62 6c 65 74 .........1tablet +| 3488: 32 74 32 08 43 52 45 41 54 45 20 54 41 42 4c 45 2t2.CREATE TABLE +| 3504: 20 74 32 28 78 29 56 07 06 17 1f 1f 01 7d 74 61 t2(x)V.......ta +| 3520: 62 6c 65 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 blet1_configt1_c +| 3536: 6f 6e 66 69 67 07 43 52 45 41 54 45 20 54 41 42 onfig.CREATE TAB +| 3552: 4c 45 20 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b LE 't1_config'(k +| 3568: 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 PRIMARY KEY, v) +| 3584: 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 5b 06 WITHOUT ROWID[. +| 3600: 07 17 21 21 01 81 01 74 61 62 6c 65 74 31 5f 64 ..!!...tablet1_d +| 3616: 6f 63 73 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 ocsizet1_docsize +| 3632: 06 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 .CREATE TABLE 't +| 3648: 31 5f 64 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 1_docsize'(id IN +| 3664: 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE +| 3680: 59 2c 20 73 7a 20 43 4c 4f 42 29 5e 05 07 17 21 Y, sz CLOB)^...! +| 3696: 21 01 81 07 74 61 62 6c 65 74 31 5f 63 6f 6e 74 !...tablet1_cont +| 3712: 65 6e 74 74 31 5f 63 6f 6e 74 65 6e 74 05 43 52 entt1_content.CR +| 3728: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 EATE TABLE 't1_c +| 3744: 6f 6e 74 65 6e 74 27 28 69 64 20 49 4e 54 45 47 ontent'(id INTEG +| 3760: 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 ER PRIMARY KEY, +| 3776: 63 30 2c 20 63 31 2c 20 63 32 29 69 04 07 17 19 c0, c1, c2)i.... +| 3792: 19 01 81 2d 74 61 62 6c 65 74 32 5f 69 64 78 74 ...-tablet2_idxt +| 3808: 31 5f 69 64 78 04 43 52 45 41 54 45 20 54 41 42 1_idx.CREATE TAB +| 3824: 4c 45 20 27 74 31 5f 69 64 78 27 28 73 65 67 69 LE 't1_idx'(segi +| 3840: 64 2c 20 74 65 72 6d 2c 20 70 67 6e 6f 2c 20 50 d, term, pgno, P +| 3856: 52 49 4d 41 52 59 20 4b 45 59 28 73 65 67 69 64 RIMARY KEY(segid +| 3872: 2c 20 74 65 72 6d 29 29 20 57 49 54 48 4f 55 54 , term)) WITHOUT +| 3888: 20 52 4f 57 49 44 55 03 07 17 1b 1b 01 81 01 74 ROWIDU........t +| 3904: 61 62 6c 65 74 31 5f 64 61 74 61 74 31 5f 64 61 ablet1_datat1_da +| 3920: 74 61 03 43 52 45 41 54 45 20 54 41 42 4c 45 20 ta.CREATE TABLE +| 3936: 27 74 31 5f 64 61 74 61 27 28 69 64 20 49 4e 54 't1_data'(id INT +| 3952: 45 47 45 52 20 50 52 49 4d 42 52 59 20 4b 45 59 EGER PRIMBRY KEY +| 3968: 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 29 38 02 06 , block BLOB)8.. +| 3984: 17 11 11 08 5f 74 61 62 6c 65 74 31 74 31 43 52 ...._tablet1t1CR +| 4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 35 LE t1 USING fts5 +| 4032: 28 61 2c 62 2c 63 29 00 00 00 39 00 00 00 00 00 (a,b,c)...9..... +| page 3 offset 8192 +| 0: 0d 00 00 00 03 0c 94 00 0f e6 0f ef 0c 94 00 00 ................ +| 3216: 00 00 00 00 86 4a 84 80 80 80 80 01 04 00 8d 18 .....J.......... +| 3232: 00 00 03 2b 02 30 30 01 02 06 01 02 06 01 02 06 ...+.00......... +| 3248: 1f 02 03 01 02 03 01 02 03 01 08 32 30 31 36 30 ...........20160 +| 3264: 36 30 39 01 02 07 01 02 07 01 02 07 01 01 34 01 609...........4. +| 3280: 02 05 01 02 05 01 02 05 01 01 35 01 02 04 01 02 ..........5..... +| 3296: 04 01 02 04 02 07 30 30 30 30 30 30 30 1c 02 04 ......0000000... +| 3312: 01 02 04 01 02 04 01 06 62 69 6e 61 72 79 03 06 ........binary.. +| 3328: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3344: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3360: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 ................ +| 3376: 03 06 01 02 02 03 06 01 02 02 01 08 63 6f 6d 70 ............comp +| 3392: 69 6c 65 72 01 02 02 01 02 02 f1 02 02 01 06 64 iler...........d +| 3408: 62 73 74 61 74 07 02 03 01 02 03 01 02 03 02 04 bstat........... +| 3424: 65 62 75 67 04 02 02 01 02 02 01 02 02 01 06 65 ebug...........e +| 3440: 6e 61 62 6c 65 07 02 02 01 02 02 01 02 02 01 02 nable........... +| 3456: 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 ................ +| 3472: 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 ................ +| 3488: 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 ................ +| 3504: 02 01 02 02 02 08 78 74 65 6e 73 69 6f 6e 1f 02 ......xtension.. +| 3520: 04 01 02 04 01 02 04 01 04 66 74 73 34 0a 02 03 .........fts4... +| 3536: 01 02 03 01 02 03 04 01 35 0d 02 03 01 02 03 01 ........5....... +| 3552: 02 03 01 03 67 63 63 01 02 03 01 02 03 01 02 03 ....gcc......... +| 3568: 02 06 65 6f 70 6f 6c 79 10 02 03 01 02 03 01 02 ..eopoly........ +| 3584: 03 01 05 6a 73 6f 6e 31 13 02 03 01 02 03 01 02 ...json1........ +| 3600: 03 01 04 6c 6f 61 64 1f 02 03 01 02 03 01 02 03 ...load......... +| 3616: 01 03 6d 61 78 1c 02 02 01 02 02 01 02 02 02 05 ..max........... +| 3632: 65 6d 6f 72 79 1c 02 03 01 02 03 01 02 03 04 04 emory........... +| 3648: 73 79 73 35 16 02 03 01 02 03 01 02 03 01 06 6e sys5...........n +| 3664: 6f 63 61 73 65 02 06 01 02 02 03 06 01 02 02 03 ocase........... +| 3680: 06 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 ................ +| 3696: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3712: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3728: 02 01 04 6f 6d 69 74 1f 02 02 01 02 02 01 02 02 ...omit......... +| 3744: 01 05 72 74 72 65 65 19 02 03 01 02 03 01 02 03 ..rtree......... +| 3760: 04 02 69 6d 01 06 01 02 02 03 06 01 02 02 03 06 ..im............ +| 3776: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3792: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3808: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 ................ +| 3824: 01 0a 74 68 72 65 61 64 73 61 66 65 22 02 02 01 ..threadsafe.... +| 3840: 02 02 01 02 02 01 04 76 74 61 62 07 02 04 01 02 .......vtab..... +| 3856: 04 01 02 04 01 01 78 01 06 01 01 02 01 06 01 01 ......x......... +| 3872: 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 ................ +| 3888: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 3904: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 3920: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 ................ +| 3936: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 ................ +| 3952: 02 01 06 01 01 02 01 06 01 01 02 08 d6 01 01 02 ................ +| 3968: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 3984: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 4000: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 ................ +| 4016: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 ................ +| 4032: 02 01 06 01 01 02 01 06 01 01 02 04 15 13 0c 0c ................ +| 4048: 12 44 13 11 0f 47 13 0f 0c 0e 11 10 0f 0e 10 0f .D...G.......... +| 4064: 44 0f 10 40 15 0f 07 01 03 00 14 24 5a 24 24 0f D..@.......$Z$$. +| 4080: 0a 03 00 24 00 00 00 00 01 01 01 00 01 01 01 01 ...$............ +| page 4 offset 12288 +| 0: 0a 00 00 00 01 0f fa 00 0f fa 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 05 04 09 0c 01 02 ................ +| page 5 offset 16384 +| 0: 0d 00 00 00 24 0c 0a 00 0f d8 0f af 0f 86 0f 74 ....$..........t +| 16: 0f 61 0f 4e 0f 2f 0f 0f 0e ef 0e d7 0e be 0e a5 .a.N./.......... +| 32: 0e 8d 0e 74 0e 5b 0e 40 0e 24 0e 08 0d ef 0d d5 ...t.[.@.$...... +| 48: 0d bb 0d a0 0d 84 0d 68 0d 4f 0d 35 0d 1b 0c fb .......h.O.5.... +| 64: 0c da 0c b9 0c 99 0c 78 0c 57 0c 3e 00 00 00 00 .......x.W.>.... +| 3072: 00 00 00 00 00 00 00 00 00 00 18 24 05 00 25 0f ...........$..%. +| 3088: 19 54 48 52 45 41 44 53 41 46 45 3d 30 58 42 49 .THREADSAFE=0XBI +| 3104: 4e 41 52 59 18 23 05 00 25 0f 19 54 48 52 45 41 NARY.#..%..THREA +| 3120: 44 53 41 46 45 3d 30 58 4e 4f 43 41 53 45 17 22 DSAFE=0XNOCASE.. +| 3136: 05 00 25 0f 17 54 48 52 45 41 44 53 41 46 45 3d ..%..THREADSAFE= +| 3152: 30 58 52 54 52 49 4d 1f 20 f5 00 33 0f 19 4f 4d 0XRTRIM. ..3..OM +| 3168: 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 4f IT LOAD EXTENSIO +| 3184: 4e 58 42 49 4e 41 52 59 1f 20 05 00 33 0f 19 4f NXBINARY. ..3..O +| 3200: 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 MIT LOAD EXTENSI +| 3216: 4f 4e 58 4e 4f 43 41 53 45 1e 1f 05 00 33 0f 17 ONXNOCASE....3.. +| 3232: 4f 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 OMIT LOAD EXTENS +| 3248: 49 4f 4e 58 52 54 52 49 4d 1f 1e 05 00 33 0f 19 IONXRTRIM....3.. +| 3264: 4c 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 30 LAX MEMORY=50000 +| 3280: 30 30 30 58 42 49 4e 41 52 59 1f 1d 05 00 33 0f 000XBINARY....3. +| 3296: 19 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 .MAX MEMORY=5000 +| 3312: 30 30 30 30 58 4e 4f 43 41 53 45 1e 1c 05 00 33 0000XNOCASE....3 +| 3328: 0f 17 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 ..MAX MEMORY=500 +| 3344: 30 30 30 30 30 58 52 54 52 49 4d 18 1b 05 00 25 00000XRTRIM....% +| 3360: 0f 19 45 4e 41 42 4c 45 20 52 54 52 45 45 58 42 ..ENABLE RTREEXB +| 3376: 49 4e 41 52 59 18 1a 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3392: 4c 45 20 52 54 52 45 45 58 4e 4f 43 41 53 45 17 LE RTREEXNOCASE. +| 3408: 19 05 00 25 0f 17 45 4e 41 42 4c 45 20 52 54 52 ...%..ENABLE RTR +| 3424: 45 45 58 52 54 52 49 4d 1a 18 05 00 29 0f 19 45 EEXRTRIM....)..E +| 3440: 4e 41 42 4c 45 20 4d 45 4d 53 59 53 35 58 42 49 NABLE MEMSYS5XBI +| 3456: 4e 41 52 59 1a 17 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3472: 45 20 4d 45 4d 53 59 53 35 58 4e 4f 43 41 53 45 E MEMSYS5XNOCASE +| 3488: 19 16 05 00 29 0f 17 45 4e 41 42 4c 45 20 4d 45 ....)..ENABLE ME +| 3504: 4d 53 59 53 35 58 52 54 52 49 4d 18 15 05 00 25 MSYS5XRTRIM....% +| 3520: 0f 19 45 4e 41 42 4c 45 20 4a 53 4f 4e 31 58 42 ..ENABLE JSON1XB +| 3536: 49 4e 41 52 59 18 14 05 00 25 0f 30 45 4e 41 42 INARY....%.0ENAB +| 3552: 4c 45 20 4a 53 4f 4e 31 58 4e 4f 43 41 53 45 17 LE JSON1XNOCASE. +| 3568: 13 05 00 25 0f 17 45 4e 41 42 4c 45 20 4a 53 4f ...%..ENABLE JSO +| 3584: 4e 31 58 52 54 52 49 4d 1a 12 05 00 29 0f 19 45 N1XRTRIM....)..E +| 3600: 4e 41 42 4c 45 20 47 45 4f 50 4f 4c 59 58 42 49 NABLE GEOPOLYXBI +| 3616: 4e 41 52 59 1a 11 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3632: 45 20 47 45 4f 50 4f 4c 59 58 4e 4f 43 41 53 45 E GEOPOLYXNOCASE +| 3648: 19 10 05 00 29 0f 17 45 4e 41 42 4c 45 20 47 45 ....)..ENABLE GE +| 3664: 4f 50 4f 4c 59 58 52 54 52 49 4d 17 0f 05 00 23 OPOLYXRTRIM....# +| 3680: 0f 19 45 4e 41 42 4c 45 20 46 54 53 35 58 42 49 ..ENABLE FTS5XBI +| 3696: 4e 41 52 59 17 0e 05 00 23 0f 19 45 4e 41 42 4c NARY....#..ENABL +| 3712: 45 20 46 54 53 35 58 4e 4f 43 41 53 45 16 0d 05 E FTS5XNOCASE... +| 3728: 00 23 0f 17 45 4f 41 42 4c 45 20 46 54 53 35 58 .#..EOABLE FTS5X +| 3744: 52 54 52 49 4d 17 0c 05 00 23 0f 19 45 4e 41 42 RTRIM....#..ENAB +| 3760: 4c 45 20 46 54 53 34 58 42 49 4e 41 52 59 17 0b LE FTS4XBINARY.. +| 3776: 05 00 23 0f 19 45 4e 41 42 4c 45 20 46 54 53 34 ..#..ENABLE FTS4 +| 3792: 58 4e 4f 43 41 53 45 16 0a 05 00 23 0f 17 45 4e XNOCASE....#..EN +| 3808: 41 42 4c 45 20 46 54 53 34 58 52 54 52 49 4d 1e ABLE FTS4XRTRIM. +| 3824: 09 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3840: 54 41 54 20 56 54 31 42 58 42 49 4e 41 52 59 1e TAT VT1BXBINARY. +| 3856: 08 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3872: 54 41 54 20 56 54 41 42 58 4e 4f 43 41 53 45 1d TAT VTABXNOCASE. +| 3888: 07 05 00 31 0f 17 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3904: 54 41 54 20 56 54 41 42 58 52 54 52 49 4d 11 06 TAT VTABXRTRIM.. +| 3920: 05 00 17 0f 19 44 45 42 55 47 58 42 49 4e 41 52 .....DEBUGXBINAR +| 3936: 59 11 15 05 00 17 0f 19 44 45 42 55 47 58 4e 4f Y.......DEBUGXNO +| 3952: 43 41 53 45 10 04 05 00 17 0f 17 44 45 42 55 47 CASE.......DEBUG +| 3968: 58 42 54 52 49 4d 27 03 05 00 43 0f 19 43 4f 4d XBTRIM'...C..COM +| 3984: 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e 30 20 PILER=gcc-5.4.0 +| 4000: 32 30 31 36 30 36 30 39 58 42 49 4e 41 52 59 27 20160609XBINARY' +| 4016: 02 05 00 43 0f 19 43 4f 4d 50 49 4c 45 52 3d 67 ...C..COMPILER=g +| 4032: 63 63 2d 35 2e 34 2e 30 20 32 30 31 36 30 36 30 cc-5.4.0 2016060 +| 4048: 39 58 4e 4f 43 41 53 45 26 01 05 00 43 0f 17 43 9XNOCASE&...C..C +| 4064: 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e OMPILER=gcc-5.4. +| 4080: 30 20 32 30 31 36 30 36 30 39 58 52 54 52 49 4d 0 20160609XRTRIM +| page 6 offset 20480 +| 0: 0d 00 00 00 24 0e e0 00 0f f8 0f f0 0f e8 0f e0 ....$........... +| 16: 0f d8 0f d0 0f c8 0f c0 0f b8 0f b0 0f a8 0f a0 ................ +| 32: 0f 98 0f 90 0f 88 0f 80 0f 78 0f 70 1f 68 0f 60 .........x.p.h.` +| 48: 0f 58 0f 50 0f 48 0f 40 0f 38 0f 30 0f 28 0f 20 .X.P.H.@.8.0.(. +| 64: 0f 18 0f 10 0f 08 0f 00 0e f8 0e f0 0e e8 0e e0 ................ +| 3808: 07 24 03 00 12 02 01 01 06 23 03 00 12 02 01 01 .$.......#...... +| 3824: 06 22 03 00 12 02 01 01 06 21 03 00 12 03 01 01 .........!...... +| 3840: 06 20 03 00 12 03 01 01 06 1f 03 00 12 03 01 01 . .............. +| 3856: 06 1e 03 00 12 03 01 01 06 1d 03 00 12 03 01 01 ................ +| 3872: 06 1c 03 00 12 03 01 01 06 1b 03 00 12 02 01 01 ................ +| 3888: 06 1a 03 00 12 02 01 01 06 19 03 00 12 02 01 01 ................ +| 3904: 06 18 03 00 12 02 01 01 06 17 03 00 12 02 01 01 ................ +| 3920: 06 16 03 00 12 02 01 01 06 15 03 00 12 02 01 01 ................ +| 3936: 06 14 03 00 12 02 01 01 06 13 03 00 12 02 01 01 ................ +| 3952: 06 12 03 00 12 02 01 01 06 11 03 00 12 02 01 01 ................ +| 3968: 06 10 03 00 12 02 01 01 06 0f 03 00 12 02 01 01 ................ +| 3984: 06 0e 03 00 12 02 01 01 06 0d 03 00 12 02 01 01 ................ +| 4000: 06 0c 03 00 12 02 01 01 06 0b 03 00 12 02 01 01 ................ +| 4016: 06 0a 03 00 12 02 01 01 05 09 03 00 12 03 01 01 ................ +| 4032: 06 08 03 00 12 03 01 01 01 17 03 00 12 03 01 01 ................ +| 4048: 06 06 03 00 12 01 01 01 06 05 03 00 12 01 01 01 ................ +| 4064: 06 04 03 00 12 01 01 01 06 03 02 f0 12 06 01 01 ................ +| 4080: 06 02 03 00 13 06 01 01 06 01 03 00 12 06 01 01 ................ +| page 7 offset 24576 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| page 8 offset 28672 +| 0: 0d 00 00 00 03 0f d6 00 0f f4 0f e9 0f d6 00 00 ................ +| 4048: 00 00 00 00 00 00 11 03 02 2b 69 6e 74 65 77 72 .........+intewr +| 4064: 69 74 79 2d 63 68 65 63 6b 09 02 02 1b 72 65 62 ity-check....reb +| 4080: 75 69 6c 64 0a 01 02 1d 6f 70 74 69 6d 69 7a 65 uild....optimize +| end crash-b366b5ac0d3887.db +}]} {} + +do_catchsql_test 55.1 { + SAVEPOINT one; + DELETE FROM t1 WHERE a MATCH 'ts'; +} {/*malformed database schema*/} + +do_execsql_test 55.2 { + ROLLBACK TO one; +} + +#------------------------------------------------------------------------- +reset_db +do_test 56.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 24576 pagesize 4096 filename crash-2acc487d09f033.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 06 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 06 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 96: 00 00 00 00 0d 00 00 00 06 0e 0f 00 0f aa 0f 53 ...............S +| 112: 0e e8 0e 8b 0e 33 0e 0f 00 00 00 00 00 00 00 00 .....3.......... +| 3584: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 22 ................ +| 3600: 06 06 17 11 11 01 31 74 61 62 6c 65 62 62 62 62 ......1tablebbbb +| 3616: 06 43 52 45 41 54 45 20 54 41 42 4c 45 20 62 62 .CREATE TABLE bb +| 3632: 28 61 29 56 05 06 17 1f 1f 01 7d 74 61 62 6c 65 (a)V.......table +| 3648: 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 6f 6e 66 t1_configt1_conf +| 3664: 69 67 05 43 52 45 41 54 45 20 54 41 42 4c 45 20 ig.CREATE TABLE +| 3680: 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b 20 50 52 't1_config'(k PR +| 3696: 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 20 57 49 IMARY KEY, v) WI +| 3712: 54 48 4f 55 54 20 52 4f 57 49 44 5b 04 07 17 21 THOUT ROWID[...! +| 3728: 21 01 81 01 74 61 62 6c 65 74 31 5f 64 6f 63 73 !...tablet1_docs +| 3744: 69 7a 65 74 31 4f 64 6f 63 73 69 7a 65 04 43 52 izet1Odocsize.CR +| 3760: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 64 EATE TABLE 't1_d +| 3776: 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 54 45 47 ocsize'(id INTEG +| 3792: 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 ER PRIMARY KEY, +| 3808: 73 7a 20 42 4c 4f 42 29 69 03 07 17 19 19 01 81 sz BLOB)i....... +| 3824: 2d 74 61 62 6c 65 74 31 5f 69 64 78 74 31 5f 69 -tablet1_idxt1_i +| 3840: 64 78 03 43 52 45 41 54 45 20 54 41 42 4c 45 20 dx.CREATE TABLE +| 3856: 27 74 31 5f 69 64 78 27 28 73 65 67 69 64 2c 20 't1_idx'(segid, +| 3872: 74 65 72 6d 2c 20 70 67 6e 6f 2c 20 50 52 49 4d term, pgno, PRIM +| 3888: 41 52 59 20 4b 45 59 28 73 65 67 69 64 2c 20 74 ARY KEY(segid, t +| 3904: 65 72 6d 29 29 20 57 49 54 48 4f 55 54 20 52 4f erm)) WITHOUT RO +| 3920: 57 49 44 55 02 07 17 1b 1b 01 81 01 74 61 62 6c WIDU........tabl +| 3936: 65 74 31 5f 64 61 74 61 74 31 5f 64 61 74 61 02 et1_datat1_data. +| 3952: 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 CREATE TABLE 't1 +| 3968: 5f 64 61 74 61 27 28 69 64 20 49 4e 54 45 47 45 _data'(id INTEGE +| 3984: 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 62 R PRIMARY KEY, b +| 4000: 6c 6f 63 6b 20 42 4c 4f 42 29 54 01 07 17 11 11 lock BLOB)T..... +| 4016: 08 81 15 74 61 62 6c 65 74 31 74 31 43 52 45 41 ...tablet1t1CREA +| 4032: 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45 TE VIRTUAL TABLE +| 4048: 20 74 31 20 55 53 49 4e 47 20 66 74 73 35 28 61 t1 USING fts5(a +| 4064: 2c 62 2c 70 72 65 66 69 78 3d 22 32 2c 32 2c 33 ,b,prefix=.2,2,3 +| 4080: 2c 34 22 2c 20 63 6f 6e 74 65 6e 74 3d 22 22 29 ,4., content=..) +| page 2 offset 4096 +| 0: 0d 0b 6a 00 37 09 4c 02 0f e7 09 4c 0f c6 0f a4 ..j.7.L....L.... +| 16: 0f 88 0f 6d 0f 4b 0f 2c 0f 0e 0e ec 0e cd 0e ad ...m.K.,........ +| 32: 0e 8e 0e 6c 0e 4b 0e 29 0e 08 0d e6 0d c4 0d b5 ...l.K.)........ +| 48: 0d 97 0d 76 0d 54 0d 31 0d 15 0c f3 0c d3 0c b5 ...v.T.1........ +| 64: 0c 95 0c 73 0c 54 0c 32 0c 10 0b ee 0b cc 0b b0 ...s.T.2........ +| 80: 0b 8d 0b 7e 0b 48 0b 2e 0b 0b 0a ef 0a cc 0a ad ...~.H.......... +| 96: 0a 8c 0a 6d 0a 4d 0a 2b 0a 0c 09 ec 09 ca 09 a8 ...m.M.+........ +| 112: 09 86 09 63 0f f1 00 00 00 00 00 00 00 00 00 00 ...c............ +| 2368: 00 00 00 00 00 00 00 00 00 00 00 00 15 0a 03 00 ................ +| 2384: 30 00 00 00 01 01 03 35 00 03 01 01 12 02 01 12 0......5........ +| 2400: 03 01 11 1c 8c 80 80 80 80 10 03 00 3e 00 00 00 ............>... +| 2416: 17 01 05 05 34 74 61 62 6c 03 02 03 01 04 77 68 ....4tabl.....wh +| 2432: 65 72 03 02 06 09 1b 8c 80 80 80 80 0f 03 00 3c er.............< +| 2448: 00 00 00 16 05 34 66 74 73 34 03 02 02 01 04 6e .....4fts4.....n +| 2464: 75 6d 62 03 06 01 04 09 1b 8c 80 80 80 80 0e 03 umb............. +| 2480: 00 3c 00 00 00 16 04 33 74 68 65 03 06 01 01 04 .<.....3the..... +| 2496: 01 03 77 68 65 03 02 04 04 0a 1b 8c 80 80 80 80 ..whe........... +| 2512: 0d 03 00 3c 00 00 00 16 04 33 6e 75 6d 03 06 01 ...<.....3num... +| 2528: 01 05 01 03 74 61 62 03 02 03 04 0a 19 8c 80 80 ....tab......... +| 2544: 80 80 0c 03 00 38 00 00 00 14 03 32 77 68 03 02 .....8.....2wh.. +| 2560: 04 00 04 33 66 74 73 03 02 02 04 07 18 8c 80 80 ...3fts......... +| 2576: 80 80 0b 03 00 36 00 00 00 13 03 32 74 61 03 02 .....6.....2ta.. +| 2592: 03 02 01 68 03 06 01 01 04 04 07 1b 8c 80 80 80 ...h............ +| 2608: 80 09 03 00 3c 00 00 00 16 03 32 6e 75 03 06 01 ....<.....2nu... +| 2624: 01 05 01 02 6f 66 03 3b 01 01 06 04 09 19 8c 80 ....of.;........ +| 2640: 80 80 80 09 03 00 38 00 00 00 14 03 32 66 74 03 ......8.....2ft. +| 2656: 02 02 01 02 69 73 03 06 01 01 03 04 07 18 8c 80 ....is.......... +| 2672: 80 80 80 08 03 00 36 00 00 00 13 02 31 74 03 08 ......6.....1t.. +| 2688: 03 01 01 04 01 01 77 03 02 04 04 09 1a 8c 80 80 ......w......... +| 2704: 80 80 07 03 00 3a ff 00 00 15 02 31 6e 03 08 01 .....:.....1n... +| 2720: 01 02 05 01 01 6f 03 06 01 01 06 04 09 18 8c 80 .....o.......... +| 2736: 80 80 80 06 03 00 36 00 00 00 13 04 02 31 66 03 ......6......1f. +| 2752: 02 02 01 01 69 03 06 01 01 03 05 06 1c 8c 80 80 ....i........... +| 2768: 80 80 05 03 00 3e 00 00 00 17 04 30 74 68 65 03 .....>.....0the. +| 2784: 06 00 f1 04 01 05 77 68 65 72 65 03 02 04 0a 15 ......where..... +| 2800: 8c 80 80 80 80 04 03 00 30 00 00 00 11 01 01 06 ........0....... +| 2816: 06 30 74 61 62 6c 65 03 02 03 07 1c 8c 80 80 80 .0table......... +| 2832: 80 03 03 00 3e 00 00 00 17 07 30 6e 75 6d 62 65 ....>.....0numbe +| 2848: 72 03 06 01 01 05 01 02 6f 66 03 06 04 0d 13 8c r.......of...... +| 2864: 80 80 80 80 02 03 00 2c 00 00 00 0f 01 01 03 02 .......,........ +| 2880: 30 6e 03 06 01 01 02 07 1b 8c 80 80 80 80 01 03 0n.............. +| 2896: 00 3c 00 00 00 16 08 30 66 74 73 34 61 75 78 03 .<.....0fts4aux. +| 2912: 02 02 01 02 69 73 03 06 04 0c 00 00 00 14 2a 00 ....is........*. +| 2928: 00 00 01 01 02 24 00 02 01 01 12 02 01 12 08 88 .....$.......... +| 2944: 80 80 80 80 12 03 00 16 00 00 00 05 02 1c 88 80 ................ +| 2960: 80 80 80 11 03 00 3e 00 00 00 17 05 34 72 6f 77 ......>.....4row +| 2976: 73 02 06 01 01 05 01 04 74 68 65 72 02 02 04 0b s.......ther.... +| 2992: 15 88 80 80 80 80 10 03 00 30 00 00 00 11 02 01 .........0...... +| 3008: 01 07 05 34 62 65 74 77 02 02 04 08 1b 88 80 80 ...4betw........ +| 3024: 80 80 0f 03 00 3c 00 00 00 16 04 04 33 72 6f 77 .....<......3row +| 3040: 02 06 01 01 05 01 03 74 68 65 02 08 05 0a 1b 88 .......the...... +| 3056: 80 80 80 80 0e 03 00 3c 00 00 00 16 01 01 02 04 .......<........ +| 3072: 33 61 72 65 02 02 03 01 03 62 65 74 02 02 07 08 3are.....bet.... +| 3088: 1b 88 80 80 80 80 0d 03 00 3c 00 00 00 16 03 32 .........<.....2 +| 3104: 74 68 02 08 02 01 01 07 00 04 33 61 6e 64 02 06 th........3and.. +| 3120: 04 0a 1b 88 80 80 80 80 0c 03 00 3c 00 00 00 16 ...........<.... +| 3136: 03 32 69 6e 02 06 01 01 06 01 02 72 6f 02 06 01 .2in.......ro... +| 3152: 01 05 04 09 18 88 80 80 80 80 0b 03 00 36 00 00 .............6.. +| 3168: 00 13 02 03 32 61 72 02 02 03 01 02 62 65 02 02 ....2ar.....be.. +| 3184: 04 05 07 1b 88 80 80 80 80 0a 03 00 3c 00 00 00 ............<... +| 3200: 16 02 31 74 02 08 02 01 01 07 00 03 32 61 6e 02 ..1t........2an. +| 3216: 06 01 01 04 09 19 88 80 80 80 80 09 03 00 38 00 ..............8. +| 3232: 00 00 14 02 31 6e 02 06 01 01 03 01 01 72 02 06 ....1n.......r.. +| 3248: 01 01 05 04 08 17 88 80 80 80 80 08 03 00 34 00 ..............4. +| 3264: 00 00 12 02 31 62 02 02 04 01 01 69 02 06 01 01 ....1b.....i.... +| 3280: 06 04 06 19 88 80 80 80 80 07 03 00 38 00 00 00 ............8... +| 3296: 14 04 02 31 32 02 02 05 01 01 61 02 08 03 01 01 ...12.....a..... +| 3312: 02 05 06 1b 88 80 80 80 80 06 03 00 3c 00 00 00 ............<... +| 3328: 16 06 30 74 68 65 72 65 02 02 02 00 02 31 31 02 ..0there.....11. +| 3344: 06 01 01 04 0a 15 88 80 80 80 80 05 03 00 30 00 ..............0. +| 3360: 00 00 11 01 01 05 04 30 74 68 65 02 06 01 01 07 .......0the..... +| 3376: 07 1c 88 80 80 80 80 04 03 00 3e 00 00 00 17 01 ..........>..... +| 3392: 01 06 02 30 6e 02 06 01 01 03 01 04 72 6f 77 73 ...0n.......rows +| 3408: 02 06 07 08 1b 88 80 80 80 80 03 03 00 3c 00 00 .............<.. +| 3424: 00 16 08 30 62 65 74 77 65 65 6e 02 02 04 01 02 ...0between..... +| 3440: 69 6e 02 06 04 0c 1a 88 80 80 80 80 02 03 00 3a in.............: +| 3456: 00 00 00 15 04 30 61 6e 64 02 06 01 01 02 02 02 .....0and....... +| 3472: 72 65 02 02 03 04 0a 17 88 80 80 80 80 01 03 00 re.............. +| 3488: 34 00 00 00 12 02 30 31 02 06 01 01 04 01 01 32 4.....01.......2 +| 3504: 02 02 05 04 08 08 84 80 80 80 81 12 03 00 16 00 ................ +| 3520: 00 00 05 04 1b 84 61 80 80 80 11 03 00 3c 00 00 ......a......<.. +| 3536: 00 16 05 34 74 61 62 6c 01 06 01 01 05 02 03 65 ...4tabl.......e +| 3552: 72 6d 01 02 04 0b 1b 84 80 80 80 80 10 03 00 3c rm.............< +| 3568: 00 00 00 16 05 34 65 61 63 68 01 02 03 01 04 70 .....4each.....p +| 3584: 72 65 73 01 02 05 04 09 1a 84 80 80 80 80 0f 03 res............. +| 3600: 00 3a 00 00 00 15 04 33 74 65 72 01 02 04 02 02 .:.....3ter..... +| 3616: 68 65 01 06 01 01 03 04 08 1b 84 80 80 80 80 0e he.............. +| 3632: 03 00 3c 00 00 00 16 04 33 70 72 65 01 02 05 01 ..<.....3pre.... +| 3648: 03 74 61 62 01 06 01 01 05 14 08 1a 84 80 80 80 .tab............ +| 3664: 80 0d 03 00 3a 00 00 00 15 04 33 66 6f 72 01 02 ....:.....3for.. +| 3680: 02 02 02 74 73 01 06 01 01 04 04 08 1b 84 80 80 ...ts........... +| 3696: 80 80 0c 03 00 3c 00 00 00 16 03 32 74 68 01 06 .....<.....2th.. +| 3712: 01 01 03 00 04 33 65 61 63 01 02 03 04 09 18 84 .....3eac....... +| 3728: 80 80 80 80 0b 03 00 36 00 00 00 13 03 32 74 71 .......6.....2tq +| 3744: 01 06 01 01 05 02 01 65 01 02 04 04 09 19 84 80 .......e........ +| 3760: 80 80 80 0a 03 00 38 00 00 00 14 03 32 69 6e 01 ......8.....2in. +| 3776: 06 01 01 02 01 02 70 72 01 02 05 0b 89 18 84 80 ......pr........ +| 3792: 80 80 80 09 03 00 36 00 00 00 13 03 32 66 6f 01 ......6.....2fo. +| 3808: 02 02 02 01 74 01 06 01 01 04 04 07 1b 84 80 80 ....t........... +| 3824: 80 80 08 03 00 3c 00 00 00 16 02 31 74 01 0a 04 .....<.....1t... +| 3840: 01 01 03 04 00 03 32 65 61 01 02 03 04 0a 17 84 ......2ea....... +| 3856: 80 80 80 80 07 03 00 34 00 00 00 12 02 31 69 01 .......4.....1i. +| 3872: 06 01 01 02 01 01 70 01 02 05 04 08 18 84 80 80 ......p......... +| 3888: 80 80 06 03 00 36 00 00 00 13 02 31 65 01 02 03 .....6.....1e... +| 3904: 01 01 66 01 08 02 01 01 04 04 06 1b 84 80 80 80 ..f............. +| 3920: 80 05 03 00 3c 00 00 00 16 05 30 74 65 72 6d 01 ....<.....0term. +| 3936: 02 04 02 02 68 65 01 06 01 01 03 04 09 14 84 80 ....he.......... +| 3952: 80 80 80 04 03 00 2e 00 00 00 10 06 30 74 61 62 ............0tab +| 3968: 6c 65 01 06 01 01 05 04 15 84 80 80 80 80 03 03 le.............. +| 3984: 00 30 00 00 00 11 02 08 30 70 72 65 73 65 6e 74 .0......0present +| 4000: 01 02 05 05 1b 84 80 80 80 80 02 03 00 3c 00 00 .............<.. +| 4016: 00 16 04 30 66 74 73 01 06 01 01 04 01 02 69 6e ...0fts.......in +| 4032: 01 06 01 01 04 0a 1a 84 80 80 80 80 01 03 00 3a ...............: +| 4048: 00 00 00 15 05 30 65 61 63 f4 01 02 03 01 03 66 .....0eac......f +| 4064: 6f 72 01 02 02 04 09 06 01 03 00 12 03 0b 0f 00 or.............. +| 4080: 00 08 8c 80 80 80 80 11 03 00 16 00 00 00 05 04 ................ +| page 3 offset 8192 +| 0: 0a 00 00 00 32 0e 4f 00 0f fa 0f f1 0f e9 0f e1 ....2.O......... +| 16: 0f d8 0f d1 0f c9 0f c1 0f b9 0f b1 0f a9 0f a0 ................ +| 32: 0f 98 0f 90 0f 87 0f 80 0f 78 0f 71 0f 68 0f 5f .........x.q.h._ +| 48: 0f 56 0f 4d 0f 41 0f 38 0f 2f 0f 26 0f 1d 0f 13 .V.M.A.8./.&.... +| 64: 0f 0a 0f 01 0e f7 0e ee 0e e6 0e dd 0e d6 0e cd ................ +| 80: 0e c3 0e ba 0e b0 0e a8 0e 9f 0e 96 0e 8e 0e 85 ................ +| 96: 0e 7c 0e 73 0e 6a 0e 60 0e 58 0e 4f 00 00 00 00 .|.s.j.`.X.O.... +| 3648: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 08 ................ +| 3664: 04 01 10 01 03 34 74 20 07 04 01 0e 01 03 34 1e .....4t ......4. +| 3680: 09 04 01 12 01 03 33 74 68 1c 08 04 01 10 01 03 ......3th....... +| 3696: 33 6e 1a 08 04 01 10 01 03 32 77 18 08 04 01 10 3n.......2w..... +| 3712: 01 03 32 74 16 08 04 01 10 01 03 32 6e 14 07 04 ..2t.......2n... +| 3728: 01 0e 01 03 32 12 08 04 01 10 01 03 31 74 10 08 ....2.......1t.. +| 3744: 04 01 10 01 03 31 6e 0e 07 04 01 0e 01 03 31 0c .....1n.......1. +| 3760: 09 04 01 12 01 03 30 74 68 0a 08 04 01 10 01 03 ......0th....... +| 3776: 30 74 08 09 04 01 12 01 03 30 6e 75 06 08 04 01 0t.......0nu.... +| 3792: 10 01 03 30 6e 04 06 04 01 0c 01 03 02 08 04 01 ...0n........... +| 3808: 10 01 02 34 72 22 07 04 01 0e 01 02 34 20 08 04 ...4r.......4 .. +| 3824: 01 10 01 02 33 72 1e 09 04 01 12 01 02 33 61 72 ....3r.......3ar +| 3840: 1c 08 04 01 10 01 02 32 74 1a 08 04 01 10 b3 02 .......2t....... +| 3856: 32 69 18 09 04 01 12 01 02 32 61 72 16 08 04 01 2i.......2ar.... +| 3872: 10 01 02 31 74 14 08 04 01 10 01 02 31 6e 12 08 ...1t.......1n.. +| 3888: 04 01 10 01 02 31 62 10 08 04 01 10 01 02 31 32 .....1b.......12 +| 3904: 0e 0b 04 01 16 01 02 30 74 68 65 72 0c 08 04 01 .......0ther.... +| 3920: 10 01 02 30 74 0a 08 04 01 10 01 02 30 6e 08 08 ...0t.......0n.. +| 3936: 04 01 10 01 02 30 62 06 08 04 01 10 01 02 30 61 .....0b.......0a +| 3952: 05 06 04 01 0c 01 02 02 07 04 09 10 01 34 74 22 .............4t. +| 3968: 06 04 09 0e 01 34 20 08 04 09 12 01 33 74 65 1e .....4 .....3te. +| 3984: 07 04 09 10 01 33 70 1c 07 04 09 10 01 33 66 1a .....3p......3f. +| 4000: 08 04 09 12 01 32 74 68 18 07 04 09 10 01 32 2d .....2th......2- +| 4016: 16 07 04 09 10 01 32 69 14 07 04 09 10 01 32 66 ......2i......2f +| 4032: 12 07 04 09 10 01 31 74 10 07 04 09 10 01 31 69 ......1t......1i +| 4048: 0e 06 04 09 0e 01 31 0c 08 04 09 12 01 30 74 65 ......1......0te +| 4064: 0a 07 04 09 10 01 30 74 08 07 04 09 10 01 30 70 ......0t......0p +| 4080: 06 08 04 09 12 01 30 66 74 04 05 04 09 0c 01 02 ......0ft....... +| page 4 offset 12288 +| 0: 0d 00 00 00 03 0f eb 00 0f f9 0f f2 0f eb 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 05 03 03 00 10 ................ +| 4080: 03 05 05 02 03 00 10 04 06 05 01 03 00 10 04 03 ................ +| page 5 offset 16384 +| 0: 0a 00 00 00 02 0f eb 00 0f eb 0f f4 00 00 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 08 03 15 01 70 ...............p +| 4080: 67 73 7a 18 0b 03 1b 01 76 65 72 73 69 6f 6e 04 gsz.....version. +| page 6 offset 20480 +| 0: 0d 00 00 00 03 0f f2 00 0f fc 0f f7 0f f2 00 00 ................ +| 4080: 00 00 03 03 02 01 03 03 02 02 01 02 02 01 02 09 ................ +| end crash-2acc487d09f033.db +}]} {} + +do_test 56.1 { + set res [catchsql { + INSERT INTO t1(b) VALUES(randomblob(250)); + INSERT INTO t1(b) VALUES(randomblob(250)); + }] + + # For some permutations - those that use the page-cache - this test + # may return SQLITE_CONSTRAINT instead of SQLITE_CORRUPT. This is because + # the corrupt db in the test over-reads the page buffer slightly, with + # different results depending on whether or not the page-cache is in use. + if {$res=="1 {constraint failed}"} { + set res "1 {database disk image is malformed}" + } + set res +} {/*malformed database schema*/} + +#------------------------------------------------------------------------- +reset_db +do_test 57.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 28672 pagesize 4096 filename x.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 01 00 00 00 07 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 ................ +| 96: 00 2e 34 20 0d 00 00 00 07 0d d2 00 0f c4 0f 6d ..4 ...........m +| 112: 0f 02 0e ab 0e 4e 0d f6 0d d2 00 00 00 00 00 00 .....N.......... +| 3536: 00 00 22 07 06 17 11 11 01 31 74 61 62 6c 65 74 .........1tablet +| 3552: 32 74 32 07 43 52 45 41 54 45 20 54 41 42 4c 45 2t2.CREATE TABLE +| 3568: 20 74 32 28 78 29 56 06 06 17 1f 1f 01 7d 74 61 t2(x)V.......ta +| 3584: 61 6b 65 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 aket1_configt1_c +| 3600: 6f 7e 66 69 67 06 43 52 45 41 54 45 20 54 41 42 o~fig.CREATE TAB +| 3616: 4c 45 20 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b LE 't1_config'(k +| 3632: 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 PRIMARY KEY, v) +| 3648: 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 5b 05 WITHOUT ROWID[. +| 3664: 07 17 21 21 01 81 01 74 61 62 6c 65 74 31 5f 64 ..!!...tablet1_d +| 3680: 6f 63 73 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 ocsizet1_docsize +| 3696: 05 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 .CREATE TABLE 't +| 3712: 31 5f 64 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 1_docsize'(id IN +| 3728: 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE +| 3744: 59 2c 20 73 7a 20 42 4c 4f 42 29 55 04 06 17 21 Y, sz BLOB)U...! +| 3760: 21 01 77 74 61 62 6c 65 74 31 5f 63 6f 6e 74 1d !.wtablet1_cont. +| 3776: 6e 74 74 31 5f 63 6f 6e 74 65 6e 74 04 43 52 45 ntt1_content.CRE +| 3792: 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 6f ATE TABLE 't1_co +| 3808: 6e 74 65 6e 74 27 28 69 64 20 49 4e 54 45 47 45 ntent'(id INTEGE +| 3824: 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 63 R PRIMARY KEY, c +| 3840: 30 29 69 03 07 17 19 19 01 81 2d 74 61 62 6c 65 0)i.......-table +| 3856: 74 31 5f 69 64 78 74 31 5f 59 64 78 03 43 52 45 t1_idxt1_Ydx.CRE +| 3872: 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 69 64 ATE TABLE 't1_id +| 3888: 78 27 28 73 65 67 69 64 2c 20 74 65 72 6d 2c 20 x'(segid, term, +| 3904: 70 67 6e 6f 2c 20 50 52 49 4d 41 52 59 20 4b 45 pgno, PRIMARY KE +| 3920: 59 28 73 65 67 69 64 2c 20 74 65 72 6d 29 29 20 Y(segid, term)) +| 3936: 57 49 54 48 4f 55 54 20 52 4f 57 49 44 55 02 07 WITHOUT ROWIDU.. +| 3952: 17 1b 1b 01 81 01 74 61 62 6c 65 74 31 5f 64 61 ......tablet1_da +| 3968: 74 61 74 31 5f 64 61 64 61 02 43 52 45 41 54 45 tat1_dada.CREATE +| 3984: 20 54 41 42 4c 45 20 27 74 31 5f 64 61 74 61 27 TABLE 't1_data' +| 4000: 28 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d (id INTEGER PRIM +| 4016: 41 52 59 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 ARY KEY, block B +| 4032: 4c 4f 42 29 3a 01 06 17 11 11 08 63 74 61 62 6c LOB):......ctabl +| 4048: 65 74 31 74 31 43 52 45 41 54 45 20 56 49 52 54 et1t1CREATE VIRT +| 4064: 55 41 4c 20 54 41 42 4c 45 20 74 31 20 55 53 49 UAL TABLE t1 USI +| 4080: 4e 47 20 66 74 73 35 28 63 6f 6e 74 65 6e 74 29 NG fts5(content) +| page 2 offset 4096 +| 0: 0d 0e b4 00 06 0e 35 00 0f e8 0e 35 0f bd 0f 4e ......5....5...N +| 16: 0e cb 0e 4f 00 00 00 00 00 00 00 00 00 00 00 00 ...O............ +| 3632: 00 00 00 00 00 18 0a 03 00 36 00 00 00 00 01 04 .........6...... +| 3648: 04 00 04 01 01 01 02 01 01 03 01 01 04 01 01 5e ...............^ +| 3664: 90 80 80 80 80 01 04 00 81 40 00 00 00 51 06 30 .........@...Q.0 +| 3680: 61 62 61 63 6b 01 01 04 04 6e 64 6f 6e 01 01 02 aback....ndon... +| 3696: 04 63 69 76 65 01 01 02 04 6c 70 68 61 01 01 02 .cive....lpha... +| 3712: 03 74 6f 6d 01 01 01 06 62 61 63 6b 75 70 01 01 .tom....backup.. +| 3728: 02 05 6f 6f 6d 65 72 01 01 01 06 63 68 61 6e 6e ..oomer....chann +| 3744: 65 01 01 01 04 74 65 73 74 01 01 04 09 08 08 08 e....test....... +| 3760: 07 0a 09 0a 0f 3a 00 17 30 00 00 00 00 01 03 03 .....:..0....... +| 3776: 00 03 01 01 01 02 01 01 03 01 01 68 8c 80 80 80 ...........h.... +| 3792: 80 01 04 00 81 54 00 00 00 5b 06 30 61 62 61 63 .....T...[.0abac +| 3808: 6b 02 02 07 04 04 6e 64 6f 6e 02 02 05 02 04 63 k.....ndon.....c +| 3824: 69 76 65 02 02 0b 02 04 6c 70 68 61 02 04 02 0a ive.....lpha.... +| 3840: 02 03 74 6f 6d 02 02 09 01 06 62 61 63 6b 75 70 ..tom.....backup +| 3856: 02 02 04 02 05 6f 6f 6d 65 72 02 02 08 01 06 63 .....oomer.....c +| 3872: 68 61 6e 6e 65 02 02 03 01 04 74 65 73 74 02 02 hanne.....test.. +| 3888: 06 04 0a 09 09 0a 08 0b 0a 0b 0f ef 00 14 2a 00 ..............*. +| 3904: 00 00 00 01 02 02 00 02 01 01 01 02 01 01 68 88 ..............h. +| 3920: 80 80 80 80 01 04 00 81 54 00 00 00 5b 06 30 61 ........T...[.0a +| 3936: 62 61 63 6b 01 02 07 04 04 6e 64 6f 6e 01 02 05 back.....ndon... +| 3952: 02 04 63 69 76 65 01 02 0b 02 04 6c 70 68 61 01 ..cive.....lpha. +| 3968: 04 02 0a 02 03 74 6f 6d 01 02 09 01 06 62 61 63 .....tom.....bac +| 3984: 6b 75 70 01 02 04 02 05 6f 6f 6d 65 72 01 02 08 kup.....oomer... +| 4000: 01 06 63 68 61 6e 6e 65 01 02 03 01 04 74 65 73 ..channe.....tes +| 4016: 74 01 02 06 04 0a 09 09 0a 08 0b 0a 0b 24 84 80 t............$.. +| 4032: 80 80 80 01 03 00 4e 00 00 00 1e 06 30 61 62 61 ......N.....0aba +| 4048: 63 6b 01 02 02 05 42 66 74 02 02 02 04 04 6e 64 ck....Bft.....nd +| 4064: 6f 6e 03 02 02 04 0a 07 05 01 03 00 10 04 0d 00 on.............. +| 4080: 00 00 11 24 00 00 00 00 01 01 01 00 01 01 01 01 ...$............ +| page 3 offset 8192 +| 0: 0a 00 00 00 04 0f e5 00 00 00 0f f3 0f ec 0f e5 ................ +| 4064: 00 00 00 00 00 06 04 01 0c 01 04 02 06 04 01 0c ................ +| 4080: 01 03 02 06 04 01 0c 01 02 02 05 04 09 0d 01 02 ................ +| page 4 offset 12288 +| 0: 0d 0e bc 00 04 0e 78 00 00 00 00 00 00 00 0e 78 ......x........x +| 16: 0e 78 00 00 00 00 00 00 00 00 00 00 00 00 00 00 .x.............. +| 3696: 00 00 00 00 00 00 00 00 42 02 04 00 81 09 61 6c ........B.....al +| 3712: 70 68 61 20 63 68 61 6e 6e 65 20 62 61 63 6b 75 pha channe backu +| 3728: 70 20 61 62 61 6e 64 6f 6e 20 74 65 73 74 20 61 p abandon test a +| 3744: 62 61 63 6b 20 62 6f 6f 6d 65 72 20 61 74 6f 6d back boomer atom +| 3760: 20 61 6c 70 68 61 20 61 63 69 76 65 00 00 00 44 alpha acive...D +| 3776: 81 09 61 6c 70 68 61 20 63 68 61 6e 6e 65 20 62 ..alpha channe b +| 3792: 61 63 6b 75 70 20 61 62 61 6e 64 6f 6e 20 74 65 ackup abandon te +| 3808: 73 74 20 61 62 61 63 6b 20 62 6f 6f 6d 65 72 20 st aback boomer +| 3824: 61 74 6f 6d 20 61 6c 70 68 61 20 61 63 69 76 65 atom alpha acive +| 4064: 0a 03 03 00 1b 61 4e 61 6e 64 6f 6e 08 02 03 00 .....aNandon.... +| 4080: 17 61 62 61 66 74 08 01 03 00 17 61 62 71 63 6b .abaft.....abqck +| page 5 offset 16384 +| 0: 0d 0f e8 00 04 0f e2 00 00 00 00 00 00 00 0f e2 ................ +| 16: 0f e2 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 4064: 00 00 04 02 03 00 0e 0a 00 00 00 06 0e 0a 04 03 ................ +| 4080: 03 00 0e 01 04 02 03 00 0e 01 04 01 03 10 0e 01 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| page 7 offset 24576 +| 0: 0d 00 00 00 03 0f d6 00 0f f4 00 00 00 00 00 00 ................ +| 4048: 00 00 00 00 00 00 09 03 02 1b 72 65 62 75 69 6c ..........rebuil +| 4064: 64 11 02 02 2b 69 6e 74 65 67 72 69 74 79 2d 63 d...+integrity-c +| 4080: 68 65 63 6b 0a 01 02 1d 6f 70 74 69 6d 69 7a 65 heck....optimize +| end x.db +}]} {} + +do_catchsql_test 57.1 { + INSERT INTO t1(t1) VALUES('optimize') +} {/*malformed database schema*/} + +#------------------------------------------------------------------------- +reset_db +do_test 58.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 24576 pagesize 4096 filename crash-5a5acd0ab42d31.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 00 .....@ ........ +| 96: 00 00 00 00 0d 00 00 00 06 0e 0f 00 0f aa 0f 53 ...............S +| 112: 0e e8 0e 8b 0e 33 0e 0f 00 00 00 00 00 00 00 00 .....3.......... +| 3584: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 22 ................ +| 3600: 06 06 17 11 11 01 31 74 61 62 6c 65 62 62 62 62 ......1tablebbbb +| 3616: 06 43 52 45 41 54 45 20 54 41 42 4c 45 20 62 62 .CREATE TABLE bb +| 3632: 28 61 29 56 05 06 17 1f 1f 01 7d 74 61 62 6c 65 (a)V.......table +| 3648: 74 31 5f 63 2a 6e 66 69 68 74 31 5f 63 6f 6e 66 t1_c*nfiht1_conf +| 3664: 69 67 05 43 52 45 41 54 45 20 54 41 42 4c 45 20 ig.CREATE TABLE +| 3680: 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b 20 50 52 't1_config'(k PR +| 3696: 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 20 57 49 IMARY KEY, v) WI +| 3712: 54 48 4f 55 54 20 52 4f 57 49 44 5b 04 07 17 21 THOUT ROWID[...! +| 3728: 21 01 81 01 74 61 62 6c 65 74 31 5f 64 6f 73 73 !...tablet1_doss +| 3744: 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 04 43 52 izet1_docsize.CR +| 3760: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 64 EATE TABLE 't1_d +| 3776: 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 54 45 47 ocsize'(id INTEG +| 3792: 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 ER PRIMARY KEY, +| 3808: 73 7a 20 42 4c 4f 42 29 69 03 07 17 19 19 01 81 sz BLOB)i....... +| 3824: 2d 74 61 62 6c 65 74 31 5f 69 64 78 74 31 5f 69 -tablet1_idxt1_i +| 3840: 64 78 03 43 52 45 41 54 45 20 54 41 42 4c 45 20 dx.CREATE TABLE +| 3856: 27 74 31 5f 69 64 78 27 28 73 65 67 69 64 2c 20 't1_idx'(segid, +| 3872: 74 65 72 6d 2c 20 70 67 6e 6f 2c 20 50 52 49 4d term, pgno, PRIM +| 3888: 41 52 59 20 4b 45 59 28 73 65 67 69 64 2c 20 74 ARY KEY(segid, t +| 3904: 65 72 6d 29 29 20 57 49 54 48 4f 55 54 20 52 4f erm)) WITHOUT RO +| 3920: 57 49 44 55 02 07 17 1b 1b 01 81 01 74 61 62 6c WIDU........tabl +| 3936: 65 74 31 5f 64 61 74 61 74 31 5f 64 61 74 61 02 et1_datat1_data. +| 3952: 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 CREATE TABLE 't1 +| 3968: 5f 64 61 74 61 27 28 69 64 20 49 4e 54 45 47 45 _data'(id INTEGE +| 3984: 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 62 R PRIMARY KEY, b +| 4000: 6c 6f 63 6b 20 42 4c 4f 42 29 54 01 07 17 11 11 lock BLOB)T..... +| 4016: 08 81 15 74 61 62 6c 65 74 31 74 31 43 52 45 41 ...tablet1t1CREA +| 4032: 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45 TE VIRTUAL TABLE +| 4048: 20 74 31 20 55 53 49 4e 47 20 66 74 73 35 28 61 t1 USING fts5(a +| 4064: 2c 62 2c 70 72 65 66 69 78 3d 22 32 2c 32 2c 33 ,b,prefix=.2,2,3 +| 4080: 2c 34 22 2c 20 63 6f 6e 74 65 6e 74 3d 22 22 29 ,4., content=..) +| page 2 offset 4096 +| 0: 0d 0b 6a 00 37 09 4c 02 0f e7 09 4c 0f c6 0f a4 ..j.7.L....L.... +| 16: 0f 88 0f 6d 0f 4b 0f 2c 0f 0e 0e ec 0e cd 0e ae ...m.K.,........ +| 32: 0e 8e 0e 6c 0e 4b 0e 29 0e 08 0d e6 0d c4 0d b5 ...l.K.)........ +| 48: 0d 97 0d 76 0d 54 0d 30 fd 15 0c f3 0c d3 0c b5 ...v.T.0........ +| 64: 0c 95 0c 73 0c 54 0c 32 0c 10 0b ee 0b cc 0b b0 ...s.T.2........ +| 80: 0b 8d 0b 7e 0b 48 0b 2e 0b 0b 0a ef 0a cc 0a ad ...~.H.......... +| 96: 0a 8c 0a 6d 0a 4d 0a 2b 0a 0c 00 00 00 00 00 00 ...m.M.+........ +| 2368: 00 00 00 00 00 00 00 00 00 00 00 00 15 0a 03 00 ................ +| 2384: 30 00 00 00 9c 01 03 35 00 03 01 01 12 02 01 12 0......5........ +| 2400: 03 01 11 1c 8c 80 80 80 80 10 03 00 3e 00 00 00 ............>... +| 2416: 17 01 05 05 34 74 61 62 6c 03 02 03 01 04 77 68 ....4tabl.....wh +| 2432: 65 72 03 02 06 09 1b 8c 80 80 80 80 0f 03 00 3c er.............< +| 2448: 00 00 00 16 05 34 66 74 73 34 03 02 02 01 04 6e .....4fts4.....n +| 2464: 75 6d 62 03 06 01 04 09 1b 8c 80 80 80 80 0e 03 umb............. +| 2480: 00 3c 00 00 00 16 04 33 74 68 65 03 06 01 01 04 .<.....3the..... +| 2496: 01 03 77 68 65 03 02 04 04 0a 1b 8c 80 80 80 80 ..whe........... +| 2512: 0d 03 00 3c 00 00 00 16 04 33 6e 75 6d 03 06 01 ...<.....3num... +| 2528: 01 05 01 03 74 61 62 05 62 03 04 0a 19 8c 80 80 ....tab.b....... +| 2544: 80 80 0c 03 00 38 00 00 00 14 03 39 a7 68 03 02 .....8.....9.h.. +| 2560: 04 10 04 33 66 74 73 03 02 02 04 07 18 8c 80 80 ...3fts......... +| 2576: 80 80 0b 03 00 36 00 00 00 13 03 32 74 61 03 02 .....6.....2ta.. +| 2592: 03 02 01 68 03 06 01 01 04 04 07 1b 8c 80 80 80 ...h............ +| 2608: 80 0a 03 00 3c 00 00 00 16 03 32 6e 75 03 06 01 ....<.....2nu... +| 2624: 01 05 01 02 6f 66 03 06 01 01 06 04 09 19 8c 80 ....of.......... +| 2640: 80 80 80 09 03 00 38 00 00 00 14 03 32 66 74 03 ......8.....2ft. +| 2656: 02 02 01 02 69 73 03 06 01 01 03 04 07 18 8c 80 ....is.......... +| 2672: 80 80 80 08 03 00 36 00 00 00 13 02 31 74 03 08 ......6.....1t.. +| 2688: 03 01 01 04 01 01 77 03 02 04 04 09 1a 8c 80 80 ......w......... +| 2704: 80 80 07 03 00 3a ff 00 00 15 02 31 6e 03 08 01 .....:.....1n... +| 2720: 01 02 05 01 01 6f 03 06 01 01 06 04 09 18 8c 80 .....o.......... +| 2736: 80 80 80 06 03 00 36 00 00 00 13 04 02 31 66 03 ......6......1f. +| 2752: 02 01 f1 01 69 03 06 01 01 03 05 06 1c 8c 80 80 ....i........... +| 2768: 80 80 05 03 00 3e 00 00 00 17 04 30 74 68 65 03 .....>.....0the. +| 2784: 06 01 01 14 01 05 77 68 65 72 65 03 02 04 0a 15 ......where..... +| 2800: 8c 80 80 80 80 04 03 00 30 00 00 00 11 01 01 06 ........0....... +| 2816: 06 30 74 61 62 6c cc 03 02 03 07 1c 8c 80 80 80 .0tabl.......... +| 2832: 80 03 03 00 3e 00 00 00 17 07 30 6e 75 6d 62 65 ....>.....0numbe +| 2848: 72 03 06 01 01 05 01 02 6f 66 02 06 04 0d 13 8c r.......of...... +| 2864: 80 80 80 80 02 03 00 2c 00 00 00 0f 01 01 03 02 .......,........ +| 2880: 30 6e 03 06 01 01 02 07 1b 8c 80 80 80 80 01 03 0n.............. +| 2896: 00 3c 00 00 00 16 08 30 66 74 73 34 61 75 78 03 .<.....0fts4aux. +| 2912: 02 02 01 02 69 73 03 06 04 0c 00 00 00 14 2a 00 ....is........*. +| 2928: 00 00 01 01 02 24 00 02 01 01 12 02 01 12 08 88 .....$.......... +| 2944: 80 80 80 80 12 03 00 16 00 00 00 05 02 1c 88 80 ................ +| 2960: 80 80 80 11 03 00 3e 00 00 00 17 05 34 72 6f 77 ......>.....4row +| 2976: 73 02 06 01 01 05 01 04 74 68 65 72 02 02 04 0b s.......ther.... +| 2992: 15 88 80 80 80 80 10 03 00 3e 10 00 00 11 02 01 .........>...... +| 3008: 01 07 05 34 62 65 74 77 02 02 04 08 1b 88 80 80 ...4betw........ +| 3024: 80 80 0f 03 00 3c 00 00 00 16 04 04 33 72 6f 77 .....<......3row +| 3040: 02 06 01 01 05 01 03 74 68 65 02 08 05 0a 1b 88 .......the...... +| 3056: 80 80 80 80 0e 03 05 0c 00 00 00 16 01 01 02 04 ................ +| 3072: 33 61 72 65 02 02 03 01 03 62 65 74 02 02 07 08 3are.....bet.... +| 3088: 1b 88 80 80 80 80 0d 03 00 3c 00 00 00 16 03 32 .........<.....2 +| 3104: 74 68 02 08 02 01 01 07 00 04 33 61 6e 64 02 06 th........3and.. +| 3120: 04 01 1b 88 80 80 80 80 0c 03 00 3c 00 00 00 16 ...........<.... +| 3136: 03 32 69 6e 02 06 01 01 06 01 02 72 6f 02 06 01 .2in.......ro... +| 3152: 01 05 04 09 18 88 80 80 80 80 0b 03 00 36 00 00 .............6.. +| 3168: 00 13 02 03 32 61 72 02 02 03 01 02 62 65 02 02 ....2ar.....be.. +| 3184: 04 05 07 1b 88 80 bf 80 80 0a 03 00 3c 00 00 00 ............<... +| 3200: 16 02 31 74 02 08 02 01 01 07 00 03 32 61 6e 02 ..1t........2an. +| 3216: 06 01 01 04 09 19 88 80 80 80 80 09 03 00 38 00 ..............8. +| 3232: 00 00 14 02 31 6e 02 06 01 01 03 01 01 72 02 06 ....1n.......r.. +| 3248: 01 01 05 03 08 17 88 80 80 80 80 08 03 00 34 00 ..............4. +| 3264: 01 00 12 02 31 62 02 02 04 01 01 69 02 06 01 01 ....1b.....i.... +| 3280: 06 04 06 19 88 80 80 80 80 07 03 00 38 00 00 00 ............8... +| 3296: 14 04 02 31 32 02 02 05 01 01 61 02 08 03 01 01 ...12.....a..... +| 3312: 02 05 06 1b 88 80 80 80 80 06 03 00 3c 00 00 00 ............<... +| 3328: 16 06 30 74 68 65 72 65 02 12 02 00 02 31 31 02 ..0there.....11. +| 3344: 06 01 01 04 0a 15 88 80 80 80 80 05 03 00 30 00 ..............0. +| 3360: 00 00 11 01 01 05 04 30 74 68 65 02 06 71 01 07 .......0the..q.. +| 3376: 07 1c 88 80 80 80 80 04 03 00 3e 00 00 00 17 01 ..........>..... +| 3392: 01 06 02 30 6e 02 06 01 01 03 01 04 72 6f 77 73 ...0n.......rows +| 3408: 02 06 07 08 1b 88 80 80 80 80 03 03 00 3c 00 00 .............<.. +| 3424: 00 16 08 30 62 65 74 77 65 65 6e 02 02 04 01 02 ...0between..... +| 3440: 69 6e 02 06 04 0c 1a 88 80 80 80 80 02 03 00 3a in.............: +| 3456: 08 f0 00 15 04 30 61 6e 64 02 06 01 01 02 02 02 .....0and....... +| 3472: 72 65 02 02 03 04 0a 17 88 80 80 80 80 01 03 00 re.............. +| 3488: 34 00 00 00 12 02 30 31 02 06 01 01 04 01 01 32 4.....01.......2 +| 3504: 02 02 07 04 08 08 84 80 80 80 80 12 03 00 16 00 ................ +| 3520: 00 00 05 04 1b 84 80 80 80 80 11 03 00 3c 00 00 .............<.. +| 3536: 00 16 05 34 74 61 62 6c 01 06 01 01 05 02 03 65 ...4tabl.......e +| 3552: 72 6d 01 02 04 0b 1b 84 80 80 80 80 10 03 00 3c rm.............< +| 3568: 00 00 00 16 05 34 65 61 63 68 01 02 03 01 04 70 .....4each.....p +| 3584: 72 65 73 01 02 05 04 09 1a 84 80 80 80 80 0f 03 res............. +| 3600: 00 3a 00 00 00 15 04 33 74 65 72 01 02 04 02 02 .:.....3ter..... +| 3616: 68 65 01 06 01 01 03 04 08 1b 84 80 80 80 80 0e he.............. +| 3632: 03 00 3c 00 00 00 16 04 33 70 72 65 01 02 05 01 ..<.....3pre.... +| 3648: 03 74 61 62 01 06 01 01 05 04 08 1a 84 80 80 80 .tab............ +| 3664: 80 0d 03 00 3a 00 00 00 15 04 33 66 6f 72 01 02 ....:.....3for.. +| 3680: 02 02 02 74 73 01 06 01 01 04 03 f8 1b 84 80 80 ...ts........... +| 3696: 80 80 0c 03 00 3c 00 00 00 16 03 32 74 68 01 06 .....<.....2th.. +| 3712: 01 01 03 00 04 33 65 61 63 01 02 03 04 09 18 84 .....3eac....... +| 3728: 80 80 80 80 0b 03 00 36 00 00 00 13 03 32 74 61 .......6.....2ta +| 3744: 01 06 01 01 05 02 01 65 00 02 04 04 09 19 84 80 .......e........ +| 3760: 80 80 80 0a 03 10 38 00 00 00 14 03 32 69 6e 01 ......8.....2in. +| 3776: 06 01 01 02 01 02 70 72 01 02 05 04 09 18 84 80 ......pr........ +| 3792: 80 80 80 09 03 00 36 00 00 00 13 03 32 66 6f 01 ......6.....2fo. +| 3808: 02 02 02 01 74 01 06 01 01 04 04 07 1b 84 80 80 ....t........... +| 3824: 80 80 08 03 00 3c 00 00 00 16 02 31 74 01 0a 04 .....<.....1t... +| 3840: 01 00 03 04 00 03 32 65 61 01 02 03 04 0a 17 84 ......2ea....... +| 3856: 80 80 80 80 07 03 00 34 00 00 00 12 02 31 69 01 .......4.....1i. +| 3872: 06 01 01 02 de 01 70 01 02 05 04 08 18 84 80 80 ......p......... +| 3888: 80 80 06 03 00 36 00 00 00 13 02 31 65 01 02 03 .....6.....1e... +| 3904: 01 01 66 01 08 02 01 01 04 04 06 1b 84 80 80 80 ..f............. +| 3920: 80 05 03 00 3c 00 00 00 16 05 30 74 65 72 6d 01 ....<.....0term. +| 3936: 02 04 02 02 68 65 01 06 01 01 03 04 09 14 84 80 ....he.......... +| 3952: 80 80 80 04 03 00 2e 00 00 00 10 06 30 74 61 62 ............0tab +| 3968: 6c 65 01 06 01 01 05 04 15 84 80 80 80 80 03 03 le.............. +| 3984: 00 30 00 00 00 11 02 08 30 70 72 65 73 65 6e 74 .0......0present +| 4000: 01 02 05 05 1b 84 80 80 80 80 02 03 00 3c 00 00 .............<.. +| 4016: 00 16 04 30 66 74 73 01 06 01 01 04 01 02 69 6e ...0fts.......in +| 4032: 01 06 01 01 04 0a 1a 84 80 80 80 80 01 03 00 3a ...............: +| 4048: 00 00 00 15 05 30 65 61 63 68 00 f2 03 01 03 66 .....0each.....f +| 4064: 6f 72 01 02 02 04 09 06 01 03 00 12 03 0b 0f 00 or.............. +| 4080: 00 08 8c 80 80 80 80 11 03 00 16 00 00 00 05 04 ................ +| page 3 offset 8192 +| 0: 0a 00 00 00 32 0e 4f 00 0f fa 0f f1 0f e9 0f e1 ....2.O......... +| 16: 0f d8 0f d1 0f c9 0f c1 0f b9 0f c1 0f a9 0f a0 ................ +| 32: 0f 98 0f 90 0f 87 0f 80 0f 78 0f 71 0f 68 0f 5f .........x.q.h._ +| 48: 0f 56 0f 4d 0f 41 0f 38 0f 2f 0f 26 0f 1d 0f 13 .V.M.A.8./.&.... +| 64: 0f 0a 0f 01 0e f7 0e ee 0e e6 0e dd 0e d7 0e cd ................ +| 80: 0e c3 0e ba 0e b0 0e a8 0e 9f 0e 96 0e 8e 0e 85 ................ +| 3648: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 08 ................ +| 3664: 04 01 10 01 03 34 74 20 07 04 01 0e 01 03 34 1e .....4t ......4. +| 3680: 09 04 01 12 34 03 33 74 68 1c 08 04 01 10 01 03 ....4.3th....... +| 3696: 33 6e 1a 08 04 01 10 01 03 32 77 18 08 04 01 10 3n.......2w..... +| 3712: 01 03 32 74 16 08 04 01 10 01 03 32 6e 14 07 04 ..2t.......2n... +| 3728: 01 0e 01 03 32 12 08 04 01 0f f1 03 31 74 10 08 ....2.......1t.. +| 3744: 04 01 10 01 03 31 6e 0e 07 04 01 0e 01 03 30 fc .....1n.......0. +| 3760: 09 04 01 12 01 03 30 74 68 0a 08 04 01 10 01 03 ......0th....... +| 3776: 30 74 08 09 04 01 12 01 03 30 6e 75 06 08 04 01 0t.......0nu.... +| 3792: 10 01 03 30 6e 04 06 04 01 0c 01 05 52 08 04 01 ...0n.......R... +| 3808: 10 01 02 34 72 22 07 04 01 0e 01 02 34 20 08 04 ...4r.......4 .. +| 3824: 01 10 01 02 33 72 1e 09 04 01 12 01 02 33 61 72 ....3r.......3ar +| 3840: 1c 08 04 01 10 01 02 32 74 1a 08 04 01 10 b3 02 .......2t....... +| 3856: 32 69 18 09 04 01 12 01 02 32 61 72 16 08 04 01 2i.......2ar.... +| 3872: 10 01 02 31 74 14 08 04 01 10 01 02 31 6e 12 08 ...1t.......1n.. +| 3888: 04 01 10 01 02 31 62 10 08 04 01 10 01 02 31 32 .....1b.......12 +| 3904: 0e 0b 04 01 16 01 02 30 74 68 65 72 0c 08 04 01 .......0ther.... +| 3920: 10 01 02 30 74 0a 08 04 01 10 01 02 30 6e 08 08 ...0t.......0n.. +| 3936: 04 01 10 01 02 30 62 06 09 04 01 10 01 02 30 61 .....0b.......0a +| 3952: 04 06 04 01 0c 01 02 02 07 04 09 10 01 34 74 22 .............4t. +| 3968: 06 04 09 0e 01 34 20 08 04 09 12 01 33 74 65 1e .....4 .....3te. +| 3984: 07 04 09 10 01 33 70 1c 07 f4 09 11 01 33 66 1a .....3p......3f. +| 4000: 08 04 09 12 01 32 74 68 18 07 04 09 10 01 32 e4 .....2th......2. +| 4016: 16 07 04 09 10 01 32 69 14 07 04 09 10 01 32 66 ......2i......2f +| 4032: 12 07 04 09 10 01 31 74 10 07 04 09 10 01 31 69 ......1t......1i +| 4048: 0e 06 04 09 0e 01 31 0c 08 04 09 12 01 30 74 65 ......1......0te +| 4064: 0a 07 04 09 10 01 30 74 08 00 00 00 00 00 00 00 ......0t........ +| page 4 offset 12288 +| 4064: 00 00 00 00 00 00 00 00 00 00 00 05 03 03 00 10 ................ +| 4080: 03 05 05 02 03 00 10 04 06 05 01 03 00 10 04 04 ................ +| page 5 offset 16384 +| 0: 0a 00 00 00 02 0f eb 00 0f eb 0f f4 00 00 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 08 03 15 01 70 ...............p +| 4080: 67 73 7a 18 0b 03 1b 01 76 65 72 73 69 6f 6e 04 gsz.....version. +| page 6 offset 20480 +| 4080: 00 00 23 03 02 01 03 03 02 00 00 00 00 00 00 00 ..#............. +| end crash-5a5acd0ab42d31.db +}]} {} + +do_catchsql_test 58.1 { + SELECT * FROM t1('t*'); +} {/*malformed database schema*/} + +#------------------------------------------------------------------------- +do_test 59.0 { + sqlite3 db {} + sqlite3_fts5_register_matchinfo db + db deserialize [decode_hexdb { +.open --hexdb +| size 32768 pagesize 4096 filename crash-96b136358d01ec.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 00 .....@ ........ +| 96: 00 00 00 00 0d 0f c7 00 07 0d 92 00 0f 8d 0f 36 ...............6 +| 112: 0e cb 0e 6b 0e 0e 0d b6 0d 92 00 00 00 00 00 00 ...k............ +| 3472: 00 00 22 08 06 17 11 11 01 31 74 61 62 6c 65 74 .........1tablet +| 3488: 32 74 32 08 43 52 45 41 54 45 20 54 41 42 4c 45 2t2.CREATE TABLE +| 3504: 20 74 32 28 78 29 56 07 06 17 1f 1f 01 7d 74 61 t2(x)V.......ta +| 3520: 62 6c 65 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 blet1_configt1_c +| 3536: 6f 6e 66 69 67 07 43 52 45 41 54 45 20 54 41 42 onfig.CREATE TAB +| 3552: 4c 45 20 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b LE 't1_config'(k +| 3568: 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 PRIMARY KEY, v) +| 3584: 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 5b 06 WITHOUT ROWID[. +| 3600: 07 17 21 21 01 81 01 74 61 62 6c 65 74 31 5f 64 ..!!...tablet1_d +| 3616: 6f 63 73 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 ocsizet1_docsize +| 3632: 06 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 .CREATE TABLE 't +| 3648: 31 5f 64 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 1_docsize'(id IN +| 3664: 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE +| 3680: 59 2c 20 73 7a 20 42 4c 4f 42 29 5e 05 07 17 21 Y, sz BLOB)^...! +| 3696: 21 01 81 07 74 61 62 6c 65 74 31 5f 63 6f 6e 74 !...tablet1_cont +| 3712: 65 6e 74 74 31 5f 63 6f 6e 74 65 6e 74 05 43 52 entt1_content.CR +| 3728: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 EATE TABLE 't1_c +| 3744: 6f 6e 74 65 6e 74 27 28 69 64 20 49 4e 54 45 47 ontent'(id INTEG +| 3760: 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 ER PRIMARY KEY, +| 3776: 63 30 2c 20 63 31 2c d6 63 32 29 69 04 07 17 19 c0, c1,.c2)i.... +| 3792: 19 01 81 2d 74 61 62 6c 65 74 31 5f 69 64 78 74 ...-tablet1_idxt +| 3808: 31 5f 69 64 78 04 43 52 45 41 54 45 20 54 41 42 1_idx.CREATE TAB +| 3824: 4c 45 20 27 74 31 5f 69 64 78 27 28 73 65 67 69 LE 't1_idx'(segi +| 3840: 64 2c 20 74 65 72 6d 2c 20 70 67 6e 6f 2c 20 50 d, term, pgno, P +| 3856: 52 49 4d 41 52 59 20 4b 45 59 28 73 65 67 69 64 RIMARY KEY(segid +| 3872: 2c 20 74 65 72 6d 29 29 20 57 49 54 48 4f 55 54 , term)) WITHOUT +| 3888: 20 52 4f 57 49 44 55 03 07 17 1b 1b 01 81 01 74 ROWIDU........t +| 3904: 61 62 6c 65 74 31 5f 64 61 74 61 74 31 5f 64 61 ablet1_datat1_da +| 3920: 74 61 03 43 52 45 41 54 45 20 54 41 42 4c 45 20 ta.CREATE TABLE +| 3936: 27 74 31 5f 64 61 74 61 27 28 69 64 20 49 4e 54 't1_data'(id INT +| 3952: 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 EGER PRIMARY KEY +| 3968: 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 29 38 02 06 , block BLOB)8.. +| 3984: 17 11 11 08 5f 74 61 62 6c 65 74 31 74 31 43 52 ...._tablet1t1CR +| 4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 35 LE t1 USING fts5 +| 4032: 28 61 2c 62 2c 63 29 00 00 00 00 00 00 00 00 00 (a,b,c)......... +| page 3 offset 8192 +| 0: 0d 00 00 00 03 0c 93 ff 0f e6 0f ef 0c 94 00 00 ................ +| 3216: 00 00 00 00 86 4a 84 80 80 80 80 01 04 00 8d 18 .....J.......... +| 3232: 00 00 03 2b 02 30 30 01 02 06 01 02 06 01 02 06 ...+.00......... +| 3248: 1f 02 03 01 02 03 01 02 03 01 08 32 31 31 36 30 ...........21160 +| 3264: 36 30 39 01 02 07 01 02 07 01 02 07 01 01 33 f1 609...........3. +| 3280: 02 05 01 02 05 01 02 05 01 01 35 01 02 03 01 02 ..........5..... +| 3296: 04 01 02 04 02 07 30 30 30 30 30 30 30 1c 02 3d ......0000000..= +| 3312: 01 02 04 01 02 04 01 06 62 69 6e 61 72 79 03 06 ........binary.. +| 3328: 01 02 02 03 06 01 01 f2 03 06 4e 02 02 03 06 01 ..........N..... +| 3344: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3360: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 ................ +| 3376: 03 06 01 02 02 03 06 01 02 02 01 08 63 6f 6d 70 ............comp +| 3392: 69 6c 65 72 01 02 02 01 02 02 01 02 02 01 06 64 iler...........d +| 3408: 62 73 74 61 74 07 02 03 01 02 13 01 02 03 02 04 bstat........... +| 3424: 65 62 75 67 04 02 02 01 02 02 01 02 02 01 07 65 ebug...........e +| 3440: 6e 61 62 6c 65 07 02 02 01 02 02 01 02 02 01 02 nable........... +| 3456: 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 ................ +| 3472: 01 02 02 01 02 02 01 02 01 f1 02 02 01 02 02 01 ................ +| 3488: 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 ................ +| 3504: 02 01 02 02 02 08 78 74 65 6e 73 69 6f 6e 1f 02 ......xtension.. +| 3520: 04 01 02 04 01 02 04 01 04 66 74 73 34 0a 02 03 .........fts4... +| 3536: 01 02 03 01 02 03 04 01 25 0d 02 03 01 02 03 01 ........%....... +| 3552: 02 03 01 03 67 63 63 01 02 03 01 02 03 01 02 03 ....gcc......... +| 3568: 02 06 65 6f 70 6f 6c 79 0f f2 03 01 02 03 01 02 ..eopoly........ +| 3584: 03 01 05 6a 73 6f 6e 31 13 02 03 01 02 03 01 02 ...json1........ +| 3600: 03 01 04 6c 6f 61 64 1f 02 03 01 02 03 01 02 03 ...load......... +| 3616: 00 03 6d 61 78 1c 02 02 01 02 02 01 02 02 02 05 ..max........... +| 3632: 65 6d 6f 72 79 1c 02 03 01 02 03 01 02 03 04 04 emory........... +| 3648: 73 79 73 35 16 02 03 01 02 03 01 02 03 01 06 6e sys5...........n +| 3664: 6f 63 61 73 65 02 06 01 02 02 13 06 00 f2 02 03 ocase........... +| 3680: 06 01 02 02 13 06 01 02 02 03 06 01 02 02 03 06 ................ +| 3696: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3712: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3728: 02 01 04 6f 6d 69 74 1f 02 02 01 02 02 01 02 02 ...omit......... +| 3744: 01 0a 22 74 72 65 65 19 02 03 01 02 03 01 02 03 ...tree......... +| 3760: 04 02 69 6d 01 06 01 02 02 03 06 01 02 02 03 06 ..im............ +| 3776: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3792: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3808: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 ................ +| 3824: 01 0a 74 68 72 65 61 64 73 61 66 65 22 02 02 01 ..threadsafe.... +| 3840: 02 02 01 02 02 01 04 76 74 61 62 07 02 04 01 02 .......vtab..... +| 3856: 04 01 02 04 01 01 78 01 06 01 01 02 01 06 01 01 ......x......... +| 3872: 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 ................ +| 3888: 01 06 01 11 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 3904: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 3920: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 ................ +| 3936: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 ................ +| 3952: 02 01 06 01 01 01 f1 06 01 01 02 ad 06 01 01 02 ................ +| 3968: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 3984: 06 01 01 01 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 4000: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 ................ +| 4016: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 ................ +| 4032: 02 01 06 01 01 02 01 06 01 01 02 04 15 13 0c 0c ................ +| 4048: 12 44 13 11 0f 47 13 0e fc 0e 11 10 0f 0e 10 0f .D...G.......... +| 4064: 44 0f 10 40 15 0f 07 01 03 00 14 24 5a 24 24 0f D..@.......$Z$$. +| 4080: 0a 03 00 24 00 00 00 00 01 01 01 00 01 01 01 01 ...$............ +| page 4 offset 12288 +| 0: 0a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 05 04 09 0c 01 02 ................ +| page 5 offset 16384 +| 0: 0d 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 3072: 00 00 00 00 00 00 00 00 00 00 18 24 05 00 25 0f ...........$..%. +| 3088: 19 54 48 52 45 41 44 53 41 46 45 3d 30 58 42 49 .THREADSAFE=0XBI +| 3104: 4e 41 52 59 18 23 05 00 25 0f 19 54 48 52 45 41 NARY.#..%..THREA +| 3120: 44 53 41 46 45 3d 30 58 4e 4f 43 41 53 45 17 8f DSAFE=0XNOCASE.. +| 3136: 05 00 25 0f 17 54 48 52 45 41 44 53 41 46 45 3d ..%..THREADSAFE= +| 3152: 30 58 52 54 52 49 4d 1f 21 05 00 33 0f 19 45 ed 0XRTRIM.!..3..E. +| 3168: 49 54 20 4c 4f 41 44 21 45 58 54 45 4e 53 49 4f IT LOAD!EXTENSIO +| 3184: 4e 58 42 49 4e 41 52 59 1f 20 05 00 33 0f 19 4f NXBINARY. ..3..O +| 3200: 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 5a 29 MIT LOAD EXTENZ) +| 3216: 4f 4e 58 4e 4f 43 41 53 45 1e 1f 05 00 33 0f 17 ONXNOCASE....3.. +| 3232: 4f 4d 59 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 OMYT LOAD EXTENS +| 3248: 49 4f 4e 58 52 54 56 a9 4d 1f 1e 05 00 33 0f 19 IONXRTV.M....3.. +| 3264: 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 30 MAX MEMORY=50000 +| 3280: 30 30 30 57 42 49 4e 31 52 59 1f 1d 05 00 33 0f 000WBIN1RY....3. +| 3296: 19 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 .MAX MEMORY=5000 +| 3312: 30 30 30 30 58 4e 4f 43 41 53 45 1e 1c 05 00 32 0000XNOCASE....2 +| 3328: 0f 17 4e 41 58 20 4d 45 4d 4f 52 59 2d 35 30 30 ..NAX MEMORY-500 +| 3344: 30 30 30 30 30 58 52 54 52 49 4d 18 1b 05 00 25 00000XRTRIM....% +| 3360: 0f 19 45 4e 41 42 4c 45 20 52 54 52 45 45 58 42 ..ENABLE RTREEXB +| 3376: 49 4e 41 52 59 18 1a 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3392: 4c 45 20 52 54 52 45 45 59 4e 4f 43 41 53 45 17 LE RTREEYNOCASE. +| 3408: 19 66 00 25 0f 17 45 4e 41 42 4c 45 20 52 54 52 .f.%..ENABLE RTR +| 3424: 45 45 58 52 54 52 49 4d 1a 18 05 00 29 0f 19 45 EEXRTRIM....)..E +| 3440: 4e 41 42 4c 45 20 4d 45 4d 53 59 53 35 58 42 49 NABLE MEMSYS5XBI +| 3456: 4e 41 52 59 1a 17 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3472: 45 20 4d 45 4d 53 59 53 35 58 4e 4f 43 41 53 45 E MEMSYS5XNOCASE +| 3488: 19 16 05 00 29 0f 17 45 4e 41 42 4c 45 20 4d 45 ....)..ENABLE ME +| 3504: 4d 53 59 76 35 58 52 54 52 49 4d 18 15 05 10 25 MSYv5XRTRIM....% +| 3520: 0f 19 45 4e 40 42 4c 45 20 4a 53 4f 4e 31 58 42 ..EN@BLE JSON1XB +| 3536: 49 4e 41 52 59 18 14 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3552: 4c 45 20 4a 53 4f 4e 32 58 4e 4f 43 41 53 45 17 LE JSON2XNOCASE. +| 3568: 13 05 00 25 0f 17 45 4e 41 42 4c 45 20 4a 53 4f ...%..ENABLE JSO +| 3584: 4e 31 58 52 54 52 49 4d 1a 12 05 00 29 0f 19 45 N1XRTRIM....)..E +| 3600: 4e 41 42 4c 45 20 47 45 4f 50 4f 4c 59 58 42 49 NABLE GEOPOLYXBI +| 3616: 4e 41 52 59 1a 11 05 00 29 0f 19 45 5f 81 42 4c NARY....)..E_.BL +| 3632: 45 20 47 45 4f 50 4f 4c 59 58 4e 4f 43 51 53 45 E GEOPOLYXNOCQSE +| 3648: 19 10 05 00 29 0f 17 45 4e 41 42 4c 45 20 47 45 ....)..ENABLE GE +| 3664: 4f 50 4f 4c 59 58 52 54 52 49 4d 17 0f 05 00 23 OPOLYXRTRIM....# +| 3680: 0f 1a 45 4e 41 42 4c 45 20 56 54 43 35 58 42 49 ..ENABLE VTC5XBI +| 3696: 4e 41 52 59 17 0e 05 00 23 0f 19 45 4e 41 42 4c NARY....#..ENABL +| 3712: 45 20 46 54 53 35 48 4e 4f 43 41 53 45 16 1d 05 E FTS5HNOCASE... +| 3728: 00 23 0f a4 45 4e 41 42 4c 45 20 46 54 53 35 58 .#..ENABLE FTS5X +| 3744: 52 54 52 49 4d 17 0c 05 00 23 0f 19 45 4e 41 42 RTRIM....#..ENAB +| 3760: 4c 45 20 46 55 53 34 58 42 49 4e 41 52 59 17 0b LE FUS4XBINARY.. +| 3776: 05 00 23 0f 19 45 4e 41 42 4c 45 20 46 54 53 34 ..#..ENABLE FTS4 +| 3792: 57 4e 4f 43 41 53 45 16 0a 05 00 23 0f 17 45 4e WNOCASE....#..EN +| 3808: 41 42 4c 45 20 46 54 53 34 05 52 54 52 49 4d 1e ABLE FTS4.RTRIM. +| 3824: 09 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3840: 54 41 54 20 56 54 41 42 58 42 49 4e 41 52 59 1e TAT VTABXBINARY. +| 3856: 08 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3872: 54 41 54 20 56 54 41 42 58 4e 4f 43 41 53 45 1d TAT VTABXNOCASE. +| 3888: 07 05 00 31 0f 17 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3904: 54 41 54 20 56 54 41 42 58 52 54 52 49 4d 11 06 TAT VTABXRTRIM.. +| 3920: 05 00 17 0f 19 44 45 42 55 47 58 42 49 4e 41 52 .....DEBUGXBINAR +| 3936: 59 11 05 05 00 17 0f 19 44 45 42 55 47 58 4e 4f Y.......DEBUGXNO +| 3952: 43 41 53 45 10 04 05 00 17 0f 17 44 45 42 55 47 CASE.......DEBUG +| 3968: 58 52 54 52 49 4d 27 03 05 00 43 0f 19 43 4f 4d XRTRIM'...C..COM +| 3984: 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e 30 20 PILER=gcc-5.4.0 +| 4000: 32 30 31 36 30 36 30 39 58 42 49 4e 41 52 59 27 20160609XBINARY' +| 4016: 02 05 00 43 0f 19 43 4f 4d 50 49 4c 45 52 3f 87 ...C..COMPILER?. +| 4032: 63 63 2d 35 2e 34 2e 30 20 32 30 31 36 30 36 30 cc-5.4.0 2016060 +| 4048: 39 58 4e 4f 43 41 53 45 26 01 05 00 43 0f 17 43 9XNOCASE&...C..C +| 4064: 45 0d 60 59 4c 45 52 3d 67 63 63 2d 35 2e 34 00 E.`YLER=gcc-5.4. +| page 6 offset 20480 +| 3808: 06 24 03 00 12 02 01 01 06 23 03 00 12 02 01 01 .$.......#...... +| 3824: 06 22 03 01 12 02 01 01 06 21 03 00 12 03 01 01 .........!...... +| 3840: 06 20 03 00 12 03 01 01 06 1f 03 00 12 03 02 01 . .............. +| 3856: 06 1e 03 00 12 03 01 01 06 1d 03 00 12 03 01 01 ................ +| 3872: 06 1c 03 00 12 03 01 01 06 1b 03 00 12 02 01 01 ................ +| 3888: 06 1a 03 00 12 02 01 01 06 19 03 00 12 02 01 01 ................ +| 3904: 06 18 03 00 12 02 01 01 06 17 03 00 12 02 01 01 ................ +| 3920: 06 16 03 00 12 02 01 01 06 15 03 00 12 02 01 01 ................ +| 3936: 06 14 03 00 12 02 01 01 06 13 03 00 12 02 01 01 ................ +| 3952: 06 12 03 00 12 02 01 01 06 11 03 00 12 02 01 01 ................ +| 3968: 06 00 03 00 12 02 01 01 06 0f 03 00 12 02 01 01 ................ +| 3984: 06 0e 03 00 12 02 01 01 06 0d 03 00 12 02 01 01 ................ +| 4000: 06 0c 03 00 12 02 01 01 06 0b 03 10 12 02 01 01 ................ +| 4016: 06 0a 03 00 12 02 01 01 06 09 03 00 12 03 01 01 ................ +| 4032: 06 08 03 00 12 03 01 01 06 07 03 00 12 03 01 01 ................ +| 4048: 07 06 03 00 12 01 01 01 06 05 03 00 12 01 01 01 ................ +| 4064: 06 04 03 00 12 01 01 01 06 03 03 00 12 06 01 01 ................ +| 4080: 06 02 03 00 12 06 01 01 06 01 03 00 12 06 01 01 ................ +| page 7 offset 24576 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| page 8 offset 28672 +| 4048: 00 00 00 00 00 00 5d 03 02 2b 69 6e 74 00 00 00 ......]..+int... +| end crash-96b136358d01ec.db +}]} {} + +do_catchsql_test 59.1 { + SELECT (matchinfo(591,t1)) FROM t1 WHERE t1 MATCH 'e*eŸ' +} {0 {}} + +#------------------------------------------------------------------------- +do_test 60.0 { + sqlite3 db {} + sqlite3_fts5_register_matchinfo db + db deserialize [decode_hexdb { +.open --hexdb +| size 32768 pagesize 4096 filename crash-c77b90b929dc92.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 00 .....@ ........ +| 96: 00 00 00 00 0d 0f c7 00 07 0d 92 00 0f 8d 0f 36 ...............6 +| 112: 0e cb 0e 6b 0e 0e 0d b6 0d 92 00 00 00 00 00 00 ...k............ +| 3472: 00 00 22 08 06 17 11 11 01 31 74 61 62 6c 65 74 .........1tablet +| 3488: 32 74 32 08 43 52 45 41 54 45 20 54 41 42 4c 45 2t2.CREATE TABLE +| 3504: 20 74 32 28 78 29 56 07 06 17 1f 1f 01 7d 74 61 t2(x)V.......ta +| 3520: 62 6c 65 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 blet1_configt1_c +| 3536: 6f 6e 66 69 67 07 43 52 45 41 54 45 20 54 41 42 onfig.CREATE TAB +| 3552: 4c 45 20 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b LE 't1_config'(k +| 3568: 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 PRIMARY KEY, v) +| 3584: 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 5b 06 WITHOUT ROWID[. +| 3600: 07 17 21 21 01 81 01 74 61 62 6c 65 74 31 5f 64 ..!!...tablet1_d +| 3616: 6f 63 73 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 ocsizet1_docsize +| 3632: 06 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 .CREATE TABLE 't +| 3648: 31 5f 64 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 1_docsize'(id IN +| 3664: 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE +| 3680: 59 2c 20 73 7a 20 42 4c 4f 42 29 5e 05 07 17 21 Y, sz BLOB)^...! +| 3696: 21 01 81 07 74 61 62 6c 65 74 31 5f 63 6f 6e 74 !...tablet1_cont +| 3712: 65 6e 74 74 31 5f 63 6f 6e 74 65 6e 74 05 43 52 entt1_content.CR +| 3728: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 EATE TABLE 't1_c +| 3744: 6f 6e 74 65 6e 74 27 28 69 64 20 49 4e 54 45 47 ontent'(id INTEG +| 3760: 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 ER PRIMARY KEY, +| 3776: 63 30 2c 20 63 31 2c d6 63 32 29 69 04 07 17 19 c0, c1,.c2)i.... +| 3792: 19 01 81 2d 74 61 62 6c 65 74 31 5f 69 64 78 74 ...-tablet1_idxt +| 3808: 31 5f 69 64 78 04 43 52 45 41 54 45 20 54 41 42 1_idx.CREATE TAB +| 3824: 4c 45 20 27 74 31 5f 69 64 78 27 28 73 65 67 69 LE 't1_idx'(segi +| 3840: 64 2c 20 74 65 72 6d 2c 20 70 67 6e 6f 2c 20 50 d, term, pgno, P +| 3856: 52 49 4d 41 52 59 20 4b 45 59 28 73 65 67 69 64 RIMARY KEY(segid +| 3872: 2c 20 74 65 72 6d 29 29 20 57 49 54 48 4f 55 54 , term)) WITHOUT +| 3888: 20 52 4f 57 49 44 55 03 07 17 1b 1b 01 81 01 74 ROWIDU........t +| 3904: 61 62 6c 65 74 31 5f 64 61 74 61 74 31 5f 64 61 ablet1_datat1_da +| 3920: 74 61 03 43 52 45 41 54 45 20 54 41 42 4c 45 20 ta.CREATE TABLE +| 3936: 27 74 31 5f 64 61 74 61 27 28 69 64 20 49 4e 54 't1_data'(id INT +| 3952: 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 EGER PRIMARY KEY +| 3968: 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 29 38 02 06 , block BLOB)8.. +| 3984: 17 11 11 08 5f 74 61 62 6c 65 74 31 74 31 43 52 ...._tablet1t1CR +| 4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 35 LE t1 USING fts5 +| 4032: 28 61 2c 62 2c 63 29 00 00 00 00 00 00 00 00 00 (a,b,c)......... +| page 3 offset 8192 +| 0: 0d 00 00 00 03 0c 93 ff 0f e6 0f ef 0c 94 00 00 ................ +| 3216: 00 00 00 00 86 4a 84 80 80 80 80 01 04 00 8d 18 .....J.......... +| 3232: 00 00 03 2b 02 30 30 01 02 06 01 02 06 01 02 06 ...+.00......... +| 3248: 1f 02 03 01 02 03 01 02 03 01 08 32 31 31 36 30 ...........21160 +| 3264: 36 30 39 01 02 07 01 02 07 01 02 07 01 01 33 f1 609...........3. +| 3280: 02 05 01 02 05 01 02 05 01 01 35 01 02 03 01 02 ..........5..... +| 3296: 04 01 02 04 02 07 30 30 30 30 30 30 30 1c 02 3d ......0000000..= +| 3312: 01 02 04 01 02 04 01 06 62 69 6e 61 72 79 03 06 ........binary.. +| 3328: 01 02 02 03 06 01 01 f2 03 06 4e 02 02 03 06 01 ..........N..... +| 3344: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3360: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 ................ +| 3376: 03 06 01 02 02 03 06 01 02 02 01 08 63 6f 6d 70 ............comp +| 3392: 69 6c 65 72 01 02 02 01 02 02 01 02 02 01 06 64 iler...........d +| 3408: 62 73 74 61 74 07 02 03 01 02 13 01 02 03 02 04 bstat........... +| 3424: 65 62 75 67 04 02 02 01 02 02 01 02 02 01 07 65 ebug...........e +| 3440: 6e 61 62 6c 65 07 02 02 01 02 02 01 02 02 01 02 nable........... +| 3456: 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 ................ +| 3472: 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 ................ +| 3488: 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 ................ +| 3504: 02 01 02 02 02 08 76 b4 65 6e 73 69 6f 6e 1f 02 ......v.ension.. +| 3520: 04 01 02 04 01 02 04 01 04 66 74 73 34 0a 02 03 .........fts4... +| 3536: 01 02 03 01 02 03 04 01 25 0d 02 03 01 02 03 01 ........%....... +| 3552: 02 03 01 03 67 63 63 01 02 03 01 02 03 01 02 03 ....gcc......... +| 3568: 02 06 65 6f 70 6f 6c 79 0f f2 03 01 02 03 01 02 ..eopoly........ +| 3584: 03 01 05 6a 73 6f 6e 31 13 02 03 01 02 03 01 02 ...json1........ +| 3600: 03 01 04 6c 6f 61 64 1f 02 03 01 02 03 01 02 03 ...load......... +| 3616: 00 03 6d 61 78 1c 02 0c 01 02 02 01 02 02 02 05 ..max........... +| 3632: 65 6d 6f 72 79 1c 02 03 01 02 03 01 02 03 04 04 emory........... +| 3648: 73 79 73 35 16 02 03 01 02 03 01 02 03 01 06 6e sys5...........n +| 3664: 6f 63 61 73 65 02 06 01 02 02 13 06 00 f2 02 03 ocase........... +| 3680: 06 01 12 02 13 06 01 02 02 03 06 01 02 02 03 06 ................ +| 3696: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3712: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3728: 02 01 04 6f 6d 69 74 1f 02 02 01 02 02 01 02 02 ...omit......... +| 3744: 01 05 72 74 72 65 65 19 02 03 01 02 03 01 02 03 ..rtree......... +| 3760: 04 02 69 6d 01 06 01 02 02 03 06 01 02 02 03 06 ..im............ +| 3776: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3792: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3808: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 ................ +| 3824: 01 0a 74 68 72 65 61 64 73 61 66 65 22 02 02 01 ..threadsafe.... +| 3840: 02 02 01 02 02 01 04 76 74 61 62 07 02 04 01 02 .......vtab..... +| 3856: 04 01 02 04 01 01 78 01 06 01 01 02 01 06 01 01 ......x......... +| 3872: 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 ................ +| 3888: 01 06 01 11 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 3904: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 3920: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 ................ +| 3936: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 ................ +| 3952: 02 01 06 01 01 01 f1 06 01 01 02 ad 06 01 01 02 ................ +| 3968: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 3984: 06 01 01 01 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 4000: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 ................ +| 4016: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 ................ +| 4032: 02 01 06 01 01 02 01 06 01 01 02 04 15 13 0c 0c ................ +| 4048: 12 44 13 11 0f 47 13 0e fc 0e 11 10 0f 0e 10 0f .D...G.......... +| 4064: 44 0f 10 40 15 0f 07 01 03 00 14 24 5a 24 24 0f D..@.......$Z$$. +| 4080: 0a 03 00 24 00 00 00 00 01 01 01 00 01 01 01 01 ...$............ +| page 4 offset 12288 +| 0: 0a 00 00 00 01 0f 00 00 00 00 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 05 04 09 0c 01 02 ................ +| page 5 offset 16384 +| 0: 0d 00 00 00 24 0c 0a 00 0f 00 00 00 00 00 00 00 ....$........... +| 3072: 00 00 00 00 00 00 00 00 00 00 18 24 05 00 25 0f ...........$..%. +| 3088: 19 54 48 52 45 41 44 53 41 46 45 3d 30 58 42 49 .THREADSAFE=0XBI +| 3104: 4e 41 52 59 18 23 05 00 25 0f 19 54 48 52 45 41 NARY.#..%..THREA +| 3120: 44 53 41 46 45 3d 30 58 4e 4f 43 41 53 45 17 8f DSAFE=0XNOCASE.. +| 3136: 05 00 25 0f 17 54 48 52 45 41 44 43 41 46 45 3d ..%..THREADCAFE= +| 3152: 30 58 52 54 52 49 4d 1f 21 05 00 33 0f 19 4f 4d 0XRTRIM.!..3..OM +| 3168: 49 54 20 4b 4f 41 44 21 45 58 54 45 4e 53 49 4f IT KOAD!EXTENSIO +| 3184: 4e 58 42 49 4e 41 52 59 1f 20 05 00 33 0f 19 4f NXBINARY. ..3..O +| 3200: 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 MIT LOAD EXTENSI +| 3216: 4f 4e 58 4e 4f 43 41 53 45 1e 1f 05 00 33 0f 17 ONXNOCASE....3.. +| 3232: 4f 4d 59 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 OMYT LOAD EXTENS +| 3248: 49 4f 4e 58 52 54 56 a9 4d 1f 1e 05 00 33 0f 19 IONXRTV.M....3.. +| 3264: 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 30 MAX MEMORY=50000 +| 3280: 30 30 30 57 42 49 4e 31 52 59 1f 1d 05 00 33 0f 000WBIN1RY....3. +| 3296: 19 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 .MAX MEMORY=5000 +| 3312: 30 30 30 30 58 4e 4f 43 41 53 45 1e 1c 05 00 32 0000XNOCASE....2 +| 3328: 0f 17 4e 41 58 20 4d 45 4d 4f 52 59 2d 35 30 30 ..NAX MEMORY-500 +| 3344: 30 30 30 30 30 58 52 54 52 49 4d 18 1b 05 00 25 00000XRTRIM....% +| 3360: 0f 19 45 4e 41 42 4c 45 20 52 54 52 45 45 58 42 ..ENABLE RTREEXB +| 3376: 49 4e 41 52 59 18 1a 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3392: 4c 45 20 52 54 52 45 45 59 4e 4f 43 41 53 45 17 LE RTREEYNOCASE. +| 3408: 19 66 00 25 0f 17 45 4e 41 42 4c 45 20 52 54 52 .f.%..ENABLE RTR +| 3424: 45 45 58 52 54 52 49 4d 1a 18 05 00 29 0f 19 45 EEXRTRIM....)..E +| 3440: 4e 41 42 4c 45 20 4d 45 4d 53 59 53 35 58 42 49 NABLE MEMSYS5XBI +| 3456: 4e 41 52 59 1a 17 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3472: 45 20 4d 45 4d 53 59 53 35 58 4e 4f 43 41 53 45 E MEMSYS5XNOCASE +| 3488: 19 16 05 00 29 0f 17 45 4e 41 42 4c 45 20 4d 45 ....)..ENABLE ME +| 3504: 4d 53 59 53 35 58 52 54 52 49 4d 18 15 05 10 25 MSYS5XRTRIM....% +| 3520: 0f 19 45 4e 40 42 4c 45 20 4a 53 4f 4e 31 58 42 ..EN@BLE JSON1XB +| 3536: 49 4e 41 52 59 18 14 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3552: 4c 45 20 4a 53 4f 4e 32 58 4e 4f 43 41 53 45 17 LE JSON2XNOCASE. +| 3568: 13 05 00 25 0f 17 45 4e 41 42 4c 45 20 4a 53 4f ...%..ENABLE JSO +| 3584: 4e 31 58 52 54 52 49 4d 1a 12 05 00 29 0f 19 45 N1XRTRIM....)..E +| 3600: 4e 41 42 4c 45 20 47 45 4f 50 4f 4c 59 58 42 49 NABLE GEOPOLYXBI +| 3616: 4e 41 52 59 1a 11 05 00 29 0f 19 45 4f 81 42 4c NARY....)..EO.BL +| 3632: 45 20 47 45 4f 50 4f 4c 59 58 4e 4f 43 51 53 45 E GEOPOLYXNOCQSE +| 3648: 19 10 05 00 29 0f 17 45 4e 41 42 4c 45 20 47 45 ....)..ENABLE GE +| 3664: 4f 50 4f 4c 59 58 52 54 52 49 4d 17 0f 05 00 23 OPOLYXRTRIM....# +| 3680: 0f 1a 45 4e 41 42 4c 45 20 46 54 53 35 58 42 49 ..ENABLE FTS5XBI +| 3696: 4e 41 52 59 17 0e 05 00 23 0f 19 45 4e 41 42 4c NARY....#..ENABL +| 3712: 45 20 46 54 53 35 48 4e 4f 43 41 53 45 16 1d 05 E FTS5HNOCASE... +| 3728: 00 23 0f a4 45 4e 41 42 4c 45 20 46 54 53 35 58 .#..ENABLE FTS5X +| 3744: 52 54 52 49 4d 17 0c 05 00 23 0f 19 45 4e 41 42 RTRIM....#..ENAB +| 3760: 4c 45 20 46 55 53 34 58 42 49 4e 41 52 59 17 0b LE FUS4XBINARY.. +| 3776: 05 00 23 0f 19 45 4e 41 42 4c 45 20 46 54 53 34 ..#..ENABLE FTS4 +| 3792: 57 4e 4f 43 41 53 45 16 0a 05 00 23 0f 17 45 4e WNOCASE....#..EN +| 3808: 41 42 4c 45 20 46 54 53 34 05 52 54 52 49 4d 1e ABLE FTS4.RTRIM. +| 3824: 09 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3840: 54 41 54 20 56 54 41 42 58 42 49 4e 41 52 59 1e TAT VTABXBINARY. +| 3856: 08 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3872: 54 41 54 20 56 54 41 42 58 4e 4f 43 41 53 45 1d TAT VTABXNOCASE. +| 3888: 07 05 00 31 0f 17 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3904: 54 41 54 20 56 54 41 42 58 52 54 52 49 4d 11 06 TAT VTABXRTRIM.. +| 3920: 05 00 17 0f 19 44 45 42 55 47 58 42 8a 4e 41 52 .....DEBUGXB.NAR +| 3936: 59 11 05 05 00 17 0f 19 44 45 42 55 47 58 4e 4f Y.......DEBUGXNO +| 3952: 43 41 53 45 10 04 05 00 17 0f 17 44 45 42 55 47 CASE.......DEBUG +| 3968: 58 52 54 52 49 4d 27 03 05 00 43 0f 19 43 4f 4d XRTRIM'...C..COM +| 3984: 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e 30 20 PILER=gcc-5.4.0 +| 4000: 32 30 31 36 30 36 30 39 58 42 49 4e 41 52 59 27 20160609XBINARY' +| 4016: 02 05 00 43 0f 19 43 4f 4d 50 49 4c 45 52 3f 87 ...C..COMPILER?. +| 4032: 63 63 2d 35 2e 34 2e 30 20 32 30 31 36 30 36 30 cc-5.4.0 2016060 +| 4048: 39 58 4e 4f 43 41 53 45 26 01 05 00 43 0f 17 43 9XNOCASE&...C..C +| 4064: 45 0d 60 59 4c 45 52 3d 67 63 63 2d 35 2e 34 2e E.`YLER=gcc-5.4. +| 4080: 30 20 32 30 31 36 30 36 30 39 68 52 54 52 49 4d 0 20160609hRTRIM +| page 6 offset 20480 +| 0: 0d 00 00 00 24 0e 00 00 00 00 00 00 00 00 00 00 ....$........... +| 3808: 06 24 03 00 12 02 01 01 06 23 03 00 12 02 01 01 .$.......#...... +| 3824: 06 22 03 01 12 02 01 01 06 21 03 00 12 03 01 01 .........!...... +| 3840: 06 20 03 00 12 03 01 01 06 1f 03 00 12 03 02 01 . .............. +| 3856: 06 1e 03 00 12 03 01 01 06 1d 03 00 12 03 01 01 ................ +| 3872: 06 1c 03 00 12 03 01 01 06 1b 03 00 12 02 01 01 ................ +| 3888: 06 1a 03 00 12 02 01 01 06 19 03 00 12 02 01 01 ................ +| 3904: 06 18 03 00 12 02 01 01 06 17 03 00 12 02 01 01 ................ +| 3920: 06 16 03 00 12 02 01 01 06 15 03 00 12 02 01 01 ................ +| 3936: 06 14 03 00 12 02 01 01 06 13 03 00 12 02 01 01 ................ +| 3952: 06 12 03 00 12 02 01 01 06 11 03 00 12 02 01 01 ................ +| 3968: 06 00 03 00 12 02 01 01 06 0f 03 00 12 02 01 01 ................ +| 3984: 06 0e 03 00 12 02 01 01 06 0d 03 00 12 02 01 01 ................ +| 4000: 06 0c 03 00 12 02 01 01 06 0b 03 00 12 02 01 01 ................ +| 4016: 06 0a 03 00 12 02 01 01 06 09 03 00 12 03 01 01 ................ +| 4032: 06 08 03 00 12 03 01 01 06 07 03 00 12 03 01 01 ................ +| 4048: 06 06 03 00 12 01 01 01 06 05 03 00 12 01 01 01 ................ +| 4064: 06 04 03 00 12 01 01 01 06 03 03 00 12 06 01 01 ................ +| 4080: 06 02 03 00 12 06 01 01 06 01 03 00 12 06 01 01 ................ +| page 7 offset 24576 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| page 8 offset 28672 +| 4048: 00 00 00 00 00 00 5d 03 00 00 00 00 00 00 00 00 ......]......... +| end crash-c77b90b929dc92.db +}]} {} + + +do_catchsql_test 60.2 { + SELECT (matchinfo(t1,591)) FROM t1 WHERE t1 MATCH 'e*eŸ' +} {/.*fts5: corrupt.*/} + +#------------------------------------------------------------------------- +do_test 61.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 28672 pagesize 4096 filename crash-e5fa281edabddf.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 00 .....@ ........ +| 96: 00 00 00 00 0d 0f c7 00 06 0d b6 00 0f 8d 0f 36 ...............6 +| 112: 0e cb 0e 6b 0e 0e 0d b6 00 00 00 00 00 00 00 00 ...k............ +| 3504: 00 00 00 00 00 00 56 07 06 17 1f 1f 01 7d 74 61 ......V.......ta +| 3520: 62 6c 65 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 blet1_configt1_c +| 3536: 6f 6e 66 69 67 07 43 52 45 41 54 45 20 54 41 42 onfig.CREATE TAB +| 3552: 4c 45 20 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b LE 't1_config'(k +| 3568: 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 PRIMARY KEY, v) +| 3584: 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 5b 06 WITHOUT ROWID[. +| 3600: 07 17 21 21 01 81 01 74 51 62 6c 65 74 31 5f 64 ..!!...tQblet1_d +| 3616: 6f 63 73 69 7a 65 74 31 5f 63 6f 63 73 69 7a 65 ocsizet1_cocsize +| 3632: 06 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 .CREATE TABLE 't +| 3648: 31 5f 64 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 1_docsize'(id IN +| 3664: 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE +| 3680: 59 2c 20 73 7a 20 42 4c 4f 42 29 5e 05 07 17 21 Y, sz BLOB)^...! +| 3696: 21 01 81 07 74 61 62 6c 65 74 31 5f 63 6f 6e 74 !...tablet1_cont +| 3712: 65 6e 74 74 31 5f 63 6f 6e 74 65 6e 74 05 43 52 entt1_content.CR +| 3728: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 EATE TABLE 't1_c +| 3744: 6f 6e 74 65 6e 74 27 28 69 64 20 49 4e 54 45 47 ontent'(id INTEG +| 3760: 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 ER PRIMARY KEY, +| 3776: 63 30 2c 20 63 31 2c 20 63 32 29 69 04 07 17 19 c0, c1, c2)i.... +| 3792: 19 01 81 2d 74 61 62 6c 65 74 31 5f 69 64 78 74 ...-tablet1_idxt +| 3808: 31 5f 69 64 78 04 43 52 45 41 54 45 20 54 41 42 1_idx.CREATE TAB +| 3824: 4c 45 20 27 74 31 5f 69 64 78 27 28 73 65 67 69 LE 't1_idx'(segi +| 3840: 64 2c 20 74 65 72 6d 2c 20 70 67 6e 6f 2c 20 50 d, term, pgno, P +| 3856: 52 49 4d 41 52 59 20 4b 45 59 28 73 65 67 69 64 RIMARY KEY(segid +| 3872: 2c 20 74 65 72 6d 29 29 20 57 49 54 48 4f 55 54 , term)) WITHOUT +| 3888: 20 52 4f 57 49 44 55 03 07 17 1b 1b 01 81 01 74 ROWIDU........t +| 3904: 61 62 6c 65 74 31 5f 64 61 74 61 74 31 5f 64 61 ablet1_datat1_da +| 3920: 74 61 03 43 52 45 41 54 45 20 54 41 42 4c 45 20 ta.CREATE TABLE +| 3936: 27 74 31 5f 64 61 74 61 27 28 69 64 20 49 4e 54 't1_data'(id INT +| 3952: 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 EGER PRIMARY KEY +| 3968: 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 29 38 02 06 , block BLOB)8.. +| 3984: 17 11 11 08 5f 74 61 62 6c 65 74 ea 74 31 43 52 ...._tablet.t1CR +| 4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 35 LE t1 USING fts5 +| 4032: 28 61 2c 62 2c 63 29 00 00 00 00 00 00 00 00 00 (a,b,c)......... +| page 3 offset 8192 +| 0: 0d 00 00 00 03 0c 94 00 0f e6 0f ef 0c 94 00 00 ................ +| 3216: 00 00 00 00 86 4a 84 80 80 80 80 01 04 00 8d 18 .....J.......... +| 3232: 00 00 03 2b 02 30 30 01 02 06 01 02 06 01 02 06 ...+.00......... +| 3248: 1f 02 13 01 02 03 01 02 03 01 08 32 30 31 36 30 ...........20160 +| 3264: 36 30 39 01 02 07 01 02 07 01 02 07 01 01 34 01 609...........4. +| 3280: 02 05 01 02 05 01 02 05 01 01 35 01 02 04 01 02 ..........5..... +| 3296: 04 01 02 04 02 07 30 30 30 30 30 30 30 1c 02 04 ......0000000... +| 3312: 01 02 04 01 02 04 01 06 62 69 6e 61 72 79 03 06 ........binary.. +| 3328: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3344: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3360: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 ................ +| 3376: 03 06 01 02 02 03 06 01 02 02 01 08 63 6f 6d 70 ............comp +| 3392: 69 6c 65 72 01 02 02 01 02 02 01 02 02 01 06 64 iler...........d +| 3408: 62 73 74 61 74 07 02 03 01 02 03 01 02 03 02 04 bstat........... +| 3424: 65 62 75 67 04 02 02 01 02 02 01 02 02 01 06 65 ebug...........e +| 3440: 6e 61 62 6c 65 07 02 02 01 02 02 01 02 02 01 02 nable........... +| 3456: 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 ................ +| 3472: 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 ................ +| 3488: 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 ................ +| 3504: 02 01 02 02 02 08 78 74 65 6e 73 69 6f 6e 1f 02 ......xtension.. +| 3520: 04 01 02 04 01 02 04 01 04 66 74 73 34 0a 02 03 .........fts4... +| 3536: 01 02 03 01 02 03 04 01 35 0d 02 03 01 02 03 01 ........5....... +| 3552: 02 03 01 03 67 63 63 01 02 03 01 02 03 01 02 03 ....gcc......... +| 3568: 02 06 65 6f 70 6f 6c 79 10 02 03 01 02 03 01 02 ..eopoly........ +| 3584: 03 01 05 6a 73 6f 6e 31 13 02 03 01 02 03 01 02 ...json1........ +| 3600: 03 01 04 6c 6f 61 64 1f 02 03 01 02 03 01 02 03 ...load......... +| 3616: 01 03 6d 61 78 1c 02 02 01 02 02 01 02 02 02 05 ..max........... +| 3632: 65 6d 6f 72 79 1c 02 03 01 02 03 01 02 03 04 04 emory........... +| 3648: 73 79 73 35 16 02 03 01 02 03 11 02 03 01 06 6e sys5...........n +| 3664: 6f 63 61 73 65 02 06 01 02 02 03 06 01 02 02 03 ocase........... +| 3680: 06 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 ................ +| 3696: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3712: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3728: 02 01 04 6f 6d 69 74 1f 02 02 01 02 02 01 02 02 ...omit......... +| 3744: 01 05 72 74 72 65 65 19 02 03 01 02 03 01 02 03 ..rtree......... +| 3760: 04 02 69 6d 01 06 01 02 02 03 06 01 02 02 03 06 ..im............ +| 3776: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3792: 02 02 03 06 01 02 02 03 06 01 02 01 13 05 01 02 ................ +| 3808: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 ................ +| 3824: 01 0a 74 68 72 65 61 64 73 61 66 65 22 02 02 01 ..threadsafe.... +| 3840: 02 02 01 01 02 01 04 76 74 61 62 07 02 04 01 02 .......vtab..... +| 3856: 04 01 02 04 01 01 78 01 06 01 01 02 01 06 01 01 ......x......... +| 3872: 02 0e 16 01 01 02 01 06 01 01 02 01 06 01 02 02 ................ +| 3888: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 3904: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 3920: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 ................ +| 3936: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 ................ +| 3952: 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 ................ +| 3968: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 3984: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 4000: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 07 01 ................ +| 4016: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 ................ +| 4032: 02 01 06 01 01 02 01 06 01 01 02 04 15 13 0c 0c ................ +| 4048: 12 44 13 11 0f 47 13 0f 0c 0e 11 10 0f 0e 10 0f .D...G.......... +| 4064: 44 0f 10 40 15 0f 07 01 03 00 14 24 5a 24 24 0f D..@.......$Z$$. +| 4080: 0a 03 00 24 00 00 00 00 01 01 01 00 01 01 01 01 ...$............ +| page 4 offset 12288 +| 0: 0a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 05 04 09 0c 01 02 ................ +| page 5 offset 16384 +| 0: 0d 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 3072: 00 00 00 00 00 00 00 00 00 00 18 24 05 00 25 0f ...........$..%. +| 3088: 19 54 48 52 45 41 44 53 41 46 45 3d 30 58 42 49 .THREADSAFE=0XBI +| 3104: 4e 41 52 59 18 e2 05 00 25 0f 19 54 48 52 45 41 NARY....%..THREA +| 3120: 44 53 41 46 45 3d 30 58 4e 4f 43 41 53 45 17 22 DSAFE=0XNOCASE.. +| 3136: 05 00 25 0f 17 54 48 52 45 41 44 53 41 46 45 3d ..%..THREADSAFE= +| 3152: 30 58 52 54 52 49 4d 1f 21 05 00 33 0f 19 4f 4d 0XRTRIM.!..3..OM +| 3168: 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 4f IT LOAD EXTENSIO +| 3184: 4e 58 42 49 4e 40 52 59 1f 20 05 00 33 0f 19 4f NXBIN@RY. ..3..O +| 3200: 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 MIT LOAD EXTENSI +| 3216: 4f 4e 58 4e 4f 43 41 53 45 1e 1f 05 00 33 0f 17 ONXNOCASE....3.. +| 3232: 4f 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 OMIT LOAD EXTENS +| 3248: 49 4f 4e 58 52 54 52 49 4d 1f 1e 05 00 33 0f 19 IONXRTRIM....3.. +| 3264: 4d 41 58 20 4e 45 4d 4f 52 59 3d 35 30 30 30 30 MAX NEMORY=50000 +| 3280: 30 30 30 58 42 49 4e 41 52 59 1f 1d 05 00 33 0f 000XBINARY....3. +| 3296: 19 4d 41 58 20 4d 45 4d 4f 52 59 3d 45 30 30 30 .MAX MEMORY=E000 +| 3312: 30 30 30 30 58 4e 4f 43 41 53 45 1e 1c 05 00 33 0000XNOCASE....3 +| 3328: 0f 17 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 ..MAX MEMORY=500 +| 3344: 30 30 30 30 30 58 52 54 52 49 4d 18 1b 05 00 25 00000XRTRIM....% +| 3360: 0f 19 45 4e 41 42 4c 45 20 52 54 52 45 45 58 42 ..ENABLE RTREEXB +| 3376: 49 4e 41 52 59 18 1a 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3392: 4c 45 20 52 54 52 45 45 58 4e 4f 43 41 53 45 17 LE RTREEXNOCASE. +| 3408: 19 05 00 25 0f 17 45 4e 41 42 4c 45 20 20 54 52 ...%..ENABLE TR +| 3424: 45 45 58 52 54 52 49 4d 1a 18 05 00 29 0f 19 45 EEXRTRIM....)..E +| 3440: 4e 41 42 4c 45 20 4d 45 4d 53 59 53 35 58 42 49 NABLE MEMSYS5XBI +| 3456: 4e 41 52 59 1a 17 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3472: 45 20 4d 45 4d 53 59 53 35 58 4e 4f 43 41 53 45 E MEMSYS5XNOCASE +| 3488: 19 16 05 00 29 0f 17 45 4e 41 42 4c 45 20 4d 45 ....)..ENABLE ME +| 3504: 4d 53 59 53 35 58 52 54 52 49 4d 18 15 05 00 25 MSYS5XRTRIM....% +| 3520: 0f 19 45 4e 41 42 4c 45 20 4a 53 4f 4e 31 58 42 ..ENABLE JSON1XB +| 3536: 49 4e 41 52 59 18 14 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3552: 4c 45 20 4a 53 4f 4e 31 58 4e 4f 43 41 53 45 17 LE JSON1XNOCASE. +| 3568: 13 05 00 25 0f 17 45 4e 41 42 4c 45 20 4a 53 4f ...%..ENABLE JSO +| 3584: 4e 31 58 52 54 52 49 4d 1a 12 05 00 29 0f 19 45 N1XRTRIM....)..E +| 3600: 4e 41 42 4c 45 20 47 45 4f 50 4f 4c 59 58 42 49 NABLE GEOPOLYXBI +| 3616: 4e 41 52 59 1a 11 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3632: 45 20 47 45 4f 50 4f 4c 59 58 4e 4f 43 41 53 45 E GEOPOLYXNOCASE +| 3648: 19 10 05 00 29 0f 17 45 4e 41 42 4c 45 20 47 45 ....)..ENABLE GE +| 3664: 4f 50 4f 4c 59 58 52 54 52 49 4d 17 0f 05 00 23 OPOLYXRTRIM....# +| 3680: 0f 19 45 4e 41 42 4c 45 20 e5 54 53 35 58 42 49 ..ENABLE .TS5XBI +| 3696: 4e 41 52 59 17 0e 05 00 23 0f 19 45 4e 41 42 4d NARY....#..ENABM +| 3712: 45 b5 46 54 53 35 58 4e 4f 43 41 53 45 16 0d 05 E.FTS5XNOCASE... +| 3728: 00 23 0f 17 45 4e 41 42 4c 45 20 46 54 53 35 58 .#..ENABLE FTS5X +| 3744: 52 54 52 49 4d 17 0c 05 00 23 0f 19 45 4e 41 42 RTRIM....#..ENAB +| 3760: 4c 45 20 46 54 53 34 58 42 b7 4e 41 52 59 17 0b LE FTS4XB.NARY.. +| 3776: 05 00 23 0f 19 45 4e 41 42 4c 45 20 46 54 53 34 ..#..ENABLE FTS4 +| 3792: 58 4e 4f 43 41 53 45 16 0a 05 00 23 0f 17 45 4e XNOCASE....#..EN +| 3808: 41 42 4c 45 20 46 54 53 34 58 52 54 52 49 4d 1e ABLE FTS4XRTRIM. +| 3824: 09 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3840: 54 41 54 20 56 54 41 42 58 42 49 4e 41 52 59 1e TAT VTABXBINARY. +| 3856: 08 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3872: 54 41 54 20 56 54 41 42 58 4e 4f 43 41 53 45 1d TAT VTABXNOCASE. +| 3888: 07 05 00 31 0f 17 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3904: 54 41 54 20 56 54 41 42 58 52 54 52 49 4d 11 06 TAT VTABXRTRIM.. +| 3920: 05 00 17 0f 19 44 45 42 55 47 58 42 49 4e 41 52 .....DEBUGXBINAR +| 3936: 59 11 05 05 00 17 0f 19 44 45 42 55 47 58 4e 4f Y.......DEBUGXNO +| 3952: 43 41 53 45 10 04 05 00 17 0f 17 44 45 42 55 47 CASE.......DEBUG +| 3968: 58 52 54 52 49 4d 27 03 05 00 43 0f 19 43 4f 4d XRTRIM'...C..COM +| 3984: 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e 30 20 PILER=gcc-5.4.0 +| 4000: 32 30 31 36 30 36 30 39 58 42 49 4e 41 52 59 27 20160609XBINARY' +| 4016: 02 05 00 43 0f 19 43 4f 4d 50 49 4c 45 52 3d 67 ...C..COMPILER=g +| 4032: 63 63 2d 35 2e 34 2e 30 20 32 30 31 36 30 36 30 cc-5.4.0 2016060 +| 4048: 39 58 4e 4f 43 41 53 45 26 01 05 00 43 0f 17 53 9XNOCASE&...C..S +| 4064: 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e OMPILER=gcc-5.4. +| 4080: 30 20 32 2f 31 00 00 00 00 00 00 00 00 00 00 00 0 2/1........... +| page 6 offset 20480 +| 3808: 06 24 03 00 12 02 01 01 06 23 03 00 12 02 01 01 .$.......#...... +| 3824: 06 22 03 00 12 02 01 01 06 21 03 00 12 03 01 01 .........!...... +| 3840: 06 20 03 00 12 03 01 01 06 1f 03 00 12 03 01 01 . .............. +| 3856: 06 1e 03 00 12 03 01 01 06 1d 03 00 12 03 01 01 ................ +| 3872: 06 1c 03 00 12 03 01 01 06 1b 03 00 12 02 01 01 ................ +| 3888: 06 1a 03 00 12 02 01 01 06 19 03 10 12 02 01 01 ................ +| 3904: 06 18 03 00 12 02 01 01 06 17 03 00 12 02 01 01 ................ +| 3920: 06 16 03 00 12 02 01 01 06 15 03 00 12 02 01 01 ................ +| 3936: 06 14 03 00 12 02 01 01 06 13 03 00 12 02 01 01 ................ +| 3952: 06 12 03 00 12 02 01 01 06 11 03 00 12 02 01 01 ................ +| 3968: 06 10 03 00 12 02 01 01 06 0f 03 00 12 02 01 01 ................ +| 3984: 06 0e 03 00 12 02 01 01 06 0d 03 00 12 02 01 01 ................ +| 4000: 06 0c 03 00 12 02 01 01 06 0b 03 00 12 02 01 01 ................ +| 4016: 06 0a 03 00 12 02 01 01 06 09 03 00 12 03 01 01 ................ +| 4032: 06 08 03 00 12 03 01 01 06 07 03 00 12 03 01 01 ................ +| 4048: 06 06 03 00 12 01 01 01 06 05 03 01 12 01 01 01 ................ +| 4064: 06 04 03 00 12 01 01 01 06 03 03 00 12 06 01 01 ................ +| 4080: 06 02 03 00 12 06 01 01 06 01 03 00 12 06 01 01 ................ +| page 7 offset 24576 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| end crash-e5fa281edabddf.db +}]} {} + +do_catchsql_test 61.1 { + CREATE VIRTUAL TABLE t3 USING fts5vocab('t1'(),'col' ); +} {/*malformed database schema*/} + +do_catchsql_test 61.2 { + SELECT * FROM t3 ORDER BY rowid; +} {/*malformed database schema*/} + +#------------------------------------------------------------------------- +do_test 62.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 28672 pagesize 4096 filename crash-44942694542e1e.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 00 .....@ ........ +| 96: 00 00 00 00 0d 0f c7 00 07 0d 92 00 0f 8d 0f 36 ...............6 +| 112: 0e cb 0e 6b 0e 0e 0d b6 0d 92 00 00 00 00 00 00 ...k............ +| 3472: 00 00 22 08 06 17 11 11 01 31 74 61 62 6c 65 74 .........1tablet +| 3488: 32 74 32 08 43 52 45 41 54 45 20 54 41 42 4c 45 2t2.CREATE TABLE +| 3504: 20 74 32 28 78 29 56 07 06 17 1f 1f 01 7d 74 61 t2(x)V.......ta +| 3520: 62 6c 65 74 31 5f 63 6f 6e 66 79 67 74 31 5f 63 blet1_confygt1_c +| 3536: 6f 6e 66 69 67 07 43 52 45 41 54 45 20 54 41 42 onfig.CREATE TAB +| 3552: 4c 45 20 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b LE 't1_config'(k +| 3568: 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 PRIMARY KEY, v) +| 3584: 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 5b 06 WITHOUT ROWID[. +| 3600: 07 17 21 21 01 81 01 74 61 62 6c 65 74 31 5f 64 ..!!...tablet1_d +| 3616: 6f 63 73 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 ocsizet1_docsize +| 3632: 06 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 .CREATE TABLE 't +| 3648: 31 5f 64 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 1_docsize'(id IN +| 3664: 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE +| 3680: 59 2c 20 73 7a 20 52 4c 4f 42 29 5e 05 07 17 21 Y, sz RLOB)^...! +| 3696: 21 01 81 07 74 61 62 6c 65 74 31 5f 63 6f 6e 74 !...tablet1_cont +| 3712: 65 6e 74 74 35 ff 63 6f 6e 74 65 6e 74 05 43 52 entt5.content.CR +| 3728: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 EATE TABLE 't1_c +| 3744: 6f 6e 74 65 6e 74 27 28 69 64 20 49 4e 54 45 47 ontent'(id INTEG +| 3760: 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 ER PRIMARY KEY, +| 3776: 63 30 2c 20 63 31 2c 20 63 42 29 69 04 07 17 19 c0, c1, cB)i.... +| 3792: 19 01 81 2d 74 61 62 6c 65 74 31 5f 69 64 78 74 ...-tablet1_idxt +| 3808: 31 5f 79 64 78 04 43 52 45 41 54 45 20 54 41 42 1_ydx.CREATE TAB +| 3824: 4c 45 20 27 74 31 5f 69 64 78 27 28 73 65 67 69 LE 't1_idx'(segi +| 3840: 64 2c 20 74 65 72 6d 2c 20 70 67 6e 6f 2c 20 50 d, term, pgno, P +| 3856: 52 49 4d 41 52 59 20 4b 45 59 28 73 65 67 69 64 RIMARY KEY(segid +| 3872: 2c 20 74 65 72 6d 29 29 20 57 49 54 48 4f 55 54 , term)) WITHOUT +| 3888: 20 52 4f 57 49 44 55 03 07 17 1b 1b 01 81 01 74 ROWIDU........t +| 3904: 61 62 6c 65 74 31 5f 64 61 74 61 74 31 5f 74 61 ablet1_datat1_ta +| 3920: 74 61 03 43 52 45 41 54 45 20 54 41 42 4c 45 20 ta.CREATE TABLE +| 3936: 27 74 31 5f 64 61 74 61 27 28 69 64 20 49 4e 54 't1_data'(id INT +| 3952: 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 EGER PRIMARY KEY +| 3968: 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 29 38 02 06 , block BLOB)8.. +| 3984: 17 11 11 08 5f 74 61 62 6c 65 74 31 74 31 43 52 ...._tablet1t1CR +| 4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 35 LE t1 USING fts5 +| 4032: 28 61 2c 62 2c 63 29 00 00 00 00 00 00 00 00 00 (a,b,c)......... +| page 3 offset 8192 +| 0: 0d 00 00 00 03 0c 94 00 0f e6 0f ef 0c 94 00 00 ................ +| 3216: 00 00 00 00 86 4a 84 80 80 80 80 01 04 00 8d 18 .....J.......... +| 3232: 00 00 03 2b 02 30 30 01 02 06 01 02 06 01 02 06 ...+.00......... +| 3248: 2f 02 03 01 02 03 01 02 03 01 08 32 30 31 36 30 /..........20160 +| 3264: 36 30 39 01 02 07 01 02 07 01 02 07 01 01 34 01 609...........4. +| 3280: 02 05 01 02 c7 01 02 05 01 01 35 01 02 04 01 02 ..........5..... +| 3296: 04 01 02 04 02 07 30 30 30 30 30 30 30 1c 02 04 ......0000000... +| 3312: 01 02 04 01 02 04 01 06 62 69 6e 61 72 79 03 06 ........binary.. +| 3328: 01 02 02 04 16 01 02 02 03 06 01 02 02 02 06 01 ................ +| 3344: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3360: 02 03 06 01 02 02 03 06 01 02 02 02 06 01 02 02 ................ +| 3376: 03 06 01 02 02 03 06 01 02 02 01 08 63 6f 6d 70 ............comp +| 3392: 69 6c 65 72 01 02 02 01 02 02 01 02 02 01 06 64 iler...........d +| 3408: 62 73 74 61 74 07 02 03 00 02 03 01 02 03 02 04 bstat........... +| 3424: 65 62 74 67 04 02 02 01 02 02 01 02 02 01 06 65 ebtg...........e +| 3440: 6e 61 62 6c 65 07 02 02 01 02 02 01 02 02 01 02 nable........... +| 3456: 02 01 02 02 01 02 02 01 02 02 01 02 01 f1 02 02 ................ +| 3472: 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 ................ +| 3488: 02 02 01 02 02 45 02 02 01 02 02 01 02 02 01 02 .....E.......... +| 3504: 02 01 02 02 02 08 78 74 65 6e 73 69 6f 6e 1f 02 ......xtension.. +| 3520: 04 01 02 09 c1 02 04 01 04 66 74 73 34 0a 02 03 .........fts4... +| 3536: 01 02 03 01 02 03 04 00 35 0d 02 03 01 02 04 01 ........5....... +| 3552: 02 03 01 0f d7 63 63 01 02 03 01 02 03 01 02 03 .....cc......... +| 3568: 02 06 65 6f 70 6f 6b 79 10 02 03 01 02 03 01 02 ..eopoky........ +| 3584: 03 01 05 6a 73 6f 6e 31 13 02 03 14 02 03 01 02 ...json1........ +| 3600: 03 01 04 6c 6f 61 64 1f 02 03 01 02 03 01 02 03 ...load......... +| 3616: 01 03 6d 61 78 1c 02 02 01 02 02 01 02 02 02 05 ..max........... +| 3632: 65 6d 6f 72 79 1c 02 03 01 02 03 01 02 03 04 04 emory........... +| 3648: 73 79 73 35 16 02 03 01 02 03 01 02 03 01 06 6e sys5...........n +| 3664: 6f 63 61 73 65 02 06 01 02 12 03 06 01 02 02 03 ocase........... +| 3680: 06 01 02 02 03 06 01 02 02 09 f6 01 02 02 03 06 ................ +| 3696: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3712: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 11 02 ................ +| 3728: 02 01 04 6f 7d 69 74 1f 02 02 01 02 02 01 02 02 ...o.it......... +| 3744: 01 05 72 74 72 65 65 19 02 03 01 02 03 01 02 03 ..rtree......... +| 3760: 04 02 69 6d 01 06 01 02 02 03 06 01 02 02 03 06 ..im............ +| 3776: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3792: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 11 02 ................ +| 3808: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 ................ +| 3824: 00 fa 74 68 72 65 61 64 73 61 66 65 22 02 02 01 ..threadsafe.... +| 3840: 02 02 01 02 02 01 04 76 74 61 62 07 03 04 01 40 .......vtab....@ +| 3856: 04 01 02 04 11 01 78 01 06 01 01 02 01 06 01 01 ......x......... +| 3872: 02 01 06 01 00 02 01 06 01 01 02 01 03 91 01 02 ................ +| 3888: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 3904: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 3920: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 ................ +| 3936: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 ................ +| 3952: 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 ................ +| 3968: 01 06 01 01 02 01 76 01 01 02 01 06 01 01 02 5c ......v......... +| 3984: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 11 06 ................ +| 4000: 01 02 02 01 06 08 11 02 01 06 01 01 02 01 06 01 ................ +| 4016: 01 02 01 06 01 01 02 01 06 01 01 02 01 05 01 01 ................ +| 4032: 02 01 06 01 01 02 01 06 01 01 02 04 15 13 0c 0c ................ +| 4048: 12 44 13 11 0f 47 13 0f 0c 0e 11 10 ca 0e 10 0f .D...G.......... +| 4064: 44 0f 10 40 15 0f 07 01 03 00 14 24 5a 14 24 0f D..@.......$Z.$. +| 4080: 0a 03 00 24 ff ff ff ff 01 01 02 00 01 01 01 01 ...$............ +| page 4 offset 12288 +| 0: 0a 00 00 00 01 0f fb 00 00 00 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 05 04 09 0c 01 02 ................ +| page 5 offset 16384 +| 0: 0d 00 00 00 24 0c 09 00 00 00 00 00 00 00 00 00 ....$........... +| 3072: 00 00 00 00 00 00 00 00 00 00 18 24 05 00 25 0f ...........$..%. +| 3088: 19 54 48 52 45 41 44 53 41 46 45 3d 30 58 42 49 .THREADSAFE=0XBI +| 3104: 4e 41 52 59 18 23 05 00 25 0f 19 54 48 52 45 41 NARY.#..%..THREA +| 3120: 44 53 41 46 45 3d 30 58 4e 4f 43 41 53 47 17 22 DSAFE=0XNOCASG.. +| 3136: 05 00 25 0f 17 54 48 52 45 41 44 53 41 46 45 3d ..%..THREADSAFE= +| 3152: 30 58 52 54 52 49 4d 1f 21 05 00 33 0f 19 4f 4d 0XRTRIM.!..3..OM +| 3168: 49 54 20 4c 3f 41 44 20 45 58 54 45 4e 53 49 4f IT L?AD EXTENSIO +| 3184: 4e 58 42 49 4e 41 52 59 1f 20 05 00 33 0f 19 4f NXBINARY. ..3..O +| 3200: 4d 49 64 20 4c 4f 41 44 20 45 58 54 45 d9 53 49 MId LOAD EXTE.SI +| 3216: 4f 4e 58 4e 4f 43 41 53 45 1e 1f 05 00 33 0f 17 ONXNOCASE....3.. +| 3232: 4f 4d 39 54 20 4c 4f 41 44 20 45 58 55 45 4e 53 OM9T LOAD EXUENS +| 3248: 49 4f 4e 58 52 54 52 49 4d 1f 1e 05 00 33 0f 19 IONXRTRIM....3.. +| 3264: 4c 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 30 LAX MEMORY=50000 +| 3280: 30 30 30 58 42 49 4e 41 52 59 1f 1d 05 00 33 0f 000XBINARY....3. +| 3296: 19 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 .MAX MEMORY=5000 +| 3312: 30 30 30 30 58 af 4f 43 41 53 45 1e 1c 05 00 33 0000X.OCASE....3 +| 3328: 0f 17 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 ..MAX MEMORY=500 +| 3344: 30 30 ab 30 30 58 62 54 52 49 4d 18 1b 05 00 25 00.00XbTRIM....% +| 3360: 0f 19 45 4e 41 42 4c 45 20 52 54 52 45 45 58 42 ..ENABLE RTREEXB +| 3376: 49 4e 41 52 59 18 1b 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3392: 4c 45 20 52 54 52 45 45 58 4e 4f 43 41 53 45 17 LE RTREEXNOCASE. +| 3408: 19 05 00 25 0f 17 45 4e 41 42 4c 45 20 52 54 52 ...%..ENABLE RTR +| 3424: 45 45 58 52 54 52 49 4d 1a 18 05 00 29 0f 19 45 EEXRTRIM....)..E +| 3440: 4e 41 42 4c 45 20 4d 45 4d 53 59 63 35 58 42 49 NABLE MEMSYc5XBI +| 3456: 4e 41 52 59 1a 17 04 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3472: 45 20 4d 45 4d 53 59 53 35 58 4e 4f 43 41 53 45 E MEMSYS5XNOCASE +| 3488: 19 16 05 00 29 0f 17 45 4e 41 42 4c 45 20 3d 45 ....)..ENABLE =E +| 3504: 4d 53 59 53 35 58 52 54 52 49 4d 18 15 05 00 25 MSYS5XRTRIM....% +| 3520: 0f 19 45 4e 41 42 4c 45 20 4a 53 4f 4e 31 58 42 ..ENABLE JSON1XB +| 3536: 49 4e 41 52 59 18 14 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3552: 4c 46 20 4a 53 4f 4e 31 58 4e 4f 43 41 53 45 17 LF JSON1XNOCASE. +| 3568: 13 05 00 25 0f 17 45 4e 41 42 4c 45 20 4a 53 4f ...%..ENABLE JSO +| 3584: 4e 31 58 52 54 52 49 4d 1a 12 05 00 29 0f 19 45 N1XRTRIM....)..E +| 3600: 4e 41 42 4c 45 20 46 45 46 50 4f 4c 59 57 42 49 NABLE FEFPOLYWBI +| 3616: 4e 41 52 59 18 11 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3632: 45 20 47 45 4f 50 4f 4c 59 58 4e 5f 43 41 53 45 E GEOPOLYXN_CASE +| 3648: 19 10 05 00 29 0f 17 45 4e 41 42 4c 45 20 47 42 ....)..ENABLE GB +| 3664: 2f 50 4f 4c 59 58 51 54 52 49 4d 17 0f 05 00 23 /POLYXQTRIM....# +| 3680: 0f 19 45 4e 41 42 4c 45 20 46 54 53 35 58 42 49 ..ENABLE FTS5XBI +| 3696: 4e 41 52 59 17 0e 05 00 23 0f 19 45 4e 41 42 4c NARY....#..ENABL +| 3712: 45 20 46 54 53 35 58 4e 4f 43 41 53 45 16 0d 05 E FTS5XNOCASE... +| 3728: 00 23 0f 17 45 4e 41 42 4c 45 20 46 54 53 35 58 .#..ENABLE FTS5X +| 3744: 52 54 52 49 4d 17 1c 05 00 23 0f 19 45 4e 41 42 RTRIM....#..ENAB +| 3760: 4c 45 20 46 54 53 34 58 42 49 4e 41 52 59 16 0b LE FTS4XBINARY.. +| 3776: 05 00 22 0f e9 45 4e 41 42 4c 35 20 46 54 53 34 .....ENABL5 FTS4 +| 3792: 58 4e 4f 43 41 53 45 16 0a 05 00 23 00 47 45 4e XNOCASE....#.GEN +| 3808: 41 42 4c 45 20 46 54 53 34 57 52 54 52 49 4d 1e ABLE FTS4WRTRIM. +| 3824: 60 05 00 31 0f 19 45 4e 41 42 4c 55 20 43 42 53 `..1..ENABLU CBS +| 3840: 54 41 54 20 56 54 42 42 58 42 49 4e 41 52 59 1e TAT VTBBXBINARY. +| 3856: 08 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3872: 54 40 54 20 56 54 41 42 58 4e 4f 43 41 53 45 1d T@T VTABXNOCASE. +| 3888: 07 05 00 31 0f 17 45 4e 41 42 4c 55 20 44 42 53 ...1..ENABLU DBS +| 3904: 54 41 54 20 56 54 41 42 58 52 54 52 49 4d 12 06 TAT VTABXRTRIM.. +| 3920: 05 00 17 0f 19 44 45 42 55 47 58 42 49 4e 41 52 .....DEBUGXBINAR +| 3936: 59 21 05 05 00 17 0f 19 44 45 42 55 47 58 4e 4f Y!......DEBUGXNO +| 3952: 43 41 53 45 10 04 05 00 17 0f 18 44 45 42 55 47 CASE.......DEBUG +| 3968: 58 42 54 52 49 4d 27 11 05 00 43 0f 19 43 4f 4d XBTRIM'...C..COM +| 3984: 50 49 48 f5 52 3d 67 63 63 2d 35 2e 34 2e 30 20 PIH.R=gcc-5.4.0 +| 4000: 32 30 31 36 30 36 30 39 58 42 49 4e 41 52 59 27 20160609XBINARY' +| 4016: 02 05 00 43 0f 19 43 4f 4d 50 49 4c 45 52 3d 67 ...C..COMPILER=g +| 4032: 63 63 2d 35 2e 34 2e 30 22 32 30 31 36 30 36 30 cc-5.4.0.2016060 +| 4048: 39 c2 3e 4f 43 41 53 45 26 01 05 00 43 0f 17 43 9.>OCASE&...C..C +| 4064: 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e OMPILER=gcc-5.4. +| 4080: 30 30 32 30 31 26 30 36 30 39 58 52 54 52 49 4d 00201&0609XRTRIM +| page 6 offset 20480 +| 0: 0d 00 00 00 24 0e e0 00 00 00 00 00 00 00 00 00 ....$........... +| 3808: 06 24 03 00 12 02 01 01 06 23 03 00 12 02 01 01 .$.......#...... +| 3824: 06 22 03 00 12 02 01 01 06 21 03 00 12 03 01 01 .........!...... +| 3840: 06 20 03 00 12 03 01 01 06 1f 03 00 12 03 01 01 . .............. +| 3856: 06 1e 03 00 12 03 01 01 06 1d 03 00 12 03 01 01 ................ +| 3872: 06 1c 03 00 12 03 01 01 06 1b 03 00 12 02 01 01 ................ +| 3888: 06 1a 03 00 12 02 01 01 06 19 03 00 12 02 01 01 ................ +| 3904: 06 18 03 00 12 02 01 00 f6 17 03 00 19 e2 f9 01 ................ +| 3920: 06 16 03 00 12 02 05 01 06 15 03 00 12 02 01 01 ................ +| 3936: 06 14 03 00 12 02 01 01 06 13 03 00 12 02 01 01 ................ +| 3952: 06 12 03 00 12 02 01 01 06 11 03 00 12 02 01 01 ................ +| 3968: 06 10 03 10 12 02 01 01 06 0f 03 00 12 02 01 01 ................ +| 3984: 06 0e 03 00 12 02 01 01 06 0d 03 00 12 02 00 f1 ................ +| 4000: 06 0c 03 00 12 02 01 01 06 0b 03 00 12 02 01 01 ................ +| 4016: 06 0a 03 00 12 02 01 01 05 09 03 00 12 03 01 01 ................ +| 4032: 06 08 03 00 12 03 01 01 06 07 03 00 12 03 01 01 ................ +| 4048: 06 06 03 00 12 01 01 01 06 05 02 ff 84 01 01 01 ................ +| 4064: 06 04 03 00 12 01 01 01 06 03 03 00 12 06 01 01 ................ +| 4080: 07 02 03 00 12 06 01 01 06 01 03 00 12 06 01 01 ................ +| page 7 offset 24576 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| end crash-44942694542e1e.db +}]} {} + +do_catchsql_test 62.1 { + WITH c(x) AS (VALUES(false) UNION ALL SELECT x+1 FROM c WHERE x<72) + INSERT INTO t1(a) SELECT randomblob(2829) FROM c; +} {/*malformed database schema*/} + +#--------------------------------------------------------------------------- +do_test 63.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 24576 pagesize 4096 filename crash-8230e6c3b368f5.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 00 .....@ ........ +| 96: 00 00 00 00 0d 00 00 00 06 0e 0f 00 0f aa 0f 53 ...............S +| 112: 0e e8 0e 8b 0e 33 0e 0f 00 00 00 00 00 00 00 00 .....3.......... +| 3584: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 22 ................ +| 3600: 06 06 17 11 11 01 31 74 61 62 7c 65 62 63 62 62 ......1tab|ebcbb +| 3616: 06 43 52 45 41 54 45 20 54 41 42 4c 45 20 62 62 .CREATE TABLE bb +| 3632: 28 61 29 56 05 06 17 1f 1f 01 7d 74 61 62 6c 65 (a)V.......table +| 3648: 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 6f 6e 66 t1_configt1_conf +| 3664: 69 67 05 43 52 45 41 54 45 20 54 41 42 4c 45 20 ig.CREATE TABLE +| 3680: 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b 20 50 52 't1_config'(k PR +| 3696: 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 20 57 49 IMARY KEY, v) WI +| 3712: 54 48 4f 55 54 20 52 4f 57 49 44 5b 04 07 17 21 THOUT ROWID[...! +| 3728: 21 01 81 01 74 61 62 6c 65 74 31 5f 64 6f 63 73 !...tablet1_docs +| 3744: 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 04 43 52 izet1_docsize.CR +| 3760: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 9d EATE TABLE 't1_. +| 3776: 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 54 45 47 ocsize'(id INTEG +| 3792: 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 ER PRIMARY KEY, +| 3808: 73 7a 20 42 4c 4f 42 29 69 03 07 17 19 19 01 81 sz BLOB)i....... +| 3824: 2d 74 61 62 6c 65 74 31 5f 69 64 78 74 31 5f 69 -tablet1_idxt1_i +| 3840: 64 78 03 43 52 45 41 54 45 20 54 41 42 4c 45 20 dx.CREATE TABLE +| 3856: 27 74 31 5f 69 64 78 27 28 73 65 67 69 64 2c 20 't1_idx'(segid, +| 3872: 74 65 72 6d 2c 20 70 67 6e 6f 2c 20 50 52 49 4d term, pgno, PRIM +| 3888: 41 52 59 20 4b 45 59 28 73 65 67 69 64 2c 20 74 ARY KEY(segid, t +| 3904: 65 72 6d 29 29 20 57 49 54 48 4f 55 54 20 52 4f erm)) WITHOUT RO +| 3920: 57 49 44 55 02 07 17 1b 1b 01 81 01 74 61 62 6c WIDU........tabl +| 3936: 65 64 31 5f 64 61 74 61 74 31 5f 64 61 74 61 02 ed1_datat1_data. +| 3952: 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 CREATE TABLE 't1 +| 3968: 5f 64 61 74 61 27 28 69 64 20 49 4e 54 45 47 45 _data'(id INTEGE +| 3984: 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 62 R PRIMARY KEY, b +| 4000: 6c 6f 63 6b 20 42 4c 4f 42 29 54 01 07 17 10 11 lock BLOB)T..... +| 4016: 08 81 15 74 61 62 6c 65 74 31 74 31 43 52 45 41 ...tablet1t1CREA +| 4032: 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45 TE VIRTUAL TABLE +| 4048: 20 74 31 20 55 53 49 4e 47 20 66 74 73 35 28 61 t1 USING fts5(a +| 4064: 2c 62 2c 70 72 65 66 69 78 3d 22 31 2c 32 2c 33 ,b,prefix=.1,2,3 +| 4080: 2c 34 22 2c 20 63 6f 6e 74 65 6e 74 3d 22 22 29 ,4., content=..) +| page 2 offset 4096 +| 0: 0d 0b 6a 00 37 09 4c 02 0f e7 09 4c 0f c6 0f a4 ..j.7.L....L.... +| 16: 0f 88 0f 6d 0f 4b 0f 2c 0f 0e 0e ec 0e cd 0e ad ...m.K.,........ +| 32: 0e 8e 0e 6c 0e 4b 0e 29 0e 08 0d e6 0d c4 0d b5 ...l.K.)........ +| 48: 0d 97 0d 76 0d 54 0d 31 0d 15 0c f3 0c d3 0c b5 ...v.T.1........ +| 64: 0c 95 0c 73 0c 54 0c 32 0c 10 0b ee 0b cc 0b b0 ...s.T.2........ +| 80: 0b 8d 0b 7e 0b 48 0b 2e 0b 0b 0a ef 0a cc 0a ad ...~.H.......... +| 96: 0a 8c 0a 6d 0a 4d 0a 2b 0a 0c 09 ec 09 ca 09 a8 ...m.M.+........ +| 112: 09 86 09 63 0f f1 00 00 00 00 00 00 00 00 00 00 ...c............ +| 2368: 00 00 00 00 00 00 00 00 00 00 00 00 15 0a 03 00 ................ +| 2384: 30 00 00 00 01 01 03 35 00 03 01 01 12 02 01 12 0......5........ +| 2400: 03 01 11 1c 8c 80 80 80 80 10 03 00 3e 00 00 00 ............>... +| 2416: 17 01 05 05 34 74 61 62 6c 03 02 03 01 04 77 68 ....4tabl.....wh +| 2432: 65 72 03 02 06 09 1b 8c 80 80 80 80 0f 03 00 3c er.............< +| 2448: 00 00 00 16 05 34 66 74 73 34 03 02 02 01 04 6e .....4fts4.....n +| 2464: 75 6d 62 03 06 01 04 09 1b 8c 80 80 80 80 0e 03 umb............. +| 2480: 00 3c 00 00 00 16 04 33 74 68 65 13 06 01 01 04 .<.....3the..... +| 2496: 01 03 77 68 65 03 02 04 04 0a 1b 8c 80 80 80 80 ..whe........... +| 2512: 0d 03 00 3c 00 00 00 16 04 33 6e 75 6d 03 06 01 ...<.....3num... +| 2528: 01 05 01 03 75 61 62 03 02 03 04 0a 19 8c 80 80 ....uab......... +| 2544: 80 80 0c 03 00 38 00 00 00 14 03 32 ec 68 03 02 .....8.....2.h.. +| 2560: 04 00 04 33 66 74 73 03 02 02 04 07 18 8c 80 80 ...3fts......... +| 2576: 80 80 0b 03 00 36 00 00 00 13 03 32 74 61 03 02 .....6.....2ta.. +| 2592: 03 02 01 68 03 06 01 01 04 04 17 1b 8c 80 80 80 ...h............ +| 2608: 80 0a 03 00 3c 00 00 00 16 03 32 6e 75 03 06 01 ....<.....2nu... +| 2624: 01 05 01 02 6f 66 03 06 01 01 06 04 09 19 8c 80 ....of.......... +| 2640: 80 80 80 09 03 00 38 00 00 00 14 03 32 66 74 03 ......8.....2ft. +| 2656: 02 02 01 02 69 73 03 06 01 01 03 04 07 18 8c 80 ....is.......... +| 2672: 80 80 80 08 03 00 36 00 00 00 13 02 31 74 03 08 ......6.....1t.. +| 2688: 03 01 01 04 01 01 77 03 02 04 04 09 1a 8c 80 80 ......w......... +| 2704: 80 80 07 03 00 3a 00 00 00 15 02 31 6e 03 08 01 .....:.....1n... +| 2720: 01 02 05 01 01 6f 03 06 01 01 06 04 09 18 8c 80 .....o.......... +| 2736: 80 80 80 06 03 00 36 00 00 00 13 04 02 31 66 03 ......6......1f. +| 2752: 02 02 01 01 69 03 06 01 01 03 05 06 1c 8c 80 80 ....i........... +| 2768: 80 80 05 03 00 3e 00 00 00 17 04 30 74 68 65 03 .....>.....0the. +| 2784: 06 01 01 04 01 05 77 68 65 72 65 03 02 04 0a 15 ......where..... +| 2800: 8c 80 80 80 80 04 03 00 30 00 00 00 11 01 01 06 ........0....... +| 2816: 06 30 74 61 62 6c 65 03 02 03 07 1c 8c 80 80 80 .0table......... +| 2832: 80 03 03 00 3e 00 00 00 17 07 30 6e 75 6d 62 65 ....>.....0numbe +| 2848: 72 03 06 01 01 05 01 02 6f 66 03 06 04 0d 13 8c r.......of...... +| 2864: 80 80 80 80 02 03 00 2c 00 00 00 0f 01 01 03 02 .......,........ +| 2880: 30 6e 03 06 01 01 02 07 1b 8c 80 80 80 80 01 03 0n.............. +| 2896: 00 3c 00 00 00 16 08 30 66 74 73 34 61 75 78 03 .<.....0fts4aux. +| 2912: 02 02 01 02 69 73 03 06 04 0c 00 00 00 14 2a 00 ....is........*. +| 2928: 00 00 01 01 02 24 00 02 01 01 12 02 01 12 08 88 .....$.......... +| 2944: 80 80 80 80 12 03 00 16 00 00 00 05 02 1c 88 80 ................ +| 2960: 80 80 80 11 03 00 3e 00 00 00 17 05 34 72 6f 77 ......>.....4row +| 2976: 73 02 06 01 01 05 01 04 74 68 65 72 02 02 04 0b s.......ther.... +| 2992: 15 88 80 80 80 80 10 03 00 30 00 00 00 11 02 01 .........0...... +| 3008: 01 07 05 34 62 65 74 77 02 02 04 08 1b 88 80 80 ...4betw........ +| 3024: 80 80 0f 03 00 3c 00 00 00 16 04 04 33 72 6f 77 .....<......3row +| 3040: 02 06 01 01 05 01 03 74 68 64 02 08 05 0a 1b 88 .......thd...... +| 3056: 80 80 80 80 0e 03 00 3c 00 00 00 16 01 01 02 04 .......<........ +| 3072: 33 61 72 65 02 02 03 01 03 62 65 74 02 02 07 08 3are.....bet.... +| 3088: 1b 88 80 80 80 80 0d 03 00 3c 00 00 00 16 03 32 .........<.....2 +| 3104: 74 68 02 08 02 01 01 07 00 04 33 61 6e 64 02 06 th........3and.. +| 3120: 04 0a 1b 88 80 80 80 80 0c 03 00 3c 00 00 00 16 ...........<.... +| 3136: 03 32 69 6e 02 06 01 01 06 01 02 72 6f 02 06 01 .2in.......ro... +| 3152: 01 43 04 09 18 88 80 80 80 80 0b 03 00 36 00 00 .C...........6.. +| 3168: 00 13 02 03 32 61 72 02 02 03 01 02 62 65 02 02 ....2ar.....be.. +| 3184: 04 05 07 1b 88 80 80 80 80 0a 03 00 3c 00 00 00 ............<... +| 3200: 16 02 31 74 02 08 02 01 01 07 00 03 32 61 6e 02 ..1t........2an. +| 3216: 06 01 01 04 09 19 88 80 80 80 80 09 03 00 38 00 ..............8. +| 3232: 00 00 14 02 31 6e 02 06 01 01 03 01 01 72 02 06 ....1n.......r.. +| 3248: 01 01 05 04 08 17 88 80 80 80 80 08 03 00 34 00 ..............4. +| 3264: 00 00 12 02 31 62 02 02 04 01 01 69 02 06 01 01 ....1b.....i.... +| 3280: 06 04 06 19 88 80 80 80 80 07 03 00 38 00 00 00 ............8... +| 3296: 14 04 02 31 32 02 02 05 01 01 61 02 08 03 01 01 ...12.....a..... +| 3312: 02 05 06 1b 88 80 80 80 80 06 03 00 3c 00 00 00 ............<... +| 3328: 16 06 30 74 68 65 72 65 02 02 01 00 02 30 21 02 ..0there.....0!. +| 3344: 06 01 01 04 0a 15 88 80 80 80 80 05 03 00 30 00 ..............0. +| 3360: 00 00 11 01 01 05 04 30 74 68 65 02 06 01 01 07 .......0the..... +| 3376: 07 1c 88 80 80 80 80 04 03 00 3e 00 00 00 17 01 ..........>..... +| 3392: 01 06 02 30 6e 02 06 01 01 03 01 04 72 6f 77 73 ...0n.......rows +| 3408: 02 06 07 08 1b 88 80 80 80 80 03 03 00 3c 00 51 .............<.Q +| 3424: 00 16 08 30 62 65 74 77 65 65 6e 02 02 04 01 02 ...0between..... +| 3440: 69 6e 02 06 04 0c 1a 88 80 80 80 80 02 03 00 3a in.............: +| 3456: 00 00 00 15 04 30 61 6e 64 02 06 01 01 02 02 02 .....0and....... +| 3472: 72 65 02 02 03 04 0a 17 88 80 80 80 80 01 03 00 re.............. +| 3488: 34 00 00 00 12 02 30 31 02 06 01 01 04 01 01 32 4.....01.......2 +| 3504: 02 02 05 04 08 08 84 80 80 80 80 12 03 00 16 00 ................ +| 3520: 00 00 05 04 1b 84 80 80 80 80 11 03 00 3c 00 00 .............<.. +| 3536: 00 16 05 34 74 51 62 6c 01 06 01 01 05 02 03 65 ...4tQbl.......e +| 3552: 72 6d 01 02 04 0b 1b 84 80 80 80 80 10 03 00 3c rm.............< +| 3568: 00 00 00 16 05 34 65 17 63 68 01 02 03 01 04 70 .....4e.ch.....p +| 3584: 72 65 73 01 02 05 04 09 1a 84 80 80 80 80 0f 03 res............. +| 3600: 00 3a 00 00 00 15 04 33 74 65 72 01 02 04 02 02 .:.....3ter..... +| 3616: 68 65 01 06 01 01 03 04 08 1b 84 80 80 80 80 0e he.............. +| 3632: 03 00 3c 00 00 00 16 04 33 70 72 65 01 02 05 01 ..<.....3pre.... +| 3648: 03 74 61 62 01 06 01 01 05 04 08 1a 84 80 80 80 .tab............ +| 3664: 80 0d 03 00 3a 00 00 00 15 04 33 66 6f 72 01 03 ....:.....3for.. +| 3680: 02 02 02 74 73 01 06 01 01 04 04 08 1b 84 80 80 ...ts........... +| 3696: 80 80 0c 03 00 3c 00 00 00 16 03 32 74 68 01 06 .....<.....2th.. +| 3712: 01 01 03 00 04 33 65 61 63 01 02 03 04 09 18 84 .....3eac....... +| 3728: 80 80 80 80 0b 03 00 36 00 00 00 13 03 32 74 61 .......6.....2ta +| 3744: 01 06 01 01 05 02 01 65 01 02 04 04 09 19 84 80 .......e........ +| 3760: 80 80 80 0a 03 00 38 00 00 00 14 03 32 69 6e 01 ......8.....2in. +| 3776: 06 01 01 02 01 02 70 72 01 02 05 04 09 18 84 80 ......pr........ +| 3792: 80 80 80 09 03 00 36 00 00 00 13 03 32 66 6f 01 ......6.....2fo. +| 3808: 02 02 02 01 74 01 06 01 01 04 04 07 1b 84 80 80 ....t........... +| 3824: 80 80 08 03 00 3c 00 00 00 16 02 31 74 01 0a 04 .....<.....1t... +| 3840: 01 01 03 04 00 03 32 65 61 01 02 03 04 0a 17 84 ......2ea....... +| 3856: 80 80 80 80 07 03 00 34 00 00 00 12 02 31 69 01 .......4.....1i. +| 3872: 06 01 01 02 01 01 70 01 02 05 04 08 18 84 80 80 ......p......... +| 3888: 80 80 06 03 00 36 00 00 00 12 02 31 65 01 02 02 .....6.....1e... +| 3904: 01 01 66 01 08 02 01 01 04 04 06 1b 84 80 80 80 ..f............. +| 3920: 80 05 03 00 3c 00 00 00 16 05 30 74 65 72 6d 01 ....<.....0term. +| 3936: 02 04 02 02 68 65 01 06 01 01 03 04 09 14 84 80 ....he.......... +| 3952: 80 80 80 04 03 00 2e 00 00 00 10 06 30 74 61 62 ............0tab +| 3968: 6c 65 01 06 01 01 05 04 15 84 80 80 80 80 03 03 le.............. +| 3984: 00 30 00 00 00 11 02 08 30 70 72 65 73 65 6e 74 .0......0present +| 4000: 01 02 05 05 1b 84 80 80 80 80 02 03 00 3c 00 00 .............<.. +| 4016: 00 16 04 30 66 74 73 01 06 01 01 04 01 02 69 6e ...0fts.......in +| 4032: 01 06 01 01 04 0a 1a 84 80 80 80 80 01 03 00 3a ...............: +| 4048: 00 00 00 15 05 30 65 61 63 68 01 02 03 01 03 66 .....0each.....f +| 4064: 6f 72 01 02 01 f4 09 06 01 03 00 12 03 0b 0f 00 or.............. +| 4080: 00 08 8c 80 80 80 80 11 03 00 16 00 00 00 05 04 ................ +| page 3 offset 8192 +| 0: 0a 00 00 00 32 0e 4f 00 0f fa 0f f1 0f e9 0f e1 ....2.O......... +| 16: 0f d8 0f d1 0f c9 0f c1 0f b9 0f b1 0f a9 0f a0 ................ +| 32: 0f 98 0f 90 0f 87 0f 80 0f 78 0f 71 0f 68 0f 5f .........x.q.h._ +| 48: 0f 56 0f 4d 0f 41 0f 38 0f 2f 0f 26 0f 1d 0f 13 .V.M.A.8./.&.... +| 64: 0f 0a 0f 01 0e f7 0e ee 0e e6 0e dd 0e d6 0e cd ................ +| 80: 0e c3 0e ba 0e b0 0e a8 0e 9f 0e 00 00 00 00 00 ................ +| 3648: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 08 ................ +| 3664: 04 01 10 01 03 34 74 20 07 04 01 0e 01 03 34 1e .....4t ......4. +| 3680: 09 04 01 12 01 03 33 74 68 1c 08 04 01 10 01 03 ......3th....... +| 3696: 33 6e 1a 08 04 01 10 01 03 32 77 18 08 04 01 10 3n.......2w..... +| 3712: 01 03 32 74 16 08 04 01 10 01 03 32 6e 14 07 04 ..2t.......2n... +| 3728: 01 0e 01 03 32 12 08 04 01 10 01 03 31 74 10 07 ....2.......1t.. +| 3744: f4 01 10 01 03 31 6e 0e 07 04 01 0e 01 03 31 0c .....1n.......1. +| 3760: 09 04 01 12 01 03 30 74 68 0a 08 04 01 10 01 03 ......0th....... +| 3776: 30 74 08 09 04 01 12 01 03 30 6e 75 06 08 04 01 0t.......0nu.... +| 3792: 10 01 03 30 6e 04 06 04 01 0c 01 03 02 08 04 01 ...0n........... +| 3808: 10 01 02 34 73 22 07 04 01 0e 01 02 34 20 08 04 ...4s.......4 .. +| 3824: 01 10 01 02 33 72 1e 09 04 01 12 01 02 33 61 72 ....3r.......3ar +| 3840: 1c 08 04 01 10 01 02 32 74 1a 08 04 01 10 01 02 .......2t....... +| 3856: 32 69 18 09 04 01 12 01 02 32 61 72 16 08 04 01 2i.......2ar.... +| 3872: 10 01 02 31 74 14 08 04 01 10 01 02 31 6e 12 08 ...1t.......1n.. +| 3888: 04 01 10 01 02 31 62 10 08 04 01 10 01 02 31 32 .....1b.......12 +| 3904: 0e 0b 04 01 16 01 02 30 74 00 00 00 00 00 00 00 .......0t....... +| page 4 offset 12288 +| 4064: 00 00 00 00 00 00 00 00 00 00 00 05 02 03 00 10 ................ +| 4080: 03 05 05 02 03 00 10 04 06 05 01 03 00 10 04 04 ................ +| page 5 offset 16384 +| 0: 0a 00 00 00 02 0f eb 00 0f eb 0f f4 00 00 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 08 03 15 01 70 ...............p +| 4080: 67 73 7a 08 0b 03 1b 01 76 65 72 73 69 6f 6e 04 gsz.....version. +| end crash-8230e6c3b368f5.db +}]} {} + +do_catchsql_test 63.1 { + SELECT * FROM t1 WHERE b MATCH 'thead*thead*theSt*'; +} {/*malformed database schema*/} + +do_catchsql_test 63.2 { + INSERT INTO t1(t1) VALUES('optimize'); +} {/*malformed database schema*/} + +do_catchsql_test 63.3 { + SELECT * FROM t1 WHERE b MATCH 'thead*thead*theSt*'; +} {/*malformed database schema*/} + +#--------------------------------------------------------------------------- +do_test 64.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 28672 pagesize 4096 filename crash-4470f0b94422f7.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 06 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 06 00 00 00 04 ................ +| 96: 00 00 00 00 0d 00 00 00 06 0d e2 00 0f c4 0f 6a ...............j +| 112: 0e fc 0e 9d 0e 3d 0d e2 00 00 00 00 00 01 00 00 .....=.......... +| 3552: 00 00 59 06 06 17 21 21 01 7f 74 61 62 6c 65 74 ..Y...!!..tablet +| 3568: 74 74 5f 63 6f 6e 66 69 67 74 74 74 5f 63 6f 6e tt_configttt_con +| 3584: 66 69 67 06 43 52 45 41 54 45 20 54 41 42 4c 45 fig.CREATE TABLE +| 3600: 20 27 74 74 74 5f 63 6f 6e 66 69 67 27 28 6b 20 'ttt_config'(k +| 3616: 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 20 PRIMARY KEY, v) +| 3632: 57 49 54 48 4f 55 54 20 52 4f 57 49 44 5e 05 07 WITHOUT ROWID^.. +| 3648: 17 23 23 01 81 03 74 61 62 6c 65 74 74 74 5f 64 .##...tablettt_d +| 3664: 6f 63 73 69 7a 65 74 74 74 5f 64 6f 63 73 69 7a ocsizettt_docsiz +| 3680: 65 05 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 e.CREATE TABLE ' +| 3696: 74 74 74 5f 64 6f 63 73 69 7a 65 27 28 69 64 20 ttt_docsize'(id +| 3712: 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 INTEGER PRIMARY +| 3728: 4b 45 59 2c 20 73 7a 20 42 4c 4f 42 29 5d 04 07 KEY, sz BLOB)].. +| 3744: 17 23 23 01 81 01 74 61 62 6c 65 74 74 74 5f 63 .##...tablettt_c +| 3760: 6f 6e 74 65 6e 74 74 74 74 5f 63 6f 6e 74 65 6e ontentttt_conten +| 3776: 74 04 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 t.CREATE TABLE ' +| 3792: 74 74 74 5f 63 6f 6e 74 65 6e 74 27 28 69 64 20 ttt_content'(id +| 3808: 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 INTEGER PRIMARY +| 3824: 4b 45 59 2c 20 63 30 2c 20 63 31 29 6c 03 07 17 KEY, c0, c1)l... +| 3840: 1b 1b 01 81 2f 74 61 62 6c 65 74 74 74 5f 69 64 ..../tablettt_id +| 3856: 78 74 74 74 5f 69 64 78 03 43 52 45 41 54 45 20 xttt_idx.CREATE +| 3872: 54 41 42 4c 45 20 27 74 74 74 5f 69 64 78 27 28 TABLE 'ttt_idx'( +| 3888: 73 65 67 69 64 2c 20 74 65 72 6d 2c 20 70 67 6e segid, term, pgn +| 3904: 6f 2c 20 50 52 49 4d 41 52 59 20 4b 45 59 28 73 o, PRIMARY KEY(s +| 3920: 65 67 69 64 2c 20 74 65 72 6d 29 29 20 57 49 54 egid, term)) WIT +| 3936: 48 4f 55 54 20 52 4f 57 49 44 58 02 07 17 1d 1d HOUT ROWIDX..... +| 3952: 01 81 03 74 61 62 6c 65 74 74 74 5f 64 61 74 61 ...tablettt_data +| 3968: 74 74 74 5f 64 61 74 61 02 43 52 45 41 54 45 20 ttt_data.CREATE +| 3984: 54 41 42 4c 45 20 27 74 74 74 5f 64 61 74 61 27 TABLE 'ttt_data' +| 4000: 28 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d (id INTEGER PRIM +| 4016: 41 52 59 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 ARY KEY, block B +| 4032: 4c 4f 42 29 3a 01 06 17 13 13 08 5f 74 61 62 6c LOB):......_tabl +| 4048: 65 74 74 74 74 74 74 43 52 45 41 54 45 20 56 49 ettttttCREATE VI +| 4064: 52 54 55 41 4c 20 54 41 42 4c 45 20 74 74 74 20 RTUAL TABLE ttt +| 4080: 55 53 49 4e 47 20 66 74 73 35 28 61 2c 20 62 29 USING fts5(a, b) +| page 2 offset 4096 +| 0: 0d 0f 44 00 05 0e 81 00 0f 1a 0e 81 0f af 0f 58 ..D............X +| 16: 0e 98 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 3712: 00 15 0a 03 00 30 00 00 00 00 01 03 03 00 03 01 .....0.......... +| 3728: 01 01 02 01 01 03 01 01 81 24 8c 80 80 80 80 01 .........$...... +| 3744: 04 00 82 4c 00 00 00 9b 02 30 65 03 1a 02 05 05 ...L.....0e..... +| 3760: 07 05 01 01 04 03 03 08 03 03 01 2e 02 05 05 07 ................ +| 3776: 05 07 05 07 05 01 01 04 03 03 08 03 03 08 03 03 ................ +| 3792: 07 f3 03 02 01 65 03 1e 03 05 05 04 05 05 01 00 .....e.......... +| 3808: 03 06 04 04 06 04 03 01 36 03 05 05 04 06 05 04 ........6....... +| 3824: 06 05 04 05 05 01 01 03 06 04 04 06 04 04 06 04 ................ +| 3840: 04 06 04 03 03 01 65 03 14 04 05 06 f5 05 01 01 ......e......... +| 3856: 02 08 09 01 20 04 05 07 05 07 05 07 05 05 01 00 .... ........... +| 3872: 02 08 0a 0a 0a 04 01 65 03 02 0a 01 06 0a 0a 0a .......e........ +| 3888: 05 01 65 03 06 01 01 0a 01 0a 01 01 0a 0a 0a 04 ..e............. +| 3904: 2b 31 21 0b 0f ef 00 14 2a 00 00 00 00 01 02 02 +1!.....*....... +| 3920: 00 02 01 01 01 02 01 01 50 88 80 80 80 80 01 04 ........P....... +| 3936: 00 81 24 00 00 00 47 02 30 65 02 1a 02 05 05 07 ..$...G.0e...... +| 3952: 05 01 01 04 03 03 08 03 03 02 01 65 02 1e 03 05 ...........e.... +| 3968: 05 04 05 05 01 01 03 06 04 04 06 04 03 03 01 65 ...............e +| 3984: 02 14 04 05 07 05 05 01 01 02 08 0a 04 01 65 02 ..............e. +| 4000: 02 0a 05 01 65 02 06 01 01 0a 04 12 14 0f 06 31 ....e..........1 +| 4016: 84 80 80 80 80 01 03 00 68 00 00 00 2b 02 30 65 ........h...+.0e +| 4032: 01 10 02 05 05 01 01 04 03 03 02 01 65 01 12 03 ............e... +| 4048: 05 05 01 01 03 06 04 03 03 01 65 01 0e 04 05 05 ..........e..... +| 4064: 01 01 02 08 04 0d 0e 06 01 03 00 12 04 4c 4c 00 .............LL. +| 4080: 00 00 11 24 00 00 00 00 01 01 01 00 01 01 01 01 ...$............ +| page 3 offset 8192 +| 0: 0a 00 00 00 03 0f ec 00 0f 00 00 00 00 00 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 00 06 04 01 0c ................ +| 4080: 01 03 02 06 04 01 0c 01 02 02 05 04 09 0c 01 02 ................ +| page 4 offset 12288 +| 0: 0d 00 00 00 04 0e 1a 00 0f c7 0f 5b 0e ef 0e 1a ...........[.... +| 3600: 00 00 00 00 00 00 00 00 00 00 81 52 04 06 00 81 ...........R.... +| 3616: 5d 81 55 65 20 65 65 20 65 65 65 20 65 20 65 65 ].Ue ee eee e ee +| 3632: 20 65 65 65 20 65 20 65 65 20 65 65 65 66 20 65 eee e ee eeef e +| 3648: 65 20 65 65 65 20 65 20 65 65 20 65 65 65 20 65 e eee e ee eee e +| 3664: 20 65 65 20 65 65 65 65 20 65 65 20 65 65 65 20 ee eeee ee eee +| 3680: 65 20 65 65 20 65 65 65 20 65 20 65 65 20 65 65 e ee eee e ee ee +| 3696: 65 65 20 65 65 20 65 65 65 20 65 20 65 65 20 65 ee ee eee e ee e +| 3712: 65 65 20 65 20 65 65 20 65 65 65 65 65 65 20 65 ee e ee eeeeee e +| 3728: 65 20 65 20 65 20 65 20 65 65 20 65 65 65 20 65 e e e e ee eee e +| 3744: 65 20 65 65 65 65 65 20 65 65 20 65 20 65 1f 65 e eeeee ee e e.e +| 3760: 20 65 65 20 65 65 65 20 65 65 20 65 65 65 65 65 ee eee ee eeeee +| 3776: 20 65 65 20 65 20 65 20 65 20 65 65 20 65 65 65 ee e e e ee eee +| 3792: 20 65 65 20 65 65 65 65 65 20 65 65 20 65 20 65 ee eeeee ee e e +| 3808: 20 65 20 65 65 20 65 65 65 20 65 65 20 65 65 6a e ee eee ee eej +| 3824: 03 03 ff 75 71 65 20 65 65 1f 65 65 65 20 65 20 ...uqe ee.eee e +| 3840: 65 65 20 65 65 65 20 65 20 65 65 20 65 65 65 65 ee eee e ee eeee +| 3856: 20 65 65 20 65 65 65 20 65 20 65 65 20 65 65 65 ee eee e ee eee +| 3872: 20 65 20 65 65 20 65 65 65 65 65 65 20 65 65 20 e ee eeeeee ee +| 3888: 65 20 65 20 65 20 65 65 20 65 65 65 20 65 65 20 e e e ee eee ee +| 3904: 65 65 65 65 65 20 65 65 20 65 20 65 20 65 20 65 eeeee ee e e e e +| 3920: 65 20 65 65 65 20 65 65 20 65 65 6a 02 04 00 75 e eee ee eej...u +| 3936: 40 65 20 65 65 20 65 65 65 20 65 20 65 65 20 65 @e ee eee e ee e +| 3952: 65 65 20 65 20 65 65 20 65 65 65 65 20 65 65 20 ee e ee eeee ee +| 3968: 65 65 65 20 65 20 65 65 20 65 65 65 20 65 20 65 eee e ee eee e e +| 3984: 65 20 65 65 65 65 65 65 20 65 65 20 65 20 65 20 e eeeeee ee e e +| 4000: 65 20 65 65 20 65 65 65 20 65 65 20 65 65 65 65 e ee eee ee eeee +| 4016: 65 20 65 65 20 65 20 65 20 65 20 65 65 20 65 65 e ee e e e ee ee +| 4032: 65 20 65 65 20 65 65 37 01 04 00 41 3f 65 20 65 e ee ee7...A?e e +| 4048: 65 20 65 65 65 20 65 20 65 65 20 65 65 65 20 65 e eee e ee eee e +| 4064: 20 65 65 20 65 65 65 65 65 65 20 65 65 20 65 20 ee eeeeee ee e +| 4080: 65 20 65 20 65 65 20 65 65 65 20 65 65 20 65 65 e e ee eee ee ee +| page 5 offset 16384 +| 0: 0d 00 00 00 04 0f e4 00 0f f9 0f f2 0f eb 0f e4 ................ +| 4064: 00 00 00 00 05 04 03 00 10 21 21 05 03 03 00 10 .........!!..... +| 4080: 11 11 05 02 03 00 10 11 11 05 01 03 00 10 09 09 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| end crash-4470f0b94422f7.db +}]} {} + +do_catchsql_test 64.1 { + SELECT * FROM ttt('e*'); +} {1 {database disk image is malformed}} + +#--------------------------------------------------------------------------- +do_test 65.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 28672 pagesize 4096 filename crash-3aef66940ace0c.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 00 .....@ ........ +| 96: 00 00 00 00 0d 0f c7 00 07 0d 92 00 0f 8d 0f 36 ...............6 +| 112: 0e cb 0e 6b 0e 0e 0d b6 0d 92 00 00 00 00 00 00 ...k............ +| 3472: 00 00 22 08 06 17 11 11 01 31 74 61 62 6c 65 74 .........1tablet +| 3488: 32 74 32 08 43 52 45 41 54 45 20 54 41 42 4c 45 2t2.CREATE TABLE +| 3504: 20 74 32 28 78 29 56 07 06 17 1f 1f 01 7d 74 61 t2(x)V.......ta +| 3520: 62 6c 65 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 blet1_configt1_c +| 3536: 6f 6e 66 69 67 07 43 52 45 41 54 45 20 54 41 42 onfig.CREATE TAB +| 3552: 4c 45 20 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b LE 't1_config'(k +| 3568: 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 PRIMARY KEY, v) +| 3584: 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 5b 06 WITHOUT ROWID[. +| 3600: 07 17 21 21 01 81 01 74 61 62 6c 65 74 31 5f 64 ..!!...tablet1_d +| 3616: 6f 63 73 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 ocsizet1_docsize +| 3632: 06 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 .CREATE TABLE 't +| 3648: 31 5f 64 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 1_docsize'(id IN +| 3664: 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE +| 3680: 59 2c 20 73 7a 20 42 4c 4f 42 29 5e 05 07 17 21 Y, sz BLOB)^...! +| 3696: 21 01 81 07 74 61 62 6c 65 74 31 5f 63 6f 6e 74 !...tablet1_cont +| 3712: 65 6e 74 74 31 5f 63 6f 6e 74 65 6e 74 05 43 52 entt1_content.CR +| 3728: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 EATE TABLE 't1_c +| 3744: 6f 6e 74 65 6e 74 27 28 69 64 20 49 4e 54 45 47 ontent'(id INTEG +| 3760: 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 ER PRIMARY KEY, +| 3776: 63 30 2c 20 63 31 2c d6 63 32 29 69 04 07 17 19 c0, c1,.c2)i.... +| 3792: 19 01 81 2d 74 61 62 6c 65 74 31 5f 69 64 78 74 ...-tablet1_idxt +| 3808: 31 5f 69 64 78 04 43 52 45 41 54 45 20 54 41 42 1_idx.CREATE TAB +| 3824: 4c 45 20 27 74 31 5f 69 64 78 27 28 73 65 67 69 LE 't1_idx'(segi +| 3840: 64 2c 20 74 65 72 6d 2c 20 70 67 6e 6f 2c 20 50 d, term, pgno, P +| 3856: 52 49 4d 41 52 59 20 4b 45 59 28 73 65 67 69 64 RIMARY KEY(segid +| 3872: 2c 20 74 65 72 6d 29 29 20 57 49 54 48 4f 55 54 , term)) WITHOUT +| 3888: 20 52 4f 57 49 44 55 03 07 17 1b 1b 01 81 01 74 ROWIDU........t +| 3904: 61 62 6c 65 74 31 5f 64 61 74 61 74 31 5f 64 61 ablet1_datat1_da +| 3920: 74 61 03 43 52 45 41 54 45 20 54 41 42 4c 45 20 ta.CREATE TABLE +| 3936: 27 74 31 5f 64 61 74 61 27 28 69 64 20 49 4e 54 't1_data'(id INT +| 3952: 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 EGER PRIMARY KEY +| 3968: 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 29 38 02 06 , block BLOB)8.. +| 3984: 17 11 11 08 5f 74 61 62 6c 65 74 31 74 31 43 52 ...._tablet1t1CR +| 4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 35 LE t1 USING fts5 +| 4032: 28 61 2c 62 2c 63 29 00 00 00 00 00 00 00 00 00 (a,b,c)......... +| page 3 offset 8192 +| 0: 0d 00 00 00 03 0c 93 ff 0f e6 0f ef 0c 94 00 00 ................ +| 3216: 00 00 00 00 86 4a 84 80 80 80 80 01 04 00 8d 18 .....J.......... +| 3232: 00 00 03 2b 02 30 30 01 02 06 01 02 06 01 02 06 ...+.00......... +| 3248: 1f 02 03 01 02 03 01 02 03 01 08 32 31 31 36 30 ...........21160 +| 3264: 36 30 39 01 02 07 01 02 07 01 02 07 01 01 33 f1 609...........3. +| 3280: 02 05 01 02 05 01 02 05 01 01 35 01 02 03 01 02 ..........5..... +| 3296: 04 01 02 04 02 07 30 30 30 30 30 30 30 1c 02 3d ......0000000..= +| 3312: 01 02 04 01 02 04 01 06 62 69 6e 61 72 79 03 06 ........binary.. +| 3328: 01 02 02 03 06 01 01 f2 03 06 4e 02 02 03 06 01 ..........N..... +| 3344: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3360: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 ................ +| 3376: 03 06 01 02 02 03 06 01 02 02 01 08 63 6f 6d 70 ............comp +| 3392: 69 6c 65 72 01 02 02 01 02 02 01 02 02 01 06 64 iler...........d +| 3408: 62 73 74 61 74 07 02 03 01 02 13 01 02 03 02 04 bstat........... +| 3424: 65 62 75 67 04 02 02 01 02 02 01 02 02 01 07 65 ebug...........e +| 3440: 6e 61 62 6c 65 07 02 02 01 02 02 01 02 02 01 02 nable........... +| 3456: 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 ................ +| 3472: 01 02 02 01 02 01 f1 02 02 01 02 02 01 02 02 01 ................ +| 3488: 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 ................ +| 3504: 02 01 02 02 02 08 76 b4 65 6e 73 69 6f 6e 1f 02 ......v.ension.. +| 3520: 04 01 02 04 01 02 04 01 04 66 74 73 34 0a 02 03 .........fts4... +| 3536: 01 02 03 01 02 03 04 01 25 0d 02 03 01 02 03 01 ........%....... +| 3552: 02 03 01 03 67 63 63 01 02 03 01 02 03 01 02 03 ....gcc......... +| 3568: 02 06 65 6f 70 6f 6c 79 0f f2 03 01 02 03 01 02 ..eopoly........ +| 3584: 03 01 05 6a 73 6f 6e 31 13 02 03 01 02 03 01 02 ...json1........ +| 3600: 03 01 04 6c 6f 61 64 1f 02 03 01 02 03 01 02 03 ...load......... +| 3616: 00 03 6d 61 78 1c 02 0c 01 02 02 01 02 02 02 05 ..max........... +| 3632: 65 6d 6f 72 79 1c 02 03 01 02 03 01 02 03 04 04 emory........... +| 3648: 73 79 73 35 16 02 03 01 02 03 01 02 03 01 06 6e sys5...........n +| 3664: 6f 63 61 73 65 02 06 01 02 02 13 06 00 f2 02 03 ocase........... +| 3680: 06 01 12 02 13 06 01 02 02 03 06 01 02 02 03 06 ................ +| 3696: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3712: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3728: 02 01 04 6f 6d 69 74 1f 02 02 01 02 02 01 02 02 ...omit......... +| 3744: 01 05 72 74 72 65 65 19 02 03 01 02 03 01 02 03 ..rtree......... +| 3760: 04 02 69 6d 01 06 01 02 02 03 06 01 02 02 03 06 ..im............ +| 3776: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3792: 02 02 03 06 01 02 02 03 06 01 02 02 8e 06 01 02 ................ +| 3808: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 ................ +| 3824: 01 0a 74 68 72 65 61 64 73 61 66 65 22 02 02 01 ..threadsafe.... +| 3840: 02 02 01 02 02 01 04 76 74 61 62 07 02 04 01 02 .......vtab..... +| 3856: 04 01 02 04 01 01 78 01 06 01 01 02 01 06 01 01 ......x......... +| 3872: 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 ................ +| 3888: 01 06 01 11 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 3904: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 3920: 01 01 02 01 06 01 01 01 01 06 01 01 02 01 06 01 ................ +| 3936: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 ................ +| 3952: 02 01 06 01 01 01 f1 06 01 01 02 ad 06 01 01 02 ................ +| 3968: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 3984: 06 01 01 01 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 4000: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 ................ +| 4016: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 ................ +| 4032: 02 01 06 01 01 02 01 06 01 01 02 04 15 13 0c 0c ................ +| 4048: 12 44 13 11 0f 47 13 0e fc 0e 11 10 0f 0e 10 0f .D...G.......... +| 4064: 44 0f 10 40 15 0f 07 01 03 00 14 24 5a 24 24 0f D..@.......$Z$$. +| 4080: 0a 03 00 24 00 00 00 00 01 01 01 00 01 01 01 01 ...$............ +| page 4 offset 12288 +| 0: 0a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 05 04 09 0c 01 02 ................ +| page 5 offset 16384 +| 0: 0d 00 00 00 24 0c 0a 00 00 00 00 00 00 00 00 00 ....$........... +| 3072: 00 00 00 00 00 00 00 00 00 00 18 24 05 00 25 0f ...........$..%. +| 3088: 19 54 48 52 45 41 44 53 41 46 45 3d 30 58 42 49 .THREADSAFE=0XBI +| 3104: 4e 41 52 59 18 23 05 00 25 0f 19 54 48 52 45 41 NARY.#..%..THREA +| 3120: 44 53 41 46 45 3d 30 58 4e 4f 43 41 53 45 17 8f DSAFE=0XNOCASE.. +| 3136: 05 00 25 0f 17 54 48 52 45 41 44 43 41 46 45 3d ..%..THREADCAFE= +| 3152: 30 58 52 54 52 49 4d 1f 21 05 00 33 0f 19 4f 4d 0XRTRIM.!..3..OM +| 3168: 49 54 20 4b 4f 41 44 21 45 58 54 45 4e 53 49 4f IT KOAD!EXTENSIO +| 3184: 4e 58 42 49 4e 41 52 59 1f 20 05 00 33 0f 19 4f NXBINARY. ..3..O +| 3200: 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 MIT LOAD EXTENSI +| 3216: 4f 4e 58 4e 4f 43 41 53 45 1e 1f 05 00 33 0f 17 ONXNOCASE....3.. +| 3232: 4f 4d 59 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 OMYT LOAD EXTENS +| 3248: 49 4f 4e 58 52 54 56 a9 4d 1f 1e 05 00 33 0f 19 IONXRTV.M....3.. +| 3264: 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 30 MAX MEMORY=50000 +| 3280: 30 30 30 57 42 49 4e 31 52 59 1f 1d 05 00 33 0f 000WBIN1RY....3. +| 3296: 19 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 .MAX MEMORY=5000 +| 3312: 30 30 30 30 58 4e 4f 43 41 53 45 1e 1c 05 00 32 0000XNOCASE....2 +| 3328: 0f 17 4e 41 58 20 4d 45 4d 4f 52 59 2d 35 30 30 ..NAX MEMORY-500 +| 3344: 30 30 30 30 30 58 52 54 52 49 4d 18 1b 05 00 25 00000XRTRIM....% +| 3360: 0f 19 45 4e 41 42 4c 45 20 52 54 52 45 45 58 42 ..ENABLE RTREEXB +| 3376: 49 4e 41 52 59 18 1a 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3392: 4c 45 20 52 54 52 45 45 59 4e 4f 43 41 53 45 17 LE RTREEYNOCASE. +| 3408: 19 66 00 25 0f 17 45 4e 41 42 4c 45 20 52 54 52 .f.%..ENABLE RTR +| 3424: 45 45 58 52 54 52 49 4d 1a 18 05 00 29 0f 19 45 EEXRTRIM....)..E +| 3440: 4e 41 42 4c 45 20 4d 45 4d 53 59 53 35 58 42 49 NABLE MEMSYS5XBI +| 3456: 4e 41 52 59 1a 17 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3472: 45 20 4d 45 4d 53 59 53 35 58 4e 4f 43 41 53 45 E MEMSYS5XNOCASE +| 3488: 19 16 05 00 29 0f 17 45 4e 41 42 4c 45 20 4d 45 ....)..ENABLE ME +| 3504: 4d 53 59 53 35 58 52 54 52 49 4d 18 15 05 10 25 MSYS5XRTRIM....% +| 3520: 0f 19 45 4e 40 42 4c 45 20 4a 53 4f 4e 31 58 42 ..EN@BLE JSON1XB +| 3536: 49 4e 41 52 59 18 14 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3552: 4c 45 20 4a 53 4f 4e 32 58 4e 4f 43 41 53 45 17 LE JSON2XNOCASE. +| 3568: 13 05 00 25 0f 17 45 4d 41 42 4c 45 20 4a 53 4f ...%..EMABLE JSO +| 3584: 4e 31 58 52 54 52 49 4d 1a 12 05 00 29 0f 19 45 N1XRTRIM....)..E +| 3600: 4e 41 42 4c 45 20 47 45 4f 50 4f 4c 59 58 42 49 NABLE GEOPOLYXBI +| 3616: 4e 41 52 59 1a 11 05 00 29 0f 19 45 4f 81 42 4c NARY....)..EO.BL +| 3632: 45 20 47 45 4f 50 4f 4c 59 58 4e 4f 43 51 53 45 E GEOPOLYXNOCQSE +| 3648: 19 10 05 00 29 0f 17 45 4e 41 42 4c 45 20 47 45 ....)..ENABLE GE +| 3664: 4f 50 4f 4c 59 58 52 54 52 49 4d 17 0f 05 00 23 OPOLYXRTRIM....# +| 3680: 0f 1a 45 4e 41 42 4c 45 20 46 54 53 35 58 42 49 ..ENABLE FTS5XBI +| 3696: 4e 41 52 59 17 0e 05 00 23 0f 19 45 4e 41 42 4c NARY....#..ENABL +| 3712: 45 20 46 54 53 35 48 4e 4f 43 41 53 45 16 1d 05 E FTS5HNOCASE... +| 3728: 00 23 0f a4 45 4e 41 42 4c 45 20 46 54 53 35 58 .#..ENABLE FTS5X +| 3744: 52 54 52 49 4d 17 0c 05 00 23 0f 19 45 4e 41 42 RTRIM....#..ENAB +| 3760: 4c 45 20 46 55 53 34 58 42 49 4e 41 52 59 17 0b LE FUS4XBINARY.. +| 3776: 05 00 23 0f 19 45 4e 41 42 4c 45 20 46 54 53 34 ..#..ENABLE FTS4 +| 3792: 57 4e 4f 43 41 53 45 16 0a 05 00 23 0f 17 45 4e WNOCASE....#..EN +| 3808: 41 42 4c 45 20 46 54 53 34 05 52 54 52 49 4d 1e ABLE FTS4.RTRIM. +| 3824: 09 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3840: 54 41 54 20 56 54 41 42 58 42 49 4e 41 52 59 1e TAT VTABXBINARY. +| 3856: 08 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3872: 54 41 54 20 56 54 41 42 58 4e 4f 43 41 53 45 1d TAT VTABXNOCASE. +| 3888: 07 05 00 31 0f 17 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3904: 54 41 54 20 56 54 41 42 58 52 54 52 49 4d 11 06 TAT VTABXRTRIM.. +| 3920: 05 00 17 0f 19 44 45 42 55 47 58 42 8a 4e 41 52 .....DEBUGXB.NAR +| 3936: 59 11 05 05 00 17 0f 19 44 45 42 55 47 58 4e 4f Y.......DEBUGXNO +| 3952: 43 41 53 45 10 04 05 00 17 0f 17 44 45 42 55 47 CASE.......DEBUG +| 3968: 58 52 54 52 49 4d 27 03 05 00 43 0f 19 43 4f 4d XRTRIM'...C..COM +| 3984: 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e 30 20 PILER=gcc-5.4.0 +| 4000: 32 30 31 36 30 36 30 39 58 42 49 4e 41 52 59 27 20160609XBINARY' +| 4016: 02 05 00 43 0f 19 43 4f 4d 50 49 4c 45 52 3f 87 ...C..COMPILER?. +| 4032: 63 63 2d 35 2e 34 2e 30 20 32 30 31 36 30 36 30 cc-5.4.0 2016060 +| 4048: 39 58 4e 4f 43 41 53 45 26 01 05 00 43 0f 17 43 9XNOCASE&...C..C +| 4064: 45 0d 60 59 4c 45 52 3d 67 63 63 2d 35 2e 34 2d E.`YLER=gcc-5.4- +| 4080: 30 20 32 30 31 36 30 36 30 39 00 00 00 00 00 00 0 20160609...... +| page 6 offset 20480 +| 3808: 06 24 03 00 12 02 01 01 06 23 03 00 12 02 01 01 .$.......#...... +| 3824: 06 22 03 01 12 02 01 01 06 21 03 00 12 03 01 01 .........!...... +| 3840: 06 20 03 00 12 03 01 01 06 1f 03 00 12 03 02 01 . .............. +| 3856: 06 1e 03 00 12 03 01 01 06 1d 03 00 12 03 01 01 ................ +| 3872: 06 1c 03 00 12 03 01 01 06 1b 03 00 12 02 01 01 ................ +| 3888: 06 1a 03 00 12 02 01 01 06 19 03 00 12 02 01 01 ................ +| 3904: 06 18 03 00 12 02 01 01 06 17 03 00 12 02 01 01 ................ +| 3920: 06 16 03 00 12 02 01 01 06 15 03 00 12 02 01 01 ................ +| 3936: 06 14 03 00 12 02 01 01 06 13 03 00 12 02 01 01 ................ +| 3952: 06 12 03 00 12 02 01 01 06 11 03 00 12 02 01 01 ................ +| 3968: 06 00 03 00 12 02 01 01 06 0f 03 00 12 02 01 01 ................ +| 3984: 06 0e 03 00 12 02 01 01 06 0d 03 00 12 02 01 01 ................ +| 4000: 06 0c 03 00 12 02 01 01 06 0b 03 00 12 02 01 01 ................ +| 4016: 06 0a 03 00 12 02 01 01 06 09 03 00 12 03 01 01 ................ +| 4032: 06 08 03 00 12 03 01 01 06 07 03 00 12 03 01 01 ................ +| 4048: 06 06 03 00 12 01 01 01 06 05 03 00 12 01 01 01 ................ +| 4064: 06 04 03 00 12 01 01 01 06 03 03 00 12 06 01 01 ................ +| 4080: 06 02 03 00 12 06 01 01 06 01 03 00 12 06 01 01 ................ +| page 7 offset 24576 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| end crash-3aef66940ace0c.db +}]} {} + +do_catchsql_test 65.1 { + SELECT ( MATCH (t1,591)) FROM t1 WHERE t1 MATCH 'e*eŸ' +} {1 {malformed database schema (t2) - invalid rootpage}} + +#------------------------------------------------------------------------- +# +reset_db +do_test 66.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 28672 pagesize 4096 filename crash-37cecb4e784e9f.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 07 .....@ ........ +| 96: 00 00 00 00 0d 00 00 00 07 0d d2 00 0f c4 0f 6d ...............m +| 112: 0f 02 0e ab 0e 4e 0d f6 0d d2 00 00 00 00 00 00 .....N.......... +| 3536: 00 00 22 07 06 17 11 11 01 31 74 61 62 6c 65 74 .........1tablet +| 3552: 32 74 32 07 43 52 45 41 54 45 20 54 41 42 4c 45 2t2.CREATE TABLE +| 3568: 20 74 32 28 78 29 56 06 06 17 1f 1f 01 7d 74 61 t2(x)V.......ta +| 3584: 62 6c 65 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 blet1_configt1_c +| 3600: 6f 6e 66 69 67 06 43 52 45 41 54 45 20 54 41 42 onfig.CREATE TAB +| 3616: 4c 45 20 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b LE 't1_config'(k +| 3632: 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 PRIMARY KEY, v) +| 3648: 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 5b 05 WITHOUT ROWID[. +| 3664: 07 17 21 21 01 81 01 74 61 62 6c 65 74 31 5f 64 ..!!...tablet1_d +| 3680: 6f 63 73 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 ocsizet1_docsize +| 3696: 05 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 .CREATE TABLE 't +| 3712: 31 5f 64 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 1_docsize'(id IN +| 3728: 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE +| 3744: 59 2c 20 73 7a 20 42 4c 4f 42 29 55 04 06 17 21 Y, sz BLOB)U...! +| 3760: 21 01 77 74 61 62 6c 65 74 31 5f 63 6f 6e 74 65 !.wtablet1_conte +| 3776: 6e 74 74 31 5f 63 6f 6e 74 65 6e 74 04 43 52 45 ntt1_content.CRE +| 3792: 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 6f ATE TABLE 't1_co +| 3808: 6e 74 65 6e 74 27 28 69 64 20 49 4e 54 45 47 45 ntent'(id INTEGE +| 3824: 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 63 R PRIMARY KEY, c +| 3840: 30 29 69 03 07 17 19 19 01 81 2d 74 61 62 6c 65 0)i.......-table +| 3856: 74 31 5f 69 64 78 74 31 5f 69 64 78 03 43 52 45 t1_idxt1_idx.CRE +| 3872: 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 69 64 ATE TABLE 't1_id +| 3888: 78 27 28 73 65 67 69 64 2c 20 74 65 72 6d 2c 20 x'(segid, term, +| 3904: 70 67 6e 6f 2c 20 50 52 49 4d 41 52 59 20 4b 45 pgno, PRIMARY KE +| 3920: 59 28 73 65 67 69 64 2c 20 74 65 72 6d 29 29 20 Y(segid, term)) +| 3936: 57 49 54 48 4f 55 54 20 52 4f 57 49 44 55 02 07 WITHOUT ROWIDU.. +| 3952: 17 1b 1b 01 81 01 74 61 62 6c 65 74 31 5f 64 61 ......tablet1_da +| 3968: 74 61 74 31 5f 64 61 74 61 02 43 52 45 41 54 45 tat1_data.CREATE +| 3984: 20 54 41 42 4c 45 20 27 74 31 5f 64 61 74 61 27 TABLE 't1_data' +| 4000: 28 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d (id INTEGER PRIM +| 4016: 41 52 49 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 ARI KEY, block B +| 4032: 4c 4f 42 29 3a 01 06 17 11 11 08 63 74 61 62 6c LOB):......ctabl +| 4048: 65 74 31 74 31 43 52 45 41 54 45 20 56 49 52 54 et1t1CREATE VIRT +| 4064: 55 41 4c 20 54 41 42 4c 45 20 74 31 20 55 53 49 UAL TABLE t1 USI +| 4080: 4e 47 20 66 74 73 35 28 63 6f 6e 74 65 6e 74 29 NG fts5(content) +| page 2 offset 4096 +| 0: 0d 00 00 00 03 0f bd 00 0f e8 0f ef 0f bd 00 01 ................ +| 4016: 00 00 00 00 00 00 00 00 00 00 00 00 00 24 84 80 .............$.. +| 4032: 80 80 80 01 03 00 4e 00 00 00 1e 06 30 61 62 61 ......N.....0aba +| 4048: 63 6b 01 02 02 04 02 66 74 02 02 02 04 04 6e 64 ck.....ft.....nd +| 4064: 6f 6e 03 02 02 04 0a 07 05 01 03 00 10 03 03 0f on.............. +| 4080: 0a 03 00 24 00 00 00 00 01 01 01 00 01 00 01 01 ...$............ +| page 3 offset 8192 +| 0: 0a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 05 04 09 0c 01 02 ................ +| page 4 offset 12288 +| 0: 0d 00 00 00 03 0f e0 00 0f f6 0f ec 0f e0 00 00 ................ +| 4064: 0a 03 03 00 1b 61 62 61 6e 64 6f 6e 08 02 03 00 .....abandon.... +| 4080: 17 61 62 61 66 74 08 01 03 00 17 61 62 61 63 6b .abaft.....aback +| page 5 offset 16384 +| 0: 0d 00 00 00 03 0f ee 00 0f fa 0f f4 0f ee 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 04 03 ................ +| 4080: 03 00 0e 01 04 02 03 00 0e 01 04 01 03 00 0e 01 ................ +| page 6 offset 20480 +| 0: 0a 00 00 01 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| page 7 offset 24576 +| 0: 0d 00 00 00 03 0f d6 00 0f f4 0f e1 0f d6 00 00 ................ +| 4048: 00 00 00 00 00 00 09 01 52 1b 72 65 62 75 69 6c ........R.rebuil +| 4064: 64 11 02 02 2b 69 6e 74 65 67 72 69 74 79 2d 63 d...+integrity-c +| 4080: 68 65 63 6b 0a 01 02 1d 6f 70 74 69 6d 69 7a 65 heck....optimize +| end crash-37cecb4e784e9f.db +}]} {} + +do_catchsql_test 66.1 { + INSERT INTO t1(t1) VALUES('integrity-check'); +} {/.*fts5: corrupt.*/} + +#------------------------------------------------------------------------- +# +reset_db +do_test 67.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 24576 pagesize 4096 filename crash-43ed0ad79c0194.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 00 .....@ ........ +| 96: 00 00 00 00 0d 00 00 00 06 0d e2 00 0f c4 0f 6a ...............j +| 112: 0e fc 0e 9d 0e 3d 0d e2 01 00 00 00 00 00 00 00 .....=.......... +| 3552: 00 00 59 06 06 17 21 21 01 7f 74 61 62 6c 65 74 ..Y...!!..tablet +| 3568: 74 74 5f 63 6f 6e 66 69 67 74 74 74 5f 63 6f 6e tt_configttt_con +| 3584: 66 69 67 06 43 52 45 41 54 45 20 54 41 42 4c 45 fig.CREATE TABLE +| 3600: 20 27 74 74 74 5f 63 6f 6e 66 69 67 27 28 6b 20 'ttt_config'(k +| 3616: 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 20 PRIMARY KEY, v) +| 3632: 57 49 54 48 4f 55 54 20 52 4f 57 49 44 5e 05 07 WITHOUT ROWID^.. +| 3648: 17 23 23 01 81 03 74 61 62 6c 65 74 74 74 5f 64 .##...tablettt_d +| 3664: 6f 63 73 69 7a 65 74 74 74 5f 64 6f 63 73 69 7a ocsizettt_docsiz +| 3680: 65 05 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 e.CREATE TABLE ' +| 3696: 74 74 74 5f 64 6f 63 73 69 7a 65 27 28 69 64 20 ttt_docsize'(id +| 3712: 49 4e 54 45 47 45 52 20 51 52 49 4d 41 52 59 20 INTEGER QRIMARY +| 3728: 4b 45 59 2c 20 73 7a 20 42 4c 4f 42 29 5d 04 07 KEY, sz BLOB)].. +| 3744: 17 23 23 01 81 01 74 61 62 6c 65 74 74 74 5f 63 .##...tablettt_c +| 3760: 6f 6e 74 65 6e 74 74 74 74 5f 63 6f 6e 74 65 6e ontentttt_conten +| 3776: 74 04 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 t.CREATE TABLE ' +| 3792: 74 74 74 5f 63 6f 6e 74 65 6e 74 27 28 69 64 20 ttt_content'(id +| 3808: 49 4e 54 45 47 45 52 20 50 52 49 4d 41 f1 59 20 INTEGER PRIMA.Y +| 3824: 4b 45 59 2c 20 63 30 2c 20 63 31 29 6c 03 07 17 KEY, c0, c1)l... +| 3840: 1b 1b 01 81 2f 74 61 62 6c 65 74 74 74 5f 69 64 ..../tablettt_id +| 3856: 78 74 74 74 5f 69 64 78 03 43 52 45 41 54 45 20 xttt_idx.CREATE +| 3872: 54 41 42 4c 45 20 27 74 74 74 5f 69 64 78 27 28 TABLE 'ttt_idx'( +| 3888: 73 65 67 69 64 2c 20 74 65 72 6d 2c 20 70 67 6e segid, term, pgn +| 3904: 6f 2c 20 50 52 49 4d 41 52 59 20 4b 45 59 28 73 o, PRIMARY KEY(s +| 3920: 65 67 69 64 2c 20 74 65 72 6d 29 29 20 57 49 54 egid, term)) WIT +| 3936: 48 4f 55 54 20 52 4f 57 49 44 58 02 07 17 1d 1d HOUT ROWIDX..... +| 3952: 01 81 03 74 61 62 6c 65 74 74 74 5f 64 61 74 61 ...tablettt_data +| 3968: 74 74 74 5f 64 61 74 61 02 43 52 45 41 54 45 20 ttt_data.CREATE +| 3984: 54 41 42 4c 45 20 27 74 74 74 5f 64 61 74 61 27 TABLE 'ttt_data' +| 4000: 28 69 64 20 49 4e 54 45 47 55 52 20 50 52 49 4d (id INTEGUR PRIM +| 4016: 41 52 59 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 ARY KEY, block B +| 4032: 4c 50 42 29 3a 02 06 17 13 13 08 5f 74 61 62 6c LPB):......_tabl +| 4048: 65 74 74 74 74 74 74 43 52 45 41 54 45 20 56 49 ettttttCREATE VI +| 4064: 52 54 55 41 4c 20 54 41 42 4c 45 20 74 74 74 20 RTUAL TABLE ttt +| 4080: 55 53 49 4e 47 20 66 74 73 35 28 61 2c 20 62 29 USING fts5(a, b) +| page 2 offset 4096 +| 0: 0d 0f 44 00 05 0e 71 00 0f e7 0e 81 0f af 0f 58 ..D...q........X +| 16: 0e 98 01 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 3712: 00 15 0a 03 00 30 00 00 00 00 01 03 03 00 03 01 .....0.......... +| 3728: 01 01 02 01 01 03 01 01 81 24 8c 80 80 80 80 01 .........$...... +| 3744: 04 00 82 4c 00 00 00 9b 02 30 65 03 1a 12 05 05 ...L.....0e..... +| 3760: 07 05 01 01 04 03 03 08 04 03 01 2e 02 05 f7 07 ................ +| 3776: 01 e6 f5 07 05 01 01 04 03 03 01 22 03 18 03 03 ................ +| 3792: 08 03 03 02 01 65 03 1e 03 05 05 04 05 05 01 01 .....e.......... +| 3808: 03 06 03 f4 06 04 03 00 36 03 ff 05 04 05 05 04 ........6....... +| 3824: 05 05 04 05 04 f1 01 03 06 04 04 06 04 04 06 04 ................ +| 3840: 04 07 04 03 03 01 65 03 14 04 05 07 05 05 01 01 ......e......... +| 3856: 02 08 a5 01 20 04 05 01 94 f7 05 07 05 05 01 01 .... ........... +| 3872: 02 08 0a 0a 0a 04 01 65 03 02 0a 00 06 0a 0a 0a .......e........ +| 3888: 05 01 65 03 06 a7 01 0a 01 0a 01 01 0a 0a 0a 04 ..e............. +| 3904: 2b 31 21 0b 0f ef 00 14 2a 00 00 00 00 01 02 02 +1!.....*....... +| 3920: 00 02 01 01 01 02 11 01 50 88 80 80 80 80 01 04 ........P....... +| 3936: 00 81 24 00 00 00 47 02 30 65 02 1a 02 05 05 07 ..$...G.0e...... +| 3952: 05 e6 01 07 aa e3 08 03 03 02 01 65 02 1e 03 05 ...........e.... +| 3968: 05 05 04 f5 01 01 03 06 04 04 06 04 13 03 01 65 ...............e +| 3984: 02 14 04 05 07 05 05 01 f7 f2 08 0a 04 01 65 02 ..............e. +| 4000: 02 0a 05 01 65 02 06 00 f1 0a 04 12 14 0f 06 31 ....e..........1 +| 4016: 84 80 80 80 80 01 03 00 68 00 00 00 2b 02 30 65 ........h...+.0e +| 4032: 01 10 02 05 05 00 01 04 03 03 02 01 65 01 12 03 ............e... +| 4048: 05 05 01 01 03 06 04 03 03 01 65 01 0e 04 05 04 ..........e..... +| 4064: 01 01 02 08 04 0d 0e 06 01 03 00 12 04 4c 4c 00 .............LL. +| 4080: 00 00 11 24 00 00 00 00 01 01 01 00 01 01 01 02 ...$............ +| page 3 offset 8192 +| 0: 0a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| page 4 offset 12288 +| 3600: 00 00 00 00 00 00 00 00 00 00 81 52 04 06 00 81 ...........R.... +| 3616: 5d 81 55 65 20 65 65 20 65 65 65 20 65 20 65 65 ].Ue ee eee e ee +| 3632: 20 65 65 65 28 15 20 65 65 20 65 65 65 65 20 65 eee(. ee eeee e +| 3648: 65 20 65 65 65 20 65 20 65 65 20 65 65 65 20 65 e eee e ee eee e +| 3664: 20 65 65 20 65 65 65 65 20 65 66 20 65 65 55 20 ee eeee ef eeU +| 3680: 65 20 65 55 20 65 65 65 20 65 20 65 65 20 65 65 e eU eee e ee ee +| 3696: 65 64 20 65 61 c0 65 65 65 20 65 20 65 65 20 65 ed ea.eee e ee e +| 3712: 65 65 20 79 20 65 65 20 65 65 65 65 65 65 20 65 ee y ee eeeeee e +| 3728: 65 1f 65 20 65 20 65 20 65 65 20 65 65 65 20 65 e.e e e ee eee e +| 3744: 65 20 65 65 65 65 65 20 65 65 20 65 20 65 20 65 e eeeee ee e e e +| 3760: 20 65 65 20 65 65 65 20 6b 85 20 65 65 65 66 65 ee eee k. eeefe +| 3776: 20 65 65 10 65 20 65 20 65 20 65 65 20 65 65 65 ee.e e e ee eee +| 3792: 20 65 65 20 65 65 65 65 65 20 65 65 20 65 20 65 ee eeeee ee e e +| 3808: 20 65 20 65 65 20 65 65 65 20 65 65 20 65 65 6a e ee eee ee eej +| 3824: 03 04 00 75 71 65 20 65 65 20 65 65 65 20 65 30 ...uqe ee eee e0 +| 3840: 65 65 20 65 65 65 20 65 20 65 65 20 65 65 65 65 ee eee e ee eeee +| 3856: 20 65 65 20 65 65 65 20 65 1f 65 65 20 65 65 65 ee eee e.ee eee +| 3872: 20 65 20 65 65 20 65 65 65 65 65 66 20 65 65 20 e ee eeeeef ee +| 3888: 65 21 27 20 65 20 55 65 20 66 65 64 20 65 65 00 e!' e Ue fed ee. +| page 5 offset 16384 +| 4064: 00 00 00 00 05 04 03 00 10 11 20 05 03 03 00 10 .......... ..... +| 4080: 11 11 05 02 03 00 00 11 11 05 01 03 00 10 09 09 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 01 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| end crash-43ed0ad79c0194.db +}]} {} + +do_catchsql_test 67.1 { + SELECT snippet(ttt, null,null, + EXISTS(SELECT 1 FROM ttt('e NuOT ee*e*ÏNuOY ee*') ) , '', + (SELECT 1 FROM ttt('eu NuOT ee*e* NuOY ee*')) + ), * FROM ttt('e') +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +# +reset_db +do_test 68.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 32768 pagesize 4096 filename crash-41234e232809e7.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 08 .....@ ........ +| 32: 00 00 00 02 00 00 00 01 00 00 00 09 00 00 00 04 ................ +| 96: 00 00 00 00 0d 0f c7 00 07 0d 92 00 0f 8d 0f 36 ...............6 +| 112: 0e cb 0e 6b 0e 0e 0d b6 0d 92 0d 92 00 00 00 00 ...k............ +| 3472: 00 00 22 08 06 17 11 11 01 31 74 61 62 6c 65 74 .........1tablet +| 3488: 32 74 32 08 43 52 45 41 54 45 20 54 41 42 4c 45 2t2.CREATE TABLE +| 3504: 20 74 32 28 78 29 56 07 06 17 1f 1f 01 7d 74 61 t2(x)V.......ta +| 3520: 62 6c 65 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 blet1_configt1_c +| 3536: 6f 6e 66 69 67 07 43 52 45 41 54 45 20 54 41 42 onfig.CREATE TAB +| 3552: 4c 45 20 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b LE 't1_config'(k +| 3568: 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 PRIMARY KEY, v) +| 3584: 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 5b 06 WITHOUT ROWID[. +| 3600: 07 17 21 21 01 81 01 74 61 62 6c 65 74 31 5f 64 ..!!...tablet1_d +| 3616: 6f 63 73 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 ocsizet1_docsize +| 3632: 06 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 .CREATE TABLE 't +| 3648: 31 5f 64 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 1_docsize'(id IN +| 3664: 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE +| 3680: 59 2c 20 73 7a 20 42 4c 4f 42 29 5e 05 07 17 21 Y, sz BLOB)^...! +| 3696: 21 01 81 07 74 61 62 6c 65 74 31 5f 63 6f 6e 74 !...tablet1_cont +| 3712: 65 6e 74 74 31 5f 63 6f 6e 74 65 6e 74 05 43 52 entt1_content.CR +| 3728: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 EATE TABLE 't1_c +| 3744: 6f 6e 74 65 6e 74 27 28 69 64 20 49 4e 54 45 47 ontent'(id INTEG +| 3760: 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 ER PRIMARY KEY, +| 3776: 63 30 2c 20 63 31 2c 20 63 32 29 69 04 07 17 19 c0, c1, c2)i.... +| 3792: 19 01 81 2d 74 61 62 6c 65 74 31 5f 69 64 78 74 ...-tablet1_idxt +| 3808: 31 5f 69 64 78 04 43 52 45 41 54 45 20 54 41 42 1_idx.CREATE TAB +| 3824: 4c 45 20 27 74 31 5f 69 64 78 27 28 73 65 67 69 LE 't1_idx'(segi +| 3840: 64 2c 20 74 65 72 6d 2c 20 70 67 6e 6f 2c 20 50 d, term, pgno, P +| 3856: 52 49 4d 41 52 59 20 4b 45 59 28 73 65 67 69 64 RIMARY KEY(segid +| 3872: 2c 20 74 65 72 6d 29 29 20 57 49 54 48 4f 55 54 , term)) WITHOUT +| 3888: 20 52 4f 57 49 44 55 03 07 17 1b 1b 01 81 01 74 ROWIDU........t +| 3904: 61 62 6c 65 74 31 5f 64 61 74 61 74 31 5f 64 61 ablet1_datat1_da +| 3920: 74 61 03 43 52 45 41 54 45 20 54 41 42 4c 45 20 ta.CREATE TABLE +| 3936: 27 74 31 5f 64 61 74 61 27 28 69 64 20 49 4e 54 't1_data'(id INT +| 3952: 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 EGER PRIMARY KEY +| 3968: 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 29 38 02 06 , block BLOB)8.. +| 3984: 17 11 11 08 5f 74 61 62 6c 65 74 31 74 31 43 52 ...._tablet1t1CR +| 4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 35 LE t1 USING fts5 +| 4032: 28 61 2c 62 2c 63 29 00 00 00 00 00 00 00 00 00 (a,b,c)......... +| page 3 offset 8192 +| 0: 0d 00 00 00 03 0c 94 00 0f e6 0f ef 0c 94 00 00 ................ +| 16: 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 ................ +| 3216: 00 00 00 00 86 4a 84 80 80 80 80 01 04 00 8d 18 .....J.......... +| 3232: 00 00 03 2b 02 30 30 01 02 06 01 02 06 01 02 06 ...+.00......... +| 3248: 1f 02 03 01 02 03 01 02 03 01 08 32 30 31 36 30 ...........20160 +| 3264: 36 30 39 01 02 07 01 02 07 01 02 07 00 01 34 01 609...........4. +| 3280: 02 05 01 02 05 01 02 05 01 01 35 01 02 04 01 02 ..........5..... +| 3296: 04 01 02 04 02 07 30 30 30 30 30 30 30 1c 02 04 ......0000000... +| 3312: 01 02 04 01 02 03 f1 06 62 69 6e 62 72 79 03 06 ........binbry.. +| 3328: 01 02 02 03 06 01 02 02 03 06 01 02 01 03 16 01 ................ +| 3344: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3360: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 ................ +| 3376: 03 04 71 02 02 03 06 11 02 02 01 08 63 6f 6d 70 ..q.........comp +| 3392: 69 6c 65 72 01 02 02 01 02 02 01 02 02 01 06 64 iler...........d +| 3408: 62 73 74 61 74 07 02 03 01 02 03 01 02 03 02 04 bstat........... +| 3424: 65 62 75 67 04 02 02 01 02 02 01 02 02 01 06 65 ebug...........e +| 3440: 6e 61 62 6c 65 07 02 02 01 02 02 01 02 02 01 02 nable........... +| 3456: 02 01 02 02 01 02 02 01 02 02 01 02 02 01 03 02 ................ +| 3472: 00 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 ................ +| 3488: 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 ................ +| 3504: 02 01 02 02 02 08 78 74 65 6e 73 69 6f 6e 1f 02 ......xtension.. +| 3520: 04 01 02 04 01 02 04 01 04 66 74 73 34 0a 02 03 .........fts4... +| 3536: 01 02 03 01 02 03 04 01 35 0d 02 03 01 02 03 01 ........5....... +| 3552: 02 03 01 03 66 63 63 01 02 03 01 02 03 01 02 03 ....fcc......... +| 3568: 02 06 65 6f 70 6f 6c 79 10 02 03 01 02 03 01 02 ..eopoly........ +| 3584: 03 01 05 6a 73 6f 5e 31 13 02 03 01 02 03 01 02 ...jso^1........ +| 3600: 03 01 04 6c 6f 61 64 1f 02 03 01 02 03 01 02 03 ...load......... +| 3616: 01 03 6d 61 78 1c 02 02 01 02 02 01 02 02 02 05 ..max........... +| 3632: 65 6d 6f 72 79 1c 02 03 01 02 03 01 02 03 04 04 emory........... +| 3648: 73 79 73 35 16 02 03 01 02 03 01 02 03 01 06 6e sys5...........n +| 3664: 6f 63 61 73 65 02 06 01 02 02 03 06 01 02 02 03 ocase........... +| 3680: 06 01 02 02 03 06 01 02 02 03 06 01 02 02 13 06 ................ +| 3696: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3712: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3728: 02 01 04 6f 6d 69 74 1f 02 02 01 02 02 01 02 02 ...omit......... +| 3744: 01 05 72 74 72 65 65 19 02 03 01 02 03 01 02 03 ..rtree......... +| 3760: 04 02 69 6d 01 06 01 02 02 03 06 01 12 02 03 06 ..im............ +| 3776: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3792: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3808: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 ................ +| 3824: 01 0a 74 68 72 65 61 64 73 61 66 65 22 02 02 01 ..threadsafe.... +| 3840: 02 02 01 02 02 01 04 76 74 61 62 07 02 04 01 02 .......vtab..... +| 3856: 04 01 02 04 01 01 78 01 06 01 01 02 01 06 01 01 ......x......... +| 3872: 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 ................ +| 3888: 01 06 01 01 02 01 06 01 01 02 01 05 f1 01 02 01 ................ +| 3904: 06 01 01 02 01 06 01 5b 02 01 06 01 01 02 01 06 .......[........ +| 3920: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 ................ +| 3936: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 ................ +| 3952: 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 ................ +| 3968: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 3984: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 4000: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 ................ +| 4016: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 ................ +| 4032: 02 01 06 01 01 02 01 06 01 01 02 04 15 13 0c 0c ................ +| 4048: 12 44 13 11 0f 47 13 0f 0c 0e 11 10 0f 0e 10 0f .D...G.......... +| 4064: 44 0f 10 40 15 0f 07 01 03 00 14 24 5a 24 24 0f D..@.......$Z$$. +| 4080: 0a 03 00 24 00 00 00 00 01 01 01 00 01 01 01 01 ...$............ +| page 4 offset 12288 +| 0: 0a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 05 04 09 0b 01 02 ................ +| page 5 offset 16384 +| 0: 0d 00 00 00 24 0c 0a 00 0f d8 0f af 0f 86 0f 74 ....$..........t +| 16: 0f 61 0f 4e 0f 2f 0f 0f 0e ef 0e d7 0e be 0e a5 .a.N./.......... +| 32: 0e 8d 0e 74 0e 5b 0e 40 0e 24 0e 08 0d ef 0d d5 ...t.[.@.$...... +| 48: 0d bb 0d a0 0d 84 0d 68 0d 4f 0d 35 0d 1b 0c fb .......h.O.5.... +| 64: 0c da 0c b9 0c 99 0c 78 0c 57 0c 3e 0c 24 0c 0a .......x.W.>.$.. +| 3072: 00 00 00 00 00 00 00 00 00 00 18 24 05 00 25 0f ...........$..%. +| 3088: 19 54 48 52 45 41 44 53 41 46 45 3d 30 58 42 49 .THREADSAFE=0XBI +| 3104: 4f 41 52 59 18 23 05 00 25 0f 19 54 48 52 45 41 OARY.#..%..THREA +| 3120: 44 53 41 46 45 3d 30 58 4e 4f 43 41 53 45 17 22 DSAFE=0XNOCASE.. +| 3136: 05 00 25 0f 17 54 48 52 45 41 44 53 41 46 45 3d ..%..THREADSAFE= +| 3152: 30 58 52 54 52 49 4d 1f 21 05 00 33 0f 19 4f 4d 0XRTRIM.!..3..OM +| 3168: 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 4f IT LOAD EXTENSIO +| 3184: 4e 58 42 49 4e 41 52 59 1f 20 05 00 33 0f 19 4f NXBINARY. ..3..O +| 3200: 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 MIT LOAD EXTENSI +| 3216: 4f 4e 58 4e 4f 43 41 53 45 1e 1f 05 00 33 0f 17 ONXNOCASE....3.. +| 3232: 4f 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 OMIT LOAD EXTENS +| 3248: 49 4f 4e 58 52 54 52 49 4d 1f 1e 05 00 33 0f 19 IONXRTRIM....3.. +| 3264: 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 30 MAX MEMORY=50000 +| 3280: 30 30 30 58 42 49 4e 41 52 59 1f 1d 05 00 33 0f 000XBINARY....3. +| 3296: 19 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 .MAX MEMORY=5000 +| 3312: 30 30 30 30 58 4e 4f 43 41 53 45 1e 1c 05 00 33 0000XNOCASE....3 +| 3328: 0f 17 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 ..MAX MEMORY=500 +| 3344: 30 30 30 30 30 58 52 54 52 49 4d 18 1b 05 00 25 00000XRTRIM....% +| 3360: 0f 19 45 4e 41 42 4c 45 20 52 54 52 45 45 58 42 ..ENABLE RTREEXB +| 3376: 49 4e 41 52 59 18 1a 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3392: 4c 45 20 52 54 52 46 45 58 4e 4f 43 41 53 45 17 LE RTRFEXNOCASE. +| 3408: 19 05 00 25 0f 17 45 4e 41 42 4c 45 20 52 54 52 ...%..ENABLE RTR +| 3424: 45 45 58 52 54 52 49 4d 1a 18 05 00 29 0f 19 45 EEXRTRIM....)..E +| 3440: 49 41 42 4c 45 20 4d 45 4d 53 59 53 35 58 42 49 IABLE MEMSYS5XBI +| 3456: 4e 41 52 59 1a 17 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3472: 45 20 4d 45 4d 53 59 53 35 58 4e 4f 43 41 53 45 E MEMSYS5XNOCASE +| 3488: 19 16 05 00 29 0f 17 45 4e 41 42 4c 45 20 4d 45 ....)..ENABLE ME +| 3504: 4d 53 59 53 35 58 52 54 52 49 4d 18 15 05 00 25 MSYS5XRTRIM....% +| 3520: 0f 19 45 4e 41 42 4c 45 20 4a 53 4f 4e 31 58 42 ..ENABLE JSON1XB +| 3536: 49 4e 41 52 59 18 14 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3552: 4c 45 20 4a 53 4f 4e 31 58 4e 4f 43 41 53 45 17 LE JSON1XNOCASE. +| 3568: 13 05 00 25 0f 17 45 4e 41 42 4c 45 20 4a 53 4f ...%..ENABLE JSO +| 3584: 4e 31 58 52 54 52 49 4d 1a 12 05 00 29 0f 19 45 N1XRTRIM....)..E +| 3600: 4e 41 42 4c 45 20 47 45 4f 50 4f 4c 59 57 42 49 NABLE GEOPOLYWBI +| 3616: 4e 41 52 59 1a 11 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3632: 45 20 47 45 4f 50 4f 4c 59 58 4e 4f 42 41 53 45 E GEOPOLYXNOBASE +| 3648: 19 10 05 00 29 0f 17 45 4e 41 42 4c 45 20 47 45 ....)..ENABLE GE +| 3664: 4f 50 4f 4c 59 58 52 54 52 49 4d 17 0f 05 00 23 OPOLYXRTRIM....# +| 3680: 0f 19 45 4e 41 42 4c 45 20 46 54 53 35 58 42 49 ..ENABLE FTS5XBI +| 3696: 4e 41 52 59 17 0e 05 00 23 0f 19 45 4e 41 42 4c NARY....#..ENABL +| 3712: 45 20 46 54 53 35 58 4e 4f 43 41 53 45 16 0d 05 E FTS5XNOCASE... +| 3728: 00 23 0f 17 45 4e 41 42 4c 45 20 46 54 53 35 58 .#..ENABLE FTS5X +| 3744: 52 54 52 49 4d 17 0c 05 00 23 0f 19 45 4e 41 42 RTRIM....#..ENAB +| 3760: 4c 45 20 46 54 53 34 58 42 49 4e 41 52 59 17 0b LE FTS4XBINARY.. +| 3776: 05 00 23 0f 19 45 4e 41 42 4c 45 20 46 54 53 34 ..#..ENABLE FTS4 +| 3792: 58 4e 4f 43 41 53 45 16 0a 05 00 23 0f 17 45 4e XNOCASE....#..EN +| 3808: 41 42 4c 45 20 46 54 53 34 58 52 54 52 49 4d 1e ABLE FTS4XRTRIM. +| 3824: 09 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3840: 54 41 54 20 56 54 41 42 58 42 49 4e 41 52 59 1e TAT VTABXBINARY. +| 3856: 08 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3872: 54 41 54 20 56 54 41 42 58 4e 4f 43 41 53 45 1d TAT VTABXNOCASE. +| 3888: 07 05 00 31 0f 17 b7 4e 41 42 4c 45 20 44 42 53 ...1...NABLE DBS +| 3904: 54 41 54 20 66 54 41 42 58 52 54 52 49 4d 11 06 TAT fTABXRTRIM.. +| 3920: 05 00 17 0f 19 44 45 42 55 47 58 42 49 4e 41 52 .....DEBUGXBINAR +| 3936: 59 11 05 05 00 17 0f 19 44 45 42 55 47 58 4e 4f Y.......DEBUGXNO +| 3952: 43 41 53 45 10 04 05 00 17 0f 17 44 45 42 55 47 CASE.......DEBUG +| 3968: 58 62 54 52 49 4d 27 03 05 00 43 0f 19 43 4f 4d XbTRIM'...C..COM +| 3984: 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e 30 20 PILER=gcc-5.4.0 +| 4000: 32 30 31 36 30 36 30 39 52 02 4a 4e 41 52 59 27 20160609R.JNARY' +| 4016: 02 05 00 43 0f 19 43 4f 4d 50 49 4c 45 52 3d 67 ...C..COMPILER=g +| 4032: 63 63 2d 35 2e 34 2e 30 20 32 30 31 36 30 36 30 cc-5.4.0 2016060 +| 4048: 39 58 4e 4f 43 41 53 45 26 01 05 00 43 0f 17 43 9XNOCASE&...C..C +| 4064: 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e OMPILER=gcc-5.4. +| 4080: 30 20 32 30 31 36 30 36 30 39 58 52 54 52 49 4d 0 20160609XRTRIM +| page 6 offset 20480 +| 0: 0d 00 00 00 24 0e e0 00 0f f8 0f f0 0f e8 0f e0 ....$........... +| 16: 0f d8 0f d0 0f c8 0f c0 0f b8 0f b0 0f a8 0f a0 ................ +| 32: 0f 98 0f 90 0f 88 0f 80 0f 78 0f 70 0f 68 0f 60 .........x.p.h.` +| 48: 0f 58 0f 50 0f 48 0f 40 0f 38 0f 30 0f 28 0f 20 .X.P.H.@.8.0.(. +| 64: 0f 18 0f 10 0f 08 0f 00 0e f8 0e f0 0e e8 0e e0 ................ +| 3808: 06 24 03 00 12 02 01 01 06 23 03 00 12 02 01 01 .$.......#...... +| 3824: 06 22 03 00 12 02 01 01 06 21 03 00 12 03 01 01 .........!...... +| 3840: 06 20 03 00 12 03 01 01 06 1f 03 00 12 03 01 01 . .............. +| 3856: 06 1e 03 00 12 03 01 01 06 1d 03 00 12 03 01 01 ................ +| 3872: 06 1c 03 00 12 03 01 01 06 1b 03 00 12 02 01 01 ................ +| 3888: 06 1a 03 00 12 02 01 01 06 19 03 00 12 02 01 01 ................ +| 3904: 06 18 03 00 12 02 01 01 06 17 03 00 12 02 01 01 ................ +| 3920: 06 16 03 00 12 02 01 01 06 15 03 00 12 02 01 01 ................ +| 3936: 06 14 03 00 12 02 01 01 06 13 03 00 12 02 01 01 ................ +| 3952: 06 12 03 00 12 02 01 01 06 11 03 00 12 02 01 01 ................ +| 3968: 06 10 03 00 12 02 01 01 06 0f 03 00 12 02 01 01 ................ +| 3984: 06 0e 03 00 12 02 01 01 06 0d 03 00 12 02 01 01 ................ +| 4000: 06 0c 03 00 12 02 01 01 06 0b 03 00 12 02 01 01 ................ +| 4016: 06 0a 03 00 12 02 01 01 06 09 03 00 12 03 01 01 ................ +| 4032: 06 08 03 00 12 03 01 01 06 07 03 00 12 03 01 01 ................ +| 4048: 06 06 03 00 12 01 01 01 06 05 03 00 12 01 01 01 ................ +| 4064: 06 04 03 00 12 01 01 01 06 03 03 00 12 06 01 01 ................ +| 4080: 06 02 03 00 12 06 01 01 06 01 03 00 12 06 01 01 ................ +| page 7 offset 24576 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| page 8 offset 28672 +| 0: 0d 00 00 00 03 0f d6 00 0f f4 0f e9 0f d6 00 00 ................ +| 4048: 00 00 00 00 00 00 11 04 02 2b 69 6e 74 65 67 72 .........+integr +| 4064: 69 74 79 2d 63 68 65 63 6b 09 02 02 1b 72 65 62 ity-check....reb +| 4080: 75 69 6c 64 0a 01 02 1d 6f 70 74 69 5d 69 71 a5 uild....opti]iq. +| end crash-41234e232809e7.db +.testctrl prng_seed 1 db +}]} {} + +do_catchsql_test 68.1 { + PRAGMA reverse_unordered_selects=ON; + INSERT INTO t1(t1) SELECT x FROM t2; +} {1 {fts5: corruption on page 1, segment 1, table "t1"}} + +#------------------------------------------------------------------------- +reset_db +do_test 69.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 32768 pagesize 4096 filename crash-31c462b8b665d0.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 08 .....@ ........ +| 32: 00 00 00 02 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 96: 00 00 00 00 0d 0f c7 00 07 0d 92 00 0f 8d 0f 36 ...............6 +| 112: 0e cb 0e 6b 0e 0e 0d b6 0d 92 00 00 00 00 00 00 ...k............ +| 3472: 00 00 22 08 06 17 11 11 01 31 74 61 62 6c 65 74 .........1tablet +| 3488: 32 74 32 08 43 52 45 41 54 45 20 54 41 42 4c 45 2t2.CREATE TABLE +| 3504: 20 74 32 28 78 29 56 07 06 17 1f 1f 01 7d 74 61 t2(x)V.......ta +| 3520: 62 6c 65 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 blet1_configt1_c +| 3536: 6f 6e 66 69 67 07 43 52 45 41 54 45 20 54 41 42 onfig.CREATE TAB +| 3552: 4c 45 20 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b LE 't1_config'(k +| 3568: 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 PRIMARY KEY, v) +| 3584: 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 5b 06 WITHOUT ROWID[. +| 3600: 07 17 21 21 01 81 01 74 61 62 6c 65 74 31 5f 64 ..!!...tablet1_d +| 3616: 6f 63 73 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 ocsizet1_docsize +| 3632: 06 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 .CREATE TABLE 't +| 3648: 31 5f 64 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 1_docsize'(id IN +| 3664: 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE +| 3680: 59 2c 20 73 7a 20 42 4c 4f 42 29 5e 05 07 17 21 Y, sz BLOB)^...! +| 3696: 21 01 81 07 74 61 62 6c 65 74 31 5f 63 6f 6e 74 !...tablet1_cont +| 3712: 65 6e 74 74 31 5f 63 6f 6e 74 65 6e 74 05 43 52 entt1_content.CR +| 3728: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 EATE TABLE 't1_c +| 3744: 6f 6e 74 65 6e 74 27 28 69 64 20 49 4e 54 45 47 ontent'(id INTEG +| 3760: 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 ER PRIMARY KEY, +| 3776: 63 39 2c 20 63 31 2c 20 63 32 29 69 04 07 17 19 c9, c1, c2)i.... +| 3792: 19 01 81 2d 74 61 62 6c 65 74 31 5f 69 64 78 74 ...-tablet1_idxt +| 3808: 31 5f 69 64 78 04 43 52 45 41 54 45 20 54 41 42 1_idx.CREATE TAB +| 3824: 4c 45 20 27 74 31 5f 69 64 78 27 28 73 65 67 69 LE 't1_idx'(segi +| 3840: 64 2c 20 74 65 72 6d 2c 20 70 67 6e 6f 2c 20 50 d, term, pgno, P +| 3856: 52 49 4d 41 52 59 20 4b 45 59 28 73 65 67 69 64 RIMARY KEY(segid +| 3872: 2c 20 74 65 72 6d 29 29 20 57 49 54 48 4f 55 54 , term)) WITHOUT +| 3888: 20 52 4f 57 49 44 55 03 07 17 1b 1b 01 81 01 74 ROWIDU........t +| 3904: 61 62 6c 65 74 31 5f 64 61 74 61 74 31 5f 64 61 ablet1_datat1_da +| 3920: 74 61 03 43 52 45 41 54 45 20 54 41 42 4c 45 20 ta.CREATE TABLE +| 3936: 27 74 31 5f 64 61 74 61 27 28 69 64 20 49 4e 54 't1_data'(id INT +| 3952: 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 EGER PRIMARY KEY +| 3968: 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 29 38 02 06 , block BLOB)8.. +| 3984: 17 11 11 08 5f 74 61 62 6c 65 74 31 74 31 43 52 ...._tablet1t1CR +| 4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 35 LE t1 USING fts5 +| 4032: 28 61 2c 62 2c 63 29 00 00 00 00 00 00 00 00 00 (a,b,c)......... +| page 3 offset 8192 +| 0: 0d 00 00 00 03 0c 94 00 0f e6 0f ef 0c 94 00 00 ................ +| 3216: 00 00 00 00 86 4a 84 80 80 80 80 01 04 00 8d 18 .....J.......... +| 3232: 00 00 03 2b 02 30 30 01 02 06 01 02 06 01 02 06 ...+.00......... +| 3248: 1f 02 03 01 02 03 01 02 03 01 08 32 30 31 36 30 ...........20160 +| 3264: 36 30 39 01 02 07 01 02 07 01 02 07 01 01 34 01 609...........4. +| 3280: 02 05 01 02 05 01 02 05 01 01 35 01 02 04 01 02 ..........5..... +| 3296: 04 01 02 04 02 07 30 30 30 30 30 30 30 1c 02 04 ......0000000... +| 3312: 01 02 04 01 02 04 01 06 62 69 6e 61 72 79 03 06 ........binary.. +| 3328: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3344: 02 02 03 06 00 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3360: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 ................ +| 3376: 03 06 01 02 02 03 06 01 02 02 01 08 63 6f 6d 70 ............comp +| 3392: 69 6c 65 72 01 02 02 01 02 02 01 02 02 01 06 64 iler...........d +| 3408: 62 73 74 61 74 07 02 03 01 02 03 01 02 03 02 04 bstat........... +| 3424: 65 62 75 67 04 02 02 01 02 02 01 02 02 01 06 65 ebug...........e +| 3440: 6e 61 62 6c 65 07 02 02 01 02 02 01 02 02 01 02 nable........... +| 3456: 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 ................ +| 3472: 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 ................ +| 3488: 01 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 ................ +| 3504: 02 01 02 02 02 08 78 74 65 6e 73 69 6f 6e 1f 02 ......xtension.. +| 3520: 04 01 02 04 01 02 04 01 04 66 74 73 34 1a 02 03 .........fts4... +| 3536: 01 02 03 01 02 03 04 01 35 0d 02 03 01 02 03 01 ........5....... +| 3552: 02 03 01 03 67 63 63 01 aa 03 01 02 03 01 02 03 ....gcc......... +| 3568: 02 06 65 6f 70 6f 6c 79 10 02 03 02 02 03 01 02 ..eopoly........ +| 3584: 03 01 05 6a 73 6f 6e 31 13 02 03 01 02 03 01 02 ...json1........ +| 3600: 03 01 04 6c 6f 61 64 1f 02 03 01 02 03 01 02 03 ...load......... +| 3616: 01 03 6d 61 78 1c 02 02 01 02 02 01 02 02 02 05 ..max........... +| 3632: 65 6d 6f 72 79 1c 02 03 01 02 03 01 02 03 04 04 emory........... +| 3648: 73 79 73 35 16 02 03 01 02 03 01 02 03 01 06 6e sys5...........n +| 3664: 6f 63 61 73 65 02 06 01 02 02 03 06 01 02 02 03 ocase........... +| 3680: 06 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 ................ +| 3696: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3712: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3728: 02 01 04 6f 6d 69 74 1f 02 02 01 02 02 01 02 01 ...omit......... +| 3744: ff ff ff ff ff ff ff ff f0 00 00 00 00 00 01 02 ................ +| 3760: 58 81 96 4d 01 06 01 02 02 03 06 01 02 02 03 06 X..M............ +| 3776: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3792: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3808: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 ................ +| 3824: 01 0a 74 68 72 65 61 64 73 61 66 65 22 02 02 01 ..threadsafe.... +| 3840: 02 02 01 02 02 01 04 76 74 61 62 07 02 04 01 02 .......vtab..... +| 3856: 04 01 02 04 01 01 78 01 06 01 01 02 01 06 01 01 ......x......... +| 3872: 02 01 06 01 1e 02 01 06 01 01 02 01 06 01 01 02 ................ +| 3888: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 3904: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 3920: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 ................ +| 3936: 00 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 ................ +| 3952: 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 ................ +| 3968: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 3984: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 4000: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 ................ +| 4016: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 ................ +| 4032: 02 01 06 01 01 02 01 06 01 01 02 04 15 13 0c 0c ................ +| 4048: 12 44 13 11 0f 47 13 0f 0b 0e 11 10 0f 0e 10 0f .D...G.......... +| 4064: 44 0f 10 40 15 0f 07 01 03 00 14 24 5a 24 24 0f D..@.......$Z$$. +| 4080: 0a 03 00 24 00 00 00 00 01 01 01 00 01 01 01 01 ...$............ +| page 4 offset 12288 +| 0: 0a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 05 04 09 0c 01 02 ................ +| page 5 offset 16384 +| 0: 0d 00 00 00 24 0c 0a 00 0f d8 0f af 0f 86 0f 74 ....$..........t +| 16: 0f 61 0f 4e 0f 2f 0f 0f 0e ef 0e d7 0e be 0e a5 .a.N./.......... +| 32: 0e 8d 0e 74 0e 5b 0e 40 0e 24 0e 08 0d ef 0d d5 ...t.[.@.$...... +| 48: 0d bb 0d a0 0d 84 0d 68 0d 4f 0d 00 00 00 00 00 .......h.O...... +| 3072: 00 00 00 00 00 00 00 00 00 00 18 24 05 00 25 0f ...........$..%. +| 3088: 19 54 48 52 45 41 44 53 41 46 45 3d 30 58 42 49 .THREADSAFE=0XBI +| 3104: 4e 41 52 59 18 23 05 00 25 0f 19 54 48 52 45 41 NARY.#..%..THREA +| 3120: 44 53 41 46 45 3d 30 58 4e 4f 43 41 53 45 17 22 DSAFE=0XNOCASE.. +| 3136: 05 00 25 0f 17 54 48 52 45 41 44 53 41 46 45 3d ..%..THREADSAFE= +| 3152: 30 58 52 54 52 49 4d 1f 21 05 00 33 0f 19 4f 4d 0XRTRIM.!..3..OM +| 3168: 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 4f IT LOAD EXTENSIO +| 3184: 4e 58 42 49 4e 41 52 59 1f 20 05 00 33 d3 19 4f NXBINARY. ..3..O +| 3200: 4d 49 54 28 2c 4f 41 44 b2 04 55 85 44 54 e5 34 MIT(,OAD..U.DT.4 +| 3216: 94 f4 e5 84 e4 f4 34 15 34 51 e1 f0 50 03 30 f1 ......4.4Q..P.0. +| 3232: 74 f4 d4 95 42 04 c4 f4 14 42 04 55 85 44 54 e5 t...B....B.U.DT. +| 3248: 34 94 f4 e5 85 25 45 24 94 d1 f1 e0 50 03 30 f1 4....%E$....P.0. +| 3264: 94 d4 15 82 04 d4 54 d4 f5 25 93 d3 53 03 03 03 ......T..%..S... +| 3280: 03 03 03 05 84 24 94 e4 15 25 91 f1 d0 50 03 30 .....$...%...P.0 +| 3296: f1 94 d4 15 82 04 d4 54 d4 f5 25 93 d3 53 03 03 .......T..%..S.. +| 3312: 03 03 03 03 05 84 e4 f4 34 15 34 51 e1 c0 50 03 ........4.4Q..P. +| 3328: 30 f1 74 d4 15 82 04 d4 54 d4 f5 25 93 d3 53 03 0.t.....T..%..S. +| 3344: 03 03 03 03 03 05 85 25 45 24 94 d1 81 b0 50 02 .......%E$....P. +| 3360: 50 f1 94 54 e4 14 24 c4 52 05 25 45 24 54 55 84 P..T..$.R.%E$TU. +| 3376: 24 94 e4 15 25 91 81 a0 50 02 50 f1 94 54 e4 14 $...%...P.P..T.. +| 3392: 24 c4 52 05 25 45 24 54 55 84 e4 f4 34 15 34 51 $.R.%E$TU...4.4Q +| 3408: 71 90 50 02 50 f1 74 54 e4 14 24 c4 52 05 25 45 q.P.P.tT..$.R.%E +| 3424: 24 54 55 85 25 45 24 94 d1 a1 80 50 02 90 f1 94 $TU.%E$....P.... +| 3440: 54 e4 14 24 c4 52 04 d4 54 d5 35 95 33 55 84 24 T..$.R..T.5.3U.$ +| 3456: 94 e4 15 25 91 a1 70 50 02 90 f1 94 54 e4 14 24 ...%..pP....T..$ +| 3472: c4 52 04 d4 54 d5 35 95 33 55 84 e4 f4 34 15 34 .R..T.5.3U...4.4 +| 3488: 51 91 60 50 02 90 f1 74 54 e4 14 24 c4 52 04 d4 Q.`P...tT..$.R.. +| 3504: 54 d5 35 95 33 55 85 25 45 24 94 d1 81 50 50 02 T.5.3U.%E$...PP. +| 3520: 50 f1 94 54 e4 14 24 c4 52 04 a5 34 f4 e3 15 84 P..T..$.R..4.... +| 3536: 24 94 e4 15 25 91 81 40 50 02 50 f1 94 54 e4 14 $...%..@P.P..T.. +| 3552: 24 c4 52 04 a5 34 f4 e3 15 84 e4 f4 34 15 34 51 $.R..4......4.4Q +| 3568: 71 30 50 02 4f f1 74 54 e4 14 24 c4 52 04 a5 34 q0P.O.tT..$.R..4 +| 3584: f4 e3 15 85 25 45 24 94 d1 a1 20 50 02 90 f1 94 ....%E$... P.... +| 3600: 54 e4 14 24 c4 52 04 74 54 f5 04 f4 c5 95 84 24 T..$.R.tT......$ +| 3616: 94 e4 15 25 91 a1 10 50 02 90 f1 94 54 e4 14 24 ...%...P....T..$ +| 3632: c4 52 04 74 54 f5 04 f4 c5 95 84 e4 f4 34 15 34 .R.tT........4.4 +| 3648: 51 91 00 50 02 90 f1 74 54 e4 14 24 c4 51 f4 74 Q..P...tT..$.Q.t +| 3664: 54 f5 04 f4 c5 95 85 25 45 24 94 d1 70 f0 50 02 T......%E$..p.P. +| 3680: 30 f1 94 54 e4 14 24 c5 20 46 54 53 35 58 42 49 0..T..$. FTS5XBI +| 3696: 4e 41 52 59 17 0e 05 00 23 0f 19 45 4e 41 42 4c NARY....#..ENABL +| 3712: 45 20 46 54 53 35 58 4f 4f 43 41 53 45 16 0d 05 E FTS5XOOCASE... +| 3728: 00 23 0f 17 45 4e 41 42 4c 45 20 46 54 53 35 58 .#..ENABLE FTS5X +| 3744: 52 54 52 49 4d 17 0c 05 00 23 0f 19 45 4e 41 42 RTRIM....#..ENAB +| 3760: 4c 45 20 46 54 53 34 58 42 49 4e 41 52 59 97 0b LE FTS4XBINARY.. +| 3776: 05 00 23 0f 19 45 4e 41 42 4c 45 20 46 54 53 34 ..#..ENABLE FTS4 +| 3792: 58 4e 4f 43 41 53 45 16 0a 05 00 23 0f 17 45 4e XNOCASE....#..EN +| 3808: 41 42 4c 45 20 46 54 53 34 58 52 54 52 49 4d 1e ABLE FTS4XRTRIM. +| 3824: 09 05 00 3e 5f 19 45 4e 41 42 4c 45 20 44 42 53 ...>_.ENABLE DBS +| 3840: 44 41 54 20 56 54 41 42 58 42 49 4e 41 52 59 1e DAT VTABXBINARY. +| 3856: 08 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3872: 54 41 54 20 56 54 41 42 58 4e 4f 43 4d e3 45 1d TAT VTABXNOCM.E. +| 3888: 07 05 00 31 0f 17 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3904: 54 41 54 20 56 54 41 42 58 52 54 52 49 4d 11 06 TAT VTABXRTRIM.. +| 3920: 05 00 17 0f 19 44 45 42 55 47 58 42 49 4e 41 52 .....DEBUGXBINAR +| 3936: 59 11 05 05 00 17 0f 19 44 45 42 55 47 58 4e 4f Y.......DEBUGXNO +| 3952: 43 41 53 45 10 02 02 50 08 5f 17 44 45 42 55 47 CASE...P._.DEBUG +| 3968: 58 52 54 52 49 4d 27 03 05 00 44 0f 19 43 4f 4d XRTRIM'...D..COM +| 3984: 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e 30 20 PILER=gcc-5.4.0 +| 4000: 32 30 31 36 30 36 30 39 58 42 49 4e 41 52 59 27 20160609XBINARY' +| 4016: 02 05 00 43 0f 19 43 4f 4d 50 49 4c 45 52 3d 67 ...C..COMPILER=g +| 4032: 63 63 2d 35 2e 34 2e 30 20 32 30 31 36 30 36 30 cc-5.4.0 2016060 +| 4048: 39 58 4e 4f 43 41 53 45 26 01 05 00 43 c9 17 43 9XNOCASE&...C..C +| 4064: 4f 4d 50 49 4c 47 02 3d 67 63 63 2d 35 2e 34 2e OMPILG.=gcc-5.4. +| 4080: 30 20 32 30 31 36 30 36 30 39 58 52 54 52 49 4d 0 20160609XRTRIM +| page 6 offset 20480 +| 0: 0d 00 00 00 24 0e e0 00 0f f8 0f f0 0f e8 0f e0 ....$........... +| 16: 0f d8 0f d0 0f c8 0f c0 0f b8 0f b0 0f a8 0f a0 ................ +| 32: 0f 98 0f 90 0f 88 0f 80 0f 78 0f 70 0f 68 0f 60 .........x.p.h.` +| 48: 0f 58 0f 50 0f 48 0f 40 0f 38 00 00 00 00 00 00 .X.P.H.@.8...... +| 3808: 06 24 03 00 12 02 01 01 06 23 03 00 12 02 01 01 .$.......#...... +| 3824: 06 22 03 00 12 02 01 01 06 21 03 00 12 03 01 01 .........!...... +| 3840: 06 20 03 00 12 03 01 01 06 1f 03 00 12 03 01 01 . .............. +| 3856: 06 1e 03 00 12 03 01 01 06 1d 03 00 12 03 01 01 ................ +| 3872: 06 1c 03 00 12 03 01 01 06 1b 03 00 12 02 01 01 ................ +| 3888: 06 1a 03 00 12 02 01 01 06 19 03 00 12 02 01 01 ................ +| 3904: 06 18 03 00 12 02 01 01 06 17 03 00 12 02 01 01 ................ +| 3920: 06 16 03 00 12 02 01 01 06 15 03 00 12 02 01 01 ................ +| 3936: 06 14 03 00 12 02 01 01 06 13 03 00 12 02 01 01 ................ +| 3952: 06 12 03 00 12 02 01 01 06 11 03 00 12 02 01 01 ................ +| 3968: 06 10 03 00 12 02 01 01 06 1f 03 00 12 02 01 01 ................ +| 3984: 06 0e 03 00 12 02 01 01 06 0d 03 00 12 02 01 01 ................ +| 4000: 06 0c 03 00 12 02 01 01 06 0b 03 00 12 02 01 01 ................ +| 4016: 06 0a 03 00 12 02 01 01 06 09 03 00 12 03 01 01 ................ +| 4032: 06 08 03 00 12 03 01 01 06 07 03 00 12 03 01 01 ................ +| 4048: 06 06 03 00 12 01 01 01 06 05 03 00 12 01 01 01 ................ +| 4064: 06 04 03 00 12 01 01 01 06 03 03 00 12 06 01 01 ................ +| 4080: 06 02 03 00 12 06 01 01 06 01 03 00 12 06 01 01 ................ +| page 7 offset 24576 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| page 8 offset 28672 +| 0: 0d 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 4048: 00 00 00 00 00 00 11 03 02 2b 69 6e 74 65 67 72 .........+integr +| 4064: 69 74 79 2d 63 68 65 63 6b 09 00 00 00 00 00 00 ity-check....... +| end crash-31c462b8b665d0.db +}]} {} + + +do_catchsql_test 69.2 { + SELECT * FROM t1 WHERE a MATCH 'fx*' +} {/.*fts5: corrupt.*/} + +#------------------------------------------------------------------------- +reset_db +do_test 70.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 24576 pagesize 4096 filename sql022250.txt.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 00 .....@ ........ +| 96: 00 00 00 00 0d 00 00 00 06 0d e2 00 0f c4 0f 6a ...............j +| 112: 0e fc 0e 9d 0e 3d 0d e2 00 00 00 00 00 00 00 00 .....=.......... +| 3552: 00 00 59 06 06 17 21 21 01 7f 74 61 62 6c 65 74 ..Y...!!..tablet +| 3568: 74 74 5f 63 6f 6e 66 69 67 74 74 74 5f 63 6f 6e tt_configttt_con +| 3584: 66 69 67 06 43 52 45 41 54 45 20 54 41 42 4c 45 fig.CREATE TABLE +| 3600: 20 27 74 74 74 5f 63 6f 6e 66 69 67 27 28 6b 20 'ttt_config'(k +| 3616: 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 20 PRIMARY KEY, v) +| 3632: 57 49 54 48 4f 55 54 20 52 4f 57 49 44 5e 15 07 WITHOUT ROWID^.. +| 3648: 17 23 23 01 81 03 74 61 62 6c 65 74 74 74 5f 64 .##...tablettt_d +| 3664: 6f 63 73 69 7a 65 74 74 74 5f 64 6f 63 73 69 7a ocsizettt_docsiz +| 3680: 65 05 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 e.CREATE TABLE ' +| 3696: 74 74 74 5f 64 6f 63 73 69 7a 65 27 28 69 64 20 ttt_docsize'(id +| 3712: 49 4d 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 IMTEGER PRIMARY +| 3728: 4b 45 59 2c 20 73 7a 20 42 4c 4f 20 29 5d 04 07 KEY, sz BLO )].. +| 3744: 17 23 23 01 81 01 74 61 62 6c 65 74 74 74 5f 63 .##...tablettt_c +| 3760: 6f 6e 74 65 6e 74 74 74 74 5f 63 6f 6e 74 65 6e ontentttt_conten +| 3776: 74 04 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 t.CREATE TABLE ' +| 3792: 74 74 74 5f 63 6f 6e 74 65 6e 74 27 28 69 64 20 ttt_content'(id +| 3808: 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 INTEGER PRIMARY +| 3824: 4b 45 59 2c 20 63 30 2c 20 63 31 29 6c 03 07 17 KEY, c0, c1)l... +| 3840: 1b 1b 01 81 2f 74 61 62 6c 65 74 74 74 5f 69 64 ..../tablettt_id +| 3856: 78 74 74 74 5f 69 64 78 03 43 52 45 41 54 45 20 xttt_idx.CREATE +| 3872: 54 41 42 4c 45 20 27 74 74 74 5f 69 64 78 27 28 TABLE 'ttt_idx'( +| 3888: 73 65 67 69 64 2c 20 74 65 72 6d 2c 20 70 67 6e segid, term, pgn +| 3904: 6f 2c 20 50 52 49 4d 41 52 59 20 4b 45 59 28 73 o, PRIMARY KEY(s +| 3920: 65 67 69 64 2c 20 74 65 72 6d 29 29 20 57 49 54 egid, term)) WIT +| 3936: 48 4f 55 54 20 52 4f 57 49 44 58 02 07 17 1d 1d HOUT ROWIDX..... +| 3952: 01 81 03 74 61 62 6c 65 74 74 74 5f 64 61 74 61 ...tablettt_data +| 3968: 74 74 74 5f 64 61 74 61 02 43 52 45 41 54 45 20 ttt_data.CREATE +| 3984: 54 41 42 4c 45 20 27 74 74 74 5f 64 61 74 61 27 TABLE 'ttt_data' +| 4000: 28 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d (id INTEGER PRIM +| 4016: 41 52 59 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 ARY KEY, block B +| 4032: 4c 4f 42 29 3a 01 06 17 13 13 08 5f 74 61 62 6c LOB):......_tabl +| 4048: 65 74 74 74 74 74 74 43 52 45 41 54 45 20 56 49 ettttttCREATE VI +| 4064: 52 54 55 41 4c 20 54 41 42 4c 45 20 74 74 74 20 RTUAL TABLE ttt +| 4080: 55 53 49 4e 47 20 66 74 73 35 28 61 2c 20 62 29 USING fts5(a, b) +| page 2 offset 4096 +| 0: 0d 0f 54 00 05 0e 81 00 0f e7 0e 81 0f af 0f 58 ..T............X +| 16: 0e 98 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 3712: 00 15 0a 03 00 30 00 00 00 00 01 03 03 00 03 01 .....0.......... +| 3728: 01 01 02 01 01 03 01 01 81 24 8c 80 80 80 80 01 .........$...... +| 3744: 04 00 82 4c 00 00 00 9b 02 30 65 03 1a 02 05 05 ...L.....0e..... +| 3760: 07 05 01 01 04 03 03 08 03 03 01 2e 02 05 05 07 ................ +| 3776: 05 07 05 07 05 01 01 14 03 03 08 03 03 08 03 bf ................ +| 3792: 07 f2 f3 02 01 65 03 1e 03 05 05 04 05 05 01 01 .....e.......... +| 3808: 03 06 04 04 06 04 03 01 36 03 05 05 04 05 05 04 ........6....... +| 3824: 05 15 04 05 05 01 01 03 06 04 04 06 04 04 a1 04 ................ +| 3840: 04 06 04 03 03 01 65 03 14 04 05 07 05 05 01 01 ......e......... +| 3856: 02 09 0a 01 20 04 05 07 05 07 05 07 05 05 01 01 .... ........... +| 3872: 02 0f da 0a 0a 04 01 64 f3 02 0a 01 06 0a 0a 0a .......d........ +| 3888: 05 01 65 03 06 01 01 0a 01 0a 01 01 0a 0a 0a 04 ..e............. +| 3904: 2b 31 21 0b 0f ef 00 14 2a 00 00 00 00 01 02 02 +1!.....*....... +| 3920: 00 02 01 01 01 02 01 01 50 88 80 80 80 80 01 04 ........P....... +| 3936: 00 81 24 00 00 00 47 01 30 65 02 1a 02 05 05 07 ..$...G.0e...... +| 3952: 05 01 01 04 03 03 08 03 03 02 01 65 02 1d f3 05 ...........e.... +| 3968: 05 04 05 05 01 01 04 06 04 04 06 04 03 03 01 65 ...............e +| 3984: 02 14 04 05 07 05 05 01 01 02 08 0a 04 01 65 02 ..............e. +| 4000: 02 0a 05 01 65 02 06 01 01 0a 04 12 14 0f 06 31 ....e..........1 +| 4016: 84 80 80 80 80 01 03 00 68 00 00 00 2b 02 30 65 ........h...+.0e +| 4032: 01 10 02 05 05 01 01 04 03 03 02 01 66 01 12 03 ............f... +| 4048: 05 05 01 01 03 06 04 03 03 01 65 01 0e 04 05 05 ..........e..... +| 4064: 01 01 02 08 04 0d 0e 06 01 03 00 12 04 4c 4c 00 .............LL. +| 4080: 00 00 11 24 00 00 00 00 01 01 01 00 01 01 01 01 ...$............ +| page 3 offset 8192 +| 0: 0a 2f 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ./.............. +| page 4 offset 12288 +| 3344: 00 00 00 00 00 00 00 00 00 00 81 52 04 06 10 81 ...........R.... +| 3360: 5d 81 55 65 20 65 65 20 65 65 65 20 65 20 65 65 ].Ue ee eee e ee +| 3376: 20 65 65 65 20 65 20 65 65 20 65 65 65 65 20 65 eee e ee eeee e +| 3392: 65 20 65 65 65 24 a5 20 65 65 20 65 65 65 20 65 e eee$. ee eee e +| 3408: 24 05 65 20 65 65 65 65 20 65 65 20 65 65 65 20 $.e eeee ee eee +| 3424: 65 20 65 65 20 65 65 65 20 65 20 65 65 20 65 65 e ee eee e ee ee +| 3440: 65 65 20 65 65 20 65 65 65 20 65 20 65 65 10 65 ee ee eee e ee.e +| 3456: 65 65 20 65 20 65 65 20 65 65 65 65 65 65 20 65 ee e ee eeeeee e +| 3472: 65 20 65 20 65 20 65 20 65 65 20 65 65 65 20 65 e e e e ee eee e +| 3488: 1f 20 65 65 66 65 65 20 65 65 20 65 20 65 20 2d . eefee ee e e - +| page 5 offset 16384 +| 4064: 00 00 00 00 05 04 d0 00 10 21 21 05 03 03 00 10 .........!!..... +| 4080: 11 11 05 02 03 00 10 11 10 05 01 03 00 10 09 09 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| end sql022250.txt.db +}]} {} + +do_catchsql_test 70.1 { + SELECT snippet(ttt, -1, '', '','','>')FROM ttt('e* NOT ee*e* NOT ee*'); +} {1 {database disk image is malformed}} + +do_catchsql_test 70.2 { + SELECT snippet(ttt, -1, '', '','',13)FROM ttt('e* NOT ee*e* NOT ee*') +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +reset_db +do_test 71.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 28672 pagesize 4096 filename sql025294.txt.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 00 01 00 40 20 20 00 00 00 00 00 00 00 07 .....@ ........ +| 96: 00 00 00 00 0d 00 00 00 07 0d d2 00 0f c4 0f 6d ...............m +| 112: 0f 02 0e ab 0e 4e 0d f6 0d d2 00 00 00 00 00 00 .....N.......... +| 3536: 00 00 22 07 06 17 11 11 01 31 74 61 62 6c 65 74 .........1tablet +| 3552: 32 74 32 07 43 52 45 41 54 45 20 54 41 42 4c 45 2t2.CREATE TABLE +| 3568: 20 74 32 28 78 29 56 06 06 17 1f 1f 01 7d 74 61 t2(x)V.......ta +| 3584: 62 6c 65 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 blet1_configt1_c +| 3600: 6f 6e 66 69 67 06 43 52 45 41 54 45 20 54 41 42 onfig.CREATE TAB +| 3616: 4c 45 20 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b LE 't1_config'(k +| 3632: 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 PRIMARY KEY, v) +| 3648: 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 5b 05 WITHOUT ROWID[. +| 3664: 07 17 21 21 01 81 01 74 61 62 6c 65 74 31 5f 64 ..!!...tablet1_d +| 3680: 6f 63 73 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 ocsizet1_docsize +| 3696: 05 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 .CREATE TABLE 't +| 3712: 31 5f 64 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 1_docsize'(id IN +| 3728: 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE +| 3744: 59 2c 20 73 7a 20 42 4c 4f 42 29 55 04 06 17 21 Y, sz BLOB)U...! +| 3760: 21 01 77 74 61 62 6c 65 74 31 5f 63 6f 6e 74 65 !.wtablet1_conte +| 3776: 6e 74 74 31 5f 63 6f 6e 74 65 6e 74 04 43 52 45 ntt1_content.CRE +| 3792: 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 6f ATE TABLE 't1_co +| 3808: 6e 74 65 6e 74 27 28 69 64 20 49 4e 54 45 47 45 ntent'(id INTEGE +| 3824: 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 63 R PRIMARY KEY, c +| 3840: 30 29 69 03 07 17 19 19 01 81 2d 74 61 62 6c 65 0)i.......-table +| 3856: 74 31 5f 69 64 78 74 31 5f 69 64 78 03 43 52 45 t1_idxt1_idx.CRE +| 3872: 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 69 64 ATE TABLE 't1_id +| 3888: 78 27 28 73 65 67 69 64 2c 20 74 65 72 6d 2c 20 x'(segid, term, +| 3904: 70 67 6e 6f 2c 20 50 52 49 4d 41 52 59 20 4b 45 pgno, PRIMARY KE +| 3920: 59 28 73 65 67 69 64 2c 20 74 65 72 6d 29 29 20 Y(segid, term)) +| 3936: 57 49 54 48 4f 55 54 20 52 4f 57 49 44 55 02 07 WITHOUT ROWIDU.. +| 3952: 17 1b 1b 01 81 01 74 61 62 6c 65 74 31 5f 64 61 ......tablet1_da +| 3968: 74 61 74 31 5f 64 61 74 61 02 43 52 45 41 54 45 tat1_data.CREATE +| 3984: 20 54 41 42 4c 45 20 27 74 31 5f 64 61 74 61 27 TABLE 't1_data' +| 4000: 28 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d (id INTEGER PRIM +| 4016: 41 52 58 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 ARX KEY, block B +| 4032: 4c 4f 42 29 3a 01 06 17 11 11 08 63 74 61 62 6c LOB):......ctabl +| 4048: 65 74 31 74 31 43 52 45 41 54 45 20 56 49 52 54 et1t1CREATE VIRT +| 4064: 55 41 4c 20 54 41 42 4c 45 20 74 31 20 55 53 49 UAL TABLE t1 USI +| 4080: 4e 47 20 66 74 73 35 28 63 6f 6e 74 65 6e 74 29 NG fts5(content) +| page 2 offset 4096 +| 0: 0d 00 00 00 03 0f bd 00 0f e8 0f ef 0f bd 00 00 ................ +| 4016: 00 00 00 00 00 00 00 00 00 00 00 00 00 24 84 80 .............$.. +| 4032: 80 80 80 01 03 00 4e 00 00 00 1e 06 30 61 62 61 ......N.....0aba +| 4048: 60 eb 01 02 02 04 01 66 74 02 02 02 04 04 6e 64 `......ft.....nd +| 4064: 6f 6e 03 01 f2 04 1a 07 05 01 03 00 10 03 03 0f on.............. +| 4080: 0a 03 00 24 00 00 00 00 01 01 4b 00 01 01 01 01 ...$......K..... +| page 3 offset 8192 +| 0: 0a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 05 04 09 0c 01 02 ................ +| page 4 offset 12288 +| 0: 0d 00 00 00 03 0f e0 00 0f f6 0f ec 0f e0 00 00 ................ +| 4064: 0a 03 03 00 1b 61 62 61 6e 64 6f 6e 08 02 03 00 .....abandon.... +| 4080: 17 61 62 61 66 74 08 01 03 00 17 61 62 61 63 6b .abaft.....aback +| page 5 offset 16384 +| 0: 0d 00 00 00 03 0f ee 00 0f fa 0f f4 0f ee 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 04 03 ................ +| 4080: 03 00 0e 01 04 02 03 00 0e 01 04 01 03 00 0e 01 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| page 7 offset 24576 +| 0: 0d 00 00 00 03 0f d6 00 0f f4 0f e1 00 00 00 00 ................ +| 4048: 00 00 00 00 00 00 09 03 02 1b 72 65 62 75 69 6c ..........rebuil +| 4064: 64 11 02 02 2b 69 6e 74 65 67 72 69 74 79 2d 63 d...+integrity-c +| 4080: 68 65 63 6b 0a 01 02 1d 6f 70 74 69 6d 69 7a 65 heck....optimize +| end sql025294.txt.db +}]} {} + +do_catchsql_test 71.2 { + INSERT INTO t1(t1) VALUES('integrity-check'); +} {/.*fts5: corrupt.*/} + +#------------------------------------------------------------------------- +reset_db +do_test 72.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 24576 pagesize 4096 filename crash-77b86d070d0ac6.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 06 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 06 00 00 00 04 ................ +| 96: 00 00 00 00 0d 00 00 00 06 0d e2 00 0f c4 0f 6a ...............j +| 112: 0e fc 0e 9d 0e 3d 0d e2 00 00 00 00 00 00 00 00 .....=.......... +| 3552: 00 00 59 06 06 17 21 21 01 7f 74 61 62 6c 65 74 ..Y...!!..tablet +| 3568: 74 74 5f 63 6f 6e 66 69 67 74 74 74 5f 63 6f 6e tt_configttt_con +| 3584: 66 69 67 06 43 52 45 41 54 45 20 54 41 42 4c 45 fig.CREATE TABLE +| 3600: 20 27 74 74 74 5f 63 6f 6e 66 69 67 27 28 6b 20 'ttt_config'(k +| 3616: 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 20 PRIMARY KEY, v) +| 3632: 57 49 54 48 4f 55 54 20 52 4f 57 49 44 5e 05 07 WITHOUT ROWID^.. +| 3648: 17 23 23 01 81 03 74 61 62 6c 65 74 74 74 5f 64 .##...tablettt_d +| 3664: 6f 63 73 69 7a 65 74 74 74 5f 64 6f 63 73 69 7a ocsizettt_docsiz +| 3680: 65 05 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 e.CREATE TABLE ' +| 3696: 74 74 74 5f 64 6f 63 73 69 7a 65 27 28 69 64 20 ttt_docsize'(id +| 3712: 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 INTEGER PRIMARY +| 3728: 4b 45 59 2c 20 73 7a 20 42 4c 4f 42 29 5d 04 07 KEY, sz BLOB)].. +| 3744: 17 23 23 01 81 01 74 61 62 6c 65 74 74 74 5f 63 .##...tablettt_c +| 3760: 6f 6e 74 65 6e 74 74 74 74 5f 63 6f 6e 74 65 6e ontentttt_conten +| 3776: 74 04 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 t.CREATE TABLE ' +| 3792: 74 74 74 5f 63 6f 6e 74 65 6e 74 27 28 69 64 20 ttt_content'(id +| 3808: 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 INTEGER PRIMARY +| 3824: 4b 45 59 2c 20 63 30 2c 20 63 31 29 6c 03 07 17 KEY, c0, c1)l... +| 3840: 1b 1b 01 81 2f 74 61 62 6c 65 74 74 74 5f 69 64 ..../tablettt_id +| 3856: 78 74 74 74 5f 69 64 78 03 43 52 45 41 54 45 20 xttt_idx.CREATE +| 3872: 54 41 42 4c 45 20 27 74 74 74 5f 69 64 78 27 28 TABLE 'ttt_idx'( +| 3888: 73 65 67 69 64 2c 20 74 65 72 6d 2c 20 70 67 6e segid, term, pgn +| 3904: 6f 2c 20 50 52 49 4d 41 52 59 20 4b 45 59 28 73 o, PRIMARY KEY(s +| 3920: 65 67 69 64 2c 20 74 65 72 6d 29 29 20 57 49 54 egid, term)) WIT +| 3936: 48 4f 55 54 20 52 4f 57 49 44 58 02 07 17 1d 1d HOUT ROWIDX..... +| 3952: 01 81 03 74 61 62 6c 65 74 74 74 5f 64 61 74 61 ...tablettt_data +| 3968: 74 74 74 5f 64 61 74 61 02 43 52 45 41 54 45 20 ttt_data.CREATE +| 3984: 54 41 42 4c 45 20 27 74 74 74 5f 64 61 74 61 27 TABLE 'ttt_data' +| 4000: 28 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d (id INTEGER PRIM +| 4016: 41 52 59 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 ARY KEY, block B +| 4032: 4c 4f 42 29 3a 01 06 17 13 13 08 5f 74 61 62 6c LOB):......_tabl +| 4048: 65 74 74 74 74 74 74 43 52 45 41 54 45 20 56 49 ettttttCREATE VI +| 4064: 52 54 55 41 4c 20 54 41 42 4c 45 20 74 74 74 20 RTUAL TABLE ttt +| 4080: 55 53 49 4e 47 20 66 74 73 35 28 61 2c 20 62 29 USING fts5(a, b) +| page 2 offset 4096 +| 0: 0d 0f 44 00 05 0e 81 00 0f e7 0e 81 0f af 0f 58 ..D............X +| 16: 0e 98 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 3712: 00 15 0a 03 00 30 00 00 00 00 01 03 03 00 03 01 .....0.......... +| 3728: 01 01 02 01 01 03 01 01 81 24 8c 80 80 80 80 01 .........$...... +| 3744: 04 00 82 4c 00 00 00 9b 02 30 65 03 1a 02 05 05 ...L.....0e..... +| 3760: 07 05 01 01 04 03 03 08 03 03 01 2e 02 05 05 07 ................ +| 3776: 05 07 05 07 05 01 01 04 03 03 08 03 03 08 03 03 ................ +| 3792: 08 03 03 02 01 65 03 1e 03 05 05 04 05 05 01 01 .....e.......... +| 3808: 03 06 04 04 06 04 03 01 36 03 05 05 04 05 05 04 ........6....... +| 3824: 05 05 04 05 05 01 01 03 06 04 04 06 04 04 06 04 ................ +| 3840: 04 06 04 03 03 01 65 03 14 04 05 07 05 05 01 01 ......e......... +| 3856: 02 08 0a 01 20 04 05 07 05 07 05 07 05 05 01 01 .... ........... +| 3872: 02 08 0a 09 fa 04 01 65 03 02 0a 01 06 0a 1a 0a .......e........ +| 3888: 05 01 65 03 06 01 01 0a 01 0a 01 01 0a 0a 0a 04 ..e............. +| 3904: 2b 31 21 0b 0f ef 00 14 2a 00 00 00 00 01 02 02 +1!.....*....... +| 3920: 00 02 01 01 01 02 01 01 50 88 80 80 80 80 01 04 ........P....... +| 3936: 00 81 24 00 00 00 47 02 30 65 02 1a 02 05 05 07 ..$...G.0e...... +| 3952: 05 01 01 04 03 03 08 03 03 02 01 65 02 1e 03 05 ...........e.... +| 3968: 05 04 05 05 01 01 03 06 04 04 06 04 03 03 01 65 ...............e +| 3984: 02 14 04 05 07 05 05 01 01 02 08 0a 04 01 65 02 ..............e. +| 4000: 02 0a 05 01 65 02 06 01 01 0a 04 12 14 0f 06 31 ....e..........1 +| 4016: 84 80 80 80 80 01 03 00 68 00 00 00 2b 02 30 65 ........h...+.0e +| 4032: 01 10 02 05 05 01 01 04 03 9f 02 01 65 01 12 03 ............e... +| 4048: 05 05 01 01 03 06 04 03 03 01 65 01 0e 14 05 05 ..........e..... +| 4064: 01 01 02 08 04 0d 0e 06 01 03 00 12 04 4c 4c 00 .............LL. +| 4080: 00 00 11 24 00 00 00 00 01 01 01 00 01 01 01 01 ...$............ +| page 3 offset 8192 +| 0: 0a 00 00 00 03 0f 00 00 00 00 00 00 00 00 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 00 06 04 01 0c ................ +| 4080: 01 03 02 06 04 01 0c 01 02 02 05 04 09 0c 01 02 ................ +| page 4 offset 12288 +| 0: 0d 00 00 00 04 00 00 00 00 00 00 00 00 00 00 00 ................ +| 3600: 00 00 00 00 00 00 00 00 00 00 81 52 04 06 00 81 ...........R.... +| 3616: 5d 81 55 65 20 65 65 20 65 65 65 20 65 20 65 65 ].Ue ee eee e ee +| 3632: 20 65 65 65 20 65 20 65 65 20 65 65 65 65 20 65 eee e ee eeee e +| 3648: 65 20 65 65 65 20 65 20 65 65 20 65 65 65 20 65 e eee e ee eee e +| 3664: 20 65 65 20 65 65 55 65 20 65 65 20 65 65 65 20 ee eeUe ee eee +| 3680: 65 20 65 65 20 65 65 65 20 65 20 65 65 20 65 65 e ee eee e ee ee +| 3696: 65 65 20 65 65 20 65 65 65 20 65 20 65 65 20 65 ee ee eee e ee e +| 3712: 65 65 20 65 20 65 65 20 65 65 65 65 65 65 20 65 ee e ee eeeeee e +| 3728: 65 20 65 20 65 20 65 20 65 65 20 65 85 65 20 65 e e e e ee e.e e +| 3744: 65 20 65 65 65 65 65 20 65 65 20 65 20 65 20 65 e eeeee ee e e e +| 3760: 20 65 65 20 65 65 65 20 65 65 20 65 65 65 65 65 ee eee ee eeeee +| 3776: 20 65 65 20 65 20 65 20 65 20 65 65 20 65 65 65 ee e e e ee eee +| 3792: 20 65 65 20 65 65 65 65 65 20 65 65 20 65 20 65 ee eeeee ee e e +| 3808: 20 65 20 65 65 20 65 65 65 20 65 65 20 65 65 6a e ee eee ee eej +| 3824: 03 04 00 75 71 65 20 65 65 20 65 65 65 20 65 20 ...uqe ee eee e +| 3840: 65 65 20 65 65 65 20 65 20 65 65 20 65 65 65 65 ee eee e ee eeee +| 3856: 20 65 65 20 65 65 65 20 65 20 65 65 20 65 65 65 ee eee e ee eee +| 3872: 20 65 20 65 65 20 65 65 65 65 65 65 20 65 65 20 e ee eeeeee ee +| 3888: 65 20 65 20 65 20 65 64 20 65 65 65 20 65 65 20 e e e ed eee ee +| 3904: 65 65 65 65 65 20 65 65 20 65 20 65 20 65 10 65 eeeee ee e e e.e +| 3920: 65 20 65 65 65 10 65 65 20 65 65 6a 02 04 00 75 e eee.ee eej...u +| 3936: 71 65 20 65 65 20 65 65 65 20 65 20 65 65 20 65 qe ee eee e ee e +| 3952: 65 65 20 65 20 65 65 20 65 65 65 65 20 65 65 20 ee e ee eeee ee +| 3968: 65 65 65 20 65 20 65 65 20 65 65 65 20 65 20 65 eee e ee eee e e +| 3984: 65 20 65 65 65 66 65 65 20 65 65 20 65 20 65 20 e eeefee ee e e +| 4000: 65 88 65 65 20 65 65 65 30 65 65 20 65 65 65 65 e.ee eee0ee eeee +| 4016: 65 20 65 65 20 65 20 65 20 65 20 65 65 20 65 65 e ee e e e ee ee +| 4032: 65 20 65 65 20 65 65 37 01 04 00 41 3f 65 20 65 e ee ee7...A?e e +| 4048: 65 20 65 65 65 20 65 20 65 65 20 65 65 65 20 65 e eee e ee eee e +| 4064: 20 65 65 20 65 65 65 65 65 65 20 65 65 20 65 20 ee eeeeee ee e +| 4080: 65 20 65 20 65 65 20 65 65 65 20 65 65 20 65 65 e e ee eee ee ee +| page 5 offset 16384 +| 0: 0d 00 00 00 04 00 00 00 00 00 00 00 00 00 00 00 ................ +| 4064: 00 00 00 00 05 04 03 00 10 21 21 05 03 03 00 10 .........!!..... +| 4080: 11 11 05 02 03 00 10 11 11 05 01 03 00 10 09 09 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| end crash-77b86d070d0ac6.db +}]} {} + +do_catchsql_test 72.1 { + INSERT INTO ttt(ttt) VALUES('integrity-check'); +} {1 {database disk image is malformed}} + +do_catchsql_test 72.2 { + SELECT 1 FROM ttt('e* NOT ee*'); +} {/.*fts5: corrupt.*/} + +#------------------------------------------------------------------------- +reset_db +do_test 73.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 24576 pagesize 4096 filename crash-b02ca2cc4d7dda.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 06 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 06 00 00 00 04 ................ +| 96: 00 00 00 00 0d 00 00 00 06 0d e2 00 0f c4 0f 6a ...............j +| 112: 0e fc 0e 9d 0e 3d 0d e2 00 00 00 00 00 00 00 00 .....=.......... +| 3552: 00 00 59 06 06 17 21 21 01 7f 74 61 62 6c 65 74 ..Y...!!..tablet +| 3568: 74 74 5f 63 6f 6e 66 69 67 74 74 74 5f 63 6f 6e tt_configttt_con +| 3584: 66 69 67 06 43 52 45 41 54 45 20 54 41 42 4c 45 fig.CREATE TABLE +| 3600: 20 27 74 74 74 5f 63 6f 6e 66 69 67 27 28 6b 20 'ttt_config'(k +| 3616: 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 20 PRIMARY KEY, v) +| 3632: 57 49 54 48 4f 55 54 20 52 4f 57 49 44 5e 05 07 WITHOUT ROWID^.. +| 3648: 17 23 23 01 81 03 74 61 62 6c 65 74 74 74 5f 64 .##...tablettt_d +| 3664: 6f 63 73 69 7a 65 74 74 74 5f 64 6f 63 73 69 7a ocsizettt_docsiz +| 3680: 65 05 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 e.CREATE TABLE ' +| 3696: 74 74 74 5f 64 6f 63 73 69 7a 65 27 28 69 64 20 ttt_docsize'(id +| 3712: 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 INTEGER PRIMARY +| 3728: 4b 45 59 2c 20 73 7a 20 42 4c 4f 42 29 5d 04 07 KEY, sz BLOB)].. +| 3744: 17 23 23 01 81 01 74 61 62 6c 65 74 74 74 5f 63 .##...tablettt_c +| 3760: 6f 6e 74 65 6e 74 74 74 74 5f 63 6f 6e 74 65 6e ontentttt_conten +| 3776: 74 04 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 t.CREATE TABLE ' +| 3792: 74 74 74 5f 63 6f 6e 74 65 6e 74 27 28 69 64 20 ttt_content'(id +| 3808: 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 INTEGER PRIMARY +| 3824: 4b 45 59 2c 20 63 30 2c 20 63 31 29 6c 03 07 17 KEY, c0, c1)l... +| 3840: 1b 1b 01 81 2f 74 61 62 6c 65 74 74 74 5f 69 64 ..../tablettt_id +| 3856: 78 74 74 74 5f 69 64 78 03 43 52 45 41 54 45 20 xttt_idx.CREATE +| 3872: 54 41 42 4c 45 20 27 74 74 74 5f 69 64 78 27 28 TABLE 'ttt_idx'( +| 3888: 73 65 67 69 64 2c 20 74 65 72 6d 2c 20 70 67 6e segid, term, pgn +| 3904: 6f 2c 20 50 52 49 4d 41 52 59 20 4b 45 59 28 73 o, PRIMARY KEY(s +| 3920: 65 67 69 64 2c 20 74 65 72 6d 29 29 20 57 49 54 egid, term)) WIT +| 3936: 48 4f 55 54 20 52 4f 57 49 44 58 02 07 17 1d 1d HOUT ROWIDX..... +| 3952: 01 81 03 74 61 62 6c 65 74 74 74 5f 64 61 74 61 ...tablettt_data +| 3968: 74 74 74 5f 64 61 74 61 02 43 52 45 41 54 45 20 ttt_data.CREATE +| 3984: 54 41 42 4c 45 20 27 74 74 74 5f 64 61 74 61 27 TABLE 'ttt_data' +| 4000: 28 69 65 20 49 4e 54 45 47 45 52 20 50 52 49 4d (ie INTEGER PRIM +| 4016: 41 52 59 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 ARY KEY, block B +| 4032: 4c 4f 42 29 3a 01 06 17 13 13 08 5f 74 61 62 6c LOB):......_tabl +| 4048: 65 74 74 74 74 74 74 43 52 45 41 54 45 20 56 49 ettttttCREATE VI +| 4064: 52 54 55 41 4c 20 54 41 42 4c 45 20 74 74 74 20 RTUAL TABLE ttt +| 4080: 55 53 49 4e 47 20 66 74 73 35 28 61 2c 20 62 29 USING fts5(a, b) +| page 2 offset 4096 +| 0: 0d 0f 44 00 05 0e 81 00 0f e7 0e 81 0f af 0f 58 ..D............X +| 16: 0e 98 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 3712: 00 15 0a 03 00 30 00 00 00 00 01 03 03 00 03 01 .....0.......... +| 3728: 01 01 02 01 01 03 01 01 81 24 8c 80 80 80 80 01 .........$...... +| 3744: 04 00 82 4c 00 00 00 9b 02 30 65 03 1a 02 05 05 ...L.....0e..... +| 3760: 07 05 01 01 04 03 03 08 03 03 01 2e 02 05 05 07 ................ +| 3776: 05 07 05 07 05 01 01 04 03 03 08 03 03 08 03 03 ................ +| 3792: 08 03 03 02 01 65 03 1e 03 05 05 04 05 05 01 01 .....e.......... +| 3808: 03 06 04 04 06 04 03 01 36 03 05 05 04 05 05 04 ........6....... +| 3824: 05 04 f4 04 05 01 01 03 06 04 04 06 04 04 06 04 ................ +| 3840: 04 06 04 db 03 01 65 03 14 04 05 07 05 05 01 01 ......e......... +| 3856: 02 08 0a 01 20 04 05 07 05 07 05 07 05 05 01 01 .... ........... +| 3872: 02 08 0a 0a 0a 04 01 65 03 02 0a 01 06 0a 0a 0a .......e........ +| 3888: 05 01 65 03 06 01 01 0a 01 0a 01 01 0a 0a 0a 04 ..e............. +| 3904: 2b 31 21 0b 0f ef 00 14 2a 00 00 00 00 01 02 02 +1!.....*....... +| 3920: 00 02 01 01 01 02 01 01 50 88 80 80 80 80 01 04 ........P....... +| 3936: 00 81 24 00 00 00 47 02 30 65 02 1a 02 05 05 07 ..$...G.0e...... +| 3952: 05 01 01 04 03 03 08 03 03 02 01 65 02 1e 03 05 ...........e.... +| 3968: 05 04 05 05 01 01 03 06 09 14 06 04 03 03 01 65 ...............e +| 3984: 02 14 04 05 07 05 05 01 01 02 08 ed 04 01 65 02 ..............e. +| 4000: 02 0a 05 01 65 02 06 01 01 0a 04 12 14 0f 06 31 ....e..........1 +| 4016: 84 80 80 80 80 01 03 00 68 00 00 00 2b 02 30 65 ........h...+.0e +| 4032: 01 10 02 05 05 01 01 04 03 03 02 01 55 01 12 03 ............U... +| 4048: 05 05 01 01 03 06 05 03 03 01 65 01 0e 04 05 05 ..........e..... +| 4064: 01 01 02 08 04 0d 0e 06 01 03 00 12 04 4c 4c 00 .............LL. +| 4080: 00 00 11 24 00 00 00 00 01 0f c1 00 01 01 01 01 ...$............ +| page 3 offset 8192 +| 0: 0a 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 00 06 04 01 0c ................ +| 4080: 01 03 02 06 04 01 0c 01 02 02 05 04 09 0c 01 02 ................ +| page 4 offset 12288 +| 0: 0d 00 00 00 04 00 00 00 00 00 00 00 00 00 00 00 ................ +| 3600: 00 00 00 00 00 00 00 00 00 00 81 52 04 06 00 81 ...........R.... +| 3616: 5d 81 55 65 20 65 65 20 65 65 65 20 65 20 65 65 ].Ue ee eee e ee +| 3632: 20 65 65 65 20 65 20 65 65 20 65 65 65 65 20 65 eee e ee eeee e +| 3648: 65 20 65 65 65 20 65 20 65 65 20 65 65 65 20 65 e eee e ee eee e +| 3664: 20 65 65 20 65 65 65 65 20 65 65 20 65 65 65 20 ee eeee ee eee +| 3680: 65 20 65 65 20 65 65 65 20 65 20 65 65 20 65 65 e ee eee e ee ee +| 3696: 65 65 20 65 65 20 65 65 65 20 65 20 65 65 20 65 ee ee eee e ee e +| 3712: 65 65 20 65 20 65 65 20 65 65 65 65 65 65 20 65 ee e ee eeeeee e +| 3728: 65 20 65 20 65 20 65 20 65 65 20 65 65 65 20 65 e e e e ee eee e +| 3744: 65 20 65 65 65 65 65 20 65 65 20 65 20 65 20 65 e eeeee ee e e e +| 3760: 20 65 65 20 65 62 d5 20 65 65 20 65 65 65 65 65 ee eb. ee eeeee +| 3776: 20 65 65 20 65 20 65 20 65 20 65 65 20 65 65 65 ee e e e ee eee +| 3792: 20 65 65 20 65 65 65 65 65 20 65 65 20 65 21 65 ee eeeee ee e!e +| 3808: 20 65 20 65 65 20 65 65 65 20 65 65 20 65 65 6a e ee eee ee eej +| 3824: 03 04 00 75 71 65 20 65 65 10 65 65 65 20 65 20 ...uqe ee.eee e +| 3840: 65 65 20 65 65 65 20 65 20 65 65 20 65 65 65 65 ee eee e ee eeee +| 3856: 20 65 65 20 65 65 65 20 65 20 65 65 20 65 65 65 ee eee e ee eee +| 3872: 20 65 20 65 65 20 65 65 65 65 65 65 20 65 65 20 e ee eeeeee ee +| 3888: 65 20 65 20 65 20 65 65 20 65 65 65 20 62 55 20 e e e ee eee bU +| 3904: 65 65 65 65 65 20 65 65 20 65 20 65 20 65 20 65 eeeee ee e e e e +| 3920: 65 20 65 65 65 20 55 65 20 65 65 6a 02 04 00 75 e eee Ue eej...u +| 3936: 71 65 20 65 65 20 65 65 65 20 65 10 65 65 20 65 qe ee eee e.ee e +| 3952: 65 65 20 65 20 65 65 20 65 65 65 65 20 65 65 20 ee e ee eeee ee +| 3968: 65 65 65 20 65 20 65 65 20 65 65 65 20 65 20 65 eee e ee eee e e +| 3984: 65 20 65 65 65 65 65 65 20 65 65 20 65 20 65 20 e eeeeee ee e e +| 4000: 65 20 65 65 20 65 65 65 20 65 65 20 65 65 65 65 e ee eee ee eeee +| 4016: 65 20 65 65 20 65 20 65 20 65 20 65 65 20 65 65 e ee e e e ee ee +| 4032: 65 20 65 65 21 65 65 37 0a 04 00 41 3f 65 20 65 e ee!ee7...A?e e +| 4048: 65 20 65 65 65 20 65 20 65 65 20 65 65 65 20 65 e eee e ee eee e +| 4064: 20 65 65 20 65 65 65 65 65 65 20 65 65 20 65 20 ee eeeeee ee e +| 4080: 65 20 65 20 65 65 20 65 65 65 20 65 65 20 65 65 e e ee eee ee ee +| page 5 offset 16384 +| 0: 0d 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 4064: 00 00 00 00 05 04 03 00 10 21 21 05 13 03 00 10 .........!!..... +| 4080: 11 11 05 02 03 00 10 11 11 05 01 03 00 10 09 09 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| end crash-b02ca2cc4d7dda.db +}]} {} + +do_catchsql_test 73.1 { + SELECT snippet(ttt,ttt, NOT 54 ), + * FROM ttt('e* NOT ee*e* NOT ee* NOT ee*e* NOT e*') ; +} {/.*fts5: corrupt.*/} + +#------------------------------------------------------------------------- +reset_db +do_test 74.0 { + sqlite3 db {} + sqlite3_fts5_register_matchinfo db + db deserialize [decode_hexdb { +| size 106496 pagesize 4096 filename x.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 01 00 00 00 1a .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 04 ................ +| 80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 ................ +| 96: 00 2e 4f 78 0d 0f c7 00 07 0d 92 00 0f 8d 0f 36 ..Ox...........6 +| 112: 0e cb 0e 6b 0e 0e 0d b6 0d 92 0d 92 00 00 00 00 ...k............ +| 3472: 00 00 22 08 06 17 11 11 01 31 74 61 62 6c 65 74 .........1tablet +| 3488: 32 74 32 08 43 52 45 41 54 45 20 54 41 42 4c 45 2t2.CREATE TABLE +| 3504: 20 74 32 28 78 29 56 07 06 17 1f 1f 01 7d 74 61 t2(x)V.......ta +| 3520: 62 6c 65 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 blet1_configt1_c +| 3536: 6f 6e 66 69 67 07 43 52 45 41 54 45 20 54 41 42 onfig.CREATE TAB +| 3552: 4c 45 20 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b LE 't1_config'(k +| 3568: 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 PRIMARY KEY, v) +| 3584: 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 5b 06 WITHOUT ROWID[. +| 3600: 07 17 21 21 01 81 01 74 61 62 6c 65 74 31 5f 64 ..!!...tablet1_d +| 3616: 6f 63 73 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 ocsizet1_docsize +| 3632: 06 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 .CREATE TABLE 't +| 3648: 31 5f 64 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 1_docsize'(id IN +| 3664: 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE +| 3680: 59 2c 20 73 7a 20 42 4c 4f 42 29 5e 05 07 17 21 Y, sz BLOB)^...! +| 3696: 21 01 81 07 74 61 62 6c 65 74 31 5f 63 6f 6e 74 !...tablet1_cont +| 3712: 65 6e 74 74 31 5f 63 6f 6e 74 65 6e 74 05 43 52 entt1_content.CR +| 3728: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 EATE TABLE 't1_c +| 3744: 6f 6e 74 65 6e 74 27 28 69 64 20 49 4e 54 45 47 ontent'(id INTEG +| 3760: 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 ER PRIMARY KEY, +| 3776: 63 30 2c 20 63 31 2c d6 63 32 29 69 04 07 17 19 c0, c1,.c2)i.... +| 3792: 19 01 81 2d 74 61 62 6c 65 74 31 5f 69 64 78 74 ...-tablet1_idxt +| 3808: 31 5f 69 64 78 04 43 52 45 41 54 45 20 54 41 42 1_idx.CREATE TAB +| 3824: 4c 45 20 27 74 31 5f 69 64 78 27 28 73 65 67 69 LE 't1_idx'(segi +| 3840: 64 2c 20 74 65 72 6d 2c 20 70 67 6e 6f 2c 20 50 d, term, pgno, P +| 3856: 52 49 4d 41 52 59 20 4b 45 59 28 73 65 67 69 64 RIMARY KEY(segid +| 3872: 2c 20 74 65 72 6d 29 29 20 57 49 54 48 4f 55 54 , term)) WITHOUT +| 3888: 20 52 4f 57 49 44 55 03 07 17 1b 1b 01 81 01 74 ROWIDU........t +| 3904: 61 62 6c 65 74 31 5f 64 61 74 61 74 31 5f 64 61 ablet1_datat1_da +| 3920: 74 61 03 43 52 45 41 54 45 20 54 41 42 4c 45 20 ta.CREATE TABLE +| 3936: 27 74 31 5f 64 61 74 61 27 28 69 64 20 49 4e 54 't1_data'(id INT +| 3952: 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 EGER PRIMARY KEY +| 3968: 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 29 38 02 06 , block BLOB)8.. +| 3984: 17 11 11 08 5f 74 61 62 6c 65 74 31 74 31 43 52 ...._tablet1t1CR +| 4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 35 LE t1 USING fts5 +| 4032: 28 61 2c 62 2c 63 29 00 00 00 00 00 00 00 00 00 (a,b,c)......... +| page 2 offset 4096 +| 0: 0d 00 00 00 24 0c 0a 00 0f d8 0f af 0f 86 0f 74 ....$..........t +| 16: 0f 61 0f 4e 0f 2f 0f 0f 0e ef 0e d7 0e be 0e a5 .a.N./.......... +| 32: 0e 8d 0e 74 0e 5b 0e 40 0e 24 0e 08 01 2f 0d d5 ...t.[.@.$.../.. +| 48: 0d bb 0d a0 0d 84 0d 68 0d 4f 0d 35 0d 1b 0c fb .......h.O.5.... +| 64: 0c da 0c b9 0c 99 0c 78 0c 57 0c 00 00 00 00 00 .......x.W...... +| 3072: 00 00 00 00 00 00 00 00 00 00 18 24 05 00 25 0f ...........$..%. +| 3088: 19 54 48 52 45 41 44 53 41 46 45 3d 30 58 42 49 .THREADSAFE=0XBI +| 3104: 4e 41 52 59 18 23 05 00 25 0f 19 54 48 52 45 41 NARY.#..%..THREA +| 3120: 44 53 41 46 45 3d 30 58 4e 4f 43 41 53 45 17 8f DSAFE=0XNOCASE.. +| 3136: 05 00 25 0f 17 54 48 52 45 41 44 53 41 46 45 3d ..%..THREADSAFE= +| 3152: 30 58 52 54 52 49 4d 1f 21 05 00 33 0f 19 4f 4d 0XRTRIM.!..3..OM +| 3168: 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 4f IT LOAD EXTENSIO +| 3184: 4e 58 42 49 4e 41 52 59 1f 20 05 00 33 0f 19 4f NXBINARY. ..3..O +| 3200: 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 MIT LOAD EXTENSI +| 3216: 4f 4e 58 4e 4f 43 41 53 45 1e 1f 05 00 33 0f 17 ONXNOCASE....3.. +| 3232: 4f 4d 59 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 OMYT LOAD EXTENS +| 3248: 49 4f 4e 58 52 54 52 49 4d 1f 1e 05 00 33 0f 19 IONXRTRIM....3.. +| 3264: 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 30 MAX MEMORY=50000 +| 3280: 30 30 30 57 42 49 4e 41 52 59 1f 1d 05 00 33 0f 000WBINARY....3. +| 3296: 19 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 .MAX MEMORY=5000 +| 3312: 30 30 30 30 58 4e 4f 43 41 53 45 1e 1c 05 00 33 0000XNOCASE....3 +| 3328: 0f 17 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 ..MAX MEMORY=500 +| 3344: 30 30 30 30 30 58 52 54 52 49 4d 18 1b 05 00 25 00000XRTRIM....% +| 3360: 0f 19 45 4e 41 42 4c 45 20 52 54 52 45 45 58 42 ..ENABLE RTREEXB +| 3376: 49 4e 41 52 59 18 1a 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3392: 4c 45 20 52 54 52 45 45 59 4e 4f 43 41 53 45 17 LE RTREEYNOCASE. +| 3408: 19 05 00 25 0f 17 45 4e 41 42 4c 45 20 52 54 52 ...%..ENABLE RTR +| 3424: 45 45 58 52 54 52 49 4d 1a 18 05 00 29 0f 19 45 EEXRTRIM....)..E +| 3440: 4e 41 42 4c 45 20 4d 45 4d 53 59 53 35 58 42 49 NABLE MEMSYS5XBI +| 3456: 4e 41 52 59 1a 17 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3472: 45 20 4d 45 4d 53 59 53 35 58 4e 4f 43 41 53 45 E MEMSYS5XNOCASE +| 3488: 19 16 05 00 29 0f 17 45 4e 41 42 4c 45 20 4d 45 ....)..ENABLE ME +| 3504: 4d 53 59 53 35 58 52 54 52 49 4d 18 15 05 00 25 MSYS5XRTRIM....% +| 3520: 0f 19 45 4e 41 42 4c 45 20 4a 53 4f 4e 31 58 42 ..ENABLE JSON1XB +| 3536: 49 4e 41 52 59 18 14 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3552: 4c 45 20 4a 53 4f 4e 31 58 4e 4f 43 41 53 45 17 LE JSON1XNOCASE. +| 3568: 13 05 00 25 0f 17 45 4e 41 42 4c 45 20 4a 53 4f ...%..ENABLE JSO +| 3584: 4e 31 58 52 54 52 49 4d 1a 12 05 00 29 0f 19 45 N1XRTRIM....)..E +| 3600: 4e 41 42 4c 45 20 47 45 4f 50 4f 4c 59 58 42 49 NABLE GEOPOLYXBI +| 3616: 4e 41 52 59 1a 11 05 00 29 0f 19 45 4f 41 42 4c NARY....)..EOABL +| 3632: 45 20 47 45 4f 50 4f 4c 59 58 4e 4f 43 51 53 45 E GEOPOLYXNOCQSE +| 3648: 19 10 05 00 29 0f 17 45 4e 41 42 4c 45 20 47 45 ....)..ENABLE GE +| 3664: 4f 50 4f 4c 59 58 52 54 52 49 4d 17 0f 05 00 23 OPOLYXRTRIM....# +| 3680: 0f 19 45 4e 41 42 4c 45 20 46 54 53 35 58 42 49 ..ENABLE FTS5XBI +| 3696: 4e 41 52 59 17 0e 05 00 23 0f 19 45 4e 41 42 4c NARY....#..ENABL +| 3712: 45 20 46 54 53 35 58 4e 4f 43 41 53 45 16 1d 05 E FTS5XNOCASE... +| 3728: 00 23 0f a4 45 4e 41 42 4c 45 20 46 54 53 35 58 .#..ENABLE FTS5X +| 3744: 52 54 52 49 4d 17 0c 05 00 23 0f 19 45 4e 41 42 RTRIM....#..ENAB +| 3760: 4c 45 20 46 55 53 34 58 42 49 4e 41 52 59 17 0b LE FUS4XBINARY.. +| 3776: 05 00 23 0f 19 45 4e 41 42 4c 45 20 46 54 53 34 ..#..ENABLE FTS4 +| 3792: 58 4e 4f 43 41 53 45 16 0a 05 00 23 0f 17 45 4e XNOCASE....#..EN +| 3808: 41 42 4c 45 20 46 54 53 34 58 52 54 52 49 4d 1e ABLE FTS4XRTRIM. +| 3824: 09 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3840: 54 41 54 20 56 54 41 42 58 42 49 4e 41 52 59 1e TAT VTABXBINARY. +| 3856: 08 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3872: 54 41 54 20 56 54 41 42 58 4e 4f 43 41 53 45 1d TAT VTABXNOCASE. +| 3888: 07 05 00 31 0f 17 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3904: 54 41 54 20 56 54 41 42 58 52 54 52 49 4d 11 06 TAT VTABXRTRIM.. +| 3920: 05 00 17 0f 19 44 45 42 55 47 58 42 49 4e 41 52 .....DEBUGXBINAR +| 3936: 59 11 05 05 00 17 0f 19 44 45 42 55 47 58 4e 4f Y.......DEBUGXNO +| 3952: 43 41 53 45 10 04 05 00 17 0f 17 44 45 42 55 47 CASE.......DEBUG +| 3968: 58 52 54 52 49 4d 27 03 05 00 43 0f 19 43 4f 4d XRTRIM'...C..COM +| 3984: 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e 30 20 PILER=gcc-5.4.0 +| 4000: 32 30 31 36 30 36 30 39 58 42 49 4e 41 52 59 27 20160609XBINARY' +| 4016: 02 05 00 43 0f 19 43 4f 4d 50 49 4c 45 52 3d 67 ...C..COMPILER=g +| 4032: 63 63 2d 35 2e 34 2e 30 20 32 30 31 36 30 36 30 cc-5.4.0 2016060 +| 4048: 39 58 4e 4f 43 41 53 45 26 01 05 00 43 0f 17 43 9XNOCASE&...C..C +| 4064: 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e OMPILER=gcc-5.4. +| 4080: 30 20 32 30 31 36 30 36 30 39 58 52 54 53 49 4d 0 20160609XRTSIM +| page 3 offset 8192 +| 0: 05 00 00 00 07 0f ba 00 00 00 00 1a 0f f6 0f ec ................ +| 16: 0f e2 0f d8 0f ce 0f c4 0f ba 00 00 00 00 00 00 ................ +| 3200: 00 00 00 00 00 00 00 00 00 00 08 01 03 00 16 2e ................ +| 3216: b1 7d 24 24 86 4a 84 80 80 80 80 01 04 00 8d 18 ..$$.J.......... +| 3232: 00 00 03 2b 02 30 30 01 02 06 01 02 06 01 02 06 ...+.00......... +| 3248: 1f 02 03 01 02 03 01 02 03 01 08 32 30 31 36 30 ...........20160 +| 3264: 36 30 39 01 02 07 01 02 07 01 02 07 01 01 34 00 609...........4. +| 3280: 02 05 01 02 05 01 02 05 01 01 35 01 02 04 01 02 ..........5..... +| 3296: 04 01 02 04 02 07 30 30 30 30 30 30 30 1c 02 3d ......0000000..= +| 3312: 01 02 04 01 02 04 01 06 62 69 6e 61 72 79 03 06 ........binary.. +| 3328: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3344: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3360: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 ................ +| 3376: 03 06 01 02 02 03 06 01 02 02 01 08 63 6f 6d 70 ............comp +| 3392: 69 6c 65 72 01 02 02 01 02 02 01 02 02 01 06 64 iler...........d +| 3408: 62 73 74 61 74 07 02 03 01 02 03 01 02 03 02 04 bstat........... +| 3424: 65 62 75 67 04 02 02 01 02 02 01 02 02 01 07 65 ebug...........e +| 3440: 6e 61 62 6c 65 07 02 02 01 02 02 01 d3 02 01 02 nable........... +| 3456: 02 01 02 02 02 02 02 01 02 02 01 02 02 01 02 02 ................ +| 3472: 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 ................ +| 3488: 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 ................ +| 3504: 02 01 02 02 02 08 78 74 65 6e 73 69 6f 6e 1f 02 ......xtension.. +| 3520: 04 01 02 04 01 02 04 01 04 66 74 73 34 0a 02 03 .........fts4... +| 3536: 01 02 03 01 02 03 04 01 35 0d 02 03 01 02 03 01 ........5....... +| 3552: 02 03 01 03 67 63 63 01 02 54 01 02 03 01 02 03 ....gcc..T...... +| 3568: 02 06 65 6f 70 6f 6c 79 10 02 03 01 02 03 01 02 ..eopoly........ +| 3584: 03 01 05 6a 73 6f 6e 31 13 02 03 01 02 03 01 02 ...json1........ +| 3600: 03 01 04 6c 6f 61 64 1f 02 03 01 02 03 01 02 03 ...load......... +| 3616: 01 03 6d 61 78 1c 02 02 01 02 02 01 02 02 02 05 ..max........... +| 3632: 65 6d 6f 72 79 1c 02 03 01 02 03 01 02 03 04 04 emory........... +| 3648: 73 79 73 35 16 02 03 01 02 03 01 02 03 01 06 6e sys5...........n +| 3664: 6f 63 61 73 65 02 06 01 02 02 03 06 01 02 02 03 ocase........... +| 3680: 06 01 02 02 13 06 01 02 02 03 06 01 02 02 03 06 ................ +| 3696: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3712: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3728: 02 01 04 6f 6d 69 74 1f 02 02 01 02 02 01 02 02 ...omit......... +| 3744: 01 05 72 74 72 65 65 19 02 03 01 02 03 01 02 03 ..rtree......... +| 3760: 04 02 69 6d 01 06 01 02 02 03 06 01 02 02 03 06 ..im............ +| 3776: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3792: 02 01 f3 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3808: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 08 ................ +| 3824: 01 0a 74 68 72 65 61 64 73 61 66 65 22 02 02 01 ..threadsafe.... +| 3840: 02 02 01 02 02 01 04 76 74 61 62 07 02 04 01 02 .......vtab..... +| 3856: 04 01 02 04 01 01 78 01 06 01 01 02 01 06 01 01 ......x......... +| 3872: 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 ................ +| 3888: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 3904: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 3920: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 ................ +| 3936: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 ................ +| 3952: 02 01 06 01 01 02 01 06 01 01 02 ad 06 01 01 02 ................ +| 3968: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 3984: 06 01 01 01 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 4000: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 ................ +| 4016: 01 02 01 06 01 01 02 01 06 01 00 00 00 19 88 80 ................ +| 4032: 80 80 80 06 00 00 00 18 88 80 80 80 80 05 00 00 ................ +| 4048: 00 17 88 80 80 80 80 04 00 00 00 16 88 80 80 80 ................ +| 4064: 80 03 00 00 00 15 88 80 80 80 80 02 00 00 00 14 ................ +| 4080: 88 80 80 80 80 01 00 00 00 13 84 80 80 80 80 01 ................ +| page 4 offset 12288 +| 0: 0a 00 00 00 08 0e bd 00 00 00 0e f9 0e ef 0e e5 ................ +| 16: 0e db 0e d1 0e c7 0e bd 00 00 00 00 00 00 00 00 ................ +| 3760: 00 00 00 00 00 00 00 00 00 00 00 00 00 09 04 01 ................ +| 3776: 12 01 02 30 f4 a3 0e 09 04 01 12 01 02 30 cf 8c ...0.........0.. +| 3792: 0c 09 04 01 12 01 02 30 7a 34 0a 09 04 01 12 01 .......0z4...... +| 3808: 02 30 72 64 08 09 04 01 12 01 02 30 6b 30 06 09 .0rd.......0k0.. +| 3824: 04 01 12 01 02 30 63 33 04 06 04 01 0c 01 02 02 .....0c3........ +| 4080: 00 00 00 00 00 00 00 00 00 00 05 04 09 0c 01 02 ................ +| page 5 offset 16384 +| 0: 05 00 00 00 0a 0f ce 00 00 00 00 12 0f fb 0f f6 ................ +| 16: 0f f1 0f ec 0f e7 0f e2 0f dd 0f d8 0f d3 0f ce ................ +| 32: 0e 8d 0e 74 0e 5b 0e 40 0e 24 0e 08 01 2f 0d d5 ...t.[.@.$.../.. +| 48: 0d bb 0d a0 0d 84 0d 68 0d 4f 0d 35 0d 1b 0c fb .......h.O.5.... +| 64: 0c da 0c b9 0c 99 0c 78 0c 57 0c 00 00 00 00 00 .......x.W...... +| 3072: 00 00 00 00 00 00 00 00 00 00 18 24 05 00 25 0f ...........$..%. +| 3088: 19 54 48 52 45 41 44 53 41 46 45 3d 30 58 42 49 .THREADSAFE=0XBI +| 3104: 4e 41 52 59 18 23 05 00 25 0f 19 54 48 52 45 41 NARY.#..%..THREA +| 3120: 44 53 41 46 45 3d 30 58 4e 4f 43 41 53 45 17 8f DSAFE=0XNOCASE.. +| 3136: 05 00 25 0f 17 54 48 52 45 41 44 53 41 46 45 3d ..%..THREADSAFE= +| 3152: 30 58 52 54 52 49 4d 1f 21 05 00 33 0f 19 4f 4d 0XRTRIM.!..3..OM +| 3168: 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 4f IT LOAD EXTENSIO +| 3184: 4e 58 42 49 4e 41 52 59 1f 20 05 00 33 0f 19 4f NXBINARY. ..3..O +| 3200: 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 MIT LOAD EXTENSI +| 3216: 4f 4e 58 4e 4f 43 41 53 45 1e 1f 05 00 33 0f 17 ONXNOCASE....3.. +| 3232: 4f 4d 59 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 OMYT LOAD EXTENS +| 3248: 49 4f 4e 58 52 54 52 49 4d 1f 1e 05 00 33 0f 19 IONXRTRIM....3.. +| 3264: 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 30 MAX MEMORY=50000 +| 3280: 30 30 30 57 42 49 4e 41 52 59 1f 1d 05 00 33 0f 000WBINARY....3. +| 3296: 19 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 .MAX MEMORY=5000 +| 3312: 30 30 30 30 58 4e 4f 43 41 53 45 1e 1c 05 00 33 0000XNOCASE....3 +| 3328: 0f 17 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 ..MAX MEMORY=500 +| 3344: 30 30 30 30 30 58 52 54 52 49 4d 18 1b 05 00 25 00000XRTRIM....% +| 3360: 0f 19 45 4e 41 42 4c 45 20 52 54 52 45 45 58 42 ..ENABLE RTREEXB +| 3376: 49 4e 41 52 59 18 1a 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3392: 4c 45 20 52 54 52 45 45 59 4e 4f 43 41 53 45 17 LE RTREEYNOCASE. +| 3408: 19 05 00 25 0f 17 45 4e 41 42 4c 45 20 52 54 52 ...%..ENABLE RTR +| 3424: 45 45 58 52 54 52 49 4d 1a 18 05 00 29 0f 19 45 EEXRTRIM....)..E +| 3440: 4e 41 42 4c 45 20 4d 45 4d 53 59 53 35 58 42 49 NABLE MEMSYS5XBI +| 3456: 4e 41 52 59 1a 17 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3472: 45 20 4d 45 4d 53 59 53 35 58 4e 4f 43 41 53 45 E MEMSYS5XNOCASE +| 3488: 19 16 05 00 29 0f 17 45 4e 41 42 4c 45 20 4d 45 ....)..ENABLE ME +| 3504: 4d 53 59 53 35 58 52 54 52 49 4d 18 15 05 00 25 MSYS5XRTRIM....% +| 3520: 0f 19 45 4e 41 42 4c 45 20 4a 53 4f 4e 31 58 42 ..ENABLE JSON1XB +| 3536: 49 4e 41 52 59 18 14 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3552: 4c 45 20 4a 53 4f 4e 31 58 4e 4f 43 41 53 45 17 LE JSON1XNOCASE. +| 3568: 13 05 00 25 0f 17 45 4e 41 42 4c 45 20 4a 53 4f ...%..ENABLE JSO +| 3584: 4e 31 58 52 54 52 49 4d 1a 12 05 00 29 0f 19 45 N1XRTRIM....)..E +| 3600: 4e 41 42 4c 45 20 47 45 4f 50 4f 4c 59 58 42 49 NABLE GEOPOLYXBI +| 3616: 4e 41 52 59 1a 11 05 00 29 0f 19 45 4f 41 42 4c NARY....)..EOABL +| 3632: 45 20 47 45 4f 50 4f 4c 59 58 4e 4f 43 51 53 45 E GEOPOLYXNOCQSE +| 3648: 19 10 05 00 29 0f 17 45 4e 41 42 4c 45 20 47 45 ....)..ENABLE GE +| 3664: 4f 50 4f 4c 59 58 52 54 52 49 4d 17 0f 05 00 23 OPOLYXRTRIM....# +| 3680: 0f 19 45 4e 41 42 4c 45 20 46 54 53 35 58 42 49 ..ENABLE FTS5XBI +| 3696: 4e 41 52 59 17 0e 05 00 23 0f 19 45 4e 41 42 4c NARY....#..ENABL +| 3712: 45 20 46 54 53 35 58 4e 4f 43 41 53 45 16 1d 05 E FTS5XNOCASE... +| 3728: 00 23 0f a4 45 4e 41 42 4c 45 20 46 54 53 35 58 .#..ENABLE FTS5X +| 3744: 52 54 52 49 4d 17 0c 05 00 23 0f 19 45 4e 41 42 RTRIM....#..ENAB +| 3760: 4c 45 20 46 55 53 34 58 42 49 4e 41 52 59 17 0b LE FUS4XBINARY.. +| 3776: 05 00 23 0f 19 45 4e 41 42 4c 45 20 46 54 53 34 ..#..ENABLE FTS4 +| 3792: 58 4e 4f 43 41 53 45 16 0a 05 00 23 0f 17 45 4e XNOCASE....#..EN +| 3808: 41 42 4c 45 20 46 54 53 34 58 52 54 52 49 4d 1e ABLE FTS4XRTRIM. +| 3824: 09 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3840: 54 41 54 20 56 54 41 42 58 42 49 4e 41 52 59 1e TAT VTABXBINARY. +| 3856: 08 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3872: 54 41 54 20 56 54 41 42 58 4e 4f 43 41 53 45 1d TAT VTABXNOCASE. +| 3888: 07 05 00 31 0f 17 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3904: 54 41 54 20 56 54 41 42 58 52 54 52 49 4d 11 06 TAT VTABXRTRIM.. +| 3920: 05 00 17 0f 19 44 45 42 55 47 58 42 49 4e 41 52 .....DEBUGXBINAR +| 3936: 59 11 05 05 00 17 0f 19 44 45 42 55 47 58 4e 4f Y.......DEBUGXNO +| 3952: 43 41 53 45 10 04 05 00 17 0f 17 44 45 42 55 47 CASE.......DEBUG +| 3968: 58 52 54 52 49 4d 27 03 05 00 43 0f 19 43 4f 4d XRTRIM'...C..COM +| 3984: 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e 30 20 PILER=gcc-5.4.0 +| 4000: 32 30 31 36 30 36 30 39 58 42 49 4e 41 52 59 27 20160609XBINARY' +| 4016: 02 05 00 43 0f 19 43 4f 4d 50 49 4c 45 52 3d 67 ...C..COMPILER=g +| 4032: 63 63 2d 35 2e 34 2e 30 20 32 30 31 36 30 00 00 cc-5.4.0 20160.. +| 4048: 00 11 09 00 00 00 10 08 00 00 00 0f 07 00 00 00 ................ +| 4064: 0e 06 00 00 00 0d 05 00 00 00 0c 04 00 00 00 0b ................ +| 4080: 03 00 00 00 0a 02 00 00 00 09 01 00 00 00 02 00 ................ +| page 6 offset 20480 +| 0: 0d 0f b0 00 25 0e bc 03 0e d7 0e ce 0f f0 0e c5 ....%........... +| 16: 0f e7 0f de 0f d5 0f cc 0f c3 0e bc 0f b7 0f a8 ................ +| 32: 0f a0 0f 98 0f 90 0f 88 0f 80 0f 78 0f 70 0f 68 ...........x.p.h +| 48: 0f 60 0f 58 0f 50 0f 48 0f 40 0f 38 0f 30 0f 28 .`.X.P.H.@.8.0.( +| 64: 0f 20 0f 18 0f 10 0f 08 0f 00 0e f8 0e 00 00 00 . .............. +| 3760: 00 00 00 00 00 00 00 00 00 00 00 00 07 09 03 00 ................ +| 3776: 14 84 6b 00 00 07 03 03 00 14 84 5b 00 00 07 02 ..k........[.... +| 3792: 03 00 14 84 63 00 00 07 01 03 00 14 85 07 00 00 ....c........... +| 3808: 06 24 03 00 12 02 01 01 06 23 03 00 12 02 01 01 .$.......#...... +| 3824: 06 22 03 00 12 02 01 01 06 21 03 00 12 03 01 01 .........!...... +| 3840: 06 20 03 00 12 03 01 01 06 1f 03 00 12 03 01 01 . .............. +| 3856: 06 1e 03 00 12 03 01 01 06 1d 03 00 12 03 01 01 ................ +| 3872: 06 1c 03 00 12 03 01 01 06 1b 03 00 12 02 01 01 ................ +| 3888: 06 1a 03 00 12 02 01 01 06 19 03 00 12 02 01 01 ................ +| 3904: 06 18 03 00 12 02 01 01 06 17 03 00 12 02 01 01 ................ +| 3920: 06 16 03 00 12 02 01 01 06 15 03 00 12 02 01 01 ................ +| 3936: 06 14 03 00 12 02 01 01 06 13 03 00 12 02 01 01 ................ +| 3952: 06 12 03 00 12 02 01 01 06 11 03 00 12 02 01 01 ................ +| 3968: 06 00 03 00 12 02 01 01 06 0f 03 00 12 02 01 01 ................ +| 3984: 06 0e 03 00 12 02 01 01 06 0d 03 00 12 02 01 01 ................ +| 4000: 06 0c 03 00 12 02 01 01 06 0b 03 00 12 02 01 01 ................ +| 4016: 0f f8 00 07 12 02 01 07 0a 03 00 14 85 05 00 00 ................ +| 4032: 0f f8 00 07 08 03 00 14 84 73 00 00 07 07 03 00 .........s...... +| 4048: 14 85 0b 00 00 07 06 03 00 14 85 02 00 00 07 05 ................ +| 4064: 03 00 14 84 70 00 00 07 04 03 00 14 84 7e 00 00 ....p........~.. +| 4080: 06 de 03 00 12 06 01 01 00 00 00 08 12 06 01 01 ................ +| page 7 offset 24576 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| page 8 offset 28672 +| 0: 0d 00 00 00 03 0f d6 00 0f f4 0f e9 0f d6 00 01 ................ +| 4048: 00 00 00 00 00 00 11 03 02 2b 69 6e 74 65 67 72 .........+integr +| 4064: 69 74 79 2d 63 68 65 63 6b 09 02 02 1b 72 65 62 ity-check....reb +| 4080: 75 69 6c 64 0a 01 02 1d 6f 70 74 69 00 00 00 00 uild....opti.... +| page 9 offset 32768 +| 0: 0d 00 00 00 01 04 3f 00 04 3f 00 00 00 00 00 00 ......?..?...... +| 1072: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 97 ................ +| 1088: 3e 01 06 00 ae 7c 00 00 ee a4 ad af cf 26 bc c7 >....|.......&.. +| 1104: a8 a1 59 d7 43 c2 93 4f ad 82 19 2e 25 e1 97 f9 ..Y.C..O....%... +| 1120: 21 59 34 ce e8 b2 e2 3c 4b f2 b4 ee 6b 35 84 c3 !Y4.....@..r... +| 1792: 8d 30 50 e6 54 9d 62 0d 11 b0 80 ad 64 44 7f 82 .0P.T.b.....dD.. +| 1808: 9a 61 c7 d5 f1 6b 34 2b ec bf 1a ee 5f bf 10 fe .a...k4+...._... +| 1824: 0f 9e 42 3d 34 1e ff 00 82 db 52 e0 65 d1 d7 0d ..B=4.....R.e... +| 1840: 77 fb 84 5c ec c3 b7 72 cf 21 26 40 85 20 f4 78 w......r.!&@. .x +| 1856: 6a f2 f4 52 8a c8 cd d9 99 73 8f 65 f5 f5 a8 af j..R.....s.e.... +| 1872: 75 bd a0 c6 ff 1a bf 64 53 01 4e ed be 22 95 1a u......dS.N..... +| 1888: 57 06 21 1d f3 f3 97 4f c7 de 83 ab 80 40 c4 02 W.!....O.....@.. +| 1904: 12 65 0d d5 55 87 6f 33 a1 b9 45 86 56 aa df fc .e..U.o3..E.V... +| 1920: 24 e6 70 37 c3 e8 bc b6 b6 e1 02 29 dc ba 7e 16 $.p7.......)..~. +| 1936: e9 52 3b 9d e7 f8 d9 d1 5c b2 db 4c 18 29 56 80 .R;........L.)V. +| 1952: f0 f1 b4 07 34 30 2c 2f ee f6 a5 80 f7 a0 b6 7c ....40,/.......| +| 1968: 43 e7 6f e2 a9 87 21 a2 96 82 d7 17 55 4d 33 ff C.o...!.....UM3. +| 1984: 78 75 40 fe db de 63 c3 b5 45 c8 37 40 97 f8 68 xu@...c..E.7@..h +| 2000: b3 03 46 2d 50 1e 2b b0 90 23 09 47 f5 05 db 9c ..F-P.+..#.G.... +| 2016: 23 71 b1 12 e4 b2 ff 41 b4 1f e3 bb 3c 50 d2 ab #q.....A...... +| 2096: 24 2d b9 ba ec 3d e0 88 60 b2 9d 02 07 86 97 13 $-...=..`....... +| 2112: e9 c5 64 c8 0f 03 e1 1b 12 0b ff 39 e1 49 95 fa ..d........9.I.. +| 2128: 6f c3 63 4a 7c 86 38 66 95 b3 57 0b 95 96 c5 e5 o.cJ|.8f..W..... +| 2144: 55 3b 4b 58 d2 59 89 fa 70 40 b5 59 a1 b9 9e 46 U;KX.Y..p@.Y...F +| 2160: ec 3a 5a c8 6d fb ac da d0 62 f6 fb 8c 26 ff 3e .:Z.m....b...&.> +| 2176: fb 54 69 ca 27 4b b5 a3 d1 27 f4 f5 2e d4 26 5a .Ti.'K...'....&Z +| 2192: 0b 3e d6 f4 11 b9 ae fd 42 65 08 6f c1 83 51 1a .>......Be.o..Q. +| 2208: 98 bc ad a9 77 8c da ef 70 dd 9b 6e e6 89 a0 75 ....w...p..n...u +| 2224: 5b c5 14 70 9c 3c 8d 99 b5 83 59 1e 77 0f 16 5e [..p.<....Y.w..^ +| 2240: 38 44 b9 da 9c c0 f0 61 23 5a 67 0e 43 48 81 ff 8D.....a#Zg.CH.. +| 2256: 50 9b 2a 36 d1 18 7e bb d2 4e 83 45 07 f8 35 5f P.*6..~..N.E..5_ +| 2272: a4 8a af 12 ab 91 3e 4f 4f 40 32 3f 7e fc 06 6a ......>OO@2?~..j +| 2288: c4 de 5e aa 2a a5 21 ba 14 51 28 65 c9 9e ef 7c ..^.*.!..Q(e...| +| 2304: 6a 65 67 fc 11 23 1d 76 e3 be e3 10 7a 34 b8 ef jeg..#.v....z4.. +| 2320: 37 b8 ba 9c 1a 13 c7 e1 91 c9 06 ad 98 3c 4b ea 7................ +| 2576: 93 1b 7c 28 fb 53 bc b2 13 2d 8e 22 50 97 4c cf ..|(.S...-..P.L. +| 2592: 06 f1 ac 55 9d c8 ce cd 59 74 9c af 7b bd 6f 7c ...U....Yt....o| +| 2608: a5 b3 a4 87 6a 67 22 f1 82 5b 3e 9e 76 b0 2f d6 ....jg...[>.v./. +| 2624: f6 6d 7a c0 f5 8f 06 c2 5b 09 0f b5 df b9 d7 c2 .mz.....[....... +| 2640: be 51 e0 5e 5a 0a 79 62 3a 3c c8 a1 50 0d a3 36 .Q.^Z.yb:<..P..6 +| 2656: e1 7d 61 4a 1b af f0 42 20 dc 10 a9 13 3d 2b 46 ..aJ...B ....=+F +| 2672: 16 98 d1 24 a8 a1 1a c8 88 0d 63 23 2b 23 b3 8d ...$......c#+#.. +| 2688: 19 13 f9 4c 76 0b 48 40 bb 02 db 1a 54 e8 ea f4 ...Lv.H@....T... +| 2704: f4 57 25 0d 50 1d 31 af 74 00 d8 f4 22 d9 53 e8 .W%.P.1.t.....S. +| 2720: 35 ae 1c c6 82 18 06 4f b6 f3 e8 2c 15 1c 38 1c 5......O...,..8. +| 2736: 5c 47 24 f4 49 44 ef a6 cc de 85 0d 61 aa f6 4f .G$.ID......a..O +| 2752: 18 4b 23 7e ca dd 04 7a 7f 6c d0 70 59 05 0f 31 .K#~...z.l.pY..1 +| 2768: de 19 71 96 7a 1b 93 a2 20 18 d6 a6 f2 8d 28 1d ..q.z... .....(. +| 2784: c0 f3 a9 20 87 f7 dc 10 eb bf bc 1e cf 7a 54 a2 ... .........zT. +| 2800: 7f 96 0d 32 a9 30 30 5c 6d 31 3c 76 4d 65 1f d0 ...2.00.m1..=......USJ +| 2928: 53 80 b1 9e 23 83 49 1a e0 22 9f 5c f3 df 87 d8 S...#.I......... +| 2944: 6f 45 ff 9b 8a 69 80 95 f8 ca bc 5c 0c b1 46 04 oE...i........F. +| 2960: 7e d7 61 72 d8 bf a1 b6 bc b3 48 c7 c2 b3 9d 7c ~.ar......H....| +| 2976: de fb a4 0a 04 2f 97 99 eb ca 8c b5 39 fc 2b 45 ...../......9.+E +| 2992: 69 7f a9 70 c6 73 a1 22 71 b0 0d 53 0c f4 c7 68 i..p.s..q..S...h +| 3008: 85 ec 11 44 0b f9 b7 3b ff b7 91 1b fb bd bf e1 ...D...;........ +| 3024: 01 90 4c 74 35 f3 ac 5a 70 bd e1 4e bb fd a8 dc ..Lt5..Zp..N.... +| 3040: 4d 38 c4 68 a8 e1 8f d1 69 b8 7a 20 b6 5c ed 2b M8.h....i.z ...+ +| 3056: e8 20 dc 0f 7e 29 3e 5f 83 76 e2 d9 b1 c0 07 66 . ..~)>_.v.....f +| 3072: cd a2 d4 57 bc 27 ff 1d 16 7a 11 d6 3d df 04 89 ...W.'...z..=... +| 3088: 45 91 e5 37 62 51 5a e6 0f 0d b8 e9 1a 43 e4 c3 E..7bQZ......C.. +| 3104: 47 94 bf 16 fb 8c e2 f7 b8 c8 7f 7f 34 3b a3 fa G...........4;.. +| 3120: 4f 39 b3 89 ee 7a 1b 80 a6 04 46 24 7f 0c d2 48 O9...z....F$...H +| 3136: b4 cb a2 df 5c af 55 11 a5 c6 f6 de 6a a4 0f dc ......U.....j... +| 3152: ae 12 98 11 9e 95 e2 b1 a6 c2 67 bb 37 ea e8 8e ..........g.7... +| 3168: d1 6f 6c 7a 3a 13 5e 1c 31 92 7e 24 72 b0 f5 b6 .olz:.^.1.~$r... +| 3184: f5 db 3e b9 3b 2a 18 da 93 29 da 2c be 4a de c6 ..>.;*...).,.J.. +| 3200: 6c 55 a0 3e 47 25 76 5c 73 08 99 17 87 e9 30 0e lU.>G%v.s.....0. +| 3216: 91 a8 cd da 9a cd 90 8b 2d 5f 0e 88 7c a7 00 42 ........-_..|..B +| 3232: 84 bd 59 1b ce fa 76 27 33 78 c1 a4 0d 29 98 45 ..Y...v'3x...).E +| 3248: d1 7f b6 7d 56 f6 67 fe 78 ae 83 03 39 66 ce 5a ....V.g.x...9f.Z +| 3264: 62 a1 e3 c5 fd 29 53 06 6e cd ff 0e 5a 95 ca 91 b....)S.n...Z... +| 3280: 1b 24 0d 42 ec e2 24 8a 01 ff 12 0b bf b1 18 74 .$.B..$........t +| 3296: 77 2d f5 9e 0a 74 e6 d4 7d 1c c1 53 d9 f5 65 9c w-...t.....S..e. +| 3312: 40 6d 8f a9 f6 7b b0 96 37 71 c2 96 8c 90 f8 29 @m......7q.....) +| 3328: 07 10 c7 2d f5 1d 80 dc 96 b7 25 65 a6 a2 ff ba ...-......%e.... +| 3344: 5d 1e c1 0d ed b1 6a 83 20 6d 06 28 6d 54 8c 88 ].....j. m.(mT.. +| 3360: 08 02 3d cf f6 79 81 f1 36 3b f0 6e e6 80 39 43 ..=..y..6;.n..9C +| 3376: 64 d6 4a 24 8b 3d 21 41 a9 48 d2 36 65 2f 5a 71 d.J$.=!A.H.6e/Zq +| 3392: eb 6f 2b 47 78 2d 8c 28 91 60 25 3c 35 81 5b 1d .o+Gx-.(.`%<5.[. +| 3408: b7 36 34 71 4c 38 f2 29 7e f5 a8 45 71 95 78 19 .64qL8.)~..Eq.x. +| 3424: 00 e8 87 4d da 50 78 5e f7 dc aa 2d 15 92 49 d8 ...M.Px^...-..I. +| 3440: 4e af 77 30 bd ad 22 1b 6a 84 ff 78 6d 37 cf 1b N.w0....j..xm7.. +| 3456: 8a d9 81 dd 34 15 a7 3a c0 53 d6 ab 5a 38 ec 69 ....4..:.S..Z8.i +| 3472: 6a 88 64 8c a6 ce 50 12 45 a1 7f a2 aa 3a a8 cf j.d...P.E....:.. +| 3488: d6 a0 80 4e d6 7a b6 50 90 64 c0 52 30 51 04 6f ...N.z.P.d.R0Q.o +| 3504: 89 25 02 b3 54 0b fb 24 89 cf f7 98 bd 8e fc 9f .%..T..$........ +| 3520: 62 0c cd fd 57 fd ac 64 b9 2a 94 62 94 38 c6 01 b...W..d.*.b.8.. +| 3536: 0c f1 b9 75 f4 3c 5d 0e d4 1f 96 b3 74 3f 96 03 ...u.<].....t?.. +| 3552: 13 b6 76 32 07 e0 1f 82 d8 27 f3 e7 2e f4 60 d0 ..v2.....'....`. +| 3568: 56 a5 8f 04 37 bd 5c 17 1e 33 94 75 d8 30 59 0d V...7....3.u.0Y. +| 3584: e5 90 f5 09 ee 5c 01 88 14 ca 69 27 08 fa e7 3c ..........i'...< +| 3600: a2 69 df e2 be 35 44 96 b5 06 69 5c 01 3f 52 67 .i...5D...i..?Rg +| 3616: 18 d2 c9 64 a7 ba 0b 59 d8 b8 53 21 74 de 2b 21 ...d...Y..S!t.+! +| 3632: 8a 53 3d 97 14 92 77 ed 51 21 4b f0 2d 69 93 09 .S=...w.Q!K.-i.. +| 3648: 57 3e 92 9f 3e 20 6c 4d bf 8b fd 4f 75 4b 19 5d W>..> lM...OuK.] +| 3664: 48 ef 23 1e 53 11 ee 76 b7 04 08 5a c4 9a 1f 6c H.#.S..v...Z...l +| 3680: 24 cb 15 7f 0b f7 86 8e 60 a4 8d 3c 2a fe 14 13 $.......`..<*... +| 3696: 03 28 80 fa 6b d7 1b 02 a7 0d 9e 88 4d 1f b2 a4 .(..k.......M... +| 3712: 63 c7 65 56 14 df 51 7e d3 d4 3b e3 45 e1 7a 49 c.eV..Q~..;.E.zI +| 3728: 1e 71 40 fe b7 ae 65 10 b1 27 3a 02 31 21 47 11 .q@...e..':.1!G. +| 3744: d9 fc 9c 32 e5 c8 40 0d b6 4b 02 ed bc da 4c 98 ...2..@..K....L. +| 3760: 35 2c d5 9e 6f b3 42 c7 8e 0a c7 fa ae ff 36 5b 5,..o.B.......6[ +| 3776: 76 08 69 3e 3c cd 4d eb 6f 0c a0 f6 23 93 a6 bb v.i><.M.o...#... +| 3792: 2f ed 44 64 22 df e8 6b 21 68 5b 35 d6 8f 68 c5 /.Dd...k!h[5..h. +| 3808: 15 1f 46 fd 12 bc b5 b5 3e a7 e4 9b b2 83 f4 12 ..F.....>....... +| 3824: ea bb 50 84 f4 40 96 c4 64 30 d8 fe 74 5b f2 ba ..P..@..d0..t[.. +| 3840: 9a 64 23 67 4a 3d 7e 54 da 8f 39 18 df 31 88 23 .d#gJ=~T..9..1.# +| 3856: d6 80 5f e9 10 9f 37 22 6f 4a 21 13 20 13 fc 66 .._...7.oJ!. ..f +| 3872: fc 4b db a8 d9 aa 55 01 48 3e 8c ac bf 16 fc 62 .K....U.H>.....b +| 3888: 95 2c 44 1f 27 bf 7b 45 7d 28 55 14 1f ed 56 ed .,D.'..E.(U...V. +| 3904: 24 5b 11 ff be a0 7a 20 3b 3e 9c 2c e6 d6 b0 ef $[....z ;>.,.... +| 3920: b4 df 16 73 f2 d3 a9 90 2b 54 c7 7a fa 25 e7 ee ...s....+T.z.%.. +| 3936: da 99 8d d7 b5 7d 0f 72 c7 61 75 d1 d7 23 dd 41 .......r.au..#.A +| 3952: 1e 46 ee ef 41 86 00 9f 1c 47 36 75 95 f6 1d 89 .F..A....G6u.... +| 3968: 13 c0 75 f8 cc 5e 08 93 e4 de a8 ee d2 ce c2 32 ..u..^.........2 +| 3984: e4 16 b0 c8 82 c1 2a 74 ed 5c 5f c1 99 f7 07 a7 ......*t.._..... +| 4000: b3 50 21 87 a1 43 dc 17 4f 2d 47 e0 be 53 ad 17 .P!..C..O-G..S.. +| 4016: f9 09 67 d1 4f 1f 72 17 62 b7 03 fa cd de 3e a7 ..g.O.r.b.....>. +| 4032: 25 a9 e7 a0 e2 3d a3 6b 2b 34 3f 55 46 18 df ef %....=.k+4?UF... +| 4048: 16 0a ce c8 67 58 eb 64 eb 7e a3 5b 4e 85 49 64 ....gX.d.~.[N.Id +| 4064: d7 f9 ec 0d 4d b6 1d 49 bb 93 e9 79 3d e1 f9 ad ....M..I...y=... +| 4080: 6d c0 45 9c 46 26 21 41 10 dd 4a 12 fc 28 f4 cc m.E.F&!A..J..(.. +| page 10 offset 36864 +| 0: 0d 00 00 00 01 04 3f 00 04 3f 00 00 00 00 00 00 ......?..?...... +| 1072: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 97 ................ +| 1088: 3e 02 06 00 ae 7c 00 00 9a 84 85 31 87 79 cc e4 >....|.....1.y.. +| 1104: 12 98 6f 2c 46 b6 5d 4e d9 5c ba 21 2f cf 51 9d ..o,F.]N...!/.Q. +| 1120: 9f 8c 48 7d 5a 15 ae 1c 54 d7 f0 7f a2 e6 cc fd ..H.Z...T....... +| 1136: ad e6 3c d5 e5 98 5d f2 64 82 5b 9e 6c d6 05 8f ..<...].d.[.l... +| 1152: de fb 33 3a 37 b3 1d 58 04 47 13 cd d9 39 17 a9 ..3:7..X.G...9.. +| 1168: a7 14 f1 fe c8 3b 21 40 a8 22 f5 3b 64 74 14 dc .....;!@...;dt.. +| 1184: c5 9e 99 8b f3 d4 5f 1a e8 1d 93 98 9e 00 7d ee ......_......... +| 1200: 9f 0f 49 b5 af 12 7e 47 8e fe 5f 70 a1 9c 32 ae ..I...~G.._p..2. +| 1216: d1 23 33 d1 c0 65 b6 8e 5a 4a 03 2f 74 27 0f 3d .#3..e..ZJ./t'.= +| 1232: 3f 42 0d 03 5f 6c ef 2c 54 6a bd ec 88 eb cd 1f ?B.._l.,Tj...... +| 1248: dd 03 9c 06 18 8e a4 c1 a5 3c ec 9d 68 da d9 d3 .........<..h... +| 1264: d8 8f ba 16 95 fd 94 ed 19 2b 2f 5e 0e a5 fa de .........+/^.... +| 1280: dc c5 a0 a2 d4 31 0f dc f5 8b 4a 28 4f 6f 9f ff .....1....J(Oo.. +| 1296: 7f e6 d4 d0 08 7e 5c 76 9a 3a ea 0e ea b4 9d 45 .....~.v.:.....E +| 1312: 4b db 96 ed 27 ad a8 09 63 3c d0 1d d9 b1 dc 94 K...'...c<...... +| 1328: 6a 8e e7 6a 9b 6e d1 8b f7 c8 60 1c 85 e9 a2 3e j..j.n....`....> +| 1344: 0c b6 2f 40 b0 2e b6 53 8f 94 74 6b 39 13 fd c9 ../@...S..tk9... +| 1360: 44 77 64 7c 62 e0 51 2e 04 0f 6a 99 5e 68 6f 72 Dwd|b.Q...j.^hor +| 1376: 08 92 02 bf 36 78 0c 98 c3 3f 3d e3 ac a4 2d 3b ....6x...?=...-; +| 1392: d7 51 cf f9 25 72 59 36 b0 ea 51 a1 9c bf 13 f1 .Q..%rY6..Q..... +| 1408: dc f5 36 f0 d9 83 fe 0a ff b5 00 ab 5b 4e 0b 33 ..6.........[N.3 +| 1424: 0a a1 fa c1 02 c8 7a af 00 30 54 d3 6e a8 37 b4 ......z..0T.n.7. +| 1440: 02 88 16 41 95 86 1f 36 e0 98 43 d5 55 57 c6 5e ...A...6..C.UW.^ +| 1456: 0d 10 40 fd 3d 85 a1 5f 19 3f 18 87 11 d9 6a ca ..@.=.._.?....j. +| 1472: 8b 5c 14 cc 64 48 b9 42 71 61 30 ca 0d 2c a5 67 ....dH.Bqa0..,.g +| 1488: 6b 4f 5b 34 1b f3 7b a1 1f ed e4 3b ef 68 27 00 kO[4.......;.h'. +| 1504: f3 8a 27 ff de 2e 07 ab 03 b1 5b b9 c3 84 53 f1 ..'.......[...S. +| 1520: d0 c5 8c 65 50 4b 7a 35 06 0c d5 2a ce 4c b9 02 ...ePKz5...*.L.. +| 1536: 7f cf 2a d9 0d 2a 1c c1 5f 94 cb 5c 05 4d 3a 7a ..*..*.._....M:z +| 1552: aa c5 3e 4e 26 93 8a 4d 1d fc 75 23 9f b5 27 87 ..>N&..M..u#..'. +| 1568: fc f7 3d aa a6 3e 5d c3 55 63 e2 a8 2a 7b 2e 26 ..=..>].Uc..*..& +| 1584: e9 64 59 1f 2a e7 ff 7c d4 69 a6 34 bb d9 23 81 .dY.*..|.i.4..#. +| 1600: de 4b ba f3 91 cd 9a 8b 98 56 f0 b8 73 7b 83 f1 .K.......V..s... +| 1616: fc e9 5c 01 7e 74 66 d0 0c 01 79 c0 b2 49 0f 78 ....~tf...y..I.x +| 1632: 79 ef 20 96 ec cb 63 67 fe 43 a7 ea 5d af 68 12 y. ...cg.C..].h. +| 1648: 84 dc 0c 5c 31 6a 1d d6 a1 1e d4 25 a3 f0 7c 19 ....1j.....%..|. +| 1664: dc 84 4f 7b e4 5a e5 40 23 07 b3 5b 92 92 4b 3a ..O..Z.@#..[..K: +| 1680: 0e 11 9c b4 ba d4 d1 ff 22 af cd 7d e8 86 c3 0a ................ +| 1696: 14 eb 01 13 f8 99 56 8c c6 26 55 8d a7 cc cc 00 ......V..&U..... +| 1712: 4c 16 7a 07 de 6c 69 02 e2 a1 e6 e1 d5 ff 12 f5 L.z..li......... +| 1728: ee d7 8f 90 e1 78 99 09 de 27 b8 b3 e1 7a df 8a .....x...'...z.. +| 1744: 08 cb c0 67 ff d4 48 bd 0b 4c 65 56 5a ed 02 47 ...g..H..LeVZ..G +| 1760: 1c de b7 58 7c cf 68 a4 7f e6 db 74 26 55 9c 14 ...X|.h....t&U.. +| 1776: 76 5a bf f3 e1 79 23 5e 33 f8 65 13 bb 36 cc ed vZ...y#^3.e..6.. +| 1792: 9e 12 63 b2 c2 14 3f 6a 4d e3 a4 63 bf 30 0f cb ..c...?jM..c.0.. +| 1808: f2 f0 d1 81 a7 26 d7 c0 92 9f 06 79 bd a7 a0 8d .....&.....y.... +| 1824: 74 21 7b c6 46 49 5c fd 01 a6 92 56 3b eb e6 d5 t!..FI.....V;... +| 1840: b4 4d 76 2a e8 00 98 3b a3 67 a9 7d 02 25 96 3b .Mv*...;.g...%.; +| 1856: 45 19 a0 d4 b5 67 88 cf 48 eb 1a b6 0e f3 2d 62 E....g..H.....-b +| 1872: 3e 0c fa b3 e7 a4 f1 76 d4 d7 f5 19 6f b2 9e e8 >......v....o... +| 1888: e1 a1 b7 be 3a ff 69 db eb 75 1c 0c 91 7f 02 4f ....:.i..u.....O +| 1904: df 15 af 09 48 2c d0 86 bd b0 80 82 e7 7b 1d a6 ....H,.......... +| 1920: 38 f1 24 79 d0 8e 4c 07 9f ed 9f fb 90 a1 7e fd 8.$y..L.......~. +| 1936: 50 c6 fe d3 61 04 a1 03 34 30 bd 98 db 5c 18 e3 P...a...40...... +| 1952: 94 d9 ba 8f 64 f8 6b c1 21 3b 22 b7 3e 71 5a 66 ....d.k.!;..>qZf +| 1968: cf 9f 51 c4 a2 36 c8 ba c3 2a 95 2e 67 e4 87 3e ..Q..6...*..g..> +| 1984: 07 ae 66 ad ec af c6 17 62 11 be 5f 15 fc 61 53 ..f.....b.._..aS +| 2000: 76 eb f5 78 da 32 8c fa 4e d3 cc 27 19 dc 89 fc v..x.2..N..'.... +| 2016: 37 4f 74 61 f7 5e 97 0f fe fc af aa 12 ee ea f2 7Ota.^.......... +| 2032: 68 36 36 cd 7e 57 41 75 48 be cd 46 e3 cd 3a 99 h66.~WAuH..F..:. +| 2048: 31 33 9a 84 8a 83 a2 fc 85 85 3c bc cf 07 b4 6d 13........<....m +| 2064: 57 d2 c9 63 a2 9d 42 07 4e cd 65 2e 65 0a af 03 W..c..B.N.e.e... +| 2080: dc a9 98 57 b6 7f da 1d b7 3c e0 ef aa 18 eb 3f ...W.....<.....? +| 2096: 78 f1 34 e6 bf c7 28 34 11 a7 bc b3 34 79 f2 85 x.4...(4....4y.. +| 2112: ed 7d fe 38 a5 48 b3 b4 8a 0d 75 12 65 04 f8 88 ...8.H....u.e... +| 2128: 71 b9 d4 86 48 c7 ea 2f af 4d 50 b9 50 a7 17 f6 q...H../.MP.P... +| 2144: 1f a2 e1 b8 aa ed 6d 85 3a e2 91 be 94 c8 fc db ......m.:....... +| 2160: 93 50 0e 50 7c cf 52 f7 55 81 3e 1a 59 4d a8 36 .P.P|.R.U.>.YM.6 +| 2176: ee 07 f1 9e 26 4a 1e d3 0b 7d 52 e3 bc 7c 91 78 ....&J....R..|.x +| 2192: 02 48 7e b5 4c 32 1e a3 ba db 93 61 94 3b d7 22 .H~.L2.....a.;.. +| 2208: 7b cb 46 5b c0 a5 1e e6 5a ed c9 82 07 48 17 d6 ..F[....Z....H.. +| 2224: de 85 ca 47 e1 16 5c c8 dc 30 5e 27 65 60 a5 41 ...G.....0^'e`.A +| 2240: 46 0a 12 0e 97 63 3f 05 5f 62 83 e0 cb 72 b6 61 F....c?._b...r.a +| 2256: 6a 4c 1d 7b 09 28 75 54 1c cd 29 bc f8 71 34 56 jL...(uT..)..q4V +| 2272: d6 ac 77 34 44 0d 5f c3 0e f1 d7 b3 dc 0d 1f 85 ..w4D._......... +| 2288: e3 db 45 88 fd db ab bf a6 ff bc 08 f3 3c 00 f0 ..E..........<.. +| 2304: 9f 3b 07 36 cc 37 f0 6d 14 a8 6b 69 d6 e4 3a ab .;.6.7.m..ki..:. +| 2320: 4f 5a b5 ad 3a e5 e3 d6 1c e1 6b 15 97 69 b0 41 OZ..:.....k..i.A +| 2336: 91 09 50 02 6a 5c c1 9e fe e7 38 7e 19 8a 36 44 ..P.j.....8~..6D +| 2352: 51 04 8d 61 d9 a3 12 34 00 b2 b1 60 f3 35 eb 8a Q..a...4...`.5.. +| 2368: dc bb a1 67 48 b6 95 81 91 fe 20 dd 03 74 bb 3b ...gH..... ..t.; +| 2384: 8c 33 58 b2 d8 55 7e 04 ea 42 6c 02 e8 26 18 19 .3X..U~..Bl..&.. +| 2400: 23 15 21 a5 73 ca 08 7b dd db fb b2 12 df 6a 5a #.!.s.........jZ +| 2416: d7 ac e4 57 61 ac 3f 81 77 df f6 a3 97 5c 69 47 ...Wa.?.w.....iG +| 2432: 5a b7 75 23 6c 60 be 97 ee b5 5d a0 c3 60 15 4a Z.u#l`....]..`.J +| 2448: 79 eb 81 9f 2a 74 22 13 7e ca 4d 3b 19 13 62 58 y...*t..~.M;..bX +| 2464: 78 25 ca 21 c3 10 1e 96 34 82 37 6b 08 b5 9b f6 x%.!....4.7k.... +| 2480: 7b 87 97 cd bf 64 67 b4 10 f6 84 16 62 74 a5 b0 .....dg.....bt.. +| 2496: 1f d4 c7 1d 8c 2d b5 10 36 5b fb 06 80 fe 97 59 .....-..6[.....Y +| 2512: b6 5a 08 0f 1e ff 0f 5f bf 28 46 4b d8 84 6c ad .Z....._.(FK..l. +| 2528: 05 c0 25 89 a9 cd 6a be a3 59 84 f0 17 1c 37 8a ..%...j..Y....7. +| 2544: 8d 09 17 bf 7d fe 47 b4 d6 d6 56 1f d8 04 66 59 ......G...V...fY +| 2560: 2c c4 b4 91 a9 ff da 4a 9c b1 29 ff 92 db 6f 19 ,......J..)...o. +| 2576: ef eb 99 ba 6e 65 2f 6a 7f 1a cf ad a3 96 8c 1d ....ne/j........ +| 2592: 62 86 42 3e a3 64 fc e0 40 4c 7c 60 77 b5 42 68 b.B>.d..@L|`w.Bh +| 2608: 3f 09 37 68 02 75 2c 22 83 d5 04 17 eb a7 e2 71 ?.7h.u,........q +| 2624: 29 36 b7 1b c5 1f 11 ce 8d 91 5a 25 39 50 16 2b )6........Z%9P.+ +| 2640: 60 29 50 9f 17 55 b0 9b a5 92 92 f8 1b e3 9c a3 `)P..U.......... +| 2656: e2 a4 cd 90 1e 21 ac 30 ac 35 de 25 30 88 6c 2c .....!.0.5.%0.l, +| 2672: 79 ea b5 0d 58 a5 37 2b ac af 7d 1f af 32 ca 58 y...X.7+.....2.X +| 2688: 27 17 68 f2 a3 ca a6 cc b2 be 12 c1 a0 43 1e 7d '.h..........C.. +| 2704: 11 ec 8e 23 22 0a ca cd 70 d1 05 fd c0 68 92 e9 ...#....p....h.. +| 2720: 1c 55 85 48 10 37 7c 02 69 bf 3f 86 cf d6 40 38 .U.H.7|.i.?...@8 +| 2736: a3 9c 83 40 b8 4b 83 51 50 0d d2 b9 c3 32 09 f8 ...@.K.QP....2.. +| 2752: bc 7b 9b b8 d7 2e 4f a0 96 48 a7 5a 1b c9 71 fb ......O..H.Z..q. +| 2768: f4 2b ff 05 54 89 26 b9 6f 25 4a b9 e2 2b e8 86 .+..T.&.o%J..+.. +| 2784: 43 22 f6 20 28 a9 39 d9 09 a2 dc 60 14 09 d6 0d C.. (.9....`.... +| 2800: 61 7c 15 5a 8f 3f cc 00 12 f5 e0 45 fe 14 1a cc a|.Z.?.....E.... +| 2816: 98 c4 de 48 75 12 02 2b 79 a1 4a 33 a5 7c 3d cd ...Hu..+y.J3.|=. +| 2832: b0 5c dd 77 15 5f d9 24 b7 6b 62 80 cb 35 5c e6 ...w._.$.kb..5.. +| 2848: a6 57 2e e9 00 9e 20 a9 c6 f0 63 a2 0e eb 9d f3 .W.... ...c..... +| 2864: bb 2c 56 03 68 35 53 5a fb 4f 44 8e 0f a4 9c 9a .,V.h5SZ.OD..... +| 2880: 0d b7 2c a9 03 14 8c 51 23 21 fb fd 46 07 68 a7 ..,....Q#!..F.h. +| 2896: f3 09 25 e4 98 55 24 da 72 ee 50 00 95 04 7c 74 ..%..U$.r.P...|t +| 2912: d0 07 8b 92 f9 27 11 3e 41 b4 3e 6c aa 56 ed 54 .....'.>A.>l.V.T +| 2928: e3 40 4d 67 8b 7b 63 cd 62 37 ec e2 1b b4 f9 eb .@Mg..c.b7...... +| 2944: ca c7 6e 8a d3 7e f5 e9 e1 33 84 31 05 cb f8 e4 ..n..~...3.1.... +| 2960: 02 76 c2 2c b9 00 32 5f be b5 f1 c8 78 e8 cf 22 .v.,..2_....x... +| 2976: 65 d8 2b 43 2c 2e 5d fb 2e 58 92 d2 3e 9b 7e 67 e.+C,.]..X..>.~g +| 2992: a7 3b 59 18 84 5d 61 92 ec 57 97 b6 bd e3 7b f2 .;Y..]a..W...... +| 3008: ff 3b 9f 48 39 ad 10 81 51 85 7c 6b 5c b0 53 10 .;.H9...Q.|k..S. +| 3024: 60 9b 4e 0d 86 c0 31 b9 a1 95 05 f4 54 13 52 6a `.N...1.....T.Rj +| 3040: 96 50 1b e5 f5 54 98 63 8f 07 28 cf b4 ba 9c 3b .P...T.c..(....; +| 3056: dd 85 3c a6 15 a6 31 f6 aa 1c 3b 31 3e d9 6e c3 ..<...1...;1>.n. +| 3072: 09 74 b9 8e 59 a1 50 10 e0 f0 48 a9 f1 dd a1 ab .t..Y.P...H..... +| 3088: b6 fc 53 9f bb 38 69 78 07 50 8c 77 cb d1 89 e2 ..S..8ix.P.w.... +| 3104: cb b9 51 dc c7 2e 8a 56 47 14 67 80 eb 7a 16 f8 ..Q....VG.g..z.. +| 3120: 87 89 58 d1 55 58 c5 bd 62 24 4d ef 46 9f 68 94 ..X.UX..b$M.F.h. +| 3136: 61 9a 08 81 dd dd f1 51 c6 40 63 9b a1 3c 3e 5a a......Q.@c..<>Z +| 3152: 78 b2 10 7c a2 47 45 57 ef 98 85 82 7e 21 3b 77 x..|.GEW....~!;w +| 3168: 1e d2 fd 0d bf a5 41 6f f5 ee 4c ed 82 f3 15 ea ......Ao..L..... +| 3184: 2e 57 5e 78 7c 22 2e a4 a6 9c 3f 50 e9 4b 02 11 .W^x|.....?P.K.. +| 3200: a0 7c 5a 19 82 9b 34 13 39 32 07 f1 46 4c ad fa .|Z...4.92..FL.. +| 3216: a7 5a fa 22 5c 84 4d 8d 07 1f 17 1f 1a 49 5f fa .Z....M......I_. +| 3232: df 22 56 ac ce 27 4d 00 a9 b0 c0 77 d8 10 56 cb ..V..'M....w..V. +| 3248: 76 e0 c8 4a 8a d1 37 c1 cc b8 74 d3 e9 61 1b 0c v..J..7...t..a.. +| 3264: 81 7b be 3d e3 17 43 75 5c ba 6f 61 74 d4 a4 e9 ...=..Cu..oat... +| 3280: d7 0f 2f d2 bc 7d 0b 6e 32 ae fc 20 db 08 39 b5 ../....n2.. ..9. +| 3296: 8f 78 1f d6 4e 84 46 05 a3 a1 15 e8 9d 36 b1 96 .x..N.F......6.. +| 3312: 3e e3 12 1b 72 28 28 e8 a5 0f e1 16 a2 31 22 48 >...r((......1.H +| 3328: 4b 77 c6 e5 3f b8 f5 bb 29 13 e1 c1 da 79 81 72 Kw..?...)....y.r +| 3344: fa 12 ab 62 da 63 ac 95 2a 1b 4a a4 4f 96 95 c0 ...b.c..*.J.O... +| 3360: 30 38 3b af 2d c5 cd aa 56 ef e4 35 1d 97 9c 3f 08;.-...V..5...? +| 3376: 57 30 2a 7a 1a 0f 4d 9e be 82 a6 fc 5e a7 9d 0d W0*z..M.....^... +| 3392: a8 11 12 39 b5 e7 5f 8d 18 1d bb a0 2b 02 10 d3 ...9.._.....+... +| 3408: 08 3a 47 de a1 93 f3 7b b7 07 21 c2 62 22 f2 b3 .:G.......!.b... +| 3424: dd 67 9c 8c 87 59 7d 87 75 46 0c 85 64 f3 09 7f .g...Y..uF..d... +| 3440: e2 7f ee ca c8 d2 2e 96 14 50 30 89 86 f6 ed 07 .........P0..... +| 3456: ee 9d 78 0c 84 c8 80 ee 2f d4 59 22 12 92 e9 80 ..x...../.Y..... +| 3472: 04 f5 ed aa 7a f5 cd 53 d7 de 45 a5 c3 e1 4e 9d ....z..S..E...N. +| 3488: fb 78 98 4d cb 5b 0b bf 1b 6a bc 50 ba 45 e5 ff .x.M.[...j.P.E.. +| 3504: d7 d0 7b 39 3d 5b eb 92 46 7f bb 21 7f 81 8b da ...9=[..F..!.... +| 3520: e7 c1 e2 12 41 82 19 7a bd 78 de bc e0 51 cf a7 ....A..z.x...Q.. +| 3536: 0e 20 0b 74 cb c8 fe e9 26 64 6e c4 e1 cc 3f b8 . .t....&dn...?. +| 3552: 62 a1 9b 79 bd 9f 05 21 b1 d6 93 66 6f 38 e5 df b..y...!...fo8.. +| 3568: 56 0c 57 c3 3b 57 04 79 f7 f4 b5 20 16 65 18 dc V.W.;W.y... .e.. +| 3584: a8 88 ce 1c bd 7f 40 c8 05 46 17 d7 99 a7 8e 07 ......@..F...... +| 3600: b7 02 2f 8c a6 49 5a 1f ce 63 3a 66 31 c2 3b 78 ../..IZ..c:f1.;x +| 3616: 84 00 80 8e 81 43 07 3a 43 ef e4 df 33 4b 18 33 .....C.:C...3K.3 +| 3632: 45 27 cc 2e e8 cf e8 be 1a 90 97 ed 99 9b b2 6f E'.............o +| 3648: 65 ff 53 ad df f8 58 47 d9 5d 71 6a 38 e4 e8 17 e.S...XG.]qj8... +| 3664: e1 1d 4c 03 cc c1 33 18 96 3e 9c 11 98 55 8e 62 ..L...3..>...U.b +| 3680: 1a af a0 33 36 f7 0c 87 0a 0c f0 43 ff e4 71 19 ...36......C..q. +| 3696: 5a 38 f8 9d 9a 53 d4 48 ff e3 40 89 e2 18 d5 3c Z8...S.H..@....< +| 3712: fb 2c 67 2c b9 f3 e5 d7 5c 97 e9 8f fb 3e 3e a2 .,g,.........>>. +| 3728: 22 63 47 fb d0 65 ed 87 b0 93 e4 28 e5 85 87 68 .cG..e.....(...h +| 3744: fc 0b 64 4c 5b 3c 5f 9f 40 96 d7 34 5b cb d0 a9 ..dL[<_.@..4[... +| 3760: 63 f5 f7 80 f4 67 49 1d 20 0d 84 f9 39 85 28 8b c....gI. ...9.(. +| 3776: 78 52 fc 86 fb f8 33 cd d6 ef ef ca 32 51 98 2c xR....3.....2Q., +| 3792: 54 d4 2e b9 78 0c 2a 5e c0 e6 2c 58 83 60 85 ea T...x.*^..,X.`.. +| 3808: 0b 33 de 33 40 a4 05 80 0d 21 b5 0e 4f 87 a1 a2 .3.3@....!..O... +| 3824: 3a 20 d6 ee 63 7a 31 93 5c 64 f4 f4 57 43 8a 65 : ..cz1..d..WC.e +| 3840: 53 e0 6e 84 c0 ee f6 44 ce 46 ea 97 fc af ec 4b S.n....D.F.....K +| 3856: ff af da 33 b8 69 e5 08 bd 36 17 aa be 1b 99 5f ...3.i...6....._ +| 3872: 80 c2 8c 27 56 1c f4 99 fa c3 1c 54 01 b7 3e 96 ...'V......T..>. +| 3888: 67 b7 f0 31 f7 20 63 51 17 61 f1 b7 06 23 24 35 g..1. cQ.a...#$5 +| 3904: f3 84 c9 01 ec 59 ba 00 83 b8 6c f9 4b 94 01 53 .....Y....l.K..S +| 3920: d4 06 ef b7 a9 08 3c 29 7d ab 8a 88 12 e2 3f a6 ......<)......?. +| 3936: a1 27 be 2e 09 aa d4 15 bc 43 2a 10 ff 2a 2d 89 .'.......C*..*-. +| 3952: fd a0 73 e7 91 14 a3 e2 2c 00 e8 c8 a7 bf 7c c3 ..s.....,.....|. +| 3968: 4c a0 89 bb 70 da 99 1a 39 5f 81 77 af 17 8d af L...p...9_.w.... +| 3984: e8 b0 e0 27 fb f0 b2 39 45 4a 82 97 93 00 a5 54 ...'...9EJ.....T +| 4000: 44 07 b7 1d ce 52 5f bd cd 07 6f ba 6f 8d 6d d7 D....R_...o.o.m. +| 4016: 2b 4f f5 4b 7b 23 45 74 d9 d8 aa e0 15 14 f6 be +O.K.#Et........ +| 4032: 74 f2 b8 38 f9 ba 64 58 93 b2 b6 0c e7 62 10 81 t..8..dX.....b.. +| 4048: ad ce 6f 23 0e cd 1c 3c 1e 30 62 64 95 d6 48 55 ..o#...<.0bd..HU +| 4064: fc 63 67 73 85 40 16 2b ab 0b 96 c4 90 8e ea 04 .cgs.@.+........ +| 4080: 64 13 bb 60 63 d8 6c d3 73 ed a9 10 03 bf 20 04 d..`c.l.s..... . +| page 11 offset 40960 +| 0: 0d 00 00 00 01 04 3f 00 04 3f 00 00 00 00 00 00 ......?..?...... +| 1072: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 97 ................ +| 1088: 3e 03 06 00 ae 7c 00 00 68 95 7f 98 0f 98 e0 e6 >....|..h....... +| 1104: fd 92 02 0f 0f 29 aa bd bc 40 56 6c 5e fa 3f 54 .....)...@Vl^.?T +| 1120: 10 24 70 48 b1 c5 fe e5 0b 21 21 65 1e a4 f1 2a .$pH.....!!e...* +| 1136: fa 3e 9a a1 b8 67 92 ab cd 27 a2 0f f3 b1 77 9b .>...g...'....w. +| 1152: 97 e6 c0 f2 c4 db a4 5c d8 f1 01 bd 3c f4 fd 0f ............<... +| 1168: 00 ca 6d 75 11 fd 56 e2 12 96 e1 f7 18 99 f3 78 ..mu..V........x +| 1184: 9b b7 ef 8c 50 1f dc c5 fa f5 92 fb 91 70 cc d3 ....P........p.. +| 1200: ca 0a c6 9b 42 d4 2f cc ce 98 48 a6 ab cd d1 d8 ....B./...H..... +| 1216: 4c 8a 98 2f 2d dc 48 d9 84 52 8f 86 57 04 b8 14 L../-.H..R..W... +| 1232: ec 33 d4 5e 69 da c5 45 8e 2c 4f d6 fc a3 ee f1 .3.^i..E.,O..... +| 1248: 7c 47 c1 5c 8d 48 1b c2 be f1 ca b8 f4 91 d8 bd |G...H.......... +| 1264: d6 41 f6 40 ee 41 90 51 14 09 a8 c5 cf 38 16 21 .A.@.A.Q.....8.! +| 1280: b5 84 48 6c 8e eb e7 b0 62 69 dd 04 c4 4a 75 f6 ..Hl....bi...Ju. +| 1296: 6f 92 af 16 33 94 81 eb 26 00 9f dd d4 01 73 32 o...3...&.....s2 +| 1312: ec ee 9d 23 bf 82 8f e0 c8 eb 8f aa 4f 69 4b 68 ...#........OiKh +| 1328: a8 4e 3d 6e d8 2c 99 49 fb 96 d1 77 f6 a0 c3 97 .N=n.,.I...w.... +| 1344: 7e c6 7d ff 90 fc 12 97 e7 71 7b 20 ea dd ba 47 ~........q. ...G +| 1360: 3f bc bf 89 2e 26 82 91 71 97 6c 5e 2e ba 8d b9 ?....&..q.l^.... +| 1376: c6 ff 51 b3 17 7d e6 ca 99 75 53 73 99 9f 5a b8 ..Q......uSs..Z. +| 1392: 11 db 5a fb b3 73 55 bd 61 0c 2c e5 03 b2 d5 1c ..Z..sU.a.,..... +| 1408: ce ea ef b4 2e 2c dd d4 55 df cc 7b 13 c9 ba 87 .....,..U....... +| 1424: 25 38 2d d7 fc 7b 47 c1 da 56 42 fe 50 8e 12 a8 %8-...G..VB.P... +| 1440: fd 9d 90 e3 ae e1 f2 5d fa 1e c5 e2 d9 74 b9 55 .......].....t.U +| 1456: d0 5b 90 1d 31 f0 fb 36 d6 e5 df 91 72 4b 69 41 .[..1..6....rKiA +| 1472: 06 33 4e 77 cc 1c a7 d1 3e 49 7f 76 8d d3 73 88 .3Nw....>I.v..s. +| 1488: 27 53 13 95 87 9a 99 cc b2 54 46 34 7f 96 98 1e 'S.......TF4.... +| 1504: fb 60 06 85 93 37 0a 90 30 4b 8a ca 7f e8 a6 6a .`...7..0K.....j +| 1520: a1 94 da ac 1f 47 80 1a 81 8f dd 19 9d 70 88 73 .....G.......p.s +| 1536: b7 4c 22 f7 fe 8b c1 05 5a ea e0 20 5d 01 55 c4 .L......Z.. ].U. +| 1552: 7c 3d 99 f7 8a 1a 15 23 1a a6 5a 02 52 35 2f 14 |=.....#..Z.R5/. +| 1568: ea 01 d4 2b 97 3f 7c e3 4f 7b 58 ea 83 2f 1f 03 ...+.?|.O.X../.. +| 1584: c3 64 4f e8 81 f2 ec 19 92 5f c7 b1 27 5a 74 12 .dO......_..'Zt. +| 1600: fd f3 d1 48 0c eb fd 57 9d 28 1e 44 28 3d 69 aa ...H...W.(.D(=i. +| 1616: 94 66 23 55 11 3b 01 52 76 0b 3b e0 fe 67 e0 fe .f#U.;.Rv.;..g.. +| 1632: f1 e5 18 2b 19 ec 22 a0 33 94 e2 33 3f 59 fc 04 ...+....3..3?Y.. +| 1648: 15 c3 ee a0 f8 18 7d 07 39 04 04 e5 df ee 0c 38 ........9......8 +| 1664: 31 94 76 b0 ec 42 89 fe 3a d9 d2 1a 4d ee b0 67 1.v..B..:...M..g +| 1680: 10 f4 e6 d1 dd 96 9c d8 ec 10 f0 4b 8c 02 37 54 ...........K..7T +| 1696: 5b b8 e6 d6 d2 d3 ad cd cc 84 02 bd 1f bf 36 de [.............6. +| 1712: fe d3 8f 68 94 5d c7 19 18 d0 91 e5 be c2 e6 40 ...h.].........@ +| 1728: b6 21 ec 29 08 49 08 96 97 a1 14 3a 32 cb 0b 24 .!.).I.....:2..$ +| 1744: d4 8c 77 f9 f8 4b 78 8c cf de 90 27 b8 a1 5b dd ..w..Kx....'..[. +| 1760: 84 c5 88 21 39 fe 1e 88 c0 1c 9b d7 46 44 4f a7 ...!9.......FDO. +| 1776: 13 49 c0 49 d6 c4 c2 06 ab 24 9b 8d 80 83 32 32 .I.I.....$....22 +| 1792: b0 ff e5 47 db 08 c0 17 c5 98 67 12 fa 2f 86 39 ...G......g../.9 +| 1808: a5 f6 13 03 4d 25 af dc 2c 52 e2 5e f0 36 b3 a2 ....M%..,R.^.6.. +| 1824: 13 5b d4 47 14 5e 26 66 f5 a8 48 b3 1a 9e c5 03 .[.G.^&f..H..... +| 1840: 39 66 f3 34 fc c2 ff a8 0f a5 66 a7 7a 6b ad dc 9f.4......f.zk.. +| 1856: 77 e2 c7 fb 10 c6 fe a6 2a 64 fc e3 84 8c 12 0e w.......*d...... +| 1872: 5d 96 80 c8 d2 0d d9 e4 f1 bd 96 51 a0 ad a2 cb ]..........Q.... +| 1888: 99 d9 13 4b de c3 f6 ab 20 d8 ad 61 23 fd be 39 ...K.... ..a#..9 +| 1904: 97 0b 90 22 49 08 e2 38 3e 43 e6 91 9a ef e5 d7 ....I..8>C...... +| 1920: 9e d3 ff 82 51 99 6d 12 f5 74 0d 84 6e f8 ed 63 ....Q.m..t..n..c +| 1936: b8 ff 4d dd ea ae a3 0c 55 13 a1 03 4c 8b 3f 0e ..M.....U...L.?. +| 1952: 54 b7 02 3e 01 5a 77 ad 6f fc 92 bd b3 6f 3e 89 T..>.Zw.o....o>. +| 1968: 8e fe e1 2e 1c d4 56 c8 5f 4c 57 73 99 95 d6 be ......V._LWs.... +| 1984: f8 f5 17 22 5f 3f 13 5d 98 c9 b5 74 b2 17 7c c8 ...._?.]...t..|. +| 2000: dd d0 a8 d1 fa da 22 5a e8 34 f9 83 93 1b 7f e7 .......Z.4...... +| 2016: ba 48 ab e4 cd 3d 54 ec a2 9b 4b ca cf 84 0a d3 .H...=T...K..... +| 2032: 4d 8c bc 0d 73 1a 29 05 0c 60 0a 4a 6e 54 d3 0f M...s.)..`.JnT.. +| 2048: 84 00 df c0 f3 0b 73 2b 3b f0 60 68 91 ae cd 60 ......s+;.`h...` +| 2064: 59 0b ee b7 dc 7c eb b1 cc 70 f3 bb 6a 27 3b bb Y....|...p..j';. +| 2080: 20 41 3c 84 9d 3d 06 94 0c 53 eb 9c 31 e3 8a a0 A<..=...S..1... +| 2096: 1e c1 65 ef a6 78 92 ae 2e f9 64 49 58 b7 c0 23 ..e..x....dIX..# +| 2112: 2b 4c ab 93 2c 78 c2 86 32 09 d0 8e bf 34 b4 9e +L..,x..2....4.. +| 2128: 59 5c 6f 69 bb 85 5d a6 02 b2 01 85 89 23 40 7f Y.oi..]......#@. +| 2144: 23 3f c7 67 da 35 cf 2e d0 36 1e 71 fa 78 da c5 #?.g.5...6.q.x.. +| 2160: 41 db 9a 14 b1 48 d4 02 36 2f ed 6a 85 4a f4 f6 A....H..6/.j.J.. +| 2176: f4 3f 46 81 2d fa 92 47 21 16 14 84 f8 c9 18 86 .?F.-..G!....... +| 2192: 74 45 16 8d b3 cd 58 93 40 62 9b 24 6b af d3 44 tE....X.@b.$k..D +| 2208: 67 f9 0a 7f e5 25 01 b9 76 3f 8e 4d 82 b8 04 ae g....%..v?.M.... +| 2224: ef ed 12 c3 9f c6 a9 54 03 8a 78 0d f4 7e bf 7d .......T..x..~.. +| 2240: c1 f5 be 24 33 54 77 e3 7f c4 c9 fd 5c 79 6d 54 ...$3Tw......ymT +| 2256: 67 2f 83 a3 04 8b 16 09 71 ff 47 67 14 6a 84 71 g/......q.Gg.j.q +| 2272: 39 54 18 0b 7d 7e ac ef 62 0f 4b dd 9c d0 0d 20 9T...~..b.K.... +| 2288: 9e 62 98 79 11 53 de 73 a9 9d 44 5e f5 5a 23 62 .b.y.S.s..D^.Z#b +| 2304: 08 01 fb de 39 57 24 ac c6 b5 5f e2 6b 07 18 2e ....9W$..._.k... +| 2320: 27 24 42 96 d8 31 68 d3 0e bb 65 9c 01 f8 93 ba '$B..1h...e..... +| 2336: 4a 2a 60 3a b3 c2 9c 17 66 1d 34 4c 0f 90 38 5e J*`:....f.4L..8^ +| 2352: a4 9c 72 9c 16 d7 c4 98 33 ed c1 95 a2 d7 cc a1 ..r.....3....... +| 2368: 3b 36 28 1c 44 1c 8f c9 f7 bc eb ed 5a d3 2d 69 ;6(.D.......Z.-i +| 2384: 6f 86 9a 2f 15 91 aa 63 0d 19 f9 bb 07 6b 87 f5 o../...c.....k.. +| 2400: 0a 48 7e db b6 9a c3 97 01 d1 91 44 37 7c ce 5c .H~........D7|.. +| 2416: 63 43 c0 92 20 31 b4 5c 36 98 01 50 05 ec 1d ac cC.. 1..6..P.... +| 2432: 73 10 66 97 48 60 c2 2b 46 3e 2b fc 52 1d 42 87 s.f.H`.+F>+.R.B. +| 2448: d3 af 2c 42 7f c4 a2 0b 2b d6 09 64 0a f9 9d f5 ..,B....+..d.... +| 2464: 54 b8 1d 96 23 1f 22 0e 56 de dc 0a f8 97 55 41 T...#...V.....UA +| 2480: 56 ff 72 20 73 2b 50 fe 0d 79 c2 d3 61 73 22 da V.r s+P..y..as.. +| 2496: 68 8e 77 23 19 ca f9 7c a6 9c 91 27 ed eb f8 9c h.w#...|...'.... +| 2512: b6 bb 72 dc 3a 62 23 17 fb 04 98 bb ce 1a fb 1f ..r.:b#......... +| 2528: 17 ab aa 46 46 5a 8b 98 c6 7e 0e 18 74 8a 62 9e ...FFZ...~..t.b. +| 2544: 1f 7f f4 dd 8c 1d 68 8d 54 3d 0c 06 95 f9 9f 06 ......h.T=...... +| 2560: 3f 97 71 00 21 d5 e9 c0 44 7b 98 27 16 ba d2 fa ?.q.!...D..'.... +| 2576: c2 33 1d 4a 34 ec ae 4d a7 6c 68 77 9b f6 7b 2c .3.J4..M.lhw..., +| 2592: 6e d3 f6 1b b6 35 6f 47 69 be 5f 96 66 13 c3 8e n....5oGi._.f... +| 2608: 2f f1 4a 5e cb 26 00 73 33 a9 05 12 7b 6d 5b 96 /.J^.&.s3....m[. +| 2624: b7 3d 7e bc 62 aa c7 fe d2 fb 11 d5 7c a3 bf 90 .=~.b.......|... +| 2640: 09 fa ba 2b 2e 8d 65 c6 3f 21 41 fa 3d 71 f8 8d ...+..e.?!A.=q.. +| 2656: e5 77 34 b2 ea 4d 28 d1 48 fc 4c 1f 2e db 9c 58 .w4..M(.H.L....X +| 2672: b1 04 54 ce 4b 68 a3 7b b6 28 16 19 22 d7 fe d3 ..T.Kh...(...... +| 2688: f9 88 dd ca f6 26 43 88 28 bb 0f 62 3b d5 d4 cf .....&C.(..b;... +| 2704: af 90 27 ca 8e 40 62 f6 50 a8 2b 23 d4 3e 6d 32 ..'..@b.P.+#.>m2 +| 2720: e0 62 79 28 29 ab fe 77 21 ad 98 62 11 8a d2 90 .by()..w!..b.... +| 2736: 9a 83 73 c5 44 45 cb dd 71 7e 45 b7 79 d8 ab 3f ..s.DE..q~E.y..? +| 2752: ea 53 89 0b 8c 18 b3 95 1b 45 d5 dd 45 5b 79 b0 .S.......E..E[y. +| 2768: e8 c2 a9 58 77 cf c2 5b 43 a2 81 0e 43 9c c2 6d ...Xw..[C...C..m +| 2784: 5b a3 7a ef ed e1 24 56 54 a4 e1 ef 07 e7 9b 0e [.z...$VT....... +| 2800: 1f 32 78 3d a4 0f c2 52 ee 7d 4e 4d 80 4d ff 00 .2x=...R..NM.M.. +| 2816: 6c 8f da b7 ff aa fd a6 05 c1 31 e1 03 5c a4 e1 l.........1..... +| 2832: 92 39 d9 be 7a 16 9a c2 4b c7 01 eb ef 54 f8 2a .9..z...K....T.* +| 2848: 60 82 bb f6 4c 86 d5 8b 23 ce 88 52 a5 35 a7 ba `...L...#..R.5.. +| 2864: b6 31 e5 ec fe 30 f9 06 98 e7 bd eb 83 08 33 e5 .1...0........3. +| 2880: c5 a2 12 68 ca cb 97 77 db 60 94 4a 43 fb c1 04 ...h...w.`.JC... +| 2896: f1 8d 16 af 2e 8d bf 61 2a ff b5 4b 80 65 8a 07 .......a*..K.e.. +| 2912: 91 17 7f b7 ee c0 57 ce 40 82 c7 c8 57 89 62 61 ......W.@...W.ba +| 2928: b8 63 fe eb c2 97 30 ed 71 84 23 b3 48 e9 d9 ba .c....0.q.#.H... +| 2944: 71 c3 66 f5 6a 66 5a ee e6 bf 72 fe 80 a1 40 24 q.f.jfZ...r...@$ +| 2960: 75 0e 01 f9 1d 18 c3 fd 73 1c 21 92 5c 2b 07 a0 u.......s.!..+.. +| 2976: 83 80 5c 0f 20 fe e9 55 d6 f4 4c 96 d8 ab 7a 5c .... ..U..L...z. +| 2992: d6 d1 1e 44 60 ee 0a 30 7e 92 cf af a3 41 20 d5 ...D`..0~....A . +| 3008: 0b de fb 9c f4 81 76 5c d6 58 5d 86 1e 14 10 c5 ......v..X]..... +| 3024: 12 b5 f1 2d 73 09 0b ad 33 2c 49 5f 59 a7 08 80 ...-s...3,I_Y... +| 3040: 30 bf 50 61 b4 0b b2 3c 53 f3 de 2f e0 87 59 58 0.Pa...MC....u. +| 3200: 36 53 ad 19 cc d1 3c 57 45 64 fc e4 19 89 42 81 6S........... +| 3424: 65 16 ad c6 c4 a3 75 78 94 b2 ce b5 9c 50 df 02 e.....ux.....P.. +| 3440: f3 c2 91 11 5a 81 de eb 31 24 16 50 16 d8 5f c5 ....Z...1$.P.._. +| 3456: df 62 1b 24 37 bb cd 5d a8 a7 93 d0 6a 5e d8 55 .b.$7..]....j^.U +| 3472: 74 cf 69 a2 4a 05 07 2e 61 09 0e fa 1e 12 ab 62 t.i.J...a......b +| 3488: d9 ca ff ab 2b b0 3b 16 16 b0 95 15 27 26 89 46 ....+.;.....'&.F +| 3504: d3 67 fb 0f 33 00 18 44 c3 a1 92 de 6e 6d b0 67 .g..3..D....nm.g +| 3520: b0 65 97 fa b9 10 1d 39 15 10 95 f1 b3 cc a3 d4 .e.....9........ +| 3536: 28 06 9f b7 00 be a1 06 8f 61 34 66 92 fa 58 0b (........a4f..X. +| 3552: 4f 91 6d 93 31 32 cb a1 97 a5 18 b2 f1 bc fc 70 O.m.12.........p +| 3568: c0 86 62 24 4b 82 ce b3 71 30 53 ac 42 c7 32 2d ..b$K...q0S.B.2- +| 3584: 3a 0e 3f 8a 91 89 0b 46 55 f3 5d 73 b1 74 a2 9e :.?....FU.]s.t.. +| 3600: 97 6e 85 b6 f0 3e 56 6d 31 50 96 87 3d 18 cf 5f .n...>Vm1P..=.._ +| 3616: 74 57 1a 65 b4 d2 4e 96 1c 6e 3f 5b a9 a0 47 2d tW.e..N..n?[..G- +| 3632: 22 5b 16 ab 27 f4 86 36 f7 8e a1 f2 9c 69 7f 0c .[..'..6.....i.. +| 3648: 85 4d 3c 08 3b fa 96 ee 92 af f9 8d c2 ca a9 f1 .M<.;........... +| 3664: 2b fa 90 4b a3 fb 35 c4 c6 05 c3 0f ad 5e bb f6 +..K..5......^.. +| 3680: 55 66 73 bd db 98 2f 70 7d 25 3f 05 3d 24 c6 0d Ufs.../p.%?.=$.. +| 3696: 8a e2 e1 3e 19 59 51 25 a9 e8 4a 0a 68 69 05 c9 ...>.YQ%..J.hi.. +| 3712: ce 1e 5f 96 c4 a1 98 2a 7b b9 e9 99 ce 84 af 0b .._....*........ +| 3728: 82 61 5b 97 b1 8c 2e ea 81 98 44 39 79 e0 d5 9c .a[.......D9y... +| 3744: 6a b9 14 0f 36 98 ea c8 cd 6b 66 fa d1 23 cf cb j...6....kf..#.. +| 3760: c6 1b 25 6c 42 13 b2 67 f9 68 0a 62 ff 37 ab ca ..%lB..g.h.b.7.. +| 3776: 22 60 77 c1 30 e4 59 5f 17 b4 47 c3 ea c5 c4 2b .`w.0.Y_..G....+ +| 3792: 62 44 94 08 87 03 1a a5 10 ae b1 53 cd 2d 40 5a bD.........S.-@Z +| 3808: 80 ef dd 56 6b 88 92 d7 3c d0 7a a0 2f 63 22 02 ...Vk...<.z./c.. +| 3824: 61 f1 ab 36 f9 16 f0 14 ba 67 7d 9c 7f 90 cc d9 a..6.....g...... +| 3840: b1 ac d3 1d 73 1e e8 bf 88 6a 6b 15 32 b3 b5 9c ....s....jk.2... +| 3856: 4a ad bd 4f f8 f8 b8 b8 2e 48 0f e2 e9 d6 d9 72 J..O.....H.....r +| 3872: 22 9a 28 8d 24 e2 f0 33 23 27 ff 37 5e 36 55 37 ..(.$..3#'.7^6U7 +| 3888: e1 92 78 19 a4 9d ff b9 22 e5 47 79 b8 de b4 9f ..x.......Gy.... +| 3904: 4e f0 65 1d 89 5e 55 86 bf 25 ff 6f 7e 27 f2 c1 N.e..^U..%.o~'.. +| 3920: 9d 26 69 ab 5b 1a 16 1d a1 b7 0a f0 60 06 12 4b .&i.[.......`..K +| 3936: ce 6b 17 d3 e7 66 ef 6d 83 10 30 96 49 8b 8d 52 .k...f.m..0.I..R +| 3952: 98 65 b1 48 a8 0d 96 ae 15 cd 00 5b f5 3e 4c f6 .e.H.......[.>L. +| 3968: 16 b3 15 5b cf 3e ba 15 86 7e 9b 92 be cb 8a de ...[.>...~...... +| 3984: d0 de c8 0e ac cb 79 cc 8f ad f6 3a 40 33 9f 91 ......y....:@3.. +| 4000: 48 7c 3f d1 33 c1 d3 51 ba 44 47 0e 41 8d fa 2d H|?.3..Q.DG.A..- +| 4016: 55 99 7f 4d 44 4a 85 dc 6b b1 9f 8a 4c 55 70 b7 U..MDJ..k...LUp. +| 4032: 2d 14 4d 72 ea 76 c8 0c 9f 9c 99 0a c2 7e 95 94 -.Mr.v.......~.. +| 4048: 61 84 76 8c 20 1f a3 2d 5f 54 ab 5b 8c fc 04 0d a.v. ..-_T.[.... +| 4064: 25 33 9f d1 f3 f7 38 1d 64 4c c1 0d 38 0e 4d b8 %3....8.dL..8.M. +| 4080: 61 16 f8 ea 3d 01 e9 f7 7b 6d 10 ed 08 07 86 f3 a...=....m...... +| page 12 offset 45056 +| 0: 0d 00 00 00 01 04 3f 00 04 3f 00 00 00 00 00 00 ......?..?...... +| 1072: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 97 ................ +| 1088: 3e 04 06 00 ae 7c 00 00 72 d0 44 9f 95 5b ad 14 >....|..r.D..[.. +| 1104: 76 bf ee ef 64 b3 05 0c b8 3c fa 37 1b ab 5a f0 v...d....<.7..Z. +| 1120: ae 4c 14 79 98 2a 95 49 a0 1e 3e ff 44 91 47 ad .L.y.*.I..>.D.G. +| 1136: 40 a0 7b 5e 0c 5f 86 45 28 c7 d9 03 81 2b 26 13 @..^._.E(....+&. +| 1152: 0c 38 01 db 73 e2 42 53 3c 8c d1 6c fd 51 a6 5a .8..s.BS<..l.Q.Z +| 1168: fa 65 81 fc 71 89 ea 74 84 df da ea e0 d8 40 ce .e..q..t......@. +| 1184: 56 a4 cb 65 0b 6b 79 7c 81 a9 55 79 8c 3e 02 dd V..e.ky|..Uy.>.. +| 1200: 17 7f 47 b6 84 c0 c2 a3 5e 79 66 83 e4 f2 73 6a ..G.....^yf...sj +| 1216: 46 9f 20 24 f3 4e 2b 6e a9 2c 9b 51 39 96 42 82 F. $.N+n.,.Q9.B. +| 1232: ec c0 c9 78 ec ec 90 e3 57 af fb 1f 51 41 a3 37 ...x....W...QA.7 +| 1248: b7 68 5d 84 5e 6a 9e 17 13 e1 87 7d 93 f5 92 8e .h].^j.......... +| 1264: 7a 8d 3b d4 40 35 df 0e 8f 80 13 f3 54 9c 57 a8 z.;.@5......T.W. +| 1280: 86 88 69 32 7d a2 22 13 ab 14 db e6 8a 1c 84 ed ..i2............ +| 1296: 54 4e d3 0a ba 5f 9e 66 88 2a 64 d8 04 86 3c 5e TN..._.f.*d...<^ +| 1312: 41 b7 2d c2 c1 a0 73 61 d8 80 3b 9d 9d 1e 11 ab A.-...sa..;..... +| 1328: ae 20 63 ac 31 80 2e 66 d5 a7 c8 8e 0a 55 78 53 . c.1..f.....UxS +| 1344: 66 8d 5d 1e cb 04 13 d9 a8 d1 09 4b 31 3c e9 ba f.]........K1<.. +| 1360: 83 69 76 4c 12 e8 95 84 e1 fe c0 64 74 c5 74 8d .ivL.......dt.t. +| 1376: 1f 75 28 75 1f 51 23 b7 df ef 45 66 61 1b 18 2e .u(u.Q#...Efa... +| 1392: c8 56 69 8f fa 2a 08 41 21 2d 6a c7 79 46 8f 95 .Vi..*.A!-j.yF.. +| 1408: dc b1 b2 d7 39 b9 a6 41 af 06 3e 53 85 c2 db c3 ....9..A..>S.... +| 1424: 42 fb b7 4f e9 e0 11 c1 a6 9a 48 fb d6 c9 26 31 B..O......H...&1 +| 1440: 3b c4 7a e2 bd 53 d7 3e d7 56 60 8f 6a 80 60 19 ;.z..S.>.V`.j.`. +| 1456: db fd 5a 7a 8c c7 43 c7 29 57 8b 6f 31 ea 13 af ..Zz..C.)W.o1... +| 1472: de 2e 32 75 5f 63 b7 bb 45 07 9e da 69 54 a0 fd ..2u_c..E...iT.. +| 1488: 0e dc 0d f5 42 17 eb 64 60 0d d8 56 4b 61 6d 8c ....B..d`..VKam. +| 1504: 82 09 f1 80 a2 25 5d 8b e5 67 76 13 9d 46 4a 6f .....%]..gv..FJo +| 1520: 30 f6 eb e0 7b 0e 01 e1 a8 b2 38 1b 9b 21 48 ed 0.........8..!H. +| 1536: c8 7f 7d 6b 0c 25 ce a8 0a 78 a8 48 45 2d b5 e7 ...k.%...x.HE-.. +| 1552: fd ca b2 a1 2b 2a b9 72 61 76 76 0c ae 9c b3 17 ....+*.ravv..... +| 1568: fa 83 07 24 24 62 18 18 d6 12 2d ad 58 0b b3 5e ...$$b....-.X..^ +| 1584: 80 da 40 b5 d0 e7 aa 28 52 bc 7f 68 7a 1d 5a 1f ..@....(R..hz.Z. +| 1600: 79 65 10 9e 4d fe be 32 c7 0f aa 7d a5 0a 43 0f ye..M..2......C. +| 1616: 3d 17 0e 88 a3 7f 3f ff 04 bc 79 56 62 0e 9b a0 =.....?...yVb... +| 1632: 51 1f 49 04 ff 40 7a c2 51 e8 36 44 85 63 df a4 Q.I..@z.Q.6D.c.. +| 1648: d5 63 a4 eb a3 71 44 b4 02 51 a1 2b 00 73 69 4e .c...qD..Q.+.siN +| 1664: 07 76 42 84 bc 87 1a e6 15 13 f7 ac 7a e8 bf c0 .vB.........z... +| 1680: a4 8a 64 fe 9c 17 a7 20 cb 67 cd e6 da f1 95 26 ..d.... .g.....& +| 1696: b6 79 d4 93 e2 52 fd 6f 44 29 db 1e 34 02 46 a4 .y...R.oD)..4.F. +| 1712: f1 c5 f0 b5 c3 19 1d 0e 14 d4 ba 08 1c 01 bf 39 ...............9 +| 1728: 54 02 e2 f5 d3 cf fb d1 d6 76 65 91 ba 7b c8 22 T........ve..... +| 1744: 85 f8 fa c7 ae ae 21 85 b9 d2 09 5f f4 3b 27 c5 ......!...._.;'. +| 1760: 13 39 39 7e 79 ae 07 37 26 72 07 22 01 40 2c 10 .99~y..7&r...@,. +| 1776: 5b 2c 9b bf 30 92 2e f5 12 9f 19 f9 2d 69 2a c6 [,..0.......-i*. +| 1792: 9a 26 1b de 18 b8 05 65 a8 b4 b3 3e 2c 04 5b bc .&.....e...>,.[. +| 1808: 63 79 56 7a ba 24 ae 69 ee d6 d5 7c 3d 5f 2f 7b cyVz.$.i...|=_/. +| 1824: e9 e5 55 f2 88 58 72 49 89 3e d5 91 be ae ea 9e ..U..XrI.>...... +| 1840: 58 20 45 2c da c7 b8 f9 db 97 68 59 c7 f4 d3 4b X E,......hY...K +| 1856: 95 b6 c7 73 1c 9b 96 d2 1c 56 17 eb 18 a1 fa 17 ...s.....V...... +| 1872: 05 72 44 05 4f c3 2b 8c 89 d2 3c a2 25 d8 16 74 .rD.O.+...<.%..t +| 1888: 48 b7 78 36 7a 86 88 ff cc 47 ac bd 73 28 1f 3f H.x6z....G..s(.? +| 1904: 13 3d b9 d2 df 9a 93 43 a6 9f b9 fc 7c b2 d4 84 .=.....C....|... +| 1920: c4 7e d0 14 60 01 63 fb 09 de a3 2f 1c ae 2e 6b .~..`.c..../...k +| 1936: 9f 8d c8 f1 2d f0 c9 a5 1f 48 c1 7c 53 c5 63 8a ....-....H.|S.c. +| 1952: 9b 0b fd f3 7f 3a 63 eb 2c 4f df 3a 57 8c 20 3e .....:c.,O.:W. > +| 1968: 0b d1 00 0f ce e3 ab a8 77 31 63 4a aa 35 5f 3f ........w1cJ.5_? +| 1984: 77 ba d3 38 7b a3 53 94 e6 9d 34 ec 5a 28 09 6e w..8..S...4.Z(.n +| 2000: 5a 85 bc 72 18 bb 15 8a 20 01 f3 88 19 74 65 1b Z..r.... ....te. +| 2016: 51 bc 8a 4d 32 a6 00 80 fc 06 f8 aa 1c ee 0e 84 Q..M2........... +| 2032: b5 70 fe 09 02 78 d4 ae 3a bb 02 ed 5a 90 d2 a9 .p...x..:...Z... +| 2048: 3e 58 8a 08 2d 4c 79 7c f2 94 89 ac 04 59 d8 17 >X..-Ly|.....Y.. +| 2064: 09 e4 c3 e0 78 6b 77 58 64 4f 2f 77 15 16 c2 41 ....xkwXdO/w...A +| 2080: 96 61 8c 23 59 73 b9 56 c3 4e da 79 34 94 e5 31 .a.#Ys.V.N.y4..1 +| 2096: 9b a6 92 ca 23 64 e4 78 32 e2 9d 2e ad 18 11 b7 ....#d.x2....... +| 2112: 12 0d 1b 40 44 4f 92 cf 78 80 86 fb af c9 30 20 ...@DO..x.....0 +| 2128: 52 9b 22 8f f4 ca 65 e5 f2 e7 a3 c3 e7 00 ee a1 R.....e......... +| 2144: ca ab 3d 71 40 bd cf c3 2f 7b 59 e7 9d 18 6a 65 ..=q@.../.Y...je +| 2160: a1 c9 84 cf 0f f5 36 d5 22 06 d4 6a b6 ad 38 03 ......6....j..8. +| 2176: b4 ac 9e c4 7a b0 71 24 a9 01 f0 5c 5b e3 c9 bd ....z.q$....[... +| 2192: d6 e7 ba 71 fd 44 22 14 af f7 18 ef 29 1d f2 0f ...q.D......)... +| 2208: 5d e1 3a 71 3b 47 44 da 21 f6 73 8b 6a a1 ff 93 ].:q;GD.!.s.j... +| 2224: d5 eb 5d d0 61 23 41 53 99 7f 23 0f c0 a3 18 3c ..].a#AS..#....< +| 2240: bd 30 e0 bf 16 41 27 71 80 67 49 61 e9 64 71 e0 .0...A'q.gIa.dq. +| 2256: 9d 42 e6 48 9b 0e 40 82 f9 e2 a5 b4 51 37 7d 6b .B.H..@.....Q7.k +| 2272: 74 54 03 bb a2 93 ac 47 f8 a5 75 86 73 0f 12 47 tT.....G..u.s..G +| 2288: 27 65 16 16 a2 ab 99 08 14 0b 7e 1b 2c 7e 9d 20 'e........~.,~. +| 2304: 58 23 14 70 bd f5 7c 2b 74 ce d1 46 b3 29 03 8f X#.p..|+t..F.).. +| 2320: 76 f3 20 ca 9e a3 fc 1a 17 f2 8e 78 97 4e 0b 5e v. ........x.N.^ +| 2336: 61 2b e1 3d f1 14 ae 9a 1f e8 a8 3c 99 02 a5 c0 a+.=.......<.... +| 2352: a6 ee bd 47 a4 3c 6d db 6c 20 dd c4 60 09 17 e7 ...G.h$^0....z..... +| 2464: 89 df 3b c6 e1 09 1d 16 69 40 e9 d7 f7 41 d3 2b ..;.....i@...A.+ +| 2480: 52 6a 9a d9 65 e2 44 de 45 63 be 85 41 b8 d4 4a Rj..e.D.Ec..A..J +| 2496: 49 b9 b8 bb 4d a6 6e 60 46 13 6f 7f b1 57 9c 5b I...M.n`F.o..W.[ +| 2512: 78 69 87 3d 42 e5 c2 0a 46 9c 38 9d e2 44 80 fe xi.=B...F.8..D.. +| 2528: c8 81 f4 fe bb ef 61 46 88 c7 6f 1a f8 00 0b 63 ......aF..o....c +| 2544: d0 ac 8e 5e 88 8d 2d 56 15 79 d2 12 d9 94 ba 2b ...^..-V.y.....+ +| 2560: ae 71 63 be 62 08 75 b9 97 fc 1e 17 89 83 3b 30 .qc.b.u.......;0 +| 2576: 07 6e f0 db 44 17 c0 49 7f ed 0a 0a 43 13 6a 72 .n..D..I....C.jr +| 2592: 78 9c 96 52 2c 02 ac da a0 90 b5 66 34 b7 a2 17 x..R,......f4... +| 2608: e7 71 11 8d 8f 22 0e 40 90 9b 0f 99 f5 10 c0 64 .q.....@.......d +| 2624: ac e3 24 0d 49 1a f4 2f 0b 13 fc 94 a7 18 b1 5f ..$.I../......._ +| 2640: a6 64 49 0b c2 35 99 b6 05 81 bd a8 f8 88 56 83 .dI..5........V. +| 2656: 17 56 cc f8 91 db 5b 18 7c 42 46 3f 3d 9a 2d b6 .V....[.|BF?=.-. +| 2672: 63 8c 62 bf 78 53 a0 23 53 40 c0 32 06 f1 c8 3a c.b.xS.#S@.2...: +| 2688: f8 17 34 a7 29 1e ab 92 2d 2d 68 c5 83 e0 0a 2e ..4.)...--h..... +| 2704: 7b f8 9c b7 32 86 c0 1e f5 29 44 a6 24 e1 d7 66 ....2....)D.$..f +| 2720: 0d 48 2b 0e 49 f8 e4 52 4a 7d bd 1c c1 44 27 f0 .H+.I..RJ....D'. +| 2736: c9 db 87 93 13 62 82 ef ad 2c ea 8f d1 3d a4 a6 .....b...,...=.. +| 2752: b8 80 87 e2 0e 27 27 b3 3d 56 66 39 de e8 21 5f .....''.=Vf9..!_ +| 2768: 95 25 d9 68 f1 57 50 0e 15 54 0b a6 44 27 e8 d9 .%.h.WP..T..D'.. +| 2784: f2 dc 5e 79 f0 ec 2b a3 39 77 8f 3d 53 70 8a d3 ..^y..+.9w.=Sp.. +| 2800: e5 aa 14 cb 7b dc 31 72 f6 90 5e 8b 3a 8c f3 77 ......1r..^.:..w +| 2816: 4d 00 a3 1d 3a 63 47 c0 2a a2 32 98 6e 5c bc 21 M...:cG.*.2.n..! +| 2832: f0 6a 34 6c 89 a5 bc 04 f6 3b 8c 96 b0 eb 0d 70 .j4l.....;.....p +| 2848: 9f 18 d5 64 ec 2e df 19 7d 1d a4 61 48 e7 0b eb ...d.......aH... +| 2864: f3 94 16 f5 7f 4c 9e 0c 78 aa 4b 61 14 13 eb e2 .....L..x.Ka.... +| 2880: 72 14 56 27 41 70 8f 7f cb e6 a1 c3 37 c4 78 32 r.V'Ap......7.x2 +| 2896: 85 ea e5 af 96 46 4b c3 93 af 8f 26 0c ea 08 b6 .....FK....&.... +| 2912: b4 a6 a4 78 6d 82 51 ab fb e5 a9 e9 89 c3 a1 be ...xm.Q......... +| 2928: 3e b9 e3 8c 61 cf 42 1d de 25 45 9e f0 ff 8b 75 >...a.B..%E....u +| 2944: 63 9b 3d d6 92 c3 ad ca c5 4c 79 9d 72 37 fd 3a c.=......Ly.r7.: +| 2960: 21 f2 8d 34 37 b9 eb a0 86 d8 1a f2 9d aa 6a 53 !..47.........jS +| 2976: f6 c9 29 d0 39 2c 66 24 c6 8e 68 9c 70 cc 9e cc ..).9,f$..h.p... +| 2992: 25 a8 dd 5e d8 e6 87 1b dc 3a 26 20 e3 1c 3b ba %..^.....:& ..;. +| 3008: f8 c4 80 83 79 49 f1 2a f0 e8 70 31 e7 b2 a5 e3 ....yI.*..p1.... +| 3024: 9f 5b 49 25 2c 33 b6 ea 38 ab a8 0a 9a a2 0e fd .[I%,3..8....... +| 3040: 3a a8 9c 0f ab 93 45 a7 54 d2 18 2f 2e 94 34 cc :.....E.T../..4. +| 3056: fa 14 b6 8e 01 8d af 80 94 e7 80 31 dc 2c b9 1e ...........1.,.. +| 3072: dd 35 91 29 28 a0 29 65 bc e3 a2 52 b0 e5 56 5a .5.)(.)e...R..VZ +| 3088: 12 21 06 fd f3 04 77 31 6d f2 e0 66 0a a3 77 f3 .!....w1m..f..w. +| 3104: 5a 17 37 59 65 d9 32 68 82 2c 72 05 16 e9 9a 89 Z.7Ye.2h.,r..... +| 3120: 10 94 53 e9 be 63 2e 82 27 28 30 1d 2a bf ad 00 ..S..c..'(0.*... +| 3136: 49 9a a7 e3 50 d0 61 b4 bd 73 3e 99 a3 a7 69 7f I...P.a..s>...i. +| 3152: a8 4c 4d 12 7f c0 55 b7 de 0c 93 5e 27 cf 4a 53 .LM...U....^'.JS +| 3168: 78 08 ee da 22 37 3f 94 2f dd a6 b2 28 84 0f 62 x....7?./...(..b +| 3184: bc 7f be 3c af ad 84 82 70 b2 98 d5 9a d5 07 34 ...<....p......4 +| 3200: 41 6f 9c 0a 17 54 cf fc 43 1e 7c ca 48 6f 23 c5 Ao...T..C.|.Ho#. +| 3216: 50 7b b6 ec e5 3a 27 0f 65 29 97 ad 37 b1 9f e6 P....:'.e)..7... +| 3232: 6b d1 d4 41 ed 5e 0e 20 22 00 ca 41 58 d2 b9 73 k..A.^. ...AX..s +| 3248: 08 2d 01 83 db d1 57 a2 e3 d2 34 c6 73 fd 2e b2 .-....W...4.s... +| 3264: 9b f2 a4 a4 f1 1d 9f 7a cc d8 93 37 b3 95 2c 99 .......z...7..,. +| 3280: f5 92 0c 91 d5 e2 2e b2 bb 49 76 19 c9 58 16 28 .........Iv..X.( +| 3296: 1e 76 60 ba d9 a8 04 e9 91 5e 7f e6 b1 dc de 9f .v`......^...... +| 3312: 6f e6 52 f2 61 53 7b 09 f4 df c4 96 15 23 99 67 o.R.aS.......#.g +| 3328: 6b b8 e8 04 05 62 0d 5f 05 d1 8a 6c 49 e8 2b 71 k....b._...lI.+q +| 3344: 10 44 8e 93 13 bc 27 14 32 52 22 2d 11 cf cd 43 .D....'.2R.-...C +| 3360: 77 a1 60 21 70 44 60 cb 64 9e b4 c6 23 aa f2 0d w.`!pD`.d...#... +| 3376: c0 6d 50 0b d9 73 d0 eb 6d d5 03 fc 74 e1 fb 49 .mP..s..m...t..I +| 3392: a1 18 4f bb a2 2f 09 54 28 80 8c 04 86 3d e1 fd ..O../.T(....=.. +| 3408: ff 55 c9 7a a0 35 ef 64 b8 87 1b 10 8c fc 3d 27 .U.z.5.d......=' +| 3424: e3 6c 30 3c 05 4d b3 37 b8 29 ba a2 46 9b 76 5d .l0<.M.7.)..F.v] +| 3440: ff 1b 30 65 70 57 f2 89 c3 17 3f 21 5d 26 5f 58 ..0epW....?!]&_X +| 3456: b1 50 6e 75 c3 dc be a9 0e 43 3e 34 4e 40 0e a1 .Pnu.....C>4N@.. +| 3472: f4 69 6d b1 56 a7 0f 09 7e 25 74 08 83 bb 9d 67 .im.V...~%t....g +| 3488: a6 f6 68 87 e9 d9 ba 16 97 42 ca 66 29 e6 df cd ..h......B.f)... +| 3504: e8 e4 ef ef c9 1f f7 d8 9c 8a a3 24 c9 43 60 4d ...........$.C`M +| 3520: 9a 73 cb 73 9f b2 b1 fe 51 9d 55 9f 37 a0 d2 3c .s.s....Q.U.7..< +| 3536: 7e be e4 35 1e 8e 81 f5 67 29 29 ac 95 0a 99 28 ~..5....g))....( +| 3552: ec 4c f2 c6 03 6b 01 76 f5 87 77 58 51 51 40 d4 .L...k.v..wXQQ@. +| 3568: 81 c7 d7 7b 40 09 8f f5 c8 a7 49 00 c9 ee 99 c9 ....@.....I..... +| 3584: 6f 4a c5 6f e7 51 8e 4c 95 a3 de 6d 53 45 7b 97 oJ.o.Q.L...mSE.. +| 3600: 7b 7f 8b b4 db f7 52 97 08 2e 4f 36 41 24 79 bd ......R...O6A$y. +| 3616: 54 bb b8 2c 32 6c 1f 54 e6 82 47 27 53 7f 57 ea T..,2l.T..G'S.W. +| 3632: 4c 10 71 9b b2 0d 9a 80 fb d2 a6 ec 8d 6e 60 64 L.q..........n`d +| 3648: 1a 73 43 13 b9 cb b1 bd da 84 82 8d 17 ae 75 4d .sC...........uM +| 3664: 1a af 28 9d 01 6d c7 f7 25 8c b5 ef 1b 5e 33 86 ..(..m..%....^3. +| 3680: e7 ce 59 c0 5b 3d e2 46 ab 85 3b b2 c0 9e 7a 75 ..Y.[=.F..;...zu +| 3696: 26 15 cd 6e 6d c2 19 fc cb 3e 5c 5e 95 94 ec 29 &..nm....>.^...) +| 3712: 94 8e d4 e6 0a 38 8c ef fc eb cb 3a 06 97 20 f3 .....8.....:.. . +| 3728: 5f 53 b5 5f 40 33 9e 41 9c db a8 f3 7a d8 aa 0e _S._@3.A....z... +| 3744: a3 37 00 87 ff 13 78 1a 98 c2 b2 aa bf 2d 10 24 .7....x......-.$ +| 3760: 8e e5 9f 9f 3c 4f e4 90 e0 6a 90 44 32 9f ca 6a ....9=J.4.... +| 3888: 9c 65 c7 29 02 bf a6 97 61 3c 6f 46 48 0a e5 a6 .e.)....a7~. +| 3952: 43 f8 b7 ae 2e af d4 9d 67 8d fa c0 24 b2 1c 55 C.......g...$..U +| 3968: ea aa 33 67 22 d4 f3 e7 87 a5 9d fe a3 04 55 ae ..3g..........U. +| 3984: 56 87 2d 21 a9 02 c2 4d a3 88 a3 3f 48 18 83 a7 V.-!...M...?H... +| 4000: 01 b9 6d 60 d4 0f 79 79 8a 4b 66 c3 49 c7 51 0b ..m`..yy.Kf.I.Q. +| 4016: 15 73 df f2 53 eb 79 03 da 55 2f 5d 9f 8f 01 a7 .s..S.y..U/].... +| 4032: c7 5d cf 61 f0 c2 78 52 56 b8 45 7d f2 d8 56 ca .].a..xRV.E...V. +| 4048: 80 84 6e ee 2a 00 ba 00 74 46 0f 76 23 76 79 55 ..n.*...tF.v#vyU +| 4064: 4f 97 57 f2 63 07 11 1f a3 46 07 14 3b 50 43 c0 O.W.c....F..;PC. +| 4080: 72 b2 89 5a 44 15 dc c8 9b 2e f8 ad 69 80 3e d7 r..ZD.......i.>. +| page 13 offset 49152 +| 0: 0d 00 00 00 01 04 3f 00 04 3f 00 00 00 00 00 00 ......?..?...... +| 1072: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 97 ................ +| 1088: 3e 05 06 00 ae 7c 00 00 40 9e 86 c7 f4 d7 a0 78 >....|..@......x +| 1104: b9 63 d2 14 9c 0c 6a 77 07 99 fe 03 10 f7 fd 07 .c....jw........ +| 1120: ea be 72 a1 24 46 49 15 05 16 1f 1c 80 72 1b 98 ..r.$FI......r.. +| 1136: e5 f9 d0 65 50 cb 29 09 d4 2b 17 e2 73 7e 45 4f ...eP.)..+..s~EO +| 1152: 05 c8 b5 bb 1a 81 34 52 32 17 53 96 c8 0d 54 19 ......4R2.S...T. +| 1168: bd d8 c3 af 35 79 a8 75 ae 2d 5d c4 44 eb b2 1f ....5y.u.-].D... +| 1184: ab 9d f6 d8 42 7b 3b dd 10 f4 34 95 57 ff ec cc ....B.;...4.W... +| 1200: 63 3f d3 93 e5 16 6d b5 3d e3 23 7b 32 88 b7 ce c?....m.=.#.2... +| 1216: 7b 9a 8c 46 26 6f ed 2f a7 83 fb fd a7 b7 1b 48 ...F&o./.......H +| 1232: 88 40 20 a5 9b de 7b d9 32 e5 72 1c 85 8a a2 63 .@ .....2.r....c +| 1248: e2 8d 7b 25 a2 13 51 ec fd 0a ed 1b 76 8f 9d 62 ...%..Q.....v..b +| 1264: 2e 9e cf 0f df 58 30 83 e5 90 40 df d2 9f c9 65 .....X0...@....e +| 1280: 3f ae c6 4a 11 a3 16 5b 6f 24 74 95 5e 21 6a c8 ?..J...[o$t.^!j. +| 1296: da 7a dc 82 c2 ab a0 93 c8 dd a8 f1 1d 78 5e 9b .z...........x^. +| 1312: c8 17 c7 e0 4f 53 1d e1 5a 5f 5c bf 23 25 fb f3 ....OS..Z_..#%.. +| 1328: a0 d4 f5 f7 16 78 f8 00 2e 49 1f 7b ff 41 5c 11 .....x...I...A.. +| 1344: 83 9e c7 f4 f5 c7 85 d3 97 dd 67 5a 1e 47 d0 41 ..........gZ.G.A +| 1360: ae cb 4c 47 4f 5f fc 9f bf 0b 20 04 94 08 11 8b ..LGO_.... ..... +| 1376: a8 98 f2 5e 86 19 3b f1 0f 9d 49 c8 02 14 68 24 ...^..;...I...h$ +| 1392: 20 71 28 49 3c 60 5a 92 ea d3 2c cd 8a 8a d7 cb q(I<`Z...,..... +| 1408: fd 14 db 41 3c d0 24 89 46 6a c4 52 8d f0 c0 dd ...A<.$.Fj.R.... +| 1424: b4 a6 0f bd c2 5a fd d1 53 bb a9 82 88 64 4a 34 .....Z..S....dJ4 +| 1440: 0f 8f ed 49 49 81 03 29 c3 c3 01 00 6a 83 21 dd ...II..)....j.!. +| 1456: bc 98 29 fb 2b 5e 78 95 4a 57 4f a7 d3 ec 44 a8 ..).+^x.JWO...D. +| 1472: 97 6c 7f 39 11 6f aa a0 60 b1 71 43 a6 45 ea 9b .l.9.o..`.qC.E.. +| 1488: e1 6f 67 2b f9 e4 8a 55 60 5b 69 f3 54 bf 5c 78 .og+...U`[i.T..x +| 1504: 32 d4 25 22 bf 9b 7a a6 a8 a3 70 53 7d e8 50 1c 2.%...z...pS..P. +| 1520: cd 2a 4a 92 a7 f1 e4 84 39 8e 72 a4 cf 3a c8 b2 .*J.....9.r..:.. +| 1536: 3e f1 06 f2 83 80 0c 1c 10 91 7b 41 92 f5 df 0f >..........A.... +| 1552: 7a dc 04 78 3c b4 cd 96 b6 15 f6 7f 85 33 db 09 z..x<........3.. +| 1568: 92 de 83 ef fc 2c 75 43 49 0c 12 61 48 14 38 d3 .....,uCI..aH.8. +| 1584: b4 fb 43 33 74 3a 0a 5b c5 03 0a d8 f2 2c 1d cf ..C3t:.[.....,.. +| 1600: e9 a0 7f be 25 24 90 77 75 2f 15 a7 ff 62 83 ef ....%$.wu/...b.. +| 1616: 01 04 b1 7c c8 5d 03 5a 28 1d a3 24 e9 7f 32 73 ...|.].Z(..$..2s +| 1632: 19 59 8c 07 30 10 be 58 c2 48 f6 d1 c6 3c b3 09 .Y..0..X.H...<.. +| 1648: e0 bf 5c c3 47 81 c8 04 71 ab 55 cb a5 7c 7a a8 ....G...q.U..|z. +| 1664: 45 28 03 75 e9 82 51 e8 bd 41 d4 1e cc b1 fe 43 E(.u..Q..A.....C +| 1680: 4d 5c 66 25 6f fa da a6 fa ea d3 de 0a 2e 5a 3d M.f%o.........Z= +| 1696: 68 65 10 b6 ee 3c 0b f6 13 bf 3e 6e 57 f3 30 43 he...<....>nW.0C +| 1712: f6 f4 e0 34 e5 47 88 73 9d 9c 36 f0 90 98 0e 5a ...4.G.s..6....Z +| 1728: 3b ab 0c 7f 8b ab ea 9c 8c 5e 34 26 d7 53 bb cb ;........^4&.S.. +| 1744: 18 58 a7 6f bf 78 43 7b 47 a6 3b 09 f7 9f 5f 81 .X.o.xC.G.;..._. +| 1760: 87 c0 17 02 8f ad 78 32 8c dd 96 0e d8 b8 66 66 ......x2......ff +| 1776: 23 91 e3 e3 37 5c 63 f4 16 c1 58 4f b7 63 de 7c #...7.c...XO.c.| +| 1792: 36 c3 67 fc 32 a3 d8 84 20 fe e6 0f 2e 2d ee 11 6.g.2... ....-.. +| 1808: 3b 97 1a 05 47 ea a1 71 c4 11 08 b1 a9 52 b7 54 ;...G..q.....R.T +| 1824: bf 4c 06 dd f5 a3 ec c2 ce eb c3 82 e0 5f 5c c1 .L..........._.. +| 1840: 51 9c e9 d7 d1 45 a6 ea 62 dd 3b 25 79 0a 5e ed Q....E..b.;%y.^. +| 1856: 5a 9d 81 8e 7c a8 fd 60 31 88 80 f3 df de 60 23 Z...|..`1.....`# +| 1872: 4e 07 7a 02 dd 89 7d 2c bd ce fa 45 ff c6 05 f4 N.z....,...E.... +| 1888: ff 2d 18 f5 13 37 c8 d8 be e9 6d 81 da 8f 03 e4 .-...7....m..... +| 1904: 82 0a c3 3b 19 10 de 4a d8 12 37 37 ce 51 4d 41 ...;...J..77.QMA +| 1920: 0a ad 6a 1f c2 80 49 84 15 7d d4 f0 53 5d d1 b1 ..j...I.....S].. +| 1936: b3 c7 27 34 2f bd 05 af df 5c 5f 86 90 55 99 e5 ..'4/....._..U.. +| 1952: 4a 9f 86 34 30 b1 fe 0d 14 2c 1a 85 28 0e 09 5e J..40....,..(..^ +| 1968: d0 31 99 e7 8c ff 6e f9 e6 a5 cb fd 85 9b f7 31 .1....n........1 +| 1984: a0 07 f4 cc ca 5d c2 37 f9 a3 6e d2 5b 2e 04 32 .....].7..n.[..2 +| 2000: 19 03 7a ee fa 9c 5c 68 89 f7 94 b9 19 fe 05 d2 ..z....h........ +| 2016: 19 f8 3b fd cf dd 6f 23 3f c3 28 15 8a 8c 86 4b ..;...o#?.(....K +| 2032: 8a ff ed ca b7 6a 69 4a fc 66 94 a0 ea b7 fa f0 .....jiJ.f...... +| 2048: b1 3b 67 85 2b 82 e4 7f 3b 8d da 02 9e 27 fb 69 .;g.+...;....'.i +| 2064: 7b 20 c6 f6 fb c2 18 fa d5 6d 60 22 be 01 93 28 . .......m`....( +| 2080: 08 71 9f b2 b0 e6 79 94 91 b6 b8 b0 ff a7 3f d8 .q....y.......?. +| 2096: e3 eb b8 8f c7 02 5b f1 35 b7 15 db 01 cd c9 f6 ......[.5....... +| 2112: d9 83 36 2f d0 8b 43 66 e2 3a 14 34 70 1e 68 e3 ..6/..Cf.:.4p.h. +| 2128: e3 a7 fd 7f 51 cb ec 60 ca fb 13 fc 11 56 97 46 ....Q..`.....V.F +| 2144: 23 53 0a aa 8d 15 de d4 cd 2b 1b de 3f d5 57 9c #S.......+..?.W. +| 2160: e5 91 3b 15 98 3e 09 d9 4e d9 25 14 01 32 fc 23 ..;..>..N.%..2.# +| 2176: 38 60 b2 8f 00 f3 21 8b 20 be 8f 6e 02 9a 58 3c 8`....!. ..n..X< +| 2192: a7 a7 6f 3e 24 cc 1f 87 ab e6 12 0f bd 0a 8b 38 ..o>$..........8 +| 2208: 17 b2 ee 80 80 bb 50 df e0 cb 72 0a 98 07 40 bc ......P...r...@. +| 2224: 8d 77 86 27 e8 07 57 4b 50 84 4c b4 73 b4 32 3d .w.'..WKP.L.s.2= +| 2240: 89 61 e8 b8 dd bf 22 c8 06 07 e6 8f 6d 54 c7 e3 .a..........mT.. +| 2256: 12 f9 1f 61 cb e2 40 e1 14 34 9a 2b 16 a7 1f ec ...a..@..4.+.... +| 2272: 85 b8 76 8e 25 59 51 5a 19 82 d9 54 7f 3d 11 05 ..v.%YQZ...T.=.. +| 2288: a6 4c 43 28 9c 65 44 5e 57 fe 04 84 ae 7f 26 5b .LC(.eD^W.....&[ +| 2304: ed 26 e2 1c 07 1f fa f1 99 7b 69 f0 03 16 bf 7c .&........i....| +| 2320: 13 16 75 80 d1 14 17 fa c9 f4 3b e5 9a 06 c0 d4 ..u.......;..... +| 2336: c2 55 53 0a 78 32 86 3c 23 a4 f8 b6 5b d4 9a 51 .US.x2.<#...[..Q +| 2352: 7c c3 1b 56 b1 e7 cd 83 02 75 8c 5e e3 6b b3 d3 |..V.....u.^.k.. +| 2368: 03 23 21 75 3f 78 a7 0f 66 87 2e 85 37 6b bc 0e .#!u?x..f...7k.. +| 2384: 2f 27 cd da 3c 1e 1a 35 2b bc 2d 97 ec 55 df 22 /'..<..5+.-..U.. +| 2400: fd 19 04 fa be 12 2a 1f 4b ed e3 0d 87 f0 61 db ......*.K.....a. +| 2416: 06 1f 86 2f 9d 80 32 97 df 70 ff b4 78 df 95 7d .../..2..p..x... +| 2432: a3 db a5 d0 db a0 b9 ed 4f a7 63 14 bf 8b 99 fd ........O.c..... +| 2448: 6e 1b d0 02 07 93 e1 be 56 4e d5 c1 41 a6 f0 ea n.......VN..A... +| 2464: 37 aa 21 7c 51 2f 5b c4 a0 ae ba e8 a7 1d c6 a5 7.!|Q/[......... +| 2480: 1e 8b 3b 86 7b f2 ea 57 b7 2f 88 37 5e 7a 89 e6 ..;....W./.7^z.. +| 2496: 09 c0 e4 3b aa bf 78 41 7c 00 14 cc 37 f1 1a f6 ...;..xA|...7... +| 2512: a5 79 49 f0 5a e0 1e 5d ba 33 ef fe 89 6e 20 01 .yI.Z..].3...n . +| 2528: 96 70 7d 58 0a 98 e0 c4 3e 20 b2 46 83 b8 1f c9 .p.X....> .F.... +| 2544: fe 78 8a 41 a9 ca 3f 2c c1 d4 35 a3 39 85 4d a1 .x.A..?,..5.9.M. +| 2560: 23 5e c7 b5 79 75 00 e7 d5 fb 30 99 00 c4 d0 31 #^..yu....0....1 +| 2576: 80 68 f2 80 1b 5e c8 30 f6 36 e1 a1 b5 87 92 35 .h...^.0.6.....5 +| 2592: 6b 9c ac 39 39 3f 54 e9 4d b4 8b bd 09 4c 6f 9a k..99?T.M....Lo. +| 2608: 9a 6d 65 1a 7a 2f 98 0f 33 32 3a 4c bd d6 c3 d5 .me.z/..32:L.... +| 2624: 2b 3f 7f 0a d7 f7 f0 05 88 5e 36 30 4d a2 0a 65 +?.......^60M..e +| 2640: f6 a9 5b 2d 5e 3f 0e 13 c9 45 1d c9 28 19 6d ba ..[-^?...E..(.m. +| 2656: ff 9d af 10 17 b2 49 34 d8 a1 c8 ce d2 b7 99 3e ......I4.......> +| 2672: 7c c6 f5 d6 74 84 05 84 2a bb 2d 25 27 67 04 f0 |...t...*.-%'g.. +| 2688: b3 6d 0a a9 43 f6 c4 51 71 1d 48 8d b3 a4 47 ed .m..C..Qq.H...G. +| 2704: d6 14 c3 27 3e 6a e9 64 ef d7 af fb b0 eb ec 3c ...'>j.d.......< +| 2720: bb 92 77 32 5e 4f 20 6d f7 a4 94 ce d9 9b c5 78 ..w2^O m.......x +| 2736: 84 f0 c3 1a ca d8 e2 ce 76 0a 5e f8 73 9d 5a 82 ........v.^.s.Z. +| 2752: 1e f5 0a 43 41 77 b9 d1 17 ef 47 0f 04 7b 5c b9 ...CAw....G..... +| 2768: 3d 96 ec 7c b6 45 7d f9 fe 46 cb d2 50 a3 7f 99 =..|.E...F..P... +| 2784: 16 ef 8d 2d d5 55 30 d8 27 18 bb f9 fa 76 80 55 ...-.U0.'....v.U +| 2800: 13 1e f3 c2 dc 11 ef 8b 5d 67 d2 cd 70 74 b0 93 ........]g..pt.. +| 2816: f9 51 ec 35 24 cc 01 55 9f 92 a5 10 7f 66 07 e9 .Q.5$..U.....f.. +| 2832: f9 0a 32 06 db fd a6 ed 3e c6 b4 55 41 45 6c d3 ..2.....>..UAEl. +| 2848: da a9 57 98 b1 79 3e 3a 6a e3 c4 37 3c 81 ba 16 ..W..y>:j..7<... +| 2864: 3b 6a 3b 29 93 80 50 6d 94 05 dd 6b 37 10 f7 5c ;j;)..Pm...k7... +| 2880: 5a 23 ef 13 1b 49 a5 fb 7b e8 49 a8 2a f4 e2 f4 Z#...I....I.*... +| 2896: 0b a6 28 03 5b 3e ce c0 f6 b4 d3 c3 b5 ed 2f 87 ..(.[>......../. +| 2912: a3 66 13 93 45 50 d5 3d 59 e4 42 fa f7 00 d8 b8 .f..EP.=Y.B..... +| 2928: 7a 3a cc ce fc d2 8d a5 e9 1b 63 f8 21 6e 4f a3 z:........c.!nO. +| 2944: 0e 17 92 56 d6 2e c9 a4 b8 07 2f 25 9e 3b ff a7 ...V....../%.;.. +| 2960: 9d 31 56 43 85 d6 ea 91 ab 29 8c 1e bd 29 f2 9c .1VC.....)...).. +| 2976: 9c 48 81 a2 a9 da 80 c8 b6 8a 67 48 1d c0 02 4a .H........gH...J +| 2992: fa a1 e3 69 15 83 c5 ca 9d 96 43 06 ad 97 c3 5c ...i......C..... +| 3008: 09 e0 de b9 0d 4e f5 99 75 e1 c0 98 20 91 c3 5d .....N..u... ..] +| 3024: 93 81 0f bf 2b 96 96 c0 9d 30 d3 84 c0 9e 60 56 ....+....0....`V +| 3040: 08 0a 3d 48 be 6b 3d 32 c4 5b e8 87 02 88 f6 f3 ..=H.k=2.[...... +| 3056: 71 71 07 f8 66 42 8d df 4e aa 44 6f ec 3a b8 7e qq..fB..N.Do.:.~ +| 3072: 4d b3 83 74 b4 bf 34 d2 2c 95 3c 44 9b c1 cc 9a M..t..4.,.+......6..#. +| 3120: 1b f5 ce 42 b0 0c 11 d4 69 a4 bf fa 4b 30 34 b4 ...B....i...K04. +| 3136: 52 48 84 cc 31 08 03 a9 93 d9 e5 c8 44 cd cf fe RH..1.......D... +| 3152: e4 2e c5 3a c2 b2 d9 bc 15 4d 7f 7a 81 52 9c 53 ...:.....M.z.R.S +| 3168: 3e 1a 74 78 c8 38 4a 02 ce 6b 96 2d 9c 3b 74 41 >.tx.8J..k.-.;tA +| 3184: 50 06 d9 6f fa c8 98 05 f5 70 c3 7f ca 0a 78 f4 P..o.....p....x. +| 3200: 01 f1 a2 f5 46 8a 02 b8 c3 37 7e cc a0 87 b0 fc ....F....7~..... +| 3216: 32 e5 86 6d 95 22 67 fd 10 56 c4 78 52 c5 12 e5 2..m..g..V.xR... +| 3232: e9 f2 b2 fa f6 78 2e a3 82 a6 73 1b 9f 96 a7 e6 .....x....s..... +| 3248: 81 49 e9 17 f1 0a e6 99 21 ce ec ff be aa 43 e6 .I......!.....C. +| 3264: ea 6a ac 2b a4 b9 0b 48 6c 81 1a 26 bf 93 69 36 .j.+...Hl..&..i6 +| 3280: 53 1e 46 2a 43 af cc ef 96 4e 0b 32 38 93 10 d5 S.F*C....N.28... +| 3296: d8 6f 88 e7 f9 92 f8 37 28 51 55 98 fa 1a 89 80 .o.....7(QU..... +| 3312: 35 d5 d1 90 f1 91 8f 4a 5a 81 2f 79 a7 e4 80 3c 5......JZ./y...< +| 3328: a2 5e e5 b3 f8 d3 56 ec cd 02 73 0e af 8d d8 81 .^....V...s..... +| 3344: 26 7b 4d 96 f1 b3 75 7a de c3 b8 a6 b6 75 f2 5c &.M...uz.....u.. +| 3360: 97 08 a3 50 9b d8 cf 43 c0 90 f7 52 da 87 8d 12 ...P...C...R.... +| 3376: 1c d4 0a de 56 26 3d af d1 d5 03 ff d8 62 a1 c5 ....V&=......b.. +| 3392: f2 7c ea 1d 39 3f f0 e1 5f cb 3d db 1a 8a a1 b7 .|..9?.._.=..... +| 3408: 10 01 f1 c6 17 53 bc 83 eb 2a c4 cf 3a 04 b1 8a .....S...*..:... +| 3424: 6d 80 20 be 7b 72 6c c9 be 4e eb f4 cd c8 60 d2 m. ..rl..N....`. +| 3440: 1c 46 b1 8a e5 9b 66 22 4f 05 bd c6 37 34 67 2d .F....f.O...74g- +| 3456: b1 c5 bb 60 de 4b c4 3a 29 14 dc de 38 9c 0d 09 ...`.K.:)...8... +| 3472: bd 78 2f f6 e4 34 18 9b 51 41 a7 57 f9 f2 ad 0d .x/..4..QA.W.... +| 3488: 82 70 5f b4 fa 84 19 5c 72 29 8e 5f b2 8b fa 2a .p_.....r)._...* +| 3504: e1 d1 55 bf b8 45 c3 79 e0 04 62 10 1f 76 79 de ..U..E.y..b..vy. +| 3520: 84 93 a8 df 92 8f 41 0d 38 14 70 b3 dd 9d c6 36 ......A.8.p....6 +| 3536: f6 12 72 ee 23 e6 ba ed 97 1f cd 36 55 ac 32 98 ..r.#......6U.2. +| 3552: 78 12 55 23 d5 64 ad ed cf 32 d3 2d 7b 51 d7 7f x.U#.d...2.-.Q.. +| 3568: 3c 6f 0f fb c7 06 75 96 ef 64 ce c4 09 15 4f b8 ....|....c/.... +| 1104: f2 f2 37 ff aa 18 0f 69 94 cc 49 85 cd f7 bb 7b ..7....i..I..... +| 1120: e5 c0 09 d1 a2 78 f4 c0 1b 23 e7 d5 ac 2d 85 27 .....x...#...-.' +| 1136: c2 48 c2 80 ad 93 a0 10 44 98 5a 6e 9e f1 92 a9 .H......D.Zn.... +| 1152: 98 f9 0d 00 2a 3a 22 36 c5 21 63 fb 95 fb c7 7e ....*:.6.!c....~ +| 1168: 6c 07 7d d1 a5 f1 a9 8f f2 d2 e2 0f 3d 02 56 94 l...........=.V. +| 1184: ea 44 2a 17 ee 99 82 71 9c 81 c7 9c 8f cd 2e 4d .D*....q.......M +| 1200: 8d 0a 1e 71 18 3d 4d 9d a6 ae 35 36 48 08 24 e8 ...q.=M...56H.$. +| 1216: 46 78 8e 66 f2 8a f1 3d c3 72 ad 65 18 5f ed 2d Fx.f...=.r.e._.- +| 1232: 7e 92 18 9e 4c 23 7c f8 8d 83 20 43 50 63 db 75 ~...L#|... CPc.u +| 1248: 12 7e 52 0e 71 13 e4 b6 31 c3 5f 88 b7 d0 76 80 .~R.q...1._...v. +| 1264: d5 9f 7e dc 8c b4 be 11 da 2c e1 a9 28 5e c8 46 ..~......,..(^.F +| 1280: 3c 0d cd 7a 2f b9 9f 53 9c 11 01 d1 09 31 5f 36 <..z/..S.....1_6 +| 1296: bb a4 f3 a0 ad ba b7 c4 ce d1 78 4e ad e8 cd f9 ..........xN.... +| 1312: 92 ed 51 05 69 ab 12 a3 89 4c 6d f5 7a c4 c5 54 ..Q.i....Lm.z..T +| 1328: 1e 26 e7 f8 22 1c 42 cb 9e ac 7a 53 79 09 3c 04 .&....B...zSy.<. +| 1344: 14 1d 49 15 c7 13 00 84 3c 7f 15 2d 5b 62 da d8 ..I.....<..-[b.. +| 1360: 8f 88 53 7a 78 f7 c4 7b be 67 25 cd 97 1f bf 19 ..Szx....g%..... +| 1376: b4 8f 4a 10 63 89 2f cf 56 c1 63 79 92 cc b1 75 ..J.c./.V.cy...u +| 1392: 45 ee 6a 9d c5 de 04 e0 60 49 c5 0e 09 a0 65 46 E.j.....`I....eF +| 1408: 57 f3 6c 5f 0f 07 99 fa 2b 2c 2c 5b bd b3 9b 50 W.l_....+,,[...P +| 1424: 38 43 94 4b 7b d2 b0 78 3b 4c e8 55 1d d3 6b 6a 8C.K...x;L.U..kj +| 1440: f7 dc 27 ef b5 b1 3a 52 9f 5a e9 7d 87 bb 61 5b ..'...:R.Z....a[ +| 1456: 39 a3 eb 5e eb f1 80 78 85 d8 86 16 76 3c b3 b0 9..^...x....v<.. +| 1472: 44 72 d7 0e 91 95 5e 7d d5 93 0a 4d 60 46 ad a8 Dr....^....M`F.. +| 1488: b7 d5 30 e2 39 4c d8 0a d4 ea 5e 46 95 39 1a 1e ..0.9L....^F.9.. +| 1504: 9d 81 27 20 bc 01 1e f5 2d 4a 19 4a a5 2d 94 91 ..' ....-J.J.-.. +| 1520: c2 a3 31 65 aa 13 f4 92 ce b2 c1 61 25 13 7d 93 ..1e.......a%... +| 1536: f0 c9 51 e5 ce 7c 0e 2d e4 f2 d4 b5 09 73 fc 96 ..Q..|.-.....s.. +| 1552: e0 a8 a7 85 b6 10 0f 95 a5 83 c5 4b ca 33 9f 98 ...........K.3.. +| 1568: 38 d9 50 29 38 08 cc b9 89 40 0e d7 1d 92 cc 4a 8.P)8....@.....J +| 1584: 7f a2 53 dd 19 d3 13 63 04 29 04 18 c2 79 97 36 ..S....c.)...y.6 +| 1600: 85 e8 95 4a 2c 34 7c 39 78 a5 fe 8b bb 22 93 4c ...J,4|9x......L +| 1616: 69 b6 86 ca 0c 61 39 b3 85 c1 df ee 28 b0 3d 73 i....a9.....(.=s +| 1632: 2f 78 84 69 f4 55 ed 6c b8 98 61 0c f5 64 15 7e /x.i.U.l..a..d.~ +| 1648: 84 bd 4f de ae 6c 1b db c1 60 c9 0a b9 22 f0 7d ..O..l...`...... +| 1664: 03 23 0f e3 f5 77 3a 3b 4b 7f 33 d9 e6 7c ee 92 .#...w:;K.3..|.. +| 1680: 0c 95 f6 9d 98 53 e3 7b 92 3b 15 a2 de 84 e6 75 .....S...;.....u +| 1696: 64 e1 44 b5 9d 16 77 10 78 65 e0 72 66 a9 13 db d.D...w.xe.rf... +| 1712: 27 cf 9f 0d 33 5d ee 89 37 cf 2f b6 fc 28 b1 93 '...3]..7./..(.. +| 1728: c3 6d 0f 80 d8 5a 16 33 ea 06 d7 81 d3 45 59 94 .m...Z.3.....EY. +| 1744: 07 73 bf 47 f6 fd 6e 78 13 5a ec 30 62 a4 c4 12 .s.G..nx.Z.0b... +| 1760: 2a 56 8f 29 03 37 0c 98 80 8e e9 50 4f 3a 37 a6 *V.).7.....PO:7. +| 1776: f6 29 3a 6f 17 54 45 8e df 98 62 1e 23 d0 ec 20 .):o.TE...b.#.. +| 1792: 4b c8 f6 cb ce ef f5 8b f2 fd a9 a8 51 5c 0f d0 K...........Q... +| 1808: e4 4d 7c a1 a1 2a 88 f1 24 78 9e cb 7c 81 6b 0a .M|..*..$x..|.k. +| 1824: 51 73 1e bf 48 21 fb b3 7a 5c 48 0f 53 04 00 5f Qs..H!..z.H.S.._ +| 1840: 7e 66 7a 2e c0 30 9f bf 2f e8 95 b5 fe ea 9c f8 ~fz..0../....... +| 1856: 0e 6d 1f 54 81 da e9 bc 28 ea 42 de 5b e0 7f 93 .m.T....(.B.[... +| 1872: 79 6d 6d 8e f3 3c ba 13 67 3a 08 31 03 43 da eb ymm..<..g:.1.C.. +| 1888: 34 40 04 3e a4 21 44 92 96 75 2c 9f 8f 03 89 98 4@.>.!D..u,..... +| 1904: f0 27 dd 27 49 3d e3 9b 37 4e 66 ef 98 bd 1b ed .'.'I=..7Nf..... +| 1920: 3f cc a4 10 85 a4 39 90 cc a3 80 3d fd e5 44 4f ?.....9....=..DO +| 1936: 45 79 64 37 d2 70 ba 69 99 0f 4d 33 5a 68 bd 53 Eyd7.p.i..M3Zh.S +| 1952: 92 24 0d 3f f4 e2 4b 72 35 cb 52 ea 3d d4 85 ee .$.?..Kr5.R.=... +| 1968: e3 12 b3 87 3a 9d 70 b8 5c bb c7 c4 98 03 84 25 ....:.p........% +| 1984: 5d 39 ef 24 bc ab b3 da 13 7b 2c bc 17 b1 76 4c ]9.$......,...vL +| 2000: 5d 5a 1c 62 14 8d 5d 20 32 dc 16 64 79 7e 13 95 ]Z.b..] 2..dy~.. +| 2016: 27 f6 99 dd f2 32 c2 43 c6 77 b4 f4 11 88 6d 3f '....2.C.w....m? +| 2032: 94 0e cb db c8 c3 9e 40 b1 7d d4 16 e6 95 f4 d9 .......@........ +| 2048: 3d 9b 5d df d5 a3 39 06 c1 eb a1 30 cb 85 77 d6 =.]...9....0..w. +| 2064: 9b d9 12 7a 6a 82 9e 3a bf 5a ff 5c b8 6f 4a bb ...zj..:.Z...oJ. +| 2080: 02 36 94 1d 5a 0c 95 da 85 d1 9c 22 5a 36 fd 28 .6..Z.......Z6.( +| 2096: 51 cf 48 60 84 a1 ce ad a9 5f 4c 51 43 71 7f d5 Q.H`....._LQCq.. +| 2112: 1a e1 6f b0 01 a5 6b f1 57 91 84 78 b0 d5 06 7c ..o...k.W..x...| +| 2128: f4 96 3d 9b e4 6d 04 b7 26 a0 5b 61 53 39 ba 49 ..=..m..&.[aS9.I +| 2144: 2c ea d8 18 e5 aa b3 ef 3e 8e 5d 91 ba 93 15 2b ,.......>.]....+ +| 2160: 4d e6 5c e7 47 ee 7c 9e d5 ca f4 82 91 84 f4 33 M...G.|........3 +| 2176: 02 46 9d dc 34 8d f1 5b 1c 15 5d db 2f c9 d0 09 .F..4..[..]./... +| 2192: fb a4 cf d9 0d 30 9f 16 3b 5e 80 ff 26 ac 19 b7 .....0..;^..&... +| 2208: b2 56 1a bb 72 7f b9 2f e7 41 6a e3 fc e5 cc e0 .V..r../.Aj..... +| 2224: 27 2c 78 86 1c 11 ee f4 51 2d 22 20 73 3b ed d6 ',x.....Q-. s;.. +| 2240: 42 c2 de 23 de c6 7d 43 8f 16 de 66 57 64 94 32 B..#...C...fWd.2 +| 2256: 13 da a6 91 b7 d5 8f 3a d6 c4 a7 e0 93 ff f8 9c .......:........ +| 2272: e3 70 a9 e9 e9 7a 50 37 3c 38 d3 fd 68 ff 17 5f .p...zP7<8..h.._ +| 2288: 96 b7 b4 77 c5 ce 1d 37 49 35 91 6c 2e 37 ef ac ...w...7I5.l.7.. +| 2304: 18 ee 9f 7a e5 28 88 a6 33 39 05 f7 c8 1f 1a 15 ...z.(..39...... +| 2320: f6 ad 55 5e 24 ea ce 21 c8 e2 1e 63 bd 7a 19 60 ..U^$..!...c.z.` +| 2336: c0 aa e4 42 2f 0f 53 f4 2c 04 99 a4 9a 13 86 6f ...B/.S.,......o +| 2352: 24 7b 42 ad c9 a9 c9 df 82 90 23 0a b6 e8 2a e4 $.B.......#...*. +| 2368: 36 c5 04 1a e4 dd 87 06 ca 6e 04 7b be 37 b4 ef 6........n...7.. +| 2384: 05 1d 6a ab 36 c8 58 52 3d ad 26 21 45 58 f4 66 ..j.6.XR=.&!EX.f +| 2400: 05 a2 95 89 9c 31 1c 3c 14 9b 59 90 29 28 f9 9d .....1.<..Y.)(.. +| 2416: f9 96 74 ab d7 30 91 81 17 ba b8 08 51 38 75 77 ..t..0......Q8uw +| 2432: 06 ca 81 1d e3 9b 67 3e 0f d9 fe 98 69 e0 d8 f1 ......g>....i... +| 2448: 78 dc 96 0e 88 50 9c b5 c1 1d 25 61 87 70 bf a5 x....P....%a.p.. +| 2464: 93 2f f9 c7 7b ab fd 2c 98 10 ef 0f ce a4 bd fa ./.....,........ +| 2480: 52 7f ff 29 53 5f 9c b5 92 2e 8c f5 36 a0 7c 88 R..)S_......6.|. +| 2496: 84 18 80 59 66 d2 c6 58 55 c0 5d 07 9f 69 d2 72 ...Yf..XU.]..i.r +| 2512: 88 0d 1d 8f 08 7c d5 b7 9c 15 4c 6c c6 1d f4 76 .....|....Ll...v +| 2528: 0a 82 32 6c 72 4e 3e cc ff 8b 62 c5 37 4e 0c 0d ..2lrN>...b.7N.. +| 2544: 09 89 8a 95 1b 63 d3 85 c6 4a 41 7c 49 46 60 95 .....c...JA|IF`. +| 2560: eb 37 60 82 84 55 ea fe 22 97 16 58 ad 33 06 ae .7`..U.....X.3.. +| 2576: d0 f5 5d 06 b3 2e b9 0a 6d d0 14 c8 d5 0b 8a cf ..].....m....... +| 2592: 51 ff 64 db a8 81 e1 8b 39 1a ef 85 18 fd a2 48 Q.d.....9......H +| 2608: 4f 47 b3 1e 5a 57 21 f0 6b 10 f0 ec 0a 4a 35 56 OG..ZW!.k....J5V +| 2624: f5 79 c5 de 62 d1 4a c2 e5 bc 69 89 9d 9b b9 4b .y..b.J...i....K +| 2640: ba 21 99 5a 58 c6 23 ab 67 45 13 4a 1d 3e da 68 .!.ZX.#.gE.J.>.h +| 2656: cf a6 4b 20 58 b0 4e 2f 0d f2 42 83 9d 96 43 6e ..K X.N/..B...Cn +| 2672: df 45 61 c5 bd ca 86 b3 6a ad 26 a2 8d c7 c4 03 .Ea.....j.&..... +| 2688: 63 d8 b8 02 0a f6 96 f9 15 46 2c bd 04 68 aa 1b c........F,..h.. +| 2704: 00 46 7e a0 bb 47 fe 5f 2b 7b b4 59 54 14 e3 25 .F~..G._+..YT..% +| 2720: cd 10 8b ff 05 6c 6d ab 9c 90 cb d4 d8 0d bd 58 .....lm........X +| 2736: be bc d5 c0 67 bd 0f d3 9d 22 71 c4 65 e6 2f bc ....g.....q.e./. +| 2752: 5e 3a 54 39 7a ec ab b1 46 80 25 5e 33 19 de 3b ^:T9z...F.%^3..; +| 2768: 4a 84 28 04 80 f6 86 aa 32 75 8e 5b 51 66 b0 64 J.(.....2u.[Qf.d +| 2784: 29 94 f6 56 6d 1c b1 2e 2f 4c 55 3b e5 82 01 75 )..Vm.../LU;...u +| 2800: 3f 58 d3 a0 b9 0b 6b 25 24 94 9e a5 ce a4 1c db ?X....k%$....... +| 2816: 51 f9 bb 53 71 19 0a 5a e3 2e 48 e1 06 24 7a b9 Q..Sq..Z..H..$z. +| 2832: 88 fb dc 4c 2a 23 b9 b9 7e 26 a4 3f 7f c7 b5 f1 ...L*#..~&.?.... +| 2848: cd cb 82 17 34 89 70 b4 10 ad be b8 4b 0d aa 07 ....4.p.....K... +| 2864: 61 6d b3 6f 56 ac a4 b2 e4 2f be 28 e6 24 3a ab am.oV..../.(.$:. +| 2880: cc 1a 8a 7f 54 c7 a3 c8 49 50 91 76 f9 75 d8 3a ....T...IP.v.u.: +| 2896: 76 1b ec 5b 51 c2 5f 6f c9 c7 60 24 0a 61 f4 ed v..[Q._o..`$.a.. +| 2912: d3 30 23 e7 a0 83 c7 5e f4 b8 1b 39 65 43 2a 8a .0#....^...9eC*. +| 2928: 1d a8 64 4d c5 4a a6 ab e1 f4 66 90 f7 2b 1c d6 ..dM.J....f..+.. +| 2944: ba 76 96 43 2d a1 6b 26 ee f1 3b 8b 2f e4 78 7e .v.C-.k&..;./.x~ +| 2960: 9b bd fa 2e 80 ff ec 26 74 5b 56 11 92 88 51 8c .......&t[V...Q. +| 2976: 6d 42 4f f5 3c 53 8b 42 3e b6 57 37 fe ad 66 59 mBO..W7..fY +| 2992: 4a f4 0e a1 39 a2 32 c0 23 78 e0 e7 db f9 f9 44 J...9.2.#x.....D +| 3008: 34 12 00 a0 d2 1f 45 25 4f 4a 72 74 9e b2 d5 e4 4.....E%OJrt.... +| 3024: 25 4e d5 0f ba 1b 87 7f 23 f2 7f dd 96 6c 1c 44 %N......#....l.D +| 3040: 0d 11 9a 22 6a f8 83 bf 37 e6 c9 5d 2e d3 dc d3 ....j...7..].... +| 3056: aa 03 03 7e bf a5 cc a1 d8 55 6f 05 1a 70 5e 89 ...~.....Uo..p^. +| 3072: d5 8e a5 08 f4 fc 90 7c d2 8a 67 53 82 25 82 02 .......|..gS.%.. +| 3088: 98 e0 ad 57 bb d4 7e fc 18 8f 25 9d d7 11 57 ab ...W..~...%...W. +| 3104: cf de 4b a5 1b 90 6b 38 a3 ef 93 ed 44 d7 9d f1 ..K...k8....D... +| 3120: 54 b1 0b 9b 2d e9 e7 d3 a6 6c 78 fd 40 d9 ea 7f T...-....lx.@... +| 3136: ec 10 60 2f ed 4c df f4 ef fb 4a cd fd 1a ea 94 ..`/.L....J..... +| 3152: 7b ac 69 70 be d1 2c ca e3 88 a7 b4 7f 95 83 eb ..ip..,......... +| 3168: 2e 82 0e 3e 60 27 b9 0c 62 56 3d b2 c4 7d 97 2c ...>`'..bV=...., +| 3184: ac cd 0b 05 b3 41 ab 7c 2e eb 4e a7 3e 8f db 53 .....A.|..N.>..S +| 3200: 88 59 f3 d6 f4 bf 5e 80 2e 5b f2 08 f8 dc 68 28 .Y....^..[....h( +| 3216: a7 a3 e3 4e ef 8f 3f 4e a6 c0 4b 04 d9 59 3e 53 ...N..?N..K..Y>S +| 3232: 07 78 2c b2 58 a6 78 a4 0a 6b 9e 6b 8d f2 3e 70 .x,.X.x..k.k..>p +| 3248: 45 07 82 f9 f0 d4 0f f7 81 e9 ab 09 c6 7a 3a ff E............z:. +| 3264: 98 6c bf 1f 43 c3 d5 ba c1 ad fe 9c a6 5e d5 f7 .l..C........^.. +| 3280: 93 ec 20 dd 33 ad 35 0b 74 47 80 1e 5b 8b c3 7c .. .3.5.tG..[..| +| 3296: 5c 81 66 82 dd 18 70 19 b0 54 18 2f 90 c2 22 bf ..f...p..T./.... +| 3312: 75 e8 b6 8e b7 3e 7c 2d b4 e9 7c b4 08 4e e9 c3 u....>|-..|..N.. +| 3328: e1 80 88 63 f2 90 ee ae 3d 9e 55 15 62 7f ee df ...c....=.U.b... +| 3344: 57 69 03 da 76 e9 f3 6b 90 9a a7 b9 bb 4b b7 2b Wi..v..k.....K.+ +| 3360: d0 a0 3b 70 b9 96 70 17 c8 0c cb ae 6e 3e e1 33 ..;p..p.....n>.3 +| 3376: 02 98 53 d6 af c6 d8 55 15 7a ea f1 8f 36 6b d4 ..S....U.z...6k. +| 3392: f2 8c fd db a5 81 50 fe c8 a8 cb 7f e6 9a a9 9f ......P......... +| 3408: 34 82 6d a5 11 6b e6 79 58 a8 54 ed 55 cd 19 46 4.m..k.yX.T.U..F +| 3424: e9 7f 92 f8 0b 3e ab b5 8d d3 9f 33 5c e7 f6 1e .....>.....3.... +| 3440: 28 f7 5f 2f 38 36 25 ed 7f 36 93 19 f1 a7 9c e7 (._/86%..6...... +| 3456: ad 4e 11 33 17 93 82 cf b4 a7 93 36 97 d9 e0 3d .N.3.......6...= +| 3472: 7e 54 b9 96 37 85 16 db e8 29 6f b0 b1 83 16 63 ~T..7....)o....c +| 3488: 9b 19 30 85 c6 17 2c b7 bc f2 2f 87 07 56 97 b3 ..0...,.../..V.. +| 3504: c4 a6 47 d5 8b 7a 68 a4 d0 11 ce ff 6c 4c 28 93 ..G..zh.....lL(. +| 3520: c3 37 ef 1e 7a 51 65 a9 6b 8a a0 a4 b0 b9 ef 17 .7..zQe.k....... +| 3536: dc 44 99 34 7b 33 f4 b9 3f d3 20 36 54 5c 10 6d .D.4.3..?. 6T..m +| 3552: 18 25 33 6e fc fa a0 16 c7 b6 78 f8 4b 0c 5e 7e .%3n......x.K.^~ +| 3568: c3 cb 95 54 90 88 57 20 d9 17 9b 82 dc e8 7f c6 ...T..W ........ +| 3584: bd d8 dc 13 ca 66 06 64 8e 46 2e 48 13 eb 4d 07 .....f.d.F.H..M. +| 3600: 24 7f 8e 35 f3 bf fb 54 80 c7 6e 93 da ce 7f 2b $..5...T..n....+ +| 3616: 60 3e 53 82 33 38 fe 69 7b f6 fa 2f d8 40 bd c8 `>S.38.i.../.@.. +| 3632: 95 75 6b a2 e5 53 7f 5c f1 f3 23 07 8f 42 04 8f .uk..S....#..B.. +| 3648: 64 43 a2 25 c1 40 07 e9 be b6 3c d6 1b 86 7f 91 dC.%.@....<..... +| 3664: f4 73 0f 6b ca 6b cb 8f 7c a8 5b 60 ee 20 a6 e6 .s.k.k..|.[`. .. +| 3680: dc 96 6b 8f 8d 62 1e 42 e0 1b 85 49 85 34 b4 c9 ..k..b.B...I.4.. +| 3696: 85 06 ec c4 b7 b5 89 47 af fa db d1 5b 9f ac 6f .......G....[..o +| 3712: 8a 27 53 48 4d ad a1 30 6b 3e 45 b1 b8 b9 50 17 .'SHM..0k>E...P. +| 3728: 7b b7 29 a8 b3 d5 9a 42 a9 2a e4 01 79 34 c5 21 ..)....B.*..y4.! +| 3744: a7 8b 21 69 ab c4 9b ec eb 0d 9f 34 b5 ff 46 2d ..!i.......4..F- +| 3760: 68 55 9b 63 9a ac ac 80 4c 45 8d eb dd b2 26 4a hU.c....LE....&J +| 3776: 91 8f ee f7 50 6f 84 81 c3 79 2e 6d 56 1c ee 01 ....Po...y.mV... +| 3792: 20 2b e5 ca 3a 51 ba 40 b6 44 15 a4 4a 3d d0 33 +..:Q.@.D..J=.3 +| 3808: 7d 2d 3c ca e3 f8 e9 1d bc b2 1e 59 1e 57 10 2f .-<........Y.W./ +| 3824: 92 4b b7 50 41 98 6f 07 0b 38 ba 31 08 3e d4 34 .K.PA.o..8.1.>.4 +| 3840: 4f f4 99 32 2f e5 7f 8f 14 8e 48 11 88 ad 18 d6 O..2/.....H..... +| 3856: db d0 e5 cc 17 4c b6 f7 45 f9 1e 9a d9 86 5d 56 .....L..E.....]V +| 3872: fa 76 52 49 cd f1 76 ea 84 b1 55 03 54 e2 ff 91 .vRI..v...U.T... +| 3888: 23 fb 22 b0 8a b8 da 6e 7e 54 25 03 f3 fb eb 9a #......n~T%..... +| 3904: ce 64 cb 70 38 c9 aa 11 e8 36 45 16 8c 82 0e 18 .d.p8....6E..... +| 3920: f3 cf 81 4c 7c ef 53 6a 5c 43 85 e3 01 c9 d5 97 ...L|.Sj.C...... +| 3936: 8b 03 78 90 35 c9 7c 3a 62 3d 66 d9 ff 6c 1f 3f ..x.5.|:b=f..l.? +| 3952: 7f a1 85 ad ec 87 65 3b 48 3c 0b 0c 94 c0 05 85 ......e;H<...... +| 3968: f9 a4 4a 79 c1 dc 7b 9f 87 c0 4f c9 7b a7 ce 5c ..Jy......O..... +| 3984: cd 11 68 d5 79 d2 e7 b5 8c 13 af d6 e9 81 ef 42 ..h.y..........B +| 4000: 4c 11 2f c0 55 33 4c f4 44 c1 75 67 ae 43 95 68 L./.U3L.D.ug.C.h +| 4016: da 90 26 d5 42 75 b6 ef 99 22 ce b8 2d 52 5f 43 ..&.Bu......-R_C +| 4032: a4 f7 23 72 17 c6 66 36 84 e6 d3 a7 da a9 ec 82 ..#r..f6........ +| 4048: b2 fd 87 90 51 2e 78 71 3f 56 49 96 d2 da 6b 67 ....Q.xq?VI...kg +| 4064: 2a f8 b1 eb ae 5d d0 e7 59 31 ed ed 24 b6 2c 32 *....]..Y1..$.,2 +| 4080: 7a b7 97 c5 e0 f1 a4 05 d3 8e fa 0f 10 ee 19 4d z..............M +| page 15 offset 57344 +| 0: 0d 00 00 00 01 04 3f 00 04 3f 00 00 00 00 00 00 ......?..?...... +| 1072: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 97 ................ +| 1088: 3e 07 06 00 ae 7c 00 00 07 2c f5 e5 48 a9 82 86 >....|...,..H... +| 1104: 40 71 a9 80 b9 af 28 61 b5 17 5a cb c3 67 db aa @q....(a..Z..g.. +| 1120: fa 64 ec 63 b4 34 d5 37 ee 52 d8 d5 38 6a 25 d0 .d.c.4.7.R..8j%. +| 1136: 1f 05 80 72 bd c3 b9 1a d1 ea f7 f0 73 0e 97 29 ...r........s..) +| 1152: 41 19 09 d1 53 df c8 c5 02 ab a2 e8 76 b8 09 3d A...S.......v..= +| 1168: 8b 07 21 05 5f 80 3d 88 67 fd fe ab e6 4b 7f 97 ..!._.=.g....K.. +| 1184: a6 17 42 02 f5 ad f3 74 6b bb 6e 2a fd af 88 3b ..B....tk.n*...; +| 1200: d9 b2 3c 0d 33 a7 93 69 c0 97 c5 c4 c0 e9 29 d5 ..<.3..i......). +| 1216: c3 78 96 13 55 f3 41 e6 87 eb 7a 2f b9 e2 28 f3 .x..U.A...z/..(. +| 1232: e1 ed b8 c8 5d bd 80 80 a6 52 50 ff 0d db 2b f2 ....]....RP...+. +| 1248: 7e 9e 91 32 fd 08 46 ba db 2b 41 00 70 6f cf 80 ~..2..F..+A.po.. +| 1264: 46 1b 49 cf ab 6e 1d 14 21 f2 3f 55 0a 4a ed 19 F.I..n..!.?U.J.. +| 1280: 11 9b 9e c0 f2 c4 55 86 2c 05 4e a2 6f 8c b2 9c ......U.,.N.o... +| 1296: 11 fd 85 0c 5b d5 ee ef 9c f2 4b 1e 74 72 bd a4 ....[.....K.tr.. +| 1312: 29 8d 0d 92 67 97 b5 b5 03 b4 a7 68 15 50 c7 7e )...g......h.P.~ +| 1328: 91 22 a8 39 3e e2 18 bd 68 20 74 c1 e5 f2 8a e5 ...9>...h t..... +| 1344: 1e 72 0c 2a e3 1a b2 60 57 ad 2b ee 1f 59 57 ce .r.*...`W.+..YW. +| 1360: 6f b2 e0 15 96 73 f6 2e 5b 82 b7 ca 6b bf ed 78 o....s..[...k..x +| 1376: e5 af 82 82 73 16 6a ce cb 09 b9 5f b3 2d c0 0c ....s.j...._.-.. +| 1392: 1d 46 5f 7e be ee 0c b3 54 e7 ed e7 cc cf f9 9d .F_~....T....... +| 1408: 42 90 94 e2 e9 c2 76 2b 02 b9 a8 73 20 0a e3 6f B.....v+...s ..o +| 1424: 23 a0 c0 59 f2 af 9d c4 78 90 ba 8e 0c ef b4 d6 #..Y....x....... +| 1440: 37 03 5e f4 7f 54 a6 d7 87 bc 20 3f e1 7a 84 d9 7.^..T.... ?.z.. +| 1456: 1b 33 4a fc bd 1f c1 8b b3 db 8f 0c fa 49 7f d7 .3J..........I.. +| 1472: 9a d0 48 8e c6 3b 79 22 d6 0c fa 93 4d 43 f3 de ..H..;y.....MC.. +| 1488: bb 10 3e 81 d2 6b b4 9f 4c 91 05 d7 87 b5 35 98 ..>..k..L.....5. +| 1504: eb 84 9d 2c 0e 4e 2b de 1b 23 0d f5 bd aa 4a 9c ...,.N+..#....J. +| 1520: 6d 7e 48 23 18 f9 72 5b ef 9e da a9 ef 1b 29 08 m~H#..r[......). +| 1536: 03 34 e2 86 97 ff c9 2d c6 04 34 65 84 20 5b 7c .4.....-..4e. [| +| 1552: df e3 2c bc 73 04 22 70 f5 03 f2 8f fe fb 72 b5 ..,.s..p......r. +| 1568: a1 93 91 a4 3e a5 20 ac 21 19 8a 60 60 48 63 a0 ....>. .!..``Hc. +| 1584: 47 80 a8 ac 9f 68 a9 ab b7 0f 71 d5 44 1a fb 4a G....h....q.D..J +| 1600: 99 06 2b fc 96 ad ac 5e 37 37 84 6e 4e de 66 ba ..+....^77.nN.f. +| 1616: c6 10 fc e9 a0 26 cf 48 f6 f6 96 b7 69 35 30 06 .....&.H....i50. +| 1632: f2 7a db 70 53 4d f9 5b 9b 5f 51 b8 f6 af fb 1c .z.pSM.[._Q..... +| 1648: f5 2a d7 01 80 d2 0e b3 fe cc 99 ad 74 ad fa df .*..........t... +| 1664: 26 86 c7 29 b2 b3 36 98 b8 b7 2c de 9c 78 41 99 &..)..6...,..xA. +| 1680: ff 46 40 76 64 64 0c f5 46 d5 bd 72 a3 46 b0 b3 .F@vdd..F..r.F.. +| 1696: 75 45 03 d1 06 4b c9 ca a6 4f 90 f7 9b 60 4a 46 uE...K...O...`JF +| 1712: ed af a1 d5 68 30 db a7 5e ca 76 bf 66 64 e7 fa ....h0..^.v.fd.. +| 1728: 50 5c 31 5f 86 94 3a 5c f4 98 3d d5 52 80 26 1e P.1_..:...=.R.&. +| 1744: 55 07 14 d5 f5 2a 17 46 8b 6d 71 d2 da 4b e7 c8 U....*.F.mq..K.. +| 1760: 3d 8f 97 b7 97 51 c5 fb aa 4f 3f 3b 85 c1 aa e8 =....Q...O?;.... +| 1776: bf 3a d9 56 c0 d3 23 31 28 46 bd 45 2e 4b b5 e7 .:.V..#1(F.E.K.. +| 1792: f6 8e fa e2 28 65 6c 42 f2 68 8f 37 cb 4d e9 26 ....(elB.h.7.M.& +| 1808: f7 63 58 8c 99 09 71 7c 14 3a d7 92 c4 cf 14 51 .cX...q|.:.....Q +| 1824: af fe 6e 6b 50 e9 3a 70 0f ac 53 9d 57 01 4f 7a ..nkP.:p..S.W.Oz +| 1840: cc b2 c5 b9 2a 3e 02 6a d4 15 6f 2e d3 20 99 ed ....*>.j..o.. .. +| 1856: d7 1f 71 01 2f b4 e4 08 fe 8c 38 02 2c 14 e0 fd ..q./.....8.,... +| 1872: ec 3e 61 a8 b7 fa de f0 81 56 6a e2 d1 3d 2f de .>a......Vj..=/. +| 1888: f8 a3 75 5d 35 1b a4 a3 8e 6e 8c 9d b2 56 56 ba ..u]5....n...VV. +| 1904: 89 ab 42 86 70 59 5e 70 51 27 fc 87 b4 5c e5 89 ..B.pY^pQ'...... +| 1920: 20 b5 bd 40 6a 68 cb 1a 70 49 b5 2f c4 02 34 66 ..@jh..pI./..4f +| 1936: be cc 14 12 e0 f9 c7 4c 84 ff 37 a6 a5 ae 5c 36 .......L..7....6 +| 1952: 03 58 8e 44 17 e1 ca 30 3e 7f c9 54 94 f4 ff 86 .X.D...0>..T.... +| 1968: 57 8b 5e 2c aa 0b 28 30 ac 5a 1a 14 39 70 8a 84 W.^,..(0.Z..9p.. +| 1984: c2 4c 64 28 b1 87 6a 34 cf 50 b7 37 2e 96 ca ba .Ld(..j4.P.7.... +| 2000: c9 b1 37 d5 56 e7 39 b6 70 b1 16 2b 67 8a fb 0f ..7.V.9.p..+g... +| 2016: b3 ec 53 b4 27 ba 3e 60 16 96 86 b0 02 ba c9 d7 ..S.'.>`........ +| 2032: e1 d9 17 0f d4 12 aa 5d 65 53 29 47 04 47 2f 6f .......]eS)G.G/o +| 2048: 76 be c9 87 4b 41 27 63 28 f9 b0 48 a9 91 36 32 v...KA'c(..H..62 +| 2064: b7 e1 1f 84 52 c5 3f 81 98 6d de e0 31 27 eb 49 ....R.?..m..1'.I +| 2080: 0c f6 0c 00 5c db 30 f4 67 4e 90 f8 32 10 87 0f ......0.gN..2... +| 2096: 27 0c 19 71 b1 b8 e6 ea 40 e9 e0 fa 30 44 f0 d0 '..q....@...0D.. +| 2112: ae 89 05 c6 71 0d 18 36 f5 75 4d 6e b9 cf 60 2a ....q..6.uMn..`* +| 2128: e9 3e 54 d3 86 34 3b 69 87 47 64 9f 0d 99 d5 d7 .>T..4;i.Gd..... +| 2144: 42 bf c6 dd 24 51 32 5d e8 70 cf dc 67 55 58 bd B...$Q2].p..gUX. +| 2160: a0 93 c4 f2 05 73 91 bc 46 d1 4a ac 03 9d b1 ab .....s..F.J..... +| 2176: e9 6a de 28 8e d4 2a 75 e6 0b f5 ed a0 a6 45 e2 .j.(..*u......E. +| 2192: ea 86 1e a9 9d ad d9 8c 82 72 26 9e 7b ca a5 13 .........r&..... +| 2208: 3e ab 9f ed bf 81 9d 51 f0 f4 47 be 89 e1 0b a4 >......Q..G..... +| 2224: ec 5f 1c 5d b2 64 d0 7f 31 59 2e 68 2c 54 d2 69 ._.].d..1Y.h,T.i +| 2240: 91 34 56 e5 b9 83 0d 3a 00 9d e3 3d 78 6d 47 1d .4V....:...=xmG. +| 2256: 09 7d 67 de d8 a2 92 e6 b1 36 4d 89 c0 dd ce 76 ..g......6M....v +| 2272: df 73 aa 1d 88 38 c5 63 43 ac af 28 35 f8 48 5c .s...8.cC..(5.H. +| 2288: 9c 4d 50 ab f4 f3 c8 3c 6a 82 99 68 f9 25 7e 86 .MP....>..tJU..O() +| 2608: f8 00 70 ee 67 18 5e 7d 4d 1a fd b5 92 84 69 4e ..p.g.^.M.....iN +| 2624: c4 de 4d 45 90 3e 70 22 d3 b6 b0 74 b3 b0 21 86 ..ME.>p....t..!. +| 2640: 2d 10 d1 d3 2f d8 89 d5 b5 bd e0 92 66 a0 99 30 -.../.......f..0 +| 2656: 0f 6d de e8 db 9a 2d 6c 20 89 43 3c e0 61 32 67 .m....-l .C<.a2g +| 2672: ee f6 be 79 20 b4 06 11 63 b3 89 0f df 28 56 df ...y ...c....(V. +| 2688: 94 3c f2 31 b0 b8 5f 11 6c 6b 9b d3 43 cc f0 38 .<.1.._.lk..C..8 +| 2704: 6a 92 3d ee a6 92 95 9c 6c 02 94 31 7b ae 7a 0c j.=.....l..1..z. +| 2720: 0b 49 0f 82 54 ae c4 b8 58 4d 57 75 d6 e9 20 5a .I..T...XMWu.. Z +| 2736: bf 5a 2e ae 2d 18 4f 60 62 d9 1f 91 1d cd da f4 .Z..-.O`b....... +| 2752: 00 e2 d8 fd f0 20 d3 70 2b 4d 6f a1 80 a2 25 16 ..... .p+Mo...%. +| 2768: 7e 75 5e 7b 45 8d 72 62 11 ba 36 7e 4f 89 ba 4d ~u^.E.rb..6~O..M +| 2784: e2 0c e3 f4 0d da ac 14 89 36 e5 c8 4f 67 a5 33 .........6..Og.3 +| 2800: ff c2 91 ac 81 25 39 21 c4 79 5c 97 6d 97 45 3a .....%9!.y..m.E: +| 2816: 6e 00 60 1e c0 e4 ee f3 a6 98 16 43 ee 56 62 32 n.`........C.Vb2 +| 2832: 85 db 7a 83 af 26 a6 3f be 66 c6 5b ff 51 d7 f2 ..z..&.?.f.[.Q.. +| 2848: 8f 2e 13 c4 b2 67 72 ac c4 4d a3 3a 69 cb b5 bc .....gr..M.:i... +| 2864: 01 74 09 53 16 3a fa 7d 0e 24 8a c8 93 14 4d ca .t.S.:...$....M. +| 2880: 95 2f 30 dc d1 6c 42 d6 78 04 c8 91 4e 86 2a a0 ./0..lB.x...N.*. +| 2896: 0f 71 da 74 20 b0 a9 a5 a5 7e 96 44 e1 86 d1 af .q.t ....~.D.... +| 2912: bc 1d 24 19 11 54 3a e1 a6 db 38 f8 64 59 29 0f ..$..T:...8.dY). +| 2928: 3d 7b ce 0d c5 38 bd cd c2 85 13 f8 62 09 83 96 =....8......b... +| 2944: 20 2c 3d 81 6b 9d 48 24 4e d6 eb 88 7e f4 6f f4 ,=.k.H$N...~.o. +| 2960: fc 55 32 3d ab 74 bd f1 4f 75 66 f5 53 a5 a6 7f .U2=.t..Ouf.S... +| 2976: f5 89 da df 35 a7 44 0b 84 8d 85 f3 08 9b 63 14 ....5.D.......c. +| 2992: 8b ea 66 43 6f 07 a3 e5 33 eb fb 8b 11 40 10 7d ..fCo...3....@.. +| 3008: 11 cd 65 d7 38 4a 50 d7 02 73 ed a6 15 ac 82 77 ..e.8JP..s.....w +| 3024: 0d 8c f3 7a ea a2 36 32 11 52 71 d3 f4 24 13 5a ...z..62.Rq..$.Z +| 3040: 22 e6 75 14 28 7f b7 0c a8 71 16 20 3c 79 f5 ce ..u.(....q. ..L..... +| 3552: 7f e9 e1 a6 3b 68 b0 1e a0 81 5a 54 f7 3c a2 7a ....;h....ZT.<.z +| 3568: 24 ee 8e 43 a7 df 14 22 da b2 8c 80 6a 40 4c bf $..C........j@L. +| 3584: f8 5a 4e d1 34 af 19 3f dc 4f 93 29 83 d1 af 7a .ZN.4..?.O.)...z +| 3600: 33 fe 25 1a 1c d5 c1 e5 98 4e 30 4d 2b 2c 6e 55 3.%......N0M+,nU +| 3616: 8d d3 0f d5 f5 a3 a1 44 10 ba d6 2d 05 71 5d 4d .......D...-.q]M +| 3632: 15 e6 06 21 00 56 c1 45 21 63 f8 61 bf c5 75 79 ...!.V.E!c.a..uy +| 3648: f5 5f 83 53 ba cd a0 e6 12 45 3b 8b 33 b6 4d 29 ._.S.....E;.3.M) +| 3664: 41 c9 ad 09 a3 f1 a7 a6 6d 8d ec 16 68 b4 a3 fb A.......m...h... +| 3680: 01 1a c3 ee 78 17 d9 de cb 2e 42 d4 a3 56 8f f4 ....x.....B..V.. +| 3696: 3c 75 b1 92 d0 b5 75 10 69 cc c0 50 a4 bb d0 4c r. +| 3776: 8d e5 f5 f6 d4 49 8d b4 65 50 38 8b b3 3e 20 c1 .....I..eP8..> . +| 3792: 07 cd ad 7d e3 d1 8e 5f 6e 76 7b fe 01 ba 08 74 ......._nv.....t +| 3808: 16 a5 1b 5e 95 a2 ff 91 bb 9f 64 3f a4 e5 95 be ...^......d?.... +| 3824: d2 59 02 aa c5 06 66 9e fe 56 6e d1 1d 31 6f b3 .Y....f..Vn..1o. +| 3840: da 6d b4 3a 17 86 e0 e6 c1 37 21 68 6c 33 d3 58 .m.:.....7!hl3.X +| 3856: e8 67 fa 04 8d 59 ae e4 fb 1f 8c 48 17 7e bb 8c .g...Y.....H.~.. +| 3872: 17 61 b4 a7 e4 6b 87 c0 6a 8b 5f d1 76 16 5c f7 .a...k..j._.v... +| 3888: 81 b4 2d 66 ad ba 46 6f 95 bf a8 19 e8 82 05 7f ..-f..Fo........ +| 3904: 43 47 8b 63 b4 14 c6 2a de 90 8c 6d 04 3d 90 dc CG.c...*...m.=.. +| 3920: c4 6b d0 4c 33 9f 6c a7 38 6b fd b8 53 de 17 5e .k.L3.l.8k..S..^ +| 3936: ef e4 17 19 25 32 eb 12 86 03 3c 61 2e b4 06 ca ....%2........|..~v3.]... +| 1104: a8 31 d7 92 4a d2 68 29 8d e4 3c 76 6f d3 a0 f2 .1..J.h)..8...j..:. +| 1152: 2a 01 f9 21 05 ec 07 3f 6f 7d ba 17 5d ba 3a 04 *..!...?o...].:. +| 1168: a9 cc 88 35 4f 43 4c dc ab 1d 1d b0 24 1f a2 83 ...5OCL.....$... +| 1184: 69 4a 89 1e a8 a3 de bc 17 15 be 3d 07 ae 42 02 iJ.........=..B. +| 1200: f6 60 ff 36 08 cf 63 1f b5 be b1 1c 45 14 b0 56 .`.6..c.....E..V +| 1216: 0c 2e b9 49 49 69 47 17 da 41 4e 91 0b 5a bd 76 ...IIiG..AN..Z.v +| 1232: be 67 65 82 27 35 0a 23 a4 21 f4 ba 78 af 0a 7c .ge.'5.#.!..x..| +| 1248: 0e f0 ca 90 c6 32 d9 dc f1 dc 25 33 da 76 c0 f5 .....2....%3.v.. +| 1264: c2 4d ed 55 d7 f8 1f 44 82 37 c3 d3 90 12 23 52 .M.U...D.7....#R +| 1280: 8d 3c 3d f0 9e bf 7b 0e 49 8a 32 5d a1 64 53 26 .<=.....I.2].dS& +| 1296: cb be bc 26 dd bf f8 a6 ad 57 e5 35 68 c1 6c b9 ...&.....W.5h.l. +| 1312: 02 d7 0d 70 f4 d9 10 8f 75 35 8b 60 21 46 62 60 ...p....u5.`!Fb` +| 1328: e4 e7 70 35 0f a9 70 0d 50 da 5c c2 17 9b c6 3b ..p5..p.P......; +| 1344: 61 83 9f 3f 1d f4 28 dc c6 32 a6 12 ff 56 dd 0e a..?..(..2...V.. +| 1360: 57 08 0e 0d 5b f8 ea 32 33 20 a7 a1 a4 9b b3 77 W...[..23 .....w +| 1376: 4f db 85 06 dd 0b 52 b0 45 ff a4 e5 23 5e ea c0 O.....R.E...#^.. +| 1392: d6 54 d6 84 cd c4 fc 2e 95 ec 78 13 cf 9a fa e3 .T........x..... +| 1408: a1 5a b3 60 1a ab 1a 97 21 67 9c cd 44 6f ee a9 .Z.`....!g..Do.. +| 1424: bf 1f 07 52 d0 1a a4 ae 15 1e b5 01 0d ac 31 32 ...R..........12 +| 1440: 11 7f 67 b4 9c 20 78 28 8b 4a 79 32 44 c8 aa 1d ..g.. x(.Jy2D... +| 1456: 05 4b 94 3e e2 f0 d4 f2 16 3c 8e b7 67 13 98 47 .K.>.....<..g..G +| 1472: 2b 6a 1a 98 c9 82 7d a7 a7 7d ee 62 dd a8 29 58 +j.........b..)X +| 1488: 98 5b 2e 5b 62 1b 1b 21 37 4d eb e7 85 ac a3 8d .[.[b..!7M...... +| 1504: fe 69 ca 05 34 83 84 34 47 8d 5b b0 8d 71 f1 22 .i..4..4G.[..q.. +| 1520: 07 b5 a9 be 42 88 58 27 84 3e 37 1c 0f c7 1f 77 ....B.X'.>7....w +| 1536: 72 5e 2c fc 80 43 03 71 00 22 7f fe 5d fe ea a7 r^,..C.q....]... +| 1552: 33 3a c6 ae db e7 1d ba 8b 61 8c b9 b4 b2 ab b4 3:.......a...... +| 1568: 2f ec 9d b1 9c bb 13 7b 6e 3a b9 aa 43 b2 14 6f /.......n:..C..o +| 1584: f0 27 00 31 f6 5b 9c e9 96 40 3a 13 2f fc 6b ae .'.1.[...@:./.k. +| 1600: 0a 55 bf b3 cd 83 23 25 f1 15 e2 2e bc 7c 6b 29 .U....#%.....|k) +| 1616: 90 8f 85 ff 5b 5a 18 52 03 84 4d f9 f9 fa e1 a0 ....[Z.R..M..... +| 1632: e8 32 29 9c 5b 9e ed 39 b3 17 f8 ef 7f 55 b4 3f .2).[..9.....U.? +| 1648: c3 f3 66 f1 bc 15 1b 78 2c 9b ab cf e1 10 d0 46 ..f....x,......F +| 1664: 3d 21 2b eb 4f 8f 1d eb 8a 5e 87 23 72 50 04 ea =!+.O....^.#rP.. +| 1680: 8f 53 1b 3a 3f cf d5 92 5a 06 c1 18 25 33 41 84 .S.:?...Z...%3A. +| 1696: 77 56 bf a5 8a d1 ff 97 51 34 d3 24 88 12 dc 33 wV......Q4.$...3 +| 1712: 09 09 fc 68 b5 2a 4a cf 7c 73 d8 ff 93 29 40 1c ...h.*J.|s...)@. +| 1728: 8e ea 7d e3 7f 25 57 f5 bf 2b 19 80 4d fa 23 35 .....%W..+..M.#5 +| 1744: 18 8b ad af 46 85 3b 51 34 49 dd cb 39 d8 50 50 ....F.;Q4I..9.PP +| 1760: 7a a5 58 af 72 66 5d 21 c2 e2 03 1f ee bb d2 b4 z.X.rf]!........ +| 1776: 62 d2 f4 c0 fb 04 12 b2 35 0c 0f 0d b9 e2 a5 28 b.......5......( +| 1792: 87 5b 76 c7 39 4d 18 8d 3f 61 ab a0 84 4a 11 22 .[v.9M..?a...J.. +| 1808: e1 a9 69 55 2b 03 41 34 73 83 0d 0b ed da a6 d8 ..iU+.A4s....... +| 1824: f8 ff 9b d2 62 1b ca 1a 40 4f 0a 86 ad e7 92 af ....b...@O...... +| 1840: ce 69 19 8e 35 5d cd 50 4e 53 2d 90 46 90 ae 8a .i..5].PNS-.F... +| 1856: 43 ac 8b 30 7f 3b b3 05 78 63 b3 b7 0b 3d 2a 13 C..0.;..xc...=*. +| 1872: 55 83 ed a0 61 6b 12 30 5c 46 f3 1f 18 1f bd 89 U...ak.0.F...... +| 1888: af 86 9a 82 ed 89 35 0e 29 06 2c b8 97 c5 ef 46 ......5.).,....F +| 1904: 90 ce 6c 83 f8 7d 08 75 76 b4 07 6b 48 15 bb bd ..l....uv..kH... +| 1920: c2 fd 79 a8 7f 54 e4 d5 93 c2 17 09 3f bb 58 84 ..y..T......?.X. +| 1936: dd 78 7a 81 c4 13 70 e5 23 73 d4 60 25 0f 91 bc .xz...p.#s.`%... +| 1952: a9 8e 54 64 46 e1 8f 11 66 7c 1e 31 5b 9e 10 d9 ..TdF...f|.1[... +| 1968: 7e 09 e8 bc 2f 73 d4 f6 27 0c a0 62 5d ce 65 40 ~.../s..'..b].e@ +| 1984: f7 24 b0 bf f0 26 c0 17 1a dd d3 5d 16 35 22 10 .$...&.....].5.. +| 2000: 4d c4 cf 5b 22 2f 12 b2 5b f2 87 04 34 4d 5c 9d M..[./..[...4M.. +| 2016: 50 48 71 c7 8e bb 7d ce 89 f3 e4 4a c1 f2 92 4f PHq........J...O +| 2032: 90 4d 58 ac 27 60 a7 e9 31 41 ca b1 c0 38 ce 2d .MX.'`..1A...8.- +| 2048: df 40 e9 cd db 2f f3 28 09 ef 14 79 99 ef 66 1b .@.../.(...y..f. +| 2064: e6 c0 7f 4c cc 61 96 b6 f9 95 9d 1b 90 16 55 08 ...L.a........U. +| 2080: 71 ad 92 88 98 01 7f b9 8e 9c a2 f4 d4 24 33 a0 q............$3. +| 2096: 53 f2 01 df 47 3f 8d 15 a8 d1 70 a9 c2 70 f8 75 S...G?....p..p.u +| 2112: b4 ed b6 7d 82 95 63 5e 86 c1 3a 7c de b1 46 fd ......c^..:|..F. +| 2128: b8 30 42 36 83 35 e2 4a 78 c2 b5 b9 1e 57 f2 19 .0B6.5.Jx....W.. +| 2144: 91 6d ff 4f 87 97 55 98 a3 86 a2 54 1f 2f 0c 8e .m.O..U....T./.. +| 2160: 72 d6 a0 37 f2 bf 84 a3 a5 b3 87 12 a3 ef 0a 00 r..7............ +| 2176: 31 69 49 83 8c 6e 82 3a 84 62 a8 ce ee ce 91 61 1iI..n.:.b.....a +| 2192: b7 26 c1 fd bf 77 15 81 23 e6 d5 6f bb c4 ab eb .&...w..#..o.... +| 2208: 8b a7 fc b6 4e cb c1 61 ab 59 a2 dc ca ca f3 42 ....N..a.Y.....B +| 2224: 90 16 d8 d0 03 43 c1 e0 d9 25 50 b0 17 65 64 d0 .....C...%P..ed. +| 2240: 11 be 52 a8 e3 bd dd 7f 39 1e 0b 85 ae 92 c0 1c ..R.....9....... +| 2256: 0c 3b 06 2a bb be b6 c7 94 d6 83 6b b0 17 6d 24 .;.*.......k..m$ +| 2272: b1 b5 2d 22 5e 7f b9 db 47 65 e6 21 37 a2 2f d3 ..-.^...Ge.!7./. +| 2288: 03 8a 91 c0 5a de 52 27 dc 4b 9a 92 f1 fc f0 93 ....Z.R'.K...... +| 2304: d1 de 9e 04 73 13 c5 ec 25 36 54 15 94 92 b9 de ....s...%6T..... +| 2320: bd e2 39 b3 5c 88 8c e6 11 48 28 b8 0d 50 5a 6f ..9......H(..PZo +| 2336: d9 8a a6 17 4f a0 82 ff 94 70 28 10 0c 41 80 c4 ....O....p(..A.. +| 2352: d9 42 a9 fe d2 f7 1c 70 45 74 fa 65 d2 cf 49 00 .B.....pEt.e..I. +| 2368: b8 24 83 06 97 f5 1c 48 a7 dd 82 2f f0 77 f5 e6 .$.....H.../.w.. +| 2384: 5c 03 11 fa 65 46 57 90 c0 6f 1f 86 58 de 34 21 ....eFW..o..X.4! +| 2400: 5f 76 d7 1e 1a 16 6a e1 ad 26 ae 6a 32 53 30 8b _v....j..&.j2S0. +| 2416: db d9 05 93 22 87 58 e8 91 d8 26 80 85 f0 01 93 ......X...&..... +| 2432: 77 0e 88 91 bc bc ce e9 5e 6e e8 b8 aa 4e ad fa w.......^n...N.. +| 2448: a8 a2 5a 17 b8 88 56 f5 71 8a 70 fe 83 4f 5c 8c ..Z...V.q.p..O.. +| 2464: 07 1a 45 cf a9 89 05 c6 81 79 90 a5 d2 53 a4 3e ..E......y...S.> +| 2480: ac be 52 ae aa 9d 30 66 c5 b7 1f 7a c8 8e 6a 3b ..R...0f...z..j; +| 2496: 82 54 6a 62 aa 6e 4a c4 02 11 5b 69 12 6f 84 23 .Tjb.nJ...[i.o.# +| 2512: 17 f6 3d 81 1f 29 60 28 7c e2 95 b4 ae 39 e6 6b ..=..)`(|....9.k +| 2528: 5e c5 df 82 66 82 57 d4 84 cd 2b 1e f2 a0 31 82 ^...f.W...+...1. +| 2544: 8b 9f 0e 0c 72 76 6b 6c 5b cb 0c 5c 2a 77 22 df ....rvkl[...*w.. +| 2560: 1e 96 44 f9 4e 22 dd 22 ff fc 14 a2 cc 36 77 02 ..D.N........6w. +| 2576: 81 8f 22 1e 46 ea 11 e1 85 41 8a ee 69 64 e6 27 ....F....A..id.' +| 2592: 8b 46 0b 4a 47 35 f4 72 71 62 a1 0c 4d 55 be a0 .F.JG5.rqb..MU.. +| 2608: 1f ae ae 8b a6 2d de 54 04 24 05 98 06 43 04 57 .....-.T.$...C.W +| 2624: 09 8e ff 81 ff 9c 9a 18 02 bf c3 66 8f 65 fb e3 ...........f.e.. +| 2640: 66 0c 20 bb 50 f5 0c a8 a8 e9 6f 53 65 b5 3a e4 f. .P.....oSe.:. +| 2656: 48 d2 1c 86 e2 a6 35 c0 91 d4 72 b6 67 21 49 fb H.....5...r.g!I. +| 2672: 0c f9 91 b8 64 46 c2 75 28 df ac c0 bc 4f 61 d9 ....dF.u(....Oa. +| 2688: 92 06 6b 48 d8 29 ba 4f e6 40 a8 c8 35 8b 83 e6 ..kH.).O.@..5... +| 2704: 79 ec a1 d3 c1 73 82 64 42 13 3c 7b 73 3e 7a 14 y....s.dB.<.s>z. +| 2720: 2d db ac 00 76 00 81 ae fb c1 30 7b dc 57 39 f5 -...v.....0..W9. +| 2736: 27 6b 1a d5 ed c2 4b 21 12 3e 4d e0 8e 58 69 9b 'k....K!.>M..Xi. +| 2752: f5 cb 26 7a 62 e5 7b 3a fd 1a 8a e6 7d e6 f1 60 ..&zb..:.......` +| 2768: cc 4f 30 39 76 b7 3d 49 3f b7 59 a3 d0 a6 c8 8e .O09v.=I?.Y..... +| 2784: 3e a8 e1 a0 1b 7d 82 39 8b c6 a5 87 3e 2c da 16 >......9....>,.. +| 2800: f0 71 cc 5e 02 17 49 6d 48 24 8c 19 9e c4 d1 97 .q.^..ImH$...... +| 2816: a6 cc 28 28 c9 3e c2 e3 19 6a 05 f2 bd 4c ef 44 ..((.>...j...L.D +| 2832: df 84 da 8c 3f 41 16 2f 87 b8 88 bd 6d 8b 6e dc ....?A./....m.n. +| 2848: fa b0 44 08 ee ef 22 84 bf c0 5c a4 2f 2c 7e a2 ..D........./,~. +| 2864: 50 8b 84 cd 60 08 d8 53 4b 2d 4f 1e 3b 14 b4 62 P...`..SK-O.;..b +| 2880: 97 a1 66 49 2a 6f 3b 36 85 c2 42 58 98 5a db 3e ..fI*o;6..BX.Z.> +| 2896: 64 fd ad 32 f2 e0 f9 5a 13 dd e1 68 6c 35 34 a5 d..2...Z...hl54. +| 2912: 3c 9a b1 8a 51 78 49 5b c6 ef 57 7f 6e de fe 2d <...QxI[..W.n..- +| 2928: 0a 2c 1b 74 3f 19 bf 8e 6d 2f 0b 22 91 36 95 5d .,.t?...m/...6.] +| 2944: a3 65 b6 3b 3a e4 de 70 9e 29 f4 c6 0a 23 27 d6 .e.;:..p.)...#'. +| 2960: d1 4a f6 2e 2b a0 ee 8d cd fc 42 7a c8 1a a6 36 .J..+.....Bz...6 +| 2976: 17 aa f4 03 b5 cc e2 54 7a c4 fb 27 e5 90 62 20 .......Tz..'..b +| 2992: 03 77 4d fc 35 7c 59 87 01 49 86 ae 82 6b 8b a7 .wM.5|Y..I...k.. +| 3008: bc b0 b0 dd f2 ef f7 ef 0a 36 a6 25 f1 70 ba 89 .........6.%.p.. +| 3024: e8 00 f4 fc c0 98 73 f2 b0 9a a2 ed d5 1e 17 d1 ......s......... +| 3040: 79 82 18 84 b0 f8 2a 5d f2 a1 03 d6 45 b0 01 26 y.....*]....E..& +| 3056: 19 01 6d b3 0e 5f 55 6c 2b 21 72 33 84 a9 ab fb ..m.._Ul+!r3.... +| 3072: 64 4d bc f0 1d 16 ae aa 09 c1 29 60 e2 63 e1 d5 dM........)`.c.. +| 3088: 84 41 6e 5c 12 08 9a 04 dd 27 b8 fe 2f fb ca 83 .An......'../... +| 3104: 2a 7b eb 05 3b 77 fd 42 31 42 84 98 89 24 3c cc *...;w.B1B...$<. +| 3120: 47 f6 bc 13 37 d7 97 98 c4 61 24 50 0b 9e e5 53 G...7....a$P...S +| 3136: 46 05 49 5e a5 51 d5 48 26 fa 31 eb 0e 76 14 16 F.I^.Q.H&.1..v.. +| 3152: e9 60 f6 05 d5 bb 47 85 e2 da f6 5a 0a c0 14 38 .`....G....Z...8 +| 3168: bb 70 4d be eb d8 6d 10 61 d6 9b c6 c4 f4 56 7f .pM...m.a.....V. +| 3184: ff b8 fb 1f 92 a3 f5 74 78 7f c0 8d 9d f4 b1 a2 .......tx....... +| 3200: f4 0c 33 da 98 9e a2 61 4c c7 41 9c ea 0f 33 54 ..3....aL.A...3T +| 3216: 40 54 31 c3 04 fa d1 4b 67 80 e2 a7 6a 77 c6 ca @T1....Kg...jw.. +| 3232: 04 fc 71 ea fa 0f 92 8e d3 40 e8 0e 1c 48 a4 55 ..q......@...H.U +| 3248: 7f 74 67 ea c9 29 67 73 b9 b9 73 b1 00 1a d4 0b .tg..)gs..s..... +| 3264: 21 95 d6 1c b4 68 2b c5 e1 18 40 7e 8e 09 6f 28 !....h+...@~..o( +| 3280: 88 2d 6f 24 d3 73 7b 89 7a a6 aa df ad ae 7b 14 .-o$.s..z....... +| 3296: d9 f0 ff 20 ba fd bf a7 d7 04 6c 35 5c 76 4e f5 ... ......l5.vN. +| 3312: d3 5b a9 2b 8f 3a 11 fa 35 26 eb 78 45 da cb 00 .[.+.:..5&.xE... +| 3328: 78 97 b1 49 82 4e c1 4d b1 aa b7 80 75 fb 20 75 x..I.N.M....u. u +| 3344: cb 3f ba 05 95 33 cd e9 b3 bd b2 84 c4 4f df af .?...3.......O.. +| 3360: 77 c2 44 24 57 01 f9 9d c1 ef b6 ce 01 6f a6 5d w.D$W........o.] +| 3376: 3d 4b 12 e9 8f c2 a6 d5 1c 3b e4 05 83 48 aa 78 =K.......;...H.x +| 3392: 4b 3a 1c 1b ad 8e bc 49 c5 ee 91 68 28 8d 74 74 K:.....I...h(.tt +| 3408: a0 e1 20 ba 3d 62 97 0a 40 58 6b 1a c9 be 53 00 .. .=b..@Xk...S. +| 3424: 5d 9f e7 8e b6 05 7a d2 d1 89 ac fd 7f 9b ec 19 ].....z......... +| 3440: d0 95 35 e6 41 32 eb 68 85 eb 06 8c 53 f2 25 f3 ..5.A2.h....S.%. +| 3456: 19 d9 1c e7 a8 c2 c3 1c cd 78 50 4d 73 2d 5c 15 .........xPMs-.. +| 3472: 7c 3e b5 f0 e8 64 f5 72 a1 4a 9d f3 87 1d 12 0b |>...d.r.J...... +| 3488: d6 50 21 18 ca 10 f9 29 32 53 14 54 08 ef 51 6d .P!....)2S.T..Qm +| 3504: 6e 20 ae f1 3c b5 6b 8b 4c 4d 68 9f 73 99 2f d2 n ..<.k.LMh.s./. +| 3520: f2 ea ba bd 72 e4 d5 de 71 c2 15 a2 57 f7 69 c0 ....r...q...W.i. +| 3536: c4 6b cb c1 72 8e a8 fe f8 79 d2 6d 97 82 bf a1 .k..r....y.m.... +| 3552: 11 35 dc 30 36 e5 32 b5 81 f7 2b 58 06 c3 28 3a .5.06.2...+X..(: +| 3568: 43 c0 bd 16 a4 b8 f7 37 2b 1f 5f 8a 2a 09 21 4e C......7+._.*.!N +| 3584: d7 14 35 31 e6 36 8d 6f 2b 2a e2 63 1f 59 8d 62 ..51.6.o+*.c.Y.b +| 3600: 16 cd 16 d5 5a 20 b0 4b 9c 44 4d bb 0e 8a 01 6d ....Z .K.DM....m +| 3616: 6c 2e 09 48 6c 32 f5 96 45 0b df 3a a4 09 a4 1c l..Hl2..E..:.... +| 3632: 44 81 72 60 8a ed 10 29 c0 62 da ba 51 4f a0 7d D.r`...).b..QO.. +| 3648: e0 9b ed 31 6a 0e f8 b5 f4 69 b2 15 d4 01 ed c5 ...1j....i...... +| 3664: e2 09 df cc 97 13 70 57 48 1b bd 4a ad 0b ad 8a ......pWH..J.... +| 3680: 80 3d c1 c0 c7 f2 2d d9 a8 b7 3f b8 e5 aa 0f 5c .=....-...?..... +| 3696: e6 95 2a c6 80 83 69 ca 0e dd f9 7e 04 48 7d d3 ..*...i....~.H.. +| 3712: d5 29 fd 9d 7f 59 d7 1d 9b cd c3 06 e7 51 13 bc .)...Y.......Q.. +| 3728: 60 d5 c3 d5 a9 0b 78 ab 01 3e 34 06 4f 8a 47 4a `.....x..>4.O.GJ +| 3744: 0d a9 c1 b5 29 c4 26 c4 5d cb bd 80 f1 d4 eb b9 ....).&.]....... +| 3760: 6b 7d 2f c3 95 7c 2c e3 86 c9 5d 41 ee 76 89 9f k./..|,...]A.v.. +| 3776: 3a f3 e2 c8 6e f5 37 a7 68 01 06 26 47 9c 1b aa :...n.7.h..&G... +| 3792: 5a dc 55 89 af 53 89 9f f2 30 ee 04 90 cc 30 de Z.U..S...0....0. +| 3808: f4 2a c9 23 59 9d cf 65 7d 2c 30 7e ae 7d 08 43 .*.#Y..e.,0~...C +| 3824: 1f a9 b8 14 3b e8 3d 1e 3e 4b 46 93 18 4b 08 ee ....;.=.>KF..K.. +| 3840: 53 f4 07 0d 4e 69 87 a4 61 9f b0 75 a7 fc 64 34 S...Ni..a..u..d4 +| 3856: 86 aa a2 82 21 40 d7 7a 03 30 d0 f4 47 d2 4a ff ....!@.z.0..G.J. +| 3872: dd 27 02 69 99 d2 c9 61 d0 fa 46 dc 07 1d f7 0b .'.i...a..F..... +| 3888: 39 0b a3 2d 7f 9f 1b ce ca 79 69 62 bc 50 1a 07 9..-.....yib.P.. +| 3904: 2c 1b 38 37 9f 93 65 dc 45 3a 1e 2b ff 3f d0 bb ,.87..e.E:.+.?.. +| 3920: 05 f2 9b 36 b5 79 9c ca 4f 7d 8a 2f 6e 62 1d 20 ...6.y..O../nb. +| 3936: ab 6e 5f 75 19 67 e9 4a e1 c4 6a 2e 8a 06 b0 36 .n_u.g.J..j....6 +| 3952: 37 47 5f a1 27 93 d4 36 80 85 f9 29 94 db 83 13 7G_.'..6...).... +| 3968: f5 16 1e 0e 6b 6c b1 c1 f2 66 0a 8e 63 ea ca 6b ....kl...f..c..k +| 3984: f3 74 91 9e 73 62 d4 09 bd a9 64 54 e1 64 b0 e4 .t..sb....dT.d.. +| 4000: b6 47 e0 ff 92 09 88 39 e1 44 f8 50 20 b2 3c 6b .G.....9.D.P .....|..g.>@.E.y +| 1104: ad 46 a6 a7 42 e7 7c 05 62 a4 28 61 03 fc 1c e4 .F..B.|.b.(a.... +| 1120: 48 2e f1 ef d9 09 23 04 f4 d7 c2 ad 73 c4 d8 69 H.....#.....s..i +| 1136: cf 15 ef a5 1a ba db 63 7c d8 e9 4c 70 cf 08 d8 .......c|..Lp... +| 1152: 62 d3 ec 60 a0 93 06 fe 50 87 b5 22 48 37 b8 46 b..`....P...H7.F +| 1168: 30 26 ed 8d 2c f3 ed c1 51 62 e8 ce 13 45 b4 4a 0&..,...Qb...E.J +| 1184: 9a 3c 76 c4 fd a6 e2 de 26 04 93 69 30 3c 26 4d .1..3. .. +| 1248: 2f 8f 00 d5 47 ae 22 01 bf 83 40 da c4 74 43 56 /...G.....@..tCV +| 1264: 32 35 6e c9 3a 82 44 0e 86 32 5a 8e 46 5c 16 23 25n.:.D..2Z.F..# +| 1280: e5 dc 7e 38 26 fd ba d1 d3 d0 7e 4e 88 6e 2a 85 ..~8&.....~N.n*. +| 1296: d1 25 70 b4 fc 55 2b 5a 5b 4e 01 1b 20 21 d2 2b .%p..U+Z[N.. !.+ +| 1312: c1 5b e9 3e f1 fe 3f 65 18 07 a6 10 3a 88 e5 9b .[.>..?e....:... +| 1328: 4d 83 91 f2 1f b5 03 2a cb 57 21 e2 20 fc 35 99 M......*.W!. .5. +| 1344: b1 49 4a 0c b9 04 35 b9 b0 43 52 14 e7 2d 03 30 .IJ...5..CR..-.0 +| 1360: 73 cc 40 39 c3 d6 58 1b 90 85 df 21 6b e4 03 87 s.@9..X....!k... +| 1376: e1 de 4c 36 2c f3 c1 0b 75 92 a9 11 6e 9b de ac ..L6,...u...n... +| 1392: cb 26 b9 8d 48 47 f3 63 44 30 b0 e6 84 31 ec aa .&..HG.cD0...1.. +| 1408: f9 64 3b b2 12 b4 13 46 be 97 6f 36 37 3a ea 4b .d;....F..o67:.K +| 1424: ef 0d d6 04 44 21 e3 25 72 6c 70 d5 58 87 7d 16 ....D!.%rlp.X... +| 1440: 37 39 d3 0a 7b fe e2 81 d1 50 6a 62 6c a8 86 ca 79.......Pjbl... +| 1456: b9 f4 4e b5 04 c3 97 1b 4e 65 49 ee f4 cd 9c e4 ..N.....NeI..... +| 1472: 89 7d 7e c8 29 9b 3e 88 82 b7 68 c4 9b ea 75 48 ..~.).>...h...uH +| 1488: 1b 5b 93 0d 12 86 df 35 44 28 95 6c f2 c4 e5 3b .[.....5D(.l...; +| 1504: 89 a7 bf 11 ce 79 76 99 bb 0d 01 2e 58 e3 e1 0d .....yv.....X... +| 1520: 06 19 5e 28 bc 06 32 4e 76 bd e5 01 a0 20 5c 3f ..^(..2Nv.... .? +| 1536: 15 ad e8 4e 64 5a 67 a4 77 3f 23 c2 be 0d 96 b5 ...NdZg.w?#..... +| 1552: 28 e9 c3 60 ec 5d 09 73 e9 dd 40 a8 7d a0 ac 4f (..`.].s..@....O +| 1568: ef 82 ac 4d 25 19 a8 1b 22 71 c2 fa 97 c3 27 da ...M%....q....'. +| 1584: 44 14 f0 41 26 20 fd 57 4f 4e 9d f5 b3 d5 7c 72 D..A& .WON....|r +| 1600: f4 e5 56 0c 8e e8 c0 39 1c 3c 0e bb ce 8f 08 cb ..V....9.<...... +| 1616: 7f dd 0c fc 11 17 4b 41 03 b6 43 17 25 92 84 8b ......KA..C.%... +| 1632: f8 51 57 92 93 14 25 11 83 ce da bf 04 0d ed 32 .QW...%........2 +| 1648: 07 dc 6d b5 e1 c5 74 42 07 06 2a eb 2c a0 78 a7 ..m...tB..*.,.x. +| 1664: de 21 66 0b ea 49 fc e9 26 87 63 5c 8f 66 59 95 .!f..I..&.c..fY. +| 1680: c1 cd e9 00 79 05 0f 57 d0 d5 a9 1e 48 f6 7a 9d ....y..W....H.z. +| 1696: 80 6d 5c 74 55 3d fd 67 0e ff 74 26 70 bf c3 9b .m.tU=.g..t&p... +| 1712: 5f af 8b 43 69 e0 49 31 63 2b e4 9f 59 09 0e 5a _..Ci.I1c+..Y..Z +| 1728: 18 84 b3 f4 19 17 18 16 5b fd ed 78 5f 68 ec 2c ........[..x_h., +| 1744: d7 66 05 6e 47 eb 9b a1 52 90 0f 34 99 4f a0 fd .f.nG...R..4.O.. +| 1760: 21 f7 4d da f6 ae e4 57 51 63 59 e1 ee b8 32 6a !.M....WQcY...2j +| 1776: 03 ac c4 c4 69 73 10 32 d5 94 25 7e 6b d0 ec 62 ....is.2..%~k..b +| 1792: 0d 17 f7 16 24 b5 df 11 7b 29 01 03 d8 ff 1a 2f ....$....)...../ +| 1808: 7f 51 ab 9e 41 5b aa 0c a5 5d e9 69 ce 3b 85 4e .Q..A[...].i.;.N +| 1824: 7d ff 5a 37 b5 74 18 78 85 95 4d 44 8a d9 8c ea ..Z7.t.x..MD.... +| 1840: 2e 10 6e e6 cb a1 3d ef 57 38 f0 9b f0 8b 0b 68 ..n...=.W8.....h +| 1856: f9 f8 9f ac 58 da f3 9c 2b 2a 90 c7 4d 00 e4 4c ....X...+*..M..L +| 1872: 46 59 01 1c 0c 6b 08 6d 33 07 41 d0 5d 23 20 3b FY...k.m3.A.]# ; +| 1888: 00 e0 6b 32 2e 76 3a 7e 09 3e c9 66 90 4f d5 9b ..k2.v:~.>.f.O.. +| 1904: 8a bd b1 6d f9 33 d9 5a b3 bb a1 2a 45 31 0b c6 ...m.3.Z...*E1.. +| 1920: ce 44 34 5d 88 61 89 d1 cc 7c 14 86 a3 d7 6b 6b .D4].a...|....kk +| 1936: 22 d2 98 ab 62 7c 43 2e a8 92 12 de 43 d4 0a 9d ....b|C.....C... +| 1952: 94 f0 41 2d 14 86 69 fe 98 71 1a a2 2b db 5b ec ..A-..i..q..+.[. +| 1968: 23 00 e3 a3 03 1d 31 97 94 e4 85 1e 5f 53 44 66 #.....1....._SDf +| 1984: 0a 21 df ec ca 6a a6 a3 f4 c8 00 40 8e 0f 19 3f .!...j.....@...? +| 2000: 36 69 a3 7c de fc 73 16 d8 d3 f9 ff 03 ac 94 dd 6i.|..s......... +| 2016: 38 d5 d9 20 2e d8 97 2a 22 9d bb a5 cc 04 07 8f 8.. ...*........ +| 2032: be 18 b4 a8 ae e5 b4 45 27 7f e2 7e 3f 91 67 98 .......E'..~?.g. +| 2048: 86 3d 3c 05 3a 1e 71 bf 40 f8 a6 ca 48 7a ca c7 .=<.:.q.@...Hz.. +| 2064: b7 67 10 bd 99 79 53 32 70 b0 74 fc d9 93 60 c8 .g...yS2p.t...`. +| 2080: 3e 39 5c fa 3c 97 34 70 57 a3 0f f8 ab 4e 3f a6 >9..<.4pW....N?. +| 2096: a5 25 26 ea 0c 60 fe 32 f3 7a 68 d0 9f 50 a4 45 .%&..`.2.zh..P.E +| 2112: 13 2e f8 41 3f e9 d1 9f 78 4a d0 a7 d3 57 3c af ...A?...xJ...W<. +| 2128: a6 69 6f 2b 4e f0 86 b5 96 2d b2 de 06 5b 68 b2 .io+N....-...[h. +| 2144: 9a b9 de e7 7d 54 0a ff b1 26 17 ea bc a7 37 ff .....T...&....7. +| 2160: 46 7b f7 00 90 ec 71 92 cb 5a b7 c8 7f 45 60 ec F.....q..Z...E`. +| 2176: 9e 30 ca 80 69 4c 07 4d c8 c9 53 c4 77 3c 93 15 .0..iL.M..S.w<.. +| 2192: 17 63 15 c0 3a 1d a2 02 82 6d 4b ed 50 4c 5d 93 .c..:....mK.PL]. +| 2208: 2d ef bf 1e 9a ff 04 26 1a f6 7b da c9 21 7b 50 -......&.....!.P +| 2224: 4d 2f 53 c4 1e b4 dc 9f 5f 33 26 80 4a 8c ef 54 M/S....._3&.J..T +| 2240: fb 58 95 55 3c ec c0 a7 c5 78 a0 91 08 b4 d6 ce .X.U<....x...... +| 2256: d8 25 27 2b 37 e9 63 72 94 c3 89 5a 58 85 f4 95 .%'+7.cr...ZX... +| 2272: 09 fe db 6c 9c 71 54 af 1a 0c eb 2d e6 a9 db a0 ...l.qT....-.... +| 2288: 04 2c 29 70 12 b2 7e 66 ae 25 ce b4 b9 5c a2 12 .,)p..~f.%...... +| 2304: 1c 75 10 d5 54 f8 04 c5 8d be 38 29 64 f8 29 00 .u..T.....8)d.). +| 2320: 07 35 e4 e7 6e bc 64 db 39 d8 98 ee 72 28 a8 8b .5..n.d.9...r(.. +| 2336: 0f 1b 87 26 6e d4 73 1b ef b6 d4 db 05 12 1b c7 ...&n.s......... +| 2352: c6 1e 02 b1 ab bb e2 29 81 9a 9e b6 80 22 6e b4 .......)......n. +| 2368: 3c 30 ad e7 8b a9 60 7e de 01 a3 74 0b 76 0b d6 <0....`~...t.v.. +| 2384: e8 a0 91 65 ce bb 6a d7 96 cc fa 96 d0 c0 39 d0 ...e..j.......9. +| 2400: 54 80 1d 8d 79 17 11 86 c5 8f a7 7f 57 92 8d b2 T...y.......W... +| 2416: 41 5b 86 61 94 23 99 77 b0 5d 5d f9 29 86 36 c8 A[.a.#.w.]].).6. +| 2432: ea f8 2f b5 7d 42 66 fe c7 0d 19 17 e6 6f c5 79 ../..Bf......o.y +| 2448: e0 22 24 70 eb 79 23 ec 45 77 d3 44 8f 74 15 bc ..$p.y#.Ew.D.t.. +| 2464: cf 46 cc ff 5b 05 1e 03 7d ea c5 82 14 11 86 4b .F..[..........K +| 2480: cb 23 5b e0 1d 96 ca ea 62 d8 71 56 c4 1f d6 ed .#[.....b.qV.... +| 2496: 9e 8f bd b0 4a fe 53 87 c8 16 1a 31 cb 25 ba 18 ....J.S....1.%.. +| 2512: 66 c3 b2 a7 91 68 ff b9 2b bc c4 f5 50 36 79 e5 f....h..+...P6y. +| 2528: 33 c3 d4 98 e1 fd f9 3a c7 a1 ef 79 29 c1 bf 7e 3......:...y)..~ +| 2544: db 7b 66 3b 95 37 02 c7 ee ca 7f 9f 50 8f 47 5d ..f;.7......P.G] +| 2560: af 45 29 e3 f5 7e 07 93 73 a0 42 ef 05 a6 2b 74 .E)..~..s.B...+t +| 2576: 92 ab 8a 9a 19 be 2f 21 cf 6a 90 02 a3 1f f4 5c ....../!.j...... +| 2592: 72 b3 31 00 85 9b 20 d8 f3 c5 00 a7 15 30 56 6c r.1... ......0Vl +| 2608: 0f 7c 84 be 55 5b 8b aa df d6 bd 9e d9 55 54 93 .|..U[.......UT. +| 2624: 44 7d 9a 9b b8 38 46 b7 1e 1c 75 17 bb 29 05 5f D....8F...u..)._ +| 2640: da 39 7e fe 9e 40 e3 a5 7c a2 ba 5d 7e 9d 14 57 .9~..@..|..]~..W +| 2656: 19 10 2a d1 85 d3 06 0f d4 d8 ac f0 03 8a bd 61 ..*............a +| 2672: 26 89 4e c6 92 0a 4b 0b a2 3e 1d 5e 88 8c ce 33 &.N...K..>.^...3 +| 2688: 47 e7 4e 69 0c e3 09 ef 3a 9f af 19 ae 83 68 21 G.Ni....:.....h! +| 2704: 75 c3 54 7b 00 a5 f3 d2 32 41 69 ac fc 31 2e 0c u.T.....2Ai..1.. +| 2720: 91 dd 90 78 2c ab 3f 0c 37 a4 8c 69 a9 ae 41 8e ...x,.?.7..i..A. +| 2736: 6b b5 b5 ee bf df bc 1c 61 bb c9 51 37 a5 d5 96 k.......a..Q7... +| 2752: 0c 26 aa 97 67 72 73 cc c1 92 d3 46 cd 3e d8 ad .&..grs....F.>.. +| 2768: e6 8b 51 ee 41 ed 39 57 65 2e 5c 39 2b eb a7 72 ..Q.A.9We..9+..r +| 2784: b6 4c a8 87 72 a4 16 b4 d9 90 db e9 25 11 53 4e .L..r.......%.SN +| 2800: ef 24 1d b0 21 bc 97 52 38 53 49 c8 31 c1 c4 9f .$..!..R8SI.1... +| 2816: 2b 13 0c 10 5b 4a 0c 6e 07 c0 ec d4 77 f0 f0 38 +...[J.n....w..8 +| 2832: a6 88 d4 28 40 30 76 f4 ab 2e 94 b5 6d 47 79 84 ...(@0v.....mGy. +| 2848: f9 aa 28 32 66 c8 aa cf 17 18 3d 92 0e 66 4e fe ..(2f.....=..fN. +| 2864: 5d 80 51 29 df 97 de a4 c6 57 23 67 84 f4 32 86 ].Q).....W#g..2. +| 2880: 51 03 b6 67 29 54 74 87 da c0 41 e9 3a 3e 07 02 Q..g)Tt...A.:>.. +| 2896: d9 85 dc 55 e7 23 60 80 b0 01 48 cd 59 21 82 fe ...U.#`...H.Y!.. +| 2912: 14 65 1a 5d 9e 5e 2b 69 52 ee 64 01 4d 46 ac 94 .e.].^+iR.d.MF.. +| 2928: 60 04 d9 2c 41 e3 5b 35 e7 cc 75 06 7d ff 48 ae `..,A.[5..u...H. +| 2944: 13 e5 4f 54 f6 78 86 c5 c4 99 58 02 41 87 a9 82 ..OT.x....X.A... +| 2960: 34 95 75 b2 e5 5e 92 23 a1 7b 3c 7b 1d 94 dd 5f 4.u..^.#..<...._ +| 2976: f6 56 07 06 41 12 a0 56 7a 15 01 58 1f 9f 15 1a .V..A..Vz..X.... +| 2992: bd 2e c6 ea b8 29 ae 13 19 a6 40 b0 8d ec 3a cd .....)....@...:. +| 3008: ca 6b b4 d5 96 95 fe 8d 34 23 aa ab df c3 23 fa .k......4#....#. +| 3024: c4 02 eb 10 8c f2 e8 e0 5f d4 e9 4a ae f4 8d 60 ........_..J...` +| 3040: a9 1f 65 40 93 26 bf 1c 49 9d b6 8f e3 f1 c7 0b ..e@.&..I....... +| 3056: a7 bb d1 ae 56 60 5c 80 d8 e0 e4 f0 72 62 45 b7 ....V`......rbE. +| 3072: 31 33 66 a7 17 a4 34 67 f6 55 b1 09 eb 75 8f f0 13f...4g.U...u.. +| 3088: cd c1 86 e3 f5 3a e7 d8 08 da 4c a7 b4 45 8b ce .....:....L..E.. +| 3104: 9c 8d 48 41 b4 3e 6b 2d fb be c3 e7 bf 80 d3 29 ..HA.>k-.......) +| 3120: 9f 8c 5d 7e 48 76 dc 95 cc 30 bf 9f e1 e0 dc c1 ..]~Hv...0...... +| 3136: da 9c 73 18 3c ff 53 49 88 dc ef 90 20 5f 3f 75 ..s.<.SI.... _?u +| 3152: 85 02 0c d4 c3 f4 c1 ed f7 82 05 c4 e9 80 97 65 ...............e +| 3168: 09 2a ac 52 c5 77 6c f4 6b 35 30 c2 fd 38 48 ae .*.R.wl.k50..8H. +| 3184: b6 f6 38 0f 87 1a 66 54 91 3a 5b a7 48 5c 9d 36 ..8...fT.:[.H..6 +| 3200: e5 7f 60 4d 68 19 ff 8d 2e 6a 8a 99 37 72 11 f9 ..`Mh....j..7r.. +| 3216: 5b 29 77 3e 76 cd 57 6b 07 d0 cf d6 b4 cd d0 1e [)w>v.Wk........ +| 3232: dd 6e 1c a1 bd 7f bd 9d 57 0d 14 25 9e 4a 36 8c .n......W..%.J6. +| 3248: 9f 7b b4 f9 db 57 22 f1 0b 47 e8 2e 04 70 e2 d6 .....W...G...p.. +| 3264: 29 0e bb 52 33 80 6b 12 a5 20 97 3b 60 01 a2 8c )..R3.k.. .;`... +| 3280: 74 68 0f b1 b0 44 63 04 fe 69 2f 98 ee d9 e9 39 th...Dc..i/....9 +| 3296: af 0d 7a 79 00 ca d4 3e 96 e1 27 f8 27 e6 a7 d5 ..zy...>..'.'... +| 3312: 0c 7d 0c 13 de 31 bd 75 cf 41 04 1f 03 bb df 1d .....1.u.A...... +| 3328: c7 c0 3b a8 15 d8 15 4b b5 89 ff 4c 16 31 7a 62 ..;....K...L.1zb +| 3344: bb f8 d0 f2 43 fe 03 68 b0 35 7a 33 ae 4e a9 0b ....C..h.5z3.N.. +| 3360: 6a c8 35 d3 2c bf 6c 35 7b 1d 4c 9d 62 c1 96 01 j.5.,.l5..L.b... +| 3376: cf 7f 5f f0 20 ce 16 25 32 bc b2 bb 29 7a 6c f5 .._. ..%2...)zl. +| 3392: 36 17 6e 1b 93 4f 0f f0 de eb 67 a8 be 6a ad 7a 6.n..O....g..j.z +| 3408: e6 42 1f 63 d2 de 72 1b f6 01 08 e8 72 d6 85 81 .B.c..r.....r... +| 3424: 3d ec 80 70 f3 84 5c 54 2a 2d 66 f8 e9 ba d7 7c =..p...T*-f....| +| 3440: af df 2a 43 f4 6c 78 3e 5d e6 92 b4 9f 02 05 bb ..*C.lx>]....... +| 3456: 84 9e ed 44 71 74 21 c9 be 07 3b 98 b1 49 b7 81 ...Dqt!...;..I.. +| 3472: 6a 4a c4 08 28 1c 19 0f ae 36 c2 6c a2 55 af 09 jJ..(....6.l.U.. +| 3488: 47 da e4 a8 32 df 4f 68 83 b1 c8 89 a0 1a e1 72 G...2.Oh.......r +| 3504: b2 dd ab e9 fd 2f c8 79 d1 7e 69 16 e7 72 85 09 ...../.y.~i..r.. +| 3520: 95 77 18 1f 2c d1 51 31 68 88 8f 0a fb a1 4f 3b .w..,.Q1h.....O; +| 3536: 6d fb 24 cc 0a 77 34 cb 49 35 06 07 a6 cd 5e 27 m.$..w4.I5....^' +| 3552: d6 87 cc ae e1 00 cc 69 22 0e 4f 5a 59 cc 8f 30 .......i..OZY..0 +| 3568: c3 65 8f 8d bc 1a 86 8f 02 9b 0a 2c 5b b5 3d fa .e.........,[.=. +| 3584: c9 fd 1c 96 4c 59 e1 ac 43 17 e8 b1 18 57 c8 48 ....LY..C....W.H +| 3600: c7 df e9 d1 d3 11 d9 4b 44 33 d9 da 9d 86 7a cb .......KD3....z. +| 3616: de 17 ce 40 aa b8 2d b6 26 a9 68 e8 72 f3 cb e0 ...@..-.&.h.r... +| 3632: ae ec b1 1e 76 0b 5c 49 93 f5 f1 04 56 fa ac 33 ....v..I....V..3 +| 3648: 65 de d4 36 6b 3a 3c e1 a7 7b b6 66 e1 69 e7 86 e..6k:<....f.i.. +| 3664: c0 42 a2 71 e5 f1 35 3b 38 1e 52 a2 bb 72 39 de .B.q..5;8.R..r9. +| 3680: e2 78 23 3a 71 b3 0b 8d 94 e5 26 80 9d d3 67 76 .x#:q.....&...gv +| 3696: 14 aa 98 b4 f8 42 78 6c a1 8f 7a bd 5a 5f 99 1f .....Bxl..z.Z_.. +| 3712: 14 5a d4 02 bf c3 60 a7 c7 34 8f 81 4b 4a f6 c2 .Z....`..4..KJ.. +| 3728: 21 0e 6e 26 55 79 10 67 13 dc da ac 80 37 ad 95 !.n&Uy.g.....7.. +| 3744: 11 4a 54 b5 f9 da f6 66 26 99 51 98 1e 05 c0 5b .JT....f&.Q....[ +| 3760: 2d 23 b8 a2 5b de c3 f4 4f 2b 3a 22 10 1c 25 cc -#..[...O+:...%. +| 3776: 57 db 91 c4 6c df 33 a1 1f d6 a3 2e 56 de 54 d1 W...l.3.....V.T. +| 3792: 02 ac 2d ff 96 d6 db ec 80 f7 79 ea 81 30 68 d0 ..-.......y..0h. +| 3808: 90 b2 d1 cc 5e ac 50 df 5e 1b 0a 78 01 70 e8 21 ....^.P.^..x.p.! +| 3824: 35 08 64 46 9a 55 91 aa 9e 41 df 4f 5b 2b 3e 1c 5.dF.U...A.O[+>. +| 3840: bc f5 90 37 d7 35 ac 34 8e 70 94 28 46 eb c8 42 ...7.5.4.p.(F..B +| 3856: 10 df 40 b6 c0 76 57 6d 79 d6 f7 b6 cf ca 88 bc ..@..vWmy....... +| 3872: f9 f6 ce ad 69 d8 a6 a7 41 cb cb c6 bf 91 ef ff ....i...A....... +| 3888: 72 6f d2 b0 1e bf db 33 4b cd 1b 81 2d 65 de ff ro.....3K...-e.. +| 3904: 5c 4b 45 f0 57 c3 5e 1f 77 6d 14 b7 ea e4 8c 88 .KE.W.^.wm...... +| 3920: d0 b1 63 86 fc 68 26 e3 b2 7a 5f ca ec 09 00 4b ..c..h&..z_....K +| 3936: dc f0 d4 70 fd c1 10 36 a9 cc fc 0d fd 24 12 13 ...p...6.....$.. +| 3952: d9 bd f0 2f 87 99 00 d1 d4 7c c6 41 f3 59 2a db .../.....|.A.Y*. +| 3968: e1 7c 44 99 65 e7 4b 2f 42 a6 02 ad eb 7b db 14 .|D.e.K/B....... +| 3984: e4 c7 0d bf a7 f1 3d 69 01 cc c0 2e 82 06 fe 13 ......=i........ +| 4000: a3 f2 df 3f fe 65 d6 73 b3 d1 48 0c 00 5f 6c 4e ...?.e.s..H.._lN +| 4016: 1e ff f6 d2 69 08 a0 7a ed 50 16 a5 b1 56 1c f5 ....i..z.P...V.. +| 4032: 14 b0 b8 3a 38 19 a6 e7 ef 28 3d b9 9f 5e 38 39 ...:8....(=..^89 +| 4048: bc 41 c2 b1 fa 0b be 8b e9 9f 2f a0 be 8a eb 7f .A......../..... +| 4064: eb 55 5e fb 3a 36 82 02 fd 50 a6 b2 7d fb e7 5a .U^.:6...P.....Z +| 4080: a4 b2 d9 d9 8b 93 7a 2e ed c3 0c 13 68 22 cf f9 ......z.....h... +| page 18 offset 69632 +| 0: 0d 00 00 00 01 04 3f 00 04 3f 00 00 00 00 00 00 ......?..?...... +| 1072: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 97 ................ +| 1088: 3e 0a 06 00 ae 7c 00 00 18 91 e3 df f3 2f 7b 53 >....|......./.S +| 1104: 6d 25 6e 2a df e7 b0 18 33 2f 73 08 d7 a8 04 75 m%n*....3/s....u +| 1120: 49 89 b0 84 41 57 fb 8f 24 85 4e 24 c6 f1 14 03 I...AW..$.N$.... +| 1136: 9e 8e ef 9d f0 5c 54 69 11 78 d5 0f d6 97 22 1e ......Ti.x...... +| 1152: b5 17 73 66 0d 95 db ce c4 12 cb b0 ae a8 53 70 ..sf..........Sp +| 1168: c7 6b 02 f6 7d 56 49 eb 11 75 da b6 c2 ba a1 f2 .k...VI..u...... +| 1184: 19 8d c3 fc 6f 9d 3e e8 0e 1a 51 37 d7 ff b1 19 ....o.>...Q7.... +| 1200: 50 bd a1 74 26 2b 53 c6 1e 12 75 20 ed 43 51 0e P..t&+S...u .CQ. +| 1216: 28 f7 c4 a3 b5 8c ec de 2c 58 29 8c 74 81 d7 2d (.......,X).t..- +| 1232: a2 8d 0a d3 85 98 6b f2 3e 34 94 08 73 06 e7 06 ......k.>4..s... +| 1248: 04 e3 81 81 2a d8 5c 61 1b e9 7c ff 92 a8 c9 50 ....*..a..|....P +| 1264: 97 a0 8e f6 1f 4b 25 ae 8e fa 84 7a 6f a7 11 42 .....K%....zo..B +| 1280: c2 97 7a 93 42 39 ab 41 9f c1 7c 86 4a 66 06 cd ..z.B9.A..|.Jf.. +| 1296: e3 20 2c 3e ba f5 3c 32 fd a7 4a 73 50 74 a4 50 . ,>..<2..JsPt.P +| 1312: 89 28 57 71 bc 3d 31 77 89 ef cd cd ff b4 fb 2c .(Wq.=1w......., +| 1328: 5f a0 55 da b0 d9 18 d4 f7 a3 e4 f0 bf a8 86 b8 _.U............. +| 1344: 64 97 49 26 1b 4a b7 e7 6f 88 a9 a8 ab 54 1c 48 d.I&.J..o....T.H +| 1360: 0c 77 2c e3 7a 83 7d 05 c8 22 f7 b7 57 92 61 97 .w,.z.......W.a. +| 1376: 69 52 51 f7 8d 4c 2a b5 5e d5 55 f4 fc ae 35 bc iRQ..L*.^.U...5. +| 1392: fa 44 c5 19 ec 62 5e 98 12 bf a8 e1 53 32 76 4c .D...b^.....S2vL +| 1408: fb 4e 80 40 85 39 71 69 bd 55 90 9b 4c 46 b1 06 .N.@.9qi.U..LF.. +| 1424: 84 14 80 f3 2b ba 43 15 7e 12 44 4b 38 2e 01 a8 ....+.C.~.DK8... +| 1440: 2e 58 11 e1 dd 6b 24 9e 6a fb 21 14 f5 ae be 7a .X...k$.j.!....z +| 1456: 9b 26 0d a8 2f 29 8c 5a 63 8a cf 58 36 e1 76 fb .&../).Zc..X6.v. +| 1472: ca 95 7a 0c 74 0d d5 57 04 a4 65 5f 2a 0d 46 ee ..z.t..W..e_*.F. +| 1488: 0c 2e a6 a1 dd 04 b8 1d b7 72 a0 d5 ad cc 8e d3 .........r...... +| 1504: bb 04 bc 39 4d 22 0f 4e 6c b1 4d b4 08 3b 7f a5 ...9M..Nl.M..;.. +| 1520: f1 7d 18 c5 bf 43 86 b3 c1 3b 85 6f 30 84 1a 7b .....C...;.o0... +| 1536: 17 d0 91 d3 6d 99 cc ff ac 64 88 53 d3 ad 1e 5f ....m....d.S..._ +| 1552: ba 4c af 64 80 ae ca c7 27 56 7e 41 02 61 f1 d2 .L.d....'V~A.a.. +| 1568: e4 4b 99 7c e4 18 41 9c f7 b9 e8 5a 3f 6e a3 57 .K.|..A....Z?n.W +| 1584: ca 18 c5 8a a8 39 c6 fe 02 d0 9d 26 37 42 07 3d .....9.....&7B.= +| 1600: 38 4e fe 9a 3b 54 39 20 23 53 8a 84 2f 4a 06 06 8N..;T9 #S../J.. +| 1616: ed 56 dd d8 bf 56 ef ca a5 c0 a4 aa d5 88 41 42 .V...V........AB +| 1632: 8a c0 37 65 f3 c8 4c 87 a5 f3 3d 99 78 2b d7 4e ..7e..L...=.x+.N +| 1648: d7 6e 51 28 3f 5c 93 cb 56 08 91 39 8e 1d fb 26 .nQ(?...V..9...& +| 1664: 5d 80 7c 44 59 c4 d4 b3 5e 0c c1 3f 85 f8 d6 d0 ].|DY...^..?.... +| 1680: 25 0f a8 c4 40 a5 f7 63 ec 2b fc 78 e6 b4 7c 72 %...@..c.+.x..|r +| 1696: 87 f0 6d 2c 00 63 dc 29 4a e5 b5 6b 9e 73 33 b4 ..m,.c.)J..k.s3. +| 1712: 19 03 1a 5c de 8f 98 fa ce 4d e3 1a 62 6b 5b f5 .........M..bk[. +| 1728: 60 d6 4c 13 39 14 06 83 90 09 56 8b 71 3b b9 bc `.L.9.....V.q;.. +| 1744: e3 7e 5e ae f5 3e b7 aa bd 73 d6 f1 47 4a 84 60 .~^..>...s..GJ.` +| 1760: 20 d7 93 ce f0 f2 1a 63 b7 f0 7e 3b 4e 36 1c dd ......c..~;N6.. +| 1776: cc ef 50 7a a9 90 f7 48 05 fb 78 e8 72 71 df 3a ..Pz...H..x.rq.: +| 1792: 41 51 2c 4c 5d 0f cd 51 0e f3 5a a1 e6 81 15 b3 AQ,L]..Q..Z..... +| 1808: bb 48 5e 13 cf 46 4c f1 26 47 4b 51 87 d7 39 a6 .H^..FL.&GKQ..9. +| 1824: 38 c5 72 52 55 97 f6 81 bc 0f a1 95 72 b8 ec 6d 8.rRU.......r..m +| 1840: dd 6d 92 03 b5 0f d6 fd 7e 29 c9 55 50 57 71 2c .m......~).UPWq, +| 1856: 34 35 21 75 6a 4e e6 f0 6a 99 b7 51 b5 3e 0a 6c 45!ujN..j..Q.>.l +| 1872: 1b c0 ba cb 92 90 15 b8 35 b9 6b 78 f2 c6 03 48 ........5.kx...H +| 1888: 66 d6 2e 75 47 b8 eb d0 30 48 c9 4d 67 d1 c1 8a f..uG...0H.Mg... +| 1904: b2 9c a9 c0 3d a1 77 e3 35 e4 85 01 e7 dc 74 bc ....=.w.5.....t. +| 1920: 8d ff f6 f0 ad e1 35 63 75 6d ee 28 53 29 1c 9c ......5cum.(S).. +| 1936: 67 dd ea 7d 1f af 87 c3 2e 8d a4 23 d2 6b db 49 g..........#.k.I +| 1952: 1a 36 10 7b e1 6c 9a 1c a2 5f 17 fb 43 e5 da f5 .6...l..._..C... +| 1968: 2c 60 86 42 1d 28 a1 5f cc 73 b2 5d 69 2b fa 18 ,`.B.(._.s.]i+.. +| 1984: 85 a2 de 30 9a c7 08 42 b9 e3 b1 a1 32 1d 70 31 ...0...B....2.p1 +| 2000: b4 7a cf f4 57 5d 5e 45 53 1d 79 35 7b 4f 9a 2f .z..W]^ES.y5.O./ +| 2016: 80 11 76 23 60 dc 86 e7 f0 74 2d 46 51 40 01 10 ..v#`....t-FQ@.. +| 2032: 13 69 90 a9 fb cb 66 8e 3f e0 a1 4e 99 eb 61 1b .i....f.?..N..a. +| 2048: fe c7 5e b5 f5 0f a3 46 64 19 09 11 c0 83 c7 28 ..^....Fd......( +| 2064: 41 20 81 d3 f5 9c 21 2b ed 06 1a 4a 89 4d 6a e5 A ....!+...J.Mj. +| 2080: 2d 4f 95 d7 3b 95 8c 59 6f 3e 79 51 5f ef b8 2a -O..;..Yo>yQ_..* +| 2096: 43 ef 07 e2 d1 d1 13 38 67 54 88 e2 6f 03 fe 05 C......8gT..o... +| 2112: 10 0e e8 e9 4e 9a 75 92 ec 1f e7 56 61 3b 54 43 ....N.u....Va;TC +| 2128: 08 af e9 d5 56 bc 87 a3 25 6f e2 b8 01 62 1a 30 ....V...%o...b.0 +| 2144: ba 26 8a b1 9a 7e 44 a2 f5 d4 75 2e a0 d0 b8 71 .&...~D...u....q +| 2160: 61 61 92 ff 32 0b a8 94 a5 81 80 d0 3b b5 51 4a aa..2.......;.QJ +| 2176: 01 e2 8e 0a 38 90 19 f7 b4 38 b0 9c 32 e1 6a f0 ....8....8..2.j. +| 2192: f8 7b f0 58 0a d1 19 c0 20 d6 54 fe 28 ac 02 64 ...X.... .T.(..d +| 2208: c4 33 55 01 d2 bd 01 51 87 01 0c 66 bb 6e 1b 94 .3U....Q...f.n.. +| 2224: 9c 24 50 40 5b 2f 64 f9 b5 b6 6b 15 fd f8 e7 05 .$P@[/d...k..... +| 2240: 37 92 95 d3 b4 1e be b3 09 c0 74 f8 ca 03 10 89 7.........t..... +| 2256: 2d 4d f5 56 0f 6e 20 72 a7 5e 1d 9f fd d5 43 af -M.V.n r.^....C. +| 2272: 2e 7b f7 91 99 ed 47 ea c7 a4 76 82 ca 5f 94 20 ......G...v.._. +| 2288: 52 01 b4 21 cb 50 d5 f2 d6 cf 1d 11 0f b2 07 ee R..!.P.......... +| 2304: f2 cf 2b 52 7b 1d e0 be 16 a1 cf 06 52 1b 33 f5 ..+R........R.3. +| 2320: c2 8e ac f1 00 2b 82 cf b4 ae d5 f0 9b 4b 11 14 .....+.......K.. +| 2336: b5 c5 43 f4 08 9d 4a 59 ba a6 52 67 fe e1 bd 2d ..C...JY..Rg...- +| 2352: c2 40 c6 3e d1 8a d1 f5 a0 b4 d0 b1 92 3f 9e 1d .@.>.........?.. +| 2368: 18 5e b4 78 30 45 57 29 30 e1 5a bb ee 7b 1e 99 .^.x0EW)0.Z..... +| 2384: 0d 10 ea 14 7b 5e 36 32 8e e1 a7 e4 76 19 48 a8 .....^62....v.H. +| 2400: c6 19 74 b9 4e 31 81 24 fe 07 e3 86 ec c4 1d 05 ..t.N1.$........ +| 2416: 1f 5a 52 14 0c 08 3f d1 93 fe b1 46 27 87 58 21 .ZR...?....F'.X! +| 2432: 4b d5 ff bb 98 d8 f8 0c 96 66 02 9c 86 f9 0e 0b K........f...... +| 2448: f0 11 7a 99 39 52 38 84 22 04 58 07 f1 95 eb c7 ..z.9R8...X..... +| 2464: 44 2e a5 fe d4 68 a9 98 77 14 4f 8a 44 4c 7d c4 D....h..w.O.DL.. +| 2480: 49 8a d7 89 83 8e e6 1e d0 af b7 41 3d 1a 85 14 I..........A=... +| 2496: ec 3a e5 1b 2c c5 17 77 85 82 19 57 37 94 93 7e .:..,..w...W7..~ +| 2512: 52 16 a3 dd 0a fd 57 1a 57 32 11 4d 71 e3 4b 1b R.....W.W2.Mq.K. +| 2528: c5 02 d7 89 74 85 b0 3d 8d 7b 53 a2 d6 60 99 d4 ....t..=..S..`.. +| 2544: ce f0 1c 3d a3 aa db db c4 80 38 7a cb 12 7e 66 ...=......8z..~f +| 2560: 3f 69 af fa 57 49 35 05 94 33 df fe 91 8a 25 3d ?i..WI5..3....%= +| 2576: 9b 32 71 72 d2 bc bc 23 61 69 9c 68 a7 58 c0 f1 .2qr...#ai.h.X.. +| 2592: 0e 20 a9 d3 d2 a9 11 d7 ee 52 46 70 b7 aa 6b f3 . .......RFp..k. +| 2608: 4a 51 a7 a5 26 92 35 44 f9 cc 7b c7 ec db 5d b6 JQ..&.5D......]. +| 2624: 5c 88 d9 bd 14 df a0 14 35 09 2f c8 76 d4 4c 19 ........5./.v.L. +| 2640: 12 29 b9 dd 9b 21 ed b8 ee 1f f9 38 05 9e 93 aa .)...!.....8.... +| 2656: ab 82 15 69 88 81 f6 4f 1b 72 bb 84 cb 9c 33 ec ...i...O.r....3. +| 2672: 94 4d 44 42 8e 8f 12 91 1f 32 07 09 38 8b 44 be .MDB.....2..8.D. +| 2688: 9e 31 49 9e 76 04 d8 b7 69 ad f1 59 81 5f d7 a0 .1I.v...i..Y._.. +| 2704: 2f 34 94 27 b4 c1 e9 f0 18 a7 43 7e 1e fd 27 5b /4.'......C~..'[ +| 2720: d8 e9 c3 5d be 8f 91 f2 4a cd 33 5f 6c 76 f6 f1 ...]....J.3_lv.. +| 2736: 17 ae 80 87 e7 ec 22 ef 73 8e a7 3a 30 dd 27 3d ........s..:0.'= +| 2752: 6d 95 59 eb f3 7f 97 b7 b9 8d ff 86 ed dd 5d f4 m.Y...........]. +| 2768: 39 3c 6a 13 3d 7a 93 3d 37 ed 8c d6 98 0f 0b 7a 9.....p..$o..... +| 3040: e1 6a a9 0a d8 f6 89 09 51 98 1f 89 1b f1 34 87 .j......Q.....4. +| 3056: a5 b3 22 d0 65 53 bd ae 57 7a 8a 8f a8 a6 10 9e ....eS..Wz...... +| 3072: 72 7c 6e 37 8f 67 db d8 89 54 77 87 6d 63 6e 31 r|n7.g...Tw.mcn1 +| 3088: ef ae 41 51 22 cc 24 08 89 f6 dd 2c f9 cb a4 f8 ..AQ..$....,.... +| 3104: ea f9 42 33 01 fd cf b9 d6 73 aa b4 9d 45 31 eb ..B3.....s...E1. +| 3120: 42 ca df 3a d2 3c c9 41 28 fd e4 a2 2f cf bf ca B..:.<.A(.../... +| 3136: 63 94 a2 74 ee 6b 4c 62 bb 74 5f cc 39 68 b5 e9 c..t.kLb.t_.9h.. +| 3152: 68 47 90 45 85 f0 20 4e 3a fe 4a 4f ab f2 fe c7 hG.E.. N:.JO.... +| 3168: f5 23 56 6e 09 9e c6 3d 36 30 82 62 6f 5f 78 f6 .#Vn...=60.bo_x. +| 3184: f6 07 58 e8 fd 98 09 e5 a5 5b 65 27 43 e6 9e 3d ..X......[e'C..= +| 3200: 98 d5 db 1d 09 35 48 b0 cd 5e 53 a1 d6 b2 4f 85 .....5H..^S...O. +| 3216: e5 4d 80 18 8f 78 6e a0 0e 35 08 7d 1d 5d 3e ab .M...xn..5...]>. +| 3232: c3 5c dc ec c9 e0 22 1a 17 a3 80 40 3d e7 66 df ...........@=.f. +| 3248: b2 38 f4 59 6d 20 e8 83 93 53 8c ac 52 26 ec 60 .8.Ym ...S..R&.` +| 3264: f8 50 85 1c 97 9c ae a3 9f bd af 75 be 73 23 b5 .P.........u.s#. +| 3280: 7f fa 1f 7f 28 16 c7 7c 5f 0a 5b 32 1a c8 45 cf ....(..|_.[2..E. +| 3296: df 2e 8e d1 d9 59 76 d5 6c 8d b5 12 8a c5 77 32 .....Yv.l.....w2 +| 3312: d4 87 02 80 09 b6 43 34 76 09 f9 b4 74 66 ce ee ......C4v...tf.. +| 3328: 26 77 62 11 b0 23 92 5c 72 38 41 e9 70 4f b5 83 &wb..#..r8A.pO.. +| 3344: e4 35 7c b2 2a 38 49 12 48 18 1c 95 5c b1 18 1a .5|.*8I.H....... +| 3360: 51 cd 4b 7b 22 94 0e c7 da c1 30 97 d1 be 42 07 Q.K.......0...B. +| 3376: 94 01 a5 fd 2f 0d 2c 53 33 c0 91 c6 bc af 2c f2 ..../.,S3.....,. +| 3392: f2 07 6e 4a d2 22 3e 3c 18 3c ca 24 bf 42 78 7a ..nJ..><.<.$.Bxz +| 3408: 69 0e a9 12 e3 20 fa 8b ad 75 27 0c c3 82 84 8d i.... ...u'..... +| 3424: 46 af 3e 1e 89 27 4d 7e f7 21 96 b4 6c 17 7f 19 F.>..'M~.!..l... +| 3440: 8a 78 d7 bb 40 67 35 45 2d d4 97 6a 4c e9 4a 58 .x..@g5E-..jL.JX +| 3456: 22 a3 bb 31 d5 4f 26 40 cc fe c8 cd 1d a4 0d 67 ...1.O&@.......g +| 3472: 14 13 05 4d 0e 15 40 ea 7d 62 c6 80 08 b9 f6 b2 ...M..@..b...... +| 3488: 44 58 66 d5 ca b6 f4 20 b0 4a b8 37 64 3d b0 a7 DXf.... .J.7d=.. +| 3504: a7 87 70 26 b2 ea f9 cf 98 03 6e 63 5a fe c4 cd ..p&......ncZ... +| 3520: 80 cb ca f4 a6 02 11 39 4f 6c bf bf a4 8e 99 32 .......9Ol.....2 +| 3536: e3 47 51 3e 85 f6 84 6b 3d 9a fe 2f 96 18 49 2a .GQ>...k=../..I* +| 3552: dc a4 56 77 d1 3f 94 61 2b 58 e7 74 ee c9 16 7b ..Vw.?.a+X.t.... +| 3568: 6f 76 47 da b2 fb 89 75 80 78 05 69 c8 3e f0 97 ovG....u.x.i.>.. +| 3584: 1b 40 f1 48 43 7f cd 0b b1 c6 cf 59 73 4f 3f 33 .@.HC......YsO?3 +| 3600: 2e 32 d2 b6 69 fc 6b f2 20 0f 12 bf f0 98 2e e2 .2..i.k. ....... +| 3616: 4f ed 80 27 00 e2 b1 c2 ca cd 6b de e1 b4 af 9e O..'......k..... +| 3632: df 4f d2 da 6d 9f b4 5d 99 4a c4 59 d4 e1 98 05 .O..m..].J.Y.... +| 3648: 68 00 a5 72 3f 0e 35 29 59 81 8a f9 f2 c0 5a de h..r?.5)Y.....Z. +| 3664: 25 35 d5 60 03 f7 0f 20 3a bb a6 45 fc cc 2d e9 %5.`... :..E..-. +| 3680: fe 37 0b 6c f2 96 19 dc 39 62 6e f7 9c 17 37 8c .7.l....9bn...7. +| 3696: 8a dd 62 a2 a3 e6 b8 a0 cd 61 f5 27 3b 29 e2 f4 ..b......a.';).. +| 3712: 0a 06 21 c3 b9 8e 70 34 59 ca d9 a1 92 db a9 74 ..!...p4Y......t +| 3728: d1 7c a1 ef 64 c2 75 51 02 b3 6a 57 6d 12 f6 eb .|..d.uQ..jWm... +| 3744: 3b 41 ce a5 ca 69 62 3e c3 55 eb 1c 80 01 79 05 ;A...ib>.U....y. +| 3760: a4 51 7c 45 00 76 41 5e 57 1a 88 aa 15 c8 42 82 .Q|E.vA^W.....B. +| 3776: d5 83 74 97 93 b4 28 df 4d 82 7d 6f ef 6a d4 e4 ..t...(.M..o.j.. +| 3792: 03 e7 20 4a e8 84 54 0c 6e 5f 8e b0 d1 0d 67 aa .. J..T.n_....g. +| 3808: f9 20 21 ea c0 4e 5c e3 f8 65 d7 67 0a 7a e0 0b . !..N...e.g.z.. +| 3824: d8 1e 53 95 bf 24 39 12 d4 30 8c 2c b4 13 2e bd ..S..$9..0.,.... +| 3840: f8 81 d5 15 35 d7 3f f2 23 a4 2c 1b cd 29 e8 88 ....5.?.#.,..).. +| 3856: a8 f1 cc 3f 9b 72 d0 8e c0 cb 80 ca c9 68 1a ca ...?.r.......h.. +| 3872: 38 09 59 e5 a6 33 95 57 55 c9 dd c4 8a 8d 36 e5 8.Y..3.WU.....6. +| 3888: 0c 40 95 77 63 4b 82 53 98 d0 bd cd 57 b6 f7 2b .@.wcK.S....W..+ +| 3904: fe 1c 25 d4 95 3d 4c 63 b7 fb 94 37 3f 6d 96 28 ..%..=Lc...7?m.( +| 3920: 1d 13 76 d2 ab 9a 7e 8f f5 a2 e6 15 0b 1c 10 14 ..v...~......... +| 3936: c3 b5 c0 1c 52 af 2a 32 35 05 4a 0c f1 cb ed 5e ....R.*25.J....^ +| 3952: 42 6c 31 8b dc 78 4b 68 5a b7 0f e3 6c d6 da e6 Bl1..xKhZ...l... +| 3968: 59 f3 34 43 47 1c 2b f6 f4 eb 6e 12 4d 60 f4 d9 Y.4CG.+...n.M`.. +| 3984: 39 2f 84 a5 8f f0 03 e7 ce b6 10 d0 a9 f9 69 76 9/............iv +| 4000: 94 29 2e 60 db fc e7 5f e6 c5 5b b9 c5 a7 1f 95 .).`..._..[..... +| 4016: 0d 66 aa 55 7b f5 ec 67 c6 f7 26 52 c4 02 60 31 .f.U...g..&R..`1 +| 4032: 7d 5e de ba 06 2f 09 1d a1 db 61 83 c8 13 be 98 .^.../....a..... +| 4048: 14 9d c9 15 1e 33 04 de a5 53 ed 42 ab 45 b1 f3 .....3...S.B.E.. +| 4064: 09 a7 47 54 14 bc a0 88 80 58 94 0a 78 f2 31 3d ..GT.....X..x.1= +| 4080: bc 0d 85 bf da a0 7e d2 be e4 1c cc b4 45 a8 bd ......~......E.. +| page 19 offset 73728 +| 0: 0d 0f e6 00 03 0c 8a 00 0c 8a 0f ec 0c 94 00 00 ................ +| 3200: 00 00 00 00 00 00 00 00 00 00 08 01 03 00 16 2e ................ +| 3216: b1 7d 24 24 86 4a 84 80 80 80 80 01 04 00 8d 18 ..$$.J.......... +| 3232: 00 00 03 2b 02 30 30 01 02 06 01 02 06 01 02 06 ...+.00......... +| 3248: 1f 02 03 01 02 03 01 02 03 01 08 32 30 31 36 30 ...........20160 +| 3264: 36 30 39 01 02 07 01 02 07 01 02 07 01 01 34 00 609...........4. +| 3280: 02 05 01 02 05 01 02 05 01 01 35 01 02 04 01 02 ..........5..... +| 3296: 04 01 02 04 02 07 30 30 30 30 30 30 30 1c 02 3d ......0000000..= +| 3312: 01 02 04 01 02 04 01 06 62 69 6e 61 72 79 03 06 ........binary.. +| 3328: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3344: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3360: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 ................ +| 3376: 03 06 01 02 02 03 06 01 02 02 01 08 63 6f 6d 70 ............comp +| 3392: 69 6c 65 72 01 02 02 01 02 02 01 02 02 01 06 64 iler...........d +| 3408: 62 73 74 61 74 07 02 03 01 02 03 01 02 03 02 04 bstat........... +| 3424: 65 62 75 67 04 02 02 01 02 02 01 02 02 01 07 65 ebug...........e +| 3440: 6e 61 62 6c 65 07 02 02 01 02 02 01 d3 02 01 02 nable........... +| 3456: 02 01 02 02 02 02 02 01 02 02 01 02 02 01 02 02 ................ +| 3472: 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 ................ +| 3488: 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 ................ +| 3504: 02 01 02 02 02 08 78 74 65 6e 73 69 6f 6e 1f 02 ......xtension.. +| 3520: 04 01 02 04 01 02 04 01 04 66 74 73 34 0a 02 03 .........fts4... +| 3536: 01 02 03 01 02 03 04 01 35 0d 02 03 01 02 03 01 ........5....... +| 3552: 02 03 01 03 67 63 63 01 02 54 01 02 03 01 02 03 ....gcc..T...... +| 3568: 02 06 65 6f 70 6f 6c 79 10 02 03 01 02 03 01 02 ..eopoly........ +| 3584: 03 01 05 6a 73 6f 6e 31 13 02 03 01 02 03 01 02 ...json1........ +| 3600: 03 01 04 6c 6f 61 64 1f 02 03 01 02 03 01 02 03 ...load......... +| 3616: 01 03 6d 61 78 1c 02 02 01 02 02 01 02 02 02 05 ..max........... +| 3632: 65 6d 6f 72 79 1c 02 03 01 02 03 01 02 03 04 04 emory........... +| 3648: 73 79 73 35 16 02 03 01 02 03 01 02 03 01 06 6e sys5...........n +| 3664: 6f 63 61 73 65 02 06 01 02 02 03 06 01 02 02 03 ocase........... +| 3680: 06 01 02 02 13 06 01 02 02 03 06 01 02 02 03 06 ................ +| 3696: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3712: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3728: 02 01 04 6f 6d 69 74 1f 02 02 01 02 02 01 02 02 ...omit......... +| 3744: 01 05 72 74 72 65 65 19 02 03 01 02 03 01 02 03 ..rtree......... +| 3760: 04 02 69 6d 01 06 01 02 02 03 06 01 02 02 03 06 ..im............ +| 3776: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3792: 02 01 f3 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3808: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 08 ................ +| 3824: 01 0a 74 68 72 65 61 64 73 61 66 65 22 02 02 01 ..threadsafe.... +| 3840: 02 02 01 02 02 01 04 76 74 61 62 07 02 04 01 02 .......vtab..... +| 3856: 04 01 02 04 01 01 78 01 06 01 01 02 01 06 01 01 ......x......... +| 3872: 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 ................ +| 3888: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 3904: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 3920: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 ................ +| 3936: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 ................ +| 3952: 02 01 06 01 01 02 01 06 01 01 02 ad 06 01 01 02 ................ +| 3968: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 3984: 06 01 01 01 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 4000: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 ................ +| 4016: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 ................ +| 4032: 02 01 06 01 01 02 01 06 01 01 02 04 15 13 0c 0c ................ +| 4048: 12 44 13 11 0f 47 13 0e fc 0e 11 10 0f 0e 10 0f .D...G.......... +| 4064: 44 0f 10 40 15 0f 00 00 00 06 14 24 12 0a 03 00 D..@.......$.... +| 4080: 2a 00 00 00 00 01 02 08 00 02 01 01 01 02 01 07 *............... +| page 20 offset 77824 +| 0: 0d 00 00 00 01 00 22 00 00 22 00 00 00 00 00 00 ................ +| 32: 00 00 9f 56 88 80 80 80 80 01 04 00 bf 30 00 00 ...V.........0.. +| 48: 0e 9c 02 30 30 01 0c 74 81 44 06 81 0b 01 0a 81 ...00..t.D...... +| 64: 03 58 58 04 01 10 82 66 10 0f 0b 81 14 24 01 0c .XX....f.....$.. +| 80: 81 14 47 4a 20 6e 01 0e 71 81 41 05 82 0f 03 01 ..GJ n..q.A..... +| 96: 0c 53 54 4c 2c 70 7d 01 12 81 43 06 1e 37 42 37 .STL,p....C..7B7 +| 112: 77 3e 01 14 81 22 07 81 30 81 5d 03 05 0d 01 08 w>......0.]..... +| 128: 82 01 81 21 01 0e 81 45 54 51 81 0a 61 02 01 30 ...!...ETQ..a..0 +| 144: 01 04 82 6e 02 01 36 08 04 83 7e 02 01 38 02 04 ...n..6...~..8.. +| 160: 83 4f 02 01 62 06 04 81 11 03 01 36 08 04 81 5b .O..b......6...[ +| 176: 03 01 64 02 04 84 5d 02 01 63 05 04 81 02 02 01 ..d...]..c...... +| 192: 64 07 04 81 67 02 01 65 0a 04 83 0c 03 02 70 77 d...g..e......pw +| 208: 04 04 83 70 02 02 66 79 08 04 82 25 02 01 68 0a ...p..fy...%..h. +| 224: 04 81 34 02 01 6b 03 02 4d 03 04 84 32 02 01 70 ..4..k..M...2..p +| 240: 01 04 81 15 02 01 73 07 04 83 4d 02 02 37 02 01 ......s...M..7.. +| 256: 74 02 02 40 03 01 68 07 04 83 45 02 02 76 69 09 t..@..h...E..vi. +| 272: 04 82 66 03 01 6c 09 04 82 34 02 01 79 01 04 84 ..f..l...4..y... +| 288: 13 02 02 c2 ba 0a 04 81 68 03 01 bc 0a 04 83 0d ........h....... +| 304: 02 04 ca 80 69 6c 09 04 81 5a 02 02 d3 84 05 04 ....il...Z...... +| 320: 83 0a 01 01 31 01 16 18 1a 30 81 4c 31 12 54 81 ....1....0.L1.T. +| 336: 00 1d 01 0e 02 25 82 4b 1d 3f 76 01 0c 43 79 4b .....%.K.?v..CyK +| 352: 5b 0b 76 01 0e 35 1a 81 08 82 77 10 01 0c 81 24 [.v..5....w....$ +| 368: 18 07 74 6f 01 08 29 81 08 69 01 10 81 0e 0d 4a ..to..)..i.....J +| 384: 81 01 07 58 01 08 6b 51 76 7a 01 0c 20 81 18 6c ...X..kQvz.. ..l +| 400: 2c 18 01 08 83 7c 7b 0e 02 01 32 08 02 48 03 04 ,....|....2..H.. +| 416: f3 a1 97 a5 03 04 83 71 02 01 33 02 04 81 2f 03 .......q..3.../. +| 432: 01 66 09 04 83 15 02 03 61 ca b1 08 04 81 48 02 .f......a.....H. +| 448: 01 63 01 02 1c 02 03 65 c2 aa 06 02 5a 02 01 68 .c.....e....Z..h +| 464: 03 04 81 6e 02 01 69 0a 04 82 5a 03 01 69 08 04 ...n..i...Z..i.. +| 480: 81 65 02 01 6a 02 02 64 06 04 84 12 02 01 6c 01 .e..j..d......l. +| 496: 02 1f 02 01 6e 03 04 83 24 02 03 6f c2 b3 07 04 ....n...$..o.... +| 512: 84 52 02 03 72 c9 9b 04 04 82 6e 02 01 73 07 04 .R..r.....n..s.. +| 528: 82 10 02 01 77 0a 02 31 02 01 79 07 04 81 7f 02 ....w..1..y..... +| 544: 02 7a 62 09 04 83 49 02 02 c2 b9 02 04 83 01 03 .zb...I......... +| 560: 02 bd 75 09 04 83 45 02 02 c6 aa 02 04 83 08 02 ..u...E......... +| 576: 03 d7 92 6a 08 02 04 01 01 32 01 0c 81 7d 72 81 ...j.....2....r. +| 592: 4e 36 01 0e 1a 81 10 81 10 11 2e 01 08 78 49 82 N6...........xI. +| 608: 3b 01 0e 73 81 64 05 1b 81 69 01 1a 18 06 7e 2c ;..s.d...i....~, +| 624: 21 12 20 5a 28 69 06 1e 06 01 0c 81 48 05 31 81 !. Z(i......H.1. +| 640: 24 01 14 25 81 42 3e 81 16 81 37 13 13 01 0c 21 $..%.B>...7....! +| 656: 0d 0f 3e 82 03 01 08 6b 60 81 27 01 10 2d 81 41 ..>....k`.'..-.A +| 672: 06 69 81 08 30 02 01 32 03 04 81 01 02 01 33 08 .i..0..2......3. +| 688: 02 3c 02 01 35 0a 04 84 64 02 01 38 05 04 83 41 .<..5...d..8...A +| 704: 02 02 61 69 09 04 82 49 02 03 66 c8 ab 09 04 82 ..ai...I..f..... +| 720: 68 02 01 68 04 04 83 27 02 01 6c 04 04 84 18 03 h..h...'..l..... +| 736: 02 72 6e 06 04 82 30 02 01 6e 04 04 84 55 03 01 .rn...0..n...U.. +| 752: 6d 05 04 83 30 03 03 76 c2 bd 09 02 57 02 01 71 m...0..v....W..q +| 768: 02 04 84 22 03 04 84 11 03 05 72 f0 92 bc bc 0a ..........r..... +| 784: 04 82 40 02 01 72 04 04 83 5b 02 01 73 05 02 6f ..@..r...[..s..o +| 800: 03 04 83 6f 02 01 75 04 02 55 02 01 78 03 04 82 ...o..u..U..x... +| 816: 56 02 01 7a 06 04 85 01 03 02 25 02 02 c2 b2 07 V..z......%..... +| 832: 04 83 7a 03 03 b3 ce bc 03 04 84 2b 03 03 bc c2 ..z........+.... +| 848: b2 09 04 83 53 02 04 ce a2 38 6e 07 04 82 23 03 ....S....8n...#. +| 864: 01 bc 08 04 83 7f 02 03 d2 b7 69 0a 04 84 19 02 ..........i..... +| 880: 02 d6 84 09 04 81 0e 01 01 33 01 06 2a 83 69 01 .........3..*.i. +| 896: 18 0f 0e 25 42 81 71 81 21 17 07 03 0d 01 1a 15 ...%B.q.!....... +| 912: 13 42 03 81 40 45 21 60 4d 1a 04 0e 01 0a 83 18 .B..@E!`M....... +| 928: 81 11 0a 01 08 64 83 24 24 01 16 62 1d 0c 06 62 .....d.$$..b...b +| 944: 50 2c 73 17 15 4d 01 0e 1b 82 15 4d 31 3a 4f 01 P,s..M.....M1:O. +| 960: 0e 22 63 51 81 7f 81 22 01 0e 21 81 0a 3e 42 82 ..cQ......!..>B. +| 976: 08 01 12 05 82 0a 33 19 10 81 37 63 02 01 32 05 ......3...7c..2. +| 992: 04 82 3c 02 01 36 02 04 84 0f 02 01 38 06 04 84 ..<..6......8... +| 1008: 1f 02 01 39 06 04 82 06 02 01 61 08 04 81 00 03 ...9......a..... +| 1024: 03 33 6f 62 01 02 69 02 01 65 02 04 84 05 07 04 .3ob..i..e...... +| 1040: 84 0e 02 01 67 09 04 82 44 02 01 6a 07 02 55 02 ....g...D..j..U. +| 1056: 01 6b 02 04 84 04 07 04 84 44 02 01 6c 06 04 84 .k.......D..l... +| 1072: 0d 02 01 6e 06 04 84 10 03 01 77 03 02 46 02 01 ...n......w..F.. +| 1088: 74 08 04 83 33 03 01 77 03 04 81 5b 02 01 75 0a t...3..w...[..u. +| 1104: 04 81 75 02 01 78 01 04 83 48 03 02 c2 b2 02 04 ..u..x...H...... +| 1120: 81 77 02 04 c2 b3 c2 b3 09 02 1b 02 06 f2 98 9e .w.............. +| 1136: a2 61 6c 08 04 83 31 01 01 34 01 0e 81 1c 82 11 .al...1..4...... +| 1152: 4e 81 06 01 10 4a 12 64 39 18 81 24 72 01 04 81 N....J.d9..$r... +| 1168: 0b 01 10 81 08 81 50 48 2a 79 15 01 14 13 72 07 ......PH*y....r. +| 1184: 2f 42 81 21 50 81 02 01 10 6d 46 3e 81 0c 81 17 /B.!P....mF>.... +| 1200: 23 01 08 0a 5f 83 38 01 0a 57 81 3e 82 0f 01 10 #..._.8..W.>.... +| 1216: 81 07 81 7e 0c 81 14 1d 01 08 1f 82 42 4e 02 01 ...~........BN.. +| 1232: 30 01 04 81 36 04 04 81 39 03 02 c2 bd 02 04 81 0...6...9....... +| 1248: 1e 02 01 31 05 04 84 31 02 01 35 0a 04 81 2b 02 ...1...1..5...+. +| 1264: 06 37 c2 b9 eb a0 86 04 04 83 0d 02 02 61 6f 04 .7...........ao. +| 1280: 04 83 39 02 01 63 07 04 83 3c 03 01 67 0a 04 84 ..9..c...<..g... +| 1296: 6a 02 01 65 07 06 68 82 7d 02 03 66 c2 be 07 04 j..e..h....f.... +| 1312: 81 3d 02 01 67 08 02 58 01 04 83 16 02 01 6c 03 .=..g..X......l. +| 1328: 04 81 73 02 01 6d 08 04 81 42 02 01 6e 04 04 83 ..s..m...B..n... +| 1344: 75 02 03 6f 65 32 06 04 84 4f 02 01 70 05 04 81 u..oe2...O..p... +| 1360: 56 03 01 77 09 04 81 47 02 02 72 32 05 02 0b 02 V..w...G..r2.... +| 1376: 01 73 01 02 47 04 04 84 09 02 01 74 07 04 85 05 .s..G......t.... +| 1392: 02 01 75 01 02 5b 02 04 76 e5 b9 83 07 04 82 03 ..u..[..v....... +| 1408: 02 01 78 01 04 81 11 02 02 c6 bf 02 04 81 3b 02 ..x...........;. +| 1424: 02 ce bc 06 04 84 3a 02 03 e1 83 93 03 04 81 2b ......:........+ +| 1440: 01 01 35 01 0e 81 7b 61 23 71 55 0c 01 08 82 30 ..5....a#qU....0 +| 1456: 2a 7b 01 0c 81 45 81 20 72 38 01 0c 29 82 25 55 *....E. r8..).%U +| 1472: 4c 21 01 0c 81 53 38 25 3a 63 01 08 83 55 81 0f L!...S8%:c...U.. +| 1488: 01 0e 81 32 5b 81 1b 31 6f 01 16 1e 6d 17 0e 17 ...2[..1o...m... +| 1504: 1f 6b 0b 3e 3d 3a 01 12 32 81 4a 81 04 55 47 20 .k.>=:..2.J..UG +| 1520: 09 01 10 82 4b 81 0a 17 3e 05 2a 02 01 31 08 04 ....K...>.*..1.. +| 1536: 84 04 02 02 36 68 06 02 18 02 01 61 02 04 84 3d ....6h.....a...= +| 1552: 08 04 81 38 03 01 34 01 02 71 02 03 63 75 6d 0a ...8..4..q..cum. +| 1568: 04 81 3a 02 01 64 09 02 52 01 04 82 48 02 01 68 ..:..d..R...H..h +| 1584: 08 02 30 02 04 83 4e 02 01 6e 01 02 42 02 05 6f ..0...N..n..B..o +| 1600: 63 6c dc ab 08 02 11 03 04 67 69 c2 be 03 04 82 cl.......gi..... +| 1616: 29 02 01 71 07 04 82 12 02 01 79 06 04 84 1b 02 )..q......y..... +| 1632: 02 7a 33 09 04 83 4c 02 02 c2 b2 05 04 84 17 03 .z3...L......... +| 1648: 01 b9 09 02 35 04 02 6b 78 0a 04 81 31 03 01 bc ....5..kx...1... +| 1664: 0a 02 41 02 08 cb 8a f0 9c bb a1 67 68 02 04 81 ..A........gh... +| 1680: 75 01 01 36 01 10 55 77 30 36 21 31 69 60 01 0e u..6..Uw06!1i`.. +| 1696: 3b 0b 3e 4e 1c 2c 1b 01 16 44 32 54 07 2f 0e 81 ;.>N.,...D2T./.. +| 1712: 11 81 03 14 01 04 81 63 01 0e 81 16 82 5c 15 20 .......c....... +| 1728: 19 01 12 0d 1f 43 6e 39 06 18 81 54 01 0c 81 40 .....Cn9...T...@ +| 1744: 2c 81 0f 4d 01 10 16 82 5e 0e 08 0d 78 51 01 12 ,..M....^...xQ.. +| 1760: 1d 81 73 81 22 29 17 66 1a 01 0a 81 3f 81 3f 19 ..s..).f....?.?. +| 1776: 02 01 30 0a 04 83 47 03 01 6d 05 04 82 3e 02 01 ..0...G..m...>.. +| 1792: 32 07 04 81 5e 03 04 82 19 02 04 34 71 6c 38 01 2...^......4ql8. +| 1808: 04 83 6b 02 02 37 67 08 04 84 53 02 01 64 04 02 ..k..7g...S..d.. +| 1824: 7b 03 01 71 02 04 81 71 02 01 65 01 04 83 66 05 ...q...q..e...f. +| 1840: 04 84 5c 02 01 69 09 04 81 3a 02 01 6a 03 04 83 .....i...:..j... +| 1856: 35 02 01 6b 06 04 83 6d 01 04 83 5f 02 04 84 0f 5..k...m..._.... +| 1872: 02 01 73 03 04 83 2d 02 01 74 06 04 84 0e 02 04 ..s...-..t...... +| 1888: 82 02 02 01 75 05 04 83 73 03 01 37 03 04 84 32 ....u...s..7...2 +| 1904: 02 01 76 03 04 83 0f 02 01 77 08 04 82 33 03 01 ..v......w...3.. +| 1920: 6f 01 02 0b 02 01 78 02 02 36 02 02 c2 b3 03 04 o.....x..6...... +| 1936: 81 07 02 04 84 29 02 02 ca a9 05 04 83 19 02 03 .....).......... +| 1952: ce bc 79 08 04 84 4b 02 02 d0 b8 05 04 81 06 02 ..y...K......... +| 1968: 06 f1 b5 87 92 35 6b 05 04 82 34 01 01 37 01 14 .....5k...4..7.. +| 1984: 81 3d 4b 17 08 16 81 03 81 20 01 0e 42 81 27 32 .=K...... ..B.'2 +| 2000: 20 0c 7a 01 0a 4c 83 12 40 19 01 16 06 21 6f 81 .z..L..@....!o. +| 2016: 02 70 36 0d 49 2e 2d 01 14 81 12 19 19 60 06 4c .p6.I.-......`.L +| 2032: 44 17 4f 01 12 03 81 12 04 81 23 81 4b 0a 01 12 D.O.......#.K... +| 2048: 0b 48 50 22 10 7f 81 38 56 01 0e 27 38 81 21 82 .HP....8V..'8.!. +| 2064: 08 2a 01 08 81 6b 41 24 01 0c 81 7c 73 81 3e 05 .*...kA$...|s.>. +| 2080: 02 02 34 67 05 04 83 5d 02 01 37 05 04 81 2f 02 ..4g...]..7.../. +| 2096: 02 73 02 01 38 01 02 21 02 01 39 09 02 4a 02 01 .s..8..!..9..J.. +| 2112: 62 0a 02 70 03 02 71 7a 01 04 83 26 02 01 64 0a b..p..qz...&..d. +| 2128: 04 84 04 02 01 65 0a 02 78 02 01 68 02 04 82 26 .....e..x..h...& +| 2144: 02 02 69 35 06 04 82 02 02 01 6b 02 04 82 0a 03 ..i5......k..... +| 2160: 02 c2 bc 05 04 82 08 02 01 6d 08 02 54 02 01 6e .........m..T..n +| 2176: 06 04 82 32 02 03 6f 74 61 02 04 81 2a 02 05 71 ...2..ota...*..q +| 2192: f2 96 8c 90 01 04 83 5a 02 01 72 09 04 83 31 02 .......Z..r...1. +| 2208: 01 74 03 02 71 04 04 83 62 02 02 79 65 04 04 83 .t..q...b..ye... +| 2224: 26 02 02 c2 aa 05 04 82 19 03 01 b3 02 02 10 02 &............... +| 2240: 04 83 48 03 01 bd 01 04 84 10 02 02 cf ad 06 04 ..H............. +| 2256: 82 04 02 04 f0 97 97 98 08 04 83 1f 02 04 f3 a5 ................ +| 2272: b3 87 08 04 81 64 01 01 38 01 0a 16 82 49 81 30 .....d..8....I.0 +| 2288: 01 0e 81 19 27 34 53 82 19 01 12 20 1f 5c 5f 81 ....'4S.... .._. +| 2304: 50 81 17 04 01 10 0e 81 2e 2d 51 67 81 16 01 0c P........-Qg.... +| 2320: 81 62 09 81 79 10 01 08 63 04 81 1c 01 0a 81 2e .b..y...c....... +| 2336: 5d 81 0f 01 0a 0b 81 40 81 63 01 10 27 81 17 81 ]......@.c..'... +| 2352: 2b 48 6c 4c 01 10 81 23 4d 64 0a 7f 81 01 02 01 +HlL...#Md...... +| 2368: 31 03 02 6b 02 01 36 06 04 83 79 02 01 37 08 04 1..k..6...y..7.. +| 2384: 84 47 02 04 39 c2 bc 61 09 04 84 61 02 05 64 c2 .G..9..a...a..d. +| 2400: b9 da 9c 01 04 81 73 02 01 66 01 04 81 58 08 04 ......s..f...X.. +| 2416: 82 39 02 02 67 74 0a 04 81 5f 02 01 68 09 04 83 .9..gt..._..h... +| 2432: 2a 02 01 69 0a 04 83 6a 03 01 78 02 04 83 11 02 *..i...j..x..... +| 2448: 01 6a 05 04 83 26 02 06 0d 82 58 03 01 70 07 04 .j...&....X..p.. +| 2464: 83 2b 02 01 6b 07 08 83 58 81 12 02 01 6e 0a 02 .+..k...X....n.. +| 2480: 71 02 01 77 0a 04 83 04 02 03 c2 ba 31 06 04 84 q..w........1... +| 2496: 4e 03 01 bd 07 04 83 18 02 02 d3 b5 05 02 68 01 N.............h. +| 2512: 01 39 01 0a 53 81 03 81 24 01 0e 13 82 3d 81 1e .9..S...$....=.. +| 2528: 37 2d 01 0c 6a 15 09 13 82 58 01 08 83 0f 81 3f 7-..j....X.....? +| 2544: 01 10 4f 81 60 81 29 64 15 0c 01 0e 4b 0d 62 0d ..O.`.)d....K.b. +| 2560: 81 00 5e 01 0a 37 81 1b 59 56 01 0e 81 0c 6b 6d ..^..7..YV....km +| 2576: 81 67 1d 01 10 38 2f 63 38 0c 37 20 6b 01 10 7e .g...8/c8.7 k..~ +| 2592: 10 18 81 4a 81 64 23 02 01 32 02 04 83 2c 02 01 ...J.d#..2...,.. +| 2608: 39 04 04 81 10 01 04 82 35 02 04 62 6e c7 9c 0a 9.......5..bn... +| 2624: 04 84 2b 02 02 65 63 06 04 83 0a 03 01 6a 02 04 ..+..ec......j.. +| 2640: 84 4d 02 01 66 01 04 83 4d 02 04 81 0a 02 03 68 .M..f...M......h +| 2656: ce bc 0a 04 83 41 02 01 6c 06 02 54 02 01 6d 08 .....A..l..T..m. +| 2672: 04 81 14 02 02 6f 6c 0a 04 84 0a 02 01 70 02 04 .....ol......p.. +| 2688: 82 2b 05 04 81 49 02 05 71 69 c2 bd 75 0a 02 46 .+...I..qi..u..F +| 2704: 02 02 72 38 0a 04 82 26 02 01 74 04 04 81 0b 04 ..r8...&..t..... +| 2720: 04 84 6e 02 01 77 03 04 81 6a 01 04 82 6b 03 01 ..n..w...j...k.. +| 2736: 65 09 04 82 58 02 01 78 06 02 6e 02 02 c2 b3 08 e...X..x..n..... +| 2752: 06 76 81 10 03 01 b9 04 02 47 03 01 bc 0a 04 83 .v.......G...... +| 2768: 03 02 02 ce bc 02 06 83 3f 19 02 03 d9 be 7a 03 ........?.....z. +| 2784: 04 82 5d 01 01 61 01 1e 17 50 16 12 12 2d 32 81 ..]..a...P...-2. +| 2800: 1e 56 81 06 04 06 1b 01 16 43 5c 52 64 19 15 27 .V.......C.Rd..' +| 2816: 1d 29 0e 52 01 1c 1d 03 81 1c 13 6b 3d 64 14 32 .).R.......k=d.2 +| 2832: 16 29 09 0a 01 22 32 13 07 81 08 03 22 05 17 20 .)....2........ +| 2848: 07 0e 7a 15 71 22 22 01 1c 30 05 06 0b 22 40 11 ..z.q....0....@. +| 2864: 49 1c 0c 15 81 1f 27 01 0c 4a 14 1c 7a 36 66 01 I.....'..J..z6f. +| 2880: 1c 10 19 81 0a 81 1c 81 02 10 54 33 09 0c 07 01 ..........T3.... +| 2896: 1e 38 2e 33 3a 22 1e 2e 35 3a 0f 07 77 15 0b 2f .8.3:...5:..w../ +| 2912: 01 2a 08 5a 35 13 0b 07 05 04 17 42 32 14 04 09 .*.Z5......B2... +| 2928: 1d 0b 09 06 42 72 1c 01 1e 22 0a 15 2b 03 04 6c ....Br......+..l +| 2944: 05 81 3d 14 19 58 21 4a 02 01 32 08 04 83 64 03 ..=..X!J..2...d. +| 2960: 01 67 07 04 82 5c 02 02 34 66 03 04 83 6d 03 01 .g......4f...m.. +| 2976: 73 08 04 81 18 02 05 37 6a 77 c2 b9 0a 04 83 1d s......7jw...... +| 2992: 02 02 38 7a 0a 04 82 3a 02 01 39 05 02 5c 03 02 ..8z...:..9..... +| 3008: c2 b3 06 02 70 02 01 61 01 04 83 03 02 01 66 02 ....p..a......f. +| 3024: 04 81 5b 02 04 82 37 02 01 68 04 04 82 78 01 02 ..[...7..h...x.. +| 3040: 67 02 01 69 04 04 84 0e 06 04 82 41 02 01 6a 01 g..i.......A..j. +| 3056: 04 82 4c 02 02 4e 03 04 81 73 03 01 6c 02 04 81 ..L..N...s..l... +| 3072: 5f 02 01 6b 08 04 81 26 02 05 6d c2 b3 6f 76 06 _..k...&..m..ov. +| 3088: 04 82 7c 02 01 6e 08 02 1c 02 01 6f 02 04 83 24 ..|..n.....o...$ +| 3104: 07 04 83 77 02 01 70 04 04 82 7e 02 01 71 0a 04 ...w..p...~..q.. +| 3120: 81 19 02 06 72 f1 b6 bc b3 68 01 04 83 0e 02 01 ....r....h...... +| 3136: 73 03 04 82 17 01 08 81 70 81 65 03 04 39 c2 ba s.......p.e..9.. +| 3152: 69 06 04 81 65 03 01 76 02 04 81 26 02 01 74 01 i...e..v...&..t. +| 3168: 02 4c 02 01 75 01 04 84 67 02 02 77 69 0a 02 09 .L..u...g..wi... +| 3184: 02 04 78 d2 b9 73 04 04 83 42 02 03 7a c2 b3 08 ..x..s...B..z... +| 3200: 02 43 02 02 c2 aa 01 04 82 61 06 04 84 10 02 02 .C.......a...... +| 3216: c6 88 07 02 1f 02 04 c8 b8 dd bf 05 04 81 72 02 ..............r. +| 3232: 02 c9 ad 07 04 84 2f 02 02 ce bc 07 02 05 02 02 ....../......... +| 3248: cf 85 0a 04 84 36 02 02 d9 a3 02 04 81 72 02 04 .....6.......r.. +| 3264: f1 a9 82 9b 0a 04 82 72 01 01 62 01 1c 81 17 06 .......r..b..... +| 3280: 4a 46 28 2a 0a 4a 10 36 05 52 20 01 1c 1f 15 5f JF(*.J.6.R ...._ +| 3296: 17 12 2b 46 03 81 2b 0f 1b 1f 4f 01 26 6d 68 10 ..+F..+...O.&mh. +| 3312: 04 08 06 20 04 10 05 20 05 07 6d 2e 09 14 05 24 ... ... ..m....$ +| 3328: 01 18 21 2b 11 12 81 0f 3e 22 11 28 51 28 01 18 ..!+....>..(Q(.. +| 3344: 12 11 4e 36 81 5a 23 3b 1a 16 26 2a 01 20 34 79 ..N6.Z#;..&*. 4y +| 3360: 1e 32 14 05 27 17 0c 4a 49 46 09 03 0d 2f 01 0e .2..'..JIF.../.. +| 3376: 15 35 6f 3c 24 5e 2e 01 14 15 40 49 25 2c 0a 1d .5o<$^....@I%,.. +| 3392: 67 70 2e 01 1c 06 03 0b 0e 76 81 0e 15 81 25 0b gp.......v....%. +| 3408: 39 2d 1a 01 14 27 1e 81 01 27 81 54 76 14 3e 02 9-...'...'.Tv.>. +| 3424: 02 31 62 08 04 83 1d 02 01 33 0a 04 83 35 02 01 .1b......3...5.. +| 3440: 37 02 04 82 6b 02 01 39 0a 02 29 02 01 61 03 04 7...k..9..)..a.. +| 3456: 82 72 02 01 64 01 04 81 05 02 04 84 1f 02 01 65 .r..d..........e +| 3472: 01 04 81 69 02 01 66 04 04 82 50 05 04 82 10 02 ...i..f...P..... +| 3488: 01 69 01 02 0d 02 01 6b 0a 04 81 0a 02 01 6c 02 .i.....k......l. +| 3504: 04 81 79 04 04 84 6e 03 01 31 0a 04 84 66 02 01 ..y...n..1...f.. +| 3520: 6f 0a 04 83 48 02 01 73 01 02 3d 03 02 10 02 01 o...H..s..=..... +| 3536: 74 02 04 82 0e 02 01 75 06 04 84 74 03 01 32 01 t......u...t..2. +| 3552: 04 82 0d 02 01 76 06 04 83 3a 02 01 78 08 04 82 .....v...:..x... +| 3568: 73 03 01 6c 09 04 84 1c 03 01 78 02 04 82 08 03 s..l......x..... +| 3584: 02 7a 69 0a 04 83 73 02 01 79 03 04 82 42 02 01 .zi...s..y...B.. +| 3600: 7a 08 04 83 03 02 06 c2 b9 e3 b1 a1 32 0a 04 81 z...........2... +| 3616: 46 03 01 bc 04 04 83 36 03 01 be 04 04 84 3f 02 F......6......?. +| 3632: 02 c6 80 0a 04 84 00 02 03 d4 a3 76 07 04 84 33 ...........v...3 +| 3648: 02 04 da ba 71 6f 08 04 84 11 02 02 dd a8 08 02 ....qo.......... +| 3664: 51 02 06 eb 9e ac 7a 73 79 06 02 32 01 01 63 01 Q.....zsy..2..c. +| 3680: 18 04 39 12 3a 05 34 56 46 59 81 0b 43 01 22 2a ..9.:.4VFY..C..* +| 3696: 1d 1f 53 2b 60 22 12 0d 13 1c 2e 35 05 03 0f 54 ..S+`......5...T +| 3712: 01 14 81 1e 81 1f 17 03 24 2a 81 0b 01 24 34 20 ........$*...$4 +| 3728: 06 21 0a 30 05 08 03 62 0c 26 10 6c 3b 0d 44 3a .!.0...b.&.l;.D: +| 3744: 01 1a 15 0b 77 04 45 3f 34 37 3f 08 13 36 53 01 ....w.E?47?..6S. +| 3760: 1c 02 38 33 49 1e 2e 81 19 43 33 3e 24 15 08 01 ..83I....C3>$... +| 3776: 1a 09 81 55 40 43 09 21 27 4c 04 5b 07 07 07 07 ...U@C.!'L.[.... +| 3792: 07 07 07 07 07 08 08 07 0a 07 0a 06 07 08 07 07 ................ +| 3808: 08 07 0a 08 56 06 0a 07 07 09 06 08 07 07 07 0a ....V........... +| 3824: 06 07 09 09 07 06 07 08 08 08 08 08 5e 07 06 07 ............^... +| 3840: 07 08 09 07 07 08 07 07 08 0b 0b 07 0a 06 07 0a ................ +| 3856: 08 09 09 0a 07 09 08 65 07 07 07 07 07 08 0b 07 .......e........ +| 3872: 06 0b 07 07 06 07 07 07 07 08 09 0c 57 0b 08 07 ............W... +| 3888: 07 0c 08 07 07 08 09 0a 07 07 07 09 07 07 07 0a ................ +| 3904: 07 06 0a 07 08 08 09 5b 07 07 0b 06 09 0a 0a 06 .......[........ +| 3920: 0a 0a 07 07 08 08 06 08 06 0e 5f 07 07 0b 0a 08 .........._..... +| 3936: 06 07 0b 07 07 0f 07 0b 07 07 07 07 06 06 0c 08 ................ +| 3952: 09 08 0c 65 08 0a 06 06 06 08 07 06 07 08 07 08 ...e............ +| 3968: 06 07 09 0b 07 0a 08 08 0a 07 08 0a 0a 58 06 07 .............X.. +| 3984: 07 0a 0b 0b 08 07 07 07 0c 07 09 06 07 09 07 07 ................ +| 4000: 58 07 0b 0a 08 07 0b 09 06 07 08 0b 0a 08 0b 0b X............... +| 4016: 07 06 09 06 07 09 09 81 25 07 07 08 07 0b 08 06 ........%....... +| 4032: 07 07 0b 0a 0b 0e 07 07 0b 06 0b 07 07 0c 0d 0a ................ +| 4048: 07 06 07 07 0a 08 0c 07 0a 08 07 08 08 0a 81 17 ................ +| 4064: 08 07 07 06 07 0b 07 0b 06 07 0b 07 07 09 07 07 ................ +| 4080: 07 07 07 07 07 08 07 07 0c 07 07 08 09 0a 07 0b ................ +| page 21 offset 81920 +| 0: 0d 00 00 00 01 00 21 00 00 21 00 00 00 00 00 00 ......!..!...... +| 32: 00 9f 57 88 80 80 80 80 02 04 00 bf 32 00 08 0e ..W.........2... +| 48: 8c 3a 14 04 29 08 18 17 4a 44 3a 19 4e 5f 56 16 .:..)...JD:.N_V. +| 64: 08 30 25 01 1c 0e 13 32 1b 0b 41 03 2f 81 34 3c .0%....2..A./.4< +| 80: 14 09 21 01 14 81 01 06 10 30 1f 28 0f 51 5f 04 ..!......0.(.Q_. +| 96: 30 63 33 74 05 02 69 02 02 34 76 0a 04 83 63 02 0c3t..i..4v...c. +| 112: 04 36 e7 8e a1 03 04 84 03 02 04 61 77 c2 b9 05 .6.........aw... +| 128: 04 82 56 02 01 63 03 04 82 02 04 04 82 0a 02 02 ..V..c.......... +| 144: 64 30 09 02 40 02 01 67 02 06 61 83 38 02 04 82 d0..@..g..a.8... +| 160: 70 03 04 84 62 03 01 73 02 04 84 5f 02 01 68 01 p...b..s..._..h. +| 176: 04 81 76 03 01 32 07 04 84 3f 02 01 69 09 02 7e ..v..2...?..i..~ +| 192: 02 01 6a 01 04 81 57 02 01 6b 01 04 82 33 02 01 ..j...W..k...3.. +| 208: 6d 05 02 7b 02 01 6e 06 04 82 51 02 01 6f 06 02 m.....n...Q..o.. +| 224: 0e 03 01 65 01 04 81 3c 02 02 70 63 06 02 1e 02 ...e...<..pc.... +| 240: 01 71 02 04 84 3b 08 02 1a 02 01 72 09 06 36 81 .q...;.....r..6. +| 256: 38 02 01 75 02 04 83 3b 03 02 54 02 01 77 04 04 8..u...;..T..w.. +| 272: 83 5c 06 04 83 21 02 01 78 07 04 81 21 02 01 79 .....!..x...!..y +| 288: 06 02 3a 02 02 7a 31 02 04 84 2a 02 02 c2 b2 02 ..:..z1...*..... +| 304: 04 81 01 03 01 b3 07 04 82 5e 03 02 bd 7a 06 04 .........^...z.. +| 320: 82 08 02 02 c6 9e 0a 04 83 4d 02 02 ce bc 05 04 .........M...... +| 336: 84 70 02 02 d3 86 06 04 82 33 02 02 d4 a7 01 02 .p.......3...... +| 352: 7c 02 02 d8 b8 06 04 82 53 02 02 df a4 04 02 7c |.......S......| +| 368: 02 03 e0 b7 ae 04 04 84 5e 02 03 e6 91 9a 03 04 ........^....... +| 384: 81 18 02 04 f0 90 ac 8e 04 04 82 39 01 01 64 01 ...........9..d. +| 400: 16 23 2f 1a 6d 81 47 65 09 1b 42 25 01 10 0d 82 .#/.m.Ge..B%.... +| 416: 18 81 3c 50 06 34 01 10 60 31 5a 14 18 16 5f 18 ....J.Z.!. +| 464: 18 0a 0a 67 3d 81 37 40 68 0e 30 17 18 01 12 08 ...g=.7@h.0..... +| 480: 6a 54 81 0c 49 14 4f 21 01 16 26 82 0d 3c 0c 60 jT..I.O!..&..<.` +| 496: 19 27 51 05 07 01 16 24 20 07 1a 81 1c 1f 24 43 .'Q....$ .....$C +| 512: 81 5b 01 16 42 21 05 81 07 0d 36 5b 21 81 15 02 .[..B!....6[!... +| 528: 01 30 01 04 84 4d 02 01 32 04 04 84 38 02 01 34 .0...M..2...8..4 +| 544: 06 04 83 1e 02 04 84 3c 01 04 81 2c 02 01 62 08 .......<...,..b. +| 560: 04 82 51 03 01 61 05 04 84 3e 02 01 63 06 04 84 ..Q..a...>..c... +| 576: 24 03 04 83 40 02 01 64 01 08 81 18 83 2e 02 01 $...@..d........ +| 592: 65 03 04 82 46 02 01 66 09 04 84 33 02 01 67 03 e...F..f...3..g. +| 608: 04 81 55 02 07 68 c2 b9 62 71 61 30 02 02 48 02 ..U..h..bqa0..H. +| 624: 01 69 04 04 82 4b 03 01 78 03 04 81 3c 02 02 6a .i...K..x...<..j +| 640: 34 05 02 47 02 01 6b 01 04 82 29 03 01 38 0a 02 4..G..k...)..8.. +| 656: 49 02 01 6c 02 04 84 1a 01 04 84 58 07 04 82 2c I..l.......X..., +| 672: 02 01 6d 06 04 83 0b 02 04 84 0c 03 02 c2 bc 08 ..m............. +| 688: 04 83 17 02 01 6e 02 04 83 70 02 01 6f 04 04 81 .....n...p..o... +| 704: 56 03 01 61 03 02 5b 03 04 65 79 64 37 06 04 81 V..a..[..eyd7... +| 720: 38 03 03 ee a9 bf 08 02 45 02 01 71 04 04 81 75 8.......E..q...u +| 736: 03 01 74 09 04 83 66 02 01 72 06 02 4f 02 01 73 ..t...f..r..O..s +| 752: 01 04 81 26 07 02 2d 02 01 74 02 02 14 02 02 3c ...&..-..t.....< +| 768: 04 04 84 5d 02 02 77 64 02 02 31 02 01 78 02 04 ...]..wd..1..x.. +| 784: 84 59 03 04 84 65 02 01 79 02 02 58 04 04 81 49 .Y...e..y..X...I +| 800: 01 04 83 17 03 02 7f 02 02 c2 b3 04 02 05 03 01 ................ +| 816: b9 01 04 84 09 03 01 be 0a 04 82 59 02 02 ce bc ...........Y.... +| 832: 06 04 81 02 02 02 d7 9d 06 04 83 31 02 04 df 84 ...........1.... +| 848: da 8c 08 04 82 67 02 04 e1 b5 b6 6b 0a 04 81 7b .....g.....k.... +| 864: 02 03 e3 a1 92 03 04 83 66 02 04 f0 9b 93 a9 07 ........f....... +| 880: 04 85 0b 04 02 a8 81 06 04 82 3e 01 01 65 01 24 ..........>..e.$ +| 896: 43 34 18 15 08 09 51 19 5b 17 28 26 11 06 23 38 C4....Q.[.(&..#8 +| 912: 2b 2b 01 1e 1c 32 20 17 0f 2c 03 0c 1b 0c 6f 25 ++...2 ..,....o% +| 928: 73 16 40 01 1e 07 12 81 5a 44 19 06 03 24 5c 0e s.@.....ZD...$.. +| 944: 16 18 39 0d 01 26 0d 09 06 41 42 09 24 1d 25 0c ..9..&...AB.$.%. +| 960: 06 1d 63 14 26 18 7d 0a 1d 01 14 25 56 29 0a 81 ..c.&......%V).. +| 976: 1a 1b 37 5d 3d 01 16 1c 81 26 25 7f 41 52 46 08 ..7]=....&%.ARF. +| 992: 1b 16 01 12 81 0f 6b 7b 0f 2d 03 7f 07 01 18 19 ......k..-...... +| 1008: 28 81 01 50 36 41 16 81 22 17 03 01 1c 16 1a 81 (..P6A.......... +| 1024: 1f 10 17 41 4c 1c 0b 66 49 10 06 01 18 54 82 38 ...AL..fI....T.8 +| 1040: 13 2a 0b 14 4c 15 0f 36 0b 02 01 30 07 04 82 28 .*..L..6...0...( +| 1056: 02 01 31 09 04 81 2b 01 04 83 38 02 01 33 03 04 ..1...+...8..3.. +| 1072: 81 76 02 01 61 08 04 84 6b 03 05 7a ea 86 b3 6a .v..a...k..z...j +| 1088: 06 04 82 52 02 01 62 0a 04 83 19 02 01 63 02 04 ...R..b......c.. +| 1104: 82 4d 03 02 c2 be 04 04 82 29 02 01 64 05 04 81 .M.......)..d... +| 1120: 7a 03 04 81 72 02 02 66 61 04 02 41 03 01 77 06 z...r..fa..A..w. +| 1136: 02 3e 02 04 82 10 02 01 67 05 04 84 48 02 01 69 .>......g...H..i +| 1152: 01 04 83 12 02 02 6c 62 07 04 81 1d 02 01 6d 01 ......lb......m. +| 1168: 04 83 6e 02 01 6f 05 02 09 02 04 81 16 02 01 70 ..n..o.........p +| 1184: 05 06 07 82 72 03 01 38 07 04 84 46 02 01 73 02 ....r..8...F..s. +| 1200: 04 84 2d 05 04 81 58 03 04 81 4a 03 02 c2 bd 0a ..-...X...J..... +| 1216: 04 83 2d 02 01 74 02 04 84 55 05 04 85 02 02 01 ..-..t...U...... +| 1232: 75 02 04 82 62 02 01 76 01 04 84 31 02 01 77 09 u...b..v...1..w. +| 1248: 04 82 15 02 01 78 01 02 45 05 04 82 14 03 04 83 .....x..E....... +| 1264: 01 02 01 79 06 04 81 0c 02 02 7a 78 09 04 81 6d ...y......zx...m +| 1280: 02 04 c2 bc 61 72 04 04 83 20 04 01 6f 01 02 70 ....ar... ..o..p +| 1296: 02 02 c9 9e 01 04 82 02 02 02 ca 83 05 04 82 3f ...............? +| 1312: 03 01 9b 05 02 52 03 01 aa 07 04 83 65 02 02 cd .....R......e... +| 1328: bb 01 04 83 07 02 02 ce b2 06 02 5b 02 03 cf a7 ...........[.... +| 1344: 78 03 04 81 3b 02 03 ef a9 89 08 04 82 20 02 04 x...;........ .. +| 1360: f5 87 b0 93 02 04 84 18 01 01 66 01 1c 2b 3d 5b ..........f..+=[ +| 1376: 23 45 2d 41 17 0e 81 1d 0f 15 1f 01 16 05 81 20 #E-A........... +| 1392: 0d 27 81 0f 3e 28 2b 13 01 14 62 2c 44 26 17 04 .'..>(+...b,D&.. +| 1408: 23 50 6d 5b 01 1c 30 5b 81 27 07 29 38 15 4d 11 #Pm[..0[.'.)8.M. +| 1424: 2e 20 17 20 01 1a 19 65 4f 14 2e 55 0e 12 39 13 . . ...eO..U..9. +| 1440: 1d 70 12 01 20 1a 0d 2f 05 81 19 2b 42 05 37 4c .p.. ../...+B.7L +| 1456: 21 22 04 25 2b 01 14 45 3e 05 10 81 47 6c 81 11 !..%+..E>...Gl.. +| 1472: 11 01 1e 78 04 12 18 0a 05 10 14 6b 05 0a 04 82 ...x.......k.... +| 1488: 05 15 01 1e 05 23 4b 18 24 31 20 27 12 2e 81 0e .....#K.$1 '.... +| 1504: 31 18 18 01 10 55 7e 29 2a 06 19 65 38 02 01 30 1....U~)*..e8..0 +| 1520: 09 02 14 02 01 31 02 04 84 00 02 01 36 06 04 84 .....1......6... +| 1536: 7a 02 01 61 07 04 83 71 02 01 62 05 04 83 10 03 z..a...q..b..... +| 1552: 02 34 02 02 63 6f 07 04 83 27 02 01 64 07 04 81 .4..co...'..d... +| 1568: 0c 03 04 81 55 03 01 6f 03 02 7e 02 01 69 02 04 ....U..o..~..i.. +| 1584: 81 08 03 02 05 03 04 82 70 02 01 6a 05 02 41 03 ........p..j..A. +| 1600: 02 6f 30 04 02 5e 02 01 6b 02 04 82 13 02 01 6c .o0..^..k......l +| 1616: 02 04 83 2d 08 04 81 20 02 01 6e 09 04 82 69 02 ...-... ..n...i. +| 1632: 02 6f 38 02 04 83 73 02 01 71 0a 04 81 4f 02 01 .o8...s..q...O.. +| 1648: 74 09 04 83 2c 02 01 75 03 04 83 79 03 03 68 c2 t...,..u...y..h. +| 1664: b3 03 04 81 09 02 02 77 64 06 04 81 79 02 01 78 .......wd...y..x +| 1680: 03 04 83 41 03 02 19 02 01 79 02 04 82 1a 07 02 ...A.....y...... +| 1696: 72 03 01 6a 06 04 83 1a 02 01 7a 05 04 84 6d 01 r..j......z...m. +| 1712: 04 81 24 02 03 c2 aa 75 0a 04 84 72 03 01 b3 04 ..$....u...r.... +| 1728: 04 82 02 03 04 82 2c 03 01 ba 07 04 26 51 03 02 ......,.....&Q.. +| 1744: bd 65 07 04 81 1a 03 01 be 09 02 44 02 02 ca 97 .e.........D.... +| 1760: 02 04 84 30 02 03 d5 a7 6f 04 02 36 03 02 bd 72 ...0....o..6...r +| 1776: 07 04 81 03 02 05 f3 b2 a7 91 68 09 04 82 22 01 ..........h..... +| 1792: 01 67 01 16 81 41 81 1f 4c 0f 0d 0d 6f 3d 04 01 .g...A..L...o=.. +| 1808: 18 12 08 5e 05 16 4f 42 61 22 45 3c 27 01 20 08 ...^..OBa.E<'. . +| 1824: 13 27 14 17 1f 08 3e 0d 41 81 1c 3e 06 1c 1e 01 .'....>.A..>.... +| 1840: 16 0c 11 6b 26 54 05 14 03 81 6b 12 01 1a 36 41 ...k&T....k...6A +| 1856: 11 0b 0c 36 7c 08 0e 0c 52 4f 50 01 0c 81 0e 22 ...6|...ROP..... +| 1872: 3d 72 1f 01 22 07 0e 22 3c 4d 1c 09 03 2d 22 1e =r.........?.....u.. +| 2656: be 6b 05 04 83 0c 02 04 c3 b8 66 70 01 04 82 2f .k........fp.../ +| 2672: 02 02 ce bc 08 04 81 04 02 03 cf a7 6b 06 04 82 ............k... +| 2688: 4d 02 03 d9 84 72 03 02 13 02 02 da 90 06 04 84 M....r.......... +| 2704: 73 02 04 f0 ad 93 a0 06 02 09 01 01 69 01 20 37 s...........i. 7 +| 2720: 4d 55 11 49 26 3c 17 0d 4a 26 03 05 0c 20 43 01 MU.I&<..J&... C. +| 2736: 10 59 08 37 5a 56 72 81 05 01 1a 16 33 32 0a 03 .Y.7ZVr.....32.. +| 2752: 18 81 17 5f 59 27 37 08 01 24 0a 70 1f 07 40 3b ..._Y'7..$.p..@; +| 2768: 15 20 09 15 30 0f 17 06 36 81 08 17 01 1c 32 0a . ..0...6.....2. +| 2784: 05 1a 5f 1c 32 78 03 15 17 81 2b 1e 01 1a 04 03 .._.2x....+..... +| 2800: 2b 07 0c 38 43 6c 0f 81 77 0e 0e 01 1a 1c 3d 81 +..8Cl..w.....=. +| 2816: 0d 0d 17 1d 4c 63 44 2e 11 2b 01 18 2b 2d 49 72 ....LcD..+..+-Ir +| 2832: 1d 22 17 31 19 27 32 4c 01 1c 0b 67 27 20 81 1c ...1.'2L...g' .. +| 2848: 47 31 29 0d 0b 12 4c 07 01 18 35 81 0b 08 0e 5f G1)...L...5...._ +| 2864: 11 18 37 81 09 09 02 01 30 09 02 19 02 02 31 63 ..7.....0.....1c +| 2880: 09 02 7f 02 01 32 04 02 2c 05 04 83 6e 02 01 35 .....2..,...n..5 +| 2896: 09 04 83 7a 03 01 79 05 02 0f 02 02 36 73 05 04 ...z..y.....6s.. +| 2912: 83 3d 02 03 38 6a 70 07 04 84 39 02 01 61 07 04 .=..8jp...9..a.. +| 2928: 83 44 02 01 62 0a 04 84 37 02 01 64 01 04 85 00 .D..b...7..d.... +| 2944: 07 04 82 36 03 02 cf a7 01 04 82 5f 02 01 66 06 ...6......._..f. +| 2960: 04 82 35 03 03 71 70 6e 04 04 82 14 02 02 67 7a ..5..qpn......gz +| 2976: 02 04 82 00 02 01 68 01 02 14 02 01 69 05 02 48 ......h.....i..H +| 2992: 02 01 6a 01 04 83 7a 07 02 12 01 02 33 02 01 6d ..j...z.....3..m +| 3008: 04 04 83 76 03 01 68 08 04 82 63 02 01 6f 03 06 ...v..h...c..o.. +| 3024: 2c 81 52 06 04 81 4f 02 01 70 06 04 83 00 03 02 ,.R...O..p...... +| 3040: c2 be 06 04 83 37 02 01 72 09 04 82 78 03 03 71 .....7..r...x..q +| 3056: 61 6c 0a 02 3e 03 02 c2 be 05 04 84 67 02 01 73 al..>.......g..s +| 3072: 09 04 81 0d 02 01 74 04 02 58 02 01 75 08 04 81 ......t..X..u... +| 3088: 17 02 01 76 04 04 83 4b 06 04 84 70 02 01 7a 02 ...v...K...p..z. +| 3104: 04 83 7e 02 02 c2 aa 03 02 61 03 01 b2 08 04 84 ..~......a...... +| 3120: 13 03 04 b9 c2 b2 6a 01 02 19 03 02 bc 63 05 04 ......j......c.. +| 3136: 84 12 02 02 c7 86 09 04 84 11 02 02 ce bc 02 02 ................ +| 3152: 17 02 03 cf ab 6e 07 02 29 02 06 d5 b1 30 d1 bb .....n..)....0.. +| 3168: 6e 01 02 4e 02 03 eb 8b a7 08 04 81 6b 03 02 b5 n..N........k... +| 3184: bc 07 04 83 08 01 01 6a 01 14 81 07 79 0f 79 32 .......j....y.y2 +| 3200: 33 13 81 16 01 16 2c 0a 81 1b 24 2f 05 81 19 19 3.....,...$/.... +| 3216: 2d 01 16 81 4c 03 14 4f 81 08 2d 05 2f 21 01 18 -...L..O..-./!.. +| 3232: 27 1f 0e 81 16 0c 82 4b 04 12 06 0e 01 1e 26 05 '......K......&. +| 3248: 22 14 55 05 09 81 16 21 05 19 37 74 32 01 1e 37 ..U....!..7t2..7 +| 3264: 07 1e 03 10 81 2c 38 07 1a 2a 2b 81 0d 08 01 1e .....,8..*+..... +| 3280: 2b 19 31 3c 4c 03 1a 14 04 1c 06 76 04 48 62 01 +.1-......p$D..b +| 3760: 12 3b 50 15 34 03 16 01 0c 32 08 08 0a 0a 0b 07 .;P.4....2...... +| 3776: 10 07 07 07 06 07 07 06 07 06 07 07 0a 08 0a 0b ................ +| 3792: 07 06 08 08 07 08 08 08 08 07 08 07 09 09 0a 81 ................ +| 3808: 03 07 07 0f 07 07 0b 09 07 07 07 0c 07 07 07 07 ................ +| 3824: 06 0f 0b 08 07 07 06 0a 08 07 07 06 0a 0d 07 0b ................ +| 3840: 11 07 07 07 08 08 0a 0a 09 0a 08 81 1e 07 0b 07 ................ +| 3856: 07 0b 07 07 08 0b 07 0a 07 07 08 07 0a 08 07 0f ................ +| 3872: 08 0b 07 07 07 0e 07 08 0a 06 08 08 06 07 08 07 ................ +| 3888: 09 09 0a 81 15 06 07 07 07 0a 08 0b 06 0e 06 07 ................ +| 3904: 07 0b 07 08 07 07 07 09 08 0a 0a 07 0b 09 0b 07 ................ +| 3920: 08 06 08 08 08 0b 81 0d 08 08 07 07 07 0b 0b 0b ................ +| 3936: 08 07 07 07 0f 0b 06 07 07 07 08 0c 07 07 08 08 ................ +| 3952: 0e 09 0b 07 06 08 06 07 07 08 09 07 0a 0a 81 1f ................ +| 3968: 07 09 08 06 0b 0a 0d 07 0a 07 08 0a 09 07 08 07 ................ +| 3984: 07 09 06 0d 09 07 0a 08 08 0b 0b 06 08 0a 08 09 ................ +| 4000: 08 08 09 81 1c 06 07 0a 07 06 08 09 07 07 0b 08 ................ +| 4016: 07 09 08 06 06 0d 07 07 0c 07 08 07 08 08 07 06 ................ +| 4032: 07 0b 07 07 07 09 08 08 07 08 0b 09 08 81 16 09 ................ +| 4048: 07 07 07 07 08 07 09 07 07 07 07 07 07 07 0a 07 ................ +| 4064: 07 07 07 09 07 09 08 07 07 07 07 07 07 07 09 09 ................ +| 4080: 0a 07 06 07 0b 09 07 09 07 08 09 09 0c 0b 07 07 ................ +| page 22 offset 86016 +| 0: 0d 00 00 00 01 00 24 00 00 24 00 00 00 00 00 00 ......$..$...... +| 32: 00 00 00 00 9f 54 88 80 80 80 80 03 04 00 bf 2c .....T........., +| 48: 00 0a 0e 8b 61 42 11 82 12 4d 05 12 81 47 48 81 ....aB...M...GH. +| 64: 1c 3a 41 17 25 01 2a 42 21 1d 1f 07 44 65 2c 0f .:A.%.*B!...De,. +| 80: 18 20 18 08 03 19 04 10 19 0b 17 26 01 1a 14 1f . .........&.... +| 96: 11 1f 2b 10 82 08 26 2b 76 0c 27 01 22 4c 23 06 ..+...&+v.'..L#. +| 112: 81 10 2e 2e 03 37 4b 13 14 14 1b 15 24 0d 01 18 .....7K.....$... +| 128: 3a 0e 4b 15 7b 2a 4a 13 24 81 10 0a 01 18 11 16 :.K..*J.$....... +| 144: 28 1f 57 54 15 16 6b 50 2f 04 04 30 6b 30 34 05 (.WT..kP/..0k04. +| 160: 04 83 1c 02 01 31 04 02 39 02 02 22 01 04 82 48 .....1..9......H +| 176: 02 01 32 09 04 81 25 02 01 34 01 04 81 1a 02 01 ..2...%..4...... +| 192: 35 01 02 09 03 01 30 09 04 83 29 02 01 37 05 04 5.....0...)..7.. +| 208: 82 70 02 01 38 06 04 83 2f 02 01 39 03 04 83 29 .p..8.../..9...) +| 224: 02 01 61 09 02 67 02 01 62 02 04 82 57 02 01 63 ..a..g..b...W..c +| 240: 01 04 82 24 02 02 64 33 09 04 84 06 02 01 65 09 ...$..d3......e. +| 256: 04 84 46 02 01 66 03 04 84 15 01 04 84 69 04 04 ..F..f.......i.. +| 272: 84 36 02 01 67 06 04 84 7e 02 08 83 35 81 2c 02 .6..g...~...5.,. +| 288: 01 68 03 04 82 3a 05 08 81 2e 81 20 02 01 69 02 .h...:..... ..i. +| 304: 04 81 69 02 01 6a 06 02 46 01 08 82 1e 82 5b 02 ..i..j..F.....[. +| 320: 04 84 20 02 01 6b 09 04 81 2e 02 01 6c 08 04 84 .. ..k......l... +| 336: 56 03 01 62 0a 04 83 3f 02 02 6d 6a 01 04 82 15 V..b...?..mj.... +| 352: 02 04 6f 69 72 33 01 04 82 25 02 01 72 01 02 44 ..oir3...%..r..D +| 368: 03 01 35 06 04 81 3b 02 02 74 74 04 04 81 79 02 ..5...;..tt...y. +| 384: 01 78 01 04 81 5b 02 02 7a 02 01 79 04 02 19 02 .x...[..z..y.... +| 400: 03 7a 71 6b 03 04 83 37 02 02 c2 b3 01 02 31 04 .zqk...7......1. +| 416: 04 82 04 03 07 ba 6e f5 9a 8b 98 76 02 02 5b 02 ......n....v..[. +| 432: 02 cb 8f 06 04 84 28 02 02 ce bc 01 04 81 66 06 ......(.......f. +| 448: 04 81 1b 02 04 83 47 04 06 ce bc ce bf df bc 09 ......G......... +| 464: 04 82 4f 02 02 dd 9c 03 04 81 62 02 04 f2 86 b3 ..O.......b..... +| 480: bb 01 04 82 08 01 01 6c 01 12 81 34 81 00 0d 2a .......l...4...* +| 496: 81 48 13 01 1a 0e 14 4f 81 17 14 12 10 75 68 36 .H.....O.....uh6 +| 512: 0b 1d 01 16 12 22 23 51 1d 7c 25 08 20 4f 7a 01 ......#Q.|%. Oz. +| 528: 12 08 0b 81 7c 71 81 12 0b 0b 01 0e 4e 50 55 82 ....|q......NPU. +| 544: 1e 2e 23 01 1c 0f 10 24 07 33 06 81 0c 73 42 1d ..#....$.3...sB. +| 560: 81 04 16 01 16 5e 62 6a 06 32 81 03 2f 0f 08 4e .....^bj.2../..N +| 576: 01 06 81 2c 22 01 12 53 81 1e 6e 40 31 0b 1c 41 ...,...S..n@1..A +| 592: 01 18 79 14 11 17 13 81 0f 81 15 19 35 40 02 01 ..y.........5@.. +| 608: 30 01 04 81 0d 03 04 83 6b 02 01 33 07 04 84 66 0.......k..3...f +| 624: 03 04 83 08 02 01 35 08 04 83 43 01 04 83 50 02 ......5...C...P. +| 640: 01 36 09 02 3b 02 01 37 07 04 84 38 02 01 61 0a .6..;..7...8..a. +| 656: 04 83 17 03 04 e1 8d 93 69 01 04 81 0e 02 01 62 ........i......b +| 672: 03 04 84 16 04 04 83 0e 02 01 63 05 04 81 79 05 ..........c...y. +| 688: 04 84 5d 02 01 64 07 04 81 4a 03 02 6d 6e 08 02 ..]..d...J..mn.. +| 704: 09 02 01 65 06 04 84 3e 03 02 76 7a 02 02 76 02 ...e...>..vz..v. +| 720: 01 66 0a 02 47 03 01 79 09 04 81 21 02 02 67 6f .f..G..y...!..go +| 736: 05 02 38 02 02 68 77 03 04 82 27 02 01 69 02 02 ..8..hw...'..i.. +| 752: 6f 04 02 6f 02 01 6b 07 04 82 61 02 01 6c 06 08 o..o..k...a..l.. +| 768: 82 2e 81 5a 02 01 6d 01 04 84 25 03 04 83 31 02 ...Z..m...%...1. +| 784: 06 2f 82 2e 03 01 68 08 04 83 73 02 01 6e 09 04 ./....h...s..n.. +| 800: 84 5a 02 01 6f 05 04 82 39 02 01 70 04 04 82 19 .Z..o...9..p.... +| 816: 05 02 0f 02 01 71 07 04 83 39 03 02 63 71 06 04 .....q...9..cq.. +| 832: 81 5e 03 01 67 07 04 83 73 03 01 70 08 04 84 6a .^..g...s..p...j +| 848: 02 01 73 03 04 83 40 02 01 74 07 04 84 7c 03 06 ..s...@..t...|.. +| 864: 35 69 7a 70 c2 bd 01 04 83 1a 02 01 75 01 08 82 5izp........u... +| 880: 2e 81 13 05 04 82 69 03 01 70 03 04 84 50 02 01 ......i..p...P.. +| 896: 76 01 04 82 52 09 04 82 64 02 02 77 73 03 04 81 v...R...d..ws... +| 912: 27 02 01 78 08 02 1f 01 04 83 64 02 01 79 04 08 '..x......d..y.. +| 928: 81 49 81 44 05 04 84 01 02 03 c2 aa 76 02 04 82 .I.D........v... +| 944: 67 03 01 b9 02 02 4f 06 02 31 03 01 bd 05 04 82 g.....O..1...... +| 960: 3d 02 02 ce bc 01 02 2c 01 01 6d 01 12 0c 47 83 =......,..m...G. +| 976: 0a 07 52 16 41 05 01 18 50 05 74 25 21 81 16 16 ..R.A...P.t%!... +| 992: 05 24 16 6c 01 22 81 05 18 06 12 2a 59 09 1f 09 .$.l.......*Y... +| 1008: 3d 27 39 17 3a 1f 04 01 14 72 81 1a 24 81 38 20 ='9.:....r..$.8 +| 1024: 27 19 2b 01 18 81 2b 24 63 0b 11 5d 2d 0c 41 1a '.+...+$c..]-.A. +| 1040: 30 01 1e 15 04 3c 3a 15 0e 28 18 06 56 81 37 20 0....<:..(..V.7 +| 1056: 0d 6b 01 1e 63 3f 42 73 0a 26 0c 6f 0f 15 13 0b .k..c?Bs.&.o.... +| 1072: 04 25 33 01 16 24 52 16 3a 21 1b 63 26 31 20 34 .%3..$R.:!.c&1 4 +| 1088: 01 0e 1a 60 13 19 3d 82 1f 01 1e 5b 07 26 07 20 ...`..=....[.&. +| 1104: 03 5a 69 6c 27 0b 22 24 20 0f 02 01 31 01 04 82 .Zil'..$ ...1... +| 1120: 6f 02 01 32 03 04 82 41 01 04 81 43 02 01 33 09 o..2...A...C..3. +| 1136: 04 81 23 03 05 7a 68 c2 bd 73 06 04 81 3a 02 01 ..#..zh..s...:.. +| 1152: 35 07 04 83 6c 02 01 38 01 04 83 1c 02 02 62 6f 5...l..8......bo +| 1168: 06 04 83 16 02 01 63 07 02 5b 03 06 6e 31 cf af ......c..[..n1.. +| 1184: 61 71 0a 04 83 33 03 02 c2 be 03 04 83 2b 02 01 aq...3.......+.. +| 1200: 64 09 04 81 19 01 04 83 09 03 01 62 0a 04 82 56 d..........b...V +| 1216: 03 01 6a 03 04 84 4e 02 01 65 05 04 82 3a 02 04 ..j...N..e...:.. +| 1232: 82 53 02 01 66 09 04 82 7a 02 01 67 02 04 82 69 .S..f...z..g...i +| 1248: 08 04 81 35 02 01 68 09 04 83 2f 02 01 69 01 04 ...5..h.../..i.. +| 1264: 81 62 02 01 6a 0a 04 81 59 02 01 6b 09 04 81 5f .b..j...Y..k..._ +| 1280: 02 01 6c 08 04 84 0d 02 01 6d 05 04 84 32 02 01 ..l......m...2.. +| 1296: 6e 01 04 83 05 02 01 6f 07 04 82 6f 02 01 70 04 n......o...o..p. +| 1312: 04 83 60 03 04 82 0d 03 03 c2 b9 70 02 04 81 43 ..`........p...C +| 1328: 02 01 71 07 04 81 13 03 04 82 35 02 01 72 03 04 ..q.......5..r.. +| 1344: 84 51 02 02 73 65 04 04 84 14 02 01 74 01 04 83 .Q..se......t... +| 1360: 5f 02 01 75 03 02 0b 03 02 c2 be 08 04 82 3a 02 _..u..........:. +| 1376: 01 76 06 04 84 43 02 01 77 01 04 82 18 01 04 81 .v...C..w....... +| 1392: 32 02 01 78 08 04 81 47 02 01 7a 01 04 82 44 02 2..x...G..z...D. +| 1408: 02 c2 b3 05 04 83 12 03 04 83 14 04 01 37 04 04 .............7.. +| 1424: 83 6c 03 03 ba dd af 05 04 82 41 02 02 c3 9f 07 .l........A..... +| 1440: 04 82 34 02 03 c6 80 78 0a 04 83 14 02 02 ca 95 ..4....x........ +| 1456: 07 04 83 0c 02 03 ce b0 67 03 02 6e 03 01 bc 05 ........g..n.... +| 1472: 02 17 04 02 6c 02 03 e7 a4 94 05 04 82 51 01 01 ....l........Q.. +| 1488: 6e 01 18 56 26 2f 54 29 5a 25 11 28 24 81 0f 01 n..V&/T)Z%.($... +| 1504: 1c 06 39 06 13 59 09 07 81 39 16 0c 39 27 4a 01 ..9..Y...9..9'J. +| 1520: 14 2a 03 53 23 81 0d 7d 5b 06 03 01 18 1e 03 81 .*.S#...[....... +| 1536: 02 34 37 29 15 34 81 30 36 01 1a 81 25 19 07 21 .47).4.06...%..! +| 1552: 07 33 12 82 02 45 04 05 01 12 82 0f 42 55 1d 08 .3...E......BU.. +| 1568: 1a 44 3d 01 16 18 17 36 54 6e 1b 49 1f 30 09 1c .D=....6Tn.I.0.. +| 1584: 01 16 68 81 00 08 48 3a 13 50 3c 28 27 01 1c 28 ..h...H:.P<('..( +| 1600: 03 06 12 5b 06 30 38 04 65 54 1a 0b 4d 01 20 03 ...[.08.eT..M. . +| 1616: 09 3d 27 04 11 59 11 1a 0b 73 53 81 03 04 26 02 .='..Y...sS...&. +| 1632: 01 32 02 04 83 3e 02 01 36 01 02 73 09 04 81 14 .2...>..6..s.... +| 1648: 02 01 37 0a 04 83 30 02 01 62 08 04 84 4d 02 02 ..7...0..b...M.. +| 1664: 63 7a 0a 04 84 08 02 01 64 07 04 83 61 03 02 7a cz......d...a..z +| 1680: 67 09 02 58 02 02 65 69 09 02 4e 02 05 67 eb 9b g..X..ei..N..g.. +| 1696: a1 72 09 04 81 06 02 01 68 01 04 82 13 02 01 69 .r......h......i +| 1712: 08 04 84 39 01 04 82 45 02 01 6a 0a 04 83 72 02 ...9...E..j...r. +| 1728: 02 6b 70 07 04 81 25 02 01 6c 0a 02 5a 02 01 6d .kp...%..l..Z..m +| 1744: 03 08 82 58 81 11 01 04 84 2c 02 01 6e 07 02 74 ...X.....,..n..t +| 1760: 02 01 6f 05 04 82 7d 02 01 71 0a 02 7c 02 02 72 ..o......q..|..r +| 1776: 75 05 04 83 09 02 01 74 03 04 83 1d 02 01 75 07 u......t......u. +| 1792: 04 84 21 02 01 76 07 04 84 49 02 01 77 03 02 09 ..!..v...I..w... +| 1808: 02 04 81 01 02 01 78 06 04 81 0f 02 01 7a 04 04 ......x......z.. +| 1824: 81 3e 02 04 c2 aa 64 6f 05 04 83 11 03 02 bc 64 .>....do.......d +| 1840: 09 04 81 7b 02 05 c6 80 39 63 64 01 04 83 62 03 ........9cd...b. +| 1856: 01 92 09 04 82 42 02 02 ce bc 09 02 4d 02 02 cf .....B......M... +| 1872: 97 06 04 83 42 02 02 d1 8b 02 02 2d 02 05 dd b1 ....B......-.... +| 1888: ca b2 70 01 02 1b 02 03 e6 b5 96 09 04 81 50 02 ..p...........P. +| 1904: 04 e8 b8 aa 6e 08 04 82 1a 02 05 f0 90 b2 b2 6c ....n..........l +| 1920: 01 02 59 02 04 f1 8f 99 80 0a 04 83 25 01 01 6f ..Y.........%..o +| 1936: 01 26 05 6b 0f 30 12 1f 16 5d 19 08 20 08 33 33 .&.k.0...].. .33 +| 1952: 1e 12 31 31 06 01 16 04 63 33 67 24 2b 81 0a 5d ..11....c3g$+..] +| 1968: 2c 0b 01 14 18 0f 36 82 49 0c 1c 08 27 4b 01 1a ,.....6.I...'K.. +| 1984: 4b 24 3a 13 64 07 14 0b 43 08 68 2e 3c 01 26 1a K$:.d...C.h.<.&. +| 2000: 0b 06 58 10 3d 1b 0a 2f 3e 27 36 1b 1b 21 05 25 ..X.=../>'6..!.% +| 2016: 0d 41 01 12 81 16 4b 2e 7c 7d 32 1f 1e 01 1a 2e .A....K.|.2..... +| 2032: 20 62 72 11 22 05 20 0a 2c 24 60 5e 01 2a 0e 5e br... .,$`^.*.^ +| 2048: 13 22 2d 1c 0c 1f 0c 10 0c 47 05 4e 03 15 06 33 ..-......G.N...3 +| 2064: 1d 06 29 01 10 81 08 81 0b 81 48 53 11 01 20 14 ..).......HS.. . +| 2080: 25 81 17 10 08 08 47 2a 56 2b 2f 20 05 26 22 02 %.....G*V+/ .&.. +| 2096: 01 30 0a 02 5e 03 02 39 76 08 04 82 5c 02 01 31 .0..^..9v......1 +| 2112: 04 02 54 02 01 33 01 04 81 2d 02 02 36 37 09 02 ..T..3...-..67.. +| 2128: 45 03 01 61 04 04 84 16 02 01 37 0a 04 84 5e 02 E..a......7...^. +| 2144: 03 39 c2 b3 01 04 83 2c 02 01 61 08 04 82 4b 02 .9.....,..a...K. +| 2160: 01 62 08 04 83 08 03 02 65 30 07 04 84 0d 02 01 .b......e0...... +| 2176: 64 02 04 82 5e 02 04 81 07 02 05 65 70 6b 7a 35 d...^......epkz5 +| 2192: 02 02 4e 03 03 db 8a 69 01 04 83 0b 02 01 66 05 ..N....i......f. +| 2208: 04 84 34 03 01 68 04 04 84 51 02 01 67 05 02 53 ..4..h...Q..g..S +| 2224: 02 04 82 78 02 01 68 09 04 83 6f 02 01 69 03 04 ...x..h...o..i.. +| 2240: 81 42 02 01 6a 01 04 84 55 03 04 84 10 02 04 81 .B..j...U....... +| 2256: 56 03 02 72 74 06 04 83 20 02 02 6c 7a 01 04 83 V..rt... ..lz... +| 2272: 38 02 01 6e 01 02 15 02 01 6f 01 04 81 7c 01 02 8..n.....o...|.. +| 2288: 27 02 01 73 01 02 36 04 02 2e 03 03 65 ce bc 08 '..s..6.....e... +| 2304: 04 82 43 02 01 74 09 04 82 7f 02 01 75 07 04 82 ..C..t......u... +| 2320: 13 03 01 66 07 04 83 21 03 02 6a 69 07 04 83 7e ...f...!..ji...~ +| 2336: 03 01 6b 01 04 84 26 02 01 76 0a 04 82 7f 03 05 ..k...&..v...... +| 2352: 67 da b2 65 75 0a 04 84 13 03 06 c2 be c9 87 6b g..eu..........k +| 2368: 61 07 04 81 5b 02 02 77 64 01 02 3c 02 01 7a 06 a...[..wd..<..z. +| 2384: 04 81 21 01 04 81 29 02 04 84 4b 03 02 79 30 09 ..!...)...K..y0. +| 2400: 04 83 7d 03 02 ce bc 02 04 81 6a 02 02 c2 aa 05 ..........j..... +| 2416: 02 50 03 01 b2 02 04 81 12 05 02 3e 04 01 63 07 .P.........>..c. +| 2432: 04 84 04 03 03 b3 62 61 01 04 84 3e 03 02 ba 6f ......ba...>...o +| 2448: 02 04 84 51 02 02 c4 91 04 04 84 36 02 05 ce b9 ...Q.......6.... +| 2464: 65 c9 97 07 04 83 76 02 04 ef 82 ac 6d 09 02 5d e.....v.....m..] +| 2480: 02 05 f0 92 bd b3 6f 03 04 81 25 03 04 99 ba 8f ......o...%..... +| 2496: 64 02 04 81 1f 02 05 f3 8a bd b1 6d 09 04 81 28 d..........m...( +| 2512: 01 01 70 01 18 46 7c 1f 14 0a 1b 2e 1c 2d 14 6b ..p..F|......-.k +| 2528: 07 01 18 19 81 05 2e 28 40 11 2b 22 0b 07 18 01 .......(@.+..... +| 2544: 14 40 13 81 36 12 2d 7f 1f 1b 1a 01 12 82 76 1e .@..6.-.......v. +| 2560: 1d 12 81 03 07 12 01 10 5a 81 38 19 36 52 26 18 ........Z.8.6R&. +| 2576: 01 12 64 5d 40 25 05 57 33 31 11 01 1e 36 36 25 ..d]@%.W31...66% +| 2592: 1b 28 07 22 42 13 0f 07 1c 5a 73 51 01 24 07 2d .(..B....ZsQ.$.- +| 2608: 06 03 81 00 22 03 1d 19 18 26 2c 16 23 4f 76 08 .........&,.#Ov. +| 2624: 01 1c 11 1b 54 78 23 1a 81 13 75 04 0c 14 12 0c ....Tx#...u..... +| 2640: 01 12 24 0d 81 4d 11 7f 22 35 2e 02 01 30 02 04 ..$..M...5...0.. +| 2656: 83 5e 02 01 31 0a 04 81 47 03 05 e7 b2 a5 c3 9f .^..1...G....... +| 2672: 04 04 83 16 02 01 35 08 02 35 02 02 36 79 09 04 ......5..5..6y.. +| 2688: 82 24 02 01 37 01 04 81 30 03 02 6d 65 04 04 82 .$..7...0..me... +| 2704: 0e 02 02 38 63 06 02 41 03 02 c9 aa 06 04 84 5b ...8c..A.......[ +| 2720: 02 01 61 03 04 83 0b 03 04 84 4c 03 04 83 5f 03 ..a.......L..._. +| 2736: 01 6c 07 04 83 7f 02 01 63 04 04 84 7b 02 01 64 .l......c......d +| 2752: 04 04 83 5d 02 01 65 06 04 83 4c 03 01 74 08 04 ...]..e...L..t.. +| 2768: 82 0b 02 01 68 03 02 06 03 04 71 e7 8e bb 08 04 ....h.....q..... +| 2784: 81 43 03 01 74 07 04 82 30 02 03 69 ce bc 07 04 .C..t...0..i.... +| 2800: 81 3c 02 03 6a 62 6c 09 02 4b 02 01 6c 09 04 81 .<..jbl..K..l... +| 2816: 60 02 01 6d 05 04 82 6f 04 04 81 62 03 04 6c 74 `..m...o...b..lt +| 2832: c2 b9 07 04 85 08 03 02 c2 be 08 04 83 2b 02 02 .............+.. +| 2848: 6e 73 08 04 81 1f 03 01 75 04 04 83 72 02 01 6f ns......u...r..o +| 2864: 06 08 81 14 83 2f 03 02 ce bc 0a 04 83 67 03 03 ...../.......g.. +| 2880: cf 80 66 07 02 28 02 01 70 01 04 82 2a 03 01 7a ..f..(..p...*..z +| 2896: 08 04 81 0d 02 01 71 07 04 81 37 02 01 73 05 02 ......q...7..s.. +| 2912: 59 03 01 6d 07 02 7a 02 01 74 05 04 82 62 02 02 Y..m..z..t...b.. +| 2928: 75 6a 03 04 81 37 02 02 77 68 08 04 84 14 02 01 uj...7..wh...... +| 2944: 78 01 04 83 6f 02 01 79 01 04 82 66 06 04 81 36 x...o..y...f...6 +| 2960: 02 01 7a 0a 04 81 15 03 05 6f f0 99 8a a6 08 04 ..z......o...... +| 2976: 82 06 02 02 c2 b2 04 04 83 38 03 03 b3 dd 9d 05 .........8...... +| 2992: 04 83 6f 03 01 b9 06 04 83 66 03 01 ba 01 04 81 ..o......f...... +| 3008: 0c 07 04 83 10 04 01 69 06 04 81 39 03 01 bd 04 .......i...9.... +| 3024: 04 82 00 06 02 16 02 02 da 99 02 04 84 49 02 07 .............I.. +| 3040: dd 9b 6e e6 89 a0 75 01 04 81 6e 02 09 f0 92 ab ..n...u...n..... +| 3056: ab ec 8c 8d ca 81 01 04 81 46 01 01 71 01 1a 1a .........F..q... +| 3072: 08 41 65 0b 22 18 69 2f 81 0e 13 05 01 18 08 2d .Ae...i/.......- +| 3088: 07 04 81 70 23 18 20 1a 0c 74 01 16 1f 11 05 6b ...p#. ..t.....k +| 3104: 2f 18 47 13 16 30 4c 01 22 12 05 2d 39 05 07 61 /.G..0L....-9..a +| 3120: 0b 06 0a 56 3f 56 2e 10 0d 50 01 16 1f 1f 3c 2b ...V?V...P....<+ +| 3136: 32 0c 43 4b 81 18 5a 01 18 16 0d 0e 32 40 42 1c 2.CK..Z.....2@B. +| 3152: 4a 24 12 17 13 01 20 03 6e 0e 1c 0f 04 0b 3b 05 J$.... .n.....;. +| 3168: 52 4e 0e 23 26 50 20 01 12 59 09 72 4f 47 44 15 RN.#&P ..Y.rOGD. +| 3184: 41 28 01 16 5e 36 24 0e 19 81 15 07 81 26 16 01 A(..^6$......&.. +| 3200: 18 81 0e 0f 5e 81 01 04 04 1a 1a 44 50 02 02 31 ....^......DP..1 +| 3216: 68 09 04 83 76 02 01 32 07 04 81 70 02 02 33 7a h...v..2...p..3z +| 3232: 05 04 84 4a 02 01 34 08 04 81 02 03 01 69 08 04 ...J..4......i.. +| 3248: 81 0b 03 01 76 02 04 81 62 02 01 37 04 04 81 78 ....v...b..7...x +| 3264: 05 04 82 51 01 02 15 02 03 38 75 77 06 04 82 1b ...Q.....8uw.... +| 3280: 02 01 39 04 02 20 03 04 84 03 03 01 74 03 04 81 ..9.. ......t... +| 3296: 60 02 01 61 04 02 24 01 04 83 63 02 01 62 09 02 `..a..$...c..b.. +| 3312: 15 02 01 63 05 06 51 84 0e 03 03 c2 be 62 04 04 ...c..Q......b.. +| 3328: 82 3d 02 02 64 70 03 04 83 36 02 01 66 06 04 82 .=..dp...6..f... +| 3344: 66 02 02 6a 38 02 04 84 0a 02 02 6d 61 05 04 81 f..j8......ma... +| 3360: 30 03 01 6e 08 04 83 71 02 01 6e 01 02 60 02 01 0..n...q..n..`.. +| 3376: 70 02 04 82 43 02 01 71 05 06 82 48 49 02 01 73 p...C..q...HI..s +| 3392: 06 04 81 1f 02 01 74 09 04 81 70 02 01 75 05 04 ......t...p..u.. +| 3408: 83 44 02 01 76 05 04 84 08 04 04 82 1d 02 01 77 .D..v..........w +| 3424: 09 02 69 02 02 78 69 08 04 82 79 02 01 7a 05 04 ..i..xi...y..z.. +| 3440: 84 22 03 09 66 cf 9f 71 67 36 e2 b1 a5 02 04 81 ....f..qg6...... +| 3456: 21 02 03 c2 aa 6a 01 04 81 12 03 01 b3 03 02 35 !....j.........5 +| 3472: 02 04 84 1f 04 04 84 19 03 04 b9 d4 87 68 02 04 .............h.. +| 3488: 81 42 03 01 ba 06 04 84 44 04 02 64 67 03 04 84 .B......D..dg... +| 3504: 4b 03 01 bc 04 04 81 42 03 01 be 01 02 48 02 02 K......B.....H.. +| 3520: ce bc 0a 04 81 2e 02 02 cf a7 02 04 83 6e 02 04 .............n.. +| 3536: e7 a4 ba 67 07 04 83 59 01 01 72 01 20 41 1d 3a ...g...Y..r. A.: +| 3552: 0b 05 04 12 1f 3c 03 2f 81 05 81 2e 13 01 14 0c .....<./........ +| 3568: 81 3f 17 81 07 63 08 81 07 01 12 81 06 71 17 0b .?...c.......q.. +| 3584: 46 81 5a 13 01 18 02 81 06 0f 49 42 2d 39 2e 2c F.Z.......IB-9., +| 3600: 45 27 01 16 06 19 27 1d 3f 53 81 3a 45 0d 20 01 E'....'.?S.:E. . +| 3616: 14 1b 07 2a 76 37 36 09 82 4d 04 01 14 0c 30 2d ...*v76..M....0- +| 3632: 2d 51 38 31 49 49 74 01 16 29 17 0a 2f 72 1d 4a -Q81IIt..)../r.J +| 3648: 81 27 11 18 01 14 63 81 1c 5f 04 4d 38 19 18 0e .'....c.._.M8... +| 3664: 01 1c 56 2f 25 5f 07 05 03 28 24 5d 2b 4d 42 13 ..V/%_...($]+MB. +| 3680: 02 02 30 71 01 04 84 03 02 01 33 08 04 83 16 01 ..0q......3..... +| 3696: 04 83 3d 02 01 35 03 02 58 02 01 37 04 04 83 0c ..=..5..X..7.... +| 3712: 03 01 64 04 04 84 46 02 02 38 61 0a 04 83 66 03 ..d...F..8a...f. +| 3728: 02 73 69 09 04 82 5f 02 01 39 09 04 84 17 02 02 .si..._..9...... +| 3744: 61 6a 03 04 83 12 02 01 62 07 04 82 72 03 01 65 aj......b...r..e +| 3760: 09 04 83 14 04 01 68 07 04 82 24 6a 09 0d 07 07 ......h...$j.... +| 3776: 06 07 07 07 07 06 07 07 08 07 0f 0d 0d 07 10 07 ................ +| 3792: 07 07 08 0a 06 07 08 0a 06 09 0b 0c 08 10 0c 08 ................ +| 3808: 0a 79 0b 0b 0b 06 07 07 0a 0b 0b 07 07 07 07 06 .y.............. +| 3824: 07 07 08 09 07 09 10 07 07 07 0a 07 08 07 07 07 ................ +| 3840: 07 0c 0d 07 0b 08 0a 0d 09 09 07 07 81 12 07 0b ................ +| 3856: 07 0b 07 07 08 06 0c 08 0b 07 07 0b 07 0b 07 07 ................ +| 3872: 07 07 07 07 07 07 0b 09 0b 07 08 07 06 08 07 0b ................ +| 3888: 07 07 0c 07 09 08 09 08 08 09 09 81 11 07 0a 07 ................ +| 3904: 07 08 07 07 07 0b 07 0b 07 08 06 0d 06 07 06 08 ................ +| 3920: 07 07 07 0a 07 07 0a 08 0b 07 07 08 07 0a 09 0a ................ +| 3936: 0a 0a 81 22 06 08 06 07 07 07 07 09 07 07 08 0b ................ +| 3952: 0a 09 07 07 0a 07 07 0f 08 08 06 0a 09 09 07 07 ................ +| 3968: 07 08 07 07 0b 0c 07 0f 08 08 07 0a 07 09 08 08 ................ +| 3984: 0b 09 0b 0a 0b 81 0b 07 07 0b 06 08 07 08 07 08 ................ +| 4000: 0f 07 07 07 07 07 06 0a 07 09 08 07 0b 0a 08 08 ................ +| 4016: 07 09 08 08 07 07 07 06 06 07 08 08 07 0b 07 0b ................ +| 4032: 08 09 07 0b 07 0a 08 0d 0f 81 13 08 07 08 07 07 ................ +| 4048: 07 0e 09 0a 07 0a 06 08 09 08 07 08 08 07 06 07 ................ +| 4064: 08 07 07 07 0b 06 08 07 0f 09 0e 0a 07 08 07 06 ................ +| 4080: 08 08 0a 81 08 08 0b 06 07 07 08 08 07 08 07 07 ................ +| page 23 offset 90112 +| 0: 0d 00 00 00 01 00 22 00 00 22 00 00 00 00 00 00 ................ +| 32: 00 00 9f 56 88 80 80 80 80 04 04 00 bf 30 00 00 ...V.........0.. +| 48: 0e 8c 03 30 72 64 04 04 81 24 02 01 66 06 04 81 ...0rd...$..f... +| 64: 05 02 04 81 0f 03 01 70 0a 04 82 45 02 01 67 01 .......p...E..g. +| 80: 04 84 1a 02 04 81 03 07 04 82 13 02 01 68 05 04 .............h.. +| 96: 83 1d 02 01 6a 02 04 83 03 02 06 82 26 39 03 01 ....j.......&9.. +| 112: 67 01 04 81 0a 02 02 6c 70 09 02 48 03 03 c9 be g......lp..H.... +| 128: 6e 05 04 83 58 02 01 6e 07 04 83 10 03 02 30 6d n...X..n......0m +| 144: 07 04 84 20 02 03 6f d2 b1 09 04 84 43 02 01 70 ... ..o.....C..p +| 160: 07 02 24 01 02 7d 02 01 71 07 04 83 2f 03 04 81 ..$.....q.../... +| 176: 18 03 01 62 08 04 82 39 02 02 72 75 0a 04 81 24 ...b...9..ru...$ +| 192: 02 01 75 02 04 81 4e 05 04 83 3d 02 01 76 03 02 ..u...N...=..v.. +| 208: 64 03 02 6b 6c 08 04 82 2f 02 01 78 05 04 84 66 d..kl.../..x...f +| 224: 02 02 79 36 02 02 39 02 02 c2 b2 04 04 84 7c 04 ..y6..9.......|. +| 240: 02 dd ab 09 04 83 71 03 02 b3 31 09 04 82 33 03 ......q...1...3. +| 256: 01 bc 04 02 6e 03 01 bd 07 04 85 09 04 01 75 07 ....n.........u. +| 272: 02 0e 02 02 ce bc 07 02 6b 02 02 d1 9e 0a 04 84 ........k....... +| 288: 51 02 03 d5 b1 36 09 02 1e 02 04 e4 8d ac 38 01 Q....6........8. +| 304: 02 61 02 04 f0 96 85 81 09 04 83 5e 03 03 9a 87 .a.........^.... +| 320: 8d 05 04 83 51 01 01 73 01 30 0f 1c 12 09 29 0d ....Q..s.0....). +| 336: 07 2e 81 01 11 2a 22 1d 04 2e 0f 08 04 23 29 0b .....*.......#). +| 352: 3b 15 01 16 2f 2f 81 20 81 07 13 56 26 3b 25 01 ;...//. ...V&;%. +| 368: 28 49 03 0a 61 04 08 2e 03 22 10 38 34 0c 09 29 (I..a......84..) +| 384: 0c 0c 34 28 0b 01 1e 0f 3c 5b 13 0a 34 11 5a 73 ..4(....<[..4.Zs +| 400: 1e 22 03 1a 54 03 01 22 08 06 3c 41 06 2c 2b 16 ....T......^btG7 +| 1328: 20 3d 07 20 0e 01 1e 10 69 42 48 08 04 08 56 08 =. ....iBH...V. +| 1344: 66 21 10 07 1a 34 01 18 1f 28 31 41 81 06 36 19 f!...4...(1A..6. +| 1360: 53 0d 0f 0c 01 1c 16 0a 0e 04 52 17 22 48 7b 43 S.........R..H.C +| 1376: 3e 04 47 44 01 1e 25 4b 0b 30 2c 0a 0c 6b 71 15 >.GD..%K.0,..kq. +| 1392: 03 4c 19 0f 16 01 22 2b 13 28 1d 24 4c 0f 46 0f .L.....+.(.$L.F. +| 1408: 2f 0b 1c 03 0e 4a 4a 33 01 10 19 29 81 24 0a 82 /....JJ3...).$.. +| 1424: 0c 46 02 01 30 05 04 82 5d 02 01 32 07 04 83 1f .F..0...]..2.... +| 1440: 02 02 33 6c 06 04 84 6f 02 01 35 08 02 33 02 06 ..3l...o..5..3.. +| 1456: 37 6e 66 ef 98 bd 06 04 81 35 02 02 61 76 03 06 7nf......5..av.. +| 1472: 82 12 73 02 01 63 02 02 57 02 04 83 09 03 01 69 ..s..c..W......i +| 1488: 05 02 66 02 01 64 06 04 81 01 02 01 65 06 02 3b ..f..d......e..; +| 1504: 03 01 71 01 04 83 6c 02 01 66 01 04 84 7c 01 04 ..q...l..f...|.. +| 1520: 83 5c 03 03 69 c2 b2 08 04 84 74 03 03 73 c2 bd ....i.....t..s.. +| 1536: 03 04 84 0a 02 01 67 06 08 82 1d 82 56 04 04 81 ......g.....V... +| 1552: 33 02 01 68 09 02 51 02 01 69 0a 02 08 02 01 6a 3..h..Q..i.....j +| 1568: 06 02 6c 03 01 6e 0a 04 81 2c 02 01 6c 08 04 83 ..l..n...,..l... +| 1584: 15 02 01 6d 04 04 84 24 03 01 33 01 04 81 3a 03 ...m...$..3...:. +| 1600: 03 6e c2 b9 07 04 81 6b 02 01 6f 06 04 83 28 02 .n.....k..o...(. +| 1616: 03 70 77 71 0a 04 81 2a 02 01 71 0a 04 84 34 02 .pwq...*..q...4. +| 1632: 01 73 05 04 81 7f 03 02 6a 73 01 04 83 08 02 01 .s......js...... +| 1648: 74 02 04 81 60 01 04 83 5e 06 04 82 37 02 01 76 t...`...^...7..v +| 1664: 08 04 81 2d 02 01 77 01 04 82 12 01 02 46 02 03 ...-..w......F.. +| 1680: 78 73 66 04 02 37 03 02 7a 62 02 04 83 1a 02 01 xsf..7..zb...... +| 1696: 79 04 02 1a 03 04 84 2a 02 04 84 22 02 01 7a 05 y......*......z. +| 1712: 04 83 4d 02 02 c2 b2 09 04 83 04 03 01 b9 04 04 ..M............. +| 1728: 82 3e 03 02 bd 6c 07 04 84 06 03 02 be 73 0a 04 .>...l.......s.. +| 1744: 83 5b 02 06 c9 82 71 c6 9a 61 05 02 7a 03 04 89 .[....q..a..z... +| 1760: e3 a1 be 04 04 83 05 02 04 ca aa 33 67 04 04 84 ...........3g... +| 1776: 61 02 03 d6 83 6b 08 04 81 77 02 02 da b0 0a 02 a....k...w...... +| 1792: 32 03 01 b6 0a 02 13 02 03 e3 9a bb 04 04 84 42 2..............B +| 1808: 02 04 f0 b6 8e b7 06 04 83 5a 01 01 76 01 1a 7f .........Z..v... +| 1824: 38 16 7c 3b 26 23 08 05 47 1c 19 20 01 1c 28 45 8.|;&#..G.. ..(E +| 1840: 20 0a 81 0a 45 15 44 05 03 1d 26 44 01 10 0c 3e ...E.D...&D...> +| 1856: 26 3c 6d 82 43 04 01 1c 04 15 3a 56 62 39 16 03 &..xqg2.. +| 2192: 84 72 03 02 76 76 01 04 82 1f 02 03 79 75 6f 04 .r..vv......yuo. +| 2208: 04 84 77 03 04 f6 84 93 a8 05 04 83 6c 02 01 7a ..w.........l..z +| 2224: 02 02 7c 02 04 83 21 05 04 83 07 02 02 c2 aa 01 ..|...!......... +| 2240: 04 81 2f 03 01 bc 0a 04 81 65 02 02 c3 be 01 04 ../......e...... +| 2256: 82 04 02 02 df 94 07 04 82 5f 02 04 ea 80 84 6e ........._.....n +| 2272: 04 04 84 73 03 03 84 b1 75 06 04 84 56 02 03 ef ...s....u...V... +| 2288: 89 9e 07 04 83 7b 02 04 f0 92 ab 9a 0a 04 84 60 ...............` +| 2304: 01 01 77 01 18 49 1d 46 47 07 65 21 05 81 13 1a ..w..I.FG.e!.... +| 2320: 06 01 16 81 7e 5a 26 1b 12 05 0f 44 03 57 01 16 ....~Z&....D.W.. +| 2336: 14 4d 31 81 0d 2c 2a 07 03 81 2c 01 14 23 0a 2a .M1..,*...,..#.* +| 2352: 66 18 65 76 21 5a 5f 01 14 14 81 4c 11 10 24 81 f.ev!Z_....L..$. +| 2368: 49 48 28 01 16 7b 0a 4c 08 10 22 81 2d 03 69 38 IH(....L....-.i8 +| 2384: 01 0e 3c 6e 1f 81 6a 2a 16 01 1a 3b 81 25 0d 28 ....d......i +| 2592: 06 04 83 60 03 01 35 0a 04 82 3d 02 01 6b 09 04 ...`..5...=..k.. +| 2608: 83 34 03 01 70 05 04 81 6e 02 01 6c 09 04 83 28 .4..p...n..l...( +| 2624: 02 01 6d 04 04 82 6f 04 04 83 09 01 04 84 48 02 ..m...o.......H. +| 2640: 04 6e 79 68 31 03 04 83 4d 02 01 6f 03 02 2d 03 .nyh1...M..o..-. +| 2656: 01 6e 09 02 61 02 01 70 04 04 82 67 02 03 71 63 .n..a..p...g..qc +| 2672: 79 09 04 81 0b 03 02 c2 bc 0a 02 30 02 01 72 08 y..........0..r. +| 2688: 02 5e 02 01 73 08 04 81 07 02 01 75 05 02 6c 05 .^..s......u..l. +| 2704: 04 84 56 02 01 76 08 04 81 01 02 01 78 05 04 84 ..V..v......x... +| 2720: 61 02 04 84 6c 02 01 7a 0a 04 83 2e 02 02 c2 ba a...l..z........ +| 2736: 04 04 81 39 03 01 bc 01 04 83 23 02 02 c9 a8 03 ...9......#..... +| 2752: 04 83 38 02 04 ce bc 62 68 02 04 82 25 02 02 d4 ..8....bh...%... +| 2768: 85 08 04 82 2d 02 02 db 91 09 04 84 29 01 01 78 ....-.......)..x +| 2784: 01 0a 27 82 4d 5c 23 01 1c 11 62 38 15 17 67 05 ..'.M.#...b8..g. +| 2800: 3e 05 33 1b 29 1c 26 01 16 0d 81 33 0c 0c 09 61 >.3.).&....3...a +| 2816: 4e 1a 54 47 01 18 22 43 09 60 11 2a 08 1b 81 30 N.TG...C.`.*...0 +| 2832: 27 44 01 24 2d 06 1c 19 2a 5e 22 24 04 2a 5c 0b 'D.$-...*^.$.*.. +| 2848: 42 0d 0f 37 06 2d 01 1a 4c 28 2d 47 14 46 18 45 B..7.-..L(-G.F.E +| 2864: 0e 2c 04 81 1a 01 14 1d 33 75 70 62 32 75 26 2d .,......3upb2u&- +| 2880: 08 01 18 41 0b 0a 0c 1f 17 27 61 81 37 3b 21 01 ...A.....'a.7;!. +| 2896: 1e 39 12 0e 1b 17 17 08 4c 04 81 18 0a 81 12 1a .9......L....... +| 2912: 01 22 0d 11 30 32 1f 5d 31 08 1e 2e 23 3b 03 49 ....02.]1...#;.I +| 2928: 05 6d 03 02 01 30 01 04 81 47 04 02 22 03 02 65 .m...0...G.....e +| 2944: 77 0a 04 82 16 02 01 32 04 08 81 55 81 2e 01 06 w......2...U.... +| 2960: 57 3a 73 02 01 36 0a 02 4f 03 01 7a 04 04 81 27 W:s..6..O..z...' +| 2976: 02 01 61 05 04 82 21 02 01 62 01 02 7e 02 01 63 ..a...!..b..~..c +| 2992: 05 04 81 0c 03 02 c2 b3 08 04 81 24 02 01 65 05 ...........$..e. +| 3008: 04 84 5e 01 04 81 04 01 04 82 3e 01 04 83 46 02 ..^.......>...F. +| 3024: 01 67 02 04 84 09 02 01 68 01 04 83 04 02 01 69 .g......h......i +| 3040: 04 04 82 31 04 04 82 5a 02 01 6a 01 04 81 21 02 ...1...Z..j...!. +| 3056: 01 6b 08 04 83 5e 03 02 68 7a 0a 04 84 67 03 04 .k...^..hz...g.. +| 3072: 77 78 64 6f 04 04 81 4c 02 01 6d 04 04 83 03 03 wxdo...L..m..... +| 3088: 01 37 01 04 83 75 03 01 67 07 04 82 04 02 01 6e .7...u..g......n +| 3104: 06 02 2c 04 04 83 52 02 01 6f 05 04 81 14 02 03 ..,...R..o...... +| 3120: 70 6d 73 08 04 83 68 02 01 71 06 04 84 7c 02 01 pms...h..q...|.. +| 3136: 72 02 04 84 20 03 04 83 33 01 04 82 13 02 04 82 r... ...3....... +| 3152: 17 03 01 69 04 04 81 1c 03 01 76 04 04 84 71 02 ...i......v...q. +| 3168: 01 73 04 04 82 53 02 01 75 01 04 81 3b 05 04 82 .s...S..u...;... +| 3184: 2a 02 01 77 03 04 82 4f 02 01 79 02 02 60 02 01 *..w...O..y..`.. +| 3200: 7a 08 04 81 34 02 04 c2 aa 6b 61 04 04 82 7b 02 z...4....ka..... +| 3216: 02 c6 b4 0a 04 81 02 02 02 ca 83 03 02 5a 02 02 .............Z.. +| 3232: d4 ae 04 04 81 45 02 02 d7 bb 0a 04 83 78 02 02 .....E.......x.. +| 3248: dc 96 06 04 82 1f 02 02 df 95 05 04 82 11 02 03 ................ +| 3264: ea b7 a6 07 04 83 6e 02 04 f0 93 a0 b9 06 04 82 ......n......... +| 3280: 6c 02 05 f5 af 82 82 73 07 02 41 01 01 79 01 1e l......s..A..y.. +| 3296: 07 21 30 08 81 04 17 25 0a 0c 81 17 27 81 25 01 .!0....%....'.%. +| 3312: 1a 03 5d 21 81 16 07 3f 27 16 3f 14 08 18 01 12 ..]!...?'.?..... +| 3328: 68 4f 0e 25 34 3a 3d 81 16 01 12 09 81 0a 3c 72 hO.%4:=...............n..t... +| 3648: 17 02 03 c5 a7 33 0a 04 84 55 02 02 c7 9d 04 04 .....3...U...... +| 3664: 81 60 02 08 ca 81 30 68 f0 90 90 b2 09 04 84 2e .`....0h........ +| 3680: 03 01 b5 02 04 82 33 02 03 d1 9e 6c 02 04 81 1a ......3....l.... +| 3696: 02 02 d4 93 04 04 81 05 02 02 d8 ab 03 04 82 49 ...............I +| 3712: 03 02 b8 73 01 04 84 1d 02 03 e2 af 9d 07 02 4d ...s...........M +| 3728: 02 03 ec 8f ad 03 04 84 47 01 01 7a 01 22 1d 42 ........G..z...B +| 3744: 10 5f 1b 08 63 1e 07 0a 30 07 0b 26 31 61 07 01 ._..c...0..&1a.. +| 3760: 1c 0a 37 29 0a 68 40 38 0a 4a 04 08 0b 07 0f 07 ..7).h@8.J...... +| 3776: 0c 07 07 09 07 08 09 09 0b 07 08 0b 06 08 07 07 ................ +| 3792: 08 08 08 06 07 06 07 08 08 09 0a 09 81 31 06 07 .............1.. +| 3808: 0d 06 06 07 0a 07 08 0e 08 07 07 0b 06 07 09 07 ................ +| 3824: 0b 07 07 07 0c 07 07 07 07 08 09 08 07 0a 81 14 ................ +| 3840: 07 07 06 0b 08 06 07 09 07 07 0f 0e 06 0b 0b 0a ................ +| 3856: 0c 07 06 06 09 06 08 0b 09 07 08 0f 0b 07 0c 08 ................ +| 3872: 06 07 07 09 08 09 07 09 0a 81 20 07 07 08 06 0c .......... ..... +| 3888: 09 0a 06 07 06 07 0b 09 09 0d 06 06 06 07 07 07 ................ +| 3904: 07 09 07 09 07 07 08 0f 07 0a 08 08 0e 07 08 07 ................ +| 3920: 08 08 0b 0a 0a 09 07 06 09 0a 81 10 07 06 09 0a ................ +| 3936: 07 08 07 07 08 07 0d 0a 07 07 0a 07 08 07 13 0a ................ +| 3952: 08 08 08 08 07 09 0a 08 09 0a 0e 08 07 08 08 0a ................ +| 3968: 09 09 0a 81 08 07 08 08 0b 08 07 07 08 0b 07 0b ................ +| 3984: 0b 0a 07 07 07 07 07 07 07 07 07 07 0f 0a 06 06 ................ +| 4000: 07 09 07 06 07 0a 07 0b 07 08 07 08 0a 08 08 81 ................ +| 4016: 16 0a 08 0e 06 07 07 06 07 08 13 07 07 0b 07 07 ................ +| 4032: 08 0a 07 07 07 0a 07 09 07 13 07 07 07 0b 07 06 ................ +| 4048: 07 0a 08 07 08 08 08 08 09 0a 0a 81 06 07 0e 07 ................ +| 4064: 0b 06 0b 0b 0b 07 08 07 0f 07 06 08 07 09 0b 0a ................ +| 4080: 06 0e 07 08 09 07 09 08 0e 07 09 08 08 08 08 09 ................ +| page 24 offset 94208 +| 0: 0d 00 00 00 01 00 28 00 00 28 00 00 00 00 00 00 ......(..(...... +| 32: 00 00 00 00 00 00 00 00 9f 50 88 80 80 80 80 05 .........P...... +| 48: 04 00 bf 24 00 09 0e 5b 14 06 1c 0d 12 03 18 37 ...$...[.......7 +| 64: 1f 05 55 32 10 15 5a 81 07 4b 04 01 16 07 0e 5f ..U2..Z..K....._ +| 80: 0b 46 0b 22 81 41 24 23 01 28 2a 07 11 09 15 0b .F...A$#.(*..... +| 96: 0f 0c 09 0a 1e 05 1f 5e 07 19 1c 1e 33 3e 01 18 .......^....3>.. +| 112: 26 0c 1b 43 08 38 11 05 81 1b 5f 20 01 1e 06 1c &..C.8...._ .... +| 128: 36 27 33 20 53 4f 06 03 1a 2f 2c 40 6d 01 18 72 6'3 SO.../,@m..r +| 144: 81 0d 20 3a 23 05 34 19 23 2b 36 01 12 2c 4d 0c .. :#.4.#+6..,M. +| 160: 59 82 04 46 40 0f 01 1a 28 15 32 33 2e 52 0f 49 Y..F@...(.23.R.I +| 176: 04 09 81 33 26 03 30 7a 34 01 04 82 05 02 01 36 ...3&.0z4......6 +| 192: 06 04 81 5a 02 04 37 ce bc 74 09 04 81 17 02 01 ...Z..7..t...... +| 208: 38 01 04 83 79 03 03 dd 9a 73 02 04 84 12 02 01 8...y....s...... +| 224: 61 04 04 82 1c 02 01 62 01 04 83 4e 07 04 82 5b a......b...N...[ +| 240: 02 01 63 0a 02 4e 02 01 64 04 04 84 7d 02 01 65 ..c..N..d......e +| 256: 01 04 84 2a 02 01 67 01 04 81 75 02 04 68 d0 bf ...*..g...u..h.. +| 272: 70 09 04 81 4a 02 01 69 01 04 84 34 05 04 82 48 p...J..i...4...H +| 288: 02 01 6a 02 02 1d 04 04 81 54 02 01 6b 03 04 81 ..j......T..k... +| 304: 0d 02 01 6c 09 04 83 54 02 01 6e 06 02 0b 01 04 ...l...T..n..... +| 320: 84 1c 02 01 6f 0a 02 26 03 01 6a 08 04 82 26 03 ....o..&..j...&. +| 336: 05 73 75 c2 bd 61 03 02 38 02 02 70 37 06 04 81 .su..a..8..p7... +| 352: 7e 02 01 71 01 04 83 67 03 01 65 06 04 84 08 02 ~..q...g..e..... +| 368: 01 72 0a 04 82 1e 02 01 74 01 04 82 6c 02 02 5d .r......t...l..] +| 384: 04 04 84 17 03 04 82 77 02 01 75 04 08 82 22 82 .......w..u..... +| 400: 0b 02 02 76 6c 03 04 83 25 02 01 77 03 04 81 24 ...vl...%..w...$ +| 416: 03 04 82 42 02 01 78 06 04 82 4a 03 02 c2 b2 02 ...B..x...J..... +| 432: 04 83 21 02 01 79 09 04 83 43 02 01 7a 04 02 51 ..!..y...C..z..Q +| 448: 02 02 c2 aa 02 02 51 03 01 b3 09 04 81 2a 03 01 ......Q......*.. +| 464: b9 06 04 82 73 03 02 bd 73 04 02 4e 04 05 76 c2 ....s...s..N..v. +| 480: be 67 65 08 02 1d 04 03 78 de bc 02 04 83 6d 04 .ge.....x.....m. +| 496: 01 7a 09 04 84 1d 02 04 ca a2 36 32 07 04 83 2e .z........62.... +| 512: 02 02 d8 aa 04 04 84 31 02 02 df 8a 02 02 73 01 .......1......s. +| 528: 02 c2 aa 01 08 81 7f 82 01 01 06 56 83 70 02 06 ...........V.p.. +| 544: 74 82 6d 01 06 81 5d 45 01 04 82 7b 01 0c 81 46 t.m...]E.......F +| 560: 13 6a 82 12 01 0c 82 24 63 15 35 74 01 12 81 14 .j.....$c.5t.... +| 576: 81 24 1f 3d 7c 15 1c 01 0a 82 39 1a 81 6f 03 02 .$.=|.....9..o.. +| 592: 30 65 07 04 83 3a 03 01 63 03 04 81 7d 03 01 65 0e...:..c......e +| 608: 09 02 03 03 01 66 01 02 6c 04 02 66 7a 03 04 82 .....f..l..fz... +| 624: 1c 03 01 6b 0a 04 82 46 03 01 7a 08 04 84 2d 03 ...k...F..z...-. +| 640: 03 c2 bd 73 0a 04 81 10 05 02 c2 bc 03 02 03 04 ...s............ +| 656: 01 be 02 04 84 35 03 04 f0 9f ad ae 08 04 83 41 .....5.........A +| 672: 02 01 b2 01 0a 3e 77 21 82 5f 01 08 81 74 82 68 .....>w!._...t.h +| 688: 01 0e 39 81 0c 81 4b 1c 4e 01 0e 83 46 06 3b 1d ..9...K.N...F.;. +| 704: 0e 38 01 0e 81 50 15 81 53 35 57 01 08 82 7d 26 .8...P..S5W....& +| 720: 1c 01 04 2f 0e 01 08 66 5d 83 24 01 0e 43 81 10 .../...f].$..C.. +| 736: 24 82 77 04 01 0a 82 0a 81 61 1f 03 01 35 08 04 $.w......a...5.. +| 752: 81 11 03 01 61 09 04 82 0b 03 01 64 07 04 81 7e ....a......d...~ +| 768: 03 01 66 05 04 82 29 03 01 67 03 04 84 17 03 04 ..f...)..g...... +| 784: 69 34 d8 a1 05 04 82 42 03 01 73 04 04 84 3d 03 i4.....B..s...=. +| 800: 01 76 06 04 81 70 04 03 76 c2 ba 07 04 81 34 03 .v...p..v.....4. +| 816: 01 78 06 04 83 48 03 03 c2 b3 36 07 02 7f 03 02 .x...H....6..... +| 832: d4 85 04 04 81 2d 03 02 d9 bc 05 04 83 20 03 04 .....-....... .. +| 848: ee b5 9c 70 03 04 83 57 03 05 f6 80 80 bb 70 05 ...p...W......p. +| 864: 04 81 6a 02 01 b3 01 06 82 41 12 01 08 67 0d 82 ..j......A...g.. +| 880: 69 01 0e 81 51 22 5c 7d 81 00 01 10 68 05 2f 81 i...Q.......h./. +| 896: 4e 81 78 03 01 0a 74 81 58 81 72 01 0c 4e 72 07 N.x...t.X.r..Nr. +| 912: 79 81 7d 01 10 44 3b 58 66 82 08 0c 2f 01 06 0c y....D;Xf.../... +| 928: 65 36 01 04 81 02 01 08 5d 43 82 10 03 01 61 06 e6......]C....a. +| 944: 04 83 3c 03 01 63 07 04 82 32 03 01 68 03 04 82 ..<..c...2..h... +| 960: 76 03 03 6a 77 6d 0a 04 84 35 03 01 70 01 04 84 v..jwm...5..p... +| 976: 70 03 01 74 01 06 84 05 0a 06 02 47 04 04 6a 75 p..t.......G..ju +| 992: ce bc 07 04 82 4d 03 02 75 65 07 04 81 05 03 01 .....M..ue...... +| 1008: 77 01 04 81 59 04 03 6f db 85 08 02 3d 03 04 c2 w...Y..o....=... +| 1024: be c2 b9 01 04 81 04 03 07 c4 a7 67 d5 bb 7a 68 ...........g..zh +| 1040: 06 04 84 05 03 02 c7 a5 02 04 81 10 02 01 b9 01 ................ +| 1056: 0c 1e 81 43 0b 81 56 01 08 82 70 81 27 01 06 34 ...C..V...p.'..4 +| 1072: 83 38 01 0c 81 0f 1d 04 81 74 01 06 82 58 65 01 .8.......t...Xe. +| 1088: 10 27 55 7a 4b 81 00 2c 29 01 0e 04 1f 24 09 83 .'UzK..,)....$.. +| 1104: 44 04 01 08 65 2f 41 2b 01 0c 34 0c 81 17 83 0f D...e/A+..4..... +| 1120: 01 04 82 69 03 02 61 73 02 02 4d 03 01 65 01 04 ...i..as..M..e.. +| 1136: 81 2e 03 02 69 61 04 04 83 06 04 03 69 69 67 08 ....ia......iig. +| 1152: 02 1b 03 03 6b c2 ba 06 04 82 49 03 01 6d 04 04 ....k.....I..m.. +| 1168: 84 67 03 01 6f 02 04 82 4b 03 01 70 06 04 84 34 .g..o...K..p...4 +| 1184: 03 04 72 61 76 76 04 02 67 03 03 73 c2 be 07 04 ..ravv..g..s.... +| 1200: 82 17 03 01 76 03 04 81 56 03 01 78 02 04 84 24 ....v...V..x...$ +| 1216: 03 03 79 69 38 05 04 84 68 03 05 c2 aa 63 c2 b2 ..yi8...h....c.. +| 1232: 08 02 69 04 01 b9 06 04 82 75 04 01 ba 01 04 81 ..i......u...... +| 1248: 51 04 01 bc 0a 04 81 0f 03 02 c5 a7 0a 04 84 71 Q..............q +| 1264: 03 05 c9 99 ee 84 af 03 04 84 10 04 06 9f db a5 ................ +| 1280: 30 71 68 07 04 82 46 03 05 ca 8b 64 78 66 0a 04 0qh...F....dxf.. +| 1296: 84 01 03 02 dd 9b 0a 04 82 4e 03 02 de bd 08 04 .........N...... +| 1312: 82 03 03 07 eb b1 bd f2 84 82 8d 04 04 84 23 02 ..............#. +| 1328: 01 ba 01 0c 2d 81 55 09 82 17 01 04 07 64 01 0e ....-.U......d.. +| 1344: 33 81 73 0e 36 81 63 01 0e 2f 60 82 09 5b 5f 2c 3.s.6.c../`..[_, +| 1360: 01 08 82 6d 81 69 01 0c 81 2d 3c 35 81 0b 01 10 ...m.i...-<5.... +| 1376: 4f 81 08 03 82 4f 29 27 01 0c 0f 03 55 82 6d 10 O....O)'....U.m. +| 1392: 01 08 0d 82 16 1e 01 06 2c 82 56 03 01 33 05 04 ........,.V..3.. +| 1408: 82 25 03 01 36 07 04 82 73 03 01 63 0a 02 48 03 .%..6...s..c..H. +| 1424: 02 66 6f 07 04 84 60 03 01 67 03 04 84 27 03 01 .fo...`..g...'.. +| 1440: 6c 0a 02 63 03 01 6d 07 04 82 75 03 01 6f 08 04 l..c..m...u..o.. +| 1456: 82 4d 04 04 61 74 d4 a5 02 04 83 3c 03 02 d9 a8 .M..at.....<.... +| 1472: 04 04 83 4e 03 03 db 93 61 02 04 81 52 03 03 e1 ...N....a...R... +| 1488: bf a7 08 04 83 42 02 01 bc 01 06 81 6c 2f 01 06 .....B......l/.. +| 1504: 81 31 32 01 06 30 81 02 01 0c 81 02 81 73 04 67 .12..0.......s.g +| 1520: 01 06 81 6c 20 01 0e 57 6d 04 81 1f 81 24 01 04 ...l ..Wm....$.. +| 1536: 83 54 01 0c 70 49 81 59 81 11 01 0e 56 81 44 0d .T..pI.Y....V.D. +| 1552: 3d 81 23 01 0a 83 15 81 6b 06 03 02 39 6d 0a 02 =.#.....k...9m.. +| 1568: 59 03 03 62 c2 aa 03 04 82 2f 03 01 63 02 04 84 Y..b...../..c... +| 1584: 45 04 05 79 76 7a c2 ba 04 04 81 19 03 01 66 07 E..yvz........f. +| 1600: 04 81 74 03 01 69 08 04 83 59 03 02 6f 37 09 04 ..t..i...Y..o7.. +| 1616: 84 38 03 01 72 04 04 81 3f 03 01 73 07 02 69 03 .8..r...?..s..i. +| 1632: 03 79 76 62 04 02 76 03 02 c2 b2 06 04 84 48 04 .yvb..v.......H. +| 1648: 03 b3 34 79 02 04 81 3d 04 01 bc 08 04 82 19 03 ..4y...=........ +| 1664: 04 ce bc ce bc 01 04 84 4a 03 04 e7 a8 a1 79 01 ........J.....y. +| 1680: 02 03 02 01 bd 01 06 58 82 1d 01 0a 82 7b 81 02 .......X........ +| 1696: 57 01 04 0a 6b 01 08 81 5f 70 13 01 12 0e 38 65 W...k..._p....8e +| 1712: 11 34 52 4c 5c 75 01 06 82 56 3e 01 02 23 01 0a .4RL.u...V>..#.. +| 1728: 81 29 08 83 2f 01 0c 81 43 81 48 2f 03 01 08 83 .)../...C.H/.... +| 1744: 5a 76 3a 03 01 30 04 04 81 71 03 01 32 01 04 82 Zv:..0...q..2... +| 1760: 1d 03 01 36 02 04 84 34 03 01 61 09 04 82 41 03 ...6...4..a...A. +| 1776: 01 68 07 02 38 03 01 6a 08 04 84 15 03 01 6c 08 .h..8..j......l. +| 1792: 04 82 66 03 01 6d 08 04 82 69 03 01 6f 01 04 82 ..f..m...i..o... +| 1808: 40 02 04 84 2d 03 02 78 03 01 73 04 08 81 29 82 @...-..x..s...). +| 1824: 08 03 03 75 75 6b 06 04 84 21 03 01 78 05 04 83 ...uuk...!..x... +| 1840: 61 04 04 c2 be c2 bc 06 04 82 5c 03 01 79 01 04 a............y.. +| 1856: 83 46 03 02 c2 b3 06 02 40 02 01 be 01 08 81 0f .F......@....... +| 1872: 81 71 01 0e 81 25 60 81 39 1d 70 01 06 1b 83 53 .q...%`.9.p....S +| 1888: 01 06 83 37 52 01 10 6b 65 18 2a 81 4d 47 33 01 ...7R..ke.*.MG3. +| 1904: 06 82 79 07 01 02 46 01 08 03 13 81 64 01 0c 5a ..y...F.....d..Z +| 1920: 65 76 82 33 04 01 08 82 61 82 19 03 02 35 64 01 ev.3....a....5d. +| 1936: 04 84 17 03 01 37 06 04 82 10 03 01 62 08 02 5b .....7......b..[ +| 1952: 03 01 66 07 04 83 04 03 01 67 06 02 36 03 01 6a ..f......g..6..j +| 1968: 01 04 83 3e 08 04 83 59 03 01 71 01 04 82 47 03 ...>...Y..q...G. +| 1984: 01 72 08 06 81 73 32 03 01 75 09 04 82 35 03 02 .r...s2..u...5.. +| 2000: 77 76 07 04 83 68 03 01 78 05 02 72 03 02 c2 b3 wv...h..x..r.... +| 2016: 0a 04 81 7e 04 01 bd 05 04 84 35 03 02 cb 8a 03 ...~......5..... +| 2032: 04 84 46 03 02 ce bc 02 04 82 72 01 05 c3 9f ca ..F.......r..... +| 2048: 83 74 03 04 81 58 02 01 a6 08 04 84 26 02 04 82 .t...X......&... +| 2064: 1d 02 01 be 06 04 81 4f 03 04 83 1d 01 02 c4 a7 .......O........ +| 2080: 01 02 0e 05 04 81 7c 04 04 84 09 02 01 b3 01 04 ......|......... +| 2096: 81 43 09 04 82 70 03 02 67 72 07 04 83 06 02 01 .C...p..gr...... +| 2112: b8 06 04 83 09 03 04 78 6d 77 75 07 04 82 69 01 .......xmwu...i. +| 2128: 02 c5 80 06 04 83 40 02 01 82 06 04 82 6a 03 04 ......@......j.. +| 2144: 82 1a 02 01 89 07 08 81 39 81 6c 02 01 8b 06 04 ........9.l..... +| 2160: 81 1a 03 01 6a 02 02 26 02 01 93 04 04 83 49 03 ....j..&......I. +| 2176: 02 6e 70 03 02 0f 01 02 c6 83 01 04 82 5b 06 04 .np..........[.. +| 2192: 83 6a 02 01 85 02 04 82 0d 03 01 31 09 02 41 03 .j.........1..A. +| 2208: 01 6b 0a 04 84 0d 02 01 88 04 06 82 16 7f 02 01 .k.............. +| 2224: 95 06 04 81 50 02 04 84 18 01 04 84 3f 03 01 78 ....P.......?..x +| 2240: 06 04 84 11 02 01 99 05 04 83 38 01 04 81 4a 02 ..........8...J. +| 2256: 01 9a 04 04 81 16 02 02 9b 62 03 02 10 02 01 a3 .........b...... +| 2272: 04 04 82 0d 01 04 84 64 02 01 a5 05 06 81 3d 61 .......d......=a +| 2288: 03 02 79 69 05 04 82 23 02 01 a8 09 04 83 44 02 ..yi...#......D. +| 2304: 01 ab 03 04 81 13 02 02 ad 75 06 04 82 07 02 01 .........u...... +| 2320: b4 05 04 82 74 03 04 75 61 65 6c 05 04 82 68 02 ....t..uael...h. +| 2336: 01 b6 03 04 81 6b 02 01 ba 05 04 83 72 02 01 bd .....k......r... +| 2352: 06 04 84 15 02 02 be 74 02 04 84 57 03 01 79 07 .......t...W..y. +| 2368: 04 82 5d 02 02 bf 67 09 04 81 42 03 01 72 03 04 ..]...g...B..r.. +| 2384: 82 7a 03 01 77 0a 02 3c 01 02 c7 80 02 04 84 1d .z..w..<........ +| 2400: 03 01 31 04 04 83 1d 02 03 81 c9 ab 06 04 83 4d ..1............M +| 2416: 02 01 82 09 04 83 25 02 03 86 d3 97 05 02 34 02 ......%.......4. +| 2432: 02 89 78 02 04 83 19 02 01 8c 03 02 56 02 04 81 ..x.........V... +| 2448: 3b 02 04 82 16 02 01 9c 03 04 82 55 04 04 81 08 ;..........U.... +| 2464: 02 01 9d 07 04 81 1c 03 01 68 04 04 83 11 02 01 .........h...... +| 2480: 9f 05 04 81 0e 02 01 a1 01 04 84 79 08 04 82 27 ...........y...' +| 2496: 02 01 a3 04 04 81 5c 06 02 33 02 02 a5 76 0a 04 .........3...v.. +| 2512: 82 07 02 08 ad 7a c8 bf e0 a4 8a 64 04 04 81 03 .....z.....d.... +| 2528: 02 01 af 05 04 84 25 02 01 b3 03 02 5c 02 01 bb ......%......... +| 2544: 06 02 06 03 01 68 03 04 81 2c 03 01 71 04 04 81 .....h...,..q... +| 2560: 69 02 01 bd 03 06 81 7a 6f 02 04 84 2f 01 03 c8 i......zo.../... +| 2576: 9d 36 02 04 83 43 02 02 a1 70 01 04 82 4a 02 01 .6...C...p...J.. +| 2592: a5 02 04 83 45 02 01 ad 01 04 81 10 02 01 b1 02 ....E........... +| 2608: 04 84 4c 07 04 84 03 02 01 bc 08 04 81 3b 03 01 ..L..........;.. +| 2624: 31 03 04 83 45 02 01 bf 07 04 81 17 01 02 c9 80 1...E........... +| 2640: 01 02 3a 01 04 83 62 02 01 82 02 04 81 55 04 04 ..:...b......U.. +| 2656: 84 6d 02 02 50 02 01 8f 02 04 84 16 06 04 83 57 .m..P..........W +| 2672: 02 01 91 04 04 83 4f 02 01 93 0a 04 81 1d 03 01 ......O......... +| 2688: 69 05 04 83 37 03 01 79 08 04 82 21 02 01 96 04 i...7..y...!.... +| 2704: 04 82 13 04 02 6c 02 04 83 29 02 01 97 04 02 2d .....l...).....- +| 2720: 02 03 99 6d 74 05 04 81 73 02 01 9f 09 04 84 63 ...mt...s......c +| 2736: 02 01 a0 05 02 6a 02 02 76 02 01 a2 02 02 2e 02 .....j..v....... +| 2752: 01 a3 01 04 82 16 02 01 a5 04 04 81 31 06 04 83 ............1... +| 2768: 0f 02 01 a8 03 04 83 47 02 01 a9 06 06 82 0d 49 .......G.......I +| 2784: 02 01 af 0a 04 83 1c 02 02 b2 34 04 04 81 3c 02 ..........4...<. +| 2800: 04 b5 74 c2 b2 03 04 81 28 02 01 ba 09 04 83 62 ..t.....(......b +| 2816: 02 01 bb 07 04 84 73 02 01 bc 06 04 81 2a 02 01 ......s......*.. +| 2832: bd 04 04 81 68 02 01 be 09 04 83 67 03 01 73 08 ....h......g..s. +| 2848: 04 83 5f 01 02 ca 80 07 04 84 0b 03 01 77 02 04 .._..........w.. +| 2864: 82 59 02 01 81 06 04 82 1c 02 01 83 08 04 83 1b .Y.............. +| 2880: 01 04 81 71 02 01 86 07 04 81 79 02 01 88 08 02 ...q......y..... +| 2896: 62 01 04 81 0a 02 01 89 04 04 81 62 02 01 8a 03 b..........b.... +| 2912: 04 83 11 01 04 83 50 02 01 8c 06 04 84 2e 02 01 ......P......... +| 2928: 8e 03 04 82 3e 04 04 82 33 03 01 68 07 04 82 3b ....>...3..h...; +| 2944: 02 01 8f 04 04 82 63 03 01 73 08 02 7e 02 01 90 ......c..s..~... +| 2960: 08 02 20 02 01 91 01 04 83 52 02 01 94 06 04 83 .. ......R...... +| 2976: 36 02 02 95 7a 0a 02 51 02 04 99 75 73 73 03 02 6...z..Q...uss.. +| 2992: 36 02 01 9c 06 04 81 27 02 01 a5 07 04 81 7b 03 6......'........ +| 3008: 07 e0 a4 aa d5 b8 61 62 0a 02 77 02 02 a6 6f 07 ......ab..w...o. +| 3024: 04 81 07 03 03 ec b2 be 02 04 82 39 02 02 a7 33 ...........9...3 +| 3040: 08 02 61 02 01 a9 03 04 84 07 02 01 b6 0a 04 84 ..a............. +| 3056: 02 02 01 b7 05 04 81 4a 03 03 6a 69 6a 05 04 81 .......J..jij... +| 3072: 48 02 01 b8 09 04 83 0a 03 04 c4 91 d8 bd 03 02 H............... +| 3088: 1c 02 01 b9 09 02 4c 02 04 ba c9 b1 37 07 04 81 ......L.....7... +| 3104: 4e 02 02 bb 70 01 04 84 4c 02 01 bc 01 04 83 0c N...p...L....... +| 3120: 02 02 be 72 05 02 04 01 02 cb 80 0a 04 84 52 02 ...r..........R. +| 3136: 01 88 07 04 83 1d 02 01 8a 08 02 7c 02 01 a1 09 ...........|.... +| 3152: 04 81 1b 03 01 30 06 04 81 52 03 01 6a 01 04 82 .....0...R..j... +| 3168: 34 02 01 a2 01 04 83 30 02 03 a3 71 64 04 02 7e 4......0...qd..~ +| 3184: 02 01 a4 0a 04 83 34 02 01 ae 06 04 84 7f 03 01 ......4......... +| 3200: 6e 06 04 83 68 01 02 cd b1 02 04 82 55 03 01 6a n...h.......U..j +| 3216: 01 04 83 5d 02 01 b7 08 04 81 58 03 01 75 07 04 ...]......X..u.. +| 3232: 84 09 02 01 b8 01 04 82 0e 06 02 22 03 04 82 4f ...............O +| 3248: 03 02 dd af 05 04 84 3f 02 01 bc 01 04 84 3b 02 .......?......;. +| 3264: 01 bd 09 04 81 61 01 04 83 3c 02 03 bf 64 67 02 .....a...<...dg. +| 3280: 04 82 0c 01 05 ce 81 66 da 94 01 02 10 03 01 6b .......f.......k +| 3296: 0a 04 82 7e 02 01 8d 08 04 83 02 02 07 ad 69 f0 ...~..........i. +| 3312: 98 a6 a7 61 09 04 84 41 03 01 72 03 04 82 62 02 ...a...A..r...b. +| 3328: 01 ae 06 04 83 5d 02 04 81 44 03 01 37 06 04 81 .....]...D..7... +| 3344: 08 02 05 af 64 cf 8d 32 01 02 11 02 01 b1 05 04 ....d..2........ +| 3360: 84 52 03 01 61 08 04 81 68 03 01 68 08 04 83 5a .R..a...h..h...Z +| 3376: 02 01 b2 05 04 84 43 01 02 7e 03 04 83 23 02 04 ......C..~...#.. +| 3392: b3 71 30 73 03 04 83 76 02 01 b5 02 04 82 04 02 .q0s...v........ +| 3408: 01 b6 0a 04 84 6e 02 01 b7 03 04 81 36 02 01 b8 .....n......6... +| 3424: 01 04 84 60 02 02 3a 03 04 84 76 01 02 50 03 02 ...`..:...v..P.. +| 3440: 32 6a 09 04 81 0c 03 01 68 03 02 11 02 01 b9 04 2j......h....... +| 3456: 04 84 0f 02 04 bb d2 b5 62 08 04 81 10 02 01 bc ........b....... +| 3472: 01 0e 4d 81 25 3f 1b 81 54 01 0c 55 81 38 06 82 ..M.%?..T..U.8.. +| 3488: 1b 01 08 21 82 66 37 01 08 65 09 83 3c 02 0a 82 ...!.f7..e..<... +| 3504: 21 08 81 52 01 08 83 4e 81 29 01 0e 47 15 81 21 !..R...N.)..G..! +| 3520: 81 0f 65 01 10 12 20 2d 38 81 00 81 73 01 10 0e ..e... -8...s... +| 3536: 33 6c 2d 3e 81 4e 07 03 01 67 03 04 83 52 03 01 3l->.N...g...R.. +| 3552: 6b 08 04 83 72 03 02 6c 32 02 04 81 51 03 03 6d k...r..l2...Q..m +| 3568: 67 79 09 04 82 67 03 01 70 04 04 81 44 03 02 71 gy...g..p...D..q +| 3584: 6a 0a 04 81 6d 03 01 79 01 04 81 5e 03 02 c2 bd j...m..y...^.... +| 3600: 07 04 81 3a 04 01 be 08 02 18 03 02 ce bc 07 02 ...:............ +| 3616: 34 02 01 bd 03 02 28 03 01 67 04 04 82 09 03 02 4.....(..g...... +| 3632: 6e 6c 01 02 22 03 01 78 02 04 83 5f 02 01 bf 02 nl.....x..._.... +| 3648: 02 16 03 01 7a 06 04 82 05 01 02 cf 80 03 02 69 ....z..........i +| 3664: 03 01 6e 05 04 83 40 02 02 81 6c 06 04 84 5d 03 ..n...@...l...]. +| 3680: 02 ca ab 04 04 81 5d 02 01 84 03 04 81 2e 03 04 ......]......... +| 3696: 82 6e 02 01 85 06 04 82 40 02 01 88 04 02 62 02 .n......@.....b. +| 3712: 03 8a 75 6a 03 04 83 50 02 01 8b 05 04 82 60 81 ..uj...P......`. +| 3728: 01 08 07 0a 07 09 07 0b 06 07 07 07 0a 0b 0a 07 ................ +| 3744: 07 0a 06 07 0a 08 07 07 07 12 09 08 0b 07 08 07 ................ +| 3760: 06 07 07 07 07 0a 09 07 0a 08 07 3f 08 07 06 06 ...........?.... +| 3776: 08 07 07 09 07 07 0a 4b 07 07 07 07 07 0a 07 07 .......K........ +| 3792: 09 07 08 08 08 0a 0b 49 07 07 07 09 07 0b 0a 08 .......I........ +| 3808: 07 08 0a 0d 08 48 07 07 08 08 09 07 07 07 09 09 .....H.......... +| 3824: 07 07 09 0a 07 07 07 08 0b 0c 0b 08 08 0d 4c 07 ..............L. +| 3840: 07 06 08 07 06 07 07 0a 08 09 09 44 07 09 07 0b ...........D.... +| 3856: 07 07 08 07 06 08 08 09 07 0a 09 41 07 07 07 07 ...........A.... +| 3872: 06 07 07 07 0e 09 09 07 0a 07 07 42 08 07 06 07 ...........B.... +| 3888: 06 0b 07 08 07 08 06 08 07 08 08 0b 0b 0b 0f 0b ................ +| 3904: 08 07 0a 08 0b 09 07 06 07 07 0c 07 06 07 08 0f ................ +| 3920: 07 0b 07 07 0b 08 08 07 07 08 07 0a 07 07 07 08 ................ +| 3936: 07 08 07 06 08 07 09 07 08 08 0e 0b 07 07 07 0b ................ +| 3952: 0a 08 0e 07 06 06 07 07 0c 09 08 07 07 0b 07 07 ................ +| 3968: 07 0b 0e 0b 07 07 07 07 0e 06 09 07 09 06 07 0b ................ +| 3984: 07 08 07 08 0a 07 07 07 07 07 07 08 07 07 0b 07 ................ +| 4000: 0a 07 0b 07 0b 07 07 06 06 07 07 07 09 07 07 0c ................ +| 4016: 08 09 07 07 07 07 09 07 09 06 0a 08 07 07 08 07 ................ +| 4032: 06 07 07 07 07 08 07 07 07 08 07 07 07 0e 08 07 ................ +| 4048: 0b 09 0a 07 07 0d 07 0b 07 0a 07 07 07 0e 0a 07 ................ +| 4064: 07 07 11 08 06 07 0a 4a 07 07 08 09 07 08 07 08 .......J........ +| 4080: 06 07 06 07 07 07 06 07 07 07 08 08 0b 07 06 09 ................ +| page 25 offset 98304 +| 0: 0d 00 00 00 01 00 29 00 00 29 00 00 00 00 00 00 ......)..)...... +| 32: 00 00 00 00 00 00 00 00 00 9f 4f 88 80 80 80 80 ..........O..... +| 48: 06 04 00 bf 22 00 00 0e 17 04 30 cf 8c 70 03 02 ..........0..p.. +| 64: 0e 02 01 8d 05 04 82 5c 03 01 63 07 04 84 19 02 ..........c..... +| 80: 01 8e 09 02 66 02 01 93 06 04 83 30 02 01 99 06 ....f......0.... +| 96: 04 84 75 04 04 84 07 03 02 c6 a3 05 04 84 01 02 ..u............. +| 112: 01 9b 08 02 42 02 01 9d 07 02 30 03 02 0b 02 01 ....B.....0..... +| 128: 9f 06 04 81 06 03 02 da a9 07 02 66 02 01 a5 09 ...........f.... +| 144: 02 0c 02 01 ab 02 04 81 39 02 01 ad 01 02 6d 03 ........9.....m. +| 160: 04 82 62 02 01 b2 0a 04 83 36 02 03 b3 d6 88 07 ..b......6...... +| 176: 04 84 78 02 01 b8 05 04 84 02 05 04 81 5d 01 02 ..x..........].. +| 192: d0 b1 0a 02 5f 03 01 7a 03 02 76 02 02 b5 35 08 ...._..z..v...5. +| 208: 04 83 63 03 01 75 07 04 84 35 02 04 b8 71 61 61 ..c..u...5...qaa +| 224: 0a 04 81 6b 02 01 bb 08 04 84 4a 02 01 bd 0a 06 ...k......J..... +| 240: 6f 83 6d 01 02 d1 80 06 04 83 65 02 01 84 07 04 o.m.......e..... +| 256: 82 1b 02 01 85 09 04 82 3f 02 02 86 6f 08 04 82 ........?...o... +| 272: 5f 02 01 88 03 04 81 29 02 01 89 02 04 83 14 08 _......)........ +| 288: 04 84 6f 03 01 63 02 04 84 1c 02 01 8a 0a 06 82 ..o..c.......... +| 304: 14 2d 03 02 6c 69 04 04 83 57 02 01 8e 07 04 84 .-..li...W...... +| 320: 48 02 06 90 e1 91 8f 6a 7a 05 04 83 46 02 03 91 H......jz...F... +| 336: 64 37 03 04 82 01 02 01 93 0a 04 82 1f 02 01 98 d7.............. +| 352: 05 04 84 30 02 03 9b 63 66 05 04 81 55 02 05 9f ...0...cf...U... +| 368: 78 6a d1 87 09 04 81 4d 02 02 a3 78 06 02 07 02 xj.....M...x.... +| 384: 04 a5 e1 a9 8f 06 02 10 02 02 af 76 09 04 83 13 ...........v.... +| 400: 03 02 7a 33 07 04 84 1f 02 01 b1 05 04 84 0e 02 ..z3............ +| 416: 02 b7 71 0a 04 82 7b 02 02 bf 62 0a 04 83 6f 01 ..q.......b...o. +| 432: 04 d2 8b 67 73 06 04 83 2b 02 04 97 e3 86 b2 07 ...gs...+....... +| 448: 04 84 12 02 01 9f 02 04 81 05 03 02 24 02 01 a7 ............$... +| 464: 04 04 84 1f 02 01 a9 04 04 81 47 06 06 23 82 23 ..........G..#.# +| 480: 02 02 b1 78 06 02 43 02 01 b9 02 04 82 44 02 01 ...x..C......D.. +| 496: bd 02 04 83 3d 08 04 81 76 02 01 bf 0a 04 85 04 ....=...v....... +| 512: 01 02 d3 8e 06 04 85 02 02 02 8f 68 03 02 75 02 ...........h..u. +| 528: 01 91 08 02 28 02 01 93 05 02 16 02 01 9d 06 04 ....(........... +| 544: 82 5e 02 02 9f 33 06 04 83 78 02 03 a7 6c 78 06 .^...3...x...lx. +| 560: 04 83 33 03 09 da a9 ec 82 b2 e1 87 90 71 06 04 ..3..........q.. +| 576: 84 7b 02 01 ab 06 04 83 27 02 01 ad 03 02 72 02 ........'.....r. +| 592: 01 af 03 04 82 0c 02 01 b5 0a 04 81 7d 02 01 bb ................ +| 608: 0a 02 58 03 01 6a 03 04 81 70 01 02 d4 81 04 04 ..X..j...p...... +| 624: 84 0d 02 01 85 06 04 81 3d 02 02 8b 35 07 04 83 ........=...5... +| 640: 50 02 02 8d 77 03 02 79 02 01 95 0a 04 84 5c 02 P...w..y........ +| 656: 03 97 6a 6c 0a 04 83 7a 02 01 99 09 04 82 26 02 ..jl...z......&. +| 672: 02 9b 71 05 04 82 01 02 02 9d 67 04 04 84 5f 02 ..q.......g..._. +| 688: 01 a7 04 04 82 4a 01 02 d5 a3 0a 04 81 00 03 01 .....J.......... +| 704: 39 06 04 81 51 02 01 a5 06 02 5e 03 01 67 02 04 9...Q.....^..g.. +| 720: 81 0d 02 01 a9 08 04 84 1e 01 02 75 02 01 aa 04 ...........u.... +| 736: 04 81 0a 02 01 ab 03 04 83 21 02 01 ac 06 02 08 .........!...... +| 752: 02 01 ad 0a 02 57 02 02 b3 74 0a 04 84 40 02 03 .....W...t...@.. +| 768: b4 61 6e 08 04 83 1a 03 02 6d 76 02 04 81 0a 02 .an......mv..... +| 784: 01 ba 06 04 83 51 02 01 bb 03 04 82 61 03 01 67 .....Q......a..g +| 800: 08 04 83 28 02 01 bc 01 02 62 01 02 d6 80 01 04 ...(.....b...... +| 816: 84 53 02 02 82 7a 08 02 7f 02 01 83 06 02 50 02 .S...z........P. +| 832: 04 81 32 02 01 86 09 04 82 52 02 01 87 09 04 83 ..2......R...... +| 848: 7b 02 01 8c 03 04 83 19 01 03 d7 89 74 0a 04 82 ............t... +| 864: 37 02 01 92 07 04 81 23 02 01 93 0a 04 81 12 02 7......#........ +| 880: 01 9a 07 02 58 02 01 9e 03 04 81 19 02 01 a0 0a ....X........... +| 896: 04 82 5e 03 01 6c 01 02 0a 03 04 78 c2 b9 63 05 ..^..l.....x..c. +| 912: 02 02 02 01 a8 0a 02 07 02 01 ab 01 04 81 01 02 ................ +| 928: 03 af c3 b0 05 04 82 4e 02 04 b7 69 c2 be 04 04 .......N...i.... +| 944: 84 44 02 05 bd e4 98 a5 6c 03 04 83 51 01 03 d8 .D......l...Q... +| 960: 9c 38 08 04 84 73 02 02 9d 73 0a 04 83 0b 02 01 .8...s...s...... +| 976: aa 02 04 84 56 02 02 ab 7a 03 04 82 7f 02 01 ac ....V...z....... +| 992: 09 04 82 40 02 02 ad 61 03 04 81 14 03 03 c6 8c ...@...a........ +| 1008: 71 09 04 82 56 02 02 b7 69 0a 04 82 5c 02 03 b8 q...V...i....... +| 1024: 66 66 05 04 81 11 03 01 7a 05 04 82 7a 02 01 be ff......z...z... +| 1040: 05 04 81 2a 02 02 bf 76 0a 02 76 01 02 d9 81 01 ...*...v..v..... +| 1056: 04 83 76 02 01 83 02 02 3c 03 01 36 05 04 81 54 ..v.....<..6...T +| 1072: 02 01 85 09 04 82 73 02 01 86 06 04 84 53 02 03 ......s......S.. +| 1088: a7 6a 61 01 04 82 0f 02 01 a8 04 02 38 02 01 b1 .ja.........8... +| 1104: 01 04 83 21 03 03 dc 94 6a 02 02 2b 02 01 b2 07 ...!....j..+.... +| 1120: 02 1a 02 01 b6 04 04 82 17 02 01 ba 04 04 83 7b ................ +| 1136: 03 01 71 03 04 82 77 02 01 bd 09 04 84 4f 01 04 ..q...w......O.. +| 1152: 82 49 01 07 da 80 e8 b6 8a 67 68 05 04 83 04 02 .I.......gh..... +| 1168: 03 85 d1 9c 06 04 81 59 02 01 8f 05 04 81 2c 02 .......Y......,. +| 1184: 01 93 01 04 83 3d 02 04 9a ed 90 8b 01 04 83 44 .....=.........D +| 1200: 02 02 9c 73 09 04 83 21 02 01 a0 0a 04 85 03 02 ...s...!........ +| 1216: 01 a6 05 02 7e 03 04 81 19 02 02 a9 77 05 04 82 ....~.......w... +| 1232: 69 02 01 ac 03 02 4f 04 04 82 76 03 03 67 c2 bc i.....O...v..g.. +| 1248: 07 04 83 38 02 01 b7 03 04 82 5b 02 01 bf 09 02 ...8......[..... +| 1264: 6a 01 02 db 83 08 04 84 55 02 02 93 70 02 04 81 j.......U...p... +| 1280: 47 02 01 a5 05 04 82 12 02 01 a6 01 02 52 01 02 G............R.. +| 1296: dc 8e 01 02 6a 02 02 96 6b 06 04 84 29 02 01 9a ....j...k...)... +| 1312: 05 04 84 05 03 04 82 3e 02 01 9f 09 04 81 64 02 .......>......d. +| 1328: 03 a4 76 77 0a 04 84 0f 02 01 a6 06 04 83 52 02 ..vw..........R. +| 1344: 01 aa 01 04 83 70 02 01 ae 01 04 83 33 01 02 dd .....p......3... +| 1360: 8c 03 04 82 1f 02 02 90 78 09 04 82 4b 02 01 96 ........x...K... +| 1376: 05 04 81 10 03 01 6c 06 04 83 24 02 02 9c 6a 04 ......l...$...j. +| 1392: 04 82 1b 02 05 a0 79 c9 a8 75 01 02 34 02 01 a8 ......y..u..4... +| 1408: 05 02 2c 02 01 b2 06 04 84 3f 02 02 ba 67 03 02 ..,......?...g.. +| 1424: 2f 02 05 bf e0 a6 ad 77 08 02 2f 01 02 de 83 05 /......w../..... +| 1440: 02 65 02 01 84 06 04 81 00 02 01 85 01 04 82 60 .e.............` +| 1456: 01 04 81 57 02 01 8f 03 04 83 3b 02 01 90 03 02 ...W......;..... +| 1472: 7b 02 01 91 07 04 83 5d 02 03 9c 78 61 07 04 81 .......]...xa... +| 1488: 00 02 01 9e 08 04 82 00 02 02 9f 6f 04 04 83 51 ...........o...Q +| 1504: 02 01 a3 04 04 81 2f 02 02 a5 73 0a 04 84 7a 02 ....../...s...z. +| 1520: 02 b1 66 08 04 81 5a 02 02 b7 78 02 02 78 02 01 ..f...Z...x..x.. +| 1536: b9 05 04 83 08 02 01 ba 0a 04 84 76 02 01 bb 07 ...........v.... +| 1552: 02 5c 02 01 bc 08 02 13 02 01 bf 08 02 2a 01 03 .............*.. +| 1568: df 82 66 08 04 82 2c 02 01 87 01 04 83 0a 02 01 ..f...,......... +| 1584: 8d 05 04 84 0c 02 05 91 72 6b 69 61 03 02 45 02 ........rkia..E. +| 1600: 03 97 de a4 09 04 82 6b 02 02 98 62 06 04 81 18 .......k...b.... +| 1616: 02 01 a0 0a 04 82 4a 02 01 bf 05 02 39 01 06 e0 ......J.....9... +| 1632: a7 9d 31 76 63 05 04 83 00 02 02 af aa 02 04 81 ..1vc........... +| 1648: 2b 02 03 b7 ae 65 01 04 84 36 02 02 bb 98 0a 04 +....e...6...... +| 1664: 82 23 01 04 e1 80 88 63 06 04 83 5c 03 01 a2 04 .#.....c........ +| 1680: 02 5c 02 02 85 9b 05 04 81 3e 02 04 8b b3 db 8f .........>...... +| 1696: 07 02 56 02 04 91 b8 64 66 08 04 82 49 02 02 95 ..V....df...I... +| 1712: 9d 08 04 81 4e 03 01 a2 03 04 81 77 02 02 9a a0 ....N......w.... +| 1728: 01 04 82 39 02 03 9d 90 69 03 02 41 02 03 a6 9a ...9....i..A.... +| 1744: 68 04 02 4c 02 05 a7 9c c7 ad 6e 06 04 83 7b 02 h..L......n..... +| 1760: 03 a8 b2 38 04 02 5f 02 02 af 88 07 02 19 02 03 ...8.._......... +| 1776: bd 96 71 03 04 81 11 01 03 e2 83 80 05 02 5f 02 ..q..........._. +| 1792: 02 b1 a6 01 04 83 34 01 04 84 06 02 02 b3 9d 01 ......4......... +| 1808: 04 83 0f 02 03 ba 9a 64 01 04 84 4f 01 03 e3 81 .......d...O.... +| 1824: 81 0a 02 21 02 02 84 8c 03 04 81 10 02 02 90 a1 ...!............ +| 1840: 02 04 81 1b 02 02 9c a3 02 04 82 2e 02 02 a5 89 ................ +| 1856: 04 04 82 23 02 02 a6 98 07 04 83 00 02 09 ab a8 ...#............ +| 1872: 77 31 63 6a c2 aa 35 04 04 81 38 03 01 aa 01 04 w1cj..5...8..... +| 1888: 82 1e 02 02 ac a4 02 02 37 02 05 b9 8e 70 34 79 ........7....p4y +| 1904: 0a 04 84 30 02 02 bd bf 01 04 83 19 01 05 e4 80 ...0............ +| 1920: 83 79 69 04 04 83 15 03 01 bb 01 02 63 02 03 8a .yi.........c... +| 1936: 8d 36 0a 04 84 57 02 05 8c 88 d0 b1 63 09 04 84 .6...W......c... +| 1952: 49 02 02 90 8e 02 04 84 60 02 02 a1 98 03 04 84 I.......`....... +| 1968: 0f 02 02 b1 a2 08 04 83 30 01 04 e5 85 87 68 02 ........0.....h. +| 1984: 04 84 19 02 03 8a a8 39 0a 02 6e 02 03 8d be 38 .......9..n....8 +| 2000: 09 04 81 78 02 04 8f 85 68 30 05 04 84 5a 03 01 ...x....h0...Z.. +| 2016: a7 09 04 82 09 02 03 92 8e 7a 04 02 28 02 02 95 .........z..(... +| 2032: 89 01 02 38 03 01 be 07 04 84 4d 02 08 9e 9e f4 ...8......M..... +| 2048: 95 95 98 68 71 07 04 84 13 02 02 9f 9f 04 04 84 ...hq........... +| 2064: 35 02 02 a0 a2 02 02 24 03 05 b4 f0 90 b1 92 0a 5......$........ +| 2080: 04 82 15 02 03 a3 a1 64 07 04 84 22 02 02 a6 bb .......d........ +| 2096: 04 04 84 52 02 05 a8 af 75 c2 bd 01 04 81 25 02 ...R....u.....%. +| 2112: 02 aa b3 06 04 81 66 02 03 ae be 7a 0a 02 4d 02 ......f....z..M. +| 2128: 08 af 96 66 6b f3 93 af 8f 04 04 83 02 02 03 bd ...fk........... +| 2144: aa 6a 07 02 62 01 04 e6 80 83 69 08 04 84 19 02 .j..b.....i..... +| 2160: 02 81 bc 0a 04 81 25 02 04 86 aa 32 75 06 04 82 ......%....2u... +| 2176: 65 02 05 96 b7 69 35 30 07 02 78 02 03 9d 98 73 e....i50..x....s +| 2192: 06 02 7f 02 02 a3 97 02 04 81 7f 02 05 a5 80 e7 ................ +| 2208: a0 b6 01 04 81 37 03 01 87 08 04 82 61 02 02 b3 .....7......a... +| 2224: b2 03 04 83 1e 02 02 b8 a0 0a 04 84 2e 02 02 bc ................ +| 2240: af 0a 04 83 71 02 02 bf 91 09 04 84 42 01 03 e7 ....q.......B... +| 2256: 81 b4 07 04 84 5e 02 02 86 8e 01 04 84 2c 02 02 .....^.......,.. +| 2272: 8b a9 09 04 82 02 02 03 8d 9d 30 05 04 84 1a 02 ..........0..... +| 2288: 02 8e b6 08 04 83 60 02 02 91 99 0a 04 82 05 02 ......`......... +| 2304: 02 92 af 08 04 81 1c 02 02 94 b9 05 04 81 45 02 ..............E. +| 2320: 02 9c 8f 06 02 14 02 02 9e a2 07 04 83 78 02 02 .............x.. +| 2336: a0 83 06 04 83 08 02 02 a1 a8 01 02 78 02 02 ae ............x... +| 2352: ae 04 04 81 0e 02 02 b5 8c 06 04 84 6c 02 02 bf ............l... +| 2368: 80 09 04 83 1e 01 04 e8 83 93 73 0a 04 83 57 02 ..........s...W. +| 2384: 02 88 a8 0a 04 84 50 02 02 89 a0 09 04 83 70 02 ......P.......p. +| 2400: 02 8a ad 05 04 83 18 02 02 95 84 04 02 3b 03 01 .............;.. +| 2416: b5 06 04 81 26 02 08 a0 91 65 ce bb 6a d7 96 09 ....&....e..j... +| 2432: 04 82 05 02 02 a7 bf 02 04 84 47 02 02 b5 bb 05 ..........G..... +| 2448: 02 0a 02 04 bf 88 6a 6b 03 04 84 2a 01 04 e9 80 ......jk...*.... +| 2464: 97 65 09 04 83 26 02 02 9a 89 04 04 83 29 02 02 .e...&.......).. +| 2480: a4 b8 05 04 82 7f 02 05 ba 83 69 76 6c 04 02 3a ..........ivl..: +| 2496: 03 01 87 03 02 3c 02 02 be b6 06 04 84 25 01 06 .....<.......%.. +| 2512: ea 81 98 64 39 79 03 04 84 12 02 02 88 bc 09 04 ...d9y.......... +| 2528: 84 40 02 03 8c b5 39 01 04 83 11 02 02 91 ab 05 .@....9......... +| 2544: 04 83 01 02 03 9d 97 63 05 04 83 07 02 02 9e a3 .......c........ +| 2560: 04 04 82 04 02 02 ae a3 03 04 81 20 02 02 b2 a1 ........... .... +| 2576: 04 02 66 02 04 b4 9d 65 6b 02 02 29 02 03 ba bd ..f....ek..).... +| 2592: 72 08 04 83 75 02 03 bc a7 37 09 04 81 55 01 03 r...u....7...U.. +| 2608: eb 84 9d 07 02 60 02 06 8f aa 6f 69 6b 68 03 02 .....`....oikh.. +| 2624: 29 02 02 92 90 0a 04 81 30 02 04 99 ba 6e 65 02 ).......0....ne. +| 2640: 04 82 1e 02 02 b8 8f 05 04 81 52 02 02 bd 80 08 ..........R..... +| 2656: 04 84 23 02 02 be bc 08 02 2e 02 02 bf bc 01 04 ..#............. +| 2672: 82 6b 01 04 ec 85 b8 76 05 04 81 76 02 03 99 ad .k.....v...v.... +| 2688: 74 07 02 7e 02 02 a3 80 06 04 81 37 02 04 ac a9 t..~.......7.... +| 2704: 79 75 01 02 6f 02 02 b9 89 06 02 66 01 03 ed 8a yu..o......f.... +| 2720: 8a 05 02 3f 02 02 96 b6 05 02 63 01 04 ee 8d 91 ...?......c..... +| 2736: 7a 02 04 82 2a 02 02 92 af 03 04 84 06 02 03 98 z...*........... +| 2752: be 78 01 04 81 50 02 03 99 82 71 06 02 13 02 04 .x...P....q..... +| 2768: 9c 8d 68 61 09 04 83 1b 02 02 a4 bd 06 04 82 24 ..ha...........$ +| 2784: 02 02 ad a9 06 04 81 5d 02 02 b4 b9 09 04 81 75 .......].......u +| 2800: 02 02 bb 8d 05 04 84 39 01 03 ef af 90 03 04 82 .......9........ +| 2816: 3d 03 02 a3 61 03 04 83 02 02 02 b4 ae 0a 04 82 =...a........... +| 2832: 0e 02 02 b5 b1 06 02 47 02 02 b7 a9 02 04 84 42 .......G.......B +| 2848: 01 05 f0 90 8e bf 34 03 04 81 40 03 04 ad b0 33 ......4...@....3 +| 2864: 38 05 04 84 19 03 02 ae 89 07 04 81 68 03 03 af 8...........h... +| 2880: b7 61 0a 04 82 2f 02 03 91 81 a7 02 04 81 04 03 .a.../.......... +| 2896: 02 89 ac 08 04 83 62 03 02 97 a6 08 04 82 64 03 ......b.......d. +| 2912: 02 af bc 07 04 83 14 03 02 b1 b3 05 04 81 34 03 ..............4. +| 2928: 02 b7 ad 07 04 82 35 03 03 bb 9f 64 07 04 84 4c ......5....d...L +| 2944: 02 03 92 8d a5 05 04 82 7b 03 03 98 ab 62 09 04 .............b.. +| 2960: 81 2f 03 02 b7 99 05 04 82 43 02 04 93 85 98 6b ./.......C.....k +| 2976: 0a 02 1e 03 02 a9 90 01 04 84 62 03 05 b6 b0 74 ..........b....t +| 2992: c2 b3 07 04 82 55 02 03 95 8e a5 06 04 83 2a 03 .....U........*. +| 3008: 02 96 95 09 04 83 0c 03 02 97 8b 06 04 84 60 03 ..............`. +| 3024: 02 b5 bd 07 04 82 56 03 02 b7 9c 06 04 82 2d 02 ......V.......-. +| 3040: 04 96 a0 80 6e 01 04 83 7f 03 02 a5 ad 03 04 83 ....n........... +| 3056: 3c 03 02 ad ac 07 02 72 02 04 97 87 b5 35 07 02 <......r.....5.. +| 3072: 5f 04 01 bc 07 02 53 03 02 8f 90 02 02 70 02 06 _.....S......p.. +| 3088: 98 8f 88 73 7a 78 06 02 35 04 01 ba 02 02 23 03 ...szx..5.....#. +| 3104: 06 a2 92 ca 8a 36 6d 07 04 82 06 02 04 99 8b 93 .....6m......... +| 3120: 7a 09 04 84 6b 03 03 8c 82 72 07 04 81 7a 03 02 z...k....r...z.. +| 3136: 94 ba 04 04 82 3c 03 02 a1 92 0a 04 84 31 03 02 .....<.......1.. +| 3152: a8 b7 08 04 84 16 03 02 b1 ac 03 04 84 28 02 05 .............(.. +| 3168: 9a 99 8d d7 b5 01 04 84 65 03 03 9d 86 7a 09 04 ........e....z.. +| 3184: 84 07 03 03 ac 80 37 09 04 84 24 02 03 9b 87 93 ......7...$..... +| 3200: 04 04 82 60 03 02 91 8b 05 04 84 6c 03 02 a0 b9 ...`.......l.... +| 3216: 05 04 82 13 03 03 a5 81 70 06 04 83 6e 03 02 b6 ........p...n... +| 3232: 9a 03 04 82 00 02 03 9c 8a a3 04 04 83 7e 03 02 .............~.. +| 3248: 96 b7 01 04 83 5b 03 02 a8 88 02 04 83 7a 03 03 .....[.......z.. +| 3264: a9 98 77 02 04 81 38 03 02 b1 b2 04 02 46 03 03 ..w...8......F.. +| 3280: b6 bb 72 03 04 82 1a 03 02 be a9 04 04 83 73 02 ..r...........s. +| 3296: 04 9d 8a 9a 6a 01 02 32 03 02 96 9c 03 02 6f 03 ....j..2......o. +| 3312: 02 a6 b2 04 04 83 35 03 02 b4 a6 05 02 43 03 02 ......5......C.. +| 3328: bc 98 05 02 4a 02 03 9e 8f 98 0a 04 81 08 03 03 ....J........... +| 3344: 90 8c 6d 07 04 84 64 03 03 b4 9f 6e 03 04 84 35 ..m...d....n...5 +| 3360: 02 04 9f a1 a9 69 07 04 84 43 02 03 a7 85 b6 06 .....i...C...... +| 3376: 02 60 02 03 ad ba b7 06 02 2b 02 03 bc b6 b6 01 .`.......+...... +| 3392: 04 81 31 01 04 f1 92 a9 98 06 02 0c 03 03 b1 9d ..1............. +| 3408: 69 08 04 84 65 02 06 9e 91 a5 ea b0 98 07 04 82 i...e........... +| 3424: 43 02 03 a1 b7 be 02 04 81 13 02 03 b4 af 9e 0a C............... +| 3440: 04 84 1d 02 05 b5 92 84 69 6e 07 04 82 52 01 05 ........in...R.. +| 3456: f2 90 9a 83 73 03 04 82 45 02 03 91 ac 81 07 04 ....s...E....... +| 3472: 82 7a 03 02 be 94 02 04 81 46 02 03 94 89 ac 04 .z.......F...... +| 3488: 04 81 4a 02 04 95 b4 ae 39 08 04 82 2a 02 05 a0 ..J.....9...*... +| 3504: 90 b5 66 34 04 04 82 46 02 05 a6 91 b7 d5 bf 06 ..f4...F........ +| 3520: 04 81 7b 02 03 ab a0 93 05 02 2b 02 04 b2 8c 80 ..........+..... +| 3536: 6a 07 04 84 1a 03 02 9c a9 0a 04 81 36 03 02 aa j...........6... +| 3552: bf 04 04 84 34 01 05 f3 82 84 8d 66 0a 04 83 75 ....4......f...u +| 3568: 02 03 88 a7 b4 06 04 83 38 02 03 ab bf a6 02 04 ........8....... +| 3584: 81 65 02 05 b0 ae a8 73 70 0a 02 10 02 04 b8 a6 .e.....sp....... +| 3600: b6 75 05 04 83 4e 02 03 bd b2 84 08 04 83 50 01 .u...N........P. +| 3616: 04 f4 82 91 84 06 04 81 6a 02 03 8c b4 be 06 02 ........j....... +| 3632: 24 02 03 9b b2 83 01 04 84 4b 02 03 a0 87 b0 05 $........K...... +| 3648: 04 83 2f 03 03 ae ba 61 05 04 82 1b 04 08 07 07 ../....a........ +| 3664: 06 07 0b 08 06 09 07 07 06 07 0a 07 09 0b 07 06 ................ +| 3680: 08 07 0a 07 08 08 07 07 08 07 0b 07 08 08 07 0c ................ +| 3696: 09 07 07 09 0b 07 09 08 08 07 08 08 0a 0a 0a 07 ................ +| 3712: 0c 07 07 0b 07 08 07 06 06 07 08 09 0f 07 06 07 ................ +| 3728: 07 06 07 08 07 08 07 07 09 07 08 08 07 08 07 06 ................ +| 3744: 07 0a 07 07 06 06 08 09 08 07 07 07 06 08 07 0a ................ +| 3760: 07 07 07 09 07 07 06 07 07 06 09 06 07 09 0a 0b ................ +| 3776: 09 08 07 08 07 08 09 08 09 07 07 07 08 06 07 07 ................ +| 3792: 07 09 06 07 08 06 07 07 07 0b 0d 09 07 07 0a 08 ................ +| 3808: 07 0a 08 0a 09 07 06 08 08 07 06 07 08 0b 07 09 ................ +| 3824: 07 07 07 08 08 07 07 08 0a 06 07 07 0a 07 07 0b ................ +| 3840: 07 06 07 09 07 08 07 08 08 07 07 07 06 06 06 09 ................ +| 3856: 07 07 0a 09 08 07 06 0c 08 09 08 0a 06 08 09 0a ................ +| 3872: 08 07 08 08 08 0b 08 07 09 08 0c 08 09 08 08 08 ................ +| 3888: 08 08 08 0f 07 07 0b 08 0b 06 09 0b 08 08 08 0a ................ +| 3904: 08 09 0a 07 08 07 07 0e 08 07 0b 09 08 0b 08 08 ................ +| 3920: 0e 08 0a 08 0a 0a 08 08 0b 07 08 08 08 08 09 08 ................ +| 3936: 08 09 08 08 08 08 07 08 08 07 08 08 08 0a 08 08 ................ +| 3952: 08 07 07 0e 08 07 0a 0a 08 08 0a 06 08 0c 08 09 ................ +| 3968: 08 09 08 08 07 09 09 09 08 0b 08 0a 08 08 07 08 ................ +| 3984: 0a 08 08 09 07 08 07 0a 08 09 08 0a 08 08 08 08 ................ +| 4000: 09 08 08 07 08 0b 0a 08 09 09 08 08 08 08 08 09 ................ +| 4016: 09 09 08 09 08 0b 09 08 08 08 08 0a 08 07 09 06 ................ +| 4032: 07 0b 06 0c 0a 09 08 08 08 08 0b 09 09 09 08 08 ................ +| 4048: 09 08 09 08 08 09 07 09 08 09 07 08 07 07 09 09 ................ +| 4064: 09 0a 08 08 09 09 09 0c 09 09 0b 0b 09 08 09 0a ................ +| 4080: 0b 0b 08 0a 08 08 0b 09 09 0a 0a 09 0a 08 09 09 ................ +| page 26 offset 102400 +| 0: 0d 00 00 00 01 0e a7 00 0e a7 00 00 00 00 00 00 ................ +| 3744: 00 00 00 00 00 00 00 82 51 88 80 80 80 80 07 04 ........Q....... +| 3760: 00 85 26 00 00 01 2d 05 30 f4 a3 b5 8c 0a 02 1b ..&...-.0....... +| 3776: 02 03 b4 91 a9 02 04 82 1b 02 04 b7 b5 89 67 06 ..............g. +| 3792: 04 84 2f 01 04 f5 86 a9 bd 01 04 81 13 02 06 91 ../............. +| 3808: be ae ca 9e 78 04 04 81 1d 02 05 99 9b b2 6f 65 ....x.........oe +| 3824: 02 04 84 07 02 03 9e 99 8b 02 02 15 02 03 a1 ab ................ +| 3840: b6 02 04 83 0f 02 03 a3 96 8c 02 04 82 20 02 03 ............. .. +| 3856: ac a3 8d 08 02 55 02 03 b0 80 82 02 04 81 18 02 .....U.......... +| 3872: 03 b1 9c bb 08 02 67 02 03 b5 be 81 07 04 83 79 ......g........y +| 3888: 02 04 bf 81 9d 71 07 04 81 7c 01 04 f6 83 ab 80 .....q...|...... +| 3904: 01 04 81 2a 02 04 89 8c a8 61 01 04 82 30 02 03 ...*.....a...0.. +| 3920: 8e ae 81 07 04 84 05 02 04 8f bd b0 6a 09 04 82 ............j... +| 3936: 1e 02 04 92 95 9c 6c 07 04 82 64 03 02 b4 9f 09 ......l...d..... +| 3952: 04 83 65 02 04 9a a9 9f 34 06 04 83 70 02 03 a4 ..e.....4...p... +| 3968: ad af 01 02 02 02 03 b4 9a 90 07 04 83 5e 01 04 .............^.. +| 3984: f7 87 a5 9d 04 04 84 62 02 03 89 83 8e 0a 04 82 .......b........ +| 4000: 2e 02 03 98 bd 8e 01 04 84 06 02 03 99 a7 8e 02 ................ +| 4016: 04 83 7d 02 03 a0 a1 bb 01 04 81 4e 02 04 a8 86 ...........N.... +| 4032: b8 64 0a 02 34 02 03 b2 9a 9e 05 04 83 16 02 04 .d..4........... +| 4048: b4 a7 93 36 06 04 83 7d 03 02 ba 9c 02 04 83 07 ...6............ +| 4064: 04 09 09 0a 0a 0c 0b 08 09 09 08 09 08 09 0a 0a ................ +| 4080: 0a 09 0a 0a 08 0a 08 09 0a 09 09 09 09 09 09 0a ................ +| end x.db +}]} {} + +do_catchsql_test 74.0.5 { + SELECT matchinfo(1,2); +} {1 {unable to use function matchinfo in the requested context}} + +do_catchsql_test 74.1 { + SELECT rowid, quote(matchinfo(t1,'pxyb... +| 2416: 17 01 05 05 34 74 61 62 6c 03 02 03 01 04 77 68 ....4tabl.....wh +| 2432: 65 72 03 02 06 09 1b 8c 80 80 80 80 0f 03 00 3c er.............< +| 2448: 00 00 00 16 05 34 66 74 73 34 03 02 02 01 04 6e .....4fts4.....n +| 2464: 75 6d 62 03 06 01 04 09 1b 8c 80 80 80 80 0e 03 umb............. +| 2480: 00 3c 00 00 00 16 04 33 74 68 65 03 06 01 01 04 .<.....3the..... +| 2496: 01 03 77 68 65 03 02 04 04 0a 1b 8c 80 80 80 80 ..whe........... +| 2512: 0d 03 00 3c 00 00 00 16 04 33 6e 75 6d 03 06 01 ...<.....3num... +| 2528: 01 05 01 03 74 61 62 03 02 03 04 0a 19 8c 80 80 ....tab......... +| 2544: 80 80 0c 03 00 38 00 00 00 14 03 32 77 68 03 02 .....8.....2wh.. +| 2560: 04 00 04 33 66 74 73 03 02 02 04 07 18 8c 80 80 ...3fts......... +| 2576: 80 80 0b 03 00 36 00 00 00 13 03 32 74 61 03 02 .....6.....2ta.. +| 2592: 03 02 01 68 03 06 01 01 04 04 07 1b 8c 80 80 80 ...h............ +| 2608: 80 0a 03 00 3c 00 00 00 16 03 32 6e 75 03 06 01 ....<.....2nu... +| 2624: 01 05 01 02 6f 66 03 06 01 01 06 04 09 19 8c 80 ....of.......... +| 2640: 82 d0 80 09 03 00 38 00 00 00 14 03 32 66 74 03 ......8.....2ft. +| 2656: 02 02 01 02 69 73 03 06 01 01 03 04 07 18 8c 80 ....is.......... +| 2672: 80 80 80 08 03 00 36 00 00 00 13 02 31 74 03 08 ......6.....1t.. +| 2688: 03 01 01 04 01 01 77 03 02 04 04 09 1a 8c 80 80 ......w......... +| 2704: 80 80 07 03 00 3a 00 00 00 15 02 31 6e 03 08 01 .....:.....1n... +| 2720: 01 02 05 01 01 6f 03 06 01 01 06 04 09 18 8c 80 .....o.......... +| 2736: 81 80 80 06 03 00 36 00 00 00 13 04 02 31 66 03 ......6......1f. +| 2752: 02 02 01 01 69 03 06 01 01 03 05 06 1c 8c 80 80 ....i........... +| 2768: 80 80 05 03 00 3e 00 00 00 17 04 30 74 68 65 03 .....>.....0the. +| 2784: 06 01 01 04 01 05 77 68 65 72 65 03 02 04 0a 15 ......where..... +| 2800: 8c 80 80 80 80 04 03 00 30 00 00 00 11 01 01 06 ........0....... +| 2816: 06 30 74 61 62 6c 65 03 02 03 07 1c 8c 80 80 80 .0table......... +| 2832: 80 03 03 00 3e 00 00 00 17 07 30 6e 75 6d 62 65 ....>.....0numbe +| 2848: 72 03 06 01 01 05 01 02 6f 66 03 06 04 0d 13 8c r.......of...... +| 2864: 80 80 80 80 02 03 00 2c 00 00 00 0f fc 01 03 02 .......,........ +| 2880: 30 6e 03 06 01 00 f2 07 1b 8c 80 80 80 80 01 03 0n.............. +| 2896: 00 3c 00 00 00 16 08 30 66 74 73 34 61 75 78 03 .<.....0fts4aux. +| 2912: 02 02 01 02 69 73 03 06 04 0c 00 00 00 18 ea 00 ....is.......... +| 2928: 00 00 01 01 02 24 00 02 01 01 12 02 01 12 08 88 .....$.......... +| 2944: 80 80 80 80 12 03 00 12 10 00 00 05 02 1c 88 80 ................ +| 2960: 80 80 80 11 03 00 3e 00 00 00 17 05 34 72 6f 77 ......>.....4row +| 2976: 73 02 06 01 01 05 01 04 74 68 65 72 02 02 04 0b s.......ther.... +| 2992: 15 88 80 80 80 80 10 03 00 2f ff ff f0 11 02 01 ........./...... +| 3008: 01 07 05 34 62 65 74 77 02 02 04 08 1b 88 80 80 ...4betw........ +| 3024: 80 80 0f 03 00 3c 00 00 00 16 04 04 33 72 6f 77 .....<......3row +| 3040: 02 06 01 01 05 01 03 74 68 65 02 08 05 0a 1b 88 .......the...... +| 3056: 80 80 80 80 0d 03 00 3c 00 00 00 16 01 01 02 04 .......<........ +| 3072: 33 61 72 65 02 02 03 01 03 62 65 74 02 02 07 08 3are.....bet.... +| 3088: 1b 88 80 80 80 80 0d 03 00 3c 00 00 00 16 03 32 .........<.....2 +| 3104: 74 68 02 08 02 01 01 07 00 04 33 61 6e 64 02 06 th........3and.. +| 3120: 04 0a 1b 88 80 80 80 80 0c 03 00 3c 00 00 00 16 ...........<.... +| 3136: 03 32 69 6e 02 06 01 01 06 01 02 72 6f 02 06 01 .2in.......ro... +| 3152: 01 05 04 09 18 88 80 80 80 80 0b 03 00 36 00 00 .............6.. +| 3168: 00 13 02 03 32 61 72 02 02 03 01 02 62 65 02 02 ....2ar.....be.. +| 3184: 04 05 07 1b 88 80 80 80 80 0a 03 00 3c 00 00 00 ............<... +| 3200: 16 02 31 74 02 08 02 01 01 07 00 03 32 61 6e 02 ..1t........2an. +| 3216: 06 01 01 04 09 19 88 80 80 80 80 09 03 00 38 00 ..............8. +| 3232: 00 00 14 02 31 6e 02 06 01 01 03 01 01 72 02 06 ....1n.......r.. +| 3248: 01 01 05 04 08 17 88 80 80 80 80 08 03 00 34 00 ..............4. +| 3264: 01 00 12 02 31 62 02 02 04 01 01 69 02 06 01 01 ....1b.....i.... +| 3280: 06 04 06 19 88 80 80 80 80 07 03 00 38 00 00 00 ............8... +| 3296: 14 04 02 31 32 02 02 05 01 01 61 02 08 03 01 01 ...12.....a..... +| 3312: 02 05 06 1b 88 80 80 80 80 06 03 00 3c 00 00 00 ............<... +| 3328: 16 06 30 74 68 65 72 65 e7 02 02 00 02 31 31 02 ..0there.....11. +| 3344: 06 01 01 04 0a 15 88 80 80 80 80 05 03 00 30 00 ..............0. +| 3360: 00 00 11 01 01 05 04 30 74 68 65 02 06 01 01 07 .......0the..... +| 3376: 07 1c 88 80 80 80 80 04 03 00 3e 00 00 00 17 01 ..........>..... +| 3392: 01 06 02 30 6e 02 06 01 01 03 01 04 72 6f 77 73 ...0n.......rows +| 3408: 02 06 07 08 1b 88 80 80 80 80 03 03 00 3c 00 00 .............<.. +| 3424: 00 16 08 30 62 65 74 77 65 65 6e 02 02 04 01 02 ...0between..... +| 3440: 69 6e 02 06 04 0c 1a 88 80 80 80 80 02 03 00 3a in.............: +| 3456: 00 00 00 15 04 30 61 6e 64 02 06 01 01 02 02 02 .....0and....... +| 3472: 72 65 02 02 03 04 0a 17 88 80 80 80 80 01 03 00 re.............. +| 3488: 34 01 00 00 12 02 30 31 02 06 01 01 04 01 01 32 4.....01.......2 +| 3504: 02 02 05 04 08 08 84 80 80 80 80 12 03 00 16 00 ................ +| 3520: 00 00 05 04 1b 84 80 80 80 80 11 03 00 3c 00 00 .............<.. +| 3536: 00 16 05 34 74 61 62 6c 01 06 01 01 05 02 03 65 ...4tabl.......e +| 3552: 72 6d 01 02 04 0b 1b 84 80 80 80 80 10 03 00 3c rm.............< +| 3568: 00 00 00 16 05 34 65 61 63 68 01 02 02 01 04 70 .....4each.....p +| 3584: 72 65 73 01 02 05 04 09 1a 84 80 80 80 80 0f 03 res............. +| 3600: 00 3a 00 00 00 15 04 33 74 65 72 01 02 04 02 02 .:.....3ter..... +| 3616: 68 65 01 06 01 01 03 04 08 1b 84 80 80 80 80 0e he.............. +| 3632: 03 00 3c 00 00 00 16 04 33 70 72 65 01 02 05 01 ..<.....3pre.... +| 3648: 03 74 61 62 01 06 01 01 05 04 08 1a 84 80 80 80 .tab............ +| 3664: 80 0d 03 00 3a 00 00 00 15 04 33 66 6f 72 01 02 ....:.....3for.. +| 3680: 02 02 02 74 73 01 06 01 01 04 04 08 1b 84 80 80 ...ts........... +| 3696: 80 80 0c 03 00 3c 00 00 00 16 03 32 74 68 01 06 .....<.....2th.. +| 3712: 01 01 03 00 04 33 65 61 63 01 02 03 04 09 18 84 .....3eac....... +| 3728: 80 80 80 80 0b 03 00 36 00 00 00 13 03 32 74 61 .......6.....2ta +| 3744: 01 06 01 01 05 02 01 65 01 02 04 04 09 19 84 80 .......e........ +| 3760: 80 80 80 0a 03 00 38 00 00 00 14 03 32 69 6e 01 ......8.....2in. +| 3776: 06 01 01 02 01 02 70 72 01 02 05 04 09 18 84 80 ......pr........ +| 3792: 80 80 80 09 03 00 36 00 00 00 13 03 32 66 6f 01 ......6.....2fo. +| 3808: 02 02 02 01 74 01 06 01 01 04 04 07 1b 84 80 80 ....t........... +| 3824: 80 80 08 03 00 3c 00 00 00 16 02 31 74 01 0a 04 .....<.....1t... +| 3840: 01 01 03 04 00 03 32 65 61 01 02 03 04 0a 17 84 ......2ea....... +| 3856: 80 80 80 80 07 03 00 34 00 00 00 12 02 31 69 01 .......4.....1i. +| 3872: 06 01 01 02 01 01 70 01 02 05 04 08 18 84 80 80 ......p......... +| 3888: 80 80 06 03 00 36 00 00 00 13 02 31 65 01 02 03 .....6.....1e... +| 3904: 01 01 66 01 08 02 01 01 04 04 06 1b 84 80 80 80 ..f............. +| 3920: 80 05 03 00 3c 00 00 00 16 05 30 74 65 72 6d 01 ....<.....0term. +| 3936: 02 04 02 02 68 65 01 06 01 01 03 04 09 14 84 80 ....he.......... +| 3952: 80 80 80 04 03 00 2e 00 00 00 10 06 30 74 61 62 ............0tab +| 3968: 6c 65 01 06 01 01 05 04 15 84 7f 80 80 80 03 03 le.............. +| 3984: 00 30 00 00 00 11 02 08 30 70 72 65 73 65 6e 74 .0......0present +| 4000: 01 02 05 05 1b 84 80 80 80 80 02 03 00 3c 00 00 .............<.. +| 4016: 00 16 04 30 66 74 73 01 06 01 01 04 01 02 69 6e ...0fts.......in +| 4032: 01 06 01 01 04 0a 1a 84 80 80 80 80 01 03 00 3a ...............: +| 4048: 00 00 00 15 05 30 65 61 63 68 01 02 03 01 03 66 .....0each.....f +| 4064: 6f 72 01 02 02 04 09 06 01 03 00 12 03 0b 0f 00 or.............. +| 4080: 00 08 8c 80 80 80 80 11 03 00 16 00 00 00 05 04 ................ +| page 3 offset 8192 +| 0: 0a 00 00 00 32 0e 4f 00 0f fa 0f f1 0f e9 0f e1 ....2.O......... +| 16: 0f d8 0f d1 0f c9 0f c1 0f b9 0f b1 0f a9 0f a0 ................ +| 32: 0f 98 0f 90 0f 87 0f 80 0f 78 0f 71 0f 68 0f 5f .........x.q.h._ +| 48: 0f 56 0f 4d 0f 41 0f 38 0f 2f 0f 26 0f 1d 0f 03 .V.M.A.8./.&.... +| 64: 0f 0a 0f 00 9e f7 0e ee 0e e6 0e dd 0e d6 0e cd ................ +| 80: 0e c3 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 3648: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 08 ................ +| 3664: 04 01 10 01 03 34 74 20 07 04 01 0e 01 03 34 1e .....4t ......4. +| 3680: 09 04 01 12 01 03 33 74 68 1c 08 04 01 10 01 03 ......3th....... +| 3696: 33 6e 1a 08 04 01 10 01 03 32 77 18 08 04 01 10 3n.......2w..... +| 3712: 01 03 32 74 16 08 04 01 10 01 03 32 6e 14 07 04 ..2t.......2n... +| 3728: 01 0e 01 03 32 12 08 04 01 10 01 03 31 74 10 08 ....2.......1t.. +| 3744: 04 01 10 01 03 31 6e 0e 07 04 01 0e 01 03 31 0c .....1n.......1. +| 3760: 09 04 01 12 01 03 30 74 68 0a 08 04 01 10 01 03 ......0th....... +| 3776: 30 74 08 09 04 01 12 01 03 30 6e 75 06 08 04 01 0t.......0nu.... +| 3792: 10 01 03 30 6e 04 06 04 01 0c 01 03 02 08 04 01 ...0n........... +| 3808: 10 01 02 34 72 22 07 04 01 0e 01 02 34 20 08 04 ...4r.......4 .. +| 3824: 01 10 01 02 33 72 1e 09 04 01 12 01 02 33 61 72 ....3r.......3ar +| 3840: 1c 18 04 01 10 01 02 32 74 1a 08 04 01 10 01 02 .......2t....... +| 3856: 32 69 18 09 04 01 12 01 02 32 61 72 16 08 04 01 2i.......2ar.... +| 3872: 10 01 02 31 74 14 08 04 01 0f 01 02 31 6e 12 0a ...1t.......1n.. +| 3888: d4 01 10 01 02 31 62 10 08 04 01 10 01 02 31 32 .....1b.......12 +| 3904: 0e 0b 04 01 16 01 02 30 74 68 65 72 0c 08 04 01 .......0ther.... +| 3920: 10 01 02 30 74 0a 08 04 01 10 01 02 30 6e 08 08 ...0t.......0n.. +| 3936: 04 01 10 01 02 30 62 06 08 04 01 10 01 02 30 61 .....0b.......0a +| 3952: 04 06 04 01 0c 01 02 02 07 04 09 10 01 34 74 22 .............4t. +| 3968: 06 04 09 0e 01 34 20 08 04 09 12 01 33 74 65 1e .....4 .....3te. +| 3984: 07 04 09 10 01 33 70 1c 07 04 09 10 01 33 66 1a .....3p......3f. +| 4000: 08 04 09 12 01 32 74 68 18 07 04 09 10 01 32 74 .....2th......2t +| 4016: 16 07 04 09 10 01 32 69 14 07 04 09 10 01 32 66 ......2i......2f +| 4032: 12 07 04 09 10 01 31 74 10 07 04 09 10 01 31 69 ......1t......1i +| 4048: 0e 06 04 09 0e 01 31 0c 08 04 09 12 01 30 74 65 ......1......0te +| 4064: 0a 07 04 09 10 01 30 74 08 07 04 09 10 01 30 70 ......0t......0p +| 4080: 06 08 04 09 1e 61 30 66 74 04 05 04 09 0c 01 02 .....a0ft....... +| page 4 offset 12288 +| 0: 0d 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 05 03 03 00 10 ................ +| 4080: 03 05 05 02 03 00 10 04 06 05 01 03 00 10 04 04 ................ +| page 5 offset 16384 +| 0: 0a 00 00 00 02 0f eb 00 0f eb 0f f4 00 00 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 08 03 15 01 70 ...............p +| 4080: 67 73 7a 18 0b 03 1b 01 76 65 72 73 69 6f 6e 04 gsz.....version. +| page 6 offset 20480 +| 0: 0d 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 4080: 00 00 03 03 02 01 da 03 02 02 01 02 00 00 00 00 ................ +| end crash-53216796d7c3d8.db +}]} {} + +do_catchsql_test 77.1 { + UPDATE t1 SET B =quote(zeroblob(200)) WHERE b MATCH 'threa*thrad*tlrad*the�d'; +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 78.0 { + CREATE VIRTUAL TABLE t1 USING fts5(a,b,c); + DELETE FROM t1_data; + INSERT INTO t1_data VALUES(1,X'245a2424'); + INSERT INTO t1_data VALUES(10,X'000000000101010001010101'); + INSERT INTO t1_data VALUES(137438953473,X'0000032b0230300102060102060102061f0203010203010203010832303136303630390102070102070102070101340102050102050102050101350102040102040102040207303030303030301c0204010204010204010662696e6172790306010202030601020203060102020306010202030601020203060102020306010202030601020203060102020306010202030601020203060102020108636f6d70696c657201200102020201020201066462737ccccccccccccccccccccccccccccccccccccccccccccbccccccccccccccccccccccccccccccccccccccccccccccca2cccccccccc461740702030102030102030204656275670402020102020102020106656e61626c6507020201020201020201020201020201020201020201020201020201020201020201020201020201020201020201020201020201020523d6763632d352e342e30203230313630363039584e4f4341534526010500430f17434f4d50494c45523d6763632d352e342e3020323031363036303958525452494d0d000000240ee00004a810000fe80fe00fd80fd00fc80fc00fb80fb00fa80fa00f980f900f880f800f780f700f680f60945736502060102020306010202030601020203060102020306010202030601020203060102020306010202030601020203060102020306010202030601020201046f6d59741f0202010202010202010572747265651945030102030102030402696d010601020203060102020306010202030601120203060102020306010202030601020203060102020306010202030601020203060102020306010202010a7468726561647361666522020201020201020201047674616207020401020401020401407801060cb102010601010201060101020106010102010601010201060101020106010102010601010201060e0102010601010201060101020106010102010601010201060101020106010102010601010201060101020106010102010601010201060101021106010102010601010201060101020106010102010601010201060101020106010102010601010201060101020106010102010601011201060101020106010102010601010201060101020106010102041513020c124413110f47130f0c0e11100f0e100f440f1040150f'); +} + +do_execsql_test 78.1 { + CREATE VIRTUAL TABLE t3 USING fts5vocab('t1','col'); +} + +do_execsql_test 78.2 { + SELECT count(rowid) FROM t3 WHERE term>='nsocse'; +} 2 + +#------------------------------------------------------------------------- +reset_db +do_test 79.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 28672 pagesize 4096 filename sql053282.txt.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 07 .....@ ........ +| 32: 00 00 00 02 00 00 00 01 00 00 00 00 00 00 00 00 ................ +| 96: 00 00 00 00 0d 0f c7 00 06 0d b6 00 0f 8d 0f 36 ...............6 +| 112: 0e cb 0e 6b 0e 0e 0d b6 0d b5 00 00 00 00 00 00 ...k............ +| 3504: 00 00 00 00 00 00 56 07 06 17 1f 1f 01 7d 74 61 ......V.......ta +| 3520: 62 6c 65 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 blet1_configt1_c +| 3536: 6f 6e 66 69 67 07 43 52 45 41 54 45 20 54 41 42 onfig.CREATE TAB +| 3552: 4c 45 20 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b LE 't1_config'(k +| 3568: 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 PRIMARY KEY, v) +| 3584: 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 5b 06 WITHOUT ROWID[. +| 3600: 07 17 21 21 01 81 01 74 61 62 6c 65 74 31 5f 64 ..!!...tablet1_d +| 3616: 6f 63 73 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 ocsizet1_docsize +| 3632: 06 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 .CREATE TABLE 't +| 3648: 31 5f 64 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 1_docsize'(id IN +| 3664: 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE +| 3680: 59 2c 20 73 7a 20 42 4c 4f 42 29 5e 05 07 17 21 Y, sz BLOB)^...! +| 3696: 21 01 81 07 74 61 62 6c 65 74 31 5f 63 6f 6e 74 !...tablet1_cont +| 3712: 65 6e 74 74 31 5f 63 6f 6e 74 65 6e 74 05 43 52 entt1_content.CR +| 3728: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 EATE TABLE 't1_c +| 3744: 6f 6e 74 65 6e 74 27 28 69 64 20 49 4e 54 45 47 ontent'(id INTEG +| 3760: 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 ER PRIMARY KEY, +| 3776: 63 30 2c 20 63 31 2c 20 63 32 29 69 04 07 17 19 c0, c1, c2)i.... +| 3792: 19 01 81 2d 74 61 62 6c 65 74 31 5f 69 64 78 74 ...-tablet1_idxt +| 3808: 31 5f 69 64 78 04 43 52 45 41 54 45 20 54 41 42 1_idx.CREATE TAB +| 3824: 4c 45 20 27 74 31 5f 69 64 78 27 28 73 65 67 69 LE 't1_idx'(segi +| 3840: 64 2c 20 74 65 72 6d 2c 20 70 67 6e 6f 2c 20 50 d, term, pgno, P +| 3856: 52 49 4d 41 52 59 20 4b 45 59 28 73 65 67 69 64 RIMARY KEY(segid +| 3872: 2c 20 74 65 72 6d 29 29 20 57 49 54 48 4f 55 54 , term)) WITHOUT +| 3888: 20 52 4f 57 49 44 55 03 07 17 1b 1b 01 81 01 74 ROWIDU........t +| 3904: 61 62 6c 65 74 31 5f 64 61 74 61 74 31 5f 64 61 ablet1_datat1_da +| 3920: 74 61 03 43 52 45 41 54 45 20 54 41 42 4c 45 20 ta.CREATE TABLE +| 3936: 27 74 31 5f 64 61 74 61 27 28 69 64 20 49 4e 54 't1_data'(id INT +| 3952: 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 EGER PRIMARY KEY +| 3968: 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 29 38 02 06 , block BLOB)8.. +| 3984: 17 11 11 08 5f 74 61 62 6c 65 74 31 74 31 43 52 ...._tablet1t1CR +| 4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 35 LE t1 USING fts5 +| 4032: 28 61 2c 62 2c 63 29 00 00 00 00 00 00 00 00 00 (a,b,c)......... +| page 3 offset 8192 +| 0: 0d 00 00 00 03 0c 94 00 0f e6 0f ef 0c 94 00 01 ................ +| 3216: 00 00 00 00 86 4a 84 80 80 80 80 01 04 00 8d 18 .....J.......... +| 3232: 00 00 03 2b 02 30 30 01 02 06 01 02 06 01 02 06 ...+.00......... +| 3248: 1f 02 03 01 02 03 01 02 03 01 08 32 30 31 36 30 ...........20160 +| 3264: 36 30 39 01 02 07 01 02 07 01 02 07 01 01 34 01 609...........4. +| 3280: 02 05 01 02 05 01 02 05 01 01 35 01 02 04 01 02 ..........5..... +| 3296: 04 01 02 04 02 07 30 30 30 30 30 30 30 1c 02 04 ......0000000... +| 3312: 01 02 04 01 02 04 01 06 62 69 6e 61 72 79 03 06 ........binary.. +| 3328: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3344: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3360: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 ................ +| 3376: 03 06 01 02 02 03 06 01 02 02 01 08 63 6f 6d 70 ............comp +| 3392: 69 6c 65 72 01 02 02 01 02 02 01 02 02 01 06 64 iler...........d +| 3408: 62 73 74 61 74 07 02 03 01 02 03 01 02 03 02 04 bstat........... +| 3424: 65 62 75 67 04 02 02 01 02 02 01 02 02 01 06 65 ebug...........e +| 3440: 6e 61 62 6c 65 07 02 02 01 02 02 01 02 02 01 02 nable........... +| 3456: 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 ................ +| 3472: 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 ................ +| 3488: 02 02 01 02 02 01 02 02 01 02 12 01 02 02 01 02 ................ +| 3504: 02 01 02 02 02 08 78 74 65 6e 73 69 6f 6e 1f 02 ......xtension.. +| 3520: 04 01 02 04 01 02 04 01 04 66 74 73 34 0a 02 03 .........fts4... +| 3536: 01 02 03 01 02 03 04 01 35 0d 02 03 01 02 03 01 ........5....... +| 3552: 02 03 01 03 67 63 63 01 02 03 01 02 03 01 02 03 ....gcc......... +| 3568: 02 06 65 6f 70 6f 6c 79 10 02 03 01 02 03 01 02 ..eopoly........ +| 3584: 03 01 05 6a 73 6f 6e 31 13 02 03 01 02 03 01 02 ...json1........ +| 3600: 03 01 04 6c 6f 61 64 1f 02 03 01 02 03 01 02 03 ...load......... +| 3616: 01 03 6d 61 78 1c 02 02 01 02 02 01 02 02 02 05 ..max........... +| 3632: 65 6d 6f 72 79 1c 02 03 01 02 03 01 02 03 04 04 emory........... +| 3648: 73 79 73 35 16 02 03 01 02 03 01 02 03 01 06 6e sys5...........n +| 3664: 6f 63 61 73 65 02 06 01 02 02 03 06 01 02 02 03 ocase........... +| 3680: 06 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 ................ +| 3696: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3712: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3728: 02 01 04 6f 6d 69 74 1f 02 02 01 02 02 01 02 02 ...omit......... +| 3744: 01 05 72 74 72 65 65 19 02 03 01 02 03 01 02 03 ..rtree......... +| 3760: 04 02 69 6d 01 06 01 02 02 03 06 01 02 02 03 06 ..im............ +| 3776: 01 02 02 03 06 01 01 02 03 06 01 02 02 03 06 01 ................ +| 3792: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3808: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 ................ +| 3824: 01 0a 74 68 72 65 61 64 73 61 66 65 22 02 02 01 ..threadsafe.... +| 3840: 02 02 01 02 02 01 04 76 74 61 62 07 02 04 01 02 .......vtab..... +| 3856: 04 01 02 04 01 01 78 01 05 f1 01 02 01 06 01 01 ......x......... +| 3872: 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 ................ +| 3888: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 3904: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 3920: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 ................ +| 3936: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 ................ +| 3952: 01 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 ................ +| 3968: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 3984: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 4000: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 ................ +| 4016: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 ................ +| 4032: 02 01 06 01 01 02 01 06 01 01 02 04 15 13 0c 0c ................ +| 4048: 12 44 13 11 0f 47 13 0f 0c 0e 11 10 0f 0e 10 0f .D...G.......... +| 4064: 44 0f 10 40 15 0f 07 01 03 00 14 24 5a 24 24 0f D..@.......$Z$$. +| 4080: 0a 03 00 24 00 00 00 00 01 01 01 00 01 01 01 01 ...$............ +| page 4 offset 12288 +| 0: 0a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 05 04 09 0c 01 02 ................ +| page 5 offset 16384 +| 0: 0d 00 00 00 24 0c 0a 00 0f d8 0f af 0f 86 0f 74 ....$..........t +| 16: 0f 61 0f 4e 0f 2f 0f 0f 0e ef 0e d7 0e be 0e a5 .a.N./.......... +| 32: 0e 8d 0e 74 0e 5b 0e 40 0e 24 0e 08 0d ef 0d d5 ...t.[.@.$...... +| 48: 0d bb 0d a0 0d 84 0d 68 0d 4f 0d 00 00 00 00 00 .......h.O...... +| 3072: 00 00 00 00 00 00 00 00 00 00 18 24 05 00 25 0f ...........$..%. +| 3088: 19 54 48 52 45 41 44 53 41 46 45 3d 30 58 42 49 .THREADSAFE=0XBI +| 3104: 4e 41 52 59 18 23 05 00 25 0f 19 54 48 52 45 41 NARY.#..%..THREA +| 3120: 44 53 41 46 45 3d 30 58 4e 4f 43 41 53 45 17 22 DSAFE=0XNOCASE.. +| 3136: 05 00 25 0f 17 54 48 52 45 41 44 53 41 46 45 3d ..%..THREADSAFE= +| 3152: 30 58 52 54 52 49 4d 1f 21 05 00 33 0f 19 4f 4d 0XRTRIM.!..3..OM +| 3168: 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 4f IT LOAD EXTENSIO +| 3184: 4e 58 42 49 4e 41 52 59 1f 20 05 00 33 0f 19 4f NXBINARY. ..3..O +| 3200: 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 MIT LOAD EXTENSI +| 3216: 4f 4e 58 4e 4f 43 41 53 45 1e 1f 05 00 33 0f 17 ONXNOCASE....3.. +| 3232: 4f 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 OMIT LOAD EXTENS +| 3248: 49 4f 4e 58 52 54 52 49 4d 1f 1e 05 00 33 0f 19 IONXRTRIM....3.. +| 3264: 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 30 MAX MEMORY=50000 +| 3280: 30 30 30 58 42 49 4e 41 52 59 1f 1d 05 00 33 0f 000XBINARY....3. +| 3296: 19 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 .MAX MEMORY=5000 +| 3312: 30 30 30 30 58 4e 4f 43 41 53 45 1e 1c 05 00 33 0000XNOCASE....3 +| 3328: 0f 17 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 ..MAX MEMORY=500 +| 3344: 30 30 30 30 30 58 52 54 52 49 4d 18 1b 05 00 2c 00000XRTRIM...., +| 3360: 0f 19 45 4e 41 42 4c 45 20 52 54 52 45 45 58 42 ..ENABLE RTREEXB +| 3376: 49 4e 41 52 59 18 1a 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3392: 4c 45 20 52 54 52 45 45 58 4e 4f 43 41 53 45 17 LE RTREEXNOCASE. +| 3408: 19 05 00 25 0f 17 45 4e 41 42 4c 45 20 52 54 52 ...%..ENABLE RTR +| 3424: 45 45 58 52 54 52 49 4d 1a 18 05 00 29 0f 19 45 EEXRTRIM....)..E +| 3440: 4e 41 42 4c 45 20 4d 45 4d 5a 69 53 35 58 42 49 NABLE MEMZiS5XBI +| 3456: 4e 41 52 59 1a 17 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3472: 45 20 4d 45 4d 53 59 53 35 58 4e 4f 43 41 53 45 E MEMSYS5XNOCASE +| 3488: 19 16 05 00 29 0f 17 45 4e 41 42 4c 45 20 4d 45 ....)..ENABLE ME +| 3504: 4d 53 59 53 35 58 52 54 52 49 4d 18 15 05 00 25 MSYS5XRTRIM....% +| 3520: 0f 19 45 4e 41 42 4c 45 20 4a 53 4f 4e 31 58 42 ..ENABLE JSON1XB +| 3536: 49 4e 41 52 59 18 14 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3552: 4c 45 20 4a 53 4f 4e 31 58 4e 4f 43 41 53 45 17 LE JSON1XNOCASE. +| 3568: 13 05 00 25 0f 17 45 4e 41 42 4c 45 20 4a 53 4f ...%..ENABLE JSO +| 3584: 4e 31 58 52 54 52 49 4d 1a 12 05 00 29 0f 19 45 N1XRTRIM....)..E +| 3600: 4e 41 42 4c 45 20 47 45 4f 50 4f 4c 59 58 42 49 NABLE GEOPOLYXBI +| 3616: 4e 41 52 59 1a 11 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3632: 45 20 47 45 4f 50 4f 4c 59 58 4e 4f 43 41 53 45 E GEOPOLYXNOCASE +| 3648: 19 10 05 00 29 0f 17 45 4e 41 42 4c 45 20 47 45 ....)..ENABLE GE +| 3664: 4f 50 4f 4c 59 58 52 54 52 49 4d 17 0f 05 00 23 OPOLYXRTRIM....# +| 3680: 0f 19 45 4e 41 42 4c 45 20 46 54 53 35 58 42 49 ..ENABLE FTS5XBI +| 3696: 4e 41 52 59 17 0e 05 00 23 0f 19 45 4e 41 42 4c NARY....#..ENABL +| 3712: 45 20 46 54 53 35 58 4e 4f 43 41 53 45 16 0d 05 E FTS5XNOCASE... +| 3728: 00 23 0f 17 45 4e 41 42 4c 45 20 46 54 53 3f d8 .#..ENABLE FTS?. +| 3744: 52 54 52 49 4d 17 0c 05 00 23 0f 19 45 4e 41 42 RTRIM....#..ENAB +| 3760: 4c 45 20 46 54 53 34 58 42 49 4e 41 52 59 17 0b LE FTS4XBINARY.. +| 3776: 05 00 23 0f 19 45 4e 41 42 4c 45 20 46 54 53 34 ..#..ENABLE FTS4 +| 3792: 58 4e 4f 43 41 53 45 16 0a 05 00 23 0f 17 45 4e XNOCASE....#..EN +| 3808: 41 42 4c 45 20 46 54 53 34 58 52 54 52 49 4d 1e ABLE FTS4XRTRIM. +| 3824: 09 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3840: 55 41 54 20 56 54 41 42 58 42 49 4e 41 52 59 1e UAT VTABXBINARY. +| 3856: 08 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3872: 54 41 54 20 56 54 41 42 58 4e 4f 43 41 53 45 1d TAT VTABXNOCASE. +| 3888: 07 05 00 31 0f 17 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3904: 54 41 54 20 56 54 41 42 58 52 54 52 49 4d 11 06 TAT VTABXRTRIM.. +| 3920: 05 00 17 0f 19 44 45 42 55 47 58 42 49 4e 41 52 .....DEBUGXBINAR +| 3936: 59 11 05 05 00 17 0f 19 44 45 42 55 47 58 4e 4f Y.......DEBUGXNO +| 3952: 43 41 53 45 10 04 05 00 17 0f 17 44 45 42 55 46 CASE.......DEBUF +| 3968: e8 52 54 52 49 4d 27 03 05 00 43 0f 19 43 4f 4d .RTRIM'...C..COM +| 3984: 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e 30 20 PILER=gcc-5.4.0 +| 4000: 32 30 31 36 30 36 30 39 58 42 49 4e 41 52 59 27 20160609XBINARY' +| 4016: 02 05 00 43 0f 19 43 4f 4d 50 49 4c 45 52 3d 67 ...C..COMPILER=g +| 4032: 63 63 2d 35 2e 34 2e 30 20 32 30 31 36 30 36 30 cc-5.4.0 2016060 +| 4048: 39 58 4e 4f 43 41 53 45 26 01 05 00 43 0f 17 43 9XNOCASE&...C..C +| 4064: 4f 4d 50 49 4c 45 52 3d 67 62 63 2d 35 2e 34 2e OMPILER=gbc-5.4. +| 4080: 30 20 32 30 31 36 30 36 30 39 58 52 54 52 49 4d 0 20160609XRTRIM +| page 6 offset 20480 +| 0: 0d 00 00 00 24 0e e0 00 0f f8 0f f0 0f e8 0f e0 ....$........... +| 16: 0f d8 0f d0 0f c8 0f c0 0f b8 0f bb 1f a8 0f a0 ................ +| 32: 0f 98 0f 90 0f 88 0f 80 0f 78 0f 70 0f 68 0f 60 .........x.p.h.` +| 48: 0f 58 0f 50 0f 48 0f 40 0f 38 0f 30 00 00 00 00 .X.P.H.@.8.0.... +| 3808: 06 24 03 00 12 02 01 01 06 23 03 00 12 02 01 01 .$.......#...... +| 3824: 06 22 03 00 12 02 01 01 06 21 03 00 12 03 01 01 .........!...... +| 3840: 06 20 03 00 12 03 01 01 06 1f 03 00 12 03 01 01 . .............. +| 3856: 06 1e 03 00 12 03 01 01 06 1d 03 00 12 03 01 01 ................ +| 3872: 06 1c 03 00 12 03 01 01 06 1b 03 00 12 02 01 01 ................ +| 3888: 06 1a 03 00 12 02 01 01 06 19 03 00 12 02 01 01 ................ +| 3904: 06 18 03 00 12 02 01 01 06 17 03 00 12 02 01 01 ................ +| 3920: 06 16 03 00 12 02 01 01 06 15 03 00 12 02 01 01 ................ +| 3936: 06 14 03 00 12 02 01 01 06 13 03 00 12 02 01 01 ................ +| 3952: 06 12 03 00 12 02 01 01 06 11 03 00 12 02 01 01 ................ +| 3968: 06 10 03 00 12 02 01 01 06 0f 03 00 12 02 01 01 ................ +| 3984: 06 0e 03 00 12 02 01 01 06 0d 03 00 12 02 01 01 ................ +| 4000: 06 0c 03 00 12 02 01 01 06 0b 03 00 12 02 01 01 ................ +| 4016: 06 0a 03 00 12 02 01 01 06 09 03 00 12 03 01 01 ................ +| 4032: 06 08 03 00 12 03 01 01 06 07 03 00 12 03 01 01 ................ +| 4048: 06 06 03 00 12 01 01 01 06 05 03 00 12 01 01 01 ................ +| 4064: 06 04 03 00 12 01 01 01 06 03 03 00 12 06 01 01 ................ +| 4080: 06 0a 83 00 12 06 01 01 06 01 03 00 12 06 01 01 ................ +| page 7 offset 24576 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| end sql053282.txt.db +}]} {} + +do_execsql_test 79.1 { + CREATE VIRTUAL TABLE t2 USING fts5vocab('t1','row'); +} + +do_catchsql_test 79.2 { + INSERT INTO t1(t1) SELECT 'merge' FROM t2; +} {1 {query aborted}} + +#------------------------------------------------------------------------- +reset_db +do_test 80.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 40960 pagesize 4096 filename crash-f928a9c1ec68dd.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 0a .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 0d 00 00 00 04 ................ +| 96: 00 00 00 00 0d 00 00 00 0d 0b 6e 00 0f a3 0f 4c ..........n....L +| 112: 0e e1 0e 81 0e 24 0d cc 0d 72 0d 1b 0c b0 0c 50 .....$...r.....P +| 128: 0b f8 0b b3 0b 6e 00 00 00 00 00 00 00 00 00 00 .....n.......... +| 2912: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 43 0d ..............C. +| 2928: 06 17 11 11 08 75 74 61 62 6c 65 74 34 74 34 43 .....utablet4t4C +| 2944: 52 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 REATE VIRTUAL TA +| 2960: 42 4c 45 20 74 34 20 55 53 49 4e 47 20 66 74 73 BLE t4 USING fts +| 2976: 35 76 6f 63 61 62 28 27 74 32 27 2c 20 27 72 6f 5vocab('t2', 'ro +| 2992: 77 27 29 43 0c 06 17 11 11 08 75 74 61 62 6c 65 w')C......utable +| 3008: 74 33 74 33 43 52 45 41 54 45 20 56 49 52 54 55 t3t3CREATE VIRTU +| 3024: 41 4c 20 54 41 42 4c 45 20 74 33 20 55 53 49 4e AL TABLE t3 USIN +| 3040: 47 20 66 74 73 35 76 6f 63 61 62 28 27 74 31 27 G fts5vocab('t1' +| 3056: 2c 20 27 72 6f 77 27 29 56 0b 06 17 1f 1f 01 7d , 'row')V....... +| 3072: 74 61 62 6c 65 74 32 5f 63 6f 6e 66 69 67 74 32 tablet2_configt2 +| 3088: 5f 63 6f 6e 66 69 67 0a 43 52 45 41 54 45 20 54 _config.CREATE T +| 3104: 41 42 4c 45 20 27 74 32 5f 63 6f 6e 66 69 67 27 ABLE 't2_config' +| 3120: 28 6b 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 (k PRIMARY KEY, +| 3136: 76 29 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 v) WITHOUT ROWID +| 3152: 5e 0a 07 17 21 21 01 81 07 74 61 62 6c 65 74 32 ^...!!...tablet2 +| 3168: 5f 63 6f 6e 74 65 6e 74 74 32 5f 63 6f 6e 74 65 _contentt2_conte +| 3184: 6e 74 09 43 52 45 41 54 45 20 54 41 42 4c 45 20 nt.CREATE TABLE +| 3200: 27 74 32 5f 63 6f 6e 74 65 6e 74 27 28 69 64 20 't2_content'(id +| 3216: 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 INTEGER PRIMARY +| 3232: 4b 45 59 2c 20 63 30 2c 20 63 31 2c 20 63 32 29 KEY, c0, c1, c2) +| 3248: 69 09 07 17 19 19 01 81 2d 74 61 62 6c 65 74 32 i.......-tablet2 +| 3264: 5f 69 64 78 74 32 5f 69 64 78 08 43 52 45 41 54 _idxt2_idx.CREAT +| 3280: 45 20 54 41 42 4c 45 20 27 74 32 5f 69 64 78 27 E TABLE 't2_idx' +| 3296: 28 73 65 67 69 64 2c 20 74 65 72 6d 2c 20 70 67 (segid, term, pg +| 3312: 6e 6f 2c 20 50 52 49 4d 41 52 59 20 4b 45 59 28 no, PRIMARY KEY( +| 3328: 73 65 67 69 64 2c 20 74 65 72 6d 29 29 20 57 49 segid, term)) WI +| 3344: 54 48 4f 55 54 20 52 4f 57 49 44 55 08 07 17 1b THOUT ROWIDU.... +| 3360: 1b 01 81 01 74 61 62 6c 65 74 32 5f 64 61 74 61 ....tablet2_data +| 3376: 74 32 5f 64 61 74 61 07 43 52 45 41 54 45 20 54 t2_data.CREATE T +| 3392: 41 42 4c 45 20 27 74 32 5f 64 61 74 61 27 28 69 ABLE 't2_data'(i +| 3408: 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 d INTEGER PRIMAR +| 3424: 59 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 4c 4f Y KEY, block BLO +| 3440: 42 29 58 07 07 17 11 11 08 81 1d 74 61 62 6c 65 B)X........table +| 3456: 74 32 74 32 43 52 45 41 54 45 20 56 49 52 54 55 t2t2CREATE VIRTU +| 3472: 41 4c 20 54 41 42 4c 45 20 74 32 20 55 53 49 4e AL TABLE t2 USIN +| 3488: 47 20 66 74 73 35 28 27 61 27 2c 5b 62 5d 2c 22 G fts5('a',[b],. +| 3504: 63 22 2c 64 65 74 61 69 6c 3d 6e 6f 6e 65 2c 63 c.,detail=none,c +| 3520: 6f 6c 75 6d 6e 73 69 7a 65 3d 30 29 56 06 06 17 olumnsize=0)V... +| 3536: 1f 1f 01 7d 74 61 62 6c 65 74 31 5f 63 6f 6e 66 ....tablet1_conf +| 3552: 69 67 74 31 5f 63 6f 6e 66 69 67 06 43 52 45 41 igt1_config.CREA +| 3568: 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 6f 6e TE TABLE 't1_con +| 3584: 66 69 67 27 28 6b 20 50 52 49 4d 41 52 59 20 4b fig'(k PRIMARY K +| 3600: 45 59 2c 20 76 29 20 57 49 54 48 4f 55 54 20 52 EY, v) WITHOUT R +| 3616: 4f 57 49 44 5b 05 07 17 21 21 01 81 01 74 61 62 OWID[...!!...tab +| 3632: 6c 65 74 31 5f 64 6f 63 73 69 7a 65 74 31 5f 64 let1_docsizet1_d +| 3648: 6f 63 73 69 7a 65 05 43 52 45 41 54 45 20 54 41 ocsize.CREATE TA +| 3664: 42 4c 45 20 27 74 31 5f 64 6f 63 73 69 7a 65 27 BLE 't1_docsize' +| 3680: 28 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d (id INTEGER PRIM +| 3696: 41 52 59 20 4b 45 59 2c 20 73 7a 20 42 4c 4f 42 ARY KEY, sz BLOB +| 3712: 29 5e 04 07 17 21 21 01 81 07 74 61 62 6c 65 74 )^...!!...tablet +| 3728: 31 5f 63 6f 6e 74 65 6e 74 74 31 5f 63 6f 6e 74 1_contentt1_cont +| 3744: 65 6e 74 04 43 52 45 41 54 45 20 54 41 42 4c 45 ent.CREATE TABLE +| 3760: 20 27 74 31 5f 63 6f 6e 74 65 6e 74 27 28 69 64 't1_content'(id +| 3776: 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 INTEGER PRIMARY +| 3792: 20 4b 45 59 2c 20 63 30 2c 20 63 31 2c 20 63 32 KEY, c0, c1, c2 +| 3808: 29 69 03 07 17 19 19 01 81 2d 74 61 62 6c 65 74 )i.......-tablet +| 3824: 31 5f 69 64 78 74 31 5f 69 64 78 03 43 52 45 41 1_idxt1_idx.CREA +| 3840: 54 45 20 54 41 42 4c 45 20 27 74 31 5f 69 64 78 TE TABLE 't1_idx +| 3856: 27 28 73 65 67 69 64 2c 20 74 65 72 6d 2c 20 70 '(segid, term, p +| 3872: 67 6e 6f 2c 20 50 52 49 4d 41 52 59 20 4b 45 59 gno, PRIMARY KEY +| 3888: 28 73 65 67 69 64 2c 20 74 65 72 6d 29 29 20 57 (segid, term)) W +| 3904: 49 54 48 4f 55 54 20 52 4f 57 49 44 55 02 07 17 ITHOUT ROWIDU... +| 3920: 1b 1b 01 81 01 74 61 62 6c 65 74 31 5f 64 61 74 .....tablet1_dat +| 3936: 61 74 31 5f 64 61 74 61 02 43 52 45 41 54 45 20 at1_data.CREATE +| 3952: 54 41 42 4c 45 20 27 74 31 5f 64 61 74 61 27 28 TABLE 't1_data'( +| 3968: 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 id INTEGER PRIMA +| 3984: 52 59 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 4c RY KEY, block BL +| 4000: 4f 42 29 5b 01 07 17 11 11 08 81 23 74 61 62 6c OB)[.......#tabl +| 4016: 65 74 31 74 31 43 52 45 41 54 45 20 56 49 52 54 et1t1CREATE VIRT +| 4032: 55 41 4c 20 54 41 42 4c 45 20 74 31 20 55 53 49 UAL TABLE t1 USI +| 4048: 4e 47 20 66 74 73 35 28 61 2c 62 20 75 6e 69 6e NG fts5(a,b unin +| 4064: 64 65 78 65 64 2c 63 2c 74 6f 6b 65 6e 69 7a 65 dexed,c,tokenize +| 4080: 3d 22 70 6f 72 74 65 72 20 61 73 63 69 69 22 29 =.porter ascii.) +| page 2 offset 4096 +| 0: 0d 0f 68 00 05 0f 13 00 0f e6 0f 13 0f a8 0f 7c ..h............| +| 16: 0f 2a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 .*.............. +| 3856: 00 00 00 15 0a 03 00 30 00 00 00 00 01 03 03 00 .......0........ +| 3872: 03 01 01 01 02 01 01 03 01 01 37 8c 80 80 80 80 ..........7..... +| 3888: 01 03 00 74 00 00 00 2e 02 30 61 03 02 02 01 01 ...t.....0a..... +| 3904: 62 03 02 03 01 01 63 03 02 04 01 01 67 03 06 01 b.....c.....g... +| 3920: 02 02 01 01 68 03 06 01 02 03 01 01 69 03 06 01 ....h.......i... +| 3936: 02 04 04 06 06 06 08 08 0f ef 00 14 2a 00 00 00 ............*... +| 3952: 00 01 02 02 00 02 01 01 01 02 01 01 25 88 80 80 ............%... +| 3968: 80 80 01 03 00 50 00 00 00 1f 02 30 67 02 08 02 .....P.....0g... +| 3984: 01 02 02 01 01 68 02 08 03 01 02 03 01 01 69 02 .....h........i. +| 4000: 08 04 01 02 04 04 09 09 37 84 80 80 80 80 01 03 ........7....... +| 4016: 00 74 00 00 00 2e 02 30 61 01 02 02 01 01 62 01 .t.....0a.....b. +| 4032: 02 03 01 01 63 01 02 04 01 01 67 01 06 01 02 02 ....c.....g..... +| 4048: 01 01 68 01 06 01 02 03 01 01 69 01 06 01 02 04 ..h.......i..... +| 4064: 04 06 06 06 08 08 07 01 03 00 14 03 09 00 09 00 ................ +| 4080: 00 00 11 24 00 00 00 00 01 01 01 00 01 01 01 01 ...$............ +| page 3 offset 8192 +| 0: 0a 00 00 00 03 00 00 00 00 00 00 00 00 00 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 00 06 04 01 0c ................ +| 4080: 01 03 02 06 04 01 0c 01 02 02 05 04 09 0c 01 02 ................ +| page 4 offset 12288 +| 0: 0d 00 00 00 03 0f be 00 0f ea 0f d4 0f be 00 00 ................ +| 4016: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 14 03 ................ +| 4032: 05 00 17 17 17 61 20 62 20 63 67 20 68 20 69 67 .....a b cg h ig +| 4048: 20 68 20 69 14 02 05 00 17 17 17 67 20 68 20 69 h i.......g h i +| 4064: 61 20 62 20 63 67 20 68 20 69 14 01 05 00 17 17 a b cg h i...... +| 4080: 17 61 20 62 20 63 64 20 65 20 66 67 20 68 20 69 .a b cd e fg h i +| page 5 offset 16384 +| 0: 0d 00 00 00 03 0f e8 00 0f f8 0f f0 0f e8 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 06 03 03 00 12 03 00 03 ................ +| 4080: 06 02 03 00 12 03 00 03 06 01 03 00 12 03 00 03 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 01 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| page 7 offset 24576 +| 0: 0d 00 00 00 03 0f 9e 00 0f e6 0f ef 00 00 00 00 ................ +| 3984: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 41 84 ..............A. +| 4000: 80 80 80 80 01 04 00 81 06 00 00 00 34 02 30 61 ............4.0a +| 4016: 01 01 01 01 01 62 01 01 01 01 01 63 01 01 01 01 .....b.....c.... +| 4032: 01 64 01 01 01 65 01 01 01 66 01 01 01 67 01 01 .d...e...f...g.. +| 4048: 01 01 01 68 01 01 01 01 01 69 01 01 01 04 06 06 ...h.....i...... +| 4064: 06 04 04 04 06 06 07 01 03 00 14 03 09 09 09 0f ................ +| 4080: 0a 03 00 24 00 00 00 00 01 01 01 00 01 01 01 01 ...$............ +| page 8 offset 28672 +| 0: 0a 00 00 00 01 0f fa 00 00 00 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 05 04 09 0c 01 12 ................ +| page 9 offset 32768 +| 0: 0d 00 00 00 03 0f be 00 0f ea 0f d4 0f be 00 00 ................ +| 4016: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 14 03 ................ +| 4032: 05 00 17 17 17 61 20 62 20 63 67 20 68 20 69 67 .....a b cg h ig +| 4048: 20 68 20 69 14 02 05 00 17 17 17 67 20 68 20 69 h i.......g h i +| 4064: 61 20 62 20 63 67 20 68 20 69 14 01 05 00 17 17 a b cg h i...... +| 4080: 17 61 20 62 20 63 64 20 65 20 66 67 20 68 20 69 .a b cd e fg h i +| page 10 offset 36864 +| 0: 0a 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 00 00 00 00 ........vers.... +| end crash-f928a9c1ec68dd.db +}]} {} + +do_catchsql_test 80.1 { +SELECT snippet(rowid, -1, '.', '..', '[', '(]'),snippet(rowid, -1, '.', '.', '', '(]'), highlight(t1, 29, 1 , '') FROM t1('g+ h') WHERE rank MATCH 'bm25(1.0, 10)' ORDER BY NOT (SELECT 1 FROM t1('g+ æ') WHERE rank MATCH 'bm25(1.0, 10)' ORDER BY rank); +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +reset_db +do_test 81.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 40960 pagesize 4096 filename crash-44e8035a976422.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 0a .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 0d 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 96: 00 00 00 00 0d 00 00 00 0d 0b 6e 00 0f a3 0f 4c ..........n....L +| 112: 0e e1 0e 81 0e 24 0d cc 0d 72 0d 1b 0c b0 0c 50 .....$...r.....P +| 128: 0b f8 0b b3 0b 6e 00 00 00 00 00 00 00 00 00 00 .....n.......... +| 2912: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 43 0d ..............C. +| 2928: 06 17 11 11 08 75 74 61 62 6c 65 74 34 74 34 43 .....utablet4t4C +| 2944: 52 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 REATE VIRTUAL TA +| 2960: 42 4c 45 20 74 34 20 55 53 49 4e 47 20 66 74 73 BLE t4 USING fts +| 2976: 35 76 6f 63 61 62 28 27 74 32 27 2c 20 27 72 6f 5vocab('t2', 'ro +| 2992: 77 27 29 43 0c 06 17 11 11 08 75 74 61 62 6c 65 w')C......utable +| 3008: 74 33 74 33 43 52 45 41 54 45 20 56 49 52 54 55 t3t3CREATE VIRTU +| 3024: 41 4c 20 54 41 42 4c 45 20 74 33 20 55 53 49 4e AL TABLE t3 USIN +| 3040: 47 20 66 74 73 35 76 6f 63 61 62 28 27 74 31 27 G fts5vocab('t1' +| 3056: 2c 20 27 72 6f 77 27 29 56 0b 06 17 1f 1f 01 7d , 'row')V....... +| 3072: 74 61 62 6c 65 74 32 5f 63 6f 6e 66 69 67 74 32 tablet2_configt2 +| 3088: 5f 63 6f 6e 66 69 67 0a 43 52 45 41 54 45 20 54 _config.CREATE T +| 3104: 41 42 4c 45 20 27 74 32 5f 63 6f 6e 66 69 67 27 ABLE 't2_config' +| 3120: 28 6b 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 (k PRIMARY KEY, +| 3136: 76 29 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 v) WITHOUT ROWID +| 3152: 5e 0a 07 17 21 21 01 81 07 74 61 62 6c 65 74 32 ^...!!...tablet2 +| 3168: 5f 63 6f 6e 74 65 6e 74 74 32 5f 63 6f 6e 74 65 _contentt2_conte +| 3184: 6e 74 09 43 52 45 41 54 45 20 54 41 42 4c 45 20 nt.CREATE TABLE +| 3200: 27 74 32 5f 63 6f 6e 74 65 6e 74 27 28 69 64 20 't2_content'(id +| 3216: 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 INTEGER PRIMARY +| 3232: 4b 45 59 2c 20 63 30 2c 20 63 31 2c 20 63 32 29 KEY, c0, c1, c2) +| 3248: 69 09 07 17 19 19 01 81 2d 74 61 62 6c 65 74 32 i.......-tablet2 +| 3264: 5f 69 64 78 74 32 5f 69 64 78 08 43 52 45 41 54 _idxt2_idx.CREAT +| 3280: 45 20 54 41 42 4c 45 20 27 74 32 5f 69 64 78 27 E TABLE 't2_idx' +| 3296: 28 73 65 67 69 64 2c 20 74 65 72 6d 2c 20 70 67 (segid, term, pg +| 3312: 6e 6f 2c 20 50 52 49 4d 41 52 59 20 4b 45 59 28 no, PRIMARY KEY( +| 3328: 73 65 67 69 64 2c 20 74 65 72 6d 29 29 20 57 49 segid, term)) WI +| 3344: 54 48 4f 55 54 20 52 4f 57 49 44 55 08 07 17 1b THOUT ROWIDU.... +| 3360: 1b 01 81 01 74 61 62 6c 65 74 32 5f 64 61 74 61 ....tablet2_data +| 3376: 74 32 5f 64 61 74 61 07 43 52 45 41 54 45 20 54 t2_data.CREATE T +| 3392: 41 42 4c 45 20 27 74 32 5f 64 61 74 61 27 28 69 ABLE 't2_data'(i +| 3408: 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 d INTEGER PRIMAR +| 3424: 59 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 4c 4f Y KEY, block BLO +| 3440: 42 29 58 07 07 17 11 11 08 81 1d 74 61 62 6c 65 B)X........table +| 3456: 74 32 74 32 43 52 45 41 54 45 20 56 49 52 54 55 t2t2CREATE VIRTU +| 3472: 41 4c 20 54 41 42 4c 45 20 74 32 20 55 53 49 4e AL TABLE t2 USIN +| 3488: 47 20 66 74 73 35 28 27 61 27 2c 5b 62 5d 2c 22 G fts5('a',[b],. +| 3504: 63 22 2c 64 65 74 61 69 6c 3d 6e 6f 6e 65 2c 63 c.,detail=none,c +| 3520: 6f 6c 75 6d 6e 73 69 7a 65 3d 30 29 56 06 06 17 olumnsize=0)V... +| 3536: 1f 1f 01 7d 74 61 62 6c 65 74 31 5f 63 6f 6e 66 ....tablet1_conf +| 3552: 69 67 74 31 5f 63 6f 6e 66 69 67 06 43 52 45 41 igt1_config.CREA +| 3568: 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 6f 6e TE TABLE 't1_con +| 3584: 66 69 67 27 28 6b 20 50 52 49 4d 41 52 59 20 4b fig'(k PRIMARY K +| 3600: 45 59 2c 20 76 29 20 57 49 54 48 4f 55 54 20 52 EY, v) WITHOUT R +| 3616: 4f 57 49 44 5b 05 07 17 21 21 01 81 01 74 61 62 OWID[...!!...tab +| 3632: 6c 65 74 31 5f 64 6f 63 73 69 7a 65 74 31 5f 64 let1_docsizet1_d +| 3648: 6f 63 73 69 7a 65 05 43 52 45 41 54 45 20 54 41 ocsize.CREATE TA +| 3664: 42 4c 45 20 27 74 31 5f 64 6f 63 73 69 7a 65 27 BLE 't1_docsize' +| 3680: 28 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d (id INTEGER PRIM +| 3696: 41 52 59 20 4b 45 59 2c 20 73 7a 20 42 4c 4f 42 ARY KEY, sz BLOB +| 3712: 29 5e 04 07 17 21 21 01 81 07 74 61 62 6c 65 74 )^...!!...tablet +| 3728: 31 5f 63 6f 6e 74 65 6e 74 74 31 5f 63 6f 6e 74 1_contentt1_cont +| 3744: 65 6e 74 04 43 52 45 41 54 45 20 54 41 42 4c 45 ent.CREATE TABLE +| 3760: 20 27 74 31 5f 63 6f 6e 74 65 6e 74 27 28 69 64 't1_content'(id +| 3776: 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 INTEGER PRIMARY +| 3792: 20 4b 45 59 2c 20 63 30 2c 20 63 31 2c 20 63 32 KEY, c0, c1, c2 +| 3808: 29 69 03 07 17 19 19 01 81 2d 74 61 62 6c 65 74 )i.......-tablet +| 3824: 31 5f 69 64 78 74 31 5f 69 64 78 03 43 52 45 41 1_idxt1_idx.CREA +| 3840: 54 45 20 54 41 42 4c 45 20 27 74 31 5f 69 64 78 TE TABLE 't1_idx +| 3856: 27 28 73 65 67 69 64 2c 20 74 65 72 6d 2c 20 70 '(segid, term, p +| 3872: 67 6e 6f 2c 20 50 52 49 4d 41 52 59 20 4b 45 59 gno, PRIMARY KEY +| 3888: 28 73 65 67 69 64 2c 20 74 65 72 6d 29 29 20 57 (segid, term)) W +| 3904: 49 54 48 4f 55 54 20 52 4f 57 49 44 55 02 07 17 ITHOUT ROWIDU... +| 3920: 1b 1b 01 81 01 74 61 62 6c 65 74 31 5f 64 61 74 .....tablet1_dat +| 3936: 61 74 31 5f 64 61 74 61 02 43 52 45 41 54 45 20 at1_data.CREATE +| 3952: 54 41 42 4c 45 20 27 74 31 5f 64 61 74 61 27 28 TABLE 't1_data'( +| 3968: 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 id INTEGER PRIMA +| 3984: 52 59 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 4c RY KEY, block BL +| 4000: 4f 42 29 5b 01 07 17 11 11 08 81 23 74 61 62 6c OB)[.......#tabl +| 4016: 65 74 31 74 31 43 52 45 41 54 45 20 56 49 52 54 et1t1CREATE VIRT +| 4032: 55 41 4c 20 54 41 42 4c 45 20 74 31 20 55 53 49 UAL TABLE t1 USI +| 4048: 4e 47 20 66 74 73 35 28 61 2c 62 20 75 6e 69 6e NG fts5(a,b unin +| 4064: 64 65 78 65 64 2c 63 2c 74 6f 6b 65 6e 69 7a 65 dexed,c,tokenize +| 4080: 3d 22 70 6f 72 74 65 72 20 61 73 63 69 69 22 29 =.porter ascii.) +| page 2 offset 4096 +| 0: 0d 0f 68 00 05 0f 13 00 0f e6 0f 13 0f a8 0f 7c ..h............| +| 16: 0f 2a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 .*.............. +| 3856: 00 00 00 15 0a 03 00 30 00 00 00 00 01 03 03 00 .......0........ +| 3872: 03 01 01 01 02 01 01 03 01 01 37 8c 80 80 80 80 ..........7..... +| 3888: 01 03 00 74 00 00 00 2e 02 30 61 03 02 02 01 01 ...t.....0a..... +| 3904: 62 03 02 03 01 01 63 03 02 04 01 01 67 03 06 01 b.....c.....g... +| 3920: 02 02 01 01 68 03 06 01 02 03 01 01 69 03 06 01 ....h.......i... +| 3936: 02 04 04 06 06 06 08 08 0f ef 00 14 2a 00 00 00 ............*... +| 3952: 00 01 02 02 00 02 01 01 01 02 01 01 25 88 80 80 ............%... +| 3968: 80 80 01 03 00 50 00 00 00 1f 02 30 67 02 08 02 .....P.....0g... +| 3984: 01 02 02 01 01 68 02 08 03 8d 02 03 01 01 6a 42 .....h........jB +| 4000: 08 04 01 02 04 04 09 09 37 84 80 80 80 80 01 03 ........7....... +| 4016: 00 74 00 00 00 2e 02 30 61 01 12 02 01 01 62 01 .t.....0a.....b. +| 4032: 02 03 01 01 63 01 02 04 01 01 67 01 06 01 02 02 ....c.....g..... +| 4048: 01 01 68 01 05 01 02 03 01 01 69 01 06 01 02 04 ..h.......i..... +| 4064: 04 06 06 06 08 08 07 01 03 00 14 03 09 00 09 00 ................ +| 4080: 00 00 11 24 00 00 00 00 01 01 01 00 01 01 01 01 ...$............ +| page 3 offset 8192 +| 0: 0a 00 00 00 03 0f ec 00 0f fa 0f f3 0f ec 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 00 06 04 01 0c ................ +| 4080: 01 03 02 06 04 01 0c 01 02 02 05 04 09 0c 01 02 ................ +| page 4 offset 12288 +| 0: 0d 00 00 00 03 0f be 00 0f ea 00 00 00 00 00 00 ................ +| 4016: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 14 03 ................ +| 4032: 05 00 17 17 17 61 20 62 20 63 67 20 68 20 69 67 .....a b cg h ig +| 4048: 20 68 20 69 14 02 05 00 17 17 17 67 20 68 20 69 h i.......g h i +| 4064: 61 20 62 20 63 67 20 68 20 69 14 01 05 00 17 17 a b cg h i...... +| 4080: 17 61 20 62 20 63 64 20 65 20 66 67 20 68 20 69 .a b cd e fg h i +| page 5 offset 16384 +| 0: 0d 00 00 00 03 0f e8 00 0f f8 0f f0 0f e8 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 06 03 03 00 12 03 00 03 ................ +| 4080: 06 02 03 00 12 03 00 03 06 01 03 00 12 03 00 03 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| page 7 offset 24576 +| 0: 0d 00 00 00 03 0f 9e 00 0f e6 0f ef 0f 9e 00 00 ................ +| 3984: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 41 84 ..............A. +| 4000: 80 80 80 80 01 04 00 81 06 00 00 00 34 02 30 61 ............4.0a +| 4016: 01 01 01 01 01 62 01 01 01 01 01 63 01 01 01 01 .....b.....c.... +| 4032: e6 64 01 01 01 65 01 01 01 66 01 01 01 67 01 01 .d...e...f...g.. +| 4048: 01 01 01 68 01 01 01 01 01 69 01 01 01 04 06 06 ...h.....i...... +| 4064: 06 04 04 04 06 06 07 01 03 00 14 03 09 09 09 0f ................ +| 4080: 0a 03 00 24 00 00 00 00 01 01 01 00 01 01 01 01 ...$............ +| page 8 offset 28672 +| 0: 0a 00 00 00 01 0f fa 00 0f fa 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 05 04 09 0c 01 02 ................ +| page 9 offset 32768 +| 0: 0d 00 00 00 03 0f be 00 0f ea 0f d4 0f be 00 00 ................ +| 4016: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 14 03 ................ +| 4032: 05 00 17 17 17 61 20 62 20 63 67 20 68 20 69 67 .....a b cg h ig +| 4048: 20 68 21 69 14 02 05 00 17 17 17 67 20 68 20 69 h!i.......g h i +| 4064: 61 20 62 20 63 67 20 68 20 69 14 01 05 00 17 17 a b cg h i...... +| 4080: 17 61 20 62 20 63 64 20 65 20 66 67 20 68 20 69 .a b cd e fg h i +| page 10 offset 36864 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| end crash-44e8035a976422.db +}]} {} + +do_catchsql_test 81.2 { + UPDATE t1 SET b=zeroblob(299); +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +reset_db +do_test 82.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 32768 pagesize 4096 filename c0.txt.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 08 .....@ ........ +| 32: 00 00 00 02 00 00 00 01 00 00 00 09 00 00 00 04 ................ +| 96: 00 00 00 00 0d 0f c7 00 07 0d 92 00 0f 8d 0f 36 ...............6 +| 112: 0e cb 0e 6b 0e 0e 0d b6 0d 92 0d 92 00 00 00 00 ...k............ +| 3472: 00 00 22 08 06 17 11 11 01 31 74 61 62 6c 65 74 .........1tablet +| 3488: 32 74 32 08 43 52 45 41 54 45 20 54 41 42 4c 45 2t2.CREATE TABLE +| 3504: 20 74 32 28 78 29 56 07 06 17 1f 1f 01 7d 74 61 t2(x)V.......ta +| 3520: 62 6c 65 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 blet1_configt1_c +| 3536: 6f 6e 66 69 67 07 43 52 45 41 54 45 20 54 41 42 onfig.CREATE TAB +| 3552: 4c 45 20 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b LE 't1_config'(k +| 3568: 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 PRIMARY KEY, v) +| 3584: 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 5b 06 WITHOUT ROWID[. +| 3600: 07 17 21 21 01 81 01 74 61 62 6c 65 74 31 5f 64 ..!!...tablet1_d +| 3616: 6f 63 73 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 ocsizet1_docsize +| 3632: 06 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 .CREATE TABLE 't +| 3648: 31 5f 64 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 1_docsize'(id IN +| 3664: 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE +| 3680: 59 2c 20 73 7a 20 42 4c 4f 42 29 5e 05 07 17 21 Y, sz BLOB)^...! +| 3696: 21 01 81 07 74 61 62 6c 65 74 31 5f 63 6f 6e 74 !...tablet1_cont +| 3712: 65 6e 74 74 31 5f 63 6f 6e 74 65 6e 74 05 43 52 entt1_content.CR +| 3728: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 EATE TABLE 't1_c +| 3744: 6f 6e 74 65 6e 74 27 28 69 64 20 49 4e 54 45 47 ontent'(id INTEG +| 3760: 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 ER PRIMARY KEY, +| 3776: 63 30 2c 20 63 31 2c 20 63 32 29 69 04 07 17 19 c0, c1, c2)i.... +| 3792: 19 01 81 2d 74 61 62 6c 65 74 31 5f 69 64 78 74 ...-tablet1_idxt +| 3808: 31 5f 69 64 78 04 43 52 45 41 54 45 20 54 41 42 1_idx.CREATE TAB +| 3824: 4c 45 20 27 74 31 5f 69 64 78 27 28 73 65 67 69 LE 't1_idx'(segi +| 3840: 64 2c 20 74 65 72 6d 2c 20 70 67 6e 6f 2c 20 50 d, term, pgno, P +| 3856: 52 49 4d 41 52 59 20 4b 45 59 28 73 65 67 69 64 RIMARY KEY(segid +| 3872: 2c 20 74 65 72 6d 29 29 20 57 49 54 48 4f 55 54 , term)) WITHOUT +| 3888: 20 52 4f 57 49 44 55 03 07 17 1b 1b 01 81 01 74 ROWIDU........t +| 3904: 61 62 6c 65 74 31 5f 64 61 74 61 74 31 5f 64 61 ablet1_datat1_da +| 3920: 74 61 03 43 52 45 41 54 45 20 54 41 42 4c 45 20 ta.CREATE TABLE +| 3936: 27 74 31 5f 64 61 74 61 27 28 69 64 20 49 4e 54 't1_data'(id INT +| 3952: 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 EGER PRIMARY KEY +| 3968: 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 29 38 02 06 , block BLOB)8.. +| 3984: 17 11 11 08 5f 74 61 62 6c 65 74 31 74 31 43 52 ...._tablet1t1CR +| 4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 35 LE t1 USING fts5 +| 4032: 28 61 2c 62 2c 63 29 00 00 00 00 00 00 00 00 00 (a,b,c)......... +| page 3 offset 8192 +| 0: 0d 00 00 00 03 0c 94 00 0f e6 0f ef 0c 94 00 00 ................ +| 3216: 00 00 00 00 86 4a 84 80 80 80 80 01 04 00 8d 18 .....J.......... +| 3232: 00 00 03 2b 02 30 30 01 02 06 01 02 06 01 02 06 ...+.00......... +| 3248: 1f 02 03 01 02 03 01 02 03 01 08 32 30 31 36 30 ...........20160 +| 3264: 36 30 39 01 02 07 01 02 07 01 02 07 01 01 34 01 609...........4. +| 3280: 02 05 01 02 05 01 02 05 01 01 35 01 02 04 01 02 ..........5..... +| 3296: 04 01 02 04 02 07 30 30 30 30 30 30 30 1c 02 04 ......0000000... +| 3312: 01 02 04 01 02 04 01 06 62 69 6e 61 72 79 03 06 ........binary.. +| 3328: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3344: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3360: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 ................ +| 3376: 03 06 01 02 02 03 06 01 02 02 01 08 63 6f 6d 70 ............comp +| 3392: 69 6c 65 72 01 02 02 01 02 02 01 02 02 01 06 64 iler...........d +| 3408: 62 73 74 61 74 07 02 03 01 02 03 01 02 03 02 04 bstat........... +| 3424: 65 62 75 67 04 02 02 01 02 02 01 02 02 01 06 65 ebug...........e +| 3440: 6e 61 62 6c 65 07 02 02 01 02 02 01 02 02 01 02 nable........... +| 3456: 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 ................ +| 3472: 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 ................ +| 3488: 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 ................ +| 3504: 02 01 02 02 02 08 78 74 65 6e 73 69 6f 6e 1f 02 ......xtension.. +| 3520: 04 01 02 04 01 02 04 01 04 66 74 73 34 0a 02 03 .........fts4... +| 3536: 01 02 03 01 02 03 04 01 35 0d 02 03 01 02 03 01 ........5....... +| 3552: 02 03 01 03 67 63 63 01 02 03 01 02 03 01 02 03 ....gcc......... +| 3568: 02 06 65 6f 70 6f 6c 79 10 02 03 01 02 03 01 02 ..eopoly........ +| 3584: 03 01 05 6a 73 6f 6e 31 13 02 03 01 02 03 01 02 ...json1........ +| 3600: 03 01 04 6c 6f 61 64 1f 02 03 01 02 03 01 02 03 ...load......... +| 3616: 01 03 6d 61 78 1c 02 02 01 02 02 01 02 02 02 05 ..max........... +| 3632: 65 6d 6f 72 79 1c 02 03 01 02 03 01 02 03 04 04 emory........... +| 3648: 73 79 73 35 16 02 03 01 02 02 03 01 03 01 06 6e sys5...........n +| 3664: 6f 63 61 73 65 02 06 01 02 02 03 06 01 02 02 03 ocase........... +| 3680: 06 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 ................ +| 3696: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3712: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3728: 02 01 04 6f 6d 69 74 1f 02 02 01 02 02 01 02 02 ...omit......... +| 3744: 01 05 72 74 72 65 65 19 02 03 01 02 03 01 02 03 ..rtree......... +| 3760: 04 02 69 6d 01 06 01 02 02 03 06 01 02 02 03 06 ..im............ +| 3776: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3792: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3808: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 ................ +| 3824: 01 0a 74 68 72 65 61 64 73 61 66 65 03 57 34 56 ..threadsafe.W4V +| 3840: 94 64 91 46 85 84 04 76 74 61 62 07 02 04 01 02 .d.F...vtab..... +| 3856: 04 01 02 04 01 01 78 01 06 01 01 02 01 06 01 01 ......x......... +| 3872: 02 01 06 01 01 02 01 06 01 01 02 01 06 01 10 02 ................ +| 3888: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 3904: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 3920: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 ................ +| 3936: 01 02 01 06 01 01 10 01 06 01 01 02 01 06 01 01 ................ +| 3952: 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 ................ +| 3968: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 3984: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 4000: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 ................ +| 4016: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 ................ +| 4032: 02 01 06 01 01 02 01 06 01 01 02 04 15 13 0c 0c ................ +| 4048: 12 44 13 11 0f 47 13 0f 0c 0e 11 10 0f 0e 10 0f .D...G.......... +| 4064: 44 0f 10 40 15 0f 07 01 03 00 14 24 5a 24 24 0f D..@.......$Z$$. +| 4080: 0a 03 00 24 00 00 00 00 01 01 01 00 01 01 01 01 ...$............ +| page 4 offset 12288 +| 0: 0a 00 00 00 01 0f fa 00 00 00 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 05 04 09 0c 01 02 ................ +| page 5 offset 16384 +| 0: 0d 00 00 00 24 0c 0a 00 0f d8 0f af 0f 86 0f 74 ....$..........t +| 16: 0f 61 0f 4e 0f 2f 0f 0f 0e ef 0e d7 0e be 0e a5 .a.N./.......... +| 32: 0e 8d 0e 74 0e 5b 0e 40 0e 24 0e 08 0d ef 0d d5 ...t.[.@.$...... +| 48: 0d bb 0d a0 0d 84 0d 68 0d 4f 0d 35 0d 1b 0c fb .......h.O.5.... +| 64: 0c da 0c b9 0c 99 0c 78 0c 57 00 00 00 00 00 00 .......x.W...... +| 3072: 00 00 00 00 00 00 00 00 00 00 18 24 05 00 25 0f ...........$..%. +| 3088: 19 54 48 52 45 41 44 53 41 46 45 3d 30 58 42 49 .THREADSAFE=0XBI +| 3104: 4e 41 52 59 18 23 05 00 25 0f 19 54 48 52 45 41 NARY.#..%..THREA +| 3120: 44 53 41 46 45 3d 30 58 4e 4f 43 41 53 45 17 22 DSAFE=0XNOCASE.. +| 3136: 05 00 25 0f 17 54 48 52 45 41 44 53 31 46 45 3d ..%..THREADS1FE= +| 3152: 30 58 52 64 52 49 4d 1f 21 05 00 33 0f 19 4f 4d 0XRdRIM.!..3..OM +| 3168: 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 4f IT LOAD EXTENSIO +| 3184: 4e 58 42 49 4e 41 52 59 1f 20 05 00 33 0f 19 4f NXBINARY. ..3..O +| 3200: 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 MIT LOAD EXTENSI +| 3216: 4f 4e 58 4e 4f 43 41 53 45 1e 1f 05 00 33 0f 17 ONXNOCASE....3.. +| 3232: 4f 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 OMIT LOAD EXTENS +| 3248: 49 4f 4e 58 52 54 52 49 4d 1f 1e 05 00 33 0f 19 IONXRTRIM....3.. +| 3264: 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 30 MAX MEMORY=50000 +| 3280: 30 30 30 58 42 49 4e 41 52 59 1f 1d 05 00 33 0f 000XBINARY....3. +| 3296: 19 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 .MAX MEMORY=5000 +| 3312: 30 30 30 30 58 4e 4f 43 41 53 45 1e 1c 05 00 33 0000XNOCASE....3 +| 3328: 0f 17 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 ..MAX MEMORY=500 +| 3344: 30 30 30 30 30 58 52 54 52 49 4d 18 1b 05 00 25 00000XRTRIM....% +| 3360: 0f 19 45 4e 41 42 4c 45 20 52 54 52 45 45 58 42 ..ENABLE RTREEXB +| 3376: 49 4e 41 52 59 18 1a 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3392: 4c 45 20 52 54 52 45 45 58 4e 4f 43 41 53 45 17 LE RTREEXNOCASE. +| 3408: 19 05 00 25 0f 17 45 4e 41 42 4c 45 20 52 54 52 ...%..ENABLE RTR +| 3424: 45 45 58 52 54 52 49 4d 1a 18 05 00 29 0f 19 45 EEXRTRIM....)..E +| 3440: 4e 41 42 4b 45 20 4d 45 4d 53 59 53 35 58 42 49 NABKE MEMSYS5XBI +| 3456: 4e 41 52 59 1a 17 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3472: 42 60 2d 45 4d 53 59 53 35 58 4e 4f 43 41 53 45 B`-EMSYS5XNOCASE +| 3488: 19 16 05 00 29 0f 17 45 4e 41 42 4c 45 20 4d 45 ....)..ENABLE ME +| 3504: 4d 53 59 53 35 58 52 54 52 49 4d 18 15 05 00 25 MSYS5XRTRIM....% +| 3520: 0f 19 45 4e 41 42 4c 45 20 4a 53 4f 4e 31 58 42 ..ENABLE JSON1XB +| 3536: 49 4e 41 52 59 18 14 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3552: 4c 45 20 4a 53 4f 4e 31 58 4e 4f 43 41 53 45 17 LE JSON1XNOCASE. +| 3568: 13 05 00 25 0f 17 45 4e 41 42 4c 45 20 4a 53 4f ...%..ENABLE JSO +| 3584: 4e 31 58 52 54 52 49 4d 1a 12 05 00 29 0f 19 45 N1XRTRIM....)..E +| 3600: 42 4c 45 20 47 45 4f 50 4f 4c 59 58 42 49 4e 41 BLE GEOPOLYXBINA +| 3616: 52 59 1a 11 05 00 39 0f 19 45 4e 41 42 4c 45 2e RY....9..ENABLE. +| 3632: 41 40 47 45 4f 50 4f 4c 59 58 4e 4f 43 41 53 40 A@GEOPOLYXNOCAS@ +| 3648: 4f 4c 59 58 55 19 10 05 00 29 0f 17 45 4e 41 42 OLYXU....)..ENAB +| 3664: 4c 45 20 47 45 4f 52 54 52 49 4d 17 0f 05 00 23 LE GEORTRIM....# +| 3680: 0f 19 45 4e 41 42 4c 45 20 46 54 53 35 58 42 49 ..ENABLE FTS5XBI +| 3696: 4e 41 52 59 17 0e 05 00 23 0f 19 45 4e 41 42 4c NARY....#..ENABL +| 3712: 45 20 46 54 53 35 58 4e 4f 43 41 53 45 16 0d 05 E FTS5XNOCASE... +| 3728: 00 23 0f 17 45 4e 41 42 4c 45 20 46 54 53 35 58 .#..ENABLE FTS5X +| 3744: 52 54 52 49 4d 17 0c 05 00 23 0f 19 45 4e 41 42 RTRIM....#..ENAB +| 3760: 4c 45 20 46 54 53 34 58 42 49 4e 41 52 59 17 0b LE FTS4XBINARY.. +| 3776: 05 00 23 0f 19 45 4e 41 42 4c 45 20 46 54 53 34 ..#..ENABLE FTS4 +| 3792: 58 4e 4f 43 41 53 45 16 0a 05 00 23 0f 17 45 4e XNOCASE....#..EN +| 3808: 41 42 4c 45 20 46 54 53 34 58 52 54 52 49 4d 1e ABLE FTS4XRTRIM. +| 3824: 09 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3840: 54 41 54 20 56 54 41 42 58 42 49 4e 41 52 59 1e TAT VTABXBINARY. +| 3856: 08 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3872: 54 41 54 20 56 54 24 15 48 4e 4f 43 41 53 45 1d TAT VT$.HNOCASE. +| 3888: 07 05 00 31 0f 17 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3904: 54 41 54 20 56 54 41 42 58 52 54 52 49 4d 11 06 TAT VTABXRTRIM.. +| 3920: 05 00 17 0f 19 44 45 42 55 47 58 42 49 4e 41 52 .....DEBUGXBINAR +| 3936: 59 11 05 05 00 17 0f 19 44 45 42 55 47 58 4e 4f Y.......DEBUGXNO +| 3952: 43 41 53 45 10 04 05 00 17 0f 17 44 45 42 55 47 CASE.......DEBUG +| 3968: 58 52 54 52 49 4d 27 03 05 00 43 0f 19 43 4f 4d XRTRIM'...C..COM +| 3984: 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e 30 20 PILER=gcc-5.4.0 +| 4000: 32 30 31 36 30 36 30 39 58 42 49 4e 41 52 59 27 20160609XBINARY' +| 4016: 02 05 00 43 0f 19 43 4f 4d 50 49 4c 45 52 3d 67 ...C..COMPILER=g +| 4032: 63 63 2d 35 2e 34 2e 30 20 32 30 31 36 30 36 30 cc-5.4.0 2016060 +| 4048: 39 58 4e 4f 43 41 53 45 26 01 05 00 43 0f 17 43 9XNOCASE&...C..C +| 4064: 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e OMPILER=gcc-5.4. +| 4080: 30 20 32 30 31 36 30 36 30 39 58 52 54 52 49 4d 0 20160609XRTRIM +| page 6 offset 20480 +| 0: 0d 00 00 00 24 0e e0 00 0f f8 0f f0 0f e8 0f e0 ....$........... +| 16: 0f d8 0f d0 0f c8 0f c0 0f b8 0f b0 0f a8 0f a0 ................ +| 32: 1f 98 0f 90 0f 88 0f 80 0f 78 0f 70 0f 68 0f 60 .........x.p.h.` +| 48: 0f 58 0f 50 0f 48 0f 40 0f 38 0f 30 0f 28 0f 20 .X.P.H.@.8.0.(. +| 64: 0f 18 0f 10 0f 08 0f 00 0e f8 0e f0 0e e8 00 00 ................ +| 3808: 06 24 03 00 12 02 01 01 06 23 03 00 12 02 01 01 .$.......#...... +| 3824: 06 22 03 00 12 02 01 01 06 21 03 00 12 03 01 01 .........!...... +| 3840: 06 20 03 00 12 03 01 01 06 1f 03 00 12 03 01 01 . .............. +| 3856: 06 1e 03 00 12 03 01 01 06 1d 03 00 12 03 01 01 ................ +| 3872: 06 1c 03 00 12 03 01 01 06 1b 03 00 12 02 01 01 ................ +| 3888: 06 1a 03 00 12 02 01 01 06 19 03 00 12 02 01 01 ................ +| 3904: 06 18 03 00 12 02 01 01 06 17 03 00 12 02 01 01 ................ +| 3920: 06 15 f3 00 12 02 01 01 06 15 03 00 12 02 01 01 ................ +| 3936: 06 14 03 00 12 02 01 01 06 13 03 00 12 02 01 01 ................ +| 3952: 06 12 03 00 12 02 01 01 06 11 03 00 12 02 01 01 ................ +| 3968: 06 10 03 00 12 02 01 01 06 0f 03 00 12 02 01 01 ................ +| 3984: 06 0e 03 00 12 02 01 01 06 0d 03 00 12 02 01 01 ................ +| 4000: 06 0c 03 00 12 02 01 01 06 0b 03 00 12 02 01 01 ................ +| 4016: 06 0a 03 00 12 02 01 01 06 09 03 00 12 03 01 01 ................ +| 4032: 06 08 03 00 12 03 01 01 06 07 03 00 12 03 01 01 ................ +| 4048: 06 06 03 00 12 01 01 01 06 05 03 00 12 01 01 01 ................ +| 4064: 06 04 03 00 12 01 01 01 06 03 03 00 12 06 01 01 ................ +| 4080: 06 02 03 00 12 06 01 01 06 01 03 00 12 06 01 01 ................ +| page 7 offset 24576 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| page 8 offset 28672 +| 0: 0d 00 00 00 03 0f d6 00 0f f4 0f e9 0f d6 00 00 ................ +| 4048: 00 00 00 00 00 00 11 03 02 2b 69 6e 74 65 67 72 .........+integr +| 4064: 69 74 79 2d 63 68 65 63 6b 09 02 02 1b 72 65 62 ity-check....reb +| 4080: 75 69 6c 64 0a 01 02 1d 6f 70 74 69 00 00 00 00 uild....opti.... +| end c0.txt.db +}]} {} + +do_execsql_test 82.2 { + UPDATE t1 SET b=quote(zeroblob(current_date)) WHERE t1 MATCH 't*'; + PRAGMA writable_schema=ON; + + UPDATE sqlite_schema SET sql='SELECT * FROM t1' WHERE rowid=6; + INSERT INTO t1(t1,rank) VALUES('secure-delete',1); +} + +do_catchsql_test 82.3 { + CREATE VIRTUAL TABLE IF NOT EXISTS t USING rtree(x,c); +} {1 {Too few columns for an rtree table}} + +do_catchsql_test 82.4 { + BEGIN; + REPLACE INTO t1(rowid,b,a,rowid) VALUES(x'44023b9eb002d28b0ee90c',1,2,3); + SAVEPOINT b; +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +reset_db +do_test 83.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 24576 pagesize 4096 filename crash-c4a4c5492615bd.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 06 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 06 00 00 00 00 ................ +| 96: 00 00 00 00 0d 00 00 00 06 0e 0f 00 0f aa 0f 53 ...............S +| 112: 0e e8 0e 8b 0e 33 0e 0f 00 01 00 00 00 00 00 00 .....3.......... +| 3584: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 22 ................ +| 3600: 06 06 17 11 11 01 31 74 61 62 6c 65 62 62 62 62 ......1tablebbbb +| 3616: 06 43 52 45 41 54 45 20 54 41 42 4c 45 20 62 62 .CREATE TABLE bb +| 3632: 28 61 29 56 05 06 17 1f 1f 01 7d 74 61 62 6c 65 (a)V.......table +| 3648: 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 6f 6e 66 t1_configt1_conf +| 3664: 69 67 05 43 52 45 41 54 45 20 54 41 42 4c 45 20 ig.CREATE TABLE +| 3680: 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b 20 50 52 't1_config'(k PR +| 3696: 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 20 57 49 IMARY KEY, v) WI +| 3712: 54 48 4f 55 54 20 52 4f 57 49 44 5b 04 07 17 21 THOUT ROWID[...! +| 3728: 21 01 81 01 74 61 62 6c 65 74 31 5f 64 6f 63 73 !...tablet1_docs +| 3744: 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 04 43 52 izet1_docsize.CR +| 3760: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 64 EATE TABLE 't1_d +| 3776: 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 54 45 47 ocsize'(id INTEG +| 3792: 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 ER PRIMARY KEY, +| 3808: 73 7a 20 42 4c 4f 42 29 69 03 07 17 19 19 01 81 sz BLOB)i....... +| 3824: 2d 74 61 62 6c 65 74 31 5f 69 64 78 74 31 5f 69 -tablet1_idxt1_i +| 3840: 64 78 03 43 52 45 41 54 45 20 54 41 42 4c 45 20 dx.CREATE TABLE +| 3856: 27 74 31 5f 69 64 78 27 28 73 65 67 69 64 2c 20 't1_idx'(segid, +| 3872: 74 65 72 6d 2c 20 70 67 6e 6f 2c 20 50 52 49 4d term, pgno, PRIM +| 3888: 41 52 59 20 4b 45 59 28 73 65 67 69 64 2c 20 74 ARY KEY(segid, t +| 3904: 65 72 6d 29 29 20 57 49 54 48 4f 55 54 20 52 4f erm)) WITHOUT RO +| 3920: 57 49 44 55 02 07 17 1b 1b 01 81 01 74 61 62 6c WIDU........tabl +| 3936: 65 74 31 5f 64 61 74 61 74 31 5f 64 61 74 61 02 et1_datat1_data. +| 3952: 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 CREATE TABLE 't1 +| 3968: 5f 64 61 74 61 27 28 69 64 20 49 4e 54 45 47 45 _data'(id INTEGE +| 3984: 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 62 R PRIMARY KEY, b +| 4000: 6c 6f 63 6b 20 42 4c 4f 42 29 54 01 07 17 11 11 lock BLOB)T..... +| 4016: 08 81 15 74 61 62 6c 65 74 31 74 31 43 52 45 41 ...tablet1t1CREA +| 4032: 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45 TE VIRTUAL TABLE +| 4048: 20 74 31 20 55 53 49 4e 47 20 66 74 73 35 28 61 t1 USING fts5(a +| 4064: 2c 62 2c 70 72 65 66 69 78 3d 22 31 2c 32 2c 33 ,b,prefix=.1,2,3 +| 4080: 2c 34 22 2c 20 63 6f 6e 74 65 6e 74 3d 22 22 29 ,4., content=..) +| page 2 offset 4096 +| 0: 0d 0b 6a 00 37 09 4c 02 0f e7 09 4c 0f c6 0f a4 ..j.7.L....L.... +| 16: 0f 88 0f 6d 0f 4b 0f 2c 0f 0e 0e ec 0e cd 0e ad ...m.K.,........ +| 32: 0e 8e 0e 6c 0e 4b 0e 29 0e 08 0d e6 0d c4 0d b5 ...l.K.)........ +| 48: 0d 97 0d 76 0d 54 0d 31 0d 15 0c f3 0c d3 0c b5 ...v.T.1........ +| 64: 0c 95 0c 73 0c 54 0c 32 0c 10 0b ee 0b cc 0b b0 ...s.T.2........ +| 80: 0b 8d 0b 7e 0b 48 0b 2e 0b 00 4a ef fa cc 0a ad ...~.H....J..... +| 96: 0a 8c 0a 6d 0a 4d 0a 2b 0a 0c 09 00 00 00 00 00 ...m.M.+........ +| 2368: 00 00 00 00 00 00 00 00 00 00 00 00 15 0a 03 00 ................ +| 2384: 30 00 00 00 01 01 03 35 00 03 01 01 12 02 01 12 0......5........ +| 2400: 03 01 11 1c 8c 80 80 80 80 10 03 00 3e 00 00 00 ............>... +| 2416: 17 01 05 05 34 74 61 62 6c 03 02 03 01 04 77 68 ....4tabl.....wh +| 2432: 65 72 03 02 06 09 1b 8c 80 80 80 80 0f 03 00 3c er.............< +| 2448: 00 00 00 16 05 34 66 74 73 34 03 02 02 01 04 6e .....4fts4.....n +| 2464: 75 6d 62 03 07 01 04 09 1b 8c 80 80 80 80 0e 03 umb............. +| 2480: 00 3c 00 00 00 16 04 33 74 68 65 03 06 01 01 04 .<.....3the..... +| 2496: 01 03 77 68 65 03 02 04 04 0a 1b 8c 80 80 80 80 ..whe........... +| 2512: 0d 03 00 3c 00 00 00 16 04 33 6e 75 6d 03 06 01 ...<.....3num... +| 2528: 01 05 01 03 74 61 62 03 02 03 04 0a 19 8c 80 80 ....tab......... +| 2544: 80 80 0c 03 00 38 00 00 00 14 03 32 77 68 03 02 .....8.....2wh.. +| 2560: 04 00 04 33 66 74 73 03 02 02 04 07 18 8c 80 80 ...3fts......... +| 2576: 80 80 0b 03 00 36 00 00 00 13 03 32 74 61 03 02 .....6.....2ta.. +| 2592: 03 02 01 68 03 06 01 01 04 04 07 1b 8c 80 80 80 ...h............ +| 2608: 80 0a 03 00 3c 00 00 00 16 03 32 6e 75 03 06 01 ....<.....2nu... +| 2624: 01 05 01 02 6f 66 03 06 01 01 06 04 09 19 8c 80 ....of.......... +| 2640: 80 80 80 09 03 00 38 00 00 00 14 03 32 66 74 03 ......8.....2ft. +| 2656: 02 02 01 02 69 73 02 06 01 01 03 04 07 18 8c 80 ....is.......... +| 2672: 80 80 22 08 03 00 36 00 00 00 13 02 31 74 03 08 ......6.....1t.. +| 2688: 03 01 01 04 01 01 77 03 02 04 04 09 1a 8c 80 80 ......w......... +| 2704: 80 80 07 03 00 3a 00 00 00 15 02 31 6e 03 08 01 .....:.....1n... +| 2720: 01 02 05 01 01 6f 03 06 01 01 06 04 09 18 8c 80 .....o.......... +| 2736: 80 80 80 06 03 00 36 00 00 00 13 04 02 31 66 03 ......6......1f. +| 2752: 02 02 01 01 69 03 06 01 01 03 05 06 1c 8c 80 80 ....i........... +| 2768: 80 80 05 03 00 3e 00 00 00 17 04 30 74 68 65 03 .....>.....0the. +| 2784: 06 01 01 04 01 05 77 68 65 72 65 03 02 04 0a 15 ......where..... +| 2800: 8c 80 80 80 80 04 03 00 30 00 00 00 11 01 01 06 ........0....... +| 2816: 06 30 74 61 62 6c 65 03 01 f3 07 1c 8c 80 80 80 .0table......... +| 2832: 80 03 03 00 3e 00 00 00 17 07 30 6e 75 6d 62 65 ....>.....0numbe +| 2848: 72 03 06 01 01 05 01 02 6f 66 03 06 04 0d 13 8c r.......of...... +| 2864: 80 80 80 80 02 03 00 2c 00 00 00 0f 01 01 03 02 .......,........ +| 2880: 30 6e 03 06 01 01 02 07 1b 8c 80 80 80 80 01 03 0n.............. +| 2896: 00 3c 00 00 00 16 08 30 66 74 73 34 61 75 78 03 .<.....0fts4aux. +| 2912: 02 02 01 02 69 73 03 06 04 0c 00 00 00 14 2a 00 ....is........*. +| 2928: 00 00 01 01 02 24 00 02 01 01 12 02 01 12 08 88 .....$.......... +| 2944: 80 80 80 80 12 03 00 16 00 00 00 05 02 1c 88 80 ................ +| 2960: 80 80 80 11 03 00 3e 00 00 00 17 05 34 72 6f 77 ......>.....4row +| 2976: 73 02 06 01 01 05 01 04 74 68 65 72 02 02 04 0b s.......ther.... +| 2992: 15 88 80 80 80 80 10 03 00 30 00 00 00 11 02 01 .........0...... +| 3008: 01 07 05 34 62 65 74 77 02 02 04 08 1b 88 80 80 ...4betw........ +| 3024: 80 80 0f 03 00 3c 00 00 00 16 04 04 33 72 6f 77 .....<......3row +| 3040: 02 06 01 01 05 01 03 74 68 65 02 08 05 0a 1b 88 .......the...... +| 3056: 80 80 80 80 0e 03 00 3c 00 00 00 16 01 01 02 04 .......<........ +| 3072: 33 61 72 65 02 02 b3 01 03 62 65 74 02 02 07 08 3are.....bet.... +| 3088: 1b 88 80 80 80 80 0d 03 00 3c 00 00 00 16 03 32 .........<.....2 +| 3104: 74 68 02 08 02 01 01 07 00 04 33 61 6e 64 02 06 th........3and.. +| 3120: 04 0a 1b 88 80 80 80 80 0c 03 00 3c 00 00 00 16 ...........<.... +| 3136: 03 32 69 6e 02 06 01 01 06 01 02 72 6f 02 06 01 .2in.......ro... +| 3152: 01 05 04 09 18 88 80 80 80 80 0b 03 00 36 00 0f .............6.. +| 3168: f0 13 02 03 32 61 72 02 02 03 01 02 62 65 02 02 ....2ar.....be.. +| 3184: 03 05 07 1b 88 80 80 80 80 0a 03 00 3c dd 00 00 ............<... +| 3200: 18 c2 31 74 02 08 02 01 01 07 00 03 32 61 6e 02 ..1t........2an. +| 3216: 06 01 01 04 09 19 88 80 80 80 80 09 03 00 38 00 ..............8. +| 3232: 00 00 14 02 31 6e 02 06 01 01 03 01 01 72 02 06 ....1n.......r.. +| 3248: 01 01 05 04 08 17 88 80 80 80 80 08 03 00 34 00 ..............4. +| 3264: 00 00 12 02 31 62 02 02 04 01 01 69 02 06 01 01 ....1b.....i.... +| 3280: 06 04 06 19 88 80 90 80 80 07 03 00 38 00 00 00 ............8... +| 3296: 14 04 02 31 32 02 02 05 01 01 61 02 08 03 01 01 ...12.....a..... +| 3312: 02 05 06 1b 88 80 80 80 80 06 03 00 3c 00 00 00 ............<... +| 3328: 16 06 30 74 68 65 72 65 02 02 02 00 02 31 31 02 ..0there.....11. +| 3344: 06 01 01 04 0a 15 88 80 80 80 80 05 03 00 30 00 ..............0. +| 3360: 00 00 11 01 01 05 04 30 74 68 65 02 06 01 01 07 .......0the..... +| 3376: 07 1c 88 80 80 80 80 04 03 00 3e 00 00 00 17 01 ..........>..... +| 3392: 01 06 02 30 6e 02 06 01 01 03 01 04 72 6f 77 73 ...0n.......rows +| 3408: 02 06 07 08 1b 88 80 80 80 80 03 03 00 3c 00 00 .............<.. +| 3424: 00 16 08 30 62 65 74 77 65 65 6e 02 02 04 01 02 ...0between..... +| 3440: 69 6e 02 06 04 0c 1a 88 80 80 80 80 02 03 00 3a in.............: +| 3456: 00 00 00 15 04 30 61 6e 64 02 06 01 01 02 02 02 .....0and....... +| 3472: 72 65 02 02 03 04 0a 17 88 80 80 80 80 01 03 00 re.............. +| 3488: 34 00 00 0c 52 02 30 31 02 06 01 01 04 01 01 32 4...R.01.......2 +| 3504: 02 02 05 04 08 08 84 80 80 80 80 12 03 00 16 00 ................ +| 3520: 00 00 05 04 1b 84 80 80 80 80 11 03 00 3c 00 00 .............<.. +| 3536: 00 16 05 34 74 61 62 6c 01 06 00 f1 05 02 03 65 ...4tabl.......e +| 3552: 72 6d 01 02 04 0b 1b 84 80 80 80 80 10 03 00 3c rm.............< +| 3568: 00 00 00 16 05 34 65 61 63 68 01 02 03 01 04 70 .....4each.....p +| 3584: 72 65 73 01 02 05 04 09 1a 84 80 80 80 80 0f 03 res............. +| 3600: 00 3a 00 00 00 15 04 33 74 65 72 01 02 04 02 02 .:.....3ter..... +| 3616: 68 65 01 06 01 01 03 04 08 1b 84 80 80 80 80 0e he.............. +| 3632: 03 00 3c 00 00 00 16 04 33 70 72 65 01 02 05 01 ..<.....3pre.... +| 3648: 03 74 61 62 01 06 01 01 05 04 08 1a 84 80 80 80 .tab............ +| 3664: 80 0d 03 00 3a 00 00 00 15 04 33 66 6f 72 01 02 ....:.....3for.. +| 3680: 02 02 02 74 73 01 06 01 01 04 04 08 1b 84 80 80 ...ts........... +| 3696: 80 80 0c 03 00 3c 00 00 00 16 03 32 74 68 01 06 .....<.....2th.. +| 3712: 01 01 03 00 04 33 65 61 63 01 02 03 04 09 18 74 .....3eac......t +| 3728: 80 80 80 80 0b 03 00 36 00 00 00 13 03 32 74 61 .......6.....2ta +| 3744: 01 06 01 01 05 02 01 65 01 02 04 04 09 19 84 80 .......e........ +| 3760: 80 80 80 0a 03 00 38 00 00 00 14 03 32 69 6e 01 ......8.....2in. +| 3776: 06 01 01 02 11 02 70 62 01 02 05 04 09 18 84 80 ......pb........ +| 3792: 80 80 80 09 03 00 36 00 00 00 13 03 32 66 6f 01 ......6.....2fo. +| 3808: 02 02 02 01 74 01 06 01 01 04 04 07 1b 84 80 80 ....t........... +| 3824: 80 80 08 03 00 3c 0d c0 00 16 12 31 74 01 0a 04 .....<.....1t... +| 3840: 01 01 03 04 00 03 32 65 61 01 02 03 04 0a 17 84 ......2ea....... +| 3856: 80 80 80 80 07 03 00 34 00 00 00 12 02 31 69 01 .......4.....1i. +| 3872: 06 01 01 02 01 01 70 01 02 05 04 08 18 84 80 80 ......p......... +| 3888: 80 80 06 03 00 36 00 00 00 13 02 31 65 01 02 03 .....6.....1e... +| 3904: 01 01 66 01 08 02 5b 01 04 04 06 1b 84 80 80 80 ..f...[......... +| 3920: 80 05 03 00 3c 00 00 00 16 05 30 74 65 72 6d 01 ....<.....0term. +| 3936: 02 04 02 02 68 65 01 06 01 01 03 04 09 14 84 80 ....he.......... +| 3952: 80 80 80 04 03 00 2e 00 00 00 10 06 30 74 61 62 ............0tab +| 3968: 6c 65 01 06 01 01 05 04 15 84 80 80 80 80 03 03 le.............. +| 3984: 00 30 00 00 00 11 01 f8 30 70 72 65 73 65 6e 74 .0......0present +| 4000: 01 02 05 05 1b 84 80 80 80 80 02 03 00 3c 00 00 .............<.. +| 4016: 00 16 04 30 66 74 73 01 06 01 01 04 01 02 69 6e ...0fts.......in +| 4032: 01 06 01 01 04 0a 1a 84 80 80 80 80 01 03 00 3a ...............: +| 4048: 00 00 00 15 05 30 65 61 63 68 01 02 03 01 03 66 .....0each.....f +| 4064: 6f 72 01 02 02 04 09 06 01 03 00 12 03 0b 0f 00 or.............. +| 4080: 00 08 8c 80 80 80 80 11 03 00 16 00 00 00 05 04 ................ +| page 3 offset 8192 +| 0: 0a 00 00 00 32 0e 4f 00 0f fa 0f f1 0f e9 0f e1 ....2.O......... +| 16: 0f d8 0f d1 0f c9 0f c1 0f b9 0f b1 0f a9 0f a0 ................ +| 32: 0f 98 0f 90 0f 87 0f 80 0f 78 0f 71 0f 68 0f 5f .........x.q.h._ +| 48: 0f 56 0f 4d 0f 41 0f 38 0f 2f 0f 26 0f 1d 0f 13 .V.M.A.8./.&.... +| 64: 0f 0a 0f 01 0e f7 0e ee 0e e6 0e dd 0e d6 0e cd ................ +| 80: 0e c3 0e ba 0e b0 0e a8 0e 9f 0e 96 0e 00 00 00 ................ +| 3648: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 08 ................ +| 3664: 04 01 10 01 03 34 74 20 07 04 02 4e 01 03 34 1e .....4t ...N..4. +| 3680: 09 04 01 12 01 03 33 74 68 1c 08 04 01 10 01 03 ......3th....... +| 3696: 33 6e 1a 08 04 01 10 01 03 32 77 18 08 04 01 10 3n.......2w..... +| 3712: 01 03 32 74 16 08 04 01 10 01 03 32 6e 14 07 04 ..2t.......2n... +| 3728: 01 0e 01 03 32 12 08 04 01 10 01 03 31 74 10 08 ....2.......1t.. +| 3744: 04 01 10 01 03 31 6e 0e 07 04 01 0e 01 03 31 0c .....1n.......1. +| 3760: 09 04 01 12 01 03 30 74 68 0a 08 04 01 10 01 03 ......0th....... +| 3776: 30 74 08 19 04 01 12 01 03 30 6e 75 06 08 04 01 0t.......0nu.... +| 3792: 10 01 03 30 6e 04 06 04 01 0c 01 03 02 08 04 01 ...0n........... +| 3808: 10 01 02 34 72 22 07 04 01 0e 01 02 34 20 08 04 ...4r.......4 .. +| 3824: 01 10 01 02 33 72 1e 09 04 01 12 01 02 33 61 72 ....3r.......3ar +| 3840: 1c 08 04 01 10 01 02 32 74 1a 08 04 01 10 01 02 .......2t....... +| 3856: 32 69 18 09 04 01 12 01 02 32 60 82 16 08 04 01 2i.......2`..... +| 3872: 10 01 02 31 74 14 08 04 01 10 01 02 31 6e 12 08 ...1t.......1n.. +| 3888: 04 01 10 01 02 31 62 10 08 04 01 10 01 02 31 32 .....1b.......12 +| 3904: 0e 0b 04 01 16 01 02 30 74 68 65 72 0c 08 04 01 .......0ther.... +| 3920: 10 01 02 30 74 0a 08 04 01 10 01 02 30 6e 08 08 ...0t.......0n.. +| 3936: 04 01 10 01 02 30 62 06 08 04 01 10 01 02 30 61 .....0b.......0a +| 3952: 04 06 04 01 0c 01 02 02 07 04 09 10 01 34 74 22 .............4t. +| 3968: 06 04 09 0e 01 34 20 08 04 09 12 01 33 74 65 1e .....4 .....3te. +| 3984: 07 04 09 10 01 33 70 1c 07 04 09 10 01 33 66 1a .....3p......3f. +| 4000: 08 04 09 12 01 32 74 68 18 07 04 09 10 01 32 74 .....2th......2t +| 4016: 16 07 04 09 10 01 32 69 14 07 04 09 10 01 32 66 ......2i......2f +| 4032: 12 07 04 09 10 01 31 74 10 07 04 09 10 01 31 69 ......1t......1i +| 4048: 0e 06 04 09 0e 01 31 0c 08 04 09 12 01 30 74 65 ......1......0te +| 4064: 0a 07 04 09 10 01 30 74 08 07 04 09 10 01 30 70 ......0t......0p +| 4080: 06 08 04 09 12 01 30 66 74 04 05 04 09 0c 01 02 ......0ft....... +| page 4 offset 12288 +| 0: 0d 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 05 03 03 00 10 ................ +| 4080: 03 05 05 02 03 00 10 04 06 05 01 03 00 10 04 04 ................ +| page 5 offset 16384 +| 0: 0a 00 00 00 02 0f eb 00 0f eb 0f f4 00 00 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 08 03 15 01 70 ...............p +| 4080: 67 83 7a 18 0b 03 1b 01 76 65 72 73 69 6f 6e 04 g.z.....version. +| page 6 offset 20480 +| 0: 0d 00 00 00 03 0f f2 00 0f fc 0f 00 00 00 00 00 ................ +| 4080: 00 00 03 03 02 01 03 03 02 02 01 02 02 01 02 09 ................ +| end crash-c4a4c5492615bd.db +}]} {} + + +do_catchsql_test 83.1 { + SELECT * FROM t1('R*R*R*R*R*R*R*R*') WHERE (a,b)<=(current_date,0 BETWEEN 'a'<>11 AND '') ORDER BY rowid DESC; +} {/.*fts5: corruption found/} + +#------------------------------------------------------------------------- +reset_db +do_test 84.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 53248 pagesize 4096 filename c1a.txt.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 0d .....@ ........ +| 32: 00 00 00 02 00 00 00 01 00 00 00 09 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 96: 00 00 00 00 0d 0f c7 00 07 0d 92 00 0f 8d 0f 36 ...............6 +| 112: 0e cb 0e 6b 0e 0e 0d b6 0d 92 0d 92 00 00 00 00 ...k............ +| 3472: 00 00 22 08 06 17 11 11 01 31 74 61 62 6c 65 74 .........1tablet +| 3488: 32 74 32 0d 43 52 45 41 54 45 20 54 41 42 4c 45 2t2.CREATE TABLE +| 3504: 20 74 32 28 78 29 56 07 06 17 1f 1f 01 7d 74 61 t2(x)V.......ta +| 3520: 62 6c 65 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 blet1_configt1_c +| 3536: 6f 6e 66 69 67 07 43 52 45 41 54 45 20 54 41 42 onfig.CREATE TAB +| 3552: 4c 45 20 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b LE 't1_config'(k +| 3568: 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 PRIMARY KEY, v) +| 3584: 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 5b 06 WITHOUT ROWID[. +| 3600: 07 17 21 21 01 81 01 74 61 62 6c 65 74 31 5f 64 ..!!...tablet1_d +| 3616: 6f 63 73 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 ocsizet1_docsize +| 3632: 06 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 .CREATE TABLE 't +| 3648: 31 5f 64 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 1_docsize'(id IN +| 3664: 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE +| 3680: 59 2c 20 73 7a 20 42 4c 4f 42 29 5e 05 07 17 21 Y, sz BLOB)^...! +| 3696: 21 01 81 07 74 61 62 6c 65 74 31 5f 63 6f 6e 74 !...tablet1_cont +| 3712: 65 6e 74 74 31 5f 63 6f 6e 74 65 6e 74 05 43 52 entt1_content.CR +| 3728: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 EATE TABLE 't1_c +| 3744: 6f 6e 74 65 6e 74 27 28 69 64 20 49 4e 54 45 47 ontent'(id INTEG +| 3760: 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 ER PRIMARY KEY, +| 3776: 63 30 2c 20 63 31 2c 20 63 32 29 69 04 07 17 19 c0, c1, c2)i.... +| 3792: 19 01 81 2d 74 61 62 6c 65 74 31 5f 69 64 78 74 ...-tablet1_idxt +| 3808: 31 5f 69 64 78 04 43 52 45 41 54 45 20 54 41 42 1_idx.CREATE TAB +| 3824: 4c 45 20 27 74 31 5f 69 64 78 27 28 73 65 67 69 LE 't1_idx'(segi +| 3840: 64 2c 20 74 65 72 6d 2c 20 70 67 6e 6f 2c 20 50 d, term, pgno, P +| 3856: 52 49 4d 41 52 59 20 4b 45 59 28 73 65 67 69 64 RIMARY KEY(segid +| 3872: 2c 20 74 65 72 6d 29 29 20 57 49 54 48 4f 55 54 , term)) WITHOUT +| 3888: 20 52 4f 57 49 44 55 03 07 17 1b 1b 01 81 01 74 ROWIDU........t +| 3904: 61 62 6c 65 74 31 5f 64 61 74 61 74 31 5f 64 61 ablet1_datat1_da +| 3920: 74 61 03 43 52 45 41 54 45 20 54 41 42 4c 45 20 ta.CREATE TABLE +| 3936: 27 74 31 5f 64 61 74 61 27 28 69 64 20 49 4e 54 't1_data'(id INT +| 3952: 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 EGER PRIMARY KEY +| 3968: 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 29 38 02 06 , block BLOB)8.. +| 3984: 17 11 11 08 5f 74 61 62 6c 65 74 31 74 31 43 52 ...._tablet1t1CR +| 4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 35 LE t1 USING fts5 +| 4032: 28 61 2c 62 2c 63 29 00 00 00 39 00 00 00 00 00 (a,b,c)...9..... +| page 3 offset 8192 +| 0: 05 00 00 00 02 0f f1 00 00 00 00 0c 0f fb 0f f1 ................ +| 4064: 00 00 0b 01 03 00 1c 81 3a 84 5e 81 3a 81 3a 0a ........:.^.:.:. +| 4080: 0a 00 00 00 0b 84 80 80 80 80 01 00 00 00 0a 0a ................ +| page 4 offset 12288 +| 0: 0a 00 00 00 01 0f fa 00 0f fa 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 05 04 09 0c 01 02 ................ +| page 7 offset 24576 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| page 10 offset 36864 +| 0: 0d 00 00 00 02 0f e2 00 0f e2 0f ef 00 00 00 00 ................ +| 4064: 00 00 0b 01 03 00 1c 81 3a 84 5e 81 3a 81 3a 0f ........:.^.:.:. +| 4080: 0a 03 00 24 00 00 00 00 01 01 02 00 01 01 01 09 ...$............ +| page 11 offset 40960 +| 0: 0d 00 00 00 01 00 22 00 00 22 00 00 00 00 00 00 ................ +| 32: 00 00 9f 56 84 80 80 80 80 01 04 00 bf 30 00 00 ...V.........0.. +| 48: 0f 58 02 30 30 19 02 05 01 02 05 01 02 05 16 02 .X.00........... +| 64: 05 01 02 05 01 02 05 61 02 05 01 02 05 01 02 05 .......a........ +| 80: 13 02 05 01 02 05 01 02 05 0d 02 03 01 02 03 01 ................ +| 96: 02 03 02 09 78 66 66 66 66 66 66 66 65 81 17 02 ....xfffffffe... +| 112: 05 01 02 05 01 02 05 01 01 31 04 02 04 01 02 04 .........1...... +| 128: 01 02 04 01 02 05 01 02 05 01 02 05 0d 02 06 01 ................ +| 144: 02 06 01 02 06 02 01 30 79 02 04 01 02 04 01 02 .......0y....... +| 160: 04 03 02 30 30 2b 02 05 01 02 05 01 02 05 58 02 ...00+........X. +| 176: 05 01 02 05 01 02 05 01 02 05 01 02 05 01 02 05 ................ +| 192: 16 02 05 01 02 05 01 02 05 05 06 30 30 30 30 30 ...........00000 +| 208: 30 81 0b 02 04 01 02 04 01 02 04 10 02 05 01 02 0............... +| 224: 05 01 02 05 03 02 32 34 76 02 05 01 02 05 01 02 ......24v....... +| 240: 05 02 01 38 07 02 04 01 02 04 01 02 04 01 01 32 ...8...........2 +| 256: 28 02 04 01 02 04 01 02 04 04 02 05 01 02 05 01 (............... +| 272: 02 05 02 01 30 1f 02 05 01 02 05 01 02 05 03 02 ....0........... +| 288: 30 30 10 02 05 01 02 05 01 02 05 6a 02 04 01 02 00.........j.... +| 304: 04 01 02 04 02 08 35 30 30 30 30 30 30 30 81 26 ......50000000.& +| 320: 02 05 01 02 05 01 02 05 01 01 33 07 02 06 01 02 ..........3..... +| 336: 06 01 02 06 81 2c 02 04 01 02 04 01 02 04 02 04 .....,.......... +| 352: 32 37 36 36 81 23 02 05 01 02 05 01 02 05 01 01 2766.#.......... +| 368: 34 13 02 05 01 02 05 01 02 05 02 03 30 39 36 1c 4...........096. +| 384: 02 05 01 02 05 01 02 05 07 02 05 01 02 05 01 02 ................ +| 400: 05 01 03 35 30 30 7f 02 05 01 02 05 01 02 05 04 ...500.......... +| 416: 02 30 30 81 0e 02 06 01 02 06 01 02 06 06 03 30 .00............0 +| 432: 30 30 81 11 02 04 01 02 04 01 02 04 01 05 36 35 00............65 +| 448: 35 33 36 81 1a 02 05 01 02 05 01 02 05 01 04 38 536............8 +| 464: 31 39 32 81 02 02 06 01 02 06 01 02 06 01 05 61 192............a +| 480: 6c 6c 6f 77 01 02 02 01 02 02 01 02 02 02 02 72 llow...........r +| 496: 67 81 08 02 04 01 02 04 01 02 04 02 05 74 6f 6d g............tom +| 512: 69 63 04 02 02 01 02 02 01 02 02 03 06 74 61 63 ic...........tac +| 528: 68 65 64 79 02 03 01 02 03 01 02 03 02 0d 75 74 hedy..........ut +| 544: 6f 63 68 65 63 6b 70 6f 69 6e 74 2b 02 04 01 02 ocheckpoint+.... +| 560: 04 01 02 04 05 06 76 61 63 75 75 6d 0d 02 03 01 ......vacuum.... +| 576: 02 03 01 02 03 01 06 62 69 6e 61 72 79 03 06 01 .......binary... +| 592: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 608: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 ................ +| 624: 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 03 ................ +| 640: 06 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 ................ +| 656: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 672: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 688: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 ................ +| 704: 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 03 ................ +| 720: 06 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 ................ +| 736: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 752: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 768: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 ................ +| 784: 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 03 ................ +| 800: 06 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 ................ +| 816: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 832: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 848: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 ................ +| 864: 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 03 ................ +| 880: 06 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 ................ +| 896: 01 02 02 02 07 79 74 65 63 6f 64 65 37 02 03 01 .....ytecode7... +| 912: 02 03 01 02 03 01 05 63 61 63 68 65 10 02 03 01 .......cache.... +| 928: 02 03 01 01 03 02 04 6c 61 6e 67 07 02 03 01 02 .......lang..... +| 944: 03 01 02 03 02 05 6f 6c 75 6d 6e 7c 02 03 01 02 ......olumn|.... +| 960: 03 01 02 03 03 06 6d 6d 65 6e 74 73 43 02 04 01 ......mmentsC... +| 976: 02 04 01 02 04 04 05 70 69 6c 65 72 07 02 02 01 .......piler.... +| 992: 02 02 01 02 02 05 04 6f 75 6e 64 7f 02 03 01 02 .......ound..... +| 1008: 03 01 02 03 03 03 75 6e 74 81 17 02 04 01 02 04 ......unt....... +| 1024: 01 02 04 02 05 75 72 73 6f 72 3a 02 03 01 02 03 .....ursor:..... +| 1040: 01 02 03 01 06 64 62 70 61 67 65 3d 02 03 01 02 .....dbpage=.... +| 1056: 03 01 02 03 03 04 73 74 61 74 40 02 03 01 02 03 ......stat@..... +| 1072: 01 02 03 02 04 65 62 75 67 0a 02 02 01 02 02 01 .....ebug....... +| 1088: 02 02 03 05 66 61 75 6c 74 0d 02 02 01 02 02 01 ....fault....... +| 1104: 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 ................ +| 1120: 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 ................ +| 1136: 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 ................ +| 1152: 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 ................ +| 1168: 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 ................ +| 1184: 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 ................ +| 1200: 02 02 01 02 02 01 02 02 01 02 02 01 02 02 4f 02 ..............O. +| 1216: 03 01 02 03 01 02 03 03 03 70 74 68 81 05 02 04 .........pth.... +| 1232: 01 02 04 01 02 04 19 02 04 01 02 04 01 02 04 02 ................ +| 1248: 05 69 72 65 63 74 34 02 02 01 02 02 01 02 02 01 .irect4......... +| 1264: 06 65 6e 61 62 6c 65 37 02 02 01 02 02 01 02 02 .enable7........ +| 1280: 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 ................ +| 1296: 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 ................ +| 1312: 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 ................ +| 1328: 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 ................ +| 1344: 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 ................ +| 1360: 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 ................ +| 1376: 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 ................ +| 1392: 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 ................ +| 1408: 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 ................ +| 1424: 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 ................ +| 1440: 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 ................ +| 1456: 02 01 02 02 02 06 78 70 6c 61 69 6e 43 02 03 01 ......xplainC... +| 1472: 02 03 01 02 03 04 01 72 81 05 02 03 01 02 03 01 .......r........ +| 1488: 02 03 03 07 74 65 6e 73 69 6f 6e 81 2f 02 04 01 ....tension./... +| 1504: 02 04 01 02 04 01 04 66 69 6c 65 13 02 03 01 02 .......file..... +| 1520: 03 01 02 03 02 05 6f 72 6d 61 74 13 02 04 01 02 ......ormat..... +| 1536: 04 01 02 04 02 03 74 73 33 46 02 03 01 02 03 01 ......ts3F...... +| 1552: 02 03 01 02 03 01 02 03 01 02 03 04 01 34 4c 02 .............4L. +| 1568: 03 01 02 03 01 02 03 04 01 35 4f 02 03 01 02 03 .........5O..... +| 1584: 01 02 03 02 03 75 6e 63 5e 02 05 01 02 05 01 02 .....unc^....... +| 1600: 05 05 04 74 69 6f 6e 73 02 05 01 02 05 01 02 05 ...tions........ +| 1616: 13 02 03 01 02 03 01 02 03 09 01 73 55 02 04 01 ...........sU... +| 1632: 02 04 01 02 04 01 07 67 65 6f 70 6f 6c 79 52 02 .......geopolyR. +| 1648: 03 01 02 03 01 02 03 01 05 68 69 6e 74 73 3a 02 .........hints:. +| 1664: 04 01 02 04 01 02 04 02 03 6f 6f 6b 61 02 04 01 .........ooka... +| 1680: 02 04 01 02 04 01 02 69 6e 01 02 04 01 02 04 01 .......in....... +| 1696: 02 04 03 04 69 74 73 7a 1f 02 04 01 02 04 01 02 ....itsz........ +| 1712: 04 03 08 74 72 69 6e 73 69 63 73 04 02 03 01 02 ...trinsics..... +| 1728: 03 01 02 03 01 07 6a 6f 75 72 6e 61 6c 16 02 03 ......journal... +| 1744: 01 02 03 01 02 03 01 06 6c 65 6e 67 74 68 81 0b ........length.. +| 1760: 02 03 01 02 03 01 02 03 01 02 05 01 02 05 01 02 ................ +| 1776: 05 0d 02 04 01 02 04 01 02 04 02 03 69 6b 65 81 ............ike. +| 1792: 0e 02 03 01 02 03 01 02 03 03 03 6d 69 74 16 02 ...........mit.. +| 1808: 05 01 02 05 01 02 05 5e 02 04 01 02 04 01 02 04 .......^........ +| 1824: 02 03 6f 61 64 81 2f 02 03 01 02 03 01 02 03 01 ..oad./......... +| 1840: 06 6d 61 6c 6c 6f 63 76 02 02 01 02 02 01 02 02 .mallocv........ +| 1856: 3a 02 03 01 02 03 01 02 03 03 02 74 68 55 02 03 :..........thU.. +| 1872: 01 02 03 01 02 03 03 01 78 79 02 02 01 02 02 01 ........xy...... +| 1888: 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 ................ +| 1904: 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 ................ +| 1920: 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 ................ +| 1936: 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 ................ +| 1952: 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 ................ +| 1968: 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 ................ +| 1984: 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 ................ +| 2000: 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 ................ +| 2016: 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 ................ +| 2032: 02 02 02 05 65 6d 6f 72 79 81 11 02 03 01 02 03 ....emory....... +| 2048: 01 02 03 04 04 73 79 73 35 58 02 03 01 02 03 01 .....sys5X...... +| 2064: 02 03 02 03 6d 61 70 19 02 03 01 02 03 01 02 03 ....map......... +| 2080: 79 02 03 01 02 03 01 02 03 02 04 75 74 65 78 81 y..........utex. +| 2096: 2c 02 02 01 02 02 01 02 02 01 06 6e 6f 63 61 73 ,..........nocas +| 2112: 65 02 06 01 02 02 03 06 01 02 02 03 06 01 02 02 e............... +| 2128: 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 03 ................ +| 2144: 06 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 ................ +| 2160: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 2176: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 2192: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 ................ +| 2208: 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 03 ................ +| 2224: 06 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 ................ +| 2240: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 2256: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 2272: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 ................ +| 2288: 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 03 ................ +| 2304: 06 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 ................ +| 2320: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 2336: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 2352: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 ................ +| 2368: 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 03 ................ +| 2384: 06 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 ................ +| 2400: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 2416: 02 02 03 06 01 02 02 03 07 72 6d 61 6c 69 7a 65 .........rmalize +| 2432: 5b 02 03 01 02 03 01 02 03 02 05 75 6d 62 65 72 [..........umber +| 2448: 81 23 02 04 01 02 04 01 02 04 01 06 6f 66 66 73 .#..........offs +| 2464: 65 74 5e 02 03 01 02 03 01 02 03 02 03 6d 69 74 et^..........mit +| 2480: 81 2c 02 03 01 02 03 01 02 03 01 02 02 01 02 02 .,.............. +| 2496: 01 02 02 02 01 70 81 26 02 04 01 02 04 01 02 04 .....p.&........ +| 2512: 02 07 76 65 72 66 6c 6f 77 34 02 03 01 02 03 01 ..verflow4...... +| 2528: 02 03 01 04 70 61 67 65 1c 02 03 01 02 03 01 02 ....page........ +| 2544: 03 64 02 04 01 02 04 01 02 04 13 02 03 01 02 03 .d.............. +| 2560: 01 02 03 01 02 03 01 02 03 01 02 03 03 09 72 65 ..............re +| 2576: 6e 74 68 65 73 69 73 49 02 04 01 02 04 01 02 04 nthesisI........ +| 2592: 03 05 74 74 65 72 6e 81 0e 02 04 01 02 04 01 02 ..ttern......... +| 2608: 04 02 05 63 61 63 68 65 1f 02 03 01 02 03 01 02 ...cache........ +| 2624: 03 02 08 72 65 75 70 64 61 74 65 61 02 03 01 02 ...reupdatea.... +| 2640: 03 01 02 03 01 04 72 65 61 64 34 02 04 01 02 04 ......read4..... +| 2656: 01 02 04 03 07 63 75 72 73 69 76 65 22 02 03 01 .....cursive.... +| 2672: 02 03 01 02 03 02 04 6f 77 69 64 01 02 03 01 02 .......owid..... +| 2688: 03 01 02 03 02 04 74 72 65 65 64 02 03 01 02 03 ......treed..... +| 2704: 01 02 03 04 02 69 6d 01 06 01 02 02 03 06 01 02 .....im......... +| 2720: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 ................ +| 2736: 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 03 ................ +| 2752: 06 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 ................ +| 2768: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 2784: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 2800: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 ................ +| 2816: 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 03 ................ +| 2832: 06 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 ................ +| 2848: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 2864: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 2880: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 ................ +| 2896: 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 03 ................ +| 2912: 06 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 ................ +| 2928: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 2944: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 2960: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 ................ +| 2976: 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 03 ................ +| 2992: 06 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 ................ +| 3008: 01 02 02 03 06 01 02 02 03 06 01 02 02 01 0a 73 ...............s +| 3024: 63 61 6e 73 74 61 74 75 73 70 02 04 01 02 04 01 canstatusp...... +| 3040: 02 04 02 05 65 63 74 6f 72 25 02 03 01 02 03 01 ....ector%...... +| 3056: 02 03 03 04 6c 65 63 74 7f 02 04 01 02 04 01 02 ....lect........ +| 3072: 04 03 05 73 73 69 6f 6e 67 02 03 01 02 03 01 02 ...ssiong....... +| 3088: 03 02 03 69 7a 65 10 02 04 01 02 04 01 02 04 04 ...ize.......... +| 3104: 02 04 01 02 04 01 02 04 01 02 04 01 02 04 01 02 ................ +| 3120: 04 01 02 04 01 02 04 01 02 04 07 02 04 01 02 04 ................ +| 3136: 01 02 04 5b 02 05 01 02 05 01 02 05 10 02 04 01 ...[............ +| 3152: 02 04 01 02 04 04 02 04 01 02 04 01 02 04 02 03 ................ +| 3168: 6f 66 74 76 02 03 01 02 03 01 02 03 02 02 71 6c oftv..........ql +| 3184: 5e 02 04 01 02 04 01 02 04 13 02 04 01 02 04 01 ^............... +| 3200: 02 04 28 02 03 01 02 03 01 02 03 02 04 74 61 74 ..(..........tat +| 3216: 34 6a 02 03 01 02 03 01 02 03 03 02 6d 74 70 02 4j..........mtp. +| 3232: 03 01 02 03 01 02 03 05 04 76 74 61 62 6d 02 03 .........vtabm.. +| 3248: 01 02 03 01 02 03 03 03 6f 72 65 81 35 02 03 01 ........ore.5... +| 3264: 02 03 01 02 03 02 0a 79 6e 63 68 72 6f 6e 6f 75 .......ynchronou +| 3280: 73 28 02 03 01 02 03 01 02 03 04 02 04 01 02 04 s(.............. +| 3296: 01 02 04 03 04 73 74 65 6d 81 32 02 02 01 02 02 .....stem.2..... +| 3312: 01 02 02 01 04 74 65 6d 70 81 35 02 02 01 02 02 .....temp.5..... +| 3328: 01 02 02 02 06 68 72 65 61 64 73 31 02 04 01 02 .....hreads1.... +| 3344: 04 01 02 04 76 02 04 01 02 04 01 02 04 08 03 61 ....v..........a +| 3360: 66 65 81 38 02 02 01 02 02 01 02 02 02 06 72 69 fe.8..........ri +| 3376: 67 67 65 72 81 20 02 03 01 02 03 01 02 03 08 01 gger. .......... +| 3392: 73 22 02 04 01 02 04 01 02 04 01 07 75 6e 6b 6e s...........unkn +| 3408: 6f 77 6e 73 02 03 01 02 03 01 02 03 01 08 76 61 owns..........va +| 3424: 72 69 61 62 6c 65 81 23 02 03 01 02 03 01 02 03 riable.#........ +| 3440: 02 03 64 62 65 81 26 02 03 01 02 03 01 02 03 02 ..dbe.&......... +| 3456: 03 69 65 77 01 02 05 01 02 05 01 02 05 02 03 74 .iew...........t +| 3472: 61 62 37 02 04 01 02 04 01 02 04 04 02 04 01 02 ab7............. +| 3488: 04 01 02 04 01 02 04 01 02 04 01 02 04 01 03 77 ...............w +| 3504: 61 6c 2b 02 03 01 02 03 01 02 03 01 02 03 01 02 al+............. +| 3520: 03 01 02 03 02 05 6f 72 6b 65 72 31 02 03 01 02 ......orker1.... +| 3536: 03 01 02 03 76 02 03 01 02 03 01 02 03 01 01 78 ....v..........x +| 3552: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 3568: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 3584: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 ................ +| 3600: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 ................ +| 3616: 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 ................ +| 3632: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 3648: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 3664: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 ................ +| 3680: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 ................ +| 3696: 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 ................ +| 3712: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 3728: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 3744: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 ................ +| 3760: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 ................ +| 3776: 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 ................ +| 3792: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 3808: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 3824: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 ................ +| 3840: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 ................ +| 3856: 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 ................ +| 3872: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 3888: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 3904: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 ................ +| 3920: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 ................ +| 3936: 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 ................ +| 3952: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 3968: 06 01 01 02 01 06 04 30 15 1e 0c 28 1b 0d 0c 15 .......0...(.... +| 3984: 0c 16 14 16 10 0c 17 0e 0e 0f 11 10 10 0e 10 11 ................ +| 4000: 18 11 82 3e 12 10 0f 10 11 10 0f 0f 10 11 0f 0f ...>............ +| 4016: 81 05 18 10 81 45 11 0d 13 0f 10 17 0c 0c 0e 18 .....E.......... +| 4032: 0c 12 10 0e 0d 0f 13 12 24 0f 17 0f 1a 0d 81 1c ........$....... +| 4048: 11 0f 17 10 82 3e 12 11 11 18 0d 12 2a 14 11 10 .....>......*... +| 4064: 13 0f 12 0f 0f 82 3a 15 10 0f 10 4d 0e 1f 0f 0d ......:....M.... +| 4080: 0f 0f 1e 10 10 1a 0f 12 0c 12 14 0f 0e 20 17 19 ............. .. +| page 12 offset 45056 +| 0: 0d 00 00 00 01 0d f4 00 0d f4 00 00 00 00 00 00 ................ +| 3568: 00 00 00 00 84 04 84 80 80 80 80 02 04 00 88 0c ................ +| 3584: 00 07 02 00 01 01 02 56 06 01 01 02 01 06 01 01 .......V........ +| 3600: 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 ................ +| 3616: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 3632: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 3648: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 ................ +| 3664: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 ................ +| 3680: 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 ................ +| 3696: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 3712: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 3728: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 ................ +| 3744: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 ................ +| 3760: 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 ................ +| 3776: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 3792: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 3808: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 ................ +| 3824: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 ................ +| 3840: 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 ................ +| 3856: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 3872: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 3888: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 ................ +| 3904: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 ................ +| 3920: 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 ................ +| 3936: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 3952: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 3968: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 ................ +| 3984: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 ................ +| 4000: 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 ................ +| 4016: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 4032: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 4048: 52 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 R............... +| 4064: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 ................ +| 4080: 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 ................ +| end c1a.txt.db +}]} {} + +do_catchsql_test 84.1 { + SELECT * FROM t1('R*R*x') ORDER BY rowid DESC; +} {1 {fts5: corruption found reading blob 137438953475 from table "t1"}} + sqlite3_fts5_may_be_corrupt 0 finish_test - diff --git a/ext/fts5/test/fts5corrupt4.test b/ext/fts5/test/fts5corrupt4.test new file mode 100644 index 0000000000..0505250c2f --- /dev/null +++ b/ext/fts5/test/fts5corrupt4.test @@ -0,0 +1,63 @@ +# 2019 May 16 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# + +source [file join [file dirname [info script]] fts5_common.tcl] +set testprefix fts5corrupt4 + +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. +ifcapable !fts5 { + finish_test + return +} +sqlite3_fts5_may_be_corrupt 1 + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE ttt USING fts5(a, b); + INSERT INTO ttt + VALUES('e ee eee e ee eee e ee eee', 'eee ee e e e ee eee ee ee'); + INSERT INTO ttt SELECT a||a, b||b FROM ttt; + INSERT INTO ttt SELECT a||a, b||b FROM ttt; +} + +expr srand(1) + +proc mutate {blob i} { + set o [expr {$i % [string length $blob]}] + set a [string range $blob 0 $o-1] + set b [string range $blob $o+1 end] + set v [expr int(rand()*255) - 127] + return "$a[binary format c $v]$b" +} +db func mutate mutate + +for {set j 1000} {$j <= 5000} {incr j 1000} { + do_test 1.$j { + for {set i 0} {$i < 1000} {incr i} { + execsql { + BEGIN; + UPDATE ttt_data SET block = mutate(block, $i) WHERE id>10; + } + foreach sql { + {SELECT snippet(ttt, -1, '.', '..', '[', ']'), * FROM ttt('e*')} + {SELECT snippet(ttt, -1, '.', '..', '[', ']'), * FROM ttt('e* NOT ee*')} + } { + catch { execsql $sql } + } + execsql ROLLBACK + } + } {} +} + +sqlite3_fts5_may_be_corrupt 0 +finish_test + diff --git a/ext/fts5/test/fts5corrupt5.test b/ext/fts5/test/fts5corrupt5.test new file mode 100644 index 0000000000..4b21a9ff74 --- /dev/null +++ b/ext/fts5/test/fts5corrupt5.test @@ -0,0 +1,1923 @@ +# 2015 Apr 24 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# This file tests that FTS5 handles corrupt databases (i.e. internal +# inconsistencies in the backing tables) correctly. In this case +# "correctly" means without crashing. +# + +source [file join [file dirname [info script]] fts5_common.tcl] +set testprefix fts5corrupt5 + +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. +ifcapable !fts5 { + finish_test + return +} +sqlite3_fts5_may_be_corrupt 1 +database_may_be_corrupt + +#------------------------------------------------------------------------- +# dbsqlfuzz crash-0f47112aa7520cf08c6a835a88fdff8c2a32a188 +# +reset_db +do_test 1.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 24576 pagesize 4096 filename crash-0f47112aa7520c.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 00 .....@ ........ +| 96: 00 00 00 00 0d 00 00 00 06 0e 0f 00 0f aa 0f 53 ...............S +| 112: 0e e8 0e 8b 0e 33 0e 0f 00 00 00 00 00 00 00 00 .....3.......... +| 3584: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 22 ................ +| 3600: 06 06 17 11 11 01 31 74 61 62 6c 65 62 62 62 62 ......1tablebbbb +| 3616: 06 43 52 45 41 54 45 20 54 41 42 4c 45 20 62 62 .CREATE TABLE bb +| 3632: 28 61 29 56 05 06 17 1f 1f 01 7d 74 61 62 6c 65 (a)V.......table +| 3648: 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 6f 6e 66 t1_configt1_conf +| 3664: 69 67 05 43 52 45 41 54 45 20 54 41 42 4c 45 20 ig.CREATE TABLE +| 3680: 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b 20 50 52 't1_config'(k PR +| 3696: 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 20 57 49 IMARY KEY, v) WI +| 3712: 54 48 4f 55 54 20 52 4f 57 49 44 5b 04 07 17 21 THOUT ROWID[...! +| 3728: 21 01 81 01 74 61 62 6c 65 74 31 5f 64 6f 63 73 !...tablet1_docs +| 3744: 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 04 43 52 izet1_docsize.CR +| 3760: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 64 EATE TABLE 't1_d +| 3776: 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 54 45 47 ocsize'(id INTEG +| 3792: 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 ER PRIMARY KEY, +| 3808: 73 7a 20 42 4c 4f 42 29 69 03 07 17 19 19 01 81 sz BLOB)i....... +| 3824: 2d 74 61 62 6c 65 74 31 5f 69 64 78 74 31 5f 69 -tablet1_idxt1_i +| 3840: 64 78 03 43 52 45 41 54 45 20 54 41 42 4c 45 20 dx.CREATE TABLE +| 3856: 27 74 31 5f 69 64 78 27 28 73 65 67 69 64 2c 20 't1_idx'(segid, +| 3872: 74 65 72 6d 2c 20 70 67 6e 6f 2c 20 50 52 49 4d term, pgno, PRIM +| 3888: 41 52 59 20 4b 45 59 28 73 65 67 69 64 2c 20 74 ARY KEY(segid, t +| 3904: 65 72 6d 29 29 20 57 49 54 48 4f 55 54 20 52 4f erm)) WITHOUT RO +| 3920: 57 49 44 55 02 07 17 1b 1b 01 81 01 74 61 62 6c WIDU........tabl +| 3936: 65 74 31 5f 64 61 74 61 74 31 5f 64 61 74 61 02 et1_datat1_data. +| 3952: 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 CREATE TABLE 't1 +| 3968: 5f 64 61 74 61 27 28 69 64 20 49 4e 54 45 47 45 _data'(id INTEGE +| 3984: 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 62 R PRIMARY KEY, b +| 4000: 6c 6f 63 6b 20 42 4c 4f 42 29 54 01 07 17 11 11 lock BLOB)T..... +| 4016: 08 81 15 74 61 62 6c 65 74 31 74 31 43 52 45 41 ...tablet1t1CREA +| 4032: 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45 TE VIRTUAL TABLE +| 4048: 20 74 31 20 55 53 49 4e 47 20 66 74 73 35 28 61 t1 USING fts5(a +| 4064: 2c 62 2c 70 72 65 66 69 78 3d 22 31 2c 32 2c 33 ,b,prefix=.1,2,3 +| 4080: 2c 34 22 2c 20 63 6f 6e 74 65 6e 74 3d 22 22 29 ,4., content=..) +| page 2 offset 4096 +| 0: 0d 0b 6a 00 37 09 4c 02 0f e7 09 4c 0f c6 0f a4 ..j.7.L....L.... +| 16: 0f 88 0f 6d 0f 4b 0f 2c 0f 0e 0e ec 0e cd 0e ad ...m.K.,........ +| 32: 0e 8e 0e 6c 0e 4b 0e 29 0e 08 0d e6 0d c4 0d b5 ...l.K.)........ +| 48: 0d 97 0d 76 0d 54 0d 31 0d 15 0c f3 0c d3 0c b5 ...v.T.1........ +| 64: 0c 95 0c 73 0c 54 0c 32 0c 10 0b ee 0b cc 0b b0 ...s.T.2........ +| 80: 0b 8d 0b 7e 0b 48 0b 2e 0b 0b 0a ef 00 00 00 00 ...~.H.......... +| 2368: 00 00 00 00 00 00 00 00 00 00 00 00 15 0a 03 00 ................ +| 2384: 30 00 00 00 01 01 03 35 00 03 01 11 12 02 01 12 0......5........ +| 2400: 03 01 11 1c 8c 80 80 80 80 10 03 00 3e 00 00 00 ............>... +| 2416: 17 01 05 05 34 74 61 62 6c 03 02 03 01 04 77 68 ....4tabl.....wh +| 2432: 65 72 03 02 06 09 1b 8c 80 80 80 80 0f 03 00 3c er.............< +| 2448: 00 00 00 16 05 34 66 74 73 34 03 02 02 01 04 6e .....4fts4.....n +| 2464: 75 6d 62 03 06 01 04 09 1b 8c 80 80 80 80 0e 03 umb............. +| 2480: 00 3c 00 00 00 16 04 33 74 68 65 03 06 01 01 04 .<.....3the..... +| 2496: 01 03 77 68 65 03 02 04 04 0a 1b 8c 80 80 80 80 ..whe........... +| 2512: 0d 03 00 3c 00 00 00 16 04 33 6e 75 6d 03 06 01 ...<.....3num... +| 2528: 01 05 01 03 74 61 62 03 02 03 04 0a 19 8c 80 80 ....tab......... +| 2544: 80 80 0c 03 00 38 00 00 00 14 03 32 77 68 03 02 .....8.....2wh.. +| 2560: 04 00 04 33 66 74 73 03 02 02 04 07 18 8c 80 80 ...3fts......... +| 2576: 80 80 0b 03 00 36 00 00 00 13 03 32 74 61 03 02 .....6.....2ta.. +| 2592: 03 02 01 68 03 06 01 01 04 04 07 1b 8c 80 80 80 ...h............ +| 2608: 80 0a 03 00 3c 00 00 00 16 03 32 6e 75 03 06 01 ....<.....2nu... +| 2624: 01 05 01 02 6f 66 03 06 01 01 06 04 09 19 8c 80 ....of.......... +| 2640: 80 80 80 09 03 00 38 00 00 00 14 03 32 66 74 03 ......8.....2ft. +| 2656: 02 02 01 02 69 73 03 06 01 01 03 04 07 18 8c 80 ....is.......... +| 2672: 80 80 80 08 03 00 36 00 00 00 13 02 31 74 03 08 ......6.....1t.. +| 2688: 03 01 01 04 01 01 77 03 02 04 04 09 1a 8c 80 80 ......w......... +| 2704: 80 80 07 03 00 3a 00 00 00 15 02 31 6e 03 08 01 .....:.....1n... +| 2720: 01 02 05 01 00 6f 03 06 01 01 06 04 09 18 8c 80 .....o.......... +| 2736: 80 80 80 06 03 00 36 00 00 00 03 04 02 31 66 03 ......6......1f. +| 2752: 02 02 01 01 69 03 06 01 01 03 04 f6 1c 8c 80 80 ....i........... +| 2768: 80 80 05 03 00 3e 00 00 00 17 04 30 74 68 65 03 .....>.....0the. +| 2784: f6 01 01 04 01 05 77 68 65 72 65 03 02 04 0a 15 ......where..... +| 2800: 8c 80 80 80 80 04 03 00 30 00 00 00 11 01 01 06 ........0....... +| 2816: 06 30 74 61 62 6c 65 0f 42 03 07 1c 8c 81 80 80 .0table.B....... +| 2832: 80 03 03 00 3e 00 00 00 17 07 30 6e 75 6d 62 65 ....>.....0numbe +| 2848: 72 03 06 01 01 05 01 02 6f 66 03 06 04 0d 13 8c r.......of...... +| 2864: 80 80 80 80 02 03 00 2c 00 00 00 0f 01 01 03 02 .......,........ +| 2880: 30 6e 03 06 01 01 02 07 1b 8c 80 80 80 80 01 03 0n.............. +| 2896: 00 3c 00 00 00 16 08 30 66 74 73 34 61 75 78 03 .<.....0fts4aux. +| 2912: 02 02 01 02 69 73 03 06 04 0c 00 00 00 14 2a 00 ....is........*. +| 2928: 00 00 01 01 02 24 00 02 01 01 12 02 01 12 08 88 .....$.......... +| 2944: 80 80 80 80 12 03 00 16 00 00 00 05 02 1c 88 80 ................ +| 2960: 80 80 80 11 03 00 3e 00 00 00 17 05 34 72 6f 77 ......>.....4row +| 2976: 73 02 06 01 01 05 01 04 74 68 65 72 02 02 04 0b s.......ther.... +| 2992: 15 88 80 80 80 80 10 03 00 30 00 00 00 11 02 01 .........0...... +| 3008: 01 07 05 34 62 65 74 77 02 02 04 08 1b 88 80 80 ...4betw........ +| 3024: 80 80 0f 03 00 3c 00 00 00 16 04 04 33 72 6f 77 .....<......3row +| 3040: 02 06 01 01 05 01 03 74 68 65 02 08 05 0a 1b 88 .......the...... +| 3056: 80 80 80 80 0e 03 00 3c 00 00 00 16 01 01 02 04 .......<........ +| 3072: 33 61 72 65 02 02 03 01 03 62 65 74 02 02 07 08 3are.....bet.... +| 3088: 1b 88 80 80 80 80 0d 03 00 3c 00 00 00 16 13 32 .........<.....2 +| 3104: 74 68 02 08 02 01 01 07 00 04 33 61 6e 64 02 06 th........3and.. +| 3120: 04 0a 1b 88 80 80 80 80 0c 03 00 3c 00 00 00 16 ...........<.... +| 3136: 03 32 69 6e 02 06 01 01 06 01 02 72 6f 02 06 01 .2in.......ro... +| 3152: 01 05 04 09 18 88 80 80 80 80 0b 03 00 36 00 00 .............6.. +| 3168: 00 13 02 03 32 61 72 02 02 03 01 02 62 65 02 02 ....2ar.....be.. +| 3184: 04 05 07 1b 88 80 80 80 80 0a 03 00 3c 00 9e 00 ............<... +| 3200: 16 02 31 74 02 08 02 01 01 07 00 03 32 61 6e 02 ..1t........2an. +| 3216: 06 01 01 04 09 19 88 80 80 80 80 09 03 00 38 00 ..............8. +| 3232: 00 00 14 02 31 6e 02 06 01 01 03 01 01 72 02 06 ....1n.......r.. +| 3248: 01 01 05 04 08 17 88 80 80 80 80 08 03 00 34 00 ..............4. +| 3264: 00 00 12 02 31 62 02 02 04 01 01 69 02 06 01 01 ....1b.....i.... +| 3280: 06 04 06 19 88 80 80 80 80 07 03 00 38 00 00 00 ............8... +| 3296: 14 04 02 31 32 02 02 05 01 01 61 02 08 03 01 01 ...12.....a..... +| 3312: 02 05 06 1b 88 80 80 80 80 06 03 00 3c 00 00 00 ............<... +| 3328: 16 06 30 74 68 65 72 65 02 02 02 00 02 31 31 02 ..0there.....11. +| 3344: 06 01 01 04 0a 15 88 80 80 80 80 05 03 00 30 00 ..............0. +| 3360: 00 00 11 01 01 05 04 30 74 68 65 02 06 01 01 07 .......0the..... +| 3376: 07 1c 88 80 80 80 80 04 03 00 3e 00 00 00 17 01 ..........>..... +| 3392: 01 06 02 30 6e 02 06 01 01 03 01 04 72 6f 77 73 ...0n.......rows +| 3408: 02 06 07 08 1b 88 80 80 80 80 03 03 00 3c 00 00 .............<.. +| 3424: 00 16 08 30 62 65 74 77 65 65 6e 02 02 04 01 02 ...0between..... +| 3440: 69 6e 02 06 04 0c 1a 88 80 80 80 80 02 03 00 3a in.............: +| 3456: 00 00 00 15 04 30 61 6e 64 02 06 01 01 02 02 02 .....0and....... +| 3472: 72 65 02 02 03 04 0a 17 88 80 80 80 80 01 03 00 re.............. +| 3488: 34 00 00 00 12 02 30 31 02 06 01 01 04 01 01 32 4.....01.......2 +| 3504: 02 02 05 04 08 08 84 80 80 80 80 12 03 00 16 00 ................ +| 3520: 00 00 05 04 1b 84 80 80 80 80 11 03 00 3c 00 00 .............<.. +| 3536: 00 16 05 34 74 61 62 6c 01 06 01 01 05 02 03 65 ...4tabl.......e +| 3552: 72 6d 01 02 04 0b 1b 84 80 80 80 80 10 03 00 3c rm.............< +| 3568: 00 00 00 16 05 34 65 61 63 68 01 02 03 01 04 70 .....4each.....p +| 3584: 72 65 73 01 02 05 04 08 1a 84 80 80 80 80 0f 03 res............. +| 3600: 00 3a 00 00 00 15 04 33 74 65 72 01 02 04 02 02 .:.....3ter..... +| 3616: 68 65 01 06 01 01 03 04 08 1b 84 80 80 80 80 0e he.............. +| 3632: 03 00 3c 00 00 00 16 04 33 80 72 65 01 02 05 01 ..<.....3.re.... +| 3648: 03 74 61 62 01 06 01 01 05 04 08 1a 84 80 80 80 .tab............ +| 3664: 80 0d 03 00 3a 00 00 00 15 04 33 66 6f 72 01 02 ....:.....3for.. +| 3680: 02 02 02 74 73 01 06 01 01 04 04 08 1b 84 80 80 ...ts........... +| 3696: 80 80 0c 03 00 3c 00 00 00 17 03 32 74 68 01 06 .....<.....2th.. +| 3712: 01 01 03 00 04 33 65 61 63 01 02 03 04 09 18 84 .....3eac....... +| 3728: 80 80 80 80 0b 03 00 36 00 00 00 13 03 32 74 61 .......6.....2ta +| 3744: 01 06 01 01 05 02 01 65 01 02 04 04 09 19 84 80 .......e........ +| 3760: 80 80 80 0a 03 00 38 00 00 00 14 03 32 69 6e 01 ......8.....2in. +| 3776: 06 01 01 02 01 02 70 72 01 02 05 04 09 18 84 80 ......pr........ +| 3792: 80 80 80 09 03 00 36 00 00 00 13 03 32 66 6f 01 ......6.....2fo. +| 3808: 02 02 02 01 74 01 06 01 01 04 04 07 1b 84 80 80 ....t........... +| 3824: 80 80 08 03 00 3c 00 00 00 16 02 31 74 01 0a 04 .....<.....1t... +| 3840: 01 01 03 04 00 03 32 65 61 01 02 03 04 0a 17 84 ......2ea....... +| 3856: 80 80 80 80 07 03 00 34 00 00 00 12 02 31 69 01 .......4.....1i. +| 3872: 06 01 01 02 01 01 70 01 02 05 04 08 18 84 80 80 ......p......... +| 3888: 80 80 06 03 00 36 00 00 00 13 02 31 65 01 02 03 .....6.....1e... +| 3904: 01 01 66 01 08 02 01 01 04 04 06 1b 84 80 80 80 ..f............. +| 3920: 80 05 03 00 3c 00 00 00 16 05 30 74 65 72 6d 01 ....<.....0term. +| 3936: 02 04 02 02 68 65 01 06 01 01 03 04 09 14 84 80 ....he.......... +| 3952: 80 80 80 04 03 00 2e 00 00 00 10 06 30 64 61 62 ............0dab +| 3968: 6c 65 01 06 01 01 05 04 15 84 80 80 80 80 03 03 le.............. +| 3984: 00 30 00 00 00 11 02 08 30 70 72 65 73 65 6e 74 .0......0present +| 4000: 01 02 05 05 1b 84 80 80 80 80 02 03 00 3c 00 00 .............<.. +| 4016: 00 16 04 30 66 74 73 01 06 01 01 04 01 02 69 6e ...0fts.......in +| 4032: 01 06 01 01 04 0a 1a 84 80 80 80 80 01 03 00 3a ...............: +| 4048: 00 00 00 15 05 30 65 61 63 68 01 02 03 01 13 66 .....0each.....f +| 4064: 6f 72 01 02 02 04 09 06 01 03 00 12 03 0b 0f 00 or.............. +| 4080: 00 08 8c 80 80 80 80 11 03 00 16 00 00 00 05 04 ................ +| page 3 offset 8192 +| 0: 0a 00 00 00 32 0e 4f 00 0f fa 0f f1 0f e9 0f e1 ....2.O......... +| 16: 0f d8 0f d1 0f c9 0f c1 0f b9 0f b1 0f a9 0f a0 ................ +| 32: 0f 98 0f 90 0f 87 0f 80 0f 78 0f 71 0f 68 0f 5f .........x.q.h._ +| 48: 0f 56 0f 4d 0f 41 0f 38 0f 2f 0f 26 0f 1d 0f 13 .V.M.A.8./.&.... +| 64: 0f 0a 0f 01 0e f7 0e ee 0e e6 0e dd 0e d6 0e cd ................ +| 80: 0e c3 0e ba 0e 00 00 00 00 00 00 00 00 00 00 00 ................ +| 3648: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 08 ................ +| 3664: 04 01 10 01 03 34 74 20 07 04 01 0e 01 03 34 1e .....4t ......4. +| 3680: 09 04 01 12 01 03 33 74 68 1c 08 04 01 10 01 03 ......3th....... +| 3696: 33 6e 1a 08 04 01 10 01 03 32 77 18 08 04 01 10 3n.......2w..... +| 3712: 01 03 32 74 16 08 04 01 10 01 03 32 6e 14 07 04 ..2t.......2n... +| 3728: 01 0e 01 03 32 12 08 04 01 10 01 03 31 74 10 08 ....2.......1t.. +| 3744: 04 01 10 01 03 31 6e 0e 07 04 01 0e 01 03 31 0c .....1n.......1. +| 3760: 09 04 01 12 01 03 30 74 68 0a 08 04 01 10 01 03 ......0th....... +| 3776: 30 74 08 09 04 01 12 01 03 30 6e 75 06 08 04 01 0t.......0nu.... +| 3792: 10 01 03 30 6e 04 06 04 01 0c 01 03 02 08 04 01 ...0n........... +| 3808: 10 01 02 34 72 22 07 04 01 0e 01 02 34 20 08 04 ...4r.......4 .. +| 3824: 01 10 01 02 33 72 1e 09 04 01 12 01 02 33 61 72 ....3r.......3ar +| 3840: 1c 08 04 01 10 01 02 32 74 1a 08 04 01 10 01 02 .......2t....... +| 3856: 32 69 18 09 04 01 12 01 02 32 61 72 16 08 04 01 2i.......2ar.... +| 3872: 10 01 02 31 74 14 08 04 01 10 01 02 31 6e 12 08 ...1t.......1n.. +| 3888: 04 01 10 01 02 31 62 10 08 04 01 10 01 02 31 32 .....1b.......12 +| 3904: 0e 0b 04 01 16 01 02 30 74 68 65 72 0c 08 04 01 .......0ther.... +| 3920: 10 01 02 30 74 0a 08 04 01 10 01 02 30 6e 08 08 ...0t.......0n.. +| 3936: 14 01 10 01 02 30 62 06 08 04 01 10 01 02 30 61 .....0b.......0a +| 3952: 04 06 04 01 0c 01 02 02 07 04 09 10 01 34 74 22 .............4t. +| 3968: 06 04 09 0e 01 34 20 08 04 09 12 01 33 74 65 1e .....4 .....3te. +| 3984: 07 04 09 10 01 33 70 1c 07 04 09 10 01 33 66 1a .....3p......3f. +| 4000: 08 04 09 12 01 32 74 68 18 07 04 09 10 01 32 74 .....2th......2t +| 4016: 16 01 64 09 10 01 32 69 14 07 04 09 10 01 32 66 ..d...2i......2f +| 4032: 12 07 04 09 10 01 31 74 10 07 04 09 10 01 31 69 ......1t......1i +| 4048: 0e 06 04 09 0e 01 31 0c 08 04 09 12 01 30 74 65 ......1......0te +| 4064: 0a 06 04 09 10 01 30 74 08 07 04 09 10 01 30 70 ......0t......0p +| 4080: 06 08 04 09 12 00 00 00 00 00 00 00 00 00 00 00 ................ +| page 4 offset 12288 +| 4064: 00 00 00 00 00 00 00 00 00 00 00 05 03 03 00 10 ................ +| 4080: 03 05 05 02 03 00 10 04 06 05 01 03 00 10 04 04 ................ +| page 5 offset 16384 +| 0: 0a 00 00 00 02 0f eb 00 0f eb 0f f4 00 00 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 08 03 15 01 70 ...............p +| 4080: 67 73 7a 18 0b 03 1b 01 76 65 72 73 69 6f 6e 04 gsz.....version. +| page 6 offset 20480 +| 0: 0d 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 4080: 00 00 03 03 02 01 03 03 02 02 01 00 00 00 00 00 ................ +| end crash-0f47112aa7520c.db + }] +} {} + +do_catchsql_test 1.1 { + SELECT * FROM t1('R*') WHERE (a,b)<=(current_date,0) ORDER BY rowid DESC; +} {/.*fts5: corrupt.*/} + +#------------------------------------------------------------------------- +# +reset_db +do_test 2.0 { + sqlite3 db {} + db deserialize [decode_hexdb { + +.open --hexdb +| size 24576 pagesize 4096 filename sql047467.txt.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 00 .....@ ........ +| 96: 00 00 00 00 0d 00 00 00 06 0e 0f 00 0f aa 0f 53 ...............S +| 112: 0e e8 0e 8b 0e 33 0e 0f 01 00 00 00 00 00 00 00 .....3.......... +| 3584: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 22 ................ +| 3600: 06 06 17 11 11 01 31 74 61 62 6c 65 62 62 62 62 ......1tablebbbb +| 3616: 06 43 52 45 41 54 45 20 54 41 42 4c 45 20 62 62 .CREATE TABLE bb +| 3632: 28 61 29 56 05 06 17 1f 1f 01 7d 74 61 62 6c 65 (a)V.......table +| 3648: 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 6f 6e 66 t1_configt1_conf +| 3664: 69 67 05 43 52 45 41 54 45 20 54 41 42 4c 45 20 ig.CREATE TABLE +| 3680: 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b 20 50 52 't1_config'(k PR +| 3696: 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 20 57 49 IMARY KEY, v) WI +| 3712: 54 48 4f 55 54 20 52 4f 57 49 44 5b 04 07 17 21 THOUT ROWID[...! +| 3728: 21 01 81 01 74 61 62 6c 65 74 31 5f 64 6f 63 73 !...tablet1_docs +| 3744: 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 04 43 52 izet1_docsize.CR +| 3760: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 64 EATE TABLE 't1_d +| 3776: 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 54 45 47 ocsize'(id INTEG +| 3792: 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 ER PRIMARY KEY, +| 3808: 73 7a 20 42 4c 4f 42 29 69 03 07 17 19 19 01 81 sz BLOB)i....... +| 3824: 2d 74 61 62 6c 65 74 31 5f 69 64 78 74 31 5f 69 -tablet1_idxt1_i +| 3840: 64 78 03 43 52 45 41 54 45 20 54 41 42 4c 45 20 dx.CREATE TABLE +| 3856: 27 74 31 5f 69 64 78 27 28 73 65 67 69 64 2c 20 't1_idx'(segid, +| 3872: 74 65 72 6d 2c 20 70 67 6e 6f 2c 20 50 52 49 4d term, pgno, PRIM +| 3888: 41 52 59 20 4b 45 59 28 73 65 67 69 64 2c 20 74 ARY KEY(segid, t +| 3904: 65 72 6d 29 29 20 57 49 54 48 4f 55 54 20 52 4f erm)) WITHOUT RO +| 3920: 57 49 44 55 02 07 17 1b 1b 01 81 01 74 61 62 6c WIDU........tabl +| 3936: 65 74 31 5f 64 61 74 61 74 31 5f 64 61 74 61 02 et1_datat1_data. +| 3952: 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 CREATE TABLE 't1 +| 3968: 5f 64 61 74 61 27 28 69 64 20 49 4e 54 45 47 45 _data'(id INTEGE +| 3984: 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 62 R PRIMARY KEY, b +| 4000: 6c 6f 63 6b 20 42 4c 4f 42 29 54 01 07 17 11 11 lock BLOB)T..... +| 4016: 08 81 15 74 61 62 6c 65 74 31 74 31 43 52 45 41 ...tablet1t1CREA +| 4032: 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45 TE VIRTUAL TABLE +| 4048: 20 74 31 20 55 53 49 4e 47 20 66 74 73 35 28 61 t1 USING fts5(a +| 4064: 2c 62 2c 70 72 65 66 69 78 3d 22 31 2c 32 2c 33 ,b,prefix=.1,2,3 +| 4080: 2c 34 22 2c 20 63 6f 6e 74 65 6e 74 3d 22 22 29 ,4., content=..) +| page 2 offset 4096 +| 0: 0d 0b 6a 00 37 09 4c 02 0f e7 09 4c 0f c6 0f a4 ..j.7.L....L.... +| 16: 0f 88 0f 6d 0f 4b 0f 2c 0f 0e 0e ec 0e cd 0e ad ...m.K.,........ +| 32: 0e 8e 0e 6c 0e 4b 0e 29 0e 08 0d e6 0d c4 0d b5 ...l.K.)........ +| 48: 0d 97 0d 76 0d 54 0d 31 0d 15 0c f3 0c d3 0c b5 ...v.T.1........ +| 64: 0c 95 0c 73 0c 54 0c 32 0c 10 0b ee 0b cc 0b b0 ...s.T.2........ +| 80: 0b 8d 0b 7e 0b 48 0b 2e 0b 0b 0a ef 00 00 00 00 ...~.H.......... +| 2368: 00 00 00 00 00 00 00 00 00 00 00 00 15 0a 03 00 ................ +| 2384: 30 00 00 00 01 01 03 35 00 03 01 11 12 02 01 12 0......5........ +| 2400: 03 01 11 1c 8c 80 80 80 80 10 03 00 3e 00 00 00 ............>... +| 2416: 17 01 05 05 34 74 61 62 6c 03 02 03 01 04 77 68 ....4tabl.....wh +| 2432: 65 72 03 02 06 09 1b 8c 80 80 80 80 0f 03 00 3c er.............< +| 2448: 00 00 00 16 05 34 66 74 73 34 03 02 02 01 04 6e .....4fts4.....n +| 2464: 75 6d 62 03 06 01 04 09 1b 8c 80 80 80 80 0e 03 umb............. +| 2480: 00 3b ff f0 00 16 04 33 74 68 65 03 06 01 01 04 .;.....3the..... +| 2496: 01 03 77 68 65 03 02 04 04 0a 1b 8c 80 80 80 80 ..whe........... +| 2512: 0d 03 00 3c 00 00 00 16 04 33 6e 75 6d 03 06 01 ...<.....3num... +| 2528: 01 05 01 03 74 61 62 03 02 03 04 0a 19 8c 80 80 ....tab......... +| 2544: 80 80 0c 03 00 38 00 00 00 14 03 32 77 68 03 02 .....8.....2wh.. +| 2560: 04 00 04 33 66 74 73 03 02 02 04 07 18 8c 80 80 ...3fts......... +| 2576: 80 80 0b 03 00 36 00 00 00 13 03 32 74 61 03 02 .....6.....2ta.. +| 2592: 03 02 01 68 03 06 01 01 04 04 07 1b 8c 80 80 80 ...h............ +| 2608: 80 0a 03 00 3c 00 00 00 16 03 32 6e 75 03 06 01 ....<.....2nu... +| 2624: 01 05 01 02 6f 66 03 06 01 01 06 04 09 19 8c 80 ....of.......... +| 2640: 80 80 80 09 03 00 38 00 00 00 14 03 32 66 74 03 ......8.....2ft. +| 2656: 02 02 01 02 69 73 03 06 01 01 03 04 07 18 8c 80 ....is.......... +| 2672: 80 80 80 08 03 00 36 00 00 00 13 02 31 74 03 08 ......6.....1t.. +| 2688: 03 01 01 04 01 01 77 03 02 04 04 09 1a 8c 80 80 ......w......... +| 2704: 80 80 07 03 00 3a 00 00 00 15 02 31 6e 03 08 01 .....:.....1n... +| 2720: 01 02 05 01 00 6f 03 06 01 01 06 14 09 18 8c 80 .....o.......... +| 2736: 80 80 80 06 03 00 36 00 00 00 03 04 02 31 66 03 ......6......1f. +| 2752: 02 02 01 01 69 03 06 01 01 03 04 f6 1c 8c 80 80 ....i........... +| 2768: 80 80 05 03 00 3e 00 00 00 17 04 30 74 68 65 03 .....>.....0the. +| 2784: f6 01 01 04 01 05 77 68 65 72 65 03 02 04 0a 15 ......where..... +| 2800: 8c 80 80 80 80 04 03 00 30 00 00 00 11 01 01 06 ........0....... +| 2816: 06 30 74 61 62 6c 65 0f 42 03 07 1c 8c 81 80 80 .0table.B....... +| 2832: 80 03 03 00 3e 00 00 00 17 07 30 6e 75 6d 62 65 ....>.....0numbe +| 2848: 72 03 06 01 01 05 01 02 6f 66 03 06 04 0d 13 8c r.......of...... +| 2864: 80 80 80 80 02 03 00 2c 00 00 00 0f 01 01 03 02 .......,........ +| 2880: 30 6e 03 06 01 01 02 07 1b 8c 80 80 80 80 01 03 0n.............. +| 2896: 00 3c 00 00 00 16 08 30 66 74 73 34 61 75 78 03 .<.....0fts4aux. +| 2912: 02 02 01 02 69 73 03 06 04 0c 00 00 00 14 2a 00 ....is........*. +| 2928: 00 00 01 01 02 24 00 02 01 01 12 02 01 12 08 88 .....$.......... +| 2944: 80 80 80 80 12 03 00 16 00 00 00 05 02 1c 88 80 ................ +| 2960: 80 80 80 11 03 00 3e 00 00 00 17 05 34 72 6f 77 ......>.....4row +| 2976: 73 02 06 01 01 05 01 04 74 68 65 72 02 02 04 0b s.......ther.... +| 2992: 15 88 80 80 80 80 10 03 00 30 00 00 00 11 02 01 .........0...... +| 3008: 01 07 05 34 62 65 74 77 02 02 04 08 1b 88 80 80 ...4betw........ +| 3024: 80 80 0f 03 00 3c 00 00 00 16 04 04 33 72 6f 77 .....<......3row +| 3040: 02 06 01 01 05 01 03 74 68 65 02 08 05 0a 1b 88 .......the...... +| 3056: 80 80 80 80 0e 03 00 3c 00 00 00 16 01 01 02 04 .......<........ +| 3072: 33 61 72 65 02 02 03 01 03 62 65 74 02 02 07 08 3are.....bet.... +| 3088: 1b 88 80 80 80 80 0d 03 00 3c 00 00 00 16 13 32 .........<.....2 +| 3104: 74 68 02 08 02 01 01 07 00 04 33 61 6e 64 02 06 th........3and.. +| 3120: 04 0a 1b 88 80 80 80 80 0c 03 00 3c 00 00 00 16 ...........<.... +| 3136: 03 32 69 6e 02 06 01 01 06 01 02 72 6f 02 06 01 .2in.......ro... +| 3152: 01 05 04 09 18 88 80 80 80 80 0b 03 00 36 00 00 .............6.. +| 3168: 00 13 02 03 32 61 72 02 02 03 01 02 62 65 02 02 ....2ar.....be.. +| 3184: 04 05 07 1b 88 80 80 80 80 0a 03 00 3c 00 94 50 ............<..P +| 3200: 16 02 31 74 02 08 02 01 01 07 00 03 32 61 6e 02 ..1t........2an. +| 3216: 06 01 01 04 09 19 88 80 80 80 80 09 03 00 38 00 ..............8. +| 3232: 00 00 14 02 31 6e 02 06 01 01 03 01 01 72 02 06 ....1n.......r.. +| 3248: 01 01 05 04 08 17 88 80 80 80 80 08 03 00 34 00 ..............4. +| 3264: 00 00 12 02 31 62 02 02 04 01 01 69 02 06 01 01 ....1b.....i.... +| 3280: 06 04 06 19 88 80 80 80 80 07 03 00 38 00 00 00 ............8... +| 3296: 14 04 02 31 32 02 02 05 01 01 61 02 08 03 01 01 ...12.....a..... +| 3312: 02 05 06 1b 88 80 80 80 80 06 03 00 3c 00 00 00 ............<... +| 3328: 16 06 30 74 68 65 72 65 02 02 02 00 02 31 31 02 ..0there.....11. +| 3344: 06 01 01 04 0a 15 88 80 80 80 80 05 03 00 30 00 ..............0. +| 3360: 00 00 11 01 01 05 04 30 74 68 65 02 06 01 01 07 .......0the..... +| 3376: 07 1c 88 80 80 80 80 04 03 00 3e 00 00 00 17 01 ..........>..... +| 3392: 01 06 02 30 6e 02 06 01 01 03 01 04 72 6f 77 73 ...0n.......rows +| 3408: 02 06 07 08 1b 88 80 80 80 80 03 03 00 3c 00 00 .............<.. +| 3424: 00 16 08 30 62 65 74 77 65 65 6e 02 02 04 01 02 ...0between..... +| 3440: 69 6e 02 06 04 0c 1a 88 80 80 80 80 02 03 00 3a in.............: +| 3456: 00 00 00 15 04 30 61 6e 64 02 06 01 01 02 02 02 .....0and....... +| 3472: 72 65 02 02 03 04 0a 17 88 80 80 80 80 01 03 00 re.............. +| 3488: 34 00 00 00 12 02 30 31 02 06 01 01 04 01 01 32 4.....01.......2 +| 3504: 02 02 05 04 08 08 84 80 80 80 80 12 03 00 16 00 ................ +| 3520: 00 00 05 04 1b 84 80 80 80 80 11 03 00 3c 00 00 .............<.. +| 3536: 00 16 05 34 74 61 62 6c 01 06 01 01 05 02 03 65 ...4tabl.......e +| 3552: 72 6d 01 02 04 0b 1b 84 80 80 80 80 10 03 00 3c rm.............< +| 3568: 00 00 00 16 05 34 65 61 63 68 01 02 03 01 04 70 .....4each.....p +| 3584: 72 65 73 01 02 05 04 08 1a 84 80 80 80 80 0f 03 res............. +| 3600: 00 3a 00 00 00 15 04 33 74 65 72 01 02 04 02 02 .:.....3ter..... +| 3616: 68 65 01 06 01 01 03 04 08 1b 84 80 80 80 80 0e he.............. +| 3632: 03 00 3c 00 00 00 16 04 33 80 72 65 01 02 05 01 ..<.....3.re.... +| 3648: 03 74 61 62 01 06 01 01 05 04 08 1a 84 80 80 80 .tab............ +| 3664: 80 0d 03 00 3a 00 00 00 15 04 33 66 6f 72 01 02 ....:.....3for.. +| 3680: 02 02 02 74 73 01 06 01 01 04 04 08 1b 84 80 80 ...ts........... +| 3696: 80 80 0c 03 00 3c 00 00 00 17 03 32 74 68 01 06 .....<.....2th.. +| 3712: 01 01 03 00 04 33 65 61 63 01 02 03 04 09 18 84 .....3eac....... +| 3728: 80 80 80 80 0b 03 00 36 00 00 00 13 03 32 74 61 .......6.....2ta +| 3744: 01 06 01 01 05 02 01 65 01 02 04 04 09 19 84 80 .......e........ +| 3760: 80 80 80 0a 03 00 38 00 00 00 14 03 32 69 6e 01 ......8.....2in. +| 3776: 06 01 01 02 01 02 70 72 01 02 05 04 09 18 84 80 ......pr........ +| 3792: 80 80 80 09 03 00 36 00 00 00 13 03 32 66 6f 01 ......6.....2fo. +| 3808: 02 02 02 01 74 01 06 01 01 04 04 07 1b 84 80 80 ....t........... +| 3824: 80 80 08 03 00 3c 00 00 00 16 02 31 74 01 0a 04 .....<.....1t... +| 3840: 01 01 03 04 00 03 32 65 61 01 02 03 04 0a 17 84 ......2ea....... +| 3856: 80 80 80 80 07 03 00 34 00 00 00 12 02 31 69 01 .......4.....1i. +| 3872: 06 01 01 02 01 01 70 01 02 05 04 08 18 84 80 80 ......p......... +| 3888: 80 80 06 03 00 36 00 00 00 13 02 31 65 01 02 03 .....6.....1e... +| 3904: 01 01 66 01 08 02 01 01 04 04 06 1b 84 80 80 80 ..f............. +| 3920: 80 05 03 00 3c 00 00 00 16 05 30 74 65 72 6d 01 ....<.....0term. +| 3936: 02 04 02 02 68 65 01 06 01 01 03 04 09 14 84 80 ....he.......... +| 3952: 80 80 80 04 03 00 2e 00 00 00 10 06 30 64 61 62 ............0dab +| 3968: 6c 65 01 06 01 01 05 04 15 84 80 80 80 80 03 03 le.............. +| 3984: 00 30 00 00 00 11 02 08 30 70 72 65 73 65 6e 74 .0......0present +| 4000: 01 02 05 05 1b 84 80 80 80 80 02 03 00 3c 00 00 .............<.. +| 4016: 00 16 04 30 66 74 73 01 06 01 01 04 01 02 69 6e ...0fts.......in +| 4032: 01 06 01 01 04 0a 1a 84 80 80 80 80 01 03 00 3a ...............: +| 4048: 00 00 00 15 05 30 65 61 63 68 01 02 03 01 13 66 .....0each.....f +| 4064: 6f 72 01 02 02 04 09 06 01 03 00 12 03 0b 0f 00 or.............. +| 4080: 00 08 8c 80 80 80 80 11 03 00 16 00 00 00 05 04 ................ +| page 3 offset 8192 +| 0: 0a 00 00 00 32 0e 4f 00 0f fa 0f f1 0f e9 0f e1 ....2.O......... +| 16: 0f d8 0f d1 0f c9 0f c1 0f b9 0f b1 0f a9 0f a0 ................ +| 32: 0f 98 0f 90 0f 87 0f 80 0f 78 0f 71 0f 68 0f 5f .........x.q.h._ +| 48: 0f 56 0f 4d 0f 41 0f 38 0f 2f 0f 26 0f 1d 0f 13 .V.M.A.8./.&.... +| 64: 0f 0a 0f 01 0e f7 0e ee 0e e6 0e dd 0e d6 0e cd ................ +| 80: 0e c3 0e ba 0e 00 00 00 00 00 00 00 00 00 00 00 ................ +| 3648: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 08 ................ +| 3664: 04 01 10 01 03 34 74 20 07 04 01 0e 01 03 34 1e .....4t ......4. +| 3680: 09 04 01 12 01 03 33 74 68 1c 08 04 01 10 01 03 ......3th....... +| 3696: 33 6e 1a 08 04 01 10 01 03 32 77 18 08 04 01 10 3n.......2w..... +| 3712: 01 03 32 74 16 08 04 01 10 01 03 32 6e 14 07 04 ..2t.......2n... +| 3728: 01 0e 01 03 32 12 08 04 01 10 01 03 31 74 10 08 ....2.......1t.. +| 3744: 04 01 10 01 03 31 6e 0e 07 04 01 0e 01 03 31 0c .....1n.......1. +| 3760: 09 04 01 12 01 03 30 74 68 0a 08 04 01 10 01 03 ......0th....... +| 3776: 30 74 08 09 04 01 12 01 03 30 6e 75 06 08 04 01 0t.......0nu.... +| 3792: 10 01 03 30 6e 04 06 04 01 0c 01 03 02 08 04 01 ...0n........... +| 3808: 10 01 02 34 72 22 07 04 01 0e 01 02 34 20 08 04 ...4r.......4 .. +| 3824: 01 10 01 02 33 72 1e 09 04 01 12 01 02 33 61 72 ....3r.......3ar +| 3840: 1c 08 04 01 10 01 02 32 74 1a 08 04 01 10 01 02 .......2t....... +| 3856: 32 69 18 09 04 01 12 01 02 32 61 72 16 08 04 01 2i.......2ar.... +| 3872: 10 01 02 31 74 14 08 04 01 10 01 02 31 6e 12 08 ...1t.......1n.. +| 3888: 04 01 10 01 02 31 62 10 08 04 01 10 01 02 31 32 .....1b.......12 +| 3904: 0e 0b 04 01 16 01 02 30 74 68 65 72 0c 08 04 01 .......0ther.... +| 3920: 10 01 02 30 74 0a 08 04 01 10 01 02 30 6e 08 08 ...0t.......0n.. +| 3936: 14 01 10 01 02 30 62 06 08 04 01 10 01 02 30 61 .....0b.......0a +| 3952: 04 06 04 01 0c 01 02 02 07 04 09 10 01 34 74 22 .............4t. +| 3968: 06 04 09 0e 01 34 20 08 04 09 12 01 33 74 65 1e .....4 .....3te. +| 3984: 07 04 09 10 01 33 70 1c 07 04 09 10 01 33 66 1a .....3p......3f. +| 4000: 08 04 09 12 01 32 74 68 18 07 04 09 10 01 32 74 .....2th......2t +| 4016: 16 01 64 09 10 01 32 69 14 07 04 09 10 01 32 66 ..d...2i......2f +| 4032: 12 07 04 09 10 01 31 74 10 07 04 09 10 01 31 69 ......1t......1i +| 4048: 0e 06 04 09 0e 01 31 0c 08 04 09 12 01 30 74 65 ......1......0te +| 4064: 0a 06 04 09 10 01 30 74 08 07 04 09 10 01 30 70 ......0t......0p +| 4080: 06 08 04 09 12 00 00 00 00 00 00 00 00 00 00 00 ................ +| page 4 offset 12288 +| 4064: 00 00 00 00 00 00 00 00 00 00 00 05 03 03 00 10 ................ +| 4080: 03 05 05 02 03 00 10 04 06 05 01 03 00 10 04 04 ................ +| page 5 offset 16384 +| 0: 0a 00 00 00 02 0f eb 00 0f eb 0f f4 00 00 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 08 03 15 01 70 ...............p +| 4080: 67 73 7a 18 0b 03 1b 01 76 65 72 73 69 6f 6e 04 gsz.....version. +| page 6 offset 20480 +| 0: 0d 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 4080: 00 00 03 03 02 01 03 03 02 02 01 00 00 00 00 00 ................ +| end sql047467.txt.db +}]} {} + +do_catchsql_test 2.1 { +SELECT * FROM t1('R*R*R*R*') WHERE (a,b)<=(current_date,0) ORDER BY rowid DESC; +} {/.*fts5: corrupt.*/} + +#------------------------------------------------------------------------- +reset_db +do_test 3.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 32768 pagesize 4096 filename crash-c69fcaceff1e50.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 08 .....@ ........ +| 32: 00 00 00 02 00 00 00 01 00 00 00 09 00 00 00 04 ................ +| 96: 00 00 00 00 0d 0f c7 00 07 0d 92 00 0f 8d 0f 36 ...............6 +| 112: 0e cb 0e 6b 0e 0e 0d b6 0d 92 0d 92 00 00 00 00 ...k............ +| 3472: 00 00 22 08 06 17 11 11 01 31 74 61 62 6c 65 74 .........1tablet +| 3488: 32 74 32 08 43 52 45 41 54 45 20 54 41 42 4c 45 2t2.CREATE TABLE +| 3504: 20 74 32 28 78 29 56 07 06 17 1f 1f 01 7d 74 61 t2(x)V.......ta +| 3520: 62 6c 65 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 blet1_configt1_c +| 3536: 6f 6e 66 69 67 07 43 52 45 41 54 45 20 54 41 42 onfig.CREATE TAB +| 3552: 4c 45 20 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b LE 't1_config'(k +| 3568: 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 PRIMARY KEY, v) +| 3584: 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 5b 06 WITHOUT ROWID[. +| 3600: 07 17 21 21 01 81 01 74 61 62 6c 65 74 31 5f 64 ..!!...tablet1_d +| 3616: 6f 63 73 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 ocsizet1_docsize +| 3632: 06 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 .CREATE TABLE 't +| 3648: 31 5f 64 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 1_docsize'(id IN +| 3664: 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE +| 3680: 59 2c 20 73 7a 20 42 4c 4f 42 29 5e 05 07 17 21 Y, sz BLOB)^...! +| 3696: 21 01 81 07 74 61 62 6c 65 74 31 5f 63 6f 6e 74 !...tablet1_cont +| 3712: 65 6e 74 74 31 5f 63 6f 6e 74 65 6e 74 05 43 52 entt1_content.CR +| 3728: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 EATE TABLE 't1_c +| 3744: 6f 6e 74 65 6e 74 27 28 69 64 20 49 4e 54 45 47 ontent'(id INTEG +| 3760: 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 ER PRIMARY KEY, +| 3776: 63 30 2c 20 63 31 2c 20 63 32 29 69 04 07 17 19 c0, c1, c2)i.... +| 3792: 19 01 81 2d 74 61 62 6c 65 74 31 5f 69 64 78 74 ...-tablet1_idxt +| 3808: 31 5f 69 64 78 04 43 52 45 41 54 45 20 54 41 42 1_idx.CREATE TAB +| 3824: 4c 45 20 27 74 31 5f 69 64 78 27 28 73 65 67 69 LE 't1_idx'(segi +| 3840: 64 2c 20 74 65 72 6d 2c 20 70 67 6e 6f 2c 20 50 d, term, pgno, P +| 3856: 52 49 4d 41 52 59 20 4b 45 59 28 73 65 67 69 64 RIMARY KEY(segid +| 3872: 2c 20 74 65 72 6d 29 29 20 57 49 54 48 4f 55 54 , term)) WITHOUT +| 3888: 20 52 4f 57 49 44 55 03 07 17 1b 1b 01 81 01 74 ROWIDU........t +| 3904: 61 62 6c 65 74 31 5f 64 61 74 61 74 31 5f 64 61 ablet1_datat1_da +| 3920: 74 61 03 43 52 45 41 54 45 20 54 41 42 4c 45 20 ta.CREATE TABLE +| 3936: 27 74 31 5f 64 61 74 61 27 28 69 64 20 49 4e 54 't1_data'(id INT +| 3952: 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 EGER PRIMARY KEY +| 3968: 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 29 38 02 06 , block BLOB)8.. +| 3984: 17 11 11 08 5f 74 61 62 6c 65 74 31 74 31 43 52 ...._tablet1t1CR +| 4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 35 LE t1 USING fts5 +| 4032: 28 61 2c 62 2c 63 29 00 00 00 00 00 00 00 00 00 (a,b,c)......... +| page 3 offset 8192 +| 0: 0d 00 00 00 03 0c 94 00 0f e6 0f ef 0c 94 00 00 ................ +| 3216: 00 00 00 00 86 4a 84 80 80 80 80 01 04 00 8d 18 .....J.......... +| 3232: 00 00 01 bb 02 30 30 01 02 06 01 02 06 01 02 06 .....00......... +| 3248: 1f 02 03 01 02 03 01 02 03 01 08 32 30 31 36 30 ...........20160 +| 3264: 36 30 39 01 02 07 01 02 07 01 02 07 01 01 34 01 609...........4. +| 3280: 02 05 01 02 05 01 02 05 01 01 35 01 02 04 01 02 ..........5..... +| 3296: 04 01 02 04 02 07 30 30 30 30 30 30 30 1c 02 04 ......0000000... +| 3312: 01 02 04 01 02 04 01 06 62 69 6e 61 72 79 03 06 ........binary.. +| 3328: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3344: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3360: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 ................ +| 3376: 03 06 01 02 02 03 06 01 02 02 01 08 63 6f 6d 70 ............comp +| 3392: 69 6c 65 72 01 02 02 01 02 02 01 02 02 01 06 64 iler...........d +| 3408: 62 73 74 61 74 07 02 03 01 02 03 01 02 03 02 04 bstat........... +| 3424: 65 62 75 67 04 02 02 01 02 02 01 02 02 01 06 65 ebug...........e +| 3440: 6b b1 62 6c 65 07 02 02 01 02 02 01 02 02 01 02 k.ble........... +| 3456: 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 ................ +| 3472: 01 02 02 01 02 02 05 02 02 01 02 02 01 02 02 01 ................ +| 3488: 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 ................ +| 3504: 02 01 02 02 02 08 78 74 65 6e 73 69 6f 6e 1f 02 ......xtension.. +| 3520: 04 01 02 04 01 02 04 01 04 66 74 73 34 1a 02 03 .........fts4... +| 3536: 01 02 03 01 02 03 04 01 35 0d 02 03 01 02 03 01 ........5....... +| 3552: 02 03 01 03 67 63 63 01 02 03 01 02 03 01 02 03 ....gcc......... +| 3568: 02 06 65 6f 70 6f 6c 79 10 02 03 01 02 03 01 02 ..eopoly........ +| 3584: 03 01 05 6a 73 6f 6e 31 13 02 03 01 02 03 01 02 ...json1........ +| 3600: 03 01 04 6c 6f 61 64 1f 02 03 01 02 03 01 02 03 ...load......... +| 3616: 01 03 6d 61 78 1c 02 02 01 02 02 01 02 02 02 05 ..max........... +| 3632: 65 6d 6f 72 79 1c 02 03 01 02 03 01 02 03 04 04 emory........... +| 3648: 73 79 73 35 16 02 03 01 02 03 01 02 03 01 06 6e sys5...........n +| 3664: 6f 63 61 73 65 02 06 01 02 02 03 06 01 02 02 03 ocase........... +| 3680: 06 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 ................ +| 3696: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3712: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3728: 02 01 04 6f 6d 69 74 1f 02 02 01 02 02 01 02 01 ...omit......... +| 3744: ff ff ff ff ff ff ff ff ff ff ff ff ff ff f1 02 ................ +| 3760: 58 81 96 4d 01 06 01 02 02 03 06 01 02 02 03 06 X..M............ +| 3776: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3792: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3808: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 ................ +| 3824: 01 0a 74 68 72 65 61 64 73 61 66 65 22 02 02 01 ..threadsafe.... +| 3840: 02 02 01 02 02 01 04 76 74 61 62 07 02 04 01 02 .......vtab..... +| 3856: 04 01 02 04 01 01 78 01 06 01 01 02 01 06 01 01 ......x......... +| 3872: 02 01 06 01 c6 02 01 06 01 01 02 01 06 01 01 02 ................ +| 3888: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 3904: 06 01 01 02 00 f6 01 01 02 01 06 01 01 02 01 06 ................ +| 3920: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 ................ +| 3936: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 ................ +| 3952: 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 ................ +| 3968: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 3984: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 4000: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 ................ +| 4016: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 ................ +| 4032: 02 01 06 01 01 02 01 06 01 01 02 04 15 13 0c 0c ................ +| 4048: 12 44 13 11 0f 47 13 0f 0c 0e 11 10 0f 0e 10 0f .D...G.......... +| 4064: 44 0f 10 40 15 0f 07 01 03 00 14 24 5a 24 24 0f D..@.......$Z$$. +| 4080: 0a 03 00 24 00 00 00 00 01 01 01 00 01 01 01 01 ...$............ +| page 4 offset 12288 +| 0: 0a 00 00 00 01 0f fa 00 0f fa 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 05 04 09 0c 01 02 ................ +| page 7 offset 24576 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| end crash-c69fcaceff1e50.db +}]} {} + +do_catchsql_test 3.1 { + UPDATE t1 SET b=quote(zeroblob(200)) WHERE a MATCH 'thra*T'; +} {/.*fts5: corrupt.*/} + +#------------------------------------------------------------------------- +reset_db +do_test 4.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 24576 pagesize 4096 filename crash-ef6738247b1344.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 10 00 06 40 00 00 06 .....@ ....@... +| 32: 00 00 00 00 00 00 00 00 00 00 00 06 00 00 00 00 ................ +| 96: 00 00 00 00 0d 00 00 00 06 0e 0f 00 0f aa 0f 53 ...............S +| 112: 0e e8 0e 8b 0e 33 0e 0f 00 00 00 00 00 00 00 00 .....3.......... +| 3584: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 22 ................ +| 3600: 06 06 17 11 11 01 31 74 61 62 6c 65 62 62 62 62 ......1tablebbbb +| 3616: 06 43 52 45 41 54 45 20 54 41 42 4c 45 20 62 62 .CREATE TABLE bb +| 3632: 28 61 29 56 05 06 17 1f 1f 01 7d 74 61 62 6c 65 (a)V.......table +| 3648: 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 6f 6e 66 t1_configt1_conf +| 3664: 69 67 05 43 52 45 41 54 45 20 54 41 42 4c 45 20 ig.CREATE TABLE +| 3680: 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b 20 50 52 't1_config'(k PR +| 3696: 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 20 57 49 IMARY KEY, v) WI +| 3712: 54 48 4f 55 54 20 52 4f 57 49 44 5b 04 07 17 21 THOUT ROWID[...! +| 3728: 21 01 81 01 74 61 62 6c 65 74 31 5f 64 6f 63 73 !...tablet1_docs +| 3744: 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 04 43 52 izet1_docsize.CR +| 3760: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 64 EATE TABLE 't1_d +| 3776: 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 54 45 47 ocsize'(id INTEG +| 3792: 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 ER PRIMARY KEY, +| 3808: 73 7a 20 42 4c 4f 42 29 69 03 07 17 19 19 01 81 sz BLOB)i....... +| 3824: 2d 74 61 62 6c 65 74 31 5f 69 64 78 74 31 5f 69 -tablet1_idxt1_i +| 3840: 64 78 03 43 52 45 41 54 45 20 54 41 42 4c 45 20 dx.CREATE TABLE +| 3856: 27 74 31 5f 69 64 78 27 28 73 65 67 69 64 2c 20 't1_idx'(segid, +| 3872: 74 65 72 6d 2c 20 6f 67 6e 6f 2c 20 50 52 49 4d term, ogno, PRIM +| 3888: 41 52 59 20 4b 45 59 28 73 65 67 69 64 2c 20 74 ARY KEY(segid, t +| 3904: 65 72 6d 29 29 20 57 49 54 48 4f 55 54 20 52 4f erm)) WITHOUT RO +| 3920: 57 49 44 55 35 07 17 1b 1b 01 81 01 74 61 62 6c WIDU5.......tabl +| 3936: 65 74 31 5f 64 61 74 61 74 31 5f 64 61 74 61 02 et1_datat1_data. +| 3952: 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 CREATE TABLE 't1 +| 3968: 5f 64 61 74 61 27 28 69 64 20 49 4e 54 45 47 45 _data'(id INTEGE +| 3984: 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 62 R PRIMARY KEY, b +| 4000: 6c 6f 63 6b 20 42 4c 4f 42 29 54 01 07 17 11 11 lock BLOB)T..... +| 4016: 08 81 15 74 61 62 6c 65 74 31 74 31 43 52 45 41 ...tablet1t1CREA +| 4032: 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45 TE VIRTUAL TABLE +| 4048: 20 74 31 20 55 53 49 4e 47 20 66 74 73 35 28 61 t1 USING fts5(a +| 4064: 2c 62 2c 70 72 65 66 69 78 3d 22 31 2c 32 2c 33 ,b,prefix=.1,2,3 +| 4080: 2c 34 22 2c 20 63 6f 6e 74 65 6e 74 3d 22 22 29 ,4., content=..) +| page 2 offset 4096 +| 0: 0d 0b 6a 00 37 09 4c 02 0f e7 09 4c 0f c6 0f a4 ..j.7.L....L.... +| 16: 0f 88 0f 6d 0f 4b 0f 2c 0f 0e 0e ec 0e cd 0e ad ...m.K.,........ +| 32: 0e 8e 0e 6c 0e 4b 0e 29 0e 08 0d e6 0d c4 0d b5 ...l.K.)........ +| 48: 0d 97 0d 76 0d 54 0d 31 0d 15 0c f3 0c d3 0c b5 ...v.T.1........ +| 64: 0c 95 0c 73 0c 54 0c 32 0c 10 0b ee 0b cc 0b b0 ...s.T.2........ +| 80: 0b 8d 0b 7e 0b 48 0b 2e 0b 0b 0a ef 0a cc 0a 00 ...~.H.......... +| 2368: 00 00 00 00 00 00 00 00 00 00 00 00 15 0a 03 00 ................ +| 2384: 30 00 00 00 01 01 03 35 00 03 01 01 12 02 01 12 0......5........ +| 2400: 03 01 11 1c 8c 80 80 80 80 10 02 9c 3e 00 00 00 ............>... +| 2416: 17 01 05 05 34 74 61 62 6c 03 02 03 01 04 77 68 ....4tabl.....wh +| 2432: 65 72 03 02 06 09 1b 8c 80 80 80 80 0f 03 00 3c er.............< +| 2448: 00 00 00 16 05 34 66 74 73 34 03 02 02 01 04 6e .....4fts4.....n +| 2464: 75 6d 62 03 06 01 04 09 1b 8c 80 80 80 80 0e 03 umb............. +| 2480: 00 3c 00 00 00 16 04 33 74 68 65 03 06 01 01 04 .<.....3the..... +| 2496: 01 03 77 68 65 03 02 04 04 0a 1b 8c 80 80 80 80 ..whe........... +| 2512: 0d 03 00 3c 00 00 00 16 04 33 6e 75 6d 03 06 01 ...<.....3num... +| 2528: 01 05 01 03 74 61 62 03 02 03 04 0a 19 8c 80 80 ....tab......... +| 2544: 80 80 0c 03 00 38 00 00 00 14 03 32 77 68 03 02 .....8.....2wh.. +| 2560: 04 00 04 33 66 74 73 03 12 02 04 07 18 8c 80 80 ...3fts......... +| 2576: 80 80 0b 03 00 36 00 00 00 13 03 32 74 61 03 02 .....6.....2ta.. +| 2592: 03 02 01 68 03 06 01 01 04 04 07 1b 8c 80 80 80 ...h............ +| 2608: 80 0a 03 00 3c 00 00 00 16 03 32 6e 75 03 06 01 ....<.....2nu... +| 2624: 01 05 01 02 6f 66 03 06 01 01 06 04 09 19 8c 80 ....of.......... +| 2640: 82 d0 80 09 03 00 38 00 00 00 14 03 32 66 74 03 ......8.....2ft. +| 2656: 02 02 01 02 69 73 03 06 01 01 03 04 07 18 8c 80 ....is.......... +| 2672: 80 80 80 08 03 00 36 00 00 00 13 02 31 74 03 08 ......6.....1t.. +| 2688: 03 01 01 04 01 01 77 03 02 04 04 09 1a 8c 80 80 ......w......... +| 2704: 80 80 07 03 00 3a 00 00 00 15 02 31 6e 03 08 01 .....:.....1n... +| 2720: 01 02 05 01 01 6f 03 06 01 01 06 04 09 18 8c 80 .....o.......... +| 2736: 81 80 80 06 03 00 36 00 00 00 13 04 12 31 66 03 ......6......1f. +| 2752: 02 02 01 01 69 03 06 01 01 03 05 06 1c 8c 80 80 ....i........... +| 2768: 80 80 05 03 00 3e 00 00 00 17 04 30 74 68 65 03 .....>.....0the. +| 2784: 06 01 01 04 01 05 77 68 65 72 65 03 02 04 0a 15 ......where..... +| 2800: 8c 80 65 80 80 04 03 00 30 00 00 00 11 01 01 06 ..e.....0....... +| 2816: 06 30 74 61 62 6c 65 03 02 03 07 1c 8c 80 80 80 .0table......... +| 2832: 80 03 03 00 3e 00 00 00 17 07 30 6e 75 6d 62 65 ....>.....0numbe +| 2848: 72 03 06 01 01 05 01 02 6f 66 03 06 04 0d 13 8c r.......of...... +| 2864: 80 80 80 80 02 03 00 2c 00 00 00 0f fc 01 03 02 .......,........ +| 2880: 30 6e 03 06 01 00 f2 07 1b 8c 80 80 80 80 01 03 0n.............. +| 2896: 00 3c 00 00 00 16 08 30 66 74 73 34 61 75 78 03 .<.....0fts4aux. +| 2912: 03 02 01 02 69 73 03 06 04 0c 00 00 00 18 ea 00 ....is.......... +| 2928: 00 00 01 01 02 24 00 02 01 01 12 02 01 12 08 88 .....$.......... +| 2944: 80 80 80 80 12 03 00 12 10 00 00 05 02 1c 88 80 ................ +| 2960: 80 80 80 11 03 00 3e 00 00 00 17 05 34 72 6f 77 ......>.....4row +| 2976: 73 02 06 01 01 05 01 04 74 68 65 72 02 02 04 0b s.......ther.... +| 2992: 15 88 80 80 80 80 10 03 00 2f ff ff f0 11 02 01 ........./...... +| 3008: 01 07 05 34 62 65 74 77 02 02 04 08 1b 88 80 80 ...4betw........ +| 3024: 80 80 0f cf 00 3c 00 00 00 16 04 04 33 72 6f 77 .....<......3row +| 3040: 02 06 01 01 05 01 03 74 68 65 02 08 05 0a 1b 88 .......the...... +| 3056: 80 80 80 80 0d 03 00 3c 00 00 00 16 00 01 02 04 .......<........ +| 3072: 33 61 72 65 02 02 03 01 03 62 65 74 02 02 07 08 3are.....bet.... +| 3088: 1b 88 80 80 80 80 0d 03 00 3c 00 00 00 16 03 32 .........<.....2 +| 3104: 74 68 02 08 02 01 01 07 00 04 33 61 6e 64 02 06 th........3and.. +| 3120: 04 0a 1b 88 80 80 80 80 0c 03 00 3c 00 00 00 16 ...........<.... +| 3136: 03 32 69 6e 02 06 01 01 06 01 02 72 6f 02 06 01 .2in.......ro... +| 3152: 01 05 04 09 18 88 80 80 80 80 0b 03 00 36 00 00 .............6.. +| 3168: 00 13 02 03 32 61 72 02 02 03 01 02 62 65 02 02 ....2ar.....be.. +| 3184: 04 05 07 1b 88 80 80 80 80 0a 03 00 3c 00 00 00 ............<... +| 3200: 16 12 31 74 02 08 02 01 01 07 00 03 32 61 6e 02 ..1t........2an. +| 3216: 06 01 01 04 09 19 88 80 80 80 80 09 03 00 38 00 ..............8. +| 3232: 00 00 14 02 31 6e 02 06 01 01 03 01 01 72 02 06 ....1n.......r.. +| 3248: 01 01 05 04 08 17 78 80 80 80 80 08 03 00 34 10 ......x.......4. +| 3264: 01 00 12 02 31 62 02 02 04 01 01 69 02 06 01 01 ....1b.....i.... +| 3280: 06 04 06 19 88 80 80 80 80 07 03 00 38 00 00 00 ............8... +| 3296: 14 04 02 31 32 02 02 05 01 01 61 02 08 03 01 01 ...12.....a..... +| 3312: 02 05 06 1b 88 80 80 80 80 06 03 00 3c 00 00 00 ............<... +| 3328: 16 06 30 74 68 65 72 65 e7 02 02 00 02 31 31 02 ..0there.....11. +| 3344: 06 01 01 04 0a 15 88 80 80 80 80 05 03 00 30 00 ..............0. +| 3360: 00 00 11 01 01 05 e5 30 74 68 65 02 06 01 01 07 .......0the..... +| 3376: 07 1c 88 80 80 80 80 04 03 00 3e 00 00 00 17 01 ..........>..... +| 3392: 01 06 02 30 6e 02 06 01 01 03 01 04 72 6f 77 73 ...0n.......rows +| 3408: 02 06 07 08 1b 88 80 80 80 80 03 03 00 3c 00 00 .............<.. +| 3424: 00 16 08 30 62 65 74 77 65 65 6e 02 02 04 01 02 ...0between..... +| 3440: 69 6e 02 06 04 0c 1a 88 80 80 80 80 02 03 00 3a in.............: +| 3456: 00 00 00 15 04 30 61 6e 64 02 06 01 01 02 02 02 .....0and....... +| 3472: 72 65 02 02 03 04 0a 17 88 80 80 80 80 01 03 00 re.............. +| 3488: 34 01 00 00 12 02 30 31 02 06 01 01 04 01 01 32 4.....01.......2 +| 3504: 02 02 05 04 08 08 84 80 80 80 80 12 03 00 16 00 ................ +| 3520: 00 00 05 04 1b 84 80 80 80 80 11 03 00 3c 00 00 .............<.. +| 3536: 00 16 05 34 74 61 62 6c 01 06 01 01 05 02 03 65 ...4tabl.......e +| 3552: 72 6d 01 02 04 0b 1b 84 80 80 80 80 10 03 00 3c rm.............< +| 3568: 00 00 00 16 05 34 65 61 63 68 01 02 02 01 04 70 .....4each.....p +| 3584: 72 65 73 01 02 05 04 09 1a 84 80 80 80 80 0f 03 res............. +| 3600: 00 3a 00 00 00 15 04 33 74 65 72 01 02 04 02 02 .:.....3ter..... +| 3616: 68 65 01 06 01 01 03 04 08 1b 84 80 80 80 80 0e he.............. +| 3632: 03 00 3c 00 00 00 16 04 33 70 72 65 01 02 05 01 ..<.....3pre.... +| 3648: 03 74 61 62 01 06 01 01 05 04 08 1a 84 80 80 80 .tab............ +| 3664: 80 0d 03 0d 1a 00 00 00 15 04 33 66 6e 72 01 02 ..........3fnr.. +| 3680: 02 02 02 74 73 01 06 01 01 04 04 08 1b 84 80 80 ...ts........... +| 3696: 80 80 0c 03 00 3c 00 00 00 16 03 32 74 68 01 06 .....<.....2th.. +| 3712: 01 01 03 00 04 33 65 61 63 01 02 03 04 09 18 84 .....3eac....... +| 3728: 80 80 80 80 0b 03 00 36 00 00 00 13 03 32 74 61 .......6.....2ta +| 3744: 01 06 01 01 05 02 01 65 01 02 04 04 09 19 84 80 .......e........ +| 3760: 80 80 80 0a 03 00 38 00 00 00 14 03 30 c9 6e 01 ......8.....0.n. +| 3776: 06 01 01 02 01 02 70 72 01 02 05 04 09 18 84 80 ......pr........ +| 3792: 80 80 80 09 03 00 36 00 00 00 13 03 32 66 6f 01 ......6.....2fo. +| 3808: 02 0b e2 01 74 01 06 01 01 04 04 07 1b 84 80 80 ....t........... +| 3824: 86 f0 08 03 00 3c 00 00 00 16 02 31 74 01 0a 04 .....<.....1t... +| 3840: 01 01 03 04 00 03 32 65 61 01 02 03 04 0a 17 84 ......2ea....... +| 3856: 80 80 80 80 07 03 00 34 00 00 00 12 02 31 69 01 .......4.....1i. +| 3872: 06 01 01 02 01 01 70 01 02 05 04 08 18 84 80 80 ......p......... +| 3888: 80 80 06 03 00 36 00 00 00 13 02 31 65 01 02 03 .....6.....1e... +| 3904: 01 01 66 01 08 02 01 01 04 04 06 1b 84 80 80 80 ..f............. +| 3920: 80 05 03 00 3c 00 00 00 16 05 30 74 65 72 6d 01 ....<.....0term. +| 3936: 02 04 02 02 68 65 01 06 01 01 03 04 09 14 84 80 ....he.......... +| 3952: 80 80 80 04 03 00 2e 00 00 00 10 06 30 74 61 62 ............0tab +| 3968: 6c 65 01 06 01 01 05 04 15 84 7f 80 80 80 03 03 le.............. +| 3984: 00 30 00 00 00 11 02 08 30 70 72 65 73 65 6e 74 .0......0present +| 4000: 01 02 05 05 1b 84 80 22 80 80 02 03 00 3c 00 00 .............<.. +| 4016: 00 16 04 30 66 74 73 01 06 01 01 04 01 02 69 6e ...0fts.......in +| 4032: 01 06 01 01 04 0a 1a 84 80 80 80 80 01 03 00 3a ...............: +| 4048: 00 00 00 15 05 30 65 61 63 68 01 02 03 01 03 66 .....0each.....f +| 4064: 6f 72 01 02 02 04 09 06 01 03 00 12 03 0b 0f 00 or.............. +| 4080: 00 08 8c 80 80 80 80 11 03 00 16 00 00 00 05 04 ................ +| page 3 offset 8192 +| 0: 0a 00 00 00 32 0e 4f 00 0f fa 0f f1 0f e9 0f e1 ....2.O......... +| 16: 0f d8 0f d1 0f c9 0f c1 0f b9 0f b1 0f a9 0f a0 ................ +| 32: 0f 98 0f 90 0f 87 0f 80 0f 78 0f 71 0f 68 0f 5f .........x.q.h._ +| 48: 0f 56 0f 00 00 00 00 00 00 00 00 00 00 00 00 00 .V.............. +| 3392: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 08 ................ +| 3408: 04 01 10 01 03 34 74 20 07 04 01 0e 01 03 34 1e .....4t ......4. +| 3424: 09 04 01 12 01 03 33 74 68 1c 08 04 01 10 01 03 ......3th....... +| 3440: 34 6e 1a 08 04 01 10 01 03 32 67 18 08 04 01 10 4n.......2g..... +| 3456: 01 03 32 74 16 08 04 01 10 01 03 32 6e 14 07 04 ..2t.......2n... +| 3472: 01 0e 01 03 32 12 08 04 01 10 01 03 31 74 10 08 ....2.......1t.. +| 3488: 04 01 10 01 03 31 6e 0e 07 04 01 0e 01 03 31 0c .....1n.......1. +| 3504: 09 04 01 12 01 03 30 74 68 0a 08 04 01 10 01 03 ......0th....... +| 3520: 30 74 08 09 04 01 12 01 03 30 6e 75 06 08 04 01 0t.......0nu.... +| 3536: 10 01 03 30 6e 04 06 04 01 0c 01 03 02 08 04 01 ...0n........... +| 3552: 10 01 02 34 72 22 07 04 01 0e 01 02 34 20 08 04 ...4r.......4 .. +| 3568: 01 10 01 02 33 72 1e 09 04 01 12 01 02 33 61 72 ....3r.......3ar +| 3584: 1c 18 04 01 10 01 02 32 74 1a 08 04 01 10 01 02 .......2t....... +| 3600: 32 69 18 09 04 01 12 01 02 32 61 72 16 08 04 01 2i.......2ar.... +| 3616: 10 01 02 31 74 2a 08 04 01 0f 01 02 31 6e 12 0a ...1t*......1n.. +| 3632: d4 01 10 01 02 31 62 10 08 04 01 10 01 02 31 32 .....1b.......12 +| 3648: 0e 0b 04 01 16 01 02 30 74 68 65 72 0c 08 04 01 .......0ther.... +| 3664: 10 01 02 30 74 0a 08 04 01 10 01 02 30 6e 08 08 ...0t.......0n.. +| 3680: 04 01 10 01 02 30 62 06 08 04 01 10 01 02 30 61 .....0b.......0a +| 3696: 04 06 03 f1 0c 01 02 02 07 04 09 10 01 34 74 22 .............4t. +| 3712: 06 04 09 0e 01 34 20 08 04 09 12 01 33 74 65 1e .....4 .....3te. +| 3728: 07 04 09 10 01 33 70 1c 07 04 09 10 01 33 66 1a .....3p......3f. +| 3744: 08 04 09 12 01 32 74 68 18 07 04 09 10 01 32 74 .....2th......2t +| 3760: 16 07 04 09 10 01 32 69 14 07 04 09 10 01 32 66 ......2i......2f +| 3776: 12 07 04 09 10 01 31 74 10 07 04 09 10 01 31 69 ......1t......1i +| 3792: 0e 06 04 09 0e 01 31 0c 08 04 09 12 01 30 74 65 ......1......0te +| 3808: 0a 07 04 09 10 01 30 74 08 07 04 09 10 01 30 70 ......0t......0p +| 3824: 06 08 04 09 1e 61 30 66 74 04 05 00 00 00 00 00 .....a0ft....... +| page 4 offset 12288 +| 4064: 00 00 00 00 00 00 00 00 00 00 00 05 03 03 00 10 ................ +| 4080: 03 05 05 02 03 00 10 04 06 05 01 03 00 10 04 04 ................ +| page 5 offset 16384 +| 0: 0a 00 00 00 02 0f eb 00 0f eb 0f f4 00 00 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 08 03 15 01 70 ...............p +| 4080: 67 73 7a 18 0b 03 1b 01 76 65 72 73 69 6f 6e 04 gsz.....version. +| end crash-ef6738247b1344.db +}]} {} + + +do_catchsql_test 4.1 { + BEGIN; + REPLACE INTO t1(rowid,b,a,rowid) VALUES(200,1,2,3); +} {1 {database disk image is malformed}} + +do_catchsql_test 4.2 { + INSERT INTO t1(t1) VALUES('delete-all'); +} {1 {database disk image is malformed}} +do_catchsql_test 4.3 { + REPLACE INTO t1(rowid,b,rowid,a) VALUES(200,1,2,3); +} {1 {database disk image is malformed}} +do_catchsql_test 4.4 { + REPLACE INTO t1(rowid,b,a,rowid) VALUES(0,1,2,3); +} {1 {database disk image is malformed}} +do_catchsql_test 4.5 { + REPLACE INTO t1(rowid,a,b,rowid) VALUES(200,1,2,3); +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +reset_db +do_test 5.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 28672 pagesize 4096 filename crash-0c6d3451d11597.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 07 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 07 00 00 00 04 ................ +| 96: 00 00 00 00 0d 00 00 00 07 0d d2 00 0f c4 0f 6d ...............m +| 112: 0f 02 0e ab 0e 4e 0d f6 0d d2 00 00 00 00 00 00 .....N.......... +| 3536: 00 00 22 07 06 17 11 11 01 31 74 61 62 6c 65 74 .........1tablet +| 3552: 32 74 32 07 43 52 45 41 54 45 20 54 41 42 4c 45 2t2.CREATE TABLE +| 3568: 20 74 32 28 78 29 56 06 06 17 1f 1f 01 7d 74 61 t2(x)V.......ta +| 3584: 62 6c 65 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 blet1_configt1_c +| 3600: 6f 6e 66 69 67 06 43 52 45 41 54 45 20 54 41 42 onfig.CREATE TAB +| 3616: 4c 45 20 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b LE 't1_config'(k +| 3632: 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 PRIMARY KEY, v) +| 3648: 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 5b 05 WITHOUT ROWID[. +| 3664: 07 17 21 21 01 81 01 74 61 62 6c 65 74 31 5f 64 ..!!...tablet1_d +| 3680: 6f 63 73 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 ocsizet1_docsize +| 3696: 05 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 .CREATE TABLE 't +| 3712: 31 5f 64 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 1_docsize'(id IN +| 3728: 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE +| 3744: 59 2c 20 73 7a 20 42 4c 4f 42 29 55 04 06 17 21 Y, sz BLOB)U...! +| 3760: 21 01 77 74 61 62 6c 65 74 31 5f 63 6f 6e 74 65 !.wtablet1_conte +| 3776: 6e 74 74 31 5f 63 6f 6e 74 65 6e 74 04 43 52 45 ntt1_content.CRE +| 3792: 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 6f ATE TABLE 't1_co +| 3808: 6e 74 65 6e 74 27 28 69 64 20 49 4e 54 45 47 45 ntent'(id INTEGE +| 3824: 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 63 R PRIMARY KEY, c +| 3840: 30 29 69 03 07 17 19 19 01 81 2d 74 61 62 6c 65 0)i.......-table +| 3856: 74 31 5f 69 64 78 74 31 5f 69 64 78 03 43 52 45 t1_idxt1_idx.CRE +| 3872: 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 69 64 ATE TABLE 't1_id +| 3888: 78 27 28 73 65 67 69 64 2c 20 74 65 72 6d 2c 20 x'(segid, term, +| 3904: 70 67 6e 6f 2c 20 50 52 49 4d 41 52 59 20 4b 45 pgno, PRIMARY KE +| 3920: 59 28 73 65 67 69 64 2c 20 74 65 72 6d 29 29 20 Y(segid, term)) +| 3936: 57 49 54 48 4f 55 54 20 52 4f 57 49 44 55 02 07 WITHOUT ROWIDU.. +| 3952: 17 1b 1b 01 81 01 74 61 62 6c 65 74 31 5f 64 61 ......tablet1_da +| 3968: 74 61 74 31 5f 64 61 74 61 02 43 52 45 41 54 45 tat1_data.CREATE +| 3984: 20 54 41 42 4c 45 20 27 74 31 5f 64 61 74 61 27 TABLE 't1_data' +| 4000: 28 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d (id INTEGER PRIM +| 4016: 41 52 b9 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 AR. KEY, block B +| 4032: 4c 4f 42 29 3a 01 06 17 11 11 08 63 74 61 62 6c LOB):......ctabl +| 4048: 65 74 31 74 31 43 52 45 41 54 45 20 56 49 52 54 et1t1CREATE VIRT +| 4064: 55 41 4c 20 54 41 42 4c 45 20 74 31 20 55 53 49 UAL TABLE t1 USI +| 4080: 4e 47 20 66 74 73 35 28 63 6f 6e 74 65 6e 74 29 NG fts5(content) +| page 2 offset 4096 +| 0: 0d 00 00 00 03 0f bd 00 0f e8 0f ef 0f bd 00 00 ................ +| 16: 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 4016: 00 00 00 00 00 00 00 00 00 00 00 00 00 24 84 80 .............$.. +| 4032: 80 80 80 01 03 00 4e 00 00 00 1e 06 30 61 62 61 ......N.....0aba +| 4048: 63 6b 01 02 02 04 02 66 74 02 02 02 04 04 6e 64 ck.....ft.....nd +| 4064: 6f 6e 03 02 02 04 0a 07 05 01 03 00 10 03 03 0f on.............. +| 4080: 0a 03 00 24 00 00 00 00 01 01 01 00 01 01 01 11 ...$............ +| page 3 offset 8192 +| 0: 0a 00 00 00 01 0f 00 00 00 00 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 05 04 09 0c 01 02 ................ +| page 4 offset 12288 +| 0: 0d 00 00 00 03 0f e0 00 0f f6 0f ec 0f e0 00 00 ................ +| 4064: 0a 03 03 00 1b 61 62 61 6e 64 6f 6e 08 02 03 00 .....abandon.... +| 4080: 17 61 62 61 66 74 08 01 03 00 17 61 62 61 63 6b .abaft.....aback +| page 5 offset 16384 +| 0: 0d 00 00 00 03 0f ee 00 0f fa 0f f4 0f ee 00 00 ................ +| 16: 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 04 03 ................ +| 4080: 03 00 0e 01 04 02 03 00 0e 01 04 01 03 00 0e 01 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| page 7 offset 24576 +| 0: 0d 00 00 10 03 0f d6 00 0f f4 10 e1 0f d6 00 00 ................ +| 16: 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 4048: 00 00 00 00 00 00 09 03 02 1b 72 65 62 75 69 6c ..........rebuil +| 4064: 64 11 02 02 2b 69 6e 74 65 67 72 69 74 79 2d 63 d...+integrity-c +| 4080: 68 65 63 6b 0a 01 02 1d 6f 70 74 69 6d 00 00 00 heck....optim... +| end crash-0c6d3451d11597.db +}]} {} + +do_execsql_test 5.1 { + INSERT INTO t1(t1,rank) VALUES('secure-delete',1); +} +do_catchsql_test 5.4 { + UPDATE t1 SET content=randomblob(500); +} {/.*fts5: corrupt.*/} + +#------------------------------------------------------------------------- +reset_db +do_test 6.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 32768 pagesize 4096 filename crash-42fa37b694d45a.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 07 .....@ ........ +| 96: 00 00 00 00 0d 00 00 00 07 0d d2 00 0f c4 0f 6d ...............m +| 112: 0f 02 0e ab 0e 4e 0d f6 0d d2 00 00 00 00 00 00 .....N.......... +| 3536: 00 00 22 07 06 17 11 11 01 31 74 61 62 6c 65 74 .........1tablet +| 3552: 32 74 32 07 43 52 45 41 54 45 20 54 41 42 4c 45 2t2.CREATE TABLE +| 3568: 20 74 32 28 78 29 56 06 06 17 1f 1f 01 7d 74 61 t2(x)V.......ta +| 3584: 62 6c 65 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 blet1_configt1_c +| 3600: 6f 6e 66 69 67 06 43 52 45 41 54 45 20 54 41 42 onfig.CREATE TAB +| 3616: 4c 45 20 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b LE 't1_config'(k +| 3632: 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 PRIMARY KEY, v) +| 3648: 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 5b 05 WITHOUT ROWID[. +| 3664: 07 17 21 21 01 81 01 74 61 62 6c 65 74 31 5f 64 ..!!...tablet1_d +| 3680: 6f 63 73 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 ocsizet1_docsize +| 3696: 05 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 .CREATE TABLE 't +| 3712: 31 5f 64 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 1_docsize'(id IN +| 3728: 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE +| 3744: 59 2c 20 73 7a 20 42 4c 4f 42 29 55 04 06 17 21 Y, sz BLOB)U...! +| 3760: 21 01 77 74 61 62 6c 65 74 31 5f 63 6f 6e 74 65 !.wtablet1_conte +| 3776: 6e 74 74 31 5f 63 6f 6e 74 65 6e 74 04 43 52 45 ntt1_content.CRE +| 3792: 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 6f ATE TABLE 't1_co +| 3808: 6e 74 65 6e 74 27 28 69 64 20 49 4e 54 45 47 45 ntent'(id INTEGE +| 3824: 52 20 50 52 49 4d 41 52 49 20 4b 45 59 2c 20 63 R PRIMARI KEY, c +| 3840: 30 29 69 03 07 17 19 19 01 81 2d 74 61 62 6c 65 0)i.......-table +| 3856: 74 31 5f 69 64 78 74 31 5f 69 64 78 03 43 52 45 t1_idxt1_idx.CRE +| 3872: 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 69 64 ATE TABLE 't1_id +| 3888: 78 27 28 73 65 67 69 64 2c 20 74 65 72 6d 2c 20 x'(segid, term, +| 3904: 70 67 6e 6f 2c 20 50 52 49 4d 41 52 59 20 4b 45 pgno, PRIMARY KE +| 3920: 59 28 73 65 67 69 64 2c 20 74 65 72 6d 29 29 20 Y(segid, term)) +| 3936: 57 49 54 48 4f 55 54 20 52 4f 57 49 44 55 02 07 WITHOUT ROWIDU.. +| 3952: 17 1b 1b 01 81 01 74 61 62 6c 65 74 31 5f 64 61 ......tablet1_da +| 3968: 74 61 74 31 5f 64 61 74 61 02 43 52 45 41 54 45 tat1_data.CREATE +| 3984: 20 54 41 42 4c 45 20 27 74 31 5f 64 61 74 61 27 TABLE 't1_data' +| 4000: 28 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d (id INTEGER PRIM +| 4016: 41 52 b9 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 AR. KEY, block B +| 4032: 4c 4f 42 29 3a 01 06 17 11 11 08 63 74 61 62 6c LOB):......ctabl +| 4048: 65 74 31 74 31 43 52 45 41 54 45 20 56 49 52 54 et1t1CREATE VIRT +| 4064: 55 41 4c 20 54 41 42 4c 45 20 74 31 20 55 53 49 UAL TABLE t1 USI +| 4080: 4e 47 20 66 74 73 35 28 63 6f 6e 74 65 6e 74 29 NG fts5(content) +| page 2 offset 4096 +| 0: 0d 00 00 00 03 0f bd 00 0f e8 0f ef 0f bd f0 00 ................ +| 16: 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 4016: 00 00 00 00 00 00 00 00 00 00 00 00 00 24 84 80 .............$.. +| 4032: 80 80 80 01 03 00 4e 00 10 00 1e 06 30 61 62 61 ......N.....0aba +| 4048: 63 6c 01 02 02 04 02 66 74 02 5f 02 04 04 6e 64 cl.....ft._...nd +| 4064: 6f 6e 02 02 02 04 0a 07 05 01 03 00 10 03 03 0f on.............. +| 4080: 0a 03 00 24 00 00 00 00 01 01 01 00 01 01 01 11 ...$............ +| page 3 offset 8192 +| 0: 0a 00 00 00 01 0f 00 01 00 00 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 05 04 09 0c 01 02 ................ +| page 4 offset 12288 +| 0: 0d 00 00 00 03 0f e0 00 0f f6 0f ec 0f e0 00 00 ................ +| 4064: 0a 03 03 00 1b 61 62 61 6e 64 6f 6e 08 02 03 00 .....abandon.... +| 4080: 17 61 62 61 66 74 08 01 03 00 17 61 62 61 63 6b .abaft.....aback +| page 5 offset 16384 +| 0: 0d 00 00 00 03 0f ee 00 0f fa 0f 00 00 00 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 04 03 ................ +| 4080: 03 00 0e 01 04 02 03 00 0e 01 04 01 03 00 0e 01 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| page 7 offset 24576 +| 0: 0d 00 00 10 03 0f d6 00 0f 00 00 00 00 00 00 00 ................ +| 4048: 00 00 00 00 00 00 09 03 02 1b 72 65 62 75 69 6c ..........rebuil +| 4064: 64 11 02 02 2b 69 6e 74 65 67 72 69 74 79 2d 63 d...+integrity-c +| 4080: 68 65 63 6b 0a 01 02 1d 6f 70 74 69 6d 00 00 00 heck....optim... +| page 8 offset 28672 +| 0: 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| end crash-42fa37b694d45a.db +}]} {} + +do_execsql_test 6.1 { + INSERT INTO t1(t1,rank) VALUES('secure-delete',1); +} +do_catchsql_test 6.2 { + UPDATE t1 SET content=randomblob(500) WHERE t1; +} {1 {constraint failed}} + +#------------------------------------------------------------------------- +reset_db +do_test 7.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 40960 pagesize 4096 filename crash-d8b4a99207c10b.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 0a .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 0d 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 96: 00 00 00 00 0d 00 00 00 0d 0b 62 00 0f 97 0f 40 ..........b....@ +| 112: 0e d5 0e 75 0e 18 0d c0 0d 66 0d 0f 0c a4 0c 44 ...u.....f.....D +| 128: 0b ec 0b a7 0b 62 00 00 00 00 00 00 00 00 00 00 .....b.......... +| 2912: 00 00 43 0d 06 17 11 11 08 75 74 61 62 6c 65 74 ..C......utablet +| 2928: 34 74 34 43 52 45 41 54 45 20 56 49 52 54 55 41 4t4CREATE VIRTUA +| 2944: 4c 20 54 41 42 4c 45 20 74 34 20 55 53 49 4e 47 L TABLE t4 USING +| 2960: 20 66 74 73 35 76 6f 63 61 62 28 27 74 32 27 2c fts5vocab('t2', +| 2976: 20 27 72 6f 77 27 29 43 0c 06 17 11 11 08 75 74 'row')C......ut +| 2992: 61 62 6c 65 74 33 74 33 43 52 45 41 54 45 20 56 ablet3t3CREATE V +| 3008: 49 52 54 55 41 4c 20 54 41 42 4c 45 20 74 33 20 IRTUAL TABLE t3 +| 3024: 55 53 49 4e 47 20 66 74 73 35 76 6f 63 61 62 28 USING fts5vocab( +| 3040: 27 74 31 27 2c 20 27 72 6f 77 27 29 56 0b 06 17 't1', 'row')V... +| 3056: 1f 1f 01 7d 74 61 62 6c 65 74 32 5f 63 6f 6e 66 ....tablet2_conf +| 3072: 69 67 74 32 5f 63 6f 6e 66 69 67 0a 43 52 45 41 igt2_config.CREA +| 3088: 54 45 20 54 41 42 4c 45 20 27 74 32 5f 63 6f 6e TE TABLE 't2_con +| 3104: 66 69 67 27 28 6b 20 50 52 49 4d 41 52 59 20 4b fig'(k PRIMARY K +| 3120: 45 59 2c 20 76 29 20 57 49 54 48 4f 55 54 20 52 EY, v) WITHOUT R +| 3136: 4f 57 49 44 5e 0a 07 17 21 21 01 81 07 74 61 62 OWID^...!!...tab +| 3152: 6c 65 74 32 5f 63 6f 6e 74 65 6e 74 74 32 5f 63 let2_contentt2_c +| 3168: 6f 6e 74 65 6e 74 09 43 52 45 41 54 45 20 54 41 ontent.CREATE TA +| 3184: 42 4c 45 20 27 74 32 5f 63 6f 6e 74 65 6e 74 27 BLE 't2_content' +| 3200: 28 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d (id INTEGER PRIM +| 3216: 41 52 59 20 4b 45 59 2c 20 63 30 2c 20 63 31 2c ARY KEY, c0, c1, +| 3232: 20 63 32 29 69 09 07 17 19 19 01 81 2d 74 61 62 c2)i.......-tab +| 3248: 6c 65 74 32 5f 69 64 78 74 32 5f 69 64 78 08 43 let2_idxt2_idx.C +| 3264: 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 32 5f REATE TABLE 't2_ +| 3280: 69 64 78 27 28 73 65 67 69 64 2c 20 74 65 72 6d idx'(segid, term +| 3296: 2c 20 70 67 6e 6f 2c 20 50 52 49 4d 41 52 59 20 , pgno, PRIMARY +| 3312: 4b 45 59 28 73 65 67 69 64 2c 20 74 65 72 6d 29 KEY(segid, term) +| 3328: 29 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 55 ) WITHOUT ROWIDU +| 3344: 08 07 17 1b 1b 01 81 01 74 61 62 6c 65 74 32 5f ........tablet2_ +| 3360: 64 61 74 61 74 32 5f 64 61 74 61 07 43 52 45 41 datat2_data.CREA +| 3376: 54 45 20 54 41 42 4c 45 20 27 74 32 5f 64 61 74 TE TABLE 't2_dat +| 3392: 61 27 28 69 64 20 49 4e 54 45 47 45 52 20 50 52 a'(id INTEGER PR +| 3408: 49 4d 41 52 59 20 4b 45 59 2c 20 62 6c 6f 63 6b IMARY KEY, block +| 3424: 20 42 4c 4f 42 29 58 07 07 17 11 11 08 81 1d 74 BLOB)X........t +| 3440: 61 62 6c 65 74 32 74 32 43 52 45 41 54 45 20 56 ablet2t2CREATE V +| 3456: 49 52 54 55 41 4c 20 54 41 42 4c 45 20 74 32 20 IRTUAL TABLE t2 +| 3472: 55 53 49 4e 47 20 66 74 73 35 28 27 61 27 2c 5b USING fts5('a',[ +| 3488: 62 5d 2c 22 63 22 2c 64 65 74 61 69 6c 3d 6e 6f b],.c.,detail=no +| 3504: 6e 65 2c 63 6f 6c 75 6d 6e 73 69 7a 65 3d 30 29 ne,columnsize=0) +| 3520: 56 06 06 17 1f 1f 01 7d 74 61 62 6c 65 74 31 5f V.......tablet1_ +| 3536: 63 6f 6e 66 69 67 74 31 5f 63 6f 6e 66 69 67 06 configt1_config. +| 3552: 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 CREATE TABLE 't1 +| 3568: 5f 63 6f 6e 66 69 67 27 28 6b 20 50 52 49 4d 41 _config'(k PRIMA +| 3584: 52 59 20 4b 45 59 2c 20 76 29 20 57 49 54 48 4f RY KEY, v) WITHO +| 3600: 55 54 20 52 4f 57 49 44 5b 05 07 17 21 21 01 81 UT ROWID[...!!.. +| 3616: 01 74 61 62 6c 65 74 31 5f 64 6f 63 73 69 7a 65 .tablet1_docsize +| 3632: 74 31 5f 64 6f 63 73 69 7a 65 05 43 52 45 41 54 t1_docsize.CREAT +| 3648: 45 20 54 41 42 4c 45 20 27 74 31 5f 64 6f 63 73 E TABLE 't1_docs +| 3664: 69 7a 65 27 28 69 64 20 49 4e 54 45 47 45 52 20 ize'(id INTEGER +| 3680: 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 73 7a 20 PRIMARY KEY, sz +| 3696: 42 4c 4f 42 29 5e 04 07 17 21 21 01 81 07 74 61 BLOB)^...!!...ta +| 3712: 62 6c 65 74 31 5f 63 6f 6e 74 65 6e 74 74 31 5f blet1_contentt1_ +| 3728: 63 6f 6e 74 65 6e 74 04 43 52 45 41 54 45 20 54 content.CREATE T +| 3744: 41 42 4c 45 20 27 74 31 5f 63 6f 6e 74 65 6e 74 ABLE 't1_content +| 3760: 27 28 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 '(id INTEGER PRI +| 3776: 4d 41 52 59 20 4b 45 59 2c 20 63 30 2c 20 63 31 MARY KEY, c0, c1 +| 3792: 2c 20 63 32 29 69 03 07 17 19 19 01 81 2d 74 61 , c2)i.......-ta +| 3808: 62 6c 65 74 31 5f 69 64 78 74 31 5f 69 64 78 03 blet1_idxt1_idx. +| 3824: 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 CREATE TABLE 't1 +| 3840: 5f 69 64 78 27 28 73 65 67 69 64 2c 20 74 65 72 _idx'(segid, ter +| 3856: 6d 2c 20 70 67 6e 6f 2c 20 50 52 49 4d 41 52 59 m, pgno, PRIMARY +| 3872: 20 4b 45 59 28 73 65 67 69 64 2c 20 74 65 72 6d KEY(segid, term +| 3888: 29 29 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 )) WITHOUT ROWID +| 3904: 55 02 07 17 1b 1b 01 81 01 74 61 62 6c 65 74 31 U........tablet1 +| 3920: 5f 64 61 74 61 74 31 5f 64 61 74 61 02 43 52 45 _datat1_data.CRE +| 3936: 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 64 61 ATE TABLE 't1_da +| 3952: 74 61 27 28 69 64 20 49 4e 54 45 47 45 52 20 50 ta'(id INTEGER P +| 3968: 52 49 4d 41 52 59 20 4b 45 59 2c 20 62 6c 6f 63 RIMARY KEY, bloc +| 3984: 6b 20 42 4c 4f 42 29 67 01 07 17 11 11 08 81 3b k BLOB)g.......; +| 4000: 74 61 62 6c 65 74 31 74 31 43 52 45 41 54 45 20 tablet1t1CREATE +| 4016: 56 49 52 54 55 41 4c 20 54 41 42 4c 45 20 74 31 VIRTUAL TABLE t1 +| 4032: 20 55 53 49 4e 47 20 66 74 73 35 28 61 2c 62 20 USING fts5(a,b +| 4048: 75 6e 69 6e 64 65 78 65 64 2c 63 2c 74 6f 6b 65 unindexed,c,toke +| 4064: 6e 69 7a 65 3d 22 70 6f 72 74 65 72 20 61 73 63 nize=.porter asc +| 4080: 69 69 22 2c 74 6f 6b 65 6e 64 61 74 61 3d 31 29 ii.,tokendata=1) +| page 2 offset 4096 +| 0: 0d 0f 68 00 05 0f 13 00 0f e6 0f 13 0f a8 0f 7c ..h............| +| 16: 0f 2a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 .*.............. +| 3856: 00 00 00 15 0a 03 00 30 00 00 00 00 01 03 03 00 .......0........ +| 3872: 03 01 01 01 02 01 01 03 01 01 37 8c 80 80 80 80 ..........7..... +| 3888: 01 03 00 74 00 00 00 2e 02 30 61 03 02 02 01 01 ...t.....0a..... +| 3904: 62 03 02 03 01 01 63 03 02 04 01 01 67 03 06 01 b.....c.....g... +| 3920: 02 02 01 01 68 03 06 01 02 03 01 01 69 03 06 01 ....h.......i... +| 3936: 02 04 04 06 06 06 08 08 0f ef 00 14 2a 00 00 00 ............*... +| 3952: 00 01 02 02 00 02 01 01 01 02 01 01 25 88 80 80 ............%... +| 3968: 80 80 01 03 00 50 00 00 00 1f 02 30 67 02 08 02 .....P.....0g... +| 3984: 01 02 02 01 01 68 02 08 03 01 02 03 01 01 69 02 .....h........i. +| 4000: 08 04 01 02 04 04 09 09 37 84 80 80 80 7f f1 03 ........7....... +| 4016: 00 74 00 00 00 2e 02 30 61 01 02 02 01 01 62 01 .t.....0a.....b. +| 4032: 02 03 01 01 63 01 02 04 01 01 67 01 06 01 02 02 ....c.....g..... +| 4048: 01 01 68 01 06 01 02 03 01 01 69 01 06 01 02 04 ..h.......i..... +| 4064: 04 06 06 06 08 08 07 01 03 00 14 03 09 00 09 00 ................ +| 4080: 00 00 11 24 00 00 00 00 01 01 01 00 01 01 01 01 ...$............ +| page 3 offset 8192 +| 0: 0a 00 00 00 03 0f ec 00 0f fa 0f f3 0f ec 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 00 06 04 01 0c ................ +| 4080: 01 03 02 06 04 01 0c 01 02 02 05 04 09 0c 01 02 ................ +| page 4 offset 12288 +| 0: 0d 00 00 00 03 0f be 00 0f ea 0f d4 0f be 00 00 ................ +| 4016: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 14 03 ................ +| 4032: 05 00 17 17 17 61 20 62 20 63 67 20 68 20 69 67 .....a b cg h ig +| 4048: 20 68 20 69 14 02 05 00 17 17 17 67 20 68 20 69 h i.......g h i +| 4064: 61 20 62 20 63 67 20 68 20 69 14 01 05 00 17 17 a b cg h i...... +| 4080: 17 61 20 62 20 63 64 20 65 20 66 67 20 68 20 69 .a b cd e fg h i +| page 5 offset 16384 +| 0: 0d 00 00 00 03 0f e8 00 0f f8 0f f0 0f e8 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 06 03 03 00 12 03 00 03 ................ +| 4080: 06 02 03 00 12 03 00 03 06 01 03 00 12 03 00 03 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| page 7 offset 24576 +| 0: 0d 00 00 00 03 0f 9e 00 0f e6 0f ef 0f 9e 00 00 ................ +| 3984: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 41 84 ..............A. +| 4000: 80 80 80 80 01 04 00 81 06 00 00 00 34 02 30 61 ............4.0a +| 4016: 01 01 01 01 01 62 01 01 01 01 01 63 01 01 01 01 .....b.....c.... +| 4032: 01 64 01 01 01 65 01 01 01 66 01 01 01 67 01 01 .d...e...f...g.. +| 4048: 01 01 01 68 01 01 01 01 01 69 01 01 01 04 06 06 ...h.....i...... +| 4064: 06 04 04 04 06 06 07 01 03 00 14 03 09 09 09 0f ................ +| 4080: 0a 03 00 24 00 00 00 00 01 01 01 00 01 01 01 01 ...$............ +| page 8 offset 28672 +| 0: 0a 00 00 00 01 0f fa 00 0f fa 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 05 04 09 0c 01 02 ................ +| page 9 offset 32768 +| 0: 0d 00 00 00 03 0f be 00 0f ea 0f d4 0f be 00 00 ................ +| 4016: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 14 03 ................ +| 4032: 05 00 17 17 17 61 20 62 20 63 67 20 68 20 69 67 .....a b cg h ig +| 4048: 20 68 20 69 14 02 05 00 17 17 17 67 20 68 20 69 h i.......g h i +| 4064: 61 20 62 20 63 67 20 68 20 69 14 01 05 00 17 17 a b cg h i...... +| 4080: 17 61 20 62 20 63 64 20 65 20 66 67 20 68 20 69 .a b cd e fg h i +| page 10 offset 36864 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| end crash-d8b4a99207c10b.db +}]} {} + +do_catchsql_test 7.1 { + SELECT snippet(t1, -1, '.', '..', '[', ']'), + highlight(t1, 2, '[', ']') + FROM t1('g + h') + WHERE rank MATCH 'bm25(1.0, 1.0)' ORDER BY rank; +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +reset_db +do_test 8.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 20480 pagesize 4096 filename crash-d57c01958e48ab.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 05 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 05 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 96: 00 00 00 00 0d 00 00 00 05 0e 10 00 0f 97 0f 40 ...............@ +| 112: 0e d5 0e 68 0e 10 01 00 00 00 00 00 00 00 00 00 ...h............ +| 3600: 56 05 06 17 1f 1f 01 7d 74 61 62 6c 65 74 31 5f V.......tablet1_ +| 3616: 63 6f 6e 66 69 67 74 31 5f 63 6f 6e 66 69 67 05 configt1_config. +| 3632: 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 CREATE TABLE 't1 +| 3648: 5f 63 6f 6e 66 69 67 27 28 6b 20 50 52 49 4d 41 _config'(k PRIMA +| 3664: 52 59 20 4b 45 59 2c 20 76 29 20 57 49 54 48 4f RY KEY, v) WITHO +| 3680: 55 54 20 52 4f 57 49 44 6b 04 07 17 21 21 01 81 UT ROWIDk...!!.. +| 3696: 21 74 61 62 6c 65 74 31 5f 64 6f 63 73 69 7a 65 !tablet1_docsize +| 3712: 74 31 5f 64 6f 63 73 69 7a 65 04 43 52 45 41 54 t1_docsize.CREAT +| 3728: 45 20 54 41 42 4c 45 20 27 74 31 5f 64 6f 63 73 E TABLE 't1_docs +| 3744: 69 7a 65 27 28 69 64 20 49 4e 54 45 47 45 52 20 ize'(id INTEGER +| 3760: 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 73 7a 20 PRIMARY KEY, sz +| 3776: 42 4c 4f 42 2c 20 6f 72 69 67 69 6e 20 49 4e 54 BLOB, origin INT +| 3792: 45 47 45 52 29 69 03 07 17 19 19 01 81 2d 74 61 EGER)i.......-ta +| 3808: 62 6c 65 74 31 5f 69 64 78 74 31 5f 69 64 78 03 blet1_idxt1_idx. +| 3824: 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 CREATE TABLE 't1 +| 3840: 5f 69 64 78 27 28 73 65 67 69 64 2c 20 74 65 72 _idx'(segid, ter +| 3856: 6d 2c 20 70 67 6e 6f 2c 20 50 52 49 4d 41 52 59 m, pgno, PRIMARY +| 3872: 20 4b 45 59 28 73 65 67 69 64 2c 20 74 65 72 6d KEY(segid, term +| 3888: 29 29 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 )) WITHOUT ROWID +| 3904: 55 02 07 17 1b 1b 01 81 01 74 61 62 6c 65 74 31 U........tablet1 +| 3920: 5f 64 61 74 61 74 31 5f 64 61 74 61 02 43 52 45 _datat1_data.CRE +| 3936: 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 64 61 ATE TABLE 't1_da +| 3952: 74 61 27 28 69 64 20 49 4e 54 45 47 45 52 20 50 ta'(id INTEGER P +| 3968: 52 49 4d 41 52 59 20 4b 45 59 2c 20 62 6c 6f 63 RIMARY KEY, bloc +| 3984: 6b 20 42 4c 4f 42 29 67 01 07 17 11 11 08 81 3b k BLOB)g.......; +| 4000: 74 61 62 6c 65 74 31 74 31 43 52 45 41 54 45 20 tablet1t1CREATE +| 4016: 56 49 52 54 55 41 4c 20 54 41 42 4c 45 20 74 31 VIRTUAL TABLE t1 +| 4032: 20 55 53 49 4e 47 20 66 74 73 35 28 61 2c 20 62 USING fts5(a, b +| 4048: 2c 20 63 6f 6e 74 65 6e 74 3d 27 27 2c 20 63 6f , content='', co +| 4064: 6e 74 65 6e 74 6c 65 73 73 5f 64 65 6c 65 74 65 ntentless_delete +| 4080: 3d 31 2c 20 74 6f 6b 65 6e 64 61 74 61 3d 31 29 =1, tokendata=1) +| page 2 offset 4096 +| 0: 0d 0f eb 00 03 0e 17 00 0f e2 0e 17 0e 31 00 00 .............1.. +| 16: 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 3600: 00 00 00 00 00 00 00 18 0a 03 00 36 00 00 00 00 ...........6.... +| 3616: ff 00 00 01 01 01 01 00 01 01 01 01 01 01 00 00 ................ +| 3632: 07 83 29 84 80 80 80 80 01 04 00 86 56 00 00 01 ..).........V... +| 3648: 96 04 30 61 61 61 01 02 02 01 04 02 04 01 08 02 ..0aaa.......... +| 3664: 04 04 04 01 10 02 04 04 04 04 04 04 04 01 20 02 .............. . +| 3680: 04 04 04 04 04 04 04 04 04 04 04 04 04 04 04 01 ................ +| 3696: 40 02 04 04 04 04 04 04 04 04 04 04 04 04 04 04 @............... +| 3712: 04 04 04 04 04 04 04 04 04 04 04 04 04 04 04 04 ................ +| 3728: 04 01 81 00 02 04 04 04 04 04 04 04 04 04 04 04 ................ +| 3744: 04 04 04 04 04 04 04 04 04 04 04 04 04 04 04 04 ................ +| 3760: 04 04 04 04 04 04 04 04 04 04 04 04 04 04 04 04 ................ +| 3776: 04 04 04 04 04 04 04 04 04 04 04 04 04 04 04 04 ................ +| 3792: 04 04 04 04 02 02 62 63 01 06 01 01 02 01 03 62 ......bc.......b +| 3808: 62 62 02 02 03 01 04 03 06 01 08 03 06 06 06 01 bb.............. +| 3824: 10 03 06 06 06 06 06 06 06 01 20 03 06 06 06 06 .......... ..... +| 3840: 06 06 06 06 06 06 06 06 06 06 06 01 40 03 06 06 ............@... +| 3856: 06 06 06 06 06 06 06 06 06 06 06 06 06 06 06 06 ................ +| 3872: 06 06 06 06 06 06 06 06 06 06 16 06 06 02 02 63 ...............c +| 3888: 64 02 06 01 01 02 01 03 63 63 63 03 02 05 01 04 d.......ccc..... +| 3904: 05 0a 01 08 05 0a 0a 0a 01 10 05 0a 0a 0a 0a 0a ................ +| 3920: 0a 0a 01 20 05 0a 0a 0a 0a 0a 0a 0a 0a 0a 0a 0a ... ............ +| 3936: 0a 0a 0a 0a 02 02 64 65 03 06 01 01 02 01 03 64 ......de.......d +| 3952: 64 64 04 02 09 01 04 09 12 01 08 09 12 12 12 01 dd.............. +| 3968: 10 09 12 12 12 12 12 12 12 02 02 65 66 04 06 01 ...........ef... +| 3984: 01 02 01 03 65 65 65 05 02 11 01 04 11 22 01 08 ....eee......... +| 4000: 11 22 22 22 02 02 66 67 05 06 01 01 02 01 03 66 ......fg.......f +| 4016: 56 66 06 02 21 01 04 21 42 02 02 67 68 06 06 01 Vf..!..!B..gh... +| 4032: 01 02 cb 03 67 67 67 07 02 41 02 02 68 69 07 06 ....ggg..A..hi.. +| 4048: 01 01 02 04 81 13 09 50 09 2e 09 1c 09 12 09 0c .......P........ +| 4064: 09 08 07 01 03 00 14 07 81 77 07 00 00 00 15 22 .........w...... +| 4080: 00 00 00 00 ff 00 00 01 00 00 00 00 00 00 05 0c ................ +| page 3 offset 8192 +| 0: 0a 00 00 00 01 0f fa 00 0f fa 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 05 04 09 0c 01 02 ................ +| page 4 offset 12288 +| 0: 0d 00 00 00 07 0f c8 00 0f f8 0f f0 0f e8 0f e0 ................ +| 16: 0f d8 0f d0 0f c8 00 00 00 00 00 00 00 00 00 00 ................ +| 4032: 00 00 00 00 00 00 00 00 06 07 04 00 10 09 7f 01 ................ +| 4048: 06 06 04 00 10 09 3f 01 06 05 04 00 10 09 1f 01 ......?......... +| 4064: 06 04 04 00 10 09 0f 01 06 03 04 00 10 09 07 01 ................ +| 4080: 06 02 04 00 10 09 03 01 06 01 04 00 10 09 01 01 ................ +| page 5 offset 16384 +| 0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04 ........version. +| end crash-d57c01958e48ab.db +}]} {} + +do_catchsql_test 8.1 { + SELECT rowid FROM t1('a* NOT ý‘') ; +} {0 {1 2 3 4 5 6 7}} + +#------------------------------------------------------------------------- +reset_db +do_test 9.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 32768 pagesize 4096 filename crash-c76a16c24c8ba6.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 08 .....@ ........ +| 32: 00 00 00 02 00 00 00 01 00 00 00 09 00 00 00 04 ................ +| 96: 00 00 00 00 0d 0f c7 00 07 0d 92 00 0f 8d 0f 36 ...............6 +| 112: 0e cb 0e 6b 0e 0e 0d b6 0d 92 0d 92 00 00 00 00 ...k............ +| 3472: 00 00 22 08 06 17 11 11 01 31 74 61 62 6c 65 74 .........1tablet +| 3488: 32 74 32 08 43 52 45 41 54 45 20 54 41 42 4c 45 2t2.CREATE TABLE +| 3504: 20 74 32 28 78 29 56 07 06 17 1f 1f 01 7d 74 61 t2(x)V.......ta +| 3520: 62 6c 65 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 blet1_configt1_c +| 3536: 6f 6e 66 69 67 07 43 52 45 41 54 45 20 54 41 42 onfig.CREATE TAB +| 3552: 4c 45 20 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b LE 't1_config'(k +| 3568: 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 PRIMARY KEY, v) +| 3584: 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 5b 06 WITHOUT ROWID[. +| 3600: 07 17 21 21 01 81 01 74 61 62 6c 65 74 31 5f 64 ..!!...tablet1_d +| 3616: 6f 63 73 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 ocsizet1_docsize +| 3632: 06 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 .CREATE TABLE 't +| 3648: 31 5f 64 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 1_docsize'(id IN +| 3664: 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE +| 3680: 59 2c 20 73 7a 20 42 4c 4f 42 29 5e 05 07 17 21 Y, sz BLOB)^...! +| 3696: 21 01 81 07 74 61 62 6c 65 74 31 5f 63 6f 6e 74 !...tablet1_cont +| 3712: 65 6e 74 74 31 5f 63 6f 6e 74 65 6e 74 05 43 52 entt1_content.CR +| 3728: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 EATE TABLE 't1_c +| 3744: 6f 6e 74 65 6e 74 27 28 69 64 20 49 4e 54 45 47 ontent'(id INTEG +| 3760: 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 ER PRIMARY KEY, +| 3776: 63 30 2c 20 63 31 2c 20 63 32 29 69 04 07 17 19 c0, c1, c2)i.... +| 3792: 19 01 81 2d 74 61 62 6c 65 74 31 5f 69 64 78 74 ...-tablet1_idxt +| 3808: 31 5f 69 64 78 04 43 52 45 41 54 45 20 54 41 42 1_idx.CREATE TAB +| 3824: 4c 45 20 27 74 31 5f 69 64 78 27 28 73 65 67 69 LE 't1_idx'(segi +| 3840: 64 2c 20 74 65 72 6d 2c 20 70 67 6e 6f 2c 20 50 d, term, pgno, P +| 3856: 52 49 4d 41 52 59 20 4b 45 59 28 73 65 67 69 64 RIMARY KEY(segid +| 3872: 2c 20 74 65 72 6d 29 29 20 57 49 54 48 4f 55 54 , term)) WITHOUT +| 3888: 20 52 4f 57 49 44 55 03 07 17 1b 1b 01 81 01 74 ROWIDU........t +| 3904: 61 62 6c 65 74 31 5f 64 61 74 61 74 31 5f 64 61 ablet1_datat1_da +| 3920: 74 61 03 43 52 45 41 54 45 20 54 41 42 4c 45 20 ta.CREATE TABLE +| 3936: 27 74 31 5f 64 61 74 61 27 28 69 64 20 49 4e 54 't1_data'(id INT +| 3952: 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 EGER PRIMARY KEY +| 3968: 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 29 38 02 06 , block BLOB)8.. +| 3984: 17 11 11 08 5f 74 61 62 6c 65 74 31 74 31 43 52 ...._tablet1t1CR +| 4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 35 LE t1 USING fts5 +| 4032: 28 61 2c 62 2c 63 29 00 00 00 00 00 00 00 00 00 (a,b,c)......... +| page 3 offset 8192 +| 0: 0d 00 00 00 03 0c 94 00 0f e6 0f ef 0c 94 00 00 ................ +| 3216: 00 00 00 00 86 4a 84 80 80 80 80 01 04 00 8d 18 .....J.......... +| 3232: 00 00 03 2b 02 30 30 01 02 06 01 02 06 01 02 06 ...+.00......... +| 3248: 1f 02 03 01 02 03 01 02 03 01 08 32 30 31 36 30 ...........20160 +| 3264: 36 30 39 01 02 07 01 02 07 01 02 07 01 01 34 01 609...........4. +| 3280: 02 05 01 02 05 01 02 05 01 01 35 01 02 04 01 02 ..........5..... +| 3296: 04 01 02 04 02 07 30 30 30 30 30 30 30 1c 02 04 ......0000000... +| 3312: 01 02 04 01 02 04 01 06 62 69 6e 61 72 79 03 06 ........binary.. +| 3328: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3344: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3360: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 ................ +| 3376: 03 06 01 02 02 03 06 01 02 02 01 08 63 6f 6d 70 ............comp +| 3392: 69 6c 65 72 01 02 02 01 02 02 01 02 02 01 06 64 iler...........d +| 3408: 62 73 74 61 74 07 02 03 01 02 03 01 02 03 02 04 bstat........... +| 3424: 65 62 75 67 04 02 02 01 02 02 01 02 02 01 06 65 ebug...........e +| 3440: 6e 61 62 6c 65 07 02 02 01 02 02 01 02 02 01 02 nable........... +| 3456: 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 ................ +| 3472: 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 ................ +| 3488: 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 ................ +| 3504: 02 01 02 02 02 08 78 74 65 6e 73 69 6f 6e 1f 02 ......xtension.. +| 3520: 04 01 02 04 01 02 04 01 04 66 74 73 34 0a 02 03 .........fts4... +| 3536: 01 02 03 01 02 03 04 01 35 0d 02 03 01 02 03 01 ........5....... +| 3552: 02 03 01 03 67 63 63 01 02 03 01 02 03 01 02 03 ....gcc......... +| 3568: 02 06 65 6f 70 6f 6c 79 10 02 03 01 02 03 01 02 ..eopoly........ +| 3584: 03 01 05 6a 73 6f 6e 31 13 02 03 01 02 03 01 02 ...json1........ +| 3600: 03 01 04 6c 6f 61 64 1f 02 03 01 02 03 01 02 03 ...load......... +| 3616: 01 03 6d 61 78 1c 02 02 01 02 02 01 02 02 02 05 ..max........... +| 3632: 65 6d 6f 72 79 1c 02 03 01 02 03 01 02 03 04 04 emory........... +| 3648: 73 79 73 35 16 02 03 01 02 03 01 02 03 01 06 6e sys5...........n +| 3664: 6f 63 61 73 65 02 06 01 02 02 03 06 01 02 02 03 ocase........... +| 3680: 06 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 ................ +| 3696: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3712: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3728: 02 01 04 6f 6d 69 74 1f 02 02 01 02 02 01 02 02 ...omit......... +| 3744: 01 05 72 74 72 65 65 19 02 03 01 02 03 01 02 03 ..rtree......... +| 3760: 04 02 69 6d 01 06 01 02 02 03 06 01 02 02 03 06 ..im............ +| 3776: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 ................ +| 3792: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 ................ +| 3808: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02 ................ +| 3824: 01 0a 74 68 72 65 61 64 73 61 66 65 03 57 34 56 ..threadsafe.W4V +| 3840: 94 64 91 46 85 84 04 76 74 61 62 07 02 04 01 02 .d.F...vtab..... +| 3856: 04 01 02 04 01 01 78 01 06 01 01 02 01 06 01 01 ......x......... +| 3872: 02 01 06 01 01 02 01 06 01 01 02 01 06 01 10 02 ................ +| 3888: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 3904: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 3920: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 ................ +| 3936: 01 02 01 06 01 01 10 01 06 01 01 02 01 06 01 01 ................ +| 3952: 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 ................ +| 3968: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 ................ +| 3984: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 ................ +| 4000: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 ................ +| 4016: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 ................ +| 4032: 02 01 06 01 01 02 01 06 01 01 02 04 15 13 0c 0c ................ +| 4048: 12 44 13 11 0f 47 13 0f 0c 0e 11 10 0f 0e 10 0f .D...G.......... +| 4064: 44 0f 10 40 15 0f 07 01 03 00 14 24 5a 24 24 0f D..@.......$Z$$. +| 4080: 0a 03 00 24 00 00 00 00 01 01 01 00 01 01 01 01 ...$............ +| page 4 offset 12288 +| 0: 0a 00 00 00 01 0f fa 00 00 00 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 05 04 09 0c 01 02 ................ +| page 5 offset 16384 +| 0: 0d 00 00 00 24 0c 0a 00 0f d8 0f af 0f 86 0f 74 ....$..........t +| 16: 0f 61 0f 4e 0f 2f 0f 0f 0e ef 0e d7 0e be 0e a5 .a.N./.......... +| 32: 0e 8d 0e 74 0e 5b 0e 40 0e 24 0e 08 0d ef 0d d5 ...t.[.@.$...... +| 48: 0d bb 0d a0 0d 84 0d 68 0d 4f 0d 35 0d 1b 0c fb .......h.O.5.... +| 64: 0c da 0c b9 0c 99 0c 78 0c 57 0c 3e 0c 24 0c 0a .......x.W.>.$.. +| 3072: 00 00 00 00 00 00 00 00 00 00 18 24 05 00 25 0f ...........$..%. +| 3088: 19 54 48 52 45 41 44 53 41 46 45 3d 30 58 42 49 .THREADSAFE=0XBI +| 3104: 4e 41 52 59 18 23 05 00 25 0f 19 54 48 52 45 41 NARY.#..%..THREA +| 3120: 44 53 41 46 45 3d 30 58 4e 4f 43 41 53 45 17 22 DSAFE=0XNOCASE.. +| 3136: 05 00 25 0f 17 54 48 52 45 41 44 53 31 46 45 3d ..%..THREADS1FE= +| 3152: 30 58 52 64 52 49 4d 1f 21 05 00 33 0f 19 4f 4d 0XRdRIM.!..3..OM +| 3168: 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 4f IT LOAD EXTENSIO +| 3184: 4e 58 42 49 4e 41 52 59 1f 20 05 00 33 0f 19 4f NXBINARY. ..3..O +| 3200: 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 MIT LOAD EXTENSI +| 3216: 4f 4e 58 4e 4f 43 41 53 45 1e 1f 05 00 33 0f 17 ONXNOCASE....3.. +| 3232: 4f 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 OMIT LOAD EXTENS +| 3248: 49 4f 4e 58 52 54 52 49 4d 1f 1e 05 00 33 0f 19 IONXRTRIM....3.. +| 3264: 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 30 MAX MEMORY=50000 +| 3280: 30 30 30 58 42 49 4e 41 52 59 1f 1d 05 00 33 0f 000XBINARY....3. +| 3296: 19 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 .MAX MEMORY=5000 +| 3312: 30 30 30 30 58 4e 4f 43 41 53 45 1e 1c 05 00 33 0000XNOCASE....3 +| 3328: 0f 17 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 ..MAX MEMORY=500 +| 3344: 30 30 30 30 30 58 52 54 52 49 4d 18 1b 05 00 25 00000XRTRIM....% +| 3360: 0f 19 45 4e 41 42 4c 45 20 52 54 52 45 45 58 42 ..ENABLE RTREEXB +| 3376: 49 4e 41 52 59 18 1a 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3392: 4c 45 20 52 54 52 45 45 58 4e 4f 43 41 53 45 17 LE RTREEXNOCASE. +| 3408: 19 05 00 25 0f 17 45 4e 41 42 4c 45 20 52 54 52 ...%..ENABLE RTR +| 3424: 45 45 58 52 54 52 49 4d 1a 18 05 00 29 0f 19 45 EEXRTRIM....)..E +| 3440: 4e 41 42 4b 45 20 4d 45 4d 53 59 53 35 58 42 49 NABKE MEMSYS5XBI +| 3456: 4e 41 52 59 1a 17 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3472: 42 60 2d 45 4d 53 59 53 35 58 4e 4f 43 41 53 45 B`-EMSYS5XNOCASE +| 3488: 19 16 05 00 29 0f 17 45 4e 41 42 4c 45 20 4d 45 ....)..ENABLE ME +| 3504: 4d 53 59 53 35 58 52 54 52 49 4d 18 15 05 00 25 MSYS5XRTRIM....% +| 3520: 0f 19 45 4e 41 42 4c 45 20 4a 53 4f 4e 31 58 42 ..ENABLE JSON1XB +| 3536: 49 4e 41 52 59 18 14 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3552: 4c 45 20 4a 53 4f 4e 31 58 4e 4f 43 41 53 45 17 LE JSON1XNOCASE. +| 3568: 13 05 00 25 0f 17 45 4e 41 42 4c 45 20 4a 53 4f ...%..ENABLE JSO +| 3584: 4e 31 58 52 54 52 49 4d 1a 12 05 00 29 0f 19 45 N1XRTRIM....)..E +| 3600: 4e 41 42 4c 45 20 47 45 4f 50 4f 4c 59 58 42 49 NABLE GEOPOLYXBI +| 3616: 4e 41 52 59 1a 11 05 00 39 0f 19 45 4e 41 42 4c NARY....9..ENABL +| 3632: 45 20 47 45 4f 50 4f 4c 59 58 4e 4f 43 41 53 45 E GEOPOLYXNOCASE +| 3648: 19 10 05 00 29 0f 17 45 4e 41 42 4c 45 20 47 45 ....)..ENABLE GE +| 3664: 4f 50 4f 4c 59 58 52 54 52 49 4d 17 0f 05 00 23 OPOLYXRTRIM....# +| 3680: 0f 19 45 4e 41 42 4c 45 20 46 54 53 35 58 42 49 ..ENABLE FTS5XBI +| 3696: 4e 41 52 59 17 0e 05 00 23 0f 19 45 4e 41 42 4c NARY....#..ENABL +| 3712: 45 20 46 54 53 35 58 4e 4f 43 41 53 45 16 0d 05 E FTS5XNOCASE... +| 3728: 00 23 0f 17 45 4e 41 42 4c 45 20 46 54 53 35 58 .#..ENABLE FTS5X +| 3744: 52 54 52 49 4d 17 0c 05 00 23 0f 19 45 4e 41 42 RTRIM....#..ENAB +| 3760: 4c 45 20 46 54 53 34 58 42 49 4e 41 52 59 17 0b LE FTS4XBINARY.. +| 3776: 05 00 23 0f 19 45 4e 41 42 4c 45 20 46 54 53 34 ..#..ENABLE FTS4 +| 3792: 58 4e 4f 43 41 53 45 16 0a 05 00 23 0f 17 45 4e XNOCASE....#..EN +| 3808: 41 42 4c 45 20 46 54 53 34 58 52 54 52 49 4d 1e ABLE FTS4XRTRIM. +| 3824: 09 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3840: 54 41 54 20 56 54 41 42 58 42 49 4e 41 52 59 1e TAT VTABXBINARY. +| 3856: 08 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3872: 54 41 54 20 56 54 24 15 48 4e 4f 43 41 53 45 1d TAT VT$.HNOCASE. +| 3888: 07 05 00 31 0f 17 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3904: 54 41 54 20 56 54 41 42 58 52 54 52 49 4d 11 06 TAT VTABXRTRIM.. +| 3920: 05 00 17 0f 19 44 45 42 55 47 58 42 49 4e 41 52 .....DEBUGXBINAR +| 3936: 59 11 05 05 00 17 0f 19 44 45 42 55 47 58 4e 4f Y.......DEBUGXNO +| 3952: 43 41 53 45 10 04 05 00 17 0f 17 44 45 42 55 47 CASE.......DEBUG +| 3968: 58 52 54 52 49 4d 27 03 05 00 43 0f 19 43 4f 4d XRTRIM'...C..COM +| 3984: 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e 30 20 PILER=gcc-5.4.0 +| 4000: 32 30 31 36 30 36 30 39 58 42 49 4e 41 52 59 27 20160609XBINARY' +| 4016: 02 05 00 43 0f 19 43 4f 4d 50 49 4c 45 52 3c 67 ...C..COMPILER 10; +} {X'0000001A04306162630102020101620202020101640206030303040806'} + +do_execsql_test 2.2 { + UPDATE t1_data SET + block=X'0000001A04306162630102025501620202020101640206030303040806' + WHERE id>10 +} + +do_catchsql_test 2.3 { + DELETE FROM t1 WHERE rowid = 1 +} {/.*fts5: corrupt.*/} + +finish_test diff --git a/ext/fts5/test/fts5corrupt8.test b/ext/fts5/test/fts5corrupt8.test new file mode 100644 index 0000000000..471a1b0e39 --- /dev/null +++ b/ext/fts5/test/fts5corrupt8.test @@ -0,0 +1,147 @@ +# 2024 Aug 28 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +source [file join [file dirname [info script]] fts5_common.tcl] +set testprefix fts5corrupt8 + +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. +ifcapable !fts5 { + finish_test + return +} + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE t1 USING fts5(x); +} + +do_execsql_test 1.1 { + UPDATE t1_data SET block='hello world' WHERE id=10 +} + +db close +sqlite3 db test.db + +do_catchsql_test 1.2 { + SELECT * FROM t1 +} {1 {fts5: corrupt structure record for table "t1"}} +do_catchsql_test 1.3 { + DROP TABLE t1 +} {0 {}} +do_execsql_test 1.4 { + SELECT * FROM sqlite_schema +} + +do_execsql_test 2.0 { + CREATE VIRTUAL TABLE t1 USING fts5(x); +} +do_execsql_test 2.1 { + UPDATE t1_config SET v=555 WHERE k='version' +} +db close +sqlite3 db test.db +do_catchsql_test 2.2 { + SELECT * FROM t1 +} {1 {invalid fts5 file format (found 555, expected 4 or 5) - run 'rebuild'}} +do_catchsql_test 2.3 { + DROP TABLE t1 +} {1 {invalid fts5 file format (found 555, expected 4 or 5) - run 'rebuild'}} +do_test 2.4 { + sqlite3_fts5_drop_corrupt_table db main t1 +} {} +do_execsql_test 2.5 { + SELECT * FROM sqlite_schema +} + +do_execsql_test 3.0 { + CREATE VIRTUAL TABLE t1 USING fts5(x); +} +do_execsql_test 3.1 { + DELETE FROM t1_config; +} +db close +sqlite3 db test.db +do_catchsql_test 3.2 { + SELECT * FROM t1 +} {1 {invalid fts5 file format (found 0, expected 4 or 5) - run 'rebuild'}} +do_catchsql_test 3.3 { + DROP TABLE t1 +} {1 {invalid fts5 file format (found 0, expected 4 or 5) - run 'rebuild'}} + + +do_test 3.4 { + sqlite3_db_config db DEFENSIVE 1 +} {1} +do_test 3.5 { + sqlite3_fts5_drop_corrupt_table db main t1 +} {} +do_test 3.6 { + sqlite3_db_config db DEFENSIVE -1 +} {1} +do_execsql_test 3.7 { + SELECT * FROM sqlite_schema +} + +#------------------------------------------------------------------------- +reset_db + +proc hex_to_blob {hex} { + binary encode hex $hex +} +db func hex_to_blob hex_to_blob + +do_execsql_test 4.0 { + CREATE VIRTUAL TABLE x1 USING fts5(x, content='', contentless_delete=1); + BEGIN; + INSERT INTO x1(rowid, x) VALUES(1, 'a b c d e f g h'); + INSERT INTO x1(rowid, x) VALUES(2, 'a b c d e f g h'); + COMMIT; + DELETE FROM x1 WHERE rowid=1; +} + +do_execsql_test 4.1 { + SELECT hex(block) FROM x1_data WHERE id=10 +} { + 00000000FF00000101010200010101010101010102 +} + +do_execsql_test 4.2.1 { + UPDATE x1_data SET block= + X'00000000FF00000101010200010101010101819C9B95A8000102' + WHERE id=10; +} + +do_catchsql_test 4.2.2 { + SELECT * FROM x1('c d e'); +} {1 {out of memory}} + +do_execsql_test 4.3.1 { + UPDATE x1_data SET block= + X'00000000FF000001010102000101010101019282AFF9A0000102' + WHERE id=10; +} + +do_catchsql_test 4.3.2 { + SELECT * FROM x1('c d e'); +} {1 {out of memory}} + +do_execsql_test 4.4.1 { + UPDATE x1_data SET block= + X'00000000FF000001010102000101010101018181808080130102' + WHERE id=10; +} + +do_catchsql_test 4.3.2 { + SELECT * FROM x1('c d e'); +} {1 {out of memory}} + +finish_test + diff --git a/ext/fts5/test/fts5corruptbig.test b/ext/fts5/test/fts5corruptbig.test new file mode 100644 index 0000000000..6019f17eee --- /dev/null +++ b/ext/fts5/test/fts5corruptbig.test @@ -0,0 +1,53 @@ +# 2025 October 13 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# This test is focused on really large position lists. Those that require +# 4 or 5 byte position-list size varints. Because of the amount of memory +# required, these tests only run on 64-bit platforms. +# + +source [file join [file dirname [info script]] fts5_common.tcl] +set testprefix fts5corruptbig + +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. +ifcapable !fts5 { + finish_test + return +} + +if { $tcl_platform(wordSize)<8 } { + finish_test + return +} + +if { $SQLITE_MAX_LENGTH!=0x7FFFFFFF } { + finish_test + return +} + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE t1 USING fts5(x); +} + +do_execsql_test 1.1 { + UPDATE t1_data SET block = zeroblob(2147483640) WHERE id=10; +} + +do_execsql_test 1.2 { + SELECT id, length(block) FROM t1_data +} {1 0 10 2147483640} + +do_catchsql_test 1.3 { + SELECT * FROM t1('abc') +} {1 {out of memory}} + +finish_test + diff --git a/ext/fts5/test/fts5delete.test b/ext/fts5/test/fts5delete.test new file mode 100644 index 0000000000..024f89594c --- /dev/null +++ b/ext/fts5/test/fts5delete.test @@ -0,0 +1,170 @@ +# 2017 May 12 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# This file implements regression tests for SQLite library. The +# focus of this script is testing the FTS5 module. +# + +source [file join [file dirname [info script]] fts5_common.tcl] +set testprefix fts5delete + +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. +ifcapable !fts5 { + finish_test + return +} +fts5_aux_test_functions db + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE t1 USING fts5(x); + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<5000 + ) + INSERT INTO t1(rowid, x) SELECT i, (i/2)*2 FROM s; +} + +do_test 1.1 { + execsql BEGIN + for {set i 1} {$i<=5000} {incr i} { + if {$i % 2} { + execsql { INSERT INTO t1 VALUES($i) } + } else { + execsql { DELETE FROM t1 WHERE rowid = $i } + } + } + execsql COMMIT +} {} + +do_test 1.2 { + execsql { INSERT INTO t1(t1, rank) VALUES('usermerge', 2); } + for {set i 0} {$i < 5} {incr i} { + execsql { INSERT INTO t1(t1, rank) VALUES('merge', 1) } + execsql { INSERT INTO t1(t1) VALUES('integrity-check') } + } +} {} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 2.0 { + CREATE TABLE test ( + id INTEGER PRIMARY KEY, + name TEXT, + value TEXT + ); + CREATE VIRTUAL TABLE test_idx USING fts5( + name, content=test, content_rowid=id + ); +} + +do_catchsql_test 2.1 { + INSERT INTO test_idx (test_idx, rowid, name) VALUES('delete', 1, 'quick'); +} {1 {database disk image is malformed}} + +do_catchsql_test 2.2 { + INSERT INTO test_idx(rowid, name) VALUES(123, 'one one one'); + INSERT INTO test_idx (test_idx, rowid, name) VALUES('delete', 123, 'one'); + INSERT INTO test_idx (test_idx, rowid, name) VALUES('delete', 123, 'one'); +} {1 {database disk image is malformed}} + +do_execsql_test 2.3 { + DROP TABLE test_idx; + CREATE VIRTUAL TABLE test_idx USING fts5( + name, content=test, content_rowid=id + ); + + INSERT INTO test_idx(rowid, name) VALUES(123, 'one one one'); + INSERT INTO test_idx(rowid, name) VALUES(124, 'two two two'); + INSERT INTO test_idx(rowid, name) VALUES(125, 'two two two'); + INSERT INTO test_idx (test_idx, rowid, name) VALUES('delete', 123, 'one'); + INSERT INTO test_idx (test_idx, rowid, name) VALUES('delete', 123, 'one'); + INSERT INTO test_idx (test_idx, rowid, name) VALUES('delete', 123, 'one'); +} + +do_catchsql_test 2.4 { + SELECT rowid FROM test_idx WHERE test_idx MATCH 'two' ORDER BY rank; +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 3.0 { + CREATE VIRTUAL TABLE tx USING fts5(a, b, c, d, content=); + INSERT INTO tx(rowid, a, c) VALUES(1, 'abc def', 'a b c'); + INSERT INTO tx(rowid, a, c) VALUES(5, 'a b c', 'a b d def'); +} +do_execsql_test 3.1 { + INSERT INTO tx(tx, rowid, a, b, c, d) + VALUES('delete', 5, 'a b c', NULL, 'a b d def', NULL); +} +do_execsql_test 3.2 { + INSERT INTO tx(tx) VALUES('integrity-check'); +} +do_execsql_test 3.3 { + INSERT INTO tx(tx, rowid, a, b, c, d) + VALUES('delete', 1, 'abc def', NULL, 'a b c', NULL); +} +do_execsql_test 3.4 { + INSERT INTO tx(tx) VALUES('integrity-check'); +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 4.0 { + CREATE VIRTUAL TABLE ft1 USING fts5(a, b UNINDEXED, + content='', contentless_unindexed=1 + ); + CREATE VIRTUAL TABLE ft2 USING fts5(a, b UNINDEXED, + content='', contentless_unindexed=1, contentless_delete=1 + ); + + INSERT INTO ft1(rowid, a, b) VALUES(1, 'one', 'i'); + INSERT INTO ft1(rowid, a, b) VALUES(2, 'two', 'ii'); + INSERT INTO ft1(rowid, a, b) VALUES(3, 'three', 'iii'); + INSERT INTO ft2(rowid, a, b) VALUES(1, 'one', 'i'); + INSERT INTO ft2(rowid, a, b) VALUES(2, 'two', 'ii'); + INSERT INTO ft2(rowid, a, b) VALUES(3, 'three', 'iii'); +} + +do_catchsql_test 4.1 { + DELETE FROM ft1 WHERE rowid=2 +} {1 {cannot DELETE from contentless fts5 table: ft1}} +do_catchsql_test 4.2 { + DELETE FROM ft2 WHERE rowid=2 +} {0 {}} + +do_catchsql_test 4.3 { + INSERT INTO ft1(ft1, rowid, a) VALUES('delete', 2, 'two'); +} {0 {}} +do_catchsql_test 4.2 { + INSERT INTO ft2(ft2, rowid, a) VALUES('delete', 2, 'two'); +} {1 {'delete' may not be used with a contentless_delete=1 table}} + +do_execsql_test 4.3 { + SELECT rowid, * FROM ft1; +} { + 1 {} i + 3 {} iii +} +do_execsql_test 4.4 { + SELECT rowid, * FROM ft2; +} { + 1 {} i + 3 {} iii +} + +do_execsql_test 4.5 { + SELECT * FROM ft1_content +} {1 i 3 iii} + +do_execsql_test 4.6 { + SELECT * FROM ft2_content +} {1 i 3 iii} + +finish_test + diff --git a/ext/fts5/test/fts5detail.test b/ext/fts5/test/fts5detail.test index 58fda3e995..267ce6187c 100644 --- a/ext/fts5/test/fts5detail.test +++ b/ext/fts5/test/fts5detail.test @@ -212,6 +212,10 @@ do_catchsql_test 4.1 { SELECT * FROM t4('a:a') } {1 {fts5: column queries are not supported (detail=none)}} +do_catchsql_test 4.2 { + SELECT * FROM t4('a:a &') +} {1 {fts5: syntax error near "&"}} + #------------------------------------------------------------------------- # Test that for the same content detail=none uses less space than # detail=col, and that detail=col uses less space than detail=full @@ -241,4 +245,3 @@ do_execsql_test 5.3 { finish_test - diff --git a/ext/fts5/test/fts5determin.test b/ext/fts5/test/fts5determin.test new file mode 100644 index 0000000000..c9094edfa1 --- /dev/null +++ b/ext/fts5/test/fts5determin.test @@ -0,0 +1,65 @@ +# 2016 March 21 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# This file implements regression tests for SQLite library. The +# focus of this script is testing the FTS5 module. +# +# Specifically, that the fts5 module is deterministic. At one point, when +# segment ids were allocated using sqlite3_randomness(), this was not the +# case. +# + +source [file join [file dirname [info script]] fts5_common.tcl] +set testprefix fts5aa +return_if_no_fts5 + +proc do_determin_test {tn} { + uplevel [list + do_execsql_test $tn { + SELECT (SELECT md5sum(id, block) FROM t1_data)== + (SELECT md5sum(id, block) FROM t2_data), + (SELECT md5sum(id, block) FROM t1_data)== + (SELECT md5sum(id, block) FROM t3_data) + } {1 1} + ] +} + +foreach_detail_mode $::testprefix { + do_execsql_test 1.0 { + CREATE VIRTUAL TABLE t1 USING fts5(a, b, prefix="1 2", detail=%DETAIL%); + CREATE VIRTUAL TABLE t2 USING fts5(a, b, prefix="1 2", detail=%DETAIL%); + CREATE VIRTUAL TABLE t3 USING fts5(a, b, prefix="1 2", detail=%DETAIL%); + } + + do_test 1.1 { + foreach t {t1 t2 t3} { + execsql [string map [list TBL $t] { + INSERT INTO TBL VALUES('a b c', 'd e f'); + INSERT INTO TBL VALUES('c1 c2 c3', 'c1 c2 c3'); + INSERT INTO TBL VALUES('xyzxyzxyz', 'xyzxyzxyz'); + }] + } + } {} + + do_determin_test 1.2 + + do_test 1.3 { + foreach t {t1 t2 t3} { + execsql [string map [list TBL $t] { + INSERT INTO TBL(TBL) VALUES('optimize'); + }] + } + } {} + + do_determin_test 1.4 +} + + +finish_test diff --git a/ext/fts5/test/fts5dlidx.test b/ext/fts5/test/fts5dlidx.test index 232b5021f1..db82db1c2b 100644 --- a/ext/fts5/test/fts5dlidx.test +++ b/ext/fts5/test/fts5dlidx.test @@ -15,7 +15,7 @@ source [file join [file dirname [info script]] fts5_common.tcl] set testprefix fts5dlidx -# If SQLITE_ENABLE_FTS5 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return @@ -66,7 +66,6 @@ proc do_dlidx_test1 {tn spc1 spc2 nEntry iFirst nStep} { } execsql COMMIT - breakpoint do_test $tn.1 { execsql { INSERT INTO t1(t1) VALUES('integrity-check') } } {} @@ -124,7 +123,6 @@ proc do_dlidx_test2 {tn nEntry iFirst nStep} { do_execsql_test $tn.1 { SELECT rowid FROM t1 WHERE t1 MATCH 'b AND a' } {1} - breakpoint do_execsql_test $tn.2 { SELECT rowid FROM t1 WHERE t1 MATCH 'b AND a' ORDER BY rowid DESC } {1} @@ -178,7 +176,7 @@ do_execsql_test 3.2 { ORDER BY rowid DESC; } {16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1} -do_execsql_test 3.2 { +do_execsql_test 3.3 { INSERT INTO abc(abc) VALUES('integrity-check'); INSERT INTO abc(abc) VALUES('optimize'); INSERT INTO abc(abc) VALUES('integrity-check'); @@ -187,7 +185,7 @@ do_execsql_test 3.2 { set v [lindex $vocab 0] set i 0 foreach v $vocab { - do_execsql_test 3.3.[incr i] { + do_execsql_test 3.4.[incr i] { SELECT rowid FROM abc WHERE abc MATCH $v } {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16} } @@ -197,4 +195,3 @@ foreach v $vocab { finish_test - diff --git a/ext/fts5/test/fts5doclist.test b/ext/fts5/test/fts5doclist.test index 411289a523..5b1becb514 100644 --- a/ext/fts5/test/fts5doclist.test +++ b/ext/fts5/test/fts5doclist.test @@ -15,7 +15,7 @@ source [file join [file dirname [info script]] fts5_common.tcl] set testprefix fts5doclist -# If SQLITE_ENABLE_FTS5 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return @@ -42,6 +42,26 @@ do_execsql_test 1.2 { INSERT INTO ccc(ccc) VALUES('integrity-check'); } +#------------------------------------------------------------------------- +# +reset_db +do_execsql_test 2.1 { + CREATE VIRTUAL TABLE tx USING fts5(x); +} -finish_test +set doc [string repeat "abc " 5000] +do_execsql_test 2.2 { + BEGIN; + INSERT INTO tx(rowid, x) VALUES(-9000000000000000000, $doc); + INSERT INTO tx(rowid, x) VALUES(9000000000000000000, $doc); + COMMIT; +} + +do_execsql_test 2.3 { + SELECT rowid FROM tx('abc'); +} { + -9000000000000000000 + 9000000000000000000 +} +finish_test diff --git a/ext/fts5/test/fts5ea.test b/ext/fts5/test/fts5ea.test index 3ccbd7d7a2..49c2f2753a 100644 --- a/ext/fts5/test/fts5ea.test +++ b/ext/fts5/test/fts5ea.test @@ -16,7 +16,7 @@ source [file join [file dirname [info script]] fts5_common.tcl] set testprefix fts5ea -# If SQLITE_ENABLE_FTS5 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return diff --git a/ext/fts5/test/fts5eb.test b/ext/fts5/test/fts5eb.test index 8205396047..bee9683c3c 100644 --- a/ext/fts5/test/fts5eb.test +++ b/ext/fts5/test/fts5eb.test @@ -13,7 +13,7 @@ source [file join [file dirname [info script]] fts5_common.tcl] set testprefix fts5eb -# If SQLITE_ENABLE_FTS5 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return @@ -33,12 +33,12 @@ foreach {tn expr res} { 1 {abc} {"abc"} 2 {abc ""} {"abc"} 3 {""} {} - 4 {abc OR ""} {"abc"} - 5 {abc NOT ""} {"abc"} - 6 {abc AND ""} {"abc"} - 7 {"" OR abc} {"abc"} - 8 {"" NOT abc} {"abc"} - 9 {"" AND abc} {"abc"} + 4 {abc OR ""} {"abc" OR ""} + 5 {abc NOT ""} {"abc" NOT ""} + 6 {abc AND ""} {"abc" AND ""} + 7 {"" OR abc} {"" OR "abc"} + 8 {"" NOT abc} {"" NOT "abc"} + 9 {"" AND abc} {"" AND "abc"} 10 {abc + "" + def} {"abc" + "def"} 11 {abc "" def} {"abc" AND "def"} 12 {r+e OR w} {"r" + "e" OR "w"} @@ -59,11 +59,45 @@ do_catchsql_test 2.1 { SELECT fts5_expr() } {1 {wrong number of arguments to function fts5_expr}} -do_catchsql_test 2.1 { +do_catchsql_test 2.2 { SELECT fts5_expr_tcl() } {1 {wrong number of arguments to function fts5_expr_tcl}} -finish_test +do_catchsql_test 2.3 { + SELECT fts5_expr('') +} {1 {fts5: syntax error near ""}} +do_catchsql_test 2.4 { + SELECT fts5_expr(NULL) +} {1 {fts5: syntax error near ""}} +do_catchsql_test 2.5 { + SELECT fts5_expr(NULL, NULL) +} {1 {parse error in ""}} +for {set i 0} {$i < 255} {incr i} { + do_test 2.6.$i { + lindex [catchsql {sELECT fts5_expr(NULL, char($i));}] 0 + } 1 +} + +do_execsql_test 3.0 { + CREATE VIRTUAL TABLE e1 USING fts5(text, tokenize = 'porter unicode61'); + INSERT INTO e1 VALUES ('just a few words with a / inside'); +} +do_execsql_test 3.1 { + SELECT rowid, format('%g',bm25(e1)) FROM e1 WHERE e1 MATCH '"just"' ORDER BY rank; +} {1 -1e-06} +do_execsql_test 3.2 { + SELECT rowid FROM e1 WHERE e1 MATCH '"/" OR "just"' +} 1 +do_execsql_test 3.3 { + SELECT rowid, format('%g',bm25(e1)) FROM e1 WHERE e1 MATCH '"/" OR "just"' ORDER BY rank; +} {1 -1e-06} + +do_execsql_test 3.4 " + SELECT fts5_expr_tcl('e AND \" \"'); +" {{AND [nearset -- {e}] [{}]}} + + +finish_test diff --git a/ext/fts5/test/fts5expr.test b/ext/fts5/test/fts5expr.test new file mode 100644 index 0000000000..49be61d9c4 --- /dev/null +++ b/ext/fts5/test/fts5expr.test @@ -0,0 +1,52 @@ +# 2024 August 8 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# This file implements regression tests for SQLite library. The +# focus of this script is testing the FTS5 module. +# + +source [file join [file dirname [info script]] fts5_common.tcl] +set testprefix fts5expr + +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. +ifcapable !fts5 { + finish_test + return +} + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE x1 USING fts5(a); + INSERT INTO x1(rowid, a) VALUES (113, 'fts5 expr test'); +} + +do_execsql_test 1.1 { + SELECT rowid FROM x1('expr'); +} {113} + +for {set ii 0} {$ii < 300} {incr ii} { + set expr "expr " + append expr [string repeat "NOT abcd " $ii] + + if {$ii<257} { + set res {0 113} + } else { + set res {1 {fts5 expression tree is too large (maximum depth 256)}} + } + do_catchsql_test 1.1.$ii { + SELECT rowid FROM x1($expr) + } $res +} + +do_execsql_test 1.2 { + SELECT rowid FROM x1 WHERE a MATCH '"..."' +} {} + +finish_test + diff --git a/ext/fts5/test/fts5fault1.test b/ext/fts5/test/fts5fault1.test index 9d63a1175f..96025133e4 100644 --- a/ext/fts5/test/fts5fault1.test +++ b/ext/fts5/test/fts5fault1.test @@ -351,4 +351,3 @@ do_faultsim_test 9.1 -faults oom-* -prep { finish_test - diff --git a/ext/fts5/test/fts5fault2.test b/ext/fts5/test/fts5fault2.test index 43c7c7a3dd..aa21f7b481 100644 --- a/ext/fts5/test/fts5fault2.test +++ b/ext/fts5/test/fts5fault2.test @@ -137,4 +137,3 @@ do_faultsim_test 5.0 -faults oom-* -prep { } finish_test - diff --git a/ext/fts5/test/fts5fault3.test b/ext/fts5/test/fts5fault3.test index bfeead4e23..a294692dbe 100644 --- a/ext/fts5/test/fts5fault3.test +++ b/ext/fts5/test/fts5fault3.test @@ -110,4 +110,3 @@ do_faultsim_test 3.2 -faults oom-* -prep { finish_test - diff --git a/ext/fts5/test/fts5fault4.test b/ext/fts5/test/fts5fault4.test index acc43ebfc6..2b4f6c4d2a 100644 --- a/ext/fts5/test/fts5fault4.test +++ b/ext/fts5/test/fts5fault4.test @@ -16,12 +16,16 @@ source [file join [file dirname [info script]] fts5_common.tcl] source $testdir/malloc_common.tcl set testprefix fts5fault4 -# If SQLITE_ENABLE_FTS3 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return } +set ::TMPDBERROR [list 1 \ + {unable to open a temporary database file for storing temporary tables} +] + #------------------------------------------------------------------------- # An OOM while dropping an fts5 table. # @@ -86,7 +90,7 @@ set ::res [db eval {SELECT rowid, x1 FROM x1 WHERE x1 MATCH '*reads'}] do_faultsim_test 4 -faults oom-* -body { db eval {SELECT rowid, x, x1 FROM x1 WHERE x1 MATCH '*reads'} } -test { - faultsim_test_result {0 {0 {} 4}} + faultsim_test_result {0 {0 {} 2}} } #------------------------------------------------------------------------- @@ -391,8 +395,7 @@ do_faultsim_test 14.1 -faults oom-t* -prep { } -body { db eval { ALTER TABLE "tbl one" RENAME TO "tbl two" } } -test { - faultsim_test_result {0 {}} + faultsim_test_result {0 {}} $::TMPDBERROR } finish_test - diff --git a/ext/fts5/test/fts5fault5.test b/ext/fts5/test/fts5fault5.test index 75b7d9af50..6b8dade504 100644 --- a/ext/fts5/test/fts5fault5.test +++ b/ext/fts5/test/fts5fault5.test @@ -105,7 +105,6 @@ do_faultsim_test 3.2 -faults oom-t* -body { faultsim_test_result {0 {1 10 11 12 13 14 15 16 17 18 19 2}} } -breakpoint do_execsql_test 3.3.0 { SELECT * FROM tv2; } { @@ -130,4 +129,3 @@ do_faultsim_test 3.3 -faults oom-t* -body { finish_test - diff --git a/ext/fts5/test/fts5fault6.test b/ext/fts5/test/fts5fault6.test index 118761fab5..1aacddce9f 100644 --- a/ext/fts5/test/fts5fault6.test +++ b/ext/fts5/test/fts5fault6.test @@ -16,7 +16,7 @@ source [file join [file dirname [info script]] fts5_common.tcl] source $testdir/malloc_common.tcl set testprefix fts5fault6 -# If SQLITE_ENABLE_FTS5 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return @@ -253,7 +253,7 @@ do_faultsim_test 5.2 -faults oom* -prep { SELECT rowid, mit(matchinfo(t1, 'x')) FROM t1 WHERE t1 MATCH 'a AND c' } } -test { - faultsim_test_result [list 0 $::res] + faultsim_test_result [list 0 $::res] {1 {SQL logic error}} } do_faultsim_test 5.3 -faults oom* -prep { @@ -264,7 +264,7 @@ do_faultsim_test 5.3 -faults oom* -prep { SELECT count(*) FROM t1 WHERE t1 MATCH 'd AND e AND f' } } -test { - faultsim_test_result {0 29} + faultsim_test_result {0 29} {1 {SQL logic error}} } do_faultsim_test 5.4 -faults oom* -prep { @@ -275,12 +275,11 @@ do_faultsim_test 5.4 -faults oom* -prep { SELECT count(*) FROM t1 WHERE t1 MATCH 'x + e' } } -test { - faultsim_test_result {0 1} + faultsim_test_result {0 1} {1 {SQL logic error}} } #------------------------------------------------------------------------- catch { db close } -breakpoint do_faultsim_test 6 -faults oom* -prep { sqlite_orig db test.db sqlite3_db_config_lookaside db 0 0 0 @@ -292,4 +291,3 @@ do_faultsim_test 6 -faults oom* -prep { db close } finish_test - diff --git a/ext/fts5/test/fts5fault7.test b/ext/fts5/test/fts5fault7.test index a35b19ade2..c93e099f82 100644 --- a/ext/fts5/test/fts5fault7.test +++ b/ext/fts5/test/fts5fault7.test @@ -116,4 +116,3 @@ do_faultsim_test 2.2 -faults oom-* -body { } finish_test - diff --git a/ext/fts5/test/fts5fault8.test b/ext/fts5/test/fts5fault8.test index ae5849495b..dc060a1592 100644 --- a/ext/fts5/test/fts5fault8.test +++ b/ext/fts5/test/fts5fault8.test @@ -54,7 +54,43 @@ foreach_detail_mode $testprefix { faultsim_test_result {0 {1 3}} {1 SQLITE_NOMEM} } } + } ;# foreach_detail_mode... -finish_test +do_execsql_test 4.0 { + CREATE VIRTUAL TABLE x2 USING fts5(a); + INSERT INTO x2(x2, rank) VALUES('crisismerge', 2); + INSERT INTO x2(x2, rank) VALUES('pgsz', 32); + INSERT INTO x2 VALUES('a b c d'); + INSERT INTO x2 VALUES('e f g h'); + INSERT INTO x2 VALUES('i j k l'); + INSERT INTO x2 VALUES('m n o p'); + INSERT INTO x2 VALUES('q r s t'); + INSERT INTO x2 VALUES('u v w x'); + INSERT INTO x2 VALUES('y z a b'); +} +faultsim_save_and_close + +do_faultsim_test 4 -faults oom-* -prep { + faultsim_restore_and_reopen +} -body { + execsql { INSERT INTO x2(x2) VALUES('optimize') } +} -test { + faultsim_test_result {0 {}} {1 SQLITE_NOMEM} +} + +set TMPDBERROR {1 {unable to open a temporary database file for storing temporary tables}} +do_faultsim_test 5 -faults oom-t* -prep { + faultsim_restore_and_reopen + execsql { PRAGMA temp_store = memory } +} -body { + execsql { PRAGMA integrity_check } +} -test { + if {[string match {*error code=7*} $testresult]==0} { + faultsim_test_result {0 ok} {1 SQLITE_NOMEM} $::TMPDBERROR + } +} + + +finish_test diff --git a/ext/fts5/test/fts5fault9.test b/ext/fts5/test/fts5fault9.test index 908a91d22a..669b13efe7 100644 --- a/ext/fts5/test/fts5fault9.test +++ b/ext/fts5/test/fts5fault9.test @@ -24,6 +24,8 @@ ifcapable !fts5 { foreach_detail_mode $testprefix { +if {"%DETAIL%" != "none"} continue + fts5_aux_test_functions db do_execsql_test 1.0 { @@ -98,14 +100,16 @@ do_faultsim_test 4.1 -faults oom-t* -body { execsql { SELECT rowid, fts5_test_collist(t4) FROM t4('2') } } -test { faultsim_test_result \ - {0 {1 {0.0 0.1 0.2} 2 {0.0 0.1 0.2} 3 {0.0 0.1 0.2}}} {1 SQLITE_NOMEM} + {0 {1 {0.0 0.1 0.2} 2 {0.0 0.1 0.2} 3 {0.0 0.1 0.2}}} \ + {1 SQLITE_NOMEM} {1 SQLITE_ERROR} {1 {SQL logic error}} } do_faultsim_test 4.2 -faults oom-t* -body { execsql { SELECT rowid, fts5_test_collist(t4) FROM t4('a5 OR b5 OR c5') } } -test { faultsim_test_result \ - {0 {4 {0.0 0.1 0.2} 5 {1.0 1.1 1.2} 6 {2.0 2.1 2.2}}} {1 SQLITE_NOMEM} + {0 {4 {0.0 0.1 0.2} 5 {1.0 1.1 1.2} 6 {2.0 2.1 2.2}}} \ + {1 SQLITE_NOMEM} {1 SQLITE_ERROR} {1 {SQL logic error}} } @@ -153,4 +157,3 @@ do_faultsim_test 6 -faults oom-* -body { } ;# foreach_detail_mode... finish_test - diff --git a/ext/fts5/test/fts5faultA.test b/ext/fts5/test/fts5faultA.test index 817ccb4ee8..212401fe58 100644 --- a/ext/fts5/test/fts5faultA.test +++ b/ext/fts5/test/fts5faultA.test @@ -61,4 +61,3 @@ do_faultsim_test 2 -faults oom* -prep { faultsim_test_result {0 {1 2}} } finish_test - diff --git a/ext/fts5/test/fts5faultB.test b/ext/fts5/test/fts5faultB.test new file mode 100644 index 0000000000..e5fc514d09 --- /dev/null +++ b/ext/fts5/test/fts5faultB.test @@ -0,0 +1,173 @@ +# 2016 February 17 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# +# This file is focused on OOM errors. +# + +source [file join [file dirname [info script]] fts5_common.tcl] +source $testdir/malloc_common.tcl +set testprefix fts5faultB + +# If SQLITE_ENABLE_FTS3 is defined, omit this file. +ifcapable !fts5 { + finish_test + return +} + +proc mit {blob} { + set scan(littleEndian) i* + set scan(bigEndian) I* + binary scan $blob $scan($::tcl_platform(byteOrder)) r + return $r +} +db func mit mit + + +#------------------------------------------------------------------------- +# Errors while registering the matchinfo() demo function. +# +do_faultsim_test 1 -faults oom* -prep { + sqlite3 db test.db +} -body { + sqlite3_fts5_register_matchinfo db +} -test { + faultsim_test_result {0 {}} {1 SQLITE_ERROR} {1 SQLITE_NOMEM} +} + + +#------------------------------------------------------------------------- +# Errors while executing the matchinfo() demo function. +# +reset_db +sqlite3_fts5_register_matchinfo db +db func mit mit +do_execsql_test 2 { + CREATE VIRTUAL TABLE t1 USING fts5(a, b); + INSERT INTO t1 VALUES('x y z', '1 2 3'); + INSERT INTO t1 VALUES('x', '1 2 3 4 5 6 7'); +} + +do_faultsim_test 2.1 -faults oom* -body { + execsql { SELECT mit(matchinfo(t1, 'a')) FROM t1('x') } +} -test { + faultsim_test_result {0 {{2 5} {2 5}}} +} + +do_faultsim_test 2.2 -faults oom* -body { + execsql { SELECT mit(matchinfo(t1, 'l')) FROM t1('x') } +} -test { + faultsim_test_result {0 {{3 3} {1 7}}} +} + +do_execsql_test 2.3 { + INSERT INTO t1 VALUES('a b c d e f', 'a b d e f c'); + INSERT INTO t1 VALUES('l m b c a', 'n o a b c z'); +} + +do_faultsim_test 2.4 -faults oom* -body { + execsql { SELECT mit(matchinfo(t1, 's')) FROM t1('a b c') } +} -test { + faultsim_test_result {0 {{3 2} {2 3}}} +} + +#------------------------------------------------------------------------- +# +reset_db +do_execsql_test 3.0 { + CREATE VIRTUAL TABLE x1 USING fts5(z); +} + +do_faultsim_test 3.1 -faults oom* -body { + execsql { + SELECT rowid FROM x1('c') WHERE rowid>1; + } +} -test { + faultsim_test_result {0 {}} +} + +do_execsql_test 3.2 { + INSERT INTO x1 VALUES('a b c'); + INSERT INTO x1 VALUES('b c d'); + INSERT INTO x1 VALUES('c d e'); + INSERT INTO x1 VALUES('d e f'); +} +do_faultsim_test 3.3 -faults oom* -body { + execsql { + SELECT rowid FROM x1('c') WHERE rowid>1; + } +} -test { + faultsim_test_result {0 {2 3}} +} + +#------------------------------------------------------------------------- +# Test OOM injection with nested colsets. +# +reset_db +do_execsql_test 4.0 { + CREATE VIRTUAL TABLE t1 USING fts5(a, b, c, d); + INSERT INTO t1 VALUES('a', 'b', 'c', 'd'); -- 1 + INSERT INTO t1 VALUES('d', 'a', 'b', 'c'); -- 2 + INSERT INTO t1 VALUES('c', 'd', 'a', 'b'); -- 3 + INSERT INTO t1 VALUES('b', 'c', 'd', 'a'); -- 4 +} +do_faultsim_test 4.1 -faults oom* -body { + execsql { SELECT rowid FROM t1('{a b c} : (b:a AND c:b)'); } +} -test { + faultsim_test_result {0 2} +} + +do_faultsim_test 4.2 -faults oom* -body { + execsql { SELECT rowid FROM t1('{a b c} : (a AND d)') } +} -test { + faultsim_test_result {0 {2 3}} +} + +#------------------------------------------------------------------------- +# Test OOM injection while parsing a CARET expression +# +reset_db +do_execsql_test 5.0 { + CREATE VIRTUAL TABLE t1 USING fts5(a); + INSERT INTO t1 VALUES('a b c d'); -- 1 + INSERT INTO t1 VALUES('d a b c'); -- 2 + INSERT INTO t1 VALUES('c d a b'); -- 3 + INSERT INTO t1 VALUES('b c d a'); -- 4 +} +do_faultsim_test 5.1 -faults oom* -body { + execsql { SELECT rowid FROM t1('^a OR ^b') } +} -test { + faultsim_test_result {0 {1 4}} +} + +#------------------------------------------------------------------------- +# Test OOM injection in a query with two MATCH expressions +# +reset_db +do_execsql_test 6.0 { + CREATE VIRTUAL TABLE t1 USING fts5(a); + INSERT INTO t1 VALUES('a b c d'); -- 1 + INSERT INTO t1 VALUES('d a b c'); -- 2 + INSERT INTO t1 VALUES('c d a b'); -- 3 + INSERT INTO t1 VALUES('b c d a'); -- 4 +} +do_faultsim_test 6.1 -faults oom* -body { + execsql { SELECT rowid FROM t1 WHERE t1 MATCH 'a' AND t1 MATCH 'b' } +} -test { + faultsim_test_result {0 {1 2 3 4}} +} +do_faultsim_test 6.2 -faults oom* -body { + execsql { SELECT rowid FROM t1 WHERE t1 MATCH 'a OR b' AND t1 MATCH 'c OR d' } +} -test { + faultsim_test_result {0 {1 2 3 4}} +} + + +finish_test diff --git a/ext/fts5/test/fts5faultD.test b/ext/fts5/test/fts5faultD.test new file mode 100644 index 0000000000..33590645ce --- /dev/null +++ b/ext/fts5/test/fts5faultD.test @@ -0,0 +1,87 @@ +# 2016 February 2 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# +# This file is focused on OOM errors. +# + +source [file join [file dirname [info script]] fts5_common.tcl] +source $testdir/malloc_common.tcl +set testprefix fts5faultD + +# If SQLITE_ENABLE_FTS3 is defined, omit this file. +ifcapable !fts5 { + finish_test + return +} + +foreach_detail_mode $testprefix { + if {"%DETAIL%"=="none"} continue + + do_execsql_test 1.0 { + CREATE VIRTUAL TABLE o1 USING fts5(a, b, c, detail=%DETAIL%); + INSERT INTO o1(o1, rank) VALUES('pgsz', 32); + + WITH s(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<60 ) + INSERT INTO o1 SELECT 'A', 'B', 'C' FROM s; + + WITH s(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<60 ) + INSERT INTO o1 SELECT 'C', 'A', 'B' FROM s; + + WITH s(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<60 ) + INSERT INTO o1 SELECT 'B', 'C', 'A' FROM s; + } + + do_faultsim_test 1 -faults int* -prep { + sqlite3 db test.db + } -body { + execsql { SELECT count(*) FROM o1('a') } + } -test { + faultsim_test_result {0 180} {1 {vtable constructor failed: o1}} + } + + do_faultsim_test 2 -faults int* -prep { + sqlite3 db test.db + } -body { + execsql { SELECT * FROM o1('a:a AND {b c}:b') ORDER BY rank } + expr 1 + } -test { + faultsim_test_result {0 1} {1 {vtable constructor failed: o1}} + } + + do_faultsim_test 3 -faults int* -prep { + sqlite3 db test.db + } -body { + execsql { SELECT * FROM o1('{b c}:b NOT a:a') ORDER BY rank } + expr 1 + } -test { + faultsim_test_result {0 1} {1 {vtable constructor failed: o1}} + } + + do_faultsim_test 4 -faults int* -prep { + sqlite3 db test.db + } -body { + execsql { SELECT * FROM o1('b:b OR a:a') } + expr 1 + } -test { + faultsim_test_result {0 1} {1 {vtable constructor failed: o1}} + } + + do_faultsim_test 5 -faults int* -prep { + sqlite3 db test.db + } -body { + execsql { SELECT count(*) FROM o1('c:b') } + expr 1 + } -test { + faultsim_test_result {0 1} {1 {vtable constructor failed: o1}} + } +} + +finish_test diff --git a/ext/fts5/test/fts5faultE.test b/ext/fts5/test/fts5faultE.test new file mode 100644 index 0000000000..07a4542102 --- /dev/null +++ b/ext/fts5/test/fts5faultE.test @@ -0,0 +1,71 @@ +# 2016 February 2 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# +# This file is focused on OOM errors. +# + +source [file join [file dirname [info script]] fts5_common.tcl] +source $testdir/malloc_common.tcl +set testprefix fts5faultE + +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. +ifcapable !fts5 { + finish_test + return +} + +faultsim_save_and_close +do_faultsim_test 1 -prep { + faultsim_restore_and_reopen +} -body { + execsql { CREATE VIRTUAL TABLE t1 USING fts5(x, y, tokenize=trigram) } +} -test { + faultsim_test_result {0 {}} {1 {vtable constructor failed: t1}} +} + +reset_db +do_execsql_test 2.0 { + CREATE VIRTUAL TABLE t1 USING fts5(x, y, tokenize=trigram); +} + +faultsim_save_and_close +do_faultsim_test 2 -prep { + faultsim_restore_and_reopen +} -body { + execsql { + INSERT INTO t1 VALUES('abcdefghijklmnopqrstuvwxyz', NULL); + SELECT count(*) FROM t1 WHERE x LIKE '%mnop%' AND t1 MATCH 'jkl'; + } +} -test { + faultsim_test_result {0 1} {1 {vtable constructor failed: t1}} +} + +reset_db +do_execsql_test 3.0 { + CREATE VIRTUAL TABLE t1 USING fts5(x, y, tokenize=trigram, detail=none); + INSERT INTO t1 VALUES('abcdefghijklmnopqrstuvwxyz', NULL); +} + +faultsim_save_and_close +do_faultsim_test 3 -prep { + faultsim_restore_and_reopen +} -body { + execsql { + SELECT count(*) FROM t1 WHERE x LIKE '%mnopqrs%' AND t1 MATCH 'abc' + } +} -test { + faultsim_test_result {0 1} {1 {vtable constructor failed: t1}} +} + + + +finish_test + diff --git a/ext/fts5/test/fts5faultF.test b/ext/fts5/test/fts5faultF.test new file mode 100644 index 0000000000..96cc2b083f --- /dev/null +++ b/ext/fts5/test/fts5faultF.test @@ -0,0 +1,111 @@ +# 2023 July 20 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# +# This file is focused on OOM errors. Particularly those that may occur +# when using contentless_delete=1 databases. +# + +source [file join [file dirname [info script]] fts5_common.tcl] +source $testdir/malloc_common.tcl +set testprefix fts5faultF + +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. +ifcapable !fts5 { + finish_test + return +} + +faultsim_save_and_close +do_faultsim_test 1 -prep { + faultsim_restore_and_reopen +} -body { + execsql { + CREATE VIRTUAL TABLE t1 USING fts5(x, y, content=, contentless_delete=1) + } +} -test { + faultsim_test_result {0 {}} {1 {vtable constructor failed: t1}} +} + +reset_db +do_execsql_test 2.0 { + CREATE VIRTUAL TABLE t1 USING fts5(doc, content=, contentless_delete=1); + BEGIN; + INSERT INTO t1(rowid, doc) VALUES(1, 'a b c d'); + INSERT INTO t1(rowid, doc) VALUES(2, 'a b c d'); + INSERT INTO t1(rowid, doc) VALUES(3, 'a b c d'); + INSERT INTO t1(rowid, doc) VALUES(4, 'a b c d'); + COMMIT; + DELETE FROM t1 WHERE rowid IN (2, 4); +} + +do_faultsim_test 2 -prep { + sqlite3 db test.db + execsql { SELECT rowid FROM t1 } +} -body { + execsql { + SELECT rowid FROM t1('b c'); + } +} -test { + faultsim_test_result {0 {1 3}} +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 3.0 { + CREATE VIRTUAL TABLE t1 USING fts5(doc, content=, contentless_delete=1); + BEGIN; + INSERT INTO t1(rowid, doc) VALUES(1, 'a b c d'); + INSERT INTO t1(rowid, doc) VALUES(2, 'a b c d'); + INSERT INTO t1(rowid, doc) VALUES(3, 'a b c d'); + INSERT INTO t1(rowid, doc) VALUES(4, 'a b c d'); + COMMIT; +} + +faultsim_save_and_close +do_faultsim_test 3 -prep { + faultsim_restore_and_reopen + execsql { SELECT rowid FROM t1 } +} -body { + execsql { + INSERT INTO t1(rowid, doc) VALUES(5, 'a b c d'); + } +} -test { + faultsim_test_result {0 {}} +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 4.0 { + CREATE VIRTUAL TABLE t1 USING fts5(doc, content=, contentless_delete=1); + INSERT INTO t1(t1, rank) VALUES('pgsz', 64); + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<1000 + ) + INSERT INTO t1(rowid, doc) SELECT i, 'a b c d' FROM s; +} + +do_execsql_test 4.1 { DELETE FROM t1 WHERE rowid <= 25 } + +faultsim_save_and_close +do_faultsim_test 4 -faults oom-t* -prep { + faultsim_restore_and_reopen + execsql { SELECT rowid FROM t1 } +} -body { + execsql { + DELETE FROM t1 WHERE rowid < 100 + } +} -test { + faultsim_test_result {0 {}} +} + + +finish_test + diff --git a/ext/fts5/test/fts5faultG.test b/ext/fts5/test/fts5faultG.test new file mode 100644 index 0000000000..9110c6336d --- /dev/null +++ b/ext/fts5/test/fts5faultG.test @@ -0,0 +1,76 @@ +# 2010 June 15 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +source [file join [file dirname [info script]] fts5_common.tcl] +source $testdir/malloc_common.tcl +set testprefix fts5faultG + +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. +ifcapable !fts5 { + finish_test + return +} + +set ::testprefix fts5faultG + + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE t1 USING fts5(a); + INSERT INTO t1 VALUES('test renaming the table'); + INSERT INTO t1 VALUES(' after it has been written'); + INSERT INTO t1 VALUES(' actually other stuff instead'); +} +faultsim_save_and_close +do_faultsim_test 1 -faults oom* -prep { + faultsim_restore_and_reopen + execsql { + BEGIN; + DELETE FROM t1 WHERE rowid=2; + } +} -body { + execsql { + DELETE FROM t1; + } +} -test { + catchsql { COMMIT } + faultsim_integrity_check + faultsim_test_result {0 {}} +} + +reset_db +do_execsql_test 2.0 { + CREATE VIRTUAL TABLE t1 USING fts5(a, content=, contentless_delete=1); + BEGIN; + INSERT INTO t1 VALUES('here''s some text'); + INSERT INTO t1 VALUES('useful stuff, text'); + INSERT INTO t1 VALUES('what would we do without text!'); + COMMIT; +} +faultsim_save_and_close +do_faultsim_test 2 -faults oom* -prep { + faultsim_restore_and_reopen + execsql { + BEGIN; + DELETE FROM t1 WHERE rowid=2; + } +} -body { + execsql { + INSERT INTO t1(t1) VALUES('optimize'); + } +} -test { + faultsim_integrity_check + faultsim_test_result {0 {}} +} + + + +finish_test diff --git a/ext/fts5/test/fts5faultH.test b/ext/fts5/test/fts5faultH.test new file mode 100644 index 0000000000..0cbbf7f5ef --- /dev/null +++ b/ext/fts5/test/fts5faultH.test @@ -0,0 +1,150 @@ +# 2010 June 15 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +source [file join [file dirname [info script]] fts5_common.tcl] +source $testdir/malloc_common.tcl +set testprefix fts5faultG + +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. +ifcapable !fts5 { + finish_test + return +} + +set ::testprefix fts5faultH + +sqlite3_fts5_register_origintext db + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE t1 USING fts5( + x, tokenize="origintext unicode61", tokendata=1 + ); + + BEGIN; + INSERT INTO t1 VALUES('oNe tWo thRee'); + INSERT INTO t1 VALUES('One Two Three'); + INSERT INTO t1 VALUES('onE twO threE'); + COMMIT; + BEGIN; + INSERT INTO t1 VALUES('one two three'); + INSERT INTO t1 VALUES('one two three'); + INSERT INTO t1 VALUES('one two three'); + COMMIT; +} + +do_faultsim_test 1 -faults oom* -prep { +} -body { + execsql { + SELECT rowid FROM t1('three'); + } +} -test { + faultsim_integrity_check + faultsim_test_result {0 {1 2 3 4 5 6}} +} + + +reset_db +sqlite3_fts5_register_origintext db +do_execsql_test 2.0 { + CREATE VIRTUAL TABLE t1 USING fts5( + x, tokenize="origintext unicode61", tokendata=1 + ); + INSERT INTO t1(t1, rank) VALUES('pgsz', 64); + + BEGIN; + INSERT INTO t1(rowid, x) VALUES(10, 'aaa bbb BBB'); + INSERT INTO t1(rowid, x) VALUES(12, 'bbb bbb bbb'); + INSERT INTO t1(rowid, x) VALUES(13, 'bbb bbb bbb'); + INSERT INTO t1(rowid, x) VALUES(14, 'bbb BBB bbb'); + INSERT INTO t1(rowid, x) VALUES(15, 'bbb bbb bbb'); + INSERT INTO t1(rowid, x) VALUES(16, 'bbb bbb bbb'); + INSERT INTO t1(rowid, x) VALUES(17, 'bbb bbb bbb'); + INSERT INTO t1(rowid, x) VALUES(18, 'bbb bbb bbb'); + INSERT INTO t1(rowid, x) VALUES(19, 'bbb bbb bbb'); + INSERT INTO t1(rowid, x) VALUES(20, 'bbb bbb bbb'); + INSERT INTO t1(rowid, x) VALUES(21, 'bbb bbb bbb'); + INSERT INTO t1(rowid, x) VALUES(22, 'bbb bbb bbb'); + INSERT INTO t1(rowid, x) VALUES(23, 'bbb bbb bbb'); + INSERT INTO t1(rowid, x) VALUES(24, 'aaa bbb BBB'); + COMMIT; +} + +do_faultsim_test 2 -faults oom* -prep { +} -body { + execsql { + SELECT rowid FROM t1('BBB AND AAA'); + } +} -test { + faultsim_integrity_check + faultsim_test_result {0 {10 24}} +} + +reset_db +sqlite3_fts5_register_origintext db +do_execsql_test 3.0 { + CREATE VIRTUAL TABLE t1 USING fts5( + x, tokenize="origintext unicode61", tokendata=1 + ); + INSERT INTO t1(t1, rank) VALUES('pgsz', 64); + + INSERT INTO t1(rowid, x) VALUES(9, 'bbb Bbb BBB'); + BEGIN; + INSERT INTO t1(rowid, x) VALUES(10, 'aaa bbb BBB'); + INSERT INTO t1(rowid, x) VALUES(11, 'bbb Bbb BBB'); + INSERT INTO t1(rowid, x) VALUES(12, 'bbb Bbb BBB'); + INSERT INTO t1(rowid, x) VALUES(13, 'bbb Bbb BBB'); + INSERT INTO t1(rowid, x) VALUES(14, 'bbb Bbb BBB'); + INSERT INTO t1(rowid, x) VALUES(15, 'bbb Bbb BBB'); + INSERT INTO t1(rowid, x) VALUES(16, 'bbb Bbb BBB'); + INSERT INTO t1(rowid, x) VALUES(17, 'bbb Bbb BBB'); + INSERT INTO t1(rowid, x) VALUES(18, 'bbb Bbb BBB'); + INSERT INTO t1(rowid, x) VALUES(19, 'bbb Bbb BBB'); + INSERT INTO t1(rowid, x) VALUES(20, 'bbb Bbb BBB'); + INSERT INTO t1(rowid, x) VALUES(21, 'bbb Bbb BBB'); + INSERT INTO t1(rowid, x) VALUES(22, 'bbb Bbb BBB'); + INSERT INTO t1(rowid, x) VALUES(23, 'bbb Bbb BBB'); + INSERT INTO t1(rowid, x) VALUES(24, 'bbb Bbb BBB'); + INSERT INTO t1(rowid, x) VALUES(25, 'bbb Bbb BBB'); + INSERT INTO t1(rowid, x) VALUES(26, 'bbb Bbb BBB'); + INSERT INTO t1(rowid, x) VALUES(27, 'bbb Bbb BBB'); + INSERT INTO t1(rowid, x) VALUES(28, 'bbb Bbb BBB'); + INSERT INTO t1(rowid, x) VALUES(29, 'bbb Bbb BBB'); + INSERT INTO t1(rowid, x) VALUES(30, 'bbb Bbb BBB'); + INSERT INTO t1(rowid, x) VALUES(31, 'bbb Bbb BBB'); + INSERT INTO t1(rowid, x) VALUES(32, 'bbb Bbb BBB'); + INSERT INTO t1(rowid, x) VALUES(33, 'bbb Bbb BBB'); + INSERT INTO t1(rowid, x) VALUES(34, 'bbb Bbb BBB'); + INSERT INTO t1(rowid, x) VALUES(35, 'aaa bbb BBB'); + COMMIT; +} + +do_faultsim_test 3.1 -faults oom* -prep { +} -body { + execsql { + SELECT rowid FROM t1('BBB AND AAA'); + } +} -test { + faultsim_integrity_check + faultsim_test_result {0 {10 35}} +} +do_faultsim_test 3.2 -faults oom* -prep { +} -body { + execsql { + SELECT count(*) FROM t1('BBB'); + } +} -test { + faultsim_integrity_check + faultsim_test_result {0 27} +} + + +finish_test diff --git a/ext/fts5/test/fts5faultI.test b/ext/fts5/test/fts5faultI.test new file mode 100644 index 0000000000..a2b04af8f5 --- /dev/null +++ b/ext/fts5/test/fts5faultI.test @@ -0,0 +1,349 @@ +# 2010 June 15 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +source [file join [file dirname [info script]] fts5_common.tcl] +source $testdir/malloc_common.tcl +set testprefix fts5faultI + +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. +ifcapable !fts5 { + finish_test + return +} + +set ::testprefix fts5faultI + +do_execsql_test 1.0 { + PRAGMA encoding = utf16; + CREATE VIRTUAL TABLE t1 USING fts5(x, locale=1); + INSERT INTO t1 VALUES('origintext unicode61 ascii porter trigram'); +} + +faultsim_save_and_close +faultsim_restore_and_reopen + +do_faultsim_test 1 -faults oom* -prep { +} -body { + execsql { + SELECT rowid FROM t1(fts5_locale('en_US', 'origintext')); + } +} -test { + faultsim_test_result {0 1} +} + +do_faultsim_test 2 -faults oom* -prep { + faultsim_restore_and_reopen + execsql { + SELECT * FROM t1('ascii'); + } +} -body { + execsql { + UPDATE t1 SET rowid=rowid+1; + } +} -test { + faultsim_test_result {0 {}} +} + +fts5_aux_test_functions db +do_faultsim_test 3 -faults oom* -prep { +} -body { + execsql { + SELECT fts5_columnlocale(t1, 0) FROM t1('unicode*'); + } +} -test { + faultsim_test_result {0 {{}}} {1 SQLITE_NOMEM} +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 4.0 { + CREATE VIRTUAL TABLE w1 USING fts5(a); +} +faultsim_save_and_close + +do_faultsim_test 4 -faults oom* -prep { + faultsim_restore_and_reopen + execsql { + BEGIN; + INSERT INTO w1 VALUES('token token token'); + } +} -body { + execsql { + INSERT INTO w1(w1, rank) VALUES('rank', 'bm25()'); + } +} -test { + faultsim_test_result {0 {}} +} + +do_faultsim_test 5 -faults oom* -prep { + faultsim_restore_and_reopen + execsql { + BEGIN; + INSERT INTO w1 VALUES('one'); + SAVEPOINT one; + INSERT INTO w1 VALUES('two'); + ROLLBACK TO one; + } + +} -body { + execsql { + INSERT INTO w1 VALUES('string'); + } +} -test { + faultsim_test_result {0 {}} +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 5.0 { + CREATE VIRTUAL TABLE w1 USING fts5(a); + INSERT INTO w1 VALUES('one two three'); +} +fts5_aux_test_functions db + +do_faultsim_test 5 -faults oom* -prep { +} -body { + execsql { + SELECT fts5_test_insttoken(w1, 0, 0) FROM w1('two'); + } +} -test { + faultsim_test_result {0 two} {1 SQLITE_NOMEM} +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 6.0 { + CREATE VIRTUAL TABLE w1 USING fts5(a); + INSERT INTO w1 VALUES('one two three'); +} +fts5_aux_test_functions db +faultsim_save_and_close + +do_faultsim_test 6 -faults oom* -prep { + faultsim_restore_and_reopen + db eval { + BEGIN; + INSERT INTO w1 VALUES('four five six'); + SAVEPOINT abc; + INSERT INTO w1 VALUES('seven eight nine'); + SAVEPOINT def; + INSERT INTO w1 VALUES('ten eleven twelve'); + } +} -body { + execsql { + RELEASE abc; + } +} -test { + faultsim_test_result {0 {}} +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 7.0 { + CREATE VIRTUAL TABLE w1 USING fts5(a); + INSERT INTO w1 VALUES('one two three'); + INSERT INTO w1 VALUES('three two one'); + DELETE FROM w1_content WHERE rowid=1; +} + +faultsim_save_and_close + +do_faultsim_test 7 -faults oom* -prep { + faultsim_restore_and_reopen + db eval { SELECT * FROM w1 } +} -body { + execsql { + PRAGMA integrity_check; + } +} -test { +} + +#------------------------------------------------------------------------- +reset_db +fts5_tclnum_register db +fts5_aux_test_functions db + +do_execsql_test 8.0 { + CREATE VIRTUAL TABLE ft USING fts5( + x, tokenize = "tclnum query", detail=columns + ); + INSERT INTO ft VALUES('one two three i ii iii'); + INSERT INTO ft VALUES('four five six iv v vi'); + INSERT INTO ft VALUES('eight nine ten viii ix x'); +} {} + +do_faultsim_test 8.1 -faults oom* -prep { +} -body { + execsql { + SELECT fts5_test_collist (ft) FROM ft('one two'); + } +} -test { + faultsim_test_result {0 {{0.0 1.0}}} {1 {SQL logic error}} {1 SQLITE_NOMEM} +} + +do_faultsim_test 8.2 -faults oom* -prep { +} -body { + execsql { + SELECT rowid FROM ft('one two') ORDER BY rank; + } +} -test { + faultsim_test_result {0 1} {1 {SQL logic error}} {1 SQLITE_NOMEM} +} + +#------------------------------------------------------------------------- +reset_db + +do_execsql_test 9.0 { + CREATE VIRTUAL TABLE ft USING fts5(x); + INSERT INTO ft VALUES('one two three i ii iii'); + INSERT INTO ft VALUES('four five six iv v vi'); + INSERT INTO ft VALUES('eight nine ten viii ix x'); +} {} + +faultsim_save_and_close + +do_faultsim_test 9.1 -faults oom* -prep { + faultsim_restore_and_reopen +} -body { + execsql { + UPDATE ft SET rowid=4 WHERE rowid=1 + } +} -test { + faultsim_test_result {0 {}} +} + +do_faultsim_test 9.2 -faults oom* -prep { + faultsim_restore_and_reopen +} -body { + execsql { + SELECT rowid FROM ft WHERE x MATCH 'one AND two AND three' + } +} -test { + faultsim_test_result {0 1} +} + +#------------------------------------------------------------------------- +reset_db + +do_execsql_test 10.0 { + CREATE VIRTUAL TABLE ft USING fts5(x, locale=1); + INSERT INTO ft VALUES(fts5_locale('hello', 'one two three i ii iii')); + INSERT INTO ft VALUES('four five six iv v vi'); + INSERT INTO ft VALUES('eight nine ten viii ix x'); +} {} + +do_execsql_test 10.1 { + SELECT fts5_get_locale(ft, 0) FROM ft WHERE x MATCH 'one AND two AND three' +} {hello} + +faultsim_save_and_close +do_faultsim_test 10.1 -faults oom* -prep { + faultsim_restore_and_reopen +} -body { + execsql { + SELECT fts5_get_locale(ft, 0) FROM ft WHERE x MATCH 'one AND two AND three' + } +} -test { + faultsim_test_result {0 hello} +} + +breakpoint +faultsim_save_and_close +do_faultsim_test 10.2 -faults oom-t* -prep { + faultsim_restore_and_reopen +} -body { + execsql { + INSERT INTO ft VALUES(zeroblob(10000)); + } +} -test { + faultsim_test_result {0 {}} +} + +#------------------------------------------------------------------------- +reset_db + +do_execsql_test 11.0 { + CREATE VIRTUAL TABLE f1 USING fts5(content); + CREATE TABLE g1(id, content); + INSERT INTO g1 VALUES(30000, 'a b c'); + INSERT INTO g1 VALUES(40000, 'd e f'); +} + +faultsim_save_and_close + +do_faultsim_test 11 -faults oom* -prep { + faultsim_restore_and_reopen +} -body { + execsql { + INSERT INTO f1(rowid, content) SELECT id, content FROM g1; + } +} -test { + faultsim_test_result {0 {}} +} + +#------------------------------------------------------------------------- +reset_db + +ifcapable foreignkey { + do_execsql_test 12.0 { + CREATE VIRTUAL TABLE f1 USING fts5(content); + CREATE TABLE p1(a INTEGER PRIMARY KEY); + CREATE TABLE c1(b REFERENCES p1 DEFERRABLE INITIALLY DEFERRED); + } + + faultsim_save_and_close + + do_faultsim_test 11 -faults oom* -prep { + faultsim_restore_and_reopen + execsql { + PRAGMA foreign_keys = 1; + BEGIN; + INSERT INTO c1 VALUES(123); + SAVEPOINT xyz; + } + } -body { + execsql { + INSERT INTO f1 VALUES('a b c'); + ROLLBACK TO xyz; + COMMIT; + } + } -test { + execsql { SELECT 123 } + faultsim_test_result \ + {1 {FOREIGN KEY constraint failed}} \ + {1 {out of memory}} \ + {1 {constraint failed}} + } +} + +#------------------------------------------------------------------------- +reset_db + +do_execsql_test 13.0 { + CREATE VIRTUAL TABLE t1 USING fts5(a, b); + INSERT INTO t1 VALUES('abc def', X'123456'); +} +faultsim_save_and_close + + +do_faultsim_test 13 -faults oom* -prep { + faultsim_restore_and_reopen +} -body { + execsql { + UPDATE t1 SET a='def abc' + } +} -test { + faultsim_test_result {0 {}} +} + +finish_test + diff --git a/ext/fts5/test/fts5first.test b/ext/fts5/test/fts5first.test new file mode 100644 index 0000000000..492681eed7 --- /dev/null +++ b/ext/fts5/test/fts5first.test @@ -0,0 +1,96 @@ +# 2017 November 25 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** + +source [file join [file dirname [info script]] fts5_common.tcl] +set testprefix fts5first + +ifcapable !fts5 { + finish_test + return +} + + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE x1 USING fts5(a, b); +} + +unset -nocomplain res +foreach {tn expr ok} { + 1 {^abc} 1 + 2 {^abc + def} 1 + 3 {^ "abc def"} 1 + 4 {^"abc def"} 1 + 5 {abc ^def} 1 + 6 {abc + ^def} 0 + 7 {abc ^+ def} 0 + 8 {"^abc"} 1 + 9 {NEAR(^abc def)} 0 +} { + set res(0) {/1 {fts5: syntax error near .*}/} + set res(1) {0 {}} + + do_catchsql_test 1.$tn { SELECT * FROM x1($expr) } $res($ok) +} + +#------------------------------------------------------------------------- +# +do_execsql_test 2.0 { + INSERT INTO x1 VALUES('a b c', 'b c a'); +} + +foreach {tn expr match} { + 1 {^a} 1 + 2 {^b} 1 + 3 {^c} 0 + 4 {^a + b} 1 + 5 {^b + c} 1 + 6 {^c + a} 0 + 7 {^"c a"} 0 + 8 {a:^a} 1 + 9 {a:^b} 0 + 10 {a:^"a b"} 1 +} { + do_execsql_test 2.$tn { SELECT EXISTS (SELECT rowid FROM x1($expr)) } $match +} + +#------------------------------------------------------------------------- +# +do_execsql_test 3.0 { + DELETE FROM x1; + INSERT INTO x1 VALUES('b a', 'c a'); + INSERT INTO x1 VALUES('a a', 'c c'); + INSERT INTO x1 VALUES('a b', 'a a'); +} +fts5_aux_test_functions db + +foreach {tn expr expect} { + 1 {^a} {{2 1}} + 2 {^c AND ^b} {{0 2} {1 0}} +} { + do_execsql_test 3.$tn { + SELECT fts5_test_queryphrase(x1) FROM x1($expr) LIMIT 1 + } [list $expect] +} + +#------------------------------------------------------------------------- +# +do_execsql_test 3.1 { + CREATE VIRTUAL TABLE x2 USING fts5(a, b, c, detail=column); +} + +do_catchsql_test 3.2 { + SELECT * FROM x2('a + b'); +} {1 {fts5: phrase queries are not supported (detail!=full)}} + +do_catchsql_test 3.3 { + SELECT * FROM x2('^a'); +} {1 {fts5: phrase queries are not supported (detail!=full)}} +finish_test diff --git a/ext/fts5/test/fts5full.test b/ext/fts5/test/fts5full.test index c640f56e06..76fdc0288f 100644 --- a/ext/fts5/test/fts5full.test +++ b/ext/fts5/test/fts5full.test @@ -17,7 +17,7 @@ source [file join [file dirname [info script]] fts5_common.tcl] set testprefix fts5full -# If SQLITE_ENABLE_FTS5 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return @@ -36,8 +36,7 @@ do_test 1.1 { execsql { INSERT INTO x8 VALUES( rnddoc(5) ); } } } msg] $msg -} {1 {database or disk is full}} +} {0 {}} finish_test - diff --git a/ext/fts5/test/fts5fuzz1.test b/ext/fts5/test/fts5fuzz1.test new file mode 100644 index 0000000000..52dc23d77c --- /dev/null +++ b/ext/fts5/test/fts5fuzz1.test @@ -0,0 +1,92 @@ +# 2014 June 17 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# This file implements regression tests for SQLite library. The +# focus of this script is testing the FTS5 module. +# + +source [file join [file dirname [info script]] fts5_common.tcl] +return_if_no_fts5 +set testprefix fts5fuzz1 + + +#------------------------------------------------------------------------- +reset_db +do_catchsql_test 1.1 { + CREATE VIRTUAL TABLE f1 USING fts5(a b); +} {/1 {parse error in.*}/} + + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 2.1 { + CREATE VIRTUAL TABLE f1 USING fts5(a, b); + INSERT INTO f1 VALUES('a b', 'c d'); + INSERT INTO f1 VALUES('e f', 'a b'); +} + +do_execsql_test 2.2.1 { + SELECT rowid FROM f1('""'); +} {} + +do_execsql_test 2.2.2 { + SELECT rowid FROM f1('"" AND a'); +} {} + + +do_execsql_test 2.2.3 { + SELECT rowid FROM f1('"" a'); +} {1 2} + +do_execsql_test 2.2.4 { + SELECT rowid FROM f1('"" OR a'); +} {1 2} + +do_execsql_test 2.3 { + SELECT a, b FROM f1('NEAR("")'); +} {} + +do_execsql_test 2.4 { + SELECT a, b FROM f1('NEAR("", 5)'); +} {} + +do_execsql_test 2.5 { + SELECT a, b FROM f1('NEAR("" c, 5)'); +} {{a b} {c d}} + +do_execsql_test 2.6 { + SELECT a, b FROM f1('NEAR("" c d, 5)'); +} {{a b} {c d}} + +do_execsql_test 2.7 { + SELECT a, b FROM f1('NEAR(c d, 5)'); +} {{a b} {c d}} + +do_execsql_test 2.8 { + SELECT rowid FROM f1('NEAR("a" "b", 5)'); +} {1 2} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 3.2 { + CREATE VIRTUAL TABLE f2 USING fts5(o, t, tokenize="ascii separators abc"); + SELECT * FROM f2('a+4'); +} {} + + + +#------------------------------------------------------------------------- +reset_db +do_catchsql_test 4.1 { + CREATE VIRTUAL TABLE f2 USING fts5(o, t); + SELECT * FROM f2('(8 AND 9)`AND 10'); +} {1 {fts5: syntax error near "`"}} + +finish_test diff --git a/ext/fts5/test/fts5hash.test b/ext/fts5/test/fts5hash.test index f3952d6157..b3d8b562c8 100644 --- a/ext/fts5/test/fts5hash.test +++ b/ext/fts5/test/fts5hash.test @@ -15,7 +15,7 @@ source [file join [file dirname [info script]] fts5_common.tcl] set testprefix fts5hash -# If SQLITE_ENABLE_FTS5 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return @@ -112,7 +112,7 @@ foreach_detail_mode $testprefix { # Add a small and very large token with the same hash value to an # empty table. At one point this would provoke an asan error. # - do_test 2.0 { + do_test 1.5 { set big [string repeat 12345 40] set hash [sqlite3_fts5_token_hash 1024 $big] while {1} { @@ -121,7 +121,6 @@ foreach_detail_mode $testprefix { } execsql { CREATE VIRTUAL TABLE t2 USING fts5(x, detail=%DETAIL%) } -breakpoint execsql { INSERT INTO t2 VALUES($small || ' ' || $big); } @@ -129,5 +128,41 @@ breakpoint } ;# foreach_detail_mode -finish_test +#------------------------------------------------------------------------- +reset_db +do_execsql_test 2.1 { + CREATE VIRTUAL TABLE t1 USING fts5(x); + INSERT INTO t1(t1, rank) VALUES('hashsize', 1024); + INSERT INTO t1(t1, rank) VALUES('automerge', 0); + INSERT INTO t1(t1, rank) VALUES('crisismerge', 1000); +} + +do_execsql_test 2.2 { + BEGIN; + INSERT INTO t1 VALUES('abc def ghi'); + SELECT count(*) FROM t1_data; +} {2} +do_execsql_test 2.3 { + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<1024 + ) + INSERT INTO t1 SELECT 'abc def ghi' FROM s; + SELECT (SELECT count(*) FROM t1_data) > 10; +} {1} + +do_execsql_test 2.4 { + COMMIT; + DROP TABLE t1; + CREATE VIRTUAL TABLE t1 USING fts5(x); + INSERT INTO t1(t1, rank) VALUES('hashsize', 1024); + INSERT INTO t1(t1, rank) VALUES('automerge', 0); + INSERT INTO t1(t1, rank) VALUES('crisismerge', 1000); + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<1024 + ) + INSERT INTO t1 SELECT 'abc' || i || ' def' || i || ' ghi' || i FROM s; + SELECT (SELECT count(*) FROM t1_data) > 100; +} {1} + +finish_test diff --git a/ext/fts5/test/fts5integrity.test b/ext/fts5/test/fts5integrity.test index 37ca9331dd..4bf120c446 100644 --- a/ext/fts5/test/fts5integrity.test +++ b/ext/fts5/test/fts5integrity.test @@ -15,7 +15,7 @@ source [file join [file dirname [info script]] fts5_common.tcl] set testprefix fts5integrity -# If SQLITE_ENABLE_FTS5 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return @@ -37,6 +37,12 @@ do_execsql_test 2.1 { INSERT INTO yy(yy) VALUES('integrity-check'); } +db close +sqlite3 db test.db +do_execsql_test 2.1 { + INSERT INTO yy(yy) VALUES('integrity-check'); +} + #-------------------------------------------------------------------- # do_execsql_test 3.0 { @@ -71,11 +77,15 @@ do_execsql_test 4.1 { INSERT INTO aa(aa) VALUES('integrity-check'); } +sqlite3_db_config db DEFENSIVE 0 do_catchsql_test 4.2 { BEGIN; UPDATE aa_docsize SET sz = X'44' WHERE rowid = 3; INSERT INTO aa(aa) VALUES('integrity-check'); } {1 {database disk image is malformed}} +do_execsql_test 4.2.1 { + PRAGMA integrity_check(aa); +} {{malformed inverted index for FTS5 table main.aa}} do_catchsql_test 4.3 { ROLLBACK; @@ -149,6 +159,7 @@ do_execsql_test 5.3 { INSERT INTO gg(gg) VALUES('integrity-check'); } +unset -nocomplain res do_test 5.4.1 { set ok 0 for {set i 0} {$i < 10000} {incr i} { @@ -187,11 +198,11 @@ foreach {tn pgsz} { INSERT INTO hh(hh, rank) VALUES('pgsz', $pgsz); WITH s(i) AS (SELECT 0 UNION ALL SELECT i+1 FROM s WHERE i<999) - INSERT INTO hh SELECT printf("%.3d%.3d%.3d %.3d%.3d%.3d",i,i,i,i+1,i+1,i+1) + INSERT INTO hh SELECT printf('%.3d%.3d%.3d %.3d%.3d%.3d',i,i,i,i+1,i+1,i+1) FROM s; WITH s(i) AS (SELECT 0 UNION ALL SELECT i+1 FROM s WHERE i<999) - INSERT INTO hh SELECT printf("%.3d%.3d%.3d %.3d%.3d%.3d",i,i,i,i+1,i+1,i+1) + INSERT INTO hh SELECT printf('%.3d%.3d%.3d %.3d%.3d%.3d',i,i,i,i+1,i+1,i+1) FROM s; INSERT INTO hh(hh) VALUES('optimize'); @@ -209,5 +220,199 @@ foreach {tn pgsz} { } {1000} } -finish_test +#------------------------------------------------------------------------- +# +reset_db +do_execsql_test 7.0 { + PRAGMA encoding = 'UTF-16'; + CREATE VIRTUAL TABLE vt0 USING fts5(c0); + INSERT INTO vt0 VALUES (x'46f0'); + SELECT quote(c0) FROM vt0; +} {X'46F0'} +do_execsql_test 7.1 { + INSERT INTO vt0(vt0) VALUES('integrity-check'); +} +do_execsql_test 7.2 { + INSERT INTO vt0(vt0) VALUES('rebuild'); +} +do_execsql_test 7.3 { + INSERT INTO vt0(vt0) VALUES('integrity-check'); +} +do_execsql_test 7.4 { + UPDATE vt0 SET c0=''; +} +do_execsql_test 7.5 { + INSERT INTO vt0(vt0) VALUES('integrity-check'); +} + +#------------------------------------------------------------------------- +# Ticket 7a458c2a5f4 +# +reset_db +do_execsql_test 8.0 { + PRAGMA locking_mode = EXCLUSIVE; + PRAGMA journal_mode = PERSIST; + CREATE VIRTUAL TABLE vt0 USING fts5(c0); +} {exclusive persist} +do_execsql_test 8.1 { + PRAGMA data_version +} {1} +do_execsql_test 8.2 { + INSERT INTO vt0(vt0) VALUES('integrity-check'); + PRAGMA data_version; +} {1} +do_execsql_test 8.1 { + INSERT INTO vt0(vt0, rank) VALUES('usermerge', 2); +} + +#------------------------------------------------------------------------- +# Ticket [771fe617] +# +reset_db +do_execsql_test 9.0 { + PRAGMA encoding = 'UTF16'; + CREATE VIRTUAL TABLE vt0 USING fts5(c0); +} + +#explain_i { SELECT quote(SUBSTR(x'37', 0)); } +#execsql { PRAGMA vdbe_trace = 1 } +do_execsql_test 9.1.1 { + SELECT quote(SUBSTR(x'37', 0)); +} {X'37'} +do_execsql_test 9.1.2 { + SELECT quote(x'37'); +} {X'37'} + +do_execsql_test 9.2 { + INSERT INTO vt0 VALUES (SUBSTR(x'37', 0)); +-- INSERT INTO vt0 VALUES (x'37'); +} +do_execsql_test 9.3 { + INSERT INTO vt0(vt0) VALUES('integrity-check'); +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 10.0 { + CREATE TABLE t1(i INTEGER PRIMARY KEY, a, b); + CREATE VIRTUAL TABLE vt0 USING fts5(a, b, content=t1); + INSERT INTO vt0(rowid, a, b) VALUES(1, 'abc', 'def'); +} +do_catchsql_test 10.1 { + INSERT INTO vt0(vt0) VALUES('integrity-check'); +} {0 {}} +do_catchsql_test 10.2 { + INSERT INTO vt0(vt0, rank) VALUES('integrity-check', 0); +} {0 {}} +do_catchsql_test 10.3 { + INSERT INTO vt0(vt0, rank) VALUES('integrity-check', 1); +} {1 {database disk image is malformed}} +do_catchsql_test 10.3 { + INSERT INTO t1 VALUES(1, 'abc', 'def'); + INSERT INTO vt0(vt0, rank) VALUES('integrity-check', 1); +} {0 {}} + +do_execsql_test 10.4 { + CREATE VIRTUAL TABLE vt1 USING fts5(a, b, content=); + INSERT INTO vt1(rowid, a, b) VALUES(1, 'abc', 'def'); +} + +do_catchsql_test 10.5.1 { + INSERT INTO vt0(vt0, rank) VALUES('integrity-check', 0); +} {0 {}} +do_catchsql_test 10.5.2 { + INSERT INTO vt0(vt0, rank) VALUES('integrity-check', 1); +} {0 {}} +do_catchsql_test 10.5.3 { + INSERT INTO vt0(vt0) VALUES('integrity-check'); +} {0 {}} + +reset_db +proc slang {in} {return [string map {th d e eh} $in]} +db function slang -deterministic -innocuous slang +do_execsql_test 11.0 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT, c TEXT AS (slang(b))); + INSERT INTO t1(b) VALUES('the quick fox jumps over the lazy brown dog'); + SELECT c FROM t1; +} {{deh quick fox jumps ovehr deh lazy brown dog}} + +do_execsql_test 11.1 { + CREATE VIRTUAL TABLE t2 USING fts5(content="t1", c); + INSERT INTO t2(t2) VALUES('rebuild'); + SELECT rowid FROM t2 WHERE t2 MATCH 'deh'; +} {1} + +do_execsql_test 11.2 { + PRAGMA integrity_check(t2); +} {ok} +db close +sqlite3 db test.db + +# FIX ME? +# +# FTS5 integrity-check does not care if the content table is unreadable or +# does not exist. It only looks for internal inconsistencies in the +# inverted index. +# +do_execsql_test 11.3 { + PRAGMA integrity_check(t2); +} {ok} +do_execsql_test 11.4 { + DROP TABLE t1; + PRAGMA integrity_check(t2); +} {ok} + +#------------------------------------------------------------------- +reset_db + +do_execsql_test 12.1 { + CREATE VIRTUAL TABLE x1 USING fts5(a, b); + INSERT INTO x1 VALUES('one', 'two'); + INSERT INTO x1 VALUES('three', 'four'); + INSERT INTO x1 VALUES('five', 'six'); +} + +do_execsql_test 12.2 { + PRAGMA integrity_check +} {ok} + +db close +sqlite3 db test.db -readonly 1 + +explain_i { + PRAGMA integrity_check + } +do_execsql_test 12.3 { + PRAGMA integrity_check +} {ok} + + +#------------------------------------------------------------------- +reset_db +do_execsql_test 13.1 { + CREATE VIRTUAL TABLE t1 USING fts5(a, tokenize=ascii); + INSERT INTO t1 VALUES('a b c'), ('d e f'); + PRAGMA integrity_check; +} {ok} +db close +sqlite3 db test.db +do_catchsql_test 13.2 { + PRAGMA integrity_check; +} {0 ok} + +do_execsql_test 13.3 { + PRAGMA writable_schema = 1; + UPDATE sqlite_schema SET sql = 'CREATE VIRTUAL TABLE t1 USING fts5(a, tokenize=blah)' + WHERE name = 't1'; +} + +db close +sqlite3 db test.db +breakpoint +do_catchsql_test 13.4 { + PRAGMA integrity_check; +} {1 {SQL logic error}} + + +finish_test diff --git a/ext/fts5/test/fts5integrity2.test b/ext/fts5/test/fts5integrity2.test new file mode 100644 index 0000000000..968be3bddf --- /dev/null +++ b/ext/fts5/test/fts5integrity2.test @@ -0,0 +1,56 @@ +# 2024 September 3 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# This file contains tests focused on the integrity-check procedure. +# + +source [file join [file dirname [info script]] fts5_common.tcl] +set testprefix fts5integrity2 + +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. +ifcapable !fts5 { + finish_test + return +} + +do_execsql_test 2.0 { + CREATE VIRTUAL TABLE t2 USING fts5(a, detail='none'); + BEGIN; + INSERT INTO t2(rowid, a) VALUES(-1, 'hello world'); + INSERT INTO t2(rowid, a) VALUES(9223372036854775807, 'hello world'); + COMMIT; +} + +do_execsql_test 2.1 { + SELECT rowid FROM t2('hello AND world'); +} {-1 9223372036854775807} + +#------------------------------------------------------------------------- +do_execsql_test 2.0 { + CREATE VIRTUAL TABLE t1 USING fts5(a, detail='none'); + CREATE TABLE r1(r); + + WITH c(x) AS (VALUES(1) UNION SELECT x<<1 FROM c) + INSERT INTO r1(r) SELECT -1-x FROM c; + + INSERT INTO t1(rowid, a) SELECT r, 'abc' FROM r1; +} + +do_execsql_test 2.1 { + PRAGMA integrity_check; +} {ok} + +do_execsql_test 2.2 { + SELECT rowid FROM t1('abc') ORDER BY +rowid; +} [db eval {SELECT r FROM r1 ORDER BY r}] + + +finish_test diff --git a/ext/fts5/test/fts5interrupt.test b/ext/fts5/test/fts5interrupt.test new file mode 100644 index 0000000000..67ef5f7e97 --- /dev/null +++ b/ext/fts5/test/fts5interrupt.test @@ -0,0 +1,67 @@ +# 2019 Jan 4 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# This file implements regression tests for SQLite library. The +# focus of this script is testing the FTS5 module. +# + +source [file join [file dirname [info script]] fts5_common.tcl] +set testprefix fts5interrupt + +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. +ifcapable !fts5 { + finish_test + return +} + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE t1 USING fts5(a); + INSERT INTO t1(t1, rank) VALUES('pgsz', 40); +} +db_save_and_close + +proc progress_handler {args} { + incr ::progress_handler_delay -1 + if {$::progress_handler_delay<=0} { return 1 } + return 0 +} + +unset -nocomplain res +foreach {tn sql} { + 1 { INSERT INTO t1(rowid, a) VALUES(0, 'z z z z') } + 2 { COMMIT } +} { + set bDone 0 + for {set i 1} {$bDone==0} {incr i} { + do_test 1.$tn.$i { + db_restore_and_reopen + execsql { + BEGIN; + INSERT INTO t1(rowid, a) VALUES(1, 'a b c d'); + INSERT INTO t1(rowid, a) VALUES(2, 'd e f g'); + INSERT INTO t1(rowid, a) VALUES(3, 'h i j k'); + INSERT INTO t1(rowid, a) VALUES(4, 'l m n o'); + } + + set ::progress_handler_delay $i + db progress 1 progress_handler + set res [catchsql $sql] + db close + if {$res=="0 {}"} { + set bDone 1 + } else { + if {$res!="1 interrupted"} { error "got: $res" } + } + set {} {} + } {} + } +} + +finish_test diff --git a/ext/fts5/test/fts5join.test b/ext/fts5/test/fts5join.test new file mode 100644 index 0000000000..e4d3b69b79 --- /dev/null +++ b/ext/fts5/test/fts5join.test @@ -0,0 +1,69 @@ +# 2014 June 17 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# This file implements regression tests for SQLite library. The +# focus of this script is testing the FTS5 module. +# + +source [file join [file dirname [info script]] fts5_common.tcl] +set testprefix fts5join + +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. +ifcapable !fts5 { + finish_test + return +} + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE vt USING fts5(x); + INSERT INTO vt VALUES('abc'); + INSERT INTO vt VALUES('xyz'); + + CREATE TABLE t1(a INTEGER PRIMARY KEY, b TIMESTAMP); + INSERT INTO t1 VALUES(1, 1), (2, 2); + CREATE INDEX i1 ON t1(b); +} + +# set sqlite_where_trace [expr 0xFFF] + +do_eqp_test 1.1 { + SELECT * FROM vt, t1 WHERE vt.rowid = t1.rowid ORDER BY t1.rowid; +} { + QUERY PLAN + |--SCAN t1 + `--SCAN vt VIRTUAL TABLE INDEX 0:= +} + +do_eqp_test 1.2 { + SELECT * FROM vt, t1 WHERE vt.rowid = t1.rowid AND b>? ORDER BY b LIMIT 10 +} { + QUERY PLAN + |--SEARCH t1 USING COVERING INDEX i1 (b>?) + `--SCAN vt VIRTUAL TABLE INDEX 0:= +} + +do_eqp_test 1.3 { + SELECT * FROM vt, t1 WHERE vt.rowid = t1.rowid AND b>? +} { + QUERY PLAN + |--SEARCH t1 USING COVERING INDEX i1 (b>?) + `--SCAN vt VIRTUAL TABLE INDEX 0:= +} + +do_eqp_test 1.4 { + SELECT * FROM vt, t1 WHERE vt.rowid = t1.rowid ORDER BY b +} { + QUERY PLAN + |--SCAN t1 USING COVERING INDEX i1 + `--SCAN vt VIRTUAL TABLE INDEX 0:= +} + + +finish_test diff --git a/ext/fts5/test/fts5lastrowid.test b/ext/fts5/test/fts5lastrowid.test new file mode 100644 index 0000000000..75866139d3 --- /dev/null +++ b/ext/fts5/test/fts5lastrowid.test @@ -0,0 +1,72 @@ +# 2017 Feb 27 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# Tests of the last_insert_rowid functionality with fts5. +# + +source [file join [file dirname [info script]] fts5_common.tcl] +set testprefix fts5lastrowid + +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. +ifcapable !fts5 { + finish_test + return +} + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE t1 USING fts5(str); +} + +do_execsql_test 1.1 { + INSERT INTO t1 VALUES('one string'); + INSERT INTO t1 VALUES('two string'); + INSERT INTO t1 VALUES('three string'); + SELECT last_insert_rowid(); +} {3} + +do_execsql_test 1.2 { + BEGIN; + INSERT INTO t1 VALUES('one string'); + INSERT INTO t1 VALUES('two string'); + INSERT INTO t1 VALUES('three string'); + COMMIT; + SELECT last_insert_rowid(); +} {6} + +do_execsql_test 1.3 { + INSERT INTO t1(rowid, str) VALUES(-22, 'some more text'); + SELECT last_insert_rowid(); +} {-22} + +do_execsql_test 1.4 { + BEGIN; + INSERT INTO t1(rowid, str) VALUES(45, 'some more text'); + INSERT INTO t1(rowid, str) VALUES(46, 'some more text'); + INSERT INTO t1(rowid, str) VALUES(222, 'some more text'); + SELECT last_insert_rowid(); + COMMIT; + SELECT last_insert_rowid(); +} {222 222} + +do_execsql_test 1.5 { + CREATE TABLE x1(x); + INSERT INTO x1 VALUES('john'), ('paul'), ('george'), ('ringo'); + INSERT INTO t1 SELECT x FROM x1; + SELECT last_insert_rowid(); +} {226} + +do_execsql_test 1.6 { + INSERT INTO t1(rowid, str) SELECT rowid+10, x FROM x1; + SELECT last_insert_rowid(); +} {14} + + +finish_test diff --git a/ext/fts5/test/fts5leftjoin.test b/ext/fts5/test/fts5leftjoin.test new file mode 100644 index 0000000000..69a172bd45 --- /dev/null +++ b/ext/fts5/test/fts5leftjoin.test @@ -0,0 +1,92 @@ +# 2014 June 17 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# This file implements regression tests for SQLite library. The +# focus of this script is testing the FTS5 module. +# + +source [file join [file dirname [info script]] fts5_common.tcl] +set testprefix fts5leftjoin + +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. +ifcapable !fts5 { + finish_test + return +} + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE vt USING fts5(x); + INSERT INTO vt VALUES('abc'); + INSERT INTO vt VALUES('xyz'); + + CREATE TABLE t1(a INTEGER PRIMARY KEY); + INSERT INTO t1 VALUES(1), (2); +} + +do_execsql_test 1.1 { + SELECT * FROM t1 LEFT JOIN ( + SELECT rowid AS rrr, * FROM vt WHERE vt MATCH 'abc' + ) ON t1.a = rrr +} {1 1 abc 2 {} {}} + +do_execsql_test 1.2 { + SELECT * FROM t1 LEFT JOIN vt ON (vt MATCH 'abc') +} {1 abc 2 abc} + + +do_execsql_test 1.3 { + DELETE FROM t1; + INSERT INTO t1 VALUES(14); +} + +do_execsql_test 1.4 { + SELECT * FROM vt LEFT JOIN t1 ON vt.rowid = 1; +} { + abc 14 + xyz {} +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 2.0 { + CREATE VIRTUAL TABLE t0 USING fts5(a,b); + INSERT INTO t0(a,b)VALUES(1,0); + CREATE TABLE t1(x); +} + +do_execsql_test 2.1 { + SELECT * FROM t0 LEFT JOIN t1; +} {1 0 {}} + +breakpoint +do_catchsql_test 2.2 { + SELECT * FROM t0 LEFT JOIN t1 ON t0.b MATCH '1'; +} {1 {no query solution}} + +do_execsql_test 2.3 { + SELECT * FROM t0 LEFT JOIN t1 ON +b MATCH '1'; +} {1 0 {}} + +#------------------------------------------------------------------------- +reset_db + +do_execsql_test 3.0 { + CREATE VIRTUAL TABLE t0 USING fts5(c0, c1); + INSERT INTO t0(c0,c1) VALUES (1,0); +} + +do_catchsql_test 3.1 { + SELECT * FROM t0 + LEFT JOIN ( SELECT 0 AS col_0 ) + ON ((((t0.c1 MATCH '1')AND(CASE WHEN t0.c0 THEN CAST(t0.c1 AS INTEGER) ELSE 1 END)))); +} {1 {no query solution}} + + +finish_test diff --git a/ext/fts5/test/fts5limits.test b/ext/fts5/test/fts5limits.test new file mode 100644 index 0000000000..90d175aa31 --- /dev/null +++ b/ext/fts5/test/fts5limits.test @@ -0,0 +1,47 @@ +# 2023 May 16 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# + +source [file join [file dirname [info script]] fts5_common.tcl] +set testprefix fts5limits +return_if_no_fts5 + + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE ft USING fts5(x); +} + +# Default limit for expression depth is 256 +# +foreach {tn nRepeat op bErr} { + 1 200 AND 0 + 2 200 NOT 0 + 3 200 OR 0 + + 4 260 AND 0 + 5 260 NOT 1 + 6 260 OR 0 +} { + set L [string repeat "abc " $nRepeat] + set Q [join $L " $op "] + + set res {0 {}} + if {$bErr} { + set res "1 {fts5 expression tree is too large (maximum depth 256)}" + } + + do_catchsql_test 1.$tn { + SELECT * FROM ft($Q) + } $res +} + +finish_test + diff --git a/ext/fts5/test/fts5locale.test b/ext/fts5/test/fts5locale.test new file mode 100644 index 0000000000..e5799fb7fd --- /dev/null +++ b/ext/fts5/test/fts5locale.test @@ -0,0 +1,748 @@ +# 2014 Dec 20 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# Tests focusing on the built-in fts5 tokenizers. +# + +source [file join [file dirname [info script]] fts5_common.tcl] +set testprefix fts5locale + +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. +ifcapable !fts5 { + finish_test + return +} + +proc transform_token {locale token} { + switch -- $locale { + reverse { + set ret "" + foreach c [split $token ""] { + set ret "$c$ret" + } + set token $ret + } + + default { + # no-op + } + } + + set token +} + +proc tcl_create {args} { return "tcl_tokenize" } +proc tcl_tokenize {tflags text} { + set iToken 1 + set bSkip 0 + if {[sqlite3_fts5_locale]=="second"} { set bSkip 1 } + foreach {w iStart iEnd} [fts5_tokenize_split $text] { + incr iToken + if {(($iToken) % ($bSkip + 1))} continue + + set w [transform_token [sqlite3_fts5_locale] $w] + sqlite3_fts5_token $w $iStart $iEnd + } +} + +#------------------------------------------------------------------------- +# Check that queries can have a locale attached to them. +# +reset_db +sqlite3_fts5_create_tokenizer -v2 db tcl tcl_create + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE t1 USING fts5(a, tokenize=tcl); + INSERT INTO t1 VALUES('abc'); + INSERT INTO t1 VALUES('cba'); +} {} + +do_execsql_test 1.1 { + SELECT rowid, a FROM t1( fts5_locale('en_US', 'abc') ); +} {1 abc} + +do_execsql_test 1.2 { + SELECT rowid, a FROM t1( fts5_locale('reverse', 'abc') ); +} {2 cba} + + +#------------------------------------------------------------------------- +# Test that the locale= option exists and seems to accept values. And +# that fts5_locale() values may only be inserted into an internal-content +# table if the locale=1 option was specified. +# +reset_db +sqlite3_fts5_create_tokenizer -v2 db tcl tcl_create + +do_execsql_test 2.1 { + CREATE VIRTUAL TABLE b1 USING fts5(x, y, locale=1, tokenize=tcl); + CREATE VIRTUAL TABLE b2 USING fts5(x, y, locale=0, tokenize=tcl); + + CREATE VIRTUAL TABLE ttt USING fts5vocab('b1', instance); +} + +do_catchsql_test 2.2.1 { + CREATE VIRTUAL TABLE b3 USING fts5(x, y, locale=2); +} {1 {malformed locale=... directive}} +do_catchsql_test 2.2.2 { + CREATE VIRTUAL TABLE b3 USING fts5(x, y, locale=111); +} {1 {malformed locale=... directive}} + +do_catchsql_test 2.3 { + INSERT INTO b1(b1, rank) VALUES('locale', 0); +} {1 {SQL logic error}} + +do_execsql_test 2.4.1 { + INSERT INTO b1 VALUES('abc', 'one two three'); +} + +do_execsql_test 2.4.2 { + INSERT INTO b1 VALUES('def', fts5_locale('reverse', 'four five six')); +} + +do_execsql_test 2.5 { + INSERT INTO b2 VALUES('abc', 'one two three'); +} + +do_catchsql_test 2.6 { + INSERT INTO b2 VALUES('def', fts5_locale('reverse', 'four five six')); +} {1 {fts5_locale() requires locale=1}} + +do_execsql_test 2.7 { SELECT rowid FROM b1('one') } {1} +do_execsql_test 2.8 { SELECT rowid FROM b1('four') } {} +do_execsql_test 2.9 { SELECT rowid FROM b1('ruof') } 2 +do_execsql_test 2.10 { SELECT rowid FROM b1(fts5_locale('reverse', 'five'))} 2 + +do_execsql_test 2.11 { + SELECT x, quote(y) FROM b1 +} { + abc {'one two three'} + def {'four five six'} +} + +do_execsql_test 2.12 { SELECT quote(y) FROM b1('ruof') } { + {'four five six'} +} + +do_execsql_test 2.13 { + INSERT INTO b1(b1) VALUES('integrity-check'); +} + +do_execsql_test 2.14 { + INSERT INTO b1(b1) VALUES('rebuild'); +} +do_execsql_test 2.15 { + INSERT INTO b1(b1) VALUES('integrity-check'); +} + +do_execsql_test 2.16 { + DELETE FROM b1 WHERE rowid=2 +} +do_execsql_test 2.17 { + INSERT INTO b1(b1) VALUES('integrity-check'); +} + +do_execsql_test 2.18 { + INSERT INTO b1(rowid, x, y) VALUES( + test_setsubtype(45, 76), 'abc def', 'def abc' + ); +} + +#------------------------------------------------------------------------- +# Test the 'delete' command with contentless tables. +# +reset_db +sqlite3_fts5_create_tokenizer -v2 db tcl tcl_create + +do_execsql_test 3.1 { + CREATE VIRTUAL TABLE c1 USING fts5(x, content=, tokenize=tcl, locale=1); + CREATE VIRTUAL TABLE c2 USING fts5vocab('c1', instance); + + INSERT INTO c1 VALUES('hello world'); + INSERT INTO c1 VALUES( fts5_locale('reverse', 'one two three') ); +} + +do_execsql_test 3.2 { + SELECT DISTINCT term FROM c2 ORDER BY 1 +} { + eerht eno hello owt world +} + +do_execsql_test 3.3 { + INSERT INTO c1(c1, rowid, x) + VALUES('delete', 2, fts5_locale('reverse', 'one two three') ); +} + +do_execsql_test 3.4 { + SELECT DISTINCT term FROM c2 ORDER BY 1 +} { + hello world +} + +#------------------------------------------------------------------------- +# Test that an UPDATE that updates a subset of the columns does not +# magically discard the locale from those columns not updated. +# +reset_db +sqlite3_fts5_create_tokenizer -v2 db tcl tcl_create + +do_execsql_test 4.1 { + CREATE VIRTUAL TABLE d1 USING fts5(x, y, locale=1, tokenize=tcl); + CREATE VIRTUAL TABLE d2 USING fts5vocab('d1', instance); + + INSERT INTO d1(rowid, x, y) VALUES(1, 'abc', 'def'); + INSERT INTO d1(rowid, x, y) VALUES(2, 'ghi', fts5_locale('reverse', 'hello')); +} + +do_execsql_test 4.2 { + SELECT DISTINCT term FROM d2 ORDER BY 1 +} { + abc def ghi olleh +} + +do_execsql_test 4.3 { + UPDATE d1 SET x='jkl' WHERE rowid=2; +} + +do_execsql_test 4.4 { + SELECT DISTINCT term FROM d2 ORDER BY 1 +} { + abc def jkl olleh +} + +do_execsql_test 4.5 { + SELECT rowid, * FROM d1 +} { + 1 abc def + 2 jkl hello +} + +do_execsql_test 4.6 { + UPDATE d1 SET rowid=4 WHERE rowid=2 +} + +do_execsql_test 4.7 { + SELECT rowid, * FROM d1 +} { + 1 abc def + 4 jkl hello +} + +fts5_aux_test_functions db + +do_execsql_test 4.8.1 { + SELECT fts5_test_columntext(d1) FROM d1('jkl') +} {{jkl hello}} +do_execsql_test 4.8.2 { + SELECT fts5_test_columntext(d1) FROM d1(fts5_locale('reverse', 'hello')) +} {{jkl hello}} + +do_execsql_test 4.9 { + SELECT fts5_test_columnlocale(d1) FROM d1(fts5_locale('reverse', 'hello')) +} {{{} reverse}} + +do_execsql_test 4.10 { + SELECT fts5_test_columnlocale(d1) FROM d1 +} { + {{} {}} + {{} reverse} +} + +#------------------------------------------------------------------------- +# Test that if an fts5_locale() value is written to an UNINDEXED +# column it is stored as text. This is so that blobs and other values +# can also be stored as is. +# +reset_db +sqlite3_fts5_create_tokenizer -v2 db tcl tcl_create + +do_execsql_test 5.1 { + CREATE VIRTUAL TABLE t1 USING fts5( + x, y UNINDEXED, locale=1, tokenize=tcl + ); + + INSERT INTO t1(rowid, x, y) VALUES(111, + fts5_locale('reverse', 'one two three'), + fts5_locale('reverse', 'four five six') + ); +} + +do_execsql_test 5.2 { + SELECT rowid, x, y FROM t1 +} { + 111 {one two three} {four five six} +} + +do_execsql_test 5.3 { + SELECT typeof(c0), typeof(c1), typeof(l0) FROM t1_content +} { + text text text +} + +#------------------------------------------------------------------------- + +foreach {tn opt} { + 1 {} + 2 {, columnsize=0} +} { + reset_db + sqlite3_fts5_create_tokenizer -v2 db tcl tcl_create + do_execsql_test 6.$tn.1 " + CREATE VIRTUAL TABLE y1 USING fts5(t, locale=1, tokenize=tcl $opt); + " + + do_execsql_test 6.$tn.2 { + INSERT INTO y1(rowid, t) VALUES + (1, fts5_locale('second', 'the city of London')), + (2, fts5_locale('second', 'shall have all the old')), + (3, fts5_locale('second', 'Liberties and Customs')), + (4, fts5_locale('second', 'which it hath been used to have')); + } + + fts5_aux_test_functions db + + do_execsql_test 6.$tn.3 { + SELECT fts5_test_columnsize(y1) FROM y1 + } { + 2 3 2 4 + } + + do_execsql_test 6.$tn.4 { + SELECT rowid, fts5_test_columnsize(y1) FROM y1('shall'); + } { + 2 3 + } + + do_execsql_test 6.$tn.5 { + SELECT rowid, fts5_test_columnsize(y1) FROM y1('shall'); + } { + 2 3 + } + + do_execsql_test 6.$tn.6 { + SELECT rowid, fts5_test_columnsize(y1) FROM y1('have'); + } { + 4 4 + } + + do_execsql_test 6.$tn.7 { + SELECT rowid, highlight(y1, 0, '[', ']') FROM y1('have'); + } { + 4 {which it hath been used to [have]} + } + + do_execsql_test 6.$tn.8 { + SELECT rowid, + highlight(y1, 0, '[', ']'), + snippet(y1, 0, '[', ']', '...', 10) + FROM y1('Liberties + Customs'); + } { + 3 {[Liberties and Customs]} + {[Liberties and Customs]} + } +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 6.0 { + CREATE VIRTUAL TABLE x1 USING fts5(x); +} +do_catchsql_test 6.1 { + INSERT INTO x1(rowid, x) VALUES(123, fts5_locale('en_AU', 'hello world')); +} {1 {fts5_locale() requires locale=1}} + +do_execsql_test 6.2 { + SELECT typeof( fts5_locale(NULL, 'xyz') ), typeof( fts5_locale('', 'abc') ); +} {text text} + +#-------------------------------------------------------------------------- +# Test that fts5_locale() works with external-content tables. +# +reset_db +sqlite3_fts5_create_tokenizer -v2 db tcl tcl_create + +do_execsql_test 7.1 { + CREATE TABLE t1(ii INTEGER PRIMARY KEY, bb BLOB, tt TEXT, locale TEXT); + CREATE VIEW v1 AS + SELECT ii AS rowid, bb, fts5_locale(locale, tt) AS tt FROM t1; + + CREATE VIRTUAL TABLE ft USING fts5( + bb, tt, locale=1, tokenize=tcl, content=v1 + ); + + INSERT INTO t1 VALUES(1, NULL, 'one two three', NULL); + INSERT INTO t1 VALUES(2, '7800616263', 'four five six', 'reverse'); + INSERT INTO t1 VALUES(3, '000000007800616263', 'seven eight nine', 'second'); +} + +do_execsql_test 7.2 { + INSERT INTO ft(ft) VALUES('rebuild'); + INSERT INTO ft(ft) VALUES('integrity-check'); +} + +do_execsql_test 7.3 { + SELECT rowid, quote(bb), quote(tt) FROM ft +} { + 1 NULL {'one two three'} + 2 '7800616263' {'four five six'} + 3 '000000007800616263' {'seven eight nine'} +} + +do_execsql_test 7.4 { SELECT rowid FROM ft('six'); } +do_execsql_test 7.5 { SELECT rowid FROM ft(fts5_locale('reverse','six')); } 2 + +fts5_aux_test_functions db + +do_execsql_test 7.6 { + SELECT fts5_test_columnlocale(ft) FROM ft; +} { + {{} {}} {{} reverse} {{} second} +} + +#------------------------------------------------------------------------- +# Test that the porter tokenizer works with locales. +# +reset_db +sqlite3_fts5_create_tokenizer -v2 db tcl tcl_create + +do_execsql_test 8.1 { + CREATE VIRTUAL TABLE ft USING fts5(tt, locale=1, tokenize="porter tcl"); + CREATE VIRTUAL TABLE vocab USING fts5vocab('ft', instance); + + INSERT INTO ft(rowid, tt) VALUES + (111, fts5_locale('second', 'the porter tokenizer is a wrapper tokenizer')), + (222, fts5_locale('reverse', 'This value may also be set')); +} + +do_execsql_test 8.1 { + SELECT DISTINCT term FROM vocab ORDER BY 1 +} { + a eb eulav osla sihT te the token yam +} + +#------------------------------------------------------------------------- +# Test that position-lists (used by xInst, xPhraseFirst etc.) work with +# locales and modes other than detail=full. +# +foreach {tn detail} { + 1 detail=full + 2 detail=none + 3 detail=column +} { + reset_db + sqlite3_fts5_create_tokenizer -v2 db tcl tcl_create + do_execsql_test 9.$tn.0 " + CREATE VIRTUAL TABLE ft USING fts5(tt, locale=1, tokenize=tcl, $detail); + " + do_execsql_test 9.$tn.1 { + CREATE VIRTUAL TABLE vocab USING fts5vocab('ft', instance); + INSERT INTO ft(rowid, tt) VALUES + (-1, fts5_locale('second', 'it is an ancient mariner')); + } + + do_execsql_test 9.$tn.2 { + SELECT DISTINCT term FROM vocab + } {an it mariner} + + do_execsql_test 9.$tn.3 { + SELECT highlight(ft, 0, '[', ']') FROM ft('mariner') + } {{it is an ancient [mariner]}} +} + +#------------------------------------------------------------------------- +# Check some corrupt fts5_locale() blob formats are detected. +# +foreach_detail_mode $::testprefix { + + reset_db + sqlite3_fts5_create_tokenizer -v2 db tcl tcl_create + fts5_aux_test_functions db + do_execsql_test 10.1 { + CREATE TABLE x1(ii INTEGER PRIMARY KEY, x); + CREATE VIRTUAL TABLE ft USING fts5(x, + content=x1, content_rowid=ii, locale=1, detail=%DETAIL%, columnsize=0 + ); + + CREATE VIRTUAL TABLE ft2 USING fts5( + x, locale=1, detail=%DETAIL%, columnsize=0 + ); + } + + foreach {tn v} { + 1 X'001152' + 2 X'0011223344' + 3 X'00E0B2EB68656c6c6f' + 4 X'00E0B2EB0068656c6c6f' + } { + do_execsql_test 10.2.$tn.0 { INSERT INTO ft(ft) VALUES('delete-all') } + do_execsql_test 10.2.$tn.1 { DELETE FROM x1; } + do_execsql_test 10.2.$tn.2 " INSERT INTO x1 VALUES(NULL, $v) " + + do_catchsql_test 10.2.$tn.3 { + INSERT INTO ft(ft) VALUES('rebuild'); + } {0 {}} + + do_catchsql_test 10.2.$tn.4 " + SELECT * FROM ft( test_setsubtype($v, 76) ); + " {1 {fts5: syntax error near ""}} + + do_execsql_test 10.2.$tn.5 { + INSERT INTO ft(rowid, x) VALUES(1, 'hello world'); + } + + if {"%DETAIL%"=="full"} { + do_execsql_test 10.2.$tn.6 { + SELECT fts5_test_poslist(ft) FROM ft('world'); + } {0.0.1} + + do_execsql_test 10.2.$tn.7.1 { + SELECT fts5_test_columnsize(ft) FROM ft('world'); + } {1} + + do_execsql_test 10.2.$tn.7.2 { + SELECT fts5_test_columnlocale(ft) FROM ft('world'); + } {{{}}} + } + + do_catchsql_test 10.2.$tn.8 { + SELECT count(*) FROM ft('hello') + } {0 1} + + do_catchsql_test 10.2.$tn.9 { + PRAGMA integrity_check; + } {0 ok} + + do_execsql_test 10.2.$tn.10 { + DELETE FROM x1; + INSERT INTO x1(ii, x) VALUES(1, 'hello world'); + } + + do_catchsql_test 10.2.$tn.11 " + INSERT INTO ft(ft, rowid, x) VALUES('delete', 1, test_setsubtype($v,76) ) + " {0 {}} + + do_catchsql_test 10.2.$tn.12 " + INSERT INTO ft(rowid, x) VALUES(2, test_setsubtype($v,76) ) + " {0 {}} + + do_execsql_test 10.2.$tn.13 { + INSERT INTO ft2(rowid, x) VALUES(1, 'hello world'); + } + do_execsql_test 10.2.$tn.14 "UPDATE ft2_content SET c0=$v" + + do_catchsql_test 10.2.$tn.15 { + PRAGMA integrity_check; + } {0 {{malformed inverted index for FTS5 table main.ft2}}} + + do_execsql_test 10.2.$tn.16 { + DELETE FROM ft2_content; + INSERT INTO ft2(ft2) VALUES('rebuild'); + } + } + +} + +#------------------------------------------------------------------------- +# +reset_db +sqlite3_fts5_create_tokenizer -v2 db tcl tcl_create +fts5_aux_test_functions db +do_execsql_test 11.0 { + CREATE VIRTUAL TABLE x1 USING fts5(abc, locale=1); + INSERT INTO x1(rowid, abc) VALUES(123, fts5_locale('en_US', 'one two three')); +} + +do_catchsql_test 11.1 { + SELECT fts5_columnlocale(x1, -1) FROM x1('two'); +} {1 SQLITE_RANGE} +do_catchsql_test 11.2 { + SELECT fts5_columnlocale(x1, 1) FROM x1('two'); +} {1 SQLITE_RANGE} + +#------------------------------------------------------------------------- +# +reset_db +do_test 12.0 { + list [catch { + sqlite3_fts5_create_tokenizer -v2 -version 3 db tcl tcl_create + } msg] $msg +} {1 {error in fts5_api.xCreateTokenizer_v2()}} + +#------------------------------------------------------------------------- +# Tests for auxiliary function fts5_get_locale(). +# +reset_db + +# Check that if the table does not support locale=1, fts5_get_locale() +# always returns NULL. +do_execsql_test 13.1.0 { + CREATE VIRTUAL TABLE nolocale USING fts5(a, b); + INSERT INTO nolocale VALUES('one two three', 'four five six'); + INSERT INTO nolocale VALUES('three two one', 'seven eight nine'); +} +do_execsql_test 13.1.1 { + SELECT fts5_get_locale(nolocale, 0) IS NULL FROM nolocale; +} {1 1} +do_execsql_test 13.1.2 { + SELECT fts5_get_locale(nolocale, 1) IS NULL FROM nolocale('one + two'); +} {1} +do_execsql_test 13.1.3 { + SELECT fts5_get_locale(nolocale, 0) IS NULL FROM nolocale('one AND two'); +} {1 1} +do_execsql_test 13.1.4 { + SELECT + fts5_get_locale(nolocale, 1) IS NULL + FROM nolocale('three AND two') ORDER BY rank +} {1 1} +do_catchsql_test 13.1.5 { + SELECT fts5_get_locale(nolocale, 2) IS NULL FROM nolocale('three AND two'); +} {1 {column index out of range}} +do_catchsql_test 13.1.6 { + SELECT fts5_get_locale(nolocale, -1) IS NULL FROM nolocale('three AND two'); +} {1 {column index out of range}} +do_catchsql_test 13.1.7 { + SELECT fts5_get_locale(nolocale) IS NULL FROM nolocale('three AND two'); +} {1 {wrong number of arguments to function fts5_get_locale()}} +do_catchsql_test 13.1.8 { + SELECT fts5_get_locale(nolocale, 0, 0) IS NULL FROM nolocale('three AND two'); +} {1 {wrong number of arguments to function fts5_get_locale()}} +do_catchsql_test 13.1.9 { + SELECT fts5_get_locale(nolocale, 'text') FROM nolocale('three AND two'); +} {1 {non-integer argument passed to function fts5_get_locale()}} + + +# Check that if the table does support locale=1, fts5_get_locale() +# returns the locale of the identified row/column. +do_execsql_test 13.2.0 { + CREATE VIRTUAL TABLE ft USING fts5(a, b, locale=1); + INSERT INTO ft VALUES( + fts5_locale('th_TH', 'one two three'), 'four five six seven' + ); + INSERT INTO ft VALUES( + 'three two one', fts5_locale('en_AU', 'seven eight nine') + ); +} + +do_execsql_test 13.2.1 { + SELECT quote(fts5_get_locale(ft, 0)), quote(fts5_get_locale(ft, 1)) FROM ft +} { 'th_TH' NULL NULL 'en_AU' } +do_execsql_test 13.2.2 { + SELECT + quote(fts5_get_locale(ft, 0)), quote(fts5_get_locale(ft, 1)) + FROM ft('one AND three') +} { 'th_TH' NULL NULL 'en_AU' } +do_execsql_test 13.2.3 { + SELECT + quote(fts5_get_locale(ft, 0)), quote(fts5_get_locale(ft, 1)) + FROM ft('one AND three') ORDER BY rank +} { NULL 'en_AU' 'th_TH' NULL } +do_execsql_test 13.2.4 { + SELECT + quote(fts5_get_locale(ft, 0)), quote(fts5_get_locale(ft, 1)) + FROM ft('one AND three') ORDER BY rowid +} { 'th_TH' NULL NULL 'en_AU' } + +do_execsql_test 13.2.5 { + SELECT + quote(fts5_get_locale(ft, '0')), quote(fts5_get_locale(ft, 1)) + FROM ft('one AND three') ORDER BY rowid +} { 'th_TH' NULL NULL 'en_AU' } + +do_catchsql_test 13.2.6 { + SELECT + quote(fts5_get_locale(ft, '0.0')), quote(fts5_get_locale(ft, 1)) + FROM ft('one AND three') ORDER BY rowid +} {1 {non-integer argument passed to function fts5_get_locale()}} +do_catchsql_test 13.2.7 { + SELECT + quote(fts5_get_locale(ft, 0.0)), quote(fts5_get_locale(ft, 1)) + FROM ft('one AND three') ORDER BY rowid +} {1 {non-integer argument passed to function fts5_get_locale()}} + +#------------------------------------------------------------------------- +# Check that UPDATE statements that may affect more than one row work. +# +reset_db +do_execsql_test 14.1 { + CREATE VIRTUAL TABLE ft USING fts5(a, b, locale=1); +} + +do_execsql_test 14.2 { + INSERT INTO ft VALUES('hello', 'world'); +} + +do_execsql_test 14.3 { + UPDATE ft SET b = fts5_locale('en_AU', 'world'); +} + +do_execsql_test 14.4 { + INSERT INTO ft VALUES(X'abcd', X'1234'); +} {} + +do_execsql_test 14.5 { + SELECT quote(a), quote(b) FROM ft +} {'hello' 'world' X'ABCD' X'1234'} + +do_execsql_test 14.6 { + DELETE FROM ft; + INSERT INTO ft VALUES(NULL, 'null'); + INSERT INTO ft VALUES(123, 'int'); + INSERT INTO ft VALUES(345.0, 'real'); + INSERT INTO ft VALUES('abc', 'text'); + INSERT INTO ft VALUES(fts5_locale('abc', 'def'), 'text'); + + SELECT a, typeof(a), b FROM ft +} { + {} null null + 123 integer int + 345.0 real real + abc text text + def text text +} + +do_execsql_test 14.7 { + SELECT quote(c0), typeof(c0) FROM ft_content +} { + NULL null + 123 integer + 345.0 real + 'abc' text + 'def' text +} + +#------------------------------------------------------------------------- +# Check that inserting UNINDEXED columns between indexed columns of a +# locale=1 table does not cause a problem. +# +reset_db +sqlite3_fts5_create_tokenizer -v2 db tcl tcl_create +fts5_aux_test_functions db + +do_execsql_test 15.1 { + CREATE VIRTUAL TABLE ft USING fts5(a, b UNINDEXED, c, locale=1, tokenize=tcl); +} + +do_execsql_test 15.2 { + INSERT INTO ft VALUES('one', 'two', 'three'); + INSERT INTO ft VALUES('one', 'two', fts5_locale('loc', 'three')); +} + +do_execsql_test 15.3 { + SELECT c2, l2 FROM ft_content +} {three {} three loc} + +do_execsql_test 15.4 { + SELECT c, fts5_columnlocale(ft, 2) FROM ft +} {three {} three loc} + + +finish_test + diff --git a/ext/fts5/test/fts5matchinfo.test b/ext/fts5/test/fts5matchinfo.test index 06f4550b47..a3bce869fb 100644 --- a/ext/fts5/test/fts5matchinfo.test +++ b/ext/fts5/test/fts5matchinfo.test @@ -467,5 +467,81 @@ do_execsql_test 12.1 { } ;# foreach_detail_mode -finish_test +#------------------------------------------------------------------------- +# Test that a bad fts5() return is detected +# +reset_db +proc xyz {} {} +db func fts5 -argcount 1 xyz +do_test 13.1 { + list [catch { sqlite3_fts5_register_matchinfo db } msg] $msg +} {1 SQLITE_ERROR} + +#------------------------------------------------------------------------- +# Test that an invalid matchinfo() flag is detected +# +reset_db +sqlite3_fts5_register_matchinfo db +do_execsql_test 14.1 { + CREATE VIRTUAL TABLE x1 USING fts5(z); + INSERT INTO x1 VALUES('a b c a b c a b c'); +} {} + +do_catchsql_test 14.2 { + SELECT matchinfo(x1, 'd') FROM x1('a b c'); +} {1 {unrecognized matchinfo flag: d}} + +#------------------------------------------------------------------------- +# Test using matchinfo() and similar on a non-full-text query +# +do_execsql_test 15.0 { + CREATE VIRTUAL TABLE t1 USING fts5(x, y); + INSERT INTO t1 VALUES('a', 'b'); + INSERT INTO t1 VALUES('c', 'd'); +} +if {$tcl_platform(byteOrder)=="littleEndian"} { + set res {X'02000000'} +} else { + set res {X'00000002'} +} +do_execsql_test 15.1 { + SELECT quote(matchinfo(t1, 'n')) FROM t1 LIMIT 1; +} $res +do_execsql_test 15.2 { + DELETE FROM t1_content WHERE rowid=1; + SELECT quote(matchinfo(t1, 'n')) FROM t1 LIMIT 1; +} $res + +fts5_aux_test_functions db +do_execsql_test 15.3 { + SELECT fts5_test_all(t1) FROM t1 LIMIT 1; +} { + {columnsize {1 1} columntext {c d} columntotalsize {2 2} poslist {} tokenize {c d} rowcount 2} +} + +#------------------------------------------------------------------------- +# +reset_db +do_execsql_test 16.0 { + CREATE TABLE t1(x); + BEGIN EXCLUSIVE; +} + +do_test 16.1 { + set rc [catch { + sqlite3 db2 test.db + db2 eval {SELECT * FROM t1} + } errmsg] + lappend rc $errmsg +} {1 {database is locked}} + +do_execsql_test 16.2 { + ROLLBACK; +} + +do_test 16.3 { + catchsql { SELECT * FROM t1 } db2 +} {0 {}} + +finish_test diff --git a/ext/fts5/test/fts5merge.test b/ext/fts5/test/fts5merge.test index 9dd1ecd026..09c18245f3 100644 --- a/ext/fts5/test/fts5merge.test +++ b/ext/fts5/test/fts5merge.test @@ -15,7 +15,7 @@ source [file join [file dirname [info script]] fts5_common.tcl] set testprefix fts5merge -# If SQLITE_ENABLE_FTS5 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return @@ -45,7 +45,7 @@ proc do_merge1_test {testname nRowPerSeg} { WITH ii(i) AS (SELECT 1 UNION ALL SELECT i+1 FROM ii WHERE i<$::nRowPerSeg) INSERT INTO x8 SELECT repeat('x y ', i % 16) FROM ii; - INSERT INTO x8(x8, rank) VALUES('automerge', 2); + INSERT INTO x8(x8, rank) VALUES('usermerge', 2); } for {set tn 1} {[lindex [fts5_level_segs x8] 0]>0} {incr tn} { @@ -84,9 +84,9 @@ proc do_merge2_test {testname nRow} { execsql { INSERT INTO x8 VALUES( rnddoc(($i%16) + 5) ) } while {[not_merged x8]} { execsql { - INSERT INTO x8(x8, rank) VALUES('automerge', 2); + INSERT INTO x8(x8, rank) VALUES('usermerge', 2); INSERT INTO x8(x8, rank) VALUES('merge', 1); - INSERT INTO x8(x8, rank) VALUES('automerge', 16); + INSERT INTO x8(x8, rank) VALUES('usermerge', 16); INSERT INTO x8(x8) VALUES('integrity-check'); } } @@ -104,9 +104,9 @@ do_merge2_test 2.2 10 do_merge2_test 2.3 20 #------------------------------------------------------------------------- -# Test that an auto-merge will complete any merge that has already been +# Test that a merge will complete any merge that has already been # started, even if the number of input segments is less than the current -# value of the 'automerge' configuration parameter. +# value of the 'usermerge' configuration parameter. # db func rnddoc fts5_rnddoc @@ -119,7 +119,7 @@ do_execsql_test 3.1 { } do_test 3.2 { execsql { - INSERT INTO x8(x8, rank) VALUES('automerge', 4); + INSERT INTO x8(x8, rank) VALUES('usermerge', 4); INSERT INTO x8(x8, rank) VALUES('merge', 1); } fts5_level_segs x8 @@ -127,14 +127,14 @@ do_test 3.2 { do_test 3.3 { execsql { - INSERT INTO x8(x8, rank) VALUES('automerge', 2); + INSERT INTO x8(x8, rank) VALUES('usermerge', 2); INSERT INTO x8(x8, rank) VALUES('merge', 1); } fts5_level_segs x8 } {2 1} do_test 3.4 { - execsql { INSERT INTO x8(x8, rank) VALUES('automerge', 4) } + execsql { INSERT INTO x8(x8, rank) VALUES('usermerge', 4) } while {[not_merged x8]} { execsql { INSERT INTO x8(x8, rank) VALUES('merge', 1) } } @@ -176,7 +176,7 @@ foreach {tn pgsz} { INSERT INTO x8 SELECT mydoc() FROM ii; WITH ii(i) AS (SELECT 1 UNION ALL SELECT i+1 FROM ii WHERE i<100) INSERT INTO x8 SELECT mydoc() FROM ii; - INSERT INTO x8(x8, rank) VALUES('automerge', 2); + INSERT INTO x8(x8, rank) VALUES('usermerge', 2); } set expect [mycount] @@ -190,5 +190,70 @@ foreach {tn pgsz} { # db eval {SELECT fts5_decode(rowid, block) AS r FROM x8_data} { puts $r } } -finish_test +#------------------------------------------------------------------------- +# Test that the 'merge' command does not modify the database if there is +# no work to do. + +do_execsql_test 5.1 { + CREATE VIRTUAL TABLE x9 USING fts5(one, two); + INSERT INTO x9(x9, rank) VALUES('pgsz', 32); + INSERT INTO x9(x9, rank) VALUES('automerge', 2); + INSERT INTO x9(x9, rank) VALUES('usermerge', 2); + INSERT INTO x9 VALUES(rnddoc(100), rnddoc(100)); + INSERT INTO x9 VALUES(rnddoc(100), rnddoc(100)); + INSERT INTO x9 VALUES(rnddoc(100), rnddoc(100)); + INSERT INTO x9 VALUES(rnddoc(100), rnddoc(100)); + INSERT INTO x9 VALUES(rnddoc(100), rnddoc(100)); + INSERT INTO x9 VALUES(rnddoc(100), rnddoc(100)); + INSERT INTO x9 VALUES(rnddoc(100), rnddoc(100)); + INSERT INTO x9 VALUES(rnddoc(100), rnddoc(100)); +} + +do_test 5.2 { + while 1 { + set nChange [db total_changes] + execsql { INSERT INTO x9(x9, rank) VALUES('merge', 1); } + set nChange [expr [db total_changes] - $nChange] + #puts $nChange + if {$nChange<2} break + } +} {} + + +#-------------------------------------------------------------------------- +# Test that running 'merge' on an empty database does not cause a +# problem. +# +reset_db +do_execsql_test 6.0 { + CREATE VIRTUAL TABLE g1 USING fts5(a, b); +} +do_execsql_test 6.1 { + INSERT INTO g1(g1, rank) VALUES('merge', 10); +} +do_execsql_test 6.2 { + INSERT INTO g1(g1, rank) VALUES('merge', -10); +} +do_execsql_test 6.3 { + INSERT INTO g1(g1) VALUES('integrity-check'); +} + +#-------------------------------------------------------------------------- +# Check that passing -2147483648 as the parameter to a merge command +# does not cause a signed integer overflow error. +# +reset_db +do_execsql_test 7.0 { + CREATE VIRTUAL TABLE f1 USING fts5(a); +} +do_execsql_test 7.1 { + INSERT INTO f1 VALUES('one two three'); + INSERT INTO f1 VALUES('four five six'); + INSERT INTO f1 VALUES('seven eight nine'); +} +do_execsql_test 7.2 { + INSERT INTO f1(f1, rank) VALUES('merge', -2147483648); +} + +finish_test diff --git a/ext/fts5/test/fts5merge2.test b/ext/fts5/test/fts5merge2.test index 87dbf14332..e8f5bb161c 100644 --- a/ext/fts5/test/fts5merge2.test +++ b/ext/fts5/test/fts5merge2.test @@ -13,7 +13,8 @@ # source [file join [file dirname [info script]] fts5_common.tcl] -set testprefix fts5merge +set testprefix fts5merge2 +return_if_no_fts5 proc dump_structure {} { db eval {SELECT fts5_decode(id, block) AS t FROM t1_data WHERE id=10} { @@ -26,8 +27,6 @@ proc dump_structure {} { foreach_detail_mode $testprefix { -if {[detail_is_none]==0} continue - do_execsql_test 1.0 { CREATE VIRTUAL TABLE t1 USING fts5(x, detail=%DETAIL%); INSERT INTO t1(t1, rank) VALUES('pgsz', 32); @@ -56,4 +55,3 @@ do_execsql_test 1.2 { } finish_test - diff --git a/ext/fts5/test/fts5misc.test b/ext/fts5/test/fts5misc.test new file mode 100644 index 0000000000..817be9560c --- /dev/null +++ b/ext/fts5/test/fts5misc.test @@ -0,0 +1,701 @@ +# 2019 September 02 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# This file implements regression tests for SQLite library. The +# focus of this script is testing the FTS5 module. +# + +source [file join [file dirname [info script]] fts5_common.tcl] +set testprefix fts5misc + +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. +ifcapable !fts5 { + finish_test + return +} + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE t1 USING fts5(a); +} + +do_catchsql_test 1.1.1 { + SELECT highlight(t1, 4, '', '') FROM t1('*'); +} {1 {unknown special query: }} +do_catchsql_test 1.1.2 { + SELECT a FROM t1 + WHERE rank = (SELECT highlight(t1, 4, '', '') FROM t1('*')); +} {1 {unknown special query: }} + +do_catchsql_test 1.2.1 { + SELECT highlight(t1, 4, '', '') FROM t1('*id'); +} {1 {no such cursor: 4}} + +do_catchsql_test 1.2.2 { + SELECT a FROM t1 + WHERE rank = (SELECT highlight(t1, 4, '', '') FROM t1('*id')); +} {1 {no such cursor: 6}} + +do_catchsql_test 1.3.1 { + SELECT highlight(t1, 4, '', '') FROM t1('*reads'); +} {1 {no such cursor: 0}} + +do_catchsql_test 1.3.2 { + SELECT a FROM t1 + WHERE rank = (SELECT highlight(t1, 4, '', '') FROM t1('*reads')); +} {1 {no such cursor: 0}} + +db close +sqlite3 db test.db + +do_catchsql_test 1.3.3 { + SELECT a FROM t1 + WHERE rank = (SELECT highlight(t1, 4, '', '') FROM t1('*reads')); +} {1 {no such cursor: 0}} + +fts5_aux_test_functions db +do_catchsql_test 1.3.4 { + SELECT fts5_columntext(t1) FROM t1('*reads'); +} {1 {no such cursor: 0}} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 2.0 { + CREATE TABLE t0(c0); + CREATE VIRTUAL TABLE vt0 USING fts5(c0); +} +do_execsql_test 2.1.1 { + BEGIN TRANSACTION; + INSERT INTO vt0(c0) VALUES ('xyz'); +} +do_execsql_test 2.1.2 { + ALTER TABLE t0 ADD COLUMN c5; +} +do_execsql_test 2.1.3 { + INSERT INTO vt0(vt0) VALUES('integrity-check'); +} +do_execsql_test 2.1.4 { + INSERT INTO vt0(c0) VALUES ('abc'); + COMMIT +} +do_execsql_test 2.1.5 { + INSERT INTO vt0(vt0) VALUES('integrity-check'); +} + +reset_db +do_execsql_test 2.2.1 { + CREATE TABLE t0(c0); + CREATE VIRTUAL TABLE vt0 USING fts5(c0); + BEGIN TRANSACTION; + INSERT INTO vt0(c0) VALUES ('xyz'); +} + +do_execsql_test 2.2.2 { + ALTER TABLE t0 RENAME TO t1; +} +do_execsql_test 2.2.3 { + INSERT INTO vt0(vt0) VALUES('integrity-check'); +} +do_execsql_test 2.2.4 { + INSERT INTO vt0(c0) VALUES ('abc'); + COMMIT; +} +do_execsql_test 2.2.5 { + INSERT INTO vt0(vt0) VALUES('integrity-check'); +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 3.0 { + CREATE VIRTUAL TABLE vt0 USING fts5(a); + PRAGMA reverse_unordered_selects = true; + INSERT INTO vt0 VALUES('365062398'), (0), (0); + INSERT INTO vt0(vt0, rank) VALUES('pgsz', '38'); +} +do_execsql_test 3.1 { + UPDATE vt0 SET a = 399905135; -- unexpected: database disk image is malformed +} +do_execsql_test 3.2 { + INSERT INTO vt0(vt0) VALUES('integrity-check'); +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 4.0 { + CREATE VIRTUAL TABLE vt0 USING fts5(c0); + INSERT INTO vt0(c0) VALUES ('xyz'); +} + +do_execsql_test 4.1 { + BEGIN; + INSERT INTO vt0(c0) VALUES ('abc'); + INSERT INTO vt0(vt0) VALUES('rebuild'); + COMMIT; +} + +do_execsql_test 4.2 { + INSERT INTO vt0(vt0) VALUES('integrity-check'); +} + +do_execsql_test 4.3 { + BEGIN; + INSERT INTO vt0(vt0) VALUES('rebuild'); + INSERT INTO vt0(vt0) VALUES('rebuild'); + COMMIT; +} + +do_execsql_test 4.4 { + INSERT INTO vt0(vt0) VALUES('integrity-check'); +} + +#------------------------------------------------------------------------- +# Ticket [81a7f7b9]. +# +reset_db +do_execsql_test 5.0 { + CREATE VIRTUAL TABLE vt0 USING fts5(c0, c1); + INSERT INTO vt0(vt0, rank) VALUES('pgsz', '65536'); + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<1236 + ) + INSERT INTO vt0(c0) SELECT '0' FROM s; +} {} + +do_execsql_test 5.1 { + UPDATE vt0 SET c1 = 'T,D&p^y/7#3*v100; } {101} + +do_execsql_test 1.9 { + DELETE FROM ft; + INSERT INTO ft(ft) VALUES('optimize'); + SELECT count(*) FROM ft_data; +} {2} +do_execsql_test 1.10 { + BEGIN; + INSERT INTO ft VALUES('Hello'); + INSERT INTO ft VALUES('hello'); + INSERT INTO ft VALUES('HELLO'); + INSERT INTO ft VALUES('today'); + INSERT INTO ft VALUES('today'); + INSERT INTO ft VALUES('today'); + INSERT INTO ft VALUES('World'); + INSERT INTO ft VALUES('world'); + INSERT INTO ft VALUES('WORLD'); +} + +do_execsql_test 1.11 { SELECT rowid FROM ft('hello'); } {1 2 3} +do_execsql_test 1.12 { SELECT rowid FROM ft('today'); } {4 5 6} +do_execsql_test 1.13 { SELECT rowid FROM ft('world'); } {7 8 9} +do_execsql_test 1.14 { SELECT rowid FROM ft('hello') ORDER BY rank; } {1 2 3} + +#------------------------------------------------------------------------ +reset_db +sqlite3_fts5_register_origintext db +proc tokens {cmd} { + set ret [list] + for {set iTok 0} {$iTok < [$cmd xInstCount]} {incr iTok} { + set txt [$cmd xInstToken $iTok 0] + set txt [string map [list "\0" "."] $txt] + lappend ret $txt + } + set ret +} +sqlite3_fts5_create_function db tokens tokens + +do_execsql_test 2.0 { + CREATE VIRTUAL TABLE x1 USING fts5( + v, tokenize="origintext unicode61", tokendata=1, detail=none + ); + + INSERT INTO x1 VALUES('xxx Xxx XXX yyy YYY yyy'); + INSERT INTO x1 VALUES('xxx yyy xxx yyy yyy yyy'); +} + +do_execsql_test 2.1 { + SELECT tokens(x1) FROM x1('xxx'); +} { + {xxx xxx.Xxx xxx.XXX} {xxx xxx} +} + +do_execsql_test 2.2 { + UPDATE x1_content SET c0 = 'xxx xxX xxx yyy yyy yyy' WHERE id=1; +} + +do_execsql_test 2.3 { + SELECT tokens(x1) FROM x1('xxx'); +} { + {xxx {} xxx} {xxx xxx} +} + +finish_test + diff --git a/ext/fts5/test/fts5origintext3.test b/ext/fts5/test/fts5origintext3.test new file mode 100644 index 0000000000..351ab1f617 --- /dev/null +++ b/ext/fts5/test/fts5origintext3.test @@ -0,0 +1,141 @@ +# 2023 November 22 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# Tests focused on phrase queries. +# + +source [file join [file dirname [info script]] fts5_common.tcl] +set testprefix fts5origintext3 + +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. +ifcapable !fts5 { + finish_test + return +} + +foreach_detail_mode $testprefix { + foreach {tn insttoken} { + 1 0 + 2 1 + } { + + reset_db + + sqlite3_fts5_register_origintext db + fts5_aux_test_functions db + proc insttoken {cmd iIdx iToken} { + set txt [$cmd xInstToken $iIdx $iToken] + string map [list "\0" "."] $txt + } + sqlite3_fts5_create_function db insttoken insttoken + + do_execsql_test $tn.1.0 { + CREATE VIRTUAL TABLE ft USING fts5( + x, tokenize="origintext unicode61", tokendata=1, detail=%DETAIL% + ); + } + + do_execsql_test $tn.1.0.1 { + INSERT INTO ft(ft, rank) VALUES('insttoken', 1); + } + + do_execsql_test $tn.1.1 { + INSERT INTO ft VALUES('Hello world HELLO WORLD hello'); + } + + do_execsql_test $tn.1.2 { + SELECT fts5_test_poslist(ft) FROM ft('hello'); + } {{0.0.0 0.0.2 0.0.4}} + + do_execsql_test $tn.1.3 { + SELECT + insttoken(ft, 0, 0), + insttoken(ft, 1, 0), + insttoken(ft, 2, 0) + FROM ft('hello'); + } {hello.Hello hello.HELLO hello} + + do_execsql_test $tn.1.3.1 { + SELECT + insttoken(ft, 0, 0), + insttoken(ft, 1, 0), + insttoken(ft, 2, 0) + FROM ft('hel*'); + } {hello.Hello hello.HELLO hello} + + do_execsql_test $tn.1.4 { + SELECT + insttoken(ft, 0, 0), + insttoken(ft, 1, 0), + insttoken(ft, 2, 0) + FROM ft('hello') ORDER BY rank; + } {hello.Hello hello.HELLO hello} + + do_execsql_test $tn.1.5 { + CREATE VIRTUAL TABLE ft2 USING fts5( + x, tokenize="origintext unicode61", tokendata=1, detail=%DETAIL% + ); + INSERT INTO ft2(rowid, x) VALUES(1, 'ONE one two three ONE'); + INSERT INTO ft2(rowid, x) VALUES(2, 'TWO one two three TWO'); + INSERT INTO ft2(rowid, x) VALUES(3, 'THREE one two three THREE'); + } + + do_execsql_test $tn.1.6 { + SELECT insttoken(ft2, 0, 0), rowid FROM ft2('three') ORDER BY rank; + } {three.THREE 3 three 1 three 2} + + do_execsql_test $tn.1.7 { + INSERT INTO ft2(rowid, x) VALUES(10, 'aaa bbb BBB'); + INSERT INTO ft2(rowid, x) VALUES(12, 'bbb bbb bbb'); + INSERT INTO ft2(rowid, x) VALUES(13, 'bbb bbb bbb'); + INSERT INTO ft2(rowid, x) VALUES(14, 'bbb BBB bbb'); + INSERT INTO ft2(rowid, x) VALUES(15, 'bbb bbb bbb'); + INSERT INTO ft2(rowid, x) VALUES(16, 'bbb bbb bbb'); + INSERT INTO ft2(rowid, x) VALUES(17, 'bbb bbb bbb'); + INSERT INTO ft2(rowid, x) VALUES(18, 'bbb bbb bbb'); + INSERT INTO ft2(rowid, x) VALUES(19, 'bbb bbb bbb'); + INSERT INTO ft2(rowid, x) VALUES(20, 'bbb bbb bbb'); + INSERT INTO ft2(rowid, x) VALUES(21, 'bbb bbb bbb'); + INSERT INTO ft2(rowid, x) VALUES(22, 'bbb bbb bbb'); + INSERT INTO ft2(rowid, x) VALUES(23, 'bbb bbb bbb'); + INSERT INTO ft2(rowid, x) VALUES(24, 'aaa bbb BBB'); + } + + do_execsql_test $tn.1.8 { SELECT rowid FROM ft2('aaa AND bbb'); } {10 24} + do_execsql_test $tn.1.9 { SELECT rowid FROM ft2('bbb AND aaa'); } {10 24} + + do_execsql_test $tn.2.0 { + CREATE VIRTUAL TABLE ft3 USING fts5( + x, tokenize="origintext unicode61", tokendata=1, detail=%DETAIL%, + prefix=2 + ); + } + do_execsql_test $tn.2.1 { + INSERT INTO ft3(rowid, x) VALUES(1, 'one'); + INSERT INTO ft3(rowid, x) VALUES(2, 'ONE'); + INSERT INTO ft3(rowid, x) VALUES(3, 'ONT'); + INSERT INTO ft3(rowid, x) VALUES(4, 'on'); + INSERT INTO ft3(rowid, x) VALUES(5, 'On'); + } + + do_execsql_test $tn.2.2 { + SELECT rowid FROM ft3('on*'); + } {1 2 3 4 5} + + do_execsql_test $tn.2.3 { + SELECT rowid, insttoken(ft3, 0, 0) FROM ft3('on*'); + } {1 one 2 one.ONE 3 ont.ONT 4 on 5 on.On} + + } +} + +finish_test + diff --git a/ext/fts5/test/fts5origintext4.test b/ext/fts5/test/fts5origintext4.test new file mode 100644 index 0000000000..3b907ba2cc --- /dev/null +++ b/ext/fts5/test/fts5origintext4.test @@ -0,0 +1,80 @@ +# 2023 November 22 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# Tests focused on phrase queries. +# + +source [file join [file dirname [info script]] fts5_common.tcl] +set testprefix fts5origintext4 + +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. +ifcapable !fts5 { + finish_test + return +} + +# The tests below verify that a doclist-index is used to limit the number +# of pages loaded into the cache. It does this by querying sqlite3_db_status() +# for the amount of memory used by the pager cache. +# +# memsubsys1 effectively limits the page-cache to 24 pages. Which masks +# the effect tested by the tests in this file. And "mmap" prevents the +# cache from being used, also preventing these tests from working. +# +if {[permutation]=="memsubsys1" || [permutation]=="mmap"} { + finish_test + return +} + +sqlite3_fts5_register_origintext db +do_execsql_test 1.0 { + PRAGMA page_size = 4096; + CREATE VIRTUAL TABLE ft USING fts5( + x, tokenize="origintext unicode61", tokendata=1 + ); +} + +do_execsql_test 1.1 { + BEGIN; + INSERT INTO ft SELECT 'the first thing'; + + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<90000 + ) + INSERT INTO ft SELECT 'The second thing' FROM s; + + INSERT INTO ft SELECT 'the first thing'; + COMMIT; + INSERT INTO ft(ft) VALUES('optimize'); +} + +foreach {tn sql expr} { + 1 { SELECT rowid FROM ft('the') } {$mem > 250000} + 2 { SELECT rowid FROM ft('first') } {$mem < 50000} + 3 { SELECT rowid FROM ft('the first') } {$mem < 50000} +} { + db close + sqlite3 db test.db + sqlite3_fts5_register_origintext db + + execsql $sql + do_test 1.2.$tn { + set mem [lindex [sqlite3_db_status db CACHE_USED 0] 1] + expr $expr + } 1 +} + +proc b {x} { string map [list "\0" "."] $x } +db func b b +# execsql_pp { SELECT segid, b(term), pgno from ft_idx } + +finish_test + diff --git a/ext/fts5/test/fts5origintext5.test b/ext/fts5/test/fts5origintext5.test new file mode 100644 index 0000000000..848cc15b5c --- /dev/null +++ b/ext/fts5/test/fts5origintext5.test @@ -0,0 +1,273 @@ +# 2023 Dec 04 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# Tests for tables that use both tokendata=1 and contentless_delete=1. +# + +source [file join [file dirname [info script]] fts5_common.tcl] +set testprefix fts5origintext + +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. +ifcapable !fts5 { + finish_test + return +} + +# Return a random integer between 0 and n-1. +# +proc random {n} { expr {abs(int(rand()*$n))} } + +# Select an element of the list passed as the only argument at random and +# return it. +# +proc select_one {list} { + set n [llength $list] + lindex $list [random $n] +} + +# Given a term that consists entirely of alphabet characters, return all +# permutations of the term using upper and lower case characters. e.g. +# +# "abc" -> {CBA cBA CbA cbA CBa cBa Cba cba} +# +proc casify {term {lRet {{}}}} { + if {$term==""} { return $lRet } + set t [string range $term 1 end] + set f1 [string toupper [string range $term 0 0]] + set f2 [string tolower [string range $term 0 0]] + set ret [list] + foreach x $lRet { + lappend ret "$x$f1" + lappend ret "$x$f2" + } + return [casify $t $ret] +} + +proc vocab {} { + list abc def ghi jkl mno pqr stu vwx yza +} + +# Return a random 3 letter term. +# +proc term {} { + if {[info exists ::expanded_vocab]==0} { + foreach v [vocab] { lappend ::expanded_vocab {*}[casify $v] } + } + + select_one $::expanded_vocab +} + +# Return a document - between 3 and 10 terms. +# +proc document {} { + set nTerm [expr [random 3] + 7] + set doc "" + for {set ii 0} {$ii < $nTerm} {incr ii} { + lappend doc [term] + } + set doc +} +db func document document + +#------------------------------------------------------------------------- + +expr srand(6) + +set NDOC 200 +set NLOOP 50 + +sqlite3_fts5_register_origintext db + +proc tokens {cmd} { + set ret [list] + for {set iTok 0} {$iTok < [$cmd xInstCount]} {incr iTok} { + set txt [$cmd xInstToken $iTok 0] + set txt [string map [list "\0" "."] $txt] + lappend ret $txt + } + set ret +} +sqlite3_fts5_create_function db tokens tokens + +proc rankfunc {cmd} { + $cmd xRowid +} +sqlite3_fts5_create_function db rankfunc rankfunc + +proc ctrl_tokens {term args} { + set ret [list] + set term [string tolower $term] + foreach doc $args { + foreach a $doc { + if {[string tolower $a]==$term} { + if {$a==$term} { + lappend ret $a + } else { + lappend ret [string tolower $a].$a + } + } + } + } + set ret +} +db func ctrl_tokens ctrl_tokens + +proc do_all_vocab_test {tn} { + foreach ::v [concat [vocab] nnn] { + set answer [execsql { + SELECT id, ctrl_tokens($::v, x) FROM ctrl WHERE x LIKE '%' || $::v || '%' + }] + do_execsql_test $tn.$::v.1 { + SELECT rowid, tokens(ft) FROM ft($::v) + } $answer + do_execsql_test $tn.$::v.2 { + SELECT rowid, tokens(ft) FROM ft($::v) ORDER BY rank + } $answer + } +} + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE ft USING fts5( + x, tokenize="origintext unicode61", content=, contentless_delete=1, + tokendata=1 + ); + + CREATE TABLE ctrl(id INTEGER PRIMARY KEY, x TEXT); + INSERT INTO ft(ft, rank) VALUES('pgsz', 64); + INSERT INTO ft(ft, rank) VALUES('rank', 'rankfunc()'); +} +do_test 1.1 { + for {set ii 0} {$ii < $NDOC} {incr ii} { + set doc [document] + execsql { + INSERT INTO ft(rowid, x) VALUES($ii, $doc); + INSERT INTO ctrl(id, x) VALUES($ii, $doc); + } + } +} {} + +#execsql_pp { SELECT * FROM ctrl } +#execsql_pp { SELECT * FROM ft } +#fts5_aux_test_functions db +#execsql_pp { SELECT rowid, tokens(ft), fts5_test_poslist(ft) FROM ft('ghi'); } + +do_all_vocab_test 1.2 + +for {set ii 0} {$ii < $NLOOP} {incr ii} { + set lRowid [execsql { SELECT id FROM ctrl WHERE random() % 2 }] + foreach r $lRowid { + execsql { DELETE FROM ft WHERE rowid = $r } + execsql { DELETE FROM ctrl WHERE rowid = $r } + + set doc [document] + execsql { INSERT INTO ft(rowid, x) VALUES($r, $doc) } + execsql { INSERT INTO ctrl(id, x) VALUES($r, $doc) } + } + do_all_vocab_test 1.3.$ii +} + +#------------------------------------------------------------------------- + +do_execsql_test 2.0 { + CREATE VIRTUAL TABLE ft2 USING fts5( + x, y, tokenize="origintext unicode61", content=, contentless_delete=1, + tokendata=1 + ); + + CREATE TABLE ctrl2(id INTEGER PRIMARY KEY, x TEXT, y TEXT); + INSERT INTO ft2(ft2, rank) VALUES('pgsz', 64); + INSERT INTO ft2(ft2, rank) VALUES('rank', 'rankfunc()'); +} +do_test 2.1 { + for {set ii 0} {$ii < $NDOC} {incr ii} { + set doc1 [document] + set doc2 [document] + execsql { + INSERT INTO ft2(rowid, x, y) VALUES($ii, $doc, $doc2); + INSERT INTO ctrl2(id, x, y) VALUES($ii, $doc, $doc2); + } + } +} {} + +proc do_all_vocab_test2 {tn} { + foreach ::v [vocab] { + set answer [execsql { + SELECT id, ctrl_tokens($::v, x, y) FROM ctrl2 + WHERE x LIKE '%' || $::v || '%' OR y LIKE '%' || $::v || '%'; + }] + do_execsql_test $tn.$::v.1 { + SELECT rowid, tokens(ft2) FROM ft2($::v) + } $answer + do_execsql_test $tn.$::v.2 { + SELECT rowid, tokens(ft2) FROM ft2($::v) ORDER BY rank + } $answer + } +} + +do_all_vocab_test2 2.2 + +for {set ii 0} {$ii < $NLOOP} {incr ii} { + set lRowid [execsql { SELECT id FROM ctrl2 WHERE random() % 2 }] + foreach r $lRowid { + execsql { DELETE FROM ft2 WHERE rowid = $r } + execsql { DELETE FROM ctrl2 WHERE rowid = $r } + + set doc1 [document] + set doc2 [document] + execsql { INSERT INTO ft2(rowid, x, y) VALUES($r, $doc, $doc1) } + execsql { INSERT INTO ctrl2(id, x, y) VALUES($r, $doc, $doc2) } + } + do_all_vocab_test 2.3.$ii +} + +#------------------------------------------------------------------------- + +unset -nocomplain ::expanded_vocab +proc vocab {} { + list abcde fghij klmno +} + +proc do_all_vocab_test3 {tn} { + foreach ::v [concat [vocab] nnn] { + set answer [execsql { + SELECT rowid, ctrl_tokens($::v, w) FROM ctrl3 WHERE w LIKE '%' || $::v || '%' + }] + do_execsql_test $tn.$::v.1 { + SELECT rowid, tokens(ft3) FROM ft3($::v) + } $answer + do_execsql_test $tn.$::v.2 { + SELECT rowid, tokens(ft3) FROM ft3($::v) ORDER BY rank + } $answer + } +} + +do_execsql_test 3.0 { + CREATE VIRTUAL TABLE ft3 USING fts5( + w, tokenize="origintext unicode61", content=, contentless_delete=1, + tokendata=1 + ); + INSERT INTO ft3(ft3, rank) VALUES('rank', 'rankfunc()'); + CREATE TABLE ctrl3(w); +} + +do_execsql_test 3.1 { + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<2 + ) + INSERT INTO ctrl3 SELECT document() FROM s; + INSERT INTO ft3(rowid, w) SELECT rowid, w FROM ctrl3; +} + +do_all_vocab_test3 3.2 + + +finish_test + diff --git a/ext/fts5/test/fts5origintext6.test b/ext/fts5/test/fts5origintext6.test new file mode 100644 index 0000000000..7b27e310b9 --- /dev/null +++ b/ext/fts5/test/fts5origintext6.test @@ -0,0 +1,209 @@ +# 2014 Jan 08 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# Tests focused on phrase queries. +# + +source [file join [file dirname [info script]] fts5_common.tcl] +set testprefix fts5origintext6 + +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. +ifcapable !fts5 { + finish_test + return +} + +proc insert_data {tbl} { + db eval " + INSERT INTO $tbl (rowid, x, y) VALUES + (1, 'ChH BDd HhG efc BjJ BGi GBG FdD','ciJ AFf ADf fBJ fhC GFI JEH fcA'), + (2, 'deg AIG Fie jII cCd Hbf igF fEE','GeA Ija gJg EDc HFi DDI dCf aDd'), + (3, 'IJC hga deC Jfa Aeg hfh CcH dfb','ajD hgC Jaf IfH CHe jIG AjD adF'), + (4, 'FiH GJH IDA AiG bBc CGG Eih bIH','hHg JaH aii IHE Ggd gcH gji CGc'), + (5, 'ceg CAd jFI GAB BGg EeC IdH acG','bBC eIG ifH eDE Adj bjb GCj ebA'), + (6, 'Eac Fbh aFF Eea jeG EIj HCc JJH','hbd giE Gfe eiI dEF abE cJf cAb'), + (7, 'dic hAc jEC AiG FEF jHc HiD HBI','aEd ebE Gfi AJG EBA faj GiG jjE'), + (8, 'Fca iEe EgE jjJ gce ijf EGc EBi','gaI dhH bFg CFc HeC CjI Jfg ccH'), + (9, 'cfd iaa HCf iHJ HjG ffh ABb ibi','CfG bia Dai eii Ejg Jeg fCg hDb'), + (10, 'Jjf hJC IID HJj bGB EbJ cgg eBj','jci jhi JAF jIg Bei Bcd cAC AJd'), + (11, 'egG Cdi bFf fEB hfH jDH jia Efd','FAd eCg fAi aiC baC eJG acF iGE'), + (12, 'Ada Gde CJI ADG gJA Cbb ccF iAB','eAE ajC FBB ccd Jgh fJg ieg hGE'), + (13, 'gBb fDG Jdd HdD fiJ Bed Cig iGg','heC FeI iaj gdg ebB giC HaD FIe'), + (14, 'FiI iDd Ffe igI bgB EJf FHG hDF','cjC AeI abf Fah cbJ ffH jEb aib'), + (15, 'jaF hBI jIH Gdh FEc Fij hgj jFh','dGA ADH feh AAI AfJ DbC gBi hGH'), + (16, 'gjH BGg iGj aFE CAH edI idf HEH','hIf DDg fjB hGi cHF BCH FjG Bgd'), + (17, 'iaI JGH hji gcj Dda eeG jDd CBi','cHg jeh caG gIc feF ihG hgJ Abj'), + (18, 'jHI iDB eFf AiH EFB CDb IAj GbC','Ghe dEI gdI jai gib dAG BIa djb'), + (19, 'abI fHG Ccf aAc FDa fiC agF bdB','afi hde IgE bGF cfg DHD diE aca'), + (20, 'IFh eDJ jfh cDg dde JGJ GAf fIJ','IBa EfH faE aeI FIF baJ FGj EIH'), + (21, 'Dee bFC bBA dEI CEj aJI ghA dCH','hBA ddA HJh dfj egI Dij dFE bGE'), + (22, 'JFE BCj FgA afc Jda FGD iHJ HDh','eAI jHe BHD Gah bbD Bgj gbh eGB'), + (23, 'edE CJE FjG aFI edA Cea FId iFe','ABG jcA ddj EEc Dcg hAI agA biA'), + (24, 'AgE cfc eef cGh aFB DcH efJ hcH','eGF HaB diG fgi bdc iGJ FGJ fFB'), + (25, 'aCa AgI GhC DDI hGJ Hgc Gcg bbG','iID Fga jHa jIj idj DFD bAC AFJ'), + (26, 'gjC JGh Fge faa eCA iGG gHE Gai','bDi hFE BbI DHD Adb Fgi hCa Hij'), + (27, 'Eji jEI jhF DFC afH cDh AGc dHA','IDe GcA ChF DIb Bif HfH agD DGh'), + (28, 'gDD AEE Dfg ICf Cbi JdE jgH eEi','eEb dBG FDE jgf cAI FaJ jaA cDd'), + (29, 'cbe Gec hgB Egi bca dHg bAJ jBf','EFB DgD GJc fDb EeE bBA GFC Hbe'), + (30, 'Adc eHB afI hDc Bhh baE hcJ BBd','JAH deg bcF Dab Bgj Gbb JHi FIB'), + (31, 'agF dIj AJJ Hfg cCG hED Igc fHC','JEf eia dHf Ggc Agj geD bEE Gei'), + (32, 'DAd cCe cbJ FjG gJe gba dJA GCf','eAf hFc bGE ABI hHA IcE abF CCE'), + (33, 'fFh jJe DhJ cDJ EBi AfD eFI IhG','fEG GCc Bjd EFF ggg CFe EHd ciB'), + (34, 'Ejb BjI eAF HaD eEJ FaG Eda AHC','Iah hgD EJG fdD cIE Daj IFf eJh'), + (35, 'aHG eCe FjA djJ dAJ jiJ IaE GGB','Acg iEF JfB FIC Eei ggj dic Iii'), + (36, 'Fdb EDF GaF JjB ehH IgC hgi DCG','cag DHI Fah hAJ bbh egG Hia hgJ'), + (37, 'HGg icC JEC AFJ Ddh dhi hfC Ich','fEg bED Bff hCJ EiA cIf bfG cGA'), + (38, 'aEJ jGI BCi FaA ebA BHj cIJ GcC','dCH ADd bGB cFE AgF geD cbG jIc'), + (39, 'JFB bBi heA BFA hgB Ahj EIE CgI','EIJ JFG FJE GeA Hdg HeH ACh GiA'), + (40, 'agB DDC CED igC Dfc DhI eiC fHi','dAB dcg iJF cej Fcc cAc AfB Fdd'), + (41, 'BdF DHj Ege hcG DEd eFa dCf gBb','FBG ChB cej iGd Hbh fCc Ibe Abh'), + (42, 'Bgc DjI cbC jGD bdb hHB IJA IJH','heg cii abb IGf eDe hJc dii fcE'), + (43, 'fhf ECa FiA aDh Jbf CiB Jhe ajD','GFE bIF aeD gDE BIE Jea DfC BEc'), + (44, 'GjE dBj DbJ ICF aDh EEH Ejb jFb','dJj aEc IBg bEG Faf fjA hjf FAF'), + (45, 'BfA efd IIJ AHG dDF eGg dIJ Gcb','Bfj jeb Ahc dAE ACH Dfb ieb dhC'), + (46, 'Ibj ege geC dJh CIi hbD EAG fGA','DEb BFe Bjg FId Fhg HeF JAc BbE'), + (47, 'dhB afC hgG bEJ aIe Cbe iEE JCD','bdg Ajc FGA jbh Jge iAj fIA jbE'), + (48, 'egH iDi bfH iiI hGC jFF Hfd AHB','bjE Beb iCc haB gIH Dea bga dfd'), + (49, 'jgf chc jGc Baj HBb jdE hgh heI','FFB aBd iEB EIG HGf Bbj EIi JbI'), + (50, 'jhe EGi ajA fbH geh EHe FdC bij','jDE bBC gbH HeE dcH iBH IFE AHi'), + (51, 'aCb JiD cgJ Bjj iAI Hbe IAF FhH','ijf bhE Jdf FED dCH bbG HcJ ebH'); + " +} + +foreach_detail_mode $testprefix { +foreach external {0 1 2} { + reset_db + + proc tokens {cmd} { + set ret [list] + for {set iTok 0} {$iTok < [$cmd xInstCount]} {incr iTok} { + set txt [$cmd xInstToken $iTok 0] + set txt [string map [list "\0" "."] $txt] + lappend ret $txt + } + set ret + } + sqlite3_fts5_create_function db tokens tokens + sqlite3_fts5_register_origintext db + + set E(0) internal + set E(1) external + set E(2) contentless + set e $E($external) + + db eval { CREATE TABLE ex(x, y) } + switch -- $external { + 0 { + do_execsql_test 1.$e.0 { + CREATE VIRTUAL TABLE ft USING fts5( + x, y, tokenize="origintext unicode61", tokendata=1, detail=%DETAIL% + ); + } + } + + 1 { + do_execsql_test 1.$e.0 { + CREATE VIRTUAL TABLE ft USING fts5( + x, y, tokenize="origintext unicode61", tokendata=1, detail=%DETAIL%, + content=ex + ); + } + } + + 2 { + do_execsql_test 1.$e.0 { + CREATE VIRTUAL TABLE ft USING fts5( + x, y, tokenize="origintext unicode61", tokendata=1, detail=%DETAIL%, + content= + ); + } + } + } + insert_data ex + insert_data ft + + proc prefixquery {prefix bInst bYOnly} { + set ret [list] + db eval { SELECT rowid, x, y FROM ex ORDER BY rowid } { + set row [list] + set bSeen 0 + + set T [concat $x $y] + if {$bYOnly} { set T $y } + + foreach w $T { + if {[string match -nocase $prefix $w]} { + set bSeen 1 + if {$bInst} { + set v [string tolower $w] + if {$w != $v} { append v ".$w" } + lappend row $v + } + } + } + + if {$bSeen} { + lappend ret $rowid + lappend ret $row + } + } + + set ret + } + + proc do_prefixquery_test {tn prefix} { + set bInst [expr {$::e!="contentless" || "%DETAIL%"=="full"}] + set expect [prefixquery $prefix $bInst 0] + set expect2 [prefixquery $prefix $bInst 1] + + uplevel [list do_execsql_test $tn.1 " + SELECT rowid, tokens(ft) FROM ft('$prefix') + " $expect] + uplevel [list do_execsql_test $tn.2 " + SELECT rowid, tokens(ft) FROM ft(fts5_insttoken('$prefix')) + " $expect] + db eval { INSERT INTO ft(ft, rank) VALUES('insttoken', 1) } + uplevel [list do_execsql_test $tn.3 " + SELECT rowid, tokens(ft) FROM ft('$prefix') + " $expect] + db eval { INSERT INTO ft(ft, rank) VALUES('insttoken', 0) } + + if {"%DETAIL%"!="none"} { + uplevel [list do_execsql_test $tn.4 " + SELECT rowid, tokens(ft) FROM ft('y: $prefix') + " $expect2] + uplevel [list do_execsql_test $tn.5 " + SELECT rowid, tokens(ft) FROM ft(fts5_insttoken('y: $prefix')) + " $expect2] + db eval { INSERT INTO ft(ft, rank) VALUES('insttoken', 1) } + uplevel [list do_execsql_test $tn.6 " + SELECT rowid, tokens(ft) FROM ft('y: $prefix') + " $expect2] + db eval { INSERT INTO ft(ft, rank) VALUES('insttoken', 0) } + } + } + + do_prefixquery_test 1.$e.1 a* + do_prefixquery_test 1.$e.2 b* + do_prefixquery_test 1.$e.3 c* + do_prefixquery_test 1.$e.4 d* + do_prefixquery_test 1.$e.5 e* + do_prefixquery_test 1.$e.6 f* + do_prefixquery_test 1.$e.7 g* + do_prefixquery_test 1.$e.8 h* + do_prefixquery_test 1.$e.9 i* + do_prefixquery_test 1.$e.10 j* +}} + + + +finish_test + diff --git a/ext/fts5/test/fts5phrase.test b/ext/fts5/test/fts5phrase.test index 6dac684e83..708cdfd83e 100644 --- a/ext/fts5/test/fts5phrase.test +++ b/ext/fts5/test/fts5phrase.test @@ -15,7 +15,7 @@ source [file join [file dirname [info script]] fts5_common.tcl] set testprefix fts5phrase -# If SQLITE_ENABLE_FTS5 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return @@ -93,15 +93,21 @@ foreach {tn cols tokens} { 10 {b} "i e" 11 {a} "i e" } { - set fts "{$cols}:[join $tokens +]" set where [list] foreach c $cols { lappend where "pmatch($c, '$tokens')" } set where [join $where " OR "] - set res [db eval "SELECT rowid FROM t3 WHERE $where"] - do_execsql_test "1.$tn.$fts->([llength $res] rows)" { - SELECT rowid FROM t3($fts) - } $res + foreach fts [list \ + "{$cols}:[join $tokens +]" \ + "{$cols}:NEAR([join $tokens +])" \ + "{$cols}:NEAR([join $tokens +],1)" \ + "{$cols}:NEAR([join $tokens +],111)" \ + ] { + set res [db eval "SELECT rowid FROM t3 WHERE $where"] + do_execsql_test "1.$tn.$fts->([llength $res] rows)" { + SELECT rowid FROM t3($fts) + } $res + } } do_execsql_test 2.0 { @@ -116,4 +122,3 @@ do_execsql_test 2.0 { } finish_test - diff --git a/ext/fts5/test/fts5plan.test b/ext/fts5/test/fts5plan.test index d7f5fd65a0..57d5254a35 100644 --- a/ext/fts5/test/fts5plan.test +++ b/ext/fts5/test/fts5plan.test @@ -15,7 +15,7 @@ source [file join [file dirname [info script]] fts5_common.tcl] set testprefix fts5plan -# If SQLITE_ENABLE_FTS5 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return @@ -29,39 +29,37 @@ do_execsql_test 1.0 { do_eqp_test 1.1 { SELECT * FROM t1, f1 WHERE f1 MATCH t1.x } { - 0 0 0 {SCAN TABLE t1} - 0 1 1 {SCAN TABLE f1 VIRTUAL TABLE INDEX 1:} + QUERY PLAN + |--SCAN t1 + `--SCAN f1 VIRTUAL TABLE INDEX 0:M1 } do_eqp_test 1.2 { SELECT * FROM t1, f1 WHERE f1 > t1.x } { - 0 0 1 {SCAN TABLE f1 VIRTUAL TABLE INDEX 0:} - 0 1 0 {SCAN TABLE t1} + QUERY PLAN + |--SCAN f1 VIRTUAL TABLE INDEX 0: + `--SCAN t1 } do_eqp_test 1.3 { SELECT * FROM f1 WHERE f1 MATCH ? ORDER BY ff } { - 0 0 0 {SCAN TABLE f1 VIRTUAL TABLE INDEX 1:} - 0 0 0 {USE TEMP B-TREE FOR ORDER BY} + QUERY PLAN + |--SCAN f1 VIRTUAL TABLE INDEX 0:M1 + `--USE TEMP B-TREE FOR ORDER BY } do_eqp_test 1.4 { SELECT * FROM f1 ORDER BY rank } { - 0 0 0 {SCAN TABLE f1 VIRTUAL TABLE INDEX 0:} - 0 0 0 {USE TEMP B-TREE FOR ORDER BY} + QUERY PLAN + |--SCAN f1 VIRTUAL TABLE INDEX 0: + `--USE TEMP B-TREE FOR ORDER BY } do_eqp_test 1.5 { SELECT * FROM f1 WHERE rank MATCH ? -} { - 0 0 0 {SCAN TABLE f1 VIRTUAL TABLE INDEX 2:} -} - - - +} {SCAN f1 VIRTUAL TABLE INDEX 0:r} finish_test - diff --git a/ext/fts5/test/fts5porter.test b/ext/fts5/test/fts5porter.test index 2535eb75b1..de1c3e15a3 100644 --- a/ext/fts5/test/fts5porter.test +++ b/ext/fts5/test/fts5porter.test @@ -17,7 +17,7 @@ source [file join [file dirname [info script]] fts5_common.tcl] set testprefix fts5porter -# If SQLITE_ENABLE_FTS5 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return @@ -11803,4 +11803,3 @@ foreach {in out} $test_vocab { finish_test - diff --git a/ext/fts5/test/fts5porter2.test b/ext/fts5/test/fts5porter2.test index 5e0aeb029f..556060baa3 100644 --- a/ext/fts5/test/fts5porter2.test +++ b/ext/fts5/test/fts5porter2.test @@ -18,7 +18,7 @@ source [file join [file dirname [info script]] fts5_common.tcl] set testprefix fts5porter2 -# If SQLITE_ENABLE_FTS5 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return @@ -67,4 +67,3 @@ foreach {in out} $test_vocab { finish_test - diff --git a/ext/fts5/test/fts5prefix.test b/ext/fts5/test/fts5prefix.test index 8e0d5a2954..7a29628ea1 100644 --- a/ext/fts5/test/fts5prefix.test +++ b/ext/fts5/test/fts5prefix.test @@ -9,13 +9,13 @@ # #*********************************************************************** # -# This file containst tests focused on prefix indexes. +# This file contains tests focused on prefix indexes. # source [file join [file dirname [info script]] fts5_common.tcl] set testprefix fts5prefix -# If SQLITE_ENABLE_FTS5 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return @@ -341,5 +341,3 @@ foreach {tn create} { } finish_test - - diff --git a/ext/fts5/test/fts5prefix2.test b/ext/fts5/test/fts5prefix2.test new file mode 100644 index 0000000000..0860b3cddd --- /dev/null +++ b/ext/fts5/test/fts5prefix2.test @@ -0,0 +1,87 @@ +# 2020 Dec 3 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# This file contains tests focused on prefix indexes. +# + +source [file join [file dirname [info script]] fts5_common.tcl] +set testprefix fts5prefix2 + +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. +ifcapable !fts5 { + finish_test + return +} + +foreach p {3 2 1} { + reset_db + do_execsql_test 1.$p.0 " + CREATE VIRTUAL TABLE t1 USING fts5(xyz, prefix=$p); + " + do_execsql_test 1.$p.1 { + INSERT INTO t1 VALUES + ('May you do good and not evil.'), + ('May you find forgiveness for yourself and forgive others.'), + ('May you share freely, never taking more than you give f.'); + } + + do_execsql_test 1.$p.2 { + SELECT highlight(t1, 0, '[', ']') FROM t1('f*'); + } { + {May you [find] [forgiveness] [for] yourself and [forgive] others.} + {May you share [freely], never taking more than you give [f].} + } +} + +do_execsql_test 2.0 { + CREATE VIRTUAL TABLE t2 USING fts5(one, prefix=3); + INSERT INTO t2 VALUES('top'); + INSERT INTO t2 VALUES('to'); + INSERT INTO t2 VALUES('tommy'); +} + +do_execsql_test 2.1 { + SELECT * FROM t2('to*'); +} {top to tommy} + +#------------------------------------------------------------------------- + +foreach {tn newrowid} { + 1 122 + 2 123 + 3 124 +} { + reset_db + do_execsql_test 3.$tn.0 { + CREATE VIRTUAL TABLE t12 USING fts5(x); + INSERT INTO t12(rowid, x) VALUES(123, 'wwww'); + } + do_execsql_test 3.$tn.1 { + BEGIN; + DELETE FROM t12 WHERE rowid=123; + SELECT * FROM t12('wwww*'); + INSERT INTO t12(rowid, x) VALUES($newrowid, 'wwww'); + SELECT * FROM t12('wwww*'); + END; + } {wwww} + do_execsql_test 3.$tn.2 { + INSERT INTO t12(t12) VALUES('integrity-check'); + } + do_execsql_test 3.$tn.3 { + SELECT rowid FROM t12('wwww*'); + } $newrowid +} + +finish_test + + + +finish_test diff --git a/ext/fts5/test/fts5query.test b/ext/fts5/test/fts5query.test index 570abf46ba..4e8bab8cf7 100644 --- a/ext/fts5/test/fts5query.test +++ b/ext/fts5/test/fts5query.test @@ -15,7 +15,7 @@ source [file join [file dirname [info script]] fts5_common.tcl] set testprefix fts5query -# If SQLITE_ENABLE_FTS5 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return @@ -64,7 +64,7 @@ for {set tn 1 ; set pgsz 64} {$tn<32} {incr tn; incr pgsz 16} { execsql COMMIT } {} - do_execsql_test 1.$tn.2 { + do_execsql_test 2.$tn.2 { INSERT INTO t1(t1) VALUES('integrity-check'); } @@ -77,7 +77,15 @@ for {set tn 1 ; set pgsz 64} {$tn<32} {incr tn; incr pgsz 16} { } } - -finish_test +reset_db +do_execsql_test 3.0 { + CREATE VIRTUAL TABLE x1 USING fts5(a); + INSERT INTO x1(rowid, a) VALUES(-1000000000000, 'toyota'); + INSERT INTO x1(rowid, a) VALUES(1, 'tarago'); +} +do_execsql_test 3.1 { + SELECT rowid FROM x1('t*'); +} {-1000000000000 1} +finish_test diff --git a/ext/fts5/test/fts5rank.test b/ext/fts5/test/fts5rank.test index 4961b42605..7a700cb97f 100644 --- a/ext/fts5/test/fts5rank.test +++ b/ext/fts5/test/fts5rank.test @@ -15,7 +15,7 @@ source [file join [file dirname [info script]] fts5_common.tcl] set testprefix fts5rank -# If SQLITE_ENABLE_FTS5 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return @@ -90,11 +90,118 @@ do_test 2.7 { execsql { SELECT rowid FROM tt('a') ORDER BY rank; } db } {1 3 2} +db2 close +#-------------------------------------------------------------------------- +# At one point there was a problem with queries such as: +# +# ... MATCH 'x OR y' ORDER BY rank; +# +# if there were zero occurrences of token 'y' in the dataset. The +# following tests verify that that problem has been addressed. +# +foreach_detail_mode $::testprefix { + do_execsql_test 3.1.0 { + CREATE VIRTUAL TABLE y1 USING fts5(z, detail=%DETAIL%); + INSERT INTO y1 VALUES('test xyz'); + INSERT INTO y1 VALUES('test test xyz test'); + INSERT INTO y1 VALUES('test test xyz'); + } + + do_execsql_test 3.1.1 { + SELECT rowid FROM y1('test OR tset'); + } {1 2 3} + + do_execsql_test 3.1.2 { + SELECT rowid FROM y1('test OR tset') ORDER BY bm25(y1) + } {2 3 1} + + do_execsql_test 3.1.3 { + SELECT rowid FROM y1('test OR tset') ORDER BY +rank + } {2 3 1} + + do_execsql_test 3.1.4 { + SELECT rowid FROM y1('test OR tset') ORDER BY rank + } {2 3 1} + + do_execsql_test 3.1.5 { + SELECT rowid FROM y1('test OR xyz') ORDER BY rank + } {3 2 1} + + + do_execsql_test 3.2.1 { + CREATE VIRTUAL TABLE z1 USING fts5(a, detail=%DETAIL%); + INSERT INTO z1 VALUES('wrinkle in time'); + SELECT * FROM z1 WHERE z1 MATCH 'wrinkle in time OR a wrinkle in time'; + } {{wrinkle in time}} +} +do_execsql_test 4.1 { + DROP TABLE IF EXISTS VTest; + CREATE virtual TABLE VTest USING FTS5( + Title, AUthor, tokenize ='porter unicode61 remove_diacritics 1', + columnsize='1', detail=full + ); + INSERT INTO VTest (Title, Author) VALUES ('wrinkle in time', 'Bill Smith'); + SELECT * FROM VTest WHERE + VTest MATCH 'wrinkle in time OR a wrinkle in time' ORDER BY rank; +} {{wrinkle in time} {Bill Smith}} +#------------------------------------------------------------------------- +reset_db +do_execsql_test 5.0 { + CREATE VIRTUAL TABLE ttt USING fts5(a); + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<100 + ) + INSERT INTO ttt SELECT 'word ' || i FROM s; +} +do_execsql_test 5.1 { + SELECT rowid FROM ttt('word') WHERE rowid BETWEEN 30 AND 40 ORDER BY rank; +} {30 31 32 33 34 35 36 37 38 39 40} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 6.0 { + CREATE VIRTUAL TABLE "My.Table" USING fts5(Text); + + INSERT INTO "My.Table" VALUES ('hello this is a test'); + INSERT INTO "My.Table" VALUES ('of trying to order by'); + INSERT INTO "My.Table" VALUES ('rank on an fts5 table'); + INSERT INTO "My.Table" VALUES ('that have periods in'); + INSERT INTO "My.Table" VALUES ('the table names.'); + INSERT INTO "My.Table" VALUES ('table table table'); +} +do_execsql_test 6.1 { + SELECT * FROM "My.Table" WHERE Text MATCH 'table' ORDER BY rank; +} { + {table table table} {the table names.} {rank on an fts5 table} +} -finish_test +#------------------------------------------------------------------------- +# forum post: https://sqlite.org/forum/forumpost/a2dd636330 +# +reset_db +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE t USING fts5 (a, b); + INSERT INTO t (a, b) VALUES ('data1', 'sentence1'), ('data2', 'sentence2'); + INSERT INTO t(t, rank) VALUES ('rank', 'bm25(10.0,1.0)'); +} + +sqlite3 db2 test.db +do_execsql_test -db db2 1.1 { + SELECT *, rank<0.0 FROM t('data*') ORDER BY RANK; +} {data1 sentence1 1 data2 sentence2 1} + +do_execsql_test 1.2 { + INSERT INTO t(t, rank) VALUES ('rank', 'bm25(10.0,1.0)'); +} +do_execsql_test -db db2 1.3 { + SELECT *, rank<0.0 FROM t('data*') ORDER BY RANK; +} {data1 sentence1 1 data2 sentence2 1} +db2 close + +finish_test diff --git a/ext/fts5/test/fts5rebuild.test b/ext/fts5/test/fts5rebuild.test index 1044421d5e..065d16b910 100644 --- a/ext/fts5/test/fts5rebuild.test +++ b/ext/fts5/test/fts5rebuild.test @@ -14,7 +14,7 @@ source [file join [file dirname [info script]] fts5_common.tcl] set testprefix fts5rebuild -# If SQLITE_ENABLE_FTS5 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return @@ -39,13 +39,14 @@ do_execsql_test 1.4 { INSERT INTO f1(f1) VALUES('integrity-check'); } {} +sqlite3_db_config db DEFENSIVE 0 do_execsql_test 1.5 { DELETE FROM f1_data; } {} do_catchsql_test 1.6 { INSERT INTO f1(f1) VALUES('integrity-check'); -} {1 {database disk image is malformed}} +} {/.*fts5: corrupt.*/} do_execsql_test 1.7 { INSERT INTO f1(f1) VALUES('rebuild'); @@ -64,4 +65,3 @@ do_catchsql_test 2.2 { INSERT INTO nc(nc) VALUES('rebuild'); } {1 {'rebuild' may not be used with a contentless fts5 table}} finish_test - diff --git a/ext/fts5/test/fts5restart.test b/ext/fts5/test/fts5restart.test index 0dd7d69454..da58fe3aed 100644 --- a/ext/fts5/test/fts5restart.test +++ b/ext/fts5/test/fts5restart.test @@ -15,7 +15,7 @@ source [file join [file dirname [info script]] fts5_common.tcl] set testprefix fts5restart -# If SQLITE_ENABLE_FTS5 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return @@ -29,6 +29,7 @@ do_execsql_test 1.0 { # Run the 'optimize' command. Check that it does not disturb ongoing # full-text queries. # +unset -nocomplain lRowid do_test 1.1 { for {set i 1} {$i < 1000} {incr i} { execsql { INSERT INTO f1 VALUES('a b c d e') } @@ -149,4 +150,3 @@ do_test 4.3 { finish_test - diff --git a/ext/fts5/test/fts5rowid.test b/ext/fts5/test/fts5rowid.test index 19590cdf0d..e4e4f6b844 100644 --- a/ext/fts5/test/fts5rowid.test +++ b/ext/fts5/test/fts5rowid.test @@ -15,7 +15,7 @@ source [file join [file dirname [info script]] fts5_common.tcl] set testprefix fts5rowid -# If SQLITE_ENABLE_FTS5 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return @@ -70,6 +70,7 @@ set res [db one {SELECT count(*) FROM x1_data}] do_execsql_test 2.3 { SELECT count(fts5_decode(rowid, block)) FROM x1_data; } $res +sqlite3_db_config db DEFENSIVE 0 do_execsql_test 2.4 { UPDATE x1_data SET block = X''; SELECT count(fts5_decode(rowid, block)) FROM x1_data; @@ -216,4 +217,3 @@ do_execsql_test 6.2 { finish_test - diff --git a/ext/fts5/test/fts5savepoint.test b/ext/fts5/test/fts5savepoint.test new file mode 100644 index 0000000000..bf66052282 --- /dev/null +++ b/ext/fts5/test/fts5savepoint.test @@ -0,0 +1,85 @@ +# 2019 Dec 26 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +source [file join [file dirname [info script]] fts5_common.tcl] +set testprefix fts5savepoint + +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. +ifcapable !fts5 { + finish_test + return +} + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE ft USING fts5(c); + BEGIN; + SAVEPOINT one; + INSERT INTO ft VALUES('a'); + SAVEPOINT two; + INSERT INTO ft VALUES('b'); + RELEASE two; + SAVEPOINT four; + INSERT INTO ft VALUES('c'); + RELEASE four; + SAVEPOINT three; + INSERT INTO ft VALUES('d'); + ROLLBACK TO three; + COMMIT; + SELECT * FROM ft +} {a b c} + +reset_db +do_catchsql_test 2.0 { + CREATE VIRTUAL TABLE ft1 USING fts5(c); + CREATE VIRTUAL TABLE ft2 USING fts5(c); + DROP TABLE ft2_idx; + BEGIN; + INSERT INTO ft2 VALUES('a'); + INSERT INTO ft1 VALUES('a'); + SAVEPOINT two; + INSERT INTO ft1 VALUES('b'); + COMMIT; +} {1 {database disk image is malformed}} + +reset_db +ifcapable fts3 { + do_execsql_test 3.0 { + CREATE VIRTUAL TABLE vt0 USING fts5(c0); + CREATE VIRTUAL TABLE vt1 USING fts4(c0); + INSERT INTO vt1(c0) VALUES(0); + } + + do_execsql_test 3.1 { + BEGIN; + UPDATE vt1 SET c0 = 0; + INSERT INTO vt1(c0) VALUES (0), (0); + UPDATE vt0 SET c0 = 0; + INSERT INTO vt1(c0) VALUES (0); + UPDATE vt1 SET c0 = 0; + INSERT INTO vt1(vt1) VALUES('automerge=1'); + UPDATE vt1 SET c0 = 0; + } + + do_catchsql_test 3.2 { + DROP TABLE vt1; + } {0 {}} + + do_execsql_test 3.3 { + SAVEPOINT x; + INSERT INTO vt0 VALUES('x'); + COMMIT; + INSERT INTO vt0(vt0) VALUES('integrity-check'); + } +} + +finish_test + diff --git a/ext/fts5/test/fts5secure.test b/ext/fts5/test/fts5secure.test new file mode 100644 index 0000000000..7314946162 --- /dev/null +++ b/ext/fts5/test/fts5secure.test @@ -0,0 +1,348 @@ +# 2023 Feb 17 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# + +source [file join [file dirname [info script]] fts5_common.tcl] +ifcapable !fts5 { finish_test ; return } +set ::testprefix fts5secure + +proc dump {tname} { + execsql_pp "SELECT * FROM ${tname}_idx" + execsql_pp "SELECT id, quote(block), fts5_decode(id,block) FROM ${tname}_data" +} + + +do_execsql_test 0.0 { + CREATE VIRTUAL TABLE t1 USING fts5(ab); + CREATE VIRTUAL TABLE v1 USING fts5vocab('t1', 'instance'); + INSERT INTO t1(rowid, ab) VALUES + (0,'abc'), (1,'abc'), (2,'abc'), (3,'abc'), (4,'def'); +} + +do_execsql_test 0.1 { + INSERT INTO t1(t1, rank) VALUES('secure-delete', 1); +} + +do_execsql_test 0.2 { + DELETE FROM t1 WHERE rowid=2; +} + +do_execsql_test 0.3 { + SELECT count(*) FROM t1_data +} 3 + +do_execsql_test 0.4 { + INSERT INTO t1(t1) VALUES('integrity-check'); +} + +do_execsql_test 0.5 { + DELETE FROM t1 WHERE rowid=3; +} + +do_execsql_test 0.6 { + INSERT INTO t1(t1) VALUES('integrity-check'); +} + +do_execsql_test 0.7 { + DELETE FROM t1 WHERE rowid=0; +} + +do_execsql_test 0.8 { + INSERT INTO t1(t1) VALUES('integrity-check'); +} + +#---------------------------------- + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE t2 USING fts5(ab); + INSERT INTO t2(rowid, ab) VALUES (5, 'key'), (6, 'value'); + INSERT INTO t2(t2, rank) VALUES('secure-delete', 1); +} + +#execsql_pp { SELECT id, quote(block) FROM t1_data } +#execsql_pp { SELECT segid, quote(term), pgno FROM t1_idx } + +do_execsql_test 1.1 { + DELETE FROM t2 WHERE rowid = 5; +} + +do_execsql_test 1.2 { + INSERT INTO t2(t2) VALUES('integrity-check'); +} + +do_execsql_test 1.3 { + DELETE FROM t2 WHERE rowid = 6; +} + +do_execsql_test 1.4 { + INSERT INTO t2(t2) VALUES('integrity-check'); +} + +do_execsql_test 1.5 { + SELECT * FROM t2('value'); + SELECT * FROM t2('v*'); +} + +do_execsql_test 1.6 { + SELECT * FROM t2('value') ORDER BY rowid DESC; + SELECT * FROM t2('v*') ORDER BY rowid DESC; +} +execsql_pp { + SELECT id, quote(block) FROM t2_data; +} + +#---------------------------------- + +do_execsql_test 2.0 { + CREATE VIRTUAL TABLE ft USING fts5(ab); + CREATE VIRTUAL TABLE vocab USING fts5vocab('ft', 'instance'); + INSERT INTO ft(rowid, ab) VALUES + (1, 'one'), + (2, 'two'), + (3, 'three'), + (4, 'four'), + (5, 'one one'), + (6, 'one two'), + (7, 'one three'), + (8, 'one four'), + (9, 'two one'), + (10, 'two two'), + (11, 'two three'), + (12, 'two four'), + (13, 'three one'), + (14, 'three two'), + (15, 'three three'), + (16, 'three four'); +} + +do_execsql_test 2.1 { + SELECT count(*) FROM ft_data; +} {3} + +do_execsql_test 2.2 { + INSERT INTO ft(ft, rank) VALUES('secure-delete', 1); +} + +do_execsql_test 2.3 { + DELETE FROM ft WHERE rowid=9; +} + +do_execsql_test 2.4 { + INSERT INTO ft(ft) VALUES('integrity-check'); +} + +do_execsql_test 2.5 { + DELETE FROM ft WHERE ab LIKE '%two%' +} + +do_execsql_test 2.6 { + INSERT INTO ft(ft) VALUES('integrity-check'); +} + +do_execsql_test 2.7 { + SELECT count(*) FROM ft_data; +} {3} + +#---------------------------------- +reset_db + +set ::vocab { + one two three four five six seven eight nine ten + eleven twelve thirteen fourteen fifteen sixteen + seventeen eighteen nineteen twenty +} +proc rnddoc {} { + set nVocab [llength $::vocab] + set ret [list] + for {set ii 0} {$ii < 8} {incr ii} { + lappend ret [lindex $::vocab [expr int(abs(rand()) * $nVocab)]] + } + set ret +} + +proc contains {list val} { + expr {[lsearch $list $val]>=0} +} + +foreach {tn pgsz} { + 2 64 + 1 1000 +} { + reset_db + db function rnddoc rnddoc + db function contains contains + + expr srand(1) + + do_execsql_test 3.$tn.0 { + CREATE VIRTUAL TABLE t1 USING fts5(x); + INSERT INTO t1(t1, rank) VALUES('pgsz', $pgsz); + WITH s(i) AS ( + VALUES(1) UNION SELECT i+1 FROM s WHERE i<20 + ) + INSERT INTO t1 SELECT rnddoc() FROM s; + } + + do_execsql_test 3.$tn.1 { + INSERT INTO t1(t1, rank) VALUES('secure-delete', 1); + } + + foreach {rowid} { + 6 16 3 4 9 14 13 7 20 15 19 10 11 2 5 18 17 1 12 8 + } { + + do_execsql_test 3.$tn.2.$rowid { + DELETE FROM t1 WHERE rowid=$rowid; + } + do_execsql_test 3.$tn.2.$rowid.ic { + INSERT INTO t1(t1) VALUES('integrity-check'); + } + + foreach v $::vocab { + do_execsql_test 3.$tn.2.$rowid.q.$v { + SELECT rowid FROM t1($v) + } [db eval {SELECT rowid FROM t1 WHERE contains(x, $v)}] + + do_execsql_test 3.$tn.2.$rowid.q.$v.DESC { + SELECT rowid FROM t1($v) ORDER BY 1 DESC + } [db eval {SELECT rowid FROM t1 WHERE contains(x, $v) ORDER BY 1 DESC}] + } + } +} + +do_execsql_test 3.3 { + INSERT INTO t1(x) VALUES('optimize'); + INSERT INTO t1(t1) VALUES('optimize'); + SELECT count(*) FROM t1_data; +} {3} + +#---------------------------------- +reset_db +do_execsql_test 4.0 { + CREATE VIRTUAL TABLE t1 USING fts5(x); + INSERT INTO t1(t1, rank) VALUES('pgsz', 32); + INSERT INTO t1(t1, rank) VALUES('secure-delete', 1); +} + +set L1 [string repeat abcdefghij 10] +set L2 [string repeat 1234567890 10] + +do_execsql_test 4.1 { + INSERT INTO t1 VALUES('aa' || $L1 || ' ' || $L2); +} +do_execsql_test 4.2 { + DELETE FROM t1 WHERE rowid=1 +} +do_execsql_test 4.3 { + INSERT INTO t1(t1) VALUES('integrity-check'); +} + +#---------------------------------- +reset_db +do_execsql_test 5.0 { + CREATE VIRTUAL TABLE t1 USING fts5(x); + INSERT INTO t1(t1, rank) VALUES('pgsz', 32); + INSERT INTO t1(t1, rank) VALUES('secure-delete', 1); +} + +set doc "aa [string repeat {abc } 60]" + +do_execsql_test 5.1 { + BEGIN; + INSERT INTO t1 VALUES($doc); + INSERT INTO t1 VALUES('aa abc'); + COMMIT; +} + +do_execsql_test 5.2 { + DELETE FROM t1 WHERE rowid = 1; +} + +do_execsql_test 5.3 { + INSERT INTO t1(t1) VALUES('integrity-check'); +} + +do_execsql_test 5.4 { SELECT rowid FROM t1('abc'); } 2 +do_execsql_test 5.5 { SELECT rowid FROM t1('aa'); } 2 + +#------------------------------------------------------------------------- +# Tests for the bug fixed by https://sqlite.org/src/info/4b60a1c3 +# +reset_db +do_execsql_test 6.0 { + CREATE VIRTUAL TABLE fts USING fts5(content); + INSERT INTO fts(fts, rank) VALUES ('secure-delete', 1); + INSERT INTO fts(rowid, content) VALUES + (3407, 'profile profile profile profile profile profile profile profile pull pulling pulling really'); + DELETE FROM fts WHERE rowid IS 3407; + INSERT INTO fts(fts) VALUES ('integrity-check'); +} + +foreach {tn detail} { + 1 full + 2 column + 3 none +} { + do_execsql_test 6.1.$detail " + DROP TABLE IF EXISTS t1; + CREATE VIRTUAL TABLE t1 USING fts5(x, detail=$detail); + " + + do_execsql_test 6.2.$detail { + INSERT INTO t1(t1, rank) VALUES('secure-delete', 1); + } + + for {set ii 1} {$ii < 100} {incr ii} { + do_execsql_test 6.3.$detail.$ii.1 { + BEGIN; + INSERT INTO t1(rowid, x) VALUES(10, 'word1'); + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i0} { + set idx [expr int(abs(rand()) * [llength $in])] + lappend out [lindex $in $idx] + set in [lreplace $in $idx $idx] + } + set out + } + + #dump fff + + set iTest 1 + foreach ii [lshuffle [db eval {SELECT rowid FROM fff}]] { + #if {$iTest==1} { dump fff } + #if {$iTest==1} { breakpoint } + do_execsql_test 3.$tn.1.$iTest.$ii { + DELETE FROM fff WHERE rowid=$ii; + } + #if {$iTest==1} { dump fff } + if {($iTest % 20)==0} { + do_execsql_test 3.$tn.1.$iTest.$ii.ic { + INSERT INTO fff(fff) VALUES('integrity-check'); + } + } + #if {$iTest==1} { break } + incr iTest + } +} + +#execsql_pp { SELECT rowid FROM fff('post') ORDER BY rowid ASC } +#breakpoint +#execsql_pp { +# SELECT rowid FROM fff('post') ORDER BY rowid DESC +#} +# +#dump fff + + +finish_test + diff --git a/ext/fts5/test/fts5secure4.test b/ext/fts5/test/fts5secure4.test new file mode 100644 index 0000000000..7588a34683 --- /dev/null +++ b/ext/fts5/test/fts5secure4.test @@ -0,0 +1,170 @@ +# 2023 April 14 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# + +source [file join [file dirname [info script]] fts5_common.tcl] +return_if_no_fts5 +set ::testprefix fts5secure4 + +#------------------------------------------------------------------------- +# Test using the 'delete' command to attempt to delete a token that +# is not present in the index in secure-delete mode. +# +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE t1 USING fts5(a, b, content=x1); + + CREATE TABLE x1(rowid INTEGER PRIMARY KEY, a, b); + INSERT INTO x1 VALUES + (1, 'hello world', 'today xyz'), + (2, 'not the day', 'crunch crumble and chomp'), + (3, 'one', 'two'); + INSERT INTO t1(t1) VALUES('rebuild'); +} + +do_execsql_test 1.1 { + INSERT INTO t1(t1, rank) VALUES('secure-delete', 1); +} + +do_execsql_test 1.2 { + INSERT INTO t1(t1, rowid, a, b) VALUES('delete', 4, 'nosuchtoken', ''); +} + +do_execsql_test 1.3 { + INSERT INTO t1(t1) VALUES('integrity-check'); +} + +do_execsql_test 1.4 { + INSERT INTO t1(t1, rowid, a, b) VALUES('delete', 1, 'crunch', ''); +} + +do_execsql_test 1.5 { + INSERT INTO t1(t1, rowid, a, b) VALUES('delete', 3, 'crunch', ''); +} + +do_execsql_test 1.6 { + INSERT INTO t1(t1) VALUES('integrity-check'); +} + +do_execsql_test 1.7 { +CREATE VIRTUAL TABLE y1 USING fts5(xx, prefix='1,2'); +INSERT INTO y1(y1, rank) VALUES('pgsz', 64); +INSERT INTO y1(y1, rank) VALUES('secure-delete', 1); +} +do_execsql_test 1.8 { + BEGIN; + INSERT INTO y1(rowid, xx) VALUES(1, 'abc def'); + INSERT INTO y1(rowid, xx) VALUES(2, 'reallyreallylongtoken'); + COMMIT; +} +do_execsql_test 1.9 { + DELETE FROM y1 WHERE rowid=1; + INSERT INTO y1(y1) VALUES('integrity-check'); +} + +do_execsql_test 1.10 { + CREATE VIRTUAL TABLE w1 USING fts5(ww, content=""); + INSERT INTO w1(rowid, ww) VALUES(123, ''); +} +do_catchsql_test 1.11 { + INSERT INTO w1(w1, rowid, ww) VALUES('delete', 123, 'xyz'); +} {1 {database disk image is malformed}} +do_catchsql_test 1.12 { + DROP TABLE w1; + CREATE VIRTUAL TABLE w1 USING fts5(ww, content=""); + INSERT INTO w1(rowid, ww) VALUES(123, ''); + DELETE FROM w1_data WHERE id>10; + INSERT INTO w1(w1, rowid, ww) VALUES('delete', 123, 'xyz'); +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +# Test using secure-delete with detail=none or detail=col. +# +foreach {tn d} {1 full 2 none 3 column} { + reset_db + do_execsql_test 2.$tn.1 " + CREATE VIRTUAL TABLE x1 USING fts5(xx, yy, zz, detail=$d, prefix='10,20'); + INSERT INTO x1(x1, rank) VALUES('pgsz', 64); + INSERT INTO x1(x1, rank) VALUES('secure-delete', 1); + " + + do_execsql_test 2.$tn.2 { + BEGIN; + INSERT INTO x1(xx, yy, zz) VALUES('a b c', 'd e f', 'a b c'); + INSERT INTO x1(xx, yy, zz) VALUES('a b c', 'd e f', 'a b c'); + INSERT INTO x1(xx, yy, zz) VALUES('a b c', 'd e f', 'a b c'); + INSERT INTO x1(xx, yy, zz) VALUES('a b c', 'd e f', 'a b c'); + INSERT INTO x1(xx, yy, zz) VALUES('a b c', 'd e f', 'a b c'); + COMMIT; + INSERT INTO x1(x1) VALUES('integrity-check'); + } + + do_execsql_test 2.$tn.3 { + DELETE FROM x1 WHERE rowid IN (2, 4, 6); + INSERT INTO x1(x1) VALUES('integrity-check'); + } + + do_execsql_test 2.$tn.4 { + DELETE FROM x1 WHERE rowid IN (1, 3, 5); + INSERT INTO x1(x1) VALUES('integrity-check'); + } + + do_execsql_test 2.$tn.5 { + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<100 + ) + INSERT INTO x1 + SELECT 'seems to be', 'used brew to', 'everything is working' FROM s + UNION ALL + SELECT 'used brew to', 'everything is working', 'seems to be' FROM s + UNION ALL + SELECT 'everything is working', 'seems to be', 'used brew to' FROM s + UNION ALL + SELECT 'abc', 'zzz', 'a b c d' + UNION ALL + SELECT 'z', 'z', 'z' FROM s + } + + do_test 2.$tn.6 { + for {set i 300} {$i > 200} {incr i -1} { + execsql { + DELETE FROM x1 WHERE rowid=$i; + INSERT INTO x1(x1) VALUES('integrity-check'); + } + } + } {} + + do_test 2.$tn.7 { + for {set i 1} {$i < 100} {incr i} { + execsql { + DELETE FROM x1 WHERE rowid=$i; + INSERT INTO x1(x1) VALUES('integrity-check'); + } + } + } {} + + do_test 2.$tn.8 { + foreach i [db eval {SELECT rowid FROM x1}] { + execsql { + DELETE FROM x1 WHERE rowid=$i; + INSERT INTO x1(x1) VALUES('integrity-check'); + } + } + } {} + + do_execsql_test 2.$tn.9 { + SELECT * FROM x1 + } {} +} + + + +finish_test + diff --git a/ext/fts5/test/fts5secure5.test b/ext/fts5/test/fts5secure5.test new file mode 100644 index 0000000000..ca8570211c --- /dev/null +++ b/ext/fts5/test/fts5secure5.test @@ -0,0 +1,129 @@ +# 2023 April 14 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# + +source [file join [file dirname [info script]] fts5_common.tcl] +return_if_no_fts5 +set ::testprefix fts5secure5 +return_if_no_fts5 + +proc dump {} { + execsql_pp { + SELECT id, quote(block), fts5_decode_none(id, block) FROM ft1_data + } +} + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE ft1 USING fts5(a, detail=none); + INSERT INTO ft1(ft1, rank) VALUES('secure-delete', 1); +} + +do_execsql_test 1.1 { + BEGIN; + INSERT INTO ft1(rowid, a) VALUES(1, 'abcd'); + INSERT INTO ft1(rowid, a) VALUES(2, 'abcd'); + INSERT INTO ft1(rowid, a) VALUES(3, 'abcd'); + COMMIT; +} +do_execsql_test 1.2 { + DELETE FROM ft1 WHERE rowid=1; +} +do_execsql_test 1.3 { + INSERT INTO ft1(ft1) VALUES('integrity-check'); +} +do_execsql_test 1.4 { + DELETE FROM ft1 WHERE rowid=3; +} +do_execsql_test 1.5 { + INSERT INTO ft1(ft1) VALUES('integrity-check'); +} +do_execsql_test 1.6 { + DELETE FROM ft1 WHERE rowid=3; +} +do_execsql_test 1.7 { + INSERT INTO ft1(ft1) VALUES('integrity-check'); +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 2.0 { + CREATE VIRTUAL TABLE ft1 USING fts5(a, detail=none); + INSERT INTO ft1(ft1, rank) VALUES('secure-delete', 1); +} + +do_execsql_test 2.1 { + BEGIN; + INSERT INTO ft1(rowid, a) VALUES(1, 'abcd one'); + INSERT INTO ft1(rowid, a) VALUES(2, 'abcd two'); + INSERT INTO ft1(rowid, a) VALUES(3, 'abcd two'); + INSERT INTO ft1(rowid, a) VALUES(4, 'abcd two'); + INSERT INTO ft1(rowid, a) VALUES(5, 'abcd three'); + COMMIT; +} + +do_execsql_test 2.2a { + DELETE FROM ft1 WHERE rowid=3; +} +do_execsql_test 2.2b { + INSERT INTO ft1(ft1) VALUES('integrity-check'); +} +do_execsql_test 2.3a { + DELETE FROM ft1 WHERE rowid=2; +} +do_execsql_test 2.3b { + INSERT INTO ft1(ft1) VALUES('integrity-check'); +} +do_execsql_test 2.4a { + DELETE FROM ft1 WHERE rowid=4; +} +do_execsql_test 2.4b { + INSERT INTO ft1(ft1) VALUES('integrity-check'); +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 3.0 { + CREATE VIRTUAL TABLE ft1 USING fts5(a, detail=none, prefix=1); + INSERT INTO ft1(ft1, rank) VALUES('secure-delete', 1); + INSERT INTO ft1(ft1, rank) VALUES('pgsz', 64); +} +do_execsql_test 3.1 { + BEGIN; + INSERT INTO ft1(a) VALUES('c'); + COMMIT; +} +do_execsql_test 3.2 { + DELETE FROM ft1 WHERE rowid IN (1); + INSERT INTO ft1(ft1) VALUES('integrity-check'); +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 4.0 { + CREATE VIRTUAL TABLE ft1 USING fts5(a, detail=none); + INSERT INTO ft1(ft1, rank) VALUES('secure-delete', 1); + INSERT INTO ft1(ft1, rank) VALUES('pgsz', 64); + + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<500 + ) + INSERT INTO ft1 SELECT 'abcdefg' FROM s; +} + +do_test 4.1 { + for {set i 500} {$i > 0} {incr i -1} { + execsql { DELETE FROM ft1 WHERE rowid=$i } + execsql { INSERT INTO ft1(ft1) VALUES('integrity-check') } + } +} {} + +finish_test + diff --git a/ext/fts5/test/fts5secure6.test b/ext/fts5/test/fts5secure6.test new file mode 100644 index 0000000000..e2f4ceabc8 --- /dev/null +++ b/ext/fts5/test/fts5secure6.test @@ -0,0 +1,141 @@ +# 2023 Feb 17 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# + +source [file join [file dirname [info script]] fts5_common.tcl] +ifcapable !fts5 { finish_test ; return } +set ::testprefix fts5secure6 + +db progress 1 progress_handler +set ::PHC 0 +proc progress_handler {args} { + incr ::PHC + # if {($::PHC % 100000)==0} breakpoint + return 0 +} + +proc setup {} { + db eval { + DROP TABLE IF EXISTS t1; + CREATE VIRTUAL TABLE t1 USING fts5(x); + WITH s(i) AS ( + VALUES(1) UNION ALL SELECT i+1 FROM s WHERE i<1000 + ) + INSERT INTO t1 SELECT 'a b c d e f g h i j k' FROM s; + } +} + +foreach {tn sd} { + 1 0 + 2 1 +} { + setup + do_execsql_test 1.$tn.0 { + INSERT INTO t1(t1, rank) VALUES('secure-delete', $sd) + } + set PHC 0 + do_execsql_test 1.$tn.1 { DELETE FROM t1; } + set phc($tn) $PHC +} + +do_test 1.3 { + expr $phc(1)*5 < $phc(2) +} {1} + +#------------------------------------------------------------------------- +reset_db + +do_execsql_test 2.0 { + CREATE VIRTUAL TABLE t1 USING fts5(x); + INSERT INTO t1(t1, rank) VALUES('secure-delete', $sd) +} + +do_execsql_test 2.1 { + BEGIN; + INSERT INTO t1(rowid, x) VALUES(-100000, 'abc def ghi'); + INSERT INTO t1(rowid, x) VALUES(-99999, 'abc def ghi'); + INSERT INTO t1(rowid, x) VALUES(9223372036854775800, 'abc def ghi'); + COMMIT; +} + +do_execsql_test 2.2 { + SELECT rowid FROM t1('def') +} {-100000 -99999 9223372036854775800} + +#------------------------------------------------------------------------- +reset_db + +do_execsql_test 3.0 { + CREATE VIRTUAL TABLE t1 USING fts5(x); + INSERT INTO t1(t1, rank) VALUES('secure-delete', $sd) +} + +do_execsql_test 3.1 { + BEGIN; + INSERT INTO t1(rowid, x) + VALUES(51869, 'when whenever where weress what turn'), + (51871, 'to were'); + COMMIT; +} + +do_execsql_test 3.2 { + DELETE FROM t1 WHERE rowid=51871; + INSERT INTO t1(t1) VALUES('integrity-check'); +} + +#------------------------------------------------------------------------- +reset_db + +do_execsql_test 4.0 { + CREATE VIRTUAL TABLE t1 USING fts5(x); + INSERT INTO t1(rowid, x) VALUES(10, 'one two'); +} +do_execsql_test 4.1 { + UPDATE t1 SET x = 'one three' WHERE rowid=10; + INSERT INTO t1(t1, rank) VALUES('secure-delete', 1); +} +do_execsql_test 4.2 { + DELETE FROM t1 WHERE rowid=10; +} +do_execsql_test 4.3 { + INSERT INTO t1(t1) VALUES('integrity-check'); +} + +#------------------------------------------------------------------------- +reset_db + +do_execsql_test 5.0 { + CREATE VIRTUAL TABLE t1 USING fts5(content); + + INSERT INTO t1(t1,rank) VALUES('secure-delete',1); + INSERT INTO t1 VALUES('active'),('boomer'),('atom'),('atomic'), + ('alpha channel backup abandon test aback boomer atom alpha active'); + DELETE FROM t1 WHERE t1 MATCH 'abandon'; +} + +do_execsql_test 5.1 { + INSERT INTO t1(t1) VALUES('rebuild'); +} + +do_execsql_test 5.2 { + DELETE FROM t1 WHERE rowid NOTNULL<5; +} + +db close +sqlite3 db test.db + +do_execsql_test 5.3 { + PRAGMA integrity_check; +} {ok} + + +finish_test + diff --git a/ext/fts5/test/fts5secure7.test b/ext/fts5/test/fts5secure7.test new file mode 100644 index 0000000000..16a044f538 --- /dev/null +++ b/ext/fts5/test/fts5secure7.test @@ -0,0 +1,116 @@ +# 2023 Feb 17 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# +# TESTRUNNER: slow +# + +source [file join [file dirname [info script]] fts5_common.tcl] +ifcapable !fts5 { finish_test ; return } +set ::testprefix fts5secure7 + + +set NVOCAB 500 +set NDOC [expr 1000] + +set NREP 100 +set nDeletePerRep [expr 5] + +set VOCAB [list] + +proc select_one {list} { + set n [llength $list] + lindex $list [expr {abs(int(rand()*$n))}] +} + +proc init_vocab {} { + set L [split "abcdefghijklmnopqrstuvwxyz" {}] + set nL [llength $L] + for {set i 0} {$i < $::NVOCAB} {incr i} { + set n [expr {6 + int(rand()*8)}] + set word "" + for {set j 0} {$j < $n} {incr j} { + append word [select_one $L] + } + lappend ::VOCAB $word + } +} + +proc get_word {} { + select_one $::VOCAB +} + +proc get_document {nWord} { + set ret [list] + for {set i 0} {$i < $nWord} {incr i} { + lappend ret [get_word] + } + return $ret +} + +init_vocab + +db func document [list get_document 12] + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE t1 USING fts5(body); + INSERT INTO t1(t1, rank) VALUES('secure-delete', 1); +} +do_execsql_test 1.1 { + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<$NDOC + ) + INSERT INTO t1 SELECT document() FROM s; +} + +for {set iRep 0} {$iRep < $NREP} {incr iRep} { + set lRowid [db eval {SELECT rowid FROM t1}] + for {set iDel 0} {$iDel < $nDeletePerRep} {incr iDel} { + set idx [select_one $lRowid] + db eval { + DELETE FROM t1 WHERE rowid=$idx + } + } + db eval { + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<$nDeletePerRep + ) + INSERT INTO t1 SELECT document() FROM s; + } + do_execsql_test 1.2.$iRep { + INSERT INTO t1(t1) VALUES('integrity-check'); + } +} + +reset_db +db func document [list get_document 12] +do_execsql_test 2.0 { + CREATE VIRTUAL TABLE t1 USING fts5(body); + INSERT INTO t1(t1, rank) VALUES('secure-delete', 1); + INSERT INTO t1(t1, rank) VALUES('pgsz', 128); +} +do_execsql_test 2.1 { + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<$NDOC + ) + INSERT INTO t1 SELECT document() FROM s; +} +for {set ii 0} {$ii < $NDOC} {incr ii} { + set lRowid [db eval {SELECT rowid FROM t1}] + set idx [select_one $lRowid] + db eval { DELETE FROM t1 WHERE rowid=$idx } + do_execsql_test 2.2.$ii { + INSERT INTO t1(t1) VALUES('integrity-check'); + } +} + +finish_test + + diff --git a/ext/fts5/test/fts5secure8.test b/ext/fts5/test/fts5secure8.test new file mode 100644 index 0000000000..8b65b7c59f --- /dev/null +++ b/ext/fts5/test/fts5secure8.test @@ -0,0 +1,71 @@ +# 2023 Nov 23 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# + +source [file join [file dirname [info script]] fts5_common.tcl] +ifcapable !fts5 { finish_test ; return } +set ::testprefix fts5secure8 + +proc sql_repeat {txt n} { + string repeat $txt $n +} +db func repeat sql_repeat + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE ft USING fts5(x); + + INSERT INTO ft(ft, rank) VALUES('pgsz', 64); + + INSERT INTO ft(rowid, x) VALUES(100, 'hello world'); + INSERT INTO ft(rowid, x) VALUES(200, 'one day'); + + BEGIN; + INSERT INTO ft(rowid, x) VALUES(45, 'one two three'); + UPDATE ft SET x = repeat('hello world ', 500) WHERE rowid=100; + COMMIT +} + +do_execsql_test 1.1 { + INSERT INTO ft(ft, rank) VALUES('secure-delete', 1); + DELETE FROM ft WHERE rowid=100; +} + +do_execsql_test 1.2 { + PRAGMA integrity_check; +} {ok} + +do_execsql_test 2.0 { + CREATE VIRTUAL TABLE xyz USING fts5 ( + name, + content='' + ); + + INSERT INTO xyz(xyz, rank) VALUES('secure-delete', 1); + INSERT INTO xyz (rowid, name) VALUES(1, 'A'); + INSERT INTO xyz (rowid, name) VALUES(2, 'A'); + INSERT INTO xyz(xyz, rowid, name) VALUES('delete', 2, 'A'); +} + +do_execsql_test 2.1 { + pragma quick_check; +} {ok} + +do_catchsql_test 2.2 { + INSERT INTO xyz(xyz, rank) VALUES('secure-delete', 'hello world'); +} {1 {SQL logic error}} + + + + + +finish_test + + diff --git a/ext/fts5/test/fts5securefault.test b/ext/fts5/test/fts5securefault.test new file mode 100644 index 0000000000..2959ab65ce --- /dev/null +++ b/ext/fts5/test/fts5securefault.test @@ -0,0 +1,225 @@ +# 2023 April 14 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# This file implements regression tests for SQLite library. The +# focus of this script is testing the FTS5 module. +# + +source [file join [file dirname [info script]] fts5_common.tcl] +source $testdir/malloc_common.tcl +set testprefix fts5securefault + +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. +return_if_no_fts5 + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE t1 USING fts5(ab); + INSERT INTO t1(rowid, ab) VALUES + (0, 'abc'), (1, 'abc'), (2, 'abc'), (3, 'abc'), (4, 'def'); +} +faultsim_save_and_close + +do_faultsim_test 1.1 -faults oom* -prep { + faultsim_restore_and_reopen + execsql { + INSERT INTO t1(t1, rank) VALUES('secure-delete', 1); + } +} -body { + execsql { DELETE FROM t1 WHERE rowid=2 } +} -test { + faultsim_test_result {0 {}} +} +do_faultsim_test 1.2 -faults oom* -prep { + faultsim_restore_and_reopen + execsql { + INSERT INTO t1(t1, rank) VALUES('secure-delete', 1); + } +} -body { + execsql { DELETE FROM t1 WHERE rowid IN(0, 1, 2, 3, 4) } +} -test { + faultsim_test_result {0 {}} +} + +#------------------------------------------------------------------------- +# +reset_db +set big [string repeat abcdefghij 5] +set big2 [string repeat klmnopqrst 5] +set doc "$big $big2" + +do_execsql_test 2.0 { + CREATE VIRTUAL TABLE t1 USING fts5(ab); + INSERT INTO t1(t1, rank) VALUES('pgsz', 64); + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<4 + ) + INSERT INTO t1(rowid, ab) SELECT i, $doc FROM s; +} +faultsim_save_and_close + +do_faultsim_test 2.1 -faults oom* -prep { + faultsim_restore_and_reopen + execsql { + INSERT INTO t1(t1, rank) VALUES('secure-delete', 1); + } +} -body { + execsql { DELETE FROM t1 WHERE rowid = 3 } + execsql { DELETE FROM t1 WHERE rowid = 4 } +} -test { + faultsim_test_result {0 {}} +} + +#------------------------------------------------------------------------- +# +reset_db +set big [string repeat abcdefghij 5] +set big2 [string repeat klmnopqrst 5] +set doc "$big $big2" + +do_execsql_test 3.0 { + CREATE VIRTUAL TABLE t1 USING fts5(ab); + INSERT INTO t1(t1, rank) VALUES('pgsz', 64); + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<25 + ) + INSERT INTO t1(rowid, ab) SELECT i, $doc FROM s; + + INSERT INTO t1(t1, rank) VALUES('secure-delete', 1); + DELETE FROM t1 WHERE rowid BETWEEN 3 AND 23; +} +faultsim_save_and_close + +do_faultsim_test 3.1 -faults oom* -prep { + faultsim_restore_and_reopen + execsql { + INSERT INTO t1(t1, rank) VALUES('secure-delete', 1); + } +} -body { + execsql { DELETE FROM t1 WHERE rowid = 24 } + execsql { DELETE FROM t1 WHERE rowid = 25 } +} -test { + faultsim_test_result {0 {}} +} + +#------------------------------------------------------------------------- +# +reset_db +set doc [string repeat "tok " 400] + +do_execsql_test 4.0 { + CREATE VIRTUAL TABLE t1 USING fts5(ab); + INSERT INTO t1(t1, rank) VALUES('pgsz', 64); + INSERT INTO t1(rowid, ab) VALUES(1, $doc), (2, $doc), (3, $doc); +} +faultsim_save_and_close + +do_faultsim_test 4.1 -faults oom* -prep { + faultsim_restore_and_reopen + execsql { + INSERT INTO t1(t1, rank) VALUES('secure-delete', 1); + } +} -body { + execsql { DELETE FROM t1 WHERE rowid = 2 } +} -test { + faultsim_test_result {0 {}} +} + +#------------------------------------------------------------------------- +# +reset_db + +set doc1 [string repeat "abc " 10] +set doc2 [string repeat "def " 10] + +do_test 5.0 { + execsql { + CREATE VIRTUAL TABLE t1 USING fts5(ab); + INSERT INTO t1(t1, rank) VALUES('pgsz', 64); + BEGIN; + } + for {set i 0} {$i < 50} {incr i} { + execsql { + INSERT INTO t1(rowid, ab) VALUES($i, 'abcdefg'); + } + } + execsql { + INSERT INTO t1(rowid, ab) VALUES(105, 'def'); + COMMIT; + } +} {} +faultsim_save_and_close + +do_faultsim_test 5.1 -faults oom* -prep { + faultsim_restore_and_reopen + execsql { + INSERT INTO t1(t1, rank) VALUES('secure-delete', 1); + } +} -body { + execsql { DELETE FROM t1 WHERE rowid = 105 } +} -test { + faultsim_test_result {0 {}} +} + +#------------------------------------------------------------------------- +# +reset_db +do_test 6.0 { + execsql { + CREATE VIRTUAL TABLE t1 USING fts5(ab); + INSERT INTO t1(t1, rank) VALUES('pgsz', 64); + BEGIN; + INSERT INTO t1(rowid, ab) VALUES(1, 'abcdefg'); + INSERT INTO t1(rowid, ab) VALUES(2, 'abcdefg'); + INSERT INTO t1(rowid, ab) VALUES(3, 'abcdefg'); + COMMIT; + } +} {} +faultsim_save_and_close + +do_faultsim_test 6.1 -faults oom* -prep { + faultsim_restore_and_reopen + execsql { + INSERT INTO t1(t1, rank) VALUES('secure-delete', 1); + } +} -body { + execsql { + UPDATE t1 SET ab='abcdefg' WHERE rowid=2; + } +} -test { + faultsim_test_result {0 {}} +} + +#------------------------------------------------------------------------- +# +reset_db +do_test 7.0 { + execsql { + CREATE VIRTUAL TABLE t1 USING fts5(ab); + INSERT INTO t1(t1, rank) VALUES('pgsz', 32); + INSERT INTO t1(t1, rank) VALUES('secure-delete', 1); + } +} {} +faultsim_save_and_close + +do_faultsim_test 7.1 -faults oom* -prep { + faultsim_restore_and_reopen + set big1 "[string repeat x 50] [string repeat y 50] [string repeat z 50]" + execsql { + BEGIN; + INSERT INTO t1 VALUES($big1); + } +} -body { + execsql { COMMIT } +} -test { + faultsim_test_result {0 {}} +} + + +finish_test diff --git a/ext/fts5/test/fts5simple.test b/ext/fts5/test/fts5simple.test index a095a16230..ad59bf0d9e 100644 --- a/ext/fts5/test/fts5simple.test +++ b/ext/fts5/test/fts5simple.test @@ -13,7 +13,7 @@ source [file join [file dirname [info script]] fts5_common.tcl] set testprefix fts5simple -# If SQLITE_ENABLE_FTS5 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return @@ -265,7 +265,7 @@ do_test 11.0 { execsql " INSERT INTO t4 VALUES('a b c \x1A'); INSERT INTO t4 VALUES('a b c d\x1A'); - INSERT INTO t4 VALUES('a b c \x1Ad'); + INSERT INTO t4 VALUES('a b c \x1Ag'); INSERT INTO t4 VALUES('a b c d'); " } {} @@ -340,7 +340,7 @@ do_test 14.2 { #------------------------------------------------------------------------- db func rnddoc fts5_rnddoc -do_execsql_test 4.0 { +do_execsql_test 14.3 { CREATE VIRTUAL TABLE x1 USING fts5(x); INSERT INTO x1(x1, rank) VALUES('pgsz', 32); @@ -348,9 +348,9 @@ do_execsql_test 4.0 { INSERT INTO x1 SELECT rnddoc(5) FROM ii; } -do_execsql_test 4.1 { +do_execsql_test 14.4 { SELECT rowid, x, x1 FROM x1 WHERE x1 MATCH '*reads' -} {0 {} 4} +} {0 {} 2} #------------------------------------------------------------------------- reset_db @@ -409,5 +409,104 @@ do_catchsql_test 19.2 { SELECT * FROM x1 WHERE x1 MATCH 'c0 AND (c1 AND (c2 AND (c3 AND (c4 AND (c5 AND (c6 AND (c7 AND (c8 AND (c9 AND (c10 AND (c11 AND (c12 AND (c13 AND (c14 AND (c15 AND (c16 AND (c17 AND (c18 AND (c19 AND (c20 AND (c21 AND (c22 AND (c23 AND (c24 AND (c25 AND (c26 AND (c27 AND (c28 AND (c29 AND (c30 AND (c31 AND (c32 AND (c33 AND (c34 AND (c35 AND (c36 AND (c37 AND (c38 AND (c39 AND (c40 AND (c41 AND (c42 AND (c43 AND (c44 AND (c45 AND (c46 AND (c47 AND (c48 AND (c49 AND (c50 AND (c51 AND (c52 AND (c53 AND (c54 AND (c55 AND (c56 AND (c57 AND (c58 AND (c59 AND (c60 AND (c61 AND (c62 AND (c63 AND (c64 AND (c65 AND (c66 AND (c67 AND (c68 AND (c69 AND (c70 AND (c71 AND (c72 AND (c73 AND (c74 AND (c75 AND (c76 AND (c77 AND (c78 AND (c79 AND (c80 AND (c81 AND (c82 AND (c83 AND (c84 AND (c85 AND (c86 AND (c87 AND (c88 AND (c89 AND (c90 AND (c91 AND (c92 AND (c93 AND (c94 AND (c95 AND (c96 AND (c97 AND (c98 AND (c99 AND (c100 AND (c101 AND (c102 AND (c103 AND (c104 AND (c105 AND (c106 AND (c107 AND (c108 AND (c109 AND (c110 AND (c111 AND (c112 AND (c113 AND (c114 AND (c115 AND (c116 AND (c117 AND (c118 AND (c119 AND (c120 AND (c121 AND (c122 AND (c123 AND (c124 AND (c125 AND (c126 AND (c127 AND (c128 AND (c129 AND (c130 AND (c131 AND (c132 AND (c133 AND (c134 AND (c135 AND (c136 AND (c137 AND (c138 AND (c139 AND (c140 AND (c141 AND (c142 AND (c143 AND (c144 AND (c145 AND (c146 AND (c147 AND (c148 AND (c149 AND (c150 AND (c151 AND (c152 AND (c153 AND (c154 AND (c155 AND (c156 AND (c157 AND (c158 AND (c159 AND (c160 AND (c161 AND (c162 AND (c163 AND (c164 AND (c165 AND (c166 AND (c167 AND (c168 AND (c169 AND (c170 AND (c171 AND (c172 AND (c173 AND (c174 AND (c175 AND (c176 AND (c177 AND (c178 AND (c179 AND (c180 AND (c181 AND (c182 AND (c183 AND (c184 AND (c185 AND (c186 AND (c187 AND (c188 AND (c189 AND (c190 AND (c191 AND (c192 AND (c193 AND (c194 AND (c195 AND (c196 AND (c197 AND (c198 AND (c199 AND c200)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))'; } {1 {fts5: parser stack overflow}} -finish_test +#------------------------------------------------------------------------- +reset_db +do_execsql_test 20.0 { + CREATE VIRTUAL TABLE x1 USING fts5(x); + INSERT INTO x1(x1, rank) VALUES('pgsz', 32); + INSERT INTO x1(rowid, x) VALUES(11111, 'onetwothree'); +} +do_test 20.1 { + for {set i 1} {$i <= 200} {incr i} { + execsql { INSERT INTO x1(rowid, x) VALUES($i, 'one two three'); } + } + execsql { INSERT INTO x1(x1) VALUES('optimize'); } + execsql { DELETE FROM x1 WHERE rowid = 4; } +} {} +do_execsql_test 20.2 { + INSERT INTO x1(x1) VALUES('optimize'); + INSERT INTO x1(x1) VALUES('integrity-check'); +} {} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 20.0 { + CREATE VIRTUAL TABLE x1 USING fts5(x); + INSERT INTO x1(x1, rank) VALUES('pgsz', 32); + INSERT INTO x1(rowid, x) VALUES(11111, 'onetwothree'); +} +do_test 20.1 { + for {set i 1} {$i <= 200} {incr i} { + execsql { INSERT INTO x1(rowid, x) VALUES($i, 'one two three'); } + } + execsql { INSERT INTO x1(x1) VALUES('optimize'); } + execsql { DELETE FROM x1 WHERE rowid = 4; } +} {} +do_execsql_test 20.2 { + INSERT INTO x1(x1) VALUES('optimize'); + INSERT INTO x1(x1) VALUES('integrity-check'); +} {} + +#------------------------------------------------------------------------- +reset_db +set doc "a b [string repeat x 100000]" +do_execsql_test 21.0 { + CREATE VIRTUAL TABLE x1 USING fts5(x); + INSERT INTO x1(rowid, x) VALUES(11111, $doc); + INSERT INTO x1(rowid, x) VALUES(11112, $doc); +} +do_execsql_test 21.1 { + INSERT INTO x1(x1) VALUES('integrity-check'); +} +do_execsql_test 21.2 { + SELECT rowid FROM x1($doc); +} {11111 11112} +do_execsql_test 21.3 { + DELETE FROM x1 WHERE rowid=11111; + INSERT INTO x1(x1) VALUES('integrity-check'); + SELECT rowid FROM x1($doc); +} {11112} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 22.0 { + CREATE VIRTUAL TABLE x1 USING fts5(x); + INSERT INTO x1(x) VALUES('a b c'); + INSERT INTO x1(x) VALUES('x y z'); + INSERT INTO x1(x) VALUES('c b a'); + INSERT INTO x1(x) VALUES('z y x'); +} + +do_catchsql_test 22.1 {SELECT * FROM x1('')} {1 {fts5: syntax error near ""}} +do_catchsql_test 22.2 {SELECT * FROM x1(NULL)} {1 {fts5: syntax error near ""}} +#------------------------------------------------------------------------- +reset_db +do_execsql_test 23.0 { + CREATE VIRTUAL TABLE x1 USING fts5(x); + SELECT count(*) FROM x1_data; +} {2} + +do_execsql_test 23.1 { + BEGIN; + INSERT INTO x1 VALUES('a b c d'); + INSERT INTO x1 VALUES('a b c d'); + INSERT INTO x1 VALUES('a b c d'); +} + +do_execsql_test 23.2 { + SELECT count(*) FROM x1_data; +} {2} + +do_execsql_test 23.3 { + INSERT INTO x1(x1) VALUES('flush'); + SELECT count(*) FROM x1_data; +} {3} + +do_execsql_test 23.4 { + ROLLBACK; + SELECT count(*) FROM x1_data; +} {2} + + +finish_test diff --git a/ext/fts5/test/fts5simple2.test b/ext/fts5/test/fts5simple2.test index 186d771f76..01c590c9f7 100644 --- a/ext/fts5/test/fts5simple2.test +++ b/ext/fts5/test/fts5simple2.test @@ -13,7 +13,7 @@ source [file join [file dirname [info script]] fts5_common.tcl] set testprefix fts5simple2 -# If SQLITE_ENABLE_FTS5 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return @@ -332,7 +332,43 @@ do_execsql_test 16.0 { DELETE FROM t2; } +#------------------------------------------------------------------------- +# +reset_db +do_execsql_test 17.0 { + CREATE VIRTUAL TABLE t2 USING fts5(x, y); + BEGIN; + INSERT INTO t2 VALUES('a aa aaa', 'b bb bbb'); + INSERT INTO t2 VALUES('a aa aaa', 'b bb bbb'); + INSERT INTO t2 VALUES('a aa aaa', 'b bb bbb'); + COMMIT; +} +do_execsql_test 17.1 { + SELECT * FROM t2('y:a*') WHERE rowid BETWEEN 10 AND 20 +} +do_execsql_test 17.2 { + BEGIN; + INSERT INTO t2 VALUES('a aa aaa', 'b bb bbb'); + SELECT * FROM t2('y:a*') WHERE rowid BETWEEN 10 AND 20 ; +} +do_execsql_test 17.3 { + COMMIT +} + +reset_db +do_execsql_test 17.4 { + CREATE VIRTUAL TABLE t2 USING fts5(x, y); + BEGIN; + INSERT INTO t2 VALUES('a aa aaa', 'b bb bbb'); + INSERT INTO t2 VALUES('a aa aaa', 'b bb bbb'); + SELECT * FROM t2('y:a*') WHERE rowid>66; +} +do_execsql_test 17.5 { SELECT * FROM t2('x:b* OR y:a*') } +do_execsql_test 17.5 { COMMIT ; SELECT * FROM t2('x:b* OR y:a*') } +do_execsql_test 17.6 { + SELECT * FROM t2('x:b* OR y:a*') WHERE rowid>55 +} + #db eval {SELECT rowid, fts5_decode_none(rowid, block) aS r FROM t2_data} {puts $r} finish_test - diff --git a/ext/fts5/test/fts5simple3.test b/ext/fts5/test/fts5simple3.test index b6922f84dd..bc3ebfc7ca 100644 --- a/ext/fts5/test/fts5simple3.test +++ b/ext/fts5/test/fts5simple3.test @@ -13,7 +13,7 @@ source [file join [file dirname [info script]] fts5_common.tcl] set testprefix fts5simple3 -# If SQLITE_ENABLE_FTS5 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return @@ -80,6 +80,39 @@ do_execsql_test 3.0 { SELECT * FROM x3('x OR y OR z'); } +#------------------------------------------------------------------------- +# Test that a crash occurring when the second or subsequent tokens in a +# phrase matched zero rows has been fixed. +# +do_execsql_test 4.0 { + CREATE VIRTUAL TABLE t1 USING fts5(x); + INSERT INTO t1 VALUES('ab'); + INSERT INTO t1 VALUES('cd'); + INSERT INTO t1 VALUES('ab cd'); + INSERT INTO t1 VALUES('ab cdXXX'); + INSERT INTO t1 VALUES('abXXX cd'); +} +do_execsql_test 4.1 { + SELECT * FROM t1('"ab cd" OR "ab cd" *'); +} {{ab cd} {ab cdXXX}} +do_execsql_test 4.2 { + SELECT * FROM t1('"xy zz" OR "ab cd" *'); +} {{ab cd} {ab cdXXX}} +do_execsql_test 4.3 { + SELECT * FROM t1('"xy zz" OR "xy zz" *'); +} +do_execsql_test 4.4 { + SELECT * FROM t1('"ab cd" OR "xy zz" *'); +} {{ab cd}} +do_execsql_test 4.5 { + CREATE VIRTUAL TABLE t2 USING fts5(x); + INSERT INTO t2 VALUES('ab'); + INSERT INTO t2 VALUES('cd'); + INSERT INTO t2 VALUES('ef'); +} +do_execsql_test 4.6 { + SELECT * FROM t2('ab + xyz'); +} -finish_test +finish_test diff --git a/ext/fts5/test/fts5synonym.test b/ext/fts5/test/fts5synonym.test index 185dda3ff4..55e2f186a9 100644 --- a/ext/fts5/test/fts5synonym.test +++ b/ext/fts5/test/fts5synonym.test @@ -15,7 +15,7 @@ source [file join [file dirname [info script]] fts5_common.tcl] set testprefix fts5synonym -# If SQLITE_ENABLE_FTS5 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return @@ -152,7 +152,7 @@ foreach {tn expr res} { 1 {abc} {"abc"} 2 {one} {"one"|"i"|"1"} 3 {3} {"3"|"iii"|"three"} - 4 {3*} {"3"|"iii"|"three" *} + 4 {3*} {"3" *} } { do_execsql_test 4.1.$tn { SELECT fts5_expr($expr, 'tokenize=tclnum') @@ -421,4 +421,3 @@ do_execsql_test 7.1.2 { } ;# foreach_detail_mode finish_test - diff --git a/ext/fts5/test/fts5synonym2.test b/ext/fts5/test/fts5synonym2.test index 7e92822c33..ec8b750c57 100644 --- a/ext/fts5/test/fts5synonym2.test +++ b/ext/fts5/test/fts5synonym2.test @@ -15,7 +15,7 @@ source [file join [file dirname [info script]] fts5_common.tcl] set testprefix fts5synonym2 -# If SQLITE_ENABLE_FTS5 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return @@ -42,7 +42,7 @@ proc fts5_test_bothlist {cmd} { } sqlite3_fts5_create_function db fts5_test_bothlist fts5_test_bothlist -proc fts5_rowid {cmd} { expr [$cmd xColumnText -1] } +proc fts5_rowid {cmd} { expr [$cmd xRowid] } sqlite3_fts5_create_function db fts5_rowid fts5_rowid do_execsql_test 1.$tok.0.1 " @@ -122,6 +122,9 @@ foreach {tn expr} { 4.1 "NEAR(one two, 2)" 4.2 "NEAR(one two three, 2)" 4.3 "NEAR(eight nine, 1) OR NEAR(six seven, 1)" + + 5.1 "one + two" + 5.2 "1 + two" } { if {[fts5_expr_ok $expr ss]==0} { do_test 1.$tok.$tn.OMITTED { list } [list] @@ -161,4 +164,3 @@ foreach {tn expr} { } finish_test - diff --git a/ext/fts5/test/fts5tok1.test b/ext/fts5/test/fts5tok1.test index 6ba170062e..c605ce3617 100644 --- a/ext/fts5/test/fts5tok1.test +++ b/ext/fts5/test/fts5tok1.test @@ -109,7 +109,42 @@ do_catchsql_test 2.0 { do_catchsql_test 2.1 { CREATE VIRTUAL TABLE t4 USING fts5tokenize; SELECT * FROM t4; -} {1 {SQL logic error or missing database}} +} {1 {SQL logic error}} + +#------------------------------------------------------------------------- +# Embedded 0x00 characters. +# +reset_db +do_execsql_test 3.1.0 { + CREATE VIRTUAL TABLE t1 USING fts5(z); + CREATE VIRTUAL TABLE tt USING fts5vocab(t1, 'instance'); + INSERT INTO t1 VALUES('abc' || char(0) || 'def'); + SELECT * FROM tt; +} { abc 1 z 0 def 1 z 1 } +do_execsql_test 3.1.1 { + SELECT hex(z) FROM t1; +} {61626300646566} +do_execsql_test 3.1.2 { + INSERT INTO t1(t1) VALUES('integrity-check'); +} {} + +do_execsql_test 3.2.0 { + CREATE VIRTUAL TABLE t2 USING fts5(z, + tokenize="unicode61 categories 'L* N* Co Cc'" + ); + CREATE VIRTUAL TABLE tu USING fts5vocab(t2, 'instance'); + + INSERT INTO t2 VALUES('abc' || char(0) || 'def'); + SELECT * FROM tu; +} { abc 1 z 0 def 1 z 1 } + +do_execsql_test 3.2.1 { + SELECT hex(z) FROM t1; +} {61626300646566} + +do_execsql_test 3.2.2 { + INSERT INTO t1(t1) VALUES('integrity-check'); +} {} finish_test diff --git a/ext/fts5/test/fts5tokendata.test b/ext/fts5/test/fts5tokendata.test new file mode 100644 index 0000000000..7f75f4fa8e --- /dev/null +++ b/ext/fts5/test/fts5tokendata.test @@ -0,0 +1,105 @@ +# 2014 Jan 08 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# Tests focused on phrase queries. +# + +source [file join [file dirname [info script]] fts5_common.tcl] +set testprefix fts5tokendata + +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. +ifcapable !fts5 { + finish_test + return +} + +foreach_detail_mode $testprefix { + + sqlite3_fts5_register_origintext db + fts5_aux_test_functions db + proc b {x} { string map [list "\0" "."] $x } + db func b b + + do_execsql_test 1.0 { + CREATE VIRTUAL TABLE ft USING fts5(a, b, tokendata=1, + tokenize="origintext unicode61", detail=%DETAIL% + ); + CREATE VIRTUAL TABLE vocab USING fts5vocab(ft, instance); + } + + do_execsql_test 1.1 { + INSERT INTO ft(rowid, a, b) VALUES + (1, 'Pedagog Pedal Pedant', 'Peculier Day Today'), + (2, 'Pedant pedantic pecked', 'Peck Penalize Pen'); + + INSERT INTO ft(rowid, a, b) VALUES + (3, 'Penalty Pence Penciled', 'One Two Three'), + (4, 'Pedant Pedal Pedant', 'Peculier Day Today'); + } + + do_execsql_test 1.2 { + SELECT DISTINCT b(term) FROM vocab + } { + day.Day one.One peck.Peck pecked peculier.Peculier pedagog.Pedagog + pedal.Pedal pedant.Pedant pedantic pen.Pen penalize.Penalize + penalty.Penalty pence.Pence penciled.Penciled three.Three + today.Today two.Two + } + + do_execsql_test 1.3.1 { + SELECT rowid FROM ft('pe*') + } { + 1 2 3 4 + } + + do_execsql_test 1.3.2 { + SELECT rowid FROM ft('pe*') ORDER BY rowid DESC + } { + 4 3 2 1 + } + + if {"%DETAIL%"!="none"} { + do_execsql_test 1.3.3 { + SELECT rowid FROM ft WHERE a MATCH 'pe*' ORDER BY rowid DESC + } { + 4 3 2 1 + } + } + + do_execsql_test 1.4 { + SELECT rowid, b( fts5_test_insttoken(ft, 0, 0) ) FROM ft('pedant') + } { + 1 pedant.Pedant + 2 pedant.Pedant + 4 pedant.Pedant + } + + do_execsql_test 1.5 { + SELECT rowid, b( fts5_test_insttoken(ft, 0, 0) ) FROM ft('pe*') + } { + 1 pedagog.Pedagog + 2 pedant.Pedant + 3 penalty.Penalty + 4 pedant.Pedant + } + + do_execsql_test 1.6 { + SELECT rowid, fts5_test_poslist(ft) FROM ft('pe*') + } { + 1 {0.0.0 0.0.1 0.0.2 0.1.0} + 2 {0.0.0 0.0.1 0.0.2 0.1.0 0.1.1 0.1.2} + 3 {0.0.0 0.0.1 0.0.2} + 4 {0.0.0 0.0.1 0.0.2 0.1.0} + } +} + +finish_test + diff --git a/ext/fts5/test/fts5tokenizer.test b/ext/fts5/test/fts5tokenizer.test index 9316d3c234..a828e3a22b 100644 --- a/ext/fts5/test/fts5tokenizer.test +++ b/ext/fts5/test/fts5tokenizer.test @@ -15,7 +15,7 @@ source [file join [file dirname [info script]] fts5_common.tcl] set testprefix fts5tokenizer -# If SQLITE_ENABLE_FTS5 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return @@ -189,7 +189,7 @@ do_catchsql_test 6.2 { } {1 {error in tokenizer constructor}} do_catchsql_test 6.3 { CREATE VIRTUAL TABLE a3 USING fts5( - x, y, tokenize = 'unicode61 remove_diacritics 2' + x, y, tokenize = 'unicode61 remove_diacritics 3' ); } {1 {error in tokenizer constructor}} do_catchsql_test 6.4 { @@ -262,5 +262,113 @@ do_execsql_test 8.3 { brown dog fox jump lazi over quick the } -finish_test +#------------------------------------------------------------------------- +# Check that the FTS5_TOKENIZE_PREFIX flag is passed to the tokenizer +# implementation. +# +reset_db +proc tcl_create {args} { return "tcl_tokenize" } +sqlite3_fts5_create_tokenizer db tcl tcl_create +set ::flags [list] +proc tcl_tokenize {tflags text} { + lappend ::flags $tflags + foreach {w iStart iEnd} [fts5_tokenize_split $text] { + sqlite3_fts5_token $w $iStart $iEnd + } +} + +do_execsql_test 9.1.1 { + CREATE VIRTUAL TABLE t1 USING fts5(a, tokenize=tcl); + INSERT INTO t1 VALUES('abc'); + INSERT INTO t1 VALUES('xyz'); +} {} +do_test 9.1.2 { set ::flags } {document document} + +set ::flags [list] +do_execsql_test 9.2.1 { SELECT * FROM t1('abc'); } {abc} +do_test 9.2.2 { set ::flags } {query} + +set ::flags [list] +do_execsql_test 9.3.1 { SELECT * FROM t1('ab*'); } {abc} +do_test 9.3.2 { set ::flags } {prefixquery} + +set ::flags [list] +do_execsql_test 9.4.1 { SELECT * FROM t1('"abc xyz" *'); } {} +do_test 9.4.2 { set ::flags } {prefixquery} + +set ::flags [list] +do_execsql_test 9.5.1 { SELECT * FROM t1('"abc xyz*"'); } {} +do_test 9.5.2 { set ::flags } {query} + +#------------------------------------------------------------------------- +# +reset_db +do_execsql_test 10.1 { + CREATE VIRTUAL TABLE x1 USING fts5(x, tokenize=unicode61); + PRAGMA writable_schema = 1; + UPDATE sqlite_schema + SET sql = 'CREATE VIRTUAL TABLE x1 USING fts5(x, tokenize="unicode61 error");' + WHERE name = 'x1'; +} + +db close +sqlite3 db test.db + +do_catchsql_test 10.2 { + SELECT * FROM x1('abc'); +} {1 {error in tokenizer constructor}} + +do_catchsql_test 10.3 { + INSERT INTO x1 VALUES('abc'); +} {1 {error in tokenizer constructor}} + +do_execsql_test 10.4 { + PRAGMA writable_schema = 1; + UPDATE sqlite_schema + SET sql = 'CREATE VIRTUAL TABLE x1 USING fts5(x, tokenize="nosuch error");' + WHERE name = 'x1'; +} + +db close +sqlite3 db test.db + +do_catchsql_test 10.5 { + SELECT * FROM x1('abc'); +} {1 {no such tokenizer: nosuch}} +do_catchsql_test 10.6 { + INSERT INTO x1 VALUES('abc'); +} {1 {no such tokenizer: nosuch}} + +do_execsql_test 10.7 { + DROP TABLE x1; + SELECT * FROM sqlite_schema; +} + +reset_db +do_execsql_test 10.8 { + CREATE VIRTUAL TABLE x1 USING fts5(x, tokenize=unicode61); + INSERT INTO x1 VALUES('a b c'), ('d e f'), ('a b c'); + CREATE VIRTUAL TABLE x1v USING fts5vocab(x1, row); + PRAGMA writable_schema = 1; + UPDATE sqlite_schema + SET sql = 'CREATE VIRTUAL TABLE x1 USING fts5(x, tokenize=simplify);' + WHERE name = 'x1'; +} + +do_execsql_test 10.9 { + SELECT * FROM x1v +} { + a 2 2 b 2 2 c 2 2 d 1 1 e 1 1 f 1 1 +} + +db close +sqlite3 db test.db + +do_execsql_test 10.10 { + SELECT * FROM x1v +} { + a 2 2 b 2 2 c 2 2 d 1 1 e 1 1 f 1 1 +} + +finish_test diff --git a/ext/fts5/test/fts5tokenizer2.test b/ext/fts5/test/fts5tokenizer2.test new file mode 100644 index 0000000000..4fe31d22c4 --- /dev/null +++ b/ext/fts5/test/fts5tokenizer2.test @@ -0,0 +1,109 @@ +# 2023 Nov 03 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# Tests focusing on the built-in fts5 tokenizers. +# + +source [file join [file dirname [info script]] fts5_common.tcl] +set testprefix fts5tokenizer2 + +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. +ifcapable !fts5 { + finish_test + return +} + +sqlite3_fts5_create_tokenizer db tst get_tst_tokenizer +proc get_tst_tokenizer {args} { + return "tst_tokenizer" +} +proc tst_tokenizer {flags txt} { + set token "" + set lTok [list] + + foreach c [split $txt {}] { + if {$token==""} { + append token $c + } else { + set t1 [string is upper $token] + set t2 [string is upper $c] + + if {$t1!=$t2} { + lappend lTok $token + set token "" + } + append token $c + } + } + if {$token!=""} { lappend lTok $token } + + set iOff 0 + foreach t $lTok { + set n [string length $t] + sqlite3_fts5_token $t $iOff [expr $iOff+$n] + incr iOff $n + } +} + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE t1 USING fts5(t, tokenize=tst); +} + +do_execsql_test 1.1 { + INSERT INTO t1 VALUES('AAdontBBmess'); +} + +do_execsql_test 1.2 { + SELECT snippet(t1, 0, '>', '<', '...', 4) FROM t1('BB'); +} {AAdont>BB', '<') FROM t1('BB'); +} {AAdont>BB', '<') FROM t1('AA'); +} {>AA', '<') FROM t1('dont'); +} {AA>dont', '<') FROM t1('mess'); +} {AAdontBB>mess<} + +do_execsql_test 1.7 { + SELECT highlight(t1, 0, '>', '<') FROM t1('BB mess'); +} {AAdont>BBmess<} + +# 2024-08-06 https://sqlite.org/forum/forumpost/171bcc2bcd +# Error handling of tokenize= arguments. +# +foreach {n tkz} { + 1 {ascii none} + 2 {unicode61 none} + 3 {porter none} + 4 {trigram none} + 5 {ascii none 0} + 6 {unicode61 none 0} + 7 {porter none 0} + 8 {trigram none 0} +} { + db eval {DROP TABLE IF EXISTS t2;} + do_catchsql_test 2.$n " + DROP TABLE IF EXISTS t2; + CREATE VIRTUAL TABLE t2 USING fts5(a,b,c,tokenize='$tkz'); + " {1 {error in tokenizer constructor}} +} + + +finish_test diff --git a/ext/fts5/test/fts5tokenizer3.test b/ext/fts5/test/fts5tokenizer3.test new file mode 100644 index 0000000000..5cdab743c2 --- /dev/null +++ b/ext/fts5/test/fts5tokenizer3.test @@ -0,0 +1,77 @@ +# 2024 Aug 10 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# Tests focusing on the built-in fts5 tokenizers. +# + +source [file join [file dirname [info script]] fts5_common.tcl] +set testprefix fts5tokenizer3 + +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. +ifcapable !fts5 { + finish_test + return +} + + +proc get_sod {args} { return "split_on_dot" } +proc get_lowercase {args} { return "lowercase" } + +proc lowercase {flags txt} { + set n [string length $txt] + sqlite3_fts5_token [string tolower $txt] 0 $n + return 0 +} + +proc split_on_dot {flags txt} { + set iOff 0 + foreach t [split $txt "."] { + set n [string length $txt] + sqlite3_fts5_token $t $iOff [expr $iOff+$n] + incr iOff [expr {$n+1}] + } + return "" +} + +foreach {tn script} { + 1 { + sqlite3_fts5_create_tokenizer db lowercase get_lowercase + sqlite3_fts5_create_tokenizer -parent lowercase db split_on_dot get_sod + } + 2 { + sqlite3_fts5_create_tokenizer -v2 db lowercase get_lowercase + sqlite3_fts5_create_tokenizer -parent lowercase db split_on_dot get_sod + } + 3 { + sqlite3_fts5_create_tokenizer db lowercase get_lowercase + sqlite3_fts5_create_tokenizer -v2 -parent lowercase db split_on_dot get_sod + } + 4 { + sqlite3_fts5_create_tokenizer -v2 db lowercase get_lowercase + sqlite3_fts5_create_tokenizer -v2 -parent lowercase db split_on_dot get_sod + } +} { + reset_db + eval $script + + do_execsql_test 1.$tn.0 { + CREATE VIRTUAL TABLE t1 USING fts5(x, tokenize=split_on_dot); + CREATE VIRTUAL TABLE t1vocab USING fts5vocab(t1, instance); + INSERT INTO t1 VALUES('ABC.Def.ghi'); + } + + do_execsql_test 1.$tn.1 { + SELECT term FROM t1vocab ORDER BY 1 + } {abc def ghi} +} + + +finish_test diff --git a/ext/fts5/test/fts5trigram.test b/ext/fts5/test/fts5trigram.test new file mode 100644 index 0000000000..377e3f7813 --- /dev/null +++ b/ext/fts5/test/fts5trigram.test @@ -0,0 +1,366 @@ +# 2020 September 30 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# +# Tests for the fts5 "trigram" tokenizer. +# + +source [file join [file dirname [info script]] fts5_common.tcl] +ifcapable !fts5 { finish_test ; return } +set ::testprefix fts5trigram + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE t1 USING fts5(y, tokenize=trigram); + INSERT INTO t1 VALUES('abcdefghijklm'); + INSERT INTO t1 VALUES('กรุงเทพมหานคร'); +} + +foreach {tn s res} { + 1 abc "(abc)defghijklm" + 2 defgh "abc(defgh)ijklm" + 3 abcdefghijklm "(abcdefghijklm)" + 4 กรุ "(กรุ)งเทพมหานคร" + 5 งเทพมห "กรุ(งเทพมห)านคร" + 6 กรุงเทพมหานคร "(กรุงเทพมหานคร)" + 7 Abc "(abc)defghijklm" + 8 deFgh "abc(defgh)ijklm" + 9 aBcdefGhijKlm "(abcdefghijklm)" +} { + do_execsql_test 1.1.$tn { + SELECT highlight(t1, 0, '(', ')') FROM t1($s) + } $res +} + +do_execsql_test 1.2.0 { + SELECT fts5_expr('ABCD', 'tokenize=trigram') +} {{"abc" + "bcd"}} + +do_execsql_test 1.2.1 { + SELECT * FROM t1 WHERE y LIKE ? ESCAPE 'a' +} + +foreach {tn like res} { + 1 {%cDef%} 1 + 2 {cDef%} {} + 3 {%f%} 1 + 4 {%f_h%} 1 + 5 {%f_g%} {} + 6 {abc%klm} 1 + 7 {ABCDEFG%} 1 + 8 {%รุงเ%} 2 + 9 {%งเ%} 2 + 10 {%"งเ"%} {} +} { + do_execsql_test 1.3.$tn { + SELECT rowid FROM t1 WHERE y LIKE $like + } $res +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 2.0 { + CREATE VIRTUAL TABLE t1 USING fts5(y, tokenize="trigram case_sensitive 1"); + INSERT INTO t1 VALUES('abcdefghijklm'); + INSERT INTO t1 VALUES('กรุงเทพมหานคร'); +} +do_catchsql_test 2.0.1 { + CREATE VIRTUAL TABLE t2 USING fts5(z, tokenize='trigram case_sensitive'); +} {1 {error in tokenizer constructor}} + +foreach {tn s res} { + 1 abc "(abc)defghijklm" + 2 defgh "abc(defgh)ijklm" + 3 abcdefghijklm "(abcdefghijklm)" + 4 กรุ "(กรุ)งเทพมหานคร" + 5 งเทพมห "กรุ(งเทพมห)านคร" + 6 กรุงเทพมหานคร "(กรุงเทพมหานคร)" + 7 Abc "" + 8 deFgh "" + 9 aBcdefGhijKlm "" +} { + do_execsql_test 2.1.$tn { + SELECT highlight(t1, 0, '(', ')') FROM t1($s) + } $res +} +foreach {tn like res} { + 1 {%cDef%} 1 + 2 {cDef%} {} + 3 {%f%} 1 + 4 {%f_h%} 1 + 5 {%f_g%} {} + 6 {abc%klm} 1 + 7 {ABCDEFG%} 1 + 8 {%รุงเ%} 2 +} { + do_execsql_test 2.2.$tn { + SELECT rowid FROM t1 WHERE y LIKE $like + } $res +} +foreach {tn like res} { + 1 {*cdef*} 1 + 2 {cdef*} {} + 3 {*f*} 1 + 4 {*f?h*} 1 + 5 {*f?g*} {} + 6 {abc*klm} 1 + 7 {abcdefg*} 1 + 8 {*รุงเ*} 2 + 9 {abc[d]efg*} 1 + 10 {abc[]d]efg*} 1 + 11 {abc[^]d]efg*} {} + 12 {abc[^]XYZ]efg*} 1 +} { + do_execsql_test 2.3.$tn { + SELECT rowid FROM t1 WHERE y GLOB $like + } $res +} + +do_execsql_test 2.3.null.1 { + SELECT rowid FROM t1 WHERE y LIKE NULL +} + +#------------------------------------------------------------------------- +reset_db +do_catchsql_test 3.1 { + CREATE VIRTUAL TABLE ttt USING fts5(c, tokenize="trigram case_sensitive 2"); +} {1 {error in tokenizer constructor}} +do_catchsql_test 3.2 { + CREATE VIRTUAL TABLE ttt USING fts5(c, tokenize="trigram case_sensitive 11"); +} {1 {error in tokenizer constructor}} +do_catchsql_test 3.3 { + CREATE VIRTUAL TABLE ttt USING fts5(c, "tokenize=trigram case_sensitive 1"); +} {0 {}} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 4.0 { + CREATE VIRTUAL TABLE t0 USING fts5(b, tokenize = "trigram"); +} +do_execsql_test 4.1 { + INSERT INTO t0 VALUES (x'000b01'); +} +do_execsql_test 4.2 { + INSERT INTO t0(t0) VALUES('integrity-check'); +} + +#------------------------------------------------------------------------- +reset_db +foreach_detail_mode $::testprefix { + foreach {ci} {0 1} { + reset_db + do_execsql_test 5.cs=$ci.0.1 " + CREATE VIRTUAL TABLE t1 USING fts5( + y, tokenize=\"trigram case_sensitive $ci\", detail=%DETAIL% + ); + " + do_execsql_test 5.cs=$ci.0.2 { + INSERT INTO t1 VALUES('abcdefghijklm'); + INSERT INTO t1 VALUES('กรุงเทพมหานคร'); + } + + foreach {tn like res} { + 1 {%cDef%} 1 + 2 {cDef%} {} + 3 {%f%} 1 + 4 {%f_h%} 1 + 5 {%f_g%} {} + 6 {abc%klm} 1 + 7 {ABCDEFG%} 1 + 8 {%รุงเ%} 2 + } { + do_execsql_test 5.cs=$ci.1.$tn { + SELECT rowid FROM t1 WHERE y LIKE $like + } $res + } + } +} + +do_execsql_test 6.0 { + CREATE VIRTUAL TABLE ci0 USING fts5(x, tokenize="trigram"); + CREATE VIRTUAL TABLE ci1 USING fts5(x, tokenize="trigram case_sensitive 1"); +} + +# LIKE and GLOB both work with case-insensitive tokenizers. Only GLOB works +# with case-sensitive. +do_eqp_test 6.1 { + SELECT * FROM ci0 WHERE x LIKE ? +} {VIRTUAL TABLE INDEX 0:L0} +do_eqp_test 6.2 { + SELECT * FROM ci0 WHERE x GLOB ? +} {VIRTUAL TABLE INDEX 0:G0} +do_eqp_test 6.3 { + SELECT * FROM ci1 WHERE x LIKE ? +} {{SCAN ci1 VIRTUAL TABLE INDEX 0:}} +do_eqp_test 6.4 { + SELECT * FROM ci1 WHERE x GLOB ? +} {VIRTUAL TABLE INDEX 0:G0} +do_eqp_test 6.5 { + SELECT * FROM ci1 WHERE x < ? +} {{SCAN ci1 VIRTUAL TABLE INDEX 0:}} +do_eqp_test 6.6 { + SELECT * FROM ci0 WHERE x < ? +} {{SCAN ci0 VIRTUAL TABLE INDEX 0:}} + +reset_db +do_execsql_test 7.0 { + CREATE VIRTUAL TABLE f USING FTS5(filename, tokenize="trigram"); + INSERT INTO f (rowid, filename) VALUES + (10, "giraffe.png"), + (20, "жираф.png"), + (30, "cat.png"), + (40, "кот.png"), + (50, "misic-🎵-.mp3"); +} +do_execsql_test 7.1 { + SELECT rowid FROM f WHERE +filename GLOB '*ир*'; +} {20} +do_execsql_test 7.2 { + SELECT rowid FROM f WHERE filename GLOB '*ир*'; +} {20} + + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 8.0 { + CREATE VIRTUAL TABLE t1 USING fts5(y, tokenize=trigram); + INSERT INTO t1 VALUES('abcdefghijklm'); +} + +foreach {tn match res} { + 1 "abc ghi" "(abc)def(ghi)jklm" + 2 "def ghi" "abc(defghi)jklm" + 3 "efg ghi" "abcd(efghi)jklm" + 4 "efghi" "abcd(efghi)jklm" + 5 "abcd jklm" "(abcd)efghi(jklm)" + 6 "ijkl jklm" "abcdefgh(ijklm)" + 7 "ijk ijkl hijk" "abcdefg(hijkl)m" + +} { + do_execsql_test 8.1.$tn { + SELECT highlight(t1, 0, '(', ')') FROM t1($match) + } $res +} + +do_execsql_test 8.2 { + CREATE VIRTUAL TABLE ft2 USING fts5(a, tokenize="trigram"); + INSERT INTO ft2 VALUES('abc x cde'); + INSERT INTO ft2 VALUES('abc cde'); + INSERT INTO ft2 VALUES('abcde'); +} + +do_execsql_test 8.3 { + SELECT highlight(ft2, 0, '[', ']') FROM ft2 WHERE ft2 MATCH 'abc AND cde'; +} { + {[abc] x [cde]} + {[abc] [cde]} + {[abcde]} +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 9.0 { + CREATE VIRTUAL TABLE t1 USING fts5( + a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, + tokenize=trigram + ); + + INSERT INTO t1(rowid, a12) VALUES(111, 'thats a tricky case though'); + INSERT INTO t1(rowid, a12) VALUES(222, 'the query planner cannot do'); +} + +do_execsql_test 9.1 { + SELECT rowid FROM t1 WHERE a12 LIKE '%tricky%' +} {111} + +do_execsql_test 9.2 { + SELECT rowid FROM t1 WHERE a12 LIKE '%tricky%' AND a12 LIKE '%case%' +} {111} + +do_execsql_test 9.3 { + SELECT rowid FROM t1 WHERE a12 LIKE NULL +} {} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 10.0 { + CREATE VIRTUAL TABLE t1 USING fts5(a, tokenize=trigram); +} + +do_test 10.1 { + foreach {val} { + "abc \UFFjkl\UFF" + "abc \UFFFjkl\UFFF" + "abc \UFFFFjkl\UFFFF" + "abc \UFFFFFjkl\UFFFFF" + "\UFFjkl\UFF abc" + "\UFFFjkl\UFFF abc" + "\UFFFFjkl\UFFFF abc" + "\UFFFFFjkl\UFFFFF abc" + "\U10001jkl\U10001 abc" + } { + execsql { INSERT INTO t1 VALUES( $val ) } + } +} {} + +do_test 10.2 { + foreach {val} { + X'E18000626320646566' + X'61EDA0806320646566' + X'61EDA0806320646566' + X'61EFBFBE6320646566' + X'76686920E18000626320646566' + X'7668692061EDA0806320646566' + X'7668692061EDA0806320646566' + X'7668692061EFBFBE6320646566' + } { + execsql " INSERT INTO t1 VALUES( $val ) " + } +} {} + +do_test 10.3 { + set a [binary format c* {0x61 0xF7 0xBF 0xBF 0xBF 0x62}] + set b [binary format c* {0x61 0xF7 0xBF 0xBF 0xBF 0xBF 0x62}] + set c [binary format c* {0x61 0xF7 0xBF 0xBF 0xBF 0xBF 0xBF 0x62}] + set d [binary format c* {0x61 0xF7 0xBF 0xBF 0xBF 0xBF 0xBF 0xBF 0x62}] + execsql { + INSERT INTO t1 VALUES($a); + INSERT INTO t1 VALUES($b); + INSERT INTO t1 VALUES($c); + INSERT INTO t1 VALUES($d); + + INSERT INTO t1 VALUES('abcd' || $a); + INSERT INTO t1 VALUES('abcd' || $b); + INSERT INTO t1 VALUES('abcd' || $c); + INSERT INTO t1 VALUES('abcd' || $d); + } +} {} + +do_execsql_test 11.0 { + CREATE VIRTUAL TABLE t4 USING fts5(y, tokenize=trigram); +} +sqlite3_fts5_register_str db +do_execsql_test 11.1 { + INSERT INTO t4 VALUES( str('') ); +} + +do_test 12.0 { + sqlite3_fts5_tokenize db trigram "abcd" +} {abc 0 3 bcd 1 4} + +do_test 12.1 { + sqlite3_fts5_tokenize db trigram "a" +} {} + +do_test 12.2 { + sqlite3_fts5_tokenize db trigram "" +} {} + +finish_test + diff --git a/ext/fts5/test/fts5trigram2.test b/ext/fts5/test/fts5trigram2.test new file mode 100644 index 0000000000..c81684a22b --- /dev/null +++ b/ext/fts5/test/fts5trigram2.test @@ -0,0 +1,137 @@ +# 2023 October 24 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# +# Tests for the fts5 "trigram" tokenizer. +# + +source [file join [file dirname [info script]] fts5_common.tcl] +ifcapable !fts5 { finish_test ; return } +set ::testprefix fts5trigram2 + +do_execsql_test 1.0 " + CREATE VIRTUAL TABLE t1 USING fts5(y, tokenize='trigram remove_diacritics 1'); + INSERT INTO t1 VALUES('abc\u0303defghijklm'); + INSERT INTO t1 VALUES('a\u0303b\u0303c\u0303defghijklm'); +" +do_catchsql_test 1.0.1 { + CREATE VIRTUAL TABLE t2 USING fts5(z, tokenize='trigram remove_diacritics'); +} {1 {error in tokenizer constructor}} + +do_execsql_test 1.1 { + SELECT highlight(t1, 0, '(', ')') FROM t1('abc'); +} [list \ + "(abc\u0303)defghijklm" \ + "(a\u0303b\u0303c\u0303)defghijklm" \ +] + +do_execsql_test 1.2 { + SELECT highlight(t1, 0, '(', ')') FROM t1('bcde'); +} [list \ + "a(bc\u0303de)fghijklm" \ + "a\u0303(b\u0303c\u0303de)fghijklm" \ +] + +do_execsql_test 1.3 { + SELECT highlight(t1, 0, '(', ')') FROM t1('cdef'); +} [list \ + "ab(c\u0303def)ghijklm" \ + "a\u0303b\u0303(c\u0303def)ghijklm" \ +] + +do_execsql_test 1.4 { + SELECT highlight(t1, 0, '(', ')') FROM t1('def'); +} [list \ + "abc\u0303(def)ghijklm" \ + "a\u0303b\u0303c\u0303(def)ghijklm" \ +] + + +#------------------------------------------------------------------------- +do_catchsql_test 2.0 { + CREATE VIRTUAL TABLE t2 USING fts5( + z, tokenize='trigram case_sensitive 1 remove_diacritics 1' + ); +} {1 {error in tokenizer constructor}} + +do_execsql_test 2.1 { + CREATE VIRTUAL TABLE t2 USING fts5( + z, tokenize='trigram case_sensitive 0 remove_diacritics 1' + ); +} +do_execsql_test 2.2 " + INSERT INTO t2 VALUES('\u00E3bcdef'); + INSERT INTO t2 VALUES('b\u00E3cdef'); + INSERT INTO t2 VALUES('bc\u00E3def'); + INSERT INTO t2 VALUES('bcd\u00E3ef'); +" + +do_execsql_test 2.3 { + SELECT highlight(t2, 0, '(', ')') FROM t2('abc'); +} "(\u00E3bc)def" +do_execsql_test 2.4 { + SELECT highlight(t2, 0, '(', ')') FROM t2('bac'); +} "(b\u00E3c)def" +do_execsql_test 2.5 { + SELECT highlight(t2, 0, '(', ')') FROM t2('bca'); +} "(bc\u00E3)def" +do_execsql_test 2.6 " + SELECT highlight(t2, 0, '(', ')') FROM t2('\u00E3bc'); +" "(\u00E3bc)def" + +#------------------------------------------------------------------------- +do_execsql_test 3.0 { + CREATE VIRTUAL TABLE t3 USING fts5( + z, tokenize='trigram remove_diacritics 1' + ); +} {} +do_execsql_test 3.1 " + INSERT INTO t3 VALUES ('\u0303abc\u0303'); +" +do_execsql_test 3.2 { + SELECT highlight(t3, 0, '(', ')') FROM t3('abc'); +} "\u0303(abc\u0303)" + +#------------------------------------------------------------------------- +do_execsql_test 4.0 { + CREATE VIRTUAL TABLE t4 USING fts5(z, tokenize=trigram); +} {} + +do_execsql_test 4.1 { + INSERT INTO t4 VALUES('ABCD'); + INSERT INTO t4 VALUES('DEFG'); +} {} + +db close +sqlite3 db test.db + +do_eqp_test 4.1 { + SELECT rowid FROM t4 WHERE z LIKE '%abc%' +} {VIRTUAL TABLE INDEX 0:L0} + +do_execsql_test 4.2 { + SELECT rowid FROM t4 WHERE z LIKE '%abc%' +} {1} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 5.0 { + CREATE VIRTUAL TABLE t5 USING fts5( + c1, tokenize='trigram', detail='none' + ); + INSERT INTO t5(rowid, c1) VALUES(1, 'abc_____xyx_yxz'); + INSERT INTO t5(rowid, c1) VALUES(2, 'abc_____xyxz'); + INSERT INTO t5(rowid, c1) VALUES(3, 'ac_____xyxz'); +} {} +do_execsql_test 5.1 { + SELECT rowid FROM t5 WHERE c1 LIKE 'abc%xyxz' +} {2} + +finish_test diff --git a/ext/fts5/test/fts5ubsan.test b/ext/fts5/test/fts5ubsan.test new file mode 100644 index 0000000000..76382a1e15 --- /dev/null +++ b/ext/fts5/test/fts5ubsan.test @@ -0,0 +1,60 @@ +# 2022 August 9 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# This test is focused on edge cases that cause ubsan errors. +# + +source [file join [file dirname [info script]] fts5_common.tcl] +set testprefix fts5ubsan + +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. +ifcapable !fts5 { + finish_test + return +} + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE x1 USING fts5(x); +} + +set BIG 9000000000000000000 +set SMALL -9000000000000000000 + +do_execsql_test 1.1 { + BEGIN; + INSERT INTO x1 (rowid, x) VALUES($BIG, 'aaa aba acc'); + INSERT INTO x1 (rowid, x) VALUES($SMALL, 'aaa abc acb'); + COMMIT; +} + +do_execsql_test 1.2 { + SELECT rowid, x FROM x1('ab*'); +} [list $SMALL {aaa abc acb} $BIG {aaa aba acc}] + +do_execsql_test 1.3 { + SELECT rowid, x FROM x1('ac*'); +} [list $SMALL {aaa abc acb} $BIG {aaa aba acc}] + +reset_db +do_execsql_test 2.0 { + CREATE VIRTUAL TABLE x1 USING fts5(x); +} + +do_execsql_test 2.1 { + INSERT INTO x1 (rowid, x) VALUES($BIG, 'aaa aba acc'); + INSERT INTO x1 (rowid, x) VALUES($SMALL, 'aaa abc acb'); +} + +do_execsql_test 2.2 { + INSERT INTO x1 (x1) VALUES('optimize'); +} + +finish_test diff --git a/ext/fts5/test/fts5umlaut.test b/ext/fts5/test/fts5umlaut.test new file mode 100644 index 0000000000..fa948107a3 --- /dev/null +++ b/ext/fts5/test/fts5umlaut.test @@ -0,0 +1,65 @@ +# 2014 June 17 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# This file implements regression tests for SQLite library. The +# focus of this script is testing the FTS5 module. +# + +source [file join [file dirname [info script]] fts5_common.tcl] +set testprefix fts5umlaut + +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. +ifcapable !fts5 { + finish_test + return +} + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE t1 USING fts5(x); + CREATE VIRTUAL TABLE t2 USING fts5( + x, + tokenize="unicode61 remove_diacritics 2" + ); +} + +foreach {tn q res1 res2} { + 1 "Hà Nội" 0 1 + 2 "Hà Noi" 1 1 + 3 "Ha Noi" 1 1 + 4 "Ha N\u1ed9i" 0 1 + 5 "Ha N\u006fi" 1 1 + 6 "Ha N\u006f\u0302i" 1 1 + 7 "Ha N\u006f\u0323\u0302i" 1 1 +} { + do_execsql_test 1.$tn.1 { + DELETE FROM t1; + INSERT INTO t1(rowid, x) VALUES (1, 'Ha Noi'); + SELECT count(*) FROM t1($q) + } $res1 + do_execsql_test 1.$tn.2 { + DELETE FROM t1; + INSERT INTO t1(rowid, x) VALUES (1, $q); + SELECT count(*) FROM t1('Ha Noi') + } $res1 + + do_execsql_test 1.$tn.2 { + DELETE FROM t2; + INSERT INTO t2(rowid, x) VALUES (1, 'Ha Noi'); + SELECT count(*) FROM t2($q) + } $res2 + do_execsql_test 1.$tn.2 { + DELETE FROM t2; + INSERT INTO t2(rowid, x) VALUES (1, $q); + SELECT count(*) FROM t2('Ha Noi') + } $res2 +} + +finish_test + diff --git a/ext/fts5/test/fts5unicode.test b/ext/fts5/test/fts5unicode.test index 46f4c4f1aa..f10e0d02d8 100644 --- a/ext/fts5/test/fts5unicode.test +++ b/ext/fts5/test/fts5unicode.test @@ -15,7 +15,7 @@ source [file join [file dirname [info script]] fts5_common.tcl] set testprefix fts5unicode -# If SQLITE_ENABLE_FTS5 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return @@ -41,7 +41,6 @@ foreach {tn t} {1 ascii 2 unicode61} { #------------------------------------------------------------------------- # Check that "unicode61" really is the default tokenizer. # - do_execsql_test 2.0 " CREATE VIRTUAL TABLE t1 USING fts5(x); CREATE VIRTUAL TABLE t2 USING fts5(x, tokenize = unicode61); @@ -50,13 +49,38 @@ do_execsql_test 2.0 " INSERT INTO t2 VALUES('\xC0\xC8\xCC'); INSERT INTO t3 VALUES('\xC0\xC8\xCC'); " -breakpoint do_execsql_test 2.1 " SELECT 't1' FROM t1 WHERE t1 MATCH '\xE0\xE8\xEC'; SELECT 't2' FROM t2 WHERE t2 MATCH '\xE0\xE8\xEC'; SELECT 't3' FROM t3 WHERE t3 MATCH '\xE0\xE8\xEC'; " {t1 t2} +#------------------------------------------------------------------------- +# Check that codepoints that require 4 bytes to store in utf-8 (those that +# require 17 or more bits to store). +# + +unset -nocomplain A B C D +set A [db one {SELECT char(0x1F75E)}] ;# Type So +set B [db one {SELECT char(0x1F5FD)}] ;# Type So +set C [db one {SELECT char(0x2F802)}] ;# Type Lo +set D [db one {SELECT char(0x2F808)}] ;# Type Lo + +do_execsql_test 3.0 " + CREATE VIRTUAL TABLE xyz USING fts5(x, + tokenize = \"unicode61 separators '$C' tokenchars '$A'\" + ); + CREATE VIRTUAL TABLE xyz_v USING fts5vocab(xyz, row); + + INSERT INTO xyz VALUES('$A$B$C$D'); +" + +do_execsql_test 3.1 { + SELECT * FROM xyz_v; +} [list $A 1 1 $D 1 1] + -finish_test + + +finish_test diff --git a/ext/fts5/test/fts5unicode2.test b/ext/fts5/test/fts5unicode2.test index d3ff5128da..7a49a1d83f 100644 --- a/ext/fts5/test/fts5unicode2.test +++ b/ext/fts5/test/fts5unicode2.test @@ -17,7 +17,7 @@ source [file join [file dirname [info script]] fts5_common.tcl] set testprefix fts5unicode2 -# If SQLITE_ENABLE_FTS5 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return @@ -116,6 +116,7 @@ set docs [list { connected by OR. }] +unset -nocomplain map set map(a) [list "\u00C4" "\u00E4"] ; # LATIN LETTER A WITH DIAERESIS set map(e) [list "\u00CB" "\u00EB"] ; # LATIN LETTER E WITH DIAERESIS set map(i) [list "\u00CF" "\u00EF"] ; # LATIN LETTER I WITH DIAERESIS @@ -160,12 +161,12 @@ foreach {tn query snippet} { the maximum x value. } 4 "rollback" { - ...[ROLLBACK]. Instead, the pending statement - will return SQLITE_ABORT upon next access after the [ROLLBACK]. + Pending statements no longer block [ROLLBACK]. Instead, the pending + statement will return SQLITE_ABORT upon... } 5 "rOllback" { - ...[ROLLBACK]. Instead, the pending statement - will return SQLITE_ABORT upon next access after the [ROLLBACK]. + Pending statements no longer block [ROLLBACK]. Instead, the pending + statement will return SQLITE_ABORT upon... } 6 "lang*" { Added support for the FTS4 [languageid] option. @@ -281,7 +282,6 @@ do_test 4.4 { #------------------------------------------------------------------------- -breakpoint do_unicode_token_test3 5.1 {tokenchars {}} { sqlite3_reset sqlite3_column_int } { @@ -471,119 +471,23 @@ do_execsql_test 8.2.3 { } {2 4} #------------------------------------------------------------------------- -# -if 0 { -foreach {tn sql} { - 1 { - CREATE VIRTUAL TABLE t5 USING fts4(tokenize=unicode61 [tokenchars= .]); - CREATE VIRTUAL TABLE t6 USING fts4( - tokenize=unicode61 [tokenchars=="] "tokenchars=[]"); - CREATE VIRTUAL TABLE t7 USING fts4(tokenize=unicode61 [separators=x\xC4]); - } - 2 { - CREATE VIRTUAL TABLE t5 USING fts4(tokenize=unicode61 "tokenchars= ."); - CREATE VIRTUAL TABLE t6 USING fts4(tokenize=unicode61 "tokenchars=[=""]"); - CREATE VIRTUAL TABLE t7 USING fts4(tokenize=unicode61 "separators=x\xC4"); - } - 3 { - CREATE VIRTUAL TABLE t5 USING fts4(tokenize=unicode61 'tokenchars= .'); - CREATE VIRTUAL TABLE t6 USING fts4(tokenize=unicode61 'tokenchars=="[]'); - CREATE VIRTUAL TABLE t7 USING fts4(tokenize=unicode61 'separators=x\xC4'); - } - 4 { - CREATE VIRTUAL TABLE t5 USING fts4(tokenize=unicode61 `tokenchars= .`); - CREATE VIRTUAL TABLE t6 USING fts4(tokenize=unicode61 `tokenchars=[="]`); - CREATE VIRTUAL TABLE t7 USING fts4(tokenize=unicode61 `separators=x\xC4`); - } -} { - do_execsql_test 9.$tn.0 { - DROP TABLE IF EXISTS t5; - DROP TABLE IF EXISTS t5aux; - DROP TABLE IF EXISTS t6; - DROP TABLE IF EXISTS t6aux; - DROP TABLE IF EXISTS t7; - DROP TABLE IF EXISTS t7aux; - } - do_execsql_test 9.$tn.1 $sql - - do_execsql_test 9.$tn.2 { - CREATE VIRTUAL TABLE t5aux USING fts4aux(t5); - INSERT INTO t5 VALUES('one two three/four.five.six'); - SELECT * FROM t5aux; - } { - four.five.six * 1 1 four.five.six 0 1 1 - {one two three} * 1 1 {one two three} 0 1 1 - } - - do_execsql_test 9.$tn.3 { - CREATE VIRTUAL TABLE t6aux USING fts4aux(t6); - INSERT INTO t6 VALUES('alpha=beta"gamma/delta[epsilon]zeta'); - SELECT * FROM t6aux; - } { - {alpha=beta"gamma} * 1 1 {alpha=beta"gamma} 0 1 1 - {delta[epsilon]zeta} * 1 1 {delta[epsilon]zeta} 0 1 1 - } - - do_execsql_test 9.$tn.4 { - CREATE VIRTUAL TABLE t7aux USING fts4aux(t7); - INSERT INTO t7 VALUES('alephxbeth\xC4gimel'); - SELECT * FROM t7aux; - } { - aleph * 1 1 aleph 0 1 1 - beth * 1 1 beth 0 1 1 - gimel * 1 1 gimel 0 1 1 - } -} - -# Check that multiple options are handled correctly. -# -do_execsql_test 10.1 { - DROP TABLE IF EXISTS t1; - CREATE VIRTUAL TABLE t1 USING fts4(tokenize=unicode61 - "tokenchars=xyz" "tokenchars=.=" "separators=.=" "separators=xy" - "separators=a" "separators=a" "tokenchars=a" "tokenchars=a" - ); - - INSERT INTO t1 VALUES('oneatwoxthreeyfour'); - INSERT INTO t1 VALUES('a.single=word'); - CREATE VIRTUAL TABLE t1aux USING fts4aux(t1); - SELECT * FROM t1aux; -} { - .single=word * 1 1 .single=word 0 1 1 - four * 1 1 four 0 1 1 - one * 1 1 one 0 1 1 - three * 1 1 three 0 1 1 - two * 1 1 two 0 1 1 -} - -# Test that case folding happens after tokenization, not before. -# -do_execsql_test 10.2 { - DROP TABLE IF EXISTS t2; - CREATE VIRTUAL TABLE t2 USING fts4(tokenize=unicode61 "separators=aB"); - INSERT INTO t2 VALUES('oneatwoBthree'); - INSERT INTO t2 VALUES('onebtwoAthree'); - CREATE VIRTUAL TABLE t2aux USING fts4aux(t2); - SELECT * FROM t2aux; -} { - one * 1 1 one 0 1 1 - onebtwoathree * 1 1 onebtwoathree 0 1 1 - three * 1 1 three 0 1 1 - two * 1 1 two 0 1 1 -} -# Test that the tokenchars and separators options work with the -# fts3tokenize table. -# -do_execsql_test 11.1 { - CREATE VIRTUAL TABLE ft1 USING fts3tokenize( - "unicode61", "tokenchars=@.", "separators=1234567890" - ); - SELECT token FROM ft1 WHERE input = 'berlin@street123sydney.road'; +foreach {tn val bErr} { + 1 0 0 + 2 1 0 + 3 2 0 + 4 3 1 + 5 11 1 } { - berlin@street sydney.road -} - + reset_db + set aRes(0) {0 {}} + set aRes(1) {1 {error in tokenizer constructor}} + set res $aRes($bErr) + do_catchsql_test 9.1.$tn " + CREATE VIRTUAL TABLE bl USING fts5( + s, tokenize='trigram remove_diacritics $val' + ); + " $res } finish_test diff --git a/ext/fts5/test/fts5unicode3.test b/ext/fts5/test/fts5unicode3.test index 876ad27461..ddb61a9997 100644 --- a/ext/fts5/test/fts5unicode3.test +++ b/ext/fts5/test/fts5unicode3.test @@ -14,14 +14,14 @@ source [file join [file dirname [info script]] fts5_common.tcl] -# If SQLITE_ENABLE_FTS5 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return } proc fts3_unicode_path {file} { - file join [file dirname [info script]] .. .. fts3 unicode $file + file join .. [file dirname [info script]] .. .. fts3 unicode $file } source [fts3_unicode_path parseunicode.tcl] @@ -36,24 +36,26 @@ foreach x [an_load_unicodedata_text $UD] { } foreach {y} [rd_load_unicodedata_text $UD] { - foreach {code ascii} $y {} + foreach {code ascii f} $y {} if {$ascii==""} { set int 0 } else { binary scan $ascii c int } - set aDiacritic($code) $int + set aDiacritic($code,$f) $int + if {$f==0} { set aDiacritic($code,1) $int } } proc tcl_fold {i {bRemoveDiacritic 0}} { global tl_lookup_table global aDiacritic + set f [expr $bRemoveDiacritic==2] if {[info exists tl_lookup_table($i)]} { set i $tl_lookup_table($i) } - if {$bRemoveDiacritic && [info exists aDiacritic($i)]} { - set i $aDiacritic($i) + if {$bRemoveDiacritic && [info exists aDiacritic($i,$f)]} { + set i $aDiacritic($i,$f) } expr $i } @@ -85,7 +87,7 @@ do_execsql_test 1.1 { SELECT count(*), min(i) FROM ii WHERE fts5_fold(i)!=CAST(tcl_fold(i) AS int); } {0 {}} -do_execsql_test 1.2 { +do_execsql_test 1.2.1 { WITH ii(i) AS ( SELECT -1 UNION ALL @@ -95,6 +97,16 @@ do_execsql_test 1.2 { WHERE fts5_fold(i,1)!=CAST(tcl_fold(i,1) AS int); } {0 {}} +do_execsql_test 1.2.2 { + WITH ii(i) AS ( + SELECT -1 + UNION ALL + SELECT i+1 FROM ii WHERE i<100000 + ) + SELECT count(*), min(i) FROM ii + WHERE fts5_fold(i,2)!=CAST(tcl_fold(i,2) AS int); +} {0 {}} + do_execsql_test 1.3 { WITH ii(i) AS ( SELECT -1 @@ -126,4 +138,3 @@ do_test 1.5 { finish_test - diff --git a/ext/fts5/test/fts5unicode4.test b/ext/fts5/test/fts5unicode4.test new file mode 100644 index 0000000000..f006d6c0a6 --- /dev/null +++ b/ext/fts5/test/fts5unicode4.test @@ -0,0 +1,61 @@ +# 2018 July 25 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# + +source [file join [file dirname [info script]] fts5_common.tcl] +set testprefix fts5unicode4 + +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. +ifcapable !fts5 { + finish_test + return +} + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE sss USING fts5(a, prefix=3); +} + +do_execsql_test 1.1 { + INSERT INTO sss VALUES('まりや'); +} + +foreach {tn enc tok} { + 1 utf-8 ascii + 2 utf-16 ascii + 3 utf-8 unicode61 + 4 utf-16 unicode61 +} { + reset_db + + do_execsql_test 1.$tn.0 " + PRAGMA encoding = '$enc'; + CREATE VIRTUAL TABLE vt2 USING fts5(c0, c1, tokenize=$tok); + " + + do_execsql_test 1.$tn.1 { + INSERT INTO vt2(c0, c1) VALUES ('bhal', x'17db'); + } + + do_execsql_test 1.$tn.2 { + UPDATE vt2 SET c0='bhal'; + } + + do_execsql_test 1.$tn.3 { + INSERT INTO vt2(vt2) VALUES('integrity-check') + } + + do_execsql_test 1.$tn.4 { + SELECT quote(c1) FROM vt2 + } {X'17DB'} +} + +finish_test diff --git a/ext/fts5/test/fts5unindexed.test b/ext/fts5/test/fts5unindexed.test index 16d43f84c2..5099a89693 100644 --- a/ext/fts5/test/fts5unindexed.test +++ b/ext/fts5/test/fts5unindexed.test @@ -15,7 +15,7 @@ source [file join [file dirname [info script]] fts5_common.tcl] set testprefix fts5unindexed -# If SQLITE_ENABLE_FTS5 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return @@ -76,4 +76,3 @@ do_execsql_test 3.2 { finish_test - diff --git a/ext/fts5/test/fts5unindexed2.test b/ext/fts5/test/fts5unindexed2.test new file mode 100644 index 0000000000..c0abfc3980 --- /dev/null +++ b/ext/fts5/test/fts5unindexed2.test @@ -0,0 +1,297 @@ +# 2024 Sep 13 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# The tests in this file focus on "unindexed" columns in contentless +# tables. +# + +source [file join [file dirname [info script]] fts5_common.tcl] +set testprefix fts5unindexed2 + +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. +ifcapable !fts5 { + finish_test + return +} + + +do_execsql_test 1.1 { + CREATE VIRTUAL TABLE t1 USING fts5( + a, b UNINDEXED, content=, contentless_unindexed=1 + ); +} {} + +do_execsql_test 1.2 { + INSERT INTO t1 VALUES('abc def', 'ghi jkl'); +} + +do_execsql_test 1.3 { + SELECT rowid, a, b FROM t1 +} {1 {} {ghi jkl}} + +do_execsql_test 1.4 { + INSERT INTO t1(rowid, a, b) VALUES(11, 'hello world', 'one two three'); +} + +do_execsql_test 1.5 { + INSERT INTO t1(t1, rowid, a, b) VALUES('delete', 1, 'abc def', 'ghi jkl'); +} + +do_execsql_test 1.6 { + SELECT rowid, a, b FROM t1 +} { + 11 {} {one two three} +} + +do_execsql_test 1.7 { + PRAGMA integrity_check +} {ok} + +do_execsql_test 1.8 { + INSERT INTO t1(rowid, a, b) VALUES(12, 'abc def', 'ghi jkl'); +} + +do_execsql_test 1.9 { + SELECT rowid, a, b FROM t1('def') +} {12 {} {ghi jkl}} + +do_execsql_test 1.10 { + SELECT rowid, a, b FROM t1('def OR hello') ORDER BY rank +} {11 {} {one two three} 12 {} {ghi jkl}} + +do_execsql_test 1.11 { + SELECT rowid, a, b FROM t1 WHERE rowid=11 +} {11 {} {one two three}} + +do_execsql_test 1.12 { + SELECT rowid, a, b FROM t1 +} {11 {} {one two three} 12 {} {ghi jkl}} + + +fts5_aux_test_functions db +do_execsql_test 1.12.2 { + SELECT rowid, fts5_test_columntext(t1) FROM t1('def OR hello') +} {11 {{} {one two three}} 12 {{} {ghi jkl}}} + +do_execsql_test 1.13 { + INSERT INTO t1(t1) VALUES('delete-all'); +} + +do_execsql_test 1.14 { + SELECT rowid, a, b FROM t1 +} + +do_execsql_test 1.15 { + PRAGMA integrity_check +} {ok} + +do_execsql_test 2.0 { + CREATE VIRTUAL TABLE t4 USING fts5( + x, y UNINDEXED, z, columnsize=0, content='', contentless_unindexed=1 + ); +} + +do_execsql_test 2.1 { + INSERT INTO t4(rowid, x, y, z) VALUES(1, 'a a', 'b b b', 'c'); +} + +#------------------------------------------------------------------------- +reset_db + +do_execsql_test 3.0 { + CREATE VIRTUAL TABLE x1 USING fts5( + a UNINDEXED, b, c UNINDEXED, d, content=, contentless_delete=1, + contentless_unindexed=1 + ); +} + +do_execsql_test 3.1 { + INSERT INTO x1(rowid, a, b, c, d) VALUES(131, 'aaa', 'bbb', 'ccc', 'ddd'); +} + +do_execsql_test 3.2 { + SELECT * FROM x1 +} {aaa {} ccc {}} + +do_execsql_test 3.3 { + INSERT INTO x1(rowid, a, b, c, d) VALUES(1000, 'AAA', 'BBB', 'CCC', 'DDD'); +} + +do_execsql_test 3.4 { + SELECT rowid, * FROM x1 +} { + 131 aaa {} ccc {} + 1000 AAA {} CCC {} +} + +do_execsql_test 3.5 { + DELETE FROM x1 WHERE rowid=131; + SELECT rowid, * FROM x1 +} { + 1000 AAA {} CCC {} +} + +do_execsql_test 3.6 { + INSERT INTO x1(rowid, a, b, c, d) VALUES(112, 'aaa', 'bbb', 'ccc', 'ddd'); + SELECT rowid, * FROM x1 +} { + 112 aaa {} ccc {} + 1000 AAA {} CCC {} +} + +do_execsql_test 3.7 { + UPDATE x1 SET b='hello', d='world', rowid=1120 WHERE rowid=112 +} + +do_execsql_test 3.8 { + SELECT rowid, * FROM x1 +} { + 1000 AAA {} CCC {} + 1120 aaa {} ccc {} +} + +do_execsql_test 3.9 { + SELECT rowid, * FROM x1('hello'); +} { + 1120 aaa {} ccc {} +} + +do_execsql_test 3.9 { + SELECT rowid, * FROM x1('bbb'); +} { + 1000 AAA {} CCC {} +} + +fts5_aux_test_functions db +do_execsql_test 3.10 { + SELECT rowid, fts5_test_columntext(x1) FROM x1('b*') +} {1000 {AAA {} CCC {}}} + +#------------------------------------------------------------------------- +# Check that if contentless_unindexed=1 is not specified, the values +# of UNINDEXED columns are not stored in the database. +# +# Also check that contentless_unindexed=1 is not allowed unless the table +# is actually contentless. +# +reset_db +do_execsql_test 4.0 { + CREATE VIRTUAL TABLE ft USING fts5(a, b, c UNINDEXED, content=''); + INSERT INTO ft VALUES('one', 'two', 'three'); + SELECT rowid, * FROM ft; +} {1 {} {} {}} + +do_execsql_test 4.1 { + SELECT name FROM sqlite_schema ORDER BY 1 +} { + ft ft_config ft_data ft_docsize ft_idx +} + +do_catchsql_test 4.2 { + CREATE VIRTUAL TABLE ft2 USING fts5( + a, b, c UNINDEXED, contentless_unindexed=1 + ); +} {1 {contentless_unindexed=1 requires a contentless table}} + +do_catchsql_test 4.3 { + DELETE FROM ft WHERE rowid=1 +} {1 {cannot DELETE from contentless fts5 table: ft}} + +#------------------------------------------------------------------------- +# Check that the usual restrictions on contentless tables apply to +# contentless_unindexed=1 tables. +# +reset_db +do_execsql_test 5.0 { + CREATE VIRTUAL TABLE ft USING fts5( + a, b UNINDEXED, c, content='', contentless_unindexed=1 + ); + INSERT INTO ft VALUES('one', 'two', 'three'); + INSERT INTO ft VALUES('four', 'five', 'six'); + INSERT INTO ft VALUES('seven', 'eight', 'nine'); + SELECT rowid, * FROM ft; +} { + 1 {} two {} + 2 {} five {} + 3 {} eight {} +} + +do_execsql_test 5.1 { + PRAGMA integrity_check +} {ok} + +do_catchsql_test 5.2 { + DELETE FROM ft WHERE rowid=2 +} {1 {cannot DELETE from contentless fts5 table: ft}} + +do_execsql_test 5.3 { + SELECT rowid, * FROM ft('six') +} { + 2 {} five {} +} + +do_catchsql_test 5.4 { + UPDATE ft SET a='x', b='y', c='z' WHERE rowid=3 +} {1 {cannot UPDATE contentless fts5 table: ft}} + +fts5_aux_test_functions db + +do_execsql_test 5.5 { + SELECT fts5_test_columntext(ft) FROM ft WHERE rowid=3 +} { + {{} eight {}} +} +do_execsql_test 5.6 { + SELECT fts5_test_columntext(ft) FROM ft('three'); +} { + {{} two {}} +} + +#------------------------------------------------------------------------- +# Check that it is possible to UPDATE a contentless_unindexed=1 table +# if the only columns being modified are UNINDEXED. +# +# If the contentless_unindexed=1 table is also contentless_delete=1, then +# it is also possible to update indexed columns - but only if *all* indexed +# columns are updated. +# +reset_db +do_execsql_test 6.0 { + CREATE VIRTUAL TABLE ft1 USING fts5(a, b UNINDEXED, c UNINDEXED, d, + contentless_unindexed=1, content='' + ); + + INSERT INTO ft1(rowid, a, b, c, d) VALUES + (100, 'x y', 'b1', 'c1', 'a b'), + (200, 'c d', 'b2', 'c2', 'a b'), + (300, 'e f', 'b3', 'c3', 'a b'); +} + +do_execsql_test 6.1 { + UPDATE ft1 SET b='b1.1', c='c1.1' WHERE rowid=100; +} +do_execsql_test 6.2 { + UPDATE ft1 SET b='b2.1' WHERE rowid=200; +} +do_execsql_test 6.3 { + UPDATE ft1 SET c='c3.1' WHERE rowid=300; +} + +do_execsql_test 6.4 { + SELECT rowid, a, b, c, d FROM ft1 +} { + 100 {} b1.1 c1.1 {} + 200 {} b2.1 c2 {} + 300 {} b3 c3.1 {} +} + +finish_test + diff --git a/ext/fts5/test/fts5update.test b/ext/fts5/test/fts5update.test index 399c7ffb11..75598b47e5 100644 --- a/ext/fts5/test/fts5update.test +++ b/ext/fts5/test/fts5update.test @@ -115,7 +115,43 @@ do_execsql_test 2.2.integrity { INSERT INTO x2(x2) VALUES('integrity-check'); } +#------------------------------------------------------------------------- +# +do_execsql_test 3.0 { + CREATE VIRTUAL TABLE x3 USING fts5(x, detail=%DETAIL%); + INSERT INTO x3 VALUES('one'); + INSERT INTO x3 VALUES('two'); + INSERT INTO x3 VALUES('one'); + INSERT INTO x3 VALUES('two'); + INSERT INTO x3 VALUES('one'); } -finish_test +do_test 3.1 { + db eval { SELECT * FROM x3('one') } { + db eval { + INSERT INTO x3(x3) VALUES('optimize'); + } + } +} {} +do_execsql_test 4.0 { + CREATE VIRTUAL TABLE x4 USING fts5(a, detail=%DETAIL%); + INSERT INTO x4 VALUES('one two three'); + INSERT INTO x4(rowid, a) VALUES('2', 'one two three'); + INSERT INTO x4(rowid, a) VALUES('3.0', 'one two three'); +} +do_catchsql_test 4.1 { + INSERT INTO x4(rowid, a) VALUES('four', 'one two three'); +} {1 {datatype mismatch}} + +do_catchsql_test 4.2 { + UPDATE x4 SET rowid = 'four' WHERE rowid=1; +} {1 {datatype mismatch}} + + +} + +reset_db +do_catchsql_test 4.0 { CREATE VIRTUAL TABLE t1 USING fts5(a,b,c); } {0 {}} +do_catchsql_test 4.1 { DELETE FROM t1 WHERE t1 MATCH 'f*'; } {0 {}} +finish_test diff --git a/ext/fts5/test/fts5update2.test b/ext/fts5/test/fts5update2.test new file mode 100644 index 0000000000..d04af4800d --- /dev/null +++ b/ext/fts5/test/fts5update2.test @@ -0,0 +1,177 @@ +# 2024 Sep 27 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# This file implements regression tests for SQLite library. The +# focus of this script is testing the FTS5 module. +# + +source [file join [file dirname [info script]] fts5_common.tcl] +set testprefix fts5update2 + +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. +ifcapable !fts5 { + finish_test + return +} + + +#------------------------------------------------------------------------- +# Test that the various types of UPDATE statement are handled correctly +# by different table types. +# +foreach_detail_mode $testprefix { +foreach {tn cu} { + 1 0 + 2 1 +} { + reset_db + do_execsql_test 1.$tn.1 " + CREATE VIRTUAL TABLE ft1 USING fts5(a, b UNINDEXED, c UNINDEXED, d, + content='', + contentless_unindexed=$cu, + detail=%DETAIL% + ); + CREATE VIRTUAL TABLE ft2 USING fts5(a, b UNINDEXED, c UNINDEXED, d, + content='', + contentless_unindexed=$cu, contentless_delete=1, + detail=%DETAIL% + ); + " + + do_execsql_test 1.$tn.2 { + INSERT INTO ft1(rowid, a, b, c, d) VALUES(1, 'a1', 'b1', 'c1', 'd1'); + INSERT INTO ft1(rowid, a, b, c, d) VALUES(2, 'a2', 'b2', 'c2', 'd2'); + INSERT INTO ft1(rowid, a, b, c, d) VALUES(3, 'a3', 'b3', 'c3', 'd3'); + + INSERT INTO ft2(rowid, a, b, c, d) VALUES(1, 'a1', 'b1', 'c1', 'd1'); + INSERT INTO ft2(rowid, a, b, c, d) VALUES(2, 'a2', 'b2', 'c2', 'd2'); + INSERT INTO ft2(rowid, a, b, c, d) VALUES(3, 'a3', 'b3', 'c3', 'd3'); + } + + # It should be possible to update a subset of the UNINDEXED columns of + # a contentless table. Regardless of whether or not contentless_unindexed=1 + # or contentless_delete=1 is set. + do_execsql_test 1.$tn.3 { + UPDATE ft1 SET b=b||'.1'; + UPDATE ft2 SET b=b||'.1'; + } + do_execsql_test 1.$tn.4 { + UPDATE ft1 SET b=b||'.2', c=c||'.2'; + UPDATE ft2 SET b=b||'.2', c=c||'.2'; + } + + set res(0) { + 1 {} {} {} {} + 2 {} {} {} {} + 3 {} {} {} {} + } + set res(1) { + 1 {} b1.1.2 c1.2 {} + 2 {} b2.1.2 c2.2 {} + 3 {} b3.1.2 c3.2 {} + } + + do_execsql_test 1.$tn.5 { + SELECT rowid, * FROM ft2 + } $res($cu) + + do_execsql_test 1.6.1 { SELECT rowid FROM ft1('a2') } {2} + do_execsql_test 1.6.2 { SELECT rowid FROM ft2('a2') } {2} + + # It should be possible to update all indexed columns (but no other subset) + # if the contentless_delete=1 option is set, as it is for "ft2". + do_execsql_test 1.$tn.7 { + UPDATE ft2 SET a='a22', d='d22' WHERE rowid=2; + } + do_execsql_test 1.$tn.8 { SELECT rowid FROM ft2('a22 AND d22') } {2} + + do_execsql_test 1.$tn.9 { + UPDATE ft2 SET a='a33', d='d33', b='b3' WHERE rowid=3; + } + + set res(1) { + 1 {} b1.1.2 c1.2 {} + 2 {} b2.1.2 c2.2 {} + 3 {} b3 c3.2 {} + } + do_execsql_test 1.$tn.10 { + SELECT rowid, * FROM ft2 + } $res($cu) + + do_catchsql_test 1.$tn.11 { + UPDATE ft2 SET a='a11' WHERE rowid=1 + } {1 {cannot UPDATE a subset of columns on fts5 contentless-delete table: ft2}} + do_catchsql_test 1.$tn.12 { + UPDATE ft2 SET d='d11' WHERE rowid=1 + } {1 {cannot UPDATE a subset of columns on fts5 contentless-delete table: ft2}} + + # It is not possible to update the values of indexed columns if + # contentless_delete=1 is not set. + do_catchsql_test 1.$tn.13 { + UPDATE ft1 SET a='a11' WHERE rowid=1 + } {1 {cannot UPDATE contentless fts5 table: ft1}} + do_catchsql_test 1.$tn.14 { + UPDATE ft1 SET d='d11' WHERE rowid=1 + } {1 {cannot UPDATE contentless fts5 table: ft1}} + + # It should be possible to update the rowid if contentless_delete=1 is + # set and all indexed columns are updated. + do_execsql_test 1.$tn.15 { + UPDATE ft2 SET a='aXone', d='dXone', rowid=11 WHERE rowid=1 + } + + set res(0) { + 2 {} {} {} {} + 3 {} {} {} {} + 11 {} {} {} {} + } + set res(1) { + 2 {} b2.1.2 c2.2 {} + 3 {} b3 c3.2 {} + 11 {} b1.1.2 c1.2 {} + } + do_execsql_test 1.$tn.16 { + SELECT rowid, * FROM ft2 + } $res($cu) + + # Should not be possible to update the rowid of a contentless_delete=1 + # table if no indexed columns are updated. + do_catchsql_test 1.$tn.17 { + UPDATE ft2 SET rowid=12 WHERE rowid=11 + } {1 {cannot UPDATE a subset of columns on fts5 contentless-delete table: ft2}} + do_catchsql_test 1.$tn.18 { + UPDATE ft1 SET rowid=12 WHERE rowid=1 + } {1 {cannot UPDATE contentless fts5 table: ft1}} + + do_execsql_test 1.$tn.19 { + UPDATE ft2 SET a='aXtwo', d='dXtwo', c='newval', rowid=12 WHERE rowid=2 + } {} + + set res(0) { + 3 {} {} {} {} + 11 {} {} {} {} + 12 {} {} {} {} + } + set res(1) { + 3 {} b3 c3.2 {} + 11 {} b1.1.2 c1.2 {} + 12 {} b2.1.2 newval {} + } + do_execsql_test 1.$tn.20 { + SELECT rowid, * FROM ft2 + } $res($cu) + + do_execsql_test 1.$tn.21 { + SELECT rowid, * FROM ft2('aXtwo AND dXtwo') + } [lrange $res($cu) 10 end] + +}} ;# end of [foreach_detail_mode] loop + +finish_test diff --git a/ext/fts5/test/fts5version.test b/ext/fts5/test/fts5version.test index 7e4d74d114..58dd9fe14e 100644 --- a/ext/fts5/test/fts5version.test +++ b/ext/fts5/test/fts5version.test @@ -16,7 +16,7 @@ source [file join [file dirname [info script]] fts5_common.tcl] set testprefix fts5version -# If SQLITE_ENABLE_FTS5 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return @@ -36,28 +36,97 @@ do_execsql_test 1.3 { SELECT rowid FROM t1 WHERE t1 MATCH 'a'; } {1} +sqlite3_db_config db DEFENSIVE 0 do_execsql_test 1.4 { - UPDATE t1_config set v=5 WHERE k='version'; -} + UPDATE t1_config set v=6 WHERE k='version'; +} do_test 1.5 { db close sqlite3 db test.db catchsql { SELECT * FROM t1 WHERE t1 MATCH 'a' } -} {1 {invalid fts5 file format (found 5, expected 4) - run 'rebuild'}} +} {1 {invalid fts5 file format (found 6, expected 4 or 5) - run 'rebuild'}} do_test 1.6 { db close sqlite3 db test.db catchsql { INSERT INTO t1 VALUES('x y z') } -} {1 {invalid fts5 file format (found 5, expected 4) - run 'rebuild'}} +} {1 {invalid fts5 file format (found 6, expected 4 or 5) - run 'rebuild'}} do_test 1.7 { + sqlite3_db_config db DEFENSIVE 0 execsql { DELETE FROM t1_config WHERE k='version' } db close sqlite3 db test.db catchsql { SELECT * FROM t1 WHERE t1 MATCH 'a' } -} {1 {invalid fts5 file format (found 0, expected 4) - run 'rebuild'}} +} {1 {invalid fts5 file format (found 0, expected 4 or 5) - run 'rebuild'}} + +do_test 1.8 { + sqlite3_db_config db DEFENSIVE 0 + execsql { INSERT INTO t1_config VALUES('version', 4) } + execsql { INSERT INTO t1(t1, rank) VALUES('secure-delete', 1) } +} {} + +do_execsql_test 1.10 { + SELECT * FROM t1_config +} {secure-delete 1 version 4} + +do_execsql_test 1.11 { + INSERT INTO t1(rowid, one) VALUES(123, 'one two three'); + DELETE FROM t1 WHERE rowid=123; + SELECT * FROM t1_config +} {secure-delete 1 version 5} + +do_execsql_test 1.11 { + INSERT INTO t1(t1) VALUES('rebuild'); + SELECT * FROM t1_config +} {secure-delete 1 version 4} + +do_execsql_test 1.12 { + SELECT * FROM t1_config +} {secure-delete 1 version 4} + +#------------------------------------------------------------------------- +reset_db + +do_execsql_test 2.0 { + CREATE VIRTUAL TABLE xyz USING fts5(x); + INSERT INTO xyz(rowid, x) VALUES + (1, 'one document'), + (2, 'two document'), + (3, 'three document'), + (4, 'four document'), + (5, 'five document'), + (6, 'six document'); + + INSERT INTO xyz(xyz, rank) VALUES('secure-delete', 1); + SELECT v FROM xyz_config WHERE k='version'; +} {4} + +do_execsql_test 2.1 { + BEGIN; + INSERT INTO xyz(rowid, x) VALUES(7, 'seven document'); + SAVEPOINT one; + DELETE FROM xyz WHERE rowid = 4; +} + +do_execsql_test 2.2 { + SELECT v FROM xyz_config WHERE k='version'; +} {4} + +do_execsql_test 2.3 { + ROLLBACK TO one; + SELECT v FROM xyz_config WHERE k='version'; +} {4} + + +do_execsql_test 2.4 { + DELETE FROM xyz WHERE rowid = 3; + COMMIT; + SELECT v FROM xyz_config WHERE k='version'; +} {5} + + finish_test diff --git a/ext/fts5/test/fts5vocab.test b/ext/fts5/test/fts5vocab.test index f7278dd5c1..b1644527ea 100644 --- a/ext/fts5/test/fts5vocab.test +++ b/ext/fts5/test/fts5vocab.test @@ -15,7 +15,7 @@ source [file join [file dirname [info script]] fts5_common.tcl] set testprefix fts5vocab -# If SQLITE_ENABLE_FTS5 is defined, omit this file. +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. ifcapable !fts5 { finish_test return @@ -79,8 +79,8 @@ do_execsql_test 1.1.2 { 3 cnt {} 0 {} 0 } -do_execsql_test 1.2.1 { SELECT * FROM v1 } { } -do_execsql_test 1.2.2 { SELECT * FROM v2 } { } +do_execsql_test 1.2.1 { SELECT * FROM v1 } {} +do_execsql_test 1.2.2 { SELECT * FROM v2 } {} do_execsql_test 1.3 { INSERT INTO t1 VALUES('x y z'); @@ -210,7 +210,6 @@ do_execsql_test 5.0 { INSERT INTO aux.t1 VALUES('x n z'); } -breakpoint do_execsql_test 5.1 { CREATE VIRTUAL TABLE temp.vm USING fts5vocab(main, t1, row); CREATE VIRTUAL TABLE temp.vt1 USING fts5vocab(t1, row); @@ -421,6 +420,7 @@ if {[detail_is_none]} { set resc [row_to_col $resr] } do_execsql_test 8.1.1 { SELECT * FROM x1_r; } $resr do_execsql_test 8.1.2 { SELECT * FROM x1_c } $resc +sqlite3_db_config db DEFENSIVE 0 do_execsql_test 8.2 { PRAGMA writable_schema = 1; UPDATE sqlite_master @@ -442,8 +442,117 @@ if {[detail_is_none]} { } sqlite3_fts5_may_be_corrupt 0 +} +#------------------------------------------------------------------------- +# Test that both "ORDER BY term" and "ORDER BY term DESC" work. +# +reset_db +do_execsql_test 9.1 { + CREATE VIRTUAL TABLE x1 USING fts5(x); + INSERT INTO x1 VALUES('def ABC ghi'); + INSERT INTO x1 VALUES('DEF abc GHI'); } -finish_test +do_execsql_test 9.2 { + CREATE VIRTUAL TABLE rrr USING fts5vocab(x1, row); + SELECT * FROM rrr +} { + abc 2 2 def 2 2 ghi 2 2 +} +do_execsql_test 9.3 { + SELECT * FROM rrr ORDER BY term ASC +} { + abc 2 2 def 2 2 ghi 2 2 +} +do_execsql_test 9.4 { + SELECT * FROM rrr ORDER BY term DESC +} { + ghi 2 2 def 2 2 abc 2 2 +} +do_test 9.5 { + set e2 [db eval { EXPLAIN SELECT * FROM rrr ORDER BY term ASC }] + expr [lsearch $e2 SorterSort]<0 +} 1 +do_test 9.6 { + set e2 [db eval { EXPLAIN SELECT * FROM rrr ORDER BY term DESC }] + expr [lsearch $e2 SorterSort]<0 +} 0 + +#------------------------------------------------------------------------- +do_execsql_test 10.0 { + CREATE VIRTUAL TABLE ft USING fts5(a, b, c); + CREATE VIRTUAL TABLE t2 USING fts5vocab('ft','row'); + CREATE VIRTUAL TABLE t3 USING fts5vocab('ft','row'); +} + +do_execsql_test 10.1 { + BEGIN; + INSERT INTO ft(b) VALUES('x y'); +} + +do_execsql_test 10.2 { + SELECT t2.term FROM t2; +} {x y} + +do_execsql_test 10.3 { + SELECT t2.term, t3.term FROM t2, t3; +} {x x x y y x y y} +do_execsql_test 10.4 { + COMMIT; +} + +do_execsql_test 10.5 { + BEGIN; + INSERT INTO ft(a) VALUES('1 2 3'); + INSERT INTO ft(a) VALUES('4 5 6'); + INSERT INTO ft(a) VALUES('1 2 3'); + INSERT INTO ft(a) VALUES('4 5 6'); + INSERT INTO ft(a) VALUES('1 2 3'); + INSERT INTO ft(a) VALUES('4 5 6'); +} + +unset -nocomplain x res +do_test 10.6 { + set res [list] + db eval { SELECT rowid FROM ft('4') } x { + db eval { SELECT * FROM t2 } + lappend res $x(rowid) + } + db eval COMMIT + set res +} {3 5 7} + +do_execsql_test 10.6.1 { + SELECT * FROM t2 WHERE termNULL; +} +do_execsql_test 10.6.3 { + SELECT * FROM t2 WHERE term=NULL; +} +do_execsql_test 10.7.1 { + SELECT * FROM t2 WHERE term?; +} +do_execsql_test 10.7.3 { + SELECT * FROM t2 WHERE term=?; +} + +# 2020-02-16 Detect recursively define fts5vocab() tables. +# Error found by dbsqlfuzz. +# +reset_db +do_execsql_test 11.100 { + CREATE VIRTUAL TABLE t3 USING fts5vocab(rowid , 'col'); + CREATE VIRTUAL TABLE rowid USING fts5vocab(rowid , 'instance'); +} {} +do_catchsql_test 11.110 { + SELECT rowid+1,rowid, * FROM t3 WHERE null>rowid ; +} {1 {SQL logic error}} + +finish_test diff --git a/ext/fts5/test/fts5vocab2.test b/ext/fts5/test/fts5vocab2.test new file mode 100644 index 0000000000..58416a7e90 --- /dev/null +++ b/ext/fts5/test/fts5vocab2.test @@ -0,0 +1,332 @@ +# 2017 August 10 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# The tests in this file focus on testing the fts5vocab module. +# + +source [file join [file dirname [info script]] fts5_common.tcl] +set testprefix fts5vocab2 + +# If SQLITE_ENABLE_FTS5 is not defined, omit this file. +ifcapable !fts5 { + finish_test + return +} + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE t1 USING fts5(a, b); + CREATE VIRTUAL TABLE v1 USING fts5vocab(t1, instance); + + INSERT INTO t1 VALUES('one two', 'two three'); + INSERT INTO t1 VALUES('three four', 'four five five five'); +} + +do_execsql_test 1.1 { + SELECT * FROM v1; +} { + five 2 b 1 + five 2 b 2 + five 2 b 3 + four 2 a 1 + four 2 b 0 + one 1 a 0 + three 1 b 1 + three 2 a 0 + two 1 a 1 + two 1 b 0 +} + +do_execsql_test 1.2 { + SELECT * FROM v1 WHERE term='three'; +} { + three 1 b 1 + three 2 a 0 +} + +do_execsql_test 1.3 { + BEGIN; + DELETE FROM t1 WHERE rowid=2; + SELECT * FROM v1; + ROLLBACK; +} { + one 1 a 0 + three 1 b 1 + two 1 a 1 + two 1 b 0 +} + +do_execsql_test 1.4 { + BEGIN; + DELETE FROM t1 WHERE rowid=1; + SELECT * FROM v1; + ROLLBACK; +} { + five 2 b 1 + five 2 b 2 + five 2 b 3 + four 2 a 1 + four 2 b 0 + three 2 a 0 +} + +do_execsql_test 1.5 { + DELETE FROM t1; + SELECT * FROM v1; +} {} + +#------------------------------------------------------------------------- +# +do_execsql_test 2.0 { + DROP TABLE IF EXISTS t1; + DROP TABLE IF EXISTS v1; + + CREATE VIRTUAL TABLE t1 USING fts5(a, b, detail=column); + CREATE VIRTUAL TABLE v1 USING fts5vocab(t1, instance); + + INSERT INTO t1 VALUES('one two', 'two three'); + INSERT INTO t1 VALUES('three four', 'four five five five'); +} + +do_execsql_test 2.1 { + SELECT * FROM v1; +} { + five 2 b {} + four 2 a {} + four 2 b {} + one 1 a {} + three 1 b {} + three 2 a {} + two 1 a {} + two 1 b {} +} + +do_execsql_test 2.2 { + SELECT * FROM v1 WHERE term='three'; +} { + three 1 b {} + three 2 a {} +} + +do_execsql_test 2.3 { + BEGIN; + DELETE FROM t1 WHERE rowid=2; + SELECT * FROM v1; + ROLLBACK; +} { + one 1 a {} + three 1 b {} + two 1 a {} + two 1 b {} +} + +do_execsql_test 2.4 { + BEGIN; + DELETE FROM t1 WHERE rowid=1; + SELECT * FROM v1; + ROLLBACK; +} { + five 2 b {} + four 2 a {} + four 2 b {} + three 2 a {} +} + +do_execsql_test 2.5 { + DELETE FROM t1; + SELECT * FROM v1; +} {} + +#------------------------------------------------------------------------- +# +do_execsql_test 3.0 { + DROP TABLE IF EXISTS t1; + DROP TABLE IF EXISTS v1; + + CREATE VIRTUAL TABLE t1 USING fts5(a, b, detail=none); + CREATE VIRTUAL TABLE v1 USING fts5vocab(t1, instance); + + INSERT INTO t1 VALUES('one two', 'two three'); + INSERT INTO t1 VALUES('three four', 'four five five five'); +} + +do_execsql_test 3.1 { + SELECT * FROM v1; +} { + five 2 {} {} + four 2 {} {} + one 1 {} {} + three 1 {} {} + three 2 {} {} + two 1 {} {} +} + +do_execsql_test 3.2 { + SELECT * FROM v1 WHERE term='three'; +} { + three 1 {} {} + three 2 {} {} +} + +do_execsql_test 3.3 { + BEGIN; + DELETE FROM t1 WHERE rowid=2; + SELECT * FROM v1; + ROLLBACK; +} { + one 1 {} {} + three 1 {} {} + two 1 {} {} +} + +do_execsql_test 3.4 { + BEGIN; + DELETE FROM t1 WHERE rowid=1; + SELECT * FROM v1; + ROLLBACK; +} { + five 2 {} {} + four 2 {} {} + three 2 {} {} +} + +do_execsql_test 3.5 { + DELETE FROM t1; + SELECT * FROM v1; +} {} + +#------------------------------------------------------------------------- +# +reset_db +do_execsql_test 4.0 { + CREATE VIRTUAL TABLE v1 USING fts5vocab(nosuchtable, col); +} + +do_catchsql_test 4.1 { + SELECT * FROM v1 WHERE term=='nosuchterm'; +} {1 {no such fts5 table: main.nosuchtable}} + +do_execsql_test 4.2.1 { + CREATE TABLE nosuchtable(nosuchtable, y, z); +} +do_catchsql_test 4.2.2 { + SELECT * FROM v1 WHERE term=='nosuchterm'; +} {1 {no such fts5 table: main.nosuchtable}} + +ifcapable fts3 { + do_execsql_test 4.3.1 { + DROP TABLE nosuchtable; + CREATE VIRTUAL TABLE nosuchtable USING fts3(a, b); + } {} + do_catchsql_test 4.3.2 { + SELECT * FROM v1 WHERE term=='nosuchterm'; + } {1 {no such fts5 table: main.nosuchtable}} + do_catchsql_test 4.3.3 { + INSERT INTO nosuchtable VALUES('id', '*id'); + SELECT * FROM v1 WHERE term=='nosuchterm'; + } {1 {no such fts5 table: main.nosuchtable}} +} + +#------------------------------------------------------------------------- +# Check that the fts5 table cannot be written while there are vocab +# cursors open. +reset_db +do_execsql_test 5.0 { + CREATE VIRTUAL TABLE t1 USING fts5(a); + CREATE VIRTUAL TABLE v1 USING fts5vocab(t1, instance); + INSERT INTO t1 VALUES('one'), ('two'), ('three'), ('four'); +} + +do_test 5.1 { + list [catch { + db eval { SELECT * FROM v1 } { + db eval {INSERT INTO t1 VALUES('five')} + } + } msg] $msg +} {1 {query aborted}} + +do_execsql_test 5.2 { + SELECT * FROM t1 +} {one two three four five} + +#------------------------------------------------------------------------- +# Check that the fts5 table cannot be written while there are vocab +# cursors open. +reset_db +do_execsql_test 5.0 { + CREATE VIRTUAL TABLE t1 USING fts5(a); + CREATE VIRTUAL TABLE v1 USING fts5vocab(t1, instance); + WITH s(i) AS ( + VALUES(1) UNION ALL SELECT i+1 FROM s WHERE i<10000 + ) + INSERT INTO t1 SELECT + 'State Emergency Service (SES), Rural Fire Service (RFS) and Volunteers' + FROM s; +} + +do_catchsql_test 5.1 { + INSERT INTO t1 SELECT rowid FROM v1 +} {1 {query aborted}} + +do_catchsql_test 5.2 { + DELETE FROM t1 WHERE rowid>100; + INSERT INTO t1 SELECT randomblob(3000) FROM v1 +} {1 {query aborted}} + +#------------------------------------------------------------------------- +reset_db +sqlite3_fts5_may_be_corrupt 1 + +do_execsql_test 6.0 { + BEGIN TRANSACTION; + CREATE VIRTUAL TABLE t1 USING fts5(a,b unindexed,c,tokenize="porter ascii",tokendata=1); + REPLACE INTO t1_data VALUES(1,X'03090009'); + REPLACE INTO t1_data VALUES(10,X'000000000103030003010101020101030101'); + REPLACE INTO t1_data VALUES(137438953473,X'0000002e023061010202010162010203010163010204010167010601020201016801060102030101690106010204040606060808'); + REPLACE INTO t1_data VALUES(274877906945,X'0000001f013067020802010202010168020803010203010169020804010204040909'); + REPLACE INTO t1_data VALUES(412316860417,X'0000002e023061030202010162030203010163030204010167030601020201016803060102030101690306010204040606060808'); + COMMIT; +} + +do_execsql_test 6.1 { + CREATE VIRTUAL TABLE t3 USING fts5vocab('t1', 'row'); +} + +do_catchsql_test 6.2 { + SELECT * FROM t3; +} {1 {database disk image is malformed}} + +sqlite3_fts5_may_be_corrupt 0 + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 7.0 { + CREATE VIRTUAL TABLE t1 USING fts5(a, b); + CREATE VIRTUAL TABLE v1 USING fts5vocab(t1, col); + + INSERT INTO t1 VALUES('xx', 'xx'); + + CREATE TABLE x1(t); + INSERT INTO x1 VALUES('xx'); + INSERT INTO x1 VALUES('xx'); + + SELECT term, col FROM v1; +} { + xx a xx b +} + +do_execsql_test 7.1 { + SELECT * FROM x1 WHERE 'a'=(SELECT col FROM v1 WHERE term=t) +} {xx xx} + + +finish_test + + diff --git a/ext/fts5/tool/fts5txt2db.tcl b/ext/fts5/tool/fts5txt2db.tcl index d5df971d4d..1996b2c313 100644 --- a/ext/fts5/tool/fts5txt2db.tcl +++ b/ext/fts5/tool/fts5txt2db.tcl @@ -12,11 +12,13 @@ proc process_cmdline {} { cmdline::process ::A $::argv { {fts5 "use fts5 (this is the default)"} {fts4 "use fts4"} + {trigram "Use tokenize=trigram"} {colsize "10 10 10" "list of column sizes"} {tblname "t1" "table name to create"} {detail "full" "Fts5 detail mode to use"} {repeat 1 "Load each file this many times"} {prefix "" "Fts prefix= option"} + {trans 1 "True to use a transaction"} database file... } { @@ -174,6 +176,7 @@ proc create_table {} { set sql "CREATE VIRTUAL TABLE IF NOT EXISTS $A(tblname) USING $A(fts) (" append sql [join $cols ,] if {$A(fts)=="fts5"} { append sql ",detail=$A(detail)" } + if {$A(trigram)} { append sql ",tokenize=trigram" } append sql ", prefix='$A(prefix)');" db eval $sql @@ -214,7 +217,7 @@ foreach c [lrange $cols 1 end] { } append sql ")" -db eval BEGIN +if {$A(trans)} { db eval BEGIN } while {$i < $N} { foreach c $cols s $A(colsize) { set R($c) [lrange $tokens $i [expr $i+$s-1]] @@ -222,7 +225,7 @@ db eval BEGIN } db eval $sql } -db eval COMMIT +if {$A(trans)} { db eval COMMIT } diff --git a/ext/fts5/tool/mkfts5c.tcl b/ext/fts5/tool/mkfts5c.tcl index 797811d46e..6f20a0cd73 100644 --- a/ext/fts5/tool/mkfts5c.tcl +++ b/ext/fts5/tool/mkfts5c.tcl @@ -2,7 +2,7 @@ # restart with tclsh \ exec tclsh "$0" "$@" -set srcdir [file dirname [file dirname [info script]]] +set srcdir [file dirname [file dirname [file normalize [info script]]]] set G(src) [string map [list %dir% $srcdir] { %dir%/fts5.h %dir%/fts5Int.h @@ -23,7 +23,27 @@ set G(src) [string map [list %dir% $srcdir] { }] set G(hdr) { - +/* +** This, the "fts5.c" source file, is a composite file that is itself +** assembled from the following files: +** +** fts5.h +** fts5Int.h +** fts5parse.h <--- Generated from fts5parse.y by Lemon +** fts5parse.c <--- Generated from fts5parse.y by Lemon +** fts5_aux.c +** fts5_buffer.c +** fts5_config.c +** fts5_expr.c +** fts5_hash.c +** fts5_index.c +** fts5_main.c +** fts5_storage.c +** fts5_tokenize.c +** fts5_unicode2.c +** fts5_varint.c +** fts5_vocab.c +*/ #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS5) #if !defined(NDEBUG) && !defined(SQLITE_DEBUG) @@ -33,10 +53,16 @@ set G(hdr) { # undef NDEBUG #endif +#ifdef HAVE_STDINT_H +#include +#endif +#ifdef HAVE_INTTYPES_H +#include +#endif } set G(footer) { - +/* Here ends the fts5.c composite file. */ #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS5) */ } @@ -60,7 +86,8 @@ proc fts5_source_id {zDir} { set L [split [readfile [file join $top manifest]]] set date [lindex $L [expr [lsearch -exact $L D]+1]] - set date [string range $date 0 [string last . $date]-1] + set idx [expr {[string last . $date]-1}] + set date [string range $date 0 $idx] set date [string map {T { }} $date] return "fts5: $date $uuid" diff --git a/ext/icu/README.txt b/ext/icu/README.txt index d744f74981..40def24662 100644 --- a/ext/icu/README.txt +++ b/ext/icu/README.txt @@ -1,19 +1,18 @@ - This directory contains source code for the SQLite "ICU" extension, an integration of the "International Components for Unicode" library with SQLite. Documentation follows. 1. Features - + 1.1 SQL Scalars upper() and lower() 1.2 Unicode Aware LIKE Operator 1.3 ICU Collation Sequences 1.4 SQL REGEXP Operator - + 2. Compilation and Usage - + 3. Bugs, Problems and Security Issues - + 3.1 The "case_sensitive_like" Pragma 3.2 The SQLITE_MAX_LIKE_PATTERN_LENGTH Macro 3.3 Collation Sequence Security Issue @@ -23,10 +22,10 @@ SQLite. Documentation follows. 1.1 SQL Scalars upper() and lower() - SQLite's built-in implementations of these two functions only + SQLite's built-in implementations of these two functions only provide case mapping for the 26 letters used in the English language. The ICU based functions provided by this extension - provide case mapping, where defined, for the full range of + provide case mapping, where defined, for the full range of unicode characters. ICU provides two types of case mapping, "general" case mapping and @@ -36,11 +35,11 @@ SQLite. Documentation follows. http://www.icu-project.org/userguide/caseMappings.html http://www.icu-project.org/userguide/posix.html#case_mappings - To utilise "general" case mapping, the upper() or lower() scalar + To utilise "general" case mapping, the upper() or lower() scalar functions are invoked with one argument: - upper('ABC') -> 'abc' - lower('abc') -> 'ABC' + upper('abc') -> 'ABC' + lower('ABC') -> 'abc' To access ICU "language specific" case mapping, upper() or lower() should be invoked with two arguments. The second argument is the name @@ -57,7 +56,7 @@ SQLite. Documentation follows. operator understands case equivalence for the 26 letters of the English language alphabet. The implementation of LIKE included in this extension uses the ICU function u_foldCase() to provide case - independent comparisons for the full range of unicode characters. + independent comparisons for the full range of unicode characters. The U_FOLD_CASE_DEFAULT flag is passed to u_foldCase(), meaning the dotless 'I' character used in the Turkish language is considered @@ -66,9 +65,9 @@ SQLite. Documentation follows. 1.3 ICU Collation Sequences - A special SQL scalar function, icu_load_collation() is provided that + A special SQL scalar function, icu_load_collation() is provided that may be used to register ICU collation sequences with SQLite. It - is always called with exactly two arguments, the ICU locale + is always called with exactly two arguments, the ICU locale identifying the collation sequence to ICU, and the name of the SQLite collation sequence to create. For example, to create an SQLite collation sequence named "turkish" using Turkish language @@ -87,7 +86,7 @@ SQLite. Documentation follows. australian_penpal_name TEXT COLLATE australian, turkish_penpal_name TEXT COLLATE turkish ); - + 1.4 SQL REGEXP Operator This extension provides an implementation of the SQL binary @@ -116,13 +115,19 @@ SQLite. Documentation follows. and use it as a dynamically loadable SQLite extension. To do this using gcc on *nix: - gcc -shared icu.c `icu-config --ldflags` -o libSqliteIcu.so + gcc -fPIC -shared icu.c `pkg-config --libs --cflags icu-io` \ + -o libSqliteIcu.so You may need to add "-I" flags so that gcc can find sqlite3ext.h and sqlite3.h. The resulting shared lib, libSqliteIcu.so, may be loaded into sqlite in the same way as any other dynamically loadable extension. + As of version 3.48, it can be enabled in the canonical build process + by passing one of --with-icu-config or --with-icu-ldflags to the + configure script, optionally together with --enable-icu-collations. + See the configure --help for more details. + 3 BUGS, PROBLEMS AND SECURITY ISSUES @@ -143,27 +148,21 @@ SQLite. Documentation follows. SQLITE_MAX_LIKE_PATTERN_LENGTH macro as the maximum length of a pattern in bytes (irrespective of encoding). The default value is defined in internal header file "limits.h". - - The ICU extension LIKE implementation suffers from the same + + The ICU extension LIKE implementation suffers from the same problem and uses the same solution. However, since the ICU extension code does not include the SQLite file "limits.h", modifying the default value therein does not affect the ICU extension. The default value of SQLITE_MAX_LIKE_PATTERN_LENGTH used by - the ICU extension LIKE operator is 50000, defined in source + the ICU extension LIKE operator is 50000, defined in source file "icu.c". - 3.3 Collation Sequence Security Issue + 3.3 Collation Sequence Security Internally, SQLite assumes that indices stored in database files are sorted according to the collation sequence indicated by the SQL schema. Changing the definition of a collation sequence after an index has been built is therefore equivalent to database - corruption. The SQLite library is not very well tested under - these conditions, and may contain potential buffer overruns - or other programming errors that could be exploited by a malicious - programmer. - - If the ICU extension is used in an environment where potentially - malicious users may execute arbitrary SQL (i.e. gears), they - should be prevented from invoking the icu_load_collation() function, - possibly using the authorisation callback. + corruption. The SQLite library is well tested for robustness in + the fact of database corruption. Database corruption may well + lead to incorrect answers, but should not cause memory errors. diff --git a/ext/icu/icu.c b/ext/icu/icu.c index a2ff49274c..50110072b5 100644 --- a/ext/icu/icu.c +++ b/ext/icu/icu.c @@ -28,7 +28,9 @@ ** provide case-independent matching. */ -#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU) +#if !defined(SQLITE_CORE) \ + || defined(SQLITE_ENABLE_ICU) \ + || defined(SQLITE_ENABLE_ICU_COLLATIONS) /* Include ICU headers */ #include @@ -45,6 +47,26 @@ #include "sqlite3.h" #endif +/* +** This function is called when an ICU function called from within +** the implementation of an SQL scalar function returns an error. +** +** The scalar function context passed as the first argument is +** loaded with an error message based on the following two args. +*/ +static void icuFunctionError( + sqlite3_context *pCtx, /* SQLite scalar function context */ + const char *zName, /* Name of ICU function that failed */ + UErrorCode e /* Error code returned by ICU function */ +){ + char zBuf[128]; + sqlite3_snprintf(128, zBuf, "ICU error: %s(): %s", zName, u_errorName(e)); + zBuf[127] = '\0'; + sqlite3_result_error(pCtx, zBuf, -1); +} + +#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU) + /* ** Maximum length (in bytes) of the pattern in a LIKE or GLOB ** operator. @@ -60,6 +82,38 @@ static void xFree(void *p){ sqlite3_free(p); } +/* +** This lookup table is used to help decode the first byte of +** a multi-byte UTF8 character. It is copied here from SQLite source +** code file utf8.c. +*/ +static const unsigned char icuUtf8Trans1[] = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00, +}; + +#define SQLITE_ICU_READ_UTF8(zIn, c) \ + c = *(zIn++); \ + if( c>=0xc0 ){ \ + c = icuUtf8Trans1[c-0xc0]; \ + while( (*zIn & 0xc0)==0x80 ){ \ + c = (c<<6) + (0x3f & *(zIn++)); \ + } \ + } + +#define SQLITE_ICU_SKIP_UTF8(zIn) \ + assert( *zIn ); \ + if( *(zIn++)>=0xc0 ){ \ + while( (*zIn & 0xc0)==0x80 ){zIn++;} \ + } + + /* ** Compare two UTF-8 strings for equality where the first string is ** a "LIKE" expression. Return true (1) if they are the same and @@ -70,19 +124,17 @@ static int icuLikeCompare( const uint8_t *zString, /* The UTF-8 string to compare against */ const UChar32 uEsc /* The escape character */ ){ - static const int MATCH_ONE = (UChar32)'_'; - static const int MATCH_ALL = (UChar32)'%'; - - int iPattern = 0; /* Current byte index in zPattern */ - int iString = 0; /* Current byte index in zString */ + static const uint32_t MATCH_ONE = (uint32_t)'_'; + static const uint32_t MATCH_ALL = (uint32_t)'%'; int prevEscape = 0; /* True if the previous character was uEsc */ - while( zPattern[iPattern]!=0 ){ + while( 1 ){ /* Read (and consume) the next character from the input pattern. */ - UChar32 uPattern; - U8_NEXT_UNSAFE(zPattern, iPattern, uPattern); + uint32_t uPattern; + SQLITE_ICU_READ_UTF8(zPattern, uPattern); + if( uPattern==0 ) break; /* There are now 4 possibilities: ** @@ -91,7 +143,7 @@ static int icuLikeCompare( ** 3. uPattern is an unescaped escape character, or ** 4. uPattern is to be handled as an ordinary character */ - if( !prevEscape && uPattern==MATCH_ALL ){ + if( uPattern==MATCH_ALL && !prevEscape && uPattern!=(uint32_t)uEsc ){ /* Case 1. */ uint8_t c; @@ -99,39 +151,39 @@ static int icuLikeCompare( ** MATCH_ALL. For each MATCH_ONE, skip one character in the ** test string. */ - while( (c=zPattern[iPattern]) == MATCH_ALL || c == MATCH_ONE ){ + while( (c=*zPattern) == MATCH_ALL || c == MATCH_ONE ){ if( c==MATCH_ONE ){ - if( zString[iString]==0 ) return 0; - U8_FWD_1_UNSAFE(zString, iString); + if( *zString==0 ) return 0; + SQLITE_ICU_SKIP_UTF8(zString); } - iPattern++; + zPattern++; } - if( zPattern[iPattern]==0 ) return 1; + if( *zPattern==0 ) return 1; - while( zString[iString] ){ - if( icuLikeCompare(&zPattern[iPattern], &zString[iString], uEsc) ){ + while( *zString ){ + if( icuLikeCompare(zPattern, zString, uEsc) ){ return 1; } - U8_FWD_1_UNSAFE(zString, iString); + SQLITE_ICU_SKIP_UTF8(zString); } return 0; - }else if( !prevEscape && uPattern==MATCH_ONE ){ + }else if( uPattern==MATCH_ONE && !prevEscape && uPattern!=(uint32_t)uEsc ){ /* Case 2. */ - if( zString[iString]==0 ) return 0; - U8_FWD_1_UNSAFE(zString, iString); + if( *zString==0 ) return 0; + SQLITE_ICU_SKIP_UTF8(zString); - }else if( !prevEscape && uPattern==uEsc){ + }else if( uPattern==(uint32_t)uEsc && !prevEscape ){ /* Case 3. */ prevEscape = 1; }else{ /* Case 4. */ - UChar32 uString; - U8_NEXT_UNSAFE(zString, iString, uString); - uString = u_foldCase(uString, U_FOLD_CASE_DEFAULT); - uPattern = u_foldCase(uPattern, U_FOLD_CASE_DEFAULT); + uint32_t uString; + SQLITE_ICU_READ_UTF8(zString, uString); + uString = (uint32_t)u_foldCase((UChar32)uString, U_FOLD_CASE_DEFAULT); + uPattern = (uint32_t)u_foldCase((UChar32)uPattern, U_FOLD_CASE_DEFAULT); if( uString!=uPattern ){ return 0; } @@ -139,7 +191,7 @@ static int icuLikeCompare( } } - return zString[iString]==0; + return *zString==0; } /* @@ -194,24 +246,6 @@ static void icuLikeFunc( } } -/* -** This function is called when an ICU function called from within -** the implementation of an SQL scalar function returns an error. -** -** The scalar function context passed as the first argument is -** loaded with an error message based on the following two args. -*/ -static void icuFunctionError( - sqlite3_context *pCtx, /* SQLite scalar function context */ - const char *zName, /* Name of ICU function that failed */ - UErrorCode e /* Error code returned by ICU function */ -){ - char zBuf[128]; - sqlite3_snprintf(128, zBuf, "ICU error: %s(): %s", zName, u_errorName(e)); - zBuf[127] = '\0'; - sqlite3_result_error(pCtx, zBuf, -1); -} - /* ** Function to delete compiled regexp objects. Registered as ** a destructor function with sqlite3_set_auxdata(). @@ -265,8 +299,9 @@ static void icuRegexpFunc(sqlite3_context *p, int nArg, sqlite3_value **apArg){ if( U_SUCCESS(status) ){ sqlite3_set_auxdata(p, 0, pExpr, icuRegexpDelete); - }else{ - assert(!pExpr); + pExpr = sqlite3_get_auxdata(p, 0); + } + if( !pExpr ){ icuFunctionError(p, "uregex_open", status); return; } @@ -319,20 +354,22 @@ static void icuRegexpFunc(sqlite3_context *p, int nArg, sqlite3_value **apArg){ ** of upper() or lower(). ** ** lower('I', 'en_us') -> 'i' -** lower('I', 'tr_tr') -> 'ı' (small dotless i) +** lower('I', 'tr_tr') -> '\u131' (small dotless i) ** ** http://www.icu-project.org/userguide/posix.html#case_mappings */ static void icuCaseFunc16(sqlite3_context *p, int nArg, sqlite3_value **apArg){ - const UChar *zInput; - UChar *zOutput; - int nInput; - int nOutput; - - UErrorCode status = U_ZERO_ERROR; + const UChar *zInput; /* Pointer to input string */ + UChar *zOutput = 0; /* Pointer to output buffer */ + int nInput; /* Size of utf-16 input string in bytes */ + int nOut; /* Size of output buffer in bytes */ + int cnt; + int bToUpper; /* True for toupper(), false for tolower() */ + UErrorCode status; const char *zLocale = 0; assert(nArg==1 || nArg==2); + bToUpper = (sqlite3_user_data(p)!=0); if( nArg==2 ){ zLocale = (const char *)sqlite3_value_text(apArg[1]); } @@ -341,28 +378,42 @@ static void icuCaseFunc16(sqlite3_context *p, int nArg, sqlite3_value **apArg){ if( !zInput ){ return; } - nInput = sqlite3_value_bytes16(apArg[0]); - - nOutput = nInput * 2 + 2; - zOutput = sqlite3_malloc(nOutput); - if( !zOutput ){ + nOut = nInput = sqlite3_value_bytes16(apArg[0]); + if( nOut==0 ){ + sqlite3_result_text16(p, "", 0, SQLITE_STATIC); return; } - if( sqlite3_user_data(p) ){ - u_strToUpper(zOutput, nOutput/2, zInput, nInput/2, zLocale, &status); - }else{ - u_strToLower(zOutput, nOutput/2, zInput, nInput/2, zLocale, &status); - } + for(cnt=0; cnt<2; cnt++){ + UChar *zNew = sqlite3_realloc(zOutput, nOut); + if( zNew==0 ){ + sqlite3_free(zOutput); + sqlite3_result_error_nomem(p); + return; + } + zOutput = zNew; + status = U_ZERO_ERROR; + if( bToUpper ){ + nOut = 2*u_strToUpper(zOutput,nOut/2,zInput,nInput/2,zLocale,&status); + }else{ + nOut = 2*u_strToLower(zOutput,nOut/2,zInput,nInput/2,zLocale,&status); + } - if( !U_SUCCESS(status) ){ - icuFunctionError(p, "u_strToLower()/u_strToUpper", status); + if( U_SUCCESS(status) ){ + sqlite3_result_text16(p, zOutput, nOut, xFree); + }else if( status==U_BUFFER_OVERFLOW_ERROR ){ + assert( cnt==0 ); + continue; + }else{ + icuFunctionError(p, bToUpper ? "u_strToUpper" : "u_strToLower", status); + } return; } - - sqlite3_result_text16(p, zOutput, -1, xFree); + assert( 0 ); /* Unreachable */ } +#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU) */ + /* ** Collation sequence destructor function. The pCtx argument points to ** a UCollator structure previously allocated using ucol_open(). @@ -420,7 +471,7 @@ static void icuLoadCollation( UCollator *pUCollator; /* ICU library collation object */ int rc; /* Return code from sqlite3_create_collation_x() */ - assert(nArg==2); + assert(nArg==2 || nArg==3); (void)nArg; /* Unused parameter */ zLocale = (const char *)sqlite3_value_text(apArg[0]); zName = (const char *)sqlite3_value_text(apArg[1]); @@ -435,7 +486,39 @@ static void icuLoadCollation( return; } assert(p); - + if(nArg==3){ + const char *zOption = (const char*)sqlite3_value_text(apArg[2]); + static const struct { + const char *zName; + UColAttributeValue val; + } aStrength[] = { + { "PRIMARY", UCOL_PRIMARY }, + { "SECONDARY", UCOL_SECONDARY }, + { "TERTIARY", UCOL_TERTIARY }, + { "DEFAULT", UCOL_DEFAULT_STRENGTH }, + { "QUARTERNARY", UCOL_QUATERNARY }, + { "IDENTICAL", UCOL_IDENTICAL }, + }; + unsigned int i; + for(i=0; i=sizeof(aStrength)/sizeof(aStrength[0]) ){ + sqlite3_str *pStr = sqlite3_str_new(sqlite3_context_db_handle(p)); + sqlite3_str_appendf(pStr, + "unknown collation strength \"%s\" - should be one of:", + zOption); + for(i=0; izName, p->nArg, p->enc, p->pContext, p->xFunc, 0, 0 + db, p->zName, p->nArg, p->enc, + p->iContext ? (void*)db : (void*)0, + p->xFunc, 0, 0 ); } return rc; } -#if !SQLITE_CORE +#ifndef SQLITE_CORE #ifdef _WIN32 __declspec(dllexport) #endif diff --git a/ext/icu/sqliteicu.h b/ext/icu/sqliteicu.h index 69b42f9821..24a4d623bd 100644 --- a/ext/icu/sqliteicu.h +++ b/ext/icu/sqliteicu.h @@ -24,4 +24,3 @@ int sqlite3IcuInit(sqlite3 *db); #ifdef __cplusplus } /* extern "C" */ #endif /* __cplusplus */ - diff --git a/ext/intck/intck1.test b/ext/intck/intck1.test new file mode 100644 index 0000000000..ef29c2c54c --- /dev/null +++ b/ext/intck/intck1.test @@ -0,0 +1,334 @@ +# 2008 Feb 19 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# The focus of this file is testing the incremental integrity check +# (intck) extension. +# + +source [file join [file dirname [info script]] intck_common.tcl] +set testprefix intck1 +return_if_no_intck + +foreach {tn sql} { + 1 "CREATE TABLE t1(a PRIMARY KEY, b)" + 2 "CREATE TABLE t2(a PRIMARY KEY, b) WITHOUT ROWID " + 3 "CREATE TABLE t3(a PRIMARY KEY, b) WITHOUT rowID;" + 4 "CREATE TABLE t4(a PRIMARY KEY, ROWID)" + 5 {CREATE TABLE t5(a PRIMARY KEY, ROWID) WITHOUT ROWID + } +} { + do_test 1.1.$tn { + db eval $sql + set {} {} + } {} +} + +set space " \n\v\t\r\f" + +do_execsql_test 1.2 { + SELECT name, (rtrim(sql, $space) LIKE '%rowid') + FROM sqlite_schema WHERE type='table' + ORDER BY 1 +} { + t1 0 + t2 1 + t3 1 + t4 0 + t5 1 +} + +do_execsql_test 1.3 { + CREATE TABLE x1(a COLLATE nocase, b INTEGER, c BLOB); + INSERT INTO x1 VALUES('lEtTeRs', 1234, 1234); +} +do_execsql_test 1.3.1 { + WITH wrapper(c1, c2, c3) AS ( + SELECT a, b, c FROM x1 + ) + SELECT * FROM wrapper WHERE c1='letters'; +} {lEtTeRs 1234 1234} +do_execsql_test 1.3.2 { + WITH wrapper(c1, c2, c3) AS ( + SELECT a, b, c FROM x1 + ) + SELECT * FROM wrapper WHERE c2='1234'; +} {lEtTeRs 1234 1234} +do_execsql_test 1.3.2 { + WITH wrapper(c1, c2, c3) AS ( + SELECT a, b, c FROM x1 + ) + SELECT * FROM wrapper WHERE c3='1234'; +} {} + +do_execsql_test 1.4 { + CREATE TABLE z1(a, b); + CREATE INDEX z1ab ON z1(a+b COLLATE nocase); +} +do_execsql_test 1.4.1 { + SELECT * FROM z1 INDEXED BY z1ab +} + +do_catchsql_test 1.5.1 { + CREATE INDEX z1b ON z1(b ASC NULLS LAST); +} {1 {unsupported use of NULLS LAST}} +do_catchsql_test 1.5.2 { + CREATE INDEX z1b ON z1(b DESC NULLS LAST); +} {1 {unsupported use of NULLS LAST}} +do_catchsql_test 1.5.3 { + CREATE INDEX z1b ON z1(b ASC NULLS FIRST); +} {1 {unsupported use of NULLS FIRST}} +do_catchsql_test 1.5.4 { + CREATE INDEX z1b ON z1(b DESC NULLS FIRST); +} {1 {unsupported use of NULLS FIRST}} + + +reset_db +do_execsql_test 1.6.1 { + CREATE TABLE t1(i INTEGER PRIMARY KEY, b, c); + CREATE INDEX i1 ON t1(b); + ANALYZE; + INSERT INTO sqlite_stat1 VALUES('t1', 'i1', '10000 10000'); + ANALYZE sqlite_schema; +} {} +do_eqp_test 1.6.2 { + SELECT 1 FROM t1 INDEXED BY i1 WHERE (b, i) IS (?, ?); +} {SEARCH} + + + +#------------------------------------------------------------------------- +reset_db + +do_test 2.0 { + set ic [sqlite3_intck db main] + $ic close +} {} + +do_execsql_test 2.1 { + CREATE TABLE t1(a, b); + INSERT INTO t1 VALUES(1, 2); + INSERT INTO t1 VALUES(3, 4); + + CREATE INDEX i1 ON t1(a COLLATE nocase); + CREATE INDEX i2 ON t1(b, a); + CREATE INDEX i3 ON t1(b + a COLLATE nocase) WHERE a!=1; +} + +do_intck_test 2.2 { +} + +# Delete a row from each of the i1 and i2 indexes using the imposter +# table interface. +# +do_test 2.3 { + db eval {SELECT name, rootpage FROM sqlite_schema} { + set R($name) $rootpage + } + sqlite3_test_control SQLITE_TESTCTRL_IMPOSTER db main 1 $R(i1) + db eval { CREATE TABLE imp1(a PRIMARY KEY, rowid) WITHOUT ROWID; } + sqlite3_test_control SQLITE_TESTCTRL_IMPOSTER db main 1 $R(i2) + db eval { CREATE TABLE imp2(b, a, rowid, PRIMARY KEY(b, a)) WITHOUT ROWID; } + sqlite3_test_control SQLITE_TESTCTRL_IMPOSTER db main 0 0 + + db eval { + PRAGMA writable_schema=on; + DELETE FROM imp1 WHERE rowid=1; + DELETE FROM imp2 WHERE rowid=2; + } + + db close + sqlite3 db test.db +} {} + +do_intck_test 2.4 { + {entry (1,1) missing from index i1} + {entry (4,3,2) missing from index i2} +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 3.0 { + CREATE TABLE x1(a, b, c, PRIMARY KEY(c, b)) WITHOUT ROWID; + CREATE INDEX x1a ON x1(a COLLATE nocase); + + INSERT INTO x1 VALUES(1, 2, 'three'); + INSERT INTO x1 VALUES(4, 5, 'six'); + INSERT INTO x1 VALUES(7, 8, 'nine'); +} + +do_intck_test 3.1 { } + +do_test 3.2 { + db eval {SELECT name, rootpage FROM sqlite_schema} { + set R($name) $rootpage + } + sqlite3_test_control SQLITE_TESTCTRL_IMPOSTER db main 1 $R(x1a) + db eval { CREATE TABLE imp1(c, b, a, PRIMARY KEY(c, b)) WITHOUT ROWID } + sqlite3_test_control SQLITE_TESTCTRL_IMPOSTER db main 0 0 + + db eval { + PRAGMA writable_schema=on; + DELETE FROM imp1 WHERE a=5; + } + execsql_pp { + } + + db close + sqlite3 db test.db +} {} + +do_intck_test 3.3 { + {entry (4,'six',5) missing from index x1a} +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 4.0 { + CREATE TABLE www(x, y, z); + CREATE INDEX w1 ON www( (x+1), z ); + INSERT INTO www VALUES(1, 1, 1), (2, 2, 2); +} + +do_intck_test 4.1 { } + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 5.0 { + CREATE TABLE t1(a, b); + CREATE INDEX i1 ON t1(a COLLATE NOCASE); + INSERT INTO t1 VALUES(1, 1); + INSERT INTO t1 VALUES(2, 2); +} + +do_test 5.1 { + set ic [sqlite3_intck db nosuchdb] + $ic step +} {SQLITE_ERROR} + +do_test 5.2 { + $ic close + set ic [sqlite3_intck db {}] + while {[$ic step]=="SQLITE_OK"} {} + set res [$ic error] + $ic close + set res +} {SQLITE_OK {}} + +do_test 5.3 { test_do_intck db "main" } {} + +do_test 5.4 { + set ret {} + set ic [sqlite3_intck db main] + db eval [$ic test_sql t1] { + if {$error_message!=""} { lappend ret $error_message } + } + $ic close + set ret +} {} + +do_test 5.5 { + set ret {} + set ic [sqlite3_intck db main] + db eval [$ic test_sql {}] { + if {$error_message!=""} { lappend ret $error_message } + } + $ic close + set ret +} {} + +db cache flush + +do_test 5.6 { + set ret {} + set ic [sqlite3_intck db main] + $ic step + db eval [$ic test_sql {}] { + if {$error_message!=""} { lappend ret $error_message } + } + $ic close + set ret +} {} + +#------------------------------------------------------------------------- +reset_db + +do_execsql_test 6.0 { + CREATE TABLE t1(x, y, PRIMARY KEY(x)) WITHOUT ROWID; + CREATE INDEX i1 ON t1(y, x); + INSERT INTO t1 VALUES(X'0000', X'1111'); +} + +do_intck_test 6.1 {} + +do_execsql_test 6.2.1 { + PRAGMA writable_schema = 1; + UPDATE sqlite_schema SET sql = 'CREATE INDEX i1' WHERE name='i1'; +} {} +do_intck_test 6.2.2 {} + +do_execsql_test 6.3.1 { + UPDATE sqlite_schema SET sql = 'CREATE INDEX i1(y' WHERE name='i1'; +} {} +do_intck_test 6.3.2 {} + +do_execsql_test 6.4.1 { + UPDATE sqlite_schema + SET sql = 'CREATE INDEX i1(y) hello world' + WHERE name='i1'; +} {} +do_intck_test 6.4.2 {} + +do_execsql_test 6.5.1 { + UPDATE sqlite_schema + SET sql = 'CREATE INDEX i1(y, x) WHERE 1 ' + WHERE name='i1'; +} {} +do_intck_test 6.5.2 {} + +do_execsql_test 6.6.1 { + UPDATE sqlite_schema + SET sql = 'CREATE INDEX i1( , ) WHERE 1 ' + WHERE name='i1'; +} {} + +do_test 6.7.2 { + set ic [sqlite3_intck db main] + $ic step +} {SQLITE_ERROR} +do_test 6.5.3 { + $ic error +} {SQLITE_ERROR {near "AS": syntax error}} +$ic close + +do_execsql_test 6.6.1 { + UPDATE sqlite_schema + SET sql = 'CREATE INDEX i1([y' + WHERE name='i1'; +} {} +do_intck_test 6.6.2 {} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 7.0 { + CREATE TABLE x1("1", "22", "3333", four); + CREATE INDEX i1 ON x1( "1" , "22", NULL); + INSERT INTO x1 VALUES(1, 22, 3333, NULL); + INSERT INTO x1 VALUES(1, 22, 3333, NULL); +} +do_execsql_test 7.1 " CREATE INDEX i2 ON x1( \"1\"\r\n\t ) " +do_execsql_test 7.2 { CREATE INDEX i3 ON x1( "22" || 'abc''def' || `1` ) } +do_execsql_test 7.3 { CREATE INDEX i4 ON x1( [22] + [1] ) } +do_execsql_test 7.4 { CREATE INDEX i5 ON x1( four||'hello' ) } + +do_intck_test 7.5 {} + + +finish_test diff --git a/ext/intck/intck2.test b/ext/intck/intck2.test new file mode 100644 index 0000000000..a35bbc70c9 --- /dev/null +++ b/ext/intck/intck2.test @@ -0,0 +1,178 @@ +# 2024 Feb 19 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# The focus of this file is testing the incremental integrity check +# (intck) extension. +# + +source [file join [file dirname [info script]] intck_common.tcl] +set testprefix intck2 +return_if_no_intck + + +do_execsql_test 1.0 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT); + INSERT INTO t1 VALUES(1, 'one'); + INSERT INTO t1 VALUES(2, 'two'); + INSERT INTO t1 VALUES(3, 'three'); + CREATE INDEX i1 ON t1(b); +} + +proc imposter_edit {obj create sql} { + sqlite3 xdb test.db + set pgno [xdb one {SELECT rootpage FROM sqlite_schema WHERE name=$obj}] + xdb eval {PRAGMA Writable_schema=ON} + + sqlite3_test_control SQLITE_TESTCTRL_IMPOSTER xdb main 1 $pgno + xdb eval $create + sqlite3_test_control SQLITE_TESTCTRL_IMPOSTER xdb main 0 0 + xdb eval $sql + xdb close +} + +imposter_edit i1 { + CREATE TABLE imp(b, a, PRIMARY KEY(b)) WITHOUT ROWID; +} { + DELETE FROM imp WHERE b='two'; + INSERT INTO imp(b, a) VALUES('four', 4); +} + +do_intck_test 1.1 { + {surplus entry ('four',4) in index i1} + {entry ('two',2) missing from index i1} +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 2.0 { + CREATE TABLE x1(a, b, "c d"); + CREATE INDEX x1a ON x1(a COLLATE nocase DESC , b ASC); + CREATE INDEX x1b ON x1( a || b || ' "''" ' COLLATE binary ASC ); + CREATE INDEX x1c ON x1( format('%s', a)ASC, format('%d', "c d" ) ); + INSERT INTO x1 VALUES('one', 2, 3); + INSERT INTO x1 VALUES('One', 4, 5); + INSERT INTO x1 VALUES('ONE', 6, 7); + INSERT INTO x1 VALUES(NULL, NULL, NULL); +} + +do_intck_test 2.1 {} + +imposter_edit x1 { + CREATE TABLE imp(a, b, c); +} { + DELETE FROM imp WHERE c=7; +} +do_intck_test 2.2 { + {surplus entry ('ONE',6,3) in index x1a} + {surplus entry ('ONE6 "''" ',3) in index x1b} + {surplus entry ('ONE','7',3) in index x1c} +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 3.0 { + CREATE TABLE x1(a, b, c); + CREATE INDEX x1all ON x1(a DESC, b ASC, c DESC); + INSERT INTO x1 VALUES(2, 1, 2); + INSERT INTO x1 VALUES(2, 1, 1); + INSERT INTO x1 VALUES(2, 2, 2); + INSERT INTO x1 VALUES(2, 2, 1); + INSERT INTO x1 VALUES(1, 1, 2); + INSERT INTO x1 VALUES(1, 1, 1); + INSERT INTO x1 VALUES(1, 2, 2); + INSERT INTO x1 VALUES(1, 2, 1); +} + +do_intck_test 3.1 { +} + +imposter_edit x1 { + CREATE TABLE imp(a, b, c); +} { + DELETE FROM imp WHERE 1; +} + +db close +sqlite3 db test.db + +do_intck_test 3.2 { + {surplus entry (2,1,2,1) in index x1all} + {surplus entry (2,1,1,2) in index x1all} + {surplus entry (2,2,2,3) in index x1all} + {surplus entry (2,2,1,4) in index x1all} + {surplus entry (1,1,2,5) in index x1all} + {surplus entry (1,1,1,6) in index x1all} + {surplus entry (1,2,2,7) in index x1all} + {surplus entry (1,2,1,8) in index x1all} +} + +do_execsql_test 3.3 { + DELETE FROM x1; + INSERT INTO x1 VALUES(NULL, NULL, NULL); + INSERT INTO x1 VALUES(NULL, NULL, NULL); + INSERT INTO x1 VALUES(NULL, NULL, NULL); + INSERT INTO x1 VALUES(NULL, NULL, NULL); +} + +do_intck_test 3.4 { +} + +imposter_edit x1 { + CREATE TABLE imp(a, b, c); +} { + DELETE FROM imp WHERE 1; + INSERT INTO imp(rowid) VALUES(-123); + INSERT INTO imp(rowid) VALUES(456); +} + +db close +sqlite3 db test.db + +do_intck_test 3.5 { + {entry (NULL,NULL,NULL,-123) missing from index x1all} + {entry (NULL,NULL,NULL,456) missing from index x1all} + {surplus entry (NULL,NULL,NULL,1) in index x1all} + {surplus entry (NULL,NULL,NULL,2) in index x1all} + {surplus entry (NULL,NULL,NULL,3) in index x1all} + {surplus entry (NULL,NULL,NULL,4) in index x1all} +} + +reset_db + +do_execsql_test 3.6 { + CREATE TABLE w1(a PRIMARY KEY, b, c); + INSERT INTO w1 VALUES(1.0, NULL, NULL); + INSERT INTO w1 VALUES(33.0, NULL, NULL); + INSERT INTO w1 VALUES(100.0, NULL, NULL); + CREATE INDEX w1bc ON w1(b, c); +} + +do_intck_test 3.7 { +} + +imposter_edit w1 { + CREATE TABLE imp(a, b, c); +} { + DELETE FROM imp WHERE a=33; + INSERT INTO imp(a) VALUES(1234.5); + INSERT INTO imp(a) VALUES(-1234.5); +} + +do_intck_test 3.8 { + {surplus entry (33.0,2) in index sqlite_autoindex_w1_1} + {entry (1234.5,4) missing from index sqlite_autoindex_w1_1} + {entry (NULL,NULL,4) missing from index w1bc} + {entry (-1234.5,5) missing from index sqlite_autoindex_w1_1} + {entry (NULL,NULL,5) missing from index w1bc} + {surplus entry (NULL,NULL,2) in index w1bc} +} + +finish_test diff --git a/ext/intck/intck_common.tcl b/ext/intck/intck_common.tcl new file mode 100644 index 0000000000..1e216b59f3 --- /dev/null +++ b/ext/intck/intck_common.tcl @@ -0,0 +1,66 @@ +# 2024 Feb 18 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +if {![info exists testdir]} { + set testdir [file join [file dirname [info script]] .. .. test] +} +source $testdir/tester.tcl + +ifcapable !vtab||!pragma { + proc return_if_no_intck {} { + finish_test + return -code return + } + return +} else { + proc return_if_no_intck {} {} +} + +proc do_intck {db {bSuspend 0}} { + set ic [sqlite3_intck $db main] + + set ret [list] + while {"SQLITE_OK"==[$ic step]} { + set msg [$ic message] + if {$msg!=""} { + lappend ret $msg + } + if {$bSuspend} { + $ic unlock + #puts "SQL: [$ic test_sql {}]" + #execsql_pp "EXPLAIN query plan [$ic test_sql {}]" + #explain_i [$ic test_sql {}] + } + } + + set err [$ic error] + if {[lindex $err 0]!="SQLITE_OK"} { + error $err + } + $ic close + + return $ret +} + +proc intck_sql {db tbl} { + set ic [sqlite3_intck $db main] + set sql [$ic test_sql $tbl] + $ic close + return $sql +} + +proc do_intck_test {tn expect} { + uplevel [list do_test $tn.a [list do_intck db] [list {*}$expect]] + uplevel [list do_test $tn.b [list do_intck db 1] [list {*}$expect]] +} + + diff --git a/ext/intck/intckbusy.test b/ext/intck/intckbusy.test new file mode 100644 index 0000000000..7c65b686bb --- /dev/null +++ b/ext/intck/intckbusy.test @@ -0,0 +1,49 @@ +# 2024 February 24 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +source [file join [file dirname [info script]] intck_common.tcl] +set testprefix intckbusy +return_if_no_intck + + + +do_execsql_test 1.0 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c); + INSERT INTO t1 VALUES(1, 2, 3); + INSERT INTO t1 VALUES(2, 'two', 'three'); + INSERT INTO t1 VALUES(3, NULL, NULL); + CREATE INDEX i1 ON t1(b, c); +} + +sqlite3 db2 test.db + +do_execsql_test -db db2 1.1 { + BEGIN EXCLUSIVE; + INSERT INTO t1 VALUES(4, 5, 6); +} + +do_test 1.2 { + set ic [sqlite3_intck db main] + $ic step +} {SQLITE_BUSY} +do_test 1.3 { + $ic unlock +} {SQLITE_BUSY} +do_test 1.4 { + $ic error +} {SQLITE_BUSY {database is locked}} +do_test 1.4 { + $ic close +} {} + +finish_test + diff --git a/ext/intck/intckcorrupt.test b/ext/intck/intckcorrupt.test new file mode 100644 index 0000000000..eee63b32f0 --- /dev/null +++ b/ext/intck/intckcorrupt.test @@ -0,0 +1,236 @@ +# 2024 Feb 21 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# The focus of this file is testing the intck extensions response +# to corruption at the b-tree level. +# + +source [file join [file dirname [info script]] intck_common.tcl] +set testprefix intckcorrupt +return_if_no_intck + +#------------------------------------------------------------------------- +reset_db +do_test 1.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 356352 pagesize 4096 filename crash-acaae0347204ae.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 d0 00 00 00 .....@ ........ +| 32: 40 00 ea 00 00 00 00 00 00 40 00 00 00 40 00 00 @........@...@.. +| 96: 00 00 00 00 0d 00 00 00 04 0e 9c 00 0f ad 0f 4f ...............O +| 112: 0e fc 0e 9c 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 3728: 00 00 00 00 00 00 00 00 00 00 00 00 5e 04 07 17 ............^... +| 3744: 1f 1f 01 81 0b 74 61 62 6c 65 74 31 5f 70 61 72 .....tablet1_par +| 3760: 65 6e 74 74 31 5f 70 61 72 65 6e 74 04 43 52 45 entt1_parent.CRE +| 3776: 41 54 45 20 54 41 42 4c 45 20 22 74 31 5f 70 61 ATE TABLE .t1_pa +| 3792: 72 65 6e 74 22 28 6e 6f 64 65 6e 6f 20 49 4e 54 rent.(nodeno INT +| 3808: 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 EGER PRIMARY KEY +| 3824: 2c 70 61 72 65 6e 74 6e 6f 64 65 29 51 03 06 17 ,parentnode)Q... +| 3840: 1b 1b 01 7b 74 61 62 6c 65 74 31 5f 6e 6f 64 65 ....tablet1_node +| 3856: 74 31 5f 6e 6f 64 65 03 43 52 45 41 54 45 20 54 t1_node.CREATE T +| 3872: 41 42 4c 45 20 22 74 31 5f 6e 6f 64 65 22 28 6e ABLE .t1_node.(n +| 3888: 6f 64 65 6e 6f 20 49 4e 54 45 47 45 52 20 50 52 odeno INTEGER PR +| 3904: 49 4d 41 52 59 20 4b 45 59 2c 64 61 74 61 29 5c IMARY KEY,data). +| 3920: 02 07 17 1d 1d 01 81 0b 74 61 62 6c 65 74 31 5f ........tablet1_ +| 3936: 72 6f 77 69 64 74 31 5f 72 6f 77 69 64 02 43 52 rowidt1_rowid.CR +| 3952: 45 41 54 45 20 54 41 42 4c 45 20 22 74 31 5f 72 EATE TABLE .t1_r +| 3968: 6f 77 69 64 22 28 72 6f 77 69 64 20 49 4e 54 45 owid.(rowid INTE +| 3984: 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c GER PRIMARY KEY, +| 4000: 6e 6f 64 65 6e 6f 2c 61 30 2c 61 31 29 51 01 07 nodeno,a0,a1)Q.. +| 4016: 17 11 11 08 81 0f 74 61 62 6c 65 74 31 74 31 43 ......tablet1t1C +| 4032: 52 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 REATE VIRTUAL TA +| 4048: 42 4c 45 20 74 31 20 55 53 49 4e 47 20 72 74 72 BLE t1 USING rtr +| 4064: 65 65 28 69 64 2c 78 30 20 50 52 49 4d 41 52 59 ee(id,x0 PRIMARY +| 4080: 20 4b 45 59 2c 70 61 72 65 6e 74 6e 6f 64 65 29 KEY,parentnode) +| page 2 offset 4096 +| 0: 51 03 06 17 1b 1b 01 7b 74 61 62 6c 65 74 31 5f Q.......tablet1_ +| 16: 6e 6f 64 65 74 31 5f 6e 6f 64 65 03 43 52 45 41 nodet1_node.CREA +| 32: 54 45 20 54 41 42 4c 45 20 22 74 31 5f 6e 6f 64 TE TABLE .t1_nod +| 48: 65 22 28 6e 6f 64 65 6e 6f 20 49 4e 54 45 47 45 e.(nodeno INTEGE +| 64: 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 64 61 R PRIMARY KEY,da +| 80: 74 61 29 5c 02 07 17 1d 1d 01 81 0b 74 61 62 6c ta).........tabl +| 96: 65 74 31 5f 72 6f 77 69 64 74 31 5f 72 6f 77 69 et1_rowidt1_rowi +| 112: 64 02 43 52 45 41 54 45 20 54 41 42 4c 45 00 00 d.CREATE TABLE.. +| 128: 01 0a 02 00 00 00 01 0e 0d 00 00 00 00 24 0e 0d .............$.. +| 144: 0c 1a 06 85 50 46 60 27 70 08 00 00 00 00 00 00 ....PF`'p....... +| 3824: 00 00 00 00 00 00 00 0d 0e 05 00 09 1d 00 74 6f ..............to +| 3840: 79 20 68 61 6c 66 10 0d 05 00 09 23 00 62 6f 74 y half.....#.bot +| 3856: 74 6f 6d 20 68 61 6c 66 0f 0c 05 00 09 21 00 72 tom half.....!.r +| 3872: 69 67 68 74 20 68 61 6c 66 0e 0b 05 00 09 1f 00 ight half....... +| 3888: 6c 65 66 74 20 43 15 f6 e6 f6 46 50 34 35 24 54 left C....FP45$T +| 3904: 15 44 52 05 44 14 24 c4 52 02 27 43 15 f6 e6 f6 .DR.D.$.R.'C.... +| 3920: 46 52 22 8e 6f 64 65 6e 6f 20 49 4e 54 45 47 45 FR..odeno INTEGE +| 3936: 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 64 61 R PRIMARY KEY,da +| 3952: 74 61 29 5c 02 07 17 1d 1d 01 81 0b 74 61 62 6c ta).........tabl +| 3968: 65 74 31 5f 72 6f 74 74 6f 6d 20 65 64 67 65 0f et1_rottom edge. +| 3984: 07 05 00 09 21 00 72 69 67 68 74 20 65 64 67 65 ....!.right edge +| 4000: 0e 06 05 00 09 1f 00 6c 65 66 74 20 65 64 67 65 .......left edge +| 4016: 0b 05 05 00 09 19 00 63 65 6e 74 65 72 17 04 05 .......center... +| 4032: 00 09 31 00 75 70 70 65 72 2d 72 69 67 68 74 20 ..1.upper-right +| 4048: 63 6f 72 6e 65 72 17 03 05 00 09 31 00 6c 6f 77 corner.....1.low +| 4064: 65 72 2d 72 69 67 68 74 20 63 6f 72 6e 65 72 16 er-right corner. +| 4080: 02 05 00 09 2f 00 75 70 70 65 72 2d 6c 65 66 74 ..../.upper-left +| page 3 offset 8192 +| 0: 20 63 6f 72 6e 65 72 16 01 05 00 09 2f 01 8c 6f corner...../..o +| 16: 77 65 72 2d 6c 53 51 4c 69 74 65 20 66 6f 72 6d wer-lSQLite form +| 32: 61 74 20 33 00 10 00 01 01 00 40 20 20 00 00 00 at 3......@ ... +| 48: 00 00 00 00 2f 00 00 0d eb 13 00 00 00 03 00 00 ..../........... +| 64: 00 04 00 00 00 00 00 00 00 06 00 00 00 01 00 00 ................ +| 80: 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 ................ +| page 6 offset 20480 +| 128: 00 00 00 00 00 00 00 00 97 3d 04 ae 7c 01 00 00 .........=..|... +| 624: 00 00 00 00 00 00 21 97 3d 04 ae 7c 01 00 00 00 ......!.=..|.... +| 1120: 00 00 00 00 00 20 97 3d 04 ae 7c 01 00 00 00 00 ..... .=..|..... +| 1616: 00 00 00 00 1f 97 3d 04 ae 7c 01 00 00 00 00 00 ......=..|...... +| 2112: 00 00 00 1e 97 3d 04 ae 7c 01 00 00 00 00 00 00 .....=..|....... +| 2608: 00 00 1d 97 d3 d0 4a e7 c0 00 00 00 00 00 00 00 ......J......... +| 3088: 00 00 00 00 00 00 00 00 00 00 00 00 01 f3 00 00 ................ +| 3600: 23 97 3d 04 ae 7c 01 00 00 00 00 00 00 00 00 00 #.=..|.......... +| 4080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 26 ...............& +| page 8 offset 28672 +| 0: 0d 00 00 00 01 04 30 00 04 30 00 00 00 00 00 00 ......0..0...... +| 1072: 97 4d 1e 14 00 ae 7c 00 00 00 00 00 00 00 00 00 .M....|......... +| 1088: 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 03 ................ +| page 10 offset 36864 +| 0: 0d 00 00 00 01 04 30 00 04 30 00 00 00 00 00 00 ......0..0...... +| 1072: 9a ee c1 80 fd 78 1f ce 1b ae eb b4 00 00 00 00 .....x.......... +| 1088: 13 20 ff 20 00 70 00 00 00 60 50 00 00 00 11 e0 . . .p...`P..... +| 1104: 00 00 00 70 00 00 00 60 50 05 35 14 c6 97 46 52 ...p...`P.5...FR +| 1120: 06 66 f7 26 d6 17 42 03 30 01 00 00 10 10 04 02 .f.&..B.0....... +| 1136: 02 00 00 00 00 00 00 00 00 40 00 00 00 00 00 00 .........@...... +| 1152: 00 00 00 00 00 40 00 00 00 40 00 00 00 00 00 00 .....@...@...... +| 4080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 05 ................ +| page 12 offset 45056 +| 0: 0d 00 00 00 01 04 30 00 04 30 e1 b4 30 97 4d 46 ......0..0..0.MF +| 16: 14 00 ae 7c 00 00 00 00 00 00 00 03 00 00 43 00 ...|..........C. +| page 47 offset 188416 +| 2512: 00 00 00 00 00 00 00 00 be 00 00 00 00 00 00 00 ................ +| page 87 offset 352256 +| 2512: 00 00 00 00 00 00 00 00 aa 00 00 00 00 00 00 00 ................ +| end crash-acaae0347204ae.db +}]} {} + +do_intck_test 1.1 { + {corruption found while reading database schema} +} + +#------------------------------------------------------------------------- +reset_db +do_test 2.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 28672 pagesize 4096 filename crash-3afa1ca9e9c1bd.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 07 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 06 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 96: 00 00 00 00 0d 00 00 00 06 0e 88 00 0f b8 0f 6d ...............m +| 112: 0f 3a 0f 0b 0e d5 0e 88 01 00 00 00 00 00 00 00 .:.............. +| 3712: 00 00 00 00 00 00 00 00 4b 06 06 17 25 25 01 5b ........K...%%.[ +| 3728: 74 61 62 6c 65 73 71 6c 69 74 65 5f 73 74 61 74 tablesqlite_stat +| 3744: 31 73 71 6c 69 74 65 5f 73 74 61 74 31 07 43 52 1sqlite_stat1.CR +| 3760: 45 41 54 45 20 54 41 42 4c 45 20 73 71 6c 69 74 EATE TABLE sqlit +| 3776: 65 5f 73 74 61 74 31 28 74 62 6c 2c 69 64 78 2c e_stat1(tbl,idx, +| 3792: 73 74 61 74 29 34 05 06 17 13 11 01 53 69 6e 64 stat)4......Sind +| 3808: 65 78 63 31 63 63 31 06 43 52 45 41 54 45 20 55 exc1cc1.CREATE U +| 3824: 4e 49 51 55 45 20 49 4e 44 45 58 20 63 31 63 20 NIQUE INDEX c1c +| 3840: 4f 4e 20 63 31 28 63 2c 20 62 29 2d 04 06 17 13 ON c1(c, b)-.... +| 3856: 11 01 45 69 6e 64 65 78 63 31 64 63 31 05 43 52 ..Eindexc1dc1.CR +| 3872: 45 41 54 45 20 49 4e 44 45 58 20 63 31 64 20 4f EATE INDEX c1d O +| 3888: 4e 20 63 31 28 64 2c 20 62 29 31 03 06 17 13 11 N c1(d, b)1..... +| 3904: 01 4d 69 6e 64 65 78 62 31 63 62 31 05 43 52 45 .Mindexb1cb1.CRE +| 3920: 41 54 45 20 55 4e 49 51 55 45 20 49 4e 44 45 58 ATE UNIQUE INDEX +| 3936: 20 62 31 63 20 4f 4e 20 62 31 28 63 29 49 02 06 b1c ON b1(c)I.. +| 3952: 17 11 11 0f 7f 74 61 62 6c 65 63 31 63 31 03 43 .....tablec1c1.C +| 3968: 52 45 41 54 45 20 54 41 42 4c 45 20 63 31 28 61 REATE TABLE c1(a +| 3984: 20 49 4e 54 20 50 52 49 4d 41 52 59 20 4b 45 59 INT PRIMARY KEY +| 4000: 2c 20 62 2c 20 63 2c 20 64 29 20 57 49 54 48 4f , b, c, d) WITHO +| 4016: 55 54 20 52 4f 57 49 44 46 01 06 17 11 11 01 79 UT ROWIDF......y +| 4032: 74 61 62 6c 65 62 31 62 31 02 43 52 45 41 54 45 tableb1b1.CREATE +| 4048: 20 54 41 42 4c 45 20 62 31 28 61 20 49 4e 54 20 TABLE b1(a INT +| 4064: 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 62 2c 20 PRIMARY KEY, b, +| 4080: 63 29 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 c) WITHOUT ROWID +| page 2 offset 4096 +| 0: 0a 00 00 00 07 0f ca 00 0f fa 0f f2 0f ea 0f e2 ................ +| 16: 0f da 00 00 00 01 00 00 00 00 00 00 00 00 00 00 ................ +| 4032: 00 00 00 00 00 00 00 00 00 00 07 04 01 0f 01 06 ................ +| 4048: 67 07 07 04 01 0f 01 06 66 06 07 04 01 0f 01 05 g.......f....... +| 4064: 65 05 07 04 01 0f 01 04 64 04 07 04 01 0f 01 03 e.......d....... +| 4080: 63 03 07 04 01 0f 01 02 62 0f 05 04 09 0f 09 61 c.......b......a +| page 3 offset 8192 +| 0: 0a 00 00 00 07 0f bd 00 0f f9 0f ef 0f e5 0f db ................ +| 16: 0f d1 0f c7 0f bd 00 00 00 00 01 00 00 00 00 00 ................ +| 4016: 00 00 00 00 00 00 00 00 00 00 00 00 00 09 05 01 ................ +| 4032: 0f 01 01 07 61 07 07 09 05 01 0f 01 01 06 61 06 ....a.........a. +| 4048: 06 09 05 01 0f 01 01 05 61 05 05 09 05 01 0f 01 ........a....... +| 4064: 01 04 61 04 04 09 05 01 0f 01 01 03 61 03 03 09 ..a.........a... +| 4080: 05 01 0f 01 01 02 61 0f 02 06 05 09 0f 09 09 61 ......a........a +| page 4 offset 12288 +| 0: 0a 00 00 00 07 0f d8 00 0f fc 0f f0 0f ea 0f e4 ................ +| 16: 0f de 0f d8 0f f6 00 00 00 00 00 00 00 00 00 00 ................ +| 4048: 00 00 00 00 00 00 00 00 05 03 01 01 07 07 05 03 ................ +| 4064: 01 01 06 06 05 03 01 01 05 05 05 03 01 01 04 04 ................ +| 4080: 05 03 01 01 03 03 05 03 01 01 0f 02 03 03 09 09 ................ +| page 5 offset 16384 +| 0: 0a 00 00 00 07 0f ca 00 0f fa 0f f2 0f ea 0f 00 ................ +| 4032: 00 00 00 00 00 00 00 00 00 00 07 04 01 0f 01 07 ................ +| 4048: 61 07 07 04 01 0f 01 06 61 06 07 04 01 0f 01 05 a.......a....... +| 4064: 61 05 07 04 01 1f 01 04 61 04 07 04 01 0f 01 03 a.......a....... +| 4080: 61 03 07 04 01 0f 01 02 61 02 05 04 09 0f 09 61 a.......a......a +| page 6 offset 20480 +| 0: 0a 00 00 00 07 0f ca 00 0f fa 0f ea 0f e2 00 00 ................ +| 4032: 00 00 00 00 00 00 00 00 00 00 07 04 01 0f 01 07 ................ +| 4048: 61 07 07 04 01 0f 01 06 61 06 07 04 01 0f 01 05 a.......a....... +| 4064: 61 05 07 04 01 0f 01 04 61 04 07 04 01 0f 01 03 a.......a....... +| 4080: 61 03 07 04 01 0f 01 0f 61 02 05 04 09 0f 09 61 a.......a......a +| page 7 offset 24576 +| 0: 0d 00 00 00 05 0f 1c 00 0f f0 0f e0 0f d3 0f c5 ................ +| 16: 0f b8 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 4016: 00 00 00 00 00 00 00 00 0b 05 04 11 11 13 62 31 ..............b1 +| 4032: 62 31 37 20 31 0c 04 04 11 13 13 62 31 62 31 63 b17 1......b1b1c +| 4048: 37 20 31 0b 03 04 11 11 13 63 31 63 31 37 20 31 7 1......c1c17 1 +| 4064: 0e 02 04 11 13 07 63 31 63 31 64 37 20 31 20 31 ......c1c1d7 1 1 +| 4080: 0e 01 04 11 13 17 63 31 63 31 63 37 20 31 00 00 ......c1c1c7 1.. +| end crash-3afa1ca9e9c1bd.db +}]} {} + +do_intck_test 2.1 { + {corruption found while reading database schema} +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 3.0 { + PRAGMA page_size = 1024; + CREATE TABLE t1(a, b); + CREATE INDEX i1 ON t1(a); + INSERT INTO t1 VALUES(1, 1), (2, 2), (3, 3); +} + +do_test 3.1 { + set pgno [db one {SELECT rootpage FROM sqlite_schema WHERE name='t1'}] + db close + hexio_write test.db [expr ($pgno-1)*1024] 0000 +} {2} + +sqlite3 db test.db +do_intck_test 3.2 { + {corruption found while scanning database object i1} + {corruption found while scanning database object t1} +} + +finish_test + + diff --git a/ext/intck/intckfault.test b/ext/intck/intckfault.test new file mode 100644 index 0000000000..0bc06e584a --- /dev/null +++ b/ext/intck/intckfault.test @@ -0,0 +1,42 @@ +# 2024 February 24 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +source [file join [file dirname [info script]] intck_common.tcl] +set testprefix intckfault +return_if_no_intck + +do_execsql_test 1.0 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c); + INSERT INTO t1 VALUES(1, 2, 3); + INSERT INTO t1 VALUES(2, 'two', 'three'); + INSERT INTO t1 VALUES(3, NULL, NULL); + CREATE INDEX i1 ON t1(b, c); +} + +do_faultsim_test 1 -faults oom-t* -prep { +} -body { + set ::ic [sqlite3_intck db main] + set nStep 0 + while {"SQLITE_OK"==[$::ic step]} { + incr nStep + if {$nStep==3} { $::ic unlock } + } + set res [$::ic error] + $::ic close + set res +} -test { + catch { $::ic close } + faultsim_test_result {0 {SQLITE_OK {}}} {0 {SQLITE_NOMEM {}}} {0 {SQLITE_NOMEM {out of memory}}} +} + +finish_test + diff --git a/ext/intck/sqlite3intck.c b/ext/intck/sqlite3intck.c new file mode 100644 index 0000000000..5f645fae6e --- /dev/null +++ b/ext/intck/sqlite3intck.c @@ -0,0 +1,941 @@ +/* +** 2024-02-08 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +*/ + +#include "sqlite3intck.h" +#include +#include + +#include +#include + +/* +** nKeyVal: +** The number of values that make up the 'key' for the current pCheck +** statement. +** +** rc: +** Error code returned by most recent sqlite3_intck_step() or +** sqlite3_intck_unlock() call. This is set to SQLITE_DONE when +** the integrity-check operation is finished. +** +** zErr: +** If the object has entered the error state, this is the error message. +** Is freed using sqlite3_free() when the object is deleted. +** +** zTestSql: +** The value returned by the most recent call to sqlite3_intck_testsql(). +** Each call to testsql() frees the previous zTestSql value (using +** sqlite3_free()) and replaces it with the new value it will return. +*/ +struct sqlite3_intck { + sqlite3 *db; + const char *zDb; /* Copy of zDb parameter to _open() */ + char *zObj; /* Current object. Or NULL. */ + + sqlite3_stmt *pCheck; /* Current check statement */ + char *zKey; + int nKeyVal; + + char *zMessage; + int bCorruptSchema; + + int rc; /* Error code */ + char *zErr; /* Error message */ + char *zTestSql; /* Returned by sqlite3_intck_test_sql() */ +}; + + +/* +** Some error has occurred while using database p->db. Save the error message +** and error code currently held by the database handle in p->rc and p->zErr. +*/ +static void intckSaveErrmsg(sqlite3_intck *p){ + p->rc = sqlite3_errcode(p->db); + sqlite3_free(p->zErr); + p->zErr = sqlite3_mprintf("%s", sqlite3_errmsg(p->db)); +} + +/* +** If the handle passed as the first argument is already in the error state, +** then this function is a no-op (returns NULL immediately). Otherwise, if an +** error occurs within this function, it leaves an error in said handle. +** +** Otherwise, this function attempts to prepare SQL statement zSql and +** return the resulting statement handle to the user. +*/ +static sqlite3_stmt *intckPrepare(sqlite3_intck *p, const char *zSql){ + sqlite3_stmt *pRet = 0; + if( p->rc==SQLITE_OK ){ + p->rc = sqlite3_prepare_v2(p->db, zSql, -1, &pRet, 0); + if( p->rc!=SQLITE_OK ){ + intckSaveErrmsg(p); + assert( pRet==0 ); + } + } + return pRet; +} + +/* +** If the handle passed as the first argument is already in the error state, +** then this function is a no-op (returns NULL immediately). Otherwise, if an +** error occurs within this function, it leaves an error in said handle. +** +** Otherwise, this function treats argument zFmt as a printf() style format +** string. It formats it according to the trailing arguments and then +** attempts to prepare the results and return the resulting prepared +** statement. +*/ +static sqlite3_stmt *intckPrepareFmt(sqlite3_intck *p, const char *zFmt, ...){ + sqlite3_stmt *pRet = 0; + va_list ap; + char *zSql = 0; + va_start(ap, zFmt); + zSql = sqlite3_vmprintf(zFmt, ap); + if( p->rc==SQLITE_OK && zSql==0 ){ + p->rc = SQLITE_NOMEM; + } + pRet = intckPrepare(p, zSql); + sqlite3_free(zSql); + va_end(ap); + return pRet; +} + +/* +** Finalize SQL statement pStmt. If an error occurs and the handle passed +** as the first argument does not already contain an error, store the +** error in the handle. +*/ +static void intckFinalize(sqlite3_intck *p, sqlite3_stmt *pStmt){ + int rc = sqlite3_finalize(pStmt); + if( p->rc==SQLITE_OK && rc!=SQLITE_OK ){ + intckSaveErrmsg(p); + } +} + +/* +** If there is already an error in handle p, return it. Otherwise, call +** sqlite3_step() on the statement handle and return that value. +*/ +static int intckStep(sqlite3_intck *p, sqlite3_stmt *pStmt){ + if( p->rc ) return p->rc; + return sqlite3_step(pStmt); +} + +/* +** Execute SQL statement zSql. There is no way to obtain any results +** returned by the statement. This function uses the sqlite3_intck error +** code convention. +*/ +static void intckExec(sqlite3_intck *p, const char *zSql){ + sqlite3_stmt *pStmt = 0; + pStmt = intckPrepare(p, zSql); + intckStep(p, pStmt); + intckFinalize(p, pStmt); +} + +/* +** A wrapper around sqlite3_mprintf() that uses the sqlite3_intck error +** code convention. +*/ +static char *intckMprintf(sqlite3_intck *p, const char *zFmt, ...){ + va_list ap; + char *zRet = 0; + va_start(ap, zFmt); + zRet = sqlite3_vmprintf(zFmt, ap); + if( p->rc==SQLITE_OK ){ + if( zRet==0 ){ + p->rc = SQLITE_NOMEM; + } + }else{ + sqlite3_free(zRet); + zRet = 0; + } + va_end(ap); + return zRet; +} + +/* +** This is used by sqlite3_intck_unlock() to save the vector key value +** required to restart the current pCheck query as a nul-terminated string +** in p->zKey. +*/ +static void intckSaveKey(sqlite3_intck *p){ + int ii; + char *zSql = 0; + sqlite3_stmt *pStmt = 0; + sqlite3_stmt *pXinfo = 0; + const char *zDir = 0; + + assert( p->pCheck ); + assert( p->zKey==0 ); + + pXinfo = intckPrepareFmt(p, + "SELECT group_concat(desc, '') FROM %Q.sqlite_schema s, " + "pragma_index_xinfo(%Q, %Q) " + "WHERE s.type='index' AND s.name=%Q", + p->zDb, p->zObj, p->zDb, p->zObj + ); + if( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXinfo) ){ + zDir = (const char*)sqlite3_column_text(pXinfo, 0); + } + + if( zDir==0 ){ + /* Object is a table, not an index. This is the easy case,as there are + ** no DESC columns or NULL values in a primary key. */ + const char *zSep = "SELECT '(' || "; + for(ii=0; iinKeyVal; ii++){ + zSql = intckMprintf(p, "%z%squote(?)", zSql, zSep); + zSep = " || ', ' || "; + } + zSql = intckMprintf(p, "%z || ')'", zSql); + }else{ + + /* Object is an index. */ + assert( p->nKeyVal>1 ); + for(ii=p->nKeyVal; ii>0; ii--){ + int bLastIsDesc = zDir[ii-1]=='1'; + int bLastIsNull = sqlite3_column_type(p->pCheck, ii)==SQLITE_NULL; + const char *zLast = sqlite3_column_name(p->pCheck, ii); + char *zLhs = 0; + char *zRhs = 0; + char *zWhere = 0; + + if( bLastIsNull ){ + if( bLastIsDesc ) continue; + zWhere = intckMprintf(p, "'%s IS NOT NULL'", zLast); + }else{ + const char *zOp = bLastIsDesc ? "<" : ">"; + zWhere = intckMprintf(p, "'%s %s ' || quote(?%d)", zLast, zOp, ii); + } + + if( ii>1 ){ + const char *zLhsSep = ""; + const char *zRhsSep = ""; + int jj; + for(jj=0; jjpCheck,jj+1); + zLhs = intckMprintf(p, "%z%s%s", zLhs, zLhsSep, zAlias); + zRhs = intckMprintf(p, "%z%squote(?%d)", zRhs, zRhsSep, jj+1); + zLhsSep = ","; + zRhsSep = " || ',' || "; + } + + zWhere = intckMprintf(p, + "'(%z) IS (' || %z || ') AND ' || %z", + zLhs, zRhs, zWhere); + } + zWhere = intckMprintf(p, "'WHERE ' || %z", zWhere); + + zSql = intckMprintf(p, "%z%s(quote( %z ) )", + zSql, + (zSql==0 ? "VALUES" : ",\n "), + zWhere + ); + } + zSql = intckMprintf(p, + "WITH wc(q) AS (\n%z\n)" + "SELECT 'VALUES' || group_concat('(' || q || ')', ',\n ') FROM wc" + , zSql + ); + } + + pStmt = intckPrepare(p, zSql); + if( p->rc==SQLITE_OK ){ + for(ii=0; iinKeyVal; ii++){ + sqlite3_bind_value(pStmt, ii+1, sqlite3_column_value(p->pCheck, ii+1)); + } + if( SQLITE_ROW==sqlite3_step(pStmt) ){ + p->zKey = intckMprintf(p,"%s",(const char*)sqlite3_column_text(pStmt, 0)); + } + intckFinalize(p, pStmt); + } + + sqlite3_free(zSql); + intckFinalize(p, pXinfo); +} + +/* +** Find the next database object (table or index) to check. If successful, +** set sqlite3_intck.zObj to point to a nul-terminated buffer containing +** the object's name before returning. +*/ +static void intckFindObject(sqlite3_intck *p){ + sqlite3_stmt *pStmt = 0; + char *zPrev = p->zObj; + p->zObj = 0; + + assert( p->rc==SQLITE_OK ); + assert( p->pCheck==0 ); + + pStmt = intckPrepareFmt(p, + "WITH tables(table_name) AS (" + " SELECT name" + " FROM %Q.sqlite_schema WHERE (type='table' OR type='index') AND rootpage" + " UNION ALL " + " SELECT 'sqlite_schema'" + ")" + "SELECT table_name FROM tables " + "WHERE ?1 IS NULL OR table_name%s?1 " + "ORDER BY 1" + , p->zDb, (p->zKey ? ">=" : ">") + ); + + if( p->rc==SQLITE_OK ){ + sqlite3_bind_text(pStmt, 1, zPrev, -1, SQLITE_TRANSIENT); + if( sqlite3_step(pStmt)==SQLITE_ROW ){ + p->zObj = intckMprintf(p,"%s",(const char*)sqlite3_column_text(pStmt, 0)); + } + } + intckFinalize(p, pStmt); + + /* If this is a new object, ensure the previous key value is cleared. */ + if( sqlite3_stricmp(p->zObj, zPrev) ){ + sqlite3_free(p->zKey); + p->zKey = 0; + } + + sqlite3_free(zPrev); +} + +/* +** Return the size in bytes of the first token in nul-terminated buffer z. +** For the purposes of this call, a token is either: +** +** * a quoted SQL string, +* * a contiguous series of ascii alphabet characters, or +* * any other single byte. +*/ +static int intckGetToken(const char *z){ + char c = z[0]; + int iRet = 1; + if( c=='\'' || c=='"' || c=='`' ){ + while( 1 ){ + if( z[iRet]==c ){ + iRet++; + if( z[iRet]!=c ) break; + } + iRet++; + } + } + else if( c=='[' ){ + while( z[iRet++]!=']' && z[iRet] ); + } + else if( (c>='A' && c<='Z') || (c>='a' && c<='z') ){ + while( (z[iRet]>='A' && z[iRet]<='Z') || (z[iRet]>='a' && z[iRet]<='z') ){ + iRet++; + } + } + + return iRet; +} + +/* +** Return true if argument c is an ascii whitespace character. +*/ +static int intckIsSpace(char c){ + return (c==' ' || c=='\t' || c=='\n' || c=='\r'); +} + +/* +** Argument z points to the text of a CREATE INDEX statement. This function +** identifies the part of the text that contains either the index WHERE +** clause (if iCol<0) or the iCol'th column of the index. +** +** If (iCol<0), the identified fragment does not include the "WHERE" keyword, +** only the expression that follows it. If (iCol>=0) then the identified +** fragment does not include any trailing sort-order keywords - "ASC" or +** "DESC". +** +** If the CREATE INDEX statement does not contain the requested field or +** clause, NULL is returned and (*pnByte) is set to 0. Otherwise, a pointer to +** the identified fragment is returned and output parameter (*pnByte) set +** to its size in bytes. +*/ +static const char *intckParseCreateIndex(const char *z, int iCol, int *pnByte){ + int iOff = 0; + int iThisCol = 0; + int iStart = 0; + int nOpen = 0; + + const char *zRet = 0; + int nRet = 0; + + int iEndOfCol = 0; + + /* Skip forward until the first "(" token */ + while( z[iOff]!='(' ){ + iOff += intckGetToken(&z[iOff]); + if( z[iOff]=='\0' ) return 0; + } + assert( z[iOff]=='(' ); + + nOpen = 1; + iOff++; + iStart = iOff; + while( z[iOff] ){ + const char *zToken = &z[iOff]; + int nToken = 0; + + /* Check if this is the end of the current column - either a "," or ")" + ** when nOpen==1. */ + if( nOpen==1 ){ + if( z[iOff]==',' || z[iOff]==')' ){ + if( iCol==iThisCol ){ + int iEnd = iEndOfCol ? iEndOfCol : iOff; + nRet = (iEnd - iStart); + zRet = &z[iStart]; + break; + } + iStart = iOff+1; + while( intckIsSpace(z[iStart]) ) iStart++; + iThisCol++; + } + if( z[iOff]==')' ) break; + } + if( z[iOff]=='(' ) nOpen++; + if( z[iOff]==')' ) nOpen--; + nToken = intckGetToken(zToken); + + if( (nToken==3 && 0==sqlite3_strnicmp(zToken, "ASC", nToken)) + || (nToken==4 && 0==sqlite3_strnicmp(zToken, "DESC", nToken)) + ){ + iEndOfCol = iOff; + }else if( 0==intckIsSpace(zToken[0]) ){ + iEndOfCol = 0; + } + + iOff += nToken; + } + + /* iStart is now the byte offset of 1 byte passed the final ')' in the + ** CREATE INDEX statement. Try to find a WHERE clause to return. */ + while( zRet==0 && z[iOff] ){ + int n = intckGetToken(&z[iOff]); + if( n==5 && 0==sqlite3_strnicmp(&z[iOff], "where", 5) ){ + zRet = &z[iOff+5]; + nRet = (int)strlen(zRet); + } + iOff += n; + } + + /* Trim any whitespace from the start and end of the returned string. */ + if( zRet ){ + while( intckIsSpace(zRet[0]) ){ + nRet--; + zRet++; + } + while( nRet>0 && intckIsSpace(zRet[nRet-1]) ) nRet--; + } + + *pnByte = nRet; + return zRet; +} + +/* +** User-defined SQL function wrapper for intckParseCreateIndex(): +** +** SELECT parse_create_index(, ); +*/ +static void intckParseCreateIndexFunc( + sqlite3_context *pCtx, + int nVal, + sqlite3_value **apVal +){ + const char *zSql = (const char*)sqlite3_value_text(apVal[0]); + int idx = sqlite3_value_int(apVal[1]); + const char *zRes = 0; + int nRes = 0; + + assert( nVal==2 ); + if( zSql ){ + zRes = intckParseCreateIndex(zSql, idx, &nRes); + } + sqlite3_result_text(pCtx, zRes, nRes, SQLITE_TRANSIENT); +} + +/* +** Return true if sqlite3_intck.db has automatic indexes enabled, false +** otherwise. +*/ +static int intckGetAutoIndex(sqlite3_intck *p){ + int bRet = 0; + sqlite3_stmt *pStmt = 0; + pStmt = intckPrepare(p, "PRAGMA automatic_index"); + if( SQLITE_ROW==intckStep(p, pStmt) ){ + bRet = sqlite3_column_int(pStmt, 0); + } + intckFinalize(p, pStmt); + return bRet; +} + +/* +** Return true if zObj is an index, or false otherwise. +*/ +static int intckIsIndex(sqlite3_intck *p, const char *zObj){ + int bRet = 0; + sqlite3_stmt *pStmt = 0; + pStmt = intckPrepareFmt(p, + "SELECT 1 FROM %Q.sqlite_schema WHERE name=%Q AND type='index'", + p->zDb, zObj + ); + if( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){ + bRet = 1; + } + intckFinalize(p, pStmt); + return bRet; +} + +/* +** Return a pointer to a nul-terminated buffer containing the SQL statement +** used to check database object zObj (a table or index) for corruption. +** If parameter zPrev is not NULL, then it must be a string containing the +** vector key required to restart the check where it left off last time. +** If pnKeyVal is not NULL, then (*pnKeyVal) is set to the number of +** columns in the vector key value for the specified object. +** +** This function uses the sqlite3_intck error code convention. +*/ +static char *intckCheckObjectSql( + sqlite3_intck *p, /* Integrity check object */ + const char *zObj, /* Object (table or index) to scan */ + const char *zPrev, /* Restart key vector, if any */ + int *pnKeyVal /* OUT: Number of key-values for this scan */ +){ + char *zRet = 0; + sqlite3_stmt *pStmt = 0; + int bAutoIndex = 0; + int bIsIndex = 0; + + const char *zCommon = + /* Relation without_rowid also contains just one row. Column "b" is + ** set to true if the table being examined is a WITHOUT ROWID table, + ** or false otherwise. */ + ", without_rowid(b) AS (" + " SELECT EXISTS (" + " SELECT 1 FROM tabname, pragma_index_list(tab, db) AS l" + " WHERE origin='pk' " + " AND NOT EXISTS (SELECT 1 FROM sqlite_schema WHERE name=l.name)" + " )" + ")" + "" + /* Table idx_cols contains 1 row for each column in each index on the + ** table being checked. Columns are: + ** + ** idx_name: Name of the index. + ** idx_ispk: True if this index is the PK of a WITHOUT ROWID table. + ** col_name: Name of indexed column, or NULL for index on expression. + ** col_expr: Indexed expression, including COLLATE clause. + ** col_alias: Alias used for column in 'intck_wrapper' table. + */ + ", idx_cols(idx_name, idx_ispk, col_name, col_expr, col_alias) AS (" + " SELECT l.name, (l.origin=='pk' AND w.b), i.name, COALESCE((" + " SELECT parse_create_index(sql, i.seqno) FROM " + " sqlite_schema WHERE name = l.name" + " ), format('\"%w\"', i.name) || ' COLLATE ' || quote(i.coll))," + " 'c' || row_number() OVER ()" + " FROM " + " tabname t," + " without_rowid w," + " pragma_index_list(t.tab, t.db) l," + " pragma_index_xinfo(l.name) i" + " WHERE i.key" + " UNION ALL" + " SELECT '', 1, '_rowid_', '_rowid_', 'r1' FROM without_rowid WHERE b=0" + ")" + "" + "" + /* + ** For a PK declared as "PRIMARY KEY(a, b) ... WITHOUT ROWID", where + ** the intck_wrapper aliases of "a" and "b" are "c1" and "c2": + ** + ** o_pk: "o.c1, o.c2" + ** i_pk: "i.'a', i.'b'" + ** ... + ** n_pk: 2 + */ + ", tabpk(db, tab, idx, o_pk, i_pk, q_pk, eq_pk, ps_pk, pk_pk, n_pk) AS (" + " WITH pkfields(f, a) AS (" + " SELECT i.col_name, i.col_alias FROM idx_cols i WHERE i.idx_ispk" + " )" + " SELECT t.db, t.tab, t.idx, " + " group_concat(a, ', '), " + " group_concat('i.'||quote(f), ', '), " + " group_concat('quote(o.'||a||')', ' || '','' || '), " + " format('(%s)==(%s)'," + " group_concat('o.'||a, ', '), " + " group_concat(format('\"%w\"', f), ', ')" + " )," + " group_concat('%s', ',')," + " group_concat('quote('||a||')', ', '), " + " count(*)" + " FROM tabname t, pkfields" + ")" + "" + ", idx(name, match_expr, partial, partial_alias, idx_ps, idx_idx) AS (" + " SELECT idx_name," + " format('(%s,%s) IS (%s,%s)', " + " group_concat(i.col_expr, ', '), i_pk," + " group_concat('o.'||i.col_alias, ', '), o_pk" + " ), " + " parse_create_index(" + " (SELECT sql FROM sqlite_schema WHERE name=idx_name), -1" + " )," + " 'cond' || row_number() OVER ()" + " , group_concat('%s', ',')" + " , group_concat('quote('||i.col_alias||')', ', ')" + " FROM tabpk t, " + " without_rowid w," + " idx_cols i" + " WHERE i.idx_ispk==0 " + " GROUP BY idx_name" + ")" + "" + ", wrapper_with(s) AS (" + " SELECT 'intck_wrapper AS (\n SELECT\n ' || (" + " WITH f(a, b) AS (" + " SELECT col_expr, col_alias FROM idx_cols" + " UNION ALL " + " SELECT partial, partial_alias FROM idx WHERE partial IS NOT NULL" + " )" + " SELECT group_concat(format('%s AS %s', a, b), ',\n ') FROM f" + " )" + " || format('\n FROM %Q.%Q ', t.db, t.tab)" + /* If the object being checked is a table, append "NOT INDEXED". + ** Otherwise, append "INDEXED BY ", and then, if the index + ** is a partial index " WHERE ". */ + " || CASE WHEN t.idx IS NULL THEN " + " 'NOT INDEXED'" + " ELSE" + " format('INDEXED BY %Q%s', t.idx, ' WHERE '||i.partial)" + " END" + " || '\n)'" + " FROM tabname t LEFT JOIN idx i ON (i.name=t.idx)" + ")" + "" + ; + + bAutoIndex = intckGetAutoIndex(p); + if( bAutoIndex ) intckExec(p, "PRAGMA automatic_index = 0"); + + bIsIndex = intckIsIndex(p, zObj); + if( bIsIndex ){ + pStmt = intckPrepareFmt(p, + /* Table idxname contains a single row. The first column, "db", contains + ** the name of the db containing the table (e.g. "main") and the second, + ** "tab", the name of the table itself. */ + "WITH tabname(db, tab, idx) AS (" + " SELECT %Q, (SELECT tbl_name FROM %Q.sqlite_schema WHERE name=%Q), %Q " + ")" + "" + ", whereclause(w_c) AS (%s)" + "" + "%s" /* zCommon */ + "" + ", case_statement(c) AS (" + " SELECT " + " 'CASE WHEN (' || group_concat(col_alias, ', ') || ', 1) IS (\n' " + " || ' SELECT ' || group_concat(col_expr, ', ') || ', 1 FROM '" + " || format('%%Q.%%Q NOT INDEXED WHERE %%s\n', t.db, t.tab, p.eq_pk)" + " || ' )\n THEN NULL\n '" + " || 'ELSE format(''surplus entry ('" + " || group_concat('%%s', ',') || ',' || p.ps_pk" + " || ') in index ' || t.idx || ''', ' " + " || group_concat('quote('||i.col_alias||')', ', ') || ', ' || p.pk_pk" + " || ')'" + " || '\n END AS error_message'" + " FROM tabname t, tabpk p, idx_cols i WHERE i.idx_name=t.idx" + ")" + "" + ", thiskey(k, n) AS (" + " SELECT group_concat(i.col_alias, ', ') || ', ' || p.o_pk, " + " count(*) + p.n_pk " + " FROM tabpk p, idx_cols i WHERE i.idx_name=p.idx" + ")" + "" + ", main_select(m, n) AS (" + " SELECT format(" + " 'WITH %%s\n' ||" + " ', idx_checker AS (\n' ||" + " ' SELECT %%s,\n' ||" + " ' %%s\n' || " + " ' FROM intck_wrapper AS o\n' ||" + " ')\n'," + " ww.s, c, t.k" + " ), t.n" + " FROM case_statement, wrapper_with ww, thiskey t" + ")" + + "SELECT m || " + " group_concat('SELECT * FROM idx_checker ' || w_c, ' UNION ALL '), n" + " FROM " + "main_select, whereclause " + , p->zDb, p->zDb, zObj, zObj + , zPrev ? zPrev : "VALUES('')", zCommon + ); + }else{ + pStmt = intckPrepareFmt(p, + /* Table tabname contains a single row. The first column, "db", contains + ** the name of the db containing the table (e.g. "main") and the second, + ** "tab", the name of the table itself. */ + "WITH tabname(db, tab, idx, prev) AS (SELECT %Q, %Q, NULL, %Q)" + "" + "%s" /* zCommon */ + + /* expr(e) contains one row for each index on table zObj. Value e + ** is set to an expression that evaluates to NULL if the required + ** entry is present in the index, or an error message otherwise. */ + ", expr(e, p) AS (" + " SELECT format('CASE WHEN EXISTS \n" + " (SELECT 1 FROM %%Q.%%Q AS i INDEXED BY %%Q WHERE %%s%%s)\n" + " THEN NULL\n" + " ELSE format(''entry (%%s,%%s) missing from index %%s'', %%s, %%s)\n" + " END\n'" + " , t.db, t.tab, i.name, i.match_expr, ' AND (' || partial || ')'," + " i.idx_ps, t.ps_pk, i.name, i.idx_idx, t.pk_pk)," + " CASE WHEN partial IS NULL THEN NULL ELSE i.partial_alias END" + " FROM tabpk t, idx i" + ")" + + ", numbered(ii, cond, e) AS (" + " SELECT 0, 'n.ii=0', 'NULL'" + " UNION ALL " + " SELECT row_number() OVER ()," + " '(n.ii='||row_number() OVER ()||COALESCE(' AND '||p||')', ')'), e" + " FROM expr" + ")" + + ", counter_with(w) AS (" + " SELECT 'WITH intck_counter(ii) AS (\n ' || " + " group_concat('SELECT '||ii, ' UNION ALL\n ') " + " || '\n)' FROM numbered" + ")" + "" + ", case_statement(c) AS (" + " SELECT 'CASE ' || " + " group_concat(format('\n WHEN %%s THEN (%%s)', cond, e), '') ||" + " '\nEND AS error_message'" + " FROM numbered" + ")" + "" + + /* This table contains a single row consisting of a single value - + ** the text of an SQL expression that may be used by the main SQL + ** statement to output an SQL literal that can be used to resume + ** the scan if it is suspended. e.g. for a rowid table, an expression + ** like: + ** + ** format('(%d,%d)', _rowid_, n.ii) + */ + ", thiskey(k, n) AS (" + " SELECT o_pk || ', ii', n_pk+1 FROM tabpk" + ")" + "" + ", whereclause(w_c) AS (" + " SELECT CASE WHEN prev!='' THEN " + " '\nWHERE (' || o_pk ||', n.ii) > ' || prev" + " ELSE ''" + " END" + " FROM tabpk, tabname" + ")" + "" + ", main_select(m, n) AS (" + " SELECT format(" + " '%%s, %%s\nSELECT %%s,\n%%s\nFROM intck_wrapper AS o" + ", intck_counter AS n%%s\nORDER BY %%s', " + " w, ww.s, c, thiskey.k, whereclause.w_c, t.o_pk" + " ), thiskey.n" + " FROM case_statement, tabpk t, counter_with, " + " wrapper_with ww, thiskey, whereclause" + ")" + + "SELECT m, n FROM main_select", + p->zDb, zObj, zPrev, zCommon + ); + } + + while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){ + zRet = intckMprintf(p, "%s", (const char*)sqlite3_column_text(pStmt, 0)); + if( pnKeyVal ){ + *pnKeyVal = sqlite3_column_int(pStmt, 1); + } + } + intckFinalize(p, pStmt); + + if( bAutoIndex ) intckExec(p, "PRAGMA automatic_index = 1"); + return zRet; +} + +/* +** Open a new integrity-check object. +*/ +int sqlite3_intck_open( + sqlite3 *db, /* Database handle to operate on */ + const char *zDbArg, /* "main", "temp" etc. */ + sqlite3_intck **ppOut /* OUT: New integrity-check handle */ +){ + sqlite3_intck *pNew = 0; + int rc = SQLITE_OK; + const char *zDb = zDbArg ? zDbArg : "main"; + int nDb = (int)strlen(zDb); + + pNew = (sqlite3_intck*)sqlite3_malloc(sizeof(*pNew) + nDb + 1); + if( pNew==0 ){ + rc = SQLITE_NOMEM; + }else{ + memset(pNew, 0, sizeof(*pNew)); + pNew->db = db; + pNew->zDb = (const char*)&pNew[1]; + memcpy(&pNew[1], zDb, nDb+1); + rc = sqlite3_create_function(db, "parse_create_index", + 2, SQLITE_UTF8, 0, intckParseCreateIndexFunc, 0, 0 + ); + if( rc!=SQLITE_OK ){ + sqlite3_intck_close(pNew); + pNew = 0; + } + } + + *ppOut = pNew; + return rc; +} + +/* +** Free the integrity-check object. +*/ +void sqlite3_intck_close(sqlite3_intck *p){ + if( p ){ + sqlite3_finalize(p->pCheck); + sqlite3_create_function( + p->db, "parse_create_index", 1, SQLITE_UTF8, 0, 0, 0, 0 + ); + sqlite3_free(p->zObj); + sqlite3_free(p->zKey); + sqlite3_free(p->zTestSql); + sqlite3_free(p->zErr); + sqlite3_free(p->zMessage); + sqlite3_free(p); + } +} + +/* +** Step the integrity-check object. +*/ +int sqlite3_intck_step(sqlite3_intck *p){ + if( p->rc==SQLITE_OK ){ + + if( p->zMessage ){ + sqlite3_free(p->zMessage); + p->zMessage = 0; + } + + if( p->bCorruptSchema ){ + p->rc = SQLITE_DONE; + }else + if( p->pCheck==0 ){ + intckFindObject(p); + if( p->rc==SQLITE_OK ){ + if( p->zObj ){ + char *zSql = 0; + zSql = intckCheckObjectSql(p, p->zObj, p->zKey, &p->nKeyVal); + p->pCheck = intckPrepare(p, zSql); + sqlite3_free(zSql); + sqlite3_free(p->zKey); + p->zKey = 0; + }else{ + p->rc = SQLITE_DONE; + } + }else if( p->rc==SQLITE_CORRUPT ){ + p->rc = SQLITE_OK; + p->zMessage = intckMprintf(p, "%s", + "corruption found while reading database schema" + ); + p->bCorruptSchema = 1; + } + } + + if( p->pCheck ){ + assert( p->rc==SQLITE_OK ); + if( sqlite3_step(p->pCheck)==SQLITE_ROW ){ + /* Normal case, do nothing. */ + }else{ + intckFinalize(p, p->pCheck); + p->pCheck = 0; + p->nKeyVal = 0; + if( p->rc==SQLITE_CORRUPT ){ + p->rc = SQLITE_OK; + p->zMessage = intckMprintf(p, + "corruption found while scanning database object %s", p->zObj + ); + } + } + } + } + + return p->rc; +} + +/* +** Return a message describing the corruption encountered by the most recent +** call to sqlite3_intck_step(), or NULL if no corruption was encountered. +*/ +const char *sqlite3_intck_message(sqlite3_intck *p){ + assert( p->pCheck==0 || p->zMessage==0 ); + if( p->zMessage ){ + return p->zMessage; + } + if( p->pCheck ){ + return (const char*)sqlite3_column_text(p->pCheck, 0); + } + return 0; +} + +/* +** Return the error code and message. +*/ +int sqlite3_intck_error(sqlite3_intck *p, const char **pzErr){ + if( pzErr ) *pzErr = p->zErr; + return (p->rc==SQLITE_DONE ? SQLITE_OK : p->rc); +} + +/* +** Close any read transaction the integrity-check object is holding open +** on the database. +*/ +int sqlite3_intck_unlock(sqlite3_intck *p){ + if( p->rc==SQLITE_OK && p->pCheck ){ + assert( p->zKey==0 && p->nKeyVal>0 ); + intckSaveKey(p); + intckFinalize(p, p->pCheck); + p->pCheck = 0; + } + return p->rc; +} + +/* +** Return the SQL statement used to check object zObj. Or, if zObj is +** NULL, the current SQL statement. +*/ +const char *sqlite3_intck_test_sql(sqlite3_intck *p, const char *zObj){ + sqlite3_free(p->zTestSql); + if( zObj ){ + p->zTestSql = intckCheckObjectSql(p, zObj, 0, 0); + }else{ + if( p->zObj ){ + p->zTestSql = intckCheckObjectSql(p, p->zObj, p->zKey, 0); + }else{ + sqlite3_free(p->zTestSql); + p->zTestSql = 0; + } + } + return p->zTestSql; +} diff --git a/ext/intck/sqlite3intck.h b/ext/intck/sqlite3intck.h new file mode 100644 index 0000000000..e08a86f289 --- /dev/null +++ b/ext/intck/sqlite3intck.h @@ -0,0 +1,171 @@ +/* +** 2024-02-08 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +*/ + +/* +** Incremental Integrity-Check Extension +** ------------------------------------- +** +** This module contains code to check whether or not an SQLite database +** is well-formed or corrupt. This is the same task as performed by SQLite's +** built-in "PRAGMA integrity_check" command. This module differs from +** "PRAGMA integrity_check" in that: +** +** + It is less thorough - this module does not detect certain types +** of corruption that are detected by the PRAGMA command. However, +** it does detect all kinds of corruption that are likely to cause +** errors in SQLite applications. +** +** + It is slower. Sometimes up to three times slower. +** +** + It allows integrity-check operations to be split into multiple +** transactions, so that the database does not need to be read-locked +** for the duration of the integrity-check. +** +** One way to use the API to run integrity-check on the "main" database +** of handle db is: +** +** int rc = SQLITE_OK; +** sqlite3_intck *p = 0; +** +** sqlite3_intck_open(db, "main", &p); +** while( SQLITE_OK==sqlite3_intck_step(p) ){ +** const char *zMsg = sqlite3_intck_message(p); +** if( zMsg ) printf("corruption: %s\n", zMsg); +** } +** rc = sqlite3_intck_error(p, &zErr); +** if( rc!=SQLITE_OK ){ +** printf("error occured (rc=%d), (errmsg=%s)\n", rc, zErr); +** } +** sqlite3_intck_close(p); +** +** Usually, the sqlite3_intck object opens a read transaction within the +** first call to sqlite3_intck_step() and holds it open until the +** integrity-check is complete. However, if sqlite3_intck_unlock() is +** called, the read transaction is ended and a new read transaction opened +** by the subsequent call to sqlite3_intck_step(). +*/ + +#ifndef _SQLITE_INTCK_H +#define _SQLITE_INTCK_H + +#include "sqlite3.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* +** An ongoing incremental integrity-check operation is represented by an +** opaque pointer of the following type. +*/ +typedef struct sqlite3_intck sqlite3_intck; + +/* +** Open a new incremental integrity-check object. If successful, populate +** output variable (*ppOut) with the new object handle and return SQLITE_OK. +** Or, if an error occurs, set (*ppOut) to NULL and return an SQLite error +** code (e.g. SQLITE_NOMEM). +** +** The integrity-check will be conducted on database zDb (which must be "main", +** "temp", or the name of an attached database) of database handle db. Once +** this function has been called successfully, the caller should not use +** database handle db until the integrity-check object has been destroyed +** using sqlite3_intck_close(). +*/ +int sqlite3_intck_open( + sqlite3 *db, /* Database handle */ + const char *zDb, /* Database name ("main", "temp" etc.) */ + sqlite3_intck **ppOut /* OUT: New sqlite3_intck handle */ +); + +/* +** Close and release all resources associated with a handle opened by an +** earlier call to sqlite3_intck_open(). The results of using an +** integrity-check handle after it has been passed to this function are +** undefined. +*/ +void sqlite3_intck_close(sqlite3_intck *pCk); + +/* +** Do the next step of the integrity-check operation specified by the handle +** passed as the only argument. This function returns SQLITE_DONE if the +** integrity-check operation is finished, or an SQLite error code if +** an error occurs, or SQLITE_OK if no error occurs but the integrity-check +** is not finished. It is not considered an error if database corruption +** is encountered. +** +** Following a successful call to sqlite3_intck_step() (one that returns +** SQLITE_OK), sqlite3_intck_message() returns a non-NULL value if +** corruption was detected in the db. +** +** If an error occurs and a value other than SQLITE_OK or SQLITE_DONE is +** returned, then the integrity-check handle is placed in an error state. +** In this state all subsequent calls to sqlite3_intck_step() or +** sqlite3_intck_unlock() will immediately return the same error. The +** sqlite3_intck_error() method may be used to obtain an English language +** error message in this case. +*/ +int sqlite3_intck_step(sqlite3_intck *pCk); + +/* +** If the previous call to sqlite3_intck_step() encountered corruption +** within the database, then this function returns a pointer to a buffer +** containing a nul-terminated string describing the corruption in +** English. If the previous call to sqlite3_intck_step() did not encounter +** corruption, or if there was no previous call, this function returns +** NULL. +*/ +const char *sqlite3_intck_message(sqlite3_intck *pCk); + +/* +** Close any read-transaction opened by an earlier call to +** sqlite3_intck_step(). Any subsequent call to sqlite3_intck_step() will +** open a new transaction. Return SQLITE_OK if successful, or an SQLite error +** code otherwise. +** +** If an error occurs, then the integrity-check handle is placed in an error +** state. In this state all subsequent calls to sqlite3_intck_step() or +** sqlite3_intck_unlock() will immediately return the same error. The +** sqlite3_intck_error() method may be used to obtain an English language +** error message in this case. +*/ +int sqlite3_intck_unlock(sqlite3_intck *pCk); + +/* +** If an error has occurred in an earlier call to sqlite3_intck_step() +** or sqlite3_intck_unlock(), then this method returns the associated +** SQLite error code. Additionally, if pzErr is not NULL, then (*pzErr) +** may be set to point to a nul-terminated string containing an English +** language error message. Or, if no error message is available, to +** NULL. +** +** If no error has occurred within sqlite3_intck_step() or +** sqlite_intck_unlock() calls on the handle passed as the first argument, +** then SQLITE_OK is returned and (*pzErr) set to NULL. +*/ +int sqlite3_intck_error(sqlite3_intck *pCk, const char **pzErr); + +/* +** This API is used for testing only. It returns the full-text of an SQL +** statement used to test object zObj, which may be a table or index. +** The returned buffer is valid until the next call to either this function +** or sqlite3_intck_close() on the same sqlite3_intck handle. +*/ +const char *sqlite3_intck_test_sql(sqlite3_intck *pCk, const char *zObj); + + +#ifdef __cplusplus +} /* end of the 'extern "C"' block */ +#endif + +#endif /* ifndef _SQLITE_INTCK_H */ diff --git a/ext/intck/test_intck.c b/ext/intck/test_intck.c new file mode 100644 index 0000000000..d3a619b503 --- /dev/null +++ b/ext/intck/test_intck.c @@ -0,0 +1,233 @@ +/* +** 2010 August 28 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** Code for testing all sorts of SQLite interfaces. This code +** is not included in the SQLite library. +*/ + +#include "sqlite3.h" +#include "sqlite3intck.h" +#include "tclsqlite.h" +#include +#include + +/* In test1.c */ +int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb); +const char *sqlite3ErrName(int); + +typedef struct TestIntck TestIntck; +struct TestIntck { + sqlite3_intck *intck; +}; + +static int testIntckCmd( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + struct Subcmd { + const char *zName; + int nArg; + const char *zExpect; + } aCmd[] = { + {"close", 0, ""}, /* 0 */ + {"step", 0, ""}, /* 1 */ + {"message", 0, ""}, /* 2 */ + {"error", 0, ""}, /* 3 */ + {"unlock", 0, ""}, /* 4 */ + {"test_sql", 1, ""}, /* 5 */ + {0 , 0} + }; + int rc = TCL_OK; + int iIdx = -1; + TestIntck *p = (TestIntck*)clientData; + + if( objc<2 ){ + Tcl_WrongNumArgs(interp, 1, objv, "SUB-COMMAND ..."); + return TCL_ERROR; + } + + rc = Tcl_GetIndexFromObjStruct( + interp, objv[1], aCmd, sizeof(aCmd[0]), "SUB-COMMAND", 0, &iIdx + ); + if( rc ) return rc; + + if( objc!=2+aCmd[iIdx].nArg ){ + Tcl_WrongNumArgs(interp, 2, objv, aCmd[iIdx].zExpect); + return TCL_ERROR; + } + + switch( iIdx ){ + case 0: assert( 0==strcmp("close", aCmd[iIdx].zName) ); { + Tcl_DeleteCommand(interp, Tcl_GetStringFromObj(objv[0], 0)); + break; + } + + case 1: assert( 0==strcmp("step", aCmd[iIdx].zName) ); { + rc = sqlite3_intck_step(p->intck); + Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1)); + break; + } + + case 2: assert( 0==strcmp("message", aCmd[iIdx].zName) ); { + const char *z = sqlite3_intck_message(p->intck); + Tcl_SetObjResult(interp, Tcl_NewStringObj(z ? z : "", -1)); + break; + } + + case 3: assert( 0==strcmp("error", aCmd[iIdx].zName) ); { + const char *zErr = 0; + Tcl_Obj *pRes; + rc = sqlite3_intck_error(p->intck, 0); + pRes = Tcl_NewObj(); + Tcl_ListObjAppendElement( + interp, pRes, Tcl_NewStringObj(sqlite3ErrName(rc), -1) + ); + sqlite3_intck_error(p->intck, &zErr); + Tcl_ListObjAppendElement( + interp, pRes, Tcl_NewStringObj(zErr ? zErr : 0, -1) + ); + Tcl_SetObjResult(interp, pRes); + break; + } + + case 4: assert( 0==strcmp("unlock", aCmd[iIdx].zName) ); { + rc = sqlite3_intck_unlock(p->intck); + Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1)); + break; + } + + case 5: assert( 0==strcmp("test_sql", aCmd[iIdx].zName) ); { + const char *zObj = Tcl_GetString(objv[2]); + const char *zSql = sqlite3_intck_test_sql(p->intck, zObj[0] ? zObj : 0); + Tcl_SetObjResult(interp, Tcl_NewStringObj(zSql, -1)); + break; + } + } + + return TCL_OK; +} + +/* +** Destructor for commands created by test_sqlite3_intck(). +*/ +static void testIntckFree(void *clientData){ + TestIntck *p = (TestIntck*)clientData; + sqlite3_intck_close(p->intck); + ckfree(p); +} + +/* +** tclcmd: sqlite3_intck DB DBNAME +*/ +static int test_sqlite3_intck( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + char zName[64]; + int iName = 0; + Tcl_CmdInfo info; + TestIntck *p = 0; + sqlite3 *db = 0; + const char *zDb = 0; + int rc = SQLITE_OK; + + if( objc!=3 ){ + Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME"); + return TCL_ERROR; + } + + p = (TestIntck*)ckalloc(sizeof(TestIntck)); + memset(p, 0, sizeof(TestIntck)); + + if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ + return TCL_ERROR; + } + zDb = Tcl_GetString(objv[2]); + if( zDb[0]=='\0' ) zDb = 0; + + rc = sqlite3_intck_open(db, zDb, &p->intck); + if( rc!=SQLITE_OK ){ + ckfree(p); + Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3_errstr(rc), -1)); + return TCL_ERROR; + } + + do { + sprintf(zName, "intck%d", iName++); + }while( Tcl_GetCommandInfo(interp, zName, &info)!=0 ); + Tcl_CreateObjCommand(interp, zName, testIntckCmd, (void*)p, testIntckFree); + Tcl_SetObjResult(interp, Tcl_NewStringObj(zName, -1)); + + return TCL_OK; +} + +/* +** tclcmd: test_do_intck DB DBNAME +*/ +static int test_do_intck( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + sqlite3 *db = 0; + const char *zDb = 0; + int rc = SQLITE_OK; + sqlite3_intck *pCk = 0; + Tcl_Obj *pRet = 0; + const char *zErr = 0; + + if( objc!=3 ){ + Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME"); + return TCL_ERROR; + } + if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ + return TCL_ERROR; + } + zDb = Tcl_GetString(objv[2]); + + pRet = Tcl_NewObj(); + Tcl_IncrRefCount(pRet); + + rc = sqlite3_intck_open(db, zDb, &pCk); + if( rc==SQLITE_OK ){ + while( sqlite3_intck_step(pCk)==SQLITE_OK ){ + const char *zMsg = sqlite3_intck_message(pCk); + if( zMsg ){ + Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj(zMsg, -1)); + } + } + rc = sqlite3_intck_error(pCk, &zErr); + } + if( rc!=SQLITE_OK ){ + if( zErr ){ + Tcl_SetObjResult(interp, Tcl_NewStringObj(zErr, -1)); + }else{ + Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1)); + } + }else{ + Tcl_SetObjResult(interp, pRet); + } + Tcl_DecrRefCount(pRet); + sqlite3_intck_close(pCk); + sqlite3_intck_close(0); + return rc ? TCL_ERROR : TCL_OK; +} + +int Sqlitetestintck_Init(Tcl_Interp *interp){ + Tcl_CreateObjCommand(interp, "sqlite3_intck", test_sqlite3_intck, 0, 0); + Tcl_CreateObjCommand(interp, "test_do_intck", test_do_intck, 0, 0); + return TCL_OK; +} diff --git a/ext/jni/GNUmakefile b/ext/jni/GNUmakefile new file mode 100644 index 0000000000..f2fc7fb7bd --- /dev/null +++ b/ext/jni/GNUmakefile @@ -0,0 +1,505 @@ +# Quick-and-dirty makefile to bootstrap the sqlite3-jni project. This +# build assumes a Linux-like system. +default: all + +JAVA_HOME ?= $(HOME)/jdk/current +# e.g. /usr/lib/jvm/default-javajava-19-openjdk-amd64 +JDK_HOME ?= $(JAVA_HOME) +# ^^^ JDK_HOME is not as widely used as JAVA_HOME +bin.jar := $(JDK_HOME)/bin/jar +bin.java := $(JDK_HOME)/bin/java +bin.javac := $(JDK_HOME)/bin/javac +bin.javadoc := $(JDK_HOME)/bin/javadoc +ifeq (,$(wildcard $(JDK_HOME))) +$(error set JDK_HOME to the top-most dir of your JDK installation.) +endif +MAKEFILE := $(lastword $(MAKEFILE_LIST)) +$(MAKEFILE): + +package.jar := sqlite3-jni.jar + +dir.top := ../.. +dir.tool := ../../tool +dir.jni := $(patsubst %/,%,$(dir $(MAKEFILE))) +dir.src := $(dir.jni)/src +dir.src.c := $(dir.src)/c +dir.bld := $(dir.jni)/bld +dir.bld.c := $(dir.bld) +dir.src.jni := $(dir.src)/org/sqlite/jni +dir.src.capi := $(dir.src.jni)/capi +dir.src.fts5 := $(dir.src.jni)/fts5 +dir.tests := $(dir.src)/tests +mkdir ?= mkdir -p +$(dir.bld.c): + $(mkdir) $@ + +javac.flags ?= -Xlint:unchecked -Xlint:deprecation +java.flags ?= +javac.flags += -encoding utf8 +# -------------^^^^^^^^^^^^^^ required for Windows builds +jnicheck ?= 1 +ifeq (1,$(jnicheck)) + java.flags += -Xcheck:jni +endif + +classpath := $(dir.src) +CLEAN_FILES := $(package.jar) +DISTCLEAN_FILES := $(dir.jni)/*~ $(dir.src.c)/*~ $(dir.src.jni)/*~ + +sqlite3-jni.h := $(dir.src.c)/sqlite3-jni.h +.NOTPARALLEL: $(sqlite3-jni.h) +CApi.java := $(dir.src.capi)/CApi.java +SQLTester.java := $(dir.src.capi)/SQLTester.java +CApi.class := $(CApi.java:.java=.class) +SQLTester.class := $(SQLTester.java:.java=.class) + +######################################################################## +# The future of FTS5 customization in this API is as yet unclear. +# The pieces are all in place, and are all thin proxies so not much +# complexity, but some semantic changes were required in porting +# which are largely untested. +# +# Reminder: this flag influences the contents of $(sqlite3-jni.h), +# which is checked in. Please do not check in changes to that file in +# which the fts5 APIs have been stripped unless that feature is +# intended to be stripped for good. +enable.fts5 ?= 1 + +ifeq (,$(wildcard $(dir.tests)/*)) + enable.tester := 0 +else + enable.tester := 1 +endif + +# bin.version-info = binary to output various sqlite3 version info +# building the distribution zip file. +bin.version-info := $(dir.top)/version-info +.NOTPARALLEL: $(bin.version-info) +$(bin.version-info): $(dir.tool)/version-info.c $(sqlite3.h) $(dir.top)/Makefile + $(MAKE) -C $(dir.top) version-info + +# Be explicit about which Java files to compile so that we can work on +# in-progress files without requiring them to be in a compilable state. +JAVA_FILES.main := $(patsubst %,$(dir.src.jni)/annotation/%,\ + Experimental.java \ + NotNull.java \ + Nullable.java \ +) $(patsubst %,$(dir.src.capi)/%,\ + AbstractCollationCallback.java \ + AggregateFunction.java \ + AuthorizerCallback.java \ + AutoExtensionCallback.java \ + BusyHandlerCallback.java \ + CollationCallback.java \ + CollationNeededCallback.java \ + CommitHookCallback.java \ + ConfigLogCallback.java \ + ConfigSqlLogCallback.java \ + NativePointerHolder.java \ + OutputPointer.java \ + PrepareMultiCallback.java \ + PreupdateHookCallback.java \ + ProgressHandlerCallback.java \ + ResultCode.java \ + RollbackHookCallback.java \ + ScalarFunction.java \ + SQLFunction.java \ + CallbackProxy.java \ + CApi.java \ + TableColumnMetadata.java \ + TraceV2Callback.java \ + UpdateHookCallback.java \ + ValueHolder.java \ + WindowFunction.java \ + XDestroyCallback.java \ + sqlite3.java \ + sqlite3_blob.java \ + sqlite3_context.java \ + sqlite3_stmt.java \ + sqlite3_value.java \ +) $(patsubst %,$(dir.src.jni)/wrapper1/%,\ + AggregateFunction.java \ + ScalarFunction.java \ + SqlFunction.java \ + Sqlite.java \ + SqliteException.java \ + ValueHolder.java \ + WindowFunction.java \ +) + +JAVA_FILES.unittest := $(patsubst %,$(dir.src.jni)/%,\ + capi/Tester1.java \ + wrapper1/Tester2.java \ +) +ifeq (1,$(enable.fts5)) + JAVA_FILES.unittest += $(patsubst %,$(dir.src.fts5)/%,\ + TesterFts5.java \ + ) + JAVA_FILES.main += $(patsubst %,$(dir.src.fts5)/%,\ + fts5_api.java \ + fts5_extension_function.java \ + fts5_tokenizer.java \ + Fts5.java \ + Fts5Context.java \ + Fts5ExtensionApi.java \ + Fts5PhraseIter.java \ + Fts5Tokenizer.java \ + XTokenizeCallback.java \ + ) +endif +JAVA_FILES.tester := $(SQLTester.java) +JAVA_FILES.package.info := \ + $(dir.src.jni)/package-info.java \ + $(dir.src.jni)/annotation/package-info.java + +CLASS_FILES.main := $(JAVA_FILES.main:.java=.class) +CLASS_FILES.unittest := $(JAVA_FILES.unittest:.java=.class) +CLASS_FILES.tester := $(JAVA_FILES.tester:.java=.class) + +JAVA_FILES += $(JAVA_FILES.main) $(JAVA_FILES.unittest) +ifeq (1,$(enable.tester)) + JAVA_FILES += $(JAVA_FILES.tester) +endif + +CLASS_FILES := +define CLASSFILE_DEPS +all: $(1).class +$(1).class: $(1).java +CLASS_FILES += $(1).class +endef +$(foreach B,$(basename \ + $(JAVA_FILES.main) $(JAVA_FILES.unittest) $(JAVA_FILES.tester)),\ + $(eval $(call CLASSFILE_DEPS,$(B)))) +$(CLASS_FILES): $(MAKEFILE) + $(bin.javac) $(javac.flags) -h $(dir.bld.c) -cp $(classpath) $(JAVA_FILES) + +#.PHONY: classfiles + +######################################################################## +# Set up sqlite3.c and sqlite3.h... +# +# To build with SEE (https://sqlite.org/see), either put sqlite3-see.c +# in the top of this build tree or pass +# sqlite3.c=PATH_TO_sqlite3-see.c to the build. Note that only +# encryption modules with no 3rd-party dependencies will currently +# work here: AES256-OFB, AES128-OFB, and AES128-CCM. Not +# coincidentally, those 3 modules are included in the sqlite3-see.c +# bundle. +# +# A custom sqlite3.c must not have any spaces in its name. +# $(sqlite3.canonical.c) must point to the sqlite3.c in +# the sqlite3 canonical source tree, as that source file +# is required for certain utility and test code. +sqlite3.canonical.c := $(firstword $(wildcard $(dir.src.c)/sqlite3.c) $(dir.top)/sqlite3.c) +sqlite3.canonical.h := $(firstword $(wildcard $(dir.src.c)/sqlite3.h) $(dir.top)/sqlite3.h) +sqlite3.c := $(sqlite3.canonical.c) +sqlite3.h := $(sqlite3.canonical.h) +#ifeq (,$(shell grep sqlite3_activate_see $(sqlite3.c) 2>/dev/null)) +# SQLITE_C_IS_SEE := 0 +#else +# SQLITE_C_IS_SEE := 1 +# $(info This is an SEE build.) +#endif + +.NOTPARALLEL: $(sqlite3.h) +$(sqlite3.h): + $(MAKE) -C $(dir.top) sqlite3.c +$(sqlite3.c): $(sqlite3.h) + +opt.threadsafe ?= 1 +opt.fatal-oom ?= 1 +opt.debug ?= 1 +opt.metrics ?= 1 +SQLITE_OPT = \ + -DSQLITE_THREADSAFE=$(opt.threadsafe) \ + -DSQLITE_TEMP_STORE=2 \ + -DSQLITE_USE_URI=1 \ + -DSQLITE_OMIT_LOAD_EXTENSION \ + -DSQLITE_OMIT_DEPRECATED \ + -DSQLITE_OMIT_SHARED_CACHE \ + -DSQLITE_C=$(sqlite3.c) \ + -DSQLITE_JNI_FATAL_OOM=$(opt.fatal-oom) \ + -DSQLITE_JNI_ENABLE_METRICS=$(opt.metrics) + +opt.extras ?= 1 +ifeq (1,$(opt.extras)) +SQLITE_OPT += -DSQLITE_ENABLE_RTREE \ + -DSQLITE_ENABLE_EXPLAIN_COMMENTS \ + -DSQLITE_ENABLE_STMTVTAB \ + -DSQLITE_ENABLE_DBPAGE_VTAB \ + -DSQLITE_ENABLE_DBSTAT_VTAB \ + -DSQLITE_ENABLE_BYTECODE_VTAB \ + -DSQLITE_ENABLE_OFFSET_SQL_FUNC \ + -DSQLITE_ENABLE_PREUPDATE_HOOK \ + -DSQLITE_ENABLE_NORMALIZE \ + -DSQLITE_ENABLE_SQLLOG \ + -DSQLITE_ENABLE_COLUMN_METADATA +endif + +ifeq (1,$(opt.debug)) + SQLITE_OPT += -DSQLITE_DEBUG -g -DDEBUG -UNDEBUG +else + SQLITE_OPT += -Os +endif + +ifeq (1,$(enable.fts5)) + SQLITE_OPT += -DSQLITE_ENABLE_FTS5 +endif + +sqlite3-jni.c := $(dir.src.c)/sqlite3-jni.c +sqlite3-jni.o := $(dir.bld.c)/sqlite3-jni.o +sqlite3-jni.h := $(dir.src.c)/sqlite3-jni.h +package.dll := $(dir.bld.c)/libsqlite3-jni.so +# All javac-generated .h files must be listed in $(sqlite3-jni.h.in): +sqlite3-jni.h.in := +# $(java.with.jni) lists all Java files which contain JNI decls: +java.with.jni := +define ADD_JNI_H +sqlite3-jni.h.in += $$(dir.bld.c)/org_sqlite_jni$(3)_$(2).h +java.with.jni += $(1)/$(2).java +$$(dir.bld.c)/org_sqlite_jni$(3)_$(2).h: $(1)/$(2).java +endef +# Invoke ADD_JNI_H once for each Java file which includes JNI +# declarations: +$(eval $(call ADD_JNI_H,$(dir.src.capi),CApi,_capi)) +$(eval $(call ADD_JNI_H,$(dir.src.capi),SQLTester,_capi)) +ifeq (1,$(enable.fts5)) + $(eval $(call ADD_JNI_H,$(dir.src.fts5),Fts5ExtensionApi,_fts5)) + $(eval $(call ADD_JNI_H,$(dir.src.fts5),fts5_api,_fts5)) + $(eval $(call ADD_JNI_H,$(dir.src.fts5),fts5_tokenizer,_fts5)) +endif +$(sqlite3-jni.h.in): $(dir.bld.c) + +#package.dll.cfiles := +package.dll.cflags = \ + -std=c99 \ + -fPIC \ + -I. \ + -I$(dir $(sqlite3.h)) \ + -I$(dir.src.c) \ + -I$(JDK_HOME)/include \ + $(patsubst %,-I%,$(patsubst %.h,,$(wildcard $(JDK_HOME)/include/*))) \ + -Wall +# The gross $(patsubst...) above is to include the platform-specific +# subdir which lives under $(JDK_HOME)/include and is a required +# include path for client-level code. +# +# Using (-Wall -Wextra) triggers an untennable number of +# gcc warnings from sqlite3.c for mundane things like +# unused parameters. +######################################################################## +ifeq (1,$(enable.tester)) + package.dll.cflags += -DSQLITE_JNI_ENABLE_SQLTester +endif + +$(sqlite3-jni.h): $(sqlite3-jni.h.in) $(MAKEFILE) + @cat $(sqlite3-jni.h.in) > $@.tmp + @if cmp $@ $@.tmp >/dev/null; then \ + rm -f $@.tmp; \ + echo "$@ not modified"; \ + else \ + mv $@.tmp $@; \ + echo "Updated $@"; \ + fi + @if [ x1 != x$(enable.fts5) ]; then \ + echo "*** REMINDER:"; \ + echo "*** enable.fts5=0, so please do not check in changes to $@."; \ + fi + +$(package.dll): $(sqlite3-jni.h) $(sqlite3.c) $(sqlite3.h) +$(package.dll): $(sqlite3-jni.c) $(MAKEFILE) + $(CC) $(package.dll.cflags) $(SQLITE_OPT) \ + $(sqlite3-jni.c) -shared -o $@ +all: $(package.dll) + +.PHONY: test test-one +Tester1.flags ?= +Tester2.flags ?= +test.flags.jvm = -ea -Djava.library.path=$(dir.bld.c) \ + $(java.flags) -cp $(classpath) +test.deps := $(CLASS_FILES) $(package.dll) +test-one: $(test.deps) + $(bin.java) $(test.flags.jvm) org.sqlite.jni.capi.Tester1 $(Tester1.flags) + $(bin.java) $(test.flags.jvm) org.sqlite.jni.wrapper1.Tester2 $(Tester2.flags) +test-sqllog: $(test.deps) + @echo "Testing with -sqllog..." + $(bin.java) $(test.flags.jvm) org.sqlite.jni.capi.Tester1 $(Tester1.flags) -sqllog +test-mt: $(test.deps) + @echo "Testing in multi-threaded mode:"; + $(bin.java) $(test.flags.jvm) org.sqlite.jni.capi.Tester1 \ + -t 7 -r 50 -shuffle $(Tester1.flags) + $(bin.java) $(test.flags.jvm) org.sqlite.jni.wrapper1.Tester2 \ + -t 7 -r 50 -shuffle $(Tester2.flags) + +test: test-one test-mt +tests: test test-sqllog + +tester.scripts := $(sort $(wildcard $(dir.src)/tests/*.test)) +tester.flags ?= # --verbose +.PHONY: tester tester-local tester-ext +ifeq (1,$(enable.tester)) +tester-local: $(CLASS_FILES.tester) $(package.dll) + $(bin.java) -ea -Djava.library.path=$(dir.bld.c) \ + $(java.flags) -cp $(classpath) \ + org.sqlite.jni.capi.SQLTester $(tester.flags) $(tester.scripts) +tester: tester-local +else +tester: + @echo "SQLTester support is disabled." +endif + +tester.extdir.default := $(dir.tests)/ext +tester.extdir ?= $(tester.extdir.default) +tester.extern-scripts := $(wildcard $(tester.extdir)/*.test) +ifneq (,$(tester.extern-scripts)) +tester-ext: + $(bin.java) -ea -Djava.library.path=$(dir.bld.c) \ + $(java.flags) -cp $(classpath) \ + org.sqlite.jni.capi.SQLTester $(tester.flags) $(tester.extern-scripts) +else +tester-ext: + @echo "******************************************************"; \ + echo "*** Include the out-of-tree test suite in the 'tester'"; \ + echo "*** target by either symlinking its directory to"; \ + echo "*** $(tester.extdir.default) or passing it to make"; \ + echo "*** as tester.extdir=/path/to/that/dir."; \ + echo "******************************************************"; +endif + +tester-ext: tester-local +tester: tester-ext +tests: tester +######################################################################## +# Build each SQLITE_THREADMODE variant and run all tests against them. +multitest: clean +define MULTIOPT +multitest: multitest-$(1) +multitest-$(1): + $$(MAKE) opt.debug=$$(opt.debug) $(patsubst %,opt.%,$(2)) \ + tests clean enable.fts5=1 +endef + +$(eval $(call MULTIOPT,01,threadsafe=0 oom=1)) +$(eval $(call MULTIOPT,00,threadsafe=0 oom=0)) +$(eval $(call MULTIOPT,11,threadsafe=1 oom=1)) +$(eval $(call MULTIOPT,10,threadsafe=1 oom=0)) +$(eval $(call MULTIOPT,21,threadsafe=2 oom=1)) +$(eval $(call MULTIOPT,20,threadsafe=2 oom=0)) + + +######################################################################## +# jar bundle... +package.jar.in := $(abspath $(dir.src)/jar.in) +CLEAN_FILES += $(package.jar.in) +JAVA_FILES.jar := $(JAVA_FILES.main) $(JAVA_FILES.unittest) $(JAVA_FILES.package.info) +CLASS_FILES.jar := $(filter-out %/package-info.class,$(JAVA_FILES.jar:.java=.class)) +$(package.jar.in): $(package.dll) $(MAKEFILE) + ls -1 \ + $(dir.src.jni)/*/*.java $(dir.src.jni)/*/*.class \ + | sed -e 's,^$(dir.src)/,,' | sort > $@ + +$(package.jar): $(CLASS_FILES.jar) $(MAKEFILE) $(package.jar.in) + @rm -f $(dir.src)/c/*~ $(dir.src.jni)/*~ + cd $(dir.src); $(bin.jar) -cfe ../$@ org.sqlite.jni.capi.Tester1 @$(package.jar.in) + @ls -la $@ + @echo "To use this jar you will need the -Djava.library.path=DIR/CONTAINING/libsqlite3-jni.so flag." + @echo "e.g. java -Djava.library.path=bld -jar $@" + +jar: $(package.jar) +run-jar: $(package.jar) $(package.dll) + $(bin.java) -Djava.library.path=$(dir.bld) -jar $(package.jar) $(run-jar.flags) + +######################################################################## +# javadoc... +dir.doc := $(dir.jni)/javadoc +doc.index := $(dir.doc)/index.html +javadoc.exclude := -exclude org.sqlite.jni.fts5 +# ^^^^ 2023-09-13: elide the fts5 parts from the public docs for +# the time being, as it's not clear where the Java bindings for +# those bits are going. +# javadoc.exclude += -exclude org.sqlite.jni.capi +# ^^^^ exclude the capi API only for certain builds (TBD) +$(doc.index): $(JAVA_FILES.main) $(MAKEFILE) + @if [ -d $(dir.doc) ]; then rm -fr $(dir.doc)/*; fi + $(bin.javadoc) -cp $(classpath) -d $(dir.doc) -quiet \ + -subpackages org.sqlite.jni $(javadoc.exclude) + @echo "javadoc output is in $@" + +.PHONY: doc javadoc docserve +.FORCE: doc +doc: $(doc.index) +javadoc: $(doc.index) +# Force rebuild of docs +redoc: + @rm -f $(doc.index) + @$(MAKE) doc +docserve: $(doc.index) + cd $(dir.doc) && althttpd -max-age 1 -page index.html +######################################################################## +# Clean up... +CLEAN_FILES += $(dir.bld.c)/* \ + $(dir.src.jni)/*.class \ + $(dir.src.jni)/*/*.class \ + $(package.dll) \ + hs_err_pid*.log + +.PHONY: clean distclean +clean: + -rm -f $(CLEAN_FILES) +distclean: clean + -rm -f $(DISTCLEAN_FILES) + -rm -fr $(dir.bld.c) $(dir.doc) + +######################################################################## +# distribution bundle rules... + +ifeq (,$(filter snapshot,$(MAKECMDGOALS))) +dist-name-prefix := sqlite-jni +else +dist-name-prefix := sqlite-jni-snapshot-$(shell /usr/bin/date +%Y%m%d) +endif +dist-name := $(dist-name-prefix)-TEMP + + +dist-dir.top := $(dist-name) +dist-dir.src := $(dist-dir.top)/src +dist.top.extras := \ + README.md + +.PHONY: dist snapshot + +dist: \ + $(bin.version-info) $(sqlite3.canonical.c) \ + $(package.jar) $(MAKEFILE) + @echo "Making end-user deliverables..." + @echo "****************************************************************************"; \ + echo "*** WARNING: be sure to build this with JDK8 (javac 1.8) for compatibility."; \ + echo "*** reasons!"; $$($(bin.javac) -version); \ + echo "****************************************************************************" + @rm -fr $(dist-dir.top) + @mkdir -p $(dist-dir.src) + @cp -p $(dist.top.extras) $(dist-dir.top)/. + @cp -p jar-dist.make $(dist-dir.top)/Makefile + @cp -p $(dir.src.c)/*.[ch] $(dist-dir.src)/. + @cp -p $(sqlite3.canonical.c) $(sqlite3.canonical.h) $(dist-dir.src)/. + @set -e; \ + vnum=$$($(bin.version-info) --download-version); \ + vjar=$$($(bin.version-info) --version); \ + vdir=$(dist-name-prefix)-$$vnum; \ + arczip=$$vdir.zip; \ + cp -p $(package.jar) $(dist-dir.top)/sqlite3-jni-$${vjar}.jar; \ + echo "Making $$arczip ..."; \ + rm -fr $$arczip $$vdir; \ + mv $(dist-dir.top) $$vdir; \ + zip -qr $$arczip $$vdir; \ + rm -fr $$vdir; \ + ls -la $$arczip; \ + set +e; \ + unzip -lv $$arczip || echo "Missing unzip app? Not fatal." + +snapshot: dist + +.PHONY: dist-clean +clean: dist-clean +dist-clean: + rm -fr $(dist-name) $(wildcard sqlite-jni-*.zip) diff --git a/ext/jni/README.md b/ext/jni/README.md new file mode 100644 index 0000000000..5ad79fce9e --- /dev/null +++ b/ext/jni/README.md @@ -0,0 +1,316 @@ +SQLite3 via JNI +======================================================================== + +This directory houses a Java Native Interface (JNI) binding for the +sqlite3 API. If you are reading this from the distribution ZIP file, +links to resources in the canonical source tree will note work. The +canonical copy of this file can be browsed at: + + + +Technical support is available in the forum: + + + + +> **FOREWARNING:** this subproject is very much in development and + subject to any number of changes. Please do not rely on any + information about its API until this disclaimer is removed. The JNI + bindings released with version 3.43 are a "tech preview." Once + finalized, strong backward compatibility guarantees will apply. + +Project goals/requirements: + +- A [1-to-1(-ish) mapping of the C API](#1to1ish) to Java via JNI, + insofar as cross-language semantics allow for. A closely-related + goal is that [the C documentation](https://sqlite.org/c3ref/intro.html) + should be usable as-is, insofar as possible, for the JNI binding. + +- Support Java as far back as version 8 (2014). + +- Environment-independent. Should work everywhere both Java + and SQLite3 do. + +- No 3rd-party dependencies beyond the JDK. That includes no + build-level dependencies for specific IDEs and toolchains. We + welcome the addition of build files for arbitrary environments + insofar as they neither interfere with each other nor become + a maintenance burden for the sqlite developers. + +Non-goals: + +- Creation of high-level OO wrapper APIs. Clients are free to create + them off of the C-style API. + +- Virtual tables are unlikely to be supported due to the amount of + glue code needed to fit them into Java. + +- Support for mixed-mode operation, where client code accesses SQLite + both via the Java-side API and the C API via their own native + code. Such cases would be a minefield of potential mis-interactions + between this project's JNI bindings and mixed-mode client code. + + +Hello World +----------------------------------------------------------------------- + +```java +import org.sqlite.jni.*; +import static org.sqlite.jni.CApi.*; + +... + +final sqlite3 db = sqlite3_open(":memory:"); +try { + final int rc = sqlite3_errcode(db); + if( 0 != rc ){ + if( null != db ){ + System.out.print("Error opening db: "+sqlite3_errmsg(db)); + }else{ + System.out.print("Error opening db: rc="+rc); + } + ... handle error ... + } + // ... else use the db ... +}finally{ + // ALWAYS close databases using sqlite3_close() or sqlite3_close_v2() + // when done with them. All of their active statement handles must + // first have been passed to sqlite3_finalize(). + sqlite3_close_v2(db); +} +``` + + +Building +======================================================================== + +The canonical builds assumes a Linux-like environment and requires: + +- GNU Make +- A JDK supporting Java 8 or higher +- A modern C compiler. gcc and clang should both work. + +Put simply: + +```console +$ export JAVA_HOME=/path/to/jdk/root +$ make +$ make test +$ make clean +``` + +The jar distribution can be created with `make jar`, but note that it +does not contain the binary DLL file. A different DLL is needed for +each target platform. + + + +One-to-One(-ish) Mapping to C +======================================================================== + +This JNI binding aims to provide as close to a 1-to-1 experience with +the C API as cross-language semantics allow. Interface changes are +necessarily made where cross-language semantics do not allow a 1-to-1, +and judiciously made where a 1-to-1 mapping would be unduly cumbersome +to use in Java. In all cases, this binding makes every effort to +provide semantics compatible with the C API documentation even if the +interface to those semantics is slightly different. Any cases which +deviate from those semantics (either removing or adding semantics) are +clearly documented. + +Where it makes sense to do so for usability, Java-side overloads are +provided which accept or return data in alternative forms or provide +sensible default argument values. In all such cases they are thin +proxies around the corresponding C APIs and do not introduce new +semantics. + +In a few cases, Java-specific capabilities have been added in +new APIs, all of which have "_java" somewhere in their names. +Examples include: + +- `sqlite3_result_java_object()` +- `sqlite3_column_java_object()` +- `sqlite3_value_java_object()` + +which, as one might surmise, collectively enable the passing of +arbitrary Java objects from user-defined SQL functions through to the +caller. + + +Golden Rule: Garbage Collection Cannot Free SQLite Resources +------------------------------------------------------------------------ + +It is important that all databases and prepared statement handles get +cleaned up by client code. A database cannot be closed if it has open +statement handles. `sqlite3_close()` fails if the db cannot be closed +whereas `sqlite3_close_v2()` recognizes that case and marks the db as +a "zombie," pending finalization when the library detects that all +pending statements have been closed. Be aware that Java garbage +collection _cannot_ close a database or finalize a prepared statement. +Those things require explicit API calls. + +Classes for which it is sensible support Java's `AutoCloseable` +interface so can be used with try-with-resources constructs. + + +Golden Rule #2: _Never_ Throw from Callbacks (Unless...) +------------------------------------------------------------------------ + +All routines in this API, barring explicitly documented exceptions, +retain C-like semantics. For example, they are not permitted to throw +or propagate exceptions and must return error information (if any) via +result codes or `null`. The only cases where the C-style APIs may +throw is through client-side misuse, e.g. passing in a null where it +may cause a `NullPointerException`. The APIs clearly mark function +parameters which should not be null, but does not generally actively +defend itself against such misuse. Some C-style APIs explicitly accept +`null` as a no-op for usability's sake, and some of the JNI APIs +deliberately return an error code, instead of segfaulting, when passed +a `null`. + +Client-defined callbacks _must never throw exceptions_ unless _very +explicitly documented_ as being throw-safe. Exceptions are generally +reserved for higher-level bindings which are constructed to +specifically deal with them and ensure that they do not leak C-level +resources. In some cases, callback handlers are permitted to throw, in +which cases they get translated to C-level result codes and/or +messages. If a callback which is not permitted to throw throws, its +exception may trigger debug output but will otherwise be suppressed. + +The reason some callbacks are permitted to throw and others not is +because all such callbacks act as proxies for C function callback +interfaces and some of those interfaces have no error-reporting +mechanism. Those which are capable of propagating errors back through +the library convert exceptions from callbacks into corresponding +C-level error information. Those which cannot propagate errors +necessarily suppress any exceptions in order to maintain the C-style +semantics of the APIs. + + +Unwieldy Constructs are Re-mapped +------------------------------------------------------------------------ + +Some constructs, when modelled 1-to-1 from C to Java, are unduly +clumsy to work with in Java because they try to shoehorn C's way of +doing certain things into Java's wildly different ways. The following +subsections cover those, starting with a verbose explanation and +demonstration of where such changes are "really necessary"... + +### Custom Collations + +A prime example of where interface changes for Java are necessary for +usability is [registration of a custom +collation](https://sqlite.org/c3ref/create_collation.html): + +```c +// C: +int sqlite3_create_collation(sqlite3 * db, const char * name, int eTextRep, + void *pUserData, + int (*xCompare)(void*,int,void const *,int,void const *)); + +int sqlite3_create_collation_v2(sqlite3 * db, const char * name, int eTextRep, + void *pUserData, + int (*xCompare)(void*,int,void const *,int,void const *), + void (*xDestroy)(void*)); +``` + +The `pUserData` object is optional client-defined state for the +`xCompare()` and/or `xDestroy()` callback functions, both of which are +passed that object as their first argument. That data is passed around +"externally" in C because that's how C models the world. If we were to +bind that part as-is to Java, the result would be awkward to use (^Yes, +we tried this.): + +```java +// Java: +int sqlite3_create_collation(sqlite3 db, String name, int eTextRep, + Object pUserData, xCompareType xCompare); + +int sqlite3_create_collation_v2(sqlite3 db, String name, int eTextRep, + Object pUserData, + xCompareType xCompare, xDestroyType xDestroy); +``` + +The awkwardness comes from (A) having two distinctly different objects +for callbacks and (B) having their internal state provided separately, +which is ill-fitting in Java. For the sake of usability, C APIs which +follow that pattern use a slightly different Java interface: + +```java +int sqlite3_create_collation(sqlite3 db, String name, int eTextRep, + SomeCallbackType collation); +``` + +Where the `Collation` class has an abstract `call()` method and +no-op `xDestroy()` method which can be overridden if needed, leading to +a much more Java-esque usage: + +```java +int rc = sqlite3_create_collation(db, "mycollation", SQLITE_UTF8, new SomeCallbackType(){ + + // Required comparison function: + @Override public int call(byte[] lhs, byte[] rhs){ ... } + + // Optional finalizer function: + @Override public void xDestroy(){ ... } + + // Optional local state: + private String localState1 = + "This is local state. There are many like it, but this one is mine."; + private MyStateType localState2 = new MyStateType(); + ... +}); +``` + +Noting that: + +- It is possible to bind in call-scope-local state via closures, if + desired, as opposed to packing it into the Collation object. + +- No capabilities of the C API are lost or unduly obscured via the + above API reshaping, so power users need not make any compromises. + +- In the specific example above, `sqlite3_create_collation_v2()` + becomes superfluous because the provided interface effectively + provides both the v1 and v2 interfaces, the difference being that + overriding the `xDestroy()` method effectively gives it v2 + semantics. + + +### User-defined SQL Functions (a.k.a. UDFs) + +The [`sqlite3_create_function()`](https://sqlite.org/c3ref/create_function.html) +family of APIs make heavy use of function pointers to provide +client-defined callbacks, necessitating interface changes in the JNI +binding. The Java API has only one core function-registration function: + +```java +int sqlite3_create_function(sqlite3 db, String funcName, int nArgs, + int encoding, SQLFunction func); +``` + +> Design question: does the encoding argument serve any purpose in + Java? That's as-yet undetermined. If not, it will be removed. + +`SQLFunction` is not used directly, but is instead instantiated via +one of its three subclasses: + +- `ScalarFunction` implements simple scalar functions using but a + single callback. +- `AggregateFunction` implements aggregate functions using two + callbacks. +- `WindowFunction` implements window functions using four + callbacks. + +Search [`Tester1.java`](/file/ext/jni/src/org/sqlite/jni/capi/Tester1.java) for +`SQLFunction` for how it's used. + +Reminder: see the disclaimer at the top of this document regarding the +in-flux nature of this API. + +### And so on... + +Various APIs which accept callbacks, e.g. `sqlite3_trace_v2()` and +`sqlite3_update_hook()`, use interfaces similar to those shown above. +Despite the changes in signature, the JNI layer makes every effort to +provide the same semantics as the C API documentation suggests. diff --git a/ext/jni/jar-dist.make b/ext/jni/jar-dist.make new file mode 100644 index 0000000000..7596c99f3f --- /dev/null +++ b/ext/jni/jar-dist.make @@ -0,0 +1,60 @@ +#!/this/is/make +#^^^^ help emacs out +# +# This is a POSIX-make-compatible makefile for building the sqlite3 +# JNI library from "dist" zip file. It must be edited to set the +# proper top-level JDK directory and, depending on the platform, add a +# platform-specific -I directory. It should build as-is with any +# 2020s-era version of gcc or clang. It requires JDK version 8 or +# higher and that JAVA_HOME points to the top-most installation +# directory of that JDK. On Ubuntu-style systems the JDK is typically +# installed under /usr/lib/jvm/java-VERSION-PLATFORM. + +default: all + +JAVA_HOME = /usr/lib/jvm/java-1.8.0-openjdk-amd64 +CFLAGS = \ + -fPIC \ + -Isrc \ + -I$(JAVA_HOME)/include \ + -I$(JAVA_HOME)/include/linux \ + -I$(JAVA_HOME)/include/apple \ + -I$(JAVA_HOME)/include/bsd \ + -Wall + +SQLITE_OPT = \ + -DSQLITE_ENABLE_RTREE \ + -DSQLITE_ENABLE_EXPLAIN_COMMENTS \ + -DSQLITE_ENABLE_STMTVTAB \ + -DSQLITE_ENABLE_DBPAGE_VTAB \ + -DSQLITE_ENABLE_DBSTAT_VTAB \ + -DSQLITE_ENABLE_BYTECODE_VTAB \ + -DSQLITE_ENABLE_OFFSET_SQL_FUNC \ + -DSQLITE_OMIT_LOAD_EXTENSION \ + -DSQLITE_OMIT_DEPRECATED \ + -DSQLITE_OMIT_SHARED_CACHE \ + -DSQLITE_THREADSAFE=1 \ + -DSQLITE_TEMP_STORE=2 \ + -DSQLITE_USE_URI=1 \ + -DSQLITE_ENABLE_FTS5 \ + -DSQLITE_DEBUG + +sqlite3-jni.dll = libsqlite3-jni.so +$(sqlite3-jni.dll): + @echo "************************************************************************"; \ + echo "*** If this fails to build, be sure to edit this makefile ***"; \ + echo "*** to configure it for your system. ***"; \ + echo "************************************************************************" + $(CC) $(CFLAGS) $(SQLITE_OPT) \ + src/sqlite3-jni.c -shared -o $@ + @echo "Now try running it with: make test" + +test.flags = -Djava.library.path=. sqlite3-jni-*.jar +test: $(sqlite3-jni.dll) + java -jar $(test.flags) + java -jar $(test.flags) -t 7 -r 10 -shuffle + +clean: + -rm -f $(sqlite3-jni.dll) + +all: $(sqlite3-jni.dll) diff --git a/ext/jni/src/c/sqlite3-jni.c b/ext/jni/src/c/sqlite3-jni.c new file mode 100644 index 0000000000..f130eff042 --- /dev/null +++ b/ext/jni/src/c/sqlite3-jni.c @@ -0,0 +1,6360 @@ +/* +** 2023-07-21 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file implements the JNI bindings declared in +** org.sqlite.jni.capi.CApi (from which sqlite3-jni.h is generated). +*/ + +/* +** If you found this comment by searching the code for +** CallStaticObjectMethod because it appears in console output then +** you're probably the victim of an OpenJDK bug: +** +** https://bugs.openjdk.org/browse/JDK-8130659 +** +** It's known to happen with OpenJDK v8 but not with v19. It was +** triggered by this code long before it made any use of +** CallStaticObjectMethod(). +*/ + +/* +** Define any SQLITE_... config defaults we want if they aren't +** overridden by the builder. Please keep these alphabetized. +*/ + +/**********************************************************************/ +/* SQLITE_D... */ +#ifndef SQLITE_DEFAULT_CACHE_SIZE +# define SQLITE_DEFAULT_CACHE_SIZE -16384 +#endif +#if !defined(SQLITE_DEFAULT_PAGE_SIZE) +# define SQLITE_DEFAULT_PAGE_SIZE 8192 +#endif +#ifndef SQLITE_DQS +# define SQLITE_DQS 0 +#endif + +/**********************************************************************/ +/* SQLITE_ENABLE_... */ +/* +** Unconditionally enable API_ARMOR in the JNI build. It ensures that +** public APIs behave predictable in the face of passing illegal NULLs +** or ranges which might otherwise invoke undefined behavior. +*/ +#undef SQLITE_ENABLE_API_ARMOR +#define SQLITE_ENABLE_API_ARMOR 1 + +#ifndef SQLITE_ENABLE_BYTECODE_VTAB +# define SQLITE_ENABLE_BYTECODE_VTAB 1 +#endif +#ifndef SQLITE_ENABLE_DBPAGE_VTAB +# define SQLITE_ENABLE_DBPAGE_VTAB 1 +#endif +#ifndef SQLITE_ENABLE_DBSTAT_VTAB +# define SQLITE_ENABLE_DBSTAT_VTAB 1 +#endif +#ifndef SQLITE_ENABLE_EXPLAIN_COMMENTS +# define SQLITE_ENABLE_EXPLAIN_COMMENTS 1 +#endif +#ifndef SQLITE_ENABLE_MATH_FUNCTIONS +# define SQLITE_ENABLE_MATH_FUNCTIONS 1 +#endif +#ifndef SQLITE_ENABLE_OFFSET_SQL_FUNC +# define SQLITE_ENABLE_OFFSET_SQL_FUNC 1 +#endif +#ifndef SQLITE_ENABLE_RTREE +# define SQLITE_ENABLE_RTREE 1 +#endif +//#ifndef SQLITE_ENABLE_SESSION +//# define SQLITE_ENABLE_SESSION 1 +//#endif +#ifndef SQLITE_ENABLE_STMTVTAB +# define SQLITE_ENABLE_STMTVTAB 1 +#endif +//#ifndef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION +//# define SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION +//#endif + +/**********************************************************************/ +/* SQLITE_J... */ +#ifdef SQLITE_JNI_FATAL_OOM +#if !SQLITE_JNI_FATAL_OOM +#undef SQLITE_JNI_FATAL_OOM +#endif +#endif + +/**********************************************************************/ +/* SQLITE_O... */ +#ifndef SQLITE_OMIT_DEPRECATED +# define SQLITE_OMIT_DEPRECATED 1 +#endif +#ifndef SQLITE_OMIT_LOAD_EXTENSION +# define SQLITE_OMIT_LOAD_EXTENSION 1 +#endif +#ifndef SQLITE_OMIT_SHARED_CACHE +# define SQLITE_OMIT_SHARED_CACHE 1 +#endif +#ifdef SQLITE_OMIT_UTF16 +/* UTF16 is required for java */ +# undef SQLITE_OMIT_UTF16 1 +#endif + +/**********************************************************************/ +/* SQLITE_S... */ +#ifndef SQLITE_STRICT_SUBTYPE +# define SQLITE_STRICT_SUBTYPE 1 +#endif + +/**********************************************************************/ +/* SQLITE_T... */ +#ifndef SQLITE_TEMP_STORE +# define SQLITE_TEMP_STORE 2 +#endif +#ifndef SQLITE_THREADSAFE +# define SQLITE_THREADSAFE 1 +#endif + +/**********************************************************************/ +/* SQLITE_USE_... */ +#ifndef SQLITE_USE_URI +# define SQLITE_USE_URI 1 +#endif + + +/* +** Which sqlite3.c we're using needs to be configurable to enable +** building against a custom copy, e.g. the SEE variant. We have to +** include sqlite3.c, as opposed to sqlite3.h, in order to get access +** to some internal details like SQLITE_MAX_... and friends. This +** increases the rebuild time considerably but we need this in order +** to access some internal functionality and keep the to-Java-exported +** values of SQLITE_MAX_... and SQLITE_LIMIT_... in sync with the C +** build. +*/ +#ifndef SQLITE_C +# define SQLITE_C sqlite3.c +#endif +#define INC__STRINGIFY_(f) #f +#define INC__STRINGIFY(f) INC__STRINGIFY_(f) +#include INC__STRINGIFY(SQLITE_C) +#undef INC__STRINGIFY_ +#undef INC__STRINGIFY +#undef SQLITE_C + +/* +** End of the sqlite3 lib setup. What follows is JNI-specific. +*/ + +#include "sqlite3-jni.h" +#include +#include /* only for testing/debugging */ +#include /* intptr_t for 32-bit builds */ + +/* Only for debugging */ +#define MARKER(pfexp) \ + do{ printf("MARKER: %s:%d:%s():\t",__FILE__,__LINE__,__func__); \ + printf pfexp; \ + } while(0) + +/* +** Creates a verbose JNI function name. Suffix must be +** the JNI-mangled form of the function's name, minus the +** prefix seen in this macro. +** +** If you get java.lang.UnsatisfiedLinkError when calling newly-added +** native bindings, be sure that the mangled name is correct. It can +** be found in the generated sqlite3-jni.h. +*/ +#define JniFuncName(Suffix) \ + Java_org_sqlite_jni_capi_CApi_sqlite3_ ## Suffix + +/* Prologue for JNI function declarations and definitions. */ +#define JniDecl(ReturnType,Suffix) \ + JNIEXPORT ReturnType JNICALL JniFuncName(Suffix) + +/* +** S3JniApi's intent is that CFunc be the name(s) of the C API func(s) +** the being-declared JNI function is wrapping, making it easier to +** find those bindings' JNI-side entry points. The other args are for +** JniDecl. See the many examples in this file. +*/ +#define S3JniApi(CFunc,ReturnType,Suffix) JniDecl(ReturnType,Suffix) + +/* +** S3JniCast_L2P and P2L cast jlong (64-bit) to/from pointers. This is +** required for casting warning-free on 32-bit builds, where we +** otherwise get complaints that we're casting between different-sized +** int types. +** +** This use of intptr_t is the _only_ reason we require +** which, in turn, requires building with -std=c99 (or later). +** +** See also: the notes for LongPtrGet_T. +*/ +#define S3JniCast_L2P(JLongAsPtr) (void*)((intptr_t)(JLongAsPtr)) +#define S3JniCast_P2L(PTR) (jlong)((intptr_t)(PTR)) + +/* +** Shortcuts for the first 2 parameters to all JNI bindings. +** +** The type of the jSelf arg differs, but no docs seem to mention +** this: for static methods it's of type jclass and for non-static +** it's jobject. jobject actually works for all funcs, in the sense +** that it compiles and runs so long as we don't use jSelf (which is +** only rarely needed in this code), but to be pedantically correct we +** need the proper type in the signature. +** +** https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/design.html#jni_interface_functions_and_pointers +*/ +#define JniArgsEnvObj JNIEnv * env, jobject jSelf +#define JniArgsEnvClass JNIEnv * env, jclass jKlazz +/* +** Helpers to account for -Xcheck:jni warnings about not having +** checked for exceptions. +*/ +#define S3JniIfThrew if( (*env)->ExceptionCheck(env) ) +#define S3JniExceptionClear (*env)->ExceptionClear(env) +#define S3JniExceptionReport (*env)->ExceptionDescribe(env) +#define S3JniExceptionIgnore S3JniIfThrew S3JniExceptionClear +#define S3JniExceptionWarnIgnore \ + S3JniIfThrew {S3JniExceptionReport; S3JniExceptionClear;}(void)0 +#define S3JniExceptionWarnCallbackThrew(STR) \ + MARKER(("WARNING: " STR " MUST NOT THROW.\n")); \ + (*env)->ExceptionDescribe(env) + +/** To be used for cases where we're _really_ not expecting an + exception, e.g. looking up well-defined Java class members. */ +#define S3JniExceptionIsFatal(MSG) S3JniIfThrew {\ + S3JniExceptionReport; S3JniExceptionClear; \ + (*env)->FatalError(env, MSG); \ + } + +/* +** Declares local var env = s3jni_env(). All JNI calls involve a +** JNIEnv somewhere, always named env, and many of our macros assume +** env is in scope. Where it's not, but should be, use this to make it +** so. +*/ +#define S3JniDeclLocal_env JNIEnv * const env = s3jni_env() + +/* Fail fatally with an OOM message. */ +static inline void s3jni_oom(JNIEnv * const env){ + (*env)->FatalError(env, "SQLite3 JNI is out of memory.") /* does not return */; +} + +/* +** sqlite3_malloc() proxy which fails fatally on OOM. This should +** only be used for routines which manage global state and have no +** recovery strategy for OOM. For sqlite3 API which can reasonably +** return SQLITE_NOMEM, s3jni_malloc() should be used instead. +*/ +static void * s3jni_malloc_or_die(JNIEnv * const env, size_t n){ + void * const rv = sqlite3_malloc(n); + if( n && !rv ) s3jni_oom(env); + return rv; +} + +/* +** Works like sqlite3_malloc() unless built with SQLITE_JNI_FATAL_OOM, +** in which case it calls s3jni_oom() on OOM. +*/ +#ifdef SQLITE_JNI_FATAL_OOM +#define s3jni_malloc(SIZE) s3jni_malloc_or_die(env, SIZE) +#else +#define s3jni_malloc(SIZE) sqlite3_malloc(((void)env,(SIZE))) +/* the ((void)env) trickery here is to avoid ^^^^^^ an otherwise + unused arg in at least one place. */ +#endif + +/* +** Works like sqlite3_realloc() unless built with SQLITE_JNI_FATAL_OOM, +** in which case it calls s3jni_oom() on OOM. +*/ +#ifdef SQLITE_JNI_FATAL_OOM +static void * s3jni_realloc_or_die(JNIEnv * const env, void * p, size_t n){ + void * const rv = sqlite3_realloc(p, (int)n); + if( n && !rv ) s3jni_oom(env); + return rv; +} +#define s3jni_realloc(MEM,SIZE) s3jni_realloc_or_die(env, (MEM), (SIZE)) +#else +#define s3jni_realloc(MEM,SIZE) sqlite3_realloc((MEM), ((void)env, (SIZE))) +#endif + +/* Fail fatally if !EXPR. */ +#define s3jni_oom_fatal(EXPR) if( !(EXPR) ) s3jni_oom(env) +/* Maybe fail fatally if !EXPR. */ +#ifdef SQLITE_JNI_FATAL_OOM +#define s3jni_oom_check s3jni_oom_fatal +#else +#define s3jni_oom_check(EXPR) +#endif +//#define S3JniDb_oom(pDb,EXPR) ((EXPR) ? sqlite3OomFault(pDb) : 0) + +#define s3jni_db_oom(pDb) (void)((pDb) ? ((pDb)->mallocFailed=1) : 0) + +/* Helpers for Java value reference management. */ +static jobject s3jni_ref_global(JNIEnv * const env, jobject const v){ + jobject const rv = v ? (*env)->NewGlobalRef(env, v) : NULL; + s3jni_oom_fatal( v ? !!rv : 1 ); + return rv; +} +static jobject s3jni_ref_local(JNIEnv * const env, jobject const v){ + jobject const rv = v ? (*env)->NewLocalRef(env, v) : NULL; + s3jni_oom_fatal( v ? !!rv : 1 ); + return rv; +} +static inline void s3jni_unref_global(JNIEnv * const env, jobject const v){ + if( v ) (*env)->DeleteGlobalRef(env, v); +} +static inline void s3jni_unref_local(JNIEnv * const env, jobject const v){ + if( v ) (*env)->DeleteLocalRef(env, v); +} +#define S3JniRefGlobal(VAR) s3jni_ref_global(env, (VAR)) +#define S3JniRefLocal(VAR) s3jni_ref_local(env, (VAR)) +#define S3JniUnrefGlobal(VAR) s3jni_unref_global(env, (VAR)) +#define S3JniUnrefLocal(VAR) s3jni_unref_local(env, (VAR)) + +/* +** Lookup key type for use with s3jni_nphop() and a cache of a +** frequently-needed Java-side class reference and one or two Java +** class member IDs. +*/ +typedef struct S3JniNphOp S3JniNphOp; +struct S3JniNphOp { + const int index /* index into S3JniGlobal.nph[] */; + const char * const zName /* Full Java name of the class */; + const char * const zMember /* Name of member property */; + const char * const zTypeSig /* JNI type signature of zMember */; + /* + ** klazz is a global ref to the class represented by pRef. + ** + ** According to: + ** + ** https://developer.ibm.com/articles/j-jni/ + ** + ** > ... the IDs returned for a given class don't change for the + ** lifetime of the JVM process. But the call to get the field or + ** method can require significant work in the JVM, because fields + ** and methods might have been inherited from superclasses, making + ** the JVM walk up the class hierarchy to find them. Because the + ** IDs are the same for a given class, you should look them up + ** once and then reuse them. Similarly, looking up class objects + ** can be expensive, so they should be cached as well. + */ + jclass klazz; + volatile jfieldID fidValue /* NativePointerHolder.nativePointer or + ** OutputPointer.T.value */; + volatile jmethodID midCtor /* klazz's no-arg constructor. Used by + ** NativePointerHolder_new(). */; +}; + +/* +** Cache keys for each concrete NativePointerHolder subclasses and +** OutputPointer.T types. The members are to be used with s3jni_nphop() +** and friends, and each one's member->index corresponds to its index +** in the S3JniGlobal.nph[] array. +*/ +static const struct { + const S3JniNphOp sqlite3; + const S3JniNphOp sqlite3_backup; + const S3JniNphOp sqlite3_blob; + const S3JniNphOp sqlite3_context; + const S3JniNphOp sqlite3_stmt; + const S3JniNphOp sqlite3_value; + const S3JniNphOp OutputPointer_Bool; + const S3JniNphOp OutputPointer_Int32; + const S3JniNphOp OutputPointer_Int64; + const S3JniNphOp OutputPointer_sqlite3; + const S3JniNphOp OutputPointer_sqlite3_blob; + const S3JniNphOp OutputPointer_sqlite3_stmt; + const S3JniNphOp OutputPointer_sqlite3_value; + const S3JniNphOp OutputPointer_String; +#ifdef SQLITE_ENABLE_FTS5 + const S3JniNphOp OutputPointer_ByteArray; + const S3JniNphOp Fts5Context; + const S3JniNphOp Fts5ExtensionApi; + const S3JniNphOp fts5_api; + const S3JniNphOp fts5_tokenizer; + const S3JniNphOp Fts5Tokenizer; +#endif +} S3JniNphOps = { +#define MkRef(INDEX, KLAZZ, MEMBER, SIG) \ + { INDEX, "org/sqlite/jni/" KLAZZ, MEMBER, SIG } +/* NativePointerHolder ref */ +#define RefN(INDEX, KLAZZ) MkRef(INDEX, KLAZZ, "nativePointer", "J") +/* OutputPointer.T ref */ +#define RefO(INDEX, KLAZZ, SIG) MkRef(INDEX, KLAZZ, "value", SIG) + RefN(0, "capi/sqlite3"), + RefN(1, "capi/sqlite3_backup"), + RefN(2, "capi/sqlite3_blob"), + RefN(3, "capi/sqlite3_context"), + RefN(4, "capi/sqlite3_stmt"), + RefN(5, "capi/sqlite3_value"), + RefO(6, "capi/OutputPointer$Bool", "Z"), + RefO(7, "capi/OutputPointer$Int32", "I"), + RefO(8, "capi/OutputPointer$Int64", "J"), + RefO(9, "capi/OutputPointer$sqlite3", + "Lorg/sqlite/jni/capi/sqlite3;"), + RefO(10, "capi/OutputPointer$sqlite3_blob", + "Lorg/sqlite/jni/capi/sqlite3_blob;"), + RefO(11, "capi/OutputPointer$sqlite3_stmt", + "Lorg/sqlite/jni/capi/sqlite3_stmt;"), + RefO(12, "capi/OutputPointer$sqlite3_value", + "Lorg/sqlite/jni/capi/sqlite3_value;"), + RefO(13, "capi/OutputPointer$String", "Ljava/lang/String;"), +#ifdef SQLITE_ENABLE_FTS5 + RefO(14, "capi/OutputPointer$ByteArray", "[B"), + RefN(15, "fts5/Fts5Context"), + RefN(16, "fts5/Fts5ExtensionApi"), + RefN(17, "fts5/fts5_api"), + RefN(18, "fts5/fts5_tokenizer"), + RefN(19, "fts5/Fts5Tokenizer") +#endif +#undef MkRef +#undef RefN +#undef RefO +}; + +#define S3JniNph(T) &S3JniNphOps.T + +enum { + /* + ** Size of the NativePointerHolder cache. Need enough space for + ** (only) the library's NativePointerHolder and OutputPointer types, + ** a fixed count known at build-time. This value needs to be + ** exactly the number of S3JniNphOp entries in the S3JniNphOps + ** object. + */ + S3Jni_NphCache_size = sizeof(S3JniNphOps) / sizeof(S3JniNphOp) +}; + +/* +** State for binding C callbacks to Java methods. +*/ +typedef struct S3JniHook S3JniHook; +struct S3JniHook{ + jobject jObj /* global ref to Java instance */; + jmethodID midCallback /* callback method. Signature depends on + ** jObj's type */; + /* We lookup the jObj.xDestroy() method as-needed for contexts which + ** support custom finalizers. Fundamentally we can support them for + ** any Java type, but we only want to expose support for them where + ** the C API does. */ + jobject jExtra /* Global ref to a per-hook-type value */; + int doXDestroy /* If true then S3JniHook_unref() will call + jObj->xDestroy() if it's available. */; + S3JniHook * pNext /* Next entry in S3Global.hooks.aFree */; +}; +/* For clean bitwise-copy init of local instances. */ +static const S3JniHook S3JniHook_empty = {0,0,0,0,0}; + +/* +** Per-(sqlite3*) state for various JNI bindings. This state is +** allocated as needed, cleaned up in sqlite3_close(_v2)(), and +** recycled when possible. +** +** Trivia: vars and parameters of this type are often named "ps" +** because this class used to have a name for which that abbreviation +** made sense. +*/ +typedef struct S3JniDb S3JniDb; +struct S3JniDb { + sqlite3 *pDb /* The associated db handle */; + jobject jDb /* A global ref of the output object which gets + returned from sqlite3_open(_v2)(). We need this in + order to have an object to pass to routines like + sqlite3_collation_needed()'s callback, or else we + have to dynamically create one for that purpose, + which would be fine except that it would be a + different instance (and maybe even a different + class) than the one the user may expect to + receive. */; + char * zMainDbName /* Holds the string allocated on behalf of + SQLITE_DBCONFIG_MAINDBNAME. */; + struct { + S3JniHook busyHandler; + S3JniHook collationNeeded; + S3JniHook commit; + S3JniHook progress; + S3JniHook rollback; + S3JniHook trace; + S3JniHook update; + S3JniHook auth; +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK + S3JniHook preUpdate; +#endif + } hooks; +#ifdef SQLITE_ENABLE_FTS5 + /* FTS5-specific state */ + struct { + jobject jApi /* global ref to s3jni_fts5_api_from_db() */; + } fts; +#endif + S3JniDb * pNext /* Next entry in SJG.perDb.aFree */; +}; + +static const char * const S3JniDb_clientdata_key = "S3JniDb"; +#define S3JniDb_from_clientdata(pDb) \ + (pDb ? sqlite3_get_clientdata(pDb, S3JniDb_clientdata_key) : 0) + +/* +** Cache for per-JNIEnv (i.e. per-thread) data. +** +** Trivia: vars and parameters of this type are often named "jc" +** because this class used to have a name for which that abbreviation +** made sense. +*/ +typedef struct S3JniEnv S3JniEnv; +struct S3JniEnv { + JNIEnv *env /* JNIEnv in which this cache entry was created */; + /* + ** pdbOpening is used to coordinate the Java/DB connection of a + ** being-open()'d db in the face of auto-extensions. + ** Auto-extensions run before we can bind the C db to its Java + ** representation, but auto-extensions require that binding to pass + ** on to their Java-side callbacks. We handle this as follows: + ** + ** - In the JNI side of sqlite3_open(), allocate the Java side of + ** that connection and set pdbOpening to point to that + ** object. + ** + ** - Call sqlite3_open(), which triggers the auto-extension + ** handler. That handler uses pdbOpening to connect the native + ** db handle which it receives with pdbOpening. + ** + ** - When sqlite3_open() returns, check whether pdbOpening->pDb is + ** NULL. If it isn't, auto-extension handling set it up. If it + ** is, complete the Java/C binding unless sqlite3_open() returns + ** a NULL db, in which case free pdbOpening. + */ + S3JniDb * pdbOpening; + S3JniEnv * pNext /* Next entry in SJG.envCache.aHead or + SJG.envCache.aFree */; +}; + +/* +** State for proxying sqlite3_auto_extension() in Java. This was +** initially a separate class from S3JniHook and now the older name is +** retained for readability in the APIs which use this, as well as for +** its better code-searchability. +*/ +typedef S3JniHook S3JniAutoExtension; + +/* +** Type IDs for SQL function categories. +*/ +enum UDFType { + UDF_UNKNOWN_TYPE = 0/*for error propagation*/, + UDF_SCALAR, + UDF_AGGREGATE, + UDF_WINDOW +}; + +/* +** State for binding Java-side UDFs. +*/ +typedef struct S3JniUdf S3JniUdf; +struct S3JniUdf { + jobject jObj /* SQLFunction instance */; + char * zFuncName /* Only for error reporting and debug logging */; + enum UDFType type /* UDF type */; + /** Method IDs for the various UDF methods. */ + jmethodID jmidxFunc /* xFunc method (scalar) */; + jmethodID jmidxStep /* xStep method (aggregate/window) */; + jmethodID jmidxFinal /* xFinal method (aggregate/window) */; + jmethodID jmidxValue /* xValue method (window) */; + jmethodID jmidxInverse /* xInverse method (window) */; + S3JniUdf * pNext /* Next entry in SJG.udf.aFree. */; +}; + +#if defined(SQLITE_JNI_ENABLE_METRICS) && 0==SQLITE_JNI_ENABLE_METRICS +# undef SQLITE_JNI_ENABLE_METRICS +#endif + +/* +** If true, modifying S3JniGlobal.metrics is protected by a mutex, +** else it isn't. +*/ +#ifdef SQLITE_DEBUG +# define S3JNI_METRICS_MUTEX SQLITE_THREADSAFE +#else +# define S3JNI_METRICS_MUTEX 0 +#endif +#ifndef SQLITE_JNI_ENABLE_METRICS +# undef S3JNI_METRICS_MUTEX +# define S3JNI_METRICS_MUTEX 0 +#endif + +/* +** Global state, e.g. caches and metrics. +*/ +typedef struct S3JniGlobalType S3JniGlobalType; +struct S3JniGlobalType { + /* + ** According to: https://developer.ibm.com/articles/j-jni/ + ** + ** > A thread can get a JNIEnv by calling GetEnv() using the JNI + ** invocation interface through a JavaVM object. The JavaVM object + ** itself can be obtained by calling the JNI GetJavaVM() method + ** using a JNIEnv object and can be cached and shared across + ** threads. Caching a copy of the JavaVM object enables any thread + ** with access to the cached object to get access to its own + ** JNIEnv when necessary. + */ + JavaVM * jvm; + /* + ** Global mutex. It must not be used for anything which might call + ** back into the JNI layer. + */ + sqlite3_mutex * mutex; + /* + ** Cache of references to Java classes and method IDs for + ** NativePointerHolder subclasses and OutputPointer.T types. + */ + struct { + S3JniNphOp list[S3Jni_NphCache_size]; + sqlite3_mutex * mutex; /* mutex for this->list */ + volatile void const * locker; /* sanity-checking-only context object + for this->mutex */ + } nph; + /* + ** Cache of per-thread state. + */ + struct { + S3JniEnv * aHead /* Linked list of in-use instances */; + S3JniEnv * aFree /* Linked list of free instances */; + sqlite3_mutex * mutex /* mutex for aHead and aFree. */; + volatile void const * locker /* env mutex is held on this + object's behalf. Used only for + sanity checking. */; + } envCache; + /* + ** Per-db state. This can move into the core library once we can tie + ** client-defined state to db handles there. + */ + struct { + S3JniDb * aFree /* Linked list of free instances */; + sqlite3_mutex * mutex /* mutex for aHead and aFree */; + volatile void const * locker + /* perDb mutex is held on this object's behalf. Used only for + sanity checking. Note that the mutex is at the class level, not + instance level. */; + } perDb; + struct { + S3JniUdf * aFree /* Head of the free-item list. Guarded by global + mutex. */; + } udf; + /* + ** Refs to global classes and methods. Obtained during static init + ** and never released. + */ + struct { + jclass cLong /* global ref to java.lang.Long */; + jclass cString /* global ref to java.lang.String */; + jobject oCharsetUtf8 /* global ref to StandardCharset.UTF_8 */; + jmethodID ctorLong1 /* the Long(long) constructor */; + jmethodID ctorStringBA /* the String(byte[],Charset) constructor */; + jmethodID stringGetBytes /* the String.getBytes(Charset) method */; + + /* + ByteBuffer may or may not be supported via JNI on any given + platform: + + https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/functions.html#nio_support + + We only store a ref to byteBuffer.klazz if JNI support for + ByteBuffer is available (which we determine during static init). + */ + struct { + jclass klazz /* global ref to java.nio.ByteBuffer */; + jmethodID midAlloc /* ByteBuffer.allocateDirect() */; + jmethodID midLimit /* ByteBuffer.limit() */; + } byteBuffer; + } g; + /* + ** The list of Java-side auto-extensions + ** (org.sqlite.jni.capi.AutoExtensionCallback objects). + */ + struct { + S3JniAutoExtension *aExt /* The auto-extension list. It is + maintained such that all active + entries are in the first contiguous + nExt array elements. */; + int nAlloc /* number of entries allocated for aExt, + as distinct from the number of active + entries. */; + int nExt /* number of active entries in aExt, all in the + first nExt'th array elements. */; + sqlite3_mutex * mutex /* mutex for manipulation/traversal of aExt */; + volatile const void * locker /* object on whose behalf the mutex + is held. Only for sanity checking + in debug builds. */; + } autoExt; +#ifdef SQLITE_ENABLE_FTS5 + struct { + volatile jobject jExt /* Global ref to Java singleton for the + Fts5ExtensionApi instance. */; + struct { + jfieldID fidA /* Fts5Phrase::a member */; + jfieldID fidB /* Fts5Phrase::b member */; + } jPhraseIter; + } fts5; +#endif + struct { +#ifdef SQLITE_ENABLE_SQLLOG + S3JniHook sqllog /* sqlite3_config(SQLITE_CONFIG_SQLLOG) callback */; +#endif + S3JniHook configlog /* sqlite3_config(SQLITE_CONFIG_LOG) callback */; + S3JniHook * aFree /* free-item list, for recycling. */; + sqlite3_mutex * mutex /* mutex for aFree */; + volatile const void * locker /* object on whose behalf the mutex + is held. Only for sanity checking + in debug builds. */; + } hook; +#ifdef SQLITE_JNI_ENABLE_METRICS + /* Internal metrics. */ + struct { + volatile unsigned nEnvHit; + volatile unsigned nEnvMiss; + volatile unsigned nEnvAlloc; + volatile unsigned nMutexEnv /* number of times envCache.mutex was entered for + a S3JniEnv operation. */; + volatile unsigned nMutexNph /* number of times SJG.mutex was entered */; + volatile unsigned nMutexHook /* number of times SJG.mutex hooks.was entered */; + volatile unsigned nMutexPerDb /* number of times perDb.mutex was entered */; + volatile unsigned nMutexAutoExt /* number of times autoExt.mutex was entered */; + volatile unsigned nMutexGlobal /* number of times global mutex was entered. */; + volatile unsigned nMutexUdf /* number of times global mutex was entered + for UDFs. */; + volatile unsigned nDestroy /* xDestroy() calls across all types */; + volatile unsigned nPdbAlloc /* Number of S3JniDb alloced. */; + volatile unsigned nPdbRecycled /* Number of S3JniDb reused. */; + volatile unsigned nUdfAlloc /* Number of S3JniUdf alloced. */; + volatile unsigned nUdfRecycled /* Number of S3JniUdf reused. */; + volatile unsigned nHookAlloc /* Number of S3JniHook alloced. */; + volatile unsigned nHookRecycled /* Number of S3JniHook reused. */; + struct { + /* Number of calls for each type of UDF callback. */ + volatile unsigned nFunc; + volatile unsigned nStep; + volatile unsigned nFinal; + volatile unsigned nValue; + volatile unsigned nInverse; + } udf; + unsigned nMetrics /* Total number of mutex-locked + metrics increments. */; +#if S3JNI_METRICS_MUTEX + sqlite3_mutex * mutex; +#endif + } metrics; +#endif /* SQLITE_JNI_ENABLE_METRICS */ +}; +static S3JniGlobalType S3JniGlobal = {}; +#define SJG S3JniGlobal + +/* Increments *p, possibly protected by a mutex. */ +#ifndef SQLITE_JNI_ENABLE_METRICS +#define s3jni_incr(PTR) +#elif S3JNI_METRICS_MUTEX +static void s3jni_incr( volatile unsigned int * const p ){ + sqlite3_mutex_enter(SJG.metrics.mutex); + ++SJG.metrics.nMetrics; + ++(*p); + sqlite3_mutex_leave(SJG.metrics.mutex); +} +#else +#define s3jni_incr(PTR) ++(*(PTR)) +#endif + +/* Helpers for working with specific mutexes. */ +#if SQLITE_THREADSAFE +#define s3jni_mutex_enter2(M, Metric) \ + sqlite3_mutex_enter( M ); \ + s3jni_incr( &SJG.metrics.Metric ) +#define s3jni_mutex_leave2(M) \ + sqlite3_mutex_leave( M ) + +#define s3jni_mutex_enter(M, L, Metric) \ + assert( (void*)env != (void*)L && "Invalid use of " #L); \ + s3jni_mutex_enter2( M, Metric ); \ + L = env +#define s3jni_mutex_leave(M, L) \ + assert( (void*)env == (void*)L && "Invalid use of " #L); \ + L = 0; \ + s3jni_mutex_leave2( M ) + +#define S3JniEnv_mutex_assertLocked \ + assert( 0 != SJG.envCache.locker && "Misuse of S3JniGlobal.envCache.mutex" ) +#define S3JniEnv_mutex_assertLocker \ + assert( (env) == SJG.envCache.locker && "Misuse of S3JniGlobal.envCache.mutex" ) +#define S3JniEnv_mutex_assertNotLocker \ + assert( (env) != SJG.envCache.locker && "Misuse of S3JniGlobal.envCache.mutex" ) + +#define S3JniEnv_mutex_enter \ + s3jni_mutex_enter( SJG.envCache.mutex, SJG.envCache.locker, nMutexEnv ) +#define S3JniEnv_mutex_leave \ + s3jni_mutex_leave( SJG.envCache.mutex, SJG.envCache.locker ) + +#define S3JniAutoExt_mutex_enter \ + s3jni_mutex_enter( SJG.autoExt.mutex, SJG.autoExt.locker, nMutexAutoExt ) +#define S3JniAutoExt_mutex_leave \ + s3jni_mutex_leave( SJG.autoExt.mutex, SJG.autoExt.locker ) +#define S3JniAutoExt_mutex_assertLocker \ + assert( env == SJG.autoExt.locker && "Misuse of S3JniGlobal.autoExt.mutex" ) + +#define S3JniGlobal_mutex_enter \ + s3jni_mutex_enter2( SJG.mutex, nMutexGlobal ) +#define S3JniGlobal_mutex_leave \ + s3jni_mutex_leave2( SJG.mutex ) + +#define S3JniHook_mutex_enter \ + s3jni_mutex_enter( SJG.hook.mutex, SJG.hook.locker, nMutexHook ) +#define S3JniHook_mutex_leave \ + s3jni_mutex_leave( SJG.hook.mutex, SJG.hook.locker ) + +#define S3JniNph_mutex_enter \ + s3jni_mutex_enter( SJG.nph.mutex, SJG.nph.locker, nMutexNph ) +#define S3JniNph_mutex_leave \ + s3jni_mutex_leave( SJG.nph.mutex, SJG.nph.locker ) + +#define S3JniDb_mutex_assertLocker \ + assert( (env) == SJG.perDb.locker && "Misuse of S3JniGlobal.perDb.mutex" ) +#define S3JniDb_mutex_enter \ + s3jni_mutex_enter( SJG.perDb.mutex, SJG.perDb.locker, nMutexPerDb ) +#define S3JniDb_mutex_leave \ + s3jni_mutex_leave( SJG.perDb.mutex, SJG.perDb.locker ) + +#else /* SQLITE_THREADSAFE==0 */ +#define S3JniAutoExt_mutex_assertLocker +#define S3JniAutoExt_mutex_enter +#define S3JniAutoExt_mutex_leave +#define S3JniDb_mutex_assertLocker +#define S3JniDb_mutex_enter +#define S3JniDb_mutex_leave +#define S3JniEnv_mutex_assertLocked +#define S3JniEnv_mutex_assertLocker +#define S3JniEnv_mutex_assertNotLocker +#define S3JniEnv_mutex_enter +#define S3JniEnv_mutex_leave +#define S3JniGlobal_mutex_enter +#define S3JniGlobal_mutex_leave +#define S3JniHook_mutex_enter +#define S3JniHook_mutex_leave +#define S3JniNph_mutex_enter +#define S3JniNph_mutex_leave +#endif + +/* Helpers for jstring and jbyteArray. */ +static const char * s3jni__jstring_to_mutf8(JNIEnv * const env, jstring v ){ + const char *z = v ? (*env)->GetStringUTFChars(env, v, NULL) : 0; + s3jni_oom_check( v ? !!z : !z ); + return z; +} + +#define s3jni_jstring_to_mutf8(ARG) s3jni__jstring_to_mutf8(env, (ARG)) +#define s3jni_mutf8_release(ARG,VAR) if( VAR ) (*env)->ReleaseStringUTFChars(env, ARG, VAR) + +/* +** If jBA is not NULL then its GetByteArrayElements() value is +** returned. If jBA is not NULL and nBA is not NULL then *nBA is set +** to the GetArrayLength() of jBA. If GetByteArrayElements() requires +** an allocation and that allocation fails then this function either +** fails fatally or returns 0, depending on build-time options. + */ +static jbyte * s3jni__jbyteArray_bytes2(JNIEnv * const env, jbyteArray jBA, jsize * nBA ){ + jbyte * const rv = jBA ? (*env)->GetByteArrayElements(env, jBA, NULL) : 0; + s3jni_oom_check( jBA ? !!rv : 1 ); + if( jBA && nBA ) *nBA = (*env)->GetArrayLength(env, jBA); + return rv; +} + +#define s3jni_jbyteArray_bytes2(jByteArray,ptrToSz) \ + s3jni__jbyteArray_bytes2(env, (jByteArray), (ptrToSz)) +#define s3jni_jbyteArray_bytes(jByteArray) s3jni__jbyteArray_bytes2(env, (jByteArray), 0) +#define s3jni_jbyteArray_release(jByteArray,jBytes) \ + if( jBytes ) (*env)->ReleaseByteArrayElements(env, jByteArray, jBytes, JNI_ABORT) +#define s3jni_jbyteArray_commit(jByteArray,jBytes) \ + if( jBytes ) (*env)->ReleaseByteArrayElements(env, jByteArray, jBytes, JNI_COMMIT) + +/* +** If jbb is-a java.nio.Buffer object and the JNI environment supports +** it, *pBuf is set to the buffer's memory and *pN is set to its +** limit() (as opposed to its capacity()). If jbb is NULL, not a +** Buffer, or the JNI environment does not support that operation, +** *pBuf is set to 0 and *pN is set to 0. +** +** Note that the length of the buffer can be larger than SQLITE_LIMIT +** but this function does not know what byte range of the buffer is +** required so cannot check for that violation. The caller is required +** to ensure that any to-be-bind()ed range fits within SQLITE_LIMIT. +** +** Sidebar: it is unfortunate that we cannot get ByteBuffer.limit() +** via a JNI method like we can for ByteBuffer.capacity(). We instead +** have to call back into Java to get the limit(). Depending on how +** the ByteBuffer is used, the limit and capacity might be the same, +** but when reusing a buffer, the limit may well change whereas the +** capacity is fixed. The problem with, e.g., read()ing blob data to a +** ByteBuffer's memory based on its capacity is that Java-level code +** is restricted to accessing the range specified in +** ByteBuffer.limit(). If we were to honor only the capacity, we +** could end up writing to, or reading from, parts of a ByteBuffer +** which client code itself cannot access without explicitly modifying +** the limit. The penalty we pay for this correctness is that we must +** call into Java to get the limit() of every ByteBuffer we work with. +** +** An alternative to having to call into ByteBuffer.limit() from here +** would be to add private native impls of all ByteBuffer-using +** methods, each of which adds a jint parameter which _must_ be set to +** theBuffer.limit() by public Java APIs which use those private impls +** to do the real work. +*/ +static void s3jni__get_nio_buffer(JNIEnv * const env, jobject jbb, void **pBuf, jint * pN ){ + *pBuf = 0; + *pN = 0; + if( jbb ){ + *pBuf = (*env)->GetDirectBufferAddress(env, jbb); + if( *pBuf ){ + /* + ** Maintenance reminder: do not use + ** (*env)->GetDirectBufferCapacity(env,jbb), even though it + ** would be much faster, for reasons explained in this + ** function's comments. + */ + *pN = (*env)->CallIntMethod(env, jbb, SJG.g.byteBuffer.midLimit); + S3JniExceptionIsFatal("Error calling ByteBuffer.limit() method."); + } + } +} +#define s3jni_get_nio_buffer(JOBJ,vpOut,jpOut) \ + s3jni__get_nio_buffer(env,(JOBJ),(vpOut),(jpOut)) + +/* +** Returns the current JNIEnv object. Fails fatally if it cannot find +** the object. +*/ +static JNIEnv * s3jni_env(void){ + JNIEnv * env = 0; + if( (*SJG.jvm)->GetEnv(SJG.jvm, (void **)&env, + JNI_VERSION_1_8) ){ + fprintf(stderr, "Fatal error: cannot get current JNIEnv.\n"); + abort(); + } + return env; +} + +/* +** Fetches the S3JniGlobal.envCache row for the given env, allocating a +** row if needed. When a row is allocated, its state is initialized +** insofar as possible. Calls (*env)->FatalError() if allocation of an +** entry fails. That's hypothetically possible but "shouldn't happen." +*/ +static S3JniEnv * S3JniEnv__get(JNIEnv * const env){ + struct S3JniEnv * row; + S3JniEnv_mutex_enter; + row = SJG.envCache.aHead; + for( ; row; row = row->pNext ){ + if( row->env == env ){ + s3jni_incr( &SJG.metrics.nEnvHit ); + S3JniEnv_mutex_leave; + return row; + } + } + s3jni_incr( &SJG.metrics.nEnvMiss ); + row = SJG.envCache.aFree; + if( row ){ + SJG.envCache.aFree = row->pNext; + }else{ + row = s3jni_malloc_or_die(env, sizeof(*row)); + s3jni_incr( &SJG.metrics.nEnvAlloc ); + } + memset(row, 0, sizeof(*row)); + row->pNext = SJG.envCache.aHead; + SJG.envCache.aHead = row; + row->env = env; + + S3JniEnv_mutex_leave; + return row; +} + +#define S3JniEnv_get() S3JniEnv__get(env) + +/* +** This function is NOT part of the sqlite3 public API. It is strictly +** for use by the sqlite project's own Java/JNI bindings. +** +** For purposes of certain hand-crafted JNI function bindings, we +** need a way of reporting errors which is consistent with the rest of +** the C API, as opposed to throwing Java exceptions. To that end, this +** internal-use-only function is a thin proxy around +** sqlite3ErrorWithMessage(). The intent is that it only be used from +** JNI bindings such as sqlite3_prepare_v2/v3(), and definitely not +** from client code. +** +** Returns err_code. +*/ +static int s3jni_db_error(sqlite3* const db, int err_code, + const char * const zMsg){ + if( db!=0 ){ + if( 0==zMsg ){ + sqlite3Error(db, err_code); + }else{ + const int nMsg = sqlite3Strlen30(zMsg); + sqlite3_mutex_enter(sqlite3_db_mutex(db)); + sqlite3ErrorWithMsg(db, err_code, "%.*s", nMsg, zMsg); + sqlite3_mutex_leave(sqlite3_db_mutex(db)); + } + } + return err_code; +} + +/* +** Creates a new jByteArray of length nP, copies p's contents into it, +** and returns that byte array (NULL on OOM unless fail-fast alloc +** errors are enabled). p may be NULL, in which case the array is +** created but no bytes are filled. +*/ +static jbyteArray s3jni__new_jbyteArray(JNIEnv * const env, + const void * const p, int nP){ + jbyteArray jba = (*env)->NewByteArray(env, (jint)nP); + + s3jni_oom_check( jba ); + if( jba && p ){ + (*env)->SetByteArrayRegion(env, jba, 0, (jint)nP, (const jbyte*)p); + } + return jba; +} + +#define s3jni_new_jbyteArray(P,n) s3jni__new_jbyteArray(env, P, n) + + +/* +** Uses the java.lang.String(byte[],Charset) constructor to create a +** new String from UTF-8 string z. n is the number of bytes to +** copy. If n<0 then sqlite3Strlen30() is used to calculate it. +** +** Returns NULL if z is NULL or on OOM, else returns a new jstring +** owned by the caller. +** +** Sidebar: this is a painfully inefficient way to convert from +** standard UTF-8 to a Java string, but JNI offers only algorithms for +** working with MUTF-8, not UTF-8. +*/ +static jstring s3jni__utf8_to_jstring(JNIEnv * const env, + const char * const z, int n){ + jstring rv = NULL; + if( 0==n || (n<0 && z && !z[0]) ){ + /* Fast-track the empty-string case via the MUTF-8 API. We could + hypothetically do this for any strings where n<4 and z is + NUL-terminated and none of z[0..3] are NUL bytes. */ + rv = (*env)->NewStringUTF(env, ""); + s3jni_oom_check( rv ); + }else if( z ){ + jbyteArray jba; + if( n<0 ) n = sqlite3Strlen30(z); + jba = s3jni_new_jbyteArray((unsigned const char *)z, n); + if( jba ){ + rv = (*env)->NewObject(env, SJG.g.cString, SJG.g.ctorStringBA, + jba, SJG.g.oCharsetUtf8); + S3JniIfThrew{ + S3JniExceptionReport; + S3JniExceptionClear; + } + S3JniUnrefLocal(jba); + } + s3jni_oom_check( rv ); + } + return rv; +} +#define s3jni_utf8_to_jstring(CStr,n) s3jni__utf8_to_jstring(env, CStr, n) + +/* +** Converts the given java.lang.String object into a NUL-terminated +** UTF-8 C-string by calling jstr.getBytes(StandardCharset.UTF_8). +** Returns NULL if jstr is NULL or on allocation error. If jstr is not +** NULL and nLen is not NULL then nLen is set to the length of the +** returned string, not including the terminating NUL. If jstr is not +** NULL and it returns NULL, this indicates an allocation error. In +** that case, if nLen is not NULL then it is either set to 0 (if +** fetching of jstr's bytes fails to allocate) or set to what would +** have been the length of the string had C-string allocation +** succeeded. +** +** The returned memory is allocated from sqlite3_malloc() and +** ownership is transferred to the caller. +*/ +static char * s3jni__jstring_to_utf8(JNIEnv * const env, + jstring jstr, int *nLen){ + jbyteArray jba; + jsize nBA; + char *rv; + + if( !jstr ) return 0; + jba = (*env)->CallObjectMethod(env, jstr, SJG.g.stringGetBytes, + SJG.g.oCharsetUtf8); + + if( (*env)->ExceptionCheck(env) || !jba + /* order of these checks is significant for -Xlint:jni */ ) { + S3JniExceptionReport; + s3jni_oom_check( jba ); + if( nLen ) *nLen = 0; + return 0; + } + nBA = (*env)->GetArrayLength(env, jba); + if( nLen ) *nLen = (int)nBA; + rv = s3jni_malloc( nBA + 1 ); + if( rv ){ + (*env)->GetByteArrayRegion(env, jba, 0, nBA, (jbyte*)rv); + rv[nBA] = 0; + } + S3JniUnrefLocal(jba); + return rv; +} +#define s3jni_jstring_to_utf8(JStr,n) s3jni__jstring_to_utf8(env, JStr, n) + +/* +** Expects to be passed a pointer from sqlite3_column_text16() or +** sqlite3_value_text16() and a byte-length value from +** sqlite3_column_bytes16() or sqlite3_value_bytes16(). It creates a +** Java String of exactly half that character length, returning NULL +** if !p or (*env)->NewString() fails. +*/ +static jstring s3jni_text16_to_jstring(JNIEnv * const env, const void * const p, int nP){ + jstring const rv = p + ? (*env)->NewString(env, (const jchar *)p, (jsize)(nP/2)) + : NULL; + s3jni_oom_check( p ? !!rv : 1 ); + return rv; +} + +/* +** Creates a new ByteBuffer instance with a capacity of n. assert()s +** that SJG.g.byteBuffer.klazz is not 0 and n>0. +*/ +static jobject s3jni__new_ByteBuffer(JNIEnv * const env, int n){ + jobject rv = 0; + assert( SJG.g.byteBuffer.klazz ); + assert( SJG.g.byteBuffer.midAlloc ); + assert( n > 0 ); + rv = (*env)->CallStaticObjectMethod(env, SJG.g.byteBuffer.klazz, + SJG.g.byteBuffer.midAlloc, (jint)n); + S3JniIfThrew { + S3JniExceptionReport; + S3JniExceptionClear; + } + s3jni_oom_check( rv ); + return rv; +} + +/* +** If n>0 and sqlite3_jni_supports_nio() is true then this creates a +** new ByteBuffer object and copies n bytes from p to it. Returns NULL +** if n is 0, sqlite3_jni_supports_nio() is false, or on allocation +** error (unless fatal alloc failures are enabled). +*/ +static jobject s3jni__blob_to_ByteBuffer(JNIEnv * const env, + const void * p, int n){ + jobject rv = NULL; + assert( n >= 0 ); + if( 0==n || !SJG.g.byteBuffer.klazz ){ + return NULL; + } + rv = s3jni__new_ByteBuffer(env, n); + if( rv ){ + void * tgt = (*env)->GetDirectBufferAddress(env, rv); + memcpy(tgt, p, (size_t)n); + } + return rv; +} + + +/* +** Requires jx to be a Throwable. Calls its toString() method and +** returns its value converted to a UTF-8 string. The caller owns the +** returned string and must eventually sqlite3_free() it. Returns 0 +** if there is a problem fetching the info or on OOM. +** +** Design note: we use toString() instead of getMessage() because the +** former includes the exception type's name: +** +** Exception e = new RuntimeException("Hi"); +** System.out.println(e.toString()); // java.lang.RuntimeException: Hi +** System.out.println(e.getMessage()); // Hi +*/ +static char * s3jni_exception_error_msg(JNIEnv * const env, jthrowable jx){ + jmethodID mid; + jstring msg; + char * zMsg; + jclass const klazz = (*env)->GetObjectClass(env, jx); + mid = (*env)->GetMethodID(env, klazz, "toString", "()Ljava/lang/String;"); + S3JniUnrefLocal(klazz); + S3JniIfThrew{ + S3JniExceptionReport; + S3JniExceptionClear; + return 0; + } + msg = (*env)->CallObjectMethod(env, jx, mid); + S3JniIfThrew{ + S3JniExceptionReport; + S3JniExceptionClear; + return 0; + } + zMsg = s3jni_jstring_to_utf8( msg, 0); + S3JniUnrefLocal(msg); + return zMsg; +} + +/* +** Extracts env's current exception, sets ps->pDb's error message to +** its message string, and clears the exception. If errCode is non-0, +** it is used as-is, else SQLITE_ERROR is assumed. If there's a +** problem extracting the exception's message, it's treated as +** non-fatal and zDfltMsg is used in its place. +** +** Locks the global S3JniDb mutex. +** +** This must only be called if a JNI exception is pending. +** +** Returns errCode unless it is 0, in which case SQLITE_ERROR is +** returned. +*/ +static int s3jni__db_exception(JNIEnv * const env, sqlite3 * const pDb, + int errCode, const char *zDfltMsg){ + jthrowable const ex = (*env)->ExceptionOccurred(env); + + if( 0==errCode ) errCode = SQLITE_ERROR; + if( ex ){ + char * zMsg; + S3JniExceptionClear; + zMsg = s3jni_exception_error_msg(env, ex); + s3jni_db_error(pDb, errCode, zMsg ? zMsg : zDfltMsg); + sqlite3_free(zMsg); + S3JniUnrefLocal(ex); + }else if( zDfltMsg ){ + s3jni_db_error(pDb, errCode, zDfltMsg); + } + return errCode; +} +#define s3jni_db_exception(pDb,ERRCODE,DFLTMSG) \ + s3jni__db_exception(env, (pDb), (ERRCODE), (DFLTMSG) ) + +/* +** Extracts the (void xDestroy()) method from jObj and applies it to +** jObj. If jObj is NULL, this is a no-op. The lack of an xDestroy() +** method is silently ignored. Any exceptions thrown by xDestroy() +** trigger a warning to stdout or stderr and then the exception is +** suppressed. +*/ +static void s3jni__call_xDestroy(JNIEnv * const env, jobject jObj){ + if( jObj ){ + jclass const klazz = (*env)->GetObjectClass(env, jObj); + jmethodID method = (*env)->GetMethodID(env, klazz, "xDestroy", "()V"); + + S3JniUnrefLocal(klazz); + if( method ){ + s3jni_incr( &SJG.metrics.nDestroy ); + (*env)->CallVoidMethod(env, jObj, method); + S3JniIfThrew{ + S3JniExceptionWarnCallbackThrew("xDestroy() callback"); + S3JniExceptionClear; + } + }else{ + /* Non-fatal. */ + S3JniExceptionClear; + } + } +} +#define s3jni_call_xDestroy(JOBJ) s3jni__call_xDestroy(env, (JOBJ)) + +/* +** Internal helper for many hook callback impls. Locks the S3JniDb +** mutex, makes a copy of src into dest, with a some differences: (1) +** if src->jObj or src->jExtra are not NULL then dest will be a new +** LOCAL ref to it instead of a copy of the prior GLOBAL ref. (2) +** dest->doXDestroy is always false. +** +** If dest->jObj is not NULL when this returns then the caller is +** obligated to eventually free the new ref by passing *dest to +** S3JniHook_localundup(). The dest pointer must NOT be passed to +** S3JniHook_unref(), as that routine assumes that dest->jObj/jExtra +** are GLOBAL refs (it's illegal to try to unref the wrong ref type). +** +** Background: when running a hook we need a call-local copy lest +** another thread modify the hook while we're running it. That copy +** has to have its own Java reference, but it need only be call-local. +*/ +static void S3JniHook__localdup( JNIEnv * const env, S3JniHook const * const src, + S3JniHook * const dest ){ + S3JniHook_mutex_enter; + *dest = *src; + if(src->jObj) dest->jObj = S3JniRefLocal(src->jObj); + if(src->jExtra) dest->jExtra = S3JniRefLocal(src->jExtra); + dest->doXDestroy = 0; + S3JniHook_mutex_leave; +} +#define S3JniHook_localdup(src,dest) S3JniHook__localdup(env,src,dest) + +static void S3JniHook__localundup( JNIEnv * const env, S3JniHook * const h ){ + S3JniUnrefLocal(h->jObj); + S3JniUnrefLocal(h->jExtra); + *h = S3JniHook_empty; +} +#define S3JniHook_localundup(HOOK) S3JniHook__localundup(env, &(HOOK)) + +/* +** Removes any Java references from s and clears its state. If +** doXDestroy is true and s->jObj is not NULL, s->jObj +** is passed to s3jni_call_xDestroy() before any references are +** cleared. It is legal to call this when the object has no Java +** references. s must not be NULL. +*/ +static void S3JniHook__unref(JNIEnv * const env, S3JniHook * const s){ + if( s->jObj ){ + if( s->doXDestroy ){ + s3jni_call_xDestroy(s->jObj); + } + S3JniUnrefGlobal(s->jObj); + S3JniUnrefGlobal(s->jExtra); + }else{ + assert( !s->jExtra ); + } + *s = S3JniHook_empty; +} +#define S3JniHook_unref(hook) S3JniHook__unref(env, (hook)) + +/* +** Allocates one blank S3JniHook object from the recycling bin, if +** available, else from the heap. Returns NULL or dies on OOM, +** depending on build options. Locks on SJG.hooks.mutex. +*/ +static S3JniHook *S3JniHook__alloc(JNIEnv * const env){ + S3JniHook * p = 0; + S3JniHook_mutex_enter; + if( SJG.hook.aFree ){ + p = SJG.hook.aFree; + SJG.hook.aFree = p->pNext; + p->pNext = 0; + s3jni_incr(&SJG.metrics.nHookRecycled); + } + S3JniHook_mutex_leave; + if( 0==p ){ + p = s3jni_malloc(sizeof(S3JniHook)); + if( p ){ + s3jni_incr(&SJG.metrics.nHookAlloc); + } + } + if( p ){ + *p = S3JniHook_empty; + } + return p; +} +#define S3JniHook_alloc() S3JniHook__alloc(env) + +/* +** The rightful fate of all results from S3JniHook_alloc(). Locks on +** SJG.hook.mutex. +*/ +static void S3JniHook__free(JNIEnv * const env, S3JniHook * const p){ + if(p){ + assert( !p->pNext ); + S3JniHook_unref(p); + S3JniHook_mutex_enter; + p->pNext = SJG.hook.aFree; + SJG.hook.aFree = p; + S3JniHook_mutex_leave; + } +} +#define S3JniHook_free(hook) S3JniHook__free(env, hook) + +#if 0 +/* S3JniHook__free() without the lock: caller must hold the global mutex */ +static void S3JniHook__free_unlocked(JNIEnv * const env, S3JniHook * const p){ + if(p){ + assert( !p->pNext ); + assert( p->pNext != SJG.hook.aFree ); + S3JniHook_unref(p); + p->pNext = SJG.hook.aFree; + SJG.hook.aFree = p; + } +} +#define S3JniHook_free_unlocked(hook) S3JniHook__free_unlocked(env, hook) +#endif + +/* +** Clears all of s's state. Requires that that the caller has locked +** S3JniGlobal.perDb.mutex. Make sure to do anything needed with +** s->pNext and s->pPrev before calling this, as this clears them. +*/ +static void S3JniDb_clear(JNIEnv * const env, S3JniDb * const s){ + S3JniDb_mutex_assertLocker; + sqlite3_free( s->zMainDbName ); +#define UNHOOK(MEMBER) \ + S3JniHook_unref(&s->hooks.MEMBER) + UNHOOK(auth); + UNHOOK(busyHandler); + UNHOOK(collationNeeded); + UNHOOK(commit); + UNHOOK(progress); + UNHOOK(rollback); + UNHOOK(trace); + UNHOOK(update); +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK + UNHOOK(preUpdate); +#endif +#undef UNHOOK + S3JniUnrefGlobal(s->jDb); + memset(s, 0, sizeof(S3JniDb)); +} + +/* +** Clears s's state and moves it to the free-list. Requires that +** S3JniGlobal.perDb.mutex is locked. +*/ +static void S3JniDb__set_aside_unlocked(JNIEnv * const env, S3JniDb * const s){ + assert( s ); + S3JniDb_mutex_assertLocker; + if( s ){ + S3JniDb_clear(env, s); + s->pNext = SJG.perDb.aFree; + SJG.perDb.aFree = s; + } +} +#define S3JniDb_set_aside_unlocked(JniDb) S3JniDb__set_aside_unlocked(env, JniDb) + +static void S3JniDb__set_aside(JNIEnv * const env, S3JniDb * const s){ + S3JniDb_mutex_enter; + S3JniDb_set_aside_unlocked(s); + S3JniDb_mutex_leave; +} +#define S3JniDb_set_aside(JNIDB) S3JniDb__set_aside(env, JNIDB) + +/* +** Uncache any state for the given JNIEnv, clearing all Java +** references the cache owns. Returns true if env was cached and false +** if it was not found in the cache. Ownership of the S3JniEnv object +** associated with the given argument is transferred to this function, +** which makes it free for re-use. +** +** Requires that the env mutex be locked. +*/ +static int S3JniEnv_uncache(JNIEnv * const env){ + struct S3JniEnv * row; + struct S3JniEnv * pPrev = 0; + + S3JniEnv_mutex_assertLocked; + row = SJG.envCache.aHead; + for( ; row; pPrev = row, row = row->pNext ){ + if( row->env == env ){ + break; + } + } + if( !row ){ + return 0; + } + if( pPrev) pPrev->pNext = row->pNext; + else{ + assert( SJG.envCache.aHead == row ); + SJG.envCache.aHead = row->pNext; + } + memset(row, 0, sizeof(S3JniEnv)); + row->pNext = SJG.envCache.aFree; + SJG.envCache.aFree = row; + return 1; +} + +/* +** Fetches the given nph-ref from cache the cache and returns the +** object with its klazz member set. This is an O(1) operation except +** on the first call for a given pRef, during which pRef->klazz and +** pRef->pRef are initialized thread-safely. In the latter case it's +** still effectively O(1), but with a much longer 1. +** +** It is up to the caller to populate the other members of the +** returned object if needed, taking care to lock the modification +** with S3JniNph_mutex_enter/leave. +** +** This simple cache catches >99% of searches in the current +** (2023-07-31) tests. +*/ +static S3JniNphOp * s3jni__nphop(JNIEnv * const env, S3JniNphOp const* pRef){ + S3JniNphOp * const pNC = &SJG.nph.list[pRef->index]; + + assert( (void*)pRef>=(void*)&S3JniNphOps && (void*)pRef<(void*)(&S3JniNphOps + 1) + && "pRef is out of range" ); + assert( pRef->index>=0 + && (pRef->index < (sizeof(S3JniNphOps) / sizeof(S3JniNphOp))) + && "pRef->index is out of range" ); + if( !pNC->klazz ){ + S3JniNph_mutex_enter; + if( !pNC->klazz ){ + jclass const klazz = (*env)->FindClass(env, pRef->zName); + //printf("FindClass %s\n", pRef->zName); + S3JniExceptionIsFatal("FindClass() unexpectedly threw"); + pNC->klazz = S3JniRefGlobal(klazz); + } + S3JniNph_mutex_leave; + } + assert( pNC->klazz ); + return pNC; +} + +#define s3jni_nphop(PRef) s3jni__nphop(env, PRef) + +/* +** Common code for accessor functions for NativePointerHolder and +** OutputPointer types. pRef must be a pointer from S3JniNphOps. jOut +** must be an instance of that class (Java's type safety takes care of +** that requirement). If necessary, this fetches the jfieldID for +** jOut's pRef->zMember, which must be of the type represented by the +** JNI type signature pRef->zTypeSig, and stores it in +** S3JniGlobal.nph.list[pRef->index]. Fails fatally if the pRef->zMember +** property is not found, as that presents a serious internal misuse. +** +** Property lookups are cached on a per-pRef basis. +*/ +static jfieldID s3jni_nphop_field(JNIEnv * const env, S3JniNphOp const* pRef){ + S3JniNphOp * const pNC = s3jni_nphop(pRef); + + if( !pNC->fidValue ){ + S3JniNph_mutex_enter; + if( !pNC->fidValue ){ + pNC->fidValue = (*env)->GetFieldID(env, pNC->klazz, + pRef->zMember, pRef->zTypeSig); + S3JniExceptionIsFatal("Code maintenance required: missing " + "required S3JniNphOp::fidValue."); + } + S3JniNph_mutex_leave; + } + assert( pNC->fidValue ); + return pNC->fidValue; +} + +/* +** Sets a native ptr value in NativePointerHolder object jNph, +** which must be of the native type described by pRef. jNph +** may not be NULL. +*/ +static void NativePointerHolder__set(JNIEnv * const env, S3JniNphOp const* pRef, + jobject jNph, const void * p){ + assert( jNph ); + (*env)->SetLongField(env, jNph, s3jni_nphop_field(env, pRef), + S3JniCast_P2L(p)); + S3JniExceptionIsFatal("Could not set NativePointerHolder.nativePointer."); +} + +#define NativePointerHolder_set(PREF,JNPH,P) \ + NativePointerHolder__set(env, PREF, JNPH, P) + +/* +** Fetches a native ptr value from NativePointerHolder object jNph, +** which must be of the native type described by pRef. This is a +** no-op if jNph is NULL. +*/ +static void * NativePointerHolder__get(JNIEnv * env, jobject jNph, + S3JniNphOp const* pRef){ + void * rv = 0; + if( jNph ){ + rv = S3JniCast_L2P( + (*env)->GetLongField(env, jNph, s3jni_nphop_field(env, pRef)) + ); + S3JniExceptionIsFatal("Cannot fetch NativePointerHolder.nativePointer."); + } + return rv; +} + +#define NativePointerHolder_get(JOBJ,NPHREF) \ + NativePointerHolder__get(env, (JOBJ), (NPHREF)) + +/* +** Helpers for extracting pointers from jobjects, noting that we rely +** on the corresponding Java interfaces having already done the +** type-checking. OBJ must be a jobject referring to a +** NativePointerHolder, where T matches PtrGet_T. Don't use these +** in contexts where that's not the case. Note that these aren't +** type-safe in the strictest sense: +** +** sqlite3 * s = PtrGet_sqlite3_stmt(...) +** +** will work, despite the incorrect macro name, so long as the +** argument is a Java sqlite3 object, as this operation only has void +** pointers to work with. +*/ +#define PtrGet_T(T,JOBJ) (T*)NativePointerHolder_get((JOBJ), S3JniNph(T)) +#define PtrGet_sqlite3(JOBJ) PtrGet_T(sqlite3, (JOBJ)) +#define PtrGet_sqlite3_backup(JOBJ) PtrGet_T(sqlite3_backup, (JOBJ)) +#define PtrGet_sqlite3_blob(JOBJ) PtrGet_T(sqlite3_blob, (JOBJ)) +#define PtrGet_sqlite3_context(JOBJ) PtrGet_T(sqlite3_context, (JOBJ)) +#define PtrGet_sqlite3_stmt(JOBJ) PtrGet_T(sqlite3_stmt, (JOBJ)) +#define PtrGet_sqlite3_value(JOBJ) PtrGet_T(sqlite3_value, (JOBJ)) +/* +** LongPtrGet_T(X,Y) expects X to be an unqualified sqlite3 struct +** type name and Y to be a native pointer to such an object in the +** form of a jlong value. The jlong is simply cast to (X*). This +** approach is, as of 2023-09-27, supplanting the former approach. We +** now do the native pointer extraction in the Java side, rather than +** the C side, because it's reportedly significantly faster. The +** intptr_t part here is necessary for compatibility with (at least) +** ARM32. +** +** 2023-11-09: testing has not revealed any measurable performance +** difference between the approach of passing type T to C compared to +** passing pointer-to-T to C, and adding support for the latter +** everywhere requires significantly more code. As of this writing, the +** older/simpler approach is being applied except for (A) where the +** newer approach has already been applied and (B) hot-spot APIs where +** a difference of microseconds (i.e. below our testing measurement +** threshold) might add up. +*/ +#define LongPtrGet_T(T,JLongAsPtr) (T*)((intptr_t)((JLongAsPtr))) +#define LongPtrGet_sqlite3(JLongAsPtr) LongPtrGet_T(sqlite3,(JLongAsPtr)) +#define LongPtrGet_sqlite3_backup(JLongAsPtr) LongPtrGet_T(sqlite3_backup,(JLongAsPtr)) +#define LongPtrGet_sqlite3_blob(JLongAsPtr) LongPtrGet_T(sqlite3_blob,(JLongAsPtr)) +#define LongPtrGet_sqlite3_stmt(JLongAsPtr) LongPtrGet_T(sqlite3_stmt,(JLongAsPtr)) +#define LongPtrGet_sqlite3_value(JLongAsPtr) LongPtrGet_T(sqlite3_value,(JLongAsPtr)) +/* +** Extracts the new S3JniDb instance from the free-list, or allocates +** one if needed, associates it with pDb, and returns. Returns NULL +** on OOM. The returned object MUST, on success of the calling +** operation, subsequently be associated with jDb via +** NativePointerHolder_set() or freed using S3JniDb_set_aside(). +*/ +static S3JniDb * S3JniDb_alloc(JNIEnv * const env, jobject jDb){ + S3JniDb * rv = 0; + S3JniDb_mutex_enter; + if( SJG.perDb.aFree ){ + rv = SJG.perDb.aFree; + SJG.perDb.aFree = rv->pNext; + rv->pNext = 0; + s3jni_incr( &SJG.metrics.nPdbRecycled ); + } + S3JniDb_mutex_leave; + if( 0==rv ){ + rv = s3jni_malloc(sizeof(S3JniDb)); + if( rv ){ + s3jni_incr( &SJG.metrics.nPdbAlloc ); + } + } + if( rv ){ + memset(rv, 0, sizeof(S3JniDb)); + rv->jDb = S3JniRefGlobal(jDb); + } + return rv; +} + +/* +** Returns the S3JniDb object for the given org.sqlite.jni.capi.sqlite3 +** object, or NULL if jDb is NULL, no pointer can be extracted +** from it, or no matching entry can be found. +*/ +static S3JniDb * S3JniDb__from_java(JNIEnv * const env, jobject jDb){ + sqlite3 * const pDb = jDb ? PtrGet_sqlite3(jDb) : 0; + return pDb ? S3JniDb_from_clientdata(pDb) : 0; +} +#define S3JniDb_from_java(jObject) S3JniDb__from_java(env,(jObject)) + +/* +** S3JniDb finalizer for use with sqlite3_set_clientdata(). +*/ +static void S3JniDb_xDestroy(void *p){ + S3JniDeclLocal_env; + S3JniDb * const ps = p; + assert( !ps->pNext && "Else ps is already in the free-list."); + S3JniDb_set_aside(ps); +} + +/* +** Evaluates to the S3JniDb object for the given sqlite3 object, or +** NULL if pDb is NULL or was not initialized via the JNI interfaces. +*/ +#define S3JniDb_from_c(sqlite3Ptr) \ + ((sqlite3Ptr) ? S3JniDb_from_clientdata(sqlite3Ptr) : 0) +#define S3JniDb_from_jlong(sqlite3PtrAsLong) \ + S3JniDb_from_c(LongPtrGet_T(sqlite3,sqlite3PtrAsLong)) + +/* +** Unref any Java-side state in (S3JniAutoExtension*) AX and zero out +** AX. +*/ +#define S3JniAutoExtension_clear(AX) S3JniHook_unref(AX); + +/* +** Initializes a pre-allocated S3JniAutoExtension object. Returns +** non-0 if there is an error collecting the required state from +** jAutoExt (which must be an AutoExtensionCallback object). On error, +** it passes ax to S3JniAutoExtension_clear(). +*/ +static int S3JniAutoExtension_init(JNIEnv *const env, + S3JniAutoExtension * const ax, + jobject const jAutoExt){ + jclass const klazz = (*env)->GetObjectClass(env, jAutoExt); + + S3JniAutoExt_mutex_assertLocker; + *ax = S3JniHook_empty; + ax->midCallback = (*env)->GetMethodID(env, klazz, "call", + "(Lorg/sqlite/jni/capi/sqlite3;)I"); + S3JniUnrefLocal(klazz); + S3JniExceptionWarnIgnore; + if( !ax->midCallback ){ + S3JniAutoExtension_clear(ax); + return SQLITE_ERROR; + } + ax->jObj = S3JniRefGlobal(jAutoExt); + return 0; +} + +/* +** Sets the value property of the OutputPointer.Bool jOut object to +** v. +*/ +static void OutputPointer_set_Bool(JNIEnv * const env, jobject const jOut, + int v){ + (*env)->SetBooleanField(env, jOut, s3jni_nphop_field( + env, S3JniNph(OutputPointer_Bool) + ), v ? JNI_TRUE : JNI_FALSE ); + S3JniExceptionIsFatal("Cannot set OutputPointer.Bool.value"); +} + +/* +** Sets the value property of the OutputPointer.Int32 jOut object to +** v. +*/ +static void OutputPointer_set_Int32(JNIEnv * const env, jobject const jOut, + int v){ + (*env)->SetIntField(env, jOut, s3jni_nphop_field( + env, S3JniNph(OutputPointer_Int32) + ), (jint)v); + S3JniExceptionIsFatal("Cannot set OutputPointer.Int32.value"); +} + +/* +** Sets the value property of the OutputPointer.Int64 jOut object to +** v. +*/ +static void OutputPointer_set_Int64(JNIEnv * const env, jobject const jOut, + jlong v){ + (*env)->SetLongField(env, jOut, s3jni_nphop_field( + env, S3JniNph(OutputPointer_Int64) + ), v); + S3JniExceptionIsFatal("Cannot set OutputPointer.Int64.value"); +} + +/* +** Internal helper for OutputPointer_set_TYPE() where TYPE is an +** Object type. +*/ +static void OutputPointer_set_obj(JNIEnv * const env, + S3JniNphOp const * const pRef, + jobject const jOut, + jobject v){ + (*env)->SetObjectField(env, jOut, s3jni_nphop_field(env, pRef), v); + S3JniExceptionIsFatal("Cannot set OutputPointer.T.value"); +} + +#ifdef SQLITE_ENABLE_FTS5 +#if 0 +/* +** Sets the value property of the OutputPointer.ByteArray jOut object +** to v. +*/ +static void OutputPointer_set_ByteArray(JNIEnv * const env, jobject const jOut, + jbyteArray const v){ + OutputPointer_set_obj(env, S3JniNph(OutputPointer_ByteArray), jOut, v); +} +#endif +#endif /* SQLITE_ENABLE_FTS5 */ + +/* +** Sets the value property of the OutputPointer.String jOut object to +** v. +*/ +static void OutputPointer_set_String(JNIEnv * const env, jobject const jOut, + jstring const v){ + OutputPointer_set_obj(env, S3JniNph(OutputPointer_String), jOut, v); +} + +/* +** Returns true if eTextRep is a valid sqlite3 encoding constant, else +** returns false. +*/ +static int encodingTypeIsValid(int eTextRep){ + switch( eTextRep ){ + case SQLITE_UTF8: case SQLITE_UTF16: + case SQLITE_UTF16LE: case SQLITE_UTF16BE: + return 1; + default: + return 0; + } +} + +/* For use with sqlite3_result_pointer(), sqlite3_value_pointer(), + sqlite3_bind_java_object(), and sqlite3_column_java_object(). */ +static const char * const s3jni__value_jref_key = "org.sqlite.jni.capi.ResultJavaVal"; + +/* +** If v is not NULL, it must be a jobject global reference. Its +** reference is relinquished. +*/ +static void S3Jni_jobject_finalizer(void *v){ + if( v ){ + S3JniDeclLocal_env; + S3JniUnrefGlobal((jobject)v); + } +} + +/* +** Returns a new Java instance of the class referred to by pRef, which +** MUST be interface-compatible with NativePointerHolder and MUST have +** a no-arg constructor. The NativePointerHolder_set() method is +** passed the new Java object (which must not be NULL) and pNative +** (which may be NULL). Hypothetically returns NULL if Java fails to +** allocate, but the JNI docs are not entirely clear on that detail. +** +** Always use a static pointer from the S3JniNphOps struct for the +** 2nd argument. +*/ +static jobject NativePointerHolder_new(JNIEnv * const env, + S3JniNphOp const * pRef, + const void * pNative){ + jobject rv = 0; + S3JniNphOp * const pNC = s3jni_nphop(pRef); + if( !pNC->midCtor ){ + S3JniNph_mutex_enter; + if( !pNC->midCtor ){ + pNC->midCtor = (*env)->GetMethodID(env, pNC->klazz, "", "()V"); + S3JniExceptionIsFatal("Cannot find constructor for class."); + } + S3JniNph_mutex_leave; + } + rv = (*env)->NewObject(env, pNC->klazz, pNC->midCtor); + S3JniExceptionIsFatal("No-arg constructor threw."); + s3jni_oom_check(rv); + if( rv ) NativePointerHolder_set(pRef, rv, pNative); + return rv; +} + +static inline jobject new_java_sqlite3(JNIEnv * const env, sqlite3 *sv){ + return NativePointerHolder_new(env, S3JniNph(sqlite3), sv); +} +static inline jobject new_java_sqlite3_backup(JNIEnv * const env, sqlite3_backup *sv){ + return NativePointerHolder_new(env, S3JniNph(sqlite3_backup), sv); +} +static inline jobject new_java_sqlite3_blob(JNIEnv * const env, sqlite3_blob *sv){ + return NativePointerHolder_new(env, S3JniNph(sqlite3_blob), sv); +} +static inline jobject new_java_sqlite3_context(JNIEnv * const env, sqlite3_context *sv){ + return NativePointerHolder_new(env, S3JniNph(sqlite3_context), sv); +} +static inline jobject new_java_sqlite3_stmt(JNIEnv * const env, sqlite3_stmt *sv){ + return NativePointerHolder_new(env, S3JniNph(sqlite3_stmt), sv); +} +static inline jobject new_java_sqlite3_value(JNIEnv * const env, sqlite3_value *sv){ + return NativePointerHolder_new(env, S3JniNph(sqlite3_value), sv); +} + +/* Helper typedefs for UDF callback types. */ +typedef void (*udf_xFunc_f)(sqlite3_context*,int,sqlite3_value**); +typedef void (*udf_xStep_f)(sqlite3_context*,int,sqlite3_value**); +typedef void (*udf_xFinal_f)(sqlite3_context*); +/*typedef void (*udf_xValue_f)(sqlite3_context*);*/ +/*typedef void (*udf_xInverse_f)(sqlite3_context*,int,sqlite3_value**);*/ + +/* +** Allocate a new S3JniUdf (User-defined Function) and associate it +** with the SQLFunction-type jObj. Returns NULL on OOM. If the +** returned object's type==UDF_UNKNOWN_TYPE then the type of UDF was +** not unambiguously detected based on which callback members it has, +** which falls into the category of user error. +** +** The caller must arrange for the returned object to eventually be +** passed to S3JniUdf_free(). +*/ +static S3JniUdf * S3JniUdf_alloc(JNIEnv * const env, jobject jObj){ + S3JniUdf * s = 0; + + S3JniGlobal_mutex_enter; + s3jni_incr(&SJG.metrics.nMutexUdf); + if( SJG.udf.aFree ){ + s = SJG.udf.aFree; + SJG.udf.aFree = s->pNext; + s->pNext = 0; + s3jni_incr(&SJG.metrics.nUdfRecycled); + } + S3JniGlobal_mutex_leave; + if( !s ){ + s = s3jni_malloc( sizeof(*s)); + s3jni_incr(&SJG.metrics.nUdfAlloc); + } + if( s ){ + const char * zFSI = /* signature for xFunc, xStep, xInverse */ + "(Lorg/sqlite/jni/capi/sqlite3_context;[Lorg/sqlite/jni/capi/sqlite3_value;)V"; + const char * zFV = /* signature for xFinal, xValue */ + "(Lorg/sqlite/jni/capi/sqlite3_context;)V"; + jclass const klazz = (*env)->GetObjectClass(env, jObj); + + memset(s, 0, sizeof(*s)); + s->jObj = S3JniRefGlobal(jObj); + +#define FGET(FuncName,FuncSig,Field) \ + s->Field = (*env)->GetMethodID(env, klazz, FuncName, FuncSig); \ + if( !s->Field ) (*env)->ExceptionClear(env) + + FGET("xFunc", zFSI, jmidxFunc); + FGET("xStep", zFSI, jmidxStep); + FGET("xFinal", zFV, jmidxFinal); + FGET("xValue", zFV, jmidxValue); + FGET("xInverse", zFSI, jmidxInverse); +#undef FGET + + S3JniUnrefLocal(klazz); + if( s->jmidxFunc ) s->type = UDF_SCALAR; + else if( s->jmidxStep && s->jmidxFinal ){ + s->type = (s->jmidxValue && s->jmidxInverse) + ? UDF_WINDOW : UDF_AGGREGATE; + }else{ + s->type = UDF_UNKNOWN_TYPE; + } + } + return s; +} + +/* +** Frees up all resources owned by s, clears its state, then either +** caches it for reuse (if cacheIt is true) or frees it. The former +** requires locking the global mutex, so it must not be held when this +** is called. +*/ +static void S3JniUdf_free(JNIEnv * const env, S3JniUdf * const s, + int cacheIt){ + assert( !s->pNext ); + if( s->jObj ){ + s3jni_call_xDestroy(s->jObj); + S3JniUnrefGlobal(s->jObj); + sqlite3_free(s->zFuncName); + assert( !s->pNext ); + memset(s, 0, sizeof(*s)); + } + if( cacheIt ){ + S3JniGlobal_mutex_enter; + s->pNext = S3JniGlobal.udf.aFree; + S3JniGlobal.udf.aFree = s; + S3JniGlobal_mutex_leave; + }else{ + sqlite3_free( s ); + } +} + +/* Finalizer for sqlite3_create_function() and friends. */ +static void S3JniUdf_finalizer(void * s){ + S3JniUdf_free(s3jni_env(), (S3JniUdf*)s, 1); +} + +/* +** Helper for processing args to UDF handlers with signature +** (sqlite3_context*,int,sqlite3_value**). +*/ +typedef struct { + jobject jcx /* sqlite3_context */; + jobjectArray jargv /* sqlite3_value[] */; +} udf_jargs; + +/* +** Converts the given (cx, argc, argv) into arguments for the given +** UDF, writing the result (Java wrappers for cx and argv) in the +** final 2 arguments. Returns 0 on success, SQLITE_NOMEM on allocation +** error. On error *jCx and *jArgv will be set to 0. The output +** objects are of type org.sqlite.jni.capi.sqlite3_context and +** array-of-org.sqlite.jni.capi.sqlite3_value, respectively. +*/ +static int udf_args(JNIEnv *env, + sqlite3_context * const cx, + int argc, sqlite3_value**argv, + jobject * jCx, jobjectArray *jArgv){ + jobjectArray ja = 0; + jobject jcx = new_java_sqlite3_context(env, cx); + jint i; + *jCx = 0; + *jArgv = 0; + if( !jcx ) goto error_oom; + ja = (*env)->NewObjectArray( + env, argc, s3jni_nphop(S3JniNph(sqlite3_value))->klazz, + NULL); + s3jni_oom_check( ja ); + if( !ja ) goto error_oom; + for(i = 0; i < argc; ++i){ + jobject jsv = new_java_sqlite3_value(env, argv[i]); + if( !jsv ) goto error_oom; + (*env)->SetObjectArrayElement(env, ja, i, jsv); + S3JniUnrefLocal(jsv)/*ja has a ref*/; + } + *jCx = jcx; + *jArgv = ja; + return 0; +error_oom: + S3JniUnrefLocal(jcx); + S3JniUnrefLocal(ja); + return SQLITE_NOMEM; +} + +/* +** Requires that jCx and jArgv are sqlite3_context +** resp. array-of-sqlite3_value values initialized by udf_args(). The +** latter will be 0-and-NULL for UDF types with no arguments. This +** function zeroes out the nativePointer member of jCx and each entry +** in jArgv. This is a safety-net precaution to avoid undefined +** behavior if a Java-side UDF holds a reference to its context or one +** of its arguments. This MUST be called from any function which +** successfully calls udf_args(), after calling the corresponding UDF +** and checking its exception status, or which Java-wraps a +** sqlite3_context for use with a UDF(ish) call. It MUST NOT be called +** in any other case. +*/ +static void udf_unargs(JNIEnv *env, jobject jCx, int argc, jobjectArray jArgv){ + int i = 0; + assert(jCx); + NativePointerHolder_set(S3JniNph(sqlite3_context), jCx, 0); + for( ; i < argc; ++i ){ + jobject jsv = (*env)->GetObjectArrayElement(env, jArgv, i); + /* + ** There is a potential Java-triggerable case of Undefined + ** Behavior here, but it would require intentional misuse of the + ** API: + ** + ** If a Java UDF grabs an sqlite3_value from its argv and then + ** assigns that element to null, it becomes unreachable to us so + ** we cannot clear out its pointer. That Java-side object's + ** getNativePointer() will then refer to a stale value, so passing + ** it into (e.g.) sqlite3_value_SOMETHING() would invoke UB. + ** + ** High-level wrappers can avoid that possibility if they do not + ** expose sqlite3_value directly to clients (as is the case in + ** org.sqlite.jni.wrapper1.SqlFunction). + ** + ** One potential (but expensive) workaround for this would be to + ** privately store a duplicate argv array in each sqlite3_context + ** wrapper object, and clear the native pointers from that copy. + */ + assert(jsv && "Someone illegally modified a UDF argument array."); + if( jsv ){ + NativePointerHolder_set(S3JniNph(sqlite3_value), jsv, 0); + } + } +} + + +/* +** Must be called immediately after a Java-side UDF callback throws. +** If translateToErr is true then it sets the exception's message in +** the result error using sqlite3_result_error(). If translateToErr is +** false then it emits a warning that the function threw but should +** not do so. In either case, it clears the exception state. +** +** Returns SQLITE_NOMEM if an allocation fails, else SQLITE_ERROR. In +** the former case it calls sqlite3_result_error_nomem(). +*/ +static int udf_report_exception(JNIEnv * const env, int translateToErr, + sqlite3_context * cx, + const char *zFuncName, const char *zFuncType ){ + jthrowable const ex = (*env)->ExceptionOccurred(env); + int rc = SQLITE_ERROR; + + assert(ex && "This must only be called when a Java exception is pending."); + if( translateToErr ){ + char * zMsg; + char * z; + + S3JniExceptionClear; + zMsg = s3jni_exception_error_msg(env, ex); + z = sqlite3_mprintf("Client-defined SQL function %s.%s() threw: %s", + zFuncName ? zFuncName : "", zFuncType, + zMsg ? zMsg : "Unknown exception" ); + sqlite3_free(zMsg); + if( z ){ + sqlite3_result_error(cx, z, -1); + sqlite3_free(z); + }else{ + sqlite3_result_error_nomem(cx); + rc = SQLITE_NOMEM; + } + }else{ + S3JniExceptionWarnCallbackThrew("client-defined SQL function"); + S3JniExceptionClear; + } + S3JniUnrefLocal(ex); + return rc; +} + +/* +** Sets up the state for calling a Java-side xFunc/xStep/xInverse() +** UDF, calls it, and returns 0 on success. +*/ +static int udf_xFSI(sqlite3_context* const pCx, int argc, + sqlite3_value** const argv, S3JniUdf * const s, + jmethodID xMethodID, const char * const zFuncType){ + S3JniDeclLocal_env; + udf_jargs args = {0,0}; + int rc = udf_args(env, pCx, argc, argv, &args.jcx, &args.jargv); + + if( 0 == rc ){ + (*env)->CallVoidMethod(env, s->jObj, xMethodID, args.jcx, args.jargv); + S3JniIfThrew{ + rc = udf_report_exception(env, 'F'==zFuncType[1]/*xFunc*/, pCx, + s->zFuncName, zFuncType); + } + udf_unargs(env, args.jcx, argc, args.jargv); + } + S3JniUnrefLocal(args.jcx); + S3JniUnrefLocal(args.jargv); + return rc; +} + +/* +** Sets up the state for calling a Java-side xFinal/xValue() UDF, +** calls it, and returns 0 on success. +*/ +static int udf_xFV(sqlite3_context* cx, S3JniUdf * s, + jmethodID xMethodID, + const char *zFuncType){ + S3JniDeclLocal_env; + jobject jcx = new_java_sqlite3_context(env, cx); + int rc = 0; + int const isFinal = 'F'==zFuncType[1]/*xFinal*/; + + if( jcx ){ + (*env)->CallVoidMethod(env, s->jObj, xMethodID, jcx); + S3JniIfThrew{ + rc = udf_report_exception(env, isFinal, cx, s->zFuncName, + zFuncType); + } + udf_unargs(env, jcx, 0, 0); + S3JniUnrefLocal(jcx); + }else{ + if( isFinal ) sqlite3_result_error_nomem(cx); + rc = SQLITE_NOMEM; + } + return rc; +} + +/* Proxy for C-to-Java xFunc. */ +static void udf_xFunc(sqlite3_context* cx, int argc, + sqlite3_value** argv){ + S3JniUdf * const s = (S3JniUdf*)sqlite3_user_data(cx); + s3jni_incr( &SJG.metrics.udf.nFunc ); + udf_xFSI(cx, argc, argv, s, s->jmidxFunc, "xFunc"); +} +/* Proxy for C-to-Java xStep. */ +static void udf_xStep(sqlite3_context* cx, int argc, + sqlite3_value** argv){ + S3JniUdf * const s = (S3JniUdf*)sqlite3_user_data(cx); + s3jni_incr( &SJG.metrics.udf.nStep ); + udf_xFSI(cx, argc, argv, s, s->jmidxStep, "xStep"); +} +/* Proxy for C-to-Java xFinal. */ +static void udf_xFinal(sqlite3_context* cx){ + S3JniUdf * const s = (S3JniUdf*)sqlite3_user_data(cx); + s3jni_incr( &SJG.metrics.udf.nFinal ); + udf_xFV(cx, s, s->jmidxFinal, "xFinal"); +} +/* Proxy for C-to-Java xValue. */ +static void udf_xValue(sqlite3_context* cx){ + S3JniUdf * const s = (S3JniUdf*)sqlite3_user_data(cx); + s3jni_incr( &SJG.metrics.udf.nValue ); + udf_xFV(cx, s, s->jmidxValue, "xValue"); +} +/* Proxy for C-to-Java xInverse. */ +static void udf_xInverse(sqlite3_context* cx, int argc, + sqlite3_value** argv){ + S3JniUdf * const s = (S3JniUdf*)sqlite3_user_data(cx); + s3jni_incr( &SJG.metrics.udf.nInverse ); + udf_xFSI(cx, argc, argv, s, s->jmidxInverse, "xInverse"); +} + + +//////////////////////////////////////////////////////////////////////// +// What follows is the JNI/C bindings. They are in alphabetical order +// except for this macro-generated subset which are kept together +// (alphabetized) here at the front... +//////////////////////////////////////////////////////////////////////// + +/** Create a trivial JNI wrapper for (int CName(void)). */ +#define WRAP_INT_VOID(JniNameSuffix,CName) \ + JniDecl(jint,JniNameSuffix)(JniArgsEnvClass){ \ + return (jint)CName(); \ + } +/** Create a trivial JNI wrapper for (int CName(int)). */ +#define WRAP_INT_INT(JniNameSuffix,CName) \ + JniDecl(jint,JniNameSuffix)(JniArgsEnvClass, jint arg){ \ + return (jint)CName((int)arg); \ + } +/* +** Create a trivial JNI wrapper for (const mutf8_string * +** CName(void)). This is only valid for functions which are known to +** return ASCII or text which is equivalent in UTF-8 and MUTF-8. +*/ +#define WRAP_MUTF8_VOID(JniNameSuffix,CName) \ + JniDecl(jstring,JniNameSuffix)(JniArgsEnvClass){ \ + jstring const rv = (*env)->NewStringUTF( env, CName() ); \ + s3jni_oom_check(rv); \ + return rv; \ + } +/** Create a trivial JNI wrapper for (int CName(sqlite3_stmt*)). */ +#define WRAP_INT_STMT(JniNameSuffix,CName) \ + JniDecl(jint,JniNameSuffix)(JniArgsEnvClass, jlong jpStmt){ \ + return (jint)CName(LongPtrGet_sqlite3_stmt(jpStmt)); \ + } +/** Create a trivial JNI wrapper for (int CName(sqlite3_stmt*,int)). */ +#define WRAP_INT_STMT_INT(JniNameSuffix,CName) \ + JniDecl(jint,JniNameSuffix)(JniArgsEnvClass, jlong jpStmt, jint n){ \ + return (jint)CName(LongPtrGet_sqlite3_stmt(jpStmt), (int)n); \ + } +/** Create a trivial JNI wrapper for (boolean CName(sqlite3_stmt*)). */ +#define WRAP_BOOL_STMT(JniNameSuffix,CName) \ + JniDecl(jboolean,JniNameSuffix)(JniArgsEnvClass, jobject jStmt){ \ + return CName(PtrGet_sqlite3_stmt(jStmt)) ? JNI_TRUE : JNI_FALSE; \ + } +/** Create a trivial JNI wrapper for (jstring CName(sqlite3_stmt*,int)). */ +#define WRAP_STR_STMT_INT(JniNameSuffix,CName) \ + JniDecl(jstring,JniNameSuffix)(JniArgsEnvClass, jlong jpStmt, jint ndx){ \ + return s3jni_utf8_to_jstring( \ + CName(LongPtrGet_sqlite3_stmt(jpStmt), (int)ndx), \ + -1); \ + } +/** Create a trivial JNI wrapper for (boolean CName(sqlite3*)). */ +#define WRAP_BOOL_DB(JniNameSuffix,CName) \ + JniDecl(jboolean,JniNameSuffix)(JniArgsEnvClass, jlong jpDb){ \ + return CName(LongPtrGet_sqlite3(jpDb)) ? JNI_TRUE : JNI_FALSE; \ + } +/** Create a trivial JNI wrapper for (int CName(sqlite3*)). */ +#define WRAP_INT_DB(JniNameSuffix,CName) \ + JniDecl(jint,JniNameSuffix)(JniArgsEnvClass, jlong jpDb){ \ + return (jint)CName(LongPtrGet_sqlite3(jpDb)); \ + } +/** Create a trivial JNI wrapper for (int64 CName(sqlite3*)). */ +#define WRAP_INT64_DB(JniNameSuffix,CName) \ + JniDecl(jlong,JniNameSuffix)(JniArgsEnvClass, jlong jpDb){ \ + return (jlong)CName(LongPtrGet_sqlite3(jpDb)); \ + } +/** Create a trivial JNI wrapper for (jstring CName(sqlite3*,int)). */ +#define WRAP_STR_DB_INT(JniNameSuffix,CName) \ + JniDecl(jstring,JniNameSuffix)(JniArgsEnvClass, jlong jpDb, jint ndx){ \ + return s3jni_utf8_to_jstring( \ + CName(LongPtrGet_sqlite3(jpDb), (int)ndx), \ + -1); \ + } +/** Create a trivial JNI wrapper for (int CName(sqlite3_value*)). */ +#define WRAP_INT_SVALUE(JniNameSuffix,CName,DfltOnNull) \ + JniDecl(jint,JniNameSuffix)(JniArgsEnvClass, jlong jpSValue){ \ + sqlite3_value * const sv = LongPtrGet_sqlite3_value(jpSValue); \ + return (jint)(sv ? CName(sv): DfltOnNull); \ + } +/** Create a trivial JNI wrapper for (boolean CName(sqlite3_value*)). */ +#define WRAP_BOOL_SVALUE(JniNameSuffix,CName,DfltOnNull) \ + JniDecl(jboolean,JniNameSuffix)(JniArgsEnvClass, jlong jpSValue){ \ + sqlite3_value * const sv = LongPtrGet_sqlite3_value(jpSValue); \ + return (jint)(sv ? CName(sv) : DfltOnNull) \ + ? JNI_TRUE : JNI_FALSE; \ + } + +WRAP_INT_DB(1changes, sqlite3_changes) +WRAP_INT64_DB(1changes64, sqlite3_changes64) +WRAP_INT_STMT(1clear_1bindings, sqlite3_clear_bindings) +WRAP_INT_STMT_INT(1column_1bytes, sqlite3_column_bytes) +WRAP_INT_STMT_INT(1column_1bytes16, sqlite3_column_bytes16) +WRAP_INT_STMT(1column_1count, sqlite3_column_count) +WRAP_STR_STMT_INT(1column_1decltype, sqlite3_column_decltype) +WRAP_STR_STMT_INT(1column_1name, sqlite3_column_name) +#ifdef SQLITE_ENABLE_COLUMN_METADATA +WRAP_STR_STMT_INT(1column_1database_1name, sqlite3_column_database_name) +WRAP_STR_STMT_INT(1column_1origin_1name, sqlite3_column_origin_name) +WRAP_STR_STMT_INT(1column_1table_1name, sqlite3_column_table_name) +#endif +WRAP_INT_STMT_INT(1column_1type, sqlite3_column_type) +WRAP_INT_STMT(1data_1count, sqlite3_data_count) +WRAP_STR_DB_INT(1db_1name, sqlite3_db_name) +WRAP_INT_DB(1error_1offset, sqlite3_error_offset) +WRAP_INT_DB(1extended_1errcode, sqlite3_extended_errcode) +WRAP_BOOL_DB(1get_1autocommit, sqlite3_get_autocommit) +WRAP_MUTF8_VOID(1libversion, sqlite3_libversion) +WRAP_INT_VOID(1libversion_1number, sqlite3_libversion_number) +WRAP_INT_VOID(1keyword_1count, sqlite3_keyword_count) +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK +WRAP_INT_DB(1preupdate_1blobwrite, sqlite3_preupdate_blobwrite) +WRAP_INT_DB(1preupdate_1count, sqlite3_preupdate_count) +WRAP_INT_DB(1preupdate_1depth, sqlite3_preupdate_depth) +#endif +WRAP_INT_INT(1release_1memory, sqlite3_release_memory) +WRAP_INT_INT(1sleep, sqlite3_sleep) +WRAP_MUTF8_VOID(1sourceid, sqlite3_sourceid) +WRAP_BOOL_STMT(1stmt_1busy, sqlite3_stmt_busy) +WRAP_INT_STMT_INT(1stmt_1explain, sqlite3_stmt_explain) +WRAP_INT_STMT(1stmt_1isexplain, sqlite3_stmt_isexplain) +WRAP_BOOL_STMT(1stmt_1readonly, sqlite3_stmt_readonly) +WRAP_INT_DB(1system_1errno, sqlite3_system_errno) +WRAP_INT_VOID(1threadsafe, sqlite3_threadsafe) +WRAP_INT_DB(1total_1changes, sqlite3_total_changes) +WRAP_INT64_DB(1total_1changes64, sqlite3_total_changes64) +WRAP_INT_SVALUE(1value_1encoding, sqlite3_value_encoding,SQLITE_UTF8) +WRAP_BOOL_SVALUE(1value_1frombind, sqlite3_value_frombind,0) +WRAP_INT_SVALUE(1value_1nochange, sqlite3_value_nochange,0) +WRAP_INT_SVALUE(1value_1numeric_1type, sqlite3_value_numeric_type,SQLITE_NULL) +WRAP_INT_SVALUE(1value_1subtype, sqlite3_value_subtype,0) +WRAP_INT_SVALUE(1value_1type, sqlite3_value_type,SQLITE_NULL) + +#undef WRAP_BOOL_DB +#undef WRAP_BOOL_STMT +#undef WRAP_BOOL_SVALUE +#undef WRAP_INT64_DB +#undef WRAP_INT_DB +#undef WRAP_INT_INT +#undef WRAP_INT_STMT +#undef WRAP_INT_STMT_INT +#undef WRAP_INT_SVALUE +#undef WRAP_INT_VOID +#undef WRAP_MUTF8_VOID +#undef WRAP_STR_STMT_INT +#undef WRAP_STR_DB_INT + +S3JniApi(sqlite3_aggregate_context(),jlong,1aggregate_1context)( + JniArgsEnvClass, jobject jCx, jboolean initialize +){ + sqlite3_context * const pCx = PtrGet_sqlite3_context(jCx); + void * const p = pCx + ? sqlite3_aggregate_context(pCx, (int)(initialize + ? (int)sizeof(void*) + : 0)) + : 0; + return S3JniCast_P2L(p); +} + +/* +** Central auto-extension runner for auto-extensions created in Java. +*/ +static int s3jni_run_java_auto_extensions(sqlite3 *pDb, const char **pzErr, + const struct sqlite3_api_routines *ignored){ + int rc = 0; + unsigned i, go = 1; + JNIEnv * env = 0; + S3JniDb * ps; + S3JniEnv * jc; + + if( 0==SJG.autoExt.nExt ) return 0; + env = s3jni_env(); + jc = S3JniEnv_get(); + S3JniDb_mutex_enter; + ps = jc->pdbOpening ? jc->pdbOpening : S3JniDb_from_c(pDb); + if( !ps ){ + *pzErr = sqlite3_mprintf("Unexpected arrival of null S3JniDb in " + "auto-extension runner."); + S3JniDb_mutex_leave; + return SQLITE_ERROR; + } + assert( ps->jDb ); + if( !ps->pDb ){ + assert( jc->pdbOpening == ps ); + rc = sqlite3_set_clientdata(pDb, S3JniDb_clientdata_key, + ps, 0/* we'll re-set this after open() + completes. */); + if( rc ){ + S3JniDb_mutex_leave; + return rc; + } + } + else{ + assert( ps == jc->pdbOpening ); + jc->pdbOpening = 0; + } + S3JniDb_mutex_leave; + NativePointerHolder_set(S3JniNph(sqlite3), ps->jDb, pDb) + /* As of here, the Java/C connection is complete except for the + (temporary) lack of finalizer for the ps object. */; + ps->pDb = pDb; + for( i = 0; go && 0==rc; ++i ){ + S3JniAutoExtension ax = S3JniHook_empty + /* We need a copy of the auto-extension object, with our own + ** local reference to it, to avoid a race condition with another + ** thread manipulating the list during the call and invaliding + ** what ax references. */; + S3JniAutoExt_mutex_enter; + if( i >= SJG.autoExt.nExt ){ + go = 0; + }else{ + S3JniHook_localdup(&SJG.autoExt.aExt[i], &ax); + } + S3JniAutoExt_mutex_leave; + if( ax.jObj ){ + rc = (*env)->CallIntMethod(env, ax.jObj, ax.midCallback, ps->jDb); + S3JniHook_localundup(ax); + S3JniIfThrew { + jthrowable const ex = (*env)->ExceptionOccurred(env); + char * zMsg; + S3JniExceptionClear; + zMsg = s3jni_exception_error_msg(env, ex); + S3JniUnrefLocal(ex); + *pzErr = sqlite3_mprintf("auto-extension threw: %s", zMsg); + sqlite3_free(zMsg); + rc = SQLITE_ERROR; + } + } + } + return rc; +} + +S3JniApi(sqlite3_auto_extension(),jint,1auto_1extension)( + JniArgsEnvClass, jobject jAutoExt +){ + int i; + S3JniAutoExtension * ax = 0; + int rc = 0; + + if( !jAutoExt ) return SQLITE_MISUSE; + S3JniAutoExt_mutex_enter; + for( i = 0; i < SJG.autoExt.nExt; ++i ){ + /* Look for a match. */ + ax = &SJG.autoExt.aExt[i]; + if( ax->jObj && (*env)->IsSameObject(env, ax->jObj, jAutoExt) ){ + /* same object, so this is a no-op. */ + S3JniAutoExt_mutex_leave; + return 0; + } + } + if( i == SJG.autoExt.nExt ){ + assert( SJG.autoExt.nExt <= SJG.autoExt.nAlloc ); + if( SJG.autoExt.nExt == SJG.autoExt.nAlloc ){ + /* Allocate another slot. */ + unsigned n = 1 + SJG.autoExt.nAlloc; + S3JniAutoExtension * const aNew = + s3jni_realloc( SJG.autoExt.aExt, n * sizeof(*ax) ); + if( !aNew ){ + rc = SQLITE_NOMEM; + }else{ + SJG.autoExt.aExt = aNew; + ++SJG.autoExt.nAlloc; + } + } + if( 0==rc ){ + ax = &SJG.autoExt.aExt[SJG.autoExt.nExt]; + rc = S3JniAutoExtension_init(env, ax, jAutoExt); + assert( rc ? (0==ax->jObj && 0==ax->midCallback) + : (0!=ax->jObj && 0!=ax->midCallback) ); + } + } + if( 0==rc ){ + static int once = 0; + if( 0==once && ++once ){ + rc = sqlite3_auto_extension( + (void(*)(void))s3jni_run_java_auto_extensions + /* Reminder: the JNI binding of sqlite3_reset_auto_extension() + ** does not call the core-lib impl. It only clears Java-side + ** auto-extensions. */ + ); + if( rc ){ + assert( ax ); + S3JniAutoExtension_clear(ax); + } + } + if( 0==rc ){ + ++SJG.autoExt.nExt; + } + } + S3JniAutoExt_mutex_leave; + return rc; +} + +S3JniApi(sqlite3_backup_finish(),jint,1backup_1finish)( + JniArgsEnvClass, jlong jpBack +){ + int rc = 0; + if( jpBack!=0 ){ + rc = sqlite3_backup_finish( LongPtrGet_sqlite3_backup(jpBack) ); + } + return rc; +} + +S3JniApi(sqlite3_backup_init(),jobject,1backup_1init)( + JniArgsEnvClass, jlong jpDbDest, jstring jTDest, + jlong jpDbSrc, jstring jTSrc +){ + sqlite3 * const pDest = LongPtrGet_sqlite3(jpDbDest); + sqlite3 * const pSrc = LongPtrGet_sqlite3(jpDbSrc); + char * const zDest = s3jni_jstring_to_utf8(jTDest, 0); + char * const zSrc = s3jni_jstring_to_utf8(jTSrc, 0); + jobject rv = 0; + + if( pDest && pSrc && zDest && zSrc ){ + sqlite3_backup * const pB = + sqlite3_backup_init(pDest, zDest, pSrc, zSrc); + if( pB ){ + rv = new_java_sqlite3_backup(env, pB); + if( !rv ){ + sqlite3_backup_finish( pB ); + } + } + } + sqlite3_free(zDest); + sqlite3_free(zSrc); + return rv; +} + +S3JniApi(sqlite3_backup_pagecount(),jint,1backup_1pagecount)( + JniArgsEnvClass, jlong jpBack +){ + return sqlite3_backup_pagecount(LongPtrGet_sqlite3_backup(jpBack)); +} + +S3JniApi(sqlite3_backup_remaining(),jint,1backup_1remaining)( + JniArgsEnvClass, jlong jpBack +){ + return sqlite3_backup_remaining(LongPtrGet_sqlite3_backup(jpBack)); +} + +S3JniApi(sqlite3_backup_step(),jint,1backup_1step)( + JniArgsEnvClass, jlong jpBack, jint nPage +){ + return sqlite3_backup_step(LongPtrGet_sqlite3_backup(jpBack), (int)nPage); +} + +S3JniApi(sqlite3_bind_blob(),jint,1bind_1blob)( + JniArgsEnvClass, jlong jpStmt, jint ndx, jbyteArray baData, jint nMax +){ + jsize nBA = 0; + jbyte * const pBuf = baData ? s3jni_jbyteArray_bytes2(baData, &nBA) : 0; + int rc; + if( pBuf ){ + if( nMax>nBA ){ + nMax = nBA; + } + rc = sqlite3_bind_blob(LongPtrGet_sqlite3_stmt(jpStmt), (int)ndx, + pBuf, (int)nMax, SQLITE_TRANSIENT); + s3jni_jbyteArray_release(baData, pBuf); + }else{ + rc = baData + ? SQLITE_NOMEM + : sqlite3_bind_null( LongPtrGet_sqlite3_stmt(jpStmt), ndx ); + } + return (jint)rc; +} + +/** + Helper for use with s3jni_setup_nio_args(). +*/ +struct S3JniNioArgs { + jobject jBuf; /* input - ByteBuffer */ + jint iOffset; /* input - byte offset */ + jint iHowMany; /* input - byte count to bind/read/write */ + jint nBuf; /* output - jBuf's buffer size */ + void * p; /* output - jBuf's buffer memory */ + void * pStart; /* output - offset of p to bind/read/write */ + int nOut; /* output - number of bytes from pStart to bind/read/write */ +}; +typedef struct S3JniNioArgs S3JniNioArgs; +static const S3JniNioArgs S3JniNioArgs_empty = { + 0,0,0,0,0,0,0 +}; + +/* +** Internal helper for sqlite3_bind_nio_buffer(), +** sqlite3_result_nio_buffer(), and similar methods which take a +** ByteBuffer object as either input or output. Populates pArgs and +** returns 0 on success, non-0 if the operation should fail. The +** caller is required to check for SJG.g.byteBuffer.klazz!=0 before calling +** this and reporting it in a way appropriate for that routine. This +** function may assert() that SJG.g.byteBuffer.klazz is not 0. +** +** The (jBuffer, iOffset, iHowMany) arguments are the (ByteBuffer, offset, +** length) arguments to the bind/result method. +** +** If iHowMany is negative then it's treated as "until the end" and +** the calculated slice is trimmed to fit if needed. If iHowMany is +** positive and extends past the end of jBuffer then SQLITE_ERROR is +** returned. +** +** Returns 0 if everything looks to be in order, else some SQLITE_... +** result code +*/ +static int s3jni_setup_nio_args( + JNIEnv *env, S3JniNioArgs * pArgs, + jobject jBuffer, jint iOffset, jint iHowMany +){ + jlong iEnd = 0; + const int bAllowTruncate = iHowMany<0; + *pArgs = S3JniNioArgs_empty; + pArgs->jBuf = jBuffer; + pArgs->iOffset = iOffset; + pArgs->iHowMany = iHowMany; + assert( SJG.g.byteBuffer.klazz ); + if( pArgs->iOffset<0 ){ + return SQLITE_ERROR + /* SQLITE_MISUSE or SQLITE_RANGE would fit better but we use + SQLITE_ERROR for consistency with the code documented for a + negative target blob offset in sqlite3_blob_read/write(). */; + } + s3jni_get_nio_buffer(pArgs->jBuf, &pArgs->p, &pArgs->nBuf); + if( !pArgs->p ){ + return SQLITE_MISUSE; + }else if( pArgs->iOffset>=pArgs->nBuf ){ + pArgs->pStart = 0; + pArgs->nOut = 0; + return 0; + } + assert( pArgs->nBuf > 0 ); + assert( pArgs->iOffset < pArgs->nBuf ); + iEnd = pArgs->iHowMany<0 + ? pArgs->nBuf - pArgs->iOffset + : pArgs->iOffset + pArgs->iHowMany; + if( iEnd>(jlong)pArgs->nBuf ){ + if( bAllowTruncate ){ + iEnd = pArgs->nBuf - pArgs->iOffset; + }else{ + return SQLITE_ERROR + /* again: for consistency with blob_read/write(), though + SQLITE_MISUSE or SQLITE_RANGE would be a better fit. */; + } + } + if( iEnd - pArgs->iOffset > (jlong)SQLITE_MAX_LENGTH ){ + return SQLITE_TOOBIG; + } + assert( pArgs->iOffset >= 0 ); + assert( iEnd > pArgs->iOffset ); + pArgs->pStart = pArgs->p + pArgs->iOffset; + pArgs->nOut = (int)(iEnd - pArgs->iOffset); + assert( pArgs->nOut > 0 ); + assert( (pArgs->pStart + pArgs->nOut) <= (pArgs->p + pArgs->nBuf) ); + return 0; +} + +S3JniApi(sqlite3_bind_nio_buffer(),jint,1bind_1nio_1buffer)( + JniArgsEnvClass, jobject jpStmt, jint ndx, jobject jBuffer, + jint iOffset, jint iN +){ + sqlite3_stmt * pStmt = PtrGet_sqlite3_stmt(jpStmt); + S3JniNioArgs args; + int rc; + if( !pStmt || !SJG.g.byteBuffer.klazz ) return SQLITE_MISUSE; + rc = s3jni_setup_nio_args(env, &args, jBuffer, iOffset, iN); + if(rc){ + return rc; + }else if( !args.pStart || !args.nOut ){ + return sqlite3_bind_null(pStmt, ndx); + } + return sqlite3_bind_blob( pStmt, (int)ndx, args.pStart, + args.nOut, SQLITE_TRANSIENT ); +} + +S3JniApi(sqlite3_bind_double(),jint,1bind_1double)( + JniArgsEnvClass, jlong jpStmt, jint ndx, jdouble val +){ + return (jint)sqlite3_bind_double(LongPtrGet_sqlite3_stmt(jpStmt), + (int)ndx, (double)val); +} + +S3JniApi(sqlite3_bind_int(),jint,1bind_1int)( + JniArgsEnvClass, jlong jpStmt, jint ndx, jint val +){ + return (jint)sqlite3_bind_int(LongPtrGet_sqlite3_stmt(jpStmt), (int)ndx, (int)val); +} + +S3JniApi(sqlite3_bind_int64(),jint,1bind_1int64)( + JniArgsEnvClass, jlong jpStmt, jint ndx, jlong val +){ + return (jint)sqlite3_bind_int64(LongPtrGet_sqlite3_stmt(jpStmt), (int)ndx, (sqlite3_int64)val); +} + +/* +** Bind a new global ref to Object `val` using sqlite3_bind_pointer(). +*/ +S3JniApi(sqlite3_bind_java_object(),jint,1bind_1java_1object)( + JniArgsEnvClass, jlong jpStmt, jint ndx, jobject val +){ + sqlite3_stmt * const pStmt = LongPtrGet_sqlite3_stmt(jpStmt); + int rc = SQLITE_MISUSE; + + if(pStmt){ + jobject const rv = S3JniRefGlobal(val); + if( rv ){ + rc = sqlite3_bind_pointer(pStmt, ndx, rv, s3jni__value_jref_key, + S3Jni_jobject_finalizer); + }else if(val){ + rc = SQLITE_NOMEM; + }else{ + rc = sqlite3_bind_null(pStmt, ndx); + } + } + return rc; +} + +S3JniApi(sqlite3_bind_null(),jint,1bind_1null)( + JniArgsEnvClass, jlong jpStmt, jint ndx +){ + return (jint)sqlite3_bind_null(LongPtrGet_sqlite3_stmt(jpStmt), (int)ndx); +} + +S3JniApi(sqlite3_bind_parameter_count(),jint,1bind_1parameter_1count)( + JniArgsEnvClass, jlong jpStmt +){ + return (jint)sqlite3_bind_parameter_count(LongPtrGet_sqlite3_stmt(jpStmt)); +} + +S3JniApi(sqlite3_bind_parameter_index(),jint,1bind_1parameter_1index)( + JniArgsEnvClass, jlong jpStmt, jbyteArray jName +){ + int rc = 0; + jbyte * const pBuf = s3jni_jbyteArray_bytes(jName); + if( pBuf ){ + rc = sqlite3_bind_parameter_index(LongPtrGet_sqlite3_stmt(jpStmt), + (const char *)pBuf); + s3jni_jbyteArray_release(jName, pBuf); + } + return rc; +} + +S3JniApi(sqlite3_bind_parameter_name(),jstring,1bind_1parameter_1name)( + JniArgsEnvClass, jlong jpStmt, jint ndx +){ + const char *z = + sqlite3_bind_parameter_name(LongPtrGet_sqlite3_stmt(jpStmt), (int)ndx); + return z ? s3jni_utf8_to_jstring(z, -1) : 0; +} + +/* +** Impl of sqlite3_bind_text/text16(). +*/ +static int s3jni__bind_text(int is16, JNIEnv *env, jlong jpStmt, jint ndx, + jbyteArray baData, jint nMax){ + jsize nBA = 0; + jbyte * const pBuf = + baData ? s3jni_jbyteArray_bytes2(baData, &nBA) : 0; + int rc; + if( pBuf ){ + if( nMax>nBA ){ + nMax = nBA; + } + /* Note that we rely on the Java layer having assured that baData + is NUL-terminated if nMax is negative. In order to avoid UB for + such cases, we do not expose the byte-limit arguments in the + public API. */ + rc = is16 + ? sqlite3_bind_text16(LongPtrGet_sqlite3_stmt(jpStmt), (int)ndx, + pBuf, (int)nMax, SQLITE_TRANSIENT) + : sqlite3_bind_text(LongPtrGet_sqlite3_stmt(jpStmt), (int)ndx, + (const char *)pBuf, + (int)nMax, SQLITE_TRANSIENT); + }else{ + rc = baData + ? sqlite3_bind_null(LongPtrGet_sqlite3_stmt(jpStmt), (int)ndx) + : SQLITE_NOMEM; + } + s3jni_jbyteArray_release(baData, pBuf); + return (jint)rc; + +} + +S3JniApi(sqlite3_bind_text(),jint,1bind_1text)( + JniArgsEnvClass, jlong jpStmt, jint ndx, jbyteArray baData, jint nMax +){ + return s3jni__bind_text(0, env, jpStmt, ndx, baData, nMax); +} + +S3JniApi(sqlite3_bind_text16(),jint,1bind_1text16)( + JniArgsEnvClass, jlong jpStmt, jint ndx, jbyteArray baData, jint nMax +){ + return s3jni__bind_text(1, env, jpStmt, ndx, baData, nMax); +} + +S3JniApi(sqlite3_bind_value(),jint,1bind_1value)( + JniArgsEnvClass, jlong jpStmt, jint ndx, jlong jpValue +){ + int rc = 0; + sqlite3_stmt * pStmt = LongPtrGet_sqlite3_stmt(jpStmt); + if( pStmt ){ + sqlite3_value *v = LongPtrGet_sqlite3_value(jpValue); + if( v ){ + rc = sqlite3_bind_value(pStmt, (int)ndx, v); + }else{ + rc = sqlite3_bind_null(pStmt, (int)ndx); + } + }else{ + rc = SQLITE_MISUSE; + } + return (jint)rc; +} + +S3JniApi(sqlite3_bind_zeroblob(),jint,1bind_1zeroblob)( + JniArgsEnvClass, jlong jpStmt, jint ndx, jint n +){ + return (jint)sqlite3_bind_zeroblob(LongPtrGet_sqlite3_stmt(jpStmt), + (int)ndx, (int)n); +} + +S3JniApi(sqlite3_bind_zeroblob64(),jint,1bind_1zeroblob64)( + JniArgsEnvClass, jlong jpStmt, jint ndx, jlong n +){ + return (jint)sqlite3_bind_zeroblob64(LongPtrGet_sqlite3_stmt(jpStmt), + (int)ndx, (sqlite3_uint64)n); +} + +S3JniApi(sqlite3_blob_bytes(),jint,1blob_1bytes)( + JniArgsEnvClass, jlong jpBlob +){ + return sqlite3_blob_bytes(LongPtrGet_sqlite3_blob(jpBlob)); +} + +S3JniApi(sqlite3_blob_close(),jint,1blob_1close)( + JniArgsEnvClass, jlong jpBlob +){ + sqlite3_blob * const b = LongPtrGet_sqlite3_blob(jpBlob); + return b ? (jint)sqlite3_blob_close(b) : SQLITE_MISUSE; +} + +S3JniApi(sqlite3_blob_open(),jint,1blob_1open)( + JniArgsEnvClass, jlong jpDb, jstring jDbName, jstring jTbl, jstring jCol, + jlong jRowId, jint flags, jobject jOut +){ + sqlite3 * const db = LongPtrGet_sqlite3(jpDb); + sqlite3_blob * pBlob = 0; + char * zDbName = 0, * zTableName = 0, * zColumnName = 0; + int rc; + + if( !db || !jDbName || !jTbl || !jCol ) return SQLITE_MISUSE; + zDbName = s3jni_jstring_to_utf8(jDbName,0); + zTableName = zDbName ? s3jni_jstring_to_utf8(jTbl,0) : 0; + zColumnName = zTableName ? s3jni_jstring_to_utf8(jCol,0) : 0; + rc = zColumnName + ? sqlite3_blob_open(db, zDbName, zTableName, zColumnName, + (sqlite3_int64)jRowId, (int)flags, &pBlob) + : SQLITE_NOMEM; + if( 0==rc ){ + jobject rv = new_java_sqlite3_blob(env, pBlob); + if( !rv ){ + sqlite3_blob_close(pBlob); + rc = SQLITE_NOMEM; + } + OutputPointer_set_obj(env, S3JniNph(OutputPointer_sqlite3_blob), jOut, rv); + } + sqlite3_free(zDbName); + sqlite3_free(zTableName); + sqlite3_free(zColumnName); + return rc; +} + +S3JniApi(sqlite3_blob_read(),jint,1blob_1read)( + JniArgsEnvClass, jlong jpBlob, jbyteArray jTgt, jint iOffset +){ + jbyte * const pBa = s3jni_jbyteArray_bytes(jTgt); + int rc = jTgt ? (pBa ? SQLITE_MISUSE : SQLITE_NOMEM) : SQLITE_MISUSE; + if( pBa ){ + jsize const nTgt = (*env)->GetArrayLength(env, jTgt); + rc = sqlite3_blob_read(LongPtrGet_sqlite3_blob(jpBlob), pBa, + (int)nTgt, (int)iOffset); + if( 0==rc ){ + s3jni_jbyteArray_commit(jTgt, pBa); + }else{ + s3jni_jbyteArray_release(jTgt, pBa); + } + } + return rc; +} + +S3JniApi(sqlite3_blob_read_nio_buffer(),jint,1blob_1read_1nio_1buffer)( + JniArgsEnvClass, jlong jpBlob, jint iSrcOff, jobject jBB, jint iTgtOff, jint iHowMany +){ + sqlite3_blob * const b = LongPtrGet_sqlite3_blob(jpBlob); + S3JniNioArgs args; + int rc; + if( !b || !SJG.g.byteBuffer.klazz || iHowMany<0 ){ + return SQLITE_MISUSE; + }else if( iTgtOff<0 || iSrcOff<0 ){ + return SQLITE_ERROR + /* for consistency with underlying sqlite3_blob_read() */; + }else if( 0==iHowMany ){ + return 0; + } + rc = s3jni_setup_nio_args(env, &args, jBB, iTgtOff, iHowMany); + if(rc){ + return rc; + }else if( !args.pStart || !args.nOut ){ + return 0; + } + assert( args.iHowMany>0 ); + return sqlite3_blob_read( b, args.pStart, (int)args.nOut, (int)iSrcOff ); +} + +S3JniApi(sqlite3_blob_reopen(),jint,1blob_1reopen)( + JniArgsEnvClass, jlong jpBlob, jlong iNewRowId +){ + return (jint)sqlite3_blob_reopen(LongPtrGet_sqlite3_blob(jpBlob), + (sqlite3_int64)iNewRowId); +} + +S3JniApi(sqlite3_blob_write(),jint,1blob_1write)( + JniArgsEnvClass, jlong jpBlob, jbyteArray jBa, jint iOffset +){ + sqlite3_blob * const b = LongPtrGet_sqlite3_blob(jpBlob); + jbyte * const pBuf = b ? s3jni_jbyteArray_bytes(jBa) : 0; + const jsize nBA = pBuf ? (*env)->GetArrayLength(env, jBa) : 0; + int rc = SQLITE_MISUSE; + if(b && pBuf){ + rc = sqlite3_blob_write( b, pBuf, (int)nBA, (int)iOffset ); + } + s3jni_jbyteArray_release(jBa, pBuf); + return (jint)rc; +} + +S3JniApi(sqlite3_blob_write_nio_buffer(),jint,1blob_1write_1nio_1buffer)( + JniArgsEnvClass, jlong jpBlob, jint iTgtOff, jobject jBB, jint iSrcOff, jint iHowMany +){ + sqlite3_blob * const b = LongPtrGet_sqlite3_blob(jpBlob); + S3JniNioArgs args; + int rc; + if( !b || !SJG.g.byteBuffer.klazz ){ + return SQLITE_MISUSE; + }else if( iTgtOff<0 || iSrcOff<0 ){ + return SQLITE_ERROR + /* for consistency with underlying sqlite3_blob_write() */; + }else if( 0==iHowMany ){ + return 0; + } + rc = s3jni_setup_nio_args(env, &args, jBB, iSrcOff, iHowMany); + if(rc){ + return rc; + }else if( !args.pStart || !args.nOut ){ + return 0; + } + return sqlite3_blob_write( b, args.pStart, (int)args.nOut, (int)iTgtOff ); +} + +/* Central C-to-Java busy handler proxy. */ +static int s3jni_busy_handler(void* pState, int n){ + S3JniDb * const ps = (S3JniDb *)pState; + int rc = 0; + S3JniDeclLocal_env; + S3JniHook hook; + + S3JniHook_localdup(&ps->hooks.busyHandler, &hook); + if( hook.jObj ){ + rc = (*env)->CallIntMethod(env, hook.jObj, + hook.midCallback, (jint)n); + S3JniIfThrew{ + S3JniExceptionWarnCallbackThrew("sqlite3_busy_handler() callback"); + rc = s3jni_db_exception(ps->pDb, SQLITE_ERROR, + "sqlite3_busy_handler() callback threw."); + } + S3JniHook_localundup(hook); + } + return rc; +} + +S3JniApi(sqlite3_busy_handler(),jint,1busy_1handler)( + JniArgsEnvClass, jlong jpDb, jobject jBusy +){ + S3JniDb * const ps = S3JniDb_from_jlong(jpDb); + S3JniHook * const pHook = ps ? &ps->hooks.busyHandler : 0; + S3JniHook hook = S3JniHook_empty; + int rc = 0; + + if( !ps ) return (jint)SQLITE_MISUSE; + S3JniDb_mutex_enter; + if( jBusy ){ + if( pHook->jObj && (*env)->IsSameObject(env, pHook->jObj, jBusy) ){ + /* Same object - this is a no-op. */ + }else{ + jclass const klazz = (*env)->GetObjectClass(env, jBusy); + hook.jObj = S3JniRefGlobal(jBusy); + hook.midCallback = (*env)->GetMethodID(env, klazz, "call", "(I)I"); + S3JniUnrefLocal(klazz); + S3JniIfThrew { + rc = SQLITE_ERROR; + } + } + } + if( 0==rc ){ + if( jBusy ){ + if( hook.jObj ){ /* Replace handler */ + rc = sqlite3_busy_handler(ps->pDb, s3jni_busy_handler, ps); + if( 0==rc ){ + S3JniHook_unref(pHook); + *pHook = hook /* transfer Java ref ownership */; + hook = S3JniHook_empty; + } + }/* else no-op */ + }else{ /* Clear handler */ + rc = sqlite3_busy_handler(ps->pDb, 0, 0); + if( 0==rc ){ + S3JniHook_unref(pHook); + } + } + } + S3JniHook_unref(&hook); + S3JniDb_mutex_leave; + return rc; +} + +S3JniApi(sqlite3_busy_timeout(),jint,1busy_1timeout)( + JniArgsEnvClass, jlong jpDb, jint ms +){ + S3JniDb * const ps = S3JniDb_from_jlong(jpDb); + int rc = SQLITE_MISUSE; + if( ps ){ + S3JniDb_mutex_enter; + S3JniHook_unref(&ps->hooks.busyHandler); + rc = sqlite3_busy_timeout(ps->pDb, (int)ms); + S3JniDb_mutex_leave; + } + return rc; +} + +S3JniApi(sqlite3_cancel_auto_extension(),jboolean,1cancel_1auto_1extension)( + JniArgsEnvClass, jobject jAutoExt +){ + S3JniAutoExtension * ax; + jboolean rc = JNI_FALSE; + int i; + + if( !jAutoExt ){ + return rc; + } + S3JniAutoExt_mutex_enter; + /* This algo corresponds to the one in the core. */ + for( i = SJG.autoExt.nExt-1; i >= 0; --i ){ + ax = &SJG.autoExt.aExt[i]; + if( ax->jObj && (*env)->IsSameObject(env, ax->jObj, jAutoExt) ){ + S3JniAutoExtension_clear(ax); + /* Move final entry into this slot. */ + --SJG.autoExt.nExt; + *ax = SJG.autoExt.aExt[SJG.autoExt.nExt]; + SJG.autoExt.aExt[SJG.autoExt.nExt] = S3JniHook_empty; + assert( !SJG.autoExt.aExt[SJG.autoExt.nExt].jObj ); + rc = JNI_TRUE; + break; + } + } + S3JniAutoExt_mutex_leave; + return rc; +} + +/* Wrapper for sqlite3_close(_v2)(). */ +static jint s3jni_close_db(JNIEnv * const env, jlong jpDb, int version){ + int rc = 0; + S3JniDb * const ps = S3JniDb_from_jlong(jpDb); + + assert(version == 1 || version == 2); + if( ps ){ + rc = 1==version + ? (jint)sqlite3_close(ps->pDb) + : (jint)sqlite3_close_v2(ps->pDb); + } + return (jint)rc; +} + +S3JniApi(sqlite3_close(),jint,1close)(JniArgsEnvClass, jlong pDb){ + return s3jni_close_db(env, pDb, 1); +} + +S3JniApi(sqlite3_close_v2(),jint,1close_1v2)(JniArgsEnvClass, jlong pDb){ + return s3jni_close_db(env, pDb, 2); +} + +/* +** Assumes z is an array of unsigned short and returns the index in +** that array of the first element with the value 0. +*/ +static unsigned int s3jni_utf16_strlen(void const * z){ + unsigned int i = 0; + const unsigned short * p = z; + while( p[i] ) ++i; + return i; +} + +/* Descriptive alias for use with sqlite3_collation_needed(). */ +typedef S3JniHook S3JniCollationNeeded; + +/* Central C-to-Java sqlite3_collation_needed16() hook impl. */ +static void s3jni_collation_needed_impl16(void *pState, sqlite3 *pDb, + int eTextRep, const void * z16Name){ + S3JniCollationNeeded * const pHook = pState; + S3JniDeclLocal_env; + S3JniHook hook; + + S3JniHook_localdup(pHook, &hook); + if( hook.jObj ){ + unsigned int const nName = s3jni_utf16_strlen(z16Name); + jstring jName = (*env)->NewString(env, (jchar const *)z16Name, nName); + + s3jni_oom_check( jName ); + assert( hook.jExtra ); + S3JniIfThrew{ + S3JniExceptionClear; + }else if( hook.jExtra ){ + (*env)->CallVoidMethod(env, hook.jObj, hook.midCallback, + hook.jExtra, (jint)eTextRep, jName); + S3JniIfThrew{ + S3JniExceptionWarnCallbackThrew("sqlite3_collation_needed() callback"); + } + } + S3JniUnrefLocal(jName); + S3JniHook_localundup(hook); + } +} + +S3JniApi(sqlite3_collation_needed(),jint,1collation_1needed)( + JniArgsEnvClass, jlong jpDb, jobject jHook +){ + S3JniDb * ps; + S3JniCollationNeeded * pHook; + int rc = 0; + + S3JniDb_mutex_enter; + ps = S3JniDb_from_jlong(jpDb); + if( !ps ){ + S3JniDb_mutex_leave; + return SQLITE_MISUSE; + } + pHook = &ps->hooks.collationNeeded; + if( pHook->jObj && jHook && + (*env)->IsSameObject(env, pHook->jObj, jHook) ){ + /* no-op */ + }else if( !jHook ){ + rc = sqlite3_collation_needed(ps->pDb, 0, 0); + if( 0==rc ){ + S3JniHook_unref(pHook); + } + }else{ + jclass const klazz = (*env)->GetObjectClass(env, jHook); + jmethodID const xCallback = (*env)->GetMethodID( + env, klazz, "call", "(Lorg/sqlite/jni/capi/sqlite3;ILjava/lang/String;)V" + ); + S3JniUnrefLocal(klazz); + S3JniIfThrew { + rc = s3jni_db_exception(ps->pDb, SQLITE_MISUSE, + "Cannot not find matching call() in " + "CollationNeededCallback object."); + }else{ + rc = sqlite3_collation_needed16(ps->pDb, pHook, + s3jni_collation_needed_impl16); + if( 0==rc ){ + S3JniHook_unref(pHook); + pHook->midCallback = xCallback; + pHook->jObj = S3JniRefGlobal(jHook); + pHook->jExtra = S3JniRefGlobal(ps->jDb); + } + } + } + S3JniDb_mutex_leave; + return rc; +} + +S3JniApi(sqlite3_column_blob(),jbyteArray,1column_1blob)( + JniArgsEnvClass, jobject jpStmt, jint ndx +){ + sqlite3_stmt * const pStmt = PtrGet_sqlite3_stmt(jpStmt); + void const * const p = sqlite3_column_blob(pStmt, (int)ndx); + int const n = p ? sqlite3_column_bytes(pStmt, (int)ndx) : 0; + + return p ? s3jni_new_jbyteArray(p, n) : 0; +} + +S3JniApi(sqlite3_column_double(),jdouble,1column_1double)( + JniArgsEnvClass, jobject jpStmt, jint ndx +){ + return (jdouble)sqlite3_column_double(PtrGet_sqlite3_stmt(jpStmt), (int)ndx); +} + +S3JniApi(sqlite3_column_int(),jint,1column_1int)( + JniArgsEnvClass, jobject jpStmt, jint ndx +){ + return (jint)sqlite3_column_int(PtrGet_sqlite3_stmt(jpStmt), (int)ndx); +} + +S3JniApi(sqlite3_column_int64(),jlong,1column_1int64)( + JniArgsEnvClass, jobject jpStmt, jint ndx +){ + return (jlong)sqlite3_column_int64(PtrGet_sqlite3_stmt(jpStmt), (int)ndx); +} + +S3JniApi(sqlite3_column_java_object(),jobject,1column_1java_1object)( + JniArgsEnvClass, jlong jpStmt, jint ndx +){ + sqlite3_stmt * const stmt = LongPtrGet_sqlite3_stmt(jpStmt); + jobject rv = 0; + if( stmt ){ + sqlite3 * const db = sqlite3_db_handle(stmt); + sqlite3_value * sv; + sqlite3_mutex_enter(sqlite3_db_mutex(db)); + sv = sqlite3_column_value(stmt, (int)ndx); + if( sv ){ + rv = S3JniRefLocal( + sqlite3_value_pointer(sv, s3jni__value_jref_key) + ); + } + sqlite3_mutex_leave(sqlite3_db_mutex(db)); + } + return rv; +} + +S3JniApi(sqlite3_column_nio_buffer(),jobject,1column_1nio_1buffer)( + JniArgsEnvClass, jobject jStmt, jint ndx +){ + sqlite3_stmt * const stmt = PtrGet_sqlite3_stmt(jStmt); + jobject rv = 0; + if( stmt ){ + const void * const p = sqlite3_column_blob(stmt, (int)ndx); + if( p ){ + const int n = sqlite3_column_bytes(stmt, (int)ndx); + rv = s3jni__blob_to_ByteBuffer(env, p, n); + } + } + return rv; +} + +S3JniApi(sqlite3_column_text(),jbyteArray,1column_1text)( + JniArgsEnvClass, jobject jpStmt, jint ndx +){ + sqlite3_stmt * const stmt = PtrGet_sqlite3_stmt(jpStmt); + const unsigned char * const p = stmt ? sqlite3_column_text(stmt, (int)ndx) : 0; + const int n = p ? sqlite3_column_bytes(stmt, (int)ndx) : 0; + return p ? s3jni_new_jbyteArray(p, n) : NULL; +} + +#if 0 +// this impl might prove useful. +S3JniApi(sqlite3_column_text(),jstring,1column_1text)( + JniArgsEnvClass, jobject jpStmt, jint ndx +){ + sqlite3_stmt * const stmt = PtrGet_sqlite3_stmt(jpStmt); + const unsigned char * const p = stmt ? sqlite3_column_text(stmt, (int)ndx) : 0; + const int n = p ? sqlite3_column_bytes(stmt, (int)ndx) : 0; + return p ? s3jni_utf8_to_jstring( (const char *)p, n) : 0; +} +#endif + +S3JniApi(sqlite3_column_text16(),jstring,1column_1text16)( + JniArgsEnvClass, jobject jpStmt, jint ndx +){ + sqlite3_stmt * const stmt = PtrGet_sqlite3_stmt(jpStmt); + const void * const p = stmt ? sqlite3_column_text16(stmt, (int)ndx) : 0; + const int n = p ? sqlite3_column_bytes16(stmt, (int)ndx) : 0; + return s3jni_text16_to_jstring(env, p, n); +} + +S3JniApi(sqlite3_column_value(),jobject,1column_1value)( + JniArgsEnvClass, jobject jpStmt, jint ndx +){ + sqlite3_value * const sv = + sqlite3_column_value(PtrGet_sqlite3_stmt(jpStmt), (int)ndx) + /* reminder: returns an SQL NULL if jpStmt==NULL */; + return new_java_sqlite3_value(env, sv); +} + +/* +** Impl for commit hooks (if isCommit is true) or rollback hooks. +*/ +static int s3jni_commit_rollback_hook_impl(int isCommit, S3JniDb * const ps){ + S3JniDeclLocal_env; + int rc = 0; + S3JniHook hook; + + S3JniHook_localdup(isCommit + ? &ps->hooks.commit : &ps->hooks.rollback, + &hook); + if( hook.jObj ){ + rc = isCommit + ? (int)(*env)->CallIntMethod(env, hook.jObj, hook.midCallback) + : (int)((*env)->CallVoidMethod(env, hook.jObj, hook.midCallback), 0); + S3JniIfThrew{ + rc = s3jni_db_exception(ps->pDb, SQLITE_ERROR, + isCommit + ? "Commit hook callback threw" + : "Rollback hook callback threw"); + } + S3JniHook_localundup(hook); + } + return rc; +} + +/* C-to-Java commit hook wrapper. */ +static int s3jni_commit_hook_impl(void *pP){ + return s3jni_commit_rollback_hook_impl(1, pP); +} + +/* C-to-Java rollback hook wrapper. */ +static void s3jni_rollback_hook_impl(void *pP){ + (void)s3jni_commit_rollback_hook_impl(0, pP); +} + +/* +** Proxy for sqlite3_commit_hook() (if isCommit is true) or +** sqlite3_rollback_hook(). +*/ +static jobject s3jni_commit_rollback_hook(int isCommit, JNIEnv * const env, + jlong jpDb, jobject jHook){ + S3JniDb * ps; + jobject pOld = 0; /* previous hook */ + S3JniHook * pHook; /* ps->hooks.commit|rollback */ + + S3JniDb_mutex_enter; + ps = S3JniDb_from_jlong(jpDb); + if( !ps ){ + s3jni_db_error(ps->pDb, SQLITE_MISUSE, 0); + S3JniDb_mutex_leave; + return 0; + } + pHook = isCommit ? &ps->hooks.commit : &ps->hooks.rollback; + pOld = pHook->jObj; + if( pOld && jHook && + (*env)->IsSameObject(env, pOld, jHook) ){ + /* No-op. */ + }else if( !jHook ){ + if( pOld ){ + jobject tmp = S3JniRefLocal(pOld); + S3JniUnrefGlobal(pOld); + pOld = tmp; + } + *pHook = S3JniHook_empty; + if( isCommit ) sqlite3_commit_hook(ps->pDb, 0, 0); + else sqlite3_rollback_hook(ps->pDb, 0, 0); + }else{ + jclass const klazz = (*env)->GetObjectClass(env, jHook); + jmethodID const xCallback = (*env)->GetMethodID(env, klazz, "call", + isCommit ? "()I" : "()V"); + S3JniUnrefLocal(klazz); + S3JniIfThrew { + S3JniExceptionReport; + S3JniExceptionClear; + s3jni_db_error(ps->pDb, SQLITE_ERROR, + "Cannot not find matching call() method in" + "hook object."); + }else{ + pHook->midCallback = xCallback; + pHook->jObj = S3JniRefGlobal(jHook); + if( isCommit ) sqlite3_commit_hook(ps->pDb, s3jni_commit_hook_impl, ps); + else sqlite3_rollback_hook(ps->pDb, s3jni_rollback_hook_impl, ps); + if( pOld ){ + jobject tmp = S3JniRefLocal(pOld); + S3JniUnrefGlobal(pOld); + pOld = tmp; + } + } + } + S3JniDb_mutex_leave; + return pOld; +} + +S3JniApi(sqlite3_commit_hook(),jobject,1commit_1hook)( + JniArgsEnvClass, jlong jpDb, jobject jHook +){ + return s3jni_commit_rollback_hook(1, env, jpDb, jHook); +} + +S3JniApi(sqlite3_compileoption_get(),jstring,1compileoption_1get)( + JniArgsEnvClass, jint n +){ + const char * z = sqlite3_compileoption_get(n); + jstring const rv = z ? (*env)->NewStringUTF( env, z ) : 0; + /* We know these to be ASCII, so MUTF-8 is fine. */; + s3jni_oom_check(z ? !!rv : 1); + return rv; +} + +S3JniApi(sqlite3_compileoption_used(),jboolean,1compileoption_1used)( + JniArgsEnvClass, jstring name +){ + const char *zUtf8 = s3jni_jstring_to_mutf8(name) + /* We know these to be ASCII, so MUTF-8 is fine (and + hypothetically faster to convert). */; + const jboolean rc = + 0==sqlite3_compileoption_used(zUtf8) ? JNI_FALSE : JNI_TRUE; + s3jni_mutf8_release(name, zUtf8); + return rc; +} + +S3JniApi(sqlite3_complete(),jint,1complete)( + JniArgsEnvClass, jbyteArray jSql +){ + jbyte * const pBuf = s3jni_jbyteArray_bytes(jSql); + const jsize nBA = pBuf ? (*env)->GetArrayLength(env, jSql) : 0; + int rc; + + assert( (nBA>0 ? 0==pBuf[nBA-1] : (pBuf ? 0==*pBuf : 1)) + && "Byte array is not NUL-terminated." ); + rc = (pBuf && 0==pBuf[(nBA ? nBA-1 : 0)]) + ? sqlite3_complete( (const char *)pBuf ) + : (jSql ? SQLITE_NOMEM : SQLITE_MISUSE); + s3jni_jbyteArray_release(jSql, pBuf); + return rc; +} + +S3JniApi(sqlite3_config() /*for a small subset of options.*/ + sqlite3_config__enable()/* internal name to avoid name-mangling issues*/, + jint,1config_1_1enable)(JniArgsEnvClass, jint n){ + switch( n ){ + case SQLITE_CONFIG_SINGLETHREAD: + case SQLITE_CONFIG_MULTITHREAD: + case SQLITE_CONFIG_SERIALIZED: + return sqlite3_config( n ); + default: + return SQLITE_MISUSE; + } +} +/* C-to-Java SQLITE_CONFIG_LOG wrapper. */ +static void s3jni_config_log(void *ignored, int errCode, const char *z){ + S3JniDeclLocal_env; + S3JniHook hook = S3JniHook_empty; + + S3JniHook_localdup(&SJG.hook.configlog, &hook); + if( hook.jObj ){ + jstring const jArg1 = z ? s3jni_utf8_to_jstring(z, -1) : 0; + if( z ? !!jArg1 : 1 ){ + (*env)->CallVoidMethod(env, hook.jObj, hook.midCallback, errCode, jArg1); + } + S3JniIfThrew{ + S3JniExceptionWarnCallbackThrew("SQLITE_CONFIG_LOG callback"); + S3JniExceptionClear; + } + S3JniHook_localundup(hook); + S3JniUnrefLocal(jArg1); + } +} + +S3JniApi(sqlite3_config() /* for SQLITE_CONFIG_LOG */ + sqlite3_config__config_log() /* internal name */, + jint, 1config_1_1CONFIG_1LOG +)(JniArgsEnvClass, jobject jLog){ + S3JniHook * const pHook = &SJG.hook.configlog; + int rc = 0; + + S3JniGlobal_mutex_enter; + if( !jLog ){ + rc = sqlite3_config( SQLITE_CONFIG_LOG, NULL, NULL ); + if( 0==rc ){ + S3JniHook_unref(pHook); + } + }else if( pHook->jObj && (*env)->IsSameObject(env, jLog, pHook->jObj) ){ + /* No-op */ + }else { + jclass const klazz = (*env)->GetObjectClass(env, jLog); + jmethodID const midCallback = (*env)->GetMethodID(env, klazz, "call", + "(ILjava/lang/String;)V"); + S3JniUnrefLocal(klazz); + if( midCallback ){ + rc = sqlite3_config( SQLITE_CONFIG_LOG, s3jni_config_log, NULL ); + if( 0==rc ){ + S3JniHook_unref(pHook); + pHook->midCallback = midCallback; + pHook->jObj = S3JniRefGlobal(jLog); + } + }else{ + S3JniExceptionWarnIgnore; + rc = SQLITE_ERROR; + } + } + S3JniGlobal_mutex_leave; + return rc; +} + +#ifdef SQLITE_ENABLE_SQLLOG +/* C-to-Java SQLITE_CONFIG_SQLLOG wrapper. */ +static void s3jni_config_sqllog(void *ignored, sqlite3 *pDb, const char *z, int op){ + jobject jArg0 = 0; + jstring jArg1 = 0; + S3JniDeclLocal_env; + S3JniDb * const ps = S3JniDb_from_c(pDb); + S3JniHook hook = S3JniHook_empty; + + if( ps ){ + S3JniHook_localdup(&SJG.hook.sqllog, &hook); + } + if( !hook.jObj ) return; + jArg0 = S3JniRefLocal(ps->jDb); + switch( op ){ + case 0: /* db opened */ + case 1: /* SQL executed */ + jArg1 = s3jni_utf8_to_jstring( z, -1); + break; + case 2: /* db closed */ + break; + default: + (*env)->FatalError(env, "Unhandled 4th arg to SQLITE_CONFIG_SQLLOG."); + break; + } + (*env)->CallVoidMethod(env, hook.jObj, hook.midCallback, jArg0, jArg1, op); + S3JniIfThrew{ + S3JniExceptionWarnCallbackThrew("SQLITE_CONFIG_SQLLOG callback"); + S3JniExceptionClear; + } + S3JniHook_localundup(hook); + S3JniUnrefLocal(jArg0); + S3JniUnrefLocal(jArg1); +} +//! Requirement of SQLITE_CONFIG_SQLLOG. +void sqlite3_init_sqllog(void){ + sqlite3_config( SQLITE_CONFIG_SQLLOG, s3jni_config_sqllog, 0 ); +} +#endif + +S3JniApi(sqlite3_config() /* for SQLITE_CONFIG_SQLLOG */ + sqlite3_config__SQLLOG() /*internal name*/, + jint, 1config_1_1SQLLOG +)(JniArgsEnvClass, jobject jLog){ +#ifndef SQLITE_ENABLE_SQLLOG + return SQLITE_MISUSE; +#else + S3JniHook * const pHook = &SJG.hook.sqllog; + int rc = 0; + + S3JniGlobal_mutex_enter; + if( !jLog ){ + rc = sqlite3_config( SQLITE_CONFIG_SQLLOG, NULL ); + if( 0==rc ){ + S3JniHook_unref(pHook); + } + }else if( pHook->jObj && (*env)->IsSameObject(env, jLog, pHook->jObj) ){ + /* No-op */ + }else { + jclass const klazz = (*env)->GetObjectClass(env, jLog); + jmethodID const midCallback = (*env)->GetMethodID(env, klazz, "call", + "(Lorg/sqlite/jni/capi/sqlite3;" + "Ljava/lang/String;" + "I)V"); + S3JniUnrefLocal(klazz); + if( midCallback ){ + rc = sqlite3_config( SQLITE_CONFIG_SQLLOG, s3jni_config_sqllog, NULL ); + if( 0==rc ){ + S3JniHook_unref(pHook); + pHook->midCallback = midCallback; + pHook->jObj = S3JniRefGlobal(jLog); + } + }else{ + S3JniExceptionWarnIgnore; + rc = SQLITE_ERROR; + } + } + S3JniGlobal_mutex_leave; + return rc; +#endif +} + +S3JniApi(sqlite3_context_db_handle(),jobject,1context_1db_1handle)( + JniArgsEnvClass, jobject jpCx +){ + sqlite3_context * const pCx = PtrGet_sqlite3_context(jpCx); + sqlite3 * const pDb = pCx ? sqlite3_context_db_handle(pCx) : 0; + S3JniDb * const ps = pDb ? S3JniDb_from_c(pDb) : 0; + return ps ? ps->jDb : 0; +} + +/* +** State for CollationCallbacks. This used to be its own separate +** type, but has since been consolidated with S3JniHook. It retains +** its own typedef for code legibility and searchability reasons. +*/ +typedef S3JniHook S3JniCollationCallback; + +/* +** Proxy for Java-side CollationCallback.xCompare() callbacks. +*/ +static int CollationCallback_xCompare(void *pArg, int nLhs, const void *lhs, + int nRhs, const void *rhs){ + S3JniCollationCallback * const pCC = pArg; + S3JniDeclLocal_env; + jint rc = 0; + if( pCC->jObj ){ + jbyteArray jbaLhs = s3jni_new_jbyteArray(lhs, (jint)nLhs); + jbyteArray jbaRhs = jbaLhs + ? s3jni_new_jbyteArray(rhs, (jint)nRhs) : 0; + if( !jbaRhs ){ + S3JniUnrefLocal(jbaLhs); + /* We have no recovery strategy here. */ + s3jni_oom_check( jbaRhs ); + return 0; + } + rc = (*env)->CallIntMethod(env, pCC->jObj, pCC->midCallback, + jbaLhs, jbaRhs); + S3JniExceptionIgnore; + S3JniUnrefLocal(jbaLhs); + S3JniUnrefLocal(jbaRhs); + } + return (int)rc; +} + +/* CollationCallback finalizer for use by the sqlite3 internals. */ +static void CollationCallback_xDestroy(void *pArg){ + S3JniCollationCallback * const pCC = pArg; + S3JniDeclLocal_env; + S3JniHook_free(pCC); +} + +S3JniApi(sqlite3_create_collation() sqlite3_create_collation_v2(), + jint,1create_1collation +)(JniArgsEnvClass, jobject jDb, jstring name, jint eTextRep, + jobject oCollation){ + int rc; + S3JniDb * ps; + + if( !jDb || !name || !encodingTypeIsValid(eTextRep) ){ + return (jint)SQLITE_MISUSE; + } + S3JniDb_mutex_enter; + ps = S3JniDb_from_java(jDb); + jclass const klazz = (*env)->GetObjectClass(env, oCollation); + jmethodID const midCallback = + (*env)->GetMethodID(env, klazz, "call", "([B[B)I"); + S3JniUnrefLocal(klazz); + S3JniIfThrew{ + rc = s3jni_db_error(ps->pDb, SQLITE_ERROR, + "Could not get call() method from " + "CollationCallback object."); + }else{ + char * const zName = s3jni_jstring_to_utf8(name, 0); + S3JniCollationCallback * const pCC = + zName ? S3JniHook_alloc() : 0; + if( pCC ){ + rc = sqlite3_create_collation_v2(ps->pDb, zName, (int)eTextRep, + pCC, CollationCallback_xCompare, + CollationCallback_xDestroy); + if( 0==rc ){ + pCC->midCallback = midCallback; + pCC->jObj = S3JniRefGlobal(oCollation); + pCC->doXDestroy = 1; + }else{ + CollationCallback_xDestroy(pCC); + } + }else{ + rc = SQLITE_NOMEM; + } + sqlite3_free(zName); + } + S3JniDb_mutex_leave; + return (jint)rc; +} + +S3JniApi(sqlite3_create_function() sqlite3_create_function_v2() + sqlite3_create_window_function(), + jint,1create_1function +)(JniArgsEnvClass, jobject jDb, jstring jFuncName, jint nArg, + jint eTextRep, jobject jFunctor){ + S3JniUdf * s = 0; + int rc; + sqlite3 * const pDb = PtrGet_sqlite3(jDb); + char * zFuncName = 0; + + if( !pDb || !jFuncName ){ + return SQLITE_MISUSE; + }else if( !encodingTypeIsValid(eTextRep) ){ + return s3jni_db_error(pDb, SQLITE_FORMAT, + "Invalid function encoding option."); + } + s = S3JniUdf_alloc(env, jFunctor); + if( !s ) return SQLITE_NOMEM; + + if( UDF_UNKNOWN_TYPE==s->type ){ + rc = s3jni_db_error(pDb, SQLITE_MISUSE, + "Cannot unambiguously determine function type."); + S3JniUdf_free(env, s, 1); + goto error_cleanup; + } + zFuncName = s3jni_jstring_to_utf8(jFuncName,0); + if( !zFuncName ){ + rc = SQLITE_NOMEM; + S3JniUdf_free(env, s, 1); + goto error_cleanup; + } + s->zFuncName = zFuncName /* pass on ownership */; + if( UDF_WINDOW == s->type ){ + rc = sqlite3_create_window_function(pDb, zFuncName, nArg, eTextRep, s, + udf_xStep, udf_xFinal, udf_xValue, + udf_xInverse, S3JniUdf_finalizer); + }else{ + udf_xFunc_f xFunc = 0; + udf_xStep_f xStep = 0; + udf_xFinal_f xFinal = 0; + if( UDF_SCALAR == s->type ){ + xFunc = udf_xFunc; + }else{ + assert( UDF_AGGREGATE == s->type ); + xStep = udf_xStep; + xFinal = udf_xFinal; + } + rc = sqlite3_create_function_v2(pDb, zFuncName, nArg, eTextRep, s, + xFunc, xStep, xFinal, S3JniUdf_finalizer); + } +error_cleanup: + /* Reminder: on sqlite3_create_function() error, s will be + ** destroyed via create_function(). */ + return (jint)rc; +} + + +S3JniApi(sqlite3_db_config() /*for MAINDBNAME*/, + jint,1db_1config__Lorg_sqlite_jni_capi_sqlite3_2ILjava_lang_String_2 +)(JniArgsEnvClass, jobject jDb, jint op, jstring jStr){ + S3JniDb * const ps = S3JniDb_from_java(jDb); + int rc; + char *zStr; + + switch( (ps && jStr) ? op : 0 ){ + case SQLITE_DBCONFIG_MAINDBNAME: + S3JniDb_mutex_enter + /* Protect against a race in modifying/freeing + ps->zMainDbName. */; + zStr = s3jni_jstring_to_utf8( jStr, 0); + if( zStr ){ + rc = sqlite3_db_config(ps->pDb, (int)op, zStr); + if( rc ){ + sqlite3_free( zStr ); + }else{ + sqlite3_free( ps->zMainDbName ); + ps->zMainDbName = zStr; + } + }else{ + rc = SQLITE_NOMEM; + } + S3JniDb_mutex_leave; + break; + case 0: + default: + rc = SQLITE_MISUSE; + } + return rc; +} + +S3JniApi( + sqlite3_db_config(), + /* WARNING: openjdk v19 creates a different mangled name for this + ** function than openjdk v8 does. We account for that by exporting + ** both versions of the name. */ + jint,1db_1config__Lorg_sqlite_jni_capi_sqlite3_2IILorg_sqlite_jni_capi_OutputPointer_Int32_2 +)( + JniArgsEnvClass, jobject jDb, jint op, jint onOff, jobject jOut +){ + S3JniDb * const ps = S3JniDb_from_java(jDb); + int rc; + switch( ps ? op : 0 ){ + case SQLITE_DBCONFIG_ENABLE_FKEY: + case SQLITE_DBCONFIG_ENABLE_TRIGGER: + case SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER: + case SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION: + case SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE: + case SQLITE_DBCONFIG_ENABLE_QPSG: + case SQLITE_DBCONFIG_TRIGGER_EQP: + case SQLITE_DBCONFIG_RESET_DATABASE: + case SQLITE_DBCONFIG_DEFENSIVE: + case SQLITE_DBCONFIG_WRITABLE_SCHEMA: + case SQLITE_DBCONFIG_LEGACY_ALTER_TABLE: + case SQLITE_DBCONFIG_DQS_DML: + case SQLITE_DBCONFIG_DQS_DDL: + case SQLITE_DBCONFIG_ENABLE_VIEW: + case SQLITE_DBCONFIG_LEGACY_FILE_FORMAT: + case SQLITE_DBCONFIG_TRUSTED_SCHEMA: + case SQLITE_DBCONFIG_STMT_SCANSTATUS: + case SQLITE_DBCONFIG_REVERSE_SCANORDER: { + int pOut = 0; + rc = sqlite3_db_config( ps->pDb, (int)op, onOff, &pOut ); + if( 0==rc && jOut ){ + OutputPointer_set_Int32(env, jOut, pOut); + } + break; + } + default: + rc = SQLITE_MISUSE; + } + return (jint)rc; +} + +/* +** This is a workaround for openjdk v19 (and possibly others) encoding +** this function's name differently than JDK v8 does. If we do not +** install both names for this function then Java will not be able to +** find the function in both environments. +*/ +JniDecl(jint,1db_1config__Lorg_sqlite_jni_capi_sqlite3_2IILorg_sqlite_jni_capi_OutputPointer_00024Int32_2)( + JniArgsEnvClass, jobject jDb, jint op, jint onOff, jobject jOut +){ + return JniFuncName(1db_1config__Lorg_sqlite_jni_capi_sqlite3_2IILorg_sqlite_jni_capi_OutputPointer_Int32_2)( + env, jKlazz, jDb, op, onOff, jOut + ); +} + +S3JniApi(sqlite3_db_filename(),jstring,1db_1filename)( + JniArgsEnvClass, jobject jDb, jstring jDbName +){ + S3JniDb * const ps = S3JniDb_from_java(jDb); + char *zDbName; + jstring jRv = 0; + int nStr = 0; + + if( !ps || !jDbName ){ + return 0; + } + zDbName = s3jni_jstring_to_utf8( jDbName, &nStr); + if( zDbName ){ + char const * zRv = sqlite3_db_filename(ps->pDb, zDbName); + sqlite3_free(zDbName); + if( zRv ){ + jRv = s3jni_utf8_to_jstring( zRv, -1); + } + } + return jRv; +} + +S3JniApi(sqlite3_db_handle(),jobject,1db_1handle)( + JniArgsEnvClass, jobject jpStmt +){ + sqlite3_stmt * const pStmt = PtrGet_sqlite3_stmt(jpStmt); + sqlite3 * const pDb = pStmt ? sqlite3_db_handle(pStmt) : 0; + S3JniDb * const ps = pDb ? S3JniDb_from_c(pDb) : 0; + return ps ? ps->jDb : 0; +} + +S3JniApi(sqlite3_db_readonly(),jint,1db_1readonly)( + JniArgsEnvClass, jobject jDb, jstring jDbName +){ + int rc = 0; + S3JniDb * const ps = S3JniDb_from_java(jDb); + char *zDbName = jDbName ? s3jni_jstring_to_utf8( jDbName, 0 ) : 0; + rc = sqlite3_db_readonly(ps ? ps->pDb : 0, zDbName); + sqlite3_free(zDbName); + return (jint)rc; +} + +S3JniApi(sqlite3_db_release_memory(),jint,1db_1release_1memory)( + JniArgsEnvClass, jobject jDb +){ + sqlite3 * const pDb = PtrGet_sqlite3(jDb); + return pDb ? sqlite3_db_release_memory(pDb) : SQLITE_MISUSE; +} + +S3JniApi(sqlite3_db_status(),jint,1db_1status)( + JniArgsEnvClass, jobject jDb, jint op, jobject jOutCurrent, + jobject jOutHigh, jboolean reset +){ + int iCur = 0, iHigh = 0; + sqlite3 * const pDb = PtrGet_sqlite3(jDb); + int rc = sqlite3_db_status( pDb, op, &iCur, &iHigh, reset ); + if( 0==rc ){ + OutputPointer_set_Int32(env, jOutCurrent, iCur); + OutputPointer_set_Int32(env, jOutHigh, iHigh); + } + return (jint)rc; +} + +S3JniApi(sqlite3_errcode(),jint,1errcode)( + JniArgsEnvClass, jobject jpDb +){ + sqlite3 * const pDb = PtrGet_sqlite3(jpDb); + return pDb ? sqlite3_errcode(pDb) : SQLITE_MISUSE; +} + +S3JniApi(sqlite3_errmsg(),jstring,1errmsg)( + JniArgsEnvClass, jobject jpDb +){ + sqlite3 * const pDb = PtrGet_sqlite3(jpDb); + return pDb ? s3jni_utf8_to_jstring( sqlite3_errmsg(pDb), -1) : 0 + /* We don't use errmsg16() directly only because it would cause an + additional level of internal encoding in sqlite3. The end + effect should be identical to using errmsg16(), however. */; +} + +S3JniApi(sqlite3_set_errmsg(),jint,1set_1errmsg)( + JniArgsEnvClass, jobject jpDb, jint errCode, jstring msg +){ + sqlite3 * const pDb = PtrGet_sqlite3(jpDb); + const char *zUtf8; + jint rc; + if( !pDb ) return SQLITE_MISUSE; + zUtf8 = msg ? s3jni_jstring_to_mutf8(msg) : NULL; + rc = sqlite3_set_errmsg(pDb, (int)errCode, zUtf8); + s3jni_mutf8_release(msg, zUtf8); + return rc; +} + +S3JniApi(sqlite3_errstr(),jstring,1errstr)( + JniArgsEnvClass, jint rcCode +){ + jstring rv; + const char * z = sqlite3_errstr((int)rcCode); + if( !z ){ + /* This hypothetically cannot happen, but we'll behave like the + low-level library would in such a case... */ + z = "unknown error"; + } + rv = (*env)->NewStringUTF(env, z) + /* We know these values to be plain ASCII, so pose no MUTF-8 + ** incompatibility */; + s3jni_oom_check( rv ); + return rv; +} + +#ifndef SQLITE_ENABLE_NORMALIZE +/* Dummy stub for sqlite3_normalized_sql(). Never called. */ +static const char * sqlite3_normalized_sql(sqlite3_stmt *s){ + S3JniDeclLocal_env; + (*env)->FatalError(env, "dummy sqlite3_normalized_sql() was " + "impossibly called.") /* does not return */; + return 0; +} +#endif + +/* +** Impl for sqlite3_expanded_sql() (if isExpanded is true) and +** sqlite3_normalized_sql(). +*/ +static jstring s3jni_xn_sql(int isExpanded, JNIEnv *env, jobject jpStmt){ + jstring rv = 0; + sqlite3_stmt * const pStmt = PtrGet_sqlite3_stmt(jpStmt); + + if( pStmt ){ + char * zSql = isExpanded + ? sqlite3_expanded_sql(pStmt) + : (char*)sqlite3_normalized_sql(pStmt); + s3jni_oom_fatal(zSql); + if( zSql ){ + rv = s3jni_utf8_to_jstring(zSql, -1); + if( isExpanded ) sqlite3_free(zSql); + } + } + return rv; +} + +S3JniApi(sqlite3_expanded_sql(),jstring,1expanded_1sql)( + JniArgsEnvClass, jobject jpStmt +){ + return s3jni_xn_sql(1, env, jpStmt); +} + +S3JniApi(sqlite3_normalized_sql(),jstring,1normalized_1sql)( + JniArgsEnvClass, jobject jpStmt +){ +#ifdef SQLITE_ENABLE_NORMALIZE + return s3jni_xn_sql(0, env, jpStmt); +#else + return 0; +#endif +} + +S3JniApi(sqlite3_extended_result_codes(),jint,1extended_1result_1codes)( + JniArgsEnvClass, jobject jpDb, jboolean onoff +){ + sqlite3 * const pDb = PtrGet_sqlite3(jpDb); + int const rc = pDb + ? sqlite3_extended_result_codes(pDb, onoff ? 1 : 0) + : SQLITE_MISUSE; + return rc; +} + +S3JniApi(sqlite3_finalize(),jint,1finalize)( + JniArgsEnvClass, jlong jpStmt +){ + return jpStmt + ? sqlite3_finalize(LongPtrGet_sqlite3_stmt(jpStmt)) + : 0; +} + +S3JniApi(sqlite3_get_auxdata(),jobject,1get_1auxdata)( + JniArgsEnvClass, jobject jCx, jint n +){ + return sqlite3_get_auxdata(PtrGet_sqlite3_context(jCx), (int)n); +} + +S3JniApi(sqlite3_initialize(),jint,1initialize)( + JniArgsEnvClass +){ + return sqlite3_initialize(); +} + +S3JniApi(sqlite3_interrupt(),void,1interrupt)( + JniArgsEnvClass, jobject jpDb +){ + sqlite3 * const pDb = PtrGet_sqlite3(jpDb); + if( pDb ){ + sqlite3_interrupt(pDb); + } +} + +S3JniApi(sqlite3_is_interrupted(),jboolean,1is_1interrupted)( + JniArgsEnvClass, jobject jpDb +){ + int rc = 0; + sqlite3 * const pDb = PtrGet_sqlite3(jpDb); + if( pDb ){ + rc = sqlite3_is_interrupted(pDb); + } + return rc ? JNI_TRUE : JNI_FALSE; +} + +/* +** Uncaches the current JNIEnv from the S3JniGlobal state, clearing +** any resources owned by that cache entry and making that slot +** available for re-use. +*/ +S3JniApi(sqlite3_java_uncache_thread(), jboolean, 1java_1uncache_1thread)( + JniArgsEnvClass +){ + int rc; + S3JniEnv_mutex_enter; + rc = S3JniEnv_uncache(env); + S3JniEnv_mutex_leave; + return rc ? JNI_TRUE : JNI_FALSE; +} + +S3JniApi(sqlite3_jni_db_error(), jint, 1jni_1db_1error)( + JniArgsEnvClass, jobject jDb, jint jRc, jstring jStr +){ + S3JniDb * const ps = S3JniDb_from_java(jDb); + int rc = SQLITE_MISUSE; + if( ps ){ + char *zStr; + zStr = jStr + ? s3jni_jstring_to_utf8( jStr, 0) + : NULL; + rc = s3jni_db_error( ps->pDb, (int)jRc, zStr ); + sqlite3_free(zStr); + } + return rc; +} + +S3JniApi(sqlite3_jni_supports_nio(), jboolean,1jni_1supports_1nio)( + JniArgsEnvClass +){ + return SJG.g.byteBuffer.klazz ? JNI_TRUE : JNI_FALSE; +} + + +S3JniApi(sqlite3_keyword_check(),jboolean,1keyword_1check)( + JniArgsEnvClass, jstring jWord +){ + int nWord = 0; + char * zWord = s3jni_jstring_to_utf8(jWord, &nWord); + int rc = 0; + + s3jni_oom_check(jWord ? !!zWord : 1); + if( zWord && nWord ){ + rc = sqlite3_keyword_check(zWord, nWord); + } + sqlite3_free(zWord); + return rc ? JNI_TRUE : JNI_FALSE; +} + +S3JniApi(sqlite3_keyword_name(),jstring,1keyword_1name)( + JniArgsEnvClass, jint ndx +){ + const char * zWord = 0; + int n = 0; + jstring rv = 0; + + if( 0==sqlite3_keyword_name(ndx, &zWord, &n) ){ + rv = s3jni_utf8_to_jstring(zWord, n); + } + return rv; +} + + +S3JniApi(sqlite3_last_insert_rowid(),jlong,1last_1insert_1rowid)( + JniArgsEnvClass, jobject jpDb +){ + return (jlong)sqlite3_last_insert_rowid(PtrGet_sqlite3(jpDb)); +} + +S3JniApi(sqlite3_limit(),jint,1limit)( + JniArgsEnvClass, jobject jpDb, jint id, jint newVal +){ + jint rc = 0; + sqlite3 * const pDb = PtrGet_sqlite3(jpDb); + if( pDb ){ + rc = sqlite3_limit( pDb, (int)id, (int)newVal ); + } + return rc; +} + +/* Pre-open() code common to sqlite3_open[_v2](). */ +static int s3jni_open_pre(JNIEnv * const env, S3JniEnv **jc, + jstring jDbName, char **zDbName, + S3JniDb ** ps){ + int rc = 0; + jobject jDb = 0; + + *jc = S3JniEnv_get(); + if( !*jc ){ + rc = SQLITE_NOMEM; + goto end; + } + *zDbName = jDbName ? s3jni_jstring_to_utf8( jDbName, 0) : 0; + if( jDbName && !*zDbName ){ + rc = SQLITE_NOMEM; + goto end; + } + jDb = new_java_sqlite3(env, 0); + if( !jDb ){ + sqlite3_free(*zDbName); + *zDbName = 0; + rc = SQLITE_NOMEM; + goto end; + } + *ps = S3JniDb_alloc(env, jDb); + if( *ps ){ + (*jc)->pdbOpening = *ps; + }else{ + S3JniUnrefLocal(jDb); + rc = SQLITE_NOMEM; + } +end: + return rc; +} + +/* +** Post-open() code common to both the sqlite3_open() and +** sqlite3_open_v2() bindings. ps->jDb must be the +** org.sqlite.jni.capi.sqlite3 object which will hold the db's native +** pointer. theRc must be the result code of the open() op. If +** *ppDb is NULL then ps is set aside and its state cleared, +** else ps is associated with *ppDb. If *ppDb is not NULL then +** ps->jDb is stored in jOut (an OutputPointer.sqlite3 instance). +** +** Must be called if s3jni_open_pre() succeeds and must not be called +** if it doesn't. +** +** Returns theRc. +*/ +static int s3jni_open_post(JNIEnv * const env, S3JniEnv * const jc, + S3JniDb * ps, sqlite3 **ppDb, + jobject jOut, int theRc){ + int rc = 0; + jc->pdbOpening = 0; + if( *ppDb ){ + assert(ps->jDb); + if( 0==ps->pDb ){ + ps->pDb = *ppDb; + NativePointerHolder_set(S3JniNph(sqlite3), ps->jDb, *ppDb); + }else{ + assert( ps->pDb==*ppDb + && "Set up via s3jni_run_java_auto_extensions()" ); + } + rc = sqlite3_set_clientdata(ps->pDb, S3JniDb_clientdata_key, + ps, S3JniDb_xDestroy) + /* As of here, the Java/C connection is complete */; + }else{ + S3JniDb_set_aside(ps); + ps = 0; + } + OutputPointer_set_obj(env, S3JniNph(OutputPointer_sqlite3), + jOut, ps ? ps->jDb : 0); + return theRc ? theRc : rc; +} + +S3JniApi(sqlite3_open(),jint,1open)( + JniArgsEnvClass, jstring strName, jobject jOut +){ + sqlite3 * pOut = 0; + char *zName = 0; + S3JniDb * ps = 0; + S3JniEnv * jc = 0; + int rc; + + if( 0==jOut ) return SQLITE_MISUSE; + rc = s3jni_open_pre(env, &jc, strName, &zName, &ps); + if( 0==rc ){ + rc = s3jni_open_post(env, jc, ps, &pOut, jOut, + sqlite3_open(zName, &pOut)); + assert(rc==0 ? pOut!=0 : 1); + sqlite3_free(zName); + } + return (jint)rc; +} + +S3JniApi(sqlite3_open_v2(),jint,1open_1v2)( + JniArgsEnvClass, jstring strName, + jobject jOut, jint flags, jstring strVfs +){ + sqlite3 * pOut = 0; + char *zName = 0; + S3JniDb * ps = 0; + S3JniEnv * jc = 0; + char *zVfs = 0; + int rc; + + if( 0==jOut ) return SQLITE_MISUSE; + rc = s3jni_open_pre(env, &jc, strName, &zName, &ps); + if( 0==rc ){ + if( strVfs ){ + zVfs = s3jni_jstring_to_utf8( strVfs, 0); + if( !zVfs ){ + rc = SQLITE_NOMEM; + } + } + if( 0==rc ){ + rc = sqlite3_open_v2(zName, &pOut, (int)flags, zVfs); + } + rc = s3jni_open_post(env, jc, ps, &pOut, jOut, rc); + } + assert(rc==0 ? pOut!=0 : 1); + sqlite3_free(zName); + sqlite3_free(zVfs); + return (jint)rc; +} + +/* Proxy for the sqlite3_prepare[_v2/3]() family. */ +static jint sqlite3_jni_prepare_v123( int prepVersion, JNIEnv * const env, + jclass self, + jlong jpDb, jbyteArray baSql, + jint nMax, jint prepFlags, + jobject jOutStmt, jobject outTail){ + sqlite3_stmt * pStmt = 0; + jobject jStmt = 0; + const char * zTail = 0; + sqlite3 * const pDb = LongPtrGet_sqlite3(jpDb); + jbyte * const pBuf = pDb ? s3jni_jbyteArray_bytes(baSql) : 0; + int rc = SQLITE_ERROR; + + assert(prepVersion==1 || prepVersion==2 || prepVersion==3); + if( !pDb || !jOutStmt ){ + rc = SQLITE_MISUSE; + goto end; + }else if( !pBuf ){ + rc = baSql ? SQLITE_NOMEM : SQLITE_MISUSE; + goto end; + } + jStmt = new_java_sqlite3_stmt(env, 0); + if( !jStmt ){ + rc = SQLITE_NOMEM; + goto end; + } + switch( prepVersion ){ + case 1: rc = sqlite3_prepare(pDb, (const char *)pBuf, + (int)nMax, &pStmt, &zTail); + break; + case 2: rc = sqlite3_prepare_v2(pDb, (const char *)pBuf, + (int)nMax, &pStmt, &zTail); + break; + case 3: rc = sqlite3_prepare_v3(pDb, (const char *)pBuf, + (int)nMax, (unsigned int)prepFlags, + &pStmt, &zTail); + break; + default: + assert(!"Invalid prepare() version"); + } +end: + s3jni_jbyteArray_release(baSql,pBuf); + if( 0==rc ){ + if( 0!=outTail ){ + /* Noting that pBuf is deallocated now but its address is all we need for + ** what follows... */ + assert(zTail ? ((void*)zTail>=(void*)pBuf) : 1); + assert(zTail ? (((int)((void*)zTail - (void*)pBuf)) >= 0) : 1); + OutputPointer_set_Int32( + env, outTail, (int)(zTail ? (zTail - (const char *)pBuf) : 0) + ); + } + if( pStmt ){ + NativePointerHolder_set(S3JniNph(sqlite3_stmt), jStmt, pStmt); + }else{ + /* Happens for comments and whitespace. */ + S3JniUnrefLocal(jStmt); + jStmt = 0; + } + }else{ + S3JniUnrefLocal(jStmt); + jStmt = 0; + } + if( jOutStmt ){ + OutputPointer_set_obj(env, S3JniNph(OutputPointer_sqlite3_stmt), + jOutStmt, jStmt); + } + return (jint)rc; +} +S3JniApi(sqlite3_prepare(),jint,1prepare)( + JNIEnv * const env, jclass self, jlong jpDb, jbyteArray baSql, + jint nMax, jobject jOutStmt, jobject outTail +){ + return sqlite3_jni_prepare_v123(1, env, self, jpDb, baSql, nMax, 0, + jOutStmt, outTail); +} +S3JniApi(sqlite3_prepare_v2(),jint,1prepare_1v2)( + JNIEnv * const env, jclass self, jlong jpDb, jbyteArray baSql, + jint nMax, jobject jOutStmt, jobject outTail +){ + return sqlite3_jni_prepare_v123(2, env, self, jpDb, baSql, nMax, 0, + jOutStmt, outTail); +} +S3JniApi(sqlite3_prepare_v3(),jint,1prepare_1v3)( + JNIEnv * const env, jclass self, jlong jpDb, jbyteArray baSql, + jint nMax, jint prepFlags, jobject jOutStmt, jobject outTail +){ + return sqlite3_jni_prepare_v123(3, env, self, jpDb, baSql, nMax, + prepFlags, jOutStmt, outTail); +} + +/* +** Impl for C-to-Java of the callbacks for both sqlite3_update_hook() +** and sqlite3_preupdate_hook(). The differences are that for +** update_hook(): +** +** - pDb is NULL +** - iKey1 is the row ID +** - iKey2 is unused +*/ +static void s3jni_updatepre_hook_impl(void * pState, sqlite3 *pDb, int opId, + const char *zDb, const char *zTable, + sqlite3_int64 iKey1, sqlite3_int64 iKey2){ + S3JniDb * const ps = pState; + S3JniDeclLocal_env; + jstring jDbName; + jstring jTable; + const int isPre = 0!=pDb; + S3JniHook hook; + + S3JniHook_localdup(isPre ? +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK + &ps->hooks.preUpdate +#else + &S3JniHook_empty +#endif + : &ps->hooks.update, &hook); + if( !hook.jObj ){ + return; + } + jDbName = s3jni_utf8_to_jstring( zDb, -1); + jTable = jDbName ? s3jni_utf8_to_jstring( zTable, -1) : 0; + S3JniIfThrew { + S3JniExceptionClear; + s3jni_db_error(ps->pDb, SQLITE_NOMEM, 0); + }else{ + assert( hook.jObj ); + assert( hook.midCallback ); + assert( ps->jDb ); +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK + if( isPre ) (*env)->CallVoidMethod(env, hook.jObj, hook.midCallback, + ps->jDb, (jint)opId, jDbName, jTable, + (jlong)iKey1, (jlong)iKey2); + else +#endif + (*env)->CallVoidMethod(env, hook.jObj, hook.midCallback, + (jint)opId, jDbName, jTable, (jlong)iKey1); + S3JniIfThrew{ + S3JniExceptionWarnCallbackThrew("sqlite3_(pre)update_hook() callback"); + s3jni_db_exception(ps->pDb, 0, + "sqlite3_(pre)update_hook() callback threw"); + } + } + S3JniUnrefLocal(jDbName); + S3JniUnrefLocal(jTable); + S3JniHook_localundup(hook); +} + +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK +static void s3jni_preupdate_hook_impl(void * pState, sqlite3 *pDb, int opId, + const char *zDb, const char *zTable, + sqlite3_int64 iKey1, sqlite3_int64 iKey2){ + return s3jni_updatepre_hook_impl(pState, pDb, opId, zDb, zTable, + iKey1, iKey2); +} +#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ + +static void s3jni_update_hook_impl(void * pState, int opId, const char *zDb, + const char *zTable, sqlite3_int64 nRowid){ + return s3jni_updatepre_hook_impl(pState, NULL, opId, zDb, zTable, nRowid, 0); +} + +#if !defined(SQLITE_ENABLE_PREUPDATE_HOOK) +/* We need no-op impls for preupdate_{count,depth,blobwrite}() */ +S3JniApi(sqlite3_preupdate_blobwrite(),jint,1preupdate_1blobwrite)( + JniArgsEnvClass, jlong jDb){ return SQLITE_MISUSE; } +S3JniApi(sqlite3_preupdate_count(),jint,1preupdate_1count)( + JniArgsEnvClass, jlong jDb){ return SQLITE_MISUSE; } +S3JniApi(sqlite3_preupdate_depth(),jint,1preupdate_1depth)( + JniArgsEnvClass, jlong jDb){ return SQLITE_MISUSE; } +#endif /* !SQLITE_ENABLE_PREUPDATE_HOOK */ + +/* +** JNI wrapper for both sqlite3_update_hook() and +** sqlite3_preupdate_hook() (if isPre is true). +*/ +static jobject s3jni_updatepre_hook(JNIEnv * env, int isPre, jlong jpDb, jobject jHook){ + S3JniDb * const ps = S3JniDb_from_jlong(jpDb); + jclass klazz; + jobject pOld = 0; + jmethodID xCallback; + S3JniHook * pHook; + + if( !ps ) return 0; + S3JniDb_mutex_enter; + pHook = isPre ? +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK + &ps->hooks.preUpdate +#else + 0 +#endif + : &ps->hooks.update; + if( !pHook ){ + goto end; + } + pOld = pHook->jObj; + if( pOld && jHook && (*env)->IsSameObject(env, pOld, jHook) ){ + goto end; + } + if( !jHook ){ + if( pOld ){ + jobject tmp = S3JniRefLocal(pOld); + S3JniUnrefGlobal(pOld); + pOld = tmp; + } + *pHook = S3JniHook_empty; +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK + if( isPre ) sqlite3_preupdate_hook(ps->pDb, 0, 0); + else +#endif + sqlite3_update_hook(ps->pDb, 0, 0); + goto end; + } + klazz = (*env)->GetObjectClass(env, jHook); + xCallback = isPre + ? (*env)->GetMethodID(env, klazz, "call", + "(Lorg/sqlite/jni/capi/sqlite3;" + "I" + "Ljava/lang/String;" + "Ljava/lang/String;" + "JJ)V") + : (*env)->GetMethodID(env, klazz, "call", + "(ILjava/lang/String;Ljava/lang/String;J)V"); + S3JniUnrefLocal(klazz); + S3JniIfThrew { + S3JniExceptionClear; + s3jni_db_error(ps->pDb, SQLITE_ERROR, + "Cannot not find matching callback on " + "(pre)update hook object."); + }else{ + pHook->midCallback = xCallback; + pHook->jObj = S3JniRefGlobal(jHook); +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK + if( isPre ) sqlite3_preupdate_hook(ps->pDb, s3jni_preupdate_hook_impl, ps); + else +#endif + sqlite3_update_hook(ps->pDb, s3jni_update_hook_impl, ps); + if( pOld ){ + jobject tmp = S3JniRefLocal(pOld); + S3JniUnrefGlobal(pOld); + pOld = tmp; + } + } +end: + S3JniDb_mutex_leave; + return pOld; +} + + +S3JniApi(sqlite3_preupdate_hook(),jobject,1preupdate_1hook)( + JniArgsEnvClass, jlong jpDb, jobject jHook +){ +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK + return s3jni_updatepre_hook(env, 1, jpDb, jHook); +#else + return NULL; +#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ +} + +/* Impl for sqlite3_preupdate_{new,old}(). */ +static int s3jni_preupdate_newold(JNIEnv * const env, int isNew, jlong jpDb, + jint iCol, jobject jOut){ +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK + sqlite3 * const pDb = LongPtrGet_sqlite3(jpDb); + int rc = SQLITE_MISUSE; + if( pDb ){ + sqlite3_value * pOut = 0; + int (*fOrig)(sqlite3*,int,sqlite3_value**) = + isNew ? sqlite3_preupdate_new : sqlite3_preupdate_old; + rc = fOrig(pDb, (int)iCol, &pOut); + if( 0==rc ){ + jobject pWrap = new_java_sqlite3_value(env, pOut); + if( !pWrap ){ + rc = SQLITE_NOMEM; + } + OutputPointer_set_obj(env, S3JniNph(OutputPointer_sqlite3_value), + jOut, pWrap); + S3JniUnrefLocal(pWrap); + } + } + return rc; +#else + return SQLITE_MISUSE; +#endif +} + +S3JniApi(sqlite3_preupdate_new(),jint,1preupdate_1new)( + JniArgsEnvClass, jlong jpDb, jint iCol, jobject jOut +){ + return s3jni_preupdate_newold(env, 1, jpDb, iCol, jOut); +} + +S3JniApi(sqlite3_preupdate_old(),jint,1preupdate_1old)( + JniArgsEnvClass, jlong jpDb, jint iCol, jobject jOut +){ + return s3jni_preupdate_newold(env, 0, jpDb, iCol, jOut); +} + + +/* Central C-to-Java sqlite3_progress_handler() proxy. */ +static int s3jni_progress_handler_impl(void *pP){ + S3JniDb * const ps = (S3JniDb *)pP; + int rc = 0; + S3JniDeclLocal_env; + S3JniHook hook; + + S3JniHook_localdup(&ps->hooks.progress, &hook); + if( hook.jObj ){ + rc = (int)(*env)->CallIntMethod(env, hook.jObj, hook.midCallback); + S3JniIfThrew{ + rc = s3jni_db_exception(ps->pDb, rc, + "sqlite3_progress_handler() callback threw"); + } + S3JniHook_localundup(hook); + } + return rc; +} + +S3JniApi(sqlite3_progress_handler(),void,1progress_1handler)( + JniArgsEnvClass,jobject jDb, jint n, jobject jProgress +){ + S3JniDb * const ps = S3JniDb_from_java(jDb); + S3JniHook * const pHook = ps ? &ps->hooks.progress : 0; + + if( !ps ) return; + S3JniDb_mutex_enter; + if( n<1 || !jProgress ){ + S3JniHook_unref(pHook); + sqlite3_progress_handler(ps->pDb, 0, 0, 0); + }else{ + jclass const klazz = (*env)->GetObjectClass(env, jProgress); + jmethodID const xCallback = (*env)->GetMethodID(env, klazz, "call", "()I"); + S3JniUnrefLocal(klazz); + S3JniIfThrew { + S3JniExceptionClear; + s3jni_db_error(ps->pDb, SQLITE_ERROR, + "Cannot not find matching xCallback() on " + "ProgressHandler object."); + }else{ + S3JniUnrefGlobal(pHook->jObj); + pHook->midCallback = xCallback; + pHook->jObj = S3JniRefGlobal(jProgress); + sqlite3_progress_handler(ps->pDb, (int)n, s3jni_progress_handler_impl, ps); + } + } + S3JniDb_mutex_leave; +} + +S3JniApi(sqlite3_randomness(),void,1randomness)( + JniArgsEnvClass, jbyteArray jTgt +){ + jbyte * const jba = s3jni_jbyteArray_bytes(jTgt); + if( jba ){ + jsize const nTgt = (*env)->GetArrayLength(env, jTgt); + sqlite3_randomness( (int)nTgt, jba ); + s3jni_jbyteArray_commit(jTgt, jba); + } +} + + +S3JniApi(sqlite3_reset(),jint,1reset)( + JniArgsEnvClass, jobject jpStmt +){ + sqlite3_stmt * const pStmt = PtrGet_sqlite3_stmt(jpStmt); + return pStmt ? sqlite3_reset(pStmt) : SQLITE_MISUSE; +} + +/* Clears all entries from S3JniGlobal.autoExt. */ +static void s3jni_reset_auto_extension(JNIEnv *env){ + int i; + S3JniAutoExt_mutex_enter; + for( i = 0; i < SJG.autoExt.nExt; ++i ){ + S3JniAutoExtension_clear( &SJG.autoExt.aExt[i] ); + } + SJG.autoExt.nExt = 0; + S3JniAutoExt_mutex_leave; +} + +S3JniApi(sqlite3_reset_auto_extension(),void,1reset_1auto_1extension)( + JniArgsEnvClass +){ + s3jni_reset_auto_extension(env); +} + +/* Impl for sqlite3_result_text/blob() and friends. */ +static void result_blob_text(int as64 /* true for text64/blob64() mode */, + int eTextRep /* 0 for blobs, else SQLITE_UTF... */, + JNIEnv * const env, sqlite3_context *pCx, + jbyteArray jBa, jlong nMax){ + int const asBlob = 0==eTextRep; + if( !pCx ){ + /* We should arguably emit a warning here. But where to log it? */ + return; + }else if( jBa ){ + jbyte * const pBuf = s3jni_jbyteArray_bytes(jBa); + jsize nBA = (*env)->GetArrayLength(env, jBa); + if( nMax>=0 && nBA>(jsize)nMax ){ + nBA = (jsize)nMax; + /** + From the sqlite docs: + + > If the 3rd parameter to any of the sqlite3_result_text* + interfaces other than sqlite3_result_text64() is negative, + then SQLite computes the string length itself by searching + the 2nd parameter for the first zero character. + + Note that the text64() interfaces take an unsigned value for + the length, which Java does not support. This binding takes + the approach of passing on negative values to the C API, + which will in turn fail with SQLITE_TOOBIG at some later + point (recall that the sqlite3_result_xyz() family do not + have result values). + */ + } + if( as64 ){ /* 64-bit... */ + static const jsize nLimit64 = + SQLITE_MAX_ALLOCATION_SIZE/*only _kinda_ arbitrary*/; + if( nBA > nLimit64 ){ + sqlite3_result_error_toobig(pCx); + }else if( asBlob ){ + sqlite3_result_blob64(pCx, pBuf, (sqlite3_uint64)nBA, + SQLITE_TRANSIENT); + }else{ /* text64... */ + if( encodingTypeIsValid(eTextRep) ){ + sqlite3_result_text64(pCx, (const char *)pBuf, + (sqlite3_uint64)nBA, + SQLITE_TRANSIENT, eTextRep); + }else{ + sqlite3_result_error_code(pCx, SQLITE_FORMAT); + } + } + }else{ /* 32-bit... */ + static const jsize nLimit = SQLITE_MAX_ALLOCATION_SIZE; + if( nBA > nLimit ){ + sqlite3_result_error_toobig(pCx); + }else if( asBlob ){ + sqlite3_result_blob(pCx, pBuf, (int)nBA, + SQLITE_TRANSIENT); + }else{ + switch( eTextRep ){ + case SQLITE_UTF8: + sqlite3_result_text(pCx, (const char *)pBuf, (int)nBA, + SQLITE_TRANSIENT); + break; + case SQLITE_UTF16: + sqlite3_result_text16(pCx, (const char *)pBuf, (int)nBA, + SQLITE_TRANSIENT); + break; + case SQLITE_UTF16LE: + sqlite3_result_text16le(pCx, (const char *)pBuf, (int)nBA, + SQLITE_TRANSIENT); + break; + case SQLITE_UTF16BE: + sqlite3_result_text16be(pCx, (const char *)pBuf, (int)nBA, + SQLITE_TRANSIENT); + break; + } + } + s3jni_jbyteArray_release(jBa, pBuf); + } + }else{ + sqlite3_result_null(pCx); + } +} + +S3JniApi(sqlite3_result_blob(),void,1result_1blob)( + JniArgsEnvClass, jobject jpCx, jbyteArray jBa, jint nMax +){ + return result_blob_text(0, 0, env, PtrGet_sqlite3_context(jpCx), jBa, nMax); +} + +S3JniApi(sqlite3_result_blob64(),void,1result_1blob64)( + JniArgsEnvClass, jobject jpCx, jbyteArray jBa, jlong nMax +){ + return result_blob_text(1, 0, env, PtrGet_sqlite3_context(jpCx), jBa, nMax); +} + +S3JniApi(sqlite3_result_double(),void,1result_1double)( + JniArgsEnvClass, jobject jpCx, jdouble v +){ + sqlite3_result_double(PtrGet_sqlite3_context(jpCx), v); +} + +S3JniApi(sqlite3_result_error(),void,1result_1error)( + JniArgsEnvClass, jobject jpCx, jbyteArray baMsg, jint eTextRep +){ + const char * zUnspecified = "Unspecified error."; + jsize const baLen = (*env)->GetArrayLength(env, baMsg); + jbyte * const pjBuf = baMsg ? s3jni_jbyteArray_bytes(baMsg) : NULL; + switch( pjBuf ? eTextRep : SQLITE_UTF8 ){ + case SQLITE_UTF8: { + const char *zMsg = pjBuf ? (const char *)pjBuf : zUnspecified; + int const n = pjBuf ? (int)baLen : (int)sqlite3Strlen30(zMsg); + sqlite3_result_error(PtrGet_sqlite3_context(jpCx), zMsg, n); + break; + } + case SQLITE_UTF16: { + const void *zMsg = pjBuf; + sqlite3_result_error16(PtrGet_sqlite3_context(jpCx), zMsg, (int)baLen); + break; + } + default: + sqlite3_result_error(PtrGet_sqlite3_context(jpCx), + "Invalid encoding argument passed " + "to sqlite3_result_error().", -1); + break; + } + s3jni_jbyteArray_release(baMsg,pjBuf); +} + +S3JniApi(sqlite3_result_error_code(),void,1result_1error_1code)( + JniArgsEnvClass, jobject jpCx, jint v +){ + sqlite3_result_error_code(PtrGet_sqlite3_context(jpCx), (int)v); +} + +S3JniApi(sqlite3_result_error_nomem(),void,1result_1error_1nomem)( + JniArgsEnvClass, jobject jpCx +){ + sqlite3_result_error_nomem(PtrGet_sqlite3_context(jpCx)); +} + +S3JniApi(sqlite3_result_error_toobig(),void,1result_1error_1toobig)( + JniArgsEnvClass, jobject jpCx +){ + sqlite3_result_error_toobig(PtrGet_sqlite3_context(jpCx)); +} + +S3JniApi(sqlite3_result_int(),void,1result_1int)( + JniArgsEnvClass, jobject jpCx, jint v +){ + sqlite3_result_int(PtrGet_sqlite3_context(jpCx), (int)v); +} + +S3JniApi(sqlite3_result_int64(),void,1result_1int64)( + JniArgsEnvClass, jobject jpCx, jlong v +){ + sqlite3_result_int64(PtrGet_sqlite3_context(jpCx), (sqlite3_int64)v); +} + +S3JniApi(sqlite3_result_java_object(),void,1result_1java_1object)( + JniArgsEnvClass, jobject jpCx, jobject v +){ + sqlite3_context * pCx = PtrGet_sqlite3_context(jpCx); + if( !pCx ) return; + else if( v ){ + jobject const rjv = S3JniRefGlobal(v); + if( rjv ){ + sqlite3_result_pointer(pCx, rjv, + s3jni__value_jref_key, S3Jni_jobject_finalizer); + }else{ + sqlite3_result_error_nomem(PtrGet_sqlite3_context(jpCx)); + } + }else{ + sqlite3_result_null(PtrGet_sqlite3_context(jpCx)); + } +} + +S3JniApi(sqlite3_result_nio_buffer(),void,1result_1nio_1buffer)( + JniArgsEnvClass, jobject jpCtx, jobject jBuffer, + jint iOffset, jint iN +){ + sqlite3_context * pCx = PtrGet_sqlite3_context(jpCtx); + int rc; + S3JniNioArgs args; + if( !pCx ){ + return; + }else if( !SJG.g.byteBuffer.klazz ){ + sqlite3_result_error( + pCx, "This JVM does not support JNI access to ByteBuffers.", -1 + ); + return; + } + rc = s3jni_setup_nio_args(env, &args, jBuffer, iOffset, iN); + if(rc){ + if( iOffset<0 ){ + sqlite3_result_error(pCx, "Start index may not be negative.", -1); + }else if( SQLITE_TOOBIG==rc ){ + sqlite3_result_error_toobig(pCx); + }else{ + sqlite3_result_error( + pCx, "Invalid arguments to sqlite3_result_nio_buffer().", -1 + ); + } + }else if( !args.pStart || !args.nOut ){ + sqlite3_result_null(pCx); + }else{ + sqlite3_result_blob(pCx, args.pStart, args.nOut, SQLITE_TRANSIENT); + } +} + + +S3JniApi(sqlite3_result_null(),void,1result_1null)( + JniArgsEnvClass, jobject jpCx +){ + sqlite3_result_null(PtrGet_sqlite3_context(jpCx)); +} + +S3JniApi(sqlite3_result_subtype(),void,1result_1subtype)( + JniArgsEnvClass, jobject jpCx, jint v +){ + sqlite3_result_subtype(PtrGet_sqlite3_context(jpCx), (unsigned int)v); +} + + +S3JniApi(sqlite3_result_text(),void,1result_1text)( + JniArgsEnvClass, jobject jpCx, jbyteArray jBa, jint nMax +){ + return result_blob_text(0, SQLITE_UTF8, env, + PtrGet_sqlite3_context(jpCx), jBa, nMax); +} + +S3JniApi(sqlite3_result_text64(),void,1result_1text64)( + JniArgsEnvClass, jobject jpCx, jbyteArray jBa, jlong nMax, + jint eTextRep +){ + return result_blob_text(1, eTextRep, env, + PtrGet_sqlite3_context(jpCx), jBa, nMax); +} + +S3JniApi(sqlite3_result_value(),void,1result_1value)( + JniArgsEnvClass, jobject jpCx, jobject jpSVal +){ + sqlite3_result_value(PtrGet_sqlite3_context(jpCx), + PtrGet_sqlite3_value(jpSVal)); +} + +S3JniApi(sqlite3_result_zeroblob(),void,1result_1zeroblob)( + JniArgsEnvClass, jobject jpCx, jint v +){ + sqlite3_result_zeroblob(PtrGet_sqlite3_context(jpCx), (int)v); +} + +S3JniApi(sqlite3_result_zeroblob64(),jint,1result_1zeroblob64)( + JniArgsEnvClass, jobject jpCx, jlong v +){ + return (jint)sqlite3_result_zeroblob64(PtrGet_sqlite3_context(jpCx), + (sqlite3_int64)v); +} + +S3JniApi(sqlite3_rollback_hook(),jobject,1rollback_1hook)( + JniArgsEnvClass, jlong jpDb, jobject jHook +){ + return s3jni_commit_rollback_hook(0, env, jpDb, jHook); +} + +/* Callback for sqlite3_set_authorizer(). */ +int s3jni_xAuth(void* pState, int op,const char*z0, const char*z1, + const char*z2,const char*z3){ + S3JniDb * const ps = pState; + S3JniDeclLocal_env; + S3JniHook hook; + int rc = 0; + + S3JniHook_localdup(&ps->hooks.auth, &hook ); + if( hook.jObj ){ + jstring const s0 = z0 ? s3jni_utf8_to_jstring( z0, -1) : 0; + jstring const s1 = z1 ? s3jni_utf8_to_jstring( z1, -1) : 0; + jstring const s2 = z2 ? s3jni_utf8_to_jstring( z2, -1) : 0; + jstring const s3 = z3 ? s3jni_utf8_to_jstring( z3, -1) : 0; + + rc = (*env)->CallIntMethod(env, hook.jObj, hook.midCallback, (jint)op, + s0, s1, s3, s3); + S3JniIfThrew{ + rc = s3jni_db_exception(ps->pDb, rc, "sqlite3_set_authorizer() callback"); + } + S3JniUnrefLocal(s0); + S3JniUnrefLocal(s1); + S3JniUnrefLocal(s2); + S3JniUnrefLocal(s3); + S3JniHook_localundup(hook); + } + return rc; +} + +S3JniApi(sqlite3_set_authorizer(),jint,1set_1authorizer)( + JniArgsEnvClass,jobject jDb, jobject jHook +){ + S3JniDb * const ps = S3JniDb_from_java(jDb); + S3JniHook * const pHook = ps ? &ps->hooks.auth : 0; + int rc = 0; + + if( !ps ) return SQLITE_MISUSE; + S3JniDb_mutex_enter; + if( !jHook ){ + S3JniHook_unref(pHook); + rc = sqlite3_set_authorizer( ps->pDb, 0, 0 ); + }else{ + jclass klazz; + if( pHook->jObj ){ + if( (*env)->IsSameObject(env, pHook->jObj, jHook) ){ + /* Same object - this is a no-op. */ + S3JniDb_mutex_leave; + return 0; + } + S3JniHook_unref(pHook); + } + pHook->jObj = S3JniRefGlobal(jHook); + klazz = (*env)->GetObjectClass(env, jHook); + pHook->midCallback = (*env)->GetMethodID(env, klazz, + "call", + "(I" + "Ljava/lang/String;" + "Ljava/lang/String;" + "Ljava/lang/String;" + "Ljava/lang/String;" + ")I"); + S3JniUnrefLocal(klazz); + S3JniIfThrew { + rc = s3jni_db_error(ps->pDb, SQLITE_ERROR, + "Error setting up Java parts of authorizer hook."); + }else{ + rc = sqlite3_set_authorizer(ps->pDb, s3jni_xAuth, ps); + } + if( rc ) S3JniHook_unref(pHook); + } + S3JniDb_mutex_leave; + return rc; +} + +S3JniApi(sqlite3_set_auxdata(),void,1set_1auxdata)( + JniArgsEnvClass, jobject jCx, jint n, jobject jAux +){ + sqlite3_set_auxdata(PtrGet_sqlite3_context(jCx), (int)n, + S3JniRefGlobal(jAux), S3Jni_jobject_finalizer); +} + +S3JniApi(sqlite3_set_last_insert_rowid(),void,1set_1last_1insert_1rowid)( + JniArgsEnvClass, jobject jpDb, jlong rowId +){ + sqlite3_set_last_insert_rowid(PtrGet_sqlite3(jpDb), + (sqlite3_int64)rowId); +} + +S3JniApi(sqlite3_shutdown(),jint,1shutdown)( + JniArgsEnvClass +){ + s3jni_reset_auto_extension(env); +#ifdef SQLITE_ENABLE_SQLLOG + S3JniHook_unref(&SJG.hook.sqllog); +#endif + S3JniHook_unref(&SJG.hook.configlog); + /* Free up S3JniDb recycling bin. */ + S3JniDb_mutex_enter; { + while( S3JniGlobal.perDb.aFree ){ + S3JniDb * const d = S3JniGlobal.perDb.aFree; + S3JniGlobal.perDb.aFree = d->pNext; + S3JniDb_clear(env, d); + sqlite3_free(d); + } + } S3JniDb_mutex_leave; + S3JniGlobal_mutex_enter; { + /* Free up S3JniUdf recycling bin. */ + while( S3JniGlobal.udf.aFree ){ + S3JniUdf * const u = S3JniGlobal.udf.aFree; + S3JniGlobal.udf.aFree = u->pNext; + u->pNext = 0; + S3JniUdf_free(env, u, 0); + } + } S3JniGlobal_mutex_leave; + S3JniHook_mutex_enter; { + /* Free up S3JniHook recycling bin. */ + while( S3JniGlobal.hook.aFree ){ + S3JniHook * const u = S3JniGlobal.hook.aFree; + S3JniGlobal.hook.aFree = u->pNext; + u->pNext = 0; + assert( !u->doXDestroy ); + assert( !u->jObj ); + assert( !u->jExtra ); + sqlite3_free( u ); + } + } S3JniHook_mutex_leave; + /* Free up env cache. */ + S3JniEnv_mutex_enter; { + while( SJG.envCache.aHead ){ + S3JniEnv_uncache( SJG.envCache.aHead->env ); + } + } S3JniEnv_mutex_leave; + /* Do not clear S3JniGlobal.jvm or S3JniGlobal.g: it's legal to + ** restart the lib. */ + return sqlite3_shutdown(); +} + +S3JniApi(sqlite3_status(),jint,1status)( + JniArgsEnvClass, jint op, jobject jOutCurrent, jobject jOutHigh, + jboolean reset +){ + int iCur = 0, iHigh = 0; + int rc = sqlite3_status( op, &iCur, &iHigh, reset ); + if( 0==rc ){ + OutputPointer_set_Int32(env, jOutCurrent, iCur); + OutputPointer_set_Int32(env, jOutHigh, iHigh); + } + return (jint)rc; +} + +S3JniApi(sqlite3_status64(),jint,1status64)( + JniArgsEnvClass, jint op, jobject jOutCurrent, jobject jOutHigh, + jboolean reset +){ + sqlite3_int64 iCur = 0, iHigh = 0; + int rc = sqlite3_status64( op, &iCur, &iHigh, reset ); + if( 0==rc ){ + OutputPointer_set_Int64(env, jOutCurrent, iCur); + OutputPointer_set_Int64(env, jOutHigh, iHigh); + } + return (jint)rc; +} + +S3JniApi(sqlite3_stmt_status(),jint,1stmt_1status)( + JniArgsEnvClass, jobject jStmt, jint op, jboolean reset +){ + return sqlite3_stmt_status(PtrGet_sqlite3_stmt(jStmt), + (int)op, reset ? 1 : 0); +} + + +static int s3jni_strlike_glob(int isLike, JNIEnv *const env, + jbyteArray baG, jbyteArray baT, jint escLike){ + int rc = 0; + jbyte * const pG = s3jni_jbyteArray_bytes(baG); + jbyte * const pT = s3jni_jbyteArray_bytes(baT); + + /* Note that we're relying on the byte arrays having been + NUL-terminated on the Java side. */ + rc = isLike + ? sqlite3_strlike((const char *)pG, (const char *)pT, + (unsigned int)escLike) + : sqlite3_strglob((const char *)pG, (const char *)pT); + s3jni_jbyteArray_release(baG, pG); + s3jni_jbyteArray_release(baT, pT); + return rc; +} + +S3JniApi(sqlite3_strglob(),jint,1strglob)( + JniArgsEnvClass, jbyteArray baG, jbyteArray baT +){ + return s3jni_strlike_glob(0, env, baG, baT, 0); +} + +S3JniApi(sqlite3_strlike(),jint,1strlike)( + JniArgsEnvClass, jbyteArray baG, jbyteArray baT, jint escChar +){ + return s3jni_strlike_glob(1, env, baG, baT, escChar); +} + +S3JniApi(sqlite3_sql(),jstring,1sql)( + JniArgsEnvClass, jobject jpStmt +){ + sqlite3_stmt * const pStmt = PtrGet_sqlite3_stmt(jpStmt); + jstring rv = 0; + if( pStmt ){ + const char * zSql = 0; + zSql = sqlite3_sql(pStmt); + rv = s3jni_utf8_to_jstring( zSql, -1); + } + return rv; +} + +S3JniApi(sqlite3_step(),jint,1step)( + JniArgsEnvClass, jlong jpStmt +){ + sqlite3_stmt * const pStmt = LongPtrGet_sqlite3_stmt(jpStmt); + return pStmt ? (jint)sqlite3_step(pStmt) : (jint)SQLITE_MISUSE; +} + +S3JniApi(sqlite3_table_column_metadata(),jint,1table_1column_1metadata)( + JniArgsEnvClass, jobject jDb, jstring jDbName, jstring jTableName, + jstring jColumnName, jobject jDataType, jobject jCollSeq, jobject jNotNull, + jobject jPrimaryKey, jobject jAutoinc +){ + sqlite3 * const db = PtrGet_sqlite3(jDb); + char * zDbName = 0, * zTableName = 0, * zColumnName = 0; + const char * pzCollSeq = 0; + const char * pzDataType = 0; + int pNotNull = 0, pPrimaryKey = 0, pAutoinc = 0; + int rc; + + if( !db || !jDbName || !jTableName ) return SQLITE_MISUSE; + zDbName = s3jni_jstring_to_utf8(jDbName,0); + zTableName = zDbName ? s3jni_jstring_to_utf8(jTableName,0) : 0; + zColumnName = (zTableName && jColumnName) + ? s3jni_jstring_to_utf8(jColumnName,0) : 0; + rc = zTableName + ? sqlite3_table_column_metadata(db, zDbName, zTableName, + zColumnName, &pzDataType, &pzCollSeq, + &pNotNull, &pPrimaryKey, &pAutoinc) + : SQLITE_NOMEM; + if( 0==rc ){ + jstring jseq = jCollSeq + ? (pzCollSeq ? s3jni_utf8_to_jstring(pzCollSeq, -1) : 0) + : 0; + jstring jdtype = jDataType + ? (pzDataType ? s3jni_utf8_to_jstring(pzDataType, -1) : 0) + : 0; + if( (jCollSeq && pzCollSeq && !jseq) + || (jDataType && pzDataType && !jdtype) ){ + rc = SQLITE_NOMEM; + }else{ + if( jNotNull ) OutputPointer_set_Bool(env, jNotNull, pNotNull); + if( jPrimaryKey ) OutputPointer_set_Bool(env, jPrimaryKey, pPrimaryKey); + if( jAutoinc ) OutputPointer_set_Bool(env, jAutoinc, pAutoinc); + if( jCollSeq ) OutputPointer_set_String(env, jCollSeq, jseq); + if( jDataType ) OutputPointer_set_String(env, jDataType, jdtype); + } + S3JniUnrefLocal(jseq); + S3JniUnrefLocal(jdtype); + } + sqlite3_free(zDbName); + sqlite3_free(zTableName); + sqlite3_free(zColumnName); + return rc; +} + +static int s3jni_trace_impl(unsigned traceflag, void *pC, void *pP, void *pX){ + S3JniDb * const ps = (S3JniDb *)pC; + S3JniDeclLocal_env; + jobject jX = NULL /* the tracer's X arg */; + jobject jP = NULL /* the tracer's P arg */; + jobject jPUnref = NULL /* potentially a local ref to jP */; + int rc = 0; + S3JniHook hook; + + S3JniHook_localdup(&ps->hooks.trace, &hook ); + if( !hook.jObj ){ + return 0; + } + switch( traceflag ){ + case SQLITE_TRACE_STMT: + jX = s3jni_utf8_to_jstring( (const char *)pX, -1); + if( !jX ) rc = SQLITE_NOMEM; + break; + case SQLITE_TRACE_PROFILE: + jX = (*env)->NewObject(env, SJG.g.cLong, SJG.g.ctorLong1, + (jlong)*((sqlite3_int64*)pX)); + // hmm. ^^^ (*pX) really is zero. + // MARKER(("profile time = %llu\n", *((sqlite3_int64*)pX))); + s3jni_oom_check( jX ); + if( !jX ) rc = SQLITE_NOMEM; + break; + case SQLITE_TRACE_ROW: + break; + case SQLITE_TRACE_CLOSE: + jP = jPUnref = S3JniRefLocal(ps->jDb); + break; + default: + assert(!"cannot happen - unknown trace flag"); + rc = SQLITE_ERROR; + } + if( 0==rc ){ + if( !jP ){ + /* Create a new temporary sqlite3_stmt wrapper */ + jP = jPUnref = new_java_sqlite3_stmt(env, pP); + if( !jP ){ + rc = SQLITE_NOMEM; + } + } + if( 0==rc ){ + assert(jP); + rc = (int)(*env)->CallIntMethod(env, hook.jObj, hook.midCallback, + (jint)traceflag, jP, jX); + S3JniIfThrew{ + rc = s3jni_db_exception(ps->pDb, SQLITE_ERROR, + "sqlite3_trace_v2() callback threw."); + } + } + } + S3JniUnrefLocal(jPUnref); + S3JniUnrefLocal(jX); + S3JniHook_localundup(hook); + return rc; +} + +S3JniApi(sqlite3_trace_v2(),jint,1trace_1v2)( + JniArgsEnvClass,jobject jDb, jint traceMask, jobject jTracer +){ + S3JniDb * const ps = S3JniDb_from_java(jDb); + int rc; + + if( !ps ) return SQLITE_MISUSE; + if( !traceMask || !jTracer ){ + S3JniDb_mutex_enter; + rc = (jint)sqlite3_trace_v2(ps->pDb, 0, 0, 0); + S3JniHook_unref(&ps->hooks.trace); + S3JniDb_mutex_leave; + }else{ + jclass const klazz = (*env)->GetObjectClass(env, jTracer); + S3JniHook hook = S3JniHook_empty; + hook.midCallback = (*env)->GetMethodID( + env, klazz, "call", "(ILjava/lang/Object;Ljava/lang/Object;)I" + ); + S3JniUnrefLocal(klazz); + S3JniIfThrew { + S3JniExceptionClear; + rc = s3jni_db_error(ps->pDb, SQLITE_ERROR, + "Cannot not find matching call() on " + "TracerCallback object."); + }else{ + hook.jObj = S3JniRefGlobal(jTracer); + S3JniDb_mutex_enter; + rc = sqlite3_trace_v2(ps->pDb, (unsigned)traceMask, s3jni_trace_impl, ps); + if( 0==rc ){ + S3JniHook_unref(&ps->hooks.trace); + ps->hooks.trace = hook /* transfer ownership of reference */; + }else{ + S3JniHook_unref(&hook); + } + S3JniDb_mutex_leave; + } + } + return rc; +} + +S3JniApi(sqlite3_txn_state(),jint,1txn_1state)( + JniArgsEnvClass,jobject jDb, jstring jSchema +){ + sqlite3 * const pDb = PtrGet_sqlite3(jDb); + int rc = SQLITE_MISUSE; + if( pDb ){ + char * zSchema = jSchema + ? s3jni_jstring_to_utf8(jSchema, 0) + : 0; + if( !jSchema || (zSchema && jSchema) ){ + rc = sqlite3_txn_state(pDb, zSchema); + sqlite3_free(zSchema); + }else{ + rc = SQLITE_NOMEM; + } + } + return rc; +} + +S3JniApi(sqlite3_update_hook(),jobject,1update_1hook)( + JniArgsEnvClass, jlong jpDb, jobject jHook +){ + return s3jni_updatepre_hook(env, 0, jpDb, jHook); +} + + +S3JniApi(sqlite3_value_blob(),jbyteArray,1value_1blob)( + JniArgsEnvClass, jlong jpSVal +){ + sqlite3_value * const sv = LongPtrGet_sqlite3_value(jpSVal); + const jbyte * pBytes = sv ? sqlite3_value_blob(sv) : 0; + int const nLen = pBytes ? sqlite3_value_bytes(sv) : 0; + + s3jni_oom_check( nLen ? !!pBytes : 1 ); + return pBytes + ? s3jni_new_jbyteArray(pBytes, nLen) + : NULL; +} + +S3JniApi(sqlite3_value_bytes(),jint,1value_1bytes)( + JniArgsEnvClass, jlong jpSVal +){ + sqlite3_value * const sv = LongPtrGet_sqlite3_value(jpSVal); + return sv ? sqlite3_value_bytes(sv) : 0; +} + +S3JniApi(sqlite3_value_bytes16(),jint,1value_1bytes16)( + JniArgsEnvClass, jlong jpSVal +){ + sqlite3_value * const sv = LongPtrGet_sqlite3_value(jpSVal); + return sv ? sqlite3_value_bytes16(sv) : 0; +} + + +S3JniApi(sqlite3_value_double(),jdouble,1value_1double)( + JniArgsEnvClass, jlong jpSVal +){ + sqlite3_value * const sv = LongPtrGet_sqlite3_value(jpSVal); + return (jdouble) (sv ? sqlite3_value_double(sv) : 0.0); +} + + +S3JniApi(sqlite3_value_dup(),jobject,1value_1dup)( + JniArgsEnvClass, jlong jpSVal +){ + sqlite3_value * const sv = LongPtrGet_sqlite3_value(jpSVal); + sqlite3_value * const sd = sv ? sqlite3_value_dup(sv) : 0; + jobject rv = sd ? new_java_sqlite3_value(env, sd) : 0; + if( sd && !rv ) { + /* OOM */ + sqlite3_value_free(sd); + } + return rv; +} + +S3JniApi(sqlite3_value_free(),void,1value_1free)( + JniArgsEnvClass, jlong jpSVal +){ + sqlite3_value * const sv = LongPtrGet_sqlite3_value(jpSVal); + if( sv ){ + sqlite3_value_free(sv); + } +} + +S3JniApi(sqlite3_value_int(),jint,1value_1int)( + JniArgsEnvClass, jlong jpSVal +){ + sqlite3_value * const sv = LongPtrGet_sqlite3_value(jpSVal); + return (jint) (sv ? sqlite3_value_int(sv) : 0); +} + +S3JniApi(sqlite3_value_int64(),jlong,1value_1int64)( + JniArgsEnvClass, jlong jpSVal +){ + sqlite3_value * const sv = LongPtrGet_sqlite3_value(jpSVal); + return (jlong) (sv ? sqlite3_value_int64(sv) : 0LL); +} + +S3JniApi(sqlite3_value_java_object(),jobject,1value_1java_1object)( + JniArgsEnvClass, jlong jpSVal +){ + sqlite3_value * const sv = LongPtrGet_sqlite3_value(jpSVal); + return sv + ? sqlite3_value_pointer(sv, s3jni__value_jref_key) + : 0; +} + +S3JniApi(sqlite3_value_nio_buffer(),jobject,1value_1nio_1buffer)( + JniArgsEnvClass, jobject jVal +){ + sqlite3_value * const sv = PtrGet_sqlite3_value(jVal); + jobject rv = 0; + if( sv ){ + const void * const p = sqlite3_value_blob(sv); + if( p ){ + const int n = sqlite3_value_bytes(sv); + rv = s3jni__blob_to_ByteBuffer(env, p, n); + } + } + return rv; +} + +S3JniApi(sqlite3_value_text(),jbyteArray,1value_1text)( + JniArgsEnvClass, jlong jpSVal +){ + sqlite3_value * const sv = LongPtrGet_sqlite3_value(jpSVal); + const unsigned char * const p = sv ? sqlite3_value_text(sv) : 0; + int const n = p ? sqlite3_value_bytes(sv) : 0; + return p ? s3jni_new_jbyteArray(p, n) : 0; +} + +#if 0 +// this impl might prove useful. +S3JniApi(sqlite3_value_text(),jstring,1value_1text)( + JniArgsEnvClass, jlong jpSVal +){ + sqlite3_value * const sv = LongPtrGet_sqlite3_value(jpSVal); + const unsigned char * const p = sv ? sqlite3_value_text(sv) : 0; + int const n = p ? sqlite3_value_bytes(sv) : 0; + return p ? s3jni_utf8_to_jstring( (const char *)p, n) : 0; +} +#endif + +S3JniApi(sqlite3_value_text16(),jstring,1value_1text16)( + JniArgsEnvClass, jlong jpSVal +){ + sqlite3_value * const sv = LongPtrGet_sqlite3_value(jpSVal); + const int n = sv ? sqlite3_value_bytes16(sv) : 0; + const void * const p = sv ? sqlite3_value_text16(sv) : 0; + return p ? s3jni_text16_to_jstring(env, p, n) : 0; +} + +JniDecl(void,1jni_1internal_1details)(JniArgsEnvClass){ + MARKER(("\nVarious bits of internal info:\n")); + puts("FTS5 is " +#ifdef SQLITE_ENABLE_FTS5 + "available" +#else + "unavailable" +#endif + "." + ); + puts("sizeofs:"); +#define SO(T) printf("\tsizeof(" #T ") = %u\n", (unsigned)sizeof(T)) + SO(void*); + SO(jmethodID); + SO(jfieldID); + SO(S3JniEnv); + SO(S3JniHook); + SO(S3JniDb); + SO(S3JniNphOps); + printf("\t(^^^ %u NativePointerHolder/OutputPointer.T types)\n", + (unsigned)S3Jni_NphCache_size); + SO(S3JniGlobal); + SO(S3JniGlobal.nph); + SO(S3JniGlobal.metrics); + SO(S3JniAutoExtension); + SO(S3JniUdf); +#undef SO +#ifdef SQLITE_JNI_ENABLE_METRICS + printf("Cache info:\n"); + printf("\tJNIEnv cache: %u allocs, %u misses, %u hits\n", + SJG.metrics.nEnvAlloc, SJG.metrics.nEnvMiss, + SJG.metrics.nEnvHit); + printf("Mutex entry:" + "\n\tglobal = %u" + "\n\tenv = %u" + "\n\tnph = %u for S3JniNphOp init" + "\n\thook = %u" + "\n\tperDb = %u" + "\n\tautoExt list = %u" + "\n\tS3JniUdf = %u (free-list)" + "\n\tmetrics = %u\n", + SJG.metrics.nMutexGlobal, SJG.metrics.nMutexEnv, + SJG.metrics.nMutexNph, SJG.metrics.nMutexHook, + SJG.metrics.nMutexPerDb, SJG.metrics.nMutexAutoExt, + SJG.metrics.nMutexUdf, SJG.metrics.nMetrics); + puts("Allocs:"); + printf("\tS3JniDb: %u alloced (*%u = %u bytes), %u recycled\n", + SJG.metrics.nPdbAlloc, (unsigned) sizeof(S3JniDb), + (unsigned)(SJG.metrics.nPdbAlloc * sizeof(S3JniDb)), + SJG.metrics.nPdbRecycled); + printf("\tS3JniUdf: %u alloced (*%u = %u bytes), %u recycled\n", + SJG.metrics.nUdfAlloc, (unsigned) sizeof(S3JniUdf), + (unsigned)(SJG.metrics.nUdfAlloc * sizeof(S3JniUdf)), + SJG.metrics.nUdfRecycled); + printf("\tS3JniHook: %u alloced (*%u = %u bytes), %u recycled\n", + SJG.metrics.nHookAlloc, (unsigned) sizeof(S3JniHook), + (unsigned)(SJG.metrics.nHookAlloc * sizeof(S3JniHook)), + SJG.metrics.nHookRecycled); + printf("\tS3JniEnv: %u alloced (*%u = %u bytes)\n", + SJG.metrics.nEnvAlloc, (unsigned) sizeof(S3JniEnv), + (unsigned)(SJG.metrics.nEnvAlloc * sizeof(S3JniEnv))); + puts("Java-side UDF calls:"); +#define UDF(T) printf("\t%-8s = %u\n", "x" #T, SJG.metrics.udf.n##T) + UDF(Func); UDF(Step); UDF(Final); UDF(Value); UDF(Inverse); +#undef UDF + printf("xDestroy calls across all callback types: %u\n", + SJG.metrics.nDestroy); +#else + puts("Built without SQLITE_JNI_ENABLE_METRICS."); +#endif +} + +//////////////////////////////////////////////////////////////////////// +// End of the sqlite3_... API bindings. Next up, FTS5... +//////////////////////////////////////////////////////////////////////// +#ifdef SQLITE_ENABLE_FTS5 + +/* Creates a verbose JNI Fts5 function name. */ +#define JniFuncNameFtsXA(Suffix) \ + Java_org_sqlite_jni_fts5_Fts5ExtensionApi_ ## Suffix +#define JniFuncNameFtsApi(Suffix) \ + Java_org_sqlite_jni_fts5_fts5_1api_ ## Suffix +#define JniFuncNameFtsTok(Suffix) \ + Java_org_sqlite_jni_fts5_fts5_tokenizer_ ## Suffix + +#define JniDeclFtsXA(ReturnType,Suffix) \ + JNIEXPORT ReturnType JNICALL \ + JniFuncNameFtsXA(Suffix) +#define JniDeclFtsApi(ReturnType,Suffix) \ + JNIEXPORT ReturnType JNICALL \ + JniFuncNameFtsApi(Suffix) +#define JniDeclFtsTok(ReturnType,Suffix) \ + JNIEXPORT ReturnType JNICALL \ + JniFuncNameFtsTok(Suffix) + +#define PtrGet_fts5_api(OBJ) NativePointerHolder_get(OBJ,S3JniNph(fts5_api)) +#define PtrGet_fts5_tokenizer(OBJ) NativePointerHolder_get(OBJ,S3JniNph(fts5_tokenizer)) +#define PtrGet_Fts5Context(OBJ) NativePointerHolder_get(OBJ,S3JniNph(Fts5Context)) +#define PtrGet_Fts5Tokenizer(OBJ) NativePointerHolder_get(OBJ,S3JniNph(Fts5Tokenizer)) +#define s3jni_ftsext() &sFts5Api/*singleton from sqlite3.c*/ +#define Fts5ExtDecl Fts5ExtensionApi const * const ext = s3jni_ftsext() + +/** + State for binding Java-side FTS5 auxiliary functions. +*/ +typedef struct { + jobject jObj /* functor instance */; + jobject jUserData /* 2nd arg to JNI binding of + xCreateFunction(), ostensibly the 3rd arg + to the lib-level xCreateFunction(), except + that we necessarily use that slot for a + Fts5JniAux instance. */; + char * zFuncName /* Only for error reporting and debug logging */; + jmethodID jmid /* callback member's method ID */; +} Fts5JniAux; + +static void Fts5JniAux_free(Fts5JniAux * const s){ + S3JniDeclLocal_env; + if( env ){ + /*MARKER(("FTS5 aux function cleanup: %s\n", s->zFuncName));*/ + s3jni_call_xDestroy(s->jObj); + S3JniUnrefGlobal(s->jObj); + S3JniUnrefGlobal(s->jUserData); + } + sqlite3_free(s->zFuncName); + sqlite3_free(s); +} + +static void Fts5JniAux_xDestroy(void *p){ + if( p ) Fts5JniAux_free(p); +} + +static Fts5JniAux * Fts5JniAux_alloc(JNIEnv * const env, jobject jObj){ + Fts5JniAux * s = s3jni_malloc( sizeof(Fts5JniAux)); + + if( s ){ + jclass klazz; + memset(s, 0, sizeof(Fts5JniAux)); + s->jObj = S3JniRefGlobal(jObj); + klazz = (*env)->GetObjectClass(env, jObj); + s->jmid = (*env)->GetMethodID(env, klazz, "call", + "(Lorg/sqlite/jni/fts5/Fts5ExtensionApi;" + "Lorg/sqlite/jni/fts5/Fts5Context;" + "Lorg/sqlite/jni/capi/sqlite3_context;" + "[Lorg/sqlite/jni/capi/sqlite3_value;)V"); + S3JniUnrefLocal(klazz); + S3JniIfThrew{ + S3JniExceptionReport; + S3JniExceptionClear; + Fts5JniAux_free(s); + s = 0; + } + } + return s; +} + +static inline jobject new_java_Fts5Context(JNIEnv * const env, Fts5Context *sv){ + return NativePointerHolder_new(env, S3JniNph(Fts5Context), sv); +} +static inline jobject new_java_fts5_api(JNIEnv * const env, fts5_api *sv){ + return NativePointerHolder_new(env, S3JniNph(fts5_api), sv); +} + +/* +** Returns a per-JNIEnv global ref to the Fts5ExtensionApi singleton +** instance, or NULL on OOM. +*/ +static jobject s3jni_getFts5ExtensionApi(JNIEnv * const env){ + if( !SJG.fts5.jExt ){ + S3JniGlobal_mutex_enter; + if( !SJG.fts5.jExt ){ + jobject const pNPH = NativePointerHolder_new( + env, S3JniNph(Fts5ExtensionApi), s3jni_ftsext() + ); + if( pNPH ){ + SJG.fts5.jExt = S3JniRefGlobal(pNPH); + S3JniUnrefLocal(pNPH); + } + } + S3JniGlobal_mutex_leave; + } + return SJG.fts5.jExt; +} + +/* +** Returns a pointer to the fts5_api instance for database connection +** db. If an error occurs, returns NULL and leaves an error in the +** database handle (accessible using sqlite3_errcode()/errmsg()). +*/ +static fts5_api *s3jni_fts5_api_from_db(sqlite3 *db){ + fts5_api *pRet = 0; + sqlite3_stmt *pStmt = 0; + if( SQLITE_OK==sqlite3_prepare(db, "SELECT fts5(?1)", -1, &pStmt, 0) ){ + sqlite3_bind_pointer(pStmt, 1, (void*)&pRet, "fts5_api_ptr", NULL); + sqlite3_step(pStmt); + } + sqlite3_finalize(pStmt); + return pRet; +} + +JniDeclFtsApi(jobject,getInstanceForDb)(JniArgsEnvClass,jobject jDb){ + S3JniDb * const ps = S3JniDb_from_java(jDb); +#if 0 + jobject rv = 0; + if( !ps ) return 0; + else if( ps->fts.jApi ){ + rv = ps->fts.jApi; + }else{ + fts5_api * const pApi = s3jni_fts5_api_from_db(ps->pDb); + if( pApi ){ + rv = new_java_fts5_api(env, pApi); + ps->fts.jApi = rv ? S3JniRefGlobal(rv) : 0; + } + } + return rv; +#else + if( ps && !ps->fts.jApi ){ + S3JniDb_mutex_enter; + if( !ps->fts.jApi ){ + fts5_api * const pApi = s3jni_fts5_api_from_db(ps->pDb); + if( pApi ){ + jobject const rv = new_java_fts5_api(env, pApi); + ps->fts.jApi = rv ? S3JniRefGlobal(rv) : 0; + } + } + S3JniDb_mutex_leave; + } + return ps ? ps->fts.jApi : 0; +#endif +} + + +JniDeclFtsXA(jobject,getInstance)(JniArgsEnvClass){ + return s3jni_getFts5ExtensionApi(env); +} + +JniDeclFtsXA(jint,xColumnCount)(JniArgsEnvObj,jobject jCtx){ + Fts5ExtDecl; + return (jint)ext->xColumnCount(PtrGet_Fts5Context(jCtx)); +} + +JniDeclFtsXA(jint,xColumnSize)(JniArgsEnvObj,jobject jCtx, jint iIdx, jobject jOut32){ + Fts5ExtDecl; + int n1 = 0; + int const rc = ext->xColumnSize(PtrGet_Fts5Context(jCtx), (int)iIdx, &n1); + if( 0==rc ) OutputPointer_set_Int32(env, jOut32, n1); + return rc; +} + +JniDeclFtsXA(jint,xColumnText)(JniArgsEnvObj,jobject jCtx, jint iCol, + jobject jOut){ + Fts5ExtDecl; + const char *pz = 0; + int pn = 0; + int rc = ext->xColumnText(PtrGet_Fts5Context(jCtx), (int)iCol, + &pz, &pn); + if( 0==rc ){ + jstring jstr = pz ? s3jni_utf8_to_jstring( pz, pn) : 0; + if( pz ){ + if( jstr ){ + OutputPointer_set_String(env, jOut, jstr); + S3JniUnrefLocal(jstr)/*jOut has a reference*/; + }else{ + rc = SQLITE_NOMEM; + } + } + } + return (jint)rc; +} + +JniDeclFtsXA(jint,xColumnTotalSize)(JniArgsEnvObj,jobject jCtx, jint iCol, jobject jOut64){ + Fts5ExtDecl; + sqlite3_int64 nOut = 0; + int const rc = ext->xColumnTotalSize(PtrGet_Fts5Context(jCtx), (int)iCol, &nOut); + if( 0==rc && jOut64 ) OutputPointer_set_Int64(env, jOut64, (jlong)nOut); + return (jint)rc; +} + +/* +** Proxy for fts5_extension_function instances plugged in via +** fts5_api::xCreateFunction(). +*/ +static void s3jni_fts5_extension_function(Fts5ExtensionApi const *pApi, + Fts5Context *pFts, + sqlite3_context *pCx, + int argc, + sqlite3_value **argv){ + Fts5JniAux * const pAux = pApi->xUserData(pFts); + jobject jpCx = 0; + jobjectArray jArgv = 0; + jobject jpFts = 0; + jobject jFXA; + int rc; + S3JniDeclLocal_env; + + assert(pAux); + jFXA = s3jni_getFts5ExtensionApi(env); + if( !jFXA ) goto error_oom; + jpFts = new_java_Fts5Context(env, pFts); + if( !jpFts ) goto error_oom; + rc = udf_args(env, pCx, argc, argv, &jpCx, &jArgv); + if( rc ) goto error_oom; + (*env)->CallVoidMethod(env, pAux->jObj, pAux->jmid, + jFXA, jpFts, jpCx, jArgv); + S3JniIfThrew{ + udf_report_exception(env, 1, pCx, pAux->zFuncName, "call"); + } + udf_unargs(env, jpCx, argc, jArgv); + S3JniUnrefLocal(jpFts); + S3JniUnrefLocal(jpCx); + S3JniUnrefLocal(jArgv); + return; +error_oom: + s3jni_db_oom( sqlite3_context_db_handle(pCx) ); + assert( !jArgv ); + assert( !jpCx ); + S3JniUnrefLocal(jpFts); + sqlite3_result_error_nomem(pCx); + return; +} + +JniDeclFtsApi(jint,xCreateFunction)(JniArgsEnvObj, jstring jName, + jobject jUserData, jobject jFunc){ + fts5_api * const pApi = PtrGet_fts5_api(jSelf); + int rc; + char * zName; + Fts5JniAux * pAux; + + assert(pApi); + zName = s3jni_jstring_to_utf8( jName, 0); + if(!zName) return SQLITE_NOMEM; + pAux = Fts5JniAux_alloc(env, jFunc); + if( pAux ){ + rc = pApi->xCreateFunction(pApi, zName, pAux, + s3jni_fts5_extension_function, + Fts5JniAux_xDestroy); + }else{ + rc = SQLITE_NOMEM; + } + if( 0==rc ){ + pAux->jUserData = jUserData ? S3JniRefGlobal(jUserData) : 0; + pAux->zFuncName = zName; + }else{ + sqlite3_free(zName); + } + return (jint)rc; +} + + +typedef struct S3JniFts5AuxData S3JniFts5AuxData; +/* +** TODO: this middle-man struct is no longer necessary. Consider +** removing it and passing around jObj itself instead. +*/ +struct S3JniFts5AuxData { + jobject jObj; +}; + +static void S3JniFts5AuxData_xDestroy(void *x){ + if( x ){ + S3JniFts5AuxData * const p = x; + if( p->jObj ){ + S3JniDeclLocal_env; + s3jni_call_xDestroy(p->jObj); + S3JniUnrefGlobal(p->jObj); + } + sqlite3_free(x); + } +} + +JniDeclFtsXA(jobject,xGetAuxdata)(JniArgsEnvObj,jobject jCtx, jboolean bClear){ + Fts5ExtDecl; + jobject rv = 0; + S3JniFts5AuxData * const pAux = ext->xGetAuxdata(PtrGet_Fts5Context(jCtx), bClear); + if( pAux ){ + if( bClear ){ + if( pAux->jObj ){ + rv = S3JniRefLocal(pAux->jObj); + S3JniUnrefGlobal(pAux->jObj); + } + /* Note that we do not call xDestroy() in this case. */ + sqlite3_free(pAux); + }else{ + rv = pAux->jObj; + } + } + return rv; +} + +JniDeclFtsXA(jint,xInst)(JniArgsEnvObj,jobject jCtx, jint iIdx, jobject jOutPhrase, + jobject jOutCol, jobject jOutOff){ + Fts5ExtDecl; + int n1 = 0, n2 = 2, n3 = 0; + int const rc = ext->xInst(PtrGet_Fts5Context(jCtx), (int)iIdx, &n1, &n2, &n3); + if( 0==rc ){ + OutputPointer_set_Int32(env, jOutPhrase, n1); + OutputPointer_set_Int32(env, jOutCol, n2); + OutputPointer_set_Int32(env, jOutOff, n3); + } + return rc; +} + +JniDeclFtsXA(jint,xInstCount)(JniArgsEnvObj,jobject jCtx, jobject jOut32){ + Fts5ExtDecl; + int nOut = 0; + int const rc = ext->xInstCount(PtrGet_Fts5Context(jCtx), &nOut); + if( 0==rc && jOut32 ) OutputPointer_set_Int32(env, jOut32, nOut); + return (jint)rc; +} + +JniDeclFtsXA(jint,xPhraseCount)(JniArgsEnvObj,jobject jCtx){ + Fts5ExtDecl; + return (jint)ext->xPhraseCount(PtrGet_Fts5Context(jCtx)); +} + +/* Copy the 'a' and 'b' fields from pSrc to Fts5PhraseIter object jIter. */ +static void s3jni_phraseIter_NToJ(JNIEnv *const env, + Fts5PhraseIter const * const pSrc, + jobject jIter){ + S3JniGlobalType * const g = &S3JniGlobal; + assert(g->fts5.jPhraseIter.fidA); + (*env)->SetLongField(env, jIter, g->fts5.jPhraseIter.fidA, + S3JniCast_P2L(pSrc->a)); + S3JniExceptionIsFatal("Cannot set Fts5PhraseIter.a field."); + (*env)->SetLongField(env, jIter, g->fts5.jPhraseIter.fidB, + S3JniCast_P2L(pSrc->b)); + S3JniExceptionIsFatal("Cannot set Fts5PhraseIter.b field."); +} + +/* Copy the 'a' and 'b' fields from Fts5PhraseIter object jIter to pDest. */ +static void s3jni_phraseIter_JToN(JNIEnv *const env, jobject jIter, + Fts5PhraseIter * const pDest){ + S3JniGlobalType * const g = &S3JniGlobal; + assert(g->fts5.jPhraseIter.fidA); + pDest->a = S3JniCast_L2P( + (*env)->GetLongField(env, jIter, g->fts5.jPhraseIter.fidA) + ); + S3JniExceptionIsFatal("Cannot get Fts5PhraseIter.a field."); + pDest->b = S3JniCast_L2P( + (*env)->GetLongField(env, jIter, g->fts5.jPhraseIter.fidB) + ); + S3JniExceptionIsFatal("Cannot get Fts5PhraseIter.b field."); +} + +JniDeclFtsXA(jint,xPhraseFirst)(JniArgsEnvObj,jobject jCtx, jint iPhrase, + jobject jIter, jobject jOutCol, + jobject jOutOff){ + Fts5ExtDecl; + Fts5PhraseIter iter; + int rc, iCol = 0, iOff = 0; + rc = ext->xPhraseFirst(PtrGet_Fts5Context(jCtx), (int)iPhrase, + &iter, &iCol, &iOff); + if( 0==rc ){ + OutputPointer_set_Int32(env, jOutCol, iCol); + OutputPointer_set_Int32(env, jOutOff, iOff); + s3jni_phraseIter_NToJ(env, &iter, jIter); + } + return rc; +} + +JniDeclFtsXA(jint,xPhraseFirstColumn)(JniArgsEnvObj,jobject jCtx, jint iPhrase, + jobject jIter, jobject jOutCol){ + Fts5ExtDecl; + Fts5PhraseIter iter; + int rc, iCol = 0; + rc = ext->xPhraseFirstColumn(PtrGet_Fts5Context(jCtx), (int)iPhrase, + &iter, &iCol); + if( 0==rc ){ + OutputPointer_set_Int32(env, jOutCol, iCol); + s3jni_phraseIter_NToJ(env, &iter, jIter); + } + return rc; +} + +JniDeclFtsXA(void,xPhraseNext)(JniArgsEnvObj,jobject jCtx, jobject jIter, + jobject jOutCol, jobject jOutOff){ + Fts5ExtDecl; + Fts5PhraseIter iter; + int iCol = 0, iOff = 0; + s3jni_phraseIter_JToN(env, jIter, &iter); + ext->xPhraseNext(PtrGet_Fts5Context(jCtx), &iter, &iCol, &iOff); + OutputPointer_set_Int32(env, jOutCol, iCol); + OutputPointer_set_Int32(env, jOutOff, iOff); + s3jni_phraseIter_NToJ(env, &iter, jIter); +} + +JniDeclFtsXA(void,xPhraseNextColumn)(JniArgsEnvObj,jobject jCtx, jobject jIter, + jobject jOutCol){ + Fts5ExtDecl; + Fts5PhraseIter iter; + int iCol = 0; + s3jni_phraseIter_JToN(env, jIter, &iter); + ext->xPhraseNextColumn(PtrGet_Fts5Context(jCtx), &iter, &iCol); + OutputPointer_set_Int32(env, jOutCol, iCol); + s3jni_phraseIter_NToJ(env, &iter, jIter); +} + + +JniDeclFtsXA(jint,xPhraseSize)(JniArgsEnvObj,jobject jCtx, jint iPhrase){ + Fts5ExtDecl; + return (jint)ext->xPhraseSize(PtrGet_Fts5Context(jCtx), (int)iPhrase); +} + +/* State for use with xQueryPhrase() and xTokenize(). */ +struct s3jni_xQueryPhraseState { + Fts5ExtensionApi const * ext; + jmethodID midCallback; /* jCallback->call() method */ + jobject jCallback; /* Fts5ExtensionApi.XQueryPhraseCallback instance */ + jobject jFcx; /* (Fts5Context*) for xQueryPhrase() + callback. This is NOT the instance that is + passed to xQueryPhrase(), it's the one + created by xQueryPhrase() for use by its + callback. */ + /* State for xTokenize() */ + struct { + const char * zPrev; + int nPrev; + jbyteArray jba; + } tok; +}; + +static int s3jni_xQueryPhrase(const Fts5ExtensionApi *xapi, + Fts5Context * pFcx, void *pData){ + struct s3jni_xQueryPhraseState * const s = pData; + S3JniDeclLocal_env; + + if( !s->jFcx ){ + s->jFcx = new_java_Fts5Context(env, pFcx); + if( !s->jFcx ) return SQLITE_NOMEM; + } + int rc = (int)(*env)->CallIntMethod(env, s->jCallback, s->midCallback, + SJG.fts5.jExt, s->jFcx); + S3JniIfThrew{ + S3JniExceptionWarnCallbackThrew("xQueryPhrase() callback"); + S3JniExceptionClear; + rc = SQLITE_ERROR; + } + return rc; +} + +JniDeclFtsXA(jint,xQueryPhrase)(JniArgsEnvObj,jobject jFcx, jint iPhrase, + jobject jCallback){ + Fts5ExtDecl; + int rc; + struct s3jni_xQueryPhraseState s; + jclass klazz = jCallback ? (*env)->GetObjectClass(env, jCallback) : NULL; + + if( !klazz ) return SQLITE_MISUSE; + s.jCallback = jCallback; + s.jFcx = 0; + s.ext = ext; + s.midCallback = (*env)->GetMethodID(env, klazz, "call", + "(Lorg/sqlite/jni/fts5/Fts5ExtensionApi;" + "Lorg/sqlite/jni/fts5/Fts5Context;)I"); + S3JniUnrefLocal(klazz); + S3JniExceptionIsFatal("Could not extract xQueryPhraseCallback.call() method."); + rc = ext->xQueryPhrase(PtrGet_Fts5Context(jFcx), iPhrase, &s, + s3jni_xQueryPhrase); + S3JniUnrefLocal(s.jFcx); + return (jint)rc; +} + + +JniDeclFtsXA(jint,xRowCount)(JniArgsEnvObj,jobject jCtx, jobject jOut64){ + Fts5ExtDecl; + sqlite3_int64 nOut = 0; + int const rc = ext->xRowCount(PtrGet_Fts5Context(jCtx), &nOut); + if( 0==rc && jOut64 ) OutputPointer_set_Int64(env, jOut64, (jlong)nOut); + return (jint)rc; +} + +JniDeclFtsXA(jlong,xRowid)(JniArgsEnvObj,jobject jCtx){ + Fts5ExtDecl; + return (jlong)ext->xRowid(PtrGet_Fts5Context(jCtx)); +} + +JniDeclFtsXA(jint,xSetAuxdata)(JniArgsEnvObj,jobject jCtx, jobject jAux){ + Fts5ExtDecl; + int rc; + S3JniFts5AuxData * pAux; + + pAux = s3jni_malloc( sizeof(*pAux)); + if( !pAux ){ + if( jAux ){ + /* Emulate how xSetAuxdata() behaves when it cannot alloc + ** its auxdata wrapper. */ + s3jni_call_xDestroy(jAux); + } + return SQLITE_NOMEM; + } + pAux->jObj = S3JniRefGlobal(jAux); + rc = ext->xSetAuxdata(PtrGet_Fts5Context(jCtx), pAux, + S3JniFts5AuxData_xDestroy); + return rc; +} + +/* xToken() impl for xTokenize(). */ +static int s3jni_xTokenize_xToken(void *p, int tFlags, const char* z, + int nZ, int iStart, int iEnd){ + int rc; + S3JniDeclLocal_env; + struct s3jni_xQueryPhraseState * const s = p; + jbyteArray jba; + + S3JniUnrefLocal(s->tok.jba); + s->tok.zPrev = z; + s->tok.nPrev = nZ; + s->tok.jba = s3jni_new_jbyteArray(z, nZ); + if( !s->tok.jba ) return SQLITE_NOMEM; + jba = s->tok.jba; + rc = (int)(*env)->CallIntMethod(env, s->jCallback, s->midCallback, + (jint)tFlags, jba, (jint)iStart, + (jint)iEnd); + S3JniIfThrew { + S3JniExceptionWarnCallbackThrew("xTokenize() callback"); + rc = SQLITE_ERROR; + } + return rc; +} + +/* +** Proxy for Fts5ExtensionApi.xTokenize() and +** fts5_tokenizer.xTokenize() +*/ +static jint s3jni_fts5_xTokenize(JniArgsEnvObj, S3JniNphOp const *pRef, + jint tokFlags, jobject jFcx, + jbyteArray jbaText, jobject jCallback){ + Fts5ExtDecl; + struct s3jni_xQueryPhraseState s; + int rc = 0; + jbyte * const pText = jCallback ? s3jni_jbyteArray_bytes(jbaText) : 0; + jsize nText = pText ? (*env)->GetArrayLength(env, jbaText) : 0; + jclass const klazz = jCallback ? (*env)->GetObjectClass(env, jCallback) : NULL; + + if( !klazz ) return SQLITE_MISUSE; + memset(&s, 0, sizeof(s)); + s.jCallback = jCallback; + s.jFcx = jFcx; + s.ext = ext; + s.midCallback = (*env)->GetMethodID(env, klazz, "call", "(I[BII)I"); + S3JniUnrefLocal(klazz); + S3JniIfThrew { + S3JniExceptionReport; + S3JniExceptionClear; + s3jni_jbyteArray_release(jbaText, pText); + return SQLITE_ERROR; + } + s.tok.jba = S3JniRefLocal(jbaText); + s.tok.zPrev = (const char *)pText; + s.tok.nPrev = (int)nText; + if( pRef == S3JniNph(Fts5ExtensionApi) ){ + rc = ext->xTokenize(PtrGet_Fts5Context(jFcx), + (const char *)pText, (int)nText, + &s, s3jni_xTokenize_xToken); + }else if( pRef == S3JniNph(fts5_tokenizer) ){ + fts5_tokenizer * const pTok = PtrGet_fts5_tokenizer(jSelf); + rc = pTok->xTokenize(PtrGet_Fts5Tokenizer(jFcx), &s, tokFlags, + (const char *)pText, (int)nText, + s3jni_xTokenize_xToken); + }else{ + (*env)->FatalError(env, "This cannot happen. Maintenance required."); + } + if( s.tok.jba ){ + assert( s.tok.zPrev ); + S3JniUnrefLocal(s.tok.jba); + } + s3jni_jbyteArray_release(jbaText, pText); + return (jint)rc; +} + +JniDeclFtsXA(jint,xTokenize)(JniArgsEnvObj,jobject jFcx, jbyteArray jbaText, + jobject jCallback){ + return s3jni_fts5_xTokenize(env, jSelf, S3JniNph(Fts5ExtensionApi), + 0, jFcx, jbaText, jCallback); +} + +JniDeclFtsTok(jint,xTokenize)(JniArgsEnvObj,jobject jFcx, jint tokFlags, + jbyteArray jbaText, jobject jCallback){ + return s3jni_fts5_xTokenize(env, jSelf, S3JniNph(Fts5Tokenizer), + tokFlags, jFcx, jbaText, jCallback); +} + + +JniDeclFtsXA(jobject,xUserData)(JniArgsEnvObj,jobject jFcx){ + Fts5ExtDecl; + Fts5JniAux * const pAux = ext->xUserData(PtrGet_Fts5Context(jFcx)); + return pAux ? pAux->jUserData : 0; +} + +#endif /* SQLITE_ENABLE_FTS5 */ + +//////////////////////////////////////////////////////////////////////// +// End of the main API bindings. Start of SQLTester bits... +//////////////////////////////////////////////////////////////////////// + +#ifdef SQLITE_JNI_ENABLE_SQLTester +typedef struct SQLTesterJni SQLTesterJni; +struct SQLTesterJni { + sqlite3_int64 nDup; +}; +static SQLTesterJni SQLTester = { + 0 +}; + +static void SQLTester_dup_destructor(void*pToFree){ + u64 *p = (u64*)pToFree; + assert( p!=0 ); + p--; + assert( p[0]==0x2bbf4b7c ); + p[0] = 0; + p[1] = 0; + sqlite3_free(p); +} + +/* +** Implementation of +** +** dup(TEXT) +** +** This SQL function simply makes a copy of its text argument. But it +** returns the result using a custom destructor, in order to provide +** tests for the use of Mem.xDel() in the SQLite VDBE. +*/ +static void SQLTester_dup_func( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + u64 *pOut; + char *z; + int n = sqlite3_value_bytes(argv[0]); + SQLTesterJni * const p = (SQLTesterJni *)sqlite3_user_data(context); + S3JniDeclLocal_env; + + ++p->nDup; + if( n>0 && (pOut = s3jni_malloc( (n+16)&~7 ))!=0 ){ + pOut[0] = 0x2bbf4b7c; + z = (char*)&pOut[1]; + memcpy(z, sqlite3_value_text(argv[0]), n); + z[n] = 0; + sqlite3_result_text(context, z, n, SQLTester_dup_destructor); + } + return; +} + +/* +** Return the number of calls to the dup() SQL function since the +** SQLTester context was opened or since the last dup_count() call. +*/ +static void SQLTester_dup_count_func( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + SQLTesterJni * const p = (SQLTesterJni *)sqlite3_user_data(context); + sqlite3_result_int64(context, p->nDup); + p->nDup = 0; +} + +/* +** Return non-zero if string z matches glob pattern zGlob and zero if the +** pattern does not match. +** +** To repeat: +** +** zero == no match +** non-zero == match +** +** Globbing rules: +** +** '*' Matches any sequence of zero or more characters. +** +** '?' Matches exactly one character. +** +** [...] Matches one character from the enclosed list of +** characters. +** +** [^...] Matches one character not in the enclosed list. +** +** '#' Matches any sequence of one or more digits with an +** optional + or - sign in front, or a hexadecimal +** literal of the form 0x... +*/ +static int SQLTester_strnotglob(const char *zGlob, const char *z){ + int c, c2; + int invert; + int seen; + + while( (c = (*(zGlob++)))!=0 ){ + if( c=='*' ){ + while( (c=(*(zGlob++))) == '*' || c=='?' ){ + if( c=='?' && (*(z++))==0 ) return 0; + } + if( c==0 ){ + return 1; + }else if( c=='[' ){ + while( *z && SQLTester_strnotglob(zGlob-1,z)==0 ){ + z++; + } + return (*z)!=0; + } + while( (c2 = (*(z++)))!=0 ){ + while( c2!=c ){ + c2 = *(z++); + if( c2==0 ) return 0; + } + if( SQLTester_strnotglob(zGlob,z) ) return 1; + } + return 0; + }else if( c=='?' ){ + if( (*(z++))==0 ) return 0; + }else if( c=='[' ){ + int prior_c = 0; + seen = 0; + invert = 0; + c = *(z++); + if( c==0 ) return 0; + c2 = *(zGlob++); + if( c2=='^' ){ + invert = 1; + c2 = *(zGlob++); + } + if( c2==']' ){ + if( c==']' ) seen = 1; + c2 = *(zGlob++); + } + while( c2 && c2!=']' ){ + if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){ + c2 = *(zGlob++); + if( c>=prior_c && c<=c2 ) seen = 1; + prior_c = 0; + }else{ + if( c==c2 ){ + seen = 1; + } + prior_c = c2; + } + c2 = *(zGlob++); + } + if( c2==0 || (seen ^ invert)==0 ) return 0; + }else if( c=='#' ){ + if( z[0]=='0' + && (z[1]=='x' || z[1]=='X') + && sqlite3Isxdigit(z[2]) + ){ + z += 3; + while( sqlite3Isxdigit(z[0]) ){ z++; } + }else{ + if( (z[0]=='-' || z[0]=='+') && sqlite3Isdigit(z[1]) ) z++; + if( !sqlite3Isdigit(z[0]) ) return 0; + z++; + while( sqlite3Isdigit(z[0]) ){ z++; } + } + }else{ + if( c!=(*(z++)) ) return 0; + } + } + return *z==0; +} + +JNIEXPORT jint JNICALL +Java_org_sqlite_jni_capi_SQLTester_strglob( + JniArgsEnvClass, jbyteArray baG, jbyteArray baT +){ + int rc = 0; + jbyte * const pG = s3jni_jbyteArray_bytes(baG); + jbyte * const pT = pG ? s3jni_jbyteArray_bytes(baT) : 0; + + s3jni_oom_fatal(pT); + /* Note that we're relying on the byte arrays having been + NUL-terminated on the Java side. */ + rc = !SQLTester_strnotglob((const char *)pG, (const char *)pT); + s3jni_jbyteArray_release(baG, pG); + s3jni_jbyteArray_release(baT, pT); + return rc; +} + + +static int SQLTester_auto_extension(sqlite3 *pDb, const char **pzErr, + const struct sqlite3_api_routines *ignored){ + sqlite3_create_function(pDb, "dup", 1, SQLITE_UTF8, &SQLTester, + SQLTester_dup_func, 0, 0); + sqlite3_create_function(pDb, "dup_count", 0, SQLITE_UTF8, &SQLTester, + SQLTester_dup_count_func, 0, 0); + return 0; +} + +JNIEXPORT void JNICALL +Java_org_sqlite_jni_capi_SQLTester_installCustomExtensions(JniArgsEnvClass){ + sqlite3_auto_extension( (void(*)(void))SQLTester_auto_extension ); +} + +#endif /* SQLITE_JNI_ENABLE_SQLTester */ +//////////////////////////////////////////////////////////////////////// +// End of SQLTester bindings. Start of lower-level bits. +//////////////////////////////////////////////////////////////////////// + +/* +** Called during static init of the CApi class to set up global +** state. +*/ +JNIEXPORT void JNICALL +Java_org_sqlite_jni_capi_CApi_init(JniArgsEnvClass){ + jclass klazz; + + memset(&S3JniGlobal, 0, sizeof(S3JniGlobal)); + if( (*env)->GetJavaVM(env, &SJG.jvm) ){ + (*env)->FatalError(env, "GetJavaVM() failure shouldn't be possible."); + return; + } + + /* Grab references to various global classes and objects... */ + SJG.g.cLong = S3JniRefGlobal((*env)->FindClass(env,"java/lang/Long")); + S3JniExceptionIsFatal("Error getting reference to Long class."); + SJG.g.ctorLong1 = (*env)->GetMethodID(env, SJG.g.cLong, + "", "(J)V"); + S3JniExceptionIsFatal("Error getting reference to Long constructor."); + + SJG.g.cString = S3JniRefGlobal((*env)->FindClass(env,"java/lang/String")); + S3JniExceptionIsFatal("Error getting reference to String class."); + SJG.g.ctorStringBA = + (*env)->GetMethodID(env, SJG.g.cString, + "", "([BLjava/nio/charset/Charset;)V"); + S3JniExceptionIsFatal("Error getting reference to String(byte[],Charset) ctor."); + SJG.g.stringGetBytes = + (*env)->GetMethodID(env, SJG.g.cString, + "getBytes", "(Ljava/nio/charset/Charset;)[B"); + S3JniExceptionIsFatal("Error getting reference to String.getBytes(Charset)."); + + { /* java.nio.charset.StandardCharsets.UTF_8 */ + jfieldID fUtf8; + klazz = (*env)->FindClass(env,"java/nio/charset/StandardCharsets"); + S3JniExceptionIsFatal("Error getting reference to StandardCharsets class."); + fUtf8 = (*env)->GetStaticFieldID(env, klazz, "UTF_8", + "Ljava/nio/charset/Charset;"); + S3JniExceptionIsFatal("Error getting StandardCharsets.UTF_8 field."); + SJG.g.oCharsetUtf8 = + S3JniRefGlobal((*env)->GetStaticObjectField(env, klazz, fUtf8)); + S3JniExceptionIsFatal("Error getting reference to StandardCharsets.UTF_8."); + S3JniUnrefLocal(klazz); + } + +#ifdef SQLITE_ENABLE_FTS5 + klazz = (*env)->FindClass(env, "org/sqlite/jni/fts5/Fts5PhraseIter"); + S3JniExceptionIsFatal("Error getting reference to org.sqlite.jni.fts5.Fts5PhraseIter."); + SJG.fts5.jPhraseIter.fidA = (*env)->GetFieldID(env, klazz, "a", "J"); + S3JniExceptionIsFatal("Cannot get Fts5PhraseIter.a field."); + SJG.fts5.jPhraseIter.fidB = (*env)->GetFieldID(env, klazz, "b", "J"); + S3JniExceptionIsFatal("Cannot get Fts5PhraseIter.b field."); + S3JniUnrefLocal(klazz); +#endif + + SJG.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); + s3jni_oom_fatal( SJG.mutex ); + SJG.hook.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); + s3jni_oom_fatal( SJG.hook.mutex ); + SJG.nph.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); + s3jni_oom_fatal( SJG.nph.mutex ); + SJG.envCache.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); + s3jni_oom_fatal( SJG.envCache.mutex ); + SJG.perDb.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); + s3jni_oom_fatal( SJG.perDb.mutex ); + SJG.autoExt.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); + s3jni_oom_fatal( SJG.autoExt.mutex ); + +#if S3JNI_METRICS_MUTEX + SJG.metrics.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); + s3jni_oom_fatal( SJG.metrics.mutex ); +#endif + + { + /* Test whether this JVM supports direct memory access via + ByteBuffer. */ + unsigned char buf[16] = {0}; + jobject bb = (*env)->NewDirectByteBuffer(env, buf, 16); + if( bb ){ + SJG.g.byteBuffer.klazz = S3JniRefGlobal((*env)->GetObjectClass(env, bb)); + SJG.g.byteBuffer.midAlloc = (*env)->GetStaticMethodID( + env, SJG.g.byteBuffer.klazz, "allocateDirect", "(I)Ljava/nio/ByteBuffer;" + ); + S3JniExceptionIsFatal("Error getting ByteBuffer.allocateDirect() method."); + SJG.g.byteBuffer.midLimit = (*env)->GetMethodID( + env, SJG.g.byteBuffer.klazz, "limit", "()I" + ); + S3JniExceptionIsFatal("Error getting ByteBuffer.limit() method."); + S3JniUnrefLocal(bb); + }else{ + SJG.g.byteBuffer.klazz = 0; + SJG.g.byteBuffer.midAlloc = 0; + } + } + + sqlite3_shutdown() + /* So that it becomes legal for Java-level code to call + ** sqlite3_config(). */; +} diff --git a/ext/jni/src/c/sqlite3-jni.h b/ext/jni/src/c/sqlite3-jni.h new file mode 100644 index 0000000000..81af5cbde1 --- /dev/null +++ b/ext/jni/src/c/sqlite3-jni.h @@ -0,0 +1,2469 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include +/* Header for class org_sqlite_jni_capi_CApi */ + +#ifndef _Included_org_sqlite_jni_capi_CApi +#define _Included_org_sqlite_jni_capi_CApi +#ifdef __cplusplus +extern "C" { +#endif +#undef org_sqlite_jni_capi_CApi_SQLITE_ACCESS_EXISTS +#define org_sqlite_jni_capi_CApi_SQLITE_ACCESS_EXISTS 0L +#undef org_sqlite_jni_capi_CApi_SQLITE_ACCESS_READWRITE +#define org_sqlite_jni_capi_CApi_SQLITE_ACCESS_READWRITE 1L +#undef org_sqlite_jni_capi_CApi_SQLITE_ACCESS_READ +#define org_sqlite_jni_capi_CApi_SQLITE_ACCESS_READ 2L +#undef org_sqlite_jni_capi_CApi_SQLITE_DENY +#define org_sqlite_jni_capi_CApi_SQLITE_DENY 1L +#undef org_sqlite_jni_capi_CApi_SQLITE_IGNORE +#define org_sqlite_jni_capi_CApi_SQLITE_IGNORE 2L +#undef org_sqlite_jni_capi_CApi_SQLITE_CREATE_INDEX +#define org_sqlite_jni_capi_CApi_SQLITE_CREATE_INDEX 1L +#undef org_sqlite_jni_capi_CApi_SQLITE_CREATE_TABLE +#define org_sqlite_jni_capi_CApi_SQLITE_CREATE_TABLE 2L +#undef org_sqlite_jni_capi_CApi_SQLITE_CREATE_TEMP_INDEX +#define org_sqlite_jni_capi_CApi_SQLITE_CREATE_TEMP_INDEX 3L +#undef org_sqlite_jni_capi_CApi_SQLITE_CREATE_TEMP_TABLE +#define org_sqlite_jni_capi_CApi_SQLITE_CREATE_TEMP_TABLE 4L +#undef org_sqlite_jni_capi_CApi_SQLITE_CREATE_TEMP_TRIGGER +#define org_sqlite_jni_capi_CApi_SQLITE_CREATE_TEMP_TRIGGER 5L +#undef org_sqlite_jni_capi_CApi_SQLITE_CREATE_TEMP_VIEW +#define org_sqlite_jni_capi_CApi_SQLITE_CREATE_TEMP_VIEW 6L +#undef org_sqlite_jni_capi_CApi_SQLITE_CREATE_TRIGGER +#define org_sqlite_jni_capi_CApi_SQLITE_CREATE_TRIGGER 7L +#undef org_sqlite_jni_capi_CApi_SQLITE_CREATE_VIEW +#define org_sqlite_jni_capi_CApi_SQLITE_CREATE_VIEW 8L +#undef org_sqlite_jni_capi_CApi_SQLITE_DELETE +#define org_sqlite_jni_capi_CApi_SQLITE_DELETE 9L +#undef org_sqlite_jni_capi_CApi_SQLITE_DROP_INDEX +#define org_sqlite_jni_capi_CApi_SQLITE_DROP_INDEX 10L +#undef org_sqlite_jni_capi_CApi_SQLITE_DROP_TABLE +#define org_sqlite_jni_capi_CApi_SQLITE_DROP_TABLE 11L +#undef org_sqlite_jni_capi_CApi_SQLITE_DROP_TEMP_INDEX +#define org_sqlite_jni_capi_CApi_SQLITE_DROP_TEMP_INDEX 12L +#undef org_sqlite_jni_capi_CApi_SQLITE_DROP_TEMP_TABLE +#define org_sqlite_jni_capi_CApi_SQLITE_DROP_TEMP_TABLE 13L +#undef org_sqlite_jni_capi_CApi_SQLITE_DROP_TEMP_TRIGGER +#define org_sqlite_jni_capi_CApi_SQLITE_DROP_TEMP_TRIGGER 14L +#undef org_sqlite_jni_capi_CApi_SQLITE_DROP_TEMP_VIEW +#define org_sqlite_jni_capi_CApi_SQLITE_DROP_TEMP_VIEW 15L +#undef org_sqlite_jni_capi_CApi_SQLITE_DROP_TRIGGER +#define org_sqlite_jni_capi_CApi_SQLITE_DROP_TRIGGER 16L +#undef org_sqlite_jni_capi_CApi_SQLITE_DROP_VIEW +#define org_sqlite_jni_capi_CApi_SQLITE_DROP_VIEW 17L +#undef org_sqlite_jni_capi_CApi_SQLITE_INSERT +#define org_sqlite_jni_capi_CApi_SQLITE_INSERT 18L +#undef org_sqlite_jni_capi_CApi_SQLITE_PRAGMA +#define org_sqlite_jni_capi_CApi_SQLITE_PRAGMA 19L +#undef org_sqlite_jni_capi_CApi_SQLITE_READ +#define org_sqlite_jni_capi_CApi_SQLITE_READ 20L +#undef org_sqlite_jni_capi_CApi_SQLITE_SELECT +#define org_sqlite_jni_capi_CApi_SQLITE_SELECT 21L +#undef org_sqlite_jni_capi_CApi_SQLITE_TRANSACTION +#define org_sqlite_jni_capi_CApi_SQLITE_TRANSACTION 22L +#undef org_sqlite_jni_capi_CApi_SQLITE_UPDATE +#define org_sqlite_jni_capi_CApi_SQLITE_UPDATE 23L +#undef org_sqlite_jni_capi_CApi_SQLITE_ATTACH +#define org_sqlite_jni_capi_CApi_SQLITE_ATTACH 24L +#undef org_sqlite_jni_capi_CApi_SQLITE_DETACH +#define org_sqlite_jni_capi_CApi_SQLITE_DETACH 25L +#undef org_sqlite_jni_capi_CApi_SQLITE_ALTER_TABLE +#define org_sqlite_jni_capi_CApi_SQLITE_ALTER_TABLE 26L +#undef org_sqlite_jni_capi_CApi_SQLITE_REINDEX +#define org_sqlite_jni_capi_CApi_SQLITE_REINDEX 27L +#undef org_sqlite_jni_capi_CApi_SQLITE_ANALYZE +#define org_sqlite_jni_capi_CApi_SQLITE_ANALYZE 28L +#undef org_sqlite_jni_capi_CApi_SQLITE_CREATE_VTABLE +#define org_sqlite_jni_capi_CApi_SQLITE_CREATE_VTABLE 29L +#undef org_sqlite_jni_capi_CApi_SQLITE_DROP_VTABLE +#define org_sqlite_jni_capi_CApi_SQLITE_DROP_VTABLE 30L +#undef org_sqlite_jni_capi_CApi_SQLITE_FUNCTION +#define org_sqlite_jni_capi_CApi_SQLITE_FUNCTION 31L +#undef org_sqlite_jni_capi_CApi_SQLITE_SAVEPOINT +#define org_sqlite_jni_capi_CApi_SQLITE_SAVEPOINT 32L +#undef org_sqlite_jni_capi_CApi_SQLITE_RECURSIVE +#define org_sqlite_jni_capi_CApi_SQLITE_RECURSIVE 33L +#undef org_sqlite_jni_capi_CApi_SQLITE_STATIC +#define org_sqlite_jni_capi_CApi_SQLITE_STATIC 0LL +#undef org_sqlite_jni_capi_CApi_SQLITE_TRANSIENT +#define org_sqlite_jni_capi_CApi_SQLITE_TRANSIENT -1LL +#undef org_sqlite_jni_capi_CApi_SQLITE_CHANGESETSTART_INVERT +#define org_sqlite_jni_capi_CApi_SQLITE_CHANGESETSTART_INVERT 2L +#undef org_sqlite_jni_capi_CApi_SQLITE_CHANGESETAPPLY_NOSAVEPOINT +#define org_sqlite_jni_capi_CApi_SQLITE_CHANGESETAPPLY_NOSAVEPOINT 1L +#undef org_sqlite_jni_capi_CApi_SQLITE_CHANGESETAPPLY_INVERT +#define org_sqlite_jni_capi_CApi_SQLITE_CHANGESETAPPLY_INVERT 2L +#undef org_sqlite_jni_capi_CApi_SQLITE_CHANGESETAPPLY_IGNORENOOP +#define org_sqlite_jni_capi_CApi_SQLITE_CHANGESETAPPLY_IGNORENOOP 4L +#undef org_sqlite_jni_capi_CApi_SQLITE_CHANGESET_DATA +#define org_sqlite_jni_capi_CApi_SQLITE_CHANGESET_DATA 1L +#undef org_sqlite_jni_capi_CApi_SQLITE_CHANGESET_NOTFOUND +#define org_sqlite_jni_capi_CApi_SQLITE_CHANGESET_NOTFOUND 2L +#undef org_sqlite_jni_capi_CApi_SQLITE_CHANGESET_CONFLICT +#define org_sqlite_jni_capi_CApi_SQLITE_CHANGESET_CONFLICT 3L +#undef org_sqlite_jni_capi_CApi_SQLITE_CHANGESET_CONSTRAINT +#define org_sqlite_jni_capi_CApi_SQLITE_CHANGESET_CONSTRAINT 4L +#undef org_sqlite_jni_capi_CApi_SQLITE_CHANGESET_FOREIGN_KEY +#define org_sqlite_jni_capi_CApi_SQLITE_CHANGESET_FOREIGN_KEY 5L +#undef org_sqlite_jni_capi_CApi_SQLITE_CHANGESET_OMIT +#define org_sqlite_jni_capi_CApi_SQLITE_CHANGESET_OMIT 0L +#undef org_sqlite_jni_capi_CApi_SQLITE_CHANGESET_REPLACE +#define org_sqlite_jni_capi_CApi_SQLITE_CHANGESET_REPLACE 1L +#undef org_sqlite_jni_capi_CApi_SQLITE_CHANGESET_ABORT +#define org_sqlite_jni_capi_CApi_SQLITE_CHANGESET_ABORT 2L +#undef org_sqlite_jni_capi_CApi_SQLITE_CONFIG_SINGLETHREAD +#define org_sqlite_jni_capi_CApi_SQLITE_CONFIG_SINGLETHREAD 1L +#undef org_sqlite_jni_capi_CApi_SQLITE_CONFIG_MULTITHREAD +#define org_sqlite_jni_capi_CApi_SQLITE_CONFIG_MULTITHREAD 2L +#undef org_sqlite_jni_capi_CApi_SQLITE_CONFIG_SERIALIZED +#define org_sqlite_jni_capi_CApi_SQLITE_CONFIG_SERIALIZED 3L +#undef org_sqlite_jni_capi_CApi_SQLITE_CONFIG_MALLOC +#define org_sqlite_jni_capi_CApi_SQLITE_CONFIG_MALLOC 4L +#undef org_sqlite_jni_capi_CApi_SQLITE_CONFIG_GETMALLOC +#define org_sqlite_jni_capi_CApi_SQLITE_CONFIG_GETMALLOC 5L +#undef org_sqlite_jni_capi_CApi_SQLITE_CONFIG_SCRATCH +#define org_sqlite_jni_capi_CApi_SQLITE_CONFIG_SCRATCH 6L +#undef org_sqlite_jni_capi_CApi_SQLITE_CONFIG_PAGECACHE +#define org_sqlite_jni_capi_CApi_SQLITE_CONFIG_PAGECACHE 7L +#undef org_sqlite_jni_capi_CApi_SQLITE_CONFIG_HEAP +#define org_sqlite_jni_capi_CApi_SQLITE_CONFIG_HEAP 8L +#undef org_sqlite_jni_capi_CApi_SQLITE_CONFIG_MEMSTATUS +#define org_sqlite_jni_capi_CApi_SQLITE_CONFIG_MEMSTATUS 9L +#undef org_sqlite_jni_capi_CApi_SQLITE_CONFIG_MUTEX +#define org_sqlite_jni_capi_CApi_SQLITE_CONFIG_MUTEX 10L +#undef org_sqlite_jni_capi_CApi_SQLITE_CONFIG_GETMUTEX +#define org_sqlite_jni_capi_CApi_SQLITE_CONFIG_GETMUTEX 11L +#undef org_sqlite_jni_capi_CApi_SQLITE_CONFIG_LOOKASIDE +#define org_sqlite_jni_capi_CApi_SQLITE_CONFIG_LOOKASIDE 13L +#undef org_sqlite_jni_capi_CApi_SQLITE_CONFIG_PCACHE +#define org_sqlite_jni_capi_CApi_SQLITE_CONFIG_PCACHE 14L +#undef org_sqlite_jni_capi_CApi_SQLITE_CONFIG_GETPCACHE +#define org_sqlite_jni_capi_CApi_SQLITE_CONFIG_GETPCACHE 15L +#undef org_sqlite_jni_capi_CApi_SQLITE_CONFIG_LOG +#define org_sqlite_jni_capi_CApi_SQLITE_CONFIG_LOG 16L +#undef org_sqlite_jni_capi_CApi_SQLITE_CONFIG_URI +#define org_sqlite_jni_capi_CApi_SQLITE_CONFIG_URI 17L +#undef org_sqlite_jni_capi_CApi_SQLITE_CONFIG_PCACHE2 +#define org_sqlite_jni_capi_CApi_SQLITE_CONFIG_PCACHE2 18L +#undef org_sqlite_jni_capi_CApi_SQLITE_CONFIG_GETPCACHE2 +#define org_sqlite_jni_capi_CApi_SQLITE_CONFIG_GETPCACHE2 19L +#undef org_sqlite_jni_capi_CApi_SQLITE_CONFIG_COVERING_INDEX_SCAN +#define org_sqlite_jni_capi_CApi_SQLITE_CONFIG_COVERING_INDEX_SCAN 20L +#undef org_sqlite_jni_capi_CApi_SQLITE_CONFIG_SQLLOG +#define org_sqlite_jni_capi_CApi_SQLITE_CONFIG_SQLLOG 21L +#undef org_sqlite_jni_capi_CApi_SQLITE_CONFIG_MMAP_SIZE +#define org_sqlite_jni_capi_CApi_SQLITE_CONFIG_MMAP_SIZE 22L +#undef org_sqlite_jni_capi_CApi_SQLITE_CONFIG_WIN32_HEAPSIZE +#define org_sqlite_jni_capi_CApi_SQLITE_CONFIG_WIN32_HEAPSIZE 23L +#undef org_sqlite_jni_capi_CApi_SQLITE_CONFIG_PCACHE_HDRSZ +#define org_sqlite_jni_capi_CApi_SQLITE_CONFIG_PCACHE_HDRSZ 24L +#undef org_sqlite_jni_capi_CApi_SQLITE_CONFIG_PMASZ +#define org_sqlite_jni_capi_CApi_SQLITE_CONFIG_PMASZ 25L +#undef org_sqlite_jni_capi_CApi_SQLITE_CONFIG_STMTJRNL_SPILL +#define org_sqlite_jni_capi_CApi_SQLITE_CONFIG_STMTJRNL_SPILL 26L +#undef org_sqlite_jni_capi_CApi_SQLITE_CONFIG_SMALL_MALLOC +#define org_sqlite_jni_capi_CApi_SQLITE_CONFIG_SMALL_MALLOC 27L +#undef org_sqlite_jni_capi_CApi_SQLITE_CONFIG_SORTERREF_SIZE +#define org_sqlite_jni_capi_CApi_SQLITE_CONFIG_SORTERREF_SIZE 28L +#undef org_sqlite_jni_capi_CApi_SQLITE_CONFIG_MEMDB_MAXSIZE +#define org_sqlite_jni_capi_CApi_SQLITE_CONFIG_MEMDB_MAXSIZE 29L +#undef org_sqlite_jni_capi_CApi_SQLITE_INTEGER +#define org_sqlite_jni_capi_CApi_SQLITE_INTEGER 1L +#undef org_sqlite_jni_capi_CApi_SQLITE_FLOAT +#define org_sqlite_jni_capi_CApi_SQLITE_FLOAT 2L +#undef org_sqlite_jni_capi_CApi_SQLITE_TEXT +#define org_sqlite_jni_capi_CApi_SQLITE_TEXT 3L +#undef org_sqlite_jni_capi_CApi_SQLITE_BLOB +#define org_sqlite_jni_capi_CApi_SQLITE_BLOB 4L +#undef org_sqlite_jni_capi_CApi_SQLITE_NULL +#define org_sqlite_jni_capi_CApi_SQLITE_NULL 5L +#undef org_sqlite_jni_capi_CApi_SQLITE_DBCONFIG_MAINDBNAME +#define org_sqlite_jni_capi_CApi_SQLITE_DBCONFIG_MAINDBNAME 1000L +#undef org_sqlite_jni_capi_CApi_SQLITE_DBCONFIG_LOOKASIDE +#define org_sqlite_jni_capi_CApi_SQLITE_DBCONFIG_LOOKASIDE 1001L +#undef org_sqlite_jni_capi_CApi_SQLITE_DBCONFIG_ENABLE_FKEY +#define org_sqlite_jni_capi_CApi_SQLITE_DBCONFIG_ENABLE_FKEY 1002L +#undef org_sqlite_jni_capi_CApi_SQLITE_DBCONFIG_ENABLE_TRIGGER +#define org_sqlite_jni_capi_CApi_SQLITE_DBCONFIG_ENABLE_TRIGGER 1003L +#undef org_sqlite_jni_capi_CApi_SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER +#define org_sqlite_jni_capi_CApi_SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER 1004L +#undef org_sqlite_jni_capi_CApi_SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION +#define org_sqlite_jni_capi_CApi_SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION 1005L +#undef org_sqlite_jni_capi_CApi_SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE +#define org_sqlite_jni_capi_CApi_SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE 1006L +#undef org_sqlite_jni_capi_CApi_SQLITE_DBCONFIG_ENABLE_QPSG +#define org_sqlite_jni_capi_CApi_SQLITE_DBCONFIG_ENABLE_QPSG 1007L +#undef org_sqlite_jni_capi_CApi_SQLITE_DBCONFIG_TRIGGER_EQP +#define org_sqlite_jni_capi_CApi_SQLITE_DBCONFIG_TRIGGER_EQP 1008L +#undef org_sqlite_jni_capi_CApi_SQLITE_DBCONFIG_RESET_DATABASE +#define org_sqlite_jni_capi_CApi_SQLITE_DBCONFIG_RESET_DATABASE 1009L +#undef org_sqlite_jni_capi_CApi_SQLITE_DBCONFIG_DEFENSIVE +#define org_sqlite_jni_capi_CApi_SQLITE_DBCONFIG_DEFENSIVE 1010L +#undef org_sqlite_jni_capi_CApi_SQLITE_DBCONFIG_WRITABLE_SCHEMA +#define org_sqlite_jni_capi_CApi_SQLITE_DBCONFIG_WRITABLE_SCHEMA 1011L +#undef org_sqlite_jni_capi_CApi_SQLITE_DBCONFIG_LEGACY_ALTER_TABLE +#define org_sqlite_jni_capi_CApi_SQLITE_DBCONFIG_LEGACY_ALTER_TABLE 1012L +#undef org_sqlite_jni_capi_CApi_SQLITE_DBCONFIG_DQS_DML +#define org_sqlite_jni_capi_CApi_SQLITE_DBCONFIG_DQS_DML 1013L +#undef org_sqlite_jni_capi_CApi_SQLITE_DBCONFIG_DQS_DDL +#define org_sqlite_jni_capi_CApi_SQLITE_DBCONFIG_DQS_DDL 1014L +#undef org_sqlite_jni_capi_CApi_SQLITE_DBCONFIG_ENABLE_VIEW +#define org_sqlite_jni_capi_CApi_SQLITE_DBCONFIG_ENABLE_VIEW 1015L +#undef org_sqlite_jni_capi_CApi_SQLITE_DBCONFIG_LEGACY_FILE_FORMAT +#define org_sqlite_jni_capi_CApi_SQLITE_DBCONFIG_LEGACY_FILE_FORMAT 1016L +#undef org_sqlite_jni_capi_CApi_SQLITE_DBCONFIG_TRUSTED_SCHEMA +#define org_sqlite_jni_capi_CApi_SQLITE_DBCONFIG_TRUSTED_SCHEMA 1017L +#undef org_sqlite_jni_capi_CApi_SQLITE_DBCONFIG_STMT_SCANSTATUS +#define org_sqlite_jni_capi_CApi_SQLITE_DBCONFIG_STMT_SCANSTATUS 1018L +#undef org_sqlite_jni_capi_CApi_SQLITE_DBCONFIG_REVERSE_SCANORDER +#define org_sqlite_jni_capi_CApi_SQLITE_DBCONFIG_REVERSE_SCANORDER 1019L +#undef org_sqlite_jni_capi_CApi_SQLITE_DBCONFIG_MAX +#define org_sqlite_jni_capi_CApi_SQLITE_DBCONFIG_MAX 1019L +#undef org_sqlite_jni_capi_CApi_SQLITE_DBSTATUS_LOOKASIDE_USED +#define org_sqlite_jni_capi_CApi_SQLITE_DBSTATUS_LOOKASIDE_USED 0L +#undef org_sqlite_jni_capi_CApi_SQLITE_DBSTATUS_CACHE_USED +#define org_sqlite_jni_capi_CApi_SQLITE_DBSTATUS_CACHE_USED 1L +#undef org_sqlite_jni_capi_CApi_SQLITE_DBSTATUS_SCHEMA_USED +#define org_sqlite_jni_capi_CApi_SQLITE_DBSTATUS_SCHEMA_USED 2L +#undef org_sqlite_jni_capi_CApi_SQLITE_DBSTATUS_STMT_USED +#define org_sqlite_jni_capi_CApi_SQLITE_DBSTATUS_STMT_USED 3L +#undef org_sqlite_jni_capi_CApi_SQLITE_DBSTATUS_LOOKASIDE_HIT +#define org_sqlite_jni_capi_CApi_SQLITE_DBSTATUS_LOOKASIDE_HIT 4L +#undef org_sqlite_jni_capi_CApi_SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE +#define org_sqlite_jni_capi_CApi_SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE 5L +#undef org_sqlite_jni_capi_CApi_SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL +#define org_sqlite_jni_capi_CApi_SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL 6L +#undef org_sqlite_jni_capi_CApi_SQLITE_DBSTATUS_CACHE_HIT +#define org_sqlite_jni_capi_CApi_SQLITE_DBSTATUS_CACHE_HIT 7L +#undef org_sqlite_jni_capi_CApi_SQLITE_DBSTATUS_CACHE_MISS +#define org_sqlite_jni_capi_CApi_SQLITE_DBSTATUS_CACHE_MISS 8L +#undef org_sqlite_jni_capi_CApi_SQLITE_DBSTATUS_CACHE_WRITE +#define org_sqlite_jni_capi_CApi_SQLITE_DBSTATUS_CACHE_WRITE 9L +#undef org_sqlite_jni_capi_CApi_SQLITE_DBSTATUS_DEFERRED_FKS +#define org_sqlite_jni_capi_CApi_SQLITE_DBSTATUS_DEFERRED_FKS 10L +#undef org_sqlite_jni_capi_CApi_SQLITE_DBSTATUS_CACHE_USED_SHARED +#define org_sqlite_jni_capi_CApi_SQLITE_DBSTATUS_CACHE_USED_SHARED 11L +#undef org_sqlite_jni_capi_CApi_SQLITE_DBSTATUS_CACHE_SPILL +#define org_sqlite_jni_capi_CApi_SQLITE_DBSTATUS_CACHE_SPILL 12L +#undef org_sqlite_jni_capi_CApi_SQLITE_DBSTATUS_MAX +#define org_sqlite_jni_capi_CApi_SQLITE_DBSTATUS_MAX 12L +#undef org_sqlite_jni_capi_CApi_SQLITE_UTF8 +#define org_sqlite_jni_capi_CApi_SQLITE_UTF8 1L +#undef org_sqlite_jni_capi_CApi_SQLITE_UTF16LE +#define org_sqlite_jni_capi_CApi_SQLITE_UTF16LE 2L +#undef org_sqlite_jni_capi_CApi_SQLITE_UTF16BE +#define org_sqlite_jni_capi_CApi_SQLITE_UTF16BE 3L +#undef org_sqlite_jni_capi_CApi_SQLITE_UTF16 +#define org_sqlite_jni_capi_CApi_SQLITE_UTF16 4L +#undef org_sqlite_jni_capi_CApi_SQLITE_UTF16_ALIGNED +#define org_sqlite_jni_capi_CApi_SQLITE_UTF16_ALIGNED 8L +#undef org_sqlite_jni_capi_CApi_SQLITE_FCNTL_LOCKSTATE +#define org_sqlite_jni_capi_CApi_SQLITE_FCNTL_LOCKSTATE 1L +#undef org_sqlite_jni_capi_CApi_SQLITE_FCNTL_GET_LOCKPROXYFILE +#define org_sqlite_jni_capi_CApi_SQLITE_FCNTL_GET_LOCKPROXYFILE 2L +#undef org_sqlite_jni_capi_CApi_SQLITE_FCNTL_SET_LOCKPROXYFILE +#define org_sqlite_jni_capi_CApi_SQLITE_FCNTL_SET_LOCKPROXYFILE 3L +#undef org_sqlite_jni_capi_CApi_SQLITE_FCNTL_LAST_ERRNO +#define org_sqlite_jni_capi_CApi_SQLITE_FCNTL_LAST_ERRNO 4L +#undef org_sqlite_jni_capi_CApi_SQLITE_FCNTL_SIZE_HINT +#define org_sqlite_jni_capi_CApi_SQLITE_FCNTL_SIZE_HINT 5L +#undef org_sqlite_jni_capi_CApi_SQLITE_FCNTL_CHUNK_SIZE +#define org_sqlite_jni_capi_CApi_SQLITE_FCNTL_CHUNK_SIZE 6L +#undef org_sqlite_jni_capi_CApi_SQLITE_FCNTL_FILE_POINTER +#define org_sqlite_jni_capi_CApi_SQLITE_FCNTL_FILE_POINTER 7L +#undef org_sqlite_jni_capi_CApi_SQLITE_FCNTL_SYNC_OMITTED +#define org_sqlite_jni_capi_CApi_SQLITE_FCNTL_SYNC_OMITTED 8L +#undef org_sqlite_jni_capi_CApi_SQLITE_FCNTL_WIN32_AV_RETRY +#define org_sqlite_jni_capi_CApi_SQLITE_FCNTL_WIN32_AV_RETRY 9L +#undef org_sqlite_jni_capi_CApi_SQLITE_FCNTL_PERSIST_WAL +#define org_sqlite_jni_capi_CApi_SQLITE_FCNTL_PERSIST_WAL 10L +#undef org_sqlite_jni_capi_CApi_SQLITE_FCNTL_OVERWRITE +#define org_sqlite_jni_capi_CApi_SQLITE_FCNTL_OVERWRITE 11L +#undef org_sqlite_jni_capi_CApi_SQLITE_FCNTL_VFSNAME +#define org_sqlite_jni_capi_CApi_SQLITE_FCNTL_VFSNAME 12L +#undef org_sqlite_jni_capi_CApi_SQLITE_FCNTL_POWERSAFE_OVERWRITE +#define org_sqlite_jni_capi_CApi_SQLITE_FCNTL_POWERSAFE_OVERWRITE 13L +#undef org_sqlite_jni_capi_CApi_SQLITE_FCNTL_PRAGMA +#define org_sqlite_jni_capi_CApi_SQLITE_FCNTL_PRAGMA 14L +#undef org_sqlite_jni_capi_CApi_SQLITE_FCNTL_BUSYHANDLER +#define org_sqlite_jni_capi_CApi_SQLITE_FCNTL_BUSYHANDLER 15L +#undef org_sqlite_jni_capi_CApi_SQLITE_FCNTL_TEMPFILENAME +#define org_sqlite_jni_capi_CApi_SQLITE_FCNTL_TEMPFILENAME 16L +#undef org_sqlite_jni_capi_CApi_SQLITE_FCNTL_MMAP_SIZE +#define org_sqlite_jni_capi_CApi_SQLITE_FCNTL_MMAP_SIZE 18L +#undef org_sqlite_jni_capi_CApi_SQLITE_FCNTL_TRACE +#define org_sqlite_jni_capi_CApi_SQLITE_FCNTL_TRACE 19L +#undef org_sqlite_jni_capi_CApi_SQLITE_FCNTL_HAS_MOVED +#define org_sqlite_jni_capi_CApi_SQLITE_FCNTL_HAS_MOVED 20L +#undef org_sqlite_jni_capi_CApi_SQLITE_FCNTL_SYNC +#define org_sqlite_jni_capi_CApi_SQLITE_FCNTL_SYNC 21L +#undef org_sqlite_jni_capi_CApi_SQLITE_FCNTL_COMMIT_PHASETWO +#define org_sqlite_jni_capi_CApi_SQLITE_FCNTL_COMMIT_PHASETWO 22L +#undef org_sqlite_jni_capi_CApi_SQLITE_FCNTL_WIN32_SET_HANDLE +#define org_sqlite_jni_capi_CApi_SQLITE_FCNTL_WIN32_SET_HANDLE 23L +#undef org_sqlite_jni_capi_CApi_SQLITE_FCNTL_WAL_BLOCK +#define org_sqlite_jni_capi_CApi_SQLITE_FCNTL_WAL_BLOCK 24L +#undef org_sqlite_jni_capi_CApi_SQLITE_FCNTL_ZIPVFS +#define org_sqlite_jni_capi_CApi_SQLITE_FCNTL_ZIPVFS 25L +#undef org_sqlite_jni_capi_CApi_SQLITE_FCNTL_RBU +#define org_sqlite_jni_capi_CApi_SQLITE_FCNTL_RBU 26L +#undef org_sqlite_jni_capi_CApi_SQLITE_FCNTL_VFS_POINTER +#define org_sqlite_jni_capi_CApi_SQLITE_FCNTL_VFS_POINTER 27L +#undef org_sqlite_jni_capi_CApi_SQLITE_FCNTL_JOURNAL_POINTER +#define org_sqlite_jni_capi_CApi_SQLITE_FCNTL_JOURNAL_POINTER 28L +#undef org_sqlite_jni_capi_CApi_SQLITE_FCNTL_WIN32_GET_HANDLE +#define org_sqlite_jni_capi_CApi_SQLITE_FCNTL_WIN32_GET_HANDLE 29L +#undef org_sqlite_jni_capi_CApi_SQLITE_FCNTL_PDB +#define org_sqlite_jni_capi_CApi_SQLITE_FCNTL_PDB 30L +#undef org_sqlite_jni_capi_CApi_SQLITE_FCNTL_BEGIN_ATOMIC_WRITE +#define org_sqlite_jni_capi_CApi_SQLITE_FCNTL_BEGIN_ATOMIC_WRITE 31L +#undef org_sqlite_jni_capi_CApi_SQLITE_FCNTL_COMMIT_ATOMIC_WRITE +#define org_sqlite_jni_capi_CApi_SQLITE_FCNTL_COMMIT_ATOMIC_WRITE 32L +#undef org_sqlite_jni_capi_CApi_SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE +#define org_sqlite_jni_capi_CApi_SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE 33L +#undef org_sqlite_jni_capi_CApi_SQLITE_FCNTL_LOCK_TIMEOUT +#define org_sqlite_jni_capi_CApi_SQLITE_FCNTL_LOCK_TIMEOUT 34L +#undef org_sqlite_jni_capi_CApi_SQLITE_FCNTL_DATA_VERSION +#define org_sqlite_jni_capi_CApi_SQLITE_FCNTL_DATA_VERSION 35L +#undef org_sqlite_jni_capi_CApi_SQLITE_FCNTL_SIZE_LIMIT +#define org_sqlite_jni_capi_CApi_SQLITE_FCNTL_SIZE_LIMIT 36L +#undef org_sqlite_jni_capi_CApi_SQLITE_FCNTL_CKPT_DONE +#define org_sqlite_jni_capi_CApi_SQLITE_FCNTL_CKPT_DONE 37L +#undef org_sqlite_jni_capi_CApi_SQLITE_FCNTL_RESERVE_BYTES +#define org_sqlite_jni_capi_CApi_SQLITE_FCNTL_RESERVE_BYTES 38L +#undef org_sqlite_jni_capi_CApi_SQLITE_FCNTL_CKPT_START +#define org_sqlite_jni_capi_CApi_SQLITE_FCNTL_CKPT_START 39L +#undef org_sqlite_jni_capi_CApi_SQLITE_FCNTL_EXTERNAL_READER +#define org_sqlite_jni_capi_CApi_SQLITE_FCNTL_EXTERNAL_READER 40L +#undef org_sqlite_jni_capi_CApi_SQLITE_FCNTL_CKSM_FILE +#define org_sqlite_jni_capi_CApi_SQLITE_FCNTL_CKSM_FILE 41L +#undef org_sqlite_jni_capi_CApi_SQLITE_FCNTL_RESET_CACHE +#define org_sqlite_jni_capi_CApi_SQLITE_FCNTL_RESET_CACHE 42L +#undef org_sqlite_jni_capi_CApi_SQLITE_LOCK_NONE +#define org_sqlite_jni_capi_CApi_SQLITE_LOCK_NONE 0L +#undef org_sqlite_jni_capi_CApi_SQLITE_LOCK_SHARED +#define org_sqlite_jni_capi_CApi_SQLITE_LOCK_SHARED 1L +#undef org_sqlite_jni_capi_CApi_SQLITE_LOCK_RESERVED +#define org_sqlite_jni_capi_CApi_SQLITE_LOCK_RESERVED 2L +#undef org_sqlite_jni_capi_CApi_SQLITE_LOCK_PENDING +#define org_sqlite_jni_capi_CApi_SQLITE_LOCK_PENDING 3L +#undef org_sqlite_jni_capi_CApi_SQLITE_LOCK_EXCLUSIVE +#define org_sqlite_jni_capi_CApi_SQLITE_LOCK_EXCLUSIVE 4L +#undef org_sqlite_jni_capi_CApi_SQLITE_IOCAP_ATOMIC +#define org_sqlite_jni_capi_CApi_SQLITE_IOCAP_ATOMIC 1L +#undef org_sqlite_jni_capi_CApi_SQLITE_IOCAP_ATOMIC512 +#define org_sqlite_jni_capi_CApi_SQLITE_IOCAP_ATOMIC512 2L +#undef org_sqlite_jni_capi_CApi_SQLITE_IOCAP_ATOMIC1K +#define org_sqlite_jni_capi_CApi_SQLITE_IOCAP_ATOMIC1K 4L +#undef org_sqlite_jni_capi_CApi_SQLITE_IOCAP_ATOMIC2K +#define org_sqlite_jni_capi_CApi_SQLITE_IOCAP_ATOMIC2K 8L +#undef org_sqlite_jni_capi_CApi_SQLITE_IOCAP_ATOMIC4K +#define org_sqlite_jni_capi_CApi_SQLITE_IOCAP_ATOMIC4K 16L +#undef org_sqlite_jni_capi_CApi_SQLITE_IOCAP_ATOMIC8K +#define org_sqlite_jni_capi_CApi_SQLITE_IOCAP_ATOMIC8K 32L +#undef org_sqlite_jni_capi_CApi_SQLITE_IOCAP_ATOMIC16K +#define org_sqlite_jni_capi_CApi_SQLITE_IOCAP_ATOMIC16K 64L +#undef org_sqlite_jni_capi_CApi_SQLITE_IOCAP_ATOMIC32K +#define org_sqlite_jni_capi_CApi_SQLITE_IOCAP_ATOMIC32K 128L +#undef org_sqlite_jni_capi_CApi_SQLITE_IOCAP_ATOMIC64K +#define org_sqlite_jni_capi_CApi_SQLITE_IOCAP_ATOMIC64K 256L +#undef org_sqlite_jni_capi_CApi_SQLITE_IOCAP_SAFE_APPEND +#define org_sqlite_jni_capi_CApi_SQLITE_IOCAP_SAFE_APPEND 512L +#undef org_sqlite_jni_capi_CApi_SQLITE_IOCAP_SEQUENTIAL +#define org_sqlite_jni_capi_CApi_SQLITE_IOCAP_SEQUENTIAL 1024L +#undef org_sqlite_jni_capi_CApi_SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN +#define org_sqlite_jni_capi_CApi_SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN 2048L +#undef org_sqlite_jni_capi_CApi_SQLITE_IOCAP_POWERSAFE_OVERWRITE +#define org_sqlite_jni_capi_CApi_SQLITE_IOCAP_POWERSAFE_OVERWRITE 4096L +#undef org_sqlite_jni_capi_CApi_SQLITE_IOCAP_IMMUTABLE +#define org_sqlite_jni_capi_CApi_SQLITE_IOCAP_IMMUTABLE 8192L +#undef org_sqlite_jni_capi_CApi_SQLITE_IOCAP_BATCH_ATOMIC +#define org_sqlite_jni_capi_CApi_SQLITE_IOCAP_BATCH_ATOMIC 16384L +#undef org_sqlite_jni_capi_CApi_SQLITE_LIMIT_LENGTH +#define org_sqlite_jni_capi_CApi_SQLITE_LIMIT_LENGTH 0L +#undef org_sqlite_jni_capi_CApi_SQLITE_LIMIT_SQL_LENGTH +#define org_sqlite_jni_capi_CApi_SQLITE_LIMIT_SQL_LENGTH 1L +#undef org_sqlite_jni_capi_CApi_SQLITE_LIMIT_COLUMN +#define org_sqlite_jni_capi_CApi_SQLITE_LIMIT_COLUMN 2L +#undef org_sqlite_jni_capi_CApi_SQLITE_LIMIT_EXPR_DEPTH +#define org_sqlite_jni_capi_CApi_SQLITE_LIMIT_EXPR_DEPTH 3L +#undef org_sqlite_jni_capi_CApi_SQLITE_LIMIT_COMPOUND_SELECT +#define org_sqlite_jni_capi_CApi_SQLITE_LIMIT_COMPOUND_SELECT 4L +#undef org_sqlite_jni_capi_CApi_SQLITE_LIMIT_VDBE_OP +#define org_sqlite_jni_capi_CApi_SQLITE_LIMIT_VDBE_OP 5L +#undef org_sqlite_jni_capi_CApi_SQLITE_LIMIT_FUNCTION_ARG +#define org_sqlite_jni_capi_CApi_SQLITE_LIMIT_FUNCTION_ARG 6L +#undef org_sqlite_jni_capi_CApi_SQLITE_LIMIT_ATTACHED +#define org_sqlite_jni_capi_CApi_SQLITE_LIMIT_ATTACHED 7L +#undef org_sqlite_jni_capi_CApi_SQLITE_LIMIT_LIKE_PATTERN_LENGTH +#define org_sqlite_jni_capi_CApi_SQLITE_LIMIT_LIKE_PATTERN_LENGTH 8L +#undef org_sqlite_jni_capi_CApi_SQLITE_LIMIT_VARIABLE_NUMBER +#define org_sqlite_jni_capi_CApi_SQLITE_LIMIT_VARIABLE_NUMBER 9L +#undef org_sqlite_jni_capi_CApi_SQLITE_LIMIT_TRIGGER_DEPTH +#define org_sqlite_jni_capi_CApi_SQLITE_LIMIT_TRIGGER_DEPTH 10L +#undef org_sqlite_jni_capi_CApi_SQLITE_LIMIT_WORKER_THREADS +#define org_sqlite_jni_capi_CApi_SQLITE_LIMIT_WORKER_THREADS 11L +#undef org_sqlite_jni_capi_CApi_SQLITE_OPEN_READONLY +#define org_sqlite_jni_capi_CApi_SQLITE_OPEN_READONLY 1L +#undef org_sqlite_jni_capi_CApi_SQLITE_OPEN_READWRITE +#define org_sqlite_jni_capi_CApi_SQLITE_OPEN_READWRITE 2L +#undef org_sqlite_jni_capi_CApi_SQLITE_OPEN_CREATE +#define org_sqlite_jni_capi_CApi_SQLITE_OPEN_CREATE 4L +#undef org_sqlite_jni_capi_CApi_SQLITE_OPEN_URI +#define org_sqlite_jni_capi_CApi_SQLITE_OPEN_URI 64L +#undef org_sqlite_jni_capi_CApi_SQLITE_OPEN_MEMORY +#define org_sqlite_jni_capi_CApi_SQLITE_OPEN_MEMORY 128L +#undef org_sqlite_jni_capi_CApi_SQLITE_OPEN_NOMUTEX +#define org_sqlite_jni_capi_CApi_SQLITE_OPEN_NOMUTEX 32768L +#undef org_sqlite_jni_capi_CApi_SQLITE_OPEN_FULLMUTEX +#define org_sqlite_jni_capi_CApi_SQLITE_OPEN_FULLMUTEX 65536L +#undef org_sqlite_jni_capi_CApi_SQLITE_OPEN_SHAREDCACHE +#define org_sqlite_jni_capi_CApi_SQLITE_OPEN_SHAREDCACHE 131072L +#undef org_sqlite_jni_capi_CApi_SQLITE_OPEN_PRIVATECACHE +#define org_sqlite_jni_capi_CApi_SQLITE_OPEN_PRIVATECACHE 262144L +#undef org_sqlite_jni_capi_CApi_SQLITE_OPEN_NOFOLLOW +#define org_sqlite_jni_capi_CApi_SQLITE_OPEN_NOFOLLOW 16777216L +#undef org_sqlite_jni_capi_CApi_SQLITE_OPEN_EXRESCODE +#define org_sqlite_jni_capi_CApi_SQLITE_OPEN_EXRESCODE 33554432L +#undef org_sqlite_jni_capi_CApi_SQLITE_PREPARE_PERSISTENT +#define org_sqlite_jni_capi_CApi_SQLITE_PREPARE_PERSISTENT 1L +#undef org_sqlite_jni_capi_CApi_SQLITE_PREPARE_NO_VTAB +#define org_sqlite_jni_capi_CApi_SQLITE_PREPARE_NO_VTAB 4L +#undef org_sqlite_jni_capi_CApi_SQLITE_OK +#define org_sqlite_jni_capi_CApi_SQLITE_OK 0L +#undef org_sqlite_jni_capi_CApi_SQLITE_ERROR +#define org_sqlite_jni_capi_CApi_SQLITE_ERROR 1L +#undef org_sqlite_jni_capi_CApi_SQLITE_INTERNAL +#define org_sqlite_jni_capi_CApi_SQLITE_INTERNAL 2L +#undef org_sqlite_jni_capi_CApi_SQLITE_PERM +#define org_sqlite_jni_capi_CApi_SQLITE_PERM 3L +#undef org_sqlite_jni_capi_CApi_SQLITE_ABORT +#define org_sqlite_jni_capi_CApi_SQLITE_ABORT 4L +#undef org_sqlite_jni_capi_CApi_SQLITE_BUSY +#define org_sqlite_jni_capi_CApi_SQLITE_BUSY 5L +#undef org_sqlite_jni_capi_CApi_SQLITE_LOCKED +#define org_sqlite_jni_capi_CApi_SQLITE_LOCKED 6L +#undef org_sqlite_jni_capi_CApi_SQLITE_NOMEM +#define org_sqlite_jni_capi_CApi_SQLITE_NOMEM 7L +#undef org_sqlite_jni_capi_CApi_SQLITE_READONLY +#define org_sqlite_jni_capi_CApi_SQLITE_READONLY 8L +#undef org_sqlite_jni_capi_CApi_SQLITE_INTERRUPT +#define org_sqlite_jni_capi_CApi_SQLITE_INTERRUPT 9L +#undef org_sqlite_jni_capi_CApi_SQLITE_IOERR +#define org_sqlite_jni_capi_CApi_SQLITE_IOERR 10L +#undef org_sqlite_jni_capi_CApi_SQLITE_CORRUPT +#define org_sqlite_jni_capi_CApi_SQLITE_CORRUPT 11L +#undef org_sqlite_jni_capi_CApi_SQLITE_NOTFOUND +#define org_sqlite_jni_capi_CApi_SQLITE_NOTFOUND 12L +#undef org_sqlite_jni_capi_CApi_SQLITE_FULL +#define org_sqlite_jni_capi_CApi_SQLITE_FULL 13L +#undef org_sqlite_jni_capi_CApi_SQLITE_CANTOPEN +#define org_sqlite_jni_capi_CApi_SQLITE_CANTOPEN 14L +#undef org_sqlite_jni_capi_CApi_SQLITE_PROTOCOL +#define org_sqlite_jni_capi_CApi_SQLITE_PROTOCOL 15L +#undef org_sqlite_jni_capi_CApi_SQLITE_EMPTY +#define org_sqlite_jni_capi_CApi_SQLITE_EMPTY 16L +#undef org_sqlite_jni_capi_CApi_SQLITE_SCHEMA +#define org_sqlite_jni_capi_CApi_SQLITE_SCHEMA 17L +#undef org_sqlite_jni_capi_CApi_SQLITE_TOOBIG +#define org_sqlite_jni_capi_CApi_SQLITE_TOOBIG 18L +#undef org_sqlite_jni_capi_CApi_SQLITE_CONSTRAINT +#define org_sqlite_jni_capi_CApi_SQLITE_CONSTRAINT 19L +#undef org_sqlite_jni_capi_CApi_SQLITE_MISMATCH +#define org_sqlite_jni_capi_CApi_SQLITE_MISMATCH 20L +#undef org_sqlite_jni_capi_CApi_SQLITE_MISUSE +#define org_sqlite_jni_capi_CApi_SQLITE_MISUSE 21L +#undef org_sqlite_jni_capi_CApi_SQLITE_NOLFS +#define org_sqlite_jni_capi_CApi_SQLITE_NOLFS 22L +#undef org_sqlite_jni_capi_CApi_SQLITE_AUTH +#define org_sqlite_jni_capi_CApi_SQLITE_AUTH 23L +#undef org_sqlite_jni_capi_CApi_SQLITE_FORMAT +#define org_sqlite_jni_capi_CApi_SQLITE_FORMAT 24L +#undef org_sqlite_jni_capi_CApi_SQLITE_RANGE +#define org_sqlite_jni_capi_CApi_SQLITE_RANGE 25L +#undef org_sqlite_jni_capi_CApi_SQLITE_NOTADB +#define org_sqlite_jni_capi_CApi_SQLITE_NOTADB 26L +#undef org_sqlite_jni_capi_CApi_SQLITE_NOTICE +#define org_sqlite_jni_capi_CApi_SQLITE_NOTICE 27L +#undef org_sqlite_jni_capi_CApi_SQLITE_WARNING +#define org_sqlite_jni_capi_CApi_SQLITE_WARNING 28L +#undef org_sqlite_jni_capi_CApi_SQLITE_ROW +#define org_sqlite_jni_capi_CApi_SQLITE_ROW 100L +#undef org_sqlite_jni_capi_CApi_SQLITE_DONE +#define org_sqlite_jni_capi_CApi_SQLITE_DONE 101L +#undef org_sqlite_jni_capi_CApi_SQLITE_ERROR_MISSING_COLLSEQ +#define org_sqlite_jni_capi_CApi_SQLITE_ERROR_MISSING_COLLSEQ 257L +#undef org_sqlite_jni_capi_CApi_SQLITE_ERROR_RETRY +#define org_sqlite_jni_capi_CApi_SQLITE_ERROR_RETRY 513L +#undef org_sqlite_jni_capi_CApi_SQLITE_ERROR_SNAPSHOT +#define org_sqlite_jni_capi_CApi_SQLITE_ERROR_SNAPSHOT 769L +#undef org_sqlite_jni_capi_CApi_SQLITE_IOERR_READ +#define org_sqlite_jni_capi_CApi_SQLITE_IOERR_READ 266L +#undef org_sqlite_jni_capi_CApi_SQLITE_IOERR_SHORT_READ +#define org_sqlite_jni_capi_CApi_SQLITE_IOERR_SHORT_READ 522L +#undef org_sqlite_jni_capi_CApi_SQLITE_IOERR_WRITE +#define org_sqlite_jni_capi_CApi_SQLITE_IOERR_WRITE 778L +#undef org_sqlite_jni_capi_CApi_SQLITE_IOERR_FSYNC +#define org_sqlite_jni_capi_CApi_SQLITE_IOERR_FSYNC 1034L +#undef org_sqlite_jni_capi_CApi_SQLITE_IOERR_DIR_FSYNC +#define org_sqlite_jni_capi_CApi_SQLITE_IOERR_DIR_FSYNC 1290L +#undef org_sqlite_jni_capi_CApi_SQLITE_IOERR_TRUNCATE +#define org_sqlite_jni_capi_CApi_SQLITE_IOERR_TRUNCATE 1546L +#undef org_sqlite_jni_capi_CApi_SQLITE_IOERR_FSTAT +#define org_sqlite_jni_capi_CApi_SQLITE_IOERR_FSTAT 1802L +#undef org_sqlite_jni_capi_CApi_SQLITE_IOERR_UNLOCK +#define org_sqlite_jni_capi_CApi_SQLITE_IOERR_UNLOCK 2058L +#undef org_sqlite_jni_capi_CApi_SQLITE_IOERR_RDLOCK +#define org_sqlite_jni_capi_CApi_SQLITE_IOERR_RDLOCK 2314L +#undef org_sqlite_jni_capi_CApi_SQLITE_IOERR_DELETE +#define org_sqlite_jni_capi_CApi_SQLITE_IOERR_DELETE 2570L +#undef org_sqlite_jni_capi_CApi_SQLITE_IOERR_BLOCKED +#define org_sqlite_jni_capi_CApi_SQLITE_IOERR_BLOCKED 2826L +#undef org_sqlite_jni_capi_CApi_SQLITE_IOERR_NOMEM +#define org_sqlite_jni_capi_CApi_SQLITE_IOERR_NOMEM 3082L +#undef org_sqlite_jni_capi_CApi_SQLITE_IOERR_ACCESS +#define org_sqlite_jni_capi_CApi_SQLITE_IOERR_ACCESS 3338L +#undef org_sqlite_jni_capi_CApi_SQLITE_IOERR_CHECKRESERVEDLOCK +#define org_sqlite_jni_capi_CApi_SQLITE_IOERR_CHECKRESERVEDLOCK 3594L +#undef org_sqlite_jni_capi_CApi_SQLITE_IOERR_LOCK +#define org_sqlite_jni_capi_CApi_SQLITE_IOERR_LOCK 3850L +#undef org_sqlite_jni_capi_CApi_SQLITE_IOERR_CLOSE +#define org_sqlite_jni_capi_CApi_SQLITE_IOERR_CLOSE 4106L +#undef org_sqlite_jni_capi_CApi_SQLITE_IOERR_DIR_CLOSE +#define org_sqlite_jni_capi_CApi_SQLITE_IOERR_DIR_CLOSE 4362L +#undef org_sqlite_jni_capi_CApi_SQLITE_IOERR_SHMOPEN +#define org_sqlite_jni_capi_CApi_SQLITE_IOERR_SHMOPEN 4618L +#undef org_sqlite_jni_capi_CApi_SQLITE_IOERR_SHMSIZE +#define org_sqlite_jni_capi_CApi_SQLITE_IOERR_SHMSIZE 4874L +#undef org_sqlite_jni_capi_CApi_SQLITE_IOERR_SHMLOCK +#define org_sqlite_jni_capi_CApi_SQLITE_IOERR_SHMLOCK 5130L +#undef org_sqlite_jni_capi_CApi_SQLITE_IOERR_SHMMAP +#define org_sqlite_jni_capi_CApi_SQLITE_IOERR_SHMMAP 5386L +#undef org_sqlite_jni_capi_CApi_SQLITE_IOERR_SEEK +#define org_sqlite_jni_capi_CApi_SQLITE_IOERR_SEEK 5642L +#undef org_sqlite_jni_capi_CApi_SQLITE_IOERR_DELETE_NOENT +#define org_sqlite_jni_capi_CApi_SQLITE_IOERR_DELETE_NOENT 5898L +#undef org_sqlite_jni_capi_CApi_SQLITE_IOERR_MMAP +#define org_sqlite_jni_capi_CApi_SQLITE_IOERR_MMAP 6154L +#undef org_sqlite_jni_capi_CApi_SQLITE_IOERR_GETTEMPPATH +#define org_sqlite_jni_capi_CApi_SQLITE_IOERR_GETTEMPPATH 6410L +#undef org_sqlite_jni_capi_CApi_SQLITE_IOERR_CONVPATH +#define org_sqlite_jni_capi_CApi_SQLITE_IOERR_CONVPATH 6666L +#undef org_sqlite_jni_capi_CApi_SQLITE_IOERR_VNODE +#define org_sqlite_jni_capi_CApi_SQLITE_IOERR_VNODE 6922L +#undef org_sqlite_jni_capi_CApi_SQLITE_IOERR_AUTH +#define org_sqlite_jni_capi_CApi_SQLITE_IOERR_AUTH 7178L +#undef org_sqlite_jni_capi_CApi_SQLITE_IOERR_BEGIN_ATOMIC +#define org_sqlite_jni_capi_CApi_SQLITE_IOERR_BEGIN_ATOMIC 7434L +#undef org_sqlite_jni_capi_CApi_SQLITE_IOERR_COMMIT_ATOMIC +#define org_sqlite_jni_capi_CApi_SQLITE_IOERR_COMMIT_ATOMIC 7690L +#undef org_sqlite_jni_capi_CApi_SQLITE_IOERR_ROLLBACK_ATOMIC +#define org_sqlite_jni_capi_CApi_SQLITE_IOERR_ROLLBACK_ATOMIC 7946L +#undef org_sqlite_jni_capi_CApi_SQLITE_IOERR_DATA +#define org_sqlite_jni_capi_CApi_SQLITE_IOERR_DATA 8202L +#undef org_sqlite_jni_capi_CApi_SQLITE_IOERR_CORRUPTFS +#define org_sqlite_jni_capi_CApi_SQLITE_IOERR_CORRUPTFS 8458L +#undef org_sqlite_jni_capi_CApi_SQLITE_LOCKED_SHAREDCACHE +#define org_sqlite_jni_capi_CApi_SQLITE_LOCKED_SHAREDCACHE 262L +#undef org_sqlite_jni_capi_CApi_SQLITE_LOCKED_VTAB +#define org_sqlite_jni_capi_CApi_SQLITE_LOCKED_VTAB 518L +#undef org_sqlite_jni_capi_CApi_SQLITE_BUSY_RECOVERY +#define org_sqlite_jni_capi_CApi_SQLITE_BUSY_RECOVERY 261L +#undef org_sqlite_jni_capi_CApi_SQLITE_BUSY_SNAPSHOT +#define org_sqlite_jni_capi_CApi_SQLITE_BUSY_SNAPSHOT 517L +#undef org_sqlite_jni_capi_CApi_SQLITE_BUSY_TIMEOUT +#define org_sqlite_jni_capi_CApi_SQLITE_BUSY_TIMEOUT 773L +#undef org_sqlite_jni_capi_CApi_SQLITE_CANTOPEN_NOTEMPDIR +#define org_sqlite_jni_capi_CApi_SQLITE_CANTOPEN_NOTEMPDIR 270L +#undef org_sqlite_jni_capi_CApi_SQLITE_CANTOPEN_ISDIR +#define org_sqlite_jni_capi_CApi_SQLITE_CANTOPEN_ISDIR 526L +#undef org_sqlite_jni_capi_CApi_SQLITE_CANTOPEN_FULLPATH +#define org_sqlite_jni_capi_CApi_SQLITE_CANTOPEN_FULLPATH 782L +#undef org_sqlite_jni_capi_CApi_SQLITE_CANTOPEN_CONVPATH +#define org_sqlite_jni_capi_CApi_SQLITE_CANTOPEN_CONVPATH 1038L +#undef org_sqlite_jni_capi_CApi_SQLITE_CANTOPEN_SYMLINK +#define org_sqlite_jni_capi_CApi_SQLITE_CANTOPEN_SYMLINK 1550L +#undef org_sqlite_jni_capi_CApi_SQLITE_CORRUPT_VTAB +#define org_sqlite_jni_capi_CApi_SQLITE_CORRUPT_VTAB 267L +#undef org_sqlite_jni_capi_CApi_SQLITE_CORRUPT_SEQUENCE +#define org_sqlite_jni_capi_CApi_SQLITE_CORRUPT_SEQUENCE 523L +#undef org_sqlite_jni_capi_CApi_SQLITE_CORRUPT_INDEX +#define org_sqlite_jni_capi_CApi_SQLITE_CORRUPT_INDEX 779L +#undef org_sqlite_jni_capi_CApi_SQLITE_READONLY_RECOVERY +#define org_sqlite_jni_capi_CApi_SQLITE_READONLY_RECOVERY 264L +#undef org_sqlite_jni_capi_CApi_SQLITE_READONLY_CANTLOCK +#define org_sqlite_jni_capi_CApi_SQLITE_READONLY_CANTLOCK 520L +#undef org_sqlite_jni_capi_CApi_SQLITE_READONLY_ROLLBACK +#define org_sqlite_jni_capi_CApi_SQLITE_READONLY_ROLLBACK 776L +#undef org_sqlite_jni_capi_CApi_SQLITE_READONLY_DBMOVED +#define org_sqlite_jni_capi_CApi_SQLITE_READONLY_DBMOVED 1032L +#undef org_sqlite_jni_capi_CApi_SQLITE_READONLY_CANTINIT +#define org_sqlite_jni_capi_CApi_SQLITE_READONLY_CANTINIT 1288L +#undef org_sqlite_jni_capi_CApi_SQLITE_READONLY_DIRECTORY +#define org_sqlite_jni_capi_CApi_SQLITE_READONLY_DIRECTORY 1544L +#undef org_sqlite_jni_capi_CApi_SQLITE_ABORT_ROLLBACK +#define org_sqlite_jni_capi_CApi_SQLITE_ABORT_ROLLBACK 516L +#undef org_sqlite_jni_capi_CApi_SQLITE_CONSTRAINT_CHECK +#define org_sqlite_jni_capi_CApi_SQLITE_CONSTRAINT_CHECK 275L +#undef org_sqlite_jni_capi_CApi_SQLITE_CONSTRAINT_COMMITHOOK +#define org_sqlite_jni_capi_CApi_SQLITE_CONSTRAINT_COMMITHOOK 531L +#undef org_sqlite_jni_capi_CApi_SQLITE_CONSTRAINT_FOREIGNKEY +#define org_sqlite_jni_capi_CApi_SQLITE_CONSTRAINT_FOREIGNKEY 787L +#undef org_sqlite_jni_capi_CApi_SQLITE_CONSTRAINT_FUNCTION +#define org_sqlite_jni_capi_CApi_SQLITE_CONSTRAINT_FUNCTION 1043L +#undef org_sqlite_jni_capi_CApi_SQLITE_CONSTRAINT_NOTNULL +#define org_sqlite_jni_capi_CApi_SQLITE_CONSTRAINT_NOTNULL 1299L +#undef org_sqlite_jni_capi_CApi_SQLITE_CONSTRAINT_PRIMARYKEY +#define org_sqlite_jni_capi_CApi_SQLITE_CONSTRAINT_PRIMARYKEY 1555L +#undef org_sqlite_jni_capi_CApi_SQLITE_CONSTRAINT_TRIGGER +#define org_sqlite_jni_capi_CApi_SQLITE_CONSTRAINT_TRIGGER 1811L +#undef org_sqlite_jni_capi_CApi_SQLITE_CONSTRAINT_UNIQUE +#define org_sqlite_jni_capi_CApi_SQLITE_CONSTRAINT_UNIQUE 2067L +#undef org_sqlite_jni_capi_CApi_SQLITE_CONSTRAINT_VTAB +#define org_sqlite_jni_capi_CApi_SQLITE_CONSTRAINT_VTAB 2323L +#undef org_sqlite_jni_capi_CApi_SQLITE_CONSTRAINT_ROWID +#define org_sqlite_jni_capi_CApi_SQLITE_CONSTRAINT_ROWID 2579L +#undef org_sqlite_jni_capi_CApi_SQLITE_CONSTRAINT_PINNED +#define org_sqlite_jni_capi_CApi_SQLITE_CONSTRAINT_PINNED 2835L +#undef org_sqlite_jni_capi_CApi_SQLITE_CONSTRAINT_DATATYPE +#define org_sqlite_jni_capi_CApi_SQLITE_CONSTRAINT_DATATYPE 3091L +#undef org_sqlite_jni_capi_CApi_SQLITE_NOTICE_RECOVER_WAL +#define org_sqlite_jni_capi_CApi_SQLITE_NOTICE_RECOVER_WAL 283L +#undef org_sqlite_jni_capi_CApi_SQLITE_NOTICE_RECOVER_ROLLBACK +#define org_sqlite_jni_capi_CApi_SQLITE_NOTICE_RECOVER_ROLLBACK 539L +#undef org_sqlite_jni_capi_CApi_SQLITE_WARNING_AUTOINDEX +#define org_sqlite_jni_capi_CApi_SQLITE_WARNING_AUTOINDEX 284L +#undef org_sqlite_jni_capi_CApi_SQLITE_AUTH_USER +#define org_sqlite_jni_capi_CApi_SQLITE_AUTH_USER 279L +#undef org_sqlite_jni_capi_CApi_SQLITE_OK_LOAD_PERMANENTLY +#define org_sqlite_jni_capi_CApi_SQLITE_OK_LOAD_PERMANENTLY 256L +#undef org_sqlite_jni_capi_CApi_SQLITE_SERIALIZE_NOCOPY +#define org_sqlite_jni_capi_CApi_SQLITE_SERIALIZE_NOCOPY 1L +#undef org_sqlite_jni_capi_CApi_SQLITE_DESERIALIZE_FREEONCLOSE +#define org_sqlite_jni_capi_CApi_SQLITE_DESERIALIZE_FREEONCLOSE 1L +#undef org_sqlite_jni_capi_CApi_SQLITE_DESERIALIZE_READONLY +#define org_sqlite_jni_capi_CApi_SQLITE_DESERIALIZE_READONLY 4L +#undef org_sqlite_jni_capi_CApi_SQLITE_DESERIALIZE_RESIZEABLE +#define org_sqlite_jni_capi_CApi_SQLITE_DESERIALIZE_RESIZEABLE 2L +#undef org_sqlite_jni_capi_CApi_SQLITE_SESSION_CONFIG_STRMSIZE +#define org_sqlite_jni_capi_CApi_SQLITE_SESSION_CONFIG_STRMSIZE 1L +#undef org_sqlite_jni_capi_CApi_SQLITE_SESSION_OBJCONFIG_SIZE +#define org_sqlite_jni_capi_CApi_SQLITE_SESSION_OBJCONFIG_SIZE 1L +#undef org_sqlite_jni_capi_CApi_SQLITE_STATUS_MEMORY_USED +#define org_sqlite_jni_capi_CApi_SQLITE_STATUS_MEMORY_USED 0L +#undef org_sqlite_jni_capi_CApi_SQLITE_STATUS_PAGECACHE_USED +#define org_sqlite_jni_capi_CApi_SQLITE_STATUS_PAGECACHE_USED 1L +#undef org_sqlite_jni_capi_CApi_SQLITE_STATUS_PAGECACHE_OVERFLOW +#define org_sqlite_jni_capi_CApi_SQLITE_STATUS_PAGECACHE_OVERFLOW 2L +#undef org_sqlite_jni_capi_CApi_SQLITE_STATUS_MALLOC_SIZE +#define org_sqlite_jni_capi_CApi_SQLITE_STATUS_MALLOC_SIZE 5L +#undef org_sqlite_jni_capi_CApi_SQLITE_STATUS_PARSER_STACK +#define org_sqlite_jni_capi_CApi_SQLITE_STATUS_PARSER_STACK 6L +#undef org_sqlite_jni_capi_CApi_SQLITE_STATUS_PAGECACHE_SIZE +#define org_sqlite_jni_capi_CApi_SQLITE_STATUS_PAGECACHE_SIZE 7L +#undef org_sqlite_jni_capi_CApi_SQLITE_STATUS_MALLOC_COUNT +#define org_sqlite_jni_capi_CApi_SQLITE_STATUS_MALLOC_COUNT 9L +#undef org_sqlite_jni_capi_CApi_SQLITE_STMTSTATUS_FULLSCAN_STEP +#define org_sqlite_jni_capi_CApi_SQLITE_STMTSTATUS_FULLSCAN_STEP 1L +#undef org_sqlite_jni_capi_CApi_SQLITE_STMTSTATUS_SORT +#define org_sqlite_jni_capi_CApi_SQLITE_STMTSTATUS_SORT 2L +#undef org_sqlite_jni_capi_CApi_SQLITE_STMTSTATUS_AUTOINDEX +#define org_sqlite_jni_capi_CApi_SQLITE_STMTSTATUS_AUTOINDEX 3L +#undef org_sqlite_jni_capi_CApi_SQLITE_STMTSTATUS_VM_STEP +#define org_sqlite_jni_capi_CApi_SQLITE_STMTSTATUS_VM_STEP 4L +#undef org_sqlite_jni_capi_CApi_SQLITE_STMTSTATUS_REPREPARE +#define org_sqlite_jni_capi_CApi_SQLITE_STMTSTATUS_REPREPARE 5L +#undef org_sqlite_jni_capi_CApi_SQLITE_STMTSTATUS_RUN +#define org_sqlite_jni_capi_CApi_SQLITE_STMTSTATUS_RUN 6L +#undef org_sqlite_jni_capi_CApi_SQLITE_STMTSTATUS_FILTER_MISS +#define org_sqlite_jni_capi_CApi_SQLITE_STMTSTATUS_FILTER_MISS 7L +#undef org_sqlite_jni_capi_CApi_SQLITE_STMTSTATUS_FILTER_HIT +#define org_sqlite_jni_capi_CApi_SQLITE_STMTSTATUS_FILTER_HIT 8L +#undef org_sqlite_jni_capi_CApi_SQLITE_STMTSTATUS_MEMUSED +#define org_sqlite_jni_capi_CApi_SQLITE_STMTSTATUS_MEMUSED 99L +#undef org_sqlite_jni_capi_CApi_SQLITE_SYNC_NORMAL +#define org_sqlite_jni_capi_CApi_SQLITE_SYNC_NORMAL 2L +#undef org_sqlite_jni_capi_CApi_SQLITE_SYNC_FULL +#define org_sqlite_jni_capi_CApi_SQLITE_SYNC_FULL 3L +#undef org_sqlite_jni_capi_CApi_SQLITE_SYNC_DATAONLY +#define org_sqlite_jni_capi_CApi_SQLITE_SYNC_DATAONLY 16L +#undef org_sqlite_jni_capi_CApi_SQLITE_TRACE_STMT +#define org_sqlite_jni_capi_CApi_SQLITE_TRACE_STMT 1L +#undef org_sqlite_jni_capi_CApi_SQLITE_TRACE_PROFILE +#define org_sqlite_jni_capi_CApi_SQLITE_TRACE_PROFILE 2L +#undef org_sqlite_jni_capi_CApi_SQLITE_TRACE_ROW +#define org_sqlite_jni_capi_CApi_SQLITE_TRACE_ROW 4L +#undef org_sqlite_jni_capi_CApi_SQLITE_TRACE_CLOSE +#define org_sqlite_jni_capi_CApi_SQLITE_TRACE_CLOSE 8L +#undef org_sqlite_jni_capi_CApi_SQLITE_TXN_NONE +#define org_sqlite_jni_capi_CApi_SQLITE_TXN_NONE 0L +#undef org_sqlite_jni_capi_CApi_SQLITE_TXN_READ +#define org_sqlite_jni_capi_CApi_SQLITE_TXN_READ 1L +#undef org_sqlite_jni_capi_CApi_SQLITE_TXN_WRITE +#define org_sqlite_jni_capi_CApi_SQLITE_TXN_WRITE 2L +#undef org_sqlite_jni_capi_CApi_SQLITE_DETERMINISTIC +#define org_sqlite_jni_capi_CApi_SQLITE_DETERMINISTIC 2048L +#undef org_sqlite_jni_capi_CApi_SQLITE_DIRECTONLY +#define org_sqlite_jni_capi_CApi_SQLITE_DIRECTONLY 524288L +#undef org_sqlite_jni_capi_CApi_SQLITE_SUBTYPE +#define org_sqlite_jni_capi_CApi_SQLITE_SUBTYPE 1048576L +#undef org_sqlite_jni_capi_CApi_SQLITE_INNOCUOUS +#define org_sqlite_jni_capi_CApi_SQLITE_INNOCUOUS 2097152L +#undef org_sqlite_jni_capi_CApi_SQLITE_RESULT_SUBTYPE +#define org_sqlite_jni_capi_CApi_SQLITE_RESULT_SUBTYPE 16777216L +#undef org_sqlite_jni_capi_CApi_SQLITE_INDEX_SCAN_UNIQUE +#define org_sqlite_jni_capi_CApi_SQLITE_INDEX_SCAN_UNIQUE 1L +#undef org_sqlite_jni_capi_CApi_SQLITE_INDEX_CONSTRAINT_EQ +#define org_sqlite_jni_capi_CApi_SQLITE_INDEX_CONSTRAINT_EQ 2L +#undef org_sqlite_jni_capi_CApi_SQLITE_INDEX_CONSTRAINT_GT +#define org_sqlite_jni_capi_CApi_SQLITE_INDEX_CONSTRAINT_GT 4L +#undef org_sqlite_jni_capi_CApi_SQLITE_INDEX_CONSTRAINT_LE +#define org_sqlite_jni_capi_CApi_SQLITE_INDEX_CONSTRAINT_LE 8L +#undef org_sqlite_jni_capi_CApi_SQLITE_INDEX_CONSTRAINT_LT +#define org_sqlite_jni_capi_CApi_SQLITE_INDEX_CONSTRAINT_LT 16L +#undef org_sqlite_jni_capi_CApi_SQLITE_INDEX_CONSTRAINT_GE +#define org_sqlite_jni_capi_CApi_SQLITE_INDEX_CONSTRAINT_GE 32L +#undef org_sqlite_jni_capi_CApi_SQLITE_INDEX_CONSTRAINT_MATCH +#define org_sqlite_jni_capi_CApi_SQLITE_INDEX_CONSTRAINT_MATCH 64L +#undef org_sqlite_jni_capi_CApi_SQLITE_INDEX_CONSTRAINT_LIKE +#define org_sqlite_jni_capi_CApi_SQLITE_INDEX_CONSTRAINT_LIKE 65L +#undef org_sqlite_jni_capi_CApi_SQLITE_INDEX_CONSTRAINT_GLOB +#define org_sqlite_jni_capi_CApi_SQLITE_INDEX_CONSTRAINT_GLOB 66L +#undef org_sqlite_jni_capi_CApi_SQLITE_INDEX_CONSTRAINT_REGEXP +#define org_sqlite_jni_capi_CApi_SQLITE_INDEX_CONSTRAINT_REGEXP 67L +#undef org_sqlite_jni_capi_CApi_SQLITE_INDEX_CONSTRAINT_NE +#define org_sqlite_jni_capi_CApi_SQLITE_INDEX_CONSTRAINT_NE 68L +#undef org_sqlite_jni_capi_CApi_SQLITE_INDEX_CONSTRAINT_ISNOT +#define org_sqlite_jni_capi_CApi_SQLITE_INDEX_CONSTRAINT_ISNOT 69L +#undef org_sqlite_jni_capi_CApi_SQLITE_INDEX_CONSTRAINT_ISNOTNULL +#define org_sqlite_jni_capi_CApi_SQLITE_INDEX_CONSTRAINT_ISNOTNULL 70L +#undef org_sqlite_jni_capi_CApi_SQLITE_INDEX_CONSTRAINT_ISNULL +#define org_sqlite_jni_capi_CApi_SQLITE_INDEX_CONSTRAINT_ISNULL 71L +#undef org_sqlite_jni_capi_CApi_SQLITE_INDEX_CONSTRAINT_IS +#define org_sqlite_jni_capi_CApi_SQLITE_INDEX_CONSTRAINT_IS 72L +#undef org_sqlite_jni_capi_CApi_SQLITE_INDEX_CONSTRAINT_LIMIT +#define org_sqlite_jni_capi_CApi_SQLITE_INDEX_CONSTRAINT_LIMIT 73L +#undef org_sqlite_jni_capi_CApi_SQLITE_INDEX_CONSTRAINT_OFFSET +#define org_sqlite_jni_capi_CApi_SQLITE_INDEX_CONSTRAINT_OFFSET 74L +#undef org_sqlite_jni_capi_CApi_SQLITE_INDEX_CONSTRAINT_FUNCTION +#define org_sqlite_jni_capi_CApi_SQLITE_INDEX_CONSTRAINT_FUNCTION 150L +#undef org_sqlite_jni_capi_CApi_SQLITE_VTAB_CONSTRAINT_SUPPORT +#define org_sqlite_jni_capi_CApi_SQLITE_VTAB_CONSTRAINT_SUPPORT 1L +#undef org_sqlite_jni_capi_CApi_SQLITE_VTAB_INNOCUOUS +#define org_sqlite_jni_capi_CApi_SQLITE_VTAB_INNOCUOUS 2L +#undef org_sqlite_jni_capi_CApi_SQLITE_VTAB_DIRECTONLY +#define org_sqlite_jni_capi_CApi_SQLITE_VTAB_DIRECTONLY 3L +#undef org_sqlite_jni_capi_CApi_SQLITE_VTAB_USES_ALL_SCHEMAS +#define org_sqlite_jni_capi_CApi_SQLITE_VTAB_USES_ALL_SCHEMAS 4L +#undef org_sqlite_jni_capi_CApi_SQLITE_ROLLBACK +#define org_sqlite_jni_capi_CApi_SQLITE_ROLLBACK 1L +#undef org_sqlite_jni_capi_CApi_SQLITE_FAIL +#define org_sqlite_jni_capi_CApi_SQLITE_FAIL 3L +#undef org_sqlite_jni_capi_CApi_SQLITE_REPLACE +#define org_sqlite_jni_capi_CApi_SQLITE_REPLACE 5L +/* + * Class: org_sqlite_jni_capi_CApi + * Method: init + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_org_sqlite_jni_capi_CApi_init + (JNIEnv *, jclass); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_java_uncache_thread + * Signature: ()Z + */ +JNIEXPORT jboolean JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1java_1uncache_1thread + (JNIEnv *, jclass); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_jni_supports_nio + * Signature: ()Z + */ +JNIEXPORT jboolean JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1jni_1supports_1nio + (JNIEnv *, jclass); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_jni_db_error + * Signature: (Lorg/sqlite/jni/capi/sqlite3;ILjava/lang/String;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1jni_1db_1error + (JNIEnv *, jclass, jobject, jint, jstring); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_aggregate_context + * Signature: (Lorg/sqlite/jni/capi/sqlite3_context;Z)J + */ +JNIEXPORT jlong JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1aggregate_1context + (JNIEnv *, jclass, jobject, jboolean); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_auto_extension + * Signature: (Lorg/sqlite/jni/capi/AutoExtensionCallback;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1auto_1extension + (JNIEnv *, jclass, jobject); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_backup_finish + * Signature: (J)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1backup_1finish + (JNIEnv *, jclass, jlong); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_backup_init + * Signature: (JLjava/lang/String;JLjava/lang/String;)Lorg/sqlite/jni/capi/sqlite3_backup; + */ +JNIEXPORT jobject JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1backup_1init + (JNIEnv *, jclass, jlong, jstring, jlong, jstring); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_backup_pagecount + * Signature: (J)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1backup_1pagecount + (JNIEnv *, jclass, jlong); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_backup_remaining + * Signature: (J)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1backup_1remaining + (JNIEnv *, jclass, jlong); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_backup_step + * Signature: (JI)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1backup_1step + (JNIEnv *, jclass, jlong, jint); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_bind_blob + * Signature: (JI[BI)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1bind_1blob + (JNIEnv *, jclass, jlong, jint, jbyteArray, jint); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_bind_double + * Signature: (JID)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1bind_1double + (JNIEnv *, jclass, jlong, jint, jdouble); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_bind_int + * Signature: (JII)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1bind_1int + (JNIEnv *, jclass, jlong, jint, jint); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_bind_int64 + * Signature: (JIJ)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1bind_1int64 + (JNIEnv *, jclass, jlong, jint, jlong); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_bind_java_object + * Signature: (JILjava/lang/Object;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1bind_1java_1object + (JNIEnv *, jclass, jlong, jint, jobject); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_bind_nio_buffer + * Signature: (Lorg/sqlite/jni/capi/sqlite3_stmt;ILjava/nio/ByteBuffer;II)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1bind_1nio_1buffer + (JNIEnv *, jclass, jobject, jint, jobject, jint, jint); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_bind_null + * Signature: (JI)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1bind_1null + (JNIEnv *, jclass, jlong, jint); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_bind_parameter_count + * Signature: (J)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1bind_1parameter_1count + (JNIEnv *, jclass, jlong); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_bind_parameter_index + * Signature: (J[B)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1bind_1parameter_1index + (JNIEnv *, jclass, jlong, jbyteArray); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_bind_parameter_name + * Signature: (JI)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1bind_1parameter_1name + (JNIEnv *, jclass, jlong, jint); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_bind_text + * Signature: (JI[BI)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1bind_1text + (JNIEnv *, jclass, jlong, jint, jbyteArray, jint); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_bind_text16 + * Signature: (JI[BI)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1bind_1text16 + (JNIEnv *, jclass, jlong, jint, jbyteArray, jint); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_bind_value + * Signature: (JIJ)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1bind_1value + (JNIEnv *, jclass, jlong, jint, jlong); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_bind_zeroblob + * Signature: (JII)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1bind_1zeroblob + (JNIEnv *, jclass, jlong, jint, jint); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_bind_zeroblob64 + * Signature: (JIJ)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1bind_1zeroblob64 + (JNIEnv *, jclass, jlong, jint, jlong); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_blob_bytes + * Signature: (J)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1blob_1bytes + (JNIEnv *, jclass, jlong); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_blob_close + * Signature: (J)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1blob_1close + (JNIEnv *, jclass, jlong); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_blob_open + * Signature: (JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;JILorg/sqlite/jni/capi/OutputPointer/sqlite3_blob;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1blob_1open + (JNIEnv *, jclass, jlong, jstring, jstring, jstring, jlong, jint, jobject); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_blob_read + * Signature: (J[BI)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1blob_1read + (JNIEnv *, jclass, jlong, jbyteArray, jint); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_blob_read_nio_buffer + * Signature: (JILjava/nio/ByteBuffer;II)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1blob_1read_1nio_1buffer + (JNIEnv *, jclass, jlong, jint, jobject, jint, jint); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_blob_reopen + * Signature: (JJ)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1blob_1reopen + (JNIEnv *, jclass, jlong, jlong); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_blob_write + * Signature: (J[BI)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1blob_1write + (JNIEnv *, jclass, jlong, jbyteArray, jint); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_blob_write_nio_buffer + * Signature: (JILjava/nio/ByteBuffer;II)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1blob_1write_1nio_1buffer + (JNIEnv *, jclass, jlong, jint, jobject, jint, jint); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_busy_handler + * Signature: (JLorg/sqlite/jni/capi/BusyHandlerCallback;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1busy_1handler + (JNIEnv *, jclass, jlong, jobject); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_busy_timeout + * Signature: (JI)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1busy_1timeout + (JNIEnv *, jclass, jlong, jint); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_cancel_auto_extension + * Signature: (Lorg/sqlite/jni/capi/AutoExtensionCallback;)Z + */ +JNIEXPORT jboolean JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1cancel_1auto_1extension + (JNIEnv *, jclass, jobject); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_changes + * Signature: (J)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1changes + (JNIEnv *, jclass, jlong); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_changes64 + * Signature: (J)J + */ +JNIEXPORT jlong JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1changes64 + (JNIEnv *, jclass, jlong); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_clear_bindings + * Signature: (J)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1clear_1bindings + (JNIEnv *, jclass, jlong); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_close + * Signature: (J)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1close + (JNIEnv *, jclass, jlong); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_close_v2 + * Signature: (J)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1close_1v2 + (JNIEnv *, jclass, jlong); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_column_blob + * Signature: (Lorg/sqlite/jni/capi/sqlite3_stmt;I)[B + */ +JNIEXPORT jbyteArray JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1column_1blob + (JNIEnv *, jclass, jobject, jint); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_column_bytes + * Signature: (JI)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1column_1bytes + (JNIEnv *, jclass, jlong, jint); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_column_bytes16 + * Signature: (JI)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1column_1bytes16 + (JNIEnv *, jclass, jlong, jint); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_column_count + * Signature: (J)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1column_1count + (JNIEnv *, jclass, jlong); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_column_database_name + * Signature: (JI)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1column_1database_1name + (JNIEnv *, jclass, jlong, jint); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_column_decltype + * Signature: (JI)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1column_1decltype + (JNIEnv *, jclass, jlong, jint); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_column_double + * Signature: (Lorg/sqlite/jni/capi/sqlite3_stmt;I)D + */ +JNIEXPORT jdouble JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1column_1double + (JNIEnv *, jclass, jobject, jint); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_column_int + * Signature: (Lorg/sqlite/jni/capi/sqlite3_stmt;I)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1column_1int + (JNIEnv *, jclass, jobject, jint); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_column_int64 + * Signature: (Lorg/sqlite/jni/capi/sqlite3_stmt;I)J + */ +JNIEXPORT jlong JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1column_1int64 + (JNIEnv *, jclass, jobject, jint); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_column_java_object + * Signature: (JI)Ljava/lang/Object; + */ +JNIEXPORT jobject JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1column_1java_1object + (JNIEnv *, jclass, jlong, jint); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_column_name + * Signature: (JI)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1column_1name + (JNIEnv *, jclass, jlong, jint); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_column_nio_buffer + * Signature: (Lorg/sqlite/jni/capi/sqlite3_stmt;I)Ljava/nio/ByteBuffer; + */ +JNIEXPORT jobject JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1column_1nio_1buffer + (JNIEnv *, jclass, jobject, jint); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_column_origin_name + * Signature: (JI)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1column_1origin_1name + (JNIEnv *, jclass, jlong, jint); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_column_table_name + * Signature: (JI)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1column_1table_1name + (JNIEnv *, jclass, jlong, jint); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_column_text + * Signature: (Lorg/sqlite/jni/capi/sqlite3_stmt;I)[B + */ +JNIEXPORT jbyteArray JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1column_1text + (JNIEnv *, jclass, jobject, jint); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_column_text16 + * Signature: (Lorg/sqlite/jni/capi/sqlite3_stmt;I)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1column_1text16 + (JNIEnv *, jclass, jobject, jint); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_column_type + * Signature: (JI)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1column_1type + (JNIEnv *, jclass, jlong, jint); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_column_value + * Signature: (Lorg/sqlite/jni/capi/sqlite3_stmt;I)Lorg/sqlite/jni/capi/sqlite3_value; + */ +JNIEXPORT jobject JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1column_1value + (JNIEnv *, jclass, jobject, jint); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_collation_needed + * Signature: (JLorg/sqlite/jni/capi/CollationNeededCallback;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1collation_1needed + (JNIEnv *, jclass, jlong, jobject); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_commit_hook + * Signature: (JLorg/sqlite/jni/capi/CommitHookCallback;)Lorg/sqlite/jni/capi/CommitHookCallback; + */ +JNIEXPORT jobject JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1commit_1hook + (JNIEnv *, jclass, jlong, jobject); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_compileoption_get + * Signature: (I)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1compileoption_1get + (JNIEnv *, jclass, jint); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_compileoption_used + * Signature: (Ljava/lang/String;)Z + */ +JNIEXPORT jboolean JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1compileoption_1used + (JNIEnv *, jclass, jstring); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_complete + * Signature: ([B)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1complete + (JNIEnv *, jclass, jbyteArray); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_config__enable + * Signature: (I)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1config_1_1enable + (JNIEnv *, jclass, jint); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_config__CONFIG_LOG + * Signature: (Lorg/sqlite/jni/capi/ConfigLogCallback;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1config_1_1CONFIG_1LOG + (JNIEnv *, jclass, jobject); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_config__SQLLOG + * Signature: (Lorg/sqlite/jni/capi/ConfigSqlLogCallback;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1config_1_1SQLLOG + (JNIEnv *, jclass, jobject); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_context_db_handle + * Signature: (Lorg/sqlite/jni/capi/sqlite3_context;)Lorg/sqlite/jni/capi/sqlite3; + */ +JNIEXPORT jobject JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1context_1db_1handle + (JNIEnv *, jclass, jobject); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_create_collation + * Signature: (Lorg/sqlite/jni/capi/sqlite3;Ljava/lang/String;ILorg/sqlite/jni/capi/CollationCallback;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1create_1collation + (JNIEnv *, jclass, jobject, jstring, jint, jobject); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_create_function + * Signature: (Lorg/sqlite/jni/capi/sqlite3;Ljava/lang/String;IILorg/sqlite/jni/capi/SQLFunction;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1create_1function + (JNIEnv *, jclass, jobject, jstring, jint, jint, jobject); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_data_count + * Signature: (J)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1data_1count + (JNIEnv *, jclass, jlong); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_db_config + * Signature: (Lorg/sqlite/jni/capi/sqlite3;IILorg/sqlite/jni/capi/OutputPointer/Int32;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1db_1config__Lorg_sqlite_jni_capi_sqlite3_2IILorg_sqlite_jni_capi_OutputPointer_Int32_2 + (JNIEnv *, jclass, jobject, jint, jint, jobject); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_db_config + * Signature: (Lorg/sqlite/jni/capi/sqlite3;ILjava/lang/String;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1db_1config__Lorg_sqlite_jni_capi_sqlite3_2ILjava_lang_String_2 + (JNIEnv *, jclass, jobject, jint, jstring); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_db_name + * Signature: (JI)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1db_1name + (JNIEnv *, jclass, jlong, jint); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_db_filename + * Signature: (Lorg/sqlite/jni/capi/sqlite3;Ljava/lang/String;)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1db_1filename + (JNIEnv *, jclass, jobject, jstring); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_db_handle + * Signature: (Lorg/sqlite/jni/capi/sqlite3_stmt;)Lorg/sqlite/jni/capi/sqlite3; + */ +JNIEXPORT jobject JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1db_1handle + (JNIEnv *, jclass, jobject); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_db_readonly + * Signature: (Lorg/sqlite/jni/capi/sqlite3;Ljava/lang/String;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1db_1readonly + (JNIEnv *, jclass, jobject, jstring); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_db_release_memory + * Signature: (Lorg/sqlite/jni/capi/sqlite3;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1db_1release_1memory + (JNIEnv *, jclass, jobject); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_db_status + * Signature: (Lorg/sqlite/jni/capi/sqlite3;ILorg/sqlite/jni/capi/OutputPointer/Int32;Lorg/sqlite/jni/capi/OutputPointer/Int32;Z)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1db_1status + (JNIEnv *, jclass, jobject, jint, jobject, jobject, jboolean); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_errcode + * Signature: (Lorg/sqlite/jni/capi/sqlite3;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1errcode + (JNIEnv *, jclass, jobject); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_errmsg + * Signature: (Lorg/sqlite/jni/capi/sqlite3;)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1errmsg + (JNIEnv *, jclass, jobject); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_set_errmsg + * Signature: (Lorg/sqlite/jni/capi/sqlite3;ILjava/lang/String;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1set_1errmsg + (JNIEnv *, jclass, jobject, jint, jstring); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_error_offset + * Signature: (J)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1error_1offset + (JNIEnv *, jclass, jlong); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_errstr + * Signature: (I)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1errstr + (JNIEnv *, jclass, jint); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_expanded_sql + * Signature: (Lorg/sqlite/jni/capi/sqlite3_stmt;)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1expanded_1sql + (JNIEnv *, jclass, jobject); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_extended_errcode + * Signature: (J)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1extended_1errcode + (JNIEnv *, jclass, jlong); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_extended_result_codes + * Signature: (Lorg/sqlite/jni/capi/sqlite3;Z)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1extended_1result_1codes + (JNIEnv *, jclass, jobject, jboolean); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_get_autocommit + * Signature: (J)Z + */ +JNIEXPORT jboolean JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1get_1autocommit + (JNIEnv *, jclass, jlong); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_get_auxdata + * Signature: (Lorg/sqlite/jni/capi/sqlite3_context;I)Ljava/lang/Object; + */ +JNIEXPORT jobject JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1get_1auxdata + (JNIEnv *, jclass, jobject, jint); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_finalize + * Signature: (J)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1finalize + (JNIEnv *, jclass, jlong); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_initialize + * Signature: ()I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1initialize + (JNIEnv *, jclass); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_interrupt + * Signature: (Lorg/sqlite/jni/capi/sqlite3;)V + */ +JNIEXPORT void JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1interrupt + (JNIEnv *, jclass, jobject); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_is_interrupted + * Signature: (Lorg/sqlite/jni/capi/sqlite3;)Z + */ +JNIEXPORT jboolean JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1is_1interrupted + (JNIEnv *, jclass, jobject); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_keyword_check + * Signature: (Ljava/lang/String;)Z + */ +JNIEXPORT jboolean JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1keyword_1check + (JNIEnv *, jclass, jstring); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_keyword_count + * Signature: ()I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1keyword_1count + (JNIEnv *, jclass); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_keyword_name + * Signature: (I)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1keyword_1name + (JNIEnv *, jclass, jint); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_last_insert_rowid + * Signature: (Lorg/sqlite/jni/capi/sqlite3;)J + */ +JNIEXPORT jlong JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1last_1insert_1rowid + (JNIEnv *, jclass, jobject); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_libversion + * Signature: ()Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1libversion + (JNIEnv *, jclass); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_libversion_number + * Signature: ()I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1libversion_1number + (JNIEnv *, jclass); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_limit + * Signature: (Lorg/sqlite/jni/capi/sqlite3;II)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1limit + (JNIEnv *, jclass, jobject, jint, jint); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_normalized_sql + * Signature: (Lorg/sqlite/jni/capi/sqlite3_stmt;)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1normalized_1sql + (JNIEnv *, jclass, jobject); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_open + * Signature: (Ljava/lang/String;Lorg/sqlite/jni/capi/OutputPointer/sqlite3;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1open + (JNIEnv *, jclass, jstring, jobject); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_open_v2 + * Signature: (Ljava/lang/String;Lorg/sqlite/jni/capi/OutputPointer/sqlite3;ILjava/lang/String;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1open_1v2 + (JNIEnv *, jclass, jstring, jobject, jint, jstring); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_prepare + * Signature: (J[BILorg/sqlite/jni/capi/OutputPointer/sqlite3_stmt;Lorg/sqlite/jni/capi/OutputPointer/Int32;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1prepare + (JNIEnv *, jclass, jlong, jbyteArray, jint, jobject, jobject); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_prepare_v2 + * Signature: (J[BILorg/sqlite/jni/capi/OutputPointer/sqlite3_stmt;Lorg/sqlite/jni/capi/OutputPointer/Int32;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1prepare_1v2 + (JNIEnv *, jclass, jlong, jbyteArray, jint, jobject, jobject); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_prepare_v3 + * Signature: (J[BIILorg/sqlite/jni/capi/OutputPointer/sqlite3_stmt;Lorg/sqlite/jni/capi/OutputPointer/Int32;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1prepare_1v3 + (JNIEnv *, jclass, jlong, jbyteArray, jint, jint, jobject, jobject); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_preupdate_blobwrite + * Signature: (J)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1preupdate_1blobwrite + (JNIEnv *, jclass, jlong); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_preupdate_count + * Signature: (J)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1preupdate_1count + (JNIEnv *, jclass, jlong); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_preupdate_depth + * Signature: (J)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1preupdate_1depth + (JNIEnv *, jclass, jlong); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_preupdate_hook + * Signature: (JLorg/sqlite/jni/capi/PreupdateHookCallback;)Lorg/sqlite/jni/capi/PreupdateHookCallback; + */ +JNIEXPORT jobject JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1preupdate_1hook + (JNIEnv *, jclass, jlong, jobject); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_preupdate_new + * Signature: (JILorg/sqlite/jni/capi/OutputPointer/sqlite3_value;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1preupdate_1new + (JNIEnv *, jclass, jlong, jint, jobject); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_preupdate_old + * Signature: (JILorg/sqlite/jni/capi/OutputPointer/sqlite3_value;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1preupdate_1old + (JNIEnv *, jclass, jlong, jint, jobject); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_progress_handler + * Signature: (Lorg/sqlite/jni/capi/sqlite3;ILorg/sqlite/jni/capi/ProgressHandlerCallback;)V + */ +JNIEXPORT void JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1progress_1handler + (JNIEnv *, jclass, jobject, jint, jobject); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_randomness + * Signature: ([B)V + */ +JNIEXPORT void JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1randomness + (JNIEnv *, jclass, jbyteArray); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_release_memory + * Signature: (I)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1release_1memory + (JNIEnv *, jclass, jint); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_reset + * Signature: (Lorg/sqlite/jni/capi/sqlite3_stmt;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1reset + (JNIEnv *, jclass, jobject); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_reset_auto_extension + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1reset_1auto_1extension + (JNIEnv *, jclass); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_result_double + * Signature: (Lorg/sqlite/jni/capi/sqlite3_context;D)V + */ +JNIEXPORT void JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1result_1double + (JNIEnv *, jclass, jobject, jdouble); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_result_error + * Signature: (Lorg/sqlite/jni/capi/sqlite3_context;[BI)V + */ +JNIEXPORT void JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1result_1error + (JNIEnv *, jclass, jobject, jbyteArray, jint); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_result_error_toobig + * Signature: (Lorg/sqlite/jni/capi/sqlite3_context;)V + */ +JNIEXPORT void JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1result_1error_1toobig + (JNIEnv *, jclass, jobject); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_result_error_nomem + * Signature: (Lorg/sqlite/jni/capi/sqlite3_context;)V + */ +JNIEXPORT void JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1result_1error_1nomem + (JNIEnv *, jclass, jobject); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_result_error_code + * Signature: (Lorg/sqlite/jni/capi/sqlite3_context;I)V + */ +JNIEXPORT void JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1result_1error_1code + (JNIEnv *, jclass, jobject, jint); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_result_int + * Signature: (Lorg/sqlite/jni/capi/sqlite3_context;I)V + */ +JNIEXPORT void JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1result_1int + (JNIEnv *, jclass, jobject, jint); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_result_int64 + * Signature: (Lorg/sqlite/jni/capi/sqlite3_context;J)V + */ +JNIEXPORT void JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1result_1int64 + (JNIEnv *, jclass, jobject, jlong); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_result_java_object + * Signature: (Lorg/sqlite/jni/capi/sqlite3_context;Ljava/lang/Object;)V + */ +JNIEXPORT void JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1result_1java_1object + (JNIEnv *, jclass, jobject, jobject); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_result_nio_buffer + * Signature: (Lorg/sqlite/jni/capi/sqlite3_context;Ljava/nio/ByteBuffer;II)V + */ +JNIEXPORT void JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1result_1nio_1buffer + (JNIEnv *, jclass, jobject, jobject, jint, jint); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_result_null + * Signature: (Lorg/sqlite/jni/capi/sqlite3_context;)V + */ +JNIEXPORT void JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1result_1null + (JNIEnv *, jclass, jobject); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_result_subtype + * Signature: (Lorg/sqlite/jni/capi/sqlite3_context;I)V + */ +JNIEXPORT void JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1result_1subtype + (JNIEnv *, jclass, jobject, jint); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_result_value + * Signature: (Lorg/sqlite/jni/capi/sqlite3_context;Lorg/sqlite/jni/capi/sqlite3_value;)V + */ +JNIEXPORT void JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1result_1value + (JNIEnv *, jclass, jobject, jobject); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_result_zeroblob + * Signature: (Lorg/sqlite/jni/capi/sqlite3_context;I)V + */ +JNIEXPORT void JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1result_1zeroblob + (JNIEnv *, jclass, jobject, jint); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_result_zeroblob64 + * Signature: (Lorg/sqlite/jni/capi/sqlite3_context;J)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1result_1zeroblob64 + (JNIEnv *, jclass, jobject, jlong); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_result_blob + * Signature: (Lorg/sqlite/jni/capi/sqlite3_context;[BI)V + */ +JNIEXPORT void JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1result_1blob + (JNIEnv *, jclass, jobject, jbyteArray, jint); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_result_blob64 + * Signature: (Lorg/sqlite/jni/capi/sqlite3_context;[BJ)V + */ +JNIEXPORT void JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1result_1blob64 + (JNIEnv *, jclass, jobject, jbyteArray, jlong); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_result_text + * Signature: (Lorg/sqlite/jni/capi/sqlite3_context;[BI)V + */ +JNIEXPORT void JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1result_1text + (JNIEnv *, jclass, jobject, jbyteArray, jint); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_result_text64 + * Signature: (Lorg/sqlite/jni/capi/sqlite3_context;[BJI)V + */ +JNIEXPORT void JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1result_1text64 + (JNIEnv *, jclass, jobject, jbyteArray, jlong, jint); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_rollback_hook + * Signature: (JLorg/sqlite/jni/capi/RollbackHookCallback;)Lorg/sqlite/jni/capi/RollbackHookCallback; + */ +JNIEXPORT jobject JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1rollback_1hook + (JNIEnv *, jclass, jlong, jobject); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_set_authorizer + * Signature: (Lorg/sqlite/jni/capi/sqlite3;Lorg/sqlite/jni/capi/AuthorizerCallback;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1set_1authorizer + (JNIEnv *, jclass, jobject, jobject); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_set_auxdata + * Signature: (Lorg/sqlite/jni/capi/sqlite3_context;ILjava/lang/Object;)V + */ +JNIEXPORT void JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1set_1auxdata + (JNIEnv *, jclass, jobject, jint, jobject); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_set_last_insert_rowid + * Signature: (Lorg/sqlite/jni/capi/sqlite3;J)V + */ +JNIEXPORT void JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1set_1last_1insert_1rowid + (JNIEnv *, jclass, jobject, jlong); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_shutdown + * Signature: ()I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1shutdown + (JNIEnv *, jclass); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_sleep + * Signature: (I)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1sleep + (JNIEnv *, jclass, jint); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_sourceid + * Signature: ()Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1sourceid + (JNIEnv *, jclass); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_sql + * Signature: (Lorg/sqlite/jni/capi/sqlite3_stmt;)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1sql + (JNIEnv *, jclass, jobject); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_status + * Signature: (ILorg/sqlite/jni/capi/OutputPointer/Int32;Lorg/sqlite/jni/capi/OutputPointer/Int32;Z)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1status + (JNIEnv *, jclass, jint, jobject, jobject, jboolean); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_status64 + * Signature: (ILorg/sqlite/jni/capi/OutputPointer/Int64;Lorg/sqlite/jni/capi/OutputPointer/Int64;Z)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1status64 + (JNIEnv *, jclass, jint, jobject, jobject, jboolean); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_step + * Signature: (J)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1step + (JNIEnv *, jclass, jlong); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_stmt_busy + * Signature: (Lorg/sqlite/jni/capi/sqlite3_stmt;)Z + */ +JNIEXPORT jboolean JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1stmt_1busy + (JNIEnv *, jclass, jobject); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_stmt_explain + * Signature: (JI)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1stmt_1explain + (JNIEnv *, jclass, jlong, jint); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_stmt_isexplain + * Signature: (J)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1stmt_1isexplain + (JNIEnv *, jclass, jlong); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_stmt_readonly + * Signature: (Lorg/sqlite/jni/capi/sqlite3_stmt;)Z + */ +JNIEXPORT jboolean JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1stmt_1readonly + (JNIEnv *, jclass, jobject); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_stmt_status + * Signature: (Lorg/sqlite/jni/capi/sqlite3_stmt;IZ)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1stmt_1status + (JNIEnv *, jclass, jobject, jint, jboolean); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_strglob + * Signature: ([B[B)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1strglob + (JNIEnv *, jclass, jbyteArray, jbyteArray); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_strlike + * Signature: ([B[BI)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1strlike + (JNIEnv *, jclass, jbyteArray, jbyteArray, jint); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_system_errno + * Signature: (J)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1system_1errno + (JNIEnv *, jclass, jlong); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_table_column_metadata + * Signature: (Lorg/sqlite/jni/capi/sqlite3;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Lorg/sqlite/jni/capi/OutputPointer/String;Lorg/sqlite/jni/capi/OutputPointer/String;Lorg/sqlite/jni/capi/OutputPointer/Bool;Lorg/sqlite/jni/capi/OutputPointer/Bool;Lorg/sqlite/jni/capi/OutputPointer/Bool;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1table_1column_1metadata + (JNIEnv *, jclass, jobject, jstring, jstring, jstring, jobject, jobject, jobject, jobject, jobject); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_threadsafe + * Signature: ()I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1threadsafe + (JNIEnv *, jclass); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_total_changes + * Signature: (J)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1total_1changes + (JNIEnv *, jclass, jlong); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_total_changes64 + * Signature: (J)J + */ +JNIEXPORT jlong JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1total_1changes64 + (JNIEnv *, jclass, jlong); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_trace_v2 + * Signature: (Lorg/sqlite/jni/capi/sqlite3;ILorg/sqlite/jni/capi/TraceV2Callback;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1trace_1v2 + (JNIEnv *, jclass, jobject, jint, jobject); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_txn_state + * Signature: (Lorg/sqlite/jni/capi/sqlite3;Ljava/lang/String;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1txn_1state + (JNIEnv *, jclass, jobject, jstring); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_update_hook + * Signature: (JLorg/sqlite/jni/capi/UpdateHookCallback;)Lorg/sqlite/jni/capi/UpdateHookCallback; + */ +JNIEXPORT jobject JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1update_1hook + (JNIEnv *, jclass, jlong, jobject); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_value_blob + * Signature: (J)[B + */ +JNIEXPORT jbyteArray JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1value_1blob + (JNIEnv *, jclass, jlong); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_value_bytes + * Signature: (J)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1value_1bytes + (JNIEnv *, jclass, jlong); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_value_bytes16 + * Signature: (J)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1value_1bytes16 + (JNIEnv *, jclass, jlong); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_value_double + * Signature: (J)D + */ +JNIEXPORT jdouble JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1value_1double + (JNIEnv *, jclass, jlong); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_value_dup + * Signature: (J)Lorg/sqlite/jni/capi/sqlite3_value; + */ +JNIEXPORT jobject JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1value_1dup + (JNIEnv *, jclass, jlong); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_value_encoding + * Signature: (J)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1value_1encoding + (JNIEnv *, jclass, jlong); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_value_free + * Signature: (J)V + */ +JNIEXPORT void JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1value_1free + (JNIEnv *, jclass, jlong); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_value_frombind + * Signature: (J)Z + */ +JNIEXPORT jboolean JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1value_1frombind + (JNIEnv *, jclass, jlong); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_value_int + * Signature: (J)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1value_1int + (JNIEnv *, jclass, jlong); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_value_int64 + * Signature: (J)J + */ +JNIEXPORT jlong JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1value_1int64 + (JNIEnv *, jclass, jlong); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_value_java_object + * Signature: (J)Ljava/lang/Object; + */ +JNIEXPORT jobject JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1value_1java_1object + (JNIEnv *, jclass, jlong); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_value_nio_buffer + * Signature: (Lorg/sqlite/jni/capi/sqlite3_value;)Ljava/nio/ByteBuffer; + */ +JNIEXPORT jobject JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1value_1nio_1buffer + (JNIEnv *, jclass, jobject); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_value_nochange + * Signature: (J)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1value_1nochange + (JNIEnv *, jclass, jlong); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_value_numeric_type + * Signature: (J)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1value_1numeric_1type + (JNIEnv *, jclass, jlong); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_value_subtype + * Signature: (J)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1value_1subtype + (JNIEnv *, jclass, jlong); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_value_text + * Signature: (J)[B + */ +JNIEXPORT jbyteArray JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1value_1text + (JNIEnv *, jclass, jlong); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_value_text16 + * Signature: (J)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1value_1text16 + (JNIEnv *, jclass, jlong); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_value_type + * Signature: (J)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1value_1type + (JNIEnv *, jclass, jlong); + +/* + * Class: org_sqlite_jni_capi_CApi + * Method: sqlite3_jni_internal_details + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_org_sqlite_jni_capi_CApi_sqlite3_1jni_1internal_1details + (JNIEnv *, jclass); + +#ifdef __cplusplus +} +#endif +#endif +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include +/* Header for class org_sqlite_jni_capi_SQLTester */ + +#ifndef _Included_org_sqlite_jni_capi_SQLTester +#define _Included_org_sqlite_jni_capi_SQLTester +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: org_sqlite_jni_capi_SQLTester + * Method: strglob + * Signature: ([B[B)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_capi_SQLTester_strglob + (JNIEnv *, jclass, jbyteArray, jbyteArray); + +/* + * Class: org_sqlite_jni_capi_SQLTester + * Method: installCustomExtensions + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_org_sqlite_jni_capi_SQLTester_installCustomExtensions + (JNIEnv *, jclass); + +#ifdef __cplusplus +} +#endif +#endif +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include +/* Header for class org_sqlite_jni_fts5_Fts5ExtensionApi */ + +#ifndef _Included_org_sqlite_jni_fts5_Fts5ExtensionApi +#define _Included_org_sqlite_jni_fts5_Fts5ExtensionApi +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: org_sqlite_jni_fts5_Fts5ExtensionApi + * Method: getInstance + * Signature: ()Lorg/sqlite/jni/fts5/Fts5ExtensionApi; + */ +JNIEXPORT jobject JNICALL Java_org_sqlite_jni_fts5_Fts5ExtensionApi_getInstance + (JNIEnv *, jclass); + +/* + * Class: org_sqlite_jni_fts5_Fts5ExtensionApi + * Method: xColumnCount + * Signature: (Lorg/sqlite/jni/fts5/Fts5Context;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_fts5_Fts5ExtensionApi_xColumnCount + (JNIEnv *, jobject, jobject); + +/* + * Class: org_sqlite_jni_fts5_Fts5ExtensionApi + * Method: xColumnSize + * Signature: (Lorg/sqlite/jni/fts5/Fts5Context;ILorg/sqlite/jni/capi/OutputPointer/Int32;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_fts5_Fts5ExtensionApi_xColumnSize + (JNIEnv *, jobject, jobject, jint, jobject); + +/* + * Class: org_sqlite_jni_fts5_Fts5ExtensionApi + * Method: xColumnText + * Signature: (Lorg/sqlite/jni/fts5/Fts5Context;ILorg/sqlite/jni/capi/OutputPointer/String;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_fts5_Fts5ExtensionApi_xColumnText + (JNIEnv *, jobject, jobject, jint, jobject); + +/* + * Class: org_sqlite_jni_fts5_Fts5ExtensionApi + * Method: xColumnTotalSize + * Signature: (Lorg/sqlite/jni/fts5/Fts5Context;ILorg/sqlite/jni/capi/OutputPointer/Int64;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_fts5_Fts5ExtensionApi_xColumnTotalSize + (JNIEnv *, jobject, jobject, jint, jobject); + +/* + * Class: org_sqlite_jni_fts5_Fts5ExtensionApi + * Method: xGetAuxdata + * Signature: (Lorg/sqlite/jni/fts5/Fts5Context;Z)Ljava/lang/Object; + */ +JNIEXPORT jobject JNICALL Java_org_sqlite_jni_fts5_Fts5ExtensionApi_xGetAuxdata + (JNIEnv *, jobject, jobject, jboolean); + +/* + * Class: org_sqlite_jni_fts5_Fts5ExtensionApi + * Method: xInst + * Signature: (Lorg/sqlite/jni/fts5/Fts5Context;ILorg/sqlite/jni/capi/OutputPointer/Int32;Lorg/sqlite/jni/capi/OutputPointer/Int32;Lorg/sqlite/jni/capi/OutputPointer/Int32;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_fts5_Fts5ExtensionApi_xInst + (JNIEnv *, jobject, jobject, jint, jobject, jobject, jobject); + +/* + * Class: org_sqlite_jni_fts5_Fts5ExtensionApi + * Method: xInstCount + * Signature: (Lorg/sqlite/jni/fts5/Fts5Context;Lorg/sqlite/jni/capi/OutputPointer/Int32;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_fts5_Fts5ExtensionApi_xInstCount + (JNIEnv *, jobject, jobject, jobject); + +/* + * Class: org_sqlite_jni_fts5_Fts5ExtensionApi + * Method: xPhraseCount + * Signature: (Lorg/sqlite/jni/fts5/Fts5Context;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_fts5_Fts5ExtensionApi_xPhraseCount + (JNIEnv *, jobject, jobject); + +/* + * Class: org_sqlite_jni_fts5_Fts5ExtensionApi + * Method: xPhraseFirst + * Signature: (Lorg/sqlite/jni/fts5/Fts5Context;ILorg/sqlite/jni/fts5/Fts5PhraseIter;Lorg/sqlite/jni/capi/OutputPointer/Int32;Lorg/sqlite/jni/capi/OutputPointer/Int32;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_fts5_Fts5ExtensionApi_xPhraseFirst + (JNIEnv *, jobject, jobject, jint, jobject, jobject, jobject); + +/* + * Class: org_sqlite_jni_fts5_Fts5ExtensionApi + * Method: xPhraseFirstColumn + * Signature: (Lorg/sqlite/jni/fts5/Fts5Context;ILorg/sqlite/jni/fts5/Fts5PhraseIter;Lorg/sqlite/jni/capi/OutputPointer/Int32;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_fts5_Fts5ExtensionApi_xPhraseFirstColumn + (JNIEnv *, jobject, jobject, jint, jobject, jobject); + +/* + * Class: org_sqlite_jni_fts5_Fts5ExtensionApi + * Method: xPhraseNext + * Signature: (Lorg/sqlite/jni/fts5/Fts5Context;Lorg/sqlite/jni/fts5/Fts5PhraseIter;Lorg/sqlite/jni/capi/OutputPointer/Int32;Lorg/sqlite/jni/capi/OutputPointer/Int32;)V + */ +JNIEXPORT void JNICALL Java_org_sqlite_jni_fts5_Fts5ExtensionApi_xPhraseNext + (JNIEnv *, jobject, jobject, jobject, jobject, jobject); + +/* + * Class: org_sqlite_jni_fts5_Fts5ExtensionApi + * Method: xPhraseNextColumn + * Signature: (Lorg/sqlite/jni/fts5/Fts5Context;Lorg/sqlite/jni/fts5/Fts5PhraseIter;Lorg/sqlite/jni/capi/OutputPointer/Int32;)V + */ +JNIEXPORT void JNICALL Java_org_sqlite_jni_fts5_Fts5ExtensionApi_xPhraseNextColumn + (JNIEnv *, jobject, jobject, jobject, jobject); + +/* + * Class: org_sqlite_jni_fts5_Fts5ExtensionApi + * Method: xPhraseSize + * Signature: (Lorg/sqlite/jni/fts5/Fts5Context;I)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_fts5_Fts5ExtensionApi_xPhraseSize + (JNIEnv *, jobject, jobject, jint); + +/* + * Class: org_sqlite_jni_fts5_Fts5ExtensionApi + * Method: xQueryPhrase + * Signature: (Lorg/sqlite/jni/fts5/Fts5Context;ILorg/sqlite/jni/fts5/Fts5ExtensionApi/XQueryPhraseCallback;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_fts5_Fts5ExtensionApi_xQueryPhrase + (JNIEnv *, jobject, jobject, jint, jobject); + +/* + * Class: org_sqlite_jni_fts5_Fts5ExtensionApi + * Method: xRowCount + * Signature: (Lorg/sqlite/jni/fts5/Fts5Context;Lorg/sqlite/jni/capi/OutputPointer/Int64;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_fts5_Fts5ExtensionApi_xRowCount + (JNIEnv *, jobject, jobject, jobject); + +/* + * Class: org_sqlite_jni_fts5_Fts5ExtensionApi + * Method: xRowid + * Signature: (Lorg/sqlite/jni/fts5/Fts5Context;)J + */ +JNIEXPORT jlong JNICALL Java_org_sqlite_jni_fts5_Fts5ExtensionApi_xRowid + (JNIEnv *, jobject, jobject); + +/* + * Class: org_sqlite_jni_fts5_Fts5ExtensionApi + * Method: xSetAuxdata + * Signature: (Lorg/sqlite/jni/fts5/Fts5Context;Ljava/lang/Object;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_fts5_Fts5ExtensionApi_xSetAuxdata + (JNIEnv *, jobject, jobject, jobject); + +/* + * Class: org_sqlite_jni_fts5_Fts5ExtensionApi + * Method: xTokenize + * Signature: (Lorg/sqlite/jni/fts5/Fts5Context;[BLorg/sqlite/jni/fts5/XTokenizeCallback;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_fts5_Fts5ExtensionApi_xTokenize + (JNIEnv *, jobject, jobject, jbyteArray, jobject); + +/* + * Class: org_sqlite_jni_fts5_Fts5ExtensionApi + * Method: xUserData + * Signature: (Lorg/sqlite/jni/fts5/Fts5Context;)Ljava/lang/Object; + */ +JNIEXPORT jobject JNICALL Java_org_sqlite_jni_fts5_Fts5ExtensionApi_xUserData + (JNIEnv *, jobject, jobject); + +#ifdef __cplusplus +} +#endif +#endif +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include +/* Header for class org_sqlite_jni_fts5_fts5_api */ + +#ifndef _Included_org_sqlite_jni_fts5_fts5_api +#define _Included_org_sqlite_jni_fts5_fts5_api +#ifdef __cplusplus +extern "C" { +#endif +#undef org_sqlite_jni_fts5_fts5_api_iVersion +#define org_sqlite_jni_fts5_fts5_api_iVersion 2L +/* + * Class: org_sqlite_jni_fts5_fts5_api + * Method: getInstanceForDb + * Signature: (Lorg/sqlite/jni/capi/sqlite3;)Lorg/sqlite/jni/fts5/fts5_api; + */ +JNIEXPORT jobject JNICALL Java_org_sqlite_jni_fts5_fts5_1api_getInstanceForDb + (JNIEnv *, jclass, jobject); + +/* + * Class: org_sqlite_jni_fts5_fts5_api + * Method: xCreateFunction + * Signature: (Ljava/lang/String;Ljava/lang/Object;Lorg/sqlite/jni/fts5/fts5_extension_function;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_fts5_fts5_1api_xCreateFunction + (JNIEnv *, jobject, jstring, jobject, jobject); + +#ifdef __cplusplus +} +#endif +#endif +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include +/* Header for class org_sqlite_jni_fts5_fts5_tokenizer */ + +#ifndef _Included_org_sqlite_jni_fts5_fts5_tokenizer +#define _Included_org_sqlite_jni_fts5_fts5_tokenizer +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: org_sqlite_jni_fts5_fts5_tokenizer + * Method: xTokenize + * Signature: (Lorg/sqlite/jni/fts5/Fts5Tokenizer;I[BLorg/sqlite/jni/fts5/XTokenizeCallback;)I + */ +JNIEXPORT jint JNICALL Java_org_sqlite_jni_fts5_fts5_1tokenizer_xTokenize + (JNIEnv *, jobject, jobject, jint, jbyteArray, jobject); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/ext/jni/src/org/sqlite/jni/annotation/Experimental.java b/ext/jni/src/org/sqlite/jni/annotation/Experimental.java new file mode 100644 index 0000000000..190435c858 --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/annotation/Experimental.java @@ -0,0 +1,30 @@ +/* +** 2023-09-27 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file houses the Experimental annotation for the sqlite3 C API. +*/ +package org.sqlite.jni.annotation; +import java.lang.annotation.*; + +/** + This annotation is for flagging methods, constructors, and types + which are expressly experimental and subject to any amount of + change or outright removal. Client code should not rely on such + features. +*/ +@Documented +@Retention(RetentionPolicy.SOURCE) +@Target({ + ElementType.METHOD, + ElementType.CONSTRUCTOR, + ElementType.TYPE +}) +public @interface Experimental{} diff --git a/ext/jni/src/org/sqlite/jni/annotation/NotNull.java b/ext/jni/src/org/sqlite/jni/annotation/NotNull.java new file mode 100644 index 0000000000..2873082446 --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/annotation/NotNull.java @@ -0,0 +1,71 @@ +/* +** 2023-09-27 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file houses the NotNull annotation for the sqlite3 C API. +*/ +package org.sqlite.jni.annotation; +import java.lang.annotation.*; + +/** + This annotation is for flagging parameters which may not legally be + null or point to closed/finalized C-side resources. + +

    In the case of Java types which map directly to C struct types + (e.g. {@link org.sqlite.jni.capi.sqlite3}, {@link + org.sqlite.jni.capi.sqlite3_stmt}, and {@link + org.sqlite.jni.capi.sqlite3_context}), a closed/finalized resource + is also considered to be null for purposes this annotation because + the C-side effect of passing such a handle is the same as if null + is passed.

    + +

    When used in the context of Java interfaces which are called + from the C APIs, this annotation communicates that the C API will + never pass a null value to the callback for that parameter.

    + +

    Passing a null, for this annotation's definition of null, for + any parameter marked with this annotation specifically invokes + undefined behavior (see below).

    + +

    Passing 0 (i.e. C NULL) or a negative value for any long-type + parameter marked with this annotation specifically invokes undefined + behavior (see below). Such values are treated as C pointers in the + JNI layer.

    + +

    Undefined behaviour: the JNI build uses the {@code + SQLITE_ENABLE_API_ARMOR} build flag, meaning that the C code + invoked with invalid NULL pointers and the like will not invoke + undefined behavior in the conventional C sense, but may, for + example, return result codes which are not documented for the + affected APIs or may otherwise behave unpredictably. In no known + cases will such arguments result in C-level code dereferencing a + NULL pointer or accessing out-of-bounds (or otherwise invalid) + memory. In other words, they may cause unexpected behavior but + should never cause an outright crash or security issue.

    + +

    Note that the C-style API does not throw any exceptions on its + own because it has a no-throw policy in order to retain its C-style + semantics, but it may trigger NullPointerExceptions (or similar) if + passed a null for a parameter flagged with this annotation.

    + +

    This annotation is informational only. No policy is in place to + programmatically ensure that NotNull is conformed to in client + code.

    + +

    This annotation is solely for the use by the classes in the + org.sqlite.jni package and subpackages, but is made public so that + javadoc will link to it from the annotated functions. It is not + part of the public API and client-level code must not rely on + it.

    +*/ +@Documented +@Retention(RetentionPolicy.SOURCE) +@Target(ElementType.PARAMETER) +public @interface NotNull{} diff --git a/ext/jni/src/org/sqlite/jni/annotation/Nullable.java b/ext/jni/src/org/sqlite/jni/annotation/Nullable.java new file mode 100644 index 0000000000..e3fa30efc9 --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/annotation/Nullable.java @@ -0,0 +1,33 @@ +/* +** 2023-09-27 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file houses the Nullable annotation for the sqlite3 C API. +*/ +package org.sqlite.jni.annotation; +import java.lang.annotation.*; + +/** + This annotation is for flagging parameters which may legally be + null, noting that they may behave differently if passed null but + are prepared to expect null as a value. When used in the context of + callback methods which are called into from the C APIs, this + annotation communicates that the C API may pass a null value to the + callback. + +

    This annotation is solely for the use by the classes in this + package but is made public so that javadoc will link to it from the + annotated functions. It is not part of the public API and + client-level code must not rely on it. +*/ +@Documented +@Retention(RetentionPolicy.SOURCE) +@Target(ElementType.PARAMETER) +public @interface Nullable{} diff --git a/ext/jni/src/org/sqlite/jni/annotation/package-info.java b/ext/jni/src/org/sqlite/jni/annotation/package-info.java new file mode 100644 index 0000000000..20ac7a3017 --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/annotation/package-info.java @@ -0,0 +1,17 @@ +/* +** 2023-09-27 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +*/ +/** + This package houses annotations specific to the JNI bindings of the + SQLite3 C API. +*/ +package org.sqlite.jni.annotation; diff --git a/ext/jni/src/org/sqlite/jni/capi/AbstractCollationCallback.java b/ext/jni/src/org/sqlite/jni/capi/AbstractCollationCallback.java new file mode 100644 index 0000000000..925536636e --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/capi/AbstractCollationCallback.java @@ -0,0 +1,34 @@ +/* +** 2023-08-25 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file is part of the JNI bindings for the sqlite3 C API. +*/ +package org.sqlite.jni.capi; +import org.sqlite.jni.annotation.NotNull; + +/** + An implementation of {@link CollationCallback} which provides a + no-op xDestroy() method. +*/ +public abstract class AbstractCollationCallback + implements CollationCallback, XDestroyCallback { + /** + Must compare the given byte arrays and return the result using + {@code memcmp()} semantics. + */ + public abstract int call(@NotNull byte[] lhs, @NotNull byte[] rhs); + + /** + Optionally override to be notified when the UDF is finalized by + SQLite. This implementation does nothing. + */ + public void xDestroy(){} +} diff --git a/ext/jni/src/org/sqlite/jni/capi/AggregateFunction.java b/ext/jni/src/org/sqlite/jni/capi/AggregateFunction.java new file mode 100644 index 0000000000..912f6ed5b5 --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/capi/AggregateFunction.java @@ -0,0 +1,138 @@ +/* +** 2023-08-25 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file is part of the JNI bindings for the sqlite3 C API. +*/ +package org.sqlite.jni.capi; + + +/** + A SQLFunction implementation for aggregate functions. Its T is the + data type of its "accumulator" state, an instance of which is + intended to be be managed using the getAggregateState() and + takeAggregateState() methods. +*/ +public abstract class AggregateFunction implements SQLFunction { + + /** + As for the xStep() argument of the C API's + sqlite3_create_function(). If this function throws, the + exception is not propagated and a warning might be emitted to a + debugging channel. + */ + public abstract void xStep(sqlite3_context cx, sqlite3_value[] args); + + /** + As for the xFinal() argument of the C API's sqlite3_create_function(). + If this function throws, it is translated into an sqlite3_result_error(). + */ + public abstract void xFinal(sqlite3_context cx); + + /** + Optionally override to be notified when the UDF is finalized by + SQLite. + */ + public void xDestroy() {} + + /** + PerContextState assists aggregate and window functions in + managing their accumulator state across calls to the UDF's + callbacks. + +

    T must be of a type which can be legally stored as a value in + java.util.HashMap. + +

    If a given aggregate or window function is called multiple times + in a single SQL statement, e.g. SELECT MYFUNC(A), MYFUNC(B)..., + then the clients need some way of knowing which call is which so + that they can map their state between their various UDF callbacks + and reset it via xFinal(). This class takes care of such + mappings. + +

    This class works by mapping + sqlite3_context.getAggregateContext() to a single piece of + state, of a client-defined type (the T part of this class), which + persists across a "matching set" of the UDF's callbacks. + +

    This class is a helper providing commonly-needed functionality + - it is not required for use with aggregate or window functions. + Client UDFs are free to perform such mappings using custom + approaches. The provided {@link AggregateFunction} and {@link + WindowFunction} classes use this. + */ + public static final class PerContextState { + private final java.util.Map> map + = new java.util.HashMap<>(); + + /** + Should be called from a UDF's xStep(), xValue(), and xInverse() + methods, passing it that method's first argument and an initial + value for the persistent state. If there is currently no + mapping for the given context within the map, one is created + using the given initial value, else the existing one is used + and the 2nd argument is ignored. It returns a ValueHolder + which can be used to modify that state directly without + requiring that the client update the underlying map's entry. + +

    The caller is obligated to eventually call + takeAggregateState() to clear the mapping. + */ + public ValueHolder getAggregateState(sqlite3_context cx, T initialValue){ + final Long key = cx.getAggregateContext(true); + ValueHolder rc = null==key ? null : map.get(key); + if( null==rc ){ + map.put(key, rc = new ValueHolder<>(initialValue)); + } + return rc; + } + + /** + Should be called from a UDF's xFinal() method and passed that + method's first argument. This function removes the value + associated with cx.getAggregateContext() from the map and + returns it, returning null if no other UDF method has been + called to set up such a mapping. The latter condition will be + the case if a UDF is used in a statement which has no result + rows. + */ + public T takeAggregateState(sqlite3_context cx){ + final ValueHolder h = map.remove(cx.getAggregateContext(false)); + return null==h ? null : h.value; + } + } + + /** Per-invocation state for the UDF. */ + private final PerContextState map = new PerContextState<>(); + + /** + To be called from the implementation's xStep() method, as well + as the xValue() and xInverse() methods of the {@link WindowFunction} + subclass, to fetch the current per-call UDF state. On the + first call to this method for any given sqlite3_context + argument, the context is set to the given initial value. On all other + calls, the 2nd argument is ignored. + + @see AggregateFunction.PerContextState#getAggregateState + */ + protected final ValueHolder getAggregateState(sqlite3_context cx, T initialValue){ + return map.getAggregateState(cx, initialValue); + } + + /** + To be called from the implementation's xFinal() method to fetch + the final state of the UDF and remove its mapping. + + see AggregateFunction.PerContextState#takeAggregateState + */ + protected final T takeAggregateState(sqlite3_context cx){ + return map.takeAggregateState(cx); + } +} diff --git a/ext/jni/src/org/sqlite/jni/capi/AuthorizerCallback.java b/ext/jni/src/org/sqlite/jni/capi/AuthorizerCallback.java new file mode 100644 index 0000000000..298e3a5900 --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/capi/AuthorizerCallback.java @@ -0,0 +1,29 @@ +/* +** 2023-08-25 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file is part of the JNI bindings for the sqlite3 C API. +*/ +package org.sqlite.jni.capi; +import org.sqlite.jni.annotation.*; + +/** + Callback for use with {@link CApi#sqlite3_set_authorizer}. +*/ +public interface AuthorizerCallback extends CallbackProxy { + /** + Must function as described for the C-level + sqlite3_set_authorizer() callback. If it throws, the error is + converted to a db-level error and the exception is suppressed. + */ + int call(int opId, @Nullable String s1, @Nullable String s2, + @Nullable String s3, @Nullable String s4); + +} diff --git a/ext/jni/src/org/sqlite/jni/capi/AutoExtensionCallback.java b/ext/jni/src/org/sqlite/jni/capi/AutoExtensionCallback.java new file mode 100644 index 0000000000..7a54132d29 --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/capi/AutoExtensionCallback.java @@ -0,0 +1,40 @@ +/* +** 2023-08-25 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file is part of the JNI bindings for the sqlite3 C API. +*/ +package org.sqlite.jni.capi; + +/** + Callback for use with the {@link CApi#sqlite3_auto_extension} + family of APIs. +*/ +public interface AutoExtensionCallback extends CallbackProxy { + /** + Must function as described for a C-level + sqlite3_auto_extension() callback. + +

    This callback may throw and the exception's error message will + be set as the db's error string. + +

    Tips for implementations: + +

    - Opening a database from an auto-extension handler will lead to + an endless recursion of the auto-handler triggering itself + indirectly for each newly-opened database. + +

    - If this routine is stateful, it may be useful to make the + overridden method synchronized. + +

    - Results are undefined if the given db is closed by an auto-extension. + */ + int call(sqlite3 db); +} diff --git a/ext/jni/src/org/sqlite/jni/capi/BusyHandlerCallback.java b/ext/jni/src/org/sqlite/jni/capi/BusyHandlerCallback.java new file mode 100644 index 0000000000..00223f0b66 --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/capi/BusyHandlerCallback.java @@ -0,0 +1,26 @@ +/* +** 2023-08-25 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file is part of the JNI bindings for the sqlite3 C API. +*/ +package org.sqlite.jni.capi; + +/** + Callback for use with {@link CApi#sqlite3_busy_handler}. +*/ +public interface BusyHandlerCallback extends CallbackProxy { + /** + Must function as documented for the C-level + sqlite3_busy_handler() callback argument, minus the (void*) + argument the C-level function requires. + */ + int call(int n); +} diff --git a/ext/jni/src/org/sqlite/jni/capi/CApi.java b/ext/jni/src/org/sqlite/jni/capi/CApi.java new file mode 100644 index 0000000000..0b840c3623 --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/capi/CApi.java @@ -0,0 +1,2897 @@ +/* +** 2023-07-21 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file declares the main JNI bindings for the sqlite3 C API. +*/ +package org.sqlite.jni.capi; +import java.util.Arrays; +import java.nio.charset.StandardCharsets; +import org.sqlite.jni.annotation.*; + +/** + This class contains the entire C-style sqlite3 JNI API binding, + minus a few bits and pieces declared in other files. For client-side + use, a static import is recommended: + +

    {@code
    +  import static org.sqlite.jni.capi.CApi.*;
    +  }
    + +

    The C-side part can be found in sqlite3-jni.c. + +

    Only functions which materially differ from their C counterparts + are documented here, and only those material differences are + documented. The C documentation is otherwise applicable for these + APIs: + +

    https://sqlite.org/c3ref/intro.html + +

    A handful of Java-specific APIs have been added which are + documented here. A number of convenience overloads are provided + which are not documented but whose semantics map 1-to-1 in an + intuitive manner. e.g. {@link + #sqlite3_result_set(sqlite3_context,int)} is equivalent to {@link + #sqlite3_result_int}, and sqlite3_result_set() has many + type-specific overloads. + +

    Notes regarding Java's Modified UTF-8 vs standard UTF-8: + +

    SQLite internally uses UTF-8 encoding, whereas Java natively uses + UTF-16. Java JNI has routines for converting to and from UTF-8, + but JNI uses what its docs call modified UTF-8 (see links below) + Care must be taken when converting Java strings to or from standard + UTF-8 to ensure that the proper conversion is performed. In short, + Java's {@code String.getBytes(StandardCharsets.UTF_8)} performs the proper + conversion in Java, and there are no JNI C APIs for that conversion + (JNI's {@code NewStringUTF()} requires its input to be in MUTF-8). + +

    The known consequences and limitations this discrepancy places on + the SQLite3 JNI binding include: + +

      + +
    • C functions which take C-style strings without a length argument + require special care when taking input from Java. In particular, + Java strings converted to byte arrays for encoding purposes are not + NUL-terminated, and conversion to a Java byte array must sometimes + be careful to add one. Functions which take a length do not require + this so long as the length is provided. Search the CApi class + for "\0" for examples. + +
    + +

    Further reading: + +

    https://stackoverflow.com/questions/57419723 +

    https://stackoverflow.com/questions/7921016 +

    https://itecnote.com/tecnote/java-getting-true-utf-8-characters-in-java-jni/ +

    https://docs.oracle.com/javase/8/docs/api/java/lang/Character.html#unicode +

    https://docs.oracle.com/javase/8/docs/api/java/io/DataInput.html#modified-utf-8 + +*/ +public final class CApi { + static { + System.loadLibrary("sqlite3-jni"); + } + //! Not used + private CApi(){} + //! Called from static init code. + private static native void init(); + + /** + Returns a nul-terminated copy of s as a UTF-8-encoded byte array, + or null if s is null. + */ + private static byte[] nulTerminateUtf8(String s){ + return null==s ? null : (s+"\0").getBytes(StandardCharsets.UTF_8); + } + + /** + Each thread which uses the SQLite3 JNI APIs should call + sqlite3_jni_uncache_thread() when it is done with the library - + either right before it terminates or when it finishes using the + SQLite API. This will clean up any cached per-thread info. + +

    This process does not close any databases or finalize + any prepared statements because their ownership does not depend on + a given thread. For proper library behavior, and to + avoid C-side leaks, be sure to finalize all statements and close + all databases before calling this function. + +

    Calling this from the main application thread is not strictly + required. Additional threads must call this before ending or they + will leak cache entries in the C heap, which in turn may keep + numerous Java-side global references active. + +

    This routine returns false without side effects if the current + JNIEnv is not cached, else returns true, but this information is + primarily for testing of the JNI bindings and is not information + which client-level code can use to make any informed + decisions. Its return type and semantics are not considered + stable and may change at any time. + */ + public static native boolean sqlite3_java_uncache_thread(); + + /** + Returns true if this JVM has JNI-level support for C-level direct + memory access using java.nio.ByteBuffer, else returns false. + */ + @Experimental + public static native boolean sqlite3_jni_supports_nio(); + + /** + For internal use only. Sets the given db's error code and + (optionally) string. If rc is 0, it defaults to SQLITE_ERROR. + + On success it returns rc. On error it may return a more serious + code, such as SQLITE_NOMEM. Returns SQLITE_MISUSE if db is null. + */ + static native int sqlite3_jni_db_error(@NotNull sqlite3 db, + int rc, @Nullable String msg); + + /** + Convenience overload which uses e.toString() as the error + message. + */ + static int sqlite3_jni_db_error(@NotNull sqlite3 db, + int rc, @NotNull Exception e){ + return sqlite3_jni_db_error(db, rc, e.toString()); + } + + ////////////////////////////////////////////////////////////////////// + // Maintenance reminder: please keep the sqlite3_.... functions + // alphabetized. The SQLITE_... values. on the other hand, are + // grouped by category. + + /** + Functions exactly like the native form except that (A) the 2nd + argument is a boolean instead of an int and (B) the returned + value is not a pointer address and is only intended for use as a + per-UDF-call lookup key in a higher-level data structure. + +

    Passing a true second argument is analogous to passing some + unspecified small, non-0 positive value to the C API and passing + false is equivalent to passing 0 to the C API. + +

    Like the C API, it returns 0 if allocation fails or if + initialize is false and no prior aggregate context was allocated + for cx. If initialize is true then it returns 0 only on + allocation error. In all cases, 0 is considered the sentinel + "not a key" value. + */ + public static native long sqlite3_aggregate_context(sqlite3_context cx, boolean initialize); + + /** + Functions almost as documented for the C API, with these + exceptions: + +

    - The callback interface is shorter because of + cross-language differences. Specifically, 3rd argument to the C + auto-extension callback interface is unnecessary here. + +

    The C API docs do not specifically say so, but if the list of + auto-extensions is manipulated from an auto-extension, it is + undefined which, if any, auto-extensions will subsequently + execute for the current database. That is, doing so will result + in unpredictable, but not undefined, behavior. + +

    See the AutoExtension class docs for more information. + */ + public static native int sqlite3_auto_extension(@NotNull AutoExtensionCallback callback); + + private static native int sqlite3_backup_finish(@NotNull long ptrToBackup); + + public static int sqlite3_backup_finish(@NotNull sqlite3_backup b){ + return null==b ? 0 : sqlite3_backup_finish(b.clearNativePointer()); + } + + private static native sqlite3_backup sqlite3_backup_init( + @NotNull long ptrToDbDest, @NotNull String destSchemaName, + @NotNull long ptrToDbSrc, @NotNull String srcSchemaName + ); + + public static sqlite3_backup sqlite3_backup_init( + @NotNull sqlite3 dbDest, @NotNull String destSchemaName, + @NotNull sqlite3 dbSrc, @NotNull String srcSchemaName + ){ + return sqlite3_backup_init( dbDest.getNativePointer(), destSchemaName, + dbSrc.getNativePointer(), srcSchemaName ); + } + + private static native int sqlite3_backup_pagecount(@NotNull long ptrToBackup); + + public static int sqlite3_backup_pagecount(@NotNull sqlite3_backup b){ + return sqlite3_backup_pagecount(b.getNativePointer()); + } + + private static native int sqlite3_backup_remaining(@NotNull long ptrToBackup); + + public static int sqlite3_backup_remaining(@NotNull sqlite3_backup b){ + return sqlite3_backup_remaining(b.getNativePointer()); + } + + private static native int sqlite3_backup_step(@NotNull long ptrToBackup, int nPage); + + public static int sqlite3_backup_step(@NotNull sqlite3_backup b, int nPage){ + return sqlite3_backup_step(b.getNativePointer(), nPage); + } + + private static native int sqlite3_bind_blob( + @NotNull long ptrToStmt, int ndx, @Nullable byte[] data, int n + ); + + /** + If n is negative, SQLITE_MISUSE is returned. If n>data.length + then n is silently truncated to data.length. + */ + public static int sqlite3_bind_blob( + @NotNull sqlite3_stmt stmt, int ndx, @Nullable byte[] data, int n + ){ + return sqlite3_bind_blob(stmt.getNativePointer(), ndx, data, n); + } + + public static int sqlite3_bind_blob( + @NotNull sqlite3_stmt stmt, int ndx, @Nullable byte[] data + ){ + return (null==data) + ? sqlite3_bind_null(stmt.getNativePointer(), ndx) + : sqlite3_bind_blob(stmt.getNativePointer(), ndx, data, data.length); + } + + /** + Convenience overload which is a simple proxy for + sqlite3_bind_nio_buffer(). + */ + @Experimental + /*public*/ static int sqlite3_bind_blob( + @NotNull sqlite3_stmt stmt, int ndx, @Nullable java.nio.ByteBuffer data, + int begin, int n + ){ + return sqlite3_bind_nio_buffer(stmt, ndx, data, begin, n); + } + + /** + Convenience overload which is equivalent to passing its arguments + to sqlite3_bind_nio_buffer() with the values 0 and -1 for the + final two arguments. + */ + @Experimental + /*public*/ static int sqlite3_bind_blob( + @NotNull sqlite3_stmt stmt, int ndx, @Nullable java.nio.ByteBuffer data + ){ + return sqlite3_bind_nio_buffer(stmt, ndx, data, 0, -1); + } + + private static native int sqlite3_bind_double( + @NotNull long ptrToStmt, int ndx, double v + ); + + public static int sqlite3_bind_double( + @NotNull sqlite3_stmt stmt, int ndx, double v + ){ + return sqlite3_bind_double(stmt.getNativePointer(), ndx, v); + } + + private static native int sqlite3_bind_int( + @NotNull long ptrToStmt, int ndx, int v + ); + + public static int sqlite3_bind_int( + @NotNull sqlite3_stmt stmt, int ndx, int v + ){ + return sqlite3_bind_int(stmt.getNativePointer(), ndx, v); + } + + private static native int sqlite3_bind_int64( + @NotNull long ptrToStmt, int ndx, long v + ); + + public static int sqlite3_bind_int64(@NotNull sqlite3_stmt stmt, int ndx, long v){ + return sqlite3_bind_int64( stmt.getNativePointer(), ndx, v ); + } + + private static native int sqlite3_bind_java_object( + @NotNull long ptrToStmt, int ndx, @Nullable Object o + ); + + /** + Binds the contents of the given buffer object as a blob. + + The byte range of the buffer may be restricted by providing a + start index and a number of bytes. beginPos may not be negative. + Negative howMany is interpreted as the remainder of the buffer + past the given start position, up to the buffer's limit() (as + opposed its capacity()). + + If beginPos+howMany would extend past the limit() of the buffer + then SQLITE_ERROR is returned. + + If any of the following are true, this function behaves like + sqlite3_bind_null(): the buffer is null, beginPos is at or past + the end of the buffer, howMany is 0, or the calculated slice of + the blob has a length of 0. + + If ndx is out of range, it returns SQLITE_RANGE, as documented + for sqlite3_bind_blob(). If beginPos is negative or if + sqlite3_jni_supports_nio() returns false then SQLITE_MISUSE is + returned. Note that this function is bound (as it were) by the + SQLITE_LIMIT_LENGTH constraint and SQLITE_TOOBIG is returned if + the resulting slice of the buffer exceeds that limit. + + This function does not modify the buffer's streaming-related + cursors. + + If the buffer is modified in a separate thread while this + operation is running, results are undefined and will likely + result in corruption of the bound data or a segmentation fault. + + Design note: this function should arguably take a java.nio.Buffer + instead of ByteBuffer, but it can only operate on "direct" + buffers and the only such class offered by Java is (apparently) + ByteBuffer. + + @see https://docs.oracle.com/javase/8/docs/api/java/nio/Buffer.html + */ + @Experimental + /*public*/ static native int sqlite3_bind_nio_buffer( + @NotNull sqlite3_stmt stmt, int ndx, @Nullable java.nio.ByteBuffer data, + int beginPos, int howMany + ); + + /** + Convenience overload which binds the given buffer's entire + contents, up to its limit() (as opposed to its capacity()). + */ + @Experimental + /*public*/ static int sqlite3_bind_nio_buffer( + @NotNull sqlite3_stmt stmt, int ndx, @Nullable java.nio.ByteBuffer data + ){ + return sqlite3_bind_nio_buffer(stmt, ndx, data, 0, -1); + } + + /** + Binds the given object at the given index. If o is null then this behaves like + sqlite3_bind_null(). + + @see #sqlite3_result_java_object + */ + public static int sqlite3_bind_java_object( + @NotNull sqlite3_stmt stmt, int ndx, @Nullable Object o + ){ + return sqlite3_bind_java_object(stmt.getNativePointer(), ndx, o); + } + + private static native int sqlite3_bind_null(@NotNull long ptrToStmt, int ndx); + + public static int sqlite3_bind_null(@NotNull sqlite3_stmt stmt, int ndx){ + return sqlite3_bind_null(stmt.getNativePointer(), ndx); + } + + private static native int sqlite3_bind_parameter_count(@NotNull long ptrToStmt); + + public static int sqlite3_bind_parameter_count(@NotNull sqlite3_stmt stmt){ + return sqlite3_bind_parameter_count(stmt.getNativePointer()); + } + + /** + Requires that paramName be a NUL-terminated UTF-8 string. + + This overload is private because: (A) to keep users from + inadvertently passing non-NUL-terminated byte arrays (an easy + thing to do). (B) it is cheaper to NUL-terminate the + String-to-byte-array conversion in the public-facing Java-side + overload than to do that in C, so that signature is the + public-facing one. + */ + private static native int sqlite3_bind_parameter_index( + @NotNull long ptrToStmt, @NotNull byte[] paramName + ); + + public static int sqlite3_bind_parameter_index( + @NotNull sqlite3_stmt stmt, @NotNull String paramName + ){ + final byte[] utf8 = nulTerminateUtf8(paramName); + return null==utf8 ? 0 : sqlite3_bind_parameter_index(stmt.getNativePointer(), utf8); + } + + private static native String sqlite3_bind_parameter_name( + @NotNull long ptrToStmt, int index + ); + + public static String sqlite3_bind_parameter_name(@NotNull sqlite3_stmt stmt, int index){ + return sqlite3_bind_parameter_name(stmt.getNativePointer(), index); + } + + private static native int sqlite3_bind_text( + @NotNull long ptrToStmt, int ndx, @Nullable byte[] utf8, int maxBytes + ); + + /** + Works like the C-level sqlite3_bind_text() but assumes + SQLITE_TRANSIENT for the final C API parameter. The byte array is + assumed to be in UTF-8 encoding. + +

    If data is not null and maxBytes>utf8.length then maxBytes is + silently truncated to utf8.length. If maxBytes is negative then + results are undefined if data is not null and does not contain a + NUL byte. + */ + static int sqlite3_bind_text( + @NotNull sqlite3_stmt stmt, int ndx, @Nullable byte[] utf8, int maxBytes + ){ + return sqlite3_bind_text(stmt.getNativePointer(), ndx, utf8, maxBytes); + } + + /** + Converts data, if not null, to a UTF-8-encoded byte array and + binds it as such, returning the result of the C-level + sqlite3_bind_null() or sqlite3_bind_text(). + */ + public static int sqlite3_bind_text( + @NotNull sqlite3_stmt stmt, int ndx, @Nullable String data + ){ + if( null==data ) return sqlite3_bind_null(stmt.getNativePointer(), ndx); + final byte[] utf8 = data.getBytes(StandardCharsets.UTF_8); + return sqlite3_bind_text(stmt.getNativePointer(), ndx, utf8, utf8.length); + } + + /** + Requires that utf8 be null or in UTF-8 encoding. + */ + public static int sqlite3_bind_text( + @NotNull sqlite3_stmt stmt, int ndx, @Nullable byte[] utf8 + ){ + return ( null==utf8 ) + ? sqlite3_bind_null(stmt.getNativePointer(), ndx) + : sqlite3_bind_text(stmt.getNativePointer(), ndx, utf8, utf8.length); + } + + private static native int sqlite3_bind_text16( + @NotNull long ptrToStmt, int ndx, @Nullable byte[] data, int maxBytes + ); + + /** + Identical to the sqlite3_bind_text() overload with the same + signature but requires that its input be encoded in UTF-16 in + platform byte order. + */ + static int sqlite3_bind_text16( + @NotNull sqlite3_stmt stmt, int ndx, @Nullable byte[] data, int maxBytes + ){ + return sqlite3_bind_text16(stmt.getNativePointer(), ndx, data, maxBytes); + } + + /** + Converts its string argument to UTF-16 and binds it as such, returning + the result of the C-side function of the same name. The 3rd argument + may be null. + */ + public static int sqlite3_bind_text16( + @NotNull sqlite3_stmt stmt, int ndx, @Nullable String data + ){ + if(null == data) return sqlite3_bind_null(stmt, ndx); + final byte[] bytes = data.getBytes(StandardCharsets.UTF_16); + return sqlite3_bind_text16(stmt.getNativePointer(), ndx, bytes, bytes.length); + } + + /** + Requires that data be null or in UTF-16 encoding in platform byte + order. Returns the result of the C-level sqlite3_bind_null() or + sqlite3_bind_text16(). + */ + public static int sqlite3_bind_text16( + @NotNull sqlite3_stmt stmt, int ndx, @Nullable byte[] data + ){ + return (null == data) + ? sqlite3_bind_null(stmt.getNativePointer(), ndx) + : sqlite3_bind_text16(stmt.getNativePointer(), ndx, data, data.length); + } + + private static native int sqlite3_bind_value(@NotNull long ptrToStmt, int ndx, long ptrToValue); + + /** + Functions like the C-level sqlite3_bind_value(), or + sqlite3_bind_null() if val is null. + */ + public static int sqlite3_bind_value(@NotNull sqlite3_stmt stmt, int ndx, sqlite3_value val){ + return sqlite3_bind_value(stmt.getNativePointer(), ndx, + null==val ? 0L : val.getNativePointer()); + } + + private static native int sqlite3_bind_zeroblob(@NotNull long ptrToStmt, int ndx, int n); + + public static int sqlite3_bind_zeroblob(@NotNull sqlite3_stmt stmt, int ndx, int n){ + return sqlite3_bind_zeroblob(stmt.getNativePointer(), ndx, n); + } + + private static native int sqlite3_bind_zeroblob64( + @NotNull long ptrToStmt, int ndx, long n + ); + + public static int sqlite3_bind_zeroblob64(@NotNull sqlite3_stmt stmt, int ndx, long n){ + return sqlite3_bind_zeroblob64(stmt.getNativePointer(), ndx, n); + } + + private static native int sqlite3_blob_bytes(@NotNull long ptrToBlob); + + public static int sqlite3_blob_bytes(@NotNull sqlite3_blob blob){ + return sqlite3_blob_bytes(blob.getNativePointer()); + } + + private static native int sqlite3_blob_close(@Nullable long ptrToBlob); + + public static int sqlite3_blob_close(@Nullable sqlite3_blob blob){ + return null==blob ? 0 : sqlite3_blob_close(blob.clearNativePointer()); + } + + private static native int sqlite3_blob_open( + @NotNull long ptrToDb, @NotNull String dbName, + @NotNull String tableName, @NotNull String columnName, + long iRow, int flags, @NotNull OutputPointer.sqlite3_blob out + ); + + public static int sqlite3_blob_open( + @NotNull sqlite3 db, @NotNull String dbName, + @NotNull String tableName, @NotNull String columnName, + long iRow, int flags, @NotNull OutputPointer.sqlite3_blob out + ){ + return sqlite3_blob_open(db.getNativePointer(), dbName, tableName, + columnName, iRow, flags, out); + } + + /** + Convenience overload. + */ + public static sqlite3_blob sqlite3_blob_open( + @NotNull sqlite3 db, @NotNull String dbName, + @NotNull String tableName, @NotNull String columnName, + long iRow, int flags ){ + final OutputPointer.sqlite3_blob out = new OutputPointer.sqlite3_blob(); + sqlite3_blob_open(db.getNativePointer(), dbName, tableName, columnName, + iRow, flags, out); + return out.take(); + } + + private static native int sqlite3_blob_read( + @NotNull long ptrToBlob, @NotNull byte[] target, int srcOffset + ); + + /** + As per C's sqlite3_blob_read(), but writes its output to the + given byte array. Note that the final argument is the offset of + the source buffer, not the target array. + */ + public static int sqlite3_blob_read( + @NotNull sqlite3_blob src, @NotNull byte[] target, int srcOffset + ){ + return sqlite3_blob_read(src.getNativePointer(), target, srcOffset); + } + + /** + An internal level of indirection. + */ + @Experimental + private static native int sqlite3_blob_read_nio_buffer( + @NotNull long ptrToBlob, int srcOffset, + @NotNull java.nio.ByteBuffer tgt, int tgtOffset, int howMany + ); + + /** + Reads howMany bytes from offset srcOffset of src into position + tgtOffset of tgt. + + Returns SQLITE_MISUSE if src is null, tgt is null, or + sqlite3_jni_supports_nio() returns false. Returns SQLITE_ERROR if + howMany or either offset are negative. If argument validation + succeeds, it returns the result of the underlying call to + sqlite3_blob_read() (0 on success). + */ + @Experimental + /*public*/ static int sqlite3_blob_read_nio_buffer( + @NotNull sqlite3_blob src, int srcOffset, + @NotNull java.nio.ByteBuffer tgt, int tgtOffset, int howMany + ){ + return (JNI_SUPPORTS_NIO && src!=null && tgt!=null) + ? sqlite3_blob_read_nio_buffer( + src.getNativePointer(), srcOffset, tgt, tgtOffset, howMany + ) + : SQLITE_MISUSE; + } + + /** + Convenience overload which reads howMany bytes from position + srcOffset of src and returns the result as a new ByteBuffer. + + srcOffset may not be negative. If howMany is negative, it is + treated as all bytes following srcOffset. + + Returns null if sqlite3_jni_supports_nio(), any arguments are + invalid, if the number of bytes to read is 0 or is larger than + the src blob, or the underlying call to sqlite3_blob_read() fails + for any reason. + */ + @Experimental + /*public*/ static java.nio.ByteBuffer sqlite3_blob_read_nio_buffer( + @NotNull sqlite3_blob src, int srcOffset, int howMany + ){ + if( !JNI_SUPPORTS_NIO || src==null ) return null; + else if( srcOffset<0 ) return null; + final int nB = sqlite3_blob_bytes(src); + if( srcOffset>=nB ) return null; + else if( howMany<0 ) howMany = nB - srcOffset; + if( srcOffset + howMany > nB ) return null; + final java.nio.ByteBuffer tgt = + java.nio.ByteBuffer.allocateDirect(howMany); + final int rc = sqlite3_blob_read_nio_buffer( + src.getNativePointer(), srcOffset, tgt, 0, howMany + ); + return 0==rc ? tgt : null; + } + + /** + Overload alias for sqlite3_blob_read_nio_buffer(). + */ + @Experimental + /*public*/ static int sqlite3_blob_read( + @NotNull sqlite3_blob src, int srcOffset, + @NotNull java.nio.ByteBuffer tgt, + int tgtOffset, int howMany + ){ + return sqlite3_blob_read_nio_buffer( + src, srcOffset, tgt, tgtOffset, howMany + ); + } + + /** + Convenience overload which uses 0 for both src and tgt offsets + and reads a number of bytes equal to the smaller of + sqlite3_blob_bytes(src) and tgt.limit(). + + On success it sets tgt.limit() to the number of bytes read. On + error, tgt.limit() is not modified. + + Returns 0 on success. Returns SQLITE_MISUSE is either argument is + null or sqlite3_jni_supports_nio() returns false. Else it returns + the result of the underlying call to sqlite3_blob_read(). + */ + @Experimental + /*public*/ static int sqlite3_blob_read( + @NotNull sqlite3_blob src, + @NotNull java.nio.ByteBuffer tgt + ){ + if(!JNI_SUPPORTS_NIO || src==null || tgt==null) return SQLITE_MISUSE; + final int nSrc = sqlite3_blob_bytes(src); + final int nTgt = tgt.limit(); + final int nRead = nTgt T sqlite3_column_java_object( + @NotNull sqlite3_stmt stmt, int ndx, @NotNull Class type + ){ + final Object o = sqlite3_column_java_object(stmt, ndx); + return type.isInstance(o) ? (T)o : null; + } + + private static native String sqlite3_column_name(@NotNull long ptrToStmt, int ndx); + + public static String sqlite3_column_name(@NotNull sqlite3_stmt stmt, int ndx){ + return sqlite3_column_name(stmt.getNativePointer(), ndx); + } + + /** + A variant of sqlite3_column_blob() which returns the blob as a + ByteBuffer object. Returns null if its argument is null, if + sqlite3_jni_supports_nio() is false, or if sqlite3_column_blob() + would return null for the same inputs. + */ + @Experimental + /*public*/ static native java.nio.ByteBuffer sqlite3_column_nio_buffer( + @NotNull sqlite3_stmt stmt, int ndx + ); + + private static native String sqlite3_column_origin_name(@NotNull long ptrToStmt, int ndx); + + /** + Only available if built with SQLITE_ENABLE_COLUMN_METADATA. + */ + public static String sqlite3_column_origin_name(@NotNull sqlite3_stmt stmt, int ndx){ + return sqlite3_column_origin_name(stmt.getNativePointer(), ndx); + } + + private static native String sqlite3_column_table_name(@NotNull long ptrToStmt, int ndx); + + /** + Only available if built with SQLITE_ENABLE_COLUMN_METADATA. + */ + public static String sqlite3_column_table_name(@NotNull sqlite3_stmt stmt, int ndx){ + return sqlite3_column_table_name(stmt.getNativePointer(), ndx); + } + + /** + Functions identially to the C API, and this note is just to + stress that the returned bytes are encoded as UTF-8. It returns + null if the underlying C-level sqlite3_column_text() returns NULL + or on allocation error. + + @see #sqlite3_column_text16(sqlite3_stmt,int) + */ + public static native byte[] sqlite3_column_text( + @NotNull sqlite3_stmt stmt, int ndx + ); + + public static native String sqlite3_column_text16( + @NotNull sqlite3_stmt stmt, int ndx + ); + + // The real utility of this function is questionable. + // /** + // Returns a Java value representation based on the value of + // sqlite_value_type(). For integer types it returns either Integer + // or Long, depending on whether the value will fit in an + // Integer. For floating-point values it always returns type Double. + + // If the column was bound using sqlite3_result_java_object() then + // that value, as an Object, is returned. + // */ + // public static Object sqlite3_column_to_java(@NotNull sqlite3_stmt stmt, + // int ndx){ + // sqlite3_value v = sqlite3_column_value(stmt, ndx); + // Object rv = null; + // if(null == v) return v; + // v = sqlite3_value_dup(v)/*need a protected value*/; + // if(null == v) return v /* OOM error in C */; + // if(112/* 'p' */ == sqlite3_value_subtype(v)){ + // rv = sqlite3_value_java_object(v); + // }else{ + // switch(sqlite3_value_type(v)){ + // case SQLITE_INTEGER: { + // final long i = sqlite3_value_int64(v); + // rv = (i<=0x7fffffff && i>=-0x7fffffff-1) + // ? new Integer((int)i) : new Long(i); + // break; + // } + // case SQLITE_FLOAT: rv = new Double(sqlite3_value_double(v)); break; + // case SQLITE_BLOB: rv = sqlite3_value_blob(v); break; + // case SQLITE_TEXT: rv = sqlite3_value_text16(v); break; + // default: break; + // } + // } + // sqlite3_value_free(v); + // return rv; + // } + + private static native int sqlite3_column_type(@NotNull long ptrToStmt, int ndx); + + public static int sqlite3_column_type(@NotNull sqlite3_stmt stmt, int ndx){ + return sqlite3_column_type(stmt.getNativePointer(), ndx); + } + + public static native sqlite3_value sqlite3_column_value( + @NotNull sqlite3_stmt stmt, int ndx + ); + + private static native int sqlite3_collation_needed( + @NotNull long ptrToDb, @Nullable CollationNeededCallback callback + ); + + /** + This functions like C's sqlite3_collation_needed16() because + Java's string type is inherently compatible with that interface. + */ + public static int sqlite3_collation_needed( + @NotNull sqlite3 db, @Nullable CollationNeededCallback callback + ){ + return sqlite3_collation_needed(db.getNativePointer(), callback); + } + + private static native CommitHookCallback sqlite3_commit_hook( + @NotNull long ptrToDb, @Nullable CommitHookCallback hook + ); + + public static CommitHookCallback sqlite3_commit_hook( + @NotNull sqlite3 db, @Nullable CommitHookCallback hook + ){ + return sqlite3_commit_hook(db.getNativePointer(), hook); + } + + public static native String sqlite3_compileoption_get(int n); + + public static native boolean sqlite3_compileoption_used(String optName); + + /** + This implementation is private because it's too easy to pass it + non-NUL-terminated byte arrays from client code. + */ + private static native int sqlite3_complete( + @NotNull byte[] nulTerminatedUtf8Sql + ); + + /** + Unlike the C API, this returns SQLITE_MISUSE if its argument is + null (as opposed to invoking UB). + */ + public static int sqlite3_complete(@NotNull String sql){ + return sqlite3_complete( nulTerminateUtf8(sql) ); + } + + /** + Internal level of indirection for sqlite3_config(int). + */ + private static native int sqlite3_config__enable(int op); + + /** + Internal level of indirection for sqlite3_config(ConfigLogCallback). + */ + private static native int sqlite3_config__CONFIG_LOG( + @Nullable ConfigLogCallback logger + ); + + /** + Internal level of indirection for sqlite3_config(ConfigSqlLogCallback). + */ + private static native int sqlite3_config__SQLLOG( + @Nullable ConfigSqlLogCallback logger + ); + + /** +

    Works like in the C API with the exception that it only supports + the following subset of configuration flags: + +

    SQLITE_CONFIG_SINGLETHREAD + SQLITE_CONFIG_MULTITHREAD + SQLITE_CONFIG_SERIALIZED + +

    Others may be added in the future. It returns SQLITE_MISUSE if + given an argument it does not handle. + +

    Note that sqlite3_config() is not threadsafe with regards to + the rest of the library. This must not be called when any other + library APIs are being called. + */ + public static int sqlite3_config(int op){ + return sqlite3_config__enable(op); + } + + /** + If the native library was built with SQLITE_ENABLE_SQLLOG defined + then this acts as a proxy for C's + sqlite3_config(SQLITE_CONFIG_SQLLOG,...). This sets or clears the + logger. If installation of a logger fails, any previous logger is + retained. + +

    If not built with SQLITE_ENABLE_SQLLOG defined, this returns + SQLITE_MISUSE. + +

    Note that sqlite3_config() is not threadsafe with regards to + the rest of the library. This must not be called when any other + library APIs are being called. + */ + public static int sqlite3_config( @Nullable ConfigSqlLogCallback logger ){ + return sqlite3_config__SQLLOG(logger); + } + + /** + The sqlite3_config() overload for handling the SQLITE_CONFIG_LOG + option. + */ + public static int sqlite3_config( @Nullable ConfigLogCallback logger ){ + return sqlite3_config__CONFIG_LOG(logger); + } + + /** + Unlike the C API, this returns null if its argument is + null (as opposed to invoking UB). + */ + public static native sqlite3 sqlite3_context_db_handle( + @NotNull sqlite3_context cx + ); + + public static native int sqlite3_create_collation( + @NotNull sqlite3 db, @NotNull String name, int eTextRep, + @NotNull CollationCallback col + ); + + /** + The Java counterpart to the C-native sqlite3_create_function(), + sqlite3_create_function_v2(), and + sqlite3_create_window_function(). Which one it behaves like + depends on which methods the final argument implements. See + SQLFunction's subclasses (ScalarFunction, AggregateFunction, + and WindowFunction) for details. + +

    Unlike the C API, this returns SQLITE_MISUSE null if its db or + functionName arguments are null (as opposed to invoking UB). + */ + public static native int sqlite3_create_function( + @NotNull sqlite3 db, @NotNull String functionName, + int nArg, int eTextRep, @NotNull SQLFunction func + ); + + private static native int sqlite3_data_count(@NotNull long ptrToStmt); + + public static int sqlite3_data_count(@NotNull sqlite3_stmt stmt){ + return sqlite3_data_count(stmt.getNativePointer()); + } + + /** + Overload for sqlite3_db_config() calls which take (int,int*) + variadic arguments. Returns SQLITE_MISUSE if op is not one of the + SQLITE_DBCONFIG_... options which uses this call form. + +

    Unlike the C API, this returns SQLITE_MISUSE if its db argument + is null (as opposed to invoking UB). + */ + public static native int sqlite3_db_config( + @NotNull sqlite3 db, int op, int onOff, @Nullable OutputPointer.Int32 out + ); + + /** + Overload for sqlite3_db_config() calls which take a (const char*) + variadic argument. As of SQLite3 v3.43 the only such option is + SQLITE_DBCONFIG_MAINDBNAME. Returns SQLITE_MISUSE if op is not + SQLITE_DBCONFIG_MAINDBNAME, but that set of options may be + extended in future versions. + */ + public static native int sqlite3_db_config( + @NotNull sqlite3 db, int op, @NotNull String val + ); + + private static native String sqlite3_db_name(@NotNull long ptrToDb, int ndx); + + public static String sqlite3_db_name(@NotNull sqlite3 db, int ndx){ + return null==db ? null : sqlite3_db_name(db.getNativePointer(), ndx); + } + + public static native String sqlite3_db_filename( + @NotNull sqlite3 db, @NotNull String dbName + ); + + public static native sqlite3 sqlite3_db_handle(@NotNull sqlite3_stmt stmt); + + public static native int sqlite3_db_readonly(@NotNull sqlite3 db, String dbName); + + public static native int sqlite3_db_release_memory(sqlite3 db); + + public static native int sqlite3_db_status( + @NotNull sqlite3 db, int op, @NotNull OutputPointer.Int32 pCurrent, + @NotNull OutputPointer.Int32 pHighwater, boolean reset + ); + + public static native int sqlite3_errcode(@NotNull sqlite3 db); + + public static native String sqlite3_errmsg(@NotNull sqlite3 db); + + /** Added in 3.51.0. */ + public static native int sqlite3_set_errmsg(@NotNull sqlite3 db, + int resultCode, + String msg); + + private static native int sqlite3_error_offset(@NotNull long ptrToDb); + + /** + Caveat: the returned byte offset values assume UTF-8-encoded + inputs, so won't always match character offsets in Java Strings. + */ + public static int sqlite3_error_offset(@NotNull sqlite3 db){ + return sqlite3_error_offset(db.getNativePointer()); + } + + public static native String sqlite3_errstr(int resultCode); + + public static native String sqlite3_expanded_sql(@NotNull sqlite3_stmt stmt); + + private static native int sqlite3_extended_errcode(@NotNull long ptrToDb); + + public static int sqlite3_extended_errcode(@NotNull sqlite3 db){ + return sqlite3_extended_errcode(db.getNativePointer()); + } + + public static native int sqlite3_extended_result_codes( + @NotNull sqlite3 db, boolean on + ); + + private static native boolean sqlite3_get_autocommit(@NotNull long ptrToDb); + + public static boolean sqlite3_get_autocommit(@NotNull sqlite3 db){ + return sqlite3_get_autocommit(db.getNativePointer()); + } + + public static native Object sqlite3_get_auxdata( + @NotNull sqlite3_context cx, int n + ); + + private static native int sqlite3_finalize(long ptrToStmt); + + public static int sqlite3_finalize(@NotNull sqlite3_stmt stmt){ + return null==stmt ? 0 : sqlite3_finalize(stmt.clearNativePointer()); + } + + public static native int sqlite3_initialize(); + + public static native void sqlite3_interrupt(@NotNull sqlite3 db); + + public static native boolean sqlite3_is_interrupted(@NotNull sqlite3 db); + + public static native boolean sqlite3_keyword_check(@NotNull String word); + + public static native int sqlite3_keyword_count(); + + public static native String sqlite3_keyword_name(int index); + + + public static native long sqlite3_last_insert_rowid(@NotNull sqlite3 db); + + public static native String sqlite3_libversion(); + + public static native int sqlite3_libversion_number(); + + public static native int sqlite3_limit(@NotNull sqlite3 db, int id, int newVal); + + /** + Only available if built with SQLITE_ENABLE_NORMALIZE. If not, it always + returns null. + */ + public static native String sqlite3_normalized_sql(@NotNull sqlite3_stmt stmt); + + /** + Works like its C counterpart and makes the native pointer of the + underling (sqlite3*) object available via + ppDb.getNativePointer(). That pointer is necessary for looking up + the JNI-side native, but clients need not pay it any + heed. Passing the object to sqlite3_close() or sqlite3_close_v2() + will clear that pointer mapping. + +

    Recall that even if opening fails, the output pointer might be + non-null. Any error message about the failure will be in that + object and it is up to the caller to sqlite3_close() that + db handle. + */ + public static native int sqlite3_open( + @Nullable String filename, @NotNull OutputPointer.sqlite3 ppDb + ); + + /** + Convenience overload which returns its db handle directly. The returned + object might not have been successfully opened: use sqlite3_errcode() to + check whether it is in an error state. + +

    Ownership of the returned value is passed to the caller, who must eventually + pass it to sqlite3_close() or sqlite3_close_v2(). + */ + public static sqlite3 sqlite3_open(@Nullable String filename){ + final OutputPointer.sqlite3 out = new OutputPointer.sqlite3(); + sqlite3_open(filename, out); + return out.take(); + } + + public static native int sqlite3_open_v2( + @Nullable String filename, @NotNull OutputPointer.sqlite3 ppDb, + int flags, @Nullable String zVfs + ); + + /** + Has the same semantics as the sqlite3-returning sqlite3_open() + but uses sqlite3_open_v2() instead of sqlite3_open(). + */ + public static sqlite3 sqlite3_open_v2(@Nullable String filename, int flags, + @Nullable String zVfs){ + final OutputPointer.sqlite3 out = new OutputPointer.sqlite3(); + sqlite3_open_v2(filename, out, flags, zVfs); + return out.take(); + } + + /** + The sqlite3_prepare() family of functions require slightly + different signatures than their native counterparts, but (A) they + retain functionally equivalent semantics and (B) overloading + allows us to install several convenience forms. + +

    All of them which take their SQL in the form of a byte[] require + that it be in UTF-8 encoding unless explicitly noted otherwise. + +

    The forms which take a "tail" output pointer return (via that + output object) the index into their SQL byte array at which the + end of the first SQL statement processed by the call was + found. That's fundamentally how the C APIs work but making use of + that value requires more copying of the input SQL into + consecutively smaller arrays in order to consume all of + it. (There is an example of doing that in this project's Tester1 + class.) For that vast majority of uses, that capability is not + necessary, however, and overloads are provided which gloss over + that. + +

    Results are undefined if maxBytes>sqlUtf8.length. + +

    This routine is private because its maxBytes value is not + strictly necessary in the Java interface, as sqlUtf8.length tells + us the information we need. Making this public would give clients + more ways to shoot themselves in the foot without providing any + real utility. + */ + private static native int sqlite3_prepare( + @NotNull long ptrToDb, @NotNull byte[] sqlUtf8, int maxBytes, + @NotNull OutputPointer.sqlite3_stmt outStmt, + @Nullable OutputPointer.Int32 pTailOffset + ); + + /** + Works like the canonical sqlite3_prepare() but its "tail" output + argument is returned as the index offset into the given + UTF-8-encoded byte array at which SQL parsing stopped. The + semantics are otherwise identical to the C API counterpart. + +

    Several overloads provided simplified call signatures. + */ + public static int sqlite3_prepare( + @NotNull sqlite3 db, @NotNull byte[] sqlUtf8, + @NotNull OutputPointer.sqlite3_stmt outStmt, + @Nullable OutputPointer.Int32 pTailOffset + ){ + return sqlite3_prepare(db.getNativePointer(), sqlUtf8, sqlUtf8.length, + outStmt, pTailOffset); + } + + public static int sqlite3_prepare( + @NotNull sqlite3 db, @NotNull byte[] sqlUtf8, + @NotNull OutputPointer.sqlite3_stmt outStmt + ){ + return sqlite3_prepare(db.getNativePointer(), sqlUtf8, sqlUtf8.length, + outStmt, null); + } + + public static int sqlite3_prepare( + @NotNull sqlite3 db, @NotNull String sql, + @NotNull OutputPointer.sqlite3_stmt outStmt + ){ + final byte[] utf8 = sql.getBytes(StandardCharsets.UTF_8); + return sqlite3_prepare(db.getNativePointer(), utf8, utf8.length, + outStmt, null); + } + + /** + Convenience overload which returns its statement handle directly, + or null on error or when reading only whitespace or + comments. sqlite3_errcode() can be used to determine whether + there was an error or the input was empty. Ownership of the + returned object is passed to the caller, who must eventually pass + it to sqlite3_finalize(). + */ + public static sqlite3_stmt sqlite3_prepare( + @NotNull sqlite3 db, @NotNull String sql + ){ + final OutputPointer.sqlite3_stmt out = new OutputPointer.sqlite3_stmt(); + sqlite3_prepare(db, sql, out); + return out.take(); + } + /** + @see #sqlite3_prepare + */ + private static native int sqlite3_prepare_v2( + @NotNull long ptrToDb, @NotNull byte[] sqlUtf8, int maxBytes, + @NotNull OutputPointer.sqlite3_stmt outStmt, + @Nullable OutputPointer.Int32 pTailOffset + ); + + /** + Works like the canonical sqlite3_prepare_v2() but its "tail" + output parameter is returned as the index offset into the given + byte array at which SQL parsing stopped. + */ + public static int sqlite3_prepare_v2( + @NotNull sqlite3 db, @NotNull byte[] sqlUtf8, + @NotNull OutputPointer.sqlite3_stmt outStmt, + @Nullable OutputPointer.Int32 pTailOffset + ){ + return sqlite3_prepare_v2(db.getNativePointer(), sqlUtf8, sqlUtf8.length, + outStmt, pTailOffset); + } + + public static int sqlite3_prepare_v2( + @NotNull sqlite3 db, @NotNull byte[] sqlUtf8, + @NotNull OutputPointer.sqlite3_stmt outStmt + ){ + return sqlite3_prepare_v2(db.getNativePointer(), sqlUtf8, sqlUtf8.length, + outStmt, null); + } + + public static int sqlite3_prepare_v2( + @NotNull sqlite3 db, @NotNull String sql, + @NotNull OutputPointer.sqlite3_stmt outStmt + ){ + final byte[] utf8 = sql.getBytes(StandardCharsets.UTF_8); + return sqlite3_prepare_v2(db.getNativePointer(), utf8, utf8.length, + outStmt, null); + } + + /** + Works identically to the sqlite3_stmt-returning sqlite3_prepare() + but uses sqlite3_prepare_v2(). + */ + public static sqlite3_stmt sqlite3_prepare_v2( + @NotNull sqlite3 db, @NotNull String sql + ){ + final OutputPointer.sqlite3_stmt out = new OutputPointer.sqlite3_stmt(); + sqlite3_prepare_v2(db, sql, out); + return out.take(); + } + + /** + @see #sqlite3_prepare + */ + private static native int sqlite3_prepare_v3( + @NotNull long ptrToDb, @NotNull byte[] sqlUtf8, int maxBytes, + int prepFlags, @NotNull OutputPointer.sqlite3_stmt outStmt, + @Nullable OutputPointer.Int32 pTailOffset + ); + + /** + Works like the canonical sqlite3_prepare_v2() but its "tail" + output parameter is returned as the index offset into the given + byte array at which SQL parsing stopped. + */ + public static int sqlite3_prepare_v3( + @NotNull sqlite3 db, @NotNull byte[] sqlUtf8, int prepFlags, + @NotNull OutputPointer.sqlite3_stmt outStmt, + @Nullable OutputPointer.Int32 pTailOffset + ){ + return sqlite3_prepare_v3(db.getNativePointer(), sqlUtf8, sqlUtf8.length, + prepFlags, outStmt, pTailOffset); + } + + /** + Convenience overload which elides the seldom-used pTailOffset + parameter. + */ + public static int sqlite3_prepare_v3( + @NotNull sqlite3 db, @NotNull byte[] sqlUtf8, int prepFlags, + @NotNull OutputPointer.sqlite3_stmt outStmt + ){ + return sqlite3_prepare_v3(db.getNativePointer(), sqlUtf8, sqlUtf8.length, + prepFlags, outStmt, null); + } + + /** + Convenience overload which elides the seldom-used pTailOffset + parameter and converts the given string to UTF-8 before passing + it on. + */ + public static int sqlite3_prepare_v3( + @NotNull sqlite3 db, @NotNull String sql, int prepFlags, + @NotNull OutputPointer.sqlite3_stmt outStmt + ){ + final byte[] utf8 = sql.getBytes(StandardCharsets.UTF_8); + return sqlite3_prepare_v3(db.getNativePointer(), utf8, utf8.length, + prepFlags, outStmt, null); + } + + /** + Works identically to the sqlite3_stmt-returning sqlite3_prepare() + but uses sqlite3_prepare_v3(). + */ + public static sqlite3_stmt sqlite3_prepare_v3( + @NotNull sqlite3 db, @NotNull String sql, int prepFlags + ){ + final OutputPointer.sqlite3_stmt out = new OutputPointer.sqlite3_stmt(); + sqlite3_prepare_v3(db, sql, prepFlags, out); + return out.take(); + } + + /** + A convenience wrapper around sqlite3_prepare_v3() which accepts + an arbitrary amount of input provided as a UTF-8-encoded byte + array. It loops over the input bytes looking for + statements. Each one it finds is passed to p.call(), passing + ownership of it to that function. If p.call() returns 0, looping + continues, else the loop stops and p.call()'s result code is + returned. If preparation of any given segment fails, looping + stops and that result code is returned. + +

    If p.call() throws, the exception is converted to a db-level + error and a non-0 code is returned, in order to retain the + C-style error semantics of the API. + +

    How each statement is handled, including whether it is finalized + or not, is up to the callback object. e.g. the callback might + collect them for later use. If it does not collect them then it + must finalize them. See PrepareMultiCallback.Finalize for a + simple proxy which does that. + */ + public static int sqlite3_prepare_multi( + @NotNull sqlite3 db, @NotNull byte[] sqlUtf8, + int prepFlags, + @NotNull PrepareMultiCallback p){ + final OutputPointer.Int32 oTail = new OutputPointer.Int32(); + int pos = 0, n = 1; + byte[] sqlChunk = sqlUtf8; + int rc = 0; + final OutputPointer.sqlite3_stmt outStmt = new OutputPointer.sqlite3_stmt(); + while( 0==rc && pos0 ){ + sqlChunk = Arrays.copyOfRange(sqlChunk, pos, + sqlChunk.length); + } + if( 0==sqlChunk.length ) break; + rc = sqlite3_prepare_v3(db, sqlChunk, prepFlags, outStmt, oTail); + if( 0!=rc ) break; + pos = oTail.value; + stmt = outStmt.take(); + if( null==stmt ){ + // empty statement (whitespace/comments) + continue; + } + try{ + rc = p.call(stmt); + }catch(Exception e){ + rc = sqlite3_jni_db_error( db, SQLITE_ERROR, e ); + } + } + return rc; + } + + /** + Convenience overload which accepts its SQL as a String and uses + no statement-preparation flags. + */ + public static int sqlite3_prepare_multi( + @NotNull sqlite3 db, @NotNull byte[] sqlUtf8, + @NotNull PrepareMultiCallback p){ + return sqlite3_prepare_multi(db, sqlUtf8, 0, p); + } + + /** + Convenience overload which accepts its SQL as a String. + */ + public static int sqlite3_prepare_multi( + @NotNull sqlite3 db, @NotNull String sql, int prepFlags, + @NotNull PrepareMultiCallback p){ + return sqlite3_prepare_multi( + db, sql.getBytes(StandardCharsets.UTF_8), prepFlags, p + ); + } + + /** + Convenience overload which accepts its SQL as a String and uses + no statement-preparation flags. + */ + public static int sqlite3_prepare_multi( + @NotNull sqlite3 db, @NotNull String sql, + @NotNull PrepareMultiCallback p){ + return sqlite3_prepare_multi(db, sql, 0, p); + } + + /** + Convenience overload which accepts its SQL as a String + array. They will be concatenated together as-is, with no + separator, and passed on to one of the other overloads. + */ + public static int sqlite3_prepare_multi( + @NotNull sqlite3 db, @NotNull String[] sql, int prepFlags, + @NotNull PrepareMultiCallback p){ + return sqlite3_prepare_multi(db, String.join("",sql), prepFlags, p); + } + + /** + Convenience overload which uses no statement-preparation flags. + */ + public static int sqlite3_prepare_multi( + @NotNull sqlite3 db, @NotNull String[] sql, + @NotNull PrepareMultiCallback p){ + return sqlite3_prepare_multi(db, sql, 0, p); + } + + private static native int sqlite3_preupdate_blobwrite(@NotNull long ptrToDb); + + /** + If the C API was built with SQLITE_ENABLE_PREUPDATE_HOOK defined, this + acts as a proxy for C's sqlite3_preupdate_blobwrite(), else it returns + SQLITE_MISUSE with no side effects. + */ + public static int sqlite3_preupdate_blobwrite(@NotNull sqlite3 db){ + return sqlite3_preupdate_blobwrite(db.getNativePointer()); + } + + private static native int sqlite3_preupdate_count(@NotNull long ptrToDb); + + /** + If the C API was built with SQLITE_ENABLE_PREUPDATE_HOOK defined, this + acts as a proxy for C's sqlite3_preupdate_count(), else it returns + SQLITE_MISUSE with no side effects. + */ + public static int sqlite3_preupdate_count(@NotNull sqlite3 db){ + return sqlite3_preupdate_count(db.getNativePointer()); + } + + private static native int sqlite3_preupdate_depth(@NotNull long ptrToDb); + + /** + If the C API was built with SQLITE_ENABLE_PREUPDATE_HOOK defined, this + acts as a proxy for C's sqlite3_preupdate_depth(), else it returns + SQLITE_MISUSE with no side effects. + */ + public static int sqlite3_preupdate_depth(@NotNull sqlite3 db){ + return sqlite3_preupdate_depth(db.getNativePointer()); + } + + private static native PreupdateHookCallback sqlite3_preupdate_hook( + @NotNull long ptrToDb, @Nullable PreupdateHookCallback hook + ); + + /** + If the C API was built with SQLITE_ENABLE_PREUPDATE_HOOK defined, this + acts as a proxy for C's sqlite3_preupdate_hook(), else it returns null + with no side effects. + */ + public static PreupdateHookCallback sqlite3_preupdate_hook( + @NotNull sqlite3 db, @Nullable PreupdateHookCallback hook + ){ + return sqlite3_preupdate_hook(db.getNativePointer(), hook); + } + + private static native int sqlite3_preupdate_new(@NotNull long ptrToDb, int col, + @NotNull OutputPointer.sqlite3_value out); + + /** + If the C API was built with SQLITE_ENABLE_PREUPDATE_HOOK defined, + this acts as a proxy for C's sqlite3_preupdate_new(), else it + returns SQLITE_MISUSE with no side effects. + + WARNING: client code _must not_ hold a reference to the returned + sqlite3_value object beyond the scope of the preupdate hook in + which this function is called. Doing so will leave the client + holding a stale pointer, the address of which could point to + anything at all after the pre-update hook is complete. This API + has no way to record such objects and clear/invalidate them at + the end of a pre-update hook. We "could" add infrastructure to do + so, but would require significant levels of bookkeeping. + */ + public static int sqlite3_preupdate_new(@NotNull sqlite3 db, int col, + @NotNull OutputPointer.sqlite3_value out){ + return sqlite3_preupdate_new(db.getNativePointer(), col, out); + } + + /** + Convenience wrapper for the 3-arg sqlite3_preupdate_new() which returns + null on error. + */ + public static sqlite3_value sqlite3_preupdate_new(@NotNull sqlite3 db, int col){ + final OutputPointer.sqlite3_value out = new OutputPointer.sqlite3_value(); + sqlite3_preupdate_new(db.getNativePointer(), col, out); + return out.take(); + } + + private static native int sqlite3_preupdate_old(@NotNull long ptrToDb, int col, + @NotNull OutputPointer.sqlite3_value out); + + /** + If the C API was built with SQLITE_ENABLE_PREUPDATE_HOOK defined, + this acts as a proxy for C's sqlite3_preupdate_old(), else it + returns SQLITE_MISUSE with no side effects. + + WARNING: see warning in sqlite3_preupdate_new() regarding the + potential for stale sqlite3_value handles. + */ + public static int sqlite3_preupdate_old(@NotNull sqlite3 db, int col, + @NotNull OutputPointer.sqlite3_value out){ + return sqlite3_preupdate_old(db.getNativePointer(), col, out); + } + + /** + Convenience wrapper for the 3-arg sqlite3_preupdate_old() which returns + null on error. + */ + public static sqlite3_value sqlite3_preupdate_old(@NotNull sqlite3 db, int col){ + final OutputPointer.sqlite3_value out = new OutputPointer.sqlite3_value(); + sqlite3_preupdate_old(db.getNativePointer(), col, out); + return out.take(); + } + + public static native void sqlite3_progress_handler( + @NotNull sqlite3 db, int n, @Nullable ProgressHandlerCallback h + ); + + public static native void sqlite3_randomness(byte[] target); + + public static native int sqlite3_release_memory(int n); + + public static native int sqlite3_reset(@NotNull sqlite3_stmt stmt); + + /** + Works like the C API except that it has no side effects if auto + extensions are currently running. (The JNI-level list of + extensions cannot be manipulated while it is being traversed.) + */ + public static native void sqlite3_reset_auto_extension(); + + public static native void sqlite3_result_double( + @NotNull sqlite3_context cx, double v + ); + + /** + The main sqlite3_result_error() impl of which all others are + proxies. eTextRep must be one of SQLITE_UTF8 or SQLITE_UTF16 and + msg must be encoded correspondingly. Any other eTextRep value + results in the C-level sqlite3_result_error() being called with a + complaint about the invalid argument. + */ + private static native void sqlite3_result_error( + @NotNull sqlite3_context cx, @NotNull byte[] msg, int eTextRep + ); + + public static void sqlite3_result_error( + @NotNull sqlite3_context cx, @NotNull byte[] utf8 + ){ + sqlite3_result_error(cx, utf8, SQLITE_UTF8); + } + + public static void sqlite3_result_error( + @NotNull sqlite3_context cx, @NotNull String msg + ){ + final byte[] utf8 = msg.getBytes(StandardCharsets.UTF_8); + sqlite3_result_error(cx, utf8, SQLITE_UTF8); + } + + public static void sqlite3_result_error16( + @NotNull sqlite3_context cx, @NotNull byte[] utf16 + ){ + sqlite3_result_error(cx, utf16, SQLITE_UTF16); + } + + public static void sqlite3_result_error16( + @NotNull sqlite3_context cx, @NotNull String msg + ){ + final byte[] utf16 = msg.getBytes(StandardCharsets.UTF_16); + sqlite3_result_error(cx, utf16, SQLITE_UTF16); + } + + /** + Equivalent to passing e.toString() to {@link + #sqlite3_result_error(sqlite3_context,String)}. Note that + toString() is used instead of getMessage() because the former + prepends the exception type name to the message. + */ + public static void sqlite3_result_error( + @NotNull sqlite3_context cx, @NotNull Exception e + ){ + sqlite3_result_error(cx, e.toString()); + } + + public static native void sqlite3_result_error_toobig( + @NotNull sqlite3_context cx + ); + + public static native void sqlite3_result_error_nomem( + @NotNull sqlite3_context cx + ); + + public static native void sqlite3_result_error_code( + @NotNull sqlite3_context cx, int c + ); + + public static native void sqlite3_result_int( + @NotNull sqlite3_context cx, int v + ); + + public static native void sqlite3_result_int64( + @NotNull sqlite3_context cx, long v + ); + + /** + Binds the SQL result to the given object, or {@link + #sqlite3_result_null} if {@code o} is null. Use {@link + #sqlite3_value_java_object} to fetch it. + +

    This is implemented in terms of C's sqlite3_result_pointer(), + but that function is not exposed to JNI because (A) + cross-language semantic mismatch and (B) Java doesn't need that + argument for its intended purpose (type safety). + + @see #sqlite3_value_java_object + @see #sqlite3_bind_java_object + */ + public static native void sqlite3_result_java_object( + @NotNull sqlite3_context cx, @NotNull Object o + ); + + /** + Similar to sqlite3_bind_nio_buffer(), this works like + sqlite3_result_blob() but accepts a java.nio.ByteBuffer as its + input source. See sqlite3_bind_nio_buffer() for the semantics of + the second and subsequent arguments. + + If cx is null then this function will silently fail. If + sqlite3_jni_supports_nio() returns false or iBegin is negative, + an error result is set. If (begin+n) extends beyond the end of + the buffer, it is silently truncated to fit. + + If any of the following apply, this function behaves like + sqlite3_result_null(): the blob is null, the resulting slice of + the blob is empty. + + If the resulting slice of the buffer exceeds SQLITE_LIMIT_LENGTH + then this function behaves like sqlite3_result_error_toobig(). + */ + @Experimental + /*public*/ static native void sqlite3_result_nio_buffer( + @NotNull sqlite3_context cx, @Nullable java.nio.ByteBuffer blob, + int begin, int n + ); + + /** + Convenience overload which uses the whole input object + as the result blob content. + */ + @Experimental + /*public*/ static void sqlite3_result_nio_buffer( + @NotNull sqlite3_context cx, @Nullable java.nio.ByteBuffer blob + ){ + sqlite3_result_nio_buffer(cx, blob, 0, -1); + } + + public static native void sqlite3_result_null( + @NotNull sqlite3_context cx + ); + + public static void sqlite3_result_set( + @NotNull sqlite3_context cx, @NotNull Boolean v + ){ + sqlite3_result_int(cx, v ? 1 : 0); + } + + public static void sqlite3_result_set( + @NotNull sqlite3_context cx, boolean v + ){ + sqlite3_result_int(cx, v ? 1 : 0); + } + + public static void sqlite3_result_set( + @NotNull sqlite3_context cx, @NotNull Double v + ){ + sqlite3_result_double(cx, v); + } + + public static void sqlite3_result_set( + @NotNull sqlite3_context cx, double v + ){ + sqlite3_result_double(cx, v); + } + + public static void sqlite3_result_set( + @NotNull sqlite3_context cx, @NotNull Integer v + ){ + sqlite3_result_int(cx, v); + } + + public static void sqlite3_result_set(@NotNull sqlite3_context cx, int v){ + sqlite3_result_int(cx, v); + } + + public static void sqlite3_result_set( + @NotNull sqlite3_context cx, @NotNull Long v + ){ + sqlite3_result_int64(cx, v); + } + + public static void sqlite3_result_set( + @NotNull sqlite3_context cx, long v + ){ + sqlite3_result_int64(cx, v); + } + + public static void sqlite3_result_set( + @NotNull sqlite3_context cx, @Nullable String v + ){ + if( null==v ) sqlite3_result_null(cx); + else sqlite3_result_text(cx, v); + } + + public static void sqlite3_result_set( + @NotNull sqlite3_context cx, @Nullable byte[] blob + ){ + if( null==blob ) sqlite3_result_null(cx); + else sqlite3_result_blob(cx, blob, blob.length); + } + + public static native void sqlite3_result_subtype( + @NotNull sqlite3_context cx, int val + ); + + public static native void sqlite3_result_value( + @NotNull sqlite3_context cx, @NotNull sqlite3_value v + ); + + public static native void sqlite3_result_zeroblob( + @NotNull sqlite3_context cx, int n + ); + + public static native int sqlite3_result_zeroblob64( + @NotNull sqlite3_context cx, long n + ); + + /** + This overload is private because its final parameter is arguably + unnecessary in Java. + */ + private static native void sqlite3_result_blob( + @NotNull sqlite3_context cx, @Nullable byte[] blob, int maxLen + ); + + public static void sqlite3_result_blob( + @NotNull sqlite3_context cx, @Nullable byte[] blob + ){ + sqlite3_result_blob(cx, blob, (int)(null==blob ? 0 : blob.length)); + } + + /** + Convenience overload which behaves like + sqlite3_result_nio_buffer(). + */ + @Experimental + /*public*/ static void sqlite3_result_blob( + @NotNull sqlite3_context cx, @Nullable java.nio.ByteBuffer blob, + int begin, int n + ){ + sqlite3_result_nio_buffer(cx, blob, begin, n); + } + + /** + Convenience overload which behaves like the two-argument overload of + sqlite3_result_nio_buffer(). + */ + @Experimental + /*public*/ static void sqlite3_result_blob( + @NotNull sqlite3_context cx, @Nullable java.nio.ByteBuffer blob + ){ + sqlite3_result_nio_buffer(cx, blob); + } + + /** + Binds the given text using C's sqlite3_result_blob64() unless: + +

      + +
    • @param blob is null: translates to sqlite3_result_null()
    • + +
    • @param blob is too large: translates to + sqlite3_result_error_toobig()
    • + +
    + +

    If @param maxLen is larger than blob.length, it is truncated + to that value. If it is negative, results are undefined.

    + +

    This overload is private because its final parameter is + arguably unnecessary in Java.

    + */ + private static native void sqlite3_result_blob64( + @NotNull sqlite3_context cx, @Nullable byte[] blob, long maxLen + ); + + public static void sqlite3_result_blob64( + @NotNull sqlite3_context cx, @Nullable byte[] blob + ){ + sqlite3_result_blob64(cx, blob, (long)(null==blob ? 0 : blob.length)); + } + + /** + This overload is private because its final parameter is + arguably unnecessary in Java. + */ + private static native void sqlite3_result_text( + @NotNull sqlite3_context cx, @Nullable byte[] utf8, int maxLen + ); + + public static void sqlite3_result_text( + @NotNull sqlite3_context cx, @Nullable byte[] utf8 + ){ + sqlite3_result_text(cx, utf8, null==utf8 ? 0 : utf8.length); + } + + public static void sqlite3_result_text( + @NotNull sqlite3_context cx, @Nullable String text + ){ + if(null == text) sqlite3_result_null(cx); + else{ + final byte[] utf8 = text.getBytes(StandardCharsets.UTF_8); + sqlite3_result_text(cx, utf8, utf8.length); + } + } + + /** + Binds the given text using C's sqlite3_result_text64() unless: + +
      + +
    • text is null: translates to a call to {@link + #sqlite3_result_null}
    • + +
    • text is too large: translates to a call to + {@link #sqlite3_result_error_toobig}
    • + +
    • The @param encoding argument has an invalid value: translates to + {@link sqlite3_result_error_code} with code SQLITE_FORMAT.
    • + +
    + + If maxLength (in bytes, not characters) is larger than + text.length, it is silently truncated to text.length. If it is + negative, results are undefined. If text is null, the subsequent + arguments are ignored. + + This overload is private because its maxLength parameter is + arguably unnecessary in Java. + */ + private static native void sqlite3_result_text64( + @NotNull sqlite3_context cx, @Nullable byte[] text, + long maxLength, int encoding + ); + + /** + Sets the current UDF result to the given bytes, which are assumed + be encoded in UTF-16 using the platform's byte order. + */ + public static void sqlite3_result_text16( + @NotNull sqlite3_context cx, @Nullable byte[] utf16 + ){ + if(null == utf16) sqlite3_result_null(cx); + else sqlite3_result_text64(cx, utf16, utf16.length, SQLITE_UTF16); + } + + public static void sqlite3_result_text16( + @NotNull sqlite3_context cx, @Nullable String text + ){ + if(null == text) sqlite3_result_null(cx); + else{ + final byte[] b = text.getBytes(StandardCharsets.UTF_16); + sqlite3_result_text64(cx, b, b.length, SQLITE_UTF16); + } + } + + private static native RollbackHookCallback sqlite3_rollback_hook( + @NotNull long ptrToDb, @Nullable RollbackHookCallback hook + ); + + public static RollbackHookCallback sqlite3_rollback_hook( + @NotNull sqlite3 db, @Nullable RollbackHookCallback hook + ){ + return sqlite3_rollback_hook(db.getNativePointer(), hook); + } + + public static native int sqlite3_set_authorizer( + @NotNull sqlite3 db, @Nullable AuthorizerCallback auth + ); + + public static native void sqlite3_set_auxdata( + @NotNull sqlite3_context cx, int n, @Nullable Object data + ); + + public static native void sqlite3_set_last_insert_rowid( + @NotNull sqlite3 db, long rowid + ); + + + /** + In addition to calling the C-level sqlite3_shutdown(), the JNI + binding also cleans up all stale per-thread state managed by the + library, as well as any registered auto-extensions, and frees up + various bits of memory. Calling this while database handles or + prepared statements are still active will leak resources. Trying + to use those objects after this routine is called invoked + undefined behavior. + */ + public static synchronized native int sqlite3_shutdown(); + + public static native int sqlite3_sleep(int ms); + + public static native String sqlite3_sourceid(); + + public static native String sqlite3_sql(@NotNull sqlite3_stmt stmt); + + //! Consider removing this. We can use sqlite3_status64() instead, + // or use that one's impl with this one's name. + public static native int sqlite3_status( + int op, @NotNull OutputPointer.Int32 pCurrent, + @NotNull OutputPointer.Int32 pHighwater, boolean reset + ); + + public static native int sqlite3_status64( + int op, @NotNull OutputPointer.Int64 pCurrent, + @NotNull OutputPointer.Int64 pHighwater, boolean reset + ); + + private static native int sqlite3_step(@NotNull long ptrToStmt); + + public static int sqlite3_step(@NotNull sqlite3_stmt stmt){ + return null==stmt ? SQLITE_MISUSE : sqlite3_step(stmt.getNativePointer()); + } + + public static native boolean sqlite3_stmt_busy(@NotNull sqlite3_stmt stmt); + + private static native int sqlite3_stmt_explain(@NotNull long ptrToStmt, int op); + + public static int sqlite3_stmt_explain(@NotNull sqlite3_stmt stmt, int op){ + return null==stmt ? SQLITE_MISUSE : sqlite3_stmt_explain(stmt.getNativePointer(), op); + } + + private static native int sqlite3_stmt_isexplain(@NotNull long ptrToStmt); + + public static int sqlite3_stmt_isexplain(@NotNull sqlite3_stmt stmt){ + return null==stmt ? 0 : sqlite3_stmt_isexplain(stmt.getNativePointer()); + } + + public static native boolean sqlite3_stmt_readonly(@NotNull sqlite3_stmt stmt); + + public static native int sqlite3_stmt_status( + @NotNull sqlite3_stmt stmt, int op, boolean reset + ); + + /** + Internal impl of the public sqlite3_strglob() method. Neither + argument may be null and both must be NUL-terminated UTF-8. + + This overload is private because: (A) to keep users from + inadvertently passing non-NUL-terminated byte arrays (an easy + thing to do). (B) it is cheaper to NUL-terminate the + String-to-byte-array conversion in the Java implementation + (sqlite3_strglob(String,String)) than to do that in C, so that + signature is the public-facing one. + */ + private static native int sqlite3_strglob( + @NotNull byte[] glob, @NotNull byte[] nulTerminatedUtf8 + ); + + public static int sqlite3_strglob( + @NotNull String glob, @NotNull String txt + ){ + return sqlite3_strglob(nulTerminateUtf8(glob), + nulTerminateUtf8(txt)); + } + + /** + The LIKE counterpart of the private sqlite3_strglob() method. + */ + private static native int sqlite3_strlike( + @NotNull byte[] glob, @NotNull byte[] nulTerminatedUtf8, + int escChar + ); + + public static int sqlite3_strlike( + @NotNull String glob, @NotNull String txt, char escChar + ){ + return sqlite3_strlike(nulTerminateUtf8(glob), + nulTerminateUtf8(txt), + (int)escChar); + } + + private static native int sqlite3_system_errno(@NotNull long ptrToDb); + + public static int sqlite3_system_errno(@NotNull sqlite3 db){ + return sqlite3_system_errno(db.getNativePointer()); + } + + public static native int sqlite3_table_column_metadata( + @NotNull sqlite3 db, @NotNull String zDbName, + @NotNull String zTableName, @NotNull String zColumnName, + @Nullable OutputPointer.String pzDataType, + @Nullable OutputPointer.String pzCollSeq, + @Nullable OutputPointer.Bool pNotNull, + @Nullable OutputPointer.Bool pPrimaryKey, + @Nullable OutputPointer.Bool pAutoinc + ); + + /** + Convenience overload which returns its results via a single + output object. If this function returns non-0 (error), the the + contents of the output object are not modified. + */ + public static int sqlite3_table_column_metadata( + @NotNull sqlite3 db, @NotNull String zDbName, + @NotNull String zTableName, @NotNull String zColumnName, + @NotNull TableColumnMetadata out){ + return sqlite3_table_column_metadata( + db, zDbName, zTableName, zColumnName, + out.pzDataType, out.pzCollSeq, out.pNotNull, + out.pPrimaryKey, out.pAutoinc); + } + + /** + Convenience overload which returns the column metadata object on + success and null on error. + */ + public static TableColumnMetadata sqlite3_table_column_metadata( + @NotNull sqlite3 db, @NotNull String zDbName, + @NotNull String zTableName, @NotNull String zColumnName){ + final TableColumnMetadata out = new TableColumnMetadata(); + return 0==sqlite3_table_column_metadata( + db, zDbName, zTableName, zColumnName, out + ) ? out : null; + } + + public static native int sqlite3_threadsafe(); + + private static native int sqlite3_total_changes(@NotNull long ptrToDb); + + public static int sqlite3_total_changes(@NotNull sqlite3 db){ + return sqlite3_total_changes(db.getNativePointer()); + } + + private static native long sqlite3_total_changes64(@NotNull long ptrToDb); + + public static long sqlite3_total_changes64(@NotNull sqlite3 db){ + return sqlite3_total_changes64(db.getNativePointer()); + } + + /** + Works like C's sqlite3_trace_v2() except that the 3rd argument to that + function is elided here because the roles of that functions' 3rd and 4th + arguments are encapsulated in the final argument to this function. + +

    Unlike the C API, which is documented as always returning 0, + this implementation returns non-0 if initialization of the tracer + mapping state fails (e.g. on OOM). + */ + public static native int sqlite3_trace_v2( + @NotNull sqlite3 db, int traceMask, @Nullable TraceV2Callback tracer + ); + + public static native int sqlite3_txn_state( + @NotNull sqlite3 db, @Nullable String zSchema + ); + + private static native UpdateHookCallback sqlite3_update_hook( + @NotNull long ptrToDb, @Nullable UpdateHookCallback hook + ); + + public static UpdateHookCallback sqlite3_update_hook( + @NotNull sqlite3 db, @Nullable UpdateHookCallback hook + ){ + return sqlite3_update_hook(db.getNativePointer(), hook); + } + + /* + Note that: + + void * sqlite3_user_data(sqlite3_context*) + + Is not relevant in the JNI binding, as its feature is replaced by + the ability to pass an object, including any relevant state, to + sqlite3_create_function(). + */ + + private static native byte[] sqlite3_value_blob(@NotNull long ptrToValue); + + public static byte[] sqlite3_value_blob(@NotNull sqlite3_value v){ + return sqlite3_value_blob(v.getNativePointer()); + } + + private static native int sqlite3_value_bytes(@NotNull long ptrToValue); + + public static int sqlite3_value_bytes(@NotNull sqlite3_value v){ + return sqlite3_value_bytes(v.getNativePointer()); + } + + private static native int sqlite3_value_bytes16(@NotNull long ptrToValue); + + public static int sqlite3_value_bytes16(@NotNull sqlite3_value v){ + return sqlite3_value_bytes16(v.getNativePointer()); + } + + private static native double sqlite3_value_double(@NotNull long ptrToValue); + + public static double sqlite3_value_double(@NotNull sqlite3_value v){ + return sqlite3_value_double(v.getNativePointer()); + } + + private static native sqlite3_value sqlite3_value_dup(@NotNull long ptrToValue); + + public static sqlite3_value sqlite3_value_dup(@NotNull sqlite3_value v){ + return sqlite3_value_dup(v.getNativePointer()); + } + + private static native int sqlite3_value_encoding(@NotNull long ptrToValue); + + public static int sqlite3_value_encoding(@NotNull sqlite3_value v){ + return sqlite3_value_encoding(v.getNativePointer()); + } + + private static native void sqlite3_value_free(@Nullable long ptrToValue); + + public static void sqlite3_value_free(@Nullable sqlite3_value v){ + if( null!=v ) sqlite3_value_free(v.clearNativePointer()); + } + + private static native boolean sqlite3_value_frombind(@NotNull long ptrToValue); + + public static boolean sqlite3_value_frombind(@NotNull sqlite3_value v){ + return sqlite3_value_frombind(v.getNativePointer()); + } + + private static native int sqlite3_value_int(@NotNull long ptrToValue); + + public static int sqlite3_value_int(@NotNull sqlite3_value v){ + return sqlite3_value_int(v.getNativePointer()); + } + + private static native long sqlite3_value_int64(@NotNull long ptrToValue); + + public static long sqlite3_value_int64(@NotNull sqlite3_value v){ + return sqlite3_value_int64(v.getNativePointer()); + } + + private static native Object sqlite3_value_java_object(@NotNull long ptrToValue); + + /** + If the given value was set using {@link + #sqlite3_result_java_object} then this function returns that + object, else it returns null. + +

    It is up to the caller to inspect the object to determine its + type, and cast it if necessary. + */ + public static Object sqlite3_value_java_object(@NotNull sqlite3_value v){ + return sqlite3_value_java_object(v.getNativePointer()); + } + + /** + A variant of sqlite3_value_java_object() which returns the + fetched object cast to T if the object is an instance of the + given Class, else it returns null. + */ + @SuppressWarnings("unchecked") + public static T sqlite3_value_java_object(@NotNull sqlite3_value v, + @NotNull Class type){ + final Object o = sqlite3_value_java_object(v); + return type.isInstance(o) ? (T)o : null; + } + + /** + A variant of sqlite3_column_blob() which returns the blob as a + ByteBuffer object. Returns null if its argument is null, if + sqlite3_jni_supports_nio() is false, or if sqlite3_value_blob() + would return null for the same input. + */ + @Experimental + /*public*/ static native java.nio.ByteBuffer sqlite3_value_nio_buffer( + @NotNull sqlite3_value v + ); + + private static native int sqlite3_value_nochange(@NotNull long ptrToValue); + + public static int sqlite3_value_nochange(@NotNull sqlite3_value v){ + return sqlite3_value_nochange(v.getNativePointer()); + } + + private static native int sqlite3_value_numeric_type(@NotNull long ptrToValue); + + public static int sqlite3_value_numeric_type(@NotNull sqlite3_value v){ + return sqlite3_value_numeric_type(v.getNativePointer()); + } + + private static native int sqlite3_value_subtype(@NotNull long ptrToValue); + + public static int sqlite3_value_subtype(@NotNull sqlite3_value v){ + return sqlite3_value_subtype(v.getNativePointer()); + } + + private static native byte[] sqlite3_value_text(@NotNull long ptrToValue); + + /** + Functions identially to the C API, and this note is just to + stress that the returned bytes are encoded as UTF-8. It returns + null if the underlying C-level sqlite3_value_text() returns NULL + or on allocation error. + */ + public static byte[] sqlite3_value_text(@NotNull sqlite3_value v){ + return sqlite3_value_text(v.getNativePointer()); + } + + private static native String sqlite3_value_text16(@NotNull long ptrToValue); + + public static String sqlite3_value_text16(@NotNull sqlite3_value v){ + return sqlite3_value_text16(v.getNativePointer()); + } + + private static native int sqlite3_value_type(@NotNull long ptrToValue); + + public static int sqlite3_value_type(@NotNull sqlite3_value v){ + return sqlite3_value_type(v.getNativePointer()); + } + + /** + This is NOT part of the public API. It exists solely as a place + for this code's developers to collect internal metrics and such. + It has no stable interface. It may go way or change behavior at + any time. + */ + public static native void sqlite3_jni_internal_details(); + + ////////////////////////////////////////////////////////////////////// + // SQLITE_... constants follow... + + // version info + public static final int SQLITE_VERSION_NUMBER = sqlite3_libversion_number(); + public static final String SQLITE_VERSION = sqlite3_libversion(); + public static final String SQLITE_SOURCE_ID = sqlite3_sourceid(); + + // access + public static final int SQLITE_ACCESS_EXISTS = 0; + public static final int SQLITE_ACCESS_READWRITE = 1; + public static final int SQLITE_ACCESS_READ = 2; + + // authorizer + public static final int SQLITE_DENY = 1; + public static final int SQLITE_IGNORE = 2; + public static final int SQLITE_CREATE_INDEX = 1; + public static final int SQLITE_CREATE_TABLE = 2; + public static final int SQLITE_CREATE_TEMP_INDEX = 3; + public static final int SQLITE_CREATE_TEMP_TABLE = 4; + public static final int SQLITE_CREATE_TEMP_TRIGGER = 5; + public static final int SQLITE_CREATE_TEMP_VIEW = 6; + public static final int SQLITE_CREATE_TRIGGER = 7; + public static final int SQLITE_CREATE_VIEW = 8; + public static final int SQLITE_DELETE = 9; + public static final int SQLITE_DROP_INDEX = 10; + public static final int SQLITE_DROP_TABLE = 11; + public static final int SQLITE_DROP_TEMP_INDEX = 12; + public static final int SQLITE_DROP_TEMP_TABLE = 13; + public static final int SQLITE_DROP_TEMP_TRIGGER = 14; + public static final int SQLITE_DROP_TEMP_VIEW = 15; + public static final int SQLITE_DROP_TRIGGER = 16; + public static final int SQLITE_DROP_VIEW = 17; + public static final int SQLITE_INSERT = 18; + public static final int SQLITE_PRAGMA = 19; + public static final int SQLITE_READ = 20; + public static final int SQLITE_SELECT = 21; + public static final int SQLITE_TRANSACTION = 22; + public static final int SQLITE_UPDATE = 23; + public static final int SQLITE_ATTACH = 24; + public static final int SQLITE_DETACH = 25; + public static final int SQLITE_ALTER_TABLE = 26; + public static final int SQLITE_REINDEX = 27; + public static final int SQLITE_ANALYZE = 28; + public static final int SQLITE_CREATE_VTABLE = 29; + public static final int SQLITE_DROP_VTABLE = 30; + public static final int SQLITE_FUNCTION = 31; + public static final int SQLITE_SAVEPOINT = 32; + public static final int SQLITE_RECURSIVE = 33; + + // blob finalizers: these should, because they are treated as + // special pointer values in C, ideally have the same sizeof() as + // the platform's (void*), but we can't know that size from here. + public static final long SQLITE_STATIC = 0; + public static final long SQLITE_TRANSIENT = -1; + + // changeset + public static final int SQLITE_CHANGESETSTART_INVERT = 2; + public static final int SQLITE_CHANGESETAPPLY_NOSAVEPOINT = 1; + public static final int SQLITE_CHANGESETAPPLY_INVERT = 2; + public static final int SQLITE_CHANGESETAPPLY_IGNORENOOP = 4; + public static final int SQLITE_CHANGESET_DATA = 1; + public static final int SQLITE_CHANGESET_NOTFOUND = 2; + public static final int SQLITE_CHANGESET_CONFLICT = 3; + public static final int SQLITE_CHANGESET_CONSTRAINT = 4; + public static final int SQLITE_CHANGESET_FOREIGN_KEY = 5; + public static final int SQLITE_CHANGESET_OMIT = 0; + public static final int SQLITE_CHANGESET_REPLACE = 1; + public static final int SQLITE_CHANGESET_ABORT = 2; + + // config + public static final int SQLITE_CONFIG_SINGLETHREAD = 1; + public static final int SQLITE_CONFIG_MULTITHREAD = 2; + public static final int SQLITE_CONFIG_SERIALIZED = 3; + public static final int SQLITE_CONFIG_MALLOC = 4; + public static final int SQLITE_CONFIG_GETMALLOC = 5; + public static final int SQLITE_CONFIG_SCRATCH = 6; + public static final int SQLITE_CONFIG_PAGECACHE = 7; + public static final int SQLITE_CONFIG_HEAP = 8; + public static final int SQLITE_CONFIG_MEMSTATUS = 9; + public static final int SQLITE_CONFIG_MUTEX = 10; + public static final int SQLITE_CONFIG_GETMUTEX = 11; + public static final int SQLITE_CONFIG_LOOKASIDE = 13; + public static final int SQLITE_CONFIG_PCACHE = 14; + public static final int SQLITE_CONFIG_GETPCACHE = 15; + public static final int SQLITE_CONFIG_LOG = 16; + public static final int SQLITE_CONFIG_URI = 17; + public static final int SQLITE_CONFIG_PCACHE2 = 18; + public static final int SQLITE_CONFIG_GETPCACHE2 = 19; + public static final int SQLITE_CONFIG_COVERING_INDEX_SCAN = 20; + public static final int SQLITE_CONFIG_SQLLOG = 21; + public static final int SQLITE_CONFIG_MMAP_SIZE = 22; + public static final int SQLITE_CONFIG_WIN32_HEAPSIZE = 23; + public static final int SQLITE_CONFIG_PCACHE_HDRSZ = 24; + public static final int SQLITE_CONFIG_PMASZ = 25; + public static final int SQLITE_CONFIG_STMTJRNL_SPILL = 26; + public static final int SQLITE_CONFIG_SMALL_MALLOC = 27; + public static final int SQLITE_CONFIG_SORTERREF_SIZE = 28; + public static final int SQLITE_CONFIG_MEMDB_MAXSIZE = 29; + + // data types + public static final int SQLITE_INTEGER = 1; + public static final int SQLITE_FLOAT = 2; + public static final int SQLITE_TEXT = 3; + public static final int SQLITE_BLOB = 4; + public static final int SQLITE_NULL = 5; + + // db config + public static final int SQLITE_DBCONFIG_MAINDBNAME = 1000; + public static final int SQLITE_DBCONFIG_LOOKASIDE = 1001; + public static final int SQLITE_DBCONFIG_ENABLE_FKEY = 1002; + public static final int SQLITE_DBCONFIG_ENABLE_TRIGGER = 1003; + public static final int SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER = 1004; + public static final int SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION = 1005; + public static final int SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE = 1006; + public static final int SQLITE_DBCONFIG_ENABLE_QPSG = 1007; + public static final int SQLITE_DBCONFIG_TRIGGER_EQP = 1008; + public static final int SQLITE_DBCONFIG_RESET_DATABASE = 1009; + public static final int SQLITE_DBCONFIG_DEFENSIVE = 1010; + public static final int SQLITE_DBCONFIG_WRITABLE_SCHEMA = 1011; + public static final int SQLITE_DBCONFIG_LEGACY_ALTER_TABLE = 1012; + public static final int SQLITE_DBCONFIG_DQS_DML = 1013; + public static final int SQLITE_DBCONFIG_DQS_DDL = 1014; + public static final int SQLITE_DBCONFIG_ENABLE_VIEW = 1015; + public static final int SQLITE_DBCONFIG_LEGACY_FILE_FORMAT = 1016; + public static final int SQLITE_DBCONFIG_TRUSTED_SCHEMA = 1017; + public static final int SQLITE_DBCONFIG_STMT_SCANSTATUS = 1018; + public static final int SQLITE_DBCONFIG_REVERSE_SCANORDER = 1019; + public static final int SQLITE_DBCONFIG_MAX = 1019; + + // db status + public static final int SQLITE_DBSTATUS_LOOKASIDE_USED = 0; + public static final int SQLITE_DBSTATUS_CACHE_USED = 1; + public static final int SQLITE_DBSTATUS_SCHEMA_USED = 2; + public static final int SQLITE_DBSTATUS_STMT_USED = 3; + public static final int SQLITE_DBSTATUS_LOOKASIDE_HIT = 4; + public static final int SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE = 5; + public static final int SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL = 6; + public static final int SQLITE_DBSTATUS_CACHE_HIT = 7; + public static final int SQLITE_DBSTATUS_CACHE_MISS = 8; + public static final int SQLITE_DBSTATUS_CACHE_WRITE = 9; + public static final int SQLITE_DBSTATUS_DEFERRED_FKS = 10; + public static final int SQLITE_DBSTATUS_CACHE_USED_SHARED = 11; + public static final int SQLITE_DBSTATUS_CACHE_SPILL = 12; + public static final int SQLITE_DBSTATUS_MAX = 12; + + // encodings + public static final int SQLITE_UTF8 = 1; + public static final int SQLITE_UTF16LE = 2; + public static final int SQLITE_UTF16BE = 3; + public static final int SQLITE_UTF16 = 4; + public static final int SQLITE_UTF16_ALIGNED = 8; + + // fcntl + public static final int SQLITE_FCNTL_LOCKSTATE = 1; + public static final int SQLITE_FCNTL_GET_LOCKPROXYFILE = 2; + public static final int SQLITE_FCNTL_SET_LOCKPROXYFILE = 3; + public static final int SQLITE_FCNTL_LAST_ERRNO = 4; + public static final int SQLITE_FCNTL_SIZE_HINT = 5; + public static final int SQLITE_FCNTL_CHUNK_SIZE = 6; + public static final int SQLITE_FCNTL_FILE_POINTER = 7; + public static final int SQLITE_FCNTL_SYNC_OMITTED = 8; + public static final int SQLITE_FCNTL_WIN32_AV_RETRY = 9; + public static final int SQLITE_FCNTL_PERSIST_WAL = 10; + public static final int SQLITE_FCNTL_OVERWRITE = 11; + public static final int SQLITE_FCNTL_VFSNAME = 12; + public static final int SQLITE_FCNTL_POWERSAFE_OVERWRITE = 13; + public static final int SQLITE_FCNTL_PRAGMA = 14; + public static final int SQLITE_FCNTL_BUSYHANDLER = 15; + public static final int SQLITE_FCNTL_TEMPFILENAME = 16; + public static final int SQLITE_FCNTL_MMAP_SIZE = 18; + public static final int SQLITE_FCNTL_TRACE = 19; + public static final int SQLITE_FCNTL_HAS_MOVED = 20; + public static final int SQLITE_FCNTL_SYNC = 21; + public static final int SQLITE_FCNTL_COMMIT_PHASETWO = 22; + public static final int SQLITE_FCNTL_WIN32_SET_HANDLE = 23; + public static final int SQLITE_FCNTL_WAL_BLOCK = 24; + public static final int SQLITE_FCNTL_ZIPVFS = 25; + public static final int SQLITE_FCNTL_RBU = 26; + public static final int SQLITE_FCNTL_VFS_POINTER = 27; + public static final int SQLITE_FCNTL_JOURNAL_POINTER = 28; + public static final int SQLITE_FCNTL_WIN32_GET_HANDLE = 29; + public static final int SQLITE_FCNTL_PDB = 30; + public static final int SQLITE_FCNTL_BEGIN_ATOMIC_WRITE = 31; + public static final int SQLITE_FCNTL_COMMIT_ATOMIC_WRITE = 32; + public static final int SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE = 33; + public static final int SQLITE_FCNTL_LOCK_TIMEOUT = 34; + public static final int SQLITE_FCNTL_DATA_VERSION = 35; + public static final int SQLITE_FCNTL_SIZE_LIMIT = 36; + public static final int SQLITE_FCNTL_CKPT_DONE = 37; + public static final int SQLITE_FCNTL_RESERVE_BYTES = 38; + public static final int SQLITE_FCNTL_CKPT_START = 39; + public static final int SQLITE_FCNTL_EXTERNAL_READER = 40; + public static final int SQLITE_FCNTL_CKSM_FILE = 41; + public static final int SQLITE_FCNTL_RESET_CACHE = 42; + + // flock + public static final int SQLITE_LOCK_NONE = 0; + public static final int SQLITE_LOCK_SHARED = 1; + public static final int SQLITE_LOCK_RESERVED = 2; + public static final int SQLITE_LOCK_PENDING = 3; + public static final int SQLITE_LOCK_EXCLUSIVE = 4; + + // iocap + public static final int SQLITE_IOCAP_ATOMIC = 1; + public static final int SQLITE_IOCAP_ATOMIC512 = 2; + public static final int SQLITE_IOCAP_ATOMIC1K = 4; + public static final int SQLITE_IOCAP_ATOMIC2K = 8; + public static final int SQLITE_IOCAP_ATOMIC4K = 16; + public static final int SQLITE_IOCAP_ATOMIC8K = 32; + public static final int SQLITE_IOCAP_ATOMIC16K = 64; + public static final int SQLITE_IOCAP_ATOMIC32K = 128; + public static final int SQLITE_IOCAP_ATOMIC64K = 256; + public static final int SQLITE_IOCAP_SAFE_APPEND = 512; + public static final int SQLITE_IOCAP_SEQUENTIAL = 1024; + public static final int SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN = 2048; + public static final int SQLITE_IOCAP_POWERSAFE_OVERWRITE = 4096; + public static final int SQLITE_IOCAP_IMMUTABLE = 8192; + public static final int SQLITE_IOCAP_BATCH_ATOMIC = 16384; + + // limits + public static final int SQLITE_LIMIT_LENGTH = 0; + public static final int SQLITE_LIMIT_SQL_LENGTH = 1; + public static final int SQLITE_LIMIT_COLUMN = 2; + public static final int SQLITE_LIMIT_EXPR_DEPTH = 3; + public static final int SQLITE_LIMIT_COMPOUND_SELECT = 4; + public static final int SQLITE_LIMIT_VDBE_OP = 5; + public static final int SQLITE_LIMIT_FUNCTION_ARG = 6; + public static final int SQLITE_LIMIT_ATTACHED = 7; + public static final int SQLITE_LIMIT_LIKE_PATTERN_LENGTH = 8; + public static final int SQLITE_LIMIT_VARIABLE_NUMBER = 9; + public static final int SQLITE_LIMIT_TRIGGER_DEPTH = 10; + public static final int SQLITE_LIMIT_WORKER_THREADS = 11; + + // open flags + + public static final int SQLITE_OPEN_READONLY = 0x00000001 /* Ok for sqlite3_open_v2() */; + public static final int SQLITE_OPEN_READWRITE = 0x00000002 /* Ok for sqlite3_open_v2() */; + public static final int SQLITE_OPEN_CREATE = 0x00000004 /* Ok for sqlite3_open_v2() */; + //public static final int SQLITE_OPEN_DELETEONCLOSE = 0x00000008 /* VFS only */; + //public static final int SQLITE_OPEN_EXCLUSIVE = 0x00000010 /* VFS only */; + //public static final int SQLITE_OPEN_AUTOPROXY = 0x00000020 /* VFS only */; + public static final int SQLITE_OPEN_URI = 0x00000040 /* Ok for sqlite3_open_v2() */; + public static final int SQLITE_OPEN_MEMORY = 0x00000080 /* Ok for sqlite3_open_v2() */; + //public static final int SQLITE_OPEN_MAIN_DB = 0x00000100 /* VFS only */; + //public static final int SQLITE_OPEN_TEMP_DB = 0x00000200 /* VFS only */; + //public static final int SQLITE_OPEN_TRANSIENT_DB = 0x00000400 /* VFS only */; + //public static final int SQLITE_OPEN_MAIN_JOURNAL = 0x00000800 /* VFS only */; + //public static final int SQLITE_OPEN_TEMP_JOURNAL = 0x00001000 /* VFS only */; + //public static final int SQLITE_OPEN_SUBJOURNAL = 0x00002000 /* VFS only */; + //public static final int SQLITE_OPEN_SUPER_JOURNAL = 0x00004000 /* VFS only */; + public static final int SQLITE_OPEN_NOMUTEX = 0x00008000 /* Ok for sqlite3_open_v2() */; + public static final int SQLITE_OPEN_FULLMUTEX = 0x00010000 /* Ok for sqlite3_open_v2() */; + public static final int SQLITE_OPEN_SHAREDCACHE = 0x00020000 /* Ok for sqlite3_open_v2() */; + public static final int SQLITE_OPEN_PRIVATECACHE = 0x00040000 /* Ok for sqlite3_open_v2() */; + //public static final int SQLITE_OPEN_WAL = 0x00080000 /* VFS only */; + public static final int SQLITE_OPEN_NOFOLLOW = 0x01000000 /* Ok for sqlite3_open_v2() */; + public static final int SQLITE_OPEN_EXRESCODE = 0x02000000 /* Extended result codes */; + + // prepare flags + public static final int SQLITE_PREPARE_PERSISTENT = 1; + public static final int SQLITE_PREPARE_NO_VTAB = 4; + + // result codes + public static final int SQLITE_OK = 0; + public static final int SQLITE_ERROR = 1; + public static final int SQLITE_INTERNAL = 2; + public static final int SQLITE_PERM = 3; + public static final int SQLITE_ABORT = 4; + public static final int SQLITE_BUSY = 5; + public static final int SQLITE_LOCKED = 6; + public static final int SQLITE_NOMEM = 7; + public static final int SQLITE_READONLY = 8; + public static final int SQLITE_INTERRUPT = 9; + public static final int SQLITE_IOERR = 10; + public static final int SQLITE_CORRUPT = 11; + public static final int SQLITE_NOTFOUND = 12; + public static final int SQLITE_FULL = 13; + public static final int SQLITE_CANTOPEN = 14; + public static final int SQLITE_PROTOCOL = 15; + public static final int SQLITE_EMPTY = 16; + public static final int SQLITE_SCHEMA = 17; + public static final int SQLITE_TOOBIG = 18; + public static final int SQLITE_CONSTRAINT = 19; + public static final int SQLITE_MISMATCH = 20; + public static final int SQLITE_MISUSE = 21; + public static final int SQLITE_NOLFS = 22; + public static final int SQLITE_AUTH = 23; + public static final int SQLITE_FORMAT = 24; + public static final int SQLITE_RANGE = 25; + public static final int SQLITE_NOTADB = 26; + public static final int SQLITE_NOTICE = 27; + public static final int SQLITE_WARNING = 28; + public static final int SQLITE_ROW = 100; + public static final int SQLITE_DONE = 101; + public static final int SQLITE_ERROR_MISSING_COLLSEQ = 257; + public static final int SQLITE_ERROR_RETRY = 513; + public static final int SQLITE_ERROR_SNAPSHOT = 769; + public static final int SQLITE_IOERR_READ = 266; + public static final int SQLITE_IOERR_SHORT_READ = 522; + public static final int SQLITE_IOERR_WRITE = 778; + public static final int SQLITE_IOERR_FSYNC = 1034; + public static final int SQLITE_IOERR_DIR_FSYNC = 1290; + public static final int SQLITE_IOERR_TRUNCATE = 1546; + public static final int SQLITE_IOERR_FSTAT = 1802; + public static final int SQLITE_IOERR_UNLOCK = 2058; + public static final int SQLITE_IOERR_RDLOCK = 2314; + public static final int SQLITE_IOERR_DELETE = 2570; + public static final int SQLITE_IOERR_BLOCKED = 2826; + public static final int SQLITE_IOERR_NOMEM = 3082; + public static final int SQLITE_IOERR_ACCESS = 3338; + public static final int SQLITE_IOERR_CHECKRESERVEDLOCK = 3594; + public static final int SQLITE_IOERR_LOCK = 3850; + public static final int SQLITE_IOERR_CLOSE = 4106; + public static final int SQLITE_IOERR_DIR_CLOSE = 4362; + public static final int SQLITE_IOERR_SHMOPEN = 4618; + public static final int SQLITE_IOERR_SHMSIZE = 4874; + public static final int SQLITE_IOERR_SHMLOCK = 5130; + public static final int SQLITE_IOERR_SHMMAP = 5386; + public static final int SQLITE_IOERR_SEEK = 5642; + public static final int SQLITE_IOERR_DELETE_NOENT = 5898; + public static final int SQLITE_IOERR_MMAP = 6154; + public static final int SQLITE_IOERR_GETTEMPPATH = 6410; + public static final int SQLITE_IOERR_CONVPATH = 6666; + public static final int SQLITE_IOERR_VNODE = 6922; + public static final int SQLITE_IOERR_AUTH = 7178; + public static final int SQLITE_IOERR_BEGIN_ATOMIC = 7434; + public static final int SQLITE_IOERR_COMMIT_ATOMIC = 7690; + public static final int SQLITE_IOERR_ROLLBACK_ATOMIC = 7946; + public static final int SQLITE_IOERR_DATA = 8202; + public static final int SQLITE_IOERR_CORRUPTFS = 8458; + public static final int SQLITE_LOCKED_SHAREDCACHE = 262; + public static final int SQLITE_LOCKED_VTAB = 518; + public static final int SQLITE_BUSY_RECOVERY = 261; + public static final int SQLITE_BUSY_SNAPSHOT = 517; + public static final int SQLITE_BUSY_TIMEOUT = 773; + public static final int SQLITE_CANTOPEN_NOTEMPDIR = 270; + public static final int SQLITE_CANTOPEN_ISDIR = 526; + public static final int SQLITE_CANTOPEN_FULLPATH = 782; + public static final int SQLITE_CANTOPEN_CONVPATH = 1038; + public static final int SQLITE_CANTOPEN_SYMLINK = 1550; + public static final int SQLITE_CORRUPT_VTAB = 267; + public static final int SQLITE_CORRUPT_SEQUENCE = 523; + public static final int SQLITE_CORRUPT_INDEX = 779; + public static final int SQLITE_READONLY_RECOVERY = 264; + public static final int SQLITE_READONLY_CANTLOCK = 520; + public static final int SQLITE_READONLY_ROLLBACK = 776; + public static final int SQLITE_READONLY_DBMOVED = 1032; + public static final int SQLITE_READONLY_CANTINIT = 1288; + public static final int SQLITE_READONLY_DIRECTORY = 1544; + public static final int SQLITE_ABORT_ROLLBACK = 516; + public static final int SQLITE_CONSTRAINT_CHECK = 275; + public static final int SQLITE_CONSTRAINT_COMMITHOOK = 531; + public static final int SQLITE_CONSTRAINT_FOREIGNKEY = 787; + public static final int SQLITE_CONSTRAINT_FUNCTION = 1043; + public static final int SQLITE_CONSTRAINT_NOTNULL = 1299; + public static final int SQLITE_CONSTRAINT_PRIMARYKEY = 1555; + public static final int SQLITE_CONSTRAINT_TRIGGER = 1811; + public static final int SQLITE_CONSTRAINT_UNIQUE = 2067; + public static final int SQLITE_CONSTRAINT_VTAB = 2323; + public static final int SQLITE_CONSTRAINT_ROWID = 2579; + public static final int SQLITE_CONSTRAINT_PINNED = 2835; + public static final int SQLITE_CONSTRAINT_DATATYPE = 3091; + public static final int SQLITE_NOTICE_RECOVER_WAL = 283; + public static final int SQLITE_NOTICE_RECOVER_ROLLBACK = 539; + public static final int SQLITE_WARNING_AUTOINDEX = 284; + public static final int SQLITE_AUTH_USER = 279; + public static final int SQLITE_OK_LOAD_PERMANENTLY = 256; + + // serialize + public static final int SQLITE_SERIALIZE_NOCOPY = 1; + public static final int SQLITE_DESERIALIZE_FREEONCLOSE = 1; + public static final int SQLITE_DESERIALIZE_READONLY = 4; + public static final int SQLITE_DESERIALIZE_RESIZEABLE = 2; + + // session + public static final int SQLITE_SESSION_CONFIG_STRMSIZE = 1; + public static final int SQLITE_SESSION_OBJCONFIG_SIZE = 1; + + // sqlite3 status + public static final int SQLITE_STATUS_MEMORY_USED = 0; + public static final int SQLITE_STATUS_PAGECACHE_USED = 1; + public static final int SQLITE_STATUS_PAGECACHE_OVERFLOW = 2; + public static final int SQLITE_STATUS_MALLOC_SIZE = 5; + public static final int SQLITE_STATUS_PARSER_STACK = 6; + public static final int SQLITE_STATUS_PAGECACHE_SIZE = 7; + public static final int SQLITE_STATUS_MALLOC_COUNT = 9; + + // stmt status + public static final int SQLITE_STMTSTATUS_FULLSCAN_STEP = 1; + public static final int SQLITE_STMTSTATUS_SORT = 2; + public static final int SQLITE_STMTSTATUS_AUTOINDEX = 3; + public static final int SQLITE_STMTSTATUS_VM_STEP = 4; + public static final int SQLITE_STMTSTATUS_REPREPARE = 5; + public static final int SQLITE_STMTSTATUS_RUN = 6; + public static final int SQLITE_STMTSTATUS_FILTER_MISS = 7; + public static final int SQLITE_STMTSTATUS_FILTER_HIT = 8; + public static final int SQLITE_STMTSTATUS_MEMUSED = 99; + + // sync flags + public static final int SQLITE_SYNC_NORMAL = 2; + public static final int SQLITE_SYNC_FULL = 3; + public static final int SQLITE_SYNC_DATAONLY = 16; + + // tracing flags + public static final int SQLITE_TRACE_STMT = 1; + public static final int SQLITE_TRACE_PROFILE = 2; + public static final int SQLITE_TRACE_ROW = 4; + public static final int SQLITE_TRACE_CLOSE = 8; + + // transaction state + public static final int SQLITE_TXN_NONE = 0; + public static final int SQLITE_TXN_READ = 1; + public static final int SQLITE_TXN_WRITE = 2; + + // udf flags + public static final int SQLITE_DETERMINISTIC = 0x000000800; + public static final int SQLITE_DIRECTONLY = 0x000080000; + public static final int SQLITE_SUBTYPE = 0x000100000; + public static final int SQLITE_INNOCUOUS = 0x000200000; + public static final int SQLITE_RESULT_SUBTYPE = 0x001000000; + + // virtual tables + public static final int SQLITE_INDEX_SCAN_UNIQUE = 1; + public static final int SQLITE_INDEX_CONSTRAINT_EQ = 2; + public static final int SQLITE_INDEX_CONSTRAINT_GT = 4; + public static final int SQLITE_INDEX_CONSTRAINT_LE = 8; + public static final int SQLITE_INDEX_CONSTRAINT_LT = 16; + public static final int SQLITE_INDEX_CONSTRAINT_GE = 32; + public static final int SQLITE_INDEX_CONSTRAINT_MATCH = 64; + public static final int SQLITE_INDEX_CONSTRAINT_LIKE = 65; + public static final int SQLITE_INDEX_CONSTRAINT_GLOB = 66; + public static final int SQLITE_INDEX_CONSTRAINT_REGEXP = 67; + public static final int SQLITE_INDEX_CONSTRAINT_NE = 68; + public static final int SQLITE_INDEX_CONSTRAINT_ISNOT = 69; + public static final int SQLITE_INDEX_CONSTRAINT_ISNOTNULL = 70; + public static final int SQLITE_INDEX_CONSTRAINT_ISNULL = 71; + public static final int SQLITE_INDEX_CONSTRAINT_IS = 72; + public static final int SQLITE_INDEX_CONSTRAINT_LIMIT = 73; + public static final int SQLITE_INDEX_CONSTRAINT_OFFSET = 74; + public static final int SQLITE_INDEX_CONSTRAINT_FUNCTION = 150; + public static final int SQLITE_VTAB_CONSTRAINT_SUPPORT = 1; + public static final int SQLITE_VTAB_INNOCUOUS = 2; + public static final int SQLITE_VTAB_DIRECTONLY = 3; + public static final int SQLITE_VTAB_USES_ALL_SCHEMAS = 4; + public static final int SQLITE_ROLLBACK = 1; + public static final int SQLITE_FAIL = 3; + public static final int SQLITE_REPLACE = 5; + static { + init(); + } + /* Must come after static init(). */ + private static final boolean JNI_SUPPORTS_NIO = sqlite3_jni_supports_nio(); +} diff --git a/ext/jni/src/org/sqlite/jni/capi/CallbackProxy.java b/ext/jni/src/org/sqlite/jni/capi/CallbackProxy.java new file mode 100644 index 0000000000..04000a3f31 --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/capi/CallbackProxy.java @@ -0,0 +1,45 @@ +/* +** 2023-08-25 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file is part of the JNI bindings for the sqlite3 C API. +*/ +package org.sqlite.jni.capi; +/** + This marker interface exists solely for use as a documentation and + class-grouping tool. It should be applied to interfaces or + classes which have a call() method implementing some specific + callback interface on behalf of the C library. + +

    Unless very explicitly documented otherwise, callbacks must + never throw. Any which do throw but should not might trigger debug + output regarding the error, but the exception will not be + propagated. For callback interfaces which support returning error + info to the core, the JNI binding will convert any exceptions to + C-level error information. For callback interfaces which do not + support returning error information, all exceptions will + necessarily be suppressed in order to retain the C-style no-throw + semantics and avoid invoking undefined behavior in the C layer. + +

    Callbacks of this style follow a common naming convention: + +

    1) They use the UpperCamelCase form of the C function they're + proxying for, minus the {@code sqlite3_} prefix, plus a {@code + Callback} suffix. e.g. {@code sqlite3_busy_handler()}'s callback is + named {@code BusyHandlerCallback}. Exceptions are made where that + would potentially be ambiguous, e.g. {@link ConfigSqlLogCallback} + instead of {@code ConfigCallback} because the {@code + sqlite3_config()} interface may need to support more callback types + in the future. + +

    2) They all have a {@code call()} method but its signature is + callback-specific. +*/ +public interface CallbackProxy {} diff --git a/ext/jni/src/org/sqlite/jni/capi/CollationCallback.java b/ext/jni/src/org/sqlite/jni/capi/CollationCallback.java new file mode 100644 index 0000000000..ed8bd09475 --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/capi/CollationCallback.java @@ -0,0 +1,35 @@ +/* +** 2023-08-25 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file is part of the JNI bindings for the sqlite3 C API. +*/ +package org.sqlite.jni.capi; +import org.sqlite.jni.annotation.NotNull; + +/** + Callback for use with {@link CApi#sqlite3_create_collation}. + + @see AbstractCollationCallback +*/ +public interface CollationCallback + extends CallbackProxy, XDestroyCallback { + /** + Must compare the given byte arrays and return the result using + {@code memcmp()} semantics. + */ + int call(@NotNull byte[] lhs, @NotNull byte[] rhs); + + /** + Called by SQLite when the collation is destroyed. If a collation + requires custom cleanup, override this method. + */ + void xDestroy(); +} diff --git a/ext/jni/src/org/sqlite/jni/capi/CollationNeededCallback.java b/ext/jni/src/org/sqlite/jni/capi/CollationNeededCallback.java new file mode 100644 index 0000000000..ffd7fa94ab --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/capi/CollationNeededCallback.java @@ -0,0 +1,29 @@ +/* +** 2023-08-25 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file is part of the JNI bindings for the sqlite3 C API. +*/ +package org.sqlite.jni.capi; + +/** + Callback for use with {@link CApi#sqlite3_collation_needed}. +*/ +public interface CollationNeededCallback extends CallbackProxy { + /** + Has the same semantics as the C-level sqlite3_create_collation() + callback. + +

    Because the C API has no mechanism for reporting errors + from this callbacks, any exceptions thrown by this callback + are suppressed. + */ + void call(sqlite3 db, int eTextRep, String collationName); +} diff --git a/ext/jni/src/org/sqlite/jni/capi/CommitHookCallback.java b/ext/jni/src/org/sqlite/jni/capi/CommitHookCallback.java new file mode 100644 index 0000000000..e1e55c78d2 --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/capi/CommitHookCallback.java @@ -0,0 +1,26 @@ +/* +** 2023-08-25 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file is part of the JNI bindings for the sqlite3 C API. +*/ +package org.sqlite.jni.capi; + +/** + Callback for use with {@link CApi#sqlite3_commit_hook}. +*/ +public interface CommitHookCallback extends CallbackProxy { + /** + Works as documented for the C-level sqlite3_commit_hook() + callback. If it throws, the exception is translated into + a db-level error. + */ + int call(); +} diff --git a/ext/jni/src/org/sqlite/jni/capi/ConfigLogCallback.java b/ext/jni/src/org/sqlite/jni/capi/ConfigLogCallback.java new file mode 100644 index 0000000000..6513b0730d --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/capi/ConfigLogCallback.java @@ -0,0 +1,25 @@ +/* +** 2023-08-23 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file is part of the JNI bindings for the sqlite3 C API. +*/ +package org.sqlite.jni.capi; + +/** + A callback for use with sqlite3_config(). +*/ +public interface ConfigLogCallback { + /** + Must function as described for a C-level callback for + {@link CApi#sqlite3_config(ConfigLogCallback)}, with the slight signature change. + */ + void call(int errCode, String msg); +} diff --git a/ext/jni/src/org/sqlite/jni/capi/ConfigSqlLogCallback.java b/ext/jni/src/org/sqlite/jni/capi/ConfigSqlLogCallback.java new file mode 100644 index 0000000000..a5530b49a4 --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/capi/ConfigSqlLogCallback.java @@ -0,0 +1,25 @@ +/* +** 2023-08-23 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file is part of the JNI bindings for the sqlite3 C API. +*/ +package org.sqlite.jni.capi; + +/** + A callback for use with sqlite3_config(). +*/ +public interface ConfigSqlLogCallback { + /** + Must function as described for a C-level callback for + {@link CApi#sqlite3_config(ConfigSqlLogCallback)}, with the slight signature change. + */ + void call(sqlite3 db, String msg, int msgType ); +} diff --git a/ext/jni/src/org/sqlite/jni/capi/NativePointerHolder.java b/ext/jni/src/org/sqlite/jni/capi/NativePointerHolder.java new file mode 100644 index 0000000000..e82909e424 --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/capi/NativePointerHolder.java @@ -0,0 +1,46 @@ +/* +** 2023-07-21 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file is part of the JNI bindings for the sqlite3 C API. +*/ +package org.sqlite.jni.capi; + +/** + A helper for passing pointers between JNI C code and Java, in + particular for output pointers of high-level object types in the + sqlite3 C API, e.g. (sqlite3**) and (sqlite3_stmt**). This is + intended to be subclassed and the ContextType is intended to be the + class which is doing the subclassing. The intent of the ContextType + is strictly to provide some level of type safety by avoiding that + NativePointerHolder is not inadvertently passed to an incompatible + function signature. + + These objects do not own the pointer they refer to. They are + intended simply to communicate that pointer between C and Java. +*/ +public class NativePointerHolder { + //! Only set from JNI, where access permissions don't matter. + private volatile long nativePointer = 0; + /** + For use ONLY by package-level APIs which act as proxies for + close/finalize operations. Such ops must call this to zero out + the pointer so that this object is not carrying a stale + pointer. This function returns the prior value of the pointer and + sets it to 0. + */ + final long clearNativePointer() { + final long rv = nativePointer; + nativePointer= 0; + return rv; + } + + public final long getNativePointer(){ return nativePointer; } +} diff --git a/ext/jni/src/org/sqlite/jni/capi/OutputPointer.java b/ext/jni/src/org/sqlite/jni/capi/OutputPointer.java new file mode 100644 index 0000000000..f50d0c57cb --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/capi/OutputPointer.java @@ -0,0 +1,253 @@ +/* +** 2023-07-21 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file is part of the JNI bindings for the sqlite3 C API. +*/ +package org.sqlite.jni.capi; + +/** + Helper classes for handling JNI output pointers. + +

    We do not use a generic OutputPointer because working with those + from the native JNI code is unduly quirky due to a lack of + autoboxing at that level. + +

    The usage is similar for all of these types: + +

    {@code
    +   OutputPointer.sqlite3 out = new OutputPointer.sqlite3();
    +   assert( null==out.get() );
    +   int rc = sqlite3_open(":memory:", out);
    +   if( 0!=rc ) ... error;
    +   assert( null!=out.get() );
    +   sqlite3 db = out.take();
    +   assert( null==out.get() );
    +   }
    + +

    With the minor exception that the primitive types permit direct + access to the object's value via the `value` property, whereas the + JNI-level opaque types do not permit client-level code to set that + property. + +

    Warning: do not share instances of these classes across + threads. Doing so may lead to corrupting sqlite3-internal state. +*/ +public final class OutputPointer { + + /** + Output pointer for use with routines, such as sqlite3_open(), + which return a database handle via an output pointer. These + pointers can only be set by the JNI layer, not by client-level + code. + */ + public static final class sqlite3 { + private org.sqlite.jni.capi.sqlite3 value; + /** Initializes with a null value. */ + public sqlite3(){value = null;} + /** Sets the current value to null. */ + public void clear(){value = null;} + /** Returns the current value. */ + public org.sqlite.jni.capi.sqlite3 get(){return value;} + /** Equivalent to calling get() then clear(). */ + public org.sqlite.jni.capi.sqlite3 take(){ + final org.sqlite.jni.capi.sqlite3 v = value; + value = null; + return v; + } + } + + /** + Output pointer for sqlite3_blob_open(). These + pointers can only be set by the JNI layer, not by client-level + code. + */ + public static final class sqlite3_blob { + private org.sqlite.jni.capi.sqlite3_blob value; + /** Initializes with a null value. */ + public sqlite3_blob(){value = null;} + /** Sets the current value to null. */ + public void clear(){value = null;} + /** Returns the current value. */ + public org.sqlite.jni.capi.sqlite3_blob get(){return value;} + /** Equivalent to calling get() then clear(). */ + public org.sqlite.jni.capi.sqlite3_blob take(){ + final org.sqlite.jni.capi.sqlite3_blob v = value; + value = null; + return v; + } + } + + /** + Output pointer for use with routines, such as sqlite3_prepare(), + which return a statement handle via an output pointer. These + pointers can only be set by the JNI layer, not by client-level + code. + */ + public static final class sqlite3_stmt { + private org.sqlite.jni.capi.sqlite3_stmt value; + /** Initializes with a null value. */ + public sqlite3_stmt(){value = null;} + /** Sets the current value to null. */ + public void clear(){value = null;} + /** Returns the current value. */ + public org.sqlite.jni.capi.sqlite3_stmt get(){return value;} + /** Equivalent to calling get() then clear(). */ + public org.sqlite.jni.capi.sqlite3_stmt take(){ + final org.sqlite.jni.capi.sqlite3_stmt v = value; + value = null; + return v; + } + } + + /** + Output pointer for use with routines, such as sqlite3_prepupdate_new(), + which return a sqlite3_value handle via an output pointer. These + pointers can only be set by the JNI layer, not by client-level + code. + */ + public static final class sqlite3_value { + private org.sqlite.jni.capi.sqlite3_value value; + /** Initializes with a null value. */ + public sqlite3_value(){value = null;} + /** Sets the current value to null. */ + public void clear(){value = null;} + /** Returns the current value. */ + public org.sqlite.jni.capi.sqlite3_value get(){return value;} + /** Equivalent to calling get() then clear(). */ + public org.sqlite.jni.capi.sqlite3_value take(){ + final org.sqlite.jni.capi.sqlite3_value v = value; + value = null; + return v; + } + } + + /** + Output pointer for use with native routines which return booleans + via integer output pointers. + */ + public static final class Bool { + /** + This is public for ease of use. Accessors are provided for + consistency with the higher-level types. + */ + public boolean value; + /** Initializes with the value 0. */ + public Bool(){this(false);} + /** Initializes with the value v. */ + public Bool(boolean v){value = v;} + /** Returns the current value. */ + public boolean get(){return value;} + /** Sets the current value to v. */ + public void set(boolean v){value = v;} + } + + /** + Output pointer for use with native routines which return integers via + output pointers. + */ + public static final class Int32 { + /** + This is public for ease of use. Accessors are provided for + consistency with the higher-level types. + */ + public int value; + /** Initializes with the value 0. */ + public Int32(){this(0);} + /** Initializes with the value v. */ + public Int32(int v){value = v;} + /** Returns the current value. */ + public int get(){return value;} + /** Sets the current value to v. */ + public void set(int v){value = v;} + } + + /** + Output pointer for use with native routines which return 64-bit integers + via output pointers. + */ + public static final class Int64 { + /** + This is public for ease of use. Accessors are provided for + consistency with the higher-level types. + */ + public long value; + /** Initializes with the value 0. */ + public Int64(){this(0);} + /** Initializes with the value v. */ + public Int64(long v){value = v;} + /** Returns the current value. */ + public long get(){return value;} + /** Sets the current value. */ + public void set(long v){value = v;} + } + + /** + Output pointer for use with native routines which return strings via + output pointers. + */ + public static final class String { + /** + This is public for ease of use. Accessors are provided for + consistency with the higher-level types. + */ + public java.lang.String value; + /** Initializes with a null value. */ + public String(){this(null);} + /** Initializes with the value v. */ + public String(java.lang.String v){value = v;} + /** Returns the current value. */ + public java.lang.String get(){return value;} + /** Sets the current value. */ + public void set(java.lang.String v){value = v;} + } + + /** + Output pointer for use with native routines which return byte + arrays via output pointers. + */ + public static final class ByteArray { + /** + This is public for ease of use. Accessors are provided for + consistency with the higher-level types. + */ + public byte[] value; + /** Initializes with the value null. */ + public ByteArray(){this(null);} + /** Initializes with the value v. */ + public ByteArray(byte[] v){value = v;} + /** Returns the current value. */ + public byte[] get(){return value;} + /** Sets the current value. */ + public void set(byte[] v){value = v;} + } + + /** + Output pointer for use with native routines which return + blobs via java.nio.ByteBuffer. + + See {@link org.sqlite.jni.capi.CApi#sqlite3_jni_supports_nio} + */ + public static final class ByteBuffer { + /** + This is public for ease of use. Accessors are provided for + consistency with the higher-level types. + */ + public java.nio.ByteBuffer value; + /** Initializes with the value null. */ + public ByteBuffer(){this(null);} + /** Initializes with the value v. */ + public ByteBuffer(java.nio.ByteBuffer v){value = v;} + /** Returns the current value. */ + public java.nio.ByteBuffer get(){return value;} + /** Sets the current value. */ + public void set(java.nio.ByteBuffer v){value = v;} + } +} diff --git a/ext/jni/src/org/sqlite/jni/capi/PrepareMultiCallback.java b/ext/jni/src/org/sqlite/jni/capi/PrepareMultiCallback.java new file mode 100644 index 0000000000..35bb069c49 --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/capi/PrepareMultiCallback.java @@ -0,0 +1,81 @@ +/* +** 2023-09-13 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file is part of the JNI bindings for the sqlite3 C API. +*/ +package org.sqlite.jni.capi; + +/** + Callback for use with {@link CApi#sqlite3_prepare_multi}. +*/ +public interface PrepareMultiCallback extends CallbackProxy { + + /** + Gets passed a sqlite3_stmt which it may handle in arbitrary ways, + transferring ownership of it to this function. + + sqlite3_prepare_multi() will _not_ finalize st - it is up + to the call() implementation how st is handled. + + Must return 0 on success or an SQLITE_... code on error. If it + throws, sqlite3_prepare_multi() will transform the exception into + a db-level error in order to retain the C-style error semantics + of the API. + + See the {@link Finalize} class for a wrapper which finalizes the + statement after calling a proxy PrepareMultiCallback. + */ + int call(sqlite3_stmt st); + + /** + A PrepareMultiCallback impl which wraps a separate impl and finalizes + any sqlite3_stmt passed to its callback. + */ + public static final class Finalize implements PrepareMultiCallback { + private final PrepareMultiCallback p; + /** + p is the proxy to call() when this.call() is called. + */ + public Finalize( PrepareMultiCallback p ){ + this.p = p; + } + /** + Calls the call() method of the proxied callback and either returns its + result or propagates an exception. Either way, it passes its argument to + sqlite3_finalize() before returning. + */ + @Override public int call(sqlite3_stmt st){ + try { + return this.p.call(st); + }finally{ + CApi.sqlite3_finalize(st); + } + } + } + + /** + A PrepareMultiCallback impl which steps entirely through a result set, + ignoring all non-error results. + */ + final class StepAll implements PrepareMultiCallback { + public StepAll(){} + /** + Calls sqlite3_step() on st until it returns something other than + SQLITE_ROW. If the final result is SQLITE_DONE then 0 is returned, + else the result of the final step is returned. + */ + @Override public int call(sqlite3_stmt st){ + int rc = CApi.SQLITE_DONE; + while( CApi.SQLITE_ROW == (rc = CApi.sqlite3_step(st)) ){} + return CApi.SQLITE_DONE==rc ? 0 : rc; + } + } +} diff --git a/ext/jni/src/org/sqlite/jni/capi/PreupdateHookCallback.java b/ext/jni/src/org/sqlite/jni/capi/PreupdateHookCallback.java new file mode 100644 index 0000000000..38f7c5613e --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/capi/PreupdateHookCallback.java @@ -0,0 +1,27 @@ +/* +** 2023-08-25 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file is part of the JNI bindings for the sqlite3 C API. +*/ +package org.sqlite.jni.capi; + +/** + Callback for use with {@link CApi#sqlite3_preupdate_hook}. +*/ +public interface PreupdateHookCallback extends CallbackProxy { + /** + Must function as described for the C-level sqlite3_preupdate_hook() + callback. If it throws, the exception is translated to a + db-level error and the exception is suppressed. + */ + void call(sqlite3 db, int op, String dbName, String dbTable, + long iKey1, long iKey2 ); +} diff --git a/ext/jni/src/org/sqlite/jni/capi/ProgressHandlerCallback.java b/ext/jni/src/org/sqlite/jni/capi/ProgressHandlerCallback.java new file mode 100644 index 0000000000..464baa2e3d --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/capi/ProgressHandlerCallback.java @@ -0,0 +1,27 @@ +/* +** 2023-08-25 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file is part of the JNI bindings for the sqlite3 C API. +*/ +package org.sqlite.jni.capi; + +/** + Callback for use with {@link CApi#sqlite3_progress_handler}. +*/ +public interface ProgressHandlerCallback extends CallbackProxy { + /** + Works as documented for the C-level sqlite3_progress_handler() callback. + +

    If it throws, the exception message is passed on to the db and + the exception is suppressed. + */ + int call(); +} diff --git a/ext/jni/src/org/sqlite/jni/capi/ResultCode.java b/ext/jni/src/org/sqlite/jni/capi/ResultCode.java new file mode 100644 index 0000000000..5a8b2e6a18 --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/capi/ResultCode.java @@ -0,0 +1,155 @@ +/* +** 2023-07-21 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file is part of the JNI bindings for the sqlite3 C API. +*/ +package org.sqlite.jni.capi; + +/** + This enum contains all of the core and "extended" result codes used + by the sqlite3 library. It is provided not for use with the C-style + API (with which it won't work) but for higher-level code which may + find it useful to map SQLite result codes to human-readable names. +*/ +public enum ResultCode { + SQLITE_OK(CApi.SQLITE_OK), + SQLITE_ERROR(CApi.SQLITE_ERROR), + SQLITE_INTERNAL(CApi.SQLITE_INTERNAL), + SQLITE_PERM(CApi.SQLITE_PERM), + SQLITE_ABORT(CApi.SQLITE_ABORT), + SQLITE_BUSY(CApi.SQLITE_BUSY), + SQLITE_LOCKED(CApi.SQLITE_LOCKED), + SQLITE_NOMEM(CApi.SQLITE_NOMEM), + SQLITE_READONLY(CApi.SQLITE_READONLY), + SQLITE_INTERRUPT(CApi.SQLITE_INTERRUPT), + SQLITE_IOERR(CApi.SQLITE_IOERR), + SQLITE_CORRUPT(CApi.SQLITE_CORRUPT), + SQLITE_NOTFOUND(CApi.SQLITE_NOTFOUND), + SQLITE_FULL(CApi.SQLITE_FULL), + SQLITE_CANTOPEN(CApi.SQLITE_CANTOPEN), + SQLITE_PROTOCOL(CApi.SQLITE_PROTOCOL), + SQLITE_EMPTY(CApi.SQLITE_EMPTY), + SQLITE_SCHEMA(CApi.SQLITE_SCHEMA), + SQLITE_TOOBIG(CApi.SQLITE_TOOBIG), + SQLITE_CONSTRAINT(CApi.SQLITE_CONSTRAINT), + SQLITE_MISMATCH(CApi.SQLITE_MISMATCH), + SQLITE_MISUSE(CApi.SQLITE_MISUSE), + SQLITE_NOLFS(CApi.SQLITE_NOLFS), + SQLITE_AUTH(CApi.SQLITE_AUTH), + SQLITE_FORMAT(CApi.SQLITE_FORMAT), + SQLITE_RANGE(CApi.SQLITE_RANGE), + SQLITE_NOTADB(CApi.SQLITE_NOTADB), + SQLITE_NOTICE(CApi.SQLITE_NOTICE), + SQLITE_WARNING(CApi.SQLITE_WARNING), + SQLITE_ROW(CApi.SQLITE_ROW), + SQLITE_DONE(CApi.SQLITE_DONE), + SQLITE_ERROR_MISSING_COLLSEQ(CApi.SQLITE_ERROR_MISSING_COLLSEQ), + SQLITE_ERROR_RETRY(CApi.SQLITE_ERROR_RETRY), + SQLITE_ERROR_SNAPSHOT(CApi.SQLITE_ERROR_SNAPSHOT), + SQLITE_IOERR_READ(CApi.SQLITE_IOERR_READ), + SQLITE_IOERR_SHORT_READ(CApi.SQLITE_IOERR_SHORT_READ), + SQLITE_IOERR_WRITE(CApi.SQLITE_IOERR_WRITE), + SQLITE_IOERR_FSYNC(CApi.SQLITE_IOERR_FSYNC), + SQLITE_IOERR_DIR_FSYNC(CApi.SQLITE_IOERR_DIR_FSYNC), + SQLITE_IOERR_TRUNCATE(CApi.SQLITE_IOERR_TRUNCATE), + SQLITE_IOERR_FSTAT(CApi.SQLITE_IOERR_FSTAT), + SQLITE_IOERR_UNLOCK(CApi.SQLITE_IOERR_UNLOCK), + SQLITE_IOERR_RDLOCK(CApi.SQLITE_IOERR_RDLOCK), + SQLITE_IOERR_DELETE(CApi.SQLITE_IOERR_DELETE), + SQLITE_IOERR_BLOCKED(CApi.SQLITE_IOERR_BLOCKED), + SQLITE_IOERR_NOMEM(CApi.SQLITE_IOERR_NOMEM), + SQLITE_IOERR_ACCESS(CApi.SQLITE_IOERR_ACCESS), + SQLITE_IOERR_CHECKRESERVEDLOCK(CApi.SQLITE_IOERR_CHECKRESERVEDLOCK), + SQLITE_IOERR_LOCK(CApi.SQLITE_IOERR_LOCK), + SQLITE_IOERR_CLOSE(CApi.SQLITE_IOERR_CLOSE), + SQLITE_IOERR_DIR_CLOSE(CApi.SQLITE_IOERR_DIR_CLOSE), + SQLITE_IOERR_SHMOPEN(CApi.SQLITE_IOERR_SHMOPEN), + SQLITE_IOERR_SHMSIZE(CApi.SQLITE_IOERR_SHMSIZE), + SQLITE_IOERR_SHMLOCK(CApi.SQLITE_IOERR_SHMLOCK), + SQLITE_IOERR_SHMMAP(CApi.SQLITE_IOERR_SHMMAP), + SQLITE_IOERR_SEEK(CApi.SQLITE_IOERR_SEEK), + SQLITE_IOERR_DELETE_NOENT(CApi.SQLITE_IOERR_DELETE_NOENT), + SQLITE_IOERR_MMAP(CApi.SQLITE_IOERR_MMAP), + SQLITE_IOERR_GETTEMPPATH(CApi.SQLITE_IOERR_GETTEMPPATH), + SQLITE_IOERR_CONVPATH(CApi.SQLITE_IOERR_CONVPATH), + SQLITE_IOERR_VNODE(CApi.SQLITE_IOERR_VNODE), + SQLITE_IOERR_AUTH(CApi.SQLITE_IOERR_AUTH), + SQLITE_IOERR_BEGIN_ATOMIC(CApi.SQLITE_IOERR_BEGIN_ATOMIC), + SQLITE_IOERR_COMMIT_ATOMIC(CApi.SQLITE_IOERR_COMMIT_ATOMIC), + SQLITE_IOERR_ROLLBACK_ATOMIC(CApi.SQLITE_IOERR_ROLLBACK_ATOMIC), + SQLITE_IOERR_DATA(CApi.SQLITE_IOERR_DATA), + SQLITE_IOERR_CORRUPTFS(CApi.SQLITE_IOERR_CORRUPTFS), + SQLITE_LOCKED_SHAREDCACHE(CApi.SQLITE_LOCKED_SHAREDCACHE), + SQLITE_LOCKED_VTAB(CApi.SQLITE_LOCKED_VTAB), + SQLITE_BUSY_RECOVERY(CApi.SQLITE_BUSY_RECOVERY), + SQLITE_BUSY_SNAPSHOT(CApi.SQLITE_BUSY_SNAPSHOT), + SQLITE_BUSY_TIMEOUT(CApi.SQLITE_BUSY_TIMEOUT), + SQLITE_CANTOPEN_NOTEMPDIR(CApi.SQLITE_CANTOPEN_NOTEMPDIR), + SQLITE_CANTOPEN_ISDIR(CApi.SQLITE_CANTOPEN_ISDIR), + SQLITE_CANTOPEN_FULLPATH(CApi.SQLITE_CANTOPEN_FULLPATH), + SQLITE_CANTOPEN_CONVPATH(CApi.SQLITE_CANTOPEN_CONVPATH), + SQLITE_CANTOPEN_SYMLINK(CApi.SQLITE_CANTOPEN_SYMLINK), + SQLITE_CORRUPT_VTAB(CApi.SQLITE_CORRUPT_VTAB), + SQLITE_CORRUPT_SEQUENCE(CApi.SQLITE_CORRUPT_SEQUENCE), + SQLITE_CORRUPT_INDEX(CApi.SQLITE_CORRUPT_INDEX), + SQLITE_READONLY_RECOVERY(CApi.SQLITE_READONLY_RECOVERY), + SQLITE_READONLY_CANTLOCK(CApi.SQLITE_READONLY_CANTLOCK), + SQLITE_READONLY_ROLLBACK(CApi.SQLITE_READONLY_ROLLBACK), + SQLITE_READONLY_DBMOVED(CApi.SQLITE_READONLY_DBMOVED), + SQLITE_READONLY_CANTINIT(CApi.SQLITE_READONLY_CANTINIT), + SQLITE_READONLY_DIRECTORY(CApi.SQLITE_READONLY_DIRECTORY), + SQLITE_ABORT_ROLLBACK(CApi.SQLITE_ABORT_ROLLBACK), + SQLITE_CONSTRAINT_CHECK(CApi.SQLITE_CONSTRAINT_CHECK), + SQLITE_CONSTRAINT_COMMITHOOK(CApi.SQLITE_CONSTRAINT_COMMITHOOK), + SQLITE_CONSTRAINT_FOREIGNKEY(CApi.SQLITE_CONSTRAINT_FOREIGNKEY), + SQLITE_CONSTRAINT_FUNCTION(CApi.SQLITE_CONSTRAINT_FUNCTION), + SQLITE_CONSTRAINT_NOTNULL(CApi.SQLITE_CONSTRAINT_NOTNULL), + SQLITE_CONSTRAINT_PRIMARYKEY(CApi.SQLITE_CONSTRAINT_PRIMARYKEY), + SQLITE_CONSTRAINT_TRIGGER(CApi.SQLITE_CONSTRAINT_TRIGGER), + SQLITE_CONSTRAINT_UNIQUE(CApi.SQLITE_CONSTRAINT_UNIQUE), + SQLITE_CONSTRAINT_VTAB(CApi.SQLITE_CONSTRAINT_VTAB), + SQLITE_CONSTRAINT_ROWID(CApi.SQLITE_CONSTRAINT_ROWID), + SQLITE_CONSTRAINT_PINNED(CApi.SQLITE_CONSTRAINT_PINNED), + SQLITE_CONSTRAINT_DATATYPE(CApi.SQLITE_CONSTRAINT_DATATYPE), + SQLITE_NOTICE_RECOVER_WAL(CApi.SQLITE_NOTICE_RECOVER_WAL), + SQLITE_NOTICE_RECOVER_ROLLBACK(CApi.SQLITE_NOTICE_RECOVER_ROLLBACK), + SQLITE_WARNING_AUTOINDEX(CApi.SQLITE_WARNING_AUTOINDEX), + SQLITE_AUTH_USER(CApi.SQLITE_AUTH_USER), + SQLITE_OK_LOAD_PERMANENTLY(CApi.SQLITE_OK_LOAD_PERMANENTLY); + + public final int value; + + ResultCode(int rc){ + value = rc; + ResultCodeMap.set(rc, this); + } + + /** + Returns the entry from this enum for the given result code, or + null if no match is found. + */ + public static ResultCode getEntryForInt(int rc){ + return ResultCodeMap.get(rc); + } + + /** + Internal level of indirection required because we cannot initialize + static enum members in an enum before the enum constructor is + invoked. + */ + private static final class ResultCodeMap { + private static final java.util.Map i2e + = new java.util.HashMap<>(); + private static void set(int rc, ResultCode e){ i2e.put(rc, e); } + private static ResultCode get(int rc){ return i2e.get(rc); } + } + +} diff --git a/ext/jni/src/org/sqlite/jni/capi/RollbackHookCallback.java b/ext/jni/src/org/sqlite/jni/capi/RollbackHookCallback.java new file mode 100644 index 0000000000..cf9c4b6e7a --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/capi/RollbackHookCallback.java @@ -0,0 +1,26 @@ +/* +** 2023-08-25 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file is part of the JNI bindings for the sqlite3 C API. +*/ +package org.sqlite.jni.capi; + +/** + Callback for use with {@link CApi#sqlite3_rollback_hook}. +*/ +public interface RollbackHookCallback extends CallbackProxy { + /** + Must function as documented for the C-level sqlite3_rollback_hook() + callback. If it throws, the exception is translated into + a db-level error. + */ + void call(); +} diff --git a/ext/jni/src/org/sqlite/jni/capi/SQLFunction.java b/ext/jni/src/org/sqlite/jni/capi/SQLFunction.java new file mode 100644 index 0000000000..7ad1381a7a --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/capi/SQLFunction.java @@ -0,0 +1,36 @@ +/* +** 2023-07-22 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file is part of the JNI bindings for the sqlite3 C API. +*/ +package org.sqlite.jni.capi; + +/** + SQLFunction is used in conjunction with the + sqlite3_create_function() JNI-bound API to give that native code + access to the callback functions needed in order to implement SQL + functions in Java. + +

    + + This class is not used by itself, but is a marker base class. The + three UDF types are modelled by the inner classes Scalar, + Aggregate, and Window. Most simply, clients may subclass + those, or create anonymous classes from them, to implement + UDFs. Clients are free to create their own classes for use with + UDFs, so long as they conform to the public interfaces defined by + those three classes. The JNI layer only actively relies on the + SQLFunction base class and the method names and signatures used by + the UDF callback interfaces. +*/ +public interface SQLFunction { + +} diff --git a/ext/jni/src/org/sqlite/jni/capi/SQLTester.java b/ext/jni/src/org/sqlite/jni/capi/SQLTester.java new file mode 100644 index 0000000000..bc2e75f8be --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/capi/SQLTester.java @@ -0,0 +1,1449 @@ +/* +** 2023-08-08 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains the main application entry pointer for the +** SQLTester framework. +*/ +package org.sqlite.jni.capi; +import java.util.ArrayList; +import java.util.Arrays; +import java.nio.charset.StandardCharsets; +import java.util.regex.*; +import static org.sqlite.jni.capi.CApi.*; + +/** + Modes for how to escape (or not) column values and names from + SQLTester.execSql() to the result buffer output. +*/ +enum ResultBufferMode { + //! Do not append to result buffer + NONE, + //! Append output escaped. + ESCAPED, + //! Append output as-is + ASIS +} + +/** + Modes to specify how to emit multi-row output from + SQLTester.execSql() to the result buffer. +*/ +enum ResultRowMode { + //! Keep all result rows on one line, space-separated. + ONELINE, + //! Add a newline between each result row. + NEWLINE +} + +/** + Base exception type for test-related failures. +*/ +class SQLTesterException extends RuntimeException { + private boolean bFatal = false; + + SQLTesterException(String msg){ + super(msg); + } + + protected SQLTesterException(String msg, boolean fatal){ + super(msg); + bFatal = fatal; + } + + /** + Indicates whether the framework should consider this exception + type as immediately fatal to the test run or not. + */ + final boolean isFatal(){ return bFatal; } +} + +class DbException extends SQLTesterException { + DbException(sqlite3 db, int rc, boolean closeDb){ + super("DB error #"+rc+": "+sqlite3_errmsg(db),true); + if( closeDb ) sqlite3_close_v2(db); + } + DbException(sqlite3 db, int rc){ + this(db, rc, false); + } +} + +/** + Generic test-failed exception. + */ +class TestScriptFailed extends SQLTesterException { + public TestScriptFailed(TestScript ts, String msg){ + super(ts.getOutputPrefix()+": "+msg, true); + } +} + +/** + Thrown when an unknown test command is encountered in a script. +*/ +class UnknownCommand extends SQLTesterException { + public UnknownCommand(TestScript ts, String cmd){ + super(ts.getOutputPrefix()+": unknown command: "+cmd, false); + } +} + +/** + Thrown when an "incompatible directive" is found in a script. This + can be the presence of a C-preprocessor construct, specific + metadata tags within a test script's header, or specific test + constructs which are incompatible with this particular + implementation. +*/ +class IncompatibleDirective extends SQLTesterException { + public IncompatibleDirective(TestScript ts, String line){ + super(ts.getOutputPrefix()+": incompatible directive: "+line, false); + } +} + +/** + Console output utility class. +*/ +class Outer { + private int verbosity = 0; + + static void out(Object val){ + System.out.print(val); + } + + Outer out(Object... vals){ + for(Object v : vals) out(v); + return this; + } + + Outer outln(Object... vals){ + out(vals).out("\n"); + return this; + } + + Outer verbose(Object... vals){ + if(verbosity>0){ + out("VERBOSE",(verbosity>1 ? "+: " : ": ")).outln(vals); + } + return this; + } + + void setVerbosity(int level){ + verbosity = level; + } + + int getVerbosity(){ + return verbosity; + } + + public boolean isVerbose(){return verbosity > 0;} + +} + +/** +

    This class provides an application which aims to implement the + rudimentary SQL-driven test tool described in the accompanying + {@code test-script-interpreter.md}. + +

    This class is an internal testing tool, not part of the public + interface but is (A) in the same package as the library because + access permissions require it to be so and (B) the JDK8 javadoc + offers no way to filter individual classes out of the doc + generation process (it can only exclude packages, but see (A)). + +

    An instance of this application provides a core set of services + which TestScript instances use for processing testing logic. + TestScripts, in turn, delegate the concrete test work to Command + objects, which the TestScript parses on their behalf. +*/ +public class SQLTester { + //! List of input script files. + private final java.util.List listInFiles = new ArrayList<>(); + //! Console output utility. + private final Outer outer = new Outer(); + //! Test input buffer. + private final StringBuilder inputBuffer = new StringBuilder(); + //! Test result buffer. + private final StringBuilder resultBuffer = new StringBuilder(); + //! Buffer for REQUIRED_PROPERTIES pragmas. + private final StringBuilder dbInitSql = new StringBuilder(); + //! Output representation of SQL NULL. + private String nullView = "nil"; + //! Total tests run. + private int nTotalTest = 0; + //! Total test script files run. + private int nTestFile = 0; + //! Number of scripts which were aborted. + private int nAbortedScript = 0; + //! Incremented by test case handlers + private int nTest = 0; + //! True to enable column name output from execSql() + private boolean emitColNames; + //! True to keep going regardless of how a test fails. + private boolean keepGoing = false; + //! The list of available db handles. + private final sqlite3[] aDb = new sqlite3[7]; + //! Index into aDb of the current db. + private int iCurrentDb = 0; + //! Name of the default db, re-created for each script. + private final String initialDbName = "test.db"; + + + public SQLTester(){ + reset(); + } + + void setVerbosity(int level){ + this.outer.setVerbosity( level ); + } + int getVerbosity(){ + return this.outer.getVerbosity(); + } + boolean isVerbose(){ + return this.outer.isVerbose(); + } + + void outputColumnNames(boolean b){ emitColNames = b; } + + void verbose(Object... vals){ + outer.verbose(vals); + } + + void outln(Object... vals){ + outer.outln(vals); + } + + void out(Object... vals){ + outer.out(vals); + } + + //! Adds the given test script to the to-test list. + public void addTestScript(String filename){ + listInFiles.add(filename); + //verbose("Added file ",filename); + } + + private void setupInitialDb() throws DbException { + if( null==aDb[0] ){ + Util.unlink(initialDbName); + openDb(0, initialDbName, true); + }else{ + outln("WARNING: setupInitialDb() unexpectedly ", + "triggered while it is opened."); + } + } + + static final String[] startEmoji = { + "🚴", "🏄", "🏇", "🤸", "⛹", "🏊", "⛷", "🧗", "🏋" + }; + static final int nStartEmoji = startEmoji.length; + static int iStartEmoji = 0; + + private static String nextStartEmoji(){ + return startEmoji[iStartEmoji++ % nStartEmoji]; + } + + public void runTests() throws Exception { + final long tStart = System.currentTimeMillis(); + for(String f : listInFiles){ + reset(); + ++nTestFile; + final TestScript ts = new TestScript(f); + outln(nextStartEmoji(), " starting [",f,"]"); + boolean threw = false; + final long timeStart = System.currentTimeMillis(); + try{ + ts.run(this); + }catch(SQLTesterException e){ + threw = true; + outln("🔥EXCEPTION: ",e.getClass().getSimpleName(),": ",e.getMessage()); + ++nAbortedScript; + if( keepGoing ) outln("Continuing anyway because of the keep-going option."); + else if( e.isFatal() ) throw e; + }finally{ + final long timeEnd = System.currentTimeMillis(); + outln("🏁",(threw ? "❌" : "✅")," ",nTest," test(s) in ", + (timeEnd-timeStart),"ms."); + } + } + final long tEnd = System.currentTimeMillis(); + outln("Total run-time: ",(tEnd-tStart),"ms"); + Util.unlink(initialDbName); + } + + private StringBuilder clearBuffer(StringBuilder b){ + b.setLength(0); + return b; + } + + StringBuilder clearInputBuffer(){ + return clearBuffer(inputBuffer); + } + + StringBuilder clearResultBuffer(){ + return clearBuffer(resultBuffer); + } + + StringBuilder getInputBuffer(){ return inputBuffer; } + + void appendInput(String n, boolean addNL){ + inputBuffer.append(n); + if(addNL) inputBuffer.append('\n'); + } + + void appendResult(String n, boolean addNL){ + resultBuffer.append(n); + if(addNL) resultBuffer.append('\n'); + } + + void appendDbInitSql(String n) throws DbException { + dbInitSql.append(n).append('\n'); + if( null!=getCurrentDb() ){ + //outln("RUNNING DB INIT CODE: ",n); + execSql(null, true, ResultBufferMode.NONE, null, n); + } + } + String getDbInitSql(){ return dbInitSql.toString(); } + + String getInputText(){ return inputBuffer.toString(); } + + String getResultText(){ return resultBuffer.toString(); } + + private String takeBuffer(StringBuilder b){ + final String rc = b.toString(); + clearBuffer(b); + return rc; + } + + String takeInputBuffer(){ return takeBuffer(inputBuffer); } + + String takeResultBuffer(){ return takeBuffer(resultBuffer); } + + int getCurrentDbId(){ return iCurrentDb; } + + SQLTester affirmDbId(int n) throws IndexOutOfBoundsException { + if(n<0 || n>=aDb.length){ + throw new IndexOutOfBoundsException("illegal db number: "+n); + } + return this; + } + + sqlite3 setCurrentDb(int n){ + affirmDbId(n); + iCurrentDb = n; + return this.aDb[n]; + } + + sqlite3 getCurrentDb(){ return aDb[iCurrentDb]; } + + sqlite3 getDbById(int id){ + return affirmDbId(id).aDb[id]; + } + + void closeDb(int id) { + final sqlite3 db = affirmDbId(id).aDb[id]; + if( null != db ){ + sqlite3_close_v2(db); + aDb[id] = null; + } + } + + void closeDb() { closeDb(iCurrentDb); } + + void closeAllDbs(){ + for(int i = 0; i 0){ + //outln("RUNNING DB INIT CODE: ",dbInitSql.toString()); + rc = execSql(db, false, ResultBufferMode.NONE, + null, dbInitSql.toString()); + } + if( 0!=rc ){ + throw new DbException(db, rc, true); + } + return aDb[iCurrentDb] = db; + } + + sqlite3 openDb(int slot, String name, boolean createIfNeeded) throws DbException { + affirmDbId(slot); + iCurrentDb = slot; + return openDb(name, createIfNeeded); + } + + /** + Resets all tester context state except for that related to + tracking running totals. + */ + void reset(){ + clearInputBuffer(); + clearResultBuffer(); + clearBuffer(dbInitSql); + closeAllDbs(); + nTest = 0; + nullView = "nil"; + emitColNames = false; + iCurrentDb = 0; + //dbInitSql.append("SELECT 1;"); + } + + void setNullValue(String v){nullView = v;} + + /** + If true, encountering an unknown command in a script causes the + remainder of the script to be skipped, rather than aborting the + whole script run. + */ + boolean skipUnknownCommands(){ + // Currently hard-coded. Potentially a flag someday. + return true; + } + + void incrementTestCounter(){ ++nTest; ++nTotalTest; } + + //! "Special" characters - we have to escape output if it contains any. + static final Pattern patternSpecial = Pattern.compile( + "[\\x00-\\x20\\x22\\x5c\\x7b\\x7d]" + ); + //! Either of '{' or '}'. + static final Pattern patternSquiggly = Pattern.compile("[{}]"); + + /** + Returns v or some escaped form of v, as defined in the tester's + spec doc. + */ + String escapeSqlValue(String v){ + if( "".equals(v) ) return "{}"; + Matcher m = patternSpecial.matcher(v); + if( !m.find() ){ + return v /* no escaping needed */; + } + m = patternSquiggly.matcher(v); + if( !m.find() ){ + return "{"+v+"}"; + } + final StringBuilder sb = new StringBuilder("\""); + final int n = v.length(); + for(int i = 0; i < n; ++i){ + final char ch = v.charAt(i); + switch(ch){ + case '\\': sb.append("\\\\"); break; + case '"': sb.append("\\\""); break; + default: + //verbose("CHAR ",(int)ch," ",ch," octal=",String.format("\\%03o", (int)ch)); + if( (int)ch < 32 ) sb.append(String.format("\\%03o", (int)ch)); + else sb.append(ch); + break; + } + } + sb.append("\""); + return sb.toString(); + } + + private void appendDbErr(sqlite3 db, StringBuilder sb, int rc){ + sb.append(org.sqlite.jni.capi.ResultCode.getEntryForInt(rc)).append(' '); + final String msg = escapeSqlValue(sqlite3_errmsg(db)); + if( '{' == msg.charAt(0) ){ + sb.append(msg); + }else{ + sb.append('{').append(msg).append('}'); + } + } + + /** + Runs SQL on behalf of test commands and outputs the results following + the very specific rules of the test framework. + + If db is null, getCurrentDb() is assumed. If throwOnError is true then + any db-side error will result in an exception, else they result in + the db's result code. + + appendMode specifies how/whether to append results to the result + buffer. rowMode specifies whether to output all results in a + single line or one line per row. If appendMode is + ResultBufferMode.NONE then rowMode is ignored and may be null. + */ + public int execSql(sqlite3 db, boolean throwOnError, + ResultBufferMode appendMode, ResultRowMode rowMode, + String sql) throws SQLTesterException { + if( null==db && null==aDb[0] ){ + // Delay opening of the initial db to enable tests to change its + // name and inject on-connect code via, e.g., the MEMDB + // directive. this setup as the potential to misinteract with + // auto-extension timing and must be done carefully. + setupInitialDb(); + } + final OutputPointer.Int32 oTail = new OutputPointer.Int32(); + final OutputPointer.sqlite3_stmt outStmt = new OutputPointer.sqlite3_stmt(); + final byte[] sqlUtf8 = sql.getBytes(StandardCharsets.UTF_8); + if( null==db ) db = getCurrentDb(); + int pos = 0, n = 1; + byte[] sqlChunk = sqlUtf8; + int rc = 0; + sqlite3_stmt stmt = null; + int spacing = 0 /* emit a space for --result if>0 */ ; + final StringBuilder sb = (ResultBufferMode.NONE==appendMode) + ? null : resultBuffer; + //outln("sqlChunk len= = ",sqlChunk.length); + try{ + while(pos < sqlChunk.length){ + if(pos > 0){ + sqlChunk = Arrays.copyOfRange(sqlChunk, pos, + sqlChunk.length); + } + if( 0==sqlChunk.length ) break; + rc = sqlite3_prepare_v2(db, sqlChunk, outStmt, oTail); + /*outln("PREPARE rc ",rc," oTail=",oTail.get(),": ", + new String(sqlChunk,StandardCharsets.UTF_8),"\n");*/ + if( 0!=rc ){ + if(throwOnError){ + throw new DbException(db, rc); + }else if( null!=sb ){ + appendDbErr(db, sb, rc); + } + break; + } + pos = oTail.value; + stmt = outStmt.take(); + if( null == stmt ){ + // empty statement was parsed. + continue; + } + if( null!=sb ){ + // Add the output to the result buffer... + final int nCol = sqlite3_column_count(stmt); + String colName = null, val = null; + while( SQLITE_ROW == (rc = sqlite3_step(stmt)) ){ + for(int i = 0; i < nCol; ++i){ + if( spacing++ > 0 ) sb.append(' '); + if( emitColNames ){ + colName = sqlite3_column_name(stmt, i); + switch(appendMode){ + case ASIS: + sb.append( colName ); + break; + case ESCAPED: + sb.append( escapeSqlValue(colName) ); + break; + default: + throw new SQLTesterException("Unhandled ResultBufferMode: "+appendMode); + } + sb.append(' '); + } + val = sqlite3_column_text16(stmt, i); + if( null==val ){ + sb.append( nullView ); + continue; + } + switch(appendMode){ + case ASIS: + sb.append( val ); + break; + case ESCAPED: + sb.append( escapeSqlValue(val) ); + break; + default: + throw new SQLTesterException("Unhandled ResultBufferMode: "+appendMode); + } + } + if( ResultRowMode.NEWLINE == rowMode ){ + spacing = 0; + sb.append('\n'); + } + } + }else{ + while( SQLITE_ROW == (rc = sqlite3_step(stmt)) ){} + } + sqlite3_finalize(stmt); + stmt = null; + if(SQLITE_ROW==rc || SQLITE_DONE==rc) rc = 0; + else if( rc!=0 ){ + if( null!=sb ){ + appendDbErr(db, sb, rc); + } + break; + } + } + }finally{ + sqlite3_reset(stmt + /* In order to trigger an exception in the + INSERT...RETURNING locking scenario: + https://sqlite.org/forum/forumpost/36f7a2e7494897df */); + sqlite3_finalize(stmt); + } + if( 0!=rc && throwOnError ){ + throw new DbException(db, rc); + } + return rc; + } + + public static void main(String[] argv) throws Exception{ + installCustomExtensions(); + boolean dumpInternals = false; + final SQLTester t = new SQLTester(); + for(String a : argv){ + if(a.startsWith("-")){ + final String flag = a.replaceFirst("-+",""); + if( flag.equals("verbose") ){ + // Use --verbose up to 3 times + t.setVerbosity(t.getVerbosity() + 1); + }else if( flag.equals("keep-going") ){ + t.keepGoing = true; + }else if( flag.equals("internals") ){ + dumpInternals = true; + }else{ + throw new IllegalArgumentException("Unhandled flag: "+flag); + } + continue; + } + t.addTestScript(a); + } + final AutoExtensionCallback ax = new AutoExtensionCallback() { + private final SQLTester tester = t; + @Override public int call(sqlite3 db){ + final String init = tester.getDbInitSql(); + if( !init.isEmpty() ){ + tester.execSql(db, true, ResultBufferMode.NONE, null, init); + } + return 0; + } + }; + sqlite3_auto_extension(ax); + try { + t.runTests(); + }finally{ + sqlite3_cancel_auto_extension(ax); + t.outln("Processed ",t.nTotalTest," test(s) in ",t.nTestFile," file(s)."); + if( t.nAbortedScript > 0 ){ + t.outln("Aborted ",t.nAbortedScript," script(s)."); + } + if( dumpInternals ){ + sqlite3_jni_internal_details(); + } + } + } + + /** + Internal impl of the public strglob() method. Neither argument + may be NULL and both _MUST_ be NUL-terminated. + */ + private static native int strglob(byte[] glob, byte[] txt); + + /** + Works essentially the same as sqlite3_strglob() except that the + glob character '#' matches a sequence of one or more digits. It + does not match when it appears at the start or middle of a series + of digits, e.g. "#23" or "1#3", but will match at the end, + e.g. "12#". + */ + static int strglob(String glob, String txt){ + return strglob( + (glob+"\0").getBytes(StandardCharsets.UTF_8), + (txt+"\0").getBytes(StandardCharsets.UTF_8) + ); + } + + /** + Sets up C-side components needed by the test framework. This must + not be called until main() is triggered so that it does not + interfere with library clients who don't use this class. + */ + static native void installCustomExtensions(); + static { + System.loadLibrary("sqlite3-jni") + /* Interestingly, when SQLTester is the main app, we have to + load that lib from here. The same load from CApi does + not happen early enough. Without this, + installCustomExtensions() is an unresolved symbol. */; + } + +} + +/** + General utilities for the SQLTester bits. +*/ +final class Util { + + //! Throws a new T, appending all msg args into a string for the message. + static void toss(Class errorType, Object... msg) throws Exception { + StringBuilder sb = new StringBuilder(); + for(Object s : msg) sb.append(s); + final java.lang.reflect.Constructor ctor = + errorType.getConstructor(String.class); + throw ctor.newInstance(sb.toString()); + } + + static void toss(Object... msg) throws Exception{ + toss(RuntimeException.class, msg); + } + + //! Tries to delete the given file, silently ignoring failure. + static void unlink(String filename){ + try{ + final java.io.File f = new java.io.File(filename); + f.delete(); + }catch(Exception e){ + /* ignore */ + } + } + + /** + Appends all entries in argv[1..end] into a space-separated + string, argv[0] is not included because it's expected to be a + command name. + */ + static String argvToString(String[] argv){ + StringBuilder sb = new StringBuilder(); + for(int i = 1; i < argv.length; ++i ){ + if( i>1 ) sb.append(" "); + sb.append( argv[i] ); + } + return sb.toString(); + } + +} + +/** + Base class for test script commands. It provides a set of utility + APIs for concrete command implementations. + + Each subclass must have a public no-arg ctor and must implement + the process() method which is abstract in this class. + + Commands are intended to be stateless, except perhaps for counters + and similar internals. Specifically, no state which changes the + behavior between any two invocations of process() should be + retained. +*/ +abstract class Command { + protected Command(){} + + /** + Must process one command-unit of work and either return + (on success) or throw (on error). + + The first two arguments specify the context of the test. The TestScript + provides the content of the test and the SQLTester providers the sandbox + in which that script is being evaluated. + + argv is a list with the command name followed by any arguments to + that command. The argcCheck() method from this class provides + very basic argc validation. + */ + public abstract void process( + SQLTester st, TestScript ts, String[] argv + ) throws Exception; + + /** + If argv.length-1 (-1 because the command's name is in argv[0]) does not + fall in the inclusive range (min,max) then this function throws. Use + a max value of -1 to mean unlimited. + */ + protected final void argcCheck(TestScript ts, String[] argv, int min, int max){ + int argc = argv.length-1; + if(argc=0 && argc>max)){ + if( min==max ){ + ts.toss(argv[0]," requires exactly ",min," argument(s)"); + }else if(max>0){ + ts.toss(argv[0]," requires ",min,"-",max," arguments."); + }else{ + ts.toss(argv[0]," requires at least ",min," arguments."); + } + } + } + + /** + Equivalent to argcCheck(argv,argc,argc). + */ + protected final void argcCheck(TestScript ts, String[] argv, int argc){ + argcCheck(ts, argv, argc, argc); + } +} + +//! --close command +class CloseDbCommand extends Command { + public void process(SQLTester t, TestScript ts, String[] argv){ + argcCheck(ts,argv,0,1); + int id; + if(argv.length>1){ + String arg = argv[1]; + if("all".equals(arg)){ + t.closeAllDbs(); + return; + } + else{ + id = Integer.parseInt(arg); + } + }else{ + id = t.getCurrentDbId(); + } + t.closeDb(id); + } +} + +//! --column-names command +class ColumnNamesCommand extends Command { + public void process( + SQLTester st, TestScript ts, String[] argv + ){ + argcCheck(ts,argv,1); + st.outputColumnNames( Integer.parseInt(argv[1])!=0 ); + } +} + +//! --db command +class DbCommand extends Command { + public void process(SQLTester t, TestScript ts, String[] argv){ + argcCheck(ts,argv,1); + t.setCurrentDb( Integer.parseInt(argv[1]) ); + } +} + +//! --glob command +class GlobCommand extends Command { + private boolean negate = false; + public GlobCommand(){} + protected GlobCommand(boolean negate){ this.negate = negate; } + + public void process(SQLTester t, TestScript ts, String[] argv){ + argcCheck(ts,argv,1,-1); + t.incrementTestCounter(); + final String sql = t.takeInputBuffer(); + int rc = t.execSql(null, true, ResultBufferMode.ESCAPED, + ResultRowMode.ONELINE, sql); + final String result = t.getResultText(); + final String sArgs = Util.argvToString(argv); + //t2.verbose2(argv[0]," rc = ",rc," result buffer:\n", result,"\nargs:\n",sArgs); + final String glob = Util.argvToString(argv); + rc = SQLTester.strglob(glob, result); + if( (negate && 0==rc) || (!negate && 0!=rc) ){ + ts.toss(argv[0], " mismatch: ", glob," vs input: ",result); + } + } +} + +//! --json command +class JsonCommand extends ResultCommand { + public JsonCommand(){ super(ResultBufferMode.ASIS); } +} + +//! --json-block command +class JsonBlockCommand extends TableResultCommand { + public JsonBlockCommand(){ super(true); } +} + +//! --new command +class NewDbCommand extends OpenDbCommand { + public NewDbCommand(){ super(true); } + public void process(SQLTester t, TestScript ts, String[] argv){ + if(argv.length>1){ + Util.unlink(argv[1]); + } + super.process(t, ts, argv); + } + +} + +//! Placeholder dummy/no-op/unimplemented commands +class NoopCommand extends Command { + private boolean verbose = false; + public NoopCommand(boolean verbose){ + this.verbose = verbose; + } + public NoopCommand(){} + public void process(SQLTester t, TestScript ts, String[] argv){ + if( this.verbose ){ + t.outln("Skipping unhandled command: "+argv[0]); + } + } +} + +//! --notglob command +class NotGlobCommand extends GlobCommand { + public NotGlobCommand(){ + super(true); + } +} + +//! --null command +class NullCommand extends Command { + public void process( + SQLTester st, TestScript ts, String[] argv + ){ + argcCheck(ts,argv,1); + st.setNullValue( argv[1] ); + } +} + +//! --open command +class OpenDbCommand extends Command { + private boolean createIfNeeded = false; + public OpenDbCommand(){} + protected OpenDbCommand(boolean c){createIfNeeded = c;} + public void process(SQLTester t, TestScript ts, String[] argv){ + argcCheck(ts,argv,1); + t.openDb(argv[1], createIfNeeded); + } +} + +//! --print command +class PrintCommand extends Command { + public void process( + SQLTester st, TestScript ts, String[] argv + ){ + st.out(ts.getOutputPrefix(),": "); + if( 1==argv.length ){ + st.out( st.getInputText() ); + }else{ + st.outln( Util.argvToString(argv) ); + } + } +} + +//! --result command +class ResultCommand extends Command { + private final ResultBufferMode bufferMode; + protected ResultCommand(ResultBufferMode bm){ bufferMode = bm; } + public ResultCommand(){ this(ResultBufferMode.ESCAPED); } + public void process(SQLTester t, TestScript ts, String[] argv){ + argcCheck(ts,argv,0,-1); + t.incrementTestCounter(); + final String sql = t.takeInputBuffer(); + //ts.verbose2(argv[0]," SQL =\n",sql); + int rc = t.execSql(null, false, bufferMode, ResultRowMode.ONELINE, sql); + final String result = t.getResultText().trim(); + final String sArgs = argv.length>1 ? Util.argvToString(argv) : ""; + if( !result.equals(sArgs) ){ + t.outln(argv[0]," FAILED comparison. Result buffer:\n", + result,"\nExpected result:\n",sArgs); + ts.toss(argv[0]+" comparison failed."); + } + } +} + +//! --run command +class RunCommand extends Command { + public void process(SQLTester t, TestScript ts, String[] argv){ + argcCheck(ts,argv,0,1); + final sqlite3 db = (1==argv.length) + ? t.getCurrentDb() : t.getDbById( Integer.parseInt(argv[1]) ); + final String sql = t.takeInputBuffer(); + final int rc = t.execSql(db, false, ResultBufferMode.NONE, + ResultRowMode.ONELINE, sql); + if( 0!=rc && t.isVerbose() ){ + String msg = sqlite3_errmsg(db); + ts.verbose1(argv[0]," non-fatal command error #",rc,": ", + msg,"\nfor SQL:\n",sql); + } + } +} + +//! --tableresult command +class TableResultCommand extends Command { + private final boolean jsonMode; + protected TableResultCommand(boolean jsonMode){ this.jsonMode = jsonMode; } + public TableResultCommand(){ this(false); } + public void process(SQLTester t, TestScript ts, String[] argv){ + argcCheck(ts,argv,0); + t.incrementTestCounter(); + String body = ts.fetchCommandBody(t); + if( null==body ) ts.toss("Missing ",argv[0]," body."); + body = body.trim(); + if( !body.endsWith("\n--end") ){ + ts.toss(argv[0], " must be terminated with --end."); + }else{ + body = body.substring(0, body.length()-6); + } + final String[] globs = body.split("\\s*\\n\\s*"); + if( globs.length < 1 ){ + ts.toss(argv[0], " requires 1 or more ", + (jsonMode ? "json snippets" : "globs"),"."); + } + final String sql = t.takeInputBuffer(); + t.execSql(null, true, + jsonMode ? ResultBufferMode.ASIS : ResultBufferMode.ESCAPED, + ResultRowMode.NEWLINE, sql); + final String rbuf = t.getResultText(); + final String[] res = rbuf.split("\n"); + if( res.length != globs.length ){ + ts.toss(argv[0], " failure: input has ", res.length, + " row(s) but expecting ",globs.length); + } + for(int i = 0; i < res.length; ++i){ + final String glob = globs[i].replaceAll("\\s+"," ").trim(); + //ts.verbose2(argv[0]," <<",glob,">> vs <<",res[i],">>"); + if( jsonMode ){ + if( !glob.equals(res[i]) ){ + ts.toss(argv[0], " json <<",glob, ">> does not match: <<", + res[i],">>"); + } + }else if( 0 != SQLTester.strglob(glob, res[i]) ){ + ts.toss(argv[0], " glob <<",glob,">> does not match: <<",res[i],">>"); + } + } + } +} + +//! --testcase command +class TestCaseCommand extends Command { + public void process(SQLTester t, TestScript ts, String[] argv){ + argcCheck(ts,argv,1); + ts.setTestCaseName(argv[1]); + t.clearResultBuffer(); + t.clearInputBuffer(); + } +} + +//! --verbosity command +class VerbosityCommand extends Command { + public void process(SQLTester t, TestScript ts, String[] argv){ + argcCheck(ts,argv,1); + ts.setVerbosity( Integer.parseInt(argv[1]) ); + } +} + +class CommandDispatcher { + + private static final java.util.Map commandMap = + new java.util.HashMap<>(); + + /** + Returns a (cached) instance mapped to name, or null if no match + is found. + */ + static Command getCommandByName(String name){ + Command rv = commandMap.get(name); + if( null!=rv ) return rv; + switch(name){ + case "close": rv = new CloseDbCommand(); break; + case "column-names": rv = new ColumnNamesCommand(); break; + case "db": rv = new DbCommand(); break; + case "glob": rv = new GlobCommand(); break; + case "json": rv = new JsonCommand(); break; + case "jsonglob": rv = new NoopCommand(true); break; + case "json-block": rv = new JsonBlockCommand(); break; + case "new": rv = new NewDbCommand(); break; + case "notglob": rv = new NotGlobCommand(); break; + case "null": rv = new NullCommand(); break; + case "oom": rv = new NoopCommand(); break; + case "open": rv = new OpenDbCommand(); break; + case "print": rv = new PrintCommand(); break; + case "result": rv = new ResultCommand(); break; + case "run": rv = new RunCommand(); break; + case "stmt-cache": rv = new NoopCommand(); break; + case "tableresult": rv = new TableResultCommand(); break; + case "testcase": rv = new TestCaseCommand(); break; + case "verbosity": rv = new VerbosityCommand(); break; + default: rv = null; break; + } + if( null!=rv ) commandMap.put(name, rv); + return rv; + } + + /** + Treats argv[0] as a command name, looks it up with + getCommandByName(), and calls process() on that instance, passing + it arguments given to this function. + */ + static void dispatch(SQLTester tester, TestScript ts, String[] argv) throws Exception{ + final Command cmd = getCommandByName(argv[0]); + if(null == cmd){ + throw new UnknownCommand(ts, argv[0]); + } + cmd.process(tester, ts, argv); + } +} + + +/** + This class represents a single test script. It handles (or + delegates) its the reading-in and parsing, but the details of + evaluation are delegated elsewhere. +*/ +class TestScript { + //! input file + private String filename = null; + //! Name pulled from the SCRIPT_MODULE_NAME directive of the file + private String moduleName = null; + //! Current test case name. + private String testCaseName = null; + //! Content buffer state. + private final Cursor cur = new Cursor(); + //! Utility for console output. + private final Outer outer = new Outer(); + + //! File content and parse state. + private static final class Cursor { + private final StringBuilder sb = new StringBuilder(); + byte[] src = null; + //! Current position in this.src. + int pos = 0; + //! Current line number. Starts at 0 for internal reasons and will + // line up with 1-based reality once parsing starts. + int lineNo = 0 /* yes, zero */; + //! Putback value for this.pos. + int putbackPos = 0; + //! Putback line number + int putbackLineNo = 0; + //! Peeked-to pos, used by peekLine() and consumePeeked(). + int peekedPos = 0; + //! Peeked-to line number. + int peekedLineNo = 0; + + //! Restore parsing state to the start of the stream. + void rewind(){ + sb.setLength(0); + pos = lineNo = putbackPos = putbackLineNo = peekedPos = peekedLineNo = 0 + /* kinda missing memset() about now. */; + } + } + + private byte[] readFile(String filename) throws Exception { + return java.nio.file.Files.readAllBytes(java.nio.file.Paths.get(filename)); + } + + /** + Initializes the script with the content of the given file. + Throws if it cannot read the file. + */ + public TestScript(String filename) throws Exception{ + this.filename = filename; + setVerbosity(2); + cur.src = readFile(filename); + } + + public String getFilename(){ + return filename; + } + + public String getModuleName(){ + return moduleName; + } + + /** + Verbosity level 0 produces no debug/verbose output. Level 1 produces + some and level 2 produces more. + */ + public void setVerbosity(int level){ + outer.setVerbosity(level); + } + + public String getOutputPrefix(){ + String rc = "["+(moduleName==null ? "" : moduleName)+"]"; + if( null!=testCaseName ) rc += "["+testCaseName+"]"; + if( null!=filename ) rc += "["+filename+"]"; + return rc + " line "+ cur.lineNo; + } + + static final String[] verboseLabel = {"🔈",/*"🔉",*/"🔊","📢"}; + //! Output vals only if level<=current verbosity level. + private TestScript verboseN(int level, Object... vals){ + final int verbosity = outer.getVerbosity(); + if(verbosity>=level){ + outer.out( verboseLabel[level-1], getOutputPrefix(), " ",level,": " + ).outln(vals); + } + return this; + } + + TestScript verbose1(Object... vals){return verboseN(1,vals);} + TestScript verbose2(Object... vals){return verboseN(2,vals);} + TestScript verbose3(Object... vals){return verboseN(3,vals);} + + private void reset(){ + testCaseName = null; + cur.rewind(); + } + + void setTestCaseName(String n){ testCaseName = n; } + + /** + Returns the next line from the buffer, minus the trailing EOL. + + Returns null when all input is consumed. Throws if it reads + illegally-encoded input, e.g. (non-)characters in the range + 128-256. + */ + String getLine(){ + if( cur.pos==cur.src.length ){ + return null /* EOF */; + } + cur.putbackPos = cur.pos; + cur.putbackLineNo = cur.lineNo; + cur.sb.setLength(0); + final boolean skipLeadingWs = false; + byte b = 0, prevB = 0; + int i = cur.pos; + if(skipLeadingWs) { + /* Skip any leading spaces, including newlines. This will eliminate + blank lines. */ + for(; i < cur.src.length; ++i, prevB=b){ + b = cur.src[i]; + switch((int)b){ + case 32/*space*/: case 9/*tab*/: case 13/*CR*/: continue; + case 10/*NL*/: ++cur.lineNo; continue; + default: break; + } + break; + } + if( i==cur.src.length ){ + return null /* EOF */; + } + } + boolean doBreak = false; + final byte[] aChar = {0,0,0,0} /* multi-byte char buffer */; + int nChar = 0 /* number of bytes in the char */; + for(; i < cur.src.length && !doBreak; ++i){ + b = cur.src[i]; + switch( (int)b ){ + case 13/*CR*/: continue; + case 10/*NL*/: + ++cur.lineNo; + if(cur.sb.length()>0) doBreak = true; + // Else it's an empty string + break; + default: + /* Multi-byte chars need to be gathered up and appended at + one time. Appending individual bytes to the StringBuffer + appends their integer value. */ + nChar = 1; + switch( b & 0xF0 ){ + case 0xC0: nChar = 2; break; + case 0xE0: nChar = 3; break; + case 0xF0: nChar = 4; break; + default: + if( b > 127 ) this.toss("Invalid character (#"+(int)b+")."); + break; + } + if( 1==nChar ){ + cur.sb.append((char)b); + }else{ + for(int x = 0; x < nChar; ++x) aChar[x] = cur.src[i+x]; + cur.sb.append(new String(Arrays.copyOf(aChar, nChar), + StandardCharsets.UTF_8)); + i += nChar-1; + } + break; + } + } + cur.pos = i; + final String rv = cur.sb.toString(); + if( i==cur.src.length && rv.isEmpty() ){ + return null /* EOF */; + } + return rv; + }/*getLine()*/ + + /** + Fetches the next line then resets the cursor to its pre-call + state. consumePeeked() can be used to consume this peeked line + without having to re-parse it. + */ + String peekLine(){ + final int oldPos = cur.pos; + final int oldPB = cur.putbackPos; + final int oldPBL = cur.putbackLineNo; + final int oldLine = cur.lineNo; + try{ return getLine(); } + finally{ + cur.peekedPos = cur.pos; + cur.peekedLineNo = cur.lineNo; + cur.pos = oldPos; + cur.lineNo = oldLine; + cur.putbackPos = oldPB; + cur.putbackLineNo = oldPBL; + } + } + + /** + Only valid after calling peekLine() and before calling getLine(). + This places the cursor to the position it would have been at had + the peekLine() had been fetched with getLine(). + */ + void consumePeeked(){ + cur.pos = cur.peekedPos; + cur.lineNo = cur.peekedLineNo; + } + + /** + Restores the cursor to the position it had before the previous + call to getLine(). + */ + void putbackLine(){ + cur.pos = cur.putbackPos; + cur.lineNo = cur.putbackLineNo; + } + + private boolean checkRequiredProperties(SQLTester t, String[] props) throws SQLTesterException{ + if( true ) return false; + int nOk = 0; + for(String rp : props){ + verbose1("REQUIRED_PROPERTIES: ",rp); + switch(rp){ + case "RECURSIVE_TRIGGERS": + t.appendDbInitSql("pragma recursive_triggers=on;"); + ++nOk; + break; + case "TEMPSTORE_FILE": + /* This _assumes_ that the lib is built with SQLITE_TEMP_STORE=1 or 2, + which we just happen to know is the case */ + t.appendDbInitSql("pragma temp_store=1;"); + ++nOk; + break; + case "TEMPSTORE_MEM": + /* This _assumes_ that the lib is built with SQLITE_TEMP_STORE=1 or 2, + which we just happen to know is the case */ + t.appendDbInitSql("pragma temp_store=0;"); + ++nOk; + break; + case "AUTOVACUUM": + t.appendDbInitSql("pragma auto_vacuum=full;"); + ++nOk; + case "INCRVACUUM": + t.appendDbInitSql("pragma auto_vacuum=incremental;"); + ++nOk; + default: + break; + } + } + return props.length == nOk; + } + + private static final Pattern patternRequiredProperties = + Pattern.compile(" REQUIRED_PROPERTIES:[ \\t]*(\\S.*)\\s*$"); + private static final Pattern patternScriptModuleName = + Pattern.compile(" SCRIPT_MODULE_NAME:[ \\t]*(\\S+)\\s*$"); + private static final Pattern patternMixedModuleName = + Pattern.compile(" ((MIXED_)?MODULE_NAME):[ \\t]*(\\S+)\\s*$"); + private static final Pattern patternCommand = + Pattern.compile("^--(([a-z-]+)( .*)?)$"); + + /** + Looks for "directives." If a compatible one is found, it is + processed and this function returns. If an incompatible one is found, + a description of it is returned and processing of the test must + end immediately. + */ + private void checkForDirective( + SQLTester tester, String line + ) throws IncompatibleDirective { + if(line.startsWith("#")){ + throw new IncompatibleDirective(this, "C-preprocessor input: "+line); + }else if(line.startsWith("---")){ + new IncompatibleDirective(this, "triple-dash: "+line); + } + Matcher m = patternScriptModuleName.matcher(line); + if( m.find() ){ + moduleName = m.group(1); + return; + } + m = patternRequiredProperties.matcher(line); + if( m.find() ){ + final String rp = m.group(1); + if( ! checkRequiredProperties( tester, rp.split("\\s+") ) ){ + throw new IncompatibleDirective(this, "REQUIRED_PROPERTIES: "+rp); + } + } + m = patternMixedModuleName.matcher(line); + if( m.find() ){ + throw new IncompatibleDirective(this, m.group(1)+": "+m.group(3)); + } + if( line.contains("\n|") ){ + throw new IncompatibleDirective(this, "newline-pipe combination."); + } + return; + } + + boolean isCommandLine(String line, boolean checkForImpl){ + final Matcher m = patternCommand.matcher(line); + boolean rc = m.find(); + if( rc && checkForImpl ){ + rc = null!=CommandDispatcher.getCommandByName(m.group(2)); + } + return rc; + } + + /** + If line looks like a command, returns an argv for that command + invocation, else returns null. + */ + String[] getCommandArgv(String line){ + final Matcher m = patternCommand.matcher(line); + return m.find() ? m.group(1).trim().split("\\s+") : null; + } + + /** + Fetches lines until the next recognized command. Throws if + checkForDirective() does. Returns null if there is no input or + it's only whitespace. The returned string retains all whitespace. + + Note that "subcommands", --command-like constructs in the body + which do not match a known command name are considered to be + content, not commands. + */ + String fetchCommandBody(SQLTester tester){ + final StringBuilder sb = new StringBuilder(); + String line; + while( (null != (line = peekLine())) ){ + checkForDirective(tester, line); + if( isCommandLine(line, true) ) break; + else { + sb.append(line).append("\n"); + consumePeeked(); + } + } + line = sb.toString(); + return line.trim().isEmpty() ? null : line; + } + + private void processCommand(SQLTester t, String[] argv) throws Exception{ + verbose1("running command: ",argv[0], " ", Util.argvToString(argv)); + if(outer.getVerbosity()>1){ + final String input = t.getInputText(); + if( !input.isEmpty() ) verbose3("Input buffer = ",input); + } + CommandDispatcher.dispatch(t, this, argv); + } + + void toss(Object... msg) throws TestScriptFailed { + StringBuilder sb = new StringBuilder(); + for(Object s : msg) sb.append(s); + throw new TestScriptFailed(this, sb.toString()); + } + + /** + Runs this test script in the context of the given tester object. + */ + public boolean run(SQLTester tester) throws Exception { + reset(); + setVerbosity(tester.getVerbosity()); + String line, directive; + String[] argv; + while( null != (line = getLine()) ){ + verbose3("input line: ",line); + checkForDirective(tester, line); + argv = getCommandArgv(line); + if( null!=argv ){ + processCommand(tester, argv); + continue; + } + tester.appendInput(line,true); + } + return true; + } +} diff --git a/ext/jni/src/org/sqlite/jni/capi/ScalarFunction.java b/ext/jni/src/org/sqlite/jni/capi/ScalarFunction.java new file mode 100644 index 0000000000..95541bdcba --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/capi/ScalarFunction.java @@ -0,0 +1,33 @@ +/* +** 2023-08-25 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file is part of the JNI bindings for the sqlite3 C API. +*/ +package org.sqlite.jni.capi; + + +/** + A SQLFunction implementation for scalar functions. +*/ +public abstract class ScalarFunction implements SQLFunction { + /** + As for the xFunc() argument of the C API's + sqlite3_create_function(). If this function throws, it is + translated into an sqlite3_result_error(). + */ + public abstract void xFunc(sqlite3_context cx, sqlite3_value[] args); + + /** + Optionally override to be notified when the UDF is finalized by + SQLite. This default implementation does nothing. + */ + public void xDestroy() {} +} diff --git a/ext/jni/src/org/sqlite/jni/capi/TableColumnMetadata.java b/ext/jni/src/org/sqlite/jni/capi/TableColumnMetadata.java new file mode 100644 index 0000000000..54808cd1ca --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/capi/TableColumnMetadata.java @@ -0,0 +1,35 @@ +/* +** 2023-07-21 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file is part of the JNI bindings for the sqlite3 C API. +*/ +package org.sqlite.jni.capi; + +/** + A wrapper object for use with sqlite3_table_column_metadata(). + They are populated only via that interface. +*/ +public final class TableColumnMetadata { + final OutputPointer.Bool pNotNull = new OutputPointer.Bool(); + final OutputPointer.Bool pPrimaryKey = new OutputPointer.Bool(); + final OutputPointer.Bool pAutoinc = new OutputPointer.Bool(); + final OutputPointer.String pzCollSeq = new OutputPointer.String(); + final OutputPointer.String pzDataType = new OutputPointer.String(); + + public TableColumnMetadata(){ + } + + public String getDataType(){ return pzDataType.value; } + public String getCollation(){ return pzCollSeq.value; } + public boolean isNotNull(){ return pNotNull.value; } + public boolean isPrimaryKey(){ return pPrimaryKey.value; } + public boolean isAutoincrement(){ return pAutoinc.value; } +} diff --git a/ext/jni/src/org/sqlite/jni/capi/Tester1.java b/ext/jni/src/org/sqlite/jni/capi/Tester1.java new file mode 100644 index 0000000000..9d14c954b8 --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/capi/Tester1.java @@ -0,0 +1,2207 @@ +/* +** 2023-07-21 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains a set of tests for the sqlite3 JNI bindings. +*/ +package org.sqlite.jni.capi; +import static org.sqlite.jni.capi.CApi.*; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +/** + An annotation for Tester1 tests which we do not want to run in + reflection-driven test mode because either they are not suitable + for multi-threaded threaded mode or we have to control their execution + order. +*/ +@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME) +@java.lang.annotation.Target({java.lang.annotation.ElementType.METHOD}) +@interface ManualTest{} +/** + Annotation for Tester1 tests which mark those which must be skipped + in multi-threaded mode. +*/ +@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME) +@java.lang.annotation.Target({java.lang.annotation.ElementType.METHOD}) +@interface SingleThreadOnly{} + +/** + Annotation for Tester1 tests which must only be run if + sqlite3_jni_supports_nio() is true. +*/ +@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME) +@java.lang.annotation.Target({java.lang.annotation.ElementType.METHOD}) +@interface RequiresJniNio{} + +public class Tester1 implements Runnable { + //! True when running in multi-threaded mode. + private static boolean mtMode = false; + //! True to sleep briefly between tests. + private static boolean takeNaps = false; + //! True to shuffle the order of the tests. + private static boolean shuffle = false; + //! True to dump the list of to-run tests to stdout. + private static int listRunTests = 0; + //! True to squelch all out() and outln() output. + private static boolean quietMode = false; + //! Total number of runTests() calls. + private static int nTestRuns = 0; + //! List of test*() methods to run. + private static List testMethods = null; + //! List of exceptions collected by run() + private static final List listErrors = new ArrayList<>(); + private static final class Metrics { + //! Number of times createNewDb() (or equivalent) is invoked. + volatile int dbOpen = 0; + } + + private final Integer tId; + + Tester1(Integer id){ + tId = id; + } + + static final Metrics metrics = new Metrics(); + + public static synchronized void outln(){ + if( !quietMode ){ + System.out.println(); + } + } + + public static synchronized void outPrefix(){ + if( !quietMode ){ + System.out.print(Thread.currentThread().getName()+": "); + } + } + + public static synchronized void outln(Object val){ + if( !quietMode ){ + outPrefix(); + System.out.println(val); + } + } + + public static synchronized void out(Object val){ + if( !quietMode ){ + System.out.print(val); + } + } + + @SuppressWarnings("unchecked") + public static synchronized void out(Object... vals){ + if( !quietMode ){ + outPrefix(); + for(Object v : vals) out(v); + } + } + + @SuppressWarnings("unchecked") + public static synchronized void outln(Object... vals){ + if( !quietMode ){ + out(vals); out("\n"); + } + } + + static volatile int affirmCount = 0; + public static synchronized int affirm(Boolean v, String comment){ + ++affirmCount; + if( false ) assert( v /* prefer assert over exception if it's enabled because + the JNI layer sometimes has to suppress exceptions, + so they might be squelched on their way back to the + top. */); + if( !v ) throw new RuntimeException(comment); + return affirmCount; + } + + public static void affirm(Boolean v){ + affirm(v, "Affirmation failed."); + } + + @SingleThreadOnly /* because it's thread-agnostic */ + private void test1(){ + affirm(sqlite3_libversion_number() == SQLITE_VERSION_NUMBER); + } + + public static sqlite3 createNewDb(){ + final OutputPointer.sqlite3 out = new OutputPointer.sqlite3(); + int rc = sqlite3_open(":memory:", out); + ++metrics.dbOpen; + sqlite3 db = out.take(); + if( 0!=rc ){ + final String msg = + null==db ? sqlite3_errstr(rc) : sqlite3_errmsg(db); + sqlite3_close(db); + throw new RuntimeException("Opening db failed: "+msg); + } + affirm( null == out.get() ); + affirm( 0 != db.getNativePointer() ); + rc = sqlite3_busy_timeout(db, 2000); + affirm( 0 == rc ); + return db; + } + + public static void execSql(sqlite3 db, String[] sql){ + execSql(db, String.join("", sql)); + } + + public static int execSql(sqlite3 db, boolean throwOnError, String sql){ + OutputPointer.Int32 oTail = new OutputPointer.Int32(); + final byte[] sqlUtf8 = sql.getBytes(StandardCharsets.UTF_8); + int pos = 0, n = 1; + byte[] sqlChunk = sqlUtf8; + int rc = 0; + sqlite3_stmt stmt = null; + final OutputPointer.sqlite3_stmt outStmt = new OutputPointer.sqlite3_stmt(); + while(pos < sqlChunk.length){ + if(pos > 0){ + sqlChunk = Arrays.copyOfRange(sqlChunk, pos, + sqlChunk.length); + } + if( 0==sqlChunk.length ) break; + rc = sqlite3_prepare_v2(db, sqlChunk, outStmt, oTail); + if(throwOnError) affirm(0 == rc); + else if( 0!=rc ) break; + pos = oTail.value; + stmt = outStmt.take(); + if( null == stmt ){ + // empty statement was parsed. + continue; + } + affirm(0 != stmt.getNativePointer()); + while( SQLITE_ROW == (rc = sqlite3_step(stmt)) ){ + } + sqlite3_finalize(stmt); + affirm(0 == stmt.getNativePointer()); + if(0!=rc && SQLITE_ROW!=rc && SQLITE_DONE!=rc){ + break; + } + } + sqlite3_finalize(stmt); + if(SQLITE_ROW==rc || SQLITE_DONE==rc) rc = 0; + if( 0!=rc && throwOnError){ + throw new RuntimeException("db op failed with rc=" + +rc+": "+sqlite3_errmsg(db)); + } + return rc; + } + + public static void execSql(sqlite3 db, String sql){ + execSql(db, true, sql); + } + + public static sqlite3_stmt prepare(sqlite3 db, boolean throwOnError, String sql){ + final OutputPointer.sqlite3_stmt outStmt = new OutputPointer.sqlite3_stmt(); + int rc = sqlite3_prepare_v2(db, sql, outStmt); + if( throwOnError ){ + affirm( 0 == rc ); + } + final sqlite3_stmt rv = outStmt.take(); + affirm( null == outStmt.get() ); + if( throwOnError ){ + affirm( 0 != rv.getNativePointer() ); + } + return rv; + } + + public static sqlite3_stmt prepare(sqlite3 db, String sql){ + return prepare(db, true, sql); + } + + private void showCompileOption(){ + int i = 0; + String optName; + outln("compile options:"); + for( ; null != (optName = sqlite3_compileoption_get(i)); ++i){ + outln("\t"+optName+"\t (used="+ + sqlite3_compileoption_used(optName)+")"); + } + } + + private void testCompileOption(){ + int i = 0; + String optName; + for( ; null != (optName = sqlite3_compileoption_get(i)); ++i){ + } + affirm( i > 10 ); + affirm( null==sqlite3_compileoption_get(-1) ); + } + + private void testOpenDb1(){ + final OutputPointer.sqlite3 out = new OutputPointer.sqlite3(); + int rc = sqlite3_open(":memory:", out); + ++metrics.dbOpen; + sqlite3 db = out.get(); + affirm(0 == rc); + affirm(db.getNativePointer()!=0); + sqlite3_db_config(db, SQLITE_DBCONFIG_DEFENSIVE, 1, null) + /* This function has different mangled names in jdk8 vs jdk19, + and this call is here to ensure that the build fails + if it cannot find both names. */; + + affirm( 0==sqlite3_db_readonly(db,"main") ); + affirm( 0==sqlite3_db_readonly(db,null) ); + affirm( 0>sqlite3_db_readonly(db,"nope") ); + affirm( 0>sqlite3_db_readonly(null,null) ); + affirm( 0==sqlite3_last_insert_rowid(null) ); + + // These interrupt checks are only to make sure that the JNI binding + // has the proper exported symbol names. They don't actually test + // anything useful. + affirm( !sqlite3_is_interrupted(db) ); + sqlite3_interrupt(db); + affirm( sqlite3_is_interrupted(db) ); + sqlite3_close_v2(db); + affirm(0 == db.getNativePointer()); + } + + private void testOpenDb2(){ + final OutputPointer.sqlite3 out = new OutputPointer.sqlite3(); + int rc = sqlite3_open_v2(":memory:", out, + SQLITE_OPEN_READWRITE + | SQLITE_OPEN_CREATE, null); + ++metrics.dbOpen; + affirm(0 == rc); + sqlite3 db = out.get(); + affirm(0 != db.getNativePointer()); + sqlite3_close_v2(db); + affirm(0 == db.getNativePointer()); + } + + private void testPrepare123(){ + sqlite3 db = createNewDb(); + int rc; + final OutputPointer.sqlite3_stmt outStmt = new OutputPointer.sqlite3_stmt(); + rc = sqlite3_prepare(db, "CREATE TABLE t1(a);", outStmt); + affirm(0 == rc); + sqlite3_stmt stmt = outStmt.take(); + affirm(0 != stmt.getNativePointer()); + affirm( !sqlite3_stmt_readonly(stmt) ); + affirm( db == sqlite3_db_handle(stmt) ); + rc = sqlite3_step(stmt); + affirm(SQLITE_DONE == rc); + sqlite3_finalize(stmt); + affirm( null == sqlite3_db_handle(stmt) ); + affirm(0 == stmt.getNativePointer()); + + { /* Demonstrate how to use the "zTail" option of + sqlite3_prepare() family of functions. */ + OutputPointer.Int32 oTail = new OutputPointer.Int32(); + final byte[] sqlUtf8 = + "CREATE TABLE t2(a); INSERT INTO t2(a) VALUES(1),(2),(3)" + .getBytes(StandardCharsets.UTF_8); + int pos = 0, n = 1; + byte[] sqlChunk = sqlUtf8; + while(pos < sqlChunk.length){ + if(pos > 0){ + sqlChunk = Arrays.copyOfRange(sqlChunk, pos, sqlChunk.length); + } + //outln("SQL chunk #"+n+" length = "+sqlChunk.length+", pos = "+pos); + if( 0==sqlChunk.length ) break; + rc = sqlite3_prepare_v2(db, sqlChunk, outStmt, oTail); + affirm(0 == rc); + stmt = outStmt.get(); + pos = oTail.value; + /*outln("SQL tail pos = "+pos+". Chunk = "+ + (new String(Arrays.copyOfRange(sqlChunk,0,pos), + StandardCharsets.UTF_8)));*/ + switch(n){ + case 1: affirm(19 == pos); break; + case 2: affirm(36 == pos); break; + default: affirm( false /* can't happen */ ); + + } + ++n; + affirm(0 != stmt.getNativePointer()); + rc = sqlite3_step(stmt); + affirm(SQLITE_DONE == rc); + sqlite3_finalize(stmt); + affirm(0 == stmt.getNativePointer()); + } + } + + + rc = sqlite3_prepare_v3(db, "INSERT INTO t2(a) VALUES(1),(2),(3)", + 0, outStmt); + affirm(0 == rc); + stmt = outStmt.get(); + affirm(0 != stmt.getNativePointer()); + sqlite3_finalize(stmt); + affirm(0 == stmt.getNativePointer() ); + + affirm( 0==sqlite3_errcode(db) ); + stmt = sqlite3_prepare(db, "intentional error"); + affirm( null==stmt ); + affirm( 0!=sqlite3_errcode(db) ); + affirm( 0==sqlite3_errmsg(db).indexOf("near \"intentional\"") ); + sqlite3_finalize(stmt); + stmt = sqlite3_prepare(db, "/* empty input*/\n-- comments only"); + affirm( null==stmt ); + affirm( 0==sqlite3_errcode(db) ); + sqlite3_close_v2(db); + } + + private void testBindFetchInt(){ + sqlite3 db = createNewDb(); + execSql(db, "CREATE TABLE t(a)"); + + sqlite3_stmt stmt = prepare(db, "INSERT INTO t(a) VALUES(:a);"); + affirm(1 == sqlite3_bind_parameter_count(stmt)); + final int paramNdx = sqlite3_bind_parameter_index(stmt, ":a"); + affirm(1 == paramNdx); + affirm( ":a".equals(sqlite3_bind_parameter_name(stmt, paramNdx))); + int total1 = 0; + long rowid = -1; + int changes = sqlite3_changes(db); + int changesT = sqlite3_total_changes(db); + long changes64 = sqlite3_changes64(db); + long changesT64 = sqlite3_total_changes64(db); + int rc; + for(int i = 99; i < 102; ++i ){ + total1 += i; + rc = sqlite3_bind_int(stmt, paramNdx, i); + affirm(0 == rc); + rc = sqlite3_step(stmt); + sqlite3_reset(stmt); + affirm(SQLITE_DONE == rc); + long x = sqlite3_last_insert_rowid(db); + affirm(x > rowid); + rowid = x; + } + sqlite3_finalize(stmt); + affirm(300 == total1); + affirm(sqlite3_changes(db) > changes); + affirm(sqlite3_total_changes(db) > changesT); + affirm(sqlite3_changes64(db) > changes64); + affirm(sqlite3_total_changes64(db) > changesT64); + stmt = prepare(db, "SELECT a FROM t ORDER BY a DESC;"); + affirm( sqlite3_stmt_readonly(stmt) ); + affirm( !sqlite3_stmt_busy(stmt) ); + if( sqlite3_compileoption_used("ENABLE_COLUMN_METADATA") ){ + /* Unlike in native C code, JNI won't trigger an + UnsatisfiedLinkError until these are called (on Linux, at + least). */ + affirm("t".equals(sqlite3_column_table_name(stmt,0))); + affirm("main".equals(sqlite3_column_database_name(stmt,0))); + affirm("a".equals(sqlite3_column_origin_name(stmt,0))); + } + + int total2 = 0; + while( SQLITE_ROW == sqlite3_step(stmt) ){ + affirm( sqlite3_stmt_busy(stmt) ); + total2 += sqlite3_column_int(stmt, 0); + sqlite3_value sv = sqlite3_column_value(stmt, 0); + affirm( null != sv ); + affirm( 0 != sv.getNativePointer() ); + affirm( SQLITE_INTEGER == sqlite3_value_type(sv) ); + } + affirm( !sqlite3_stmt_busy(stmt) ); + sqlite3_finalize(stmt); + affirm(total1 == total2); + + // sqlite3_value_frombind() checks... + stmt = prepare(db, "SELECT 1, ?"); + sqlite3_bind_int(stmt, 1, 2); + rc = sqlite3_step(stmt); + affirm( SQLITE_ROW==rc ); + affirm( !sqlite3_value_frombind(sqlite3_column_value(stmt, 0)) ); + affirm( sqlite3_value_frombind(sqlite3_column_value(stmt, 1)) ); + sqlite3_finalize(stmt); + + sqlite3_close_v2(db); + affirm(0 == db.getNativePointer()); + } + + private void testBindFetchInt64(){ + try (sqlite3 db = createNewDb()){ + execSql(db, "CREATE TABLE t(a)"); + sqlite3_stmt stmt = prepare(db, "INSERT INTO t(a) VALUES(?);"); + long total1 = 0; + for(long i = 0xffffffff; i < 0xffffffff + 3; ++i ){ + total1 += i; + sqlite3_bind_int64(stmt, 1, i); + sqlite3_step(stmt); + sqlite3_reset(stmt); + } + sqlite3_finalize(stmt); + stmt = prepare(db, "SELECT a FROM t ORDER BY a DESC;"); + long total2 = 0; + while( SQLITE_ROW == sqlite3_step(stmt) ){ + total2 += sqlite3_column_int64(stmt, 0); + } + sqlite3_finalize(stmt); + affirm(total1 == total2); + //sqlite3_close_v2(db); + } + } + + private void testBindFetchDouble(){ + try (sqlite3 db = createNewDb()){ + execSql(db, "CREATE TABLE t(a)"); + sqlite3_stmt stmt = prepare(db, "INSERT INTO t(a) VALUES(?);"); + double total1 = 0; + for(double i = 1.5; i < 5.0; i = i + 1.0 ){ + total1 += i; + sqlite3_bind_double(stmt, 1, i); + sqlite3_step(stmt); + sqlite3_reset(stmt); + } + sqlite3_finalize(stmt); + stmt = prepare(db, "SELECT a FROM t ORDER BY a DESC;"); + double total2 = 0; + int counter = 0; + while( SQLITE_ROW == sqlite3_step(stmt) ){ + ++counter; + total2 += sqlite3_column_double(stmt, 0); + } + affirm(4 == counter); + sqlite3_finalize(stmt); + affirm(total2<=total1+0.01 && total2>=total1-0.01); + //sqlite3_close_v2(db); + } + } + + private void testBindFetchText(){ + sqlite3 db = createNewDb(); + execSql(db, "CREATE TABLE t(a)"); + sqlite3_stmt stmt = prepare(db, "INSERT INTO t(a) VALUES(?);"); + String[] list1 = { "hell🤩", "w😃rld", "!🤩" }; + int rc; + int n = 0; + for( String e : list1 ){ + rc = (0==n) + ? sqlite3_bind_text(stmt, 1, e) + : sqlite3_bind_text16(stmt, 1, e); + affirm(0 == rc); + rc = sqlite3_step(stmt); + affirm(SQLITE_DONE==rc); + sqlite3_reset(stmt); + } + sqlite3_finalize(stmt); + stmt = prepare(db, "SELECT a FROM t ORDER BY a DESC;"); + StringBuilder sbuf = new StringBuilder(); + n = 0; + final boolean tryNio = sqlite3_jni_supports_nio(); + while( SQLITE_ROW == sqlite3_step(stmt) ){ + final sqlite3_value sv = sqlite3_value_dup(sqlite3_column_value(stmt,0)); + final String txt = sqlite3_column_text16(stmt, 0); + sbuf.append( txt ); + affirm( txt.equals(new String( + sqlite3_column_text(stmt, 0), + StandardCharsets.UTF_8 + )) ); + affirm( txt.length() < sqlite3_value_bytes(sv) ); + affirm( txt.equals(new String( + sqlite3_value_text(sv), + StandardCharsets.UTF_8)) ); + affirm( txt.length() == sqlite3_value_bytes16(sv)/2 ); + affirm( txt.equals(sqlite3_value_text16(sv)) ); + if( tryNio ){ + java.nio.ByteBuffer bu = sqlite3_value_nio_buffer(sv); + byte ba[] = sqlite3_value_blob(sv); + affirm( ba.length == bu.capacity() ); + int i = 0; + for( byte b : ba ){ + affirm( b == bu.get(i++) ); + } + } + sqlite3_value_free(sv); + ++n; + } + sqlite3_finalize(stmt); + affirm(3 == n); + affirm("w😃rldhell🤩!🤩".contentEquals(sbuf)); + + try( sqlite3_stmt stmt2 = prepare(db, "SELECT ?, ?") ){ + rc = sqlite3_bind_text(stmt2, 1, ""); + affirm( 0==rc ); + rc = sqlite3_bind_text(stmt2, 2, (String)null); + affirm( 0==rc ); + rc = sqlite3_step(stmt2); + affirm( SQLITE_ROW==rc ); + byte[] colBa = sqlite3_column_text(stmt2, 0); + affirm( 0==colBa.length ); + colBa = sqlite3_column_text(stmt2, 1); + affirm( null==colBa ); + //sqlite3_finalize(stmt); + } + + if(true){ + sqlite3_close_v2(db); + }else{ + // Let the Object.finalize() override deal with it. + } + } + + private void testBindFetchBlob(){ + sqlite3 db = createNewDb(); + execSql(db, "CREATE TABLE t(a)"); + sqlite3_stmt stmt = prepare(db, "INSERT INTO t(a) VALUES(?);"); + byte[] list1 = { 0x32, 0x33, 0x34 }; + int rc = sqlite3_bind_blob(stmt, 1, list1); + affirm( 0==rc ); + rc = sqlite3_step(stmt); + affirm(SQLITE_DONE == rc); + sqlite3_finalize(stmt); + stmt = prepare(db, "SELECT a FROM t ORDER BY a DESC;"); + int n = 0; + int total = 0; + while( SQLITE_ROW == sqlite3_step(stmt) ){ + byte[] blob = sqlite3_column_blob(stmt, 0); + affirm(3 == blob.length); + int i = 0; + for(byte b : blob){ + affirm(b == list1[i++]); + total += b; + } + ++n; + } + sqlite3_finalize(stmt); + affirm(1 == n); + affirm(total == 0x32 + 0x33 + 0x34); + sqlite3_close_v2(db); + } + + @RequiresJniNio + private void testBindByteBuffer(){ + /* TODO: these tests need to be much more extensive to check the + begin/end range handling. */ + + java.nio.ByteBuffer zeroCheck = + java.nio.ByteBuffer.allocateDirect(0); + affirm( null != zeroCheck ); + zeroCheck = null; + sqlite3 db = createNewDb(); + execSql(db, "CREATE TABLE t(a)"); + + final java.nio.ByteBuffer buf = java.nio.ByteBuffer.allocateDirect(10); + buf.put((byte)0x31)/*note that we'll skip this one*/ + .put((byte)0x32) + .put((byte)0x33) + .put((byte)0x34) + .put((byte)0x35)/*we'll skip this one too*/; + + final int expectTotal = buf.get(1) + buf.get(2) + buf.get(3); + sqlite3_stmt stmt = prepare(db, "INSERT INTO t(a) VALUES(?);"); + affirm( SQLITE_ERROR == sqlite3_bind_blob(stmt, 1, buf, -1, 0), + "Buffer offset may not be negative." ); + affirm( 0 == sqlite3_bind_blob(stmt, 1, buf, 1, 3) ); + affirm( SQLITE_DONE == sqlite3_step(stmt) ); + sqlite3_finalize(stmt); + stmt = prepare(db, "SELECT a FROM t;"); + int total = 0; + affirm( SQLITE_ROW == sqlite3_step(stmt) ); + byte blob[] = sqlite3_column_blob(stmt, 0); + java.nio.ByteBuffer nioBlob = + sqlite3_column_nio_buffer(stmt, 0); + affirm(3 == blob.length); + affirm(blob.length == nioBlob.capacity()); + affirm(blob.length == nioBlob.limit()); + int i = 0; + for(byte b : blob){ + affirm( i<=3 ); + affirm(b == buf.get(1 + i)); + affirm(b == nioBlob.get(i)); + ++i; + total += b; + } + affirm( SQLITE_DONE == sqlite3_step(stmt) ); + sqlite3_finalize(stmt); + affirm(total == expectTotal); + + SQLFunction func = + new ScalarFunction(){ + public void xFunc(sqlite3_context cx, sqlite3_value[] args){ + sqlite3_result_blob(cx, buf, 1, 3); + } + }; + + affirm( 0 == sqlite3_create_function(db, "myfunc", -1, SQLITE_UTF8, func) ); + stmt = prepare(db, "SELECT myfunc()"); + affirm( SQLITE_ROW == sqlite3_step(stmt) ); + blob = sqlite3_column_blob(stmt, 0); + affirm(3 == blob.length); + i = 0; + total = 0; + for(byte b : blob){ + affirm( i<=3 ); + affirm(b == buf.get(1 + i++)); + total += b; + } + affirm( SQLITE_DONE == sqlite3_step(stmt) ); + sqlite3_finalize(stmt); + affirm(total == expectTotal); + + sqlite3_close_v2(db); + } + + private void testSql(){ + sqlite3 db = createNewDb(); + sqlite3_stmt stmt = prepare(db, "SELECT 1"); + affirm( "SELECT 1".equals(sqlite3_sql(stmt)) ); + sqlite3_finalize(stmt); + stmt = prepare(db, "SELECT ?"); + sqlite3_bind_text(stmt, 1, "hell😃"); + final String expect = "SELECT 'hell😃'"; + affirm( expect.equals(sqlite3_expanded_sql(stmt)) ); + String n = sqlite3_normalized_sql(stmt); + affirm( null==n || "SELECT?;".equals(n) ); + sqlite3_finalize(stmt); + sqlite3_close(db); + } + + private void testCollation(){ + final sqlite3 db = createNewDb(); + execSql(db, "CREATE TABLE t(a); INSERT INTO t(a) VALUES('a'),('b'),('c')"); + final ValueHolder xDestroyCalled = new ValueHolder<>(0); + final CollationCallback myCollation = new CollationCallback() { + private final String myState = + "this is local state. There is much like it, but this is mine."; + @Override + // Reverse-sorts its inputs... + public int call(byte[] lhs, byte[] rhs){ + int len = lhs.length > rhs.length ? rhs.length : lhs.length; + int c = 0, i = 0; + for(i = 0; i < len; ++i){ + c = lhs[i] - rhs[i]; + if(0 != c) break; + } + if(0==c){ + if(i < lhs.length) c = 1; + else if(i < rhs.length) c = -1; + } + return -c; + } + @Override + public void xDestroy() { + // Just demonstrates that xDestroy is called. + ++xDestroyCalled.value; + } + }; + final CollationNeededCallback collLoader = new CollationNeededCallback(){ + @Override + public void call(sqlite3 dbArg, int eTextRep, String collationName){ + affirm(dbArg == db/* as opposed to a temporary object*/); + sqlite3_create_collation(dbArg, "reversi", eTextRep, myCollation); + } + }; + int rc = sqlite3_collation_needed(db, collLoader); + affirm( 0 == rc ); + rc = sqlite3_collation_needed(db, collLoader); + affirm( 0 == rc /* Installing the same object again is a no-op */); + sqlite3_stmt stmt = prepare(db, "SELECT a FROM t ORDER BY a COLLATE reversi"); + int counter = 0; + while( SQLITE_ROW == sqlite3_step(stmt) ){ + final String val = sqlite3_column_text16(stmt, 0); + ++counter; + //outln("REVERSI'd row#"+counter+": "+val); + switch(counter){ + case 1: affirm("c".equals(val)); break; + case 2: affirm("b".equals(val)); break; + case 3: affirm("a".equals(val)); break; + } + } + affirm(3 == counter); + sqlite3_finalize(stmt); + stmt = prepare(db, "SELECT a FROM t ORDER BY a"); + counter = 0; + while( SQLITE_ROW == sqlite3_step(stmt) ){ + final String val = sqlite3_column_text16(stmt, 0); + ++counter; + //outln("Non-REVERSI'd row#"+counter+": "+val); + switch(counter){ + case 3: affirm("c".equals(val)); break; + case 2: affirm("b".equals(val)); break; + case 1: affirm("a".equals(val)); break; + } + } + affirm(3 == counter); + sqlite3_finalize(stmt); + affirm( 0 == xDestroyCalled.value ); + rc = sqlite3_collation_needed(db, null); + affirm( 0 == rc ); + sqlite3_close_v2(db); + affirm( 0 == db.getNativePointer() ); + affirm( 1 == xDestroyCalled.value ); + } + + @SingleThreadOnly /* because it's thread-agnostic */ + private void testToUtf8(){ + /** + https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html + + Let's ensure that we can convert to standard UTF-8 in Java code + (noting that the JNI native API has no way to do this). + */ + final byte[] ba = "a \0 b".getBytes(StandardCharsets.UTF_8); + affirm( 5 == ba.length /* as opposed to 6 in modified utf-8 */); + } + + private void testStatus(){ + final OutputPointer.Int64 cur64 = new OutputPointer.Int64(); + final OutputPointer.Int64 high64 = new OutputPointer.Int64(); + final OutputPointer.Int32 cur32 = new OutputPointer.Int32(); + final OutputPointer.Int32 high32 = new OutputPointer.Int32(); + final sqlite3 db = createNewDb(); + execSql(db, "create table t(a); insert into t values(1),(2),(3)"); + + int rc = sqlite3_status(SQLITE_STATUS_MEMORY_USED, cur32, high32, false); + affirm( 0 == rc ); + affirm( cur32.value > 0 ); + affirm( high32.value >= cur32.value ); + + rc = sqlite3_status64(SQLITE_STATUS_MEMORY_USED, cur64, high64, false); + affirm( 0 == rc ); + affirm( cur64.value > 0 ); + affirm( high64.value >= cur64.value ); + + cur32.value = 0; + high32.value = 1; + rc = sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, cur32, high32, false); + affirm( 0 == rc ); + affirm( cur32.value > 0 ); + affirm( high32.value == 0 /* always 0 for SCHEMA_USED */ ); + + sqlite3_close_v2(db); + } + + private void testUdf1(){ + final sqlite3 db = createNewDb(); + // These ValueHolders are just to confirm that the func did what we want... + final ValueHolder xDestroyCalled = new ValueHolder<>(false); + final ValueHolder xFuncAccum = new ValueHolder<>(0); + final ValueHolder neverEverDoThisInClientCode = new ValueHolder<>(null); + final ValueHolder neverEverDoThisInClientCode2 = new ValueHolder<>(null); + + // Create an SQLFunction instance using one of its 3 subclasses: + // Scalar, Aggregate, or Window: + SQLFunction func = + // Each of the 3 subclasses requires a different set of + // functions, all of which must be implemented. Anonymous + // classes are a convenient way to implement these. + new ScalarFunction(){ + public void xFunc(sqlite3_context cx, sqlite3_value[] args){ + affirm(db == sqlite3_context_db_handle(cx)); + if( null==neverEverDoThisInClientCode.value ){ + /* !!!NEVER!!! hold a reference to an sqlite3_value or + sqlite3_context object like this in client code! They + are ONLY legal for the duration of their single + call. We do it here ONLY to test that the defenses + against clients doing this are working. */ + neverEverDoThisInClientCode2.value = cx; + neverEverDoThisInClientCode.value = args; + } + int result = 0; + for( sqlite3_value v : args ) result += sqlite3_value_int(v); + xFuncAccum.value += result;// just for post-run testing + sqlite3_result_int(cx, result); + } + /* OPTIONALLY override xDestroy... */ + public void xDestroy(){ + xDestroyCalled.value = true; + } + }; + + // Register and use the function... + int rc = sqlite3_create_function(db, "myfunc", -1, SQLITE_UTF8, func); + affirm(0 == rc); + affirm(0 == xFuncAccum.value); + final sqlite3_stmt stmt = prepare(db, "SELECT myfunc(1,2,3)"); + int n = 0; + while( SQLITE_ROW == sqlite3_step(stmt) ){ + affirm( 6 == sqlite3_column_int(stmt, 0) ); + ++n; + } + sqlite3_finalize(stmt); + affirm(1 == n); + affirm(6 == xFuncAccum.value); + affirm( !xDestroyCalled.value ); + affirm( null!=neverEverDoThisInClientCode.value ); + affirm( null!=neverEverDoThisInClientCode2.value ); + affirm( 0 xFuncAccum = new ValueHolder<>(0); + + SQLFunction funcAgg = new AggregateFunction(){ + @Override public void xStep(sqlite3_context cx, sqlite3_value[] args){ + /** Throwing from here should emit loud noise on stdout or stderr + but the exception is suppressed because we have no way to inform + sqlite about it from these callbacks. */ + //throw new RuntimeException("Throwing from an xStep"); + } + @Override public void xFinal(sqlite3_context cx){ + throw new RuntimeException("Throwing from an xFinal"); + } + }; + int rc = sqlite3_create_function(db, "myagg", 1, SQLITE_UTF8, funcAgg); + affirm(0 == rc); + affirm(0 == xFuncAccum.value); + sqlite3_stmt stmt = prepare(db, "SELECT myagg(1)"); + rc = sqlite3_step(stmt); + sqlite3_finalize(stmt); + affirm( 0 != rc ); + affirm( sqlite3_errmsg(db).indexOf("an xFinal") > 0 ); + + SQLFunction funcSc = new ScalarFunction(){ + @Override public void xFunc(sqlite3_context cx, sqlite3_value[] args){ + throw new RuntimeException("Throwing from an xFunc"); + } + }; + rc = sqlite3_create_function(db, "mysca", 0, SQLITE_UTF8, funcSc); + affirm(0 == rc); + affirm(0 == xFuncAccum.value); + stmt = prepare(db, "SELECT mysca()"); + rc = sqlite3_step(stmt); + sqlite3_finalize(stmt); + affirm( 0 != rc ); + affirm( sqlite3_errmsg(db).indexOf("an xFunc") > 0 ); + rc = sqlite3_create_function(db, "mysca", 1, -1, funcSc); + affirm( SQLITE_FORMAT==rc, "invalid encoding value." ); + sqlite3_close_v2(db); + } + + @SingleThreadOnly + private void testUdfJavaObject(){ + affirm( !mtMode ); + final sqlite3 db = createNewDb(); + final ValueHolder testResult = new ValueHolder<>(db); + final ValueHolder boundObj = new ValueHolder<>(42); + final SQLFunction func = new ScalarFunction(){ + public void xFunc(sqlite3_context cx, sqlite3_value args[]){ + sqlite3_result_java_object(cx, testResult.value); + affirm( sqlite3_value_java_object(args[0]) == boundObj ); + } + }; + int rc = sqlite3_create_function(db, "myfunc", -1, SQLITE_UTF8, func); + affirm(0 == rc); + sqlite3_stmt stmt = prepare(db, "select myfunc(?)"); + affirm( 0 != stmt.getNativePointer() ); + affirm( testResult.value == db ); + rc = sqlite3_bind_java_object(stmt, 1, boundObj); + affirm( 0==rc ); + int n = 0; + if( SQLITE_ROW == sqlite3_step(stmt) ){ + affirm( testResult.value == sqlite3_column_java_object(stmt, 0) ); + affirm( testResult.value == sqlite3_column_java_object(stmt, 0, sqlite3.class) ); + affirm( null == sqlite3_column_java_object(stmt, 0, sqlite3_stmt.class) ); + affirm( null == sqlite3_column_java_object(stmt,1) ); + final sqlite3_value v = sqlite3_column_value(stmt, 0); + affirm( testResult.value == sqlite3_value_java_object(v) ); + affirm( testResult.value == sqlite3_value_java_object(v, sqlite3.class) ); + affirm( testResult.value == + sqlite3_value_java_object(v, testResult.value.getClass()) ); + affirm( testResult.value == sqlite3_value_java_object(v, Object.class) ); + affirm( null == sqlite3_value_java_object(v, String.class) ); + ++n; + } + sqlite3_finalize(stmt); + affirm( 1 == n ); + affirm( 0==sqlite3_db_release_memory(db) ); + sqlite3_close_v2(db); + } + + private void testUdfAggregate(){ + final sqlite3 db = createNewDb(); + final ValueHolder xFinalNull = + // To confirm that xFinal() is called with no aggregate state + // when the corresponding result set is empty. + new ValueHolder<>(false); + final ValueHolder neverEverDoThisInClientCode = new ValueHolder<>(null); + final ValueHolder neverEverDoThisInClientCode2 = new ValueHolder<>(null); + SQLFunction func = new AggregateFunction(){ + @Override + public void xStep(sqlite3_context cx, sqlite3_value[] args){ + if( null==neverEverDoThisInClientCode.value ){ + /* !!!NEVER!!! hold a reference to an sqlite3_value or + sqlite3_context object like this in client code! They + are ONLY legal for the duration of their single + call. We do it here ONLY to test that the defenses + against clients doing this are working. */ + neverEverDoThisInClientCode.value = args; + } + final ValueHolder agg = this.getAggregateState(cx, 0); + agg.value += sqlite3_value_int(args[0]); + affirm( agg == this.getAggregateState(cx, 0) ); + } + @Override + public void xFinal(sqlite3_context cx){ + if( null==neverEverDoThisInClientCode2.value ){ + neverEverDoThisInClientCode2.value = cx; + } + final Integer v = this.takeAggregateState(cx); + if(null == v){ + xFinalNull.value = true; + sqlite3_result_null(cx); + }else{ + sqlite3_result_int(cx, v); + } + } + }; + execSql(db, "CREATE TABLE t(a); INSERT INTO t(a) VALUES(1),(2),(3)"); + int rc = sqlite3_create_function(db, "myfunc", 1, SQLITE_UTF8, func); + affirm(0 == rc); + sqlite3_stmt stmt = prepare(db, "select myfunc(a), myfunc(a+10) from t"); + affirm( 0==sqlite3_stmt_status(stmt, SQLITE_STMTSTATUS_RUN, false) ); + int n = 0; + if( SQLITE_ROW == sqlite3_step(stmt) ){ + int v = sqlite3_column_int(stmt, 0); + affirm( 6 == v ); + int v2 = sqlite3_column_int(stmt, 1); + affirm( 30+v == v2 ); + ++n; + } + affirm( 1==n ); + affirm(!xFinalNull.value); + affirm( null!=neverEverDoThisInClientCode.value ); + affirm( null!=neverEverDoThisInClientCode2.value ); + affirm( 0(){ + + private void xStepInverse(sqlite3_context cx, int v){ + this.getAggregateState(cx,0).value += v; + } + @Override public void xStep(sqlite3_context cx, sqlite3_value[] args){ + this.xStepInverse(cx, sqlite3_value_int(args[0])); + } + @Override public void xInverse(sqlite3_context cx, sqlite3_value[] args){ + this.xStepInverse(cx, -sqlite3_value_int(args[0])); + } + + private void xFinalValue(sqlite3_context cx, Integer v){ + if(null == v) sqlite3_result_null(cx); + else sqlite3_result_int(cx, v); + } + @Override public void xFinal(sqlite3_context cx){ + xFinalValue(cx, this.takeAggregateState(cx)); + } + @Override public void xValue(sqlite3_context cx){ + xFinalValue(cx, this.getAggregateState(cx,null).value); + } + }; + int rc = sqlite3_create_function(db, "winsumint", 1, SQLITE_UTF8, func); + affirm( 0 == rc ); + execSql(db, new String[] { + "CREATE TEMP TABLE twin(x, y); INSERT INTO twin VALUES", + "('a', 4),('b', 5),('c', 3),('d', 8),('e', 1)" + }); + final sqlite3_stmt stmt = prepare(db, + "SELECT x, winsumint(y) OVER ("+ + "ORDER BY x ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING"+ + ") AS sum_y "+ + "FROM twin ORDER BY x;"); + int n = 0; + while( SQLITE_ROW == sqlite3_step(stmt) ){ + final String s = sqlite3_column_text16(stmt, 0); + final int i = sqlite3_column_int(stmt, 1); + switch(++n){ + case 1: affirm( "a".equals(s) && 9==i ); break; + case 2: affirm( "b".equals(s) && 12==i ); break; + case 3: affirm( "c".equals(s) && 16==i ); break; + case 4: affirm( "d".equals(s) && 12==i ); break; + case 5: affirm( "e".equals(s) && 9==i ); break; + default: affirm( false /* cannot happen */ ); + } + } + sqlite3_finalize(stmt); + affirm( 5 == n ); + sqlite3_close_v2(db); + } + + private void listBoundMethods(){ + if(false){ + final java.lang.reflect.Field[] declaredFields = + CApi.class.getDeclaredFields(); + outln("Bound constants:\n"); + for(java.lang.reflect.Field field : declaredFields) { + if(java.lang.reflect.Modifier.isStatic(field.getModifiers())) { + outln("\t",field.getName()); + } + } + } + final java.lang.reflect.Method[] declaredMethods = + CApi.class.getDeclaredMethods(); + final java.util.List funcList = new java.util.ArrayList<>(); + for(java.lang.reflect.Method m : declaredMethods){ + if((m.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0){ + final String name = m.getName(); + if(name.startsWith("sqlite3_")){ + funcList.add(name); + } + } + } + int count = 0; + java.util.Collections.sort(funcList); + for(String n : funcList){ + ++count; + outln("\t",n,"()"); + } + outln(count," functions named sqlite3_*."); + } + + private void testTrace(){ + final sqlite3 db = createNewDb(); + final ValueHolder counter = new ValueHolder<>(0); + /* Ensure that characters outside of the UTF BMP survive the trip + from Java to sqlite3 and back to Java. (At no small efficiency + penalty.) */ + final String nonBmpChar = "😃"; + int rc = sqlite3_trace_v2( + db, SQLITE_TRACE_STMT | SQLITE_TRACE_PROFILE + | SQLITE_TRACE_ROW | SQLITE_TRACE_CLOSE, + new TraceV2Callback(){ + @Override public int call(int traceFlag, Object pNative, Object x){ + ++counter.value; + //outln("TRACE "+traceFlag+" pNative = "+pNative.getClass().getName()); + switch(traceFlag){ + case SQLITE_TRACE_STMT: + affirm(pNative instanceof sqlite3_stmt); + //outln("TRACE_STMT sql = "+x); + affirm(x instanceof String); + affirm( ((String)x).indexOf(nonBmpChar) > 0 ); + break; + case SQLITE_TRACE_PROFILE: + affirm(pNative instanceof sqlite3_stmt); + affirm(x instanceof Long); + //outln("TRACE_PROFILE time = "+x); + break; + case SQLITE_TRACE_ROW: + affirm(pNative instanceof sqlite3_stmt); + affirm(null == x); + //outln("TRACE_ROW = "+sqlite3_column_text16((sqlite3_stmt)pNative, 0)); + break; + case SQLITE_TRACE_CLOSE: + affirm(pNative instanceof sqlite3); + affirm(null == x); + break; + default: + affirm(false /*cannot happen*/); + break; + } + return 0; + } + }); + affirm( 0==rc ); + execSql(db, "SELECT coalesce(null,null,'"+nonBmpChar+"'); "+ + "SELECT 'w"+nonBmpChar+"orld'"); + affirm( 6 == counter.value ); + sqlite3_close_v2(db); + affirm( 7 == counter.value ); + } + + @SingleThreadOnly /* because threads inherently break this test */ + private static void testBusy(){ + final String dbName = "_busy-handler.db"; + try{ + final OutputPointer.sqlite3 outDb = new OutputPointer.sqlite3(); + final OutputPointer.sqlite3_stmt outStmt = new OutputPointer.sqlite3_stmt(); + + int rc = sqlite3_open(dbName, outDb); + ++metrics.dbOpen; + affirm( 0 == rc ); + final sqlite3 db1 = outDb.get(); + execSql(db1, "CREATE TABLE IF NOT EXISTS t(a)"); + rc = sqlite3_open(dbName, outDb); + ++metrics.dbOpen; + affirm( 0 == rc ); + affirm( outDb.get() != db1 ); + final sqlite3 db2 = outDb.get(); + + affirm( "main".equals( sqlite3_db_name(db1, 0) ) ); + rc = sqlite3_db_config(db1, SQLITE_DBCONFIG_MAINDBNAME, "foo"); + affirm( sqlite3_db_filename(db1, "foo").endsWith(dbName) ); + affirm( "foo".equals( sqlite3_db_name(db1, 0) ) ); + affirm( SQLITE_MISUSE == sqlite3_db_config(db1, 0, 0, null) ); + + final ValueHolder xBusyCalled = new ValueHolder<>(0); + BusyHandlerCallback handler = new BusyHandlerCallback(){ + @Override public int call(int n){ + //outln("busy handler #"+n); + return n > 2 ? 0 : ++xBusyCalled.value; + } + }; + rc = sqlite3_busy_handler(db2, handler); + affirm(0 == rc); + + // Force a locked condition... + execSql(db1, "BEGIN EXCLUSIVE"); + rc = sqlite3_prepare_v2(db2, "SELECT * from t", outStmt); + affirm( SQLITE_BUSY == rc); + affirm( null == outStmt.get() ); + affirm( 3 == xBusyCalled.value ); + sqlite3_close_v2(db1); + sqlite3_close_v2(db2); + }finally{ + try{(new java.io.File(dbName)).delete();} + catch(Exception e){/* ignore */} + } + } + + private void testProgress(){ + final sqlite3 db = createNewDb(); + final ValueHolder counter = new ValueHolder<>(0); + sqlite3_progress_handler(db, 1, new ProgressHandlerCallback(){ + @Override public int call(){ + ++counter.value; + return 0; + } + }); + execSql(db, "SELECT 1; SELECT 2;"); + affirm( counter.value > 0 ); + int nOld = counter.value; + sqlite3_progress_handler(db, 0, null); + execSql(db, "SELECT 1; SELECT 2;"); + affirm( nOld == counter.value ); + sqlite3_close_v2(db); + } + + private void testCommitHook(){ + final sqlite3 db = createNewDb(); + sqlite3_extended_result_codes(db, true); + final ValueHolder counter = new ValueHolder<>(0); + final ValueHolder hookResult = new ValueHolder<>(0); + final CommitHookCallback theHook = new CommitHookCallback(){ + @Override public int call(){ + ++counter.value; + return hookResult.value; + } + }; + CommitHookCallback oldHook = sqlite3_commit_hook(db, theHook); + affirm( null == oldHook ); + execSql(db, "CREATE TABLE t(a); INSERT INTO t(a) VALUES('a'),('b'),('c')"); + affirm( 2 == counter.value ); + execSql(db, "BEGIN; SELECT 1; SELECT 2; COMMIT;"); + affirm( 2 == counter.value /* NOT invoked if no changes are made */ ); + execSql(db, "BEGIN; update t set a='d' where a='c'; COMMIT;"); + affirm( 3 == counter.value ); + oldHook = sqlite3_commit_hook(db, theHook); + affirm( theHook == oldHook ); + execSql(db, "BEGIN; update t set a='e' where a='d'; COMMIT;"); + affirm( 4 == counter.value ); + oldHook = sqlite3_commit_hook(db, null); + affirm( theHook == oldHook ); + execSql(db, "BEGIN; update t set a='f' where a='e'; COMMIT;"); + affirm( 4 == counter.value ); + oldHook = sqlite3_commit_hook(db, null); + affirm( null == oldHook ); + execSql(db, "BEGIN; update t set a='g' where a='f'; COMMIT;"); + affirm( 4 == counter.value ); + + final CommitHookCallback newHook = new CommitHookCallback(){ + @Override public int call(){return 0;} + }; + oldHook = sqlite3_commit_hook(db, newHook); + affirm( null == oldHook ); + execSql(db, "BEGIN; update t set a='h' where a='g'; COMMIT;"); + affirm( 4 == counter.value ); + oldHook = sqlite3_commit_hook(db, theHook); + affirm( newHook == oldHook ); + execSql(db, "BEGIN; update t set a='i' where a='h'; COMMIT;"); + affirm( 5 == counter.value ); + hookResult.value = SQLITE_ERROR; + int rc = execSql(db, false, "BEGIN; update t set a='j' where a='i'; COMMIT;"); + affirm( SQLITE_CONSTRAINT_COMMITHOOK == rc ); + affirm( 6 == counter.value ); + sqlite3_close_v2(db); + } + + private void testUpdateHook(){ + final sqlite3 db = createNewDb(); + final ValueHolder counter = new ValueHolder<>(0); + final ValueHolder expectedOp = new ValueHolder<>(0); + final UpdateHookCallback theHook = new UpdateHookCallback(){ + @Override + public void call(int opId, String dbName, String tableName, long rowId){ + ++counter.value; + if( 0!=expectedOp.value ){ + affirm( expectedOp.value == opId ); + } + } + }; + UpdateHookCallback oldHook = sqlite3_update_hook(db, theHook); + affirm( null == oldHook ); + expectedOp.value = SQLITE_INSERT; + execSql(db, "CREATE TABLE t(a); INSERT INTO t(a) VALUES('a'),('b'),('c')"); + affirm( 3 == counter.value ); + expectedOp.value = SQLITE_UPDATE; + execSql(db, "update t set a='d' where a='c';"); + affirm( 4 == counter.value ); + oldHook = sqlite3_update_hook(db, theHook); + affirm( theHook == oldHook ); + expectedOp.value = SQLITE_DELETE; + execSql(db, "DELETE FROM t where a='d'"); + affirm( 5 == counter.value ); + oldHook = sqlite3_update_hook(db, null); + affirm( theHook == oldHook ); + execSql(db, "update t set a='e' where a='b';"); + affirm( 5 == counter.value ); + oldHook = sqlite3_update_hook(db, null); + affirm( null == oldHook ); + + final UpdateHookCallback newHook = new UpdateHookCallback(){ + @Override public void call(int opId, String dbName, String tableName, long rowId){ + } + }; + oldHook = sqlite3_update_hook(db, newHook); + affirm( null == oldHook ); + execSql(db, "update t set a='h' where a='a'"); + affirm( 5 == counter.value ); + oldHook = sqlite3_update_hook(db, theHook); + affirm( newHook == oldHook ); + expectedOp.value = SQLITE_UPDATE; + execSql(db, "update t set a='i' where a='h'"); + affirm( 6 == counter.value ); + sqlite3_close_v2(db); + } + + /** + This test is functionally identical to testUpdateHook(), only with a + different callback type. + */ + private void testPreUpdateHook(){ + if( !sqlite3_compileoption_used("ENABLE_PREUPDATE_HOOK") ){ + //outln("Skipping testPreUpdateHook(): no pre-update hook support."); + return; + } + final sqlite3 db = createNewDb(); + final ValueHolder counter = new ValueHolder<>(0); + final ValueHolder expectedOp = new ValueHolder<>(0); + final PreupdateHookCallback theHook = new PreupdateHookCallback(){ + @Override + public void call(sqlite3 db, int opId, String dbName, String dbTable, + long iKey1, long iKey2 ){ + ++counter.value; + switch( opId ){ + case SQLITE_UPDATE: + affirm( 0 < sqlite3_preupdate_count(db) ); + affirm( null != sqlite3_preupdate_new(db, 0) ); + affirm( null != sqlite3_preupdate_old(db, 0) ); + break; + case SQLITE_INSERT: + affirm( null != sqlite3_preupdate_new(db, 0) ); + break; + case SQLITE_DELETE: + affirm( null != sqlite3_preupdate_old(db, 0) ); + break; + default: + break; + } + if( 0!=expectedOp.value ){ + affirm( expectedOp.value == opId ); + } + } + }; + PreupdateHookCallback oldHook = sqlite3_preupdate_hook(db, theHook); + affirm( null == oldHook ); + expectedOp.value = SQLITE_INSERT; + execSql(db, "CREATE TABLE t(a); INSERT INTO t(a) VALUES('a'),('b'),('c')"); + affirm( 3 == counter.value ); + expectedOp.value = SQLITE_UPDATE; + execSql(db, "update t set a='d' where a='c';"); + affirm( 4 == counter.value ); + oldHook = sqlite3_preupdate_hook(db, theHook); + affirm( theHook == oldHook ); + expectedOp.value = SQLITE_DELETE; + execSql(db, "DELETE FROM t where a='d'"); + affirm( 5 == counter.value ); + oldHook = sqlite3_preupdate_hook(db, null); + affirm( theHook == oldHook ); + execSql(db, "update t set a='e' where a='b';"); + affirm( 5 == counter.value ); + oldHook = sqlite3_preupdate_hook(db, null); + affirm( null == oldHook ); + + final PreupdateHookCallback newHook = new PreupdateHookCallback(){ + @Override + public void call(sqlite3 db, int opId, String dbName, + String tableName, long iKey1, long iKey2){ + } + }; + oldHook = sqlite3_preupdate_hook(db, newHook); + affirm( null == oldHook ); + execSql(db, "update t set a='h' where a='a'"); + affirm( 5 == counter.value ); + oldHook = sqlite3_preupdate_hook(db, theHook); + affirm( newHook == oldHook ); + expectedOp.value = SQLITE_UPDATE; + execSql(db, "update t set a='i' where a='h'"); + affirm( 6 == counter.value ); + + sqlite3_close_v2(db); + } + + private void testRollbackHook(){ + final sqlite3 db = createNewDb(); + final ValueHolder counter = new ValueHolder<>(0); + final RollbackHookCallback theHook = new RollbackHookCallback(){ + @Override public void call(){ + ++counter.value; + } + }; + RollbackHookCallback oldHook = sqlite3_rollback_hook(db, theHook); + affirm( null == oldHook ); + execSql(db, "CREATE TABLE t(a); INSERT INTO t(a) VALUES('a'),('b'),('c')"); + affirm( 0 == counter.value ); + execSql(db, false, "BEGIN; SELECT 1; SELECT 2; ROLLBACK;"); + affirm( 1 == counter.value /* contra to commit hook, is invoked if no changes are made */ ); + + final RollbackHookCallback newHook = new RollbackHookCallback(){ + @Override public void call(){return;} + }; + oldHook = sqlite3_rollback_hook(db, newHook); + affirm( theHook == oldHook ); + execSql(db, false, "BEGIN; SELECT 1; ROLLBACK;"); + affirm( 1 == counter.value ); + oldHook = sqlite3_rollback_hook(db, theHook); + affirm( newHook == oldHook ); + execSql(db, false, "BEGIN; SELECT 1; ROLLBACK;"); + affirm( 2 == counter.value ); + int rc = execSql(db, false, "BEGIN; SELECT 1; ROLLBACK;"); + affirm( 0 == rc ); + affirm( 3 == counter.value ); + sqlite3_close_v2(db); + } + + /** + If FTS5 is available, runs FTS5 tests, else returns with no side + effects. If it is available but loading of the FTS5 bits fails, + it throws. + */ + @SuppressWarnings("unchecked") + @SingleThreadOnly /* because the Fts5 parts are not yet known to be + thread-safe */ + private void testFts5() throws Exception { + if( !sqlite3_compileoption_used("ENABLE_FTS5") ){ + //outln("SQLITE_ENABLE_FTS5 is not set. Skipping FTS5 tests."); + return; + } + Exception err = null; + try { + Class t = Class.forName("org.sqlite.jni.fts5.TesterFts5"); + java.lang.reflect.Constructor ctor = t.getConstructor(); + ctor.setAccessible(true); + final long timeStart = System.currentTimeMillis(); + ctor.newInstance() /* will run all tests */; + final long timeEnd = System.currentTimeMillis(); + outln("FTS5 Tests done in ",(timeEnd - timeStart),"ms"); + }catch(ClassNotFoundException e){ + outln("FTS5 classes not loaded."); + err = e; + }catch(NoSuchMethodException e){ + outln("FTS5 tester ctor not found."); + err = e; + }catch(Exception e){ + outln("Instantiation of FTS5 tester threw."); + err = e; + } + if( null != err ){ + outln("Exception: "+err); + err.printStackTrace(); + throw err; + } + } + + private void testAuthorizer(){ + final sqlite3 db = createNewDb(); + final ValueHolder counter = new ValueHolder<>(0); + final ValueHolder authRc = new ValueHolder<>(0); + final AuthorizerCallback auth = new AuthorizerCallback(){ + public int call(int op, String s0, String s1, String s2, String s3){ + ++counter.value; + //outln("xAuth(): "+s0+" "+s1+" "+s2+" "+s3); + return authRc.value; + } + }; + execSql(db, "CREATE TABLE t(a); INSERT INTO t(a) VALUES('a'),('b'),('c')"); + sqlite3_set_authorizer(db, auth); + execSql(db, "UPDATE t SET a=1"); + affirm( 1 == counter.value ); + authRc.value = SQLITE_DENY; + int rc = execSql(db, false, "UPDATE t SET a=2"); + affirm( SQLITE_AUTH==rc ); + sqlite3_set_authorizer(db, null); + rc = execSql(db, false, "UPDATE t SET a=2"); + affirm( 0==rc ); + // TODO: expand these tests considerably + sqlite3_close(db); + } + + @SingleThreadOnly /* because multiple threads legitimately make these + results unpredictable */ + private synchronized void testAutoExtension(){ + final ValueHolder val = new ValueHolder<>(0); + final ValueHolder toss = new ValueHolder<>(null); + final AutoExtensionCallback ax = new AutoExtensionCallback(){ + @Override public int call(sqlite3 db){ + ++val.value; + if( null!=toss.value ){ + throw new RuntimeException(toss.value); + } + return 0; + } + }; + int rc = sqlite3_auto_extension( ax ); + affirm( 0==rc ); + sqlite3_close(createNewDb()); + affirm( 1==val.value ); + sqlite3_close(createNewDb()); + affirm( 2==val.value ); + sqlite3_reset_auto_extension(); + sqlite3_close(createNewDb()); + affirm( 2==val.value ); + rc = sqlite3_auto_extension( ax ); + affirm( 0==rc ); + // Must not add a new entry + rc = sqlite3_auto_extension( ax ); + affirm( 0==rc ); + sqlite3_close( createNewDb() ); + affirm( 3==val.value ); + + sqlite3 db = createNewDb(); + affirm( 4==val.value ); + execSql(db, "ATTACH ':memory:' as foo"); + affirm( 4==val.value, "ATTACH uses the same connection, not sub-connections." ); + sqlite3_close(db); + db = null; + + affirm( sqlite3_cancel_auto_extension(ax) ); + affirm( !sqlite3_cancel_auto_extension(ax) ); + sqlite3_close(createNewDb()); + affirm( 4==val.value ); + rc = sqlite3_auto_extension( ax ); + affirm( 0==rc ); + Exception err = null; + toss.value = "Throwing from auto_extension."; + try{ + sqlite3_close(createNewDb()); + }catch(Exception e){ + err = e; + } + affirm( err!=null ); + affirm( err.getMessage().indexOf(toss.value)>0 ); + toss.value = null; + + val.value = 0; + final AutoExtensionCallback ax2 = new AutoExtensionCallback(){ + @Override public int call(sqlite3 db){ + ++val.value; + return 0; + } + }; + rc = sqlite3_auto_extension( ax2 ); + affirm( 0 == rc ); + sqlite3_close(createNewDb()); + affirm( 2 == val.value ); + affirm( sqlite3_cancel_auto_extension(ax) ); + affirm( !sqlite3_cancel_auto_extension(ax) ); + sqlite3_close(createNewDb()); + affirm( 3 == val.value ); + rc = sqlite3_auto_extension( ax ); + affirm( 0 == rc ); + sqlite3_close(createNewDb()); + affirm( 5 == val.value ); + affirm( sqlite3_cancel_auto_extension(ax2) ); + affirm( !sqlite3_cancel_auto_extension(ax2) ); + sqlite3_close(createNewDb()); + affirm( 6 == val.value ); + rc = sqlite3_auto_extension( ax2 ); + affirm( 0 == rc ); + sqlite3_close(createNewDb()); + affirm( 8 == val.value ); + + sqlite3_reset_auto_extension(); + sqlite3_close(createNewDb()); + affirm( 8 == val.value ); + affirm( !sqlite3_cancel_auto_extension(ax) ); + affirm( !sqlite3_cancel_auto_extension(ax2) ); + sqlite3_close(createNewDb()); + affirm( 8 == val.value ); + } + + + private void testColumnMetadata(){ + final sqlite3 db = createNewDb(); + execSql(db, new String[] { + "CREATE TABLE t(a duck primary key not null collate noCase); ", + "INSERT INTO t(a) VALUES(1),(2),(3);" + }); + OutputPointer.Bool bNotNull = new OutputPointer.Bool(); + OutputPointer.Bool bPrimaryKey = new OutputPointer.Bool(); + OutputPointer.Bool bAutoinc = new OutputPointer.Bool(); + OutputPointer.String zCollSeq = new OutputPointer.String(); + OutputPointer.String zDataType = new OutputPointer.String(); + int rc = sqlite3_table_column_metadata( + db, "main", "t", "a", zDataType, zCollSeq, + bNotNull, bPrimaryKey, bAutoinc); + affirm( 0==rc ); + affirm( bPrimaryKey.value ); + affirm( !bAutoinc.value ); + affirm( bNotNull.value ); + affirm( "noCase".equals(zCollSeq.value) ); + affirm( "duck".equals(zDataType.value) ); + + TableColumnMetadata m = + sqlite3_table_column_metadata(db, "main", "t", "a"); + affirm( null != m ); + affirm( bPrimaryKey.value == m.isPrimaryKey() ); + affirm( bAutoinc.value == m.isAutoincrement() ); + affirm( bNotNull.value == m.isNotNull() ); + affirm( zCollSeq.value.equals(m.getCollation()) ); + affirm( zDataType.value.equals(m.getDataType()) ); + + affirm( null == sqlite3_table_column_metadata(db, "nope", "t", "a") ); + affirm( null == sqlite3_table_column_metadata(db, "main", "nope", "a") ); + + m = sqlite3_table_column_metadata(db, "main", "t", null) + /* Check only for existence of table */; + affirm( null != m ); + affirm( m.isPrimaryKey() ); + affirm( !m.isAutoincrement() ); + affirm( !m.isNotNull() ); + affirm( "BINARY".equalsIgnoreCase(m.getCollation()) ); + affirm( "INTEGER".equalsIgnoreCase(m.getDataType()) ); + + sqlite3_close_v2(db); + } + + private void testTxnState(){ + final sqlite3 db = createNewDb(); + affirm( SQLITE_TXN_NONE == sqlite3_txn_state(db, null) ); + affirm( sqlite3_get_autocommit(db) ); + execSql(db, "BEGIN;"); + affirm( !sqlite3_get_autocommit(db) ); + affirm( SQLITE_TXN_NONE == sqlite3_txn_state(db, null) ); + execSql(db, "SELECT * FROM sqlite_schema;"); + affirm( SQLITE_TXN_READ == sqlite3_txn_state(db, "main") ); + execSql(db, "CREATE TABLE t(a);"); + affirm( SQLITE_TXN_WRITE == sqlite3_txn_state(db, null) ); + execSql(db, "ROLLBACK;"); + affirm( SQLITE_TXN_NONE == sqlite3_txn_state(db, null) ); + sqlite3_close_v2(db); + } + + + private void testExplain(){ + final sqlite3 db = createNewDb(); + sqlite3_stmt stmt = prepare(db,"SELECT 1"); + + affirm( 0 == sqlite3_stmt_isexplain(stmt) ); + int rc = sqlite3_stmt_explain(stmt, 1); + affirm( 1 == sqlite3_stmt_isexplain(stmt) ); + rc = sqlite3_stmt_explain(stmt, 2); + affirm( 2 == sqlite3_stmt_isexplain(stmt) ); + sqlite3_finalize(stmt); + sqlite3_close_v2(db); + } + + private void testLimit(){ + final sqlite3 db = createNewDb(); + int v; + + v = sqlite3_limit(db, SQLITE_LIMIT_LENGTH, -1); + affirm( v > 0 ); + affirm( v == sqlite3_limit(db, SQLITE_LIMIT_LENGTH, v-1) ); + affirm( v-1 == sqlite3_limit(db, SQLITE_LIMIT_LENGTH, -1) ); + sqlite3_close_v2(db); + } + + private void testComplete(){ + affirm( 0==sqlite3_complete("select 1") ); + affirm( 0!=sqlite3_complete("select 1;") ); + affirm( 0!=sqlite3_complete("nope 'nope' 'nope' 1;"), "Yup" ); + } + + private void testKeyword(){ + final int n = sqlite3_keyword_count(); + affirm( n>0 ); + affirm( !sqlite3_keyword_check("_nope_") ); + affirm( sqlite3_keyword_check("seLect") ); + affirm( null!=sqlite3_keyword_name(0) ); + affirm( null!=sqlite3_keyword_name(n-1) ); + affirm( null==sqlite3_keyword_name(n) ); + } + + private void testBackup(){ + final sqlite3 dbDest = createNewDb(); + + try (sqlite3 dbSrc = createNewDb()) { + execSql(dbSrc, new String[]{ + "pragma page_size=512; VACUUM;", + "create table t(a);", + "insert into t(a) values(1),(2),(3);" + }); + affirm( null==sqlite3_backup_init(dbSrc,"main",dbSrc,"main") ); + try (sqlite3_backup b = sqlite3_backup_init(dbDest,"main",dbSrc,"main")) { + affirm( null!=b ); + affirm( b.getNativePointer()!=0 ); + int rc; + while( SQLITE_DONE!=(rc = sqlite3_backup_step(b, 1)) ){ + affirm( 0==rc ); + } + affirm( sqlite3_backup_pagecount(b) > 0 ); + rc = sqlite3_backup_finish(b); + affirm( 0==rc ); + affirm( b.getNativePointer()==0 ); + } + } + + try (sqlite3_stmt stmt = prepare(dbDest,"SELECT sum(a) from t")) { + sqlite3_step(stmt); + affirm( sqlite3_column_int(stmt,0) == 6 ); + } + sqlite3_close_v2(dbDest); + } + + private void testRandomness(){ + byte[] foo = new byte[20]; + int i = 0; + for( byte b : foo ){ + i += b; + } + affirm( i==0 ); + sqlite3_randomness(foo); + for( byte b : foo ){ + if(b!=0) ++i; + } + affirm( i!=0, "There's a very slight chance that 0 is actually correct." ); + } + + private void testBlobOpen(){ + final sqlite3 db = createNewDb(); + + execSql(db, "CREATE TABLE T(a BLOB);" + +"INSERT INTO t(rowid,a) VALUES(1, 'def'),(2, 'XYZ');" + ); + final OutputPointer.sqlite3_blob pOut = new OutputPointer.sqlite3_blob(); + int rc = sqlite3_blob_open(db, "main", "t", "a", + sqlite3_last_insert_rowid(db), 1, pOut); + affirm( 0==rc ); + sqlite3_blob b = pOut.take(); + affirm( null!=b ); + affirm( 0!=b.getNativePointer() ); + affirm( 3==sqlite3_blob_bytes(b) ); + rc = sqlite3_blob_write( b, new byte[] {100, 101, 102 /*"DEF"*/}, 0); + affirm( 0==rc ); + rc = sqlite3_blob_close(b); + affirm( 0==rc ); + rc = sqlite3_blob_close(b); + affirm( 0!=rc ); + affirm( 0==b.getNativePointer() ); + sqlite3_stmt stmt = prepare(db,"SELECT length(a), a FROM t ORDER BY a"); + affirm( SQLITE_ROW == sqlite3_step(stmt) ); + affirm( 3 == sqlite3_column_int(stmt,0) ); + affirm( "def".equals(sqlite3_column_text16(stmt,1)) ); + sqlite3_finalize(stmt); + + b = sqlite3_blob_open(db, "main", "t", "a", + sqlite3_last_insert_rowid(db), 0); + affirm( null!=b ); + rc = sqlite3_blob_reopen(b, 2); + affirm( 0==rc ); + final byte[] tgt = new byte[3]; + rc = sqlite3_blob_read(b, tgt, 0); + affirm( 0==rc ); + affirm( 100==tgt[0] && 101==tgt[1] && 102==tgt[2], "DEF" ); + rc = sqlite3_blob_close(b); + affirm( 0==rc ); + + if( !sqlite3_jni_supports_nio() ){ + outln("WARNING: skipping tests for ByteBuffer-using sqlite3_blob APIs ", + "because this platform lacks that support."); + sqlite3_close_v2(db); + return; + } + /* Sanity checks for the java.nio.ByteBuffer-taking overloads of + sqlite3_blob_read/write(). */ + execSql(db, "UPDATE t SET a=zeroblob(10)"); + b = sqlite3_blob_open(db, "main", "t", "a", 1, 1); + affirm( null!=b ); + java.nio.ByteBuffer bb = java.nio.ByteBuffer.allocateDirect(10); + for( byte i = 0; i < 10; ++i ){ + bb.put((int)i, (byte)(48+i & 0xff)); + } + rc = sqlite3_blob_write(b, 1, bb, 1, 10); + affirm( rc==SQLITE_ERROR, "b length < (srcOffset + bb length)" ); + rc = sqlite3_blob_write(b, -1, bb); + affirm( rc==SQLITE_ERROR, "Target offset may not be negative" ); + rc = sqlite3_blob_write(b, 0, bb, -1, -1); + affirm( rc==SQLITE_ERROR, "Source offset may not be negative" ); + rc = sqlite3_blob_write(b, 1, bb, 1, 8); + affirm( rc==0 ); + // b's contents: 0 49 50 51 52 53 54 55 56 0 + // ascii: 0 '1' '2' '3' '4' '5' '6' '7' '8' 0 + byte br[] = new byte[10]; + java.nio.ByteBuffer bbr = + java.nio.ByteBuffer.allocateDirect(bb.limit()); + rc = sqlite3_blob_read( b, br, 0 ); + affirm( rc==0 ); + rc = sqlite3_blob_read( b, bbr ); + affirm( rc==0 ); + java.nio.ByteBuffer bbr2 = sqlite3_blob_read_nio_buffer(b, 0, 12); + affirm( null==bbr2, "Read size is too big"); + bbr2 = sqlite3_blob_read_nio_buffer(b, -1, 3); + affirm( null==bbr2, "Source offset is negative"); + bbr2 = sqlite3_blob_read_nio_buffer(b, 5, 6); + affirm( null==bbr2, "Read pos+size is too big"); + bbr2 = sqlite3_blob_read_nio_buffer(b, 4, 7); + affirm( null==bbr2, "Read pos+size is too big"); + bbr2 = sqlite3_blob_read_nio_buffer(b, 4, 6); + affirm( null!=bbr2 ); + java.nio.ByteBuffer bbr3 = + java.nio.ByteBuffer.allocateDirect(2 * bb.limit()); + java.nio.ByteBuffer bbr4 = + java.nio.ByteBuffer.allocateDirect(5); + rc = sqlite3_blob_read( b, bbr3 ); + affirm( rc==0 ); + rc = sqlite3_blob_read( b, bbr4 ); + affirm( rc==0 ); + affirm( sqlite3_blob_bytes(b)==bbr3.limit() ); + affirm( 5==bbr4.limit() ); + sqlite3_blob_close(b); + affirm( 0==br[0] ); + affirm( 0==br[9] ); + affirm( 0==bbr.get(0) ); + affirm( 0==bbr.get(9) ); + affirm( bbr2.limit() == 6 ); + affirm( 0==bbr3.get(0) ); + { + Exception ex = null; + try{ bbr3.get(11); } + catch(Exception e){ex = e;} + affirm( ex instanceof IndexOutOfBoundsException, + "bbr3.limit() was reset by read()" ); + ex = null; + } + affirm( 0==bbr4.get(0) ); + for( int i = 1; i < 9; ++i ){ + affirm( br[i] == 48 + i ); + affirm( br[i] == bbr.get(i) ); + affirm( br[i] == bbr3.get(i) ); + if( i>3 ){ + affirm( br[i] == bbr2.get(i-4) ); + } + if( i < bbr4.limit() ){ + affirm( br[i] == bbr4.get(i) ); + } + } + sqlite3_close_v2(db); + } + + private void testPrepareMulti(){ + final sqlite3 db = createNewDb(); + final String[] sql = { + "create table t(","a)", + "; insert into t(a) values(1),(2),(3);", + "select a from t;" + }; + final List liStmt = new ArrayList<>(); + final PrepareMultiCallback proxy = new PrepareMultiCallback.StepAll(); + final ValueHolder toss = new ValueHolder<>(null); + PrepareMultiCallback m = new PrepareMultiCallback() { + @Override public int call(sqlite3_stmt st){ + liStmt.add(st); + if( null!=toss.value ){ + throw new RuntimeException(toss.value); + } + return proxy.call(st); + } + }; + int rc = sqlite3_prepare_multi(db, sql, m); + affirm( 0==rc ); + affirm( liStmt.size() == 3 ); + for( sqlite3_stmt st : liStmt ){ + sqlite3_finalize(st); + } + toss.value = "This is an exception."; + rc = sqlite3_prepare_multi(db, "SELECT 1", m); + affirm( SQLITE_ERROR==rc ); + affirm( sqlite3_errmsg(db).indexOf(toss.value)>0 ); + sqlite3_close_v2(db); + } + + private void testSetErrmsg(){ + final sqlite3 db = createNewDb(); + + int rc = sqlite3_set_errmsg(db, SQLITE_RANGE, "nope"); + affirm( 0==rc ); + affirm( SQLITE_MISUSE == sqlite3_set_errmsg(null, 0, null) ); + affirm( "nope".equals(sqlite3_errmsg(db)) ); + affirm( SQLITE_RANGE == sqlite3_errcode(db) ); + rc = sqlite3_set_errmsg(db, 0, null); + affirm( "not an error".equals(sqlite3_errmsg(db)) ); + affirm( 0 == sqlite3_errcode(db) ); + sqlite3_close_v2(db); + } + + /* Copy/paste/rename this to add new tests. */ + private void _testTemplate(){ + final sqlite3 db = createNewDb(); + sqlite3_stmt stmt = prepare(db,"SELECT 1"); + sqlite3_finalize(stmt); + sqlite3_close_v2(db); + } + + + @ManualTest /* we really only want to run this test manually */ + private void testSleep(){ + out("Sleeping briefly... "); + sqlite3_sleep(600); + outln("Woke up."); + } + + private void nap() throws InterruptedException { + if( takeNaps ){ + Thread.sleep(java.util.concurrent.ThreadLocalRandom.current().nextInt(3, 17), 0); + } + } + + @ManualTest /* because we only want to run this test on demand */ + private void testFail(){ + affirm( false, "Intentional failure." ); + } + + private void runTests(boolean fromThread) throws Exception { + if(false) showCompileOption(); + List mlist = testMethods; + affirm( null!=mlist ); + if( shuffle ){ + mlist = new ArrayList<>( testMethods.subList(0, testMethods.size()) ); + java.util.Collections.shuffle(mlist); + } + if( (!fromThread && listRunTests>0) || listRunTests>1 ){ + synchronized(this.getClass()){ + if( !fromThread ){ + out("Initial test"," list: "); + for(java.lang.reflect.Method m : testMethods){ + out(m.getName()+" "); + } + outln(); + outln("(That list excludes some which are hard-coded to run.)"); + } + out("Running"," tests: "); + for(java.lang.reflect.Method m : mlist){ + out(m.getName()+" "); + } + outln(); + } + } + for(java.lang.reflect.Method m : mlist){ + nap(); + try{ + m.invoke(this); + }catch(java.lang.reflect.InvocationTargetException e){ + outln("FAILURE: ",m.getName(),"(): ", e.getCause()); + throw e; + } + } + synchronized( this.getClass() ){ + ++nTestRuns; + } + } + + public void run() { + try { + runTests(0!=this.tId); + }catch(Exception e){ + synchronized( listErrors ){ + listErrors.add(e); + } + }finally{ + affirm( sqlite3_java_uncache_thread() ); + affirm( !sqlite3_java_uncache_thread() ); + } + } + + /** + Runs the basic sqlite3 JNI binding sanity-check suite. + + CLI flags: + + -q|-quiet: disables most test output. + + -t|-thread N: runs the tests in N threads + concurrently. Default=1. + + -r|-repeat N: repeats the tests in a loop N times, each one + consisting of the -thread value's threads. + + -shuffle: randomizes the order of most of the test functions. + + -naps: sleep small random intervals between tests in order to add + some chaos for cross-thread contention. + + + -list-tests: outputs the list of tests being run, minus some + which are hard-coded. In multi-threaded mode, use this twice to + to emit the list run by each thread (which may differ from the initial + list, in particular if -shuffle is used). + + -fail: forces an exception to be thrown during the test run. Use + with -shuffle to make its appearance unpredictable. + + -v: emit some developer-mode info at the end. + */ + public static void main(String[] args) throws Exception { + int nThread = 1; + boolean doSomethingForDev = false; + int nRepeat = 1; + boolean forceFail = false; + boolean sqlLog = false; + boolean configLog = false; + boolean squelchTestOutput = false; + for( int i = 0; i < args.length; ){ + String arg = args[i++]; + if(arg.startsWith("-")){ + arg = arg.replaceFirst("-+",""); + if(arg.equals("v")){ + doSomethingForDev = true; + //listBoundMethods(); + }else if(arg.equals("t") || arg.equals("thread")){ + nThread = Integer.parseInt(args[i++]); + }else if(arg.equals("r") || arg.equals("repeat")){ + nRepeat = Integer.parseInt(args[i++]); + }else if(arg.equals("shuffle")){ + shuffle = true; + }else if(arg.equals("list-tests")){ + ++listRunTests; + }else if(arg.equals("fail")){ + forceFail = true; + }else if(arg.equals("sqllog")){ + sqlLog = true; + }else if(arg.equals("configlog")){ + configLog = true; + }else if(arg.equals("naps")){ + takeNaps = true; + }else if(arg.equals("q") || arg.equals("quiet")){ + squelchTestOutput = true; + }else{ + throw new IllegalArgumentException("Unhandled flag:"+arg); + } + } + } + + if( sqlLog ){ + if( sqlite3_compileoption_used("ENABLE_SQLLOG") ){ + final ConfigSqlLogCallback log = new ConfigSqlLogCallback() { + @Override public void call(sqlite3 db, String msg, int op){ + switch(op){ + case 0: outln("Opening db: ",db); break; + case 1: outln("SQL ",db,": ",msg); break; + case 2: outln("Closing db: ",db); break; + } + } + }; + int rc = sqlite3_config( log ); + affirm( 0==rc ); + rc = sqlite3_config( (ConfigSqlLogCallback)null ); + affirm( 0==rc ); + rc = sqlite3_config( log ); + affirm( 0==rc ); + }else{ + outln("WARNING: -sqllog is not active because library was built ", + "without SQLITE_ENABLE_SQLLOG."); + } + } + if( configLog ){ + final ConfigLogCallback log = new ConfigLogCallback() { + @Override public void call(int code, String msg){ + outln("ConfigLogCallback: ",ResultCode.getEntryForInt(code),": ", msg); + } + }; + int rc = sqlite3_config( log ); + affirm( 0==rc ); + rc = sqlite3_config( (ConfigLogCallback)null ); + affirm( 0==rc ); + rc = sqlite3_config( log ); + affirm( 0==rc ); + } + + quietMode = squelchTestOutput; + outln("If you just saw warning messages regarding CallStaticObjectMethod, ", + "you are very likely seeing the side effects of a known openjdk8 ", + "bug. It is unsightly but does not affect the library."); + + { + // Build list of tests to run from the methods named test*(). + testMethods = new ArrayList<>(); + int nSkipped = 0; + for(final java.lang.reflect.Method m : Tester1.class.getDeclaredMethods()){ + final String name = m.getName(); + if( name.equals("testFail") ){ + if( forceFail ){ + testMethods.add(m); + } + }else if( m.isAnnotationPresent( RequiresJniNio.class ) + && !sqlite3_jni_supports_nio() ){ + outln("Skipping test for lack of JNI java.nio.ByteBuffer support: ", + name,"()\n"); + ++nSkipped; + }else if( !m.isAnnotationPresent( ManualTest.class ) ){ + if( nThread>1 && m.isAnnotationPresent( SingleThreadOnly.class ) ){ + out("Skipping test in multi-thread mode: ",name,"()\n"); + ++nSkipped; + }else if( name.startsWith("test") ){ + testMethods.add(m); + } + } + } + } + + final long timeStart = System.currentTimeMillis(); + int nLoop = 0; + switch( sqlite3_threadsafe() ){ /* Sanity checking */ + case 0: + affirm( SQLITE_ERROR==sqlite3_config( SQLITE_CONFIG_SINGLETHREAD ), + "Could not switch to single-thread mode." ); + affirm( SQLITE_ERROR==sqlite3_config( SQLITE_CONFIG_MULTITHREAD ), + "Could switch to multithread mode." ); + affirm( SQLITE_ERROR==sqlite3_config( SQLITE_CONFIG_SERIALIZED ), + "Could not switch to serialized threading mode." ); + outln("This is a single-threaded build. Not using threads."); + nThread = 1; + break; + case 1: + case 2: + affirm( 0==sqlite3_config( SQLITE_CONFIG_SINGLETHREAD ), + "Could not switch to single-thread mode." ); + affirm( 0==sqlite3_config( SQLITE_CONFIG_MULTITHREAD ), + "Could not switch to multithread mode." ); + affirm( 0==sqlite3_config( SQLITE_CONFIG_SERIALIZED ), + "Could not switch to serialized threading mode." ); + break; + default: + affirm( false, "Unhandled SQLITE_THREADSAFE value." ); + } + outln("libversion_number: ", + sqlite3_libversion_number(),"\n", + sqlite3_libversion(),"\n",SQLITE_SOURCE_ID,"\n", + "SQLITE_THREADSAFE=",sqlite3_threadsafe()); + outln("JVM NIO support? ",sqlite3_jni_supports_nio() ? "YES" : "NO"); + final boolean showLoopCount = (nRepeat>1 && nThread>1); + if( showLoopCount ){ + outln("Running ",nRepeat," loop(s) with ",nThread," thread(s) each."); + } + if( takeNaps ) outln("Napping between tests is enabled."); + for( int n = 0; n < nRepeat; ++n ){ + ++nLoop; + if( showLoopCount ) out((1==nLoop ? "" : " ")+nLoop); + if( nThread<=1 ){ + new Tester1(0).runTests(false); + continue; + } + Tester1.mtMode = true; + final ExecutorService ex = Executors.newFixedThreadPool( nThread ); + for( int i = 0; i < nThread; ++i ){ + ex.submit( new Tester1(i), i ); + } + ex.shutdown(); + try{ + ex.awaitTermination(nThread*200, java.util.concurrent.TimeUnit.MILLISECONDS); + ex.shutdownNow(); + }catch (InterruptedException ie){ + ex.shutdownNow(); + Thread.currentThread().interrupt(); + } + if( !listErrors.isEmpty() ){ + quietMode = false; + outln("TEST ERRORS:"); + Exception err = null; + for( Exception e : listErrors ){ + e.printStackTrace(); + if( null==err ) err = e; + } + if( null!=err ) throw err; + } + } + if( showLoopCount ) outln(); + quietMode = false; + + final long timeEnd = System.currentTimeMillis(); + outln("Tests done. Metrics across ",nTestRuns," total iteration(s):"); + outln("\tAssertions checked: ",affirmCount); + outln("\tDatabases opened: ",metrics.dbOpen); + if( doSomethingForDev ){ + sqlite3_jni_internal_details(); + } + affirm( 0==sqlite3_release_memory(1) ); + sqlite3_shutdown(); + int nMethods = 0; + int nNatives = 0; + final java.lang.reflect.Method[] declaredMethods = + CApi.class.getDeclaredMethods(); + for(java.lang.reflect.Method m : declaredMethods){ + final int mod = m.getModifiers(); + if( 0!=(mod & java.lang.reflect.Modifier.STATIC) ){ + final String name = m.getName(); + if(name.startsWith("sqlite3_")){ + ++nMethods; + if( 0!=(mod & java.lang.reflect.Modifier.NATIVE) ){ + ++nNatives; + } + } + } + } + outln("\tCApi.sqlite3_*() methods: "+ + nMethods+" total, with "+ + nNatives+" native, "+ + (nMethods - nNatives)+" Java" + ); + outln("\tTotal test time = " + +(timeEnd - timeStart)+"ms"); + } +} diff --git a/ext/jni/src/org/sqlite/jni/capi/TraceV2Callback.java b/ext/jni/src/org/sqlite/jni/capi/TraceV2Callback.java new file mode 100644 index 0000000000..56465a2c0a --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/capi/TraceV2Callback.java @@ -0,0 +1,50 @@ +/* +** 2023-08-25 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file is part of the JNI bindings for the sqlite3 C API. +*/ +package org.sqlite.jni.capi; +import org.sqlite.jni.annotation.Nullable; + +/** + Callback for use with {@link CApi#sqlite3_trace_v2}. +*/ +public interface TraceV2Callback extends CallbackProxy { + /** + Called by sqlite3 for various tracing operations, as per + sqlite3_trace_v2(). Note that this interface elides the 2nd + argument to the native trace callback, as that role is better + filled by instance-local state. + +

    These callbacks may throw, in which case their exceptions are + converted to C-level error information. + +

    The 2nd argument to this function, if non-null, will be a an + sqlite3 or sqlite3_stmt object, depending on the first argument + (see below). + +

    The final argument to this function is the "X" argument + documented for sqlite3_trace() and sqlite3_trace_v2(). Its type + depends on value of the first argument: + +

    - SQLITE_TRACE_STMT: pNative is a sqlite3_stmt. pX is a String + containing the prepared SQL. + +

    - SQLITE_TRACE_PROFILE: pNative is a sqlite3_stmt. pX is a Long + holding an approximate number of nanoseconds the statement took + to run. + +

    - SQLITE_TRACE_ROW: pNative is a sqlite3_stmt. pX is null. + +

    - SQLITE_TRACE_CLOSE: pNative is a sqlite3. pX is null. + */ + int call(int traceFlag, Object pNative, @Nullable Object pX); +} diff --git a/ext/jni/src/org/sqlite/jni/capi/UpdateHookCallback.java b/ext/jni/src/org/sqlite/jni/capi/UpdateHookCallback.java new file mode 100644 index 0000000000..e3d491f67e --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/capi/UpdateHookCallback.java @@ -0,0 +1,26 @@ +/* +** 2023-08-25 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file is part of the JNI bindings for the sqlite3 C API. +*/ +package org.sqlite.jni.capi; + +/** + Callback for use with {@link CApi#sqlite3_update_hook}. +*/ +public interface UpdateHookCallback extends CallbackProxy { + /** + Must function as described for the C-level sqlite3_update_hook() + callback. If it throws, the exception is translated into + a db-level error. + */ + void call(int opId, String dbName, String tableName, long rowId); +} diff --git a/ext/jni/src/org/sqlite/jni/capi/ValueHolder.java b/ext/jni/src/org/sqlite/jni/capi/ValueHolder.java new file mode 100644 index 0000000000..0a469fea9a --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/capi/ValueHolder.java @@ -0,0 +1,27 @@ +/* +** 2023-10-16 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains the ValueHolder utility class for the sqlite3 +** JNI bindings. +*/ +package org.sqlite.jni.capi; + +/** + A helper class which simply holds a single value. Its primary use + is for communicating values out of anonymous classes, as doing so + requires a "final" reference, as well as communicating aggregate + SQL function state across calls to such functions. +*/ +public class ValueHolder { + public T value; + public ValueHolder(){} + public ValueHolder(T v){value = v;} +} diff --git a/ext/jni/src/org/sqlite/jni/capi/WindowFunction.java b/ext/jni/src/org/sqlite/jni/capi/WindowFunction.java new file mode 100644 index 0000000000..eaf1bb9a35 --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/capi/WindowFunction.java @@ -0,0 +1,39 @@ +/* +** 2023-08-25 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file is part of the JNI bindings for the sqlite3 C API. +*/ +package org.sqlite.jni.capi; + + +/** + A SQLFunction implementation for window functions. Note that + WindowFunction inherits from {@link AggregateFunction} and each + instance is required to implement the inherited abstract methods + from that class. See {@link AggregateFunction} for information on + managing the UDF's invocation-specific state. +*/ +public abstract class WindowFunction extends AggregateFunction { + + /** + As for the xInverse() argument of the C API's + sqlite3_create_window_function(). If this function throws, the + exception is not propagated and a warning might be emitted + to a debugging channel. + */ + public abstract void xInverse(sqlite3_context cx, sqlite3_value[] args); + + /** + As for the xValue() argument of the C API's sqlite3_create_window_function(). + See xInverse() for the fate of any exceptions this throws. + */ + public abstract void xValue(sqlite3_context cx); +} diff --git a/ext/jni/src/org/sqlite/jni/capi/XDestroyCallback.java b/ext/jni/src/org/sqlite/jni/capi/XDestroyCallback.java new file mode 100644 index 0000000000..ce6c6a6abf --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/capi/XDestroyCallback.java @@ -0,0 +1,37 @@ +/* +** 2023-07-21 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file declares JNI bindings for the sqlite3 C API. +*/ +package org.sqlite.jni.capi; + +/** + Callback for a hook called by SQLite when certain client-provided + state are destroyed. It gets its name from the pervasive use of + the symbol name xDestroy() for this purpose in the C API + documentation. +*/ +public interface XDestroyCallback { + /** + Must perform any cleanup required by this object. Must not + throw. Must not call back into the sqlite3 API, else it might + invoke a deadlock. + + WARNING: as a rule, it is never safe to register individual + instances with this interface multiple times in the + library. e.g., do not register the same CollationCallback with + multiple arities or names using sqlite3_create_collation(). If + this rule is violated, the library will eventually try to free + each individual reference, leading to memory corruption or a + crash via duplicate free(). + */ + void xDestroy(); +} diff --git a/ext/jni/src/org/sqlite/jni/capi/package-info.java b/ext/jni/src/org/sqlite/jni/capi/package-info.java new file mode 100644 index 0000000000..127f380675 --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/capi/package-info.java @@ -0,0 +1,89 @@ +/** + This package houses a JNI binding to the SQLite3 C API. + +

    The primary interfaces are in {@link + org.sqlite.jni.capi.CApi}.

    + +

    API Goals and Requirements

    + +
      + +
    • A 1-to-1(-ish) mapping of the C API to Java via JNI, insofar + as cross-language semantics allow for. A closely-related goal is + that the C + documentation should be usable as-is, insofar as possible, + for most of the JNI binding. As a rule, undocumented symbols in + the Java interface behave as documented for their C API + counterpart. Only semantic differences and Java-specific features + are documented here.
    • + +
    • Support Java as far back as version 8 (2014).
    • + +
    • Environment-independent. Should work everywhere both Java and + SQLite3 do.
    • + +
    • No 3rd-party dependencies beyond the JDK. That includes no + build-level dependencies for specific IDEs and toolchains. We + welcome the addition of build files for arbitrary environments + insofar as they neither interfere with each other nor become a + maintenance burden for the sqlite developers.
    • + +
    + +

    Non-Goals

    + +
      + +
    • Creation of high-level OO wrapper APIs. Clients are free to + create them off of the C-style API.
    • + +
    • Support for mixed-mode operation, where client code accesses + SQLite both via the Java-side API and the C API via their own + native code. In such cases, proxy functionalities (primarily + callback handler wrappers of all sorts) may fail because the + C-side use of the SQLite APIs will bypass those proxies.
    • + +
    + +

    State of this API

    + +

    As of version 3.43, this software is in "tech preview" form. We + tentatively plan to stamp it as stable with the 3.44 release.

    + +

    Threading Considerations

    + +

    This API is, if built with SQLITE_THREADSAFE set to 1 or 2, + thread-safe, insofar as the C API guarantees, with some addenda:

    + +
      + +
    • It is not legal to use Java-facing SQLite3 resource handles + (sqlite3, sqlite3_stmt, etc) from multiple threads concurrently, + nor to use any database-specific resources concurrently in a + thread separate from the one the database is currently in use + in. i.e. do not use a sqlite3_stmt in thread #2 when thread #1 is + using the database which prepared that handle. + +
      Violating this will eventually corrupt the JNI-level bindings + between Java's and C's view of the database. This is a limitation + of the JNI bindings, not the lower-level library. +
    • + +
    • It is legal to use a given handle, and database-specific + resources, across threads, so long as no two threads pass + resources owned by the same database into the library + concurrently. +
    • + +
    + +

    Any number of threads may, of course, create and use any number + of database handles they wish. Care only needs to be taken when + those handles or their associated resources cross threads, or...

    + +

    When built with SQLITE_THREADSAFE=0 then no threading guarantees + are provided and multi-threaded use of the library will provoke + undefined behavior.

    + +*/ +package org.sqlite.jni.capi; diff --git a/ext/jni/src/org/sqlite/jni/capi/sqlite3.java b/ext/jni/src/org/sqlite/jni/capi/sqlite3.java new file mode 100644 index 0000000000..cc6f2e6e8d --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/capi/sqlite3.java @@ -0,0 +1,43 @@ +/* +** 2023-07-21 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file is part of the JNI bindings for the sqlite3 C API. +*/ +package org.sqlite.jni.capi; + +/** + A wrapper for communicating C-level (sqlite3*) instances with + Java. These wrappers do not own their associated pointer, they + simply provide a type-safe way to communicate it between Java + and C via JNI. +*/ +public final class sqlite3 extends NativePointerHolder + implements AutoCloseable { + + // Only invoked from JNI + private sqlite3(){} + + public String toString(){ + final long ptr = getNativePointer(); + if( 0==ptr ){ + return sqlite3.class.getSimpleName()+"@null"; + } + final String fn = CApi.sqlite3_db_filename(this, "main"); + return sqlite3.class.getSimpleName() + +"@"+String.format("0x%08x",ptr) + +"["+((null == fn) ? "" : fn)+"]" + ; + } + + @Override public void close(){ + CApi.sqlite3_close_v2(this); + } +} diff --git a/ext/jni/src/org/sqlite/jni/capi/sqlite3_backup.java b/ext/jni/src/org/sqlite/jni/capi/sqlite3_backup.java new file mode 100644 index 0000000000..0ef75c17eb --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/capi/sqlite3_backup.java @@ -0,0 +1,31 @@ +/* +** 2023-09-03 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file is part of the JNI bindings for the sqlite3 C API. +*/ +package org.sqlite.jni.capi; + +/** + A wrapper for passing C-level (sqlite3_backup*) instances around in + Java. These wrappers do not own their associated pointer, they + simply provide a type-safe way to communicate it between Java and C + via JNI. +*/ +public final class sqlite3_backup extends NativePointerHolder + implements AutoCloseable { + // Only invoked from JNI. + private sqlite3_backup(){} + + @Override public void close(){ + CApi.sqlite3_backup_finish(this); + } + +} diff --git a/ext/jni/src/org/sqlite/jni/capi/sqlite3_blob.java b/ext/jni/src/org/sqlite/jni/capi/sqlite3_blob.java new file mode 100644 index 0000000000..bdc0200af4 --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/capi/sqlite3_blob.java @@ -0,0 +1,30 @@ +/* +** 2023-09-03 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file is part of the JNI bindings for the sqlite3 C API. +*/ +package org.sqlite.jni.capi; + +/** + A wrapper for passing C-level (sqlite3_blob*) instances around in + Java. These wrappers do not own their associated pointer, they + simply provide a type-safe way to communicate it between Java and C + via JNI. +*/ +public final class sqlite3_blob extends NativePointerHolder + implements AutoCloseable { + // Only invoked from JNI. + private sqlite3_blob(){} + + @Override public void close(){ + CApi.sqlite3_blob_close(this); + } +} diff --git a/ext/jni/src/org/sqlite/jni/capi/sqlite3_context.java b/ext/jni/src/org/sqlite/jni/capi/sqlite3_context.java new file mode 100644 index 0000000000..82ec49af16 --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/capi/sqlite3_context.java @@ -0,0 +1,79 @@ +/* +** 2023-07-21 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file is part of the JNI bindings for the sqlite3 C API. +*/ +package org.sqlite.jni.capi; + +/** + sqlite3_context instances are used in conjunction with user-defined + SQL functions (a.k.a. UDFs). +*/ +public final class sqlite3_context extends NativePointerHolder { + private Long aggregateContext = null; + + /** + getAggregateContext() corresponds to C's + sqlite3_aggregate_context(), with a slightly different interface + to account for cross-language differences. It serves the same + purposes in a slightly different way: it provides a key which is + stable across invocations of a UDF's callbacks, such that all + calls into those callbacks can determine which "set" of those + calls they belong to. + +

    Note that use of this method is not a requirement for proper use + of this class. sqlite3_aggregate_context() can also be used. + +

    If the argument is true and the aggregate context has not yet + been set up, it will be initialized and fetched on demand, else it + won't. The intent is that xStep(), xValue(), and xInverse() + methods pass true and xFinal() methods pass false. + +

    This function treats numeric 0 as null, always returning null instead + of 0. + +

    If this object is being used in the context of an aggregate or + window UDF, this function returns a non-0 value which is distinct + for each set of UDF callbacks from a single invocation of the + UDF, otherwise it returns 0. The returned value is only only + valid within the context of execution of a single SQL statement, + and must not be re-used by future invocations of the UDF in + different SQL statements. + +

    Consider this SQL, where MYFUNC is a user-defined aggregate function: + +

    {@code
    +     SELECT MYFUNC(A), MYFUNC(B) FROM T;
    +     }
    + +

    The xStep() and xFinal() methods of the callback need to be able + to differentiate between those two invocations in order to + perform their work properly. The value returned by + getAggregateContext() will be distinct for each of those + invocations of MYFUNC() and is intended to be used as a lookup + key for mapping callback invocations to whatever client-defined + state is needed by the UDF. + +

    There is one case where this will return null in the context + of an aggregate or window function: if the result set has no + rows, the UDF's xFinal() will be called without any other x...() + members having been called. In that one case, no aggregate + context key will have been generated. xFinal() implementations + need to be prepared to accept that condition as legal. + */ + public synchronized Long getAggregateContext(boolean initIfNeeded){ + if( aggregateContext==null ){ + aggregateContext = CApi.sqlite3_aggregate_context(this, initIfNeeded); + if( !initIfNeeded && null==aggregateContext ) aggregateContext = 0L; + } + return (null==aggregateContext || 0!=aggregateContext) ? aggregateContext : null; + } +} diff --git a/ext/jni/src/org/sqlite/jni/capi/sqlite3_stmt.java b/ext/jni/src/org/sqlite/jni/capi/sqlite3_stmt.java new file mode 100644 index 0000000000..564891c727 --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/capi/sqlite3_stmt.java @@ -0,0 +1,30 @@ +/* +** 2023-07-21 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file is part of the JNI bindings for the sqlite3 C API. +*/ +package org.sqlite.jni.capi; + +/** + A wrapper for communicating C-level (sqlite3_stmt*) instances with + Java. These wrappers do not own their associated pointer, they + simply provide a type-safe way to communicate it between Java and C + via JNI. +*/ +public final class sqlite3_stmt extends NativePointerHolder + implements AutoCloseable { + // Only invoked from JNI. + private sqlite3_stmt(){} + + @Override public void close(){ + CApi.sqlite3_finalize(this); + } +} diff --git a/ext/jni/src/org/sqlite/jni/capi/sqlite3_value.java b/ext/jni/src/org/sqlite/jni/capi/sqlite3_value.java new file mode 100644 index 0000000000..a4772f0f63 --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/capi/sqlite3_value.java @@ -0,0 +1,19 @@ +/* +** 2023-07-21 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file is part of the JNI bindings for the sqlite3 C API. +*/ +package org.sqlite.jni.capi; + +public final class sqlite3_value extends NativePointerHolder { + //! Invoked only from JNI. + private sqlite3_value(){} +} diff --git a/ext/jni/src/org/sqlite/jni/fts5/Fts5.java b/ext/jni/src/org/sqlite/jni/fts5/Fts5.java new file mode 100644 index 0000000000..0dceeafd2e --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/fts5/Fts5.java @@ -0,0 +1,32 @@ +/* +** 2023-08-05 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file is part of the JNI bindings for the sqlite3 C API. +*/ +package org.sqlite.jni.fts5; + +/** + INCOMPLETE AND COMPLETELY UNTESTED. + + A utility object for holding FTS5-specific types and constants + which are used by multiple FTS5 classes. +*/ +public final class Fts5 { + /* Not used */ + private Fts5(){} + + + public static final int FTS5_TOKENIZE_QUERY = 0x0001; + public static final int FTS5_TOKENIZE_PREFIX = 0x0002; + public static final int FTS5_TOKENIZE_DOCUMENT = 0x0004; + public static final int FTS5_TOKENIZE_AUX = 0x0008; + public static final int FTS5_TOKEN_COLOCATED = 0x0001; +} diff --git a/ext/jni/src/org/sqlite/jni/fts5/Fts5Context.java b/ext/jni/src/org/sqlite/jni/fts5/Fts5Context.java new file mode 100644 index 0000000000..439b477910 --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/fts5/Fts5Context.java @@ -0,0 +1,24 @@ +/* +** 2023-08-04 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file is part of the JNI bindings for the sqlite3 C API. +*/ +package org.sqlite.jni.fts5; +import org.sqlite.jni.capi.*; + +/** + A wrapper for communicating C-level (Fts5Context*) instances with + Java. These wrappers do not own their associated pointer, they + simply provide a type-safe way to communicate it between Java and C + via JNI. +*/ +public final class Fts5Context extends NativePointerHolder { +} diff --git a/ext/jni/src/org/sqlite/jni/fts5/Fts5ExtensionApi.java b/ext/jni/src/org/sqlite/jni/fts5/Fts5ExtensionApi.java new file mode 100644 index 0000000000..f409f4961d --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/fts5/Fts5ExtensionApi.java @@ -0,0 +1,96 @@ +/* +** 2023-08-04 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file is part of the JNI bindings for the sqlite3 C API. +*/ +package org.sqlite.jni.fts5; +import org.sqlite.jni.capi.*; +import org.sqlite.jni.annotation.*; + +/** +*/ +public final class Fts5ExtensionApi extends NativePointerHolder { + //! Only called from JNI + private Fts5ExtensionApi(){} + private final int iVersion = 2; + + /* Callback type for used by xQueryPhrase(). */ + public interface XQueryPhraseCallback { + int call(Fts5ExtensionApi fapi, Fts5Context cx); + } + + /** + Returns the singleton instance of this class. + */ + public static native Fts5ExtensionApi getInstance(); + + public native int xColumnCount(@NotNull Fts5Context fcx); + + public native int xColumnSize(@NotNull Fts5Context cx, int iCol, + @NotNull OutputPointer.Int32 pnToken); + + public native int xColumnText(@NotNull Fts5Context cx, int iCol, + @NotNull OutputPointer.String txt); + + public native int xColumnTotalSize(@NotNull Fts5Context fcx, int iCol, + @NotNull OutputPointer.Int64 pnToken); + + public native Object xGetAuxdata(@NotNull Fts5Context cx, boolean clearIt); + + public native int xInst(@NotNull Fts5Context cx, int iIdx, + @NotNull OutputPointer.Int32 piPhrase, + @NotNull OutputPointer.Int32 piCol, + @NotNull OutputPointer.Int32 piOff); + + public native int xInstCount(@NotNull Fts5Context fcx, + @NotNull OutputPointer.Int32 pnInst); + + public native int xPhraseCount(@NotNull Fts5Context fcx); + + public native int xPhraseFirst(@NotNull Fts5Context cx, int iPhrase, + @NotNull Fts5PhraseIter iter, + @NotNull OutputPointer.Int32 iCol, + @NotNull OutputPointer.Int32 iOff); + + public native int xPhraseFirstColumn(@NotNull Fts5Context cx, int iPhrase, + @NotNull Fts5PhraseIter iter, + @NotNull OutputPointer.Int32 iCol); + public native void xPhraseNext(@NotNull Fts5Context cx, + @NotNull Fts5PhraseIter iter, + @NotNull OutputPointer.Int32 iCol, + @NotNull OutputPointer.Int32 iOff); + public native void xPhraseNextColumn(@NotNull Fts5Context cx, + @NotNull Fts5PhraseIter iter, + @NotNull OutputPointer.Int32 iCol); + public native int xPhraseSize(@NotNull Fts5Context fcx, int iPhrase); + + public native int xQueryPhrase(@NotNull Fts5Context cx, int iPhrase, + @NotNull XQueryPhraseCallback callback); + public native int xRowCount(@NotNull Fts5Context fcx, + @NotNull OutputPointer.Int64 nRow); + + public native long xRowid(@NotNull Fts5Context cx); + /* Note that the JNI binding lacks the C version's xDelete() + callback argument. Instead, if pAux has an xDestroy() method, it + is called if the FTS5 API finalizes the aux state (including if + allocation of storage for the auxdata fails). Any reference to + pAux held by the JNI layer will be relinquished regardless of + whether pAux has an xDestroy() method. */ + + public native int xSetAuxdata(@NotNull Fts5Context cx, @Nullable Object pAux); + + public native int xTokenize(@NotNull Fts5Context cx, @NotNull byte[] pText, + @NotNull XTokenizeCallback callback); + + public native Object xUserData(Fts5Context cx); + //^^^ returns the pointer passed as the 3rd arg to the C-level + // fts5_api::xCreateFunction(). +} diff --git a/ext/jni/src/org/sqlite/jni/fts5/Fts5PhraseIter.java b/ext/jni/src/org/sqlite/jni/fts5/Fts5PhraseIter.java new file mode 100644 index 0000000000..5774eb5936 --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/fts5/Fts5PhraseIter.java @@ -0,0 +1,25 @@ +/* +** 2023-08-04 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file is part of the JNI bindings for the sqlite3 C API. +*/ +package org.sqlite.jni.fts5; +import org.sqlite.jni.capi.NativePointerHolder; + +/** + A wrapper for C-level Fts5PhraseIter. They are only modified and + inspected by native-level code. +*/ +public final class Fts5PhraseIter extends NativePointerHolder { + //! Updated and used only by native code. + private long a; + private long b; +} diff --git a/ext/jni/src/org/sqlite/jni/fts5/Fts5Tokenizer.java b/ext/jni/src/org/sqlite/jni/fts5/Fts5Tokenizer.java new file mode 100644 index 0000000000..b72e5d0fc0 --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/fts5/Fts5Tokenizer.java @@ -0,0 +1,31 @@ +/* +** 2023-08-05x +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file is part of the JNI bindings for the sqlite3 C API. +*/ +package org.sqlite.jni.fts5; +import org.sqlite.jni.capi.NativePointerHolder; + +/** + INCOMPLETE AND COMPLETELY UNTESTED. + + A wrapper for communicating C-level (Fts5Tokenizer*) instances with + Java. These wrappers do not own their associated pointer, they + simply provide a type-safe way to communicate it between Java and C + via JNI. + + At the C level, the Fts5Tokenizer type is essentially a void + pointer used specifically for tokenizers. +*/ +public final class Fts5Tokenizer extends NativePointerHolder { + //! Only called from JNI. + private Fts5Tokenizer(){} +} diff --git a/ext/jni/src/org/sqlite/jni/fts5/TesterFts5.java b/ext/jni/src/org/sqlite/jni/fts5/TesterFts5.java new file mode 100644 index 0000000000..4d97ced47d --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/fts5/TesterFts5.java @@ -0,0 +1,841 @@ +/* +** 2023-08-04 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains a set of tests for the sqlite3 JNI bindings. +*/ +package org.sqlite.jni.fts5; +import java.util.*; +import static org.sqlite.jni.capi.CApi.*; +import static org.sqlite.jni.capi.Tester1.*; +import org.sqlite.jni.capi.*; +import java.nio.charset.StandardCharsets; + +public class TesterFts5 { + + private static void test1(){ + final Fts5ExtensionApi fea = Fts5ExtensionApi.getInstance(); + affirm( null != fea ); + affirm( fea.getNativePointer() != 0 ); + affirm( fea == Fts5ExtensionApi.getInstance() )/*singleton*/; + + sqlite3 db = createNewDb(); + fts5_api fApi = fts5_api.getInstanceForDb(db); + affirm( fApi != null ); + affirm( fApi == fts5_api.getInstanceForDb(db) /* singleton per db */ ); + + execSql(db, new String[] { + "CREATE VIRTUAL TABLE ft USING fts5(a, b);", + "INSERT INTO ft(rowid, a, b) VALUES(1, 'X Y', 'Y Z');", + "INSERT INTO ft(rowid, a, b) VALUES(2, 'A Z', 'Y Y');" + }); + + final String pUserData = "This is pUserData"; + final int outputs[] = {0, 0}; + final fts5_extension_function func = new fts5_extension_function(){ + @Override public void call(Fts5ExtensionApi ext, Fts5Context fCx, + sqlite3_context pCx, sqlite3_value argv[]){ + final int nCols = ext.xColumnCount(fCx); + affirm( 2 == nCols ); + affirm( nCols == argv.length ); + affirm( ext.xUserData(fCx) == pUserData ); + final OutputPointer.String op = new OutputPointer.String(); + final OutputPointer.Int32 colsz = new OutputPointer.Int32(); + final OutputPointer.Int64 colTotalSz = new OutputPointer.Int64(); + for(int i = 0; i < nCols; ++i ){ + int rc = ext.xColumnText(fCx, i, op); + affirm( 0 == rc ); + final String val = op.value; + affirm( val.equals(sqlite3_value_text16(argv[i])) ); + rc = ext.xColumnSize(fCx, i, colsz); + affirm( 0==rc ); + affirm( 3==sqlite3_value_bytes(argv[i]) ); + rc = ext.xColumnTotalSize(fCx, i, colTotalSz); + affirm( 0==rc ); + } + ++outputs[0]; + } + public void xDestroy(){ + outputs[1] = 1; + } + }; + + int rc = fApi.xCreateFunction("myaux", pUserData, func); + affirm( 0==rc ); + + affirm( 0==outputs[0] ); + execSql(db, "select myaux(ft,a,b) from ft;"); + affirm( 2==outputs[0] ); + affirm( 0==outputs[1] ); + sqlite3_close_v2(db); + affirm( 1==outputs[1] ); + } + + /* + ** Argument sql is a string containing one or more SQL statements + ** separated by ";" characters. This function executes each of these + ** statements against the database passed as the first argument. If + ** no error occurs, the results of the SQL script are returned as + ** an array of strings. If an error does occur, a RuntimeException is + ** thrown. + */ + private static String[] sqlite3_exec(sqlite3 db, String sql) { + List aOut = new ArrayList<>(); + + /* Iterate through the list of SQL statements. For each, step through + ** it and add any results to the aOut[] array. */ + int rc = sqlite3_prepare_multi(db, sql, new PrepareMultiCallback() { + @Override public int call(sqlite3_stmt pStmt){ + while( SQLITE_ROW==sqlite3_step(pStmt) ){ + int ii; + for(ii=0; ii, ); + */ + class fts5_aux implements fts5_extension_function { + @Override public void call( + Fts5ExtensionApi ext, + Fts5Context fCx, + sqlite3_context pCx, + sqlite3_value argv[] + ){ + if( argv.length>1 ){ + throw new RuntimeException("fts5_aux: wrong number of args"); + } + + boolean bClear = (argv.length==1); + Object obj = ext.xGetAuxdata(fCx, bClear); + if( obj instanceof String ){ + sqlite3_result_text16(pCx, (String)obj); + } + + if( argv.length==1 ){ + String val = sqlite3_value_text16(argv[0]); + if( !val.isEmpty() ){ + ext.xSetAuxdata(fCx, val); + } + } + } + public void xDestroy(){ } + } + + /* + ** fts5_inst(); + ** + ** This is used to test the xInstCount() and xInst() APIs. It returns a + ** text value containing a Tcl list with xInstCount() elements. Each + ** element is itself a list of 3 integers - the phrase number, column + ** number and token offset returned by each call to xInst(). + */ + fts5_extension_function fts5_inst = new fts5_extension_function(){ + @Override public void call( + Fts5ExtensionApi ext, + Fts5Context fCx, + sqlite3_context pCx, + sqlite3_value argv[] + ){ + if( argv.length!=0 ){ + throw new RuntimeException("fts5_inst: wrong number of args"); + } + + OutputPointer.Int32 pnInst = new OutputPointer.Int32(); + OutputPointer.Int32 piPhrase = new OutputPointer.Int32(); + OutputPointer.Int32 piCol = new OutputPointer.Int32(); + OutputPointer.Int32 piOff = new OutputPointer.Int32(); + String ret = ""; + + int rc = ext.xInstCount(fCx, pnInst); + int nInst = pnInst.get(); + int ii; + + for(ii=0; rc==SQLITE_OK && ii0 ) ret += " "; + ret += "{"+piPhrase.get()+" "+piCol.get()+" "+piOff.get()+"}"; + } + + sqlite3_result_text(pCx, ret); + } + public void xDestroy(){ } + }; + + /* + ** fts5_pinst(); + ** + ** Like SQL function fts5_inst(), except using the following + ** + ** xPhraseCount + ** xPhraseFirst + ** xPhraseNext + */ + fts5_extension_function fts5_pinst = new fts5_extension_function(){ + @Override public void call( + Fts5ExtensionApi ext, + Fts5Context fCx, + sqlite3_context pCx, + sqlite3_value argv[] + ){ + if( argv.length!=0 ){ + throw new RuntimeException("fts5_pinst: wrong number of args"); + } + + OutputPointer.Int32 piCol = new OutputPointer.Int32(); + OutputPointer.Int32 piOff = new OutputPointer.Int32(); + String ret = ""; + int rc = SQLITE_OK; + + int nPhrase = ext.xPhraseCount(fCx); + int ii; + + for(ii=0; rc==SQLITE_OK && ii=0; + ext.xPhraseNext(fCx, pIter, piCol, piOff) + ){ + if( !ret.isEmpty() ) ret += " "; + ret += "{"+ii+" "+piCol.get()+" "+piOff.get()+"}"; + } + } + + if( rc!=SQLITE_OK ){ + throw new RuntimeException("fts5_pinst: rc=" + rc); + }else{ + sqlite3_result_text(pCx, ret); + } + } + public void xDestroy(){ } + }; + + /* + ** fts5_pcolinst(); + ** + ** Like SQL function fts5_pinst(), except using the following + ** + ** xPhraseFirstColumn + ** xPhraseNextColumn + */ + fts5_extension_function fts5_pcolinst = new fts5_extension_function(){ + @Override public void call( + Fts5ExtensionApi ext, + Fts5Context fCx, + sqlite3_context pCx, + sqlite3_value argv[] + ){ + if( argv.length!=0 ){ + throw new RuntimeException("fts5_pcolinst: wrong number of args"); + } + + OutputPointer.Int32 piCol = new OutputPointer.Int32(); + String ret = ""; + int rc = SQLITE_OK; + + int nPhrase = ext.xPhraseCount(fCx); + int ii; + + for(ii=0; rc==SQLITE_OK && ii=0; + ext.xPhraseNextColumn(fCx, pIter, piCol) + ){ + if( !ret.isEmpty() ) ret += " "; + ret += "{"+ii+" "+piCol.get()+"}"; + } + } + + if( rc!=SQLITE_OK ){ + throw new RuntimeException("fts5_pcolinst: rc=" + rc); + }else{ + sqlite3_result_text(pCx, ret); + } + } + public void xDestroy(){ } + }; + + /* + ** fts5_rowcount(); + */ + fts5_extension_function fts5_rowcount = new fts5_extension_function(){ + @Override public void call( + Fts5ExtensionApi ext, + Fts5Context fCx, + sqlite3_context pCx, + sqlite3_value argv[] + ){ + if( argv.length!=0 ){ + throw new RuntimeException("fts5_rowcount: wrong number of args"); + } + OutputPointer.Int64 pnRow = new OutputPointer.Int64(); + + int rc = ext.xRowCount(fCx, pnRow); + if( rc==SQLITE_OK ){ + sqlite3_result_int64(pCx, pnRow.get()); + }else{ + throw new RuntimeException("fts5_rowcount: rc=" + rc); + } + } + public void xDestroy(){ } + }; + + /* + ** fts5_phrasesize(); + */ + fts5_extension_function fts5_phrasesize = new fts5_extension_function(){ + @Override public void call( + Fts5ExtensionApi ext, + Fts5Context fCx, + sqlite3_context pCx, + sqlite3_value argv[] + ){ + if( argv.length!=1 ){ + throw new RuntimeException("fts5_phrasesize: wrong number of args"); + } + int iPhrase = sqlite3_value_int(argv[0]); + + int sz = ext.xPhraseSize(fCx, iPhrase); + sqlite3_result_int(pCx, sz); + } + public void xDestroy(){ } + }; + + /* + ** fts5_phrasehits(, ); + ** + ** Use the xQueryPhrase() API to determine how many hits, in total, + ** there are for phrase in the database. + */ + fts5_extension_function fts5_phrasehits = new fts5_extension_function(){ + @Override public void call( + Fts5ExtensionApi ext, + Fts5Context fCx, + sqlite3_context pCx, + sqlite3_value argv[] + ){ + if( argv.length!=1 ){ + throw new RuntimeException("fts5_phrasesize: wrong number of args"); + } + int iPhrase = sqlite3_value_int(argv[0]); + int rc = SQLITE_OK; + + class MyCallback implements Fts5ExtensionApi.XQueryPhraseCallback { + public int nRet = 0; + public int getRet() { return nRet; } + + @Override + public int call(Fts5ExtensionApi fapi, Fts5Context cx){ + OutputPointer.Int32 pnInst = new OutputPointer.Int32(); + int rc = fapi.xInstCount(cx, pnInst); + nRet += pnInst.get(); + return rc; + } + }; + + MyCallback xCall = new MyCallback(); + rc = ext.xQueryPhrase(fCx, iPhrase, xCall); + if( rc!=SQLITE_OK ){ + throw new RuntimeException("fts5_phrasehits: rc=" + rc); + } + sqlite3_result_int(pCx, xCall.getRet()); + } + public void xDestroy(){ } + }; + + /* + ** fts5_tokenize(, ) + */ + fts5_extension_function fts5_tokenize = new fts5_extension_function(){ + @Override public void call( + Fts5ExtensionApi ext, + Fts5Context fCx, + sqlite3_context pCx, + sqlite3_value argv[] + ){ + if( argv.length!=1 ){ + throw new RuntimeException("fts5_tokenize: wrong number of args"); + } + byte[] utf8 = sqlite3_value_text(argv[0]); + int rc = SQLITE_OK; + + class MyCallback implements XTokenizeCallback { + private List myList = new ArrayList<>(); + + public String getval() { + return String.join("+", myList); + } + + @Override + public int call(int tFlags, byte[] txt, int iStart, int iEnd){ + try { + String str = new String(txt, StandardCharsets.UTF_8); + myList.add(str); + } catch (Exception e) { + } + return SQLITE_OK; + } + }; + + MyCallback xCall = new MyCallback(); + ext.xTokenize(fCx, utf8, xCall); + sqlite3_result_text16(pCx, xCall.getval()); + + if( rc!=SQLITE_OK ){ + throw new RuntimeException("fts5_tokenize: rc=" + rc); + } + } + public void xDestroy(){ } + }; + + fts5_api api = fts5_api.getInstanceForDb(db); + api.xCreateFunction("fts5_rowid", fts5_rowid); + api.xCreateFunction("fts5_columncount", fts5_columncount); + api.xCreateFunction("fts5_columnsize", fts5_columnsize); + api.xCreateFunction("fts5_columntext", fts5_columntext); + api.xCreateFunction("fts5_columntotalsize", fts5_columntsize); + + api.xCreateFunction("fts5_aux1", new fts5_aux()); + api.xCreateFunction("fts5_aux2", new fts5_aux()); + + api.xCreateFunction("fts5_inst", fts5_inst); + api.xCreateFunction("fts5_pinst", fts5_pinst); + api.xCreateFunction("fts5_pcolinst", fts5_pcolinst); + api.xCreateFunction("fts5_rowcount", fts5_rowcount); + api.xCreateFunction("fts5_phrasesize", fts5_phrasesize); + api.xCreateFunction("fts5_phrasehits", fts5_phrasehits); + api.xCreateFunction("fts5_tokenize", fts5_tokenize); + } + /* + ** Test of various Fts5ExtensionApi methods + */ + private static void test2(){ + + /* Open db and populate an fts5 table */ + sqlite3 db = createNewDb(); + do_execsql_test(db, + "CREATE VIRTUAL TABLE ft USING fts5(a, b);" + + "INSERT INTO ft(rowid, a, b) VALUES(-9223372036854775808, 'x', 'x');" + + "INSERT INTO ft(rowid, a, b) VALUES(0, 'x', 'x');" + + "INSERT INTO ft(rowid, a, b) VALUES(1, 'x y z', 'x y z');" + + "INSERT INTO ft(rowid, a, b) VALUES(2, 'x y z', 'x z');" + + "INSERT INTO ft(rowid, a, b) VALUES(3, 'x y z', 'x y z');" + + "INSERT INTO ft(rowid, a, b) VALUES(9223372036854775807, 'x', 'x');" + ); + + create_test_functions(db); + + /* Test that fts5_rowid() seems to work */ + do_execsql_test(db, + "SELECT rowid==fts5_rowid(ft) FROM ft('x')", + "[1, 1, 1, 1, 1, 1]" + ); + + /* Test fts5_columncount() */ + do_execsql_test(db, + "SELECT fts5_columncount(ft) FROM ft('x')", + "[2, 2, 2, 2, 2, 2]" + ); + + /* Test fts5_columnsize() */ + do_execsql_test(db, + "SELECT fts5_columnsize(ft, 0) FROM ft('x') ORDER BY rowid", + "[1, 1, 3, 3, 3, 1]" + ); + do_execsql_test(db, + "SELECT fts5_columnsize(ft, 1) FROM ft('x') ORDER BY rowid", + "[1, 1, 3, 2, 3, 1]" + ); + do_execsql_test(db, + "SELECT fts5_columnsize(ft, -1) FROM ft('x') ORDER BY rowid", + "[2, 2, 6, 5, 6, 2]" + ); + + /* Test that xColumnSize() returns SQLITE_RANGE if the column number + ** is out-of range */ + try { + do_execsql_test(db, + "SELECT fts5_columnsize(ft, 2) FROM ft('x') ORDER BY rowid" + ); + } catch( RuntimeException e ){ + affirm( e.getMessage().matches(".*column index out of range") ); + } + + /* Test fts5_columntext() */ + do_execsql_test(db, + "SELECT fts5_columntext(ft, 0) FROM ft('x') ORDER BY rowid", + "[x, x, x y z, x y z, x y z, x]" + ); + do_execsql_test(db, + "SELECT fts5_columntext(ft, 1) FROM ft('x') ORDER BY rowid", + "[x, x, x y z, x z, x y z, x]" + ); + boolean threw = false; + try{ + /* columntext() used to return NULLs when given an out-of bounds column + but now results in a range error. */ + do_execsql_test(db, + "SELECT fts5_columntext(ft, 2) FROM ft('x') ORDER BY rowid", + "[null, null, null, null, null, null]" + ); + }catch(Exception e){ + threw = true; + affirm( e.getMessage().matches(".*column index out of range") ); + } + affirm( threw ); + threw = false; + + /* Test fts5_columntotalsize() */ + do_execsql_test(db, + "SELECT fts5_columntotalsize(ft, 0) FROM ft('x') ORDER BY rowid", + "[12, 12, 12, 12, 12, 12]" + ); + do_execsql_test(db, + "SELECT fts5_columntotalsize(ft, 1) FROM ft('x') ORDER BY rowid", + "[11, 11, 11, 11, 11, 11]" + ); + do_execsql_test(db, + "SELECT fts5_columntotalsize(ft, -1) FROM ft('x') ORDER BY rowid", + "[23, 23, 23, 23, 23, 23]" + ); + + /* Test that xColumnTotalSize() returns SQLITE_RANGE if the column + ** number is out-of range */ + try { + do_execsql_test(db, + "SELECT fts5_columntotalsize(ft, 2) FROM ft('x') ORDER BY rowid" + ); + } catch( RuntimeException e ){ + affirm( e.getMessage().matches(".*column index out of range") ); + } + + do_execsql_test(db, + "SELECT rowid, fts5_rowcount(ft) FROM ft('z')", + "[1, 6, 2, 6, 3, 6]" + ); + + sqlite3_close_v2(db); + } + + /* + ** Test of various Fts5ExtensionApi methods + */ + private static void test3(){ + + /* Open db and populate an fts5 table */ + sqlite3 db = createNewDb(); + do_execsql_test(db, + "CREATE VIRTUAL TABLE ft USING fts5(a, b);" + + "INSERT INTO ft(a, b) VALUES('the one', 1);" + + "INSERT INTO ft(a, b) VALUES('the two', 2);" + + "INSERT INTO ft(a, b) VALUES('the three', 3);" + + "INSERT INTO ft(a, b) VALUES('the four', '');" + ); + create_test_functions(db); + + /* Test fts5_aux1() + fts5_aux2() - users of xGetAuxdata and xSetAuxdata */ + do_execsql_test(db, + "SELECT fts5_aux1(ft, a) FROM ft('the')", + "[null, the one, the two, the three]" + ); + do_execsql_test(db, + "SELECT fts5_aux2(ft, b) FROM ft('the')", + "[null, 1, 2, 3]" + ); + do_execsql_test(db, + "SELECT fts5_aux1(ft, a), fts5_aux2(ft, b) FROM ft('the')", + "[null, null, the one, 1, the two, 2, the three, 3]" + ); + do_execsql_test(db, + "SELECT fts5_aux1(ft, b), fts5_aux1(ft) FROM ft('the')", + "[null, 1, 1, 2, 2, 3, 3, null]" + ); + } + + /* + ** Test of various Fts5ExtensionApi methods + */ + private static void test4(){ + + /* Open db and populate an fts5 table */ + sqlite3 db = createNewDb(); + create_test_functions(db); + do_execsql_test(db, + "CREATE VIRTUAL TABLE ft USING fts5(a, b);" + + "INSERT INTO ft(a, b) VALUES('one two three', 'two three four');" + + "INSERT INTO ft(a, b) VALUES('two three four', 'three four five');" + + "INSERT INTO ft(a, b) VALUES('three four five', 'four five six');" + ); + + + do_execsql_test(db, + "SELECT fts5_inst(ft) FROM ft('two')", + "[{0 0 1} {0 1 0}, {0 0 0}]" + ); + do_execsql_test(db, + "SELECT fts5_inst(ft) FROM ft('four')", + "[{0 1 2}, {0 0 2} {0 1 1}, {0 0 1} {0 1 0}]" + ); + + do_execsql_test(db, + "SELECT fts5_inst(ft) FROM ft('a OR b OR four')", + "[{2 1 2}, {2 0 2} {2 1 1}, {2 0 1} {2 1 0}]" + ); + do_execsql_test(db, + "SELECT fts5_inst(ft) FROM ft('two four')", + "[{0 0 1} {0 1 0} {1 1 2}, {0 0 0} {1 0 2} {1 1 1}]" + ); + + do_execsql_test(db, + "SELECT fts5_pinst(ft) FROM ft('two')", + "[{0 0 1} {0 1 0}, {0 0 0}]" + ); + do_execsql_test(db, + "SELECT fts5_pinst(ft) FROM ft('four')", + "[{0 1 2}, {0 0 2} {0 1 1}, {0 0 1} {0 1 0}]" + ); + do_execsql_test(db, + "SELECT fts5_pinst(ft) FROM ft('a OR b OR four')", + "[{2 1 2}, {2 0 2} {2 1 1}, {2 0 1} {2 1 0}]" + ); + do_execsql_test(db, + "SELECT fts5_pinst(ft) FROM ft('two four')", + "[{0 0 1} {0 1 0} {1 1 2}, {0 0 0} {1 0 2} {1 1 1}]" + ); + + do_execsql_test(db, + "SELECT fts5_pcolinst(ft) FROM ft('two')", + "[{0 0} {0 1}, {0 0}]" + ); + do_execsql_test(db, + "SELECT fts5_pcolinst(ft) FROM ft('four')", + "[{0 1}, {0 0} {0 1}, {0 0} {0 1}]" + ); + do_execsql_test(db, + "SELECT fts5_pcolinst(ft) FROM ft('a OR b OR four')", + "[{2 1}, {2 0} {2 1}, {2 0} {2 1}]" + ); + do_execsql_test(db, + "SELECT fts5_pcolinst(ft) FROM ft('two four')", + "[{0 0} {0 1} {1 1}, {0 0} {1 0} {1 1}]" + ); + + do_execsql_test(db, + "SELECT fts5_phrasesize(ft, 0) FROM ft('four five six') LIMIT 1;", + "[1]" + ); + do_execsql_test(db, + "SELECT fts5_phrasesize(ft, 0) FROM ft('four + five + six') LIMIT 1;", + "[3]" + ); + + + sqlite3_close_v2(db); + } + + private static void test5(){ + /* Open db and populate an fts5 table */ + sqlite3 db = createNewDb(); + create_test_functions(db); + do_execsql_test(db, + "CREATE VIRTUAL TABLE ft USING fts5(x, b);" + + "INSERT INTO ft(x) VALUES('one two three four five six seven eight');" + + "INSERT INTO ft(x) VALUES('one two one four one six one eight');" + + "INSERT INTO ft(x) VALUES('one two three four five six seven eight');" + ); + + do_execsql_test(db, + "SELECT fts5_phrasehits(ft, 0) FROM ft('one') LIMIT 1", + "[6]" + ); + + sqlite3_close_v2(db); + } + + private static void test6(){ + sqlite3 db = createNewDb(); + create_test_functions(db); + do_execsql_test(db, + "CREATE VIRTUAL TABLE ft USING fts5(x, b);" + + "INSERT INTO ft(x) VALUES('one two three four five six seven eight');" + ); + + do_execsql_test(db, + "SELECT fts5_tokenize(ft, 'abc def ghi') FROM ft('one')", + "[abc+def+ghi]" + ); + do_execsql_test(db, + "SELECT fts5_tokenize(ft, 'it''s BEEN a...') FROM ft('one')", + "[it+s+been+a]" + ); + + sqlite3_close_v2(db); + } + + private static synchronized void runTests(){ + test1(); + test2(); + test3(); + test4(); + test5(); + test6(); + } + + public TesterFts5(){ + runTests(); + } +} diff --git a/ext/jni/src/org/sqlite/jni/fts5/XTokenizeCallback.java b/ext/jni/src/org/sqlite/jni/fts5/XTokenizeCallback.java new file mode 100644 index 0000000000..3aa514f314 --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/fts5/XTokenizeCallback.java @@ -0,0 +1,22 @@ +/* +** 2023-08-04 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file is part of the JNI bindings for the sqlite3 C API. +*/ +package org.sqlite.jni.fts5; + + +/** + Callback type for use with xTokenize() variants. +*/ +public interface XTokenizeCallback { + int call(int tFlags, byte[] txt, int iStart, int iEnd); +} diff --git a/ext/jni/src/org/sqlite/jni/fts5/fts5_api.java b/ext/jni/src/org/sqlite/jni/fts5/fts5_api.java new file mode 100644 index 0000000000..d7d2da430d --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/fts5/fts5_api.java @@ -0,0 +1,76 @@ +/* +** 2023-08-05 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file is part of the JNI bindings for the sqlite3 C API. +*/ +package org.sqlite.jni.fts5; +import org.sqlite.jni.annotation.*; +import org.sqlite.jni.capi.*; + +/** + A wrapper for communicating C-level (fts5_api*) instances with + Java. These wrappers do not own their associated pointer, they + simply provide a type-safe way to communicate it between Java and C + via JNI. +*/ +public final class fts5_api extends NativePointerHolder { + /* Only invoked from JNI */ + private fts5_api(){} + + public static final int iVersion = 2; + + /** + Returns the fts5_api instance associated with the given db, or + null if something goes horribly wrong. + */ + public static synchronized native fts5_api getInstanceForDb(@NotNull sqlite3 db); + + public synchronized native int xCreateFunction(@NotNull String name, + @Nullable Object userData, + @NotNull fts5_extension_function xFunction); + + /** + Convenience overload which passes null as the 2nd argument to the + 3-parameter form. + */ + public int xCreateFunction(@NotNull String name, + @NotNull fts5_extension_function xFunction){ + return xCreateFunction(name, null, xFunction); + } + + // /* Create a new auxiliary function */ + // int (*xCreateFunction)( + // fts5_api *pApi, + // const char *zName, + // void *pContext, + // fts5_extension_function xFunction, + // void (*xDestroy)(void*) + // ); + + // Still potentially todo: + + // int (*xCreateTokenizer)( + // fts5_api *pApi, + // const char *zName, + // void *pContext, + // fts5_tokenizer *pTokenizer, + // void (*xDestroy)(void*) + // ); + + // /* Find an existing tokenizer */ + // int (*xFindTokenizer)( + // fts5_api *pApi, + // const char *zName, + // void **ppContext, + // fts5_tokenizer *pTokenizer + // ); + +} diff --git a/ext/jni/src/org/sqlite/jni/fts5/fts5_extension_function.java b/ext/jni/src/org/sqlite/jni/fts5/fts5_extension_function.java new file mode 100644 index 0000000000..6e98f64ff3 --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/fts5/fts5_extension_function.java @@ -0,0 +1,51 @@ +/* +** 2023-08-05 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file is part of the JNI bindings for the sqlite3 C API. +*/ +package org.sqlite.jni.fts5; +import org.sqlite.jni.capi.sqlite3_context; +import org.sqlite.jni.capi.sqlite3_value; + +/** + JNI-level wrapper for C's fts5_extension_function type. +*/ +public interface fts5_extension_function { + // typedef void (*fts5_extension_function)( + // const Fts5ExtensionApi *pApi, /* API offered by current FTS version */ + // Fts5Context *pFts, /* First arg to pass to pApi functions */ + // sqlite3_context *pCtx, /* Context for returning result/error */ + // int nVal, /* Number of values in apVal[] array */ + // sqlite3_value **apVal /* Array of trailing arguments */ + // ); + + /** + The callback implementation, corresponding to the xFunction + argument of C's fts5_api::xCreateFunction(). + */ + void call(Fts5ExtensionApi ext, Fts5Context fCx, + sqlite3_context pCx, sqlite3_value argv[]); + /** + Is called when this function is destroyed by sqlite3. Typically + this function will be empty. + */ + void xDestroy(); + + /** + A base implementation of fts5_extension_function() which has a + no-op xDestroy() method. + */ + abstract class Abstract implements fts5_extension_function { + @Override public abstract void call(Fts5ExtensionApi ext, Fts5Context fCx, + sqlite3_context pCx, sqlite3_value argv[]); + @Override public void xDestroy(){} + } +} diff --git a/ext/jni/src/org/sqlite/jni/fts5/fts5_tokenizer.java b/ext/jni/src/org/sqlite/jni/fts5/fts5_tokenizer.java new file mode 100644 index 0000000000..f4ada4dc30 --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/fts5/fts5_tokenizer.java @@ -0,0 +1,49 @@ +/* +** 2023-08-05 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file is part of the JNI bindings for the sqlite3 C API. +*/ +package org.sqlite.jni.fts5; +import org.sqlite.jni.capi.NativePointerHolder; +import org.sqlite.jni.annotation.NotNull; + +/** + A wrapper for communicating C-level (fts5_tokenizer*) instances with + Java. These wrappers do not own their associated pointer, they + simply provide a type-safe way to communicate it between Java and C + via JNI. +*/ +public final class fts5_tokenizer extends NativePointerHolder { + /* Only invoked by JNI */ + private fts5_tokenizer(){} + + // int (*xCreate)(void*, const char **azArg, int nArg, Fts5Tokenizer **ppOut); + // void (*xDelete)(Fts5Tokenizer*); + + public native int xTokenize(@NotNull Fts5Tokenizer t, int tokFlags, + @NotNull byte pText[], + @NotNull XTokenizeCallback callback); + + + // int (*xTokenize)(Fts5Tokenizer*, + // void *pCtx, + // int flags, /* Mask of FTS5_TOKENIZE_* flags */ + // const char *pText, int nText, + // int (*xToken)( + // void *pCtx, /* Copy of 2nd argument to xTokenize() */ + // int tflags, /* Mask of FTS5_TOKEN_* flags */ + // const char *pToken, /* Pointer to buffer containing token */ + // int nToken, /* Size of token in bytes */ + // int iStart, /* Byte offset of token within input text */ + // int iEnd /* Byte offset of end of token within input text */ + // ) + // ); +} diff --git a/ext/jni/src/org/sqlite/jni/test-script-interpreter.md b/ext/jni/src/org/sqlite/jni/test-script-interpreter.md new file mode 100644 index 0000000000..cc7b7e7f9a --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/test-script-interpreter.md @@ -0,0 +1,270 @@ +# Specifications For A Rudimentary SQLite Test Script Interpreter + +## Overview + +The purpose of the Test Script Interpreter is to read and interpret +script files that contain SQL commands and desired results. The +interpreter will check results and report any discrepancies found. + +The test script files are ASCII text files. The filename always ends with +".test". Each script is evaluated independently; context does not carry +forward from one script to the next. So, for example, the --null command +run in one test script does not cause any changes in the behavior of +subsequent test scripts. All open database connections are closed +at the end of each test script. All database files created by a test +script are deleted when the script finishes. + +## Parsing Rules: + + 1. The test script is read line by line, where a line is a sequence of + characters that runs up to the next '\\n' (0x0a) character or until + the end of the file. There is never a need to read ahead past the + end of the current line. + + 2. If any line contains the string " MODULE_NAME:" (with a space before + the initial "M") or "MIXED_MODULE_NAME:" then that test script is + incompatible with this spec. Processing of the test script should + end immediately. There is no need to read any more of the file. + In verbose mode, the interpreter might choose to emit an informational + messages saying that the test script was abandoned due to an + incompatible module type. + + 3. If any line contains the string "SCRIPT_MODULE_NAME:" then the input + script is known to be of the correct type for this specification and + processing may continue. The "MODULE_NAME" checking in steps 2 and 3 + may optionally be discontinued after sighting a "SCRIPT_MODULE_NAME". + + 4. If any line contains "REQUIRED_PROPERTIES:" and that substring is followed + by any non-whitespace text, then the script is not compatible with this + spec. Processing should stop immediately. In verbose mode, the + interpreter might choose to emit an information message saying that the + test script was abandoned due to unsupported requirement properties. + + 5. If any line begins with the "\|" (0x7c) character, that indicates that + the input script is not compatible with this specification. Processing + of the script should stop immediately. In verbose mode, the interpreter + might choose to emit an informational message indicating that the + test script was abandoned because it contained "a dbtotxt format database + specification". + + 6. Any line that begins with "#" is a C-preprocessor line. The interpreter + described by this spec does not know how to deal with C-preprocessor lines. + Hence, processing should be abandoned. In verbose mode, the interpreter + might emit an informational message similar to + "script NAME abandoned due to C-preprocessor line: ..." + + 7. If a line begins with exactly two minus signs followed by a + lowercase letter, that is a command. Process commands as described + below. + + 8. All other lines should be accumulated into the "input buffer". + The various commands will have access to this input buffer. + Some commands will reset the buffer. + +## Initialization + +The initial state of the interpreter at the start of processing each script +is as if the following command sequence had been run: + +> ~~~ +--close all +--db 0 +--new test.db +--null nil +~~~ + +In words, all database connections are closed except for connection 0 (the +default) which is open on an empty database named "test.db". The string +"nil" is displayed for NULL column values. + +The only context carried forward after the evaluation of one test script +into the evaluation of the next test script is the count of the number of +tests run and the number of failures seen. + +## Commands: + +Each command looks like an SQL comment. The command begins at the left +margin (no leading space) and starts with exactly 2 minus signs ("-"). +The command name consists of lowercase letters and maybe a "-" or two. +Some commands have arguments. + +The arguments are separated from the command name by one or more spaces. + +Commands have access to the input buffer and might reset the input buffer. +The command can also optionally read (and consume) additional text from +script that comes after the command. + +Unknown or unrecognized commands indicate that the script contains features +that are not (yet) supported by this specification. Processing of the +script should terminate immediately. When this happens and when the +interpreter is in a "verbose" mode, the interpreter might choose to emit +an informational message along the lines of "test script NAME abandoned +due to unsupported command: --whatever". + +The initial implementation will only recognize a few commands. Other +commands may be added later. The following is the initial set of +commands: + +### The --testcase command + +Every test case starts with a --testcase command. The --testcase +command resets both the "input buffer" and the "result buffer". The +argument to the --testcase command is the name of the test case. That +test case name is used for logging and debugging and when printing +errors. The input buffer is set to the body of the test case. + +### The --result command + +The --result command tries to execute the text in the input buffer as SQL. +For each row of result coming out of this SQL, the text of that result is +appended to the "result buffer". If a result row contains multiple columns, +the columns are processed from left to right. For each column, text is +appended to the result buffer according to the following rules: + + * If the result buffer already contains some text, append a space. + (In this way, all column values and all row values are separated from + each other by a single space.) + + * If sqlite3_column_text() returns NULL, then append "nil" - or + some other text that is specified by the --null command - and skip + all subsequent rules. + + * If sqlite3_column_text() is an empty string, append `{}` to the + result buffer and skip all subsequent rules. + + * If sqlite3_column_text() does not contain any special + characters, append it to the result buffer without any + formatting and skip all subsequent rules. Special characters are: + 0x00 to 0x20 (inclusive), double-quote (0x22), backslash (0x5c), + curly braces (0x7b and 0x7d). + + * If sqlite3_column_text() does not contains curly braces, then put + the text inside of `{...}` and append it and skip all subsequent rules. + + * Append the text within double-quotes (`"..."`) and within the text + escape '"' and '\\' by prepending a single '\\' and escape any + control characters (characters less than 0x20) using octal notation: + '\\NNN'. + +If an error is encountered while running the SQL, then append the +symbolic C-preprocessor name for the error +code (ex: "SQLITE_CONSTRAINT") as if it were a column value. Then append +the error message text as if it where a column value. Then stop processing. + +After the SQL text has been run, compare the content of the result buffer +against the argument to the --result command and report a testing error if +there are any differences. + +The --result command resets the input buffer, but it does not reset +the result buffer. This distinction does not matter for the --result +command itself, but it is important for related commands like --glob +and --notglob. Sometimes test cases will contains a bunch of SQL +followed by multiple --glob and/or --notglob statements. All of the +globs should be evaluated against the result buffer, but the SQL should +only be run once. This is accomplished by resetting the input buffer +but not the result buffer. + +### The --glob command + +The --glob command works just like --result except that the argument to +--glob is interpreted as a TEST-GLOB pattern and the results are compared +using that glob pattern rather than using strcmp(). Other than that, +the two operate the same. + +The TEST-GLOB pattern is slightly different for a standard GLOB: + + * The '*' character matches zero or more characters. + + * The '?' character matches any single character + + * The '[...]' character sequence machines a single character + in between the brackets. + + * The '#' character matches one or more digits (This is the main + difference between standard unix-glob and TEST-GLOB. unix-glob + does not have this feature. It was added to because it comes + up a lot during SQLite testing.) + +### The --notglob command + +The --notglob command works just like --glob except that it reports an +error if the GLOB does match, rather than if the GLOB does not match. + +### The --oom command + +This command is to be used for out-of-memory testing. It means that +OOM errors should be simulated to ensure that SQLite is able to deal with +them. This command can be silently ignored for now. We might add support +for this later. + +### The --tableresult command + +The --tableresult command works like --glob except that the GLOB pattern +to be matched is taken from subsequent lines of the input script up to +the next --end. Every span of one or more whitespace characters in this +pattern text is collapsed into a single space (0x20). +Leading and trailing whitespace are removed from the pattern. +The --end that ends the GLOB pattern is not part of the GLOB pattern, but +the --end is consumed from the script input. + +### The --new and --open commands + +The --new and --open commands cause a database file to be opened. +The name of the file is the argument to the command. The --new command +opens an initially empty database (it deletes the file before opening it) +whereas the --open command opens an existing database if it already +exists. + +### The --db command + +The script interpreter can have up to 7 different SQLite database +connections open at a time. The --db command is used to switch between +them. The argument to --db is an integer between 0 and 6 that selects +which database connection to use moving forward. + +### The --close command + +The --close command causes an existing database connection to close. +This command is a no-op if the database connection is not currently +open. There can be up to 7 different database connections, numbered 0 +through 6. The number of the database connection to close is an +argument to the --close command, which will fail if an out-of-range +value is provided. Or if the argument to --close is "all" then all +open database connections are closed. If passed no argument, the +currently-active database is assumed. + +### The --null command + +The NULL command changes the text that is used to represent SQL NULL +values in the result buffer. + +### The --run command + +The --run command executes text in the input buffer as if it where SQL. +However, nothing is added to the result buffer. Any output from the SQL +is silently ignored. Errors in the SQL are silently ignored. + +The --run command normally executes the SQL in the current database +connection. However, if --run has an argument that is an integer between +0 and 6 then the SQL is run in the alternative database connection specified +by that argument. + +### The --json and --json-block commands + +The --json and --json-block commands work like --result and --tableresult, +respectively. The difference is that column values are appended to the +result buffer literally, without ever enclosing the values in `{...}` or +`"..."` and without escaping any characters in the column value and comparison +is always an exact strcmp() not a GLOB. + +### The --print command + +The --print command emits both its arguments and its body (if any) to +stdout, indenting each line of output. + +### The --column-names command + +The --column-names command requires 0 or 1 as an argument, to disable +resp. enable it, and modifies SQL execution to include column names +in output. When this option is on, each column value emitted gets +prefixed by its column name, with a single space between them. diff --git a/ext/jni/src/org/sqlite/jni/wrapper1/AggregateFunction.java b/ext/jni/src/org/sqlite/jni/wrapper1/AggregateFunction.java new file mode 100644 index 0000000000..fc63b53542 --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/wrapper1/AggregateFunction.java @@ -0,0 +1,144 @@ +/* +** 2023-10-16 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file is part of the wrapper1 interface for sqlite3. +*/ +package org.sqlite.jni.wrapper1; + +/** + EXPERIMENTAL/INCOMPLETE/UNTESTED + + A SqlFunction implementation for aggregate functions. The T type + represents the type of data accumulated by this aggregate while it + works. e.g. a SUM()-like UDF might use Integer or Long and a + CONCAT()-like UDF might use a StringBuilder or a List. +*/ +public abstract class AggregateFunction implements SqlFunction { + + /** + As for the xStep() argument of the C API's + sqlite3_create_function(). If this function throws, the + exception is reported via sqlite3_result_error(). + */ + public abstract void xStep(SqlFunction.Arguments args); + + /** + As for the xFinal() argument of the C API's + sqlite3_create_function(). If this function throws, it is + translated into sqlite3_result_error(). + + Note that the passed-in object will not actually contain any + arguments for xFinal() but will contain the context object needed + for setting the call's result or error state. + */ + public abstract void xFinal(SqlFunction.Arguments args); + + /** + Optionally override to be notified when the UDF is finalized by + SQLite. + */ + public void xDestroy() {} + + /** + PerContextState assists aggregate and window functions in + managing their accumulator state across calls to the UDF's + callbacks. + +

    T must be of a type which can be legally stored as a value in + java.util.HashMap. + +

    If a given aggregate or window function is called multiple times + in a single SQL statement, e.g. SELECT MYFUNC(A), MYFUNC(B)..., + then the clients need some way of knowing which call is which so + that they can map their state between their various UDF callbacks + and reset it via xFinal(). This class takes care of such + mappings. + +

    This class works by mapping + sqlite3_context.getAggregateContext() to a single piece of + state, of a client-defined type (the T part of this class), which + persists across a "matching set" of the UDF's callbacks. + +

    This class is a helper providing commonly-needed functionality + - it is not required for use with aggregate or window functions. + Client UDFs are free to perform such mappings using custom + approaches. The provided {@link AggregateFunction} and {@link + WindowFunction} classes use this. + */ + public static final class PerContextState { + private final java.util.Map> map + = new java.util.HashMap<>(); + + /** + Should be called from a UDF's xStep(), xValue(), and xInverse() + methods, passing it that method's first argument and an initial + value for the persistent state. If there is currently no + mapping for the given context within the map, one is created + using the given initial value, else the existing one is used + and the 2nd argument is ignored. It returns a ValueHolder + which can be used to modify that state directly without + requiring that the client update the underlying map's entry. + +

    The caller is obligated to eventually call + takeAggregateState() to clear the mapping. + */ + public ValueHolder getAggregateState(SqlFunction.Arguments args, T initialValue){ + final Long key = args.getContext().getAggregateContext(true); + ValueHolder rc = null==key ? null : map.get(key); + if( null==rc ){ + map.put(key, rc = new ValueHolder<>(initialValue)); + } + return rc; + } + + /** + Should be called from a UDF's xFinal() method and passed that + method's first argument. This function removes the value + associated with with the arguments' aggregate context from the + map and returns it, returning null if no other UDF method has + been called to set up such a mapping. The latter condition will + be the case if a UDF is used in a statement which has no result + rows. + */ + public T takeAggregateState(SqlFunction.Arguments args){ + final ValueHolder h = map.remove(args.getContext().getAggregateContext(false)); + return null==h ? null : h.value; + } + } + + /** Per-invocation state for the UDF. */ + private final PerContextState map = new PerContextState<>(); + + /** + To be called from the implementation's xStep() method, as well + as the xValue() and xInverse() methods of the {@link WindowFunction} + subclass, to fetch the current per-call UDF state. On the + first call to this method for any given sqlite3_context + argument, the context is set to the given initial value. On all other + calls, the 2nd argument is ignored. + + @see SQLFunction.PerContextState#getAggregateState + */ + protected final ValueHolder getAggregateState(SqlFunction.Arguments args, T initialValue){ + return map.getAggregateState(args, initialValue); + } + + /** + To be called from the implementation's xFinal() method to fetch + the final state of the UDF and remove its mapping. + + see SQLFunction.PerContextState#takeAggregateState + */ + protected final T takeAggregateState(SqlFunction.Arguments args){ + return map.takeAggregateState(args); + } + +} diff --git a/ext/jni/src/org/sqlite/jni/wrapper1/ScalarFunction.java b/ext/jni/src/org/sqlite/jni/wrapper1/ScalarFunction.java new file mode 100644 index 0000000000..c616ae7393 --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/wrapper1/ScalarFunction.java @@ -0,0 +1,33 @@ +/* +** 2023-10-16 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file is part of the wrapper1 interface for sqlite3. +*/ +package org.sqlite.jni.wrapper1; + +/** + The SqlFunction type for scalar SQL functions. +*/ +public abstract class ScalarFunction implements SqlFunction { + /** + As for the xFunc() argument of the C API's + sqlite3_create_function(). If this function throws, it is + translated into an sqlite3_result_error(). + */ + public abstract void xFunc(SqlFunction.Arguments args); + + /** + Optionally override to be notified when the UDF is finalized by + SQLite. This default implementation does nothing. + */ + public void xDestroy() {} + +} diff --git a/ext/jni/src/org/sqlite/jni/wrapper1/SqlFunction.java b/ext/jni/src/org/sqlite/jni/wrapper1/SqlFunction.java new file mode 100644 index 0000000000..bb0fd0ccd4 --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/wrapper1/SqlFunction.java @@ -0,0 +1,318 @@ +/* +** 2023-10-16 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file is part of the wrapper1 interface for sqlite3. +*/ +package org.sqlite.jni.wrapper1; +import org.sqlite.jni.capi.CApi; +import org.sqlite.jni.capi.sqlite3_context; +import org.sqlite.jni.capi.sqlite3_value; + +/** + Base marker interface for SQLite's three types of User-Defined SQL + Functions (UDFs): Scalar, Aggregate, and Window functions. +*/ +public interface SqlFunction { + + int DETERMINISTIC = CApi.SQLITE_DETERMINISTIC; + int INNOCUOUS = CApi.SQLITE_INNOCUOUS; + int DIRECTONLY = CApi.SQLITE_DIRECTONLY; + int SUBTYPE = CApi.SQLITE_SUBTYPE; + int RESULT_SUBTYPE = CApi.SQLITE_RESULT_SUBTYPE; + int UTF8 = CApi.SQLITE_UTF8; + int UTF16 = CApi.SQLITE_UTF16; + + /** + The Arguments type is an abstraction on top of the lower-level + UDF function argument types. It provides _most_ of the functionality + of the lower-level interface, insofar as possible without "leaking" + those types into this API. + */ + final class Arguments implements Iterable{ + private final sqlite3_context cx; + private final sqlite3_value args[]; + public final int length; + + /** + Must be passed the context and arguments for the UDF call this + object is wrapping. Intended to be used by internal proxy + classes which "convert" the lower-level interface into this + package's higher-level interface, e.g. ScalarAdapter and + AggregateAdapter. + + Passing null for the args is equivalent to passing a length-0 + array. + */ + Arguments(sqlite3_context cx, sqlite3_value args[]){ + this.cx = cx; + this.args = args==null ? new sqlite3_value[0] : args; + this.length = this.args.length; + } + + /** + Returns the sqlite3_value at the given argument index or throws + an IllegalArgumentException exception if ndx is out of range. + */ + private sqlite3_value valueAt(int ndx){ + if(ndx<0 || ndx>=args.length){ + throw new IllegalArgumentException( + "SQL function argument index "+ndx+" is out of range." + ); + } + return args[ndx]; + } + + //! Returns the underlying sqlite3_context for these arguments. + sqlite3_context getContext(){return cx;} + + /** + Returns the Sqlite (db) object associated with this UDF call, + or null if the UDF is somehow called without such an object or + the db has been closed in an untimely manner (e.g. closed by a + UDF call). + */ + public Sqlite getDb(){ + return Sqlite.fromNative( CApi.sqlite3_context_db_handle(cx) ); + } + + public int getArgCount(){ return args.length; } + + public int getInt(int argNdx){return CApi.sqlite3_value_int(valueAt(argNdx));} + public long getInt64(int argNdx){return CApi.sqlite3_value_int64(valueAt(argNdx));} + public double getDouble(int argNdx){return CApi.sqlite3_value_double(valueAt(argNdx));} + public byte[] getBlob(int argNdx){return CApi.sqlite3_value_blob(valueAt(argNdx));} + public byte[] getText(int argNdx){return CApi.sqlite3_value_text(valueAt(argNdx));} + public String getText16(int argNdx){return CApi.sqlite3_value_text16(valueAt(argNdx));} + public int getBytes(int argNdx){return CApi.sqlite3_value_bytes(valueAt(argNdx));} + public int getBytes16(int argNdx){return CApi.sqlite3_value_bytes16(valueAt(argNdx));} + public Object getObject(int argNdx){return CApi.sqlite3_value_java_object(valueAt(argNdx));} + public T getObject(int argNdx, Class type){ + return CApi.sqlite3_value_java_object(valueAt(argNdx), type); + } + + public int getType(int argNdx){return CApi.sqlite3_value_type(valueAt(argNdx));} + public int getSubtype(int argNdx){return CApi.sqlite3_value_subtype(valueAt(argNdx));} + public int getNumericType(int argNdx){return CApi.sqlite3_value_numeric_type(valueAt(argNdx));} + public int getNoChange(int argNdx){return CApi.sqlite3_value_nochange(valueAt(argNdx));} + public boolean getFromBind(int argNdx){return CApi.sqlite3_value_frombind(valueAt(argNdx));} + public int getEncoding(int argNdx){return CApi.sqlite3_value_encoding(valueAt(argNdx));} + + public void resultInt(int v){ CApi.sqlite3_result_int(cx, v); } + public void resultInt64(long v){ CApi.sqlite3_result_int64(cx, v); } + public void resultDouble(double v){ CApi.sqlite3_result_double(cx, v); } + public void resultError(String msg){CApi.sqlite3_result_error(cx, msg);} + public void resultError(Exception e){CApi.sqlite3_result_error(cx, e);} + public void resultErrorTooBig(){CApi.sqlite3_result_error_toobig(cx);} + public void resultErrorCode(int rc){CApi.sqlite3_result_error_code(cx, rc);} + public void resultObject(Object o){CApi.sqlite3_result_java_object(cx, o);} + public void resultNull(){CApi.sqlite3_result_null(cx);} + /** + Analog to sqlite3_result_value(), using the Value object at the + given argument index. + */ + public void resultArg(int argNdx){CApi.sqlite3_result_value(cx, valueAt(argNdx));} + public void resultSubtype(int subtype){CApi.sqlite3_result_subtype(cx, subtype);} + public void resultZeroBlob(long n){ + // Throw on error? If n is too big, + // sqlite3_result_error_toobig() is automatically called. + CApi.sqlite3_result_zeroblob64(cx, n); + } + + public void resultBlob(byte[] blob){CApi.sqlite3_result_blob(cx, blob);} + public void resultText(byte[] utf8){CApi.sqlite3_result_text(cx, utf8);} + public void resultText(String txt){CApi.sqlite3_result_text(cx, txt);} + public void resultText16(byte[] utf16){CApi.sqlite3_result_text16(cx, utf16);} + public void resultText16(String txt){CApi.sqlite3_result_text16(cx, txt);} + + /** + Callbacks should invoke this on OOM errors, instead of throwing + OutOfMemoryError, because the latter cannot be propagated + through the C API. + */ + public void resultNoMem(){CApi.sqlite3_result_error_nomem(cx);} + + /** + Analog to sqlite3_set_auxdata() but throws if argNdx is out of + range. + */ + public void setAuxData(int argNdx, Object o){ + /* From the API docs: https://sqlite.org/c3ref/get_auxdata.html + + The value of the N parameter to these interfaces should be + non-negative. Future enhancements may make use of negative N + values to define new kinds of function caching behavior. + */ + valueAt(argNdx); + CApi.sqlite3_set_auxdata(cx, argNdx, o); + } + + /** + Analog to sqlite3_get_auxdata() but throws if argNdx is out of + range. + */ + public Object getAuxData(int argNdx){ + valueAt(argNdx); + return CApi.sqlite3_get_auxdata(cx, argNdx); + } + + /** + Represents a single SqlFunction argument. Primarily intended + for use with the Arguments class's Iterable interface. + */ + public final static class Arg { + private final Arguments a; + private final int ndx; + /* Only for use by the Arguments class. */ + private Arg(Arguments a, int ndx){ + this.a = a; + this.ndx = ndx; + } + /** Returns this argument's index in its parent argument list. */ + public int getIndex(){return ndx;} + public int getInt(){return a.getInt(ndx);} + public long getInt64(){return a.getInt64(ndx);} + public double getDouble(){return a.getDouble(ndx);} + public byte[] getBlob(){return a.getBlob(ndx);} + public byte[] getText(){return a.getText(ndx);} + public String getText16(){return a.getText16(ndx);} + public int getBytes(){return a.getBytes(ndx);} + public int getBytes16(){return a.getBytes16(ndx);} + public Object getObject(){return a.getObject(ndx);} + public T getObject(Class type){ return a.getObject(ndx, type); } + public int getType(){return a.getType(ndx);} + public Object getAuxData(){return a.getAuxData(ndx);} + public void setAuxData(Object o){a.setAuxData(ndx, o);} + } + + @Override + public java.util.Iterator iterator(){ + final Arg[] proxies = new Arg[args.length]; + for( int i = 0; i < args.length; ++i ){ + proxies[i] = new Arg(this, i); + } + return java.util.Arrays.stream(proxies).iterator(); + } + + } + + /** + Internal-use adapter for wrapping this package's ScalarFunction + for use with the org.sqlite.jni.capi.ScalarFunction interface. + */ + final class ScalarAdapter extends org.sqlite.jni.capi.ScalarFunction { + private final ScalarFunction impl; + ScalarAdapter(ScalarFunction impl){ + this.impl = impl; + } + /** + Proxies this.impl.xFunc(), adapting the call arguments to that + function's signature. If the proxy throws, it's translated to + sqlite_result_error() with the exception's message. + */ + public void xFunc(sqlite3_context cx, sqlite3_value[] args){ + try{ + impl.xFunc( new SqlFunction.Arguments(cx, args) ); + }catch(Exception e){ + CApi.sqlite3_result_error(cx, e); + } + } + + public void xDestroy(){ + impl.xDestroy(); + } + } + + /** + Internal-use adapter for wrapping this package's AggregateFunction + for use with the org.sqlite.jni.capi.AggregateFunction interface. + */ + class AggregateAdapter extends org.sqlite.jni.capi.AggregateFunction { + /*cannot be final without duplicating the whole body in WindowAdapter*/ + private final AggregateFunction impl; + AggregateAdapter(AggregateFunction impl){ + this.impl = impl; + } + + /** + Proxies this.impl.xStep(), adapting the call arguments to that + function's signature. If the proxied function throws, it is + translated to sqlite_result_error() with the exception's + message. + */ + public void xStep(sqlite3_context cx, sqlite3_value[] args){ + try{ + impl.xStep( new SqlFunction.Arguments(cx, args) ); + }catch(Exception e){ + CApi.sqlite3_result_error(cx, e); + } + } + + /** + As for the xFinal() argument of the C API's + sqlite3_create_function(). If the proxied function throws, it + is translated into a sqlite3_result_error(). + */ + public void xFinal(sqlite3_context cx){ + try{ + impl.xFinal( new SqlFunction.Arguments(cx, null) ); + }catch(Exception e){ + CApi.sqlite3_result_error(cx, e); + } + } + + public void xDestroy(){ + impl.xDestroy(); + } + } + + /** + Internal-use adapter for wrapping this package's WindowFunction + for use with the org.sqlite.jni.capi.WindowFunction interface. + */ + final class WindowAdapter extends AggregateAdapter { + private final WindowFunction impl; + WindowAdapter(WindowFunction impl){ + super(impl); + this.impl = impl; + } + + /** + Proxies this.impl.xInverse(), adapting the call arguments to that + function's signature. If the proxied function throws, it is + translated to sqlite_result_error() with the exception's + message. + */ + public void xInverse(sqlite3_context cx, sqlite3_value[] args){ + try{ + impl.xInverse( new SqlFunction.Arguments(cx, args) ); + }catch(Exception e){ + CApi.sqlite3_result_error(cx, e); + } + } + + /** + As for the xValue() argument of the C API's sqlite3_create_window_function(). + If the proxied function throws, it is translated into a sqlite3_result_error(). + */ + public void xValue(sqlite3_context cx){ + try{ + impl.xValue( new SqlFunction.Arguments(cx, null) ); + }catch(Exception e){ + CApi.sqlite3_result_error(cx, e); + } + } + + public void xDestroy(){ + impl.xDestroy(); + } + } + +} diff --git a/ext/jni/src/org/sqlite/jni/wrapper1/Sqlite.java b/ext/jni/src/org/sqlite/jni/wrapper1/Sqlite.java new file mode 100644 index 0000000000..d259e0ce62 --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/wrapper1/Sqlite.java @@ -0,0 +1,1994 @@ +/* +** 2023-10-09 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file is part of the wrapper1 interface for sqlite3. +*/ +package org.sqlite.jni.wrapper1; +import java.nio.charset.StandardCharsets; +import org.sqlite.jni.capi.CApi; +import org.sqlite.jni.capi.sqlite3; +import org.sqlite.jni.capi.sqlite3_stmt; +import org.sqlite.jni.capi.sqlite3_backup; +import org.sqlite.jni.capi.sqlite3_blob; +import org.sqlite.jni.capi.OutputPointer; + +/** + This class represents a database connection, analog to the C-side + sqlite3 class but with added argument validation, exceptions, and + similar "smoothing of sharp edges" to make the API safe to use from + Java. It also acts as a namespace for other types for which + individual instances are tied to a specific database connection. +*/ +public final class Sqlite implements AutoCloseable { + private sqlite3 db; + private static final boolean JNI_SUPPORTS_NIO = + CApi.sqlite3_jni_supports_nio(); + + // Result codes + public static final int OK = CApi.SQLITE_OK; + public static final int ERROR = CApi.SQLITE_ERROR; + public static final int INTERNAL = CApi.SQLITE_INTERNAL; + public static final int PERM = CApi.SQLITE_PERM; + public static final int ABORT = CApi.SQLITE_ABORT; + public static final int BUSY = CApi.SQLITE_BUSY; + public static final int LOCKED = CApi.SQLITE_LOCKED; + public static final int NOMEM = CApi.SQLITE_NOMEM; + public static final int READONLY = CApi.SQLITE_READONLY; + public static final int INTERRUPT = CApi.SQLITE_INTERRUPT; + public static final int IOERR = CApi.SQLITE_IOERR; + public static final int CORRUPT = CApi.SQLITE_CORRUPT; + public static final int NOTFOUND = CApi.SQLITE_NOTFOUND; + public static final int FULL = CApi.SQLITE_FULL; + public static final int CANTOPEN = CApi.SQLITE_CANTOPEN; + public static final int PROTOCOL = CApi.SQLITE_PROTOCOL; + public static final int EMPTY = CApi.SQLITE_EMPTY; + public static final int SCHEMA = CApi.SQLITE_SCHEMA; + public static final int TOOBIG = CApi.SQLITE_TOOBIG; + public static final int CONSTRAINT = CApi. SQLITE_CONSTRAINT; + public static final int MISMATCH = CApi.SQLITE_MISMATCH; + public static final int MISUSE = CApi.SQLITE_MISUSE; + public static final int NOLFS = CApi.SQLITE_NOLFS; + public static final int AUTH = CApi.SQLITE_AUTH; + public static final int FORMAT = CApi.SQLITE_FORMAT; + public static final int RANGE = CApi.SQLITE_RANGE; + public static final int NOTADB = CApi.SQLITE_NOTADB; + public static final int NOTICE = CApi.SQLITE_NOTICE; + public static final int WARNING = CApi.SQLITE_WARNING; + public static final int ROW = CApi.SQLITE_ROW; + public static final int DONE = CApi.SQLITE_DONE; + public static final int ERROR_MISSING_COLLSEQ = CApi.SQLITE_ERROR_MISSING_COLLSEQ; + public static final int ERROR_RETRY = CApi.SQLITE_ERROR_RETRY; + public static final int ERROR_SNAPSHOT = CApi.SQLITE_ERROR_SNAPSHOT; + public static final int IOERR_READ = CApi.SQLITE_IOERR_READ; + public static final int IOERR_SHORT_READ = CApi.SQLITE_IOERR_SHORT_READ; + public static final int IOERR_WRITE = CApi.SQLITE_IOERR_WRITE; + public static final int IOERR_FSYNC = CApi.SQLITE_IOERR_FSYNC; + public static final int IOERR_DIR_FSYNC = CApi.SQLITE_IOERR_DIR_FSYNC; + public static final int IOERR_TRUNCATE = CApi.SQLITE_IOERR_TRUNCATE; + public static final int IOERR_FSTAT = CApi.SQLITE_IOERR_FSTAT; + public static final int IOERR_UNLOCK = CApi.SQLITE_IOERR_UNLOCK; + public static final int IOERR_RDLOCK = CApi.SQLITE_IOERR_RDLOCK; + public static final int IOERR_DELETE = CApi.SQLITE_IOERR_DELETE; + public static final int IOERR_BLOCKED = CApi.SQLITE_IOERR_BLOCKED; + public static final int IOERR_NOMEM = CApi.SQLITE_IOERR_NOMEM; + public static final int IOERR_ACCESS = CApi.SQLITE_IOERR_ACCESS; + public static final int IOERR_CHECKRESERVEDLOCK = CApi.SQLITE_IOERR_CHECKRESERVEDLOCK; + public static final int IOERR_LOCK = CApi.SQLITE_IOERR_LOCK; + public static final int IOERR_CLOSE = CApi.SQLITE_IOERR_CLOSE; + public static final int IOERR_DIR_CLOSE = CApi.SQLITE_IOERR_DIR_CLOSE; + public static final int IOERR_SHMOPEN = CApi.SQLITE_IOERR_SHMOPEN; + public static final int IOERR_SHMSIZE = CApi.SQLITE_IOERR_SHMSIZE; + public static final int IOERR_SHMLOCK = CApi.SQLITE_IOERR_SHMLOCK; + public static final int IOERR_SHMMAP = CApi.SQLITE_IOERR_SHMMAP; + public static final int IOERR_SEEK = CApi.SQLITE_IOERR_SEEK; + public static final int IOERR_DELETE_NOENT = CApi.SQLITE_IOERR_DELETE_NOENT; + public static final int IOERR_MMAP = CApi.SQLITE_IOERR_MMAP; + public static final int IOERR_GETTEMPPATH = CApi.SQLITE_IOERR_GETTEMPPATH; + public static final int IOERR_CONVPATH = CApi.SQLITE_IOERR_CONVPATH; + public static final int IOERR_VNODE = CApi.SQLITE_IOERR_VNODE; + public static final int IOERR_AUTH = CApi.SQLITE_IOERR_AUTH; + public static final int IOERR_BEGIN_ATOMIC = CApi.SQLITE_IOERR_BEGIN_ATOMIC; + public static final int IOERR_COMMIT_ATOMIC = CApi.SQLITE_IOERR_COMMIT_ATOMIC; + public static final int IOERR_ROLLBACK_ATOMIC = CApi.SQLITE_IOERR_ROLLBACK_ATOMIC; + public static final int IOERR_DATA = CApi.SQLITE_IOERR_DATA; + public static final int IOERR_CORRUPTFS = CApi.SQLITE_IOERR_CORRUPTFS; + public static final int LOCKED_SHAREDCACHE = CApi.SQLITE_LOCKED_SHAREDCACHE; + public static final int LOCKED_VTAB = CApi.SQLITE_LOCKED_VTAB; + public static final int BUSY_RECOVERY = CApi.SQLITE_BUSY_RECOVERY; + public static final int BUSY_SNAPSHOT = CApi.SQLITE_BUSY_SNAPSHOT; + public static final int BUSY_TIMEOUT = CApi.SQLITE_BUSY_TIMEOUT; + public static final int CANTOPEN_NOTEMPDIR = CApi.SQLITE_CANTOPEN_NOTEMPDIR; + public static final int CANTOPEN_ISDIR = CApi.SQLITE_CANTOPEN_ISDIR; + public static final int CANTOPEN_FULLPATH = CApi.SQLITE_CANTOPEN_FULLPATH; + public static final int CANTOPEN_CONVPATH = CApi.SQLITE_CANTOPEN_CONVPATH; + public static final int CANTOPEN_SYMLINK = CApi.SQLITE_CANTOPEN_SYMLINK; + public static final int CORRUPT_VTAB = CApi.SQLITE_CORRUPT_VTAB; + public static final int CORRUPT_SEQUENCE = CApi.SQLITE_CORRUPT_SEQUENCE; + public static final int CORRUPT_INDEX = CApi.SQLITE_CORRUPT_INDEX; + public static final int READONLY_RECOVERY = CApi.SQLITE_READONLY_RECOVERY; + public static final int READONLY_CANTLOCK = CApi.SQLITE_READONLY_CANTLOCK; + public static final int READONLY_ROLLBACK = CApi.SQLITE_READONLY_ROLLBACK; + public static final int READONLY_DBMOVED = CApi.SQLITE_READONLY_DBMOVED; + public static final int READONLY_CANTINIT = CApi.SQLITE_READONLY_CANTINIT; + public static final int READONLY_DIRECTORY = CApi.SQLITE_READONLY_DIRECTORY; + public static final int ABORT_ROLLBACK = CApi.SQLITE_ABORT_ROLLBACK; + public static final int CONSTRAINT_CHECK = CApi.SQLITE_CONSTRAINT_CHECK; + public static final int CONSTRAINT_COMMITHOOK = CApi.SQLITE_CONSTRAINT_COMMITHOOK; + public static final int CONSTRAINT_FOREIGNKEY = CApi.SQLITE_CONSTRAINT_FOREIGNKEY; + public static final int CONSTRAINT_FUNCTION = CApi.SQLITE_CONSTRAINT_FUNCTION; + public static final int CONSTRAINT_NOTNULL = CApi.SQLITE_CONSTRAINT_NOTNULL; + public static final int CONSTRAINT_PRIMARYKEY = CApi.SQLITE_CONSTRAINT_PRIMARYKEY; + public static final int CONSTRAINT_TRIGGER = CApi.SQLITE_CONSTRAINT_TRIGGER; + public static final int CONSTRAINT_UNIQUE = CApi.SQLITE_CONSTRAINT_UNIQUE; + public static final int CONSTRAINT_VTAB = CApi.SQLITE_CONSTRAINT_VTAB; + public static final int CONSTRAINT_ROWID = CApi.SQLITE_CONSTRAINT_ROWID; + public static final int CONSTRAINT_PINNED = CApi.SQLITE_CONSTRAINT_PINNED; + public static final int CONSTRAINT_DATATYPE = CApi.SQLITE_CONSTRAINT_DATATYPE; + public static final int NOTICE_RECOVER_WAL = CApi.SQLITE_NOTICE_RECOVER_WAL; + public static final int NOTICE_RECOVER_ROLLBACK = CApi.SQLITE_NOTICE_RECOVER_ROLLBACK; + public static final int WARNING_AUTOINDEX = CApi.SQLITE_WARNING_AUTOINDEX; + public static final int AUTH_USER = CApi.SQLITE_AUTH_USER; + public static final int OK_LOAD_PERMANENTLY = CApi.SQLITE_OK_LOAD_PERMANENTLY; + + // sqlite3_open() flags + public static final int OPEN_READWRITE = CApi.SQLITE_OPEN_READWRITE; + public static final int OPEN_CREATE = CApi.SQLITE_OPEN_CREATE; + public static final int OPEN_EXRESCODE = CApi.SQLITE_OPEN_EXRESCODE; + + // transaction state + public static final int TXN_NONE = CApi.SQLITE_TXN_NONE; + public static final int TXN_READ = CApi.SQLITE_TXN_READ; + public static final int TXN_WRITE = CApi.SQLITE_TXN_WRITE; + + // sqlite3_status() ops + public static final int STATUS_MEMORY_USED = CApi.SQLITE_STATUS_MEMORY_USED; + public static final int STATUS_PAGECACHE_USED = CApi.SQLITE_STATUS_PAGECACHE_USED; + public static final int STATUS_PAGECACHE_OVERFLOW = CApi.SQLITE_STATUS_PAGECACHE_OVERFLOW; + public static final int STATUS_MALLOC_SIZE = CApi.SQLITE_STATUS_MALLOC_SIZE; + public static final int STATUS_PARSER_STACK = CApi.SQLITE_STATUS_PARSER_STACK; + public static final int STATUS_PAGECACHE_SIZE = CApi.SQLITE_STATUS_PAGECACHE_SIZE; + public static final int STATUS_MALLOC_COUNT = CApi.SQLITE_STATUS_MALLOC_COUNT; + + // sqlite3_db_status() ops + public static final int DBSTATUS_LOOKASIDE_USED = CApi.SQLITE_DBSTATUS_LOOKASIDE_USED; + public static final int DBSTATUS_CACHE_USED = CApi.SQLITE_DBSTATUS_CACHE_USED; + public static final int DBSTATUS_SCHEMA_USED = CApi.SQLITE_DBSTATUS_SCHEMA_USED; + public static final int DBSTATUS_STMT_USED = CApi.SQLITE_DBSTATUS_STMT_USED; + public static final int DBSTATUS_LOOKASIDE_HIT = CApi.SQLITE_DBSTATUS_LOOKASIDE_HIT; + public static final int DBSTATUS_LOOKASIDE_MISS_SIZE = CApi.SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE; + public static final int DBSTATUS_LOOKASIDE_MISS_FULL = CApi.SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL; + public static final int DBSTATUS_CACHE_HIT = CApi.SQLITE_DBSTATUS_CACHE_HIT; + public static final int DBSTATUS_CACHE_MISS = CApi.SQLITE_DBSTATUS_CACHE_MISS; + public static final int DBSTATUS_CACHE_WRITE = CApi.SQLITE_DBSTATUS_CACHE_WRITE; + public static final int DBSTATUS_DEFERRED_FKS = CApi.SQLITE_DBSTATUS_DEFERRED_FKS; + public static final int DBSTATUS_CACHE_USED_SHARED = CApi.SQLITE_DBSTATUS_CACHE_USED_SHARED; + public static final int DBSTATUS_CACHE_SPILL = CApi.SQLITE_DBSTATUS_CACHE_SPILL; + + // Limits + public static final int LIMIT_LENGTH = CApi.SQLITE_LIMIT_LENGTH; + public static final int LIMIT_SQL_LENGTH = CApi.SQLITE_LIMIT_SQL_LENGTH; + public static final int LIMIT_COLUMN = CApi.SQLITE_LIMIT_COLUMN; + public static final int LIMIT_EXPR_DEPTH = CApi.SQLITE_LIMIT_EXPR_DEPTH; + public static final int LIMIT_COMPOUND_SELECT = CApi.SQLITE_LIMIT_COMPOUND_SELECT; + public static final int LIMIT_VDBE_OP = CApi.SQLITE_LIMIT_VDBE_OP; + public static final int LIMIT_FUNCTION_ARG = CApi.SQLITE_LIMIT_FUNCTION_ARG; + public static final int LIMIT_ATTACHED = CApi.SQLITE_LIMIT_ATTACHED; + public static final int LIMIT_LIKE_PATTERN_LENGTH = CApi.SQLITE_LIMIT_LIKE_PATTERN_LENGTH; + public static final int LIMIT_VARIABLE_NUMBER = CApi.SQLITE_LIMIT_VARIABLE_NUMBER; + public static final int LIMIT_TRIGGER_DEPTH = CApi.SQLITE_LIMIT_TRIGGER_DEPTH; + public static final int LIMIT_WORKER_THREADS = CApi.SQLITE_LIMIT_WORKER_THREADS; + + // sqlite3_prepare_v3() flags + public static final int PREPARE_PERSISTENT = CApi.SQLITE_PREPARE_PERSISTENT; + public static final int PREPARE_NO_VTAB = CApi.SQLITE_PREPARE_NO_VTAB; + + // sqlite3_trace_v2() flags + public static final int TRACE_STMT = CApi.SQLITE_TRACE_STMT; + public static final int TRACE_PROFILE = CApi.SQLITE_TRACE_PROFILE; + public static final int TRACE_ROW = CApi.SQLITE_TRACE_ROW; + public static final int TRACE_CLOSE = CApi.SQLITE_TRACE_CLOSE; + public static final int TRACE_ALL = TRACE_STMT | TRACE_PROFILE | TRACE_ROW | TRACE_CLOSE; + + // sqlite3_db_config() ops + public static final int DBCONFIG_ENABLE_FKEY = CApi.SQLITE_DBCONFIG_ENABLE_FKEY; + public static final int DBCONFIG_ENABLE_TRIGGER = CApi.SQLITE_DBCONFIG_ENABLE_TRIGGER; + public static final int DBCONFIG_ENABLE_FTS3_TOKENIZER = CApi.SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER; + public static final int DBCONFIG_ENABLE_LOAD_EXTENSION = CApi.SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION; + public static final int DBCONFIG_NO_CKPT_ON_CLOSE = CApi.SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE; + public static final int DBCONFIG_ENABLE_QPSG = CApi.SQLITE_DBCONFIG_ENABLE_QPSG; + public static final int DBCONFIG_TRIGGER_EQP = CApi.SQLITE_DBCONFIG_TRIGGER_EQP; + public static final int DBCONFIG_RESET_DATABASE = CApi.SQLITE_DBCONFIG_RESET_DATABASE; + public static final int DBCONFIG_DEFENSIVE = CApi.SQLITE_DBCONFIG_DEFENSIVE; + public static final int DBCONFIG_WRITABLE_SCHEMA = CApi.SQLITE_DBCONFIG_WRITABLE_SCHEMA; + public static final int DBCONFIG_LEGACY_ALTER_TABLE = CApi.SQLITE_DBCONFIG_LEGACY_ALTER_TABLE; + public static final int DBCONFIG_DQS_DML = CApi.SQLITE_DBCONFIG_DQS_DML; + public static final int DBCONFIG_DQS_DDL = CApi.SQLITE_DBCONFIG_DQS_DDL; + public static final int DBCONFIG_ENABLE_VIEW = CApi.SQLITE_DBCONFIG_ENABLE_VIEW; + public static final int DBCONFIG_LEGACY_FILE_FORMAT = CApi.SQLITE_DBCONFIG_LEGACY_FILE_FORMAT; + public static final int DBCONFIG_TRUSTED_SCHEMA = CApi.SQLITE_DBCONFIG_TRUSTED_SCHEMA; + public static final int DBCONFIG_STMT_SCANSTATUS = CApi.SQLITE_DBCONFIG_STMT_SCANSTATUS; + public static final int DBCONFIG_REVERSE_SCANORDER = CApi.SQLITE_DBCONFIG_REVERSE_SCANORDER; + + // sqlite3_config() ops + public static final int CONFIG_SINGLETHREAD = CApi.SQLITE_CONFIG_SINGLETHREAD; + public static final int CONFIG_MULTITHREAD = CApi.SQLITE_CONFIG_MULTITHREAD; + public static final int CONFIG_SERIALIZED = CApi.SQLITE_CONFIG_SERIALIZED; + + // Encodings + public static final int UTF8 = CApi.SQLITE_UTF8; + public static final int UTF16 = CApi.SQLITE_UTF16; + public static final int UTF16LE = CApi.SQLITE_UTF16LE; + public static final int UTF16BE = CApi.SQLITE_UTF16BE; + /* We elide the UTF16_ALIGNED from this interface because it + is irrelevant for the Java interface. */ + + // SQL data type IDs + public static final int INTEGER = CApi.SQLITE_INTEGER; + public static final int FLOAT = CApi.SQLITE_FLOAT; + public static final int TEXT = CApi.SQLITE_TEXT; + public static final int BLOB = CApi.SQLITE_BLOB; + public static final int NULL = CApi.SQLITE_NULL; + + // Authorizer codes. + public static final int DENY = CApi.SQLITE_DENY; + public static final int IGNORE = CApi.SQLITE_IGNORE; + public static final int CREATE_INDEX = CApi.SQLITE_CREATE_INDEX; + public static final int CREATE_TABLE = CApi.SQLITE_CREATE_TABLE; + public static final int CREATE_TEMP_INDEX = CApi.SQLITE_CREATE_TEMP_INDEX; + public static final int CREATE_TEMP_TABLE = CApi.SQLITE_CREATE_TEMP_TABLE; + public static final int CREATE_TEMP_TRIGGER = CApi.SQLITE_CREATE_TEMP_TRIGGER; + public static final int CREATE_TEMP_VIEW = CApi.SQLITE_CREATE_TEMP_VIEW; + public static final int CREATE_TRIGGER = CApi.SQLITE_CREATE_TRIGGER; + public static final int CREATE_VIEW = CApi.SQLITE_CREATE_VIEW; + public static final int DELETE = CApi.SQLITE_DELETE; + public static final int DROP_INDEX = CApi.SQLITE_DROP_INDEX; + public static final int DROP_TABLE = CApi.SQLITE_DROP_TABLE; + public static final int DROP_TEMP_INDEX = CApi.SQLITE_DROP_TEMP_INDEX; + public static final int DROP_TEMP_TABLE = CApi.SQLITE_DROP_TEMP_TABLE; + public static final int DROP_TEMP_TRIGGER = CApi.SQLITE_DROP_TEMP_TRIGGER; + public static final int DROP_TEMP_VIEW = CApi.SQLITE_DROP_TEMP_VIEW; + public static final int DROP_TRIGGER = CApi.SQLITE_DROP_TRIGGER; + public static final int DROP_VIEW = CApi.SQLITE_DROP_VIEW; + public static final int INSERT = CApi.SQLITE_INSERT; + public static final int PRAGMA = CApi.SQLITE_PRAGMA; + public static final int READ = CApi.SQLITE_READ; + public static final int SELECT = CApi.SQLITE_SELECT; + public static final int TRANSACTION = CApi.SQLITE_TRANSACTION; + public static final int UPDATE = CApi.SQLITE_UPDATE; + public static final int ATTACH = CApi.SQLITE_ATTACH; + public static final int DETACH = CApi.SQLITE_DETACH; + public static final int ALTER_TABLE = CApi.SQLITE_ALTER_TABLE; + public static final int REINDEX = CApi.SQLITE_REINDEX; + public static final int ANALYZE = CApi.SQLITE_ANALYZE; + public static final int CREATE_VTABLE = CApi.SQLITE_CREATE_VTABLE; + public static final int DROP_VTABLE = CApi.SQLITE_DROP_VTABLE; + public static final int FUNCTION = CApi.SQLITE_FUNCTION; + public static final int SAVEPOINT = CApi.SQLITE_SAVEPOINT; + public static final int RECURSIVE = CApi.SQLITE_RECURSIVE; + + //! Used only by the open() factory functions. + private Sqlite(sqlite3 db){ + this.db = db; + } + + /** Maps org.sqlite.jni.capi.sqlite3 to Sqlite instances. */ + private static final java.util.Map nativeToWrapper + = new java.util.HashMap<>(); + + + /** + When any given thread is done using the SQLite library, calling + this will free up any native-side resources which may be + associated specifically with that thread. This is not strictly + necessary, in particular in applications which only use SQLite + from a single thread, but may help free some otherwise errant + resources. + + Calling into SQLite from a given thread after this has been + called in that thread is harmless. The library will simply start + to re-cache certain state for that thread. + + Contrariwise, failing to call this will effectively leak a small + amount of cached state for the thread, which may add up to + significant amounts if the application uses SQLite from many + threads. + + This must never be called while actively using SQLite from this + thread, e.g. from within a query loop or a callback which is + operating on behalf of the library. + */ + static void uncacheThread(){ + CApi.sqlite3_java_uncache_thread(); + } + + /** + Returns the Sqlite object associated with the given sqlite3 + object, or null if there is no such mapping. + */ + static Sqlite fromNative(sqlite3 low){ + synchronized(nativeToWrapper){ + return nativeToWrapper.get(low); + } + } + + /** + Returns a newly-opened db connection or throws SqliteException if + opening fails. All arguments are as documented for + sqlite3_open_v2(). + + Design question: do we want static factory functions or should + this be reformulated as a constructor? + */ + public static Sqlite open(String filename, int flags, String vfsName){ + final OutputPointer.sqlite3 out = new OutputPointer.sqlite3(); + final int rc = CApi.sqlite3_open_v2(filename, out, flags, vfsName); + final sqlite3 n = out.take(); + if( 0!=rc ){ + if( null==n ) throw new SqliteException(rc); + final SqliteException ex = new SqliteException(n); + n.close(); + throw ex; + } + final Sqlite rv = new Sqlite(n); + synchronized(nativeToWrapper){ + nativeToWrapper.put(n, rv); + } + runAutoExtensions(rv); + return rv; + } + + public static Sqlite open(String filename, int flags){ + return open(filename, flags, null); + } + + public static Sqlite open(String filename){ + return open(filename, OPEN_READWRITE|OPEN_CREATE, null); + } + + public static String libVersion(){ + return CApi.sqlite3_libversion(); + } + + public static int libVersionNumber(){ + return CApi.sqlite3_libversion_number(); + } + + public static String libSourceId(){ + return CApi.sqlite3_sourceid(); + } + + /** + Returns the value of the native library's build-time value of the + SQLITE_THREADSAFE build option. + */ + public static int libThreadsafe(){ + return CApi.sqlite3_threadsafe(); + } + + /** + Analog to sqlite3_compileoption_get(). + */ + public static String compileOptionGet(int n){ + return CApi.sqlite3_compileoption_get(n); + } + + /** + Analog to sqlite3_compileoption_used(). + */ + public static boolean compileOptionUsed(String optName){ + return CApi.sqlite3_compileoption_used(optName); + } + + private static final boolean hasNormalizeSql = + compileOptionUsed("ENABLE_NORMALIZE"); + + private static final boolean hasSqlLog = + compileOptionUsed("ENABLE_SQLLOG"); + + /** + Throws UnsupportedOperationException if check is false. + flag is expected to be the name of an SQLITE_ENABLE_... + build flag. + */ + private static void checkSupported(boolean check, String flag){ + if( !check ){ + throw new UnsupportedOperationException( + "Library was built without "+flag + ); + } + } + + /** + Analog to sqlite3_complete(). + */ + public static boolean isCompleteStatement(String sql){ + switch(CApi.sqlite3_complete(sql)){ + case 0: return false; + case CApi.SQLITE_MISUSE: + throw new IllegalArgumentException("Input may not be null."); + case CApi.SQLITE_NOMEM: + throw new OutOfMemoryError(); + default: + return true; + } + } + + public static int keywordCount(){ + return CApi.sqlite3_keyword_count(); + } + + public static boolean keywordCheck(String word){ + return CApi.sqlite3_keyword_check(word); + } + + public static String keywordName(int index){ + return CApi.sqlite3_keyword_name(index); + } + + public static boolean strglob(String glob, String txt){ + return 0==CApi.sqlite3_strglob(glob, txt); + } + + public static boolean strlike(String glob, String txt, char escChar){ + return 0==CApi.sqlite3_strlike(glob, txt, escChar); + } + + /** + Output object for use with status() and libStatus(). + */ + public static final class Status { + /** The current value for the requested status() or libStatus() metric. */ + long current; + /** The peak value for the requested status() or libStatus() metric. */ + long peak; + } + + /** + As per sqlite3_status64(), but returns its current and high-water + results as a Status object. Throws if the first argument is + not one of the STATUS_... constants. + */ + public static Status libStatus(int op, boolean resetStats){ + org.sqlite.jni.capi.OutputPointer.Int64 pCurrent = + new org.sqlite.jni.capi.OutputPointer.Int64(); + org.sqlite.jni.capi.OutputPointer.Int64 pHighwater = + new org.sqlite.jni.capi.OutputPointer.Int64(); + checkRcStatic( CApi.sqlite3_status64(op, pCurrent, pHighwater, resetStats) ); + final Status s = new Status(); + s.current = pCurrent.value; + s.peak = pHighwater.value; + return s; + } + + /** + As per sqlite3_db_status(), but returns its current and + high-water results as a Status object. Throws if the first + argument is not one of the DBSTATUS_... constants or on any other + misuse. + */ + public Status status(int op, boolean resetStats){ + org.sqlite.jni.capi.OutputPointer.Int32 pCurrent = + new org.sqlite.jni.capi.OutputPointer.Int32(); + org.sqlite.jni.capi.OutputPointer.Int32 pHighwater = + new org.sqlite.jni.capi.OutputPointer.Int32(); + checkRc( CApi.sqlite3_db_status(thisDb(), op, pCurrent, pHighwater, resetStats) ); + final Status s = new Status(); + s.current = pCurrent.value; + s.peak = pHighwater.value; + return s; + } + + @Override public void close(){ + if(null!=this.db){ + synchronized(nativeToWrapper){ + nativeToWrapper.remove(this.db); + } + this.db.close(); + this.db = null; + } + } + + /** + Returns this object's underlying native db handle, or null if + this instance has been closed. This is very specifically not + public. + */ + sqlite3 nativeHandle(){ return this.db; } + + private sqlite3 thisDb(){ + if( null==db || 0==db.getNativePointer() ){ + throw new IllegalArgumentException("This database instance is closed."); + } + return this.db; + } + + // private byte[] stringToUtf8(String s){ + // return s==null ? null : s.getBytes(StandardCharsets.UTF_8); + // } + + /** + If rc!=0, throws an SqliteException. If this db is currently + opened and has non-0 sqlite3_errcode(), the error state is + extracted from it, else only the string form of rc is used. It is + the caller's responsibility to filter out non-error codes such as + SQLITE_ROW and SQLITE_DONE before calling this. + + As a special case, if rc is SQLITE_NOMEM, an OutOfMemoryError is + thrown. + */ + private void checkRc(int rc){ + if( 0!=rc ){ + if( CApi.SQLITE_NOMEM==rc ){ + throw new OutOfMemoryError(); + }else if( null==db || 0==CApi.sqlite3_errcode(db) ){ + throw new SqliteException(rc); + }else{ + throw new SqliteException(db); + } + } + } + + /** + Like checkRc() but behaves as if that function were + called with a null db object. + */ + private static void checkRcStatic(int rc){ + if( 0!=rc ){ + if( CApi.SQLITE_NOMEM==rc ){ + throw new OutOfMemoryError(); + }else{ + throw new SqliteException(rc); + } + } + } + + /** + Toggles the use of extended result codes on or off. By default + they are turned off, but they can be enabled by default by + including the OPEN_EXRESCODE flag when opening a database. + + Because this API reports db-side errors using exceptions, + enabling this may change the values returned by + SqliteException.errcode(). + */ + public void useExtendedResultCodes(boolean on){ + checkRc( CApi.sqlite3_extended_result_codes(thisDb(), on) ); + } + + /** + Analog to sqlite3_prepare_v3(), this prepares the first SQL + statement from the given input string and returns it as a + Stmt. It throws an SqliteException if preparation fails or an + IllegalArgumentException if the input is empty (e.g. contains + only comments or whitespace). + + The first argument must be SQL input in UTF-8 encoding. + + prepFlags must be 0 or a bitmask of the PREPARE_... constants. + + For processing multiple statements from a single input, use + prepareMulti(). + + Design note: though the C-level API succeeds with a null + statement object for empty inputs, that approach is cumbersome to + use in higher-level APIs because every prepared statement has to + be checked for null before using it. + */ + public Stmt prepare(byte utf8Sql[], int prepFlags){ + final OutputPointer.sqlite3_stmt out = new OutputPointer.sqlite3_stmt(); + final int rc = CApi.sqlite3_prepare_v3(thisDb(), utf8Sql, prepFlags, out); + checkRc(rc); + final sqlite3_stmt q = out.take(); + if( null==q ){ + /* The C-level API treats input which is devoid of SQL + statements (e.g. all comments or an empty string) as success + but returns a NULL sqlite3_stmt object. In higher-level APIs, + wrapping a "successful NULL" object that way is tedious to + use because it forces clients and/or wrapper-level code to + check for that unusual case. In practice, higher-level + bindings are generally better-served by treating empty SQL + input as an error. */ + throw new IllegalArgumentException("Input contains no SQL statements."); + } + return new Stmt(this, q); + } + + /** + Equivalent to prepare(X, prepFlags), where X is + sql.getBytes(StandardCharsets.UTF_8). + */ + public Stmt prepare(String sql, int prepFlags){ + return prepare( sql.getBytes(StandardCharsets.UTF_8), prepFlags ); + } + + /** + Equivalent to prepare(sql, 0). + */ + public Stmt prepare(String sql){ + return prepare(sql, 0); + } + + + /** + Callback type for use with prepareMulti(). + */ + public interface PrepareMulti { + /** + Gets passed a Stmt which it may handle in arbitrary ways. + Ownership of st is passed to this function. It must throw on + error. + */ + void call(Sqlite.Stmt st); + } + + /** + A PrepareMulti implementation which calls another PrepareMulti + object and then finalizes its statement. + */ + public static class PrepareMultiFinalize implements PrepareMulti { + private final PrepareMulti pm; + /** + Proxies the given PrepareMulti via this object's call() method. + */ + public PrepareMultiFinalize(PrepareMulti proxy){ + this.pm = proxy; + } + /** + Passes st to the call() method of the object this one proxies, + then finalizes st, propagating any exceptions from call() after + finalizing st. + */ + @Override public void call(Stmt st){ + try{ pm.call(st); } + finally{ st.finalizeStmt(); } + } + } + + /** + Equivalent to prepareMulti(sql,0,visitor). + */ + public void prepareMulti(String sql, PrepareMulti visitor){ + prepareMulti( sql, 0, visitor ); + } + + /** + Equivalent to prepareMulti(X,prepFlags,visitor), where X is + sql.getBytes(StandardCharsets.UTF_8). + */ + public void prepareMulti(String sql, int prepFlags, PrepareMulti visitor){ + prepareMulti(sql.getBytes(StandardCharsets.UTF_8), prepFlags, visitor); + } + + /** + A variant of prepare() which can handle multiple SQL statements + in a single input string. For each statement in the given string, + the statement is passed to visitor.call() a single time, passing + ownership of the statement to that function. This function does + not step() or close() statements - those operations are left to + caller or the visitor function. + + Unlike prepare(), this function does not fail if the input + contains only whitespace or SQL comments. In that case it is up + to the caller to arrange for that to be an error (if desired). + + PrepareMultiFinalize offers a proxy which finalizes each + statement after it is passed to another client-defined visitor. + + Be aware that certain legal SQL constructs may fail in the + preparation phase, before the corresponding statement can be + stepped. Most notably, authorizer checks which disallow access to + something in a statement behave that way. + */ + public void prepareMulti(byte sqlUtf8[], int prepFlags, PrepareMulti visitor){ + int pos = 0, n = 1; + byte[] sqlChunk = sqlUtf8; + final org.sqlite.jni.capi.OutputPointer.sqlite3_stmt outStmt = + new org.sqlite.jni.capi.OutputPointer.sqlite3_stmt(); + final org.sqlite.jni.capi.OutputPointer.Int32 oTail = + new org.sqlite.jni.capi.OutputPointer.Int32(); + while( pos < sqlChunk.length ){ + sqlite3_stmt stmt; + if( pos>0 ){ + sqlChunk = java.util.Arrays.copyOfRange(sqlChunk, pos, sqlChunk.length); + } + if( 0==sqlChunk.length ) break; + checkRc( + CApi.sqlite3_prepare_v3(db, sqlChunk, prepFlags, outStmt, oTail) + ); + pos = oTail.value; + stmt = outStmt.take(); + if( null==stmt ){ + /* empty statement, e.g. only comments or whitespace, was parsed. */ + continue; + } + visitor.call(new Stmt(this, stmt)); + } + } + + public void createFunction(String name, int nArg, int eTextRep, ScalarFunction f){ + int rc = CApi.sqlite3_create_function(thisDb(), name, nArg, eTextRep, + new SqlFunction.ScalarAdapter(f)); + if( 0!=rc ) throw new SqliteException(db); + } + + public void createFunction(String name, int nArg, ScalarFunction f){ + this.createFunction(name, nArg, CApi.SQLITE_UTF8, f); + } + + public void createFunction(String name, int nArg, int eTextRep, AggregateFunction f){ + int rc = CApi.sqlite3_create_function(thisDb(), name, nArg, eTextRep, + new SqlFunction.AggregateAdapter(f)); + if( 0!=rc ) throw new SqliteException(db); + } + + public void createFunction(String name, int nArg, AggregateFunction f){ + this.createFunction(name, nArg, CApi.SQLITE_UTF8, f); + } + + public void createFunction(String name, int nArg, int eTextRep, WindowFunction f){ + int rc = CApi.sqlite3_create_function(thisDb(), name, nArg, eTextRep, + new SqlFunction.WindowAdapter(f)); + if( 0!=rc ) throw new SqliteException(db); + } + + public void createFunction(String name, int nArg, WindowFunction f){ + this.createFunction(name, nArg, CApi.SQLITE_UTF8, f); + } + + public long changes(){ + return CApi.sqlite3_changes64(thisDb()); + } + + public long totalChanges(){ + return CApi.sqlite3_total_changes64(thisDb()); + } + + public long lastInsertRowId(){ + return CApi.sqlite3_last_insert_rowid(thisDb()); + } + + public void setLastInsertRowId(long rowId){ + CApi.sqlite3_set_last_insert_rowid(thisDb(), rowId); + } + + public void interrupt(){ + CApi.sqlite3_interrupt(thisDb()); + } + + public boolean isInterrupted(){ + return CApi.sqlite3_is_interrupted(thisDb()); + } + + public boolean isAutoCommit(){ + return CApi.sqlite3_get_autocommit(thisDb()); + } + + /** + Analog to sqlite3_txn_state(). Returns one of TXN_NONE, TXN_READ, + or TXN_WRITE to denote this database's current transaction state + for the given schema name (or the most restrictive state of any + schema if zSchema is null). + */ + public int transactionState(String zSchema){ + return CApi.sqlite3_txn_state(thisDb(), zSchema); + } + + /** + Analog to sqlite3_db_name(). Returns null if passed an unknown + index. + */ + public String dbName(int dbNdx){ + return CApi.sqlite3_db_name(thisDb(), dbNdx); + } + + /** + Analog to sqlite3_db_filename(). Returns null if passed an + unknown db name. + */ + public String dbFileName(String dbName){ + return CApi.sqlite3_db_filename(thisDb(), dbName); + } + + /** + Analog to sqlite3_db_config() for the call forms which take one + of the boolean-type db configuration flags (namely the + DBCONFIG_... constants defined in this class). On success it + returns the result of that underlying call. Throws on error. + */ + public boolean dbConfig(int op, boolean on){ + org.sqlite.jni.capi.OutputPointer.Int32 pOut = + new org.sqlite.jni.capi.OutputPointer.Int32(); + checkRc( CApi.sqlite3_db_config(thisDb(), op, on ? 1 : 0, pOut) ); + return pOut.get()!=0; + } + + /** + Analog to the variant of sqlite3_db_config() for configuring the + SQLITE_DBCONFIG_MAINDBNAME option. Throws on error. + */ + public void setMainDbName(String name){ + checkRc( + CApi.sqlite3_db_config(thisDb(), CApi.SQLITE_DBCONFIG_MAINDBNAME, + name) + ); + } + + /** + Analog to sqlite3_db_readonly() but throws an SqliteException + with result code SQLITE_NOTFOUND if given an unknown database + name. + */ + public boolean readOnly(String dbName){ + final int rc = CApi.sqlite3_db_readonly(thisDb(), dbName); + if( 0==rc ) return false; + else if( rc>0 ) return true; + throw new SqliteException(CApi.SQLITE_NOTFOUND); + } + + /** + Analog to sqlite3_db_release_memory(). + */ + public void releaseMemory(){ + CApi.sqlite3_db_release_memory(thisDb()); + } + + /** + Analog to sqlite3_release_memory(). + */ + public static int libReleaseMemory(int n){ + return CApi.sqlite3_release_memory(n); + } + + /** + Analog to sqlite3_limit(). limitId must be one of the + LIMIT_... constants. + + Returns the old limit for the given option. If newLimit is + negative, it returns the old limit without modifying the limit. + + If sqlite3_limit() returns a negative value, this function throws + an SqliteException with the SQLITE_RANGE result code but no + further error info (because that case does not qualify as a + db-level error). Such errors may indicate an invalid argument + value or an invalid range for newLimit (the underlying function + does not differentiate between those). + */ + public int limit(int limitId, int newLimit){ + final int rc = CApi.sqlite3_limit(thisDb(), limitId, newLimit); + if( rc<0 ){ + throw new SqliteException(CApi.SQLITE_RANGE); + } + return rc; + } + + /** + Analog to sqlite3_errstr(). + */ + static String errstr(int resultCode){ + return CApi.sqlite3_errstr(resultCode); + } + + /** + A wrapper object for use with tableColumnMetadata(). They are + created and populated only via that interface. + */ + public final class TableColumnMetadata { + Boolean pNotNull = null; + Boolean pPrimaryKey = null; + Boolean pAutoinc = null; + String pzCollSeq = null; + String pzDataType = null; + + private TableColumnMetadata(){} + + public String getDataType(){ return pzDataType; } + public String getCollation(){ return pzCollSeq; } + public boolean isNotNull(){ return pNotNull; } + public boolean isPrimaryKey(){ return pPrimaryKey; } + public boolean isAutoincrement(){ return pAutoinc; } + } + + /** + Returns data about a database, table, and (optionally) column + (which may be null), as per sqlite3_table_column_metadata(). + Throws if passed invalid arguments, else returns the result as a + new TableColumnMetadata object. + */ + TableColumnMetadata tableColumnMetadata( + String zDbName, String zTableName, String zColumnName + ){ + org.sqlite.jni.capi.OutputPointer.String pzDataType + = new org.sqlite.jni.capi.OutputPointer.String(); + org.sqlite.jni.capi.OutputPointer.String pzCollSeq + = new org.sqlite.jni.capi.OutputPointer.String(); + org.sqlite.jni.capi.OutputPointer.Bool pNotNull + = new org.sqlite.jni.capi.OutputPointer.Bool(); + org.sqlite.jni.capi.OutputPointer.Bool pPrimaryKey + = new org.sqlite.jni.capi.OutputPointer.Bool(); + org.sqlite.jni.capi.OutputPointer.Bool pAutoinc + = new org.sqlite.jni.capi.OutputPointer.Bool(); + final int rc = CApi.sqlite3_table_column_metadata( + thisDb(), zDbName, zTableName, zColumnName, + pzDataType, pzCollSeq, pNotNull, pPrimaryKey, pAutoinc + ); + checkRc(rc); + TableColumnMetadata rv = new TableColumnMetadata(); + rv.pzDataType = pzDataType.value; + rv.pzCollSeq = pzCollSeq.value; + rv.pNotNull = pNotNull.value; + rv.pPrimaryKey = pPrimaryKey.value; + rv.pAutoinc = pAutoinc.value; + return rv; + } + + public interface TraceCallback { + /** + Called by sqlite3 for various tracing operations, as per + sqlite3_trace_v2(). Note that this interface elides the 2nd + argument to the native trace callback, as that role is better + filled by instance-local state. + +

    These callbacks may throw, in which case their exceptions are + converted to C-level error information. + +

    The 2nd argument to this function, if non-null, will be a an + Sqlite or Sqlite.Stmt object, depending on the first argument + (see below). + +

    The final argument to this function is the "X" argument + documented for sqlite3_trace() and sqlite3_trace_v2(). Its type + depends on value of the first argument: + +

    - SQLITE_TRACE_STMT: pNative is a Sqlite.Stmt. pX is a String + containing the prepared SQL. + +

    - SQLITE_TRACE_PROFILE: pNative is a sqlite3_stmt. pX is a Long + holding an approximate number of nanoseconds the statement took + to run. + +

    - SQLITE_TRACE_ROW: pNative is a sqlite3_stmt. pX is null. + +

    - SQLITE_TRACE_CLOSE: pNative is a sqlite3. pX is null. + */ + void call(int traceFlag, Object pNative, Object pX); + } + + /** + Analog to sqlite3_trace_v2(). traceMask must be a mask of the + TRACE_... constants. Pass a null callback to remove tracing. + + Throws on error. + */ + public void trace(int traceMask, TraceCallback callback){ + final Sqlite self = this; + final org.sqlite.jni.capi.TraceV2Callback tc = + (null==callback) ? null : new org.sqlite.jni.capi.TraceV2Callback(){ + @SuppressWarnings("unchecked") + @Override public int call(int flag, Object pNative, Object pX){ + switch(flag){ + case TRACE_ROW: + case TRACE_PROFILE: + case TRACE_STMT: + callback.call(flag, Sqlite.Stmt.fromNative((sqlite3_stmt)pNative), pX); + break; + case TRACE_CLOSE: + callback.call(flag, self, pX); + break; + } + return 0; + } + }; + checkRc( CApi.sqlite3_trace_v2(thisDb(), traceMask, tc) ); + } + + /** + Corresponds to the sqlite3_stmt class. Use Sqlite.prepare() to + create new instances. + */ + public static final class Stmt implements AutoCloseable { + private Sqlite _db; + private sqlite3_stmt stmt; + + /** Only called by the prepare() factory functions. */ + Stmt(Sqlite db, sqlite3_stmt stmt){ + this._db = db; + this.stmt = stmt; + synchronized(nativeToWrapper){ + nativeToWrapper.put(this.stmt, this); + } + } + + sqlite3_stmt nativeHandle(){ + return stmt; + } + + /** Maps org.sqlite.jni.capi.sqlite3_stmt to Stmt instances. */ + private static final java.util.Map nativeToWrapper + = new java.util.HashMap<>(); + + /** + Returns the Stmt object associated with the given sqlite3_stmt + object, or null if there is no such mapping. + */ + static Stmt fromNative(sqlite3_stmt low){ + synchronized(nativeToWrapper){ + return nativeToWrapper.get(low); + } + } + + /** + If this statement is still opened, its low-level handle is + returned, else an IllegalArgumentException is thrown. + */ + private sqlite3_stmt thisStmt(){ + if( null==stmt || 0==stmt.getNativePointer() ){ + throw new IllegalArgumentException("This Stmt has been finalized."); + } + return stmt; + } + + /** Throws if n is out of range of this statement's result column + count. Intended to be used by the columnXyz() methods. */ + private sqlite3_stmt checkColIndex(int n){ + if(n<0 || n>=columnCount()){ + throw new IllegalArgumentException("Column index "+n+" is out of range."); + } + return thisStmt(); + } + + /** + Corresponds to sqlite3_finalize(), but we cannot override the + name finalize() here because this one requires a different + signature. It does not throw on error here because "destructors + do not throw." If it returns non-0, the object is still + finalized, but the result code is an indication that something + went wrong in a prior call into the statement's API, as + documented for sqlite3_finalize(). + */ + public int finalizeStmt(){ + int rc = 0; + if( null!=stmt ){ + synchronized(nativeToWrapper){ + nativeToWrapper.remove(this.stmt); + } + CApi.sqlite3_finalize(stmt); + stmt = null; + _db = null; + } + return rc; + } + + @Override public void close(){ + finalizeStmt(); + } + + /** + Throws if rc is any value other than 0, SQLITE_ROW, or + SQLITE_DONE, else returns rc. Error state for the exception is + extracted from this statement object (if it's opened) or the + string form of rc. + */ + private int checkRc(int rc){ + switch(rc){ + case 0: + case CApi.SQLITE_ROW: + case CApi.SQLITE_DONE: return rc; + default: + if( null==stmt ) throw new SqliteException(rc); + else throw new SqliteException(this); + } + } + + /** + Works like sqlite3_step() but returns true for SQLITE_ROW, + false for SQLITE_DONE, and throws SqliteException for any other + result. + */ + public boolean step(){ + switch(checkRc(CApi.sqlite3_step(thisStmt()))){ + case CApi.SQLITE_ROW: return true; + case CApi.SQLITE_DONE: return false; + default: + throw new IllegalStateException( + "This \"cannot happen\": all possible result codes were checked already." + ); + } + } + + /** + Works like sqlite3_step(), returning the same result codes as + that function unless throwOnError is true, in which case it + will throw an SqliteException for any result codes other than + Sqlite.ROW or Sqlite.DONE. + + The utility of this overload over the no-argument one is the + ability to handle BUSY and LOCKED errors more easily. + */ + public int step(boolean throwOnError){ + final int rc = (null==stmt) + ? Sqlite.MISUSE + : CApi.sqlite3_step(stmt); + return throwOnError ? checkRc(rc) : rc; + } + + /** + Returns the Sqlite which prepared this statement, or null if + this statement has been finalized. + */ + public Sqlite getDb(){ return this._db; } + + /** + Works like sqlite3_reset() but throws on error. + */ + public void reset(){ + checkRc(CApi.sqlite3_reset(thisStmt())); + } + + public boolean isBusy(){ + return CApi.sqlite3_stmt_busy(thisStmt()); + } + + public boolean isReadOnly(){ + return CApi.sqlite3_stmt_readonly(thisStmt()); + } + + public String sql(){ + return CApi.sqlite3_sql(thisStmt()); + } + + public String expandedSql(){ + return CApi.sqlite3_expanded_sql(thisStmt()); + } + + /** + Analog to sqlite3_stmt_explain() but throws if op is invalid. + */ + public void explain(int op){ + checkRc(CApi.sqlite3_stmt_explain(thisStmt(), op)); + } + + /** + Analog to sqlite3_stmt_isexplain(). + */ + public int isExplain(){ + return CApi.sqlite3_stmt_isexplain(thisStmt()); + } + + /** + Analog to sqlite3_normalized_sql(), but throws + UnsupportedOperationException if the library was built without + the SQLITE_ENABLE_NORMALIZE flag. + */ + public String normalizedSql(){ + Sqlite.checkSupported(hasNormalizeSql, "SQLITE_ENABLE_NORMALIZE"); + return CApi.sqlite3_normalized_sql(thisStmt()); + } + + public void clearBindings(){ + CApi.sqlite3_clear_bindings( thisStmt() ); + } + public void bindInt(int ndx, int val){ + checkRc(CApi.sqlite3_bind_int(thisStmt(), ndx, val)); + } + public void bindInt64(int ndx, long val){ + checkRc(CApi.sqlite3_bind_int64(thisStmt(), ndx, val)); + } + public void bindDouble(int ndx, double val){ + checkRc(CApi.sqlite3_bind_double(thisStmt(), ndx, val)); + } + public void bindObject(int ndx, Object o){ + checkRc(CApi.sqlite3_bind_java_object(thisStmt(), ndx, o)); + } + public void bindNull(int ndx){ + checkRc(CApi.sqlite3_bind_null(thisStmt(), ndx)); + } + public int bindParameterCount(){ + return CApi.sqlite3_bind_parameter_count(thisStmt()); + } + public int bindParameterIndex(String paramName){ + return CApi.sqlite3_bind_parameter_index(thisStmt(), paramName); + } + public String bindParameterName(int ndx){ + return CApi.sqlite3_bind_parameter_name(thisStmt(), ndx); + } + public void bindText(int ndx, byte[] utf8){ + checkRc(CApi.sqlite3_bind_text(thisStmt(), ndx, utf8)); + } + public void bindText(int ndx, String asUtf8){ + checkRc(CApi.sqlite3_bind_text(thisStmt(), ndx, asUtf8)); + } + public void bindText16(int ndx, byte[] utf16){ + checkRc(CApi.sqlite3_bind_text16(thisStmt(), ndx, utf16)); + } + public void bindText16(int ndx, String asUtf16){ + checkRc(CApi.sqlite3_bind_text16(thisStmt(), ndx, asUtf16)); + } + public void bindZeroBlob(int ndx, int n){ + checkRc(CApi.sqlite3_bind_zeroblob(thisStmt(), ndx, n)); + } + public void bindBlob(int ndx, byte[] bytes){ + checkRc(CApi.sqlite3_bind_blob(thisStmt(), ndx, bytes)); + } + + public byte[] columnBlob(int ndx){ + return CApi.sqlite3_column_blob( checkColIndex(ndx), ndx ); + } + public byte[] columnText(int ndx){ + return CApi.sqlite3_column_text( checkColIndex(ndx), ndx ); + } + public String columnText16(int ndx){ + return CApi.sqlite3_column_text16( checkColIndex(ndx), ndx ); + } + public int columnBytes(int ndx){ + return CApi.sqlite3_column_bytes( checkColIndex(ndx), ndx ); + } + public int columnBytes16(int ndx){ + return CApi.sqlite3_column_bytes16( checkColIndex(ndx), ndx ); + } + public int columnInt(int ndx){ + return CApi.sqlite3_column_int( checkColIndex(ndx), ndx ); + } + public long columnInt64(int ndx){ + return CApi.sqlite3_column_int64( checkColIndex(ndx), ndx ); + } + public double columnDouble(int ndx){ + return CApi.sqlite3_column_double( checkColIndex(ndx), ndx ); + } + public int columnType(int ndx){ + return CApi.sqlite3_column_type( checkColIndex(ndx), ndx ); + } + public String columnDeclType(int ndx){ + return CApi.sqlite3_column_decltype( checkColIndex(ndx), ndx ); + } + /** + Analog to sqlite3_column_count() but throws if this statement + has been finalized. + */ + public int columnCount(){ + /* We cannot reliably cache the column count in a class + member because an ALTER TABLE from a separate statement + can invalidate that count and we have no way, short of + installing a COMMIT handler or the like, of knowing when + to re-read it. We cannot install such a handler without + interfering with a client's ability to do so. */ + return CApi.sqlite3_column_count(thisStmt()); + } + public int columnDataCount(){ + return CApi.sqlite3_data_count( thisStmt() ); + } + public Object columnObject(int ndx){ + return CApi.sqlite3_column_java_object( checkColIndex(ndx), ndx ); + } + public T columnObject(int ndx, Class type){ + return CApi.sqlite3_column_java_object( checkColIndex(ndx), ndx, type ); + } + public String columnName(int ndx){ + return CApi.sqlite3_column_name( checkColIndex(ndx), ndx ); + } + public String columnDatabaseName(int ndx){ + return CApi.sqlite3_column_database_name( checkColIndex(ndx), ndx ); + } + public String columnOriginName(int ndx){ + return CApi.sqlite3_column_origin_name( checkColIndex(ndx), ndx ); + } + public String columnTableName(int ndx){ + return CApi.sqlite3_column_table_name( checkColIndex(ndx), ndx ); + } + } /* Stmt class */ + + /** + Interface for auto-extensions, as per the + sqlite3_auto_extension() API. + + Design note: the chicken/egg timing of auto-extension execution + requires that this feature be entirely re-implemented in Java + because the C-level API has no access to the Sqlite type so + cannot pass on an object of that type while the database is being + opened. One side effect of this reimplementation is that this + class's list of auto-extensions is 100% independent of the + C-level list so, e.g., clearAutoExtensions() will have no effect + on auto-extensions added via the C-level API and databases opened + from that level of API will not be passed to this level's + AutoExtension instances. + */ + public interface AutoExtension { + public void call(Sqlite db); + } + + private static final java.util.Set autoExtensions = + new java.util.LinkedHashSet<>(); + + /** + Passes db to all auto-extensions. If any one of them throws, + db.close() is called before the exception is propagated. + */ + private static void runAutoExtensions(Sqlite db){ + AutoExtension list[]; + synchronized(autoExtensions){ + /* Avoid that modifications to the AutoExtension list from within + auto-extensions affect this execution of this list. */ + list = autoExtensions.toArray(new AutoExtension[0]); + } + try { + for( AutoExtension ax : list ) ax.call(db); + }catch(Exception e){ + db.close(); + throw e; + } + } + + /** + Analog to sqlite3_auto_extension(), adds the given object to the + list of auto-extensions if it is not already in that list. The + given object will be run as part of Sqlite.open(), and passed the + being-opened database. If the extension throws then open() will + fail. + + This API does not guaranty whether or not manipulations made to + the auto-extension list from within auto-extension callbacks will + affect the current traversal of the auto-extension list. Whether + or not they do is unspecified and subject to change between + versions. e.g. if an AutoExtension calls addAutoExtension(), + whether or not the new extension will be run on the being-opened + database is undefined. + + Note that calling Sqlite.open() from an auto-extension will + necessarily result in recursion loop and (eventually) a stack + overflow. + */ + public static void addAutoExtension( AutoExtension e ){ + if( null==e ){ + throw new IllegalArgumentException("AutoExtension may not be null."); + } + synchronized(autoExtensions){ + autoExtensions.add(e); + } + } + + /** + Removes the given object from the auto-extension list if it is in + that list, otherwise this has no side-effects beyond briefly + locking that list. + */ + public static void removeAutoExtension( AutoExtension e ){ + synchronized(autoExtensions){ + autoExtensions.remove(e); + } + } + + /** + Removes all auto-extensions which were added via addAutoExtension(). + */ + public static void clearAutoExtensions(){ + synchronized(autoExtensions){ + autoExtensions.clear(); + } + } + + /** + Encapsulates state related to the sqlite3 backup API. Use + Sqlite.initBackup() to create new instances. + */ + public static final class Backup implements AutoCloseable { + private sqlite3_backup b; + private Sqlite dbTo; + private Sqlite dbFrom; + + Backup(Sqlite dbDest, String schemaDest,Sqlite dbSrc, String schemaSrc){ + this.dbTo = dbDest; + this.dbFrom = dbSrc; + b = CApi.sqlite3_backup_init(dbDest.nativeHandle(), schemaDest, + dbSrc.nativeHandle(), schemaSrc); + if(null==b) toss(); + } + + private void toss(){ + int rc = CApi.sqlite3_errcode(dbTo.nativeHandle()); + if(0!=rc) throw new SqliteException(dbTo); + rc = CApi.sqlite3_errcode(dbFrom.nativeHandle()); + if(0!=rc) throw new SqliteException(dbFrom); + throw new SqliteException(CApi.SQLITE_ERROR); + } + + private sqlite3_backup getNative(){ + if( null==b ) throw new IllegalStateException("This Backup is already closed."); + return b; + } + /** + If this backup is still active, this completes the backup and + frees its native resources, otherwise it this is a no-op. + */ + public void finish(){ + if( null!=b ){ + CApi.sqlite3_backup_finish(b); + b = null; + dbTo = null; + dbFrom = null; + } + } + + /** Equivalent to finish(). */ + @Override public void close(){ + this.finish(); + } + + /** + Analog to sqlite3_backup_step(). Returns 0 if stepping succeeds + or, Sqlite.DONE if the end is reached, Sqlite.BUSY if one of + the databases is busy, Sqlite.LOCKED if one of the databases is + locked, and throws for any other result code or if this object + has been closed. Note that BUSY and LOCKED are not necessarily + permanent errors, so do not trigger an exception. + */ + public int step(int pageCount){ + final int rc = CApi.sqlite3_backup_step(getNative(), pageCount); + switch(rc){ + case 0: + case Sqlite.DONE: + case Sqlite.BUSY: + case Sqlite.LOCKED: + return rc; + default: + toss(); + return CApi.SQLITE_ERROR/*not reached*/; + } + } + + /** + Analog to sqlite3_backup_pagecount(). + */ + public int pageCount(){ + return CApi.sqlite3_backup_pagecount(getNative()); + } + + /** + Analog to sqlite3_backup_remaining(). + */ + public int remaining(){ + return CApi.sqlite3_backup_remaining(getNative()); + } + } + + /** + Analog to sqlite3_backup_init(). If schemaSrc is null, "main" is + assumed. Throws if either this db or dbSrc (the source db) are + not opened, if either of schemaDest or schemaSrc are null, or if + the underlying call to sqlite3_backup_init() fails. + + The returned object must eventually be cleaned up by either + arranging for it to be auto-closed (e.g. using + try-with-resources) or by calling its finish() method. + */ + public Backup initBackup(String schemaDest, Sqlite dbSrc, String schemaSrc){ + thisDb(); + dbSrc.thisDb(); + if( null==schemaSrc || null==schemaDest ){ + throw new IllegalArgumentException( + "Neither the source nor destination schema name may be null." + ); + } + return new Backup(this, schemaDest, dbSrc, schemaSrc); + } + + + /** + Callback type for use with createCollation(). + */ + public interface Collation { + /** + Called by the SQLite core to compare inputs. Implementations + must compare its two arguments using memcmp(3) semantics. + + Warning: the SQLite core has no mechanism for reporting errors + from custom collations and its workflow does not accommodate + propagation of exceptions from callbacks. Any exceptions thrown + from collations will be silently suppressed and sorting results + will be unpredictable. + */ + int call(byte[] lhs, byte[] rhs); + } + + /** + Analog to sqlite3_create_collation(). + + Throws if name is null or empty, c is null, or the encoding flag + is invalid. The encoding must be one of the UTF8, UTF16, UTF16LE, + or UTF16BE constants. + */ + public void createCollation(String name, int encoding, Collation c){ + thisDb(); + if( null==name || name.isEmpty()){ + throw new IllegalArgumentException("Collation name may not be null or empty."); + } + if( null==c ){ + throw new IllegalArgumentException("Collation may not be null."); + } + switch(encoding){ + case UTF8: + case UTF16: + case UTF16LE: + case UTF16BE: + break; + default: + throw new IllegalArgumentException("Invalid Collation encoding."); + } + checkRc( + CApi.sqlite3_create_collation( + thisDb(), name, encoding, new org.sqlite.jni.capi.CollationCallback(){ + @Override public int call(byte[] lhs, byte[] rhs){ + try{return c.call(lhs, rhs);} + catch(Exception e){return 0;} + } + @Override public void xDestroy(){} + } + ) + ); + } + + /** + Callback for use with onCollationNeeded(). + */ + public interface CollationNeeded { + /** + Must behave as documented for the callback for + sqlite3_collation_needed(). + + Warning: the C API has no mechanism for reporting or + propagating errors from this callback, so any exceptions it + throws are suppressed. + */ + void call(Sqlite db, int encoding, String collationName); + } + + /** + Sets up the given object to be called by the SQLite core when it + encounters a collation name which it does not know. Pass a null + object to disconnect the object from the core. This replaces any + existing collation-needed loader, or is a no-op if the given + object is already registered. Throws if registering the loader + fails. + */ + public void onCollationNeeded( CollationNeeded cn ){ + org.sqlite.jni.capi.CollationNeededCallback cnc = null; + if( null!=cn ){ + cnc = new org.sqlite.jni.capi.CollationNeededCallback(){ + @Override public void call(sqlite3 db, int encoding, String collationName){ + final Sqlite xdb = Sqlite.fromNative(db); + if(null!=xdb) cn.call(xdb, encoding, collationName); + } + }; + } + checkRc( CApi.sqlite3_collation_needed(thisDb(), cnc) ); + } + + /** + Callback for use with busyHandler(). + */ + public interface BusyHandler { + /** + Must function as documented for the C-level + sqlite3_busy_handler() callback argument, minus the (void*) + argument the C-level function requires. + + If this function throws, it is translated to a database-level + error. + */ + int call(int n); + } + + /** + Analog to sqlite3_busy_timeout(). + */ + public void setBusyTimeout(int ms){ + checkRc(CApi.sqlite3_busy_timeout(thisDb(), ms)); + } + + /** + Analog to sqlite3_busy_handler(). If b is null then any + current handler is cleared. + */ + public void setBusyHandler( BusyHandler b ){ + org.sqlite.jni.capi.BusyHandlerCallback bhc = null; + if( null!=b ){ + /*bhc = new org.sqlite.jni.capi.BusyHandlerCallback(){ + @Override public int call(int n){ + return b.call(n); + } + };*/ + bhc = b::call; + } + checkRc( CApi.sqlite3_busy_handler(thisDb(), bhc) ); + } + + public interface CommitHook { + /** + Must behave as documented for the C-level sqlite3_commit_hook() + callback. If it throws, the exception is translated into + a db-level error. + */ + int call(); + } + + /** + A level of indirection to permit setCommitHook() to have similar + semantics as the C API, returning the previous hook. The caveat + is that if the low-level API is used to install a hook, it will + have a different hook type than Sqlite.CommitHook so + setCommitHook() will return null instead of that object. + */ + private static class CommitHookProxy + implements org.sqlite.jni.capi.CommitHookCallback { + final CommitHook commitHook; + CommitHookProxy(CommitHook ch){ + this.commitHook = ch; + } + @Override public int call(){ + return commitHook.call(); + } + } + + /** + Analog to sqlite3_commit_hook(). Returns the previous hook, if + any (else null). Throws if this db is closed. + + Minor caveat: if a commit hook is set on this object's underlying + db handle using the lower-level SQLite API, this function may + return null when replacing it, despite there being a hook, + because it will have a different callback type. So long as the + handle is only manipulated via the high-level API, this caveat + does not apply. + */ + public CommitHook setCommitHook( CommitHook c ){ + CommitHookProxy chp = null; + if( null!=c ){ + chp = new CommitHookProxy(c); + } + final org.sqlite.jni.capi.CommitHookCallback rv = + CApi.sqlite3_commit_hook(thisDb(), chp); + return (rv instanceof CommitHookProxy) + ? ((CommitHookProxy)rv).commitHook + : null; + } + + + public interface RollbackHook { + /** + Must behave as documented for the C-level sqlite3_rollback_hook() + callback. If it throws, the exception is translated into + a db-level error. + */ + void call(); + } + + /** + A level of indirection to permit setRollbackHook() to have similar + semantics as the C API, returning the previous hook. The caveat + is that if the low-level API is used to install a hook, it will + have a different hook type than Sqlite.RollbackHook so + setRollbackHook() will return null instead of that object. + */ + private static class RollbackHookProxy + implements org.sqlite.jni.capi.RollbackHookCallback { + final RollbackHook rollbackHook; + RollbackHookProxy(RollbackHook ch){ + this.rollbackHook = ch; + } + @Override public void call(){rollbackHook.call();} + } + + /** + Analog to sqlite3_rollback_hook(). Returns the previous hook, if + any (else null). Throws if this db is closed. + + Minor caveat: if a rollback hook is set on this object's underlying + db handle using the lower-level SQLite API, this function may + return null when replacing it, despite there being a hook, + because it will have a different callback type. So long as the + handle is only manipulated via the high-level API, this caveat + does not apply. + */ + public RollbackHook setRollbackHook( RollbackHook c ){ + RollbackHookProxy chp = null; + if( null!=c ){ + chp = new RollbackHookProxy(c); + } + final org.sqlite.jni.capi.RollbackHookCallback rv = + CApi.sqlite3_rollback_hook(thisDb(), chp); + return (rv instanceof RollbackHookProxy) + ? ((RollbackHookProxy)rv).rollbackHook + : null; + } + + public interface UpdateHook { + /** + Must function as described for the C-level sqlite3_update_hook() + callback. + */ + void call(int opId, String dbName, String tableName, long rowId); + } + + /** + A level of indirection to permit setUpdateHook() to have similar + semantics as the C API, returning the previous hook. The caveat + is that if the low-level API is used to install a hook, it will + have a different hook type than Sqlite.UpdateHook so + setUpdateHook() will return null instead of that object. + */ + private static class UpdateHookProxy + implements org.sqlite.jni.capi.UpdateHookCallback { + final UpdateHook updateHook; + UpdateHookProxy(UpdateHook ch){ + this.updateHook = ch; + } + @Override public void call(int opId, String dbName, String tableName, long rowId){ + updateHook.call(opId, dbName, tableName, rowId); + } + } + + /** + Analog to sqlite3_update_hook(). Returns the previous hook, if + any (else null). Throws if this db is closed. + + Minor caveat: if a update hook is set on this object's underlying + db handle using the lower-level SQLite API, this function may + return null when replacing it, despite there being a hook, + because it will have a different callback type. So long as the + handle is only manipulated via the high-level API, this caveat + does not apply. + */ + public UpdateHook setUpdateHook( UpdateHook c ){ + UpdateHookProxy chp = null; + if( null!=c ){ + chp = new UpdateHookProxy(c); + } + final org.sqlite.jni.capi.UpdateHookCallback rv = + CApi.sqlite3_update_hook(thisDb(), chp); + return (rv instanceof UpdateHookProxy) + ? ((UpdateHookProxy)rv).updateHook + : null; + } + + + /** + Callback interface for use with setProgressHandler(). + */ + public interface ProgressHandler { + /** + Must behave as documented for the C-level sqlite3_progress_handler() + callback. If it throws, the exception is translated into + a db-level error. + */ + int call(); + } + + /** + Analog to sqlite3_progress_handler(), sets the current progress + handler or clears it if p is null. + + Note that this API, in contrast to setUpdateHook(), + setRollbackHook(), and setCommitHook(), cannot return the + previous handler. That inconsistency is part of the lower-level C + API. + */ + public void setProgressHandler( int n, ProgressHandler p ){ + org.sqlite.jni.capi.ProgressHandlerCallback phc = null; + if( null!=p ){ + /*phc = new org.sqlite.jni.capi.ProgressHandlerCallback(){ + @Override public int call(){ return p.call(); } + };*/ + phc = p::call; + } + CApi.sqlite3_progress_handler( thisDb(), n, phc ); + } + + + /** + Callback for use with setAuthorizer(). + */ + public interface Authorizer { + /** + Must function as described for the C-level + sqlite3_set_authorizer() callback. If it throws, the error is + converted to a db-level error and the exception is suppressed. + */ + int call(int opId, String s1, String s2, String s3, String s4); + } + + /** + Analog to sqlite3_set_authorizer(), this sets the current + authorizer callback, or clears if it passed null. + */ + public void setAuthorizer( Authorizer a ) { + org.sqlite.jni.capi.AuthorizerCallback ac = null; + if( null!=a ){ + /*ac = new org.sqlite.jni.capi.AuthorizerCallback(){ + @Override public int call(int opId, String s1, String s2, String s3, String s4){ + return a.call(opId, s1, s2, s3, s4); + } + };*/ + ac = a::call; + } + checkRc( CApi.sqlite3_set_authorizer( thisDb(), ac ) ); + } + + /** + Object type for use with blobOpen() + */ + public final class Blob implements AutoCloseable { + private Sqlite db; + private sqlite3_blob b; + Blob(Sqlite db, sqlite3_blob b){ + this.db = db; + this.b = b; + } + + /** + If this blob is still opened, its low-level handle is + returned, else an IllegalArgumentException is thrown. + */ + private sqlite3_blob thisBlob(){ + if( null==b || 0==b.getNativePointer() ){ + throw new IllegalArgumentException("This Blob has been finalized."); + } + return b; + } + + /** + Analog to sqlite3_blob_close(). + */ + @Override public void close(){ + if( null!=b ){ + CApi.sqlite3_blob_close(b); + b = null; + db = null; + } + } + + /** + Throws if the JVM does not have JNI-level support for + ByteBuffer. + */ + private void checkNio(){ + if( !Sqlite.JNI_SUPPORTS_NIO ){ + throw new UnsupportedOperationException( + "This JVM does not support JNI access to ByteBuffer." + ); + } + } + /** + Analog to sqlite3_blob_reopen() but throws on error. + */ + public void reopen(long newRowId){ + db.checkRc( CApi.sqlite3_blob_reopen(thisBlob(), newRowId) ); + } + + /** + Analog to sqlite3_blob_write() but throws on error. + */ + public void write( byte[] bytes, int atOffset ){ + db.checkRc( CApi.sqlite3_blob_write(thisBlob(), bytes, atOffset) ); + } + + /** + Analog to sqlite3_blob_read() but throws on error. + */ + public void read( byte[] dest, int atOffset ){ + db.checkRc( CApi.sqlite3_blob_read(thisBlob(), dest, atOffset) ); + } + + /** + Analog to sqlite3_blob_bytes(). + */ + public int bytes(){ + return CApi.sqlite3_blob_bytes(thisBlob()); + } + } + + /** + Analog to sqlite3_blob_open(). Returns a Blob object for the + given database, table, column, and rowid. The blob is opened for + read-write mode if writeable is true, else it is read-only. + + The returned object must eventually be freed, before this + database is closed, by either arranging for it to be auto-closed + or calling its close() method. + + Throws on error. + */ + public Blob blobOpen(String dbName, String tableName, String columnName, + long iRow, boolean writeable){ + final OutputPointer.sqlite3_blob out = new OutputPointer.sqlite3_blob(); + checkRc( + CApi.sqlite3_blob_open(thisDb(), dbName, tableName, columnName, + iRow, writeable ? 1 : 0, out) + ); + return new Blob(this, out.take()); + } + + /** + Callback for use with libConfigLog(). + */ + public interface ConfigLog { + /** + Must function as described for a C-level callback for + sqlite3_config()'s SQLITE_CONFIG_LOG callback, with the slight + signature change. Any exceptions thrown from this callback are + necessarily suppressed. + */ + void call(int errCode, String msg); + } + + /** + Analog to sqlite3_config() with the SQLITE_CONFIG_LOG option, + this sets or (if log is null) clears the current logger. + */ + public static void libConfigLog(ConfigLog log){ + final org.sqlite.jni.capi.ConfigLogCallback l = + null==log + ? null + /*: new org.sqlite.jni.capi.ConfigLogCallback() { + @Override public void call(int errCode, String msg){ + log.call(errCode, msg); + } + };*/ + : log::call; + checkRcStatic(CApi.sqlite3_config(l)); + } + + /** + Callback for use with libConfigSqlLog(). + */ + public interface ConfigSqlLog { + /** + Must function as described for a C-level callback for + sqlite3_config()'s SQLITE_CONFIG_SQLLOG callback, with the + slight signature change. Any exceptions thrown from this + callback are necessarily suppressed. + */ + void call(Sqlite db, String msg, int msgType); + } + + /** + Analog to sqlite3_config() with the SQLITE_CONFIG_SQLLOG option, + this sets or (if log is null) clears the current logger. + + If SQLite is built without SQLITE_ENABLE_SQLLOG defined then this + will throw an UnsupportedOperationException. + */ + public static void libConfigSqlLog(ConfigSqlLog log){ + Sqlite.checkSupported(hasNormalizeSql, "SQLITE_ENABLE_SQLLOG"); + final org.sqlite.jni.capi.ConfigSqlLogCallback l = + null==log + ? null + : new org.sqlite.jni.capi.ConfigSqlLogCallback() { + @Override public void call(sqlite3 db, String msg, int msgType){ + try{ + log.call(fromNative(db), msg, msgType); + }catch(Exception e){ + /* Suppressed */ + } + } + }; + checkRcStatic(CApi.sqlite3_config(l)); + } + + /** + Analog to the C-level sqlite3_config() with one of the + SQLITE_CONFIG_... constants defined as CONFIG_... in this + class. Throws on error, including passing of an unknown option or + if a specified option is not supported by the underlying build of + the SQLite library. + */ + public static void libConfigOp( int op ){ + checkRcStatic(CApi.sqlite3_config(op)); + } + +} diff --git a/ext/jni/src/org/sqlite/jni/wrapper1/SqliteException.java b/ext/jni/src/org/sqlite/jni/wrapper1/SqliteException.java new file mode 100644 index 0000000000..9b4440f190 --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/wrapper1/SqliteException.java @@ -0,0 +1,85 @@ +/* +** 2023-10-09 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file is part of the wrapper1 interface for sqlite3. +*/ +package org.sqlite.jni.wrapper1; +import org.sqlite.jni.capi.CApi; +import org.sqlite.jni.capi.sqlite3; + +/** + A wrapper for communicating C-level (sqlite3*) instances with + Java. These wrappers do not own their associated pointer, they + simply provide a type-safe way to communicate it between Java + and C via JNI. +*/ +public final class SqliteException extends java.lang.RuntimeException { + private int errCode = CApi.SQLITE_ERROR; + private int xerrCode = CApi.SQLITE_ERROR; + private int errOffset = -1; + private int sysErrno = 0; + + /** + Records the given error string and uses SQLITE_ERROR for both the + error code and extended error code. + */ + public SqliteException(String msg){ + super(msg); + } + + /** + Uses sqlite3_errstr(sqlite3ResultCode) for the error string and + sets both the error code and extended error code to the given + value. This approach includes no database-level information and + systemErrno() will be 0, so is intended only for use with sqlite3 + APIs for which a result code is not an error but which the + higher-level wrapper should treat as one. + */ + public SqliteException(int sqlite3ResultCode){ + super(CApi.sqlite3_errstr(sqlite3ResultCode)); + errCode = xerrCode = sqlite3ResultCode; + } + + /** + Records the current error state of db (which must not be null and + must refer to an opened db object). Note that this does not close + the db. + + Design note: closing the db on error is really only useful during + a failed db-open operation, and the place(s) where that can + happen are inside this library, not client-level code. + */ + SqliteException(sqlite3 db){ + super(CApi.sqlite3_errmsg(db)); + errCode = CApi.sqlite3_errcode(db); + xerrCode = CApi.sqlite3_extended_errcode(db); + errOffset = CApi.sqlite3_error_offset(db); + sysErrno = CApi.sqlite3_system_errno(db); + } + + /** + Records the current error state of db (which must not be null and + must refer to an open database). + */ + public SqliteException(Sqlite db){ + this(db.nativeHandle()); + } + + public SqliteException(Sqlite.Stmt stmt){ + this(stmt.getDb()); + } + + public int errcode(){ return errCode; } + public int extendedErrcode(){ return xerrCode; } + public int errorOffset(){ return errOffset; } + public int systemErrno(){ return sysErrno; } + +} diff --git a/ext/jni/src/org/sqlite/jni/wrapper1/Tester2.java b/ext/jni/src/org/sqlite/jni/wrapper1/Tester2.java new file mode 100644 index 0000000000..528e1f61c6 --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/wrapper1/Tester2.java @@ -0,0 +1,1212 @@ +/* +** 2023-10-09 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains a set of tests for the sqlite3 JNI bindings. +*/ +package org.sqlite.jni.wrapper1; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import org.sqlite.jni.capi.CApi; + +/** + An annotation for Tester2 tests which we do not want to run in + reflection-driven test mode because either they are not suitable + for multi-threaded threaded mode or we have to control their execution + order. +*/ +@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME) +@java.lang.annotation.Target({java.lang.annotation.ElementType.METHOD}) +@interface ManualTest{} +/** + Annotation for Tester2 tests which mark those which must be skipped + in multi-threaded mode. +*/ +@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME) +@java.lang.annotation.Target({java.lang.annotation.ElementType.METHOD}) +@interface SingleThreadOnly{} + +public class Tester2 implements Runnable { + //! True when running in multi-threaded mode. + private static boolean mtMode = false; + //! True to sleep briefly between tests. + private static boolean takeNaps = false; + //! True to shuffle the order of the tests. + private static boolean shuffle = false; + //! True to dump the list of to-run tests to stdout. + private static int listRunTests = 0; + //! True to squelch all out() and outln() output. + private static boolean quietMode = false; + //! Total number of runTests() calls. + private static int nTestRuns = 0; + //! List of test*() methods to run. + private static List testMethods = null; + //! List of exceptions collected by run() + private static final List listErrors = new ArrayList<>(); + private static final class Metrics { + //! Number of times createNewDb() (or equivalent) is invoked. + volatile int dbOpen = 0; + } + + //! Instance ID. + private final Integer tId; + + Tester2(Integer id){ + tId = id; + } + + static final Metrics metrics = new Metrics(); + + public static synchronized void outln(){ + if( !quietMode ){ + System.out.println(); + } + } + + public static synchronized void outPrefix(){ + if( !quietMode ){ + System.out.print(Thread.currentThread().getName()+": "); + } + } + + public static synchronized void outln(Object val){ + if( !quietMode ){ + outPrefix(); + System.out.println(val); + } + } + + public static synchronized void out(Object val){ + if( !quietMode ){ + System.out.print(val); + } + } + + @SuppressWarnings("unchecked") + public static synchronized void out(Object... vals){ + if( !quietMode ){ + outPrefix(); + for(Object v : vals) out(v); + } + } + + @SuppressWarnings("unchecked") + public static synchronized void outln(Object... vals){ + if( !quietMode ){ + out(vals); out("\n"); + } + } + + static volatile int affirmCount = 0; + public static synchronized int affirm(Boolean v, String comment){ + ++affirmCount; + if( false ) assert( v /* prefer assert over exception if it's enabled because + the JNI layer sometimes has to suppress exceptions, + so they might be squelched on their way back to the + top. */); + if( !v ) throw new RuntimeException(comment); + return affirmCount; + } + + public static void affirm(Boolean v){ + affirm(v, "Affirmation failed."); + } + + + public static void execSql(Sqlite db, String sql[]){ + execSql(db, String.join("", sql)); + } + + /** + Executes all SQL statements in the given string. If throwOnError + is true then it will throw for any prepare/step errors, else it + will return the corresponding non-0 result code. + */ + public static int execSql(Sqlite dbw, boolean throwOnError, String sql){ + final ValueHolder rv = new ValueHolder<>(0); + final Sqlite.PrepareMulti pm = new Sqlite.PrepareMulti(){ + @Override public void call(Sqlite.Stmt stmt){ + try{ + while( Sqlite.ROW == (rv.value = stmt.step(throwOnError)) ){} + } + finally{ stmt.finalizeStmt(); } + } + }; + try { + dbw.prepareMulti(sql, pm); + }catch(SqliteException se){ + if( throwOnError ){ + throw se; + }else{ + /* This error (likely) happened in the prepare() phase and we + need to preempt it. */ + rv.value = se.errcode(); + } + } + return (rv.value==Sqlite.DONE) ? 0 : rv.value; + } + + static void execSql(Sqlite db, String sql){ + execSql(db, true, sql); + } + + @SingleThreadOnly /* because it's thread-agnostic */ + private void test1(){ + affirm(Sqlite.libVersionNumber() == CApi.SQLITE_VERSION_NUMBER); + } + + private void nap() throws InterruptedException { + if( takeNaps ){ + Thread.sleep(java.util.concurrent.ThreadLocalRandom.current().nextInt(3, 17), 0); + } + } + + Sqlite openDb(String name){ + final Sqlite db = Sqlite.open(name, Sqlite.OPEN_READWRITE| + Sqlite.OPEN_CREATE| + Sqlite.OPEN_EXRESCODE); + ++metrics.dbOpen; + return db; + } + + Sqlite openDb(){ return openDb(":memory:"); } + + void testOpenDb1(){ + Sqlite db = openDb(); + affirm( 0!=db.nativeHandle().getNativePointer() ); + affirm( "main".equals( db.dbName(0) ) ); + db.setMainDbName("foo"); + affirm( "foo".equals( db.dbName(0) ) ); + affirm( db.dbConfig(Sqlite.DBCONFIG_DEFENSIVE, true) + /* The underlying function has different mangled names in jdk8 + vs jdk19, and this call is here to ensure that the build + fails if it cannot find both names. */ ); + affirm( !db.dbConfig(Sqlite.DBCONFIG_DEFENSIVE, false) ); + SqliteException ex = null; + try{ db.dbConfig(0, false); } + catch(SqliteException e){ ex = e; } + affirm( null!=ex ); + ex = null; + db.close(); + affirm( null==db.nativeHandle() ); + + try{ db = openDb("/no/such/dir/.../probably"); } + catch(SqliteException e){ ex = e; } + affirm( ex!=null ); + affirm( ex.errcode() != 0 ); + affirm( ex.extendedErrcode() != 0 ); + affirm( ex.errorOffset() < 0 ); + // there's no reliable way to predict what ex.systemErrno() might be + } + + void testPrepare1(){ + try (Sqlite db = openDb()) { + Sqlite.Stmt stmt = db.prepare("SELECT ?1"); + Exception e = null; + affirm( null!=stmt.nativeHandle() ); + affirm( db == stmt.getDb() ); + affirm( 1==stmt.bindParameterCount() ); + affirm( "?1".equals(stmt.bindParameterName(1)) ); + affirm( null==stmt.bindParameterName(2) ); + stmt.bindInt64(1, 1); + stmt.bindDouble(1, 1.1); + stmt.bindObject(1, db); + stmt.bindNull(1); + stmt.bindText(1, new byte[] {32,32,32}); + stmt.bindText(1, "123"); + stmt.bindText16(1, "123".getBytes(StandardCharsets.UTF_16)); + stmt.bindText16(1, "123"); + stmt.bindZeroBlob(1, 8); + stmt.bindBlob(1, new byte[] {1,2,3,4}); + stmt.bindInt(1, 17); + try{ stmt.bindInt(2,1); } + catch(Exception ex){ e = ex; } + affirm( null!=e ); + e = null; + affirm( stmt.step() ); + try{ stmt.columnInt(1); } + catch(Exception ex){ e = ex; } + affirm( null!=e ); + e = null; + affirm( 17 == stmt.columnInt(0) ); + affirm( 17L == stmt.columnInt64(0) ); + affirm( 17.0 == stmt.columnDouble(0) ); + affirm( "17".equals(stmt.columnText16(0)) ); + affirm( !stmt.step() ); + stmt.reset(); + affirm( Sqlite.ROW==stmt.step(false) ); + affirm( !stmt.step() ); + affirm( 0 == stmt.finalizeStmt() ); + affirm( null==stmt.nativeHandle() ); + + stmt = db.prepare("SELECT ?"); + stmt.bindObject(1, db); + affirm( Sqlite.ROW == stmt.step(false) ); + affirm( db==stmt.columnObject(0) ); + affirm( db==stmt.columnObject(0, Sqlite.class ) ); + affirm( null==stmt.columnObject(0, Sqlite.Stmt.class ) ); + affirm( 0==stmt.finalizeStmt() ) + /* getting a non-0 out of sqlite3_finalize() is tricky */; + affirm( null==stmt.nativeHandle() ); + } + } + + void testUdfScalar(){ + final ValueHolder xDestroyCalled = new ValueHolder<>(0); + try (Sqlite db = openDb()) { + execSql(db, "create table t(a); insert into t(a) values(1),(2),(3)"); + final ValueHolder vh = new ValueHolder<>(0); + final ScalarFunction f = new ScalarFunction(){ + public void xFunc(SqlFunction.Arguments args){ + affirm( db == args.getDb() ); + for( SqlFunction.Arguments.Arg arg : args ){ + vh.value += arg.getInt(); + } + args.resultInt(vh.value); + } + public void xDestroy(){ + ++xDestroyCalled.value; + } + }; + db.createFunction("myfunc", -1, f); + Sqlite.Stmt q = db.prepare("select myfunc(1,2,3)"); + affirm( q.step() ); + affirm( 6 == vh.value ); + affirm( 6 == q.columnInt(0) ); + q.finalizeStmt(); + affirm( 0 == xDestroyCalled.value ); + vh.value = 0; + q = db.prepare("select myfunc(-1,-2,-3)"); + affirm( q.step() ); + affirm( -6 == vh.value ); + affirm( -6 == q.columnInt(0) ); + affirm( 0 == xDestroyCalled.value ); + q.finalizeStmt(); + } + affirm( 1 == xDestroyCalled.value ); + } + + void testUdfAggregate(){ + final ValueHolder xDestroyCalled = new ValueHolder<>(0); + Sqlite.Stmt q = null; + try (Sqlite db = openDb()) { + execSql(db, "create table t(a); insert into t(a) values(1),(2),(3)"); + final AggregateFunction f = new AggregateFunction(){ + public void xStep(SqlFunction.Arguments args){ + final ValueHolder agg = this.getAggregateState(args, 0); + for( SqlFunction.Arguments.Arg arg : args ){ + agg.value += arg.getInt(); + } + } + public void xFinal(SqlFunction.Arguments args){ + final Integer v = this.takeAggregateState(args); + if( null==v ) args.resultNull(); + else args.resultInt(v); + } + public void xDestroy(){ + ++xDestroyCalled.value; + } + }; + db.createFunction("summer", 1, f); + q = db.prepare( + "with cte(v) as ("+ + "select 3 union all select 5 union all select 7"+ + ") select summer(v), summer(v+1) from cte" + /* ------------------^^^^^^^^^^^ ensures that we're handling + sqlite3_aggregate_context() properly. */ + ); + affirm( q.step() ); + affirm( 15==q.columnInt(0) ); + q.finalizeStmt(); + q = null; + affirm( 0 == xDestroyCalled.value ); + db.createFunction("summerN", -1, f); + + q = db.prepare("select summerN(1,8,9), summerN(2,3,4)"); + affirm( q.step() ); + affirm( 18==q.columnInt(0) ); + affirm( 9==q.columnInt(1) ); + q.finalizeStmt(); + q = null; + + }/*db*/ + finally{ + if( null!=q ) q.finalizeStmt(); + } + affirm( 2 == xDestroyCalled.value + /* because we've bound the same instance twice */ ); + } + + private void testUdfWindow(){ + final Sqlite db = openDb(); + /* Example window function, table, and results taken from: + https://sqlite.org/windowfunctions.html#udfwinfunc */ + final WindowFunction func = new WindowFunction(){ + //! Impl of xStep() and xInverse() + private void xStepInverse(SqlFunction.Arguments args, int v){ + this.getAggregateState(args,0).value += v; + } + @Override public void xStep(SqlFunction.Arguments args){ + this.xStepInverse(args, args.getInt(0)); + } + @Override public void xInverse(SqlFunction.Arguments args){ + this.xStepInverse(args, -args.getInt(0)); + } + //! Impl of xFinal() and xValue() + private void xFinalValue(SqlFunction.Arguments args, Integer v){ + if(null == v) args.resultNull(); + else args.resultInt(v); + } + @Override public void xFinal(SqlFunction.Arguments args){ + xFinalValue(args, this.takeAggregateState(args)); + affirm( null == this.getAggregateState(args,null).value ); + } + @Override public void xValue(SqlFunction.Arguments args){ + xFinalValue(args, this.getAggregateState(args,null).value); + } + }; + db.createFunction("winsumint", 1, func); + execSql(db, new String[] { + "CREATE TEMP TABLE twin(x, y); INSERT INTO twin VALUES", + "('a', 4),('b', 5),('c', 3),('d', 8),('e', 1)" + }); + final Sqlite.Stmt stmt = db.prepare( + "SELECT x, winsumint(y) OVER ("+ + "ORDER BY x ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING"+ + ") AS sum_y "+ + "FROM twin ORDER BY x;" + ); + int n = 0; + while( stmt.step() ){ + final String s = stmt.columnText16(0); + final int i = stmt.columnInt(1); + switch(++n){ + case 1: affirm( "a".equals(s) && 9==i ); break; + case 2: affirm( "b".equals(s) && 12==i ); break; + case 3: affirm( "c".equals(s) && 16==i ); break; + case 4: affirm( "d".equals(s) && 12==i ); break; + case 5: affirm( "e".equals(s) && 9==i ); break; + default: affirm( false /* cannot happen */ ); + } + } + stmt.close(); + affirm( 5 == n ); + db.close(); + } + + private void testKeyword(){ + final int n = Sqlite.keywordCount(); + affirm( n>0 ); + affirm( !Sqlite.keywordCheck("_nope_") ); + affirm( Sqlite.keywordCheck("seLect") ); + affirm( null!=Sqlite.keywordName(0) ); + affirm( null!=Sqlite.keywordName(n-1) ); + affirm( null==Sqlite.keywordName(n) ); + } + + + private void testExplain(){ + final Sqlite db = openDb(); + Sqlite.Stmt q = db.prepare("SELECT 1"); + affirm( 0 == q.isExplain() ); + q.explain(0); + affirm( 0 == q.isExplain() ); + q.explain(1); + affirm( 1 == q.isExplain() ); + q.explain(2); + affirm( 2 == q.isExplain() ); + Exception ex = null; + try{ + q.explain(-1); + }catch(Exception e){ + ex = e; + } + affirm( ex instanceof SqliteException ); + q.finalizeStmt(); + db.close(); + } + + + private void testTrace(){ + final Sqlite db = openDb(); + final ValueHolder counter = new ValueHolder<>(0); + /* Ensure that characters outside of the UTF BMP survive the trip + from Java to sqlite3 and back to Java. (At no small efficiency + penalty.) */ + final String nonBmpChar = "😃"; + db.trace( + Sqlite.TRACE_ALL, + new Sqlite.TraceCallback(){ + @Override public void call(int traceFlag, Object pNative, Object x){ + ++counter.value; + //outln("TRACE "+traceFlag+" pNative = "+pNative.getClass().getName()); + switch(traceFlag){ + case Sqlite.TRACE_STMT: + affirm(pNative instanceof Sqlite.Stmt); + //outln("TRACE_STMT sql = "+x); + affirm(x instanceof String); + affirm( ((String)x).indexOf(nonBmpChar) > 0 ); + break; + case Sqlite.TRACE_PROFILE: + affirm(pNative instanceof Sqlite.Stmt); + affirm(x instanceof Long); + //outln("TRACE_PROFILE time = "+x); + break; + case Sqlite.TRACE_ROW: + affirm(pNative instanceof Sqlite.Stmt); + affirm(null == x); + //outln("TRACE_ROW = "+sqlite3_column_text16((sqlite3_stmt)pNative, 0)); + break; + case Sqlite.TRACE_CLOSE: + affirm(pNative instanceof Sqlite); + affirm(null == x); + break; + default: + affirm(false /*cannot happen*/); + break; + } + } + }); + execSql(db, "SELECT coalesce(null,null,'"+nonBmpChar+"'); "+ + "SELECT 'w"+nonBmpChar+"orld'"); + affirm( 6 == counter.value ); + db.close(); + affirm( 7 == counter.value ); + } + + private void testStatus(){ + final Sqlite db = openDb(); + execSql(db, "create table t(a); insert into t values(1),(2),(3)"); + + Sqlite.Status s = Sqlite.libStatus(Sqlite.STATUS_MEMORY_USED, false); + affirm( s.current > 0 ); + affirm( s.peak >= s.current ); + + s = db.status(Sqlite.DBSTATUS_SCHEMA_USED, false); + affirm( s.current > 0 ); + affirm( s.peak == 0 /* always 0 for SCHEMA_USED */ ); + + db.close(); + } + + @SingleThreadOnly /* because multiple threads legitimately make these + results unpredictable */ + private synchronized void testAutoExtension(){ + final ValueHolder val = new ValueHolder<>(0); + final ValueHolder toss = new ValueHolder<>(null); + final Sqlite.AutoExtension ax = new Sqlite.AutoExtension(){ + @Override public void call(Sqlite db){ + ++val.value; + if( null!=toss.value ){ + throw new RuntimeException(toss.value); + } + } + }; + Sqlite.addAutoExtension(ax); + openDb().close(); + affirm( 1==val.value ); + openDb().close(); + affirm( 2==val.value ); + Sqlite.clearAutoExtensions(); + openDb().close(); + affirm( 2==val.value ); + + Sqlite.addAutoExtension( ax ); + Sqlite.addAutoExtension( ax ); // Must not add a second entry + Sqlite.addAutoExtension( ax ); // or a third one + openDb().close(); + affirm( 3==val.value ); + + Sqlite db = openDb(); + affirm( 4==val.value ); + execSql(db, "ATTACH ':memory:' as foo"); + affirm( 4==val.value, "ATTACH uses the same connection, not sub-connections." ); + db.close(); + db = null; + + Sqlite.removeAutoExtension(ax); + openDb().close(); + affirm( 4==val.value ); + Sqlite.addAutoExtension(ax); + Exception err = null; + toss.value = "Throwing from auto_extension."; + try{ + openDb(); + }catch(Exception e){ + err = e; + } + affirm( err!=null ); + affirm( err.getMessage().contains(toss.value) ); + toss.value = null; + + val.value = 0; + final Sqlite.AutoExtension ax2 = new Sqlite.AutoExtension(){ + @Override public void call(Sqlite db){ + ++val.value; + } + }; + Sqlite.addAutoExtension(ax2); + openDb().close(); + affirm( 2 == val.value ); + Sqlite.removeAutoExtension(ax); + openDb().close(); + affirm( 3 == val.value ); + Sqlite.addAutoExtension(ax); + openDb().close(); + affirm( 5 == val.value ); + Sqlite.removeAutoExtension(ax2); + openDb().close(); + affirm( 6 == val.value ); + Sqlite.addAutoExtension(ax2); + openDb().close(); + affirm( 8 == val.value ); + + Sqlite.clearAutoExtensions(); + openDb().close(); + affirm( 8 == val.value ); + } + + private void testBackup(){ + final Sqlite dbDest = openDb(); + + try (Sqlite dbSrc = openDb()) { + execSql(dbSrc, new String[]{ + "pragma page_size=512; VACUUM;", + "create table t(a);", + "insert into t(a) values(1),(2),(3);" + }); + Exception e = null; + try { + dbSrc.initBackup("main",dbSrc,"main"); + }catch(Exception x){ + e = x; + } + affirm( e instanceof SqliteException ); + e = null; + try (Sqlite.Backup b = dbDest.initBackup("main",dbSrc,"main")) { + affirm( null!=b ); + int rc; + while( Sqlite.DONE!=(rc = b.step(1)) ){ + affirm( 0==rc ); + } + affirm( b.pageCount() > 0 ); + b.finish(); + } + } + + try (Sqlite.Stmt q = dbDest.prepare("SELECT sum(a) from t")) { + q.step(); + affirm( q.columnInt(0) == 6 ); + } + dbDest.close(); + } + + private void testCollation(){ + final Sqlite db = openDb(); + execSql(db, "CREATE TABLE t(a); INSERT INTO t(a) VALUES('a'),('b'),('c')"); + final Sqlite.Collation myCollation = new Sqlite.Collation() { + private final String myState = + "this is local state. There is much like it, but this is mine."; + @Override + // Reverse-sorts its inputs... + public int call(byte[] lhs, byte[] rhs){ + int len = lhs.length > rhs.length ? rhs.length : lhs.length; + int c = 0, i = 0; + for(i = 0; i < len; ++i){ + c = lhs[i] - rhs[i]; + if(0 != c) break; + } + if(0==c){ + if(i < lhs.length) c = 1; + else if(i < rhs.length) c = -1; + } + return -c; + } + }; + final Sqlite.CollationNeeded collLoader = new Sqlite.CollationNeeded(){ + @Override + public void call(Sqlite dbArg, int eTextRep, String collationName){ + affirm(dbArg == db); + db.createCollation("reversi", eTextRep, myCollation); + } + }; + db.onCollationNeeded(collLoader); + Sqlite.Stmt stmt = db.prepare("SELECT a FROM t ORDER BY a COLLATE reversi"); + int counter = 0; + while( stmt.step() ){ + final String val = stmt.columnText16(0); + ++counter; + switch(counter){ + case 1: affirm("c".equals(val)); break; + case 2: affirm("b".equals(val)); break; + case 3: affirm("a".equals(val)); break; + } + } + affirm(3 == counter); + stmt.finalizeStmt(); + stmt = db.prepare("SELECT a FROM t ORDER BY a"); + counter = 0; + while( stmt.step() ){ + final String val = stmt.columnText16(0); + ++counter; + //outln("Non-REVERSI'd row#"+counter+": "+val); + switch(counter){ + case 3: affirm("c".equals(val)); break; + case 2: affirm("b".equals(val)); break; + case 1: affirm("a".equals(val)); break; + } + } + affirm(3 == counter); + stmt.finalizeStmt(); + db.onCollationNeeded(null); + db.close(); + } + + @SingleThreadOnly /* because threads inherently break this test */ + private void testBusy(){ + final String dbName = "_busy-handler.db"; + try{ + Sqlite db1 = openDb(dbName); + ++metrics.dbOpen; + execSql(db1, "CREATE TABLE IF NOT EXISTS t(a)"); + Sqlite db2 = openDb(dbName); + ++metrics.dbOpen; + + final ValueHolder xBusyCalled = new ValueHolder<>(0); + Sqlite.BusyHandler handler = new Sqlite.BusyHandler(){ + @Override public int call(int n){ + return n > 2 ? 0 : ++xBusyCalled.value; + } + }; + db2.setBusyHandler(handler); + + // Force a locked condition... + execSql(db1, "BEGIN EXCLUSIVE"); + int rc = 0; + SqliteException ex = null; + try{ + db2.prepare("SELECT * from t"); + }catch(SqliteException x){ + ex = x; + } + affirm( null!=ex ); + affirm( Sqlite.BUSY == ex.errcode() ); + affirm( 3 == xBusyCalled.value ); + db1.close(); + db2.close(); + }finally{ + try{(new java.io.File(dbName)).delete();} + catch(Exception e){/* ignore */} + } + } + + private void testCommitHook(){ + final Sqlite db = openDb(); + final ValueHolder counter = new ValueHolder<>(0); + final ValueHolder hookResult = new ValueHolder<>(0); + final Sqlite.CommitHook theHook = new Sqlite.CommitHook(){ + @Override public int call(){ + ++counter.value; + return hookResult.value; + } + }; + Sqlite.CommitHook oldHook = db.setCommitHook(theHook); + affirm( null == oldHook ); + execSql(db, "CREATE TABLE t(a); INSERT INTO t(a) VALUES('a'),('b'),('c')"); + affirm( 2 == counter.value ); + execSql(db, "BEGIN; SELECT 1; SELECT 2; COMMIT;"); + affirm( 2 == counter.value /* NOT invoked if no changes are made */ ); + execSql(db, "BEGIN; update t set a='d' where a='c'; COMMIT;"); + affirm( 3 == counter.value ); + oldHook = db.setCommitHook(theHook); + affirm( theHook == oldHook ); + execSql(db, "BEGIN; update t set a='e' where a='d'; COMMIT;"); + affirm( 4 == counter.value ); + oldHook = db.setCommitHook(null); + affirm( theHook == oldHook ); + execSql(db, "BEGIN; update t set a='f' where a='e'; COMMIT;"); + affirm( 4 == counter.value ); + oldHook = db.setCommitHook(null); + affirm( null == oldHook ); + execSql(db, "BEGIN; update t set a='g' where a='f'; COMMIT;"); + affirm( 4 == counter.value ); + + final Sqlite.CommitHook newHook = new Sqlite.CommitHook(){ + @Override public int call(){return 0;} + }; + oldHook = db.setCommitHook(newHook); + affirm( null == oldHook ); + execSql(db, "BEGIN; update t set a='h' where a='g'; COMMIT;"); + affirm( 4 == counter.value ); + oldHook = db.setCommitHook(theHook); + affirm( newHook == oldHook ); + execSql(db, "BEGIN; update t set a='i' where a='h'; COMMIT;"); + affirm( 5 == counter.value ); + hookResult.value = Sqlite.ERROR; + int rc = execSql(db, false, "BEGIN; update t set a='j' where a='i'; COMMIT;"); + affirm( Sqlite.CONSTRAINT_COMMITHOOK == rc ); + affirm( 6 == counter.value ); + db.close(); + } + + private void testRollbackHook(){ + final Sqlite db = openDb(); + final ValueHolder counter = new ValueHolder<>(0); + final Sqlite.RollbackHook theHook = new Sqlite.RollbackHook(){ + @Override public void call(){ + ++counter.value; + } + }; + Sqlite.RollbackHook oldHook = db.setRollbackHook(theHook); + affirm( null == oldHook ); + execSql(db, "CREATE TABLE t(a); INSERT INTO t(a) VALUES('a'),('b'),('c')"); + affirm( 0 == counter.value ); + execSql(db, false, "BEGIN; SELECT 1; SELECT 2; ROLLBACK;"); + affirm( 1 == counter.value /* contra to commit hook, is invoked if no changes are made */ ); + + final Sqlite.RollbackHook newHook = new Sqlite.RollbackHook(){ + @Override public void call(){} + }; + oldHook = db.setRollbackHook(newHook); + affirm( theHook == oldHook ); + execSql(db, false, "BEGIN; SELECT 1; ROLLBACK;"); + affirm( 1 == counter.value ); + oldHook = db.setRollbackHook(theHook); + affirm( newHook == oldHook ); + execSql(db, false, "BEGIN; SELECT 1; ROLLBACK;"); + affirm( 2 == counter.value ); + int rc = execSql(db, false, "BEGIN; SELECT 1; ROLLBACK;"); + affirm( 0 == rc ); + affirm( 3 == counter.value ); + db.close(); + } + + private void testUpdateHook(){ + final Sqlite db = openDb(); + final ValueHolder counter = new ValueHolder<>(0); + final ValueHolder expectedOp = new ValueHolder<>(0); + final Sqlite.UpdateHook theHook = new Sqlite.UpdateHook(){ + @Override + public void call(int opId, String dbName, String tableName, long rowId){ + ++counter.value; + if( 0!=expectedOp.value ){ + affirm( expectedOp.value == opId ); + } + } + }; + Sqlite.UpdateHook oldHook = db.setUpdateHook(theHook); + affirm( null == oldHook ); + expectedOp.value = Sqlite.INSERT; + execSql(db, "CREATE TABLE t(a); INSERT INTO t(a) VALUES('a'),('b'),('c')"); + affirm( 3 == counter.value ); + expectedOp.value = Sqlite.UPDATE; + execSql(db, "update t set a='d' where a='c';"); + affirm( 4 == counter.value ); + oldHook = db.setUpdateHook(theHook); + affirm( theHook == oldHook ); + expectedOp.value = Sqlite.DELETE; + execSql(db, "DELETE FROM t where a='d'"); + affirm( 5 == counter.value ); + oldHook = db.setUpdateHook(null); + affirm( theHook == oldHook ); + execSql(db, "update t set a='e' where a='b';"); + affirm( 5 == counter.value ); + oldHook = db.setUpdateHook(null); + affirm( null == oldHook ); + + final Sqlite.UpdateHook newHook = new Sqlite.UpdateHook(){ + @Override public void call(int opId, String dbName, String tableName, long rowId){ + } + }; + oldHook = db.setUpdateHook(newHook); + affirm( null == oldHook ); + execSql(db, "update t set a='h' where a='a'"); + affirm( 5 == counter.value ); + oldHook = db.setUpdateHook(theHook); + affirm( newHook == oldHook ); + expectedOp.value = Sqlite.UPDATE; + execSql(db, "update t set a='i' where a='h'"); + affirm( 6 == counter.value ); + db.close(); + } + + private void testProgress(){ + final Sqlite db = openDb(); + final ValueHolder counter = new ValueHolder<>(0); + db.setProgressHandler(1, new Sqlite.ProgressHandler(){ + @Override public int call(){ + ++counter.value; + return 0; + } + }); + execSql(db, "SELECT 1; SELECT 2;"); + affirm( counter.value > 0 ); + int nOld = counter.value; + db.setProgressHandler(0, null); + execSql(db, "SELECT 1; SELECT 2;"); + affirm( nOld == counter.value ); + db.close(); + } + + private void testAuthorizer(){ + final Sqlite db = openDb(); + final ValueHolder counter = new ValueHolder<>(0); + final ValueHolder authRc = new ValueHolder<>(0); + final Sqlite.Authorizer auth = new Sqlite.Authorizer(){ + public int call(int op, String s0, String s1, String s2, String s3){ + ++counter.value; + //outln("xAuth(): "+s0+" "+s1+" "+s2+" "+s3); + return authRc.value; + } + }; + execSql(db, "CREATE TABLE t(a); INSERT INTO t(a) VALUES('a'),('b'),('c')"); + db.setAuthorizer(auth); + execSql(db, "UPDATE t SET a=1"); + affirm( 1 == counter.value ); + authRc.value = Sqlite.DENY; + int rc = execSql(db, false, "UPDATE t SET a=2"); + affirm( Sqlite.AUTH==rc ); + db.setAuthorizer(null); + rc = execSql(db, false, "UPDATE t SET a=2"); + affirm( 0==rc ); + db.close(); + } + + private void testBlobOpen(){ + final Sqlite db = openDb(); + + execSql(db, "CREATE TABLE T(a BLOB);" + +"INSERT INTO t(rowid,a) VALUES(1, 'def'),(2, 'XYZ');" + ); + Sqlite.Blob b = db.blobOpen("main", "t", "a", + db.lastInsertRowId(), true); + affirm( 3==b.bytes() ); + b.write(new byte[] {100, 101, 102 /*"DEF"*/}, 0); + b.close(); + Sqlite.Stmt stmt = db.prepare("SELECT length(a), a FROM t ORDER BY a"); + affirm( stmt.step() ); + affirm( 3 == stmt.columnInt(0) ); + affirm( "def".equals(stmt.columnText16(1)) ); + stmt.finalizeStmt(); + + b = db.blobOpen("main", "t", "a", db.lastInsertRowId(), false); + final byte[] tgt = new byte[3]; + b.read( tgt, 0 ); + affirm( 100==tgt[0] && 101==tgt[1] && 102==tgt[2], "DEF" ); + execSql(db,"UPDATE t SET a=zeroblob(10) WHERE rowid=2"); + b.close(); + b = db.blobOpen("main", "t", "a", db.lastInsertRowId(), true); + byte[] bw = new byte[]{ + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 + }; + b.write(bw, 0); + byte[] br = new byte[10]; + b.read(br, 0); + for( int i = 0; i < br.length; ++i ){ + affirm(bw[i] == br[i]); + } + b.close(); + db.close(); + } + + void testPrepareMulti(){ + final ValueHolder fCount = new ValueHolder<>(0); + final ValueHolder mCount = new ValueHolder<>(0); + try (Sqlite db = openDb()) { + execSql(db, "create table t(a); insert into t(a) values(1),(2),(3)"); + db.createFunction("counter", -1, new ScalarFunction(){ + @Override public void xFunc(SqlFunction.Arguments args){ + ++fCount.value; + args.resultNull(); + } + } + ); + final Sqlite.PrepareMulti pm = new Sqlite.PrepareMultiFinalize( + new Sqlite.PrepareMulti() { + @Override public void call(Sqlite.Stmt q){ + ++mCount.value; + while(q.step()){} + } + } + ); + final String sql = "select counter(*) from t;"+ + "select counter(*) from t; /* comment */"+ + "select counter(*) from t; -- comment\n" + ; + db.prepareMulti(sql, pm); + } + affirm( 3 == mCount.value ); + affirm( 9 == fCount.value ); + } + + + /* Copy/paste/rename this to add new tests. */ + private void _testTemplate(){ + try (Sqlite db = openDb()) { + Sqlite.Stmt stmt = db.prepare("SELECT 1"); + stmt.finalizeStmt(); + } + } + + private void runTests(boolean fromThread) throws Exception { + List mlist = testMethods; + affirm( null!=mlist ); + if( shuffle ){ + mlist = new ArrayList<>( testMethods.subList(0, testMethods.size()) ); + java.util.Collections.shuffle(mlist); + } + if( (!fromThread && listRunTests>0) || listRunTests>1 ){ + synchronized(this.getClass()){ + if( !fromThread ){ + out("Initial test"," list: "); + for(java.lang.reflect.Method m : testMethods){ + out(m.getName()+" "); + } + outln(); + outln("(That list excludes some which are hard-coded to run.)"); + } + out("Running"," tests: "); + for(java.lang.reflect.Method m : mlist){ + out(m.getName()+" "); + } + outln(); + } + } + for(java.lang.reflect.Method m : mlist){ + nap(); + try{ + m.invoke(this); + }catch(java.lang.reflect.InvocationTargetException e){ + outln("FAILURE: ",m.getName(),"(): ", e.getCause()); + throw e; + } + } + synchronized( this.getClass() ){ + ++nTestRuns; + } + } + + public void run() { + try { + runTests(0!=this.tId); + }catch(Exception e){ + synchronized( listErrors ){ + listErrors.add(e); + } + }finally{ + Sqlite.uncacheThread(); + } + } + + /** + Runs the basic sqlite3 JNI binding sanity-check suite. + + CLI flags: + + -q|-quiet: disables most test output. + + -t|-thread N: runs the tests in N threads + concurrently. Default=1. + + -r|-repeat N: repeats the tests in a loop N times, each one + consisting of the -thread value's threads. + + -shuffle: randomizes the order of most of the test functions. + + -naps: sleep small random intervals between tests in order to add + some chaos for cross-thread contention. + + -list-tests: outputs the list of tests being run, minus some + which are hard-coded. In multi-threaded mode, use this twice to + to emit the list run by each thread (which may differ from the initial + list, in particular if -shuffle is used). + + -fail: forces an exception to be thrown during the test run. Use + with -shuffle to make its appearance unpredictable. + + -v: emit some developer-mode info at the end. + */ + public static void main(String[] args) throws Exception { + int nThread = 1; + int nRepeat = 1; + boolean doSomethingForDev = false; + boolean forceFail = false; + boolean sqlLog = false; + boolean configLog = false; + boolean squelchTestOutput = false; + for( int i = 0; i < args.length; ){ + String arg = args[i++]; + if(arg.startsWith("-")){ + arg = arg.replaceFirst("-+",""); + if(arg.equals("v")){ + doSomethingForDev = true; + //listBoundMethods(); + }else if(arg.equals("t") || arg.equals("thread")){ + nThread = Integer.parseInt(args[i++]); + }else if(arg.equals("r") || arg.equals("repeat")){ + nRepeat = Integer.parseInt(args[i++]); + }else if(arg.equals("shuffle")){ + shuffle = true; + }else if(arg.equals("list-tests")){ + ++listRunTests; + }else if(arg.equals("fail")){ + forceFail = true; + }else if(arg.equals("sqllog")){ + sqlLog = true; + }else if(arg.equals("configlog")){ + configLog = true; + }else if(arg.equals("naps")){ + takeNaps = true; + }else if(arg.equals("q") || arg.equals("quiet")){ + squelchTestOutput = true; + }else{ + throw new IllegalArgumentException("Unhandled flag:"+arg); + } + } + } + + if( sqlLog ){ + if( Sqlite.compileOptionUsed("ENABLE_SQLLOG") ){ + Sqlite.libConfigSqlLog( new Sqlite.ConfigSqlLog() { + @Override public void call(Sqlite db, String msg, int op){ + switch(op){ + case 0: outln("Opening db: ",db); break; + case 1: outln("SQL ",db,": ",msg); break; + case 2: outln("Closing db: ",db); break; + } + } + } + ); + }else{ + outln("WARNING: -sqllog is not active because library was built ", + "without SQLITE_ENABLE_SQLLOG."); + } + } + if( configLog ){ + Sqlite.libConfigLog( new Sqlite.ConfigLog() { + @Override public void call(int code, String msg){ + outln("ConfigLog: ",Sqlite.errstr(code),": ", msg); + } + } + ); + } + + quietMode = squelchTestOutput; + outln("If you just saw warning messages regarding CallStaticObjectMethod, ", + "you are very likely seeing the side effects of a known openjdk8 ", + "bug. It is unsightly but does not affect the library."); + + { + // Build list of tests to run from the methods named test*(). + testMethods = new ArrayList<>(); + int nSkipped = 0; + for(final java.lang.reflect.Method m : Tester2.class.getDeclaredMethods()){ + final String name = m.getName(); + if( name.equals("testFail") ){ + if( forceFail ){ + testMethods.add(m); + } + }else if( !m.isAnnotationPresent( ManualTest.class ) ){ + if( nThread>1 && m.isAnnotationPresent( SingleThreadOnly.class ) ){ + if( 0==nSkipped++ ){ + out("Skipping tests in multi-thread mode:"); + } + out(" "+name+"()"); + }else if( name.startsWith("test") ){ + testMethods.add(m); + } + } + } + if( nSkipped>0 ) out("\n"); + } + + final long timeStart = System.currentTimeMillis(); + outln("libversion_number: ", + Sqlite.libVersionNumber(),"\n", + Sqlite.libVersion(),"\n",Sqlite.libSourceId(),"\n", + "SQLITE_THREADSAFE=",CApi.sqlite3_threadsafe()); + final boolean showLoopCount = (nRepeat>1 && nThread>1); + if( showLoopCount ){ + outln("Running ",nRepeat," loop(s) with ",nThread," thread(s) each."); + } + if( takeNaps ) outln("Napping between tests is enabled."); + int nLoop = 0; + for( int n = 0; n < nRepeat; ++n ){ + ++nLoop; + if( showLoopCount ) out((1==nLoop ? "" : " ")+nLoop); + if( nThread<=1 ){ + new Tester2(0).runTests(false); + continue; + } + Tester2.mtMode = true; + final ExecutorService ex = Executors.newFixedThreadPool( nThread ); + for( int i = 0; i < nThread; ++i ){ + ex.submit( new Tester2(i), i ); + } + ex.shutdown(); + try{ + ex.awaitTermination(nThread*200, java.util.concurrent.TimeUnit.MILLISECONDS); + ex.shutdownNow(); + }catch (InterruptedException ie){ + ex.shutdownNow(); + Thread.currentThread().interrupt(); + } + if( !listErrors.isEmpty() ){ + quietMode = false; + outln("TEST ERRORS:"); + Exception err = null; + for( Exception e : listErrors ){ + e.printStackTrace(); + if( null==err ) err = e; + } + if( null!=err ) throw err; + } + } + if( showLoopCount ) outln(); + quietMode = false; + + final long timeEnd = System.currentTimeMillis(); + outln("Tests done. Metrics across ",nTestRuns," total iteration(s):"); + outln("\tAssertions checked: ",affirmCount); + outln("\tDatabases opened: ",metrics.dbOpen); + if( doSomethingForDev ){ + CApi.sqlite3_jni_internal_details(); + } + affirm( 0==Sqlite.libReleaseMemory(1) ); + CApi.sqlite3_shutdown(); + int nMethods = 0; + int nNatives = 0; + int nCanonical = 0; + final java.lang.reflect.Method[] declaredMethods = + CApi.class.getDeclaredMethods(); + for(java.lang.reflect.Method m : declaredMethods){ + final int mod = m.getModifiers(); + if( 0!=(mod & java.lang.reflect.Modifier.STATIC) ){ + final String name = m.getName(); + if(name.startsWith("sqlite3_")){ + ++nMethods; + if( 0!=(mod & java.lang.reflect.Modifier.NATIVE) ){ + ++nNatives; + } + } + } + } + outln("\tCApi.sqlite3_*() methods: "+ + nMethods+" total, with "+ + nNatives+" native, "+ + (nMethods - nNatives)+" Java" + ); + outln("\tTotal test time = " + +(timeEnd - timeStart)+"ms"); + } +} diff --git a/ext/jni/src/org/sqlite/jni/wrapper1/ValueHolder.java b/ext/jni/src/org/sqlite/jni/wrapper1/ValueHolder.java new file mode 100644 index 0000000000..7549bb97b2 --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/wrapper1/ValueHolder.java @@ -0,0 +1,25 @@ +/* +** 2023-10-16 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains the ValueHolder utility class. +*/ +package org.sqlite.jni.wrapper1; + +/** + A helper class which simply holds a single value. Its primary use + is for communicating values out of anonymous callbacks, as doing so + requires a "final" reference. +*/ +public class ValueHolder { + public T value; + public ValueHolder(){} + public ValueHolder(T v){value = v;} +} diff --git a/ext/jni/src/org/sqlite/jni/wrapper1/WindowFunction.java b/ext/jni/src/org/sqlite/jni/wrapper1/WindowFunction.java new file mode 100644 index 0000000000..a3905567d4 --- /dev/null +++ b/ext/jni/src/org/sqlite/jni/wrapper1/WindowFunction.java @@ -0,0 +1,42 @@ +/* +** 2023-10-16 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file is part of the wrapper1 interface for sqlite3. +*/ +package org.sqlite.jni.wrapper1; + +/** + A SqlFunction implementation for window functions. The T type + represents the type of data accumulated by this function while it + works. e.g. a SUM()-like UDF might use Integer or Long and a + CONCAT()-like UDF might use a StringBuilder or a List. +*/ +public abstract class WindowFunction extends AggregateFunction { + + /** + As for the xInverse() argument of the C API's + sqlite3_create_window_function(). If this function throws, the + exception is reported via sqlite3_result_error(). + */ + public abstract void xInverse(SqlFunction.Arguments args); + + /** + As for the xValue() argument of the C API's + sqlite3_create_window_function(). If this function throws, it is + translated into sqlite3_result_error(). + + Note that the passed-in object will not actually contain any + arguments for xValue() but will contain the context object needed + for setting the call's result or error state. + */ + public abstract void xValue(SqlFunction.Arguments args); + +} diff --git a/ext/jni/src/tests/000-000-sanity.test b/ext/jni/src/tests/000-000-sanity.test new file mode 100644 index 0000000000..4ccbece31c --- /dev/null +++ b/ext/jni/src/tests/000-000-sanity.test @@ -0,0 +1,53 @@ +/* +** This is a comment. There are many like it but this one is mine. +** +** SCRIPT_MODULE_NAME: sanity-check +** xMIXED_MODULE_NAME: mixed-module +** xMODULE_NAME: module-name +** xREQUIRED_PROPERTIES: small fast reliable +** xREQUIRED_PROPERTIES: RECURSIVE_TRIGGERS +** xREQUIRED_PROPERTIES: TEMPSTORE_FILE TEMPSTORE_MEM +** xREQUIRED_PROPERTIES: AUTOVACUUM INCRVACUUM +** +*/ +--print starting up 😃 +--close all +--oom +--db 0 +--new my.db +--null zilch +--testcase 1.0 +SELECT 1, null; +--result 1 zilch +--glob *zil* +--notglob *ZIL* +SELECT 1, 2; +intentional error; +--run +--testcase json-1 +SELECT json_array(1,2,3) +--json [1,2,3] +--testcase tableresult-1 + select 1, 'a'; + select 2, 'b'; +--tableresult + # [a-z] + 2 b +--end +--testcase json-block-1 + select json_array(1,2,3); + select json_object('a',1,'b',2); +--json-block + [1,2,3] + {"a":1,"b":2} +--end +--testcase col-names-on +--column-names 1 + select 1 as 'a', 2 as 'b'; +--result a 1 b 2 +--testcase col-names-off +--column-names 0 + select 1 as 'a', 2 as 'b'; +--result 1 2 +--close +--print reached the end 😃 diff --git a/ext/jni/src/tests/000-001-ignored.test b/ext/jni/src/tests/000-001-ignored.test new file mode 100644 index 0000000000..5af852e197 --- /dev/null +++ b/ext/jni/src/tests/000-001-ignored.test @@ -0,0 +1,9 @@ +/* +** This script must be marked as ignored because it contains +** content which triggers that condition. +** +** SCRIPT_MODULE_NAME: ignored +** +*/ + +| diff --git a/ext/jni/src/tests/900-001-fts.test b/ext/jni/src/tests/900-001-fts.test new file mode 100644 index 0000000000..65285e86b0 --- /dev/null +++ b/ext/jni/src/tests/900-001-fts.test @@ -0,0 +1,12 @@ +/* +** SCRIPT_MODULE_NAME: fts5-sanity-checks +** xREQUIRED_PROPERTIES: FTS5 +** +*/ + +--testcase 1.0 +CREATE VIRTUAL TABLE email USING fts5(sender, title, body); +insert into email values('fred','Help!','Dear Sir...'); +insert into email values('barney','Assistance','Dear Madam...'); +select * from email where email match 'assistance'; +--result barney Assistance {Dear Madam...} diff --git a/ext/misc/README.md b/ext/misc/README.md new file mode 100644 index 0000000000..6d34ab9afb --- /dev/null +++ b/ext/misc/README.md @@ -0,0 +1,50 @@ +## Miscellaneous Extensions + +This folder contains a collection of smaller loadable extensions. +See for instructions on how +to compile and use loadable extensions. +Each extension in this folder is implemented in a single file of C code. + +Each source file contains a description in its header comment. See the +header comments for details about each extension. Additional notes are +as follows: + + * **csv.c** — A [virtual table](https://sqlite.org/vtab.html) + for reading + [Comma-Separated-Value (CSV) files](https://en.wikipedia.org/wiki/Comma-separated_values). + + * **dbdump.c** — This is not actually a loadable extension, but + rather a library that implements an approximate equivalent to the + ".dump" command of the + [command-line shell](https://sqlite.org/cli.html). + + * **json1.c** — Various SQL functions and table-valued functions + for processing JSON. This extension is already built into the + [SQLite amalgamation](https://sqlite.org/amalgamation.html). See + for additional information. + + * **rot13.c** — This file implements the very simple rot13() + substitution function. This file makes a good template for implementing + new custom SQL functions for SQLite. + + * **series.c** — This is an implementation of the + "generate_series" [virtual table](https://sqlite.org/vtab.html). + It can make a good template for new custom virtual table implementations. + + * **shathree.c** — An implementation of the sha3() and + sha3_query() SQL functions. The file is named "shathree.c" instead + of "sha3.c" because the default entry point names in SQLite are based + on the source filename with digits removed, so if we used the name + "sha3.c" then the entry point would conflict with the prior "sha1.c" + extension. + + * **unionvtab.c** — Implementation of the unionvtab and + [swarmvtab](https://sqlite.org/swarmvtab.html) virtual tables. + These virtual tables allow a single + large table to be spread out across multiple database files. In the + case of swarmvtab, the individual database files can be attached on + demand. + + * **zipfile.c** — A [virtual table](https://sqlite.org/vtab.html) + that can read and write a + [ZIP archive](https://en.wikipedia.org/wiki/Zip_%28file_format%29). diff --git a/ext/misc/amatch.c b/ext/misc/amatch.c index 852491988a..587c610b95 100644 --- a/ext/misc/amatch.c +++ b/ext/misc/amatch.c @@ -482,9 +482,9 @@ struct amatch_rule { amatch_rule *pNext; /* Next rule in order of increasing rCost */ char *zFrom; /* Transform from (a string from user input) */ amatch_cost rCost; /* Cost of this transformation */ - amatch_langid iLang; /* The langauge to which this rule belongs */ + amatch_langid iLang; /* The language to which this rule belongs */ amatch_len nFrom, nTo; /* Length of the zFrom and zTo strings */ - char zTo[4]; /* Tranform to V.W value (extra space appended) */ + char zTo[4]; /* Transform to V.W value (extra space appended) */ }; /* @@ -514,7 +514,7 @@ struct amatch_cursor { sqlite3_int64 iRowid; /* The rowid of the current word */ amatch_langid iLang; /* Use this language ID */ amatch_cost rLimit; /* Maximum cost of any term */ - int nBuf; /* Space allocated for zBuf */ + sqlite3_int64 nBuf; /* Space allocated for zBuf */ int oomErr; /* True following an OOM error */ int nWord; /* Number of amatch_word objects */ char *zBuf; /* Temp-use buffer space */ @@ -619,16 +619,16 @@ static int amatchLoadOneRule( if( p->rDel==0 || p->rDel>rCost ) p->rDel = rCost; }else { - pRule = sqlite3_malloc( sizeof(*pRule) + nFrom + nTo ); + pRule = sqlite3_malloc64( sizeof(*pRule) + nFrom + nTo ); if( pRule==0 ){ rc = SQLITE_NOMEM; }else{ memset(pRule, 0, sizeof(*pRule)); pRule->zFrom = &pRule->zTo[nTo+1]; - pRule->nFrom = nFrom; + pRule->nFrom = (amatch_len)nFrom; memcpy(pRule->zFrom, zFrom, nFrom+1); memcpy(pRule->zTo, zTo, nTo+1); - pRule->nTo = nTo; + pRule->nTo = (amatch_len)nTo; pRule->rCost = rCost; pRule->iLang = (int)iLang; } @@ -738,16 +738,16 @@ static int amatchLoadRules( ** `mno` becomes mno */ static char *amatchDequote(const char *zIn){ - int nIn; /* Size of input string, in bytes */ + sqlite3_int64 nIn; /* Size of input string, in bytes */ char *zOut; /* Output (dequoted) string */ - nIn = (int)strlen(zIn); - zOut = sqlite3_malloc(nIn+1); + nIn = strlen(zIn); + zOut = sqlite3_malloc64(nIn+1); if( zOut ){ char q = zIn[0]; /* Quote character (if any ) */ if( q!='[' && q!= '\'' && q!='"' && q!='`' ){ - memcpy(zOut, zIn, nIn+1); + memcpy(zOut, zIn, (size_t)(nIn+1)); }else{ int iOut = 0; /* Index of next byte to write to output */ int iIn; /* Index of next byte to read from input */ @@ -900,6 +900,7 @@ static int amatchConnect( rc = amatchLoadRules(db, pNew, pzErr); } if( rc==SQLITE_OK ){ + sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS); rc = sqlite3_declare_vtab(db, "CREATE TABLE x(word,distance,language," "command HIDDEN,nword HIDDEN)" @@ -1001,7 +1002,6 @@ static void amatchWriteCost(amatch_word *pWord){ /* Circumvent compiler warnings about the use of strcpy() by supplying ** our own implementation. */ -#if defined(__OpenBSD__) static void amatchStrcpy(char *dest, const char *src){ while( (*(dest++) = *(src++))!=0 ){} } @@ -1009,11 +1009,6 @@ static void amatchStrcat(char *dest, const char *src){ while( *dest ) dest++; amatchStrcpy(dest, src); } -#else -# define amatchStrcpy strcpy -# define amatchStrcat strcat -#endif - /* ** Add a new amatch_word object to the queue. @@ -1044,7 +1039,7 @@ static void amatchAddWord( nTail = (int)strlen(zWordTail); if( nBase+nTail+3>pCur->nBuf ){ pCur->nBuf = nBase+nTail+100; - pCur->zBuf = sqlite3_realloc(pCur->zBuf, pCur->nBuf); + pCur->zBuf = sqlite3_realloc64(pCur->zBuf, pCur->nBuf); if( pCur->zBuf==0 ){ pCur->nBuf = 0; return; @@ -1075,13 +1070,13 @@ static void amatchAddWord( } return; } - pWord = sqlite3_malloc( sizeof(*pWord) + nBase + nTail - 1 ); + pWord = sqlite3_malloc64( sizeof(*pWord) + nBase + nTail - 1 ); if( pWord==0 ) return; memset(pWord, 0, sizeof(*pWord)); pWord->rCost = rCost; pWord->iSeq = pCur->nWord++; amatchWriteCost(pWord); - pWord->nMatch = nMatch; + pWord->nMatch = (short)nMatch; pWord->pNext = pCur->pAllWords; pCur->pAllWords = pWord; pWord->sCost.zKey = pWord->zCost; @@ -1110,13 +1105,13 @@ static int amatchNext(sqlite3_vtab_cursor *cur){ amatch_avl *pNode; int isMatch = 0; amatch_vtab *p = pCur->pVtab; - int nWord; + sqlite3_int64 nWord; int rc; int i; const char *zW; amatch_rule *pRule; char *zBuf = 0; - char nBuf = 0; + sqlite3_int64 nBuf = 0; char zNext[8]; char zNextIn[8]; int nNextIn; @@ -1162,8 +1157,8 @@ static int amatchNext(sqlite3_vtab_cursor *cur){ #endif nWord = (int)strlen(pWord->zWord+2); if( nWord+20>nBuf ){ - nBuf = nWord+100; - zBuf = sqlite3_realloc(zBuf, nBuf); + nBuf = (char)(nWord+100); + zBuf = sqlite3_realloc64(zBuf, nBuf); if( zBuf==0 ) return SQLITE_NOMEM; } amatchStrcpy(zBuf, pWord->zWord+2); @@ -1479,7 +1474,9 @@ static sqlite3_module amatchModule = { 0, /* xRename */ 0, /* xSavepoint */ 0, /* xRelease */ - 0 /* xRollbackTo */ + 0, /* xRollbackTo */ + 0, /* xShadowName */ + 0 /* xIntegrity */ }; #endif /* SQLITE_OMIT_VIRTUALTABLE */ diff --git a/ext/misc/anycollseq.c b/ext/misc/anycollseq.c new file mode 100644 index 0000000000..27b7049d5f --- /dev/null +++ b/ext/misc/anycollseq.c @@ -0,0 +1,58 @@ +/* +** 2017-04-16 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** This file implements a run-time loadable extension to SQLite that +** registers a sqlite3_collation_needed() callback to register a fake +** collating function for any unknown collating sequence. The fake +** collating function works like BINARY. +** +** This extension can be used to load schemas that contain one or more +** unknown collating sequences. +*/ +#include "sqlite3ext.h" +SQLITE_EXTENSION_INIT1 +#include + +static int anyCollFunc( + void *NotUsed, + int nKey1, const void *pKey1, + int nKey2, const void *pKey2 +){ + int rc, n; + n = nKey1 +#include + +/* The append mark at the end of the database is: +** +** Start-Of-SQLite3-NNNNNNNN +** 123456789 123456789 12345 +** +** The NNNNNNNN represents a 64-bit big-endian unsigned integer which is +** the offset to page 1, and also the length of the prefix content. +*/ +#define APND_MARK_PREFIX "Start-Of-SQLite3-" +#define APND_MARK_PREFIX_SZ 17 +#define APND_MARK_FOS_SZ 8 +#define APND_MARK_SIZE (APND_MARK_PREFIX_SZ+APND_MARK_FOS_SZ) + +/* +** Maximum size of the combined prefix + database + append-mark. This +** must be less than 0x40000000 to avoid locking issues on Windows. +*/ +#define APND_MAX_SIZE (0x40000000) + +/* +** Try to align the database to an even multiple of APND_ROUNDUP bytes. +*/ +#ifndef APND_ROUNDUP +#define APND_ROUNDUP 4096 +#endif +#define APND_ALIGN_MASK ((sqlite3_int64)(APND_ROUNDUP-1)) +#define APND_START_ROUNDUP(fsz) (((fsz)+APND_ALIGN_MASK) & ~APND_ALIGN_MASK) + +/* +** Forward declaration of objects used by this utility +*/ +typedef struct sqlite3_vfs ApndVfs; +typedef struct ApndFile ApndFile; + +/* Access to a lower-level VFS that (might) implement dynamic loading, +** access to randomness, etc. +*/ +#define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData)) +#define ORIGFILE(p) ((sqlite3_file*)(((ApndFile*)(p))+1)) + +/* An open appendvfs file +** +** An instance of this structure describes the appended database file. +** A separate sqlite3_file object is always appended. The appended +** sqlite3_file object (which can be accessed using ORIGFILE()) describes +** the entire file, including the prefix, the database, and the +** append-mark. +** +** The structure of an AppendVFS database is like this: +** +** +-------------+---------+----------+-------------+ +** | prefix-file | padding | database | append-mark | +** +-------------+---------+----------+-------------+ +** ^ ^ +** | | +** iPgOne iMark +** +** +** "prefix file" - file onto which the database has been appended. +** "padding" - zero or more bytes inserted so that "database" +** starts on an APND_ROUNDUP boundary +** "database" - The SQLite database file +** "append-mark" - The 25-byte "Start-Of-SQLite3-NNNNNNNN" that indicates +** the offset from the start of prefix-file to the start +** of "database". +** +** The size of the database is iMark - iPgOne. +** +** The NNNNNNNN in the "Start-Of-SQLite3-NNNNNNNN" suffix is the value +** of iPgOne stored as a big-ending 64-bit integer. +** +** iMark will be the size of the underlying file minus 25 (APND_MARKSIZE). +** Or, iMark is -1 to indicate that it has not yet been written. +*/ +struct ApndFile { + sqlite3_file base; /* Subclass. MUST BE FIRST! */ + sqlite3_int64 iPgOne; /* Offset to the start of the database */ + sqlite3_int64 iMark; /* Offset of the append mark. -1 if unwritten */ + /* Always followed by another sqlite3_file that describes the whole file */ +}; + +/* +** Methods for ApndFile +*/ +static int apndClose(sqlite3_file*); +static int apndRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst); +static int apndWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst); +static int apndTruncate(sqlite3_file*, sqlite3_int64 size); +static int apndSync(sqlite3_file*, int flags); +static int apndFileSize(sqlite3_file*, sqlite3_int64 *pSize); +static int apndLock(sqlite3_file*, int); +static int apndUnlock(sqlite3_file*, int); +static int apndCheckReservedLock(sqlite3_file*, int *pResOut); +static int apndFileControl(sqlite3_file*, int op, void *pArg); +static int apndSectorSize(sqlite3_file*); +static int apndDeviceCharacteristics(sqlite3_file*); +static int apndShmMap(sqlite3_file*, int iPg, int pgsz, int, void volatile**); +static int apndShmLock(sqlite3_file*, int offset, int n, int flags); +static void apndShmBarrier(sqlite3_file*); +static int apndShmUnmap(sqlite3_file*, int deleteFlag); +static int apndFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp); +static int apndUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p); + +/* +** Methods for ApndVfs +*/ +static int apndOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *); +static int apndDelete(sqlite3_vfs*, const char *zName, int syncDir); +static int apndAccess(sqlite3_vfs*, const char *zName, int flags, int *); +static int apndFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut); +static void *apndDlOpen(sqlite3_vfs*, const char *zFilename); +static void apndDlError(sqlite3_vfs*, int nByte, char *zErrMsg); +static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void); +static void apndDlClose(sqlite3_vfs*, void*); +static int apndRandomness(sqlite3_vfs*, int nByte, char *zOut); +static int apndSleep(sqlite3_vfs*, int microseconds); +static int apndCurrentTime(sqlite3_vfs*, double*); +static int apndGetLastError(sqlite3_vfs*, int, char *); +static int apndCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*); +static int apndSetSystemCall(sqlite3_vfs*, const char*,sqlite3_syscall_ptr); +static sqlite3_syscall_ptr apndGetSystemCall(sqlite3_vfs*, const char *z); +static const char *apndNextSystemCall(sqlite3_vfs*, const char *zName); + +static sqlite3_vfs apnd_vfs = { + 3, /* iVersion (set when registered) */ + 0, /* szOsFile (set when registered) */ + 1024, /* mxPathname */ + 0, /* pNext */ + "apndvfs", /* zName */ + 0, /* pAppData (set when registered) */ + apndOpen, /* xOpen */ + apndDelete, /* xDelete */ + apndAccess, /* xAccess */ + apndFullPathname, /* xFullPathname */ + apndDlOpen, /* xDlOpen */ + apndDlError, /* xDlError */ + apndDlSym, /* xDlSym */ + apndDlClose, /* xDlClose */ + apndRandomness, /* xRandomness */ + apndSleep, /* xSleep */ + apndCurrentTime, /* xCurrentTime */ + apndGetLastError, /* xGetLastError */ + apndCurrentTimeInt64, /* xCurrentTimeInt64 */ + apndSetSystemCall, /* xSetSystemCall */ + apndGetSystemCall, /* xGetSystemCall */ + apndNextSystemCall /* xNextSystemCall */ +}; + +static const sqlite3_io_methods apnd_io_methods = { + 3, /* iVersion */ + apndClose, /* xClose */ + apndRead, /* xRead */ + apndWrite, /* xWrite */ + apndTruncate, /* xTruncate */ + apndSync, /* xSync */ + apndFileSize, /* xFileSize */ + apndLock, /* xLock */ + apndUnlock, /* xUnlock */ + apndCheckReservedLock, /* xCheckReservedLock */ + apndFileControl, /* xFileControl */ + apndSectorSize, /* xSectorSize */ + apndDeviceCharacteristics, /* xDeviceCharacteristics */ + apndShmMap, /* xShmMap */ + apndShmLock, /* xShmLock */ + apndShmBarrier, /* xShmBarrier */ + apndShmUnmap, /* xShmUnmap */ + apndFetch, /* xFetch */ + apndUnfetch /* xUnfetch */ +}; + +/* +** Close an apnd-file. +*/ +static int apndClose(sqlite3_file *pFile){ + pFile = ORIGFILE(pFile); + return pFile->pMethods->xClose(pFile); +} + +/* +** Read data from an apnd-file. +*/ +static int apndRead( + sqlite3_file *pFile, + void *zBuf, + int iAmt, + sqlite_int64 iOfst +){ + ApndFile *paf = (ApndFile *)pFile; + pFile = ORIGFILE(pFile); + return pFile->pMethods->xRead(pFile, zBuf, iAmt, paf->iPgOne+iOfst); +} + +/* +** Add the append-mark onto what should become the end of the file. +* If and only if this succeeds, internal ApndFile.iMark is updated. +* Parameter iWriteEnd is the appendvfs-relative offset of the new mark. +*/ +static int apndWriteMark( + ApndFile *paf, + sqlite3_file *pFile, + sqlite_int64 iWriteEnd +){ + sqlite_int64 iPgOne = paf->iPgOne; + unsigned char a[APND_MARK_SIZE]; + int i = APND_MARK_FOS_SZ; + int rc; + assert(pFile == ORIGFILE(paf)); + memcpy(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ); + while( --i >= 0 ){ + a[APND_MARK_PREFIX_SZ+i] = (unsigned char)(iPgOne & 0xff); + iPgOne >>= 8; + } + iWriteEnd += paf->iPgOne; + if( SQLITE_OK==(rc = pFile->pMethods->xWrite + (pFile, a, APND_MARK_SIZE, iWriteEnd)) ){ + paf->iMark = iWriteEnd; + } + return rc; +} + +/* +** Write data to an apnd-file. +*/ +static int apndWrite( + sqlite3_file *pFile, + const void *zBuf, + int iAmt, + sqlite_int64 iOfst +){ + ApndFile *paf = (ApndFile *)pFile; + sqlite_int64 iWriteEnd = iOfst + iAmt; + if( iWriteEnd>=APND_MAX_SIZE ) return SQLITE_FULL; + pFile = ORIGFILE(pFile); + /* If append-mark is absent or will be overwritten, write it. */ + if( paf->iMark < 0 || paf->iPgOne + iWriteEnd > paf->iMark ){ + int rc = apndWriteMark(paf, pFile, iWriteEnd); + if( SQLITE_OK!=rc ) return rc; + } + return pFile->pMethods->xWrite(pFile, zBuf, iAmt, paf->iPgOne+iOfst); +} + +/* +** Truncate an apnd-file. +*/ +static int apndTruncate(sqlite3_file *pFile, sqlite_int64 size){ + ApndFile *paf = (ApndFile *)pFile; + pFile = ORIGFILE(pFile); + /* The append mark goes out first so truncate failure does not lose it. */ + if( SQLITE_OK!=apndWriteMark(paf, pFile, size) ) return SQLITE_IOERR; + /* Truncate underlying file just past append mark */ + return pFile->pMethods->xTruncate(pFile, paf->iMark+APND_MARK_SIZE); +} + +/* +** Sync an apnd-file. +*/ +static int apndSync(sqlite3_file *pFile, int flags){ + pFile = ORIGFILE(pFile); + return pFile->pMethods->xSync(pFile, flags); +} + +/* +** Return the current file-size of an apnd-file. +** If the append mark is not yet there, the file-size is 0. +*/ +static int apndFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){ + ApndFile *paf = (ApndFile *)pFile; + *pSize = ( paf->iMark >= 0 )? (paf->iMark - paf->iPgOne) : 0; + return SQLITE_OK; +} + +/* +** Lock an apnd-file. +*/ +static int apndLock(sqlite3_file *pFile, int eLock){ + pFile = ORIGFILE(pFile); + return pFile->pMethods->xLock(pFile, eLock); +} + +/* +** Unlock an apnd-file. +*/ +static int apndUnlock(sqlite3_file *pFile, int eLock){ + pFile = ORIGFILE(pFile); + return pFile->pMethods->xUnlock(pFile, eLock); +} + +/* +** Check if another file-handle holds a RESERVED lock on an apnd-file. +*/ +static int apndCheckReservedLock(sqlite3_file *pFile, int *pResOut){ + pFile = ORIGFILE(pFile); + return pFile->pMethods->xCheckReservedLock(pFile, pResOut); +} + +/* +** File control method. For custom operations on an apnd-file. +*/ +static int apndFileControl(sqlite3_file *pFile, int op, void *pArg){ + ApndFile *paf = (ApndFile *)pFile; + int rc; + pFile = ORIGFILE(pFile); + if( op==SQLITE_FCNTL_SIZE_HINT ) *(sqlite3_int64*)pArg += paf->iPgOne; + rc = pFile->pMethods->xFileControl(pFile, op, pArg); + if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){ + *(char**)pArg = sqlite3_mprintf("apnd(%lld)/%z", paf->iPgOne,*(char**)pArg); + } + return rc; +} + +/* +** Return the sector-size in bytes for an apnd-file. +*/ +static int apndSectorSize(sqlite3_file *pFile){ + pFile = ORIGFILE(pFile); + return pFile->pMethods->xSectorSize(pFile); +} + +/* +** Return the device characteristic flags supported by an apnd-file. +*/ +static int apndDeviceCharacteristics(sqlite3_file *pFile){ + pFile = ORIGFILE(pFile); + return pFile->pMethods->xDeviceCharacteristics(pFile); +} + +/* Create a shared memory file mapping */ +static int apndShmMap( + sqlite3_file *pFile, + int iPg, + int pgsz, + int bExtend, + void volatile **pp +){ + pFile = ORIGFILE(pFile); + return pFile->pMethods->xShmMap(pFile,iPg,pgsz,bExtend,pp); +} + +/* Perform locking on a shared-memory segment */ +static int apndShmLock(sqlite3_file *pFile, int offset, int n, int flags){ + pFile = ORIGFILE(pFile); + return pFile->pMethods->xShmLock(pFile,offset,n,flags); +} + +/* Memory barrier operation on shared memory */ +static void apndShmBarrier(sqlite3_file *pFile){ + pFile = ORIGFILE(pFile); + pFile->pMethods->xShmBarrier(pFile); +} + +/* Unmap a shared memory segment */ +static int apndShmUnmap(sqlite3_file *pFile, int deleteFlag){ + pFile = ORIGFILE(pFile); + return pFile->pMethods->xShmUnmap(pFile,deleteFlag); +} + +/* Fetch a page of a memory-mapped file */ +static int apndFetch( + sqlite3_file *pFile, + sqlite3_int64 iOfst, + int iAmt, + void **pp +){ + ApndFile *p = (ApndFile *)pFile; + if( p->iMark < 0 || iOfst+iAmt > p->iMark ){ + return SQLITE_IOERR; /* Cannot read what is not yet there. */ + } + pFile = ORIGFILE(pFile); + return pFile->pMethods->xFetch(pFile, iOfst+p->iPgOne, iAmt, pp); +} + +/* Release a memory-mapped page */ +static int apndUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){ + ApndFile *p = (ApndFile *)pFile; + pFile = ORIGFILE(pFile); + return pFile->pMethods->xUnfetch(pFile, iOfst+p->iPgOne, pPage); +} + +/* +** Try to read the append-mark off the end of a file. Return the +** start of the appended database if the append-mark is present. +** If there is no valid append-mark, return -1; +** +** An append-mark is only valid if the NNNNNNNN start-of-database offset +** indicates that the appended database contains at least one page. The +** start-of-database value must be a multiple of 512. +*/ +static sqlite3_int64 apndReadMark(sqlite3_int64 sz, sqlite3_file *pFile){ + int rc, i; + sqlite3_int64 iMark; + int msbs = 8 * (APND_MARK_FOS_SZ-1); + unsigned char a[APND_MARK_SIZE]; + + if( APND_MARK_SIZE!=(sz & 0x1ff) ) return -1; + rc = pFile->pMethods->xRead(pFile, a, APND_MARK_SIZE, sz-APND_MARK_SIZE); + if( rc ) return -1; + if( memcmp(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ)!=0 ) return -1; + iMark = ((sqlite3_int64)(a[APND_MARK_PREFIX_SZ] & 0x7f)) << msbs; + for(i=1; i<8; i++){ + msbs -= 8; + iMark |= (sqlite3_int64)a[APND_MARK_PREFIX_SZ+i]< (sz - APND_MARK_SIZE - 512) ) return -1; + if( iMark & 0x1ff ) return -1; + return iMark; +} + +static const char apvfsSqliteHdr[] = "SQLite format 3"; +/* +** Check to see if the file is an appendvfs SQLite database file. +** Return true iff it is such. Parameter sz is the file's size. +*/ +static int apndIsAppendvfsDatabase(sqlite3_int64 sz, sqlite3_file *pFile){ + int rc; + char zHdr[16]; + sqlite3_int64 iMark = apndReadMark(sz, pFile); + if( iMark>=0 ){ + /* If file has the correct end-marker, the expected odd size, and the + ** SQLite DB type marker where the end-marker puts it, then it + ** is an appendvfs database. + */ + rc = pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), iMark); + if( SQLITE_OK==rc + && memcmp(zHdr, apvfsSqliteHdr, sizeof(zHdr))==0 + && (sz & 0x1ff) == APND_MARK_SIZE + && sz>=512+APND_MARK_SIZE + ){ + return 1; /* It's an appendvfs database */ + } + } + return 0; +} + +/* +** Check to see if the file is an ordinary SQLite database file. +** Return true iff so. Parameter sz is the file's size. +*/ +static int apndIsOrdinaryDatabaseFile(sqlite3_int64 sz, sqlite3_file *pFile){ + char zHdr[16]; + if( apndIsAppendvfsDatabase(sz, pFile) /* rule 2 */ + || (sz & 0x1ff) != 0 + || SQLITE_OK!=pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), 0) + || memcmp(zHdr, apvfsSqliteHdr, sizeof(zHdr))!=0 + ){ + return 0; + }else{ + return 1; + } +} + +/* +** Open an apnd file handle. +*/ +static int apndOpen( + sqlite3_vfs *pApndVfs, + const char *zName, + sqlite3_file *pFile, + int flags, + int *pOutFlags +){ + ApndFile *pApndFile = (ApndFile*)pFile; + sqlite3_file *pBaseFile = ORIGFILE(pFile); + sqlite3_vfs *pBaseVfs = ORIGVFS(pApndVfs); + int rc; + sqlite3_int64 sz = 0; + if( (flags & SQLITE_OPEN_MAIN_DB)==0 ){ + /* The appendvfs is not to be used for transient or temporary databases. + ** Just use the base VFS open to initialize the given file object and + ** open the underlying file. (Appendvfs is then unused for this file.) + */ + return pBaseVfs->xOpen(pBaseVfs, zName, pFile, flags, pOutFlags); + } + memset(pApndFile, 0, sizeof(ApndFile)); + pFile->pMethods = &apnd_io_methods; + pApndFile->iMark = -1; /* Append mark not yet written */ + + rc = pBaseVfs->xOpen(pBaseVfs, zName, pBaseFile, flags, pOutFlags); + if( rc==SQLITE_OK ){ + rc = pBaseFile->pMethods->xFileSize(pBaseFile, &sz); + if( rc ){ + pBaseFile->pMethods->xClose(pBaseFile); + } + } + if( rc ){ + pFile->pMethods = 0; + return rc; + } + if( apndIsOrdinaryDatabaseFile(sz, pBaseFile) ){ + /* The file being opened appears to be just an ordinary DB. Copy + ** the base dispatch-table so this instance mimics the base VFS. + */ + memmove(pApndFile, pBaseFile, pBaseVfs->szOsFile); + return SQLITE_OK; + } + pApndFile->iPgOne = apndReadMark(sz, pFile); + if( pApndFile->iPgOne>=0 ){ + pApndFile->iMark = sz - APND_MARK_SIZE; /* Append mark found */ + return SQLITE_OK; + } + if( (flags & SQLITE_OPEN_CREATE)==0 ){ + pBaseFile->pMethods->xClose(pBaseFile); + rc = SQLITE_CANTOPEN; + pFile->pMethods = 0; + }else{ + /* Round newly added appendvfs location to #define'd page boundary. + ** Note that nothing has yet been written to the underlying file. + ** The append mark will be written along with first content write. + ** Until then, paf->iMark value indicates it is not yet written. + */ + pApndFile->iPgOne = APND_START_ROUNDUP(sz); + } + return rc; +} + +/* +** Delete an apnd file. +** For an appendvfs, this could mean delete the appendvfs portion, +** leaving the appendee as it was before it gained an appendvfs. +** For now, this code deletes the underlying file too. +*/ +static int apndDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){ + return ORIGVFS(pVfs)->xDelete(ORIGVFS(pVfs), zPath, dirSync); +} + +/* +** All other VFS methods are pass-thrus. +*/ +static int apndAccess( + sqlite3_vfs *pVfs, + const char *zPath, + int flags, + int *pResOut +){ + return ORIGVFS(pVfs)->xAccess(ORIGVFS(pVfs), zPath, flags, pResOut); +} +static int apndFullPathname( + sqlite3_vfs *pVfs, + const char *zPath, + int nOut, + char *zOut +){ + return ORIGVFS(pVfs)->xFullPathname(ORIGVFS(pVfs),zPath,nOut,zOut); +} +static void *apndDlOpen(sqlite3_vfs *pVfs, const char *zPath){ + return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath); +} +static void apndDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){ + ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg); +} +static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){ + return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym); +} +static void apndDlClose(sqlite3_vfs *pVfs, void *pHandle){ + ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle); +} +static int apndRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){ + return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut); +} +static int apndSleep(sqlite3_vfs *pVfs, int nMicro){ + return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro); +} +static int apndCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){ + return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut); +} +static int apndGetLastError(sqlite3_vfs *pVfs, int a, char *b){ + return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b); +} +static int apndCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){ + return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p); +} +static int apndSetSystemCall( + sqlite3_vfs *pVfs, + const char *zName, + sqlite3_syscall_ptr pCall +){ + return ORIGVFS(pVfs)->xSetSystemCall(ORIGVFS(pVfs),zName,pCall); +} +static sqlite3_syscall_ptr apndGetSystemCall( + sqlite3_vfs *pVfs, + const char *zName +){ + return ORIGVFS(pVfs)->xGetSystemCall(ORIGVFS(pVfs),zName); +} +static const char *apndNextSystemCall(sqlite3_vfs *pVfs, const char *zName){ + return ORIGVFS(pVfs)->xNextSystemCall(ORIGVFS(pVfs), zName); +} + + +#ifdef _WIN32 +__declspec(dllexport) +#endif +/* +** This routine is called when the extension is loaded. +** Register the new VFS. +*/ +int sqlite3_appendvfs_init( + sqlite3 *db, + char **pzErrMsg, + const sqlite3_api_routines *pApi +){ + int rc = SQLITE_OK; + sqlite3_vfs *pOrig; + SQLITE_EXTENSION_INIT2(pApi); + (void)pzErrMsg; + (void)db; + pOrig = sqlite3_vfs_find(0); + if( pOrig==0 ) return SQLITE_ERROR; + apnd_vfs.iVersion = pOrig->iVersion; + apnd_vfs.pAppData = pOrig; + apnd_vfs.szOsFile = pOrig->szOsFile + sizeof(ApndFile); + rc = sqlite3_vfs_register(&apnd_vfs, 0); +#ifdef APPENDVFS_TEST + if( rc==SQLITE_OK ){ + rc = sqlite3_auto_extension((void(*)(void))apndvfsRegister); + } +#endif + if( rc==SQLITE_OK ) rc = SQLITE_OK_LOAD_PERMANENTLY; + return rc; +} diff --git a/ext/misc/base64.c b/ext/misc/base64.c new file mode 100644 index 0000000000..2da767bb0d --- /dev/null +++ b/ext/misc/base64.c @@ -0,0 +1,297 @@ +/* +** 2022-11-18 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** This is a SQLite extension for converting in either direction +** between a (binary) blob and base64 text. Base64 can transit a +** sane USASCII channel unmolested. It also plays nicely in CSV or +** written as TCL brace-enclosed literals or SQL string literals, +** and can be used unmodified in XML-like documents. +** +** This is an independent implementation of conversions specified in +** RFC 4648, done on the above date by the author (Larry Brasfield) +** who thereby has the right to put this into the public domain. +** +** The conversions meet RFC 4648 requirements, provided that this +** C source specifies that line-feeds are included in the encoded +** data to limit visible line lengths to 72 characters and to +** terminate any encoded blob having non-zero length. +** +** Length limitations are not imposed except that the runtime +** SQLite string or blob length limits are respected. Otherwise, +** any length binary sequence can be represented and recovered. +** Generated base64 sequences, with their line-feeds included, +** can be concatenated; the result converted back to binary will +** be the concatenation of the represented binary sequences. +** +** This SQLite3 extension creates a function, base64(x), which +** either: converts text x containing base64 to a returned blob; +** or converts a blob x to returned text containing base64. An +** error will be thrown for other input argument types. +** +** This code relies on UTF-8 encoding only with respect to the +** meaning of the first 128 (7-bit) codes matching that of USASCII. +** It will fail miserably if somehow made to try to convert EBCDIC. +** Because it is table-driven, it could be enhanced to handle that, +** but the world and SQLite have moved on from that anachronism. +** +** To build the extension: +** Set shell variable SQDIR= +** *Nix: gcc -O2 -shared -I$SQDIR -fPIC -o base64.so base64.c +** OSX: gcc -O2 -dynamiclib -fPIC -I$SQDIR -o base64.dylib base64.c +** Win32: gcc -O2 -shared -I%SQDIR% -o base64.dll base64.c +** Win32: cl /Os -I%SQDIR% base64.c -link -dll -out:base64.dll +*/ + +#include + +#include "sqlite3ext.h" + +#ifndef deliberate_fall_through +/* Quiet some compilers about some of our intentional code. */ +# if GCC_VERSION>=7000000 +# define deliberate_fall_through __attribute__((fallthrough)); +# else +# define deliberate_fall_through +# endif +#endif + +SQLITE_EXTENSION_INIT1; + +#define PC 0x80 /* pad character */ +#define WS 0x81 /* whitespace */ +#define ND 0x82 /* Not above or digit-value */ +#define PAD_CHAR '=' + +#ifndef U8_TYPEDEF +typedef unsigned char u8; +#define U8_TYPEDEF +#endif + +/* Decoding table, ASCII (7-bit) value to base 64 digit value or other */ +static const u8 b64DigitValues[128] = { + /* HT LF VT FF CR */ + ND,ND,ND,ND, ND,ND,ND,ND, ND,WS,WS,WS, WS,WS,ND,ND, + /* US */ + ND,ND,ND,ND, ND,ND,ND,ND, ND,ND,ND,ND, ND,ND,ND,ND, + /*sp + / */ + WS,ND,ND,ND, ND,ND,ND,ND, ND,ND,ND,62, ND,ND,ND,63, + /* 0 1 5 9 = */ + 52,53,54,55, 56,57,58,59, 60,61,ND,ND, ND,PC,ND,ND, + /* A O */ + ND, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14, + /* P Z */ + 15,16,17,18, 19,20,21,22, 23,24,25,ND, ND,ND,ND,ND, + /* a o */ + ND,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40, + /* p z */ + 41,42,43,44, 45,46,47,48, 49,50,51,ND, ND,ND,ND,ND +}; + +static const char b64Numerals[64+1] += "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + +#define BX_DV_PROTO(c) \ + ((((u8)(c))<0x80)? (u8)(b64DigitValues[(u8)(c)]) : 0x80) +#define IS_BX_DIGIT(bdp) (((u8)(bdp))<0x80) +#define IS_BX_WS(bdp) ((bdp)==WS) +#define IS_BX_PAD(bdp) ((bdp)==PC) +#define BX_NUMERAL(dv) (b64Numerals[(u8)(dv)]) +/* Width of base64 lines. Should be an integer multiple of 4. */ +#define B64_DARK_MAX 72 + +/* Encode a byte buffer into base64 text with linefeeds appended to limit +** encoded group lengths to B64_DARK_MAX or to terminate the last group. +*/ +static char* toBase64( u8 *pIn, int nbIn, char *pOut ){ + int nCol = 0; + while( nbIn >= 3 ){ + /* Do the bit-shuffle, exploiting unsigned input to avoid masking. */ + pOut[0] = BX_NUMERAL(pIn[0]>>2); + pOut[1] = BX_NUMERAL(((pIn[0]<<4)|(pIn[1]>>4))&0x3f); + pOut[2] = BX_NUMERAL(((pIn[1]&0xf)<<2)|(pIn[2]>>6)); + pOut[3] = BX_NUMERAL(pIn[2]&0x3f); + pOut += 4; + nbIn -= 3; + pIn += 3; + if( (nCol += 4)>=B64_DARK_MAX || nbIn<=0 ){ + *pOut++ = '\n'; + nCol = 0; + } + } + if( nbIn > 0 ){ + signed char nco = nbIn+1; + int nbe; + unsigned long qv = *pIn++; + for( nbe=1; nbe<3; ++nbe ){ + qv <<= 8; + if( nbe=0; --nbe ){ + char ce = (nbe>= 6; + pOut[nbe] = ce; + } + pOut += 4; + *pOut++ = '\n'; + } + *pOut = 0; + return pOut; +} + +/* Skip over text which is not base64 numeral(s). */ +static char * skipNonB64( char *s, int nc ){ + char c; + while( nc-- > 0 && (c = *s) && !IS_BX_DIGIT(BX_DV_PROTO(c)) ) ++s; + return s; +} + +/* Decode base64 text into a byte buffer. */ +static u8* fromBase64( char *pIn, int ncIn, u8 *pOut ){ + if( ncIn>0 && pIn[ncIn-1]=='\n' ) --ncIn; + while( ncIn>0 && *pIn!=PAD_CHAR ){ + static signed char nboi[] = { 0, 0, 1, 2, 3 }; + char *pUse = skipNonB64(pIn, ncIn); + unsigned long qv = 0L; + int nti, nbo, nac; + ncIn -= (pUse - pIn); + pIn = pUse; + nti = (ncIn>4)? 4 : ncIn; + ncIn -= nti; + nbo = nboi[nti]; + if( nbo==0 ) break; + for( nac=0; nac<4; ++nac ){ + char c = (nac>8) & 0xff; + deliberate_fall_through; /* FALLTHRU */ + case 1: + pOut[0] = (qv>>16) & 0xff; + break; + } + pOut += nbo; + } + return pOut; +} + +/* This function does the work for the SQLite base64(x) UDF. */ +static void base64(sqlite3_context *context, int na, sqlite3_value *av[]){ + sqlite3_int64 nb; + sqlite3_int64 nv = sqlite3_value_bytes(av[0]); + sqlite3_int64 nc; + int nvMax = sqlite3_limit(sqlite3_context_db_handle(context), + SQLITE_LIMIT_LENGTH, -1); + char *cBuf; + u8 *bBuf; + assert(na==1); + switch( sqlite3_value_type(av[0]) ){ + case SQLITE_BLOB: + nb = nv; + nc = 4*((nv+2)/3); /* quads needed */ + nc += (nc+(B64_DARK_MAX-1))/B64_DARK_MAX + 1; /* LFs and a 0-terminator */ + if( nvMax < nc ){ + sqlite3_result_error(context, "blob expanded to base64 too big", -1); + return; + } + bBuf = (u8*)sqlite3_value_blob(av[0]); + if( !bBuf ){ + if( SQLITE_NOMEM==sqlite3_errcode(sqlite3_context_db_handle(context)) ){ + goto memFail; + } + sqlite3_result_text(context,"",-1,SQLITE_STATIC); + break; + } + cBuf = sqlite3_malloc(nc); + if( !cBuf ) goto memFail; + nc = (int)(toBase64(bBuf, nb, cBuf) - cBuf); + sqlite3_result_text(context, cBuf, nc, sqlite3_free); + break; + case SQLITE_TEXT: + nc = nv; + nb = 3*((nv+3)/4); /* may overestimate due to LF and padding */ + if( nvMax < nb ){ + sqlite3_result_error(context, "blob from base64 may be too big", -1); + return; + }else if( nb<1 ){ + nb = 1; + } + cBuf = (char *)sqlite3_value_text(av[0]); + if( !cBuf ){ + if( SQLITE_NOMEM==sqlite3_errcode(sqlite3_context_db_handle(context)) ){ + goto memFail; + } + sqlite3_result_zeroblob(context, 0); + break; + } + bBuf = sqlite3_malloc(nb); + if( !bBuf ) goto memFail; + nb = (int)(fromBase64(cBuf, nc, bBuf) - bBuf); + sqlite3_result_blob(context, bBuf, nb, sqlite3_free); + break; + default: + sqlite3_result_error(context, "base64 accepts only blob or text", -1); + return; + } + return; + memFail: + sqlite3_result_error(context, "base64 OOM", -1); +} + +/* +** Establish linkage to running SQLite library. +*/ +#ifndef SQLITE_SHELL_EXTFUNCS +#ifdef _WIN32 +__declspec(dllexport) +#endif +int sqlite3_base_init +#else +static int sqlite3_base64_init +#endif +(sqlite3 *db, char **pzErr, const sqlite3_api_routines *pApi){ + SQLITE_EXTENSION_INIT2(pApi); + (void)pzErr; + return sqlite3_create_function + (db, "base64", 1, + SQLITE_DETERMINISTIC|SQLITE_INNOCUOUS|SQLITE_DIRECTONLY|SQLITE_UTF8, + 0, base64, 0, 0); +} + +/* +** Define some macros to allow this extension to be built into the shell +** conveniently, in conjunction with use of SQLITE_SHELL_EXTFUNCS. This +** allows shell.c, as distributed, to have this extension built in. +*/ +#define BASE64_INIT(db) sqlite3_base64_init(db, 0, 0) +#define BASE64_EXPOSE(db, pzErr) /* Not needed, ..._init() does this. */ diff --git a/ext/misc/base85.c b/ext/misc/base85.c new file mode 100644 index 0000000000..63245e2e4a --- /dev/null +++ b/ext/misc/base85.c @@ -0,0 +1,454 @@ +/* +** 2022-11-16 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** This is a utility for converting binary to base85 or vice-versa. +** It can be built as a standalone program or an SQLite3 extension. +** +** Much like base64 representations, base85 can be sent through a +** sane USASCII channel unmolested. It also plays nicely in CSV or +** written as TCL brace-enclosed literals or SQL string literals. +** It is not suited for unmodified use in XML-like documents. +** +** The encoding used resembles Ascii85, but was devised by the author +** (Larry Brasfield) before Mozilla, Adobe, ZMODEM or other Ascii85 +** variant sources existed, in the 1984 timeframe on a VAX mainframe. +** Further, this is an independent implementation of a base85 system. +** Hence, the author has rightfully put this into the public domain. +** +** Base85 numerals are taken from the set of 7-bit USASCII codes, +** excluding control characters and Space ! " ' ( ) { | } ~ Del +** in code order representing digit values 0 to 84 (base 10.) +** +** Groups of 4 bytes, interpreted as big-endian 32-bit values, +** are represented as 5-digit base85 numbers with MS to LS digit +** order. Groups of 1-3 bytes are represented with 2-4 digits, +** still big-endian but 8-24 bit values. (Using big-endian yields +** the simplest transition to byte groups smaller than 4 bytes. +** These byte groups can also be considered base-256 numbers.) +** Groups of 0 bytes are represented with 0 digits and vice-versa. +** No pad characters are used; Encoded base85 numeral sequence +** (aka "group") length maps 1-to-1 to the decoded binary length. +** +** Any character not in the base85 numeral set delimits groups. +** When base85 is streamed or stored in containers of indefinite +** size, newline is used to separate it into sub-sequences of no +** more than 80 digits so that fgets() can be used to read it. +** +** Length limitations are not imposed except that the runtime +** SQLite string or blob length limits are respected. Otherwise, +** any length binary sequence can be represented and recovered. +** Base85 sequences can be concatenated by separating them with +** a non-base85 character; the conversion to binary will then +** be the concatenation of the represented binary sequences. + +** The standalone program either converts base85 on stdin to create +** a binary file or converts a binary file to base85 on stdout. +** Read or make it blurt its help for invocation details. +** +** The SQLite3 extension creates a function, base85(x), which will +** either convert text base85 to a blob or a blob to text base85 +** and return the result (or throw an error for other types.) +** Unless built with OMIT_BASE85_CHECKER defined, it also creates a +** function, is_base85(t), which returns 1 iff the text t contains +** nothing other than base85 numerals and whitespace, or 0 otherwise. +** +** To build the extension: +** Set shell variable SQDIR= +** and variable OPTS to -DOMIT_BASE85_CHECKER if is_base85() unwanted. +** *Nix: gcc -O2 -shared -I$SQDIR $OPTS -fPIC -o base85.so base85.c +** OSX: gcc -O2 -dynamiclib -fPIC -I$SQDIR $OPTS -o base85.dylib base85.c +** Win32: gcc -O2 -shared -I%SQDIR% %OPTS% -o base85.dll base85.c +** Win32: cl /Os -I%SQDIR% %OPTS% base85.c -link -dll -out:base85.dll +** +** To build the standalone program, define PP symbol BASE85_STANDALONE. Eg. +** *Nix or OSX: gcc -O2 -DBASE85_STANDALONE base85.c -o base85 +** Win32: gcc -O2 -DBASE85_STANDALONE -o base85.exe base85.c +** Win32: cl /Os /MD -DBASE85_STANDALONE base85.c +*/ + +#include +#include +#include +#include +#ifndef OMIT_BASE85_CHECKER +# include +#endif + +#ifndef BASE85_STANDALONE + +# include "sqlite3ext.h" + +SQLITE_EXTENSION_INIT1; + +#else + +# ifdef _WIN32 +# include +# include +# else +# define setmode(fd,m) +# endif + +static char *zHelp = + "Usage: base85 \n" + " is either -r to read or -w to write ,\n" + " content to be converted to/from base85 on stdout/stdin.\n" + " names a binary file to be rendered or created.\n" + " Or, the name '-' refers to the stdin or stdout stream.\n" + ; + +static void sayHelp(){ + printf("%s", zHelp); +} +#endif + +#ifndef U8_TYPEDEF +typedef unsigned char u8; +#define U8_TYPEDEF +#endif + +/* Classify c according to interval within USASCII set w.r.t. base85 + * Values of 1 and 3 are base85 numerals. Values of 0, 2, or 4 are not. + */ +#define B85_CLASS( c ) (((c)>='#')+((c)>'&')+((c)>='*')+((c)>'z')) + +/* Provide digitValue to b85Numeral offset as a function of above class. */ +static u8 b85_cOffset[] = { 0, '#', 0, '*'-4, 0 }; +#define B85_DNOS( c ) b85_cOffset[B85_CLASS(c)] + +/* Say whether c is a base85 numeral. */ +#define IS_B85( c ) (B85_CLASS(c) & 1) + +#if 0 /* Not used, */ +static u8 base85DigitValue( char c ){ + u8 dv = (u8)(c - '#'); + if( dv>87 ) return 0xff; + return (dv > 3)? dv-3 : dv; +} +#endif + +/* Width of base64 lines. Should be an integer multiple of 5. */ +#define B85_DARK_MAX 80 + + +static char * skipNonB85( char *s, int nc ){ + char c; + while( nc-- > 0 && (c = *s) && !IS_B85(c) ) ++s; + return s; +} + +/* Convert small integer, known to be in 0..84 inclusive, to base85 numeral. + * Do not use the macro form with argument expression having a side-effect.*/ +#if 0 +static char base85Numeral( u8 b ){ + return (b < 4)? (char)(b + '#') : (char)(b - 4 + '*'); +} +#else +# define base85Numeral( dn )\ + ((char)(((dn) < 4)? (char)((dn) + '#') : (char)((dn) - 4 + '*'))) +#endif + +static char *putcs(char *pc, char *s){ + char c; + while( (c = *s++)!=0 ) *pc++ = c; + return pc; +} + +/* Encode a byte buffer into base85 text. If pSep!=0, it's a C string +** to be appended to encoded groups to limit their length to B85_DARK_MAX +** or to terminate the last group (to aid concatenation.) +*/ +static char* toBase85( u8 *pIn, int nbIn, char *pOut, char *pSep ){ + int nCol = 0; + while( nbIn >= 4 ){ + int nco = 5; + unsigned long qbv = (((unsigned long)pIn[0])<<24) | + (pIn[1]<<16) | (pIn[2]<<8) | pIn[3]; + while( nco > 0 ){ + unsigned nqv = (unsigned)(qbv/85UL); + unsigned char dv = qbv - 85UL*nqv; + qbv = nqv; + pOut[--nco] = base85Numeral(dv); + } + nbIn -= 4; + pIn += 4; + pOut += 5; + if( pSep && (nCol += 5)>=B85_DARK_MAX ){ + pOut = putcs(pOut, pSep); + nCol = 0; + } + } + if( nbIn > 0 ){ + int nco = nbIn + 1; + unsigned long qv = *pIn++; + int nbe = 1; + while( nbe++ < nbIn ){ + qv = (qv<<8) | *pIn++; + } + nCol += nco; + while( nco > 0 ){ + u8 dv = (u8)(qv % 85); + qv /= 85; + pOut[--nco] = base85Numeral(dv); + } + pOut += (nbIn+1); + } + if( pSep && nCol>0 ) pOut = putcs(pOut, pSep); + *pOut = 0; + return pOut; +} + +/* Decode base85 text into a byte buffer. */ +static u8* fromBase85( char *pIn, int ncIn, u8 *pOut ){ + if( ncIn>0 && pIn[ncIn-1]=='\n' ) --ncIn; + while( ncIn>0 ){ + static signed char nboi[] = { 0, 0, 1, 2, 3, 4 }; + char *pUse = skipNonB85(pIn, ncIn); + unsigned long qv = 0L; + int nti, nbo; + ncIn -= (pUse - pIn); + pIn = pUse; + nti = (ncIn>5)? 5 : ncIn; + nbo = nboi[nti]; + if( nbo==0 ) break; + while( nti>0 ){ + char c = *pIn++; + u8 cdo = B85_DNOS(c); + --ncIn; + if( cdo==0 ) break; + qv = 85 * qv + (c - cdo); + --nti; + } + nbo -= nti; /* Adjust for early (non-digit) end of group. */ + switch( nbo ){ + case 4: + *pOut++ = (qv >> 24)&0xff; + /* FALLTHRU */ + case 3: + *pOut++ = (qv >> 16)&0xff; + /* FALLTHRU */ + case 2: + *pOut++ = (qv >> 8)&0xff; + /* FALLTHRU */ + case 1: + *pOut++ = qv&0xff; + /* FALLTHRU */ + case 0: + break; + } + } + return pOut; +} + +#ifndef OMIT_BASE85_CHECKER +/* Say whether input char sequence is all (base85 and/or whitespace).*/ +static int allBase85( char *p, int len ){ + char c; + while( len-- > 0 && (c = *p++) != 0 ){ + if( !IS_B85(c) && !isspace(c) ) return 0; + } + return 1; +} +#endif + +#ifndef BASE85_STANDALONE + +# ifndef OMIT_BASE85_CHECKER +/* This function does the work for the SQLite is_base85(t) UDF. */ +static void is_base85(sqlite3_context *context, int na, sqlite3_value *av[]){ + assert(na==1); + switch( sqlite3_value_type(av[0]) ){ + case SQLITE_TEXT: + { + int rv = allBase85( (char *)sqlite3_value_text(av[0]), + sqlite3_value_bytes(av[0]) ); + sqlite3_result_int(context, rv); + } + break; + case SQLITE_NULL: + sqlite3_result_null(context); + break; + default: + sqlite3_result_error(context, "is_base85 accepts only text or NULL", -1); + return; + } +} +# endif + +/* This function does the work for the SQLite base85(x) UDF. */ +static void base85(sqlite3_context *context, int na, sqlite3_value *av[]){ + sqlite3_int64 nb, nc, nv = sqlite3_value_bytes(av[0]); + int nvMax = sqlite3_limit(sqlite3_context_db_handle(context), + SQLITE_LIMIT_LENGTH, -1); + char *cBuf; + u8 *bBuf; + assert(na==1); + switch( sqlite3_value_type(av[0]) ){ + case SQLITE_BLOB: + nb = nv; + /* ulongs tail newlines tailenc+nul*/ + nc = 5*(nv/4) + nv%4 + nv/64+1 + 2; + if( nvMax < nc ){ + sqlite3_result_error(context, "blob expanded to base85 too big", -1); + return; + } + bBuf = (u8*)sqlite3_value_blob(av[0]); + if( !bBuf ){ + if( SQLITE_NOMEM==sqlite3_errcode(sqlite3_context_db_handle(context)) ){ + goto memFail; + } + sqlite3_result_text(context,"",-1,SQLITE_STATIC); + break; + } + cBuf = sqlite3_malloc(nc); + if( !cBuf ) goto memFail; + nc = (int)(toBase85(bBuf, nb, cBuf, "\n") - cBuf); + sqlite3_result_text(context, cBuf, nc, sqlite3_free); + break; + case SQLITE_TEXT: + nc = nv; + nb = 4*(nv/5) + nv%5; /* may overestimate */ + if( nvMax < nb ){ + sqlite3_result_error(context, "blob from base85 may be too big", -1); + return; + }else if( nb<1 ){ + nb = 1; + } + cBuf = (char *)sqlite3_value_text(av[0]); + if( !cBuf ){ + if( SQLITE_NOMEM==sqlite3_errcode(sqlite3_context_db_handle(context)) ){ + goto memFail; + } + sqlite3_result_zeroblob(context, 0); + break; + } + bBuf = sqlite3_malloc(nb); + if( !bBuf ) goto memFail; + nb = (int)(fromBase85(cBuf, nc, bBuf) - bBuf); + sqlite3_result_blob(context, bBuf, nb, sqlite3_free); + break; + default: + sqlite3_result_error(context, "base85 accepts only blob or text.", -1); + return; + } + return; + memFail: + sqlite3_result_error(context, "base85 OOM", -1); +} + +/* +** Establish linkage to running SQLite library. +*/ +#ifndef SQLITE_SHELL_EXTFUNCS +#ifdef _WIN32 +__declspec(dllexport) +#endif +int sqlite3_base_init +#else +static int sqlite3_base85_init +#endif +(sqlite3 *db, char **pzErr, const sqlite3_api_routines *pApi){ + SQLITE_EXTENSION_INIT2(pApi); + (void)pzErr; +# ifndef OMIT_BASE85_CHECKER + { + int rc = sqlite3_create_function + (db, "is_base85", 1, + SQLITE_DETERMINISTIC|SQLITE_INNOCUOUS|SQLITE_UTF8, + 0, is_base85, 0, 0); + if( rc!=SQLITE_OK ) return rc; + } +# endif + return sqlite3_create_function + (db, "base85", 1, + SQLITE_DETERMINISTIC|SQLITE_INNOCUOUS|SQLITE_DIRECTONLY|SQLITE_UTF8, + 0, base85, 0, 0); +} + +/* +** Define some macros to allow this extension to be built into the shell +** conveniently, in conjunction with use of SQLITE_SHELL_EXTFUNCS. This +** allows shell.c, as distributed, to have this extension built in. +*/ +# define BASE85_INIT(db) sqlite3_base85_init(db, 0, 0) +# define BASE85_EXPOSE(db, pzErr) /* Not needed, ..._init() does this. */ + +#else /* standalone program */ + +int main(int na, char *av[]){ + int cin; + int rc = 0; + u8 bBuf[4*(B85_DARK_MAX/5)]; + char cBuf[5*(sizeof(bBuf)/4)+2]; + size_t nio; +# ifndef OMIT_BASE85_CHECKER + int b85Clean = 1; +# endif + char rw; + FILE *fb = 0, *foc = 0; + char fmode[3] = "xb"; + if( na < 3 || av[1][0]!='-' || (rw = av[1][1])==0 || (rw!='r' && rw!='w') ){ + sayHelp(); + return 0; + } + fmode[0] = rw; + if( av[2][0]=='-' && av[2][1]==0 ){ + switch( rw ){ + case 'r': + fb = stdin; + setmode(fileno(stdin), O_BINARY); + break; + case 'w': + fb = stdout; + setmode(fileno(stdout), O_BINARY); + break; + } + }else{ + fb = fopen(av[2], fmode); + foc = fb; + } + if( !fb ){ + fprintf(stderr, "Cannot open %s for %c\n", av[2], rw); + rc = 1; + }else{ + switch( rw ){ + case 'r': + while( (nio = fread( bBuf, 1, sizeof(bBuf), fb))>0 ){ + toBase85( bBuf, (int)nio, cBuf, 0 ); + fprintf(stdout, "%s\n", cBuf); + } + break; + case 'w': + while( 0 != fgets(cBuf, sizeof(cBuf), stdin) ){ + int nc = strlen(cBuf); + size_t nbo = fromBase85( cBuf, nc, bBuf ) - bBuf; + if( 1 != fwrite(bBuf, nbo, 1, fb) ) rc = 1; +# ifndef OMIT_BASE85_CHECKER + b85Clean &= allBase85( cBuf, nc ); +# endif + } + break; + default: + sayHelp(); + rc = 1; + } + if( foc ) fclose(foc); + } +# ifndef OMIT_BASE85_CHECKER + if( !b85Clean ){ + fprintf(stderr, "Base85 input had non-base85 dark or control content.\n"); + } +# endif + return rc; +} + +#endif diff --git a/ext/misc/basexx.c b/ext/misc/basexx.c new file mode 100644 index 0000000000..0dcde54356 --- /dev/null +++ b/ext/misc/basexx.c @@ -0,0 +1,89 @@ +/* +** 2022-11-20 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** This source allows multiple SQLite extensions to be either: combined +** into a single runtime-loadable library; or built into the SQLite shell +** using a preprocessing convention set by src/shell.c.in (and shell.c). +** +** Presently, it combines the base64.c and base85.c extensions. However, +** it can be used as a template for other combinations. +** +** Example usages: +** +** - Build a runtime-loadable extension from SQLite checkout directory: +** *Nix, OSX: gcc -O2 -shared -I. -fPIC -o basexx.so ext/misc/basexx.c +** Win32: cl /Os -I. ext/misc/basexx.c -link -dll -out:basexx.dll +** +** - Incorporate as built-in in sqlite3 shell: +** *Nix, OSX with gcc on a like platform: +** export mop1=-DSQLITE_SHELL_EXTSRC=ext/misc/basexx.c +** export mop2=-DSQLITE_SHELL_EXTFUNCS=BASEXX +** make sqlite3 "OPTS=$mop1 $mop2" +** Win32 with Microsoft toolset on Windows: +** set mop1=-DSQLITE_SHELL_EXTSRC=ext/misc/basexx.c +** set mop2=-DSQLITE_SHELL_EXTFUNCS=BASEXX +** set mops="OPTS=%mop1% %mop2%" +** nmake -f Makefile.msc sqlite3.exe %mops% +*/ + +#ifndef SQLITE_SHELL_EXTFUNCS /* Guard for #include as built-in extension. */ +# include "sqlite3ext.h" +SQLITE_EXTENSION_INIT1; +#endif + +static void init_api_ptr(const sqlite3_api_routines *pApi){ + SQLITE_EXTENSION_INIT2(pApi); +} + +#undef SQLITE_EXTENSION_INIT1 +#define SQLITE_EXTENSION_INIT1 /* */ +#undef SQLITE_EXTENSION_INIT2 +#define SQLITE_EXTENSION_INIT2(v) (void)v + +typedef unsigned char u8; +#define U8_TYPEDEF + +/* These next 2 undef's are only needed because the entry point names + * collide when formulated per the rules stated for loadable extension + * entry point names that will be deduced from the file basenames. + */ +#undef sqlite3_base_init +#define sqlite3_base_init sqlite3_base64_init +#include "base64.c" + +#undef sqlite3_base_init +#define sqlite3_base_init sqlite3_base85_init +#include "base85.c" + +#ifdef _WIN32 +__declspec(dllexport) +#endif +int sqlite3_basexx_init(sqlite3 *db, char **pzErr, + const sqlite3_api_routines *pApi){ + int rc1; + int rc2; + + init_api_ptr(pApi); + rc1 = BASE64_INIT(db); + rc2 = BASE85_INIT(db); + + if( rc1==SQLITE_OK && rc2==SQLITE_OK ){ + BASE64_EXPOSE(db, pzErr); + BASE64_EXPOSE(db, pzErr); + return SQLITE_OK; + }else{ + return SQLITE_ERROR; + } +} + +# define BASEXX_INIT(db) sqlite3_basexx_init(db, 0, 0) +# define BASEXX_EXPOSE(db, pzErr) /* Not needed, ..._init() does this. */ diff --git a/ext/misc/blobio.c b/ext/misc/blobio.c new file mode 100644 index 0000000000..3a1ee8465c --- /dev/null +++ b/ext/misc/blobio.c @@ -0,0 +1,152 @@ +/* +** 2019-03-30 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +** An SQL function that uses the incremental BLOB I/O mechanism of SQLite +** to read or write part of a blob. This is intended for debugging use +** in the CLI. +** +** readblob(SCHEMA,TABLE,COLUMN,ROWID,OFFSET,N) +** +** Returns N bytes of the blob starting at OFFSET. +** +** writeblob(SCHEMA,TABLE,COLUMN,ROWID,OFFSET,NEWDATA) +** +** NEWDATA must be a blob. The content of NEWDATA overwrites the +** existing BLOB data at SCHEMA.TABLE.COLUMN for row ROWID beginning +** at OFFSET bytes into the blob. +*/ +#include "sqlite3ext.h" +SQLITE_EXTENSION_INIT1 +#include +#include + +static void readblobFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + sqlite3_blob *pBlob = 0; + const char *zSchema; + const char *zTable; + const char *zColumn; + sqlite3_int64 iRowid; + int iOfst; + unsigned char *aData; + int nData; + sqlite3 *db; + int rc; + + zSchema = (const char*)sqlite3_value_text(argv[0]); + zTable = (const char*)sqlite3_value_text(argv[1]); + if( zTable==0 ){ + sqlite3_result_error(context, "bad table name", -1); + return; + } + zColumn = (const char*)sqlite3_value_text(argv[2]); + if( zTable==0 ){ + sqlite3_result_error(context, "bad column name", -1); + return; + } + iRowid = sqlite3_value_int64(argv[3]); + iOfst = sqlite3_value_int(argv[4]); + nData = sqlite3_value_int(argv[5]); + if( nData<=0 ) return; + aData = sqlite3_malloc64( nData+1 ); + if( aData==0 ){ + sqlite3_result_error_nomem(context); + return; + } + db = sqlite3_context_db_handle(context); + rc = sqlite3_blob_open(db, zSchema, zTable, zColumn, iRowid, 0, &pBlob); + if( rc ){ + sqlite3_free(aData); + sqlite3_result_error(context, "cannot open BLOB pointer", -1); + return; + } + rc = sqlite3_blob_read(pBlob, aData, nData, iOfst); + sqlite3_blob_close(pBlob); + if( rc ){ + sqlite3_free(aData); + sqlite3_result_error(context, "BLOB read failed", -1); + }else{ + sqlite3_result_blob(context, aData, nData, sqlite3_free); + } +} + +static void writeblobFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + sqlite3_blob *pBlob = 0; + const char *zSchema; + const char *zTable; + const char *zColumn; + sqlite3_int64 iRowid; + int iOfst; + unsigned char *aData; + int nData; + sqlite3 *db; + int rc; + + zSchema = (const char*)sqlite3_value_text(argv[0]); + zTable = (const char*)sqlite3_value_text(argv[1]); + if( zTable==0 ){ + sqlite3_result_error(context, "bad table name", -1); + return; + } + zColumn = (const char*)sqlite3_value_text(argv[2]); + if( zTable==0 ){ + sqlite3_result_error(context, "bad column name", -1); + return; + } + iRowid = sqlite3_value_int64(argv[3]); + iOfst = sqlite3_value_int(argv[4]); + if( sqlite3_value_type(argv[5])!=SQLITE_BLOB ){ + sqlite3_result_error(context, "6th argument must be a BLOB", -1); + return; + } + nData = sqlite3_value_bytes(argv[5]); + aData = (unsigned char *)sqlite3_value_blob(argv[5]); + db = sqlite3_context_db_handle(context); + rc = sqlite3_blob_open(db, zSchema, zTable, zColumn, iRowid, 1, &pBlob); + if( rc ){ + sqlite3_result_error(context, "cannot open BLOB pointer", -1); + return; + } + rc = sqlite3_blob_write(pBlob, aData, nData, iOfst); + sqlite3_blob_close(pBlob); + if( rc ){ + sqlite3_result_error(context, "BLOB write failed", -1); + } +} + + +#ifdef _WIN32 +__declspec(dllexport) +#endif +int sqlite3_blobio_init( + sqlite3 *db, + char **pzErrMsg, + const sqlite3_api_routines *pApi +){ + int rc = SQLITE_OK; + SQLITE_EXTENSION_INIT2(pApi); + (void)pzErrMsg; /* Unused parameter */ + rc = sqlite3_create_function(db, "readblob", 6, SQLITE_UTF8, 0, + readblobFunc, 0, 0); + if( rc==SQLITE_OK ){ + rc = sqlite3_create_function(db, "writeblob", 6, SQLITE_UTF8, 0, + writeblobFunc, 0, 0); + } + return rc; +} diff --git a/ext/misc/btreeinfo.c b/ext/misc/btreeinfo.c new file mode 100644 index 0000000000..9c726f5f17 --- /dev/null +++ b/ext/misc/btreeinfo.c @@ -0,0 +1,430 @@ +/* +** 2017-10-24 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +** This file contains an implementation of the "sqlite_btreeinfo" virtual table. +** +** The sqlite_btreeinfo virtual table is a read-only eponymous-only virtual +** table that shows information about all btrees in an SQLite database file. +** The schema is like this: +** +** CREATE TABLE sqlite_btreeinfo( +** type TEXT, -- "table" or "index" +** name TEXT, -- Name of table or index for this btree. +** tbl_name TEXT, -- Associated table +** rootpage INT, -- The root page of the btree +** sql TEXT, -- SQL for this btree - from sqlite_schema +** hasRowid BOOLEAN, -- True if the btree has a rowid +** nEntry INT, -- Estimated number of entries +** nPage INT, -- Estimated number of pages +** depth INT, -- Depth of the btree +** szPage INT, -- Size of each page in bytes +** zSchema TEXT HIDDEN -- The schema to which this btree belongs +** ); +** +** The first 5 fields are taken directly from the sqlite_schema table. +** Considering only the first 5 fields, the only difference between +** this virtual table and the sqlite_schema table is that this virtual +** table omits all entries that have a 0 or NULL rowid - in other words +** it omits triggers and views. +** +** The value added by this table comes in the next 5 fields. +** +** Note that nEntry and nPage are *estimated*. They are computed doing +** a single search from the root to a leaf, counting the number of cells +** at each level, and assuming that unvisited pages have a similar number +** of cells. +** +** The sqlite_dbpage virtual table must be available for this virtual table +** to operate. +** +** USAGE EXAMPLES: +** +** Show the table btrees in a schema order with the tables with the most +** rows occurring first: +** +** SELECT name, nEntry +** FROM sqlite_btreeinfo +** WHERE type='table' +** ORDER BY nEntry DESC, name; +** +** Show the names of all WITHOUT ROWID tables: +** +** SELECT name FROM sqlite_btreeinfo +** WHERE type='table' AND NOT hasRowid; +*/ +#if !defined(SQLITEINT_H) +#include "sqlite3ext.h" +#endif +SQLITE_EXTENSION_INIT1 +#include +#include + +/* Columns available in this virtual table */ +#define BINFO_COLUMN_TYPE 0 +#define BINFO_COLUMN_NAME 1 +#define BINFO_COLUMN_TBL_NAME 2 +#define BINFO_COLUMN_ROOTPAGE 3 +#define BINFO_COLUMN_SQL 4 +#define BINFO_COLUMN_HASROWID 5 +#define BINFO_COLUMN_NENTRY 6 +#define BINFO_COLUMN_NPAGE 7 +#define BINFO_COLUMN_DEPTH 8 +#define BINFO_COLUMN_SZPAGE 9 +#define BINFO_COLUMN_SCHEMA 10 + +/* Forward declarations */ +typedef struct BinfoTable BinfoTable; +typedef struct BinfoCursor BinfoCursor; + +/* A cursor for the sqlite_btreeinfo table */ +struct BinfoCursor { + sqlite3_vtab_cursor base; /* Base class. Must be first */ + sqlite3_stmt *pStmt; /* Query against sqlite_schema */ + int rc; /* Result of previous sqlite_step() call */ + int hasRowid; /* hasRowid value. Negative if unknown. */ + sqlite3_int64 nEntry; /* nEntry value */ + int nPage; /* nPage value */ + int depth; /* depth value */ + int szPage; /* size of a btree page. 0 if unknown */ + char *zSchema; /* Schema being interrogated */ +}; + +/* The sqlite_btreeinfo table */ +struct BinfoTable { + sqlite3_vtab base; /* Base class. Must be first */ + sqlite3 *db; /* The databse connection */ +}; + +/* +** Connect to the sqlite_btreeinfo virtual table. +*/ +static int binfoConnect( + sqlite3 *db, + void *pAux, + int argc, const char *const*argv, + sqlite3_vtab **ppVtab, + char **pzErr +){ + BinfoTable *pTab = 0; + int rc = SQLITE_OK; + rc = sqlite3_declare_vtab(db, + "CREATE TABLE x(\n" + " type TEXT,\n" + " name TEXT,\n" + " tbl_name TEXT,\n" + " rootpage INT,\n" + " sql TEXT,\n" + " hasRowid BOOLEAN,\n" + " nEntry INT,\n" + " nPage INT,\n" + " depth INT,\n" + " szPage INT,\n" + " zSchema TEXT HIDDEN\n" + ")"); + if( rc==SQLITE_OK ){ + pTab = (BinfoTable *)sqlite3_malloc64(sizeof(BinfoTable)); + if( pTab==0 ) rc = SQLITE_NOMEM; + } + assert( rc==SQLITE_OK || pTab==0 ); + if( pTab ){ + pTab->db = db; + } + *ppVtab = (sqlite3_vtab*)pTab; + return rc; +} + +/* +** Disconnect from or destroy a btreeinfo virtual table. +*/ +static int binfoDisconnect(sqlite3_vtab *pVtab){ + sqlite3_free(pVtab); + return SQLITE_OK; +} + +/* +** idxNum: +** +** 0 Use "main" for the schema +** 1 Schema identified by parameter ?1 +*/ +static int binfoBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){ + int i; + pIdxInfo->estimatedCost = 10000.0; /* Cost estimate */ + pIdxInfo->estimatedRows = 100; + for(i=0; inConstraint; i++){ + struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[i]; + if( p->usable + && p->iColumn==BINFO_COLUMN_SCHEMA + && p->op==SQLITE_INDEX_CONSTRAINT_EQ + ){ + pIdxInfo->estimatedCost = 1000.0; + pIdxInfo->idxNum = 1; + pIdxInfo->aConstraintUsage[i].argvIndex = 1; + pIdxInfo->aConstraintUsage[i].omit = 1; + break; + } + } + return SQLITE_OK; +} + +/* +** Open a new btreeinfo cursor. +*/ +static int binfoOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){ + BinfoCursor *pCsr; + + pCsr = (BinfoCursor *)sqlite3_malloc64(sizeof(BinfoCursor)); + if( pCsr==0 ){ + return SQLITE_NOMEM; + }else{ + memset(pCsr, 0, sizeof(BinfoCursor)); + pCsr->base.pVtab = pVTab; + } + + *ppCursor = (sqlite3_vtab_cursor *)pCsr; + return SQLITE_OK; +} + +/* +** Close a btreeinfo cursor. +*/ +static int binfoClose(sqlite3_vtab_cursor *pCursor){ + BinfoCursor *pCsr = (BinfoCursor *)pCursor; + sqlite3_finalize(pCsr->pStmt); + sqlite3_free(pCsr->zSchema); + sqlite3_free(pCsr); + return SQLITE_OK; +} + +/* +** Move a btreeinfo cursor to the next entry in the file. +*/ +static int binfoNext(sqlite3_vtab_cursor *pCursor){ + BinfoCursor *pCsr = (BinfoCursor *)pCursor; + pCsr->rc = sqlite3_step(pCsr->pStmt); + pCsr->hasRowid = -1; + return pCsr->rc==SQLITE_ERROR ? SQLITE_ERROR : SQLITE_OK; +} + +/* We have reached EOF if previous sqlite3_step() returned +** anything other than SQLITE_ROW; +*/ +static int binfoEof(sqlite3_vtab_cursor *pCursor){ + BinfoCursor *pCsr = (BinfoCursor *)pCursor; + return pCsr->rc!=SQLITE_ROW; +} + +/* Position a cursor back to the beginning. +*/ +static int binfoFilter( + sqlite3_vtab_cursor *pCursor, + int idxNum, const char *idxStr, + int argc, sqlite3_value **argv +){ + BinfoCursor *pCsr = (BinfoCursor *)pCursor; + BinfoTable *pTab = (BinfoTable *)pCursor->pVtab; + char *zSql; + int rc; + + sqlite3_free(pCsr->zSchema); + if( idxNum==1 && sqlite3_value_type(argv[0])!=SQLITE_NULL ){ + pCsr->zSchema = sqlite3_mprintf("%s", sqlite3_value_text(argv[0])); + }else{ + pCsr->zSchema = sqlite3_mprintf("main"); + } + zSql = sqlite3_mprintf( + "SELECT 0, 'table','sqlite_schema','sqlite_schema',1,NULL " + "UNION ALL " + "SELECT rowid, type, name, tbl_name, rootpage, sql" + " FROM \"%w\".sqlite_schema WHERE rootpage>=1", + pCsr->zSchema); + sqlite3_finalize(pCsr->pStmt); + pCsr->pStmt = 0; + pCsr->hasRowid = -1; + rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pStmt, 0); + sqlite3_free(zSql); + if( rc==SQLITE_OK ){ + rc = binfoNext(pCursor); + } + return rc; +} + +/* Decode big-endian integers */ +static unsigned int get_uint16(unsigned char *a){ + return (a[0]<<8)|a[1]; +} +static unsigned int get_uint32(unsigned char *a){ + return (a[0]<<24)|(a[1]<<16)|(a[2]<<8)|a[3]; +} + +/* Examine the b-tree rooted at pgno and estimate its size. +** Return non-zero if anything goes wrong. +*/ +static int binfoCompute(sqlite3 *db, int pgno, BinfoCursor *pCsr){ + sqlite3_int64 nEntry = 1; + int nPage = 1; + unsigned char *aData; + sqlite3_stmt *pStmt = 0; + int rc = SQLITE_OK; + int pgsz = 0; + int nCell; + int iCell; + + rc = sqlite3_prepare_v2(db, + "SELECT data FROM sqlite_dbpage('main') WHERE pgno=?1", -1, + &pStmt, 0); + if( rc ) return rc; + pCsr->depth = 1; + while(1){ + sqlite3_bind_int(pStmt, 1, pgno); + rc = sqlite3_step(pStmt); + if( rc!=SQLITE_ROW ){ + rc = SQLITE_ERROR; + break; + } + pCsr->szPage = pgsz = sqlite3_column_bytes(pStmt, 0); + aData = (unsigned char*)sqlite3_column_blob(pStmt, 0); + if( aData==0 ){ + rc = SQLITE_NOMEM; + break; + } + if( pgno==1 ){ + aData += 100; + pgsz -= 100; + } + pCsr->hasRowid = aData[0]!=2 && aData[0]!=10; + nCell = get_uint16(aData+3); + nEntry *= (nCell+1); + if( aData[0]==10 || aData[0]==13 ) break; + nPage *= (nCell+1); + if( nCell<=1 ){ + pgno = get_uint32(aData+8); + }else{ + iCell = get_uint16(aData+12+2*(nCell/2)); + if( pgno==1 ) iCell -= 100; + if( iCell<=12 || iCell>=pgsz-4 ){ + rc = SQLITE_CORRUPT; + break; + } + pgno = get_uint32(aData+iCell); + } + pCsr->depth++; + sqlite3_reset(pStmt); + } + sqlite3_finalize(pStmt); + pCsr->nPage = nPage; + pCsr->nEntry = nEntry; + if( rc==SQLITE_ROW ) rc = SQLITE_OK; + return rc; +} + +/* Return a column for the sqlite_btreeinfo table */ +static int binfoColumn( + sqlite3_vtab_cursor *pCursor, + sqlite3_context *ctx, + int i +){ + BinfoCursor *pCsr = (BinfoCursor *)pCursor; + if( i>=BINFO_COLUMN_HASROWID && i<=BINFO_COLUMN_SZPAGE && pCsr->hasRowid<0 ){ + int pgno = sqlite3_column_int(pCsr->pStmt, BINFO_COLUMN_ROOTPAGE+1); + sqlite3 *db = sqlite3_context_db_handle(ctx); + int rc = binfoCompute(db, pgno, pCsr); + if( rc ){ + pCursor->pVtab->zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(db)); + return SQLITE_ERROR; + } + } + switch( i ){ + case BINFO_COLUMN_NAME: + case BINFO_COLUMN_TYPE: + case BINFO_COLUMN_TBL_NAME: + case BINFO_COLUMN_ROOTPAGE: + case BINFO_COLUMN_SQL: { + sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pStmt, i+1)); + break; + } + case BINFO_COLUMN_HASROWID: { + sqlite3_result_int(ctx, pCsr->hasRowid); + break; + } + case BINFO_COLUMN_NENTRY: { + sqlite3_result_int64(ctx, pCsr->nEntry); + break; + } + case BINFO_COLUMN_NPAGE: { + sqlite3_result_int(ctx, pCsr->nPage); + break; + } + case BINFO_COLUMN_DEPTH: { + sqlite3_result_int(ctx, pCsr->depth); + break; + } + case BINFO_COLUMN_SCHEMA: { + sqlite3_result_text(ctx, pCsr->zSchema, -1, SQLITE_STATIC); + break; + } + } + return SQLITE_OK; +} + +/* Return the ROWID for the sqlite_btreeinfo table */ +static int binfoRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){ + BinfoCursor *pCsr = (BinfoCursor *)pCursor; + *pRowid = sqlite3_column_int64(pCsr->pStmt, 0); + return SQLITE_OK; +} + +/* +** Invoke this routine to register the "sqlite_btreeinfo" virtual table module +*/ +int sqlite3BinfoRegister(sqlite3 *db){ + static sqlite3_module binfo_module = { + 0, /* iVersion */ + 0, /* xCreate */ + binfoConnect, /* xConnect */ + binfoBestIndex, /* xBestIndex */ + binfoDisconnect, /* xDisconnect */ + 0, /* xDestroy */ + binfoOpen, /* xOpen - open a cursor */ + binfoClose, /* xClose - close a cursor */ + binfoFilter, /* xFilter - configure scan constraints */ + binfoNext, /* xNext - advance a cursor */ + binfoEof, /* xEof - check for end of scan */ + binfoColumn, /* xColumn - read data */ + binfoRowid, /* xRowid - read data */ + 0, /* xUpdate */ + 0, /* xBegin */ + 0, /* xSync */ + 0, /* xCommit */ + 0, /* xRollback */ + 0, /* xFindMethod */ + 0, /* xRename */ + 0, /* xSavepoint */ + 0, /* xRelease */ + 0, /* xRollbackTo */ + 0, /* xShadowName */ + 0 /* xIntegrity */ + }; + return sqlite3_create_module(db, "sqlite_btreeinfo", &binfo_module, 0); +} + +#ifdef _WIN32 +__declspec(dllexport) +#endif +int sqlite3_btreeinfo_init( + sqlite3 *db, + char **pzErrMsg, + const sqlite3_api_routines *pApi +){ + SQLITE_EXTENSION_INIT2(pApi); + return sqlite3BinfoRegister(db); +} diff --git a/ext/misc/cksumvfs.c b/ext/misc/cksumvfs.c new file mode 100644 index 0000000000..6491e85cde --- /dev/null +++ b/ext/misc/cksumvfs.c @@ -0,0 +1,847 @@ +/* +** 2020-04-20 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +** This file implements a VFS shim that writes a checksum on each page +** of an SQLite database file. When reading pages, the checksum is verified +** and an error is raised if the checksum is incorrect. +** +** COMPILING +** +** This extension requires SQLite 3.32.0 or later. It uses the +** sqlite3_database_file_object() interface which was added in +** version 3.32.0, so it will not link with an earlier version of +** SQLite. +** +** To build this extension as a separately loaded shared library or +** DLL, use compiler command-lines similar to the following: +** +** (linux) gcc -fPIC -shared cksumvfs.c -o cksumvfs.so +** (mac) clang -fPIC -dynamiclib cksumvfs.c -o cksumvfs.dylib +** (windows) cl cksumvfs.c -link -dll -out:cksumvfs.dll +** +** You may want to add additional compiler options, of course, +** according to the needs of your project. +** +** If you want to statically link this extension with your product, +** then compile it like any other C-language module but add the +** "-DSQLITE_CKSUMVFS_STATIC" option so that this module knows that +** it is being statically linked rather than dynamically linked +** +** LOADING +** +** To load this extension as a shared library, you first have to +** bring up a dummy SQLite database connection to use as the argument +** to the sqlite3_load_extension() API call. Then you invoke the +** sqlite3_load_extension() API and shutdown the dummy database +** connection. All subsequent database connections that are opened +** will include this extension. For example: +** +** sqlite3 *db; +** sqlite3_open(":memory:", &db); +** sqlite3_load_extension(db, "./cksumvfs"); +** sqlite3_close(db); +** +** If this extension is compiled with -DSQLITE_CKSUMVFS_STATIC and +** statically linked against the application, initialize it using +** a single API call as follows: +** +** sqlite3_register_cksumvfs(); +** +** Cksumvfs is a VFS Shim. When loaded, "cksmvfs" becomes the new +** default VFS and it uses the prior default VFS as the next VFS +** down in the stack. This is normally what you want. However, in +** complex situations where multiple VFS shims are being loaded, +** it might be important to ensure that cksumvfs is loaded in the +** correct order so that it sequences itself into the default VFS +** Shim stack in the right order. +** +** USING +** +** Open database connections using the sqlite3_open() or +** sqlite3_open_v2() interfaces, as normal. Ordinary database files +** (without a checksum) will operate normally. Databases with +** checksums will return an SQLITE_IOERR_DATA error if a page is +** encountered that contains an invalid checksum. +** +** Checksumming only works on databases that have a reserve-bytes +** value of exactly 8. The default value for reserve-bytes is 0. +** Hence, newly created database files will omit the checksum by +** default. To create a database that includes a checksum, change +** the reserve-bytes value to 8 by runing: +** +** int n = 8; +** sqlite3_file_control(db, 0, SQLITE_FCNTL_RESERVE_BYTES, &n); +** +** If you do this immediately after creating a new database file, +** before anything else has been written into the file, then that +** might be all that you need to do. Otherwise, the API call +** above should be followed by: +** +** sqlite3_exec(db, "VACUUM", 0, 0, 0); +** +** It never hurts to run the VACUUM, even if you don't need it. +** If the database is in WAL mode, you should shutdown and +** reopen all database connections before continuing. +** +** From the CLI, use the ".filectrl reserve_bytes 8" command, +** followed by "VACUUM;". +** +** Note that SQLite allows the number of reserve-bytes to be +** increased but not decreased. So if a database file already +** has a reserve-bytes value greater than 8, there is no way to +** activate checksumming on that database, other than to dump +** and restore the database file. Note also that other extensions +** might also make use of the reserve-bytes. Checksumming will +** be incompatible with those other extensions. +** +** VERIFICATION OF CHECKSUMS +** +** If any checksum is incorrect, the "PRAGMA quick_check" command +** will find it. To verify that checksums are actually enabled +** and running, use the following query: +** +** SELECT count(*), verify_checksum(data) +** FROM sqlite_dbpage +** GROUP BY 2; +** +** There are three possible outputs form the verify_checksum() +** function: 1, 0, and NULL. 1 is returned if the checksum is +** correct. 0 is returned if the checksum is incorrect. NULL +** is returned if the page is unreadable. If checksumming is +** enabled, the read will fail if the checksum is wrong, so the +** usual result from verify_checksum() on a bad checksum is NULL. +** +** If everything is OK, the query above should return a single +** row where the second column is 1. Any other result indicates +** either that there is a checksum error, or checksum validation +** is disabled. +** +** CONTROLLING CHECKSUM VERIFICATION +** +** The cksumvfs extension implements a new PRAGMA statement that can +** be used to disable, re-enable, or query the status of checksum +** verification: +** +** PRAGMA checksum_verification; -- query status +** PRAGMA checksum_verification=OFF; -- disable verification +** PRAGMA checksum_verification=ON; -- re-enable verification +** +** The "checksum_verification" pragma will return "1" (true) or "0" +** (false) if checksum verification is enabled or disabled, respectively. +** "Verification" in this context means the feature that causes +** SQLITE_IOERR_DATA errors if a checksum mismatch is detected while +** reading. Checksums are always kept up-to-date as long as the +** reserve-bytes value of the database is 8, regardless of the setting +** of this pragma. Checksum verification can be disabled (for example) +** to do forensic analysis of a database that has previously reported +** a checksum error. +** +** The "checksum_verification" pragma will always respond with "0" if +** the database file does not have a reserve-bytes value of 8. The +** pragma will return no rows at all if the cksumvfs extension is +** not loaded. +** +** IMPLEMENTATION NOTES +** +** The checksum is stored in the last 8 bytes of each page. This +** module only operates if the "bytes of reserved space on each page" +** value at offset 20 the SQLite database header is exactly 8. If +** the reserved-space value is not 8, this module is a no-op. +*/ +#if defined(SQLITE_AMALGAMATION) && !defined(SQLITE_CKSUMVFS_STATIC) +# define SQLITE_CKSUMVFS_STATIC +#endif +#ifdef SQLITE_CKSUMVFS_STATIC +# include "sqlite3.h" +#else +# include "sqlite3ext.h" + SQLITE_EXTENSION_INIT1 +#endif +#include +#include + + +/* +** Forward declaration of objects used by this utility +*/ +typedef struct sqlite3_vfs CksmVfs; +typedef struct CksmFile CksmFile; + +/* +** Useful datatype abbreviations +*/ +#if !defined(SQLITE_AMALGAMATION) + typedef unsigned char u8; + typedef unsigned int u32; +#endif + +/* Access to a lower-level VFS that (might) implement dynamic loading, +** access to randomness, etc. +*/ +#define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData)) +#define ORIGFILE(p) ((sqlite3_file*)(((CksmFile*)(p))+1)) + +/* An open file */ +struct CksmFile { + sqlite3_file base; /* IO methods */ + const char *zFName; /* Original name of the file */ + char computeCksm; /* True to compute checksums. + ** Always true if reserve size is 8. */ + char verifyCksm; /* True to verify checksums */ + CksmFile *pPartner; /* Ptr from WAL to main-db, or from main-db to WAL */ +}; + +/* +** Methods for CksmFile +*/ +static int cksmClose(sqlite3_file*); +static int cksmRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst); +static int cksmWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst); +static int cksmTruncate(sqlite3_file*, sqlite3_int64 size); +static int cksmSync(sqlite3_file*, int flags); +static int cksmFileSize(sqlite3_file*, sqlite3_int64 *pSize); +static int cksmLock(sqlite3_file*, int); +static int cksmUnlock(sqlite3_file*, int); +static int cksmCheckReservedLock(sqlite3_file*, int *pResOut); +static int cksmFileControl(sqlite3_file*, int op, void *pArg); +static int cksmSectorSize(sqlite3_file*); +static int cksmDeviceCharacteristics(sqlite3_file*); +static int cksmShmMap(sqlite3_file*, int iPg, int pgsz, int, void volatile**); +static int cksmShmLock(sqlite3_file*, int offset, int n, int flags); +static void cksmShmBarrier(sqlite3_file*); +static int cksmShmUnmap(sqlite3_file*, int deleteFlag); +static int cksmFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp); +static int cksmUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p); + +/* +** Methods for CksmVfs +*/ +static int cksmOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *); +static int cksmDelete(sqlite3_vfs*, const char *zName, int syncDir); +static int cksmAccess(sqlite3_vfs*, const char *zName, int flags, int *); +static int cksmFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut); +static void *cksmDlOpen(sqlite3_vfs*, const char *zFilename); +static void cksmDlError(sqlite3_vfs*, int nByte, char *zErrMsg); +static void (*cksmDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void); +static void cksmDlClose(sqlite3_vfs*, void*); +static int cksmRandomness(sqlite3_vfs*, int nByte, char *zOut); +static int cksmSleep(sqlite3_vfs*, int microseconds); +static int cksmCurrentTime(sqlite3_vfs*, double*); +static int cksmGetLastError(sqlite3_vfs*, int, char *); +static int cksmCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*); +static int cksmSetSystemCall(sqlite3_vfs*, const char*,sqlite3_syscall_ptr); +static sqlite3_syscall_ptr cksmGetSystemCall(sqlite3_vfs*, const char *z); +static const char *cksmNextSystemCall(sqlite3_vfs*, const char *zName); + +static sqlite3_vfs cksm_vfs = { + 3, /* iVersion (set when registered) */ + 0, /* szOsFile (set when registered) */ + 1024, /* mxPathname */ + 0, /* pNext */ + "cksmvfs", /* zName */ + 0, /* pAppData (set when registered) */ + cksmOpen, /* xOpen */ + cksmDelete, /* xDelete */ + cksmAccess, /* xAccess */ + cksmFullPathname, /* xFullPathname */ + cksmDlOpen, /* xDlOpen */ + cksmDlError, /* xDlError */ + cksmDlSym, /* xDlSym */ + cksmDlClose, /* xDlClose */ + cksmRandomness, /* xRandomness */ + cksmSleep, /* xSleep */ + cksmCurrentTime, /* xCurrentTime */ + cksmGetLastError, /* xGetLastError */ + cksmCurrentTimeInt64, /* xCurrentTimeInt64 */ + cksmSetSystemCall, /* xSetSystemCall */ + cksmGetSystemCall, /* xGetSystemCall */ + cksmNextSystemCall /* xNextSystemCall */ +}; + +static const sqlite3_io_methods cksm_io_methods = { + 3, /* iVersion */ + cksmClose, /* xClose */ + cksmRead, /* xRead */ + cksmWrite, /* xWrite */ + cksmTruncate, /* xTruncate */ + cksmSync, /* xSync */ + cksmFileSize, /* xFileSize */ + cksmLock, /* xLock */ + cksmUnlock, /* xUnlock */ + cksmCheckReservedLock, /* xCheckReservedLock */ + cksmFileControl, /* xFileControl */ + cksmSectorSize, /* xSectorSize */ + cksmDeviceCharacteristics, /* xDeviceCharacteristics */ + cksmShmMap, /* xShmMap */ + cksmShmLock, /* xShmLock */ + cksmShmBarrier, /* xShmBarrier */ + cksmShmUnmap, /* xShmUnmap */ + cksmFetch, /* xFetch */ + cksmUnfetch /* xUnfetch */ +}; + +/* Do byte swapping on a unsigned 32-bit integer */ +#define BYTESWAP32(x) ( \ + (((x)&0x000000FF)<<24) + (((x)&0x0000FF00)<<8) \ + + (((x)&0x00FF0000)>>8) + (((x)&0xFF000000)>>24) \ +) + +/* Compute a checksum on a buffer */ +static void cksmCompute( + u8 *a, /* Content to be checksummed */ + int nByte, /* Bytes of content in a[]. Must be a multiple of 8. */ + u8 *aOut /* OUT: Final 8-byte checksum value output */ +){ + u32 s1 = 0, s2 = 0; + u32 *aData = (u32*)a; + u32 *aEnd = (u32*)&a[nByte]; + u32 x = 1; + + assert( nByte>=8 ); + assert( (nByte&0x00000007)==0 ); + assert( nByte<=65536 ); + + if( 1 == *(u8*)&x ){ + /* Little-endian */ + do { + s1 += *aData++ + s2; + s2 += *aData++ + s1; + }while( aData65536 || (nByte & (nByte-1))!=0 ) return; + cksmCompute(data, nByte-8, cksum); + sqlite3_result_int(context, memcmp(data+nByte-8,cksum,8)==0); +} + +#ifdef SQLITE_CKSUMVFS_INIT_FUNCNAME +/* +** SQL function: initialize_cksumvfs(SCHEMANAME) +** +** This SQL functions (whose name is actually determined at compile-time +** by the value of the SQLITE_CKSUMVFS_INIT_FUNCNAME macro) invokes: +** +** sqlite3_file_control(db, SCHEMANAME, SQLITE_FCNTL_RESERVE_BYTE, &n); +** +** In order to set the reserve bytes value to 8, so that cksumvfs will +** operation. This feature is provided (if and only if the +** SQLITE_CKSUMVFS_INIT_FUNCNAME compile-time option is set to a string +** which is the name of the SQL function) so as to provide the ability +** to invoke the file-control in programming languages that lack +** direct access to the sqlite3_file_control() interface (ex: Java). +** +** This interface is undocumented, apart from this comment. Usage +** example: +** +** 1. Compile with -DSQLITE_CKSUMVFS_INIT_FUNCNAME="ckvfs_init" +** 2. Run: "SELECT cksum_init('main'); VACUUM;" +*/ +static void cksmInitFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + int nByte = 8; + const char *zSchemaName = (const char*)sqlite3_value_text(argv[0]); + sqlite3 *db = sqlite3_context_db_handle(context); + sqlite3_file_control(db, zSchemaName, SQLITE_FCNTL_RESERVE_BYTES, &nByte); + /* Return NULL */ +} +#endif /* SQLITE_CKSUMBFS_INIT_FUNCNAME */ + +/* +** Close a cksm-file. +*/ +static int cksmClose(sqlite3_file *pFile){ + CksmFile *p = (CksmFile *)pFile; + if( p->pPartner ){ + assert( p->pPartner->pPartner==p ); + p->pPartner->pPartner = 0; + p->pPartner = 0; + } + pFile = ORIGFILE(pFile); + return pFile->pMethods->xClose(pFile); +} + +/* +** Set the computeCkSm and verifyCksm flags, if they need to be +** changed. +*/ +static void cksmSetFlags(CksmFile *p, int hasCorrectReserveSize){ + if( hasCorrectReserveSize!=p->computeCksm ){ + p->computeCksm = p->verifyCksm = hasCorrectReserveSize; + if( p->pPartner ){ + p->pPartner->verifyCksm = hasCorrectReserveSize; + p->pPartner->computeCksm = hasCorrectReserveSize; + } + } +} + +/* +** Read data from a cksm-file. +*/ +static int cksmRead( + sqlite3_file *pFile, + void *zBuf, + int iAmt, + sqlite_int64 iOfst +){ + int rc; + CksmFile *p = (CksmFile *)pFile; + pFile = ORIGFILE(pFile); + rc = pFile->pMethods->xRead(pFile, zBuf, iAmt, iOfst); + if( rc==SQLITE_OK ){ + if( iOfst==0 && iAmt>=100 && ( + memcmp(zBuf,"SQLite format 3",16)==0 || memcmp(zBuf,"ZV-",3)==0 + )){ + u8 *d = (u8*)zBuf; + char hasCorrectReserveSize = (d[20]==8); + cksmSetFlags(p, hasCorrectReserveSize); + } + /* Verify the checksum if + ** (1) the size indicates that we are dealing with a complete + ** database page + ** (2) checksum verification is enabled + */ + if( iAmt>=512 && (iAmt & (iAmt-1))==0 /* (1) */ + && p->verifyCksm /* (2) */ + ){ + u8 cksum[8]; + cksmCompute((u8*)zBuf, iAmt-8, cksum); + if( memcmp((u8*)zBuf+iAmt-8, cksum, 8)!=0 ){ + sqlite3_log(SQLITE_IOERR_DATA, + "checksum fault offset %lld of \"%s\"", + iOfst, p->zFName); + rc = SQLITE_IOERR_DATA; + } + } + } + return rc; +} + +/* +** Write data to a cksm-file. +*/ +static int cksmWrite( + sqlite3_file *pFile, + const void *zBuf, + int iAmt, + sqlite_int64 iOfst +){ + CksmFile *p = (CksmFile *)pFile; + pFile = ORIGFILE(pFile); + if( iOfst==0 && iAmt>=100 && ( + memcmp(zBuf,"SQLite format 3",16)==0 || memcmp(zBuf,"ZV-",3)==0 + )){ + u8 *d = (u8*)zBuf; + char hasCorrectReserveSize = (d[20]==8); + cksmSetFlags(p, hasCorrectReserveSize); + } + /* If the write size is appropriate for a database page and if + ** checksums where ever enabled, then it will be safe to compute + ** the checksums. The reserve byte size might have increased, but + ** it will never decrease. And because it cannot decrease, the + ** checksum will not overwrite anything. + */ + if( iAmt>=512 + && p->computeCksm + ){ + cksmCompute((u8*)zBuf, iAmt-8, ((u8*)zBuf)+iAmt-8); + } + return pFile->pMethods->xWrite(pFile, zBuf, iAmt, iOfst); +} + +/* +** Truncate a cksm-file. +*/ +static int cksmTruncate(sqlite3_file *pFile, sqlite_int64 size){ + pFile = ORIGFILE(pFile); + return pFile->pMethods->xTruncate(pFile, size); +} + +/* +** Sync a cksm-file. +*/ +static int cksmSync(sqlite3_file *pFile, int flags){ + pFile = ORIGFILE(pFile); + return pFile->pMethods->xSync(pFile, flags); +} + +/* +** Return the current file-size of a cksm-file. +*/ +static int cksmFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){ + CksmFile *p = (CksmFile *)pFile; + pFile = ORIGFILE(p); + return pFile->pMethods->xFileSize(pFile, pSize); +} + +/* +** Lock a cksm-file. +*/ +static int cksmLock(sqlite3_file *pFile, int eLock){ + pFile = ORIGFILE(pFile); + return pFile->pMethods->xLock(pFile, eLock); +} + +/* +** Unlock a cksm-file. +*/ +static int cksmUnlock(sqlite3_file *pFile, int eLock){ + pFile = ORIGFILE(pFile); + return pFile->pMethods->xUnlock(pFile, eLock); +} + +/* +** Check if another file-handle holds a RESERVED lock on a cksm-file. +*/ +static int cksmCheckReservedLock(sqlite3_file *pFile, int *pResOut){ + pFile = ORIGFILE(pFile); + return pFile->pMethods->xCheckReservedLock(pFile, pResOut); +} + +/* +** File control method. For custom operations on a cksm-file. +*/ +static int cksmFileControl(sqlite3_file *pFile, int op, void *pArg){ + int rc; + CksmFile *p = (CksmFile*)pFile; + pFile = ORIGFILE(pFile); + if( op==SQLITE_FCNTL_PRAGMA ){ + char **azArg = (char**)pArg; + assert( azArg[1]!=0 ); + if( sqlite3_stricmp(azArg[1],"checksum_verification")==0 ){ + char *zArg = azArg[2]; + if( zArg!=0 ){ + if( (zArg[0]>='1' && zArg[0]<='9') + || sqlite3_strlike("enable%",zArg,0)==0 + || sqlite3_stricmp("yes",zArg)==0 + || sqlite3_stricmp("on",zArg)==0 + ){ + p->verifyCksm = p->computeCksm; + }else{ + p->verifyCksm = 0; + } + if( p->pPartner ) p->pPartner->verifyCksm = p->verifyCksm; + } + azArg[0] = sqlite3_mprintf("%d",p->verifyCksm); + return SQLITE_OK; + }else if( p->computeCksm && azArg[2]!=0 + && sqlite3_stricmp(azArg[1], "page_size")==0 ){ + /* Do not allow page size changes on a checksum database */ + return SQLITE_OK; + } + } + rc = pFile->pMethods->xFileControl(pFile, op, pArg); + if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){ + *(char**)pArg = sqlite3_mprintf("cksm/%z", *(char**)pArg); + } + return rc; +} + +/* +** Return the sector-size in bytes for a cksm-file. +*/ +static int cksmSectorSize(sqlite3_file *pFile){ + pFile = ORIGFILE(pFile); + return pFile->pMethods->xSectorSize(pFile); +} + +/* +** Return the device characteristic flags supported by a cksm-file. +*/ +static int cksmDeviceCharacteristics(sqlite3_file *pFile){ + int devchar = 0; + pFile = ORIGFILE(pFile); + devchar = pFile->pMethods->xDeviceCharacteristics(pFile); + return (devchar & ~SQLITE_IOCAP_SUBPAGE_READ); +} + +/* Create a shared memory file mapping */ +static int cksmShmMap( + sqlite3_file *pFile, + int iPg, + int pgsz, + int bExtend, + void volatile **pp +){ + pFile = ORIGFILE(pFile); + return pFile->pMethods->xShmMap(pFile,iPg,pgsz,bExtend,pp); +} + +/* Perform locking on a shared-memory segment */ +static int cksmShmLock(sqlite3_file *pFile, int offset, int n, int flags){ + pFile = ORIGFILE(pFile); + return pFile->pMethods->xShmLock(pFile,offset,n,flags); +} + +/* Memory barrier operation on shared memory */ +static void cksmShmBarrier(sqlite3_file *pFile){ + pFile = ORIGFILE(pFile); + pFile->pMethods->xShmBarrier(pFile); +} + +/* Unmap a shared memory segment */ +static int cksmShmUnmap(sqlite3_file *pFile, int deleteFlag){ + pFile = ORIGFILE(pFile); + return pFile->pMethods->xShmUnmap(pFile,deleteFlag); +} + +/* Fetch a page of a memory-mapped file */ +static int cksmFetch( + sqlite3_file *pFile, + sqlite3_int64 iOfst, + int iAmt, + void **pp +){ + CksmFile *p = (CksmFile *)pFile; + if( p->computeCksm ){ + *pp = 0; + return SQLITE_OK; + } + pFile = ORIGFILE(pFile); + if( pFile->pMethods->iVersion>2 && pFile->pMethods->xFetch ){ + return pFile->pMethods->xFetch(pFile, iOfst, iAmt, pp); + } + *pp = 0; + return SQLITE_OK; +} + +/* Release a memory-mapped page */ +static int cksmUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){ + pFile = ORIGFILE(pFile); + if( pFile->pMethods->iVersion>2 && pFile->pMethods->xUnfetch ){ + return pFile->pMethods->xUnfetch(pFile, iOfst, pPage); + } + return SQLITE_OK; +} + +/* +** Open a cksm file handle. +*/ +static int cksmOpen( + sqlite3_vfs *pVfs, + const char *zName, + sqlite3_file *pFile, + int flags, + int *pOutFlags +){ + CksmFile *p; + sqlite3_file *pSubFile; + sqlite3_vfs *pSubVfs; + int rc; + pSubVfs = ORIGVFS(pVfs); + if( (flags & SQLITE_OPEN_MAIN_DB)==0 ){ + return pSubVfs->xOpen(pSubVfs, zName, pFile, flags, pOutFlags); + } + p = (CksmFile*)pFile; + memset(p, 0, sizeof(*p)); + pSubFile = ORIGFILE(pFile); + pFile->pMethods = &cksm_io_methods; + rc = pSubVfs->xOpen(pSubVfs, zName, pSubFile, flags, pOutFlags); + if( rc ) goto cksm_open_done; + p->zFName = zName; +cksm_open_done: + if( rc ) pFile->pMethods = 0; + return rc; +} + +/* +** All other VFS methods are pass-thrus. +*/ +static int cksmDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){ + return ORIGVFS(pVfs)->xDelete(ORIGVFS(pVfs), zPath, dirSync); +} +static int cksmAccess( + sqlite3_vfs *pVfs, + const char *zPath, + int flags, + int *pResOut +){ + return ORIGVFS(pVfs)->xAccess(ORIGVFS(pVfs), zPath, flags, pResOut); +} +static int cksmFullPathname( + sqlite3_vfs *pVfs, + const char *zPath, + int nOut, + char *zOut +){ + return ORIGVFS(pVfs)->xFullPathname(ORIGVFS(pVfs),zPath,nOut,zOut); +} +static void *cksmDlOpen(sqlite3_vfs *pVfs, const char *zPath){ + return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath); +} +static void cksmDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){ + ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg); +} +static void (*cksmDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){ + return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym); +} +static void cksmDlClose(sqlite3_vfs *pVfs, void *pHandle){ + ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle); +} +static int cksmRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){ + return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut); +} +static int cksmSleep(sqlite3_vfs *pVfs, int nMicro){ + return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro); +} +static int cksmCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){ + return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut); +} +static int cksmGetLastError(sqlite3_vfs *pVfs, int a, char *b){ + return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b); +} +static int cksmCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){ + sqlite3_vfs *pOrig = ORIGVFS(pVfs); + int rc; + assert( pOrig->iVersion>=2 ); + if( pOrig->xCurrentTimeInt64 ){ + rc = pOrig->xCurrentTimeInt64(pOrig, p); + }else{ + double r; + rc = pOrig->xCurrentTime(pOrig, &r); + *p = (sqlite3_int64)(r*86400000.0); + } + return rc; +} +static int cksmSetSystemCall( + sqlite3_vfs *pVfs, + const char *zName, + sqlite3_syscall_ptr pCall +){ + return ORIGVFS(pVfs)->xSetSystemCall(ORIGVFS(pVfs),zName,pCall); +} +static sqlite3_syscall_ptr cksmGetSystemCall( + sqlite3_vfs *pVfs, + const char *zName +){ + return ORIGVFS(pVfs)->xGetSystemCall(ORIGVFS(pVfs),zName); +} +static const char *cksmNextSystemCall(sqlite3_vfs *pVfs, const char *zName){ + return ORIGVFS(pVfs)->xNextSystemCall(ORIGVFS(pVfs), zName); +} + +/* Register the verify_checksum() SQL function. +*/ +static int cksmRegisterFunc( + sqlite3 *db, + char **pzErrMsg, + const sqlite3_api_routines *pApi +){ + int rc; + if( db==0 ) return SQLITE_OK; + rc = sqlite3_create_function(db, "verify_checksum", 1, + SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC, + 0, cksmVerifyFunc, 0, 0); +#ifdef SQLITE_CKSUMVFS_INIT_FUNCNAME + (void)sqlite3_create_function(db, SQLITE_CKSUMVFS_INIT_FUNCNAME, 1, + SQLITE_UTF8|SQLITE_DIRECTONLY, + 0, cksmInitFunc, 0, 0); +#endif + return rc; +} + +/* +** Register the cksum VFS as the default VFS for the system. +** Also make arrangements to automatically register the "verify_checksum()" +** SQL function on each new database connection. +*/ +static int cksmRegisterVfs(void){ + int rc = SQLITE_OK; + sqlite3_vfs *pOrig = sqlite3_vfs_find(0); + if( pOrig==0 ) return SQLITE_ERROR; + cksm_vfs.iVersion = pOrig->iVersion; + cksm_vfs.pAppData = pOrig; + cksm_vfs.szOsFile = pOrig->szOsFile + sizeof(CksmFile); + rc = sqlite3_vfs_register(&cksm_vfs, 1); + if( rc==SQLITE_OK ){ + rc = sqlite3_auto_extension((void(*)(void))cksmRegisterFunc); + } + return rc; +} + +#if defined(SQLITE_CKSUMVFS_STATIC) +/* This variant of the initializer runs when the extension is +** statically linked. +*/ +int sqlite3_register_cksumvfs(const char *NotUsed){ + (void)NotUsed; + return cksmRegisterVfs(); +} +int sqlite3_unregister_cksumvfs(void){ + if( sqlite3_vfs_find("cksmvfs") ){ + sqlite3_vfs_unregister(&cksm_vfs); + sqlite3_cancel_auto_extension((void(*)(void))cksmRegisterFunc); + } + return SQLITE_OK; +} +#endif /* defined(SQLITE_CKSUMVFS_STATIC */ + +#if !defined(SQLITE_CKSUMVFS_STATIC) +/* This variant of the initializer function is used when the +** extension is shared library to be loaded at run-time. +*/ +#ifdef _WIN32 +__declspec(dllexport) +#endif +/* +** This routine is called by sqlite3_load_extension() when the +** extension is first loaded. +***/ +int sqlite3_cksumvfs_init( + sqlite3 *db, + char **pzErrMsg, + const sqlite3_api_routines *pApi +){ + int rc; + SQLITE_EXTENSION_INIT2(pApi); + (void)pzErrMsg; /* not used */ + rc = cksmRegisterFunc(db, 0, 0); + if( rc==SQLITE_OK ){ + rc = cksmRegisterVfs(); + } + if( rc==SQLITE_OK ) rc = SQLITE_OK_LOAD_PERMANENTLY; + return rc; +} +#endif /* !defined(SQLITE_CKSUMVFS_STATIC) */ diff --git a/ext/misc/closure.c b/ext/misc/closure.c index 510c46ec90..14caf271f9 100644 --- a/ext/misc/closure.c +++ b/ext/misc/closure.c @@ -137,7 +137,7 @@ ** AND closure.idname='groupId' ** AND closure.parentname='parentId'; ** -** See the documentation at http://www.sqlite.org/loadext.html for information +** See the documentation at http://sqlite.org/loadext.html for information ** on how to compile and use loadable extensions such as this one. */ #include "sqlite3ext.h" @@ -422,16 +422,16 @@ static closure_avl *queuePull(closure_queue *pQueue){ ** `mno` becomes mno */ static char *closureDequote(const char *zIn){ - int nIn; /* Size of input string, in bytes */ + sqlite3_int64 nIn; /* Size of input string, in bytes */ char *zOut; /* Output (dequoted) string */ - nIn = (int)strlen(zIn); - zOut = sqlite3_malloc(nIn+1); + nIn = strlen(zIn); + zOut = sqlite3_malloc64(nIn+1); if( zOut ){ char q = zIn[0]; /* Quote character (if any ) */ if( q!='[' && q!= '\'' && q!='"' && q!='`' ){ - memcpy(zOut, zIn, nIn+1); + memcpy(zOut, zIn, (size_t)(nIn+1)); }else{ int iOut = 0; /* Index of next byte to write to output */ int iIn; /* Index of next byte to read from input */ @@ -588,12 +588,17 @@ static int closureOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){ return SQLITE_OK; } +/* +** Wrapper around sqlite3_free +*/ +static void closureMemFree(closure_avl *p){ sqlite3_free(p); } + /* ** Free up all the memory allocated by a cursor. Set it rLimit to 0 ** to indicate that it is at EOF. */ static void closureClearCursor(closure_cursor *pCur){ - closureAvlDestroy(pCur->pClosure, (void(*)(closure_avl*))sqlite3_free); + closureAvlDestroy(pCur->pClosure, closureMemFree); sqlite3_free(pCur->zTableName); sqlite3_free(pCur->zIdColumn); sqlite3_free(pCur->zParentColumn); @@ -826,17 +831,12 @@ static int closureBestIndex( int iPlan = 0; int i; int idx = 1; - int seenMatch = 0; const struct sqlite3_index_constraint *pConstraint; closure_vtab *pVtab = (closure_vtab*)pTab; double rCost = 10000000.0; pConstraint = pIdxInfo->aConstraint; for(i=0; inConstraint; i++, pConstraint++){ - if( pConstraint->iColumn==CLOSURE_COL_ROOT - && pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){ - seenMatch = 1; - } if( pConstraint->usable==0 ) continue; if( (iPlan & 1)==0 && pConstraint->iColumn==CLOSURE_COL_ROOT @@ -893,6 +893,18 @@ static int closureBestIndex( ** or else the result is an empty set. */ iPlan = 0; } + if( (iPlan&1)==0 ){ + /* If there is no usable "root=?" term, then set the index-type to 0. + ** Also clear any argvIndex variables already set. This is necessary + ** to prevent the core from throwing an "xBestIndex malfunction error" + ** error (because the argvIndex values are not contiguously assigned + ** starting from 1). */ + rCost *= 1e30; + for(i=0; inConstraint; i++, pConstraint++){ + pIdxInfo->aConstraintUsage[i].argvIndex = 0; + } + iPlan = 0; + } pIdxInfo->idxNum = iPlan; if( pIdxInfo->nOrderBy==1 && pIdxInfo->aOrderBy[0].iColumn==CLOSURE_COL_ID @@ -900,7 +912,6 @@ static int closureBestIndex( ){ pIdxInfo->orderByConsumed = 1; } - if( seenMatch && (iPlan&1)==0 ) rCost *= 1e30; pIdxInfo->estimatedCost = rCost; return SQLITE_OK; @@ -932,7 +943,9 @@ static sqlite3_module closureModule = { 0, /* xRename */ 0, /* xSavepoint */ 0, /* xRelease */ - 0 /* xRollbackTo */ + 0, /* xRollbackTo */ + 0, /* xShadowName */ + 0 /* xIntegrity */ }; #endif /* SQLITE_OMIT_VIRTUALTABLE */ diff --git a/ext/misc/completion.c b/ext/misc/completion.c new file mode 100644 index 0000000000..67b40d84d1 --- /dev/null +++ b/ext/misc/completion.c @@ -0,0 +1,510 @@ +/* +** 2017-07-10 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** This file implements an eponymous virtual table that returns suggested +** completions for a partial SQL input. +** +** Suggested usage: +** +** SELECT DISTINCT candidate COLLATE nocase +** FROM completion($prefix,$wholeline) +** ORDER BY 1; +** +** The two query parameters are optional. $prefix is the text of the +** current word being typed and that is to be completed. $wholeline is +** the complete input line, used for context. +** +** The raw completion() table might return the same candidate multiple +** times, for example if the same column name is used to two or more +** tables. And the candidates are returned in an arbitrary order. Hence, +** the DISTINCT and ORDER BY are recommended. +** +** This virtual table operates at the speed of human typing, and so there +** is no attempt to make it fast. Even a slow implementation will be much +** faster than any human can type. +** +*/ +#include "sqlite3ext.h" +SQLITE_EXTENSION_INIT1 +#include +#include +#include + +#ifndef SQLITE_OMIT_VIRTUALTABLE + +#ifndef IsAlnum +#define IsAlnum(X) isalnum((unsigned char)X) +#endif + + +/* completion_vtab is a subclass of sqlite3_vtab which will +** serve as the underlying representation of a completion virtual table +*/ +typedef struct completion_vtab completion_vtab; +struct completion_vtab { + sqlite3_vtab base; /* Base class - must be first */ + sqlite3 *db; /* Database connection for this completion vtab */ +}; + +/* completion_cursor is a subclass of sqlite3_vtab_cursor which will +** serve as the underlying representation of a cursor that scans +** over rows of the result +*/ +typedef struct completion_cursor completion_cursor; +struct completion_cursor { + sqlite3_vtab_cursor base; /* Base class - must be first */ + sqlite3 *db; /* Database connection for this cursor */ + int nPrefix, nLine; /* Number of bytes in zPrefix and zLine */ + char *zPrefix; /* The prefix for the word we want to complete */ + char *zLine; /* The whole that we want to complete */ + const char *zCurrentRow; /* Current output row */ + int szRow; /* Length of the zCurrentRow string */ + sqlite3_stmt *pStmt; /* Current statement */ + sqlite3_int64 iRowid; /* The rowid */ + int ePhase; /* Current phase */ + int j; /* inter-phase counter */ +}; + +/* Values for ePhase: +*/ +#define COMPLETION_FIRST_PHASE 1 +#define COMPLETION_KEYWORDS 1 +#define COMPLETION_PRAGMAS 2 +#define COMPLETION_FUNCTIONS 3 +#define COMPLETION_COLLATIONS 4 +#define COMPLETION_INDEXES 5 +#define COMPLETION_TRIGGERS 6 +#define COMPLETION_DATABASES 7 +#define COMPLETION_TABLES 8 /* Also VIEWs and TRIGGERs */ +#define COMPLETION_COLUMNS 9 +#define COMPLETION_MODULES 10 +#define COMPLETION_EOF 11 + +/* +** The completionConnect() method is invoked to create a new +** completion_vtab that describes the completion virtual table. +** +** Think of this routine as the constructor for completion_vtab objects. +** +** All this routine needs to do is: +** +** (1) Allocate the completion_vtab object and initialize all fields. +** +** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the +** result set of queries against completion will look like. +*/ +static int completionConnect( + sqlite3 *db, + void *pAux, + int argc, const char *const*argv, + sqlite3_vtab **ppVtab, + char **pzErr +){ + completion_vtab *pNew; + int rc; + + (void)(pAux); /* Unused parameter */ + (void)(argc); /* Unused parameter */ + (void)(argv); /* Unused parameter */ + (void)(pzErr); /* Unused parameter */ + +/* Column numbers */ +#define COMPLETION_COLUMN_CANDIDATE 0 /* Suggested completion of the input */ +#define COMPLETION_COLUMN_PREFIX 1 /* Prefix of the word to be completed */ +#define COMPLETION_COLUMN_WHOLELINE 2 /* Entire line seen so far */ +#define COMPLETION_COLUMN_PHASE 3 /* ePhase - used for debugging only */ + + sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS); + rc = sqlite3_declare_vtab(db, + "CREATE TABLE x(" + " candidate TEXT," + " prefix TEXT HIDDEN," + " wholeline TEXT HIDDEN," + " phase INT HIDDEN" /* Used for debugging only */ + ")"); + if( rc==SQLITE_OK ){ + pNew = sqlite3_malloc( sizeof(*pNew) ); + *ppVtab = (sqlite3_vtab*)pNew; + if( pNew==0 ) return SQLITE_NOMEM; + memset(pNew, 0, sizeof(*pNew)); + pNew->db = db; + } + return rc; +} + +/* +** This method is the destructor for completion_cursor objects. +*/ +static int completionDisconnect(sqlite3_vtab *pVtab){ + sqlite3_free(pVtab); + return SQLITE_OK; +} + +/* +** Constructor for a new completion_cursor object. +*/ +static int completionOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){ + completion_cursor *pCur; + pCur = sqlite3_malloc( sizeof(*pCur) ); + if( pCur==0 ) return SQLITE_NOMEM; + memset(pCur, 0, sizeof(*pCur)); + pCur->db = ((completion_vtab*)p)->db; + *ppCursor = &pCur->base; + return SQLITE_OK; +} + +/* +** Reset the completion_cursor. +*/ +static void completionCursorReset(completion_cursor *pCur){ + sqlite3_free(pCur->zPrefix); pCur->zPrefix = 0; pCur->nPrefix = 0; + sqlite3_free(pCur->zLine); pCur->zLine = 0; pCur->nLine = 0; + sqlite3_finalize(pCur->pStmt); pCur->pStmt = 0; + pCur->j = 0; +} + +/* +** Destructor for a completion_cursor. +*/ +static int completionClose(sqlite3_vtab_cursor *cur){ + completionCursorReset((completion_cursor*)cur); + sqlite3_free(cur); + return SQLITE_OK; +} + +/* +** Advance a completion_cursor to its next row of output. +** +** The ->ePhase, ->j, and ->pStmt fields of the completion_cursor object +** record the current state of the scan. This routine sets ->zCurrentRow +** to the current row of output and then returns. If no more rows remain, +** then ->ePhase is set to COMPLETION_EOF which will signal the virtual +** table that has reached the end of its scan. +** +** The current implementation just lists potential identifiers and +** keywords and filters them by zPrefix. Future enhancements should +** take zLine into account to try to restrict the set of identifiers and +** keywords based on what would be legal at the current point of input. +*/ +static int completionNext(sqlite3_vtab_cursor *cur){ + completion_cursor *pCur = (completion_cursor*)cur; + int eNextPhase = 0; /* Next phase to try if current phase reaches end */ + int iCol = -1; /* If >=0, step pCur->pStmt and use the i-th column */ + pCur->iRowid++; + while( pCur->ePhase!=COMPLETION_EOF ){ + switch( pCur->ePhase ){ + case COMPLETION_KEYWORDS: { + if( pCur->j >= sqlite3_keyword_count() ){ + pCur->zCurrentRow = 0; + pCur->ePhase = COMPLETION_DATABASES; + }else{ + sqlite3_keyword_name(pCur->j++, &pCur->zCurrentRow, &pCur->szRow); + } + iCol = -1; + break; + } + case COMPLETION_DATABASES: { + if( pCur->pStmt==0 ){ + sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, + &pCur->pStmt, 0); + } + iCol = 1; + eNextPhase = COMPLETION_TABLES; + break; + } + case COMPLETION_TABLES: { + if( pCur->pStmt==0 ){ + sqlite3_stmt *pS2; + char *zSql = 0; + const char *zSep = ""; + sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0); + while( sqlite3_step(pS2)==SQLITE_ROW ){ + const char *zDb = (const char*)sqlite3_column_text(pS2, 1); + zSql = sqlite3_mprintf( + "%z%s" + "SELECT name FROM \"%w\".sqlite_schema", + zSql, zSep, zDb + ); + if( zSql==0 ) return SQLITE_NOMEM; + zSep = " UNION "; + } + sqlite3_finalize(pS2); + sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0); + sqlite3_free(zSql); + } + iCol = 0; + eNextPhase = COMPLETION_COLUMNS; + break; + } + case COMPLETION_COLUMNS: { + if( pCur->pStmt==0 ){ + sqlite3_stmt *pS2; + char *zSql = 0; + const char *zSep = ""; + sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0); + while( sqlite3_step(pS2)==SQLITE_ROW ){ + const char *zDb = (const char*)sqlite3_column_text(pS2, 1); + zSql = sqlite3_mprintf( + "%z%s" + "SELECT pti.name FROM \"%w\".sqlite_schema AS sm" + " JOIN pragma_table_xinfo(sm.name,%Q) AS pti" + " WHERE sm.type='table'", + zSql, zSep, zDb, zDb + ); + if( zSql==0 ) return SQLITE_NOMEM; + zSep = " UNION "; + } + sqlite3_finalize(pS2); + sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0); + sqlite3_free(zSql); + } + iCol = 0; + eNextPhase = COMPLETION_EOF; + break; + } + } + if( iCol<0 ){ + /* This case is when the phase presets zCurrentRow */ + if( pCur->zCurrentRow==0 ) continue; + }else{ + if( sqlite3_step(pCur->pStmt)==SQLITE_ROW ){ + /* Extract the next row of content */ + pCur->zCurrentRow = (const char*)sqlite3_column_text(pCur->pStmt, iCol); + pCur->szRow = sqlite3_column_bytes(pCur->pStmt, iCol); + }else{ + /* When all rows are finished, advance to the next phase */ + sqlite3_finalize(pCur->pStmt); + pCur->pStmt = 0; + pCur->ePhase = eNextPhase; + continue; + } + } + if( pCur->nPrefix==0 ) break; + if( pCur->nPrefix<=pCur->szRow + && sqlite3_strnicmp(pCur->zPrefix, pCur->zCurrentRow, pCur->nPrefix)==0 + ){ + break; + } + } + + return SQLITE_OK; +} + +/* +** Return values of columns for the row at which the completion_cursor +** is currently pointing. +*/ +static int completionColumn( + sqlite3_vtab_cursor *cur, /* The cursor */ + sqlite3_context *ctx, /* First argument to sqlite3_result_...() */ + int i /* Which column to return */ +){ + completion_cursor *pCur = (completion_cursor*)cur; + switch( i ){ + case COMPLETION_COLUMN_CANDIDATE: { + sqlite3_result_text(ctx, pCur->zCurrentRow, pCur->szRow,SQLITE_TRANSIENT); + break; + } + case COMPLETION_COLUMN_PREFIX: { + sqlite3_result_text(ctx, pCur->zPrefix, -1, SQLITE_TRANSIENT); + break; + } + case COMPLETION_COLUMN_WHOLELINE: { + sqlite3_result_text(ctx, pCur->zLine, -1, SQLITE_TRANSIENT); + break; + } + case COMPLETION_COLUMN_PHASE: { + sqlite3_result_int(ctx, pCur->ePhase); + break; + } + } + return SQLITE_OK; +} + +/* +** Return the rowid for the current row. In this implementation, the +** rowid is the same as the output value. +*/ +static int completionRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ + completion_cursor *pCur = (completion_cursor*)cur; + *pRowid = pCur->iRowid; + return SQLITE_OK; +} + +/* +** Return TRUE if the cursor has been moved off of the last +** row of output. +*/ +static int completionEof(sqlite3_vtab_cursor *cur){ + completion_cursor *pCur = (completion_cursor*)cur; + return pCur->ePhase >= COMPLETION_EOF; +} + +/* +** This method is called to "rewind" the completion_cursor object back +** to the first row of output. This method is always called at least +** once prior to any call to completionColumn() or completionRowid() or +** completionEof(). +*/ +static int completionFilter( + sqlite3_vtab_cursor *pVtabCursor, + int idxNum, const char *idxStr, + int argc, sqlite3_value **argv +){ + completion_cursor *pCur = (completion_cursor *)pVtabCursor; + int iArg = 0; + (void)(idxStr); /* Unused parameter */ + (void)(argc); /* Unused parameter */ + completionCursorReset(pCur); + if( idxNum & 1 ){ + pCur->nPrefix = sqlite3_value_bytes(argv[iArg]); + if( pCur->nPrefix>0 ){ + pCur->zPrefix = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg])); + if( pCur->zPrefix==0 ) return SQLITE_NOMEM; + pCur->nPrefix = (int)strlen(pCur->zPrefix); + } + iArg = 1; + } + if( idxNum & 2 ){ + pCur->nLine = sqlite3_value_bytes(argv[iArg]); + if( pCur->nLine>0 ){ + pCur->zLine = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg])); + if( pCur->zLine==0 ) return SQLITE_NOMEM; + pCur->nLine = (int)strlen(pCur->zLine); + } + } + if( pCur->zLine!=0 && pCur->zPrefix==0 ){ + int i = pCur->nLine; + while( i>0 && (IsAlnum(pCur->zLine[i-1]) || pCur->zLine[i-1]=='_') ){ + i--; + } + pCur->nPrefix = pCur->nLine - i; + if( pCur->nPrefix>0 ){ + pCur->zPrefix = sqlite3_mprintf("%.*s", pCur->nPrefix, pCur->zLine + i); + if( pCur->zPrefix==0 ) return SQLITE_NOMEM; + pCur->nPrefix = (int)strlen(pCur->zPrefix); + } + } + pCur->iRowid = 0; + pCur->ePhase = COMPLETION_FIRST_PHASE; + return completionNext(pVtabCursor); +} + +/* +** SQLite will invoke this method one or more times while planning a query +** that uses the completion virtual table. This routine needs to create +** a query plan for each invocation and compute an estimated cost for that +** plan. +** +** There are two hidden parameters that act as arguments to the table-valued +** function: "prefix" and "wholeline". Bit 0 of idxNum is set if "prefix" +** is available and bit 1 is set if "wholeline" is available. +*/ +static int completionBestIndex( + sqlite3_vtab *tab, + sqlite3_index_info *pIdxInfo +){ + int i; /* Loop over constraints */ + int idxNum = 0; /* The query plan bitmask */ + int prefixIdx = -1; /* Index of the start= constraint, or -1 if none */ + int wholelineIdx = -1; /* Index of the stop= constraint, or -1 if none */ + int nArg = 0; /* Number of arguments that completeFilter() expects */ + const struct sqlite3_index_constraint *pConstraint; + + (void)(tab); /* Unused parameter */ + pConstraint = pIdxInfo->aConstraint; + for(i=0; inConstraint; i++, pConstraint++){ + if( pConstraint->usable==0 ) continue; + if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue; + switch( pConstraint->iColumn ){ + case COMPLETION_COLUMN_PREFIX: + prefixIdx = i; + idxNum |= 1; + break; + case COMPLETION_COLUMN_WHOLELINE: + wholelineIdx = i; + idxNum |= 2; + break; + } + } + if( prefixIdx>=0 ){ + pIdxInfo->aConstraintUsage[prefixIdx].argvIndex = ++nArg; + pIdxInfo->aConstraintUsage[prefixIdx].omit = 1; + } + if( wholelineIdx>=0 ){ + pIdxInfo->aConstraintUsage[wholelineIdx].argvIndex = ++nArg; + pIdxInfo->aConstraintUsage[wholelineIdx].omit = 1; + } + pIdxInfo->idxNum = idxNum; + pIdxInfo->estimatedCost = (double)5000 - 1000*nArg; + pIdxInfo->estimatedRows = 500 - 100*nArg; + return SQLITE_OK; +} + +/* +** This following structure defines all the methods for the +** completion virtual table. +*/ +static sqlite3_module completionModule = { + 0, /* iVersion */ + 0, /* xCreate */ + completionConnect, /* xConnect */ + completionBestIndex, /* xBestIndex */ + completionDisconnect, /* xDisconnect */ + 0, /* xDestroy */ + completionOpen, /* xOpen - open a cursor */ + completionClose, /* xClose - close a cursor */ + completionFilter, /* xFilter - configure scan constraints */ + completionNext, /* xNext - advance a cursor */ + completionEof, /* xEof - check for end of scan */ + completionColumn, /* xColumn - read data */ + completionRowid, /* xRowid - read data */ + 0, /* xUpdate */ + 0, /* xBegin */ + 0, /* xSync */ + 0, /* xCommit */ + 0, /* xRollback */ + 0, /* xFindMethod */ + 0, /* xRename */ + 0, /* xSavepoint */ + 0, /* xRelease */ + 0, /* xRollbackTo */ + 0, /* xShadowName */ + 0 /* xIntegrity */ +}; + +#endif /* SQLITE_OMIT_VIRTUALTABLE */ + +int sqlite3CompletionVtabInit(sqlite3 *db){ + int rc = SQLITE_OK; +#ifndef SQLITE_OMIT_VIRTUALTABLE + rc = sqlite3_create_module(db, "completion", &completionModule, 0); +#endif + return rc; +} + +#ifdef _WIN32 +__declspec(dllexport) +#endif +int sqlite3_completion_init( + sqlite3 *db, + char **pzErrMsg, + const sqlite3_api_routines *pApi +){ + int rc = SQLITE_OK; + SQLITE_EXTENSION_INIT2(pApi); + (void)(pzErrMsg); /* Unused parameter */ +#ifndef SQLITE_OMIT_VIRTUALTABLE + rc = sqlite3CompletionVtabInit(db); +#endif + return rc; +} diff --git a/ext/misc/compress.c b/ext/misc/compress.c index bf38d4c93c..48ea5182d7 100644 --- a/ext/misc/compress.c +++ b/ext/misc/compress.c @@ -27,6 +27,21 @@ SQLITE_EXTENSION_INIT1 ** seven bits per integer stored in the lower seven bits of each byte. ** More significant bits occur first. The most significant bit (0x80) ** is a flag to indicate the end of the integer. +** +** This function, SQLAR, and ZIP all use the same "deflate" compression +** algorithm, but each is subtly different: +** +** * ZIP uses raw deflate. +** +** * SQLAR uses the "zlib format" which is raw deflate with a two-byte +** algorithm-identification header and a four-byte checksum at the end. +** +** * This utility uses the "zlib format" like SQLAR, but adds the variable- +** length integer uncompressed size value at the beginning. +** +** This function might be extended in the future to support compression +** formats other than deflate, by providing a different algorithm-id +** mark following the variable-length integer size parameter. */ static void compressFunc( sqlite3_context *context, @@ -44,7 +59,7 @@ static void compressFunc( pIn = sqlite3_value_blob(argv[0]); nIn = sqlite3_value_bytes(argv[0]); nOut = 13 + nIn + (nIn+999)/1000; - pOut = sqlite3_malloc( nOut+5 ); + pOut = sqlite3_malloc64( nOut+5 ); for(i=4; i>=0; i--){ x[i] = (nIn >> (7*(4-i)))&0x7f; } @@ -74,7 +89,7 @@ static void uncompressFunc( unsigned int nIn; unsigned long int nOut; int rc; - int i; + unsigned int i; pIn = sqlite3_value_blob(argv[0]); nIn = sqlite3_value_bytes(argv[0]); @@ -83,7 +98,7 @@ static void uncompressFunc( nOut = (nOut<<7) | (pIn[i]&0x7f); if( (pIn[i]&0x80)!=0 ){ i++; break; } } - pOut = sqlite3_malloc( nOut+1 ); + pOut = sqlite3_malloc64( nOut+1 ); rc = uncompress(pOut, &nOut, &pIn[i], nIn-i); if( rc==Z_OK ){ sqlite3_result_blob(context, pOut, nOut, sqlite3_free); @@ -104,11 +119,13 @@ int sqlite3_compress_init( int rc = SQLITE_OK; SQLITE_EXTENSION_INIT2(pApi); (void)pzErrMsg; /* Unused parameter */ - rc = sqlite3_create_function(db, "compress", 1, SQLITE_UTF8, 0, - compressFunc, 0, 0); + rc = sqlite3_create_function(db, "compress", 1, + SQLITE_UTF8 | SQLITE_INNOCUOUS, + 0, compressFunc, 0, 0); if( rc==SQLITE_OK ){ - rc = sqlite3_create_function(db, "uncompress", 1, SQLITE_UTF8, 0, - uncompressFunc, 0, 0); + rc = sqlite3_create_function(db, "uncompress", 1, + SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC, + 0, uncompressFunc, 0, 0); } return rc; } diff --git a/ext/misc/csv.c b/ext/misc/csv.c new file mode 100644 index 0000000000..1caaaec876 --- /dev/null +++ b/ext/misc/csv.c @@ -0,0 +1,976 @@ +/* +** 2016-05-28 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +** This file contains the implementation of an SQLite virtual table for +** reading CSV files. +** +** Usage: +** +** .load ./csv +** CREATE VIRTUAL TABLE temp.csv USING csv(filename=FILENAME); +** SELECT * FROM csv; +** +** The columns are named "c1", "c2", "c3", ... by default. Or the +** application can define its own CREATE TABLE statement using the +** schema= parameter, like this: +** +** CREATE VIRTUAL TABLE temp.csv2 USING csv( +** filename = "../http.log", +** schema = "CREATE TABLE x(date,ipaddr,url,referrer,userAgent)" +** ); +** +** Instead of specifying a file, the text of the CSV can be loaded using +** the data= parameter. +** +** If the columns=N parameter is supplied, then the CSV file is assumed to have +** N columns. If both the columns= and schema= parameters are omitted, then +** the number and names of the columns is determined by the first line of +** the CSV input. +** +** Some extra debugging features (used for testing virtual tables) are available +** if this module is compiled with -DSQLITE_TEST. +*/ +#include +SQLITE_EXTENSION_INIT1 +#include +#include +#include +#include +#include +#include + +#ifndef SQLITE_OMIT_VIRTUALTABLE + +/* +** A macro to hint to the compiler that a function should not be +** inlined. +*/ +#if defined(__GNUC__) +# define CSV_NOINLINE __attribute__((noinline)) +#elif defined(_MSC_VER) && _MSC_VER>=1310 +# define CSV_NOINLINE __declspec(noinline) +#else +# define CSV_NOINLINE +#endif + +#ifndef SQLITEINT_H +typedef sqlite3_int64 i64; +#endif + +/* Max size of the error message in a CsvReader */ +#define CSV_MXERR 200 + +/* Size of the CsvReader input buffer */ +#define CSV_INBUFSZ 1024 + +/* A context object used when read a CSV file. */ +typedef struct CsvReader CsvReader; +struct CsvReader { + FILE *in; /* Read the CSV text from this input stream */ + char *z; /* Accumulated text for a field */ + i64 n; /* Number of bytes in z */ + i64 nAlloc; /* Space allocated for z[] */ + i64 nLine; /* Current line number */ + int bNotFirst; /* True if prior text has been seen */ + int cTerm; /* Character that terminated the most recent field */ + size_t iIn; /* Next unread character in the input buffer */ + size_t nIn; /* Number of characters in the input buffer */ + char *zIn; /* The input buffer */ + char zErr[CSV_MXERR]; /* Error message */ +}; + +/* Initialize a CsvReader object */ +static void csv_reader_init(CsvReader *p){ + p->in = 0; + p->z = 0; + p->n = 0; + p->nAlloc = 0; + p->nLine = 0; + p->bNotFirst = 0; + p->nIn = 0; + p->zIn = 0; + p->zErr[0] = 0; +} + +/* Close and reset a CsvReader object */ +static void csv_reader_reset(CsvReader *p){ + if( p->in ){ + fclose(p->in); + sqlite3_free(p->zIn); + } + sqlite3_free(p->z); + csv_reader_init(p); +} + +/* Report an error on a CsvReader */ +static void csv_errmsg(CsvReader *p, const char *zFormat, ...){ + va_list ap; + va_start(ap, zFormat); + sqlite3_vsnprintf(CSV_MXERR, p->zErr, zFormat, ap); + va_end(ap); +} + +/* Open the file associated with a CsvReader +** Return the number of errors. +*/ +static int csv_reader_open( + CsvReader *p, /* The reader to open */ + const char *zFilename, /* Read from this filename */ + const char *zData /* ... or use this data */ +){ + if( zFilename ){ + p->zIn = sqlite3_malloc( CSV_INBUFSZ ); + if( p->zIn==0 ){ + csv_errmsg(p, "out of memory"); + return 1; + } + p->in = fopen(zFilename, "rb"); + if( p->in==0 ){ + sqlite3_free(p->zIn); + csv_reader_reset(p); + csv_errmsg(p, "cannot open '%s' for reading", zFilename); + return 1; + } + }else{ + assert( p->in==0 ); + p->zIn = (char*)zData; + p->nIn = strlen(zData); + } + return 0; +} + +/* The input buffer has overflowed. Refill the input buffer, then +** return the next character +*/ +static CSV_NOINLINE int csv_getc_refill(CsvReader *p){ + size_t got; + + assert( p->iIn>=p->nIn ); /* Only called on an empty input buffer */ + assert( p->in!=0 ); /* Only called if reading froma file */ + + got = fread(p->zIn, 1, CSV_INBUFSZ, p->in); + if( got==0 ) return EOF; + p->nIn = got; + p->iIn = 1; + return p->zIn[0]; +} + +/* Return the next character of input. Return EOF at end of input. */ +static int csv_getc(CsvReader *p){ + if( p->iIn >= p->nIn ){ + if( p->in!=0 ) return csv_getc_refill(p); + return EOF; + } + return ((unsigned char*)p->zIn)[p->iIn++]; +} + +/* Increase the size of p->z and append character c to the end. +** Return 0 on success and non-zero if there is an OOM error */ +static CSV_NOINLINE int csv_resize_and_append(CsvReader *p, char c){ + char *zNew; + i64 nNew = p->nAlloc*2 + 100; + zNew = sqlite3_realloc64(p->z, nNew); + if( zNew ){ + p->z = zNew; + p->nAlloc = nNew; + p->z[p->n++] = c; + return 0; + }else{ + csv_errmsg(p, "out of memory"); + return 1; + } +} + +/* Append a single character to the CsvReader.z[] array. +** Return 0 on success and non-zero if there is an OOM error */ +static int csv_append(CsvReader *p, char c){ + if( p->n>=p->nAlloc-1 ) return csv_resize_and_append(p, c); + p->z[p->n++] = c; + return 0; +} + +/* Read a single field of CSV text. Compatible with rfc4180 and extended +** with the option of having a separator other than ",". +** +** + Input comes from p->in. +** + Store results in p->z of length p->n. Space to hold p->z comes +** from sqlite3_malloc64(). +** + Keep track of the line number in p->nLine. +** + Store the character that terminates the field in p->cTerm. Store +** EOF on end-of-file. +** +** Return 0 at EOF or on OOM. On EOF, the p->cTerm character will have +** been set to EOF. +*/ +static char *csv_read_one_field(CsvReader *p){ + int c; + p->n = 0; + c = csv_getc(p); + if( c==EOF ){ + p->cTerm = EOF; + return 0; + } + if( c=='"' ){ + int pc, ppc; + int startLine = p->nLine; + pc = ppc = 0; + while( 1 ){ + c = csv_getc(p); + if( c<='"' || pc=='"' ){ + if( c=='\n' ) p->nLine++; + if( c=='"' ){ + if( pc=='"' ){ + pc = 0; + continue; + } + } + if( (c==',' && pc=='"') + || (c=='\n' && pc=='"') + || (c=='\n' && pc=='\r' && ppc=='"') + || (c==EOF && pc=='"') + ){ + do{ p->n--; }while( p->z[p->n]!='"' ); + p->cTerm = (char)c; + break; + } + if( pc=='"' && c!='\r' ){ + csv_errmsg(p, "line %d: unescaped %c character", p->nLine, '"'); + break; + } + if( c==EOF ){ + csv_errmsg(p, "line %d: unterminated %c-quoted field\n", + startLine, '"'); + p->cTerm = (char)c; + break; + } + } + if( csv_append(p, (char)c) ) return 0; + ppc = pc; + pc = c; + } + }else{ + /* If this is the first field being parsed and it begins with the + ** UTF-8 BOM (0xEF BB BF) then skip the BOM */ + if( (c&0xff)==0xef && p->bNotFirst==0 ){ + csv_append(p, (char)c); + c = csv_getc(p); + if( (c&0xff)==0xbb ){ + csv_append(p, (char)c); + c = csv_getc(p); + if( (c&0xff)==0xbf ){ + p->bNotFirst = 1; + p->n = 0; + return csv_read_one_field(p); + } + } + } + while( c>',' || (c!=EOF && c!=',' && c!='\n') ){ + if( csv_append(p, (char)c) ) return 0; + c = csv_getc(p); + } + if( c=='\n' ){ + p->nLine++; + if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--; + } + p->cTerm = (char)c; + } + assert( p->z==0 || p->nnAlloc ); + if( p->z ) p->z[p->n] = 0; + p->bNotFirst = 1; + return p->z; +} + + +/* Forward references to the various virtual table methods implemented +** in this file. */ +static int csvtabCreate(sqlite3*, void*, int, const char*const*, + sqlite3_vtab**,char**); +static int csvtabConnect(sqlite3*, void*, int, const char*const*, + sqlite3_vtab**,char**); +static int csvtabBestIndex(sqlite3_vtab*,sqlite3_index_info*); +static int csvtabDisconnect(sqlite3_vtab*); +static int csvtabOpen(sqlite3_vtab*, sqlite3_vtab_cursor**); +static int csvtabClose(sqlite3_vtab_cursor*); +static int csvtabFilter(sqlite3_vtab_cursor*, int idxNum, const char *idxStr, + int argc, sqlite3_value **argv); +static int csvtabNext(sqlite3_vtab_cursor*); +static int csvtabEof(sqlite3_vtab_cursor*); +static int csvtabColumn(sqlite3_vtab_cursor*,sqlite3_context*,int); +static int csvtabRowid(sqlite3_vtab_cursor*,sqlite3_int64*); + +/* An instance of the CSV virtual table */ +typedef struct CsvTable { + sqlite3_vtab base; /* Base class. Must be first */ + char *zFilename; /* Name of the CSV file */ + char *zData; /* Raw CSV data in lieu of zFilename */ + long iStart; /* Offset to start of data in zFilename */ + int nCol; /* Number of columns in the CSV file */ + unsigned int tstFlags; /* Bit values used for testing */ +} CsvTable; + +/* Allowed values for tstFlags */ +#define CSVTEST_FIDX 0x0001 /* Pretend that constrained search cost less*/ + +/* A cursor for the CSV virtual table */ +typedef struct CsvCursor { + sqlite3_vtab_cursor base; /* Base class. Must be first */ + CsvReader rdr; /* The CsvReader object */ + char **azVal; /* Value of the current row */ + int *aLen; /* Length of each entry */ + sqlite3_int64 iRowid; /* The current rowid. Negative for EOF */ +} CsvCursor; + +/* Transfer error message text from a reader into a CsvTable */ +static void csv_xfer_error(CsvTable *pTab, CsvReader *pRdr){ + sqlite3_free(pTab->base.zErrMsg); + pTab->base.zErrMsg = sqlite3_mprintf("%s", pRdr->zErr); +} + +/* +** This method is the destructor fo a CsvTable object. +*/ +static int csvtabDisconnect(sqlite3_vtab *pVtab){ + CsvTable *p = (CsvTable*)pVtab; + sqlite3_free(p->zFilename); + sqlite3_free(p->zData); + sqlite3_free(p); + return SQLITE_OK; +} + +/* Skip leading whitespace. Return a pointer to the first non-whitespace +** character, or to the zero terminator if the string has only whitespace */ +static const char *csv_skip_whitespace(const char *z){ + while( isspace((unsigned char)z[0]) ) z++; + return z; +} + +/* Remove trailing whitespace from the end of string z[] */ +static void csv_trim_whitespace(char *z){ + size_t n = strlen(z); + while( n>0 && isspace((unsigned char)z[n]) ) n--; + z[n] = 0; +} + +/* Dequote the string */ +static void csv_dequote(char *z){ + int j; + char cQuote = z[0]; + size_t i, n; + + if( cQuote!='\'' && cQuote!='"' ) return; + n = strlen(z); + if( n<2 || z[n-1]!=z[0] ) return; + for(i=1, j=0; izErr. If there are no errors, p->zErr[0]==0. +*/ +static int csv_string_parameter( + CsvReader *p, /* Leave the error message here, if there is one */ + const char *zParam, /* Parameter we are checking for */ + const char *zArg, /* Raw text of the virtual table argment */ + char **pzVal /* Write the dequoted string value here */ +){ + const char *zValue; + zValue = csv_parameter(zParam,(int)strlen(zParam),zArg); + if( zValue==0 ) return 0; + p->zErr[0] = 0; + if( *pzVal ){ + csv_errmsg(p, "more than one '%s' parameter", zParam); + return 1; + } + *pzVal = sqlite3_mprintf("%s", zValue); + if( *pzVal==0 ){ + csv_errmsg(p, "out of memory"); + return 1; + } + csv_trim_whitespace(*pzVal); + csv_dequote(*pzVal); + return 1; +} + + +/* Return 0 if the argument is false and 1 if it is true. Return -1 if +** we cannot really tell. +*/ +static int csv_boolean(const char *z){ + if( sqlite3_stricmp("yes",z)==0 + || sqlite3_stricmp("on",z)==0 + || sqlite3_stricmp("true",z)==0 + || (z[0]=='1' && z[1]==0) + ){ + return 1; + } + if( sqlite3_stricmp("no",z)==0 + || sqlite3_stricmp("off",z)==0 + || sqlite3_stricmp("false",z)==0 + || (z[0]=='0' && z[1]==0) + ){ + return 0; + } + return -1; +} + +/* Check to see if the string is of the form: "TAG = BOOLEAN" or just "TAG". +** If it is, set *pValue to be the value of the boolean ("true" if there is +** not "= BOOLEAN" component) and return non-zero. If the input string +** does not begin with TAG, return zero. +*/ +static int csv_boolean_parameter( + const char *zTag, /* Tag we are looking for */ + int nTag, /* Size of the tag in bytes */ + const char *z, /* Input parameter */ + int *pValue /* Write boolean value here */ +){ + int b; + z = csv_skip_whitespace(z); + if( strncmp(zTag, z, nTag)!=0 ) return 0; + z = csv_skip_whitespace(z + nTag); + if( z[0]==0 ){ + *pValue = 1; + return 1; + } + if( z[0]!='=' ) return 0; + z = csv_skip_whitespace(z+1); + b = csv_boolean(z); + if( b>=0 ){ + *pValue = b; + return 1; + } + return 0; +} + +/* +** Parameters: +** filename=FILENAME Name of file containing CSV content +** data=TEXT Direct CSV content. +** schema=SCHEMA Alternative CSV schema. +** header=YES|NO First row of CSV defines the names of +** columns if "yes". Default "no". +** columns=N Assume the CSV file contains N columns. +** +** Only available if compiled with SQLITE_TEST: +** +** testflags=N Bitmask of test flags. Optional +** +** If schema= is omitted, then the columns are named "c0", "c1", "c2", +** and so forth. If columns=N is omitted, then the file is opened and +** the number of columns in the first row is counted to determine the +** column count. If header=YES, then the first row is skipped. +*/ +static int csvtabConnect( + sqlite3 *db, + void *pAux, + int argc, const char *const*argv, + sqlite3_vtab **ppVtab, + char **pzErr +){ + CsvTable *pNew = 0; /* The CsvTable object to construct */ + int bHeader = -1; /* header= flags. -1 means not seen yet */ + int rc = SQLITE_OK; /* Result code from this routine */ + int i, j; /* Loop counters */ +#ifdef SQLITE_TEST + int tstFlags = 0; /* Value for testflags=N parameter */ +#endif + int b; /* Value of a boolean parameter */ + int nCol = -99; /* Value of the columns= parameter */ + CsvReader sRdr; /* A CSV file reader used to store an error + ** message and/or to count the number of columns */ + static const char *azParam[] = { + "filename", "data", "schema", + }; + char *azPValue[3]; /* Parameter values */ +# define CSV_FILENAME (azPValue[0]) +# define CSV_DATA (azPValue[1]) +# define CSV_SCHEMA (azPValue[2]) + + assert( sizeof(azPValue)==sizeof(azParam) ); + memset(&sRdr, 0, sizeof(sRdr)); + memset(azPValue, 0, sizeof(azPValue)); + for(i=3; i=0 ){ + csv_errmsg(&sRdr, "more than one 'header' parameter"); + goto csvtab_connect_error; + } + bHeader = b; + }else +#ifdef SQLITE_TEST + if( (zValue = csv_parameter("testflags",9,z))!=0 ){ + tstFlags = (unsigned int)atoi(zValue); + }else +#endif + if( (zValue = csv_parameter("columns",7,z))!=0 ){ + if( nCol>0 ){ + csv_errmsg(&sRdr, "more than one 'columns' parameter"); + goto csvtab_connect_error; + } + nCol = atoi(zValue); + if( nCol<=0 ){ + csv_errmsg(&sRdr, "column= value must be positive"); + goto csvtab_connect_error; + } + }else + { + csv_errmsg(&sRdr, "bad parameter: '%s'", z); + goto csvtab_connect_error; + } + } + if( (CSV_FILENAME==0)==(CSV_DATA==0) ){ + csv_errmsg(&sRdr, "must specify either filename= or data= but not both"); + goto csvtab_connect_error; + } + + if( (nCol<=0 || bHeader==1) + && csv_reader_open(&sRdr, CSV_FILENAME, CSV_DATA) + ){ + goto csvtab_connect_error; + } + pNew = sqlite3_malloc( sizeof(*pNew) ); + *ppVtab = (sqlite3_vtab*)pNew; + if( pNew==0 ) goto csvtab_connect_oom; + memset(pNew, 0, sizeof(*pNew)); + if( CSV_SCHEMA==0 ){ + sqlite3_str *pStr = sqlite3_str_new(0); + char *zSep = ""; + int iCol = 0; + sqlite3_str_appendf(pStr, "CREATE TABLE x("); + if( nCol<0 && bHeader<1 ){ + nCol = 0; + do{ + csv_read_one_field(&sRdr); + nCol++; + }while( sRdr.cTerm==',' ); + } + if( nCol>0 && bHeader<1 ){ + for(iCol=0; iCol0 && iColnCol = nCol; + sqlite3_str_appendf(pStr, ")"); + CSV_SCHEMA = sqlite3_str_finish(pStr); + if( CSV_SCHEMA==0 ) goto csvtab_connect_oom; + }else if( nCol<0 ){ + do{ + csv_read_one_field(&sRdr); + pNew->nCol++; + }while( sRdr.cTerm==',' ); + }else{ + pNew->nCol = nCol; + } + pNew->zFilename = CSV_FILENAME; CSV_FILENAME = 0; + pNew->zData = CSV_DATA; CSV_DATA = 0; +#ifdef SQLITE_TEST + pNew->tstFlags = tstFlags; +#endif + if( bHeader!=1 ){ + pNew->iStart = 0; + }else if( pNew->zData ){ + pNew->iStart = (int)sRdr.iIn; + }else{ + pNew->iStart = (int)(ftell(sRdr.in) - sRdr.nIn + sRdr.iIn); + } + csv_reader_reset(&sRdr); + rc = sqlite3_declare_vtab(db, CSV_SCHEMA); + if( rc ){ + csv_errmsg(&sRdr, "bad schema: '%s' - %s", CSV_SCHEMA, sqlite3_errmsg(db)); + goto csvtab_connect_error; + } + for(i=0; ibase); + for(i=0; ibase.pVtab; + int i; + for(i=0; inCol; i++){ + sqlite3_free(pCur->azVal[i]); + pCur->azVal[i] = 0; + pCur->aLen[i] = 0; + } +} + +/* +** The xConnect and xCreate methods do the same thing, but they must be +** different so that the virtual table is not an eponymous virtual table. +*/ +static int csvtabCreate( + sqlite3 *db, + void *pAux, + int argc, const char *const*argv, + sqlite3_vtab **ppVtab, + char **pzErr +){ + return csvtabConnect(db, pAux, argc, argv, ppVtab, pzErr); +} + +/* +** Destructor for a CsvCursor. +*/ +static int csvtabClose(sqlite3_vtab_cursor *cur){ + CsvCursor *pCur = (CsvCursor*)cur; + csvtabCursorRowReset(pCur); + csv_reader_reset(&pCur->rdr); + sqlite3_free(cur); + return SQLITE_OK; +} + +/* +** Constructor for a new CsvTable cursor object. +*/ +static int csvtabOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){ + CsvTable *pTab = (CsvTable*)p; + CsvCursor *pCur; + size_t nByte; + nByte = sizeof(*pCur) + (sizeof(char*)+sizeof(int))*pTab->nCol; + pCur = sqlite3_malloc64( nByte ); + if( pCur==0 ) return SQLITE_NOMEM; + memset(pCur, 0, nByte); + pCur->azVal = (char**)&pCur[1]; + pCur->aLen = (int*)&pCur->azVal[pTab->nCol]; + *ppCursor = &pCur->base; + if( csv_reader_open(&pCur->rdr, pTab->zFilename, pTab->zData) ){ + csv_xfer_error(pTab, &pCur->rdr); + return SQLITE_ERROR; + } + return SQLITE_OK; +} + + +/* +** Advance a CsvCursor to its next row of input. +** Set the EOF marker if we reach the end of input. +*/ +static int csvtabNext(sqlite3_vtab_cursor *cur){ + CsvCursor *pCur = (CsvCursor*)cur; + CsvTable *pTab = (CsvTable*)cur->pVtab; + int i = 0; + char *z; + do{ + z = csv_read_one_field(&pCur->rdr); + if( z==0 ){ + break; + } + if( inCol ){ + if( pCur->aLen[i] < pCur->rdr.n+1 ){ + char *zNew = sqlite3_realloc64(pCur->azVal[i], pCur->rdr.n+1); + if( zNew==0 ){ + csv_errmsg(&pCur->rdr, "out of memory"); + csv_xfer_error(pTab, &pCur->rdr); + break; + } + pCur->azVal[i] = zNew; + pCur->aLen[i] = pCur->rdr.n+1; + } + memcpy(pCur->azVal[i], z, pCur->rdr.n+1); + i++; + } + }while( pCur->rdr.cTerm==',' ); + if( z==0 && i==0 ){ + pCur->iRowid = -1; + }else{ + pCur->iRowid++; + while( inCol ){ + sqlite3_free(pCur->azVal[i]); + pCur->azVal[i] = 0; + pCur->aLen[i] = 0; + i++; + } + } + return SQLITE_OK; +} + +/* +** Return values of columns for the row at which the CsvCursor +** is currently pointing. +*/ +static int csvtabColumn( + sqlite3_vtab_cursor *cur, /* The cursor */ + sqlite3_context *ctx, /* First argument to sqlite3_result_...() */ + int i /* Which column to return */ +){ + CsvCursor *pCur = (CsvCursor*)cur; + CsvTable *pTab = (CsvTable*)cur->pVtab; + if( i>=0 && inCol && pCur->azVal[i]!=0 ){ + sqlite3_result_text(ctx, pCur->azVal[i], -1, SQLITE_TRANSIENT); + } + return SQLITE_OK; +} + +/* +** Return the rowid for the current row. +*/ +static int csvtabRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ + CsvCursor *pCur = (CsvCursor*)cur; + *pRowid = pCur->iRowid; + return SQLITE_OK; +} + +/* +** Return TRUE if the cursor has been moved off of the last +** row of output. +*/ +static int csvtabEof(sqlite3_vtab_cursor *cur){ + CsvCursor *pCur = (CsvCursor*)cur; + return pCur->iRowid<0; +} + +/* +** Only a full table scan is supported. So xFilter simply rewinds to +** the beginning. +*/ +static int csvtabFilter( + sqlite3_vtab_cursor *pVtabCursor, + int idxNum, const char *idxStr, + int argc, sqlite3_value **argv +){ + CsvCursor *pCur = (CsvCursor*)pVtabCursor; + CsvTable *pTab = (CsvTable*)pVtabCursor->pVtab; + pCur->iRowid = 0; + + /* Ensure the field buffer is always allocated. Otherwise, if the + ** first field is zero bytes in size, this may be mistaken for an OOM + ** error in csvtabNext(). */ + if( csv_append(&pCur->rdr, 0) ) return SQLITE_NOMEM; + + if( pCur->rdr.in==0 ){ + assert( pCur->rdr.zIn==pTab->zData ); + assert( pTab->iStart>=0 ); + assert( (size_t)pTab->iStart<=pCur->rdr.nIn ); + pCur->rdr.iIn = pTab->iStart; + }else{ + fseek(pCur->rdr.in, pTab->iStart, SEEK_SET); + pCur->rdr.iIn = 0; + pCur->rdr.nIn = 0; + } + return csvtabNext(pVtabCursor); +} + +/* +** Only a forward full table scan is supported. xBestIndex is mostly +** a no-op. If CSVTEST_FIDX is set, then the presence of equality +** constraints lowers the estimated cost, which is fiction, but is useful +** for testing certain kinds of virtual table behavior. +*/ +static int csvtabBestIndex( + sqlite3_vtab *tab, + sqlite3_index_info *pIdxInfo +){ + pIdxInfo->estimatedCost = 1000000; +#ifdef SQLITE_TEST + if( (((CsvTable*)tab)->tstFlags & CSVTEST_FIDX)!=0 ){ + /* The usual (and sensible) case is to always do a full table scan. + ** The code in this branch only runs when testflags=1. This code + ** generates an artifical and unrealistic plan which is useful + ** for testing virtual table logic but is not helpful to real applications. + ** + ** Any ==, LIKE, or GLOB constraint is marked as usable by the virtual + ** table (even though it is not) and the cost of running the virtual table + ** is reduced from 1 million to just 10. The constraints are *not* marked + ** as omittable, however, so the query planner should still generate a + ** plan that gives a correct answer, even if they plan is not optimal. + */ + int i; + int nConst = 0; + for(i=0; inConstraint; i++){ + unsigned char op; + if( pIdxInfo->aConstraint[i].usable==0 ) continue; + op = pIdxInfo->aConstraint[i].op; + if( op==SQLITE_INDEX_CONSTRAINT_EQ + || op==SQLITE_INDEX_CONSTRAINT_LIKE + || op==SQLITE_INDEX_CONSTRAINT_GLOB + ){ + pIdxInfo->estimatedCost = 10; + pIdxInfo->aConstraintUsage[nConst].argvIndex = nConst+1; + nConst++; + } + } + } +#endif + return SQLITE_OK; +} + + +static sqlite3_module CsvModule = { + 0, /* iVersion */ + csvtabCreate, /* xCreate */ + csvtabConnect, /* xConnect */ + csvtabBestIndex, /* xBestIndex */ + csvtabDisconnect, /* xDisconnect */ + csvtabDisconnect, /* xDestroy */ + csvtabOpen, /* xOpen - open a cursor */ + csvtabClose, /* xClose - close a cursor */ + csvtabFilter, /* xFilter - configure scan constraints */ + csvtabNext, /* xNext - advance a cursor */ + csvtabEof, /* xEof - check for end of scan */ + csvtabColumn, /* xColumn - read data */ + csvtabRowid, /* xRowid - read data */ + 0, /* xUpdate */ + 0, /* xBegin */ + 0, /* xSync */ + 0, /* xCommit */ + 0, /* xRollback */ + 0, /* xFindMethod */ + 0, /* xRename */ + 0, /* xSavepoint */ + 0, /* xRelease */ + 0, /* xRollbackTo */ + 0, /* xShadowName */ + 0 /* xIntegrity */ +}; + +#ifdef SQLITE_TEST +/* +** For virtual table testing, make a version of the CSV virtual table +** available that has an xUpdate function. But the xUpdate always returns +** SQLITE_READONLY since the CSV file is not really writable. +*/ +static int csvtabUpdate(sqlite3_vtab *p,int n,sqlite3_value**v,sqlite3_int64*x){ + return SQLITE_READONLY; +} +static sqlite3_module CsvModuleFauxWrite = { + 0, /* iVersion */ + csvtabCreate, /* xCreate */ + csvtabConnect, /* xConnect */ + csvtabBestIndex, /* xBestIndex */ + csvtabDisconnect, /* xDisconnect */ + csvtabDisconnect, /* xDestroy */ + csvtabOpen, /* xOpen - open a cursor */ + csvtabClose, /* xClose - close a cursor */ + csvtabFilter, /* xFilter - configure scan constraints */ + csvtabNext, /* xNext - advance a cursor */ + csvtabEof, /* xEof - check for end of scan */ + csvtabColumn, /* xColumn - read data */ + csvtabRowid, /* xRowid - read data */ + csvtabUpdate, /* xUpdate */ + 0, /* xBegin */ + 0, /* xSync */ + 0, /* xCommit */ + 0, /* xRollback */ + 0, /* xFindMethod */ + 0, /* xRename */ + 0, /* xSavepoint */ + 0, /* xRelease */ + 0, /* xRollbackTo */ + 0, /* xShadowName */ + 0 /* xIntegrity */ +}; +#endif /* SQLITE_TEST */ + +#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */ + + +#ifdef _WIN32 +__declspec(dllexport) +#endif +/* +** This routine is called when the extension is loaded. The new +** CSV virtual table module is registered with the calling database +** connection. +*/ +int sqlite3_csv_init( + sqlite3 *db, + char **pzErrMsg, + const sqlite3_api_routines *pApi +){ +#ifndef SQLITE_OMIT_VIRTUALTABLE + int rc; + SQLITE_EXTENSION_INIT2(pApi); + rc = sqlite3_create_module(db, "csv", &CsvModule, 0); +#ifdef SQLITE_TEST + if( rc==SQLITE_OK ){ + rc = sqlite3_create_module(db, "csv_wr", &CsvModuleFauxWrite, 0); + } +#endif + return rc; +#else + return SQLITE_OK; +#endif +} diff --git a/ext/misc/dbdump.c b/ext/misc/dbdump.c new file mode 100644 index 0000000000..0b93d2b9be --- /dev/null +++ b/ext/misc/dbdump.c @@ -0,0 +1,724 @@ +/* +** 2016-03-13 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +** This file implements a C-language subroutine that converts the content +** of an SQLite database into UTF-8 text SQL statements that can be used +** to exactly recreate the original database. ROWID values are preserved. +** +** A prototype of the implemented subroutine is this: +** +** int sqlite3_db_dump( +** sqlite3 *db, +** const char *zSchema, +** const char *zTable, +** void (*xCallback)(void*, const char*), +** void *pArg +** ); +** +** The db parameter is the database connection. zSchema is the schema within +** that database which is to be dumped. Usually the zSchema is "main" but +** can also be "temp" or any ATTACH-ed database. If zTable is not NULL, then +** only the content of that one table is dumped. If zTable is NULL, then all +** tables are dumped. +** +** The generate text is passed to xCallback() in multiple calls. The second +** argument to xCallback() is a copy of the pArg parameter. The first +** argument is some of the output text that this routine generates. The +** signature to xCallback() is designed to make it compatible with fputs(). +** +** The sqlite3_db_dump() subroutine returns SQLITE_OK on success or some error +** code if it encounters a problem. +** +** If this file is compiled with -DDBDUMP_STANDALONE then a "main()" routine +** is included so that this routine becomes a command-line utility. The +** command-line utility takes two or three arguments which are the name +** of the database file, the schema, and optionally the table, forming the +** first three arguments of a single call to the library routine. +*/ +#include "sqlite3.h" +#include +#include +#include + +/* +** The state of the dump process. +*/ +typedef struct DState DState; +struct DState { + sqlite3 *db; /* The database connection */ + int nErr; /* Number of errors seen so far */ + int rc; /* Error code */ + int writableSchema; /* True if in writable_schema mode */ + int (*xCallback)(const char*,void*); /* Send output here */ + void *pArg; /* Argument to xCallback() */ +}; + +/* +** A variable length string to which one can append text. +*/ +typedef struct DText DText; +struct DText { + char *z; /* The text */ + sqlite3_int64 n; /* Number of bytes of content in z[] */ + sqlite3_int64 nAlloc; /* Number of bytes allocated to z[] */ +}; + +/* +** Initialize and destroy a DText object +*/ +static void initText(DText *p){ + memset(p, 0, sizeof(*p)); +} +static void freeText(DText *p){ + sqlite3_free(p->z); + initText(p); +} + +/* zIn is either a pointer to a NULL-terminated string in memory obtained +** from malloc(), or a NULL pointer. The string pointed to by zAppend is +** added to zIn, and the result returned in memory obtained from malloc(). +** zIn, if it was not NULL, is freed. +** +** If the third argument, quote, is not '\0', then it is used as a +** quote character for zAppend. +*/ +static void appendText(DText *p, char const *zAppend, char quote){ + int len; + int i; + int nAppend = (int)(strlen(zAppend) & 0x3fffffff); + + len = nAppend+p->n+1; + if( quote ){ + len += 2; + for(i=0; in+len>=p->nAlloc ){ + char *zNew; + p->nAlloc = p->nAlloc*2 + len + 20; + zNew = sqlite3_realloc64(p->z, p->nAlloc); + if( zNew==0 ){ + freeText(p); + return; + } + p->z = zNew; + } + + if( quote ){ + char *zCsr = p->z+p->n; + *zCsr++ = quote; + for(i=0; in = (int)(zCsr - p->z); + *zCsr = '\0'; + }else{ + memcpy(p->z+p->n, zAppend, nAppend); + p->n += nAppend; + p->z[p->n] = '\0'; + } +} + +/* +** Attempt to determine if identifier zName needs to be quoted, either +** because it contains non-alphanumeric characters, or because it is an +** SQLite keyword. Be conservative in this estimate: When in doubt assume +** that quoting is required. +** +** Return '"' if quoting is required. Return 0 if no quoting is required. +*/ +static char quoteChar(const char *zName){ + int i; + if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"'; + for(i=0; zName[i]; i++){ + if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"'; + } + return sqlite3_keyword_check(zName, i) ? '"' : 0; +} + + +/* +** Release memory previously allocated by tableColumnList(). +*/ +static void freeColumnList(char **azCol){ + int i; + for(i=1; azCol[i]; i++){ + sqlite3_free(azCol[i]); + } + /* azCol[0] is a static string */ + sqlite3_free(azCol); +} + +/* +** Return a list of pointers to strings which are the names of all +** columns in table zTab. The memory to hold the names is dynamically +** allocated and must be released by the caller using a subsequent call +** to freeColumnList(). +** +** The azCol[0] entry is usually NULL. However, if zTab contains a rowid +** value that needs to be preserved, then azCol[0] is filled in with the +** name of the rowid column. +** +** The first regular column in the table is azCol[1]. The list is terminated +** by an entry with azCol[i]==0. +*/ +static char **tableColumnList(DState *p, const char *zTab){ + char **azCol = 0; + sqlite3_stmt *pStmt = 0; + char *zSql; + sqlite3_int64 nCol = 0; + sqlite3_int64 nAlloc = 0; + int nPK = 0; /* Number of PRIMARY KEY columns seen */ + int isIPK = 0; /* True if one PRIMARY KEY column of type INTEGER */ + int preserveRowid = 1; + int rc; + + zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab); + if( zSql==0 ) return 0; + rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); + sqlite3_free(zSql); + if( rc ) return 0; + while( sqlite3_step(pStmt)==SQLITE_ROW ){ + if( nCol>=nAlloc-2 ){ + char **azNew; + nAlloc = nAlloc*2 + nCol + 10; + azNew = sqlite3_realloc64(azCol, nAlloc*sizeof(azCol[0])); + if( azNew==0 ) goto col_oom; + azCol = azNew; + azCol[0] = 0; + } + azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1)); + if( azCol[nCol]==0 ) goto col_oom; + if( sqlite3_column_int(pStmt, 5) ){ + nPK++; + if( nPK==1 + && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2), + "INTEGER")==0 + ){ + isIPK = 1; + }else{ + isIPK = 0; + } + } + } + sqlite3_finalize(pStmt); + pStmt = 0; + azCol[nCol+1] = 0; + + /* The decision of whether or not a rowid really needs to be preserved + ** is tricky. We never need to preserve a rowid for a WITHOUT ROWID table + ** or a table with an INTEGER PRIMARY KEY. We are unable to preserve + ** rowids on tables where the rowid is inaccessible because there are other + ** columns in the table named "rowid", "_rowid_", and "oid". + */ + if( isIPK ){ + /* If a single PRIMARY KEY column with type INTEGER was seen, then it + ** might be an alise for the ROWID. But it might also be a WITHOUT ROWID + ** table or a INTEGER PRIMARY KEY DESC column, neither of which are + ** ROWID aliases. To distinguish these cases, check to see if + ** there is a "pk" entry in "PRAGMA index_list". There will be + ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID. + */ + zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)" + " WHERE origin='pk'", zTab); + if( zSql==0 ) goto col_oom; + rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); + sqlite3_free(zSql); + if( rc ){ + freeColumnList(azCol); + return 0; + } + rc = sqlite3_step(pStmt); + sqlite3_finalize(pStmt); + pStmt = 0; + preserveRowid = rc==SQLITE_ROW; + } + if( preserveRowid ){ + /* Only preserve the rowid if we can find a name to use for the + ** rowid */ + static char *azRowid[] = { "rowid", "_rowid_", "oid" }; + int i, j; + for(j=0; j<3; j++){ + for(i=1; i<=nCol; i++){ + if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break; + } + if( i>nCol ){ + /* At this point, we know that azRowid[j] is not the name of any + ** ordinary column in the table. Verify that azRowid[j] is a valid + ** name for the rowid before adding it to azCol[0]. WITHOUT ROWID + ** tables will fail this last check */ + rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0); + if( rc==SQLITE_OK ) azCol[0] = azRowid[j]; + break; + } + } + } + return azCol; + +col_oom: + sqlite3_finalize(pStmt); + freeColumnList(azCol); + p->nErr++; + p->rc = SQLITE_NOMEM; + return 0; +} + +/* +** Send mprintf-formatted content to the output callback. +*/ +static void output_formatted(DState *p, const char *zFormat, ...){ + va_list ap; + char *z; + va_start(ap, zFormat); + z = sqlite3_vmprintf(zFormat, ap); + va_end(ap); + p->xCallback(z, p->pArg); + sqlite3_free(z); +} + +/* +** Find a string that is not found anywhere in z[]. Return a pointer +** to that string. +** +** Try to use zA and zB first. If both of those are already found in z[] +** then make up some string and store it in the buffer zBuf. +*/ +static const char *unused_string( + const char *z, /* Result must not appear anywhere in z */ + const char *zA, const char *zB, /* Try these first */ + char *zBuf /* Space to store a generated string */ +){ + unsigned i = 0; + if( strstr(z, zA)==0 ) return zA; + if( strstr(z, zB)==0 ) return zB; + do{ + sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++); + }while( strstr(z,zBuf)!=0 ); + return zBuf; +} + +/* +** Output the given string as a quoted string using SQL quoting conventions. +** Additionallly , escape the "\n" and "\r" characters so that they do not +** get corrupted by end-of-line translation facilities in some operating +** systems. +*/ +static void output_quoted_escaped_string(DState *p, const char *z){ + int i; + char c; + for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){} + if( c==0 ){ + output_formatted(p,"'%s'",z); + }else{ + const char *zNL = 0; + const char *zCR = 0; + int nNL = 0; + int nCR = 0; + char zBuf1[20], zBuf2[20]; + for(i=0; z[i]; i++){ + if( z[i]=='\n' ) nNL++; + if( z[i]=='\r' ) nCR++; + } + if( nNL ){ + p->xCallback("replace(", p->pArg); + zNL = unused_string(z, "\\n", "\\012", zBuf1); + } + if( nCR ){ + p->xCallback("replace(", p->pArg); + zCR = unused_string(z, "\\r", "\\015", zBuf2); + } + p->xCallback("'", p->pArg); + while( *z ){ + for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){} + if( c=='\'' ) i++; + if( i ){ + output_formatted(p, "%.*s", i, z); + z += i; + } + if( c=='\'' ){ + p->xCallback("'", p->pArg); + continue; + } + if( c==0 ){ + break; + } + z++; + if( c=='\n' ){ + p->xCallback(zNL, p->pArg); + continue; + } + p->xCallback(zCR, p->pArg); + } + p->xCallback("'", p->pArg); + if( nCR ){ + output_formatted(p, ",'%s',char(13))", zCR); + } + if( nNL ){ + output_formatted(p, ",'%s',char(10))", zNL); + } + } +} + +/* +** This is an sqlite3_exec callback routine used for dumping the database. +** Each row received by this callback consists of a table name, +** the table type ("index" or "table") and SQL to create the table. +** This routine should print text sufficient to recreate the table. +*/ +static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){ + int rc; + const char *zTable; + const char *zType; + const char *zSql; + DState *p = (DState*)pArg; + sqlite3_stmt *pStmt; + + (void)azCol; + if( nArg!=3 ) return 1; + zTable = azArg[0]; + zType = azArg[1]; + zSql = azArg[2]; + + if( strcmp(zTable, "sqlite_sequence")==0 ){ + p->xCallback("DELETE FROM sqlite_sequence;\n", p->pArg); + }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){ + p->xCallback("ANALYZE sqlite_schema;\n", p->pArg); + }else if( strncmp(zTable, "sqlite_", 7)==0 ){ + return 0; + }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){ + if( !p->writableSchema ){ + p->xCallback("PRAGMA writable_schema=ON;\n", p->pArg); + p->writableSchema = 1; + } + output_formatted(p, + "INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)" + "VALUES('table','%q','%q',0,'%q');", + zTable, zTable, zSql); + return 0; + }else{ + if( sqlite3_strglob("CREATE TABLE ['\"]*", zSql)==0 ){ + p->xCallback("CREATE TABLE IF NOT EXISTS ", p->pArg); + p->xCallback(zSql+13, p->pArg); + }else{ + p->xCallback(zSql, p->pArg); + } + p->xCallback(";\n", p->pArg); + } + + if( strcmp(zType, "table")==0 ){ + DText sSelect; + DText sTable; + char **azTCol; + int i; + int nCol; + + azTCol = tableColumnList(p, zTable); + if( azTCol==0 ) return 0; + + initText(&sTable); + appendText(&sTable, "INSERT INTO ", 0); + + /* Always quote the table name, even if it appears to be pure ascii, + ** in case it is a keyword. Ex: INSERT INTO "table" ... */ + appendText(&sTable, zTable, quoteChar(zTable)); + + /* If preserving the rowid, add a column list after the table name. + ** In other words: "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)" + ** instead of the usual "INSERT INTO tab VALUES(...)". + */ + if( azTCol[0] ){ + appendText(&sTable, "(", 0); + appendText(&sTable, azTCol[0], 0); + for(i=1; azTCol[i]; i++){ + appendText(&sTable, ",", 0); + appendText(&sTable, azTCol[i], quoteChar(azTCol[i])); + } + appendText(&sTable, ")", 0); + } + appendText(&sTable, " VALUES(", 0); + + /* Build an appropriate SELECT statement */ + initText(&sSelect); + appendText(&sSelect, "SELECT ", 0); + if( azTCol[0] ){ + appendText(&sSelect, azTCol[0], 0); + appendText(&sSelect, ",", 0); + } + for(i=1; azTCol[i]; i++){ + appendText(&sSelect, azTCol[i], quoteChar(azTCol[i])); + if( azTCol[i+1] ){ + appendText(&sSelect, ",", 0); + } + } + nCol = i; + if( azTCol[0]==0 ) nCol--; + freeColumnList(azTCol); + appendText(&sSelect, " FROM ", 0); + appendText(&sSelect, zTable, quoteChar(zTable)); + + rc = sqlite3_prepare_v2(p->db, sSelect.z, -1, &pStmt, 0); + if( rc!=SQLITE_OK ){ + p->nErr++; + if( p->rc==SQLITE_OK ) p->rc = rc; + }else{ + while( SQLITE_ROW==sqlite3_step(pStmt) ){ + p->xCallback(sTable.z, p->pArg); + for(i=0; ixCallback(",", p->pArg); + switch( sqlite3_column_type(pStmt,i) ){ + case SQLITE_INTEGER: { + output_formatted(p, "%lld", sqlite3_column_int64(pStmt,i)); + break; + } + case SQLITE_FLOAT: { + double r = sqlite3_column_double(pStmt,i); + sqlite3_uint64 ur; + memcpy(&ur,&r,sizeof(r)); + if( ur==0x7ff0000000000000LL ){ + p->xCallback("1e999", p->pArg); + }else if( ur==0xfff0000000000000LL ){ + p->xCallback("-1e999", p->pArg); + }else{ + output_formatted(p, "%!.20g", r); + } + break; + } + case SQLITE_NULL: { + p->xCallback("NULL", p->pArg); + break; + } + case SQLITE_TEXT: { + output_quoted_escaped_string(p, + (const char*)sqlite3_column_text(pStmt,i)); + break; + } + case SQLITE_BLOB: { + int nByte = sqlite3_column_bytes(pStmt,i); + unsigned char *a = (unsigned char*)sqlite3_column_blob(pStmt,i); + int j; + p->xCallback("x'", p->pArg); + for(j=0; j>4)&15]; + zWord[1] = "0123456789abcdef"[a[j]&15]; + zWord[2] = 0; + p->xCallback(zWord, p->pArg); + } + p->xCallback("'", p->pArg); + break; + } + } + } + p->xCallback(");\n", p->pArg); + } + } + sqlite3_finalize(pStmt); + freeText(&sTable); + freeText(&sSelect); + } + return 0; +} + + +/* +** Execute a query statement that will generate SQL output. Print +** the result columns, comma-separated, on a line and then add a +** semicolon terminator to the end of that line. +** +** If the number of columns is 1 and that column contains text "--" +** then write the semicolon on a separate line. That way, if a +** "--" comment occurs at the end of the statement, the comment +** won't consume the semicolon terminator. +*/ +static void output_sql_from_query( + DState *p, /* Query context */ + const char *zSelect, /* SELECT statement to extract content */ + ... +){ + sqlite3_stmt *pSelect; + int rc; + int nResult; + int i; + const char *z; + char *zSql; + va_list ap; + va_start(ap, zSelect); + zSql = sqlite3_vmprintf(zSelect, ap); + va_end(ap); + if( zSql==0 ){ + p->rc = SQLITE_NOMEM; + p->nErr++; + return; + } + rc = sqlite3_prepare_v2(p->db, zSql, -1, &pSelect, 0); + sqlite3_free(zSql); + if( rc!=SQLITE_OK || !pSelect ){ + output_formatted(p, "/**** ERROR: (%d) %s *****/\n", rc, + sqlite3_errmsg(p->db)); + p->nErr++; + return; + } + rc = sqlite3_step(pSelect); + nResult = sqlite3_column_count(pSelect); + while( rc==SQLITE_ROW ){ + z = (const char*)sqlite3_column_text(pSelect, 0); + p->xCallback(z, p->pArg); + for(i=1; ixCallback(",", p->pArg); + p->xCallback((const char*)sqlite3_column_text(pSelect,i), p->pArg); + } + if( z==0 ) z = ""; + while( z[0] && (z[0]!='-' || z[1]!='-') ) z++; + if( z[0] ){ + p->xCallback("\n;\n", p->pArg); + }else{ + p->xCallback(";\n", p->pArg); + } + rc = sqlite3_step(pSelect); + } + rc = sqlite3_finalize(pSelect); + if( rc!=SQLITE_OK ){ + output_formatted(p, "/**** ERROR: (%d) %s *****/\n", rc, + sqlite3_errmsg(p->db)); + if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++; + } +} + +/* +** Run zQuery. Use dump_callback() as the callback routine so that +** the contents of the query are output as SQL statements. +** +** If we get a SQLITE_CORRUPT error, rerun the query after appending +** "ORDER BY rowid DESC" to the end. +*/ +static void run_schema_dump_query( + DState *p, + const char *zQuery, + ... +){ + char *zErr = 0; + char *z; + va_list ap; + va_start(ap, zQuery); + z = sqlite3_vmprintf(zQuery, ap); + va_end(ap); + sqlite3_exec(p->db, z, dump_callback, p, &zErr); + sqlite3_free(z); + if( zErr ){ + output_formatted(p, "/****** %s ******/\n", zErr); + sqlite3_free(zErr); + p->nErr++; + zErr = 0; + } +} + +/* +** Convert an SQLite database into SQL statements that will recreate that +** database. +*/ +int sqlite3_db_dump( + sqlite3 *db, /* The database connection */ + const char *zSchema, /* Which schema to dump. Usually "main". */ + const char *zTable, /* Which table to dump. NULL means everything. */ + int (*xCallback)(const char*,void*), /* Output sent to this callback */ + void *pArg /* Second argument of the callback */ +){ + DState x; + memset(&x, 0, sizeof(x)); + x.rc = sqlite3_exec(db, "BEGIN", 0, 0, 0); + if( x.rc ) return x.rc; + x.db = db; + x.xCallback = xCallback; + x.pArg = pArg; + xCallback("PRAGMA foreign_keys=OFF;\nBEGIN TRANSACTION;\n", pArg); + if( zTable==0 ){ + run_schema_dump_query(&x, + "SELECT name, type, sql FROM \"%w\".sqlite_schema " + "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'", + zSchema + ); + run_schema_dump_query(&x, + "SELECT name, type, sql FROM \"%w\".sqlite_schema " + "WHERE name=='sqlite_sequence'", zSchema + ); + output_sql_from_query(&x, + "SELECT sql FROM sqlite_schema " + "WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0 + ); + }else{ + run_schema_dump_query(&x, + "SELECT name, type, sql FROM \"%w\".sqlite_schema " + "WHERE tbl_name=%Q COLLATE nocase AND type=='table'" + " AND sql NOT NULL", + zSchema, zTable + ); + output_sql_from_query(&x, + "SELECT sql FROM \"%w\".sqlite_schema " + "WHERE sql NOT NULL" + " AND type IN ('index','trigger','view')" + " AND tbl_name=%Q COLLATE nocase", + zSchema, zTable + ); + } + if( x.writableSchema ){ + xCallback("PRAGMA writable_schema=OFF;\n", pArg); + } + xCallback(x.nErr ? "ROLLBACK; -- due to errors\n" : "COMMIT;\n", pArg); + sqlite3_exec(db, "COMMIT", 0, 0, 0); + return x.rc; +} + + + +/* The generic subroutine is above. The code the follows implements +** the command-line interface. +*/ +#ifdef DBDUMP_STANDALONE +#include + +/* +** Command-line interface +*/ +int main(int argc, char **argv){ + sqlite3 *db; + const char *zDb; + const char *zSchema; + const char *zTable = 0; + int rc; + + if( argc<2 || argc>4 ){ + fprintf(stderr, "Usage: %s DATABASE ?SCHEMA? ?TABLE?\n", argv[0]); + return 1; + } + zDb = argv[1]; + zSchema = argc>=3 ? argv[2] : "main"; + zTable = argc==4 ? argv[3] : 0; + + rc = sqlite3_open(zDb, &db); + if( rc ){ + fprintf(stderr, "Cannot open \"%s\": %s\n", zDb, sqlite3_errmsg(db)); + sqlite3_close(db); + return 1; + } + rc = sqlite3_db_dump(db, zSchema, zTable, + (int(*)(const char*,void*))fputs, (void*)stdout); + if( rc ){ + fprintf(stderr, "Error: sqlite3_db_dump() returns %d\n", rc); + } + sqlite3_close(db); + return rc!=SQLITE_OK; +} +#endif /* DBDUMP_STANDALONE */ diff --git a/ext/misc/decimal.c b/ext/misc/decimal.c new file mode 100644 index 0000000000..f87699f96b --- /dev/null +++ b/ext/misc/decimal.c @@ -0,0 +1,902 @@ +/* +** 2020-06-22 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +** Routines to implement arbitrary-precision decimal math. +** +** The focus here is on simplicity and correctness, not performance. +*/ +#include "sqlite3ext.h" +SQLITE_EXTENSION_INIT1 +#include +#include +#include +#include + +/* Mark a function parameter as unused, to suppress nuisance compiler +** warnings. */ +#ifndef UNUSED_PARAMETER +# define UNUSED_PARAMETER(X) (void)(X) +#endif + +#ifndef IsSpace +#define IsSpace(X) isspace((unsigned char)X) +#endif + +/* A decimal object */ +typedef struct Decimal Decimal; +struct Decimal { + char sign; /* 0 for positive, 1 for negative */ + char oom; /* True if an OOM is encountered */ + char isNull; /* True if holds a NULL rather than a number */ + char isInit; /* True upon initialization */ + int nDigit; /* Total number of digits */ + int nFrac; /* Number of digits to the right of the decimal point */ + signed char *a; /* Array of digits. Most significant first. */ +}; + +/* +** Release memory held by a Decimal, but do not free the object itself. +*/ +static void decimal_clear(Decimal *p){ + sqlite3_free(p->a); +} + +/* +** Destroy a Decimal object +*/ +static void decimal_free(Decimal *p){ + if( p ){ + decimal_clear(p); + sqlite3_free(p); + } +} + +/* +** Allocate a new Decimal object initialized to the text in zIn[]. +** Return NULL if any kind of error occurs. +*/ +static Decimal *decimalNewFromText(const char *zIn, int n){ + Decimal *p = 0; + int i; + int iExp = 0; + + p = sqlite3_malloc( sizeof(*p) ); + if( p==0 ) goto new_from_text_failed; + p->sign = 0; + p->oom = 0; + p->isInit = 1; + p->isNull = 0; + p->nDigit = 0; + p->nFrac = 0; + p->a = sqlite3_malloc64( n+1 ); + if( p->a==0 ) goto new_from_text_failed; + for(i=0; IsSpace(zIn[i]); i++){} + if( zIn[i]=='-' ){ + p->sign = 1; + i++; + }else if( zIn[i]=='+' ){ + i++; + } + while( i='0' && c<='9' ){ + p->a[p->nDigit++] = c - '0'; + }else if( c=='.' ){ + p->nFrac = p->nDigit + 1; + }else if( c=='e' || c=='E' ){ + int j = i+1; + int neg = 0; + if( j>=n ) break; + if( zIn[j]=='-' ){ + neg = 1; + j++; + }else if( zIn[j]=='+' ){ + j++; + } + while( j='0' && zIn[j]<='9' ){ + iExp = iExp*10 + zIn[j] - '0'; + } + j++; + } + if( neg ) iExp = -iExp; + break; + } + i++; + } + if( p->nFrac ){ + p->nFrac = p->nDigit - (p->nFrac - 1); + } + if( iExp>0 ){ + if( p->nFrac>0 ){ + if( iExp<=p->nFrac ){ + p->nFrac -= iExp; + iExp = 0; + }else{ + iExp -= p->nFrac; + p->nFrac = 0; + } + } + if( iExp>0 ){ + p->a = sqlite3_realloc64(p->a, (sqlite3_int64)p->nDigit + + (sqlite3_int64)iExp + 1 ); + if( p->a==0 ) goto new_from_text_failed; + memset(p->a+p->nDigit, 0, iExp); + p->nDigit += iExp; + } + }else if( iExp<0 ){ + int nExtra; + iExp = -iExp; + nExtra = p->nDigit - p->nFrac - 1; + if( nExtra ){ + if( nExtra>=iExp ){ + p->nFrac += iExp; + iExp = 0; + }else{ + iExp -= nExtra; + p->nFrac = p->nDigit - 1; + } + } + if( iExp>0 ){ + p->a = sqlite3_realloc64(p->a, (sqlite3_int64)p->nDigit + + (sqlite3_int64)iExp + 1 ); + if( p->a==0 ) goto new_from_text_failed; + memmove(p->a+iExp, p->a, p->nDigit); + memset(p->a, 0, iExp); + p->nDigit += iExp; + p->nFrac += iExp; + } + } + if( p->sign ){ + for(i=0; inDigit && p->a[i]==0; i++){} + if( i>=p->nDigit ) p->sign = 0; + } + return p; + +new_from_text_failed: + if( p ){ + if( p->a ) sqlite3_free(p->a); + sqlite3_free(p); + } + return 0; +} + +/* Forward reference */ +static Decimal *decimalFromDouble(double); + +/* +** Allocate a new Decimal object from an sqlite3_value. Return a pointer +** to the new object, or NULL if there is an error. If the pCtx argument +** is not NULL, then errors are reported on it as well. +** +** If the pIn argument is SQLITE_TEXT or SQLITE_INTEGER, it is converted +** directly into a Decimal. For SQLITE_FLOAT or for SQLITE_BLOB of length +** 8 bytes, the resulting double value is expanded into its decimal equivalent. +** If pIn is NULL or if it is a BLOB that is not exactly 8 bytes in length, +** then NULL is returned. +*/ +static Decimal *decimal_new( + sqlite3_context *pCtx, /* Report error here, if not null */ + sqlite3_value *pIn, /* Construct the decimal object from this */ + int bTextOnly /* Always interpret pIn as text if true */ +){ + Decimal *p = 0; + int eType = sqlite3_value_type(pIn); + if( bTextOnly && (eType==SQLITE_FLOAT || eType==SQLITE_BLOB) ){ + eType = SQLITE_TEXT; + } + switch( eType ){ + case SQLITE_TEXT: + case SQLITE_INTEGER: { + const char *zIn = (const char*)sqlite3_value_text(pIn); + int n = sqlite3_value_bytes(pIn); + p = decimalNewFromText(zIn, n); + if( p==0 ) goto new_failed; + break; + } + + case SQLITE_FLOAT: { + p = decimalFromDouble(sqlite3_value_double(pIn)); + break; + } + + case SQLITE_BLOB: { + const unsigned char *x; + unsigned int i; + sqlite3_uint64 v = 0; + double r; + + if( sqlite3_value_bytes(pIn)!=sizeof(r) ) break; + x = sqlite3_value_blob(pIn); + for(i=0; ioom ){ + sqlite3_result_error_nomem(pCtx); + return; + } + if( p->isNull ){ + sqlite3_result_null(pCtx); + return; + } + z = sqlite3_malloc64( (sqlite3_int64)p->nDigit+4 ); + if( z==0 ){ + sqlite3_result_error_nomem(pCtx); + return; + } + i = 0; + if( p->nDigit==0 || (p->nDigit==1 && p->a[0]==0) ){ + p->sign = 0; + } + if( p->sign ){ + z[0] = '-'; + i = 1; + } + n = p->nDigit - p->nFrac; + if( n<=0 ){ + z[i++] = '0'; + } + j = 0; + while( n>1 && p->a[j]==0 ){ + j++; + n--; + } + while( n>0 ){ + z[i++] = p->a[j] + '0'; + j++; + n--; + } + if( p->nFrac ){ + z[i++] = '.'; + do{ + z[i++] = p->a[j] + '0'; + j++; + }while( jnDigit ); + } + z[i] = 0; + sqlite3_result_text(pCtx, z, i, sqlite3_free); +} + +/* +** Make the given Decimal the result in an format similar to '%+#e'. +** In other words, show exponential notation with leading and trailing +** zeros omitted. +*/ +static void decimal_result_sci(sqlite3_context *pCtx, Decimal *p){ + char *z; /* The output buffer */ + int i; /* Loop counter */ + int nZero; /* Number of leading zeros */ + int nDigit; /* Number of digits not counting trailing zeros */ + int nFrac; /* Digits to the right of the decimal point */ + int exp; /* Exponent value */ + signed char zero; /* Zero value */ + signed char *a; /* Array of digits */ + + if( p==0 || p->oom ){ + sqlite3_result_error_nomem(pCtx); + return; + } + if( p->isNull ){ + sqlite3_result_null(pCtx); + return; + } + for(nDigit=p->nDigit; nDigit>0 && p->a[nDigit-1]==0; nDigit--){} + for(nZero=0; nZeroa[nZero]==0; nZero++){} + nFrac = p->nFrac + (nDigit - p->nDigit); + nDigit -= nZero; + z = sqlite3_malloc64( (sqlite3_int64)nDigit+20 ); + if( z==0 ){ + sqlite3_result_error_nomem(pCtx); + return; + } + if( nDigit==0 ){ + zero = 0; + a = &zero; + nDigit = 1; + nFrac = 0; + }else{ + a = &p->a[nZero]; + } + if( p->sign && nDigit>0 ){ + z[0] = '-'; + }else{ + z[0] = '+'; + } + z[1] = a[0]+'0'; + z[2] = '.'; + if( nDigit==1 ){ + z[3] = '0'; + i = 4; + }else{ + for(i=1; iisNull==0 +** pB!=0 +** pB->isNull==0 +*/ +static int decimal_cmp(Decimal *pA, Decimal *pB){ + int nASig, nBSig, rc, n; + while( pA->nFrac>0 && pA->a[pA->nDigit-1]==0 ){ + pA->nDigit--; + pA->nFrac--; + } + while( pB->nFrac>0 && pB->a[pB->nDigit-1]==0 ){ + pB->nDigit--; + pB->nFrac--; + } + if( pA->sign!=pB->sign ){ + return pA->sign ? -1 : +1; + } + if( pA->sign ){ + Decimal *pTemp = pA; + pA = pB; + pB = pTemp; + } + nASig = pA->nDigit - pA->nFrac; + nBSig = pB->nDigit - pB->nFrac; + if( nASig!=nBSig ){ + return nASig - nBSig; + } + n = pA->nDigit; + if( n>pB->nDigit ) n = pB->nDigit; + rc = memcmp(pA->a, pB->a, n); + if( rc==0 ){ + rc = pA->nDigit - pB->nDigit; + } + return rc; +} + +/* +** SQL Function: decimal_cmp(X, Y) +** +** Return negative, zero, or positive if X is less then, equal to, or +** greater than Y. +*/ +static void decimalCmpFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + Decimal *pA = 0, *pB = 0; + int rc; + + UNUSED_PARAMETER(argc); + pA = decimal_new(context, argv[0], 1); + if( pA==0 || pA->isNull ) goto cmp_done; + pB = decimal_new(context, argv[1], 1); + if( pB==0 || pB->isNull ) goto cmp_done; + rc = decimal_cmp(pA, pB); + if( rc<0 ) rc = -1; + else if( rc>0 ) rc = +1; + sqlite3_result_int(context, rc); +cmp_done: + decimal_free(pA); + decimal_free(pB); +} + +/* +** Expand the Decimal so that it has a least nDigit digits and nFrac +** digits to the right of the decimal point. +*/ +static void decimal_expand(Decimal *p, int nDigit, int nFrac){ + int nAddSig; + int nAddFrac; + if( p==0 ) return; + nAddFrac = nFrac - p->nFrac; + nAddSig = (nDigit - p->nDigit) - nAddFrac; + if( nAddFrac==0 && nAddSig==0 ) return; + p->a = sqlite3_realloc64(p->a, nDigit+1); + if( p->a==0 ){ + p->oom = 1; + return; + } + if( nAddSig ){ + memmove(p->a+nAddSig, p->a, p->nDigit); + memset(p->a, 0, nAddSig); + p->nDigit += nAddSig; + } + if( nAddFrac ){ + memset(p->a+p->nDigit, 0, nAddFrac); + p->nDigit += nAddFrac; + p->nFrac += nAddFrac; + } +} + +/* +** Add the value pB into pA. A := A + B. +** +** Both pA and pB might become denormalized by this routine. +*/ +static void decimal_add(Decimal *pA, Decimal *pB){ + int nSig, nFrac, nDigit; + int i, rc; + if( pA==0 ){ + return; + } + if( pA->oom || pB==0 || pB->oom ){ + pA->oom = 1; + return; + } + if( pA->isNull || pB->isNull ){ + pA->isNull = 1; + return; + } + nSig = pA->nDigit - pA->nFrac; + if( nSig && pA->a[0]==0 ) nSig--; + if( nSignDigit-pB->nFrac ){ + nSig = pB->nDigit - pB->nFrac; + } + nFrac = pA->nFrac; + if( nFracnFrac ) nFrac = pB->nFrac; + nDigit = nSig + nFrac + 1; + decimal_expand(pA, nDigit, nFrac); + decimal_expand(pB, nDigit, nFrac); + if( pA->oom || pB->oom ){ + pA->oom = 1; + }else{ + if( pA->sign==pB->sign ){ + int carry = 0; + for(i=nDigit-1; i>=0; i--){ + int x = pA->a[i] + pB->a[i] + carry; + if( x>=10 ){ + carry = 1; + pA->a[i] = x - 10; + }else{ + carry = 0; + pA->a[i] = x; + } + } + }else{ + signed char *aA, *aB; + int borrow = 0; + rc = memcmp(pA->a, pB->a, nDigit); + if( rc<0 ){ + aA = pB->a; + aB = pA->a; + pA->sign = !pA->sign; + }else{ + aA = pA->a; + aB = pB->a; + } + for(i=nDigit-1; i>=0; i--){ + int x = aA[i] - aB[i] - borrow; + if( x<0 ){ + pA->a[i] = x+10; + borrow = 1; + }else{ + pA->a[i] = x; + borrow = 0; + } + } + } + } +} + +/* +** Multiply A by B. A := A * B +** +** All significant digits after the decimal point are retained. +** Trailing zeros after the decimal point are omitted as long as +** the number of digits after the decimal point is no less than +** either the number of digits in either input. +*/ +static void decimalMul(Decimal *pA, Decimal *pB){ + signed char *acc = 0; + int i, j, k; + int minFrac; + + if( pA==0 || pA->oom || pA->isNull + || pB==0 || pB->oom || pB->isNull + ){ + goto mul_end; + } + acc = sqlite3_malloc64( (sqlite3_int64)pA->nDigit + + (sqlite3_int64)pB->nDigit + 2 ); + if( acc==0 ){ + pA->oom = 1; + goto mul_end; + } + memset(acc, 0, pA->nDigit + pB->nDigit + 2); + minFrac = pA->nFrac; + if( pB->nFracnFrac; + for(i=pA->nDigit-1; i>=0; i--){ + signed char f = pA->a[i]; + int carry = 0, x; + for(j=pB->nDigit-1, k=i+j+3; j>=0; j--, k--){ + x = acc[k] + f*pB->a[j] + carry; + acc[k] = x%10; + carry = x/10; + } + x = acc[k] + carry; + acc[k] = x%10; + acc[k-1] += x/10; + } + sqlite3_free(pA->a); + pA->a = acc; + acc = 0; + pA->nDigit += pB->nDigit + 2; + pA->nFrac += pB->nFrac; + pA->sign ^= pB->sign; + while( pA->nFrac>minFrac && pA->a[pA->nDigit-1]==0 ){ + pA->nFrac--; + pA->nDigit--; + } + +mul_end: + sqlite3_free(acc); +} + +/* +** Create a new Decimal object that contains an integer power of 2. +*/ +static Decimal *decimalPow2(int N){ + Decimal *pA = 0; /* The result to be returned */ + Decimal *pX = 0; /* Multiplier */ + if( N<-20000 || N>20000 ) goto pow2_fault; + pA = decimalNewFromText("1.0", 3); + if( pA==0 || pA->oom ) goto pow2_fault; + if( N==0 ) return pA; + if( N>0 ){ + pX = decimalNewFromText("2.0", 3); + }else{ + N = -N; + pX = decimalNewFromText("0.5", 3); + } + if( pX==0 || pX->oom ) goto pow2_fault; + while( 1 /* Exit by break */ ){ + if( N & 1 ){ + decimalMul(pA, pX); + if( pA->oom ) goto pow2_fault; + } + N >>= 1; + if( N==0 ) break; + decimalMul(pX, pX); + } + decimal_free(pX); + return pA; + +pow2_fault: + decimal_free(pA); + decimal_free(pX); + return 0; +} + +/* +** Use an IEEE754 binary64 ("double") to generate a new Decimal object. +*/ +static Decimal *decimalFromDouble(double r){ + sqlite3_int64 m, a; + int e; + int isNeg; + Decimal *pA; + Decimal *pX; + char zNum[100]; + if( r<0.0 ){ + isNeg = 1; + r = -r; + }else{ + isNeg = 0; + } + memcpy(&a,&r,sizeof(a)); + if( a==0 || a==(sqlite3_int64)0x8000000000000000LL){ + e = 0; + m = 0; + }else{ + e = a>>52; + m = a & ((((sqlite3_int64)1)<<52)-1); + if( e==0 ){ + m <<= 1; + }else{ + m |= ((sqlite3_int64)1)<<52; + } + while( e<1075 && m>0 && (m&1)==0 ){ + m >>= 1; + e++; + } + if( isNeg ) m = -m; + e = e - 1075; + if( e>971 ){ + return 0; /* A NaN or an Infinity */ + } + } + + /* At this point m is the integer significand and e is the exponent */ + sqlite3_snprintf(sizeof(zNum), zNum, "%lld", m); + pA = decimalNewFromText(zNum, (int)strlen(zNum)); + pX = decimalPow2(e); + decimalMul(pA, pX); + decimal_free(pX); + return pA; +} + +/* +** SQL Function: decimal(X) +** OR: decimal_exp(X) +** +** Convert input X into decimal and then back into text. +** +** If X is originally a float, then a full decimal expansion of that floating +** point value is done. Or if X is an 8-byte blob, it is interpreted +** as a float and similarly expanded. +** +** The decimal_exp(X) function returns the result in exponential notation. +** decimal(X) returns a complete decimal, without the e+NNN at the end. +*/ +static void decimalFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + Decimal *p = decimal_new(context, argv[0], 0); + UNUSED_PARAMETER(argc); + if( p ){ + if( sqlite3_user_data(context)!=0 ){ + decimal_result_sci(context, p); + }else{ + decimal_result(context, p); + } + decimal_free(p); + } +} + +/* +** Compare text in decimal order. +*/ +static int decimalCollFunc( + void *notUsed, + int nKey1, const void *pKey1, + int nKey2, const void *pKey2 +){ + const unsigned char *zA = (const unsigned char*)pKey1; + const unsigned char *zB = (const unsigned char*)pKey2; + Decimal *pA = decimalNewFromText((const char*)zA, nKey1); + Decimal *pB = decimalNewFromText((const char*)zB, nKey2); + int rc; + UNUSED_PARAMETER(notUsed); + if( pA==0 || pB==0 ){ + rc = 0; + }else{ + rc = decimal_cmp(pA, pB); + } + decimal_free(pA); + decimal_free(pB); + return rc; +} + + +/* +** SQL Function: decimal_add(X, Y) +** decimal_sub(X, Y) +** +** Return the sum or difference of X and Y. +*/ +static void decimalAddFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + Decimal *pA = decimal_new(context, argv[0], 1); + Decimal *pB = decimal_new(context, argv[1], 1); + UNUSED_PARAMETER(argc); + decimal_add(pA, pB); + decimal_result(context, pA); + decimal_free(pA); + decimal_free(pB); +} +static void decimalSubFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + Decimal *pA = decimal_new(context, argv[0], 1); + Decimal *pB = decimal_new(context, argv[1], 1); + UNUSED_PARAMETER(argc); + if( pB ){ + pB->sign = !pB->sign; + decimal_add(pA, pB); + decimal_result(context, pA); + } + decimal_free(pA); + decimal_free(pB); +} + +/* Aggregate function: decimal_sum(X) +** +** Works like sum() except that it uses decimal arithmetic for unlimited +** precision. +*/ +static void decimalSumStep( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + Decimal *p; + Decimal *pArg; + UNUSED_PARAMETER(argc); + p = sqlite3_aggregate_context(context, sizeof(*p)); + if( p==0 ) return; + if( !p->isInit ){ + p->isInit = 1; + p->a = sqlite3_malloc(2); + if( p->a==0 ){ + p->oom = 1; + }else{ + p->a[0] = 0; + } + p->nDigit = 1; + p->nFrac = 0; + } + if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; + pArg = decimal_new(context, argv[0], 1); + decimal_add(p, pArg); + decimal_free(pArg); +} +static void decimalSumInverse( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + Decimal *p; + Decimal *pArg; + UNUSED_PARAMETER(argc); + p = sqlite3_aggregate_context(context, sizeof(*p)); + if( p==0 ) return; + if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; + pArg = decimal_new(context, argv[0], 1); + if( pArg ) pArg->sign = !pArg->sign; + decimal_add(p, pArg); + decimal_free(pArg); +} +static void decimalSumValue(sqlite3_context *context){ + Decimal *p = sqlite3_aggregate_context(context, 0); + if( p==0 ) return; + decimal_result(context, p); +} +static void decimalSumFinalize(sqlite3_context *context){ + Decimal *p = sqlite3_aggregate_context(context, 0); + if( p==0 ) return; + decimal_result(context, p); + decimal_clear(p); +} + +/* +** SQL Function: decimal_mul(X, Y) +** +** Return the product of X and Y. +*/ +static void decimalMulFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + Decimal *pA = decimal_new(context, argv[0], 1); + Decimal *pB = decimal_new(context, argv[1], 1); + UNUSED_PARAMETER(argc); + if( pA==0 || pA->oom || pA->isNull + || pB==0 || pB->oom || pB->isNull + ){ + goto mul_end; + } + decimalMul(pA, pB); + if( pA->oom ){ + goto mul_end; + } + decimal_result(context, pA); + +mul_end: + decimal_free(pA); + decimal_free(pB); +} + +/* +** SQL Function: decimal_pow2(N) +** +** Return the N-th power of 2. N must be an integer. +*/ +static void decimalPow2Func( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + UNUSED_PARAMETER(argc); + if( sqlite3_value_type(argv[0])==SQLITE_INTEGER ){ + Decimal *pA = decimalPow2(sqlite3_value_int(argv[0])); + decimal_result_sci(context, pA); + decimal_free(pA); + } +} + +#ifdef _WIN32 +__declspec(dllexport) +#endif +int sqlite3_decimal_init( + sqlite3 *db, + char **pzErrMsg, + const sqlite3_api_routines *pApi +){ + int rc = SQLITE_OK; + static const struct { + const char *zFuncName; + int nArg; + int iArg; + void (*xFunc)(sqlite3_context*,int,sqlite3_value**); + } aFunc[] = { + { "decimal", 1, 0, decimalFunc }, + { "decimal_exp", 1, 1, decimalFunc }, + { "decimal_cmp", 2, 0, decimalCmpFunc }, + { "decimal_add", 2, 0, decimalAddFunc }, + { "decimal_sub", 2, 0, decimalSubFunc }, + { "decimal_mul", 2, 0, decimalMulFunc }, + { "decimal_pow2", 1, 0, decimalPow2Func }, + }; + unsigned int i; + (void)pzErrMsg; /* Unused parameter */ + + SQLITE_EXTENSION_INIT2(pApi); + + for(i=0; i<(int)(sizeof(aFunc)/sizeof(aFunc[0])) && rc==SQLITE_OK; i++){ + rc = sqlite3_create_function(db, aFunc[i].zFuncName, aFunc[i].nArg, + SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC, + aFunc[i].iArg ? db : 0, aFunc[i].xFunc, 0, 0); + } + if( rc==SQLITE_OK ){ + rc = sqlite3_create_window_function(db, "decimal_sum", 1, + SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC, 0, + decimalSumStep, decimalSumFinalize, + decimalSumValue, decimalSumInverse, 0); + } + if( rc==SQLITE_OK ){ + rc = sqlite3_create_collation(db, "decimal", SQLITE_UTF8, + 0, decimalCollFunc); + } + return rc; +} diff --git a/ext/misc/eval.c b/ext/misc/eval.c index 71b6b69f20..d3849d6587 100644 --- a/ext/misc/eval.c +++ b/ext/misc/eval.c @@ -34,6 +34,7 @@ struct EvalResult { static int callback(void *pCtx, int argc, char **argv, char **colnames){ struct EvalResult *p = (struct EvalResult*)pCtx; int i; + if( argv==0 ) return 0; for(i=0; inAlloc<=0x7fffffff ? sqlite3_realloc(p->z, (int)p->nAlloc) : 0; + zNew = p->nAlloc<=0x7fffffff ? sqlite3_realloc64(p->z, p->nAlloc) : 0; if( zNew==0 ){ sqlite3_free(p->z); memset(p, 0, sizeof(*p)); @@ -112,10 +113,12 @@ int sqlite3_eval_init( int rc = SQLITE_OK; SQLITE_EXTENSION_INIT2(pApi); (void)pzErrMsg; /* Unused parameter */ - rc = sqlite3_create_function(db, "eval", 1, SQLITE_UTF8, 0, + rc = sqlite3_create_function(db, "eval", 1, + SQLITE_UTF8|SQLITE_DIRECTONLY, 0, sqlEvalFunc, 0, 0); if( rc==SQLITE_OK ){ - rc = sqlite3_create_function(db, "eval", 2, SQLITE_UTF8, 0, + rc = sqlite3_create_function(db, "eval", 2, + SQLITE_UTF8|SQLITE_DIRECTONLY, 0, sqlEvalFunc, 0, 0); } return rc; diff --git a/ext/misc/explain.c b/ext/misc/explain.c new file mode 100644 index 0000000000..726af76b96 --- /dev/null +++ b/ext/misc/explain.c @@ -0,0 +1,323 @@ +/* +** 2018-09-16 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** This file demonstrates an eponymous virtual table that returns the +** EXPLAIN output from an SQL statement. +** +** Usage example: +** +** .load ./explain +** SELECT p2 FROM explain('SELECT * FROM sqlite_schema') +** WHERE opcode='OpenRead'; +** +** This module was originally written to help simplify SQLite testing, +** by providing an easier means of verifying certain patterns in the +** generated bytecode. +*/ +#if !defined(SQLITEINT_H) +#include "sqlite3ext.h" +#endif +SQLITE_EXTENSION_INIT1 +#include +#include + +#ifndef SQLITE_OMIT_VIRTUALTABLE + +/* explain_vtab is a subclass of sqlite3_vtab which will +** serve as the underlying representation of a explain virtual table +*/ +typedef struct explain_vtab explain_vtab; +struct explain_vtab { + sqlite3_vtab base; /* Base class - must be first */ + sqlite3 *db; /* Database connection for this explain vtab */ +}; + +/* explain_cursor is a subclass of sqlite3_vtab_cursor which will +** serve as the underlying representation of a cursor that scans +** over rows of the result from an EXPLAIN operation. +*/ +typedef struct explain_cursor explain_cursor; +struct explain_cursor { + sqlite3_vtab_cursor base; /* Base class - must be first */ + sqlite3 *db; /* Database connection for this cursor */ + char *zSql; /* Value for the EXPLN_COLUMN_SQL column */ + sqlite3_stmt *pExplain; /* Statement being explained */ + int rc; /* Result of last sqlite3_step() on pExplain */ +}; + +/* +** The explainConnect() method is invoked to create a new +** explain_vtab that describes the explain virtual table. +** +** Think of this routine as the constructor for explain_vtab objects. +** +** All this routine needs to do is: +** +** (1) Allocate the explain_vtab object and initialize all fields. +** +** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the +** result set of queries against explain will look like. +*/ +static int explainConnect( + sqlite3 *db, + void *pAux, + int argc, const char *const*argv, + sqlite3_vtab **ppVtab, + char **pzErr +){ + explain_vtab *pNew; + int rc; + +/* Column numbers */ +#define EXPLN_COLUMN_ADDR 0 /* Instruction address */ +#define EXPLN_COLUMN_OPCODE 1 /* Opcode */ +#define EXPLN_COLUMN_P1 2 /* Operand 1 */ +#define EXPLN_COLUMN_P2 3 /* Operand 2 */ +#define EXPLN_COLUMN_P3 4 /* Operand 3 */ +#define EXPLN_COLUMN_P4 5 /* Operand 4 */ +#define EXPLN_COLUMN_P5 6 /* Operand 5 */ +#define EXPLN_COLUMN_COMMENT 7 /* Comment */ +#define EXPLN_COLUMN_SQL 8 /* SQL that is being explained */ + + + rc = sqlite3_declare_vtab(db, + "CREATE TABLE x(addr,opcode,p1,p2,p3,p4,p5,comment,sql HIDDEN)"); + if( rc==SQLITE_OK ){ + pNew = sqlite3_malloc( sizeof(*pNew) ); + *ppVtab = (sqlite3_vtab*)pNew; + if( pNew==0 ) return SQLITE_NOMEM; + memset(pNew, 0, sizeof(*pNew)); + pNew->db = db; + } + return rc; +} + +/* +** This method is the destructor for explain_cursor objects. +*/ +static int explainDisconnect(sqlite3_vtab *pVtab){ + sqlite3_free(pVtab); + return SQLITE_OK; +} + +/* +** Constructor for a new explain_cursor object. +*/ +static int explainOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){ + explain_cursor *pCur; + pCur = sqlite3_malloc( sizeof(*pCur) ); + if( pCur==0 ) return SQLITE_NOMEM; + memset(pCur, 0, sizeof(*pCur)); + pCur->db = ((explain_vtab*)p)->db; + *ppCursor = &pCur->base; + return SQLITE_OK; +} + +/* +** Destructor for a explain_cursor. +*/ +static int explainClose(sqlite3_vtab_cursor *cur){ + explain_cursor *pCur = (explain_cursor*)cur; + sqlite3_finalize(pCur->pExplain); + sqlite3_free(pCur->zSql); + sqlite3_free(pCur); + return SQLITE_OK; +} + + +/* +** Advance a explain_cursor to its next row of output. +*/ +static int explainNext(sqlite3_vtab_cursor *cur){ + explain_cursor *pCur = (explain_cursor*)cur; + pCur->rc = sqlite3_step(pCur->pExplain); + if( pCur->rc!=SQLITE_DONE && pCur->rc!=SQLITE_ROW ) return pCur->rc; + return SQLITE_OK; +} + +/* +** Return values of columns for the row at which the explain_cursor +** is currently pointing. +*/ +static int explainColumn( + sqlite3_vtab_cursor *cur, /* The cursor */ + sqlite3_context *ctx, /* First argument to sqlite3_result_...() */ + int i /* Which column to return */ +){ + explain_cursor *pCur = (explain_cursor*)cur; + if( i==EXPLN_COLUMN_SQL ){ + sqlite3_result_text(ctx, pCur->zSql, -1, SQLITE_TRANSIENT); + }else{ + sqlite3_result_value(ctx, sqlite3_column_value(pCur->pExplain, i)); + } + return SQLITE_OK; +} + +/* +** Return the rowid for the current row. In this implementation, the +** rowid is the same as the output value. +*/ +static int explainRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ + explain_cursor *pCur = (explain_cursor*)cur; + *pRowid = sqlite3_column_int64(pCur->pExplain, 0); + return SQLITE_OK; +} + +/* +** Return TRUE if the cursor has been moved off of the last +** row of output. +*/ +static int explainEof(sqlite3_vtab_cursor *cur){ + explain_cursor *pCur = (explain_cursor*)cur; + return pCur->rc!=SQLITE_ROW; +} + +/* +** This method is called to "rewind" the explain_cursor object back +** to the first row of output. This method is always called at least +** once prior to any call to explainColumn() or explainRowid() or +** explainEof(). +** +** The argv[0] is the SQL statement that is to be explained. +*/ +static int explainFilter( + sqlite3_vtab_cursor *pVtabCursor, + int idxNum, const char *idxStr, + int argc, sqlite3_value **argv +){ + explain_cursor *pCur = (explain_cursor *)pVtabCursor; + char *zSql = 0; + int rc; + sqlite3_finalize(pCur->pExplain); + pCur->pExplain = 0; + if( sqlite3_value_type(argv[0])!=SQLITE_TEXT ){ + pCur->rc = SQLITE_DONE; + return SQLITE_OK; + } + sqlite3_free(pCur->zSql); + pCur->zSql = sqlite3_mprintf("%s", sqlite3_value_text(argv[0])); + if( pCur->zSql ){ + zSql = sqlite3_mprintf("EXPLAIN %s", pCur->zSql); + } + if( zSql==0 ){ + rc = SQLITE_NOMEM; + }else{ + rc = sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pExplain, 0); + sqlite3_free(zSql); + } + if( rc ){ + sqlite3_finalize(pCur->pExplain); + pCur->pExplain = 0; + sqlite3_free(pCur->zSql); + pCur->zSql = 0; + }else{ + pCur->rc = sqlite3_step(pCur->pExplain); + rc = (pCur->rc==SQLITE_DONE || pCur->rc==SQLITE_ROW) ? SQLITE_OK : pCur->rc; + } + return rc; +} + +/* +** SQLite will invoke this method one or more times while planning a query +** that uses the explain virtual table. This routine needs to create +** a query plan for each invocation and compute an estimated cost for that +** plan. +*/ +static int explainBestIndex( + sqlite3_vtab *tab, + sqlite3_index_info *pIdxInfo +){ + int i; /* Loop counter */ + int idx = -1; /* Index of a usable == constraint against SQL */ + int unusable = 0; /* True if there are unusable constraints on SQL */ + + pIdxInfo->estimatedRows = 500; + for(i=0; inConstraint; i++){ + struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[i]; + if( p->iColumn!=EXPLN_COLUMN_SQL ) continue; + if( !p->usable ){ + unusable = 1; + }else if( p->op==SQLITE_INDEX_CONSTRAINT_EQ ){ + idx = i; + } + } + if( idx>=0 ){ + /* There exists a usable == constraint against the SQL column */ + pIdxInfo->estimatedCost = 10.0; + pIdxInfo->idxNum = 1; + pIdxInfo->aConstraintUsage[idx].argvIndex = 1; + pIdxInfo->aConstraintUsage[idx].omit = 1; + }else if( unusable ){ + /* There are unusable constraints against the SQL column. Do not allow + ** this plan to continue forward. */ + return SQLITE_CONSTRAINT; + } + return SQLITE_OK; +} + +/* +** This following structure defines all the methods for the +** explain virtual table. +*/ +static sqlite3_module explainModule = { + 0, /* iVersion */ + 0, /* xCreate */ + explainConnect, /* xConnect */ + explainBestIndex, /* xBestIndex */ + explainDisconnect, /* xDisconnect */ + 0, /* xDestroy */ + explainOpen, /* xOpen - open a cursor */ + explainClose, /* xClose - close a cursor */ + explainFilter, /* xFilter - configure scan constraints */ + explainNext, /* xNext - advance a cursor */ + explainEof, /* xEof - check for end of scan */ + explainColumn, /* xColumn - read data */ + explainRowid, /* xRowid - read data */ + 0, /* xUpdate */ + 0, /* xBegin */ + 0, /* xSync */ + 0, /* xCommit */ + 0, /* xRollback */ + 0, /* xFindMethod */ + 0, /* xRename */ + 0, /* xSavepoint */ + 0, /* xRelease */ + 0, /* xRollbackTo */ + 0, /* xShadowName */ + 0 /* xIntegrity */ +}; + +#endif /* SQLITE_OMIT_VIRTUALTABLE */ + +int sqlite3ExplainVtabInit(sqlite3 *db){ + int rc = SQLITE_OK; +#ifndef SQLITE_OMIT_VIRTUALTABLE + rc = sqlite3_create_module(db, "explain", &explainModule, 0); +#endif + return rc; +} + +#ifdef _WIN32 +__declspec(dllexport) +#endif +int sqlite3_explain_init( + sqlite3 *db, + char **pzErrMsg, + const sqlite3_api_routines *pApi +){ + int rc = SQLITE_OK; + SQLITE_EXTENSION_INIT2(pApi); +#ifndef SQLITE_OMIT_VIRTUALTABLE + rc = sqlite3ExplainVtabInit(db); +#endif + return rc; +} diff --git a/ext/misc/fileio.c b/ext/misc/fileio.c index fbe2d030c0..6cc2ae0085 100644 --- a/ext/misc/fileio.c +++ b/ext/misc/fileio.c @@ -11,11 +11,203 @@ ****************************************************************************** ** ** This SQLite extension implements SQL functions readfile() and -** writefile(). +** writefile(), and eponymous virtual type "fsdir". +** +** WRITEFILE(FILE, DATA [, MODE [, MTIME]]): +** +** If neither of the optional arguments is present, then this UDF +** function writes blob DATA to file FILE. If successful, the number +** of bytes written is returned. If an error occurs, NULL is returned. +** +** If the first option argument - MODE - is present, then it must +** be passed an integer value that corresponds to a POSIX mode +** value (file type + permissions, as returned in the stat.st_mode +** field by the stat() system call). Three types of files may +** be written/created: +** +** regular files: (mode & 0170000)==0100000 +** symbolic links: (mode & 0170000)==0120000 +** directories: (mode & 0170000)==0040000 +** +** For a directory, the DATA is ignored. For a symbolic link, it is +** interpreted as text and used as the target of the link. For a +** regular file, it is interpreted as a blob and written into the +** named file. Regardless of the type of file, its permissions are +** set to (mode & 0777) before returning. +** +** If the optional MTIME argument is present, then it is interpreted +** as an integer - the number of seconds since the unix epoch. The +** modification-time of the target file is set to this value before +** returning. +** +** If five or more arguments are passed to this function and an +** error is encountered, an exception is raised. +** +** READFILE(FILE): +** +** Read and return the contents of file FILE (type blob) from disk. +** +** FSDIR: +** +** Used as follows: +** +** SELECT * FROM fsdir($path [, $dir]); +** +** Parameter $path is an absolute or relative pathname. If the file that it +** refers to does not exist, it is an error. If the path refers to a regular +** file or symbolic link, it returns a single row. Or, if the path refers +** to a directory, it returns one row for the directory, and one row for each +** file within the hierarchy rooted at $path. +** +** Each row has the following columns: +** +** name: Path to file or directory (text value). +** mode: Value of stat.st_mode for directory entry (an integer). +** mtime: Value of stat.st_mtime for directory entry (an integer). +** data: For a regular file, a blob containing the file data. For a +** symlink, a text value containing the text of the link. For a +** directory, NULL. +** level: Directory hierarchy level. Topmost is 1. +** +** If a non-NULL value is specified for the optional $dir parameter and +** $path is a relative path, then $path is interpreted relative to $dir. +** And the paths returned in the "name" column of the table are also +** relative to directory $dir. +** +** Notes on building this extension for Windows: +** Unless linked statically with the SQLite library, a preprocessor +** symbol, FILEIO_WIN32_DLL, must be #define'd to create a stand-alone +** DLL form of this extension for WIN32. See its use below for details. */ #include "sqlite3ext.h" SQLITE_EXTENSION_INIT1 #include +#include +#include + +#include +#include +#include +#if !defined(_WIN32) && !defined(WIN32) +# include +# include +# include +# include +# define STRUCT_STAT struct stat +#else +# include "windirent.h" +# include +# define STRUCT_STAT struct _stat +# define chmod(path,mode) fileio_chmod(path,mode) +# define mkdir(path,mode) fileio_mkdir(path) +#endif +#include +#include + +/* When used as part of the CLI, the sqlite3_stdio.h module will have +** been included before this one. In that case use the sqlite3_stdio.h +** #defines. If not, create our own for fopen(). +*/ +#ifndef _SQLITE3_STDIO_H_ +# define sqlite3_fopen fopen +#endif + +/* +** Structure of the fsdir() table-valued function +*/ + /* 0 1 2 3 4 5 6 */ +#define FSDIR_SCHEMA "(name,mode,mtime,data,level,path HIDDEN,dir HIDDEN)" + +#define FSDIR_COLUMN_NAME 0 /* Name of the file */ +#define FSDIR_COLUMN_MODE 1 /* Access mode */ +#define FSDIR_COLUMN_MTIME 2 /* Last modification time */ +#define FSDIR_COLUMN_DATA 3 /* File content */ +#define FSDIR_COLUMN_LEVEL 4 /* Level. Topmost is 1 */ +#define FSDIR_COLUMN_PATH 5 /* Path to top of search */ +#define FSDIR_COLUMN_DIR 6 /* Path is relative to this directory */ + +/* +** UTF8 chmod() function for Windows +*/ +#if defined(_WIN32) || defined(WIN32) +static int fileio_chmod(const char *zPath, int pmode){ + sqlite3_int64 sz = strlen(zPath); + wchar_t *b1 = sqlite3_malloc64( (sz+1)*sizeof(b1[0]) ); + int rc; + if( b1==0 ) return -1; + sz = MultiByteToWideChar(CP_UTF8, 0, zPath, sz, b1, sz); + b1[sz] = 0; + rc = _wchmod(b1, pmode); + sqlite3_free(b1); + return rc; +} +#endif + +/* +** UTF8 mkdir() function for Windows +*/ +#if defined(_WIN32) || defined(WIN32) +static int fileio_mkdir(const char *zPath){ + sqlite3_int64 sz = strlen(zPath); + wchar_t *b1 = sqlite3_malloc64( (sz+1)*sizeof(b1[0]) ); + int rc; + if( b1==0 ) return -1; + sz = MultiByteToWideChar(CP_UTF8, 0, zPath, sz, b1, sz); + b1[sz] = 0; + rc = _wmkdir(b1); + sqlite3_free(b1); + return rc; +} +#endif + + +/* +** Set the result stored by context ctx to a blob containing the +** contents of file zName. Or, leave the result unchanged (NULL) +** if the file does not exist or is unreadable. +** +** If the file exceeds the SQLite blob size limit, through an +** SQLITE_TOOBIG error. +** +** Throw an SQLITE_IOERR if there are difficulties pulling the file +** off of disk. +*/ +static void readFileContents(sqlite3_context *ctx, const char *zName){ + FILE *in; + sqlite3_int64 nIn; + void *pBuf; + sqlite3 *db; + int mxBlob; + + in = sqlite3_fopen(zName, "rb"); + if( in==0 ){ + /* File does not exist or is unreadable. Leave the result set to NULL. */ + return; + } + fseek(in, 0, SEEK_END); + nIn = ftell(in); + rewind(in); + db = sqlite3_context_db_handle(ctx); + mxBlob = sqlite3_limit(db, SQLITE_LIMIT_LENGTH, -1); + if( nIn>mxBlob ){ + sqlite3_result_error_code(ctx, SQLITE_TOOBIG); + fclose(in); + return; + } + pBuf = sqlite3_malloc64( nIn ? nIn : 1 ); + if( pBuf==0 ){ + sqlite3_result_error_nomem(ctx); + fclose(in); + return; + } + if( nIn==(sqlite3_int64)fread(pBuf, 1, (size_t)nIn, in) ){ + sqlite3_result_blob64(ctx, pBuf, nIn, sqlite3_free); + }else{ + sqlite3_result_error_code(ctx, SQLITE_IOERR); + sqlite3_free(pBuf); + } + fclose(in); +} /* ** Implementation of the "readfile(X)" SQL function. The entire content @@ -28,56 +220,880 @@ static void readfileFunc( sqlite3_value **argv ){ const char *zName; - FILE *in; - long nIn; - void *pBuf; - + (void)(argc); /* Unused parameter */ zName = (const char*)sqlite3_value_text(argv[0]); if( zName==0 ) return; - in = fopen(zName, "rb"); - if( in==0 ) return; - fseek(in, 0, SEEK_END); - nIn = ftell(in); - rewind(in); - pBuf = sqlite3_malloc( nIn ); - if( pBuf && 1==fread(pBuf, nIn, 1, in) ){ - sqlite3_result_blob(context, pBuf, nIn, sqlite3_free); + readFileContents(context, zName); +} + +/* +** Set the error message contained in context ctx to the results of +** vprintf(zFmt, ...). +*/ +static void ctxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){ + char *zMsg = 0; + va_list ap; + va_start(ap, zFmt); + zMsg = sqlite3_vmprintf(zFmt, ap); + sqlite3_result_error(ctx, zMsg, -1); + sqlite3_free(zMsg); + va_end(ap); +} + +#if defined(_WIN32) +/* +** This function is designed to convert a Win32 FILETIME structure into the +** number of seconds since the Unix Epoch (1970-01-01 00:00:00 UTC). +*/ +static sqlite3_uint64 fileTimeToUnixTime( + LPFILETIME pFileTime +){ + SYSTEMTIME epochSystemTime; + ULARGE_INTEGER epochIntervals; + FILETIME epochFileTime; + ULARGE_INTEGER fileIntervals; + + memset(&epochSystemTime, 0, sizeof(SYSTEMTIME)); + epochSystemTime.wYear = 1970; + epochSystemTime.wMonth = 1; + epochSystemTime.wDay = 1; + SystemTimeToFileTime(&epochSystemTime, &epochFileTime); + epochIntervals.LowPart = epochFileTime.dwLowDateTime; + epochIntervals.HighPart = epochFileTime.dwHighDateTime; + + fileIntervals.LowPart = pFileTime->dwLowDateTime; + fileIntervals.HighPart = pFileTime->dwHighDateTime; + + return (fileIntervals.QuadPart - epochIntervals.QuadPart) / 10000000; +} + + +#if defined(FILEIO_WIN32_DLL) && (defined(_WIN32) || defined(WIN32)) +# /* To allow a standalone DLL, use this next replacement function: */ +# undef sqlite3_win32_utf8_to_unicode +# define sqlite3_win32_utf8_to_unicode utf8_to_utf16 +# +LPWSTR utf8_to_utf16(const char *z){ + int nAllot = MultiByteToWideChar(CP_UTF8, 0, z, -1, NULL, 0); + LPWSTR rv = sqlite3_malloc(nAllot * sizeof(WCHAR)); + if( rv!=0 && 0 < MultiByteToWideChar(CP_UTF8, 0, z, -1, rv, nAllot) ) + return rv; + sqlite3_free(rv); + return 0; +} +#endif + +/* +** This function attempts to normalize the time values found in the stat() +** buffer to UTC. This is necessary on Win32, where the runtime library +** appears to return these values as local times. +*/ +static void statTimesToUtc( + const char *zPath, + STRUCT_STAT *pStatBuf +){ + HANDLE hFindFile; + WIN32_FIND_DATAW fd; + LPWSTR zUnicodeName; + extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*); + zUnicodeName = sqlite3_win32_utf8_to_unicode(zPath); + if( zUnicodeName ){ + memset(&fd, 0, sizeof(WIN32_FIND_DATAW)); + hFindFile = FindFirstFileW(zUnicodeName, &fd); + if( hFindFile!=NULL ){ + pStatBuf->st_ctime = (time_t)fileTimeToUnixTime(&fd.ftCreationTime); + pStatBuf->st_atime = (time_t)fileTimeToUnixTime(&fd.ftLastAccessTime); + pStatBuf->st_mtime = (time_t)fileTimeToUnixTime(&fd.ftLastWriteTime); + FindClose(hFindFile); + } + sqlite3_free(zUnicodeName); + } +} +#endif + +/* +** This function is used in place of stat(). On Windows, special handling +** is required in order for the included time to be returned as UTC. On all +** other systems, this function simply calls stat(). +*/ +static int fileStat( + const char *zPath, + STRUCT_STAT *pStatBuf +){ +#if defined(_WIN32) + sqlite3_int64 sz = strlen(zPath); + wchar_t *b1 = sqlite3_malloc64( (sz+1)*sizeof(b1[0]) ); + int rc; + if( b1==0 ) return 1; + sz = MultiByteToWideChar(CP_UTF8, 0, zPath, sz, b1, sz); + b1[sz] = 0; + rc = _wstat(b1, pStatBuf); + if( rc==0 ) statTimesToUtc(zPath, pStatBuf); + sqlite3_free(b1); + return rc; +#else + return stat(zPath, pStatBuf); +#endif +} + +/* +** This function is used in place of lstat(). On Windows, special handling +** is required in order for the included time to be returned as UTC. On all +** other systems, this function simply calls lstat(). +*/ +static int fileLinkStat( + const char *zPath, + STRUCT_STAT *pStatBuf +){ +#if defined(_WIN32) + return fileStat(zPath, pStatBuf); +#else + return lstat(zPath, pStatBuf); +#endif +} + +/* +** Argument zFile is the name of a file that will be created and/or written +** by SQL function writefile(). This function ensures that the directory +** zFile will be written to exists, creating it if required. The permissions +** for any path components created by this function are set in accordance +** with the current umask. +** +** If an OOM condition is encountered, SQLITE_NOMEM is returned. Otherwise, +** SQLITE_OK is returned if the directory is successfully created, or +** SQLITE_ERROR otherwise. +*/ +static int makeDirectory( + const char *zFile +){ + char *zCopy = sqlite3_mprintf("%s", zFile); + int rc = SQLITE_OK; + + if( zCopy==0 ){ + rc = SQLITE_NOMEM; }else{ - sqlite3_free(pBuf); + int nCopy = (int)strlen(zCopy); + int i = 1; + + while( rc==SQLITE_OK ){ + STRUCT_STAT sStat; + int rc2; + + for(; zCopy[i]!='/' && i=0 ){ +#if defined(_WIN32) +#if !SQLITE_OS_WINRT + /* Windows */ + FILETIME lastAccess; + FILETIME lastWrite; + SYSTEMTIME currentTime; + LONGLONG intervals; + HANDLE hFile; + LPWSTR zUnicodeName; + extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*); + + GetSystemTime(¤tTime); + SystemTimeToFileTime(¤tTime, &lastAccess); + intervals = (mtime*10000000) + 116444736000000000; + lastWrite.dwLowDateTime = (DWORD)intervals; + lastWrite.dwHighDateTime = intervals >> 32; + zUnicodeName = sqlite3_win32_utf8_to_unicode(zFile); + if( zUnicodeName==0 ){ + return 1; + } + hFile = CreateFileW( + zUnicodeName, FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING, + FILE_FLAG_BACKUP_SEMANTICS, NULL + ); + sqlite3_free(zUnicodeName); + if( hFile!=INVALID_HANDLE_VALUE ){ + BOOL bResult = SetFileTime(hFile, NULL, &lastAccess, &lastWrite); + CloseHandle(hFile); + return !bResult; + }else{ + return 1; + } +#endif +#elif defined(AT_FDCWD) && 0 /* utimensat() is not universally available */ + /* Recent unix */ + struct timespec times[2]; + times[0].tv_nsec = times[1].tv_nsec = 0; + times[0].tv_sec = time(0); + times[1].tv_sec = mtime; + if( utimensat(AT_FDCWD, zFile, times, AT_SYMLINK_NOFOLLOW) ){ + return 1; + } +#else + /* Legacy unix. + ** + ** Do not use utimes() on a symbolic link - it sees through the link and + ** modifies the timestamps on the target. Or fails if the target does + ** not exist. */ + if( 0==S_ISLNK(mode) ){ + struct timeval times[2]; + times[0].tv_usec = times[1].tv_usec = 0; + times[0].tv_sec = time(0); + times[1].tv_sec = mtime; + if( utimes(zFile, times) ){ + return 1; + } + } +#endif + } + + return 0; } /* -** Implementation of the "writefile(X,Y)" SQL function. The argument Y -** is written into file X. The number of bytes written is returned. Or -** NULL is returned if something goes wrong, such as being unable to open -** file X for writing. +** Implementation of the "writefile(W,X[,Y[,Z]]])" SQL function. +** Refer to header comments at the top of this file for details. */ static void writefileFunc( sqlite3_context *context, int argc, sqlite3_value **argv ){ - FILE *out; - const char *z; - sqlite3_int64 rc; const char *zFile; + mode_t mode = 0; + int res; + sqlite3_int64 mtime = -1; + + if( argc<2 || argc>4 ){ + sqlite3_result_error(context, + "wrong number of arguments to function writefile()", -1 + ); + return; + } zFile = (const char*)sqlite3_value_text(argv[0]); if( zFile==0 ) return; - out = fopen(zFile, "wb"); - if( out==0 ) return; - z = (const char*)sqlite3_value_blob(argv[1]); - if( z==0 ){ - rc = 0; + if( argc>=3 ){ + mode = (mode_t)sqlite3_value_int(argv[2]); + } + if( argc==4 ){ + mtime = sqlite3_value_int64(argv[3]); + } + + res = writeFile(context, zFile, argv[1], mode, mtime); + if( res==1 && errno==ENOENT ){ + if( makeDirectory(zFile)==SQLITE_OK ){ + res = writeFile(context, zFile, argv[1], mode, mtime); + } + } + + if( argc>2 && res!=0 ){ + if( S_ISLNK(mode) ){ + ctxErrorMsg(context, "failed to create symlink: %s", zFile); + }else if( S_ISDIR(mode) ){ + ctxErrorMsg(context, "failed to create directory: %s", zFile); + }else{ + ctxErrorMsg(context, "failed to write file: %s", zFile); + } + } +} + +/* +** SQL function: lsmode(MODE) +** +** Given a numberic st_mode from stat(), convert it into a human-readable +** text string in the style of "ls -l". +*/ +static void lsModeFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + int i; + int iMode = sqlite3_value_int(argv[0]); + char z[16]; + (void)argc; + if( S_ISLNK(iMode) ){ + z[0] = 'l'; + }else if( S_ISREG(iMode) ){ + z[0] = '-'; + }else if( S_ISDIR(iMode) ){ + z[0] = 'd'; }else{ - rc = fwrite(z, 1, sqlite3_value_bytes(argv[1]), out); + z[0] = '?'; + } + for(i=0; i<3; i++){ + int m = (iMode >> ((2-i)*3)); + char *a = &z[1 + i*3]; + a[0] = (m & 0x4) ? 'r' : '-'; + a[1] = (m & 0x2) ? 'w' : '-'; + a[2] = (m & 0x1) ? 'x' : '-'; + } + z[10] = '\0'; + sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT); +} + +#ifndef SQLITE_OMIT_VIRTUALTABLE + +/* +** Cursor type for recursively iterating through a directory structure. +*/ +typedef struct fsdir_cursor fsdir_cursor; +typedef struct FsdirLevel FsdirLevel; + +struct FsdirLevel { + DIR *pDir; /* From opendir() */ + char *zDir; /* Name of directory (nul-terminated) */ +}; + +struct fsdir_cursor { + sqlite3_vtab_cursor base; /* Base class - must be first */ + + int nLvl; /* Number of entries in aLvl[] array */ + int mxLvl; /* Maximum level */ + int iLvl; /* Index of current entry */ + FsdirLevel *aLvl; /* Hierarchy of directories being traversed */ + + const char *zBase; + int nBase; + + STRUCT_STAT sStat; /* Current lstat() results */ + char *zPath; /* Path to current entry */ + sqlite3_int64 iRowid; /* Current rowid */ +}; + +typedef struct fsdir_tab fsdir_tab; +struct fsdir_tab { + sqlite3_vtab base; /* Base class - must be first */ +}; + +/* +** Construct a new fsdir virtual table object. +*/ +static int fsdirConnect( + sqlite3 *db, + void *pAux, + int argc, const char *const*argv, + sqlite3_vtab **ppVtab, + char **pzErr +){ + fsdir_tab *pNew = 0; + int rc; + (void)pAux; + (void)argc; + (void)argv; + (void)pzErr; + rc = sqlite3_declare_vtab(db, "CREATE TABLE x" FSDIR_SCHEMA); + if( rc==SQLITE_OK ){ + pNew = (fsdir_tab*)sqlite3_malloc( sizeof(*pNew) ); + if( pNew==0 ) return SQLITE_NOMEM; + memset(pNew, 0, sizeof(*pNew)); + sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY); + } + *ppVtab = (sqlite3_vtab*)pNew; + return rc; +} + +/* +** This method is the destructor for fsdir vtab objects. +*/ +static int fsdirDisconnect(sqlite3_vtab *pVtab){ + sqlite3_free(pVtab); + return SQLITE_OK; +} + +/* +** Constructor for a new fsdir_cursor object. +*/ +static int fsdirOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){ + fsdir_cursor *pCur; + (void)p; + pCur = sqlite3_malloc( sizeof(*pCur) ); + if( pCur==0 ) return SQLITE_NOMEM; + memset(pCur, 0, sizeof(*pCur)); + pCur->iLvl = -1; + *ppCursor = &pCur->base; + return SQLITE_OK; +} + +/* +** Reset a cursor back to the state it was in when first returned +** by fsdirOpen(). +*/ +static void fsdirResetCursor(fsdir_cursor *pCur){ + int i; + for(i=0; i<=pCur->iLvl; i++){ + FsdirLevel *pLvl = &pCur->aLvl[i]; + if( pLvl->pDir ) closedir(pLvl->pDir); + sqlite3_free(pLvl->zDir); } - fclose(out); - sqlite3_result_int64(context, rc); + sqlite3_free(pCur->zPath); + sqlite3_free(pCur->aLvl); + pCur->aLvl = 0; + pCur->zPath = 0; + pCur->zBase = 0; + pCur->nBase = 0; + pCur->nLvl = 0; + pCur->iLvl = -1; + pCur->iRowid = 1; } +/* +** Destructor for an fsdir_cursor. +*/ +static int fsdirClose(sqlite3_vtab_cursor *cur){ + fsdir_cursor *pCur = (fsdir_cursor*)cur; + + fsdirResetCursor(pCur); + sqlite3_free(pCur); + return SQLITE_OK; +} + +/* +** Set the error message for the virtual table associated with cursor +** pCur to the results of vprintf(zFmt, ...). +*/ +static void fsdirSetErrmsg(fsdir_cursor *pCur, const char *zFmt, ...){ + va_list ap; + va_start(ap, zFmt); + pCur->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap); + va_end(ap); +} + + +/* +** Advance an fsdir_cursor to its next row of output. +*/ +static int fsdirNext(sqlite3_vtab_cursor *cur){ + fsdir_cursor *pCur = (fsdir_cursor*)cur; + mode_t m = pCur->sStat.st_mode; + + pCur->iRowid++; + if( S_ISDIR(m) && pCur->iLvl+3mxLvl ){ + /* Descend into this directory */ + int iNew = pCur->iLvl + 1; + FsdirLevel *pLvl; + if( iNew>=pCur->nLvl ){ + int nNew = iNew+1; + sqlite3_int64 nByte = nNew*sizeof(FsdirLevel); + FsdirLevel *aNew = (FsdirLevel*)sqlite3_realloc64(pCur->aLvl, nByte); + if( aNew==0 ) return SQLITE_NOMEM; + memset(&aNew[pCur->nLvl], 0, sizeof(FsdirLevel)*(nNew-pCur->nLvl)); + pCur->aLvl = aNew; + pCur->nLvl = nNew; + } + pCur->iLvl = iNew; + pLvl = &pCur->aLvl[iNew]; + + pLvl->zDir = pCur->zPath; + pCur->zPath = 0; + pLvl->pDir = opendir(pLvl->zDir); + if( pLvl->pDir==0 ){ + fsdirSetErrmsg(pCur, "cannot read directory: %s", pLvl->zDir); + return SQLITE_ERROR; + } + } + + while( pCur->iLvl>=0 ){ + FsdirLevel *pLvl = &pCur->aLvl[pCur->iLvl]; + struct dirent *pEntry = readdir(pLvl->pDir); + if( pEntry ){ + if( pEntry->d_name[0]=='.' ){ + if( pEntry->d_name[1]=='.' && pEntry->d_name[2]=='\0' ) continue; + if( pEntry->d_name[1]=='\0' ) continue; + } + sqlite3_free(pCur->zPath); + pCur->zPath = sqlite3_mprintf("%s/%s", pLvl->zDir, pEntry->d_name); + if( pCur->zPath==0 ) return SQLITE_NOMEM; + if( fileLinkStat(pCur->zPath, &pCur->sStat) ){ + fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath); + return SQLITE_ERROR; + } + return SQLITE_OK; + } + closedir(pLvl->pDir); + sqlite3_free(pLvl->zDir); + pLvl->pDir = 0; + pLvl->zDir = 0; + pCur->iLvl--; + } + + /* EOF */ + sqlite3_free(pCur->zPath); + pCur->zPath = 0; + return SQLITE_OK; +} + +/* +** Return values of columns for the row at which the series_cursor +** is currently pointing. +*/ +static int fsdirColumn( + sqlite3_vtab_cursor *cur, /* The cursor */ + sqlite3_context *ctx, /* First argument to sqlite3_result_...() */ + int i /* Which column to return */ +){ + fsdir_cursor *pCur = (fsdir_cursor*)cur; + switch( i ){ + case FSDIR_COLUMN_NAME: { + sqlite3_result_text(ctx, &pCur->zPath[pCur->nBase], -1, SQLITE_TRANSIENT); + break; + } + + case FSDIR_COLUMN_MODE: + sqlite3_result_int64(ctx, pCur->sStat.st_mode); + break; + + case FSDIR_COLUMN_MTIME: + sqlite3_result_int64(ctx, pCur->sStat.st_mtime); + break; + + case FSDIR_COLUMN_DATA: { + mode_t m = pCur->sStat.st_mode; + if( S_ISDIR(m) ){ + sqlite3_result_null(ctx); +#if !defined(_WIN32) && !defined(WIN32) + }else if( S_ISLNK(m) ){ + char aStatic[64]; + char *aBuf = aStatic; + sqlite3_int64 nBuf = 64; + int n; + + while( 1 ){ + n = readlink(pCur->zPath, aBuf, nBuf); + if( nzPath); + } + break; + } + case FSDIR_COLUMN_LEVEL: + sqlite3_result_int(ctx, pCur->iLvl+2); + break; + case FSDIR_COLUMN_PATH: + default: { + /* The FSDIR_COLUMN_PATH and FSDIR_COLUMN_DIR are input parameters. + ** always return their values as NULL */ + break; + } + } + return SQLITE_OK; +} + +/* +** Return the rowid for the current row. In this implementation, the +** first row returned is assigned rowid value 1, and each subsequent +** row a value 1 more than that of the previous. +*/ +static int fsdirRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ + fsdir_cursor *pCur = (fsdir_cursor*)cur; + *pRowid = pCur->iRowid; + return SQLITE_OK; +} + +/* +** Return TRUE if the cursor has been moved off of the last +** row of output. +*/ +static int fsdirEof(sqlite3_vtab_cursor *cur){ + fsdir_cursor *pCur = (fsdir_cursor*)cur; + return (pCur->zPath==0); +} + +/* +** xFilter callback. +** +** idxNum bit Meaning +** 0x01 PATH=N +** 0x02 DIR=N +** 0x04 LEVEL0 ); + zDir = (const char*)sqlite3_value_text(argv[0]); + if( zDir==0 ){ + fsdirSetErrmsg(pCur, "table function fsdir requires a non-NULL argument"); + return SQLITE_ERROR; + } + i = 1; + if( (idxNum & 0x02)!=0 ){ + assert( argc>i ); + pCur->zBase = (const char*)sqlite3_value_text(argv[i++]); + } + if( (idxNum & 0x0c)!=0 ){ + assert( argc>i ); + pCur->mxLvl = sqlite3_value_int(argv[i++]); + if( idxNum & 0x08 ) pCur->mxLvl++; + if( pCur->mxLvl<=0 ) pCur->mxLvl = 1000000000; + }else{ + pCur->mxLvl = 1000000000; + } + if( pCur->zBase ){ + pCur->nBase = (int)strlen(pCur->zBase)+1; + pCur->zPath = sqlite3_mprintf("%s/%s", pCur->zBase, zDir); + }else{ + pCur->zPath = sqlite3_mprintf("%s", zDir); + } + + if( pCur->zPath==0 ){ + return SQLITE_NOMEM; + } + if( fileLinkStat(pCur->zPath, &pCur->sStat) ){ + fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath); + return SQLITE_ERROR; + } + + return SQLITE_OK; +} + +/* +** SQLite will invoke this method one or more times while planning a query +** that uses the generate_series virtual table. This routine needs to create +** a query plan for each invocation and compute an estimated cost for that +** plan. +** +** In this implementation idxNum is used to represent the +** query plan. idxStr is unused. +** +** The query plan is represented by bits in idxNum: +** +** 0x01 The path value is supplied by argv[0] +** 0x02 dir is in argv[1] +** 0x04 maxdepth is in argv[1] or [2] +*/ +static int fsdirBestIndex( + sqlite3_vtab *tab, + sqlite3_index_info *pIdxInfo +){ + int i; /* Loop over constraints */ + int idxPath = -1; /* Index in pIdxInfo->aConstraint of PATH= */ + int idxDir = -1; /* Index in pIdxInfo->aConstraint of DIR= */ + int idxLevel = -1; /* Index in pIdxInfo->aConstraint of LEVEL< or <= */ + int idxLevelEQ = 0; /* 0x08 for LEVEL<= or LEVEL=. 0x04 for LEVEL< */ + int omitLevel = 0; /* omit the LEVEL constraint */ + int seenPath = 0; /* True if an unusable PATH= constraint is seen */ + int seenDir = 0; /* True if an unusable DIR= constraint is seen */ + const struct sqlite3_index_constraint *pConstraint; + + (void)tab; + pConstraint = pIdxInfo->aConstraint; + for(i=0; inConstraint; i++, pConstraint++){ + if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){ + switch( pConstraint->iColumn ){ + case FSDIR_COLUMN_PATH: { + if( pConstraint->usable ){ + idxPath = i; + seenPath = 0; + }else if( idxPath<0 ){ + seenPath = 1; + } + break; + } + case FSDIR_COLUMN_DIR: { + if( pConstraint->usable ){ + idxDir = i; + seenDir = 0; + }else if( idxDir<0 ){ + seenDir = 1; + } + break; + } + case FSDIR_COLUMN_LEVEL: { + if( pConstraint->usable && idxLevel<0 ){ + idxLevel = i; + idxLevelEQ = 0x08; + omitLevel = 0; + } + break; + } + } + }else + if( pConstraint->iColumn==FSDIR_COLUMN_LEVEL + && pConstraint->usable + && idxLevel<0 + ){ + if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_LE ){ + idxLevel = i; + idxLevelEQ = 0x08; + omitLevel = 1; + }else if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_LT ){ + idxLevel = i; + idxLevelEQ = 0x04; + omitLevel = 1; + } + } + } + if( seenPath || seenDir ){ + /* If input parameters are unusable, disallow this plan */ + return SQLITE_CONSTRAINT; + } + + if( idxPath<0 ){ + pIdxInfo->idxNum = 0; + /* The pIdxInfo->estimatedCost should have been initialized to a huge + ** number. Leave it unchanged. */ + pIdxInfo->estimatedRows = 0x7fffffff; + }else{ + pIdxInfo->aConstraintUsage[idxPath].omit = 1; + pIdxInfo->aConstraintUsage[idxPath].argvIndex = 1; + pIdxInfo->idxNum = 0x01; + pIdxInfo->estimatedCost = 1.0e9; + i = 2; + if( idxDir>=0 ){ + pIdxInfo->aConstraintUsage[idxDir].omit = 1; + pIdxInfo->aConstraintUsage[idxDir].argvIndex = i++; + pIdxInfo->idxNum |= 0x02; + pIdxInfo->estimatedCost /= 1.0e4; + } + if( idxLevel>=0 ){ + pIdxInfo->aConstraintUsage[idxLevel].omit = omitLevel; + pIdxInfo->aConstraintUsage[idxLevel].argvIndex = i++; + pIdxInfo->idxNum |= idxLevelEQ; + pIdxInfo->estimatedCost /= 1.0e4; + } + } + + return SQLITE_OK; +} + +/* +** Register the "fsdir" virtual table. +*/ +static int fsdirRegister(sqlite3 *db){ + static sqlite3_module fsdirModule = { + 0, /* iVersion */ + 0, /* xCreate */ + fsdirConnect, /* xConnect */ + fsdirBestIndex, /* xBestIndex */ + fsdirDisconnect, /* xDisconnect */ + 0, /* xDestroy */ + fsdirOpen, /* xOpen - open a cursor */ + fsdirClose, /* xClose - close a cursor */ + fsdirFilter, /* xFilter - configure scan constraints */ + fsdirNext, /* xNext - advance a cursor */ + fsdirEof, /* xEof - check for end of scan */ + fsdirColumn, /* xColumn - read data */ + fsdirRowid, /* xRowid - read data */ + 0, /* xUpdate */ + 0, /* xBegin */ + 0, /* xSync */ + 0, /* xCommit */ + 0, /* xRollback */ + 0, /* xFindMethod */ + 0, /* xRename */ + 0, /* xSavepoint */ + 0, /* xRelease */ + 0, /* xRollbackTo */ + 0, /* xShadowName */ + 0 /* xIntegrity */ + }; + + int rc = sqlite3_create_module(db, "fsdir", &fsdirModule, 0); + return rc; +} +#else /* SQLITE_OMIT_VIRTUALTABLE */ +# define fsdirRegister(x) SQLITE_OK +#endif #ifdef _WIN32 __declspec(dllexport) @@ -90,11 +1106,28 @@ int sqlite3_fileio_init( int rc = SQLITE_OK; SQLITE_EXTENSION_INIT2(pApi); (void)pzErrMsg; /* Unused parameter */ - rc = sqlite3_create_function(db, "readfile", 1, SQLITE_UTF8, 0, + rc = sqlite3_create_function(db, "readfile", 1, + SQLITE_UTF8|SQLITE_DIRECTONLY, 0, readfileFunc, 0, 0); if( rc==SQLITE_OK ){ - rc = sqlite3_create_function(db, "writefile", 2, SQLITE_UTF8, 0, + rc = sqlite3_create_function(db, "writefile", -1, + SQLITE_UTF8|SQLITE_DIRECTONLY, 0, writefileFunc, 0, 0); } + if( rc==SQLITE_OK ){ + rc = sqlite3_create_function(db, "lsmode", 1, SQLITE_UTF8, 0, + lsModeFunc, 0, 0); + } + if( rc==SQLITE_OK ){ + rc = fsdirRegister(db); + } return rc; } + +#if defined(FILEIO_WIN32_DLL) && (defined(_WIN32) || defined(WIN32)) +/* To allow a standalone DLL, make test_windirent.c use the same + * redefined SQLite API calls as the above extension code does. + * Just pull in this .c to accomplish this. As a beneficial side + * effect, this extension becomes a single translation unit. */ +# include "test_windirent.c" +#endif diff --git a/ext/misc/fossildelta.c b/ext/misc/fossildelta.c new file mode 100644 index 0000000000..d24a87700e --- /dev/null +++ b/ext/misc/fossildelta.c @@ -0,0 +1,1107 @@ +/* +** 2019-02-19 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +** This SQLite extension implements the delta functions used by the RBU +** extension. Three scalar functions and one table-valued function are +** implemented here: +** +** delta_apply(X,D) -- apply delta D to file X and return the result +** delta_create(X,Y) -- compute and return a delta that carries X into Y +** delta_output_size(D) -- blob size in bytes output from applying delta D +** delta_parse(D) -- returns rows describing delta D +** +** The delta format is the Fossil delta format, described in a comment +** on the delete_create() function implementation below, and also at +** +** https://fossil-scm.org/fossil/doc/trunk/www/delta_format.wiki +** +** This delta format is used by the RBU extension, which is the main +** reason that these routines are included in the extension library. +** RBU does not use this extension directly. Rather, this extension is +** provided as a convenience to developers who want to analyze RBU files +** that contain deltas. +*/ +#include +#include +#include +#include "sqlite3ext.h" +SQLITE_EXTENSION_INIT1 + +#ifndef SQLITE_AMALGAMATION +/* +** The "u32" type must be an unsigned 32-bit integer. Adjust this +*/ +typedef unsigned int u32; + +/* +** Must be a 16-bit value +*/ +typedef short int s16; +typedef unsigned short int u16; + +#endif /* SQLITE_AMALGAMATION */ + + +/* +** The width of a hash window in bytes. The algorithm only works if this +** is a power of 2. +*/ +#define NHASH 16 + +/* +** The current state of the rolling hash. +** +** z[] holds the values that have been hashed. z[] is a circular buffer. +** z[i] is the first entry and z[(i+NHASH-1)%NHASH] is the last entry of +** the window. +** +** Hash.a is the sum of all elements of hash.z[]. Hash.b is a weighted +** sum. Hash.b is z[i]*NHASH + z[i+1]*(NHASH-1) + ... + z[i+NHASH-1]*1. +** (Each index for z[] should be module NHASH, of course. The %NHASH operator +** is omitted in the prior expression for brevity.) +*/ +typedef struct hash hash; +struct hash { + u16 a, b; /* Hash values */ + u16 i; /* Start of the hash window */ + char z[NHASH]; /* The values that have been hashed */ +}; + +/* +** Initialize the rolling hash using the first NHASH characters of z[] +*/ +static void hash_init(hash *pHash, const char *z){ + u16 a, b, i; + a = b = z[0]; + for(i=1; iz, z, NHASH); + pHash->a = a & 0xffff; + pHash->b = b & 0xffff; + pHash->i = 0; +} + +/* +** Advance the rolling hash by a single character "c" +*/ +static void hash_next(hash *pHash, int c){ + u16 old = pHash->z[pHash->i]; + pHash->z[pHash->i] = c; + pHash->i = (pHash->i+1)&(NHASH-1); + pHash->a = pHash->a - old + c; + pHash->b = pHash->b - NHASH*old + pHash->a; +} + +/* +** Return a 32-bit hash value +*/ +static u32 hash_32bit(hash *pHash){ + return (pHash->a & 0xffff) | (((u32)(pHash->b & 0xffff))<<16); +} + +/* +** Compute a hash on NHASH bytes. +** +** This routine is intended to be equivalent to: +** hash h; +** hash_init(&h, zInput); +** return hash_32bit(&h); +*/ +static u32 hash_once(const char *z){ + u16 a, b, i; + a = b = z[0]; + for(i=1; i0; i++, v>>=6){ + zBuf[i] = zDigits[v&0x3f]; + } + for(j=i-1; j>=0; j--){ + *(*pz)++ = zBuf[j]; + } +} + +/* +** Read bytes from *pz and convert them into a positive integer. When +** finished, leave *pz pointing to the first character past the end of +** the integer. The *pLen parameter holds the length of the string +** in *pz and is decremented once for each character in the integer. +*/ +static unsigned int deltaGetInt(const char **pz, int *pLen){ + static const signed char zValue[] = { + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, + -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, + 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, 36, + -1, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, + 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, -1, -1, -1, 63, -1, + }; + unsigned int v = 0; + int c; + unsigned char *z = (unsigned char*)*pz; + unsigned char *zStart = z; + while( (c = zValue[0x7f&*(z++)])>=0 ){ + v = (v<<6) + c; + } + z--; + *pLen -= z - zStart; + *pz = (char*)z; + return v; +} + +/* +** Return the number digits in the base-64 representation of a positive integer +*/ +static int digit_count(int v){ + unsigned int i, x; + for(i=1, x=64; v>=x; i++, x <<= 6){} + return i; +} + +#ifdef __GNUC__ +# define GCC_VERSION (__GNUC__*1000000+__GNUC_MINOR__*1000+__GNUC_PATCHLEVEL__) +#else +# define GCC_VERSION 0 +#endif + +/* +** Compute a 32-bit big-endian checksum on the N-byte buffer. If the +** buffer is not a multiple of 4 bytes length, compute the sum that would +** have occurred if the buffer was padded with zeros to the next multiple +** of four bytes. +*/ +static unsigned int checksum(const char *zIn, size_t N){ + static const int byteOrderTest = 1; + const unsigned char *z = (const unsigned char *)zIn; + const unsigned char *zEnd = (const unsigned char*)&zIn[N&~3]; + unsigned sum = 0; + assert( (z - (const unsigned char*)0)%4==0 ); /* Four-byte alignment */ + if( 0==*(char*)&byteOrderTest ){ + /* This is a big-endian machine */ + while( z=4003000 + while( z=1300 + while( z= 16){ + sum0 += ((unsigned)z[0] + z[4] + z[8] + z[12]); + sum1 += ((unsigned)z[1] + z[5] + z[9] + z[13]); + sum2 += ((unsigned)z[2] + z[6] + z[10]+ z[14]); + sum += ((unsigned)z[3] + z[7] + z[11]+ z[15]); + z += 16; + N -= 16; + } + while(N >= 4){ + sum0 += z[0]; + sum1 += z[1]; + sum2 += z[2]; + sum += z[3]; + z += 4; + N -= 4; + } + sum += (sum2 << 8) + (sum1 << 16) + (sum0 << 24); +#endif + } + switch(N&3){ + case 3: sum += (z[2] << 8); + case 2: sum += (z[1] << 16); + case 1: sum += (z[0] << 24); + default: ; + } + return sum; +} + +/* +** Create a new delta. +** +** The delta is written into a preallocated buffer, zDelta, which +** should be at least 60 bytes longer than the target file, zOut. +** The delta string will be NUL-terminated, but it might also contain +** embedded NUL characters if either the zSrc or zOut files are +** binary. This function returns the length of the delta string +** in bytes, excluding the final NUL terminator character. +** +** Output Format: +** +** The delta begins with a base64 number followed by a newline. This +** number is the number of bytes in the TARGET file. Thus, given a +** delta file z, a program can compute the size of the output file +** simply by reading the first line and decoding the base-64 number +** found there. The delta_output_size() routine does exactly this. +** +** After the initial size number, the delta consists of a series of +** literal text segments and commands to copy from the SOURCE file. +** A copy command looks like this: +** +** NNN@MMM, +** +** where NNN is the number of bytes to be copied and MMM is the offset +** into the source file of the first byte (both base-64). If NNN is 0 +** it means copy the rest of the input file. Literal text is like this: +** +** NNN:TTTTT +** +** where NNN is the number of bytes of text (base-64) and TTTTT is the text. +** +** The last term is of the form +** +** NNN; +** +** In this case, NNN is a 32-bit bigendian checksum of the output file +** that can be used to verify that the delta applied correctly. All +** numbers are in base-64. +** +** Pure text files generate a pure text delta. Binary files generate a +** delta that may contain some binary data. +** +** Algorithm: +** +** The encoder first builds a hash table to help it find matching +** patterns in the source file. 16-byte chunks of the source file +** sampled at evenly spaced intervals are used to populate the hash +** table. +** +** Next we begin scanning the target file using a sliding 16-byte +** window. The hash of the 16-byte window in the target is used to +** search for a matching section in the source file. When a match +** is found, a copy command is added to the delta. An effort is +** made to extend the matching section to regions that come before +** and after the 16-byte hash window. A copy command is only issued +** if the result would use less space that just quoting the text +** literally. Literal text is added to the delta for sections that +** do not match or which can not be encoded efficiently using copy +** commands. +*/ +static int delta_create( + const char *zSrc, /* The source or pattern file */ + unsigned int lenSrc, /* Length of the source file */ + const char *zOut, /* The target file */ + unsigned int lenOut, /* Length of the target file */ + char *zDelta /* Write the delta into this buffer */ +){ + int i, base; + char *zOrigDelta = zDelta; + hash h; + int nHash; /* Number of hash table entries */ + int *landmark; /* Primary hash table */ + int *collide; /* Collision chain */ + int lastRead = -1; /* Last byte of zSrc read by a COPY command */ + + /* Add the target file size to the beginning of the delta + */ + putInt(lenOut, &zDelta); + *(zDelta++) = '\n'; + + /* If the source file is very small, it means that we have no + ** chance of ever doing a copy command. Just output a single + ** literal segment for the entire target and exit. + */ + if( lenSrc<=NHASH ){ + putInt(lenOut, &zDelta); + *(zDelta++) = ':'; + memcpy(zDelta, zOut, lenOut); + zDelta += lenOut; + putInt(checksum(zOut, lenOut), &zDelta); + *(zDelta++) = ';'; + return zDelta - zOrigDelta; + } + + /* Compute the hash table used to locate matching sections in the + ** source file. + */ + nHash = lenSrc/NHASH; + collide = sqlite3_malloc64( (sqlite3_int64)nHash*2*sizeof(int) ); + memset(collide, -1, nHash*2*sizeof(int)); + landmark = &collide[nHash]; + for(i=0; i=0 && (limit--)>0 ){ + /* + ** The hash window has identified a potential match against + ** landmark block iBlock. But we need to investigate further. + ** + ** Look for a region in zOut that matches zSrc. Anchor the search + ** at zSrc[iSrc] and zOut[base+i]. Do not include anything prior to + ** zOut[base] or after zOut[outLen] nor anything after zSrc[srcLen]. + ** + ** Set cnt equal to the length of the match and set ofst so that + ** zSrc[ofst] is the first element of the match. litsz is the number + ** of characters between zOut[base] and the beginning of the match. + ** sz will be the overhead (in bytes) needed to encode the copy + ** command. Only generate copy command if the overhead of the + ** copy command is less than the amount of literal text to be copied. + */ + int cnt, ofst, litsz; + int j, k, x, y; + int sz; + int limitX; + + /* Beginning at iSrc, match forwards as far as we can. j counts + ** the number of characters that match */ + iSrc = iBlock*NHASH; + y = base+i; + limitX = ( lenSrc-iSrc <= lenOut-y ) ? lenSrc : iSrc + lenOut - y; + for(x=iSrc; x=sz && cnt>bestCnt ){ + /* Remember this match only if it is the best so far and it + ** does not increase the file size */ + bestCnt = cnt; + bestOfst = iSrc-k; + bestLitsz = litsz; + } + + /* Check the next matching block */ + iBlock = collide[iBlock]; + } + + /* We have a copy command that does not cause the delta to be larger + ** than a literal insert. So add the copy command to the delta. + */ + if( bestCnt>0 ){ + if( bestLitsz>0 ){ + /* Add an insert command before the copy */ + putInt(bestLitsz,&zDelta); + *(zDelta++) = ':'; + memcpy(zDelta, &zOut[base], bestLitsz); + zDelta += bestLitsz; + base += bestLitsz; + } + base += bestCnt; + putInt(bestCnt, &zDelta); + *(zDelta++) = '@'; + putInt(bestOfst, &zDelta); + *(zDelta++) = ','; + if( bestOfst + bestCnt -1 > lastRead ){ + lastRead = bestOfst + bestCnt - 1; + } + bestCnt = 0; + break; + } + + /* If we reach this point, it means no match is found so far */ + if( base+i+NHASH>=lenOut ){ + /* We have reached the end of the file and have not found any + ** matches. Do an "insert" for everything that does not match */ + putInt(lenOut-base, &zDelta); + *(zDelta++) = ':'; + memcpy(zDelta, &zOut[base], lenOut-base); + zDelta += lenOut-base; + base = lenOut; + break; + } + + /* Advance the hash by one character. Keep looking for a match */ + hash_next(&h, zOut[base+i+NHASH]); + i++; + } + } + /* Output a final "insert" record to get all the text at the end of + ** the file that does not match anything in the source file. + */ + if( base0 ){ + unsigned int cnt, ofst; + cnt = deltaGetInt(&zDelta, &lenDelta); + switch( zDelta[0] ){ + case '@': { + zDelta++; lenDelta--; + ofst = deltaGetInt(&zDelta, &lenDelta); + if( lenDelta>0 && zDelta[0]!=',' ){ + /* ERROR: copy command not terminated by ',' */ + return -1; + } + zDelta++; lenDelta--; + total += cnt; + if( total>limit ){ + /* ERROR: copy exceeds output file size */ + return -1; + } + if( ofst+cnt > lenSrc ){ + /* ERROR: copy extends past end of input */ + return -1; + } + memcpy(zOut, &zSrc[ofst], cnt); + zOut += cnt; + break; + } + case ':': { + zDelta++; lenDelta--; + total += cnt; + if( total>limit ){ + /* ERROR: insert command gives an output larger than predicted */ + return -1; + } + if( cnt>lenDelta ){ + /* ERROR: insert count exceeds size of delta */ + return -1; + } + memcpy(zOut, zDelta, cnt); + zOut += cnt; + zDelta += cnt; + lenDelta -= cnt; + break; + } + case ';': { + zDelta++; lenDelta--; + zOut[0] = 0; +#ifdef FOSSIL_ENABLE_DELTA_CKSUM_TEST + if( cnt!=checksum(zOrigOut, total) ){ + /* ERROR: bad checksum */ + return -1; + } +#endif + if( total!=limit ){ + /* ERROR: generated size does not match predicted size */ + return -1; + } + return total; + } + default: { + /* ERROR: unknown delta operator */ + return -1; + } + } + } + /* ERROR: unterminated delta */ + return -1; +} + +/* +** SQL functions: delta_create(X,Y) +** +** Return a delta for carrying X into Y. +*/ +static void deltaCreateFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + const char *aOrig; int nOrig; /* old blob */ + const char *aNew; int nNew; /* new blob */ + char *aOut; int nOut; /* output delta */ + + assert( argc==2 ); + if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; + if( sqlite3_value_type(argv[1])==SQLITE_NULL ) return; + nOrig = sqlite3_value_bytes(argv[0]); + aOrig = (const char*)sqlite3_value_blob(argv[0]); + nNew = sqlite3_value_bytes(argv[1]); + aNew = (const char*)sqlite3_value_blob(argv[1]); + aOut = sqlite3_malloc64(nNew+70); + if( aOut==0 ){ + sqlite3_result_error_nomem(context); + }else{ + nOut = delta_create(aOrig, nOrig, aNew, nNew, aOut); + if( nOut<0 ){ + sqlite3_free(aOut); + sqlite3_result_error(context, "cannot create fossil delta", -1); + }else{ + sqlite3_result_blob(context, aOut, nOut, sqlite3_free); + } + } +} + +/* +** SQL functions: delta_apply(X,D) +** +** Return the result of applying delta D to input X. +*/ +static void deltaApplyFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + const char *aOrig; int nOrig; /* The X input */ + const char *aDelta; int nDelta; /* The input delta (D) */ + char *aOut; int nOut, nOut2; /* The output */ + + assert( argc==2 ); + if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; + if( sqlite3_value_type(argv[1])==SQLITE_NULL ) return; + nOrig = sqlite3_value_bytes(argv[0]); + aOrig = (const char*)sqlite3_value_blob(argv[0]); + nDelta = sqlite3_value_bytes(argv[1]); + aDelta = (const char*)sqlite3_value_blob(argv[1]); + + /* Figure out the size of the output */ + nOut = delta_output_size(aDelta, nDelta); + if( nOut<0 ){ + sqlite3_result_error(context, "corrupt fossil delta", -1); + return; + } + aOut = sqlite3_malloc64((sqlite3_int64)nOut+1); + if( aOut==0 ){ + sqlite3_result_error_nomem(context); + }else{ + nOut2 = delta_apply(aOrig, nOrig, aDelta, nDelta, aOut); + if( nOut2!=nOut ){ + sqlite3_free(aOut); + sqlite3_result_error(context, "corrupt fossil delta", -1); + }else{ + sqlite3_result_blob(context, aOut, nOut, sqlite3_free); + } + } +} + + +/* +** SQL functions: delta_output_size(D) +** +** Return the size of the output that results from applying delta D. +*/ +static void deltaOutputSizeFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + const char *aDelta; int nDelta; /* The input delta (D) */ + int nOut; /* Size of output */ + assert( argc==1 ); + if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; + nDelta = sqlite3_value_bytes(argv[0]); + aDelta = (const char*)sqlite3_value_blob(argv[0]); + + /* Figure out the size of the output */ + nOut = delta_output_size(aDelta, nDelta); + if( nOut<0 ){ + sqlite3_result_error(context, "corrupt fossil delta", -1); + return; + }else{ + sqlite3_result_int(context, nOut); + } +} + +/***************************************************************************** +** Table-valued SQL function: delta_parse(DELTA) +** +** Schema: +** +** CREATE TABLE delta_parse( +** op TEXT, +** a1 INT, +** a2 ANY, +** delta HIDDEN BLOB +** ); +** +** Given an input DELTA, this function parses the delta and returns +** rows for each entry in the delta. The op column has one of the +** values SIZE, COPY, INSERT, CHECKSUM, ERROR. +** +** Assuming no errors, the first row has op='SIZE'. a1 is the size of +** the output in bytes and a2 is NULL. +** +** After the initial SIZE row, there are zero or more 'COPY' and/or 'INSERT' +** rows. A COPY row means content is copied from the source into the +** output. Column a1 is the number of bytes to copy and a2 is the offset +** into source from which to begin copying. An INSERT row means to +** insert text into the output stream. Column a1 is the number of bytes +** to insert and column is a BLOB that contains the text to be inserted. +** +** The last row of a well-formed delta will have an op value of 'CHECKSUM'. +** The a1 column will be the value of the checksum and a2 will be NULL. +** +** If the input delta is not well-formed, then a row with an op value +** of 'ERROR' is returned. The a1 value of the ERROR row is the offset +** into the delta where the error was encountered and a2 is NULL. +*/ +typedef struct deltaparsevtab_vtab deltaparsevtab_vtab; +typedef struct deltaparsevtab_cursor deltaparsevtab_cursor; +struct deltaparsevtab_vtab { + sqlite3_vtab base; /* Base class - must be first */ + /* No additional information needed */ +}; +struct deltaparsevtab_cursor { + sqlite3_vtab_cursor base; /* Base class - must be first */ + char *aDelta; /* The delta being parsed */ + int nDelta; /* Number of bytes in the delta */ + int iCursor; /* Current cursor location */ + int eOp; /* Name of current operator */ + unsigned int a1, a2; /* Arguments to current operator */ + int iNext; /* Next cursor value */ +}; + +/* Operator names: +*/ +static const char *azOp[] = { + "SIZE", "COPY", "INSERT", "CHECKSUM", "ERROR", "EOF" +}; +#define DELTAPARSE_OP_SIZE 0 +#define DELTAPARSE_OP_COPY 1 +#define DELTAPARSE_OP_INSERT 2 +#define DELTAPARSE_OP_CHECKSUM 3 +#define DELTAPARSE_OP_ERROR 4 +#define DELTAPARSE_OP_EOF 5 + +/* +** The deltaparsevtabConnect() method is invoked to create a new +** deltaparse virtual table. +** +** Think of this routine as the constructor for deltaparsevtab_vtab objects. +** +** All this routine needs to do is: +** +** (1) Allocate the deltaparsevtab_vtab object and initialize all fields. +** +** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the +** result set of queries against the virtual table will look like. +*/ +static int deltaparsevtabConnect( + sqlite3 *db, + void *pAux, + int argc, const char *const*argv, + sqlite3_vtab **ppVtab, + char **pzErr +){ + deltaparsevtab_vtab *pNew; + int rc; + + rc = sqlite3_declare_vtab(db, + "CREATE TABLE x(op,a1,a2,delta HIDDEN)" + ); + /* For convenience, define symbolic names for the index to each column. */ +#define DELTAPARSEVTAB_OP 0 +#define DELTAPARSEVTAB_A1 1 +#define DELTAPARSEVTAB_A2 2 +#define DELTAPARSEVTAB_DELTA 3 + if( rc==SQLITE_OK ){ + pNew = sqlite3_malloc64( sizeof(*pNew) ); + *ppVtab = (sqlite3_vtab*)pNew; + if( pNew==0 ) return SQLITE_NOMEM; + memset(pNew, 0, sizeof(*pNew)); + sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS); + } + return rc; +} + +/* +** This method is the destructor for deltaparsevtab_vtab objects. +*/ +static int deltaparsevtabDisconnect(sqlite3_vtab *pVtab){ + deltaparsevtab_vtab *p = (deltaparsevtab_vtab*)pVtab; + sqlite3_free(p); + return SQLITE_OK; +} + +/* +** Constructor for a new deltaparsevtab_cursor object. +*/ +static int deltaparsevtabOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){ + deltaparsevtab_cursor *pCur; + pCur = sqlite3_malloc( sizeof(*pCur) ); + if( pCur==0 ) return SQLITE_NOMEM; + memset(pCur, 0, sizeof(*pCur)); + *ppCursor = &pCur->base; + return SQLITE_OK; +} + +/* +** Destructor for a deltaparsevtab_cursor. +*/ +static int deltaparsevtabClose(sqlite3_vtab_cursor *cur){ + deltaparsevtab_cursor *pCur = (deltaparsevtab_cursor*)cur; + sqlite3_free(pCur->aDelta); + sqlite3_free(pCur); + return SQLITE_OK; +} + + +/* +** Advance a deltaparsevtab_cursor to its next row of output. +*/ +static int deltaparsevtabNext(sqlite3_vtab_cursor *cur){ + deltaparsevtab_cursor *pCur = (deltaparsevtab_cursor*)cur; + const char *z; + int i = 0; + + pCur->iCursor = pCur->iNext; + if( pCur->iCursor >= pCur->nDelta ){ + pCur->eOp = DELTAPARSE_OP_ERROR; + pCur->iNext = pCur->nDelta; + return SQLITE_OK; + } + z = pCur->aDelta + pCur->iCursor; + pCur->a1 = deltaGetInt(&z, &i); + switch( z[0] ){ + case '@': { + z++; + if( pCur->iNext>=pCur->nDelta ){ + pCur->eOp = DELTAPARSE_OP_ERROR; + pCur->iNext = pCur->nDelta; + break; + } + pCur->a2 = deltaGetInt(&z, &i); + pCur->eOp = DELTAPARSE_OP_COPY; + pCur->iNext = (int)(&z[1] - pCur->aDelta); + break; + } + case ':': { + z++; + pCur->a2 = (unsigned int)(z - pCur->aDelta); + pCur->eOp = DELTAPARSE_OP_INSERT; + pCur->iNext = (int)(&z[pCur->a1] - pCur->aDelta); + break; + } + case ';': { + pCur->eOp = DELTAPARSE_OP_CHECKSUM; + pCur->iNext = pCur->nDelta; + break; + } + default: { + if( pCur->iNext==pCur->nDelta ){ + pCur->eOp = DELTAPARSE_OP_EOF; + }else{ + pCur->eOp = DELTAPARSE_OP_ERROR; + pCur->iNext = pCur->nDelta; + } + break; + } + } + return SQLITE_OK; +} + +/* +** Return values of columns for the row at which the deltaparsevtab_cursor +** is currently pointing. +*/ +static int deltaparsevtabColumn( + sqlite3_vtab_cursor *cur, /* The cursor */ + sqlite3_context *ctx, /* First argument to sqlite3_result_...() */ + int i /* Which column to return */ +){ + deltaparsevtab_cursor *pCur = (deltaparsevtab_cursor*)cur; + switch( i ){ + case DELTAPARSEVTAB_OP: { + sqlite3_result_text(ctx, azOp[pCur->eOp], -1, SQLITE_STATIC); + break; + } + case DELTAPARSEVTAB_A1: { + sqlite3_result_int(ctx, pCur->a1); + break; + } + case DELTAPARSEVTAB_A2: { + if( pCur->eOp==DELTAPARSE_OP_COPY ){ + sqlite3_result_int(ctx, pCur->a2); + }else if( pCur->eOp==DELTAPARSE_OP_INSERT ){ + if( pCur->a2 + pCur->a1 > pCur->nDelta ){ + sqlite3_result_zeroblob(ctx, pCur->a1); + }else{ + sqlite3_result_blob(ctx, pCur->aDelta+pCur->a2, pCur->a1, + SQLITE_TRANSIENT); + } + } + break; + } + case DELTAPARSEVTAB_DELTA: { + sqlite3_result_blob(ctx, pCur->aDelta, pCur->nDelta, SQLITE_TRANSIENT); + break; + } + } + return SQLITE_OK; +} + +/* +** Return the rowid for the current row. In this implementation, the +** rowid is the same as the output value. +*/ +static int deltaparsevtabRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ + deltaparsevtab_cursor *pCur = (deltaparsevtab_cursor*)cur; + *pRowid = pCur->iCursor; + return SQLITE_OK; +} + +/* +** Return TRUE if the cursor has been moved off of the last +** row of output. +*/ +static int deltaparsevtabEof(sqlite3_vtab_cursor *cur){ + deltaparsevtab_cursor *pCur = (deltaparsevtab_cursor*)cur; + return pCur->eOp==DELTAPARSE_OP_EOF || pCur->iCursor>=pCur->nDelta; +} + +/* +** This method is called to "rewind" the deltaparsevtab_cursor object back +** to the first row of output. This method is always called at least +** once prior to any call to deltaparsevtabColumn() or deltaparsevtabRowid() or +** deltaparsevtabEof(). +*/ +static int deltaparsevtabFilter( + sqlite3_vtab_cursor *pVtabCursor, + int idxNum, const char *idxStr, + int argc, sqlite3_value **argv +){ + deltaparsevtab_cursor *pCur = (deltaparsevtab_cursor *)pVtabCursor; + const char *a; + int i = 0; + pCur->eOp = DELTAPARSE_OP_ERROR; + if( idxNum!=1 ){ + return SQLITE_OK; + } + pCur->nDelta = sqlite3_value_bytes(argv[0]); + a = (const char*)sqlite3_value_blob(argv[0]); + if( pCur->nDelta==0 || a==0 ){ + return SQLITE_OK; + } + pCur->aDelta = sqlite3_malloc64( pCur->nDelta+1 ); + if( pCur->aDelta==0 ){ + pCur->nDelta = 0; + return SQLITE_NOMEM; + } + memcpy(pCur->aDelta, a, pCur->nDelta); + pCur->aDelta[pCur->nDelta] = 0; + a = pCur->aDelta; + pCur->eOp = DELTAPARSE_OP_SIZE; + pCur->a1 = deltaGetInt(&a, &i); + if( a[0]!='\n' ){ + pCur->eOp = DELTAPARSE_OP_ERROR; + pCur->a1 = pCur->a2 = 0; + pCur->iNext = pCur->nDelta; + return SQLITE_OK; + } + a++; + pCur->iNext = (unsigned int)(a - pCur->aDelta); + return SQLITE_OK; +} + +/* +** SQLite will invoke this method one or more times while planning a query +** that uses the virtual table. This routine needs to create +** a query plan for each invocation and compute an estimated cost for that +** plan. +*/ +static int deltaparsevtabBestIndex( + sqlite3_vtab *tab, + sqlite3_index_info *pIdxInfo +){ + int i; + for(i=0; inConstraint; i++){ + if( pIdxInfo->aConstraint[i].iColumn != DELTAPARSEVTAB_DELTA ) continue; + if( pIdxInfo->aConstraint[i].usable==0 ) continue; + if( pIdxInfo->aConstraint[i].op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue; + pIdxInfo->aConstraintUsage[i].argvIndex = 1; + pIdxInfo->aConstraintUsage[i].omit = 1; + pIdxInfo->estimatedCost = (double)1; + pIdxInfo->estimatedRows = 10; + pIdxInfo->idxNum = 1; + return SQLITE_OK; + } + pIdxInfo->idxNum = 0; + pIdxInfo->estimatedCost = (double)0x7fffffff; + pIdxInfo->estimatedRows = 0x7fffffff; + return SQLITE_CONSTRAINT; +} + +/* +** This following structure defines all the methods for the +** virtual table. +*/ +static sqlite3_module deltaparsevtabModule = { + /* iVersion */ 0, + /* xCreate */ 0, + /* xConnect */ deltaparsevtabConnect, + /* xBestIndex */ deltaparsevtabBestIndex, + /* xDisconnect */ deltaparsevtabDisconnect, + /* xDestroy */ 0, + /* xOpen */ deltaparsevtabOpen, + /* xClose */ deltaparsevtabClose, + /* xFilter */ deltaparsevtabFilter, + /* xNext */ deltaparsevtabNext, + /* xEof */ deltaparsevtabEof, + /* xColumn */ deltaparsevtabColumn, + /* xRowid */ deltaparsevtabRowid, + /* xUpdate */ 0, + /* xBegin */ 0, + /* xSync */ 0, + /* xCommit */ 0, + /* xRollback */ 0, + /* xFindMethod */ 0, + /* xRename */ 0, + /* xSavepoint */ 0, + /* xRelease */ 0, + /* xRollbackTo */ 0, + /* xShadowName */ 0, + /* xIntegrity */ 0 +}; + + + +#ifdef _WIN32 +__declspec(dllexport) +#endif +int sqlite3_fossildelta_init( + sqlite3 *db, + char **pzErrMsg, + const sqlite3_api_routines *pApi +){ + static const int enc = SQLITE_UTF8|SQLITE_INNOCUOUS; + int rc = SQLITE_OK; + SQLITE_EXTENSION_INIT2(pApi); + (void)pzErrMsg; /* Unused parameter */ + rc = sqlite3_create_function(db, "delta_create", 2, enc, 0, + deltaCreateFunc, 0, 0); + if( rc==SQLITE_OK ){ + rc = sqlite3_create_function(db, "delta_apply", 2, enc, 0, + deltaApplyFunc, 0, 0); + } + if( rc==SQLITE_OK ){ + rc = sqlite3_create_function(db, "delta_output_size", 1, enc, 0, + deltaOutputSizeFunc, 0, 0); + } + if( rc==SQLITE_OK ){ + rc = sqlite3_create_module(db, "delta_parse", &deltaparsevtabModule, 0); + } + return rc; +} diff --git a/ext/misc/fuzzer.c b/ext/misc/fuzzer.c index 3ed4b0a977..e16d005d9c 100644 --- a/ext/misc/fuzzer.c +++ b/ext/misc/fuzzer.c @@ -68,7 +68,7 @@ ** AND distance<200; ** ** This first query outputs the string "abcdefg" and all strings that -** can be derived from that string by appling the specified transformations. +** can be derived from that string by applying the specified transformations. ** The strings are output together with their total transformation cost ** (called "distance") and appear in order of increasing cost. No string ** is output more than once. If there are multiple ways to transform the @@ -97,7 +97,7 @@ ** LIMIT 20 ** ** The query above gives the 20 closest words to the $word being tested. -** (Note that for good performance, the vocubulary.w column should be +** (Note that for good performance, the vocabulary.w column should be ** indexed.) ** ** A similar query can be used to find all words in the dictionary that @@ -207,7 +207,7 @@ struct fuzzer_rule { ** Every stem is added to a hash table as it is output. Generation of ** duplicate stems is suppressed. ** -** Active stems (those that might generate new outputs) are kepts on a linked +** Active stems (those that might generate new outputs) are kept on a linked ** list sorted by increasing cost. The cost is the sum of rBaseCost and ** pRule->rCost. */ @@ -337,17 +337,17 @@ static int fuzzerLoadOneRule( rc = SQLITE_ERROR; }else{ - pRule = sqlite3_malloc( sizeof(*pRule) + nFrom + nTo ); + pRule = sqlite3_malloc64( sizeof(*pRule) + nFrom + nTo ); if( pRule==0 ){ rc = SQLITE_NOMEM; }else{ memset(pRule, 0, sizeof(*pRule)); pRule->zFrom = pRule->zTo; pRule->zFrom += nTo + 1; - pRule->nFrom = nFrom; + pRule->nFrom = (fuzzer_len)nFrom; memcpy(pRule->zFrom, zFrom, nFrom+1); memcpy(pRule->zTo, zTo, nTo+1); - pRule->nTo = nTo; + pRule->nTo = (fuzzer_len)nTo; pRule->rCost = nCost; pRule->iRuleset = (int)iRuleset; } @@ -447,16 +447,16 @@ static int fuzzerLoadRules( ** `mno` becomes mno */ static char *fuzzerDequote(const char *zIn){ - int nIn; /* Size of input string, in bytes */ + sqlite3_int64 nIn; /* Size of input string, in bytes */ char *zOut; /* Output (dequoted) string */ - nIn = (int)strlen(zIn); - zOut = sqlite3_malloc(nIn+1); + nIn = strlen(zIn); + zOut = sqlite3_malloc64(nIn+1); if( zOut ){ char q = zIn[0]; /* Quote character (if any ) */ if( q!='[' && q!= '\'' && q!='"' && q!='`' ){ - memcpy(zOut, zIn, nIn+1); + memcpy(zOut, zIn, (size_t)(nIn+1)); }else{ int iOut = 0; /* Index of next byte to write to output */ int iIn; /* Index of next byte to read from input */ @@ -513,10 +513,10 @@ static int fuzzerConnect( ); rc = SQLITE_ERROR; }else{ - int nModule; /* Length of zModule, in bytes */ + sqlite3_int64 nModule; /* Length of zModule, in bytes */ - nModule = (int)strlen(zModule); - pNew = sqlite3_malloc( sizeof(*pNew) + nModule + 1); + nModule = strlen(zModule); + pNew = sqlite3_malloc64( sizeof(*pNew) + nModule + 1); if( pNew==0 ){ rc = SQLITE_NOMEM; }else{ @@ -524,7 +524,7 @@ static int fuzzerConnect( memset(pNew, 0, sizeof(*pNew)); pNew->zClassName = (char*)&pNew[1]; - memcpy(pNew->zClassName, zModule, nModule+1); + memcpy(pNew->zClassName, zModule, (size_t)(nModule+1)); zTab = fuzzerDequote(argv[3]); if( zTab==0 ){ @@ -540,6 +540,8 @@ static int fuzzerConnect( if( rc!=SQLITE_OK ){ fuzzerDisconnect((sqlite3_vtab *)pNew); pNew = 0; + }else{ + sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS); } } } @@ -872,7 +874,7 @@ static fuzzer_stem *fuzzerNewStem( fuzzer_rule *pRule; unsigned int h; - pNew = sqlite3_malloc( sizeof(*pNew) + (int)strlen(zWord) + 1 ); + pNew = sqlite3_malloc64( sizeof(*pNew) + strlen(zWord) + 1 ); if( pNew==0 ) return 0; memset(pNew, 0, sizeof(*pNew)); pNew->zBasis = (char*)&pNew[1]; @@ -1163,6 +1165,11 @@ static sqlite3_module fuzzerModule = { 0, /* xRollback */ 0, /* xFindMethod */ 0, /* xRename */ + 0, /* xSavepoint */ + 0, /* xRelease */ + 0, /* xRollbackTo */ + 0, /* xShadowName */ + 0 /* xIntegrity */ }; #endif /* SQLITE_OMIT_VIRTUALTABLE */ diff --git a/ext/misc/ieee754.c b/ext/misc/ieee754.c index 20c5e61433..7f1c6d9e1f 100644 --- a/ext/misc/ieee754.c +++ b/ext/misc/ieee754.c @@ -26,16 +26,76 @@ ** ** Examples: ** -** ieee754(2.0) -> 'ieee754(2,0)' -** ieee754(45.25) -> 'ieee754(181,-2)' -** ieee754(2, 0) -> 2.0 -** ieee754(181, -2) -> 45.25 +** ieee754(2.0) -> 'ieee754(2,0)' +** ieee754(45.25) -> 'ieee754(181,-2)' +** ieee754(2, 0) -> 2.0 +** ieee754(181, -2) -> 45.25 +** +** Two additional functions break apart the one-argument ieee754() +** result into separate integer values: +** +** ieee754_mantissa(45.25) -> 181 +** ieee754_exponent(45.25) -> -2 +** +** These functions convert binary64 numbers into blobs and back again. +** +** ieee754_from_blob(x'3ff0000000000000') -> 1.0 +** ieee754_to_blob(1.0) -> x'3ff0000000000000' +** +** In all single-argument functions, if the argument is an 8-byte blob +** then that blob is interpreted as a big-endian binary64 value. +** +** +** EXACT DECIMAL REPRESENTATION OF BINARY64 VALUES +** ----------------------------------------------- +** +** This extension in combination with the separate 'decimal' extension +** can be used to compute the exact decimal representation of binary64 +** values. To begin, first compute a table of exponent values: +** +** CREATE TABLE pow2(x INTEGER PRIMARY KEY, v TEXT); +** WITH RECURSIVE c(x,v) AS ( +** VALUES(0,'1') +** UNION ALL +** SELECT x+1, decimal_mul(v,'2') FROM c WHERE x+1<=971 +** ) INSERT INTO pow2(x,v) SELECT x, v FROM c; +** WITH RECURSIVE c(x,v) AS ( +** VALUES(-1,'0.5') +** UNION ALL +** SELECT x-1, decimal_mul(v,'0.5') FROM c WHERE x-1>=-1075 +** ) INSERT INTO pow2(x,v) SELECT x, v FROM c; +** +** Then, to compute the exact decimal representation of a floating +** point value (the value 47.49 is used in the example) do: +** +** WITH c(n) AS (VALUES(47.49)) +** ---------------^^^^^---- Replace with whatever you want +** SELECT decimal_mul(ieee754_mantissa(c.n),pow2.v) +** FROM pow2, c WHERE pow2.x=ieee754_exponent(c.n); +** +** Here is a query to show various boundry values for the binary64 +** number format: +** +** WITH c(name,bin) AS (VALUES +** ('minimum positive value', x'0000000000000001'), +** ('maximum subnormal value', x'000fffffffffffff'), +** ('minimum positive normal value', x'0010000000000000'), +** ('maximum value', x'7fefffffffffffff')) +** SELECT c.name, decimal_mul(ieee754_mantissa(c.bin),pow2.v) +** FROM pow2, c WHERE pow2.x=ieee754_exponent(c.bin); +** */ #include "sqlite3ext.h" SQLITE_EXTENSION_INIT1 #include #include +/* Mark a function parameter as unused, to suppress nuisance compiler +** warnings. */ +#ifndef UNUSED_PARAMETER +# define UNUSED_PARAMETER(X) (void)(X) +#endif + /* ** Implementation of the ieee754() function */ @@ -51,8 +111,19 @@ static void ieee754func( int isNeg; char zResult[100]; assert( sizeof(m)==sizeof(r) ); - if( sqlite3_value_type(argv[0])!=SQLITE_FLOAT ) return; - r = sqlite3_value_double(argv[0]); + if( sqlite3_value_type(argv[0])==SQLITE_BLOB + && sqlite3_value_bytes(argv[0])==sizeof(r) + ){ + const unsigned char *x = sqlite3_value_blob(argv[0]); + unsigned int i; + sqlite3_uint64 v = 0; + for(i=0; i>52; m = a & ((((sqlite3_int64)1)<<52)-1); - m |= ((sqlite3_int64)1)<<52; + if( e==0 ){ + m <<= 1; + }else{ + m |= ((sqlite3_int64)1)<<52; + } while( e<1075 && m>0 && (m&1)==0 ){ m >>= 1; e++; } if( isNeg ) m = -m; } - sqlite3_snprintf(sizeof(zResult), zResult, "ieee754(%lld,%d)", - m, e-1075); - sqlite3_result_text(context, zResult, -1, SQLITE_TRANSIENT); - }else if( argc==2 ){ + switch( *(int*)sqlite3_user_data(context) ){ + case 0: + sqlite3_snprintf(sizeof(zResult), zResult, "ieee754(%lld,%d)", + m, e-1075); + sqlite3_result_text(context, zResult, -1, SQLITE_TRANSIENT); + break; + case 1: + sqlite3_result_int64(context, m); + break; + case 2: + sqlite3_result_int(context, e-1075); + break; + } + }else{ sqlite3_int64 m, e, a; double r; int isNeg = 0; m = sqlite3_value_int64(argv[0]); e = sqlite3_value_int64(argv[1]); + + /* Limit the range of e. Ticket 22dea1cfdb9151e4 2021-03-02 */ + if( e>10000 ){ + e = 10000; + }else if( e<-10000 ){ + e = -10000; + } + if( m<0 ){ isNeg = 1; m = -m; if( m<0 ) return; - }else if( m==0 && e>1000 && e<1000 ){ + }else if( m==0 && e>-1000 && e<1000 ){ sqlite3_result_double(context, 0.0); return; } @@ -99,8 +195,17 @@ static void ieee754func( e--; } e += 1075; - if( e<0 ) e = m = 0; - if( e>0x7ff ) e = 0x7ff; + if( e<=0 ){ + /* Subnormal */ + if( 1-e >= 64 ){ + m = 0; + }else{ + m >>= 1-e; + } + e = 0; + }else if( e>0x7ff ){ + e = 0x7ff; + } a = m & ((((sqlite3_int64)1)<<52)-1); a |= e<<52; if( isNeg ) a |= ((sqlite3_uint64)1)<<63; @@ -109,6 +214,82 @@ static void ieee754func( } } +/* +** Functions to convert between blobs and floats. +*/ +static void ieee754func_from_blob( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + UNUSED_PARAMETER(argc); + if( sqlite3_value_type(argv[0])==SQLITE_BLOB + && sqlite3_value_bytes(argv[0])==sizeof(double) + ){ + double r; + const unsigned char *x = sqlite3_value_blob(argv[0]); + unsigned int i; + sqlite3_uint64 v = 0; + for(i=0; i>= 8; + } + sqlite3_result_blob(context, a, sizeof(r), SQLITE_TRANSIENT); + } +} + +/* +** SQL Function: ieee754_inc(r,N) +** +** Move the floating point value r by N quantums and return the new +** values. +** +** Behind the scenes: this routine merely casts r into a 64-bit unsigned +** integer, adds N, then casts the value back into float. +** +** Example: To find the smallest positive number: +** +** SELECT ieee754_inc(0.0,+1); +*/ +static void ieee754inc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + double r; + sqlite3_int64 N; + sqlite3_uint64 m1, m2; + double r2; + UNUSED_PARAMETER(argc); + r = sqlite3_value_double(argv[0]); + N = sqlite3_value_int64(argv[1]); + memcpy(&m1, &r, 8); + m2 = m1 + N; + memcpy(&r2, &m2, 8); + sqlite3_result_double(context, r2); +} + #ifdef _WIN32 __declspec(dllexport) @@ -118,14 +299,29 @@ int sqlite3_ieee_init( char **pzErrMsg, const sqlite3_api_routines *pApi ){ + static const struct { + char *zFName; + int nArg; + int iAux; + void (*xFunc)(sqlite3_context*,int,sqlite3_value**); + } aFunc[] = { + { "ieee754", 1, 0, ieee754func }, + { "ieee754", 2, 0, ieee754func }, + { "ieee754_mantissa", 1, 1, ieee754func }, + { "ieee754_exponent", 1, 2, ieee754func }, + { "ieee754_to_blob", 1, 0, ieee754func_to_blob }, + { "ieee754_from_blob", 1, 0, ieee754func_from_blob }, + { "ieee754_inc", 2, 0, ieee754inc }, + }; + unsigned int i; int rc = SQLITE_OK; SQLITE_EXTENSION_INIT2(pApi); (void)pzErrMsg; /* Unused parameter */ - rc = sqlite3_create_function(db, "ieee754", 1, SQLITE_UTF8, 0, - ieee754func, 0, 0); - if( rc==SQLITE_OK ){ - rc = sqlite3_create_function(db, "ieee754", 2, SQLITE_UTF8, 0, - ieee754func, 0, 0); + for(i=0; i -#include -#include -#include - -/* Mark a function parameter as unused, to suppress nuisance compiler -** warnings. */ -#ifndef UNUSED_PARAM -# define UNUSED_PARAM(X) (void)(X) -#endif - -#ifndef LARGEST_INT64 -# define LARGEST_INT64 (0xffffffff|(((sqlite3_int64)0x7fffffff)<<32)) -# define SMALLEST_INT64 (((sqlite3_int64)-1) - LARGEST_INT64) -#endif - -/* -** Versions of isspace(), isalnum() and isdigit() to which it is safe -** to pass signed char values. -*/ -#ifdef sqlite3Isdigit - /* Use the SQLite core versions if this routine is part of the - ** SQLite amalgamation */ -# define safe_isdigit(x) sqlite3Isdigit(x) -# define safe_isalnum(x) sqlite3Isalnum(x) -#else - /* Use the standard library for separate compilation */ -#include /* amalgamator: keep */ -# define safe_isdigit(x) isdigit((unsigned char)(x)) -# define safe_isalnum(x) isalnum((unsigned char)(x)) -#endif - -/* -** Growing our own isspace() routine this way is twice as fast as -** the library isspace() function, resulting in a 7% overall performance -** increase for the parser. (Ubuntu14.10 gcc 4.8.4 x64 with -Os). -*/ -static const char jsonIsSpace[] = { - 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -}; -#define safe_isspace(x) (jsonIsSpace[(unsigned char)x]) - -#ifndef SQLITE_AMALGAMATION - /* Unsigned integer types. These are already defined in the sqliteInt.h, - ** but the definitions need to be repeated for separate compilation. */ - typedef sqlite3_uint64 u64; - typedef unsigned int u32; - typedef unsigned char u8; -#endif - -/* Objects */ -typedef struct JsonString JsonString; -typedef struct JsonNode JsonNode; -typedef struct JsonParse JsonParse; - -/* An instance of this object represents a JSON string -** under construction. Really, this is a generic string accumulator -** that can be and is used to create strings other than JSON. -*/ -struct JsonString { - sqlite3_context *pCtx; /* Function context - put error messages here */ - char *zBuf; /* Append JSON content here */ - u64 nAlloc; /* Bytes of storage available in zBuf[] */ - u64 nUsed; /* Bytes of zBuf[] currently used */ - u8 bStatic; /* True if zBuf is static space */ - u8 bErr; /* True if an error has been encountered */ - char zSpace[100]; /* Initial static space */ -}; - -/* JSON type values -*/ -#define JSON_NULL 0 -#define JSON_TRUE 1 -#define JSON_FALSE 2 -#define JSON_INT 3 -#define JSON_REAL 4 -#define JSON_STRING 5 -#define JSON_ARRAY 6 -#define JSON_OBJECT 7 - -/* The "subtype" set for JSON values */ -#define JSON_SUBTYPE 74 /* Ascii for "J" */ - -/* -** Names of the various JSON types: -*/ -static const char * const jsonType[] = { - "null", "true", "false", "integer", "real", "text", "array", "object" -}; - -/* Bit values for the JsonNode.jnFlag field -*/ -#define JNODE_RAW 0x01 /* Content is raw, not JSON encoded */ -#define JNODE_ESCAPE 0x02 /* Content is text with \ escapes */ -#define JNODE_REMOVE 0x04 /* Do not output */ -#define JNODE_REPLACE 0x08 /* Replace with JsonNode.iVal */ -#define JNODE_APPEND 0x10 /* More ARRAY/OBJECT entries at u.iAppend */ -#define JNODE_LABEL 0x20 /* Is a label of an object */ - - -/* A single node of parsed JSON -*/ -struct JsonNode { - u8 eType; /* One of the JSON_ type values */ - u8 jnFlags; /* JNODE flags */ - u8 iVal; /* Replacement value when JNODE_REPLACE */ - u32 n; /* Bytes of content, or number of sub-nodes */ - union { - const char *zJContent; /* Content for INT, REAL, and STRING */ - u32 iAppend; /* More terms for ARRAY and OBJECT */ - u32 iKey; /* Key for ARRAY objects in json_tree() */ - } u; -}; - -/* A completely parsed JSON string -*/ -struct JsonParse { - u32 nNode; /* Number of slots of aNode[] used */ - u32 nAlloc; /* Number of slots of aNode[] allocated */ - JsonNode *aNode; /* Array of nodes containing the parse */ - const char *zJson; /* Original JSON string */ - u32 *aUp; /* Index of parent of each node */ - u8 oom; /* Set to true if out of memory */ - u8 nErr; /* Number of errors seen */ -}; - -/************************************************************************** -** Utility routines for dealing with JsonString objects -**************************************************************************/ - -/* Set the JsonString object to an empty string -*/ -static void jsonZero(JsonString *p){ - p->zBuf = p->zSpace; - p->nAlloc = sizeof(p->zSpace); - p->nUsed = 0; - p->bStatic = 1; -} - -/* Initialize the JsonString object -*/ -static void jsonInit(JsonString *p, sqlite3_context *pCtx){ - p->pCtx = pCtx; - p->bErr = 0; - jsonZero(p); -} - - -/* Free all allocated memory and reset the JsonString object back to its -** initial state. -*/ -static void jsonReset(JsonString *p){ - if( !p->bStatic ) sqlite3_free(p->zBuf); - jsonZero(p); -} - - -/* Report an out-of-memory (OOM) condition -*/ -static void jsonOom(JsonString *p){ - p->bErr = 1; - sqlite3_result_error_nomem(p->pCtx); - jsonReset(p); -} - -/* Enlarge pJson->zBuf so that it can hold at least N more bytes. -** Return zero on success. Return non-zero on an OOM error -*/ -static int jsonGrow(JsonString *p, u32 N){ - u64 nTotal = NnAlloc ? p->nAlloc*2 : p->nAlloc+N+10; - char *zNew; - if( p->bStatic ){ - if( p->bErr ) return 1; - zNew = sqlite3_malloc64(nTotal); - if( zNew==0 ){ - jsonOom(p); - return SQLITE_NOMEM; - } - memcpy(zNew, p->zBuf, (size_t)p->nUsed); - p->zBuf = zNew; - p->bStatic = 0; - }else{ - zNew = sqlite3_realloc64(p->zBuf, nTotal); - if( zNew==0 ){ - jsonOom(p); - return SQLITE_NOMEM; - } - p->zBuf = zNew; - } - p->nAlloc = nTotal; - return SQLITE_OK; -} - -/* Append N bytes from zIn onto the end of the JsonString string. -*/ -static void jsonAppendRaw(JsonString *p, const char *zIn, u32 N){ - if( (N+p->nUsed >= p->nAlloc) && jsonGrow(p,N)!=0 ) return; - memcpy(p->zBuf+p->nUsed, zIn, N); - p->nUsed += N; -} - -/* Append formatted text (not to exceed N bytes) to the JsonString. -*/ -static void jsonPrintf(int N, JsonString *p, const char *zFormat, ...){ - va_list ap; - if( (p->nUsed + N >= p->nAlloc) && jsonGrow(p, N) ) return; - va_start(ap, zFormat); - sqlite3_vsnprintf(N, p->zBuf+p->nUsed, zFormat, ap); - va_end(ap); - p->nUsed += (int)strlen(p->zBuf+p->nUsed); -} - -/* Append a single character -*/ -static void jsonAppendChar(JsonString *p, char c){ - if( p->nUsed>=p->nAlloc && jsonGrow(p,1)!=0 ) return; - p->zBuf[p->nUsed++] = c; -} - -/* Append a comma separator to the output buffer, if the previous -** character is not '[' or '{'. -*/ -static void jsonAppendSeparator(JsonString *p){ - char c; - if( p->nUsed==0 ) return; - c = p->zBuf[p->nUsed-1]; - if( c!='[' && c!='{' ) jsonAppendChar(p, ','); -} - -/* Append the N-byte string in zIn to the end of the JsonString string -** under construction. Enclose the string in "..." and escape -** any double-quotes or backslash characters contained within the -** string. -*/ -static void jsonAppendString(JsonString *p, const char *zIn, u32 N){ - u32 i; - if( (N+p->nUsed+2 >= p->nAlloc) && jsonGrow(p,N+2)!=0 ) return; - p->zBuf[p->nUsed++] = '"'; - for(i=0; inUsed+N+3-i > p->nAlloc) && jsonGrow(p,N+3-i)!=0 ) return; - p->zBuf[p->nUsed++] = '\\'; - }else if( c<=0x1f ){ - static const char aSpecial[] = { - 0, 0, 0, 0, 0, 0, 0, 0, 'b', 't', 'n', 0, 'f', 'r', 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 - }; - assert( sizeof(aSpecial)==32 ); - assert( aSpecial['\b']=='b' ); - assert( aSpecial['\f']=='f' ); - assert( aSpecial['\n']=='n' ); - assert( aSpecial['\r']=='r' ); - assert( aSpecial['\t']=='t' ); - if( aSpecial[c] ){ - c = aSpecial[c]; - goto json_simple_escape; - } - if( (p->nUsed+N+7+i > p->nAlloc) && jsonGrow(p,N+7-i)!=0 ) return; - p->zBuf[p->nUsed++] = '\\'; - p->zBuf[p->nUsed++] = 'u'; - p->zBuf[p->nUsed++] = '0'; - p->zBuf[p->nUsed++] = '0'; - p->zBuf[p->nUsed++] = '0' + (c>>4); - c = "0123456789abcdef"[c&0xf]; - } - p->zBuf[p->nUsed++] = c; - } - p->zBuf[p->nUsed++] = '"'; - assert( p->nUsednAlloc ); -} - -/* -** Append a function parameter value to the JSON string under -** construction. -*/ -static void jsonAppendValue( - JsonString *p, /* Append to this JSON string */ - sqlite3_value *pValue /* Value to append */ -){ - switch( sqlite3_value_type(pValue) ){ - case SQLITE_NULL: { - jsonAppendRaw(p, "null", 4); - break; - } - case SQLITE_INTEGER: - case SQLITE_FLOAT: { - const char *z = (const char*)sqlite3_value_text(pValue); - u32 n = (u32)sqlite3_value_bytes(pValue); - jsonAppendRaw(p, z, n); - break; - } - case SQLITE_TEXT: { - const char *z = (const char*)sqlite3_value_text(pValue); - u32 n = (u32)sqlite3_value_bytes(pValue); - if( sqlite3_value_subtype(pValue)==JSON_SUBTYPE ){ - jsonAppendRaw(p, z, n); - }else{ - jsonAppendString(p, z, n); - } - break; - } - default: { - if( p->bErr==0 ){ - sqlite3_result_error(p->pCtx, "JSON cannot hold BLOB values", -1); - p->bErr = 2; - jsonReset(p); - } - break; - } - } -} - - -/* Make the JSON in p the result of the SQL function. -*/ -static void jsonResult(JsonString *p){ - if( p->bErr==0 ){ - sqlite3_result_text64(p->pCtx, p->zBuf, p->nUsed, - p->bStatic ? SQLITE_TRANSIENT : sqlite3_free, - SQLITE_UTF8); - jsonZero(p); - } - assert( p->bStatic ); -} - -/************************************************************************** -** Utility routines for dealing with JsonNode and JsonParse objects -**************************************************************************/ - -/* -** Return the number of consecutive JsonNode slots need to represent -** the parsed JSON at pNode. The minimum answer is 1. For ARRAY and -** OBJECT types, the number might be larger. -** -** Appended elements are not counted. The value returned is the number -** by which the JsonNode counter should increment in order to go to the -** next peer value. -*/ -static u32 jsonNodeSize(JsonNode *pNode){ - return pNode->eType>=JSON_ARRAY ? pNode->n+1 : 1; -} - -/* -** Reclaim all memory allocated by a JsonParse object. But do not -** delete the JsonParse object itself. -*/ -static void jsonParseReset(JsonParse *pParse){ - sqlite3_free(pParse->aNode); - pParse->aNode = 0; - pParse->nNode = 0; - pParse->nAlloc = 0; - sqlite3_free(pParse->aUp); - pParse->aUp = 0; -} - -/* -** Convert the JsonNode pNode into a pure JSON string and -** append to pOut. Subsubstructure is also included. Return -** the number of JsonNode objects that are encoded. -*/ -static void jsonRenderNode( - JsonNode *pNode, /* The node to render */ - JsonString *pOut, /* Write JSON here */ - sqlite3_value **aReplace /* Replacement values */ -){ - switch( pNode->eType ){ - default: { - assert( pNode->eType==JSON_NULL ); - jsonAppendRaw(pOut, "null", 4); - break; - } - case JSON_TRUE: { - jsonAppendRaw(pOut, "true", 4); - break; - } - case JSON_FALSE: { - jsonAppendRaw(pOut, "false", 5); - break; - } - case JSON_STRING: { - if( pNode->jnFlags & JNODE_RAW ){ - jsonAppendString(pOut, pNode->u.zJContent, pNode->n); - break; - } - /* Fall through into the next case */ - } - case JSON_REAL: - case JSON_INT: { - jsonAppendRaw(pOut, pNode->u.zJContent, pNode->n); - break; - } - case JSON_ARRAY: { - u32 j = 1; - jsonAppendChar(pOut, '['); - for(;;){ - while( j<=pNode->n ){ - if( pNode[j].jnFlags & (JNODE_REMOVE|JNODE_REPLACE) ){ - if( pNode[j].jnFlags & JNODE_REPLACE ){ - jsonAppendSeparator(pOut); - jsonAppendValue(pOut, aReplace[pNode[j].iVal]); - } - }else{ - jsonAppendSeparator(pOut); - jsonRenderNode(&pNode[j], pOut, aReplace); - } - j += jsonNodeSize(&pNode[j]); - } - if( (pNode->jnFlags & JNODE_APPEND)==0 ) break; - pNode = &pNode[pNode->u.iAppend]; - j = 1; - } - jsonAppendChar(pOut, ']'); - break; - } - case JSON_OBJECT: { - u32 j = 1; - jsonAppendChar(pOut, '{'); - for(;;){ - while( j<=pNode->n ){ - if( (pNode[j+1].jnFlags & JNODE_REMOVE)==0 ){ - jsonAppendSeparator(pOut); - jsonRenderNode(&pNode[j], pOut, aReplace); - jsonAppendChar(pOut, ':'); - if( pNode[j+1].jnFlags & JNODE_REPLACE ){ - jsonAppendValue(pOut, aReplace[pNode[j+1].iVal]); - }else{ - jsonRenderNode(&pNode[j+1], pOut, aReplace); - } - } - j += 1 + jsonNodeSize(&pNode[j+1]); - } - if( (pNode->jnFlags & JNODE_APPEND)==0 ) break; - pNode = &pNode[pNode->u.iAppend]; - j = 1; - } - jsonAppendChar(pOut, '}'); - break; - } - } -} - -/* -** Return a JsonNode and all its descendents as a JSON string. -*/ -static void jsonReturnJson( - JsonNode *pNode, /* Node to return */ - sqlite3_context *pCtx, /* Return value for this function */ - sqlite3_value **aReplace /* Array of replacement values */ -){ - JsonString s; - jsonInit(&s, pCtx); - jsonRenderNode(pNode, &s, aReplace); - jsonResult(&s); - sqlite3_result_subtype(pCtx, JSON_SUBTYPE); -} - -/* -** Make the JsonNode the return value of the function. -*/ -static void jsonReturn( - JsonNode *pNode, /* Node to return */ - sqlite3_context *pCtx, /* Return value for this function */ - sqlite3_value **aReplace /* Array of replacement values */ -){ - switch( pNode->eType ){ - default: { - assert( pNode->eType==JSON_NULL ); - sqlite3_result_null(pCtx); - break; - } - case JSON_TRUE: { - sqlite3_result_int(pCtx, 1); - break; - } - case JSON_FALSE: { - sqlite3_result_int(pCtx, 0); - break; - } - case JSON_INT: { - sqlite3_int64 i = 0; - const char *z = pNode->u.zJContent; - if( z[0]=='-' ){ z++; } - while( z[0]>='0' && z[0]<='9' ){ - unsigned v = *(z++) - '0'; - if( i>=LARGEST_INT64/10 ){ - if( i>LARGEST_INT64/10 ) goto int_as_real; - if( z[0]>='0' && z[0]<='9' ) goto int_as_real; - if( v==9 ) goto int_as_real; - if( v==8 ){ - if( pNode->u.zJContent[0]=='-' ){ - sqlite3_result_int64(pCtx, SMALLEST_INT64); - goto int_done; - }else{ - goto int_as_real; - } - } - } - i = i*10 + v; - } - if( pNode->u.zJContent[0]=='-' ){ i = -i; } - sqlite3_result_int64(pCtx, i); - int_done: - break; - int_as_real: /* fall through to real */; - } - case JSON_REAL: { - double r; -#ifdef SQLITE_AMALGAMATION - const char *z = pNode->u.zJContent; - sqlite3AtoF(z, &r, sqlite3Strlen30(z), SQLITE_UTF8); -#else - r = strtod(pNode->u.zJContent, 0); -#endif - sqlite3_result_double(pCtx, r); - break; - } - case JSON_STRING: { -#if 0 /* Never happens because JNODE_RAW is only set by json_set(), - ** json_insert() and json_replace() and those routines do not - ** call jsonReturn() */ - if( pNode->jnFlags & JNODE_RAW ){ - sqlite3_result_text(pCtx, pNode->u.zJContent, pNode->n, - SQLITE_TRANSIENT); - }else -#endif - assert( (pNode->jnFlags & JNODE_RAW)==0 ); - if( (pNode->jnFlags & JNODE_ESCAPE)==0 ){ - /* JSON formatted without any backslash-escapes */ - sqlite3_result_text(pCtx, pNode->u.zJContent+1, pNode->n-2, - SQLITE_TRANSIENT); - }else{ - /* Translate JSON formatted string into raw text */ - u32 i; - u32 n = pNode->n; - const char *z = pNode->u.zJContent; - char *zOut; - u32 j; - zOut = sqlite3_malloc( n+1 ); - if( zOut==0 ){ - sqlite3_result_error_nomem(pCtx); - break; - } - for(i=1, j=0; i='0' && c<='9' ) v = v*16 + c - '0'; - else if( c>='A' && c<='F' ) v = v*16 + c - 'A' + 10; - else if( c>='a' && c<='f' ) v = v*16 + c - 'a' + 10; - else break; - } - if( v==0 ) break; - if( v<=0x7f ){ - zOut[j++] = (char)v; - }else if( v<=0x7ff ){ - zOut[j++] = (char)(0xc0 | (v>>6)); - zOut[j++] = 0x80 | (v&0x3f); - }else{ - zOut[j++] = (char)(0xe0 | (v>>12)); - zOut[j++] = 0x80 | ((v>>6)&0x3f); - zOut[j++] = 0x80 | (v&0x3f); - } - }else{ - if( c=='b' ){ - c = '\b'; - }else if( c=='f' ){ - c = '\f'; - }else if( c=='n' ){ - c = '\n'; - }else if( c=='r' ){ - c = '\r'; - }else if( c=='t' ){ - c = '\t'; - } - zOut[j++] = c; - } - } - } - zOut[j] = 0; - sqlite3_result_text(pCtx, zOut, j, sqlite3_free); - } - break; - } - case JSON_ARRAY: - case JSON_OBJECT: { - jsonReturnJson(pNode, pCtx, aReplace); - break; - } - } -} - -/* Forward reference */ -static int jsonParseAddNode(JsonParse*,u32,u32,const char*); - -/* -** A macro to hint to the compiler that a function should not be -** inlined. -*/ -#if defined(__GNUC__) -# define JSON_NOINLINE __attribute__((noinline)) -#elif defined(_MSC_VER) && _MSC_VER>=1310 -# define JSON_NOINLINE __declspec(noinline) -#else -# define JSON_NOINLINE -#endif - - -static JSON_NOINLINE int jsonParseAddNodeExpand( - JsonParse *pParse, /* Append the node to this object */ - u32 eType, /* Node type */ - u32 n, /* Content size or sub-node count */ - const char *zContent /* Content */ -){ - u32 nNew; - JsonNode *pNew; - assert( pParse->nNode>=pParse->nAlloc ); - if( pParse->oom ) return -1; - nNew = pParse->nAlloc*2 + 10; - pNew = sqlite3_realloc(pParse->aNode, sizeof(JsonNode)*nNew); - if( pNew==0 ){ - pParse->oom = 1; - return -1; - } - pParse->nAlloc = nNew; - pParse->aNode = pNew; - assert( pParse->nNodenAlloc ); - return jsonParseAddNode(pParse, eType, n, zContent); -} - -/* -** Create a new JsonNode instance based on the arguments and append that -** instance to the JsonParse. Return the index in pParse->aNode[] of the -** new node, or -1 if a memory allocation fails. -*/ -static int jsonParseAddNode( - JsonParse *pParse, /* Append the node to this object */ - u32 eType, /* Node type */ - u32 n, /* Content size or sub-node count */ - const char *zContent /* Content */ -){ - JsonNode *p; - if( pParse->nNode>=pParse->nAlloc ){ - return jsonParseAddNodeExpand(pParse, eType, n, zContent); - } - p = &pParse->aNode[pParse->nNode]; - p->eType = (u8)eType; - p->jnFlags = 0; - p->iVal = 0; - p->n = n; - p->u.zJContent = zContent; - return pParse->nNode++; -} - -/* -** Parse a single JSON value which begins at pParse->zJson[i]. Return the -** index of the first character past the end of the value parsed. -** -** Return negative for a syntax error. Special cases: return -2 if the -** first non-whitespace character is '}' and return -3 if the first -** non-whitespace character is ']'. -*/ -static int jsonParseValue(JsonParse *pParse, u32 i){ - char c; - u32 j; - int iThis; - int x; - JsonNode *pNode; - while( safe_isspace(pParse->zJson[i]) ){ i++; } - if( (c = pParse->zJson[i])=='{' ){ - /* Parse object */ - iThis = jsonParseAddNode(pParse, JSON_OBJECT, 0, 0); - if( iThis<0 ) return -1; - for(j=i+1;;j++){ - while( safe_isspace(pParse->zJson[j]) ){ j++; } - x = jsonParseValue(pParse, j); - if( x<0 ){ - if( x==(-2) && pParse->nNode==(u32)iThis+1 ) return j+1; - return -1; - } - if( pParse->oom ) return -1; - pNode = &pParse->aNode[pParse->nNode-1]; - if( pNode->eType!=JSON_STRING ) return -1; - pNode->jnFlags |= JNODE_LABEL; - j = x; - while( safe_isspace(pParse->zJson[j]) ){ j++; } - if( pParse->zJson[j]!=':' ) return -1; - j++; - x = jsonParseValue(pParse, j); - if( x<0 ) return -1; - j = x; - while( safe_isspace(pParse->zJson[j]) ){ j++; } - c = pParse->zJson[j]; - if( c==',' ) continue; - if( c!='}' ) return -1; - break; - } - pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1; - return j+1; - }else if( c=='[' ){ - /* Parse array */ - iThis = jsonParseAddNode(pParse, JSON_ARRAY, 0, 0); - if( iThis<0 ) return -1; - for(j=i+1;;j++){ - while( safe_isspace(pParse->zJson[j]) ){ j++; } - x = jsonParseValue(pParse, j); - if( x<0 ){ - if( x==(-3) && pParse->nNode==(u32)iThis+1 ) return j+1; - return -1; - } - j = x; - while( safe_isspace(pParse->zJson[j]) ){ j++; } - c = pParse->zJson[j]; - if( c==',' ) continue; - if( c!=']' ) return -1; - break; - } - pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1; - return j+1; - }else if( c=='"' ){ - /* Parse string */ - u8 jnFlags = 0; - j = i+1; - for(;;){ - c = pParse->zJson[j]; - if( c==0 ) return -1; - if( c=='\\' ){ - c = pParse->zJson[++j]; - if( c==0 ) return -1; - jnFlags = JNODE_ESCAPE; - }else if( c=='"' ){ - break; - } - j++; - } - jsonParseAddNode(pParse, JSON_STRING, j+1-i, &pParse->zJson[i]); - if( !pParse->oom ) pParse->aNode[pParse->nNode-1].jnFlags = jnFlags; - return j+1; - }else if( c=='n' - && strncmp(pParse->zJson+i,"null",4)==0 - && !safe_isalnum(pParse->zJson[i+4]) ){ - jsonParseAddNode(pParse, JSON_NULL, 0, 0); - return i+4; - }else if( c=='t' - && strncmp(pParse->zJson+i,"true",4)==0 - && !safe_isalnum(pParse->zJson[i+4]) ){ - jsonParseAddNode(pParse, JSON_TRUE, 0, 0); - return i+4; - }else if( c=='f' - && strncmp(pParse->zJson+i,"false",5)==0 - && !safe_isalnum(pParse->zJson[i+5]) ){ - jsonParseAddNode(pParse, JSON_FALSE, 0, 0); - return i+5; - }else if( c=='-' || (c>='0' && c<='9') ){ - /* Parse number */ - u8 seenDP = 0; - u8 seenE = 0; - j = i+1; - for(;; j++){ - c = pParse->zJson[j]; - if( c>='0' && c<='9' ) continue; - if( c=='.' ){ - if( pParse->zJson[j-1]=='-' ) return -1; - if( seenDP ) return -1; - seenDP = 1; - continue; - } - if( c=='e' || c=='E' ){ - if( pParse->zJson[j-1]<'0' ) return -1; - if( seenE ) return -1; - seenDP = seenE = 1; - c = pParse->zJson[j+1]; - if( c=='+' || c=='-' ){ - j++; - c = pParse->zJson[j+1]; - } - if( c<'0' || c>'9' ) return -1; - continue; - } - break; - } - if( pParse->zJson[j-1]<'0' ) return -1; - jsonParseAddNode(pParse, seenDP ? JSON_REAL : JSON_INT, - j - i, &pParse->zJson[i]); - return j; - }else if( c=='}' ){ - return -2; /* End of {...} */ - }else if( c==']' ){ - return -3; /* End of [...] */ - }else if( c==0 ){ - return 0; /* End of file */ - }else{ - return -1; /* Syntax error */ - } -} - -/* -** Parse a complete JSON string. Return 0 on success or non-zero if there -** are any errors. If an error occurs, free all memory associated with -** pParse. -** -** pParse is uninitialized when this routine is called. -*/ -static int jsonParse( - JsonParse *pParse, /* Initialize and fill this JsonParse object */ - sqlite3_context *pCtx, /* Report errors here */ - const char *zJson /* Input JSON text to be parsed */ -){ - int i; - memset(pParse, 0, sizeof(*pParse)); - if( zJson==0 ) return 1; - pParse->zJson = zJson; - i = jsonParseValue(pParse, 0); - if( pParse->oom ) i = -1; - if( i>0 ){ - while( safe_isspace(zJson[i]) ) i++; - if( zJson[i] ) i = -1; - } - if( i<=0 ){ - if( pCtx!=0 ){ - if( pParse->oom ){ - sqlite3_result_error_nomem(pCtx); - }else{ - sqlite3_result_error(pCtx, "malformed JSON", -1); - } - } - jsonParseReset(pParse); - return 1; - } - return 0; -} - -/* Mark node i of pParse as being a child of iParent. Call recursively -** to fill in all the descendants of node i. -*/ -static void jsonParseFillInParentage(JsonParse *pParse, u32 i, u32 iParent){ - JsonNode *pNode = &pParse->aNode[i]; - u32 j; - pParse->aUp[i] = iParent; - switch( pNode->eType ){ - case JSON_ARRAY: { - for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j)){ - jsonParseFillInParentage(pParse, i+j, i); - } - break; - } - case JSON_OBJECT: { - for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j+1)+1){ - pParse->aUp[i+j] = i; - jsonParseFillInParentage(pParse, i+j+1, i); - } - break; - } - default: { - break; - } - } -} - -/* -** Compute the parentage of all nodes in a completed parse. -*/ -static int jsonParseFindParents(JsonParse *pParse){ - u32 *aUp; - assert( pParse->aUp==0 ); - aUp = pParse->aUp = sqlite3_malloc( sizeof(u32)*pParse->nNode ); - if( aUp==0 ){ - pParse->oom = 1; - return SQLITE_NOMEM; - } - jsonParseFillInParentage(pParse, 0, 0); - return SQLITE_OK; -} - -/* -** Compare the OBJECT label at pNode against zKey,nKey. Return true on -** a match. -*/ -static int jsonLabelCompare(JsonNode *pNode, const char *zKey, u32 nKey){ - if( pNode->jnFlags & JNODE_RAW ){ - if( pNode->n!=nKey ) return 0; - return strncmp(pNode->u.zJContent, zKey, nKey)==0; - }else{ - if( pNode->n!=nKey+2 ) return 0; - return strncmp(pNode->u.zJContent+1, zKey, nKey)==0; - } -} - -/* forward declaration */ -static JsonNode *jsonLookupAppend(JsonParse*,const char*,int*,const char**); - -/* -** Search along zPath to find the node specified. Return a pointer -** to that node, or NULL if zPath is malformed or if there is no such -** node. -** -** If pApnd!=0, then try to append new nodes to complete zPath if it is -** possible to do so and if no existing node corresponds to zPath. If -** new nodes are appended *pApnd is set to 1. -*/ -static JsonNode *jsonLookupStep( - JsonParse *pParse, /* The JSON to search */ - u32 iRoot, /* Begin the search at this node */ - const char *zPath, /* The path to search */ - int *pApnd, /* Append nodes to complete path if not NULL */ - const char **pzErr /* Make *pzErr point to any syntax error in zPath */ -){ - u32 i, j, nKey; - const char *zKey; - JsonNode *pRoot = &pParse->aNode[iRoot]; - if( zPath[0]==0 ) return pRoot; - if( zPath[0]=='.' ){ - if( pRoot->eType!=JSON_OBJECT ) return 0; - zPath++; - if( zPath[0]=='"' ){ - zKey = zPath + 1; - for(i=1; zPath[i] && zPath[i]!='"'; i++){} - nKey = i-1; - if( zPath[i] ){ - i++; - }else{ - *pzErr = zPath; - return 0; - } - }else{ - zKey = zPath; - for(i=0; zPath[i] && zPath[i]!='.' && zPath[i]!='['; i++){} - nKey = i; - } - if( nKey==0 ){ - *pzErr = zPath; - return 0; - } - j = 1; - for(;;){ - while( j<=pRoot->n ){ - if( jsonLabelCompare(pRoot+j, zKey, nKey) ){ - return jsonLookupStep(pParse, iRoot+j+1, &zPath[i], pApnd, pzErr); - } - j++; - j += jsonNodeSize(&pRoot[j]); - } - if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break; - iRoot += pRoot->u.iAppend; - pRoot = &pParse->aNode[iRoot]; - j = 1; - } - if( pApnd ){ - u32 iStart, iLabel; - JsonNode *pNode; - iStart = jsonParseAddNode(pParse, JSON_OBJECT, 2, 0); - iLabel = jsonParseAddNode(pParse, JSON_STRING, i, zPath); - zPath += i; - pNode = jsonLookupAppend(pParse, zPath, pApnd, pzErr); - if( pParse->oom ) return 0; - if( pNode ){ - pRoot = &pParse->aNode[iRoot]; - pRoot->u.iAppend = iStart - iRoot; - pRoot->jnFlags |= JNODE_APPEND; - pParse->aNode[iLabel].jnFlags |= JNODE_RAW; - } - return pNode; - } - }else if( zPath[0]=='[' && safe_isdigit(zPath[1]) ){ - if( pRoot->eType!=JSON_ARRAY ) return 0; - i = 0; - j = 1; - while( safe_isdigit(zPath[j]) ){ - i = i*10 + zPath[j] - '0'; - j++; - } - if( zPath[j]!=']' ){ - *pzErr = zPath; - return 0; - } - zPath += j + 1; - j = 1; - for(;;){ - while( j<=pRoot->n && (i>0 || (pRoot[j].jnFlags & JNODE_REMOVE)!=0) ){ - if( (pRoot[j].jnFlags & JNODE_REMOVE)==0 ) i--; - j += jsonNodeSize(&pRoot[j]); - } - if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break; - iRoot += pRoot->u.iAppend; - pRoot = &pParse->aNode[iRoot]; - j = 1; - } - if( j<=pRoot->n ){ - return jsonLookupStep(pParse, iRoot+j, zPath, pApnd, pzErr); - } - if( i==0 && pApnd ){ - u32 iStart; - JsonNode *pNode; - iStart = jsonParseAddNode(pParse, JSON_ARRAY, 1, 0); - pNode = jsonLookupAppend(pParse, zPath, pApnd, pzErr); - if( pParse->oom ) return 0; - if( pNode ){ - pRoot = &pParse->aNode[iRoot]; - pRoot->u.iAppend = iStart - iRoot; - pRoot->jnFlags |= JNODE_APPEND; - } - return pNode; - } - }else{ - *pzErr = zPath; - } - return 0; -} - -/* -** Append content to pParse that will complete zPath. Return a pointer -** to the inserted node, or return NULL if the append fails. -*/ -static JsonNode *jsonLookupAppend( - JsonParse *pParse, /* Append content to the JSON parse */ - const char *zPath, /* Description of content to append */ - int *pApnd, /* Set this flag to 1 */ - const char **pzErr /* Make this point to any syntax error */ -){ - *pApnd = 1; - if( zPath[0]==0 ){ - jsonParseAddNode(pParse, JSON_NULL, 0, 0); - return pParse->oom ? 0 : &pParse->aNode[pParse->nNode-1]; - } - if( zPath[0]=='.' ){ - jsonParseAddNode(pParse, JSON_OBJECT, 0, 0); - }else if( strncmp(zPath,"[0]",3)==0 ){ - jsonParseAddNode(pParse, JSON_ARRAY, 0, 0); - }else{ - return 0; - } - if( pParse->oom ) return 0; - return jsonLookupStep(pParse, pParse->nNode-1, zPath, pApnd, pzErr); -} - -/* -** Return the text of a syntax error message on a JSON path. Space is -** obtained from sqlite3_malloc(). -*/ -static char *jsonPathSyntaxError(const char *zErr){ - return sqlite3_mprintf("JSON path error near '%q'", zErr); -} - -/* -** Do a node lookup using zPath. Return a pointer to the node on success. -** Return NULL if not found or if there is an error. -** -** On an error, write an error message into pCtx and increment the -** pParse->nErr counter. -** -** If pApnd!=NULL then try to append missing nodes and set *pApnd = 1 if -** nodes are appended. -*/ -static JsonNode *jsonLookup( - JsonParse *pParse, /* The JSON to search */ - const char *zPath, /* The path to search */ - int *pApnd, /* Append nodes to complete path if not NULL */ - sqlite3_context *pCtx /* Report errors here, if not NULL */ -){ - const char *zErr = 0; - JsonNode *pNode = 0; - char *zMsg; - - if( zPath==0 ) return 0; - if( zPath[0]!='$' ){ - zErr = zPath; - goto lookup_err; - } - zPath++; - pNode = jsonLookupStep(pParse, 0, zPath, pApnd, &zErr); - if( zErr==0 ) return pNode; - -lookup_err: - pParse->nErr++; - assert( zErr!=0 && pCtx!=0 ); - zMsg = jsonPathSyntaxError(zErr); - if( zMsg ){ - sqlite3_result_error(pCtx, zMsg, -1); - sqlite3_free(zMsg); - }else{ - sqlite3_result_error_nomem(pCtx); - } - return 0; -} - - -/* -** Report the wrong number of arguments for json_insert(), json_replace() -** or json_set(). -*/ -static void jsonWrongNumArgs( - sqlite3_context *pCtx, - const char *zFuncName -){ - char *zMsg = sqlite3_mprintf("json_%s() needs an odd number of arguments", - zFuncName); - sqlite3_result_error(pCtx, zMsg, -1); - sqlite3_free(zMsg); -} - - -/**************************************************************************** -** SQL functions used for testing and debugging -****************************************************************************/ - -#ifdef SQLITE_DEBUG -/* -** The json_parse(JSON) function returns a string which describes -** a parse of the JSON provided. Or it returns NULL if JSON is not -** well-formed. -*/ -static void jsonParseFunc( - sqlite3_context *ctx, - int argc, - sqlite3_value **argv -){ - JsonString s; /* Output string - not real JSON */ - JsonParse x; /* The parse */ - u32 i; - - assert( argc==1 ); - if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return; - jsonParseFindParents(&x); - jsonInit(&s, ctx); - for(i=0; ieType==JSON_ARRAY ){ - assert( (pNode->jnFlags & JNODE_APPEND)==0 ); - for(i=1; i<=pNode->n; n++){ - i += jsonNodeSize(&pNode[i]); - } - } - if( x.nErr==0 ) sqlite3_result_int64(ctx, n); - jsonParseReset(&x); -} - -/* -** json_extract(JSON, PATH, ...) -** -** Return the element described by PATH. Return NULL if there is no -** PATH element. If there are multiple PATHs, then return a JSON array -** with the result from each path. Throw an error if the JSON or any PATH -** is malformed. -*/ -static void jsonExtractFunc( - sqlite3_context *ctx, - int argc, - sqlite3_value **argv -){ - JsonParse x; /* The parse */ - JsonNode *pNode; - const char *zPath; - JsonString jx; - int i; - - if( argc<2 ) return; - if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return; - jsonInit(&jx, ctx); - jsonAppendChar(&jx, '['); - for(i=1; i2 ){ - jsonAppendSeparator(&jx); - if( pNode ){ - jsonRenderNode(pNode, &jx, 0); - }else{ - jsonAppendRaw(&jx, "null", 4); - } - }else if( pNode ){ - jsonReturn(pNode, ctx, 0); - } - } - if( argc>2 && i==argc ){ - jsonAppendChar(&jx, ']'); - jsonResult(&jx); - sqlite3_result_subtype(ctx, JSON_SUBTYPE); - } - jsonReset(&jx); - jsonParseReset(&x); -} - -/* -** Implementation of the json_object(NAME,VALUE,...) function. Return a JSON -** object that contains all name/value given in arguments. Or if any name -** is not a string or if any value is a BLOB, throw an error. -*/ -static void jsonObjectFunc( - sqlite3_context *ctx, - int argc, - sqlite3_value **argv -){ - int i; - JsonString jx; - const char *z; - u32 n; - - if( argc&1 ){ - sqlite3_result_error(ctx, "json_object() requires an even number " - "of arguments", -1); - return; - } - jsonInit(&jx, ctx); - jsonAppendChar(&jx, '{'); - for(i=0; ijnFlags |= JNODE_REMOVE; - } - if( (x.aNode[0].jnFlags & JNODE_REMOVE)==0 ){ - jsonReturnJson(x.aNode, ctx, 0); - } -remove_done: - jsonParseReset(&x); -} - -/* -** json_replace(JSON, PATH, VALUE, ...) -** -** Replace the value at PATH with VALUE. If PATH does not already exist, -** this routine is a no-op. If JSON or PATH is malformed, throw an error. -*/ -static void jsonReplaceFunc( - sqlite3_context *ctx, - int argc, - sqlite3_value **argv -){ - JsonParse x; /* The parse */ - JsonNode *pNode; - const char *zPath; - u32 i; - - if( argc<1 ) return; - if( (argc&1)==0 ) { - jsonWrongNumArgs(ctx, "replace"); - return; - } - if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return; - assert( x.nNode ); - for(i=1; i<(u32)argc; i+=2){ - zPath = (const char*)sqlite3_value_text(argv[i]); - pNode = jsonLookup(&x, zPath, 0, ctx); - if( x.nErr ) goto replace_err; - if( pNode ){ - pNode->jnFlags |= (u8)JNODE_REPLACE; - pNode->iVal = (u8)(i+1); - } - } - if( x.aNode[0].jnFlags & JNODE_REPLACE ){ - sqlite3_result_value(ctx, argv[x.aNode[0].iVal]); - }else{ - jsonReturnJson(x.aNode, ctx, argv); - } -replace_err: - jsonParseReset(&x); -} - -/* -** json_set(JSON, PATH, VALUE, ...) -** -** Set the value at PATH to VALUE. Create the PATH if it does not already -** exist. Overwrite existing values that do exist. -** If JSON or PATH is malformed, throw an error. -** -** json_insert(JSON, PATH, VALUE, ...) -** -** Create PATH and initialize it to VALUE. If PATH already exists, this -** routine is a no-op. If JSON or PATH is malformed, throw an error. -*/ -static void jsonSetFunc( - sqlite3_context *ctx, - int argc, - sqlite3_value **argv -){ - JsonParse x; /* The parse */ - JsonNode *pNode; - const char *zPath; - u32 i; - int bApnd; - int bIsSet = *(int*)sqlite3_user_data(ctx); - - if( argc<1 ) return; - if( (argc&1)==0 ) { - jsonWrongNumArgs(ctx, bIsSet ? "set" : "insert"); - return; - } - if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return; - assert( x.nNode ); - for(i=1; i<(u32)argc; i+=2){ - zPath = (const char*)sqlite3_value_text(argv[i]); - bApnd = 0; - pNode = jsonLookup(&x, zPath, &bApnd, ctx); - if( x.oom ){ - sqlite3_result_error_nomem(ctx); - goto jsonSetDone; - }else if( x.nErr ){ - goto jsonSetDone; - }else if( pNode && (bApnd || bIsSet) ){ - pNode->jnFlags |= (u8)JNODE_REPLACE; - pNode->iVal = (u8)(i+1); - } - } - if( x.aNode[0].jnFlags & JNODE_REPLACE ){ - sqlite3_result_value(ctx, argv[x.aNode[0].iVal]); - }else{ - jsonReturnJson(x.aNode, ctx, argv); - } -jsonSetDone: - jsonParseReset(&x); -} - -/* -** json_type(JSON) -** json_type(JSON, PATH) -** -** Return the top-level "type" of a JSON string. Throw an error if -** either the JSON or PATH inputs are not well-formed. -*/ -static void jsonTypeFunc( - sqlite3_context *ctx, - int argc, - sqlite3_value **argv -){ - JsonParse x; /* The parse */ - const char *zPath; - JsonNode *pNode; - - if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return; - assert( x.nNode ); - if( argc==2 ){ - zPath = (const char*)sqlite3_value_text(argv[1]); - pNode = jsonLookup(&x, zPath, 0, ctx); - }else{ - pNode = x.aNode; - } - if( pNode ){ - sqlite3_result_text(ctx, jsonType[pNode->eType], -1, SQLITE_STATIC); - } - jsonParseReset(&x); -} - -/* -** json_valid(JSON) -** -** Return 1 if JSON is a well-formed JSON string according to RFC-7159. -** Return 0 otherwise. -*/ -static void jsonValidFunc( - sqlite3_context *ctx, - int argc, - sqlite3_value **argv -){ - JsonParse x; /* The parse */ - int rc = 0; - - UNUSED_PARAM(argc); - if( jsonParse(&x, 0, (const char*)sqlite3_value_text(argv[0]))==0 ){ - rc = 1; - } - jsonParseReset(&x); - sqlite3_result_int(ctx, rc); -} - - -/**************************************************************************** -** Aggregate SQL function implementations -****************************************************************************/ -/* -** json_group_array(VALUE) -** -** Return a JSON array composed of all values in the aggregate. -*/ -static void jsonArrayStep( - sqlite3_context *ctx, - int argc, - sqlite3_value **argv -){ - JsonString *pStr; - UNUSED_PARAM(argc); - pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr)); - if( pStr ){ - if( pStr->zBuf==0 ){ - jsonInit(pStr, ctx); - jsonAppendChar(pStr, '['); - }else{ - jsonAppendChar(pStr, ','); - pStr->pCtx = ctx; - } - jsonAppendValue(pStr, argv[0]); - } -} -static void jsonArrayFinal(sqlite3_context *ctx){ - JsonString *pStr; - pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0); - if( pStr ){ - pStr->pCtx = ctx; - jsonAppendChar(pStr, ']'); - if( pStr->bErr ){ - if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx); - assert( pStr->bStatic ); - }else{ - sqlite3_result_text(ctx, pStr->zBuf, pStr->nUsed, - pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free); - pStr->bStatic = 1; - } - }else{ - sqlite3_result_text(ctx, "[]", 2, SQLITE_STATIC); - } - sqlite3_result_subtype(ctx, JSON_SUBTYPE); -} - -/* -** json_group_obj(NAME,VALUE) -** -** Return a JSON object composed of all names and values in the aggregate. -*/ -static void jsonObjectStep( - sqlite3_context *ctx, - int argc, - sqlite3_value **argv -){ - JsonString *pStr; - const char *z; - u32 n; - UNUSED_PARAM(argc); - pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr)); - if( pStr ){ - if( pStr->zBuf==0 ){ - jsonInit(pStr, ctx); - jsonAppendChar(pStr, '{'); - }else{ - jsonAppendChar(pStr, ','); - pStr->pCtx = ctx; - } - z = (const char*)sqlite3_value_text(argv[0]); - n = (u32)sqlite3_value_bytes(argv[0]); - jsonAppendString(pStr, z, n); - jsonAppendChar(pStr, ':'); - jsonAppendValue(pStr, argv[1]); - } -} -static void jsonObjectFinal(sqlite3_context *ctx){ - JsonString *pStr; - pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0); - if( pStr ){ - jsonAppendChar(pStr, '}'); - if( pStr->bErr ){ - if( pStr->bErr==0 ) sqlite3_result_error_nomem(ctx); - assert( pStr->bStatic ); - }else{ - sqlite3_result_text(ctx, pStr->zBuf, pStr->nUsed, - pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free); - pStr->bStatic = 1; - } - }else{ - sqlite3_result_text(ctx, "{}", 2, SQLITE_STATIC); - } - sqlite3_result_subtype(ctx, JSON_SUBTYPE); -} - - -#ifndef SQLITE_OMIT_VIRTUALTABLE -/**************************************************************************** -** The json_each virtual table -****************************************************************************/ -typedef struct JsonEachCursor JsonEachCursor; -struct JsonEachCursor { - sqlite3_vtab_cursor base; /* Base class - must be first */ - u32 iRowid; /* The rowid */ - u32 iBegin; /* The first node of the scan */ - u32 i; /* Index in sParse.aNode[] of current row */ - u32 iEnd; /* EOF when i equals or exceeds this value */ - u8 eType; /* Type of top-level element */ - u8 bRecursive; /* True for json_tree(). False for json_each() */ - char *zJson; /* Input JSON */ - char *zRoot; /* Path by which to filter zJson */ - JsonParse sParse; /* Parse of the input JSON */ -}; - -/* Constructor for the json_each virtual table */ -static int jsonEachConnect( - sqlite3 *db, - void *pAux, - int argc, const char *const*argv, - sqlite3_vtab **ppVtab, - char **pzErr -){ - sqlite3_vtab *pNew; - int rc; - -/* Column numbers */ -#define JEACH_KEY 0 -#define JEACH_VALUE 1 -#define JEACH_TYPE 2 -#define JEACH_ATOM 3 -#define JEACH_ID 4 -#define JEACH_PARENT 5 -#define JEACH_FULLKEY 6 -#define JEACH_PATH 7 -#define JEACH_JSON 8 -#define JEACH_ROOT 9 - - UNUSED_PARAM(pzErr); - UNUSED_PARAM(argv); - UNUSED_PARAM(argc); - UNUSED_PARAM(pAux); - rc = sqlite3_declare_vtab(db, - "CREATE TABLE x(key,value,type,atom,id,parent,fullkey,path," - "json HIDDEN,root HIDDEN)"); - if( rc==SQLITE_OK ){ - pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) ); - if( pNew==0 ) return SQLITE_NOMEM; - memset(pNew, 0, sizeof(*pNew)); - } - return rc; -} - -/* destructor for json_each virtual table */ -static int jsonEachDisconnect(sqlite3_vtab *pVtab){ - sqlite3_free(pVtab); - return SQLITE_OK; -} - -/* constructor for a JsonEachCursor object for json_each(). */ -static int jsonEachOpenEach(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){ - JsonEachCursor *pCur; - - UNUSED_PARAM(p); - pCur = sqlite3_malloc( sizeof(*pCur) ); - if( pCur==0 ) return SQLITE_NOMEM; - memset(pCur, 0, sizeof(*pCur)); - *ppCursor = &pCur->base; - return SQLITE_OK; -} - -/* constructor for a JsonEachCursor object for json_tree(). */ -static int jsonEachOpenTree(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){ - int rc = jsonEachOpenEach(p, ppCursor); - if( rc==SQLITE_OK ){ - JsonEachCursor *pCur = (JsonEachCursor*)*ppCursor; - pCur->bRecursive = 1; - } - return rc; -} - -/* Reset a JsonEachCursor back to its original state. Free any memory -** held. */ -static void jsonEachCursorReset(JsonEachCursor *p){ - sqlite3_free(p->zJson); - sqlite3_free(p->zRoot); - jsonParseReset(&p->sParse); - p->iRowid = 0; - p->i = 0; - p->iEnd = 0; - p->eType = 0; - p->zJson = 0; - p->zRoot = 0; -} - -/* Destructor for a jsonEachCursor object */ -static int jsonEachClose(sqlite3_vtab_cursor *cur){ - JsonEachCursor *p = (JsonEachCursor*)cur; - jsonEachCursorReset(p); - sqlite3_free(cur); - return SQLITE_OK; -} - -/* Return TRUE if the jsonEachCursor object has been advanced off the end -** of the JSON object */ -static int jsonEachEof(sqlite3_vtab_cursor *cur){ - JsonEachCursor *p = (JsonEachCursor*)cur; - return p->i >= p->iEnd; -} - -/* Advance the cursor to the next element for json_tree() */ -static int jsonEachNext(sqlite3_vtab_cursor *cur){ - JsonEachCursor *p = (JsonEachCursor*)cur; - if( p->bRecursive ){ - if( p->sParse.aNode[p->i].jnFlags & JNODE_LABEL ) p->i++; - p->i++; - p->iRowid++; - if( p->iiEnd ){ - u32 iUp = p->sParse.aUp[p->i]; - JsonNode *pUp = &p->sParse.aNode[iUp]; - p->eType = pUp->eType; - if( pUp->eType==JSON_ARRAY ){ - if( iUp==p->i-1 ){ - pUp->u.iKey = 0; - }else{ - pUp->u.iKey++; - } - } - } - }else{ - switch( p->eType ){ - case JSON_ARRAY: { - p->i += jsonNodeSize(&p->sParse.aNode[p->i]); - p->iRowid++; - break; - } - case JSON_OBJECT: { - p->i += 1 + jsonNodeSize(&p->sParse.aNode[p->i+1]); - p->iRowid++; - break; - } - default: { - p->i = p->iEnd; - break; - } - } - } - return SQLITE_OK; -} - -/* Append the name of the path for element i to pStr -*/ -static void jsonEachComputePath( - JsonEachCursor *p, /* The cursor */ - JsonString *pStr, /* Write the path here */ - u32 i /* Path to this element */ -){ - JsonNode *pNode, *pUp; - u32 iUp; - if( i==0 ){ - jsonAppendChar(pStr, '$'); - return; - } - iUp = p->sParse.aUp[i]; - jsonEachComputePath(p, pStr, iUp); - pNode = &p->sParse.aNode[i]; - pUp = &p->sParse.aNode[iUp]; - if( pUp->eType==JSON_ARRAY ){ - jsonPrintf(30, pStr, "[%d]", pUp->u.iKey); - }else{ - assert( pUp->eType==JSON_OBJECT ); - if( (pNode->jnFlags & JNODE_LABEL)==0 ) pNode--; - assert( pNode->eType==JSON_STRING ); - assert( pNode->jnFlags & JNODE_LABEL ); - jsonPrintf(pNode->n+1, pStr, ".%.*s", pNode->n-2, pNode->u.zJContent+1); - } -} - -/* Return the value of a column */ -static int jsonEachColumn( - sqlite3_vtab_cursor *cur, /* The cursor */ - sqlite3_context *ctx, /* First argument to sqlite3_result_...() */ - int i /* Which column to return */ -){ - JsonEachCursor *p = (JsonEachCursor*)cur; - JsonNode *pThis = &p->sParse.aNode[p->i]; - switch( i ){ - case JEACH_KEY: { - if( p->i==0 ) break; - if( p->eType==JSON_OBJECT ){ - jsonReturn(pThis, ctx, 0); - }else if( p->eType==JSON_ARRAY ){ - u32 iKey; - if( p->bRecursive ){ - if( p->iRowid==0 ) break; - iKey = p->sParse.aNode[p->sParse.aUp[p->i]].u.iKey; - }else{ - iKey = p->iRowid; - } - sqlite3_result_int64(ctx, (sqlite3_int64)iKey); - } - break; - } - case JEACH_VALUE: { - if( pThis->jnFlags & JNODE_LABEL ) pThis++; - jsonReturn(pThis, ctx, 0); - break; - } - case JEACH_TYPE: { - if( pThis->jnFlags & JNODE_LABEL ) pThis++; - sqlite3_result_text(ctx, jsonType[pThis->eType], -1, SQLITE_STATIC); - break; - } - case JEACH_ATOM: { - if( pThis->jnFlags & JNODE_LABEL ) pThis++; - if( pThis->eType>=JSON_ARRAY ) break; - jsonReturn(pThis, ctx, 0); - break; - } - case JEACH_ID: { - sqlite3_result_int64(ctx, - (sqlite3_int64)p->i + ((pThis->jnFlags & JNODE_LABEL)!=0)); - break; - } - case JEACH_PARENT: { - if( p->i>p->iBegin && p->bRecursive ){ - sqlite3_result_int64(ctx, (sqlite3_int64)p->sParse.aUp[p->i]); - } - break; - } - case JEACH_FULLKEY: { - JsonString x; - jsonInit(&x, ctx); - if( p->bRecursive ){ - jsonEachComputePath(p, &x, p->i); - }else{ - if( p->zRoot ){ - jsonAppendRaw(&x, p->zRoot, (int)strlen(p->zRoot)); - }else{ - jsonAppendChar(&x, '$'); - } - if( p->eType==JSON_ARRAY ){ - jsonPrintf(30, &x, "[%d]", p->iRowid); - }else{ - jsonPrintf(pThis->n, &x, ".%.*s", pThis->n-2, pThis->u.zJContent+1); - } - } - jsonResult(&x); - break; - } - case JEACH_PATH: { - if( p->bRecursive ){ - JsonString x; - jsonInit(&x, ctx); - jsonEachComputePath(p, &x, p->sParse.aUp[p->i]); - jsonResult(&x); - break; - } - /* For json_each() path and root are the same so fall through - ** into the root case */ - } - case JEACH_ROOT: { - const char *zRoot = p->zRoot; - if( zRoot==0 ) zRoot = "$"; - sqlite3_result_text(ctx, zRoot, -1, SQLITE_STATIC); - break; - } - case JEACH_JSON: { - assert( i==JEACH_JSON ); - sqlite3_result_text(ctx, p->sParse.zJson, -1, SQLITE_STATIC); - break; - } - } - return SQLITE_OK; -} - -/* Return the current rowid value */ -static int jsonEachRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ - JsonEachCursor *p = (JsonEachCursor*)cur; - *pRowid = p->iRowid; - return SQLITE_OK; -} - -/* The query strategy is to look for an equality constraint on the json -** column. Without such a constraint, the table cannot operate. idxNum is -** 1 if the constraint is found, 3 if the constraint and zRoot are found, -** and 0 otherwise. -*/ -static int jsonEachBestIndex( - sqlite3_vtab *tab, - sqlite3_index_info *pIdxInfo -){ - int i; - int jsonIdx = -1; - int rootIdx = -1; - const struct sqlite3_index_constraint *pConstraint; - - UNUSED_PARAM(tab); - pConstraint = pIdxInfo->aConstraint; - for(i=0; inConstraint; i++, pConstraint++){ - if( pConstraint->usable==0 ) continue; - if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue; - switch( pConstraint->iColumn ){ - case JEACH_JSON: jsonIdx = i; break; - case JEACH_ROOT: rootIdx = i; break; - default: /* no-op */ break; - } - } - if( jsonIdx<0 ){ - pIdxInfo->idxNum = 0; - pIdxInfo->estimatedCost = 1e99; - }else{ - pIdxInfo->estimatedCost = 1.0; - pIdxInfo->aConstraintUsage[jsonIdx].argvIndex = 1; - pIdxInfo->aConstraintUsage[jsonIdx].omit = 1; - if( rootIdx<0 ){ - pIdxInfo->idxNum = 1; - }else{ - pIdxInfo->aConstraintUsage[rootIdx].argvIndex = 2; - pIdxInfo->aConstraintUsage[rootIdx].omit = 1; - pIdxInfo->idxNum = 3; - } - } - return SQLITE_OK; -} - -/* Start a search on a new JSON string */ -static int jsonEachFilter( - sqlite3_vtab_cursor *cur, - int idxNum, const char *idxStr, - int argc, sqlite3_value **argv -){ - JsonEachCursor *p = (JsonEachCursor*)cur; - const char *z; - const char *zRoot = 0; - sqlite3_int64 n; - - UNUSED_PARAM(idxStr); - UNUSED_PARAM(argc); - jsonEachCursorReset(p); - if( idxNum==0 ) return SQLITE_OK; - z = (const char*)sqlite3_value_text(argv[0]); - if( z==0 ) return SQLITE_OK; - n = sqlite3_value_bytes(argv[0]); - p->zJson = sqlite3_malloc64( n+1 ); - if( p->zJson==0 ) return SQLITE_NOMEM; - memcpy(p->zJson, z, (size_t)n+1); - if( jsonParse(&p->sParse, 0, p->zJson) ){ - int rc = SQLITE_NOMEM; - if( p->sParse.oom==0 ){ - sqlite3_free(cur->pVtab->zErrMsg); - cur->pVtab->zErrMsg = sqlite3_mprintf("malformed JSON"); - if( cur->pVtab->zErrMsg ) rc = SQLITE_ERROR; - } - jsonEachCursorReset(p); - return rc; - }else if( p->bRecursive && jsonParseFindParents(&p->sParse) ){ - jsonEachCursorReset(p); - return SQLITE_NOMEM; - }else{ - JsonNode *pNode = 0; - if( idxNum==3 ){ - const char *zErr = 0; - zRoot = (const char*)sqlite3_value_text(argv[1]); - if( zRoot==0 ) return SQLITE_OK; - n = sqlite3_value_bytes(argv[1]); - p->zRoot = sqlite3_malloc64( n+1 ); - if( p->zRoot==0 ) return SQLITE_NOMEM; - memcpy(p->zRoot, zRoot, (size_t)n+1); - if( zRoot[0]!='$' ){ - zErr = zRoot; - }else{ - pNode = jsonLookupStep(&p->sParse, 0, p->zRoot+1, 0, &zErr); - } - if( zErr ){ - sqlite3_free(cur->pVtab->zErrMsg); - cur->pVtab->zErrMsg = jsonPathSyntaxError(zErr); - jsonEachCursorReset(p); - return cur->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM; - }else if( pNode==0 ){ - return SQLITE_OK; - } - }else{ - pNode = p->sParse.aNode; - } - p->iBegin = p->i = (int)(pNode - p->sParse.aNode); - p->eType = pNode->eType; - if( p->eType>=JSON_ARRAY ){ - pNode->u.iKey = 0; - p->iEnd = p->i + pNode->n + 1; - if( p->bRecursive ){ - p->eType = p->sParse.aNode[p->sParse.aUp[p->i]].eType; - if( p->i>0 && (p->sParse.aNode[p->i-1].jnFlags & JNODE_LABEL)!=0 ){ - p->i--; - } - }else{ - p->i++; - } - }else{ - p->iEnd = p->i+1; - } - } - return SQLITE_OK; -} - -/* The methods of the json_each virtual table */ -static sqlite3_module jsonEachModule = { - 0, /* iVersion */ - 0, /* xCreate */ - jsonEachConnect, /* xConnect */ - jsonEachBestIndex, /* xBestIndex */ - jsonEachDisconnect, /* xDisconnect */ - 0, /* xDestroy */ - jsonEachOpenEach, /* xOpen - open a cursor */ - jsonEachClose, /* xClose - close a cursor */ - jsonEachFilter, /* xFilter - configure scan constraints */ - jsonEachNext, /* xNext - advance a cursor */ - jsonEachEof, /* xEof - check for end of scan */ - jsonEachColumn, /* xColumn - read data */ - jsonEachRowid, /* xRowid - read data */ - 0, /* xUpdate */ - 0, /* xBegin */ - 0, /* xSync */ - 0, /* xCommit */ - 0, /* xRollback */ - 0, /* xFindMethod */ - 0, /* xRename */ - 0, /* xSavepoint */ - 0, /* xRelease */ - 0 /* xRollbackTo */ -}; - -/* The methods of the json_tree virtual table. */ -static sqlite3_module jsonTreeModule = { - 0, /* iVersion */ - 0, /* xCreate */ - jsonEachConnect, /* xConnect */ - jsonEachBestIndex, /* xBestIndex */ - jsonEachDisconnect, /* xDisconnect */ - 0, /* xDestroy */ - jsonEachOpenTree, /* xOpen - open a cursor */ - jsonEachClose, /* xClose - close a cursor */ - jsonEachFilter, /* xFilter - configure scan constraints */ - jsonEachNext, /* xNext - advance a cursor */ - jsonEachEof, /* xEof - check for end of scan */ - jsonEachColumn, /* xColumn - read data */ - jsonEachRowid, /* xRowid - read data */ - 0, /* xUpdate */ - 0, /* xBegin */ - 0, /* xSync */ - 0, /* xCommit */ - 0, /* xRollback */ - 0, /* xFindMethod */ - 0, /* xRename */ - 0, /* xSavepoint */ - 0, /* xRelease */ - 0 /* xRollbackTo */ -}; -#endif /* SQLITE_OMIT_VIRTUALTABLE */ - -/**************************************************************************** -** The following routines are the only publically visible identifiers in this -** file. Call the following routines in order to register the various SQL -** functions and the virtual table implemented by this file. -****************************************************************************/ - -int sqlite3Json1Init(sqlite3 *db){ - int rc = SQLITE_OK; - unsigned int i; - static const struct { - const char *zName; - int nArg; - int flag; - void (*xFunc)(sqlite3_context*,int,sqlite3_value**); - } aFunc[] = { - { "json", 1, 0, jsonRemoveFunc }, - { "json_array", -1, 0, jsonArrayFunc }, - { "json_array_length", 1, 0, jsonArrayLengthFunc }, - { "json_array_length", 2, 0, jsonArrayLengthFunc }, - { "json_extract", -1, 0, jsonExtractFunc }, - { "json_insert", -1, 0, jsonSetFunc }, - { "json_object", -1, 0, jsonObjectFunc }, - { "json_remove", -1, 0, jsonRemoveFunc }, - { "json_replace", -1, 0, jsonReplaceFunc }, - { "json_set", -1, 1, jsonSetFunc }, - { "json_type", 1, 0, jsonTypeFunc }, - { "json_type", 2, 0, jsonTypeFunc }, - { "json_valid", 1, 0, jsonValidFunc }, - -#if SQLITE_DEBUG - /* DEBUG and TESTING functions */ - { "json_parse", 1, 0, jsonParseFunc }, - { "json_test1", 1, 0, jsonTest1Func }, -#endif - }; - static const struct { - const char *zName; - int nArg; - void (*xStep)(sqlite3_context*,int,sqlite3_value**); - void (*xFinal)(sqlite3_context*); - } aAgg[] = { - { "json_group_array", 1, jsonArrayStep, jsonArrayFinal }, - { "json_group_object", 2, jsonObjectStep, jsonObjectFinal }, - }; -#ifndef SQLITE_OMIT_VIRTUALTABLE - static const struct { - const char *zName; - sqlite3_module *pModule; - } aMod[] = { - { "json_each", &jsonEachModule }, - { "json_tree", &jsonTreeModule }, - }; -#endif - for(i=0; i +#include + +#ifndef SQLITE_OMIT_VIRTUALTABLE + +/* memstat_vtab is a subclass of sqlite3_vtab which will +** serve as the underlying representation of a memstat virtual table +*/ +typedef struct memstat_vtab memstat_vtab; +struct memstat_vtab { + sqlite3_vtab base; /* Base class - must be first */ + sqlite3 *db; /* Database connection for this memstat vtab */ +}; + +/* memstat_cursor is a subclass of sqlite3_vtab_cursor which will +** serve as the underlying representation of a cursor that scans +** over rows of the result +*/ +typedef struct memstat_cursor memstat_cursor; +struct memstat_cursor { + sqlite3_vtab_cursor base; /* Base class - must be first */ + sqlite3 *db; /* Database connection for this cursor */ + int iRowid; /* Current row in aMemstatColumn[] */ + int iDb; /* Which schema we are looking at */ + int nDb; /* Number of schemas */ + char **azDb; /* Names of all schemas */ + sqlite3_int64 aVal[2]; /* Result values */ +}; + +/* +** The memstatConnect() method is invoked to create a new +** memstat_vtab that describes the memstat virtual table. +** +** Think of this routine as the constructor for memstat_vtab objects. +** +** All this routine needs to do is: +** +** (1) Allocate the memstat_vtab object and initialize all fields. +** +** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the +** result set of queries against memstat will look like. +*/ +static int memstatConnect( + sqlite3 *db, + void *pAux, + int argc, const char *const*argv, + sqlite3_vtab **ppVtab, + char **pzErr +){ + memstat_vtab *pNew; + int rc; + +/* Column numbers */ +#define MSV_COLUMN_NAME 0 /* Name of quantity being measured */ +#define MSV_COLUMN_SCHEMA 1 /* schema name */ +#define MSV_COLUMN_VALUE 2 /* Current value */ +#define MSV_COLUMN_HIWTR 3 /* Highwater mark */ + + rc = sqlite3_declare_vtab(db,"CREATE TABLE x(name,schema,value,hiwtr)"); + if( rc==SQLITE_OK ){ + pNew = sqlite3_malloc( sizeof(*pNew) ); + *ppVtab = (sqlite3_vtab*)pNew; + if( pNew==0 ) return SQLITE_NOMEM; + memset(pNew, 0, sizeof(*pNew)); + pNew->db = db; + } + return rc; +} + +/* +** This method is the destructor for memstat_cursor objects. +*/ +static int memstatDisconnect(sqlite3_vtab *pVtab){ + sqlite3_free(pVtab); + return SQLITE_OK; +} + +/* +** Constructor for a new memstat_cursor object. +*/ +static int memstatOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){ + memstat_cursor *pCur; + pCur = sqlite3_malloc( sizeof(*pCur) ); + if( pCur==0 ) return SQLITE_NOMEM; + memset(pCur, 0, sizeof(*pCur)); + pCur->db = ((memstat_vtab*)p)->db; + *ppCursor = &pCur->base; + return SQLITE_OK; +} + +/* +** Clear all the schema names from a cursor +*/ +static void memstatClearSchema(memstat_cursor *pCur){ + int i; + if( pCur->azDb==0 ) return; + for(i=0; inDb; i++){ + sqlite3_free(pCur->azDb[i]); + } + sqlite3_free(pCur->azDb); + pCur->azDb = 0; + pCur->nDb = 0; +} + +/* +** Fill in the azDb[] array for the cursor. +*/ +static int memstatFindSchemas(memstat_cursor *pCur){ + sqlite3_stmt *pStmt = 0; + int rc; + if( pCur->nDb ) return SQLITE_OK; + rc = sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pStmt, 0); + if( rc ){ + sqlite3_finalize(pStmt); + return rc; + } + while( sqlite3_step(pStmt)==SQLITE_ROW ){ + char **az, *z; + az = sqlite3_realloc64(pCur->azDb, sizeof(char*)*(pCur->nDb+1)); + if( az==0 ){ + memstatClearSchema(pCur); + return SQLITE_NOMEM; + } + pCur->azDb = az; + z = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1)); + if( z==0 ){ + memstatClearSchema(pCur); + return SQLITE_NOMEM; + } + pCur->azDb[pCur->nDb] = z; + pCur->nDb++; + } + sqlite3_finalize(pStmt); + return SQLITE_OK; +} + + +/* +** Destructor for a memstat_cursor. +*/ +static int memstatClose(sqlite3_vtab_cursor *cur){ + memstat_cursor *pCur = (memstat_cursor*)cur; + memstatClearSchema(pCur); + sqlite3_free(cur); + return SQLITE_OK; +} + + +/* +** Allowed values for aMemstatColumn[].eType +*/ +#define MSV_GSTAT 0 /* sqlite3_status64() information */ +#define MSV_DB 1 /* sqlite3_db_status() information */ +#define MSV_ZIPVFS 2 /* ZIPVFS file-control with 64-bit return */ + +/* +** An array of quantities that can be measured and reported by +** this virtual table +*/ +static const struct MemstatColumns { + const char *zName; /* Symbolic name */ + unsigned char eType; /* Type of interface */ + unsigned char mNull; /* Bitmask of which columns are NULL */ + /* 2: dbname, 4: current, 8: hiwtr */ + int eOp; /* Opcode */ +} aMemstatColumn[] = { + {"MEMORY_USED", MSV_GSTAT, 2, SQLITE_STATUS_MEMORY_USED }, + {"MALLOC_SIZE", MSV_GSTAT, 6, SQLITE_STATUS_MALLOC_SIZE }, + {"MALLOC_COUNT", MSV_GSTAT, 2, SQLITE_STATUS_MALLOC_COUNT }, + {"PAGECACHE_USED", MSV_GSTAT, 2, SQLITE_STATUS_PAGECACHE_USED }, + {"PAGECACHE_OVERFLOW", MSV_GSTAT, 2, SQLITE_STATUS_PAGECACHE_OVERFLOW }, + {"PAGECACHE_SIZE", MSV_GSTAT, 6, SQLITE_STATUS_PAGECACHE_SIZE }, + {"PARSER_STACK", MSV_GSTAT, 6, SQLITE_STATUS_PARSER_STACK }, + {"DB_LOOKASIDE_USED", MSV_DB, 2, SQLITE_DBSTATUS_LOOKASIDE_USED }, + {"DB_LOOKASIDE_HIT", MSV_DB, 6, SQLITE_DBSTATUS_LOOKASIDE_HIT }, + {"DB_LOOKASIDE_MISS_SIZE", MSV_DB, 6, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE}, + {"DB_LOOKASIDE_MISS_FULL", MSV_DB, 6, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL}, + {"DB_CACHE_USED", MSV_DB, 10, SQLITE_DBSTATUS_CACHE_USED }, +#if SQLITE_VERSION_NUMBER >= 3140000 + {"DB_CACHE_USED_SHARED", MSV_DB, 10, SQLITE_DBSTATUS_CACHE_USED_SHARED }, +#endif + {"DB_SCHEMA_USED", MSV_DB, 10, SQLITE_DBSTATUS_SCHEMA_USED }, + {"DB_STMT_USED", MSV_DB, 10, SQLITE_DBSTATUS_STMT_USED }, + {"DB_CACHE_HIT", MSV_DB, 10, SQLITE_DBSTATUS_CACHE_HIT }, + {"DB_CACHE_MISS", MSV_DB, 10, SQLITE_DBSTATUS_CACHE_MISS }, + {"DB_CACHE_WRITE", MSV_DB, 10, SQLITE_DBSTATUS_CACHE_WRITE }, +#if SQLITE_VERSION_NUMBER >= 3230000 + {"DB_CACHE_SPILL", MSV_DB, 10, SQLITE_DBSTATUS_CACHE_SPILL }, +#endif + {"DB_DEFERRED_FKS", MSV_DB, 10, SQLITE_DBSTATUS_DEFERRED_FKS }, +#ifdef SQLITE_ENABLE_ZIPVFS + {"ZIPVFS_CACHE_USED", MSV_ZIPVFS, 8, 231454 }, + {"ZIPVFS_CACHE_HIT", MSV_ZIPVFS, 8, 231455 }, + {"ZIPVFS_CACHE_MISS", MSV_ZIPVFS, 8, 231456 }, + {"ZIPVFS_CACHE_WRITE", MSV_ZIPVFS, 8, 231457 }, + {"ZIPVFS_DIRECT_READ", MSV_ZIPVFS, 8, 231458 }, + {"ZIPVFS_DIRECT_BYTES", MSV_ZIPVFS, 8, 231459 }, +#endif /* SQLITE_ENABLE_ZIPVFS */ +}; +#define MSV_NROW (sizeof(aMemstatColumn)/sizeof(aMemstatColumn[0])) + +/* +** Advance a memstat_cursor to its next row of output. +*/ +static int memstatNext(sqlite3_vtab_cursor *cur){ + memstat_cursor *pCur = (memstat_cursor*)cur; + int i; + assert( pCur->iRowid<=MSV_NROW ); + while(1){ + i = (int)pCur->iRowid - 1; + if( i<0 || (aMemstatColumn[i].mNull & 2)!=0 || (++pCur->iDb)>=pCur->nDb ){ + pCur->iRowid++; + if( pCur->iRowid>MSV_NROW ) return SQLITE_OK; /* End of the table */ + pCur->iDb = 0; + i++; + } + pCur->aVal[0] = 0; + pCur->aVal[1] = 0; + switch( aMemstatColumn[i].eType ){ + case MSV_GSTAT: { + if( sqlite3_libversion_number()>=3010000 ){ + sqlite3_status64(aMemstatColumn[i].eOp, + &pCur->aVal[0], &pCur->aVal[1],0); + }else{ + int xCur, xHiwtr; + sqlite3_status(aMemstatColumn[i].eOp, &xCur, &xHiwtr, 0); + pCur->aVal[0] = xCur; + pCur->aVal[1] = xHiwtr; + } + break; + } + case MSV_DB: { + int xCur, xHiwtr; + sqlite3_db_status(pCur->db, aMemstatColumn[i].eOp, &xCur, &xHiwtr, 0); + pCur->aVal[0] = xCur; + pCur->aVal[1] = xHiwtr; + break; + } + case MSV_ZIPVFS: { + int rc; + rc = sqlite3_file_control(pCur->db, pCur->azDb[pCur->iDb], + aMemstatColumn[i].eOp, (void*)&pCur->aVal[0]); + if( rc!=SQLITE_OK ) continue; + break; + } + } + break; + } + return SQLITE_OK; +} + + +/* +** Return values of columns for the row at which the memstat_cursor +** is currently pointing. +*/ +static int memstatColumn( + sqlite3_vtab_cursor *cur, /* The cursor */ + sqlite3_context *ctx, /* First argument to sqlite3_result_...() */ + int iCol /* Which column to return */ +){ + memstat_cursor *pCur = (memstat_cursor*)cur; + int i; + assert( pCur->iRowid>0 && pCur->iRowid<=MSV_NROW ); + i = (int)pCur->iRowid - 1; + if( (aMemstatColumn[i].mNull & (1<azDb[pCur->iDb], -1, 0); + break; + } + case MSV_COLUMN_VALUE: { + sqlite3_result_int64(ctx, pCur->aVal[0]); + break; + } + case MSV_COLUMN_HIWTR: { + sqlite3_result_int64(ctx, pCur->aVal[1]); + break; + } + } + return SQLITE_OK; +} + +/* +** Return the rowid for the current row. In this implementation, the +** rowid is the same as the output value. +*/ +static int memstatRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ + memstat_cursor *pCur = (memstat_cursor*)cur; + *pRowid = pCur->iRowid*1000 + pCur->iDb; + return SQLITE_OK; +} + +/* +** Return TRUE if the cursor has been moved off of the last +** row of output. +*/ +static int memstatEof(sqlite3_vtab_cursor *cur){ + memstat_cursor *pCur = (memstat_cursor*)cur; + return pCur->iRowid>MSV_NROW; +} + +/* +** This method is called to "rewind" the memstat_cursor object back +** to the first row of output. This method is always called at least +** once prior to any call to memstatColumn() or memstatRowid() or +** memstatEof(). +*/ +static int memstatFilter( + sqlite3_vtab_cursor *pVtabCursor, + int idxNum, const char *idxStr, + int argc, sqlite3_value **argv +){ + memstat_cursor *pCur = (memstat_cursor *)pVtabCursor; + int rc = memstatFindSchemas(pCur); + if( rc ) return rc; + pCur->iRowid = 0; + pCur->iDb = 0; + return memstatNext(pVtabCursor); +} + +/* +** SQLite will invoke this method one or more times while planning a query +** that uses the memstat virtual table. This routine needs to create +** a query plan for each invocation and compute an estimated cost for that +** plan. +*/ +static int memstatBestIndex( + sqlite3_vtab *tab, + sqlite3_index_info *pIdxInfo +){ + pIdxInfo->estimatedCost = (double)500; + pIdxInfo->estimatedRows = 500; + return SQLITE_OK; +} + +/* +** This following structure defines all the methods for the +** memstat virtual table. +*/ +static sqlite3_module memstatModule = { + 0, /* iVersion */ + 0, /* xCreate */ + memstatConnect, /* xConnect */ + memstatBestIndex, /* xBestIndex */ + memstatDisconnect, /* xDisconnect */ + 0, /* xDestroy */ + memstatOpen, /* xOpen - open a cursor */ + memstatClose, /* xClose - close a cursor */ + memstatFilter, /* xFilter - configure scan constraints */ + memstatNext, /* xNext - advance a cursor */ + memstatEof, /* xEof - check for end of scan */ + memstatColumn, /* xColumn - read data */ + memstatRowid, /* xRowid - read data */ + 0, /* xUpdate */ + 0, /* xBegin */ + 0, /* xSync */ + 0, /* xCommit */ + 0, /* xRollback */ + 0, /* xFindMethod */ + 0, /* xRename */ + 0, /* xSavepoint */ + 0, /* xRelease */ + 0, /* xRollbackTo */ + 0, /* xShadowName */ + 0 /* xIntegrity */ +}; + +#endif /* SQLITE_OMIT_VIRTUALTABLE */ + +int sqlite3MemstatVtabInit( + sqlite3 *db, + char **NotUsed1, + const sqlite3_api_routines *NotUsed2 +){ + int rc = SQLITE_OK; + (void)NotUsed1; + (void)NotUsed2; +#ifndef SQLITE_OMIT_VIRTUALTABLE + rc = sqlite3_create_module(db, "sqlite_memstat", &memstatModule, 0); +#endif + return rc; +} + +#ifndef SQLITE_CORE +#ifdef _WIN32 +__declspec(dllexport) +#endif +int sqlite3_memstat_init( + sqlite3 *db, + char **pzErrMsg, + const sqlite3_api_routines *pApi +){ + int rc = SQLITE_OK; + SQLITE_EXTENSION_INIT2(pApi); +#ifndef SQLITE_OMIT_VIRTUALTABLE + rc = sqlite3MemstatVtabInit(db, 0, 0); +#endif + return rc; +} +#endif /* SQLITE_CORE */ +#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_MEMSTATVTAB) */ diff --git a/ext/misc/memtrace.c b/ext/misc/memtrace.c new file mode 100644 index 0000000000..19ff4c4170 --- /dev/null +++ b/ext/misc/memtrace.c @@ -0,0 +1,108 @@ +/* +** 2019-01-21 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** This file implements an extension that uses the SQLITE_CONFIG_MALLOC +** mechanism to add a tracing layer on top of SQLite. If this extension +** is registered prior to sqlite3_initialize(), it will cause all memory +** allocation activities to be logged on standard output, or to some other +** FILE specified by the initializer. +** +** This file needs to be compiled into the application that uses it. +** +** This extension is used to implement the --memtrace option of the +** command-line shell. +*/ +#include +#include +#include + +/* The original memory allocation routines */ +static sqlite3_mem_methods memtraceBase; +static FILE *memtraceOut; + +/* Methods that trace memory allocations */ +static void *memtraceMalloc(int n){ + if( memtraceOut ){ + fprintf(memtraceOut, "MEMTRACE: allocate %d bytes\n", + memtraceBase.xRoundup(n)); + } + return memtraceBase.xMalloc(n); +} +static void memtraceFree(void *p){ + if( p==0 ) return; + if( memtraceOut ){ + fprintf(memtraceOut, "MEMTRACE: free %d bytes\n", memtraceBase.xSize(p)); + } + memtraceBase.xFree(p); +} +static void *memtraceRealloc(void *p, int n){ + if( p==0 ) return memtraceMalloc(n); + if( n==0 ){ + memtraceFree(p); + return 0; + } + if( memtraceOut ){ + fprintf(memtraceOut, "MEMTRACE: resize %d -> %d bytes\n", + memtraceBase.xSize(p), memtraceBase.xRoundup(n)); + } + return memtraceBase.xRealloc(p, n); +} +static int memtraceSize(void *p){ + return memtraceBase.xSize(p); +} +static int memtraceRoundup(int n){ + return memtraceBase.xRoundup(n); +} +static int memtraceInit(void *p){ + return memtraceBase.xInit(p); +} +static void memtraceShutdown(void *p){ + memtraceBase.xShutdown(p); +} + +/* The substitute memory allocator */ +static sqlite3_mem_methods ersaztMethods = { + memtraceMalloc, + memtraceFree, + memtraceRealloc, + memtraceSize, + memtraceRoundup, + memtraceInit, + memtraceShutdown, + 0 +}; + +/* Begin tracing memory allocations to out. */ +int sqlite3MemTraceActivate(FILE *out){ + int rc = SQLITE_OK; + if( memtraceBase.xMalloc==0 ){ + rc = sqlite3_config(SQLITE_CONFIG_GETMALLOC, &memtraceBase); + if( rc==SQLITE_OK ){ + rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &ersaztMethods); + } + } + memtraceOut = out; + return rc; +} + +/* Deactivate memory tracing */ +int sqlite3MemTraceDeactivate(void){ + int rc = SQLITE_OK; + if( memtraceBase.xMalloc!=0 ){ + rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &memtraceBase); + if( rc==SQLITE_OK ){ + memset(&memtraceBase, 0, sizeof(memtraceBase)); + } + } + memtraceOut = 0; + return rc; +} diff --git a/ext/misc/mmapwarm.c b/ext/misc/mmapwarm.c new file mode 100644 index 0000000000..851f8b0eb7 --- /dev/null +++ b/ext/misc/mmapwarm.c @@ -0,0 +1,108 @@ +/* +** 2017-09-18 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +*/ + +#include "sqlite3.h" + + +/* +** This function is used to touch each page of a mapping of a memory +** mapped SQLite database. Assuming that the system has sufficient free +** memory and supports sufficiently large mappings, this causes the OS +** to cache the entire database in main memory, making subsequent +** database accesses faster. +** +** If the second parameter to this function is not NULL, it is the name of +** the specific database to operate on (i.e. "main" or the name of an +** attached database). +** +** SQLITE_OK is returned if successful, or an SQLite error code otherwise. +** It is not considered an error if the file is not memory-mapped, or if +** the mapping does not span the entire file. If an error does occur, a +** transaction may be left open on the database file. +** +** It is illegal to call this function when the database handle has an +** open transaction. SQLITE_MISUSE is returned in this case. +*/ +int sqlite3_mmap_warm(sqlite3 *db, const char *zDb){ + int rc = SQLITE_OK; + char *zSql = 0; + int pgsz = 0; + unsigned int nTotal = 0; + + if( 0==sqlite3_get_autocommit(db) ) return SQLITE_MISUSE; + + /* Open a read-only transaction on the file in question */ + zSql = sqlite3_mprintf("BEGIN; SELECT * FROM %s%q%ssqlite_schema", + (zDb ? "'" : ""), (zDb ? zDb : ""), (zDb ? "'." : "") + ); + if( zSql==0 ) return SQLITE_NOMEM; + rc = sqlite3_exec(db, zSql, 0, 0, 0); + sqlite3_free(zSql); + + /* Find the SQLite page size of the file */ + if( rc==SQLITE_OK ){ + zSql = sqlite3_mprintf("PRAGMA %s%q%spage_size", + (zDb ? "'" : ""), (zDb ? zDb : ""), (zDb ? "'." : "") + ); + if( zSql==0 ){ + rc = SQLITE_NOMEM; + }else{ + sqlite3_stmt *pPgsz = 0; + rc = sqlite3_prepare_v2(db, zSql, -1, &pPgsz, 0); + sqlite3_free(zSql); + if( rc==SQLITE_OK ){ + if( sqlite3_step(pPgsz)==SQLITE_ROW ){ + pgsz = sqlite3_column_int(pPgsz, 0); + } + rc = sqlite3_finalize(pPgsz); + } + if( rc==SQLITE_OK && pgsz==0 ){ + rc = SQLITE_ERROR; + } + } + } + + /* Touch each mmap'd page of the file */ + if( rc==SQLITE_OK ){ + int rc2; + sqlite3_file *pFd = 0; + rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_FILE_POINTER, &pFd); + if( rc==SQLITE_OK && pFd->pMethods->iVersion>=3 ){ + sqlite3_int64 iPg = 1; + sqlite3_io_methods const *p = pFd->pMethods; + while( 1 ){ + unsigned char *pMap; + rc = p->xFetch(pFd, pgsz*iPg, pgsz, (void**)&pMap); + if( rc!=SQLITE_OK || pMap==0 ) break; + + nTotal += (unsigned int)pMap[0]; + nTotal += (unsigned int)pMap[pgsz-1]; + + rc = p->xUnfetch(pFd, pgsz*iPg, (void*)pMap); + if( rc!=SQLITE_OK ) break; + iPg++; + } + sqlite3_log(SQLITE_OK, + "sqlite3_mmap_warm_cache: Warmed up %d pages of %s", iPg==1?0:iPg, + sqlite3_db_filename(db, zDb) + ); + } + + rc2 = sqlite3_exec(db, "END", 0, 0, 0); + if( rc==SQLITE_OK ) rc = rc2; + } + + (void)nTotal; + return rc; +} diff --git a/ext/misc/nextchar.c b/ext/misc/nextchar.c index 49dfd24f1f..60fa3db94d 100644 --- a/ext/misc/nextchar.c +++ b/ext/misc/nextchar.c @@ -85,7 +85,7 @@ static void nextCharAppend(nextCharContext *p, unsigned c){ if( p->nUsed+1 > p->nAlloc ){ unsigned int *aNew; int n = p->nAlloc*2 + 30; - aNew = sqlite3_realloc(p->aResult, n*sizeof(unsigned int)); + aNew = sqlite3_realloc64(p->aResult, n*sizeof(unsigned int)); if( aNew==0 ){ p->mallocFailed = 1; return; @@ -269,7 +269,7 @@ static void nextCharFunc( sqlite3_result_error_nomem(context); }else{ unsigned char *pRes; - pRes = sqlite3_malloc( c.nUsed*4 + 1 ); + pRes = sqlite3_malloc64( c.nUsed*4 + 1 ); if( pRes==0 ){ sqlite3_result_error_nomem(context); }else{ @@ -297,14 +297,17 @@ int sqlite3_nextchar_init( int rc = SQLITE_OK; SQLITE_EXTENSION_INIT2(pApi); (void)pzErrMsg; /* Unused parameter */ - rc = sqlite3_create_function(db, "next_char", 3, SQLITE_UTF8, 0, + rc = sqlite3_create_function(db, "next_char", 3, + SQLITE_UTF8|SQLITE_INNOCUOUS, 0, nextCharFunc, 0, 0); if( rc==SQLITE_OK ){ - rc = sqlite3_create_function(db, "next_char", 4, SQLITE_UTF8, 0, + rc = sqlite3_create_function(db, "next_char", 4, + SQLITE_UTF8|SQLITE_INNOCUOUS, 0, nextCharFunc, 0, 0); } if( rc==SQLITE_OK ){ - rc = sqlite3_create_function(db, "next_char", 5, SQLITE_UTF8, 0, + rc = sqlite3_create_function(db, "next_char", 5, + SQLITE_UTF8|SQLITE_INNOCUOUS, 0, nextCharFunc, 0, 0); } return rc; diff --git a/ext/misc/noop.c b/ext/misc/noop.c new file mode 100644 index 0000000000..18c25e10f7 --- /dev/null +++ b/ext/misc/noop.c @@ -0,0 +1,90 @@ +/* +** 2020-01-08 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +** This SQLite extension implements a noop() function used for testing. +** +** Variants: +** +** noop(X) The default. Deterministic. +** noop_i(X) Deterministic and innocuous. +** noop_do(X) Deterministic and direct-only. +** noop_nd(X) Non-deterministic. +*/ +#include "sqlite3ext.h" +SQLITE_EXTENSION_INIT1 +#include +#include + +/* +** Implementation of the noop() function. +** +** The function returns its argument, unchanged. +*/ +static void noopfunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + assert( argc==1 ); + sqlite3_result_value(context, argv[0]); +} + +/* +** Implementation of the multitype_text() function. +** +** The function returns its argument. The result will always have a +** TEXT value. But if the original input is numeric, it will also +** have that numeric value. +*/ +static void multitypeTextFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + assert( argc==1 ); + (void)argc; + (void)sqlite3_value_text(argv[0]); + sqlite3_result_value(context, argv[0]); +} + +#ifdef _WIN32 +__declspec(dllexport) +#endif +int sqlite3_noop_init( + sqlite3 *db, + char **pzErrMsg, + const sqlite3_api_routines *pApi +){ + int rc = SQLITE_OK; + SQLITE_EXTENSION_INIT2(pApi); + (void)pzErrMsg; /* Unused parameter */ + rc = sqlite3_create_function(db, "noop", 1, + SQLITE_UTF8 | SQLITE_DETERMINISTIC, + 0, noopfunc, 0, 0); + if( rc ) return rc; + rc = sqlite3_create_function(db, "noop_i", 1, + SQLITE_UTF8 | SQLITE_DETERMINISTIC | SQLITE_INNOCUOUS, + 0, noopfunc, 0, 0); + if( rc ) return rc; + rc = sqlite3_create_function(db, "noop_do", 1, + SQLITE_UTF8 | SQLITE_DETERMINISTIC | SQLITE_DIRECTONLY, + 0, noopfunc, 0, 0); + if( rc ) return rc; + rc = sqlite3_create_function(db, "noop_nd", 1, + SQLITE_UTF8, + 0, noopfunc, 0, 0); + if( rc ) return rc; + rc = sqlite3_create_function(db, "multitype_text", 1, + SQLITE_UTF8, + 0, multitypeTextFunc, 0, 0); + return rc; +} diff --git a/ext/misc/normalize.c b/ext/misc/normalize.c new file mode 100644 index 0000000000..44ddcd3882 --- /dev/null +++ b/ext/misc/normalize.c @@ -0,0 +1,717 @@ +/* +** 2018-01-08 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +** This file contains code to implement the sqlite3_normalize() function. +** +** char *sqlite3_normalize(const char *zSql); +** +** This function takes an SQL string as input and returns a "normalized" +** version of that string in memory obtained from sqlite3_malloc64(). The +** caller is responsible for ensuring that the returned memory is freed. +** +** If a memory allocation error occurs, this routine returns NULL. +** +** The normalization consists of the following transformations: +** +** (1) Convert every literal (string, blob literal, numeric constant, +** or "NULL" constant) into a ? +** +** (2) Remove all superfluous whitespace, including comments. Change +** all required whitespace to a single space character. +** +** (3) Lowercase all ASCII characters. +** +** (4) If an IN or NOT IN operator is followed by a list of 1 or more +** values, convert that list into "(?,?,?)". +** +** The purpose of normalization is two-fold: +** +** (1) Sanitize queries by removing potentially private or sensitive +** information contained in literals. +** +** (2) Identify structurally identical queries by comparing their +** normalized forms. +** +** Command-Line Utility +** -------------------- +** +** This file also contains code for a command-line utility that converts +** SQL queries in text files into their normalized forms. To build the +** command-line program, compile this file with -DSQLITE_NORMALIZE_CLI +** and link it against the SQLite library. +*/ +#include +#include + +/* +** Implementation note: +** +** Much of the tokenizer logic is copied out of the tokenize.c source file +** of SQLite. That logic could be simplified for this particular application, +** but that would impose a risk of introducing subtle errors. It is best to +** keep the code as close to the original as possible. +** +** The tokenize code is in sync with the SQLite core as of 2018-01-08. +** Any future changes to the core tokenizer might require corresponding +** adjustments to the tokenizer logic in this module. +*/ + + +/* Character classes for tokenizing +** +** In the sqlite3GetToken() function, a switch() on aiClass[c] is implemented +** using a lookup table, whereas a switch() directly on c uses a binary search. +** The lookup table is much faster. To maximize speed, and to ensure that +** a lookup table is used, all of the classes need to be small integers and +** all of them need to be used within the switch. +*/ +#define CC_X 0 /* The letter 'x', or start of BLOB literal */ +#define CC_KYWD 1 /* Alphabetics or '_'. Usable in a keyword */ +#define CC_ID 2 /* unicode characters usable in IDs */ +#define CC_DIGIT 3 /* Digits */ +#define CC_DOLLAR 4 /* '$' */ +#define CC_VARALPHA 5 /* '@', '#', ':'. Alphabetic SQL variables */ +#define CC_VARNUM 6 /* '?'. Numeric SQL variables */ +#define CC_SPACE 7 /* Space characters */ +#define CC_QUOTE 8 /* '"', '\'', or '`'. String literals, quoted ids */ +#define CC_QUOTE2 9 /* '['. [...] style quoted ids */ +#define CC_PIPE 10 /* '|'. Bitwise OR or concatenate */ +#define CC_MINUS 11 /* '-'. Minus or SQL-style comment */ +#define CC_LT 12 /* '<'. Part of < or <= or <> */ +#define CC_GT 13 /* '>'. Part of > or >= */ +#define CC_EQ 14 /* '='. Part of = or == */ +#define CC_BANG 15 /* '!'. Part of != */ +#define CC_SLASH 16 /* '/'. / or c-style comment */ +#define CC_LP 17 /* '(' */ +#define CC_RP 18 /* ')' */ +#define CC_SEMI 19 /* ';' */ +#define CC_PLUS 20 /* '+' */ +#define CC_STAR 21 /* '*' */ +#define CC_PERCENT 22 /* '%' */ +#define CC_COMMA 23 /* ',' */ +#define CC_AND 24 /* '&' */ +#define CC_TILDA 25 /* '~' */ +#define CC_DOT 26 /* '.' */ +#define CC_ILLEGAL 27 /* Illegal character */ + +static const unsigned char aiClass[] = { +/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */ +/* 0x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 7, 7, 27, 7, 7, 27, 27, +/* 1x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, +/* 2x */ 7, 15, 8, 5, 4, 22, 24, 8, 17, 18, 21, 20, 23, 11, 26, 16, +/* 3x */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 19, 12, 14, 13, 6, +/* 4x */ 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, +/* 5x */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 9, 27, 27, 27, 1, +/* 6x */ 8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, +/* 7x */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 27, 10, 27, 25, 27, +/* 8x */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, +/* 9x */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, +/* Ax */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, +/* Bx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, +/* Cx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, +/* Dx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, +/* Ex */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, +/* Fx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 +}; + +/* An array to map all upper-case characters into their corresponding +** lower-case character. +** +** SQLite only considers US-ASCII (or EBCDIC) characters. We do not +** handle case conversions for the UTF character set since the tables +** involved are nearly as big or bigger than SQLite itself. +*/ +static const unsigned char sqlite3UpperToLower[] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, + 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, + 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, + 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103, + 104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121, + 122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107, + 108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125, + 126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, + 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161, + 162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179, + 180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197, + 198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215, + 216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233, + 234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251, + 252,253,254,255 +}; + +/* +** The following 256 byte lookup table is used to support SQLites built-in +** equivalents to the following standard library functions: +** +** isspace() 0x01 +** isalpha() 0x02 +** isdigit() 0x04 +** isalnum() 0x06 +** isxdigit() 0x08 +** toupper() 0x20 +** SQLite identifier character 0x40 +** Quote character 0x80 +** +** Bit 0x20 is set if the mapped character requires translation to upper +** case. i.e. if the character is a lower-case ASCII character. +** If x is a lower-case ASCII character, then its upper-case equivalent +** is (x - 0x20). Therefore toupper() can be implemented as: +** +** (x & ~(map[x]&0x20)) +** +** The equivalent of tolower() is implemented using the sqlite3UpperToLower[] +** array. tolower() is used more often than toupper() by SQLite. +** +** Bit 0x40 is set if the character is non-alphanumeric and can be used in an +** SQLite identifier. Identifiers are alphanumerics, "_", "$", and any +** non-ASCII UTF character. Hence the test for whether or not a character is +** part of an identifier is 0x46. +*/ +static const unsigned char sqlite3CtypeMap[256] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00..07 ........ */ + 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, /* 08..0f ........ */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 10..17 ........ */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 18..1f ........ */ + 0x01, 0x00, 0x80, 0x00, 0x40, 0x00, 0x00, 0x80, /* 20..27 !"#$%&' */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 28..2f ()*+,-./ */ + 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, /* 30..37 01234567 */ + 0x0c, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 38..3f 89:;<=>? */ + + 0x00, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x02, /* 40..47 @ABCDEFG */ + 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, /* 48..4f HIJKLMNO */ + 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, /* 50..57 PQRSTUVW */ + 0x02, 0x02, 0x02, 0x80, 0x00, 0x00, 0x00, 0x40, /* 58..5f XYZ[\]^_ */ + 0x80, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x22, /* 60..67 `abcdefg */ + 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, /* 68..6f hijklmno */ + 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, /* 70..77 pqrstuvw */ + 0x22, 0x22, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, /* 78..7f xyz{|}~. */ + + 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* 80..87 ........ */ + 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* 88..8f ........ */ + 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* 90..97 ........ */ + 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* 98..9f ........ */ + 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* a0..a7 ........ */ + 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* a8..af ........ */ + 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* b0..b7 ........ */ + 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* b8..bf ........ */ + + 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* c0..c7 ........ */ + 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* c8..cf ........ */ + 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* d0..d7 ........ */ + 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* d8..df ........ */ + 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* e0..e7 ........ */ + 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* e8..ef ........ */ + 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* f0..f7 ........ */ + 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40 /* f8..ff ........ */ +}; +#define sqlite3Toupper(x) ((x)&~(sqlite3CtypeMap[(unsigned char)(x)]&0x20)) +#define sqlite3Isspace(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x01) +#define sqlite3Isalnum(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x06) +#define sqlite3Isalpha(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x02) +#define sqlite3Isdigit(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x04) +#define sqlite3Isxdigit(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x08) +#define sqlite3Tolower(x) (sqlite3UpperToLower[(unsigned char)(x)]) +#define sqlite3Isquote(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x80) + + +/* +** If X is a character that can be used in an identifier then +** IdChar(X) will be true. Otherwise it is false. +** +** For ASCII, any character with the high-order bit set is +** allowed in an identifier. For 7-bit characters, +** sqlite3IsIdChar[X] must be 1. +** +** For EBCDIC, the rules are more complex but have the same +** end result. +** +** Ticket #1066. the SQL standard does not allow '$' in the +** middle of identifiers. But many SQL implementations do. +** SQLite will allow '$' in identifiers for compatibility. +** But the feature is undocumented. +*/ +#define IdChar(C) ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0) + +/* +** Ignore testcase() macros +*/ +#define testcase(X) + +/* +** Token values +*/ +#define TK_SPACE 0 +#define TK_NAME 1 +#define TK_LITERAL 2 +#define TK_PUNCT 3 +#define TK_ERROR 4 + +#define TK_MINUS TK_PUNCT +#define TK_LP TK_PUNCT +#define TK_RP TK_PUNCT +#define TK_SEMI TK_PUNCT +#define TK_PLUS TK_PUNCT +#define TK_STAR TK_PUNCT +#define TK_SLASH TK_PUNCT +#define TK_REM TK_PUNCT +#define TK_EQ TK_PUNCT +#define TK_LE TK_PUNCT +#define TK_NE TK_PUNCT +#define TK_LSHIFT TK_PUNCT +#define TK_LT TK_PUNCT +#define TK_GE TK_PUNCT +#define TK_RSHIFT TK_PUNCT +#define TK_GT TK_PUNCT +#define TK_GE TK_PUNCT +#define TK_BITOR TK_PUNCT +#define TK_CONCAT TK_PUNCT +#define TK_COMMA TK_PUNCT +#define TK_BITAND TK_PUNCT +#define TK_BITNOT TK_PUNCT +#define TK_STRING TK_LITERAL +#define TK_ID TK_NAME +#define TK_ILLEGAL TK_ERROR +#define TK_DOT TK_PUNCT +#define TK_INTEGER TK_LITERAL +#define TK_FLOAT TK_LITERAL +#define TK_VARIABLE TK_LITERAL +#define TK_BLOB TK_LITERAL + +/* Disable nuisance warnings about case fall-through */ +#if !defined(deliberate_fall_through) && defined(__GCC__) && __GCC__>=7 +# define deliberate_fall_through __attribute__((fallthrough)); +#else +# define deliberate_fall_through +#endif + +/* +** Return the length (in bytes) of the token that begins at z[0]. +** Store the token type in *tokenType before returning. +*/ +static sqlite3_int64 sqlite3GetToken(const unsigned char *z, int *tokenType){ + sqlite3_int64 i; + int c; + switch( aiClass[*z] ){ /* Switch on the character-class of the first byte + ** of the token. See the comment on the CC_ defines + ** above. */ + case CC_SPACE: { + for(i=1; sqlite3Isspace(z[i]); i++){} + *tokenType = TK_SPACE; + return i; + } + case CC_MINUS: { + if( z[1]=='-' ){ + for(i=2; (c=z[i])!=0 && c!='\n'; i++){} + *tokenType = TK_SPACE; + return i; + } + *tokenType = TK_MINUS; + return 1; + } + case CC_LP: { + *tokenType = TK_LP; + return 1; + } + case CC_RP: { + *tokenType = TK_RP; + return 1; + } + case CC_SEMI: { + *tokenType = TK_SEMI; + return 1; + } + case CC_PLUS: { + *tokenType = TK_PLUS; + return 1; + } + case CC_STAR: { + *tokenType = TK_STAR; + return 1; + } + case CC_SLASH: { + if( z[1]!='*' || z[2]==0 ){ + *tokenType = TK_SLASH; + return 1; + } + for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){} + if( c ) i++; + *tokenType = TK_SPACE; + return i; + } + case CC_PERCENT: { + *tokenType = TK_REM; + return 1; + } + case CC_EQ: { + *tokenType = TK_EQ; + return 1 + (z[1]=='='); + } + case CC_LT: { + if( (c=z[1])=='=' ){ + *tokenType = TK_LE; + return 2; + }else if( c=='>' ){ + *tokenType = TK_NE; + return 2; + }else if( c=='<' ){ + *tokenType = TK_LSHIFT; + return 2; + }else{ + *tokenType = TK_LT; + return 1; + } + } + case CC_GT: { + if( (c=z[1])=='=' ){ + *tokenType = TK_GE; + return 2; + }else if( c=='>' ){ + *tokenType = TK_RSHIFT; + return 2; + }else{ + *tokenType = TK_GT; + return 1; + } + } + case CC_BANG: { + if( z[1]!='=' ){ + *tokenType = TK_ILLEGAL; + return 1; + }else{ + *tokenType = TK_NE; + return 2; + } + } + case CC_PIPE: { + if( z[1]!='|' ){ + *tokenType = TK_BITOR; + return 1; + }else{ + *tokenType = TK_CONCAT; + return 2; + } + } + case CC_COMMA: { + *tokenType = TK_COMMA; + return 1; + } + case CC_AND: { + *tokenType = TK_BITAND; + return 1; + } + case CC_TILDA: { + *tokenType = TK_BITNOT; + return 1; + } + case CC_QUOTE: { + int delim = z[0]; + testcase( delim=='`' ); + testcase( delim=='\'' ); + testcase( delim=='"' ); + for(i=1; (c=z[i])!=0; i++){ + if( c==delim ){ + if( z[i+1]==delim ){ + i++; + }else{ + break; + } + } + } + if( c=='\'' ){ + *tokenType = TK_STRING; + return i+1; + }else if( c!=0 ){ + *tokenType = TK_ID; + return i+1; + }else{ + *tokenType = TK_ILLEGAL; + return i; + } + } + case CC_DOT: { + if( !sqlite3Isdigit(z[1]) ){ + *tokenType = TK_DOT; + return 1; + } + /* If the next character is a digit, this is a floating point + ** number that begins with ".". Fall thru into the next case */ + /* no break */ deliberate_fall_through + } + case CC_DIGIT: { + *tokenType = TK_INTEGER; + if( z[0]=='0' && (z[1]=='x' || z[1]=='X') && sqlite3Isxdigit(z[2]) ){ + for(i=3; sqlite3Isxdigit(z[i]); i++){} + return i; + } + for(i=0; sqlite3Isdigit(z[i]); i++){} + if( z[i]=='.' ){ + i++; + while( sqlite3Isdigit(z[i]) ){ i++; } + *tokenType = TK_FLOAT; + } + if( (z[i]=='e' || z[i]=='E') && + ( sqlite3Isdigit(z[i+1]) + || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2])) + ) + ){ + i += 2; + while( sqlite3Isdigit(z[i]) ){ i++; } + *tokenType = TK_FLOAT; + } + while( IdChar(z[i]) ){ + *tokenType = TK_ILLEGAL; + i++; + } + return i; + } + case CC_QUOTE2: { + for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){} + *tokenType = c==']' ? TK_ID : TK_ILLEGAL; + return i; + } + case CC_VARNUM: { + *tokenType = TK_VARIABLE; + for(i=1; sqlite3Isdigit(z[i]); i++){} + return i; + } + case CC_DOLLAR: + case CC_VARALPHA: { + int n = 0; + testcase( z[0]=='$' ); testcase( z[0]=='@' ); + testcase( z[0]==':' ); testcase( z[0]=='#' ); + *tokenType = TK_VARIABLE; + for(i=1; (c=z[i])!=0; i++){ + if( IdChar(c) ){ + n++; + }else if( c=='(' && n>0 ){ + do{ + i++; + }while( (c=z[i])!=0 && !sqlite3Isspace(c) && c!=')' ); + if( c==')' ){ + i++; + }else{ + *tokenType = TK_ILLEGAL; + } + break; + }else if( c==':' && z[i+1]==':' ){ + i++; + }else{ + break; + } + } + if( n==0 ) *tokenType = TK_ILLEGAL; + return i; + } + case CC_KYWD: { + for(i=1; aiClass[z[i]]<=CC_KYWD; i++){} + if( IdChar(z[i]) ){ + /* This token started out using characters that can appear in keywords, + ** but z[i] is a character not allowed within keywords, so this must + ** be an identifier instead */ + i++; + break; + } + *tokenType = TK_ID; + return i; + } + case CC_X: { + testcase( z[0]=='x' ); testcase( z[0]=='X' ); + if( z[1]=='\'' ){ + *tokenType = TK_BLOB; + for(i=2; sqlite3Isxdigit(z[i]); i++){} + if( z[i]!='\'' || i%2 ){ + *tokenType = TK_ILLEGAL; + while( z[i] && z[i]!='\'' ){ i++; } + } + if( z[i] ) i++; + return i; + } + /* If it is not a BLOB literal, then it must be an ID, since no + ** SQL keywords start with the letter 'x'. Fall through */ + /* no break */ deliberate_fall_through + } + case CC_ID: { + i = 1; + break; + } + default: { + *tokenType = TK_ILLEGAL; + return 1; + } + } + while( IdChar(z[i]) ){ i++; } + *tokenType = TK_ID; + return i; +} + +char *sqlite3_normalize(const char *zSql){ + char *z; /* The output string */ + sqlite3_int64 nZ; /* Size of the output string in bytes */ + sqlite3_int64 nSql; /* Size of the input string in bytes */ + int i; /* Next character to read from zSql[] */ + int j; /* Next slot to fill in on z[] */ + int tokenType; /* Type of the next token */ + sqlite3_int64 n; /* Size of the next token */ + int k; /* Loop counter */ + + nSql = strlen(zSql); + nZ = nSql; + z = sqlite3_malloc64( nZ+2 ); + if( z==0 ) return 0; + for(i=j=0; zSql[i]; i += n){ + n = sqlite3GetToken((unsigned char*)zSql+i, &tokenType); + switch( tokenType ){ + case TK_SPACE: { + break; + } + case TK_ERROR: { + sqlite3_free(z); + return 0; + } + case TK_LITERAL: { + z[j++] = '?'; + break; + } + case TK_PUNCT: + case TK_NAME: { + if( n==4 && sqlite3_strnicmp(zSql+i,"NULL",4)==0 ){ + if( (j>=3 && strncmp(z+j-2,"is",2)==0 && !IdChar(z[j-3])) + || (j>=4 && strncmp(z+j-3,"not",3)==0 && !IdChar(z[j-4])) + ){ + /* NULL is a keyword in this case, not a literal value */ + }else{ + /* Here the NULL is a literal value */ + z[j++] = '?'; + break; + } + } + if( j>0 && IdChar(z[j-1]) && IdChar(zSql[i]) ) z[j++] = ' '; + for(k=0; k0 && z[j-1]==' ' ){ j--; } + if( j>0 && z[j-1]!=';' ){ z[j++] = ';'; } + z[j] = 0; + + /* Make a second pass converting "in(...)" where the "..." is not a + ** SELECT statement into "in(?,?,?)" */ + for(i=0; i5 ){ + memmove(z+n+5, z+n+k, j-(n+k)); + } + j = j-k+5; + z[j] = 0; + memcpy(z+n, "?,?,?", 5); + } + return z; +} + +/* +** For testing purposes, or to build a stand-alone SQL normalizer program, +** compile this one source file with the -DSQLITE_NORMALIZE_CLI and link +** it against any SQLite library. The resulting command-line program will +** run sqlite3_normalize() over the text of all files named on the command- +** line and show the result on standard output. +*/ +#ifdef SQLITE_NORMALIZE_CLI +#include +#include + +/* +** Break zIn up into separate SQL statements and run sqlite3_normalize() +** on each one. Print the result of each run. +*/ +static void normalizeFile(char *zIn){ + int i; + if( zIn==0 ) return; + for(i=0; zIn[i]; i++){ + char cSaved; + if( zIn[i]!=';' ) continue; + cSaved = zIn[i+1]; + zIn[i+1] = 0; + if( sqlite3_complete(zIn) ){ + char *zOut = sqlite3_normalize(zIn); + if( zOut ){ + printf("%s\n", zOut); + sqlite3_free(zOut); + }else{ + fprintf(stderr, "ERROR: %s\n", zIn); + } + zIn[i+1] = cSaved; + zIn += i+1; + i = -1; + }else{ + zIn[i+1] = cSaved; + } + } +} + +/* +** The main routine for "sql_normalize". Read files named on the +** command-line and run the text of each through sqlite3_normalize(). +*/ +int main(int argc, char **argv){ + int i; + FILE *in; + char *zBuf = 0; + sqlite3_int64 sz, got; + + for(i=1; i +#include +#include + +/* The original page cache routines */ +static sqlite3_pcache_methods2 pcacheBase; +static FILE *pcachetraceOut; + +/* Methods that trace pcache activity */ +static int pcachetraceInit(void *pArg){ + int nRes; + if( pcachetraceOut ){ + fprintf(pcachetraceOut, "PCACHETRACE: xInit(%p)\n", pArg); + } + nRes = pcacheBase.xInit(pArg); + if( pcachetraceOut ){ + fprintf(pcachetraceOut, "PCACHETRACE: xInit(%p) -> %d\n", pArg, nRes); + } + return nRes; +} +static void pcachetraceShutdown(void *pArg){ + if( pcachetraceOut ){ + fprintf(pcachetraceOut, "PCACHETRACE: xShutdown(%p)\n", pArg); + } + pcacheBase.xShutdown(pArg); +} +static sqlite3_pcache *pcachetraceCreate(int szPage, int szExtra, int bPurge){ + sqlite3_pcache *pRes; + if( pcachetraceOut ){ + fprintf(pcachetraceOut, "PCACHETRACE: xCreate(%d,%d,%d)\n", + szPage, szExtra, bPurge); + } + pRes = pcacheBase.xCreate(szPage, szExtra, bPurge); + if( pcachetraceOut ){ + fprintf(pcachetraceOut, "PCACHETRACE: xCreate(%d,%d,%d) -> %p\n", + szPage, szExtra, bPurge, pRes); + } + return pRes; +} +static void pcachetraceCachesize(sqlite3_pcache *p, int nCachesize){ + if( pcachetraceOut ){ + fprintf(pcachetraceOut, "PCACHETRACE: xCachesize(%p, %d)\n", p, nCachesize); + } + pcacheBase.xCachesize(p, nCachesize); +} +static int pcachetracePagecount(sqlite3_pcache *p){ + int nRes; + if( pcachetraceOut ){ + fprintf(pcachetraceOut, "PCACHETRACE: xPagecount(%p)\n", p); + } + nRes = pcacheBase.xPagecount(p); + if( pcachetraceOut ){ + fprintf(pcachetraceOut, "PCACHETRACE: xPagecount(%p) -> %d\n", p, nRes); + } + return nRes; +} +static sqlite3_pcache_page *pcachetraceFetch( + sqlite3_pcache *p, + unsigned key, + int crFg +){ + sqlite3_pcache_page *pRes; + if( pcachetraceOut ){ + fprintf(pcachetraceOut, "PCACHETRACE: xFetch(%p,%u,%d)\n", p, key, crFg); + } + pRes = pcacheBase.xFetch(p, key, crFg); + if( pcachetraceOut ){ + fprintf(pcachetraceOut, "PCACHETRACE: xFetch(%p,%u,%d) -> %p\n", + p, key, crFg, pRes); + } + return pRes; +} +static void pcachetraceUnpin( + sqlite3_pcache *p, + sqlite3_pcache_page *pPg, + int bDiscard +){ + if( pcachetraceOut ){ + fprintf(pcachetraceOut, "PCACHETRACE: xUnpin(%p, %p, %d)\n", + p, pPg, bDiscard); + } + pcacheBase.xUnpin(p, pPg, bDiscard); +} +static void pcachetraceRekey( + sqlite3_pcache *p, + sqlite3_pcache_page *pPg, + unsigned oldKey, + unsigned newKey +){ + if( pcachetraceOut ){ + fprintf(pcachetraceOut, "PCACHETRACE: xRekey(%p, %p, %u, %u)\n", + p, pPg, oldKey, newKey); + } + pcacheBase.xRekey(p, pPg, oldKey, newKey); +} +static void pcachetraceTruncate(sqlite3_pcache *p, unsigned n){ + if( pcachetraceOut ){ + fprintf(pcachetraceOut, "PCACHETRACE: xTruncate(%p, %u)\n", p, n); + } + pcacheBase.xTruncate(p, n); +} +static void pcachetraceDestroy(sqlite3_pcache *p){ + if( pcachetraceOut ){ + fprintf(pcachetraceOut, "PCACHETRACE: xDestroy(%p)\n", p); + } + pcacheBase.xDestroy(p); +} +static void pcachetraceShrink(sqlite3_pcache *p){ + if( pcachetraceOut ){ + fprintf(pcachetraceOut, "PCACHETRACE: xShrink(%p)\n", p); + } + pcacheBase.xShrink(p); +} + +/* The substitute pcache methods */ +static sqlite3_pcache_methods2 ersaztPcacheMethods = { + 0, + 0, + pcachetraceInit, + pcachetraceShutdown, + pcachetraceCreate, + pcachetraceCachesize, + pcachetracePagecount, + pcachetraceFetch, + pcachetraceUnpin, + pcachetraceRekey, + pcachetraceTruncate, + pcachetraceDestroy, + pcachetraceShrink +}; + +/* Begin tracing memory allocations to out. */ +int sqlite3PcacheTraceActivate(FILE *out){ + int rc = SQLITE_OK; + if( pcacheBase.xFetch==0 ){ + rc = sqlite3_config(SQLITE_CONFIG_GETPCACHE2, &pcacheBase); + if( rc==SQLITE_OK ){ + rc = sqlite3_config(SQLITE_CONFIG_PCACHE2, &ersaztPcacheMethods); + } + } + pcachetraceOut = out; + return rc; +} + +/* Deactivate memory tracing */ +int sqlite3PcacheTraceDeactivate(void){ + int rc = SQLITE_OK; + if( pcacheBase.xFetch!=0 ){ + rc = sqlite3_config(SQLITE_CONFIG_PCACHE2, &pcacheBase); + if( rc==SQLITE_OK ){ + memset(&pcacheBase, 0, sizeof(pcacheBase)); + } + } + pcachetraceOut = 0; + return rc; +} diff --git a/ext/misc/percentile.c b/ext/misc/percentile.c index 74a4c9d12b..98e45cc3a8 100644 --- a/ext/misc/percentile.c +++ b/ext/misc/percentile.c @@ -11,7 +11,7 @@ ****************************************************************************** ** ** This file contains code to implement the percentile(Y,P) SQL function -** as described below: +** and similar as described below: ** ** (1) The percentile(Y,P) function is an aggregate function taking ** exactly two arguments. @@ -57,29 +57,108 @@ ** (12) The percentile(Y,P) is implemented as a single C99 source-code ** file that compiles into a shared-library or DLL that can be loaded ** into SQLite using the sqlite3_load_extension() interface. +** +** (13) A separate median(Y) function is the equivalent percentile(Y,50). +** +** (14) A separate percentile_cont(Y,P) function is equivalent to +** percentile(Y,P/100.0). In other words, the fraction value in +** the second argument is in the range of 0 to 1 instead of 0 to 100. +** +** (15) A separate percentile_disc(Y,P) function is like +** percentile_cont(Y,P) except that instead of returning the weighted +** average of the nearest two input values, it returns the next lower +** value. So the percentile_disc(Y,P) will always return a value +** that was one of the inputs. +** +** (16) All of median(), percentile(Y,P), percentile_cont(Y,P) and +** percentile_disc(Y,P) can be used as window functions. +** +** Differences from standard SQL: +** +** * The percentile_cont(X,P) function is equivalent to the following in +** standard SQL: +** +** (percentile_cont(P) WITHIN GROUP (ORDER BY X)) +** +** The SQLite syntax is much more compact. The standard SQL syntax +** is also supported if SQLite is compiled with the +** -DSQLITE_ENABLE_ORDERED_SET_AGGREGATES option. +** +** * No median(X) function exists in the SQL standard. App developers +** are expected to write "percentile_cont(0.5)WITHIN GROUP(ORDER BY X)". +** +** * No percentile(Y,P) function exists in the SQL standard. Instead of +** percential(Y,P), developers must write this: +** "percentile_cont(P/100.0) WITHIN GROUP (ORDER BY Y)". Note that +** the fraction parameter to percentile() goes from 0 to 100 whereas +** the fraction parameter in SQL standard percentile_cont() goes from +** 0 to 1. +** +** Implementation notes as of 2024-08-31: +** +** * The regular aggregate-function versions of these routines work +** by accumulating all values in an array of doubles, then sorting +** that array using quicksort before computing the answer. Thus +** the runtime is O(NlogN) where N is the number of rows of input. +** +** * For the window-function versions of these routines, the array of +** inputs is sorted as soon as the first value is computed. Thereafter, +** the array is kept in sorted order using an insert-sort. This +** results in O(N*K) performance where K is the size of the window. +** One can imagine alternative implementations that give O(N*logN*logK) +** performance, but they require more complex logic and data structures. +** The developers have elected to keep the asymptotically slower +** algorithm for now, for simplicity, under the theory that window +** functions are seldom used and when they are, the window size K is +** often small. The developers might revisit that decision later, +** should the need arise. */ -#include "sqlite3ext.h" -SQLITE_EXTENSION_INIT1 +#if defined(SQLITE3_H) + /* no-op */ +#elif defined(SQLITE_STATIC_PERCENTILE) +# include "sqlite3.h" +#else +# include "sqlite3ext.h" + SQLITE_EXTENSION_INIT1 +#endif #include #include #include -/* The following object is the session context for a single percentile() -** function. We have to remember all input Y values until the very end. +/* The following object is the group context for a single percentile() +** aggregate. Remember all input Y values until the very end. ** Those values are accumulated in the Percentile.a[] array. */ typedef struct Percentile Percentile; struct Percentile { unsigned nAlloc; /* Number of slots allocated for a[] */ unsigned nUsed; /* Number of slots actually used in a[] */ - double rPct; /* 1.0 more than the value for P */ + char bSorted; /* True if a[] is already in sorted order */ + char bKeepSorted; /* True if advantageous to keep a[] sorted */ + char bPctValid; /* True if rPct is valid */ + double rPct; /* Fraction. 0.0 to 1.0 */ double *a; /* Array of Y values */ }; +/* Details of each function in the percentile family */ +typedef struct PercentileFunc PercentileFunc; +struct PercentileFunc { + const char *zName; /* Function name */ + char nArg; /* Number of arguments */ + char mxFrac; /* Maximum value of the "fraction" input */ + char bDiscrete; /* True for percentile_disc() */ +}; +static const PercentileFunc aPercentFunc[] = { + { "median", 1, 1, 0 }, + { "percentile", 2, 100, 0 }, + { "percentile_cont", 2, 1, 0 }, + { "percentile_disc", 2, 1, 1 }, +}; + /* ** Return TRUE if the input floating-point number is an infinity. */ -static int isInfinity(double r){ +static int percentIsInfinity(double r){ sqlite3_uint64 u; assert( sizeof(u)==sizeof(r) ); memcpy(&u, &r, sizeof(u)); @@ -87,13 +166,64 @@ static int isInfinity(double r){ } /* -** Return TRUE if two doubles differ by 0.001 or less +** Return TRUE if two doubles differ by 0.001 or less. */ -static int sameValue(double a, double b){ +static int percentSameValue(double a, double b){ a -= b; return a>=-0.001 && a<=0.001; } +/* +** Search p (which must have p->bSorted) looking for an entry with +** value y. Return the index of that entry. +** +** If bExact is true, return -1 if the entry is not found. +** +** If bExact is false, return the index at which a new entry with +** value y should be insert in order to keep the values in sorted +** order. The smallest return value in this case will be 0, and +** the largest return value will be p->nUsed. +*/ +static int percentBinarySearch(Percentile *p, double y, int bExact){ + int iFirst = 0; /* First element of search range */ + int iLast = p->nUsed - 1; /* Last element of search range */ + while( iLast>=iFirst ){ + int iMid = (iFirst+iLast)/2; + double x = p->a[iMid]; + if( xy ){ + iLast = iMid - 1; + }else{ + return iMid; + } + } + if( bExact ) return -1; + return iFirst; +} + +/* +** Generate an error for a percentile function. +** +** The error format string must have exactly one occurrence of "%%s()" +** (with two '%' characters). That substring will be replaced by the name +** of the function. +*/ +static void percentError(sqlite3_context *pCtx, const char *zFormat, ...){ + PercentileFunc *pFunc = (PercentileFunc*)sqlite3_user_data(pCtx); + char *zMsg1; + char *zMsg2; + va_list ap; + + va_start(ap, zFormat); + zMsg1 = sqlite3_vmprintf(zFormat, ap); + va_end(ap); + zMsg2 = zMsg1 ? sqlite3_mprintf(zMsg1, pFunc->zName) : 0; + sqlite3_result_error(pCtx, zMsg2, -1); + sqlite3_free(zMsg1); + sqlite3_free(zMsg2); +} + /* ** The "step" function for percentile(Y,P) is called once for each ** input row. @@ -103,16 +233,24 @@ static void percentStep(sqlite3_context *pCtx, int argc, sqlite3_value **argv){ double rPct; int eType; double y; - assert( argc==2 ); - - /* Requirement 3: P must be a number between 0 and 100 */ - eType = sqlite3_value_numeric_type(argv[1]); - rPct = sqlite3_value_double(argv[1]); - if( (eType!=SQLITE_INTEGER && eType!=SQLITE_FLOAT) || - ((rPct = sqlite3_value_double(argv[1]))<0.0 || rPct>100.0) ){ - sqlite3_result_error(pCtx, "2nd argument to percentile() is not " - "a number between 0.0 and 100.0", -1); - return; + assert( argc==2 || argc==1 ); + + if( argc==1 ){ + /* Requirement 13: median(Y) is the same as percentile(Y,50). */ + rPct = 0.5; + }else{ + /* Requirement 3: P must be a number between 0 and 100 */ + PercentileFunc *pFunc = (PercentileFunc*)sqlite3_user_data(pCtx); + eType = sqlite3_value_numeric_type(argv[1]); + rPct = sqlite3_value_double(argv[1])/(double)pFunc->mxFrac; + if( (eType!=SQLITE_INTEGER && eType!=SQLITE_FLOAT) + || rPct<0.0 || rPct>1.0 + ){ + percentError(pCtx, "the fraction argument to %%s()" + " is not between 0.0 and %.1f", + (double)pFunc->mxFrac); + return; + } } /* Allocate the session context. */ @@ -121,11 +259,12 @@ static void percentStep(sqlite3_context *pCtx, int argc, sqlite3_value **argv){ /* Remember the P value. Throw an error if the P value is different ** from any prior row, per Requirement (2). */ - if( p->rPct==0.0 ){ - p->rPct = rPct+1.0; - }else if( !sameValue(p->rPct,rPct+1.0) ){ - sqlite3_result_error(pCtx, "2nd argument to percentile() is not the " - "same for all input rows", -1); + if( !p->bPctValid ){ + p->rPct = rPct; + p->bPctValid = 1; + }else if( !percentSameValue(p->rPct,rPct) ){ + percentError(pCtx, "the fraction argument to %%s()" + " is not the same for all input rows"); return; } @@ -136,22 +275,21 @@ static void percentStep(sqlite3_context *pCtx, int argc, sqlite3_value **argv){ /* If not NULL, then Y must be numeric. Otherwise throw an error. ** Requirement 4 */ if( eType!=SQLITE_INTEGER && eType!=SQLITE_FLOAT ){ - sqlite3_result_error(pCtx, "1st argument to percentile() is not " - "numeric", -1); + percentError(pCtx, "input to %%s() is not numeric"); return; } /* Throw an error if the Y value is infinity or NaN */ y = sqlite3_value_double(argv[0]); - if( isInfinity(y) ){ - sqlite3_result_error(pCtx, "Inf input to percentile()", -1); + if( percentIsInfinity(y) ){ + percentError(pCtx, "Inf input to %%s()"); return; } /* Allocate and store the Y */ if( p->nUsed>=p->nAlloc ){ unsigned n = p->nAlloc*2 + 250; - double *a = sqlite3_realloc(p->a, sizeof(double)*n); + double *a = sqlite3_realloc64(p->a, sizeof(double)*n); if( a==0 ){ sqlite3_free(p->a); memset(p, 0, sizeof(*p)); @@ -161,26 +299,143 @@ static void percentStep(sqlite3_context *pCtx, int argc, sqlite3_value **argv){ p->nAlloc = n; p->a = a; } - p->a[p->nUsed++] = y; + if( p->nUsed==0 ){ + p->a[p->nUsed++] = y; + p->bSorted = 1; + }else if( !p->bSorted || y>=p->a[p->nUsed-1] ){ + p->a[p->nUsed++] = y; + }else if( p->bKeepSorted ){ + int i; + i = percentBinarySearch(p, y, 0); + if( i<(int)p->nUsed ){ + memmove(&p->a[i+1], &p->a[i], (p->nUsed-i)*sizeof(p->a[0])); + } + p->a[i] = y; + p->nUsed++; + }else{ + p->a[p->nUsed++] = y; + p->bSorted = 0; + } } /* -** Compare to doubles for sorting using qsort() +** Interchange two doubles. */ -static int doubleCmp(const void *pA, const void *pB){ - double a = *(double*)pA; - double b = *(double*)pB; - if( a==b ) return 0; - if( a=2 ); + if( a[0]>a[n-1] ){ + SWAP_DOUBLE(a[0],a[n-1]) + } + if( n==2 ) return; + iGt = n-1; + i = n/2; + if( a[0]>a[i] ){ + SWAP_DOUBLE(a[0],a[i]) + }else if( a[i]>a[iGt] ){ + SWAP_DOUBLE(a[i],a[iGt]) + } + if( n==3 ) return; + rPivot = a[i]; + iLt = i = 1; + do{ + if( a[i]iLt ) SWAP_DOUBLE(a[i],a[iLt]) + iLt++; + i++; + }else if( a[i]>rPivot ){ + do{ + iGt--; + }while( iGt>i && a[iGt]>rPivot ); + SWAP_DOUBLE(a[i],a[iGt]) + }else{ + i++; + } + }while( i=2 ) percentSort(a, iLt); + if( n-iGt>=2 ) percentSort(a+iGt, n-iGt); + +/* Uncomment for testing */ +#if 0 + for(i=0; ibSorted==0 ){ + assert( p->nUsed>1 ); + percentSort(p->a, p->nUsed); + p->bSorted = 1; + } + p->bKeepSorted = 1; + + /* Find and remove the row */ + i = percentBinarySearch(p, y, 1); + if( i>=0 ){ + p->nUsed--; + if( i<(int)p->nUsed ){ + memmove(&p->a[i], &p->a[i+1], (p->nUsed - i)*sizeof(p->a[0])); + } + } +} + +/* +** Compute the final output of percentile(). Clean up all allocated +** memory if and only if bIsFinal is true. +*/ +static void percentCompute(sqlite3_context *pCtx, int bIsFinal){ + Percentile *p; + PercentileFunc *pFunc = (PercentileFunc*)sqlite3_user_data(pCtx); unsigned i1, i2; double v1, v2; double ix, vx; @@ -188,21 +443,38 @@ static void percentFinal(sqlite3_context *pCtx){ if( p==0 ) return; if( p->a==0 ) return; if( p->nUsed ){ - qsort(p->a, p->nUsed, sizeof(double), doubleCmp); - ix = (p->rPct-1.0)*(p->nUsed-1)*0.01; + if( p->bSorted==0 ){ + assert( p->nUsed>1 ); + percentSort(p->a, p->nUsed); + p->bSorted = 1; + } + ix = p->rPct*(p->nUsed-1); i1 = (unsigned)ix; - i2 = ix==(double)i1 || i1==p->nUsed-1 ? i1 : i1+1; - v1 = p->a[i1]; - v2 = p->a[i2]; - vx = v1 + (v2-v1)*(ix-i1); + if( pFunc->bDiscrete ){ + vx = p->a[i1]; + }else{ + i2 = ix==(double)i1 || i1==p->nUsed-1 ? i1 : i1+1; + v1 = p->a[i1]; + v2 = p->a[i2]; + vx = v1 + (v2-v1)*(ix-i1); + } sqlite3_result_double(pCtx, vx); } - sqlite3_free(p->a); - memset(p, 0, sizeof(*p)); + if( bIsFinal ){ + sqlite3_free(p->a); + memset(p, 0, sizeof(*p)); + }else{ + p->bKeepSorted = 1; + } +} +static void percentFinal(sqlite3_context *pCtx){ + percentCompute(pCtx, 1); +} +static void percentValue(sqlite3_context *pCtx){ + percentCompute(pCtx, 0); } - -#ifdef _WIN32 +#if defined(_WIN32) && !defined(SQLITE3_H) && !defined(SQLITE_STATIC_PERCENTILE) __declspec(dllexport) #endif int sqlite3_percentile_init( @@ -211,9 +483,21 @@ int sqlite3_percentile_init( const sqlite3_api_routines *pApi ){ int rc = SQLITE_OK; + unsigned int i; +#ifdef SQLITE3EXT_H SQLITE_EXTENSION_INIT2(pApi); +#else + (void)pApi; /* Unused parameter */ +#endif (void)pzErrMsg; /* Unused parameter */ - rc = sqlite3_create_function(db, "percentile", 2, SQLITE_UTF8, 0, - 0, percentStep, percentFinal); + for(i=0; i +#include + +/* prefixes_vtab is a subclass of sqlite3_vtab which is +** underlying representation of the virtual table +*/ +typedef struct prefixes_vtab prefixes_vtab; +struct prefixes_vtab { + sqlite3_vtab base; /* Base class - must be first */ + /* No additional fields are necessary */ +}; + +/* prefixes_cursor is a subclass of sqlite3_vtab_cursor which will +** serve as the underlying representation of a cursor that scans +** over rows of the result +*/ +typedef struct prefixes_cursor prefixes_cursor; +struct prefixes_cursor { + sqlite3_vtab_cursor base; /* Base class - must be first */ + sqlite3_int64 iRowid; /* The rowid */ + char *zStr; /* Original string to be prefixed */ + int nStr; /* Length of the string in bytes */ +}; + +/* +** The prefixesConnect() method is invoked to create a new +** template virtual table. +** +** Think of this routine as the constructor for prefixes_vtab objects. +** +** All this routine needs to do is: +** +** (1) Allocate the prefixes_vtab object and initialize all fields. +** +** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the +** result set of queries against the virtual table will look like. +*/ +static int prefixesConnect( + sqlite3 *db, + void *pAux, + int argc, const char *const*argv, + sqlite3_vtab **ppVtab, + char **pzErr +){ + prefixes_vtab *pNew; + int rc; + + rc = sqlite3_declare_vtab(db, + "CREATE TABLE prefixes(prefix TEXT, original_string TEXT HIDDEN)" + ); + if( rc==SQLITE_OK ){ + pNew = sqlite3_malloc( sizeof(*pNew) ); + *ppVtab = (sqlite3_vtab*)pNew; + if( pNew==0 ) return SQLITE_NOMEM; + memset(pNew, 0, sizeof(*pNew)); + sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS); + } + return rc; +} + +/* +** This method is the destructor for prefixes_vtab objects. +*/ +static int prefixesDisconnect(sqlite3_vtab *pVtab){ + prefixes_vtab *p = (prefixes_vtab*)pVtab; + sqlite3_free(p); + return SQLITE_OK; +} + +/* +** Constructor for a new prefixes_cursor object. +*/ +static int prefixesOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){ + prefixes_cursor *pCur; + pCur = sqlite3_malloc( sizeof(*pCur) ); + if( pCur==0 ) return SQLITE_NOMEM; + memset(pCur, 0, sizeof(*pCur)); + *ppCursor = &pCur->base; + return SQLITE_OK; +} + +/* +** Destructor for a prefixes_cursor. +*/ +static int prefixesClose(sqlite3_vtab_cursor *cur){ + prefixes_cursor *pCur = (prefixes_cursor*)cur; + sqlite3_free(pCur->zStr); + sqlite3_free(pCur); + return SQLITE_OK; +} + + +/* +** Advance a prefixes_cursor to its next row of output. +*/ +static int prefixesNext(sqlite3_vtab_cursor *cur){ + prefixes_cursor *pCur = (prefixes_cursor*)cur; + pCur->iRowid++; + return SQLITE_OK; +} + +/* +** Return values of columns for the row at which the prefixes_cursor +** is currently pointing. +*/ +static int prefixesColumn( + sqlite3_vtab_cursor *cur, /* The cursor */ + sqlite3_context *ctx, /* First argument to sqlite3_result_...() */ + int i /* Which column to return */ +){ + prefixes_cursor *pCur = (prefixes_cursor*)cur; + switch( i ){ + case 0: + sqlite3_result_text(ctx, pCur->zStr, pCur->nStr - (int)pCur->iRowid, + 0); + break; + default: + sqlite3_result_text(ctx, pCur->zStr, pCur->nStr, 0); + break; + } + return SQLITE_OK; +} + +/* +** Return the rowid for the current row. In this implementation, the +** rowid is the same as the output value. +*/ +static int prefixesRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ + prefixes_cursor *pCur = (prefixes_cursor*)cur; + *pRowid = pCur->iRowid; + return SQLITE_OK; +} + +/* +** Return TRUE if the cursor has been moved off of the last +** row of output. +*/ +static int prefixesEof(sqlite3_vtab_cursor *cur){ + prefixes_cursor *pCur = (prefixes_cursor*)cur; + return pCur->iRowid>pCur->nStr; +} + +/* +** This method is called to "rewind" the prefixes_cursor object back +** to the first row of output. This method is always called at least +** once prior to any call to prefixesColumn() or prefixesRowid() or +** prefixesEof(). +*/ +static int prefixesFilter( + sqlite3_vtab_cursor *pVtabCursor, + int idxNum, const char *idxStr, + int argc, sqlite3_value **argv +){ + prefixes_cursor *pCur = (prefixes_cursor *)pVtabCursor; + sqlite3_free(pCur->zStr); + if( argc>0 ){ + pCur->zStr = sqlite3_mprintf("%s", sqlite3_value_text(argv[0])); + pCur->nStr = pCur->zStr ? (int)strlen(pCur->zStr) : 0; + }else{ + pCur->zStr = 0; + pCur->nStr = 0; + } + pCur->iRowid = 0; + return SQLITE_OK; +} + +/* +** SQLite will invoke this method one or more times while planning a query +** that uses the virtual table. This routine needs to create +** a query plan for each invocation and compute an estimated cost for that +** plan. +*/ +static int prefixesBestIndex( + sqlite3_vtab *tab, + sqlite3_index_info *pIdxInfo +){ + /* Search for a usable equality constraint against column 1 + ** (original_string) and use it if at all possible */ + int i; + const struct sqlite3_index_constraint *p; + + for(i=0, p=pIdxInfo->aConstraint; inConstraint; i++, p++){ + if( p->iColumn!=1 ) continue; + if( p->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue; + if( !p->usable ) continue; + pIdxInfo->aConstraintUsage[i].argvIndex = 1; + pIdxInfo->aConstraintUsage[i].omit = 1; + pIdxInfo->estimatedCost = (double)10; + pIdxInfo->estimatedRows = 10; + return SQLITE_OK; + } + pIdxInfo->estimatedCost = (double)1000000000; + pIdxInfo->estimatedRows = 1000000000; + return SQLITE_OK; +} + +/* +** This following structure defines all the methods for the +** virtual table. +*/ +static sqlite3_module prefixesModule = { + /* iVersion */ 0, + /* xCreate */ 0, + /* xConnect */ prefixesConnect, + /* xBestIndex */ prefixesBestIndex, + /* xDisconnect */ prefixesDisconnect, + /* xDestroy */ 0, + /* xOpen */ prefixesOpen, + /* xClose */ prefixesClose, + /* xFilter */ prefixesFilter, + /* xNext */ prefixesNext, + /* xEof */ prefixesEof, + /* xColumn */ prefixesColumn, + /* xRowid */ prefixesRowid, + /* xUpdate */ 0, + /* xBegin */ 0, + /* xSync */ 0, + /* xCommit */ 0, + /* xRollback */ 0, + /* xFindMethod */ 0, + /* xRename */ 0, + /* xSavepoint */ 0, + /* xRelease */ 0, + /* xRollbackTo */ 0, + /* xShadowName */ 0, + /* xIntegrity */ 0 +}; + +/* +** This is a copy of the SQLITE_SKIP_UTF8(zIn) macro in sqliteInt.h. +** +** Assuming zIn points to the first byte of a UTF-8 character, +** advance zIn to point to the first byte of the next UTF-8 character. +*/ +#define PREFIX_SKIP_UTF8(zIn) { \ + if( (*(zIn++))>=0xc0 ){ \ + while( (*zIn & 0xc0)==0x80 ){ zIn++; } \ + } \ +} + +/* +** Implementation of function prefix_length(). This function accepts two +** strings as arguments and returns the length in characters (not bytes), +** of the longest prefix shared by the two strings. For example: +** +** prefix_length('abcdxxx', 'abcyy') == 3 +** prefix_length('abcdxxx', 'bcyyy') == 0 +** prefix_length('abcdxxx', 'ab') == 2 +** prefix_length('ab', 'abcd') == 2 +** +** This function assumes the input is well-formed utf-8. If it is not, +** it is possible for this function to return -1. +*/ +static void prefixLengthFunc( + sqlite3_context *ctx, + int nVal, + sqlite3_value **apVal +){ + int nByte; /* Number of bytes to compare */ + int nRet = 0; /* Return value */ + const unsigned char *zL = sqlite3_value_text(apVal[0]); + const unsigned char *zR = sqlite3_value_text(apVal[1]); + int nL = sqlite3_value_bytes(apVal[0]); + int nR = sqlite3_value_bytes(apVal[1]); + int i; + + nByte = (nL > nR ? nL : nR); + for(i=0; i

    Everything

    ' +.print '' +SELECT geopoly_svg(_shape, + printf('style="fill:none;stroke:%s;stroke-width:1"',clr) + ) + FROM geo1; +SELECT geopoly_svg(poly, + printf('style="fill:%s;fill-opacity:0.5;"',clr) + ) + FROM querypoly; +.print '' + +.print '

    Overlap Query

    ' +.print '
    '
    +.print 'SELECT *'
    +.print '  FROM geo1, querypoly'
    +.print ' WHERE geopoly_overlap(_shape, poly);'
    +.print 
    +EXPLAIN QUERY PLAN
    +SELECT geopoly_svg(_shape,
    +         printf('style="fill:none;stroke:%s;stroke-width:1"',geo1.clr)
    +       )
    +  FROM geo1, querypoly
    + WHERE geopoly_overlap(_shape, poly);
    +.print '
    ' +.print '' +SELECT geopoly_svg(_shape, + printf('style="fill:none;stroke:%s;stroke-width:1"',geo1.clr) + ) + FROM geo1, querypoly + WHERE geopoly_overlap(_shape, poly); +SELECT geopoly_svg(poly, + printf('style="fill:%s;fill-opacity:0.5;"',clr) + ) + FROM querypoly; +.print '' + +.print '

    Overlap Query And Result Bounding Box

    ' +.print '' +SELECT geopoly_svg(_shape, + printf('style="fill:none;stroke:%s;stroke-width:1"',geo1.clr) + ) + FROM geo1, querypoly + WHERE geopoly_overlap(_shape, poly); +SELECT geopoly_svg(geopoly_bbox(poly), + 'style="fill:none;stroke:black;stroke-width:3"' + ) + FROM querypoly; +SELECT geopoly_svg(poly, + printf('style="fill:%s;fill-opacity:0.5;"',clr) + ) + FROM querypoly; +SELECT geopoly_svg(geopoly_group_bbox(_shape), + 'style="fill:none;stroke:red;stroke-width:3"' + ) + FROM geo1, querypoly + WHERE geopoly_overlap(_shape, poly); +.print '' + +.print '

    Bounding-Box Overlap Query

    ' +.print '' +SELECT geopoly_svg(_shape, + printf('style="fill:none;stroke:%s;stroke-width:1"',geo1.clr) + ), + geopoly_svg(geopoly_bbox(_shape), + 'style="fill:none;stroke:black;stroke-width:1"' + ) + FROM geo1, querypoly + WHERE geopoly_overlap(geopoly_bbox(_shape), geopoly_bbox(poly)); +SELECT geopoly_svg(poly, + printf('style="fill:%s;fill-opacity:0.5;"',clr) + ) + FROM querypoly; +SELECT geopoly_svg(geopoly_bbox(poly), + 'style="fill:none;stroke:black;stroke-width:3"' + ) + FROM querypoly; +.print '' + +.print '

    Within Query

    ' +.print '
    '
    +.print 'SELECT *'
    +.print '  FROM geo1, querypoly'
    +.print ' WHERE geopoly_within(_shape, poly);'
    +.print 
    +EXPLAIN QUERY PLAN
    +SELECT geopoly_svg(_shape,
    +         printf('style="fill:none;stroke:%s;stroke-width:1"',geo1.clr)
    +       )
    +  FROM geo1, querypoly
    + WHERE geopoly_within(_shape, poly);
    +.print '
    ' +.print '' +SELECT geopoly_svg(_shape, + printf('style="fill:none;stroke:%s;stroke-width:1"',geo1.clr) + ) + FROM geo1, querypoly + WHERE geopoly_within(_shape, poly); +SELECT geopoly_svg(poly, + printf('style="fill:%s;fill-opacity:0.5;"',clr) + ) + FROM querypoly; +.print '' + +.print '

    Bounding-Box WITHIN Query

    ' +.print '' +SELECT geopoly_svg(_shape, + printf('style="fill:none;stroke:%s;stroke-width:1"',geo1.clr) + ), + geopoly_svg(geopoly_bbox(_shape), + 'style="fill:none;stroke:black;stroke-width:1"' + ) + FROM geo1, querypoly + WHERE geopoly_within(geopoly_bbox(_shape), geopoly_bbox(poly)); +SELECT geopoly_svg(poly, + printf('style="fill:%s;fill-opacity:0.5;"',clr) + ) + FROM querypoly; +SELECT geopoly_svg(geopoly_bbox(poly), + 'style="fill:none;stroke:black;stroke-width:3"' + ) + FROM querypoly; +.print '' + +.print '

    Not Overlap Query

    ' +.print '
    '
    +.print 'SELECT *'
    +.print '  FROM geo1, querypoly'
    +.print ' WHERE NOT geopoly_overlap(_shape, poly);'
    +.print 
    +EXPLAIN QUERY PLAN
    +SELECT geopoly_svg(_shape,
    +         printf('style="fill:none;stroke:%s;stroke-width:1"',geo1.clr)
    +       )
    +  FROM geo1, querypoly
    + WHERE NOT geopoly_overlap(_shape, poly);
    +.print '
    ' +.print '' +SELECT geopoly_svg(_shape, + printf('style="fill:none;stroke:%s;stroke-width:1"',geo1.clr) + ) + FROM geo1, querypoly + WHERE NOT geopoly_overlap(_shape, poly); +SELECT geopoly_svg(poly, + printf('style="fill:%s;fill-opacity:0.5;"',clr) + ) + FROM querypoly; +.print '' + +.print '

    Not Within Query

    ' +.print '
    '
    +.print 'SELECT *'
    +.print '  FROM geo1, querypoly'
    +.print ' WHERE NOT geopoly_within(_shape, poly);'
    +.print 
    +EXPLAIN QUERY PLAN
    +SELECT geopoly_svg(_shape,
    +         printf('style="fill:none;stroke:%s;stroke-width:1"',geo1.clr)
    +       )
    +  FROM geo1, querypoly
    + WHERE NOT geopoly_within(_shape, poly);
    +.print '
    ' +.print '' +SELECT geopoly_svg(_shape, + printf('style="fill:none;stroke:%s;stroke-width:1"',geo1.clr) + ) + FROM geo1, querypoly + WHERE NOT geopoly_within(_shape, poly); +SELECT geopoly_svg(poly, + printf('style="fill:%s;fill-opacity:0.5;"',clr) + ) + FROM querypoly; +.print '' + +.print '

    Color-Change For Overlapping Elements

    ' +BEGIN; +UPDATE geo1 + SET clr=CASE WHEN rowid IN (SELECT geo1.rowid FROM geo1, querypoly + WHERE geopoly_overlap(_shape,poly)) + THEN 'red' ELSE 'blue' END; +.print '' +SELECT geopoly_svg(_shape, + printf('style="fill:none;stroke:%s;stroke-width:1"',geo1.clr) + ) + FROM geo1; +SELECT geopoly_svg(poly,'style="fill:none;stroke:black;stroke-width:2"') + FROM querypoly; +ROLLBACK; +.print '' + +.print '

    Color-Change And Move Overlapping Elements

    ' +BEGIN; +UPDATE geo1 + SET clr=CASE WHEN rowid IN (SELECT geo1.rowid FROM geo1, querypoly + WHERE geopoly_overlap(_shape,poly)) + THEN 'red' ELSE '#76ccff' END; +UPDATE geo1 + SET _shape=geopoly_xform(_shape,1,0,0,1,300,0) + WHERE geopoly_overlap(_shape,(SELECT poly FROM querypoly)); +.print '' +SELECT geopoly_svg(_shape, + printf('style="fill:none;stroke:%s;stroke-width:1"',geo1.clr) + ) + FROM geo1; +SELECT geopoly_svg(poly,'style="fill:none;stroke:black;stroke-width:2"') + FROM querypoly; +--ROLLBACK; +.print '' + + +.print '

    Overlap With Translated Query Polygon

    ' +UPDATE querypoly SET poly=geopoly_xform(poly,1,0,0,1,300,0); +.print '' +SELECT geopoly_svg(_shape, + printf('style="fill:none;stroke:%s;stroke-width:1"',geo1.clr) + ) + FROM geo1 + WHERE geopoly_overlap(_shape,(SELECT poly FROM querypoly)); +SELECT geopoly_svg(poly,'style="fill:none;stroke:black;stroke-width:2"') + FROM querypoly; +ROLLBACK; +.print '' + +.print '

    Regular Polygons

    ' +.print '' +SELECT geopoly_svg(geopoly_regular(100,100,40,3),'style="fill:none;stroke:red;stroke-width:1"'); +SELECT geopoly_svg(geopoly_regular(200,100,40,4),'style="fill:none;stroke:orange;stroke-width:1"'); +SELECT geopoly_svg(geopoly_regular(300,100,40,5),'style="fill:none;stroke:green;stroke-width:1"'); +SELECT geopoly_svg(geopoly_regular(400,100,40,6),'style="fill:none;stroke:blue;stroke-width:1"'); +SELECT geopoly_svg(geopoly_regular(500,100,40,7),'style="fill:none;stroke:purple;stroke-width:1"'); +SELECT geopoly_svg(geopoly_regular(600,100,40,8),'style="fill:none;stroke:red;stroke-width:1"'); +SELECT geopoly_svg(geopoly_regular(700,100,40,10),'style="fill:none;stroke:orange;stroke-width:1"'); +SELECT geopoly_svg(geopoly_regular(800,100,40,20),'style="fill:none;stroke:green;stroke-width:1"'); +SELECT geopoly_svg(geopoly_regular(900,100,40,30),'style="fill:none;stroke:blue;stroke-width:1"'); +.print '' + +.print '' diff --git a/ext/session/changeset.c b/ext/session/changeset.c new file mode 100644 index 0000000000..fe5c4dc5e7 --- /dev/null +++ b/ext/session/changeset.c @@ -0,0 +1,472 @@ +/* +** 2014-08-18 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains code to implement the "changeset" command line +** utility for displaying and transforming changesets generated by +** the Sessions extension. +*/ +#include "sqlite3.h" +#include +#include +#include +#include +#include + + +/* +** Show a usage message on stderr then quit. +*/ +static void usage(const char *argv0){ + fprintf(stderr, "Usage: %s FILENAME COMMAND ...\n", argv0); + fprintf(stderr, + "COMMANDs:\n" + " apply DB [OPTIONS] Apply the changeset to database file DB. OPTIONS:\n" + " -n|--dryrun Test run. Don't apply changes\n" + " --enablefk Enable FOREIGN KEY support\n" + " --nosavepoint \\\n" + " --invert \\___ Flags passed into\n" + " --ignorenoop / changeset_apply_v2()\n" + " --fknoaction /\n" + " concat FILE2 OUT Concatenate FILENAME and FILE2 into OUT\n" + " dump Show the complete content of the changeset\n" + " invert OUT Write an inverted changeset into file OUT\n" + " sql Give a pseudo-SQL rendering of the changeset\n" + ); + exit(1); +} + +/* +** Read the content of a disk file into an in-memory buffer +*/ +static void readFile(const char *zFilename, int *pSz, void **ppBuf){ + FILE *f; + sqlite3_int64 sz; + void *pBuf; + f = fopen(zFilename, "rb"); + if( f==0 ){ + fprintf(stderr, "cannot open \"%s\" for reading\n", zFilename); + exit(1); + } + fseek(f, 0, SEEK_END); + sz = ftell(f); + rewind(f); + pBuf = sqlite3_malloc64( sz ? sz : 1 ); + if( pBuf==0 ){ + fprintf(stderr, "cannot allocate %d to hold content of \"%s\"\n", + (int)sz, zFilename); + exit(1); + } + if( sz>0 ){ + if( fread(pBuf, (size_t)sz, 1, f)!=1 ){ + fprintf(stderr, "cannot read all %d bytes of \"%s\"\n", + (int)sz, zFilename); + exit(1); + } + fclose(f); + } + *pSz = (int)sz; + *ppBuf = pBuf; +} + +/* Array for converting from half-bytes (nybbles) into ASCII hex +** digits. */ +static const char hexdigits[] = { + '0', '1', '2', '3', '4', '5', '6', '7', + '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' +}; + +/* +** Render an sqlite3_value as an SQL string. +*/ +static void renderValue(sqlite3_value *pVal){ + switch( sqlite3_value_type(pVal) ){ + case SQLITE_FLOAT: { + double r1; + char zBuf[50]; + r1 = sqlite3_value_double(pVal); + sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.15g", r1); + printf("%s", zBuf); + break; + } + case SQLITE_INTEGER: { + printf("%lld", sqlite3_value_int64(pVal)); + break; + } + case SQLITE_BLOB: { + char const *zBlob = sqlite3_value_blob(pVal); + int nBlob = sqlite3_value_bytes(pVal); + int i; + printf("x'"); + for(i=0; i>4)&0x0F]); + putchar(hexdigits[(zBlob[i])&0x0F]); + } + putchar('\''); + break; + } + case SQLITE_TEXT: { + const unsigned char *zArg = sqlite3_value_text(pVal); + putchar('\''); + while( zArg[0] ){ + putchar(zArg[0]); + if( zArg[0]=='\'' ) putchar(zArg[0]); + zArg++; + } + putchar('\''); + break; + } + default: { + assert( sqlite3_value_type(pVal)==SQLITE_NULL ); + printf("NULL"); + break; + } + } +} + +/* +** Number of conflicts seen +*/ +static int nConflict = 0; + +/* +** The conflict callback +*/ +static int conflictCallback( + void *pCtx, + int eConflict, + sqlite3_changeset_iter *pIter +){ + int op, bIndirect, nCol, i; + const char *zTab; + unsigned char *abPK; + const char *zType = ""; + const char *zOp = ""; + const char *zSep = " "; + + nConflict++; + sqlite3changeset_op(pIter, &zTab, &nCol, &op, &bIndirect); + sqlite3changeset_pk(pIter, &abPK, 0); + switch( eConflict ){ + case SQLITE_CHANGESET_DATA: zType = "DATA"; break; + case SQLITE_CHANGESET_NOTFOUND: zType = "NOTFOUND"; break; + case SQLITE_CHANGESET_CONFLICT: zType = "PRIMARY KEY"; break; + case SQLITE_CHANGESET_FOREIGN_KEY: zType = "FOREIGN KEY"; break; + case SQLITE_CHANGESET_CONSTRAINT: zType = "CONSTRAINT"; break; + } + switch( op ){ + case SQLITE_UPDATE: zOp = "UPDATE of"; break; + case SQLITE_INSERT: zOp = "INSERT into"; break; + case SQLITE_DELETE: zOp = "DELETE from"; break; + } + printf("%s conflict on %s table %s with primary key", zType, zOp, zTab); + for(i=0; i0 && fwrite(pOutBuf, szOut, 1, out)!=1 ){ + fprintf(stderr, "unable to write all %d bytes of output to \"%s\"\n", + szOut, zOut); + } + fclose(out); + sqlite3_free(pOutBuf); + sqlite3_free(pB); + }else + + /* changeset FILENAME dump + ** Show the complete content of the changeset in FILENAME + */ + if( strcmp(argv[2],"dump")==0 ){ + int cnt = 0; + int i; + sqlite3_changeset_iter *pIter; + rc = sqlite3changeset_start(&pIter, sz, pBuf); + if( rc!=SQLITE_OK ){ + fprintf(stderr, "sqlite3changeset_start() returns %d\n", rc); + exit(1); + } + while( sqlite3changeset_next(pIter)==SQLITE_ROW ){ + int op, bIndirect, nCol; + const char *zTab; + unsigned char *abPK; + sqlite3changeset_op(pIter, &zTab, &nCol, &op, &bIndirect); + cnt++; + printf("%d: %s table=[%s] indirect=%d nColumn=%d\n", + cnt, op==SQLITE_INSERT ? "INSERT" : + op==SQLITE_UPDATE ? "UPDATE" : "DELETE", + zTab, bIndirect, nCol); + sqlite3changeset_pk(pIter, &abPK, 0); + for(i=0; i0 && fwrite(pOutBuf, szOut, 1, out)!=1 ){ + fprintf(stderr, "unable to write all %d bytes of output to \"%s\"\n", + szOut, zOut); + } + fclose(out); + sqlite3_free(pOutBuf); + }else + + /* changeset FILE sql + ** Show the content of the changeset as pseudo-SQL + */ + if( strcmp(argv[2],"sql")==0 ){ + int cnt = 0; + char *zPrevTab = 0; + char *zSQLTabName = 0; + sqlite3_changeset_iter *pIter = 0; + rc = sqlite3changeset_start(&pIter, sz, pBuf); + if( rc!=SQLITE_OK ){ + fprintf(stderr, "sqlite3changeset_start() returns %d\n", rc); + exit(1); + } + printf("BEGIN;\n"); + while( sqlite3changeset_next(pIter)==SQLITE_ROW ){ + int op, bIndirect, nCol; + const char *zTab; + sqlite3changeset_op(pIter, &zTab, &nCol, &op, &bIndirect); + cnt++; + if( zPrevTab==0 || strcmp(zPrevTab,zTab)!=0 ){ + sqlite3_free(zPrevTab); + sqlite3_free(zSQLTabName); + zPrevTab = sqlite3_mprintf("%s", zTab); + if( !isalnum(zTab[0]) || sqlite3_strglob("*[^a-zA-Z0-9]*",zTab)==0 ){ + zSQLTabName = sqlite3_mprintf("\"%w\"", zTab); + }else{ + zSQLTabName = sqlite3_mprintf("%s", zTab); + } + printf("/****** Changes for table %s ***************/\n", zSQLTabName); + } + switch( op ){ + case SQLITE_DELETE: { + unsigned char *abPK; + int i; + const char *zSep = " "; + sqlite3changeset_pk(pIter, &abPK, 0); + printf("/* %d */ DELETE FROM %s WHERE", cnt, zSQLTabName); + for(i=0; i1). +** +** 8. The "indirect" flag may be toggled for any change. +** +** Entire group of changes may also be operated on: +** +** 9. Duplicate an existing group. +** +** 10. Remove an existing group. +** +** 11. The positions of two groups may be exchanged. +** +** There are also schema changes: +** +** 12. A non-PK column may be added to a table. In this case a NULL +** value is appended to all records. +** +** 13. A PK column may be added to a table. In this case a non-NULL +** value is appended to all INSERT, DELETE and UPDATE old.* records. +** An "undefined" is appended to new.* UPDATE records. +** +** 14. A column may be removed from a table, provided that it is not the +** only PRIMARY KEY column in the table. In this case the corresponding +** field is removed from all records. In cases where this leaves an UPDATE +** with no non-PK, non-undefined fields, the entire change is removed. +*/ + +#include "sqlite3.h" +#include +#include +#include +#include +#include + +#define FUZZ_VALUE_SUB 1 /* Replace one value with a copy of another */ +#define FUZZ_VALUE_MOD 2 /* Modify content by 1 bit */ +#define FUZZ_VALUE_RND 3 /* Replace with pseudo-random value */ + +#define FUZZ_CHANGE_DUP 4 /* Duplicate an existing change */ +#define FUZZ_CHANGE_DEL 5 /* Completely remove one change */ +#define FUZZ_CHANGE_TYPE 6 /* Change the type of one change */ +#define FUZZ_CHANGE_FIELD 7 /* Change an UPDATE to modify fewer columns */ +#define FUZZ_CHANGE_INDIRECT 8 /* Toggle the "indirect" flag of a change */ + +#define FUZZ_GROUP_DUP 9 /* Duplicate a change group */ +#define FUZZ_GROUP_DEL 10 /* Delete an entire change group */ +#define FUZZ_GROUP_SWAP 11 /* Exchange the position of two groups */ + +#define FUZZ_COLUMN_ADD 12 /* Add column to table definition */ +#define FUZZ_COLUMN_ADDPK 13 /* Add PK column to table definition */ +#define FUZZ_COLUMN_DEL 14 /* Remove column from table definition */ + + + +typedef unsigned char u8; +typedef sqlite3_uint64 u64; +typedef sqlite3_int64 i64; +typedef unsigned int u32; + +/* +** Show a usage message on stderr then quit. +*/ +static void usage(const char *argv0){ + fprintf(stderr, "Usage: %s FILENAME ?SEED N?\n", argv0); + exit(1); +} + +/* +** Read the content of a disk file into an in-memory buffer +*/ +static void fuzzReadFile(const char *zFilename, int *pSz, void **ppBuf){ + FILE *f; + sqlite3_int64 sz; + void *pBuf; + f = fopen(zFilename, "rb"); + if( f==0 ){ + fprintf(stderr, "cannot open \"%s\" for reading\n", zFilename); + exit(1); + } + fseek(f, 0, SEEK_END); + sz = ftell(f); + rewind(f); + pBuf = sqlite3_malloc64( sz ? sz : 1 ); + if( pBuf==0 ){ + fprintf(stderr, "cannot allocate %d to hold content of \"%s\"\n", + (int)sz, zFilename); + exit(1); + } + if( sz>0 ){ + if( fread(pBuf, (size_t)sz, 1, f)!=1 ){ + fprintf(stderr, "cannot read all %d bytes of \"%s\"\n", + (int)sz, zFilename); + exit(1); + } + fclose(f); + } + *pSz = (int)sz; + *ppBuf = pBuf; +} + +/* +** Write the contents of buffer pBuf, size nBuf bytes, into file zFilename +** on disk. zFilename, if it already exists, is clobbered. +*/ +static void fuzzWriteFile(const char *zFilename, void *pBuf, int nBuf){ + FILE *f; + f = fopen(zFilename, "wb"); + if( f==0 ){ + fprintf(stderr, "cannot open \"%s\" for writing\n", zFilename); + exit(1); + } + if( fwrite(pBuf, nBuf, 1, f)!=1 ){ + fprintf(stderr, "cannot write to \"%s\"\n", zFilename); + exit(1); + } + fclose(f); +} + +static int fuzzCorrupt(){ + return SQLITE_CORRUPT; +} + +/************************************************************************* +** The following block is a copy of the implementation of SQLite function +** sqlite3_randomness. This version has two important differences: +** +** 1. It always uses the same seed. So the sequence of random data output +** is the same for every run of the program. +** +** 2. It is not threadsafe. +*/ +static struct sqlite3PrngType { + unsigned char i, j; /* State variables */ + unsigned char s[256]; /* State variables */ +} sqlite3Prng = { + 0xAF, 0x28, + { + 0x71, 0xF5, 0xB4, 0x6E, 0x80, 0xAB, 0x1D, 0xB8, + 0xFB, 0xB7, 0x49, 0xBF, 0xFF, 0x72, 0x2D, 0x14, + 0x79, 0x09, 0xE3, 0x78, 0x76, 0xB0, 0x2C, 0x0A, + 0x8E, 0x23, 0xEE, 0xDF, 0xE0, 0x9A, 0x2F, 0x67, + 0xE1, 0xBE, 0x0E, 0xA7, 0x08, 0x97, 0xEB, 0x77, + 0x78, 0xBA, 0x9D, 0xCA, 0x49, 0x4C, 0x60, 0x9A, + 0xF6, 0xBD, 0xDA, 0x7F, 0xBC, 0x48, 0x58, 0x52, + 0xE5, 0xCD, 0x83, 0x72, 0x23, 0x52, 0xFF, 0x6D, + 0xEF, 0x0F, 0x82, 0x29, 0xA0, 0x83, 0x3F, 0x7D, + 0xA4, 0x88, 0x31, 0xE7, 0x88, 0x92, 0x3B, 0x9B, + 0x3B, 0x2C, 0xC2, 0x4C, 0x71, 0xA2, 0xB0, 0xEA, + 0x36, 0xD0, 0x00, 0xF1, 0xD3, 0x39, 0x17, 0x5D, + 0x2A, 0x7A, 0xE4, 0xAD, 0xE1, 0x64, 0xCE, 0x0F, + 0x9C, 0xD9, 0xF5, 0xED, 0xB0, 0x22, 0x5E, 0x62, + 0x97, 0x02, 0xA3, 0x8C, 0x67, 0x80, 0xFC, 0x88, + 0x14, 0x0B, 0x15, 0x10, 0x0F, 0xC7, 0x40, 0xD4, + 0xF1, 0xF9, 0x0E, 0x1A, 0xCE, 0xB9, 0x1E, 0xA1, + 0x72, 0x8E, 0xD7, 0x78, 0x39, 0xCD, 0xF4, 0x5D, + 0x2A, 0x59, 0x26, 0x34, 0xF2, 0x73, 0x0B, 0xA0, + 0x02, 0x51, 0x2C, 0x03, 0xA3, 0xA7, 0x43, 0x13, + 0xE8, 0x98, 0x2B, 0xD2, 0x53, 0xF8, 0xEE, 0x91, + 0x7D, 0xE7, 0xE3, 0xDA, 0xD5, 0xBB, 0xC0, 0x92, + 0x9D, 0x98, 0x01, 0x2C, 0xF9, 0xB9, 0xA0, 0xEB, + 0xCF, 0x32, 0xFA, 0x01, 0x49, 0xA5, 0x1D, 0x9A, + 0x76, 0x86, 0x3F, 0x40, 0xD4, 0x89, 0x8F, 0x9C, + 0xE2, 0xE3, 0x11, 0x31, 0x37, 0xB2, 0x49, 0x28, + 0x35, 0xC0, 0x99, 0xB6, 0xD0, 0xBC, 0x66, 0x35, + 0xF7, 0x83, 0x5B, 0xD7, 0x37, 0x1A, 0x2B, 0x18, + 0xA6, 0xFF, 0x8D, 0x7C, 0x81, 0xA8, 0xFC, 0x9E, + 0xC4, 0xEC, 0x80, 0xD0, 0x98, 0xA7, 0x76, 0xCC, + 0x9C, 0x2F, 0x7B, 0xFF, 0x8E, 0x0E, 0xBB, 0x90, + 0xAE, 0x13, 0x06, 0xF5, 0x1C, 0x4E, 0x52, 0xF7 + } +}; + +/* +** Generate and return single random byte +*/ +static unsigned char fuzzRandomByte(void){ + unsigned char t; + sqlite3Prng.i++; + t = sqlite3Prng.s[sqlite3Prng.i]; + sqlite3Prng.j += t; + sqlite3Prng.s[sqlite3Prng.i] = sqlite3Prng.s[sqlite3Prng.j]; + sqlite3Prng.s[sqlite3Prng.j] = t; + t += sqlite3Prng.s[sqlite3Prng.i]; + return sqlite3Prng.s[t]; +} + +/* +** Return N random bytes. +*/ +static void fuzzRandomBlob(int nBuf, unsigned char *zBuf){ + int i; + for(i=0; i0 ); + fuzzRandomBlob(sizeof(ret), (unsigned char*)&ret); + return (ret % nRange); +} + +static u64 fuzzRandomU64(){ + u64 ret; + fuzzRandomBlob(sizeof(ret), (unsigned char*)&ret); + return ret; +} + +static void fuzzRandomSeed(unsigned int iSeed){ + int i; + for(i=0; i<256; i+=4){ + sqlite3Prng.s[i] ^= ((iSeed >> 24) & 0xFF); + sqlite3Prng.s[i+1] ^= ((iSeed >> 16) & 0xFF); + sqlite3Prng.s[i+2] ^= ((iSeed >> 8) & 0xFF); + sqlite3Prng.s[i+3] ^= ((iSeed >> 0) & 0xFF); + } +} +/* +** End of code for generating pseudo-random values. +*************************************************************************/ + +typedef struct FuzzChangeset FuzzChangeset; +typedef struct FuzzChangesetGroup FuzzChangesetGroup; +typedef struct FuzzChange FuzzChange; + +/* +** Object containing partially parsed changeset. +*/ +struct FuzzChangeset { + int bPatchset; /* True for a patchset */ + FuzzChangesetGroup **apGroup; /* Array of groups in changeset */ + int nGroup; /* Number of items in list pGroup */ + u8 **apVal; /* Array of all values in changeset */ + int nVal; /* Number of used slots in apVal[] */ + int nChange; /* Number of changes in changeset */ + int nUpdate; /* Number of UPDATE changes in changeset */ +}; + +/* +** There is one object of this type for each change-group (table header) +** in the input changeset. +*/ +struct FuzzChangesetGroup { + const char *zTab; /* Name of table */ + int nCol; /* Number of columns in table */ + u8 *aPK; /* PK array for this table */ + u8 *aChange; /* Buffer containing array of changes */ + int szChange; /* Size of buffer aChange[] in bytes */ + int nChange; /* Number of changes in buffer aChange[] */ +}; + +/* +** Description of a fuzz change to be applied to a changeset. +*/ +struct FuzzChange { + int eType; /* One of the FUZZ_* constants above */ + int iChange; /* Change or UPDATE to modify */ + int iGroup; /* Group to modify */ + int iDelete; /* Field to remove (FUZZ_COLUMN_DEL) */ + u8 *pSub1; /* Replace this value with pSub2 */ + u8 *pSub2; /* And this one with pSub1 */ + u8 aSub[128]; /* Buffer for substitute value */ + int iCurrent; /* Current change number */ +}; + +/* +** Allocate and return nByte bytes of zeroed memory. +*/ +static void *fuzzMalloc(sqlite3_int64 nByte){ + void *pRet = sqlite3_malloc64(nByte); + if( pRet ){ + memset(pRet, 0, (size_t)nByte); + } + return pRet; +} + +/* +** Free the buffer indicated by the first argument. This function is used +** to free buffers allocated by fuzzMalloc(). +*/ +static void fuzzFree(void *p){ + sqlite3_free(p); +} + +/* +** Argument p points to a buffer containing an SQLite varint that, assuming the +** input is not corrupt, may be between 0 and 0x7FFFFFFF, inclusive. Before +** returning, this function sets (*pnVal) to the value of that varint, and +** returns the number of bytes of space that it takes up. +*/ +static int fuzzGetVarint(u8 *p, int *pnVal){ + int i; + sqlite3_uint64 nVal = 0; + for(i=0; i<9; i++){ + nVal = (nVal<<7) + (p[i] & 0x7F); + if( (p[i] & 0x80)==0 ){ + i++; + break; + } + } + *pnVal = (int)nVal; + return i; +} + +/* +** Write value nVal into the buffer indicated by argument p as an SQLite +** varint. nVal is guaranteed to be between 0 and (2^21-1), inclusive. +** Return the number of bytes written to buffer p. +*/ +static int fuzzPutVarint(u8 *p, int nVal){ + assert( nVal>0 && nVal<2097152 ); + if( nVal<128 ){ + p[0] = (u8)nVal; + return 1; + } + if( nVal<16384 ){ + p[0] = ((nVal >> 7) & 0x7F) | 0x80; + p[1] = (nVal & 0x7F); + return 2; + } + + p[0] = ((nVal >> 14) & 0x7F) | 0x80; + p[1] = ((nVal >> 7) & 0x7F) | 0x80; + p[2] = (nVal & 0x7F); + return 3; +} + +/* +** Read a 64-bit big-endian integer value from buffer aRec[]. Return +** the value read. +*/ +static i64 fuzzGetI64(u8 *aRec){ + return (i64)( + (((u64)aRec[0]) << 56) + + (((u64)aRec[1]) << 48) + + (((u64)aRec[2]) << 40) + + (((u64)aRec[3]) << 32) + + (((u64)aRec[4]) << 24) + + (((u64)aRec[5]) << 16) + + (((u64)aRec[6]) << 8) + + (((u64)aRec[7]) << 0) + ); +} + +/* +** Write value iVal to buffer aRec[] as an unsigned 64-bit big-endian integer. +*/ +static void fuzzPutU64(u8 *aRec, u64 iVal){ + aRec[0] = (iVal>>56) & 0xFF; + aRec[1] = (iVal>>48) & 0xFF; + aRec[2] = (iVal>>40) & 0xFF; + aRec[3] = (iVal>>32) & 0xFF; + aRec[4] = (iVal>>24) & 0xFF; + aRec[5] = (iVal>>16) & 0xFF; + aRec[6] = (iVal>> 8) & 0xFF; + aRec[7] = (iVal) & 0xFF; +} + +/* +** Parse a single table-header from the input. Allocate a new change-group +** object with the results. Return SQLITE_OK if successful, or an error code +** otherwise. +*/ +static int fuzzParseHeader( + FuzzChangeset *pParse, /* Changeset parse object */ + u8 **ppHdr, /* IN/OUT: Iterator */ + u8 *pEnd, /* 1 byte past EOF */ + FuzzChangesetGroup **ppGrp /* OUT: New change-group object */ +){ + int rc = SQLITE_OK; + FuzzChangesetGroup *pGrp; + u8 cHdr = (pParse->bPatchset ? 'P' : 'T'); + + assert( pEnd>(*ppHdr) ); + pGrp = (FuzzChangesetGroup*)fuzzMalloc(sizeof(FuzzChangesetGroup)); + if( !pGrp ){ + rc = SQLITE_NOMEM; + }else{ + u8 *p = *ppHdr; + if( p[0]!=cHdr ){ + rc = fuzzCorrupt(); + }else{ + p++; + p += fuzzGetVarint(p, &pGrp->nCol); + pGrp->aPK = p; + p += pGrp->nCol; + pGrp->zTab = (const char*)p; + p = &p[strlen((const char*)p)+1]; + + if( p>=pEnd ){ + rc = fuzzCorrupt(); + } + } + *ppHdr = p; + } + + if( rc!=SQLITE_OK ){ + fuzzFree(pGrp); + pGrp = 0; + } + + *ppGrp = pGrp; + return rc; +} + +/* +** Argument p points to a buffer containing a single changeset-record value. +** This function attempts to determine the size of the value in bytes. If +** successful, it sets (*pSz) to the size and returns SQLITE_OK. Or, if the +** buffer does not contain a valid value, SQLITE_CORRUPT is returned and +** the final value of (*pSz) is undefined. +*/ +static int fuzzChangeSize(u8 *p, int *pSz){ + u8 eType = p[0]; + switch( eType ){ + case 0x00: /* undefined */ + case 0x05: /* null */ + *pSz = 1; + break; + + case 0x01: /* integer */ + case 0x02: /* real */ + *pSz = 9; + break; + + case 0x03: /* text */ + case 0x04: { /* blob */ + int nTxt; + int sz; + sz = fuzzGetVarint(&p[1], &nTxt); + *pSz = 1 + sz + nTxt; + break; + } + + default: + return fuzzCorrupt(); + } + return SQLITE_OK; +} + +/* +** When this function is called, (*ppRec) points to the start of a +** record in a changeset being parsed. This function adds entries +** to the pParse->apVal[] array for all values and advances (*ppRec) +** to one byte past the end of the record. Argument pEnd points to +** one byte past the end of the input changeset. +** +** Argument bPkOnly is true if the record being parsed is part of +** a DELETE record in a patchset. In this case, all non-primary-key +** fields have been omitted from the record. +** +** SQLITE_OK is returned if successful, or an SQLite error code otherwise. +*/ +static int fuzzParseRecord( + u8 **ppRec, /* IN/OUT: Iterator */ + u8 *pEnd, /* One byte after end of input data */ + FuzzChangeset *pParse, /* Changeset parse context */ + int bPkOnly /* True if non-PK fields omitted */ +){ + int rc = SQLITE_OK; + FuzzChangesetGroup *pGrp = pParse->apGroup[pParse->nGroup-1]; + int i; + u8 *p = *ppRec; + + for(i=0; rc==SQLITE_OK && inCol; i++){ + if( bPkOnly==0 || pGrp->aPK[i] ){ + int sz; + if( p>=pEnd ) break; + if( (pParse->nVal & (pParse->nVal-1))==0 ){ + int nNew = pParse->nVal ? pParse->nVal*2 : 4; + u8 **apNew = (u8**)sqlite3_realloc(pParse->apVal, nNew*sizeof(u8*)); + if( apNew==0 ) return SQLITE_NOMEM; + pParse->apVal = apNew; + } + pParse->apVal[pParse->nVal++] = p; + rc = fuzzChangeSize(p, &sz); + p += sz; + } + } + + if( rc==SQLITE_OK && inCol ){ + rc = fuzzCorrupt(); + } + + *ppRec = p; + return rc; +} + +/* +** Parse the array of changes starting at (*ppData) and add entries for +** all values to the pParse->apVal[] array. Argument pEnd points to one byte +** past the end of the input changeset. If successful, set (*ppData) to point +** to one byte past the end of the change array and return SQLITE_OK. +** Otherwise, return an SQLite error code. The final value of (*ppData) is +** undefined in this case. +*/ +static int fuzzParseChanges(u8 **ppData, u8 *pEnd, FuzzChangeset *pParse){ + u8 cHdr = (pParse->bPatchset ? 'P' : 'T'); + FuzzChangesetGroup *pGrp = pParse->apGroup[pParse->nGroup-1]; + int rc = SQLITE_OK; + u8 *p = *ppData; + + pGrp->aChange = p; + while( rc==SQLITE_OK && pnUpdate++; + if( pParse->bPatchset==0 ){ + rc = fuzzParseRecord(&p, pEnd, pParse, 0); + } + }else if( eOp!=SQLITE_INSERT && eOp!=SQLITE_DELETE ){ + rc = fuzzCorrupt(); + } + if( rc==SQLITE_OK ){ + int bPkOnly = (eOp==SQLITE_DELETE && pParse->bPatchset); + rc = fuzzParseRecord(&p, pEnd, pParse, bPkOnly); + } + pGrp->nChange++; + pParse->nChange++; + } + pGrp->szChange = p - pGrp->aChange; + + *ppData = p; + return rc; +} + +/* +** Parse the changeset stored in buffer pChangeset (nChangeset bytes in +** size). If successful, write the results into (*pParse) and return +** SQLITE_OK. Or, if an error occurs, return an SQLite error code. The +** final state of (*pParse) is undefined in this case. +*/ +static int fuzzParseChangeset( + u8 *pChangeset, /* Buffer containing changeset */ + int nChangeset, /* Size of buffer in bytes */ + FuzzChangeset *pParse /* OUT: Results of parse */ +){ + u8 *pEnd = &pChangeset[nChangeset]; + u8 *p = pChangeset; + int rc = SQLITE_OK; + + memset(pParse, 0, sizeof(FuzzChangeset)); + if( nChangeset>0 ){ + pParse->bPatchset = (pChangeset[0]=='P'); + } + + while( rc==SQLITE_OK && papGroup, sizeof(FuzzChangesetGroup*)*(pParse->nGroup+1) + ); + if( apNew==0 ){ + rc = SQLITE_NOMEM; + }else{ + apNew[pParse->nGroup] = pGrp; + pParse->apGroup = apNew; + pParse->nGroup++; + } + rc = fuzzParseChanges(&p, pEnd, pParse); + } + } + + return rc; +} + +/* +** When this function is called, (*ppRec) points to the first byte of +** a record that is part of change-group pGrp. This function attempts +** to output a human-readable version of the record to stdout and advance +** (*ppRec) to point to the first byte past the end of the record before +** returning. If successful, SQLITE_OK is returned. Otherwise, an SQLite +** error code. +** +** If parameter bPkOnly is non-zero, then all non-primary-key fields have +** been omitted from the record. This occurs for records that are part +** of DELETE changes in patchsets. +*/ +static int fuzzPrintRecord(FuzzChangesetGroup *pGrp, u8 **ppRec, int bPKOnly){ + int rc = SQLITE_OK; + u8 *p = *ppRec; + int i; + const char *zPre = " ("; + + for(i=0; inCol; i++){ + if( bPKOnly==0 || pGrp->aPK[i] ){ + u8 eType = p++[0]; + switch( eType ){ + case 0x00: /* undefined */ + printf("%sn/a", zPre); + break; + + case 0x01: { /* integer */ + sqlite3_int64 iVal = 0; + iVal = fuzzGetI64(p); + printf("%s%lld", zPre, iVal); + p += 8; + break; + } + + case 0x02: { /* real */ + sqlite3_int64 iVal = 0; + double fVal = 0.0; + iVal = fuzzGetI64(p); + memcpy(&fVal, &iVal, 8); + printf("%s%f", zPre, fVal); + p += 8; + break; + } + + case 0x03: /* text */ + case 0x04: { /* blob */ + int nTxt; + p += fuzzGetVarint(p, &nTxt); + printf("%s%s", zPre, eType==0x03 ? "'" : "X'"); + for(i=0; i>4 ]); + printf("%c", aHex[ p[i] & 0x0F ]); + } + } + printf("'"); + p += nTxt; + break; + } + + case 0x05: /* null */ + printf("%sNULL", zPre); + break; + } + zPre = ", "; + } + } + printf(")"); + + *ppRec = p; + return rc; +} + +/* +** Print a human-readable version of the table-header and all changes in the +** change-group passed as the second argument. +*/ +static void fuzzPrintGroup(FuzzChangeset *pParse, FuzzChangesetGroup *pGrp){ + int i; + u8 *p; + + /* The table header */ + printf("TABLE: %s nCol=%d aPK=", pGrp->zTab, pGrp->nCol); + for(i=0; inCol; i++){ + printf("%d", (int)pGrp->aPK[i]); + } + printf("\n"); + + /* The array of changes */ + p = pGrp->aChange; + for(i=0; inChange; i++){ + u8 eType = p[0]; + u8 bIndirect = p[1]; + printf("%s (ind=%d):", + (eType==SQLITE_INSERT) ? "INSERT" : + (eType==SQLITE_DELETE ? "DELETE" : "UPDATE"), + bIndirect + ); + p += 2; + + if( pParse->bPatchset==0 && eType==SQLITE_UPDATE ){ + fuzzPrintRecord(pGrp, &p, 0); + } + fuzzPrintRecord(pGrp, &p, eType==SQLITE_DELETE && pParse->bPatchset); + printf("\n"); + } +} + +/* +** Initialize the object passed as the second parameter with details +** of the change that will be attempted (type of change, to which part of the +** changeset it applies etc.). If successful, return SQLITE_OK. Or, if an +** error occurs, return an SQLite error code. +** +** If a negative value is returned, then the selected change would have +** produced a non-well-formed changeset. In this case the caller should +** call this function again. +*/ +static int fuzzSelectChange(FuzzChangeset *pParse, FuzzChange *pChange){ + int iSub; + + memset(pChange, 0, sizeof(FuzzChange)); + pChange->eType = fuzzRandomInt(FUZZ_COLUMN_DEL) + 1; + + assert( pChange->eType==FUZZ_VALUE_SUB + || pChange->eType==FUZZ_VALUE_MOD + || pChange->eType==FUZZ_VALUE_RND + || pChange->eType==FUZZ_CHANGE_DUP + || pChange->eType==FUZZ_CHANGE_DEL + || pChange->eType==FUZZ_CHANGE_TYPE + || pChange->eType==FUZZ_CHANGE_FIELD + || pChange->eType==FUZZ_CHANGE_INDIRECT + || pChange->eType==FUZZ_GROUP_DUP + || pChange->eType==FUZZ_GROUP_DEL + || pChange->eType==FUZZ_GROUP_SWAP + || pChange->eType==FUZZ_COLUMN_ADD + || pChange->eType==FUZZ_COLUMN_ADDPK + || pChange->eType==FUZZ_COLUMN_DEL + ); + + pChange->iGroup = fuzzRandomInt(pParse->nGroup); + pChange->iChange = fuzzRandomInt(pParse->nChange); + if( pChange->eType==FUZZ_CHANGE_FIELD ){ + if( pParse->nUpdate==0 ) return -1; + pChange->iChange = fuzzRandomInt(pParse->nUpdate); + } + + pChange->iDelete = -1; + if( pChange->eType==FUZZ_COLUMN_DEL ){ + FuzzChangesetGroup *pGrp = pParse->apGroup[pChange->iGroup]; + int i; + pChange->iDelete = fuzzRandomInt(pGrp->nCol); + for(i=pGrp->nCol-1; i>=0; i--){ + if( pGrp->aPK[i] && pChange->iDelete!=i ) break; + } + if( i<0 ) return -1; + } + + if( pChange->eType==FUZZ_GROUP_SWAP ){ + FuzzChangesetGroup *pGrp; + int iGrp = pChange->iGroup; + if( pParse->nGroup==1 ) return -1; + while( iGrp==pChange->iGroup ){ + iGrp = fuzzRandomInt(pParse->nGroup); + } + pGrp = pParse->apGroup[pChange->iGroup]; + pParse->apGroup[pChange->iGroup] = pParse->apGroup[iGrp]; + pParse->apGroup[iGrp] = pGrp; + } + + if( pChange->eType==FUZZ_VALUE_SUB + || pChange->eType==FUZZ_VALUE_MOD + || pChange->eType==FUZZ_VALUE_RND + ){ + iSub = fuzzRandomInt(pParse->nVal); + pChange->pSub1 = pParse->apVal[iSub]; + if( pChange->eType==FUZZ_VALUE_SUB ){ + iSub = fuzzRandomInt(pParse->nVal); + pChange->pSub2 = pParse->apVal[iSub]; + }else{ + pChange->pSub2 = pChange->aSub; + } + + if( pChange->eType==FUZZ_VALUE_RND ){ + pChange->aSub[0] = (u8)(fuzzRandomInt(5) + 1); + switch( pChange->aSub[0] ){ + case 0x01: { /* integer */ + u64 iVal = fuzzRandomU64(); + fuzzPutU64(&pChange->aSub[1], iVal); + break; + } + + case 0x02: { /* real */ + u64 iVal1 = fuzzRandomU64(); + u64 iVal2 = fuzzRandomU64(); + double d = (double)iVal1 / (double)iVal2; + memcpy(&iVal1, &d, sizeof(iVal1)); + fuzzPutU64(&pChange->aSub[1], iVal1); + break; + } + + case 0x03: /* text */ + case 0x04: { /* blob */ + int nByte = fuzzRandomInt(48); + pChange->aSub[1] = (u8)nByte; + fuzzRandomBlob(nByte, &pChange->aSub[2]); + if( pChange->aSub[0]==0x03 ){ + int i; + for(i=0; iaSub[2+i] &= 0x7F; + } + } + break; + } + } + } + if( pChange->eType==FUZZ_VALUE_MOD ){ + int sz; + int iMod = -1; + fuzzChangeSize(pChange->pSub1, &sz); + memcpy(pChange->aSub, pChange->pSub1, sz); + switch( pChange->aSub[0] ){ + case 0x01: + case 0x02: + iMod = fuzzRandomInt(8) + 1; + break; + + case 0x03: /* text */ + case 0x04: { /* blob */ + int nByte; + int iFirst = 1 + fuzzGetVarint(&pChange->aSub[1], &nByte); + if( nByte>0 ){ + iMod = fuzzRandomInt(nByte) + iFirst; + } + break; + } + } + + if( iMod>=0 ){ + u8 mask = (1 << fuzzRandomInt(8 - (pChange->aSub[0]==0x03))); + pChange->aSub[iMod] ^= mask; + } + } + } + + return SQLITE_OK; +} + +/* +** Copy a single change from the input to the output changeset, making +** any modifications specified by (*pFuzz). +*/ +static int fuzzCopyChange( + FuzzChangeset *pParse, + int iGrp, + FuzzChange *pFuzz, + u8 **pp, u8 **ppOut /* IN/OUT: Input and output pointers */ +){ + int bPS = pParse->bPatchset; + FuzzChangesetGroup *pGrp = pParse->apGroup[iGrp]; + u8 *p = *pp; + u8 *pOut = *ppOut; + u8 eType = p++[0]; + int iRec; + int nRec = ((eType==SQLITE_UPDATE && !bPS) ? 2 : 1); + int iUndef = -1; + int nUpdate = 0; + + u8 eNew = eType; + if( pFuzz->iCurrent==pFuzz->iChange && pFuzz->eType==FUZZ_CHANGE_TYPE ){ + switch( eType ){ + case SQLITE_INSERT: + eNew = SQLITE_DELETE; + break; + case SQLITE_DELETE: + eNew = SQLITE_UPDATE; + break; + case SQLITE_UPDATE: + eNew = SQLITE_INSERT; + break; + } + } + + if( pFuzz->iCurrent==pFuzz->iChange + && pFuzz->eType==FUZZ_CHANGE_FIELD && eType==SQLITE_UPDATE + ){ + int sz; + int i; + int nDef = 0; + u8 *pCsr = p+1; + for(i=0; inCol; i++){ + if( pCsr[0] && pGrp->aPK[i]==0 ) nDef++; + fuzzChangeSize(pCsr, &sz); + pCsr += sz; + } + if( nDef<=1 ) return -1; + nDef = fuzzRandomInt(nDef); + pCsr = p+1; + for(i=0; inCol; i++){ + if( pCsr[0] && pGrp->aPK[i]==0 ){ + if( nDef==0 ) iUndef = i; + nDef--; + } + fuzzChangeSize(pCsr, &sz); + pCsr += sz; + } + } + + /* Copy the change type and indirect flag. If the fuzz mode is + ** FUZZ_CHANGE_INDIRECT, and the current change is the one selected for + ** fuzzing, invert the indirect flag. */ + *(pOut++) = eNew; + if( pFuzz->eType==FUZZ_CHANGE_INDIRECT && pFuzz->iCurrent==pFuzz->iChange ){ + *(pOut++) = !(*(p++)); + }else{ + *(pOut++) = *(p++); + } + + for(iRec=0; iRecnCol; i++){ + int sz; + u8 *pCopy = p; + + /* If this is a patchset, and the input is a DELETE, then the only + ** fields present are the PK fields. So, if this is not a PK, skip to + ** the next column. If the current fuzz is FUZZ_CHANGE_TYPE, then + ** write a randomly selected value to the output. */ + if( bPS && eType==SQLITE_DELETE && pGrp->aPK[i]==0 ){ + if( eType!=eNew ){ + assert( eNew==SQLITE_UPDATE ); + do { + pCopy = pParse->apVal[fuzzRandomInt(pParse->nVal)]; + }while( pCopy[0]==0x00 ); + fuzzChangeSize(pCopy, &sz); + memcpy(pOut, pCopy, sz); + pOut += sz; + } + continue; + } + + if( p==pFuzz->pSub1 ){ + pCopy = pFuzz->pSub2; + }else if( p==pFuzz->pSub2 ){ + pCopy = pFuzz->pSub1; + }else if( i==iUndef ){ + pCopy = (u8*)"\0"; + } + + if( pCopy[0]==0x00 && eNew!=eType && eType==SQLITE_UPDATE && iRec==0 ){ + while( pCopy[0]==0x00 ){ + pCopy = pParse->apVal[fuzzRandomInt(pParse->nVal)]; + } + }else if( p[0]==0x00 && pCopy[0]!=0x00 ){ + return -1; + }else{ + if( pGrp->aPK[i]>0 && pCopy[0]==0x05 ) return -1; + } + + if( (pFuzz->iGroup!=iGrp || i!=pFuzz->iDelete) + && (eNew==eType || eType!=SQLITE_UPDATE || iRec==0) + && (eNew==eType || eNew!=SQLITE_DELETE || !bPS || pGrp->aPK[i]) + ){ + fuzzChangeSize(pCopy, &sz); + memcpy(pOut, pCopy, sz); + pOut += sz; + nUpdate += (pGrp->aPK[i]==0 && pCopy[0]!=0x00); + } + + fuzzChangeSize(p, &sz); + p += sz; + } + + if( iGrp==pFuzz->iGroup ){ + if( pFuzz->eType==FUZZ_COLUMN_ADD ){ + if( !bPS || eType!=SQLITE_DELETE ) *(pOut++) = 0x05; + }else if( pFuzz->eType==FUZZ_COLUMN_ADDPK ){ + if( iRec==1 ){ + *(pOut++) = 0x00; + }else{ + u8 *pNew; + int szNew; + do { + pNew = pParse->apVal[fuzzRandomInt(pParse->nVal)]; + }while( pNew[0]==0x00 || pNew[0]==0x05 ); + fuzzChangeSize(pNew, &szNew); + memcpy(pOut, pNew, szNew); + pOut += szNew; + } + } + } + } + + if( pFuzz->iCurrent==pFuzz->iChange ){ + if( pFuzz->eType==FUZZ_CHANGE_DUP ){ + int nByte = pOut - (*ppOut); + memcpy(pOut, *ppOut, nByte); + pOut += nByte; + } + + if( pFuzz->eType==FUZZ_CHANGE_DEL ){ + pOut = *ppOut; + } + if( eNew!=eType && eNew==SQLITE_UPDATE && !bPS ){ + int i; + u8 *pCsr = (*ppOut) + 2; + for(i=0; inCol; i++){ + int sz; + u8 *pCopy = pCsr; + if( pGrp->aPK[i] ) pCopy = (u8*)"\0"; + fuzzChangeSize(pCopy, &sz); + memcpy(pOut, pCopy, sz); + pOut += sz; + fuzzChangeSize(pCsr, &sz); + pCsr += sz; + } + } + } + + /* If a column is being deleted from this group, and this change was an + ** UPDATE, and there are now no non-PK, non-undefined columns in the + ** change, remove it altogether. */ + if( pFuzz->eType==FUZZ_COLUMN_DEL && pFuzz->iGroup==iGrp + && eType==SQLITE_UPDATE && nUpdate==0 + ){ + pOut = *ppOut; + } + + *pp = p; + *ppOut = pOut; + pFuzz->iCurrent += (eType==SQLITE_UPDATE || pFuzz->eType!=FUZZ_CHANGE_FIELD); + return SQLITE_OK; +} + +/* +** Fuzz the changeset parsed into object pParse and write the results +** to file zOut on disk. Argument pBuf points to a buffer that is guaranteed +** to be large enough to hold the fuzzed changeset. +** +** Return SQLITE_OK if successful, or an SQLite error code if an error occurs. +*/ +static int fuzzDoOneFuzz( + const char *zOut, /* Filename to write modified changeset to */ + u8 *pBuf, /* Buffer to use for modified changeset */ + FuzzChangeset *pParse /* Parse of input changeset */ +){ + FuzzChange change; + int iGrp; + int rc = -1; + + while( rc<0 ){ + u8 *pOut = pBuf; + rc = fuzzSelectChange(pParse, &change); + for(iGrp=0; rc==SQLITE_OK && iGrpnGroup; iGrp++){ + FuzzChangesetGroup *pGrp = pParse->apGroup[iGrp]; + int nTab = strlen(pGrp->zTab) + 1; + int j; + int nRep = 1; + + /* If this is the group to delete for a FUZZ_GROUP_DEL change, jump to + ** the next group. Unless this is the only group in the changeset - in + ** that case this change cannot be applied. + ** + ** Or, if this is a FUZZ_GROUP_DUP, set nRep to 2 to output two + ** copies of the group. */ + if( change.iGroup==iGrp ){ + if( change.eType==FUZZ_GROUP_DEL ){ + if( pParse->nGroup==1 ) rc = -1; + continue; + } + else if( change.eType==FUZZ_GROUP_DUP ){ + nRep = 2; + } + } + + for(j=0; jaChange; + int nCol = pGrp->nCol; + int iPKDel = 0; + if( iGrp==change.iGroup ){ + if( change.eType==FUZZ_COLUMN_ADD + || change.eType==FUZZ_COLUMN_ADDPK + ){ + nCol++; + }else if( change.eType==FUZZ_COLUMN_DEL ){ + nCol--; + iPKDel = pGrp->aPK[change.iDelete]; + } + } + + /* Output a table header */ + pOut++[0] = pParse->bPatchset ? 'P' : 'T'; + pOut += fuzzPutVarint(pOut, nCol); + + for(i=0; inCol; i++){ + if( iGrp!=change.iGroup || i!=change.iDelete ){ + u8 v = pGrp->aPK[i]; + if( iPKDel && v>iPKDel ) v--; + *(pOut++) = v; + } + } + if( nCol>pGrp->nCol ){ + if( change.eType==FUZZ_COLUMN_ADD ){ + *(pOut++) = 0x00; + }else{ + u8 max = 0; + for(i=0; inCol; i++){ + if( pGrp->aPK[i]>max ) max = pGrp->aPK[i]; + } + *(pOut++) = max+1; + } + } + memcpy(pOut, pGrp->zTab, nTab); + pOut += nTab; + + /* Output the change array. */ + pSaved = pOut; + for(i=0; rc==SQLITE_OK && inChange; i++){ + rc = fuzzCopyChange(pParse, iGrp, &change, &p, &pOut); + } + if( pOut==pSaved ) rc = -1; + } + } + if( rc==SQLITE_OK ){ + fuzzWriteFile(zOut, pBuf, pOut-pBuf); + } + } + + return rc; +} + +int main(int argc, char **argv){ + int nRepeat = 0; /* Number of output files */ + int iSeed = 0; /* Value of PRNG seed */ + const char *zInput; /* Name of input file */ + void *pChangeset = 0; /* Input changeset */ + int nChangeset = 0; /* Size of input changeset in bytes */ + int i; /* Current output file */ + FuzzChangeset changeset; /* Partially parsed changeset */ + int rc; + u8 *pBuf = 0; + + if( argc!=4 && argc!=2 ) usage(argv[0]); + zInput = argv[1]; + + fuzzReadFile(zInput, &nChangeset, &pChangeset); + rc = fuzzParseChangeset(pChangeset, nChangeset, &changeset); + + if( rc==SQLITE_OK ){ + if( argc==2 ){ + for(i=0; i2; +} { + {INSERT t2 0 X. {} {i 3 t y}} + {INSERT t2 0 X. {} {i 4 t y}} +} + +do_iterator_test 6.1.7 * { + SELECT indirect(1); + DELETE FROM t2 WHERE x = 4; + SELECT indirect(0); + INSERT INTO t2 VALUES(4, 'new'); +} { + {UPDATE t2 0 X. {i 4 t y} {{} {} t new}} +} + +do_iterator_test 6.1.8 * { + CREATE TABLE t3(a, b PRIMARY KEY); + CREATE TABLE t4(a, b PRIMARY KEY); + CREATE TRIGGER t4t AFTER UPDATE ON t4 BEGIN + UPDATE t3 SET a = new.a WHERE b = new.b; + END; + + SELECT indirect(1); + INSERT INTO t3 VALUES('one', 1); + INSERT INTO t4 VALUES('one', 1); + SELECT indirect(0); + UPDATE t4 SET a = 'two' WHERE b = 1; +} { + {INSERT t3 1 .X {} {t two i 1}} + {INSERT t4 0 .X {} {t two i 1}} +} + +sqlite3session S db main +do_execsql_test 6.2.1 { + SELECT indirect(0); + SELECT indirect(-1); + SELECT indirect(45); + SELECT indirect(-100); +} {0 0 1 1} +S delete + +#------------------------------------------------------------------------- +# Test that if a conflict-handler that has been passed either NOTFOUND or +# CONSTRAINT returns REPLACE - the sqlite3changeset_apply() call returns +# MISUSE and rolls back any changes made so far. +# +# 7.1.*: NOTFOUND conflict-callback. +# 7.2.*: CONSTRAINT conflict-callback. +# +proc xConflict {args} {return REPLACE} +test_reset + +do_execsql_test 7.1.1 { + CREATE TABLE t1(a PRIMARY KEY, b); + INSERT INTO t1 VALUES(1, 'one'); + INSERT INTO t1 VALUES(2, 'two'); +} +do_test 7.1.2 { + execsql { + CREATE TABLE t1(a PRIMARY KEY, b NOT NULL); + INSERT INTO t1 VALUES(1, 'one'); + } db2 +} {} +do_test 7.1.3 { + set changeset [changeset_from_sql { + UPDATE t1 SET b = 'five' WHERE a = 1; + UPDATE t1 SET b = 'six' WHERE a = 2; + }] + set x [list] + sqlite3session_foreach c $changeset { lappend x $c } + set x +} [list \ + {UPDATE t1 0 X. {i 1 t one} {{} {} t five}} \ + {UPDATE t1 0 X. {i 2 t two} {{} {} t six}} \ +] +do_test 7.1.4 { + list [catch {sqlite3changeset_apply db2 $changeset xConflict} msg] $msg +} {1 SQLITE_MISUSE} +do_test 7.1.5 { execsql { SELECT * FROM t1 } db2 } {1 one} + +do_test 7.2.1 { + set changeset [changeset_from_sql { UPDATE t1 SET b = NULL WHERE a = 1 }] + + set x [list] + sqlite3session_foreach c $changeset { lappend x $c } + set x +} [list \ + {UPDATE t1 0 X. {i 1 t five} {{} {} n {}}} \ +] +do_test 7.2.2 { + list [catch {sqlite3changeset_apply db2 $changeset xConflict} msg] $msg +} {1 SQLITE_MISUSE} +do_test 7.2.3 { execsql { SELECT * FROM t1 } db2 } {1 one} + +#------------------------------------------------------------------------- +# Test that if a conflict-handler returns ABORT, application of the +# changeset is rolled back and the sqlite3changeset_apply() method returns +# SQLITE_ABORT. +# +# Also test that the same thing happens if a conflict handler returns an +# unrecognized integer value. Except, in this case SQLITE_MISUSE is returned +# instead of SQLITE_ABORT. +# +foreach {tn conflict_return apply_return} { + 1 ABORT SQLITE_ABORT + 2 567 SQLITE_MISUSE +} { + test_reset + proc xConflict {args} [list return $conflict_return] + + do_test 8.$tn.0 { + do_common_sql { + CREATE TABLE t1(x, y, PRIMARY KEY(x, y)); + INSERT INTO t1 VALUES('x', 'y'); + } + execsql { INSERT INTO t1 VALUES('w', 'w') } + + set changeset [changeset_from_sql { DELETE FROM t1 WHERE 1 }] + + set x [list] + sqlite3session_foreach c $changeset { lappend x $c } + set x + } [list \ + {DELETE t1 0 XX {t w t w} {}} \ + {DELETE t1 0 XX {t x t y} {}} \ + ] + + do_test 8.$tn.1 { + list [catch {sqlite3changeset_apply db2 $changeset xConflict} msg] $msg + } [list 1 $apply_return] + + do_test 8.$tn.2 { + execsql {SELECT * FROM t1} db2 + } {x y} +} + + +#------------------------------------------------------------------------- +# Try to cause an infinite loop as follows: +# +# 1. Have a changeset insert a row that causes a CONFLICT callback, +# 2. Have the conflict handler return REPLACE, +# 3. After the session module deletes the conflicting row, have a trigger +# re-insert it. +# 4. Goto step 1... +# +# This doesn't work, as the second invocation of the conflict handler is a +# CONSTRAINT, not a CONFLICT. There is at most one CONFLICT callback for +# each change in the changeset. +# +test_reset +proc xConflict {type args} { + if {$type == "CONFLICT"} { return REPLACE } + return OMIT +} +do_test 9.1 { + execsql { + CREATE TABLE t1(a PRIMARY KEY, b); + } + execsql { + CREATE TABLE t1(a PRIMARY KEY, b); + INSERT INTO t1 VALUES('x', 2); + CREATE TRIGGER tr1 AFTER DELETE ON t1 BEGIN + INSERT INTO t1 VALUES(old.a, old.b); + END; + } db2 +} {} +do_test 9.2 { + set changeset [changeset_from_sql { INSERT INTO t1 VALUES('x', 1) }] + sqlite3changeset_apply db2 $changeset xConflict +} {} +do_test 9.3 { + execsql { SELECT * FROM t1 } db2 +} {x 2} + +#------------------------------------------------------------------------- +# +test_reset +db function enable [list S enable] + +do_common_sql { + CREATE TABLE t1(a PRIMARY KEY, b); + INSERT INTO t1 VALUES('x', 'X'); +} + +do_iterator_test 10.1 t1 { + INSERT INTO t1 VALUES('y', 'Y'); + SELECT enable(0); + INSERT INTO t1 VALUES('z', 'Z'); + SELECT enable(1); +} { + {INSERT t1 0 X. {} {t y t Y}} +} + +sqlite3session S db main +do_execsql_test 10.2 { + SELECT enable(0); + SELECT enable(-1); + SELECT enable(1); + SELECT enable(-1); +} {0 0 1 1} +S delete + +#------------------------------------------------------------------------- +test_reset +do_common_sql { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c, d, e, f); + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<32 + ) + INSERT INTO t1 SELECT NULL, 0, 0, 0, 0, 0 FROM s +} + +do_then_apply_sql -ignorenoop { + UPDATE t1 SET f=f+1 WHERE a=1; + UPDATE t1 SET e=e+1 WHERE a=2; + UPDATE t1 SET e=e+1, f=f+1 WHERE a=3; + UPDATE t1 SET d=d+1 WHERE a=4; + UPDATE t1 SET d=d+1, f=f+1 WHERE a=5; + UPDATE t1 SET d=d+1, e=e+1 WHERE a=6; + UPDATE t1 SET d=d+1, e=e+1, f=f+1 WHERE a=7; + UPDATE t1 SET c=c+1 WHERE a=8; + UPDATE t1 SET c=c+1, f=f+1 WHERE a=9; + UPDATE t1 SET c=c+1, e=e+1 WHERE a=10; + UPDATE t1 SET c=c+1, e=e+1, f=f+1 WHERE a=11; + UPDATE t1 SET c=c+1, d=d+1 WHERE a=12; + UPDATE t1 SET c=c+1, d=d+1, f=f+1 WHERE a=13; + UPDATE t1 SET c=c+1, d=d+1, e=e+1 WHERE a=14; + UPDATE t1 SET c=c+1, d=d+1, e=e+1, f=f+1 WHERE a=15; + UPDATE t1 SET d=d+1 WHERE a=16; + UPDATE t1 SET d=d+1, f=f+1 WHERE a=17; + UPDATE t1 SET d=d+1, e=e+1 WHERE a=18; + UPDATE t1 SET d=d+1, e=e+1, f=f+1 WHERE a=19; + UPDATE t1 SET d=d+1, d=d+1 WHERE a=20; + UPDATE t1 SET d=d+1, d=d+1, f=f+1 WHERE a=21; + UPDATE t1 SET d=d+1, d=d+1, e=e+1 WHERE a=22; + UPDATE t1 SET d=d+1, d=d+1, e=e+1, f=f+1 WHERE a=23; + UPDATE t1 SET d=d+1, c=c+1 WHERE a=24; + UPDATE t1 SET d=d+1, c=c+1, f=f+1 WHERE a=25; + UPDATE t1 SET d=d+1, c=c+1, e=e+1 WHERE a=26; + UPDATE t1 SET d=d+1, c=c+1, e=e+1, f=f+1 WHERE a=27; + UPDATE t1 SET d=d+1, c=c+1, d=d+1 WHERE a=28; + UPDATE t1 SET d=d+1, c=c+1, d=d+1, f=f+1 WHERE a=29; + UPDATE t1 SET d=d+1, c=c+1, d=d+1, e=e+1 WHERE a=30; + UPDATE t1 SET d=d+1, c=c+1, d=d+1, e=e+1, f=f+1 WHERE a=31; +} + +do_test 11.0 { + compare_db db db2 +} {} + +finish_test diff --git a/ext/session/session3.test b/ext/session/session3.test new file mode 100644 index 0000000000..ee955f1376 --- /dev/null +++ b/ext/session/session3.test @@ -0,0 +1,214 @@ +# 2011 March 24 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for the session module. More +# specifically, it focuses on testing the session modules response to +# database schema modifications and mismatches. +# + +if {![info exists testdir]} { + set testdir [file join [file dirname [info script]] .. .. test] +} +source [file join [file dirname [info script]] session_common.tcl] +source $testdir/tester.tcl +ifcapable !session {finish_test; return} + +set testprefix session3 + +#------------------------------------------------------------------------- +# These tests - session3-1.* - verify that the session module behaves +# correctly when confronted with a schema mismatch when applying a +# changeset (in function sqlite3changeset_apply()). +# +# session3-1.1.*: Table does not exist in target db. +# session3-1.2.*: Table has wrong number of columns in target db. +# session3-1.3.*: Table has wrong PK columns in target db. +# +db close +sqlite3_shutdown +test_sqlite3_log log +sqlite3 db test.db + +proc log {code msg} { lappend ::log $code $msg } + +forcedelete test.db2 +sqlite3 db2 test.db2 + +do_execsql_test 1.0 { + CREATE TABLE t1(a PRIMARY KEY, b); +} +do_test 1.1 { + set ::log {} + do_then_apply_sql { + INSERT INTO t1 VALUES(1, 2); + INSERT INTO t1 VALUES(3, 4); + } + set ::log +} {SQLITE_SCHEMA {sqlite3changeset_apply(): no such table: t1}} + +do_test 1.2.0 { + execsql { CREATE TABLE t1(a PRIMARY KEY, b, c) } db2 +} {} +do_test 1.2.1 { + set ::log {} + do_then_apply_sql { + INSERT INTO t1 VALUES(5, 6); + INSERT INTO t1 VALUES(7, 8); + } + set ::log +} {} +do_test 1.2.2 { + db2 eval { SELECT * FROM t1 } +} {5 6 {} 7 8 {}} + +do_test 1.3.0 { + execsql { + DROP TABLE t1; + CREATE TABLE t1(a, b PRIMARY KEY); + } db2 +} {} +do_test 1.3.1 { + set ::log {} + do_then_apply_sql { + INSERT INTO t1 VALUES(9, 10); + INSERT INTO t1 VALUES(11, 12); + } + set ::log +} {SQLITE_SCHEMA {sqlite3changeset_apply(): primary key mismatch for table t1}} + +#------------------------------------------------------------------------- +# These tests - session3-2.* - verify that the session module behaves +# correctly when the schema of an attached table is modified during the +# session. +# +# session3-2.1.*: Table is dropped midway through the session. +# session3-2.2.*: Table is dropped and recreated with a different # cols. +# session3-2.3.*: Table is dropped and recreated with a different PK. +# +# In all of these scenarios, the call to sqlite3session_changeset() will +# return SQLITE_SCHEMA. Also: +# +# session3-2.4.*: Table is dropped and recreated with an identical schema. +# In this case sqlite3session_changeset() returns SQLITE_OK. +# + +do_test 2.1 { + execsql { CREATE TABLE t2(a, b PRIMARY KEY) } + sqlite3session S db main + S attach t2 + execsql { + INSERT INTO t2 VALUES(1, 2); + DROP TABLE t2; + } + list [catch { S changeset } msg] $msg +} {1 SQLITE_SCHEMA} + +do_test 2.2.1 { + S delete + sqlite3session S db main + execsql { CREATE TABLE t2(a, b PRIMARY KEY, c) } + S attach t2 + execsql { + INSERT INTO t2 VALUES(1, 2, 3); + DROP TABLE t2; + CREATE TABLE t2(a, b PRIMARY KEY); + } + list [catch { S changeset } msg] $msg +} {1 SQLITE_SCHEMA} +do_test 2.2.2 { + S delete + sqlite3session S db main + execsql { + DROP TABLE t2; + CREATE TABLE t2(a, b PRIMARY KEY, c); + } + S attach t2 + execsql { + INSERT INTO t2 VALUES(1, 2, 3); + DROP TABLE t2; + CREATE TABLE t2(a, b PRIMARY KEY, c, d); + } + catch { S changeset } +} {0} +do_test 2.2.3 { + S delete + sqlite3session S db main + execsql { + DROP TABLE t2; + CREATE TABLE t2(a, b PRIMARY KEY, c); + } + S attach t2 + execsql { + INSERT INTO t2 VALUES(1, 2, 3); + DROP TABLE t2; + CREATE TABLE t2(a, b PRIMARY KEY); + INSERT INTO t2 VALUES(4, 5); + } + list [catch { S changeset } msg] $msg +} {1 SQLITE_SCHEMA} +do_test 2.2.4 { + S delete + sqlite3session S db main + execsql { + DROP TABLE t2; + CREATE TABLE t2(a, b PRIMARY KEY, c); + } + S attach t2 + execsql { + INSERT INTO t2 VALUES(1, 2, 3); + DROP TABLE t2; + CREATE TABLE t2(a, b PRIMARY KEY, c, d); + INSERT INTO t2 VALUES(4, 5, 6, 7); + } + catch { S changeset } +} {0} + +do_test 2.3 { + S delete + sqlite3session S db main + execsql { + DROP TABLE t2; + CREATE TABLE t2(a, b PRIMARY KEY); + } + S attach t2 + execsql { + INSERT INTO t2 VALUES(1, 2); + DROP TABLE t2; + CREATE TABLE t2(a PRIMARY KEY, b); + } + list [catch { S changeset } msg] $msg +} {1 SQLITE_SCHEMA} + +do_test 2.4 { + S delete + sqlite3session S db main + execsql { + DROP TABLE t2; + CREATE TABLE t2(a, b PRIMARY KEY); + } + S attach t2 + execsql { + INSERT INTO t2 VALUES(1, 2); + DROP TABLE t2; + CREATE TABLE t2(a, b PRIMARY KEY); + } + list [catch { S changeset } msg] $msg +} {0 {}} + +S delete + + +catch { db close } +catch { db2 close } +sqlite3_shutdown +test_sqlite3_log +sqlite3_initialize + +finish_test diff --git a/ext/session/session4.test b/ext/session/session4.test new file mode 100644 index 0000000000..55cb76f153 --- /dev/null +++ b/ext/session/session4.test @@ -0,0 +1,149 @@ +# 2011 March 25 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for the session module. +# + +if {$tcl_version<8.6} { + puts "This module requires Tcl 8.6 or later" + return +} + +if {![info exists testdir]} { + set testdir [file join [file dirname [info script]] .. .. test] +} +source [file join [file dirname [info script]] session_common.tcl] +source $testdir/tester.tcl +ifcapable !session {finish_test; return} + +set testprefix session4 + +do_test 1.0 { + execsql { + CREATE TABLE x(a, b, c, d, e, PRIMARY KEY(c, e)); + INSERT INTO x VALUES(65.21, X'28B0', 16.35, NULL, 'doers'); + INSERT INTO x VALUES(NULL, 78.49, 2, X'60', -66); + INSERT INTO x VALUES('cathedral', NULL, 35, NULL, X'B220937E80A2D8'); + INSERT INTO x VALUES(NULL, 'masking', -91.37, NULL, X'596D'); + INSERT INTO x VALUES(19, 'domains', 'espouse', -94, 'throw'); + } + + set changeset [changeset_from_sql { + DELETE FROM x WHERE e = -66; + UPDATE x SET a = 'parameterizable', b = 31.8 WHERE c = 35; + INSERT INTO x VALUES(-75.61, -17, 16.85, NULL, X'D73DB02678'); + }] + set {} {} +} {} + + +# This currently causes crashes. sqlite3changeset_invert() does not handle +# corrupt changesets well. +if 0 { + do_test 1.1 { + for {set i 0} {$i < [string length $changeset]} {incr i} { + set before [string range $changeset 0 [expr $i-1]] + set after [string range $changeset [expr $i+1] end] + for {set j 10} {$j < 260} {incr j} { + set x [binary format "a*ca*" $before $j $after] + catch { sqlite3changeset_invert $x } + } + } + } {} +} + +do_test 1.2 { + set x [binary format "ca*" 0 [string range $changeset 1 end]] + list [catch { sqlite3changeset_invert $x } msg] $msg +} {1 SQLITE_CORRUPT} + +do_test 1.3 { + set x [binary format "ca*" 0 [string range $changeset 1 end]] + list [catch { sqlite3changeset_apply db $x xConflict } msg] $msg +} {1 SQLITE_CORRUPT} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 2.0 { + CREATE TABLE t1(a INTEGER PRIMARY KEY,b,c,d); + CREATE TABLE t2(e TEXT PRIMARY KEY NOT NULL,f,g); + CREATE TABLE t3(w REAL PRIMARY KEY NOT NULL,x,y); + CREATE TABLE t4(z PRIMARY KEY) WITHOUT ROWID; +} + +foreach {tn blob} { + 1 54010174340012000000 + 2 54fefe8bcb0012000300 + 3 5480809280808001017434001200fb + 4 50af9c939c9c9cb09c9c6400b09c9c6400 + 5 12000300 + 6 09847304 + 7 5401017434001208 + 8 54010174340012fc0386868600 + 9 54010174340012FC0386868600 + 10 548894FEFE + 11 54010171340012E703ABFA7433FD1200 + 12 540101743400120003FFED00010000000000000002120002400C00000000000054040100000074310017000100000000000000050100000000000000030100000000000000040000010000000000000004010000000000000003001700010000000000000007030378797A01000000000000000F000001000000000000000F030378797A005403010000743200090003037838790100000000800000000200000000000000000900030378327902400C0000000000000304666F7572 + 13 540101743400120003001200010000000000000002120002400C0000000000005404010000007431001700010000000000000005010000000000000003010000000000000004000001000000000000000401000000000000000300170001000000000000000703FC87797A01000000000000000F000001000000000000000F030378797A005403010000743200090003037838790100000000800000000200000000000000000900030378327902400C0000000000000304666F7572 + 14 540101743400120003001200010000000000000002120002400C00000000000054040100000074310017000100000000000000050100000000000000030100000000000000040000010000000000000004010000000000000003001700010000000000000007030378797A01000000000000000F000001000000000000000F03FC87797A005403010000743200090003037838790100000000800000000200000000000000000900030378327902400C0000000000000304666F7572 + 15 540101743400120003001200010000000000000002120002400C00000000000054040100000074310017000100000000000000050100000000000000030100000000000000040000010000000000000004010000000000000003001700010000000000000007030378797A01000000000000000F000001000000000000000F030378797A005403010000743200090003FC8738790100000000800000000200000000000000000900030378327902400C0000000000000304666F7572 + 16 540101743400120003001200010000000000000002120002400C00000000000054040100000074310017000100000000000000050100000000000000030100000000000000040000010000000000000004010000000000000003001700010000000000000007030378797A01000000000000000F000001000000000000000F030378797A00540301000074320009000303783879010000000080000000020000000000000000090003FC87327902400C0000000000000304666F7572 + 17 540101743400120003FFE3000412F7010000E600000000021202120002400C0000000000005B0401000000743100171C0304646F750002400C000000000000540401000000D3310017000100000000000000050100000000000378797A405403000002F10100000100000000000004090001000100000007030378797A0100000000000D0007000001000000002300000F1B0378797A405403013900743200090003038C3879010000000000000000000002120002400C0000000000005B0401000000743117170003047C5E00FF + 18 54010174340012000300120001000000E6FF100000120002401E00000000000054040100000074310017000100040000010000000000000004FFFF7FFF0000000000010000010000001000000007030378797A01000000000000000F000000000000FA0304666F7572 + 19 540101743400120003001200010000000000000002121B02400C00000000000054040000000074310017000100000000000000050100000000000000030100000000000000040000010000000000000004010000000000000003001700010000000000000007030378817A01000000000000000F000001000000000100000F030378797A005403010000743200090003FFE809000303780000000000000304666F7572 + 20 5401017D3400120003001200010000000000000002120002400CFC00000000005404010000007431001700010000000000000005010000000000000003010000000000000004000001000000000000000401000000000000000300170001000000000000000703FFFF797A01000000000000000F000001000000000000000F030378797A005403010000743200090003037838790100000000800000000200000000000000000900030378326C02400C0000000000000304666F7572 + 21 5401017434001200030012000100FFE20000000002120002400C00000000000054040100E0007431001700010000E99D000000020000000003FFE70009000303783279020004000001030000000000002117000003001700012701000100000000743100000100000000008000090003037F387901000000008000000002000000000400000009005303010A00FF7FFFFF00000000000304664F6572 + 22 540101743400120003FFFF7FFF0000000000000002120002400C00000000000054040100000074310017000100000000000000050100000000000000030100010000000000000000040000010000000000000004010000000000000003001700010000000000000007030378797A01000000000000000F000001000000000000000F030378797A005403010000743200090003037838790100000000800000000200000000000000000900030378327902400C0000000000000304666F7572 + 23 540101742700120100120003F5FF0300 + 24 5401017434E312540101743400120003FFFC00 + 25 540101743400540101743D3D3D3D3D3D3D3D3D3D3D3D3D3400120003FFED000300 + 26 5401017446EA5301743D1D3D3D01743D1D3D3DCF3D3D3D1A3D3D3D3D3400120003FFFF000000 + 27 540101743400540101743D3D3D3D3D3D3D3D3D3D251000120003FF81000000000000 + 28 540101340012000397FF3D7F3D3400120003001200540101743D3D3D3D3D3D393D3D3D12000300 + 29 500174340050010F74340012000300120003FFE5 + 30 5004007233E900177FEF0054257F0002EF001200031E12000300 + 31 5001015001015252525250010174340012EF039A9A0100E351525D52525252525252525252525252525252525250010174340012EF039A0100009A9A9A9A9A9BA3B200120003010040743400 + 32 5401017400123400120003FFFC00 + 33 540101743400120003001200010000000000004002120002400C0000000000005404010000007431001700010000000000000005010000000000000003010000000000000004000001000000000000000401000000000000000300170001000000000000000703FC87797A01000000000000000F000001000000000000000F030378797A005403010000743200090003037838790100000000800000000200000000000000000900030378327902400C0000000000000304666F7572 + 34 54040100000074310017000100000002000015050100000000000000030100000000140000040000010000000000000004010000000000000003001700010000000000000007030378797A01000000000000000F000001000000000000000F030378797A0054030100007432000900030378387901000000008E000000020000000000000000090003FFFF000002400C0000000000000304666F7572 + 35 540101743400120003001200010000000000000002120002400C00000000000050060100000074310017000100000000000000050100000000000000030100000003001700010000666F7572 + 36 540101743400120003001200010000000000000002120002400C00000000000050050100000074310017000100000000000000050100000000000000030100000003001700010000666F7572 + 37 540101743400120003001200010000000000000002120002400C00000000000050040100008074310017000100000000000000050100000000000000030100000003001700010000666F7572 + 38 540101743400120003001200010000000000000002120002400C00000000000050040100000074310017000000000000000000050100000000000000030100000003001700010000666F7572 + 39 540101743400120003001200010000000000000002120002400C00000000000050040100018074310017000100000000000000050100000000000000030100000003001700010000666F7572 + 40 540101743400120003001200010000000000000002120002400C0000000000005004FEFFFFFF74310017000100000000000000050100000000000000030100000003001700010000666F7572 + 41 540101743400120003001200010000000000000002120002400C00000000000050040100000074310017000004000000000000050100000000000000030100000003001700010000666F7572 + 42 540101743400120003001200010000000000000002120002400C0000000000005005FFFF050074310017000100000000000000050100000000000000030100000003001700010000666F7572 + 43 540101743400120003001200010000000000000002120002400C000000000000500401006E0074310017000300000000001221050100000000000000030100000003001700010000666F7572 + 44 540101743400120003001200010000000000020000120002400C00000000000050050100000074310017000100000000000000050100004000000000030100000025001700010000666F7572 + 45 540101743400120003001200010000000000ECFF02120002400C000000000000500401F9FF00743100170001000000000000000500E1000000000000030100000003000000000000666F7572 + 46 54010174340B0B0B0B0B0B0B0B0B0B0B0B0B0B0B00120003001200010000000000000002120002400C00000000000050040100000074310017010000000000000000050100FFE900000000030100000003007F00000000666F7572 + 47 54010103001200010000000000020002120002400C0000000000005004010000F374310017000100000000000000050100000000000000030100000003001700010000666F8E72 + 48 540101743400120003001200010000000000000002120002400C00000000000050030012000174310017000700000000000000050100002000000001000000000003001700010000666F7572 + 49 540101743400120004001200010000000000000002120002400C0000000000005004010000FC733100170001000000000000000501000000000000000301000000F6FF17000100007C6F7572 + 50 54010174FFDDFF8003001200010000100000000002120002400C000000000000500401000000743100170000000005010000000000000000000003010072 + 51 540101743200120003001200010000000000000002120002400C00000000000050040100001074310017000000000003010000120300170100000000000000050100000000000000030100000003001700010000666F7572 + 52 540101745401017434001200010000000000001702120002400C00000000000050040100001A74310017000100000000000100000100000000000000030100000003001700010000666F7572 + 53 540101743400120003001200010000000000000002120002400C000000000000500401000000743100170001000002400C00000000000050040110000074310017000000000000050100000000000000030100000003001700010000666F7572 + 54 540101743400120003001200010000000000000002120002400C000000000002120002400C00000000000050040100000074310017FF0050040100000074310017FF7F00000000000000050100000000000000030100000003001700010000666F7572 + 55 540101743400120003001200010000000000000002120002400C00000000000050040100000074310017000100010080000001000000020003010100000300170100000003001700010000666F7572 + 56 5487ffffff7f +} { + do_test 2.$tn { + set changeset [binary decode hex $blob] +#set fd [open x.change w+] +#fconfigure $fd -translation binary +#puts -nonewline $fd $changeset +#close $fd + list [catch { sqlite3changeset_apply db $changeset xConflict } msg] $msg + } {1 SQLITE_CORRUPT} +} + +finish_test diff --git a/ext/session/session5.test b/ext/session/session5.test new file mode 100644 index 0000000000..9b8f9ffe12 --- /dev/null +++ b/ext/session/session5.test @@ -0,0 +1,408 @@ +# 2011 April 13 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for the session module. +# Specifically, for the sqlite3changeset_concat() command. +# + +if {![info exists testdir]} { + set testdir [file join [file dirname [info script]] .. .. test] +} +source [file join [file dirname [info script]] session_common.tcl] +source $testdir/tester.tcl +ifcapable !session {finish_test; return} + +set testprefix session5 + +# Organization of tests: +# +# session5-1.*: Simple tests to check the concat() function produces +# correct results. +# +# session5-2.*: More complicated tests. +# +# session5-3.*: Schema mismatch errors. +# +# session5-4.*: Test the concat cases that indicate that the database +# was modified in between recording of the two changesets +# being concatenated (i.e. two changesets that INSERT rows +# with the same PK values). +# + +proc do_concat_test {tn args} { + + set subtest 0 + foreach sql $args { + incr subtest + sqlite3session S db main ; S attach * + execsql $sql + + set c [S changeset] + if {[info commands s_prev] != ""} { + set c_concat [sqlite3changeset_concat $c_prev $c] + set c_two [s_prev changeset] + s_prev delete + + set h_concat [changeset_to_list $c_concat] + set h_two [changeset_to_list $c_two] + + do_test $tn.$subtest [list set {} $h_concat] $h_two + } + set c_prev $c + rename S s_prev + } + + catch { s_prev delete } +} + +#------------------------------------------------------------------------- +# Test cases session5-1.* - simple tests. +# +do_execsql_test 1.0 { + CREATE TABLE t1(a PRIMARY KEY, b); +} + +do_concat_test 1.1.1 { + INSERT INTO t1 VALUES(1, 'one'); +} { + INSERT INTO t1 VALUES(2, 'two'); +} + +do_concat_test 1.1.2 { + UPDATE t1 SET b = 'five' WHERE a = 1; +} { + UPDATE t1 SET b = 'six' WHERE a = 2; +} + +do_concat_test 1.1.3 { + DELETE FROM t1 WHERE a = 1; +} { + DELETE FROM t1 WHERE a = 2; +} + + +# 1.2.1: INSERT + DELETE -> (none) +# 1.2.2: INSERT + UPDATE -> INSERT +# +# 1.2.3: DELETE + INSERT (matching data) -> (none) +# 1.2.4: DELETE + INSERT (non-matching data) -> UPDATE +# +# 1.2.5: UPDATE + UPDATE (matching data) -> (none) +# 1.2.6: UPDATE + UPDATE (non-matching data) -> UPDATE +# 1.2.7: UPDATE + DELETE -> DELETE +# +do_concat_test 1.2.1 { + INSERT INTO t1 VALUES('x', 'y'); +} { + DELETE FROM t1 WHERE a = 'x'; +} +do_concat_test 1.2.2 { + INSERT INTO t1 VALUES(5.0, 'five'); +} { + UPDATE t1 SET b = 'six' WHERE a = 5.0; +} + +do_execsql_test 1.2.3.1 "INSERT INTO t1 VALUES('I', 'one')" +do_concat_test 1.2.3.2 { + DELETE FROM t1 WHERE a = 'I'; +} { + INSERT INTO t1 VALUES('I', 'one'); +} +do_concat_test 1.2.4 { + DELETE FROM t1 WHERE a = 'I'; +} { + INSERT INTO t1 VALUES('I', 'two'); +} +do_concat_test 1.2.5 { + UPDATE t1 SET b = 'five' WHERE a = 'I'; +} { + UPDATE t1 SET b = 'two' WHERE a = 'I'; +} +do_concat_test 1.2.6 { + UPDATE t1 SET b = 'six' WHERE a = 'I'; +} { + UPDATE t1 SET b = 'seven' WHERE a = 'I'; +} +do_concat_test 1.2.7 { + UPDATE t1 SET b = 'eight' WHERE a = 'I'; +} { + DELETE FROM t1 WHERE a = 'I'; +} + + +#------------------------------------------------------------------------- +# Test cases session5-2.* - more complex tests. +# +db function indirect indirect +proc indirect {{x -1}} { + S indirect $x + s_prev indirect $x +} +do_concat_test 2.1 { + CREATE TABLE abc(a, b, c PRIMARY KEY); + INSERT INTO abc VALUES(NULL, NULL, 1); + INSERT INTO abc VALUES('abcdefghijkl', NULL, 2); +} { + DELETE FROM abc WHERE c = 1; + UPDATE abc SET c = 1 WHERE c = 2; +} { + INSERT INTO abc VALUES('abcdefghijkl', NULL, 2); + INSERT INTO abc VALUES(1.0, 2.0, 3); +} { + UPDATE abc SET a = a-1; +} { + CREATE TABLE def(d, e, f, PRIMARY KEY(e, f)); + INSERT INTO def VALUES('x', randomblob(11000), 67); + INSERT INTO def SELECT d, e, f+1 FROM def; + INSERT INTO def SELECT d, e, f+2 FROM def; + INSERT INTO def SELECT d, e, f+4 FROM def; +} { + DELETE FROM def WHERE rowid>4; +} { + INSERT INTO def SELECT d, e, f+4 FROM def; +} { + INSERT INTO abc VALUES(22, 44, -1); +} { + UPDATE abc SET c=-2 WHERE c=-1; + UPDATE abc SET c=-3 WHERE c=-2; +} { + UPDATE abc SET c=-4 WHERE c=-3; +} { + UPDATE abc SET a=a+1 WHERE c=-3; + UPDATE abc SET a=a+1 WHERE c=-3; +} { + UPDATE abc SET a=a+1 WHERE c=-3; + UPDATE abc SET a=a+1 WHERE c=-3; +} { + INSERT INTO abc VALUES('one', 'two', 'three'); +} { + SELECT indirect(1); + UPDATE abc SET a='one point five' WHERE c = 'three'; +} { + SELECT indirect(0); + UPDATE abc SET a='one point six' WHERE c = 'three'; +} { + CREATE TABLE x1(a, b, PRIMARY KEY(a)); + SELECT indirect(1); + INSERT INTO x1 VALUES(1, 2); +} { + SELECT indirect(1); + UPDATE x1 SET b = 3 WHERE a = 1; +} + +catch {db close} +forcedelete test.db +sqlite3 db test.db +do_concat_test 2.2 { + CREATE TABLE t1(a, b, PRIMARY KEY(b)); + CREATE TABLE t2(a PRIMARY KEY, b); + INSERT INTO t1 VALUES('string', 1); + INSERT INTO t1 VALUES(4, 2); + INSERT INTO t1 VALUES(X'FFAAFFAAFFAA', 3); +} { + INSERT INTO t2 VALUES('one', 'two'); + INSERT INTO t2 VALUES(1, NULL); + UPDATE t1 SET a = 5 WHERE a = 2; +} { + DELETE FROM t2 WHERE a = 1; + UPDATE t1 SET a = 4 WHERE a = 2; + INSERT INTO t2 VALUES('x', 'y'); +} + +do_test 2.3.0 { + catch {db close} + forcedelete test.db + sqlite3 db test.db + + set sql1 "" + set sql2 "" + for {set i 1} {$i < 120} {incr i} { + append sql1 "INSERT INTO x1 VALUES($i*4, $i);" + } + for {set i 1} {$i < 120} {incr i} { + append sql2 "DELETE FROM x1 WHERE a = $i*4;" + } + set {} {} +} {} +do_concat_test 2.3 { + CREATE TABLE x1(a PRIMARY KEY, b) +} $sql1 $sql2 $sql1 $sql2 + +do_concat_test 2.4 { + CREATE TABLE x2(a PRIMARY KEY, b); + CREATE TABLE x3(a PRIMARY KEY, b); + + INSERT INTO x2 VALUES('a', 'b'); + INSERT INTO x2 VALUES('x', 'y'); + INSERT INTO x3 VALUES('a', 'b'); +} { + INSERT INTO x2 VALUES('c', 'd'); + INSERT INTO x3 VALUES('e', 'f'); + INSERT INTO x3 VALUES('x', 'y'); +} + +do_concat_test 2.5 { + UPDATE x3 SET b = 'Y' WHERE a = 'x' +} { + DELETE FROM x3 WHERE a = 'x' +} { + DELETE FROM x2 WHERE a = 'a' +} { + INSERT INTO x2 VALUES('a', 'B'); +} + +for {set k 1} {$k <=10} {incr k} { + do_test 2.6.$k.1 { + drop_all_tables + set sql1 "" + set sql2 "" + for {set i 1} {$i < 120} {incr i} { + append sql1 "INSERT INTO x1 VALUES(randomblob(20+(random()%10)), $i);" + } + for {set i 1} {$i < 120} {incr i} { + append sql2 "DELETE FROM x1 WHERE rowid = $i;" + } + set {} {} + } {} + do_concat_test 2.6.$k { + CREATE TABLE x1(a PRIMARY KEY, b) + } $sql1 $sql2 $sql1 $sql2 +} + +for {set k 1} {$k <=10} {incr k} { + do_test 2.7.$k.1 { + drop_all_tables + set sql1 "" + set sql2 "" + for {set i 1} {$i < 120} {incr i} { + append sql1 { + INSERT INTO x1 VALUES( + CASE WHEN random()%2 THEN random() ELSE randomblob(20+random()%10) END, + CASE WHEN random()%2 THEN random() ELSE randomblob(20+random()%10) END + ); + } + } + for {set i 1} {$i < 120} {incr i} { + append sql2 "DELETE FROM x1 WHERE rowid = $i;" + } + set {} {} + } {} + do_concat_test 2.7.$k { + CREATE TABLE x1(a PRIMARY KEY, b) + } $sql1 $sql2 $sql1 $sql2 +} + + +#------------------------------------------------------------------------- +# Test that schema incompatibilities are detected correctly. +# +# session5-3.1: Incompatible number of columns. +# session5-3.2: Incompatible PK definition. +# + +do_test 3.1 { + db close + forcedelete test.db + sqlite3 db test.db + + execsql { CREATE TABLE t1(a PRIMARY KEY, b) } + set c1 [changeset_from_sql { INSERT INTO t1 VALUES(1, 2) }] + execsql { + DROP TABLE t1; + CREATE TABLE t1(a PRIMARY KEY, b, c); + } + set c2 [changeset_from_sql { INSERT INTO t1 VALUES(2, 3, 4) }] + + list [catch { sqlite3changeset_concat $c1 $c2 } msg] $msg +} {1 SQLITE_SCHEMA} + +do_test 3.2 { + db close + forcedelete test.db + sqlite3 db test.db + + execsql { CREATE TABLE t1(a PRIMARY KEY, b) } + set c1 [changeset_from_sql { INSERT INTO t1 VALUES(1, 2) }] + execsql { + DROP TABLE t1; + CREATE TABLE t1(a, b PRIMARY KEY); + } + set c2 [changeset_from_sql { INSERT INTO t1 VALUES(2, 3) }] + + list [catch { sqlite3changeset_concat $c1 $c2 } msg] $msg +} {1 SQLITE_SCHEMA} + +#------------------------------------------------------------------------- +# Test that concat() handles these properly: +# +# session5-4.1: INSERT + INSERT +# session5-4.2: UPDATE + INSERT +# session5-4.3: DELETE + UPDATE +# session5-4.4: DELETE + DELETE +# + +proc do_concat_test2 {tn sql1 sqlX sql2 expected} { + sqlite3session S db main ; S attach * + execsql $sql1 + set ::c1 [S changeset] + S delete + + execsql $sqlX + + sqlite3session S db main ; S attach * + execsql $sql2 + set ::c2 [S changeset] + S delete + + uplevel do_test $tn [list { + changeset_to_list [sqlite3changeset_concat $::c1 $::c2] + }] [list [normalize_list $expected]] +} + +drop_all_tables db +do_concat_test2 4.1 { + CREATE TABLE t1(a PRIMARY KEY, b); + INSERT INTO t1 VALUES('key', 'value'); +} { + DELETE FROM t1 WHERE a = 'key'; +} { + INSERT INTO t1 VALUES('key', 'xxx'); +} { + {INSERT t1 0 X. {} {t key t value}} +} +do_concat_test2 4.2 { + UPDATE t1 SET b = 'yyy'; +} { + DELETE FROM t1 WHERE a = 'key'; +} { + INSERT INTO t1 VALUES('key', 'value'); +} { + {UPDATE t1 0 X. {t key t xxx} {{} {} t yyy}} +} +do_concat_test2 4.3 { + DELETE FROM t1 WHERE a = 'key'; +} { + INSERT INTO t1 VALUES('key', 'www'); +} { + UPDATE t1 SET b = 'valueX' WHERE a = 'key'; +} { + {DELETE t1 0 X. {t key t value} {}} +} +do_concat_test2 4.4 { + DELETE FROM t1 WHERE a = 'key'; +} { + INSERT INTO t1 VALUES('key', 'ttt'); +} { + DELETE FROM t1 WHERE a = 'key'; +} { + {DELETE t1 0 X. {t key t valueX} {}} +} + +finish_test diff --git a/ext/session/session6.test b/ext/session/session6.test new file mode 100644 index 0000000000..22fa93cb35 --- /dev/null +++ b/ext/session/session6.test @@ -0,0 +1,91 @@ +# 2011 July 11 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite sessions extension. +# Specifically, it tests that sessions work when the database is modified +# using incremental blob handles. +# + +if {![info exists testdir]} { + set testdir [file join [file dirname [info script]] .. .. test] +} +source [file join [file dirname [info script]] session_common.tcl] +source $testdir/tester.tcl +ifcapable !session {finish_test; return} +ifcapable !incrblob {finish_test; return} + +set testprefix session6 + +proc do_then_apply_tcl {tcl {dbname main}} { + proc xConflict args { return "OMIT" } + set rc [catch { + sqlite3session S db $dbname + db eval "SELECT name FROM $dbname.sqlite_master WHERE type = 'table'" { + S attach $name + } + eval $tcl + sqlite3changeset_apply db2 [S changeset] xConflict + } msg] + + catch { S delete } + if {$rc} {error $msg} +} + +test_sqlite3_log x +proc x {args} {puts $args} + +forcedelete test.db2 +sqlite3 db2 test.db2 + +do_common_sql { + CREATE TABLE t1(a PRIMARY KEY, b); + CREATE TABLE t2(c PRIMARY KEY, d); +} + +# Test a blob update. +# +do_test 1.1 { + do_then_apply_tcl { + db eval { INSERT INTO t1 VALUES(1, 'helloworld') } + db eval { INSERT INTO t2 VALUES(2, 'onetwothree') } + } + compare_db db db2 +} {} +do_test 1.2 { + do_then_apply_tcl { + set fd [db incrblob t1 b 1] + puts -nonewline $fd 1234567890 + close $fd + } + compare_db db db2 +} {} + +# Test an attached database. +# +do_test 2.1 { + forcedelete test.db3 + file copy test.db2 test.db3 + execsql { ATTACH 'test.db3' AS aux; } + + do_then_apply_tcl { + set fd [db incrblob aux t2 d 1] + puts -nonewline $fd fourfivesix + close $fd + } aux + + sqlite3 db3 test.db3 + compare_db db2 db3 +} {} + + +db3 close +db2 close + +finish_test diff --git a/ext/session/session8.test b/ext/session/session8.test new file mode 100644 index 0000000000..884da0e775 --- /dev/null +++ b/ext/session/session8.test @@ -0,0 +1,91 @@ +# 2011 July 13 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. +# + +if {![info exists testdir]} { + set testdir [file join [file dirname [info script]] .. .. test] +} +source [file join [file dirname [info script]] session_common.tcl] +source $testdir/tester.tcl +ifcapable !session {finish_test; return} + +set testprefix session8 + +proc noop {args} {} + +# Like [dbcksum] in tester.tcl. Except this version is not sensitive +# to changes in the value of implicit IPK columns. +# +proc udbcksum {db dbname} { + if {$dbname=="temp"} { + set master sqlite_temp_master + } else { + set master $dbname.sqlite_master + } + set alltab [$db eval "SELECT name FROM $master WHERE type='table'"] + set txt [$db eval "SELECT * FROM $master"]\n + foreach tab $alltab { + append txt [lsort [$db eval "SELECT * FROM $dbname.$tab"]]\n + } + return [md5 $txt] +} + +proc do_then_undo {tn sql} { + set ck1 [udbcksum db main] + + sqlite3session S db main + S attach * + db eval $sql + + set ck2 [udbcksum db main] + + set invert [sqlite3changeset_invert [S changeset]] + S delete + sqlite3changeset_apply db $invert noop + + set ck3 [udbcksum db main] + + set a [expr {$ck1==$ck2}] + set b [expr {$ck1==$ck3}] + uplevel [list do_test $tn.1 "set {} $a" 0] + uplevel [list do_test $tn.2 "set {} $b" 1] +} + +do_execsql_test 1.1 { + CREATE TABLE t1(a PRIMARY KEY, b); + INSERT INTO t1 VALUES(1, 2); + INSERT INTO t1 VALUES('abc', 'xyz'); +} +do_then_undo 1.2 { INSERT INTO t1 VALUES(3, 4); } +do_then_undo 1.3 { DELETE FROM t1 WHERE b=2; } +do_then_undo 1.4 { UPDATE t1 SET b = 3 WHERE a = 1; } + +do_execsql_test 2.1 { + CREATE TABLE t2(a, b PRIMARY KEY); + INSERT INTO t2 VALUES(1, 2); + INSERT INTO t2 VALUES('abc', 'xyz'); +} +do_then_undo 1.2 { INSERT INTO t2 VALUES(3, 4); } +do_then_undo 1.3 { DELETE FROM t2 WHERE b=2; } +do_then_undo 1.4 { UPDATE t1 SET a = '123' WHERE b = 'xyz'; } + +do_execsql_test 3.1 { + CREATE TABLE t3(a, b, c, d, e, PRIMARY KEY(c, e)); + INSERT INTO t3 VALUES('x', 45, 0.0, 'abcdef', 12); + INSERT INTO t3 VALUES(45, 0.0, 'abcdef', 12, 'x'); + INSERT INTO t3 VALUES(0.0, 'abcdef', 12, 'x', 45); +} + +do_then_undo 3.2 { UPDATE t3 SET b=b||b WHERE e!='x' } +do_then_undo 3.3 { UPDATE t3 SET a = 46 } + +finish_test diff --git a/ext/session/session9.test b/ext/session/session9.test new file mode 100644 index 0000000000..6207aae427 --- /dev/null +++ b/ext/session/session9.test @@ -0,0 +1,328 @@ +# 2013 July 04 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# This file tests that the sessions module handles foreign key constraint +# violations when applying changesets as required. +# + +if {![info exists testdir]} { + set testdir [file join [file dirname [info script]] .. .. test] +} +source [file join [file dirname [info script]] session_common.tcl] +source $testdir/tester.tcl +ifcapable !session {finish_test; return} +set testprefix session9 + + +#-------------------------------------------------------------------- +# Basic tests. +# +proc populate_db {} { + drop_all_tables + execsql { + PRAGMA foreign_keys = 1; + CREATE TABLE p1(a PRIMARY KEY, b); + CREATE TABLE c1(a PRIMARY KEY, b REFERENCES p1); + CREATE TABLE c2(a PRIMARY KEY, + b REFERENCES p1 DEFERRABLE INITIALLY DEFERRED + ); + + INSERT INTO p1 VALUES(1, 'one'); + INSERT INTO p1 VALUES(2, 'two'); + INSERT INTO p1 VALUES(3, 'three'); + INSERT INTO p1 VALUES(4, 'four'); + } +} + +proc capture_changeset {sql} { + sqlite3session S db main + + foreach t [db eval {SELECT name FROM sqlite_master WHERE type='table'}] { + S attach $t + } + execsql $sql + set ret [S changeset] + S delete + + return $ret +} + +do_test 1.1 { + populate_db + set cc [capture_changeset { + INSERT INTO c1 VALUES('ii', 2); + INSERT INTO c2 VALUES('iii', 3); + }] + set {} {} +} {} + +proc xConflict {args} { + lappend ::xConflict {*}$args + return $::conflictret +} + +foreach {tn delrow trans conflictargs conflictret} { + 1 2 0 {FOREIGN_KEY 1} OMIT + 2 3 0 {FOREIGN_KEY 1} OMIT + 3 2 1 {FOREIGN_KEY 1} OMIT + 4 3 1 {FOREIGN_KEY 1} OMIT + 5 2 0 {FOREIGN_KEY 1} ABORT + 6 3 0 {FOREIGN_KEY 1} ABORT + 7 2 1 {FOREIGN_KEY 1} ABORT + 8 3 1 {FOREIGN_KEY 1} ABORT +} { + + set A(OMIT,0) {0 {}} + set A(OMIT,1) {0 {}} + set A(ABORT,0) {1 SQLITE_CONSTRAINT} + set A(ABORT,1) {1 SQLITE_CONSTRAINT} + do_test 1.2.$tn.1 { + populate_db + execsql { DELETE FROM p1 WHERE a=($delrow+0) } + if {$trans} { execsql BEGIN } + + set ::xConflict [list] + list [catch {sqlite3changeset_apply db $::cc xConflict} msg] $msg + } $A($conflictret,$trans) + + do_test 1.2.$tn.2 { set ::xConflict } $conflictargs + + set A(OMIT,0) {1 1} + set A(OMIT,1) {1 1} + set A(ABORT,0) {0 0} + set A(ABORT,1) {0 0} + + do_test 1.2.$tn.3 { + execsql { SELECT count(*) FROM c1 UNION ALL SELECT count(*) FROM c2 } + } $A($conflictret,$trans) + + do_test 1.2.$tn.4 { expr ![sqlite3_get_autocommit db] } $trans + do_test 1.2.$tn.5 { + if { $trans && $conflictret=="ABORT" } { execsql COMMIT } + } {} + catchsql ROLLBACK +} + +#-------------------------------------------------------------------- +# Test that closing a transaction clears the defer_foreign_keys flag. +# +foreach {tn open noclose close} { + 1 BEGIN {} COMMIT + 2 BEGIN {} ROLLBACK + + 3 {SAVEPOINT one} {} {RELEASE one} + 4 {SAVEPOINT one} {ROLLBACK TO one} {RELEASE one} +} { + execsql $open + do_execsql_test 2.$tn.1 { PRAGMA defer_foreign_keys } {0} + + do_execsql_test 2.$tn.2 { + PRAGMA defer_foreign_keys = 1; + PRAGMA defer_foreign_keys; + } {1} + + execsql $noclose + do_execsql_test 2.$tn.3 { PRAGMA defer_foreign_keys } {1} + + execsql $close + do_execsql_test 2.$tn.4 { PRAGMA defer_foreign_keys } {0} +} + +#-------------------------------------------------------------------- +# Test that a cyclic relationship can be inserted and deleted. +# +# This situation does not come up in practice, but testing it serves to +# show that it does not matter which order parent and child keys +# are processed in internally when applying a changeset. +# +drop_all_tables + +do_execsql_test 3.1 { + CREATE TABLE t1(a PRIMARY KEY, b); + CREATE TABLE t2(x PRIMARY KEY, y); +} + +# Create changesets as follows: +# +# $cc1 - Insert a row into t1. +# $cc2 - Insert a row into t2. +# $cc - Combination of $cc1 and $cc2. +# +# $ccdel1 - Delete the row from t1. +# $ccdel2 - Delete the row from t2. +# $ccdel - Combination of $cc1 and $cc2. +# +do_test 3.2 { + set cc1 [capture_changeset { + INSERT INTO t1 VALUES('one', 'value one'); + }] + set ccdel1 [capture_changeset { DELETE FROM t1; }] + set cc2 [capture_changeset { + INSERT INTO t2 VALUES('value one', 'one'); + }] + set ccdel2 [capture_changeset { DELETE FROM t2; }] + set cc [capture_changeset { + INSERT INTO t1 VALUES('one', 'value one'); + INSERT INTO t2 VALUES('value one', 'one'); + }] + set ccdel [capture_changeset { + DELETE FROM t1; + DELETE FROM t2; + }] + set {} {} +} {} + +# Now modify the database schema to create a cyclic foreign key dependency +# between tables t1 and t2. This means that although changesets $cc and +# $ccdel can be applied, none of the others may without violating the +# foreign key constraints. +# +do_test 3.3 { + + drop_all_tables + execsql { + CREATE TABLE t1(a PRIMARY KEY, b REFERENCES t2); + CREATE TABLE t2(x PRIMARY KEY, y REFERENCES t1); + } + + + proc conflict_handler {args} { return "ABORT" } + sqlite3changeset_apply db $cc conflict_handler + + execsql { + SELECT * FROM t1; + SELECT * FROM t2; + } +} {one {value one} {value one} one} + +do_test 3.3.1 { + list [catch {sqlite3changeset_apply db $::ccdel1 conflict_handler} msg] $msg +} {1 SQLITE_CONSTRAINT} + +do_test 3.3.2 { + list [catch {sqlite3changeset_apply db $::ccdel2 conflict_handler} msg] $msg +} {1 SQLITE_CONSTRAINT} + +do_test 3.3.4.1 { + list [catch {sqlite3changeset_apply db $::ccdel conflict_handler} msg] $msg +} {0 {}} +do_execsql_test 3.3.4.2 { + SELECT * FROM t1; + SELECT * FROM t2; +} {} + +do_test 3.5.1 { + list [catch {sqlite3changeset_apply db $::cc1 conflict_handler} msg] $msg +} {1 SQLITE_CONSTRAINT} +do_test 3.5.2 { + list [catch {sqlite3changeset_apply db $::cc2 conflict_handler} msg] $msg +} {1 SQLITE_CONSTRAINT} + +#-------------------------------------------------------------------- +# Test that if a change that affects FK processing is not applied +# due to a separate constraint, SQLite does not get confused and +# increment FK counters anyway. +# +drop_all_tables +do_execsql_test 4.1 { + CREATE TABLE p1(x PRIMARY KEY, y); + CREATE TABLE c1(a PRIMARY KEY, b REFERENCES p1); + INSERT INTO p1 VALUES(1,1); +} + +do_execsql_test 4.2.1 { + BEGIN; + PRAGMA defer_foreign_keys = 1; + INSERT INTO c1 VALUES('x', 'x'); +} +do_catchsql_test 4.2.2 { COMMIT } {1 {FOREIGN KEY constraint failed}} +do_catchsql_test 4.2.3 { ROLLBACK } {0 {}} + +do_execsql_test 4.3.1 { + BEGIN; + PRAGMA defer_foreign_keys = 1; + INSERT INTO c1 VALUES(1, 1); +} +do_catchsql_test 4.3.2 { + INSERT INTO c1 VALUES(1, 'x') +} {1 {UNIQUE constraint failed: c1.a}} + +do_catchsql_test 4.3.3 { COMMIT } {0 {}} +do_catchsql_test 4.3.4 { BEGIN ; COMMIT } {0 {}} + +#-------------------------------------------------------------------- +# Test that if a DELETE change cannot be applied due to an +# SQLITE_CONSTRAINT error thrown by a trigger program, things do not +# go awry. + +drop_all_tables +reset_db +do_execsql_test 5.1 { + CREATE TABLE x1(x PRIMARY KEY, y); + CREATE TABLE x2(x PRIMARY KEY, y); + INSERT INTO x2 VALUES(1, 1); + INSERT INTO x1 VALUES(1, 1); +} + +set ::cc [changeset_from_sql { DELETE FROM x1; }] + +do_execsql_test 5.2 { + INSERT INTO x1 VALUES(1, 1); + CREATE TRIGGER tr1 AFTER DELETE ON x1 BEGIN + INSERT INTO x2 VALUES(old.x, old.y); + END; +} {} + +proc conflict_handler {args} { return "ABORT" } +do_test 5.3 { + list [catch {sqlite3changeset_apply db $::cc conflict_handler} msg] $msg +} {1 SQLITE_ABORT} + +do_execsql_test 5.4 { + SELECT * FROM X1; +} {1 1} + +#-------------------------------------------------------------------- +reset_db +do_execsql_test 6.0 { + CREATE TABLE p1(a INTEGER PRIMARY KEY, b); + CREATE TABLE c1(x INTEGER PRIMARY KEY, y REFERENCES p1); +} + +set ::cc [changeset_from_sql { + INSERT INTO c1 VALUES(10, 20); +}] + +forcedelete test.db2 +sqlite3 db2 test.db2 +do_execsql_test -db db2 6.1 { + CREATE TABLE c1(x INTEGER PRIMARY KEY, y REFERENCES p1); + PRAGMA foreign_keys = ON; + PRAGMA foreign_keys; +} {1} + +proc conflict_handler {args} { + puts $args + return "OMIT" +} + +do_test 6.2 { + list [catch {sqlite3changeset_apply_v2 db2 $::cc conflict_handler} msg] $msg +} {1 SQLITE_ERROR} + +do_test 6.3.1 { sqlite3_errmsg db2 } {no such table: main.p1} +do_test 6.3.2 { sqlite3_errcode db2 } {SQLITE_ERROR} + +do_test 6.4 { + catchsql { INSERT INTO c1 VALUES(100, 200) } db2 +} {1 {no such table: main.p1}} + +finish_test diff --git a/ext/session/sessionA.test b/ext/session/sessionA.test new file mode 100644 index 0000000000..0e0a14e38a --- /dev/null +++ b/ext/session/sessionA.test @@ -0,0 +1,106 @@ +# 2013 July 04 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# This file tests that filter callbacks work as required. +# + +if {![info exists testdir]} { + set testdir [file join [file dirname [info script]] .. .. test] +} +source [file join [file dirname [info script]] session_common.tcl] +source $testdir/tester.tcl +ifcapable !session {finish_test; return} +set testprefix sessionA + + +forcedelete test.db2 +sqlite3 db2 test.db2 +foreach {tn db} {1 db 2 db2} { + do_test 1.$tn.1 { + execsql { + CREATE TABLE t1(a PRIMARY KEY, b); + CREATE TABLE t2(a PRIMARY KEY, b); + CREATE TABLE t3(a PRIMARY KEY, b); + } $db + } {} +} + +proc tbl_filter {zTbl} { + return $::table_filter($zTbl) +} + +do_test 2.1 { + set ::table_filter(t1) 1 + set ::table_filter(t2) 0 + set ::table_filter(t3) 1 + + sqlite3session S db main + S table_filter tbl_filter + + execsql { + INSERT INTO t1 VALUES('a', 'b'); + INSERT INTO t2 VALUES('c', 'd'); + INSERT INTO t3 VALUES('e', 'f'); + } + + set changeset [S changeset] + S delete + sqlite3changeset_apply db2 $changeset xConflict + + execsql { + SELECT * FROM t1; + SELECT * FROM t2; + SELECT * FROM t3; + } db2 +} {a b e f} + +#------------------------------------------------------------------------- +# Test that filter callbacks passed to sqlite3changeset_apply() are +# invoked correctly. +# +reset_db +do_execsql_test 3.1 { + CREATE TABLE t1(a PRIMARY KEY, b); + CREATE TABLE t2(x PRIMARY KEY, y); +} + +do_test 3.2 { + execsql BEGIN + set ::cs [changeset_from_sql { + INSERT INTO t1 VALUES(1, 2); + INSERT INTO t2 VALUES('x', 'y'); + }] + execsql ROLLBACK + set {} {} +} {} + +proc filter {x y} { + return [string equal $x $y] +} + +do_test 3.3 { + sqlite3changeset_apply db $::cs {} [list filter t1] + execsql { + SELECT * FROM t1; + SELECT * FROM t2; + } +} {1 2} + +do_test 3.4 { + execsql { DELETE FROM t1 } + sqlite3changeset_apply db $::cs {} [list filter t2] + execsql { + SELECT * FROM t1; + SELECT * FROM t2; + } +} {x y} + +finish_test diff --git a/ext/session/sessionB.test b/ext/session/sessionB.test new file mode 100644 index 0000000000..2c103d5e36 --- /dev/null +++ b/ext/session/sessionB.test @@ -0,0 +1,507 @@ +# 2014 August 16 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# This file implements regression tests for sessions SQLite extension. +# Specifically, this file contains tests for "patchset" changes. +# + +if {![info exists testdir]} { + set testdir [file join [file dirname [info script]] .. .. test] +} +source [file join [file dirname [info script]] session_common.tcl] +source $testdir/tester.tcl +ifcapable !session {finish_test; return} + +set testprefix sessionB + +# +# 1.*: Test that the blobs returned by the session_patchset() API are +# as expected. Also the sqlite3_changeset_iter functions. +# +# 2.*: Test that patchset blobs are handled by sqlite3changeset_apply(). +# +# 3.*: Test that sqlite3changeset_invert() works with patchset blobs. +# Correct behaviour is to return SQLITE_CORRUPT. + +proc do_sql2patchset_test {tn sql res} { + sqlite3session S db main + S attach * + execsql $sql + uplevel [list do_patchset_test $tn S $res] + S delete +} + +#------------------------------------------------------------------------- +# Run simple tests of the _patchset() API. +# +do_execsql_test 1.0 { + CREATE TABLE t1(a, b, c, d, PRIMARY KEY(d, a)); + INSERT INTO t1 VALUES(1, 2, 3, 4); + INSERT INTO t1 VALUES(5, 6, 7, 8); + INSERT INTO t1 VALUES(9, 10, 11, 12); +} + +do_test 1.1 { + sqlite3session S db main + S attach t1 + execsql { + INSERT INTO t1 VALUES('w', 'x', 'y', 'z'); + DELETE FROM t1 WHERE d=4; + UPDATE t1 SET c = 14 WHERE a=5; + } +} {} + +do_patchset_test 1.2 S { + {UPDATE t1 0 X..X {i 5 {} {} {} {} i 8} {{} {} {} {} i 14 {} {}}} + {INSERT t1 0 X..X {} {t w t x t y t z}} + {DELETE t1 0 X..X {i 1 {} {} {} {} i 4} {}} +} + +do_test 1.3 { + S delete +} {} + +do_sql2patchset_test 1.4 { + DELETE FROM t1; +} { + {DELETE t1 0 X..X {i 5 {} {} {} {} i 8} {}} + {DELETE t1 0 X..X {t w {} {} {} {} t z} {}} + {DELETE t1 0 X..X {i 9 {} {} {} {} i 12} {}} +} + +do_sql2patchset_test 1.5 { + INSERT INTO t1 VALUES(X'61626364', NULL, NULL, 4.2); + INSERT INTO t1 VALUES(4.2, NULL, NULL, X'61626364'); +} { + {INSERT t1 0 X..X {} {f 4.2 n {} n {} b abcd}} + {INSERT t1 0 X..X {} {b abcd n {} n {} f 4.2}} +} + +do_sql2patchset_test 1.6 { + UPDATE t1 SET b=45 WHERE typeof(a)=='blob'; + UPDATE t1 SET c='zzzz' WHERE typeof(a)!='blob'; +} { + {UPDATE t1 0 X..X {f 4.2 {} {} {} {} b abcd} {{} {} {} {} t zzzz {} {}}} + {UPDATE t1 0 X..X {b abcd {} {} {} {} f 4.2} {{} {} i 45 {} {} {} {}}} +} + +do_sql2patchset_test 1.7 { + UPDATE t1 SET b='xyz' WHERE typeof(a)=='blob'; + UPDATE t1 SET c='xyz' WHERE typeof(a)!='blob'; + UPDATE t1 SET b=45 WHERE typeof(a)=='blob'; + UPDATE t1 SET c='zzzz' WHERE typeof(a)!='blob'; +} { +} + +do_sql2patchset_test 1.8 { + DELETE FROM t1; +} { + {DELETE t1 0 X..X {f 4.2 {} {} {} {} b abcd} {}} + {DELETE t1 0 X..X {b abcd {} {} {} {} f 4.2} {}} +} + +#------------------------------------------------------------------------- +# Run simple tests of _apply() with patchset objects. +# +reset_db + +proc noop {args} { error $args } +proc exec_rollback_replay {sql} { + sqlite3session S db main + S attach * + execsql BEGIN + execsql $sql + set patchset [S patchset] + S delete + execsql ROLLBACK + sqlite3changeset_apply db $patchset noop +} + +do_execsql_test 2.0 { + CREATE TABLE t2(a, b, c, d, PRIMARY KEY(b,c)); + CREATE TABLE t3(w, x, y, z, PRIMARY KEY(w)); +} + +do_test 2.1 { + exec_rollback_replay { + INSERT INTO t2 VALUES(1, 2, 3, 4); + INSERT INTO t2 VALUES('w', 'x', 'y', 'z'); + } + execsql { SELECT * FROM t2 } +} {1 2 3 4 w x y z} + +do_test 2.2 { + exec_rollback_replay { + DELETE FROM t2 WHERE a=1; + UPDATE t2 SET d = 'a'; + } + execsql { SELECT * FROM t2 } +} {w x y a} + +#------------------------------------------------------------------------- +# sqlite3changeset_invert() +# +reset_db + +do_execsql_test 3.1 { CREATE TABLE t1(x PRIMARY KEY, y) } +do_test 3.2 { + sqlite3session S db main + S attach * + execsql { INSERT INTO t1 VALUES(1, 2) } + set patchset [S patchset] + S delete + list [catch { sqlite3changeset_invert $patchset } msg] [set msg] +} {1 SQLITE_CORRUPT} + + +#------------------------------------------------------------------------- +# sqlite3changeset_concat() +# +reset_db + +proc do_patchconcat_test {tn args} { + set bRevert 0 + if {[lindex $args 0] == "-revert"} { + set bRevert 1 + set args [lrange $args 1 end] + } + set nSql [expr [llength $args]-1] + set res [lindex $args $nSql] + set patchlist [list] + + execsql BEGIN + if {$bRevert} { execsql { SAVEPOINT x } } + foreach sql [lrange $args 0 end-1] { + sqlite3session S db main + S attach * + execsql $sql + lappend patchlist [S patchset] + S delete + if {$bRevert} { execsql { ROLLBACK TO x } } + } + execsql ROLLBACK + + set patch [lindex $patchlist 0] + foreach p [lrange $patchlist 1 end] { + set patch [sqlite3changeset_concat $patch $p] + } + + set x [list] + sqlite3session_foreach c $patch { lappend x $c } + + uplevel [list do_test $tn [list set {} $x] [list {*}$res]] +} + +do_execsql_test 4.1.1 { + CREATE TABLE t1(x PRIMARY KEY, y, z); +} +do_patchconcat_test 4.1.2 { + INSERT INTO t1 VALUES(1, 2, 3); +} { + INSERT INTO t1 VALUES(4, 5, 6); +} { + {INSERT t1 0 X.. {} {i 1 i 2 i 3}} + {INSERT t1 0 X.. {} {i 4 i 5 i 6}} +} + +do_execsql_test 4.2.1 { + INSERT INTO t1 VALUES(1, 2, 3); + INSERT INTO t1 VALUES(4, 5, 6); +} + +do_patchconcat_test 4.2.2 { + UPDATE t1 SET z = 'abc' WHERE x=1 +} { + UPDATE t1 SET z = 'def' WHERE x=4 +} { + {UPDATE t1 0 X.. {i 1 {} {} {} {}} {{} {} {} {} t abc}} + {UPDATE t1 0 X.. {i 4 {} {} {} {}} {{} {} {} {} t def}} +} + +do_patchconcat_test 4.2.3 { + DELETE FROM t1 WHERE x=1; +} { + DELETE FROM t1 WHERE x=4; +} { + {DELETE t1 0 X.. {i 1 {} {} {} {}} {}} + {DELETE t1 0 X.. {i 4 {} {} {} {}} {}} +} + + +do_execsql_test 4.3.1 { + CREATE TABLE t2(a, b, c, d, PRIMARY KEY(c, b)); + INSERT INTO t2 VALUES('.', 1, 1, '.'); + INSERT INTO t2 VALUES('.', 1, 2, '.'); + INSERT INTO t2 VALUES('.', 2, 1, '.'); + INSERT INTO t2 VALUES('.', 2, 2, '.'); +} + +# INSERT + INSERT +do_patchconcat_test 4.3.2 -revert { + INSERT INTO t2 VALUES('a', 'a', 'a', 'a'); +} { + INSERT INTO t2 VALUES('b', 'a', 'a', 'b'); +} { + {INSERT t2 0 .XX. {} {t a t a t a t a}} +} + +# INSERT + DELETE +do_patchconcat_test 4.3.3 { + INSERT INTO t2 VALUES('a', 'a', 'a', 'a'); +} { + DELETE FROM t2 WHERE c = 'a'; +} {} + +# INSERT + UPDATE +do_patchconcat_test 4.3.4 { + INSERT INTO t2 VALUES('a', 'a', 'a', 'a'); +} { + UPDATE t2 SET d = 'b' WHERE c='a'; +} { + {INSERT t2 0 .XX. {} {t a t a t a t b}} +} + +# UPDATE + UPDATE +do_patchconcat_test 4.3.5 { + UPDATE t2 SET a = 'a' WHERE c=1 AND b=2; +} { + UPDATE t2 SET d = 'd' WHERE c=1 AND b=2; +} { + {UPDATE t2 0 .XX. {{} {} i 2 i 1 {} {}} {t a {} {} {} {} t d}} +} + +# UPDATE + DELETE +do_patchconcat_test 4.3.6 { + UPDATE t2 SET a = 'a' WHERE c=1 AND b=2; +} { + DELETE FROM t2 WHERE c=1 AND b=2; +} { + {DELETE t2 0 .XX. {{} {} i 2 i 1 {} {}} {}} +} + +# DELETE + INSERT +do_patchconcat_test 4.3.7 { + DELETE FROM t2 WHERE b=1; +} { + INSERT INTO t2 VALUES('x', 1, 2, '.'); +} { + {DELETE t2 0 .XX. {{} {} i 1 i 1 {} {}} {}} + {UPDATE t2 0 .XX. {{} {} i 1 i 2 {} {}} {t x {} {} {} {} t .}} +} + +# DELETE + UPDATE +do_patchconcat_test 4.3.8 -revert { + DELETE FROM t2 WHERE b=1 AND c=2; +} { + UPDATE t2 SET a=5 WHERE b=1 AND c=2; +} { + {DELETE t2 0 .XX. {{} {} i 1 i 2 {} {}} {}} +} + +# DELETE + UPDATE +do_patchconcat_test 4.3.9 -revert { + DELETE FROM t2 WHERE b=1 AND c=2; +} { + DELETE FROM t2 WHERE b=1; +} { + {DELETE t2 0 .XX. {{} {} i 1 i 1 {} {}} {}} + {DELETE t2 0 .XX. {{} {} i 1 i 2 {} {}} {}} +} + +#------------------------------------------------------------------------- +# More rigorous testing of the _patchset(), _apply and _concat() APIs. +# +# The inputs to each test are a populate database and a list of DML +# statements. This test determines that the final database is the same +# if: +# +# 1) the statements are executed directly on the database. +# +# 2) a single patchset is collected while executing the statements and +# then applied to a copy of the original database file. +# +# 3) individual patchsets are collected for statement while executing +# them and concatenated together before being applied to a copy of +# the original database. The concatenation is done in a couple of +# different ways - linear, pairwise etc. +# +# All tests, as it happens, are run with both changesets and patchsets. +# But the focus is on patchset capabilities. +# + +# Return a checksum of the contents of the database file. Implicit IPK +# columns are not included in the checksum - just modifying rowids does +# not change the database checksum. +# +proc databasecksum {db} { + set alltab [$db eval {SELECT name FROM sqlite_master WHERE type='table'}] + foreach tab $alltab { + $db eval "SELECT * FROM $tab LIMIT 1" res { } + set slist [list] + foreach col [lsort $res(*)] { + lappend slist "quote($col)" + } + set sql "SELECT [join $slist ,] FROM $tab" + append txt "[lsort [$db eval $sql]]\n" + } + return [md5 $txt] +} + +proc do_patchset_test {tn tstcmd lSql} { + if {$tstcmd != "patchset" && $tstcmd != "changeset"} { + error "have $tstcmd: must be patchset or changeset" + } + + foreach fname {test.db2 test.db3 test.db4 test.db5} { + forcedelete $fname + forcecopy test.db $fname + } + + # Execute the SQL statements on [db]. Collect a patchset for each + # individual statement, as well as a single patchset for the entire + # operation. + sqlite3session S db main + S attach * + foreach sql $lSql { + sqlite3session T db main + T attach * + db eval $sql + lappend lPatch [T $tstcmd] + T delete + } + set patchset [S $tstcmd] + S delete + + # Calculate a checksum for the final database. + set cksum [databasecksum db] + + # 1. Apply the single large patchset to test.db2 + sqlite3 db2 test.db2 + sqlite3changeset_apply db2 $patchset noop + uplevel [list do_test $tn.1 { databasecksum db2 } $cksum ] + db2 close + + # 2. Apply each of the single-statement patchsets to test.db3 + sqlite3 db2 test.db3 + foreach p $lPatch { + sqlite3changeset_apply db2 $p noop + } + uplevel [list do_test $tn.2 { databasecksum db2 } $cksum ] + db2 close + + # 3. Concatenate all single-statement patchsets into a single large + # patchset, then apply it to test.db4. + # + sqlite3 db2 test.db4 + set big "" + foreach p $lPatch { + set big [sqlite3changeset_concat $big $p] + } + sqlite3changeset_apply db2 $big noop + uplevel [list do_test $tn.3 { databasecksum db2 } $cksum ] + db2 close + + # 4. Concatenate all single-statement patchsets pairwise into a single + # large patchset, then apply it to test.db5. Pairwise concatenation: + # + # a b c d e f g h i j k + # -> {a b} {c d} {e f} {g h} {i j} k + # -> {a b c d} {e f g h} {i j k} + # -> {a b c d e f g h} {i j k} + # -> {a b c d e f g h i j k} + # -> APPLY! + # + sqlite3 db2 test.db5 + set L $lPatch + while {[llength $L] > 1} { + set O [list] + for {set i 0} {$i < [llength $L]} {incr i 2} { + if {$i==[llength $L]-1} { + lappend O [lindex $L $i] + } else { + set i1 [expr $i+1] + lappend O [sqlite3changeset_concat [lindex $L $i] [lindex $L $i1]] + } + } + set L $O + } + sqlite3changeset_apply db2 [lindex $L 0] noop + uplevel [list do_test $tn.4 { databasecksum db2 } $cksum ] + db2 close +} + +proc do_patchset_changeset_test {tn initsql args} { + foreach tstcmd {patchset changeset} { + reset_db + execsql $initsql + set x 0 + foreach sql $args { + incr x + set lSql [split $sql ";"] + uplevel [list do_patchset_test $tn.$tstcmd.$x $tstcmd $lSql] + } + } +} + +do_patchset_changeset_test 5.1 { + CREATE TABLE t1(a PRIMARY KEY, b, c); + INSERT INTO t1 VALUES(1, 2, 3); +} { + INSERT INTO t1 VALUES(4, 5, 6); + DELETE FROM t1 WHERE a=1; +} { + INSERT INTO t1 VALUES(7, 8, 9); + UPDATE t1 SET c = 5; + INSERT INTO t1 VALUES(10, 11, 12); + UPDATE t1 SET c = 6; + INSERT INTO t1 VALUES(13, 14, 15); +} { + UPDATE t1 SET c=c+1; + DELETE FROM t1 WHERE (a%2); +} + +do_patchset_changeset_test 5.2 { + CREATE TABLE t1(a PRIMARY KEY, b, c); + CREATE TABLE t2(a, b, c, d, PRIMARY KEY(c, b)); +} { + INSERT INTO t1 VALUES(x'00', 0, 'zero'); + INSERT INTO t1 VALUES(x'01', 1, 'one'); + INSERT INTO t1 VALUES(x'02', 4, 'four'); + INSERT INTO t1 VALUES(x'03', 9, 'nine'); + INSERT INTO t1 VALUES(x'04', 16, 'sixteen'); + INSERT INTO t1 VALUES(x'05', 25, 'twenty-five'); +} { + UPDATE t1 SET a = b WHERE b<=4; + INSERT INTO t2 SELECT NULL, * FROM t1; + DELETE FROM t1 WHERE b=25; +} { + DELETE FROM t2; + INSERT INTO t2 SELECT NULL, * FROM t1; + DELETE FROM t1; + INSERT INTO t1 SELECT b, c, d FROM t2; + UPDATE t1 SET b = b+1; + UPDATE t1 SET b = b+1; + UPDATE t1 SET b = b+1; +} + +set initsql { CREATE TABLE t1(a, b, c, PRIMARY KEY(c, b)); } +for {set i 0} {$i < 1000} {incr i} { + append insert "INSERT INTO t1 VALUES($i, $i, $i);" + append delete "DELETE FROM t1 WHERE b=$i;" +} +do_patchset_changeset_test 5.3 \ + $initsql $insert $delete \ + $insert $delete \ + "$insert $delete" \ + $delete + + +finish_test diff --git a/ext/session/sessionC.test b/ext/session/sessionC.test new file mode 100644 index 0000000000..74370cb79a --- /dev/null +++ b/ext/session/sessionC.test @@ -0,0 +1,197 @@ +# 2014 August 16 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# + +if {![info exists testdir]} { + set testdir [file join [file dirname [info script]] .. .. test] +} +source [file join [file dirname [info script]] session_common.tcl] +source $testdir/tester.tcl +ifcapable !session {finish_test; return} + +set testprefix sessionC + +#------------------------------------------------------------------------- +# Test the outcome of a DELETE operation made as part of applying a +# changeset failing with SQLITE_CONSTRAINT. This may happen if an +# ON DELETE RESTRICT foreign key action is triggered, or if a trigger +# program raises a constraint somehow. +# +# UPDATE: The above is no longer true, as "PRAGMA defer_foreign_keys" +# now disables "RESTRICT" processing. The test below has been rewritten +# to use a trigger instead of a foreign key to test this case. +# +do_execsql_test 1.0 { + PRAGMA foreign_keys = 1; + + CREATE TABLE p(a PRIMARY KEY, b, c); + CREATE TABLE c(d PRIMARY KEY, e /* REFERENCES p ON DELETE RESTRICT */); + + CREATE TRIGGER restrict_trig BEFORE DELETE ON p BEGIN + SELECT raise(ABORT, 'error!') FROM c WHERE e=old.a; + END; + + INSERT INTO p VALUES('one', 1, 1); + INSERT INTO p VALUES('two', 2, 2); + INSERT INTO p VALUES('three', 3, 3); + + INSERT INTO c VALUES(1, 'one'); + INSERT INTO c VALUES(3, 'three'); +} + +do_test 1.1 { + execsql BEGIN + set C [changeset_from_sql { + INSERT INTO c VALUES(4, 'one'); + DELETE FROM p WHERE a='two'; + }] + execsql ROLLBACK + execsql { + INSERT INTO c VALUES(2, 'two'); + } +} {} + +do_test 1.2.1 { + proc xConflict {args} { return "ABORT" } + catch { sqlite3changeset_apply db $C xConflict } msg + set msg +} {SQLITE_ABORT} +do_execsql_test 1.2.2 { SELECT * FROM c } {1 one 3 three 2 two} + +do_test 1.3.1 { + proc xConflict {args} { return "OMIT" } + catch { sqlite3changeset_apply db $C xConflict } msg + set msg +} {} +do_execsql_test 1.3.2 { SELECT * FROM c } {1 one 3 three 2 two 4 one} +do_execsql_test 1.3.3 { + SELECT * FROM p; +} {one 1 1 two 2 2 three 3 3} + + +#------------------------------------------------------------------------- +# Test that concatenating a changeset with a patchset does not work. +# Any attempt to do so returns SQLITE_ERROR. +# +reset_db +do_execsql_test 2.0 { + CREATE TABLE x1(t, v PRIMARY KEY); + INSERT INTO x1 VALUES(12, 55); + INSERT INTO x1 VALUES(55, 14); +} + +do_test 2.1 { + execsql BEGIN + + sqlite3session S1 db main + S1 attach * + execsql { + UPDATE x1 SET t=13 WHERE v=55; + INSERT INTO x1 VALUES(99, 123); + } + set patchset [S1 patchset] + S1 delete + + sqlite3session S1 db main + S1 attach * + execsql { + UPDATE x1 SET t=56 WHERE v=14; + INSERT INTO x1 VALUES(22, 998); + } + set changeset [S1 changeset] + S1 delete + + execsql ROLLBACK +} {} + +do_test 2.2 { + set rc [catch { sqlite3changeset_concat $patchset $changeset } msg] + list $rc $msg +} {1 SQLITE_ERROR} + +do_test 2.3 { + set rc [catch { sqlite3changeset_concat $changeset $patchset } msg] + list $rc $msg +} {1 SQLITE_ERROR} + +do_test 2.4 { + set rc [catch { sqlite3changeset_concat {} $patchset } msg] + list $rc $msg +} [list 0 $patchset] + +do_test 2.5 { + set rc [catch { sqlite3changeset_concat $patchset {} } msg] + list $rc $msg +} [list 0 $patchset] + +do_test 2.6 { + set rc [catch { sqlite3changeset_concat {} $changeset } msg] + list $rc $msg +} [list 0 $changeset] + +do_test 2.7 { + set rc [catch { sqlite3changeset_concat $changeset {} } msg] + list $rc $msg +} [list 0 $changeset] + +do_test 2.8 { + set rc [catch { sqlite3changeset_concat {} {} } msg] + list $rc $msg +} [list 0 {}] + + +#------------------------------------------------------------------------- +# Test that the xFilter argument to sqlite3changeset_apply() works. +# +reset_db +do_execsql_test 3.0 { + CREATE TABLE t1(a PRIMARY KEY, b); + CREATE TABLE t2(a PRIMARY KEY, b); + CREATE TABLE t3(a PRIMARY KEY, b); +} +do_test 3.1 { + execsql BEGIN + set changeset [changeset_from_sql { + INSERT INTO t1 VALUES(1, 1); + INSERT INTO t2 VALUES(2, 2); + INSERT INTO t3 VALUES(3, 3); + }] + execsql ROLLBACK +} {} +do_test 3.2 { + proc xFilter {zName} { + if {$zName == "t1"} { return 1 } + return 0 + } + sqlite3changeset_apply db $changeset noop xFilter + execsql { + SELECT * FROM t1; + SELECT * FROM t2; + SELECT * FROM t3; + } +} {1 1} +do_test 3.3 { + proc xFilter {zName} { + if {$zName == "t3"} { return 1 } + return 0 + } + sqlite3changeset_apply db $changeset noop xFilter + execsql { + SELECT * FROM t1; + SELECT * FROM t2; + SELECT * FROM t3; + } +} {1 1 3 3} + + + +finish_test diff --git a/ext/session/sessionD.test b/ext/session/sessionD.test new file mode 100644 index 0000000000..43ec03b447 --- /dev/null +++ b/ext/session/sessionD.test @@ -0,0 +1,308 @@ +# 2014 August 16 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file focuses on the sqlite3session_diff() function. +# + +if {![info exists testdir]} { + set testdir [file join [file dirname [info script]] .. .. test] +} +source [file join [file dirname [info script]] session_common.tcl] +source $testdir/tester.tcl +ifcapable !session {finish_test; return} + +set testprefix sessionD + +proc scksum {db dbname} { + + if {$dbname=="temp"} { + set master sqlite_temp_master + } else { + set master $dbname.sqlite_master + } + + set alltab [$db eval "SELECT name FROM $master WHERE type='table'"] + set txt [$db eval "SELECT * FROM $master ORDER BY type,name,sql"] + foreach tab $alltab { + set cols [list] + db eval "PRAGMA $dbname.table_info = $tab" x { + lappend cols "quote($x(name))" + } + set cols [join $cols ,] + append txt [db eval "SELECT $cols FROM $tab ORDER BY $cols"] + } + return [md5 $txt] +} + +# Ensure that the diff produced by comparing the current contents of [db] +# with itself is empty. +proc do_empty_diff_test {tn} { + forcedelete test.db2 + forcecopy test.db test.db2 + + execsql { ATTACH 'test.db2' AS aux } + sqlite3session S db main + foreach tbl [db eval {SELECT name FROM sqlite_master WHERE type='table'}] { + S attach $tbl + S diff aux $tbl + } + + set ::C [S changeset] + S delete + + uplevel [list do_test $tn {string length $::C} 0] +} + + +forcedelete test.db2 +do_execsql_test 1.0 { + CREATE TABLE t2(a PRIMARY KEY, b); + INSERT INTO t2 VALUES(1, 'one'); + INSERT INTO t2 VALUES(2, 'two'); + + ATTACH 'test.db2' AS aux; + CREATE TABLE aux.t2(a PRIMARY KEY, b); +} + +do_test 1.1 { + sqlite3session S db main + S attach t2 + S diff aux t2 + set C [S changeset] + S delete +} {} + +do_test 1.2 { + sqlite3 db2 test.db2 + sqlite3changeset_apply db2 $C "" + db2 close + db eval { SELECT * FROM aux.t2 } +} {1 one 2 two} + +do_diff_test 2.1 { + CREATE TABLE aux.t1(x, y, PRIMARY KEY(y)); + CREATE TABLE t1(x, y, PRIMARY KEY(y)); + + INSERT INTO t1 VALUES(1, 2); + INSERT INTO t1 VALUES(NULL, 'xyz'); + INSERT INTO t1 VALUES(4.5, 5.5); +} + +do_diff_test 2.2 { + CREATE TABLE aux.t1(x, y, PRIMARY KEY(y)); + CREATE TABLE t1(x, y, PRIMARY KEY(y)); + + INSERT INTO aux.t1 VALUES(1, 2); + INSERT INTO aux.t1 VALUES(NULL, 'xyz'); + INSERT INTO aux.t1 VALUES(4.5, 5.5); +} + +do_diff_test 2.3 { + CREATE TABLE aux.t1(a PRIMARY KEY, b TEXT); + CREATE TABLE t1(a PRIMARY KEY, b TEXT); + + INSERT INTO aux.t1 VALUES(1, 'one'); + INSERT INTO aux.t1 VALUES(2, 'two'); + INSERT INTO aux.t1 VALUES(3, 'three'); + + INSERT INTO t1 VALUES(1, 'I'); + INSERT INTO t1 VALUES(2, 'two'); + INSERT INTO t1 VALUES(3, 'III'); +} + +do_diff_test 2.4 { + CREATE TABLE aux.t1(a, b, c, d, PRIMARY KEY(c, b, a)); + CREATE TABLE t1(a, b, c, d, PRIMARY KEY(c, b, a)); + + INSERT INTO t1 VALUES('hvkzyipambwdqlvwv','',-458331.50,X'DA51ED5E84'); + INSERT INTO t1 VALUES(X'C5C6B5DD','jjxrath',40917,830244); + INSERT INTO t1 VALUES(-204877.54,X'1704C253D5F3AFA8',155120.88,NULL); + INSERT INTO t1 + VALUES('ckmqmzoeuvxisxqy',X'EB5A5D3A1DD22FD1','tidhjcbvbppdt',-642987.37); + INSERT INTO t1 VALUES(-851726,-161992,-469943,-159541); + INSERT INTO t1 VALUES(X'4A6A667F858938',185083,X'7A',NULL); + + INSERT INTO aux.t1 VALUES(415075.74,'auawczkb',X'',X'57B4FAAF2595'); + INSERT INTO aux.t1 VALUES(727637,711560,-181340,'hphuo'); + INSERT INTO aux.t1 + VALUES(-921322.81,662959,'lvlgwdgxaurr','ajjrzrbhqflsutnymgc'); + INSERT INTO aux.t1 VALUES(-146061,-377892,X'4E','gepvpvvuhszpxabbb'); + INSERT INTO aux.t1 VALUES(-851726,-161992,-469943,-159541); + INSERT INTO aux.t1 VALUES(X'4A6A667F858938',185083,X'7A',NULL); + INSERT INTO aux.t1 VALUES(-204877.54,X'1704C253D5F3AFA8',155120.88, 4); + INSERT INTO aux.t1 + VALUES('ckmqmzoeuvxisxqy',X'EB5A5D3A1DD22FD1','tidgtsplhjcbvbppdt',-642987.3); +} + +reset_db +do_execsql_test 3.0 { + CREATE TABLE t1(a, b, c, PRIMARY KEY(a)); + INSERT INTO t1 VALUES(1, 2, 3); + INSERT INTO t1 VALUES(4, 5, 6); + INSERT INTO t1 VALUES(7, 8, 9); + + CREATE TABLE t2(a, b, c, PRIMARY KEY(a, b)); + INSERT INTO t2 VALUES(1, 2, 3); + INSERT INTO t2 VALUES(4, 5, 6); + INSERT INTO t2 VALUES(7, 8, 9); + + CREATE TABLE t3(a, b, c, PRIMARY KEY(a, b, c)); + INSERT INTO t3 VALUES(1, 2, 3); + INSERT INTO t3 VALUES(4, 5, 6); + INSERT INTO t3 VALUES(7, 8, 9); +} +do_empty_diff_test 3.1 + + +#------------------------------------------------------------------------- +# Test some error cases: +# +# 1) schema mismatches between the two dbs, and +# 2) tables with no primary keys. This is not actually an error, but +# should not add any changes to the session object. +# +reset_db +forcedelete test.db2 +do_execsql_test 4.0 { + ATTACH 'test.db2' AS ixua; + CREATE TABLE ixua.t1(a, b, c); + CREATE TABLE main.t1(a, b, c); + INSERT INTO main.t1 VALUES(1, 2, 3); + + CREATE TABLE ixua.t2(a PRIMARY KEY, b, c); + CREATE TABLE main.t2(a PRIMARY KEY, b, x); +} + +do_test 4.1.1 { + sqlite3session S db main + S attach t1 + list [catch { S diff ixua t1 } msg] $msg +} {0 {}} +do_test 4.1.2 { + string length [S changeset] +} {0} +S delete + +do_test 4.2.2 { + sqlite3session S db main + S attach t2 + list [catch { S diff ixua t2 } msg] $msg +} {1 {SQLITE_SCHEMA - table schemas do not match}} +S delete + +do_test 4.3.1 { + sqlite3session S db main + S attach t4 + execsql { CREATE TABLE t4(i PRIMARY KEY, b) } + list [catch { S diff ixua t4 } msg] $msg +} {1 {SQLITE_SCHEMA - no such table: ixua.t4}} +S delete +do_catchsql_test 4.3.2 { + SELECT * FROM ixua.t4; +} {1 {no such table: ixua.t4}} + +do_test 4.4.1 { + sqlite3session S db main + S attach sqlite_stat1 + execsql { ANALYZE } + execsql { DROP TABLE ixua.sqlite_stat1 } + list [catch { S diff ixua sqlite_stat1 } msg] $msg +} {1 {SQLITE_SCHEMA - no such table: ixua.sqlite_stat1}} +S delete +do_catchsql_test 4.4.2 { + SELECT * FROM ixua.sqlite_stat1; +} {1 {no such table: ixua.sqlite_stat1}} + +do_test 4.5.1 { + sqlite3session S db main + S attach t8 + list [catch { S diff ixua t8 } msg] $msg +} {0 {}} +S delete +do_catchsql_test 4.5.2 { + SELECT * FROM ixua.i8; +} {1 {no such table: ixua.i8}} + +#------------------------------------------------------------------------- +# Test that sqlite3session_diff() really does automatically attach tables, +# as documented. +# +reset_db +forcedelete test.db2 +do_execsql_test 4.0 { + ATTACH 'test.db2' AS two; + CREATE TABLE two.t1(a PRIMARY KEY, b, c); + CREATE TABLE t1(a PRIMARY KEY, b, c); + + INSERT INTO two.t1 VALUES(1, 2, 3), (4, 5, 6), (7, 8, 9); +} + +do_test 4.1 { + sqlite3session S db main + S attach t8 + S diff two t1 +} {} + +do_changeset_test 4.2 S { + {DELETE t1 0 X.. {i 1 i 2 i 3} {}} + {DELETE t1 0 X.. {i 4 i 5 i 6} {}} + {DELETE t1 0 X.. {i 7 i 8 i 9} {}} +} + +S delete + +#------------------------------------------------------------------------- +# Test that sqlite3session_diff() really does return errors if the named +# table or database do not exist. +# +reset_db +forcedelete test.db2 +do_execsql_test 5.0 { + ATTACH 'test.db2' AS two; + CREATE TABLE main.t1(a INTEGER PRIMARY KEY, b); + CREATE TABLE main.t2(a INTEGER PRIMARY KEY, b); + CREATE TABLE two.t1(a, b INTEGER PRIMARY KEY); +} + +proc do_sessions_diff_error {tn db tbl err} { + sqlite3session S db main + set rc [catch {S diff $db $tbl} msg] + + set ::sdgot [list $rc $msg] + do_test $tn [list set sdgot] [list {*}$err] + + S delete +} + +# Test that it is an error if the named db is missing. +do_sessions_diff_error 5.1 nosuchdb t1 { + 1 {SQLITE_SCHEMA - no such table: nosuchdb.t1} +} + +# Test that it is an error if the named db is present, but named table is not. +do_sessions_diff_error 5.2 two t2 { + 1 {SQLITE_SCHEMA - no such table: two.t2} +} + +# Test that it is an error if the tables are present, but schemas do not match. +do_sessions_diff_error 5.3 two t1 { + 1 {SQLITE_SCHEMA - table schemas do not match} +} + +do_test 5.4 { + sqlite3session S db main + + catch {S diff two blue} + catch {S diff two blue} + + S delete +} {} + +finish_test diff --git a/ext/session/sessionE.test b/ext/session/sessionE.test new file mode 100644 index 0000000000..9cec7d779d --- /dev/null +++ b/ext/session/sessionE.test @@ -0,0 +1,113 @@ +# 2015 June 02 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# This file implements regression tests for the sessions module. +# Specifically, it tests that operations on tables without primary keys +# are ignored. +# + + + +if {![info exists testdir]} { + set testdir [file join [file dirname [info script]] .. .. test] +} +source [file join [file dirname [info script]] session_common.tcl] +source $testdir/tester.tcl +ifcapable !session {finish_test; return} +set testprefix sessionE + +# +# Test plan: +# +# 1.*: Test that non-PK tables are not auto-attached. +# 2.*: Test that explicitly attaching a non-PK table is a no-op. +# 3.*: Test that sqlite3session_diff() on a non-PK table is a no-op. +# + + +#-------------------------------------------------------------------------- +reset_db +do_execsql_test 1.0 { + CREATE TABLE t1(a, b); + CREATE TABLE t2(a PRIMARY KEY, b); +} +do_test 1.1 { + sqlite3session S db main + S attach * + execsql { + INSERT INTO t1 VALUES(1, 2); + INSERT INTO t2 VALUES(1, 2); + } +} {} +do_changeset_test 1.2 S { + {INSERT t2 0 X. {} {i 1 i 2}} +} +S delete + +reset_db +do_execsql_test 2.0 { + CREATE TABLE t1(a, b); + CREATE TABLE t2(a PRIMARY KEY, b); +} +do_test 2.1 { + sqlite3session S db main + S attach t1 + S attach t2 + execsql { + INSERT INTO t1 VALUES(3, 4); + INSERT INTO t2 VALUES(3, 4); + INSERT INTO t1 VALUES(5, 6); + INSERT INTO t2 VALUES(5, 6); + } +} {} +do_changeset_test 2.2 S { + {INSERT t2 0 X. {} {i 3 i 4}} + {INSERT t2 0 X. {} {i 5 i 6}} +} +S delete + +reset_db +forcedelete test.db2 +do_execsql_test 3.0 { + ATTACH 'test.db2' AS aux; + CREATE TABLE aux.t1(a, b); + CREATE TABLE aux.t2(a PRIMARY KEY, b); + + CREATE TABLE t1(a, b); + CREATE TABLE t2(a PRIMARY KEY, b); + + INSERT INTO t1 VALUES(1, 2); + INSERT INTO t2 VALUES(3, 4); +} +do_test 3.1 { + sqlite3session S db main + S attach t1 + S diff aux t1 + + S attach t2 + S diff aux t2 +} {} +do_changeset_test 3.2 S { + {INSERT t2 0 X. {} {i 3 i 4}} +} +do_execsql_test 3.3 { + INSERT INTO t1 VALUES(5, 6); + INSERT INTO t2 VALUES(7, 8); +} +do_changeset_test 3.4 S { + {INSERT t2 0 X. {} {i 3 i 4}} + {INSERT t2 0 X. {} {i 7 i 8}} +} + + +S delete + +finish_test diff --git a/ext/session/sessionF.test b/ext/session/sessionF.test new file mode 100644 index 0000000000..6a6eabcfd5 --- /dev/null +++ b/ext/session/sessionF.test @@ -0,0 +1,294 @@ +# 2015 June 02 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# This file implements regression tests for the sessions module. +# Specifically, it tests that tables appear in the correct order +# within changesets and patchsets. +# + + + +if {![info exists testdir]} { + set testdir [file join [file dirname [info script]] .. .. test] +} +source [file join [file dirname [info script]] session_common.tcl] +source $testdir/tester.tcl +ifcapable !session {finish_test; return} +set testprefix sessionF + +# +# Test plan: +# +# 1.*: Test that sqlite3session_changeset() and sqlite3session_patchset() +# output tables in the right order. +# +# 2.*: Test that sqlite3session_invert() does not modify the order of +# tables within a changeset. +# +# 3.*: Test that sqlite3session_concat outputs tables in the right order. +# + +# Create a db schema to use. +# +do_execsql_test 1.0 { + CREATE TABLE t3(e PRIMARY KEY, f); + CREATE TABLE t1(a PRIMARY KEY, b); + CREATE TABLE t2(c PRIMARY KEY, d); +} + +#----------------------------------------------------------------------- +# 1.* - changeset() and patchset(). +# + +foreach {tn setup result} { + 1 { + S attach * + } { + {INSERT t2 0 X. {} {i 2 t two}} + {INSERT t1 0 X. {} {i 1 t one}} + {INSERT t3 0 X. {} {i 3 t three}} + } + + 2 { + S attach t1 + S attach * + } { + {INSERT t1 0 X. {} {i 1 t one}} + {INSERT t2 0 X. {} {i 2 t two}} + {INSERT t3 0 X. {} {i 3 t three}} + } + + 3 { + S attach t3 + S attach t2 + S attach t1 + } { + {INSERT t3 0 X. {} {i 3 t three}} + {INSERT t2 0 X. {} {i 2 t two}} + {INSERT t1 0 X. {} {i 1 t one}} + } +} { + execsql { + DELETE FROM t1; + DELETE FROM t2; + DELETE FROM t3; + } + sqlite3session S db main + eval $setup + + do_execsql_test 1.$tn.1 { + INSERT INTO t2 VALUES(2, 'two'); + INSERT INTO t1 VALUES(1, 'one'); + INSERT INTO t3 VALUES(3, 'three'); + } + + do_changeset_test 1.1.$tn.2 S $result + do_patchset_test 1.1.$tn.3 S $result + + S delete +} + +foreach {tn setup result} { + 1 { + S attach * + } { + {INSERT t2 0 X. {} {i 4 t four}} + {INSERT t2 0 X. {} {i 5 t five}} + {INSERT t1 0 X. {} {i 1 t one}} + {INSERT t3 0 X. {} {i 6 t six}} + } + + 2 { + S attach t1 + S attach * + } { + {INSERT t1 0 X. {} {i 1 t one}} + {INSERT t2 0 X. {} {i 4 t four}} + {INSERT t2 0 X. {} {i 5 t five}} + {INSERT t3 0 X. {} {i 6 t six}} + } + + 3 { + S attach t3 + S attach t2 + S attach t1 + } { + {INSERT t3 0 X. {} {i 6 t six}} + {INSERT t2 0 X. {} {i 4 t four}} + {INSERT t2 0 X. {} {i 5 t five}} + {INSERT t1 0 X. {} {i 1 t one}} + } +} { + execsql { + DELETE FROM t1; + DELETE FROM t2; + DELETE FROM t3; + } + sqlite3session S db main + eval $setup + + do_execsql_test 1.$tn.1 { + INSERT INTO t2 VALUES(2, 'two'); + INSERT INTO t1 VALUES(1, 'one'); + DELETE FROM t2; + INSERT INTO t2 VALUES(4, 'four'); + INSERT INTO t2 VALUES(5, 'five'); + INSERT INTO t3 VALUES(6, 'six'); + } + + do_changeset_test 1.2.$tn.2 S $result + do_patchset_test 1.2.$tn.2 S $result + + S delete +} + +#------------------------------------------------------------------------- +# 2.* - invert() +# + +foreach {tn setup result} { + 1 { + S attach * + } { + {DELETE t2 0 X. {i 4 t four} {}} + {DELETE t2 0 X. {i 5 t five} {}} + {DELETE t1 0 X. {i 1 t one} {}} + {DELETE t3 0 X. {i 6 t six} {}} + } + + 2 { + S attach t1 + S attach * + } { + {DELETE t1 0 X. {i 1 t one} {}} + {DELETE t2 0 X. {i 4 t four} {}} + {DELETE t2 0 X. {i 5 t five} {}} + {DELETE t3 0 X. {i 6 t six} {}} + } + + 3 { + S attach t3 + S attach t2 + S attach t1 + } { + {DELETE t3 0 X. {i 6 t six} {}} + {DELETE t2 0 X. {i 4 t four} {}} + {DELETE t2 0 X. {i 5 t five} {}} + {DELETE t1 0 X. {i 1 t one} {}} + } +} { + execsql { + DELETE FROM t1; + DELETE FROM t2; + DELETE FROM t3; + } + sqlite3session S db main + eval $setup + + do_execsql_test 1.$tn.1 { + INSERT INTO t2 VALUES(2, 'two'); + INSERT INTO t1 VALUES(1, 'one'); + DELETE FROM t2; + INSERT INTO t2 VALUES(4, 'four'); + INSERT INTO t2 VALUES(5, 'five'); + INSERT INTO t3 VALUES(6, 'six'); + } + + do_changeset_invert_test 2.$tn.2 S $result + + S delete +} + +#------------------------------------------------------------------------- +# 3.* - concat() +# +foreach {tn setup1 sql1 setup2 sql2 result} { + 1 { + S attach * + } { + INSERT INTO t1 VALUES(1, 'one'); + INSERT INTO t2 VALUES(2, 'two'); + } { + S attach t2 + S attach t1 + } { + INSERT INTO t1 VALUES(3, 'three'); + INSERT INTO t2 VALUES(4, 'four'); + } { + {INSERT t1 0 X. {} {i 1 t one}} + {INSERT t1 0 X. {} {i 3 t three}} + {INSERT t2 0 X. {} {i 2 t two}} + {INSERT t2 0 X. {} {i 4 t four}} + } + + 1 { + S attach t2 + S attach t1 + } { + INSERT INTO t1 VALUES(1, 'one'); + INSERT INTO t2 VALUES(2, 'two'); + } { + S attach * + } { + INSERT INTO t1 VALUES(3, 'three'); + INSERT INTO t2 VALUES(4, 'four'); + } { + {INSERT t2 0 X. {} {i 2 t two}} + {INSERT t2 0 X. {} {i 4 t four}} + {INSERT t1 0 X. {} {i 1 t one}} + {INSERT t1 0 X. {} {i 3 t three}} + } + + 1 { + S attach * + } { + INSERT INTO t2 VALUES(2, 'two'); + } { + S attach * + } { + INSERT INTO t1 VALUES(3, 'three'); + INSERT INTO t2 VALUES(4, 'four'); + INSERT INTO t3 VALUES(5, 'five'); + } { + {INSERT t2 0 X. {} {i 2 t two}} + {INSERT t2 0 X. {} {i 4 t four}} + {INSERT t1 0 X. {} {i 3 t three}} + {INSERT t3 0 X. {} {i 5 t five}} + } + +} { + execsql { + DELETE FROM t1; + DELETE FROM t2; + DELETE FROM t3; + } + sqlite3session S db main + eval $setup1 + execsql $sql1 + set c1 [S changeset] + S delete + + sqlite3session S db main + eval $setup2 + execsql $sql2 + set c2 [S changeset] + S delete + + set res [list] + sqlite3session_foreach x [sqlite3changeset_concat $c1 $c2] { + lappend res $x + } + + do_test 3.$tn { set res } [list {*}$result] +} + + +finish_test diff --git a/ext/session/sessionG.test b/ext/session/sessionG.test new file mode 100644 index 0000000000..1ebcc926a5 --- /dev/null +++ b/ext/session/sessionG.test @@ -0,0 +1,250 @@ +# 2016 March 30 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# This file implements regression tests for the sessions module. +# Specifically, it tests that UNIQUE constraints are dealt with correctly. +# + + + +if {![info exists testdir]} { + set testdir [file join [file dirname [info script]] .. .. test] +} +source [file join [file dirname [info script]] session_common.tcl] +source $testdir/tester.tcl +ifcapable !session {finish_test; return} +set testprefix sessionG + + +forcedelete test.db2 +sqlite3 db2 test.db2 + +do_test 1.0 { + do_common_sql { + CREATE TABLE t1(a PRIMARY KEY, b UNIQUE); + INSERT INTO t1 VALUES(1, 'one'); + INSERT INTO t1 VALUES(2, 'two'); + INSERT INTO t1 VALUES(3, 'three'); + } + do_then_apply_sql -ignorenoop { + DELETE FROM t1 WHERE a=1; + INSERT INTO t1 VALUES(4, 'one'); + } + compare_db db db2 +} {} + +do_test 1.1 { + do_then_apply_sql -ignorenoop { + DELETE FROM t1 WHERE a=4; + INSERT INTO t1 VALUES(1, 'one'); + } + compare_db db db2 +} {} + +do_test 1.2 { + execsql { INSERT INTO t1 VALUES(5, 'five') } db2 + do_then_apply_sql -ignorenoop { + INSERT INTO t1 VALUES(11, 'eleven'); + INSERT INTO t1 VALUES(12, 'five'); + } + execsql { SELECT * FROM t1 } db2 +} {2 two 3 three 1 one 5 five 11 eleven} + +do_test 1.3 { + execsql { SELECT * FROM t1 } +} {2 two 3 three 1 one 11 eleven 12 five} + +#------------------------------------------------------------------------- +# +reset_db +db2 close +forcedelete test.db2 +sqlite3 db2 test.db2 + +do_test 2.1 { + do_common_sql { + CREATE TABLE t1(a PRIMARY KEY, b UNIQUE, c UNIQUE); + INSERT INTO t1 VALUES(1, 1, 1); + INSERT INTO t1 VALUES(2, 2, 2); + INSERT INTO t1 VALUES(3, 3, 3); + } +} {} + +do_test 2.2.1 { + # It is not possible to apply the changeset generated by the following + # SQL, as none of the three updated rows may be updated as part of the + # first pass. + do_then_apply_sql -ignorenoop { + UPDATE t1 SET b=0 WHERE a=1; + UPDATE t1 SET b=1 WHERE a=2; + UPDATE t1 SET b=2 WHERE a=3; + UPDATE t1 SET b=3 WHERE a=1; + } + db2 eval { SELECT a, b FROM t1 } +} {1 1 2 2 3 3} +do_test 2.2.2 { db eval { SELECT a, b FROM t1 } } {1 3 2 1 3 2} + +#------------------------------------------------------------------------- +# +reset_db +db2 close +forcedelete test.db2 +sqlite3 db2 test.db2 + +do_test 3.1 { + do_common_sql { + CREATE TABLE t1(a PRIMARY KEY, b UNIQUE, c UNIQUE); + INSERT INTO t1 VALUES(1, 1, 1); + INSERT INTO t1 VALUES(2, 2, 2); + INSERT INTO t1 VALUES(3, 3, 3); + } +} {} + +do_test 3.3 { + do_then_apply_sql -ignorenoop { + UPDATE t1 SET b=4 WHERE a=3; + UPDATE t1 SET b=3 WHERE a=2; + UPDATE t1 SET b=2 WHERE a=1; + } + compare_db db db2 +} {} + +do_test 3.4 { + do_then_apply_sql -ignorenoop { + UPDATE t1 SET b=1 WHERE a=1; + UPDATE t1 SET b=2 WHERE a=2; + UPDATE t1 SET b=3 WHERE a=3; + } + compare_db db db2 +} {} + +#------------------------------------------------------------------------- +# +reset_db +db2 close +forcedelete test.db2 +sqlite3 db2 test.db2 + +do_test 4.1 { + do_common_sql { + CREATE TABLE t1(a PRIMARY KEY, b UNIQUE); + INSERT INTO t1 VALUES(1, 1); + INSERT INTO t1 VALUES(2, 2); + INSERT INTO t1 VALUES(3, 3); + + CREATE TABLE t2(a PRIMARY KEY, b UNIQUE); + INSERT INTO t2 VALUES(1, 1); + INSERT INTO t2 VALUES(2, 2); + INSERT INTO t2 VALUES(3, 3); + } +} {} + +do_test 4.2 { + do_then_apply_sql -ignorenoop { + UPDATE t1 SET b=4 WHERE a=3; + UPDATE t1 SET b=3 WHERE a=2; + UPDATE t1 SET b=2 WHERE a=1; + + UPDATE t2 SET b=0 WHERE a=1; + UPDATE t2 SET b=1 WHERE a=2; + UPDATE t2 SET b=2 WHERE a=3; + } + compare_db db db2 +} {} + +do_test 4.3 { + do_then_apply_sql -ignorenoop { + UPDATE t1 SET b=1 WHERE a=1; + UPDATE t1 SET b=2 WHERE a=2; + UPDATE t1 SET b=3 WHERE a=3; + + UPDATE t2 SET b=3 WHERE a=3; + UPDATE t2 SET b=2 WHERE a=2; + UPDATE t2 SET b=1 WHERE a=1; + } + compare_db db db2 +} {} + +#------------------------------------------------------------------------- +reset_db +catch { db2 close } +forcedelete test.db2 +sqlite3 db2 test.db2 + +do_execsql_test 5.0.1 { + CREATE TABLE t1(a PRIMARY KEY, b, c); + CREATE TABLE t2(a, b, c PRIMARY KEY); + CREATE TABLE t3(a, b PRIMARY KEY, c); +} +do_execsql_test -db db2 5.0.2 { + CREATE TABLE t1(a PRIMARY KEY, b, c); + CREATE TABLE t2(a, b, c); + CREATE TABLE t3(a, b PRIMARY KEY, c); +} + +do_test 5.1 { + do_then_apply_sql -ignorenoop { + INSERT INTO t1 VALUES(1, 2, 3); + INSERT INTO t2 VALUES(4, 5, 6); + INSERT INTO t3 VALUES(7, 8, 9); + } + + db2 eval { + SELECT * FROM t1; + SELECT * FROM t2; + SELECT * FROM t3; + } +} {1 2 3 7 8 9} + +#------------------------------------------------------------------------- + +reset_db +db func number_name number_name +do_execsql_test 6.0 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b); + CREATE UNIQUE INDEX t1b ON t1(b); + WITH s(i) AS ( + SELECT 1 + UNION ALL + SELECT i+1 FROM s WHERE i<1000 + ) + INSERT INTO t1 SELECT i, number_name(i) FROM s; +} + +do_test 6.1 { + db eval BEGIN + set ::C [changeset_from_sql { + DELETE FROM t1; + WITH s(i) AS ( + SELECT 1 + UNION ALL + SELECT i+1 FROM s WHERE i<1000 + ) + INSERT INTO t1 SELECT i, number_name(i+1) FROM s; + }] + db eval ROLLBACK + execsql { SELECT count(*) FROM t1 WHERE number_name(a) IS NOT b } +} {0} + +proc xConflict {args} { exit ; return "OMIT" } +do_test 6.2 { + sqlite3changeset_apply db $C xConflict +} {} + +do_execsql_test 6.3 { SELECT count(*) FROM t1; } {1000} +do_execsql_test 6.4 { + SELECT count(*) FROM t1 WHERE number_name(a+1) IS NOT b; +} {0} + +# db eval { SELECT * FROM t1 } { puts "$a || $b" } + + +finish_test diff --git a/ext/session/sessionH.test b/ext/session/sessionH.test new file mode 100644 index 0000000000..e1b12571c6 --- /dev/null +++ b/ext/session/sessionH.test @@ -0,0 +1,84 @@ +# 2018 January 18 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +if {![info exists testdir]} { + set testdir [file join [file dirname [info script]] .. .. test] +} +source [file join [file dirname [info script]] session_common.tcl] +source $testdir/tester.tcl +ifcapable !session {finish_test; return} +set testprefix sessionH + +forcedelete test.db2 +sqlite3 db2 test.db2 + +do_test 1.0 { + do_common_sql { + CREATE TABLE t1(a, b, c, PRIMARY KEY(a, b)); + } + do_then_apply_sql -ignorenoop { + WITH s(i) AS ( + VALUES(1) UNION ALL SELECT i+1 FROM s WHERe i<10000 + ) + INSERT INTO t1 SELECT 'abcde', randomblob(16), i FROM s; + } + compare_db db db2 +} {} + +#------------------------------------------------------------------------ +db2 close +reset_db + +do_execsql_test 2.0 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c); + INSERT INTO main.t1 VALUES(1, 2, 3), (4, 5, 6), (7, 8, 9); +} + +do_test 2.1 { + sqlite3session S db main + S attach * + db eval { + BEGIN; + INSERT INTO t1 VALUES(10, 11, 12); + DELETE FROM t1 WHERE a=1; + UPDATE t1 SET b='five', c='six' WHERE a=4; + } + + set C [S changeset] + db eval ROLLBACK + S delete + set {} {} +} {} + +do_execsql_test 2.2 { + CREATE TEMP TABLE t1(a INTEGER PRIMARY KEY, b, c); + INSERT INTO temp.t1 VALUES(1, 2, 3), (4, 5, 6), (7, 8, 9); +} + +set ::conflict [list] +proc xConflict {args} { lappend ::conflict $args ; return "" } +do_test 2.3 { + sqlite3changeset_apply db $C xConflict + set ::conflict +} {} +do_execsql_test 2.4 { + SELECT * FROM main.t1; + SELECT '****'; + SELECT * FROM temp.t1; +} { + 4 five six 7 8 9 10 11 12 + **** + 1 2 3 4 5 6 7 8 9 +} + + +finish_test diff --git a/ext/session/sessionI.test b/ext/session/sessionI.test new file mode 100644 index 0000000000..2012f24dca --- /dev/null +++ b/ext/session/sessionI.test @@ -0,0 +1,88 @@ +# 2015 July 14 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# + +if {![info exists testdir]} { + set testdir [file join [file dirname [info script]] .. .. test] +} +source [file join [file dirname [info script]] session_common.tcl] +source $testdir/tester.tcl +ifcapable !session {finish_test; return} + +set testprefix sessionI + +forcedelete test.db2 +sqlite3 db2 test.db2 + +do_test 1.0 { + do_common_sql { + CREATE TABLE t1(x INTEGER PRIMARY KEY, y); + } +} {} + +set C [changeset_from_sql { + INSERT INTO t1 VALUES(1, 'one'); + INSERT INTO t1 VALUES(2, 'two'); + INSERT INTO t1 VALUES(3, 'three'); + INSERT INTO t1 VALUES(4, 'four'); + INSERT INTO t1 VALUES(5, 'five'); + INSERT INTO t1 VALUES(6, 'six'); +}] + +do_execsql_test 1.1 { + SELECT * FROM t1 +} { + 1 one 2 two 3 three 4 four 5 five 6 six +} + +proc xFilter {data} { + foreach {op tname flag pk old new} $data {} + if {$op=="INSERT"} { + set ipk [lindex $new 1] + return [expr $ipk % 2] + } + return 1 +} +proc xConflict {args} { +} + +sqlite3changeset_apply_v3 db2 $C xConflict xFilter + +do_execsql_test -db db2 1.2 { + SELECT * FROM t1 +} { + 1 one 3 three 5 five +} + +do_execsql_test -db db2 1.3 { + DELETE FROM t1 +} +sqlite3changeset_apply_v3 db2 $C xConflict + +do_execsql_test -db db2 1.4 { + SELECT * FROM t1 +} { + 1 one 2 two 3 three 4 four 5 five 6 six +} + +proc xFilter2 {data} { + return 0 +} +do_execsql_test -db db2 1.5 { + DELETE FROM t1 +} +sqlite3changeset_apply_v3 db2 $C xConflict xFilter2 +do_execsql_test -db db2 1.6 { + SELECT * FROM t1 +} { } + +finish_test diff --git a/ext/session/session_common.tcl b/ext/session/session_common.tcl new file mode 100644 index 0000000000..7c1273bb1a --- /dev/null +++ b/ext/session/session_common.tcl @@ -0,0 +1,303 @@ + +proc do_changeset_test {tn session res} { + set r [list] + foreach x $res {lappend r $x} + uplevel do_test $tn [list [subst -nocommands { + set x [list] + sqlite3session_foreach c [$session changeset] { lappend x [set c] } + set x + }]] [list $r] +} + +proc do_patchset_test {tn session res} { + set r [list] + foreach x $res {lappend r $x} + uplevel do_test $tn [list [subst -nocommands { + set x [list] + sqlite3session_foreach c [$session patchset] { lappend x [set c] } + set x + }]] [list $r] +} + + +proc do_changeset_invert_test {tn session res} { + set r [list] + foreach x $res {lappend r $x} + uplevel do_test $tn [list [subst -nocommands { + set x [list] + set changeset [sqlite3changeset_invert [$session changeset]] + sqlite3session_foreach c [set changeset] { lappend x [set c] } + set x + }]] [list $r] +} + + +proc do_conflict_test {tn args} { + + set O(-tables) [list] + set O(-sql) [list] + set O(-conflicts) [list] + set O(-policy) "OMIT" + + array set V $args + foreach key [array names V] { + if {![info exists O($key)]} {error "no such option: $key"} + } + array set O $args + + proc xConflict {args} [subst -nocommands { + lappend ::xConflict [set args] + return $O(-policy) + }] + proc bgerror {args} { set ::background_error $args } + + sqlite3session S db main + S object_config rowid 1 + foreach t $O(-tables) { S attach $t } + execsql $O(-sql) + + set ::xConflict [list] + sqlite3changeset_apply db2 [S changeset] xConflict + + set conflicts [list] + foreach c $O(-conflicts) { + lappend conflicts $c + } + + after 1 {set go 1} + vwait go + + uplevel do_test $tn [list { set ::xConflict }] [list $conflicts] + S delete +} + +proc do_common_sql {sql} { + execsql $sql db + execsql $sql db2 +} + +proc changeset_from_sql {sql {dbname main}} { + if {$dbname == "main"} { + return [sql_exec_changeset db $sql] + } + set rc [catch { + sqlite3session S db $dbname + S object_config rowid 1 + db eval "SELECT name FROM $dbname.sqlite_master WHERE type = 'table'" { + S attach $name + } + db eval $sql + S changeset + } changeset] + catch { S delete } + + if {$rc} { + error $changeset + } + return $changeset +} + +proc patchset_from_sql {sql {dbname main}} { + set rc [catch { + sqlite3session S db $dbname + db eval "SELECT name FROM $dbname.sqlite_master WHERE type = 'table'" { + S attach $name + } + db eval $sql + S patchset + } patchset] + catch { S delete } + + if {$rc} { + error $patchset + } + return $patchset +} + +# Usage: do_then_apply_sql ?-ignorenoop? SQL ?DBNAME? +# +proc do_then_apply_sql {args} { + + set bIgnoreNoop 0 + set a1 [lindex $args 0] + if {[string length $a1]>1 && [string first $a1 -ignorenoop]==0} { + set bIgnoreNoop 1 + set args [lrange $args 1 end] + } + + if {[llength $args]!=1 && [llength $args]!=2} { + error "usage: do_then_apply_sql ?-ignorenoop? SQL ?DBNAME?" + } + + set sql [lindex $args 0] + if {[llength $args]==1} { + set dbname main + } else { + set dbname [lindex $args 1] + } + + set ::n_conflict 0 + proc xConflict args { incr ::n_conflict ; return "OMIT" } + set rc [catch { + sqlite3session S db $dbname + S object_config rowid 1 + db eval "SELECT name FROM $dbname.sqlite_master WHERE type = 'table'" { + S attach $name + } + db eval $sql + set ::changeset [S changeset] + sqlite3changeset_apply db2 $::changeset xConflict + } msg] + + catch { S delete } + if {$rc} {error $msg} + + if {$bIgnoreNoop} { + set nSave $::n_conflict + set ::n_conflict 0 + proc xConflict args { incr ::n_conflict ; return "OMIT" } + sqlite3changeset_apply_v2 -ignorenoop db2 $::changeset xConflict + if {$::n_conflict!=$nSave} { + error "-ignorenoop problem ($::n_conflict $nSave)..." + } + } +} + +proc do_iterator_test {tn tbl_list sql res} { + sqlite3session S db main + S object_config rowid 1 + + if {[llength $tbl_list]==0} { S attach * } + foreach t $tbl_list {S attach $t} + + execsql $sql + + set r [list] + foreach v $res { lappend r $v } + + set x [list] +# set ::c [S changeset] ; execsql_pp { SELECT quote($::c) } + sqlite3session_foreach c [S changeset] { lappend x $c } + uplevel do_test $tn [list [list set {} $x]] [list $r] + + S delete +} + +# Compare the contents of all tables in [db1] and [db2]. Throw an error if +# they are not identical, or return an empty string if they are. +# +proc compare_db {db1 db2} { + + set sql {SELECT name FROM sqlite_master WHERE type = 'table' ORDER BY name} + set lot1 [$db1 eval $sql] + set lot2 [$db2 eval $sql] + + if {$lot1 != $lot2} { + puts $lot1 + puts $lot2 + error "databases contain different tables" + } + + foreach tbl $lot1 { + set col1 [list] + set col2 [list] + set quoted [list] + + $db1 eval "PRAGMA table_info = $tbl" { lappend col1 $name } + $db2 eval "PRAGMA table_info = $tbl" { + lappend col2 $name + lappend quoted "\"[string map {\" \"\"} $name]\"" + } + if {$col1 != $col2} { error "table $tbl schema mismatch" } + + set sql "SELECT * FROM $tbl ORDER BY [join $quoted ,]" + set data1 [$db1 eval $sql] + set data2 [$db2 eval $sql] + if {$data1 != $data2} { + puts "$db1: $data1" + puts "$db2: $data2" + error "table $tbl data mismatch" + } + } + + return "" +} + +proc changeset_to_list {c} { + set list [list] + sqlite3session_foreach elem $c { lappend list $elem } + lsort $list +} + +set ones {zero one two three four five six seven eight nine + ten eleven twelve thirteen fourteen fifteen sixteen seventeen + eighteen nineteen} +set tens {{} ten twenty thirty forty fifty sixty seventy eighty ninety} +proc number_name {n} { + if {$n>=1000} { + set txt "[number_name [expr {$n/1000}]] thousand" + set n [expr {$n%1000}] + } else { + set txt {} + } + if {$n>=100} { + append txt " [lindex $::ones [expr {$n/100}]] hundred" + set n [expr {$n%100}] + } + if {$n>=20} { + append txt " [lindex $::tens [expr {$n/10}]]" + set n [expr {$n%10}] + } + if {$n>0} { + append txt " [lindex $::ones $n]" + } + set txt [string trim $txt] + if {$txt==""} {set txt zero} + return $txt +} + +proc scksum {db dbname} { + + if {$dbname=="temp"} { + set master sqlite_temp_master + } else { + set master $dbname.sqlite_master + } + + set alltab [$db eval "SELECT name FROM $master WHERE type='table'"] + set txt [$db eval "SELECT * FROM $master ORDER BY type,name,sql"] + foreach tab $alltab { + set cols [list] + db eval "PRAGMA $dbname.table_info = $tab" x { + lappend cols "quote($x(name))" + } + set cols [join $cols ,] + append txt [db eval "SELECT $cols FROM $dbname.$tab ORDER BY $cols"] + } + return [md5 $txt] +} + +proc do_diff_test {tn setup} { + reset_db + forcedelete test.db2 + execsql { ATTACH 'test.db2' AS aux } + execsql $setup + + sqlite3session S db main + S object_config rowid 1 + foreach tbl [db eval {SELECT name FROM sqlite_master WHERE type='table'}] { + S attach $tbl + S diff aux $tbl + } + + set C [S changeset] + S delete + + sqlite3 db2 test.db2 + sqlite3changeset_apply db2 $C "" + uplevel do_test $tn.1 [list {execsql { PRAGMA integrity_check } db2}] ok + db2 close + + set cksum [scksum db main] + uplevel do_test $tn.2 [list {scksum db aux}] [list $cksum] +} diff --git a/ext/session/session_gen.test b/ext/session/session_gen.test new file mode 100644 index 0000000000..e9de4beaba --- /dev/null +++ b/ext/session/session_gen.test @@ -0,0 +1,191 @@ +# 2025 Jan 28 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. +# + +if {![info exists testdir]} { + set testdir [file join [file dirname [info script]] .. .. test] +} +source [file join [file dirname [info script]] session_common.tcl] +source $testdir/tester.tcl +ifcapable !session {finish_test; return} + +set testprefix session_gen + + +foreach {otn sct} { + 1 VIRTUAL + 2 STORED +} { +eval [string map [list %TYPE% $sct] { + reset_db + set testprefix $testprefix-$otn + +do_execsql_test 1.0 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b); + + CREATE TABLE t2(a INTEGER PRIMARY KEY, b AS (c+1) %TYPE%, c); + + CREATE TABLE t3( + a, + b AS (a+10) %TYPE%, + c, + d AS (c+1) %TYPE%, + e, + PRIMARY KEY(c, e) + ) WITHOUT ROWID; + + CREATE TABLE t4(a AS (c*100) %TYPE%, b INTEGER PRIMARY KEY, c); + + CREATE TABLE t5(x, y); +} + +foreach {tn sql changeset} { + + 0.1 { + INSERT INTO t5 VALUES('abc', 'def'); + } { + {INSERT t5 0 X.. {} {i 1 t abc t def}} + } + 0.2 { + UPDATE t5 SET y='xyz' WHERE rowid=1; + } { + {UPDATE t5 0 X.. {i 1 {} {} t def} {{} {} {} {} t xyz}} + } + 0.3 { + DELETE FROM t5; + } { + {DELETE t5 0 X.. {i 1 t abc t xyz} {}} + } + + 1.1 { + INSERT INTO t2 VALUES(1, 2); + INSERT INTO t2 VALUES(2, 123); + } { + {INSERT t2 0 X. {} {i 1 i 2}} + {INSERT t2 0 X. {} {i 2 i 123}} + } + 1.2 { + UPDATE t2 SET c=456 WHERE a=1 + } { + {UPDATE t2 0 X. {i 1 i 2} {{} {} i 456}} + } + + 1.3 { + DELETE FROM t2 WHERE a=2 + } { + {DELETE t2 0 X. {i 2 i 123} {}} + } + + 1.4 { + UPDATE t2 SET a=15 + } { + {INSERT t2 0 X. {} {i 15 i 456}} + {DELETE t2 0 X. {i 1 i 456} {}} + } + + 2.1 { + INSERT INTO t3 VALUES(5, 6, 7); + INSERT INTO t3 VALUES(8, 9, 10); + } { + {INSERT t3 0 .XX {} {i 8 i 9 i 10}} + {INSERT t3 0 .XX {} {i 5 i 6 i 7}} + } + + 2.2 { + UPDATE t3 SET a = 505 WHERE (c, e) = (6, 7); + } { + {UPDATE t3 0 .XX {i 5 i 6 i 7} {i 505 {} {} {} {}}} + } + + 2.3 { + DELETE FROM t3 WHERE (c, e) = (9, 10); + } { + {DELETE t3 0 .XX {i 8 i 9 i 10} {}} + } + + 2.4 { + UPDATE t3 SET c=1000 + } { + {DELETE t3 0 .XX {i 505 i 6 i 7} {}} + {INSERT t3 0 .XX {} {i 505 i 1000 i 7}} + } + + 3.1 { + INSERT INTO t4 VALUES(100, 100); + } { + {INSERT t4 0 X. {} {i 100 i 100}} + } + +} { + do_test 1.$tn.1 { + sqlite3session S db main + S object_config rowid 1 + S attach * + execsql $sql + } {} + + do_changeset_test 1.$tn.2 S $changeset + + S delete +} +#------------------------------------------------------------------------- +reset_db + +forcedelete test.db2 +sqlite3 db2 test.db2 + +do_common_sql { + CREATE TABLE t0(x INTEGER PRIMARY KEY, y); + INSERT INTO t0 VALUES(1, 'one'); + INSERT INTO t0 VALUES(2, 'two'); + + CREATE TABLE t1(a AS (c*10) %TYPE%, b INTEGER PRIMARY KEY, c); + INSERT INTO t1 VALUES(1, 5); + INSERT INTO t1 VALUES(2, 10); + INSERT INTO t1 VALUES(3, 5); + + CREATE TABLE t2( + a, b, c AS (a*b) %TYPE%, + 'k 1', 'k 2', PRIMARY KEY('k 1', 'k 2') + ) WITHOUT ROWID; + INSERT INTO t2 VALUES('a', 'b', 1, 11); + INSERT INTO t2 VALUES('A', 'B', 2, 22); + INSERT INTO t2 VALUES('Aa', 'Bb', 3, 33); +} + +foreach {tn sql} { + 1.1 { INSERT INTO t0 VALUES(4, 15) } + 1.2 { INSERT INTO t1 VALUES(4, 15) } + 1.3 { INSERT INTO t2 VALUES(1, 2, 3, 4) } + + 2.1 { UPDATE t1 SET c=100 WHERE b=2 } + 2.2 { UPDATE t2 SET a=11 } + + 3.1 { DELETE FROM t2 WHERE (t2.'k 1') = 2 } + 3.2 { DELETE FROM t1 } +} { + do_test 2.$tn.1 { + # execsql { PRAGMA vdbe_listing = 1 } db2 + do_then_apply_sql $sql + } {} + do_test 2.$tn.2 { + compare_db db db2 + } {} +} +db2 close + +}]} + + + + +finish_test diff --git a/ext/session/session_speed_test.c b/ext/session/session_speed_test.c new file mode 100644 index 0000000000..405001c694 --- /dev/null +++ b/ext/session/session_speed_test.c @@ -0,0 +1,358 @@ +/* +** 2017 January 31 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains the source code for a standalone program used to +** test the performance of the sessions module. Compile and run: +** +** ./session_speed_test -help +** +** for details. +*/ + +#include "sqlite3.h" +#include +#include +#include +#include +#include + +/************************************************************************* +** Start of generic command line parser. +*/ +#define CMDLINE_BARE 0 +#define CMDLINE_INTEGER 1 +#define CMDLINE_STRING 2 +#define CMDLINE_BOOLEAN 3 + +typedef struct CmdLineOption CmdLineOption; +struct CmdLineOption { + const char *zText; /* Name of command line option */ + const char *zHelp; /* Help text for option */ + int eType; /* One of the CMDLINE_* values */ + int iOff; /* Offset of output variable */ +}; + +#define CMDLINE_INT32(x,y,z) {x, y, CMDLINE_INTEGER, z} +#define CMDLINE_BOOL(x,y,z) {x, y, CMDLINE_BOOLEAN, z} +#define CMDLINE_TEXT(x,y,z) {x, y, CMDLINE_STRING, z} +#define CMDLINE_NONE(x,y,z) {x, y, CMDLINE_BARE, z} + +static void option_requires_argument_error(CmdLineOption *pOpt){ + fprintf(stderr, "Option requires a%s argument: %s\n", + pOpt->eType==CMDLINE_INTEGER ? "n integer" : + pOpt->eType==CMDLINE_STRING ? " string" : " boolean", + pOpt->zText + ); + exit(1); +} + +static void ambiguous_option_error(const char *zArg){ + fprintf(stderr, "Option is ambiguous: %s\n", zArg); + exit(1); +} + +static void unknown_option_error( + const char *zArg, + CmdLineOption *aOpt, + const char *zHelp +){ + int i; + fprintf(stderr, "Unknown option: %s\n", zArg); + fprintf(stderr, "\nOptions are:\n"); + fprintf(stderr, " % -30sEcho command line options\n", "-cmdline:verbose"); + for(i=0; aOpt[i].zText; i++){ + int eType = aOpt[i].eType; + char *zOpt = sqlite3_mprintf("%s %s", aOpt[i].zText, + eType==CMDLINE_BARE ? "" : + eType==CMDLINE_INTEGER ? "N" : + eType==CMDLINE_BOOLEAN ? "BOOLEAN" : "TEXT" + ); + fprintf(stderr, " % -30s%s\n", zOpt, aOpt[i].zHelp); + sqlite3_free(zOpt); + } + if( zHelp ){ + fprintf(stderr, "\n%s\n", zHelp); + } + exit(1); +} + +static int get_integer_option(CmdLineOption *pOpt, const char *zArg){ + int i = 0; + int iRet = 0; + int bSign = 1; + if( zArg[0]=='-' ){ + bSign = -1; + i = 1; + } + while( zArg[i] ){ + if( zArg[i]<'0' || zArg[i]>'9' ) option_requires_argument_error(pOpt); + iRet = iRet*10 + (zArg[i] - '0'); + i++; + } + return (iRet*bSign); +} + +static int get_boolean_option(CmdLineOption *pOpt, const char *zArg){ + if( 0==sqlite3_stricmp(zArg, "true") ) return 1; + if( 0==sqlite3_stricmp(zArg, "1") ) return 1; + if( 0==sqlite3_stricmp(zArg, "0") ) return 0; + if( 0==sqlite3_stricmp(zArg, "false") ) return 0; + option_requires_argument_error(pOpt); + return 0; +} + +static void parse_command_line( + int argc, + char **argv, + int iStart, + CmdLineOption *aOpt, + void *pStruct, + const char *zHelp +){ + char *pOut = (char*)pStruct; + int bVerbose = 0; + int iArg; + + for(iArg=iStart; iArgzText, zArg, nArg) ){ + if( nMatch ){ + ambiguous_option_error(zArg); + } + nMatch++; + if( pOpt->eType==CMDLINE_BARE ){ + *(int*)(&pOut[pOpt->iOff]) = 1; + }else{ + iArg++; + if( iArg==argc ){ + option_requires_argument_error(pOpt); + } + switch( pOpt->eType ){ + case CMDLINE_INTEGER: + *(int*)(&pOut[pOpt->iOff]) = get_integer_option(pOpt, argv[iArg]); + break; + case CMDLINE_STRING: + *(const char**)(&pOut[pOpt->iOff]) = argv[iArg]; + break; + case CMDLINE_BOOLEAN: + *(int*)(&pOut[pOpt->iOff]) = get_boolean_option(pOpt, argv[iArg]); + break; + } + } + } + } + + if( nMatch==0 && 0==sqlite3_strnicmp("-cmdline:verbose", zArg, nArg) ){ + bVerbose = 1; + nMatch = 1; + } + + if( nMatch==0 ){ + unknown_option_error(zArg, aOpt, zHelp); + } + } + + if( bVerbose ){ + int iOpt; + fprintf(stdout, "Options are: "); + for(iOpt=0; aOpt[iOpt].zText; iOpt++){ + CmdLineOption *pOpt = &aOpt[iOpt]; + if( pOpt->eType!=CMDLINE_BARE || *(int*)(&pOut[pOpt->iOff]) ){ + fprintf(stdout, "%s ", pOpt->zText); + } + switch( pOpt->eType ){ + case CMDLINE_INTEGER: + fprintf(stdout, "%d ", *(int*)(&pOut[pOpt->iOff])); + break; + case CMDLINE_BOOLEAN: + fprintf(stdout, "%d ", *(int*)(&pOut[pOpt->iOff])); + break; + case CMDLINE_STRING: + fprintf(stdout, "%s ", *(const char**)(&pOut[pOpt->iOff])); + break; + } + } + fprintf(stdout, "\n"); + } +} +/* +** End of generic command line parser. +*************************************************************************/ + +static void abort_due_to_error(int rc){ + fprintf(stderr, "Error: %d\n"); + exit(-1); +} + +static void execsql(sqlite3 *db, const char *zSql){ + int rc = sqlite3_exec(db, zSql, 0, 0, 0); + if( rc!=SQLITE_OK ) abort_due_to_error(rc); +} + +static int xConflict(void *pCtx, int eConflict, sqlite3_changeset_iter *p){ + return SQLITE_CHANGESET_ABORT; +} + +static void run_test( + sqlite3 *db, + sqlite3 *db2, + int nRow, + const char *zSql +){ + sqlite3_session *pSession = 0; + sqlite3_stmt *pStmt = 0; + int rc; + int i; + int nChangeset; + void *pChangeset; + + /* Attach a session object to database db */ + rc = sqlite3session_create(db, "main", &pSession); + if( rc!=SQLITE_OK ) abort_due_to_error(rc); + + /* Configure the session to capture changes on all tables */ + rc = sqlite3session_attach(pSession, 0); + if( rc!=SQLITE_OK ) abort_due_to_error(rc); + + /* Prepare the SQL statement */ + rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0); + if( rc!=SQLITE_OK ) abort_due_to_error(rc); + + /* Open a transaction */ + execsql(db, "BEGIN"); + + /* Execute the SQL statement nRow times */ + for(i=0; i $mem1} + } $eRes +} + +do_test 1.3 { + S delete +} {} + +finish_test diff --git a/ext/session/sessionnoact.test b/ext/session/sessionnoact.test new file mode 100644 index 0000000000..54e9a62151 --- /dev/null +++ b/ext/session/sessionnoact.test @@ -0,0 +1,232 @@ +# 2023 October 20 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. +# + +if {![info exists testdir]} { + set testdir [file join [file dirname [info script]] .. .. test] +} +source [file join [file dirname [info script]] session_common.tcl] +source $testdir/tester.tcl +ifcapable !session {finish_test; return} + +set testprefix sessionnoact + +do_execsql_test 1.0 { + CREATE TABLE p1(a INTEGER PRIMARY KEY, b, c UNIQUE); + INSERT INTO p1 VALUES(1, 1, 'one'); + INSERT INTO p1 VALUES(2, 2, 'two'); + INSERT INTO p1 VALUES(3, 3, 'three'); + INSERT INTO p1 VALUES(4, 4, 'four'); +} + +db_save + +set C [changeset_from_sql { + DELETE FROM p1 WHERE a=2; + UPDATE p1 SET c='six' WHERE a=3; + INSERT INTO p1 VALUES(5, 5, 'two'); + INSERT INTO p1 VALUES(6, 6, 'three'); +}] + +db_restore_and_reopen + +do_execsql_test 1.1 { + CREATE TABLE c1(x INTEGER PRIMARY KEY, y, + FOREIGN KEY(y) REFERENCES p1(c) ON DELETE CASCADE ON UPDATE SET NULL + ); + + INSERT INTO c1 VALUES(10, 'one'); + INSERT INTO c1 VALUES(20, 'two'); + INSERT INTO c1 VALUES(30, 'three'); + INSERT INTO c1 VALUES(40, 'four'); +} + +db_save + +do_execsql_test 1.2 { + PRAGMA foreign_keys = 1; +} + +set ::nConflict 0 +proc conflict {args} { + incr ::nConflict + return "ABORT" +} + +sqlite3changeset_apply_v2 db $C conflict + +do_execsql_test 1.3 { + SELECT * FROM c1 +} { + 10 one + 30 {} + 40 four +} + +db_restore_and_reopen + +do_execsql_test 1.4 { + PRAGMA foreign_keys = 1; +} + +do_execsql_test 1.5 { + UPDATE p1 SET c=12345 WHERE a = 45; +} + +sqlite3changeset_apply_v2 -noaction db $C conflict +do_execsql_test 1.6 { + SELECT * FROM c1 +} { + 10 one + 20 two + 30 three + 40 four +} + +do_execsql_test 1.7 { + PRAGMA foreign_keys = 1; + UPDATE p1 SET c = 'ten' WHERE c='two'; + SELECT * FROM c1; +} { + 10 one + 20 {} + 30 three + 40 four +} + +do_execsql_test 1.8 { + PRAGMA foreign_key_check +} + +#------------------------------------------------------------------------- +# Check that a changeset that causes an FK violation may not be applied, +# even if SQLITE_CHANGESETAPPLY_FKNOACTION is specified. +# +# UPDATE: Unless the conflict-handler returns OMIT. In that case it can +# be committed. See test cases 3.* in this file. +# +reset_db +do_execsql_test 2.0 { + CREATE TABLE p1(a INTEGER PRIMARY KEY, b, c UNIQUE); + INSERT INTO p1 VALUES(1, 1, 'one'); + INSERT INTO p1 VALUES(2, 2, 'two'); + + CREATE TABLE c1(x REFERENCES p1(c) ON DELETE CASCADE); + INSERT INTO c1 VALUES('two'); +} + +db_save + +set C [changeset_from_sql { + DELETE FROM p1 WHERE a=2; +}] + +db_restore_and_reopen + +do_test 2.1 { + sqlite3changeset_apply_v2 -noaction db $C conflict +} {} +do_execsql_test 2.2 { + SELECT * FROM p1 +} {1 1 one} + +db_restore_and_reopen +db eval { PRAGMA foreign_keys = 1 } + +do_test 2.3 { + list [catch { sqlite3changeset_apply_v2 -noaction db $C conflict } msg] $msg +} {1 SQLITE_CONSTRAINT} +do_execsql_test 2.4 { + SELECT * FROM p1; +} {1 1 one 2 2 two} +do_execsql_test 2.5 { + SELECT * FROM c1; +} {two} + +db_restore_and_reopen +db eval { PRAGMA foreign_keys = 1 } + +do_test 2.6 { + list [catch { + sqlite3changeset_apply_v2 -ignorenoop -noaction db $C conflict + } msg] $msg +} {1 SQLITE_CONSTRAINT} +do_execsql_test 2.7 { + SELECT * FROM p1; +} {1 1 one 2 2 two} +do_execsql_test 2.8 { + SELECT * FROM c1; +} {two} + +#------------------------------------------------------------------------- +# +reset_db +do_execsql_test 3.0 { + CREATE TABLE p1(a INTEGER PRIMARY KEY, b, c UNIQUE); + INSERT INTO p1 VALUES(1, 1, 'one'); + INSERT INTO p1 VALUES(2, 2, 'two'); + + CREATE TABLE c1(x REFERENCES p1(c) ON DELETE CASCADE); + INSERT INTO c1 VALUES('two'); +} + +set ::nConflict 0 +proc conflict {args} { + incr ::nConflict + return "OMIT" +} + +db_save + +set C [changeset_from_sql { + DELETE FROM p1 WHERE a=2; +}] + +db_restore_and_reopen + +do_test 3.1 { + sqlite3changeset_apply_v2 -noaction db $C conflict +} {} +do_execsql_test 3.2 { + SELECT * FROM p1 +} {1 1 one} + +db_restore_and_reopen +db eval { PRAGMA foreign_keys = 1 } + +do_test 3.3 { + list [catch { sqlite3changeset_apply_v2 -noaction db $C conflict } msg] $msg +} {0 {}} +do_execsql_test 3.4 { + SELECT * FROM p1; +} {1 1 one} +do_execsql_test 3.5 { + SELECT * FROM c1; +} {two} + +db_restore_and_reopen +db eval { PRAGMA foreign_keys = 1 } + +do_test 3.6 { + list [catch { + sqlite3changeset_apply_v2 -ignorenoop -noaction db $C conflict + } msg] $msg +} {0 {}} +do_execsql_test 3.7 { + SELECT * FROM p1; +} {1 1 one} +do_execsql_test 3.8 { + SELECT * FROM c1; +} {two} + +finish_test + diff --git a/ext/session/sessionnoop.test b/ext/session/sessionnoop.test new file mode 100644 index 0000000000..16c60b7abf --- /dev/null +++ b/ext/session/sessionnoop.test @@ -0,0 +1,187 @@ +# 2021 Februar 20 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. +# + +if {![info exists testdir]} { + set testdir [file join [file dirname [info script]] .. .. test] +} +source [file join [file dirname [info script]] session_common.tcl] +source $testdir/tester.tcl +ifcapable !session {finish_test; return} + +set testprefix sessionnoop + +#------------------------------------------------------------------------- +# Test plan: +# +# 1.*: Test that concatenating changesets cannot produce a noop UPDATE. +# 2.*: Test that rebasing changesets cannot produce a noop UPDATE. +# 3.*: Test that sqlite3changeset_apply() ignores noop UPDATE changes. +# + +do_execsql_test 1.0 { + CREATE TABLE t1(a PRIMARY KEY, b, c, d); + INSERT INTO t1 VALUES(1, 1, 1, 1); + INSERT INTO t1 VALUES(2, 2, 2, 2); + INSERT INTO t1 VALUES(3, 3, 3, 3); +} + +proc do_concat_test {tn sql1 sql2 res} { + uplevel [list do_test $tn [subst -nocommands { + set C1 [changeset_from_sql {$sql1}] + set C2 [changeset_from_sql {$sql2}] + set C3 [sqlite3changeset_concat [set C1] [set C2]] + set got [list] + sqlite3session_foreach elem [set C3] { lappend got [set elem] } + set got + }] [list {*}$res]] +} + +do_concat_test 1.1 { + UPDATE t1 SET c=c+1; +} { + UPDATE t1 SET c=c-1; +} { +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 2.0 { + CREATE TABLE t1(a PRIMARY KEY, b, c); + INSERT INTO t1 VALUES(1, 1, 1); + INSERT INTO t1 VALUES(2, 2, 2); + INSERT INTO t1 VALUES(3, 3, 3); +} + +proc do_rebase_test {tn sql_local sql_remote conflict_res expected} { + proc xConflict {args} [list return $conflict_res] + + uplevel [list \ + do_test $tn [subst -nocommands { + execsql BEGIN + set c_remote [changeset_from_sql {$sql_remote}] + execsql ROLLBACK + + execsql BEGIN + set c_local [changeset_from_sql {$sql_local}] + set base [sqlite3changeset_apply_v2 db [set c_remote] xConflict] + execsql ROLLBACK + + sqlite3rebaser_create R + R config [set base] + set res [list] + sqlite3session_foreach elem [R rebase [set c_local]] { + lappend res [set elem] + } + R delete + set res + }] [list {*}$expected] + ] +} + +do_rebase_test 2.1 { + UPDATE t1 SET c=2 WHERE a=1; -- local +} { + UPDATE t1 SET c=3 WHERE a=1; -- remote +} OMIT { + {UPDATE t1 0 X.. {i 1 {} {} i 3} {{} {} {} {} i 2}} +} + +do_rebase_test 2.2 { + UPDATE t1 SET c=2 WHERE a=1; -- local +} { + UPDATE t1 SET c=3 WHERE a=1; -- remote +} REPLACE { +} + +do_rebase_test 2.3.1 { + UPDATE t1 SET c=4 WHERE a=1; -- local +} { + UPDATE t1 SET c=4 WHERE a=1 -- remote +} OMIT { + {UPDATE t1 0 X.. {i 1 {} {} i 4} {{} {} {} {} i 4}} +} + +do_rebase_test 2.3.2 { + UPDATE t1 SET c=5 WHERE a=1; -- local +} { + UPDATE t1 SET c=5 WHERE a=1 -- remote +} REPLACE { +} + +#------------------------------------------------------------------------- +# +reset_db +do_execsql_test 3.0 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c); + INSERT INTO t1 VALUES(1, 1, 1); + INSERT INTO t1 VALUES(2, 2, 2); + INSERT INTO t1 VALUES(3, 3, 3); + INSERT INTO t1 VALUES(4, 4, 4); +} + +# Arg $pkstr contains one character for each column in the table. An +# "X" for PK column, or a "." for a non-PK. +# +proc mk_tbl_header {name pkstr} { + set ret [binary format H2c 54 [string length $pkstr]] + foreach i [split $pkstr {}] { + if {$i=="X"} { + append ret [binary format H2 01] + } else { + if {$i!="."} {error "bad pkstr: $pkstr ($i)"} + append ret [binary format H2 00] + } + } + append ret $name + append ret [binary format H2 00] + set ret +} + +proc mk_update_change {args} { + set ret [binary format H2H2 17 00] + foreach a $args { + if {$a==""} { + append ret [binary format H2 00] + } else { + append ret [binary format H2W 01 $a] + } + } + set ret +} + +proc xConflict {args} { return "ABORT" } +do_test 3.1 { + set C [mk_tbl_header t1 X..] + append C [mk_update_change 1 {} 1 {} {} 500] + append C [mk_update_change 2 {} {} {} {} {}] + append C [mk_update_change 3 3 {} {} 600 {}] + append C [mk_update_change 4 {} {} {} {} {}] + + sqlite3changeset_apply_v2 db $C xConflict +} {} +do_execsql_test 3.2 { + SELECT * FROM t1 +} { + 1 1 500 + 2 2 2 + 3 600 3 + 4 4 4 +} + + + + + + +finish_test + diff --git a/ext/session/sessionnoop2.test b/ext/session/sessionnoop2.test new file mode 100644 index 0000000000..30ec2e8cdc --- /dev/null +++ b/ext/session/sessionnoop2.test @@ -0,0 +1,324 @@ +# 2011 March 07 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. +# + +if {![info exists testdir]} { + set testdir [file join [file dirname [info script]] .. .. test] +} +source [file join [file dirname [info script]] session_common.tcl] +source $testdir/tester.tcl +ifcapable !session {finish_test; return} + +set testprefix sessionnoop2 + +foreach {tn wo} { + 1 "" + 2 " WITHOUT ROWID " +} { + reset_db + eval [string map [list %WO% $wo] { +do_execsql_test $tn.1.0 { + CREATE TABLE t1(a PRIMARY KEY, b, c) %WO%; + INSERT INTO t1 VALUES('a', 'A', 'AAA'); + INSERT INTO t1 VALUES('b', 'B', 'BBB'); + INSERT INTO t1 VALUES('c', 'C', 'CCC'); + INSERT INTO t1 VALUES('d', 'D', 'DDD'); + INSERT INTO t1 VALUES('e', 'E', 'EEE'); +} + +forcedelete test.db2 +sqlite3 db2 test.db2 + +do_execsql_test -db db2 $tn.1.1 { + CREATE TABLE t1(a PRIMARY KEY, b, c) %WO%; + INSERT INTO t1 VALUES('a', 'A', 'AAA'); + INSERT INTO t1 VALUES('b', 'B', '123'); + INSERT INTO t1 VALUES('c', 'C', 'CCC'); + INSERT INTO t1 VALUES('e', 'E', 'EEE'); + INSERT INTO t1 VALUES('f', 'F', 'FFF'); +} + +set C [changeset_from_sql { + UPDATE t1 SET c='123' WHERE a='b'; + DELETE FROM t1 WHERE a='d'; + INSERT INTO t1 VALUES('f', 'F', 'FFF'); +}] + + +set ::conflict_list [list] +proc xConflict {args} { + lappend ::conflict_list $args + return "OMIT" +} +do_test $tn.1.2 { + sqlite3changeset_apply_v2 db2 $C xConflict + set ::conflict_list +} [list {*}{ + {UPDATE t1 DATA {t b {} {} t BBB} {{} {} {} {} t 123} {t b t B t 123}} + {INSERT t1 CONFLICT {t f t F t FFF} {t f t F t FFF}} + {DELETE t1 NOTFOUND {t d t D t DDD}} +}] +do_test $tn.1.3 { + set ::conflict_list [list] + sqlite3changeset_apply_v2 db2 $C xConflict + set ::conflict_list +} [list {*}{ + {UPDATE t1 DATA {t b {} {} t BBB} {{} {} {} {} t 123} {t b t B t 123}} + {INSERT t1 CONFLICT {t f t F t FFF} {t f t F t FFF}} + {DELETE t1 NOTFOUND {t d t D t DDD}} +}] + +do_test $tn.1.4 { + set ::conflict_list [list] + sqlite3changeset_apply_v2 -ignorenoop db2 $C xConflict + set ::conflict_list +} {} + +do_execsql_test -db db2 1.5 { + UPDATE t1 SET b='G' WHERE a='f'; + UPDATE t1 SET c='456' WHERE a='b'; +} + +do_test $tn.1.6 { + set ::conflict_list [list] + sqlite3changeset_apply_v2 -ignorenoop db2 $C xConflict + set ::conflict_list +} [list {*}{ + {UPDATE t1 DATA {t b {} {} t BBB} {{} {} {} {} t 123} {t b t B t 456}} + {INSERT t1 CONFLICT {t f t F t FFF} {t f t G t FFF}} +}] + +db2 close + +#-------------------------------------------------------------------------- + +reset_db +forcedelete test.db2 +sqlite3 db2 test.db2 +do_execsql_test $tn.2.0 { + CREATE TABLE t1(a PRIMARY KEY, b) %WO%; +} +do_execsql_test -db db2 $tn.2.1 { + CREATE TABLE t1(a PRIMARY KEY, b, c DEFAULT 'val') %WO%; +} + +do_test $tn.2.2 { + do_then_apply_sql -ignorenoop { + INSERT INTO t1 VALUES(1, 2); + } + do_then_apply_sql -ignorenoop { + UPDATE t1 SET b=2 WHERE a=1 + } +} {} + +db2 close + +}] +} + + +#------------------------------------------------------------------------- +reset_db +forcedelete test.db2 +do_execsql_test 3.0 { + CREATE TABLE xyz(a, b, c, PRIMARY KEY(a, b), UNIQUE(c)); + ANALYZE; + WITH s(i) AS ( + VALUES(1) UNION ALL SELECT i+1 FROM s WHERE i<100 + ) + INSERT INTO xyz SELECT i, i, i FROM s; + VACUUM INTO 'test.db2'; +} + +set C [changeset_from_sql { ANALYZE }] +sqlite3 db2 test.db2 + +set ::conflict_list [list] +proc xConflict {args} { lappend ::conflict_list $args ; return "OMIT" } +do_test 3.1 { + sqlite3changeset_apply_v2 db2 $C xConflict + set ::conflict_list +} {} + +do_test 3.2 { + sqlite3changeset_apply_v2 -ignorenoop db2 $C xConflict + set ::conflict_list +} {} + +do_test 3.3 { + sqlite3changeset_apply_v2 db2 $C xConflict + set ::conflict_list +} [list {*}{ + {INSERT sqlite_stat1 CONFLICT {t xyz t sqlite_autoindex_xyz_1 t {100 1 1}} {t xyz t sqlite_autoindex_xyz_1 t {100 1 1}}} + {INSERT sqlite_stat1 CONFLICT {t xyz t sqlite_autoindex_xyz_2 t {100 1}} {t xyz t sqlite_autoindex_xyz_2 t {100 1}}} +}] + +do_execsql_test -db db2 3.4 { + UPDATE sqlite_stat1 SET stat='200 1 1' WHERE idx='sqlite_autoindex_xyz_1'; +} + +do_test 3.5 { + set ::conflict_list [list] + sqlite3changeset_apply_v2 -ignorenoop db2 $C xConflict + set ::conflict_list +} [list {*}{ + {INSERT sqlite_stat1 CONFLICT {t xyz t sqlite_autoindex_xyz_1 t {100 1 1}} {t xyz t sqlite_autoindex_xyz_1 t {200 1 1}}} +}] + +#------------------------------------------------------------------------- +# Test that the conflict-handler is invoked for conflicts caused by +# DELETE ops even if SQLITE_CHANGESETAPPLY_IGNORENOOP is specified. +# +set ::conflict_list [list] +proc xConflict {args} { + lappend ::conflict_list $args + return "OMIT" +} + +foreach {tn wo} { + 1 "" + 2 " WITHOUT ROWID " +} { + reset_db + catch { db2 close } + forcedelete test.db2 + do_execsql_test 4.$tn.0 " + CREATE TABLE x1(a INTEGER PRIMARY KEY, b, c) $wo; + INSERT INTO x1 VALUES(1, 'one', 'i'); + INSERT INTO x1 VALUES(2, 'two', 'ii'); + INSERT INTO x1 VALUES(3, 'three', 'iii'); + VACUUM INTO 'test.db2'; + " + + set C [changeset_from_sql { + DELETE FROM x1 WHERE a=2; + }] + + sqlite3 db2 test.db2 + do_execsql_test -db db2 4.$tn.1 { + UPDATE x1 SET b='four hundred' WHERE a=2; + } + + do_test 4.$tn.2.1 { + set ::conflict_list [list] + sqlite3changeset_apply_v2 db2 $C xConflict + set ::conflict_list + } [list {*}{ + {DELETE x1 DATA {i 2 t two t ii} {i 2 t {four hundred} t ii}} + }] + + do_test 4.$tn.2.2 { + set ::conflict_list [list] + sqlite3changeset_apply_v2 -ignorenoop db2 $C xConflict + set ::conflict_list + } [list {*}{ + {DELETE x1 DATA {i 2 t two t ii} {i 2 t {four hundred} t ii}} + }] +} + +foreach {tn wo} { + 1 "" + 2 " WITHOUT ROWID " +} { + reset_db + catch { db2 close } + forcedelete test.db2 + do_execsql_test 5.$tn.0 " + PRAGMA foreign_keys = ON; + CREATE TABLE p1(a INTEGER PRIMARY KEY, b, c) $wo; + CREATE TABLE c1(a INTEGER PRIMARY KEY REFERENCES p1, b); + INSERT INTO p1 VALUES(1, 'one', 'i'); + INSERT INTO p1 VALUES(2, 'two', 'ii'); + INSERT INTO p1 VALUES(3, 'three', 'iii'); + VACUUM INTO 'test.db2'; + " + + set C [changeset_from_sql { + DELETE FROM p1 WHERE a=2; + }] + + sqlite3 db2 test.db2 + do_execsql_test -db db2 5.$tn.1 { + PRAGMA foreign_keys = ON; + INSERT INTO c1 VALUES(2, 'xyz'); + } + + do_test 5.$tn.2.1 { + set ::conflict_list [list] + sqlite3changeset_apply_v2 db2 $C xConflict + set ::conflict_list + } [list {*}{ + {FOREIGN_KEY 1} + }] + + # Reinsert the row deleted by test case 5.$tn.2.1 + execsql { INSERT INTO p1 VALUES(2, 'two', 'ii') } db2 + + do_test 5.$tn.2.2 { + set ::conflict_list [list] + sqlite3changeset_apply_v2 -ignorenoop db2 $C xConflict + set ::conflict_list + } [list {*}{ + {FOREIGN_KEY 1} + }] +} + +foreach {tn wo} { + 1 "" + 2 " WITHOUT ROWID " +} { + reset_db + catch { db2 close } + forcedelete test.db2 + do_execsql_test 6.$tn.0 " + CREATE TABLE x1(a INTEGER PRIMARY KEY, b, c) $wo; + CREATE TABLE x2(x UNIQUE); + + INSERT INTO x1 VALUES(1, 'one', 'i'); + INSERT INTO x1 VALUES(2, 'two', 'ii'); + INSERT INTO x1 VALUES(3, 'three', 'iii'); + VACUUM INTO 'test.db2'; + " + + set C [changeset_from_sql { + DELETE FROM x1 WHERE a=2; + }] + + sqlite3 db2 test.db2 + do_execsql_test -db db2 6.$tn.1 { + INSERT INTO x2 VALUES(0); + CREATE TRIGGER tr1 AFTER DELETE ON x1 BEGIN + INSERT INTO x2 VALUES(0); + END; + } + + do_test 6.$tn.2.1 { + set ::conflict_list [list] + sqlite3changeset_apply_v2 db2 $C xConflict + set ::conflict_list + } [list {*}{ + {DELETE x1 CONSTRAINT {i 2 t two t ii}} + }] + +breakpoint + do_test 6.$tn.2.2 { + set ::conflict_list [list] + sqlite3changeset_apply_v2 -ignorenoop db2 $C xConflict + set ::conflict_list + } [list {*}{ + {DELETE x1 CONSTRAINT {i 2 t two t ii}} + }] +} + + +finish_test + diff --git a/ext/session/sessionrebase.test b/ext/session/sessionrebase.test new file mode 100644 index 0000000000..033348f9ce --- /dev/null +++ b/ext/session/sessionrebase.test @@ -0,0 +1,552 @@ +# 2018 March 14 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. +# + +if {![info exists testdir]} { + set testdir [file join [file dirname [info script]] .. .. test] +} +source [file join [file dirname [info script]] session_common.tcl] +source $testdir/tester.tcl +ifcapable !session {finish_test; return} + +set testprefix sessionrebase + +set ::lConflict [list] +proc xConflict {args} { + set res [lindex $::lConflict 0] + set ::lConflict [lrange $::lConflict 1 end] + return $res +} + +#------------------------------------------------------------------------- +# The following test cases - 1.* - test that the rebase blobs output by +# sqlite3_changeset_apply_v2 look correct in some simple cases. The blob +# is itself a changeset, containing records determined as follows: +# +# * For each conflict resolved with REPLACE, the rebase blob contains +# a DELETE record. All fields other than the PK fields are undefined. +# +# * For each conflict resolved with OMIT, the rebase blob contains an +# INSERT record. For an INSERT or UPDATE operation, the indirect flag +# is clear and all updated fields are defined. For a DELETE operation, +# the indirect flag is set and all non-PK fields left undefined. +# +proc do_apply_v2_test {tn sql modsql conflict_handler res} { + + execsql BEGIN + sqlite3session S db main + S attach * + execsql $sql + set changeset [S changeset] + S delete + execsql ROLLBACK + + execsql BEGIN + execsql $modsql + set ::lConflict $conflict_handler + set blob [sqlite3changeset_apply_v2 db $changeset xConflict] + execsql ROLLBACK + + uplevel [list do_test $tn [list changeset_to_list $blob] [list {*}$res]] +} + + +set ::lConflict [list] +proc xConflict {args} { + set res [lindex $::lConflict 0] + set ::lConflict [lrange $::lConflict 1 end] + return $res +} + +# Take a copy of database test.db in file test.db2. Execute $sql1 +# against test.db and $sql2 against test.db2. Capture a changeset +# for each. Then send the test.db2 changeset to test.db and apply +# it with the conflict handlers in $conflict_handler. Patch the +# test.db changeset and then execute it against test.db2. Test that +# the two databases come out the same. +# +proc do_rebase_test {tn sql1 sql2 conflict_handler {testsql ""} {testres ""}} { + + for {set i 1} {$i <= 2} {incr i} { + forcedelete test.db2 test.db2-journal test.db2-wal + forcecopy test.db test.db2 + sqlite3 db2 test.db2 + + db eval BEGIN + + sqlite3session S1 db main + S1 object_config rowid 1 + S1 attach * + execsql $sql1 db + set c1 [S1 changeset] + S1 delete + + if {$i==1} { + sqlite3session S2 db2 main + S2 object_config rowid 1 + S2 attach * + execsql $sql2 db2 + set c2 [S2 changeset] + S2 delete + } else { + set c2 [list] + foreach sql [split $sql2 ";"] { + if {[string is space $sql]} continue + sqlite3session S2 db2 main + S2 object_config rowid 1 + S2 attach * + execsql $sql db2 + lappend c2 [S2 changeset] + S2 delete + } + } + + set ::lConflict $conflict_handler + set rebase [list] + if {$i==1} { + lappend rebase [sqlite3changeset_apply_v2 db $c2 xConflict] + } else { + foreach c $c2 { +#puts "apply_v2: [changeset_to_list $c]" + lappend rebase [sqlite3changeset_apply_v2 db $c xConflict] + } + #puts "llength: [llength $rebase]" + } + #if {$tn=="2.1.4"} { puts [changeset_to_list $rebase] ; breakpoint } + #puts [changeset_to_list [lindex $rebase 0]] ; breakpoint + #puts [llength $rebase] + + sqlite3rebaser_create R + foreach r $rebase { +#puts [changeset_to_list $r] + R configure $r + } + set c1r [R rebase $c1] + R delete + #if {$tn=="2.1.4"} { puts [changeset_to_list $c1r] } + + sqlite3changeset_apply_v2 db2 $c1r xConflictAbort + + if {[string range $tn end end]!="*"} { + uplevel [list do_test $tn.$i.1 [list compare_db db db2] {}] + } + db2 close + + if {$testsql!=""} { + uplevel [list do_execsql_test $tn.$i.2 $testsql $testres] + } + + db eval ROLLBACK + } +} + +do_execsql_test 1.0 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b); + INSERT INTO t1 VALUES(1, 'value A'); +} + +do_apply_v2_test 1.1.1 { + UPDATE t1 SET b = 'value B' WHERE a=1; +} { + UPDATE t1 SET b = 'value C' WHERE a=1; +} { + OMIT +} { + {INSERT t1 0 X. {} {i 1 t {value B}}} +} + +do_apply_v2_test 1.1.2 { + UPDATE t1 SET b = 'value B' WHERE a=1; +} { + UPDATE t1 SET b = 'value C' WHERE a=1; +} { + REPLACE +} { + {INSERT t1 1 X. {} {i 1 t {value B}}} +} + +do_apply_v2_test 1.2.1 { + INSERT INTO t1 VALUES(2, 'first'); +} { + INSERT INTO t1 VALUES(2, 'second'); +} { + OMIT +} { + {INSERT t1 0 X. {} {i 2 t first}} +} +do_apply_v2_test 1.2.2 { + INSERT INTO t1 VALUES(2, 'first'); +} { + INSERT INTO t1 VALUES(2, 'second'); +} { + REPLACE +} { + {INSERT t1 1 X. {} {i 2 t first}} +} + +do_apply_v2_test 1.3.1 { + DELETE FROM t1 WHERE a=1; +} { + UPDATE t1 SET b='value D' WHERE a=1; +} { + OMIT +} { + {DELETE t1 0 X. {i 1 t {value A}} {}} +} +do_apply_v2_test 1.3.2 { + DELETE FROM t1 WHERE a=1; +} { + UPDATE t1 SET b='value D' WHERE a=1; +} { + REPLACE +} { + {DELETE t1 1 X. {i 1 t {value A}} {}} +} + +#------------------------------------------------------------------------- +# Test cases 2.* - simple tests of rebasing actual changesets. +# +# 2.1.1 - 1u2u1r +# 2.1.2 - 1u2u2r +# 2.1.3 - 1d2d +# 2.1.4 - 1d2u1r +# 2.1.5 - 1d2u2r !! +# 2.1.6 - 1u2d1r +# 2.1.7 - 1u2d2r +# +# 2.1.8 - 1i2i2r +# 2.1.9 - 1i2i1r +# + +proc xConflictAbort {args} { + return "ABORT" +} + +reset_db +do_execsql_test 2.1.0 { + CREATE TABLE t1 (a INTEGER PRIMARY KEY, b TEXT); + INSERT INTO t1 VALUES(1, 'one'); + INSERT INTO t1 VALUES(2, 'two'); + INSERT INTO t1 VALUES(3, 'three'); +} +do_rebase_test 2.1.1 { + UPDATE t1 SET b = 'two.1' WHERE a=2 +} { + UPDATE t1 SET b = 'two.2' WHERE a=2; +} { + OMIT +} { SELECT * FROM t1 } {1 one 2 two.1 3 three} + +do_rebase_test 2.1.2 { + UPDATE t1 SET b = 'two.1' WHERE a=2 +} { + UPDATE t1 SET b = 'two.2' WHERE a=2; +} { + REPLACE +} { SELECT * FROM t1 } {1 one 2 two.2 3 three} + +do_rebase_test 2.1.3 { + DELETE FROM t1 WHERE a=3 +} { + DELETE FROM t1 WHERE a=3; +} { + OMIT +} { SELECT * FROM t1 } {1 one 2 two} + +do_rebase_test 2.1.4 { + DELETE FROM t1 WHERE a=1 +} { + UPDATE t1 SET b='one.2' WHERE a=1 +} { + OMIT +} { SELECT * FROM t1 } {2 two 3 three} + +#do_rebase_test 2.1.5 { +# DELETE FROM t1 WHERE a=1; +#} { +# UPDATE t1 SET b='one.2' WHERE a=1 +#} { +# REPLACE +#} { SELECT * FROM t1 } {2 two 3 three} + +do_rebase_test 2.1.6 { + UPDATE t1 SET b='three.1' WHERE a=3 +} { + DELETE FROM t1 WHERE a=3; +} { + OMIT +} { SELECT * FROM t1 } {1 one 2 two 3 three.1} + +do_rebase_test 2.1.7 { + UPDATE t1 SET b='three.1' WHERE a=3 +} { + DELETE FROM t1 WHERE a=3; +} { + REPLACE +} { SELECT * FROM t1 } {1 one 2 two} + +do_rebase_test 2.1.8 { + INSERT INTO t1 VALUES(4, 'four.1') +} { + INSERT INTO t1 VALUES(4, 'four.2'); +} { + REPLACE +} { SELECT * FROM t1 } {1 one 2 two 3 three 4 four.2} + +do_rebase_test 2.1.9 { + INSERT INTO t1 VALUES(4, 'four.1') +} { + INSERT INTO t1 VALUES(4, 'four.2'); +} { + OMIT +} { SELECT * FROM t1 } {1 one 2 two 3 three 4 four.1} + +do_execsql_test 2.2.0 { + CREATE TABLE t2(x, y, z PRIMARY KEY); + INSERT INTO t2 VALUES('i', 'a', 'A'); + INSERT INTO t2 VALUES('ii', 'b', 'B'); + INSERT INTO t2 VALUES('iii', 'c', 'C'); + + CREATE TABLE t3(a INTEGER PRIMARY KEY, b, c); + INSERT INTO t3 VALUES(-1, 'z', 'Z'); + INSERT INTO t3 VALUES(-2, 'y', 'Y'); +} + +do_rebase_test 2.2.1 { + UPDATE t2 SET x=1 WHERE z='A' +} { + UPDATE t2 SET y='one' WHERE z='A'; +} { +} { SELECT * FROM t2 WHERE z='A' } { 1 one A } + +do_rebase_test 2.2.2 { + UPDATE t2 SET x=1, y='one' WHERE z='B' +} { + UPDATE t2 SET y='two' WHERE z='B'; +} { + REPLACE +} { SELECT * FROM t2 WHERE z='B' } { 1 two B } + +do_rebase_test 2.2.3 { + UPDATE t2 SET x=1, y='one' WHERE z='B' +} { + UPDATE t2 SET y='two' WHERE z='B'; +} { + OMIT +} { SELECT * FROM t2 WHERE z='B' } { 1 one B } + + +reset_db +do_execsql_test 2.3.0 { + CREATE TABLE t1 (b TEXT); + INSERT INTO t1(rowid, b) VALUES(1, 'one'); + INSERT INTO t1(rowid, b) VALUES(2, 'two'); + INSERT INTO t1(rowid, b) VALUES(3, 'three'); +} +do_rebase_test 2.3.1 { + UPDATE t1 SET b = 'two.1' WHERE rowid=2 +} { + UPDATE t1 SET b = 'two.2' WHERE rowid=2; +} { + OMIT +} { SELECT rowid, * FROM t1 } {1 one 2 two.1 3 three} + +do_rebase_test 2.3.2 { + UPDATE t1 SET b = 'two.1' WHERE rowid=2 +} { + UPDATE t1 SET b = 'two.2' WHERE rowid=2; +} { + REPLACE +} { SELECT rowid, * FROM t1 } {1 one 2 two.2 3 three} + +do_rebase_test 2.3.3 { + DELETE FROM t1 WHERE rowid=3 +} { + DELETE FROM t1 WHERE rowid=3; +} { + OMIT +} { SELECT rowid, * FROM t1 } {1 one 2 two} + +do_rebase_test 2.3.4 { + DELETE FROM t1 WHERE rowid=1 +} { + UPDATE t1 SET b='one.2' WHERE rowid=1 +} { + OMIT +} { SELECT rowid, * FROM t1 } {2 two 3 three} + +do_rebase_test 2.3.6 { + UPDATE t1 SET b='three.1' WHERE rowid=3 +} { + DELETE FROM t1 WHERE rowid=3; +} { + OMIT +} { SELECT rowid, * FROM t1 } {1 one 2 two 3 three.1} + +do_rebase_test 2.3.7 { + UPDATE t1 SET b='three.1' WHERE rowid=3 +} { + DELETE FROM t1 WHERE rowid=3; +} { + REPLACE +} { SELECT rowid, * FROM t1 } {1 one 2 two} + +do_rebase_test 2.3.8 { + INSERT INTO t1(rowid, b) VALUES(4, 'four.1') +} { + INSERT INTO t1(rowid, b) VALUES(4, 'four.2'); +} { + REPLACE +} { SELECT rowid, * FROM t1 } {1 one 2 two 3 three 4 four.2} + +do_rebase_test 2.3.9 { + INSERT INTO t1(rowid, b) VALUES(4, 'four.1') +} { + INSERT INTO t1(rowid, b) VALUES(4, 'four.2'); +} { + OMIT +} { SELECT rowid, * FROM t1 } {1 one 2 two 3 three 4 four.1} + + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 3.0 { + CREATE TABLE t3(a, b, c, PRIMARY KEY(b, c)); + CREATE TABLE abcdefghijkl(x PRIMARY KEY, y, z); + + INSERT INTO t3 VALUES(1, 2, 3); + INSERT INTO t3 VALUES(4, 2, 5); + INSERT INTO t3 VALUES(7, 2, 9); + + INSERT INTO abcdefghijkl VALUES('a', 'b', 'c'); + INSERT INTO abcdefghijkl VALUES('d', 'e', 'f'); + INSERT INTO abcdefghijkl VALUES('g', 'h', 'i'); +} + +breakpoint +# do_rebase_test 3.6.tn { +# UPDATE abcdefghijkl SET z='X', y='X' WHERE x='d'; +# } { +# UPDATE abcdefghijkl SET y=1 WHERE x='d'; +# UPDATE abcdefghijkl SET z=1 WHERE x='d'; +# } [list REPLACE REPLACE REPLACE] + +foreach {tn p} { + 1 OMIT 2 REPLACE +} { + do_rebase_test 3.1.$tn { + INSERT INTO t3 VALUES(1, 1, 1); + UPDATE abcdefghijkl SET y=2; + } { + INSERT INTO t3 VALUES(4, 1, 1); + DELETE FROM abcdefghijkl; + } [list $p $p $p $p $p $p $p $p] + + do_rebase_test 3.2.$tn { + INSERT INTO abcdefghijkl SELECT * FROM t3; + UPDATE t3 SET b=b+1; + } { + INSERT INTO t3 VALUES(3, 3, 3); + INSERT INTO abcdefghijkl SELECT * FROM t3; + } [list $p $p $p $p $p $p $p $p] + + do_rebase_test 3.3.$tn { + INSERT INTO abcdefghijkl VALUES(22, 23, 24); + } { + INSERT INTO abcdefghijkl VALUES(22, 25, 26); + UPDATE abcdefghijkl SET y=400 WHERE x=22; + } [list $p $p $p $p $p $p $p $p] + + do_rebase_test 3.4.$tn { + INSERT INTO abcdefghijkl VALUES(22, 23, 24); + } { + INSERT INTO abcdefghijkl VALUES(22, 25, 26); + UPDATE abcdefghijkl SET y=400 WHERE x=22; + } [list REPLACE $p] + + do_rebase_test 3.5.$tn* { + UPDATE abcdefghijkl SET y='X' WHERE x='d'; + } { + DELETE FROM abcdefghijkl WHERE x='d'; + INSERT INTO abcdefghijkl VALUES('d', NULL, NULL); + } [list $p $p $p] + do_rebase_test 3.5.$tn { + UPDATE abcdefghijkl SET y='X' WHERE x='d'; + } { + DELETE FROM abcdefghijkl WHERE x='d'; + INSERT INTO abcdefghijkl VALUES('d', NULL, NULL); + } [list REPLACE $p $p] + + do_rebase_test 3.6.$tn { + UPDATE abcdefghijkl SET z='X', y='X' WHERE x='d'; + } { + UPDATE abcdefghijkl SET y=1 WHERE x='d'; + UPDATE abcdefghijkl SET z=1 WHERE x='d'; + } [list REPLACE $p $p] +} + +#------------------------------------------------------------------------- +# Check that apply_v2() does not create a rebase buffer for a patchset. +# And that it is not possible to rebase a patchset. +# +do_execsql_test 4.0 { + CREATE TABLE t5(o PRIMARY KEY, p, q); + INSERT INTO t5 VALUES(1, 2, 3); + INSERT INTO t5 VALUES(4, 5, 6); +} +foreach {tn cmd rebasable} { + 1 patchset 0 + 2 changeset 1 +} { + proc xConflict {args} { return "OMIT" } + do_test 4.1.$tn { + execsql { + BEGIN; + DELETE FROM t5 WHERE o=4; + } + + sqlite3session S db main + S attach * + execsql { + INSERT INTO t5 VALUES(4, 'five', 'six'); + } + set P [S $cmd] + S delete + + execsql ROLLBACK; + + set ::rebase [sqlite3changeset_apply_v2 db $P xConflict] + expr [llength $::rebase]>0 + } $rebasable +} + +foreach {tn cmd rebasable} { + 1 patchset 0 + 2 changeset 1 +} { + do_test 4.2.$tn { + sqlite3session S db main + S attach * + execsql { + INSERT INTO t5 VALUES(5+$tn, 'five', 'six'); + } + set P [S $cmd] + S delete + + sqlite3rebaser_create R + R configure $::rebase + expr [catch {R rebase $P}]==0 + } $rebasable + + catch { R delete } +} +finish_test diff --git a/ext/session/sessionrowid.test b/ext/session/sessionrowid.test new file mode 100644 index 0000000000..a39105ff9e --- /dev/null +++ b/ext/session/sessionrowid.test @@ -0,0 +1,282 @@ +# 2011 Mar 16 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# The focus of this file is testing the session module. +# + +if {![info exists testdir]} { + set testdir [file join [file dirname [info script]] .. .. test] +} +source [file join [file dirname [info script]] session_common.tcl] +source $testdir/tester.tcl +ifcapable !session {finish_test; return} + +set testprefix sessionrowid + +do_execsql_test 0.0 { + CREATE TABLE t1(a, b); +} + +foreach {tn rowid bEmpty} { + 1 0 1 + 2 1 0 + 3 -1 1 +} { + do_test 0.$tn { + sqlite3session S db main + if {$rowid>=0} { S object_config rowid $rowid } + S attach t1 + execsql { INSERT INTO t1 VALUES(1, 2); } + expr [string length [S changeset]]==0 + } $bEmpty + S delete +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 1.0 { + CREATE TABLE t1(a, b); +} + +do_iterator_test 1.1 t1 { + INSERT INTO t1 VALUES('i', 'one'); +} { + {INSERT t1 0 X.. {} {i 1 t i t one}} +} + +do_execsql_test 1.2 { + SELECT rowid, * FROM t1 +} {1 i one} + +do_iterator_test 1.3 t1 { + UPDATE t1 SET b='two' +} { + {UPDATE t1 0 X.. {i 1 {} {} t one} {{} {} {} {} t two}} +} + +do_iterator_test 1.4 t1 { + DELETE FROM t1; +} { + {DELETE t1 0 X.. {i 1 t i t two} {}} +} + +do_iterator_test 1.5 t1 { + INSERT INTO t1(rowid, a, b) VALUES(14, 'hello', 'world'); + INSERT INTO t1(rowid, a, b) VALUES(NULL, 'yes', 'no'); + INSERT INTO t1(rowid, a, b) VALUES(-123, 'ii', 'iii'); +} { + {INSERT t1 0 X.. {} {i -123 t ii t iii}} + {INSERT t1 0 X.. {} {i 15 t yes t no}} + {INSERT t1 0 X.. {} {i 14 t hello t world}} +} + +do_iterator_test 1.6 t1 { + UPDATE t1 SET a='deluxe' WHERE rowid=14; + DELETE FROM t1 WHERE rowid=-123; + INSERT INTO t1 VALUES('x', 'xi'); +} { + {DELETE t1 0 X.. {i -123 t ii t iii} {}} + {UPDATE t1 0 X.. {i 14 t hello {} {}} {{} {} t deluxe {} {}}} + {INSERT t1 0 X.. {} {i 16 t x t xi}} +} + +#------------------------------------------------------------------------- +reset_db +forcedelete test.db2 +sqlite3 db2 test.db2 + +do_execsql_test 2.0 { + CREATE TABLE t1(a, b); +} +do_execsql_test -db db2 2.0.1 { + CREATE TABLE t1(a, b); +} + +proc xConflict {args} { + puts "CONFLICT!" + return "OMIT" +} + +do_test 2.1 { + set C [changeset_from_sql { + INSERT INTO t1 VALUES('abc', 'def'); + }] + sqlite3changeset_apply db2 $C xConflict + execsql { SELECT * FROM t1 } db2 +} {abc def} +do_test 2.2 { + set C [changeset_from_sql { + UPDATE t1 SET b='hello' + }] + sqlite3changeset_apply db2 $C xConflict + execsql { SELECT * FROM t1 } db2 +} {abc hello} +do_test 2.3 { + set C [changeset_from_sql { + DELETE FROM t1 WHERE b='hello' + }] + sqlite3changeset_apply db2 $C xConflict + execsql { SELECT * FROM t1 } db2 +} {} + +do_test 2.4 { + do_then_apply_sql { + INSERT INTO t1 VALUES('i', 'one'); + INSERT INTO t1 VALUES('ii', 'two'); + INSERT INTO t1 VALUES('iii', 'three'); + INSERT INTO t1 VALUES('iv', 'four'); + } + compare_db db db2 +} {} + +do_test 2.5 { + do_then_apply_sql { + DELETE FROM t1 WHERE a='ii'; + UPDATE t1 SET b='THREE' WHERE a='iii'; + UPDATE t1 SET a='III' WHERE a='iii'; + INSERT INTO t1 VALUES('v', 'five'); + } + compare_db db db2 +} {} + +do_execsql_test 2.6 {SELECT * FROM t1} {i one III THREE iv four v five} +do_execsql_test -db db2 2.7 {SELECT * FROM t1} {i one III THREE iv four v five} + +#------------------------------------------------------------------------- +db2 close +reset_db +forcedelete test.db2 +sqlite3 db2 test.db2 + +set init_sql { + CREATE TABlE t4(a, b); + CREATE INDEX t4a ON t4(a); + CREATE UNIQUE INDEX t4b ON t4(b); +} + +do_execsql_test 3.0 $init_sql +do_execsql_test -db db2 3.0a $init_sql + +do_execsql_test -db db2 3.1 { + INSERT INTO t4(rowid, a, b) VALUES(43, 'hello', 'world'); +} +do_conflict_test 3.2 -sql { + INSERT INTO t4(rowid, a, b) VALUES(43, 'abc', 'def'); +} -tables t4 -conflicts { + {INSERT t4 CONFLICT {i 43 t abc t def} {i 43 t hello t world}} +} +do_execsql_test -db db2 3.3 { + SELECT * FROM t4 +} {hello world} + +do_execsql_test 3.4 { DELETE FROM t4 } +do_conflict_test 3.5 -sql { + INSERT INTO t4(rowid, a, b) VALUES(43, 'abc', 'def'); +} -tables t4 -conflicts { + {INSERT t4 CONFLICT {i 43 t abc t def} {i 43 t hello t world}} +} -policy REPLACE +do_execsql_test -db db2 3.6 { + SELECT * FROM t4 +} {abc def} + +do_execsql_test 3.7 { DELETE FROM t4 } +do_conflict_test 3.8 -sql { + INSERT INTO t4(rowid, a, b) VALUES(45, 'xyz', 'def'); +} -tables t4 -conflicts { + {INSERT t4 CONSTRAINT {i 45 t xyz t def}} +} +do_execsql_test -db db2 3.9 { + SELECT * FROM t4 +} {abc def} + + +do_execsql_test -db db 3.10a { DELETE FROM t4 } +do_execsql_test -db db2 3.10b { DELETE FROM t4 } + +do_execsql_test -db db 3.11a { + INSERT INTO t4(rowid, a, b) VALUES(111, 'one', 'one'); + INSERT INTO t4(rowid, a, b) VALUES(222, 'two', 'two'); +} +do_execsql_test -db db2 3.11b { + INSERT INTO t4(rowid, a, b) VALUES(111, 'one', 'blip'); +} + +do_conflict_test 3.12 -sql { + DELETE FROM t4 WHERE a='one'; +} -tables t4 -conflicts { + {DELETE t4 DATA {i 111 t one t one} {i 111 t one t blip}} +} +do_execsql_test -db db2 3.13 { + SELECT * FROM t4 +} {one blip} + +do_conflict_test 3.14 -sql { + DELETE FROM t4 WHERE a='two'; +} -tables t4 -conflicts { + {DELETE t4 NOTFOUND {i 222 t two t two}} +} +do_execsql_test -db db2 3.15 { + SELECT * FROM t4 +} {one blip} + +do_execsql_test -db db 3.16a { DELETE FROM t4 } +do_execsql_test -db db2 3.16b { DELETE FROM t4 } + +do_execsql_test -db db 3.17a { + INSERT INTO t4(rowid, a, b) VALUES(111, 'one', 'one'); + INSERT INTO t4(rowid, a, b) VALUES(222, 'two', 'two'); +} +do_execsql_test -db db2 3.17b { + INSERT INTO t4(rowid, a, b) VALUES(111, 'one', 'blip'); +} + +do_conflict_test 3.18 -sql { + UPDATE t4 SET b='xyz' WHERE a='one' +} -tables t4 -conflicts { + {UPDATE t4 DATA {i 111 {} {} t one} {{} {} {} {} t xyz} {i 111 t one t blip}} +} +do_execsql_test -db db2 3.19 { + SELECT * FROM t4 +} {one blip} + +do_conflict_test 3.20 -sql { + UPDATE t4 SET b='123' WHERE a='two' +} -tables t4 -conflicts { + {UPDATE t4 NOTFOUND {i 222 {} {} t two} {{} {} {} {} t 123}} +} +do_execsql_test -db db2 3.21 { + SELECT * FROM t4 +} {one blip} +db2 close + +#-------------------------------------------------------------------------- +breakpoint +do_diff_test 4.0 { + CREATE TABLE t1(x, y); + CREATE TABLE aux.t1(x, y); + INSERT INTO t1 VALUES(1, 2); +} + +do_diff_test 4.1 { + CREATE TABLE t1(x, y); + CREATE TABLE aux.t1(x, y); + INSERT INTO aux.t1 VALUES(1, 2); +} + +do_diff_test 4.2 { + CREATE TABLE t1(x, y); + CREATE TABLE aux.t1(x, y); + INSERT INTO t1(rowid, x, y) VALUES(413, 'hello', 'there'); + INSERT INTO aux.t1(rowid, x, y) VALUES(413, 'hello', 'world'); +} + +finish_test + diff --git a/ext/session/sessionsize.test b/ext/session/sessionsize.test new file mode 100644 index 0000000000..01638c6677 --- /dev/null +++ b/ext/session/sessionsize.test @@ -0,0 +1,131 @@ +# 2021 April 22 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. +# + +if {![info exists testdir]} { + set testdir [file join [file dirname [info script]] .. .. test] +} +source [file join [file dirname [info script]] session_common.tcl] +source $testdir/tester.tcl +ifcapable !session {finish_test; return} + +set testprefix sessionsize + +proc do_changeset_size_test {tn sql} { + sqlite3session S db main + S attach * + db eval $sql + + set sz [S changeset_size] + set C [S changeset] + set szC [string length $C] + S delete + + do_test $tn "expr $sz" $szC +} + +do_execsql_test 1.0 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c); + INSERT INTO t1 VALUES(1, 'abc', 'def'); + INSERT INTO t1 VALUES(2, 'ghi', 'jkl'); +} + +do_changeset_size_test 1.1 { + INSERT INTO t1 VALUES(3, 'hello', 'world'); +} + +do_changeset_size_test 1.2 { + DELETE FROM t1 WHERE a=2; +} + +do_changeset_size_test 1.3 { + DELETE FROM t1 WHERE a=3; + INSERT INTO t1 VALUES(3, 1, 2); +} + +do_changeset_size_test 1.4 { + UPDATE t1 SET c='hello world' WHERE a=3; +} + +#------------------------------------------------------------------------- + +do_execsql_test 2.0 { + CREATE TABlE t2(a, b, c, d, PRIMARY KEY(a, b)) WITHOUT ROWID; + CREATE TABlE t3(a, b, c, d PRIMARY KEY); +} + +do_changeset_size_test 2.1 { + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<50 + ) + INSERT INTO t2 SELECT i, i+1, i+2, i+3 FROM s; + + UPDATE t2 SET c=randomblob(a) WHERE a>10 +} + +do_changeset_size_test 2.2 { + DELETE FROM t2 WHERE a=1; + INSERT INTO t2 VALUES(1, 4, 3, 4); +} + +do_changeset_size_test 2.2 { + UPDATE t2 SET b=4 WHERE a=2 +} + +do_changeset_size_test 2.3 { + INSERT INTO t2 VALUES('a', 'b', 'c', 'd'); + UPDATE t2 SET c='qwertyuiop' WHERE a='a'; +} + +do_changeset_size_test 2.4 { + DELETE FROM t2 WHERE a='a'; + INSERT INTO t2 VALUES('a', 'b', 'c', 'd'); +} + +do_changeset_size_test 2.5 { + UPDATE t2 SET a='aa', b='bb' WHERE (a, b) = ('a', 'b'); +} + +do_changeset_size_test 2.6 { + UPDATE t2 SET a='a', b='b' WHERE (a, b) = ('aa', 'bb'); +} + +do_changeset_size_test 2.7 { + INSERT INTO t3 DEFAULT VALUES; + INSERT INTO t3 VALUES(1,2,3,4); +} + +#------------------------------------------------------------------------- +reset_db + +do_execsql_test 3.0 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b); +} + +do_test 3.1 { + sqlite3session S db main + S object_config size -1 +} 1 + +do_test 3.2.1 { S object_config size 0 } 0 +do_test 3.2.2 { S object_config size -1 } 0 +do_test 3.2.3 { S object_config size 1 } 1 +do_test 3.2.4 { S object_config size -1 } 1 + +do_test 3.3 { S attach t1 } {} +do_test 3.4 { S object_config size 1 } {SQLITE_MISUSE} +do_test 3.4 { S object_config size -1 } {1} + +S delete + +finish_test + diff --git a/ext/session/sessionstat1.test b/ext/session/sessionstat1.test new file mode 100644 index 0000000000..16dc4e2727 --- /dev/null +++ b/ext/session/sessionstat1.test @@ -0,0 +1,310 @@ +# 2018 January 12 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +if {![info exists testdir]} { + set testdir [file join [file dirname [info script]] .. .. test] +} +source [file join [file dirname [info script]] session_common.tcl] +source $testdir/tester.tcl +ifcapable !session {finish_test; return} + +set testprefix sessionstat1 + +do_execsql_test 1.0 { + CREATE TABLE t1(a PRIMARY KEY, b, c); + CREATE INDEX t1b ON t1(b); + CREATE INDEX t1c ON t1(c); + + WITH s(i) AS ( + SELECT 0 UNION ALL SELECT i+1 FROM s WHERE (i+1)<32 + ) + INSERT INTO t1 SELECT i, i%8, i%2 FROM s; +} + +do_iterator_test 1.1 {} { + ANALYZE +} { + {INSERT sqlite_stat1 0 XX. {} {t t1 t sqlite_autoindex_t1_1 t {32 1}}} + {INSERT sqlite_stat1 0 XX. {} {t t1 t t1b t {32 4}}} + {INSERT sqlite_stat1 0 XX. {} {t t1 t t1c t {32 16}}} +} + +do_execsql_test 1.2 { + WITH s(i) AS ( + SELECT 32 UNION ALL SELECT i+1 FROM s WHERE (i+1)<64 + ) + INSERT INTO t1 SELECT i, i%8, i%2 FROM s; +} + +do_iterator_test 1.3 {} { + ANALYZE +} { + {UPDATE sqlite_stat1 0 XX. {t t1 t sqlite_autoindex_t1_1 t {32 1}} {{} {} {} {} t {64 1}}} + {UPDATE sqlite_stat1 0 XX. {t t1 t t1b t {32 4}} {{} {} {} {} t {64 8}}} + {UPDATE sqlite_stat1 0 XX. {t t1 t t1c t {32 16}} {{} {} {} {} t {64 32}}} +} + +do_iterator_test 1.5 {} { + DROP INDEX t1b; +} { + {DELETE sqlite_stat1 0 XX. {t t1 t t1b t {64 8}} {}} +} + +do_iterator_test 1.6 {} { + DROP TABLE t1; +} { + {DELETE sqlite_stat1 0 XX. {t t1 t sqlite_autoindex_t1_1 t {64 1}} {}} + {DELETE sqlite_stat1 0 XX. {t t1 t t1c t {64 32}} {}} +} + +#------------------------------------------------------------------------- +# +catch { db2 close } +forcedelete test.db2 +sqlite3 db2 test.db2 + +do_test 2.0 { + do_common_sql { + CREATE TABLE t1(a PRIMARY KEY, b, c); + CREATE INDEX t1b ON t1(b); + CREATE INDEX t1c ON t1(c); + ANALYZE; + } +} {} + +do_test 2.1 { + do_then_apply_sql -ignorenoop { + WITH s(i) AS ( + SELECT 0 UNION ALL SELECT i+1 FROM s WHERE (i+1)<32 + ) + INSERT INTO t1 SELECT i, i%8, i%2 FROM s; + ANALYZE; + } +} {} + +do_execsql_test -db db2 2.2 { + SELECT * FROM sqlite_stat1 ORDER BY tbl, idx +} { + t1 sqlite_autoindex_t1_1 {32 1} + t1 t1b {32 4} + t1 t1c {32 16} +} + +do_test 2.3 { + do_then_apply_sql -ignorenoop { DROP INDEX t1c } +} {} + +do_execsql_test -db db2 2.4 { + SELECT * FROM sqlite_stat1 ORDER BY tbl, idx; +} { + t1 sqlite_autoindex_t1_1 {32 1} + t1 t1b {32 4} +} + +do_test 2.3 { + do_then_apply_sql -ignorenoop { DROP TABLE t1 } +} {} + +do_execsql_test -db db2 2.4 { + SELECT * FROM sqlite_stat1 +} { +} + +do_execsql_test -db db2 2.5 { SELECT count(*) FROM t1 } 32 + +#------------------------------------------------------------------------- +db2 close +forcedelete test.db2 +reset_db +sqlite3 db2 test.db2 + +do_test 3.0 { + do_common_sql { + CREATE TABLE t1(a, b, c); + ANALYZE; + DELETE FROM sqlite_stat1; + } + execsql { + INSERT INTO t1 VALUES(1, 1, 1); + INSERT INTO t1 VALUES(2, 2, 2); + INSERT INTO t1 VALUES(3, 3, 3); + INSERT INTO t1 VALUES(4, 4, 4); + } +} {} + +do_iterator_test 3.1 {} { + ANALYZE +} { + {INSERT sqlite_stat1 0 XX. {} {t t1 b {} t 4}} +} +db null null +db2 null null +do_execsql_test 3.2 { + SELECT * FROM sqlite_stat1; +} {t1 null 4} +do_test 3.3 { + execsql { DELETE FROM sqlite_stat1 } + do_then_apply_sql -ignorenoop { ANALYZE } + execsql { SELECT * FROM sqlite_stat1 } db2 +} {t1 null 4} +do_test 3.4 { + execsql { INSERT INTO t1 VALUES(5,5,5) } + do_then_apply_sql -ignorenoop { ANALYZE } + execsql { SELECT * FROM sqlite_stat1 } db2 +} {t1 null 5} +do_test 3.5 { + do_then_apply_sql -ignorenoop { DROP TABLE t1 } + execsql { SELECT * FROM sqlite_stat1 } db2 +} {} + +do_test 3.6.1 { + execsql { + CREATE TABLE t1(a, b, c); + CREATE TABLE t2(x, y, z); + INSERT INTO t1 VALUES(1,1,1), (2,2,2), (3,3,3), (4,4,4), (5,5,5); + INSERT INTO t2 SELECT * FROM t1; + DELETE FROM sqlite_stat1; + } + sqlite3session S db main + S attach sqlite_stat1 + execsql { ANALYZE } +} {} +do_changeset_test 3.6.2 S { + {INSERT sqlite_stat1 0 XX. {} {t t2 b {} t 5}} + {INSERT sqlite_stat1 0 XX. {} {t t1 b {} t 5}} +} +do_changeset_invert_test 3.6.3 S { + {DELETE sqlite_stat1 0 XX. {t t2 b {} t 5} {}} + {DELETE sqlite_stat1 0 XX. {t t1 b {} t 5} {}} +} +do_test 3.6.4 { S delete } {} + +proc sql_changeset_concat {args} { + foreach sql $args { + sqlite3session S db main + S attach sqlite_stat1 + execsql $sql + set change [S changeset] + S delete + + if {[info vars ret]!=""} { + set ret [sqlite3changeset_concat $ret $change] + } else { + set ret $change + } + } + + changeset_to_list $ret +} + +proc do_scc_test {tn args} { + uplevel [list \ + do_test $tn [concat sql_changeset_concat [lrange $args 0 end-1]] \ + [list {*}[ lindex $args end ]] + ] +} + +do_execsql_test 3.7.0 { + DELETE FROM sqlite_stat1; +} +do_scc_test 3.7.1 { + ANALYZE; +} { + INSERT INTO t2 VALUES(6,6,6); + ANALYZE; +} { + {INSERT sqlite_stat1 0 XX. {} {t t1 b {} t 5}} + {INSERT sqlite_stat1 0 XX. {} {t t2 b {} t 6}} +} + +#------------------------------------------------------------------------- +catch { db2 close } +reset_db +forcedelete test.db2 +sqlite3 db2 test.db2 + +do_test 4.1.0 { + do_common_sql { + CREATE TABLE t1(a, b); + CREATE INDEX i1 ON t1(a); + CREATE INDEX i2 ON t1(b); + INSERT INTO t1 VALUES(1,1), (2,2); + ANALYZE; + } + execsql { DELETE FROM sqlite_stat1 } +} {} + +do_test 4.1.1 { + execsql { INSERT INTO t1 VALUES(3,3); } + set C [changeset_from_sql {ANALYZE}] + set ::c [list] + proc xConflict {args} { + lappend ::c $args + return "OMIT" + } + sqlite3changeset_apply db2 $C xConflict + set ::c +} [list {*}{ + {INSERT sqlite_stat1 CONFLICT {t t1 t i1 t {3 1}} {t t1 t i1 t {2 1}}} + {INSERT sqlite_stat1 CONFLICT {t t1 t i2 t {3 1}} {t t1 t i2 t {2 1}}} +}] + +do_execsql_test -db db2 4.1.2 { + SELECT * FROM sqlite_stat1 ORDER BY 1,2; +} {t1 i1 {2 1} t1 i2 {2 1}} + +do_test 4.1.3 { + proc xConflict {args} { + return "REPLACE" + } + sqlite3changeset_apply db2 $C xConflict + execsql { SELECT * FROM sqlite_stat1 ORDER BY 1,2 } db2 +} {t1 i1 {3 1} t1 i2 {3 1}} + +do_test 4.2.0 { + do_common_sql { + DROP TABLE t1; + CREATE TABLE t3(x,y); + INSERT INTO t3 VALUES('a','a'); + INSERT INTO t3 VALUES('b','b'); + ANALYZE; + } + execsql { DELETE FROM sqlite_stat1 } +} {} +do_test 4.2.1 { + execsql { INSERT INTO t3 VALUES('c','c'); } + set C [changeset_from_sql {ANALYZE}] + set ::c [list] + proc xConflict {args} { + lappend ::c $args + return "OMIT" + } + sqlite3changeset_apply db2 $C xConflict + set ::c +} [list {*}{ + {INSERT sqlite_stat1 CONFLICT {t t3 b {} t 3} {t t3 b {} t 2}} +}] + +db2 null null +do_execsql_test -db db2 4.2.2 { + SELECT * FROM sqlite_stat1 ORDER BY 1,2; +} {t3 null 2} + +do_test 4.2.3 { + proc xConflict {args} { + return "REPLACE" + } + sqlite3changeset_apply db2 $C xConflict + execsql { SELECT * FROM sqlite_stat1 ORDER BY 1,2 } db2 +} {t3 null 3} + +finish_test diff --git a/ext/session/sessionwor.test b/ext/session/sessionwor.test new file mode 100644 index 0000000000..7d9e5c6a89 --- /dev/null +++ b/ext/session/sessionwor.test @@ -0,0 +1,123 @@ +# 2017 Jan 31 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# The focus of this file is testing the session module. Specifically, +# testing support for WITHOUT ROWID tables. +# + +if {![info exists testdir]} { + set testdir [file join [file dirname [info script]] .. .. test] +} +source [file join [file dirname [info script]] session_common.tcl] +source $testdir/tester.tcl +ifcapable !session {finish_test; return} + +set testprefix sessionwor + +proc test_reset {} { + catch { db close } + catch { db2 close } + forcedelete test.db test.db2 + sqlite3 db test.db + sqlite3 db2 test.db2 +} + +foreach {tn wo} { + 1 "" + 2 "WITHOUT ROWID" +} { + reset_db + + do_execsql_test 1.$tn.0 "CREATE TABLE t1(a PRIMARY KEY, b) $wo ;" + + do_iterator_test 1.$tn.1 t1 { + INSERT INTO t1 VALUES('one', 'two'); + } { + {INSERT t1 0 X. {} {t one t two}} + } + + do_iterator_test 1.$tn.2 t1 { + UPDATE t1 SET b='three' + } { + {UPDATE t1 0 X. {t one t two} {{} {} t three}} + } + + do_iterator_test 1.$tn.3 t1 { + REPLACE INTO t1 VALUES('one', 'four'); + } { + {UPDATE t1 0 X. {t one t three} {{} {} t four}} + } + + do_iterator_test 1.$tn.4 t1 { + DELETE FROM t1; + } { + {DELETE t1 0 X. {t one t four} {}} + } +} + +foreach {tn wo} { + 1 "" + 2 "WITHOUT ROWID" +} { + reset_db + + do_execsql_test 2.$tn.0.1 "CREATE TABLE t1(a INTEGER PRIMARY KEY, b) $wo ;" + do_execsql_test 2.$tn.0.2 "CREATE TABLE t2(a INTEGER PRIMARY KEY, b) $wo ;" + do_execsql_test 2.$tn.0.3 "CREATE TABLE t3(a INTEGER PRIMARY KEY, b) $wo ;" + + do_iterator_test 1.1 t1 { + INSERT INTO t1 VALUES(1, 'two'); + } { + {INSERT t1 0 X. {} {i 1 t two}} + } + + do_iterator_test 2.$tn.2 t1 { + UPDATE t1 SET b='three' + } { + {UPDATE t1 0 X. {i 1 t two} {{} {} t three}} + } + + do_iterator_test 2.$tn.3 t1 { + REPLACE INTO t1 VALUES(1, 'four'); + } { + {UPDATE t1 0 X. {i 1 t three} {{} {} t four}} + } + + do_iterator_test 2.$tn.4 t1 { + DELETE FROM t1; + } { + {DELETE t1 0 X. {i 1 t four} {}} + } + + do_execsql_test 2.$tn.5 { + INSERT INTO t1 VALUES(1, 'one'); + INSERT INTO t1 VALUES(2, 'two'); + INSERT INTO t1 VALUES(3, 'three'); + } + + do_iterator_test 2.$tn.6 t2 { + INSERT INTO t2 SELECT a, b FROM t1 + } { + {INSERT t2 0 X. {} {i 1 t one}} + {INSERT t2 0 X. {} {i 2 t two}} + {INSERT t2 0 X. {} {i 3 t three}} + } + do_iterator_test 2.$tn.7 t3 { + INSERT INTO t3 SELECT * FROM t1 + } { + {INSERT t3 0 X. {} {i 1 t one}} + {INSERT t3 0 X. {} {i 2 t two}} + {INSERT t3 0 X. {} {i 3 t three}} + } +} + +finish_test + diff --git a/ext/session/sqlite3session.c b/ext/session/sqlite3session.c new file mode 100644 index 0000000000..90fedc6db4 --- /dev/null +++ b/ext/session/sqlite3session.c @@ -0,0 +1,6762 @@ + +#if defined(SQLITE_ENABLE_SESSION) && defined(SQLITE_ENABLE_PREUPDATE_HOOK) +#include "sqlite3session.h" +#include +#include + +#ifndef SQLITE_AMALGAMATION +# include "sqliteInt.h" +# include "vdbeInt.h" +#endif + +typedef struct SessionTable SessionTable; +typedef struct SessionChange SessionChange; +typedef struct SessionBuffer SessionBuffer; +typedef struct SessionInput SessionInput; + +/* +** Minimum chunk size used by streaming versions of functions. +*/ +#ifndef SESSIONS_STRM_CHUNK_SIZE +# ifdef SQLITE_TEST +# define SESSIONS_STRM_CHUNK_SIZE 64 +# else +# define SESSIONS_STRM_CHUNK_SIZE 1024 +# endif +#endif + +#define SESSIONS_ROWID "_rowid_" + +static int sessions_strm_chunk_size = SESSIONS_STRM_CHUNK_SIZE; + +typedef struct SessionHook SessionHook; +struct SessionHook { + void *pCtx; + int (*xOld)(void*,int,sqlite3_value**); + int (*xNew)(void*,int,sqlite3_value**); + int (*xCount)(void*); + int (*xDepth)(void*); +}; + +/* +** Session handle structure. +*/ +struct sqlite3_session { + sqlite3 *db; /* Database handle session is attached to */ + char *zDb; /* Name of database session is attached to */ + int bEnableSize; /* True if changeset_size() enabled */ + int bEnable; /* True if currently recording */ + int bIndirect; /* True if all changes are indirect */ + int bAutoAttach; /* True to auto-attach tables */ + int bImplicitPK; /* True to handle tables with implicit PK */ + int rc; /* Non-zero if an error has occurred */ + void *pFilterCtx; /* First argument to pass to xTableFilter */ + int (*xTableFilter)(void *pCtx, const char *zTab); + i64 nMalloc; /* Number of bytes of data allocated */ + i64 nMaxChangesetSize; + sqlite3_value *pZeroBlob; /* Value containing X'' */ + sqlite3_session *pNext; /* Next session object on same db. */ + SessionTable *pTable; /* List of attached tables */ + SessionHook hook; /* APIs to grab new and old data with */ +}; + +/* +** Instances of this structure are used to build strings or binary records. +*/ +struct SessionBuffer { + u8 *aBuf; /* Pointer to changeset buffer */ + int nBuf; /* Size of buffer aBuf */ + int nAlloc; /* Size of allocation containing aBuf */ +}; + +/* +** An object of this type is used internally as an abstraction for +** input data. Input data may be supplied either as a single large buffer +** (e.g. sqlite3changeset_start()) or using a stream function (e.g. +** sqlite3changeset_start_strm()). +** +** bNoDiscard: +** If true, then the only time data is discarded is as a result of explicit +** sessionDiscardData() calls. Not within every sessionInputBuffer() call. +*/ +struct SessionInput { + int bNoDiscard; /* If true, do not discard in InputBuffer() */ + int iCurrent; /* Offset in aData[] of current change */ + int iNext; /* Offset in aData[] of next change */ + u8 *aData; /* Pointer to buffer containing changeset */ + int nData; /* Number of bytes in aData */ + + SessionBuffer buf; /* Current read buffer */ + int (*xInput)(void*, void*, int*); /* Input stream call (or NULL) */ + void *pIn; /* First argument to xInput */ + int bEof; /* Set to true after xInput finished */ +}; + +/* +** Structure for changeset iterators. +*/ +struct sqlite3_changeset_iter { + SessionInput in; /* Input buffer or stream */ + SessionBuffer tblhdr; /* Buffer to hold apValue/zTab/abPK/ */ + int bPatchset; /* True if this is a patchset */ + int bInvert; /* True to invert changeset */ + int bSkipEmpty; /* Skip noop UPDATE changes */ + int rc; /* Iterator error code */ + sqlite3_stmt *pConflict; /* Points to conflicting row, if any */ + char *zTab; /* Current table */ + int nCol; /* Number of columns in zTab */ + int op; /* Current operation */ + int bIndirect; /* True if current change was indirect */ + u8 *abPK; /* Primary key array */ + sqlite3_value **apValue; /* old.* and new.* values */ +}; + +/* +** Each session object maintains a set of the following structures, one +** for each table the session object is monitoring. The structures are +** stored in a linked list starting at sqlite3_session.pTable. +** +** The keys of the SessionTable.aChange[] hash table are all rows that have +** been modified in any way since the session object was attached to the +** table. +** +** The data associated with each hash-table entry is a structure containing +** a subset of the initial values that the modified row contained at the +** start of the session. Or no initial values if the row was inserted. +** +** pDfltStmt: +** This is only used by the sqlite3changegroup_xxx() APIs, not by +** regular sqlite3_session objects. It is a SELECT statement that +** selects the default value for each table column. For example, +** if the table is +** +** CREATE TABLE xx(a DEFAULT 1, b, c DEFAULT 'abc') +** +** then this variable is the compiled version of: +** +** SELECT 1, NULL, 'abc' +*/ +struct SessionTable { + SessionTable *pNext; + char *zName; /* Local name of table */ + int nCol; /* Number of non-hidden columns */ + int nTotalCol; /* Number of columns including hidden */ + int bStat1; /* True if this is sqlite_stat1 */ + int bRowid; /* True if this table uses rowid for PK */ + const char **azCol; /* Column names */ + const char **azDflt; /* Default value expressions */ + int *aiIdx; /* Index to pass to xNew/xOld */ + u8 *abPK; /* Array of primary key flags */ + int nEntry; /* Total number of entries in hash table */ + int nChange; /* Size of apChange[] array */ + SessionChange **apChange; /* Hash table buckets */ + sqlite3_stmt *pDfltStmt; +}; + +/* +** RECORD FORMAT: +** +** The following record format is similar to (but not compatible with) that +** used in SQLite database files. This format is used as part of the +** change-set binary format, and so must be architecture independent. +** +** Unlike the SQLite database record format, each field is self-contained - +** there is no separation of header and data. Each field begins with a +** single byte describing its type, as follows: +** +** 0x00: Undefined value. +** 0x01: Integer value. +** 0x02: Real value. +** 0x03: Text value. +** 0x04: Blob value. +** 0x05: SQL NULL value. +** +** Note that the above match the definitions of SQLITE_INTEGER, SQLITE_TEXT +** and so on in sqlite3.h. For undefined and NULL values, the field consists +** only of the single type byte. For other types of values, the type byte +** is followed by: +** +** Text values: +** A varint containing the number of bytes in the value (encoded using +** UTF-8). Followed by a buffer containing the UTF-8 representation +** of the text value. There is no nul terminator. +** +** Blob values: +** A varint containing the number of bytes in the value, followed by +** a buffer containing the value itself. +** +** Integer values: +** An 8-byte big-endian integer value. +** +** Real values: +** An 8-byte big-endian IEEE 754-2008 real value. +** +** Varint values are encoded in the same way as varints in the SQLite +** record format. +** +** CHANGESET FORMAT: +** +** A changeset is a collection of DELETE, UPDATE and INSERT operations on +** one or more tables. Operations on a single table are grouped together, +** but may occur in any order (i.e. deletes, updates and inserts are all +** mixed together). +** +** Each group of changes begins with a table header: +** +** 1 byte: Constant 0x54 (capital 'T') +** Varint: Number of columns in the table. +** nCol bytes: 0x01 for PK columns, 0x00 otherwise. +** N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated. +** +** Followed by one or more changes to the table. +** +** 1 byte: Either SQLITE_INSERT (0x12), UPDATE (0x17) or DELETE (0x09). +** 1 byte: The "indirect-change" flag. +** old.* record: (delete and update only) +** new.* record: (insert and update only) +** +** The "old.*" and "new.*" records, if present, are N field records in the +** format described above under "RECORD FORMAT", where N is the number of +** columns in the table. The i'th field of each record is associated with +** the i'th column of the table, counting from left to right in the order +** in which columns were declared in the CREATE TABLE statement. +** +** The new.* record that is part of each INSERT change contains the values +** that make up the new row. Similarly, the old.* record that is part of each +** DELETE change contains the values that made up the row that was deleted +** from the database. In the changeset format, the records that are part +** of INSERT or DELETE changes never contain any undefined (type byte 0x00) +** fields. +** +** Within the old.* record associated with an UPDATE change, all fields +** associated with table columns that are not PRIMARY KEY columns and are +** not modified by the UPDATE change are set to "undefined". Other fields +** are set to the values that made up the row before the UPDATE that the +** change records took place. Within the new.* record, fields associated +** with table columns modified by the UPDATE change contain the new +** values. Fields associated with table columns that are not modified +** are set to "undefined". +** +** PATCHSET FORMAT: +** +** A patchset is also a collection of changes. It is similar to a changeset, +** but leaves undefined those fields that are not useful if no conflict +** resolution is required when applying the changeset. +** +** Each group of changes begins with a table header: +** +** 1 byte: Constant 0x50 (capital 'P') +** Varint: Number of columns in the table. +** nCol bytes: 0x01 for PK columns, 0x00 otherwise. +** N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated. +** +** Followed by one or more changes to the table. +** +** 1 byte: Either SQLITE_INSERT (0x12), UPDATE (0x17) or DELETE (0x09). +** 1 byte: The "indirect-change" flag. +** single record: (PK fields for DELETE, PK and modified fields for UPDATE, +** full record for INSERT). +** +** As in the changeset format, each field of the single record that is part +** of a patchset change is associated with the correspondingly positioned +** table column, counting from left to right within the CREATE TABLE +** statement. +** +** For a DELETE change, all fields within the record except those associated +** with PRIMARY KEY columns are omitted. The PRIMARY KEY fields contain the +** values identifying the row to delete. +** +** For an UPDATE change, all fields except those associated with PRIMARY KEY +** columns and columns that are modified by the UPDATE are set to "undefined". +** PRIMARY KEY fields contain the values identifying the table row to update, +** and fields associated with modified columns contain the new column values. +** +** The records associated with INSERT changes are in the same format as for +** changesets. It is not possible for a record associated with an INSERT +** change to contain a field set to "undefined". +** +** REBASE BLOB FORMAT: +** +** A rebase blob may be output by sqlite3changeset_apply_v2() and its +** streaming equivalent for use with the sqlite3_rebaser APIs to rebase +** existing changesets. A rebase blob contains one entry for each conflict +** resolved using either the OMIT or REPLACE strategies within the apply_v2() +** call. +** +** The format used for a rebase blob is very similar to that used for +** changesets. All entries related to a single table are grouped together. +** +** Each group of entries begins with a table header in changeset format: +** +** 1 byte: Constant 0x54 (capital 'T') +** Varint: Number of columns in the table. +** nCol bytes: 0x01 for PK columns, 0x00 otherwise. +** N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated. +** +** Followed by one or more entries associated with the table. +** +** 1 byte: Either SQLITE_INSERT (0x12), DELETE (0x09). +** 1 byte: Flag. 0x01 for REPLACE, 0x00 for OMIT. +** record: (in the record format defined above). +** +** In a rebase blob, the first field is set to SQLITE_INSERT if the change +** that caused the conflict was an INSERT or UPDATE, or to SQLITE_DELETE if +** it was a DELETE. The second field is set to 0x01 if the conflict +** resolution strategy was REPLACE, or 0x00 if it was OMIT. +** +** If the change that caused the conflict was a DELETE, then the single +** record is a copy of the old.* record from the original changeset. If it +** was an INSERT, then the single record is a copy of the new.* record. If +** the conflicting change was an UPDATE, then the single record is a copy +** of the new.* record with the PK fields filled in based on the original +** old.* record. +*/ + +/* +** For each row modified during a session, there exists a single instance of +** this structure stored in a SessionTable.aChange[] hash table. +*/ +struct SessionChange { + u8 op; /* One of UPDATE, DELETE, INSERT */ + u8 bIndirect; /* True if this change is "indirect" */ + u16 nRecordField; /* Number of fields in aRecord[] */ + int nMaxSize; /* Max size of eventual changeset record */ + int nRecord; /* Number of bytes in buffer aRecord[] */ + u8 *aRecord; /* Buffer containing old.* record */ + SessionChange *pNext; /* For hash-table collisions */ +}; + +/* +** Write a varint with value iVal into the buffer at aBuf. Return the +** number of bytes written. +*/ +static int sessionVarintPut(u8 *aBuf, int iVal){ + return putVarint32(aBuf, iVal); +} + +/* +** Return the number of bytes required to store value iVal as a varint. +*/ +static int sessionVarintLen(int iVal){ + return sqlite3VarintLen(iVal); +} + +/* +** Read a varint value from aBuf[] into *piVal. Return the number of +** bytes read. +*/ +static int sessionVarintGet(const u8 *aBuf, int *piVal){ + return getVarint32(aBuf, *piVal); +} + +/* Load an unaligned and unsigned 32-bit integer */ +#define SESSION_UINT32(x) (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3]) + +/* +** Read a 64-bit big-endian integer value from buffer aRec[]. Return +** the value read. +*/ +static sqlite3_int64 sessionGetI64(u8 *aRec){ + u64 x = SESSION_UINT32(aRec); + u32 y = SESSION_UINT32(aRec+4); + x = (x<<32) + y; + return (sqlite3_int64)x; +} + +/* +** Write a 64-bit big-endian integer value to the buffer aBuf[]. +*/ +static void sessionPutI64(u8 *aBuf, sqlite3_int64 i){ + aBuf[0] = (i>>56) & 0xFF; + aBuf[1] = (i>>48) & 0xFF; + aBuf[2] = (i>>40) & 0xFF; + aBuf[3] = (i>>32) & 0xFF; + aBuf[4] = (i>>24) & 0xFF; + aBuf[5] = (i>>16) & 0xFF; + aBuf[6] = (i>> 8) & 0xFF; + aBuf[7] = (i>> 0) & 0xFF; +} + +/* +** This function is used to serialize the contents of value pValue (see +** comment titled "RECORD FORMAT" above). +** +** If it is non-NULL, the serialized form of the value is written to +** buffer aBuf. *pnWrite is set to the number of bytes written before +** returning. Or, if aBuf is NULL, the only thing this function does is +** set *pnWrite. +** +** If no error occurs, SQLITE_OK is returned. Or, if an OOM error occurs +** within a call to sqlite3_value_text() (may fail if the db is utf-16)) +** SQLITE_NOMEM is returned. +*/ +static int sessionSerializeValue( + u8 *aBuf, /* If non-NULL, write serialized value here */ + sqlite3_value *pValue, /* Value to serialize */ + sqlite3_int64 *pnWrite /* IN/OUT: Increment by bytes written */ +){ + int nByte; /* Size of serialized value in bytes */ + + if( pValue ){ + int eType; /* Value type (SQLITE_NULL, TEXT etc.) */ + + eType = sqlite3_value_type(pValue); + if( aBuf ) aBuf[0] = eType; + + switch( eType ){ + case SQLITE_NULL: + nByte = 1; + break; + + case SQLITE_INTEGER: + case SQLITE_FLOAT: + if( aBuf ){ + /* TODO: SQLite does something special to deal with mixed-endian + ** floating point values (e.g. ARM7). This code probably should + ** too. */ + u64 i; + if( eType==SQLITE_INTEGER ){ + i = (u64)sqlite3_value_int64(pValue); + }else{ + double r; + assert( sizeof(double)==8 && sizeof(u64)==8 ); + r = sqlite3_value_double(pValue); + memcpy(&i, &r, 8); + } + sessionPutI64(&aBuf[1], i); + } + nByte = 9; + break; + + default: { + u8 *z; + int n; + int nVarint; + + assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB ); + if( eType==SQLITE_TEXT ){ + z = (u8 *)sqlite3_value_text(pValue); + }else{ + z = (u8 *)sqlite3_value_blob(pValue); + } + n = sqlite3_value_bytes(pValue); + if( z==0 && (eType!=SQLITE_BLOB || n>0) ) return SQLITE_NOMEM; + nVarint = sessionVarintLen(n); + + if( aBuf ){ + sessionVarintPut(&aBuf[1], n); + if( n>0 ) memcpy(&aBuf[nVarint + 1], z, n); + } + + nByte = 1 + nVarint + n; + break; + } + } + }else{ + nByte = 1; + if( aBuf ) aBuf[0] = '\0'; + } + + if( pnWrite ) *pnWrite += nByte; + return SQLITE_OK; +} + +/* +** Allocate and return a pointer to a buffer nByte bytes in size. If +** pSession is not NULL, increase the sqlite3_session.nMalloc variable +** by the number of bytes allocated. +*/ +static void *sessionMalloc64(sqlite3_session *pSession, i64 nByte){ + void *pRet = sqlite3_malloc64(nByte); + if( pSession ) pSession->nMalloc += sqlite3_msize(pRet); + return pRet; +} + +/* +** Free buffer pFree, which must have been allocated by an earlier +** call to sessionMalloc64(). If pSession is not NULL, decrease the +** sqlite3_session.nMalloc counter by the number of bytes freed. +*/ +static void sessionFree(sqlite3_session *pSession, void *pFree){ + if( pSession ) pSession->nMalloc -= sqlite3_msize(pFree); + sqlite3_free(pFree); +} + +/* +** This macro is used to calculate hash key values for data structures. In +** order to use this macro, the entire data structure must be represented +** as a series of unsigned integers. In order to calculate a hash-key value +** for a data structure represented as three such integers, the macro may +** then be used as follows: +** +** int hash_key_value; +** hash_key_value = HASH_APPEND(0, ); +** hash_key_value = HASH_APPEND(hash_key_value, ); +** hash_key_value = HASH_APPEND(hash_key_value, ); +** +** In practice, the data structures this macro is used for are the primary +** key values of modified rows. +*/ +#define HASH_APPEND(hash, add) ((hash) << 3) ^ (hash) ^ (unsigned int)(add) + +/* +** Append the hash of the 64-bit integer passed as the second argument to the +** hash-key value passed as the first. Return the new hash-key value. +*/ +static unsigned int sessionHashAppendI64(unsigned int h, i64 i){ + h = HASH_APPEND(h, i & 0xFFFFFFFF); + return HASH_APPEND(h, (i>>32)&0xFFFFFFFF); +} + +/* +** Append the hash of the blob passed via the second and third arguments to +** the hash-key value passed as the first. Return the new hash-key value. +*/ +static unsigned int sessionHashAppendBlob(unsigned int h, int n, const u8 *z){ + int i; + for(i=0; inTotalCol==pSession->hook.xCount(pSession->hook.pCtx) ); + if( pTab->bRowid ){ + h = sessionHashAppendI64(h, iRowid); + }else{ + assert( *pbNullPK==0 ); + for(i=0; inCol; i++){ + if( pTab->abPK[i] ){ + int rc; + int eType; + sqlite3_value *pVal; + int iIdx = pTab->aiIdx[i]; + + if( bNew ){ + rc = pSession->hook.xNew(pSession->hook.pCtx, iIdx, &pVal); + }else{ + rc = pSession->hook.xOld(pSession->hook.pCtx, iIdx, &pVal); + } + if( rc!=SQLITE_OK ) return rc; + + eType = sqlite3_value_type(pVal); + h = sessionHashAppendType(h, eType); + if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){ + i64 iVal; + if( eType==SQLITE_INTEGER ){ + iVal = sqlite3_value_int64(pVal); + }else{ + double rVal = sqlite3_value_double(pVal); + assert( sizeof(iVal)==8 && sizeof(rVal)==8 ); + memcpy(&iVal, &rVal, 8); + } + h = sessionHashAppendI64(h, iVal); + }else if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){ + const u8 *z; + int n; + if( eType==SQLITE_TEXT ){ + z = (const u8 *)sqlite3_value_text(pVal); + }else{ + z = (const u8 *)sqlite3_value_blob(pVal); + } + n = sqlite3_value_bytes(pVal); + if( !z && (eType!=SQLITE_BLOB || n>0) ) return SQLITE_NOMEM; + h = sessionHashAppendBlob(h, n, z); + }else{ + assert( eType==SQLITE_NULL ); + assert( pTab->bStat1==0 || i!=1 ); + *pbNullPK = 1; + } + } + } + } + + *piHash = (h % pTab->nChange); + return SQLITE_OK; +} + +/* +** The buffer that the argument points to contains a serialized SQL value. +** Return the number of bytes of space occupied by the value (including +** the type byte). +*/ +static int sessionSerialLen(const u8 *a){ + int e; + int n; + assert( a!=0 ); + e = *a; + if( e==0 || e==0xFF ) return 1; + if( e==SQLITE_NULL ) return 1; + if( e==SQLITE_INTEGER || e==SQLITE_FLOAT ) return 9; + return sessionVarintGet(&a[1], &n) + 1 + n; +} + +/* +** Based on the primary key values stored in change aRecord, calculate a +** hash key. Assume the has table has nBucket buckets. The hash keys +** calculated by this function are compatible with those calculated by +** sessionPreupdateHash(). +** +** The bPkOnly argument is non-zero if the record at aRecord[] is from +** a patchset DELETE. In this case the non-PK fields are omitted entirely. +*/ +static unsigned int sessionChangeHash( + SessionTable *pTab, /* Table handle */ + int bPkOnly, /* Record consists of PK fields only */ + u8 *aRecord, /* Change record */ + int nBucket /* Assume this many buckets in hash table */ +){ + unsigned int h = 0; /* Value to return */ + int i; /* Used to iterate through columns */ + u8 *a = aRecord; /* Used to iterate through change record */ + + for(i=0; inCol; i++){ + int eType = *a; + int isPK = pTab->abPK[i]; + if( bPkOnly && isPK==0 ) continue; + + /* It is not possible for eType to be SQLITE_NULL here. The session + ** module does not record changes for rows with NULL values stored in + ** primary key columns. */ + assert( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT + || eType==SQLITE_TEXT || eType==SQLITE_BLOB + || eType==SQLITE_NULL || eType==0 + ); + assert( !isPK || (eType!=0 && eType!=SQLITE_NULL) ); + + if( isPK ){ + a++; + h = sessionHashAppendType(h, eType); + if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){ + h = sessionHashAppendI64(h, sessionGetI64(a)); + a += 8; + }else{ + int n; + a += sessionVarintGet(a, &n); + h = sessionHashAppendBlob(h, n, a); + a += n; + } + }else{ + a += sessionSerialLen(a); + } + } + return (h % nBucket); +} + +/* +** Arguments aLeft and aRight are pointers to change records for table pTab. +** This function returns true if the two records apply to the same row (i.e. +** have the same values stored in the primary key columns), or false +** otherwise. +*/ +static int sessionChangeEqual( + SessionTable *pTab, /* Table used for PK definition */ + int bLeftPkOnly, /* True if aLeft[] contains PK fields only */ + u8 *aLeft, /* Change record */ + int bRightPkOnly, /* True if aRight[] contains PK fields only */ + u8 *aRight /* Change record */ +){ + u8 *a1 = aLeft; /* Cursor to iterate through aLeft */ + u8 *a2 = aRight; /* Cursor to iterate through aRight */ + int iCol; /* Used to iterate through table columns */ + + for(iCol=0; iColnCol; iCol++){ + if( pTab->abPK[iCol] ){ + int n1 = sessionSerialLen(a1); + int n2 = sessionSerialLen(a2); + + if( n1!=n2 || memcmp(a1, a2, n1) ){ + return 0; + } + a1 += n1; + a2 += n2; + }else{ + if( bLeftPkOnly==0 ) a1 += sessionSerialLen(a1); + if( bRightPkOnly==0 ) a2 += sessionSerialLen(a2); + } + } + + return 1; +} + +/* +** Arguments aLeft and aRight both point to buffers containing change +** records with nCol columns. This function "merges" the two records into +** a single records which is written to the buffer at *paOut. *paOut is +** then set to point to one byte after the last byte written before +** returning. +** +** The merging of records is done as follows: For each column, if the +** aRight record contains a value for the column, copy the value from +** their. Otherwise, if aLeft contains a value, copy it. If neither +** record contains a value for a given column, then neither does the +** output record. +*/ +static void sessionMergeRecord( + u8 **paOut, + int nCol, + u8 *aLeft, + u8 *aRight +){ + u8 *a1 = aLeft; /* Cursor used to iterate through aLeft */ + u8 *a2 = aRight; /* Cursor used to iterate through aRight */ + u8 *aOut = *paOut; /* Output cursor */ + int iCol; /* Used to iterate from 0 to nCol */ + + for(iCol=0; iColnCol; i++){ + int nOld; + u8 *aOld; + int nNew; + u8 *aNew; + + aOld = sessionMergeValue(&aOld1, &aOld2, &nOld); + aNew = sessionMergeValue(&aNew1, &aNew2, &nNew); + if( pTab->abPK[i] || nOld!=nNew || memcmp(aOld, aNew, nNew) ){ + if( pTab->abPK[i]==0 ) bRequired = 1; + memcpy(aOut, aOld, nOld); + aOut += nOld; + }else{ + *(aOut++) = '\0'; + } + } + + if( !bRequired ) return 0; + } + + /* Write the new.* vector */ + aOld1 = aOldRecord1; + aOld2 = aOldRecord2; + aNew1 = aNewRecord1; + aNew2 = aNewRecord2; + for(i=0; inCol; i++){ + int nOld; + u8 *aOld; + int nNew; + u8 *aNew; + + aOld = sessionMergeValue(&aOld1, &aOld2, &nOld); + aNew = sessionMergeValue(&aNew1, &aNew2, &nNew); + if( bPatchset==0 + && (pTab->abPK[i] || (nOld==nNew && 0==memcmp(aOld, aNew, nNew))) + ){ + *(aOut++) = '\0'; + }else{ + memcpy(aOut, aNew, nNew); + aOut += nNew; + } + } + + *paOut = aOut; + return 1; +} + +/* +** This function is only called from within a pre-update-hook callback. +** It determines if the current pre-update-hook change affects the same row +** as the change stored in argument pChange. If so, it returns true. Otherwise +** if the pre-update-hook does not affect the same row as pChange, it returns +** false. +*/ +static int sessionPreupdateEqual( + sqlite3_session *pSession, /* Session object that owns SessionTable */ + i64 iRowid, /* Rowid value if pTab->bRowid */ + SessionTable *pTab, /* Table associated with change */ + SessionChange *pChange, /* Change to compare to */ + int op /* Current pre-update operation */ +){ + int iCol; /* Used to iterate through columns */ + u8 *a = pChange->aRecord; /* Cursor used to scan change record */ + + if( pTab->bRowid ){ + if( a[0]!=SQLITE_INTEGER ) return 0; + return sessionGetI64(&a[1])==iRowid; + } + + assert( op==SQLITE_INSERT || op==SQLITE_UPDATE || op==SQLITE_DELETE ); + for(iCol=0; iColnCol; iCol++){ + if( !pTab->abPK[iCol] ){ + a += sessionSerialLen(a); + }else{ + sqlite3_value *pVal; /* Value returned by preupdate_new/old */ + int rc; /* Error code from preupdate_new/old */ + int eType = *a++; /* Type of value from change record */ + int iIdx = pTab->aiIdx[iCol]; + + /* The following calls to preupdate_new() and preupdate_old() can not + ** fail. This is because they cache their return values, and by the + ** time control flows to here they have already been called once from + ** within sessionPreupdateHash(). The first two asserts below verify + ** this (that the method has already been called). */ + if( op==SQLITE_INSERT ){ + /* assert( db->pPreUpdate->pNewUnpacked || db->pPreUpdate->aNew ); */ + rc = pSession->hook.xNew(pSession->hook.pCtx, iIdx, &pVal); + }else{ + /* assert( db->pPreUpdate->pUnpacked ); */ + rc = pSession->hook.xOld(pSession->hook.pCtx, iIdx, &pVal); + } + assert( rc==SQLITE_OK ); + (void)rc; /* Suppress warning about unused variable */ + if( sqlite3_value_type(pVal)!=eType ) return 0; + + /* A SessionChange object never has a NULL value in a PK column */ + assert( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT + || eType==SQLITE_BLOB || eType==SQLITE_TEXT + ); + + if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){ + i64 iVal = sessionGetI64(a); + a += 8; + if( eType==SQLITE_INTEGER ){ + if( sqlite3_value_int64(pVal)!=iVal ) return 0; + }else{ + double rVal; + assert( sizeof(iVal)==8 && sizeof(rVal)==8 ); + memcpy(&rVal, &iVal, 8); + if( sqlite3_value_double(pVal)!=rVal ) return 0; + } + }else{ + int n; + const u8 *z; + a += sessionVarintGet(a, &n); + if( sqlite3_value_bytes(pVal)!=n ) return 0; + if( eType==SQLITE_TEXT ){ + z = sqlite3_value_text(pVal); + }else{ + z = sqlite3_value_blob(pVal); + } + if( n>0 && memcmp(a, z, n) ) return 0; + a += n; + } + } + } + + return 1; +} + +/* +** If required, grow the hash table used to store changes on table pTab +** (part of the session pSession). If a fatal OOM error occurs, set the +** session object to failed and return SQLITE_ERROR. Otherwise, return +** SQLITE_OK. +** +** It is possible that a non-fatal OOM error occurs in this function. In +** that case the hash-table does not grow, but SQLITE_OK is returned anyway. +** Growing the hash table in this case is a performance optimization only, +** it is not required for correct operation. +*/ +static int sessionGrowHash( + sqlite3_session *pSession, /* For memory accounting. May be NULL */ + int bPatchset, + SessionTable *pTab +){ + if( pTab->nChange==0 || pTab->nEntry>=(pTab->nChange/2) ){ + int i; + SessionChange **apNew; + sqlite3_int64 nNew = 2*(sqlite3_int64)(pTab->nChange ? pTab->nChange : 128); + + apNew = (SessionChange**)sessionMalloc64( + pSession, sizeof(SessionChange*) * nNew + ); + if( apNew==0 ){ + if( pTab->nChange==0 ){ + return SQLITE_ERROR; + } + return SQLITE_OK; + } + memset(apNew, 0, sizeof(SessionChange *) * nNew); + + for(i=0; inChange; i++){ + SessionChange *p; + SessionChange *pNext; + for(p=pTab->apChange[i]; p; p=pNext){ + int bPkOnly = (p->op==SQLITE_DELETE && bPatchset); + int iHash = sessionChangeHash(pTab, bPkOnly, p->aRecord, nNew); + pNext = p->pNext; + p->pNext = apNew[iHash]; + apNew[iHash] = p; + } + } + + sessionFree(pSession, pTab->apChange); + pTab->nChange = nNew; + pTab->apChange = apNew; + } + + return SQLITE_OK; +} + +/* +** This function queries the database for the names of the columns of table +** zThis, in schema zDb. +** +** Otherwise, if they are not NULL, variable *pnCol is set to the number +** of columns in the database table and variable *pzTab is set to point to a +** nul-terminated copy of the table name. *pazCol (if not NULL) is set to +** point to an array of pointers to column names. And *pabPK (again, if not +** NULL) is set to point to an array of booleans - true if the corresponding +** column is part of the primary key. +** +** For example, if the table is declared as: +** +** CREATE TABLE tbl1(w, x DEFAULT 'abc', y, z, PRIMARY KEY(w, z)); +** +** Then the five output variables are populated as follows: +** +** *pnCol = 4 +** *pzTab = "tbl1" +** *pazCol = {"w", "x", "y", "z"} +** *pazDflt = {NULL, 'abc', NULL, NULL} +** *pabPK = {1, 0, 0, 1} +** +** All returned buffers are part of the same single allocation, which must +** be freed using sqlite3_free() by the caller +*/ +static int sessionTableInfo( + sqlite3_session *pSession, /* For memory accounting. May be NULL */ + sqlite3 *db, /* Database connection */ + const char *zDb, /* Name of attached database (e.g. "main") */ + const char *zThis, /* Table name */ + int *pnCol, /* OUT: number of columns */ + int *pnTotalCol, /* OUT: number of hidden columns */ + const char **pzTab, /* OUT: Copy of zThis */ + const char ***pazCol, /* OUT: Array of column names for table */ + const char ***pazDflt, /* OUT: Array of default value expressions */ + int **paiIdx, /* OUT: Array of xNew/xOld indexes */ + u8 **pabPK, /* OUT: Array of booleans - true for PK col */ + int *pbRowid /* OUT: True if only PK is a rowid */ +){ + char *zPragma; + sqlite3_stmt *pStmt; + int rc; + sqlite3_int64 nByte; + int nDbCol = 0; + int nThis; + int i; + u8 *pAlloc = 0; + char **azCol = 0; + char **azDflt = 0; + u8 *abPK = 0; + int *aiIdx = 0; + int bRowid = 0; /* Set to true to use rowid as PK */ + + assert( pazCol && pabPK ); + + *pazCol = 0; + *pabPK = 0; + *pnCol = 0; + if( pnTotalCol ) *pnTotalCol = 0; + if( paiIdx ) *paiIdx = 0; + if( pzTab ) *pzTab = 0; + if( pazDflt ) *pazDflt = 0; + + nThis = sqlite3Strlen30(zThis); + if( nThis==12 && 0==sqlite3_stricmp("sqlite_stat1", zThis) ){ + rc = sqlite3_table_column_metadata(db, zDb, zThis, 0, 0, 0, 0, 0, 0); + if( rc==SQLITE_OK ){ + /* For sqlite_stat1, pretend that (tbl,idx) is the PRIMARY KEY. */ + zPragma = sqlite3_mprintf( + "SELECT 0, 'tbl', '', 0, '', 1, 0 UNION ALL " + "SELECT 1, 'idx', '', 0, '', 2, 0 UNION ALL " + "SELECT 2, 'stat', '', 0, '', 0, 0" + ); + }else if( rc==SQLITE_ERROR ){ + zPragma = sqlite3_mprintf(""); + }else{ + return rc; + } + }else{ + zPragma = sqlite3_mprintf("PRAGMA '%q'.table_xinfo('%q')", zDb, zThis); + } + if( !zPragma ){ + return SQLITE_NOMEM; + } + + rc = sqlite3_prepare_v2(db, zPragma, -1, &pStmt, 0); + sqlite3_free(zPragma); + if( rc!=SQLITE_OK ){ + return rc; + } + + nByte = nThis + 1; + bRowid = (pbRowid!=0); + while( SQLITE_ROW==sqlite3_step(pStmt) ){ + nByte += sqlite3_column_bytes(pStmt, 1); /* name */ + nByte += sqlite3_column_bytes(pStmt, 4); /* dflt_value */ + if( sqlite3_column_int(pStmt, 6)==0 ){ /* !hidden */ + nDbCol++; + } + if( sqlite3_column_int(pStmt, 5) ) bRowid = 0; /* pk */ + } + if( nDbCol==0 ) bRowid = 0; + nDbCol += bRowid; + nByte += strlen(SESSIONS_ROWID); + rc = sqlite3_reset(pStmt); + + if( rc==SQLITE_OK ){ + nByte += nDbCol * (sizeof(const char *)*2 +sizeof(int)+sizeof(u8) + 1 + 1); + pAlloc = sessionMalloc64(pSession, nByte); + if( pAlloc==0 ){ + rc = SQLITE_NOMEM; + }else{ + memset(pAlloc, 0, nByte); + } + } + if( rc==SQLITE_OK ){ + azCol = (char **)pAlloc; + azDflt = (char**)&azCol[nDbCol]; + aiIdx = (int*)&azDflt[nDbCol]; + abPK = (u8 *)&aiIdx[nDbCol]; + pAlloc = &abPK[nDbCol]; + if( pzTab ){ + memcpy(pAlloc, zThis, nThis+1); + *pzTab = (char *)pAlloc; + pAlloc += nThis+1; + } + + i = 0; + if( bRowid ){ + size_t nName = strlen(SESSIONS_ROWID); + memcpy(pAlloc, SESSIONS_ROWID, nName+1); + azCol[i] = (char*)pAlloc; + pAlloc += nName+1; + abPK[i] = 1; + aiIdx[i] = -1; + i++; + } + while( SQLITE_ROW==sqlite3_step(pStmt) ){ + if( sqlite3_column_int(pStmt, 6)==0 ){ /* !hidden */ + int nName = sqlite3_column_bytes(pStmt, 1); + int nDflt = sqlite3_column_bytes(pStmt, 4); + const unsigned char *zName = sqlite3_column_text(pStmt, 1); + const unsigned char *zDflt = sqlite3_column_text(pStmt, 4); + + if( zName==0 ) break; + memcpy(pAlloc, zName, nName+1); + azCol[i] = (char *)pAlloc; + pAlloc += nName+1; + if( zDflt ){ + memcpy(pAlloc, zDflt, nDflt+1); + azDflt[i] = (char *)pAlloc; + pAlloc += nDflt+1; + }else{ + azDflt[i] = 0; + } + abPK[i] = sqlite3_column_int(pStmt, 5); + aiIdx[i] = sqlite3_column_int(pStmt, 0); + i++; + } + if( pnTotalCol ) (*pnTotalCol)++; + } + rc = sqlite3_reset(pStmt); + } + + /* If successful, populate the output variables. Otherwise, zero them and + ** free any allocation made. An error code will be returned in this case. + */ + if( rc==SQLITE_OK ){ + *pazCol = (const char**)azCol; + if( pazDflt ) *pazDflt = (const char**)azDflt; + *pabPK = abPK; + *pnCol = nDbCol; + if( paiIdx ) *paiIdx = aiIdx; + }else{ + sessionFree(pSession, azCol); + } + if( pbRowid ) *pbRowid = bRowid; + sqlite3_finalize(pStmt); + return rc; +} + +/* +** This function is called to initialize the SessionTable.nCol, azCol[] +** abPK[] and azDflt[] members of SessionTable object pTab. If these +** fields are already initialized, this function is a no-op. +** +** If an error occurs, an error code is stored in sqlite3_session.rc and +** non-zero returned. Or, if no error occurs but the table has no primary +** key, sqlite3_session.rc is left set to SQLITE_OK and non-zero returned to +** indicate that updates on this table should be ignored. SessionTable.abPK +** is set to NULL in this case. +*/ +static int sessionInitTable( + sqlite3_session *pSession, /* Optional session handle */ + SessionTable *pTab, /* Table object to initialize */ + sqlite3 *db, /* Database handle to read schema from */ + const char *zDb /* Name of db - "main", "temp" etc. */ +){ + int rc = SQLITE_OK; + + if( pTab->nCol==0 ){ + u8 *abPK; + assert( pTab->azCol==0 || pTab->abPK==0 ); + sqlite3_free(pTab->azCol); + pTab->abPK = 0; + rc = sessionTableInfo(pSession, db, zDb, + pTab->zName, &pTab->nCol, &pTab->nTotalCol, 0, &pTab->azCol, + &pTab->azDflt, &pTab->aiIdx, &abPK, + ((pSession==0 || pSession->bImplicitPK) ? &pTab->bRowid : 0) + ); + if( rc==SQLITE_OK ){ + int i; + for(i=0; inCol; i++){ + if( abPK[i] ){ + pTab->abPK = abPK; + break; + } + } + if( 0==sqlite3_stricmp("sqlite_stat1", pTab->zName) ){ + pTab->bStat1 = 1; + } + + if( pSession && pSession->bEnableSize ){ + pSession->nMaxChangesetSize += ( + 1 + sessionVarintLen(pTab->nCol) + pTab->nCol + strlen(pTab->zName)+1 + ); + } + } + } + + if( pSession ){ + pSession->rc = rc; + return (rc || pTab->abPK==0); + } + return rc; +} + +/* +** Re-initialize table object pTab. +*/ +static int sessionReinitTable(sqlite3_session *pSession, SessionTable *pTab){ + int nCol = 0; + int nTotalCol = 0; + const char **azCol = 0; + const char **azDflt = 0; + int *aiIdx = 0; + u8 *abPK = 0; + int bRowid = 0; + + assert( pSession->rc==SQLITE_OK ); + + pSession->rc = sessionTableInfo(pSession, pSession->db, pSession->zDb, + pTab->zName, &nCol, &nTotalCol, 0, &azCol, &azDflt, &aiIdx, &abPK, + (pSession->bImplicitPK ? &bRowid : 0) + ); + if( pSession->rc==SQLITE_OK ){ + if( pTab->nCol>nCol || pTab->bRowid!=bRowid ){ + pSession->rc = SQLITE_SCHEMA; + }else{ + int ii; + int nOldCol = pTab->nCol; + for(ii=0; iinCol ){ + if( pTab->abPK[ii]!=abPK[ii] ){ + pSession->rc = SQLITE_SCHEMA; + } + }else if( abPK[ii] ){ + pSession->rc = SQLITE_SCHEMA; + } + } + + if( pSession->rc==SQLITE_OK ){ + const char **a = pTab->azCol; + pTab->azCol = azCol; + pTab->nCol = nCol; + pTab->nTotalCol = nTotalCol; + pTab->azDflt = azDflt; + pTab->abPK = abPK; + pTab->aiIdx = aiIdx; + azCol = a; + } + if( pSession->bEnableSize ){ + pSession->nMaxChangesetSize += (nCol - nOldCol); + pSession->nMaxChangesetSize += sessionVarintLen(nCol); + pSession->nMaxChangesetSize -= sessionVarintLen(nOldCol); + } + } + } + + sqlite3_free((char*)azCol); + return pSession->rc; +} + +/* +** Session-change object (*pp) contains an old.* record with fewer than +** nCol fields. This function updates it with the default values for +** the missing fields. +*/ +static void sessionUpdateOneChange( + sqlite3_session *pSession, /* For memory accounting */ + int *pRc, /* IN/OUT: Error code */ + SessionChange **pp, /* IN/OUT: Change object to update */ + int nCol, /* Number of columns now in table */ + sqlite3_stmt *pDflt /* SELECT */ +){ + SessionChange *pOld = *pp; + + while( pOld->nRecordFieldnRecordField; + int eType = sqlite3_column_type(pDflt, iField); + switch( eType ){ + case SQLITE_NULL: + nIncr = 1; + break; + case SQLITE_INTEGER: + case SQLITE_FLOAT: + nIncr = 9; + break; + default: { + int n = sqlite3_column_bytes(pDflt, iField); + nIncr = 1 + sessionVarintLen(n) + n; + assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB ); + break; + } + } + + nByte = nIncr + (sizeof(SessionChange) + pOld->nRecord); + pNew = sessionMalloc64(pSession, nByte); + if( pNew==0 ){ + *pRc = SQLITE_NOMEM; + return; + }else{ + memcpy(pNew, pOld, sizeof(SessionChange)); + pNew->aRecord = (u8*)&pNew[1]; + memcpy(pNew->aRecord, pOld->aRecord, pOld->nRecord); + pNew->aRecord[pNew->nRecord++] = (u8)eType; + switch( eType ){ + case SQLITE_INTEGER: { + i64 iVal = sqlite3_column_int64(pDflt, iField); + sessionPutI64(&pNew->aRecord[pNew->nRecord], iVal); + pNew->nRecord += 8; + break; + } + + case SQLITE_FLOAT: { + double rVal = sqlite3_column_double(pDflt, iField); + i64 iVal = 0; + memcpy(&iVal, &rVal, sizeof(rVal)); + sessionPutI64(&pNew->aRecord[pNew->nRecord], iVal); + pNew->nRecord += 8; + break; + } + + case SQLITE_TEXT: { + int n = sqlite3_column_bytes(pDflt, iField); + const char *z = (const char*)sqlite3_column_text(pDflt, iField); + pNew->nRecord += sessionVarintPut(&pNew->aRecord[pNew->nRecord], n); + memcpy(&pNew->aRecord[pNew->nRecord], z, n); + pNew->nRecord += n; + break; + } + + case SQLITE_BLOB: { + int n = sqlite3_column_bytes(pDflt, iField); + const u8 *z = (const u8*)sqlite3_column_blob(pDflt, iField); + pNew->nRecord += sessionVarintPut(&pNew->aRecord[pNew->nRecord], n); + memcpy(&pNew->aRecord[pNew->nRecord], z, n); + pNew->nRecord += n; + break; + } + + default: + assert( eType==SQLITE_NULL ); + break; + } + + sessionFree(pSession, pOld); + *pp = pOld = pNew; + pNew->nRecordField++; + pNew->nMaxSize += nIncr; + if( pSession ){ + pSession->nMaxChangesetSize += nIncr; + } + } + } +} + +/* +** Ensure that there is room in the buffer to append nByte bytes of data. +** If not, use sqlite3_realloc() to grow the buffer so that there is. +** +** If successful, return zero. Otherwise, if an OOM condition is encountered, +** set *pRc to SQLITE_NOMEM and return non-zero. +*/ +static int sessionBufferGrow(SessionBuffer *p, i64 nByte, int *pRc){ +#define SESSION_MAX_BUFFER_SZ (0x7FFFFF00 - 1) + i64 nReq = p->nBuf + nByte; + if( *pRc==SQLITE_OK && nReq>p->nAlloc ){ + u8 *aNew; + i64 nNew = p->nAlloc ? p->nAlloc : 128; + + do { + nNew = nNew*2; + }while( nNewSESSION_MAX_BUFFER_SZ ){ + nNew = SESSION_MAX_BUFFER_SZ; + if( nNewaBuf, nNew); + if( 0==aNew ){ + *pRc = SQLITE_NOMEM; + }else{ + p->aBuf = aNew; + p->nAlloc = nNew; + } + } + return (*pRc!=SQLITE_OK); +} + + +/* +** This function is a no-op if *pRc is other than SQLITE_OK when it is +** called. Otherwise, append a string to the buffer. All bytes in the string +** up to (but not including) the nul-terminator are written to the buffer. +** +** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before +** returning. +*/ +static void sessionAppendStr( + SessionBuffer *p, + const char *zStr, + int *pRc +){ + int nStr = sqlite3Strlen30(zStr); + if( 0==sessionBufferGrow(p, nStr+1, pRc) ){ + memcpy(&p->aBuf[p->nBuf], zStr, nStr); + p->nBuf += nStr; + p->aBuf[p->nBuf] = 0x00; + } +} + +/* +** Format a string using printf() style formatting and then append it to the +** buffer using sessionAppendString(). +*/ +static void sessionAppendPrintf( + SessionBuffer *p, /* Buffer to append to */ + int *pRc, + const char *zFmt, + ... +){ + if( *pRc==SQLITE_OK ){ + char *zApp = 0; + va_list ap; + va_start(ap, zFmt); + zApp = sqlite3_vmprintf(zFmt, ap); + if( zApp==0 ){ + *pRc = SQLITE_NOMEM; + }else{ + sessionAppendStr(p, zApp, pRc); + } + va_end(ap); + sqlite3_free(zApp); + } +} + +/* +** Prepare a statement against database handle db that SELECTs a single +** row containing the default values for each column in table pTab. For +** example, if pTab is declared as: +** +** CREATE TABLE pTab(a PRIMARY KEY, b DEFAULT 123, c DEFAULT 'abcd'); +** +** Then this function prepares and returns the SQL statement: +** +** SELECT NULL, 123, 'abcd'; +*/ +static int sessionPrepareDfltStmt( + sqlite3 *db, /* Database handle */ + SessionTable *pTab, /* Table to prepare statement for */ + sqlite3_stmt **ppStmt /* OUT: Statement handle */ +){ + SessionBuffer sql = {0,0,0}; + int rc = SQLITE_OK; + const char *zSep = " "; + int ii = 0; + + *ppStmt = 0; + sessionAppendPrintf(&sql, &rc, "SELECT"); + for(ii=0; iinCol; ii++){ + const char *zDflt = pTab->azDflt[ii] ? pTab->azDflt[ii] : "NULL"; + sessionAppendPrintf(&sql, &rc, "%s%s", zSep, zDflt); + zSep = ", "; + } + if( rc==SQLITE_OK ){ + rc = sqlite3_prepare_v2(db, (const char*)sql.aBuf, -1, ppStmt, 0); + } + sqlite3_free(sql.aBuf); + + return rc; +} + +/* +** Table pTab has one or more existing change-records with old.* records +** with fewer than pTab->nCol columns. This function updates all such +** change-records with the default values for the missing columns. +*/ +static int sessionUpdateChanges(sqlite3_session *pSession, SessionTable *pTab){ + sqlite3_stmt *pStmt = 0; + int rc = pSession->rc; + + rc = sessionPrepareDfltStmt(pSession->db, pTab, &pStmt); + if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){ + int ii = 0; + SessionChange **pp = 0; + for(ii=0; iinChange; ii++){ + for(pp=&pTab->apChange[ii]; *pp; pp=&((*pp)->pNext)){ + if( (*pp)->nRecordField!=pTab->nCol ){ + sessionUpdateOneChange(pSession, &rc, pp, pTab->nCol, pStmt); + } + } + } + } + + pSession->rc = rc; + rc = sqlite3_finalize(pStmt); + if( pSession->rc==SQLITE_OK ) pSession->rc = rc; + return pSession->rc; +} + +/* +** Versions of the four methods in object SessionHook for use with the +** sqlite_stat1 table. The purpose of this is to substitute a zero-length +** blob each time a NULL value is read from the "idx" column of the +** sqlite_stat1 table. +*/ +typedef struct SessionStat1Ctx SessionStat1Ctx; +struct SessionStat1Ctx { + SessionHook hook; + sqlite3_session *pSession; +}; +static int sessionStat1Old(void *pCtx, int iCol, sqlite3_value **ppVal){ + SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx; + sqlite3_value *pVal = 0; + int rc = p->hook.xOld(p->hook.pCtx, iCol, &pVal); + if( rc==SQLITE_OK && iCol==1 && sqlite3_value_type(pVal)==SQLITE_NULL ){ + pVal = p->pSession->pZeroBlob; + } + *ppVal = pVal; + return rc; +} +static int sessionStat1New(void *pCtx, int iCol, sqlite3_value **ppVal){ + SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx; + sqlite3_value *pVal = 0; + int rc = p->hook.xNew(p->hook.pCtx, iCol, &pVal); + if( rc==SQLITE_OK && iCol==1 && sqlite3_value_type(pVal)==SQLITE_NULL ){ + pVal = p->pSession->pZeroBlob; + } + *ppVal = pVal; + return rc; +} +static int sessionStat1Count(void *pCtx){ + SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx; + return p->hook.xCount(p->hook.pCtx); +} +static int sessionStat1Depth(void *pCtx){ + SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx; + return p->hook.xDepth(p->hook.pCtx); +} + +static int sessionUpdateMaxSize( + int op, + sqlite3_session *pSession, /* Session object pTab is attached to */ + SessionTable *pTab, /* Table that change applies to */ + SessionChange *pC /* Update pC->nMaxSize */ +){ + i64 nNew = 2; + if( pC->op==SQLITE_INSERT ){ + if( pTab->bRowid ) nNew += 9; + if( op!=SQLITE_DELETE ){ + int ii; + for(ii=0; iinCol; ii++){ + sqlite3_value *p = 0; + pSession->hook.xNew(pSession->hook.pCtx, pTab->aiIdx[ii], &p); + sessionSerializeValue(0, p, &nNew); + } + } + }else if( op==SQLITE_DELETE ){ + nNew += pC->nRecord; + if( sqlite3_preupdate_blobwrite(pSession->db)>=0 ){ + nNew += pC->nRecord; + } + }else{ + int ii; + u8 *pCsr = pC->aRecord; + if( pTab->bRowid ){ + nNew += 9 + 1; + pCsr += 9; + } + for(ii=pTab->bRowid; iinCol; ii++){ + int bChanged = 1; + int nOld = 0; + int eType; + int iIdx = pTab->aiIdx[ii]; + sqlite3_value *p = 0; + pSession->hook.xNew(pSession->hook.pCtx, iIdx, &p); + if( p==0 ){ + return SQLITE_NOMEM; + } + + eType = *pCsr++; + switch( eType ){ + case SQLITE_NULL: + bChanged = sqlite3_value_type(p)!=SQLITE_NULL; + break; + + case SQLITE_FLOAT: + case SQLITE_INTEGER: { + if( eType==sqlite3_value_type(p) ){ + sqlite3_int64 iVal = sessionGetI64(pCsr); + if( eType==SQLITE_INTEGER ){ + bChanged = (iVal!=sqlite3_value_int64(p)); + }else{ + double dVal; + memcpy(&dVal, &iVal, 8); + bChanged = (dVal!=sqlite3_value_double(p)); + } + } + nOld = 8; + pCsr += 8; + break; + } + + default: { + int nByte; + nOld = sessionVarintGet(pCsr, &nByte); + pCsr += nOld; + nOld += nByte; + assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB ); + if( eType==sqlite3_value_type(p) + && nByte==sqlite3_value_bytes(p) + && (nByte==0 || 0==memcmp(pCsr, sqlite3_value_blob(p), nByte)) + ){ + bChanged = 0; + } + pCsr += nByte; + break; + } + } + + if( bChanged && pTab->abPK[ii] ){ + nNew = pC->nRecord + 2; + break; + } + + if( bChanged ){ + nNew += 1 + nOld; + sessionSerializeValue(0, p, &nNew); + }else if( pTab->abPK[ii] ){ + nNew += 2 + nOld; + }else{ + nNew += 2; + } + } + } + + if( nNew>pC->nMaxSize ){ + int nIncr = nNew - pC->nMaxSize; + pC->nMaxSize = nNew; + pSession->nMaxChangesetSize += nIncr; + } + return SQLITE_OK; +} + +/* +** This function is only called from with a pre-update-hook reporting a +** change on table pTab (attached to session pSession). The type of change +** (UPDATE, INSERT, DELETE) is specified by the first argument. +** +** Unless one is already present or an error occurs, an entry is added +** to the changed-rows hash table associated with table pTab. +*/ +static void sessionPreupdateOneChange( + int op, /* One of SQLITE_UPDATE, INSERT, DELETE */ + i64 iRowid, + sqlite3_session *pSession, /* Session object pTab is attached to */ + SessionTable *pTab /* Table that change applies to */ +){ + int iHash; + int bNull = 0; + int rc = SQLITE_OK; + int nExpect = 0; + SessionStat1Ctx stat1 = {{0,0,0,0,0},0}; + + if( pSession->rc ) return; + + /* Load table details if required */ + if( sessionInitTable(pSession, pTab, pSession->db, pSession->zDb) ) return; + + /* Check the number of columns in this xPreUpdate call matches the + ** number of columns in the table. */ + nExpect = pSession->hook.xCount(pSession->hook.pCtx); + if( pTab->nTotalColnTotalCol!=nExpect ){ + pSession->rc = SQLITE_SCHEMA; + return; + } + + /* Grow the hash table if required */ + if( sessionGrowHash(pSession, 0, pTab) ){ + pSession->rc = SQLITE_NOMEM; + return; + } + + if( pTab->bStat1 ){ + stat1.hook = pSession->hook; + stat1.pSession = pSession; + pSession->hook.pCtx = (void*)&stat1; + pSession->hook.xNew = sessionStat1New; + pSession->hook.xOld = sessionStat1Old; + pSession->hook.xCount = sessionStat1Count; + pSession->hook.xDepth = sessionStat1Depth; + if( pSession->pZeroBlob==0 ){ + sqlite3_value *p = sqlite3ValueNew(0); + if( p==0 ){ + rc = SQLITE_NOMEM; + goto error_out; + } + sqlite3ValueSetStr(p, 0, "", 0, SQLITE_STATIC); + pSession->pZeroBlob = p; + } + } + + /* Calculate the hash-key for this change. If the primary key of the row + ** includes a NULL value, exit early. Such changes are ignored by the + ** session module. */ + rc = sessionPreupdateHash( + pSession, iRowid, pTab, op==SQLITE_INSERT, &iHash, &bNull + ); + if( rc!=SQLITE_OK ) goto error_out; + + if( bNull==0 ){ + /* Search the hash table for an existing record for this row. */ + SessionChange *pC; + for(pC=pTab->apChange[iHash]; pC; pC=pC->pNext){ + if( sessionPreupdateEqual(pSession, iRowid, pTab, pC, op) ) break; + } + + if( pC==0 ){ + /* Create a new change object containing all the old values (if + ** this is an SQLITE_UPDATE or SQLITE_DELETE), or just the PK + ** values (if this is an INSERT). */ + sqlite3_int64 nByte; /* Number of bytes to allocate */ + int i; /* Used to iterate through columns */ + + assert( rc==SQLITE_OK ); + pTab->nEntry++; + + /* Figure out how large an allocation is required */ + nByte = sizeof(SessionChange); + for(i=pTab->bRowid; inCol; i++){ + int iIdx = pTab->aiIdx[i]; + sqlite3_value *p = 0; + if( op!=SQLITE_INSERT ){ + /* This may fail if the column has a non-NULL default and was added + ** using ALTER TABLE ADD COLUMN after this record was created. */ + rc = pSession->hook.xOld(pSession->hook.pCtx, iIdx, &p); + }else if( pTab->abPK[i] ){ + TESTONLY(int trc = ) pSession->hook.xNew(pSession->hook.pCtx,iIdx,&p); + assert( trc==SQLITE_OK ); + } + + if( rc==SQLITE_OK ){ + /* This may fail if SQLite value p contains a utf-16 string that must + ** be converted to utf-8 and an OOM error occurs while doing so. */ + rc = sessionSerializeValue(0, p, &nByte); + } + if( rc!=SQLITE_OK ) goto error_out; + } + if( pTab->bRowid ){ + nByte += 9; /* Size of rowid field - an integer */ + } + + /* Allocate the change object */ + pC = (SessionChange*)sessionMalloc64(pSession, nByte); + if( !pC ){ + rc = SQLITE_NOMEM; + goto error_out; + }else{ + memset(pC, 0, sizeof(SessionChange)); + pC->aRecord = (u8 *)&pC[1]; + } + + /* Populate the change object. None of the preupdate_old(), + ** preupdate_new() or SerializeValue() calls below may fail as all + ** required values and encodings have already been cached in memory. + ** It is not possible for an OOM to occur in this block. */ + nByte = 0; + if( pTab->bRowid ){ + pC->aRecord[0] = SQLITE_INTEGER; + sessionPutI64(&pC->aRecord[1], iRowid); + nByte = 9; + } + for(i=pTab->bRowid; inCol; i++){ + sqlite3_value *p = 0; + int iIdx = pTab->aiIdx[i]; + if( op!=SQLITE_INSERT ){ + pSession->hook.xOld(pSession->hook.pCtx, iIdx, &p); + }else if( pTab->abPK[i] ){ + pSession->hook.xNew(pSession->hook.pCtx, iIdx, &p); + } + sessionSerializeValue(&pC->aRecord[nByte], p, &nByte); + } + + /* Add the change to the hash-table */ + if( pSession->bIndirect || pSession->hook.xDepth(pSession->hook.pCtx) ){ + pC->bIndirect = 1; + } + pC->nRecordField = pTab->nCol; + pC->nRecord = nByte; + pC->op = op; + pC->pNext = pTab->apChange[iHash]; + pTab->apChange[iHash] = pC; + + }else if( pC->bIndirect ){ + /* If the existing change is considered "indirect", but this current + ** change is "direct", mark the change object as direct. */ + if( pSession->hook.xDepth(pSession->hook.pCtx)==0 + && pSession->bIndirect==0 + ){ + pC->bIndirect = 0; + } + } + + assert( rc==SQLITE_OK ); + if( pSession->bEnableSize ){ + rc = sessionUpdateMaxSize(op, pSession, pTab, pC); + } + } + + + /* If an error has occurred, mark the session object as failed. */ + error_out: + if( pTab->bStat1 ){ + pSession->hook = stat1.hook; + } + if( rc!=SQLITE_OK ){ + pSession->rc = rc; + } +} + +static int sessionFindTable( + sqlite3_session *pSession, + const char *zName, + SessionTable **ppTab +){ + int rc = SQLITE_OK; + int nName = sqlite3Strlen30(zName); + SessionTable *pRet; + + /* Search for an existing table */ + for(pRet=pSession->pTable; pRet; pRet=pRet->pNext){ + if( 0==sqlite3_strnicmp(pRet->zName, zName, nName+1) ) break; + } + + if( pRet==0 && pSession->bAutoAttach ){ + /* If there is a table-filter configured, invoke it. If it returns 0, + ** do not automatically add the new table. */ + if( pSession->xTableFilter==0 + || pSession->xTableFilter(pSession->pFilterCtx, zName) + ){ + rc = sqlite3session_attach(pSession, zName); + if( rc==SQLITE_OK ){ + pRet = pSession->pTable; + while( ALWAYS(pRet) && pRet->pNext ){ + pRet = pRet->pNext; + } + assert( pRet!=0 ); + assert( 0==sqlite3_strnicmp(pRet->zName, zName, nName+1) ); + } + } + } + + assert( rc==SQLITE_OK || pRet==0 ); + *ppTab = pRet; + return rc; +} + +/* +** The 'pre-update' hook registered by this module with SQLite databases. +*/ +static void xPreUpdate( + void *pCtx, /* Copy of third arg to preupdate_hook() */ + sqlite3 *db, /* Database handle */ + int op, /* SQLITE_UPDATE, DELETE or INSERT */ + char const *zDb, /* Database name */ + char const *zName, /* Table name */ + sqlite3_int64 iKey1, /* Rowid of row about to be deleted/updated */ + sqlite3_int64 iKey2 /* New rowid value (for a rowid UPDATE) */ +){ + sqlite3_session *pSession; + int nDb = sqlite3Strlen30(zDb); + + assert( sqlite3_mutex_held(db->mutex) ); + (void)iKey1; + (void)iKey2; + + for(pSession=(sqlite3_session *)pCtx; pSession; pSession=pSession->pNext){ + SessionTable *pTab; + + /* If this session is attached to a different database ("main", "temp" + ** etc.), or if it is not currently enabled, there is nothing to do. Skip + ** to the next session object attached to this database. */ + if( pSession->bEnable==0 ) continue; + if( pSession->rc ) continue; + if( sqlite3_strnicmp(zDb, pSession->zDb, nDb+1) ) continue; + + pSession->rc = sessionFindTable(pSession, zName, &pTab); + if( pTab ){ + assert( pSession->rc==SQLITE_OK ); + assert( op==SQLITE_UPDATE || iKey1==iKey2 ); + sessionPreupdateOneChange(op, iKey1, pSession, pTab); + if( op==SQLITE_UPDATE ){ + sessionPreupdateOneChange(SQLITE_INSERT, iKey2, pSession, pTab); + } + } + } +} + +/* +** The pre-update hook implementations. +*/ +static int sessionPreupdateOld(void *pCtx, int iVal, sqlite3_value **ppVal){ + return sqlite3_preupdate_old((sqlite3*)pCtx, iVal, ppVal); +} +static int sessionPreupdateNew(void *pCtx, int iVal, sqlite3_value **ppVal){ + return sqlite3_preupdate_new((sqlite3*)pCtx, iVal, ppVal); +} +static int sessionPreupdateCount(void *pCtx){ + return sqlite3_preupdate_count((sqlite3*)pCtx); +} +static int sessionPreupdateDepth(void *pCtx){ + return sqlite3_preupdate_depth((sqlite3*)pCtx); +} + +/* +** Install the pre-update hooks on the session object passed as the only +** argument. +*/ +static void sessionPreupdateHooks( + sqlite3_session *pSession +){ + pSession->hook.pCtx = (void*)pSession->db; + pSession->hook.xOld = sessionPreupdateOld; + pSession->hook.xNew = sessionPreupdateNew; + pSession->hook.xCount = sessionPreupdateCount; + pSession->hook.xDepth = sessionPreupdateDepth; +} + +typedef struct SessionDiffCtx SessionDiffCtx; +struct SessionDiffCtx { + sqlite3_stmt *pStmt; + int bRowid; + int nOldOff; +}; + +/* +** The diff hook implementations. +*/ +static int sessionDiffOld(void *pCtx, int iVal, sqlite3_value **ppVal){ + SessionDiffCtx *p = (SessionDiffCtx*)pCtx; + *ppVal = sqlite3_column_value(p->pStmt, iVal+p->nOldOff+p->bRowid); + return SQLITE_OK; +} +static int sessionDiffNew(void *pCtx, int iVal, sqlite3_value **ppVal){ + SessionDiffCtx *p = (SessionDiffCtx*)pCtx; + *ppVal = sqlite3_column_value(p->pStmt, iVal+p->bRowid); + return SQLITE_OK; +} +static int sessionDiffCount(void *pCtx){ + SessionDiffCtx *p = (SessionDiffCtx*)pCtx; + return (p->nOldOff ? p->nOldOff : sqlite3_column_count(p->pStmt)) - p->bRowid; +} +static int sessionDiffDepth(void *pCtx){ + (void)pCtx; + return 0; +} + +/* +** Install the diff hooks on the session object passed as the only +** argument. +*/ +static void sessionDiffHooks( + sqlite3_session *pSession, + SessionDiffCtx *pDiffCtx +){ + pSession->hook.pCtx = (void*)pDiffCtx; + pSession->hook.xOld = sessionDiffOld; + pSession->hook.xNew = sessionDiffNew; + pSession->hook.xCount = sessionDiffCount; + pSession->hook.xDepth = sessionDiffDepth; +} + +static char *sessionExprComparePK( + int nCol, + const char *zDb1, const char *zDb2, + const char *zTab, + const char **azCol, u8 *abPK +){ + int i; + const char *zSep = ""; + char *zRet = 0; + + for(i=0; ibRowid, pTab->zName, zExpr + ); + + if( zStmt==0 ){ + rc = SQLITE_NOMEM; + }else{ + sqlite3_stmt *pStmt; + rc = sqlite3_prepare(pSession->db, zStmt, -1, &pStmt, 0); + if( rc==SQLITE_OK ){ + SessionDiffCtx *pDiffCtx = (SessionDiffCtx*)pSession->hook.pCtx; + pDiffCtx->pStmt = pStmt; + pDiffCtx->nOldOff = 0; + pDiffCtx->bRowid = pTab->bRowid; + while( SQLITE_ROW==sqlite3_step(pStmt) ){ + i64 iRowid = (pTab->bRowid ? sqlite3_column_int64(pStmt, 0) : 0); + sessionPreupdateOneChange(op, iRowid, pSession, pTab); + } + rc = sqlite3_finalize(pStmt); + } + sqlite3_free(zStmt); + } + + return rc; +} + +/* +** Return a comma-separated list of the fully-qualified (with both database +** and table name) column names from table pTab. e.g. +** +** "main"."t1"."a", "main"."t1"."b", "main"."t1"."c" +*/ +static char *sessionAllCols( + const char *zDb, + SessionTable *pTab +){ + int ii; + char *zRet = 0; + for(ii=0; iinCol; ii++){ + zRet = sqlite3_mprintf("%z%s\"%w\".\"%w\".\"%w\"", + zRet, (zRet ? ", " : ""), zDb, pTab->zName, pTab->azCol[ii] + ); + if( !zRet ) break; + } + return zRet; +} + +static int sessionDiffFindModified( + sqlite3_session *pSession, + SessionTable *pTab, + const char *zFrom, + const char *zExpr +){ + int rc = SQLITE_OK; + + char *zExpr2 = sessionExprCompareOther(pTab->nCol, + pSession->zDb, zFrom, pTab->zName, pTab->azCol, pTab->abPK + ); + if( zExpr2==0 ){ + rc = SQLITE_NOMEM; + }else{ + char *z1 = sessionAllCols(pSession->zDb, pTab); + char *z2 = sessionAllCols(zFrom, pTab); + char *zStmt = sqlite3_mprintf( + "SELECT %s,%s FROM \"%w\".\"%w\", \"%w\".\"%w\" WHERE %s AND (%z)", + z1, z2, pSession->zDb, pTab->zName, zFrom, pTab->zName, zExpr, zExpr2 + ); + if( zStmt==0 || z1==0 || z2==0 ){ + rc = SQLITE_NOMEM; + }else{ + sqlite3_stmt *pStmt; + rc = sqlite3_prepare(pSession->db, zStmt, -1, &pStmt, 0); + + if( rc==SQLITE_OK ){ + SessionDiffCtx *pDiffCtx = (SessionDiffCtx*)pSession->hook.pCtx; + pDiffCtx->pStmt = pStmt; + pDiffCtx->nOldOff = pTab->nCol; + while( SQLITE_ROW==sqlite3_step(pStmt) ){ + i64 iRowid = (pTab->bRowid ? sqlite3_column_int64(pStmt, 0) : 0); + sessionPreupdateOneChange(SQLITE_UPDATE, iRowid, pSession, pTab); + } + rc = sqlite3_finalize(pStmt); + } + } + sqlite3_free(zStmt); + sqlite3_free(z1); + sqlite3_free(z2); + } + + return rc; +} + +int sqlite3session_diff( + sqlite3_session *pSession, + const char *zFrom, + const char *zTbl, + char **pzErrMsg +){ + const char *zDb = pSession->zDb; + int rc = pSession->rc; + SessionDiffCtx d; + + memset(&d, 0, sizeof(d)); + sessionDiffHooks(pSession, &d); + + sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db)); + if( pzErrMsg ) *pzErrMsg = 0; + if( rc==SQLITE_OK ){ + char *zExpr = 0; + sqlite3 *db = pSession->db; + SessionTable *pTo; /* Table zTbl */ + + /* Locate and if necessary initialize the target table object */ + pSession->bAutoAttach++; + rc = sessionFindTable(pSession, zTbl, &pTo); + pSession->bAutoAttach--; + if( pTo==0 ) goto diff_out; + if( sessionInitTable(pSession, pTo, pSession->db, pSession->zDb) ){ + rc = pSession->rc; + goto diff_out; + } + + /* Check the table schemas match */ + if( rc==SQLITE_OK ){ + int bHasPk = 0; + int bMismatch = 0; + int nCol = 0; /* Columns in zFrom.zTbl */ + int bRowid = 0; + u8 *abPK = 0; + const char **azCol = 0; + char *zDbExists = 0; + + /* Check that database zFrom is attached. */ + zDbExists = sqlite3_mprintf("SELECT * FROM %Q.sqlite_schema", zFrom); + if( zDbExists==0 ){ + rc = SQLITE_NOMEM; + }else{ + sqlite3_stmt *pDbExists = 0; + rc = sqlite3_prepare_v2(db, zDbExists, -1, &pDbExists, 0); + if( rc==SQLITE_ERROR ){ + rc = SQLITE_OK; + nCol = -1; + } + sqlite3_finalize(pDbExists); + sqlite3_free(zDbExists); + } + + if( rc==SQLITE_OK && nCol==0 ){ + rc = sessionTableInfo(0, db, zFrom, zTbl, + &nCol, 0, 0, &azCol, 0, 0, &abPK, + pSession->bImplicitPK ? &bRowid : 0 + ); + } + if( rc==SQLITE_OK ){ + if( pTo->nCol!=nCol ){ + if( nCol<=0 ){ + rc = SQLITE_SCHEMA; + if( pzErrMsg ){ + *pzErrMsg = sqlite3_mprintf("no such table: %s.%s", zFrom, zTbl); + } + }else{ + bMismatch = 1; + } + }else{ + int i; + for(i=0; iabPK[i]!=abPK[i] ) bMismatch = 1; + if( sqlite3_stricmp(azCol[i], pTo->azCol[i]) ) bMismatch = 1; + if( abPK[i] ) bHasPk = 1; + } + } + } + sqlite3_free((char*)azCol); + if( bMismatch ){ + if( pzErrMsg ){ + *pzErrMsg = sqlite3_mprintf("table schemas do not match"); + } + rc = SQLITE_SCHEMA; + } + if( bHasPk==0 ){ + /* Ignore tables with no primary keys */ + goto diff_out; + } + } + + if( rc==SQLITE_OK ){ + zExpr = sessionExprComparePK(pTo->nCol, + zDb, zFrom, pTo->zName, pTo->azCol, pTo->abPK + ); + } + + /* Find new rows */ + if( rc==SQLITE_OK ){ + rc = sessionDiffFindNew(SQLITE_INSERT, pSession, pTo, zDb, zFrom, zExpr); + } + + /* Find old rows */ + if( rc==SQLITE_OK ){ + rc = sessionDiffFindNew(SQLITE_DELETE, pSession, pTo, zFrom, zDb, zExpr); + } + + /* Find modified rows */ + if( rc==SQLITE_OK ){ + rc = sessionDiffFindModified(pSession, pTo, zFrom, zExpr); + } + + sqlite3_free(zExpr); + } + + diff_out: + sessionPreupdateHooks(pSession); + sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db)); + return rc; +} + +/* +** Create a session object. This session object will record changes to +** database zDb attached to connection db. +*/ +int sqlite3session_create( + sqlite3 *db, /* Database handle */ + const char *zDb, /* Name of db (e.g. "main") */ + sqlite3_session **ppSession /* OUT: New session object */ +){ + sqlite3_session *pNew; /* Newly allocated session object */ + sqlite3_session *pOld; /* Session object already attached to db */ + int nDb = sqlite3Strlen30(zDb); /* Length of zDb in bytes */ + + /* Zero the output value in case an error occurs. */ + *ppSession = 0; + + /* Allocate and populate the new session object. */ + pNew = (sqlite3_session *)sqlite3_malloc64(sizeof(sqlite3_session) + nDb + 1); + if( !pNew ) return SQLITE_NOMEM; + memset(pNew, 0, sizeof(sqlite3_session)); + pNew->db = db; + pNew->zDb = (char *)&pNew[1]; + pNew->bEnable = 1; + memcpy(pNew->zDb, zDb, nDb+1); + sessionPreupdateHooks(pNew); + + /* Add the new session object to the linked list of session objects + ** attached to database handle $db. Do this under the cover of the db + ** handle mutex. */ + sqlite3_mutex_enter(sqlite3_db_mutex(db)); + pOld = (sqlite3_session*)sqlite3_preupdate_hook(db, xPreUpdate, (void*)pNew); + pNew->pNext = pOld; + sqlite3_mutex_leave(sqlite3_db_mutex(db)); + + *ppSession = pNew; + return SQLITE_OK; +} + +/* +** Free the list of table objects passed as the first argument. The contents +** of the changed-rows hash tables are also deleted. +*/ +static void sessionDeleteTable(sqlite3_session *pSession, SessionTable *pList){ + SessionTable *pNext; + SessionTable *pTab; + + for(pTab=pList; pTab; pTab=pNext){ + int i; + pNext = pTab->pNext; + for(i=0; inChange; i++){ + SessionChange *p; + SessionChange *pNextChange; + for(p=pTab->apChange[i]; p; p=pNextChange){ + pNextChange = p->pNext; + sessionFree(pSession, p); + } + } + sqlite3_finalize(pTab->pDfltStmt); + sessionFree(pSession, (char*)pTab->azCol); /* cast works around VC++ bug */ + sessionFree(pSession, pTab->apChange); + sessionFree(pSession, pTab); + } +} + +/* +** Delete a session object previously allocated using sqlite3session_create(). +*/ +void sqlite3session_delete(sqlite3_session *pSession){ + sqlite3 *db = pSession->db; + sqlite3_session *pHead; + sqlite3_session **pp; + + /* Unlink the session from the linked list of sessions attached to the + ** database handle. Hold the db mutex while doing so. */ + sqlite3_mutex_enter(sqlite3_db_mutex(db)); + pHead = (sqlite3_session*)sqlite3_preupdate_hook(db, 0, 0); + for(pp=&pHead; ALWAYS((*pp)!=0); pp=&((*pp)->pNext)){ + if( (*pp)==pSession ){ + *pp = (*pp)->pNext; + if( pHead ) sqlite3_preupdate_hook(db, xPreUpdate, (void*)pHead); + break; + } + } + sqlite3_mutex_leave(sqlite3_db_mutex(db)); + sqlite3ValueFree(pSession->pZeroBlob); + + /* Delete all attached table objects. And the contents of their + ** associated hash-tables. */ + sessionDeleteTable(pSession, pSession->pTable); + + /* Free the session object. */ + sqlite3_free(pSession); +} + +/* +** Set a table filter on a Session Object. +*/ +void sqlite3session_table_filter( + sqlite3_session *pSession, + int(*xFilter)(void*, const char*), + void *pCtx /* First argument passed to xFilter */ +){ + pSession->bAutoAttach = 1; + pSession->pFilterCtx = pCtx; + pSession->xTableFilter = xFilter; +} + +/* +** Attach a table to a session. All subsequent changes made to the table +** while the session object is enabled will be recorded. +** +** Only tables that have a PRIMARY KEY defined may be attached. It does +** not matter if the PRIMARY KEY is an "INTEGER PRIMARY KEY" (rowid alias) +** or not. +*/ +int sqlite3session_attach( + sqlite3_session *pSession, /* Session object */ + const char *zName /* Table name */ +){ + int rc = SQLITE_OK; + sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db)); + + if( !zName ){ + pSession->bAutoAttach = 1; + }else{ + SessionTable *pTab; /* New table object (if required) */ + int nName; /* Number of bytes in string zName */ + + /* First search for an existing entry. If one is found, this call is + ** a no-op. Return early. */ + nName = sqlite3Strlen30(zName); + for(pTab=pSession->pTable; pTab; pTab=pTab->pNext){ + if( 0==sqlite3_strnicmp(pTab->zName, zName, nName+1) ) break; + } + + if( !pTab ){ + /* Allocate new SessionTable object. */ + int nByte = sizeof(SessionTable) + nName + 1; + pTab = (SessionTable*)sessionMalloc64(pSession, nByte); + if( !pTab ){ + rc = SQLITE_NOMEM; + }else{ + /* Populate the new SessionTable object and link it into the list. + ** The new object must be linked onto the end of the list, not + ** simply added to the start of it in order to ensure that tables + ** appear in the correct order when a changeset or patchset is + ** eventually generated. */ + SessionTable **ppTab; + memset(pTab, 0, sizeof(SessionTable)); + pTab->zName = (char *)&pTab[1]; + memcpy(pTab->zName, zName, nName+1); + for(ppTab=&pSession->pTable; *ppTab; ppTab=&(*ppTab)->pNext); + *ppTab = pTab; + } + } + } + + sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db)); + return rc; +} + +/* +** Append the value passed as the second argument to the buffer passed +** as the first. +** +** This function is a no-op if *pRc is non-zero when it is called. +** Otherwise, if an error occurs, *pRc is set to an SQLite error code +** before returning. +*/ +static void sessionAppendValue(SessionBuffer *p, sqlite3_value *pVal, int *pRc){ + int rc = *pRc; + if( rc==SQLITE_OK ){ + sqlite3_int64 nByte = 0; + rc = sessionSerializeValue(0, pVal, &nByte); + sessionBufferGrow(p, nByte, &rc); + if( rc==SQLITE_OK ){ + rc = sessionSerializeValue(&p->aBuf[p->nBuf], pVal, 0); + p->nBuf += nByte; + }else{ + *pRc = rc; + } + } +} + +/* +** This function is a no-op if *pRc is other than SQLITE_OK when it is +** called. Otherwise, append a single byte to the buffer. +** +** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before +** returning. +*/ +static void sessionAppendByte(SessionBuffer *p, u8 v, int *pRc){ + if( 0==sessionBufferGrow(p, 1, pRc) ){ + p->aBuf[p->nBuf++] = v; + } +} + +/* +** This function is a no-op if *pRc is other than SQLITE_OK when it is +** called. Otherwise, append a single varint to the buffer. +** +** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before +** returning. +*/ +static void sessionAppendVarint(SessionBuffer *p, int v, int *pRc){ + if( 0==sessionBufferGrow(p, 9, pRc) ){ + p->nBuf += sessionVarintPut(&p->aBuf[p->nBuf], v); + } +} + +/* +** This function is a no-op if *pRc is other than SQLITE_OK when it is +** called. Otherwise, append a blob of data to the buffer. +** +** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before +** returning. +*/ +static void sessionAppendBlob( + SessionBuffer *p, + const u8 *aBlob, + int nBlob, + int *pRc +){ + if( nBlob>0 && 0==sessionBufferGrow(p, nBlob, pRc) ){ + memcpy(&p->aBuf[p->nBuf], aBlob, nBlob); + p->nBuf += nBlob; + } +} + +/* +** This function is a no-op if *pRc is other than SQLITE_OK when it is +** called. Otherwise, append the string representation of integer iVal +** to the buffer. No nul-terminator is written. +** +** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before +** returning. +*/ +static void sessionAppendInteger( + SessionBuffer *p, /* Buffer to append to */ + int iVal, /* Value to write the string rep. of */ + int *pRc /* IN/OUT: Error code */ +){ + char aBuf[24]; + sqlite3_snprintf(sizeof(aBuf)-1, aBuf, "%d", iVal); + sessionAppendStr(p, aBuf, pRc); +} + +/* +** This function is a no-op if *pRc is other than SQLITE_OK when it is +** called. Otherwise, append the string zStr enclosed in quotes (") and +** with any embedded quote characters escaped to the buffer. No +** nul-terminator byte is written. +** +** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before +** returning. +*/ +static void sessionAppendIdent( + SessionBuffer *p, /* Buffer to a append to */ + const char *zStr, /* String to quote, escape and append */ + int *pRc /* IN/OUT: Error code */ +){ + int nStr = sqlite3Strlen30(zStr)*2 + 2 + 2; + if( 0==sessionBufferGrow(p, nStr, pRc) ){ + char *zOut = (char *)&p->aBuf[p->nBuf]; + const char *zIn = zStr; + *zOut++ = '"'; + if( zIn!=0 ){ + while( *zIn ){ + if( *zIn=='"' ) *zOut++ = '"'; + *zOut++ = *(zIn++); + } + } + *zOut++ = '"'; + p->nBuf = (int)((u8 *)zOut - p->aBuf); + p->aBuf[p->nBuf] = 0x00; + } +} + +/* +** This function is a no-op if *pRc is other than SQLITE_OK when it is +** called. Otherwse, it appends the serialized version of the value stored +** in column iCol of the row that SQL statement pStmt currently points +** to to the buffer. +*/ +static void sessionAppendCol( + SessionBuffer *p, /* Buffer to append to */ + sqlite3_stmt *pStmt, /* Handle pointing to row containing value */ + int iCol, /* Column to read value from */ + int *pRc /* IN/OUT: Error code */ +){ + if( *pRc==SQLITE_OK ){ + int eType = sqlite3_column_type(pStmt, iCol); + sessionAppendByte(p, (u8)eType, pRc); + if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){ + sqlite3_int64 i; + u8 aBuf[8]; + if( eType==SQLITE_INTEGER ){ + i = sqlite3_column_int64(pStmt, iCol); + }else{ + double r = sqlite3_column_double(pStmt, iCol); + memcpy(&i, &r, 8); + } + sessionPutI64(aBuf, i); + sessionAppendBlob(p, aBuf, 8, pRc); + } + if( eType==SQLITE_BLOB || eType==SQLITE_TEXT ){ + u8 *z; + int nByte; + if( eType==SQLITE_BLOB ){ + z = (u8 *)sqlite3_column_blob(pStmt, iCol); + }else{ + z = (u8 *)sqlite3_column_text(pStmt, iCol); + } + nByte = sqlite3_column_bytes(pStmt, iCol); + if( z || (eType==SQLITE_BLOB && nByte==0) ){ + sessionAppendVarint(p, nByte, pRc); + sessionAppendBlob(p, z, nByte, pRc); + }else{ + *pRc = SQLITE_NOMEM; + } + } + } +} + +/* +** +** This function appends an update change to the buffer (see the comments +** under "CHANGESET FORMAT" at the top of the file). An update change +** consists of: +** +** 1 byte: SQLITE_UPDATE (0x17) +** n bytes: old.* record (see RECORD FORMAT) +** m bytes: new.* record (see RECORD FORMAT) +** +** The SessionChange object passed as the third argument contains the +** values that were stored in the row when the session began (the old.* +** values). The statement handle passed as the second argument points +** at the current version of the row (the new.* values). +** +** If all of the old.* values are equal to their corresponding new.* value +** (i.e. nothing has changed), then no data at all is appended to the buffer. +** +** Otherwise, the old.* record contains all primary key values and the +** original values of any fields that have been modified. The new.* record +** contains the new values of only those fields that have been modified. +*/ +static int sessionAppendUpdate( + SessionBuffer *pBuf, /* Buffer to append to */ + int bPatchset, /* True for "patchset", 0 for "changeset" */ + sqlite3_stmt *pStmt, /* Statement handle pointing at new row */ + SessionChange *p, /* Object containing old values */ + u8 *abPK /* Boolean array - true for PK columns */ +){ + int rc = SQLITE_OK; + SessionBuffer buf2 = {0,0,0}; /* Buffer to accumulate new.* record in */ + int bNoop = 1; /* Set to zero if any values are modified */ + int nRewind = pBuf->nBuf; /* Set to zero if any values are modified */ + int i; /* Used to iterate through columns */ + u8 *pCsr = p->aRecord; /* Used to iterate through old.* values */ + + assert( abPK!=0 ); + sessionAppendByte(pBuf, SQLITE_UPDATE, &rc); + sessionAppendByte(pBuf, p->bIndirect, &rc); + for(i=0; inBuf = nRewind; + }else{ + sessionAppendBlob(pBuf, buf2.aBuf, buf2.nBuf, &rc); + } + sqlite3_free(buf2.aBuf); + + return rc; +} + +/* +** Append a DELETE change to the buffer passed as the first argument. Use +** the changeset format if argument bPatchset is zero, or the patchset +** format otherwise. +*/ +static int sessionAppendDelete( + SessionBuffer *pBuf, /* Buffer to append to */ + int bPatchset, /* True for "patchset", 0 for "changeset" */ + SessionChange *p, /* Object containing old values */ + int nCol, /* Number of columns in table */ + u8 *abPK /* Boolean array - true for PK columns */ +){ + int rc = SQLITE_OK; + + sessionAppendByte(pBuf, SQLITE_DELETE, &rc); + sessionAppendByte(pBuf, p->bIndirect, &rc); + + if( bPatchset==0 ){ + sessionAppendBlob(pBuf, p->aRecord, p->nRecord, &rc); + }else{ + int i; + u8 *a = p->aRecord; + for(i=0; iaRecord)==p->nRecord ); + } + + return rc; +} + +static int sessionPrepare( + sqlite3 *db, + sqlite3_stmt **pp, + char **pzErrmsg, + const char *zSql +){ + int rc = sqlite3_prepare_v2(db, zSql, -1, pp, 0); + if( pzErrmsg && rc!=SQLITE_OK ){ + *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db)); + } + return rc; +} + +/* +** Formulate and prepare a SELECT statement to retrieve a row from table +** zTab in database zDb based on its primary key. i.e. +** +** SELECT *, FROM zDb.zTab WHERE (pk1, pk2,...) IS (?1, ?2,...) +** +** where is: +** +** 1 AND (?A OR ?1 IS ) AND ... +** +** for each non-pk . +*/ +static int sessionSelectStmt( + sqlite3 *db, /* Database handle */ + int bIgnoreNoop, + const char *zDb, /* Database name */ + const char *zTab, /* Table name */ + int bRowid, + int nCol, /* Number of columns in table */ + const char **azCol, /* Names of table columns */ + u8 *abPK, /* PRIMARY KEY array */ + sqlite3_stmt **ppStmt, /* OUT: Prepared SELECT statement */ + char **pzErrmsg /* OUT: Error message */ +){ + int rc = SQLITE_OK; + char *zSql = 0; + const char *zSep = ""; + int i; + + SessionBuffer cols = {0, 0, 0}; + SessionBuffer nooptest = {0, 0, 0}; + SessionBuffer pkfield = {0, 0, 0}; + SessionBuffer pkvar = {0, 0, 0}; + + sessionAppendStr(&nooptest, ", 1", &rc); + + if( 0==sqlite3_stricmp("sqlite_stat1", zTab) ){ + sessionAppendStr(&nooptest, " AND (?6 OR ?3 IS stat)", &rc); + sessionAppendStr(&pkfield, "tbl, idx", &rc); + sessionAppendStr(&pkvar, + "?1, (CASE WHEN ?2=X'' THEN NULL ELSE ?2 END)", &rc + ); + sessionAppendStr(&cols, "tbl, ?2, stat", &rc); + }else{ + #if 0 + if( bRowid ){ + sessionAppendStr(&cols, SESSIONS_ROWID, &rc); + } + #endif + for(i=0; iaRecord; + + for(i=0; inCol, pRc); + sessionAppendBlob(pBuf, pTab->abPK, pTab->nCol, pRc); + sessionAppendBlob(pBuf, (u8 *)pTab->zName, (int)strlen(pTab->zName)+1, pRc); +} + +/* +** Generate either a changeset (if argument bPatchset is zero) or a patchset +** (if it is non-zero) based on the current contents of the session object +** passed as the first argument. +** +** If no error occurs, SQLITE_OK is returned and the new changeset/patchset +** stored in output variables *pnChangeset and *ppChangeset. Or, if an error +** occurs, an SQLite error code is returned and both output variables set +** to 0. +*/ +static int sessionGenerateChangeset( + sqlite3_session *pSession, /* Session object */ + int bPatchset, /* True for patchset, false for changeset */ + int (*xOutput)(void *pOut, const void *pData, int nData), + void *pOut, /* First argument for xOutput */ + int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */ + void **ppChangeset /* OUT: Buffer containing changeset */ +){ + sqlite3 *db = pSession->db; /* Source database handle */ + SessionTable *pTab; /* Used to iterate through attached tables */ + SessionBuffer buf = {0,0,0}; /* Buffer in which to accumulate changeset */ + int rc; /* Return code */ + + assert( xOutput==0 || (pnChangeset==0 && ppChangeset==0) ); + assert( xOutput!=0 || (pnChangeset!=0 && ppChangeset!=0) ); + + /* Zero the output variables in case an error occurs. If this session + ** object is already in the error state (sqlite3_session.rc != SQLITE_OK), + ** this call will be a no-op. */ + if( xOutput==0 ){ + assert( pnChangeset!=0 && ppChangeset!=0 ); + *pnChangeset = 0; + *ppChangeset = 0; + } + + if( pSession->rc ) return pSession->rc; + rc = sqlite3_exec(pSession->db, "SAVEPOINT changeset", 0, 0, 0); + if( rc!=SQLITE_OK ) return rc; + + sqlite3_mutex_enter(sqlite3_db_mutex(db)); + + for(pTab=pSession->pTable; rc==SQLITE_OK && pTab; pTab=pTab->pNext){ + if( pTab->nEntry ){ + const char *zName = pTab->zName; + int i; /* Used to iterate through hash buckets */ + sqlite3_stmt *pSel = 0; /* SELECT statement to query table pTab */ + int nRewind = buf.nBuf; /* Initial size of write buffer */ + int nNoop; /* Size of buffer after writing tbl header */ + int nOldCol = pTab->nCol; + + /* Check the table schema is still Ok. */ + rc = sessionReinitTable(pSession, pTab); + if( rc==SQLITE_OK && pTab->nCol!=nOldCol ){ + rc = sessionUpdateChanges(pSession, pTab); + } + + /* Write a table header */ + sessionAppendTableHdr(&buf, bPatchset, pTab, &rc); + + /* Build and compile a statement to execute: */ + if( rc==SQLITE_OK ){ + rc = sessionSelectStmt(db, 0, pSession->zDb, + zName, pTab->bRowid, pTab->nCol, pTab->azCol, pTab->abPK, &pSel, 0 + ); + } + + nNoop = buf.nBuf; + for(i=0; inChange && rc==SQLITE_OK; i++){ + SessionChange *p; /* Used to iterate through changes */ + + for(p=pTab->apChange[i]; rc==SQLITE_OK && p; p=p->pNext){ + rc = sessionSelectBind(pSel, pTab->nCol, pTab->abPK, p); + if( rc!=SQLITE_OK ) continue; + if( sqlite3_step(pSel)==SQLITE_ROW ){ + if( p->op==SQLITE_INSERT ){ + int iCol; + sessionAppendByte(&buf, SQLITE_INSERT, &rc); + sessionAppendByte(&buf, p->bIndirect, &rc); + for(iCol=0; iColnCol; iCol++){ + sessionAppendCol(&buf, pSel, iCol, &rc); + } + }else{ + assert( pTab->abPK!=0 ); + rc = sessionAppendUpdate(&buf, bPatchset, pSel, p, pTab->abPK); + } + }else if( p->op!=SQLITE_INSERT ){ + rc = sessionAppendDelete(&buf, bPatchset, p, pTab->nCol,pTab->abPK); + } + if( rc==SQLITE_OK ){ + rc = sqlite3_reset(pSel); + } + + /* If the buffer is now larger than sessions_strm_chunk_size, pass + ** its contents to the xOutput() callback. */ + if( xOutput + && rc==SQLITE_OK + && buf.nBuf>nNoop + && buf.nBuf>sessions_strm_chunk_size + ){ + rc = xOutput(pOut, (void*)buf.aBuf, buf.nBuf); + nNoop = -1; + buf.nBuf = 0; + } + + } + } + + sqlite3_finalize(pSel); + if( buf.nBuf==nNoop ){ + buf.nBuf = nRewind; + } + } + } + + if( rc==SQLITE_OK ){ + if( xOutput==0 ){ + *pnChangeset = buf.nBuf; + *ppChangeset = buf.aBuf; + buf.aBuf = 0; + }else if( buf.nBuf>0 ){ + rc = xOutput(pOut, (void*)buf.aBuf, buf.nBuf); + } + } + + sqlite3_free(buf.aBuf); + sqlite3_exec(db, "RELEASE changeset", 0, 0, 0); + sqlite3_mutex_leave(sqlite3_db_mutex(db)); + return rc; +} + +/* +** Obtain a changeset object containing all changes recorded by the +** session object passed as the first argument. +** +** It is the responsibility of the caller to eventually free the buffer +** using sqlite3_free(). +*/ +int sqlite3session_changeset( + sqlite3_session *pSession, /* Session object */ + int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */ + void **ppChangeset /* OUT: Buffer containing changeset */ +){ + int rc; + + if( pnChangeset==0 || ppChangeset==0 ) return SQLITE_MISUSE; + rc = sessionGenerateChangeset(pSession, 0, 0, 0, pnChangeset, ppChangeset); + assert( rc || pnChangeset==0 + || pSession->bEnableSize==0 || *pnChangeset<=pSession->nMaxChangesetSize + ); + return rc; +} + +/* +** Streaming version of sqlite3session_changeset(). +*/ +int sqlite3session_changeset_strm( + sqlite3_session *pSession, + int (*xOutput)(void *pOut, const void *pData, int nData), + void *pOut +){ + if( xOutput==0 ) return SQLITE_MISUSE; + return sessionGenerateChangeset(pSession, 0, xOutput, pOut, 0, 0); +} + +/* +** Streaming version of sqlite3session_patchset(). +*/ +int sqlite3session_patchset_strm( + sqlite3_session *pSession, + int (*xOutput)(void *pOut, const void *pData, int nData), + void *pOut +){ + if( xOutput==0 ) return SQLITE_MISUSE; + return sessionGenerateChangeset(pSession, 1, xOutput, pOut, 0, 0); +} + +/* +** Obtain a patchset object containing all changes recorded by the +** session object passed as the first argument. +** +** It is the responsibility of the caller to eventually free the buffer +** using sqlite3_free(). +*/ +int sqlite3session_patchset( + sqlite3_session *pSession, /* Session object */ + int *pnPatchset, /* OUT: Size of buffer at *ppChangeset */ + void **ppPatchset /* OUT: Buffer containing changeset */ +){ + if( pnPatchset==0 || ppPatchset==0 ) return SQLITE_MISUSE; + return sessionGenerateChangeset(pSession, 1, 0, 0, pnPatchset, ppPatchset); +} + +/* +** Enable or disable the session object passed as the first argument. +*/ +int sqlite3session_enable(sqlite3_session *pSession, int bEnable){ + int ret; + sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db)); + if( bEnable>=0 ){ + pSession->bEnable = bEnable; + } + ret = pSession->bEnable; + sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db)); + return ret; +} + +/* +** Enable or disable the session object passed as the first argument. +*/ +int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect){ + int ret; + sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db)); + if( bIndirect>=0 ){ + pSession->bIndirect = bIndirect; + } + ret = pSession->bIndirect; + sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db)); + return ret; +} + +/* +** Return true if there have been no changes to monitored tables recorded +** by the session object passed as the only argument. +*/ +int sqlite3session_isempty(sqlite3_session *pSession){ + int ret = 0; + SessionTable *pTab; + + sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db)); + for(pTab=pSession->pTable; pTab && ret==0; pTab=pTab->pNext){ + ret = (pTab->nEntry>0); + } + sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db)); + + return (ret==0); +} + +/* +** Return the amount of heap memory in use. +*/ +sqlite3_int64 sqlite3session_memory_used(sqlite3_session *pSession){ + return pSession->nMalloc; +} + +/* +** Configure the session object passed as the first argument. +*/ +int sqlite3session_object_config(sqlite3_session *pSession, int op, void *pArg){ + int rc = SQLITE_OK; + switch( op ){ + case SQLITE_SESSION_OBJCONFIG_SIZE: { + int iArg = *(int*)pArg; + if( iArg>=0 ){ + if( pSession->pTable ){ + rc = SQLITE_MISUSE; + }else{ + pSession->bEnableSize = (iArg!=0); + } + } + *(int*)pArg = pSession->bEnableSize; + break; + } + + case SQLITE_SESSION_OBJCONFIG_ROWID: { + int iArg = *(int*)pArg; + if( iArg>=0 ){ + if( pSession->pTable ){ + rc = SQLITE_MISUSE; + }else{ + pSession->bImplicitPK = (iArg!=0); + } + } + *(int*)pArg = pSession->bImplicitPK; + break; + } + + default: + rc = SQLITE_MISUSE; + } + + return rc; +} + +/* +** Return the maximum size of sqlite3session_changeset() output. +*/ +sqlite3_int64 sqlite3session_changeset_size(sqlite3_session *pSession){ + return pSession->nMaxChangesetSize; +} + +/* +** Do the work for either sqlite3changeset_start() or start_strm(). +*/ +static int sessionChangesetStart( + sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */ + int (*xInput)(void *pIn, void *pData, int *pnData), + void *pIn, + int nChangeset, /* Size of buffer pChangeset in bytes */ + void *pChangeset, /* Pointer to buffer containing changeset */ + int bInvert, /* True to invert changeset */ + int bSkipEmpty /* True to skip empty UPDATE changes */ +){ + sqlite3_changeset_iter *pRet; /* Iterator to return */ + int nByte; /* Number of bytes to allocate for iterator */ + + assert( xInput==0 || (pChangeset==0 && nChangeset==0) ); + + /* Zero the output variable in case an error occurs. */ + *pp = 0; + + /* Allocate and initialize the iterator structure. */ + nByte = sizeof(sqlite3_changeset_iter); + pRet = (sqlite3_changeset_iter *)sqlite3_malloc(nByte); + if( !pRet ) return SQLITE_NOMEM; + memset(pRet, 0, sizeof(sqlite3_changeset_iter)); + pRet->in.aData = (u8 *)pChangeset; + pRet->in.nData = nChangeset; + pRet->in.xInput = xInput; + pRet->in.pIn = pIn; + pRet->in.bEof = (xInput ? 0 : 1); + pRet->bInvert = bInvert; + pRet->bSkipEmpty = bSkipEmpty; + + /* Populate the output variable and return success. */ + *pp = pRet; + return SQLITE_OK; +} + +/* +** Create an iterator used to iterate through the contents of a changeset. +*/ +int sqlite3changeset_start( + sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */ + int nChangeset, /* Size of buffer pChangeset in bytes */ + void *pChangeset /* Pointer to buffer containing changeset */ +){ + return sessionChangesetStart(pp, 0, 0, nChangeset, pChangeset, 0, 0); +} +int sqlite3changeset_start_v2( + sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */ + int nChangeset, /* Size of buffer pChangeset in bytes */ + void *pChangeset, /* Pointer to buffer containing changeset */ + int flags +){ + int bInvert = !!(flags & SQLITE_CHANGESETSTART_INVERT); + return sessionChangesetStart(pp, 0, 0, nChangeset, pChangeset, bInvert, 0); +} + +/* +** Streaming version of sqlite3changeset_start(). +*/ +int sqlite3changeset_start_strm( + sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */ + int (*xInput)(void *pIn, void *pData, int *pnData), + void *pIn +){ + return sessionChangesetStart(pp, xInput, pIn, 0, 0, 0, 0); +} +int sqlite3changeset_start_v2_strm( + sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */ + int (*xInput)(void *pIn, void *pData, int *pnData), + void *pIn, + int flags +){ + int bInvert = !!(flags & SQLITE_CHANGESETSTART_INVERT); + return sessionChangesetStart(pp, xInput, pIn, 0, 0, bInvert, 0); +} + +/* +** If the SessionInput object passed as the only argument is a streaming +** object and the buffer is full, discard some data to free up space. +*/ +static void sessionDiscardData(SessionInput *pIn){ + if( pIn->xInput && pIn->iCurrent>=sessions_strm_chunk_size ){ + int nMove = pIn->buf.nBuf - pIn->iCurrent; + assert( nMove>=0 ); + if( nMove>0 ){ + memmove(pIn->buf.aBuf, &pIn->buf.aBuf[pIn->iCurrent], nMove); + } + pIn->buf.nBuf -= pIn->iCurrent; + pIn->iNext -= pIn->iCurrent; + pIn->iCurrent = 0; + pIn->nData = pIn->buf.nBuf; + } +} + +/* +** Ensure that there are at least nByte bytes available in the buffer. Or, +** if there are not nByte bytes remaining in the input, that all available +** data is in the buffer. +** +** Return an SQLite error code if an error occurs, or SQLITE_OK otherwise. +*/ +static int sessionInputBuffer(SessionInput *pIn, int nByte){ + int rc = SQLITE_OK; + if( pIn->xInput ){ + while( !pIn->bEof && (pIn->iNext+nByte)>=pIn->nData && rc==SQLITE_OK ){ + int nNew = sessions_strm_chunk_size; + + if( pIn->bNoDiscard==0 ) sessionDiscardData(pIn); + if( SQLITE_OK==sessionBufferGrow(&pIn->buf, nNew, &rc) ){ + rc = pIn->xInput(pIn->pIn, &pIn->buf.aBuf[pIn->buf.nBuf], &nNew); + if( nNew==0 ){ + pIn->bEof = 1; + }else{ + pIn->buf.nBuf += nNew; + } + } + + pIn->aData = pIn->buf.aBuf; + pIn->nData = pIn->buf.nBuf; + } + } + return rc; +} + +/* +** When this function is called, *ppRec points to the start of a record +** that contains nCol values. This function advances the pointer *ppRec +** until it points to the byte immediately following that record. +*/ +static void sessionSkipRecord( + u8 **ppRec, /* IN/OUT: Record pointer */ + int nCol /* Number of values in record */ +){ + u8 *aRec = *ppRec; + int i; + for(i=0; iiNext>=pIn->nData ){ + rc = SQLITE_CORRUPT_BKPT; + }else{ + eType = pIn->aData[pIn->iNext++]; + assert( apOut[i]==0 ); + if( eType ){ + if( pbEmpty ) *pbEmpty = 0; + apOut[i] = sqlite3ValueNew(0); + if( !apOut[i] ) rc = SQLITE_NOMEM; + } + } + } + + if( rc==SQLITE_OK ){ + u8 *aVal = &pIn->aData[pIn->iNext]; + if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){ + int nByte; + pIn->iNext += sessionVarintGet(aVal, &nByte); + rc = sessionInputBuffer(pIn, nByte); + if( rc==SQLITE_OK ){ + if( nByte<0 || nByte>pIn->nData-pIn->iNext ){ + rc = SQLITE_CORRUPT_BKPT; + }else{ + u8 enc = (eType==SQLITE_TEXT ? SQLITE_UTF8 : 0); + rc = sessionValueSetStr(apOut[i],&pIn->aData[pIn->iNext],nByte,enc); + pIn->iNext += nByte; + } + } + } + if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){ + if( (pIn->nData-pIn->iNext)<8 ){ + rc = SQLITE_CORRUPT_BKPT; + }else{ + sqlite3_int64 v = sessionGetI64(aVal); + if( eType==SQLITE_INTEGER ){ + sqlite3VdbeMemSetInt64(apOut[i], v); + }else{ + double d; + memcpy(&d, &v, 8); + sqlite3VdbeMemSetDouble(apOut[i], d); + } + pIn->iNext += 8; + } + } + } + } + + return rc; +} + +/* +** The input pointer currently points to the second byte of a table-header. +** Specifically, to the following: +** +** + number of columns in table (varint) +** + array of PK flags (1 byte per column), +** + table name (nul terminated). +** +** This function ensures that all of the above is present in the input +** buffer (i.e. that it can be accessed without any calls to xInput()). +** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code. +** The input pointer is not moved. +*/ +static int sessionChangesetBufferTblhdr(SessionInput *pIn, int *pnByte){ + int rc = SQLITE_OK; + int nCol = 0; + int nRead = 0; + + rc = sessionInputBuffer(pIn, 9); + if( rc==SQLITE_OK ){ + nRead += sessionVarintGet(&pIn->aData[pIn->iNext + nRead], &nCol); + /* The hard upper limit for the number of columns in an SQLite + ** database table is, according to sqliteLimit.h, 32676. So + ** consider any table-header that purports to have more than 65536 + ** columns to be corrupt. This is convenient because otherwise, + ** if the (nCol>65536) condition below were omitted, a sufficiently + ** large value for nCol may cause nRead to wrap around and become + ** negative. Leading to a crash. */ + if( nCol<0 || nCol>65536 ){ + rc = SQLITE_CORRUPT_BKPT; + }else{ + rc = sessionInputBuffer(pIn, nRead+nCol+100); + nRead += nCol; + } + } + + while( rc==SQLITE_OK ){ + while( (pIn->iNext + nRead)nData && pIn->aData[pIn->iNext + nRead] ){ + nRead++; + } + if( (pIn->iNext + nRead)nData ) break; + rc = sessionInputBuffer(pIn, nRead + 100); + } + *pnByte = nRead+1; + return rc; +} + +/* +** The input pointer currently points to the first byte of the first field +** of a record consisting of nCol columns. This function ensures the entire +** record is buffered. It does not move the input pointer. +** +** If successful, SQLITE_OK is returned and *pnByte is set to the size of +** the record in bytes. Otherwise, an SQLite error code is returned. The +** final value of *pnByte is undefined in this case. +*/ +static int sessionChangesetBufferRecord( + SessionInput *pIn, /* Input data */ + int nCol, /* Number of columns in record */ + int *pnByte /* OUT: Size of record in bytes */ +){ + int rc = SQLITE_OK; + int nByte = 0; + int i; + for(i=0; rc==SQLITE_OK && iaData[pIn->iNext + nByte++]; + if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){ + int n; + nByte += sessionVarintGet(&pIn->aData[pIn->iNext+nByte], &n); + nByte += n; + rc = sessionInputBuffer(pIn, nByte); + }else if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){ + nByte += 8; + } + } + } + *pnByte = nByte; + return rc; +} + +/* +** The input pointer currently points to the second byte of a table-header. +** Specifically, to the following: +** +** + number of columns in table (varint) +** + array of PK flags (1 byte per column), +** + table name (nul terminated). +** +** This function decodes the table-header and populates the p->nCol, +** p->zTab and p->abPK[] variables accordingly. The p->apValue[] array is +** also allocated or resized according to the new value of p->nCol. The +** input pointer is left pointing to the byte following the table header. +** +** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code +** is returned and the final values of the various fields enumerated above +** are undefined. +*/ +static int sessionChangesetReadTblhdr(sqlite3_changeset_iter *p){ + int rc; + int nCopy; + assert( p->rc==SQLITE_OK ); + + rc = sessionChangesetBufferTblhdr(&p->in, &nCopy); + if( rc==SQLITE_OK ){ + int nByte; + int nVarint; + nVarint = sessionVarintGet(&p->in.aData[p->in.iNext], &p->nCol); + if( p->nCol>0 ){ + nCopy -= nVarint; + p->in.iNext += nVarint; + nByte = p->nCol * sizeof(sqlite3_value*) * 2 + nCopy; + p->tblhdr.nBuf = 0; + sessionBufferGrow(&p->tblhdr, nByte, &rc); + }else{ + rc = SQLITE_CORRUPT_BKPT; + } + } + + if( rc==SQLITE_OK ){ + size_t iPK = sizeof(sqlite3_value*)*p->nCol*2; + memset(p->tblhdr.aBuf, 0, iPK); + memcpy(&p->tblhdr.aBuf[iPK], &p->in.aData[p->in.iNext], nCopy); + p->in.iNext += nCopy; + } + + p->apValue = (sqlite3_value**)p->tblhdr.aBuf; + if( p->apValue==0 ){ + p->abPK = 0; + p->zTab = 0; + }else{ + p->abPK = (u8*)&p->apValue[p->nCol*2]; + p->zTab = p->abPK ? (char*)&p->abPK[p->nCol] : 0; + } + return (p->rc = rc); +} + +/* +** Advance the changeset iterator to the next change. The differences between +** this function and sessionChangesetNext() are that +** +** * If pbEmpty is not NULL and the change is a no-op UPDATE (an UPDATE +** that modifies no columns), this function sets (*pbEmpty) to 1. +** +** * If the iterator is configured to skip no-op UPDATEs, +** sessionChangesetNext() does that. This function does not. +*/ +static int sessionChangesetNextOne( + sqlite3_changeset_iter *p, /* Changeset iterator */ + u8 **paRec, /* If non-NULL, store record pointer here */ + int *pnRec, /* If non-NULL, store size of record here */ + int *pbNew, /* If non-NULL, true if new table */ + int *pbEmpty +){ + int i; + u8 op; + + assert( (paRec==0 && pnRec==0) || (paRec && pnRec) ); + assert( pbEmpty==0 || *pbEmpty==0 ); + + /* If the iterator is in the error-state, return immediately. */ + if( p->rc!=SQLITE_OK ) return p->rc; + + /* Free the current contents of p->apValue[], if any. */ + if( p->apValue ){ + for(i=0; inCol*2; i++){ + sqlite3ValueFree(p->apValue[i]); + } + memset(p->apValue, 0, sizeof(sqlite3_value*)*p->nCol*2); + } + + /* Make sure the buffer contains at least 10 bytes of input data, or all + ** remaining data if there are less than 10 bytes available. This is + ** sufficient either for the 'T' or 'P' byte and the varint that follows + ** it, or for the two single byte values otherwise. */ + p->rc = sessionInputBuffer(&p->in, 2); + if( p->rc!=SQLITE_OK ) return p->rc; + + p->in.iCurrent = p->in.iNext; + sessionDiscardData(&p->in); + + /* If the iterator is already at the end of the changeset, return DONE. */ + if( p->in.iNext>=p->in.nData ){ + return SQLITE_DONE; + } + + op = p->in.aData[p->in.iNext++]; + while( op=='T' || op=='P' ){ + if( pbNew ) *pbNew = 1; + p->bPatchset = (op=='P'); + if( sessionChangesetReadTblhdr(p) ) return p->rc; + if( (p->rc = sessionInputBuffer(&p->in, 2)) ) return p->rc; + p->in.iCurrent = p->in.iNext; + if( p->in.iNext>=p->in.nData ) return SQLITE_DONE; + op = p->in.aData[p->in.iNext++]; + } + + if( p->zTab==0 || (p->bPatchset && p->bInvert) ){ + /* The first record in the changeset is not a table header. Must be a + ** corrupt changeset. */ + assert( p->in.iNext==1 || p->zTab ); + return (p->rc = SQLITE_CORRUPT_BKPT); + } + + p->op = op; + p->bIndirect = p->in.aData[p->in.iNext++]; + if( p->op!=SQLITE_UPDATE && p->op!=SQLITE_DELETE && p->op!=SQLITE_INSERT ){ + return (p->rc = SQLITE_CORRUPT_BKPT); + } + + if( paRec ){ + int nVal; /* Number of values to buffer */ + if( p->bPatchset==0 && op==SQLITE_UPDATE ){ + nVal = p->nCol * 2; + }else if( p->bPatchset && op==SQLITE_DELETE ){ + nVal = 0; + for(i=0; inCol; i++) if( p->abPK[i] ) nVal++; + }else{ + nVal = p->nCol; + } + p->rc = sessionChangesetBufferRecord(&p->in, nVal, pnRec); + if( p->rc!=SQLITE_OK ) return p->rc; + *paRec = &p->in.aData[p->in.iNext]; + p->in.iNext += *pnRec; + }else{ + sqlite3_value **apOld = (p->bInvert ? &p->apValue[p->nCol] : p->apValue); + sqlite3_value **apNew = (p->bInvert ? p->apValue : &p->apValue[p->nCol]); + + /* If this is an UPDATE or DELETE, read the old.* record. */ + if( p->op!=SQLITE_INSERT && (p->bPatchset==0 || p->op==SQLITE_DELETE) ){ + u8 *abPK = p->bPatchset ? p->abPK : 0; + p->rc = sessionReadRecord(&p->in, p->nCol, abPK, apOld, 0); + if( p->rc!=SQLITE_OK ) return p->rc; + } + + /* If this is an INSERT or UPDATE, read the new.* record. */ + if( p->op!=SQLITE_DELETE ){ + p->rc = sessionReadRecord(&p->in, p->nCol, 0, apNew, pbEmpty); + if( p->rc!=SQLITE_OK ) return p->rc; + } + + if( (p->bPatchset || p->bInvert) && p->op==SQLITE_UPDATE ){ + /* If this is an UPDATE that is part of a patchset, then all PK and + ** modified fields are present in the new.* record. The old.* record + ** is currently completely empty. This block shifts the PK fields from + ** new.* to old.*, to accommodate the code that reads these arrays. */ + for(i=0; inCol; i++){ + assert( p->bPatchset==0 || p->apValue[i]==0 ); + if( p->abPK[i] ){ + assert( p->apValue[i]==0 ); + p->apValue[i] = p->apValue[i+p->nCol]; + if( p->apValue[i]==0 ) return (p->rc = SQLITE_CORRUPT_BKPT); + p->apValue[i+p->nCol] = 0; + } + } + }else if( p->bInvert ){ + if( p->op==SQLITE_INSERT ) p->op = SQLITE_DELETE; + else if( p->op==SQLITE_DELETE ) p->op = SQLITE_INSERT; + } + + /* If this is an UPDATE that is part of a changeset, then check that + ** there are no fields in the old.* record that are not (a) PK fields, + ** or (b) also present in the new.* record. + ** + ** Such records are technically corrupt, but the rebaser was at one + ** point generating them. Under most circumstances this is benign, but + ** can cause spurious SQLITE_RANGE errors when applying the changeset. */ + if( p->bPatchset==0 && p->op==SQLITE_UPDATE){ + for(i=0; inCol; i++){ + if( p->abPK[i]==0 && p->apValue[i+p->nCol]==0 ){ + sqlite3ValueFree(p->apValue[i]); + p->apValue[i] = 0; + } + } + } + } + + return SQLITE_ROW; +} + +/* +** Advance the changeset iterator to the next change. +** +** If both paRec and pnRec are NULL, then this function works like the public +** API sqlite3changeset_next(). If SQLITE_ROW is returned, then the +** sqlite3changeset_new() and old() APIs may be used to query for values. +** +** Otherwise, if paRec and pnRec are not NULL, then a pointer to the change +** record is written to *paRec before returning and the number of bytes in +** the record to *pnRec. +** +** Either way, this function returns SQLITE_ROW if the iterator is +** successfully advanced to the next change in the changeset, an SQLite +** error code if an error occurs, or SQLITE_DONE if there are no further +** changes in the changeset. +*/ +static int sessionChangesetNext( + sqlite3_changeset_iter *p, /* Changeset iterator */ + u8 **paRec, /* If non-NULL, store record pointer here */ + int *pnRec, /* If non-NULL, store size of record here */ + int *pbNew /* If non-NULL, true if new table */ +){ + int bEmpty; + int rc; + do { + bEmpty = 0; + rc = sessionChangesetNextOne(p, paRec, pnRec, pbNew, &bEmpty); + }while( rc==SQLITE_ROW && p->bSkipEmpty && bEmpty); + return rc; +} + +/* +** Advance an iterator created by sqlite3changeset_start() to the next +** change in the changeset. This function may return SQLITE_ROW, SQLITE_DONE +** or SQLITE_CORRUPT. +** +** This function may not be called on iterators passed to a conflict handler +** callback by changeset_apply(). +*/ +int sqlite3changeset_next(sqlite3_changeset_iter *p){ + return sessionChangesetNext(p, 0, 0, 0); +} + +/* +** The following function extracts information on the current change +** from a changeset iterator. It may only be called after changeset_next() +** has returned SQLITE_ROW. +*/ +int sqlite3changeset_op( + sqlite3_changeset_iter *pIter, /* Iterator handle */ + const char **pzTab, /* OUT: Pointer to table name */ + int *pnCol, /* OUT: Number of columns in table */ + int *pOp, /* OUT: SQLITE_INSERT, DELETE or UPDATE */ + int *pbIndirect /* OUT: True if change is indirect */ +){ + *pOp = pIter->op; + *pnCol = pIter->nCol; + *pzTab = pIter->zTab; + if( pbIndirect ) *pbIndirect = pIter->bIndirect; + return SQLITE_OK; +} + +/* +** Return information regarding the PRIMARY KEY and number of columns in +** the database table affected by the change that pIter currently points +** to. This function may only be called after changeset_next() returns +** SQLITE_ROW. +*/ +int sqlite3changeset_pk( + sqlite3_changeset_iter *pIter, /* Iterator object */ + unsigned char **pabPK, /* OUT: Array of boolean - true for PK cols */ + int *pnCol /* OUT: Number of entries in output array */ +){ + *pabPK = pIter->abPK; + if( pnCol ) *pnCol = pIter->nCol; + return SQLITE_OK; +} + +/* +** This function may only be called while the iterator is pointing to an +** SQLITE_UPDATE or SQLITE_DELETE change (see sqlite3changeset_op()). +** Otherwise, SQLITE_MISUSE is returned. +** +** It sets *ppValue to point to an sqlite3_value structure containing the +** iVal'th value in the old.* record. Or, if that particular value is not +** included in the record (because the change is an UPDATE and the field +** was not modified and is not a PK column), set *ppValue to NULL. +** +** If value iVal is out-of-range, SQLITE_RANGE is returned and *ppValue is +** not modified. Otherwise, SQLITE_OK. +*/ +int sqlite3changeset_old( + sqlite3_changeset_iter *pIter, /* Changeset iterator */ + int iVal, /* Index of old.* value to retrieve */ + sqlite3_value **ppValue /* OUT: Old value (or NULL pointer) */ +){ + if( pIter->op!=SQLITE_UPDATE && pIter->op!=SQLITE_DELETE ){ + return SQLITE_MISUSE; + } + if( iVal<0 || iVal>=pIter->nCol ){ + return SQLITE_RANGE; + } + *ppValue = pIter->apValue[iVal]; + return SQLITE_OK; +} + +/* +** This function may only be called while the iterator is pointing to an +** SQLITE_UPDATE or SQLITE_INSERT change (see sqlite3changeset_op()). +** Otherwise, SQLITE_MISUSE is returned. +** +** It sets *ppValue to point to an sqlite3_value structure containing the +** iVal'th value in the new.* record. Or, if that particular value is not +** included in the record (because the change is an UPDATE and the field +** was not modified), set *ppValue to NULL. +** +** If value iVal is out-of-range, SQLITE_RANGE is returned and *ppValue is +** not modified. Otherwise, SQLITE_OK. +*/ +int sqlite3changeset_new( + sqlite3_changeset_iter *pIter, /* Changeset iterator */ + int iVal, /* Index of new.* value to retrieve */ + sqlite3_value **ppValue /* OUT: New value (or NULL pointer) */ +){ + if( pIter->op!=SQLITE_UPDATE && pIter->op!=SQLITE_INSERT ){ + return SQLITE_MISUSE; + } + if( iVal<0 || iVal>=pIter->nCol ){ + return SQLITE_RANGE; + } + *ppValue = pIter->apValue[pIter->nCol+iVal]; + return SQLITE_OK; +} + +/* +** The following two macros are used internally. They are similar to the +** sqlite3changeset_new() and sqlite3changeset_old() functions, except that +** they omit all error checking and return a pointer to the requested value. +*/ +#define sessionChangesetNew(pIter, iVal) (pIter)->apValue[(pIter)->nCol+(iVal)] +#define sessionChangesetOld(pIter, iVal) (pIter)->apValue[(iVal)] + +/* +** This function may only be called with a changeset iterator that has been +** passed to an SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT +** conflict-handler function. Otherwise, SQLITE_MISUSE is returned. +** +** If successful, *ppValue is set to point to an sqlite3_value structure +** containing the iVal'th value of the conflicting record. +** +** If value iVal is out-of-range or some other error occurs, an SQLite error +** code is returned. Otherwise, SQLITE_OK. +*/ +int sqlite3changeset_conflict( + sqlite3_changeset_iter *pIter, /* Changeset iterator */ + int iVal, /* Index of conflict record value to fetch */ + sqlite3_value **ppValue /* OUT: Value from conflicting row */ +){ + if( !pIter->pConflict ){ + return SQLITE_MISUSE; + } + if( iVal<0 || iVal>=pIter->nCol ){ + return SQLITE_RANGE; + } + *ppValue = sqlite3_column_value(pIter->pConflict, iVal); + return SQLITE_OK; +} + +/* +** This function may only be called with an iterator passed to an +** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case +** it sets the output variable to the total number of known foreign key +** violations in the destination database and returns SQLITE_OK. +** +** In all other cases this function returns SQLITE_MISUSE. +*/ +int sqlite3changeset_fk_conflicts( + sqlite3_changeset_iter *pIter, /* Changeset iterator */ + int *pnOut /* OUT: Number of FK violations */ +){ + if( pIter->pConflict || pIter->apValue ){ + return SQLITE_MISUSE; + } + *pnOut = pIter->nCol; + return SQLITE_OK; +} + + +/* +** Finalize an iterator allocated with sqlite3changeset_start(). +** +** This function may not be called on iterators passed to a conflict handler +** callback by changeset_apply(). +*/ +int sqlite3changeset_finalize(sqlite3_changeset_iter *p){ + int rc = SQLITE_OK; + if( p ){ + int i; /* Used to iterate through p->apValue[] */ + rc = p->rc; + if( p->apValue ){ + for(i=0; inCol*2; i++) sqlite3ValueFree(p->apValue[i]); + } + sqlite3_free(p->tblhdr.aBuf); + sqlite3_free(p->in.buf.aBuf); + sqlite3_free(p); + } + return rc; +} + +static int sessionChangesetInvert( + SessionInput *pInput, /* Input changeset */ + int (*xOutput)(void *pOut, const void *pData, int nData), + void *pOut, + int *pnInverted, /* OUT: Number of bytes in output changeset */ + void **ppInverted /* OUT: Inverse of pChangeset */ +){ + int rc = SQLITE_OK; /* Return value */ + SessionBuffer sOut; /* Output buffer */ + int nCol = 0; /* Number of cols in current table */ + u8 *abPK = 0; /* PK array for current table */ + sqlite3_value **apVal = 0; /* Space for values for UPDATE inversion */ + SessionBuffer sPK = {0, 0, 0}; /* PK array for current table */ + + /* Initialize the output buffer */ + memset(&sOut, 0, sizeof(SessionBuffer)); + + /* Zero the output variables in case an error occurs. */ + if( ppInverted ){ + *ppInverted = 0; + *pnInverted = 0; + } + + while( 1 ){ + u8 eType; + + /* Test for EOF. */ + if( (rc = sessionInputBuffer(pInput, 2)) ) goto finished_invert; + if( pInput->iNext>=pInput->nData ) break; + eType = pInput->aData[pInput->iNext]; + + switch( eType ){ + case 'T': { + /* A 'table' record consists of: + ** + ** * A constant 'T' character, + ** * Number of columns in said table (a varint), + ** * An array of nCol bytes (sPK), + ** * A nul-terminated table name. + */ + int nByte; + int nVar; + pInput->iNext++; + if( (rc = sessionChangesetBufferTblhdr(pInput, &nByte)) ){ + goto finished_invert; + } + nVar = sessionVarintGet(&pInput->aData[pInput->iNext], &nCol); + sPK.nBuf = 0; + sessionAppendBlob(&sPK, &pInput->aData[pInput->iNext+nVar], nCol, &rc); + sessionAppendByte(&sOut, eType, &rc); + sessionAppendBlob(&sOut, &pInput->aData[pInput->iNext], nByte, &rc); + if( rc ) goto finished_invert; + + pInput->iNext += nByte; + sqlite3_free(apVal); + apVal = 0; + abPK = sPK.aBuf; + break; + } + + case SQLITE_INSERT: + case SQLITE_DELETE: { + int nByte; + int bIndirect = pInput->aData[pInput->iNext+1]; + int eType2 = (eType==SQLITE_DELETE ? SQLITE_INSERT : SQLITE_DELETE); + pInput->iNext += 2; + assert( rc==SQLITE_OK ); + rc = sessionChangesetBufferRecord(pInput, nCol, &nByte); + sessionAppendByte(&sOut, eType2, &rc); + sessionAppendByte(&sOut, bIndirect, &rc); + sessionAppendBlob(&sOut, &pInput->aData[pInput->iNext], nByte, &rc); + pInput->iNext += nByte; + if( rc ) goto finished_invert; + break; + } + + case SQLITE_UPDATE: { + int iCol; + + if( 0==apVal ){ + apVal = (sqlite3_value **)sqlite3_malloc64(sizeof(apVal[0])*nCol*2); + if( 0==apVal ){ + rc = SQLITE_NOMEM; + goto finished_invert; + } + memset(apVal, 0, sizeof(apVal[0])*nCol*2); + } + + /* Write the header for the new UPDATE change. Same as the original. */ + sessionAppendByte(&sOut, eType, &rc); + sessionAppendByte(&sOut, pInput->aData[pInput->iNext+1], &rc); + + /* Read the old.* and new.* records for the update change. */ + pInput->iNext += 2; + rc = sessionReadRecord(pInput, nCol, 0, &apVal[0], 0); + if( rc==SQLITE_OK ){ + rc = sessionReadRecord(pInput, nCol, 0, &apVal[nCol], 0); + } + + /* Write the new old.* record. Consists of the PK columns from the + ** original old.* record, and the other values from the original + ** new.* record. */ + for(iCol=0; iCol=sessions_strm_chunk_size ){ + rc = xOutput(pOut, sOut.aBuf, sOut.nBuf); + sOut.nBuf = 0; + if( rc!=SQLITE_OK ) goto finished_invert; + } + } + + assert( rc==SQLITE_OK ); + if( pnInverted && ALWAYS(ppInverted) ){ + *pnInverted = sOut.nBuf; + *ppInverted = sOut.aBuf; + sOut.aBuf = 0; + }else if( sOut.nBuf>0 && ALWAYS(xOutput!=0) ){ + rc = xOutput(pOut, sOut.aBuf, sOut.nBuf); + } + + finished_invert: + sqlite3_free(sOut.aBuf); + sqlite3_free(apVal); + sqlite3_free(sPK.aBuf); + return rc; +} + + +/* +** Invert a changeset object. +*/ +int sqlite3changeset_invert( + int nChangeset, /* Number of bytes in input */ + const void *pChangeset, /* Input changeset */ + int *pnInverted, /* OUT: Number of bytes in output changeset */ + void **ppInverted /* OUT: Inverse of pChangeset */ +){ + SessionInput sInput; + + /* Set up the input stream */ + memset(&sInput, 0, sizeof(SessionInput)); + sInput.nData = nChangeset; + sInput.aData = (u8*)pChangeset; + + return sessionChangesetInvert(&sInput, 0, 0, pnInverted, ppInverted); +} + +/* +** Streaming version of sqlite3changeset_invert(). +*/ +int sqlite3changeset_invert_strm( + int (*xInput)(void *pIn, void *pData, int *pnData), + void *pIn, + int (*xOutput)(void *pOut, const void *pData, int nData), + void *pOut +){ + SessionInput sInput; + int rc; + + /* Set up the input stream */ + memset(&sInput, 0, sizeof(SessionInput)); + sInput.xInput = xInput; + sInput.pIn = pIn; + + rc = sessionChangesetInvert(&sInput, xOutput, pOut, 0, 0); + sqlite3_free(sInput.buf.aBuf); + return rc; +} + + +typedef struct SessionUpdate SessionUpdate; +struct SessionUpdate { + sqlite3_stmt *pStmt; + u32 *aMask; + SessionUpdate *pNext; +}; + +typedef struct SessionApplyCtx SessionApplyCtx; +struct SessionApplyCtx { + sqlite3 *db; + sqlite3_stmt *pDelete; /* DELETE statement */ + sqlite3_stmt *pInsert; /* INSERT statement */ + sqlite3_stmt *pSelect; /* SELECT statement */ + int nCol; /* Size of azCol[] and abPK[] arrays */ + const char **azCol; /* Array of column names */ + u8 *abPK; /* Boolean array - true if column is in PK */ + u32 *aUpdateMask; /* Used by sessionUpdateFind */ + SessionUpdate *pUp; + int bStat1; /* True if table is sqlite_stat1 */ + int bDeferConstraints; /* True to defer constraints */ + int bInvertConstraints; /* Invert when iterating constraints buffer */ + SessionBuffer constraints; /* Deferred constraints are stored here */ + SessionBuffer rebase; /* Rebase information (if any) here */ + u8 bRebaseStarted; /* If table header is already in rebase */ + u8 bRebase; /* True to collect rebase information */ + u8 bIgnoreNoop; /* True to ignore no-op conflicts */ + int bRowid; + char *zErr; /* Error message, if any */ +}; + +/* Number of prepared UPDATE statements to cache. */ +#define SESSION_UPDATE_CACHE_SZ 12 + +/* +** Find a prepared UPDATE statement suitable for the UPDATE step currently +** being visited by the iterator. The UPDATE is of the form: +** +** UPDATE tbl SET col = ?, col2 = ? WHERE pk1 IS ? AND pk2 IS ? +*/ +static int sessionUpdateFind( + sqlite3_changeset_iter *pIter, + SessionApplyCtx *p, + int bPatchset, + sqlite3_stmt **ppStmt +){ + int rc = SQLITE_OK; + SessionUpdate *pUp = 0; + int nCol = pIter->nCol; + int nU32 = (pIter->nCol+33)/32; + int ii; + + if( p->aUpdateMask==0 ){ + p->aUpdateMask = sqlite3_malloc(nU32*sizeof(u32)); + if( p->aUpdateMask==0 ){ + rc = SQLITE_NOMEM; + } + } + + if( rc==SQLITE_OK ){ + memset(p->aUpdateMask, 0, nU32*sizeof(u32)); + rc = SQLITE_CORRUPT; + for(ii=0; iinCol; ii++){ + if( sessionChangesetNew(pIter, ii) ){ + p->aUpdateMask[ii/32] |= (1<<(ii%32)); + rc = SQLITE_OK; + } + } + } + + if( rc==SQLITE_OK ){ + if( bPatchset ) p->aUpdateMask[nCol/32] |= (1<<(nCol%32)); + + if( p->pUp ){ + int nUp = 0; + SessionUpdate **pp = &p->pUp; + while( 1 ){ + nUp++; + if( 0==memcmp(p->aUpdateMask, (*pp)->aMask, nU32*sizeof(u32)) ){ + pUp = *pp; + *pp = pUp->pNext; + pUp->pNext = p->pUp; + p->pUp = pUp; + break; + } + + if( (*pp)->pNext ){ + pp = &(*pp)->pNext; + }else{ + if( nUp>=SESSION_UPDATE_CACHE_SZ ){ + sqlite3_finalize((*pp)->pStmt); + sqlite3_free(*pp); + *pp = 0; + } + break; + } + } + } + + if( pUp==0 ){ + int nByte = sizeof(SessionUpdate) * nU32*sizeof(u32); + int bStat1 = (sqlite3_stricmp(pIter->zTab, "sqlite_stat1")==0); + pUp = (SessionUpdate*)sqlite3_malloc(nByte); + if( pUp==0 ){ + rc = SQLITE_NOMEM; + }else{ + const char *zSep = ""; + SessionBuffer buf; + + memset(&buf, 0, sizeof(buf)); + pUp->aMask = (u32*)&pUp[1]; + memcpy(pUp->aMask, p->aUpdateMask, nU32*sizeof(u32)); + + sessionAppendStr(&buf, "UPDATE main.", &rc); + sessionAppendIdent(&buf, pIter->zTab, &rc); + sessionAppendStr(&buf, " SET ", &rc); + + /* Create the assignments part of the UPDATE */ + for(ii=0; iinCol; ii++){ + if( p->abPK[ii]==0 && sessionChangesetNew(pIter, ii) ){ + sessionAppendStr(&buf, zSep, &rc); + sessionAppendIdent(&buf, p->azCol[ii], &rc); + sessionAppendStr(&buf, " = ?", &rc); + sessionAppendInteger(&buf, ii*2+1, &rc); + zSep = ", "; + } + } + + /* Create the WHERE clause part of the UPDATE */ + zSep = ""; + sessionAppendStr(&buf, " WHERE ", &rc); + for(ii=0; iinCol; ii++){ + if( p->abPK[ii] || (bPatchset==0 && sessionChangesetOld(pIter, ii)) ){ + sessionAppendStr(&buf, zSep, &rc); + if( bStat1 && ii==1 ){ + assert( sqlite3_stricmp(p->azCol[ii], "idx")==0 ); + sessionAppendStr(&buf, + "idx IS CASE " + "WHEN length(?4)=0 AND typeof(?4)='blob' THEN NULL " + "ELSE ?4 END ", &rc + ); + }else{ + sessionAppendIdent(&buf, p->azCol[ii], &rc); + sessionAppendStr(&buf, " IS ?", &rc); + sessionAppendInteger(&buf, ii*2+2, &rc); + } + zSep = " AND "; + } + } + + if( rc==SQLITE_OK ){ + char *zSql = (char*)buf.aBuf; + rc = sqlite3_prepare_v2(p->db, zSql, buf.nBuf, &pUp->pStmt, 0); + } + + if( rc!=SQLITE_OK ){ + sqlite3_free(pUp); + pUp = 0; + }else{ + pUp->pNext = p->pUp; + p->pUp = pUp; + } + sqlite3_free(buf.aBuf); + } + } + } + + assert( (rc==SQLITE_OK)==(pUp!=0) ); + if( pUp ){ + *ppStmt = pUp->pStmt; + }else{ + *ppStmt = 0; + } + return rc; +} + +/* +** Free all cached UPDATE statements. +*/ +static void sessionUpdateFree(SessionApplyCtx *p){ + SessionUpdate *pUp; + SessionUpdate *pNext; + for(pUp=p->pUp; pUp; pUp=pNext){ + pNext = pUp->pNext; + sqlite3_finalize(pUp->pStmt); + sqlite3_free(pUp); + } + p->pUp = 0; + sqlite3_free(p->aUpdateMask); + p->aUpdateMask = 0; +} + +/* +** Formulate a statement to DELETE a row from database db. Assuming a table +** structure like this: +** +** CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c)); +** +** The DELETE statement looks like this: +** +** DELETE FROM x WHERE a = :1 AND c = :3 AND (:5 OR b IS :2 AND d IS :4) +** +** Variable :5 (nCol+1) is a boolean. It should be set to 0 if we require +** matching b and d values, or 1 otherwise. The second case comes up if the +** conflict handler is invoked with NOTFOUND and returns CHANGESET_REPLACE. +** +** If successful, SQLITE_OK is returned and SessionApplyCtx.pDelete is left +** pointing to the prepared version of the SQL statement. +*/ +static int sessionDeleteRow( + sqlite3 *db, /* Database handle */ + const char *zTab, /* Table name */ + SessionApplyCtx *p /* Session changeset-apply context */ +){ + int i; + const char *zSep = ""; + int rc = SQLITE_OK; + SessionBuffer buf = {0, 0, 0}; + int nPk = 0; + + sessionAppendStr(&buf, "DELETE FROM main.", &rc); + sessionAppendIdent(&buf, zTab, &rc); + sessionAppendStr(&buf, " WHERE ", &rc); + + for(i=0; inCol; i++){ + if( p->abPK[i] ){ + nPk++; + sessionAppendStr(&buf, zSep, &rc); + sessionAppendIdent(&buf, p->azCol[i], &rc); + sessionAppendStr(&buf, " = ?", &rc); + sessionAppendInteger(&buf, i+1, &rc); + zSep = " AND "; + } + } + + if( nPknCol ){ + sessionAppendStr(&buf, " AND (?", &rc); + sessionAppendInteger(&buf, p->nCol+1, &rc); + sessionAppendStr(&buf, " OR ", &rc); + + zSep = ""; + for(i=0; inCol; i++){ + if( !p->abPK[i] ){ + sessionAppendStr(&buf, zSep, &rc); + sessionAppendIdent(&buf, p->azCol[i], &rc); + sessionAppendStr(&buf, " IS ?", &rc); + sessionAppendInteger(&buf, i+1, &rc); + zSep = "AND "; + } + } + sessionAppendStr(&buf, ")", &rc); + } + + if( rc==SQLITE_OK ){ + rc = sessionPrepare(db, &p->pDelete, &p->zErr, (char*)buf.aBuf); + } + sqlite3_free(buf.aBuf); + + return rc; +} + +/* +** Formulate and prepare an SQL statement to query table zTab by primary +** key. Assuming the following table structure: +** +** CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c)); +** +** The SELECT statement looks like this: +** +** SELECT * FROM x WHERE a = ?1 AND c = ?3 +** +** If successful, SQLITE_OK is returned and SessionApplyCtx.pSelect is left +** pointing to the prepared version of the SQL statement. +*/ +static int sessionSelectRow( + sqlite3 *db, /* Database handle */ + const char *zTab, /* Table name */ + SessionApplyCtx *p /* Session changeset-apply context */ +){ + /* TODO */ + return sessionSelectStmt(db, p->bIgnoreNoop, + "main", zTab, p->bRowid, p->nCol, p->azCol, p->abPK, &p->pSelect, &p->zErr + ); +} + +/* +** Formulate and prepare an INSERT statement to add a record to table zTab. +** For example: +** +** INSERT INTO main."zTab" VALUES(?1, ?2, ?3 ...); +** +** If successful, SQLITE_OK is returned and SessionApplyCtx.pInsert is left +** pointing to the prepared version of the SQL statement. +*/ +static int sessionInsertRow( + sqlite3 *db, /* Database handle */ + const char *zTab, /* Table name */ + SessionApplyCtx *p /* Session changeset-apply context */ +){ + int rc = SQLITE_OK; + int i; + SessionBuffer buf = {0, 0, 0}; + + sessionAppendStr(&buf, "INSERT INTO main.", &rc); + sessionAppendIdent(&buf, zTab, &rc); + sessionAppendStr(&buf, "(", &rc); + for(i=0; inCol; i++){ + if( i!=0 ) sessionAppendStr(&buf, ", ", &rc); + sessionAppendIdent(&buf, p->azCol[i], &rc); + } + + sessionAppendStr(&buf, ") VALUES(?", &rc); + for(i=1; inCol; i++){ + sessionAppendStr(&buf, ", ?", &rc); + } + sessionAppendStr(&buf, ")", &rc); + + if( rc==SQLITE_OK ){ + rc = sessionPrepare(db, &p->pInsert, &p->zErr, (char*)buf.aBuf); + } + sqlite3_free(buf.aBuf); + return rc; +} + +/* +** Prepare statements for applying changes to the sqlite_stat1 table. +** These are similar to those created by sessionSelectRow(), +** sessionInsertRow(), sessionUpdateRow() and sessionDeleteRow() for +** other tables. +*/ +static int sessionStat1Sql(sqlite3 *db, SessionApplyCtx *p){ + int rc = sessionSelectRow(db, "sqlite_stat1", p); + if( rc==SQLITE_OK ){ + rc = sessionPrepare(db, &p->pInsert, 0, + "INSERT INTO main.sqlite_stat1 VALUES(?1, " + "CASE WHEN length(?2)=0 AND typeof(?2)='blob' THEN NULL ELSE ?2 END, " + "?3)" + ); + } + if( rc==SQLITE_OK ){ + rc = sessionPrepare(db, &p->pDelete, 0, + "DELETE FROM main.sqlite_stat1 WHERE tbl=?1 AND idx IS " + "CASE WHEN length(?2)=0 AND typeof(?2)='blob' THEN NULL ELSE ?2 END " + "AND (?4 OR stat IS ?3)" + ); + } + return rc; +} + +/* +** A wrapper around sqlite3_bind_value() that detects an extra problem. +** See comments in the body of this function for details. +*/ +static int sessionBindValue( + sqlite3_stmt *pStmt, /* Statement to bind value to */ + int i, /* Parameter number to bind to */ + sqlite3_value *pVal /* Value to bind */ +){ + int eType = sqlite3_value_type(pVal); + /* COVERAGE: The (pVal->z==0) branch is never true using current versions + ** of SQLite. If a malloc fails in an sqlite3_value_xxx() function, either + ** the (pVal->z) variable remains as it was or the type of the value is + ** set to SQLITE_NULL. */ + if( (eType==SQLITE_TEXT || eType==SQLITE_BLOB) && pVal->z==0 ){ + /* This condition occurs when an earlier OOM in a call to + ** sqlite3_value_text() or sqlite3_value_blob() (perhaps from within + ** a conflict-handler) has zeroed the pVal->z pointer. Return NOMEM. */ + return SQLITE_NOMEM; + } + return sqlite3_bind_value(pStmt, i, pVal); +} + +/* +** Iterator pIter must point to an SQLITE_INSERT entry. This function +** transfers new.* values from the current iterator entry to statement +** pStmt. The table being inserted into has nCol columns. +** +** New.* value $i from the iterator is bound to variable ($i+1) of +** statement pStmt. If parameter abPK is NULL, all values from 0 to (nCol-1) +** are transfered to the statement. Otherwise, if abPK is not NULL, it points +** to an array nCol elements in size. In this case only those values for +** which abPK[$i] is true are read from the iterator and bound to the +** statement. +** +** An SQLite error code is returned if an error occurs. Otherwise, SQLITE_OK. +*/ +static int sessionBindRow( + sqlite3_changeset_iter *pIter, /* Iterator to read values from */ + int(*xValue)(sqlite3_changeset_iter *, int, sqlite3_value **), + int nCol, /* Number of columns */ + u8 *abPK, /* If not NULL, bind only if true */ + sqlite3_stmt *pStmt /* Bind values to this statement */ +){ + int i; + int rc = SQLITE_OK; + + /* Neither sqlite3changeset_old or sqlite3changeset_new can fail if the + ** argument iterator points to a suitable entry. Make sure that xValue + ** is one of these to guarantee that it is safe to ignore the return + ** in the code below. */ + assert( xValue==sqlite3changeset_old || xValue==sqlite3changeset_new ); + + for(i=0; rc==SQLITE_OK && ipSelect; + int rc; /* Return code */ + int nCol; /* Number of columns in table */ + int op; /* Changset operation (SQLITE_UPDATE etc.) */ + const char *zDummy; /* Unused */ + + sqlite3_clear_bindings(pSelect); + sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0); + rc = sessionBindRow(pIter, + op==SQLITE_INSERT ? sqlite3changeset_new : sqlite3changeset_old, + nCol, p->abPK, pSelect + ); + + if( op!=SQLITE_DELETE && p->bIgnoreNoop ){ + int ii; + for(ii=0; rc==SQLITE_OK && iiabPK[ii]==0 ){ + sqlite3_value *pVal = 0; + sqlite3changeset_new(pIter, ii, &pVal); + sqlite3_bind_int(pSelect, ii+1+nCol, (pVal==0)); + if( pVal ) rc = sessionBindValue(pSelect, ii+1, pVal); + } + } + } + + if( rc==SQLITE_OK ){ + rc = sqlite3_step(pSelect); + if( rc!=SQLITE_ROW ) rc = sqlite3_reset(pSelect); + } + + return rc; +} + +/* +** This function is called from within sqlite3changeset_apply_v2() when +** a conflict is encountered and resolved using conflict resolution +** mode eType (either SQLITE_CHANGESET_OMIT or SQLITE_CHANGESET_REPLACE).. +** It adds a conflict resolution record to the buffer in +** SessionApplyCtx.rebase, which will eventually be returned to the caller +** of apply_v2() as the "rebase" buffer. +** +** Return SQLITE_OK if successful, or an SQLite error code otherwise. +*/ +static int sessionRebaseAdd( + SessionApplyCtx *p, /* Apply context */ + int eType, /* Conflict resolution (OMIT or REPLACE) */ + sqlite3_changeset_iter *pIter /* Iterator pointing at current change */ +){ + int rc = SQLITE_OK; + if( p->bRebase ){ + int i; + int eOp = pIter->op; + if( p->bRebaseStarted==0 ){ + /* Append a table-header to the rebase buffer */ + const char *zTab = pIter->zTab; + sessionAppendByte(&p->rebase, 'T', &rc); + sessionAppendVarint(&p->rebase, p->nCol, &rc); + sessionAppendBlob(&p->rebase, p->abPK, p->nCol, &rc); + sessionAppendBlob(&p->rebase, (u8*)zTab, (int)strlen(zTab)+1, &rc); + p->bRebaseStarted = 1; + } + + assert( eType==SQLITE_CHANGESET_REPLACE||eType==SQLITE_CHANGESET_OMIT ); + assert( eOp==SQLITE_DELETE || eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE ); + + sessionAppendByte(&p->rebase, + (eOp==SQLITE_DELETE ? SQLITE_DELETE : SQLITE_INSERT), &rc + ); + sessionAppendByte(&p->rebase, (eType==SQLITE_CHANGESET_REPLACE), &rc); + for(i=0; inCol; i++){ + sqlite3_value *pVal = 0; + if( eOp==SQLITE_DELETE || (eOp==SQLITE_UPDATE && p->abPK[i]) ){ + sqlite3changeset_old(pIter, i, &pVal); + }else{ + sqlite3changeset_new(pIter, i, &pVal); + } + sessionAppendValue(&p->rebase, pVal, &rc); + } + } + return rc; +} + +/* +** Invoke the conflict handler for the change that the changeset iterator +** currently points to. +** +** Argument eType must be either CHANGESET_DATA or CHANGESET_CONFLICT. +** If argument pbReplace is NULL, then the type of conflict handler invoked +** depends solely on eType, as follows: +** +** eType value Value passed to xConflict +** ------------------------------------------------- +** CHANGESET_DATA CHANGESET_NOTFOUND +** CHANGESET_CONFLICT CHANGESET_CONSTRAINT +** +** Or, if pbReplace is not NULL, then an attempt is made to find an existing +** record with the same primary key as the record about to be deleted, updated +** or inserted. If such a record can be found, it is available to the conflict +** handler as the "conflicting" record. In this case the type of conflict +** handler invoked is as follows: +** +** eType value PK Record found? Value passed to xConflict +** ---------------------------------------------------------------- +** CHANGESET_DATA Yes CHANGESET_DATA +** CHANGESET_DATA No CHANGESET_NOTFOUND +** CHANGESET_CONFLICT Yes CHANGESET_CONFLICT +** CHANGESET_CONFLICT No CHANGESET_CONSTRAINT +** +** If pbReplace is not NULL, and a record with a matching PK is found, and +** the conflict handler function returns SQLITE_CHANGESET_REPLACE, *pbReplace +** is set to non-zero before returning SQLITE_OK. +** +** If the conflict handler returns SQLITE_CHANGESET_ABORT, SQLITE_ABORT is +** returned. Or, if the conflict handler returns an invalid value, +** SQLITE_MISUSE. If the conflict handler returns SQLITE_CHANGESET_OMIT, +** this function returns SQLITE_OK. +*/ +static int sessionConflictHandler( + int eType, /* Either CHANGESET_DATA or CONFLICT */ + SessionApplyCtx *p, /* changeset_apply() context */ + sqlite3_changeset_iter *pIter, /* Changeset iterator */ + int(*xConflict)(void *, int, sqlite3_changeset_iter*), + void *pCtx, /* First argument for conflict handler */ + int *pbReplace /* OUT: Set to true if PK row is found */ +){ + int res = SQLITE_CHANGESET_OMIT;/* Value returned by conflict handler */ + int rc; + int nCol; + int op; + const char *zDummy; + + sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0); + + assert( eType==SQLITE_CHANGESET_CONFLICT || eType==SQLITE_CHANGESET_DATA ); + assert( SQLITE_CHANGESET_CONFLICT+1==SQLITE_CHANGESET_CONSTRAINT ); + assert( SQLITE_CHANGESET_DATA+1==SQLITE_CHANGESET_NOTFOUND ); + + /* Bind the new.* PRIMARY KEY values to the SELECT statement. */ + if( pbReplace ){ + rc = sessionSeekToRow(pIter, p); + }else{ + rc = SQLITE_OK; + } + + if( rc==SQLITE_ROW ){ + /* There exists another row with the new.* primary key. */ + if( 0==p->bIgnoreNoop + || 0==sqlite3_column_int(p->pSelect, sqlite3_column_count(p->pSelect)-1) + ){ + pIter->pConflict = p->pSelect; + res = xConflict(pCtx, eType, pIter); + pIter->pConflict = 0; + } + rc = sqlite3_reset(p->pSelect); + }else if( rc==SQLITE_OK ){ + if( p->bDeferConstraints && eType==SQLITE_CHANGESET_CONFLICT ){ + /* Instead of invoking the conflict handler, append the change blob + ** to the SessionApplyCtx.constraints buffer. */ + u8 *aBlob = &pIter->in.aData[pIter->in.iCurrent]; + int nBlob = pIter->in.iNext - pIter->in.iCurrent; + sessionAppendBlob(&p->constraints, aBlob, nBlob, &rc); + return SQLITE_OK; + }else if( p->bIgnoreNoop==0 || op!=SQLITE_DELETE + || eType==SQLITE_CHANGESET_CONFLICT + ){ + /* No other row with the new.* primary key. */ + res = xConflict(pCtx, eType+1, pIter); + if( res==SQLITE_CHANGESET_REPLACE ) rc = SQLITE_MISUSE; + } + } + + if( rc==SQLITE_OK ){ + switch( res ){ + case SQLITE_CHANGESET_REPLACE: + assert( pbReplace ); + *pbReplace = 1; + break; + + case SQLITE_CHANGESET_OMIT: + break; + + case SQLITE_CHANGESET_ABORT: + rc = SQLITE_ABORT; + break; + + default: + rc = SQLITE_MISUSE; + break; + } + if( rc==SQLITE_OK ){ + rc = sessionRebaseAdd(p, res, pIter); + } + } + + return rc; +} + +/* +** Attempt to apply the change that the iterator passed as the first argument +** currently points to to the database. If a conflict is encountered, invoke +** the conflict handler callback. +** +** If argument pbRetry is NULL, then ignore any CHANGESET_DATA conflict. If +** one is encountered, update or delete the row with the matching primary key +** instead. Or, if pbRetry is not NULL and a CHANGESET_DATA conflict occurs, +** invoke the conflict handler. If it returns CHANGESET_REPLACE, set *pbRetry +** to true before returning. In this case the caller will invoke this function +** again, this time with pbRetry set to NULL. +** +** If argument pbReplace is NULL and a CHANGESET_CONFLICT conflict is +** encountered invoke the conflict handler with CHANGESET_CONSTRAINT instead. +** Or, if pbReplace is not NULL, invoke it with CHANGESET_CONFLICT. If such +** an invocation returns SQLITE_CHANGESET_REPLACE, set *pbReplace to true +** before retrying. In this case the caller attempts to remove the conflicting +** row before invoking this function again, this time with pbReplace set +** to NULL. +** +** If any conflict handler returns SQLITE_CHANGESET_ABORT, this function +** returns SQLITE_ABORT. Otherwise, if no error occurs, SQLITE_OK is +** returned. +*/ +static int sessionApplyOneOp( + sqlite3_changeset_iter *pIter, /* Changeset iterator */ + SessionApplyCtx *p, /* changeset_apply() context */ + int(*xConflict)(void *, int, sqlite3_changeset_iter *), + void *pCtx, /* First argument for the conflict handler */ + int *pbReplace, /* OUT: True to remove PK row and retry */ + int *pbRetry /* OUT: True to retry. */ +){ + const char *zDummy; + int op; + int nCol; + int rc = SQLITE_OK; + + assert( p->pDelete && p->pInsert && p->pSelect ); + assert( p->azCol && p->abPK ); + assert( !pbReplace || *pbReplace==0 ); + + sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0); + + if( op==SQLITE_DELETE ){ + + /* Bind values to the DELETE statement. If conflict handling is required, + ** bind values for all columns and set bound variable (nCol+1) to true. + ** Or, if conflict handling is not required, bind just the PK column + ** values and, if it exists, set (nCol+1) to false. Conflict handling + ** is not required if: + ** + ** * this is a patchset, or + ** * (pbRetry==0), or + ** * all columns of the table are PK columns (in this case there is + ** no (nCol+1) variable to bind to). + */ + u8 *abPK = (pIter->bPatchset ? p->abPK : 0); + rc = sessionBindRow(pIter, sqlite3changeset_old, nCol, abPK, p->pDelete); + if( rc==SQLITE_OK && sqlite3_bind_parameter_count(p->pDelete)>nCol ){ + rc = sqlite3_bind_int(p->pDelete, nCol+1, (pbRetry==0 || abPK)); + } + if( rc!=SQLITE_OK ) return rc; + + sqlite3_step(p->pDelete); + rc = sqlite3_reset(p->pDelete); + if( rc==SQLITE_OK && sqlite3_changes(p->db)==0 ){ + rc = sessionConflictHandler( + SQLITE_CHANGESET_DATA, p, pIter, xConflict, pCtx, pbRetry + ); + }else if( (rc&0xff)==SQLITE_CONSTRAINT ){ + rc = sessionConflictHandler( + SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, 0 + ); + } + + }else if( op==SQLITE_UPDATE ){ + int i; + sqlite3_stmt *pUp = 0; + int bPatchset = (pbRetry==0 || pIter->bPatchset); + + rc = sessionUpdateFind(pIter, p, bPatchset, &pUp); + + /* Bind values to the UPDATE statement. */ + for(i=0; rc==SQLITE_OK && iabPK[i] || (bPatchset==0 && pOld) ){ + rc = sessionBindValue(pUp, i*2+2, pOld); + } + if( rc==SQLITE_OK && pNew ){ + rc = sessionBindValue(pUp, i*2+1, pNew); + } + } + if( rc!=SQLITE_OK ) return rc; + + /* Attempt the UPDATE. In the case of a NOTFOUND or DATA conflict, + ** the result will be SQLITE_OK with 0 rows modified. */ + sqlite3_step(pUp); + rc = sqlite3_reset(pUp); + + if( rc==SQLITE_OK && sqlite3_changes(p->db)==0 ){ + /* A NOTFOUND or DATA error. Search the table to see if it contains + ** a row with a matching primary key. If so, this is a DATA conflict. + ** Otherwise, if there is no primary key match, it is a NOTFOUND. */ + + rc = sessionConflictHandler( + SQLITE_CHANGESET_DATA, p, pIter, xConflict, pCtx, pbRetry + ); + + }else if( (rc&0xff)==SQLITE_CONSTRAINT ){ + /* This is always a CONSTRAINT conflict. */ + rc = sessionConflictHandler( + SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, 0 + ); + } + + }else{ + assert( op==SQLITE_INSERT ); + if( p->bStat1 ){ + /* Check if there is a conflicting row. For sqlite_stat1, this needs + ** to be done using a SELECT, as there is no PRIMARY KEY in the + ** database schema to throw an exception if a duplicate is inserted. */ + rc = sessionSeekToRow(pIter, p); + if( rc==SQLITE_ROW ){ + rc = SQLITE_CONSTRAINT; + sqlite3_reset(p->pSelect); + } + } + + if( rc==SQLITE_OK ){ + rc = sessionBindRow(pIter, sqlite3changeset_new, nCol, 0, p->pInsert); + if( rc!=SQLITE_OK ) return rc; + + sqlite3_step(p->pInsert); + rc = sqlite3_reset(p->pInsert); + } + + if( (rc&0xff)==SQLITE_CONSTRAINT ){ + rc = sessionConflictHandler( + SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, pbReplace + ); + } + } + + return rc; +} + +/* +** Attempt to apply the change that the iterator passed as the first argument +** currently points to to the database. If a conflict is encountered, invoke +** the conflict handler callback. +** +** The difference between this function and sessionApplyOne() is that this +** function handles the case where the conflict-handler is invoked and +** returns SQLITE_CHANGESET_REPLACE - indicating that the change should be +** retried in some manner. +*/ +static int sessionApplyOneWithRetry( + sqlite3 *db, /* Apply change to "main" db of this handle */ + sqlite3_changeset_iter *pIter, /* Changeset iterator to read change from */ + SessionApplyCtx *pApply, /* Apply context */ + int(*xConflict)(void*, int, sqlite3_changeset_iter*), + void *pCtx /* First argument passed to xConflict */ +){ + int bReplace = 0; + int bRetry = 0; + int rc; + + rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, &bReplace, &bRetry); + if( rc==SQLITE_OK ){ + /* If the bRetry flag is set, the change has not been applied due to an + ** SQLITE_CHANGESET_DATA problem (i.e. this is an UPDATE or DELETE and + ** a row with the correct PK is present in the db, but one or more other + ** fields do not contain the expected values) and the conflict handler + ** returned SQLITE_CHANGESET_REPLACE. In this case retry the operation, + ** but pass NULL as the final argument so that sessionApplyOneOp() ignores + ** the SQLITE_CHANGESET_DATA problem. */ + if( bRetry ){ + assert( pIter->op==SQLITE_UPDATE || pIter->op==SQLITE_DELETE ); + rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0); + } + + /* If the bReplace flag is set, the change is an INSERT that has not + ** been performed because the database already contains a row with the + ** specified primary key and the conflict handler returned + ** SQLITE_CHANGESET_REPLACE. In this case remove the conflicting row + ** before reattempting the INSERT. */ + else if( bReplace ){ + assert( pIter->op==SQLITE_INSERT ); + rc = sqlite3_exec(db, "SAVEPOINT replace_op", 0, 0, 0); + if( rc==SQLITE_OK ){ + rc = sessionBindRow(pIter, + sqlite3changeset_new, pApply->nCol, pApply->abPK, pApply->pDelete); + sqlite3_bind_int(pApply->pDelete, pApply->nCol+1, 1); + } + if( rc==SQLITE_OK ){ + sqlite3_step(pApply->pDelete); + rc = sqlite3_reset(pApply->pDelete); + } + if( rc==SQLITE_OK ){ + rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0); + } + if( rc==SQLITE_OK ){ + rc = sqlite3_exec(db, "RELEASE replace_op", 0, 0, 0); + } + } + } + + return rc; +} + +/* +** Retry the changes accumulated in the pApply->constraints buffer. +*/ +static int sessionRetryConstraints( + sqlite3 *db, + int bPatchset, + const char *zTab, + SessionApplyCtx *pApply, + int(*xConflict)(void*, int, sqlite3_changeset_iter*), + void *pCtx /* First argument passed to xConflict */ +){ + int rc = SQLITE_OK; + + while( pApply->constraints.nBuf ){ + sqlite3_changeset_iter *pIter2 = 0; + SessionBuffer cons = pApply->constraints; + memset(&pApply->constraints, 0, sizeof(SessionBuffer)); + + rc = sessionChangesetStart( + &pIter2, 0, 0, cons.nBuf, cons.aBuf, pApply->bInvertConstraints, 1 + ); + if( rc==SQLITE_OK ){ + size_t nByte = 2*pApply->nCol*sizeof(sqlite3_value*); + int rc2; + pIter2->bPatchset = bPatchset; + pIter2->zTab = (char*)zTab; + pIter2->nCol = pApply->nCol; + pIter2->abPK = pApply->abPK; + sessionBufferGrow(&pIter2->tblhdr, nByte, &rc); + pIter2->apValue = (sqlite3_value**)pIter2->tblhdr.aBuf; + if( rc==SQLITE_OK ) memset(pIter2->apValue, 0, nByte); + + while( rc==SQLITE_OK && SQLITE_ROW==sqlite3changeset_next(pIter2) ){ + rc = sessionApplyOneWithRetry(db, pIter2, pApply, xConflict, pCtx); + } + + rc2 = sqlite3changeset_finalize(pIter2); + if( rc==SQLITE_OK ) rc = rc2; + } + assert( pApply->bDeferConstraints || pApply->constraints.nBuf==0 ); + + sqlite3_free(cons.aBuf); + if( rc!=SQLITE_OK ) break; + if( pApply->constraints.nBuf>=cons.nBuf ){ + /* No progress was made on the last round. */ + pApply->bDeferConstraints = 0; + } + } + + return rc; +} + +/* +** Argument pIter is a changeset iterator that has been initialized, but +** not yet passed to sqlite3changeset_next(). This function applies the +** changeset to the main database attached to handle "db". The supplied +** conflict handler callback is invoked to resolve any conflicts encountered +** while applying the change. +*/ +static int sessionChangesetApply( + sqlite3 *db, /* Apply change to "main" db of this handle */ + sqlite3_changeset_iter *pIter, /* Changeset to apply */ + int(*xFilter)( + void *pCtx, /* Copy of sixth arg to _apply() */ + const char *zTab /* Table name */ + ), + int(*xFilterIter)( + void *pCtx, /* Copy of sixth arg to _apply() */ + sqlite3_changeset_iter *p + ), + int(*xConflict)( + void *pCtx, /* Copy of fifth arg to _apply() */ + int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */ + sqlite3_changeset_iter *p /* Handle describing change and conflict */ + ), + void *pCtx, /* First argument passed to xConflict */ + void **ppRebase, int *pnRebase, /* OUT: Rebase information */ + int flags /* SESSION_APPLY_XXX flags */ +){ + int schemaMismatch = 0; + int rc = SQLITE_OK; /* Return code */ + const char *zTab = 0; /* Name of current table */ + int nTab = 0; /* Result of sqlite3Strlen30(zTab) */ + SessionApplyCtx sApply; /* changeset_apply() context object */ + int bPatchset; + u64 savedFlag = db->flags & SQLITE_FkNoAction; + + assert( xConflict!=0 ); + + sqlite3_mutex_enter(sqlite3_db_mutex(db)); + if( flags & SQLITE_CHANGESETAPPLY_FKNOACTION ){ + db->flags |= ((u64)SQLITE_FkNoAction); + db->aDb[0].pSchema->schema_cookie -= 32; + } + + pIter->in.bNoDiscard = 1; + memset(&sApply, 0, sizeof(sApply)); + sApply.bRebase = (ppRebase && pnRebase); + sApply.bInvertConstraints = !!(flags & SQLITE_CHANGESETAPPLY_INVERT); + sApply.bIgnoreNoop = !!(flags & SQLITE_CHANGESETAPPLY_IGNORENOOP); + if( (flags & SQLITE_CHANGESETAPPLY_NOSAVEPOINT)==0 ){ + rc = sqlite3_exec(db, "SAVEPOINT changeset_apply", 0, 0, 0); + } + if( rc==SQLITE_OK ){ + rc = sqlite3_exec(db, "PRAGMA defer_foreign_keys = 1", 0, 0, 0); + } + while( rc==SQLITE_OK && SQLITE_ROW==sqlite3changeset_next(pIter) ){ + int nCol; + int op; + const char *zNew; + + sqlite3changeset_op(pIter, &zNew, &nCol, &op, 0); + + if( zTab==0 || sqlite3_strnicmp(zNew, zTab, nTab+1) ){ + u8 *abPK; + + rc = sessionRetryConstraints( + db, pIter->bPatchset, zTab, &sApply, xConflict, pCtx + ); + if( rc!=SQLITE_OK ) break; + + sessionUpdateFree(&sApply); + sqlite3_free((char*)sApply.azCol); /* cast works around VC++ bug */ + sqlite3_finalize(sApply.pDelete); + sqlite3_finalize(sApply.pInsert); + sqlite3_finalize(sApply.pSelect); + sApply.db = db; + sApply.pDelete = 0; + sApply.pInsert = 0; + sApply.pSelect = 0; + sApply.nCol = 0; + sApply.azCol = 0; + sApply.abPK = 0; + sApply.bStat1 = 0; + sApply.bDeferConstraints = 1; + sApply.bRebaseStarted = 0; + sApply.bRowid = 0; + memset(&sApply.constraints, 0, sizeof(SessionBuffer)); + + /* If an xFilter() callback was specified, invoke it now. If the + ** xFilter callback returns zero, skip this table. If it returns + ** non-zero, proceed. */ + schemaMismatch = (xFilter && (0==xFilter(pCtx, zNew))); + if( schemaMismatch ){ + zTab = sqlite3_mprintf("%s", zNew); + if( zTab==0 ){ + rc = SQLITE_NOMEM; + break; + } + nTab = (int)strlen(zTab); + sApply.azCol = (const char **)zTab; + }else{ + int nMinCol = 0; + int i; + + sqlite3changeset_pk(pIter, &abPK, 0); + rc = sessionTableInfo(0, db, "main", zNew, + &sApply.nCol, 0, &zTab, &sApply.azCol, 0, 0, + &sApply.abPK, &sApply.bRowid + ); + if( rc!=SQLITE_OK ) break; + for(i=0; ibPatchset; + if( rc==SQLITE_OK ){ + rc = sqlite3changeset_finalize(pIter); + }else{ + sqlite3changeset_finalize(pIter); + } + + if( rc==SQLITE_OK ){ + rc = sessionRetryConstraints(db, bPatchset, zTab, &sApply, xConflict, pCtx); + } + + if( rc==SQLITE_OK ){ + int nFk, notUsed; + sqlite3_db_status(db, SQLITE_DBSTATUS_DEFERRED_FKS, &nFk, ¬Used, 0); + if( nFk!=0 ){ + int res = SQLITE_CHANGESET_ABORT; + sqlite3_changeset_iter sIter; + memset(&sIter, 0, sizeof(sIter)); + sIter.nCol = nFk; + res = xConflict(pCtx, SQLITE_CHANGESET_FOREIGN_KEY, &sIter); + if( res!=SQLITE_CHANGESET_OMIT ){ + rc = SQLITE_CONSTRAINT; + } + } + } + + { + int rc2 = sqlite3_exec(db, "PRAGMA defer_foreign_keys = 0", 0, 0, 0); + if( rc==SQLITE_OK ) rc = rc2; + } + + if( (flags & SQLITE_CHANGESETAPPLY_NOSAVEPOINT)==0 ){ + if( rc==SQLITE_OK ){ + rc = sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0); + } + if( rc!=SQLITE_OK ){ + sqlite3_exec(db, "ROLLBACK TO changeset_apply", 0, 0, 0); + sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0); + } + } + + assert( sApply.bRebase || sApply.rebase.nBuf==0 ); + if( rc==SQLITE_OK && bPatchset==0 && sApply.bRebase ){ + assert( ppRebase!=0 && pnRebase!=0 ); + *ppRebase = (void*)sApply.rebase.aBuf; + *pnRebase = sApply.rebase.nBuf; + sApply.rebase.aBuf = 0; + } + sessionUpdateFree(&sApply); + sqlite3_finalize(sApply.pInsert); + sqlite3_finalize(sApply.pDelete); + sqlite3_finalize(sApply.pSelect); + sqlite3_free((char*)sApply.azCol); /* cast works around VC++ bug */ + sqlite3_free((char*)sApply.constraints.aBuf); + sqlite3_free((char*)sApply.rebase.aBuf); + + if( (flags & SQLITE_CHANGESETAPPLY_FKNOACTION) && savedFlag==0 ){ + assert( db->flags & SQLITE_FkNoAction ); + db->flags &= ~((u64)SQLITE_FkNoAction); + db->aDb[0].pSchema->schema_cookie -= 32; + } + + assert( rc!=SQLITE_OK || sApply.zErr==0 ); + sqlite3_set_errmsg(db, rc, sApply.zErr); + sqlite3_free(sApply.zErr); + + sqlite3_mutex_leave(sqlite3_db_mutex(db)); + return rc; +} + +/* +** This function is called by all six sqlite3changeset_apply() variants: +** +** + sqlite3changeset_apply() +** + sqlite3changeset_apply_v2() +** + sqlite3changeset_apply_v3() +** + sqlite3changeset_apply_strm() +** + sqlite3changeset_apply_strm_v2() +** + sqlite3changeset_apply_strm_v3() +** +** Arguments passed to this function are as follows: +** +** db: +** Database handle to apply changeset to main database of. +** +** nChangeset/pChangeset: +** These are both passed zero for the streaming variants. For the normal +** apply() functions, these are passed the size of and the buffer containing +** the changeset, respectively. +** +** xInput/pIn: +** These are both passed zero for the normal variants. For the streaming +** apply() functions, these are passed the input callback and context +** pointer, respectively. +** +** xFilter: +** The filter function as passed to apply() or apply_v2() (to filter by +** table name), if any. This is always NULL for apply_v3() calls. +** +** xFilterIter: +** The filter function as passed to apply_v3(), if any. +** +** xConflict: +** The conflict handler callback (must not be NULL). +** +** pCtx: +** The context pointer passed to the xFilter and xConflict handler callbacks. +** +** ppRebase, pnRebase: +** Zero for apply(). The rebase changeset output pointers, if any, for +** apply_v2() and apply_v3(). +** +** flags: +** Zero for apply(). The flags parameter for apply_v2() and apply_v3(). +*/ +static int sessionChangesetApplyV23( + sqlite3 *db, /* Apply change to "main" db of this handle */ + int nChangeset, /* Size of changeset in bytes */ + void *pChangeset, /* Changeset blob */ + int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */ + void *pIn, /* First arg for xInput */ + int(*xFilter)( + void *pCtx, /* Copy of sixth arg to _apply() */ + const char *zTab /* Table name */ + ), + int(*xFilterIter)( + void *pCtx, /* Copy of sixth arg to _apply() */ + sqlite3_changeset_iter *p /* Handle describing current change */ + ), + int(*xConflict)( + void *pCtx, /* Copy of sixth arg to _apply() */ + int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */ + sqlite3_changeset_iter *p /* Handle describing change and conflict */ + ), + void *pCtx, /* First argument passed to xConflict */ + void **ppRebase, int *pnRebase, + int flags +){ + sqlite3_changeset_iter *pIter; /* Iterator to skip through changeset */ + int bInverse = !!(flags & SQLITE_CHANGESETAPPLY_INVERT); + int rc = sessionChangesetStart( + &pIter, xInput, pIn, nChangeset, pChangeset, bInverse, 1 + ); + if( rc==SQLITE_OK ){ + rc = sessionChangesetApply(db, pIter, + xFilter, xFilterIter, xConflict, pCtx, ppRebase, pnRebase, flags + ); + } + return rc; +} + +/* +** Apply the changeset passed via pChangeset/nChangeset to the main +** database attached to handle "db". +*/ +int sqlite3changeset_apply_v2( + sqlite3 *db, /* Apply change to "main" db of this handle */ + int nChangeset, /* Size of changeset in bytes */ + void *pChangeset, /* Changeset blob */ + int(*xFilter)( + void *pCtx, /* Copy of sixth arg to _apply() */ + const char *zTab /* Table name */ + ), + int(*xConflict)( + void *pCtx, /* Copy of sixth arg to _apply() */ + int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */ + sqlite3_changeset_iter *p /* Handle describing change and conflict */ + ), + void *pCtx, /* First argument passed to xConflict */ + void **ppRebase, int *pnRebase, + int flags +){ + return sessionChangesetApplyV23(db, + nChangeset, pChangeset, 0, 0, + xFilter, 0, xConflict, pCtx, + ppRebase, pnRebase, flags + ); +} + +/* +** Apply the changeset passed via pChangeset/nChangeset to the main +** database attached to handle "db". +*/ +int sqlite3changeset_apply_v3( + sqlite3 *db, /* Apply change to "main" db of this handle */ + int nChangeset, /* Size of changeset in bytes */ + void *pChangeset, /* Changeset blob */ + int(*xFilter)( + void *pCtx, /* Copy of sixth arg to _apply() */ + sqlite3_changeset_iter *p /* Handle describing current change */ + ), + int(*xConflict)( + void *pCtx, /* Copy of sixth arg to _apply() */ + int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */ + sqlite3_changeset_iter *p /* Handle describing change and conflict */ + ), + void *pCtx, /* First argument passed to xConflict */ + void **ppRebase, int *pnRebase, + int flags +){ + return sessionChangesetApplyV23(db, + nChangeset, pChangeset, 0, 0, + 0, xFilter, xConflict, pCtx, + ppRebase, pnRebase, flags + ); +} + +/* +** Apply the changeset passed via pChangeset/nChangeset to the main database +** attached to handle "db". Invoke the supplied conflict handler callback +** to resolve any conflicts encountered while applying the change. +*/ +int sqlite3changeset_apply( + sqlite3 *db, /* Apply change to "main" db of this handle */ + int nChangeset, /* Size of changeset in bytes */ + void *pChangeset, /* Changeset blob */ + int(*xFilter)( + void *pCtx, /* Copy of sixth arg to _apply() */ + const char *zTab /* Table name */ + ), + int(*xConflict)( + void *pCtx, /* Copy of fifth arg to _apply() */ + int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */ + sqlite3_changeset_iter *p /* Handle describing change and conflict */ + ), + void *pCtx /* First argument passed to xConflict */ +){ + return sessionChangesetApplyV23(db, + nChangeset, pChangeset, 0, 0, + xFilter, 0, xConflict, pCtx, + 0, 0, 0 + ); +} + +/* +** Apply the changeset passed via xInput/pIn to the main database +** attached to handle "db". Invoke the supplied conflict handler callback +** to resolve any conflicts encountered while applying the change. +*/ +int sqlite3changeset_apply_v3_strm( + sqlite3 *db, /* Apply change to "main" db of this handle */ + int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */ + void *pIn, /* First arg for xInput */ + int(*xFilter)( + void *pCtx, /* Copy of sixth arg to _apply() */ + sqlite3_changeset_iter *p + ), + int(*xConflict)( + void *pCtx, /* Copy of sixth arg to _apply() */ + int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */ + sqlite3_changeset_iter *p /* Handle describing change and conflict */ + ), + void *pCtx, /* First argument passed to xConflict */ + void **ppRebase, int *pnRebase, + int flags +){ + return sessionChangesetApplyV23(db, + 0, 0, xInput, pIn, + 0, xFilter, xConflict, pCtx, + ppRebase, pnRebase, flags + ); +} +int sqlite3changeset_apply_v2_strm( + sqlite3 *db, /* Apply change to "main" db of this handle */ + int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */ + void *pIn, /* First arg for xInput */ + int(*xFilter)( + void *pCtx, /* Copy of sixth arg to _apply() */ + const char *zTab /* Table name */ + ), + int(*xConflict)( + void *pCtx, /* Copy of sixth arg to _apply() */ + int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */ + sqlite3_changeset_iter *p /* Handle describing change and conflict */ + ), + void *pCtx, /* First argument passed to xConflict */ + void **ppRebase, int *pnRebase, + int flags +){ + return sessionChangesetApplyV23(db, + 0, 0, xInput, pIn, + xFilter, 0, xConflict, pCtx, + ppRebase, pnRebase, flags + ); +} +int sqlite3changeset_apply_strm( + sqlite3 *db, /* Apply change to "main" db of this handle */ + int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */ + void *pIn, /* First arg for xInput */ + int(*xFilter)( + void *pCtx, /* Copy of sixth arg to _apply() */ + const char *zTab /* Table name */ + ), + int(*xConflict)( + void *pCtx, /* Copy of sixth arg to _apply() */ + int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */ + sqlite3_changeset_iter *p /* Handle describing change and conflict */ + ), + void *pCtx /* First argument passed to xConflict */ +){ + return sessionChangesetApplyV23(db, + 0, 0, xInput, pIn, + xFilter, 0, xConflict, pCtx, + 0, 0, 0 + ); +} + +/* +** sqlite3_changegroup handle. +*/ +struct sqlite3_changegroup { + int rc; /* Error code */ + int bPatch; /* True to accumulate patchsets */ + SessionTable *pList; /* List of tables in current patch */ + SessionBuffer rec; + + sqlite3 *db; /* Configured by changegroup_schema() */ + char *zDb; /* Configured by changegroup_schema() */ +}; + +/* +** This function is called to merge two changes to the same row together as +** part of an sqlite3changeset_concat() operation. A new change object is +** allocated and a pointer to it stored in *ppNew. +*/ +static int sessionChangeMerge( + SessionTable *pTab, /* Table structure */ + int bRebase, /* True for a rebase hash-table */ + int bPatchset, /* True for patchsets */ + SessionChange *pExist, /* Existing change */ + int op2, /* Second change operation */ + int bIndirect, /* True if second change is indirect */ + u8 *aRec, /* Second change record */ + int nRec, /* Number of bytes in aRec */ + SessionChange **ppNew /* OUT: Merged change */ +){ + SessionChange *pNew = 0; + int rc = SQLITE_OK; + assert( aRec!=0 ); + + if( !pExist ){ + pNew = (SessionChange *)sqlite3_malloc64(sizeof(SessionChange) + nRec); + if( !pNew ){ + return SQLITE_NOMEM; + } + memset(pNew, 0, sizeof(SessionChange)); + pNew->op = op2; + pNew->bIndirect = bIndirect; + pNew->aRecord = (u8*)&pNew[1]; + if( bIndirect==0 || bRebase==0 ){ + pNew->nRecord = nRec; + memcpy(pNew->aRecord, aRec, nRec); + }else{ + int i; + u8 *pIn = aRec; + u8 *pOut = pNew->aRecord; + for(i=0; inCol; i++){ + int nIn = sessionSerialLen(pIn); + if( *pIn==0 ){ + *pOut++ = 0; + }else if( pTab->abPK[i]==0 ){ + *pOut++ = 0xFF; + }else{ + memcpy(pOut, pIn, nIn); + pOut += nIn; + } + pIn += nIn; + } + pNew->nRecord = pOut - pNew->aRecord; + } + }else if( bRebase ){ + if( pExist->op==SQLITE_DELETE && pExist->bIndirect ){ + *ppNew = pExist; + }else{ + sqlite3_int64 nByte = nRec + pExist->nRecord + sizeof(SessionChange); + pNew = (SessionChange*)sqlite3_malloc64(nByte); + if( pNew==0 ){ + rc = SQLITE_NOMEM; + }else{ + int i; + u8 *a1 = pExist->aRecord; + u8 *a2 = aRec; + u8 *pOut; + + memset(pNew, 0, nByte); + pNew->bIndirect = bIndirect || pExist->bIndirect; + pNew->op = op2; + pOut = pNew->aRecord = (u8*)&pNew[1]; + + for(i=0; inCol; i++){ + int n1 = sessionSerialLen(a1); + int n2 = sessionSerialLen(a2); + if( *a1==0xFF || (pTab->abPK[i]==0 && bIndirect) ){ + *pOut++ = 0xFF; + }else if( *a2==0 ){ + memcpy(pOut, a1, n1); + pOut += n1; + }else{ + memcpy(pOut, a2, n2); + pOut += n2; + } + a1 += n1; + a2 += n2; + } + pNew->nRecord = pOut - pNew->aRecord; + } + sqlite3_free(pExist); + } + }else{ + int op1 = pExist->op; + + /* + ** op1=INSERT, op2=INSERT -> Unsupported. Discard op2. + ** op1=INSERT, op2=UPDATE -> INSERT. + ** op1=INSERT, op2=DELETE -> (none) + ** + ** op1=UPDATE, op2=INSERT -> Unsupported. Discard op2. + ** op1=UPDATE, op2=UPDATE -> UPDATE. + ** op1=UPDATE, op2=DELETE -> DELETE. + ** + ** op1=DELETE, op2=INSERT -> UPDATE. + ** op1=DELETE, op2=UPDATE -> Unsupported. Discard op2. + ** op1=DELETE, op2=DELETE -> Unsupported. Discard op2. + */ + if( (op1==SQLITE_INSERT && op2==SQLITE_INSERT) + || (op1==SQLITE_UPDATE && op2==SQLITE_INSERT) + || (op1==SQLITE_DELETE && op2==SQLITE_UPDATE) + || (op1==SQLITE_DELETE && op2==SQLITE_DELETE) + ){ + pNew = pExist; + }else if( op1==SQLITE_INSERT && op2==SQLITE_DELETE ){ + sqlite3_free(pExist); + assert( pNew==0 ); + }else{ + u8 *aExist = pExist->aRecord; + sqlite3_int64 nByte; + u8 *aCsr; + + /* Allocate a new SessionChange object. Ensure that the aRecord[] + ** buffer of the new object is large enough to hold any record that + ** may be generated by combining the input records. */ + nByte = sizeof(SessionChange) + pExist->nRecord + nRec; + pNew = (SessionChange *)sqlite3_malloc64(nByte); + if( !pNew ){ + sqlite3_free(pExist); + return SQLITE_NOMEM; + } + memset(pNew, 0, sizeof(SessionChange)); + pNew->bIndirect = (bIndirect && pExist->bIndirect); + aCsr = pNew->aRecord = (u8 *)&pNew[1]; + + if( op1==SQLITE_INSERT ){ /* INSERT + UPDATE */ + u8 *a1 = aRec; + assert( op2==SQLITE_UPDATE ); + pNew->op = SQLITE_INSERT; + if( bPatchset==0 ) sessionSkipRecord(&a1, pTab->nCol); + sessionMergeRecord(&aCsr, pTab->nCol, aExist, a1); + }else if( op1==SQLITE_DELETE ){ /* DELETE + INSERT */ + assert( op2==SQLITE_INSERT ); + pNew->op = SQLITE_UPDATE; + if( bPatchset ){ + memcpy(aCsr, aRec, nRec); + aCsr += nRec; + }else{ + if( 0==sessionMergeUpdate(&aCsr, pTab, bPatchset, aExist, 0,aRec,0) ){ + sqlite3_free(pNew); + pNew = 0; + } + } + }else if( op2==SQLITE_UPDATE ){ /* UPDATE + UPDATE */ + u8 *a1 = aExist; + u8 *a2 = aRec; + assert( op1==SQLITE_UPDATE ); + if( bPatchset==0 ){ + sessionSkipRecord(&a1, pTab->nCol); + sessionSkipRecord(&a2, pTab->nCol); + } + pNew->op = SQLITE_UPDATE; + if( 0==sessionMergeUpdate(&aCsr, pTab, bPatchset, aRec, aExist,a1,a2) ){ + sqlite3_free(pNew); + pNew = 0; + } + }else{ /* UPDATE + DELETE */ + assert( op1==SQLITE_UPDATE && op2==SQLITE_DELETE ); + pNew->op = SQLITE_DELETE; + if( bPatchset ){ + memcpy(aCsr, aRec, nRec); + aCsr += nRec; + }else{ + sessionMergeRecord(&aCsr, pTab->nCol, aRec, aExist); + } + } + + if( pNew ){ + pNew->nRecord = (int)(aCsr - pNew->aRecord); + } + sqlite3_free(pExist); + } + } + + *ppNew = pNew; + return rc; +} + +/* +** Check if a changeset entry with nCol columns and the PK array passed +** as the final argument to this function is compatible with SessionTable +** pTab. If so, return 1. Otherwise, if they are incompatible in some way, +** return 0. +*/ +static int sessionChangesetCheckCompat( + SessionTable *pTab, + int nCol, + u8 *abPK +){ + if( pTab->azCol && nColnCol ){ + int ii; + for(ii=0; iinCol; ii++){ + u8 bPK = (ii < nCol) ? abPK[ii] : 0; + if( pTab->abPK[ii]!=bPK ) return 0; + } + return 1; + } + return (pTab->nCol==nCol && 0==memcmp(abPK, pTab->abPK, nCol)); +} + +static int sessionChangesetExtendRecord( + sqlite3_changegroup *pGrp, + SessionTable *pTab, + int nCol, + int op, + const u8 *aRec, + int nRec, + SessionBuffer *pOut +){ + int rc = SQLITE_OK; + int ii = 0; + + assert( pTab->azCol ); + assert( nColnCol ); + + pOut->nBuf = 0; + if( op==SQLITE_INSERT || (op==SQLITE_DELETE && pGrp->bPatch==0) ){ + /* Append the missing default column values to the record. */ + sessionAppendBlob(pOut, aRec, nRec, &rc); + if( rc==SQLITE_OK && pTab->pDfltStmt==0 ){ + rc = sessionPrepareDfltStmt(pGrp->db, pTab, &pTab->pDfltStmt); + if( rc==SQLITE_OK && SQLITE_ROW!=sqlite3_step(pTab->pDfltStmt) ){ + rc = sqlite3_errcode(pGrp->db); + } + } + for(ii=nCol; rc==SQLITE_OK && iinCol; ii++){ + int eType = sqlite3_column_type(pTab->pDfltStmt, ii); + sessionAppendByte(pOut, eType, &rc); + switch( eType ){ + case SQLITE_FLOAT: + case SQLITE_INTEGER: { + i64 iVal; + if( eType==SQLITE_INTEGER ){ + iVal = sqlite3_column_int64(pTab->pDfltStmt, ii); + }else{ + double rVal = sqlite3_column_int64(pTab->pDfltStmt, ii); + memcpy(&iVal, &rVal, sizeof(i64)); + } + if( SQLITE_OK==sessionBufferGrow(pOut, 8, &rc) ){ + sessionPutI64(&pOut->aBuf[pOut->nBuf], iVal); + pOut->nBuf += 8; + } + break; + } + + case SQLITE_BLOB: + case SQLITE_TEXT: { + int n = sqlite3_column_bytes(pTab->pDfltStmt, ii); + sessionAppendVarint(pOut, n, &rc); + if( eType==SQLITE_TEXT ){ + const u8 *z = (const u8*)sqlite3_column_text(pTab->pDfltStmt, ii); + sessionAppendBlob(pOut, z, n, &rc); + }else{ + const u8 *z = (const u8*)sqlite3_column_blob(pTab->pDfltStmt, ii); + sessionAppendBlob(pOut, z, n, &rc); + } + break; + } + + default: + assert( eType==SQLITE_NULL ); + break; + } + } + }else if( op==SQLITE_UPDATE ){ + /* Append missing "undefined" entries to the old.* record. And, if this + ** is an UPDATE, to the new.* record as well. */ + int iOff = 0; + if( pGrp->bPatch==0 ){ + for(ii=0; iinCol-nCol); ii++){ + sessionAppendByte(pOut, 0x00, &rc); + } + } + + sessionAppendBlob(pOut, &aRec[iOff], nRec-iOff, &rc); + for(ii=0; ii<(pTab->nCol-nCol); ii++){ + sessionAppendByte(pOut, 0x00, &rc); + } + }else{ + assert( op==SQLITE_DELETE && pGrp->bPatch ); + sessionAppendBlob(pOut, aRec, nRec, &rc); + } + + return rc; +} + +/* +** Locate or create a SessionTable object that may be used to add the +** change currently pointed to by iterator pIter to changegroup pGrp. +** If successful, set output variable (*ppTab) to point to the table +** object and return SQLITE_OK. Otherwise, if some error occurs, return +** an SQLite error code and leave (*ppTab) set to NULL. +*/ +static int sessionChangesetFindTable( + sqlite3_changegroup *pGrp, + const char *zTab, + sqlite3_changeset_iter *pIter, + SessionTable **ppTab +){ + int rc = SQLITE_OK; + SessionTable *pTab = 0; + int nTab = (int)strlen(zTab); + u8 *abPK = 0; + int nCol = 0; + + *ppTab = 0; + sqlite3changeset_pk(pIter, &abPK, &nCol); + + /* Search the list for an existing table */ + for(pTab = pGrp->pList; pTab; pTab=pTab->pNext){ + if( 0==sqlite3_strnicmp(pTab->zName, zTab, nTab+1) ) break; + } + + /* If one was not found above, create a new table now */ + if( !pTab ){ + SessionTable **ppNew; + + pTab = sqlite3_malloc64(sizeof(SessionTable) + nCol + nTab+1); + if( !pTab ){ + return SQLITE_NOMEM; + } + memset(pTab, 0, sizeof(SessionTable)); + pTab->nCol = nCol; + pTab->abPK = (u8*)&pTab[1]; + memcpy(pTab->abPK, abPK, nCol); + pTab->zName = (char*)&pTab->abPK[nCol]; + memcpy(pTab->zName, zTab, nTab+1); + + if( pGrp->db ){ + pTab->nCol = 0; + rc = sessionInitTable(0, pTab, pGrp->db, pGrp->zDb); + if( rc ){ + assert( pTab->azCol==0 ); + sqlite3_free(pTab); + return rc; + } + } + + /* The new object must be linked on to the end of the list, not + ** simply added to the start of it. This is to ensure that the + ** tables within the output of sqlite3changegroup_output() are in + ** the right order. */ + for(ppNew=&pGrp->pList; *ppNew; ppNew=&(*ppNew)->pNext); + *ppNew = pTab; + } + + /* Check that the table is compatible. */ + if( !sessionChangesetCheckCompat(pTab, nCol, abPK) ){ + rc = SQLITE_SCHEMA; + } + + *ppTab = pTab; + return rc; +} + +/* +** Add the change currently indicated by iterator pIter to the hash table +** belonging to changegroup pGrp. +*/ +static int sessionOneChangeToHash( + sqlite3_changegroup *pGrp, + sqlite3_changeset_iter *pIter, + int bRebase +){ + int rc = SQLITE_OK; + int nCol = 0; + int op = 0; + int iHash = 0; + int bIndirect = 0; + SessionChange *pChange = 0; + SessionChange *pExist = 0; + SessionChange **pp = 0; + SessionTable *pTab = 0; + u8 *aRec = &pIter->in.aData[pIter->in.iCurrent + 2]; + int nRec = (pIter->in.iNext - pIter->in.iCurrent) - 2; + + assert( nRec>0 ); + + /* Ensure that only changesets, or only patchsets, but not a mixture + ** of both, are being combined. It is an error to try to combine a + ** changeset and a patchset. */ + if( pGrp->pList==0 ){ + pGrp->bPatch = pIter->bPatchset; + }else if( pIter->bPatchset!=pGrp->bPatch ){ + rc = SQLITE_ERROR; + } + + if( rc==SQLITE_OK ){ + const char *zTab = 0; + sqlite3changeset_op(pIter, &zTab, &nCol, &op, &bIndirect); + rc = sessionChangesetFindTable(pGrp, zTab, pIter, &pTab); + } + + if( rc==SQLITE_OK && nColnCol ){ + SessionBuffer *pBuf = &pGrp->rec; + rc = sessionChangesetExtendRecord(pGrp, pTab, nCol, op, aRec, nRec, pBuf); + aRec = pBuf->aBuf; + nRec = pBuf->nBuf; + assert( pGrp->db ); + } + + if( rc==SQLITE_OK && sessionGrowHash(0, pIter->bPatchset, pTab) ){ + rc = SQLITE_NOMEM; + } + + if( rc==SQLITE_OK ){ + /* Search for existing entry. If found, remove it from the hash table. + ** Code below may link it back in. */ + iHash = sessionChangeHash( + pTab, (pIter->bPatchset && op==SQLITE_DELETE), aRec, pTab->nChange + ); + for(pp=&pTab->apChange[iHash]; *pp; pp=&(*pp)->pNext){ + int bPkOnly1 = 0; + int bPkOnly2 = 0; + if( pIter->bPatchset ){ + bPkOnly1 = (*pp)->op==SQLITE_DELETE; + bPkOnly2 = op==SQLITE_DELETE; + } + if( sessionChangeEqual(pTab, bPkOnly1, (*pp)->aRecord, bPkOnly2, aRec) ){ + pExist = *pp; + *pp = (*pp)->pNext; + pTab->nEntry--; + break; + } + } + } + + if( rc==SQLITE_OK ){ + rc = sessionChangeMerge(pTab, bRebase, + pIter->bPatchset, pExist, op, bIndirect, aRec, nRec, &pChange + ); + } + if( rc==SQLITE_OK && pChange ){ + pChange->pNext = pTab->apChange[iHash]; + pTab->apChange[iHash] = pChange; + pTab->nEntry++; + } + + if( rc==SQLITE_OK ) rc = pIter->rc; + return rc; +} + +/* +** Add all changes in the changeset traversed by the iterator passed as +** the first argument to the changegroup hash tables. +*/ +static int sessionChangesetToHash( + sqlite3_changeset_iter *pIter, /* Iterator to read from */ + sqlite3_changegroup *pGrp, /* Changegroup object to add changeset to */ + int bRebase /* True if hash table is for rebasing */ +){ + u8 *aRec; + int nRec; + int rc = SQLITE_OK; + + pIter->in.bNoDiscard = 1; + while( SQLITE_ROW==(sessionChangesetNext(pIter, &aRec, &nRec, 0)) ){ + rc = sessionOneChangeToHash(pGrp, pIter, bRebase); + if( rc!=SQLITE_OK ) break; + } + + if( rc==SQLITE_OK ) rc = pIter->rc; + return rc; +} + +/* +** Serialize a changeset (or patchset) based on all changesets (or patchsets) +** added to the changegroup object passed as the first argument. +** +** If xOutput is not NULL, then the changeset/patchset is returned to the +** user via one or more calls to xOutput, as with the other streaming +** interfaces. +** +** Or, if xOutput is NULL, then (*ppOut) is populated with a pointer to a +** buffer containing the output changeset before this function returns. In +** this case (*pnOut) is set to the size of the output buffer in bytes. It +** is the responsibility of the caller to free the output buffer using +** sqlite3_free() when it is no longer required. +** +** If successful, SQLITE_OK is returned. Or, if an error occurs, an SQLite +** error code. If an error occurs and xOutput is NULL, (*ppOut) and (*pnOut) +** are both set to 0 before returning. +*/ +static int sessionChangegroupOutput( + sqlite3_changegroup *pGrp, + int (*xOutput)(void *pOut, const void *pData, int nData), + void *pOut, + int *pnOut, + void **ppOut +){ + int rc = SQLITE_OK; + SessionBuffer buf = {0, 0, 0}; + SessionTable *pTab; + assert( xOutput==0 || (ppOut==0 && pnOut==0) ); + + /* Create the serialized output changeset based on the contents of the + ** hash tables attached to the SessionTable objects in list p->pList. + */ + for(pTab=pGrp->pList; rc==SQLITE_OK && pTab; pTab=pTab->pNext){ + int i; + if( pTab->nEntry==0 ) continue; + + sessionAppendTableHdr(&buf, pGrp->bPatch, pTab, &rc); + for(i=0; inChange; i++){ + SessionChange *p; + for(p=pTab->apChange[i]; p; p=p->pNext){ + sessionAppendByte(&buf, p->op, &rc); + sessionAppendByte(&buf, p->bIndirect, &rc); + sessionAppendBlob(&buf, p->aRecord, p->nRecord, &rc); + if( rc==SQLITE_OK && xOutput && buf.nBuf>=sessions_strm_chunk_size ){ + rc = xOutput(pOut, buf.aBuf, buf.nBuf); + buf.nBuf = 0; + } + } + } + } + + if( rc==SQLITE_OK ){ + if( xOutput ){ + if( buf.nBuf>0 ) rc = xOutput(pOut, buf.aBuf, buf.nBuf); + }else if( ppOut ){ + *ppOut = buf.aBuf; + if( pnOut ) *pnOut = buf.nBuf; + buf.aBuf = 0; + } + } + sqlite3_free(buf.aBuf); + + return rc; +} + +/* +** Allocate a new, empty, sqlite3_changegroup. +*/ +int sqlite3changegroup_new(sqlite3_changegroup **pp){ + int rc = SQLITE_OK; /* Return code */ + sqlite3_changegroup *p; /* New object */ + p = (sqlite3_changegroup*)sqlite3_malloc(sizeof(sqlite3_changegroup)); + if( p==0 ){ + rc = SQLITE_NOMEM; + }else{ + memset(p, 0, sizeof(sqlite3_changegroup)); + } + *pp = p; + return rc; +} + +/* +** Provide a database schema to the changegroup object. +*/ +int sqlite3changegroup_schema( + sqlite3_changegroup *pGrp, + sqlite3 *db, + const char *zDb +){ + int rc = SQLITE_OK; + + if( pGrp->pList || pGrp->db ){ + /* Cannot add a schema after one or more calls to sqlite3changegroup_add(), + ** or after sqlite3changegroup_schema() has already been called. */ + rc = SQLITE_MISUSE; + }else{ + pGrp->zDb = sqlite3_mprintf("%s", zDb); + if( pGrp->zDb==0 ){ + rc = SQLITE_NOMEM; + }else{ + pGrp->db = db; + } + } + return rc; +} + +/* +** Add the changeset currently stored in buffer pData, size nData bytes, +** to changeset-group p. +*/ +int sqlite3changegroup_add(sqlite3_changegroup *pGrp, int nData, void *pData){ + sqlite3_changeset_iter *pIter; /* Iterator opened on pData/nData */ + int rc; /* Return code */ + + rc = sqlite3changeset_start(&pIter, nData, pData); + if( rc==SQLITE_OK ){ + rc = sessionChangesetToHash(pIter, pGrp, 0); + } + sqlite3changeset_finalize(pIter); + return rc; +} + +/* +** Add a single change to a changeset-group. +*/ +int sqlite3changegroup_add_change( + sqlite3_changegroup *pGrp, + sqlite3_changeset_iter *pIter +){ + int rc = SQLITE_OK; + + if( pIter->in.iCurrent==pIter->in.iNext + || pIter->rc!=SQLITE_OK + || pIter->bInvert + ){ + /* Iterator does not point to any valid entry or is an INVERT iterator. */ + rc = SQLITE_ERROR; + }else{ + pIter->in.bNoDiscard = 1; + rc = sessionOneChangeToHash(pGrp, pIter, 0); + } + return rc; +} + +/* +** Obtain a buffer containing a changeset representing the concatenation +** of all changesets added to the group so far. +*/ +int sqlite3changegroup_output( + sqlite3_changegroup *pGrp, + int *pnData, + void **ppData +){ + return sessionChangegroupOutput(pGrp, 0, 0, pnData, ppData); +} + +/* +** Streaming versions of changegroup_add(). +*/ +int sqlite3changegroup_add_strm( + sqlite3_changegroup *pGrp, + int (*xInput)(void *pIn, void *pData, int *pnData), + void *pIn +){ + sqlite3_changeset_iter *pIter; /* Iterator opened on pData/nData */ + int rc; /* Return code */ + + rc = sqlite3changeset_start_strm(&pIter, xInput, pIn); + if( rc==SQLITE_OK ){ + rc = sessionChangesetToHash(pIter, pGrp, 0); + } + sqlite3changeset_finalize(pIter); + return rc; +} + +/* +** Streaming versions of changegroup_output(). +*/ +int sqlite3changegroup_output_strm( + sqlite3_changegroup *pGrp, + int (*xOutput)(void *pOut, const void *pData, int nData), + void *pOut +){ + return sessionChangegroupOutput(pGrp, xOutput, pOut, 0, 0); +} + +/* +** Delete a changegroup object. +*/ +void sqlite3changegroup_delete(sqlite3_changegroup *pGrp){ + if( pGrp ){ + sqlite3_free(pGrp->zDb); + sessionDeleteTable(0, pGrp->pList); + sqlite3_free(pGrp->rec.aBuf); + sqlite3_free(pGrp); + } +} + +/* +** Combine two changesets together. +*/ +int sqlite3changeset_concat( + int nLeft, /* Number of bytes in lhs input */ + void *pLeft, /* Lhs input changeset */ + int nRight /* Number of bytes in rhs input */, + void *pRight, /* Rhs input changeset */ + int *pnOut, /* OUT: Number of bytes in output changeset */ + void **ppOut /* OUT: changeset (left right) */ +){ + sqlite3_changegroup *pGrp; + int rc; + + rc = sqlite3changegroup_new(&pGrp); + if( rc==SQLITE_OK ){ + rc = sqlite3changegroup_add(pGrp, nLeft, pLeft); + } + if( rc==SQLITE_OK ){ + rc = sqlite3changegroup_add(pGrp, nRight, pRight); + } + if( rc==SQLITE_OK ){ + rc = sqlite3changegroup_output(pGrp, pnOut, ppOut); + } + sqlite3changegroup_delete(pGrp); + + return rc; +} + +/* +** Streaming version of sqlite3changeset_concat(). +*/ +int sqlite3changeset_concat_strm( + int (*xInputA)(void *pIn, void *pData, int *pnData), + void *pInA, + int (*xInputB)(void *pIn, void *pData, int *pnData), + void *pInB, + int (*xOutput)(void *pOut, const void *pData, int nData), + void *pOut +){ + sqlite3_changegroup *pGrp; + int rc; + + rc = sqlite3changegroup_new(&pGrp); + if( rc==SQLITE_OK ){ + rc = sqlite3changegroup_add_strm(pGrp, xInputA, pInA); + } + if( rc==SQLITE_OK ){ + rc = sqlite3changegroup_add_strm(pGrp, xInputB, pInB); + } + if( rc==SQLITE_OK ){ + rc = sqlite3changegroup_output_strm(pGrp, xOutput, pOut); + } + sqlite3changegroup_delete(pGrp); + + return rc; +} + +/* +** Changeset rebaser handle. +*/ +struct sqlite3_rebaser { + sqlite3_changegroup grp; /* Hash table */ +}; + +/* +** Buffers a1 and a2 must both contain a sessions module record nCol +** fields in size. This function appends an nCol sessions module +** record to buffer pBuf that is a copy of a1, except that for +** each field that is undefined in a1[], swap in the field from a2[]. +*/ +static void sessionAppendRecordMerge( + SessionBuffer *pBuf, /* Buffer to append to */ + int nCol, /* Number of columns in each record */ + u8 *a1, int n1, /* Record 1 */ + u8 *a2, int n2, /* Record 2 */ + int *pRc /* IN/OUT: error code */ +){ + sessionBufferGrow(pBuf, n1+n2, pRc); + if( *pRc==SQLITE_OK ){ + int i; + u8 *pOut = &pBuf->aBuf[pBuf->nBuf]; + for(i=0; inBuf = pOut-pBuf->aBuf; + assert( pBuf->nBuf<=pBuf->nAlloc ); + } +} + +/* +** This function is called when rebasing a local UPDATE change against one +** or more remote UPDATE changes. The aRec/nRec buffer contains the current +** old.* and new.* records for the change. The rebase buffer (a single +** record) is in aChange/nChange. The rebased change is appended to buffer +** pBuf. +** +** Rebasing the UPDATE involves: +** +** * Removing any changes to fields for which the corresponding field +** in the rebase buffer is set to "replaced" (type 0xFF). If this +** means the UPDATE change updates no fields, nothing is appended +** to the output buffer. +** +** * For each field modified by the local change for which the +** corresponding field in the rebase buffer is not "undefined" (0x00) +** or "replaced" (0xFF), the old.* value is replaced by the value +** in the rebase buffer. +*/ +static void sessionAppendPartialUpdate( + SessionBuffer *pBuf, /* Append record here */ + sqlite3_changeset_iter *pIter, /* Iterator pointed at local change */ + u8 *aRec, int nRec, /* Local change */ + u8 *aChange, int nChange, /* Record to rebase against */ + int *pRc /* IN/OUT: Return Code */ +){ + sessionBufferGrow(pBuf, 2+nRec+nChange, pRc); + if( *pRc==SQLITE_OK ){ + int bData = 0; + u8 *pOut = &pBuf->aBuf[pBuf->nBuf]; + int i; + u8 *a1 = aRec; + u8 *a2 = aChange; + + *pOut++ = SQLITE_UPDATE; + *pOut++ = pIter->bIndirect; + for(i=0; inCol; i++){ + int n1 = sessionSerialLen(a1); + int n2 = sessionSerialLen(a2); + if( pIter->abPK[i] || a2[0]==0 ){ + if( !pIter->abPK[i] && a1[0] ) bData = 1; + memcpy(pOut, a1, n1); + pOut += n1; + }else if( a2[0]!=0xFF && a1[0] ){ + bData = 1; + memcpy(pOut, a2, n2); + pOut += n2; + }else{ + *pOut++ = '\0'; + } + a1 += n1; + a2 += n2; + } + if( bData ){ + a2 = aChange; + for(i=0; inCol; i++){ + int n1 = sessionSerialLen(a1); + int n2 = sessionSerialLen(a2); + if( pIter->abPK[i] || a2[0]!=0xFF ){ + memcpy(pOut, a1, n1); + pOut += n1; + }else{ + *pOut++ = '\0'; + } + a1 += n1; + a2 += n2; + } + pBuf->nBuf = (pOut - pBuf->aBuf); + } + } +} + +/* +** pIter is configured to iterate through a changeset. This function rebases +** that changeset according to the current configuration of the rebaser +** object passed as the first argument. If no error occurs and argument xOutput +** is not NULL, then the changeset is returned to the caller by invoking +** xOutput zero or more times and SQLITE_OK returned. Or, if xOutput is NULL, +** then (*ppOut) is set to point to a buffer containing the rebased changeset +** before this function returns. In this case (*pnOut) is set to the size of +** the buffer in bytes. It is the responsibility of the caller to eventually +** free the (*ppOut) buffer using sqlite3_free(). +** +** If an error occurs, an SQLite error code is returned. If ppOut and +** pnOut are not NULL, then the two output parameters are set to 0 before +** returning. +*/ +static int sessionRebase( + sqlite3_rebaser *p, /* Rebaser hash table */ + sqlite3_changeset_iter *pIter, /* Input data */ + int (*xOutput)(void *pOut, const void *pData, int nData), + void *pOut, /* Context for xOutput callback */ + int *pnOut, /* OUT: Number of bytes in output changeset */ + void **ppOut /* OUT: Inverse of pChangeset */ +){ + int rc = SQLITE_OK; + u8 *aRec = 0; + int nRec = 0; + int bNew = 0; + SessionTable *pTab = 0; + SessionBuffer sOut = {0,0,0}; + + while( SQLITE_ROW==sessionChangesetNext(pIter, &aRec, &nRec, &bNew) ){ + SessionChange *pChange = 0; + int bDone = 0; + + if( bNew ){ + const char *zTab = pIter->zTab; + for(pTab=p->grp.pList; pTab; pTab=pTab->pNext){ + if( 0==sqlite3_stricmp(pTab->zName, zTab) ) break; + } + bNew = 0; + + /* A patchset may not be rebased */ + if( pIter->bPatchset ){ + rc = SQLITE_ERROR; + } + + /* Append a table header to the output for this new table */ + sessionAppendByte(&sOut, pIter->bPatchset ? 'P' : 'T', &rc); + sessionAppendVarint(&sOut, pIter->nCol, &rc); + sessionAppendBlob(&sOut, pIter->abPK, pIter->nCol, &rc); + sessionAppendBlob(&sOut,(u8*)pIter->zTab,(int)strlen(pIter->zTab)+1,&rc); + } + + if( pTab && rc==SQLITE_OK ){ + int iHash = sessionChangeHash(pTab, 0, aRec, pTab->nChange); + + for(pChange=pTab->apChange[iHash]; pChange; pChange=pChange->pNext){ + if( sessionChangeEqual(pTab, 0, aRec, 0, pChange->aRecord) ){ + break; + } + } + } + + if( pChange ){ + assert( pChange->op==SQLITE_DELETE || pChange->op==SQLITE_INSERT ); + switch( pIter->op ){ + case SQLITE_INSERT: + if( pChange->op==SQLITE_INSERT ){ + bDone = 1; + if( pChange->bIndirect==0 ){ + sessionAppendByte(&sOut, SQLITE_UPDATE, &rc); + sessionAppendByte(&sOut, pIter->bIndirect, &rc); + sessionAppendBlob(&sOut, pChange->aRecord, pChange->nRecord, &rc); + sessionAppendBlob(&sOut, aRec, nRec, &rc); + } + } + break; + + case SQLITE_UPDATE: + bDone = 1; + if( pChange->op==SQLITE_DELETE ){ + if( pChange->bIndirect==0 ){ + u8 *pCsr = aRec; + sessionSkipRecord(&pCsr, pIter->nCol); + sessionAppendByte(&sOut, SQLITE_INSERT, &rc); + sessionAppendByte(&sOut, pIter->bIndirect, &rc); + sessionAppendRecordMerge(&sOut, pIter->nCol, + pCsr, nRec-(pCsr-aRec), + pChange->aRecord, pChange->nRecord, &rc + ); + } + }else{ + sessionAppendPartialUpdate(&sOut, pIter, + aRec, nRec, pChange->aRecord, pChange->nRecord, &rc + ); + } + break; + + default: + assert( pIter->op==SQLITE_DELETE ); + bDone = 1; + if( pChange->op==SQLITE_INSERT ){ + sessionAppendByte(&sOut, SQLITE_DELETE, &rc); + sessionAppendByte(&sOut, pIter->bIndirect, &rc); + sessionAppendRecordMerge(&sOut, pIter->nCol, + pChange->aRecord, pChange->nRecord, aRec, nRec, &rc + ); + } + break; + } + } + + if( bDone==0 ){ + sessionAppendByte(&sOut, pIter->op, &rc); + sessionAppendByte(&sOut, pIter->bIndirect, &rc); + sessionAppendBlob(&sOut, aRec, nRec, &rc); + } + if( rc==SQLITE_OK && xOutput && sOut.nBuf>sessions_strm_chunk_size ){ + rc = xOutput(pOut, sOut.aBuf, sOut.nBuf); + sOut.nBuf = 0; + } + if( rc ) break; + } + + if( rc!=SQLITE_OK ){ + sqlite3_free(sOut.aBuf); + memset(&sOut, 0, sizeof(sOut)); + } + + if( rc==SQLITE_OK ){ + if( xOutput ){ + if( sOut.nBuf>0 ){ + rc = xOutput(pOut, sOut.aBuf, sOut.nBuf); + } + }else if( ppOut ){ + *ppOut = (void*)sOut.aBuf; + *pnOut = sOut.nBuf; + sOut.aBuf = 0; + } + } + sqlite3_free(sOut.aBuf); + return rc; +} + +/* +** Create a new rebaser object. +*/ +int sqlite3rebaser_create(sqlite3_rebaser **ppNew){ + int rc = SQLITE_OK; + sqlite3_rebaser *pNew; + + pNew = sqlite3_malloc(sizeof(sqlite3_rebaser)); + if( pNew==0 ){ + rc = SQLITE_NOMEM; + }else{ + memset(pNew, 0, sizeof(sqlite3_rebaser)); + } + *ppNew = pNew; + return rc; +} + +/* +** Call this one or more times to configure a rebaser. +*/ +int sqlite3rebaser_configure( + sqlite3_rebaser *p, + int nRebase, const void *pRebase +){ + sqlite3_changeset_iter *pIter = 0; /* Iterator opened on pData/nData */ + int rc; /* Return code */ + rc = sqlite3changeset_start(&pIter, nRebase, (void*)pRebase); + if( rc==SQLITE_OK ){ + rc = sessionChangesetToHash(pIter, &p->grp, 1); + } + sqlite3changeset_finalize(pIter); + return rc; +} + +/* +** Rebase a changeset according to current rebaser configuration +*/ +int sqlite3rebaser_rebase( + sqlite3_rebaser *p, + int nIn, const void *pIn, + int *pnOut, void **ppOut +){ + sqlite3_changeset_iter *pIter = 0; /* Iterator to skip through input */ + int rc = sqlite3changeset_start(&pIter, nIn, (void*)pIn); + + if( rc==SQLITE_OK ){ + rc = sessionRebase(p, pIter, 0, 0, pnOut, ppOut); + sqlite3changeset_finalize(pIter); + } + + return rc; +} + +/* +** Rebase a changeset according to current rebaser configuration +*/ +int sqlite3rebaser_rebase_strm( + sqlite3_rebaser *p, + int (*xInput)(void *pIn, void *pData, int *pnData), + void *pIn, + int (*xOutput)(void *pOut, const void *pData, int nData), + void *pOut +){ + sqlite3_changeset_iter *pIter = 0; /* Iterator to skip through input */ + int rc = sqlite3changeset_start_strm(&pIter, xInput, pIn); + + if( rc==SQLITE_OK ){ + rc = sessionRebase(p, pIter, xOutput, pOut, 0, 0); + sqlite3changeset_finalize(pIter); + } + + return rc; +} + +/* +** Destroy a rebaser object +*/ +void sqlite3rebaser_delete(sqlite3_rebaser *p){ + if( p ){ + sessionDeleteTable(0, p->grp.pList); + sqlite3_free(p->grp.rec.aBuf); + sqlite3_free(p); + } +} + +/* +** Global configuration +*/ +int sqlite3session_config(int op, void *pArg){ + int rc = SQLITE_OK; + switch( op ){ + case SQLITE_SESSION_CONFIG_STRMSIZE: { + int *pInt = (int*)pArg; + if( *pInt>0 ){ + sessions_strm_chunk_size = *pInt; + } + *pInt = sessions_strm_chunk_size; + break; + } + default: + rc = SQLITE_MISUSE; + break; + } + return rc; +} + +#endif /* SQLITE_ENABLE_SESSION && SQLITE_ENABLE_PREUPDATE_HOOK */ diff --git a/ext/session/sqlite3session.h b/ext/session/sqlite3session.h new file mode 100644 index 0000000000..28b90eb6b5 --- /dev/null +++ b/ext/session/sqlite3session.h @@ -0,0 +1,1863 @@ + +#if !defined(__SQLITESESSION_H_) && defined(SQLITE_ENABLE_SESSION) +#define __SQLITESESSION_H_ 1 + +/* +** Make sure we can call this stuff from C++. +*/ +#ifdef __cplusplus +extern "C" { +#endif + +#include "sqlite3.h" + +/* +** CAPI3REF: Session Object Handle +** +** An instance of this object is a [session] that can be used to +** record changes to a database. +*/ +typedef struct sqlite3_session sqlite3_session; + +/* +** CAPI3REF: Changeset Iterator Handle +** +** An instance of this object acts as a cursor for iterating +** over the elements of a [changeset] or [patchset]. +*/ +typedef struct sqlite3_changeset_iter sqlite3_changeset_iter; + +/* +** CAPI3REF: Create A New Session Object +** CONSTRUCTOR: sqlite3_session +** +** Create a new session object attached to database handle db. If successful, +** a pointer to the new object is written to *ppSession and SQLITE_OK is +** returned. If an error occurs, *ppSession is set to NULL and an SQLite +** error code (e.g. SQLITE_NOMEM) is returned. +** +** It is possible to create multiple session objects attached to a single +** database handle. +** +** Session objects created using this function should be deleted using the +** [sqlite3session_delete()] function before the database handle that they +** are attached to is itself closed. If the database handle is closed before +** the session object is deleted, then the results of calling any session +** module function, including [sqlite3session_delete()] on the session object +** are undefined. +** +** Because the session module uses the [sqlite3_preupdate_hook()] API, it +** is not possible for an application to register a pre-update hook on a +** database handle that has one or more session objects attached. Nor is +** it possible to create a session object attached to a database handle for +** which a pre-update hook is already defined. The results of attempting +** either of these things are undefined. +** +** The session object will be used to create changesets for tables in +** database zDb, where zDb is either "main", or "temp", or the name of an +** attached database. It is not an error if database zDb is not attached +** to the database when the session object is created. +*/ +int sqlite3session_create( + sqlite3 *db, /* Database handle */ + const char *zDb, /* Name of db (e.g. "main") */ + sqlite3_session **ppSession /* OUT: New session object */ +); + +/* +** CAPI3REF: Delete A Session Object +** DESTRUCTOR: sqlite3_session +** +** Delete a session object previously allocated using +** [sqlite3session_create()]. Once a session object has been deleted, the +** results of attempting to use pSession with any other session module +** function are undefined. +** +** Session objects must be deleted before the database handle to which they +** are attached is closed. Refer to the documentation for +** [sqlite3session_create()] for details. +*/ +void sqlite3session_delete(sqlite3_session *pSession); + +/* +** CAPI3REF: Configure a Session Object +** METHOD: sqlite3_session +** +** This method is used to configure a session object after it has been +** created. At present the only valid values for the second parameter are +** [SQLITE_SESSION_OBJCONFIG_SIZE] and [SQLITE_SESSION_OBJCONFIG_ROWID]. +** +*/ +int sqlite3session_object_config(sqlite3_session*, int op, void *pArg); + +/* +** CAPI3REF: Options for sqlite3session_object_config +** +** The following values may passed as the the 2nd parameter to +** sqlite3session_object_config(). +** +**
    SQLITE_SESSION_OBJCONFIG_SIZE
    +** This option is used to set, clear or query the flag that enables +** the [sqlite3session_changeset_size()] API. Because it imposes some +** computational overhead, this API is disabled by default. Argument +** pArg must point to a value of type (int). If the value is initially +** 0, then the sqlite3session_changeset_size() API is disabled. If it +** is greater than 0, then the same API is enabled. Or, if the initial +** value is less than zero, no change is made. In all cases the (int) +** variable is set to 1 if the sqlite3session_changeset_size() API is +** enabled following the current call, or 0 otherwise. +** +** It is an error (SQLITE_MISUSE) to attempt to modify this setting after +** the first table has been attached to the session object. +** +**
    SQLITE_SESSION_OBJCONFIG_ROWID
    +** This option is used to set, clear or query the flag that enables +** collection of data for tables with no explicit PRIMARY KEY. +** +** Normally, tables with no explicit PRIMARY KEY are simply ignored +** by the sessions module. However, if this flag is set, it behaves +** as if such tables have a column "_rowid_ INTEGER PRIMARY KEY" inserted +** as their leftmost columns. +** +** It is an error (SQLITE_MISUSE) to attempt to modify this setting after +** the first table has been attached to the session object. +*/ +#define SQLITE_SESSION_OBJCONFIG_SIZE 1 +#define SQLITE_SESSION_OBJCONFIG_ROWID 2 + +/* +** CAPI3REF: Enable Or Disable A Session Object +** METHOD: sqlite3_session +** +** Enable or disable the recording of changes by a session object. When +** enabled, a session object records changes made to the database. When +** disabled - it does not. A newly created session object is enabled. +** Refer to the documentation for [sqlite3session_changeset()] for further +** details regarding how enabling and disabling a session object affects +** the eventual changesets. +** +** Passing zero to this function disables the session. Passing a value +** greater than zero enables it. Passing a value less than zero is a +** no-op, and may be used to query the current state of the session. +** +** The return value indicates the final state of the session object: 0 if +** the session is disabled, or 1 if it is enabled. +*/ +int sqlite3session_enable(sqlite3_session *pSession, int bEnable); + +/* +** CAPI3REF: Set Or Clear the Indirect Change Flag +** METHOD: sqlite3_session +** +** Each change recorded by a session object is marked as either direct or +** indirect. A change is marked as indirect if either: +** +**
      +**
    • The session object "indirect" flag is set when the change is +** made, or +**
    • The change is made by an SQL trigger or foreign key action +** instead of directly as a result of a users SQL statement. +**
    +** +** If a single row is affected by more than one operation within a session, +** then the change is considered indirect if all operations meet the criteria +** for an indirect change above, or direct otherwise. +** +** This function is used to set, clear or query the session object indirect +** flag. If the second argument passed to this function is zero, then the +** indirect flag is cleared. If it is greater than zero, the indirect flag +** is set. Passing a value less than zero does not modify the current value +** of the indirect flag, and may be used to query the current state of the +** indirect flag for the specified session object. +** +** The return value indicates the final state of the indirect flag: 0 if +** it is clear, or 1 if it is set. +*/ +int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect); + +/* +** CAPI3REF: Attach A Table To A Session Object +** METHOD: sqlite3_session +** +** If argument zTab is not NULL, then it is the name of a table to attach +** to the session object passed as the first argument. All subsequent changes +** made to the table while the session object is enabled will be recorded. See +** documentation for [sqlite3session_changeset()] for further details. +** +** Or, if argument zTab is NULL, then changes are recorded for all tables +** in the database. If additional tables are added to the database (by +** executing "CREATE TABLE" statements) after this call is made, changes for +** the new tables are also recorded. +** +** Changes can only be recorded for tables that have a PRIMARY KEY explicitly +** defined as part of their CREATE TABLE statement. It does not matter if the +** PRIMARY KEY is an "INTEGER PRIMARY KEY" (rowid alias) or not. The PRIMARY +** KEY may consist of a single column, or may be a composite key. +** +** It is not an error if the named table does not exist in the database. Nor +** is it an error if the named table does not have a PRIMARY KEY. However, +** no changes will be recorded in either of these scenarios. +** +** Changes are not recorded for individual rows that have NULL values stored +** in one or more of their PRIMARY KEY columns. +** +** SQLITE_OK is returned if the call completes without error. Or, if an error +** occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned. +** +**

    Special sqlite_stat1 Handling

    +** +** As of SQLite version 3.22.0, the "sqlite_stat1" table is an exception to +** some of the rules above. In SQLite, the schema of sqlite_stat1 is: +**
    +**        CREATE TABLE sqlite_stat1(tbl,idx,stat)  
    +**  
    +** +** Even though sqlite_stat1 does not have a PRIMARY KEY, changes are +** recorded for it as if the PRIMARY KEY is (tbl,idx). Additionally, changes +** are recorded for rows for which (idx IS NULL) is true. However, for such +** rows a zero-length blob (SQL value X'') is stored in the changeset or +** patchset instead of a NULL value. This allows such changesets to be +** manipulated by legacy implementations of sqlite3changeset_invert(), +** concat() and similar. +** +** The sqlite3changeset_apply() function automatically converts the +** zero-length blob back to a NULL value when updating the sqlite_stat1 +** table. However, if the application calls sqlite3changeset_new(), +** sqlite3changeset_old() or sqlite3changeset_conflict on a changeset +** iterator directly (including on a changeset iterator passed to a +** conflict-handler callback) then the X'' value is returned. The application +** must translate X'' to NULL itself if required. +** +** Legacy (older than 3.22.0) versions of the sessions module cannot capture +** changes made to the sqlite_stat1 table. Legacy versions of the +** sqlite3changeset_apply() function silently ignore any modifications to the +** sqlite_stat1 table that are part of a changeset or patchset. +*/ +int sqlite3session_attach( + sqlite3_session *pSession, /* Session object */ + const char *zTab /* Table name */ +); + +/* +** CAPI3REF: Set a table filter on a Session Object. +** METHOD: sqlite3_session +** +** The second argument (xFilter) is the "filter callback". For changes to rows +** in tables that are not attached to the Session object, the filter is called +** to determine whether changes to the table's rows should be tracked or not. +** If xFilter returns 0, changes are not tracked. Note that once a table is +** attached, xFilter will not be called again. +*/ +void sqlite3session_table_filter( + sqlite3_session *pSession, /* Session object */ + int(*xFilter)( + void *pCtx, /* Copy of third arg to _filter_table() */ + const char *zTab /* Table name */ + ), + void *pCtx /* First argument passed to xFilter */ +); + +/* +** CAPI3REF: Generate A Changeset From A Session Object +** METHOD: sqlite3_session +** +** Obtain a changeset containing changes to the tables attached to the +** session object passed as the first argument. If successful, +** set *ppChangeset to point to a buffer containing the changeset +** and *pnChangeset to the size of the changeset in bytes before returning +** SQLITE_OK. If an error occurs, set both *ppChangeset and *pnChangeset to +** zero and return an SQLite error code. +** +** A changeset consists of zero or more INSERT, UPDATE and/or DELETE changes, +** each representing a change to a single row of an attached table. An INSERT +** change contains the values of each field of a new database row. A DELETE +** contains the original values of each field of a deleted database row. An +** UPDATE change contains the original values of each field of an updated +** database row along with the updated values for each updated non-primary-key +** column. It is not possible for an UPDATE change to represent a change that +** modifies the values of primary key columns. If such a change is made, it +** is represented in a changeset as a DELETE followed by an INSERT. +** +** Changes are not recorded for rows that have NULL values stored in one or +** more of their PRIMARY KEY columns. If such a row is inserted or deleted, +** no corresponding change is present in the changesets returned by this +** function. If an existing row with one or more NULL values stored in +** PRIMARY KEY columns is updated so that all PRIMARY KEY columns are non-NULL, +** only an INSERT is appears in the changeset. Similarly, if an existing row +** with non-NULL PRIMARY KEY values is updated so that one or more of its +** PRIMARY KEY columns are set to NULL, the resulting changeset contains a +** DELETE change only. +** +** The contents of a changeset may be traversed using an iterator created +** using the [sqlite3changeset_start()] API. A changeset may be applied to +** a database with a compatible schema using the [sqlite3changeset_apply()] +** API. +** +** Within a changeset generated by this function, all changes related to a +** single table are grouped together. In other words, when iterating through +** a changeset or when applying a changeset to a database, all changes related +** to a single table are processed before moving on to the next table. Tables +** are sorted in the same order in which they were attached (or auto-attached) +** to the sqlite3_session object. The order in which the changes related to +** a single table are stored is undefined. +** +** Following a successful call to this function, it is the responsibility of +** the caller to eventually free the buffer that *ppChangeset points to using +** [sqlite3_free()]. +** +**

    Changeset Generation

    +** +** Once a table has been attached to a session object, the session object +** records the primary key values of all new rows inserted into the table. +** It also records the original primary key and other column values of any +** deleted or updated rows. For each unique primary key value, data is only +** recorded once - the first time a row with said primary key is inserted, +** updated or deleted in the lifetime of the session. +** +** There is one exception to the previous paragraph: when a row is inserted, +** updated or deleted, if one or more of its primary key columns contain a +** NULL value, no record of the change is made. +** +** The session object therefore accumulates two types of records - those +** that consist of primary key values only (created when the user inserts +** a new record) and those that consist of the primary key values and the +** original values of other table columns (created when the users deletes +** or updates a record). +** +** When this function is called, the requested changeset is created using +** both the accumulated records and the current contents of the database +** file. Specifically: +** +**
      +**
    • For each record generated by an insert, the database is queried +** for a row with a matching primary key. If one is found, an INSERT +** change is added to the changeset. If no such row is found, no change +** is added to the changeset. +** +**
    • For each record generated by an update or delete, the database is +** queried for a row with a matching primary key. If such a row is +** found and one or more of the non-primary key fields have been +** modified from their original values, an UPDATE change is added to +** the changeset. Or, if no such row is found in the table, a DELETE +** change is added to the changeset. If there is a row with a matching +** primary key in the database, but all fields contain their original +** values, no change is added to the changeset. +**
    +** +** This means, amongst other things, that if a row is inserted and then later +** deleted while a session object is active, neither the insert nor the delete +** will be present in the changeset. Or if a row is deleted and then later a +** row with the same primary key values inserted while a session object is +** active, the resulting changeset will contain an UPDATE change instead of +** a DELETE and an INSERT. +** +** When a session object is disabled (see the [sqlite3session_enable()] API), +** it does not accumulate records when rows are inserted, updated or deleted. +** This may appear to have some counter-intuitive effects if a single row +** is written to more than once during a session. For example, if a row +** is inserted while a session object is enabled, then later deleted while +** the same session object is disabled, no INSERT record will appear in the +** changeset, even though the delete took place while the session was disabled. +** Or, if one field of a row is updated while a session is enabled, and +** then another field of the same row is updated while the session is disabled, +** the resulting changeset will contain an UPDATE change that updates both +** fields. +*/ +int sqlite3session_changeset( + sqlite3_session *pSession, /* Session object */ + int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */ + void **ppChangeset /* OUT: Buffer containing changeset */ +); + +/* +** CAPI3REF: Return An Upper-limit For The Size Of The Changeset +** METHOD: sqlite3_session +** +** By default, this function always returns 0. For it to return +** a useful result, the sqlite3_session object must have been configured +** to enable this API using sqlite3session_object_config() with the +** SQLITE_SESSION_OBJCONFIG_SIZE verb. +** +** When enabled, this function returns an upper limit, in bytes, for the size +** of the changeset that might be produced if sqlite3session_changeset() were +** called. The final changeset size might be equal to or smaller than the +** size in bytes returned by this function. +*/ +sqlite3_int64 sqlite3session_changeset_size(sqlite3_session *pSession); + +/* +** CAPI3REF: Load The Difference Between Tables Into A Session +** METHOD: sqlite3_session +** +** If it is not already attached to the session object passed as the first +** argument, this function attaches table zTbl in the same manner as the +** [sqlite3session_attach()] function. If zTbl does not exist, or if it +** does not have a primary key, this function is a no-op (but does not return +** an error). +** +** Argument zFromDb must be the name of a database ("main", "temp" etc.) +** attached to the same database handle as the session object that contains +** a table compatible with the table attached to the session by this function. +** A table is considered compatible if it: +** +**
      +**
    • Has the same name, +**
    • Has the same set of columns declared in the same order, and +**
    • Has the same PRIMARY KEY definition. +**
    +** +** If the tables are not compatible, SQLITE_SCHEMA is returned. If the tables +** are compatible but do not have any PRIMARY KEY columns, it is not an error +** but no changes are added to the session object. As with other session +** APIs, tables without PRIMARY KEYs are simply ignored. +** +** This function adds a set of changes to the session object that could be +** used to update the table in database zFrom (call this the "from-table") +** so that its content is the same as the table attached to the session +** object (call this the "to-table"). Specifically: +** +**
      +**
    • For each row (primary key) that exists in the to-table but not in +** the from-table, an INSERT record is added to the session object. +** +**
    • For each row (primary key) that exists in the to-table but not in +** the from-table, a DELETE record is added to the session object. +** +**
    • For each row (primary key) that exists in both tables, but features +** different non-PK values in each, an UPDATE record is added to the +** session. +**
    +** +** To clarify, if this function is called and then a changeset constructed +** using [sqlite3session_changeset()], then after applying that changeset to +** database zFrom the contents of the two compatible tables would be +** identical. +** +** Unless the call to this function is a no-op as described above, it is an +** error if database zFrom does not exist or does not contain the required +** compatible table. +** +** If the operation is successful, SQLITE_OK is returned. Otherwise, an SQLite +** error code. In this case, if argument pzErrMsg is not NULL, *pzErrMsg +** may be set to point to a buffer containing an English language error +** message. It is the responsibility of the caller to free this buffer using +** sqlite3_free(). +*/ +int sqlite3session_diff( + sqlite3_session *pSession, + const char *zFromDb, + const char *zTbl, + char **pzErrMsg +); + + +/* +** CAPI3REF: Generate A Patchset From A Session Object +** METHOD: sqlite3_session +** +** The differences between a patchset and a changeset are that: +** +**
      +**
    • DELETE records consist of the primary key fields only. The +** original values of other fields are omitted. +**
    • The original values of any modified fields are omitted from +** UPDATE records. +**
    +** +** A patchset blob may be used with up to date versions of all +** sqlite3changeset_xxx API functions except for sqlite3changeset_invert(), +** which returns SQLITE_CORRUPT if it is passed a patchset. Similarly, +** attempting to use a patchset blob with old versions of the +** sqlite3changeset_xxx APIs also provokes an SQLITE_CORRUPT error. +** +** Because the non-primary key "old.*" fields are omitted, no +** SQLITE_CHANGESET_DATA conflicts can be detected or reported if a patchset +** is passed to the sqlite3changeset_apply() API. Other conflict types work +** in the same way as for changesets. +** +** Changes within a patchset are ordered in the same way as for changesets +** generated by the sqlite3session_changeset() function (i.e. all changes for +** a single table are grouped together, tables appear in the order in which +** they were attached to the session object). +*/ +int sqlite3session_patchset( + sqlite3_session *pSession, /* Session object */ + int *pnPatchset, /* OUT: Size of buffer at *ppPatchset */ + void **ppPatchset /* OUT: Buffer containing patchset */ +); + +/* +** CAPI3REF: Test if a changeset has recorded any changes. +** +** Return non-zero if no changes to attached tables have been recorded by +** the session object passed as the first argument. Otherwise, if one or +** more changes have been recorded, return zero. +** +** Even if this function returns zero, it is possible that calling +** [sqlite3session_changeset()] on the session handle may still return a +** changeset that contains no changes. This can happen when a row in +** an attached table is modified and then later on the original values +** are restored. However, if this function returns non-zero, then it is +** guaranteed that a call to sqlite3session_changeset() will return a +** changeset containing zero changes. +*/ +int sqlite3session_isempty(sqlite3_session *pSession); + +/* +** CAPI3REF: Query for the amount of heap memory used by a session object. +** +** This API returns the total amount of heap memory in bytes currently +** used by the session object passed as the only argument. +*/ +sqlite3_int64 sqlite3session_memory_used(sqlite3_session *pSession); + +/* +** CAPI3REF: Create An Iterator To Traverse A Changeset +** CONSTRUCTOR: sqlite3_changeset_iter +** +** Create an iterator used to iterate through the contents of a changeset. +** If successful, *pp is set to point to the iterator handle and SQLITE_OK +** is returned. Otherwise, if an error occurs, *pp is set to zero and an +** SQLite error code is returned. +** +** The following functions can be used to advance and query a changeset +** iterator created by this function: +** +**
      +**
    • [sqlite3changeset_next()] +**
    • [sqlite3changeset_op()] +**
    • [sqlite3changeset_new()] +**
    • [sqlite3changeset_old()] +**
    +** +** It is the responsibility of the caller to eventually destroy the iterator +** by passing it to [sqlite3changeset_finalize()]. The buffer containing the +** changeset (pChangeset) must remain valid until after the iterator is +** destroyed. +** +** Assuming the changeset blob was created by one of the +** [sqlite3session_changeset()], [sqlite3changeset_concat()] or +** [sqlite3changeset_invert()] functions, all changes within the changeset +** that apply to a single table are grouped together. This means that when +** an application iterates through a changeset using an iterator created by +** this function, all changes that relate to a single table are visited +** consecutively. There is no chance that the iterator will visit a change +** the applies to table X, then one for table Y, and then later on visit +** another change for table X. +** +** The behavior of sqlite3changeset_start_v2() and its streaming equivalent +** may be modified by passing a combination of +** [SQLITE_CHANGESETSTART_INVERT | supported flags] as the 4th parameter. +** +** Note that the sqlite3changeset_start_v2() API is still experimental +** and therefore subject to change. +*/ +int sqlite3changeset_start( + sqlite3_changeset_iter **pp, /* OUT: New changeset iterator handle */ + int nChangeset, /* Size of changeset blob in bytes */ + void *pChangeset /* Pointer to blob containing changeset */ +); +int sqlite3changeset_start_v2( + sqlite3_changeset_iter **pp, /* OUT: New changeset iterator handle */ + int nChangeset, /* Size of changeset blob in bytes */ + void *pChangeset, /* Pointer to blob containing changeset */ + int flags /* SESSION_CHANGESETSTART_* flags */ +); + +/* +** CAPI3REF: Flags for sqlite3changeset_start_v2 +** +** The following flags may passed via the 4th parameter to +** [sqlite3changeset_start_v2] and [sqlite3changeset_start_v2_strm]: +** +**
    SQLITE_CHANGESETSTART_INVERT
    +** Invert the changeset while iterating through it. This is equivalent to +** inverting a changeset using sqlite3changeset_invert() before applying it. +** It is an error to specify this flag with a patchset. +*/ +#define SQLITE_CHANGESETSTART_INVERT 0x0002 + + +/* +** CAPI3REF: Advance A Changeset Iterator +** METHOD: sqlite3_changeset_iter +** +** This function may only be used with iterators created by the function +** [sqlite3changeset_start()]. If it is called on an iterator passed to +** a conflict-handler callback by [sqlite3changeset_apply()], SQLITE_MISUSE +** is returned and the call has no effect. +** +** Immediately after an iterator is created by sqlite3changeset_start(), it +** does not point to any change in the changeset. Assuming the changeset +** is not empty, the first call to this function advances the iterator to +** point to the first change in the changeset. Each subsequent call advances +** the iterator to point to the next change in the changeset (if any). If +** no error occurs and the iterator points to a valid change after a call +** to sqlite3changeset_next() has advanced it, SQLITE_ROW is returned. +** Otherwise, if all changes in the changeset have already been visited, +** SQLITE_DONE is returned. +** +** If an error occurs, an SQLite error code is returned. Possible error +** codes include SQLITE_CORRUPT (if the changeset buffer is corrupt) or +** SQLITE_NOMEM. +*/ +int sqlite3changeset_next(sqlite3_changeset_iter *pIter); + +/* +** CAPI3REF: Obtain The Current Operation From A Changeset Iterator +** METHOD: sqlite3_changeset_iter +** +** The pIter argument passed to this function may either be an iterator +** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator +** created by [sqlite3changeset_start()]. In the latter case, the most recent +** call to [sqlite3changeset_next()] must have returned [SQLITE_ROW]. If this +** is not the case, this function returns [SQLITE_MISUSE]. +** +** Arguments pOp, pnCol and pzTab may not be NULL. Upon return, three +** outputs are set through these pointers: +** +** *pOp is set to one of [SQLITE_INSERT], [SQLITE_DELETE] or [SQLITE_UPDATE], +** depending on the type of change that the iterator currently points to; +** +** *pnCol is set to the number of columns in the table affected by the change; and +** +** *pzTab is set to point to a nul-terminated utf-8 encoded string containing +** the name of the table affected by the current change. The buffer remains +** valid until either sqlite3changeset_next() is called on the iterator +** or until the conflict-handler function returns. +** +** If pbIndirect is not NULL, then *pbIndirect is set to true (1) if the change +** is an indirect change, or false (0) otherwise. See the documentation for +** [sqlite3session_indirect()] for a description of direct and indirect +** changes. +** +** If no error occurs, SQLITE_OK is returned. If an error does occur, an +** SQLite error code is returned. The values of the output variables may not +** be trusted in this case. +*/ +int sqlite3changeset_op( + sqlite3_changeset_iter *pIter, /* Iterator object */ + const char **pzTab, /* OUT: Pointer to table name */ + int *pnCol, /* OUT: Number of columns in table */ + int *pOp, /* OUT: SQLITE_INSERT, DELETE or UPDATE */ + int *pbIndirect /* OUT: True for an 'indirect' change */ +); + +/* +** CAPI3REF: Obtain The Primary Key Definition Of A Table +** METHOD: sqlite3_changeset_iter +** +** For each modified table, a changeset includes the following: +** +**
      +**
    • The number of columns in the table, and +**
    • Which of those columns make up the tables PRIMARY KEY. +**
    +** +** This function is used to find which columns comprise the PRIMARY KEY of +** the table modified by the change that iterator pIter currently points to. +** If successful, *pabPK is set to point to an array of nCol entries, where +** nCol is the number of columns in the table. Elements of *pabPK are set to +** 0x01 if the corresponding column is part of the tables primary key, or +** 0x00 if it is not. +** +** If argument pnCol is not NULL, then *pnCol is set to the number of columns +** in the table. +** +** If this function is called when the iterator does not point to a valid +** entry, SQLITE_MISUSE is returned and the output variables zeroed. Otherwise, +** SQLITE_OK is returned and the output variables populated as described +** above. +*/ +int sqlite3changeset_pk( + sqlite3_changeset_iter *pIter, /* Iterator object */ + unsigned char **pabPK, /* OUT: Array of boolean - true for PK cols */ + int *pnCol /* OUT: Number of entries in output array */ +); + +/* +** CAPI3REF: Obtain old.* Values From A Changeset Iterator +** METHOD: sqlite3_changeset_iter +** +** The pIter argument passed to this function may either be an iterator +** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator +** created by [sqlite3changeset_start()]. In the latter case, the most recent +** call to [sqlite3changeset_next()] must have returned SQLITE_ROW. +** Furthermore, it may only be called if the type of change that the iterator +** currently points to is either [SQLITE_DELETE] or [SQLITE_UPDATE]. Otherwise, +** this function returns [SQLITE_MISUSE] and sets *ppValue to NULL. +** +** Argument iVal must be greater than or equal to 0, and less than the number +** of columns in the table affected by the current change. Otherwise, +** [SQLITE_RANGE] is returned and *ppValue is set to NULL. +** +** If successful, this function sets *ppValue to point to a protected +** sqlite3_value object containing the iVal'th value from the vector of +** original row values stored as part of the UPDATE or DELETE change and +** returns SQLITE_OK. The name of the function comes from the fact that this +** is similar to the "old.*" columns available to update or delete triggers. +** +** If some other error occurs (e.g. an OOM condition), an SQLite error code +** is returned and *ppValue is set to NULL. +*/ +int sqlite3changeset_old( + sqlite3_changeset_iter *pIter, /* Changeset iterator */ + int iVal, /* Column number */ + sqlite3_value **ppValue /* OUT: Old value (or NULL pointer) */ +); + +/* +** CAPI3REF: Obtain new.* Values From A Changeset Iterator +** METHOD: sqlite3_changeset_iter +** +** The pIter argument passed to this function may either be an iterator +** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator +** created by [sqlite3changeset_start()]. In the latter case, the most recent +** call to [sqlite3changeset_next()] must have returned SQLITE_ROW. +** Furthermore, it may only be called if the type of change that the iterator +** currently points to is either [SQLITE_UPDATE] or [SQLITE_INSERT]. Otherwise, +** this function returns [SQLITE_MISUSE] and sets *ppValue to NULL. +** +** Argument iVal must be greater than or equal to 0, and less than the number +** of columns in the table affected by the current change. Otherwise, +** [SQLITE_RANGE] is returned and *ppValue is set to NULL. +** +** If successful, this function sets *ppValue to point to a protected +** sqlite3_value object containing the iVal'th value from the vector of +** new row values stored as part of the UPDATE or INSERT change and +** returns SQLITE_OK. If the change is an UPDATE and does not include +** a new value for the requested column, *ppValue is set to NULL and +** SQLITE_OK returned. The name of the function comes from the fact that +** this is similar to the "new.*" columns available to update or delete +** triggers. +** +** If some other error occurs (e.g. an OOM condition), an SQLite error code +** is returned and *ppValue is set to NULL. +*/ +int sqlite3changeset_new( + sqlite3_changeset_iter *pIter, /* Changeset iterator */ + int iVal, /* Column number */ + sqlite3_value **ppValue /* OUT: New value (or NULL pointer) */ +); + +/* +** CAPI3REF: Obtain Conflicting Row Values From A Changeset Iterator +** METHOD: sqlite3_changeset_iter +** +** This function should only be used with iterator objects passed to a +** conflict-handler callback by [sqlite3changeset_apply()] with either +** [SQLITE_CHANGESET_DATA] or [SQLITE_CHANGESET_CONFLICT]. If this function +** is called on any other iterator, [SQLITE_MISUSE] is returned and *ppValue +** is set to NULL. +** +** Argument iVal must be greater than or equal to 0, and less than the number +** of columns in the table affected by the current change. Otherwise, +** [SQLITE_RANGE] is returned and *ppValue is set to NULL. +** +** If successful, this function sets *ppValue to point to a protected +** sqlite3_value object containing the iVal'th value from the +** "conflicting row" associated with the current conflict-handler callback +** and returns SQLITE_OK. +** +** If some other error occurs (e.g. an OOM condition), an SQLite error code +** is returned and *ppValue is set to NULL. +*/ +int sqlite3changeset_conflict( + sqlite3_changeset_iter *pIter, /* Changeset iterator */ + int iVal, /* Column number */ + sqlite3_value **ppValue /* OUT: Value from conflicting row */ +); + +/* +** CAPI3REF: Determine The Number Of Foreign Key Constraint Violations +** METHOD: sqlite3_changeset_iter +** +** This function may only be called with an iterator passed to an +** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case +** it sets the output variable to the total number of known foreign key +** violations in the destination database and returns SQLITE_OK. +** +** In all other cases this function returns SQLITE_MISUSE. +*/ +int sqlite3changeset_fk_conflicts( + sqlite3_changeset_iter *pIter, /* Changeset iterator */ + int *pnOut /* OUT: Number of FK violations */ +); + + +/* +** CAPI3REF: Finalize A Changeset Iterator +** METHOD: sqlite3_changeset_iter +** +** This function is used to finalize an iterator allocated with +** [sqlite3changeset_start()]. +** +** This function should only be called on iterators created using the +** [sqlite3changeset_start()] function. If an application calls this +** function with an iterator passed to a conflict-handler by +** [sqlite3changeset_apply()], [SQLITE_MISUSE] is immediately returned and the +** call has no effect. +** +** If an error was encountered within a call to an sqlite3changeset_xxx() +** function (for example an [SQLITE_CORRUPT] in [sqlite3changeset_next()] or an +** [SQLITE_NOMEM] in [sqlite3changeset_new()]) then an error code corresponding +** to that error is returned by this function. Otherwise, SQLITE_OK is +** returned. This is to allow the following pattern (pseudo-code): +** +**
    +**   sqlite3changeset_start();
    +**   while( SQLITE_ROW==sqlite3changeset_next() ){
    +**     // Do something with change.
    +**   }
    +**   rc = sqlite3changeset_finalize();
    +**   if( rc!=SQLITE_OK ){
    +**     // An error has occurred 
    +**   }
    +** 
    +*/ +int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter); + +/* +** CAPI3REF: Invert A Changeset +** +** This function is used to "invert" a changeset object. Applying an inverted +** changeset to a database reverses the effects of applying the uninverted +** changeset. Specifically: +** +**
      +**
    • Each DELETE change is changed to an INSERT, and +**
    • Each INSERT change is changed to a DELETE, and +**
    • For each UPDATE change, the old.* and new.* values are exchanged. +**
    +** +** This function does not change the order in which changes appear within +** the changeset. It merely reverses the sense of each individual change. +** +** If successful, a pointer to a buffer containing the inverted changeset +** is stored in *ppOut, the size of the same buffer is stored in *pnOut, and +** SQLITE_OK is returned. If an error occurs, both *pnOut and *ppOut are +** zeroed and an SQLite error code returned. +** +** It is the responsibility of the caller to eventually call sqlite3_free() +** on the *ppOut pointer to free the buffer allocation following a successful +** call to this function. +** +** WARNING/TODO: This function currently assumes that the input is a valid +** changeset. If it is not, the results are undefined. +*/ +int sqlite3changeset_invert( + int nIn, const void *pIn, /* Input changeset */ + int *pnOut, void **ppOut /* OUT: Inverse of input */ +); + +/* +** CAPI3REF: Concatenate Two Changeset Objects +** +** This function is used to concatenate two changesets, A and B, into a +** single changeset. The result is a changeset equivalent to applying +** changeset A followed by changeset B. +** +** This function combines the two input changesets using an +** sqlite3_changegroup object. Calling it produces similar results as the +** following code fragment: +** +**
    +**   sqlite3_changegroup *pGrp;
    +**   rc = sqlite3_changegroup_new(&pGrp);
    +**   if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nA, pA);
    +**   if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nB, pB);
    +**   if( rc==SQLITE_OK ){
    +**     rc = sqlite3changegroup_output(pGrp, pnOut, ppOut);
    +**   }else{
    +**     *ppOut = 0;
    +**     *pnOut = 0;
    +**   }
    +** 
    +** +** Refer to the sqlite3_changegroup documentation below for details. +*/ +int sqlite3changeset_concat( + int nA, /* Number of bytes in buffer pA */ + void *pA, /* Pointer to buffer containing changeset A */ + int nB, /* Number of bytes in buffer pB */ + void *pB, /* Pointer to buffer containing changeset B */ + int *pnOut, /* OUT: Number of bytes in output changeset */ + void **ppOut /* OUT: Buffer containing output changeset */ +); + +/* +** CAPI3REF: Changegroup Handle +** +** A changegroup is an object used to combine two or more +** [changesets] or [patchsets] +*/ +typedef struct sqlite3_changegroup sqlite3_changegroup; + +/* +** CAPI3REF: Create A New Changegroup Object +** CONSTRUCTOR: sqlite3_changegroup +** +** An sqlite3_changegroup object is used to combine two or more changesets +** (or patchsets) into a single changeset (or patchset). A single changegroup +** object may combine changesets or patchsets, but not both. The output is +** always in the same format as the input. +** +** If successful, this function returns SQLITE_OK and populates (*pp) with +** a pointer to a new sqlite3_changegroup object before returning. The caller +** should eventually free the returned object using a call to +** sqlite3changegroup_delete(). If an error occurs, an SQLite error code +** (i.e. SQLITE_NOMEM) is returned and *pp is set to NULL. +** +** The usual usage pattern for an sqlite3_changegroup object is as follows: +** +**
      +**
    • It is created using a call to sqlite3changegroup_new(). +** +**
    • Zero or more changesets (or patchsets) are added to the object +** by calling sqlite3changegroup_add(). +** +**
    • The result of combining all input changesets together is obtained +** by the application via a call to sqlite3changegroup_output(). +** +**
    • The object is deleted using a call to sqlite3changegroup_delete(). +**
    +** +** Any number of calls to add() and output() may be made between the calls to +** new() and delete(), and in any order. +** +** As well as the regular sqlite3changegroup_add() and +** sqlite3changegroup_output() functions, also available are the streaming +** versions sqlite3changegroup_add_strm() and sqlite3changegroup_output_strm(). +*/ +int sqlite3changegroup_new(sqlite3_changegroup **pp); + +/* +** CAPI3REF: Add a Schema to a Changegroup +** METHOD: sqlite3_changegroup_schema +** +** This method may be used to optionally enforce the rule that the changesets +** added to the changegroup handle must match the schema of database zDb +** ("main", "temp", or the name of an attached database). If +** sqlite3changegroup_add() is called to add a changeset that is not compatible +** with the configured schema, SQLITE_SCHEMA is returned and the changegroup +** object is left in an undefined state. +** +** A changeset schema is considered compatible with the database schema in +** the same way as for sqlite3changeset_apply(). Specifically, for each +** table in the changeset, there exists a database table with: +** +**
      +**
    • The name identified by the changeset, and +**
    • at least as many columns as recorded in the changeset, and +**
    • the primary key columns in the same position as recorded in +** the changeset. +**
    +** +** The output of the changegroup object always has the same schema as the +** database nominated using this function. In cases where changesets passed +** to sqlite3changegroup_add() have fewer columns than the corresponding table +** in the database schema, these are filled in using the default column +** values from the database schema. This makes it possible to combined +** changesets that have different numbers of columns for a single table +** within a changegroup, provided that they are otherwise compatible. +*/ +int sqlite3changegroup_schema(sqlite3_changegroup*, sqlite3*, const char *zDb); + +/* +** CAPI3REF: Add A Changeset To A Changegroup +** METHOD: sqlite3_changegroup +** +** Add all changes within the changeset (or patchset) in buffer pData (size +** nData bytes) to the changegroup. +** +** If the buffer contains a patchset, then all prior calls to this function +** on the same changegroup object must also have specified patchsets. Or, if +** the buffer contains a changeset, so must have the earlier calls to this +** function. Otherwise, SQLITE_ERROR is returned and no changes are added +** to the changegroup. +** +** Rows within the changeset and changegroup are identified by the values in +** their PRIMARY KEY columns. A change in the changeset is considered to +** apply to the same row as a change already present in the changegroup if +** the two rows have the same primary key. +** +** Changes to rows that do not already appear in the changegroup are +** simply copied into it. Or, if both the new changeset and the changegroup +** contain changes that apply to a single row, the final contents of the +** changegroup depends on the type of each change, as follows: +** +** +** +** +**
    Existing Change New Change Output Change +**
    INSERT INSERT +** The new change is ignored. This case does not occur if the new +** changeset was recorded immediately after the changesets already +** added to the changegroup. +**
    INSERT UPDATE +** The INSERT change remains in the changegroup. The values in the +** INSERT change are modified as if the row was inserted by the +** existing change and then updated according to the new change. +**
    INSERT DELETE +** The existing INSERT is removed from the changegroup. The DELETE is +** not added. +**
    UPDATE INSERT +** The new change is ignored. This case does not occur if the new +** changeset was recorded immediately after the changesets already +** added to the changegroup. +**
    UPDATE UPDATE +** The existing UPDATE remains within the changegroup. It is amended +** so that the accompanying values are as if the row was updated once +** by the existing change and then again by the new change. +**
    UPDATE DELETE +** The existing UPDATE is replaced by the new DELETE within the +** changegroup. +**
    DELETE INSERT +** If one or more of the column values in the row inserted by the +** new change differ from those in the row deleted by the existing +** change, the existing DELETE is replaced by an UPDATE within the +** changegroup. Otherwise, if the inserted row is exactly the same +** as the deleted row, the existing DELETE is simply discarded. +**
    DELETE UPDATE +** The new change is ignored. This case does not occur if the new +** changeset was recorded immediately after the changesets already +** added to the changegroup. +**
    DELETE DELETE +** The new change is ignored. This case does not occur if the new +** changeset was recorded immediately after the changesets already +** added to the changegroup. +**
    +** +** If the new changeset contains changes to a table that is already present +** in the changegroup, then the number of columns and the position of the +** primary key columns for the table must be consistent. If this is not the +** case, this function fails with SQLITE_SCHEMA. Except, if the changegroup +** object has been configured with a database schema using the +** sqlite3changegroup_schema() API, then it is possible to combine changesets +** with different numbers of columns for a single table, provided that +** they are otherwise compatible. +** +** If the input changeset appears to be corrupt and the corruption is +** detected, SQLITE_CORRUPT is returned. Or, if an out-of-memory condition +** occurs during processing, this function returns SQLITE_NOMEM. +** +** In all cases, if an error occurs the state of the final contents of the +** changegroup is undefined. If no error occurs, SQLITE_OK is returned. +*/ +int sqlite3changegroup_add(sqlite3_changegroup*, int nData, void *pData); + +/* +** CAPI3REF: Add A Single Change To A Changegroup +** METHOD: sqlite3_changegroup +** +** This function adds the single change currently indicated by the iterator +** passed as the second argument to the changegroup object. The rules for +** adding the change are just as described for [sqlite3changegroup_add()]. +** +** If the change is successfully added to the changegroup, SQLITE_OK is +** returned. Otherwise, an SQLite error code is returned. +** +** The iterator must point to a valid entry when this function is called. +** If it does not, SQLITE_ERROR is returned and no change is added to the +** changegroup. Additionally, the iterator must not have been opened with +** the SQLITE_CHANGESETAPPLY_INVERT flag. In this case SQLITE_ERROR is also +** returned. +*/ +int sqlite3changegroup_add_change( + sqlite3_changegroup*, + sqlite3_changeset_iter* +); + + + +/* +** CAPI3REF: Obtain A Composite Changeset From A Changegroup +** METHOD: sqlite3_changegroup +** +** Obtain a buffer containing a changeset (or patchset) representing the +** current contents of the changegroup. If the inputs to the changegroup +** were themselves changesets, the output is a changeset. Or, if the +** inputs were patchsets, the output is also a patchset. +** +** As with the output of the sqlite3session_changeset() and +** sqlite3session_patchset() functions, all changes related to a single +** table are grouped together in the output of this function. Tables appear +** in the same order as for the very first changeset added to the changegroup. +** If the second or subsequent changesets added to the changegroup contain +** changes for tables that do not appear in the first changeset, they are +** appended onto the end of the output changeset, again in the order in +** which they are first encountered. +** +** If an error occurs, an SQLite error code is returned and the output +** variables (*pnData) and (*ppData) are set to 0. Otherwise, SQLITE_OK +** is returned and the output variables are set to the size of and a +** pointer to the output buffer, respectively. In this case it is the +** responsibility of the caller to eventually free the buffer using a +** call to sqlite3_free(). +*/ +int sqlite3changegroup_output( + sqlite3_changegroup*, + int *pnData, /* OUT: Size of output buffer in bytes */ + void **ppData /* OUT: Pointer to output buffer */ +); + +/* +** CAPI3REF: Delete A Changegroup Object +** DESTRUCTOR: sqlite3_changegroup +*/ +void sqlite3changegroup_delete(sqlite3_changegroup*); + +/* +** CAPI3REF: Apply A Changeset To A Database +** +** Apply a changeset or patchset to a database. These functions attempt to +** update the "main" database attached to handle db with the changes found in +** the changeset passed via the second and third arguments. +** +** All changes made by these functions are enclosed in a savepoint transaction. +** If any other error (aside from a constraint failure when attempting to +** write to the target database) occurs, then the savepoint transaction is +** rolled back, restoring the target database to its original state, and an +** SQLite error code returned. Additionally, starting with version 3.51.0, +** an error code and error message that may be accessed using the +** [sqlite3_errcode()] and [sqlite3_errmsg()] APIs are left in the database +** handle. +** +** The fourth argument (xFilter) passed to these functions is the "filter +** callback". This may be passed NULL, in which case all changes in the +** changeset are applied to the database. For sqlite3changeset_apply() and +** sqlite3_changeset_apply_v2(), if it is not NULL, then it is invoked once +** for each table affected by at least one change in the changeset. In this +** case the table name is passed as the second argument, and a copy of +** the context pointer passed as the sixth argument to apply() or apply_v2() +** as the first. If the "filter callback" returns zero, then no attempt is +** made to apply any changes to the table. Otherwise, if the return value is +** non-zero, all changes related to the table are attempted. +** +** For sqlite3_changeset_apply_v3(), the xFilter callback is invoked once +** per change. The second argument in this case is an sqlite3_changeset_iter +** that may be queried using the usual APIs for the details of the current +** change. If the "filter callback" returns zero in this case, then no attempt +** is made to apply the current change. If it returns non-zero, the change +** is applied. +** +** For each table that is not excluded by the filter callback, this function +** tests that the target database contains a compatible table. A table is +** considered compatible if all of the following are true: +** +**
      +**
    • The table has the same name as the name recorded in the +** changeset, and +**
    • The table has at least as many columns as recorded in the +** changeset, and +**
    • The table has primary key columns in the same position as +** recorded in the changeset. +**
    +** +** If there is no compatible table, it is not an error, but none of the +** changes associated with the table are applied. A warning message is issued +** via the sqlite3_log() mechanism with the error code SQLITE_SCHEMA. At most +** one such warning is issued for each table in the changeset. +** +** For each change for which there is a compatible table, an attempt is made +** to modify the table contents according to each UPDATE, INSERT or DELETE +** change that is not excluded by a filter callback. If a change cannot be +** applied cleanly, the conflict handler function passed as the fifth argument +** to sqlite3changeset_apply() may be invoked. A description of exactly when +** the conflict handler is invoked for each type of change is below. +** +** Unlike the xFilter argument, xConflict may not be passed NULL. The results +** of passing anything other than a valid function pointer as the xConflict +** argument are undefined. +** +** Each time the conflict handler function is invoked, it must return one +** of [SQLITE_CHANGESET_OMIT], [SQLITE_CHANGESET_ABORT] or +** [SQLITE_CHANGESET_REPLACE]. SQLITE_CHANGESET_REPLACE may only be returned +** if the second argument passed to the conflict handler is either +** SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If the conflict-handler +** returns an illegal value, any changes already made are rolled back and +** the call to sqlite3changeset_apply() returns SQLITE_MISUSE. Different +** actions are taken by sqlite3changeset_apply() depending on the value +** returned by each invocation of the conflict-handler function. Refer to +** the documentation for the three +** [SQLITE_CHANGESET_OMIT|available return values] for details. +** +**
    +**
    DELETE Changes
    +** For each DELETE change, the function checks if the target database +** contains a row with the same primary key value (or values) as the +** original row values stored in the changeset. If it does, and the values +** stored in all non-primary key columns also match the values stored in +** the changeset the row is deleted from the target database. +** +** If a row with matching primary key values is found, but one or more of +** the non-primary key fields contains a value different from the original +** row value stored in the changeset, the conflict-handler function is +** invoked with [SQLITE_CHANGESET_DATA] as the second argument. If the +** database table has more columns than are recorded in the changeset, +** only the values of those non-primary key fields are compared against +** the current database contents - any trailing database table columns +** are ignored. +** +** If no row with matching primary key values is found in the database, +** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND] +** passed as the second argument. +** +** If the DELETE operation is attempted, but SQLite returns SQLITE_CONSTRAINT +** (which can only happen if a foreign key constraint is violated), the +** conflict-handler function is invoked with [SQLITE_CHANGESET_CONSTRAINT] +** passed as the second argument. This includes the case where the DELETE +** operation is attempted because an earlier call to the conflict handler +** function returned [SQLITE_CHANGESET_REPLACE]. +** +**
    INSERT Changes
    +** For each INSERT change, an attempt is made to insert the new row into +** the database. If the changeset row contains fewer fields than the +** database table, the trailing fields are populated with their default +** values. +** +** If the attempt to insert the row fails because the database already +** contains a row with the same primary key values, the conflict handler +** function is invoked with the second argument set to +** [SQLITE_CHANGESET_CONFLICT]. +** +** If the attempt to insert the row fails because of some other constraint +** violation (e.g. NOT NULL or UNIQUE), the conflict handler function is +** invoked with the second argument set to [SQLITE_CHANGESET_CONSTRAINT]. +** This includes the case where the INSERT operation is re-attempted because +** an earlier call to the conflict handler function returned +** [SQLITE_CHANGESET_REPLACE]. +** +**
    UPDATE Changes
    +** For each UPDATE change, the function checks if the target database +** contains a row with the same primary key value (or values) as the +** original row values stored in the changeset. If it does, and the values +** stored in all modified non-primary key columns also match the values +** stored in the changeset the row is updated within the target database. +** +** If a row with matching primary key values is found, but one or more of +** the modified non-primary key fields contains a value different from an +** original row value stored in the changeset, the conflict-handler function +** is invoked with [SQLITE_CHANGESET_DATA] as the second argument. Since +** UPDATE changes only contain values for non-primary key fields that are +** to be modified, only those fields need to match the original values to +** avoid the SQLITE_CHANGESET_DATA conflict-handler callback. +** +** If no row with matching primary key values is found in the database, +** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND] +** passed as the second argument. +** +** If the UPDATE operation is attempted, but SQLite returns +** SQLITE_CONSTRAINT, the conflict-handler function is invoked with +** [SQLITE_CHANGESET_CONSTRAINT] passed as the second argument. +** This includes the case where the UPDATE operation is attempted after +** an earlier call to the conflict handler function returned +** [SQLITE_CHANGESET_REPLACE]. +**
    +** +** It is safe to execute SQL statements, including those that write to the +** table that the callback related to, from within the xConflict callback. +** This can be used to further customize the application's conflict +** resolution strategy. +** +** If the output parameters (ppRebase) and (pnRebase) are non-NULL and +** the input is a changeset (not a patchset), then sqlite3changeset_apply_v2() +** may set (*ppRebase) to point to a "rebase" that may be used with the +** sqlite3_rebaser APIs buffer before returning. In this case (*pnRebase) +** is set to the size of the buffer in bytes. It is the responsibility of the +** caller to eventually free any such buffer using sqlite3_free(). The buffer +** is only allocated and populated if one or more conflicts were encountered +** while applying the patchset. See comments surrounding the sqlite3_rebaser +** APIs for further details. +** +** The behavior of sqlite3changeset_apply_v2() and its streaming equivalent +** may be modified by passing a combination of +** [SQLITE_CHANGESETAPPLY_NOSAVEPOINT | supported flags] as the 9th parameter. +** +** Note that the sqlite3changeset_apply_v2() API is still experimental +** and therefore subject to change. +*/ +int sqlite3changeset_apply( + sqlite3 *db, /* Apply change to "main" db of this handle */ + int nChangeset, /* Size of changeset in bytes */ + void *pChangeset, /* Changeset blob */ + int(*xFilter)( + void *pCtx, /* Copy of sixth arg to _apply() */ + const char *zTab /* Table name */ + ), + int(*xConflict)( + void *pCtx, /* Copy of sixth arg to _apply() */ + int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */ + sqlite3_changeset_iter *p /* Handle describing change and conflict */ + ), + void *pCtx /* First argument passed to xConflict */ +); +int sqlite3changeset_apply_v2( + sqlite3 *db, /* Apply change to "main" db of this handle */ + int nChangeset, /* Size of changeset in bytes */ + void *pChangeset, /* Changeset blob */ + int(*xFilter)( + void *pCtx, /* Copy of sixth arg to _apply() */ + const char *zTab /* Table name */ + ), + int(*xConflict)( + void *pCtx, /* Copy of sixth arg to _apply() */ + int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */ + sqlite3_changeset_iter *p /* Handle describing change and conflict */ + ), + void *pCtx, /* First argument passed to xConflict */ + void **ppRebase, int *pnRebase, /* OUT: Rebase data */ + int flags /* SESSION_CHANGESETAPPLY_* flags */ +); +int sqlite3changeset_apply_v3( + sqlite3 *db, /* Apply change to "main" db of this handle */ + int nChangeset, /* Size of changeset in bytes */ + void *pChangeset, /* Changeset blob */ + int(*xFilter)( + void *pCtx, /* Copy of sixth arg to _apply() */ + sqlite3_changeset_iter *p /* Handle describing change */ + ), + int(*xConflict)( + void *pCtx, /* Copy of sixth arg to _apply() */ + int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */ + sqlite3_changeset_iter *p /* Handle describing change and conflict */ + ), + void *pCtx, /* First argument passed to xConflict */ + void **ppRebase, int *pnRebase, /* OUT: Rebase data */ + int flags /* SESSION_CHANGESETAPPLY_* flags */ +); + +/* +** CAPI3REF: Flags for sqlite3changeset_apply_v2 +** +** The following flags may passed via the 9th parameter to +** [sqlite3changeset_apply_v2] and [sqlite3changeset_apply_v2_strm]: +** +**
    +**
    SQLITE_CHANGESETAPPLY_NOSAVEPOINT
    +** Usually, the sessions module encloses all operations performed by +** a single call to apply_v2() or apply_v2_strm() in a [SAVEPOINT]. The +** SAVEPOINT is committed if the changeset or patchset is successfully +** applied, or rolled back if an error occurs. Specifying this flag +** causes the sessions module to omit this savepoint. In this case, if the +** caller has an open transaction or savepoint when apply_v2() is called, +** it may revert the partially applied changeset by rolling it back. +** +**
    SQLITE_CHANGESETAPPLY_INVERT
    +** Invert the changeset before applying it. This is equivalent to inverting +** a changeset using sqlite3changeset_invert() before applying it. It is +** an error to specify this flag with a patchset. +** +**
    SQLITE_CHANGESETAPPLY_IGNORENOOP
    +** Do not invoke the conflict handler callback for any changes that +** would not actually modify the database even if they were applied. +** Specifically, this means that the conflict handler is not invoked +** for: +**
      +**
    • a delete change if the row being deleted cannot be found, +**
    • an update change if the modified fields are already set to +** their new values in the conflicting row, or +**
    • an insert change if all fields of the conflicting row match +** the row being inserted. +**
    +** +**
    SQLITE_CHANGESETAPPLY_FKNOACTION
    +** If this flag it set, then all foreign key constraints in the target +** database behave as if they were declared with "ON UPDATE NO ACTION ON +** DELETE NO ACTION", even if they are actually CASCADE, RESTRICT, SET NULL +** or SET DEFAULT. +*/ +#define SQLITE_CHANGESETAPPLY_NOSAVEPOINT 0x0001 +#define SQLITE_CHANGESETAPPLY_INVERT 0x0002 +#define SQLITE_CHANGESETAPPLY_IGNORENOOP 0x0004 +#define SQLITE_CHANGESETAPPLY_FKNOACTION 0x0008 + +/* +** CAPI3REF: Constants Passed To The Conflict Handler +** +** Values that may be passed as the second argument to a conflict-handler. +** +**
    +**
    SQLITE_CHANGESET_DATA
    +** The conflict handler is invoked with CHANGESET_DATA as the second argument +** when processing a DELETE or UPDATE change if a row with the required +** PRIMARY KEY fields is present in the database, but one or more other +** (non primary-key) fields modified by the update do not contain the +** expected "before" values. +** +** The conflicting row, in this case, is the database row with the matching +** primary key. +** +**
    SQLITE_CHANGESET_NOTFOUND
    +** The conflict handler is invoked with CHANGESET_NOTFOUND as the second +** argument when processing a DELETE or UPDATE change if a row with the +** required PRIMARY KEY fields is not present in the database. +** +** There is no conflicting row in this case. The results of invoking the +** sqlite3changeset_conflict() API are undefined. +** +**
    SQLITE_CHANGESET_CONFLICT
    +** CHANGESET_CONFLICT is passed as the second argument to the conflict +** handler while processing an INSERT change if the operation would result +** in duplicate primary key values. +** +** The conflicting row in this case is the database row with the matching +** primary key. +** +**
    SQLITE_CHANGESET_FOREIGN_KEY
    +** If foreign key handling is enabled, and applying a changeset leaves the +** database in a state containing foreign key violations, the conflict +** handler is invoked with CHANGESET_FOREIGN_KEY as the second argument +** exactly once before the changeset is committed. If the conflict handler +** returns CHANGESET_OMIT, the changes, including those that caused the +** foreign key constraint violation, are committed. Or, if it returns +** CHANGESET_ABORT, the changeset is rolled back. +** +** No current or conflicting row information is provided. The only function +** it is possible to call on the supplied sqlite3_changeset_iter handle +** is sqlite3changeset_fk_conflicts(). +** +**
    SQLITE_CHANGESET_CONSTRAINT
    +** If any other constraint violation occurs while applying a change (i.e. +** a UNIQUE, CHECK or NOT NULL constraint), the conflict handler is +** invoked with CHANGESET_CONSTRAINT as the second argument. +** +** There is no conflicting row in this case. The results of invoking the +** sqlite3changeset_conflict() API are undefined. +** +**
    +*/ +#define SQLITE_CHANGESET_DATA 1 +#define SQLITE_CHANGESET_NOTFOUND 2 +#define SQLITE_CHANGESET_CONFLICT 3 +#define SQLITE_CHANGESET_CONSTRAINT 4 +#define SQLITE_CHANGESET_FOREIGN_KEY 5 + +/* +** CAPI3REF: Constants Returned By The Conflict Handler +** +** A conflict handler callback must return one of the following three values. +** +**
    +**
    SQLITE_CHANGESET_OMIT
    +** If a conflict handler returns this value no special action is taken. The +** change that caused the conflict is not applied. The session module +** continues to the next change in the changeset. +** +**
    SQLITE_CHANGESET_REPLACE
    +** This value may only be returned if the second argument to the conflict +** handler was SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If this +** is not the case, any changes applied so far are rolled back and the +** call to sqlite3changeset_apply() returns SQLITE_MISUSE. +** +** If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_DATA conflict +** handler, then the conflicting row is either updated or deleted, depending +** on the type of change. +** +** If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_CONFLICT conflict +** handler, then the conflicting row is removed from the database and a +** second attempt to apply the change is made. If this second attempt fails, +** the original row is restored to the database before continuing. +** +**
    SQLITE_CHANGESET_ABORT
    +** If this value is returned, any changes applied so far are rolled back +** and the call to sqlite3changeset_apply() returns SQLITE_ABORT. +**
    +*/ +#define SQLITE_CHANGESET_OMIT 0 +#define SQLITE_CHANGESET_REPLACE 1 +#define SQLITE_CHANGESET_ABORT 2 + +/* +** CAPI3REF: Rebasing changesets +** EXPERIMENTAL +** +** Suppose there is a site hosting a database in state S0. And that +** modifications are made that move that database to state S1 and a +** changeset recorded (the "local" changeset). Then, a changeset based +** on S0 is received from another site (the "remote" changeset) and +** applied to the database. The database is then in state +** (S1+"remote"), where the exact state depends on any conflict +** resolution decisions (OMIT or REPLACE) made while applying "remote". +** Rebasing a changeset is to update it to take those conflict +** resolution decisions into account, so that the same conflicts +** do not have to be resolved elsewhere in the network. +** +** For example, if both the local and remote changesets contain an +** INSERT of the same key on "CREATE TABLE t1(a PRIMARY KEY, b)": +** +** local: INSERT INTO t1 VALUES(1, 'v1'); +** remote: INSERT INTO t1 VALUES(1, 'v2'); +** +** and the conflict resolution is REPLACE, then the INSERT change is +** removed from the local changeset (it was overridden). Or, if the +** conflict resolution was "OMIT", then the local changeset is modified +** to instead contain: +** +** UPDATE t1 SET b = 'v2' WHERE a=1; +** +** Changes within the local changeset are rebased as follows: +** +**
    +**
    Local INSERT
    +** This may only conflict with a remote INSERT. If the conflict +** resolution was OMIT, then add an UPDATE change to the rebased +** changeset. Or, if the conflict resolution was REPLACE, add +** nothing to the rebased changeset. +** +**
    Local DELETE
    +** This may conflict with a remote UPDATE or DELETE. In both cases the +** only possible resolution is OMIT. If the remote operation was a +** DELETE, then add no change to the rebased changeset. If the remote +** operation was an UPDATE, then the old.* fields of change are updated +** to reflect the new.* values in the UPDATE. +** +**
    Local UPDATE
    +** This may conflict with a remote UPDATE or DELETE. If it conflicts +** with a DELETE, and the conflict resolution was OMIT, then the update +** is changed into an INSERT. Any undefined values in the new.* record +** from the update change are filled in using the old.* values from +** the conflicting DELETE. Or, if the conflict resolution was REPLACE, +** the UPDATE change is simply omitted from the rebased changeset. +** +** If conflict is with a remote UPDATE and the resolution is OMIT, then +** the old.* values are rebased using the new.* values in the remote +** change. Or, if the resolution is REPLACE, then the change is copied +** into the rebased changeset with updates to columns also updated by +** the conflicting remote UPDATE removed. If this means no columns would +** be updated, the change is omitted. +**
    +** +** A local change may be rebased against multiple remote changes +** simultaneously. If a single key is modified by multiple remote +** changesets, they are combined as follows before the local changeset +** is rebased: +** +**
      +**
    • If there has been one or more REPLACE resolutions on a +** key, it is rebased according to a REPLACE. +** +**
    • If there have been no REPLACE resolutions on a key, then +** the local changeset is rebased according to the most recent +** of the OMIT resolutions. +**
    +** +** Note that conflict resolutions from multiple remote changesets are +** combined on a per-field basis, not per-row. This means that in the +** case of multiple remote UPDATE operations, some fields of a single +** local change may be rebased for REPLACE while others are rebased for +** OMIT. +** +** In order to rebase a local changeset, the remote changeset must first +** be applied to the local database using sqlite3changeset_apply_v2() and +** the buffer of rebase information captured. Then: +** +**
      +**
    1. An sqlite3_rebaser object is created by calling +** sqlite3rebaser_create(). +**
    2. The new object is configured with the rebase buffer obtained from +** sqlite3changeset_apply_v2() by calling sqlite3rebaser_configure(). +** If the local changeset is to be rebased against multiple remote +** changesets, then sqlite3rebaser_configure() should be called +** multiple times, in the same order that the multiple +** sqlite3changeset_apply_v2() calls were made. +**
    3. Each local changeset is rebased by calling sqlite3rebaser_rebase(). +**
    4. The sqlite3_rebaser object is deleted by calling +** sqlite3rebaser_delete(). +**
    +*/ +typedef struct sqlite3_rebaser sqlite3_rebaser; + +/* +** CAPI3REF: Create a changeset rebaser object. +** EXPERIMENTAL +** +** Allocate a new changeset rebaser object. If successful, set (*ppNew) to +** point to the new object and return SQLITE_OK. Otherwise, if an error +** occurs, return an SQLite error code (e.g. SQLITE_NOMEM) and set (*ppNew) +** to NULL. +*/ +int sqlite3rebaser_create(sqlite3_rebaser **ppNew); + +/* +** CAPI3REF: Configure a changeset rebaser object. +** EXPERIMENTAL +** +** Configure the changeset rebaser object to rebase changesets according +** to the conflict resolutions described by buffer pRebase (size nRebase +** bytes), which must have been obtained from a previous call to +** sqlite3changeset_apply_v2(). +*/ +int sqlite3rebaser_configure( + sqlite3_rebaser*, + int nRebase, const void *pRebase +); + +/* +** CAPI3REF: Rebase a changeset +** EXPERIMENTAL +** +** Argument pIn must point to a buffer containing a changeset nIn bytes +** in size. This function allocates and populates a buffer with a copy +** of the changeset rebased according to the configuration of the +** rebaser object passed as the first argument. If successful, (*ppOut) +** is set to point to the new buffer containing the rebased changeset and +** (*pnOut) to its size in bytes and SQLITE_OK returned. It is the +** responsibility of the caller to eventually free the new buffer using +** sqlite3_free(). Otherwise, if an error occurs, (*ppOut) and (*pnOut) +** are set to zero and an SQLite error code returned. +*/ +int sqlite3rebaser_rebase( + sqlite3_rebaser*, + int nIn, const void *pIn, + int *pnOut, void **ppOut +); + +/* +** CAPI3REF: Delete a changeset rebaser object. +** EXPERIMENTAL +** +** Delete the changeset rebaser object and all associated resources. There +** should be one call to this function for each successful invocation +** of sqlite3rebaser_create(). +*/ +void sqlite3rebaser_delete(sqlite3_rebaser *p); + +/* +** CAPI3REF: Streaming Versions of API functions. +** +** The six streaming API xxx_strm() functions serve similar purposes to the +** corresponding non-streaming API functions: +** +** +** +**
    Streaming functionNon-streaming equivalent
    sqlite3changeset_apply_strm[sqlite3changeset_apply] +**
    sqlite3changeset_apply_strm_v2[sqlite3changeset_apply_v2] +**
    sqlite3changeset_concat_strm[sqlite3changeset_concat] +**
    sqlite3changeset_invert_strm[sqlite3changeset_invert] +**
    sqlite3changeset_start_strm[sqlite3changeset_start] +**
    sqlite3session_changeset_strm[sqlite3session_changeset] +**
    sqlite3session_patchset_strm[sqlite3session_patchset] +**
    +** +** Non-streaming functions that accept changesets (or patchsets) as input +** require that the entire changeset be stored in a single buffer in memory. +** Similarly, those that return a changeset or patchset do so by returning +** a pointer to a single large buffer allocated using sqlite3_malloc(). +** Normally this is convenient. However, if an application running in a +** low-memory environment is required to handle very large changesets, the +** large contiguous memory allocations required can become onerous. +** +** In order to avoid this problem, instead of a single large buffer, input +** is passed to a streaming API functions by way of a callback function that +** the sessions module invokes to incrementally request input data as it is +** required. In all cases, a pair of API function parameters such as +** +**
    +**        int nChangeset,
    +**        void *pChangeset,
    +**  
    +** +** Is replaced by: +** +**
    +**        int (*xInput)(void *pIn, void *pData, int *pnData),
    +**        void *pIn,
    +**  
    +** +** Each time the xInput callback is invoked by the sessions module, the first +** argument passed is a copy of the supplied pIn context pointer. The second +** argument, pData, points to a buffer (*pnData) bytes in size. Assuming no +** error occurs the xInput method should copy up to (*pnData) bytes of data +** into the buffer and set (*pnData) to the actual number of bytes copied +** before returning SQLITE_OK. If the input is completely exhausted, (*pnData) +** should be set to zero to indicate this. Or, if an error occurs, an SQLite +** error code should be returned. In all cases, if an xInput callback returns +** an error, all processing is abandoned and the streaming API function +** returns a copy of the error code to the caller. +** +** In the case of sqlite3changeset_start_strm(), the xInput callback may be +** invoked by the sessions module at any point during the lifetime of the +** iterator. If such an xInput callback returns an error, the iterator enters +** an error state, whereby all subsequent calls to iterator functions +** immediately fail with the same error code as returned by xInput. +** +** Similarly, streaming API functions that return changesets (or patchsets) +** return them in chunks by way of a callback function instead of via a +** pointer to a single large buffer. In this case, a pair of parameters such +** as: +** +**
    +**        int *pnChangeset,
    +**        void **ppChangeset,
    +**  
    +** +** Is replaced by: +** +**
    +**        int (*xOutput)(void *pOut, const void *pData, int nData),
    +**        void *pOut
    +**  
    +** +** The xOutput callback is invoked zero or more times to return data to +** the application. The first parameter passed to each call is a copy of the +** pOut pointer supplied by the application. The second parameter, pData, +** points to a buffer nData bytes in size containing the chunk of output +** data being returned. If the xOutput callback successfully processes the +** supplied data, it should return SQLITE_OK to indicate success. Otherwise, +** it should return some other SQLite error code. In this case processing +** is immediately abandoned and the streaming API function returns a copy +** of the xOutput error code to the application. +** +** The sessions module never invokes an xOutput callback with the third +** parameter set to a value less than or equal to zero. Other than this, +** no guarantees are made as to the size of the chunks of data returned. +*/ +int sqlite3changeset_apply_strm( + sqlite3 *db, /* Apply change to "main" db of this handle */ + int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */ + void *pIn, /* First arg for xInput */ + int(*xFilter)( + void *pCtx, /* Copy of sixth arg to _apply() */ + const char *zTab /* Table name */ + ), + int(*xConflict)( + void *pCtx, /* Copy of sixth arg to _apply() */ + int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */ + sqlite3_changeset_iter *p /* Handle describing change and conflict */ + ), + void *pCtx /* First argument passed to xConflict */ +); +int sqlite3changeset_apply_v2_strm( + sqlite3 *db, /* Apply change to "main" db of this handle */ + int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */ + void *pIn, /* First arg for xInput */ + int(*xFilter)( + void *pCtx, /* Copy of sixth arg to _apply() */ + const char *zTab /* Table name */ + ), + int(*xConflict)( + void *pCtx, /* Copy of sixth arg to _apply() */ + int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */ + sqlite3_changeset_iter *p /* Handle describing change and conflict */ + ), + void *pCtx, /* First argument passed to xConflict */ + void **ppRebase, int *pnRebase, + int flags +); +int sqlite3changeset_apply_v3_strm( + sqlite3 *db, /* Apply change to "main" db of this handle */ + int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */ + void *pIn, /* First arg for xInput */ + int(*xFilter)( + void *pCtx, /* Copy of sixth arg to _apply() */ + sqlite3_changeset_iter *p + ), + int(*xConflict)( + void *pCtx, /* Copy of sixth arg to _apply() */ + int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */ + sqlite3_changeset_iter *p /* Handle describing change and conflict */ + ), + void *pCtx, /* First argument passed to xConflict */ + void **ppRebase, int *pnRebase, + int flags +); +int sqlite3changeset_concat_strm( + int (*xInputA)(void *pIn, void *pData, int *pnData), + void *pInA, + int (*xInputB)(void *pIn, void *pData, int *pnData), + void *pInB, + int (*xOutput)(void *pOut, const void *pData, int nData), + void *pOut +); +int sqlite3changeset_invert_strm( + int (*xInput)(void *pIn, void *pData, int *pnData), + void *pIn, + int (*xOutput)(void *pOut, const void *pData, int nData), + void *pOut +); +int sqlite3changeset_start_strm( + sqlite3_changeset_iter **pp, + int (*xInput)(void *pIn, void *pData, int *pnData), + void *pIn +); +int sqlite3changeset_start_v2_strm( + sqlite3_changeset_iter **pp, + int (*xInput)(void *pIn, void *pData, int *pnData), + void *pIn, + int flags +); +int sqlite3session_changeset_strm( + sqlite3_session *pSession, + int (*xOutput)(void *pOut, const void *pData, int nData), + void *pOut +); +int sqlite3session_patchset_strm( + sqlite3_session *pSession, + int (*xOutput)(void *pOut, const void *pData, int nData), + void *pOut +); +int sqlite3changegroup_add_strm(sqlite3_changegroup*, + int (*xInput)(void *pIn, void *pData, int *pnData), + void *pIn +); +int sqlite3changegroup_output_strm(sqlite3_changegroup*, + int (*xOutput)(void *pOut, const void *pData, int nData), + void *pOut +); +int sqlite3rebaser_rebase_strm( + sqlite3_rebaser *pRebaser, + int (*xInput)(void *pIn, void *pData, int *pnData), + void *pIn, + int (*xOutput)(void *pOut, const void *pData, int nData), + void *pOut +); + +/* +** CAPI3REF: Configure global parameters +** +** The sqlite3session_config() interface is used to make global configuration +** changes to the sessions module in order to tune it to the specific needs +** of the application. +** +** The sqlite3session_config() interface is not threadsafe. If it is invoked +** while any other thread is inside any other sessions method then the +** results are undefined. Furthermore, if it is invoked after any sessions +** related objects have been created, the results are also undefined. +** +** The first argument to the sqlite3session_config() function must be one +** of the SQLITE_SESSION_CONFIG_XXX constants defined below. The +** interpretation of the (void*) value passed as the second parameter and +** the effect of calling this function depends on the value of the first +** parameter. +** +**
    +**
    SQLITE_SESSION_CONFIG_STRMSIZE
    +** By default, the sessions module streaming interfaces attempt to input +** and output data in approximately 1 KiB chunks. This operand may be used +** to set and query the value of this configuration setting. The pointer +** passed as the second argument must point to a value of type (int). +** If this value is greater than 0, it is used as the new streaming data +** chunk size for both input and output. Before returning, the (int) value +** pointed to by pArg is set to the final value of the streaming interface +** chunk size. +**
    +** +** This function returns SQLITE_OK if successful, or an SQLite error code +** otherwise. +*/ +int sqlite3session_config(int op, void *pArg); + +/* +** CAPI3REF: Values for sqlite3session_config(). +*/ +#define SQLITE_SESSION_CONFIG_STRMSIZE 1 + +/* +** Make sure we can call this stuff from C++. +*/ +#ifdef __cplusplus +} +#endif + +#endif /* !defined(__SQLITESESSION_H_) && defined(SQLITE_ENABLE_SESSION) */ diff --git a/ext/session/test_session.c b/ext/session/test_session.c new file mode 100644 index 0000000000..6ad5b37749 --- /dev/null +++ b/ext/session/test_session.c @@ -0,0 +1,1828 @@ + +#if defined(SQLITE_TEST) && defined(SQLITE_ENABLE_SESSION) \ + && defined(SQLITE_ENABLE_PREUPDATE_HOOK) + +#include "sqlite3session.h" +#include +#include +#include "tclsqlite.h" + +#ifndef SQLITE_AMALGAMATION + typedef unsigned char u8; +#endif + +typedef struct TestSession TestSession; +struct TestSession { + sqlite3_session *pSession; + Tcl_Interp *interp; + Tcl_Obj *pFilterScript; +}; + +typedef struct TestStreamInput TestStreamInput; +struct TestStreamInput { + int nStream; /* Maximum chunk size */ + unsigned char *aData; /* Pointer to buffer containing data */ + int nData; /* Size of buffer aData in bytes */ + int iData; /* Bytes of data already read by sessions */ +}; + +/* +** Extract an sqlite3* db handle from the object passed as the second +** argument. If successful, set *pDb to point to the db handle and return +** TCL_OK. Otherwise, return TCL_ERROR. +*/ +static int dbHandleFromObj(Tcl_Interp *interp, Tcl_Obj *pObj, sqlite3 **pDb){ + Tcl_CmdInfo info; + if( 0==Tcl_GetCommandInfo(interp, Tcl_GetString(pObj), &info) ){ + Tcl_AppendResult(interp, "no such handle: ", Tcl_GetString(pObj), NULL); + return TCL_ERROR; + } + + *pDb = *(sqlite3 **)info.objClientData; + return TCL_OK; +} + +/************************************************************************* +** The following code is copied byte-for-byte from the sessions module +** documentation. It is used by some of the sessions modules tests to +** ensure that the example in the documentation does actually work. +*/ +/* +** Argument zSql points to a buffer containing an SQL script to execute +** against the database handle passed as the first argument. As well as +** executing the SQL script, this function collects a changeset recording +** all changes made to the "main" database file. Assuming no error occurs, +** output variables (*ppChangeset) and (*pnChangeset) are set to point +** to a buffer containing the changeset and the size of the changeset in +** bytes before returning SQLITE_OK. In this case it is the responsibility +** of the caller to eventually free the changeset blob by passing it to +** the sqlite3_free function. +** +** Or, if an error does occur, return an SQLite error code. The final +** value of (*pChangeset) and (*pnChangeset) are undefined in this case. +*/ +int sql_exec_changeset( + sqlite3 *db, /* Database handle */ + const char *zSql, /* SQL script to execute */ + int *pnChangeset, /* OUT: Size of changeset blob in bytes */ + void **ppChangeset /* OUT: Pointer to changeset blob */ +){ + sqlite3_session *pSession = 0; + int rc; + int val = 1; + + /* Create a new session object */ + rc = sqlite3session_create(db, "main", &pSession); + sqlite3session_object_config(pSession, SQLITE_SESSION_OBJCONFIG_ROWID, &val); + + /* Configure the session object to record changes to all tables */ + if( rc==SQLITE_OK ) rc = sqlite3session_attach(pSession, NULL); + + /* Execute the SQL script */ + if( rc==SQLITE_OK ) rc = sqlite3_exec(db, zSql, 0, 0, 0); + + /* Collect the changeset */ + if( rc==SQLITE_OK ){ + rc = sqlite3session_changeset(pSession, pnChangeset, ppChangeset); + } + + /* Delete the session object */ + sqlite3session_delete(pSession); + + return rc; +} +/************************************************************************/ + + +#ifdef SQLITE_DEBUG +static int sqlite3_test_changeset(int, void *, char **); +static void assert_changeset_is_ok(int n, void *p){ + char *z = 0; + (void)sqlite3_test_changeset(n, p, &z); + assert( z==0 ); +} +#else +# define assert_changeset_is_ok(n,p) +#endif + +/* +** Tclcmd: sql_exec_changeset DB SQL +*/ +static int SQLITE_TCLAPI test_sql_exec_changeset( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + const char *zSql; + sqlite3 *db; + void *pChangeset; + int nChangeset; + int rc; + + if( objc!=3 ){ + Tcl_WrongNumArgs(interp, 1, objv, "DB SQL"); + return TCL_ERROR; + } + if( dbHandleFromObj(interp, objv[1], &db) ) return TCL_ERROR; + zSql = (const char*)Tcl_GetString(objv[2]); + + rc = sql_exec_changeset(db, zSql, &nChangeset, &pChangeset); + if( rc!=SQLITE_OK ){ + Tcl_ResetResult(interp); + Tcl_AppendResult(interp, "error in sql_exec_changeset()", NULL); + return TCL_ERROR; + } + + assert_changeset_is_ok(nChangeset, pChangeset); + Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(pChangeset, nChangeset)); + sqlite3_free(pChangeset); + return TCL_OK; +} + + + +#define SESSION_STREAM_TCL_VAR "sqlite3session_streams" + +/* +** Attempt to find the global variable zVar within interpreter interp +** and extract an integer value from it. Return this value. +** +** If the named variable cannot be found, or if it cannot be interpreted +** as a integer, return 0. +*/ +static int test_tcl_integer(Tcl_Interp *interp, const char *zVar){ + Tcl_Obj *pObj; + int iVal = 0; + Tcl_Obj *pName = Tcl_NewStringObj(zVar, -1); + Tcl_IncrRefCount(pName); + pObj = Tcl_ObjGetVar2(interp, pName, 0, TCL_GLOBAL_ONLY); + Tcl_DecrRefCount(pName); + if( pObj ) Tcl_GetIntFromObj(0, pObj, &iVal); + return iVal; +} + +static int test_session_error(Tcl_Interp *interp, int rc, char *zErr){ + extern const char *sqlite3ErrName(int); + Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1)); + if( zErr ){ + Tcl_AppendResult(interp, " - ", zErr, NULL); + sqlite3_free(zErr); + } + return TCL_ERROR; +} + +static int test_table_filter(void *pCtx, const char *zTbl){ + TestSession *p = (TestSession*)pCtx; + Tcl_Obj *pEval; + int rc; + int bRes = 0; + + pEval = Tcl_DuplicateObj(p->pFilterScript); + Tcl_IncrRefCount(pEval); + rc = Tcl_ListObjAppendElement(p->interp, pEval, Tcl_NewStringObj(zTbl, -1)); + if( rc==TCL_OK ){ + rc = Tcl_EvalObjEx(p->interp, pEval, TCL_EVAL_GLOBAL); + } + if( rc==TCL_OK ){ + rc = Tcl_GetBooleanFromObj(p->interp, Tcl_GetObjResult(p->interp), &bRes); + } + if( rc!=TCL_OK ){ + /* printf("error: %s\n", Tcl_GetStringResult(p->interp)); */ + Tcl_BackgroundError(p->interp); + } + Tcl_DecrRefCount(pEval); + + return bRes; +} + +struct TestSessionsBlob { + void *p; + int n; +}; +typedef struct TestSessionsBlob TestSessionsBlob; + +static int testStreamOutput( + void *pCtx, + const void *pData, + int nData +){ + TestSessionsBlob *pBlob = (TestSessionsBlob*)pCtx; + char *pNew; + + assert( nData>0 ); + pNew = (char*)sqlite3_realloc(pBlob->p, pBlob->n + nData); + if( pNew==0 ){ + return SQLITE_NOMEM; + } + pBlob->p = (void*)pNew; + memcpy(&pNew[pBlob->n], pData, nData); + pBlob->n += nData; + return SQLITE_OK; +} + +/* +** Tclcmd: $session attach TABLE +** $session changeset +** $session delete +** $session enable BOOL +** $session indirect INTEGER +** $session patchset +** $session table_filter SCRIPT +*/ +static int SQLITE_TCLAPI test_session_cmd( + void *clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + TestSession *p = (TestSession*)clientData; + sqlite3_session *pSession = p->pSession; + static struct SessionSubcmd { + const char *zSub; + int nArg; + const char *zMsg; + int iSub; + } aSub[] = { + { "attach", 1, "TABLE", }, /* 0 */ + { "changeset", 0, "", }, /* 1 */ + { "delete", 0, "", }, /* 2 */ + { "enable", 1, "BOOL", }, /* 3 */ + { "indirect", 1, "BOOL", }, /* 4 */ + { "isempty", 0, "", }, /* 5 */ + { "table_filter", 1, "SCRIPT", }, /* 6 */ + { "patchset", 0, "", }, /* 7 */ + { "diff", 2, "FROMDB TBL", }, /* 8 */ + { "memory_used", 0, "", }, /* 9 */ + { "changeset_size", 0, "", }, /* 10 */ + { "object_config", 2, "OPTION INTEGER", }, /* 11 */ + { 0 } + }; + int iSub; + int rc; + + if( objc<2 ){ + Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ..."); + return TCL_ERROR; + } + rc = Tcl_GetIndexFromObjStruct(interp, + objv[1], aSub, sizeof(aSub[0]), "sub-command", 0, &iSub + ); + if( rc!=TCL_OK ) return rc; + if( objc!=2+aSub[iSub].nArg ){ + Tcl_WrongNumArgs(interp, 2, objv, aSub[iSub].zMsg); + return TCL_ERROR; + } + + switch( iSub ){ + case 0: { /* attach */ + char *zArg = Tcl_GetString(objv[2]); + if( zArg[0]=='*' && zArg[1]=='\0' ) zArg = 0; + rc = sqlite3session_attach(pSession, zArg); + if( rc!=SQLITE_OK ){ + return test_session_error(interp, rc, 0); + } + break; + } + + case 7: /* patchset */ + case 1: { /* changeset */ + TestSessionsBlob o = {0, 0}; + if( test_tcl_integer(interp, SESSION_STREAM_TCL_VAR) ){ + void *pCtx = (void*)&o; + if( iSub==7 ){ + rc = sqlite3session_patchset_strm(pSession, testStreamOutput, pCtx); + }else{ + rc = sqlite3session_changeset_strm(pSession, testStreamOutput, pCtx); + } + }else{ + if( iSub==7 ){ + rc = sqlite3session_patchset(pSession, &o.n, &o.p); + }else{ + rc = sqlite3session_changeset(pSession, &o.n, &o.p); + } + } + if( rc==SQLITE_OK ){ + assert_changeset_is_ok(o.n, o.p); + Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(o.p, o.n)); + } + sqlite3_free(o.p); + if( rc!=SQLITE_OK ){ + return test_session_error(interp, rc, 0); + } + break; + } + + case 2: /* delete */ + Tcl_DeleteCommand(interp, Tcl_GetString(objv[0])); + break; + + case 3: { /* enable */ + int val; + if( Tcl_GetIntFromObj(interp, objv[2], &val) ) return TCL_ERROR; + val = sqlite3session_enable(pSession, val); + Tcl_SetObjResult(interp, Tcl_NewBooleanObj(val)); + break; + } + + case 4: { /* indirect */ + int val; + if( Tcl_GetIntFromObj(interp, objv[2], &val) ) return TCL_ERROR; + val = sqlite3session_indirect(pSession, val); + Tcl_SetObjResult(interp, Tcl_NewBooleanObj(val)); + break; + } + + case 5: { /* isempty */ + int val; + val = sqlite3session_isempty(pSession); + Tcl_SetObjResult(interp, Tcl_NewBooleanObj(val)); + break; + } + + case 6: { /* table_filter */ + if( p->pFilterScript ) Tcl_DecrRefCount(p->pFilterScript); + p->interp = interp; + p->pFilterScript = Tcl_DuplicateObj(objv[2]); + Tcl_IncrRefCount(p->pFilterScript); + sqlite3session_table_filter(pSession, test_table_filter, clientData); + break; + } + + case 8: { /* diff */ + char *zErr = 0; + rc = sqlite3session_diff(pSession, + Tcl_GetString(objv[2]), + Tcl_GetString(objv[3]), + &zErr + ); + assert( rc!=SQLITE_OK || zErr==0 ); + if( rc ){ + return test_session_error(interp, rc, zErr); + } + break; + } + + case 9: { /* memory_used */ + sqlite3_int64 nMalloc = sqlite3session_memory_used(pSession); + Tcl_SetObjResult(interp, Tcl_NewWideIntObj(nMalloc)); + break; + } + + case 10: { + sqlite3_int64 nSize = sqlite3session_changeset_size(pSession); + Tcl_SetObjResult(interp, Tcl_NewWideIntObj(nSize)); + break; + } + case 11: { /* object_config */ + struct ObjConfOpt { + const char *zName; + int opt; + } aOpt[] = { + { "size", SQLITE_SESSION_OBJCONFIG_SIZE }, + { "rowid", SQLITE_SESSION_OBJCONFIG_ROWID }, + { 0, 0 } + }; + int sz = (int)sizeof(aOpt[0]); + + int iArg; + Tcl_Size iOpt; + if( Tcl_GetIndexFromObjStruct(interp,objv[2],aOpt,sz,"option",0,&iOpt) ){ + return TCL_ERROR; + } + if( Tcl_GetIntFromObj(interp, objv[3], &iArg) ){ + return TCL_ERROR; + } + rc = sqlite3session_object_config(pSession, aOpt[iOpt].opt, &iArg); + if( rc!=SQLITE_OK ){ + extern const char *sqlite3ErrName(int); + Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1)); + }else{ + Tcl_SetObjResult(interp, Tcl_NewIntObj(iArg)); + } + break; + } + } + + return TCL_OK; +} + +static void SQLITE_TCLAPI test_session_del(void *clientData){ + TestSession *p = (TestSession*)clientData; + if( p->pFilterScript ) Tcl_DecrRefCount(p->pFilterScript); + sqlite3session_delete(p->pSession); + ckfree((char*)p); +} + +/* +** Tclcmd: sqlite3session CMD DB-HANDLE DB-NAME +*/ +static int SQLITE_TCLAPI test_sqlite3session( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + sqlite3 *db; + Tcl_CmdInfo info; + int rc; /* sqlite3session_create() return code */ + TestSession *p; /* New wrapper object */ + int iArg = -1; + + if( objc!=4 ){ + Tcl_WrongNumArgs(interp, 1, objv, "CMD DB-HANDLE DB-NAME"); + return TCL_ERROR; + } + + if( 0==Tcl_GetCommandInfo(interp, Tcl_GetString(objv[2]), &info) ){ + Tcl_AppendResult(interp, "no such handle: ", Tcl_GetString(objv[2]), NULL); + return TCL_ERROR; + } + db = *(sqlite3 **)info.objClientData; + + p = (TestSession*)ckalloc(sizeof(TestSession)); + memset(p, 0, sizeof(TestSession)); + rc = sqlite3session_create(db, Tcl_GetString(objv[3]), &p->pSession); + if( rc!=SQLITE_OK ){ + ckfree((char*)p); + return test_session_error(interp, rc, 0); + } + + /* Query the SQLITE_SESSION_OBJCONFIG_SIZE option to ensure that it + ** is clear by default. Then set it. */ + sqlite3session_object_config(p->pSession,SQLITE_SESSION_OBJCONFIG_SIZE,&iArg); + assert( iArg==0 ); + iArg = 1; + sqlite3session_object_config(p->pSession,SQLITE_SESSION_OBJCONFIG_SIZE,&iArg); + + Tcl_CreateObjCommand( + interp, Tcl_GetString(objv[1]), test_session_cmd, (ClientData)p, + test_session_del + ); + Tcl_SetObjResult(interp, objv[1]); + return TCL_OK; +} + +static void test_append_value(Tcl_Obj *pList, sqlite3_value *pVal){ + if( pVal==0 ){ + Tcl_ListObjAppendElement(0, pList, Tcl_NewObj()); + Tcl_ListObjAppendElement(0, pList, Tcl_NewObj()); + }else{ + Tcl_Obj *pObj; + switch( sqlite3_value_type(pVal) ){ + case SQLITE_NULL: + Tcl_ListObjAppendElement(0, pList, Tcl_NewStringObj("n", 1)); + pObj = Tcl_NewObj(); + break; + case SQLITE_INTEGER: + Tcl_ListObjAppendElement(0, pList, Tcl_NewStringObj("i", 1)); + pObj = Tcl_NewWideIntObj(sqlite3_value_int64(pVal)); + break; + case SQLITE_FLOAT: + Tcl_ListObjAppendElement(0, pList, Tcl_NewStringObj("f", 1)); + pObj = Tcl_NewDoubleObj(sqlite3_value_double(pVal)); + break; + case SQLITE_TEXT: { + const char *z = (char*)sqlite3_value_blob(pVal); + int n = sqlite3_value_bytes(pVal); + Tcl_ListObjAppendElement(0, pList, Tcl_NewStringObj("t", 1)); + pObj = Tcl_NewStringObj(z, n); + break; + } + default: + assert( sqlite3_value_type(pVal)==SQLITE_BLOB ); + Tcl_ListObjAppendElement(0, pList, Tcl_NewStringObj("b", 1)); + pObj = Tcl_NewByteArrayObj( + sqlite3_value_blob(pVal), + sqlite3_value_bytes(pVal) + ); + break; + } + Tcl_ListObjAppendElement(0, pList, pObj); + } +} + +typedef struct TestConflictHandler TestConflictHandler; +struct TestConflictHandler { + Tcl_Interp *interp; + Tcl_Obj *pConflictScript; + Tcl_Obj *pFilterScript; +}; + +static int test_obj_eq_string(Tcl_Obj *p, const char *z){ + Tcl_Size n; + Tcl_Size nObj; + char *zObj; + + n = (Tcl_Size)strlen(z); + zObj = Tcl_GetStringFromObj(p, &nObj); + + return (nObj==n && (n==0 || 0==memcmp(zObj, z, n))); +} + +static Tcl_Obj *testIterData(sqlite3_changeset_iter *pIter){ + Tcl_Obj *pVar = 0; + int nCol; /* Number of columns in table */ + int nCol2; /* Number of columns in table */ + int op; /* SQLITE_INSERT, UPDATE or DELETE */ + const char *zTab; /* Name of table change applies to */ + Tcl_Obj *pOld; /* Vector of old.* values */ + Tcl_Obj *pNew; /* Vector of new.* values */ + int bIndirect; + + char *zPK; + unsigned char *abPK; + int i; + + sqlite3changeset_op(pIter, &zTab, &nCol, &op, &bIndirect); + pVar = Tcl_NewObj(); + + Tcl_ListObjAppendElement(0, pVar, Tcl_NewStringObj( + op==SQLITE_INSERT ? "INSERT" : + op==SQLITE_UPDATE ? "UPDATE" : + "DELETE", -1 + )); + + Tcl_ListObjAppendElement(0, pVar, Tcl_NewStringObj(zTab, -1)); + Tcl_ListObjAppendElement(0, pVar, Tcl_NewBooleanObj(bIndirect)); + + zPK = ckalloc(nCol+1); + memset(zPK, 0, nCol+1); + sqlite3changeset_pk(pIter, &abPK, &nCol2); + assert( nCol==nCol2 ); + for(i=0; iinterp; + + pEval = Tcl_DuplicateObj(p->pFilterScript); + Tcl_IncrRefCount(pEval); + + if( TCL_OK!=Tcl_ListObjAppendElement(0, pEval, Tcl_NewStringObj(zTab, -1)) + || TCL_OK!=Tcl_EvalObjEx(interp, pEval, TCL_EVAL_GLOBAL) + || TCL_OK!=Tcl_GetIntFromObj(interp, Tcl_GetObjResult(interp), &res) + ){ + Tcl_BackgroundError(interp); + } + + Tcl_DecrRefCount(pEval); + return res; +} + +static int test_filter_v3_handler( + void *pCtx, /* Pointer to TestConflictHandler structure */ + sqlite3_changeset_iter *pIter +){ + TestConflictHandler *p = (TestConflictHandler *)pCtx; + int res = 1; + Tcl_Obj *pEval = 0; + Tcl_Interp *interp = p->interp; + + pEval = Tcl_DuplicateObj(p->pFilterScript); + Tcl_IncrRefCount(pEval); + Tcl_ListObjAppendElement(0, pEval, testIterData(pIter)); + + if( TCL_OK!=Tcl_EvalObjEx(interp, pEval, TCL_EVAL_GLOBAL) + || TCL_OK!=Tcl_GetIntFromObj(interp, Tcl_GetObjResult(interp), &res) + ){ + Tcl_BackgroundError(interp); + } + + Tcl_DecrRefCount(pEval); + return res; +} + +static int test_conflict_handler( + void *pCtx, /* Pointer to TestConflictHandler structure */ + int eConf, /* DATA, MISSING, CONFLICT, CONSTRAINT */ + sqlite3_changeset_iter *pIter /* Handle describing change and conflict */ +){ + TestConflictHandler *p = (TestConflictHandler *)pCtx; + Tcl_Obj *pEval; + Tcl_Interp *interp = p->interp; + int ret = 0; /* Return value */ + + int op; /* SQLITE_UPDATE, DELETE or INSERT */ + const char *zTab; /* Name of table conflict is on */ + int nCol; /* Number of columns in table zTab */ + + pEval = Tcl_DuplicateObj(p->pConflictScript); + Tcl_IncrRefCount(pEval); + + sqlite3changeset_op(pIter, &zTab, &nCol, &op, 0); + + if( eConf==SQLITE_CHANGESET_FOREIGN_KEY ){ + int nFk; + sqlite3changeset_fk_conflicts(pIter, &nFk); + Tcl_ListObjAppendElement(0, pEval, Tcl_NewStringObj("FOREIGN_KEY", -1)); + Tcl_ListObjAppendElement(0, pEval, Tcl_NewIntObj(nFk)); + }else{ + + /* Append the operation type. */ + Tcl_ListObjAppendElement(0, pEval, Tcl_NewStringObj( + op==SQLITE_INSERT ? "INSERT" : + op==SQLITE_UPDATE ? "UPDATE" : + "DELETE", -1 + )); + + /* Append the table name. */ + Tcl_ListObjAppendElement(0, pEval, Tcl_NewStringObj(zTab, -1)); + + /* Append the conflict type. */ + switch( eConf ){ + case SQLITE_CHANGESET_DATA: + Tcl_ListObjAppendElement(interp, pEval,Tcl_NewStringObj("DATA",-1)); + break; + case SQLITE_CHANGESET_NOTFOUND: + Tcl_ListObjAppendElement(interp, pEval,Tcl_NewStringObj("NOTFOUND",-1)); + break; + case SQLITE_CHANGESET_CONFLICT: + Tcl_ListObjAppendElement(interp, pEval,Tcl_NewStringObj("CONFLICT",-1)); + break; + case SQLITE_CHANGESET_CONSTRAINT: + Tcl_ListObjAppendElement(interp, pEval,Tcl_NewStringObj("CONSTRAINT",-1)); + break; + } + + /* If this is not an INSERT, append the old row */ + if( op!=SQLITE_INSERT ){ + int i; + Tcl_Obj *pOld = Tcl_NewObj(); + for(i=0; inData - p->iData; /* Bytes of data available */ + int nRet = p->nStream; /* Bytes actually returned */ + + /* Allocate and free some space. There is no point to this, other than + ** that it allows the regular OOM fault-injection tests to cause an error + ** in this function. */ + void *pAlloc = sqlite3_malloc(10); + if( pAlloc==0 ) return SQLITE_NOMEM; + sqlite3_free(pAlloc); + + if( nRet>nReq ) nRet = nReq; + if( nRet>nRem ) nRet = nRem; + + assert( nRet>=0 ); + if( nRet>0 ){ + memcpy(pData, &p->aData[p->iData], nRet); + p->iData += nRet; + } + + *pnData = nRet; + return SQLITE_OK; +} + + +static int SQLITE_TCLAPI testSqlite3changesetApply( + int iVersion, + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + sqlite3 *db; /* Database handle */ + Tcl_CmdInfo info; /* Database Tcl command (objv[1]) info */ + int rc; /* Return code from changeset_invert() */ + void *pChangeset; /* Buffer containing changeset */ + Tcl_Size nChangeset; /* Size of buffer aChangeset in bytes */ + TestConflictHandler ctx; + TestStreamInput sStr; + void *pRebase = 0; + int nRebase = 0; + int flags = 0; /* Flags for apply_v2() */ + + assert( iVersion==1 || iVersion==2 || iVersion==3 ); + + memset(&sStr, 0, sizeof(sStr)); + sStr.nStream = test_tcl_integer(interp, SESSION_STREAM_TCL_VAR); + + /* Check for the -nosavepoint, -invert or -ignorenoop switches */ + if( iVersion==2 || iVersion==3 ){ + while( objc>1 ){ + const char *z1 = Tcl_GetString(objv[1]); + int n = (int)strlen(z1); + if( n>3 && n<=12 && 0==sqlite3_strnicmp("-nosavepoint", z1, n) ){ + flags |= SQLITE_CHANGESETAPPLY_NOSAVEPOINT; + } + else if( n>3 && n<=9 && 0==sqlite3_strnicmp("-noaction", z1, n) ){ + flags |= SQLITE_CHANGESETAPPLY_FKNOACTION; + } + else if( n>2 && n<=7 && 0==sqlite3_strnicmp("-invert", z1, n) ){ + flags |= SQLITE_CHANGESETAPPLY_INVERT; + } + else if( n>2 && n<=11 && 0==sqlite3_strnicmp("-ignorenoop", z1, n) ){ + flags |= SQLITE_CHANGESETAPPLY_IGNORENOOP; + }else{ + break; + } + objc--; + objv++; + } + } + + if( objc!=4 && objc!=5 ){ + const char *zMsg; + if( iVersion==2 || iVersion==3 ){ + zMsg = "?-nosavepoint? ?-inverse? ?-ignorenoop? " + "DB CHANGESET CONFLICT-SCRIPT ?FILTER-SCRIPT?"; + }else{ + zMsg = "DB CHANGESET CONFLICT-SCRIPT ?FILTER-SCRIPT?"; + } + Tcl_WrongNumArgs(interp, 1, objv, zMsg); + return TCL_ERROR; + } + if( 0==Tcl_GetCommandInfo(interp, Tcl_GetString(objv[1]), &info) ){ + Tcl_AppendResult(interp, "no such handle: ", Tcl_GetString(objv[1]), NULL); + return TCL_ERROR; + } + db = *(sqlite3 **)info.objClientData; + pChangeset = (void *)Tcl_GetByteArrayFromObj(objv[2], &nChangeset); + ctx.pConflictScript = objv[3]; + ctx.pFilterScript = objc==5 ? objv[4] : 0; + ctx.interp = interp; + + if( sStr.nStream==0 ){ + switch( iVersion ){ + case 1: + rc = sqlite3changeset_apply(db, (int)nChangeset, pChangeset, + (objc==5)?test_filter_handler:0, test_conflict_handler, (void*)&ctx + ); + break; + case 2: + rc = sqlite3changeset_apply_v2(db, (int)nChangeset, pChangeset, + (objc==5)?test_filter_handler:0, test_conflict_handler, (void*)&ctx, + &pRebase, &nRebase, flags + ); + break; + case 3: + rc = sqlite3changeset_apply_v3(db, (int)nChangeset, pChangeset, + (objc==5)?test_filter_v3_handler:0, test_conflict_handler, + (void*)&ctx, &pRebase, &nRebase, flags + ); + break; + } + }else{ + sStr.aData = (unsigned char*)pChangeset; + sStr.nData = (int)nChangeset; + switch( iVersion ){ + case 1: + rc = sqlite3changeset_apply_strm(db, testStreamInput, (void*)&sStr, + (objc==5) ? test_filter_handler : 0, + test_conflict_handler, (void *)&ctx + ); + break; + case 2: + rc = sqlite3changeset_apply_v2_strm(db, testStreamInput, (void*)&sStr, + (objc==5) ? test_filter_handler : 0, + test_conflict_handler, (void *)&ctx, + &pRebase, &nRebase, flags + ); + break; + case 3: + rc = sqlite3changeset_apply_v3_strm(db, testStreamInput, (void*)&sStr, + (objc==5) ? test_filter_v3_handler : 0, + test_conflict_handler, (void *)&ctx, + &pRebase, &nRebase, flags + ); + break; + } + } + + if( rc!=SQLITE_OK ){ + return test_session_error(interp, rc, 0); + }else{ + Tcl_ResetResult(interp); + if( (iVersion==2 || iVersion==3) && pRebase ){ + Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(pRebase, nRebase)); + } + } + sqlite3_free(pRebase); + return TCL_OK; +} + +/* +** sqlite3changeset_apply DB CHANGESET CONFLICT-SCRIPT ?FILTER-SCRIPT? +*/ +static int SQLITE_TCLAPI test_sqlite3changeset_apply( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + return testSqlite3changesetApply(1, clientData, interp, objc, objv); +} +/* +** sqlite3changeset_apply_v2 DB CHANGESET CONFLICT-SCRIPT ?FILTER-SCRIPT? +*/ +static int SQLITE_TCLAPI test_sqlite3changeset_apply_v2( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + return testSqlite3changesetApply(2, clientData, interp, objc, objv); +} +/* +** sqlite3changeset_apply_v3 DB CHANGESET CONFLICT-SCRIPT ?FILTER-SCRIPT? +*/ +static int SQLITE_TCLAPI test_sqlite3changeset_apply_v3( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + return testSqlite3changesetApply(3, clientData, interp, objc, objv); +} + +/* +** sqlite3changeset_apply_replace_all DB CHANGESET +*/ +static int SQLITE_TCLAPI test_sqlite3changeset_apply_replace_all( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + sqlite3 *db; /* Database handle */ + Tcl_CmdInfo info; /* Database Tcl command (objv[1]) info */ + int rc; /* Return code from changeset_invert() */ + void *pChangeset; /* Buffer containing changeset */ + Tcl_Size nChangeset; /* Size of buffer aChangeset in bytes */ + + if( objc!=3 ){ + Tcl_WrongNumArgs(interp, 1, objv, "DB CHANGESET"); + return TCL_ERROR; + } + if( 0==Tcl_GetCommandInfo(interp, Tcl_GetString(objv[1]), &info) ){ + Tcl_AppendResult(interp, "no such handle: ", Tcl_GetString(objv[2]), NULL); + return TCL_ERROR; + } + db = *(sqlite3 **)info.objClientData; + pChangeset = (void *)Tcl_GetByteArrayFromObj(objv[2], &nChangeset); + + rc = sqlite3changeset_apply(db, (int)nChangeset, pChangeset, + 0, replace_handler,0); + if( rc!=SQLITE_OK ){ + return test_session_error(interp, rc, 0); + } + Tcl_ResetResult(interp); + return TCL_OK; +} + + +/* +** sqlite3changeset_invert CHANGESET +*/ +static int SQLITE_TCLAPI test_sqlite3changeset_invert( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + int rc; /* Return code from changeset_invert() */ + Tcl_Size nn; + TestStreamInput sIn; /* Input stream */ + TestSessionsBlob sOut; /* Output blob */ + + if( objc!=2 ){ + Tcl_WrongNumArgs(interp, 1, objv, "CHANGESET"); + return TCL_ERROR; + } + + memset(&sIn, 0, sizeof(sIn)); + memset(&sOut, 0, sizeof(sOut)); + sIn.nStream = test_tcl_integer(interp, SESSION_STREAM_TCL_VAR); + sIn.aData = Tcl_GetByteArrayFromObj(objv[1], &nn); + sIn.nData = (int)nn; + + if( sIn.nStream ){ + rc = sqlite3changeset_invert_strm( + testStreamInput, (void*)&sIn, testStreamOutput, (void*)&sOut + ); + }else{ + rc = sqlite3changeset_invert(sIn.nData, sIn.aData, &sOut.n, &sOut.p); + } + if( rc!=SQLITE_OK ){ + rc = test_session_error(interp, rc, 0); + }else{ + assert_changeset_is_ok(sOut.n, sOut.p); + Tcl_SetObjResult(interp,Tcl_NewByteArrayObj((unsigned char*)sOut.p,sOut.n)); + } + sqlite3_free(sOut.p); + return rc; +} + +/* +** sqlite3changeset_concat LEFT RIGHT +*/ +static int SQLITE_TCLAPI test_sqlite3changeset_concat( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + int rc; /* Return code from changeset_invert() */ + Tcl_Size nn; + + TestStreamInput sLeft; /* Input stream */ + TestStreamInput sRight; /* Input stream */ + TestSessionsBlob sOut = {0,0}; /* Output blob */ + + if( objc!=3 ){ + Tcl_WrongNumArgs(interp, 1, objv, "LEFT RIGHT"); + return TCL_ERROR; + } + + memset(&sLeft, 0, sizeof(sLeft)); + memset(&sRight, 0, sizeof(sRight)); + sLeft.aData = Tcl_GetByteArrayFromObj(objv[1], &nn); + sLeft.nData = (int)nn; + sRight.aData = Tcl_GetByteArrayFromObj(objv[2], &nn); + sRight.nData = (int)nn; + sLeft.nStream = test_tcl_integer(interp, SESSION_STREAM_TCL_VAR); + sRight.nStream = sLeft.nStream; + + if( sLeft.nStream>0 ){ + rc = sqlite3changeset_concat_strm( + testStreamInput, (void*)&sLeft, + testStreamInput, (void*)&sRight, + testStreamOutput, (void*)&sOut + ); + }else{ + rc = sqlite3changeset_concat( + sLeft.nData, sLeft.aData, sRight.nData, sRight.aData, &sOut.n, &sOut.p + ); + } + + if( rc!=SQLITE_OK ){ + rc = test_session_error(interp, rc, 0); + }else{ + assert_changeset_is_ok(sOut.n, sOut.p); + Tcl_SetObjResult(interp,Tcl_NewByteArrayObj((unsigned char*)sOut.p,sOut.n)); + } + sqlite3_free(sOut.p); + return rc; +} + +/* +** sqlite3session_foreach VARNAME CHANGESET SCRIPT +*/ +static int SQLITE_TCLAPI test_sqlite3session_foreach( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + void *pChangeset; + Tcl_Size nChangeset; + sqlite3_changeset_iter *pIter; + int rc; + Tcl_Obj *pVarname; + Tcl_Obj *pCS; + Tcl_Obj *pScript; + int isCheckNext = 0; + int isInvert = 0; + + TestStreamInput sStr; + memset(&sStr, 0, sizeof(sStr)); + + while( objc>1 ){ + char *zOpt = Tcl_GetString(objv[1]); + int nOpt = (int)strlen(zOpt); + if( zOpt[0]!='-' ) break; + if( nOpt<=7 && 0==sqlite3_strnicmp(zOpt, "-invert", nOpt) ){ + isInvert = 1; + }else + if( nOpt<=5 && 0==sqlite3_strnicmp(zOpt, "-next", nOpt) ){ + isCheckNext = 1; + }else{ + break; + } + objv++; + objc--; + } + if( objc!=4 ){ + Tcl_WrongNumArgs( + interp, 1, objv, "?-next? ?-invert? VARNAME CHANGESET SCRIPT"); + return TCL_ERROR; + } + + pVarname = objv[1]; + pCS = objv[2]; + pScript = objv[3]; + + pChangeset = (void *)Tcl_GetByteArrayFromObj(pCS, &nChangeset); + sStr.nStream = test_tcl_integer(interp, SESSION_STREAM_TCL_VAR); + if( isInvert ){ + int f = SQLITE_CHANGESETSTART_INVERT; + if( sStr.nStream==0 ){ + rc = sqlite3changeset_start_v2(&pIter, (int)nChangeset, pChangeset, f); + }else{ + void *pCtx = (void*)&sStr; + sStr.aData = (unsigned char*)pChangeset; + sStr.nData = (int)nChangeset; + rc = sqlite3changeset_start_v2_strm(&pIter, testStreamInput, pCtx, f); + } + }else{ + if( sStr.nStream==0 ){ + rc = sqlite3changeset_start(&pIter, (int)nChangeset, pChangeset); + }else{ + sStr.aData = (unsigned char*)pChangeset; + sStr.nData = (int)nChangeset; + rc = sqlite3changeset_start_strm(&pIter, testStreamInput, (void*)&sStr); + } + } + if( rc!=SQLITE_OK ){ + return test_session_error(interp, rc, 0); + } + + while( SQLITE_ROW==sqlite3changeset_next(pIter) ){ + Tcl_Obj *pVar = 0; /* Tcl value to set $VARNAME to */ + pVar = testIterData(pIter); + Tcl_ObjSetVar2(interp, pVarname, 0, pVar, 0); + rc = Tcl_EvalObjEx(interp, pScript, 0); + if( rc!=TCL_OK && rc!=TCL_CONTINUE ){ + sqlite3changeset_finalize(pIter); + return rc==TCL_BREAK ? TCL_OK : rc; + } + } + + if( isCheckNext ){ + int rc2 = sqlite3changeset_next(pIter); + rc = sqlite3changeset_finalize(pIter); + assert( (rc2==SQLITE_DONE && rc==SQLITE_OK) || rc2==rc ); + }else{ + rc = sqlite3changeset_finalize(pIter); + } + if( rc!=SQLITE_OK ){ + return test_session_error(interp, rc, 0); + } + + return TCL_OK; +} + +/* +** tclcmd: CMD configure REBASE-BLOB +** tclcmd: CMD rebase CHANGESET +** tclcmd: CMD delete +*/ +static int SQLITE_TCLAPI test_rebaser_cmd( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + static struct RebaseSubcmd { + const char *zSub; + int nArg; + const char *zMsg; + int iSub; + } aSub[] = { + { "configure", 1, "REBASE-BLOB" }, /* 0 */ + { "delete", 0, "" }, /* 1 */ + { "rebase", 1, "CHANGESET" }, /* 2 */ + { 0 } + }; + + sqlite3_rebaser *p = (sqlite3_rebaser*)clientData; + int iSub; + int rc; + + if( objc<2 ){ + Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ..."); + return TCL_ERROR; + } + rc = Tcl_GetIndexFromObjStruct(interp, + objv[1], aSub, sizeof(aSub[0]), "sub-command", 0, &iSub + ); + if( rc!=TCL_OK ) return rc; + if( objc!=2+aSub[iSub].nArg ){ + Tcl_WrongNumArgs(interp, 2, objv, aSub[iSub].zMsg); + return TCL_ERROR; + } + + assert( iSub==0 || iSub==1 || iSub==2 ); + assert( rc==SQLITE_OK ); + switch( iSub ){ + case 0: { /* configure */ + Tcl_Size nRebase = 0; + unsigned char *pRebase = Tcl_GetByteArrayFromObj(objv[2], &nRebase); + rc = sqlite3rebaser_configure(p, (int)nRebase, pRebase); + break; + } + + case 1: /* delete */ + Tcl_DeleteCommand(interp, Tcl_GetString(objv[0])); + break; + + default: { /* rebase */ + TestStreamInput sStr; /* Input stream */ + TestSessionsBlob sOut; /* Output blob */ + Tcl_Size nn; + + memset(&sStr, 0, sizeof(sStr)); + memset(&sOut, 0, sizeof(sOut)); + sStr.aData = Tcl_GetByteArrayFromObj(objv[2], &nn); + sStr.nData = nn; + sStr.nStream = test_tcl_integer(interp, SESSION_STREAM_TCL_VAR); + + if( sStr.nStream ){ + rc = sqlite3rebaser_rebase_strm(p, + testStreamInput, (void*)&sStr, + testStreamOutput, (void*)&sOut + ); + }else{ + rc = sqlite3rebaser_rebase(p, sStr.nData, sStr.aData, &sOut.n, &sOut.p); + } + + if( rc==SQLITE_OK ){ + assert_changeset_is_ok(sOut.n, sOut.p); + Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(sOut.p, sOut.n)); + } + sqlite3_free(sOut.p); + break; + } + } + + if( rc!=SQLITE_OK ){ + return test_session_error(interp, rc, 0); + } + return TCL_OK; +} + +static void SQLITE_TCLAPI test_rebaser_del(void *clientData){ + sqlite3_rebaser *p = (sqlite3_rebaser*)clientData; + sqlite3rebaser_delete(p); +} + +/* +** tclcmd: sqlite3rebaser_create NAME +*/ +static int SQLITE_TCLAPI test_sqlite3rebaser_create( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + int rc; + sqlite3_rebaser *pNew = 0; + if( objc!=2 ){ + Tcl_WrongNumArgs(interp, 1, objv, "NAME"); + return SQLITE_ERROR; + } + + rc = sqlite3rebaser_create(&pNew); + if( rc!=SQLITE_OK ){ + return test_session_error(interp, rc, 0); + } + + Tcl_CreateObjCommand(interp, Tcl_GetString(objv[1]), test_rebaser_cmd, + (ClientData)pNew, test_rebaser_del + ); + Tcl_SetObjResult(interp, objv[1]); + return TCL_OK; +} + +/* +** Run some sanity checks on the changeset in nChangeset byte buffer +** pChangeset. If any fail, return a non-zero value and, optionally, +** set output variable (*pzErr) to point to a buffer containing an +** English language error message describing the problem. In this +** case it is the responsibility of the caller to free the buffer +** using sqlite3_free(). +** +** Or, if the changeset appears to be well-formed, this function +** returns SQLITE_OK and sets (*pzErr) to NULL. +*/ +static int sqlite3_test_changeset( + int nChangeset, + void *pChangeset, + char **pzErr +){ + sqlite3_changeset_iter *pIter = 0; + char *zErr = 0; + int rc = SQLITE_OK; + int bPatch = (nChangeset>0 && ((char*)pChangeset)[0]=='P'); + + rc = sqlite3changeset_start(&pIter, nChangeset, pChangeset); + if( rc==SQLITE_OK ){ + int rc2; + while( rc==SQLITE_OK && SQLITE_ROW==sqlite3changeset_next(pIter) ){ + unsigned char *aPk = 0; + int nCol = 0; + int op = 0; + const char *zTab = 0; + + sqlite3changeset_pk(pIter, &aPk, &nCol); + sqlite3changeset_op(pIter, &zTab, &nCol, &op, 0); + + if( op==SQLITE_UPDATE ){ + int iCol; + for(iCol=0; iColpGrp); + ckfree(pGrp); +} + +/* +** Tclcmd: $changegroup schema DB DBNAME +** Tclcmd: $changegroup add CHANGESET +** Tclcmd: $changegroup output +** Tclcmd: $changegroup delete +*/ +static int SQLITE_TCLAPI test_changegroup_cmd( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + TestChangegroup *p = (TestChangegroup*)clientData; + static struct ChangegroupCmd { + const char *zSub; + int nArg; + const char *zMsg; + int iSub; + } aSub[] = { + { "schema", 2, "DB DBNAME", }, /* 0 */ + { "add", 1, "CHANGESET", }, /* 1 */ + { "output", 0, "", }, /* 2 */ + { "delete", 0, "", }, /* 3 */ + { "add_change", 1, "ITERATOR", }, /* 4 */ + { 0 } + }; + int rc = TCL_OK; + int iSub = 0; + + if( objc<2 ){ + Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ..."); + return TCL_ERROR; + } + rc = Tcl_GetIndexFromObjStruct(interp, + objv[1], aSub, sizeof(aSub[0]), "sub-command", 0, &iSub + ); + if( rc!=TCL_OK ) return rc; + if( objc!=2+aSub[iSub].nArg ){ + Tcl_WrongNumArgs(interp, 2, objv, aSub[iSub].zMsg); + return TCL_ERROR; + } + + switch( iSub ){ + case 0: { /* schema */ + sqlite3 *db = 0; + const char *zDb = Tcl_GetString(objv[3]); + if( dbHandleFromObj(interp, objv[2], &db) ){ + return TCL_ERROR; + } + rc = sqlite3changegroup_schema(p->pGrp, db, zDb); + if( rc!=SQLITE_OK ) rc = test_session_error(interp, rc, 0); + break; + }; + + case 1: { /* add */ + Tcl_Size nByte = 0; + const u8 *aByte = Tcl_GetByteArrayFromObj(objv[2], &nByte); + rc = sqlite3changegroup_add(p->pGrp, (int)nByte, (void*)aByte); + if( rc!=SQLITE_OK ) rc = test_session_error(interp, rc, 0); + break; + }; + + case 2: { /* output */ + int nByte = 0; + u8 *aByte = 0; + rc = sqlite3changegroup_output(p->pGrp, &nByte, (void**)&aByte); + if( rc!=SQLITE_OK ){ + rc = test_session_error(interp, rc, 0); + }else{ + Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(aByte, nByte)); + } + sqlite3_free(aByte); + break; + }; + + case 4: { /* add_change */ + Tcl_CmdInfo cmdInfo; /* Database Tcl command (objv[2]) info */ + TestChangeIter *pIter = 0; + const char *zIter = Tcl_GetString(objv[2]); + if( 0==Tcl_GetCommandInfo(interp, zIter, &cmdInfo) ){ + Tcl_AppendResult(interp, "no such iter: ", Tcl_GetString(objv[2]), NULL); + return TCL_ERROR; + } + + pIter = (struct TestChangeIter*)cmdInfo.objClientData; + + rc = sqlite3changegroup_add_change(p->pGrp, pIter->pIter); + if( rc!=SQLITE_OK ){ + rc = test_session_error(interp, rc, 0); + } + break; + }; + + default: { /* delete */ + assert( iSub==3 ); + Tcl_DeleteCommand(interp, Tcl_GetString(objv[0])); + break; + } + } + + return rc; +} + +/* +** Tclcmd: sqlite3changegroup CMD +*/ +static int SQLITE_TCLAPI test_sqlite3changegroup( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + int rc; /* sqlite3changegroup_new() return code */ + TestChangegroup *p; /* New wrapper object */ + + if( objc!=2 ){ + Tcl_WrongNumArgs(interp, 1, objv, "CMD"); + return TCL_ERROR; + } + + p = (TestChangegroup*)ckalloc(sizeof(TestChangegroup)); + memset(p, 0, sizeof(TestChangegroup)); + rc = sqlite3changegroup_new(&p->pGrp); + if( rc!=SQLITE_OK ){ + ckfree((char*)p); + return test_session_error(interp, rc, 0); + } + + Tcl_CreateObjCommand( + interp, Tcl_GetString(objv[1]), test_changegroup_cmd, (ClientData)p, + test_changegroup_del + ); + Tcl_SetObjResult(interp, objv[1]); + return TCL_OK; +} + +extern const char *sqlite3ErrName(int); + +/* +** Destructor for Tcl iterator command object. +*/ +static void test_iter_del(void *clientData){ + TestChangeIter *p = (TestChangeIter*)clientData; + sqlite3changeset_finalize(p->pIter); + ckfree(p); +} + +static int SQLITE_TCLAPI test_iter_cmd( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + static const char *aSub[] = { + "next", /* 0 */ + "data", /* 1 */ + "finalize", /* 2 */ + 0 + }; + int iSub = 0; + + TestChangeIter *p = (TestChangeIter*)clientData; + int rc = SQLITE_OK; + + if( objc<2 ){ + Tcl_WrongNumArgs(interp, 1, objv, "CMD"); + return TCL_ERROR; + } + + if( Tcl_GetIndexFromObj(interp, objv[1], aSub, "sub-command", 0, &iSub) ){ + return TCL_ERROR; + } + switch( iSub ){ + case 0: + rc = sqlite3changeset_next(p->pIter); + Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1)); + break; + case 1: + Tcl_SetObjResult(interp, testIterData(p->pIter)); + break; + case 2: + rc = sqlite3changeset_finalize(p->pIter); + p->pIter = 0; + Tcl_DeleteCommand(interp, Tcl_GetString(objv[0])); + Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1)); + break; + default: + assert( 0 ); + break; + } + + return TCL_OK; +} + +/* +** Tclcmd: sqlite3changeset_start ?-invert? CHANGESET +*/ +static int SQLITE_TCLAPI test_sqlite3changeset_start( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + int isInvert = 0; + void *pChangeset = 0; /* Buffer containing changeset */ + Tcl_Size nChangeset = 0; /* Size of buffer aChangeset in bytes */ + TestChangeIter *pNew = 0; + sqlite3_changeset_iter *pIter = 0; + int flags = 0; + int rc = SQLITE_OK; + int nAlloc = 0; /* Bytes of space to allocate */ + + static int iCmd = 1; + char zCmd[64]; + + if( objc==3 ){ + Tcl_Size n = 0; + const char *z = Tcl_GetStringFromObj(objv[1], &n); + isInvert = (n>=2 && sqlite3_strnicmp(z, "-invert", (int)n)==0); + } + + if( objc!=2 && (objc!=3 || !isInvert) ){ + Tcl_WrongNumArgs(interp, 1, objv, "?-invert? CHANGESET"); + return TCL_ERROR; + } + + pChangeset = (void *)Tcl_GetByteArrayFromObj(objv[objc-1], &nChangeset); + flags = isInvert ? SQLITE_CHANGESETSTART_INVERT : 0; + + nAlloc = sizeof(TestChangeIter); + if( test_tcl_integer(interp, SESSION_STREAM_TCL_VAR) ){ + nAlloc += nChangeset; + } + pNew = (TestChangeIter*)ckalloc(nAlloc); + memset(pNew, 0, nAlloc); + if( test_tcl_integer(interp, SESSION_STREAM_TCL_VAR) ){ + pNew->in.nStream = test_tcl_integer(interp, SESSION_STREAM_TCL_VAR); + pNew->in.nData = nChangeset; + pNew->in.aData = (unsigned char*)&pNew[1]; + memcpy(pNew->in.aData, pChangeset, nChangeset); + } + + if( pNew->in.nStream ){ + void *pCtx = (void*)&pNew->in; + rc = sqlite3changeset_start_v2_strm(&pIter, testStreamInput, pCtx, flags); + }else{ + rc = sqlite3changeset_start_v2(&pIter, (int)nChangeset, pChangeset, flags); + } + if( rc!=SQLITE_OK ){ + char *zErr = sqlite3_mprintf( + "error in sqlite3changeset_start_v2() - %d", rc + ); + Tcl_AppendResult(interp, zErr, (char*)0); + ckfree(pNew); + return TCL_ERROR; + } + pNew->pIter = pIter; + + sprintf(zCmd, "csiter%d", iCmd++); + Tcl_CreateObjCommand(interp, zCmd, test_iter_cmd, (void*)pNew, test_iter_del); + Tcl_SetObjResult(interp, Tcl_NewStringObj(zCmd, -1)); + return TCL_OK; +} + +int TestSession_Init(Tcl_Interp *interp){ + struct Cmd { + const char *zCmd; + Tcl_ObjCmdProc *xProc; + } aCmd[] = { + { "sqlite3session", test_sqlite3session }, + { "sqlite3changegroup", test_sqlite3changegroup }, + { "sqlite3changeset_start", test_sqlite3changeset_start }, + { "sqlite3session_foreach", test_sqlite3session_foreach }, + { "sqlite3changeset_invert", test_sqlite3changeset_invert }, + { "sqlite3changeset_concat", test_sqlite3changeset_concat }, + { "sqlite3changeset_apply", test_sqlite3changeset_apply }, + { "sqlite3changeset_apply_v2", test_sqlite3changeset_apply_v2 }, + { "sqlite3changeset_apply_v3", test_sqlite3changeset_apply_v3 }, + { "sqlite3changeset_apply_replace_all", + test_sqlite3changeset_apply_replace_all }, + { "sql_exec_changeset", test_sql_exec_changeset }, + { "sqlite3rebaser_create", test_sqlite3rebaser_create }, + { "sqlite3session_config", test_sqlite3session_config }, + { "test_changeset", test_changeset }, + }; + int i; + + for(i=0; izCmd, p->xProc, 0, 0); + } + + return TCL_OK; +} + +#endif /* SQLITE_TEST && SQLITE_SESSION && SQLITE_PREUPDATE_HOOK */ diff --git a/ext/userauth/sqlite3userauth.h b/ext/userauth/sqlite3userauth.h deleted file mode 100644 index 619477cac9..0000000000 --- a/ext/userauth/sqlite3userauth.h +++ /dev/null @@ -1,88 +0,0 @@ -/* -** 2014-09-08 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -************************************************************************* -** -** This file contains the application interface definitions for the -** user-authentication extension feature. -** -** To compile with the user-authentication feature, append this file to -** end of an SQLite amalgamation header file ("sqlite3.h"), then add -** the SQLITE_USER_AUTHENTICATION compile-time option. See the -** user-auth.txt file in the same source directory as this file for -** additional information. -*/ -#ifdef SQLITE_USER_AUTHENTICATION - -/* -** If a database contains the SQLITE_USER table, then the -** sqlite3_user_authenticate() interface must be invoked with an -** appropriate username and password prior to enable read and write -** access to the database. -** -** Return SQLITE_OK on success or SQLITE_ERROR if the username/password -** combination is incorrect or unknown. -** -** If the SQLITE_USER table is not present in the database file, then -** this interface is a harmless no-op returnning SQLITE_OK. -*/ -int sqlite3_user_authenticate( - sqlite3 *db, /* The database connection */ - const char *zUsername, /* Username */ - const char *aPW, /* Password or credentials */ - int nPW /* Number of bytes in aPW[] */ -); - -/* -** The sqlite3_user_add() interface can be used (by an admin user only) -** to create a new user. When called on a no-authentication-required -** database, this routine converts the database into an authentication- -** required database, automatically makes the added user an -** administrator, and logs in the current connection as that user. -** The sqlite3_user_add() interface only works for the "main" database, not -** for any ATTACH-ed databases. Any call to sqlite3_user_add() by a -** non-admin user results in an error. -*/ -int sqlite3_user_add( - sqlite3 *db, /* Database connection */ - const char *zUsername, /* Username to be added */ - const char *aPW, /* Password or credentials */ - int nPW, /* Number of bytes in aPW[] */ - int isAdmin /* True to give new user admin privilege */ -); - -/* -** The sqlite3_user_change() interface can be used to change a users -** login credentials or admin privilege. Any user can change their own -** login credentials. Only an admin user can change another users login -** credentials or admin privilege setting. No user may change their own -** admin privilege setting. -*/ -int sqlite3_user_change( - sqlite3 *db, /* Database connection */ - const char *zUsername, /* Username to change */ - const char *aPW, /* New password or credentials */ - int nPW, /* Number of bytes in aPW[] */ - int isAdmin /* Modified admin privilege for the user */ -); - -/* -** The sqlite3_user_delete() interface can be used (by an admin user only) -** to delete a user. The currently logged-in user cannot be deleted, -** which guarantees that there is always an admin user and hence that -** the database cannot be converted into a no-authentication-required -** database. -*/ -int sqlite3_user_delete( - sqlite3 *db, /* Database connection */ - const char *zUsername /* Username to remove */ -); - -#endif /* SQLITE_USER_AUTHENTICATION */ diff --git a/ext/userauth/user-auth.txt b/ext/userauth/user-auth.txt deleted file mode 100644 index ba4eabc137..0000000000 --- a/ext/userauth/user-auth.txt +++ /dev/null @@ -1,164 +0,0 @@ -Activate the user authentication logic by including the -ext/userauth/userauth.c source code file in the build and -adding the -DSQLITE_USER_AUTHENTICATION compile-time option. -The ext/userauth/sqlite3userauth.h header file is available to -applications to define the interface. - -When using the SQLite amalgamation, it is sufficient to append -the ext/userauth/userauth.c source file onto the end of the -amalgamation. - -The following new APIs are available when user authentication is -activated: - - int sqlite3_user_authenticate( - sqlite3 *db, /* The database connection */ - const char *zUsername, /* Username */ - const char *aPW, /* Password or credentials */ - int nPW /* Number of bytes in aPW[] */ - ); - - int sqlite3_user_add( - sqlite3 *db, /* Database connection */ - const char *zUsername, /* Username to be added */ - const char *aPW, /* Password or credentials */ - int nPW, /* Number of bytes in aPW[] */ - int isAdmin /* True to give new user admin privilege */ - ); - - int sqlite3_user_change( - sqlite3 *db, /* Database connection */ - const char *zUsername, /* Username to change */ - const void *aPW, /* Modified password or credentials */ - int nPW, /* Number of bytes in aPW[] */ - int isAdmin /* Modified admin privilege for the user */ - ); - - int sqlite3_user_delete( - sqlite3 *db, /* Database connection */ - const char *zUsername /* Username to remove */ - ); - -With this extension, a database can be marked as requiring authentication. -By default a database does not require authentication. - -The sqlite3_open(), sqlite3_open16(), and sqlite3_open_v2() interfaces -work as before: they open a new database connection. However, if the -database being opened requires authentication, then attempts to read -or write from the database will fail with an SQLITE_AUTH error until -after sqlite3_user_authenticate() has been called successfully. The -sqlite3_user_authenticate() call will return SQLITE_OK if the -authentication credentials are accepted and SQLITE_ERROR if not. - -Calling sqlite3_user_authenticate() on a no-authentication-required -database connection is a harmless no-op. - -If the database is encrypted, then sqlite3_key_v2() must be called first, -with the correct decryption key, prior to invoking sqlite3_user_authenticate(). - -To recapitulate: When opening an existing unencrypted authentication- -required database, the call sequence is: - - sqlite3_open_v2() - sqlite3_user_authenticate(); - /* Database is now usable */ - -To open an existing, encrypted, authentication-required database, the -call sequence is: - - sqlite3_open_v2(); - sqlite3_key_v2(); - sqlite3_user_authenticate(); - /* Database is now usable */ - -When opening a no-authentication-required database, the database -connection is treated as if it was authenticated as an admin user. - -When ATTACH-ing new database files to a connection, each newly attached -database that is an authentication-required database is checked using -the same username and password as supplied to the main database. If that -check fails, then the ATTACH command fails with an SQLITE_AUTH error. - -The sqlite3_user_add() interface can be used (by an admin user only) -to create a new user. When called on a no-authentication-required -database and when A is true, the sqlite3_user_add(D,U,P,N,A) routine -converts the database into an authentication-required database and -logs in the database connection D as user U with password P,N. -To convert a no-authentication-required database into an authentication- -required database, the isAdmin parameter must be true. If -sqlite3_user_add(D,U,P,N,A) is called on a no-authentication-required -database and A is false, then the call fails with an SQLITE_AUTH error. - -Any call to sqlite3_user_add() by a non-admin user results in an error. - -Hence, to create a new, unencrypted, authentication-required database, -the call sequence is: - - sqlite3_open_v2(); - sqlite3_user_add(); - -And to create a new, encrypted, authentication-required database, the call -sequence is: - - sqlite3_open_v2(); - sqlite3_key_v2(); - sqlite3_user_add(); - -The sqlite3_user_delete() interface can be used (by an admin user only) -to delete a user. The currently logged-in user cannot be deleted, -which guarantees that there is always an admin user and hence that -the database cannot be converted into a no-authentication-required -database. - -The sqlite3_user_change() interface can be used to change a users -login credentials or admin privilege. Any user can change their own -password. Only an admin user can change another users login -credentials or admin privilege setting. No user may change their own -admin privilege setting. - -The sqlite3_set_authorizer() callback is modified to take a 7th parameter -which is the username of the currently logged in user, or NULL for a -no-authentication-required database. - ------------------------------------------------------------------------------ -Implementation notes: - -An authentication-required database is identified by the presence of a -new table: - - CREATE TABLE sqlite_user( - uname TEXT PRIMARY KEY, - isAdmin BOOLEAN, - pw BLOB - ) WITHOUT ROWID; - -The sqlite_user table is inaccessible (unreadable and unwriteable) to -non-admin users and is read-only for admin users. However, if the same -database file is opened by a version of SQLite that omits -the -DSQLITE_USER_AUTHENTICATION compile-time option, then the sqlite_user -table will be readable by anybody and writeable by anybody if -the "PRAGMA writable_schema=ON" statement is run first. - -The sqlite_user.pw field is encoded by a built-in SQL function -"sqlite_crypt(X,Y)". The two arguments are both BLOBs. The first argument -is the plaintext password supplied to the sqlite3_user_authenticate() -interface. The second argument is the sqlite_user.pw value and is supplied -so that the function can extract the "salt" used by the password encoder. -The result of sqlite_crypt(X,Y) is another blob which is the value that -ends up being stored in sqlite_user.pw. To verify credentials X supplied -by the sqlite3_user_authenticate() routine, SQLite runs: - - sqlite_user.pw == sqlite_crypt(X, sqlite_user.pw) - -To compute an appropriate sqlite_user.pw value from a new or modified -password X, sqlite_crypt(X,NULL) is run. A new random salt is selected -when the second argument is NULL. - -The built-in version of of sqlite_crypt() uses a simple Ceasar-cypher -which prevents passwords from being revealed by searching the raw database -for ASCII text, but is otherwise trivally broken. For better password -security, the database should be encrypted using the SQLite Encryption -Extension or similar technology. Or, the application can use the -sqlite3_create_function() interface to provide an alternative -implementation of sqlite_crypt() that computes a stronger password hash, -perhaps using a cryptographic hash function like SHA1. diff --git a/ext/userauth/userauth.c b/ext/userauth/userauth.c deleted file mode 100644 index 6ce99053d3..0000000000 --- a/ext/userauth/userauth.c +++ /dev/null @@ -1,355 +0,0 @@ -/* -** 2014-09-08 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -************************************************************************* -** -** This file contains the bulk of the implementation of the -** user-authentication extension feature. Some parts of the user- -** authentication code are contained within the SQLite core (in the -** src/ subdirectory of the main source code tree) but those parts -** that could reasonable be separated out are moved into this file. -** -** To compile with the user-authentication feature, append this file to -** end of an SQLite amalgamation, then add the SQLITE_USER_AUTHENTICATION -** compile-time option. See the user-auth.txt file in the same source -** directory as this file for additional information. -*/ -#ifdef SQLITE_USER_AUTHENTICATION -#ifndef _SQLITEINT_H_ -# include "sqliteInt.h" -#endif - -/* -** Prepare an SQL statement for use by the user authentication logic. -** Return a pointer to the prepared statement on success. Return a -** NULL pointer if there is an error of any kind. -*/ -static sqlite3_stmt *sqlite3UserAuthPrepare( - sqlite3 *db, - const char *zFormat, - ... -){ - sqlite3_stmt *pStmt; - char *zSql; - int rc; - va_list ap; - int savedFlags = db->flags; - - va_start(ap, zFormat); - zSql = sqlite3_vmprintf(zFormat, ap); - va_end(ap); - if( zSql==0 ) return 0; - db->flags |= SQLITE_WriteSchema; - rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); - db->flags = savedFlags; - sqlite3_free(zSql); - if( rc ){ - sqlite3_finalize(pStmt); - pStmt = 0; - } - return pStmt; -} - -/* -** Check to see if the sqlite_user table exists in database zDb. -*/ -static int userTableExists(sqlite3 *db, const char *zDb){ - int rc; - sqlite3_mutex_enter(db->mutex); - sqlite3BtreeEnterAll(db); - if( db->init.busy==0 ){ - char *zErr = 0; - sqlite3Init(db, &zErr); - sqlite3DbFree(db, zErr); - } - rc = sqlite3FindTable(db, "sqlite_user", zDb)!=0; - sqlite3BtreeLeaveAll(db); - sqlite3_mutex_leave(db->mutex); - return rc; -} - -/* -** Check to see if database zDb has a "sqlite_user" table and if it does -** whether that table can authenticate zUser with nPw,zPw. Write one of -** the UAUTH_* user authorization level codes into *peAuth and return a -** result code. -*/ -static int userAuthCheckLogin( - sqlite3 *db, /* The database connection to check */ - const char *zDb, /* Name of specific database to check */ - u8 *peAuth /* OUT: One of UAUTH_* constants */ -){ - sqlite3_stmt *pStmt; - int rc; - - *peAuth = UAUTH_Unknown; - if( !userTableExists(db, "main") ){ - *peAuth = UAUTH_Admin; /* No sqlite_user table. Everybody is admin. */ - return SQLITE_OK; - } - if( db->auth.zAuthUser==0 ){ - *peAuth = UAUTH_Fail; - return SQLITE_OK; - } - pStmt = sqlite3UserAuthPrepare(db, - "SELECT pw=sqlite_crypt(?1,pw), isAdmin FROM \"%w\".sqlite_user" - " WHERE uname=?2", zDb); - if( pStmt==0 ) return SQLITE_NOMEM; - sqlite3_bind_blob(pStmt, 1, db->auth.zAuthPW, db->auth.nAuthPW,SQLITE_STATIC); - sqlite3_bind_text(pStmt, 2, db->auth.zAuthUser, -1, SQLITE_STATIC); - rc = sqlite3_step(pStmt); - if( rc==SQLITE_ROW && sqlite3_column_int(pStmt,0) ){ - *peAuth = sqlite3_column_int(pStmt, 1) + UAUTH_User; - }else{ - *peAuth = UAUTH_Fail; - } - return sqlite3_finalize(pStmt); -} -int sqlite3UserAuthCheckLogin( - sqlite3 *db, /* The database connection to check */ - const char *zDb, /* Name of specific database to check */ - u8 *peAuth /* OUT: One of UAUTH_* constants */ -){ - int rc; - u8 savedAuthLevel; - assert( zDb!=0 ); - assert( peAuth!=0 ); - savedAuthLevel = db->auth.authLevel; - db->auth.authLevel = UAUTH_Admin; - rc = userAuthCheckLogin(db, zDb, peAuth); - db->auth.authLevel = savedAuthLevel; - return rc; -} - -/* -** If the current authLevel is UAUTH_Unknown, the take actions to figure -** out what authLevel should be -*/ -void sqlite3UserAuthInit(sqlite3 *db){ - if( db->auth.authLevel==UAUTH_Unknown ){ - u8 authLevel = UAUTH_Fail; - sqlite3UserAuthCheckLogin(db, "main", &authLevel); - db->auth.authLevel = authLevel; - if( authLevelflags &= ~SQLITE_WriteSchema; - } -} - -/* -** Implementation of the sqlite_crypt(X,Y) function. -** -** If Y is NULL then generate a new hash for password X and return that -** hash. If Y is not null, then generate a hash for password X using the -** same salt as the previous hash Y and return the new hash. -*/ -void sqlite3CryptFunc( - sqlite3_context *context, - int NotUsed, - sqlite3_value **argv -){ - const char *zIn; - int nIn, ii; - u8 *zOut; - char zSalt[8]; - zIn = sqlite3_value_blob(argv[0]); - nIn = sqlite3_value_bytes(argv[0]); - if( sqlite3_value_type(argv[1])==SQLITE_BLOB - && sqlite3_value_bytes(argv[1])==nIn+sizeof(zSalt) - ){ - memcpy(zSalt, sqlite3_value_blob(argv[1]), sizeof(zSalt)); - }else{ - sqlite3_randomness(sizeof(zSalt), zSalt); - } - zOut = sqlite3_malloc( nIn+sizeof(zSalt) ); - if( zOut==0 ){ - sqlite3_result_error_nomem(context); - }else{ - memcpy(zOut, zSalt, sizeof(zSalt)); - for(ii=0; iiauth.authLevel = UAUTH_Unknown; - sqlite3_free(db->auth.zAuthUser); - sqlite3_free(db->auth.zAuthPW); - memset(&db->auth, 0, sizeof(db->auth)); - db->auth.zAuthUser = sqlite3_mprintf("%s", zUsername); - if( db->auth.zAuthUser==0 ) return SQLITE_NOMEM; - db->auth.zAuthPW = sqlite3_malloc( nPW+1 ); - if( db->auth.zAuthPW==0 ) return SQLITE_NOMEM; - memcpy(db->auth.zAuthPW,zPW,nPW); - db->auth.nAuthPW = nPW; - rc = sqlite3UserAuthCheckLogin(db, "main", &authLevel); - db->auth.authLevel = authLevel; - sqlite3ExpirePreparedStatements(db); - if( rc ){ - return rc; /* OOM error, I/O error, etc. */ - } - if( authLevelauth.authLevelauth.zAuthUser==0 ){ - assert( isAdmin!=0 ); - sqlite3_user_authenticate(db, zUsername, aPW, nPW); - } - return SQLITE_OK; -} - -/* -** The sqlite3_user_change() interface can be used to change a users -** login credentials or admin privilege. Any user can change their own -** login credentials. Only an admin user can change another users login -** credentials or admin privilege setting. No user may change their own -** admin privilege setting. -*/ -int sqlite3_user_change( - sqlite3 *db, /* Database connection */ - const char *zUsername, /* Username to change */ - const char *aPW, /* Modified password or credentials */ - int nPW, /* Number of bytes in aPW[] */ - int isAdmin /* Modified admin privilege for the user */ -){ - sqlite3_stmt *pStmt; - int rc; - u8 authLevel; - - authLevel = db->auth.authLevel; - if( authLevelauth.zAuthUser, zUsername)!=0 ){ - if( db->auth.authLevelauth.authLevel = UAUTH_Admin; - if( !userTableExists(db, "main") ){ - /* This routine is a no-op if the user to be modified does not exist */ - }else{ - pStmt = sqlite3UserAuthPrepare(db, - "UPDATE sqlite_user SET isAdmin=%d, pw=sqlite_crypt(?1,NULL)" - " WHERE uname=%Q", isAdmin, zUsername); - if( pStmt==0 ){ - rc = SQLITE_NOMEM; - }else{ - sqlite3_bind_blob(pStmt, 1, aPW, nPW, SQLITE_STATIC); - sqlite3_step(pStmt); - rc = sqlite3_finalize(pStmt); - } - } - db->auth.authLevel = authLevel; - return rc; -} - -/* -** The sqlite3_user_delete() interface can be used (by an admin user only) -** to delete a user. The currently logged-in user cannot be deleted, -** which guarantees that there is always an admin user and hence that -** the database cannot be converted into a no-authentication-required -** database. -*/ -int sqlite3_user_delete( - sqlite3 *db, /* Database connection */ - const char *zUsername /* Username to remove */ -){ - sqlite3_stmt *pStmt; - if( db->auth.authLevelauth.zAuthUser, zUsername)==0 ){ - /* Cannot delete self */ - return SQLITE_AUTH; - } - if( !userTableExists(db, "main") ){ - /* This routine is a no-op if the user to be deleted does not exist */ - return SQLITE_OK; - } - pStmt = sqlite3UserAuthPrepare(db, - "DELETE FROM sqlite_user WHERE uname=%Q", zUsername); - if( pStmt==0 ) return SQLITE_NOMEM; - sqlite3_step(pStmt); - return sqlite3_finalize(pStmt); -} - -#endif /* SQLITE_USER_AUTHENTICATION */ diff --git a/ext/wasm/EXPORTED_FUNCTIONS.fiddle.in b/ext/wasm/EXPORTED_FUNCTIONS.fiddle.in new file mode 100644 index 0000000000..103704df10 --- /dev/null +++ b/ext/wasm/EXPORTED_FUNCTIONS.fiddle.in @@ -0,0 +1,10 @@ +_fiddle_db_arg +_fiddle_db_filename +_fiddle_exec +_fiddle_experiment +_fiddle_interrupt +_fiddle_main +_fiddle_reset_db +_fiddle_db_handle +_fiddle_db_vfs +_fiddle_export_db diff --git a/ext/wasm/GNUmakefile b/ext/wasm/GNUmakefile new file mode 100644 index 0000000000..937e16d6ef --- /dev/null +++ b/ext/wasm/GNUmakefile @@ -0,0 +1,1553 @@ +####################################################################### +# This GNU makefile creates the canonical sqlite3 WASM builds. +# +# This build assumes a Linux platform and is not intended for +# general-purpose client-level use, except for creating builds with +# custom configurations. It is primarily intended for the SQLite +# project's own development of its JS/WASM components. +# +# Primary targets: +# +# default, all = build in dev mode +# +# o0, o1, o2, o3, os, oz = full clean/rebuild with the -Ox level indicated +# by the target name. Rebuild is necessary for all components to get +# the desired optimization level. +# +# dist = create end user deliverables. Add dist.build=oX to build +# with a specific optimization level, where oX is one of the +# above-listed o? or qo? target names. +# +# snapshot = like dist, but uses a zip file name which clearly +# marks it as a prerelease/snapshot build. +# +# clean = clean up +# +# Required tools beyond those needed for the canonical builds: +# +# - Emscripten SDK: https://emscripten.org/docs/getting_started/downloads.html +# - The bash shell +# - GNU make, GNU sed, GNU awk, GNU grep (all in the $PATH and without +# a "g" prefix like they have on some non-GNU systems) +# - wasm-strip for release builds: https://github.com/WebAssembly/wabt +# - InfoZip for 'dist' zip file +######################################################################## +default: all +MAKEFILE = $(lastword $(MAKEFILE_LIST)) +CLEAN_FILES = +DISTCLEAN_FILES = config.make +MAKING_CLEAN = $(if $(filter %clean,$(MAKECMDGOALS)),1,0) + +# +# dir.X = various directory names. +# +# dir.top = the top dir of the canonical build tree, where +# sqlite3.[ch] live. +# +dir.top = ../.. +dir.wasm = $(patsubst %/,%,$(dir $(MAKEFILE))) +dir.api = api +dir.jacc = jaccwabyt +dir.common = common +dir.fiddle = fiddle +dir.fiddle.debug = fiddle-debug +dir.tool = $(dir.top)/tool +# dir.dout = output dir for deliverables +dir.dout = $(dir.wasm)/jswasm +# dir.tmp = output dir for intermediary build files, as opposed to +# end-user deliverables. +dir.tmp = $(dir.wasm)/bld +dir.wasmfs = $(dir.dout) + +# +# Emoji for log messages. +# +emo.bug = 🐞 +emo.compile = ⏳ +emo.roadblock = 🚧 +emo.disk = 💾 +emo.done = ✅ +emo.fire = 🔥 +emo.folder = 📁 +emo.garbage = 🗑 +emo.lock = 🔒 +emo.magic = 🧙 +emo.megaphone = 📣 +emo.mute = 🔇 +emo.edit = ✏️ +emo.stop = 🛑 +emo.strip = 💈 +emo.test = 🧪 +emo.tool = 🔨 +emo.wasm-opt = 🧼 +# 👷🪄🧮🧫🧽🍿⛽🚧🎱🪚🏆🧼 + +# +# Special-case builds for which we require certain pre-conditions +# which, if not met, may cause warnings or fatal errors in the build. +# This also affects the default optimization level flags. The fiddle +# targets are in this list because they are used for generating +# sqlite.org/fiddle. +# +OPTIMIZED_TARGETS = dist snapshot fiddle fiddle.debug + +ifeq (1,$(MAKING_CLEAN)) + bin.wasm-strip = echo "not stripping" + bin.wasm-opt = irrelevant + bin.emcc = irrelevant + bin.bash = irrelevant + emcc.version = unknown +else + # Include config.make and perform some bootstrapping... + ifeq (,$(wildcard ./config.make)) + $(error Missing config.make. It gets generated by the configure script if the EMSDK is found) + endif + include ./config.make + ifeq (,$(bin.bash)) + $(error Configure script did not find the bash shell) + endif + ifeq (,$(bin.emcc)) + $(error Configure script did not find emcc) + endif + emcc.version = $(shell $(bin.emcc) --version | sed -n 1p | sed -e 's/^.* \([3-9][^ ]*\) .*$$/\1/;') + $(info using emcc version [$(emcc.version)]) + ifeq (,$(bin.wasm-strip)) + # + # We need wasm-strip for release builds (see below for why) but + # not strictly for non-release builds. + # + achtung = $(emo.fire)WARNING + $(info $(achtung): *******************************************************************) + $(info $(achtung): Builds using -Oz will minify WASM-exported names, breaking) + $(info $(achtung): _All The Things_. The workaround for that is to build) + $(info $(achtung): with -g3 (which explodes the file size) and then strip the debug) + $(info $(achtung): info after compilation, using wasm-strip, to shrink the wasm file.) + $(info $(achtung): wasm-strip was not found in the PATH so we cannot strip those.) + $(info $(achtung): If this build uses any optimization level higher than -O1 then) + $(info $(achtung): the ***resulting JS code WILL NOT BE USABLE***.) + $(info $(achtung): wasm-strip is part of the wabt package:) + $(info $(achtung): https://github.com/WebAssembly/wabt) + $(info $(achtung): on Ubuntu-like systems it can be installed with:) + $(info $(achtung): sudo apt install wabt) + $(info $(achtung): *******************************************************************) + ifneq (,$(filter $(OPTIMIZED_TARGETS),$(MAKECMDGOALS))) + $(error Cannot make release-quality binary because wasm-strip is not available.) + endif + bin.wasm-strip = echo "not wasm-stripping" + endif + ifeq (,$(filter $(OPTIMIZED_TARGETS),$(MAKECMDGOALS))) + $(info ==============================================================) + $(info == Development build. Make one of (dist, snapshot) for a) + $(info == smaller and faster release build.) + $(info ==============================================================) + endif +endif +# ^^^ end of are-we-MAKING_CLEAN + +# +# Common vars and $(call)/$(eval)able utilities. +# +# The "b." prefix on some APIs is for "build". It was initially used +# only for features specific to each distinct js/wasm build. That's no +# longer the case, but the naming convention has stuck. +# + +loud ?= 0 +ifeq (1,$(loud)) + $(info $(emo.megaphone) Emitting loud build info. Pass loud=0 to disable it.) + b.cmd@ = + loud.if = 1 +else + $(info $(emo.mute) Eliding loud build info. Pass loud=1 to enable it.) + b.cmd@ = @ + loud.if = +endif + +# +# logtag.X value for log context labeling. logtag.OTHERX can be +# assigned to customize it for a given X. This tag is used by +# b.call.X, b.eval.X, etc. for logging. There motivation for this is +# adding a build-specific prefix to messages so that the output of +# parallel builds is easier to sort through. We use emoji for the +# prefixes because it's far easier for my eyes to sort through than +# using only each build's name as the prefix. +# +# Each distinct build sets up its own logtag.BUILDNAME. +# +logtag.@ = [$@] +logtag.filter = [$(emo.disk) $@] +logtag.test = [$(emo.test) $@] +logtag.cp = [$(emo.disk) $@] + +# +# $(call b.echo,LOGTAG,msg) +# +b.echo = echo $(logtag.$(1)) $(2) + +# +# $(call b.mkdir@) +# +# $1 = optional LOGTAG +# +b.mkdir@ = if [ ! -d $(dir $@) ]; then \ + echo '[$(emo.folder)+] $(if $(1),$(logtag.$(1)),[$(dir $@)])'; \ + mkdir -p $(dir $@) || exit; fi + +# +# $(call b.cp,@,src,dest) +# +# $1 = logtag, $2 = src file(s). $3 = dest dir +b.cp = $(call b.mkdir@); \ + echo '$(logtag.$(1)) $(emo.disk) $(2) ==> $3'; \ + cp -p $(2) $(3) || exit + +# +# $(call b.c-pp.shcmd,LOGTAG,src,dest,-Dx=y...) +# +# Resolves to shell code to create $(3) from $(2) and $(4) using +# $(bin.c-pp). +# +# $1 = build name/logtag +# $2 = Input file(s) +# $3 = Output file +# $4 = optional $(bin.c-pp) flags +define b.c-pp.shcmd +$(call b.mkdir@); \ +$(call b.echo,$(1),$(emo.disk)$(emo.lock) $(bin.c-pp) $(4) $(if $(loud.if),$(2))); \ +rm -f $(3); \ +$(bin.c-pp) -o $(3) $(4) $(2) || exit; \ +chmod -w $(3) +endef + +# +# $(eval $(call b.c-pp.target,LOGTAG,src,dest,-Dx=y...)) +# +# Creates target $(3) using $(bin.c-pp) $(2) $(4). +# +# Args: as for $(b.c-pp.shcmd). +define b.c-pp.target +$(3): $$(MAKEFILE_LIST) $$(bin.c-pp) $(2) + @$$(call b.c-pp.shcmd,$(1),$(2),$(3),$(4) $(b.c-pp.target.flags)) +CLEAN_FILES += $(3) +endef + +c-pp.D.64bit = -Dbits64 + +# +# $(call b.strip-js-emcc-bindings) +# +# $1 = an optional log message prefix +# +# Our JS code installs bindings of each sqlite3_...() WASM export. The +# generated Emscripten JS file does the same using its own framework, +# but we don't use those results and can speed up lib init, and reduce +# memory cost a bit, by stripping them out. Emscripten code-generation +# changes can "break" this, causing this to be a no-op, but (probably) +# the worst that can happen in that case is that it doesn't actually +# strip anything, leading to slightly larger JS files. +# +# This is intended to be used in makefile targets which generate an +# Emscripten module and where $@ is the module's .js/.mjs file. +b.strip-js-emcc-bindings = \ + sed -i -e '/^.*= \(_sqlite3\|_fiddle\)[^=]*=.*createExportWrapper/d' \ + -e '/^var \(_sqlite3\|_fiddle\)[^=]*=.*makeInvalidEarlyAccess/d' $@ || exit; \ + echo '$(1) $(emo.garbage) (Probably) /createExportWrapper()/d and /makeInvalidEarlyAccess()/d' + + +# +# Set up sqlite3.c and sqlite3.h... +# +# To build with SEE (https://sqlite.org/see), either put sqlite3-see.c +# in $(dir.top) or pass sqlite3.c=PATH_TO_sqlite3-see.c to the $(MAKE) +# invocation. Note that only encryption modules with no 3rd-party +# dependencies will currently work here: AES256-OFB, AES128-OFB, and +# AES128-CCM. Not coincidentally, those 3 modules are included in the +# sqlite3-see.c bundle. Note, however, that distributing an SEE build +# of the WASM on a public site is in violation of the SEE license +# because it effectively provides a usable copy of the SEE build to +# all visitors. +# +# A custom sqlite3.c must not have any spaces in its name. +# $(sqlite3.canonical.c) must point to the sqlite3.c in +# the sqlite3 canonical source tree, as that source file +# is required for certain utility and test code. +sqlite3.canonical.c = $(dir.top)/sqlite3.c +sqlite3.c ?= $(firstword $(wildcard $(dir.top)/sqlite3-see.c) $(sqlite3.canonical.c)) +sqlite3.h = $(dir $(sqlite3.c))/sqlite3.h + +# +# bin.version-info = binary to output various sqlite3 version info for +# embedding in the JS files and in building the distribution zip file. +# It must NOT be in $(dir.tmp) because we need it to survive the +# cleanup process for the dist build to work properly. +bin.version-info = ./version-info +$(bin.version-info): $(dir.tool)/version-info.c $(sqlite3.h) $(dir.top)/Makefile + $(CC) -o $@ -I$(dir $(sqlite3.h)) $(dir.tool)/version-info.c +t-version-info: $(bin.version-info) +DISTCLEAN_FILES += $(bin.version-info) +# +# bin.stripcomments is used for stripping C/C++-style comments from JS +# files. The JS files contain large chunks of documentation which we +# don't need for all builds. That app's -k flag is of particular +# importance here, as it allows us to retain the opening comment +# block(s), which contain the license header and version info. +bin.stripccomments = $(dir.tool)/stripccomments +$(bin.stripccomments): $(bin.stripccomments).c $(MAKEFILE) + $(CC) -o $@ $< +t-stripccomments: $(bin.stripccomments) +DISTCLEAN_FILES += $(bin.stripccomments) + + +ifeq (1,$(MAKING_CLEAN)) + SQLITE_C_IS_SEE = 0 +else + ifeq (,$(shell grep sqlite3_activate_see $(sqlite3.c))) + SQLITE_C_IS_SEE = 0 + else + SQLITE_C_IS_SEE = 1 + $(info This is an SEE build) + endif +endif + +# +# barebones=1 disables all "extraneous" stuff from sqlite3-wasm.c, the +# goal being to create a WASM file with only the core APIs. +# +ifeq (1,$(barebones)) + wasm-bare-bones = 1 + $(info ==============================================================) + $(info == This is a bare-bones build. It trades away features for) + $(info == a smaller .wasm file.) + $(info ==============================================================) +else + wasm-bare-bones = 0 +endif +# undefine barebones # relatively new gmake feature, not ubiquitous + +# +# It's important that sqlite3.h be built to completion before any +# other parts of the build run, thus we use .NOTPARALLEL to disable +# parallel build of that file and its dependants. However, that makes +# the whole build non-parallelizable because everything has a dep on +# sqlite3.h/c. The alternative is to force the user to run (make +# sqlite3.c) from the top of the tree before running this build. +# +#.NOTPARALLEL: $(sqlite3.h) +# +$(sqlite3.h): + @echo "$(sqlite3.h) is out of date. "; \ + echo "To avoid problems with parallel builds, we're exiting now. Please do:"; \ + echo " $(MAKE) -C $(dir.top) sqlite3.c"; \ + echo "and try again."; exit 1 +# $(MAKE) -C $(dir.top) sqlite3.c +$(sqlite3.c): $(sqlite3.h) + +# Common options for building sqlite3-wasm.c and speedtest1.c. +# Explicit ENABLEs... +SQLITE_OPT.common = \ + -DSQLITE_THREADSAFE=0 \ + -DSQLITE_TEMP_STORE=2 \ + -DSQLITE_ENABLE_MATH_FUNCTIONS \ + -DSQLITE_OS_KV_OPTIONAL=1 \ + '-DSQLITE_DEFAULT_UNIX_VFS="unix-none"' \ + -DSQLITE_USE_URI=1 \ + -DSQLITE_C=$(sqlite3.c) \ + -DSQLITE_OMIT_DEPRECATED \ + -DSQLITE_OMIT_UTF16 \ + -DSQLITE_OMIT_LOAD_EXTENSION \ + -DSQLITE_OMIT_SHARED_CACHE +# ^^^ These particular OMITs are hard-coded in sqlite3-wasm.c and +# removing them from this list will serve only to break the speedtest1 +# builds. + +# Currently always needed but TODO is paring tester1.c-pp.js down +# to be able to run without this: +SQLITE_OPT.common += -DSQLITE_WASM_ENABLE_C_TESTS + +# Extra flags for full-featured builds... +SQLITE_OPT.full-featured = \ + -DSQLITE_ENABLE_BYTECODE_VTAB \ + -DSQLITE_ENABLE_DBPAGE_VTAB \ + -DSQLITE_ENABLE_DBSTAT_VTAB \ + -DSQLITE_ENABLE_FTS5 \ + -DSQLITE_ENABLE_MATH_FUNCTIONS \ + -DSQLITE_ENABLE_OFFSET_SQL_FUNC \ + -DSQLITE_ENABLE_PREUPDATE_HOOK \ + -DSQLITE_ENABLE_RTREE \ + -DSQLITE_ENABLE_SESSION \ + -DSQLITE_ENABLE_STMTVTAB \ + -DSQLITE_ENABLE_UNKNOWN_SQL_FUNCTION \ + -DSQLITE_ENABLE_COLUMN_METADATA \ + -DSQLITE_ENABLE_PERCENTILE=1 + + +ifeq (0,$(wasm-bare-bones)) + # The so-called canonical build is full-featured: + SQLITE_OPT = \ + $(SQLITE_OPT.common) \ + $(SQLITE_OPT.full-featured) +else + # The so-called bare-bones build is exactly that: + SQLITE_OPT = \ + $(SQLITE_OPT.common) \ + -DSQLITE_WASM_BARE_BONES + # SQLITE_WASM_BARE_BONES tells sqlite3-wasm.c to explicitly omit + # a bunch of stuff, in the interest of keeping the wasm file size + # down. As of this writing it equates to: + # + # -USQLITE_ENABLE_DBPAGE_VTAB + # -USQLITE_ENABLE_DBSTAT_VTAB + # -USQLITE_ENABLE_EXPLAIN_COMMENTS + # -USQLITE_ENABLE_FTS5 + # -USQLITE_ENABLE_OFFSET_SQL_FUNC + # -USQLITE_ENABLE_PREUPDATE_HOOK + # -USQLITE_ENABLE_RTREE + # -USQLITE_ENABLE_SESSION + # -USQLITE_ENABLE_STMTVTAB + # -DSQLITE_OMIT_AUTHORIZATION + # -DSQLITE_OMIT_GET_TABLE + # -DSQLITE_OMIT_INCRBLOB + # -DSQLITE_OMIT_INTROSPECTION_PRAGMAS + # -DSQLITE_OMIT_JSON + # -DSQLITE_OMIT_PROGRESS_CALLBACK + # -DSQLITE_OMIT_WAL + # + # There are others we want here but which require explicit OMIT when + # creating their amalgamation, and that step is TODO: + # + # -DSQLITE_OMIT_EXPLAIN + # -DSQLITE_OMIT_TRIGGER + # -DSQLITE_OMIT_VIRTUALTABLE + # -DSQLITE_OMIT_WINDOWFUNC +endif + +#SQLITE_OPT += -DSQLITE_DEBUG +# Enabling SQLITE_DEBUG will break sqlite3_wasm_vfs_create_file() +# (and thus sqlite3_js_vfs_create_file()). Those functions are +# deprecated and alternatives are in place, but this crash behavior +# can be used to find errant uses of sqlite3_js_vfs_create_file() +# in client code. +######################################################################## +# The following flags are hard-coded into sqlite3-wasm.c and cannot be +# modified via the build process: +# +# SQLITE_ENABLE_API_ARMOR +# SQLITE_OMIT_LOAD_EXTENSION +# SQLITE_OMIT_DEPRECATED +# SQLITE_OMIT_UTF16 +# SQLITE_OMIT_SHARED_CACHE +######################################################################## + + +######################################################################## +# Adding custom C code via sqlite3_wasm_extra_init.c: +# +# If the canonical build process finds the file +# sqlite3_wasm_extra_init.c in the main wasm build directory, it +# arranges to include that file in the build of sqlite3.wasm and +# defines SQLITE_EXTRA_INIT_MUTEXED=sqlite3_wasm_extra_init. +# +# sqlite3_wasm_extra_init() must be a function with this signature: +# +# int sqlite3_wasm_extra_init(const char *) +# +# and the sqlite3 library will call it with an argument of NULL one +# time during sqlite3_initialize(). If it returns non-0, +# initialization of the library will fail. +# +# The filename can be overridden with: +# +# make sqlite3_wasm_extra_init.c=my_custom_stuff.c +# +# See example_extra_init.c for an example implementation. +######################################################################## +sqlite3_wasm_extra_init.c ?= $(wildcard sqlite3_wasm_extra_init.c) +cflags.wasm_extra_init = +ifneq (,$(sqlite3_wasm_extra_init.c)) + $(info Enabling SQLITE_EXTRA_INIT via $(sqlite3_wasm_extra_init.c).) + cflags.wasm_extra_init = -DSQLITE_WASM_EXTRA_INIT +endif + +# +# Actitivates (or not) a custom Module.instantiateWasm() override for +# Emscripten. That override gives us more control over exactly which +# WASM file is in use. +# +# If $(WASM_CUSTOM_INSTANTIATE) is 1 then mkwasmbuilds will add +# -Dcustom-Module.instantiateWasm to some of the builds. This is +# experimental but works on all browsers tested by its developer. +# +# Changing this may require a clean rebuild. It also might not work. +# +WASM_CUSTOM_INSTANTIATE = 1 + +######################################################################## +# $(bin.c-pp): a minimal text file preprocessor. Like C's but much +# less so. +# +# Historical notes about preprocessing files in this project: +# +# - We first attempted to use gcc and/or clang to preprocess JS files +# in the same way we would normally do C files, but C-specific quirks +# of each makes that untennable. +# +# - We implemented c-pp-lite.c (the C-Minus Pre-processor) as a custom +# generic/file-format-agnostic preprocessor to enable us to pack +# code for different target builds into the same JS files. Most +# notably, some ES6 module (a.k.a. ESM) features cannot legally be +# referenced at all in non-ESM code, e.g. the "import" and "export" +# keywords. This preprocessing step permits us to swap out sections +# of code where necessary for ESM and non-ESM (a.k.a. vanilla JS) +# require different implementations. The alternative to such +# preprocessing, would be to have separate source files for ES6 +# builds, which would have a higher maintenance burden than +# c-pp-lite.c seems likely to. +# +# c-pp-lite.c was written specifically for the sqlite project's +# JavaScript builds but is maintained as a standalone project: +# https://fossil.wanderinghorse.net/r/c-pp +# +# The SQLITE_... build flags used here have NO EFFECT on the JS/WASM +# build. They are solely for use with $(bin.c-pp) itself. +# +# -D... flags which should be included in all invocations should be +# appended to $(b.c-pp.target.flags). +bin.c-pp = ./c-pp-lite +$(bin.c-pp): c-pp-lite.c $(sqlite3.c) $(MAKEFILE) + $(CC) -O0 -o $@ c-pp-lite.c $(sqlite3.c) '-DCMPP_DEFAULT_DELIM="//#"' -I$(dir.top) \ + -DSQLITE_OMIT_LOAD_EXTENSION -DSQLITE_OMIT_DEPRECATED -DSQLITE_OMIT_UTF16 \ + -DSQLITE_OMIT_SHARED_CACHE -DSQLITE_OMIT_WAL -DSQLITE_THREADSAFE=0 \ + -DSQLITE_TEMP_STORE=3 +DISTCLEAN_FILES += $(bin.c-pp) +b.c-pp.target.flags ?= +ifeq (1,$(SQLITE_C_IS_SEE)) + b.c-pp.target.flags += -Denable-see +endif + +# cflags.common = C compiler flags for all builds +cflags.common = -I. -I$(dir $(sqlite3.c)) -std=c99 -fPIC +# emcc.WASM_BIGINT = 1 for BigInt (C int64) support, else 0. The API +# disables certain features if BigInt is not enabled and such builds +# _are not tested_ on any regular basis. +emcc.WASM_BIGINT ?= 1 +emcc.MEMORY64 ?= 0 +######################################################################## +# https://emscripten.org/docs/tools_reference/settings_reference.html#memory64 +# +# 64-bit build requires wasm-strip 1.0.36 (maybe 1.0.35, but not +# 1.0.34) or will fail to strip with "tables may not be 64-bit". +######################################################################## + +# emcc_opt = optimization-related flags. These are primarily used by +# the various oX targets. build times for -O levels higher than 0 are +# painful at dev-time. +# +# When running any of the $(OPTIMIZED_TARGETS) explicitly, e.g. for a +# release distribution, use a higher optimization level. Experience +# has shown -Oz to produce the smallest deliverables with only a +# roughly 10% performance hit in the resulting WASM file compared to +# -O2 (which consistently creates the fastest-running deliverables). +# Build time suffers greatly compared to -O0, which is why -O0 is the +# default. +ifeq (,$(filter $(OPTIMIZED_TARGETS),$(MAKECMDGOALS))) + emcc_opt ?= -O0 +else + emcc_opt ?= -Oz +endif + +# When passing emcc_opt from the CLI, += and re-assignment have no +# effect, so emcc_opt+=-g3 doesn't work. So... +emcc_opt_full = $(emcc_opt) -g3 +# ^^^ ALWAYS use -g3. See below for why. +# +# ^^^ -flto improves runtime speed at -O0 considerably but doubles +# build time. +# +# ^^^^ (-O3, -Oz, -Os) all minify symbol names and there appears to be +# no way around that except to use -g3, but -g3 causes the binary file +# size to absolutely explode (approx. 5x larger). This minification +# utterly breaks the resulting module, making it unsable except as +# self-contained/self-referential-only code, as ALL of the exported +# symbols get minified names. +# +# However, we have an option for using -Oz or -Os: +# +# Build with (-Os -g3) or (-Oz -g3) then use wasm-strip, from the wabt +# tools package (https://github.com/WebAssembly/wabt), to strip the +# debugging symbols. That results in a small build with unmangled +# symbol names. -Oz gives ever-so-slightly better compression than +# -Os: not quite 1% in some completely unscientific tests. Runtime +# speed for the unit tests is all over the place either way so it's +# difficult to say whether -Os gives any speed benefit over -Oz. +# +# Much practice has demonstrated that -O2 consistently gives the best +# runtime speeds, but not by a large enough factor to rule out use of +# -Oz when smaller deliverable size is a priority. +######################################################################## + +######################################################################## +# EXPORTED_FUNCTIONS.* = files for use with Emscripten's +# -sEXPORTED_FUNCTION flag. +EXPORTED_FUNCTIONS.api.core = $(dir.api)/EXPORTED_FUNCTIONS.sqlite3-core +EXPORTED_FUNCTIONS.api.in = $(EXPORTED_FUNCTIONS.api.core) +ifeq (1,$(SQLITE_C_IS_SEE)) + EXPORTED_FUNCTIONS.api.in += $(dir.api)/EXPORTED_FUNCTIONS.sqlite3-see +endif +ifeq (0,$(wasm-bare-bones)) + EXPORTED_FUNCTIONS.api.in += $(dir.api)/EXPORTED_FUNCTIONS.sqlite3-extras +endif +EXPORTED_FUNCTIONS.api = $(dir.tmp)/EXPORTED_FUNCTIONS.api +$(EXPORTED_FUNCTIONS.api): $(EXPORTED_FUNCTIONS.api.in) $(sqlite3.c) $(MAKEFILE) + @$(call b.mkdir@) + cat $(EXPORTED_FUNCTIONS.api.in) > $@ + +######################################################################## +# emcc flags for .c/.o/.wasm/.js. +emcc.flags = +ifeq (1,$(emcc.verbose)) +emcc.flags += -v +# -v is _very_ loud but also informative about what it's doing +endif + +# +# emcc flags for .c/.o. +# +emcc.cflags = +emcc.cflags += -std=c99 -fPIC +# -------------^^^^^^^^ we need c99 for $(sqlite3-wasm.c), primarily +# for variadic macros and snprintf() to implement +# sqlite3__wasm_enum_json(). +emcc.cflags += -I. -I$(dir.top) + +# +# emcc flags specific to building .js/.wasm files... +# +emcc.jsflags = -fPIC +#emcc.jsflags += -Wno-gcc-install-dir-libstdcxx +#emcc is not passing ^^^ this on to clang +emcc.jsflags += --no-entry +emcc.jsflags += -sWASM_BIGINT=$(emcc.WASM_BIGINT) +emcc.jsflags += -sMODULARIZE +emcc.jsflags += -sDYNAMIC_EXECUTION=0 +emcc.jsflags += -sNO_POLYFILL +emcc.jsflags += -sEXPORTED_FUNCTIONS=@$(EXPORTED_FUNCTIONS.api) +emcc.exportedRuntimeMethods = \ + -sEXPORTED_RUNTIME_METHODS=wasmMemory +# wasmMemory ==> required by our code for use with -sIMPORTED_MEMORY +# Emscripten 4.0.7 (2025-04-15) stops exporting HEAP* by default. +emcc.jsflags += $(emcc.exportedRuntimeMethods) +emcc.jsflags += -sUSE_CLOSURE_COMPILER=0 +emcc.jsflags += -sIMPORTED_MEMORY +ifeq (,$(filter -O0,$(emcc_opt))) +emcc.assert ?= 0 +else +emcc.assert ?= 2 +endif +emcc.jsflags += -sASSERTIONS=$(emcc.assert) +emcc.jsflags += -sSTRICT_JS=0 +# STRICT_JS disabled due to: +# https://github.com/emscripten-core/emscripten/issues/18610 +# TL;DR: does not work with MODULARIZE or EXPORT_ES6 as of version +# 3.1.31. The fix for that in newer emcc's is to throw a built-time +# error if STRICT_JS is used together with those options. + +# emcc.jsflags += -sSTRICT=1 +# -sSTRICT=1 Causes failures about unknown symbols which the build +# tools should be installing, e.g. __syscall_geteuid32 + +# +# -sINITIAL_MEMORY: How much memory we need to start with is governed +# at least in part by whether -sALLOW_MEMORY_GROWTH is enabled. If so, +# we can start with less. If not, we need as much as we'll ever +# possibly use (which, of course, we can't know for sure). speedtest1 +# shows that performance for even moderate workloads MAY suffer +# considerably if we start small and have to grow at runtime. +# e.g. OPFS-backed (speedtest1 --size 75) take MAY take X time with +# 16mb+ memory and 3X time when starting with 8MB. However, such test +# results are inconsistent due to browser internals which are opaque +# to us. +# +# 2024-03-04: emsdk 3.1.55 replaces INITIAL_MEMORY with INITIAL_HEAP, +# but also says (in its changelog): "Note that it is currently not +# supported in all configurations (#21071)." +# https://github.com/emscripten-core/emscripten/blob/main/ChangeLog.md +# +# 2025-09-25: it turns out that _this_ WASM's heap size is not +# affected by Emscripten's in-memory virtual filesystem, so we don't +# strictly need a lot of heap. Resizing the heap is slow, though, so +# we want to start off with some room to grow. +# +emcc.jsflags += -sALLOW_MEMORY_GROWTH +emcc.INITIAL_MEMORY.128 = 134217728 +emcc.INITIAL_MEMORY.96 = 100663296 +emcc.INITIAL_MEMORY.64 = 67108864 +emcc.INITIAL_MEMORY.32 = 33554432 +emcc.INITIAL_MEMORY.16 = 16777216 +emcc.INITIAL_MEMORY.8 = 8388608 +emcc.INITIAL_MEMORY.4 = 4194304 +emcc.INITIAL_MEMORY ?= 8 +ifeq (,$(emcc.INITIAL_MEMORY.$(emcc.INITIAL_MEMORY))) +$(error emcc.INITIAL_MEMORY must be one of: 4, 8, 16, 32, 64, 96, 128 (megabytes)) +endif +emcc.jsflags += -sINITIAL_MEMORY=$(emcc.INITIAL_MEMORY.$(emcc.INITIAL_MEMORY)) +# /INITIAL_MEMORY +######################################################################## +#emcc.jsflags += -sMEMORY64=$(emcc.MEMORY64) + +emcc.jsflags += $(emcc.environment) +emcc.jsflags += -sSTACK_SIZE=512KB +# ^^^ ACHTUNG: emsdk 3.1.27 reduced the default stack size from 5MB to +# a mere 64KB, which leads to silent memory corruption via the kvvfs +# VFS, which requires twice that for its xRead() and xWrite() methods. +# 2023-03: those methods have since been adapted to use a malloc()'d +# buffer. +######################################################################## +# $(sqlite3.js.init-func) is the name Emscripten assigns our exported +# module init/load function. This symbol name is hard-coded in +# $(extern-post-js.js) as well as in numerous docs. +# +# "sqlite3InitModule" is the symbol we document for client use, so +# that's the symbol name which must be exported, whether it comes from +# Emscripten or our own code in extern-post-js.js. +# +# That said... we can change $(sqlite3.js.init-func) as long as the +# name "sqlite3InitModule" is the one which gets exposed via the +# resulting JS files. That can be accomplished via +# extern-post-js.js. However... using a temporary symbol name here +# and then adding sqlite3InitModule() ourselves results in 2 global +# symbols: we cannot "delete" the Emscripten-defined +# $(sqlite3.js.init-func) from vanilla builds (as opposed to ESM +# builds) because it's declared with "var". +sqlite3.js.init-func = sqlite3InitModule +emcc.jsflags += -sEXPORT_NAME=$(sqlite3.js.init-func) +emcc.jsflags += -sGLOBAL_BASE=4096 # HYPOTHETICALLY keep func table indexes from overlapping w/ heap addr. +#emcc.jsflags += -sSTRICT # fails due to missing __syscall_...() +#emcc.jsflags += -sALLOW_UNIMPLEMENTED_SYSCALLS +#emcc.jsflags += -sFILESYSTEM=0 # only for experimentation. fiddle needs the FS API +#emcc.jsflags += -sABORTING_MALLOC # only for experimentation +emcc.jsflags += -sALLOW_TABLE_GROWTH +# ^^^^ -sALLOW_TABLE_GROWTH is required for installing new SQL UDFs +emcc.jsflags += -Wno-limited-postlink-optimizations +# ^^^^ emcc likes to warn when we have "limited optimizations" via the +# -g3 flag. +# emcc.jsflags += -sSTANDALONE_WASM # causes OOM errors, not sure why. + +# Re. undefined symbol handling, see: https://lld.llvm.org/WebAssembly.html +emcc.jsflags += -sERROR_ON_UNDEFINED_SYMBOLS=1 +emcc.jsflags += -sLLD_REPORT_UNDEFINED +#emcc.jsflags += --allow-undefined +#emcc.jsflags += --import-undefined +#emcc.jsflags += --unresolved-symbols=import-dynamic --experimental-pic +#emcc.jsflags += --experimental-pic --unresolved-symbols=ingore-all --import-undefined +#emcc.jsflags += --unresolved-symbols=ignore-all + +######################################################################## +# -sSINGLE_FILE: +# https://github.com/emscripten-core/emscripten/blob/main/src/settings.js +# +# -sSINGLE_FILE=1 would be _really_ nice but we have to build with -g3 +# for -O2 and higher to work (else minification breaks the code) and +# cannot wasm-strip the binary before it gets encoded into the JS +# file. The result is that the generated JS file is, because of the +# -g3 debugging info, _huge_. +######################################################################## + + +sqlite3.wasm = $(dir.dout)/sqlite3.wasm +sqlite3-wasm.c = $(dir.api)/sqlite3-wasm.c +sqlite3-wasm.c.in = $(sqlite3-wasm.c) $(sqlite3_wasm_extra_init.c) + +# +# b.call.patch-export-default is used by mkwasmbuilds.c and the +# wasmfs build. $1 is 1 if the build mode needs this workaround +# (modes: esm, bundler-friendly, node) and 0 if not (vanilla). $2 must +# be 0 for all builds except sqlite3-wasmfs.mjs, in which case it must +# be 1. $(3) is an optional log prefix, defaulting to $(logtag.@) +# +# Reminder for ESM builds: even if we use -sEXPORT_ES6=0, emcc _still_ +# adds: +# +# export default $(sqlite3.js.init-func); +# +# when building *.mjs, which is bad because we need to export an +# overwritten version of that function and cannot "export default" +# twice. Because of this, we have to sed *.mjs to remove the _first_ +# instance (only) of /^export default/. +# +# Upstream RFE: +# https://github.com/emscripten-core/emscripten/issues/18237 +# +# Maintenance reminder: Mac sed works differently than GNU sed, so we +# use awk instead of sed for this. +# +define b.call.patch-export-default +if [ x1 = x$(1) ]; then \ + echo "$(if $(3),$(3),$(logtag.@)) $(emo.bug) Fragile" \ + "workaround for emscripten/issues/18237." \ + "See b.call.patch-export-default."; \ + {\ + awk '/^export default/ && !f{f=1; next} 1' $@ \ + > $@.tmp && mv $@.tmp $@; \ + } || exit; \ + if [ x1 = x$(2) ]; then \ + if ! grep -q '^export default' $@; then \ + echo "Cannot find export default." 1>&2; \ + exit 1; \ + fi; \ + fi; \ +fi +endef + +# +# The various -D... values used by *.c-pp.js include: +# +# -Dtarget:es6-module: for all ESM module builds +# +# -Dtarget:node: for node.js builds +# +# -Dtarget:es6-module -Dtarget:es6-bundler-friendly: intended for +# "bundler-friendly" ESM module build. These have some restrictions +# on how URL() objects are constructed in some contexts: URLs which +# refer to files which are part of this project must be referenced +# as string literals so that bundlers' static-analysis tools can +# find those files and include them in their bundles. +# +# -Dtarget:es6-module -Dtarget:node: is intended for use by node.js +# for node.js, as opposed to by node.js on behalf of a +# browser. Mixing -sENVIRONMENT=web and -sENVIRONMENT=node leads to +# ambiguity and confusion on node's part, as it's unable to +# reliably determine whether the target is a browser or node. +# +# To repeat: all node.js builds are 100% untested and unsupported. +# +######################################################################## + +# +# Inputs/outputs for the sqlite3-api.js family. +# +# sqlite3-api.jses = the list of JS files which make up +# sqlite3-api.js, in the order they need to be assembled. +sqlite3-api.jses = $(sqlite3-license-version.js) +sqlite3-api.jses += $(dir.api)/sqlite3-api-prologue.js +sqlite3-api.jses += $(dir.common)/whwasmutil.js +sqlite3-api.jses += $(dir.jacc)/jaccwabyt.js +sqlite3-api.jses += $(dir.api)/sqlite3-api-glue.c-pp.js +sqlite3-api.jses += $(sqlite3-api-build-version.js) +sqlite3-api.jses += $(dir.api)/sqlite3-api-oo1.c-pp.js +sqlite3-api.jses += $(dir.api)/sqlite3-api-worker1.c-pp.js +sqlite3-api.jses += $(dir.api)/sqlite3-vfs-helper.c-pp.js +ifeq (0,$(wasm-bare-bones)) + sqlite3-api.jses += $(dir.api)/sqlite3-vtab-helper.c-pp.js +endif +sqlite3-api.jses += $(dir.api)/sqlite3-vfs-opfs.c-pp.js +sqlite3-api.jses += $(dir.api)/sqlite3-vfs-opfs-sahpool.c-pp.js +sqlite3-api.jses += $(dir.api)/sqlite3-api-cleanup.js + +# +# $(sqlite3-license-version.js) contains the license header and +# in-comment build version info. +# +# Maintenance reminder: there are awk binaries out there which do not +# support -e SCRIPT. +# +sqlite3-license-version.js = $(dir.tmp)/sqlite3-license-version.js +$(sqlite3-license-version.js): $(bin.version-info) \ + $(dir.api)/sqlite3-license-version-header.js + @echo '$(logtag.@) $(emo.disk)'; { \ + $(call b.mkdir@); \ + cat $(dir.api)/sqlite3-license-version-header.js || exit $$?; \ + echo '/*'; \ + echo '** This code was built from sqlite3 version...'; \ + echo '**'; \ + awk '/define SQLITE_VERSION/{$$1=""; print "**" $$0}' $(sqlite3.h); \ + awk '/define SQLITE_SOURCE_ID/{$$1=""; print "**" $$0}' $(sqlite3.h); \ + echo '**'; echo '** Emscripten SDK: $(emcc.version)'; \ + echo '**'; \ + echo '*/'; \ + } > $@ + +# +# $(sqlite3-api-build-version.js) injects the build version info into +# the bundle in JSON form. +# +sqlite3-api-build-version.js = $(dir.tmp)/sqlite3-api-build-version.js +$(sqlite3-api-build-version.js): $(bin.version-info) $(MAKEFILE) + @echo '$(logtag.@) $(emo.disk)'; { \ + $(call b.mkdir@); \ + echo 'globalThis.sqlite3ApiBootstrap.initializers.push(function(sqlite3){'; \ + echo -n ' sqlite3.version = '; \ + $(bin.version-info) --json; \ + echo ';'; \ + echo '});'; \ + } > $@ + +# +# extern-post-js* and extern-pre-js* are files for use with +# Emscripten's --extern-pre-js and --extern-post-js flags. +# +extern-pre-js.js = $(dir.api)/extern-pre-js.js +extern-post-js.in.js = $(dir.api)/extern-post-js.c-pp.js + +# +# Emscripten flags for --[extern-][pre|post]-js=... for the +# various builds. +# pre-post-jses.*.deps = lists of dependencies for the +# --[extern-][pre/post]-js files. +# +pre-post-jses.common.deps = $(extern-pre-js.js) $(sqlite3-license-version.js) + +# --post-js and --pre-js are emcc flags we use to append/prepend JS to +# the generated emscripten module file. These rules set up the core +# pre/post files for use by the various builds. --pre-js is used to +# inject code which needs to run as part of the pre-WASM-load phase. +# --post-js injects code which runs after the WASM module is loaded +# and includes the entirety of the library plus some +# Emscripten-specific post-bootstrapping code. +pre-js.in.js = $(dir.api)/pre-js.c-pp.js +post-js.in.js = $(dir.tmp)/post-js.c-pp.js +post-jses.js = \ + $(dir.api)/post-js-header.js \ + $(sqlite3-api.jses) \ + $(dir.api)/post-js-footer.js +$(post-js.in.js): $(MKDIR.bld) $(post-jses.js) $(MAKEFILE) + @$(call b.echo,@,$(emo.disk)); \ + for i in $(post-jses.js); do \ + echo "/* BEGIN FILE: $$i */"; \ + cat $$i || exit $$?; \ + echo "/* END FILE: $$i */"; \ + done > $@ + +# +# speedtest1 decls needed before the $(bin.mkws)-generated makefile +# is included. +# +bin.speedtest1 = ../../speedtest1 +speedtest1.c = ../../test/speedtest1.c +speedtest1.c.in = $(speedtest1.c) $(sqlite3-wasm.c) +EXPORTED_FUNCTIONS.speedtest1 = $(abspath $(dir.tmp)/EXPORTED_FUNCTIONS.speedtest1) + +# +# fiddle build flags +# +# Flags specifically for debug builds of fiddle. Performance suffers +# greatly in debug builds. +######################################################################## +# shell.c and its build flags... +# +# We should ideally collect these from ../../configure and past +# them in ./config.make. The problem with that is that SHELL_OPT is +# generated at make-time, not configure-time. +ifneq (1,$(MAKING_CLEAN)) + make-np-0 = make -C $(dir.top) -n -p + make-np-1 = sed -e 's/(TOP)/(dir.top)/g' + # Extract SHELL_OPT and SHELL_DEP from the top-most makefile and import + # them as vars here... + $(eval $(shell $(make-np-0) | grep -e '^SHELL_OPT ' | $(make-np-1))) + $(eval $(shell $(make-np-0) | grep -e '^SHELL_DEP ' | $(make-np-1))) + # ^^^ can't do that in 1 invocation b/c newlines get stripped + ifeq (,$(SHELL_OPT)) + $(error Could not parse SHELL_OPT from $(dir.top)/Makefile.) + endif + ifeq (,$(SHELL_DEP)) + $(error Could not parse SHELL_DEP from $(dir.top)/Makefile.) + endif +$(dir.top)/shell.c: $(SHELL_DEP) $(dir.tool)/mkshellc.tcl $(sqlite3.c) + $(MAKE) -C $(dir.top) shell.c +endif +# /shell.c +######################################################################## + +EXPORTED_FUNCTIONS.fiddle = $(dir.tmp)/EXPORTED_FUNCTIONS.fiddle +$(EXPORTED_FUNCTIONS.fiddle): $(fiddle.EXPORTED_FUNCTIONS.in) $(MAKEFILE_LIST) + @$(b.mkdir@) + @sort -u $(fiddle.EXPORTED_FUNCTIONS.in) > $@ + @echo $(logtag.@) $(emo.disk) + +emcc.flags.fiddle = \ + $(emcc.cflags) $(emcc_opt_full) \ + --minify 0 \ + -sALLOW_TABLE_GROWTH \ + -sMEMORY64=$(emcc.MEMORY64) \ + -sINITIAL_MEMORY=$(emcc.INITIAL_MEMORY.8) \ + -sABORTING_MALLOC \ + -sSTRICT_JS=0 \ + -sENVIRONMENT=web,worker \ + -sMODULARIZE \ + -sDYNAMIC_EXECUTION=0 \ + -sWASM_BIGINT=$(emcc.WASM_BIGINT) \ + -sEXPORT_NAME=$(sqlite3.js.init-func) \ + -Wno-limited-postlink-optimizations \ + $(emcc.exportedRuntimeMethods),FS \ + -sEXPORTED_FUNCTIONS=@$(abspath $(EXPORTED_FUNCTIONS.fiddle)) \ + $(SQLITE_OPT.full-featured) \ + $(SQLITE_OPT.common) \ + $(SHELL_OPT) \ + -UHAVE_READLINE -UHAVE_EDITLINE -UHAVE_LINENOISE \ + -USQLITE_HAVE_ZLIB \ + -USQLITE_WASM_BARE_BONES \ + -DSQLITE_SHELL_FIDDLE + +clean: clean-fiddle +clean-fiddle: + rm -f $(dir.fiddle)/fiddle-module.js \ + $(dir.fiddle)/*.wasm \ + $(dir.fiddle)/sqlite3-opfs-*.js \ + $(dir.fiddle)/*.gz \ + EXPORTED_FUNCTIONS.fiddle + rm -fr $(dir.fiddle-debug) + +emcc.flags.fiddle.debug = $(emcc.flags.fiddle) \ + -DSQLITE_DEBUG \ + -DSQLITE_ENABLE_SELECTTRACE \ + -DSQLITE_ENABLE_WHERETRACE + +fiddle.EXPORTED_FUNCTIONS.in = \ + EXPORTED_FUNCTIONS.fiddle.in \ + $(dir.api)/EXPORTED_FUNCTIONS.sqlite3-core \ + $(dir.api)/EXPORTED_FUNCTIONS.sqlite3-extras + +fiddle.c.in = $(dir.top)/shell.c $(sqlite3-wasm.c) + +# +# WASMFS build - unsupported and untested. We used WASMFS +# to jumpstart development early on, but it has always been +# a moving target, in that Emscripten updates have broken +# our build often enough that we no longer actively support it. +# It's interesting to keep around, though. +# +# Only add wasmfs if wasmfs.enable=1 or we're running (dist)clean +# +ifneq (,$(filter wasmfs b-wasmfs for-testing,$(MAKECMDGOALS))) +wasmfs.enable ?= 1 +else +# Unconditionally enable wasmfs for [dist]clean so that the wasmfs +# sub-make can clean up. +wasmfs.enable ?= $(MAKING_CLEAN) +endif +cflags.wasmfs = -DSQLITE_ENABLE_WASMFS +# end wasmfs (the rest is in mkwasmbuilds.c) +# + +# +# $(bin.mkwb) is used for generating much of the makefile code for the +# various wasm builds. It used to be generated in this makefile via a +# difficult-to-read/maintain block of $(eval)'d code. Attempts were +# made to generate it from tcl and bash (shell) but having to escape +# the $ references in those languages made it just as illegible as the +# native makefile code. Somewhat surprisingly, moving that code +# generation to C makes it slightly less illegible than the previous 3 +# options. +# +# Maintenance notes: +# +# - Ordering of this block within this file is fragile. The generated +# makefile sets up many vars which are useful for the other targets. +# +# - Vars which are used by $(bin.mkwb) in dependency lists and such +# need to be defined before this is included. Those used in recipies +# may be defined after this step. +# +bin.mkwb = ./mkwasmbuilds +ifneq (1,$(MAKING_CLEAN)) +$(bin.mkwb): $(bin.mkwb).c $(MAKEFILE) + $(CC) -O0 -g -std=c99 -o $@ $< -DWASM_CUSTOM_INSTANTIATE=$(WASM_CUSTOM_INSTANTIATE) + +.wasmbuilds.make: $(bin.mkwb) + @rm -f $@ + $(bin.mkwb) > $@ + @chmod -w $@ +-include .wasmbuilds.make +endif +CLEAN_FILES += .wasmbuilds.make $(bin.mkwb) + +# +# $(sqlite3.ext.js) = API-related files which are standalone +# files, not part of the amalgamation. This list holds +# the name of each such _output_ file. +# +sqlite3.ext.js = + +######################################################################## +# We need separate copies of certain supplementary JS files for the +# bundler-friendly build. Concretely, any supplemental JS files which +# themselves use importScripts() or Workers or URL() constructors +# which refer to other in-tree (m)JS files require a bundler-friendly +# copy. Bundler-friendly builds replace certain references to string +# vars/expressions with string literals, as bundler tools are static +# code analyzers and cannot cope with the former. + +# +# sqlite3-worker1*.* +# TODO: 64-bit +# +define gen-worker1 +# $1 = X.ext part of sqlite3-worker1X.ext +# $2 = $(c-pp.D.NAME) +$(call b.c-pp.target,filter,$(dir.api)/sqlite3-worker1.c-pp.js,\ + $(dir.dout)/sqlite3-worker1$(1),$(2)) +sqlite3.ext.js += $(dir.dout)/sqlite3-worker1$(1) +all: $(dir.dout)/sqlite3-worker1$(1) +endef + +$(eval $(call gen-worker1,.js,$(c-pp.D.vanilla))) +$(eval $(call gen-worker1,.mjs,$(c-pp.D.esm))) +$(eval $(call gen-worker1,-bundler-friendly.mjs,$(c-pp.D.bundler))) + +# +# sqlite3-worker1-promiser*.* +# TODO: 64-bit +# +define gen-promiser +# $1 = X.ext part of sqlite3-worker1-promiserX.ext +# $2 = $(c-pp.D.NAME) +$(call b.c-pp.target,filter,$(dir.api)/sqlite3-worker1-promiser.c-pp.js,\ + $(dir.dout)/sqlite3-worker1-promiser$(1),$(2)) +sqlite3.ext.js += $(dir.dout)/sqlite3-worker1-promiser$(1) +all: $(dir.dout)/sqlite3-worker1-promiser$(1) +endef + +$(eval $(call gen-promiser,.js,$(c-pp.D.vanilla))) +$(eval $(call gen-promiser,.mjs,$(c-pp.D.esm))) +$(eval $(call gen-promiser,-bundler-friendly.mjs,$(c-pp.D.bundler))) + +# +# demo1-worker-*.*: +# $1 = .js or .mjs +# $2 = .html or -esm.html +# $3 = -D... flags for $(bin.c-pp) +# +define gen-dwp +$(call b.c-pp.target,test,demo-worker1-promiser.c-pp.js,demo-worker1-promiser$(1),$(3)) +$(call b.c-pp.target,test,demo-worker1-promiser.c-pp.html,demo-worker1-promiser$(2),$(3)) +demos: demo-worker1-promiser$(1) demo-worker1-promiser$(2) +endef +$(eval $(call gen-dwp,.js,.html,$(c-pp.D.vanilla))) +$(eval $(call gen-dwp,.mjs,-esm.html,$(c-pp.D.esm))) +all: demos +# End worker/promiser generation +####################################################################### + +# +# "SOAP" is a static file which is not part of the amalgamation but +# gets copied into the build output folder and into each of the fiddle +# builds. +# +sqlite3.ext.js += $(dir.dout)/sqlite3-opfs-async-proxy.js +$(dir.dout)/sqlite3-opfs-async-proxy.js: $(dir.api)/sqlite3-opfs-async-proxy.js + @$(call b.cp,@,$<,$@) + +# +# Add a dep of $(sqlite3.ext.js) on every individual build's JS file. +# The primary purpose of this is to force them to be copied early in +# the build process, which is sometimes a time-saver during +# development, allowing the developer to reload a test page while +# other parts of the build are still running. Another reason is that +# we don't otherwise have a great place to attach them such that +# they're always copied when we need them. +# +$(foreach B,$(b.names),$(eval $(out.$(B).js): $(sqlite3.ext.js))) +# +# b-all: builds all available js/wasm builds. +# +$(foreach B,$(b.names),$(eval b-all: $(out.$(B).js))) + +# +# speedtest1 is our primary benchmarking tool. +# +# emcc.speedtest1.common = emcc flags used by multiple builds of speedtest1 +# emcc.speedtest1 = emcc flags used by the main build of speedtest1 +# +# These flags get applied via $(bin.mkwb). +emcc.speedtest1.common = $(emcc_opt_full) +emcc.speedtest1 = -I. -I$(dir $(sqlite3.canonical.c)) +emcc.speedtest1 += -sENVIRONMENT=web +emcc.speedtest1 += -sALLOW_MEMORY_GROWTH +emcc.speedtest1 += -sINITIAL_MEMORY=$(emcc.INITIAL_MEMORY.32) +emcc.speedtest1.common += -sINVOKE_RUN=0 +emcc.speedtest1.common += --no-entry +emcc.speedtest1.common += -sABORTING_MALLOC +emcc.speedtest1.common += -sSTRICT_JS=0 +emcc.speedtest1.common += -sMODULARIZE +emcc.speedtest1.common += -Wno-limited-postlink-optimizations +emcc.speedtest1.common += -Wno-unused-main +# ^^^^ -Wno-unused-main is for emcc 3.1.52+. speedtest1 has a wasm_main() which is +# exported and called by the JS code. +emcc.speedtest1.common += -sSTACK_SIZE=512KB +emcc.speedtest1.common += -sEXPORTED_FUNCTIONS=@$(EXPORTED_FUNCTIONS.speedtest1) +emcc.speedtest1.common += $(emcc.exportedRuntimeMethods) +emcc.speedtest1.common += -sALLOW_TABLE_GROWTH +emcc.speedtest1.common += -sDYNAMIC_EXECUTION=0 +emcc.speedtest1.common += --minify 0 +emcc.speedtest1.common += -sEXPORT_NAME=$(sqlite3.js.init-func) +emcc.speedtest1.common += -sWASM_BIGINT=$(emcc.WASM_BIGINT) +speedtest1.exit-runtime0 = -sEXIT_RUNTIME=0 +speedtest1.exit-runtime1 = -sEXIT_RUNTIME=1 +# Re -sEXIT_RUNTIME=1 vs 0: if it's 1 and speedtest1 crashes, we get +# this error from Emscripten: +# +# > native function `free` called after runtime exit (use +# NO_EXIT_RUNTIME to keep it alive after main() exits)) +# +# If it's 0 and it crashes, we get: +# +# > stdio streams had content in them that was not flushed. you should +# set EXIT_RUNTIME to 1 (see the FAQ), or make sure to emit a newline +# when you printf etc. +# +# and pending output is not flushed because it didn't end with a +# newline (by design). The lesser of the two evils seems to be +# -sEXIT_RUNTIME=1 but we need EXIT_RUNTIME=0 for the worker-based app +# which runs speedtest1 multiple times. + +$(EXPORTED_FUNCTIONS.speedtest1): $(EXPORTED_FUNCTIONS.api.core) + @$(call b.echo,@,$(emo.disk)); \ + $(call b.mkdir@); \ + { echo _wasm_main; cat $(EXPORTED_FUNCTIONS.api.core); } > $@ || exit +speedtest1: b-speedtest1 + +# +# Generate 64-bit variants of speedtest1*.{js,html} +# +define gen-st64 +$(2): $(1) + @$$(call b.echo,speedtest164,$$(emo.disk)$(emo.lock) Creating from $$<) + @rm -f $$@; \ + sed -e 's/$(3)\.js/$(3)-64bit\.js/' < $$< > $$@; \ + chmod -w $$@ +b-speedtest164: $(2) +CLEAN_FILES += $(2) +endef + +$(eval $(call gen-st64,speedtest1.html,speedtest1-64bit.html,speedtest1)) +$(eval $(call gen-st64,speedtest1-worker.html,speedtest1-worker-64bit.html,speedtest1-worker)) +$(eval $(call gen-st64,speedtest1-worker.js,speedtest1-worker-64bit.js,speedtest1-worker)) +# end speedtest1.js +######################################################################## + +# +# tester1 is the main unit and regression test application and needs +# to be able to run in 4 separate modes to cover the primary +# client-side use cases: +# +# 1) Load sqlite3 in the main UI thread of a conventional script. +# 2) Load sqlite3 in a conventional Worker thread. +# 3) Load sqlite3 as an ES6 module (ESM) in the main thread. +# 4) Load sqlite3 as an ESM worker. (Not all browsers support this.) +# +# To that end, we require two separate builds of tester1.js: +# +# tester1.js: cases 1 and 2 +# tester1.mjs: cases 3 and 4 +# +# Then we need those again in 64-bit builds, which require a 64-bit +# pair of js/wasm files. +# +# To create those, we filter tester1.c-pp.js/html with $(bin.c-pp)... + +# tester1.js variants: +define gen-tester1.js +# $1 = build name to have dep on +# $2 = suffix for tester1SUFFIX JS +# $3 = $(bin.c-pp) flags +$(call b.c-pp.target,test,tester1.c-pp.js,tester1$(2),$(3)) +tester1$(2): $(sqlite3.ext.js) $(out.$(1).wasm) +tester1-$(1): tester1$(2) +tester1: tester1$(2) +endef + +$(eval $(call gen-tester1.js,vanilla,.js, \ + $(c-pp.D.vanilla) \ + -Dsqlite3.js=$(dir.dout)/sqlite3.js)) +$(eval $(call gen-tester1.js,vanilla64,-64bit.js, \ + $(c-pp.D.vanilla64) \ + -Dsqlite3.js=$(dir.dout)/sqlite3-64bit.js)) +$(eval $(call gen-tester1.js,esm,.mjs, \ + $(c-pp.D.esm) \ + -Dsqlite3.js=$(dir.dout)/sqlite3.mjs)) +$(eval $(call gen-tester1.js,esm64,-64bit.mjs, \ + $(c-pp.D.esm64) \ + -Dsqlite3.js=$(dir.dout)/sqlite3-64bit.mjs)) + +# tester1.html variants: +define gen-tester1.html +# $1 = build name to have a dep on +# $2 = filename suffix: empty, -64bit, -esm, esm-64bit +# $3 = $(bin.c-pp) flags +$(call b.c-pp.target,test,tester1.c-pp.html,tester1$(2).html,$(3)) +tester1$(2).html: tester1-$(1) +tester1: tester1$(2).html +endef + +$(eval $(call gen-tester1.html,vanilla,,\ + $(c-pp.D.vanilla) \ + -Dbitness=32 \ + -Dtitle="UI thread" \ + -Dtester1.js=tester1.js \ + -Dsqlite3.js=$(dir.dout)/sqlite3.js)) +$(eval $(call gen-tester1.html,vanilla64,-64bit,\ + $(c-pp.D.vanilla64) \ + -Dbitness=64 \ + -Dtitle="UI thread" \ + -Dtester1.js=tester1-64bit.js \ + -Dsqlite3.js=$(dir.dout)/sqlite3-64bit.js)) +$(eval $(call gen-tester1.html,esm,-esm,\ + $(c-pp.D.esm) \ + -Dbitness=32 \ + -Dtitle="ES6 Module in UI thread" \ + -Dtester1.js=tester1.mjs \ + -Dsqlite3.js=$(dir.dout)/sqlite3.mjs)) +$(eval $(call gen-tester1.html,esm64,-esm-64bit,\ + $(c-pp.D.esm64) \ + -Dbitness=64 \ + -Dtitle="ES6 Module in UI thread" \ + -Dtester1.js=tester1-64bit.mjs \ + -Dsqlite3.js=$(dir.dout)/sqlite3-64bit.mjs)) + +# tester1-worker.html variants: +# There is no ESM variant of this file. Instead, that page accepts a +# ?esm URL flag to switch to ESM mode. +$(eval $(call b.c-pp.target,test,tester1-worker.c-pp.html,\ + tester1-worker.html,-Dbitness=32)) +$(eval $(call b.c-pp.target,test,tester1-worker.c-pp.html,\ + tester1-worker-64bit.html,$(c-pp.D.64bit) -Dbitness=64)) +tester1: tester1-worker.html tester1-worker-64bit.html +tester1-worker.html: tester1.mjs +tester1-worker-64bit.html: tester1-64bit.mjs + +all: tester1 +# end tester1 +######################################################################## + +# +# Convenience rules to rebuild with various -Ox levels. Much +# experimentation shows -O2 to be the clear winner in terms of speed. +# -Oz results are significantly smaller and only slightly slower than +# -O2 (very roughly 10% in highly unscientific tests), so -Oz is the +# shipping configuration. +# +# Achtung: build times with anything higher than -O0 are somewhat +# painful, which is why -O0 is the default. +.PHONY: o0 o1 o2 o3 os oz +emcc-opt-extra = +#ifeq (1,$(wasm-bare-bones)) +#emcc-opt-extra += -flto +# ^^^^ -flto can have a considerably performance boost at -O0 but +# doubles the build time and seems to have negligible, if any, effect +# on higher optimization levels. +# +# -flto does not shrink the size of bare-bones builds by any measurable +# amount. +#endif +o0: clean + $(MAKE) -e "emcc_opt=-O0" +o1: clean + $(MAKE) -e "emcc_opt=-O1 $(emcc-opt-extra)" +o2: clean + $(MAKE) -e "emcc_opt=-O2 $(emcc-opt-extra)" +o3: clean + $(MAKE) -e "emcc_opt=-O3 $(emcc-opt-extra)" +os: clean + @echo "$(emo.fire)WARNING$(emo.fire): -Os can result in a build with mysteriously missing pieces!" + $(MAKE) -e "emcc_opt=-Os $(emcc-opt-extra)" +oz: clean + $(MAKE) -e "emcc_opt=-Oz $(emcc-opt-extra)" + +# +# Push files to the public wasm-testing.sqlite.org server. +# +# Ideally only -Oz builds should be pushed, so the practice has become: +# +# make clean +# make -j4 for-testing +# make push-testing +# +wasm-testing.include = *.js *.mjs *.html \ + ./tests \ + $(dir.dout) $(dir.common) $(dir.fiddle) $(dir.fiddle.debug) $(dir.jacc) +wasm-testing.exclude = sql/speedtest1.sql jswasm/*/* +wasm-testing.dir = /jail/sites/wasm-testing +wasm-testing.dest ?= wasm-testing:$(wasm-testing.dir) +# ---------------------^^^^^^^^^^^^ ssh alias +.PHONY: push-testing +push-testing: + rsync -z -e ssh --ignore-times --chown=stephan:www-data --group -r \ + $(patsubst %,--exclude=%,$(wasm-testing.exclude)) \ + $(wasm-testing.include) $(wasm-testing.dest) + @echo "Updating gzipped copies..."; \ + ssh wasm-testing 'cd $(wasm-testing.dir) && bash .gzip' || \ + echo "SSH failed: it's likely that stale content will be served via old gzip files." + +# build everything needed by push-testing with -Oz +.PHONY: for-testing +for-testing: emcc_opt=-Oz +for-testing: loud=1 +for-testing.deps = \ + tester1 demos \ + b-vanilla b-vanilla64 \ + b-esm b-esm64 \ + b-fiddle b-fiddle.debug \ + b-speedtest1 b-speedtest164 \ + b-wasmfs +for-testing: $(for-testing.deps) + +######################################################################## +# If we find a copy of https://sqlite.org/wasm checked out, copy +# certain files over to it, applying some automated edits... +wasm.docs.home ?= ../../../wasm +wasm.docs.found = $(if $(wildcard $(wasm.docs.home)/api-index.md),\ + $(wildcard $(wasm.docs.home)),) +.PHONY: update-docs +ifeq (,$(wasm.docs.found)) +update-docs: + @echo "Cannot find wasm docs checkout."; \ + echo "Pass wasm.docs.home=/path/to/wasm/docs/checkout or edit this makefile to suit."; \ + exit 127 +else +wasm.docs.jswasm = $(wasm.docs.home)/jswasm +update-docs: $(bin.stripccomments) $(out.sqlite3.js) $(out.sqlite3.wasm) + @echo "Copying files to the /wasm docs. Be sure to use an -Oz build for this!"; + cp -p $(sqlite3.wasm) $(wasm.docs.jswasm)/. + $(bin.stripccomments) -k -k < $(out.vanilla.js) \ + | sed -e '/^[ \t]*$$/d' > $(wasm.docs.jswasm)/sqlite3.js + cp -p demo-123.js demo-123.html demo-123-worker.html $(wasm.docs.home)/. + sed -n -e '/EXTRACT_BEGIN/,/EXTRACT_END/p' \ + module-symbols.html > $(wasm.docs.home)/module-symbols.html +endif +# end /wasm docs +######################################################################## + +# Run local web server for the test/demo pages. +httpd: + althttpd -max-age 1 -enable-sab 1 -page index.html + +######################################################################## +# fiddle_remote is the remote destination for the fiddle app. It must +# be a [user@]HOST:/path for rsync. The target "should probably" +# contain a symlink of index.html -> fiddle.html. +fiddle_remote ?= +ifeq (,$(fiddle_remote)) +ifneq (,$(wildcard /home/stephan)) + fiddle_remote = wh:www/wasm-testing/fiddle/. +else ifneq (,$(wildcard /home/drh)) + #fiddle_remote = if appropriate, add that user@host:/path here +endif +endif +push-fiddle: fiddle + @if [ x = "x$(fiddle_remote)" ]; then \ + echo "fiddle_remote must be a [user@]HOST:/path for rsync"; \ + exit 1; \ + fi + rsync -va fiddle/ $(fiddle_remote) +# end fiddle remote push +######################################################################## + +.PHONY: clean distclean +clean: + -rm -f $(CLEAN_FILES) + -rm -fr $(dir.dout) $(dir.tmp) + +distclean: clean + -rm -f $(DISTCLEAN_FILES) + +CLEAN_FILES += *~ $(dir.jacc)/*~ $(dir.api)/*~ $(dir.common)/*~ $(dir.fiddle)/*~ \ + +######################################################################## +# Create main client downloadable zip file: +ifneq (,$(filter dist snapshot,$(MAKECMDGOALS))) +ifeq (1,$(SQLITE_C_IS_SEE)) +dist-name-extra = -see +else +dist-name-extra = +endif +dist-name-prefix = sqlite-wasm$(dist-name-extra) +.PHONY: dist +dist: + ./mkdist.sh $(dist-name-prefix) +snapshot: + ./mkdist.sh $(dist-name-prefix) --snapshot +endif +# ^^^ making dist/snapshot +CLEAN_FILES += $(wildcard sqlite-wasm-*.zip) + +######################################################################## +# Explanation of, and some commentary on, various emcc build flags +# follows. Full docs for these can be found at: +# +# https://github.com/emscripten-core/emscripten/blob/main/src/settings.js +# +# -sENVIRONMENT=web: elides bootstrap code related to non-web JS +# environments like node.js. Removing this makes the output a tiny +# tick larger but hypothetically makes it more portable to +# non-browser JS environments. +# +# -sMODULARIZE: changes how the generated code is structured to avoid +# declaring a global Module object and instead installing a function +# which loads and initializes the module. The function is named... +# +# -sEXPORT_NAME=jsFunctionName (see -sMODULARIZE) +# +# -sEXPORTED_RUNTIME_METHODS=@/absolute/path/to/file: a file +# containing a list of emscripten-supplied APIs, one per line, which +# must be exported into the generated JS. Must be an absolute path! +# +# -sEXPORTED_FUNCTIONS=@/absolute/path/to/file: a file containing a +# list of C functions, one per line, which must be exported via wasm +# so they're visible to JS. C symbols names in that file must all +# start with an underscore for reasons known only to the emcc +# developers. e.g., _sqlite3_open_v2 and _sqlite3_finalize. Must be +# an absolute path! +# +# -sSTRICT_JS ensures that the emitted JS code includes the 'use +# strict' option. Note that -sSTRICT is more broadly-scoped and +# results in build errors. +# +# -sALLOW_TABLE_GROWTH is required for (at a minimum) the UDF-binding +# feature. Without it, JS functions cannot be made to proxy C-side +# callbacks. +# +# -sABORTING_MALLOC causes the JS-bound _malloc() to abort rather than +# return 0 on OOM. If set to 0 then all code which uses _malloc() +# must, just like in C, check the result before using it, else +# they're likely to corrupt the JS/WASM heap by writing to its +# address of 0. It is, as of this writing, enabled in Emscripten by +# default but we enable it explicitly in case that default changes. +# +# -sDYNAMIC_EXECUTION=0 disables eval() and the Function constructor. +# If the build runs without these, it's preferable to use this flag +# because certain execution environments disallow those constructs. +# This flag is not strictly necessary, however. +# +# --no-entry: for compiling library code with no main(). If this is +# not supplied and the code has a main(), it is called as part of the +# module init process. Note that main() is #if'd out of shell.c +# (renamed) when building in wasm mode. +# +# --pre-js/--post-js=FILE relative or absolute paths to JS files to +# prepend/append to the emcc-generated bootstrapping JS. It's +# easier/faster to develop with separate JS files (reduces rebuilding +# requirements) but certain configurations, namely -sMODULARIZE, may +# require using at least a --pre-js file. They can be used +# individually and need not be paired. +# +# -O0..-O3 and -Oz: optimization levels affect not only C-style +# optimization but whether or not the resulting generated JS code +# gets minified. -O0 compiles _much_ more quickly than -O3 or -Oz, +# and doesn't minimize any JS code, so is recommended for +# development. -O3 or -Oz are recommended for deployment, but +# primarily because -Oz will shrink the wasm file notably. JS-side +# minification makes little difference in terms of overall +# distributable size. +# +# --minify 0: supposedly disables minification of the generated JS +# code, regardless of optimization level, but that's not quite true: +# search the main makefile for wasm-strip for details. Minification +# of the JS has minimal overall effect in the larger scheme of things +# and results in JS files which can neither be edited nor viewed as +# text files in Fossil (which flags them as binary because of their +# extreme line lengths). Interestingly, whether or not the comments +# in the generated JS file get stripped is unaffected by this setting +# and depends entirely on the optimization level. Higher optimization +# levels reduce the size of the JS considerably even without +# minification. +# +######################################################################## diff --git a/ext/wasm/README-dist.txt b/ext/wasm/README-dist.txt new file mode 100644 index 0000000000..4a527fc5ef --- /dev/null +++ b/ext/wasm/README-dist.txt @@ -0,0 +1,50 @@ +This is the README for the sqlite3 WASM/JS distribution. + +Main project page: https://sqlite.org + +Documentation: https://sqlite.org/wasm + +This archive contains the following deliverables for the WASM/JS +build: + +- jswasm/sqlite3.js is the canonical "vanilla JS" version. + +- jswasm/sqlite3.mjs is the same but in ES6 module form + +- jswasm/*-bundler-friendly.js and .mjs are variants which are + intended to be compatible with "bundler" tools commonly seen in + node.js-based projects. Projects using such tools should use those + variants, where available, instead of files without the + "-bundler-friendly" suffix. Some files do not have separate + variants. + +- jswasm/sqlite3.wasm is the binary WASM file imported by all of the + above-listed JS files. + +- The jswasm directory additionally contains a number of supplemental + JS files which cannot be bundled directly with the main JS files + but are necessary for certain usages. + +- The top-level directory contains various demonstration and test + applications for sqlite3.js and sqlite3.mjs. + sqlite3-bundler-friendly.mjs requires client-side build tools to make + use of and is not demonstrated here. + +Browsers will not serve WASM files from file:// URLs, so the test and +demonstration apps require a web server and that server must, for the +OPFS[^1]-related features, include the following headers in its response +when serving the files: + + Cross-Origin-Opener-Policy: same-origin + Cross-Origin-Embedder-Policy: require-corp + +Most functionality will work without those headers but the OPFS[^1] +storage capability will not be available without them. + +One simple way to get the demo apps up and running on Unix-style +systems is to install althttpd (https://sqlite.org/althttpd) and run: + + althttpd --enable-sab --page index.html + + +[^1]: https://developer.mozilla.org/en-US/docs/Web/API/File_System_API/Origin_private_file_system diff --git a/ext/wasm/README.md b/ext/wasm/README.md new file mode 100644 index 0000000000..48a2537c66 --- /dev/null +++ b/ext/wasm/README.md @@ -0,0 +1,150 @@ +This directory houses the [Web Assembly (WASM)](https://en.wikipedia.org/wiki/WebAssembly) +parts of the sqlite3 build. + +It requires [emscripten][] and that the build environment be set up +for emscripten. _Release_ builds also require [the wabt tools](#wabt), +but dev builds do not. A mini-HOWTO for setting that up follows... + +First, install the Emscripten SDK, as documented +[here](https://emscripten.org/docs/getting_started/downloads.html) and summarized +below for Linux environments: + +``` +# Clone the emscripten repository: +$ sudo apt install git +$ git clone https://github.com/emscripten-core/emsdk.git +$ cd emsdk + +# Download and install the latest SDK tools: +$ ./emsdk install latest + +# Make the "latest" SDK "active" for the current user: +$ ./emsdk activate latest +``` + +Those parts only need to be run once, but the SDK can be updated using: + +``` +$ git pull +$ ./emsdk install latest +$ ./emsdk activate latest +``` + +(Sidebar: Emscripten updates can and do _change things_, i.e. _break +things_, so it's considered _required practice_ to test thoroughly +after upgrading it! Our build process makes no guarantees about which +Emscripten version(s) will or won't work, but it's important that +production builds are built using a compatible version. During active +development, the EMSDK is frequently updated, the goal being to keep +`sqlite3.wasm` working with "the latest" EMSDK.) + +The SQLite configure script will search for the EMSDK. One way +to ensure that it finds it is: + +``` +# Activate PATH and other environment variables in the current terminal: +$ source ./emsdk_env.sh + +$ which emcc +/path/to/emsdk/upstream/emscripten/emcc + +$ ./configure ... +``` + +Optionally, add that `source` part to your login shell's resource file +(`~/.bashrc` or equivalent). + +Another way is to pass the EMSDK dir to configure: + +``` +$ ./configure --with-emsdk=/path/to/emsdk +``` + +The build tree uses a small wrapper for invoking the `emcc` (the +Emscripten compiler): `tool/emcc.sh` is generated from +`tool/emcc.sh.in` using the EMSDK path found by the configure process. + +With that in place, the most common build approaches are: + +``` +# From the top of the tree: +$ make fiddle +``` + +Or: + +``` +$ cd ext/wasm +$ make +``` + +Those will generate the a number of files required for a handful of +test and demo applications which can be accessed via +`index.html`. WASM content cannot, due to XMLHttpRequest security +limitations, be loaded if the containing HTML file is opened directly +in the browser (i.e. if it is opened using a `file://` URL), so it +needs to be served via an HTTP server. For example, using +[althttpd][]: + +``` +$ cd ext/wasm +$ althttpd --enable-sab --max-age 1 --page index.html +# Or, more simply, from the ext/wasm dir: +$ make httpd +``` + +That will open the system's browser and visit the index page, from +which (almost) all of the test and demo applications can be accessed. +(`ext/wasm/SQLTester` is not listed in that page because it's only of +real utility when it's used in conjunction with the project's +proprietary test suite, which most users don't have access to.) + +When serving this app via [althttpd][], it must be a version from +2022-09-26 or newer so that it recognizes the `--enable-sab` flag, +which causes althttpd to emit two HTTP response headers which are +required to enable JavaScript's `SharedArrayBuffer` and `Atomics` +APIs. Those APIs are required in order to enable the [OPFS][]-related +features in the apps which use them. + +# Testing on a remote machine that is accessed via SSH + +*NB: The following are developer notes, last validated on 2023-07-19* + + * Remote: Install git, emsdk, and althttpd + * Use a [version of althttpd][althttpd] from + September 26, 2022 or newer. + * Remote: Install the SQLite source tree. CD to ext/wasm + * Remote: "`make`" to build WASM + * Remote: `althttpd --enable-sab --port 8080 --popup` + * Local: `ssh -L 8180:localhost:8080 remote` + * Local: Point your web-browser at http://localhost:8180/index.html + +In order to enable [SharedArrayBuffer][], the web-browser requires +that the two extra Cross-Origin lines be present in HTTP reply headers +and that the request must come from "localhost" (_or_ over an SSL +connection). Since the web-server is on a different machine from the +web-broser, the localhost requirement means that the connection must +be tunneled using SSH. + + +# The wabt Tools + +_Release_ builds require the wabt tools: + + + +Specifically, we need `wasm-strip` so that the resulting WASM file is not +several megabytes. + +Pre-built binaries can be downloaded from: + + + +As of 2025-10-14, versions 1.36.0 or higher are known to work and +1.34.0 is known to not work with current Emscripten output. + + +[emscripten]: https://emscripten.org +[althttpd]: https://sqlite.org/althttpd +[SharedArrayBuffer]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer +[OPFS]: https://developer.mozilla.org/en-US/docs/Web/API/File_System_API/Origin_private_file_system diff --git a/ext/wasm/SQLTester/GNUmakefile b/ext/wasm/SQLTester/GNUmakefile new file mode 100644 index 0000000000..8fa1247138 --- /dev/null +++ b/ext/wasm/SQLTester/GNUmakefile @@ -0,0 +1,55 @@ +#!/this/is/make +# +# This makefile compiles SQLTester test files into something +# we can readily import into JavaScript. +all: + +SHELL := $(shell which bash 2>/dev/null) +MAKEFILE := $(lastword $(MAKEFILE_LIST)) +CLEAN_FILES := +DISTCLEAN_FILES := ./--dummy-- *~ + +test-list.mjs := test-list.mjs +test-list.mjs.gz := $(test-list.mjs).gz +CLEAN_FILES += $(test-list.mjs) + +tests.dir := $(firstword $(wildcard tests ../../jni/src/tests)) +$(info test script dir=$(tests.dir)) + +tests.all := $(wildcard $(tests.dir)/*.test) + +bin.touint8array := ./touint8array +$(bin.touint8array): $(bin.touint8array).c $(MAKEFILE) + $(CC) -o $@ $< +CLEAN_FILES += $(bin.touint8array) + +ifneq (,$(tests.all)) +$(test-list.mjs): $(bin.touint8array) $(tests.all) $(MAKEFILE) + @{\ + echo 'export default ['; \ + sep=''; \ + for f in $(sort $(tests.all)); do \ + echo -en $$sep'{"name": "'$${f##*/}'", "content":'; \ + $(bin.touint8array) < $$f; \ + echo -n '}'; \ + sep=',\n'; \ + done; \ + echo '];'; \ + } > $@ + @echo "Created $@" +$(test-list.mjs.gz): $(test-list.mjs) + gzip -c $< > $@ +CLEAN_FILES += $(test-list.mjs.gz) +all: $(test-list.mjs.gz) +else + @echo "Cannot build $(test-list.mjs) for lack of input test files."; \ + echo "Symlink ./tests to a directory containing SQLTester-format "; \ + echo "test scripts named *.test, then try again"; \ + exit 1 +endif + +.PHONY: clean distclean +clean: + -rm -f $(CLEAN_FILES) +distclean: clean + -rm -f $(DISTCLEAN_FILES) diff --git a/ext/wasm/SQLTester/SQLTester.mjs b/ext/wasm/SQLTester/SQLTester.mjs new file mode 100644 index 0000000000..61f6aea2cf --- /dev/null +++ b/ext/wasm/SQLTester/SQLTester.mjs @@ -0,0 +1,1339 @@ +/* +** 2023-08-29 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains the main application entry pointer for the JS +** implementation of the SQLTester framework. +** +** This version is not well-documented because it's a direct port of +** the Java implementation, which is documented: in the main SQLite3 +** source tree, see ext/jni/src/org/sqlite/jni/capi/SQLTester.java. +*/ + +import sqlite3ApiInit from '/jswasm/sqlite3.mjs'; + +const sqlite3 = await sqlite3ApiInit(); + +const log = (...args)=>{ + console.log('SQLTester:',...args); +}; + +/** + Try to install vfsName as the new default VFS. Once this succeeds + (returns true) then it becomes a no-op on future calls. Throws if + VFS registration as the default VFS fails but has no side effects + if vfsName is not currently registered. +*/ +const tryInstallVfs = function f(vfsName){ + if(f.vfsName) return false; + const pVfs = sqlite3.capi.sqlite3_vfs_find(vfsName); + if(pVfs){ + log("Installing",'"'+vfsName+'"',"as default VFS."); + const rc = sqlite3.capi.sqlite3_vfs_register(pVfs, 1); + if(rc){ + sqlite3.SQLite3Error.toss(rc,"While trying to register",vfsName,"vfs."); + } + f.vfsName = vfsName; + } + return !!pVfs; +}; +tryInstallVfs.vfsName = undefined; + +if( 0 && globalThis.WorkerGlobalScope ){ + // Try OPFS storage, if available... + if( 1 && sqlite3.oo1.OpfsDb ){ + /* Really slow with these tests */ + tryInstallVfs("opfs"); + }else if( sqlite3.installOpfsSAHPoolVfs ){ + await sqlite3.installOpfsSAHPoolVfs({ + clearOnInit: true, + initialCapacity: 15, + name: 'opfs-SQLTester' + }).then(pool=>{ + tryInstallVfs(pool.vfsName); + }).catch(e=>{ + log("OpfsSAHPool could not load:",e); + }); + } +} +// Return a new enum entry value +const newE = ()=>Object.create(null); + +const newObj = (props)=>Object.assign(newE(), props); + +/** + Modes for how to escape (or not) column values and names from + SQLTester.execSql() to the result buffer output. +*/ +const ResultBufferMode = Object.assign(Object.create(null),{ + //! Do not append to result buffer + NONE: newE(), + //! Append output escaped. + ESCAPED: newE(), + //! Append output as-is + ASIS: newE() +}); + +/** + Modes to specify how to emit multi-row output from + SQLTester.execSql() to the result buffer. +*/ +const ResultRowMode = newObj({ + //! Keep all result rows on one line, space-separated. + ONLINE: newE(), + //! Add a newline between each result row. + NEWLINE: newE() +}); + +class SQLTesterException extends globalThis.Error { + constructor(testScript, ...args){ + if(testScript){ + super( [testScript.getOutputPrefix()+": ", ...args].join('') ); + }else{ + super( args.join('') ); + } + this.name = 'SQLTesterException'; + } + isFatal() { return false; } +} + +SQLTesterException.toss = (...args)=>{ + throw new SQLTesterException(...args); +} + +class DbException extends SQLTesterException { + constructor(testScript, pDb, rc, closeDb=false){ + super(testScript, "DB error #"+rc+": "+sqlite3.capi.sqlite3_errmsg(pDb)); + this.name = 'DbException'; + if( closeDb ) sqlite3.capi.sqlite3_close_v2(pDb); + } + isFatal() { return true; } +} + +class TestScriptFailed extends SQLTesterException { + constructor(testScript, ...args){ + super(testScript,...args); + this.name = 'TestScriptFailed'; + } + isFatal() { return true; } +} + +class UnknownCommand extends SQLTesterException { + constructor(testScript, cmdName){ + super(testScript, cmdName); + this.name = 'UnknownCommand'; + } + isFatal() { return true; } +} + +class IncompatibleDirective extends SQLTesterException { + constructor(testScript, ...args){ + super(testScript,...args); + this.name = 'IncompatibleDirective'; + } +} + +//! For throwing where an expression is required. +const toss = (errType, ...args)=>{ + throw new errType(...args); +}; + +const __utf8Decoder = new TextDecoder(); +const __utf8Encoder = new TextEncoder('utf-8'); +//! Workaround for Util.utf8Decode() +const __SAB = ('undefined'===typeof globalThis.SharedArrayBuffer) + ? function(){} : globalThis.SharedArrayBuffer; + + +/* Frequently-reused regexes. */ +const Rx = newObj({ + requiredProperties: / REQUIRED_PROPERTIES:[ \t]*(\S.*)\s*$/, + scriptModuleName: / SCRIPT_MODULE_NAME:[ \t]*(\S+)\s*$/, + mixedModuleName: / ((MIXED_)?MODULE_NAME):[ \t]*(\S+)\s*$/, + command: /^--(([a-z-]+)( .*)?)$/, + //! "Special" characters - we have to escape output if it contains any. + special: /[\x00-\x20\x22\x5c\x7b\x7d]/, + squiggly: /[{}]/ +}); + + + +const Util = newObj({ + toss, + + unlink: function f(fn){ + if(!f.unlink){ + f.unlink = sqlite3.wasm.xWrap('sqlite3__wasm_vfs_unlink','int', + ['*','string']); + } + return 0==f.unlink(0,fn); + }, + + argvToString: (list)=>{ + const m = [...list]; + m.shift() /* strip command name */; + return m.join(" ") + }, + + utf8Decode: function(arrayBuffer, begin, end){ + return __utf8Decoder.decode( + (arrayBuffer.buffer instanceof __SAB) + ? arrayBuffer.slice(begin, end) + : arrayBuffer.subarray(begin, end) + ); + }, + + utf8Encode: (str)=>__utf8Encoder.encode(str), + + strglob: sqlite3.wasm.xWrap('sqlite3__wasm_SQLTester_strglob','int', + ['string','string']) +})/*Util*/; + +/** + Output logger utility. +*/ +class Outer { + #lnBuf = []; + #verbosity = 0; + #logger = console.log.bind(console); + + constructor(func){ + if(func) this.setFunc(func); + } + + logger(...args){ + if(args.length){ + this.#logger = args[0]; + return this; + } + return this.#logger; + } + + out(...args){ + if( this.getOutputPrefix && !this.#lnBuf.length ){ + this.#lnBuf.push(this.getOutputPrefix()); + } + this.#lnBuf.push(...args); + return this; + } + + #outlnImpl(vLevel, ...args){ + if( this.getOutputPrefix && !this.#lnBuf.length ){ + this.#lnBuf.push(this.getOutputPrefix()); + } + this.#lnBuf.push(...args,'\n'); + const msg = this.#lnBuf.join(''); + this.#lnBuf.length = 0; + this.#logger(msg); + return this; + } + + outln(...args){ + return this.#outlnImpl(0,...args); + } + + outputPrefix(){ + if( 0==arguments.length ){ + return (this.getOutputPrefix + ? (this.getOutputPrefix() ?? '') : ''); + }else{ + this.getOutputPrefix = arguments[0]; + return this; + } + } + + static #verboseLabel = ["🔈",/*"🔉",*/"🔊","📢"]; + verboseN(lvl, args){ + if( this.#verbosity>=lvl ){ + this.#outlnImpl(lvl, Outer.#verboseLabel[lvl-1],': ',...args); + } + } + verbose1(...args){ return this.verboseN(1,args); } + verbose2(...args){ return this.verboseN(2,args); } + verbose3(...args){ return this.verboseN(3,args); } + + verbosity(){ + const rc = this.#verbosity; + if(arguments.length) this.#verbosity = +arguments[0]; + return rc; + } + +}/*Outer*/ + +class SQLTester { + + //! Console output utility. + #outer = new Outer().outputPrefix( ()=>'SQLTester: ' ); + //! List of input scripts. + #aScripts = []; + //! Test input buffer. + #inputBuffer = []; + //! Test result buffer. + #resultBuffer = []; + //! Output representation of SQL NULL. + #nullView; + metrics = newObj({ + //! Total tests run + nTotalTest: 0, + //! Total test script files run + nTestFile: 0, + //! Test-case count for to the current TestScript + nTest: 0, + //! Names of scripts which were aborted. + failedScripts: [] + }); + #emitColNames = false; + //! True to keep going regardless of how a test fails. + #keepGoing = false; + #db = newObj({ + //! The list of available db handles. + list: new Array(7), + //! Index into this.list of the current db. + iCurrentDb: 0, + //! Name of the default db, re-created for each script. + initialDbName: "test.db", + //! Buffer for REQUIRED_PROPERTIES pragmas. + initSql: ['select 1;'], + //! (sqlite3*) to the current db. + currentDb: function(){ + return this.list[this.iCurrentDb]; + } + }); + + constructor(){ + this.reset(); + } + + outln(...args){ return this.#outer.outln(...args); } + out(...args){ return this.#outer.out(...args); } + outer(...args){ + if(args.length){ + this.#outer = args[0]; + return this; + } + return this.#outer; + } + verbose1(...args){ return this.#outer.verboseN(1,args); } + verbose2(...args){ return this.#outer.verboseN(2,args); } + verbose3(...args){ return this.#outer.verboseN(3,args); } + verbosity(...args){ + const rc = this.#outer.verbosity(...args); + return args.length ? this : rc; + } + setLogger(func){ + this.#outer.logger(func); + return this; + } + + incrementTestCounter(){ + ++this.metrics.nTotalTest; + ++this.metrics.nTest; + } + + reset(){ + this.clearInputBuffer(); + this.clearResultBuffer(); + this.#clearBuffer(this.#db.initSql); + this.closeAllDbs(); + this.metrics.nTest = 0; + this.#nullView = "nil"; + this.#emitColNames = false; + this.#db.iCurrentDb = 0; + //this.#db.initSql.push("SELECT 1;"); + } + + appendInput(line, addNL){ + this.#inputBuffer.push(line); + if( addNL ) this.#inputBuffer.push('\n'); + } + appendResult(line, addNL){ + this.#resultBuffer.push(line); + if( addNL ) this.#resultBuffer.push('\n'); + } + appendDbInitSql(sql){ + this.#db.initSql.push(sql); + if( this.currentDb() ){ + this.execSql(null, true, ResultBufferMode.NONE, null, sql); + } + } + + #runInitSql(pDb){ + let rc = 0; + for(const sql of this.#db.initSql){ + this.#outer.verbose2("RUNNING DB INIT CODE: ",sql); + rc = this.execSql(pDb, false, ResultBufferMode.NONE, null, sql); + if( rc ) break; + } + return rc; + } + +#clearBuffer(buffer){ + buffer.length = 0; + return buffer; + } + + clearInputBuffer(){ return this.#clearBuffer(this.#inputBuffer); } + clearResultBuffer(){return this.#clearBuffer(this.#resultBuffer); } + + getInputText(){ return this.#inputBuffer.join(''); } + getResultText(){ return this.#resultBuffer.join(''); } + + #takeBuffer(buffer){ + const s = buffer.join(''); + buffer.length = 0; + return s; + } + + takeInputBuffer(){ + return this.#takeBuffer(this.#inputBuffer); + } + takeResultBuffer(){ + return this.#takeBuffer(this.#resultBuffer); + } + + nullValue(){ + return (0==arguments.length) + ? this.#nullView + : (this.#nullView = ''+arguments[0]); + } + + outputColumnNames(){ + return (0==arguments.length) + ? this.#emitColNames + : (this.#emitColNames = !!arguments[0]); + } + + currentDbId(){ + return (0==arguments.length) + ? this.#db.iCurrentDb + : (this.#affirmDbId(arguments[0]).#db.iCurrentDb = arguments[0]); + } + + #affirmDbId(id){ + if(id<0 || id>=this.#db.list.length){ + toss(SQLTesterException, "Database index ",id," is out of range."); + } + return this; + } + + currentDb(...args){ + if( 0!=args.length ){ + this.#affirmDbId(id).#db.iCurrentDb = id; + } + return this.#db.currentDb(); + } + + getDbById(id){ + return this.#affirmDbId(id).#db.list[id]; + } + + getCurrentDb(){ return this.#db.list[this.#db.iCurrentDb]; } + + + closeDb(id) { + if( 0==arguments.length ){ + id = this.#db.iCurrentDb; + } + const pDb = this.#affirmDbId(id).#db.list[id]; + if( pDb ){ + sqlite3.capi.sqlite3_close_v2(pDb); + this.#db.list[id] = null; + } + } + + closeAllDbs(){ + for(let i = 0; i 0){ + rc = this.#runInitSql(pDb); + } + if( 0!=rc ){ + sqlite3.SQLite3Error.toss( + rc, + "sqlite3 result code",rc+":", + (pDb ? sqlite3.capi.sqlite3_errmsg(pDb) + : sqlite3.capi.sqlite3_errstr(rc)) + ); + } + return this.#db.list[this.#db.iCurrentDb] = pDb; + }catch(e){ + sqlite3.capi.sqlite3_close_v2(pDb); + throw e; + } + } + + addTestScript(ts){ + if( 2===arguments.length ){ + ts = new TestScript(arguments[0], arguments[1]); + }else if(ts instanceof Uint8Array){ + ts = new TestScript('', ts); + }else if('string' === typeof arguments[1]){ + ts = new TestScript('', Util.utf8Encode(arguments[1])); + } + if( !(ts instanceof TestScript) ){ + Util.toss(SQLTesterException, "Invalid argument type for addTestScript()"); + } + this.#aScripts.push(ts); + return this; + } + + runTests(){ + this.outln("SQLite version ", sqlite3.capi.sqlite3_libversion()," with ", + sqlite3.wasm.ptr.size, "-byte WASM pointers"); + const tStart = (new Date()).getTime(); + let isVerbose = this.verbosity(); + this.metrics.failedScripts.length = 0; + this.metrics.nTotalTest = 0; + this.metrics.nTestFile = 0; + for(const ts of this.#aScripts){ + this.reset(); + ++this.metrics.nTestFile; + let threw = false; + const timeStart = (new Date()).getTime(); + let msgTail = ''; + try{ + ts.run(this); + }catch(e){ + if(e instanceof SQLTesterException){ + threw = true; + this.outln("🔥EXCEPTION: ",e); + this.metrics.failedScripts.push({script: ts.filename(), message:e.toString()}); + if( this.#keepGoing ){ + this.outln("Continuing anyway because of the keep-going option."); + }else if( e.isFatal() ){ + throw e; + } + }else{ + throw e; + } + }finally{ + const timeEnd = (new Date()).getTime(); + this.out("🏁", (threw ? "❌" : "✅"), " ", + this.metrics.nTest, " test(s) in ", + (timeEnd-timeStart),"ms. "); + const mod = ts.moduleName(); + if( mod ){ + this.out( "[",mod,"] " ); + } + this.outln(ts.filename()); + } + } + const tEnd = (new Date()).getTime(); + Util.unlink(this.#db.initialDbName); + this.outln("Took ",(tEnd-tStart),"ms. Test count = ", + this.metrics.nTotalTest,", script count = ", + this.#aScripts.length,( + this.metrics.failedScripts.length + ? ", failed scripts = "+this.metrics.failedScripts.length + : "" + ) + ); + return this; + } + + #setupInitialDb(){ + if( !this.#db.list[0] ){ + Util.unlink(this.#db.initialDbName); + this.openDb(0, this.#db.initialDbName, true); + }else{ + this.#outer.outln("WARNING: setupInitialDb() was unexpectedly ", + "triggered while it is opened."); + } + } + + #escapeSqlValue(v){ + if( !v ) return "{}"; + if( !Rx.special.test(v) ){ + return v /* no escaping needed */; + } + if( !Rx.squiggly.test(v) ){ + return "{"+v+"}"; + } + const sb = ["\""]; + const n = v.length; + for(let i = 0; i < n; ++i){ + const ch = v.charAt(i); + switch(ch){ + case '\\': sb.push("\\\\"); break; + case '"': sb.push("\\\""); break; + default:{ + //verbose("CHAR ",(int)ch," ",ch," octal=",String.format("\\%03o", (int)ch)); + const ccode = ch.charCodeAt(i); + if( ccode < 32 ) sb.push('\\',ccode.toString(8),'o'); + else sb.push(ch); + break; + } + } + } + sb.push("\""); + return sb.join(''); + } + + #appendDbErr(pDb, sb, rc){ + sb.push(sqlite3.capi.sqlite3_js_rc_str(rc), ' '); + const msg = this.#escapeSqlValue(sqlite3.capi.sqlite3_errmsg(pDb)); + if( '{' === msg.charAt(0) ){ + sb.push(msg); + }else{ + sb.push('{', msg, '}'); + } + } + + #checkDbRc(pDb,rc){ + sqlite3.oo1.DB.checkRc(pDb, rc); + } + + execSql(pDb, throwOnError, appendMode, rowMode, sql){ + if( !pDb && !this.#db.list[0] ){ + this.#setupInitialDb(); + } + if( !pDb ) pDb = this.#db.currentDb(); + const wasm = sqlite3.wasm, capi = sqlite3.capi; + sql = (sql instanceof Uint8Array) + ? sql + : Util.utf8Encode(capi.sqlite3_js_sql_to_string(sql)); + const self = this; + const sb = (ResultBufferMode.NONE===appendMode) ? null : this.#resultBuffer; + let rc = 0; + wasm.scopedAllocCall(function(){ + let sqlByteLen = Number(sql.byteLength); + const ppStmt = wasm.scopedAlloc( + (2 * wasm.ptr.size) /* output (sqlite3_stmt**) arg and pzTail */ + + (sqlByteLen + 1/* SQL + NUL */) + ); + const pzTail = wasm.ptr.add(ppStmt, wasm.ptr.size) /* final arg to sqlite3_prepare_v2() */; + let pSql = wasm.ptr.add(pzTail, wasm.ptr.size); + const pSqlEnd = wasm.ptr.add(pSql, sqlByteLen); + wasm.heap8().set(sql, Number(pSql)); + wasm.poke8(pSqlEnd, 0/*NUL terminator*/); + let pos = 0, n = 1, spacing = 0; + while( pSql && wasm.peek8(pSql) ){ + wasm.pokePtr([ppStmt, pzTail], 0); + rc = capi.sqlite3_prepare_v3( + pDb, pSql, sqlByteLen, 0, ppStmt, pzTail + ); + if( 0!==rc ){ + if(throwOnError){ + throw new DbException(self, pDb, rc); + }else if( sb ){ + self.#appendDbErr(pDb, sb, rc); + } + break; + } + const pStmt = wasm.peekPtr(ppStmt); + pSql = wasm.peekPtr(pzTail); + sqlByteLen = Number(pSqlEnd - pSql); + if(!pStmt) continue /* only whitespace or comments */; + if( sb ){ + const nCol = capi.sqlite3_column_count(pStmt); + let colName, val; + while( capi.SQLITE_ROW === (rc = capi.sqlite3_step(pStmt)) ) { + for( let i=0; i < nCol; ++i ){ + if( spacing++ > 0 ) sb.push(' '); + if( self.#emitColNames ){ + colName = capi.sqlite3_column_name(pStmt, i); + switch(appendMode){ + case ResultBufferMode.ASIS: sb.push( colName ); break; + case ResultBufferMode.ESCAPED: + sb.push( self.#escapeSqlValue(colName) ); + break; + default: + self.toss("Unhandled ResultBufferMode."); + } + sb.push(' '); + } + val = capi.sqlite3_column_text(pStmt, i); + if( null===val ){ + sb.push( self.#nullView ); + continue; + } + switch(appendMode){ + case ResultBufferMode.ASIS: sb.push( val ); break; + case ResultBufferMode.ESCAPED: + sb.push( self.#escapeSqlValue(val) ); + break; + } + }/* column loop */ + if( ResultRowMode.NEWLINE === rowMode ){ + spacing = 0; + sb.push('\n'); + } + }/* row loop */ + }else{ // no output but possibly other side effects + while( capi.SQLITE_ROW === (rc = capi.sqlite3_step(pStmt)) ) {} + } + capi.sqlite3_finalize(pStmt); + if( capi.SQLITE_ROW===rc || capi.SQLITE_DONE===rc) rc = 0; + else if( rc!=0 ){ + if( sb ){ + self.#appendDbErr(pDb, sb, rc); + } + break; + } + }/* SQL script loop */; + })/*scopedAllocCall()*/; + return rc; + } + +}/*SQLTester*/ + +class Command { + constructor(){ + } + + process(sqlTester,testScript,argv){ + SQLTesterException.toss("process() must be overridden"); + } + + argcCheck(testScript,argv,min,max){ + const argc = argv.length-1; + if(argc=0 && argc>max)){ + if( min==max ){ + testScript.toss(argv[0]," requires exactly ",min," argument(s)"); + }else if(max>0){ + testScript.toss(argv[0]," requires ",min,"-",max," arguments."); + }else{ + testScript.toss(argv[0]," requires at least ",min," arguments."); + } + } + } +} + +class Cursor { + src; + sb = []; + pos = 0; + //! Current line number. Starts at 0 for internal reasons and will + // line up with 1-based reality once parsing starts. + lineNo = 0 /* yes, zero */; + //! Putback value for this.pos. + putbackPos = 0; + //! Putback line number + putbackLineNo = 0; + //! Peeked-to pos, used by peekLine() and consumePeeked(). + peekedPos = 0; + //! Peeked-to line number. + peekedLineNo = 0; + + constructor(){ + } + + //! Restore parsing state to the start of the stream. + rewind(){ + this.sb.length = this.pos = this.lineNo + = this.putbackPos = this.putbackLineNo + = this.peekedPos = this.peekedLineNo = 0; + } +} + +class TestScript { + #cursor = new Cursor(); + #moduleName = null; + #filename = null; + #testCaseName = null; + #outer = new Outer().outputPrefix( ()=>this.getOutputPrefix()+': ' ); + + constructor(...args){ + let content, filename; + if( 2 == args.length ){ + filename = args[0]; + content = args[1]; + }else if( 1 == args.length ){ + if(args[0] instanceof Object){ + const o = args[0]; + filename = o.name; + content = o.content; + }else{ + content = args[0]; + } + } + if(!(content instanceof Uint8Array)){ + if('string' === typeof content){ + content = Util.utf8Encode(content); + }else if((content instanceof ArrayBuffer) + ||(content instanceof Array)){ + content = new Uint8Array(content); + }else{ + toss(Error, "Invalid content type for TestScript constructor."); + } + } + this.#filename = filename; + this.#cursor.src = content; + } + + moduleName(){ + return (0==arguments.length) + ? this.#moduleName : (this.#moduleName = arguments[0]); + } + + testCaseName(){ + return (0==arguments.length) + ? this.#testCaseName : (this.#testCaseName = arguments[0]); + } + filename(){ + return (0==arguments.length) + ? this.#filename : (this.#filename = arguments[0]); + } + + getOutputPrefix() { + let rc = "["+(this.#moduleName || '')+"]"; + if( this.#testCaseName ) rc += "["+this.#testCaseName+"]"; + if( this.#filename ) rc += '['+this.#filename+']'; + return rc + " line "+ this.#cursor.lineNo; + } + + reset(){ + this.#testCaseName = null; + this.#cursor.rewind(); + return this; + } + + toss(...args){ + throw new TestScriptFailed(this,...args); + } + + verbose1(...args){ return this.#outer.verboseN(1,args); } + verbose2(...args){ return this.#outer.verboseN(2,args); } + verbose3(...args){ return this.#outer.verboseN(3,args); } + verbosity(...args){ + const rc = this.#outer.verbosity(...args); + return args.length ? this : rc; + } + + #checkRequiredProperties(tester, props){ + if(true) return false; + let nOk = 0; + for(const rp of props){ + this.verbose2("REQUIRED_PROPERTIES: ",rp); + switch(rp){ + case "RECURSIVE_TRIGGERS": + tester.appendDbInitSql("pragma recursive_triggers=on;"); + ++nOk; + break; + case "TEMPSTORE_FILE": + /* This _assumes_ that the lib is built with SQLITE_TEMP_STORE=1 or 2, + which we just happen to know is the case */ + tester.appendDbInitSql("pragma temp_store=1;"); + ++nOk; + break; + case "TEMPSTORE_MEM": + /* This _assumes_ that the lib is built with SQLITE_TEMP_STORE=1 or 2, + which we just happen to know is the case */ + tester.appendDbInitSql("pragma temp_store=0;"); + ++nOk; + break; + case "AUTOVACUUM": + tester.appendDbInitSql("pragma auto_vacuum=full;"); + ++nOk; + break; + case "INCRVACUUM": + tester.appendDbInitSql("pragma auto_vacuum=incremental;"); + ++nOk; + default: + break; + } + } + return props.length == nOk; + } + + #checkForDirective(tester,line){ + if(line.startsWith("#")){ + throw new IncompatibleDirective(this, "C-preprocessor input: "+line); + }else if(line.startsWith("---")){ + throw new IncompatibleDirective(this, "triple-dash: ",line); + } + let m = Rx.scriptModuleName.exec(line); + if( m ){ + this.#moduleName = m[1]; + return; + } + m = Rx.requiredProperties.exec(line); + if( m ){ + const rp = m[1]; + if( !this.#checkRequiredProperties( tester, rp.split(/\s+/).filter(v=>!!v) ) ){ + throw new IncompatibleDirective(this, "REQUIRED_PROPERTIES: "+rp); + } + } + + m = Rx.mixedModuleName.exec(line); + if( m ){ + throw new IncompatibleDirective(this, m[1]+": "+m[3]); + } + if( line.indexOf("\n|")>=0 ){ + throw new IncompatibleDirective(this, "newline-pipe combination."); + } + + } + + #getCommandArgv(line){ + const m = Rx.command.exec(line); + return m ? m[1].trim().split(/\s+/) : null; + } + + + #isCommandLine(line, checkForImpl){ + let m = Rx.command.exec(line); + if( m && checkForImpl ){ + m = !!CommandDispatcher.getCommandByName(m[2]); + } + return !!m; + } + + fetchCommandBody(tester){ + const sb = []; + let line; + while( (null !== (line = this.peekLine())) ){ + this.#checkForDirective(tester, line); + if( this.#isCommandLine(line, true) ) break; + sb.push(line,"\n"); + this.consumePeeked(); + } + line = sb.join(''); + return !!line.trim() ? line : null; + } + + run(tester){ + this.reset(); + this.#outer.verbosity( tester.verbosity() ); + this.#outer.logger( tester.outer().logger() ); + let line, directive, argv = []; + while( null != (line = this.getLine()) ){ + this.verbose3("run() input line: ",line); + this.#checkForDirective(tester, line); + argv = this.#getCommandArgv(line); + if( argv ){ + this.#processCommand(tester, argv); + continue; + } + tester.appendInput(line,true); + } + return true; + } + + #processCommand(tester, argv){ + this.verbose2("processCommand(): ",argv[0], " ", Util.argvToString(argv)); + if(this.#outer.verbosity()>1){ + const input = tester.getInputText(); + this.verbose3("processCommand() input buffer = ",input); + } + CommandDispatcher.dispatch(tester, this, argv); + } + + getLine(){ + const cur = this.#cursor; + if( cur.pos==cur.src.byteLength ){ + return null/*EOF*/; + } + cur.putbackPos = cur.pos; + cur.putbackLineNo = cur.lineNo; + cur.sb.length = 0; + let b = 0, prevB = 0, i = cur.pos; + let doBreak = false; + let nChar = 0 /* number of bytes in the aChar char */; + const end = cur.src.byteLength; + for(; i < end && !doBreak; ++i){ + b = cur.src[i]; + switch( b ){ + case 13/*CR*/: continue; + case 10/*NL*/: + ++cur.lineNo; + if(cur.sb.length>0) doBreak = true; + // Else it's an empty string + break; + default:{ + /* Multi-byte chars need to be gathered up and appended at + one time so that we can get them as string objects. */ + nChar = 1; + switch( b & 0xF0 ){ + case 0xC0: nChar = 2; break; + case 0xE0: nChar = 3; break; + case 0xF0: nChar = 4; break; + default: + if( b > 127 ) this.toss("Invalid character (#"+b+")."); + break; + } + if( 1==nChar ){ + cur.sb.push(String.fromCharCode(b)); + }else{ + const aChar = [] /* multi-byte char buffer */; + for(let x = 0; (x < nChar) && (i+x < end); ++x) aChar[x] = cur.src[i+x]; + cur.sb.push( + Util.utf8Decode( new Uint8Array(aChar) ) + ); + i += nChar-1; + } + break; + } + } + } + cur.pos = i; + const rv = cur.sb.join(''); + if( i==cur.src.byteLength && 0==rv.length ){ + return null /* EOF */; + } + return rv; + }/*getLine()*/ + + /** + Fetches the next line then resets the cursor to its pre-call + state. consumePeeked() can be used to consume this peeked line + without having to re-parse it. + */ + peekLine(){ + const cur = this.#cursor; + const oldPos = cur.pos; + const oldPB = cur.putbackPos; + const oldPBL = cur.putbackLineNo; + const oldLine = cur.lineNo; + try { + return this.getLine(); + }finally{ + cur.peekedPos = cur.pos; + cur.peekedLineNo = cur.lineNo; + cur.pos = oldPos; + cur.lineNo = oldLine; + cur.putbackPos = oldPB; + cur.putbackLineNo = oldPBL; + } + } + + + /** + Only valid after calling peekLine() and before calling getLine(). + This places the cursor to the position it would have been at had + the peekLine() had been fetched with getLine(). + */ + consumePeeked(){ + const cur = this.#cursor; + cur.pos = cur.peekedPos; + cur.lineNo = cur.peekedLineNo; + } + + /** + Restores the cursor to the position it had before the previous + call to getLine(). + */ + putbackLine(){ + const cur = this.#cursor; + cur.pos = cur.putbackPos; + cur.lineNo = cur.putbackLineNo; + } + +}/*TestScript*/; + +//! --close command +class CloseDbCommand extends Command { + process(t, ts, argv){ + this.argcCheck(ts,argv,0,1); + let id; + if(argv.length>1){ + const arg = argv[1]; + if( "all" === arg ){ + t.closeAllDbs(); + return; + } + else{ + id = parseInt(arg); + } + }else{ + id = t.currentDbId(); + } + t.closeDb(id); + } +} + +//! --column-names command +class ColumnNamesCommand extends Command { + process( st, ts, argv ){ + this.argcCheck(ts,argv,1); + st.outputColumnNames( !!parseInt(argv[1]) ); + } +} + +//! --db command +class DbCommand extends Command { + process(t, ts, argv){ + this.argcCheck(ts,argv,1); + t.currentDbId( parseInt(argv[1]) ); + } +} + +//! --glob command +class GlobCommand extends Command { + #negate = false; + constructor(negate=false){ + super(); + this.#negate = negate; + } + + process(t, ts, argv){ + this.argcCheck(ts,argv,1,-1); + t.incrementTestCounter(); + const sql = t.takeInputBuffer(); + let rc = t.execSql(null, true, ResultBufferMode.ESCAPED, + ResultRowMode.ONELINE, sql); + const result = t.getResultText(); + const sArgs = Util.argvToString(argv); + //t2.verbose2(argv[0]," rc = ",rc," result buffer:\n", result,"\nargs:\n",sArgs); + const glob = Util.argvToString(argv); + rc = Util.strglob(glob, result); + if( (this.#negate && 0===rc) || (!this.#negate && 0!==rc) ){ + ts.toss(argv[0], " mismatch: ", glob," vs input: ",result); + } + } +} + +//! --notglob command +class NotGlobCommand extends GlobCommand { + constructor(){super(true);} +} + +//! --open command +class OpenDbCommand extends Command { + #createIfNeeded = false; + constructor(createIfNeeded=false){ + super(); + this.#createIfNeeded = createIfNeeded; + } + process(t, ts, argv){ + this.argcCheck(ts,argv,1); + t.openDb(argv[1], this.#createIfNeeded); + } +} + +//! --new command +class NewDbCommand extends OpenDbCommand { + constructor(){ super(true); } +} + +//! Placeholder dummy/no-op commands +class NoopCommand extends Command { + process(t, ts, argv){} +} + +//! --null command +class NullCommand extends Command { + process(st, ts, argv){ + this.argcCheck(ts,argv,1); + st.nullValue( argv[1] ); + } +} + +//! --print command +class PrintCommand extends Command { + process(st, ts, argv){ + st.out(ts.getOutputPrefix(),': '); + if( 1==argv.length ){ + st.out( st.getInputText() ); + }else{ + st.outln( Util.argvToString(argv) ); + } + } +} + +//! --result command +class ResultCommand extends Command { + #bufferMode; + constructor(resultBufferMode = ResultBufferMode.ESCAPED){ + super(); + this.#bufferMode = resultBufferMode; + } + process(t, ts, argv){ + this.argcCheck(ts,argv,0,-1); + t.incrementTestCounter(); + const sql = t.takeInputBuffer(); + //ts.verbose2(argv[0]," SQL =\n",sql); + t.execSql(null, false, this.#bufferMode, ResultRowMode.ONELINE, sql); + const result = t.getResultText().trim(); + const sArgs = argv.length>1 ? Util.argvToString(argv) : ""; + if( result !== sArgs ){ + t.outln(argv[0]," FAILED comparison. Result buffer:\n", + result,"\nExpected result:\n",sArgs); + ts.toss(argv[0]+" comparison failed."); + } + } +} + +//! --json command +class JsonCommand extends ResultCommand { + constructor(){ super(ResultBufferMode.ASIS); } +} + +//! --run command +class RunCommand extends Command { + process(t, ts, argv){ + this.argcCheck(ts,argv,0,1); + const pDb = (1==argv.length) + ? t.currentDb() : t.getDbById( parseInt(argv[1]) ); + const sql = t.takeInputBuffer(); + const rc = t.execSql(pDb, false, ResultBufferMode.NONE, + ResultRowMode.ONELINE, sql); + if( 0!==rc && t.verbosity()>0 ){ + const msg = sqlite3.capi.sqlite3_errmsg(pDb); + ts.verbose2(argv[0]," non-fatal command error #",rc,": ", + msg,"\nfor SQL:\n",sql); + } + } +} + +//! --tableresult command +class TableResultCommand extends Command { + #jsonMode; + constructor(jsonMode=false){ + super(); + this.#jsonMode = jsonMode; + } + process(t, ts, argv){ + this.argcCheck(ts,argv,0); + t.incrementTestCounter(); + let body = ts.fetchCommandBody(t); + if( null===body ) ts.toss("Missing ",argv[0]," body."); + body = body.trim(); + if( !body.endsWith("\n--end") ){ + ts.toss(argv[0], " must be terminated with --end\\n"); + }else{ + body = body.substring(0, body.length-6); + } + const globs = body.split(/\s*\n\s*/); + if( globs.length < 1 ){ + ts.toss(argv[0], " requires 1 or more ", + (this.#jsonMode ? "json snippets" : "globs"),"."); + } + const sql = t.takeInputBuffer(); + t.execSql(null, true, + this.#jsonMode ? ResultBufferMode.ASIS : ResultBufferMode.ESCAPED, + ResultRowMode.NEWLINE, sql); + const rbuf = t.getResultText().trim(); + const res = rbuf.split(/\r?\n/); + if( res.length !== globs.length ){ + ts.toss(argv[0], " failure: input has ", res.length, + " row(s) but expecting ",globs.length); + } + for(let i = 0; i < res.length; ++i){ + const glob = globs[i].replaceAll(/\s+/g," ").trim(); + //ts.verbose2(argv[0]," <<",glob,">> vs <<",res[i],">>"); + if( this.#jsonMode ){ + if( glob!==res[i] ){ + ts.toss(argv[0], " json <<",glob, ">> does not match: <<", + res[i],">>"); + } + }else if( 0!=Util.strglob(glob, res[i]) ){ + ts.toss(argv[0], " glob <<",glob,">> does not match: <<",res[i],">>"); + } + } + } +} + +//! --json-block command +class JsonBlockCommand extends TableResultCommand { + constructor(){ super(true); } +} + +//! --testcase command +class TestCaseCommand extends Command { + process(tester, script, argv){ + this.argcCheck(script, argv,1); + script.testCaseName(argv[1]); + tester.clearResultBuffer(); + tester.clearInputBuffer(); + } +} + + +//! --verbosity command +class VerbosityCommand extends Command { + process(t, ts, argv){ + this.argcCheck(ts,argv,1); + ts.verbosity( parseInt(argv[1]) ); + } +} + +class CommandDispatcher { + static map = newObj(); + + static getCommandByName(name){ + let rv = CommandDispatcher.map[name]; + if( rv ) return rv; + switch(name){ + case "close": rv = new CloseDbCommand(); break; + case "column-names": rv = new ColumnNamesCommand(); break; + case "db": rv = new DbCommand(); break; + case "glob": rv = new GlobCommand(); break; + case "json": rv = new JsonCommand(); break; + case "json-block": rv = new JsonBlockCommand(); break; + case "new": rv = new NewDbCommand(); break; + case "notglob": rv = new NotGlobCommand(); break; + case "null": rv = new NullCommand(); break; + case "oom": rv = new NoopCommand(); break; + case "open": rv = new OpenDbCommand(); break; + case "print": rv = new PrintCommand(); break; + case "result": rv = new ResultCommand(); break; + case "run": rv = new RunCommand(); break; + case "tableresult": rv = new TableResultCommand(); break; + case "testcase": rv = new TestCaseCommand(); break; + case "verbosity": rv = new VerbosityCommand(); break; + } + if( rv ){ + CommandDispatcher.map[name] = rv; + } + return rv; + } + + static dispatch(tester, testScript, argv){ + const cmd = CommandDispatcher.getCommandByName(argv[0]); + if( !cmd ){ + toss(UnknownCommand,testScript,argv[0]); + } + cmd.process(tester, testScript, argv); + } +}/*CommandDispatcher*/ + +const namespace = newObj({ + Command, + DbException, + IncompatibleDirective, + Outer, + SQLTester, + SQLTesterException, + TestScript, + TestScriptFailed, + UnknownCommand, + Util, + sqlite3 +}); + +export {namespace as default}; diff --git a/ext/wasm/SQLTester/SQLTester.run.mjs b/ext/wasm/SQLTester/SQLTester.run.mjs new file mode 100644 index 0000000000..bf20c2ceed --- /dev/null +++ b/ext/wasm/SQLTester/SQLTester.run.mjs @@ -0,0 +1,151 @@ +/* +** 2023-08-29 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains a test application for SQLTester.mjs. It loads +** test scripts and runs them through the SQLTester class. +*/ +import {default as ns} from './SQLTester.mjs'; +import {default as allTests} from './test-list.mjs'; + +globalThis.sqlite3 = ns.sqlite3; +const log = function f(...args){ + console.log('SQLTester.run:',...args); + return f; +}; + +const out = function f(...args){ return f.outer.out(...args) }; +out.outer = new ns.Outer(); +out.outer.getOutputPrefix = ()=>'SQLTester.run: '; +const outln = (...args)=>{ return out.outer.outln(...args) }; + +const affirm = function(expr, msg){ + if( !expr ){ + throw new Error(arguments[1] + ? ("Assertion failed: "+arguments[1]) + : "Assertion failed"); + } +} + +let ts = new ns.TestScript('SQLTester-sanity-check.test',` +/* +** This is a comment. There are many like it but this one is mine. +** +** SCRIPT_MODULE_NAME: sanity-check-0 +** xMIXED_MODULE_NAME: mixed-module +** xMODULE_NAME: module-name +** xREQUIRED_PROPERTIES: small fast reliable +** xREQUIRED_PROPERTIES: RECURSIVE_TRIGGERS +** xREQUIRED_PROPERTIES: TEMPSTORE_FILE TEMPSTORE_MEM +** xREQUIRED_PROPERTIES: AUTOVACUUM INCRVACUUM +** +*/ +/* --verbosity 3 */ +/* ---must-fail */ +/* # must fail */ +/* --verbosity 0 */ +--print Hello, world. +--close all +--oom +--db 0 +--new my.db +--null zilch +--testcase 1.0 +SELECT 1, null; +--result 1 zilch +--glob *zil* +--notglob *ZIL* +SELECT 1, 2; +intentional error; +--run +/* ---intentional-failure */ +--testcase json-1 +SELECT json_array(1,2,3) +--json [1,2,3] +--testcase tableresult-1 + select 1, 'a' UNION + select 2, 'b' UNION + select 3, 'c' ORDER by 1 +--tableresult + # [a-z] + 2 b + 3 c +--end +--testcase json-block-1 + select json_array(1,2,3); + select json_object('a',1,'b',2); +--json-block + [1,2,3] + {"a":1,"b":2} +--end +--testcase col-names-on +--column-names 1 + select 1 as 'a', 2 as 'b'; +--result a 1 b 2 +--testcase col-names-off +--column-names 0 + select 1 as 'a', 2 as 'b'; +--result 1 2 +--close +--testcase the-end +--print Until next time +`); + +const sqt = new ns.SQLTester() + .setLogger(console.log.bind(console)) + .verbosity(1) + .addTestScript(ts); +sqt.outer().outputPrefix(''); + +const runTests = function(){ + try{ + if( 0 ){ + affirm( !sqt.getCurrentDb(), 'sqt.getCurrentDb()' ); + sqt.openDb('/foo.db', true); + affirm( !!sqt.getCurrentDb(),'sqt.getCurrentDb()' ); + affirm( 'zilch' !== sqt.nullValue() ); + ts.run(sqt); + affirm( 'zilch' === sqt.nullValue() ); + sqt.addTestScript(ts); + }else{ + for(const t of allTests){ + sqt.addTestScript( new ns.TestScript(t) ); + } + allTests.length = 0; + } + sqt.runTests(); + }finally{ + //log( "Metrics:", sqt.metrics ); + sqt.reset(); + } +}; + +if( globalThis.WorkerGlobalScope ){ + const wPost = (type,payload)=>globalThis.postMessage({type, payload}); + globalThis.onmessage = function({data}){ + switch(data.type){ + case 'run-tests':{ + try{ runTests(); } + finally{ wPost('tests-end', sqt.metrics); } + break; + } + default: + log("unhandled onmessage: ",data); + break; + } + }; + sqt.setLogger((msg)=>{ + wPost('stdout', {message: msg}); + }); + wPost('is-ready'); + //globalThis.onmessage({data:{type:'run-tests'}}); +}else{ + runTests(); +} diff --git a/ext/wasm/SQLTester/index.html b/ext/wasm/SQLTester/index.html new file mode 100644 index 0000000000..2d85b91163 --- /dev/null +++ b/ext/wasm/SQLTester/index.html @@ -0,0 +1,125 @@ + + + + + + + + + SQLTester + + + +

    SQLTester for JS/WASM

    +

    This app reads in a build-time-defined set of SQLTester test + scripts and runs them through the test suite. +

    +
    + Options + + + + + +
    +
    Test output will go here.
    + + + + diff --git a/ext/wasm/SQLTester/touint8array.c b/ext/wasm/SQLTester/touint8array.c new file mode 100644 index 0000000000..b03ad42f03 --- /dev/null +++ b/ext/wasm/SQLTester/touint8array.c @@ -0,0 +1,29 @@ +/* +** 2023-08-29 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains a tool for writing out the contents of stdin as +** a comma-separated list of numbers, one per byte. +*/ + +#include +int main(int argc, char const **argv){ + int i; + int rc = 0, colWidth = 30; + int ch; + printf("["); + for( i=0; EOF!=(ch = fgetc(stdin)); ++i ){ + if( 0!=i ) printf(","); + if( i && 0==(i%colWidth) ) puts(""); + printf("%d",ch); + } + printf("]"); + return rc; +} diff --git a/ext/wasm/api/EXPORTED_FUNCTIONS.sqlite3-core b/ext/wasm/api/EXPORTED_FUNCTIONS.sqlite3-core new file mode 100644 index 0000000000..5060545102 --- /dev/null +++ b/ext/wasm/api/EXPORTED_FUNCTIONS.sqlite3-core @@ -0,0 +1,157 @@ +_malloc +_free +_realloc +_sqlite3_aggregate_context +_sqlite3_auto_extension +_sqlite3_bind_blob +_sqlite3_bind_double +_sqlite3_bind_int +_sqlite3_bind_int64 +_sqlite3_bind_null +_sqlite3_bind_parameter_count +_sqlite3_bind_parameter_index +_sqlite3_bind_parameter_name +_sqlite3_bind_pointer +_sqlite3_bind_text +_sqlite3_busy_handler +_sqlite3_busy_timeout +_sqlite3_cancel_auto_extension +_sqlite3_changes +_sqlite3_changes64 +_sqlite3_clear_bindings +_sqlite3_close_v2 +_sqlite3_collation_needed +_sqlite3_column_blob +_sqlite3_column_bytes +_sqlite3_column_count +_sqlite3_column_decltype +_sqlite3_column_double +_sqlite3_column_int +_sqlite3_column_int64 +_sqlite3_column_name +_sqlite3_column_text +_sqlite3_column_type +_sqlite3_column_value +_sqlite3_commit_hook +_sqlite3_compileoption_get +_sqlite3_compileoption_used +_sqlite3_complete +_sqlite3_context_db_handle +_sqlite3_create_collation +_sqlite3_create_collation_v2 +_sqlite3_create_function +_sqlite3_create_function_v2 +_sqlite3_data_count +_sqlite3_db_filename +_sqlite3_db_handle +_sqlite3_db_name +_sqlite3_db_readonly +_sqlite3_db_status +_sqlite3_db_status64 +_sqlite3_deserialize +_sqlite3_errcode +_sqlite3_errmsg +_sqlite3_error_offset +_sqlite3_errstr +_sqlite3_exec +_sqlite3_expanded_sql +_sqlite3_extended_errcode +_sqlite3_extended_result_codes +_sqlite3_file_control +_sqlite3_finalize +_sqlite3_free +_sqlite3_get_auxdata +_sqlite3_get_autocommit +_sqlite3_initialize +_sqlite3_interrupt +_sqlite3_is_interrupted +_sqlite3_keyword_count +_sqlite3_keyword_name +_sqlite3_keyword_check +_sqlite3_last_insert_rowid +_sqlite3_libversion +_sqlite3_libversion_number +_sqlite3_limit +_sqlite3_malloc +_sqlite3_malloc64 +_sqlite3_msize +_sqlite3_open +_sqlite3_open_v2 +_sqlite3_overload_function +_sqlite3_prepare_v2 +_sqlite3_prepare_v3 +_sqlite3_randomness +_sqlite3_realloc +_sqlite3_realloc64 +_sqlite3_reset +_sqlite3_reset_auto_extension +_sqlite3_result_blob +_sqlite3_result_double +_sqlite3_result_error +_sqlite3_result_error_code +_sqlite3_result_error_nomem +_sqlite3_result_error_toobig +_sqlite3_result_int +_sqlite3_result_int64 +_sqlite3_result_null +_sqlite3_result_pointer +_sqlite3_result_subtype +_sqlite3_result_text +_sqlite3_result_zeroblob +_sqlite3_result_zeroblob64 +_sqlite3_rollback_hook +_sqlite3_serialize +_sqlite3_set_auxdata +_sqlite3_set_last_insert_rowid +_sqlite3_set_errmsg +_sqlite3_shutdown +_sqlite3_sourceid +_sqlite3_sql +_sqlite3_status +_sqlite3_status64 +_sqlite3_step +_sqlite3_stmt_busy +_sqlite3_stmt_explain +_sqlite3_stmt_isexplain +_sqlite3_stmt_readonly +_sqlite3_stmt_status +_sqlite3_strglob +_sqlite3_stricmp +_sqlite3_strlike +_sqlite3_strnicmp +_sqlite3_table_column_metadata +_sqlite3_total_changes +_sqlite3_total_changes64 +_sqlite3_trace_v2 +_sqlite3_txn_state +_sqlite3_update_hook +_sqlite3_uri_boolean +_sqlite3_uri_int64 +_sqlite3_uri_key +_sqlite3_uri_parameter +_sqlite3_user_data +_sqlite3_value_blob +_sqlite3_value_bytes +_sqlite3_value_double +_sqlite3_value_dup +_sqlite3_value_free +_sqlite3_value_frombind +_sqlite3_value_int +_sqlite3_value_int64 +_sqlite3_value_nochange +_sqlite3_value_numeric_type +_sqlite3_value_pointer +_sqlite3_value_subtype +_sqlite3_value_text +_sqlite3_value_type +_sqlite3_vfs_find +_sqlite3_vfs_register +_sqlite3_vfs_unregister +_sqlite3_vtab_collation +_sqlite3_vtab_distinct +_sqlite3_vtab_in +_sqlite3_vtab_in_first +_sqlite3_vtab_in_next +_sqlite3_vtab_nochange +_sqlite3_vtab_on_conflict +_sqlite3_vtab_rhs_value diff --git a/ext/wasm/api/EXPORTED_FUNCTIONS.sqlite3-extras b/ext/wasm/api/EXPORTED_FUNCTIONS.sqlite3-extras new file mode 100644 index 0000000000..e8304b5f2a --- /dev/null +++ b/ext/wasm/api/EXPORTED_FUNCTIONS.sqlite3-extras @@ -0,0 +1,68 @@ +_sqlite3_column_database_name +_sqlite3_column_origin_name +_sqlite3_column_table_name +_sqlite3_create_module +_sqlite3_create_module_v2 +_sqlite3_create_window_function +_sqlite3_declare_vtab +_sqlite3_drop_modules +_sqlite3_preupdate_blobwrite +_sqlite3_preupdate_count +_sqlite3_preupdate_depth +_sqlite3_preupdate_hook +_sqlite3_preupdate_new +_sqlite3_preupdate_old +_sqlite3_progress_handler +_sqlite3_set_authorizer +_sqlite3_vtab_collation +_sqlite3_vtab_distinct +_sqlite3_vtab_in +_sqlite3_vtab_in_first +_sqlite3_vtab_in_next +_sqlite3_vtab_nochange +_sqlite3_vtab_on_conflict +_sqlite3_vtab_rhs_value +_sqlite3changegroup_add +_sqlite3changegroup_add_strm +_sqlite3changegroup_delete +_sqlite3changegroup_new +_sqlite3changegroup_output +_sqlite3changegroup_output_strm +_sqlite3changeset_apply +_sqlite3changeset_apply_strm +_sqlite3changeset_apply_v2 +_sqlite3changeset_apply_v2_strm +_sqlite3changeset_apply_v3 +_sqlite3changeset_apply_v3_strm +_sqlite3changeset_concat +_sqlite3changeset_concat_strm +_sqlite3changeset_conflict +_sqlite3changeset_finalize +_sqlite3changeset_fk_conflicts +_sqlite3changeset_invert +_sqlite3changeset_invert_strm +_sqlite3changeset_new +_sqlite3changeset_next +_sqlite3changeset_old +_sqlite3changeset_op +_sqlite3changeset_pk +_sqlite3changeset_start +_sqlite3changeset_start_strm +_sqlite3changeset_start_v2 +_sqlite3changeset_start_v2_strm +_sqlite3session_attach +_sqlite3session_changeset +_sqlite3session_changeset_size +_sqlite3session_changeset_strm +_sqlite3session_config +_sqlite3session_create +_sqlite3session_delete +_sqlite3session_diff +_sqlite3session_enable +_sqlite3session_indirect +_sqlite3session_isempty +_sqlite3session_memory_used +_sqlite3session_object_config +_sqlite3session_patchset +_sqlite3session_patchset_strm +_sqlite3session_table_filter diff --git a/ext/wasm/api/EXPORTED_FUNCTIONS.sqlite3-see b/ext/wasm/api/EXPORTED_FUNCTIONS.sqlite3-see new file mode 100644 index 0000000000..83f3a97dbc --- /dev/null +++ b/ext/wasm/api/EXPORTED_FUNCTIONS.sqlite3-see @@ -0,0 +1,5 @@ +_sqlite3_key +_sqlite3_key_v2 +_sqlite3_rekey +_sqlite3_rekey_v2 +_sqlite3_activate_see diff --git a/ext/wasm/api/EXPORTED_RUNTIME_METHODS.sqlite3-api b/ext/wasm/api/EXPORTED_RUNTIME_METHODS.sqlite3-api new file mode 100644 index 0000000000..aab1d8bd37 --- /dev/null +++ b/ext/wasm/api/EXPORTED_RUNTIME_METHODS.sqlite3-api @@ -0,0 +1,3 @@ +FS +wasmMemory + diff --git a/ext/wasm/api/README.md b/ext/wasm/api/README.md new file mode 100644 index 0000000000..3c9669e6ba --- /dev/null +++ b/ext/wasm/api/README.md @@ -0,0 +1,213 @@ +# sqlite3-api.js And Friends + +This is the README for the files `sqlite3-*.js` and +`sqlite3-wasm.c`. This collection of files is used to build a +single-file distribution of the sqlite3 JS/WASM API. It is broken into +multiple JS files because: + +1. To facilitate including or excluding certain components for + specific use cases. e.g. by removing `sqlite3-api-oo1.js` if the + OO#1 API is not needed. + +2. To facilitate modularizing the pieces for use in different WASM + build environments. e.g. the files `post-js-*.js` are for use with + Emscripten's `--post-js` feature, and nowhere else. + As-yet-hypothetical comparable toolchains would necessarily have + similar facilities. + +3. Certain components must be in their own standalone files in order + to be loaded as JS Workers. + +The structure described here is the current state of things, as of +this writing, but is not set in stone forever and may change at any +time. This doc targets maintainers of this code and those wanting to +dive in to the details, not end user. + +First off, a pikchr of the proverbial onion: + +```pikchr toggle center +scale = 0.85 +D0: dot invis +define matryoshka { + $rad = $rad + $2 + circle with se at D0.c radius $rad + text at last.nw $1 below ljust +# $anchor = last circle.e +} +$rad = 5mm +C0: circle with se at D0.c "sqlite3-api.js" fit +$rad = C0.width / 2 +matryoshka("--post-js",5mm) +matryoshka("Emscripten",12mm) +circle with nw at 7mm s of 3mm e of last circle.nw "--pre-js" fit +matryoshka("--extern-pre/post-js",9mm) +matryoshka("sqlite3.js",6mm) + +#CX: last circle +right + +CW: file with w at 15mm e of 3rd circle.ne "sqlite3.wasm" fit +arrow from CW.w to 3rd circle.ne + +CC: circle with e at 1cm w of last circle.w "End User" fit radius 1cm +arrow <- from CC.e to 6th circle.w +``` + +Actually, `sqlite3.js` and `--extern-js` are the same. The former is +what the client sees and the latter is how it looks from a code +maintenance point of view. + +At the center of the onion is `sqlite3-api.js`, which gets generated +by concatenating the following files together in their listed order: + +- **`sqlite3-api-prologue.js`**\ + Contains the initial bootstrap setup of the sqlite3 API + objects. This is exposed as a function, rather than objects, so that + the next step can pass in a config object which abstracts away parts + of the WASM environment, to facilitate plugging it in to arbitrary + WASM toolchains. The bootstrapping function gets removed from the + global scope in a later stage of the bootstrapping process. +- **`../common/whwasmutil.js`**\ + A semi-third-party collection of JS/WASM utility code intended to + replace much of the Emscripten glue. The sqlite3 APIs internally use + these APIs instead of their Emscripten counterparts, in order to be + more portable to arbitrary WASM toolchains and help reduce + regressions caused by toolchain-level behavioral changes. This API + is configurable, in principle, for use with arbitrary WASM + toolchains. It is "semi-third-party" in that it was created in order + to support this tree but is standalone and maintained together + with... +- **`../jaccwabyt/jaccwabyt.js`**\ + Another semi-third-party API which creates bindings between JS + and C structs, such that changes to the struct state from either JS + or C are visible to the other end of the connection. This is also an + independent spinoff project, conceived for the sqlite3 project but + maintained separately. +- **`sqlite3-api-glue.js`**\ + Invokes functionality exposed by the previous two files to flesh out + low-level parts of `sqlite3-api-prologue.js`. Most of these pieces + involve populating the `sqlite3.capi.wasm` object and creating + `sqlite3.capi.sqlite3_...()` bindings. This file also deletes most + global-scope symbols the above files create, effectively moving them + into the scope being used for initializing the API. +- **`/sqlite3-api-build-version.js`**\ + Gets created by the build process and populates the + `sqlite3.version` object. This part is not critical, but records the + version of the library against which this module was built. +- **`sqlite3-api-oo1.js`**\ + Provides a high-level object-oriented wrapper to the lower-level C + API, colloquially known as OO API #1. Its API is similar to other + high-level sqlite3 JS wrappers and should feel relatively familiar + to anyone familiar with such APIs. It is not a "required component" + and can be elided from builds which do not want it. +- **`sqlite3-api-worker1.js`**\ + A Worker-thread-based API which uses OO API #1 to provide an + interface to a database which can be driven from the main Window + thread via the Worker message-passing interface. Like OO API #1, + this is an optional component, offering one of any number of + potential implementations for such an API. + - **`sqlite3-worker1.js`**\ + Is not part of the amalgamated sources and is intended to be + loaded by a client Worker thread. It loads the sqlite3 module + and runs the Worker #1 API which is implemented in + `sqlite3-api-worker1.js`. + - **`sqlite3-worker1-promiser.js`**\ + Is likewise not part of the amalgamated sources and provides + a Promise-based interface into the Worker #1 API. This is + a far user-friendlier way to interface with databases running + in a Worker thread. +- **`sqlite3-vfs-helper.js`**\ + Installs the `sqlite3.vfs` namespace, which contain helpers for use + by downstream code which creates `sqlite3_vfs` implementations. +- **`sqlite3-vtab-helper.js`**\ + Installs the `sqlite3.vtab` namespace, which contain helpers for use + by downstream code which creates `sqlite3_module` implementations. +- **`sqlite3-vfs-opfs.c-pp.js`**\ + is an sqlite3 VFS implementation which supports the [Origin-Private + FileSystem (OPFS)][OPFS] as a storage layer to provide persistent + storage for database files in a browser. It requires... + - **`sqlite3-opfs-async-proxy.js`**\ + is the asynchronous backend part of the [OPFS][] proxy. It + speaks directly to the (async) OPFS API and channels those + results back to its synchronous counterpart. This file, because + it must be started in its own Worker, is not part of the + amalgamation. +- **`sqlite3-vfs-opfs-sahpool.c-pp.js`**\ + is another sqlite3 VFS supporting the [OPFS][], but uses a + completely different approach than the above-listed one. +- **`sqlite3-api-cleanup.js`**\ + The previous files do not immediately extend the library. Instead + they add callback functions to be called during its + bootstrapping. Some also temporarily create global objects in order + to communicate their state to the files which follow them. This file + cleans up any dangling globals and runs the API bootstrapping + process, which is what finally executes the initialization code + installed by the previous files. As of this writing, this code + ensures that the previous files leave no more than a single global + symbol installed - `sqlite3InitModule()`. When adapting the API for + non-Emscripten toolchains, this "should" be the only file, of those + in this list, where changes are needed. The Emscripten-specific + pieces described below may also require counterparts in any as-yet + hypothetical alternative build. + + +**Files with the extension `.c-pp.js`** are intended [to be processed +with `c-pp`](#c-pp), noting that such preprocessing may be applied +after all of the relevant files are concatenated. The `.c-pp.js` +extension is used primarily to keep the code maintainers cognisant of +the fact that those files contain constructs which may not run as-is +in any given JavaScript environment. + +The build process glues those files together, resulting in +`sqlite3-api.js`, which is everything except for the Emscripten-specific +files detailed below (into which `sqlite3-api.js` gets injected). + +The non-JS outlier file is `sqlite3-wasm.c`: it is a proxy for +`sqlite3.c` which `#include`'s that file and adds a handful of +WASM-specific helper functions, at least two of which requires access +to private/static `sqlite3.c` internals. `sqlite3.wasm` is compiled +from this file rather than `sqlite3.c`. + +The following Emscripten-specific files are injected into the +build-generated `sqlite3.js` along with `sqlite3-api.js`. + +- **`extern-pre-js.js`**\ + Emscripten-specific header for Emscripten's `--extern-pre-js` + flag. As of this writing, that file is only used for experimentation + purposes and holds no code relevant to the production deliverables. +- **`pre-js.c-pp.js`**\ + Emscripten-specific header for Emscripten's `--pre-js` flag. This + file overrides certain Emscripten behavior before Emscripten does + most of its work. +- **`post-js-header.js`**\ + Emscripten-specific header for the `--post-js` input. It opens up, + but does not close, a function used for initializing the library. +- (**`sqlite3-api.js`** gets sandwiched between these ↑ two + ↓ files.) +- **`post-js-footer.js`**\ + Emscripten-specific footer for the `--post-js` input. This closes + off the function opened by `post-js-header.js`. +- **`extern-post-js.c-pp.js`**\ + Emscripten-specific header for Emscripten's `--extern-post-js` + flag. This file is run in the global scope. It overwrites the + Emscripten-installed `sqlite3InitModule()` function with one which + first runs the original implementation, then runs the function + installed by `post-js-header/footer.js` to initialize the library, + then initializes the asynchronous parts of the sqlite3 module (for + example, the [OPFS][] VFS support). Its final step is to return a + Promise of the sqlite3 namespace object to the caller of + `sqlite3InitModule()` (the library user). + + +Preprocessing of Source Files +------------------------------------------------------------------------ + +Certain files in the build require preprocessing to filter in/out +parts which differ between vanilla JS, ES6 Modules, and node.js +builds. The preprocessor application itself is in +[`c-pp.c`](/file/ext/wasm/c-pp.c) and the complete technical details +of such preprocessing are maintained in +[`GNUMakefile`](/file/ext/wasm/GNUmakefile). + + +[OPFS]: https://developer.mozilla.org/en-US/docs/Web/API/File_System_API/Origin_private_file_system diff --git a/ext/wasm/api/extern-post-js.c-pp.js b/ext/wasm/api/extern-post-js.c-pp.js new file mode 100644 index 0000000000..606e02ae28 --- /dev/null +++ b/ext/wasm/api/extern-post-js.c-pp.js @@ -0,0 +1,135 @@ + +/* ^^^^ ACHTUNG: blank line at the start is necessary because + Emscripten will not add a newline in some cases and we need + a blank line for a sed-based kludge for the ES6 build. + + extern-post-js.js must be appended to the resulting sqlite3.js + file. It gets its name from being used as the value for the + --extern-post-js=... Emscripten flag. This code, unlike most of the + associated JS code, runs outside of the Emscripten-generated module + init scope, in the current global scope. + + At the time this is run, the global-scope sqlite3InitModule + function will have just been defined. +*/ +//#if target:es6-module +const toExportForESM = +//#endif +(function(){ + //console.warn("this is extern-post-js"); + /** + In order to hide the sqlite3InitModule()'s resulting + Emscripten module from downstream clients (and simplify our + documentation by being able to elide those details), we hide that + function and expose a hand-written sqlite3InitModule() to return + the sqlite3 object (most of the time). + */ + const originalInit = sqlite3InitModule; + if(!originalInit){ + throw new Error("Expecting globalThis.sqlite3InitModule to be defined by the Emscripten build."); + } + /** + We need to add some state which our custom Module.locateFile() + can see, but an Emscripten limitation currently prevents us from + attaching it to the sqlite3InitModule function object: + + https://github.com/emscripten-core/emscripten/issues/18071 + + The only(?) current workaround is to temporarily stash this state + into the global scope and delete it when sqlite3InitModule() + is called. + */ + const sIMS = globalThis.sqlite3InitModuleState = Object.assign(Object.create(null),{ + moduleScript: globalThis?.document?.currentScript, + isWorker: ('undefined' !== typeof WorkerGlobalScope), + location: globalThis.location, + urlParams: globalThis?.location?.href + ? new URL(globalThis.location.href).searchParams + : new URLSearchParams(), + /* + It is literally impossible to reliably get the name of _this_ script + at runtime, so impossible to reliably derive X.wasm from script name + X.js. (This is apparently why Emscripten hard-codes the name of the + wasm file into their output.) Thus we need, at build-time, to set + the name of the WASM file which our custom instantiateWasm() should to + load. The build process populates this. + + Module.instantiateWasm() is found in pre-js.c-pp.js. + */ + wasmFilename: '@sqlite3.wasm@' /* replaced by the build process */ + }); + sIMS.debugModule = + sIMS.urlParams.has('sqlite3.debugModule') + ? (...args)=>console.warn('sqlite3.debugModule:',...args) + : ()=>{}; + + if(sIMS.urlParams.has('sqlite3.dir')){ + sIMS.sqlite3Dir = sIMS.urlParams.get('sqlite3.dir') +'/'; + }else if(sIMS.moduleScript){ + const li = sIMS.moduleScript.src.split('/'); + li.pop(); + sIMS.sqlite3Dir = li.join('/') + '/'; + } + + const sIM = globalThis.sqlite3InitModule = function ff(...args){ + //console.warn("Using replaced sqlite3InitModule()",globalThis.location); + return originalInit(...args).then((EmscriptenModule)=>{ + sIMS.debugModule("sqlite3InitModule() sIMS =",sIMS); + sIMS.debugModule("sqlite3InitModule() EmscriptenModule =",EmscriptenModule); + const s = EmscriptenModule.runSQLite3PostLoadInit( + sIMS, + EmscriptenModule /* see post-js-header/footer.js */, + !!ff.__isUnderTest + ); + sIMS.debugModule("sqlite3InitModule() sqlite3 =",s); + //const rv = s.asyncPostInit(); + //delete s.asyncPostInit; +//#if wasmfs + if('undefined'!==typeof WorkerGlobalScope && + EmscriptenModule['ENVIRONMENT_IS_PTHREAD']){ + /** Workaround for wasmfs-generated worker, which calls this + routine from each individual thread and requires that its + argument be returned. The if() condition above is fragile, + based solely on inspection of the offending code, not + public Emscripten details. */ + //console.warn("sqlite3InitModule() returning E-module.",EmscriptenModule); + return EmscriptenModule; + } +//#endif + return s; + }).catch((e)=>{ + console.error("Exception loading sqlite3 module:",e); + throw e; + }); + }; + sIM.ready = originalInit.ready; + + if(sIMS.moduleScript){ + let src = sIMS.moduleScript.src.split('/'); + src.pop(); + sIMS.scriptDir = src.join('/') + '/'; + } + sIMS.debugModule('extern-post-js.c-pp.js sqlite3InitModuleState =',sIMS); +//#if not target:es6-module +// Emscripten does not inject these module-loader bits in ES6 module +// builds and including them here breaks JS bundlers, so elide them +// from ESM builds. + /* Replace the various module exports performed by the Emscripten + glue... */ + if (typeof exports === 'object' && typeof module === 'object'){ + module.exports = sIM; + module.exports.default = sIM; + }else if( 'function'===typeof define && define.amd ){ + define([], ()=>sIM); + }else if (typeof exports === 'object'){ + exports["sqlite3InitModule"] = sIM; + } + /* AMD modules get injected in a way we cannot override, + so we can't handle those here. */ +//#endif // !target:es6-module + return sIM; +})(); +//#if target:es6-module +sqlite3InitModule = toExportForESM; +export default sqlite3InitModule; +//#endif diff --git a/ext/wasm/api/extern-pre-js.js b/ext/wasm/api/extern-pre-js.js new file mode 100644 index 0000000000..7d47d33e33 --- /dev/null +++ b/ext/wasm/api/extern-pre-js.js @@ -0,0 +1,7 @@ +/* extern-pre-js.js must be prepended to the resulting sqlite3.js + file. This file is currently only used for holding snippets during + test and development. + + It gets its name from being used as the value for the + --extern-pre-js=... Emscripten flag. +*/ diff --git a/ext/wasm/api/post-js-footer.js b/ext/wasm/api/post-js-footer.js new file mode 100644 index 0000000000..c6a2e1517c --- /dev/null +++ b/ext/wasm/api/post-js-footer.js @@ -0,0 +1,3 @@ +//console.warn("This is the end of the Module.runSQLite3PostLoadInit handler."); +}/*Module.runSQLite3PostLoadInit(...)*/; +//console.warn("This is the end of the setup of the (pending) Module.runSQLite3PostLoadInit"); diff --git a/ext/wasm/api/post-js-header.js b/ext/wasm/api/post-js-header.js new file mode 100644 index 0000000000..cdc6b3a38d --- /dev/null +++ b/ext/wasm/api/post-js-header.js @@ -0,0 +1,43 @@ +/** + post-js-header.js is to be prepended to other code to create + post-js.js for use with Emscripten's --post-js flag, so it gets + injected in the earliest stages of sqlite3InitModule(). + + This function wraps the whole SQLite3 library but does not + bootstrap it. + + Running this function will bootstrap the library and return + a Promise to the sqlite3 namespace object. +*/ +Module.runSQLite3PostLoadInit = function( + sqlite3InitScriptInfo /* populated by extern-post-js.c-pp.js */, + EmscriptenModule/*the Emscripten-style module object*/, + sqlite3IsUnderTest +){ + /** ^^^ Don't use Module.postRun, as that runs a different time + depending on whether this file is built with emcc 3.1.x or + 4.0.x. This function name is intentionally obnoxiously verbose to + ensure that we don't collide with current and future Emscripten + symbol names. */ + 'use strict'; + delete EmscriptenModule.runSQLite3PostLoadInit; + //console.warn("This is the start of Module.runSQLite3PostLoadInit()"); + /* This function will contain at least the following: + + - post-js-header.js => this file + - sqlite3-api-prologue.js => Bootstrapping bits for the following files + - common/whwasmutil.js => Generic JS/WASM glue + - jaccwabyt/jaccwabyt.js => C struct-binding glue + - sqlite3-api-glue.js => glues previous parts together + - sqlite3-api-oo1.js => SQLite3 OO API #1 + - sqlite3-api-worker1.js => Worker-based API + - sqlite3-vfs-helper.c-pp.js => Utilities for VFS impls + - sqlite3-vtab-helper.c-pp.js => Utilities for virtual table impls + - sqlite3-vfs-opfs.c-pp.js => OPFS VFS + - sqlite3-vfs-opfs-sahpool.c-pp.js => OPFS SAHPool VFS + - sqlite3-api-cleanup.js => final bootstrapping phase + - post-js-footer.js => this file's epilogue + + And all of that gets sandwiched between extern-pre-js.js and + extern-post-js.js. + */ diff --git a/ext/wasm/api/pre-js.c-pp.js b/ext/wasm/api/pre-js.c-pp.js new file mode 100644 index 0000000000..8a4a0f9fd0 --- /dev/null +++ b/ext/wasm/api/pre-js.c-pp.js @@ -0,0 +1,115 @@ +/** + BEGIN FILE: api/pre-js.js + + This file is intended to be prepended to the sqlite3.js build using + Emscripten's --pre-js=THIS_FILE flag (or equivalent). It is run + from inside of sqlite3InitModule(), after Emscripten's Module is + defined, but early enough that we can ammend, or even outright + replace, Module from here. + + Because this runs in-between Emscripten's own bootstrapping and + Emscripten's main work, we must be careful with file-local symbol + names. e.g. don't overwrite anything Emscripten defines and do not + use 'const' for local symbols which Emscripten might try to use for + itself. i.e. try to keep file-local symbol names obnoxiously + collision-resistant. +*/ +(function(Module){ + const sIMS = + globalThis.sqlite3InitModuleState/*from extern-post-js.c-pp.js*/ + || Object.assign(Object.create(null),{ + debugModule: ()=>{ + console.warn("globalThis.sqlite3InitModuleState is missing"); + } + }); + delete globalThis.sqlite3InitModuleState; + sIMS.debugModule('pre-js.js sqlite3InitModuleState =',sIMS); + + /** + This custom locateFile() tries to figure out where to load `path` + from. The intent is to provide a way for foo/bar/X.js loaded from a + Worker constructor or importScripts() to be able to resolve + foo/bar/X.wasm (in the latter case, with some help): + + 1) If URL param named the same as `path` is set, it is returned. + + 2) If sqlite3InitModuleState.sqlite3Dir is set, then (thatName + path) + is returned (it's assumed to end with '/'). + + 3) If this code is running in the main UI thread AND it was loaded + from a SCRIPT tag, the directory part of that URL is used + as the prefix. (This form of resolution unfortunately does not + function for scripts loaded via importScripts().) + + 4) If none of the above apply, (prefix+path) is returned. + + None of the above apply in ES6 builds, which uses a much simpler + approach. + */ + Module['locateFile'] = function(path, prefix) { +//#if target:es6-module + return new URL(path, import.meta.url).href; +//#else + 'use strict'; + let theFile; + const up = this.urlParams; + if(up.has(path)){ + theFile = up.get(path); + }else if(this.sqlite3Dir){ + theFile = this.sqlite3Dir + path; + }else if(this.scriptDir){ + theFile = this.scriptDir + path; + }else{ + theFile = prefix + path; + } + this.debugModule( + "locateFile(",arguments[0], ',', arguments[1],")", + 'sqlite3InitModuleState.scriptDir =',this.scriptDir, + 'up.entries() =',Array.from(up.entries()), + "result =", theFile + ); + return theFile; +//#endif target:es6-module + }.bind(sIMS); + +//#if Module.instantiateWasm +//#if not wasmfs + /** + Override Module.instantiateWasm(). + + A custom Module.instantiateWasm() does not work in WASMFS builds: + + https://github.com/emscripten-core/emscripten/issues/17951 + + In such builds we must disable this. + */ + Module['instantiateWasm'] = function callee(imports,onSuccess){ + const sims = this; + const uri = Module.locateFile( + sims.wasmFilename, ( + ('undefined'===typeof scriptDirectory/*var defined by Emscripten glue*/) + ? "" : scriptDirectory) + ); + sims.debugModule("instantiateWasm() uri =", uri, "sIMS =",this); + const wfetch = ()=>fetch(uri, {credentials: 'same-origin'}); + const finalThen = (arg)=>{ + arg.imports = imports; + sims.instantiateWasm = arg /* used by sqlite3-api-prologue.c-pp.js */; + onSuccess(arg.instance, arg.module); + }; + const loadWasm = WebAssembly.instantiateStreaming + ? async ()=> + WebAssembly + .instantiateStreaming(wfetch(), imports) + .then(finalThen) + : async ()=>// Safari < v15 + wfetch() + .then(response => response.arrayBuffer()) + .then(bytes => WebAssembly.instantiate(bytes, imports)) + .then(finalThen) + return loadWasm(); + }.bind(sIMS); +//#endif not wasmfs +//#endif Module.instantiateWasm +})(Module); +/* END FILE: api/pre-js.js. */ diff --git a/ext/wasm/api/sqlite3-api-cleanup.js b/ext/wasm/api/sqlite3-api-cleanup.js new file mode 100644 index 0000000000..2235663261 --- /dev/null +++ b/ext/wasm/api/sqlite3-api-cleanup.js @@ -0,0 +1,83 @@ +/* + 2022-07-22 + + The author disclaims copyright to this source code. In place of a + legal notice, here is a blessing: + + * May you do good and not evil. + * May you find forgiveness for yourself and forgive others. + * May you share freely, never taking more than you give. + + *********************************************************************** + + This file is the tail end of the sqlite3-api.js constellation, + intended to be appended after all other sqlite3-api-*.js files so + that it can finalize any setup and clean up any global symbols + temporarily used for setting up the API's various subsystems. + + In Emscripten builds it's run in the context of what amounts to a + Module.postRun handler, though it's no longer actually a postRun + handler because Emscripten 4.0 changed postRun semantics in an + incompatible way. + + In terms of amalgamation code placement, this file is appended + immediately after the final sqlite3-api-*.js piece. Those files + cooperate to prepare sqlite3ApiBootstrap() and this file calls it. + It is run within a context which gives it access to Emscripten's + Module object, after sqlite3.wasm is loaded but before + sqlite3ApiBootstrap() has been called. + + Because this code resides (after building) inside the function + installed by post-js-header.js, it has access to the +*/ +'use strict'; +if( 'undefined' === typeof EmscriptenModule/*from post-js-header.js*/ ){ + console.warn("This is not running in the context of Module.runSQLite3PostLoadInit()"); + throw new Error("sqlite3-api-cleanup.js expects to be running in the "+ + "context of its Emscripten module loader."); +} +try{ + /* Config options for sqlite3ApiBootstrap(). */ + const bootstrapConfig = Object.assign( + Object.create(null), + globalThis.sqlite3ApiBootstrap.defaultConfig, // default options + globalThis.sqlite3ApiConfig || {}, // optional client-provided options + /** The WASM-environment-dependent configuration for sqlite3ApiBootstrap() */ + { + memory: ('undefined'!==typeof wasmMemory) + ? wasmMemory + : EmscriptenModule['wasmMemory'], + exports: ('undefined'!==typeof wasmExports) + ? wasmExports /* emscripten >=3.1.44 */ + : (Object.prototype.hasOwnProperty.call(EmscriptenModule,'wasmExports') + ? EmscriptenModule['wasmExports'] + : EmscriptenModule['asm']/* emscripten <=3.1.43 */) + } + ); + + /** Figure out if this is a 32- or 64-bit WASM build. */ + bootstrapConfig.wasmPtrIR = + 'number'===(typeof bootstrapConfig.exports.sqlite3_libversion()) + ? 'i32' :'i64'; + const sIMS = sqlite3InitScriptInfo; + sIMS.debugModule("Bootstrapping lib config", sIMS); + + /** + For purposes of the Emscripten build, call sqlite3ApiBootstrap(). + Ideally clients should be able to inject their own config here, + but that's not practical in this particular build constellation + because of the order everything happens in. Clients may either + define globalThis.sqlite3ApiConfig or modify + globalThis.sqlite3ApiBootstrap.defaultConfig to tweak the default + configuration used by a no-args call to sqlite3ApiBootstrap(), + but must have first loaded their WASM module in order to be able + to provide the necessary configuration state. + */ + const p = globalThis.sqlite3ApiBootstrap(bootstrapConfig); + delete globalThis.sqlite3ApiBootstrap; + return p /* the eventual result of globalThis.sqlite3InitModule() */; +}catch(e){ + console.error("sqlite3ApiBootstrap() error:",e); + throw e; +} +throw new Error("Maintenance required: this line should never be reached"); diff --git a/ext/wasm/api/sqlite3-api-glue.c-pp.js b/ext/wasm/api/sqlite3-api-glue.c-pp.js new file mode 100644 index 0000000000..1c42b01508 --- /dev/null +++ b/ext/wasm/api/sqlite3-api-glue.c-pp.js @@ -0,0 +1,2019 @@ +/* + 2022-07-22 + + The author disclaims copyright to this source code. In place of a + legal notice, here is a blessing: + + * May you do good and not evil. + * May you find forgiveness for yourself and forgive others. + * May you share freely, never taking more than you give. + + *********************************************************************** + + This file glues together disparate pieces of JS which are loaded in + previous steps of the sqlite3-api.js bootstrapping process: + sqlite3-api-prologue.js, whwasmutil.js, and jaccwabyt.js. It + initializes the main API pieces so that the downstream components + (e.g. sqlite3-api-oo1.js) have all of the infrastructure that they + need. +*/ +globalThis.sqlite3ApiBootstrap.initializers.push(function(sqlite3){ + 'use strict'; + const toss = (...args)=>{throw new Error(args.join(' '))}; + const capi = sqlite3.capi, wasm = sqlite3.wasm, util = sqlite3.util; + globalThis.WhWasmUtilInstaller(wasm); + delete globalThis.WhWasmUtilInstaller; + + if(0){ + /** + Please keep this block around as a maintenance reminder + that we cannot rely on this type of check. + + This block fails on Safari, per a report at + https://sqlite.org/forum/forumpost/e5b20e1feb. + + It turns out that what Safari serves from the indirect function + table (e.g. wasm.functionEntry(X)) is anonymous functions which + wrap the WASM functions, rather than returning the WASM + functions themselves. That means comparison of such functions + is useless for determining whether or not we have a specific + function from wasm.exports. i.e. if function X is indirection + function table entry N then wasm.exports.X is not equal to + wasm.functionEntry(N) in Safari, despite being so in the other + browsers. + */ + /** + Find a mapping for SQLITE_WASM_DEALLOC, which the API + guarantees is a WASM pointer to the same underlying function as + wasm.dealloc() (noting that wasm.dealloc() is permitted to be a + JS wrapper around the WASM function). There is unfortunately no + O(1) algorithm for finding this pointer: we have to walk the + WASM indirect function table to find it. However, experience + indicates that that particular function is always very close to + the front of the table (it's been entry #3 in all relevant + tests). + */ + const dealloc = wasm.exports[sqlite3.config.deallocExportName]; + const nFunc = wasm.functionTable().length; + let i; + for(i = 0; i < nFunc; ++i){ + const e = wasm.functionEntry(i); + if(dealloc === e){ + capi.SQLITE_WASM_DEALLOC = i; + break; + } + } + if(dealloc !== wasm.functionEntry(capi.SQLITE_WASM_DEALLOC)){ + toss("Internal error: cannot find function pointer for SQLITE_WASM_DEALLOC."); + } + } + + /** + Signatures for the WASM-exported C-side functions. Each entry + is an array with 2+ elements: + + [ "c-side name", + "result type" (wasm.xWrap() syntax), + [arg types in xWrap() syntax] + // ^^^ this needn't strictly be an array: it can be subsequent + // elements instead: [x,y,z] is equivalent to x,y,z + ] + + Support for the API-specific data types in the result/argument + type strings gets plugged in at a later phase in the API + initialization process. + */ + const bindingSignatures = { + core: [ + // Please keep these sorted by function name! + ["sqlite3_aggregate_context","void*", "sqlite3_context*", "int"], + /* sqlite3_auto_extension() has a hand-written binding. */ + /* sqlite3_bind_blob() and sqlite3_bind_text() have hand-written + bindings to permit more flexible inputs. */ + ["sqlite3_bind_double","int", "sqlite3_stmt*", "int", "f64"], + ["sqlite3_bind_int","int", "sqlite3_stmt*", "int", "int"], + ["sqlite3_bind_null",undefined, "sqlite3_stmt*", "int"], + ["sqlite3_bind_parameter_count", "int", "sqlite3_stmt*"], + ["sqlite3_bind_parameter_index","int", "sqlite3_stmt*", "string"], + ["sqlite3_bind_parameter_name", "string", "sqlite3_stmt*", "int"], + ["sqlite3_bind_pointer", "int", + "sqlite3_stmt*", "int", "*", "string:static", "*"], + ["sqlite3_busy_handler","int", [ + "sqlite3*", + new wasm.xWrap.FuncPtrAdapter({ + signature: 'i(pi)', + contextKey: (argv,argIndex)=>argv[0/* sqlite3* */] + }), + "*" + ]], + ["sqlite3_busy_timeout","int", "sqlite3*", "int"], + /* sqlite3_cancel_auto_extension() has a hand-written binding. */ + /* sqlite3_close_v2() is implemented by hand to perform some + extra work. */ + ["sqlite3_changes", "int", "sqlite3*"], + ["sqlite3_clear_bindings","int", "sqlite3_stmt*"], + ["sqlite3_collation_needed", "int", "sqlite3*", "*", "*"/*=>v(ppis)*/], + ["sqlite3_column_blob","*", "sqlite3_stmt*", "int"], + ["sqlite3_column_bytes","int", "sqlite3_stmt*", "int"], + ["sqlite3_column_count", "int", "sqlite3_stmt*"], + ["sqlite3_column_decltype", "string", "sqlite3_stmt*", "int"], + ["sqlite3_column_double","f64", "sqlite3_stmt*", "int"], + ["sqlite3_column_int","int", "sqlite3_stmt*", "int"], + ["sqlite3_column_name","string", "sqlite3_stmt*", "int"], + ["sqlite3_column_text","string", "sqlite3_stmt*", "int"], + ["sqlite3_column_type","int", "sqlite3_stmt*", "int"], + ["sqlite3_column_value","sqlite3_value*", "sqlite3_stmt*", "int"], + ["sqlite3_commit_hook", "void*", [ + "sqlite3*", + new wasm.xWrap.FuncPtrAdapter({ + name: 'sqlite3_commit_hook', + signature: 'i(p)', + contextKey: (argv)=>argv[0/* sqlite3* */] + }), + '*' + ]], + ["sqlite3_compileoption_get", "string", "int"], + ["sqlite3_compileoption_used", "int", "string"], + ["sqlite3_complete", "int", "string:flexible"], + ["sqlite3_context_db_handle", "sqlite3*", "sqlite3_context*"], + /* sqlite3_create_collation() and sqlite3_create_collation_v2() + use hand-written bindings to simplify passing of the callback + function. */ + /* sqlite3_create_function(), sqlite3_create_function_v2(), and + sqlite3_create_window_function() use hand-written bindings to + simplify handling of their function-type arguments. */ + ["sqlite3_data_count", "int", "sqlite3_stmt*"], + ["sqlite3_db_filename", "string", "sqlite3*", "string"], + ["sqlite3_db_handle", "sqlite3*", "sqlite3_stmt*"], + ["sqlite3_db_name", "string", "sqlite3*", "int"], + ["sqlite3_db_readonly", "int", "sqlite3*", "string"], + ["sqlite3_db_status", "int", "sqlite3*", "int", "*", "*", "int"], + ["sqlite3_errcode", "int", "sqlite3*"], + ["sqlite3_errmsg", "string", "sqlite3*"], + ["sqlite3_error_offset", "int", "sqlite3*"], + ["sqlite3_errstr", "string", "int"], + ["sqlite3_exec", "int", [ + "sqlite3*", "string:flexible", + new wasm.xWrap.FuncPtrAdapter({ + signature: 'i(pipp)', + bindScope: 'transient', + callProxy: (callback)=>{ + let aNames; + return (pVoid, nCols, pColVals, pColNames)=>{ + try { + const aVals = wasm.cArgvToJs(nCols, pColVals); + if(!aNames) aNames = wasm.cArgvToJs(nCols, pColNames); + return callback(aVals, aNames) | 0; + }catch(e){ + /* If we set the db error state here, the higher-level + exec() call replaces it with its own, so we have no way + of reporting the exception message except the console. We + must not propagate exceptions through the C API. Though + we make an effort to report OOM here, sqlite3_exec() + translates that into SQLITE_ABORT as well. */ + return e.resultCode || capi.SQLITE_ERROR; + } + } + } + }), + "*", "**" + ]], + ["sqlite3_expanded_sql", "string", "sqlite3_stmt*"], + ["sqlite3_extended_errcode", "int", "sqlite3*"], + ["sqlite3_extended_result_codes", "int", "sqlite3*", "int"], + ["sqlite3_file_control", "int", "sqlite3*", "string", "int", "*"], + ["sqlite3_finalize", "int", "sqlite3_stmt*"], + ["sqlite3_free", undefined,"*"], + ["sqlite3_get_autocommit", "int", "sqlite3*"], + ["sqlite3_get_auxdata", "*", "sqlite3_context*", "int"], + ["sqlite3_initialize", undefined], + ["sqlite3_interrupt", undefined, "sqlite3*"], + ["sqlite3_is_interrupted", "int", "sqlite3*"], + ["sqlite3_keyword_count", "int"], + ["sqlite3_keyword_name", "int", ["int", "**", "*"]], + ["sqlite3_keyword_check", "int", ["string", "int"]], + ["sqlite3_libversion", "string"], + ["sqlite3_libversion_number", "int"], + ["sqlite3_limit", "int", ["sqlite3*", "int", "int"]], + ["sqlite3_malloc", "*","int"], + ["sqlite3_open", "int", "string", "*"], + ["sqlite3_open_v2", "int", "string", "*", "int", "string"], + /* sqlite3_prepare_v2() and sqlite3_prepare_v3() are handled + separately due to us requiring two different sets of semantics + for those, depending on how their SQL argument is provided. */ + /* sqlite3_randomness() uses a hand-written wrapper to extend + the range of supported argument types. */ + ["sqlite3_realloc", "*","*","int"], + ["sqlite3_reset", "int", "sqlite3_stmt*"], + /* sqlite3_reset_auto_extension() has a hand-written binding. */ + ["sqlite3_result_blob", undefined, "sqlite3_context*", "*", "int", "*"], + ["sqlite3_result_double", undefined, "sqlite3_context*", "f64"], + ["sqlite3_result_error", undefined, "sqlite3_context*", "string", "int"], + ["sqlite3_result_error_code", undefined, "sqlite3_context*", "int"], + ["sqlite3_result_error_nomem", undefined, "sqlite3_context*"], + ["sqlite3_result_error_toobig", undefined, "sqlite3_context*"], + ["sqlite3_result_int", undefined, "sqlite3_context*", "int"], + ["sqlite3_result_null", undefined, "sqlite3_context*"], + ["sqlite3_result_pointer", undefined, + "sqlite3_context*", "*", "string:static", "*"], + ["sqlite3_result_subtype", undefined, "sqlite3_value*", "int"], + ["sqlite3_result_text", undefined, "sqlite3_context*", "string", "int", "*"], + ["sqlite3_result_zeroblob", undefined, "sqlite3_context*", "int"], + ["sqlite3_rollback_hook", "void*", [ + "sqlite3*", + new wasm.xWrap.FuncPtrAdapter({ + name: 'sqlite3_rollback_hook', + signature: 'v(p)', + contextKey: (argv)=>argv[0/* sqlite3* */] + }), + '*' + ]], + /** + We do not have a way to automatically clean up destructors + which are automatically converted from JS functions via the + final argument to sqlite3_set_auxdata(). Because of that, + automatic function conversion is not supported for this + function. Clients should use wasm.installFunction() to create + such callbacks, then pass that pointer to + sqlite3_set_auxdata(). Relying on automated conversions here + would lead to leaks of JS/WASM proxy functions because + sqlite3_set_auxdata() is frequently called in UDFs. + + The sqlite3.oo1.DB class's onclose handlers can be used for this + purpose. For example: + + const pAuxDtor = wasm.installFunction('v(p)', function(ptr){ + //free ptr + }); + myDb.onclose = { + after: ()=>{ + wasm.uninstallFunction(pAuxDtor); + } + }; + + Then pass pAuxDtor as the final argument to appropriate + sqlite3_set_auxdata() calls. + + Prior to 3.49.0 this binding ostensibly had automatic + function conversion here but a typo prevented it from + working. Rather than fix it, it was removed because testing + the fix brought the huge potential for memory leaks to the + forefront. + */ + ["sqlite3_set_auxdata", undefined, [ + "sqlite3_context*", "int", "*", + true + ? "*" + : new wasm.xWrap.FuncPtrAdapter({ + /* If we can find a way to automate their cleanup, JS functions can + be auto-converted with this. */ + name: 'xDestroyAuxData', + signature: 'v(p)', + contextKey: (argv, argIndex)=>argv[0/* sqlite3_context* */] + }) + ]], + ['sqlite3_set_errmsg', 'int', 'sqlite3*', 'int', 'string'], + ["sqlite3_shutdown", undefined], + ["sqlite3_sourceid", "string"], + ["sqlite3_sql", "string", "sqlite3_stmt*"], + ["sqlite3_status", "int", "int", "*", "*", "int"], + ["sqlite3_step", "int", "sqlite3_stmt*"], + ["sqlite3_stmt_busy", "int", "sqlite3_stmt*"], + ["sqlite3_stmt_readonly", "int", "sqlite3_stmt*"], + ["sqlite3_stmt_status", "int", "sqlite3_stmt*", "int", "int"], + ["sqlite3_strglob", "int", "string","string"], + ["sqlite3_stricmp", "int", "string", "string"], + ["sqlite3_strlike", "int", "string", "string","int"], + ["sqlite3_strnicmp", "int", "string", "string", "int"], + ["sqlite3_table_column_metadata", "int", + "sqlite3*", "string", "string", "string", + "**", "**", "*", "*", "*"], + ["sqlite3_total_changes", "int", "sqlite3*"], + ["sqlite3_trace_v2", "int", [ + "sqlite3*", "int", + new wasm.xWrap.FuncPtrAdapter({ + name: 'sqlite3_trace_v2::callback', + signature: 'i(ippp)', + contextKey: (argv,argIndex)=>argv[0/* sqlite3* */] + }), + "*" + ]], + ["sqlite3_txn_state", "int", ["sqlite3*","string"]], + /* sqlite3_uri_...() have very specific requirements for their + first C-string arguments, so we cannot perform any + string-type conversion on their first argument. */ + ["sqlite3_uri_boolean", "int", "sqlite3_filename", "string", "int"], + ["sqlite3_uri_key", "string", "sqlite3_filename", "int"], + ["sqlite3_uri_parameter", "string", "sqlite3_filename", "string"], + ["sqlite3_user_data","void*", "sqlite3_context*"], + ["sqlite3_value_blob", "*", "sqlite3_value*"], + ["sqlite3_value_bytes","int", "sqlite3_value*"], + ["sqlite3_value_double","f64", "sqlite3_value*"], + ["sqlite3_value_dup", "sqlite3_value*", "sqlite3_value*"], + ["sqlite3_value_free", undefined, "sqlite3_value*"], + ["sqlite3_value_frombind", "int", "sqlite3_value*"], + ["sqlite3_value_int","int", "sqlite3_value*"], + ["sqlite3_value_nochange", "int", "sqlite3_value*"], + ["sqlite3_value_numeric_type", "int", "sqlite3_value*"], + ["sqlite3_value_pointer", "*", "sqlite3_value*", "string:static"], + ["sqlite3_value_subtype", "int", "sqlite3_value*"], + ["sqlite3_value_text", "string", "sqlite3_value*"], + ["sqlite3_value_type", "int", "sqlite3_value*"], + ["sqlite3_vfs_find", "*", "string"], + ["sqlite3_vfs_register", "int", "sqlite3_vfs*", "int"], + ["sqlite3_vfs_unregister", "int", "sqlite3_vfs*"] + + /* This list gets extended with optional pieces below */ + ]/*.core*/, + /** + Functions which require BigInt (int64) support are separated + from the others because we need to conditionally bind them or + apply dummy impls, depending on the capabilities of the + environment. (That said: we never actually build without + BigInt support, and such builds are untested.) + + Not all of these functions directly require int64 but are only + for use with APIs which require int64. For example, the + vtab-related functions. + */ + int64: [ + ["sqlite3_bind_int64","int", ["sqlite3_stmt*", "int", "i64"]], + ["sqlite3_changes64","i64", ["sqlite3*"]], + ["sqlite3_column_int64","i64", ["sqlite3_stmt*", "int"]], + ["sqlite3_deserialize", "int", "sqlite3*", "string", "*", "i64", "i64", "int"] + /* Careful! Short version: de/serialize() are problematic because they + might use a different allocator than the user for managing the + deserialized block. de/serialize() are ONLY safe to use with + sqlite3_malloc(), sqlite3_free(), and its 64-bit variants. Because + of this, the canonical builds of sqlite3.wasm/js guarantee that + sqlite3.wasm.alloc() and friends use those allocators. Custom builds + may not guarantee that, however. */, + ["sqlite3_last_insert_rowid", "i64", ["sqlite3*"]], + ["sqlite3_malloc64", "*","i64"], + ["sqlite3_msize", "i64", "*"], + ["sqlite3_overload_function", "int", ["sqlite3*","string","int"]], + ["sqlite3_realloc64", "*","*", "i64"], + ["sqlite3_result_int64", undefined, "*", "i64"], + ["sqlite3_result_zeroblob64", "int", "*", "i64"], + ["sqlite3_serialize","*", "sqlite3*", "string", "*", "int"], + ["sqlite3_set_last_insert_rowid", undefined, ["sqlite3*", "i64"]], + ["sqlite3_status64", "int", "int", "*", "*", "int"], + ["sqlite3_db_status64", "int", "sqlite3*", "int", "*", "*", "int"], + ["sqlite3_total_changes64", "i64", ["sqlite3*"]], + ["sqlite3_update_hook", "*", [ + "sqlite3*", + new wasm.xWrap.FuncPtrAdapter({ + name: 'sqlite3_update_hook::callback', + signature: "v(pippj)", + contextKey: (argv)=>argv[0/* sqlite3* */], + callProxy: (callback)=>{ + return (p,op,z0,z1,rowid)=>{ + callback(p, op, wasm.cstrToJs(z0), wasm.cstrToJs(z1), rowid); + }; + } + }), + "*" + ]], + ["sqlite3_uri_int64", "i64", ["sqlite3_filename", "string", "i64"]], + ["sqlite3_value_int64","i64", "sqlite3_value*"] + /* This list gets extended with optional pieces below */ + ]/*.int64*/, + /** + Functions which are intended solely for API-internal use by the + WASM components, not client code. These get installed into + sqlite3.util. Some of them get exposed to clients via variants + in sqlite3_js_...(). + + 2024-01-11: these were renamed, with two underscores in the + prefix, to ensure that clients do not accidentally depend on + them. They have always been documented as internal-use-only, + so no clients "should" be depending on the old names. + */ + wasmInternal: [ + ["sqlite3__wasm_db_reset", "int", "sqlite3*"], + ["sqlite3__wasm_db_vfs", "sqlite3_vfs*", "sqlite3*","string"], + [/* DO NOT USE. This is deprecated since 2023-08-11 because it + can trigger assert() in debug builds when used with file + sizes which are not an exact multiple of a valid db page + size. This function is retained only so that + sqlite3_js_vfs_create_file() can continue to work (for a + given value of work), but that function emits a + config.warn() log message directing the reader to + alternatives. */ + "sqlite3__wasm_vfs_create_file", "int", "sqlite3_vfs*","string","*", "int" + ], + ["sqlite3__wasm_posix_create_file", "int", "string","*", "int"], + ["sqlite3__wasm_vfs_unlink", "int", "sqlite3_vfs*","string"], + ["sqlite3__wasm_qfmt_token","string:dealloc", "string","int"] + ]/*.wasmInternal*/ + } /*bindingSignatures*/; + + if( !!wasm.exports.sqlite3_progress_handler ){ + bindingSignatures.core.push( + ["sqlite3_progress_handler", undefined, [ + "sqlite3*", "int", new wasm.xWrap.FuncPtrAdapter({ + name: 'xProgressHandler', + signature: 'i(p)', + bindScope: 'context', + contextKey: (argv,argIndex)=>argv[0/* sqlite3* */] + }), "*" + ]] + ); + } + + if( !!wasm.exports.sqlite3_stmt_explain ){ + bindingSignatures.core.push( + ["sqlite3_stmt_explain", "int", "sqlite3_stmt*", "int"], + ["sqlite3_stmt_isexplain", "int", "sqlite3_stmt*"] + ); + } + + if( !!wasm.exports.sqlite3_set_authorizer ){ + bindingSignatures.core.push( + ["sqlite3_set_authorizer", "int", [ + "sqlite3*", + new wasm.xWrap.FuncPtrAdapter({ + name: "sqlite3_set_authorizer::xAuth", + signature: "i(pi"+"ssss)", + contextKey: (argv, argIndex)=>argv[0/*(sqlite3*)*/], + /** + We use callProxy here to ensure (A) that exceptions + thrown from callback() have well-defined behavior and (B) + that its result is coerced to an integer. + */ + callProxy: (callback)=>{ + return (pV, iCode, s0, s1, s2, s3)=>{ + try{ + s0 = s0 && wasm.cstrToJs(s0); s1 = s1 && wasm.cstrToJs(s1); + s2 = s2 && wasm.cstrToJs(s2); s3 = s3 && wasm.cstrToJs(s3); + return callback(pV, iCode, s0, s1, s2, s3) | 0; + }catch(e){ + return e.resultCode || capi.SQLITE_ERROR; + } + } + } + }), + "*"/*pUserData*/ + ]] + ); + }/* sqlite3_set_authorizer() */ + + if( !!wasm.exports.sqlite3_column_origin_name ){ + bindingSignatures.core.push( + ["sqlite3_column_database_name","string", "sqlite3_stmt*", "int"], + ["sqlite3_column_origin_name","string", "sqlite3_stmt*", "int"], + ["sqlite3_column_table_name","string", "sqlite3_stmt*", "int"] + ); + } + + if(false && wasm.compileOptionUsed('SQLITE_ENABLE_NORMALIZE')){ + /* ^^^ "the problem" is that this is an optional feature and the + build-time function-export list does not currently take + optional features into account. */ + bindingSignatures.core.push(["sqlite3_normalized_sql", "string", "sqlite3_stmt*"]); + } + +//#if enable-see + if( !!wasm.exports.sqlite3_key_v2 ){ + /** + This code is capable of using an SEE build but an SEE WASM + build is generally incompatible with SEE's license conditions. + The project's official stance on WASM builds of SEE is: it is + permitted for use internally within organizations which have + licensed SEE, but not for sites made available to the public or + to unlicensed end users because exposing an SEE build of + sqlite3.wasm effectively provides all clients with a working + copy of SEE. + */ + bindingSignatures.core.push( + ["sqlite3_key", "int", "sqlite3*", "string", "int"], + ["sqlite3_key_v2","int","sqlite3*","string","*","int"], + ["sqlite3_rekey", "int", "sqlite3*", "string", "int"], + ["sqlite3_rekey_v2", "int", "sqlite3*", "string", "*", "int"], + ["sqlite3_activate_see", undefined, "string"] + ); + } +//#endif enable-see + + if( wasm.bigIntEnabled && !!wasm.exports.sqlite3_declare_vtab ){ + bindingSignatures.int64.push( + ["sqlite3_create_module", "int", + ["sqlite3*","string","sqlite3_module*","*"]], + ["sqlite3_create_module_v2", "int", + ["sqlite3*","string","sqlite3_module*","*","*"]], + ["sqlite3_declare_vtab", "int", ["sqlite3*", "string:flexible"]], + ["sqlite3_drop_modules", "int", ["sqlite3*", "**"]], + ["sqlite3_vtab_collation","string","sqlite3_index_info*","int"], + /*["sqlite3_vtab_config" is variadic and requires a hand-written + proxy.] */ + ["sqlite3_vtab_distinct","int", "sqlite3_index_info*"], + ["sqlite3_vtab_in","int", "sqlite3_index_info*", "int", "int"], + ["sqlite3_vtab_in_first", "int", "sqlite3_value*", "**"], + ["sqlite3_vtab_in_next", "int", "sqlite3_value*", "**"], + ["sqlite3_vtab_nochange","int", "sqlite3_context*"], + ["sqlite3_vtab_on_conflict","int", "sqlite3*"], + ["sqlite3_vtab_rhs_value","int", "sqlite3_index_info*", "int", "**"] + ); + }/* virtual table APIs */ + + if(wasm.bigIntEnabled && !!wasm.exports.sqlite3_preupdate_hook){ + bindingSignatures.int64.push( + ["sqlite3_preupdate_blobwrite", "int", "sqlite3*"], + ["sqlite3_preupdate_count", "int", "sqlite3*"], + ["sqlite3_preupdate_depth", "int", "sqlite3*"], + ["sqlite3_preupdate_hook", "*", [ + "sqlite3*", + new wasm.xWrap.FuncPtrAdapter({ + name: 'sqlite3_preupdate_hook', + signature: "v(ppippjj)", + contextKey: (argv)=>argv[0/* sqlite3* */], + callProxy: (callback)=>{ + return (p,db,op,zDb,zTbl,iKey1,iKey2)=>{ + callback(p, db, op, wasm.cstrToJs(zDb), wasm.cstrToJs(zTbl), + iKey1, iKey2); + }; + } + }), + "*" + ]], + ["sqlite3_preupdate_new", "int", ["sqlite3*", "int", "**"]], + ["sqlite3_preupdate_old", "int", ["sqlite3*", "int", "**"]] + ); + } /* preupdate API */ + + // Add session/changeset APIs... + if(wasm.bigIntEnabled + && !!wasm.exports.sqlite3changegroup_add + && !!wasm.exports.sqlite3session_create + && !!wasm.exports.sqlite3_preupdate_hook /* required by the session API */){ + /** + FuncPtrAdapter options for session-related callbacks with the + native signature "i(ps)". This proxy converts the 2nd argument + from a C string to a JS string before passing the arguments on + to the client-provided JS callback. + */ + const __ipsProxy = { + signature: 'i(ps)', + callProxy:(callback)=>{ + return (p,s)=>{ + try{return callback(p, wasm.cstrToJs(s)) | 0} + catch(e){return e.resultCode || capi.SQLITE_ERROR} + } + } + }; + + bindingSignatures.int64.push( + ['sqlite3changegroup_add', 'int', ['sqlite3_changegroup*', 'int', 'void*']], + ['sqlite3changegroup_add_strm', 'int', [ + 'sqlite3_changegroup*', + new wasm.xWrap.FuncPtrAdapter({ + name: 'xInput', signature: 'i(ppp)', bindScope: 'transient' + }), + 'void*' + ]], + ['sqlite3changegroup_delete', undefined, ['sqlite3_changegroup*']], + ['sqlite3changegroup_new', 'int', ['**']], + ['sqlite3changegroup_output', 'int', ['sqlite3_changegroup*', 'int*', '**']], + ['sqlite3changegroup_output_strm', 'int', [ + 'sqlite3_changegroup*', + new wasm.xWrap.FuncPtrAdapter({ + name: 'xOutput', signature: 'i(ppi)', bindScope: 'transient' + }), + 'void*' + ]], + ['sqlite3changeset_apply', 'int', [ + 'sqlite3*', 'int', 'void*', + new wasm.xWrap.FuncPtrAdapter({ + name: 'xFilter', bindScope: 'transient', ...__ipsProxy + }), + new wasm.xWrap.FuncPtrAdapter({ + name: 'xConflict', signature: 'i(pip)', bindScope: 'transient' + }), + 'void*' + ]], + ['sqlite3changeset_apply_strm', 'int', [ + 'sqlite3*', + new wasm.xWrap.FuncPtrAdapter({ + name: 'xInput', signature: 'i(ppp)', bindScope: 'transient' + }), + 'void*', + new wasm.xWrap.FuncPtrAdapter({ + name: 'xFilter', bindScope: 'transient', ...__ipsProxy + }), + new wasm.xWrap.FuncPtrAdapter({ + name: 'xConflict', signature: 'i(pip)', bindScope: 'transient' + }), + 'void*' + ]], + ['sqlite3changeset_apply_v2', 'int', [ + 'sqlite3*', 'int', 'void*', + new wasm.xWrap.FuncPtrAdapter({ + name: 'xFilter', bindScope: 'transient', ...__ipsProxy + }), + new wasm.xWrap.FuncPtrAdapter({ + name: 'xConflict', signature: 'i(pip)', bindScope: 'transient' + }), + 'void*', '**', 'int*', 'int' + + ]], + ['sqlite3changeset_apply_v2_strm', 'int', [ + 'sqlite3*', + new wasm.xWrap.FuncPtrAdapter({ + name: 'xInput', signature: 'i(ppp)', bindScope: 'transient' + }), + 'void*', + new wasm.xWrap.FuncPtrAdapter({ + name: 'xFilter', bindScope: 'transient', ...__ipsProxy + }), + new wasm.xWrap.FuncPtrAdapter({ + name: 'xConflict', signature: 'i(pip)', bindScope: 'transient' + }), + 'void*', '**', 'int*', 'int' + ]], + ['sqlite3changeset_apply_v3', 'int', [ + 'sqlite3*', 'int', 'void*', + new wasm.xWrap.FuncPtrAdapter({ + name: 'xFilter', signature: 'i(pp)', bindScope: 'transient' + }), + new wasm.xWrap.FuncPtrAdapter({ + name: 'xConflict', signature: 'i(pip)', bindScope: 'transient' + }), + 'void*', '**', 'int*', 'int' + + ]], + ['sqlite3changeset_apply_v3_strm', 'int', [ + 'sqlite3*', + new wasm.xWrap.FuncPtrAdapter({ + name: 'xInput', signature: 'i(ppp)', bindScope: 'transient' + }), + 'void*', + new wasm.xWrap.FuncPtrAdapter({ + name: 'xFilter', signature: 'i(pp)', bindScope: 'transient' + }), + new wasm.xWrap.FuncPtrAdapter({ + name: 'xConflict', signature: 'i(pip)', bindScope: 'transient' + }), + 'void*', '**', 'int*', 'int' + ]], + ['sqlite3changeset_concat', 'int', ['int','void*', 'int', 'void*', 'int*', '**']], + ['sqlite3changeset_concat_strm', 'int', [ + new wasm.xWrap.FuncPtrAdapter({ + name: 'xInputA', signature: 'i(ppp)', bindScope: 'transient' + }), + 'void*', + new wasm.xWrap.FuncPtrAdapter({ + name: 'xInputB', signature: 'i(ppp)', bindScope: 'transient' + }), + 'void*', + new wasm.xWrap.FuncPtrAdapter({ + name: 'xOutput', signature: 'i(ppi)', bindScope: 'transient' + }), + 'void*' + ]], + ['sqlite3changeset_conflict', 'int', ['sqlite3_changeset_iter*', 'int', '**']], + ['sqlite3changeset_finalize', 'int', ['sqlite3_changeset_iter*']], + ['sqlite3changeset_fk_conflicts', 'int', ['sqlite3_changeset_iter*', 'int*']], + ['sqlite3changeset_invert', 'int', ['int', 'void*', 'int*', '**']], + ['sqlite3changeset_invert_strm', 'int', [ + new wasm.xWrap.FuncPtrAdapter({ + name: 'xInput', signature: 'i(ppp)', bindScope: 'transient' + }), + 'void*', + new wasm.xWrap.FuncPtrAdapter({ + name: 'xOutput', signature: 'i(ppi)', bindScope: 'transient' + }), + 'void*' + ]], + ['sqlite3changeset_new', 'int', ['sqlite3_changeset_iter*', 'int', '**']], + ['sqlite3changeset_next', 'int', ['sqlite3_changeset_iter*']], + ['sqlite3changeset_old', 'int', ['sqlite3_changeset_iter*', 'int', '**']], + ['sqlite3changeset_op', 'int', [ + 'sqlite3_changeset_iter*', '**', 'int*', 'int*','int*' + ]], + ['sqlite3changeset_pk', 'int', ['sqlite3_changeset_iter*', '**', 'int*']], + ['sqlite3changeset_start', 'int', ['**', 'int', '*']], + ['sqlite3changeset_start_strm', 'int', [ + '**', + new wasm.xWrap.FuncPtrAdapter({ + name: 'xInput', signature: 'i(ppp)', bindScope: 'transient' + }), + 'void*' + ]], + ['sqlite3changeset_start_v2', 'int', ['**', 'int', '*', 'int']], + ['sqlite3changeset_start_v2_strm', 'int', [ + '**', + new wasm.xWrap.FuncPtrAdapter({ + name: 'xInput', signature: 'i(ppp)', bindScope: 'transient' + }), + 'void*', 'int' + ]], + ['sqlite3session_attach', 'int', ['sqlite3_session*', 'string']], + ['sqlite3session_changeset', 'int', ['sqlite3_session*', 'int*', '**']], + ['sqlite3session_changeset_size', 'i64', ['sqlite3_session*']], + ['sqlite3session_changeset_strm', 'int', [ + 'sqlite3_session*', + new wasm.xWrap.FuncPtrAdapter({ + name: 'xOutput', signature: 'i(ppp)', bindScope: 'transient' + }), + 'void*' + ]], + ['sqlite3session_config', 'int', ['int', 'void*']], + ['sqlite3session_create', 'int', ['sqlite3*', 'string', '**']], + //sqlite3session_delete() is bound manually + ['sqlite3session_diff', 'int', ['sqlite3_session*', 'string', 'string', '**']], + ['sqlite3session_enable', 'int', ['sqlite3_session*', 'int']], + ['sqlite3session_indirect', 'int', ['sqlite3_session*', 'int']], + ['sqlite3session_isempty', 'int', ['sqlite3_session*']], + ['sqlite3session_memory_used', 'i64', ['sqlite3_session*']], + ['sqlite3session_object_config', 'int', ['sqlite3_session*', 'int', 'void*']], + ['sqlite3session_patchset', 'int', ['sqlite3_session*', '*', '**']], + ['sqlite3session_patchset_strm', 'int', [ + 'sqlite3_session*', + new wasm.xWrap.FuncPtrAdapter({ + name: 'xOutput', signature: 'i(ppp)', bindScope: 'transient' + }), + 'void*' + ]], + ['sqlite3session_table_filter', undefined, [ + 'sqlite3_session*', + new wasm.xWrap.FuncPtrAdapter({ + name: 'xFilter', ...__ipsProxy, + contextKey: (argv,argIndex)=>argv[0/* (sqlite3_session*) */] + }), + '*' + ]] + ); + }/*session/changeset APIs*/ + + /** + Prepare JS<->C struct bindings for the non-opaque struct types we + need... + */ + sqlite3.StructBinder = globalThis.Jaccwabyt({ + heap: wasm.heap8u, + alloc: wasm.alloc, + dealloc: wasm.dealloc, + bigIntEnabled: wasm.bigIntEnabled, + pointerIR: wasm.ptr.ir, + memberPrefix: /* Never change this: this prefix is baked into any + amount of code and client-facing docs. (Much + later: it probably should have been '$$', but see + the previous sentence.) */ '$' + }); + delete globalThis.Jaccwabyt; + + {// wasm.xWrap() bindings... + + /* Convert Arrays and certain TypedArrays to strings for + 'string:flexible'-type arguments */ + const __xString = wasm.xWrap.argAdapter('string'); + wasm.xWrap.argAdapter( + 'string:flexible', (v)=>__xString(util.flexibleString(v)) + ); + + /** + The 'string:static' argument adapter treats its argument as + either... + + - WASM pointer: assumed to be a long-lived C-string which gets + returned as-is. + + - Anything else: gets coerced to a JS string for use as a map + key. If a matching entry is found (as described next), it is + returned, else wasm.allocCString() is used to create a a new + string, map its pointer to a copy of (''+v) for the remainder + of the application's life, and returns that pointer value for + this call and all future calls which are passed a + string-equivalent argument. + + Use case: sqlite3_bind_pointer(), sqlite3_result_pointer(), and + sqlite3_value_pointer() call for "a static string and + preferably a string literal". This converter is used to ensure + that the string value seen by those functions is long-lived and + behaves as they need it to, at the cost of a one-time leak of + each distinct key. + */ + wasm.xWrap.argAdapter( + 'string:static', + function(v){ + if(wasm.isPtr(v)) return v; + v = ''+v; + let rc = this[v]; + return rc || (this[v] = wasm.allocCString(v)); + }.bind(Object.create(null)) + ); + + /** + Add some descriptive xWrap() aliases for '*' intended to (A) + improve readability/correctness of bindingSignatures and (B) + provide automatic conversion from higher-level representations, + e.g. capi.sqlite3_vfs to `sqlite3_vfs*` via (capi.sqlite3_vfs + instance).pointer. + */ + const __xArgPtr = wasm.xWrap.argAdapter('*'); + const nilType = function(){ + /*a class which no value can ever be an instance of*/ + }; + wasm.xWrap.argAdapter('sqlite3_filename', __xArgPtr) + ('sqlite3_context*', __xArgPtr) + ('sqlite3_value*', __xArgPtr) + ('void*', __xArgPtr) + ('sqlite3_changegroup*', __xArgPtr) + ('sqlite3_changeset_iter*', __xArgPtr) + ('sqlite3_session*', __xArgPtr) + ('sqlite3_stmt*', (v)=> + __xArgPtr((v instanceof (sqlite3?.oo1?.Stmt || nilType)) + ? v.pointer : v)) + ('sqlite3*', (v)=> + __xArgPtr((v instanceof (sqlite3?.oo1?.DB || nilType)) + ? v.pointer : v)) + /** + `sqlite3_vfs*`: + + - v is-a string: use the result of sqlite3_vfs_find(v) but + throw if it returns 0. + - v is-a capi.sqlite3_vfs: use v.pointer. + - Else return the same as the `'*'` argument conversion. + */ + ('sqlite3_vfs*', (v)=>{ + if('string'===typeof v){ + /* A NULL sqlite3_vfs pointer will be treated as the default + VFS in many contexts. We specifically do not want that + behavior here. */ + return capi.sqlite3_vfs_find(v) + || sqlite3.SQLite3Error.toss( + capi.SQLITE_NOTFOUND, + "Unknown sqlite3_vfs name:", v + ); + } + return __xArgPtr((v instanceof (capi.sqlite3_vfs || nilType)) + ? v.pointer : v); + }); + if( wasm.exports.sqlite3_declare_vtab ){ + wasm.xWrap.argAdapter('sqlite3_index_info*', (v)=> + __xArgPtr((v instanceof (capi.sqlite3_index_info || nilType)) + ? v.pointer : v)) + ('sqlite3_module*', (v)=> + __xArgPtr((v instanceof (capi.sqlite3_module || nilType)) + ? v.pointer : v) + ); + } + + /** + Alias `T*` to `*` for return type conversions for common T + types, primarily to improve legibility of their binding + signatures. + */ + const __xRcPtr = wasm.xWrap.resultAdapter('*'); + wasm.xWrap.resultAdapter('sqlite3*', __xRcPtr) + ('sqlite3_context*', __xRcPtr) + ('sqlite3_stmt*', __xRcPtr) + ('sqlite3_value*', __xRcPtr) + ('sqlite3_vfs*', __xRcPtr) + ('void*', __xRcPtr); + + /** + Populate api object with sqlite3_...() by binding the "raw" wasm + exports into type-converting proxies using wasm.xWrap(). + */ + for(const e of bindingSignatures.core){ + capi[e[0]] = wasm.xWrap.apply(null, e); + } + for(const e of bindingSignatures.wasmInternal){ + util[e[0]] = wasm.xWrap.apply(null, e); + } + + /* For C API functions which cannot work properly unless + wasm.bigIntEnabled is true, install a bogus impl which throws + if called when bigIntEnabled is false. The alternative would be + to elide these functions altogether, which seems likely to + cause more confusion, as documented APIs would resolve to the + undefined value in incompatible builds. */ + for(const e of bindingSignatures.int64){ + capi[e[0]] = wasm.bigIntEnabled + ? wasm.xWrap.apply(null, e) + : ()=>toss(e[0]+"() is unavailable due to lack", + "of BigInt support in this build."); + } + + /* We're done with bindingSignatures but it's const, so... */ + delete bindingSignatures.core; + delete bindingSignatures.int64; + delete bindingSignatures.wasmInternal; + + /** + Sets the given db's error state. Accepts: + + - (sqlite3*, int code, string msg) + - (sqlite3*, Error e [,string msg = ''+e]) + + If passed a WasmAllocError, the message is ignored and the + result code is SQLITE_NOMEM. If passed any other Error type, + the result code defaults to SQLITE_ERROR unless the Error + object has a resultCode property, in which case that is used + (e.g. SQLite3Error has that). If passed a non-WasmAllocError + exception, the message string defaults to ''+theError. + + Returns either the final result code, capi.SQLITE_NOMEM if + setting the message string triggers an OOM, or + capi.SQLITE_MISUSE if pDb is NULL or invalid (with the caveat + that behavior in the later case is undefined if pDb is not + "valid enough"). + + Pass (pDb,0,0) to clear the error state. + */ + util.sqlite3__wasm_db_error = function(pDb, resultCode, message){ + if( !pDb ) return capi.SQLITE_MISUSE; + if(resultCode instanceof sqlite3.WasmAllocError){ + resultCode = capi.SQLITE_NOMEM; + message = 0 /*avoid allocating message string*/; + }else if(resultCode instanceof Error){ + message = message || ''+resultCode; + resultCode = (resultCode.resultCode || capi.SQLITE_ERROR); + } + return capi.sqlite3_set_errmsg(pDb, resultCode, message) || resultCode; + }; + }/*xWrap() bindings*/ + + {/* Import C-level constants and structs... */ + const cJson = wasm.xCall('sqlite3__wasm_enum_json'); + if(!cJson){ + toss("Maintenance required: increase sqlite3__wasm_enum_json()'s", + "static buffer size!"); + } + wasm.ctype = JSON.parse(wasm.cstrToJs(cJson)); + // Groups of SQLITE_xyz macros... + const defineGroups = ['access', 'authorizer', + 'blobFinalizers', 'changeset', + 'config', 'dataTypes', + 'dbConfig', 'dbStatus', + 'encodings', 'fcntl', 'flock', 'ioCap', + 'limits', 'openFlags', + 'prepareFlags', 'resultCodes', + 'sqlite3Status', + 'stmtStatus', 'syncFlags', + 'trace', 'txnState', 'udfFlags', + 'version']; + if(wasm.bigIntEnabled){ + defineGroups.push('serialize', 'session', 'vtab'); + } + for(const t of defineGroups){ + for(const e of Object.entries(wasm.ctype[t])){ + // ^^^ [k,v] there triggers a buggy code transformation via + // one of the Emscripten-driven optimizers. + capi[e[0]] = e[1]; + } + } + if(!wasm.functionEntry(capi.SQLITE_WASM_DEALLOC)){ + toss("Internal error: cannot resolve exported function", + "entry SQLITE_WASM_DEALLOC (=="+capi.SQLITE_WASM_DEALLOC+")."); + } + const __rcMap = Object.create(null); + for(const t of ['resultCodes']){ + for(const e of Object.entries(wasm.ctype[t])){ + __rcMap[e[1]] = e[0]; + } + } + /** + For the given integer, returns the SQLITE_xxx result code as a + string, or undefined if no such mapping is found. + */ + capi.sqlite3_js_rc_str = (rc)=>__rcMap[rc]; + + /* Bind all registered C-side structs... */ + const notThese = Object.assign(Object.create(null),{ + // For each struct to NOT register, map its name to true: + WasmTestStruct: true, + /* We unregister the kvvfs VFS from Worker threads below. */ + sqlite3_kvvfs_methods: !util.isUIThread(), + /* sqlite3_index_info and friends require int64: */ + sqlite3_index_info: !wasm.bigIntEnabled, + sqlite3_index_constraint: !wasm.bigIntEnabled, + sqlite3_index_orderby: !wasm.bigIntEnabled, + sqlite3_index_constraint_usage: !wasm.bigIntEnabled + }); + for(const s of wasm.ctype.structs){ + if(!notThese[s.name]){ + capi[s.name] = sqlite3.StructBinder(s); + } + } + if(capi.sqlite3_index_info){ + /* Move these inner structs into sqlite3_index_info. Binding + ** them to WASM requires that we create top-level structs to + ** model them with, but those are no longer needed after we've + ** passed them to StructBinder. */ + for(const k of ['sqlite3_index_constraint', + 'sqlite3_index_orderby', + 'sqlite3_index_constraint_usage']){ + capi.sqlite3_index_info[k] = capi[k]; + delete capi[k]; + } + capi.sqlite3_vtab_config = wasm.xWrap( + 'sqlite3__wasm_vtab_config','int',[ + 'sqlite3*', 'int', 'int'] + ); + }/* end vtab-related setup */ + }/*end C constant and struct imports*/ + + /** + Internal helper to assist in validating call argument counts in + the hand-written sqlite3_xyz() wrappers. We do this only for + consistency with non-special-case wrappings. + */ + const __dbArgcMismatch = (pDb,f,n)=>{ + return util.sqlite3__wasm_db_error(pDb, capi.SQLITE_MISUSE, + f+"() requires "+n+" argument"+ + (1===n?"":'s')+"."); + }; + + /** Code duplication reducer for functions which take an encoding + argument and require SQLITE_UTF8. Sets the db error code to + SQLITE_FORMAT, installs a descriptive error message, + and returns SQLITE_FORMAT. */ + const __errEncoding = (pDb)=>{ + return util.sqlite3__wasm_db_error( + pDb, capi.SQLITE_FORMAT, "SQLITE_UTF8 is the only supported encoding." + ); + }; + + /** + __dbCleanupMap is infrastructure for recording registration of + UDFs and collations so that sqlite3_close_v2() can clean up any + automated JS-to-WASM function conversions installed by those. + */ + const __argPDb = (pDb)=>wasm.xWrap.argAdapter('sqlite3*')(pDb); + const __argStr = (str)=>wasm.isPtr(str) ? wasm.cstrToJs(str) : str; + const __dbCleanupMap = function( + pDb, mode/*0=remove, >0=create if needed, <0=do not create if missing*/ + ){ + pDb = __argPDb(pDb); + let m = this.dbMap.get(pDb); + if(!mode){ + this.dbMap.delete(pDb); + return m; + }else if(!m && mode>0){ + this.dbMap.set(pDb, (m = Object.create(null))); + } + return m; + }.bind(Object.assign(Object.create(null),{ + dbMap: new Map + })); + + __dbCleanupMap.addCollation = function(pDb, name){ + const m = __dbCleanupMap(pDb, 1); + if(!m.collation) m.collation = new Set; + m.collation.add(__argStr(name).toLowerCase()); + }; + + __dbCleanupMap._addUDF = function(pDb, name, arity, map){ + /* Map UDF name to a Set of arity values */ + name = __argStr(name).toLowerCase(); + let u = map.get(name); + if(!u) map.set(name, (u = new Set)); + u.add((arity<0) ? -1 : arity); + }; + + __dbCleanupMap.addFunction = function(pDb, name, arity){ + const m = __dbCleanupMap(pDb, 1); + if(!m.udf) m.udf = new Map; + this._addUDF(pDb, name, arity, m.udf); + }; + + if( wasm.exports.sqlite3_create_window_function ){ + __dbCleanupMap.addWindowFunc = function(pDb, name, arity){ + const m = __dbCleanupMap(pDb, 1); + if(!m.wudf) m.wudf = new Map; + this._addUDF(pDb, name, arity, m.wudf); + }; + } + + /** + Intended to be called _only_ from sqlite3_close_v2(), + passed its non-0 db argument. + + This function frees up certain automatically-installed WASM + function bindings which were installed on behalf of the given db, + as those may otherwise leak. + + Notable caveat: this is only ever run via + sqlite3.capi.sqlite3_close_v2(). If a client, for whatever + reason, uses sqlite3.wasm.exports.sqlite3_close_v2() (the + function directly exported from WASM), this cleanup will not + happen. + + This is not a silver bullet for avoiding automation-related + leaks but represents "an honest effort." + + The issue being addressed here is covered at: + + https://sqlite.org/wasm/doc/trunk/api-c-style.md#convert-func-ptr + */ + __dbCleanupMap.cleanup = function(pDb){ + pDb = __argPDb(pDb); + //wasm.xWrap.FuncPtrAdapter.debugFuncInstall = false; + /** + Installing NULL functions in the C API will remove those + bindings. The FuncPtrAdapter which sits between us and the C + API will also treat that as an opportunity to + wasm.uninstallFunction() any WASM function bindings it has + installed for pDb. + */ + for(const obj of [ + /* pairs of [funcName, itsArityInC] */ + ['sqlite3_busy_handler',3], + ['sqlite3_commit_hook',3], + ['sqlite3_preupdate_hook',3], + ['sqlite3_progress_handler',4], + ['sqlite3_rollback_hook',3], + ['sqlite3_set_authorizer',3], + ['sqlite3_trace_v2', 4], + ['sqlite3_update_hook',3] + /* + We do not yet have a way to clean up automatically-converted + sqlite3_set_auxdata() finalizers. + */ + ]){ + const [name, arity] = obj; + const x = wasm.exports[name]; + if( !x ){ + /* assume it was built without this API */ + continue; + } + const closeArgs = [pDb]; + closeArgs.length = arity + /* Recall that: (A) undefined entries translate to 0 when + passed to WASM and (B) Safari wraps wasm.exports.* in + nullary functions so x.length is 0 there. */; + //wasm.xWrap.debug = true; + try{ capi[name](...closeArgs) } + catch(e){ + /* This "cannot happen" unless something is well and truly sideways. */ + sqlite3.config.warn("close-time call of",name+"(",closeArgs,") threw:",e); + } + //wasm.xWrap.debug = false; + } + const m = __dbCleanupMap(pDb, 0); + if(!m) return; + if(m.collation){ + for(const name of m.collation){ + try{ + capi.sqlite3_create_collation_v2( + pDb, name, capi.SQLITE_UTF8, 0, 0, 0 + ); + }catch(e){ + /*ignored*/ + } + } + delete m.collation; + } + let i; + for(i = 0; i < 2; ++i){ /* Clean up UDFs... */ + const fmap = i ? m.wudf : m.udf; + if(!fmap) continue; + const func = i + ? capi.sqlite3_create_window_function + : capi.sqlite3_create_function_v2; + for(const e of fmap){ + const name = e[0], arities = e[1]; + const fargs = [pDb, name, 0/*arity*/, capi.SQLITE_UTF8, 0, 0, 0, 0, 0]; + if(i) fargs.push(0); + for(const arity of arities){ + try{ fargs[2] = arity; func.apply(null, fargs); } + catch(e){/*ignored*/} + } + arities.clear(); + } + fmap.clear(); + } + delete m.udf; + delete m.wudf; + }/*__dbCleanupMap.cleanup()*/; + + {/* Binding of sqlite3_close_v2() */ + const __sqlite3CloseV2 = wasm.xWrap("sqlite3_close_v2", "int", "sqlite3*"); + capi.sqlite3_close_v2 = function(pDb){ + if(1!==arguments.length) return __dbArgcMismatch(pDb, 'sqlite3_close_v2', 1); + if(pDb){ + try{__dbCleanupMap.cleanup(pDb)} catch(e){/*ignored*/} + } + return __sqlite3CloseV2(pDb); + }; + }/*sqlite3_close_v2()*/ + + if(capi.sqlite3session_create){ + const __sqlite3SessionDelete = wasm.xWrap( + 'sqlite3session_delete', undefined, ['sqlite3_session*'] + ); + capi.sqlite3session_delete = function(pSession){ + if(1!==arguments.length){ + return __dbArgcMismatch(pDb, 'sqlite3session_delete', 1); + /* Yes, we're returning a value from a void function. That seems + like the lesser evil compared to not maintaining arg-count + consistency as we do with other similar bindings. */ + } + else if(pSession){ + //wasm.xWrap.FuncPtrAdapter.debugFuncInstall = true; + capi.sqlite3session_table_filter(pSession, 0, 0); + } + __sqlite3SessionDelete(pSession); + }; + } + + {/* Bindings for sqlite3_create_collation[_v2]() */ + // contextKey() impl for wasm.xWrap.FuncPtrAdapter + const contextKey = (argv,argIndex)=>{ + return 'argv['+argIndex+']:'+argv[0/* sqlite3* */]+ + ':'+wasm.cstrToJs(argv[1/* collation name */]).toLowerCase() + }; + const __sqlite3CreateCollationV2 = wasm.xWrap( + 'sqlite3_create_collation_v2', 'int', [ + 'sqlite3*', 'string', 'int', '*', + new wasm.xWrap.FuncPtrAdapter({ + /* int(*xCompare)(void*,int,const void*,int,const void*) */ + name: 'xCompare', signature: 'i(pipip)', contextKey + }), + new wasm.xWrap.FuncPtrAdapter({ + /* void(*xDestroy(void*) */ + name: 'xDestroy', signature: 'v(p)', contextKey + }) + ] + ); + + /** + Works exactly like C's sqlite3_create_collation_v2() except that: + + 1) It returns capi.SQLITE_FORMAT if the 3rd argument contains + any encoding-related value other than capi.SQLITE_UTF8. No + other encodings are supported. As a special case, if the + bottom 4 bits of that argument are 0, SQLITE_UTF8 is + assumed. + + 2) It accepts JS functions for its function-pointer arguments, + for which it will install WASM-bound proxies. The bindings + are "permanent," in that they will stay in the WASM + environment until it shuts down unless the client calls this + again with the same collation name and a value of 0 or null + for the the function pointer(s). sqlite3_close_v2() will + also clean up such automatically-installed WASM functions. + + For consistency with the C API, it requires the same number of + arguments. It returns capi.SQLITE_MISUSE if passed any other + argument count. + + Returns 0 on success, non-0 on error, in which case the error + state of pDb (of type `sqlite3*` or argument-convertible to it) + may contain more information. + */ + capi.sqlite3_create_collation_v2 = function(pDb,zName,eTextRep,pArg,xCompare,xDestroy){ + if(6!==arguments.length) return __dbArgcMismatch(pDb, 'sqlite3_create_collation_v2', 6); + else if( 0 === (eTextRep & 0xf) ){ + eTextRep |= capi.SQLITE_UTF8; + }else if( capi.SQLITE_UTF8 !== (eTextRep & 0xf) ){ + return __errEncoding(pDb); + } + try{ + const rc = __sqlite3CreateCollationV2(pDb, zName, eTextRep, pArg, xCompare, xDestroy); + if(0===rc && xCompare instanceof Function){ + __dbCleanupMap.addCollation(pDb, zName); + } + return rc; + }catch(e){ + return util.sqlite3__wasm_db_error(pDb, e); + } + }; + + capi.sqlite3_create_collation = (pDb,zName,eTextRep,pArg,xCompare)=>{ + return (5===arguments.length) + ? capi.sqlite3_create_collation_v2(pDb,zName,eTextRep,pArg,xCompare,0) + : __dbArgcMismatch(pDb, 'sqlite3_create_collation', 5); + }; + + }/*sqlite3_create_collation() and friends*/ + + {/* Special-case handling of sqlite3_create_function_v2() + and sqlite3_create_window_function(). */ + /** FuncPtrAdapter for contextKey() for sqlite3_create_function() + and friends. */ + const contextKey = function(argv,argIndex){ + return ( + argv[0/* sqlite3* */] + +':'+(argv[2/*number of UDF args*/] < 0 ? -1 : argv[2]) + +':'+argIndex/*distinct for each xAbc callback type*/ + +':'+wasm.cstrToJs(argv[1]).toLowerCase() + ) + }; + + /** + JS proxies for the various sqlite3_create[_window]_function() + callbacks, structured in a form usable by wasm.xWrap.FuncPtrAdapter. + */ + const __cfProxy = Object.assign(Object.create(null), { + xInverseAndStep: { + signature:'v(pip)', contextKey, + callProxy: (callback)=>{ + return (pCtx, argc, pArgv)=>{ + try{ callback(pCtx, ...capi.sqlite3_values_to_js(argc, pArgv)) } + catch(e){ capi.sqlite3_result_error_js(pCtx, e) } + }; + } + }, + xFinalAndValue: { + signature:'v(p)', contextKey, + callProxy: (callback)=>{ + return (pCtx)=>{ + try{ capi.sqlite3_result_js(pCtx, callback(pCtx)) } + catch(e){ capi.sqlite3_result_error_js(pCtx, e) } + }; + } + }, + xFunc: { + signature:'v(pip)', contextKey, + callProxy: (callback)=>{ + return (pCtx, argc, pArgv)=>{ + try{ + capi.sqlite3_result_js( + pCtx, + callback(pCtx, ...capi.sqlite3_values_to_js(argc, pArgv)) + ); + }catch(e){ + //console.error('xFunc() caught:',e); + capi.sqlite3_result_error_js(pCtx, e); + } + }; + } + }, + xDestroy: { + signature:'v(p)', contextKey, + //Arguable: a well-behaved destructor doesn't require a proxy. + callProxy: (callback)=>{ + return (pVoid)=>{ + try{ callback(pVoid) } + catch(e){ console.error("UDF xDestroy method threw:",e) } + }; + } + } + })/*__cfProxy*/; + + const __sqlite3CreateFunction = wasm.xWrap( + "sqlite3_create_function_v2", "int", [ + "sqlite3*", "string"/*funcName*/, "int"/*nArg*/, + "int"/*eTextRep*/, "*"/*pApp*/, + new wasm.xWrap.FuncPtrAdapter({name: 'xFunc', ...__cfProxy.xFunc}), + new wasm.xWrap.FuncPtrAdapter({name: 'xStep', ...__cfProxy.xInverseAndStep}), + new wasm.xWrap.FuncPtrAdapter({name: 'xFinal', ...__cfProxy.xFinalAndValue}), + new wasm.xWrap.FuncPtrAdapter({name: 'xDestroy', ...__cfProxy.xDestroy}) + ] + ); + + const __sqlite3CreateWindowFunction = + wasm.exports.sqlite3_create_window_function + ? wasm.xWrap( + "sqlite3_create_window_function", "int", [ + "sqlite3*", "string"/*funcName*/, "int"/*nArg*/, + "int"/*eTextRep*/, "*"/*pApp*/, + new wasm.xWrap.FuncPtrAdapter({name: 'xStep', ...__cfProxy.xInverseAndStep}), + new wasm.xWrap.FuncPtrAdapter({name: 'xFinal', ...__cfProxy.xFinalAndValue}), + new wasm.xWrap.FuncPtrAdapter({name: 'xValue', ...__cfProxy.xFinalAndValue}), + new wasm.xWrap.FuncPtrAdapter({name: 'xInverse', ...__cfProxy.xInverseAndStep}), + new wasm.xWrap.FuncPtrAdapter({name: 'xDestroy', ...__cfProxy.xDestroy}) + ] + ) + : undefined; + + /* Documented in the api object's initializer. */ + capi.sqlite3_create_function_v2 = function f( + pDb, funcName, nArg, eTextRep, pApp, + xFunc, //void (*xFunc)(sqlite3_context*,int,sqlite3_value**) + xStep, //void (*xStep)(sqlite3_context*,int,sqlite3_value**) + xFinal, //void (*xFinal)(sqlite3_context*) + xDestroy //void (*xDestroy)(void*) + ){ + if( f.length!==arguments.length ){ + return __dbArgcMismatch(pDb,"sqlite3_create_function_v2",f.length); + }else if( 0 === (eTextRep & 0xf) ){ + eTextRep |= capi.SQLITE_UTF8; + }else if( capi.SQLITE_UTF8 !== (eTextRep & 0xf) ){ + return __errEncoding(pDb); + } + try{ + const rc = __sqlite3CreateFunction(pDb, funcName, nArg, eTextRep, + pApp, xFunc, xStep, xFinal, xDestroy); + if(0===rc && (xFunc instanceof Function + || xStep instanceof Function + || xFinal instanceof Function + || xDestroy instanceof Function)){ + __dbCleanupMap.addFunction(pDb, funcName, nArg); + } + return rc; + }catch(e){ + console.error("sqlite3_create_function_v2() setup threw:",e); + return util.sqlite3__wasm_db_error(pDb, e, "Creation of UDF threw: "+e); + } + }; + + /* Documented in the api object's initializer. */ + capi.sqlite3_create_function = function f( + pDb, funcName, nArg, eTextRep, pApp, + xFunc, xStep, xFinal + ){ + return (f.length===arguments.length) + ? capi.sqlite3_create_function_v2(pDb, funcName, nArg, eTextRep, + pApp, xFunc, xStep, xFinal, 0) + : __dbArgcMismatch(pDb,"sqlite3_create_function",f.length); + }; + + /* Documented in the api object's initializer. */ + if( __sqlite3CreateWindowFunction ){ + capi.sqlite3_create_window_function = function f( + pDb, funcName, nArg, eTextRep, pApp, + xStep, //void (*xStep)(sqlite3_context*,int,sqlite3_value**) + xFinal, //void (*xFinal)(sqlite3_context*) + xValue, //void (*xValue)(sqlite3_context*) + xInverse,//void (*xInverse)(sqlite3_context*,int,sqlite3_value**) + xDestroy //void (*xDestroy)(void*) + ){ + if( f.length!==arguments.length ){ + return __dbArgcMismatch(pDb,"sqlite3_create_window_function",f.length); + }else if( 0 === (eTextRep & 0xf) ){ + eTextRep |= capi.SQLITE_UTF8; + }else if( capi.SQLITE_UTF8 !== (eTextRep & 0xf) ){ + return __errEncoding(pDb); + } + try{ + const rc = __sqlite3CreateWindowFunction(pDb, funcName, nArg, eTextRep, + pApp, xStep, xFinal, xValue, + xInverse, xDestroy); + if(0===rc && (xStep instanceof Function + || xFinal instanceof Function + || xValue instanceof Function + || xInverse instanceof Function + || xDestroy instanceof Function)){ + __dbCleanupMap.addWindowFunc(pDb, funcName, nArg); + } + return rc; + }catch(e){ + console.error("sqlite3_create_window_function() setup threw:",e); + return util.sqlite3__wasm_db_error(pDb, e, "Creation of UDF threw: "+e); + } + }; + }else{ + delete capi.sqlite3_create_window_function; + } + /** + A _deprecated_ alias for capi.sqlite3_result_js() which + predates the addition of that function in the public API. + */ + capi.sqlite3_create_function_v2.udfSetResult = + capi.sqlite3_create_function.udfSetResult = capi.sqlite3_result_js; + if(capi.sqlite3_create_window_function){ + capi.sqlite3_create_window_function.udfSetResult = capi.sqlite3_result_js; + } + + /** + A _deprecated_ alias for capi.sqlite3_values_to_js() which + predates the addition of that function in the public API. + */ + capi.sqlite3_create_function_v2.udfConvertArgs = + capi.sqlite3_create_function.udfConvertArgs = capi.sqlite3_values_to_js; + if(capi.sqlite3_create_window_function){ + capi.sqlite3_create_window_function.udfConvertArgs = capi.sqlite3_values_to_js; + } + + /** + A _deprecated_ alias for capi.sqlite3_result_error_js() which + predates the addition of that function in the public API. + */ + capi.sqlite3_create_function_v2.udfSetError = + capi.sqlite3_create_function.udfSetError = capi.sqlite3_result_error_js; + if(capi.sqlite3_create_window_function){ + capi.sqlite3_create_window_function.udfSetError = capi.sqlite3_result_error_js; + } + + }/*sqlite3_create_function_v2() and sqlite3_create_window_function() proxies*/; + + {/* Special-case handling of sqlite3_prepare_v2() and + sqlite3_prepare_v3() */ + + /** + Helper for string:flexible conversions which requires a + byte-length counterpart argument. Passed a value and its + ostensible length, this function returns [V,N], where V is + either v or a to-string transformed copy of v and N is either n + (if v is a WASM pointer, in which case n might be a BigInt), -1 + (if v is a string or Array), or the byte length of v (if it's a + byte array or ArrayBuffer). + */ + const __flexiString = (v,n)=>{ + if('string'===typeof v){ + n = -1; + }else if(util.isSQLableTypedArray(v)){ + n = v.byteLength; + v = wasm.typedArrayToString( + (v instanceof ArrayBuffer) ? new Uint8Array(v) : v + ); + }else if(Array.isArray(v)){ + v = v.join(""); + n = -1; + } + return [v, n]; + }; + + /** + Scope-local holder of the two impls of sqlite3_prepare_v2/v3(). + */ + const __prepare = { + /** + This binding expects a JS string as its 2nd argument and + null as its final argument. In order to compile multiple + statements from a single string, the "full" impl (see + below) must be used. + */ + basic: wasm.xWrap('sqlite3_prepare_v3', + "int", ["sqlite3*", "string", + "int"/*ignored for this impl!*/, + "int", "**", + "**"/*MUST be 0 or null or undefined!*/]), + /** + Impl which requires that the 2nd argument be a pointer to the + SQL string, instead of being converted to a JS string. This + variant is necessary for cases where we require a non-NULL + value for the final argument (prepare/step of multiple + statements from one input string). For simpler cases, where + only the first statement in the SQL string is required, the + wrapper named sqlite3_prepare_v2() is sufficient and easier + to use because it doesn't require dealing with pointers. + */ + full: wasm.xWrap('sqlite3_prepare_v3', + "int", ["sqlite3*", "*", "int", "int", + "**", "**"]) + }; + + /* Documented in the capi object's initializer. */ + capi.sqlite3_prepare_v3 = function f(pDb, sql, sqlLen, prepFlags, ppStmt, pzTail){ + if(f.length!==arguments.length){ + return __dbArgcMismatch(pDb,"sqlite3_prepare_v3",f.length); + } + const [xSql, xSqlLen] = __flexiString(sql, Number(sqlLen)); + switch(typeof xSql){ + case 'string': return __prepare.basic(pDb, xSql, xSqlLen, prepFlags, ppStmt, null); + case (typeof wasm.ptr.null): + return __prepare.full(pDb, wasm.ptr.coerce(xSql), xSqlLen, prepFlags, + ppStmt, pzTail); + default: + return util.sqlite3__wasm_db_error( + pDb, capi.SQLITE_MISUSE, + "Invalid SQL argument type for sqlite3_prepare_v2/v3(). typeof="+(typeof xSql) + ); + } + }; + + /* Documented in the capi object's initializer. */ + capi.sqlite3_prepare_v2 = function f(pDb, sql, sqlLen, ppStmt, pzTail){ + return (f.length===arguments.length) + ? capi.sqlite3_prepare_v3(pDb, sql, sqlLen, 0, ppStmt, pzTail) + : __dbArgcMismatch(pDb,"sqlite3_prepare_v2",f.length); + }; + + }/*sqlite3_prepare_v2/v3()*/ + + {/*sqlite3_bind_text/blob()*/ + const __bindText = wasm.xWrap("sqlite3_bind_text", "int", [ + "sqlite3_stmt*", "int", "string", "int", "*" + ]); + const __bindBlob = wasm.xWrap("sqlite3_bind_blob", "int", [ + "sqlite3_stmt*", "int", "*", "int", "*" + ]); + + /** Documented in the capi object's initializer. */ + capi.sqlite3_bind_text = function f(pStmt, iCol, text, nText, xDestroy){ + if(f.length!==arguments.length){ + return __dbArgcMismatch(capi.sqlite3_db_handle(pStmt), + "sqlite3_bind_text", f.length); + }else if(wasm.isPtr(text) || null===text){ + return __bindText(pStmt, iCol, text, nText, xDestroy); + }else if(text instanceof ArrayBuffer){ + text = new Uint8Array(text); + }else if(Array.isArray(pMem)){ + text = pMem.join(''); + } + let p, n; + try{ + if(util.isSQLableTypedArray(text)){ + p = wasm.allocFromTypedArray(text); + n = text.byteLength; + }else if('string'===typeof text){ + [p, n] = wasm.allocCString(text); + }else{ + return util.sqlite3__wasm_db_error( + capi.sqlite3_db_handle(pStmt), capi.SQLITE_MISUSE, + "Invalid 3rd argument type for sqlite3_bind_text()." + ); + } + return __bindText(pStmt, iCol, p, n, capi.SQLITE_WASM_DEALLOC); + }catch(e){ + wasm.dealloc(p); + return util.sqlite3__wasm_db_error( + capi.sqlite3_db_handle(pStmt), e + ); + } + }/*sqlite3_bind_text()*/; + + /** Documented in the capi object's initializer. */ + capi.sqlite3_bind_blob = function f(pStmt, iCol, pMem, nMem, xDestroy){ + if(f.length!==arguments.length){ + return __dbArgcMismatch(capi.sqlite3_db_handle(pStmt), + "sqlite3_bind_blob", f.length); + }else if(wasm.isPtr(pMem) || null===pMem){ + return __bindBlob(pStmt, iCol, pMem, nMem, xDestroy); + }else if(pMem instanceof ArrayBuffer){ + pMem = new Uint8Array(pMem); + }else if(Array.isArray(pMem)){ + pMem = pMem.join(''); + } + let p, n; + try{ + if(util.isBindableTypedArray(pMem)){ + p = wasm.allocFromTypedArray(pMem); + n = nMem>=0 ? nMem : pMem.byteLength; + }else if('string'===typeof pMem){ + [p, n] = wasm.allocCString(pMem); + }else{ + return util.sqlite3__wasm_db_error( + capi.sqlite3_db_handle(pStmt), capi.SQLITE_MISUSE, + "Invalid 3rd argument type for sqlite3_bind_blob()." + ); + } + return __bindBlob(pStmt, iCol, p, n, capi.SQLITE_WASM_DEALLOC); + }catch(e){ + wasm.dealloc(p); + return util.sqlite3__wasm_db_error( + capi.sqlite3_db_handle(pStmt), e + ); + } + }/*sqlite3_bind_blob()*/; + + }/*sqlite3_bind_text/blob()*/ + + {/* sqlite3_config() */ + /** + Wraps a small subset of the C API's sqlite3_config() options. + Unsupported options trigger the return of capi.SQLITE_NOTFOUND. + Passing fewer than 2 arguments triggers return of + capi.SQLITE_MISUSE. + */ + capi.sqlite3_config = function(op, ...args){ + if(arguments.length<2) return capi.SQLITE_MISUSE; + switch(op){ + case capi.SQLITE_CONFIG_COVERING_INDEX_SCAN: // 20 /* int */ + case capi.SQLITE_CONFIG_MEMSTATUS:// 9 /* boolean */ + case capi.SQLITE_CONFIG_SMALL_MALLOC: // 27 /* boolean */ + case capi.SQLITE_CONFIG_SORTERREF_SIZE: // 28 /* int nByte */ + case capi.SQLITE_CONFIG_STMTJRNL_SPILL: // 26 /* int nByte */ + case capi.SQLITE_CONFIG_URI:// 17 /* int */ + return wasm.exports.sqlite3__wasm_config_i(op, args[0]); + case capi.SQLITE_CONFIG_LOOKASIDE: // 13 /* int int */ + return wasm.exports.sqlite3__wasm_config_ii(op, args[0], args[1]); + case capi.SQLITE_CONFIG_MEMDB_MAXSIZE: // 29 /* sqlite3_int64 */ + return wasm.exports.sqlite3__wasm_config_j(op, args[0]); + case capi.SQLITE_CONFIG_GETMALLOC: // 5 /* sqlite3_mem_methods* */ + case capi.SQLITE_CONFIG_GETMUTEX: // 11 /* sqlite3_mutex_methods* */ + case capi.SQLITE_CONFIG_GETPCACHE2: // 19 /* sqlite3_pcache_methods2* */ + case capi.SQLITE_CONFIG_GETPCACHE: // 15 /* no-op */ + case capi.SQLITE_CONFIG_HEAP: // 8 /* void*, int nByte, int min */ + case capi.SQLITE_CONFIG_LOG: // 16 /* xFunc, void* */ + case capi.SQLITE_CONFIG_MALLOC:// 4 /* sqlite3_mem_methods* */ + case capi.SQLITE_CONFIG_MMAP_SIZE: // 22 /* sqlite3_int64, sqlite3_int64 */ + case capi.SQLITE_CONFIG_MULTITHREAD: // 2 /* nil */ + case capi.SQLITE_CONFIG_MUTEX: // 10 /* sqlite3_mutex_methods* */ + case capi.SQLITE_CONFIG_PAGECACHE: // 7 /* void*, int sz, int N */ + case capi.SQLITE_CONFIG_PCACHE2: // 18 /* sqlite3_pcache_methods2* */ + case capi.SQLITE_CONFIG_PCACHE: // 14 /* no-op */ + case capi.SQLITE_CONFIG_PCACHE_HDRSZ: // 24 /* int *psz */ + case capi.SQLITE_CONFIG_PMASZ: // 25 /* unsigned int szPma */ + case capi.SQLITE_CONFIG_SERIALIZED: // 3 /* nil */ + case capi.SQLITE_CONFIG_SINGLETHREAD: // 1 /* nil */: + case capi.SQLITE_CONFIG_SQLLOG: // 21 /* xSqllog, void* */ + case capi.SQLITE_CONFIG_WIN32_HEAPSIZE: // 23 /* int nByte */ + default: + /* maintenance note: we specifically do not include + SQLITE_CONFIG_ROWID_IN_VIEW here, on the grounds that + it's only for legacy support and no apps written with + this API require that. */ + return capi.SQLITE_NOTFOUND; + } + }; + }/* sqlite3_config() */ + + {/*auto-extension bindings.*/ + const __autoExtFptr = new Set; + + capi.sqlite3_auto_extension = function(fPtr){ + if( fPtr instanceof Function ){ + fPtr = wasm.installFunction('i(ppp)', fPtr); + }else if( 1!==arguments.length || !wasm.isPtr(fPtr) ){ + return capi.SQLITE_MISUSE; + } + const rc = wasm.exports.sqlite3_auto_extension(fPtr); + if( fPtr!==arguments[0] ){ + if(0===rc) __autoExtFptr.add(fPtr); + else wasm.uninstallFunction(fPtr); + } + return rc; + }; + + capi.sqlite3_cancel_auto_extension = function(fPtr){ + /* We do not do an automatic JS-to-WASM function conversion here + because it would be senseless: the converted pointer would + never possibly match an already-installed one. */; + if(!fPtr || 1!==arguments.length || !wasm.isPtr(fPtr)) return 0; + return wasm.exports.sqlite3_cancel_auto_extension(fPtr); + /* Note that it "cannot happen" that a client passes a pointer which + is in __autoExtFptr because __autoExtFptr only contains automatic + conversions created inside sqlite3_auto_extension() and + never exposed to the client. */ + }; + + capi.sqlite3_reset_auto_extension = function(){ + wasm.exports.sqlite3_reset_auto_extension(); + for(const fp of __autoExtFptr) wasm.uninstallFunction(fp); + __autoExtFptr.clear(); + }; + }/* auto-extension */ + + const pKvvfs = capi.sqlite3_vfs_find("kvvfs"); + if( pKvvfs ){/* kvvfs-specific glue */ + if(util.isUIThread()){ + const kvvfsMethods = new capi.sqlite3_kvvfs_methods( + wasm.exports.sqlite3__wasm_kvvfs_methods() + ); + delete capi.sqlite3_kvvfs_methods; + + const kvvfsMakeKey = wasm.exports.sqlite3__wasm_kvvfsMakeKeyOnPstack, + pstack = wasm.pstack; + + const kvvfsStorage = (zClass)=> + ((115/*=='s'*/===wasm.peek(zClass)) + ? sessionStorage : localStorage); + + /** + Implementations for members of the object referred to by + sqlite3__wasm_kvvfs_methods(). We swap out the native + implementations with these, which use localStorage or + sessionStorage for their backing store. + */ + const kvvfsImpls = { + xRead: (zClass, zKey, zBuf, nBuf)=>{ + const stack = pstack.pointer, + astack = wasm.scopedAllocPush(); + try { + const zXKey = kvvfsMakeKey(zClass,zKey); + if(!zXKey) return -3/*OOM*/; + const jKey = wasm.cstrToJs(zXKey); + const jV = kvvfsStorage(zClass).getItem(jKey); + if(!jV) return -1; + const nV = jV.length /* We are relying 100% on v being + ASCII so that jV.length is equal + to the C-string's byte length. */; + if(nBuf<=0) return nV; + else if(1===nBuf){ + wasm.poke(zBuf, 0); + return nV; + } + const zV = wasm.scopedAllocCString(jV); + if(nBuf > nV + 1) nBuf = nV + 1; + wasm.heap8u().copyWithin( + Number(zBuf), Number(zV), wasm.ptr.addn(zV, nBuf,- 1) + ); + wasm.poke(wasm.ptr.add(zBuf, nBuf, -1), 0); + return nBuf - 1; + }catch(e){ + sqlite3.config.error("kvstorageRead()",e); + return -2; + }finally{ + pstack.restore(stack); + wasm.scopedAllocPop(astack); + } + }, + xWrite: (zClass, zKey, zData)=>{ + const stack = pstack.pointer; + try { + const zXKey = kvvfsMakeKey(zClass,zKey); + if(!zXKey) return 1/*OOM*/; + const jKey = wasm.cstrToJs(zXKey); + kvvfsStorage(zClass).setItem(jKey, wasm.cstrToJs(zData)); + return 0; + }catch(e){ + sqlite3.config.error("kvstorageWrite()",e); + return capi.SQLITE_IOERR; + }finally{ + pstack.restore(stack); + } + }, + xDelete: (zClass, zKey)=>{ + const stack = pstack.pointer; + try { + const zXKey = kvvfsMakeKey(zClass,zKey); + if(!zXKey) return 1/*OOM*/; + kvvfsStorage(zClass).removeItem(wasm.cstrToJs(zXKey)); + return 0; + }catch(e){ + sqlite3.config.error("kvstorageDelete()",e); + return capi.SQLITE_IOERR; + }finally{ + pstack.restore(stack); + } + } + }/*kvvfsImpls*/; + for(const k of Object.keys(kvvfsImpls)){ + kvvfsMethods[kvvfsMethods.memberKey(k)] = + wasm.installFunction( + kvvfsMethods.memberSignature(k), + kvvfsImpls[k] + ); + } + }else{ + /* Worker thread: unregister kvvfs to avoid it being used + for anything other than local/sessionStorage. It "can" + be used that way but it's not really intended to be. */ + capi.sqlite3_vfs_unregister(pKvvfs); + } + }/*pKvvfs*/ + + /* Warn if client-level code makes use of FuncPtrAdapter. */ + wasm.xWrap.FuncPtrAdapter.warnOnUse = true; + + const StructBinder = sqlite3.StructBinder + /* we require a local alias b/c StructBinder is removed from the sqlite3 + object during the final steps of the API cleanup. */; + /** + Installs a StructBinder-bound function pointer member of the + given name and function in the given StructBinder.StructType + target object. + + It creates a WASM proxy for the given function and arranges for + that proxy to be cleaned up when tgt.dispose() is called. Throws + on the slightest hint of error, e.g. tgt is-not-a StructType, + name does not map to a struct-bound member, etc. + + As a special case, if the given function is a pointer, then + `wasm.functionEntry()` is used to validate that it is a known + function. If so, it is used as-is with no extra level of proxying + or cleanup, else an exception is thrown. It is legal to pass a + value of 0, indicating a NULL pointer, with the caveat that 0 + _is_ a legal function pointer in WASM but it will not be accepted + as such _here_. (Justification: the function at address zero must + be one which initially came from the WASM module, not a method we + want to bind to a virtual table or VFS.) + + This function returns a proxy for itself which is bound to tgt + and takes 2 args (name,func). That function returns the same + thing as this one, permitting calls to be chained. + + If called with only 1 arg, it has no side effects but returns a + func with the same signature as described above. + + ACHTUNG: because we cannot generically know how to transform JS + exceptions into result codes, the installed functions do no + automatic catching of exceptions. It is critical, to avoid + undefined behavior in the C layer, that methods mapped via + this function do not throw. The exception, as it were, to that + rule is... + + If applyArgcCheck is true then each JS function (as opposed to + function pointers) gets wrapped in a proxy which asserts that it + is passed the expected number of arguments, throwing if the + argument count does not match expectations. That is only intended + for dev-time usage for sanity checking, and may leave the C + environment in an undefined state. + */ + const installMethod = function callee( + tgt, name, func, applyArgcCheck = callee.installMethodArgcCheck + ){ + if(!(tgt instanceof StructBinder.StructType)){ + toss("Usage error: target object is-not-a StructType."); + }else if(!(func instanceof Function) && !wasm.isPtr(func)){ + toss("Usage error: expecting a Function or WASM pointer to one."); + } + if(1===arguments.length){ + return (n,f)=>callee(tgt, n, f, applyArgcCheck); + } + if(!callee.argcProxy){ + callee.argcProxy = function(tgt, funcName, func,sig){ + return function(...args){ + if(func.length!==arguments.length){ + toss("Argument mismatch for", + tgt.structInfo.name+"::"+funcName + +": Native signature is:",sig); + } + return func.apply(this, args); + } + }; + /* An ondispose() callback for use with + StructBinder-created types. */ + callee.removeFuncList = function(){ + if(this.ondispose.__removeFuncList){ + this.ondispose.__removeFuncList.forEach( + (v,ndx)=>{ + if(wasm.isPtr(v)){ + try{wasm.uninstallFunction(v)} + catch(e){/*ignore*/} + } + /* else it's a descriptive label for the next number in + the list. */ + } + ); + delete this.ondispose.__removeFuncList; + } + }; + }/*static init*/ + const sigN = tgt.memberSignature(name); + if(sigN.length<2){ + toss("Member",name,"does not have a function pointer signature:",sigN); + } + const memKey = tgt.memberKey(name); + const fProxy = (applyArgcCheck && !wasm.isPtr(func)) + /** This middle-man proxy is only for use during development, to + confirm that we always pass the proper number of + arguments. We know that the C-level code will always use the + correct argument count. */ + ? callee.argcProxy(tgt, memKey, func, sigN) + : func; + if(wasm.isPtr(fProxy)){ + if(fProxy && !wasm.functionEntry(fProxy)){ + toss("Pointer",fProxy,"is not a WASM function table entry."); + } + tgt[memKey] = fProxy; + }else{ + const pFunc = wasm.installFunction(fProxy, tgt.memberSignature(name)); + tgt[memKey] = pFunc; + if(!tgt.ondispose || !tgt.ondispose.__removeFuncList){ + tgt.addOnDispose('ondispose.__removeFuncList handler', + callee.removeFuncList); + tgt.ondispose.__removeFuncList = []; + } + tgt.ondispose.__removeFuncList.push(memKey, pFunc); + } + return (n,f)=>callee(tgt, n, f, applyArgcCheck); + }/*installMethod*/; + installMethod.installMethodArgcCheck = false; + + /** + Installs methods into the given StructBinder.StructType-type + instance. Each entry in the given methods object must map to a + known member of the given StructType, else an exception will be + triggered. See installMethod() for more details, including the + semantics of the 3rd argument. + + As an exception to the above, if any two or more methods in the + 2nd argument are the exact same function, installMethod() is + _not_ called for the 2nd and subsequent instances, and instead + those instances get assigned the same method pointer which is + created for the first instance. This optimization is primarily to + accommodate special handling of sqlite3_module::xConnect and + xCreate methods. + + On success, returns its first argument. Throws on error. + */ + const installMethods = function( + structInstance, methods, applyArgcCheck = installMethod.installMethodArgcCheck + ){ + const seen = new Map /* map of */; + for(const k of Object.keys(methods)){ + const m = methods[k]; + const prior = seen.get(m); + if(prior){ + const mkey = structInstance.memberKey(k); + structInstance[mkey] = structInstance[structInstance.memberKey(prior)]; + }else{ + installMethod(structInstance, k, m, applyArgcCheck); + seen.set(m, k); + } + } + return structInstance; + }; + + /** + Equivalent to calling installMethod(this,...arguments) with a + first argument of this object. If called with 1 or 2 arguments + and the first is an object, it's instead equivalent to calling + installMethods(this,...arguments). + */ + StructBinder.StructType.prototype.installMethod = function callee( + name, func, applyArgcCheck = installMethod.installMethodArgcCheck + ){ + return (arguments.length < 3 && name && 'object'===typeof name) + ? installMethods(this, ...arguments) + : installMethod(this, ...arguments); + }; + + /** + Equivalent to calling installMethods() with a first argument + of this object. + */ + StructBinder.StructType.prototype.installMethods = function( + methods, applyArgcCheck = installMethod.installMethodArgcCheck + ){ + return installMethods(this, methods, applyArgcCheck); + }; + +}); diff --git a/ext/wasm/api/sqlite3-api-oo1.c-pp.js b/ext/wasm/api/sqlite3-api-oo1.c-pp.js new file mode 100644 index 0000000000..f7a4e9ebd7 --- /dev/null +++ b/ext/wasm/api/sqlite3-api-oo1.c-pp.js @@ -0,0 +1,2350 @@ +//#if not omit-oo1 +/* + 2022-07-22 + + The author disclaims copyright to this source code. In place of a + legal notice, here is a blessing: + + * May you do good and not evil. + * May you find forgiveness for yourself and forgive others. + * May you share freely, never taking more than you give. + + *********************************************************************** + + This file contains the so-called OO #1 API wrapper for the sqlite3 + WASM build. It requires that sqlite3-api-glue.js has already run + and it installs its deliverable as sqlite3.oo1. +*/ +globalThis.sqlite3ApiBootstrap.initializers.push(function(sqlite3){ + const toss3 = (...args)=>{throw new sqlite3.SQLite3Error(...args)}; + + const capi = sqlite3.capi, wasm = sqlite3.wasm, util = sqlite3.util; + /* What follows is colloquially known as "OO API #1". It is a + binding of the sqlite3 API which is designed to be run within + the same thread (main or worker) as the one in which the + sqlite3 WASM binding was initialized. This wrapper cannot use + the sqlite3 binding if, e.g., the wrapper is in the main thread + and the sqlite3 API is in a worker. */ + + /** + In order to keep clients from manipulating, perhaps + inadvertently, the underlying pointer values of DB and Stmt + instances, we'll gate access to them via the `pointer` property + accessor and store their real values in this map. Keys = DB/Stmt + objects, values = pointer values. This also unifies how those are + accessed, for potential use downstream via custom + wasm.xWrap() function signatures which know how to extract + it. + */ + const __ptrMap = new WeakMap(); + /** + A Set of oo1.DB or oo1.Stmt objects which are proxies for + (sqlite3*) resp. (sqlite3_stmt*) pointers which themselves are + owned elsewhere. Objects in this Set do not own their underlying + handle and that handle must be guaranteed (by the client) to + outlive the proxy. DB.close()/Stmt.finalize() methods will remove + the object from this Set _instead_ of closing/finalizing the + pointer. These proxies are primarily intended as a way to briefly + wrap an (sqlite3[_stmt]*) object as an oo1.DB/Stmt without taking + over ownership, to take advantage of simplifies usage compared to + the C API while not imposing any change of ownership. + + See DB.wrapHandle() and Stmt.wrapHandle(). + */ + const __doesNotOwnHandle = new Set(); + /** + Map of DB instances to objects, each object being a map of Stmt + wasm pointers to Stmt objects. + */ + const __stmtMap = new WeakMap(); + + /** If object opts has _its own_ property named p then that + property's value is returned, else dflt is returned. */ + const getOwnOption = (opts, p, dflt)=>{ + const d = Object.getOwnPropertyDescriptor(opts,p); + return d ? d.value : dflt; + }; + + // Documented in DB.checkRc() + const checkSqlite3Rc = function(dbPtr, sqliteResultCode){ + if(sqliteResultCode){ + if(dbPtr instanceof DB) dbPtr = dbPtr.pointer; + toss3( + sqliteResultCode, + "sqlite3 result code",sqliteResultCode+":", + (dbPtr + ? capi.sqlite3_errmsg(dbPtr) + : capi.sqlite3_errstr(sqliteResultCode)) + ); + } + return arguments[0]; + }; + + /** + sqlite3_trace_v2() callback which gets installed by the DB ctor + if its open-flags contain "t". + */ + const __dbTraceToConsole = + wasm.installFunction('i(ippp)', function(t,c,p,x){ + if(capi.SQLITE_TRACE_STMT===t){ + // x == SQL, p == sqlite3_stmt* + console.log("SQL TRACE #"+(++this.counter)+' via sqlite3@'+c+':', + wasm.cstrToJs(x)); + } + }.bind({counter: 0})); + + /** + A map of sqlite3_vfs pointers to SQL code or a callback function + to run when the DB constructor opens a database with the given + VFS. In the latter case, the call signature is + (theDbObject,sqlite3Namespace) and the callback is expected to + throw on error. + */ + const __vfsPostOpenCallback = Object.create(null); + +//#if enable-see + /** + Converts ArrayBuffer or Uint8Array ba into a string of hex + digits. + */ + const byteArrayToHex = function(ba){ + if( ba instanceof ArrayBuffer ){ + ba = new Uint8Array(ba); + } + const li = []; + const digits = "0123456789abcdef"; + for( const d of ba ){ + li.push( digits[(d & 0xf0) >> 4], digits[d & 0x0f] ); + } + return li.join(''); + }; + + /** + Internal helper to apply an SEE key to a just-opened + database. Requires that db be-a DB object which has just been + opened, opt be the options object processed by its ctor, and opt + must have either the key, hexkey, or textkey properties, either + as a string, an ArrayBuffer, or a Uint8Array. + + This is a no-op in non-SEE builds. It throws on error and returns + without side effects if none of the key/textkey/hexkey options + are set. It throws if more than one is set or if any are set to + values of an invalid type. + + Returns true if it applies the key, else an unspecified falsy + value. Note that applying the key does not imply that the key is + correct, only that it was passed on to the db. + */ + const dbCtorApplySEEKey = function(db,opt){ + if( !capi.sqlite3_key_v2 ) return; + let keytype; + let key; + const check = (opt.key ? 1 : 0) + (opt.hexkey ? 1 : 0) + (opt.textkey ? 1 : 0); + if( !check ) return; + else if( check>1 ){ + toss3(capi.SQLITE_MISUSE, + "Only ONE of (key, hexkey, textkey) may be provided."); + } + if( opt.key ){ + /* It is not legal to bind an argument to PRAGMA key=?, so we + convert it to a hexkey... */ + keytype = 'key'; + key = opt.key; + if('string'===typeof key){ + key = new TextEncoder('utf-8').encode(key); + } + if((key instanceof ArrayBuffer) || (key instanceof Uint8Array)){ + key = byteArrayToHex(key); + keytype = 'hexkey'; + }else{ + toss3(capi.SQLITE_MISUSE, + "Invalid value for the 'key' option. Expecting a string,", + "ArrayBuffer, or Uint8Array."); + return; + } + }else if( opt.textkey ){ + /* For textkey we need it to be in string form, so convert it to + a string if it's a byte array... */ + keytype = 'textkey'; + key = opt.textkey; + if(key instanceof ArrayBuffer){ + key = new Uint8Array(key); + } + if(key instanceof Uint8Array){ + key = new TextDecoder('utf-8').decode(key); + }else if('string'!==typeof key){ + toss3(capi.SQLITE_MISUSE, + "Invalid value for the 'textkey' option. Expecting a string,", + "ArrayBuffer, or Uint8Array."); + } + }else if( opt.hexkey ){ + keytype = 'hexkey'; + key = opt.hexkey; + if((key instanceof ArrayBuffer) || (key instanceof Uint8Array)){ + key = byteArrayToHex(key); + }else if('string'!==typeof key){ + toss3(capi.SQLITE_MISUSE, + "Invalid value for the 'hexkey' option. Expecting a string,", + "ArrayBuffer, or Uint8Array."); + } + /* else assume it's valid hex codes */ + }else{ + return; + } + let stmt; + try{ + stmt = db.prepare("PRAGMA "+keytype+"="+util.sqlite3__wasm_qfmt_token(key, 1)); + stmt.step(); + return true; + }finally{ + if(stmt) stmt.finalize(); + } + }; +//#endif enable-see + + /** + A proxy for DB class constructors. It must be called with the + being-construct DB object as its "this". See the DB constructor + for the argument docs. This is split into a separate function + in order to enable simple creation of special-case DB constructors, + e.g. JsStorageDb and OpfsDb. + + Expects to be passed a configuration object with the following + properties: + + - `.filename`: the db filename. It may be a special name like ":memory:" + or "". + + - `.flags`: as documented in the DB constructor. + + - `.vfs`: as documented in the DB constructor. + + It also accepts those as the first 3 arguments. + */ + const dbCtorHelper = function ctor(...args){ + if(!ctor._name2vfs){ + /** + Map special filenames which we handle here (instead of in C) + to some helpful metadata... + + As of 2022-09-20, the C API supports the names :localStorage: + and :sessionStorage: for kvvfs. However, C code cannot + determine (without embedded JS code, e.g. via Emscripten's + EM_JS()) whether the kvvfs is legal in the current browser + context (namely the main UI thread). In order to help client + code fail early on, instead of it being delayed until they + try to read or write a kvvfs-backed db, we'll check for those + names here and throw if they're not legal in the current + context. + */ + ctor._name2vfs = Object.create(null); + const isWorkerThread = ('function'===typeof importScripts/*===running in worker thread*/) + ? (n)=>toss3("The VFS for",n,"is only available in the main window thread.") + : false; + ctor._name2vfs[':localStorage:'] = { + vfs: 'kvvfs', filename: isWorkerThread || (()=>'local') + }; + ctor._name2vfs[':sessionStorage:'] = { + vfs: 'kvvfs', filename: isWorkerThread || (()=>'session') + }; + } + const opt = ctor.normalizeArgs(...args); + //sqlite3.config.debug("DB ctor",opt); + let pDb; + if( (pDb = opt['sqlite3*']) ){ + /* This property ^^^^^ is very specifically NOT DOCUMENTED and + NOT part of the public API. This is a back door for functions + like DB.wrapDbHandle(). */ + //sqlite3.config.debug("creating proxy db from",opt); + if( !opt['sqlite3*:takeOwnership'] ){ + /* This is object does not own its handle. */ + __doesNotOwnHandle.add(this); + } + this.filename = capi.sqlite3_db_filename(pDb,'main'); + }else{ + let fn = opt.filename, vfsName = opt.vfs, flagsStr = opt.flags; + if( ('string'!==typeof fn && !wasm.isPtr(fn)) + || 'string'!==typeof flagsStr + || (vfsName && ('string'!==typeof vfsName && !wasm.isPtr(vfsName))) ){ + sqlite3.config.error("Invalid DB ctor args",opt,arguments); + toss3("Invalid arguments for DB constructor:", arguments, "opts:", opt); + } + let fnJs = wasm.isPtr(fn) ? wasm.cstrToJs(fn) : fn; + const vfsCheck = ctor._name2vfs[fnJs]; + if(vfsCheck){ + vfsName = vfsCheck.vfs; + fn = fnJs = vfsCheck.filename(fnJs); + } + let oflags = 0; + if( flagsStr.indexOf('c')>=0 ){ + oflags |= capi.SQLITE_OPEN_CREATE | capi.SQLITE_OPEN_READWRITE; + } + if( flagsStr.indexOf('w')>=0 ) oflags |= capi.SQLITE_OPEN_READWRITE; + if( 0===oflags ) oflags |= capi.SQLITE_OPEN_READONLY; + oflags |= capi.SQLITE_OPEN_EXRESCODE; + const stack = wasm.pstack.pointer; + try { + const pPtr = wasm.pstack.allocPtr() /* output (sqlite3**) arg */; + let rc = capi.sqlite3_open_v2(fn, pPtr, oflags, vfsName || wasm.ptr.null); + pDb = wasm.peekPtr(pPtr); + checkSqlite3Rc(pDb, rc); + capi.sqlite3_extended_result_codes(pDb, 1); + if(flagsStr.indexOf('t')>=0){ + capi.sqlite3_trace_v2(pDb, capi.SQLITE_TRACE_STMT, + __dbTraceToConsole, pDb); + } + }catch( e ){ + if( pDb ) capi.sqlite3_close_v2(pDb); + throw e; + }finally{ + wasm.pstack.restore(stack); + } + this.filename = fnJs; + } + __ptrMap.set(this, pDb); + __stmtMap.set(this, Object.create(null)); + if( !opt['sqlite3*'] ){ + try{ +//#if enable-see + dbCtorApplySEEKey(this,opt); +//#endif + // Check for per-VFS post-open SQL/callback... + const pVfs = capi.sqlite3_js_db_vfs(pDb) + || toss3("Internal error: cannot get VFS for new db handle."); + const postInitSql = __vfsPostOpenCallback[pVfs]; + if(postInitSql){ + /** + Reminder: if this db is encrypted and the client did _not_ pass + in the key, any init code will fail, causing the ctor to throw. + We don't actually know whether the db is encrypted, so we cannot + sensibly apply any heuristics which skip the init code only for + encrypted databases for which no key has yet been supplied. + */ + if(postInitSql instanceof Function){ + postInitSql(this, sqlite3); + }else{ + checkSqlite3Rc( + pDb, capi.sqlite3_exec(pDb, postInitSql, 0, 0, 0) + ); + } + } + }catch(e){ + this.close(); + throw e; + } + } + }; + + /** + Sets a callback which should be called after a db is opened with + the given sqlite3_vfs pointer. The 2nd argument must be a + function, which gets called with + (theOo1DbObject,sqlite3Namespace) at the end of the DB() + constructor. The function must throw on error, in which case the + db is closed and the exception is propagated. This function is + intended only for use by DB subclasses or sqlite3_vfs + implementations. + + Prior to 2024-07-22, it was legal to pass SQL code as the second + argument, but that can interfere with a client's ability to run + pragmas which must be run before anything else, namely (pragma + locking_mode=exclusive) for use with WAL mode. That capability + had only ever been used as an internal detail of the two OPFS + VFSes, and they no longer use it that way. + */ + dbCtorHelper.setVfsPostOpenCallback = function(pVfs, callback){ + if( !(callback instanceof Function)){ + toss3("dbCtorHelper.setVfsPostOpenCallback() should not be used with "+ + "a non-function argument.",arguments); + } + __vfsPostOpenCallback[pVfs] = callback; + }; + + /** + A helper for DB constructors. It accepts either a single + config-style object or up to 3 arguments (filename, dbOpenFlags, + dbVfsName). It returns a new object containing: + + { filename: ..., flags: ..., vfs: ... } + + If passed an object, any additional properties it has are copied + as-is into the new object. + */ + dbCtorHelper.normalizeArgs = function(filename=':memory:',flags = 'c',vfs = null){ + const arg = {}; + if(1===arguments.length && arguments[0] && 'object'===typeof arguments[0]){ + Object.assign(arg, arguments[0]); + if(undefined===arg.flags) arg.flags = 'c'; + if(undefined===arg.vfs) arg.vfs = null; + if(undefined===arg.filename) arg.filename = ':memory:'; + }else{ + arg.filename = filename; + arg.flags = flags; + arg.vfs = vfs; + } + return arg; + }; + /** + The DB class provides a high-level OO wrapper around an sqlite3 + db handle. + + The given db filename must be resolvable using whatever + filesystem layer (virtual or otherwise) is set up for the default + sqlite3 VFS. + + Note that the special sqlite3 db names ":memory:" and "" + (temporary db) have their normal special meanings here and need + not resolve to real filenames, but "" uses an on-storage + temporary database and requires that the VFS support that. + + The second argument specifies the open/create mode for the + database. It must be string containing a sequence of letters (in + any order, but case sensitive) specifying the mode: + + - "c": create if it does not exist, else fail if it does not + exist. Implies the "w" flag. + + - "w": write. Implies "r": a db cannot be write-only. + + - "r": read-only if neither "w" nor "c" are provided, else it + is ignored. + + - "t": enable tracing of SQL executed on this database handle, + sending it to `console.log()`. To disable it later, call + `sqlite3.capi.sqlite3_trace_v2(thisDb.pointer, 0, 0, 0)`. + + If "w" is not provided, the db is implicitly read-only, noting + that "rc" is meaningless + + Any other letters are currently ignored. The default is + "c". These modes are ignored for the special ":memory:" and "" + names and _may_ be ignored altogether for certain VFSes. + + The final argument is analogous to the final argument of + sqlite3_open_v2(): the name of an sqlite3 VFS. Pass a falsy value, + or none at all, to use the default. If passed a value, it must + be the string name of a VFS. + + The constructor optionally (and preferably) takes its arguments + in the form of a single configuration object with the following + properties: + + - `filename`: database file name + - `flags`: open-mode flags + - `vfs`: the VFS fname + +//#if enable-see + SEE-capable builds optionally support ONE of the following + additional options: + + - `key`, `hexkey`, or `textkey`: encryption key as a string, + ArrayBuffer, or Uint8Array. These flags function as documented + for the SEE pragmas of the same names. Using a byte array for + `hexkey` is equivalent to the same series of hex codes in + string form, so `'666f6f'` is equivalent to + `Uint8Array([0x66,0x6f,0x6f])`. A `textkey` byte array is + assumed to be UTF-8. A `key` string is transformed into a UTF-8 + byte array, and a `key` byte array is transformed into a + `hexkey` with the same bytes. + + In non-SEE builds, these options are ignored. In SEE builds, + `PRAGMA key/textkey/hexkey=X` is executed immediately after + opening the db. If more than one of the options is provided, + or any option has an invalid argument type, an exception is + thrown. + + Note that some DB subclasses may run post-initialization SQL + code, e.g. to set a busy-handler timeout or tweak the page cache + size. Such code is run _after_ the SEE key is applied. If no key + is supplied and the database is encrypted, execution of the + post-initialization SQL will fail, causing the constructor to + throw. +//#endif enable-see + + The `filename` and `vfs` arguments may be either JS strings or + C-strings allocated via WASM. `flags` is required to be a JS + string (because it's specific to this API, which is specific + to JS). + + For purposes of passing a DB instance to C-style sqlite3 + functions, the DB object's read-only `pointer` property holds its + `sqlite3*` pointer value. That property can also be used to check + whether this DB instance is still open: it will evaluate to + `undefined` after the DB object's close() method is called. + + In the main window thread, the filenames `":localStorage:"` and + `":sessionStorage:"` are special: they cause the db to use either + localStorage or sessionStorage for storing the database using + the kvvfs. If one of these names are used, they trump + any vfs name set in the arguments. + */ + const DB = function(...args){ + dbCtorHelper.apply(this, args); + }; + DB.dbCtorHelper = dbCtorHelper; + + /** + Internal-use enum for mapping JS types to DB-bindable types. + These do not (and need not) line up with the SQLITE_type + values. All values in this enum must be truthy and (mostly) + distinct but they need not be numbers. + */ + const BindTypes = { + null: 1, + number: 2, + string: 3, + boolean: 4, + blob: 5 + }; + if(wasm.bigIntEnabled){ + BindTypes.bigint = BindTypes.number; + } + + /** + This class wraps sqlite3_stmt. Calling this constructor + directly will trigger an exception. Use DB.prepare() to create + new instances. + + For purposes of passing a Stmt instance to C-style sqlite3 + functions, its read-only `pointer` property holds its `sqlite3_stmt*` + pointer value. + + Other non-function properties include: + + - `db`: the DB object which created the statement. + + - `columnCount`: the number of result columns in the query, or 0 + for queries which cannot return results. This property is a + read-only proxy for sqlite3_column_count() and its use in loops + should be avoided because of the call overhead associated with + that. The `columnCount` is not cached when the Stmt is created + because a schema change made between this statement's preparation + and when it is stepped may invalidate it. + + - `parameterCount`: the number of bindable parameters in the + query. Like `columnCount`, this property is ready-only and is a + proxy for a C API call. + + As a general rule, most methods of this class will throw if + called on an instance which has been finalized. For brevity's + sake, the method docs do not all repeat this warning. + */ + const Stmt = function(/*oo1db, stmtPtr, BindTypes [,takeOwnership=true] */){ + if(BindTypes!==arguments[2]){ + toss3(capi.SQLITE_MISUSE, "Do not call the Stmt constructor directly. Use DB.prepare()."); + } + this.db = arguments[0]; + __ptrMap.set(this, arguments[1]); + if( arguments.length>3 && !arguments[3] ){ + __doesNotOwnHandle.add(this); + } + }; + + /** Throws if the given DB has been closed, else it is returned. */ + const affirmDbOpen = function(db){ + if(!db.pointer) toss3("DB has been closed."); + return db; + }; + + /** Throws if ndx is not an integer or if it is out of range + for stmt.columnCount, else returns stmt. + + Reminder: this will also fail after the statement is finalized + but the resulting error will be about an out-of-bounds column + index rather than a statement-is-finalized error. + */ + const affirmColIndex = function(stmt,ndx){ + if((ndx !== (ndx|0)) || ndx<0 || ndx>=stmt.columnCount){ + toss3("Column index",ndx,"is out of range."); + } + return stmt; + }; + + /** + Expects to be passed the `arguments` object from DB.exec(). Does + the argument processing/validation, throws on error, and returns + a new object on success: + + { sql: the SQL, opt: optionsObj, cbArg: function} + + The opt object is a normalized copy of any passed to this + function. The sql will be converted to a string if it is provided + in one of the supported non-string formats. + + cbArg is only set if the opt.callback or opt.resultRows are set, + in which case it's a function which expects to be passed the + current Stmt and returns the callback argument of the type + indicated by the input arguments. + */ + const parseExecArgs = function(db, args){ + const out = Object.create(null); + out.opt = Object.create(null); + switch(args.length){ + case 1: + if('string'===typeof args[0] || util.isSQLableTypedArray(args[0])){ + out.sql = args[0]; + }else if(Array.isArray(args[0])){ + out.sql = args[0]; + }else if(args[0] && 'object'===typeof args[0]){ + out.opt = args[0]; + out.sql = out.opt.sql; + } + break; + case 2: + out.sql = args[0]; + out.opt = args[1]; + break; + default: toss3("Invalid argument count for exec()."); + }; + out.sql = util.flexibleString(out.sql); + if('string'!==typeof out.sql){ + toss3("Missing SQL argument or unsupported SQL value type."); + } + const opt = out.opt; + switch(opt.returnValue){ + case 'resultRows': + if(!opt.resultRows) opt.resultRows = []; + out.returnVal = ()=>opt.resultRows; + break; + case 'saveSql': + if(!opt.saveSql) opt.saveSql = []; + out.returnVal = ()=>opt.saveSql; + break; + case undefined: + case 'this': + out.returnVal = ()=>db; + break; + default: + toss3("Invalid returnValue value:",opt.returnValue); + } + if(!opt.callback && !opt.returnValue && undefined!==opt.rowMode){ + if(!opt.resultRows) opt.resultRows = []; + out.returnVal = ()=>opt.resultRows; + } + if(opt.callback || opt.resultRows){ + switch((undefined===opt.rowMode) ? 'array' : opt.rowMode) { + case 'object': + out.cbArg = (stmt,cache)=>{ + if( !cache.columnNames ) cache.columnNames = stmt.getColumnNames([]); + /* https://sqlite.org/forum/forumpost/3632183d2470617d: + conversion of rows to objects (key/val pairs) is + somewhat expensive for large data sets because of the + native-to-JS conversion of the column names. If we + instead cache the names and build objects from that + list of strings, it can run twice as fast. The + difference is not noticeable for small data sets but + becomes human-perceivable when enough rows are + involved. */ + const row = stmt.get([]); + const rv = Object.create(null); + for( const i in cache.columnNames ) rv[cache.columnNames[i]] = row[i]; + return rv; + }; + break; + case 'array': out.cbArg = (stmt)=>stmt.get([]); break; + case 'stmt': + if(Array.isArray(opt.resultRows)){ + toss3("exec(): invalid rowMode for a resultRows array: must", + "be one of 'array', 'object',", + "a result column number, or column name reference."); + } + out.cbArg = (stmt)=>stmt; + break; + default: + if(util.isInt32(opt.rowMode)){ + out.cbArg = (stmt)=>stmt.get(opt.rowMode); + break; + }else if('string'===typeof opt.rowMode + && opt.rowMode.length>1 + && '$'===opt.rowMode[0]){ + /* "$X": fetch column named "X" (case-sensitive!). Prior + to 2022-12-14 ":X" and "@X" were also permitted, but + having so many options is unnecessary and likely to + cause confusion. */ + const $colName = opt.rowMode.substr(1); + out.cbArg = (stmt)=>{ + const rc = stmt.get(Object.create(null))[$colName]; + return (undefined===rc) + ? toss3(capi.SQLITE_NOTFOUND, + "exec(): unknown result column:",$colName) + : rc; + }; + break; + } + toss3("Invalid rowMode:",opt.rowMode); + } + } + return out; + }; + + /** + Internal impl of the DB.selectValue(), selectArray(), and + selectObject() methods. + */ + const __selectFirstRow = (db, sql, bind, ...getArgs)=>{ + const stmt = db.prepare(sql); + try { + const rc = stmt.bind(bind).step() ? stmt.get(...getArgs) : undefined; + stmt.reset(/*for INSERT...RETURNING locking case*/); + return rc; + }finally{ + stmt.finalize(); + } + }; + + /** + Internal impl of the DB.selectArrays() and selectObjects() + methods. + */ + const __selectAll = + (db, sql, bind, rowMode)=>db.exec({ + sql, bind, rowMode, returnValue: 'resultRows' + }); + + /** + Expects to be given a DB instance or an `sqlite3*` pointer (may + be null) and an sqlite3 API result code. If the result code is + not falsy, this function throws an SQLite3Error with an error + message from sqlite3_errmsg(), using db (or, if db is-a DB, + db.pointer) as the db handle, or sqlite3_errstr() if db is + falsy. Note that if it's passed a non-error code like SQLITE_ROW + or SQLITE_DONE, it will still throw but the error string might be + "Not an error." The various non-0 non-error codes need to be + checked for in client code where they are expected. + + The thrown exception's `resultCode` property will be the value of + the second argument to this function. + + If it does not throw, it returns its first argument. + */ + DB.checkRc = (db,resultCode)=>checkSqlite3Rc(db,resultCode); + + DB.prototype = { + /** Returns true if this db handle is open, else false. */ + isOpen: function(){ + return !!this.pointer; + }, + /** Throws if this given DB has been closed, else returns `this`. */ + affirmOpen: function(){ + return affirmDbOpen(this); + }, + /** + Finalizes all open statements and closes this database + connection (with one exception noted below). This is a no-op if + the db has already been closed. After calling close(), + `this.pointer` will resolve to `undefined`, and that can be + used to check whether the db instance is still opened. + + If this.onclose.before is a function then it is called before + any close-related cleanup. + + If this.onclose.after is a function then it is called after the + db is closed but before auxiliary state like this.filename is + cleared. + + Both onclose handlers are passed this object, with the onclose + object as their "this," noting that the db will have been + closed when onclose.after is called. If this db is not opened + when close() is called, neither of the handlers are called. Any + exceptions the handlers throw are ignored because "destructors + must not throw". + + Garbage collection of a db handle, if it happens at all, will + never trigger close(), so onclose handlers are not a reliable + way to implement close-time cleanup or maintenance of a db. + + If this instance was created using DB.wrapHandle() and does not + own this.pointer then it does not close the db handle but it + does perform all other work, such as calling onclose callbacks + and disassociating this object from this.pointer. + */ + close: function(){ + const pDb = this.pointer; + if(pDb){ + if(this.onclose && (this.onclose.before instanceof Function)){ + try{this.onclose.before(this)} + catch(e){/*ignore*/} + } + Object.keys(__stmtMap.get(this)).forEach((k,s)=>{ + if(s && s.pointer){ + try{s.finalize()} + catch(e){/*ignore*/} + } + }); + __ptrMap.delete(this); + __stmtMap.delete(this); + if( !__doesNotOwnHandle.delete(this) ){ + capi.sqlite3_close_v2(pDb); + } + if(this.onclose && (this.onclose.after instanceof Function)){ + try{this.onclose.after(this)} + catch(e){/*ignore*/} + } + delete this.filename; + } + }, + /** + Returns the number of changes, as per sqlite3_changes() + (if the first argument is false) or sqlite3_total_changes() + (if it's true). If the 2nd argument is true, it uses + sqlite3_changes64() or sqlite3_total_changes64(), which + will trigger an exception if this build does not have + BigInt support enabled. + */ + changes: function(total=false,sixtyFour=false){ + const p = affirmDbOpen(this).pointer; + if(total){ + return sixtyFour + ? capi.sqlite3_total_changes64(p) + : capi.sqlite3_total_changes(p); + }else{ + return sixtyFour + ? capi.sqlite3_changes64(p) + : capi.sqlite3_changes(p); + } + }, + /** + Similar to the this.filename but returns the + sqlite3_db_filename() value for the given database name, + defaulting to "main". The argument may be either a JS string + or a pointer to a WASM-allocated C-string. + */ + dbFilename: function(dbName='main'){ + return capi.sqlite3_db_filename(affirmDbOpen(this).pointer, dbName); + }, + /** + Returns the name of the given 0-based db number, as documented + for sqlite3_db_name(). + */ + dbName: function(dbNumber=0){ + return capi.sqlite3_db_name(affirmDbOpen(this).pointer, dbNumber); + }, + /** + Returns the name of the sqlite3_vfs used by the given database + of this connection (defaulting to 'main'). The argument may be + either a JS string or a WASM C-string. Returns undefined if the + given db name is invalid. Throws if this object has been + close()d. + */ + dbVfsName: function(dbName=0){ + let rc; + const pVfs = capi.sqlite3_js_db_vfs( + affirmDbOpen(this).pointer, dbName + ); + if(pVfs){ + const v = new capi.sqlite3_vfs(pVfs); + try{ rc = wasm.cstrToJs(v.$zName) } + finally { v.dispose() } + } + return rc; + }, + /** + Compiles the given SQL and returns a prepared Stmt. This is + the only way to create new Stmt objects. Throws on error. + + The given SQL must be a string, a Uint8Array holding SQL, a + WASM pointer to memory holding the NUL-terminated SQL string, + or an array of strings. In the latter case, the array is + concatenated together, with no separators, to form the SQL + string (arrays are often a convenient way to formulate long + statements). If the SQL contains no statements, an + SQLite3Error is thrown. + + Design note: the C API permits empty SQL, reporting it as a 0 + result code and a NULL stmt pointer. Supporting that case here + would cause extra work for all clients: any use of the Stmt API + on such a statement will necessarily throw, so clients would be + required to check `stmt.pointer` after calling `prepare()` in + order to determine whether the Stmt instance is empty or not. + Long-time practice (with other sqlite3 script bindings) + suggests that the empty-prepare case is sufficiently rare that + supporting it here would simply hurt overall usability. + */ + prepare: function(sql){ + affirmDbOpen(this); + const stack = wasm.pstack.pointer; + let ppStmt, pStmt; + try{ + ppStmt = wasm.pstack.alloc(8)/* output (sqlite3_stmt**) arg */; + DB.checkRc(this, capi.sqlite3_prepare_v2(this.pointer, sql, -1, ppStmt, null)); + pStmt = wasm.peekPtr(ppStmt); + } + finally { + wasm.pstack.restore(stack); + } + if(!pStmt) toss3("Cannot prepare empty SQL."); + const stmt = new Stmt(this, pStmt, BindTypes); + __stmtMap.get(this)[pStmt] = stmt; + return stmt; + }, + /** + Executes one or more SQL statements in the form of a single + string. Its arguments must be either (sql,optionsObject) or + (optionsObject). In the latter case, optionsObject.sql must + contain the SQL to execute. By default it returns this object + but that can be changed via the `returnValue` option as + described below. Throws on error. + + If no SQL is provided, or a non-string is provided, an + exception is triggered. Empty SQL, on the other hand, is + simply a no-op. + + The optional options object may contain any of the following + properties: + + - `sql` = the SQL to run (unless it's provided as the first + argument). This must be of type string, Uint8Array, or an array + of strings. In the latter case they're concatenated together + as-is, _with no separator_ between elements, before evaluation. + The array form is often simpler for long hand-written queries. + + - `bind` = a single value valid as an argument for + Stmt.bind(). This is _only_ applied to the _first_ non-empty + statement in the SQL which has any bindable parameters. (Empty + statements are skipped entirely.) + + - `saveSql` = an optional array. If set, the SQL of each + executed statement is appended to this array before the + statement is executed (but after it is prepared - we don't have + the string until after that). Empty SQL statements are elided + but can have odd effects in the output. e.g. SQL of: `"select + 1; -- empty\n; select 2"` will result in an array containing + `["select 1;", "--empty \n; select 2"]`. That's simply how + sqlite3 records the SQL for the 2nd statement. + + ================================================================== + The following options apply _only_ to the _first_ statement + which has a non-zero result column count, regardless of whether + the statement actually produces any result rows. + ================================================================== + + - `columnNames`: if this is an array, the column names of the + result set are stored in this array before the callback (if + any) is triggered (regardless of whether the query produces any + result rows). If no statement has result columns, this value is + unchanged. Achtung: an SQL result may have multiple columns + with identical names. + + - `callback` = a function which gets called for each row of the + result set, but only if that statement has any result rows. The + callback's "this" is the options object, noting that this + function synthesizes one if the caller does not pass one to + exec(). The second argument passed to the callback is always + the current Stmt object, as it's needed if the caller wants to + fetch the column names or some such (noting that they could + also be fetched via `this.columnNames`, if the client provides + the `columnNames` option). If the callback returns a literal + `false` (as opposed to any other falsy value, e.g. an implicit + `undefined` return), any ongoing statement-`step()` iteration + stops without an error. The return value of the callback is + otherwise ignored. + + ACHTUNG: The callback MUST NOT modify the Stmt object. Calling + any of the Stmt.get() variants, Stmt.getColumnName(), or + similar, is legal, but calling step() or finalize() is + not. Member methods which are illegal in this context will + trigger an exception, but clients must also refrain from using + any lower-level (C-style) APIs which might modify the + statement. + + The first argument passed to the callback defaults to an array of + values from the current result row but may be changed with ... + + - `rowMode` = specifies the type of he callback's first argument. + It may be any of... + + A) A string describing what type of argument should be passed + as the first argument to the callback: + + A.1) `'array'` (the default) causes the results of + `stmt.get([])` to be passed to the `callback` and/or appended + to `resultRows`. + + A.2) `'object'` causes the results of + `stmt.get(Object.create(null))` to be passed to the + `callback` and/or appended to `resultRows`. Achtung: an SQL + result may have multiple columns with identical names. In + that case, the right-most column will be the one set in this + object! + + A.3) `'stmt'` causes the current Stmt to be passed to the + callback, but this mode will trigger an exception if + `resultRows` is an array because appending the transient + statement to the array would be downright unhelpful. + + B) An integer, indicating a zero-based column in the result + row. Only that one single value will be passed on. + + C) A string with a minimum length of 2 and leading character of + '$' will fetch the row as an object, extract that one field, + and pass that field's value to the callback. Note that these + keys are case-sensitive so must match the case used in the + SQL. e.g. `"select a A from t"` with a `rowMode` of `'$A'` + would work but `'$a'` would not. A reference to a column not in + the result set will trigger an exception on the first row (as + the check is not performed until rows are fetched). Note also + that `$` is a legal identifier character in JS so need not be + quoted. + + Any other `rowMode` value triggers an exception. + + - `resultRows`: if this is an array, it functions similarly to + the `callback` option: each row of the result set (if any), + with the exception that the `rowMode` 'stmt' is not legal. It + is legal to use both `resultRows` and `callback`, but + `resultRows` is likely much simpler to use for small data sets + and can be used over a WebWorker-style message interface. + exec() throws if `resultRows` is set and `rowMode` is 'stmt'. + + - `returnValue`: is a string specifying what this function + should return: + + A) The default value is (usually) `"this"`, meaning that the + DB object itself should be returned. The exception is if + the caller passes neither of `callback` nor `returnValue` + but does pass an explicit `rowMode` then the default + `returnValue` is `"resultRows"`, described below. + + B) `"resultRows"` means to return the value of the + `resultRows` option. If `resultRows` is not set, this + function behaves as if it were set to an empty array. + + C) `"saveSql"` means to return the value of the + `saveSql` option. If `saveSql` is not set, this + function behaves as if it were set to an empty array. + + Potential TODOs: + + - `bind`: permit an array of arrays/objects to bind. The first + sub-array would act on the first statement which has bindable + parameters (as it does now). The 2nd would act on the next such + statement, etc. + + - `callback` and `resultRows`: permit an array entries with + semantics similar to those described for `bind` above. + + */ + exec: function(/*(sql [,obj]) || (obj)*/){ + affirmDbOpen(this); + const arg = parseExecArgs(this, arguments); + if(!arg.sql){ + return toss3("exec() requires an SQL string."); + } + const opt = arg.opt; + const callback = opt.callback; + const resultRows = + Array.isArray(opt.resultRows) ? opt.resultRows : undefined; + let stmt; + let bind = opt.bind; + let evalFirstResult = !!( + arg.cbArg || opt.columnNames || resultRows + ) /* true to step through the first result-returning statement */; + const stack = wasm.scopedAllocPush(); + const saveSql = Array.isArray(opt.saveSql) ? opt.saveSql : undefined; + try{ + const isTA = util.isSQLableTypedArray(arg.sql) + /* Optimization: if the SQL is a TypedArray we can save some string + conversion costs. */; + /* Allocate the two output pointers (ppStmt, pzTail) and heap + space for the SQL (pSql). When prepare_v2() returns, pzTail + will point to somewhere in pSql. */ + let sqlByteLen = isTA ? arg.sql.byteLength : wasm.jstrlen(arg.sql); + const ppStmt = wasm.scopedAlloc( + /* output (sqlite3_stmt**) arg and pzTail */ + (2 * wasm.ptr.size) + (sqlByteLen + 1/* SQL + NUL */) + ); + const pzTail = wasm.ptr.add(ppStmt, wasm.ptr.size) /* final arg to sqlite3_prepare_v2() */; + let pSql = wasm.ptr.add(pzTail, wasm.ptr.size); + const pSqlEnd = wasm.ptr.add(pSql, sqlByteLen); + if(isTA) wasm.heap8().set(arg.sql, pSql); + else wasm.jstrcpy(arg.sql, wasm.heap8(), pSql, sqlByteLen, false); + wasm.poke(wasm.ptr.add(pSql, sqlByteLen), 0/*NUL terminator*/); + while(pSql && wasm.peek(pSql, 'i8') + /* Maintenance reminder:^^^ _must_ be 'i8' or else we + will very likely cause an endless loop. What that's + doing is checking for a terminating NUL byte. If we + use i32 or similar then we read 4 bytes, read stuff + around the NUL terminator, and get stuck in and + endless loop at the end of the SQL, endlessly + re-preparing an empty statement. */ ){ + wasm.pokePtr([ppStmt, pzTail], 0); + DB.checkRc(this, capi.sqlite3_prepare_v3( + this.pointer, pSql, sqlByteLen, 0, ppStmt, pzTail + )); + const pStmt = wasm.peekPtr(ppStmt); + pSql = wasm.peekPtr(pzTail); + sqlByteLen = Number(wasm.ptr.add(pSqlEnd,-pSql)); + if(!pStmt) continue; + //sqlite3.config.debug("exec() pSql =",capi.sqlite3_sql(pStmt)); + if(saveSql) saveSql.push(capi.sqlite3_sql(pStmt).trim()); + stmt = new Stmt(this, pStmt, BindTypes); + if(bind && stmt.parameterCount){ + stmt.bind(bind); + bind = null; + } + if(evalFirstResult && stmt.columnCount){ + /* Only forward SELECT-style results for the FIRST query + in the SQL which potentially has them. */ + let gotColNames = Array.isArray( + opt.columnNames + /* As reported in + https://sqlite.org/forum/forumpost/7774b773937cbe0a + we need to delay fetching of the column names until + after the first step() (if we step() at all) because + a schema change between the prepare() and step(), via + another connection, may invalidate the column count + and names. */) ? 0 : 1; + evalFirstResult = false; + if(arg.cbArg || resultRows){ + const cbArgCache = Object.create(null) + /* 2nd arg for arg.cbArg, used by (at least) row-to-object + converter */; + for( ; stmt.step(); __execLock.delete(stmt) ){ + if(0===gotColNames++){ + stmt.getColumnNames(cbArgCache.columnNames = (opt.columnNames || [])); + } + __execLock.add(stmt); + const row = arg.cbArg(stmt,cbArgCache); + if(resultRows) resultRows.push(row); + if(callback && false === callback.call(opt, row, stmt)){ + break; + } + } + __execLock.delete(stmt); + } + if(0===gotColNames){ + /* opt.columnNames was provided but we visited no result rows */ + stmt.getColumnNames(opt.columnNames); + } + }else{ + stmt.step(); + } + stmt.reset( + /* In order to trigger an exception in the + INSERT...RETURNING locking scenario: + https://sqlite.org/forum/forumpost/36f7a2e7494897df + */).finalize(); + stmt = null; + }/*prepare() loop*/ + }/*catch(e){ + sqlite3.config.warn("DB.exec() is propagating exception",opt,e); + throw e; + }*/finally{ + if(stmt){ + __execLock.delete(stmt); + stmt.finalize(); + } + wasm.scopedAllocPop(stack); + } + return arg.returnVal(); + }/*exec()*/, + + /** + Creates a new UDF (User-Defined Function) which is accessible + via SQL code. This function may be called in any of the + following forms: + + - (name, function) + - (name, function, optionsObject) + - (name, optionsObject) + - (optionsObject) + + In the final two cases, the function must be defined as the + `callback` property of the options object (optionally called + `xFunc` to align with the C API documentation). In the final + case, the function's name must be the 'name' property. + + The first two call forms can only be used for creating scalar + functions. Creating an aggregate or window function requires + the options-object form (see below for details). + + UDFs can be removed as documented for + sqlite3_create_function_v2() and + sqlite3_create_window_function(), but doing so will "leak" the + JS-created WASM binding of those functions (meaning that their + entries in the WASM indirect function table still + exist). Eliminating that potential leak is a pending TODO. + + On success, returns this object. Throws on error. + + When called from SQL arguments to the UDF, and its result, + will be converted between JS and SQL with as much fidelity as + is feasible, triggering an exception if a type conversion + cannot be determined. The docs for sqlite3_create_function_v2() + describe the conversions in more detail. + + The values set in the options object differ for scalar and + aggregate functions: + + - Scalar: set the `xFunc` function-type property to the UDF + function. + + - Aggregate: set the `xStep` and `xFinal` function-type + properties to the "step" and "final" callbacks for the + aggregate. Do not set the `xFunc` property. + + - Window: set the `xStep`, `xFinal`, `xValue`, and `xInverse` + function-type properties. Do not set the `xFunc` property. + + The options object may optionally have an `xDestroy` + function-type property, as per sqlite3_create_function_v2(). + Its argument will be the WASM-pointer-type value of the `pApp` + property, and this function will throw if `pApp` is defined but + is not null, undefined, or a numeric (WASM pointer) + value. i.e. `pApp`, if set, must be value suitable for use as a + WASM pointer argument, noting that `null` or `undefined` will + translate to 0 for that purpose. + + The options object may contain flags to modify how + the function is defined: + + - `arity`: the number of arguments which SQL calls to this + function expect or require. The default value is `xFunc.length` + or `xStep.length` (i.e. the number of declared parameters it + has) **MINUS 1** (see below for why). As a special case, if the + `length` is 0, its arity is also 0 instead of -1. A negative + arity value means that the function is variadic and may accept + any number of arguments, up to sqlite3's compile-time + limits. sqlite3 will enforce the argument count if is zero or + greater. The callback always receives a pointer to an + `sqlite3_context` object as its first argument. Any arguments + after that are from SQL code. The leading context argument does + _not_ count towards the function's arity. See the docs for + sqlite3.capi.sqlite3_create_function_v2() for why that argument + is needed in the interface. + + The following options-object properties correspond to flags + documented at: + + https://sqlite.org/c3ref/create_function.html + + - `deterministic` = sqlite3.capi.SQLITE_DETERMINISTIC + - `directOnly` = sqlite3.capi.SQLITE_DIRECTONLY + - `innocuous` = sqlite3.capi.SQLITE_INNOCUOUS + + Sidebar: the ability to add new WASM-accessible functions to + the runtime requires that the WASM build is compiled with the + equivalent functionality as that provided by Emscripten's + `-sALLOW_TABLE_GROWTH` flag. + */ + createFunction: function f(name, xFunc, opt){ + const isFunc = (f)=>(f instanceof Function); + switch(arguments.length){ + case 1: /* (optionsObject) */ + opt = name; + name = opt.name; + xFunc = opt.xFunc || 0; + break; + case 2: /* (name, callback|optionsObject) */ + if(!isFunc(xFunc)){ + opt = xFunc; + xFunc = opt.xFunc || 0; + } + break; + case 3: /* name, xFunc, opt */ + break; + default: break; + } + if(!opt) opt = {}; + if('string' !== typeof name){ + toss3("Invalid arguments: missing function name."); + } + let xStep = opt.xStep || 0; + let xFinal = opt.xFinal || 0; + const xValue = opt.xValue || 0; + const xInverse = opt.xInverse || 0; + let isWindow = undefined; + if(isFunc(xFunc)){ + isWindow = false; + if(isFunc(xStep) || isFunc(xFinal)){ + toss3("Ambiguous arguments: scalar or aggregate?"); + } + xStep = xFinal = null; + }else if(isFunc(xStep)){ + if(!isFunc(xFinal)){ + toss3("Missing xFinal() callback for aggregate or window UDF."); + } + xFunc = null; + }else if(isFunc(xFinal)){ + toss3("Missing xStep() callback for aggregate or window UDF."); + }else{ + toss3("Missing function-type properties."); + } + if(false === isWindow){ + if(isFunc(xValue) || isFunc(xInverse)){ + toss3("xValue and xInverse are not permitted for non-window UDFs."); + } + }else if(isFunc(xValue)){ + if(!isFunc(xInverse)){ + toss3("xInverse must be provided if xValue is."); + } + isWindow = true; + }else if(isFunc(xInverse)){ + toss3("xValue must be provided if xInverse is."); + } + const pApp = opt.pApp; + if( undefined!==pApp + && null!==pApp + && !wasm.isPtr(pApp) ){ + toss3("Invalid value for pApp property. Must be a legal WASM pointer value."); + } + const xDestroy = opt.xDestroy || 0; + if(xDestroy && !isFunc(xDestroy)){ + toss3("xDestroy property must be a function."); + } + let fFlags = 0 /*flags for sqlite3_create_function_v2()*/; + if(getOwnOption(opt, 'deterministic')) fFlags |= capi.SQLITE_DETERMINISTIC; + if(getOwnOption(opt, 'directOnly')) fFlags |= capi.SQLITE_DIRECTONLY; + if(getOwnOption(opt, 'innocuous')) fFlags |= capi.SQLITE_INNOCUOUS; + name = name.toLowerCase(); + const xArity = xFunc || xStep; + const arity = getOwnOption(opt, 'arity'); + const arityArg = ('number'===typeof arity + ? arity + : (xArity.length ? xArity.length-1/*for pCtx arg*/ : 0)); + let rc; + if( isWindow ){ + rc = capi.sqlite3_create_window_function( + this.pointer, name, arityArg, + capi.SQLITE_UTF8 | fFlags, pApp || 0, + xStep, xFinal, xValue, xInverse, xDestroy); + }else{ + rc = capi.sqlite3_create_function_v2( + this.pointer, name, arityArg, + capi.SQLITE_UTF8 | fFlags, pApp || 0, + xFunc, xStep, xFinal, xDestroy); + } + DB.checkRc(this, rc); + return this; + }/*createFunction()*/, + /** + Prepares the given SQL, step()s it one time, and returns + the value of the first result column. If it has no results, + undefined is returned. + + If passed a second argument, it is treated like an argument + to Stmt.bind(), so may be any type supported by that + function. Passing the undefined value is the same as passing + no value, which is useful when... + + If passed a 3rd argument, it is expected to be one of the + SQLITE_{typename} constants. Passing the undefined value is + the same as not passing a value. + + Throws on error (e.g. malformed SQL). + */ + selectValue: function(sql,bind,asType){ + return __selectFirstRow(this, sql, bind, 0, asType); + }, + + /** + Runs the given query and returns an array of the values from + the first result column of each row of the result set. The 2nd + argument is an optional value for use in a single-argument call + to Stmt.bind(). The 3rd argument may be any value suitable for + use as the 2nd argument to Stmt.get(). If a 3rd argument is + desired but no bind data are needed, pass `undefined` for the 2nd + argument. + + If there are no result rows, an empty array is returned. + */ + selectValues: function(sql,bind,asType){ + const stmt = this.prepare(sql), rc = []; + try { + stmt.bind(bind); + while(stmt.step()) rc.push(stmt.get(0,asType)); + stmt.reset(/*for INSERT...RETURNING locking case*/); + }finally{ + stmt.finalize(); + } + return rc; + }, + + /** + Prepares the given SQL, step()s it one time, and returns an + array containing the values of the first result row. If it has + no results, `undefined` is returned. + + If passed a second argument other than `undefined`, it is + treated like an argument to Stmt.bind(), so may be any type + supported by that function. + + Throws on error (e.g. malformed SQL). + */ + selectArray: function(sql,bind){ + return __selectFirstRow(this, sql, bind, []); + }, + + /** + Prepares the given SQL, step()s it one time, and returns an + object containing the key/value pairs of the first result + row. If it has no results, `undefined` is returned. + + Note that the order of returned object's keys is not guaranteed + to be the same as the order of the fields in the query string. + + If passed a second argument other than `undefined`, it is + treated like an argument to Stmt.bind(), so may be any type + supported by that function. + + Throws on error (e.g. malformed SQL). + */ + selectObject: function(sql,bind){ + return __selectFirstRow(this, sql, bind, {}); + }, + + /** + Runs the given SQL and returns an array of all results, with + each row represented as an array, as per the 'array' `rowMode` + option to `exec()`. An empty result set resolves + to an empty array. The second argument, if any, is treated as + the 'bind' option to a call to exec(). + */ + selectArrays: function(sql,bind){ + return __selectAll(this, sql, bind, 'array'); + }, + + /** + Works identically to selectArrays() except that each value + in the returned array is an object, as per the 'object' `rowMode` + option to `exec()`. + */ + selectObjects: function(sql,bind){ + return __selectAll(this, sql, bind, 'object'); + }, + + /** + Returns the number of currently-opened Stmt handles for this db + handle, or 0 if this DB instance is closed. Note that only + handles prepared via this.prepare() are counted, and not + handles prepared using capi.sqlite3_prepare_v3() (or + equivalent). + */ + openStatementCount: function(){ + return this.pointer ? Object.keys(__stmtMap.get(this)).length : 0; + }, + + /** + Starts a transaction, calls the given callback, and then either + rolls back or commits the transaction, depending on whether the + callback throws. The callback is passed this db object as its + only argument. On success, returns the result of the + callback. Throws on error. + + Note that transactions may not be nested, so this will throw if + it is called recursively. For nested transactions, use the + savepoint() method or manually manage SAVEPOINTs using exec(). + + If called with 2 arguments, the first must be a keyword which + is legal immediately after a BEGIN statement, e.g. one of + "DEFERRED", "IMMEDIATE", or "EXCLUSIVE". Though the exact list + of supported keywords is not hard-coded here, in order to be + future-compatible, if the argument does not look like a single + keyword then an exception is triggered with a description of + the problem. + */ + transaction: function(/* [beginQualifier,] */callback){ + let opener = 'BEGIN'; + if(arguments.length>1){ + if(/[^a-zA-Z]/.test(arguments[0])){ + toss3(capi.SQLITE_MISUSE, "Invalid argument for BEGIN qualifier."); + } + opener += ' '+arguments[0]; + callback = arguments[1]; + } + affirmDbOpen(this).exec(opener); + try { + const rc = callback(this); + this.exec("COMMIT"); + return rc; + }catch(e){ + this.exec("ROLLBACK"); + throw e; + } + }, + + /** + This works similarly to transaction() but uses sqlite3's SAVEPOINT + feature. This function starts a savepoint (with an unspecified name) + and calls the given callback function, passing it this db object. + If the callback returns, the savepoint is released (committed). If + the callback throws, the savepoint is rolled back. If it does not + throw, it returns the result of the callback. + */ + savepoint: function(callback){ + affirmDbOpen(this).exec("SAVEPOINT oo1"); + try { + const rc = callback(this); + this.exec("RELEASE oo1"); + return rc; + }catch(e){ + this.exec("ROLLBACK to SAVEPOINT oo1; RELEASE SAVEPOINT oo1"); + throw e; + } + }, + + /** + A convenience form of DB.checkRc(this,resultCode). If it does + not throw, it returns this object. + */ + checkRc: function(resultCode){ + return checkSqlite3Rc(this, resultCode); + }, + }/*DB.prototype*/; + + /** + Returns a new oo1.DB instance which wraps the given (sqlite3*) + WASM pointer, optionally with or without taking over ownership of + that pointer. + + The first argument must be either a non-NULL (sqlite3*) WASM + pointer. + + The second argument, defaulting to false, specifies ownership of + the first argument. If it is truthy, the returned object will + pass that pointer to sqlite3_close() when its close() method is + called, otherwise it will not. + + Throws if pDb is not a non-0 WASM pointer. + + The caller MUST GUARANTEE that the passed-in handle will outlive + the returned object, i.e. that it will not be closed. If it is closed, + this object will hold a stale pointer and results are undefined. + + Aside from its lifetime, the proxy is to be treated as any other + DB instance, including the requirement of calling close() on + it. close() will free up internal resources owned by the proxy + and disassociate the proxy from that handle but will not + actually close the proxied db handle unless this function is + passed a thruthy second argument. + + To stress: + + - DO NOT call sqlite3_close() (or similar) on the being-proxied + pointer while a proxy is active. + + - ALWAYS eventually call close() on the returned object. If the + proxy does not own the underlying handle then its MUST be + closed BEFORE the being-proxied handle is closed. + + Design notes: + + - wrapHandle() "could" accept a DB object instance as its first + argument and proxy thatDb.pointer but there is currently no use + case where doing so would be useful, so it does not allow + that. That restriction may be lifted in a future version. + */ + DB.wrapHandle = function(pDb, takeOwnership=false){ + if( !pDb || !wasm.isPtr(pDb) ){ + throw new sqlite3.SQLite3Error(capi.SQLITE_MISUSE, + "Argument must be a WASM sqlite3 pointer"); + } + return new DB({ + /* This ctor call style is very specifically internal-use-only. + It is not documented and may change at any time. */ + "sqlite3*": pDb, + "sqlite3*:takeOwnership": !!takeOwnership + }); + }; + + /** Throws if the given Stmt has been finalized, else stmt is + returned. */ + const affirmStmtOpen = function(stmt){ + if(!stmt.pointer) toss3("Stmt has been closed."); + return stmt; + }; + + /** Returns an opaque truthy value from the BindTypes + enum if v's type is a valid bindable type, else + returns a falsy value. As a special case, a value of + undefined is treated as a bind type of null. */ + const isSupportedBindType = function(v){ + let t = BindTypes[(null===v||undefined===v) ? 'null' : typeof v]; + switch(t){ + case BindTypes.boolean: + case BindTypes.null: + case BindTypes.number: + case BindTypes.string: + return t; + case BindTypes.bigint: + return wasm.bigIntEnabled ? t : undefined; + default: + return util.isBindableTypedArray(v) ? BindTypes.blob : undefined; + } + }; + + /** + If isSupportedBindType(v) returns a truthy value, this + function returns that value, else it throws. + */ + const affirmSupportedBindType = function(v){ + //sqlite3.config.log('affirmSupportedBindType',v); + return isSupportedBindType(v) || toss3("Unsupported bind() argument type:",typeof v); + }; + + /** + If key is a number and within range of stmt's bound parameter + count, key is returned. + + If key is not a number then it must be a JS string (not a WASM + string) and it is checked against named parameters. If a match is + found, its index is returned. + + Else it throws. + */ + const affirmParamIndex = function(stmt,key){ + const n = ('number'===typeof key) + ? key : capi.sqlite3_bind_parameter_index(stmt.pointer, key); + if( 0===n || !util.isInt32(n) ) toss3("Invalid bind() parameter name: "+key); + else if( n<1 || n>stmt.parameterCount ) toss3("Bind index",key,"is out of range."); + return n; + }; + + /** + Each Stmt object which is "locked" by DB.exec() gets an entry + here to note that "lock". + + The reason this is in place is because exec({callback:...})'s + callback gets access to the Stmt objects created internally by + exec() but it must not use certain Stmt APIs. + */ + const __execLock = new Set(); + /** + This is a Stmt.get() counterpart of __execLock. Each time + Stmt.step() returns true, the statement is added to this set, + indicating that Stmt.get() is legal. Stmt APIs which invalidate + that status remove the Stmt object from this set, which will + cause Stmt.get() to throw with a descriptive error message + instead of a more generic "API misuse" if we were to allow that + call to reach the C API. + */ + const __stmtMayGet = new Set(); + + /** + Stmt APIs which are prohibited on locked objects must call + affirmNotLockedByExec() before doing any work. + + If __execLock.has(stmt) is truthy, this throws an exception + complaining that the 2nd argument (an operation name, + e.g. "bind()") is not legal while the statement is "locked". + Locking happens before an exec()-like callback is passed a + statement, to ensure that the callback does not mutate or + finalize the statement. If it does not throw, it returns stmt. + */ + const affirmNotLockedByExec = function(stmt,currentOpName){ + if(__execLock.has(stmt)){ + toss3("Operation is illegal when statement is locked:",currentOpName); + } + return stmt; + }; + + /** + Binds a single bound parameter value on the given stmt at the + given index (numeric or named) using the given bindType (see + the BindTypes enum) and value. Throws on error. Returns stmt on + success. + */ + const bindOne = function f(stmt,ndx,bindType,val){ + affirmNotLockedByExec(affirmStmtOpen(stmt), 'bind()'); + if(!f._){ + f._tooBigInt = (v)=>toss3( + "BigInt value is too big to store without precision loss:", v + ); + f._ = { + string: function(stmt, ndx, val, asBlob){ + const [pStr, n] = wasm.allocCString(val, true); + const f = asBlob ? capi.sqlite3_bind_blob : capi.sqlite3_bind_text; + return f(stmt.pointer, ndx, pStr, n, capi.SQLITE_WASM_DEALLOC); + } + }; + }/* static init */ + affirmSupportedBindType(val); + ndx = affirmParamIndex(stmt,ndx); + let rc = 0; + switch((null===val || undefined===val) ? BindTypes.null : bindType){ + case BindTypes.null: + rc = capi.sqlite3_bind_null(stmt.pointer, ndx); + break; + case BindTypes.string: + rc = f._.string(stmt, ndx, val, false); + break; + case BindTypes.number: { + let m; + if(util.isInt32(val)) m = capi.sqlite3_bind_int; + else if('bigint'===typeof val){ + if(!util.bigIntFits64(val)){ + f._tooBigInt(val); + }else if(wasm.bigIntEnabled){ + m = capi.sqlite3_bind_int64; + }else if(util.bigIntFitsDouble(val)){ + val = Number(val); + m = capi.sqlite3_bind_double; + }else{ + f._tooBigInt(val); + } + }else{ // !int32, !bigint + val = Number(val); + if(wasm.bigIntEnabled && Number.isInteger(val)){ + m = capi.sqlite3_bind_int64; + }else{ + m = capi.sqlite3_bind_double; + } + } + rc = m(stmt.pointer, ndx, val); + break; + } + case BindTypes.boolean: + rc = capi.sqlite3_bind_int(stmt.pointer, ndx, val ? 1 : 0); + break; + case BindTypes.blob: { + if('string'===typeof val){ + rc = f._.string(stmt, ndx, val, true); + break; + }else if(val instanceof ArrayBuffer){ + val = new Uint8Array(val); + }else if(!util.isBindableTypedArray(val)){ + toss3("Binding a value as a blob requires", + "that it be a string, Uint8Array, Int8Array, or ArrayBuffer."); + } + const pBlob = wasm.alloc(val.byteLength || 1); + wasm.heap8().set(val.byteLength ? val : [0], Number(pBlob)) + rc = capi.sqlite3_bind_blob(stmt.pointer, ndx, pBlob, val.byteLength, + capi.SQLITE_WASM_DEALLOC); + break; + } + default: + sqlite3.config.warn("Unsupported bind() argument type:",val); + toss3("Unsupported bind() argument type: "+(typeof val)); + } + if(rc) DB.checkRc(stmt.db.pointer, rc); + return stmt; + }; + + Stmt.prototype = { + /** + "Finalizes" this statement. This is a no-op if the statement + has already been finalized. Returns the result of + sqlite3_finalize() (0 on success, non-0 on error), or the + undefined value if the statement has already been + finalized. Regardless of success or failure, most methods in + this class will throw if called after this is. + + This method always throws if called when it is illegal to do + so. Namely, when triggered via a per-row callback handler of a + DB.exec() call. + + If Stmt does not own its underlying (sqlite3_stmt*) (see + Stmt.wrapHandle()) then this function will not pass it to + sqlite3_finalize(). + */ + finalize: function(){ + const ptr = this.pointer; + if(ptr){ + affirmNotLockedByExec(this,'finalize()'); + const rc = (__doesNotOwnHandle.delete(this) + ? 0 + : capi.sqlite3_finalize(ptr)); + delete __stmtMap.get(this.db)[ptr]; + __ptrMap.delete(this); + __execLock.delete(this); + __stmtMayGet.delete(this); + delete this.parameterCount; + delete this.db; + return rc; + } + }, + /** + Clears all bound values. Returns this object. Throws if this + statement has been finalized or if modification of the + statement is currently illegal (e.g. in the per-row callback of + a DB.exec() call). + */ + clearBindings: function(){ + affirmNotLockedByExec(affirmStmtOpen(this), 'clearBindings()') + capi.sqlite3_clear_bindings(this.pointer); + __stmtMayGet.delete(this); + return this; + }, + /** + Resets this statement so that it may be step()ed again from the + beginning. Returns this object. Throws if this statement has + been finalized, if it may not legally be reset because it is + currently being used from a DB.exec() callback, or if the + underlying call to sqlite3_reset() returns non-0. + + If passed a truthy argument then this.clearBindings() is + also called, otherwise any existing bindings, along with + any memory allocated for them, are retained. + + In versions 3.42.0 and earlier, this function did not throw if + sqlite3_reset() returns non-0, but it was discovered that + throwing (or significant extra client-side code) is necessary + in order to avoid certain silent failure scenarios, as + discussed at: + + https://sqlite.org/forum/forumpost/36f7a2e7494897df + */ + reset: function(alsoClearBinds){ + affirmNotLockedByExec(this,'reset()'); + if(alsoClearBinds) this.clearBindings(); + const rc = capi.sqlite3_reset(affirmStmtOpen(this).pointer); + __stmtMayGet.delete(this); + checkSqlite3Rc(this.db, rc); + return this; + }, + /** + Binds one or more values to its bindable parameters. It + accepts 1 or 2 arguments: + + If passed a single argument, it must be either an array, an + object, or a value of a bindable type (see below). + + If passed 2 arguments, the first one is the 1-based bind + index or bindable parameter name and the second one must be + a value of a bindable type. + + Bindable value types: + + - null is bound as NULL. + + - undefined as a standalone value is a no-op intended to + simplify certain client-side use cases: passing undefined as + a value to this function will not actually bind anything and + this function will skip confirmation that binding is even + legal. (Those semantics simplify certain client-side uses.) + Conversely, a value of undefined as an array or object + property when binding an array/object (see below) is treated + the same as null. + + - Numbers are bound as either doubles or integers: doubles if + they are larger than 32 bits, else double or int32, depending + on whether they have a fractional part. Booleans are bound as + integer 0 or 1. It is not expected the distinction of binding + doubles which have no fractional parts and integers is + significant for the majority of clients due to sqlite3's data + typing model. If BigInt support is enabled then this routine + will bind BigInt values as 64-bit integers if they'll fit in + 64 bits. If that support disabled, it will store the BigInt + as an int32 or a double if it can do so without loss of + precision. If the BigInt is _too BigInt_ then it will throw. + + - Strings are bound as strings (use bindAsBlob() to force + blob binding). + + - Uint8Array, Int8Array, and ArrayBuffer instances are bound as + blobs. + + If passed an array, each element of the array is bound at + the parameter index equal to the array index plus 1 + (because arrays are 0-based but binding is 1-based). + + If passed an object, each object key is treated as a + bindable parameter name. The object keys _must_ match any + bindable parameter names, including any `$`, `@`, or `:` + prefix. Because `$` is a legal identifier chararacter in + JavaScript, that is the suggested prefix for bindable + parameters: `stmt.bind({$a: 1, $b: 2})`. + + It returns this object on success and throws on + error. Errors include: + + - Any bind index is out of range, a named bind parameter + does not match, or this statement has no bindable + parameters. + + - Any value to bind is of an unsupported type. + + - Passed no arguments or more than two. + + - The statement has been finalized. + */ + bind: function(/*[ndx,] arg*/){ + affirmStmtOpen(this); + let ndx, arg; + switch(arguments.length){ + case 1: ndx = 1; arg = arguments[0]; break; + case 2: ndx = arguments[0]; arg = arguments[1]; break; + default: toss3("Invalid bind() arguments."); + } + if(undefined===arg){ + /* It might seem intuitive to bind undefined as NULL + but this approach simplifies certain client-side + uses when passing on arguments between 2+ levels of + functions. */ + return this; + }else if(!this.parameterCount){ + toss3("This statement has no bindable parameters."); + } + __stmtMayGet.delete(this); + if(null===arg){ + /* bind NULL */ + return bindOne(this, ndx, BindTypes.null, arg); + } + else if(Array.isArray(arg)){ + /* bind each entry by index */ + if(1!==arguments.length){ + toss3("When binding an array, an index argument is not permitted."); + } + arg.forEach((v,i)=>bindOne(this, i+1, affirmSupportedBindType(v), v)); + return this; + }else if(arg instanceof ArrayBuffer){ + arg = new Uint8Array(arg); + } + if('object'===typeof arg/*null was checked above*/ + && !util.isBindableTypedArray(arg)){ + /* Treat each property of arg as a named bound parameter. */ + if(1!==arguments.length){ + toss3("When binding an object, an index argument is not permitted."); + } + Object.keys(arg) + .forEach(k=>bindOne(this, k, + affirmSupportedBindType(arg[k]), + arg[k])); + return this; + }else{ + return bindOne(this, ndx, affirmSupportedBindType(arg), arg); + } + toss3("Should not reach this point."); + }, + /** + Special case of bind() which binds the given value using the + BLOB binding mechanism instead of the default selected one for + the value. The ndx may be a numbered or named bind index. The + value must be of type string, null/undefined (both get treated + as null), or a TypedArray of a type supported by the bind() + API. This API cannot bind numbers as blobs. + + If passed a single argument, a bind index of 1 is assumed and + the first argument is the value. + */ + bindAsBlob: function(ndx,arg){ + affirmStmtOpen(this); + if(1===arguments.length){ + arg = ndx; + ndx = 1; + } + const t = affirmSupportedBindType(arg); + if(BindTypes.string !== t && BindTypes.blob !== t + && BindTypes.null !== t){ + toss3("Invalid value type for bindAsBlob()"); + } + return bindOne(this, ndx, BindTypes.blob, arg); + }, + /** + Steps the statement one time. If the result indicates that a + row of data is available, a truthy value is returned. If no + row of data is available, a falsy value is returned. Throws on + error. + */ + step: function(){ + affirmNotLockedByExec(this, 'step()'); + const rc = capi.sqlite3_step(affirmStmtOpen(this).pointer); + switch(rc){ + case capi.SQLITE_DONE: + __stmtMayGet.delete(this); + return false; + case capi.SQLITE_ROW: + __stmtMayGet.add(this); + return true; + default: + __stmtMayGet.delete(this); + sqlite3.config.warn("sqlite3_step() rc=",rc, + capi.sqlite3_js_rc_str(rc), + "SQL =", capi.sqlite3_sql(this.pointer)); + DB.checkRc(this.db.pointer, rc); + } + }, + /** + Functions exactly like step() except that... + + 1) On success, it calls this.reset() and returns this object. + + 2) On error, it throws and does not call reset(). + + This is intended to simplify constructs like: + + ``` + for(...) { + stmt.bind(...).stepReset(); + } + ``` + + Note that the reset() call makes it illegal to call this.get() + after the step. + */ + stepReset: function(){ + this.step(); + return this.reset(); + }, + /** + Functions like step() except that it calls finalize() on this + statement immediately after stepping, even if the step() call + throws. + + On success, it returns true if the step indicated that a row of + data was available, else it returns a falsy value. + + This is intended to simplify use cases such as: + + ``` + aDb.prepare("insert into foo(a) values(?)").bind(123).stepFinalize(); + ``` + */ + stepFinalize: function(){ + try{ + const rc = this.step(); + this.reset(/*for INSERT...RETURNING locking case*/); + return rc; + }finally{ + try{this.finalize()} + catch(e){/*ignored*/} + } + }, + + /** + Fetches the value from the given 0-based column index of + the current data row, throwing if index is out of range. + + Requires that step() has just returned a truthy value, else + an exception is thrown. + + By default it will determine the data type of the result + automatically. If passed a second argument, it must be one + of the enumeration values for sqlite3 types, which are + defined as members of the sqlite3 module: SQLITE_INTEGER, + SQLITE_FLOAT, SQLITE_TEXT, SQLITE_BLOB. Any other value, + except for undefined, will trigger an exception. Passing + undefined is the same as not passing a value. It is legal + to, e.g., fetch an integer value as a string, in which case + sqlite3 will convert the value to a string. + + If ndx is an array, this function behaves a differently: it + assigns the indexes of the array, from 0 to the number of + result columns, to the values of the corresponding column, + and returns that array. + + If ndx is a plain object, this function behaves even + differentlier: it assigns the properties of the object to + the values of their corresponding result columns and returns + that object. + + Blobs are returned as Uint8Array instances. + + Potential TODO: add type ID SQLITE_JSON, which fetches the + result as a string and passes it (if it's not null) to + JSON.parse(), returning the result of that. Until then, + getJSON() can be used for that. + */ + get: function(ndx,asType){ + if(!__stmtMayGet.has(affirmStmtOpen(this))){ + toss3("Stmt.step() has not (recently) returned true."); + } + if(Array.isArray(ndx)){ + let i = 0; + const n = this.columnCount; + while(i=Number.MIN_SAFE_INTEGER && rc<=Number.MAX_SAFE_INTEGER){ + /* Coerce "normal" number ranges to normal number values, + and only return BigInt-type values for numbers out of this + range. */ + return Number(rc).valueOf(); + } + return rc; + }else{ + const rc = capi.sqlite3_column_double(this.pointer, ndx); + if(rc>Number.MAX_SAFE_INTEGER || rctoss3("The pointer property is read-only.") + } + Object.defineProperty(Stmt.prototype, 'pointer', prop); + Object.defineProperty(DB.prototype, 'pointer', prop); + } + /** + Stmt.columnCount is an interceptor for sqlite3_column_count(). + + This requires an unfortunate performance hit compared to caching + columnCount when the Stmt is created/prepared (as was done in + SQLite <=3.42.0), but is necessary in order to handle certain + corner cases, as described in + https://sqlite.org/forum/forumpost/7774b773937cbe0a. + */ + Object.defineProperty(Stmt.prototype, 'columnCount', { + enumerable: false, + get: function(){return capi.sqlite3_column_count(this.pointer)}, + set: ()=>toss3("The columnCount property is read-only.") + }); + + Object.defineProperty(Stmt.prototype, 'parameterCount', { + enumerable: false, + get: function(){return capi.sqlite3_bind_parameter_count(this.pointer)}, + set: ()=>toss3("The parameterCount property is read-only.") + }); + + /** + The Stmt counterpart of oo1.DB.wrapHandle(), this creates a Stmt + instance which wraps a WASM (sqlite3_stmt*) in the oo1 API, + optionally with or without taking over ownership of that pointer. + + The first argument must be an oo1.DB instance[^1]. + + The second argument must be a valid WASM (sqlite3_stmt*), as + produced by sqlite3_prepare_v2() and sqlite3_prepare_v3(). + + The third argument, defaulting to false, specifies whether the + returned Stmt object takes over ownership of the underlying + (sqlite3_stmt*). If true, the returned object's finalize() method + will finalize that handle, else it will not. If it is false, + ownership of pStmt is unchanged and pStmt MUST outlive the + returned object or results are undefined. + + This function throws if the arguments are invalid. On success it + returns a new Stmt object which wraps the given statement + pointer. + + Like all Stmt objects, the finalize() method must eventually be + called on the returned object to free up internal resources, + regardless of whether this function's third argument is true or + not. + + [^1]: The first argument cannot be a (sqlite3*) because the + resulting Stmt object requires a parent DB object. It is not yet + determined whether it would be of general benefit to refactor the + DB/Stmt pair internals to communicate in terms of the underlying + (sqlite3*) rather than a DB object. If so, we could laxen the + first argument's requirement and allow an (sqlite3*). Because + DB.wrapHandle() enables multiple DB objects to proxy the same + (sqlite3*), we cannot unambiguously translate the first arugment + from (sqlite3*) to DB instances for us with this function's first + argument. + */ + Stmt.wrapHandle = function(oo1db, pStmt, takeOwnership=false){ + let ctor = Stmt; + if( !(oo1db instanceof DB) || !oo1db.pointer ){ + throw new sqlite3.SQLite3Error(sqlite3.SQLITE_MISUSE, + "First argument must be an opened "+ + "sqlite3.oo1.DB instance"); + } + if( !pStmt || !wasm.isPtr(pStmt) ){ + throw new sqlite3.SQLite3Error(sqlite3.SQLITE_MISUSE, + "Second argument must be a WASM "+ + "sqlite3_stmt pointer"); + } + return new Stmt(oo1db, pStmt, BindTypes, !!takeOwnership); + } + + /** The OO API's public namespace. */ + sqlite3.oo1 = { + DB, + Stmt + }/*oo1 object*/; + + if(util.isUIThread()){ + /** + Functionally equivalent to DB(storageName,'c','kvvfs') except + that it throws if the given storage name is not one of 'local' + or 'session'. + + As of version 3.46, the argument may optionally be an options + object in the form: + + { + filename: 'session'|'local', + ... etc. (all options supported by the DB ctor) + } + + noting that the 'vfs' option supported by main DB + constructor is ignored here: the vfs is always 'kvvfs'. + */ + sqlite3.oo1.JsStorageDb = function(storageName='session'){ + const opt = dbCtorHelper.normalizeArgs(...arguments); + storageName = opt.filename; + if('session'!==storageName && 'local'!==storageName){ + toss3("JsStorageDb db name must be one of 'session' or 'local'."); + } + opt.vfs = 'kvvfs'; + dbCtorHelper.call(this, opt); + }; + const jdb = sqlite3.oo1.JsStorageDb; + jdb.prototype = Object.create(DB.prototype); + /** Equivalent to sqlite3_js_kvvfs_clear(). */ + jdb.clearStorage = capi.sqlite3_js_kvvfs_clear; + /** + Clears this database instance's storage or throws if this + instance has been closed. Returns the number of + database blocks which were cleaned up. + */ + jdb.prototype.clearStorage = function(){ + return jdb.clearStorage(affirmDbOpen(this).filename); + }; + /** Equivalent to sqlite3_js_kvvfs_size(). */ + jdb.storageSize = capi.sqlite3_js_kvvfs_size; + /** + Returns the _approximate_ number of bytes this database takes + up in its storage or throws if this instance has been closed. + */ + jdb.prototype.storageSize = function(){ + return jdb.storageSize(affirmDbOpen(this).filename); + }; + }/*main-window-only bits*/ + +}); +//#else +/* Built with the omit-oo1 flag. */ +//#endif if not omit-oo1 diff --git a/ext/wasm/api/sqlite3-api-prologue.js b/ext/wasm/api/sqlite3-api-prologue.js new file mode 100644 index 0000000000..069f3fdb5c --- /dev/null +++ b/ext/wasm/api/sqlite3-api-prologue.js @@ -0,0 +1,2209 @@ +/* + 2022-05-22 + + The author disclaims copyright to this source code. In place of a + legal notice, here is a blessing: + + * May you do good and not evil. + * May you find forgiveness for yourself and forgive others. + * May you share freely, never taking more than you give. + + *********************************************************************** + + This file is intended to be combined at build-time with other + related code, most notably a header and footer which wraps this + whole file into a single callback which can be run after Emscripten + loads the corresponding WASM module. The sqlite3 JS API has no hard + requirements on Emscripten and does not expose any Emscripten APIs + to clients. It is structured such that its build can be tweaked to + include it in arbitrary WASM environments which can supply the + necessary underlying features (e.g. a POSIX file I/O layer). + + Main project home page: https://sqlite.org + + Documentation home page: https://sqlite.org/wasm +*/ + +/** + sqlite3ApiBootstrap() is the only global symbol persistently + exposed by this API. It is intended to be called one time at the + end of the API amalgamation process, passed configuration details + for the current environment, and then optionally be removed from + the global object using `delete globalThis.sqlite3ApiBootstrap`. + + This function is not intended for client-level use. It is intended + for use in creating bundles configured for specific WASM + environments. + + This function expects a configuration object, intended to abstract + away details specific to any given WASM environment, primarily so + that it can be used without any direct dependency on + Emscripten. (Note the default values for the config object!) The + config object is only honored the first time this is + called. Subsequent calls ignore the argument and return the same + (configured) object which gets initialized by the first call. This + function will throw if any of the required config options are + missing. + + The config object properties include: + + - `exports`[^1]: the "exports" object for the current WASM + environment. In an Emscripten-based build, this should be set to + `Module['asm']` (versions <=3.1.43) or `Module['wasmExports']` + (versions >=3.1.44). + + - `memory`[^1]: optional WebAssembly.Memory object, defaulting to + `exports.memory`. In Emscripten environments this should be set + to `Module.wasmMemory` if the build uses `-sIMPORTED_MEMORY`, or be + left undefined/falsy to default to `exports.memory` when using + WASM-exported memory. + + - `functionTable`[^1]: optional WebAssembly.Table object holding + the indirect function table. If not set then the table is assumed + to be in `exports.__indirect_function_table`. + + - `bigIntEnabled`: true if BigInt support is enabled. Defaults to + true if `globalThis.BigInt64Array` is available, else false. Some APIs + will throw exceptions if called without BigInt support, as BigInt + is required for marshalling C-side int64 into and out of JS. + (Sidebar: it is technically possible to add int64 support via + marshalling of int32 pairs, but doing so is unduly invasive.) + + - `allocExportName`: the name of the function, in `exports`, of the + `malloc(3)`-compatible routine for the WASM environment. Defaults + to `"sqlite3_malloc"`. Beware that using any allocator other than + sqlite3_malloc() may require care in certain client-side code + regarding which allocator is uses. Notably, sqlite3_deserialize() + and sqlite3_serialize() can only safely use memory from different + allocators under very specific conditions. The canonical builds + of this API guaranty that `sqlite3_malloc()` is the JS-side + allocator implementation. + + - `deallocExportName`: the name of the function, in `exports`, of + the `free(3)`-compatible routine for the WASM + environment. Defaults to `"sqlite3_free"`. + + - `reallocExportName`: the name of the function, in `exports`, of + the `realloc(3)`-compatible routine for the WASM + environment. Defaults to `"sqlite3_realloc"`. + + - `debug`, `log`, `warn`, and `error` may be functions equivalent + to the like-named methods of the global `console` object. By + default, these map directly to their `console` counterparts, but + can be replaced with (e.g.) empty functions to squelch all such + output. + + - `wasmfsOpfsDir`[^1]: Specifies the "mount point" of the OPFS-backed + filesystem in WASMFS-capable builds. + + + [^1] = This property may optionally be a function, in which case + this function calls that function to fetch the value, + enabling delayed evaluation. + + The returned object is the top-level sqlite3 namespace object. + + + Client code may optionally assign sqlite3ApiBootstrap.defaultConfig + an object-type value before calling sqlite3ApiBootstrap() (without + arguments) in order to tell that call to use this object as its + default config value. The intention of this is to provide + downstream clients with a reasonably flexible approach for plugging + in an environment-suitable configuration without having to define a + new global-scope symbol. + + However, because clients who access this library via an + Emscripten-hosted module will not have an opportunity to call + sqlite3ApiBootstrap() themselves, nor to access it before it is + called, an alternative option for setting the configuration is to + define globalThis.sqlite3ApiConfig to an object. If it is set, it + is used instead of sqlite3ApiBootstrap.defaultConfig if + sqlite3ApiBootstrap() is called without arguments. Setting the + `exports` and `memory` parts require already having loaded the WASM + module, though. + + Both sqlite3ApiBootstrap.defaultConfig and + globalThis.sqlite3ApiConfig get deleted by sqlite3ApiBootstrap() + because any changes to them made after that point would have no + useful effect. + + This function returns a Promise to the sqlite3 namespace object, + which resolves after the async pieces of the library init are + complete. +*/ +'use strict'; +globalThis.sqlite3ApiBootstrap = async function sqlite3ApiBootstrap( + apiConfig = (globalThis.sqlite3ApiConfig || sqlite3ApiBootstrap.defaultConfig) +){ + if(sqlite3ApiBootstrap.sqlite3){ /* already initialized */ + (sqlite3ApiBootstrap.sqlite3.config || console).warn( + "sqlite3ApiBootstrap() called multiple times.", + "Config and external initializers are ignored on calls after the first." + ); + return sqlite3ApiBootstrap.sqlite3; + } + const config = Object.assign(Object.create(null),{ + exports: undefined, + memory: undefined, + bigIntEnabled: !!globalThis.BigInt64Array, + debug: console.debug.bind(console), + warn: console.warn.bind(console), + error: console.error.bind(console), + log: console.log.bind(console), + wasmfsOpfsDir: '/opfs', + /** + useStdAlloc is just for testing allocator discrepancies. The + docs guarantee that this is false in the canonical builds. For + 99% of purposes it doesn't matter which allocators we use, but + it becomes significant with, e.g., sqlite3_deserialize() and + certain wasm.xWrap.resultAdapter()s. + */ + useStdAlloc: false + }, apiConfig || {}); + + Object.assign(config, { + allocExportName: config.useStdAlloc ? 'malloc' : 'sqlite3_malloc', + deallocExportName: config.useStdAlloc ? 'free' : 'sqlite3_free', + reallocExportName: config.useStdAlloc ? 'realloc' : 'sqlite3_realloc' + }); + + [ + // If any of these config options are functions, replace them with + // the result of calling that function. They must not be async. + 'exports', 'memory', 'functionTable', 'wasmfsOpfsDir' + ].forEach((k)=>{ + if('function' === typeof config[k]){ + config[k] = config[k](); + } + }); + + /** + Eliminate any confusion about whether these config objects may + be used after library initialization by eliminating the outward-facing + objects... + */ + delete globalThis.sqlite3ApiConfig; + delete sqlite3ApiBootstrap.defaultConfig; + + /** + The main sqlite3 binding API gets installed into this object, + mimicking the C API as closely as we can. The numerous members + names with prefixes 'sqlite3_' and 'SQLITE_' behave, insofar as + possible, identically to the C-native counterparts, as documented at: + + https://sqlite.org/c3ref/intro.html + + A very few exceptions require an additional level of proxy + function or may otherwise require special attention in the WASM + environment, and all such cases are documented somewhere below + in this file or in sqlite3-api-glue.js. capi members which are + not documented are installed as 1-to-1 proxies for their + C-side counterparts. + */ + const capi = Object.create(null); + /** + Holds state which are specific to the WASM-related + infrastructure and glue code. + + Note that a number of members of this object are injected + dynamically after the api object is fully constructed, so + not all are documented in this file. + */ + const wasm = Object.create(null); + + /** Internal helper for SQLite3Error ctor. */ + const __rcStr = (rc)=>{ + return (capi.sqlite3_js_rc_str && capi.sqlite3_js_rc_str(rc)) + || ("Unknown result code #"+rc); + }; + + /** Internal helper for SQLite3Error ctor. */ + const isInt32 = (n)=> + 'number'===typeof n + && n===(n | 0) + && n<=2147483647 && n>=-2147483648; + + /** + An Error subclass specifically for reporting DB-level errors and + enabling clients to unambiguously identify such exceptions. + The C-level APIs never throw, but some of the higher-level + C-style APIs do and the object-oriented APIs use exceptions + exclusively to report errors. + */ + class SQLite3Error extends Error { + /** + Constructs this object with a message depending on its arguments: + + If its first argument is an integer, it is assumed to be + an SQLITE_... result code and it is passed to + sqlite3.capi.sqlite3_js_rc_str() to stringify it. + + If called with exactly 2 arguments and the 2nd is an object, + that object is treated as the 2nd argument to the parent + constructor. + + The exception's message is created by concatenating its + arguments with a space between each, except for the + two-args-with-an-object form and that the first argument will + get coerced to a string, as described above, if it's an + integer. + + If passed an integer first argument, the error object's + `resultCode` member will be set to the given integer value, + else it will be set to capi.SQLITE_ERROR. + */ + constructor(...args){ + let rc; + if(args.length){ + if(isInt32(args[0])){ + rc = args[0]; + if(1===args.length){ + super(__rcStr(args[0])); + }else{ + const rcStr = __rcStr(rc); + if('object'===typeof args[1]){ + super(rcStr,args[1]); + }else{ + args[0] = rcStr+':'; + super(args.join(' ')); + } + } + }else{ + if(2===args.length && 'object'===typeof args[1]){ + super(...args); + }else{ + super(args.join(' ')); + } + } + } + this.resultCode = rc || capi.SQLITE_ERROR; + this.name = 'SQLite3Error'; + } + }; + + /** + Functionally equivalent to the SQLite3Error constructor but may + be used as part of an expression, e.g.: + + ``` + return someFunction(x) || SQLite3Error.toss(...); + ``` + */ + SQLite3Error.toss = (...args)=>{ + throw new SQLite3Error(...args); + }; + const toss3 = SQLite3Error.toss; + + if(config.wasmfsOpfsDir && !/^\/[^/]+$/.test(config.wasmfsOpfsDir)){ + toss3("config.wasmfsOpfsDir must be falsy or in the form '/dir-name'."); + } + + /** + Returns true if the given BigInt value is small enough to fit + into an int64 value, else false. + */ + const bigIntFits64 = function f(b){ + if(!f._max){ + f._max = BigInt("0x7fffffffffffffff"); + f._min = ~f._max; + } + return b >= f._min && b <= f._max; + }; + + /** + Returns true if the given BigInt value is small enough to fit + into an int32, else false. + */ + const bigIntFits32 = (b)=>(b >= (-0x7fffffffn - 1n) && b <= 0x7fffffffn); + + /** + Returns true if the given BigInt value is small enough to fit + into a double value without loss of precision, else false. + */ + const bigIntFitsDouble = function f(b){ + if(!f._min){ + f._min = Number.MIN_SAFE_INTEGER; + f._max = Number.MAX_SAFE_INTEGER; + } + return b >= f._min && b <= f._max; + }; + + /** Returns v if v appears to be a TypedArray, else false. */ + const isTypedArray = (v)=>{ + return (v && v.constructor && isInt32(v.constructor.BYTES_PER_ELEMENT)) ? v : false; + }; + + /** + Returns true if v appears to be one of our bind()-able TypedArray + types: Uint8Array or Int8Array or ArrayBuffer. Support for + TypedArrays with element sizes >1 is a potential TODO just + waiting on a use case to justify them. Until then, their `buffer` + property can be used to pass them as an ArrayBuffer. If it's not + a bindable array type, a falsy value is returned. + */ + const isBindableTypedArray = (v)=> + v && (v instanceof Uint8Array + || v instanceof Int8Array + || v instanceof ArrayBuffer); + + /** + Returns true if v appears to be one of the TypedArray types + which is legal for holding SQL code (as opposed to binary blobs). + + Currently this is the same as isBindableTypedArray() but it + seems likely that we'll eventually want to add Uint32Array + and friends to the isBindableTypedArray() list but not to the + isSQLableTypedArray() list. + */ + const isSQLableTypedArray = (v)=> + v && (v instanceof Uint8Array + || v instanceof Int8Array + || v instanceof ArrayBuffer); + + /** Returns true if isBindableTypedArray(v) does, else throws with a message + that v is not a supported TypedArray value. */ + const affirmBindableTypedArray = (v)=> + isBindableTypedArray(v) + || toss3("Value is not of a supported TypedArray type."); + + /** + If v is-a Array, its join("") result is returned. If + isSQLableTypedArray(v) is true then wasm.typedArrayToString(v) is + returned. If it looks like a WASM pointer, wasm.cstrToJs(v) is + returned. Else v is returned as-is. + + Reminder to self: the "return as-is" instead of returning ''+v is + arguably a design mistake but changing it is risky at this point. + */ + const flexibleString = function(v){ + if(isSQLableTypedArray(v)){ + return wasm.typedArrayToString( + (v instanceof ArrayBuffer) ? new Uint8Array(v) : v, + 0, v.length + ); + } + else if(Array.isArray(v)) return v.join(""); + else if(wasm.isPtr(v)) v = wasm.cstrToJs(v); + return v; + }; + + /** + An Error subclass specifically for reporting Wasm-level malloc() + failure and enabling clients to unambiguously identify such + exceptions. + */ + class WasmAllocError extends Error { + /** + If called with 2 arguments and the 2nd one is an object, it + behaves like the Error constructor, else it concatenates all + arguments together with a single space between each to + construct an error message string. As a special case, if + called with no arguments then it uses a default error + message. + */ + constructor(...args){ + if(2===args.length && 'object'===typeof args[1]){ + super(...args); + }else if(args.length){ + super(args.join(' ')); + }else{ + super("Allocation failed."); + } + this.resultCode = capi.SQLITE_NOMEM; + this.name = 'WasmAllocError'; + } + }; + /** + Functionally equivalent to the WasmAllocError constructor but may + be used as part of an expression, e.g.: + + ``` + return someAllocatingFunction(x) || WasmAllocError.toss(...); + ``` + */ + WasmAllocError.toss = (...args)=>{ + throw new WasmAllocError(...args); + }; + + Object.assign(capi, { + /** + sqlite3_bind_blob() works exactly like its C counterpart unless + its 3rd argument is one of: + + - JS string: the 3rd argument is converted to a C string, the + 4th argument is ignored, and the C-string's length is used + in its place. + + - Array: converted to a string as defined for "flexible + strings" and then it's treated as a JS string. + + - Int8Array or Uint8Array: wasm.allocFromTypedArray() is used to + conver the memory to the WASM heap. If the 4th argument is + 0 or greater, it is used as-is, otherwise the array's byteLength + value is used. This is an exception to the C API's undefined + behavior for a negative 4th argument, but results are undefined + if the given 4th argument value is greater than the byteLength + of the input array. + + - If it's an ArrayBuffer, it gets wrapped in a Uint8Array and + treated as that type. + + In all of those cases, the final argument (destructor) is + ignored and capi.SQLITE_WASM_DEALLOC is assumed. + + A 3rd argument of `null` is treated as if it were a WASM pointer + of 0. + + If the 3rd argument is neither a WASM pointer nor one of the + above-described types, capi.SQLITE_MISUSE is returned. + + The first argument may be either an `sqlite3_stmt*` WASM + pointer or an sqlite3.oo1.Stmt instance. + + For consistency with the C API, it requires the same number of + arguments. It returns capi.SQLITE_MISUSE if passed any other + argument count. + */ + sqlite3_bind_blob: undefined/*installed later*/, + + /** + sqlite3_bind_text() works exactly like its C counterpart unless + its 3rd argument is one of: + + - JS string: the 3rd argument is converted to a C string, the + 4th argument is ignored, and the C-string's length is used + in its place. + + - Array: converted to a string as defined for "flexible + strings". The 4th argument is ignored and a value of -1 + is assumed. + + - Int8Array or Uint8Array: is assumed to contain UTF-8 text, is + converted to a string. The 4th argument is ignored, replaced + by the array's byteLength value. + + - If it's an ArrayBuffer, it gets wrapped in a Uint8Array and + treated as that type. + + In each of those cases, the final argument (text destructor) is + ignored and capi.SQLITE_WASM_DEALLOC is assumed. + + A 3rd argument of `null` is treated as if it were a WASM pointer + of 0. + + If the 3rd argument is neither a WASM pointer nor one of the + above-described types, capi.SQLITE_MISUSE is returned. + + The first argument may be either an `sqlite3_stmt*` WASM + pointer or an sqlite3.oo1.Stmt instance. + + For consistency with the C API, it requires the same number of + arguments. It returns capi.SQLITE_MISUSE if passed any other + argument count. + + If client code needs to bind partial strings, it needs to + either parcel the string up before passing it in here or it + must pass in a WASM pointer for the 3rd argument and a valid + 4th-argument value, taking care not to pass a value which + truncates a multi-byte UTF-8 character. When passing + WASM-format strings, it is important that the final argument be + valid or unexpected content can result, or WASM may crash if + the application reads past the WASM heap bounds. + */ + sqlite3_bind_text: undefined/*installed later*/, + + /** + sqlite3_create_function_v2() differs from its native + counterpart only in the following ways: + + 1) The fourth argument (`eTextRep`) argument must not specify + any encoding other than sqlite3.SQLITE_UTF8. The JS API does not + currently support any other encoding and likely never + will. This function does not replace that argument on its own + because it may contain other flags. As a special case, if + the bottom 4 bits of that argument are 0, SQLITE_UTF8 is + assumed. + + 2) Any of the four final arguments may be either WASM pointers + (assumed to be function pointers) or JS Functions. In the + latter case, each gets bound to WASM using + sqlite3.capi.wasm.installFunction() and that wrapper is passed + on to the native implementation. + + For consistency with the C API, it requires the same number of + arguments. It returns capi.SQLITE_MISUSE if passed any other + argument count. + + The semantics of JS functions are: + + xFunc: is passed `(pCtx, ...values)`. Its return value becomes + the new SQL function's result. + + xStep: is passed `(pCtx, ...values)`. Its return value is + ignored. + + xFinal: is passed `(pCtx)`. Its return value becomes the new + aggregate SQL function's result. + + xDestroy: is passed `(void*)`. Its return value is ignored. The + pointer passed to it is the one from the 5th argument to + sqlite3_create_function_v2(). + + Note that: + + - `pCtx` in the above descriptions is a `sqlite3_context*`. At + least 99 times out of a hundred, that initial argument will + be irrelevant for JS UDF bindings, but it needs to be there + so that the cases where it _is_ relevant, in particular with + window and aggregate functions, have full access to the + lower-level sqlite3 APIs. + + - When wrapping JS functions, the remaining arguments are passd + to them as positional arguments, not as an array of + arguments, because that allows callback definitions to be + more JS-idiomatic than C-like. For example `(pCtx,a,b)=>a+b` + is more intuitive and legible than + `(pCtx,args)=>args[0]+args[1]`. For cases where an array of + arguments would be more convenient, the callbacks simply need + to be declared like `(pCtx,...args)=>{...}`, in which case + `args` will be an array. + + - If a JS wrapper throws, it gets translated to + sqlite3_result_error() or sqlite3_result_error_nomem(), + depending on whether the exception is an + sqlite3.WasmAllocError object or not. + + - When passing on WASM function pointers, arguments are _not_ + converted or reformulated. They are passed on as-is in raw + pointer form using their native C signatures. Only JS + functions passed in to this routine, and thus wrapped by this + routine, get automatic conversions of arguments and result + values. The routines which perform those conversions are + exposed for client-side use as sqlite3_values_to_js(), + sqlite3_result_js(), and sqlite3_result_error_js(). + + For xFunc(), xStep(), and xFinal(): + + - When called from SQL, arguments to the UDF, and its result, + will be converted between JS and SQL with as much fidelity as + is feasible, triggering an exception if a type conversion + cannot be determined. Some freedom is afforded to numeric + conversions due to friction between the JS and C worlds: + integers which are larger than 32 bits may be treated as + doubles or BigInts. + + If any JS-side bound functions throw, those exceptions are + intercepted and converted to database-side errors with the + exception of xDestroy(): any exception from it is ignored, + possibly generating a console.error() message. Destructors + must not throw. + + Automatically-converted JS-to-WASM functions will be cleaned up + either when (A) this function is called again with the same + name, arity, and encoding, but null/0 values for the functions, + or (B) when pDb is passed to sqlite3_close_v2(). If this factor + is relevant for a given client, they can create WASM-bound JS + functions themselves, hold on to their pointers, and pass the + pointers in to here. Later on, they can free those pointers + (using `wasm.uninstallFunction()` or equivalent). + + C reference: https://sqlite.org/c3ref/create_function.html + + Maintenance reminder: the ability to add new + WASM-accessible functions to the runtime requires that the + WASM build is compiled with emcc's `-sALLOW_TABLE_GROWTH` + flag. + */ + sqlite3_create_function_v2: ( + pDb, funcName, nArg, eTextRep, pApp, + xFunc, xStep, xFinal, xDestroy + )=>{/*installed later*/}, + /** + Equivalent to passing the same arguments to + sqlite3_create_function_v2(), with 0 as the final argument. + */ + sqlite3_create_function: ( + pDb, funcName, nArg, eTextRep, pApp, + xFunc, xStep, xFinal + )=>{/*installed later*/}, + /** + The sqlite3_create_window_function() JS wrapper differs from + its native implementation in the exact same way that + sqlite3_create_function_v2() does. The additional function, + xInverse(), is treated identically to xStep() by the wrapping + layer. + */ + sqlite3_create_window_function: ( + pDb, funcName, nArg, eTextRep, pApp, + xStep, xFinal, xValue, xInverse, xDestroy + )=>{/*installed later*/}, + /** + The sqlite3_prepare_v3() binding handles two different uses + with differing JS/WASM semantics: + + 1) sqlite3_prepare_v3(pDb, sqlString, -1, prepFlags, ppStmt , null) + + 2) sqlite3_prepare_v3(pDb, sqlPointer, sqlByteLen, prepFlags, ppStmt, sqlPointerToPointer) + + The SQL length argument (the 3rd argument) must, for usage (1), + always be negative because it must be a byte length and that + value is expensive to calculate from JS (where only the + character length of strings is readily available). It is + retained in this API's interface for code/documentation + compatibility reasons but is currently _always_ ignored. With + usage (2), the 3rd argument is used as-is but is is still + critical that the C-style input string (2nd argument) be + terminated with a 0 byte. + + In usage (1), the 2nd argument must be of type string, + Uint8Array, Int8Array, or ArrayBuffer (all of which are assumed + to hold SQL). If it is, this function assumes case (1) and + calls the underyling C function with the equivalent of: + + (pDb, sqlAsString, -1, prepFlags, ppStmt, null) + + The `pzTail` argument is ignored in this case because its + result is meaningless when a string-type value is passed + through: the string goes through another level of internal + conversion for WASM's sake and the result pointer would refer + to that transient conversion's memory, not the passed-in + string. + + If the sql argument is not a string, it must be a _pointer_ to + a NUL-terminated string which was allocated in the WASM memory + (e.g. using capi.wasm.alloc() or equivalent). In that case, + the final argument may be 0/null/undefined or must be a pointer + to which the "tail" of the compiled SQL is written, as + documented for the C-side sqlite3_prepare_v3(). + + In case (2), the underlying C function is called with the + equivalent of: + + (pDb, sqlAsPointer, sqlByteLen, prepFlags, ppStmt, pzTail) + + It returns its result and compiled statement as documented in + the C API. Fetching the output pointers (5th and 6th + parameters) requires using `capi.wasm.peek()` (or + equivalent) and the `pzTail` will point to an address relative to + the `sqlAsPointer` value. + + If passed an invalid 2nd argument type, this function will + return SQLITE_MISUSE and sqlite3_errmsg() will contain a string + describing the problem. + + Side-note: if given an empty string, or one which contains only + comments or an empty SQL expression, 0 is returned but the result + output pointer will be NULL. + */ + sqlite3_prepare_v3: (dbPtr, sql, sqlByteLen, prepFlags, + stmtPtrPtr, strPtrPtr)=>{}/*installed later*/, + + /** + Equivalent to calling sqlite3_prapare_v3() with 0 as its 4th argument. + */ + sqlite3_prepare_v2: (dbPtr, sql, sqlByteLen, + stmtPtrPtr,strPtrPtr)=>{}/*installed later*/, + + /** + This binding enables the callback argument to be a JavaScript. + + If the callback is a function, then for the duration of the + sqlite3_exec() call, it installs a WASM-bound function which + acts as a proxy for the given callback. That proxy will also + perform a conversion of the callback's arguments from + `(char**)` to JS arrays of strings. However, for API + consistency's sake it will still honor the C-level callback + parameter order and will call it like: + + `callback(pVoid, colCount, listOfValues, listOfColNames)` + + If the callback is not a JS function then this binding performs + no translation of the callback, but the sql argument is still + converted to a WASM string for the call using the + "string:flexible" argument converter. + */ + sqlite3_exec: (pDb, sql, callback, pVoid, pErrMsg)=>{}/*installed later*/, + + /** + If passed a single argument which appears to be a byte-oriented + TypedArray (Int8Array or Uint8Array), this function treats that + TypedArray as an output target, fetches `theArray.byteLength` + bytes of randomness, and populates the whole array with it. As + a special case, if the array's length is 0, this function + behaves as if it were passed (0,0). When called this way, it + returns its argument, else it returns the `undefined` value. + + If called with any other arguments, they are passed on as-is + to the C API. Results are undefined if passed any incompatible + values. + */ + sqlite3_randomness: (n, outPtr)=>{/*installed later*/}, + }/*capi*/); + + /** + Various internal-use utilities are added here as needed. They + are bound to an object only so that we have access to them in + the differently-scoped steps of the API bootstrapping + process. At the end of the API setup process, this object gets + removed. These are NOT part of the public API. + */ + const util = { + affirmBindableTypedArray, flexibleString, + bigIntFits32, bigIntFits64, bigIntFitsDouble, + isBindableTypedArray, + isInt32, isSQLableTypedArray, isTypedArray, + isUIThread: ()=>(globalThis.window===globalThis && !!globalThis.document), + // is this true for ESM?: 'undefined'===typeof WorkerGlobalScope + toss: function(...args){throw new Error(args.join(' '))}, + toss3, + typedArrayPart: wasm.typedArrayPart, + /** + Given a byte array or ArrayBuffer, this function throws if the + lead bytes of that buffer do not hold a SQLite3 database header, + else it returns without side effects. + + Added in 3.44. + */ + affirmDbHeader: function(bytes){ + if(bytes instanceof ArrayBuffer) bytes = new Uint8Array(bytes); + const header = "SQLite format 3"; + if( header.length > bytes.byteLength ){ + toss3("Input does not contain an SQLite3 database header."); + } + for(let i = 0; i < header.length; ++i){ + if( header.charCodeAt(i) !== bytes[i] ){ + toss3("Input does not contain an SQLite3 database header."); + } + } + }, + /** + Given a byte array or ArrayBuffer, this function throws if the + database does not, at a cursory glance, appear to be an SQLite3 + database. It only examines the size and header, but further + checks may be added in the future. + + Added in 3.44. + */ + affirmIsDb: function(bytes){ + if(bytes instanceof ArrayBuffer) bytes = new Uint8Array(bytes); + const n = bytes.byteLength; + if(n<512 || n%512!==0) { + toss3("Byte array size",n,"is invalid for an SQLite3 db."); + } + util.affirmDbHeader(bytes); + } + }/*util*/; + + /** + wasm.X properties which are used for configuring the wasm + environment via whwashutil.js. + */ + Object.assign(wasm, { + /** + The WASM IR (Intermediate Representation) value for + pointer-type values. If set then it MUST be one of 'i32' or + 'i64' (else an exception will be thrown). If it's not set, it + will default to 'i32'. + */ + pointerIR: config.wasmPtrIR, + + /** + True if BigInt support was enabled via (e.g.) the + Emscripten -sWASM_BIGINT flag, else false. When + enabled, certain 64-bit sqlite3 APIs are enabled which + are not otherwise enabled due to JS/WASM int64 + impedance mismatches. + */ + bigIntEnabled: !!config.bigIntEnabled, + + /** + The symbols exported by the WASM environment. + */ + exports: config.exports + || toss3("Missing API config.exports (WASM module exports)."), + + /** + When Emscripten compiles with `-sIMPORTED_MEMORY`, it + initializes the heap and imports it into wasm, as opposed to + the other way around. In this case, the memory is not + available via this.exports.memory. + */ + memory: config.memory + || config.exports['memory'] + || toss3("API config object requires a WebAssembly.Memory object", + "in either config.exports.memory (exported)", + "or config.memory (imported)."), + + /** + WebAssembly.Table object holding the indirect function call + table. Defaults to exports.__indirect_function_table. + */ + functionTable: config.functionTable, + + /** + The API's primary point of access to the WASM-side memory + allocator. Works like sqlite3_malloc() but throws a + WasmAllocError if allocation fails. It is important that any + code which might pass through the sqlite3 C API NOT throw and + must instead return SQLITE_NOMEM (or equivalent, depending on + the context). + + Very few cases in the sqlite3 JS APIs can result in + client-defined functions propagating exceptions via the C-style + API. Most notably, this applies to WASM-bound JS functions + which are created directly by clients and passed on _as WASM + function pointers_ to functions such as + sqlite3_create_function_v2(). Such bindings created + transparently by this API will automatically use wrappers which + catch exceptions and convert them to appropriate error codes. + + For cases where non-throwing allocation is required, use + this.alloc.impl(), which is the unadulterated WASM-exported + counterpart of this wrapper. + + Design note: this function is not named "malloc" primarily + because Emscripten uses that name and we wanted to avoid any + confusion early on in this code's development, when it still + had close ties to Emscripten's glue code. + */ + alloc: undefined/*installed later*/, + + /** + Rarely necessary in JS code, this routine works like + sqlite3_realloc(M,N), where M is either NULL or a pointer + obtained from this function or this.alloc() and N is the number + of bytes to reallocate the block to. Returns a pointer to the + reallocated block or 0 if allocation fails. + + If M is NULL and N is positive, this behaves like + this.alloc(N). If N is 0, it behaves like this.dealloc(). + Results are undefined if N is negative (sqlite3_realloc() + treats that as 0, but if this code is built with a different + allocator it may misbehave with negative values). + + Like this.alloc.impl(), this.realloc.impl() is a direct binding + to the underlying realloc() implementation which does not throw + exceptions, instead returning 0 on allocation error. + */ + realloc: undefined/*installed later*/, + + /** + The API's primary point of access to the WASM-side memory + deallocator. Works like sqlite3_free(). + + Design note: this function is not named "free" for the same + reason that this.alloc() is not called this.malloc(). + */ + dealloc: undefined/*installed later*/ + + /* Many more wasm-related APIs get installed later on. */ + }/*wasm*/); + + /** + wasm.alloc()'s srcTypedArray.byteLength bytes, + populates them with the values from the source + TypedArray, and returns the pointer to that memory. The + returned pointer must eventually be passed to + wasm.dealloc() to clean it up. + + The argument may be a Uint8Array, Int8Array, or ArrayBuffer, + and it throws if passed any other type. + + As a special case, to avoid further special cases where + this is used, if srcTypedArray.byteLength is 0, it + allocates a single byte and sets it to the value + 0. Even in such cases, calls must behave as if the + allocated memory has exactly srcTypedArray.byteLength + bytes. + */ + wasm.allocFromTypedArray = function(srcTypedArray){ + if(srcTypedArray instanceof ArrayBuffer){ + srcTypedArray = new Uint8Array(srcTypedArray); + } + affirmBindableTypedArray(srcTypedArray); + const pRet = wasm.alloc(srcTypedArray.byteLength || 1); + wasm.heapForSize(srcTypedArray.constructor) + .set(srcTypedArray.byteLength ? srcTypedArray : [0], Number(pRet)) + /* Maintenance note: the order of alloc() and heapForSize() calls + is significant: https://sqlite.org/forum/forumpost/05b77273be104532 */; + return pRet; + }; + + { + // Set up allocators... + const keyAlloc = config.allocExportName, + keyDealloc = config.deallocExportName, + keyRealloc = config.reallocExportName; + for(const key of [keyAlloc, keyDealloc, keyRealloc]){ + const f = wasm.exports[key]; + if(!(f instanceof Function)) toss3("Missing required exports[",key,"] function."); + } + + wasm.alloc = function f(n){ + return f.impl(n) || WasmAllocError.toss("Failed to allocate",n," bytes."); + }; + wasm.alloc.impl = wasm.exports[keyAlloc]; + wasm.realloc = function f(m,n){ + const m2 = f.impl(wasm.ptr.coerce(m)/*tag:64bit*/,n); + return n ? (m2 || WasmAllocError.toss("Failed to reallocate",n," bytes.")) : wasm.ptr.null; + }; + wasm.realloc.impl = wasm.exports[keyRealloc]; + wasm.dealloc = function f(m){ + f.impl(wasm.ptr.coerce(m)/*tag:64bit*/); + }; + wasm.dealloc.impl = wasm.exports[keyDealloc]; + } + + /** + Reports info about compile-time options using + sqlite3_compileoption_get() and sqlite3_compileoption_used(). It + has several distinct uses: + + If optName is an array then it is expected to be a list of + compilation options and this function returns an object + which maps each such option to true or false, indicating + whether or not the given option was included in this + build. That object is returned. + + If optName is an object, its keys are expected to be compilation + options and this function sets each entry to true or false, + indicating whether the compilation option was used or not. That + object is returned. + + If passed no arguments then it returns an object mapping + all known compilation options to their compile-time values, + or boolean true if they are defined with no value. This + result, which is relatively expensive to compute, is cached + and returned for future no-argument calls. + + In all other cases it returns true if the given option was + active when when compiling the sqlite3 module, else false. + + Compile-time option names may optionally include their + "SQLITE_" prefix. When it returns an object of all options, + the prefix is elided. + */ + wasm.compileOptionUsed = function f(optName){ + if(!arguments.length){ + if(f._result) return f._result; + else if(!f._opt){ + f._rx = /^([^=]+)=(.+)/; + f._rxInt = /^-?\d+$/; + f._opt = function(opt, rv){ + const m = f._rx.exec(opt); + rv[0] = (m ? m[1] : opt); + rv[1] = m ? (f._rxInt.test(m[2]) ? +m[2] : m[2]) : true; + }; + } + const rc = Object.create(null), ov = [0,0]; + let i = 0, k; + while((k = capi.sqlite3_compileoption_get(i++))){ + f._opt(k,ov); + rc[ov[0]] = ov[1]; + } + return f._result = rc; + }else if(Array.isArray(optName)){ + const rc = Object.create(null); + optName.forEach((v)=>{ + rc[v] = capi.sqlite3_compileoption_used(v); + }); + return rc; + }else if('object' === typeof optName){ + Object.keys(optName).forEach((k)=> { + optName[k] = capi.sqlite3_compileoption_used(k); + }); + return optName; + } + return ( + 'string'===typeof optName + ) ? !!capi.sqlite3_compileoption_used(optName) : false; + }/*compileOptionUsed()*/; + + /** + sqlite3.wasm.pstack (pseudo-stack) holds a special-case intended + solely for short-lived, small data. In practice, it's primarily + used to allocate output pointers. It mus not be used for any + memory which needs to outlive the scope in which it's obtained + from pstack. + + The library guarantees only that a minimum of 2kb are available + in this allocator, and it may provide more (it's a build-time + value). pstack.quota and pstack.remaining can be used to get the + total resp. remaining amount of memory. + + It has only a single intended usage: + + ``` + const stackPos = pstack.pointer; + try{ + const ptr = pstack.alloc(8); + // ==> pstack.pointer === ptr + const otherPtr = pstack.alloc(8); + // ==> pstack.pointer === otherPtr + ... + }finally{ + pstack.restore(stackPos); + // ==> pstack.pointer === stackPos + } + ``` + + This allocator is much faster than a general-purpose one but is + limited to usage patterns like the one shown above. + + It operates from a static range of memory which lives outside of + space managed by Emscripten's stack-management, so does not + collide with Emscripten-provided stack allocation APIs. The + memory lives in the WASM heap and can be used with routines such + as wasm.poke() and wasm.heap8u().slice(). + */ + wasm.pstack = Object.assign(Object.create(null),{ + /** + Sets the current pstack position to the given pointer. Results + are undefined if the passed-in value did not come from + this.pointer. + + In debug builds this may trigger an assert() in the WASM + environment if passed an illegal value. + */ + restore: wasm.exports.sqlite3__wasm_pstack_restore, + + /** + Attempts to allocate the given number of bytes from the + pstack. On success, it zeroes out a block of memory of the + given size, adjusts the pstack pointer, and returns a pointer + to the memory. On error, throws a WasmAllocError. The + memory must eventually be released using restore(). + + If n is a string, it must be a WASM "IR" value in the set + accepted by wasm.sizeofIR(), which is mapped to the size of + that data type. If passed a string not in that set, it throws a + WasmAllocError. + + This method always adjusts the given value to be a multiple + of 8 bytes because failing to do so can lead to incorrect + results when reading and writing 64-bit values from/to the WASM + heap. Similarly, the returned address is always 8-byte aligned. + */ + alloc: function(n){ + if('string'===typeof n && !(n = wasm.sizeofIR(n))){ + WasmAllocError.toss("Invalid value for pstack.alloc(",arguments[0],")"); + } + return wasm.exports.sqlite3__wasm_pstack_alloc(n) + || WasmAllocError.toss("Could not allocate",n, + "bytes from the pstack."); + }, + + /** + alloc()'s n chunks, each sz bytes, as a single memory block and + returns the addresses as an array of n element, each holding + the address of one chunk. + + sz may optionally be an IR string accepted by wasm.sizeofIR(). + + Throws a WasmAllocError if allocation fails. + + Example: + + ``` + const [p1, p2, p3] = wasm.pstack.allocChunks(3, wasm.ptr.size); + ``` + */ + allocChunks: function(n,sz){ + if('string'===typeof sz && !(sz = wasm.sizeofIR(sz))){ + WasmAllocError.toss("Invalid size value for allocChunks(",arguments[1],")"); + } + const mem = wasm.pstack.alloc(n * sz); + const rc = [mem]; + let i = 1, offset = sz; + for(; i < n; ++i, offset += sz) rc.push(wasm.ptr.add(mem, offset)); + return rc; + }, + + /** + A convenience wrapper for allocChunks() which sizes each chunk + as either 8 bytes (safePtrSize is truthy) or wasm.ptr.size (if + safePtrSize is falsy). + + How it returns its result differs depending on its first + argument: if it's 1, it returns a single pointer value. If it's + more than 1, it returns the same as allocChunks(). + + When a returned pointers will refer to a 64-bit value, e.g. a + double or int64, and that value must be written or fetched, + e.g. using wasm.poke() or wasm.peek(), it is + important that the pointer in question be aligned to an 8-byte + boundary or else it will not be fetched or written properly and + will corrupt or read neighboring memory. + + However, when all pointers involved point to "small" data, it + is safe to pass a falsy value to save a tiny bit of memory. + */ + allocPtr: (n=1,safePtrSize=true)=>{ + return 1===n + ? wasm.pstack.alloc(safePtrSize ? 8 : wasm.ptr.size) + : wasm.pstack.allocChunks(n, safePtrSize ? 8 : wasm.ptr.size); + }, + + /** + Records the current pstack position, calls the given function, + passing it the sqlite3 object, then restores the pstack + regardless of whether the function throws. Returns the result + of the call or propagates an exception on error. + + Added in 3.44. + */ + call: function(f){ + const stackPos = wasm.pstack.pointer; + try{ return f(sqlite3) } + finally{ wasm.pstack.restore(stackPos); } + } + + })/*wasm.pstack*/; + + Object.defineProperties(wasm.pstack, { + /** + Resolves to the current pstack position pointer either as a + Number (32-bit WASM) or BigInt (64-bit WASM). This value is + intended _only_ to be saved for passing to restore(). Writing + to this memory, without first reserving it via + wasm.pstack.alloc() and friends, leads to undefined results. + */ + pointer: { + configurable: false, iterable: true, writeable: false, + get: wasm.exports.sqlite3__wasm_pstack_ptr + //Whether or not a setter as an alternative to restore() is + //clearer or would just lead to confusion or misuse is unclear. + //set: wasm.exports.sqlite3__wasm_pstack_restore + }, + + /** + Resolves to the total number of bytes available in the pstack + allocator, including any space which is currently + allocated. This value is a compile-time constant. + */ + quota: { + configurable: false, iterable: true, writeable: false, + get: wasm.exports.sqlite3__wasm_pstack_quota + }, + + /** + Resolves to the number of bytes remaining in the pstack + allocator. + */ + remaining: { + configurable: false, iterable: true, writeable: false, + get: wasm.exports.sqlite3__wasm_pstack_remaining + } + })/*wasm.pstack properties*/; + + capi.sqlite3_randomness = (...args)=>{ + if(1===args.length + && util.isTypedArray(args[0]) + && 1===args[0].BYTES_PER_ELEMENT){ + const ta = args[0]; + if(0===ta.byteLength){ + wasm.exports.sqlite3_randomness(0,wasm.ptr.null); + return ta; + } + const stack = wasm.pstack.pointer; + try { + let n = ta.byteLength, offset = 0; + const r = wasm.exports.sqlite3_randomness; + const heap = wasm.heap8u(); + const nAlloc = n < 512 ? n : 512; + const ptr = wasm.pstack.alloc(nAlloc); + do{ + const j = (n>nAlloc ? nAlloc : n); + r(j, ptr); + ta.set(wasm.typedArrayPart(heap, ptr, wasm.ptr.add(ptr,j)), offset); + n -= j; + offset += j; + } while(n > 0); + }catch(e){ + config.error("Highly unexpected (and ignored!) "+ + "exception in sqlite3_randomness():",e); + }finally{ + wasm.pstack.restore(stack); + } + return ta; + } + wasm.exports.sqlite3_randomness(...args); + }; + + /** State for sqlite3_wasmfs_opfs_dir(). */ + let __wasmfsOpfsDir = undefined; + /** + If the wasm environment has a WASMFS/OPFS-backed persistent + storage directory, its path is returned by this function. If it + does not then it returns "" (noting that "" is a falsy value). + + The first time this is called, this function inspects the current + environment to determine whether WASMFS persistence support is + available and, if it is, enables it (if needed). After the first + call it always returns the cached result. + + If the returned string is not empty, any files stored under the + returned path (recursively) are housed in OPFS storage. If the + returned string is empty, this particular persistent storage + option is not available on the client. + + Though the mount point name returned by this function is intended + to remain stable, clients should not hard-coded it anywhere. + Always call this function to get the path. + + This function is a no-op in most builds of this library, as the + WASMFS capability requires a custom build. + */ + capi.sqlite3_wasmfs_opfs_dir = function(){ + if(undefined !== __wasmfsOpfsDir) return __wasmfsOpfsDir; + // If we have no OPFS, there is no persistent dir + const pdir = config.wasmfsOpfsDir; + if(!pdir + || !globalThis.FileSystemHandle + || !globalThis.FileSystemDirectoryHandle + || !globalThis.FileSystemFileHandle + || !wasm.exports.sqlite3__wasm_init_wasmfs){ + return __wasmfsOpfsDir = ""; + } + try{ + if(pdir && 0===wasm.xCallWrapped( + 'sqlite3__wasm_init_wasmfs', 'i32', ['string'], pdir + )){ + return __wasmfsOpfsDir = pdir; + }else{ + return __wasmfsOpfsDir = ""; + } + }catch(e){ + // sqlite3__wasm_init_wasmfs() is not available + return __wasmfsOpfsDir = ""; + } + }; + + /** + Returns true if sqlite3.capi.sqlite3_wasmfs_opfs_dir() is a + non-empty string and the given name starts with (that string + + '/'), else returns false. + */ + capi.sqlite3_wasmfs_filename_is_persistent = function(name){ + const p = capi.sqlite3_wasmfs_opfs_dir(); + return (p && name) ? name.startsWith(p+'/') : false; + }; + + /** + Given an `sqlite3*`, an sqlite3_vfs name, and an optional db name + (defaulting to "main"), returns a truthy value (see below) if + that db uses that VFS, else returns false. If pDb is falsy then + the 3rd argument is ignored and this function returns a truthy + value if the default VFS name matches that of the 2nd argument. + Results are undefined if pDb is truthy but refers to an invalid + pointer. The 3rd argument specifies the database name of the + given database connection to check, defaulting to the main db. + + The 2nd and 3rd arguments may either be a JS string or a WASM + C-string. If the 2nd argument is a NULL WASM pointer, the default + VFS is assumed. If the 3rd is a NULL WASM pointer, "main" is + assumed. + + The truthy value it returns is a pointer to the `sqlite3_vfs` + object. + + To permit safe use of this function from APIs which may be called + via C (like SQL UDFs), this function does not throw: if bad + arguments cause a conversion error when passing into wasm-space, + false is returned. + */ + capi.sqlite3_js_db_uses_vfs = function(pDb,vfsName,dbName=0){ + try{ + const pK = capi.sqlite3_vfs_find(vfsName); + if(!pK) return false; + else if(!pDb){ + return pK===capi.sqlite3_vfs_find(0) ? pK : false; + }else{ + return pK===capi.sqlite3_js_db_vfs(pDb,dbName) ? pK : false; + } + }catch(e){ + /* Ignore - probably bad args to a wasm-bound function. */ + return false; + } + }; + + /** + Returns an array of the names of all currently-registered sqlite3 + VFSes. + */ + capi.sqlite3_js_vfs_list = function(){ + const rc = []; + let pVfs = capi.sqlite3_vfs_find(wasm.ptr.coerce(0)); + while(pVfs){ + const oVfs = new capi.sqlite3_vfs(pVfs); + rc.push(wasm.cstrToJs(oVfs.$zName)); + pVfs = oVfs.$pNext; + oVfs.dispose(); + } + return rc; + }; + + /** + A convenience wrapper around sqlite3_serialize() which serializes + the given `sqlite3*` pointer to a Uint8Array. The first argument + may be either an `sqlite3*` or an sqlite3.oo1.DB instance. + + On success it returns a Uint8Array. If the schema is empty, an + empty array is returned. + + `schema` is the schema to serialize. It may be a WASM C-string + pointer or a JS string. If it is falsy, it defaults to `"main"`. + + On error it throws with a description of the problem. + */ + capi.sqlite3_js_db_export = function(pDb, schema=0){ + pDb = wasm.xWrap.testConvertArg('sqlite3*', pDb); + if(!pDb) toss3('Invalid sqlite3* argument.'); + if(!wasm.bigIntEnabled) toss3('BigInt support is not enabled.'); + const scope = wasm.scopedAllocPush(); + let pOut; + try{ + const pSize = wasm.scopedAlloc(8/*i64*/ + wasm.ptr.size); + const ppOut = wasm.ptr.add(pSize, 8); + /** + Maintenance reminder, since this cost a full hour of grief + and confusion: if the order of pSize/ppOut are reversed in + that memory block, fetching the value of pSize after the + export reads a garbage size because it's not on an 8-byte + memory boundary! + */ + const zSchema = schema + ? (wasm.isPtr(schema) ? schema : wasm.scopedAllocCString(''+schema)) + : wasm.ptr.null; + let rc = wasm.exports.sqlite3__wasm_db_serialize( + pDb, zSchema, ppOut, pSize, 0 + ); + if(rc){ + toss3("Database serialization failed with code", + sqlite3.capi.sqlite3_js_rc_str(rc)); + } + pOut = wasm.peekPtr(ppOut); + const nOut = wasm.peek(pSize, 'i64'); + rc = nOut + ? wasm.heap8u().slice(Number(pOut), Number(pOut) + Number(nOut)) + : new Uint8Array(); + return rc; + }finally{ + if(pOut) wasm.exports.sqlite3_free(pOut); + wasm.scopedAllocPop(scope); + } + }; + + /** + Given a `sqlite3*` and a database name (JS string or WASM + C-string pointer, which may be 0), returns a pointer to the + sqlite3_vfs responsible for it. If the given db name is null/0, + or not provided, then "main" is assumed. + */ + capi.sqlite3_js_db_vfs = + (dbPointer, dbName=0)=>util.sqlite3__wasm_db_vfs(dbPointer, dbName); + + /** + A thin wrapper around capi.sqlite3_aggregate_context() which + behaves the same except that it throws a WasmAllocError if that + function returns 0. As a special case, if n is falsy it does + _not_ throw if that function returns 0. That special case is + intended for use with xFinal() implementations. + */ + capi.sqlite3_js_aggregate_context = (pCtx, n)=>{ + return capi.sqlite3_aggregate_context(pCtx, n) + || (n ? WasmAllocError.toss("Cannot allocate",n, + "bytes for sqlite3_aggregate_context()") + : 0); + }; + + /** + If the current environment supports the POSIX file APIs, this routine + creates (or overwrites) the given file using those APIs. This is + primarily intended for use in Emscripten-based builds where the POSIX + APIs are transparently proxied by an in-memory virtual filesystem. + It may behave differently in other environments. + + The first argument must be either a JS string or WASM C-string + holding the filename. This routine does _not_ create intermediary + directories if the filename has a directory part. + + The 2nd argument may either a valid WASM memory pointer, an + ArrayBuffer, or a Uint8Array. The 3rd must be the length, in + bytes, of the data array to copy. If the 2nd argument is an + ArrayBuffer or Uint8Array and the 3rd is not a positive integer + then the 3rd defaults to the array's byteLength value. + + Results are undefined if data is a WASM pointer and dataLen is + exceeds data's bounds. + + Throws if any arguments are invalid or if creating or writing to + the file fails. + + Added in 3.43 as an alternative for the deprecated + sqlite3_js_vfs_create_file(). + */ + capi.sqlite3_js_posix_create_file = function(filename, data, dataLen){ + let pData; + if(data && wasm.isPtr(data)){ + pData = data; + }else if(data instanceof ArrayBuffer || data instanceof Uint8Array){ + pData = wasm.allocFromTypedArray(data); + if(arguments.length<3 || !util.isInt32(dataLen) || dataLen<0){ + dataLen = data.byteLength; + } + }else{ + SQLite3Error.toss("Invalid 2nd argument for sqlite3_js_posix_create_file()."); + } + try{ + if(!util.isInt32(dataLen) || dataLen<0){ + SQLite3Error.toss("Invalid 3rd argument for sqlite3_js_posix_create_file()."); + } + const rc = util.sqlite3__wasm_posix_create_file(filename, pData, dataLen); + if(rc) SQLite3Error.toss("Creation of file failed with sqlite3 result code", + capi.sqlite3_js_rc_str(rc)); + }finally{ + if( pData && pData!==data ) wasm.dealloc(pData); + } + }; + + /** + Deprecation warning: this function does not work properly in + debug builds of sqlite3 because its out-of-scope use of the + sqlite3_vfs API triggers assertions in the core library. That + was unfortunately not discovered until 2023-08-11. This function + is now deprecated. It should not be used in new code and should + be removed from existing code. + + Alternative options: + + - The "unix" VFS and its variants can get equivalent + functionality with sqlite3_js_posix_create_file(). + + - OPFS: use either sqlite3.oo1.OpfsDb.importDb(), for the "opfs" + VFS, or the importDb() method of the PoolUtil object provided + by the "opfs-sahpool" OPFS (noting that its VFS name may differ + depending on client-side configuration). We cannot proxy those + from here because the former is necessarily asynchronous and + the latter requires information not available to this function. + + Historical (deprecated) behaviour: + + Creates a file using the storage appropriate for the given + sqlite3_vfs. The first argument may be a VFS name (JS string + only, NOT a WASM C-string), WASM-managed `sqlite3_vfs*`, or + a capi.sqlite3_vfs instance. Pass 0 (a NULL pointer) to use the + default VFS. If passed a string which does not resolve using + sqlite3_vfs_find(), an exception is thrown. (Note that a WASM + C-string is not accepted because it is impossible to + distinguish from a C-level `sqlite3_vfs*`.) + + The second argument, the filename, must be a JS or WASM C-string. + + The 3rd may either be falsy, a valid WASM memory pointer, an + ArrayBuffer, or a Uint8Array. The 4th must be the length, in + bytes, of the data array to copy. If the 3rd argument is an + ArrayBuffer or Uint8Array and the 4th is not a positive integer + then the 4th defaults to the array's byteLength value. + + If data is falsy then a file is created with dataLen bytes filled + with uninitialized data (whatever truncate() leaves there). If + data is not falsy then a file is created or truncated and it is + filled with the first dataLen bytes of the data source. + + Throws if any arguments are invalid or if creating or writing to + the file fails. + + Note that most VFSes do _not_ automatically create directory + parts of filenames, nor do all VFSes have a concept of + directories. If the given filename is not valid for the given + VFS, an exception will be thrown. This function exists primarily + to assist in implementing file-upload capability, with the caveat + that clients must have some idea of the VFS into which they want + to upload and that VFS must support the operation. + + VFS-specific notes: + + - "memdb": results are undefined. + + - "kvvfs": will fail with an I/O error due to strict internal + requirements of that VFS's xTruncate(). + + - "unix" and related: will use the WASM build's equivalent of the + POSIX I/O APIs. This will work so long as neither a specific + VFS nor the WASM environment imposes requirements which break + it. (Much later: it turns out that debug builds of the library + impose such requirements, in that they assert() that dataLen is + an even multiple of a valid db page size.) + + - "opfs": uses OPFS storage and creates directory parts of the + filename. It can only be used to import an SQLite3 database + file and will fail if given anything else. + */ + capi.sqlite3_js_vfs_create_file = function(vfs, filename, data, dataLen){ + config.warn("sqlite3_js_vfs_create_file() is deprecated and", + "should be avoided because it can lead to C-level crashes.", + "See its documentation for alternatives."); + let pData; + if(data){ + if( wasm.isPtr(data) ){ + pData = data; + }else{ + if( data instanceof ArrayBuffer ){ + data = new Uint8Array(data); + } + if( data instanceof Uint8Array ){ + pData = wasm.allocFromTypedArray(data); + if(arguments.length<4 || !util.isInt32(dataLen) || dataLen<0){ + dataLen = data.byteLength; + } + }else{ + SQLite3Error.toss("Invalid 3rd argument type for sqlite3_js_vfs_create_file()."); + } + } + }else{ + pData = 0; + } + if(!util.isInt32(dataLen) || dataLen<0){ + if( pData && pData!==data ) wasm.dealloc(pData); + SQLite3Error.toss("Invalid 4th argument for sqlite3_js_vfs_create_file()."); + } + try{ + const rc = util.sqlite3__wasm_vfs_create_file(vfs, filename, pData, dataLen); + if(rc) SQLite3Error.toss("Creation of file failed with sqlite3 result code", + capi.sqlite3_js_rc_str(rc)); + }finally{ + if( pData && pData!==data ) wasm.dealloc(pData); + } + }; + + /** + Converts SQL input from a variety of convenient formats + to plain strings. + + If v is a string, it is returned as-is. If it is-a Array, its + join("") result is returned. If is is a Uint8Array, Int8Array, + or ArrayBuffer, it is assumed to hold UTF-8-encoded text and is + decoded to a string. If it looks like a WASM pointer, + wasm.cstrToJs(sql) is returned. Else undefined is returned. + + Added in 3.44 + */ + capi.sqlite3_js_sql_to_string = (sql)=>{ + if('string' === typeof sql){ + return sql; + } + const x = flexibleString(v); + return x===v ? undefined : x; + } + + if( util.isUIThread() ){ + /* Features specific to the main window thread... */ + + /** + Internal helper for sqlite3_js_kvvfs_clear() and friends. + Its argument should be one of ('local','session',""). + */ + const __kvvfsInfo = function(which){ + const rc = Object.create(null); + rc.prefix = 'kvvfs-'+which; + rc.stores = []; + if('session'===which || ""===which) rc.stores.push(globalThis.sessionStorage); + if('local'===which || ""===which) rc.stores.push(globalThis.localStorage); + return rc; + }; + + /** + Clears all storage used by the kvvfs DB backend, deleting any + DB(s) stored there. Its argument must be either 'session', + 'local', or "". In the first two cases, only sessionStorage + resp. localStorage is cleared. If it's an empty string (the + default) then both are cleared. Only storage keys which match + the pattern used by kvvfs are cleared: any other client-side + data are retained. + + This function is only available in the main window thread. + + Returns the number of entries cleared. + */ + capi.sqlite3_js_kvvfs_clear = function(which=""){ + let rc = 0; + const kvinfo = __kvvfsInfo(which); + kvinfo.stores.forEach((s)=>{ + const toRm = [] /* keys to remove */; + let i; + for( i = 0; i < s.length; ++i ){ + const k = s.key(i); + if(k.startsWith(kvinfo.prefix)) toRm.push(k); + } + toRm.forEach((kk)=>s.removeItem(kk)); + rc += toRm.length; + }); + return rc; + }; + + /** + This routine guesses the approximate amount of + window.localStorage and/or window.sessionStorage in use by the + kvvfs database backend. Its argument must be one of + ('session', 'local', ""). In the first two cases, only + sessionStorage resp. localStorage is counted. If it's an empty + string (the default) then both are counted. Only storage keys + which match the pattern used by kvvfs are counted. The returned + value is the "length" value of every matching key and value, + noting that JavaScript stores each character in 2 bytes. + + Note that the returned size is not authoritative from the + perspective of how much data can fit into localStorage and + sessionStorage, as the precise algorithms for determining + those limits are unspecified and may include per-entry + overhead invisible to clients. + */ + capi.sqlite3_js_kvvfs_size = function(which=""){ + let sz = 0; + const kvinfo = __kvvfsInfo(which); + kvinfo.stores.forEach((s)=>{ + let i; + for(i = 0; i < s.length; ++i){ + const k = s.key(i); + if(k.startsWith(kvinfo.prefix)){ + sz += k.length; + sz += s.getItem(k).length; + } + } + }); + return sz * 2 /* because JS uses 2-byte char encoding */; + }; + + }/* main-window-only bits */ + + /** + Wraps all known variants of the C-side variadic + sqlite3_db_config(). + + Full docs: https://sqlite.org/c3ref/db_config.html + + Returns capi.SQLITE_MISUSE if op is not a valid operation ID. + + The variants which take `(int, int*)` arguments treat a + missing or falsy pointer argument as 0. + */ + capi.sqlite3_db_config = function(pDb, op, ...args){ + switch(op){ + case capi.SQLITE_DBCONFIG_ENABLE_FKEY: + case capi.SQLITE_DBCONFIG_ENABLE_TRIGGER: + case capi.SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER: + case capi.SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION: + case capi.SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE: + case capi.SQLITE_DBCONFIG_ENABLE_QPSG: + case capi.SQLITE_DBCONFIG_TRIGGER_EQP: + case capi.SQLITE_DBCONFIG_RESET_DATABASE: + case capi.SQLITE_DBCONFIG_DEFENSIVE: + case capi.SQLITE_DBCONFIG_WRITABLE_SCHEMA: + case capi.SQLITE_DBCONFIG_LEGACY_ALTER_TABLE: + case capi.SQLITE_DBCONFIG_DQS_DML: + case capi.SQLITE_DBCONFIG_DQS_DDL: + case capi.SQLITE_DBCONFIG_ENABLE_VIEW: + case capi.SQLITE_DBCONFIG_LEGACY_FILE_FORMAT: + case capi.SQLITE_DBCONFIG_TRUSTED_SCHEMA: + case capi.SQLITE_DBCONFIG_STMT_SCANSTATUS: + case capi.SQLITE_DBCONFIG_REVERSE_SCANORDER: + case capi.SQLITE_DBCONFIG_ENABLE_ATTACH_CREATE: + case capi.SQLITE_DBCONFIG_ENABLE_ATTACH_WRITE: + case capi.SQLITE_DBCONFIG_ENABLE_COMMENTS: + if( !this.ip ){ + this.ip = wasm.xWrap('sqlite3__wasm_db_config_ip','int', + ['sqlite3*', 'int', 'int', '*']); + } + return this.ip(pDb, op, args[0], args[1] || 0); + case capi.SQLITE_DBCONFIG_LOOKASIDE: + if( !this.pii ){ + this.pii = wasm.xWrap('sqlite3__wasm_db_config_pii', 'int', + ['sqlite3*', 'int', '*', 'int', 'int']); + } + return this.pii(pDb, op, args[0], args[1], args[2]); + case capi.SQLITE_DBCONFIG_MAINDBNAME: + if(!this.s){ + this.s = wasm.xWrap('sqlite3__wasm_db_config_s','int', + ['sqlite3*', 'int', 'string:static'] + /* MAINDBNAME requires a static string */); + } + return this.s(pDb, op, args[0]); + default: + return capi.SQLITE_MISUSE; + } + }.bind(Object.create(null)); + + /** + Given a (sqlite3_value*), this function attempts to convert it + to an equivalent JS value with as much fidelity as feasible and + return it. + + By default it throws if it cannot determine any sensible + conversion. If passed a falsy second argument, it instead returns + `undefined` if no suitable conversion is found. Note that there + is no conversion from SQL to JS which results in the `undefined` + value, so `undefined` has an unambiguous meaning here. It will + always throw a WasmAllocError if allocating memory for a + conversion fails. + + Caveats: + + - It does not support sqlite3_value_to_pointer() conversions + because those require a type name string which this function + does not have and cannot sensibly be given at the level of the + API where this is used (e.g. automatically converting UDF + arguments). Clients using sqlite3_value_to_pointer(), and its + related APIs, will need to manage those themselves. + */ + capi.sqlite3_value_to_js = function(pVal,throwIfCannotConvert=true){ + let arg; + const valType = capi.sqlite3_value_type(pVal); + switch(valType){ + case capi.SQLITE_INTEGER: + if(wasm.bigIntEnabled){ + arg = capi.sqlite3_value_int64(pVal); + if(util.bigIntFitsDouble(arg)) arg = Number(arg); + } + else arg = capi.sqlite3_value_double(pVal)/*yes, double, for larger integers*/; + break; + case capi.SQLITE_FLOAT: + arg = capi.sqlite3_value_double(pVal); + break; + case capi.SQLITE_TEXT: + arg = capi.sqlite3_value_text(pVal); + break; + case capi.SQLITE_BLOB:{ + const n = capi.sqlite3_value_bytes(pVal); + const pBlob = capi.sqlite3_value_blob(pVal); + if(n && !pBlob) sqlite3.WasmAllocError.toss( + "Cannot allocate memory for blob argument of",n,"byte(s)" + ); + arg = n + ? wasm.heap8u().slice(Number(pBlob), Number(pBlob) + Number(n)) + : null; + break; + } + case capi.SQLITE_NULL: + arg = null; break; + default: + if(throwIfCannotConvert){ + toss3(capi.SQLITE_MISMATCH, + "Unhandled sqlite3_value_type():",valType); + } + arg = undefined; + } + return arg; + }; + + /** + Requires a C-style array of `sqlite3_value*` objects and the + number of entries in that array. Returns a JS array containing + the results of passing each C array entry to + sqlite3_value_to_js(). The 3rd argument to this function is + passed on as the 2nd argument to that one. + */ + capi.sqlite3_values_to_js = function(argc,pArgv,throwIfCannotConvert=true){ + let i; + const tgt = []; + for(i = 0; i < argc; ++i){ + /** + Curiously: despite ostensibly requiring 8-byte + alignment, the pArgv array is parcelled into chunks of + 4 bytes (1 pointer each). The values those point to + have 8-byte alignment but the individual argv entries + do not. + */ + tgt.push(capi.sqlite3_value_to_js( + wasm.peekPtr(wasm.ptr.add(pArgv, wasm.ptr.size * i)), + throwIfCannotConvert + )); + } + return tgt; + }; + + /** + Calls either sqlite3_result_error_nomem(), if e is-a + WasmAllocError, or sqlite3_result_error(). In the latter case, + the second argument is coerced to a string to create the error + message. + + The first argument is a (sqlite3_context*). Returns void. + Does not throw. + */ + capi.sqlite3_result_error_js = function(pCtx,e){ + if(e instanceof WasmAllocError){ + capi.sqlite3_result_error_nomem(pCtx); + }else{ + /* Maintenance reminder: ''+e, rather than e.message, + will prefix e.message with e.name, so it includes + the exception's type name in the result. */; + capi.sqlite3_result_error(pCtx, ''+e, -1); + } + }; + + /** + This function passes its 2nd argument to one of the + sqlite3_result_xyz() routines, depending on the type of that + argument: + + - If (val instanceof Error), this function passes it to + sqlite3_result_error_js(). + - `null`: `sqlite3_result_null()` + - `boolean`: `sqlite3_result_int()` with a value of 0 or 1. + - `number`: `sqlite3_result_int()`, `sqlite3_result_int64()`, or + `sqlite3_result_double()`, depending on the range of the number + and whether or not int64 support is enabled. + - `bigint`: similar to `number` but will trigger an error if the + value is too big to store in an int64. + - `string`: `sqlite3_result_text()` + - Uint8Array or Int8Array or ArrayBuffer: `sqlite3_result_blob()` + - `undefined`: is a no-op provided to simplify certain use cases. + + Anything else triggers `sqlite3_result_error()` with a + description of the problem. + + The first argument to this function is a `(sqlite3_context*)`. + Returns void. Does not throw. + */ + capi.sqlite3_result_js = function(pCtx,val){ + if(val instanceof Error){ + capi.sqlite3_result_error_js(pCtx, val); + return; + } + try{ + switch(typeof val) { + case 'undefined': + /* This is a no-op. This routine originated in the create_function() + family of APIs and in that context, passing in undefined indicated + that the caller was responsible for calling sqlite3_result_xxx() + (if needed). */ + break; + case 'boolean': + capi.sqlite3_result_int(pCtx, val ? 1 : 0); + break; + case 'bigint': + if(util.bigIntFits32(val)){ + capi.sqlite3_result_int(pCtx, Number(val)); + }else if(util.bigIntFitsDouble(val)){ + capi.sqlite3_result_double(pCtx, Number(val)); + }else if(wasm.bigIntEnabled){ + if(util.bigIntFits64(val)) capi.sqlite3_result_int64(pCtx, val); + else toss3("BigInt value",val.toString(),"is too BigInt for int64."); + }else{ + toss3("BigInt value",val.toString(),"is too BigInt."); + } + break; + case 'number': { + let f; + if(util.isInt32(val)){ + f = capi.sqlite3_result_int; + }else if(wasm.bigIntEnabled + && Number.isInteger(val) + && util.bigIntFits64(BigInt(val))){ + f = capi.sqlite3_result_int64; + }else{ + f = capi.sqlite3_result_double; + } + f(pCtx, val); + break; + } + case 'string': { + const [p, n] = wasm.allocCString(val,true); + capi.sqlite3_result_text(pCtx, p, n, capi.SQLITE_WASM_DEALLOC); + break; + } + case 'object': + if(null===val/*yes, typeof null === 'object'*/) { + capi.sqlite3_result_null(pCtx); + break; + }else if(util.isBindableTypedArray(val)){ + const pBlob = wasm.allocFromTypedArray(val); + capi.sqlite3_result_blob( + pCtx, pBlob, val.byteLength, + capi.SQLITE_WASM_DEALLOC + ); + break; + } + // else fall through + default: + toss3("Don't not how to handle this UDF result value:",(typeof val), val); + } + }catch(e){ + capi.sqlite3_result_error_js(pCtx, e); + } + }; + + /** + Returns the result sqlite3_column_value(pStmt,iCol) passed to + sqlite3_value_to_js(). The 3rd argument of this function is + ignored by this function except to pass it on as the second + argument of sqlite3_value_to_js(). If the sqlite3_column_value() + returns NULL (e.g. because the column index is out of range), + this function returns `undefined`, regardless of the 3rd + argument. If the 3rd argument is falsy and conversion fails, + `undefined` will be returned. + + Note that sqlite3_column_value() returns an "unprotected" value + object, but in a single-threaded environment (like this one) + there is no distinction between protected and unprotected values. + */ + capi.sqlite3_column_js = function(pStmt, iCol, throwIfCannotConvert=true){ + const v = capi.sqlite3_column_value(pStmt, iCol); + return (0===v) ? undefined : capi.sqlite3_value_to_js(v, throwIfCannotConvert); + }; + + /** + Internal impl of sqlite3_preupdate_new/old_js() and + sqlite3changeset_new/old_js(). + */ + const __newOldValue = function(pObj, iCol, impl){ + impl = capi[impl]; + if(!this.ptr) this.ptr = wasm.allocPtr(); + else wasm.pokePtr(this.ptr, 0); + const rc = impl(pObj, iCol, this.ptr); + if(rc) return SQLite3Error.toss(rc,arguments[2]+"() failed with code "+rc); + const pv = wasm.peekPtr(this.ptr); + return pv ? capi.sqlite3_value_to_js( pv, true ) : undefined; + }.bind(Object.create(null)); + + /** + A wrapper around sqlite3_preupdate_new() which fetches the + sqlite3_value at the given index and returns the result of + passing it to sqlite3_value_to_js(). Throws on error. + */ + capi.sqlite3_preupdate_new_js = + (pDb, iCol)=>__newOldValue(pDb, iCol, 'sqlite3_preupdate_new'); + + /** + The sqlite3_preupdate_old() counterpart of + sqlite3_preupdate_new_js(), with an identical interface. + */ + capi.sqlite3_preupdate_old_js = + (pDb, iCol)=>__newOldValue(pDb, iCol, 'sqlite3_preupdate_old'); + + /** + A wrapper around sqlite3changeset_new() which fetches the + sqlite3_value at the given index and returns the result of + passing it to sqlite3_value_to_js(). Throws on error. + + If sqlite3changeset_new() succeeds but has no value to report, + this function returns the undefined value, noting that undefined + is a valid conversion from an `sqlite3_value`, so is unambiguous. + */ + capi.sqlite3changeset_new_js = + (pChangesetIter, iCol) => __newOldValue(pChangesetIter, iCol, + 'sqlite3changeset_new'); + + /** + The sqlite3changeset_old() counterpart of + sqlite3changeset_new_js(), with an identical interface. + */ + capi.sqlite3changeset_old_js = + (pChangesetIter, iCol)=>__newOldValue(pChangesetIter, iCol, + 'sqlite3changeset_old'); + + /* The remainder of the API will be set up in later steps. */ + const sqlite3 = { + WasmAllocError: WasmAllocError, + SQLite3Error: SQLite3Error, + capi, + util /* internal: will get removed after library init */, + wasm, + config, + /** + Holds the version info of the sqlite3 source tree from which + the generated sqlite3-api.js gets built. Note that its version + may well differ from that reported by sqlite3_libversion(), but + that should be considered a source file mismatch, as the JS and + WASM files are intended to be built and distributed together. + + This object is initially a placeholder which gets replaced by a + build-generated object. + */ + version: Object.create(null), + + /** + The library reserves the 'client' property for client-side use + and promises to never define a property with this name nor to + ever rely on specific contents of it. It makes no such guarantees + for other properties. + */ + client: undefined, + + /** + This function is not part of the public interface, but a + piece of internal bootstrapping infrastructure. + + Performs any optional asynchronous library-level initialization + which might be required. This function returns a Promise which + resolves to the sqlite3 namespace object. Any error in the + async init will be fatal to the init as a whole, but init + routines are themselves welcome to install dummy catch() + handlers which are not fatal if their failure should be + considered non-fatal. If called more than once, the second and + subsequent calls are no-ops which return a pre-resolved + Promise. + + Ideally this function is called as part of the Promise chain + which handles the loading and bootstrapping of the API. If not + then it must be called by client-level code, which must not use + the library until the returned Promise resolves. + + If called multiple times it will return the same Promise on + subsequent calls. The current build setup precludes that + possibility, so it's only a hypothetical problem if/when this + function ever needs to be invoked by clients. + + In Emscripten-based builds, this function is called + automatically and deleted from this object. + */ + asyncPostInit: async function ff(){ + if(ff.isReady instanceof Promise) return ff.isReady; + let lia = sqlite3ApiBootstrap.initializersAsync; + delete sqlite3ApiBootstrap.initializersAsync; + const postInit = async ()=>{ + if(!sqlite3.__isUnderTest){ + /* Delete references to internal-only APIs which are used by + some initializers. Retain them when running in test mode + so that we can add tests for them. */ + delete sqlite3.util; + /* It's conceivable that we might want to expose + StructBinder to client-side code, but it's only useful if + clients build their own sqlite3.wasm which contains their + own C struct types. */ + delete sqlite3.StructBinder; + } + return sqlite3; + }; + const catcher = (e)=>{ + config.error("an async sqlite3 initializer failed:",e); + throw e; + }; + if(!lia || !lia.length){ + return ff.isReady = postInit().catch(catcher); + } + lia = lia.map((f)=>{ + return (f instanceof Function) ? async x=>f(sqlite3) : f; + }); + lia.push(postInit); + let p = Promise.resolve(sqlite3); + while(lia.length) p = p.then(lia.shift()); + return ff.isReady = p.catch(catcher); + }, + /** + scriptInfo ideally gets injected into this object by the + infrastructure which assembles the JS/WASM module. It contains + state which must be collected before sqlite3ApiBootstrap() can + be declared. It is not necessarily available to any + sqlite3ApiBootstrap.initializers but "should" be in place (if + it's added at all) by the time that + sqlite3ApiBootstrap.initializersAsync is processed. + + This state is not part of the public API, only intended for use + with the sqlite3 API bootstrapping and wasm-loading process. + */ + scriptInfo: undefined + }; + try{ + sqlite3ApiBootstrap.initializers.forEach((f)=>{ + f(sqlite3); + }); + }catch(e){ + /* If we don't report this here, it can get completely swallowed + up and disappear into the abyss of Promises and Workers. */ + console.error("sqlite3 bootstrap initializer threw:",e); + throw e; + } + delete sqlite3ApiBootstrap.initializers; + sqlite3ApiBootstrap.sqlite3 = sqlite3; + delete globalThis.sqlite3ApiBootstrap; + delete globalThis.sqlite3ApiConfig; + sqlite3InitScriptInfo.debugModule( + "sqlite3ApiBootstrap() complete", sqlite3 + ); + sqlite3.scriptInfo /* used by some async init code */ = + sqlite3InitScriptInfo /* from post-js-header.js */; + if( (sqlite3.__isUnderTest = sqlite3IsUnderTest /* from post-js-header.js */) ){ + sqlite3.config.emscripten = EmscriptenModule; + const iw = sqlite3InitScriptInfo.instantiateWasm; + if( iw ){ + /* Metadata injected by the custom Module.instantiateWasm() + in pre-js.c-pp.js. */ + sqlite3.wasm.module = iw.module; + sqlite3.wasm.instance = iw.instance; + sqlite3.wasm.imports = iw.imports; + } + } + return sqlite3.asyncPostInit().then((s)=>{ + sqlite3InitScriptInfo.debugModule( + "sqlite3.asyncPostInit() complete", sqlite3 + ); + delete s.asyncPostInit; + delete s.scriptInfo; + delete s.emscripten; + return s; + }); +}/*sqlite3ApiBootstrap()*/; + +/** + globalThis.sqlite3ApiBootstrap.initializers is an internal detail + used by the various pieces of the sqlite3 API's amalgamation + process. It must not be modified by client code except when plugging + such code into the amalgamation process. + + Each component of the amalgamation is expected to append a function + to this array. When sqlite3ApiBootstrap() is called for the first + time, each such function will be called (in their appended order) + and passed the sqlite3 namespace object, into which they can install + their features. At the end of that process, this array is deleted. + + The order of insertion into this array is significant for + some pieces. e.g. sqlite3.capi and sqlite3.wasm cannot be fully + utilized until the whwasmutil.js part is plugged in via + sqlite3-api-glue.js. +*/ +globalThis.sqlite3ApiBootstrap.initializers = []; + +/** + globalThis.sqlite3ApiBootstrap.initializersAsync is an internal detail + used by the sqlite3 API's amalgamation process. It must not be + modified by client code except when plugging such code into the + amalgamation process. + + The counterpart of globalThis.sqlite3ApiBootstrap.initializers, + specifically for initializers which are asynchronous. All entries in + this list must be either async functions, non-async functions which + return a Promise, or a Promise. Each function in the list is called + with the sqlite3 object as its only argument. + + The resolved value of any Promise is ignored and rejection will kill + the asyncPostInit() process (at an indeterminate point because all + of them are run asynchronously in parallel). + + This list is not processed until the client calls + sqlite3.asyncPostInit(). This means, for example, that intializers + added to globalThis.sqlite3ApiBootstrap.initializers may push entries to + this list. +*/ +globalThis.sqlite3ApiBootstrap.initializersAsync = []; + +/** + Client code may assign sqlite3ApiBootstrap.defaultConfig an + object-type value before calling sqlite3ApiBootstrap() (without + arguments) in order to tell that call to use this object as its + default config value. The intention of this is to provide + downstream clients with a reasonably flexible approach for plugging in + an environment-suitable configuration without having to define a new + global-scope symbol. +*/ +globalThis.sqlite3ApiBootstrap.defaultConfig = Object.create(null); + +/** + Placeholder: gets installed by the first call to + globalThis.sqlite3ApiBootstrap(). However, it is recommended that the + caller of sqlite3ApiBootstrap() capture its return value and delete + globalThis.sqlite3ApiBootstrap after calling it. It returns the same + value which will be stored here. +*/ +globalThis.sqlite3ApiBootstrap.sqlite3 = undefined; diff --git a/ext/wasm/api/sqlite3-api-worker1.c-pp.js b/ext/wasm/api/sqlite3-api-worker1.c-pp.js new file mode 100644 index 0000000000..25262abf85 --- /dev/null +++ b/ext/wasm/api/sqlite3-api-worker1.c-pp.js @@ -0,0 +1,680 @@ +//#if not omit-oo1 +/** + 2022-07-22 + + The author disclaims copyright to this source code. In place of a + legal notice, here is a blessing: + + * May you do good and not evil. + * May you find forgiveness for yourself and forgive others. + * May you share freely, never taking more than you give. + + *********************************************************************** + + This file implements the initializer for SQLite's "Worker API #1", a + very basic DB access API intended to be scripted from a main window + thread via Worker-style messages. Because of limitations in that + type of communication, this API is minimalistic and only capable of + serving relatively basic DB requests (e.g. it cannot process nested + query loops concurrently). + + This file requires that the core C-style sqlite3 API and OO API #1 + have been loaded. +*/ + +/** + sqlite3.initWorker1API() implements a Worker-based wrapper around + SQLite3 OO API #1, colloquially known as "Worker API #1". + + In order to permit this API to be loaded in worker threads without + automatically registering onmessage handlers, initializing the + worker API requires calling initWorker1API(). If this function is + called from a non-worker thread then it throws an exception. It + must only be called once per Worker. + + When initialized, it installs message listeners to receive Worker + messages and then it posts a message in the form: + + ``` + {type:'sqlite3-api', result:'worker1-ready'} + ``` + + to let the client know that it has been initialized. Clients may + optionally depend on this function not returning until + initialization is complete, as the initialization is synchronous. + In some contexts, however, listening for the above message is + a better fit. + + Note that the worker-based interface can be slightly quirky because + of its async nature. In particular, any number of messages may be posted + to the worker before it starts handling any of them. If, e.g., an + "open" operation fails, any subsequent messages will fail. The + Promise-based wrapper for this API (`sqlite3-worker1-promiser.js`) + is more comfortable to use in that regard. + + The documentation for the input and output worker messages for + this API follows... + + ==================================================================== + Common message format... + + Each message posted to the worker has an operation-independent + envelope and operation-dependent arguments: + + ``` + { + type: string, // one of: 'open', 'close', 'exec', 'export', 'config-get' + + messageId: OPTIONAL arbitrary value. The worker will copy it as-is + into response messages to assist in client-side dispatching. + + dbId: a db identifier string (returned by 'open') which tells the + operation which database instance to work on. If not provided, the + first-opened db is used. This is an "opaque" value, with no + inherently useful syntax or information. Its value is subject to + change with any given build of this API and cannot be used as a + basis for anything useful beyond its one intended purpose. + + args: ...operation-dependent arguments... + + // the framework may add other properties for testing or debugging + // purposes. + + } + ``` + + Response messages, posted back to the main thread, look like: + + ``` + { + type: string. Same as above except for error responses, which have the type + 'error', + + messageId: same value, if any, provided by the inbound message + + dbId: the id of the db which was operated on, if any, as returned + by the corresponding 'open' operation. + + result: ...operation-dependent result... + + } + ``` + + ==================================================================== + Error responses + + Errors are reported messages in an operation-independent format: + + ``` + { + type: "error", + + messageId: ...as above..., + + dbId: ...as above... + + result: { + + operation: type of the triggering operation: 'open', 'close', ... + + message: ...error message text... + + errorClass: string. The ErrorClass.name property from the thrown exception. + + input: the message object which triggered the error. + + stack: _if available_, a stack trace array. + + } + + } + ``` + + + ==================================================================== + "config-get" + + This operation fetches the serializable parts of the sqlite3 API + configuration. + + Message format: + + ``` + { + type: "config-get", + messageId: ...as above..., + args: currently ignored and may be elided. + } + ``` + + Response: + + ``` + { + type: "config-get", + messageId: ...as above..., + result: { + + version: sqlite3.version object + + bigIntEnabled: bool. True if BigInt support is enabled. + + vfsList: result of sqlite3.capi.sqlite3_js_vfs_list() + } + } + ``` + + + ==================================================================== + "open" a database + + Message format: + + ``` + { + type: "open", + messageId: ...as above..., + args:{ + + filename [=":memory:" or "" (unspecified)]: the db filename. + See the sqlite3.oo1.DB constructor for peculiarities and + transformations, + + vfs: sqlite3_vfs name. Ignored if filename is ":memory:" or "". + This may change how the given filename is resolved. + } + } + ``` + + Response: + + ``` + { + type: "open", + messageId: ...as above..., + result: { + filename: db filename, possibly differing from the input. + + dbId: an opaque ID value which must be passed in the message + envelope to other calls in this API to tell them which db to + use. If it is not provided to future calls, they will default to + operating on the least-recently-opened db. This property is, for + API consistency's sake, also part of the containing message + envelope. Only the `open` operation includes it in the `result` + property. + + persistent: true if the given filename resides in the + known-persistent storage, else false. + + vfs: name of the VFS the "main" db is using. + } + } + ``` + + ==================================================================== + "close" a database + + Message format: + + ``` + { + type: "close", + messageId: ...as above... + dbId: ...as above... + args: OPTIONAL {unlink: boolean} + } + ``` + + If the `dbId` does not refer to an opened ID, this is a no-op. If + the `args` object contains a truthy `unlink` value then the database + will be unlinked (deleted) after closing it. The inability to close a + db (because it's not opened) or delete its file does not trigger an + error. + + Response: + + ``` + { + type: "close", + messageId: ...as above..., + result: { + + filename: filename of closed db, or undefined if no db was closed + + } + } + ``` + + ==================================================================== + "exec" SQL + + All SQL execution is processed through the exec operation. It offers + most of the features of the oo1.DB.exec() method, with a few limitations + imposed by the state having to cross thread boundaries. + + Message format: + + ``` + { + type: "exec", + messageId: ...as above... + dbId: ...as above... + args: string (SQL) or {... see below ...} + } + ``` + + Response: + + ``` + { + type: "exec", + messageId: ...as above..., + dbId: ...as above... + result: { + input arguments, possibly modified. See below. + } + } + ``` + + The arguments are in the same form accepted by oo1.DB.exec(), with + the exceptions noted below. + + If `args.countChanges` (added in version 3.43) is truthy then the + `result` property contained by the returned object will have a + `changeCount` property which holds the number of changes made by the + provided SQL. Because the SQL may contain an arbitrary number of + statements, the `changeCount` is calculated by calling + `sqlite3_total_changes()` before and after the SQL is evaluated. If + the value of `countChanges` is 64 then the `changeCount` property + will be returned as a 64-bit integer in the form of a BigInt (noting + that that will trigger an exception if used in a BigInt-incapable + build). In the latter case, the number of changes is calculated by + calling `sqlite3_total_changes64()` before and after the SQL is + evaluated. + + If the `args.lastInsertRowId` (added in version 3.50.0) is truthy + then the `result` property contained by the returned object will + have a `lastInsertRowId` will hold a BigInt-type value corresponding + to the result of sqlite3_last_insert_rowid(). This value is only + fetched once, after the SQL is run, regardless of how many + statements the SQL contains. This API has no idea whether the SQL + contains any INSERTs, so it is up to the client to apply/rely on + this property only when it makes sense to do so. + + A function-type args.callback property cannot cross + the window/Worker boundary, so is not useful here. If + args.callback is a string then it is assumed to be a + message type key, in which case a callback function will be + applied which posts each row result via: + + postMessage({type: thatKeyType, + rowNumber: 1-based-#, + row: theRow, + columnNames: anArray + }) + + And, at the end of the result set (whether or not any result rows + were produced), it will post an identical message with + (row=undefined, rowNumber=null) to alert the caller than the result + set is completed. Note that a row value of `null` is a legal row + result for certain arg.rowMode values. + + (Design note: we don't use (row=undefined, rowNumber=undefined) to + indicate end-of-results because fetching those would be + indistinguishable from fetching from an empty object unless the + client used hasOwnProperty() (or similar) to distinguish "missing + property" from "property with the undefined value". Similarly, + `null` is a legal value for `row` in some case , whereas the db + layer won't emit a result value of `undefined`.) + + The callback proxy must not recurse into this interface. An exec() + call will tie up the Worker thread, causing any recursion attempt + to wait until the first exec() is completed. + + The response is the input options object (or a synthesized one if + passed only a string), noting that options.resultRows and + options.columnNames may be populated by the call to db.exec(). + + + ==================================================================== + "export" the current db + + To export the underlying database as a byte array... + + Message format: + + ``` + { + type: "export", + messageId: ...as above..., + dbId: ...as above... + } + ``` + + Response: + + ``` + { + type: "export", + messageId: ...as above..., + dbId: ...as above... + result: { + byteArray: Uint8Array (as per sqlite3_js_db_export()), + filename: the db filename, + mimetype: "application/x-sqlite3" + } + } + ``` + +*/ +globalThis.sqlite3ApiBootstrap.initializers.push(function(sqlite3){ +const util = sqlite3.util; +sqlite3.initWorker1API = function(){ + 'use strict'; + const toss = (...args)=>{throw new Error(args.join(' '))}; + if(!(globalThis.WorkerGlobalScope instanceof Function)){ + toss("initWorker1API() must be run from a Worker thread."); + } + const sqlite3 = this.sqlite3 || toss("Missing this.sqlite3 object."); + const DB = sqlite3.oo1.DB; + + /** + Returns the app-wide unique ID for the given db, creating one if + needed. + */ + const getDbId = function(db){ + let id = wState.idMap.get(db); + if(id) return id; + id = 'db#'+(++wState.idSeq)+':'+ + Math.floor(Math.random() * 100000000)+':'+ + Math.floor(Math.random() * 100000000); + /** ^^^ can't simply use db.pointer b/c closing/opening may re-use + the same address, which could map pending messages to a wrong + instance. + + 2025-07: https://github.com/sqlite/sqlite-wasm/issues/113 + demonstrates that two Worker1s can end up with the same IDs, + despite using different instances of the library, so we need + to add some randomness to the IDs instead of relying on the + pointer addresses. + */ + wState.idMap.set(db, id); + return id; + }; + + /** + Internal helper for managing Worker-level state. + */ + const wState = { + /** + Each opened DB is added to this.dbList, and the first entry in + that list is the default db. As each db is closed, its entry is + removed from the list. + */ + dbList: [], + /** Sequence number of dbId generation. */ + idSeq: 0, + /** Map of DB instances to dbId. */ + idMap: new WeakMap, + /** Temp holder for "transferable" postMessage() state. */ + xfer: [], + open: function(opt){ + const db = new DB(opt); + this.dbs[getDbId(db)] = db; + if(this.dbList.indexOf(db)<0) this.dbList.push(db); + return db; + }, + close: function(db,alsoUnlink){ + if(db){ + delete this.dbs[getDbId(db)]; + const filename = db.filename; + const pVfs = util.sqlite3__wasm_db_vfs(db.pointer, 0); + db.close(); + const ddNdx = this.dbList.indexOf(db); + if(ddNdx>=0) this.dbList.splice(ddNdx, 1); + if(alsoUnlink && filename && pVfs){ + util.sqlite3__wasm_vfs_unlink(pVfs, filename); + } + } + }, + /** + Posts the given worker message value. If xferList is provided, + it must be an array, in which case a copy of it passed as + postMessage()'s second argument and xferList.length is set to + 0. + */ + post: function(msg,xferList){ + if(xferList && xferList.length){ + globalThis.postMessage( msg, Array.from(xferList) ); + xferList.length = 0; + }else{ + globalThis.postMessage(msg); + } + }, + /** Map of DB IDs to DBs. */ + dbs: Object.create(null), + /** Fetch the DB for the given id. Throw if require=true and the + id is not valid, else return the db or undefined. */ + getDb: function(id,require=true){ + return this.dbs[id] + || (require ? toss("Unknown (or closed) DB ID:",id) : undefined); + } + }; + + /** Throws if the given db is falsy or not opened, else returns its + argument. */ + const affirmDbOpen = function(db = wState.dbList[0]){ + return (db && db.pointer) ? db : toss("DB is not opened."); + }; + + /** Extract dbId from the given message payload. */ + const getMsgDb = function(msgData,affirmExists=true){ + const db = wState.getDb(msgData.dbId,false) || wState.dbList[0]; + return affirmExists ? affirmDbOpen(db) : db; + }; + + const getDefaultDbId = function(){ + return wState.dbList[0] && getDbId(wState.dbList[0]); + }; + + const isSpecialDbFilename = (n)=>{ + return ""===n || ':'===n[0]; + }; + + /** + A level of "organizational abstraction" for the Worker1 + API. Each method in this object must map directly to a Worker1 + message type key. The onmessage() dispatcher attempts to + dispatch all inbound messages to a method of this object, + passing it the event.data part of the inbound event object. All + methods must return a plain Object containing any result + state, which the dispatcher may amend. All methods must throw + on error. + */ + const wMsgHandler = { + open: function(ev){ + const oargs = Object.create(null), args = (ev.args || Object.create(null)); + if(args.simulateError){ // undocumented internal testing option + toss("Throwing because of simulateError flag."); + } + const rc = Object.create(null); + oargs.vfs = args.vfs; + oargs.filename = args.filename || ""; + const db = wState.open(oargs); + rc.filename = db.filename; + rc.persistent = !!sqlite3.capi.sqlite3_js_db_uses_vfs(db.pointer, "opfs"); + rc.dbId = getDbId(db); + rc.vfs = db.dbVfsName(); + return rc; + }, + + close: function(ev){ + const db = getMsgDb(ev,false); + const response = { + filename: db && db.filename + }; + if(db){ + const doUnlink = ((ev.args && 'object'===typeof ev.args) + ? !!ev.args.unlink : false); + wState.close(db, doUnlink); + } + return response; + }, + + exec: function(ev){ + const rc = ( + 'string'===typeof ev.args + ) ? {sql: ev.args} : (ev.args || Object.create(null)); + if('stmt'===rc.rowMode){ + toss("Invalid rowMode for 'exec': stmt mode", + "does not work in the Worker API."); + }else if(!rc.sql){ + toss("'exec' requires input SQL."); + } + const db = getMsgDb(ev); + if(rc.callback || Array.isArray(rc.resultRows)){ + // Part of a copy-avoidance optimization for blobs + db._blobXfer = wState.xfer; + } + const theCallback = rc.callback; + let rowNumber = 0; + const hadColNames = !!rc.columnNames; + if('string' === typeof theCallback){ + if(!hadColNames) rc.columnNames = []; + /* Treat this as a worker message type and post each + row as a message of that type. */ + rc.callback = function(row,stmt){ + wState.post({ + type: theCallback, + columnNames: rc.columnNames, + rowNumber: ++rowNumber, + row: row + }, wState.xfer); + } + } + try { + const changeCount = !!rc.countChanges + ? db.changes(true,(64===rc.countChanges)) + : undefined; + db.exec(rc); + if(undefined !== changeCount){ + rc.changeCount = db.changes(true,64===rc.countChanges) - changeCount; + } + const lastInsertRowId = !!rc.lastInsertRowId + ? sqlite3.capi.sqlite3_last_insert_rowid(db) + : undefined; + if( undefined!==lastInsertRowId ){ + rc.lastInsertRowId = lastInsertRowId; + } + if(rc.callback instanceof Function){ + rc.callback = theCallback; + /* Post a sentinel message to tell the client that the end + of the result set has been reached (possibly with zero + rows). */ + wState.post({ + type: theCallback, + columnNames: rc.columnNames, + rowNumber: null /*null to distinguish from "property not set"*/, + row: undefined /*undefined because null is a legal row value + for some rowType values, but undefined is not*/ + }); + } + }finally{ + delete db._blobXfer; + if(rc.callback) rc.callback = theCallback; + } + return rc; + }/*exec()*/, + + 'config-get': function(){ + const rc = Object.create(null), src = sqlite3.config; + [ + 'bigIntEnabled' + ].forEach(function(k){ + if(Object.getOwnPropertyDescriptor(src, k)) rc[k] = src[k]; + }); + rc.version = sqlite3.version; + rc.vfsList = sqlite3.capi.sqlite3_js_vfs_list(); + return rc; + }, + + /** + Exports the database to a byte array, as per + sqlite3_serialize(). Response is an object: + + { + byteArray: Uint8Array (db file contents), + filename: the current db filename, + mimetype: 'application/x-sqlite3' + } + */ + export: function(ev){ + const db = getMsgDb(ev); + const response = { + byteArray: sqlite3.capi.sqlite3_js_db_export(db.pointer), + filename: db.filename, + mimetype: 'application/x-sqlite3' + }; + wState.xfer.push(response.byteArray.buffer); + return response; + }/*export()*/, + + toss: function(ev){ + toss("Testing worker exception"); + } + }/*wMsgHandler*/; + + globalThis.onmessage = async function(ev){ + ev = ev.data; + let result, dbId = ev.dbId, evType = ev.type; + const arrivalTime = performance.now(); + try { + if(wMsgHandler.hasOwnProperty(evType) && + wMsgHandler[evType] instanceof Function){ + result = await wMsgHandler[evType](ev); + }else{ + toss("Unknown db worker message type:",ev.type); + } + }catch(err){ + evType = 'error'; + result = { + operation: ev.type, + message: err.message, + errorClass: err.name, + input: ev + }; + if(err.stack){ + result.stack = ('string'===typeof err.stack) + ? err.stack.split(/\n\s*/) : err.stack; + } + if(0) sqlite3.config.warn("Worker is propagating an exception to main thread.", + "Reporting it _here_ for the stack trace:",err,result); + } + if(!dbId){ + dbId = result.dbId/*from 'open' cmd*/ + || getDefaultDbId(); + } + // Timing info is primarily for use in testing this API. It's not part of + // the public API. arrivalTime = when the worker got the message. + wState.post({ + type: evType, + dbId: dbId, + messageId: ev.messageId, + workerReceivedTime: arrivalTime, + workerRespondTime: performance.now(), + departureTime: ev.departureTime, + // TODO: move the timing bits into... + //timing:{ + // departure: ev.departureTime, + // workerReceived: arrivalTime, + // workerResponse: performance.now(); + //}, + result: result + }, wState.xfer); + }; + globalThis.postMessage({type:'sqlite3-api',result:'worker1-ready'}); +}.bind({sqlite3}); +}); +//#else +/* Built with the omit-oo1 flag. */ +//#endif if not omit-oo1 diff --git a/ext/wasm/api/sqlite3-license-version-header.js b/ext/wasm/api/sqlite3-license-version-header.js new file mode 100644 index 0000000000..4829894638 --- /dev/null +++ b/ext/wasm/api/sqlite3-license-version-header.js @@ -0,0 +1,25 @@ +/* +** LICENSE for the sqlite3 WebAssembly/JavaScript APIs. +** +** This bundle (typically released as sqlite3.js or sqlite3.mjs) +** is an amalgamation of JavaScript source code from two projects: +** +** 1) https://emscripten.org: the Emscripten "glue code" is covered by +** the terms of the MIT license and University of Illinois/NCSA +** Open Source License, as described at: +** +** https://emscripten.org/docs/introducing_emscripten/emscripten_license.html +** +** 2) https://sqlite.org: all code and documentation labeled as being +** from this source are released under the same terms as the sqlite3 +** C library: +** +** 2022-10-16 +** +** The author disclaims copyright to this source code. In place of a +** legal notice, here is a blessing: +** +** * May you do good and not evil. +** * May you find forgiveness for yourself and forgive others. +** * May you share freely, never taking more than you give. +*/ diff --git a/ext/wasm/api/sqlite3-opfs-async-proxy.js b/ext/wasm/api/sqlite3-opfs-async-proxy.js new file mode 100644 index 0000000000..e10d0dd505 --- /dev/null +++ b/ext/wasm/api/sqlite3-opfs-async-proxy.js @@ -0,0 +1,805 @@ +/* + 2022-09-16 + + The author disclaims copyright to this source code. In place of a + legal notice, here is a blessing: + + * May you do good and not evil. + * May you find forgiveness for yourself and forgive others. + * May you share freely, never taking more than you give. + + *********************************************************************** + + A Worker which manages asynchronous OPFS handles on behalf of a + synchronous API which controls it via a combination of Worker + messages, SharedArrayBuffer, and Atomics. It is the asynchronous + counterpart of the API defined in sqlite3-vfs-opfs.js. + + Highly indebted to: + + https://github.com/rhashimoto/wa-sqlite/blob/master/src/examples/OriginPrivateFileSystemVFS.js + + for demonstrating how to use the OPFS APIs. + + This file is to be loaded as a Worker. It does not have any direct + access to the sqlite3 JS/WASM bits, so any bits which it needs (most + notably SQLITE_xxx integer codes) have to be imported into it via an + initialization process. + + This file represents an implementation detail of a larger piece of + code, and not a public interface. Its details may change at any time + and are not intended to be used by any client-level code. + + 2022-11-27: Chrome v108 changes some async methods to synchronous, as + documented at: + + https://developer.chrome.com/blog/sync-methods-for-accesshandles/ + + Firefox v111 and Safari 16.4, both released in March 2023, also + include this. + + We cannot change to the sync forms at this point without breaking + clients who use Chrome v104-ish or higher. truncate(), getSize(), + flush(), and close() are now (as of v108) synchronous. Calling them + with an "await", as we have to for the async forms, is still legal + with the sync forms but is superfluous. Calling the async forms with + theFunc().then(...) is not compatible with the change to + synchronous, but we do do not use those APIs that way. i.e. we don't + _need_ to change anything for this, but at some point (after Chrome + versions (approximately) 104-107 are extinct) should change our + usage of those methods to remove the "await". +*/ +"use strict"; +const wPost = (type,...args)=>postMessage({type, payload:args}); +const installAsyncProxy = function(){ + const toss = function(...args){throw new Error(args.join(' '))}; + if(globalThis.window === globalThis){ + toss("This code cannot run from the main thread.", + "Load it as a Worker from a separate Worker."); + }else if(!navigator?.storage?.getDirectory){ + toss("This API requires navigator.storage.getDirectory."); + } + + /** + Will hold state copied to this object from the synchronous side of + this API. + */ + const state = Object.create(null); + + /** + verbose: + + 0 = no logging output + 1 = only errors + 2 = warnings and errors + 3 = debug, warnings, and errors + */ + state.verbose = 1; + + const loggers = { + 0:console.error.bind(console), + 1:console.warn.bind(console), + 2:console.log.bind(console) + }; + const logImpl = (level,...args)=>{ + if(state.verbose>level) loggers[level]("OPFS asyncer:",...args); + }; + const log = (...args)=>logImpl(2, ...args); + const warn = (...args)=>logImpl(1, ...args); + const error = (...args)=>logImpl(0, ...args); + + /** + __openFiles is a map of sqlite3_file pointers (integers) to + metadata related to a given OPFS file handles. The pointers are, in + this side of the interface, opaque file handle IDs provided by the + synchronous part of this constellation. Each value is an object + with a structure demonstrated in the xOpen() impl. + */ + const __openFiles = Object.create(null); + /** + __implicitLocks is a Set of sqlite3_file pointers (integers) which were + "auto-locked". i.e. those for which we obtained a sync access + handle without an explicit xLock() call. Such locks will be + released during db connection idle time, whereas a sync access + handle obtained via xLock(), or subsequently xLock()'d after + auto-acquisition, will not be released until xUnlock() is called. + + Maintenance reminder: if we relinquish auto-locks at the end of the + operation which acquires them, we pay a massive performance + penalty: speedtest1 benchmarks take up to 4x as long. By delaying + the lock release until idle time, the hit is negligible. + */ + const __implicitLocks = new Set(); + + /** + Expects an OPFS file path. It gets resolved, such that ".." + components are properly expanded, and returned. If the 2nd arg is + true, the result is returned as an array of path elements, else an + absolute path string is returned. + */ + const getResolvedPath = function(filename,splitIt){ + const p = new URL( + filename, 'file://irrelevant' + ).pathname; + return splitIt ? p.split('/').filter((v)=>!!v) : p; + }; + + /** + Takes the absolute path to a filesystem element. Returns an array + of [handleOfContainingDir, filename]. If the 2nd argument is truthy + then each directory element leading to the file is created along + the way. Throws if any creation or resolution fails. + */ + const getDirForFilename = async function f(absFilename, createDirs = false){ + const path = getResolvedPath(absFilename, true); + const filename = path.pop(); + let dh = state.rootDir; + for(const dirName of path){ + if(dirName){ + dh = await dh.getDirectoryHandle(dirName, {create: !!createDirs}); + } + } + return [dh, filename]; + }; + + /** + If the given file-holding object has a sync handle attached to it, + that handle is removed and asynchronously closed. Though it may + sound sensible to continue work as soon as the close() returns + (noting that it's asynchronous), doing so can cause operations + performed soon afterwards, e.g. a call to getSyncHandle(), to fail + because they may happen out of order from the close(). OPFS does + not guaranty that the actual order of operations is retained in + such cases. i.e. always "await" on the result of this function. + */ + const closeSyncHandle = async (fh)=>{ + if(fh.syncHandle){ + log("Closing sync handle for",fh.filenameAbs); + const h = fh.syncHandle; + delete fh.syncHandle; + delete fh.xLock; + __implicitLocks.delete(fh.fid); + return h.close(); + } + }; + + /** + A proxy for closeSyncHandle() which is guaranteed to not throw. + + This function is part of a lock/unlock step in functions which + require a sync access handle but may be called without xLock() + having been called first. Such calls need to release that + handle to avoid locking the file for all of time. This is an + _attempt_ at reducing cross-tab contention but it may prove + to be more of a problem than a solution and may need to be + removed. + */ + const closeSyncHandleNoThrow = async (fh)=>{ + try{await closeSyncHandle(fh)} + catch(e){ + warn("closeSyncHandleNoThrow() ignoring:",e,fh); + } + }; + + /* Release all auto-locks. */ + const releaseImplicitLocks = async ()=>{ + if(__implicitLocks.size){ + /* Release all auto-locks. */ + for(const fid of __implicitLocks){ + const fh = __openFiles[fid]; + await closeSyncHandleNoThrow(fh); + log("Auto-unlocked",fid,fh.filenameAbs); + } + } + }; + + /** + An experiment in improving concurrency by freeing up implicit locks + sooner. This is known to impact performance dramatically but it has + also shown to improve concurrency considerably. + + If fh.releaseImplicitLocks is truthy and fh is in __implicitLocks, + this routine returns closeSyncHandleNoThrow(), else it is a no-op. + */ + const releaseImplicitLock = async (fh)=>{ + if(fh.releaseImplicitLocks && __implicitLocks.has(fh.fid)){ + return closeSyncHandleNoThrow(fh); + } + }; + + /** + An error class specifically for use with getSyncHandle(), the goal + of which is to eventually be able to distinguish unambiguously + between locking-related failures and other types, noting that we + cannot currently do so because createSyncAccessHandle() does not + define its exceptions in the required level of detail. + + 2022-11-29: according to: + + https://github.com/whatwg/fs/pull/21 + + NoModificationAllowedError will be the standard exception thrown + when acquisition of a sync access handle fails due to a locking + error. As of this writing, that error type is not visible in the + dev console in Chrome v109, nor is it documented in MDN, but an + error with that "name" property is being thrown from the OPFS + layer. + */ + class GetSyncHandleError extends Error { + constructor(errorObject, ...msg){ + super([ + ...msg, ': '+errorObject.name+':', + errorObject.message + ].join(' '), { + cause: errorObject + }); + this.name = 'GetSyncHandleError'; + } + }; + + /** + Attempts to find a suitable SQLITE_xyz result code for Error + object e. Returns either such a translation or rc if if it does + not know how to translate the exception. + */ + GetSyncHandleError.convertRc = (e,rc)=>{ + if( e instanceof GetSyncHandleError ){ + if( e.cause.name==='NoModificationAllowedError' + /* Inconsistent exception.name from Chrome/ium with the + same exception.message text: */ + || (e.cause.name==='DOMException' + && 0===e.cause.message.indexOf('Access Handles cannot')) ){ + return state.sq3Codes.SQLITE_BUSY; + }else if( 'NotFoundError'===e.cause.name ){ + /** + Maintenance reminder: SQLITE_NOTFOUND, though it looks like + a good match, has different semantics than NotFoundError + and is not suitable here. + */ + return state.sq3Codes.SQLITE_CANTOPEN; + } + }else if( 'NotFoundError'===e?.name ){ + return state.sq3Codes.SQLITE_CANTOPEN; + } + return rc; + }; + + /** + Returns the sync access handle associated with the given file + handle object (which must be a valid handle object, as created by + xOpen()), lazily opening it if needed. + + In order to help alleviate cross-tab contention for a dabase, if + an exception is thrown while acquiring the handle, this routine + will wait briefly and try again, up to some fixed number of + times. If acquisition still fails at that point it will give up + and propagate the exception. Client-level code will see that as + an I/O error. + + 2024-06-12: there is a rare race condition here which has been + reported a single time: + + https://sqlite.org/forum/forumpost/9ee7f5340802d600 + + What appears to be happening is that file we're waiting for a + lock on is deleted while we wait. What currently happens here is + that a locking exception is thrown but the exception type is + NotFoundError. In such cases, we very probably should attempt to + re-open/re-create the file an obtain the lock on it (noting that + there's another race condition there). That's easy to say but + creating a viable test for that condition has proven challenging + so far. + */ + const getSyncHandle = async (fh,opName)=>{ + if(!fh.syncHandle){ + const t = performance.now(); + log("Acquiring sync handle for",fh.filenameAbs); + const maxTries = 6, + msBase = state.asyncIdleWaitTime * 2; + let i = 1, ms = msBase; + for(; true; ms = msBase * ++i){ + try { + //if(i<3) toss("Just testing getSyncHandle() wait-and-retry."); + //TODO? A config option which tells it to throw here + //randomly every now and then, for testing purposes. + fh.syncHandle = await fh.fileHandle.createSyncAccessHandle(); + break; + }catch(e){ + if(i === maxTries){ + throw new GetSyncHandleError( + e, "Error getting sync handle for",opName+"().",maxTries, + "attempts failed.",fh.filenameAbs + ); + } + warn("Error getting sync handle for",opName+"(). Waiting",ms, + "ms and trying again.",fh.filenameAbs,e); + Atomics.wait(state.sabOPView, state.opIds.retry, 0, ms); + } + } + log("Got",opName+"() sync handle for",fh.filenameAbs, + 'in',performance.now() - t,'ms'); + if(!fh.xLock){ + __implicitLocks.add(fh.fid); + log("Acquired implicit lock for",opName+"()",fh.fid,fh.filenameAbs); + } + } + return fh.syncHandle; + }; + + /** + Stores the given value at state.sabOPView[state.opIds.rc] and then + Atomics.notify()'s it. + */ + const storeAndNotify = (opName, value)=>{ + log(opName+"() => notify(",value,")"); + Atomics.store(state.sabOPView, state.opIds.rc, value); + Atomics.notify(state.sabOPView, state.opIds.rc); + }; + + /** + Throws if fh is a file-holding object which is flagged as read-only. + */ + const affirmNotRO = function(opName,fh){ + if(fh.readOnly) toss(opName+"(): File is read-only: "+fh.filenameAbs); + }; + + /** + Gets set to true by the 'opfs-async-shutdown' command to quit the + wait loop. This is only intended for debugging purposes: we cannot + inspect this file's state while the tight waitLoop() is running and + need a way to stop that loop for introspection purposes. + */ + let flagAsyncShutdown = false; + + /** + Asynchronous wrappers for sqlite3_vfs and sqlite3_io_methods + methods, as well as helpers like mkdir(). + */ + const vfsAsyncImpls = { + 'opfs-async-shutdown': async ()=>{ + flagAsyncShutdown = true; + storeAndNotify('opfs-async-shutdown', 0); + }, + mkdir: async (dirname)=>{ + let rc = 0; + try { + await getDirForFilename(dirname+"/filepart", true); + }catch(e){ + state.s11n.storeException(2,e); + rc = state.sq3Codes.SQLITE_IOERR; + } + storeAndNotify('mkdir', rc); + }, + xAccess: async (filename)=>{ + /* OPFS cannot support the full range of xAccess() queries + sqlite3 calls for. We can essentially just tell if the file + is accessible, but if it is then it's automatically writable + (unless it's locked, which we cannot(?) know without trying + to open it). OPFS does not have the notion of read-only. + + The return semantics of this function differ from sqlite3's + xAccess semantics because we are limited in what we can + communicate back to our synchronous communication partner: 0 = + accessible, non-0 means not accessible. + */ + let rc = 0; + try{ + const [dh, fn] = await getDirForFilename(filename); + await dh.getFileHandle(fn); + }catch(e){ + state.s11n.storeException(2,e); + rc = state.sq3Codes.SQLITE_IOERR; + } + storeAndNotify('xAccess', rc); + }, + xClose: async function(fid/*sqlite3_file pointer*/){ + const opName = 'xClose'; + __implicitLocks.delete(fid); + const fh = __openFiles[fid]; + let rc = 0; + if(fh){ + delete __openFiles[fid]; + await closeSyncHandle(fh); + if(fh.deleteOnClose){ + try{ await fh.dirHandle.removeEntry(fh.filenamePart) } + catch(e){ warn("Ignoring dirHandle.removeEntry() failure of",fh,e) } + } + }else{ + state.s11n.serialize(); + rc = state.sq3Codes.SQLITE_NOTFOUND; + } + storeAndNotify(opName, rc); + }, + xDelete: async function(...args){ + const rc = await vfsAsyncImpls.xDeleteNoWait(...args); + storeAndNotify('xDelete', rc); + }, + xDeleteNoWait: async function(filename, syncDir = 0, recursive = false){ + /* The syncDir flag is, for purposes of the VFS API's semantics, + ignored here. However, if it has the value 0x1234 then: after + deleting the given file, recursively try to delete any empty + directories left behind in its wake (ignoring any errors and + stopping at the first failure). + + That said: we don't know for sure that removeEntry() fails if + the dir is not empty because the API is not documented. It has, + however, a "recursive" flag which defaults to false, so + presumably it will fail if the dir is not empty and that flag + is false. + */ + let rc = 0; + try { + while(filename){ + const [hDir, filenamePart] = await getDirForFilename(filename, false); + if(!filenamePart) break; + await hDir.removeEntry(filenamePart, {recursive}); + if(0x1234 !== syncDir) break; + recursive = false; + filename = getResolvedPath(filename, true); + filename.pop(); + filename = filename.join('/'); + } + }catch(e){ + state.s11n.storeException(2,e); + rc = state.sq3Codes.SQLITE_IOERR_DELETE; + } + return rc; + }, + xFileSize: async function(fid/*sqlite3_file pointer*/){ + const fh = __openFiles[fid]; + let rc = 0; + try{ + const sz = await (await getSyncHandle(fh,'xFileSize')).getSize(); + state.s11n.serialize(Number(sz)); + }catch(e){ + state.s11n.storeException(1,e); + rc = GetSyncHandleError.convertRc(e,state.sq3Codes.SQLITE_IOERR); + } + await releaseImplicitLock(fh); + storeAndNotify('xFileSize', rc); + }, + xLock: async function(fid/*sqlite3_file pointer*/, + lockType/*SQLITE_LOCK_...*/){ + const fh = __openFiles[fid]; + let rc = 0; + const oldLockType = fh.xLock; + fh.xLock = lockType; + if( !fh.syncHandle ){ + try { + await getSyncHandle(fh,'xLock'); + __implicitLocks.delete(fid); + }catch(e){ + state.s11n.storeException(1,e); + rc = GetSyncHandleError.convertRc(e,state.sq3Codes.SQLITE_IOERR_LOCK); + fh.xLock = oldLockType; + } + } + storeAndNotify('xLock',rc); + }, + xOpen: async function(fid/*sqlite3_file pointer*/, filename, + flags/*SQLITE_OPEN_...*/, + opfsFlags/*OPFS_...*/){ + const opName = 'xOpen'; + const create = (state.sq3Codes.SQLITE_OPEN_CREATE & flags); + try{ + let hDir, filenamePart; + try { + [hDir, filenamePart] = await getDirForFilename(filename, !!create); + }catch(e){ + state.s11n.storeException(1,e); + storeAndNotify(opName, state.sq3Codes.SQLITE_NOTFOUND); + return; + } + if( state.opfsFlags.OPFS_UNLINK_BEFORE_OPEN & opfsFlags ){ + try{ + await hDir.removeEntry(filenamePart); + }catch(e){ + /* ignoring */ + //warn("Ignoring failed Unlink of",filename,":",e); + } + } + const hFile = await hDir.getFileHandle(filenamePart, {create}); + const fh = Object.assign(Object.create(null),{ + fid: fid, + filenameAbs: filename, + filenamePart: filenamePart, + dirHandle: hDir, + fileHandle: hFile, + sabView: state.sabFileBufView, + readOnly: !create && !!(state.sq3Codes.SQLITE_OPEN_READONLY & flags), + deleteOnClose: !!(state.sq3Codes.SQLITE_OPEN_DELETEONCLOSE & flags) + }); + fh.releaseImplicitLocks = + (opfsFlags & state.opfsFlags.OPFS_UNLOCK_ASAP) + || state.opfsFlags.defaultUnlockAsap; + __openFiles[fid] = fh; + storeAndNotify(opName, 0); + }catch(e){ + error(opName,e); + state.s11n.storeException(1,e); + storeAndNotify(opName, state.sq3Codes.SQLITE_IOERR); + } + }, + xRead: async function(fid/*sqlite3_file pointer*/,n,offset64){ + let rc = 0, nRead; + const fh = __openFiles[fid]; + try{ + nRead = (await getSyncHandle(fh,'xRead')).read( + fh.sabView.subarray(0, n), + {at: Number(offset64)} + ); + if(nRead < n){/* Zero-fill remaining bytes */ + fh.sabView.fill(0, nRead, n); + rc = state.sq3Codes.SQLITE_IOERR_SHORT_READ; + } + }catch(e){ + error("xRead() failed",e,fh); + state.s11n.storeException(1,e); + rc = GetSyncHandleError.convertRc(e,state.sq3Codes.SQLITE_IOERR_READ); + } + await releaseImplicitLock(fh); + storeAndNotify('xRead',rc); + }, + xSync: async function(fid/*sqlite3_file pointer*/,flags/*ignored*/){ + const fh = __openFiles[fid]; + let rc = 0; + if(!fh.readOnly && fh.syncHandle){ + try { + await fh.syncHandle.flush(); + }catch(e){ + state.s11n.storeException(2,e); + rc = state.sq3Codes.SQLITE_IOERR_FSYNC; + } + } + storeAndNotify('xSync',rc); + }, + xTruncate: async function(fid/*sqlite3_file pointer*/,size){ + let rc = 0; + const fh = __openFiles[fid]; + try{ + affirmNotRO('xTruncate', fh); + await (await getSyncHandle(fh,'xTruncate')).truncate(size); + }catch(e){ + error("xTruncate():",e,fh); + state.s11n.storeException(2,e); + rc = GetSyncHandleError.convertRc(e,state.sq3Codes.SQLITE_IOERR_TRUNCATE); + } + await releaseImplicitLock(fh); + storeAndNotify('xTruncate',rc); + }, + xUnlock: async function(fid/*sqlite3_file pointer*/, + lockType/*SQLITE_LOCK_...*/){ + let rc = 0; + const fh = __openFiles[fid]; + if( fh.syncHandle + && state.sq3Codes.SQLITE_LOCK_NONE===lockType + /* Note that we do not differentiate between lock types in + this VFS. We're either locked or unlocked. */ ){ + try { await closeSyncHandle(fh) } + catch(e){ + state.s11n.storeException(1,e); + rc = state.sq3Codes.SQLITE_IOERR_UNLOCK; + } + } + storeAndNotify('xUnlock',rc); + }, + xWrite: async function(fid/*sqlite3_file pointer*/,n,offset64){ + let rc; + const fh = __openFiles[fid]; + try{ + affirmNotRO('xWrite', fh); + rc = ( + n === (await getSyncHandle(fh,'xWrite')) + .write(fh.sabView.subarray(0, n), + {at: Number(offset64)}) + ) ? 0 : state.sq3Codes.SQLITE_IOERR_WRITE; + }catch(e){ + error("xWrite():",e,fh); + state.s11n.storeException(1,e); + rc = GetSyncHandleError.convertRc(e,state.sq3Codes.SQLITE_IOERR_WRITE); + } + await releaseImplicitLock(fh); + storeAndNotify('xWrite',rc); + } + }/*vfsAsyncImpls*/; + + const initS11n = ()=>{ + /** + ACHTUNG: this code is 100% duplicated in the other half of this + proxy! The documentation is maintained in the "synchronous half". + */ + if(state.s11n) return state.s11n; + const textDecoder = new TextDecoder(), + textEncoder = new TextEncoder('utf-8'), + viewU8 = new Uint8Array(state.sabIO, state.sabS11nOffset, state.sabS11nSize), + viewDV = new DataView(state.sabIO, state.sabS11nOffset, state.sabS11nSize); + state.s11n = Object.create(null); + const TypeIds = Object.create(null); + TypeIds.number = { id: 1, size: 8, getter: 'getFloat64', setter: 'setFloat64' }; + TypeIds.bigint = { id: 2, size: 8, getter: 'getBigInt64', setter: 'setBigInt64' }; + TypeIds.boolean = { id: 3, size: 4, getter: 'getInt32', setter: 'setInt32' }; + TypeIds.string = { id: 4 }; + const getTypeId = (v)=>( + TypeIds[typeof v] + || toss("Maintenance required: this value type cannot be serialized.",v) + ); + const getTypeIdById = (tid)=>{ + switch(tid){ + case TypeIds.number.id: return TypeIds.number; + case TypeIds.bigint.id: return TypeIds.bigint; + case TypeIds.boolean.id: return TypeIds.boolean; + case TypeIds.string.id: return TypeIds.string; + default: toss("Invalid type ID:",tid); + } + }; + state.s11n.deserialize = function(clear=false){ + const argc = viewU8[0]; + const rc = argc ? [] : null; + if(argc){ + const typeIds = []; + let offset = 1, i, n, v; + for(i = 0; i < argc; ++i, ++offset){ + typeIds.push(getTypeIdById(viewU8[offset])); + } + for(i = 0; i < argc; ++i){ + const t = typeIds[i]; + if(t.getter){ + v = viewDV[t.getter](offset, state.littleEndian); + offset += t.size; + }else{/*String*/ + n = viewDV.getInt32(offset, state.littleEndian); + offset += 4; + v = textDecoder.decode(viewU8.slice(offset, offset+n)); + offset += n; + } + rc.push(v); + } + } + if(clear) viewU8[0] = 0; + //log("deserialize:",argc, rc); + return rc; + }; + state.s11n.serialize = function(...args){ + if(args.length){ + //log("serialize():",args); + const typeIds = []; + let i = 0, offset = 1; + viewU8[0] = args.length & 0xff /* header = # of args */; + for(; i < args.length; ++i, ++offset){ + /* Write the TypeIds.id value into the next args.length + bytes. */ + typeIds.push(getTypeId(args[i])); + viewU8[offset] = typeIds[i].id; + } + for(i = 0; i < args.length; ++i) { + /* Deserialize the following bytes based on their + corresponding TypeIds.id from the header. */ + const t = typeIds[i]; + if(t.setter){ + viewDV[t.setter](offset, args[i], state.littleEndian); + offset += t.size; + }else{/*String*/ + const s = textEncoder.encode(args[i]); + viewDV.setInt32(offset, s.byteLength, state.littleEndian); + offset += 4; + viewU8.set(s, offset); + offset += s.byteLength; + } + } + //log("serialize() result:",viewU8.slice(0,offset)); + }else{ + viewU8[0] = 0; + } + }; + + state.s11n.storeException = state.asyncS11nExceptions + ? ((priority,e)=>{ + if(priority<=state.asyncS11nExceptions){ + state.s11n.serialize([e.name,': ',e.message].join("")); + } + }) + : ()=>{}; + + return state.s11n; + }/*initS11n()*/; + + const waitLoop = async function f(){ + const opHandlers = Object.create(null); + for(let k of Object.keys(state.opIds)){ + const vi = vfsAsyncImpls[k]; + if(!vi) continue; + const o = Object.create(null); + opHandlers[state.opIds[k]] = o; + o.key = k; + o.f = vi; + } + while(!flagAsyncShutdown){ + try { + if('not-equal'!==Atomics.wait( + state.sabOPView, state.opIds.whichOp, 0, state.asyncIdleWaitTime + )){ + /* Maintenance note: we compare against 'not-equal' because + + https://github.com/tomayac/sqlite-wasm/issues/12 + + is reporting that this occasionally, under high loads, + returns 'ok', which leads to the whichOp being 0 (which + isn't a valid operation ID and leads to an exception, + along with a corresponding ugly console log + message). Unfortunately, the conditions for that cannot + be reliably reproduced. The only place in our code which + writes a 0 to the state.opIds.whichOp SharedArrayBuffer + index is a few lines down from here, and that instance + is required in order for clear communication between + the sync half of this proxy and this half. + */ + await releaseImplicitLocks(); + continue; + } + const opId = Atomics.load(state.sabOPView, state.opIds.whichOp); + Atomics.store(state.sabOPView, state.opIds.whichOp, 0); + const hnd = opHandlers[opId] ?? toss("No waitLoop handler for whichOp #",opId); + const args = state.s11n.deserialize( + true /* clear s11n to keep the caller from confusing this with + an exception string written by the upcoming + operation */ + ) || []; + //warn("waitLoop() whichOp =",opId, hnd, args); + if(hnd.f) await hnd.f(...args); + else error("Missing callback for opId",opId); + }catch(e){ + error('in waitLoop():',e); + } + } + }; + + navigator.storage.getDirectory().then(function(d){ + state.rootDir = d; + globalThis.onmessage = function({data}){ + switch(data.type){ + case 'opfs-async-init':{ + /* Receive shared state from synchronous partner */ + const opt = data.args; + for(const k in opt) state[k] = opt[k]; + state.verbose = opt.verbose ?? 1; + state.sabOPView = new Int32Array(state.sabOP); + state.sabFileBufView = new Uint8Array(state.sabIO, 0, state.fileBufferSize); + state.sabS11nView = new Uint8Array(state.sabIO, state.sabS11nOffset, state.sabS11nSize); + Object.keys(vfsAsyncImpls).forEach((k)=>{ + if(!Number.isFinite(state.opIds[k])){ + toss("Maintenance required: missing state.opIds[",k,"]"); + } + }); + initS11n(); + log("init state",state); + wPost('opfs-async-inited'); + waitLoop(); + break; + } + case 'opfs-async-restart': + if(flagAsyncShutdown){ + warn("Restarting after opfs-async-shutdown. Might or might not work."); + flagAsyncShutdown = false; + waitLoop(); + } + break; + } + }; + wPost('opfs-async-loaded'); + }).catch((e)=>error("error initializing OPFS asyncer:",e)); +}/*installAsyncProxy()*/; +if(!globalThis.SharedArrayBuffer){ + wPost('opfs-unavailable', "Missing SharedArrayBuffer API.", + "The server must emit the COOP/COEP response headers to enable that."); +}else if(!globalThis.Atomics){ + wPost('opfs-unavailable', "Missing Atomics API.", + "The server must emit the COOP/COEP response headers to enable that."); +}else if(!globalThis.FileSystemHandle || + !globalThis.FileSystemDirectoryHandle || + !globalThis.FileSystemFileHandle || + !globalThis.FileSystemFileHandle.prototype.createSyncAccessHandle || + !navigator?.storage?.getDirectory){ + wPost('opfs-unavailable',"Missing required OPFS APIs."); +}else{ + installAsyncProxy(); +} diff --git a/ext/wasm/api/sqlite3-vfs-helper.c-pp.js b/ext/wasm/api/sqlite3-vfs-helper.c-pp.js new file mode 100644 index 0000000000..4d29c7b91a --- /dev/null +++ b/ext/wasm/api/sqlite3-vfs-helper.c-pp.js @@ -0,0 +1,103 @@ +/* +** 2022-11-30 +** +** The author disclaims copyright to this source code. In place of a +** legal notice, here is a blessing: +** +** * May you do good and not evil. +** * May you find forgiveness for yourself and forgive others. +** * May you share freely, never taking more than you give. +*/ + +/** + This file installs sqlite3.vfs, a namespace of helpers for use in + the creation of JavaScript implementations of sqlite3_vfs. +*/ +'use strict'; +globalThis.sqlite3ApiBootstrap.initializers.push(function(sqlite3){ + const wasm = sqlite3.wasm, capi = sqlite3.capi, toss = sqlite3.util.toss3; + const vfs = Object.create(null); + sqlite3.vfs = vfs; + + /** + Uses sqlite3_vfs_register() to register this + sqlite3.capi.sqlite3_vfs instance. This object must have already + been filled out properly. If the first argument is truthy, the + VFS is registered as the default VFS, else it is not. + + On success, returns this object. Throws on error. + */ + capi.sqlite3_vfs.prototype.registerVfs = function(asDefault=false){ + if(!(this instanceof sqlite3.capi.sqlite3_vfs)){ + toss("Expecting a sqlite3_vfs-type argument."); + } + const rc = capi.sqlite3_vfs_register(this, asDefault ? 1 : 0); + if(rc){ + toss("sqlite3_vfs_register(",this,") failed with rc",rc); + } + if(this.pointer !== capi.sqlite3_vfs_find(this.$zName)){ + toss("BUG: sqlite3_vfs_find(vfs.$zName) failed for just-installed VFS", + this); + } + return this; + }; + + /** + A wrapper for + sqlite3.StructBinder.StructType.prototype.installMethods() or + registerVfs() to reduce installation of a VFS and/or its I/O + methods to a single call. + + Accepts an object which contains the properties "io" and/or + "vfs", each of which is itself an object with following properties: + + - `struct`: an sqlite3.StructBinder.StructType-type struct. This + must be a populated (except for the methods) object of type + sqlite3_io_methods (for the "io" entry) or sqlite3_vfs (for the + "vfs" entry). + + - `methods`: an object mapping sqlite3_io_methods method names + (e.g. 'xClose') to JS implementations of those methods. The JS + implementations must be call-compatible with their native + counterparts. + + For each of those object, this function passes its (`struct`, + `methods`, (optional) `applyArgcCheck`) properties to + installMethods(). + + If the `vfs` entry is set then: + + - Its `struct` property's registerVfs() is called. The + `vfs` entry may optionally have an `asDefault` property, which + gets passed as the argument to registerVfs(). + + - If `struct.$zName` is falsy and the entry has a string-type + `name` property, `struct.$zName` is set to the C-string form of + that `name` value before registerVfs() is called. That string + gets added to the on-dispose state of the struct. + + On success returns this object. Throws on error. + */ + vfs.installVfs = function(opt){ + let count = 0; + const propList = ['io','vfs']; + for(const key of propList){ + const o = opt[key]; + if(o){ + ++count; + o.struct.installMethods(o.methods, !!o.applyArgcCheck); + if('vfs'===key){ + if(!o.struct.$zName && 'string'===typeof o.name){ + o.struct.addOnDispose( + o.struct.$zName = wasm.allocCString(o.name) + ); + } + o.struct.registerVfs(!!o.asDefault); + } + } + } + if(!count) toss("Misuse: installVfs() options object requires at least", + "one of:", propList); + return this; + }; +}/*sqlite3ApiBootstrap.initializers.push()*/); diff --git a/ext/wasm/api/sqlite3-vfs-opfs-sahpool.c-pp.js b/ext/wasm/api/sqlite3-vfs-opfs-sahpool.c-pp.js new file mode 100644 index 0000000000..69be338b0c --- /dev/null +++ b/ext/wasm/api/sqlite3-vfs-opfs-sahpool.c-pp.js @@ -0,0 +1,1462 @@ +//#if not target:node +/* + 2023-07-14 + + The author disclaims copyright to this source code. In place of a + legal notice, here is a blessing: + + * May you do good and not evil. + * May you find forgiveness for yourself and forgive others. + * May you share freely, never taking more than you give. + + *********************************************************************** + + This file holds a sqlite3_vfs backed by OPFS storage which uses a + different implementation strategy than the "opfs" VFS. This one is a + port of Roy Hashimoto's OPFS SyncAccessHandle pool: + + https://github.com/rhashimoto/wa-sqlite/blob/master/src/examples/AccessHandlePoolVFS.js + + As described at: + + https://github.com/rhashimoto/wa-sqlite/discussions/67 + + with Roy's explicit permission to permit us to port his to our + infrastructure rather than having to clean-room reverse-engineer it: + + https://sqlite.org/forum/forumpost/e140d84e71 + + Primary differences from the "opfs" VFS include: + + - This one avoids the need for a sub-worker to synchronize + communication between the synchronous C API and the + only-partly-synchronous OPFS API. + + - It does so by opening a fixed number of OPFS files at + library-level initialization time, obtaining SyncAccessHandles to + each, and manipulating those handles via the synchronous sqlite3_vfs + interface. If it cannot open them (e.g. they are already opened by + another tab) then the VFS will not be installed. + + - Because of that, this one lacks all library-level concurrency + support. + + - Also because of that, it does not require the SharedArrayBuffer, + so can function without the COOP/COEP HTTP response headers. + + - It can hypothetically support Safari 16.4+, whereas the "opfs" VFS + requires v17 due to a subworker/storage bug in 16.x which makes it + incompatible with that VFS. + + - This VFS requires the "semi-fully-sync" FileSystemSyncAccessHandle + (hereafter "SAH") APIs released with Chrome v108 (and all other + major browsers released since March 2023). If that API is not + detected, the VFS is not registered. +*/ +globalThis.sqlite3ApiBootstrap.initializers.push(function(sqlite3){ + 'use strict'; + const toss = sqlite3.util.toss; + const toss3 = sqlite3.util.toss3; + const initPromises = Object.create(null) /* cache of (name:result) of VFS init results */; + const capi = sqlite3.capi; + const util = sqlite3.util; + const wasm = sqlite3.wasm; + // Config opts for the VFS... + const SECTOR_SIZE = 4096; + const HEADER_MAX_PATH_SIZE = 512; + const HEADER_FLAGS_SIZE = 4; + const HEADER_DIGEST_SIZE = 8; + const HEADER_CORPUS_SIZE = HEADER_MAX_PATH_SIZE + HEADER_FLAGS_SIZE; + const HEADER_OFFSET_FLAGS = HEADER_MAX_PATH_SIZE; + const HEADER_OFFSET_DIGEST = HEADER_CORPUS_SIZE; + const HEADER_OFFSET_DATA = SECTOR_SIZE; + /* Bitmask of file types which may persist across sessions. + SQLITE_OPEN_xyz types not listed here may be inadvertently + left in OPFS but are treated as transient by this VFS and + they will be cleaned up during VFS init. */ + const PERSISTENT_FILE_TYPES = + capi.SQLITE_OPEN_MAIN_DB | + capi.SQLITE_OPEN_MAIN_JOURNAL | + capi.SQLITE_OPEN_SUPER_JOURNAL | + capi.SQLITE_OPEN_WAL; + const FLAG_COMPUTE_DIGEST_V2 = capi.SQLITE_OPEN_MEMORY + /* Part of the fix for + https://github.com/sqlite/sqlite-wasm/issues/97 + + Summary: prior to version 3.50.0 computeDigest() always computes + a value of [0,0] due to overflows, so it does not do anything + useful. Fixing it invalidates old persistent files, so we + instead only fix it for files created or updated since the bug + was discovered and fixed. + + This flag determines whether we use the broken legacy + computeDigest() or the v2 variant. We only use this flag for + newly-created/overwritten files. Pre-existing files have the + broken digest stored in them so need to continue to use that. + + What this means, in terms of db file compatibility between + versions: + + - DBs created with versions older than this fix (<3.50.0) + can be read by post-fix versions. Such DBs which are written + to in-place (not replaced) by newer versions can still be read + by older versions, as the affected digest is only modified + when the SAH slot is assigned to a given filename. + + - DBs created with post-fix versions will, when read by a pre-fix + version, be seen as having a "bad digest" and will be + unceremoniously replaced by that pre-fix version. When swapping + back to a post-fix version, that version will see that the file + entry is missing the FLAG_COMPUTE_DIGEST_V2 bit so will treat it + as a legacy file. + + This flag is stored in the same memory as the various + SQLITE_OPEN_... flags and we must be careful here to not use a + flag bit which is otherwise relevant for the VFS. + SQLITE_OPEN_MEMORY is handled by sqlite3_open_v2() and friends, + not the VFS, so we'll repurpose that one. If we take a + currently-unused bit and it ends up, at some later point, being + used, we would have to invalidate existing VFS files in order to + move to another bit. Similarly, if the SQLITE_OPEN_MEMORY bit + were ever reassigned (which it won't be!), we'd invalidate all + VFS-side files. + */; + + /** Subdirectory of the VFS's space where "opaque" (randomly-named) + files are stored. Changing this effectively invalidates the data + stored under older names (orphaning it), so don't do that. */ + const OPAQUE_DIR_NAME = ".opaque"; + + /** + Returns short a string of random alphanumeric characters + suitable for use as a random filename. + */ + const getRandomName = ()=>Math.random().toString(36).slice(2); + + const textDecoder = new TextDecoder(); + const textEncoder = new TextEncoder(); + + const optionDefaults = Object.assign(Object.create(null),{ + name: 'opfs-sahpool', + directory: undefined /* derived from .name */, + initialCapacity: 6, + clearOnInit: false, + /* Logging verbosity 3+ == everything, 2 == warnings+errors, 1 == + errors only. */ + verbosity: 2, + forceReinitIfPreviouslyFailed: false + }); + + /** Logging routines, from most to least serious. */ + const loggers = [ + sqlite3.config.error, + sqlite3.config.warn, + sqlite3.config.log + ]; + const log = sqlite3.config.log; + const warn = sqlite3.config.warn; + const error = sqlite3.config.error; + + /* Maps (sqlite3_vfs*) to OpfsSAHPool instances */ + const __mapVfsToPool = new Map(); + const getPoolForVfs = (pVfs)=>__mapVfsToPool.get(pVfs); + const setPoolForVfs = (pVfs,pool)=>{ + if(pool) __mapVfsToPool.set(pVfs, pool); + else __mapVfsToPool.delete(pVfs); + }; + /* Maps (sqlite3_file*) to OpfsSAHPool instances */ + const __mapSqlite3File = new Map(); + const getPoolForPFile = (pFile)=>__mapSqlite3File.get(pFile); + const setPoolForPFile = (pFile,pool)=>{ + if(pool) __mapSqlite3File.set(pFile, pool); + else __mapSqlite3File.delete(pFile); + }; + + /** + Impls for the sqlite3_io_methods methods. Maintenance reminder: + members are in alphabetical order to simplify finding them. + */ + const ioMethods = { + xCheckReservedLock: function(pFile,pOut){ + const pool = getPoolForPFile(pFile); + pool.log('xCheckReservedLock'); + pool.storeErr(); + wasm.poke32(pOut, 1); + return 0; + }, + xClose: function(pFile){ + const pool = getPoolForPFile(pFile); + pool.storeErr(); + const file = pool.getOFileForS3File(pFile); + if(file) { + try{ + pool.log(`xClose ${file.path}`); + pool.mapS3FileToOFile(pFile, false); + file.sah.flush(); + if(file.flags & capi.SQLITE_OPEN_DELETEONCLOSE){ + pool.deletePath(file.path); + } + }catch(e){ + return pool.storeErr(e, capi.SQLITE_IOERR); + } + } + return 0; + }, + xDeviceCharacteristics: function(pFile){ + return capi.SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN; + }, + xFileControl: function(pFile, opId, pArg){ + return capi.SQLITE_NOTFOUND; + }, + xFileSize: function(pFile,pSz64){ + const pool = getPoolForPFile(pFile); + pool.log(`xFileSize`); + const file = pool.getOFileForS3File(pFile); + const size = file.sah.getSize() - HEADER_OFFSET_DATA; + //log(`xFileSize ${file.path} ${size}`); + wasm.poke64(pSz64, BigInt(size)); + return 0; + }, + xLock: function(pFile,lockType){ + const pool = getPoolForPFile(pFile); + pool.log(`xLock ${lockType}`); + pool.storeErr(); + const file = pool.getOFileForS3File(pFile); + file.lockType = lockType; + return 0; + }, + xRead: function(pFile,pDest,n,offset64){ + const pool = getPoolForPFile(pFile); + pool.storeErr(); + const file = pool.getOFileForS3File(pFile); + pool.log(`xRead ${file.path} ${n} @ ${offset64}`); + try { + const nRead = file.sah.read( + wasm.heap8u().subarray(Number(pDest), Number(pDest)+n), + {at: HEADER_OFFSET_DATA + Number(offset64)} + ); + if(nRead < n){ + wasm.heap8u().fill(0, Number(pDest) + nRead, Number(pDest) + n); + return capi.SQLITE_IOERR_SHORT_READ; + } + return 0; + }catch(e){ + return pool.storeErr(e, capi.SQLITE_IOERR); + } + }, + xSectorSize: function(pFile){ + return SECTOR_SIZE; + }, + xSync: function(pFile,flags){ + const pool = getPoolForPFile(pFile); + pool.log(`xSync ${flags}`); + pool.storeErr(); + const file = pool.getOFileForS3File(pFile); + //log(`xSync ${file.path} ${flags}`); + try{ + file.sah.flush(); + return 0; + }catch(e){ + return pool.storeErr(e, capi.SQLITE_IOERR); + } + }, + xTruncate: function(pFile,sz64){ + const pool = getPoolForPFile(pFile); + pool.log(`xTruncate ${sz64}`); + pool.storeErr(); + const file = pool.getOFileForS3File(pFile); + //log(`xTruncate ${file.path} ${iSize}`); + try{ + file.sah.truncate(HEADER_OFFSET_DATA + Number(sz64)); + return 0; + }catch(e){ + return pool.storeErr(e, capi.SQLITE_IOERR); + } + }, + xUnlock: function(pFile,lockType){ + const pool = getPoolForPFile(pFile); + pool.log('xUnlock'); + const file = pool.getOFileForS3File(pFile); + file.lockType = lockType; + return 0; + }, + xWrite: function(pFile,pSrc,n,offset64){ + const pool = getPoolForPFile(pFile); + pool.storeErr(); + const file = pool.getOFileForS3File(pFile); + pool.log(`xWrite ${file.path} ${n} ${offset64}`); + try{ + const nBytes = file.sah.write( + wasm.heap8u().subarray(Number(pSrc), Number(pSrc)+n), + { at: HEADER_OFFSET_DATA + Number(offset64) } + ); + return n===nBytes ? 0 : toss("Unknown write() failure."); + }catch(e){ + return pool.storeErr(e, capi.SQLITE_IOERR); + } + } + }/*ioMethods*/; + + const opfsIoMethods = new capi.sqlite3_io_methods(); + opfsIoMethods.$iVersion = 1; + sqlite3.vfs.installVfs({ + io: {struct: opfsIoMethods, methods: ioMethods} + }); + + /** + Impls for the sqlite3_vfs methods. Maintenance reminder: members + are in alphabetical order to simplify finding them. + */ + const vfsMethods = { + xAccess: function(pVfs,zName,flags,pOut){ + //log(`xAccess ${wasm.cstrToJs(zName)}`); + const pool = getPoolForVfs(pVfs); + pool.storeErr(); + try{ + const name = pool.getPath(zName); + wasm.poke32(pOut, pool.hasFilename(name) ? 1 : 0); + }catch(e){ + /*ignored*/ + wasm.poke32(pOut, 0); + } + return 0; + }, + xCurrentTime: function(pVfs,pOut){ + wasm.poke(pOut, 2440587.5 + (new Date().getTime()/86400000), + 'double'); + return 0; + }, + xCurrentTimeInt64: function(pVfs,pOut){ + wasm.poke(pOut, (2440587.5 * 86400000) + new Date().getTime(), + 'i64'); + return 0; + }, + xDelete: function(pVfs, zName, doSyncDir){ + const pool = getPoolForVfs(pVfs); + pool.log(`xDelete ${wasm.cstrToJs(zName)}`); + pool.storeErr(); + try{ + pool.deletePath(pool.getPath(zName)); + return 0; + }catch(e){ + pool.storeErr(e); + return capi.SQLITE_IOERR_DELETE; + } + }, + xFullPathname: function(pVfs,zName,nOut,pOut){ + //const pool = getPoolForVfs(pVfs); + //pool.log(`xFullPathname ${wasm.cstrToJs(zName)}`); + const i = wasm.cstrncpy(pOut, zName, nOut); + return i nOut) wasm.poke8(pOut + nOut - 1, 0); + }catch(e){ + return capi.SQLITE_NOMEM; + }finally{ + wasm.scopedAllocPop(scope); + } + } + return e ? (e.sqlite3Rc || capi.SQLITE_IOERR) : 0; + }, + //xSleep is optionally defined below + xOpen: function f(pVfs, zName, pFile, flags, pOutFlags){ + const pool = getPoolForVfs(pVfs); + try{ + flags &= ~FLAG_COMPUTE_DIGEST_V2; + pool.log(`xOpen ${wasm.cstrToJs(zName)} ${flags}`); + // First try to open a path that already exists in the file system. + const path = (zName && wasm.peek8(zName)) + ? pool.getPath(zName) + : getRandomName(); + let sah = pool.getSAHForPath(path); + if(!sah && (flags & capi.SQLITE_OPEN_CREATE)) { + // File not found so try to create it. + if(pool.getFileCount() < pool.getCapacity()) { + // Choose an unassociated OPFS file from the pool. + sah = pool.nextAvailableSAH(); + pool.setAssociatedPath(sah, path, flags); + }else{ + // File pool is full. + toss('SAH pool is full. Cannot create file',path); + } + } + if(!sah){ + toss('file not found:',path); + } + // Subsequent I/O methods are only passed the sqlite3_file + // pointer, so map the relevant info we need to that pointer. + const file = {path, flags, sah}; + pool.mapS3FileToOFile(pFile, file); + file.lockType = capi.SQLITE_LOCK_NONE; + const sq3File = new capi.sqlite3_file(pFile); + sq3File.$pMethods = opfsIoMethods.pointer; + sq3File.dispose(); + wasm.poke32(pOutFlags, flags); + return 0; + }catch(e){ + pool.storeErr(e); + return capi.SQLITE_CANTOPEN; + } + }/*xOpen()*/ + }/*vfsMethods*/; + + /** + Creates and initializes an sqlite3_vfs instance for an + OpfsSAHPool. The argument is the VFS's name (JS string). + + Throws if the VFS name is already registered or if something + goes terribly wrong via sqlite3.vfs.installVfs(). + + Maintenance reminder: the only detail about the returned object + which is specific to any given OpfsSAHPool instance is the $zName + member. All other state is identical. + */ + const createOpfsVfs = function(vfsName){ + if( sqlite3.capi.sqlite3_vfs_find(vfsName)){ + toss3("VFS name is already registered:", vfsName); + } + const opfsVfs = new capi.sqlite3_vfs(); + /* We fetch the default VFS so that we can inherit some + methods from it. */ + const pDVfs = capi.sqlite3_vfs_find(null); + const dVfs = pDVfs + ? new capi.sqlite3_vfs(pDVfs) + : null /* dVfs will be null when sqlite3 is built with + SQLITE_OS_OTHER. */; + opfsVfs.$iVersion = 2/*yes, two*/; + opfsVfs.$szOsFile = capi.sqlite3_file.structInfo.sizeof; + opfsVfs.$mxPathname = HEADER_MAX_PATH_SIZE; + opfsVfs.addOnDispose( + opfsVfs.$zName = wasm.allocCString(vfsName), + ()=>setPoolForVfs(opfsVfs.pointer, 0) + ); + + if(dVfs){ + /* Inherit certain VFS members from the default VFS, + if available. */ + opfsVfs.$xRandomness = dVfs.$xRandomness; + opfsVfs.$xSleep = dVfs.$xSleep; + dVfs.dispose(); + } + if(!opfsVfs.$xRandomness && !vfsMethods.xRandomness){ + /* If the default VFS has no xRandomness(), add a basic JS impl... */ + vfsMethods.xRandomness = function(pVfs, nOut, pOut){ + const heap = wasm.heap8u(); + let i = 0; + const npOut = Number(pOut); + for(; i < nOut; ++i) heap[npOut + i] = (Math.random()*255000) & 0xFF; + return i; + }; + } + if(!opfsVfs.$xSleep && !vfsMethods.xSleep){ + vfsMethods.xSleep = (pVfs,ms)=>0; + } + sqlite3.vfs.installVfs({ + vfs: {struct: opfsVfs, methods: vfsMethods} + }); + return opfsVfs; + }; + + /** + Class for managing OPFS-related state for the + OPFS SharedAccessHandle Pool sqlite3_vfs. + */ + class OpfsSAHPool { + /* OPFS dir in which VFS metadata is stored. */ + vfsDir; + /* Directory handle to this.vfsDir. */ + #dhVfsRoot; + /* Directory handle to the subdir of this.#dhVfsRoot which holds + the randomly-named "opaque" files. This subdir exists in the + hope that we can eventually support client-created files in + this.#dhVfsRoot. */ + #dhOpaque; + /* Directory handle to this.dhVfsRoot's parent dir. Needed + for a VFS-wipe op. */ + #dhVfsParent; + /* Maps SAHs to their opaque file names. */ + #mapSAHToName = new Map(); + /* Maps client-side file names to SAHs. */ + #mapFilenameToSAH = new Map(); + /* Set of currently-unused SAHs. */ + #availableSAH = new Set(); + /* Maps (sqlite3_file*) to xOpen's file objects. */ + #mapS3FileToOFile_ = new Map(); + + /* Maps SAH to an abstract File Object which contains + various metadata about that handle. */ + + /** Buffer used by [sg]etAssociatedPath(). */ + #apBody = new Uint8Array(HEADER_CORPUS_SIZE); + // DataView for this.#apBody + #dvBody; + + // associated sqlite3_vfs instance + #cVfs; + + // Logging verbosity. See optionDefaults.verbosity. + #verbosity; + + constructor(options = Object.create(null)){ + this.#verbosity = options.verbosity ?? optionDefaults.verbosity; + this.vfsName = options.name || optionDefaults.name; + this.#cVfs = createOpfsVfs(this.vfsName); + setPoolForVfs(this.#cVfs.pointer, this); + this.vfsDir = options.directory || ("."+this.vfsName); + this.#dvBody = + new DataView(this.#apBody.buffer, this.#apBody.byteOffset); + this.isReady = this + .reset(!!(options.clearOnInit ?? optionDefaults.clearOnInit)) + .then(()=>{ + if(this.$error) throw this.$error; + return this.getCapacity() + ? Promise.resolve(undefined) + : this.addCapacity(options.initialCapacity + || optionDefaults.initialCapacity); + }); + } + + #logImpl(level,...args){ + if(this.#verbosity>level) loggers[level](this.vfsName+":",...args); + }; + log(...args){this.#logImpl(2, ...args)}; + warn(...args){this.#logImpl(1, ...args)}; + error(...args){this.#logImpl(0, ...args)}; + + getVfs(){return this.#cVfs} + + /* Current pool capacity. */ + getCapacity(){return this.#mapSAHToName.size} + + /* Current number of in-use files from pool. */ + getFileCount(){return this.#mapFilenameToSAH.size} + + /* Returns an array of the names of all + currently-opened client-specified filenames. */ + getFileNames(){ + const rc = []; + for(const n of this.#mapFilenameToSAH.keys()) rc.push(n); + return rc; + } + + /** + Adds n files to the pool's capacity. This change is + persistent across settings. Returns a Promise which resolves + to the new capacity. + */ + async addCapacity(n){ + for(let i = 0; i < n; ++i){ + const name = getRandomName(); + const h = await this.#dhOpaque.getFileHandle(name, {create:true}); + const ah = await h.createSyncAccessHandle(); + this.#mapSAHToName.set(ah,name); + this.setAssociatedPath(ah, '', 0); + //this.#createFileObject(ah,undefined,name); + } + return this.getCapacity(); + } + + /** + Reduce capacity by n, but can only reduce up to the limit + of currently-available SAHs. Returns a Promise which resolves + to the number of slots really removed. + */ + async reduceCapacity(n){ + let nRm = 0; + for(const ah of Array.from(this.#availableSAH)){ + if(nRm === n || this.getFileCount() === this.getCapacity()){ + break; + } + const name = this.#mapSAHToName.get(ah); + //this.#unmapFileObject(ah); + ah.close(); + await this.#dhOpaque.removeEntry(name); + this.#mapSAHToName.delete(ah); + this.#availableSAH.delete(ah); + ++nRm; + } + return nRm; + } + + /** + Releases all currently-opened SAHs. The only legal operation + after this is acquireAccessHandles() or (if this is called from + pauseVfs()) either of isPaused() or unpauseVfs(). + */ + releaseAccessHandles(){ + for(const ah of this.#mapSAHToName.keys()) ah.close(); + this.#mapSAHToName.clear(); + this.#mapFilenameToSAH.clear(); + this.#availableSAH.clear(); + } + + /** + Opens all files under this.vfsDir/this.#dhOpaque and acquires a + SAH for each. Returns a Promise which resolves to no value but + completes once all SAHs are acquired. If acquiring an SAH + throws, this.$error will contain the corresponding Error + object. + + If it throws, it releases any SAHs which it may have + acquired before the exception was thrown, leaving the VFS in a + well-defined but unusable state. + + If clearFiles is true, the client-stored state of each file is + cleared when its handle is acquired, including its name, flags, + and any data stored after the metadata block. + */ + async acquireAccessHandles(clearFiles=false){ + const files = []; + for await (const [name,h] of this.#dhOpaque){ + if('file'===h.kind){ + files.push([name,h]); + } + } + return Promise.all(files.map(async([name,h])=>{ + try{ + const ah = await h.createSyncAccessHandle() + this.#mapSAHToName.set(ah, name); + if(clearFiles){ + ah.truncate(HEADER_OFFSET_DATA); + this.setAssociatedPath(ah, '', 0); + }else{ + const path = this.getAssociatedPath(ah); + if(path){ + this.#mapFilenameToSAH.set(path, ah); + }else{ + this.#availableSAH.add(ah); + } + } + }catch(e){ + this.storeErr(e); + this.releaseAccessHandles(); + throw e; + } + })); + } + + /** + Given an SAH, returns the client-specified name of + that file by extracting it from the SAH's header. + + On error, it disassociates SAH from the pool and + returns an empty string. + */ + getAssociatedPath(sah){ + sah.read(this.#apBody, {at: 0}); + // Delete any unexpected files left over by previous + // untimely errors... + const flags = this.#dvBody.getUint32(HEADER_OFFSET_FLAGS); + if(this.#apBody[0] && + ((flags & capi.SQLITE_OPEN_DELETEONCLOSE) || + (flags & PERSISTENT_FILE_TYPES)===0)){ + warn(`Removing file with unexpected flags ${flags.toString(16)}`, + this.#apBody); + this.setAssociatedPath(sah, '', 0); + return ''; + } + + const fileDigest = new Uint32Array(HEADER_DIGEST_SIZE / 4); + sah.read(fileDigest, {at: HEADER_OFFSET_DIGEST}); + const compDigest = this.computeDigest(this.#apBody, flags); + //warn("getAssociatedPath() flags",'0x'+flags.toString(16), "compDigest", compDigest); + if(fileDigest.every((v,i) => v===compDigest[i])){ + // Valid digest + const pathBytes = this.#apBody.findIndex((v)=>0===v); + if(0===pathBytes){ + // This file is unassociated, so truncate it to avoid + // leaving stale db data laying around. + sah.truncate(HEADER_OFFSET_DATA); + } + //warn("getAssociatedPath() flags",'0x'+flags.toString(16), "compDigest", compDigest,"pathBytes",pathBytes); + return pathBytes + ? textDecoder.decode(this.#apBody.subarray(0,pathBytes)) + : ''; + }else{ + // Invalid digest + warn('Disassociating file with bad digest.'); + this.setAssociatedPath(sah, '', 0); + return ''; + } + } + + /** + Stores the given client-defined path and SQLITE_OPEN_xyz flags + into the given SAH. If path is an empty string then the file is + disassociated from the pool but its previous name is preserved + in the metadata. + */ + setAssociatedPath(sah, path, flags){ + const enc = textEncoder.encodeInto(path, this.#apBody); + if(HEADER_MAX_PATH_SIZE <= enc.written + 1/*NUL byte*/){ + toss("Path too long:",path); + } + if(path && flags){ + /* When creating or re-writing files, update their digest, if + needed, to v2. We continue to use v1 for the (!path) case + (empty files) because there's little reason not to use a + digest of 0 for empty entries. */ + flags |= FLAG_COMPUTE_DIGEST_V2; + } + this.#apBody.fill(0, enc.written, HEADER_MAX_PATH_SIZE); + this.#dvBody.setUint32(HEADER_OFFSET_FLAGS, flags); + const digest = this.computeDigest(this.#apBody, flags); + //console.warn("setAssociatedPath(",path,") digest",digest); + sah.write(this.#apBody, {at: 0}); + sah.write(digest, {at: HEADER_OFFSET_DIGEST}); + sah.flush(); + + if(path){ + this.#mapFilenameToSAH.set(path, sah); + this.#availableSAH.delete(sah); + }else{ + // This is not a persistent file, so eliminate the contents. + sah.truncate(HEADER_OFFSET_DATA); + this.#availableSAH.add(sah); + } + } + + /** + Computes a digest for the given byte array and returns it as a + two-element Uint32Array. This digest gets stored in the + metadata for each file as a validation check. Changing this + algorithm invalidates all existing databases for this VFS, so + don't do that. + + See the docs for FLAG_COMPUTE_DIGEST_V2 for more details. + */ + computeDigest(byteArray, fileFlags){ + if( fileFlags & FLAG_COMPUTE_DIGEST_V2 ){ + let h1 = 0xdeadbeef; + let h2 = 0x41c6ce57; + for(const v of byteArray){ + h1 = Math.imul(h1 ^ v, 2654435761); + h2 = Math.imul(h2 ^ v, 104729); + } + return new Uint32Array([h1>>>0, h2>>>0]); + }else{ + /* this is what the buggy legacy computation worked out to */ + return new Uint32Array([0,0]); + } + } + + /** + Re-initializes the state of the SAH pool, releasing and + re-acquiring all handles. + + See acquireAccessHandles() for the specifics of the clearFiles + argument. + */ + async reset(clearFiles){ + await this.isReady; + let h = await navigator.storage.getDirectory(); + let prev, prevName; + for(const d of this.vfsDir.split('/')){ + if(d){ + prev = h; + h = await h.getDirectoryHandle(d,{create:true}); + } + } + this.#dhVfsRoot = h; + this.#dhVfsParent = prev; + this.#dhOpaque = await this.#dhVfsRoot.getDirectoryHandle( + OPAQUE_DIR_NAME,{create:true} + ); + this.releaseAccessHandles(); + return this.acquireAccessHandles(clearFiles); + } + + /** + Returns the pathname part of the given argument, + which may be any of: + + - a URL object + - A JS string representing a file name + - Wasm C-string representing a file name + + All "../" parts and duplicate slashes are resolve/removed from + the returned result. + */ + getPath(arg) { + if(wasm.isPtr(arg)) arg = wasm.cstrToJs(arg); + return ((arg instanceof URL) + ? arg + : new URL(arg, 'file://localhost/')).pathname; + } + + /** + Removes the association of the given client-specified file + name (JS string) from the pool. Returns true if a mapping + is found, else false. + */ + deletePath(path) { + const sah = this.#mapFilenameToSAH.get(path); + if(sah) { + // Un-associate the name from the SAH. + this.#mapFilenameToSAH.delete(path); + this.setAssociatedPath(sah, '', 0); + } + return !!sah; + } + + /** + Sets e (an Error object) as this object's current error. Pass a + falsy (or no) value to clear it. If code is truthy it is + assumed to be an SQLITE_xxx result code, defaulting to + SQLITE_IOERR if code is falsy. + + Returns the 2nd argument. + */ + storeErr(e,code){ + if(e){ + e.sqlite3Rc = code || capi.SQLITE_IOERR; + this.error(e); + } + this.$error = e; + return code; + } + /** + Pops this object's Error object and returns + it (a falsy value if no error is set). + */ + popErr(){ + const rc = this.$error; + this.$error = undefined; + return rc; + } + + /** + Returns the next available SAH without removing + it from the set. + */ + nextAvailableSAH(){ + const [rc] = this.#availableSAH.keys(); + return rc; + } + + /** + Given an (sqlite3_file*), returns the mapped + xOpen file object. + */ + getOFileForS3File(pFile){ + return this.#mapS3FileToOFile_.get(pFile); + } + /** + Maps or unmaps (if file is falsy) the given (sqlite3_file*) + to an xOpen file object and to this pool object. + */ + mapS3FileToOFile(pFile,file){ + if(file){ + this.#mapS3FileToOFile_.set(pFile, file); + setPoolForPFile(pFile, this); + }else{ + this.#mapS3FileToOFile_.delete(pFile); + setPoolForPFile(pFile, false); + } + } + + /** + Returns true if the given client-defined file name is in this + object's name-to-SAH map. + */ + hasFilename(name){ + return this.#mapFilenameToSAH.has(name) + } + + /** + Returns the SAH associated with the given + client-defined file name. + */ + getSAHForPath(path){ + return this.#mapFilenameToSAH.get(path); + } + + /** + Removes this object's sqlite3_vfs registration and shuts down + this object, releasing all handles, mappings, and whatnot, + including deleting its data directory. There is currently no + way to "revive" the object and reaquire its + resources. Similarly, there is no recovery strategy if removal + of any given SAH fails, so such errors are ignored by this + function. + + This function is intended primarily for testing. + + Resolves to true if it did its job, false if the + VFS has already been shut down. + + @see pauseVfs() + @see unpauseVfs() + */ + async removeVfs(){ + if(!this.#cVfs.pointer || !this.#dhOpaque) return false; + capi.sqlite3_vfs_unregister(this.#cVfs.pointer); + this.#cVfs.dispose(); + delete initPromises[this.vfsName]; + try{ + this.releaseAccessHandles(); + await this.#dhVfsRoot.removeEntry(OPAQUE_DIR_NAME, {recursive: true}); + this.#dhOpaque = undefined; + await this.#dhVfsParent.removeEntry( + this.#dhVfsRoot.name, {recursive: true} + ); + this.#dhVfsRoot = this.#dhVfsParent = undefined; + }catch(e){ + sqlite3.config.error(this.vfsName,"removeVfs() failed with no recovery strategy:",e); + /*otherwise ignored - there is no recovery strategy*/ + } + return true; + } + + + /** + "Pauses" this VFS by unregistering it from SQLite and + relinquishing all open SAHs, leaving the associated files + intact. If this object is already paused, this is a + no-op. Returns this object. + + This function throws if SQLite has any opened file handles + hosted by this VFS, as the alternative would be to invoke + Undefined Behavior by closing file handles out from under the + library. Similarly, automatically closing any database handles + opened by this VFS would invoke Undefined Behavior in + downstream code which is holding those pointers. + + If this function throws due to open file handles then it has + no side effects. If the OPFS API throws while closing handles + then the VFS is left in an undefined state. + + @see isPaused() + @see unpauseVfs() + */ + pauseVfs(){ + if(this.#mapS3FileToOFile_.size>0){ + sqlite3.SQLite3Error.toss( + capi.SQLITE_MISUSE, "Cannot pause VFS", + this.vfsName,"because it has opened files." + ); + } + if(this.#mapSAHToName.size>0){ + capi.sqlite3_vfs_unregister(this.vfsName); + this.releaseAccessHandles(); + } + return this; + } + + /** + Returns true if this pool is currently paused else false. + + @see pauseVfs() + @see unpauseVfs() + */ + isPaused(){ + return 0===this.#mapSAHToName.size; + } + + /** + "Unpauses" this VFS, reacquiring all SAH's and (if successful) + re-registering it with SQLite. This is a no-op if the VFS is + not currently paused. + + The returned Promise resolves to this object. See + acquireAccessHandles() for how it behaves if it throws due to + SAH acquisition failure. + + @see isPaused() + @see pauseVfs() + */ + async unpauseVfs(){ + if(0===this.#mapSAHToName.size){ + return this.acquireAccessHandles(false). + then(()=>capi.sqlite3_vfs_register(this.#cVfs, 0),this); + } + return this; + } + + //! Documented elsewhere in this file. + exportFile(name){ + const sah = this.#mapFilenameToSAH.get(name) || toss("File not found:",name); + const n = sah.getSize() - HEADER_OFFSET_DATA; + const b = new Uint8Array(n>0 ? n : 0); + if(n>0){ + const nRead = sah.read(b, {at: HEADER_OFFSET_DATA}); + if(nRead != n){ + toss("Expected to read "+n+" bytes but read "+nRead+"."); + } + } + return b; + } + + //! Impl for importDb() when its 2nd arg is a function. + async importDbChunked(name, callback){ + const sah = this.#mapFilenameToSAH.get(name) + || this.nextAvailableSAH() + || toss("No available handles to import to."); + sah.truncate(0); + let nWrote = 0, chunk, checkedHeader = false, err = false; + try{ + while( undefined !== (chunk = await callback()) ){ + if(chunk instanceof ArrayBuffer) chunk = new Uint8Array(chunk); + if( !checkedHeader && 0===nWrote && chunk.byteLength>=15 ){ + util.affirmDbHeader(chunk); + checkedHeader = true; + } + sah.write(chunk, {at: HEADER_OFFSET_DATA + nWrote}); + nWrote += chunk.byteLength; + } + if( nWrote < 512 || 0!==nWrote % 512 ){ + toss("Input size",nWrote,"is not correct for an SQLite database."); + } + if( !checkedHeader ){ + const header = new Uint8Array(20); + sah.read( header, {at: 0} ); + util.affirmDbHeader( header ); + } + sah.write(new Uint8Array([1,1]), { + at: HEADER_OFFSET_DATA + 18 + }/*force db out of WAL mode*/); + }catch(e){ + this.setAssociatedPath(sah, '', 0); + throw e; + } + this.setAssociatedPath(sah, name, capi.SQLITE_OPEN_MAIN_DB); + return nWrote; + } + + //! Documented elsewhere in this file. + importDb(name, bytes){ + if( bytes instanceof ArrayBuffer ) bytes = new Uint8Array(bytes); + else if( bytes instanceof Function ) return this.importDbChunked(name, bytes); + const sah = this.#mapFilenameToSAH.get(name) + || this.nextAvailableSAH() + || toss("No available handles to import to."); + const n = bytes.byteLength; + if(n<512 || n%512!=0){ + toss("Byte array size is invalid for an SQLite db."); + } + const header = "SQLite format 3"; + for(let i = 0; i < header.length; ++i){ + if( header.charCodeAt(i) !== bytes[i] ){ + toss("Input does not contain an SQLite database header."); + } + } + const nWrote = sah.write(bytes, {at: HEADER_OFFSET_DATA}); + if(nWrote != n){ + this.setAssociatedPath(sah, '', 0); + toss("Expected to write "+n+" bytes but wrote "+nWrote+"."); + }else{ + sah.write(new Uint8Array([1,1]), {at: HEADER_OFFSET_DATA+18} + /* force db out of WAL mode */); + this.setAssociatedPath(sah, name, capi.SQLITE_OPEN_MAIN_DB); + } + return nWrote; + } + + }/*class OpfsSAHPool*/; + + + /** + A OpfsSAHPoolUtil instance is exposed to clients in order to + manipulate an OpfsSAHPool object without directly exposing that + object and allowing for some semantic changes compared to that + class. + + Class docs are in the client-level docs for + installOpfsSAHPoolVfs(). + */ + class OpfsSAHPoolUtil { + /* This object's associated OpfsSAHPool. */ + #p; + + constructor(sahPool){ + this.#p = sahPool; + this.vfsName = sahPool.vfsName; + } + + async addCapacity(n){ return this.#p.addCapacity(n) } + + async reduceCapacity(n){ return this.#p.reduceCapacity(n) } + + getCapacity(){ return this.#p.getCapacity(this.#p) } + + getFileCount(){ return this.#p.getFileCount() } + getFileNames(){ return this.#p.getFileNames() } + + async reserveMinimumCapacity(min){ + const c = this.#p.getCapacity(); + return (c < min) ? this.#p.addCapacity(min - c) : c; + } + + exportFile(name){ return this.#p.exportFile(name) } + + importDb(name, bytes){ return this.#p.importDb(name,bytes) } + + async wipeFiles(){ return this.#p.reset(true) } + + unlink(filename){ return this.#p.deletePath(filename) } + + async removeVfs(){ return this.#p.removeVfs() } + + pauseVfs(){ this.#p.pauseVfs(); return this; } + async unpauseVfs(){ return this.#p.unpauseVfs().then(()=>this); } + isPaused(){ return this.#p.isPaused() } + + }/* class OpfsSAHPoolUtil */; + + /** + Returns a resolved Promise if the current environment + has a "fully-sync" SAH impl, else a rejected Promise. + */ + const apiVersionCheck = async ()=>{ + const dh = await navigator.storage.getDirectory(); + const fn = '.opfs-sahpool-sync-check-'+getRandomName(); + const fh = await dh.getFileHandle(fn, { create: true }); + const ah = await fh.createSyncAccessHandle(); + const close = ah.close(); + await close; + await dh.removeEntry(fn); + if(close?.then){ + toss("The local OPFS API is too old for opfs-sahpool:", + "it has an async FileSystemSyncAccessHandle.close() method."); + } + return true; + }; + + /** + installOpfsSAHPoolVfs() asynchronously initializes the OPFS + SyncAccessHandle (a.k.a. SAH) Pool VFS. It returns a Promise which + either resolves to a utility object described below or rejects with + an Error value. + + Initialization of this VFS is not automatic because its + registration requires that it lock all resources it + will potentially use, even if client code does not want + to use them. That, in turn, can lead to locking errors + when, for example, one page in a given origin has loaded + this VFS but does not use it, then another page in that + origin tries to use the VFS. If the VFS were automatically + registered, the second page would fail to load the VFS + due to OPFS locking errors. + + If this function is called more than once with a given "name" + option (see below), it will return the same Promise. Calls for + different names will return different Promises which resolve to + independent objects and refer to different VFS registrations. + + On success, the resulting Promise resolves to a utility object + which can be used to query and manipulate the pool. Its API is + described at the end of these docs. + + This function accepts an options object to configure certain + parts but it is only acknowledged for the very first call and + ignored for all subsequent calls. + + The options, in alphabetical order: + + - `clearOnInit`: (default=false) if truthy, contents and filename + mapping are removed from each SAH it is acquired during + initialization of the VFS, leaving the VFS's storage in a pristine + state. Use this only for databases which need not survive a page + reload. + + - `initialCapacity`: (default=6) Specifies the default capacity of + the VFS. This should not be set unduly high because the VFS has + to open (and keep open) a file for each entry in the pool. This + setting only has an effect when the pool is initially empty. It + does not have any effect if a pool already exists. + + - `directory`: (default="."+`name`) Specifies the OPFS directory + name in which to store metadata for the `"opfs-sahpool"` + sqlite3_vfs. Only one instance of this VFS can be installed per + JavaScript engine, and any two engines with the same storage + directory name will collide with each other, leading to locking + errors and the inability to register the VFS in the second and + subsequent engine. Using a different directory name for each + application enables different engines in the same HTTP origin to + co-exist, but their data are invisible to each other. Changing + this name will effectively orphan any databases stored under + previous names. The default is unspecified but descriptive. This + option may contain multiple path elements, e.g. "foo/bar/baz", + and they are created automatically. In practice there should be + no driving need to change this. ACHTUNG: all files in this + directory are assumed to be managed by the VFS. Do not place + other files in that directory, as they may be deleted or + otherwise modified by the VFS. + + - `name`: (default="opfs-sahpool") sets the name to register this + VFS under. Normally this should not be changed, but it is + possible to register this VFS under multiple names so long as + each has its own separate directory to work from. The storage for + each is invisible to all others. The name must be a string + compatible with `sqlite3_vfs_register()` and friends and suitable + for use in URI-style database file names. + + Achtung: if a custom `name` is provided, a custom `directory` + must also be provided if any other instance is registered with + the default directory. If no directory is explicitly provided + then a directory name is synthesized from the `name` option. + + + - `forceReinitIfPreviouslyFailed`: (default=`false`) Is a fallback option + to assist in working around certain flaky environments which may + mysteriously fail to permit access to OPFS sync access handles on + an initial attempt but permit it on a second attemp. This option + should never be used but is provided for those who choose to + throw caution to the wind and trust such environments. If this + option is truthy _and_ the previous attempt to initialize this + VFS with the same `name` failed, the VFS will attempt to + initialize a second time instead of returning the cached + failure. See discussion at: + + + + Peculiarities of this VFS vis a vis other SQLite VFSes: + + - Paths given to it _must_ be absolute. Relative paths will not + be properly recognized. This is arguably a bug but correcting it + requires some hoop-jumping in routines which have no business + doing such tricks. + + - It is possible to install multiple instances under different + names, each sandboxed from one another inside their own private + directory. This feature exists primarily as a way for disparate + applications within a given HTTP origin to use this VFS without + introducing locking issues between them. + + + The API for the utility object passed on by this function's + Promise, in alphabetical order... + + - [async] number addCapacity(n) + + Adds `n` entries to the current pool. This change is persistent + across sessions so should not be called automatically at each app + startup (but see `reserveMinimumCapacity()`). Its returned Promise + resolves to the new capacity. Because this operation is necessarily + asynchronous, the C-level VFS API cannot call this on its own as + needed. + + - byteArray exportFile(name) + + Synchronously reads the contents of the given file into a Uint8Array + and returns it. This will throw if the given name is not currently + in active use or on I/O error. Note that the given name is _not_ + visible directly in OPFS (or, if it is, it's not from this VFS). + + - number getCapacity() + + Returns the number of files currently contained + in the SAH pool. The default capacity is only large enough for one + or two databases and their associated temp files. + + - number getFileCount() + + Returns the number of files from the pool currently allocated to + slots. This is not the same as the files being "opened". + + - array getFileNames() + + Returns an array of the names of the files currently allocated to + slots. This list is the same length as getFileCount(). + + - void importDb(name, bytes) + + Imports the contents of an SQLite database, provided as a byte + array or ArrayBuffer, under the given name, overwriting any + existing content. Throws if the pool has no available file slots, + on I/O error, or if the input does not appear to be a + database. In the latter case, only a cursory examination is made. + Results are undefined if the given db name refers to an opened + db. Note that this routine is _only_ for importing database + files, not arbitrary files, the reason being that this VFS will + automatically clean up any non-database files so importing them + is pointless. + + If passed a function for its second argument, its behavior + changes to asynchronous and it imports its data in chunks fed to + it by the given callback function. It calls the callback (which + may be async) repeatedly, expecting either a Uint8Array or + ArrayBuffer (to denote new input) or undefined (to denote + EOF). For so long as the callback continues to return + non-undefined, it will append incoming data to the given + VFS-hosted database file. The result of the resolved Promise when + called this way is the size of the resulting database. + + On success this routine rewrites the database header bytes in the + output file (not the input array) to force disabling of WAL mode. + + On a write error, the handle is removed from the pool and made + available for re-use. + + - [async] number reduceCapacity(n) + + Removes up to `n` entries from the pool, with the caveat that it can + only remove currently-unused entries. It returns a Promise which + resolves to the number of entries actually removed. + + - [async] boolean removeVfs() + + Unregisters the opfs-sahpool VFS and removes its directory from OPFS + (which means that _all client content_ is removed). After calling + this, the VFS may no longer be used and there is no way to re-add it + aside from reloading the current JavaScript context. + + Results are undefined if a database is currently in use with this + VFS. + + The returned Promise resolves to true if it performed the removal + and false if the VFS was not installed. + + If the VFS has a multi-level directory, e.g. "/foo/bar/baz", _only_ + the bottom-most directory is removed because this VFS cannot know for + certain whether the higher-level directories contain data which + should be removed. + + - [async] number reserveMinimumCapacity(min) + + If the current capacity is less than `min`, the capacity is + increased to `min`, else this returns with no side effects. The + resulting Promise resolves to the new capacity. + + - boolean unlink(filename) + + If a virtual file exists with the given name, disassociates it from + the pool and returns true, else returns false without side + effects. Results are undefined if the file is currently in active + use. + + - string vfsName + + The SQLite VFS name under which this pool's VFS is registered. + + - [async] void wipeFiles() + + Clears all client-defined state of all SAHs and makes all of them + available for re-use by the pool. Results are undefined if any such + handles are currently in use, e.g. by an sqlite3 db. + + APIs specific to the "pause" capability (added in version 3.49): + + Summary: "pausing" the VFS disassociates it from SQLite and + relinquishes its SAHs so that they may be opened by another + instance of this VFS (running in a separate tab/page or Worker). + "Unpausing" it takes back control, if able. + + - pauseVfs() + + "Pauses" this VFS by unregistering it from SQLite and + relinquishing all open SAHs, leaving the associated files intact. + This enables pages/tabs to coordinate semi-concurrent usage of + this VFS. If this object is already paused, this is a + no-op. Returns this object. Throws if SQLite has any opened file + handles hosted by this VFS. If this function throws due to open + file handles then it has no side effects. If the OPFS API throws + while closing handles then the VFS is left in an undefined state. + + - isPaused() + + Returns true if this VFS is paused, else false. + + - [async] unpauseVfs() + + Restores the VFS to an active state after having called + pauseVfs() on it. This is a no-op if the VFS is not paused. The + returned Promise resolves to this object on success. A rejected + Promise means there was a problem reacquiring the SAH handles + (possibly because they're in use by another instance or have + since been removed). Generically speaking, there is no recovery + strategy for that type of error, but if the problem is simply + that the OPFS files are locked, then a later attempt to unpause + it, made after the concurrent instance releases the SAHs, may + recover from the situation. + */ + sqlite3.installOpfsSAHPoolVfs = async function(options=Object.create(null)){ + options = Object.assign(Object.create(null), optionDefaults, (options||{})); + const vfsName = options.name; + if(options.$testThrowPhase1){ + throw options.$testThrowPhase1; + } + if(initPromises[vfsName]){ + try { + const p = await initPromises[vfsName]; + //log("installOpfsSAHPoolVfs() returning cached result",options,vfsName,p); + return p; + }catch(e){ + //log("installOpfsSAHPoolVfs() got cached failure",options,vfsName,e); + if( options.forceReinitIfPreviouslyFailed ){ + delete initPromises[vfsName]; + /* Fall through and try again. */ + }else{ + throw e; + } + } + } + if(!globalThis.FileSystemHandle || + !globalThis.FileSystemDirectoryHandle || + !globalThis.FileSystemFileHandle || + !globalThis.FileSystemFileHandle.prototype.createSyncAccessHandle || + !navigator?.storage?.getDirectory){ + return (initPromises[vfsName] = Promise.reject(new Error("Missing required OPFS APIs."))); + } + + /** + Maintenance reminder: the order of ASYNC ops in this function + is significant. We need to have them all chained at the very + end in order to be able to catch a race condition where + installOpfsSAHPoolVfs() is called twice in rapid succession, + e.g.: + + installOpfsSAHPoolVfs().then(console.warn.bind(console)); + installOpfsSAHPoolVfs().then(console.warn.bind(console)); + + If the timing of the async calls is not "just right" then that + second call can end up triggering the init a second time and chaos + ensues. + */ + return initPromises[vfsName] = apiVersionCheck().then(async function(){ + if(options.$testThrowPhase2){ + throw options.$testThrowPhase2; + } + const thePool = new OpfsSAHPool(options); + return thePool.isReady.then(async()=>{ + /** The poolUtil object will be the result of the + resolved Promise. */ + const poolUtil = new OpfsSAHPoolUtil(thePool); + if(sqlite3.oo1){ + const oo1 = sqlite3.oo1; + const theVfs = thePool.getVfs(); + const OpfsSAHPoolDb = function(...args){ + const opt = oo1.DB.dbCtorHelper.normalizeArgs(...args); + opt.vfs = theVfs.$zName; + oo1.DB.dbCtorHelper.call(this, opt); + }; + OpfsSAHPoolDb.prototype = Object.create(oo1.DB.prototype); + poolUtil.OpfsSAHPoolDb = OpfsSAHPoolDb; + }/*extend sqlite3.oo1*/ + thePool.log("VFS initialized."); + return poolUtil; + }).catch(async (e)=>{ + await thePool.removeVfs().catch(()=>{}); + throw e; + }); + }).catch((err)=>{ + //error("rejecting promise:",err); + return initPromises[vfsName] = Promise.reject(err); + }); + }/*installOpfsSAHPoolVfs()*/; +}/*sqlite3ApiBootstrap.initializers*/); +//#else +/* + The OPFS SAH Pool VFS parts are elided from builds targeting + node.js. +*/ +//#endif target:node diff --git a/ext/wasm/api/sqlite3-vfs-opfs.c-pp.js b/ext/wasm/api/sqlite3-vfs-opfs.c-pp.js new file mode 100644 index 0000000000..2b636460dd --- /dev/null +++ b/ext/wasm/api/sqlite3-vfs-opfs.c-pp.js @@ -0,0 +1,1460 @@ +//#if not target:node +/* + 2022-09-18 + + The author disclaims copyright to this source code. In place of a + legal notice, here is a blessing: + + * May you do good and not evil. + * May you find forgiveness for yourself and forgive others. + * May you share freely, never taking more than you give. + + *********************************************************************** + + This file holds the synchronous half of an sqlite3_vfs + implementation which proxies, in a synchronous fashion, the + asynchronous Origin-Private FileSystem (OPFS) APIs using a second + Worker, implemented in sqlite3-opfs-async-proxy.js. This file is + intended to be appended to the main sqlite3 JS deliverable somewhere + after sqlite3-api-oo1.js and before sqlite3-api-cleanup.js. +*/ +'use strict'; +globalThis.sqlite3ApiBootstrap.initializers.push(function(sqlite3){ +/** + installOpfsVfs() returns a Promise which, on success, installs an + sqlite3_vfs named "opfs", suitable for use with all sqlite3 APIs + which accept a VFS. It is intended to be called via + sqlite3ApiBootstrap.initializers or an equivalent mechanism. + + The installed VFS uses the Origin-Private FileSystem API for + all file storage. On error it is rejected with an exception + explaining the problem. Reasons for rejection include, but are + not limited to: + + - The counterpart Worker (see below) could not be loaded. + + - The environment does not support OPFS. That includes when + this function is called from the main window thread. + + Significant notes and limitations: + + - The OPFS features used here are only available in dedicated Worker + threads. This file tries to detect that case, resulting in a + rejected Promise if those features do not seem to be available. + + - It requires the SharedArrayBuffer and Atomics classes, and the + former is only available if the HTTP server emits the so-called + COOP and COEP response headers. These features are required for + proxying OPFS's synchronous API via the synchronous interface + required by the sqlite3_vfs API. + + - This function may only be called a single time. When called, this + function removes itself from the sqlite3 object. + + All arguments to this function are for internal/development purposes + only. They do not constitute a public API and may change at any + time. + + The argument may optionally be a plain object with the following + configuration options: + + - proxyUri: name of the async proxy JS file. + + - verbose (=2): an integer 0-3. 0 disables all logging, 1 enables + logging of errors. 2 enables logging of warnings and errors. 3 + additionally enables debugging info. Logging is performed + via the sqlite3.config.{log|warn|error}() functions. + + - sanityChecks (=false): if true, some basic sanity tests are run on + the OPFS VFS API after it's initialized, before the returned + Promise resolves. This is only intended for testing and + development of the VFS, not client-side use. + + On success, the Promise resolves to the top-most sqlite3 namespace + object and that object gets a new object installed in its + `opfs` property, containing several OPFS-specific utilities. +*/ +const installOpfsVfs = function callee(options){ + if(!globalThis.SharedArrayBuffer + || !globalThis.Atomics){ + return Promise.reject( + new Error("Cannot install OPFS: Missing SharedArrayBuffer and/or Atomics. "+ + "The server must emit the COOP/COEP response headers to enable those. "+ + "See https://sqlite.org/wasm/doc/trunk/persistence.md#coop-coep") + ); + }else if('undefined'===typeof WorkerGlobalScope){ + return Promise.reject( + new Error("The OPFS sqlite3_vfs cannot run in the main thread "+ + "because it requires Atomics.wait().") + ); + }else if(!globalThis.FileSystemHandle || + !globalThis.FileSystemDirectoryHandle || + !globalThis.FileSystemFileHandle || + !globalThis.FileSystemFileHandle.prototype.createSyncAccessHandle || + !navigator?.storage?.getDirectory){ + return Promise.reject( + new Error("Missing required OPFS APIs.") + ); + } + if(!options || 'object'!==typeof options){ + options = Object.create(null); + } + const urlParams = new URL(globalThis.location.href).searchParams; + if(urlParams.has('opfs-disable')){ + //sqlite3.config.warn('Explicitly not installing "opfs" VFS due to opfs-disable flag.'); + return Promise.resolve(sqlite3); + } + if(undefined===options.verbose){ + options.verbose = urlParams.has('opfs-verbose') + ? (+urlParams.get('opfs-verbose') || 2) : 1; + } + if(undefined===options.sanityChecks){ + options.sanityChecks = urlParams.has('opfs-sanity-check'); + } + if(undefined===options.proxyUri){ + options.proxyUri = callee.defaultProxyUri; + } + + //sqlite3.config.warn("OPFS options =",options,globalThis.location); + + if('function' === typeof options.proxyUri){ + options.proxyUri = options.proxyUri(); + } + const thePromise = new Promise(function(promiseResolve_, promiseReject_){ + const loggers = [ + sqlite3.config.error, + sqlite3.config.warn, + sqlite3.config.log + ]; + const logImpl = (level,...args)=>{ + if(options.verbose>level) loggers[level]("OPFS syncer:",...args); + }; + const log = (...args)=>logImpl(2, ...args); + const warn = (...args)=>logImpl(1, ...args); + const error = (...args)=>logImpl(0, ...args); + const toss = sqlite3.util.toss; + const capi = sqlite3.capi; + const util = sqlite3.util; + const wasm = sqlite3.wasm; + const sqlite3_vfs = capi.sqlite3_vfs; + const sqlite3_file = capi.sqlite3_file; + const sqlite3_io_methods = capi.sqlite3_io_methods; + /** + Generic utilities for working with OPFS. This will get filled out + by the Promise setup and, on success, installed as sqlite3.opfs. + + ACHTUNG: do not rely on these APIs in client code. They are + experimental and subject to change or removal as the + OPFS-specific sqlite3_vfs evolves. + */ + const opfsUtil = Object.create(null); + + /** + Returns true if _this_ thread has access to the OPFS APIs. + */ + const thisThreadHasOPFS = ()=>{ + return globalThis.FileSystemHandle && + globalThis.FileSystemDirectoryHandle && + globalThis.FileSystemFileHandle && + globalThis.FileSystemFileHandle.prototype.createSyncAccessHandle && + navigator?.storage?.getDirectory; + }; + + /** + Not part of the public API. Solely for internal/development + use. + */ + opfsUtil.metrics = { + dump: function(){ + let k, n = 0, t = 0, w = 0; + for(k in state.opIds){ + const m = metrics[k]; + n += m.count; + t += m.time; + w += m.wait; + m.avgTime = (m.count && m.time) ? (m.time / m.count) : 0; + m.avgWait = (m.count && m.wait) ? (m.wait / m.count) : 0; + } + sqlite3.config.log(globalThis.location.href, + "metrics for",globalThis.location.href,":",metrics, + "\nTotal of",n,"op(s) for",t, + "ms (incl. "+w+" ms of waiting on the async side)"); + sqlite3.config.log("Serialization metrics:",metrics.s11n); + W.postMessage({type:'opfs-async-metrics'}); + }, + reset: function(){ + let k; + const r = (m)=>(m.count = m.time = m.wait = 0); + for(k in state.opIds){ + r(metrics[k] = Object.create(null)); + } + let s = metrics.s11n = Object.create(null); + s = s.serialize = Object.create(null); + s.count = s.time = 0; + s = metrics.s11n.deserialize = Object.create(null); + s.count = s.time = 0; + } + }/*metrics*/; + const opfsIoMethods = new sqlite3_io_methods(); + const opfsVfs = new sqlite3_vfs() + .addOnDispose( ()=>opfsIoMethods.dispose()); + let promiseWasRejected = undefined; + const promiseReject = (err)=>{ + promiseWasRejected = true; + opfsVfs.dispose(); + return promiseReject_(err); + }; + const promiseResolve = ()=>{ + promiseWasRejected = false; + return promiseResolve_(sqlite3); + }; + const W = +//#if target:es6-bundler-friendly + new Worker(new URL("sqlite3-opfs-async-proxy.js", import.meta.url)); +//#elif target:es6-module + new Worker(new URL(options.proxyUri, import.meta.url)); +//#else + new Worker(options.proxyUri); +//#endif + setTimeout(()=>{ + /* At attempt to work around a browser-specific quirk in which + the Worker load is failing in such a way that we neither + resolve nor reject it. This workaround gives that resolve/reject + a time limit and rejects if that timer expires. Discussion: + https://sqlite.org/forum/forumpost/a708c98dcb3ef */ + if(undefined===promiseWasRejected){ + promiseReject( + new Error("Timeout while waiting for OPFS async proxy worker.") + ); + } + }, 4000); + W._originalOnError = W.onerror /* will be restored later */; + W.onerror = function(err){ + // The error object doesn't contain any useful info when the + // failure is, e.g., that the remote script is 404. + error("Error initializing OPFS asyncer:",err); + promiseReject(new Error("Loading OPFS async Worker failed for unknown reasons.")); + }; + const pDVfs = capi.sqlite3_vfs_find(null)/*pointer to default VFS*/; + const dVfs = pDVfs + ? new sqlite3_vfs(pDVfs) + : null /* dVfs will be null when sqlite3 is built with + SQLITE_OS_OTHER. */; + opfsIoMethods.$iVersion = 1; + opfsVfs.$iVersion = 2/*yes, two*/; + opfsVfs.$szOsFile = capi.sqlite3_file.structInfo.sizeof; + opfsVfs.$mxPathname = 1024/* sure, why not? The OPFS name length limit + is undocumented/unspecified. */; + opfsVfs.$zName = wasm.allocCString("opfs"); + // All C-side memory of opfsVfs is zeroed out, but just to be explicit: + opfsVfs.$xDlOpen = opfsVfs.$xDlError = opfsVfs.$xDlSym = opfsVfs.$xDlClose = null; + opfsVfs.addOnDispose( + '$zName', opfsVfs.$zName, + 'cleanup default VFS wrapper', ()=>(dVfs ? dVfs.dispose() : null) + ); + /** + Pedantic sidebar about opfsVfs.ondispose: the entries in that array + are items to clean up when opfsVfs.dispose() is called, but in this + environment it will never be called. The VFS instance simply + hangs around until the WASM module instance is cleaned up. We + "could" _hypothetically_ clean it up by "importing" an + sqlite3_os_end() impl into the wasm build, but the shutdown order + of the wasm engine and the JS one are undefined so there is no + guaranty that the opfsVfs instance would be available in one + environment or the other when sqlite3_os_end() is called (_if_ it + gets called at all in a wasm build, which is undefined). + */ + /** + State which we send to the async-api Worker or share with it. + This object must initially contain only cloneable or sharable + objects. After the worker's "inited" message arrives, other types + of data may be added to it. + + For purposes of Atomics.wait() and Atomics.notify(), we use a + SharedArrayBuffer with one slot reserved for each of the API + proxy's methods. The sync side of the API uses Atomics.wait() + on the corresponding slot and the async side uses + Atomics.notify() on that slot. + + The approach of using a single SAB to serialize comms for all + instances might(?) lead to deadlock situations in multi-db + cases. We should probably have one SAB here with a single slot + for locking a per-file initialization step and then allocate a + separate SAB like the above one for each file. That will + require a bit of acrobatics but should be feasible. The most + problematic part is that xOpen() would have to use + postMessage() to communicate its SharedArrayBuffer, and mixing + that approach with Atomics.wait/notify() gets a bit messy. + */ + const state = Object.create(null); + state.verbose = options.verbose; + state.littleEndian = (()=>{ + const buffer = new ArrayBuffer(2); + new DataView(buffer).setInt16(0, 256, true /* ==>littleEndian */); + // Int16Array uses the platform's endianness. + return new Int16Array(buffer)[0] === 256; + })(); + /** + asyncIdleWaitTime is how long (ms) to wait, in the async proxy, + for each Atomics.wait() when waiting on inbound VFS API calls. + We need to wake up periodically to give the thread a chance to + do other things. If this is too high (e.g. 500ms) then even two + workers/tabs can easily run into locking errors. Some multiple + of this value is also used for determining how long to wait on + lock contention to free up. + */ + state.asyncIdleWaitTime = 150; + + /** + Whether the async counterpart should log exceptions to + the serialization channel. That produces a great deal of + noise for seemingly innocuous things like xAccess() checks + for missing files, so this option may have one of 3 values: + + 0 = no exception logging. + + 1 = only log exceptions for "significant" ops like xOpen(), + xRead(), and xWrite(). + + 2 = log all exceptions. + */ + state.asyncS11nExceptions = 1; + /* Size of file I/O buffer block. 64k = max sqlite3 page size, and + xRead/xWrite() will never deal in blocks larger than that. */ + state.fileBufferSize = 1024 * 64; + state.sabS11nOffset = state.fileBufferSize; + /** + The size of the block in our SAB for serializing arguments and + result values. Needs to be large enough to hold serialized + values of any of the proxied APIs. Filenames are the largest + part but are limited to opfsVfs.$mxPathname bytes. We also + store exceptions there, so it needs to be long enough to hold + a reasonably long exception string. + */ + state.sabS11nSize = opfsVfs.$mxPathname * 2; + /** + The SAB used for all data I/O between the synchronous and + async halves (file i/o and arg/result s11n). + */ + state.sabIO = new SharedArrayBuffer( + state.fileBufferSize/* file i/o block */ + + state.sabS11nSize/* argument/result serialization block */ + ); + state.opIds = Object.create(null); + const metrics = Object.create(null); + { + /* Indexes for use in our SharedArrayBuffer... */ + let i = 0; + /* SAB slot used to communicate which operation is desired + between both workers. This worker writes to it and the other + listens for changes. */ + state.opIds.whichOp = i++; + /* Slot for storing return values. This worker listens to that + slot and the other worker writes to it. */ + state.opIds.rc = i++; + /* Each function gets an ID which this worker writes to + the whichOp slot. The async-api worker uses Atomic.wait() + on the whichOp slot to figure out which operation to run + next. */ + state.opIds.xAccess = i++; + state.opIds.xClose = i++; + state.opIds.xDelete = i++; + state.opIds.xDeleteNoWait = i++; + state.opIds.xFileSize = i++; + state.opIds.xLock = i++; + state.opIds.xOpen = i++; + state.opIds.xRead = i++; + state.opIds.xSleep = i++; + state.opIds.xSync = i++; + state.opIds.xTruncate = i++; + state.opIds.xUnlock = i++; + state.opIds.xWrite = i++; + state.opIds.mkdir = i++; + state.opIds['opfs-async-metrics'] = i++; + state.opIds['opfs-async-shutdown'] = i++; + /* The retry slot is used by the async part for wait-and-retry + semantics. Though we could hypothetically use the xSleep slot + for that, doing so might lead to undesired side effects. */ + state.opIds.retry = i++; + state.sabOP = new SharedArrayBuffer( + i * 4/* ==sizeof int32, noting that Atomics.wait() and friends + can only function on Int32Array views of an SAB. */); + opfsUtil.metrics.reset(); + } + /** + SQLITE_xxx constants to export to the async worker + counterpart... + */ + state.sq3Codes = Object.create(null); + [ + 'SQLITE_ACCESS_EXISTS', + 'SQLITE_ACCESS_READWRITE', + 'SQLITE_BUSY', + 'SQLITE_CANTOPEN', + 'SQLITE_ERROR', + 'SQLITE_IOERR', + 'SQLITE_IOERR_ACCESS', + 'SQLITE_IOERR_CLOSE', + 'SQLITE_IOERR_DELETE', + 'SQLITE_IOERR_FSYNC', + 'SQLITE_IOERR_LOCK', + 'SQLITE_IOERR_READ', + 'SQLITE_IOERR_SHORT_READ', + 'SQLITE_IOERR_TRUNCATE', + 'SQLITE_IOERR_UNLOCK', + 'SQLITE_IOERR_WRITE', + 'SQLITE_LOCK_EXCLUSIVE', + 'SQLITE_LOCK_NONE', + 'SQLITE_LOCK_PENDING', + 'SQLITE_LOCK_RESERVED', + 'SQLITE_LOCK_SHARED', + 'SQLITE_LOCKED', + 'SQLITE_MISUSE', + 'SQLITE_NOTFOUND', + 'SQLITE_OPEN_CREATE', + 'SQLITE_OPEN_DELETEONCLOSE', + 'SQLITE_OPEN_MAIN_DB', + 'SQLITE_OPEN_READONLY' + ].forEach((k)=>{ + if(undefined === (state.sq3Codes[k] = capi[k])){ + toss("Maintenance required: not found:",k); + } + }); + state.opfsFlags = Object.assign(Object.create(null),{ + /** + Flag for use with xOpen(). URI flag "opfs-unlock-asap=1" + enables this. See defaultUnlockAsap, below. + */ + OPFS_UNLOCK_ASAP: 0x01, + /** + Flag for use with xOpen(). URI flag "delete-before-open=1" + tells the VFS to delete the db file before attempting to open + it. This can be used, e.g., to replace a db which has been + corrupted (without forcing us to expose a delete/unlink() + function in the public API). + + Failure to unlink the file is ignored but may lead to + downstream errors. An unlink can fail if, e.g., another tab + has the handle open. + + It goes without saying that deleting a file out from under another + instance results in Undefined Behavior. + */ + OPFS_UNLINK_BEFORE_OPEN: 0x02, + /** + If true, any async routine which implicitly acquires a sync + access handle (i.e. an OPFS lock) will release that lock at + the end of the call which acquires it. If false, such + "autolocks" are not released until the VFS is idle for some + brief amount of time. + + The benefit of enabling this is much higher concurrency. The + down-side is much-reduced performance (as much as a 4x decrease + in speedtest1). + */ + defaultUnlockAsap: false + }); + + /** + Runs the given operation (by name) in the async worker + counterpart, waits for its response, and returns the result + which the async worker writes to SAB[state.opIds.rc]. The + 2nd and subsequent arguments must be the arguments for the + async op. + */ + const opRun = (op,...args)=>{ + const opNdx = state.opIds[op] || toss("Invalid op ID:",op); + state.s11n.serialize(...args); + Atomics.store(state.sabOPView, state.opIds.rc, -1); + Atomics.store(state.sabOPView, state.opIds.whichOp, opNdx); + Atomics.notify(state.sabOPView, state.opIds.whichOp) + /* async thread will take over here */; + const t = performance.now(); + while('not-equal'!==Atomics.wait(state.sabOPView, state.opIds.rc, -1)){ + /* + The reason for this loop is buried in the details of a long + discussion at: + + https://github.com/sqlite/sqlite-wasm/issues/12 + + Summary: in at least one browser flavor, under high loads, + the wait()/notify() pairings can get out of sync. Calling + wait() here until it returns 'not-equal' gets them back in + sync. + */ + } + /* When the above wait() call returns 'not-equal', the async + half will have completed the operation and reported its results + in the state.opIds.rc slot of the SAB. */ + const rc = Atomics.load(state.sabOPView, state.opIds.rc); + metrics[op].wait += performance.now() - t; + if(rc && state.asyncS11nExceptions){ + const err = state.s11n.deserialize(); + if(err) error(op+"() async error:",...err); + } + return rc; + }; + + /** + Not part of the public API. Only for test/development use. + */ + opfsUtil.debug = { + asyncShutdown: ()=>{ + warn("Shutting down OPFS async listener. The OPFS VFS will no longer work."); + opRun('opfs-async-shutdown'); + }, + asyncRestart: ()=>{ + warn("Attempting to restart OPFS VFS async listener. Might work, might not."); + W.postMessage({type: 'opfs-async-restart'}); + } + }; + + const initS11n = ()=>{ + /** + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + ACHTUNG: this code is 100% duplicated in the other half of + this proxy! The documentation is maintained in the + "synchronous half". + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + + This proxy de/serializes cross-thread function arguments and + output-pointer values via the state.sabIO SharedArrayBuffer, + using the region defined by (state.sabS11nOffset, + state.sabS11nOffset + state.sabS11nSize]. Only one dataset is + recorded at a time. + + This is not a general-purpose format. It only supports the + range of operations, and data sizes, needed by the + sqlite3_vfs and sqlite3_io_methods operations. Serialized + data are transient and this serialization algorithm may + change at any time. + + The data format can be succinctly summarized as: + + Nt...Td...D + + Where: + + - N = number of entries (1 byte) + + - t = type ID of first argument (1 byte) + + - ...T = type IDs of the 2nd and subsequent arguments (1 byte + each). + + - d = raw bytes of first argument (per-type size). + + - ...D = raw bytes of the 2nd and subsequent arguments (per-type + size). + + All types except strings have fixed sizes. Strings are stored + using their TextEncoder/TextDecoder representations. It would + arguably make more sense to store them as Int16Arrays of + their JS character values, but how best/fastest to get that + in and out of string form is an open point. Initial + experimentation with that approach did not gain us any speed. + + Historical note: this impl was initially about 1% this size by + using using JSON.stringify/parse(), but using fit-to-purpose + serialization saves considerable runtime. + */ + if(state.s11n) return state.s11n; + const textDecoder = new TextDecoder(), + textEncoder = new TextEncoder('utf-8'), + viewU8 = new Uint8Array(state.sabIO, state.sabS11nOffset, state.sabS11nSize), + viewDV = new DataView(state.sabIO, state.sabS11nOffset, state.sabS11nSize); + state.s11n = Object.create(null); + /* Only arguments and return values of these types may be + serialized. This covers the whole range of types needed by the + sqlite3_vfs API. */ + const TypeIds = Object.create(null); + TypeIds.number = { id: 1, size: 8, getter: 'getFloat64', setter: 'setFloat64' }; + TypeIds.bigint = { id: 2, size: 8, getter: 'getBigInt64', setter: 'setBigInt64' }; + TypeIds.boolean = { id: 3, size: 4, getter: 'getInt32', setter: 'setInt32' }; + TypeIds.string = { id: 4 }; + + const getTypeId = (v)=>( + TypeIds[typeof v] + || toss("Maintenance required: this value type cannot be serialized.",v) + ); + const getTypeIdById = (tid)=>{ + switch(tid){ + case TypeIds.number.id: return TypeIds.number; + case TypeIds.bigint.id: return TypeIds.bigint; + case TypeIds.boolean.id: return TypeIds.boolean; + case TypeIds.string.id: return TypeIds.string; + default: toss("Invalid type ID:",tid); + } + }; + + /** + Returns an array of the deserialized state stored by the most + recent serialize() operation (from from this thread or the + counterpart thread), or null if the serialization buffer is + empty. If passed a truthy argument, the serialization buffer + is cleared after deserialization. + */ + state.s11n.deserialize = function(clear=false){ + ++metrics.s11n.deserialize.count; + const t = performance.now(); + const argc = viewU8[0]; + const rc = argc ? [] : null; + if(argc){ + const typeIds = []; + let offset = 1, i, n, v; + for(i = 0; i < argc; ++i, ++offset){ + typeIds.push(getTypeIdById(viewU8[offset])); + } + for(i = 0; i < argc; ++i){ + const t = typeIds[i]; + if(t.getter){ + v = viewDV[t.getter](offset, state.littleEndian); + offset += t.size; + }else{/*String*/ + n = viewDV.getInt32(offset, state.littleEndian); + offset += 4; + v = textDecoder.decode(viewU8.slice(offset, offset+n)); + offset += n; + } + rc.push(v); + } + } + if(clear) viewU8[0] = 0; + //log("deserialize:",argc, rc); + metrics.s11n.deserialize.time += performance.now() - t; + return rc; + }; + + /** + Serializes all arguments to the shared buffer for consumption + by the counterpart thread. + + This routine is only intended for serializing OPFS VFS + arguments and (in at least one special case) result values, + and the buffer is sized to be able to comfortably handle + those. + + If passed no arguments then it zeroes out the serialization + state. + */ + state.s11n.serialize = function(...args){ + const t = performance.now(); + ++metrics.s11n.serialize.count; + if(args.length){ + //log("serialize():",args); + const typeIds = []; + let i = 0, offset = 1; + viewU8[0] = args.length & 0xff /* header = # of args */; + for(; i < args.length; ++i, ++offset){ + /* Write the TypeIds.id value into the next args.length + bytes. */ + typeIds.push(getTypeId(args[i])); + viewU8[offset] = typeIds[i].id; + } + for(i = 0; i < args.length; ++i) { + /* Deserialize the following bytes based on their + corresponding TypeIds.id from the header. */ + const t = typeIds[i]; + if(t.setter){ + viewDV[t.setter](offset, args[i], state.littleEndian); + offset += t.size; + }else{/*String*/ + const s = textEncoder.encode(args[i]); + viewDV.setInt32(offset, s.byteLength, state.littleEndian); + offset += 4; + viewU8.set(s, offset); + offset += s.byteLength; + } + } + //log("serialize() result:",viewU8.slice(0,offset)); + }else{ + viewU8[0] = 0; + } + metrics.s11n.serialize.time += performance.now() - t; + }; + return state.s11n; + }/*initS11n()*/; + + /** + Generates a random ASCII string len characters long, intended for + use as a temporary file name. + */ + const randomFilename = function f(len=16){ + if(!f._chars){ + f._chars = "abcdefghijklmnopqrstuvwxyz"+ + "ABCDEFGHIJKLMNOPQRSTUVWXYZ"+ + "012346789"; + f._n = f._chars.length; + } + const a = []; + let i = 0; + for( ; i < len; ++i){ + const ndx = Math.random() * (f._n * 64) % f._n | 0; + a[i] = f._chars[ndx]; + } + return a.join(""); + /* + An alternative impl. with an unpredictable length + but much simpler: + + Math.floor(Math.random() * Number.MAX_SAFE_INTEGER).toString(36) + */ + }; + + /** + Map of sqlite3_file pointers to objects constructed by xOpen(). + */ + const __openFiles = Object.create(null); + + const opTimer = Object.create(null); + opTimer.op = undefined; + opTimer.start = undefined; + const mTimeStart = (op)=>{ + opTimer.start = performance.now(); + opTimer.op = op; + ++metrics[op].count; + }; + const mTimeEnd = ()=>( + metrics[opTimer.op].time += performance.now() - opTimer.start + ); + + /** + Impls for the sqlite3_io_methods methods. Maintenance reminder: + members are in alphabetical order to simplify finding them. + */ + const ioSyncWrappers = { + xCheckReservedLock: function(pFile,pOut){ + /** + As of late 2022, only a single lock can be held on an OPFS + file. We have no way of checking whether any _other_ db + connection has a lock except by trying to obtain and (on + success) release a sync-handle for it, but doing so would + involve an inherent race condition. For the time being, + pending a better solution, we simply report whether the + given pFile is open. + + Update 2024-06-12: based on forum discussions, this + function now always sets pOut to 0 (false): + + https://sqlite.org/forum/forumpost/a2f573b00cda1372 + */ + wasm.poke(pOut, 0, 'i32'); + return 0; + }, + xClose: function(pFile){ + mTimeStart('xClose'); + let rc = 0; + const f = __openFiles[pFile]; + if(f){ + delete __openFiles[pFile]; + rc = opRun('xClose', pFile); + if(f.sq3File) f.sq3File.dispose(); + } + mTimeEnd(); + return rc; + }, + xDeviceCharacteristics: function(pFile){ + return capi.SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN; + }, + xFileControl: function(pFile, opId, pArg){ + /*mTimeStart('xFileControl'); + mTimeEnd();*/ + return capi.SQLITE_NOTFOUND; + }, + xFileSize: function(pFile,pSz64){ + mTimeStart('xFileSize'); + let rc = opRun('xFileSize', pFile); + if(0==rc){ + try { + const sz = state.s11n.deserialize()[0]; + wasm.poke(pSz64, sz, 'i64'); + }catch(e){ + error("Unexpected error reading xFileSize() result:",e); + rc = state.sq3Codes.SQLITE_IOERR; + } + } + mTimeEnd(); + return rc; + }, + xLock: function(pFile,lockType){ + mTimeStart('xLock'); + const f = __openFiles[pFile]; + let rc = 0; + /* All OPFS locks are exclusive locks. If xLock() has + previously succeeded, do nothing except record the lock + type. If no lock is active, have the async counterpart + lock the file. */ + if( !f.lockType ) { + rc = opRun('xLock', pFile, lockType); + if( 0===rc ) f.lockType = lockType; + }else{ + f.lockType = lockType; + } + mTimeEnd(); + return rc; + }, + xRead: function(pFile,pDest,n,offset64){ + mTimeStart('xRead'); + const f = __openFiles[pFile]; + let rc; + try { + rc = opRun('xRead',pFile, n, Number(offset64)); + if(0===rc || capi.SQLITE_IOERR_SHORT_READ===rc){ + /** + Results get written to the SharedArrayBuffer f.sabView. + Because the heap is _not_ a SharedArrayBuffer, we have + to copy the results. TypedArray.set() seems to be the + fastest way to copy this. */ + wasm.heap8u().set(f.sabView.subarray(0, n), Number(pDest)); + } + }catch(e){ + error("xRead(",arguments,") failed:",e,f); + rc = capi.SQLITE_IOERR_READ; + } + mTimeEnd(); + return rc; + }, + xSync: function(pFile,flags){ + mTimeStart('xSync'); + ++metrics.xSync.count; + const rc = opRun('xSync', pFile, flags); + mTimeEnd(); + return rc; + }, + xTruncate: function(pFile,sz64){ + mTimeStart('xTruncate'); + const rc = opRun('xTruncate', pFile, Number(sz64)); + mTimeEnd(); + return rc; + }, + xUnlock: function(pFile,lockType){ + mTimeStart('xUnlock'); + const f = __openFiles[pFile]; + let rc = 0; + if( capi.SQLITE_LOCK_NONE === lockType + && f.lockType ){ + rc = opRun('xUnlock', pFile, lockType); + } + if( 0===rc ) f.lockType = lockType; + mTimeEnd(); + return rc; + }, + xWrite: function(pFile,pSrc,n,offset64){ + mTimeStart('xWrite'); + const f = __openFiles[pFile]; + let rc; + try { + f.sabView.set(wasm.heap8u().subarray( + Number(pSrc), Number(pSrc) + n + )); + rc = opRun('xWrite', pFile, n, Number(offset64)); + }catch(e){ + error("xWrite(",arguments,") failed:",e,f); + rc = capi.SQLITE_IOERR_WRITE; + } + mTimeEnd(); + return rc; + } + }/*ioSyncWrappers*/; + + /** + Impls for the sqlite3_vfs methods. Maintenance reminder: members + are in alphabetical order to simplify finding them. + */ + const vfsSyncWrappers = { + xAccess: function(pVfs,zName,flags,pOut){ + mTimeStart('xAccess'); + const rc = opRun('xAccess', wasm.cstrToJs(zName)); + wasm.poke( pOut, (rc ? 0 : 1), 'i32' ); + mTimeEnd(); + return 0; + }, + xCurrentTime: function(pVfs,pOut){ + /* If it turns out that we need to adjust for timezone, see: + https://stackoverflow.com/a/11760121/1458521 */ + wasm.poke(pOut, 2440587.5 + (new Date().getTime()/86400000), + 'double'); + return 0; + }, + xCurrentTimeInt64: function(pVfs,pOut){ + wasm.poke(pOut, (2440587.5 * 86400000) + new Date().getTime(), + 'i64'); + return 0; + }, + xDelete: function(pVfs, zName, doSyncDir){ + mTimeStart('xDelete'); + const rc = opRun('xDelete', wasm.cstrToJs(zName), doSyncDir, false); + mTimeEnd(); + return rc; + }, + xFullPathname: function(pVfs,zName,nOut,pOut){ + /* Until/unless we have some notion of "current dir" + in OPFS, simply copy zName to pOut... */ + const i = wasm.cstrncpy(pOut, zName, nOut); + return i!!v) : p; + }; + + /** + Takes the absolute path to a filesystem element. Returns an + array of [handleOfContainingDir, filename]. If the 2nd argument + is truthy then each directory element leading to the file is + created along the way. Throws if any creation or resolution + fails. + */ + opfsUtil.getDirForFilename = async function f(absFilename, createDirs = false){ + const path = opfsUtil.getResolvedPath(absFilename, true); + const filename = path.pop(); + let dh = opfsUtil.rootDirectory; + for(const dirName of path){ + if(dirName){ + dh = await dh.getDirectoryHandle(dirName, {create: !!createDirs}); + } + } + return [dh, filename]; + }; + + /** + Creates the given directory name, recursively, in + the OPFS filesystem. Returns true if it succeeds or the + directory already exists, else false. + */ + opfsUtil.mkdir = async function(absDirName){ + try { + await opfsUtil.getDirForFilename(absDirName+"/filepart", true); + return true; + }catch(e){ + //sqlite3.config.warn("mkdir(",absDirName,") failed:",e); + return false; + } + }; + /** + Checks whether the given OPFS filesystem entry exists, + returning true if it does, false if it doesn't or if an + exception is intercepted while trying to make the + determination. + */ + opfsUtil.entryExists = async function(fsEntryName){ + try { + const [dh, fn] = await opfsUtil.getDirForFilename(fsEntryName); + await dh.getFileHandle(fn); + return true; + }catch(e){ + return false; + } + }; + + /** + Generates a random ASCII string, intended for use as a + temporary file name. Its argument is the length of the string, + defaulting to 16. + */ + opfsUtil.randomFilename = randomFilename; + + /** + Returns a promise which resolves to an object which represents + all files and directories in the OPFS tree. The top-most object + has two properties: `dirs` is an array of directory entries + (described below) and `files` is a list of file names for all + files in that directory. + + Traversal starts at sqlite3.opfs.rootDirectory. + + Each `dirs` entry is an object in this form: + + ``` + { name: directoryName, + dirs: [...subdirs], + files: [...file names] + } + ``` + + The `files` and `subdirs` entries are always set but may be + empty arrays. + + The returned object has the same structure but its `name` is + an empty string. All returned objects are created with + Object.create(null), so have no prototype. + + Design note: the entries do not contain more information, + e.g. file sizes, because getting such info is not only + expensive but is subject to locking-related errors. + */ + opfsUtil.treeList = async function(){ + const doDir = async function callee(dirHandle,tgt){ + tgt.name = dirHandle.name; + tgt.dirs = []; + tgt.files = []; + for await (const handle of dirHandle.values()){ + if('directory' === handle.kind){ + const subDir = Object.create(null); + tgt.dirs.push(subDir); + await callee(handle, subDir); + }else{ + tgt.files.push(handle.name); + } + } + }; + const root = Object.create(null); + await doDir(opfsUtil.rootDirectory, root); + return root; + }; + + /** + Irrevocably deletes _all_ files in the current origin's OPFS. + Obviously, this must be used with great caution. It may throw + an exception if removal of anything fails (e.g. a file is + locked), but the precise conditions under which the underlying + APIs will throw are not documented (so we cannot tell you what + they are). + */ + opfsUtil.rmfr = async function(){ + const dir = opfsUtil.rootDirectory, opt = {recurse: true}; + for await (const handle of dir.values()){ + dir.removeEntry(handle.name, opt); + } + }; + + /** + Deletes the given OPFS filesystem entry. As this environment + has no notion of "current directory", the given name must be an + absolute path. If the 2nd argument is truthy, deletion is + recursive (use with caution!). + + The returned Promise resolves to true if the deletion was + successful, else false (but...). The OPFS API reports the + reason for the failure only in human-readable form, not + exceptions which can be type-checked to determine the + failure. Because of that... + + If the final argument is truthy then this function will + propagate any exception on error, rather than returning false. + */ + opfsUtil.unlink = async function(fsEntryName, recursive = false, + throwOnError = false){ + try { + const [hDir, filenamePart] = + await opfsUtil.getDirForFilename(fsEntryName, false); + await hDir.removeEntry(filenamePart, {recursive}); + return true; + }catch(e){ + if(throwOnError){ + throw new Error("unlink(",arguments[0],") failed: "+e.message,{ + cause: e + }); + } + return false; + } + }; + + /** + Traverses the OPFS filesystem, calling a callback for each + entry. The argument may be either a callback function or an + options object with any of the following properties: + + - `callback`: function which gets called for each filesystem + entry. It gets passed 3 arguments: 1) the + FileSystemFileHandle or FileSystemDirectoryHandle of each + entry (noting that both are instanceof FileSystemHandle). 2) + the FileSystemDirectoryHandle of the parent directory. 3) the + current depth level, with 0 being at the top of the tree + relative to the starting directory. If the callback returns a + literal false, as opposed to any other falsy value, traversal + stops without an error. Any exceptions it throws are + propagated. Results are undefined if the callback manipulate + the filesystem (e.g. removing or adding entries) because the + how OPFS iterators behave in the face of such changes is + undocumented. + + - `recursive` [bool=true]: specifies whether to recurse into + subdirectories or not. Whether recursion is depth-first or + breadth-first is unspecified! + + - `directory` [FileSystemDirectoryEntry=sqlite3.opfs.rootDirectory] + specifies the starting directory. + + If this function is passed a function, it is assumed to be the + callback. + + Returns a promise because it has to (by virtue of being async) + but that promise has no specific meaning: the traversal it + performs is synchronous. The promise must be used to catch any + exceptions propagated by the callback, however. + */ + opfsUtil.traverse = async function(opt){ + const defaultOpt = { + recursive: true, + directory: opfsUtil.rootDirectory + }; + if('function'===typeof opt){ + opt = {callback:opt}; + } + opt = Object.assign(defaultOpt, opt||{}); + const doDir = async function callee(dirHandle, depth){ + for await (const handle of dirHandle.values()){ + if(false === opt.callback(handle, dirHandle, depth)) return false; + else if(opt.recursive && 'directory' === handle.kind){ + if(false === await callee(handle, depth + 1)) break; + } + } + }; + doDir(opt.directory, 0); + }; + + /** + impl of importDb() when it's given a function as its second + argument. + */ + const importDbChunked = async function(filename, callback){ + const [hDir, fnamePart] = await opfsUtil.getDirForFilename(filename, true); + const hFile = await hDir.getFileHandle(fnamePart, {create:true}); + let sah = await hFile.createSyncAccessHandle(); + let nWrote = 0, chunk, checkedHeader = false, err = false; + try{ + sah.truncate(0); + while( undefined !== (chunk = await callback()) ){ + if(chunk instanceof ArrayBuffer) chunk = new Uint8Array(chunk); + if( !checkedHeader && 0===nWrote && chunk.byteLength>=15 ){ + util.affirmDbHeader(chunk); + checkedHeader = true; + } + sah.write(chunk, {at: nWrote}); + nWrote += chunk.byteLength; + } + if( nWrote < 512 || 0!==nWrote % 512 ){ + toss("Input size",nWrote,"is not correct for an SQLite database."); + } + if( !checkedHeader ){ + const header = new Uint8Array(20); + sah.read( header, {at: 0} ); + util.affirmDbHeader( header ); + } + sah.write(new Uint8Array([1,1]), {at: 18}/*force db out of WAL mode*/); + return nWrote; + }catch(e){ + await sah.close(); + sah = undefined; + await hDir.removeEntry( fnamePart ).catch(()=>{}); + throw e; + }finally { + if( sah ) await sah.close(); + } + }; + + /** + Asynchronously imports the given bytes (a byte array or + ArrayBuffer) into the given database file. + + Results are undefined if the given db name refers to an opened + db. + + If passed a function for its second argument, its behaviour + changes: imports its data in chunks fed to it by the given + callback function. It calls the callback (which may be async) + repeatedly, expecting either a Uint8Array or ArrayBuffer (to + denote new input) or undefined (to denote EOF). For so long as + the callback continues to return non-undefined, it will append + incoming data to the given VFS-hosted database file. When + called this way, the resolved value of the returned Promise is + the number of bytes written to the target file. + + It very specifically requires the input to be an SQLite3 + database and throws if that's not the case. It does so in + order to prevent this function from taking on a larger scope + than it is specifically intended to. i.e. we do not want it to + become a convenience for importing arbitrary files into OPFS. + + This routine rewrites the database header bytes in the output + file (not the input array) to force disabling of WAL mode. + + On error this throws and the state of the input file is + undefined (it depends on where the exception was triggered). + + On success, resolves to the number of bytes written. + */ + opfsUtil.importDb = async function(filename, bytes){ + if( bytes instanceof Function ){ + return importDbChunked(filename, bytes); + } + if(bytes instanceof ArrayBuffer) bytes = new Uint8Array(bytes); + util.affirmIsDb(bytes); + const n = bytes.byteLength; + const [hDir, fnamePart] = await opfsUtil.getDirForFilename(filename, true); + let sah, err, nWrote = 0; + try { + const hFile = await hDir.getFileHandle(fnamePart, {create:true}); + sah = await hFile.createSyncAccessHandle(); + sah.truncate(0); + nWrote = sah.write(bytes, {at: 0}); + if(nWrote != n){ + toss("Expected to write "+n+" bytes but wrote "+nWrote+"."); + } + sah.write(new Uint8Array([1,1]), {at: 18}) /* force db out of WAL mode */; + return nWrote; + }catch(e){ + if( sah ){ await sah.close(); sah = undefined; } + await hDir.removeEntry( fnamePart ).catch(()=>{}); + throw e; + }finally{ + if( sah ) await sah.close(); + } + }; + + if(sqlite3.oo1){ + const OpfsDb = function(...args){ + const opt = sqlite3.oo1.DB.dbCtorHelper.normalizeArgs(...args); + opt.vfs = opfsVfs.$zName; + sqlite3.oo1.DB.dbCtorHelper.call(this, opt); + }; + OpfsDb.prototype = Object.create(sqlite3.oo1.DB.prototype); + sqlite3.oo1.OpfsDb = OpfsDb; + OpfsDb.importDb = opfsUtil.importDb; + sqlite3.oo1.DB.dbCtorHelper.setVfsPostOpenCallback( + opfsVfs.pointer, + function(oo1Db, sqlite3){ + /* Set a relatively high default busy-timeout handler to + help OPFS dbs deal with multi-tab/multi-worker + contention. */ + sqlite3.capi.sqlite3_busy_timeout(oo1Db, 10000); + } + ); + }/*extend sqlite3.oo1*/ + + const sanityCheck = function(){ + const scope = wasm.scopedAllocPush(); + const sq3File = new sqlite3_file(); + try{ + const fid = sq3File.pointer; + const openFlags = capi.SQLITE_OPEN_CREATE + | capi.SQLITE_OPEN_READWRITE + //| capi.SQLITE_OPEN_DELETEONCLOSE + | capi.SQLITE_OPEN_MAIN_DB; + const pOut = wasm.scopedAlloc(8); + const dbFile = "/sanity/check/file"+randomFilename(8); + const zDbFile = wasm.scopedAllocCString(dbFile); + let rc; + state.s11n.serialize("This is ä string."); + rc = state.s11n.deserialize(); + log("deserialize() says:",rc); + if("This is ä string."!==rc[0]) toss("String d13n error."); + vfsSyncWrappers.xAccess(opfsVfs.pointer, zDbFile, 0, pOut); + rc = wasm.peek(pOut,'i32'); + log("xAccess(",dbFile,") exists ?=",rc); + rc = vfsSyncWrappers.xOpen(opfsVfs.pointer, zDbFile, + fid, openFlags, pOut); + log("open rc =",rc,"state.sabOPView[xOpen] =", + state.sabOPView[state.opIds.xOpen]); + if(0!==rc){ + error("open failed with code",rc); + return; + } + vfsSyncWrappers.xAccess(opfsVfs.pointer, zDbFile, 0, pOut); + rc = wasm.peek(pOut,'i32'); + if(!rc) toss("xAccess() failed to detect file."); + rc = ioSyncWrappers.xSync(sq3File.pointer, 0); + if(rc) toss('sync failed w/ rc',rc); + rc = ioSyncWrappers.xTruncate(sq3File.pointer, 1024); + if(rc) toss('truncate failed w/ rc',rc); + wasm.poke(pOut,0,'i64'); + rc = ioSyncWrappers.xFileSize(sq3File.pointer, pOut); + if(rc) toss('xFileSize failed w/ rc',rc); + log("xFileSize says:",wasm.peek(pOut, 'i64')); + rc = ioSyncWrappers.xWrite(sq3File.pointer, zDbFile, 10, 1); + if(rc) toss("xWrite() failed!"); + const readBuf = wasm.scopedAlloc(16); + rc = ioSyncWrappers.xRead(sq3File.pointer, readBuf, 6, 2); + wasm.poke(readBuf+6,0); + let jRead = wasm.cstrToJs(readBuf); + log("xRead() got:",jRead); + if("sanity"!==jRead) toss("Unexpected xRead() value."); + if(vfsSyncWrappers.xSleep){ + log("xSleep()ing before close()ing..."); + vfsSyncWrappers.xSleep(opfsVfs.pointer,2000); + log("waking up from xSleep()"); + } + rc = ioSyncWrappers.xClose(fid); + log("xClose rc =",rc,"sabOPView =",state.sabOPView); + log("Deleting file:",dbFile); + vfsSyncWrappers.xDelete(opfsVfs.pointer, zDbFile, 0x1234); + vfsSyncWrappers.xAccess(opfsVfs.pointer, zDbFile, 0, pOut); + rc = wasm.peek(pOut,'i32'); + if(rc) toss("Expecting 0 from xAccess(",dbFile,") after xDelete()."); + warn("End of OPFS sanity checks."); + }finally{ + sq3File.dispose(); + wasm.scopedAllocPop(scope); + } + }/*sanityCheck()*/; + + W.onmessage = function({data}){ + //log("Worker.onmessage:",data); + switch(data.type){ + case 'opfs-unavailable': + /* Async proxy has determined that OPFS is unavailable. There's + nothing more for us to do here. */ + promiseReject(new Error(data.payload.join(' '))); + break; + case 'opfs-async-loaded': + /* Arrives as soon as the asyc proxy finishes loading. + Pass our config and shared state on to the async + worker. */ + W.postMessage({type: 'opfs-async-init',args: state}); + break; + case 'opfs-async-inited': { + /* Indicates that the async partner has received the 'init' + and has finished initializing, so the real work can + begin... */ + if(true===promiseWasRejected){ + break /* promise was already rejected via timer */; + } + try { + sqlite3.vfs.installVfs({ + io: {struct: opfsIoMethods, methods: ioSyncWrappers}, + vfs: {struct: opfsVfs, methods: vfsSyncWrappers} + }); + state.sabOPView = new Int32Array(state.sabOP); + state.sabFileBufView = new Uint8Array(state.sabIO, 0, state.fileBufferSize); + state.sabS11nView = new Uint8Array(state.sabIO, state.sabS11nOffset, state.sabS11nSize); + initS11n(); + if(options.sanityChecks){ + warn("Running sanity checks because of opfs-sanity-check URL arg..."); + sanityCheck(); + } + if(thisThreadHasOPFS()){ + navigator.storage.getDirectory().then((d)=>{ + W.onerror = W._originalOnError; + delete W._originalOnError; + sqlite3.opfs = opfsUtil; + opfsUtil.rootDirectory = d; + log("End of OPFS sqlite3_vfs setup.", opfsVfs); + promiseResolve(); + }).catch(promiseReject); + }else{ + promiseResolve(); + } + }catch(e){ + error(e); + promiseReject(e); + } + break; + } + default: { + const errMsg = ( + "Unexpected message from the OPFS async worker: " + + JSON.stringify(data) + ); + error(errMsg); + promiseReject(new Error(errMsg)); + break; + } + }/*switch(data.type)*/ + }/*W.onmessage()*/; + })/*thePromise*/; + return thePromise; +}/*installOpfsVfs()*/; +installOpfsVfs.defaultProxyUri = + "sqlite3-opfs-async-proxy.js"; +globalThis.sqlite3ApiBootstrap.initializersAsync.push(async (sqlite3)=>{ + try{ + let proxyJs = installOpfsVfs.defaultProxyUri; + if(sqlite3.scriptInfo.sqlite3Dir){ + installOpfsVfs.defaultProxyUri = + sqlite3.scriptInfo.sqlite3Dir + proxyJs; + //sqlite3.config.warn("installOpfsVfs.defaultProxyUri =",installOpfsVfs.defaultProxyUri); + } + return installOpfsVfs().catch((e)=>{ + sqlite3.config.warn("Ignoring inability to install OPFS sqlite3_vfs:",e.message); + }); + }catch(e){ + sqlite3.config.error("installOpfsVfs() exception:",e); + return Promise.reject(e); + } +}); +}/*sqlite3ApiBootstrap.initializers.push()*/); +//#else +/* The OPFS VFS parts are elided from builds targeting node.js. */ +//#endif target:node diff --git a/ext/wasm/api/sqlite3-vtab-helper.c-pp.js b/ext/wasm/api/sqlite3-vtab-helper.c-pp.js new file mode 100644 index 0000000000..4c2338fc5a --- /dev/null +++ b/ext/wasm/api/sqlite3-vtab-helper.c-pp.js @@ -0,0 +1,431 @@ +/* +** 2022-11-30 +** +** The author disclaims copyright to this source code. In place of a +** legal notice, here is a blessing: +** +** * May you do good and not evil. +** * May you find forgiveness for yourself and forgive others. +** * May you share freely, never taking more than you give. +*/ + +/** + This file installs sqlite3.vtab, a namespace of helpers for use in + the creation of JavaScript implementations virtual tables. If built + without virtual table support then this function does nothing. +*/ +'use strict'; +globalThis.sqlite3ApiBootstrap.initializers.push(function(sqlite3){ + if( !sqlite3.wasm.exports.sqlite3_declare_vtab ){ + return; + } + const wasm = sqlite3.wasm, capi = sqlite3.capi, toss = sqlite3.util.toss3; + const vtab = Object.create(null); + sqlite3.vtab = vtab; + + const sii = capi.sqlite3_index_info; + /** + If n is >=0 and less than this.$nConstraint, this function + returns either a WASM pointer to the 0-based nth entry of + this.$aConstraint (if passed a truthy 2nd argument) or an + sqlite3_index_info.sqlite3_index_constraint object wrapping that + address (if passed a falsy value or no 2nd argument). Returns a + falsy value if n is out of range. + */ + sii.prototype.nthConstraint = function(n, asPtr=false){ + if(n<0 || n>=this.$nConstraint) return false; + const ptr = wasm.ptr.add( + this.$aConstraint, + sii.sqlite3_index_constraint.structInfo.sizeof * n + ); + return asPtr ? ptr : new sii.sqlite3_index_constraint(ptr); + }; + + /** + Works identically to nthConstraint() but returns state from + this.$aConstraintUsage, so returns an + sqlite3_index_info.sqlite3_index_constraint_usage instance + if passed no 2nd argument or a falsy 2nd argument. + */ + sii.prototype.nthConstraintUsage = function(n, asPtr=false){ + if(n<0 || n>=this.$nConstraint) return false; + const ptr = wasm.ptr.add( + this.$aConstraintUsage, + sii.sqlite3_index_constraint_usage.structInfo.sizeof * n + ); + return asPtr ? ptr : new sii.sqlite3_index_constraint_usage(ptr); + }; + + /** + If n is >=0 and less than this.$nOrderBy, this function + returns either a WASM pointer to the 0-based nth entry of + this.$aOrderBy (if passed a truthy 2nd argument) or an + sqlite3_index_info.sqlite3_index_orderby object wrapping that + address (if passed a falsy value or no 2nd argument). Returns a + falsy value if n is out of range. + */ + sii.prototype.nthOrderBy = function(n, asPtr=false){ + if(n<0 || n>=this.$nOrderBy) return false; + const ptr = wasm.ptr.add( + this.$aOrderBy, + sii.sqlite3_index_orderby.structInfo.sizeof * n + ); + return asPtr ? ptr : new sii.sqlite3_index_orderby(ptr); + }; + + /** + Internal factory function for xVtab and xCursor impls. + */ + const __xWrapFactory = function(methodName,StructType){ + return function(ptr,removeMapping=false){ + if(0===arguments.length) ptr = new StructType; + if(ptr instanceof StructType){ + //T.assert(!this.has(ptr.pointer)); + this.set(ptr.pointer, ptr); + return ptr; + }else if(!wasm.isPtr(ptr)){ + sqlite3.SQLite3Error.toss("Invalid argument to",methodName+"()"); + } + let rc = this.get(ptr); + if(removeMapping) this.delete(ptr); + return rc; + }.bind(new Map); + }; + + /** + A factory function which implements a simple lifetime manager for + mappings between C struct pointers and their JS-level wrappers. + The first argument must be the logical name of the manager + (e.g. 'xVtab' or 'xCursor'), which is only used for error + reporting. The second must be the capi.XYZ struct-type value, + e.g. capi.sqlite3_vtab or capi.sqlite3_vtab_cursor. + + Returns an object with 4 methods: create(), get(), unget(), and + dispose(), plus a StructType member with the value of the 2nd + argument. The methods are documented in the body of this + function. + */ + const StructPtrMapper = function(name, StructType){ + const __xWrap = __xWrapFactory(name,StructType); + /** + This object houses a small API for managing mappings of (`T*`) + to StructType objects, specifically within the lifetime + requirements of sqlite3_module methods. + */ + return Object.assign(Object.create(null),{ + /** The StructType object for this object's API. */ + StructType, + /** + Creates a new StructType object, writes its `pointer` + value to the given output pointer, and returns that + object. Its intended usage depends on StructType: + + sqlite3_vtab: to be called from sqlite3_module::xConnect() + or xCreate() implementations. + + sqlite3_vtab_cursor: to be called from xOpen(). + + This will throw if allocation of the StructType instance + fails or if ppOut is not a pointer-type value. + */ + create: (ppOut)=>{ + const rc = __xWrap(); + wasm.pokePtr(ppOut, rc.pointer); + return rc; + }, + /** + Returns the StructType object previously mapped to the + given pointer using create(). Its intended usage depends + on StructType: + + sqlite3_vtab: to be called from sqlite3_module methods which + take a (sqlite3_vtab*) pointer _except_ for + xDestroy()/xDisconnect(), in which case unget() or dispose(). + + sqlite3_vtab_cursor: to be called from any sqlite3_module methods + which take a `sqlite3_vtab_cursor*` argument except xClose(), + in which case use unget() or dispose(). + + Rule to remember: _never_ call dispose() on an instance + returned by this function. + */ + get: (pCObj)=>__xWrap(pCObj), + /** + Identical to get() but also disconnects the mapping between the + given pointer and the returned StructType object, such that + future calls to this function or get() with the same pointer + will return the undefined value. Its intended usage depends + on StructType: + + sqlite3_vtab: to be called from sqlite3_module::xDisconnect() or + xDestroy() implementations or in error handling of a failed + xCreate() or xConnect(). + + sqlite3_vtab_cursor: to be called from xClose() or during + cleanup in a failed xOpen(). + + Calling this method obligates the caller to call dispose() on + the returned object when they're done with it. + */ + unget: (pCObj)=>__xWrap(pCObj,true), + /** + Works like unget() plus it calls dispose() on the + StructType object. + */ + dispose: (pCObj)=>{ + const o = __xWrap(pCObj,true); + if(o) o.dispose(); + } + }); + }; + + /** + A lifetime-management object for mapping `sqlite3_vtab*` + instances in sqlite3_module methods to capi.sqlite3_vtab + objects. + + The API docs are in the API-internal StructPtrMapper(). + */ + vtab.xVtab = StructPtrMapper('xVtab', capi.sqlite3_vtab); + + /** + A lifetime-management object for mapping `sqlite3_vtab_cursor*` + instances in sqlite3_module methods to capi.sqlite3_vtab_cursor + objects. + + The API docs are in the API-internal StructPtrMapper(). + */ + vtab.xCursor = StructPtrMapper('xCursor', capi.sqlite3_vtab_cursor); + + /** + Convenience form of creating an sqlite3_index_info wrapper, + intended for use in xBestIndex implementations. Note that the + caller is expected to call dispose() on the returned object + before returning. Though not _strictly_ required, as that object + does not own the pIdxInfo memory, it is nonetheless good form. + */ + vtab.xIndexInfo = (pIdxInfo)=>new capi.sqlite3_index_info(pIdxInfo); + + /** + Given an sqlite3_module method name and error object, this + function returns sqlite3.capi.SQLITE_NOMEM if (e instanceof + sqlite3.WasmAllocError), else it returns its second argument. Its + intended usage is in the methods of a sqlite3_vfs or + sqlite3_module: + + ``` + try{ + let rc = ... + return rc; + }catch(e){ + return sqlite3.vtab.xError( + 'xColumn', e, sqlite3.capi.SQLITE_XYZ); + // where SQLITE_XYZ is some call-appropriate result code. + } + ``` + + If no 3rd argument is provided, its default depends on + the error type: + + - An sqlite3.WasmAllocError always resolves to capi.SQLITE_NOMEM. + + - If err is an SQLite3Error then its `resultCode` property + is used. + + - If all else fails, capi.SQLITE_ERROR is used. + + If xError.errorReporter is a function, it is called in + order to report the error, else the error is not reported. + If that function throws, that exception is ignored. + */ + vtab.xError = function f(methodName, err, defaultRc){ + if(f.errorReporter instanceof Function){ + try{f.errorReporter("sqlite3_module::"+methodName+"(): "+err.message);} + catch(e){/*ignored*/} + } + let rc; + if(err instanceof sqlite3.WasmAllocError) rc = capi.SQLITE_NOMEM; + else if(arguments.length>2) rc = defaultRc; + else if(err instanceof sqlite3.SQLite3Error) rc = err.resultCode; + return rc || capi.SQLITE_ERROR; + }; + vtab.xError.errorReporter = 1 ? sqlite3.config.error.bind(sqlite3.config) : false; + + /** + A helper for sqlite3_vtab::xRowid() and xUpdate() + implementations. It must be passed the final argument to one of + those methods (an output pointer to an int64 row ID) and the + value to store at the output pointer's address. Returns the same + as wasm.poke() and will throw if the 1st or 2nd arguments + are invalid for that function. + + Example xRowid impl: + + ``` + const xRowid = (pCursor, ppRowid64)=>{ + const c = vtab.xCursor(pCursor); + vtab.xRowid(ppRowid64, c.myRowId); + return 0; + }; + ``` + */ + vtab.xRowid = (ppRowid64, value)=>wasm.poke(ppRowid64, value, 'i64'); + + /** + A helper to initialize and set up an sqlite3_module object for + later installation into individual databases using + sqlite3_create_module(). Requires an object with the following + properties: + + - `methods`: an object containing a mapping of properties with + the C-side names of the sqlite3_module methods, e.g. xCreate, + xBestIndex, etc., to JS implementations for those functions. + Certain special-case handling is performed, as described below. + + - `catchExceptions` (default=false): if truthy, the given methods + are not mapped as-is, but are instead wrapped inside wrappers + which translate exceptions into result codes of SQLITE_ERROR or + SQLITE_NOMEM, depending on whether the exception is an + sqlite3.WasmAllocError. In the case of the xConnect and xCreate + methods, the exception handler also sets the output error + string to the exception's error string. + + - OPTIONAL `struct`: a sqlite3.capi.sqlite3_module() instance. If + not set, one will be created automatically. If the current + "this" is-a sqlite3_module then it is unconditionally used in + place of `struct`. + + - OPTIONAL `iVersion`: if set, it must be an integer value and it + gets assigned to the `$iVersion` member of the struct object. + If it's _not_ set, and the passed-in `struct` object's `$iVersion` + is 0 (the default) then this function attempts to define a value + for that property based on the list of methods it has. + + If `catchExceptions` is false, it is up to the client to ensure + that no exceptions escape the methods, as doing so would move + them through the C API, leading to undefined + behavior. (vtab.xError() is intended to assist in reporting + such exceptions.) + + Certain methods may refer to the same implementation. To simplify + the definition of such methods: + + - If `methods.xConnect` is `true` then the value of + `methods.xCreate` is used in its place, and vice versa. sqlite + treats xConnect/xCreate functions specially if they are exactly + the same function (same pointer value). + + - If `methods.xDisconnect` is true then the value of + `methods.xDestroy` is used in its place, and vice versa. + + This is to facilitate creation of those methods inline in the + passed-in object without requiring the client to explicitly get a + reference to one of them in order to assign it to the other + one. + + The `catchExceptions`-installed handlers will account for + identical references to the above functions and will install the + same wrapper function for both. + + The given methods are expected to return integer values, as + expected by the C API. If `catchExceptions` is truthy, the return + value of the wrapped function will be used as-is and will be + translated to 0 if the function returns a falsy value (e.g. if it + does not have an explicit return). If `catchExceptions` is _not_ + active, the method implementations must explicitly return integer + values. + + Throws on error. On success, returns the sqlite3_module object + (`this` or `opt.struct` or a new sqlite3_module instance, + depending on how it's called). + */ + vtab.setupModule = function(opt){ + let createdMod = false; + const mod = (this instanceof capi.sqlite3_module) + ? this : (opt.struct || (createdMod = new capi.sqlite3_module())); + try{ + const methods = opt.methods || toss("Missing 'methods' object."); + for(const e of Object.entries({ + // -----^ ==> [k,v] triggers a broken code transformation in + // some versions of the emsdk toolchain. + xConnect: 'xCreate', xDisconnect: 'xDestroy' + })){ + // Remap X=true to X=Y for certain X/Y combinations + const k = e[0], v = e[1]; + if(true === methods[k]) methods[k] = methods[v]; + else if(true === methods[v]) methods[v] = methods[k]; + } + if(opt.catchExceptions){ + const fwrap = function(methodName, func){ + if(['xConnect','xCreate'].indexOf(methodName) >= 0){ + return function(pDb, pAux, argc, argv, ppVtab, pzErr){ + try{return func(...arguments) || 0} + catch(e){ + if(!(e instanceof sqlite3.WasmAllocError)){ + wasm.dealloc(wasm.peekPtr(pzErr)); + wasm.pokePtr(pzErr, wasm.allocCString(e.message)); + } + return vtab.xError(methodName, e); + } + }; + }else{ + return function(...args){ + try{return func(...args) || 0} + catch(e){ + return vtab.xError(methodName, e); + } + }; + } + }; + const mnames = [ + 'xCreate', 'xConnect', 'xBestIndex', 'xDisconnect', + 'xDestroy', 'xOpen', 'xClose', 'xFilter', 'xNext', + 'xEof', 'xColumn', 'xRowid', 'xUpdate', + 'xBegin', 'xSync', 'xCommit', 'xRollback', + 'xFindFunction', 'xRename', 'xSavepoint', 'xRelease', + 'xRollbackTo', 'xShadowName' + ]; + const remethods = Object.create(null); + for(const k of mnames){ + const m = methods[k]; + if(!(m instanceof Function)) continue; + else if('xConnect'===k && methods.xCreate===m){ + remethods[k] = methods.xCreate; + }else if('xCreate'===k && methods.xConnect===m){ + remethods[k] = methods.xConnect; + }else{ + remethods[k] = fwrap(k, m); + } + } + mod.installMethods(remethods, false); + }else{ + // No automatic exception handling. Trust the client + // to not throw. + mod.installMethods( + methods, !!opt.applyArgcCheck/*undocumented option*/ + ); + } + if(0===mod.$iVersion){ + let v; + if('number'===typeof opt.iVersion) v = opt.iVersion; + else if(mod.$xIntegrity) v = 4; + else if(mod.$xShadowName) v = 3; + else if(mod.$xSavePoint || mod.$xRelease || mod.$xRollbackTo) v = 2; + else v = 1; + mod.$iVersion = v; + } + }catch(e){ + if(createdMod) createdMod.dispose(); + throw e; + } + return mod; + }/*setupModule()*/; + + /** + Equivalent to calling vtab.setupModule() with this sqlite3_module + object as the call's `this`. + */ + capi.sqlite3_module.prototype.setupModule = function(opt){ + return vtab.setupModule.call(this, opt); + }; +}/*sqlite3ApiBootstrap.initializers.push()*/); diff --git a/ext/wasm/api/sqlite3-wasm.c b/ext/wasm/api/sqlite3-wasm.c new file mode 100644 index 0000000000..4d5e9b2962 --- /dev/null +++ b/ext/wasm/api/sqlite3-wasm.c @@ -0,0 +1,1955 @@ +/* +** This file requires access to sqlite3.c static state in order to +** implement certain WASM-specific features, and thus directly +** includes that file. Unlike the rest of sqlite3.c, this file +** requires compiling with -std=c99 (or equivalent, or a later C +** version) because it makes use of features not available in C89. +** +** At its simplest, to build sqlite3.wasm either place this file +** in the same directory as sqlite3.c/h before compilation or use the +** -I/path flag to tell the compiler where to find both of those +** files, then compile this file. For example: +** +** emcc -o sqlite3.wasm ... -I/path/to/sqlite3-c-and-h sqlite3-wasm.c +*/ +#define SQLITE_WASM +#ifdef SQLITE_WASM_ENABLE_C_TESTS +# undef SQLITE_WASM_ENABLE_C_TESTS +# define SQLITE_WASM_ENABLE_C_TESTS 1 +/* +** Code blocked off by SQLITE_WASM_ENABLE_C_TESTS is intended solely +** for use in unit/regression testing. They may be safely omitted from +** client-side builds. The main unit test script, tester1.js, will +** skip related tests if it doesn't find the corresponding functions +** in the WASM exports. +*/ +#else +# define SQLITE_WASM_ENABLE_C_TESTS 0 +#endif + +/* +** Threading and file locking: JS is single-threaded. Each Worker +** thread is a separate instance of the JS engine so can never access +** the same db handle as another thread, thus multi-threading support +** is unnecessary in the library. Because the filesystems are virtual +** and local to a given wasm runtime instance, two Workers can never +** access the same db file at once, with the exception of OPFS. +** +** Summary: except for the case of OPFS, which supports locking using +** its own API, threading and file locking support are unnecessary in +** the wasm build. +*/ + +/* +** Undefine any SQLITE_... config flags which we specifically do not +** want defined. Please keep these alphabetized. +*/ +#undef SQLITE_OMIT_DESERIALIZE +#undef SQLITE_OMIT_MEMORYDB + +/* +** Define any SQLITE_... config defaults we want if they aren't +** overridden by the builder. Please keep these alphabetized. +*/ + +/**********************************************************************/ +/* SQLITE_D... */ +#ifndef SQLITE_DEFAULT_CACHE_SIZE +/* +** The OPFS impls benefit tremendously from an increased cache size +** when working on large workloads, e.g. speedtest1 --size 50 or +** higher. On smaller workloads, e.g. speedtest1 --size 25, they +** clearly benefit from having 4mb of cache, but not as much as a +** larger cache benefits the larger workloads. Speed differences +** between 2x and nearly 3x have been measured with ample page cache. +*/ +# define SQLITE_DEFAULT_CACHE_SIZE -16384 +#endif +#if !defined(SQLITE_DEFAULT_PAGE_SIZE) +/* +** OPFS performance is improved by approx. 12% with a page size of 8kb +** instead of 4kb. Performance with 16kb is equivalent to 8kb. +** +** Performance difference of kvvfs with a page size of 8kb compared to +** 4kb, as measured by speedtest1 --size 4, is indeterminate: +** measurements are all over the place either way and not +** significantly different. +*/ +# define SQLITE_DEFAULT_PAGE_SIZE 8192 +#endif +#ifndef SQLITE_DEFAULT_UNIX_VFS +# define SQLITE_DEFAULT_UNIX_VFS "unix-none" +#endif +#undef SQLITE_DQS +#define SQLITE_DQS 0 + +/**********************************************************************/ +/* SQLITE_ENABLE_... */ +/* +** Unconditionally enable API_ARMOR in the WASM build. It ensures that +** public APIs behave predictable in the face of passing illegal NULLs +** or ranges which might otherwise invoke undefined behavior. +*/ +#undef SQLITE_ENABLE_API_ARMOR +#define SQLITE_ENABLE_API_ARMOR 1 + +/**********************************************************************/ +/* SQLITE_O... */ +#undef SQLITE_OMIT_DEPRECATED +#define SQLITE_OMIT_DEPRECATED 1 +#undef SQLITE_OMIT_LOAD_EXTENSION +#define SQLITE_OMIT_LOAD_EXTENSION 1 +#undef SQLITE_OMIT_SHARED_CACHE +#define SQLITE_OMIT_SHARED_CACHE 1 +#undef SQLITE_OMIT_UTF16 +#define SQLITE_OMIT_UTF16 1 +#undef SQLITE_OS_KV_OPTIONAL +#define SQLITE_OS_KV_OPTIONAL 1 + +/**********************************************************************/ +/* SQLITE_S... */ +#ifndef SQLITE_STRICT_SUBTYPE +# define SQLITE_STRICT_SUBTYPE 1 +#endif + +/**********************************************************************/ +/* SQLITE_T... */ +#ifndef SQLITE_TEMP_STORE +# define SQLITE_TEMP_STORE 2 +#endif +#ifndef SQLITE_THREADSAFE +# define SQLITE_THREADSAFE 0 +#endif + +/**********************************************************************/ +/* SQLITE_USE_... */ +#ifndef SQLITE_USE_URI +# define SQLITE_USE_URI 1 +#endif + +#ifdef SQLITE_WASM_EXTRA_INIT +/* SQLITE_EXTRA_INIT vs SQLITE_EXTRA_INIT_MUTEXED: +** see https://sqlite.org/forum/forumpost/14183b98fc0b1dea */ +# define SQLITE_EXTRA_INIT_MUTEXED sqlite3_wasm_extra_init +#endif + +/* +** If SQLITE_WASM_BARE_BONES is defined, undefine most of the ENABLE +** macros. This will, when using the canonical makefile, also elide +** any C functions from the WASM exports which are listed in +** ./EXPORT_FUNCTIONS.sqlite3-extras. +*/ +#ifdef SQLITE_WASM_BARE_BONES +# undef SQLITE_ENABLE_COLUMN_METADATA +# undef SQLITE_ENABLE_DBPAGE_VTAB +# undef SQLITE_ENABLE_DBSTAT_VTAB +# undef SQLITE_ENABLE_EXPLAIN_COMMENTS +# undef SQLITE_ENABLE_FTS5 +# undef SQLITE_ENABLE_OFFSET_SQL_FUNC +# undef SQLITE_ENABLE_PERCENTILE +# undef SQLITE_ENABLE_PREUPDATE_HOOK +# undef SQLITE_ENABLE_RTREE +# undef SQLITE_ENABLE_SESSION +# undef SQLITE_ENABLE_STMTVTAB +# undef SQLITE_OMIT_AUTHORIZATION +# define SQLITE_OMIT_AUTHORIZATION +# undef SQLITE_OMIT_GET_TABLE +# define SQLITE_OMIT_GET_TABLE +# undef SQLITE_OMIT_INCRBLOB +# define SQLITE_OMIT_INCRBLOB +# undef SQLITE_OMIT_INTROSPECTION_PRAGMAS +# define SQLITE_OMIT_INTROSPECTION_PRAGMAS +# undef SQLITE_OMIT_JSON +# define SQLITE_OMIT_JSON +# undef SQLITE_OMIT_PROGRESS_CALLBACK +# define SQLITE_OMIT_PROGRESS_CALLBACK +# undef SQLITE_OMIT_WAL +# define SQLITE_OMIT_WAL +/* + The following OMITs do not work with the standard amalgamation, so + require a custom build: + + fossil clean -x + ./configure + OPTS='...' make -e sqlite3 + + where ... has a -D... for each of the following OMIT flags: + +# undef SQLITE_OMIT_EXPLAIN +# define SQLITE_OMIT_EXPLAIN + +# undef SQLITE_OMIT_TRIGGER +# define SQLITE_OMIT_TRIGGER + +# undef SQLITE_OMIT_VIRTUALTABLE +# define SQLITE_OMIT_VIRTUALTABLE + +# undef SQLITE_OMIT_WINDOWFUNC +# define SQLITE_OMIT_WINDOWFUNC + + As of this writing (2024-07-25), such a build fails in various ways + for as-yet-unknown reasons. +*/ +#endif + +#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_WASM_BARE_BONES) +# define SQLITE_WASM_HAS_VTAB 1 +#else +# define SQLITE_WASM_HAS_VTAB 0 +#endif + +#include + +/* +** SQLITE_WASM_EXPORT is functionally identical to EMSCRIPTEN_KEEPALIVE +** but is not Emscripten-specific. It explicitly marks functions for +** export into the target wasm file without requiring explicit listing +** of those functions in Emscripten's -sEXPORTED_FUNCTIONS=... list +** (or equivalent in other build platforms). Any function with neither +** this attribute nor which is listed as an explicit export will not +** be exported from the wasm file (but may still be used internally +** within the wasm file). +** +** The functions in this file (sqlite3-wasm.c) which require exporting +** are marked with this flag. They may also be added to any explicit +** build-time export list but need not be. All of these APIs are +** intended for use only within the project's own JS/WASM code, and +** not by client code, so an argument can be made for reducing their +** visibility by not including them in any build-time export lists. +** +** 2022-09-11: it's not yet _proven_ that this approach works in +** non-Emscripten builds. If not, such builds will need to export +** those using the --export=... wasm-ld flag (or equivalent). As of +** this writing we are tied to Emscripten for various reasons +** and cannot test the library with other build environments. +*/ +#define SQLITE_WASM_EXPORT __attribute__((used,visibility("default"))) +// See also: +//__attribute__((export_name("theExportedName"), used, visibility("default"))) + +/* +** Which sqlite3.c we're using needs to be configurable to enable +** building against a custom copy, e.g. the SEE variant. We #include +** the .c file, rather than the header, so that the WASM extensions +** have access to private API internals (namely for kvvfs and +** SQLTester pieces). +** +** The caveat here is that custom variants need to account for +** exporting any necessary symbols (e.g. sqlite3_activate_see()). We +** cannot export them from here using SQLITE_WASM_EXPORT because that +** attribute (apparently) has to be part of the function definition. +*/ +#ifndef SQLITE_C +# define SQLITE_C sqlite3.c /* yes, .c instead of .h. */ +#endif +#define INC__STRINGIFY_(f) #f +#define INC__STRINGIFY(f) INC__STRINGIFY_(f) +#include INC__STRINGIFY(SQLITE_C) +#undef INC__STRINGIFY_ +#undef INC__STRINGIFY +#undef SQLITE_C + +#if 0 +/* +** An EXPERIMENT in implementing a stack-based allocator analog to +** Emscripten's stackSave(), stackAlloc(), stackRestore(). +** Unfortunately, this cannot work together with Emscripten because +** Emscripten defines its own native one and we'd stomp on each +** other's memory. Other than that complication, basic tests show it +** to work just fine. +** +** Another option is to malloc() a chunk of our own and call that our +** "stack". +*/ +SQLITE_WASM_EXPORT void * sqlite3__wasm_stack_end(void){ + extern void __heap_base + /* see https://stackoverflow.com/questions/10038964 */; + return &__heap_base; +} +SQLITE_WASM_EXPORT void * sqlite3__wasm_stack_begin(void){ + extern void __data_end; + return &__data_end; +} +static void * pWasmStackPtr = 0; +SQLITE_WASM_EXPORT void * sqlite3__wasm_stack_ptr(void){ + if(!pWasmStackPtr) pWasmStackPtr = sqlite3__wasm_stack_end(); + return pWasmStackPtr; +} +SQLITE_WASM_EXPORT void sqlite3__wasm_stack_restore(void * p){ + pWasmStackPtr = p; +} +SQLITE_WASM_EXPORT void * sqlite3__wasm_stack_alloc(int n){ + if(n<=0) return 0; + n = (n + 7) & ~7 /* align to 8-byte boundary */; + unsigned char * const p = (unsigned char *)sqlite3__wasm_stack_ptr(); + unsigned const char * const b = (unsigned const char *)sqlite3__wasm_stack_begin(); + if(b + n >= p || b + n < b/*overflow*/) return 0; + return pWasmStackPtr = p - n; +} +#endif /* stack allocator experiment */ + +/* +** State for the "pseudo-stack" allocator implemented in +** sqlite3__wasm_pstack_xyz(). In order to avoid colliding with +** Emscripten-controlled stack space, it carves out a bit of stack +** memory to use for that purpose. This memory ends up in the +** WASM-managed memory, such that routines which manipulate the wasm +** heap can also be used to manipulate this memory. +** +** This particular allocator is intended for small allocations such as +** storage for output pointers. We cannot reasonably size it large +** enough for general-purpose string conversions because some of our +** tests use input files (strings) of 16MB+. +*/ +static unsigned char PStack_mem[ + 1024 * 4 /* API docs guaranty at least 2kb and it's been set at 4kb + since it was introduced. */ +] = {0}; +static struct { + unsigned const char * const pBegin;/* Start (inclusive) of memory */ + unsigned const char * const pEnd; /* One-after-the-end of memory */ + unsigned char * pPos; /* Current stack pointer */ +} PStack = { + &PStack_mem[0], + &PStack_mem[0] + sizeof(PStack_mem), + &PStack_mem[0] + sizeof(PStack_mem) +}; +/* +** Returns the current pstack position. +*/ +SQLITE_WASM_EXPORT void * sqlite3__wasm_pstack_ptr(void){ + return PStack.pPos; +} +/* +** Sets the pstack position pointer to p. Results are undefined if the +** given value did not come from sqlite3__wasm_pstack_ptr(). +*/ +SQLITE_WASM_EXPORT void sqlite3__wasm_pstack_restore(unsigned char * p){ + assert(p>=PStack.pBegin && p<=PStack.pEnd && p>=PStack.pPos); + assert(0==((unsigned long long)p & 0x7)); + if(p>=PStack.pBegin && p<=PStack.pEnd /*&& p>=PStack.pPos*/){ + PStack.pPos = p; + } +} +/* +** Allocate and zero out n bytes from the pstack. Returns a pointer to +** the memory on success, 0 on error (including a negative n value). n +** is always adjusted to be a multiple of 8 and returned memory is +** always zeroed out before returning (because this keeps the client +** JS code from having to do so, and most uses of the pstack will +** call for doing so). +*/ +SQLITE_WASM_EXPORT void * sqlite3__wasm_pstack_alloc(int n){ + if( n<=0 ) return 0; + n = (n + 7) & ~7 /* align to 8-byte boundary */; + if( PStack.pBegin + n > PStack.pPos /*not enough space left*/ + || PStack.pBegin + n <= PStack.pBegin /*overflow*/ ) return 0; + memset((PStack.pPos = PStack.pPos - n), 0, (unsigned int)n); + return PStack.pPos; +} +/* +** Return the number of bytes left which can be +** sqlite3__wasm_pstack_alloc()'d. +*/ +SQLITE_WASM_EXPORT int sqlite3__wasm_pstack_remaining(void){ + assert(PStack.pPos >= PStack.pBegin); + assert(PStack.pPos <= PStack.pEnd); + return (int)(PStack.pPos - PStack.pBegin); +} + +/* +** Return the total number of bytes available in the pstack, including +** any space which is currently allocated. This value is a +** compile-time constant. +*/ +SQLITE_WASM_EXPORT int sqlite3__wasm_pstack_quota(void){ + return (int)(PStack.pEnd - PStack.pBegin); +} + +#if SQLITE_WASM_ENABLE_C_TESTS +struct WasmTestStruct { + int v4; + void * ppV; + const char * cstr; + int64_t v8; + void (*xFunc)(void*); +}; +typedef struct WasmTestStruct WasmTestStruct; +SQLITE_WASM_EXPORT +void sqlite3__wasm_test_struct(WasmTestStruct * s){ + if(s){ + if( 0 ){ + /* Do not be alarmed by the small (and odd) pointer values. + Function pointers in WASM are their index into the + indirect function table, not their address. */ + fprintf(stderr,"%s:%s()@%p s=@%p xFunc=@%p\n", + __FILE__, __func__, + (void*)sqlite3__wasm_test_struct, + s, (void*)s->xFunc); + } + s->v4 *= 2; + s->v8 = s->v4 * 2; + s->ppV = s; + s->cstr = __FILE__; + if(s->xFunc) s->xFunc(s); + } +} +#endif /* SQLITE_WASM_ENABLE_C_TESTS */ + +/* +** This function is NOT part of the sqlite3 public API. It is strictly +** for use by the sqlite project's own JS/WASM bindings. Unlike the +** rest of the sqlite3 API, this part requires C99 for snprintf() and +** variadic macros. +** +** Returns a string containing a JSON-format "enum" of C-level +** constants and struct-related metadata intended to be imported into +** the JS environment. The JSON is initialized the first time this +** function is called and that result is reused for all future calls. +** +** If this function returns NULL then it means that the internal +** buffer is not large enough for the generated JSON and needs to be +** increased. In debug builds that will trigger an assert(). +** +** 2025-09-19: for reasons entirely not understood, building with emcc +** -sMEMORY64=2 causes this function to fail (return 0). -sMEMORY64=1 +** fails to compile with "tables may not be 64-bit" but does not tell +** us where it's happening. +*/ +SQLITE_WASM_EXPORT +const char * sqlite3__wasm_enum_json(void){ + static char aBuffer[1024 * 20] = + {0} /* where the JSON goes. 2025-09-19: output size=19295, but + that can vary slightly from build to build, so a little + leeway is needed here. */; + int n = 0, nChildren = 0, nStruct = 0 + /* output counters for figuring out where commas go */; + char * zPos = &aBuffer[1] /* skip first byte for now to help protect + ** against a small race condition */; + char const * const zEnd = &aBuffer[0] + sizeof(aBuffer) /* one-past-the-end */; + if(aBuffer[0]) return aBuffer; + /* Leave aBuffer[0] at 0 until the end to help guard against a tiny + ** race condition. If this is called twice concurrently, they might + ** end up both writing to aBuffer, but they'll both write the same + ** thing, so that's okay. If we set byte 0 up front then the 2nd + ** instance might return and use the string before the 1st instance + ** is done filling it. */ + +/* Core output macros... */ +#define lenCheck assert(zPos < zEnd - 128 \ + && "sqlite3__wasm_enum_json() buffer is too small."); \ + if( zPos >= zEnd - 128 ) return 0 +#define outf(format,...) \ + zPos += snprintf(zPos, ((size_t)(zEnd - zPos)), format, __VA_ARGS__); \ + lenCheck +#define out(TXT) outf("%s",TXT) +#define CloseBrace(LEVEL) \ + assert(LEVEL<5); memset(zPos, '}', LEVEL); zPos+=LEVEL; lenCheck + +/* Macros for emitting maps of integer- and string-type macros to +** their values. */ +#define DefGroup(KEY) n = 0; \ + outf("%s\"" #KEY "\": {",(nChildren++ ? "," : "")); +#define DefInt(KEY) \ + outf("%s\"%s\": %d", (n++ ? ", " : ""), #KEY, (int)KEY) +#define DefStr(KEY) \ + outf("%s\"%s\": \"%s\"", (n++ ? ", " : ""), #KEY, KEY) +#define _DefGroup CloseBrace(1) + + /* The following groups are sorted alphabetic by group name. */ + DefGroup(access){ + DefInt(SQLITE_ACCESS_EXISTS); + DefInt(SQLITE_ACCESS_READWRITE); + DefInt(SQLITE_ACCESS_READ)/*docs say this is unused*/; + } _DefGroup; + + DefGroup(authorizer){ + DefInt(SQLITE_DENY); + DefInt(SQLITE_IGNORE); + DefInt(SQLITE_CREATE_INDEX); + DefInt(SQLITE_CREATE_TABLE); + DefInt(SQLITE_CREATE_TEMP_INDEX); + DefInt(SQLITE_CREATE_TEMP_TABLE); + DefInt(SQLITE_CREATE_TEMP_TRIGGER); + DefInt(SQLITE_CREATE_TEMP_VIEW); + DefInt(SQLITE_CREATE_TRIGGER); + DefInt(SQLITE_CREATE_VIEW); + DefInt(SQLITE_DELETE); + DefInt(SQLITE_DROP_INDEX); + DefInt(SQLITE_DROP_TABLE); + DefInt(SQLITE_DROP_TEMP_INDEX); + DefInt(SQLITE_DROP_TEMP_TABLE); + DefInt(SQLITE_DROP_TEMP_TRIGGER); + DefInt(SQLITE_DROP_TEMP_VIEW); + DefInt(SQLITE_DROP_TRIGGER); + DefInt(SQLITE_DROP_VIEW); + DefInt(SQLITE_INSERT); + DefInt(SQLITE_PRAGMA); + DefInt(SQLITE_READ); + DefInt(SQLITE_SELECT); + DefInt(SQLITE_TRANSACTION); + DefInt(SQLITE_UPDATE); + DefInt(SQLITE_ATTACH); + DefInt(SQLITE_DETACH); + DefInt(SQLITE_ALTER_TABLE); + DefInt(SQLITE_REINDEX); + DefInt(SQLITE_ANALYZE); + DefInt(SQLITE_CREATE_VTABLE); + DefInt(SQLITE_DROP_VTABLE); + DefInt(SQLITE_FUNCTION); + DefInt(SQLITE_SAVEPOINT); + //DefInt(SQLITE_COPY) /* No longer used */; + DefInt(SQLITE_RECURSIVE); + } _DefGroup; + + DefGroup(blobFinalizers) { + /* SQLITE_STATIC/TRANSIENT need to be handled explicitly as + ** integers to avoid casting-related warnings. */ + out("\"SQLITE_STATIC\":0, \"SQLITE_TRANSIENT\":-1"); + outf(",\"SQLITE_WASM_DEALLOC\": %lld", + (sqlite3_int64)(sqlite3_free)); + } _DefGroup; + + DefGroup(changeset){ +#ifdef SQLITE_CHANGESETSTART_INVERT + DefInt(SQLITE_CHANGESETSTART_INVERT); + DefInt(SQLITE_CHANGESETAPPLY_NOSAVEPOINT); + DefInt(SQLITE_CHANGESETAPPLY_INVERT); + DefInt(SQLITE_CHANGESETAPPLY_IGNORENOOP); + + DefInt(SQLITE_CHANGESET_DATA); + DefInt(SQLITE_CHANGESET_NOTFOUND); + DefInt(SQLITE_CHANGESET_CONFLICT); + DefInt(SQLITE_CHANGESET_CONSTRAINT); + DefInt(SQLITE_CHANGESET_FOREIGN_KEY); + + DefInt(SQLITE_CHANGESET_OMIT); + DefInt(SQLITE_CHANGESET_REPLACE); + DefInt(SQLITE_CHANGESET_ABORT); +#endif + } _DefGroup; + + DefGroup(config){ + DefInt(SQLITE_CONFIG_SINGLETHREAD); + DefInt(SQLITE_CONFIG_MULTITHREAD); + DefInt(SQLITE_CONFIG_SERIALIZED); + DefInt(SQLITE_CONFIG_MALLOC); + DefInt(SQLITE_CONFIG_GETMALLOC); + DefInt(SQLITE_CONFIG_SCRATCH); + DefInt(SQLITE_CONFIG_PAGECACHE); + DefInt(SQLITE_CONFIG_HEAP); + DefInt(SQLITE_CONFIG_MEMSTATUS); + DefInt(SQLITE_CONFIG_MUTEX); + DefInt(SQLITE_CONFIG_GETMUTEX); +/* previously SQLITE_CONFIG_CHUNKALLOC 12 which is now unused. */ + DefInt(SQLITE_CONFIG_LOOKASIDE); + DefInt(SQLITE_CONFIG_PCACHE); + DefInt(SQLITE_CONFIG_GETPCACHE); + DefInt(SQLITE_CONFIG_LOG); + DefInt(SQLITE_CONFIG_URI); + DefInt(SQLITE_CONFIG_PCACHE2); + DefInt(SQLITE_CONFIG_GETPCACHE2); + DefInt(SQLITE_CONFIG_COVERING_INDEX_SCAN); + DefInt(SQLITE_CONFIG_SQLLOG); + DefInt(SQLITE_CONFIG_MMAP_SIZE); + DefInt(SQLITE_CONFIG_WIN32_HEAPSIZE); + DefInt(SQLITE_CONFIG_PCACHE_HDRSZ); + DefInt(SQLITE_CONFIG_PMASZ); + DefInt(SQLITE_CONFIG_STMTJRNL_SPILL); + DefInt(SQLITE_CONFIG_SMALL_MALLOC); + DefInt(SQLITE_CONFIG_SORTERREF_SIZE); + DefInt(SQLITE_CONFIG_MEMDB_MAXSIZE); + /* maintenance note: we specifically do not include + SQLITE_CONFIG_ROWID_IN_VIEW here, on the grounds that + it's only for legacy support and no apps written with + this API require that. */ + } _DefGroup; + + DefGroup(dataTypes) { + DefInt(SQLITE_INTEGER); + DefInt(SQLITE_FLOAT); + DefInt(SQLITE_TEXT); + DefInt(SQLITE_BLOB); + DefInt(SQLITE_NULL); + } _DefGroup; + + DefGroup(dbConfig){ + DefInt(SQLITE_DBCONFIG_MAINDBNAME); + DefInt(SQLITE_DBCONFIG_LOOKASIDE); + DefInt(SQLITE_DBCONFIG_ENABLE_FKEY); + DefInt(SQLITE_DBCONFIG_ENABLE_TRIGGER); + DefInt(SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION); + DefInt(SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE); + DefInt(SQLITE_DBCONFIG_ENABLE_QPSG); + DefInt(SQLITE_DBCONFIG_TRIGGER_EQP); + DefInt(SQLITE_DBCONFIG_RESET_DATABASE); + DefInt(SQLITE_DBCONFIG_DEFENSIVE); + DefInt(SQLITE_DBCONFIG_WRITABLE_SCHEMA); + DefInt(SQLITE_DBCONFIG_LEGACY_ALTER_TABLE); + DefInt(SQLITE_DBCONFIG_DQS_DML); + DefInt(SQLITE_DBCONFIG_DQS_DDL); + DefInt(SQLITE_DBCONFIG_ENABLE_VIEW); + DefInt(SQLITE_DBCONFIG_LEGACY_FILE_FORMAT); + DefInt(SQLITE_DBCONFIG_TRUSTED_SCHEMA); + DefInt(SQLITE_DBCONFIG_STMT_SCANSTATUS); + DefInt(SQLITE_DBCONFIG_REVERSE_SCANORDER); + DefInt(SQLITE_DBCONFIG_ENABLE_ATTACH_CREATE); + DefInt(SQLITE_DBCONFIG_ENABLE_ATTACH_WRITE); + DefInt(SQLITE_DBCONFIG_ENABLE_COMMENTS); + DefInt(SQLITE_DBCONFIG_MAX); + } _DefGroup; + + DefGroup(dbStatus){ + DefInt(SQLITE_DBSTATUS_LOOKASIDE_USED); + DefInt(SQLITE_DBSTATUS_CACHE_USED); + DefInt(SQLITE_DBSTATUS_SCHEMA_USED); + DefInt(SQLITE_DBSTATUS_STMT_USED); + DefInt(SQLITE_DBSTATUS_LOOKASIDE_HIT); + DefInt(SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE); + DefInt(SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL); + DefInt(SQLITE_DBSTATUS_CACHE_HIT); + DefInt(SQLITE_DBSTATUS_CACHE_MISS); + DefInt(SQLITE_DBSTATUS_CACHE_WRITE); + DefInt(SQLITE_DBSTATUS_DEFERRED_FKS); + DefInt(SQLITE_DBSTATUS_CACHE_USED_SHARED); + DefInt(SQLITE_DBSTATUS_CACHE_SPILL); + DefInt(SQLITE_DBSTATUS_TEMPBUF_SPILL); + DefInt(SQLITE_DBSTATUS_MAX); + } _DefGroup; + + DefGroup(encodings) { + /* Noting that the wasm binding only aims to support UTF-8. */ + DefInt(SQLITE_UTF8); + DefInt(SQLITE_UTF16LE); + DefInt(SQLITE_UTF16BE); + DefInt(SQLITE_UTF16); + /*deprecated DefInt(SQLITE_ANY); */ + DefInt(SQLITE_UTF16_ALIGNED); + } _DefGroup; + + DefGroup(fcntl) { + DefInt(SQLITE_FCNTL_LOCKSTATE); + DefInt(SQLITE_FCNTL_GET_LOCKPROXYFILE); + DefInt(SQLITE_FCNTL_SET_LOCKPROXYFILE); + DefInt(SQLITE_FCNTL_LAST_ERRNO); + DefInt(SQLITE_FCNTL_SIZE_HINT); + DefInt(SQLITE_FCNTL_CHUNK_SIZE); + DefInt(SQLITE_FCNTL_FILE_POINTER); + DefInt(SQLITE_FCNTL_SYNC_OMITTED); + DefInt(SQLITE_FCNTL_WIN32_AV_RETRY); + DefInt(SQLITE_FCNTL_PERSIST_WAL); + DefInt(SQLITE_FCNTL_OVERWRITE); + DefInt(SQLITE_FCNTL_VFSNAME); + DefInt(SQLITE_FCNTL_POWERSAFE_OVERWRITE); + DefInt(SQLITE_FCNTL_PRAGMA); + DefInt(SQLITE_FCNTL_BUSYHANDLER); + DefInt(SQLITE_FCNTL_TEMPFILENAME); + DefInt(SQLITE_FCNTL_MMAP_SIZE); + DefInt(SQLITE_FCNTL_TRACE); + DefInt(SQLITE_FCNTL_HAS_MOVED); + DefInt(SQLITE_FCNTL_SYNC); + DefInt(SQLITE_FCNTL_COMMIT_PHASETWO); + DefInt(SQLITE_FCNTL_WIN32_SET_HANDLE); + DefInt(SQLITE_FCNTL_WAL_BLOCK); + DefInt(SQLITE_FCNTL_ZIPVFS); + DefInt(SQLITE_FCNTL_RBU); + DefInt(SQLITE_FCNTL_VFS_POINTER); + DefInt(SQLITE_FCNTL_JOURNAL_POINTER); + DefInt(SQLITE_FCNTL_WIN32_GET_HANDLE); + DefInt(SQLITE_FCNTL_PDB); + DefInt(SQLITE_FCNTL_BEGIN_ATOMIC_WRITE); + DefInt(SQLITE_FCNTL_COMMIT_ATOMIC_WRITE); + DefInt(SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE); + DefInt(SQLITE_FCNTL_LOCK_TIMEOUT); + DefInt(SQLITE_FCNTL_DATA_VERSION); + DefInt(SQLITE_FCNTL_SIZE_LIMIT); + DefInt(SQLITE_FCNTL_CKPT_DONE); + DefInt(SQLITE_FCNTL_RESERVE_BYTES); + DefInt(SQLITE_FCNTL_CKPT_START); + DefInt(SQLITE_FCNTL_EXTERNAL_READER); + DefInt(SQLITE_FCNTL_CKSM_FILE); + DefInt(SQLITE_FCNTL_RESET_CACHE); + } _DefGroup; + + DefGroup(flock) { + DefInt(SQLITE_LOCK_NONE); + DefInt(SQLITE_LOCK_SHARED); + DefInt(SQLITE_LOCK_RESERVED); + DefInt(SQLITE_LOCK_PENDING); + DefInt(SQLITE_LOCK_EXCLUSIVE); + } _DefGroup; + + DefGroup(ioCap) { + DefInt(SQLITE_IOCAP_ATOMIC); + DefInt(SQLITE_IOCAP_ATOMIC512); + DefInt(SQLITE_IOCAP_ATOMIC1K); + DefInt(SQLITE_IOCAP_ATOMIC2K); + DefInt(SQLITE_IOCAP_ATOMIC4K); + DefInt(SQLITE_IOCAP_ATOMIC8K); + DefInt(SQLITE_IOCAP_ATOMIC16K); + DefInt(SQLITE_IOCAP_ATOMIC32K); + DefInt(SQLITE_IOCAP_ATOMIC64K); + DefInt(SQLITE_IOCAP_SAFE_APPEND); + DefInt(SQLITE_IOCAP_SEQUENTIAL); + DefInt(SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN); + DefInt(SQLITE_IOCAP_POWERSAFE_OVERWRITE); + DefInt(SQLITE_IOCAP_IMMUTABLE); + DefInt(SQLITE_IOCAP_BATCH_ATOMIC); + } _DefGroup; + + DefGroup(limits) { + DefInt(SQLITE_MAX_ALLOCATION_SIZE); + DefInt(SQLITE_LIMIT_LENGTH); + DefInt(SQLITE_MAX_LENGTH); + DefInt(SQLITE_LIMIT_SQL_LENGTH); + DefInt(SQLITE_MAX_SQL_LENGTH); + DefInt(SQLITE_LIMIT_COLUMN); + DefInt(SQLITE_MAX_COLUMN); + DefInt(SQLITE_LIMIT_EXPR_DEPTH); + DefInt(SQLITE_MAX_EXPR_DEPTH); + DefInt(SQLITE_LIMIT_COMPOUND_SELECT); + DefInt(SQLITE_MAX_COMPOUND_SELECT); + DefInt(SQLITE_LIMIT_VDBE_OP); + DefInt(SQLITE_MAX_VDBE_OP); + DefInt(SQLITE_LIMIT_FUNCTION_ARG); + DefInt(SQLITE_MAX_FUNCTION_ARG); + DefInt(SQLITE_LIMIT_ATTACHED); + DefInt(SQLITE_MAX_ATTACHED); + DefInt(SQLITE_LIMIT_LIKE_PATTERN_LENGTH); + DefInt(SQLITE_MAX_LIKE_PATTERN_LENGTH); + DefInt(SQLITE_LIMIT_VARIABLE_NUMBER); + DefInt(SQLITE_MAX_VARIABLE_NUMBER); + DefInt(SQLITE_LIMIT_TRIGGER_DEPTH); + DefInt(SQLITE_MAX_TRIGGER_DEPTH); + DefInt(SQLITE_LIMIT_WORKER_THREADS); + DefInt(SQLITE_MAX_WORKER_THREADS); + } _DefGroup; + + DefGroup(openFlags) { + /* Noting that not all of these will have any effect in + ** WASM-space. */ + DefInt(SQLITE_OPEN_READONLY); + DefInt(SQLITE_OPEN_READWRITE); + DefInt(SQLITE_OPEN_CREATE); + DefInt(SQLITE_OPEN_URI); + DefInt(SQLITE_OPEN_MEMORY); + DefInt(SQLITE_OPEN_NOMUTEX); + DefInt(SQLITE_OPEN_FULLMUTEX); + DefInt(SQLITE_OPEN_SHAREDCACHE); + DefInt(SQLITE_OPEN_PRIVATECACHE); + DefInt(SQLITE_OPEN_EXRESCODE); + DefInt(SQLITE_OPEN_NOFOLLOW); + /* OPEN flags for use with VFSes... */ + DefInt(SQLITE_OPEN_MAIN_DB); + DefInt(SQLITE_OPEN_MAIN_JOURNAL); + DefInt(SQLITE_OPEN_TEMP_DB); + DefInt(SQLITE_OPEN_TEMP_JOURNAL); + DefInt(SQLITE_OPEN_TRANSIENT_DB); + DefInt(SQLITE_OPEN_SUBJOURNAL); + DefInt(SQLITE_OPEN_SUPER_JOURNAL); + DefInt(SQLITE_OPEN_WAL); + DefInt(SQLITE_OPEN_DELETEONCLOSE); + DefInt(SQLITE_OPEN_EXCLUSIVE); + } _DefGroup; + + DefGroup(prepareFlags) { + DefInt(SQLITE_PREPARE_PERSISTENT); + DefInt(SQLITE_PREPARE_NORMALIZE); + DefInt(SQLITE_PREPARE_NO_VTAB); + } _DefGroup; + + DefGroup(resultCodes) { + DefInt(SQLITE_OK); + DefInt(SQLITE_ERROR); + DefInt(SQLITE_INTERNAL); + DefInt(SQLITE_PERM); + DefInt(SQLITE_ABORT); + DefInt(SQLITE_BUSY); + DefInt(SQLITE_LOCKED); + DefInt(SQLITE_NOMEM); + DefInt(SQLITE_READONLY); + DefInt(SQLITE_INTERRUPT); + DefInt(SQLITE_IOERR); + DefInt(SQLITE_CORRUPT); + DefInt(SQLITE_NOTFOUND); + DefInt(SQLITE_FULL); + DefInt(SQLITE_CANTOPEN); + DefInt(SQLITE_PROTOCOL); + DefInt(SQLITE_EMPTY); + DefInt(SQLITE_SCHEMA); + DefInt(SQLITE_TOOBIG); + DefInt(SQLITE_CONSTRAINT); + DefInt(SQLITE_MISMATCH); + DefInt(SQLITE_MISUSE); + DefInt(SQLITE_NOLFS); + DefInt(SQLITE_AUTH); + DefInt(SQLITE_FORMAT); + DefInt(SQLITE_RANGE); + DefInt(SQLITE_NOTADB); + DefInt(SQLITE_NOTICE); + DefInt(SQLITE_WARNING); + DefInt(SQLITE_ROW); + DefInt(SQLITE_DONE); + // Extended Result Codes + DefInt(SQLITE_ERROR_MISSING_COLLSEQ); + DefInt(SQLITE_ERROR_RETRY); + DefInt(SQLITE_ERROR_SNAPSHOT); + DefInt(SQLITE_IOERR_READ); + DefInt(SQLITE_IOERR_SHORT_READ); + DefInt(SQLITE_IOERR_WRITE); + DefInt(SQLITE_IOERR_FSYNC); + DefInt(SQLITE_IOERR_DIR_FSYNC); + DefInt(SQLITE_IOERR_TRUNCATE); + DefInt(SQLITE_IOERR_FSTAT); + DefInt(SQLITE_IOERR_UNLOCK); + DefInt(SQLITE_IOERR_RDLOCK); + DefInt(SQLITE_IOERR_DELETE); + DefInt(SQLITE_IOERR_BLOCKED); + DefInt(SQLITE_IOERR_NOMEM); + DefInt(SQLITE_IOERR_ACCESS); + DefInt(SQLITE_IOERR_CHECKRESERVEDLOCK); + DefInt(SQLITE_IOERR_LOCK); + DefInt(SQLITE_IOERR_CLOSE); + DefInt(SQLITE_IOERR_DIR_CLOSE); + DefInt(SQLITE_IOERR_SHMOPEN); + DefInt(SQLITE_IOERR_SHMSIZE); + DefInt(SQLITE_IOERR_SHMLOCK); + DefInt(SQLITE_IOERR_SHMMAP); + DefInt(SQLITE_IOERR_SEEK); + DefInt(SQLITE_IOERR_DELETE_NOENT); + DefInt(SQLITE_IOERR_MMAP); + DefInt(SQLITE_IOERR_GETTEMPPATH); + DefInt(SQLITE_IOERR_CONVPATH); + DefInt(SQLITE_IOERR_VNODE); + DefInt(SQLITE_IOERR_AUTH); + DefInt(SQLITE_IOERR_BEGIN_ATOMIC); + DefInt(SQLITE_IOERR_COMMIT_ATOMIC); + DefInt(SQLITE_IOERR_ROLLBACK_ATOMIC); + DefInt(SQLITE_IOERR_DATA); + DefInt(SQLITE_IOERR_CORRUPTFS); + DefInt(SQLITE_LOCKED_SHAREDCACHE); + DefInt(SQLITE_LOCKED_VTAB); + DefInt(SQLITE_BUSY_RECOVERY); + DefInt(SQLITE_BUSY_SNAPSHOT); + DefInt(SQLITE_BUSY_TIMEOUT); + DefInt(SQLITE_CANTOPEN_NOTEMPDIR); + DefInt(SQLITE_CANTOPEN_ISDIR); + DefInt(SQLITE_CANTOPEN_FULLPATH); + DefInt(SQLITE_CANTOPEN_CONVPATH); + //DefInt(SQLITE_CANTOPEN_DIRTYWAL)/*docs say not used*/; + DefInt(SQLITE_CANTOPEN_SYMLINK); + DefInt(SQLITE_CORRUPT_VTAB); + DefInt(SQLITE_CORRUPT_SEQUENCE); + DefInt(SQLITE_CORRUPT_INDEX); + DefInt(SQLITE_READONLY_RECOVERY); + DefInt(SQLITE_READONLY_CANTLOCK); + DefInt(SQLITE_READONLY_ROLLBACK); + DefInt(SQLITE_READONLY_DBMOVED); + DefInt(SQLITE_READONLY_CANTINIT); + DefInt(SQLITE_READONLY_DIRECTORY); + DefInt(SQLITE_ABORT_ROLLBACK); + DefInt(SQLITE_CONSTRAINT_CHECK); + DefInt(SQLITE_CONSTRAINT_COMMITHOOK); + DefInt(SQLITE_CONSTRAINT_FOREIGNKEY); + DefInt(SQLITE_CONSTRAINT_FUNCTION); + DefInt(SQLITE_CONSTRAINT_NOTNULL); + DefInt(SQLITE_CONSTRAINT_PRIMARYKEY); + DefInt(SQLITE_CONSTRAINT_TRIGGER); + DefInt(SQLITE_CONSTRAINT_UNIQUE); + DefInt(SQLITE_CONSTRAINT_VTAB); + DefInt(SQLITE_CONSTRAINT_ROWID); + DefInt(SQLITE_CONSTRAINT_PINNED); + DefInt(SQLITE_CONSTRAINT_DATATYPE); + DefInt(SQLITE_NOTICE_RECOVER_WAL); + DefInt(SQLITE_NOTICE_RECOVER_ROLLBACK); + DefInt(SQLITE_WARNING_AUTOINDEX); + DefInt(SQLITE_AUTH_USER); + DefInt(SQLITE_OK_LOAD_PERMANENTLY); + //DefInt(SQLITE_OK_SYMLINK) /* internal use only */; + } _DefGroup; + + DefGroup(serialize){ + DefInt(SQLITE_SERIALIZE_NOCOPY); + DefInt(SQLITE_DESERIALIZE_FREEONCLOSE); + DefInt(SQLITE_DESERIALIZE_READONLY); + DefInt(SQLITE_DESERIALIZE_RESIZEABLE); + } _DefGroup; + + DefGroup(session){ +#ifdef SQLITE_SESSION_CONFIG_STRMSIZE + DefInt(SQLITE_SESSION_CONFIG_STRMSIZE); + DefInt(SQLITE_SESSION_OBJCONFIG_SIZE); +#endif + } _DefGroup; + + DefGroup(sqlite3Status){ + DefInt(SQLITE_STATUS_MEMORY_USED); + DefInt(SQLITE_STATUS_PAGECACHE_USED); + DefInt(SQLITE_STATUS_PAGECACHE_OVERFLOW); + //DefInt(SQLITE_STATUS_SCRATCH_USED) /* NOT USED */; + //DefInt(SQLITE_STATUS_SCRATCH_OVERFLOW) /* NOT USED */; + DefInt(SQLITE_STATUS_MALLOC_SIZE); + DefInt(SQLITE_STATUS_PARSER_STACK); + DefInt(SQLITE_STATUS_PAGECACHE_SIZE); + //DefInt(SQLITE_STATUS_SCRATCH_SIZE) /* NOT USED */; + DefInt(SQLITE_STATUS_MALLOC_COUNT); + } _DefGroup; + + DefGroup(stmtStatus){ + DefInt(SQLITE_STMTSTATUS_FULLSCAN_STEP); + DefInt(SQLITE_STMTSTATUS_SORT); + DefInt(SQLITE_STMTSTATUS_AUTOINDEX); + DefInt(SQLITE_STMTSTATUS_VM_STEP); + DefInt(SQLITE_STMTSTATUS_REPREPARE); + DefInt(SQLITE_STMTSTATUS_RUN); + DefInt(SQLITE_STMTSTATUS_FILTER_MISS); + DefInt(SQLITE_STMTSTATUS_FILTER_HIT); + DefInt(SQLITE_STMTSTATUS_MEMUSED); + } _DefGroup; + + DefGroup(syncFlags) { + DefInt(SQLITE_SYNC_NORMAL); + DefInt(SQLITE_SYNC_FULL); + DefInt(SQLITE_SYNC_DATAONLY); + } _DefGroup; + + DefGroup(trace) { + DefInt(SQLITE_TRACE_STMT); + DefInt(SQLITE_TRACE_PROFILE); + DefInt(SQLITE_TRACE_ROW); + DefInt(SQLITE_TRACE_CLOSE); + } _DefGroup; + + DefGroup(txnState){ + DefInt(SQLITE_TXN_NONE); + DefInt(SQLITE_TXN_READ); + DefInt(SQLITE_TXN_WRITE); + } _DefGroup; + + DefGroup(udfFlags) { + DefInt(SQLITE_DETERMINISTIC); + DefInt(SQLITE_DIRECTONLY); + DefInt(SQLITE_INNOCUOUS); + DefInt(SQLITE_SUBTYPE); + DefInt(SQLITE_RESULT_SUBTYPE); + DefInt(SQLITE_SELFORDER1); + } _DefGroup; + + DefGroup(version) { + DefInt(SQLITE_VERSION_NUMBER); + DefStr(SQLITE_VERSION); + DefStr(SQLITE_SOURCE_ID); + } _DefGroup; + + DefGroup(vtab) { +#if SQLITE_WASM_HAS_VTAB + DefInt(SQLITE_INDEX_SCAN_UNIQUE); + DefInt(SQLITE_INDEX_CONSTRAINT_EQ); + DefInt(SQLITE_INDEX_CONSTRAINT_GT); + DefInt(SQLITE_INDEX_CONSTRAINT_LE); + DefInt(SQLITE_INDEX_CONSTRAINT_LT); + DefInt(SQLITE_INDEX_CONSTRAINT_GE); + DefInt(SQLITE_INDEX_CONSTRAINT_MATCH); + DefInt(SQLITE_INDEX_CONSTRAINT_LIKE); + DefInt(SQLITE_INDEX_CONSTRAINT_GLOB); + DefInt(SQLITE_INDEX_CONSTRAINT_REGEXP); + DefInt(SQLITE_INDEX_CONSTRAINT_NE); + DefInt(SQLITE_INDEX_CONSTRAINT_ISNOT); + DefInt(SQLITE_INDEX_CONSTRAINT_ISNOTNULL); + DefInt(SQLITE_INDEX_CONSTRAINT_ISNULL); + DefInt(SQLITE_INDEX_CONSTRAINT_IS); + DefInt(SQLITE_INDEX_CONSTRAINT_LIMIT); + DefInt(SQLITE_INDEX_CONSTRAINT_OFFSET); + DefInt(SQLITE_INDEX_CONSTRAINT_FUNCTION); + DefInt(SQLITE_VTAB_CONSTRAINT_SUPPORT); + DefInt(SQLITE_VTAB_INNOCUOUS); + DefInt(SQLITE_VTAB_DIRECTONLY); + DefInt(SQLITE_VTAB_USES_ALL_SCHEMAS); + DefInt(SQLITE_ROLLBACK); + //DefInt(SQLITE_IGNORE); // Also used by sqlite3_authorizer() callback + DefInt(SQLITE_FAIL); + //DefInt(SQLITE_ABORT); // Also an error code + DefInt(SQLITE_REPLACE); +#endif /*SQLITE_WASM_HAS_VTAB*/ + } _DefGroup; + +#undef DefGroup +#undef DefStr +#undef DefInt +#undef _DefGroup + + /* + ** Emit an array of "StructBinder" struct descriptions, which look + ** like: + ** + ** { + ** "name": "MyStruct", + ** "sizeof": 16, + ** "members": { + ** "member1": {"offset": 0,"sizeof": 4,"signature": "i"}, + ** "member2": {"offset": 4,"sizeof": 4,"signature": "p"}, + ** "member3": {"offset": 8,"sizeof": 8,"signature": "j"} + ** } + ** } + ** + ** Detailed documentation for those bits are in the Jaccwabyt + ** JS-side component. + */ + + /** Macros for emitting StructBinder description. */ +#define StructBinder__(TYPE) \ + n = 0; \ + outf("%s{", (nStruct++ ? ", " : "")); \ + out("\"name\": \"" # TYPE "\","); \ + outf("\"sizeof\": %d", (int)sizeof(TYPE)); \ + out(",\"members\": {"); +#define StructBinder_(T) StructBinder__(T) + /** ^^^ indirection needed to expand CurrentStruct */ +#define StructBinder StructBinder_(CurrentStruct) +#define _StructBinder CloseBrace(2) +#define M(MEMBER,SIG) \ + outf("%s\"%s\": " \ + "{\"offset\":%d,\"sizeof\": %d,\"signature\":\"%s\"}", \ + (n++ ? ", " : ""), #MEMBER, \ + (int)offsetof(CurrentStruct,MEMBER), \ + (int)sizeof(((CurrentStruct*)0)->MEMBER), \ + SIG) + + nStruct = 0; + out(", \"structs\": ["); { + +#define CurrentStruct sqlite3_vfs + StructBinder { + M(iVersion, "i"); + M(szOsFile, "i"); + M(mxPathname, "i"); + M(pNext, "p"); + M(zName, "s"); + M(pAppData, "p"); + M(xOpen, "i(pppip)"); + M(xDelete, "i(ppi)"); + M(xAccess, "i(ppip)"); + M(xFullPathname, "i(ppip)"); + M(xDlOpen, "v(pp)"); + M(xDlError, "v(pip)"); + M(xDlSym, "p()"); + M(xDlClose, "v(pp)"); + M(xRandomness, "i(pip)"); + M(xSleep, "i(pi)"); + M(xCurrentTime, "i(pp)"); + M(xGetLastError, "i(pip)"); + M(xCurrentTimeInt64, "i(pp)"); + M(xSetSystemCall, "i(ppp)"); + M(xGetSystemCall, "p(pp)"); + M(xNextSystemCall, "p(pp)"); + } _StructBinder; +#undef CurrentStruct + +#define CurrentStruct sqlite3_io_methods + StructBinder { + M(iVersion, "i"); + M(xClose, "i(p)"); + M(xRead, "i(ppij)"); + M(xWrite, "i(ppij)"); + M(xTruncate, "i(pj)"); + M(xSync, "i(pi)"); + M(xFileSize, "i(pp)"); + M(xLock, "i(pi)"); + M(xUnlock, "i(pi)"); + M(xCheckReservedLock, "i(pp)"); + M(xFileControl, "i(pip)"); + M(xSectorSize, "i(p)"); + M(xDeviceCharacteristics, "i(p)"); + M(xShmMap, "i(piiip)"); + M(xShmLock, "i(piii)"); + M(xShmBarrier, "v(p)"); + M(xShmUnmap, "i(pi)"); + M(xFetch, "i(pjip)"); + M(xUnfetch, "i(pjp)"); + } _StructBinder; +#undef CurrentStruct + +#define CurrentStruct sqlite3_file + StructBinder { + M(pMethods, "p"); + } _StructBinder; +#undef CurrentStruct + +#define CurrentStruct sqlite3_kvvfs_methods + StructBinder { + M(xRead, "i(sspi)"); + M(xWrite, "i(sss)"); + M(xDelete, "i(ss)"); + M(nKeySize, "i"); + } _StructBinder; +#undef CurrentStruct + +#if SQLITE_WASM_HAS_VTAB +#define CurrentStruct sqlite3_vtab + StructBinder { + M(pModule, "p"); + M(nRef, "i"); + M(zErrMsg, "p"); + } _StructBinder; +#undef CurrentStruct + +#define CurrentStruct sqlite3_vtab_cursor + StructBinder { + M(pVtab, "p"); + } _StructBinder; +#undef CurrentStruct + +#define CurrentStruct sqlite3_module + StructBinder { + M(iVersion, "i"); + M(xCreate, "i(ppippp)"); + M(xConnect, "i(ppippp)"); + M(xBestIndex, "i(pp)"); + M(xDisconnect, "i(p)"); + M(xDestroy, "i(p)"); + M(xOpen, "i(pp)"); + M(xClose, "i(p)"); + M(xFilter, "i(pisip)"); + M(xNext, "i(p)"); + M(xEof, "i(p)"); + M(xColumn, "i(ppi)"); + M(xRowid, "i(pp)"); + M(xUpdate, "i(pipp)"); + M(xBegin, "i(p)"); + M(xSync, "i(p)"); + M(xCommit, "i(p)"); + M(xRollback, "i(p)"); + M(xFindFunction, "i(pispp)"); + M(xRename, "i(ps)"); + // ^^^ v1. v2+ follows... + M(xSavepoint, "i(pi)"); + M(xRelease, "i(pi)"); + M(xRollbackTo, "i(pi)"); + // ^^^ v2. v3+ follows... + M(xShadowName, "i(s)"); + // ^^^ v3. v4+ follows... + M(xIntegrity, "i(pppip)"); + } _StructBinder; +#undef CurrentStruct + + /** + ** Workaround: in order to map the various inner structs from + ** sqlite3_index_info, we have to uplift those into constructs we + ** can access by type name. These structs _must_ match their + ** in-sqlite3_index_info counterparts byte for byte. + */ + typedef struct { + int iColumn; + unsigned char op; + unsigned char usable; + int iTermOffset; + } sqlite3_index_constraint; + typedef struct { + int iColumn; + unsigned char desc; + } sqlite3_index_orderby; + typedef struct { + int argvIndex; + unsigned char omit; + } sqlite3_index_constraint_usage; + { /* Validate that the above struct sizeof()s match + ** expectations. We could improve upon this by + ** checking the offsetof() for each member. */ + const sqlite3_index_info siiCheck = {0}; +#define IndexSzCheck(T,M) \ + (sizeof(T) == sizeof(*siiCheck.M)) + if(!IndexSzCheck(sqlite3_index_constraint,aConstraint) + || !IndexSzCheck(sqlite3_index_orderby,aOrderBy) + || !IndexSzCheck(sqlite3_index_constraint_usage,aConstraintUsage)){ + assert(!"sizeof mismatch in sqlite3_index_... struct(s)"); + return 0; + } +#undef IndexSzCheck + } + +#define CurrentStruct sqlite3_index_constraint + StructBinder { + M(iColumn, "i"); + M(op, "C"); + M(usable, "C"); + M(iTermOffset, "i"); + } _StructBinder; +#undef CurrentStruct + +#define CurrentStruct sqlite3_index_orderby + StructBinder { + M(iColumn, "i"); + M(desc, "C"); + } _StructBinder; +#undef CurrentStruct + +#define CurrentStruct sqlite3_index_constraint_usage + StructBinder { + M(argvIndex, "i"); + M(omit, "C"); + } _StructBinder; +#undef CurrentStruct + +#define CurrentStruct sqlite3_index_info + StructBinder { + M(nConstraint, "i"); + M(aConstraint, "p"); + M(nOrderBy, "i"); + M(aOrderBy, "p"); + M(aConstraintUsage, "p"); + M(idxNum, "i"); + M(idxStr, "p"); + M(needToFreeIdxStr, "i"); + M(orderByConsumed, "i"); + M(estimatedCost, "d"); + M(estimatedRows, "j"); + M(idxFlags, "i"); + M(colUsed, "j"); + } _StructBinder; +#undef CurrentStruct + +#endif /*SQLITE_WASM_HAS_VTAB*/ + +#if SQLITE_WASM_ENABLE_C_TESTS +#define CurrentStruct WasmTestStruct + StructBinder { + M(v4, "i"); + M(cstr, "s"); + M(ppV, "p"); + M(v8, "j"); + M(xFunc, "v(p)"); + } _StructBinder; +#undef CurrentStruct +#endif /*SQLITE_WASM_ENABLE_C_TESTS*/ + + } out( "]"/*structs*/); + + out("}"/*top-level object*/); + *zPos = 0; + aBuffer[0] = '{'/*end of the race-condition workaround*/; + return aBuffer; +#undef StructBinder +#undef StructBinder_ +#undef StructBinder__ +#undef M +#undef _StructBinder +#undef CloseBrace +#undef out +#undef outf +#undef lenCheck +} + +/* +** This function is NOT part of the sqlite3 public API. It is strictly +** for use by the sqlite project's own JS/WASM bindings. +** +** This function invokes the xDelete method of the given VFS (or the +** default VFS if pVfs is NULL), passing on the given filename. If +** zName is NULL, no default VFS is found, or it has no xDelete +** method, SQLITE_MISUSE is returned, else the result of the xDelete() +** call is returned. +*/ +SQLITE_WASM_EXPORT +int sqlite3__wasm_vfs_unlink(sqlite3_vfs *pVfs, const char *zName){ + int rc = SQLITE_MISUSE /* ??? */; + if( 0==pVfs && 0!=zName ) pVfs = sqlite3_vfs_find(0); + if( zName && pVfs && pVfs->xDelete ){ + rc = pVfs->xDelete(pVfs, zName, 1); + } + return rc; +} + +/* +** This function is NOT part of the sqlite3 public API. It is strictly +** for use by the sqlite project's own JS/WASM bindings. +** +** Returns a pointer to the given DB's VFS for the given DB name, +** defaulting to "main" if zDbName is 0. Returns 0 if no db with the +** given name is open. +*/ +SQLITE_WASM_EXPORT +sqlite3_vfs * sqlite3__wasm_db_vfs(sqlite3 *pDb, const char *zDbName){ + sqlite3_vfs * pVfs = 0; + sqlite3_file_control(pDb, zDbName ? zDbName : "main", + SQLITE_FCNTL_VFS_POINTER, &pVfs); + return pVfs; +} + +/* +** This function is NOT part of the sqlite3 public API. It is strictly +** for use by the sqlite project's own JS/WASM bindings. +** +** This function resets the given db pointer's database as described at +** +** https://sqlite.org/c3ref/c_dbconfig_defensive.html#sqlitedbconfigresetdatabase +** +** But beware: virtual tables destroyed that way do not have their +** xDestroy() called, so will leak if they require that function for +** proper cleanup. +** +** Returns 0 on success, an SQLITE_xxx code on error. Returns +** SQLITE_MISUSE if pDb is NULL. +*/ +SQLITE_WASM_EXPORT +int sqlite3__wasm_db_reset(sqlite3 *pDb){ + int rc = SQLITE_MISUSE; + if( pDb ){ + sqlite3_table_column_metadata(pDb, "main", 0, 0, 0, 0, 0, 0, 0); + rc = sqlite3_db_config(pDb, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0); + if( 0==rc ){ + rc = sqlite3_exec(pDb, "VACUUM", 0, 0, 0); + sqlite3_db_config(pDb, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0); + } + } + return rc; +} + +/* +** This function is NOT part of the sqlite3 public API. It is strictly +** for use by the sqlite project's own JS/WASM bindings. +** +** Uses the given database's VFS xRead to stream the db file's +** contents out to the given callback. The callback gets a single +** chunk of size n (its 2nd argument) on each call and must return 0 +** on success, non-0 on error. This function returns 0 on success, +** SQLITE_NOTFOUND if no db is open, or propagates any other non-0 +** code from the callback. Note that this is not thread-friendly: it +** expects that it will be the only thread reading the db file and +** takes no measures to ensure that is the case. +** +** This implementation appears to work fine, but +** sqlite3__wasm_db_serialize() is arguably the better way to achieve +** this. +*/ +SQLITE_WASM_EXPORT +int sqlite3__wasm_db_export_chunked( sqlite3* pDb, + int (*xCallback)(unsigned const char *zOut, int n) ){ + sqlite3_int64 nSize = 0; + sqlite3_int64 nPos = 0; + sqlite3_file * pFile = 0; + unsigned char buf[1024 * 8]; + int nBuf = (int)sizeof(buf); + int rc = pDb + ? sqlite3_file_control(pDb, "main", + SQLITE_FCNTL_FILE_POINTER, &pFile) + : SQLITE_NOTFOUND; + if( rc ) return rc; + rc = pFile->pMethods->xFileSize(pFile, &nSize); + if( rc ) return rc; + if(nSize % nBuf){ + /* DB size is not an even multiple of the buffer size. Reduce + ** buffer size so that we do not unduly inflate the db size + ** with zero-padding when exporting. */ + if(0 == nSize % 4096) nBuf = 4096; + else if(0 == nSize % 2048) nBuf = 2048; + else if(0 == nSize % 1024) nBuf = 1024; + else nBuf = 512; + } + for( ; 0==rc && nPospMethods->xRead(pFile, buf, nBuf, nPos); + if( SQLITE_IOERR_SHORT_READ == rc ){ + rc = (nPos + nBuf) < nSize ? rc : 0/*assume EOF*/; + } + if( 0==rc ) rc = xCallback(buf, nBuf); + } + return rc; +} + +/* +** This function is NOT part of the sqlite3 public API. It is strictly +** for use by the sqlite project's own JS/WASM bindings. +** +** A proxy for sqlite3_serialize() which serializes the schema zSchema +** of pDb, placing the serialized output in pOut and nOut. nOut may be +** NULL. If zSchema is NULL then "main" is assumed. If pDb or pOut are +** NULL then SQLITE_MISUSE is returned. If allocation of the +** serialized copy fails, SQLITE_NOMEM is returned. On success, 0 is +** returned and `*pOut` will contain a pointer to the memory unless +** mFlags includes SQLITE_SERIALIZE_NOCOPY and the database has no +** contiguous memory representation, in which case `*pOut` will be +** NULL but 0 will be returned. +** +** If `*pOut` is not NULL, the caller is responsible for passing it to +** sqlite3_free() to free it. +*/ +SQLITE_WASM_EXPORT +int sqlite3__wasm_db_serialize( sqlite3 *pDb, const char *zSchema, + unsigned char **pOut, + sqlite3_int64 *nOut, unsigned int mFlags ){ + unsigned char * z; + if( !pDb || !pOut ) return SQLITE_MISUSE; + if( nOut ) *nOut = 0; + z = sqlite3_serialize(pDb, zSchema ? zSchema : "main", nOut, mFlags); + if( z || (SQLITE_SERIALIZE_NOCOPY & mFlags) ){ + *pOut = z; + return 0; + }else{ + return SQLITE_NOMEM; + } +} + +/* +** This function is NOT part of the sqlite3 public API. It is strictly +** for use by the sqlite project's own JS/WASM bindings. +** +** ACHTUNG: it was discovered on 2023-08-11 that, with SQLITE_DEBUG, +** this function's out-of-scope use of the sqlite3_vfs/file/io_methods +** APIs leads to triggering of assertions in the core library. Its use +** is now deprecated and VFS-specific APIs for importing files need to +** be found to replace it. sqlite3__wasm_posix_create_file() is +** suitable for the "unix" family of VFSes. +** +** Creates a new file using the I/O API of the given VFS, containing +** the given number of bytes of the given data. If the file exists, it +** is truncated to the given length and populated with the given +** data. +** +** This function exists so that we can implement the equivalent of +** Emscripten's FS.createDataFile() in a VFS-agnostic way. This +** functionality is intended for use in uploading database files. +** +** Not all VFSes support this functionality, e.g. the "kvvfs" does +** not. +** +** If pVfs is NULL, sqlite3_vfs_find(0) is used. +** +** If zFile is NULL, pVfs is NULL (and sqlite3_vfs_find(0) returns +** NULL), or nData is negative, SQLITE_MISUSE are returned. +** +** On success, it creates a new file with the given name, populated +** with the first nData bytes of pData. If pData is NULL, the file is +** created and/or truncated to nData bytes. +** +** Whether or not directory components of zFilename are created +** automatically or not is unspecified: that detail is left to the +** VFS. The "opfs" VFS, for example, creates them. +** +** If an error happens while populating or truncating the file, the +** target file will be deleted (if needed) if this function created +** it. If this function did not create it, it is not deleted but may +** be left in an undefined state. +** +** Returns 0 on success. On error, it returns a code described above +** or propagates a code from one of the I/O methods. +** +** Design note: nData is an integer, instead of int64, for WASM +** portability, so that the API can still work in builds where BigInt +** support is disabled or unavailable. +*/ +SQLITE_WASM_EXPORT +int sqlite3__wasm_vfs_create_file( sqlite3_vfs *pVfs, + const char *zFilename, + const unsigned char * pData, + int nData ){ + int rc; + sqlite3_file *pFile = 0; + sqlite3_io_methods const *pIo; + const int openFlags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE +#if 0 && defined(SQLITE_DEBUG) + | SQLITE_OPEN_MAIN_JOURNAL + /* ^^^^ This is for testing a horrible workaround to avoid + triggering a specific assert() in os_unix.c:unixOpen(). Please + do not enable this in real builds. */ +#endif + ; + int flagsOut = 0; + int fileExisted = 0; + int doUnlock = 0; + const unsigned char *pPos = pData; + const int blockSize = 512 + /* Because we are using pFile->pMethods->xWrite() for writing, and + ** it may have a buffer limit related to sqlite3's pager size, we + ** conservatively write in 512-byte blocks (smallest page + ** size). */; + //fprintf(stderr, "pVfs=%p, zFilename=%s, nData=%d\n", pVfs, zFilename, nData); + if( !pVfs ) pVfs = sqlite3_vfs_find(0); + if( !pVfs || !zFilename || nData<0 ) return SQLITE_MISUSE; + pVfs->xAccess(pVfs, zFilename, SQLITE_ACCESS_EXISTS, &fileExisted); + rc = sqlite3OsOpenMalloc(pVfs, zFilename, &pFile, openFlags, &flagsOut); +#if 0 +# define RC fprintf(stderr,"create_file(%s,%s) @%d rc=%d\n", \ + pVfs->zName, zFilename, __LINE__, rc); +#else +# define RC +#endif + RC; + if(rc) return rc; + pIo = pFile->pMethods; + if( pIo->xLock ) { + /* We need xLock() in order to accommodate the OPFS VFS, as it + ** obtains a writeable handle via the lock operation and releases + ** it in xUnlock(). If we don't do those here, we have to add code + ** to the VFS to account check whether it was locked before + ** xFileSize(), xTruncate(), and the like, and release the lock + ** only if it was unlocked when the op was started. */ + rc = pIo->xLock(pFile, SQLITE_LOCK_EXCLUSIVE); + RC; + doUnlock = 0==rc; + } + if( 0==rc ){ + rc = pIo->xTruncate(pFile, nData); + RC; + } + if( 0==rc && 0!=pData && nData>0 ){ + while( 0==rc && nData>0 ){ + const int n = nData>=blockSize ? blockSize : nData; + rc = pIo->xWrite(pFile, pPos, n, (sqlite3_int64)(pPos - pData)); + RC; + nData -= n; + pPos += n; + } + if( 0==rc && nData>0 ){ + assert( nDataxWrite(pFile, pPos, nData, + (sqlite3_int64)(pPos - pData)); + RC; + } + } + if( pIo->xUnlock && doUnlock!=0 ){ + pIo->xUnlock(pFile, SQLITE_LOCK_NONE); + } + pIo->xClose(pFile); + if( rc!=0 && 0==fileExisted ){ + pVfs->xDelete(pVfs, zFilename, 1); + } + RC; +#undef RC + return rc; +} + +/** +** This function is NOT part of the sqlite3 public API. It is strictly +** for use by the sqlite project's own JS/WASM bindings. +** +** Creates or overwrites a file using the POSIX file API, +** i.e. Emscripten's virtual filesystem. Creates or truncates +** zFilename, appends pData bytes to it, and returns 0 on success or +** SQLITE_IOERR on error. +*/ +SQLITE_WASM_EXPORT +int sqlite3__wasm_posix_create_file( const char *zFilename, + const unsigned char * pData, + int nData ){ + int rc; + FILE * pFile = 0; + int fileExisted = 0; + size_t nWrote = 1; + + if( !zFilename || nData<0 || (pData==0 && nData>0) ) return SQLITE_MISUSE; + pFile = fopen(zFilename, "w"); + if( 0==pFile ) return SQLITE_IOERR; + if( nData>0 ){ + nWrote = fwrite(pData, (size_t)nData, 1, pFile); + } + fclose(pFile); + return 1==nWrote ? 0 : SQLITE_IOERR; +} + +/* +** This function is NOT part of the sqlite3 public API. It is strictly +** for use by the sqlite project's own JS/WASM bindings. +** +** Allocates sqlite3KvvfsMethods.nKeySize bytes from +** sqlite3__wasm_pstack_alloc() and returns 0 if that allocation fails, +** else it passes that string to kvstorageMakeKey() and returns a +** NUL-terminated pointer to that string. It is up to the caller to +** use sqlite3__wasm_pstack_restore() to free the returned pointer. +*/ +SQLITE_WASM_EXPORT +char * sqlite3__wasm_kvvfsMakeKeyOnPstack(const char *zClass, + const char *zKeyIn){ + assert(sqlite3KvvfsMethods.nKeySize>24); + char *zKeyOut = + (char *)sqlite3__wasm_pstack_alloc(sqlite3KvvfsMethods.nKeySize); + if(zKeyOut){ + kvstorageMakeKey(zClass, zKeyIn, zKeyOut); + } + return zKeyOut; +} + +/* +** This function is NOT part of the sqlite3 public API. It is strictly +** for use by the sqlite project's own JS/WASM bindings. +** +** Returns the pointer to the singleton object which holds the kvvfs +** I/O methods and associated state. +*/ +SQLITE_WASM_EXPORT +sqlite3_kvvfs_methods * sqlite3__wasm_kvvfs_methods(void){ + return &sqlite3KvvfsMethods; +} + +#if SQLITE_WASM_HAS_VTAB +/* +** This function is NOT part of the sqlite3 public API. It is strictly +** for use by the sqlite project's own JS/WASM bindings. +** +** This is a proxy for the variadic sqlite3_vtab_config() which passes +** its argument on, or not, to sqlite3_vtab_config(), depending on the +** value of its 2nd argument. Returns the result of +** sqlite3_vtab_config(), or SQLITE_MISUSE if the 2nd arg is not a +** valid value. +*/ +SQLITE_WASM_EXPORT +int sqlite3__wasm_vtab_config(sqlite3 *pDb, int op, int arg){ + switch(op){ + case SQLITE_VTAB_DIRECTONLY: + case SQLITE_VTAB_INNOCUOUS: + return sqlite3_vtab_config(pDb, op); + case SQLITE_VTAB_CONSTRAINT_SUPPORT: + return sqlite3_vtab_config(pDb, op, arg); + default: + return SQLITE_MISUSE; + } +} +#endif /*SQLITE_WASM_HAS_VTAB*/ + +/* +** This function is NOT part of the sqlite3 public API. It is strictly +** for use by the sqlite project's own JS/WASM bindings. +** +** Wrapper for the variants of sqlite3_db_config() which take +** (int,int*) variadic args. +*/ +SQLITE_WASM_EXPORT +int sqlite3__wasm_db_config_ip(sqlite3 *pDb, int op, int arg1, int* pArg2){ + switch(op){ + case SQLITE_DBCONFIG_ENABLE_FKEY: + case SQLITE_DBCONFIG_ENABLE_TRIGGER: + case SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER: + case SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION: + case SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE: + case SQLITE_DBCONFIG_ENABLE_QPSG: + case SQLITE_DBCONFIG_TRIGGER_EQP: + case SQLITE_DBCONFIG_RESET_DATABASE: + case SQLITE_DBCONFIG_DEFENSIVE: + case SQLITE_DBCONFIG_WRITABLE_SCHEMA: + case SQLITE_DBCONFIG_LEGACY_ALTER_TABLE: + case SQLITE_DBCONFIG_DQS_DML: + case SQLITE_DBCONFIG_DQS_DDL: + case SQLITE_DBCONFIG_ENABLE_VIEW: + case SQLITE_DBCONFIG_LEGACY_FILE_FORMAT: + case SQLITE_DBCONFIG_TRUSTED_SCHEMA: + case SQLITE_DBCONFIG_STMT_SCANSTATUS: + case SQLITE_DBCONFIG_REVERSE_SCANORDER: + case SQLITE_DBCONFIG_ENABLE_ATTACH_CREATE: + case SQLITE_DBCONFIG_ENABLE_ATTACH_WRITE: + case SQLITE_DBCONFIG_ENABLE_COMMENTS: + return sqlite3_db_config(pDb, op, arg1, pArg2); + default: return SQLITE_MISUSE; + } +} + +/* +** This function is NOT part of the sqlite3 public API. It is strictly +** for use by the sqlite project's own JS/WASM bindings. +** +** Wrapper for the variants of sqlite3_db_config() which take +** (void*,int,int) variadic args. +*/ +SQLITE_WASM_EXPORT +int sqlite3__wasm_db_config_pii(sqlite3 *pDb, int op, void * pArg1, int arg2, int arg3){ + switch(op){ + case SQLITE_DBCONFIG_LOOKASIDE: + return sqlite3_db_config(pDb, op, pArg1, arg2, arg3); + default: return SQLITE_MISUSE; + } +} + +/* +** This function is NOT part of the sqlite3 public API. It is strictly +** for use by the sqlite project's own JS/WASM bindings. +** +** Wrapper for the variants of sqlite3_db_config() which take +** (const char *) variadic args. +*/ +SQLITE_WASM_EXPORT +int sqlite3__wasm_db_config_s(sqlite3 *pDb, int op, const char *zArg){ + switch(op){ + case SQLITE_DBCONFIG_MAINDBNAME: + return sqlite3_db_config(pDb, op, zArg); + default: return SQLITE_MISUSE; + } +} + + +/* +** This function is NOT part of the sqlite3 public API. It is strictly +** for use by the sqlite project's own JS/WASM bindings. +** +** Binding for combinations of sqlite3_config() arguments which take +** a single integer argument. +*/ +SQLITE_WASM_EXPORT +int sqlite3__wasm_config_i(int op, int arg){ + return sqlite3_config(op, arg); +} + +/* +** This function is NOT part of the sqlite3 public API. It is strictly +** for use by the sqlite project's own JS/WASM bindings. +** +** Binding for combinations of sqlite3_config() arguments which take +** two int arguments. +*/ +SQLITE_WASM_EXPORT +int sqlite3__wasm_config_ii(int op, int arg1, int arg2){ + return sqlite3_config(op, arg1, arg2); +} + +/* +** This function is NOT part of the sqlite3 public API. It is strictly +** for use by the sqlite project's own JS/WASM bindings. +** +** Binding for combinations of sqlite3_config() arguments which take +** a single i64 argument. +*/ +SQLITE_WASM_EXPORT +int sqlite3__wasm_config_j(int op, sqlite3_int64 arg){ + return sqlite3_config(op, arg); +} + +/* +** This function is NOT part of the sqlite3 public API. It is strictly +** for use by the sqlite project's own JS/WASM bindings. +** +** If z is not NULL, returns the result of passing z to +** sqlite3_mprintf()'s %Q modifier (if addQuotes is true) or %q (if +** addQuotes is 0). Returns NULL if z is NULL or on OOM. +*/ +SQLITE_WASM_EXPORT +char * sqlite3__wasm_qfmt_token(char *z, int addQuotes){ + char * rc = 0; + if( z ){ + rc = addQuotes + ? sqlite3_mprintf("%Q", z) + : sqlite3_mprintf("%q", z); + } + return rc; +} + +#if defined(__EMSCRIPTEN__) && defined(SQLITE_ENABLE_WASMFS) +#include +#include + +/* +** This function is NOT part of the sqlite3 public API. It is strictly +** for use by the sqlite project's own JS/WASM bindings, specifically +** only when building with Emscripten's WASMFS support. +** +** This function should only be called if the JS side detects the +** existence of the Origin-Private FileSystem (OPFS) APIs in the +** client. The first time it is called, this function instantiates a +** WASMFS backend impl for OPFS. On success, subsequent calls are +** no-ops. +** +** This function may be passed a "mount point" name, which must have a +** leading "/" and is currently restricted to a single path component, +** e.g. "/foo" is legal but "/foo/" and "/foo/bar" are not. If it is +** NULL or empty, it defaults to "/opfs". +** +** Returns 0 on success, SQLITE_NOMEM if instantiation of the backend +** object fails, SQLITE_IOERR if mkdir() of the zMountPoint dir in +** the virtual FS fails. In builds compiled without SQLITE_ENABLE_WASMFS +** defined, SQLITE_NOTFOUND is returned without side effects. +*/ +SQLITE_WASM_EXPORT +int sqlite3__wasm_init_wasmfs(const char *zMountPoint){ + static backend_t pOpfs = 0; + if( !zMountPoint || !*zMountPoint ) zMountPoint = "/opfs"; + if( !pOpfs ){ + pOpfs = wasmfs_create_opfs_backend(); + } + /** It's not enough to instantiate the backend. We have to create a + mountpoint in the VFS and attach the backend to it. */ + if( pOpfs && 0!=access(zMountPoint, F_OK) ){ + /* Note that this check and is not robust but it will + hypothetically suffice for the transient wasm-based virtual + filesystem we're currently running in. */ + const int rc = wasmfs_create_directory(zMountPoint, 0777, pOpfs); + /*emscripten_console_logf("OPFS mkdir(%s) rc=%d", zMountPoint, rc);*/ + if(rc) return SQLITE_IOERR; + } + return pOpfs ? 0 : SQLITE_NOMEM; +} +#else +SQLITE_WASM_EXPORT +int sqlite3__wasm_init_wasmfs(const char *zUnused){ + //emscripten_console_warn("WASMFS OPFS is not compiled in."); + (void)zUnused; + return SQLITE_NOTFOUND; +} +#endif /* __EMSCRIPTEN__ && SQLITE_ENABLE_WASMFS */ + +#if SQLITE_WASM_ENABLE_C_TESTS + +SQLITE_WASM_EXPORT +int sqlite3__wasm_test_intptr(int * p){ + return *p = *p * 2; +} + +SQLITE_WASM_EXPORT +void * sqlite3__wasm_test_voidptr(void * p){ + return p; +} + +SQLITE_WASM_EXPORT +int64_t sqlite3__wasm_test_int64_max(void){ + return (int64_t)0x7fffffffffffffff; +} + +SQLITE_WASM_EXPORT +int64_t sqlite3__wasm_test_int64_min(void){ + return ~sqlite3__wasm_test_int64_max(); +} + +SQLITE_WASM_EXPORT +int64_t sqlite3__wasm_test_int64_times2(int64_t x){ + return x * 2; +} + +SQLITE_WASM_EXPORT +void sqlite3__wasm_test_int64_minmax(int64_t * min, int64_t *max){ + *max = sqlite3__wasm_test_int64_max(); + *min = sqlite3__wasm_test_int64_min(); + /*printf("minmax: min=%lld, max=%lld\n", *min, *max);*/ +} + +SQLITE_WASM_EXPORT +int64_t sqlite3__wasm_test_int64ptr(int64_t * p){ + /*printf("sqlite3__wasm_test_int64ptr( @%lld = 0x%llx )\n", (int64_t)p, *p);*/ + return *p = *p * 2; +} + +SQLITE_WASM_EXPORT +void sqlite3__wasm_test_stack_overflow(int recurse){ + if(recurse) sqlite3__wasm_test_stack_overflow(recurse); +} + +/* For testing the 'string:dealloc' whwasmutil.xWrap() conversion. */ +SQLITE_WASM_EXPORT +char * sqlite3__wasm_test_str_hello(int fail){ + char * s = fail ? 0 : (char *)sqlite3_malloc(6); + if(s){ + memcpy(s, "hello", 5); + s[5] = 0; + } + return s; +} + +/* +** For testing using SQLTester scripts. +** +** Return non-zero if string z matches glob pattern zGlob and zero if the +** pattern does not match. +** +** To repeat: +** +** zero == no match +** non-zero == match +** +** Globbing rules: +** +** '*' Matches any sequence of zero or more characters. +** +** '?' Matches exactly one character. +** +** [...] Matches one character from the enclosed list of +** characters. +** +** [^...] Matches one character not in the enclosed list. +** +** '#' Matches any sequence of one or more digits with an +** optional + or - sign in front, or a hexadecimal +** literal of the form 0x... +*/ +static int sqlite3__wasm_SQLTester_strnotglob(const char *zGlob, const char *z){ + int c, c2; + int invert; + int seen; + typedef int (*recurse_f)(const char *,const char *); + static const recurse_f recurse = sqlite3__wasm_SQLTester_strnotglob; + + while( (c = (*(zGlob++)))!=0 ){ + if( c=='*' ){ + while( (c=(*(zGlob++))) == '*' || c=='?' ){ + if( c=='?' && (*(z++))==0 ) return 0; + } + if( c==0 ){ + return 1; + }else if( c=='[' ){ + while( *z && recurse(zGlob-1,z)==0 ){ + z++; + } + return (*z)!=0; + } + while( (c2 = (*(z++)))!=0 ){ + while( c2!=c ){ + c2 = *(z++); + if( c2==0 ) return 0; + } + if( recurse(zGlob,z) ) return 1; + } + return 0; + }else if( c=='?' ){ + if( (*(z++))==0 ) return 0; + }else if( c=='[' ){ + int prior_c = 0; + seen = 0; + invert = 0; + c = *(z++); + if( c==0 ) return 0; + c2 = *(zGlob++); + if( c2=='^' ){ + invert = 1; + c2 = *(zGlob++); + } + if( c2==']' ){ + if( c==']' ) seen = 1; + c2 = *(zGlob++); + } + while( c2 && c2!=']' ){ + if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){ + c2 = *(zGlob++); + if( c>=prior_c && c<=c2 ) seen = 1; + prior_c = 0; + }else{ + if( c==c2 ){ + seen = 1; + } + prior_c = c2; + } + c2 = *(zGlob++); + } + if( c2==0 || (seen ^ invert)==0 ) return 0; + }else if( c=='#' ){ + if( z[0]=='0' + && (z[1]=='x' || z[1]=='X') + && sqlite3Isxdigit(z[2]) + ){ + z += 3; + while( sqlite3Isxdigit(z[0]) ){ z++; } + }else{ + if( (z[0]=='-' || z[0]=='+') && sqlite3Isdigit(z[1]) ) z++; + if( !sqlite3Isdigit(z[0]) ) return 0; + z++; + while( sqlite3Isdigit(z[0]) ){ z++; } + } + }else{ + if( c!=(*(z++)) ) return 0; + } + } + return *z==0; +} + +SQLITE_WASM_EXPORT +int sqlite3__wasm_SQLTester_strglob(const char *zGlob, const char *z){ + return !sqlite3__wasm_SQLTester_strnotglob(zGlob, z); +} + +#endif /* SQLITE_WASM_ENABLE_C_TESTS */ + +#undef SQLITE_WASM_EXPORT +#undef SQLITE_WASM_HAS_VTAB +#undef SQLITE_WASM_BARE_BONES +#undef SQLITE_WASM_ENABLE_C_TESTS diff --git a/ext/wasm/api/sqlite3-worker1-promiser.c-pp.js b/ext/wasm/api/sqlite3-worker1-promiser.c-pp.js new file mode 100644 index 0000000000..1a09bf9a6a --- /dev/null +++ b/ext/wasm/api/sqlite3-worker1-promiser.c-pp.js @@ -0,0 +1,349 @@ +//#if not omit-oo1 +/* + 2022-08-24 + + The author disclaims copyright to this source code. In place of a + legal notice, here is a blessing: + + * May you do good and not evil. + * May you find forgiveness for yourself and forgive others. + * May you share freely, never taking more than you give. + + *********************************************************************** + + This file implements a Promise-based proxy for the sqlite3 Worker + API #1. It is intended to be included either from the main thread or + a Worker, but only if (A) the environment supports nested Workers + and (B) it's _not_ a Worker which loads the sqlite3 WASM/JS + module. This file's features will load that module and provide a + slightly simpler client-side interface than the slightly-lower-level + Worker API does. + + This script necessarily exposes one global symbol, but clients may + freely `delete` that symbol after calling it. +*/ +'use strict'; +/** + Configures an sqlite3 Worker API #1 Worker such that it can be + manipulated via a Promise-based interface and returns a factory + function which returns Promises for communicating with the worker. + This proxy has an _almost_ identical interface to the normal + worker API, with any exceptions documented below. + + It requires a configuration object with the following properties: + + - `worker` (required): a Worker instance which loads + `sqlite3-worker1.js` or a functional equivalent. Note that the + promiser factory replaces the worker.onmessage property. This + config option may alternately be a function, in which case this + function re-assigns this property with the result of calling that + function, enabling delayed instantiation of a Worker. + + - `onready` (optional, but...): this callback is called with no + arguments when the worker fires its initial + 'sqlite3-api'/'worker1-ready' message, which it does when + sqlite3.initWorker1API() completes its initialization. This is the + simplest way to tell the worker to kick off work at the earliest + opportunity, and the only way to know when the worker module has + completed loading. The irony of using a callback for this, instead + of returning a promise from sqlite3Worker1Promiser() is not lost on + the developers: see sqlite3Worker1Promiser.v2() which uses a + Promise instead. + + - `onunhandled` (optional): a callback which gets passed the + message event object for any worker.onmessage() events which + are not handled by this proxy. Ideally that "should" never + happen, as this proxy aims to handle all known message types. + + - `generateMessageId` (optional): a function which, when passed an + about-to-be-posted message object, generates a _unique_ message ID + for the message, which this API then assigns as the messageId + property of the message. It _must_ generate unique IDs on each call + so that dispatching can work. If not defined, a default generator + is used (which should be sufficient for most or all cases). + + - `debug` (optional): a console.debug()-style function for logging + information about messages. + + This function returns a stateful factory function with the + following interfaces: + + - Promise function(messageType, messageArgs) + - Promise function({message object}) + + The first form expects the "type" and "args" values for a Worker + message. The second expects an object in the form {type:..., + args:...} plus any other properties the client cares to set. This + function will always set the `messageId` property on the object, + even if it's already set, and will set the `dbId` property to the + current database ID if it is _not_ set in the message object. + + The function throws on error. + + The function installs a temporary message listener, posts a + message to the configured Worker, and handles the message's + response via the temporary message listener. The then() callback + of the returned Promise is passed the `message.data` property from + the resulting message, i.e. the payload from the worker, stripped + of the lower-level event state which the onmessage() handler + receives. + + Example usage: + + ``` + const config = {...}; + const sq3Promiser = sqlite3Worker1Promiser(config); + sq3Promiser('open', {filename:"/foo.db"}).then(function(msg){ + console.log("open response",msg); // => {type:'open', result: {filename:'/foo.db'}, ...} + }); + sq3Promiser({type:'close'}).then((msg)=>{ + console.log("close response",msg); // => {type:'close', result: {filename:'/foo.db'}, ...} + }); + ``` + + Differences from Worker API #1: + + - exec's {callback: STRING} option does not work via this + interface (it triggers an exception), but {callback: function} + does and works exactly like the STRING form does in the Worker: + the callback is called one time for each row of the result set, + passed the same worker message format as the worker API emits: + + {type:typeString, + row:VALUE, + rowNumber:1-based-#, + columnNames: array} + + Where `typeString` is an internally-synthesized message type string + used temporarily for worker message dispatching. It can be ignored + by all client code except that which tests this API. The `row` + property contains the row result in the form implied by the + `rowMode` option (defaulting to `'array'`). The `rowNumber` is a + 1-based integer value incremented by 1 on each call into the + callback. + + At the end of the result set, the same event is fired with + (row=undefined, rowNumber=null) to indicate that + the end of the result set has been reached. Note that the rows + arrive via worker-posted messages, with all the implications + of that. + + Notable shortcomings: + + - "v1" of this this API is not suitable for use as an ESM module + because ESM worker modules were not widely supported when it was + developed. For use as an ESM module, see the "v2" interface later + on in this file. +*/ +globalThis.sqlite3Worker1Promiser = function callee(config = callee.defaultConfig){ + // Inspired by: https://stackoverflow.com/a/52439530 + if(1===arguments.length && 'function'===typeof arguments[0]){ + const f = config; + config = Object.assign(Object.create(null), callee.defaultConfig); + config.onready = f; + }else{ + config = Object.assign(Object.create(null), callee.defaultConfig, config); + } + const handlerMap = Object.create(null); + const noop = function(){}; + const err = config.onerror + || noop /* config.onerror is intentionally undocumented + pending finding a less ambiguous name */; + const debug = config.debug || noop; + const idTypeMap = config.generateMessageId ? undefined : Object.create(null); + const genMsgId = config.generateMessageId || function(msg){ + return msg.type+'#'+(idTypeMap[msg.type] = (idTypeMap[msg.type]||0) + 1); + }; + const toss = (...args)=>{throw new Error(args.join(' '))}; + if(!config.worker) config.worker = callee.defaultConfig.worker; + if('function'===typeof config.worker) config.worker = config.worker(); + let dbId; + let promiserFunc; + config.worker.onmessage = function(ev){ + ev = ev.data; + debug('worker1.onmessage',ev); + let msgHandler = handlerMap[ev.messageId]; + if(!msgHandler){ + if(ev && 'sqlite3-api'===ev.type && 'worker1-ready'===ev.result) { + /*fired one time when the Worker1 API initializes*/ + if(config.onready) config.onready(promiserFunc); + return; + } + msgHandler = handlerMap[ev.type] /* check for exec per-row callback */; + if(msgHandler && msgHandler.onrow){ + msgHandler.onrow(ev); + return; + } + if(config.onunhandled) config.onunhandled(arguments[0]); + else err("sqlite3Worker1Promiser() unhandled worker message:",ev); + return; + } + delete handlerMap[ev.messageId]; + switch(ev.type){ + case 'error': + msgHandler.reject(ev); + return; + case 'open': + if(!dbId) dbId = ev.dbId; + break; + case 'close': + if(ev.dbId===dbId) dbId = undefined; + break; + default: + break; + } + try {msgHandler.resolve(ev)} + catch(e){msgHandler.reject(e)} + }/*worker.onmessage()*/; + return promiserFunc = function(/*(msgType, msgArgs) || (msgEnvelope)*/){ + let msg; + if(1===arguments.length){ + msg = arguments[0]; + }else if(2===arguments.length){ + msg = Object.create(null); + msg.type = arguments[0]; + msg.args = arguments[1]; + msg.dbId = msg.args.dbId; + }else{ + toss("Invalid arguments for sqlite3Worker1Promiser()-created factory."); + } + if(!msg.dbId && msg.type!=='open') msg.dbId = dbId; + msg.messageId = genMsgId(msg); + msg.departureTime = performance.now(); + const proxy = Object.create(null); + proxy.message = msg; + let rowCallbackId /* message handler ID for exec on-row callback proxy */; + if('exec'===msg.type && msg.args){ + if('function'===typeof msg.args.callback){ + rowCallbackId = msg.messageId+':row'; + proxy.onrow = msg.args.callback; + msg.args.callback = rowCallbackId; + handlerMap[rowCallbackId] = proxy; + }else if('string' === typeof msg.args.callback){ + toss("exec callback may not be a string when using the Promise interface."); + /** + Design note: the reason for this limitation is that this + API takes over worker.onmessage() and the client has no way + of adding their own message-type handlers to it. Per-row + callbacks are implemented as short-lived message.type + mappings for worker.onmessage(). + + We "could" work around this by providing a new + config.fallbackMessageHandler (or some such) which contains + a map of event type names to callbacks. Seems like overkill + for now, seeing as the client can pass callback functions + to this interface (whereas the string-form "callback" is + needed for the over-the-Worker interface). + */ + } + } + //debug("requestWork", msg); + let p = new Promise(function(resolve, reject){ + proxy.resolve = resolve; + proxy.reject = reject; + handlerMap[msg.messageId] = proxy; + debug("Posting",msg.type,"message to Worker dbId="+(dbId||'default')+':',msg); + config.worker.postMessage(msg); + }); + if(rowCallbackId) p = p.finally(()=>delete handlerMap[rowCallbackId]); + return p; + }; +}/*sqlite3Worker1Promiser()*/; + +globalThis.sqlite3Worker1Promiser.defaultConfig = { + worker: function(){ +//#if target:es6-bundler-friendly + return new Worker(new URL("sqlite3-worker1-bundler-friendly.mjs", import.meta.url),{ + type: 'module' + }); +//#elif target:es6-module + return new Worker(new URL("sqlite3-worker1.js", import.meta.url)); +//#else + let theJs = "sqlite3-worker1.js"; + if(this.currentScript){ + const src = this.currentScript.src.split('/'); + src.pop(); + theJs = src.join('/')+'/' + theJs; + //sqlite3.config.warn("promiser currentScript, theJs =",this.currentScript,theJs); + }else if(globalThis.location){ + //sqlite3.config.warn("promiser globalThis.location =",globalThis.location); + const urlParams = new URL(globalThis.location.href).searchParams; + if(urlParams.has('sqlite3.dir')){ + theJs = urlParams.get('sqlite3.dir') + '/' + theJs; + } + } + return new Worker(theJs + globalThis.location.search); +//#endif + } +//#if not target:es6-module + .bind({ + currentScript: globalThis?.document?.currentScript + }) +//#endif + , + onerror: (...args)=>console.error('worker1 promiser error',...args) +}/*defaultConfig*/; + +/** + sqlite3Worker1Promiser.v2(), added in 3.46, works identically to + sqlite3Worker1Promiser() except that it returns a Promise instead + of relying an an onready callback in the config object. The Promise + resolves to the same factory function which + sqlite3Worker1Promiser() returns. + + If config is-a function or is an object which contains an onready + function, that function is replaced by a proxy which will resolve + after calling the original function and will reject if that + function throws. +*/ +globalThis.sqlite3Worker1Promiser.v2 = function callee(config = callee.defaultConfig){ + let oldFunc; + if( 'function' == typeof config ){ + oldFunc = config; + config = {}; + }else if('function'===typeof config?.onready){ + oldFunc = config.onready; + delete config.onready; + } + const promiseProxy = Object.create(null); + config = Object.assign((config || Object.create(null)),{ + onready: async function(func){ + try { + if( oldFunc ) await oldFunc(func); + promiseProxy.resolve(func); + } + catch(e){promiseProxy.reject(e)} + } + }); + const p = new Promise(function(resolve,reject){ + promiseProxy.resolve = resolve; + promiseProxy.reject = reject; + }); + try{ + this.original(config); + }catch(e){ + promiseProxy.reject(e); + } + return p; +}.bind({ + /* We do this because clients are recommended to delete + globalThis.sqlite3Worker1Promiser. */ + original: sqlite3Worker1Promiser +}); + +globalThis.sqlite3Worker1Promiser.v2.defaultConfig = + globalThis.sqlite3Worker1Promiser.defaultConfig; + +//#if target:es6-module +/** + When built as a module, we export sqlite3Worker1Promiser.v2() + instead of sqlite3Worker1Promise() because (A) its interface is more + conventional for ESM usage and (B) the ESM export option for this + API did not exist until v2 was created, so there's no backwards + incompatibility. +*/ +export default sqlite3Worker1Promiser.v2; +//#endif /* target:es6-module */ +//#else +/* Built with the omit-oo1 flag. */ +//#endif if not omit-oo1 diff --git a/ext/wasm/api/sqlite3-worker1.c-pp.js b/ext/wasm/api/sqlite3-worker1.c-pp.js new file mode 100644 index 0000000000..036c4c6ea3 --- /dev/null +++ b/ext/wasm/api/sqlite3-worker1.c-pp.js @@ -0,0 +1,56 @@ +//#if not omit-oo1 +/* + 2022-05-23 + + The author disclaims copyright to this source code. In place of a + legal notice, here is a blessing: + + * May you do good and not evil. + * May you find forgiveness for yourself and forgive others. + * May you share freely, never taking more than you give. + + *********************************************************************** + + This is a JS Worker file for the main sqlite3 api. It loads + sqlite3.js, initializes the module, and postMessage()'s a message + after the module is initialized: + + {type: 'sqlite3-api', result: 'worker1-ready'} + + This seemingly superfluous level of indirection is necessary when + loading sqlite3.js via a Worker. Instantiating a worker with new + Worker("sqlite.js") will not (cannot) call sqlite3InitModule() to + initialize the module due to a timing/order-of-operations conflict + (and that symbol is not exported in a way that a Worker loading it + that way can see it). Thus JS code wanting to load the sqlite3 + Worker-specific API needs to pass _this_ file (or equivalent) to the + Worker constructor and then listen for an event in the form shown + above in order to know when the module has completed initialization. + + This file accepts a URL arguments to adjust how it loads sqlite3.js: + + - `sqlite3.dir`, if set, treats the given directory name as the + directory from which `sqlite3.js` will be loaded. +*/ +//#if target:es6-bundler-friendly +import {default as sqlite3InitModule} from './sqlite3-bundler-friendly.mjs'; +//#elif target:es6-module + return new Worker(new URL("sqlite3.js", import.meta.url)); +//#else +"use strict"; +{ + const urlParams = globalThis.location + ? new URL(globalThis.location.href).searchParams + : new URLSearchParams(); + let theJs = 'sqlite3.js'; + if(urlParams.has('sqlite3.dir')){ + theJs = urlParams.get('sqlite3.dir') + '/' + theJs; + } + //console.warn("worker1 theJs =",theJs); + importScripts(theJs); +} +//#endif +sqlite3InitModule().then(sqlite3 => sqlite3.initWorker1API()); +//#else +/* Built with the omit-oo1 flag. */ +//#endif if not omit-oo1 diff --git a/ext/wasm/c-pp-lite.c b/ext/wasm/c-pp-lite.c new file mode 100644 index 0000000000..2120c457dd --- /dev/null +++ b/ext/wasm/c-pp-lite.c @@ -0,0 +1,2767 @@ +/* +** 2022-11-12: +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** * May you do good and not evil. +** * May you find forgiveness for yourself and forgive others. +** * May you share freely, never taking more than you give. +** +************************************************************************ +** +** The C-minus Preprocessor: a truly minimal C-like preprocessor. +** Why? Because C preprocessors _can_ process non-C code but generally make +** quite a mess of it. The purpose of this application is an extremely +** minimal preprocessor with only the most basic functionality of a C +** preprocessor, namely. +** +** The supported preprocessor directives are documented in the +** README.md hosted with this file. +** +** Any mention of "#" in the docs, e.g. "#if", is symbolic. The +** directive delimiter is configurable and defaults to "##". Define +** CMPP_DEFAULT_DELIM to a string when compiling to define the default +** at build-time. +** +** This preprocessor has only minimal support for replacement of tokens +** which live in the "content" blocks of inputs (that is, the pieces +** which are not prepocessor lines). +** +** See this file's README.md for details. +** +** Design note: this code makes use of sqlite3. Though not _strictly_ +** needed in order to implement it, this tool was specifically created +** for use with the sqlite3 project's own JavaScript code, so there's +** no reason not to make use of it to do some of the heavy lifting. It +** does not require any cutting-edge sqlite3 features and should be +** usable with any version which supports `WITHOUT ROWID`. +** +** Author(s): +** +** - Stephan Beal +** +** Canonical homes: +** +** - https://fossil.wanderinghorse.net/r/c-pp +** - https://sqlite.org/src/file/ext/wasm/c-pp.c +** +** With the former hosting this app's SCM and the latter being the +** single known deployment of c-pp.c, where much of its development +** happens. +*/ + +#include +#include +#include +#include +#include +#include +#include + +#include "sqlite3.h" + +#if defined(_WIN32) || defined(WIN32) +# include +# include +# ifndef access +# define access(f,m) _access((f),(m)) +# endif +#else +# include +#endif + +#ifndef CMPP_DEFAULT_DELIM +#define CMPP_DEFAULT_DELIM "##" +#endif + +#ifndef CMPP_ATSIGN +#define CMPP_ATSIGN (unsigned char)'@' +#endif + +#if 1 +# define CMPP_NORETURN __attribute__((noreturn)) +#else +# define CMPP_NORETURN +#endif + +/* Fatally exits the app with the given printf-style message. */ +static CMPP_NORETURN void fatalv__base(char const *zFile, int line, + char const *zFmt, va_list); +static CMPP_NORETURN void fatal__base(char const *zFile, int line, + char const *zFmt, ...); +#define fatalv(...) fatalv__base(__FILE__,__LINE__,__VA_ARGS__) +#define fatal(...) fatal__base(__FILE__,__LINE__,__VA_ARGS__) + +/** Proxy for free(), for symmetry with cmpp_realloc(). */ +static void cmpp_free(void *p); +/** A realloc() proxy which dies fatally on allocation error. */ +static void * cmpp_realloc(void * p, unsigned n); +#if 0 +/** A malloc() proxy which dies fatally on allocation error. */ +static void * cmpp_malloc(unsigned n); +#endif + +static void check__oom2(void const *p, char const *zFile, int line){ + if(!p) fatal("Alloc failed at %s:%d", zFile, line); +} +#define check__oom(P) check__oom2((P), __FILE__, __LINE__) + +/* +** If p is stdin or stderr then this is a no-op, else it is a +** proxy for fclose(). This is a no-op if p is NULL. +*/ +static void FILE_close(FILE *p); +/* +** Works like fopen() but accepts the special name "-" to mean either +** stdin (if zMode indicates a real-only mode) or stdout. Fails +** fatally on error. +*/ +static FILE * FILE_open(char const *zName, const char * zMode); +/* +** Reads the entire contents of the given file, allocating it in a +** buffer which gets assigned to `*pOut`. `*nOut` gets assigned the +** length of the output buffer. Fails fatally on error. +*/ +static void FILE_slurp(FILE *pFile, unsigned char **pOut, + unsigned * nOut); + +/* +** Intended to be passed an sqlite3 result code. If it's a non-0 value +** other than SQLITE_ROW or SQLITE_DONE then it emits a fatal error +** message which contains both the given string and the +** sqlite3_errmsg() from the application's database instance. +*/ +static void db_affirm_rc(int rc, const char * zMsg); + +/* +** Proxy for sqlite3_str_finish() which fails fatally if that +** routine returns NULL. +*/ +static char * db_str_finish(sqlite3_str *s, int * n); +/* +** Proxy for sqlite3_str_new() which fails fatally if that +** routine returns NULL. +*/ +static sqlite3_str * db_str_new(void); + +/* +** Proxy for sqlite3_step() which fails fatally if the result +** is anything other than SQLITE_ROW or SQLITE_DONE. +*/ +static int db_step(sqlite3_stmt *pStmt); +/* +** Proxy for sqlite3_bind_int() which fails fatally on error. +*/ +static void db_bind_int(sqlite3_stmt *pStmt, int col, int val); +/* +** Proxy for sqlite3_bind_null() which fails fatally on error. +*/ +static void db_bind_null(sqlite3_stmt *pStmt, int col); +/* +** Proxy for sqlite3_bind_text() which fails fatally on error. +*/ +static void db_bind_text(sqlite3_stmt *pStmt, int col, const char * zStr); +/* +** Proxy for sqlite3_bind_text() which fails fatally on error. +*/ +static void db_bind_textn(sqlite3_stmt *pStmt, int col, const char * zStr, int len); +#if 0 +/* +** Proxy for sqlite3_bind_text() which fails fatally on error. It uses +** sqlite3_str_vappendf() so supports all of its formatting options. +*/ +static void db_bind_textv(sqlite3_stmt *pStmt, int col, const char * zFmt, ...); +#endif +/* +** Proxy for sqlite3_free(), to be passed any memory which is allocated +** by sqlite3_malloc(). +*/ +static void db_free(void *m); + +/* +** Returns true if the first nKey bytes of zKey are a legal string. If +** it returns false and zErrPos is not null, *zErrPos is set to the +** position of the illegal character. If nKey is negative, strlen() is +** used to calculate it. +*/ +static int cmpp_is_legal_key(char const *zKey, int nKey, char const **zErrPos); + +/* +** Fails fatally if !cmpp_is_legal_key(zKey). +*/ +static void cmpp_affirm_legal_key(char const *zKey, int nKey); + +/* +** Adds the given `#define` macro name to the list of macros, ignoring +** any duplicates. Fails fatally on error. +** +** If zVal is NULL then zKey may contain an '=', from which the value +** will be extracted. If zVal is not NULL then zKey may _not_ contain +** an '='. +*/ +static void db_define_add(const char * zKey, char const *zVal); + +/* +** Returns true if the given key is already in the `#define` list, +** else false. Fails fatally on db error. +** +** nName is the length of the key part of zName (which might have +** a following =y part. If it's negative, strlen() is used to +** calculate it. +*/ +static int db_define_has(const char * zName, int nName); + +/* +** Returns true if the given key is already in the `#define` list, and +** it has a truthy value (is not empty and not equal to '0'), else +** false. Fails fatally on db error. +** +** nName is the length of zName, or <0 to use strlen() to figure +** it out. +*/ +static int db_define_get_bool(const char * zName, int nName); + +/* +** Searches for a define where (k GLOB zName). If one is found, a copy +** of it is assigned to *zVal (the caller must eventually db_free() +** it)), *nVal (if nVal is not NULL) is assigned its strlen, and +** returns non-0. If no match is found, 0 is returned and neither +** *zVal nor *nVal are modified. If more than one result matches, a +** fatal error is triggered. +** +** It is legal for *zVal to be NULL (and *nVal to be 0) if it returns +** non-0. That just means that the key was defined with no value part. +*/ +static int db_define_get(const char * zName, int nName, char **zVal, unsigned int *nVal); + +/* +** Removes the given `#define` macro name from the list of +** macros. Fails fatally on error. +*/ +static void db_define_rm(const char * zKey); +/* +** Adds the given filename to the list of being-`#include`d files, +** using the given source file name and line number of error reporting +** purposes. If recursion is later detected. +*/ +static void db_including_add(const char * zKey, const char * zSrc, int srcLine); +/* +** Adds the given dir to the list of includes. They are checked in the +** order they are added. +*/ +static void db_include_dir_add(const char * zKey); +/* +** Returns a resolved path of PREFIX+'/'+zKey, where PREFIX is one of +** the `#include` dirs (db_include_dir_add()). If no file match is +** found, NULL is returned. Memory must eventually be passed to +** db_free() to free it. +*/ +static char * db_include_search(const char * zKey); +/* +** Removes the given key from the `#include` list. +*/ +static void db_include_rm(const char * zKey); +/* +** A proxy for sqlite3_prepare() which fails fatally on error. +*/ +static void db_prepare(sqlite3_stmt **pStmt, const char * zSql, ...); + +/* +** Opens the given file and processes its contents as c-pp, sending +** all output to the global c-pp output channel. Fails fatally on +** error. +*/ +static void cmpp_process_file(const char * zName); + +/* +** Operator policy for cmpp_kvp_parse(). +*/ +enum cmpp_key_op_e { + /* Fail if the key contains an operator. */ + cmpp_key_op_none, + /* Accept only '='. */ + cmpp_key_op_eq1 +}; +typedef enum cmpp_key_op_e cmpp_key_op_e; + +/* +** Operators and operator policies for use with X=Y-format keys. +*/ +#define cmpp_kvp_op_map(E) \ + E(none,"") \ + E(eq1,"=") \ + E(eq2,"==") \ + E(lt,"<") \ + E(le,"<=") \ + E(gt,">") \ + E(ge,">=") + +enum cmpp_kvp_op_e { +#define E(N,S) cmpp_kvp_op_ ## N, + cmpp_kvp_op_map(E) +#undef E +}; +typedef enum cmpp_kvp_op_e cmpp_kvp_op_e; + +/* +** A snippet from a string. +*/ +struct cmpp_snippet { + char const *z; + unsigned int n; +}; +typedef struct cmpp_snippet cmpp_snippet; +#define cmpp_snippet_empty_m {0,0} + +/* +** Result type for cmpp_kvp_parse(). +*/ +struct cmpp_kvp { + cmpp_snippet k; + cmpp_snippet v; + cmpp_kvp_op_e op; +}; + +typedef struct cmpp_kvp cmpp_kvp; +#define cmpp_kvp_empty_m \ + {cmpp_snippet_empty_m,cmpp_snippet_empty_m,cmpp_kvp_op_none} +static const cmpp_kvp cmpp_kvp_empty = cmpp_kvp_empty_m; + +/* +** Parses X or X=Y into p. Fails fatally on error. +** +** If nKey is negative then strlen() is used to calculate it. +** +** The third argument specifies whether/how to permit/treat the '=' +** part of X=Y. +*/ +static void cmpp_kvp_parse(cmpp_kvp * p, + char const *zKey, int nKey, + cmpp_kvp_op_e opPolicy); + +/* +** Wrapper around a FILE handle. +*/ +typedef struct FileWrapper FileWrapper; +struct FileWrapper { + /* File's name. */ + char const *zName; + /* FILE handle. */ + FILE * pFile; + /* Where FileWrapper_slurp() stores the file's contents. */ + unsigned char * zContent; + /* Size of this->zContent, as set by FileWrapper_slurp(). */ + unsigned nContent; + /* See Global::pFiles. */ + FileWrapper * pTail; +}; +#define FileWrapper_empty_m {0,0,0,0,0} +static const FileWrapper FileWrapper_empty = FileWrapper_empty_m; + +/* +** Proxy for FILE_close() and frees all memory owned by p. A no-op if +** p is already closed. +*/ +static void FileWrapper_close(FileWrapper * p); +/* Proxy for FILE_open(). Closes p first if it's currently opened. */ +static void FileWrapper_open(FileWrapper * p, const char * zName, const char *zMode); +/* Proxy for FILE_slurp(). */ +static void FileWrapper_slurp(FileWrapper * p); +/* +** If p->zContent ends in \n or \r\n, that part is replaced with 0 and +** p->nContent is adjusted. Returns true if it chomps, else false. +*/ +int FileWrapper_chomp(FileWrapper * p); + +/* +** Outputs a printf()-formatted message to stderr. +*/ +static void g_stderr(char const *zFmt, ...); +/* +** Outputs a printf()-formatted message to stderr. +*/ +static void g_stderrv(char const *zFmt, va_list); +#define g_debug(lvl,pfexpr) \ + if(lvl<=g.flags.doDebug) g_stderr("%s @ %s():%d: ",g.zArgv0,__func__,__LINE__); \ + if(lvl<=g.flags.doDebug) g_stderr pfexpr + +#define g_warn(zFmt,...) g_stderr("%s:%d %s() " zFmt "\n", __FILE__, __LINE__, __func__, __VA_ARGS__) +#define g_warn0(zMsg) g_stderr("%s:%d %s() %s\n", __FILE__, __LINE__, __func__, zMsg) + +void cmpp_free(void *p){ + sqlite3_free(p); +} + +void * cmpp_realloc(void * p, unsigned n){ + void * const rc = sqlite3_realloc(p, n); + if(!rc) fatal("realloc(P,%u) failed", n); + return rc; +} + +#if 0 +void * cmpp_malloc(unsigned n){ + void * const rc = sqlite3_alloc(n); + if(!rc) fatal("malloc(%u) failed", n); + return rc; +} +#endif + +FILE * FILE_open(char const *zName, const char * zMode){ + FILE * p; + if('-'==zName[0] && 0==zName[1]){ + p = strstr(zMode,"w") ? stdout : stdin; + }else{ + p = fopen(zName, zMode); + if(!p) fatal("Cannot open file [%s] with mode [%s]", zName, zMode); + } + return p; +} + +void FILE_close(FILE *p){ + if(p && p!=stdout && p!=stderr){ + fclose(p); + } +} + +void FILE_slurp(FILE *pFile, unsigned char **pOut, + unsigned * nOut){ + unsigned char zBuf[1024 * 8]; + unsigned char * pDest = 0; + unsigned nAlloc = 0; + unsigned nOff = 0; + /* Note that this needs to be able to work on non-seekable streams, + ** thus we read in chunks instead of doing a single alloc and + ** filling it in one go. */ + while( !feof(pFile) ){ + size_t const n = fread(zBuf, 1, sizeof(zBuf), pFile); + if(n>0){ + if(nAlloc < nOff + n + 1){ + nAlloc = nOff + n + 1; + pDest = cmpp_realloc(pDest, nAlloc); + } + memcpy(pDest + nOff, zBuf, n); + nOff += n; + } + } + if(pDest) pDest[nOff] = 0; + *pOut = pDest; + *nOut = nOff; +} + +void FileWrapper_close(FileWrapper * p){ + if(p->pFile) FILE_close(p->pFile); + if(p->zContent) cmpp_free(p->zContent); + *p = FileWrapper_empty; +} + +void FileWrapper_open(FileWrapper * p, const char * zName, + const char * zMode){ + FileWrapper_close(p); + p->pFile = FILE_open(zName, zMode); + p->zName = zName; +} + +void FileWrapper_slurp(FileWrapper * p){ + assert(!p->zContent); + assert(p->pFile); + FILE_slurp(p->pFile, &p->zContent, &p->nContent); +} + +int FileWrapper_chomp(FileWrapper * p){ + if( p->nContent && '\n'==p->zContent[p->nContent-1] ){ + p->zContent[--p->nContent] = 0; + if( p->nContent && '\r'==p->zContent[p->nContent-1] ){ + p->zContent[--p->nContent] = 0; + } + return 1; + } + return 0; +} + +enum CmppParseState { +TS_Start = 1, +TS_If, +TS_IfPassed, +TS_Else, +TS_Error +}; +typedef enum CmppParseState CmppParseState; +enum CmppTokenType { + +#define CmppToken_map(E) \ + E(Invalid,0) \ + E(Assert,"assert") \ + E(AtPolicy,"@policy") \ + E(Comment,"//") \ + E(Define,"define") \ + E(Elif,"elif") \ + E(Else,"else") \ + E(Endif,"endif") \ + E(Error,"error") \ + E(If,"if") \ + E(Include,"include") \ + E(Line,0) \ + E(Opaque,0) \ + E(Pragma,"pragma") \ + E(Savepoint,"savepoint") \ + E(Stderr,"stderr") \ + E(Undef,"undef") + +#define E(N,TOK) TT_ ## N, + CmppToken_map(E) +#undef E +}; +typedef enum CmppTokenType CmppTokenType; + +/* +** Map of directive (formerly keyword) names and their token types. +*/ +static const struct { +#define E(N,TOK) struct cmpp_snippet N; + CmppToken_map(E) +#undef E +} DStrings = { +#define E(N,TOK) .N = {TOK,sizeof(TOK)-1}, + CmppToken_map(E) +#undef E +}; + +//static +char const * TT_cstr(int tt){ + switch(tt){ +#define E(N,TOK) case TT_ ## N: return DStrings.N.z; + CmppToken_map(E) +#undef E + } + return NULL; +} + +struct CmppToken { + CmppTokenType ttype; + /* Line number of this token in the source file. */ + unsigned lineNo; + /* Start of the token. */ + unsigned char const * zBegin; + /* One-past-the-end byte of the token. */ + unsigned char const * zEnd; +}; +typedef struct CmppToken CmppToken; +#define CmppToken_empty_m {TT_Invalid,0,0,0} +static const CmppToken CmppToken_empty = CmppToken_empty_m; + +/* +** CmppLevel represents one "level" of tokenization, starting at the +** top of the main input, incrementing once for each level of `#if`, +** and decrementing for each `#endif`. +** pushes a level. +*/ +typedef struct CmppLevel CmppLevel; +struct CmppLevel { + unsigned short flags; + /* + ** Used for controlling which parts of an if/elif/...endif chain + ** should get output. + */ + unsigned short skipLevel; + /* The token which started this level (an 'if' or 'include'). */ + CmppToken token; + CmppParseState pstate; +}; +#define CmppLevel_empty_m {0U,0U,CmppToken_empty_m,TS_Start} +static const CmppLevel CmppLevel_empty = CmppLevel_empty_m; +enum CmppLevel_Flags { +/* Max depth of nested `#if` constructs in a single tokenizer. */ +CmppLevel_Max = 10, +/* Max number of keyword arguments. */ +CmppArgs_Max = 15, +/* Directive line buffer size */ +CmppArgs_BufSize = 1024, +/* Flag indicating that output for a CmpLevel should be elided. */ +CmppLevel_F_ELIDE = 0x01, +/* +** Mask of CmppLevel::flags which are inherited when CmppLevel_push() +** is used. +*/ +CmppLevel_F_INHERIT_MASK = CmppLevel_F_ELIDE +}; + +typedef struct CmppTokenizer CmppTokenizer; +typedef struct CmppKeyword CmppKeyword; +typedef void (*cmpp_keyword_f)(CmppKeyword const * pKw, CmppTokenizer * t); +struct CmppKeyword { + const char *zName; + unsigned nName; + int bTokenize; + CmppTokenType ttype; + cmpp_keyword_f xCall; +}; + +static CmppKeyword const * CmppKeyword_search(const char *zName); +static void cmpp_process_keyword(CmppTokenizer * const t); + +/* +** Tokenizer for c-pp input files. +*/ +struct CmppTokenizer { + const char * zName; /* Input (file) name for error reporting */ + unsigned const char * zBegin; /* start of input */ + unsigned const char * zEnd; /* one-after-the-end of input */ + unsigned const char * zPos; /* current position */ + unsigned int lineNo; /* line # of current pos */ + unsigned nSavepoint; + CmppParseState pstate; + CmppToken token; /* current token result */ + struct { + unsigned ndx; + CmppLevel stack[CmppLevel_Max]; + } level; + /* Args for use in cmpp_keyword_f() impls. */ + struct { + CmppKeyword const * pKw; + int argc; + const unsigned char * argv[CmppArgs_Max]; + unsigned char lineBuf[CmppArgs_BufSize]; + } args; +}; +#define CT_level(t) (t)->level.stack[(t)->level.ndx] +#define CT_pstate(t) CT_level(t).pstate +#define CT_skipLevel(t) CT_level(t).skipLevel +#define CLvl_skip(lvl) ((lvl)->skipLevel || ((lvl)->flags & CmppLevel_F_ELIDE)) +#define CT_skip(t) CLvl_skip(&CT_level(t)) +#define CmppTokenizer_empty_m { \ + .zName=0, .zBegin=0, .zEnd=0, \ + .zPos=0, \ + .lineNo=1U, \ + .pstate = TS_Start, \ + .token = CmppToken_empty_m, \ + .level = {0U,{CmppLevel_empty_m}}, \ + .args = {0,0,{0},{0}} \ + } +static const CmppTokenizer CmppTokenizer_empty = CmppTokenizer_empty_m; + +static void CmppTokenizer_cleanup(CmppTokenizer * const t); + +static void cmpp_t_out(CmppTokenizer * t, void const *z, unsigned int n); +/*static void cmpp_t_outf(CmppTokenizer * t, char const *zFmt, ...);*/ + +/* +** Pushes a new level into the given tokenizer. Fails fatally if +** it's too deep. +*/ +static void CmppLevel_push(CmppTokenizer * const t); +/* +** Pops a level from the tokenizer. Fails fatally if the top +** level is popped. +*/ +static void CmppLevel_pop(CmppTokenizer * const t); +/* +** Returns the current level object. +*/ +static CmppLevel * CmppLevel_get(CmppTokenizer * const t); + +/* +** Policies for how to handle undefined @tokens@ when performing +** content filtering. +*/ +enum AtPolicy { + AT_invalid = -1, + /** Turn off @foo@ parsing. */ + AT_OFF = 0, + /** Retain undefined @foo@ - emit it as-is. */ + AT_RETAIN, + /** Elide undefined @foo@. */ + AT_ELIDE, + /** Error for undefined @foo@. */ + AT_ERROR, + AT_DEFAULT = AT_ERROR +}; +typedef enum AtPolicy AtPolicy; + +static AtPolicy AtPolicy_fromStr(char const *z, int bEnforce){ + if( 0==strcmp(z, "retain") ) return AT_RETAIN; + if( 0==strcmp(z, "elide") ) return AT_ELIDE; + if( 0==strcmp(z, "error") ) return AT_ERROR; + if( 0==strcmp(z, "off") ) return AT_OFF; + if( bEnforce ){ + fatal("Invalid @ policy value: %s. " + "Try one of retain|elide|error|off.", z); + } + return AT_invalid; +} + +/* +** Global app state singleton. +*/ +static struct Global { + /* main()'s argv[0]. */ + const char * zArgv0; + /* App's db instance. */ + sqlite3 * db; + /* Current tokenizer (for error reporting purposes). */ + CmppTokenizer const * tok; + /* + ** We use a linked-list of these to keep track of our opened + ** files so that we can clean then up via atexit() in the case of + ** fatal error (to please valgrind). + */ + FileWrapper * pFiles; + /* Output channel. */ + FileWrapper out; + struct { + /* + ** Bytes of the keyword delimiter/prefix. Owned + ** elsewhere. + */ + const char * z; + /* Byte length of this->zDelim. */ + unsigned short n; + /* + ** The @token@ delimiter. + ** + ** Potential TODO is replace this with a pair of opener/closer + ** strings, e.g. "{{" and "}}". + */ + const unsigned char chAt; + } delim; + struct { +#define CMPP_SAVEPOINT_NAME "_cmpp_" +#define GStmt_map(E) \ + E(defIns,"INSERT OR REPLACE INTO def(k,v) VALUES(?,?)") \ + E(defDel,"DELETE FROM def WHERE k GLOB ?") \ + E(defHas,"SELECT 1 FROM def WHERE k GLOB ?") \ + E(defGet,"SELECT k,v FROM def WHERE k GLOB ?") \ + E(defGetBool, \ + "SELECT 1 FROM def WHERE k = ?1" \ + " AND v IS NOT NULL" \ + " AND '0'!=v AND ''!=v") \ + E(defSelAll,"SELECT k,v FROM def ORDER BY k") \ + E(inclIns,"INSERT OR FAIL INTO incl(file,srcFile," \ + "srcLine) VALUES(?,?,?)") \ + E(inclDel,"DELETE FROM incl WHERE file=?") \ + E(inclHas,"SELECT 1 FROM incl WHERE file=?") \ + E(inclPathAdd,"INSERT OR FAIL INTO " \ + "inclpath(seq,dir) VALUES(?,?)") \ + E(inclSearch, \ + "SELECT ?1 fn WHERE fileExists(fn) " \ + "UNION ALL SELECT * FROM (" \ + "SELECT replace(dir||'/'||?1, '//','/') AS fn " \ + "FROM inclpath WHERE fileExists(fn) ORDER BY seq"\ + ")") \ + E(spBegin,"SAVEPOINT " CMPP_SAVEPOINT_NAME) \ + E(spRollback,"ROLLBACK TO SAVEPOINT " \ + CMPP_SAVEPOINT_NAME) \ + E(spRelease,"RELEASE SAVEPOINT " CMPP_SAVEPOINT_NAME) + +#define E(N,S) sqlite3_stmt * N; + GStmt_map(E) +#undef E + } stmt; + struct { + FILE * pFile; + int expandSql; + } sqlTrace; + struct { + AtPolicy atPolicy; + /* If true, enables certain debugging output. */ + char doDebug; + /* If true, chomp() files read via -Fx=file. */ + char chompF; + } flags; +} g = { + .zArgv0 = "?", + .db = 0, + .tok = 0, + .pFiles = 0, + .out = FileWrapper_empty_m, + .delim = { + .z = CMPP_DEFAULT_DELIM, + .n = (unsigned short) sizeof(CMPP_DEFAULT_DELIM)-1, + .chAt = '@' + }, + .stmt = { + .defIns = 0, + .defDel = 0, + .defHas = 0, + .defGet = 0, + .defGetBool = 0, + .inclIns = 0, + .inclDel = 0, + .inclHas = 0, + .inclPathAdd = 0, + .inclSearch = 0 + }, + .sqlTrace = { + .pFile = 0, + .expandSql = 0 + }, + .flags = { + .atPolicy = AT_OFF, + .doDebug = 0, + .chompF = 0 + } +}; + +/** Distinct IDs for each g.stmt member. */ +enum GStmt_e { + GStmt_none = 0, +#define E(N,S) GStmt_ ## N, + GStmt_map(E) +#undef E +}; + +/* +** Returns the g.stmt.X corresponding to `which`, initializing it if +** needed. It does not return NULL - it fails fatally on error. +*/ +static sqlite3_stmt * g_stmt(enum GStmt_e which){ + sqlite3_stmt ** q = 0; + char const * zSql = 0; + switch(which){ + case GStmt_none: + fatal("GStmt_none is not a valid statement handle"); + return NULL; +#define E(N,S) case GStmt_ ## N: zSql = S; q = &g.stmt.N; break; + GStmt_map(E) +#undef E + } + assert( q ); + assert( zSql && *zSql ); + if( !*q ){ + db_prepare(q, "%s", zSql); + assert( *q ); + } + return *q; +} +static void g_stmt_reset(sqlite3_stmt * const q){ + sqlite3_clear_bindings(q); + sqlite3_reset(q); +} + +#if 0 +/* +** Outputs a printf()-formatted message to c-pp's global output +** channel. +*/ +static void g_outf(char const *zFmt, ...); +void g_outf(char const *zFmt, ...){ + va_list va; + va_start(va, zFmt); + vfprintf(g.out.pFile, zFmt, va); + va_end(va); +} +#endif + +/* Outputs n bytes from z to c-pp's global output channel. */ +static void g_out(void const *z, unsigned int n); +void g_out(void const *z, unsigned int n){ + if(g.out.pFile && 1!=fwrite(z, n, 1, g.out.pFile)){ + int const err = errno; + fatal("fwrite() output failed with errno #%d", err); + } +} + +void g_stderrv(char const *zFmt, va_list va){ + if( g.out.pFile==stdout ){ + fflush(g.out.pFile); + } + vfprintf(stderr, zFmt, va); +} + +void g_stderr(char const *zFmt, ...){ + va_list va; + va_start(va, zFmt); + g_stderrv(zFmt, va); + va_end(va); +} + +/* +** Emits n bytes of z if CT_skip(t) is false. +*/ +void cmpp_t_out(CmppTokenizer * t, void const *z, unsigned int n){ + g_debug(3,("CT_skipLevel() ?= %d\n",CT_skipLevel(t))); + g_debug(3,("CT_skip() ?= %d\n",CT_skip(t))); + if(!CT_skip(t)) g_out(z, n); +} + +void CmppLevel_push(CmppTokenizer * const t){ + CmppLevel * pPrev; + CmppLevel * p; + if(t->level.ndx+1 == (unsigned)CmppLevel_Max){ + fatal("%sif nesting level is too deep. Max=%d\n", + g.delim.z, CmppLevel_Max); + } + pPrev = &CT_level(t); + g_debug(3,("push from tokenizer level=%u flags=%04x\n", + t->level.ndx, pPrev->flags)); + p = &t->level.stack[++t->level.ndx]; + *p = CmppLevel_empty; + p->token = t->token; + p->flags = (CmppLevel_F_INHERIT_MASK & pPrev->flags); + if(CLvl_skip(pPrev)) p->flags |= CmppLevel_F_ELIDE; + g_debug(3,("push to tokenizer level=%u flags=%04x\n", + t->level.ndx, p->flags)); +} + +void CmppLevel_pop(CmppTokenizer * const t){ + if(!t->level.ndx){ + fatal("Internal error: CmppLevel_pop() at the top of the stack"); + } + g_debug(3,("pop from tokenizer level=%u, flags=%04x skipLevel?=%d\n", + t->level.ndx, + t->level.stack[t->level.ndx].flags, CT_skipLevel(t))); + g_debug(3,("CT_skipLevel() ?= %d\n",CT_skipLevel(t))); + g_debug(3,("CT_skip() ?= %d\n",CT_skip(t))); + t->level.stack[t->level.ndx--] = CmppLevel_empty; + g_debug(3,("pop to tokenizer level=%u, flags=%04x\n", t->level.ndx, + t->level.stack[t->level.ndx].flags)); + g_debug(3,("CT_skipLevel() ?= %d\n",CT_skipLevel(t))); + g_debug(3,("CT_skip() ?= %d\n",CT_skip(t))); +} + +CmppLevel * CmppLevel_get(CmppTokenizer * const t){ + return &t->level.stack[t->level.ndx]; +} + + +void db_affirm_rc(int rc, const char * zMsg){ + switch(rc){ + case 0: + case SQLITE_DONE: + case SQLITE_ROW: + break; + default: + assert( g.db ); + fatal("Db error #%d %s: %s", rc, zMsg, + sqlite3_errmsg(g.db)); + } +} + +int db_step(sqlite3_stmt *pStmt){ + int const rc = sqlite3_step(pStmt); + switch( rc ){ + case SQLITE_ROW: + case SQLITE_DONE: + break; + default: + db_affirm_rc(rc, "from db_step()"); + } + return rc; +} + +static sqlite3_str * db_str_new(void){ + sqlite3_str * rc = sqlite3_str_new(g.db); + if(!rc) fatal("Alloc failed for sqlite3_str_new()"); + return rc; +} + +static char * db_str_finish(sqlite3_str *s, int * n){ + int const rc = sqlite3_str_errcode(s); + if(rc) fatal("Error #%d from sqlite3_str_errcode()", rc); + if(n) *n = sqlite3_str_length(s); + char * z = sqlite3_str_finish(s); + if(!z) fatal("Alloc failed for sqlite3_str_new()"); + return z; +} + +void db_prepare(sqlite3_stmt **pStmt, const char * zSql, ...){ + int rc; + sqlite3_str * str = db_str_new(); + char * z = 0; + int n = 0; + va_list va; + if(!str) fatal("sqlite3_str_new() failed"); + va_start(va, zSql); + sqlite3_str_vappendf(str, zSql, va); + va_end(va); + rc = sqlite3_str_errcode(str); + if(rc) fatal("sqlite3_str_errcode() = %d", rc); + z = db_str_finish(str, &n); + rc = sqlite3_prepare_v2(g.db, z, n, pStmt, 0); + if(rc) fatal("Error #%d (%s) preparing: %s", + rc, sqlite3_errmsg(g.db), z); + sqlite3_free(z); +} + +void db_bind_int(sqlite3_stmt *pStmt, int col, int val){ + db_affirm_rc(sqlite3_bind_int(pStmt, col, val), + "from db_bind_int()"); +} + +void db_bind_null(sqlite3_stmt *pStmt, int col){ + db_affirm_rc(sqlite3_bind_null(pStmt, col), + "from db_bind_null()"); +} + +void db_bind_textn(sqlite3_stmt *pStmt, int col, + const char * zStr, int n){ + db_affirm_rc( + (zStr && n) + ? sqlite3_bind_text(pStmt, col, zStr, n, SQLITE_TRANSIENT) + : sqlite3_bind_null(pStmt, col), + "from db_bind_textn()" + ); +} + +void db_bind_text(sqlite3_stmt *pStmt, int col, + const char * zStr){ + db_bind_textn(pStmt, col, zStr, -1); +} + +#if 0 +void db_bind_textv(sqlite3_stmt *pStmt, int col, + const char * zFmt, ...){ + int rc; + sqlite3_str * str = db_str_new(); + int n = 0; + char * z; + va_list va; + va_start(va,zFmt); + sqlite3_str_vappendf(str, zFmt, va); + va_end(va); + z = db_str_finish(str, &n); + rc = sqlite3_bind_text(pStmt, col, z, n, sqlite3_free); + db_affirm_rc(rc,"from db_bind_textv()"); +} +#endif + +void db_free(void *m){ + sqlite3_free(m); +} + +void db_define_add(const char * zKey, char const *zVal){ + cmpp_kvp kvp = cmpp_kvp_empty; + cmpp_kvp_parse(&kvp, zKey, -1, + zVal + ? cmpp_key_op_none + : cmpp_key_op_eq1 + ); + if( kvp.v.z ){ + if( zVal ){ + assert(!"cannot happen - cmpp_key_op_none will prevent it"); + fatal("Cannot assign two values to [%.*s] [%.*s] [%s]", + kvp.k.n, kvp.k.z, kvp.v.n, kvp.v.z, zVal); + } + }else{ + kvp.v.z = zVal; + kvp.v.n = zVal ? (int)strlen(zVal) : 0; + } + sqlite3_stmt * const q = g_stmt(GStmt_defIns); + //g_stderr("zKey=%s\nzVal=%s\nzEq=%s\n", zKey, zVal, zEq); + db_bind_textn(q, 1, kvp.k.z, kvp.k.n); + if( kvp.v.z ){ + if( kvp.v.n ){ + db_bind_textn(q, 2, kvp.v.z, (int)kvp.v.n); + }else{ + db_bind_null(q, 2); + } + }else{ + db_bind_int(q, 2, 1); + } + db_step(q); + g_debug(2,("define: %s%s%s\n", + zKey, + zVal ? " with value " : "", + zVal ? zVal : "")); + sqlite3_reset(q); +} + +static void db_define_add_file(const char * zKey){ + cmpp_kvp kvp = cmpp_kvp_empty; + cmpp_kvp_parse(&kvp, zKey, -1, cmpp_kvp_op_eq1); + if( !kvp.v.z || !kvp.v.n ){ + fatal("Invalid filename: %s", zKey); + } + sqlite3_stmt * q = 0; + FileWrapper fw = FileWrapper_empty; + FileWrapper_open(&fw, kvp.v.z, "r"); + FileWrapper_slurp(&fw); + q = g_stmt(GStmt_defIns); + //g_stderr("zKey=%s\nzVal=%s\nzEq=%s\n", zKey, zVal, zEq); + db_bind_textn(q, 1, kvp.k.z, (int)kvp.k.n); + if( g.flags.chompF ){ + FileWrapper_chomp(&fw); + } + if( fw.nContent ){ + db_affirm_rc( + sqlite3_bind_text(q, 2, + (char const *)fw.zContent, + (int)fw.nContent, sqlite3_free), + "binding file content"); + fw.zContent = 0 /* transfered ownership */; + fw.nContent = 0; + }else{ + db_affirm_rc( sqlite3_bind_null(q, 2), + "binding empty file content"); + } + FileWrapper_close(&fw); + db_step(q); + g_stmt_reset(q); + g_debug(2,("define: %s%s%s\n", + kvp.k.z, + kvp.v.z ? " with value " : "", + kvp.v.z ? kvp.v.z : "")); +} + +#define ustr_c(X) ((unsigned char const *)X) + +static inline unsigned int cmpp_strlen(char const *z, int n){ + return n<0 ? (int)strlen(z) : (unsigned)n; +} + + +int db_define_has(const char * zName, int nName){ + int rc; + sqlite3_stmt * const q = g_stmt(GStmt_defHas); + nName = cmpp_strlen(zName, nName); + db_bind_textn(q, 1, zName, nName); + rc = db_step(q); + if(SQLITE_ROW == rc){ + rc = 1; + }else{ + assert(SQLITE_DONE==rc); + rc = 0; + } + g_debug(1,("defined [%s] ?= %d\n",zName, rc)); + g_stmt_reset(q); + return rc; +} + +int db_define_get_bool(const char * zName, int nName){ + sqlite3_stmt * const q = g_stmt(GStmt_defGetBool); + int rc = 0; + nName = cmpp_strlen(zName, nName); + db_bind_textn(q, 1, zName, nName); + rc = db_step(q); + if(SQLITE_ROW == rc){ + if( SQLITE_ROW==sqlite3_step(q) ){ + fatal("Key is ambiguous: %s", zName); + } + rc = 1; + }else{ + assert(SQLITE_DONE==rc); + rc = 0; + } + g_stmt_reset(q); + return rc; +} + +int db_define_get(const char * zName, int nName, + char **zVal, unsigned int *nVal){ + sqlite3_stmt * q = g_stmt(GStmt_defGet); + nName = cmpp_strlen(zName, nName); + db_bind_textn(q, 1, zName, nName); + int n = 0; + int rc = db_step(q); + if(SQLITE_ROW == rc){ + const unsigned char * z = sqlite3_column_text(q, 1); + n = sqlite3_column_bytes(q,1); + if( nVal ) *nVal = (unsigned)n; + *zVal = sqlite3_mprintf("%.*s", n, z); + if( n && z ) check__oom(*zVal); + if( SQLITE_ROW==sqlite3_step(q) ){ + db_free(*zVal); + *zVal = 0; + fatal("Key is ambiguous: %.*s\n", + nName, zName); + } + rc = 1; + }else{ + assert(SQLITE_DONE==rc); + rc = 0; + } + g_debug(1,("define [%.*s] ?= %d %.*s\n", + nName, zName, rc, + *zVal ? n : 0, + *zVal ? *zVal : "")); + g_stmt_reset(q); + return rc; +} + +void db_define_rm(const char * zKey){ + int rc; + int n = 0; + sqlite3_stmt * const q = g_stmt(GStmt_defDel); + db_bind_text(q, 1, zKey); + rc = db_step(q); + if(SQLITE_DONE != rc){ + db_affirm_rc(rc, "Stepping DELETE on def"); + } + g_debug(2,("undefine: %.*s\n",n, zKey)); + g_stmt_reset(q); +} + +void db_including_add(const char * zKey, const char * zSrc, int srcLine){ + int rc; + sqlite3_stmt * const q = g_stmt(GStmt_inclIns); + db_bind_text(q, 1, zKey); + db_bind_text(q, 2, zSrc); + db_bind_int(q, 3, srcLine); + rc = db_step(q); + if(SQLITE_DONE != rc){ + db_affirm_rc(rc, "Stepping INSERT on incl"); + } + g_debug(2,("is-including-file add [%s] from [%s]:%d\n", zKey, zSrc, srcLine)); + g_stmt_reset(q); +} + +void db_include_rm(const char * zKey){ + int rc; + sqlite3_stmt * const q = g_stmt(GStmt_inclDel); + db_bind_text(q, 1, zKey); + rc = db_step(q); + if(SQLITE_DONE != rc){ + db_affirm_rc(rc, "Stepping DELETE on incl"); + } + g_debug(2,("inclpath rm [%s]\n", zKey)); + g_stmt_reset(q); +} + +char * db_include_search(const char * zKey){ + char * zName = 0; + sqlite3_stmt * const q = g_stmt(GStmt_inclSearch); + db_bind_text(q, 1, zKey); + if(SQLITE_ROW==db_step(q)){ + const unsigned char * z = sqlite3_column_text(q, 0); + zName = z ? sqlite3_mprintf("%s", z) : 0; + if(!zName) fatal("Alloc failed"); + } + g_stmt_reset(q); + return zName; +} + +static int db_including_has(const char * zName){ + int rc; + sqlite3_stmt * const q = g_stmt(GStmt_inclHas); + db_bind_text(q, 1, zName); + rc = db_step(q); + if(SQLITE_ROW == rc){ + rc = 1; + }else{ + assert(SQLITE_DONE==rc); + rc = 0; + } + g_debug(2,("inclpath has [%s] = %d\n",zName, rc)); + g_stmt_reset(q); + return rc; +} + +#if 0 +/* +** Fails fatally if the `#include` list contains the given key. +*/ +static void db_including_check(const char * zKey); +void db_including_check(const char * zName){ + if(db_including_has(zName)){ + fatal("Recursive include detected: %s\n", zName); + } +} +#endif + +void db_include_dir_add(const char * zDir){ + static int seq = 0; + int rc; + sqlite3_stmt * const q = g_stmt(GStmt_inclPathAdd); + db_bind_int(q, 1, ++seq); + db_bind_text(q, 2, zDir); + rc = db_step(q); + if(SQLITE_DONE != rc){ + db_affirm_rc(rc, "Stepping INSERT on inclpath"); + } + g_debug(2,("inclpath add #%d: %s\n",seq, zDir)); + g_stmt_reset(q); +} + +void g_FileWrapper_link(FileWrapper *fp){ + assert(!fp->pTail); + fp->pTail = g.pFiles; + g.pFiles = fp; +} + +void g_FileWrapper_close(FileWrapper *fp){ + assert(fp); + assert(fp->pTail || g.pFiles==fp); + g.pFiles = fp->pTail; + fp->pTail = 0; + FileWrapper_close(fp); +} + +static void g_cleanup(int bCloseFileChain){ + if( g.db ){ +#define E(N,S) sqlite3_finalize(g.stmt.N); g.stmt.N = 0; + GStmt_map(E) +#undef E + } + if( bCloseFileChain ){ + FileWrapper * fpNext = 0; + for( FileWrapper * fp=g.pFiles; fp; fp=fpNext ){ + fpNext = fp->pTail; + fp->pTail = 0; + FileWrapper_close(fp); + } + } + FileWrapper_close(&g.out); + if(g.db){ + sqlite3_close(g.db); + g.db = 0; + } +} + +static void cmpp_atexit(void){ + g_cleanup(1); +} + +int cmpp_is_legal_key(char const *zKey, int nKey, char const **zAt){ + char const * z = zKey; + nKey = cmpp_strlen(zKey, nKey); + if( !nKey ){ + if( zAt ) *zAt = z; + return 0; + } + char const * const zEnd = z ? z + nKey : NULL; + for( ; z < zEnd; ++z ){ + switch( (0x80 & *z) ? 0 : *z ){ + case 0: + case '_': + continue; + case '-': + case '.': + case '/': + case ':': + case '=': + case '0': case '1': case '2': case '3': case '4': + case '5': case '6': case '7': case '8': case '9': + if( z==zKey ) break; + continue; + default: + if( isalpha((int)*z) ) continue; + } + if( zAt ) *zAt = z; + return 0; + } + assert( z==zEnd ); + return 1; +} + +void cmpp_affirm_legal_key(char const *zKey, int nKey){ + char const *zAt = 0; + nKey = cmpp_strlen(zKey, nKey); + if( !cmpp_is_legal_key(zKey, nKey, &zAt) ){ + assert( zAt ); + fatal("Illegal character 0x%02x in key [%.*s]\n", + (int)*zAt, nKey, zKey); + } +} + +/* +** sqlite3 UDF which returns true if its argument refers to an +** accessible file, else false. +*/ +static void udf_file_exists( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + const char *zName; + (void)(argc); /* Unused parameter */ + zName = (const char*)sqlite3_value_text(argv[0]); + if( zName==0 ) return; + sqlite3_result_int(context, 0==access(zName, 0)); +} + +/** + ** This sqlite3_trace_v2() callback outputs tracing info using + ** g.sqlTrace.pFile. +*/ +static int cmpp__db_sq3TraceV2(unsigned t,void*c,void*p,void*x){ + static unsigned int counter = 0; + switch(t){ + case SQLITE_TRACE_STMT:{ + FILE * const fp = g.sqlTrace.pFile; + if( fp ){ + char const * const zSql = (char const *)x; + char * const zExp = g.sqlTrace.expandSql + ? sqlite3_expanded_sql((sqlite3_stmt*)p) + : 0; + fprintf(fp, "SQL TRACE #%u: %s\n", + ++counter, zExp ? zExp : zSql); + sqlite3_free(zExp); + } + break; + } + } + return 0; +} + +/* Initialize g.db, failing fatally on error. */ +static void cmpp_initdb(void){ + int rc; + char * zErr = 0; + const char * zSchema = + "CREATE TABLE def(" + /* ^^^ defines */ + "k TEXT PRIMARY KEY NOT NULL," + "v TEXT DEFAULT NULL" + ") WITHOUT ROWID;" + "CREATE TABLE incl(" + /* ^^^ files currently being included */ + "file TEXT PRIMARY KEY NOT NULL," + "srcFile TEXT DEFAULT NULL," + "srcLine INTEGER DEFAULT 0" + ") WITHOUT ROWID;" + "CREATE TABLE inclpath(" + /* ^^^ include path */ + "seq INTEGER UNIQUE ON CONFLICT IGNORE, " + "dir TEXT PRIMARY KEY NOT NULL ON CONFLICT IGNORE" + ");" + "BEGIN;" + ; + assert(0==g.db); + if(g.db) return; + rc = sqlite3_open_v2(":memory:", &g.db, SQLITE_OPEN_READWRITE, 0); + if(rc) fatal("Error opening :memory: db."); + sqlite3_trace_v2(g.db, SQLITE_TRACE_STMT, cmpp__db_sq3TraceV2, 0); + rc = sqlite3_exec(g.db, zSchema, 0, 0, &zErr); + if(rc) fatal("Error initializing database: %s", zErr); + rc = sqlite3_create_function(g.db, "fileExists", 1, + SQLITE_UTF8|SQLITE_DIRECTONLY, 0, + udf_file_exists, 0, 0); + db_affirm_rc(rc, "UDF registration failed."); +} + +/* +** For position zPos, which must be in the half-open range +** [zBegin,zEnd), returns g.delim.n if it is at the start of a line and +** starts with g.delim.z, else returns 0. +*/ +//static +unsigned short cmpp_is_delim(unsigned char const *zBegin, + unsigned char const *zEnd, + unsigned char const *zPos){ + assert(zEnd>zBegin); + assert(zPos=zBegin); + if(zPos>zBegin && + ('\n'!=*(zPos - 1) + || ((unsigned)(zEnd - zPos) <= g.delim.n))){ + return 0; + }else if(0==memcmp(zPos, g.delim.z, g.delim.n)){ + return g.delim.n; + }else{ + return 0; + } +} + +static void cmpp_t_out_expand(CmppTokenizer * const t, + unsigned char const * zFrom, + unsigned int n); + +static inline int cmpp__isspace(int ch){ + return ' '==ch || '\t'==ch; +} + +static inline unsigned cmpp__strlenu(unsigned char const *z, int n){ + return n<0 ? (unsigned)strlen((char const *)z) : (unsigned)n; +} + +static inline void cmpp__skip_space_c( unsigned char const **p, + unsigned char const *zEnd ){ + unsigned char const * z = *p; + while( zzPos,t->zEnd) for a derective delimiter. Emits any + non-delimiter output found along the way. + + This updtes t->zPos and t->lineNo as it goes. + + If a delimiter is found, it updates t->token and returns 0. + On no match returns 0. +*/ +static +int CmppTokenizer__delim_search(CmppTokenizer * const t){ + if(!t->zPos) t->zPos = t->zBegin; + if( t->zPos>=t->zEnd ){ + return 0; + } + assert( (t->zPos==t->zBegin || t->zPos[-1]=='\n') + && "Else we've mismanaged something."); + char const * const zD = g.delim.z; + unsigned short const nD = g.delim.n; + unsigned char const * const zEnd = t->zEnd; + unsigned char const * zLeft = t->zPos; + unsigned char const * z = zLeft; + + assert( 0==*zEnd && "Else we'll misinteract with strcspn()" ); + if( *zEnd ){ + fatal("Input must be NUL-terminated."); + return 0; + } +#define tflush \ + if(z>zEnd) z=zEnd; \ + if( z>zLeft ) { \ + cmpp_t_out_expand(t, zLeft, (unsigned)(z-zLeft)); \ + } zLeft = z + while(z < zEnd){ + size_t nNlTotal = 0; + unsigned char const * zNl; + size_t nNl2 = strcspn((char const *)z, "\n"); + zNl = (z + nNl2 >= zEnd ? zEnd : z + nNl2); + if( nNl2 >= CmppArgs_BufSize /* too long */ + //|| '\n'!=(char)*zNl /* end of input */ + /* ^^^ we have to accept a missing trailing EOL for the + sake of -e scripts. */ + ){ + /* we'd like to error out here, but only if we know we're + reading reading a directive line. */ + ++t->lineNo; + z = zNl + 1; + tflush; + continue; + } + nNlTotal += nNl2; + assert( '\n'==*zNl || !*zNl ); + assert( '\n'==*zNl || zNl==zEnd ); + //g_stderr("input: zNl=%d z=<<<%.*s>>>", (int)*zNl, (zNl-z), z); + unsigned char const * const zBOL = z; + cmpp__skip_space_c(&z, zNl); + if( z+nD < zNl && 0==memcmp(z, zD, nD) ){ + /* Found a directive delimiter. */ + if( zBOL!=z ){ + /* Do not emit space from the same line which preceeds a + delimiter */ + zLeft = z; + } + while( zNl>z && zNllineNo; + ++zNl; + nNl2 = strcspn((char const *)zNl, "\n"); + if( !nNl2 ) break; + nNlTotal += nNl2; + zNl += nNl2; + } + assert( zNl<=zEnd && "Else our input was not NUL-terminated"); + if( nNlTotal >= CmppArgs_BufSize ){ + fatal("Directive line is too long (%u)", + (unsigned)(zNl-z)); + break; + } + tflush; + t->token.zBegin = z + nD; + t->token.zEnd = zNl; + cmpp__skip_space_c(&t->token.zBegin, t->token.zEnd); + t->token.ttype = TT_Line; + t->token.lineNo = t->lineNo++; + t->zPos = t->token.zEnd + 1; + if( 0 ){ + g_stderr("token=<<%.*s>>", (t->token.zEnd - t->token.zBegin), + t->token.zBegin); + } + return 1; + } + z = zNl+1; + ++t->lineNo; + tflush; + //g_stderr("line #%d no match\n",(int)t->lineNo); + } + tflush; + t->zPos = z; + return 0; +#undef tflush +} + +void cmpp_kvp_parse(cmpp_kvp * p, char const *zKey, int nKey, + cmpp_kvp_op_e opPolicy){ + char chEq = 0; + char opLen = 0; + *p = cmpp_kvp_empty; + p->k.z = zKey; + p->k.n = cmpp_strlen(zKey, nKey); + switch( opPolicy ){ + case cmpp_kvp_op_none: break; + case cmpp_kvp_op_eq1: + chEq = '='; + opLen = 1; + break; + default: + assert(!"don't use these yet"); + /* todo: ==, !=, <=, <, >, >= */ + chEq = '='; + opLen = 1; + break; + } + assert( chEq ); + p->op = cmpp_kvp_op_none; + const char * const zEnd = p->k.z + p->k.n; + for(const char * zPos = p->k.z ; *zPos && zPosop = cmpp_kvp_op_eq1; + p->k.n = (unsigned)(zPos - zKey); + zPos += opLen; + assert( zPos <= zEnd ); + p->v.z = zPos; + p->v.n = (unsigned)(zEnd - zPos); + break; + } + } + cmpp_affirm_legal_key(p->k.z, p->k.n); +} + +static void cmpp_t_out_expand(CmppTokenizer * const t, + unsigned char const * zFrom, + unsigned int n){ + unsigned char const *zLeft = zFrom; + unsigned char const * const zEnd = zFrom + n; + unsigned char const *z = AT_OFF==g.flags.atPolicy ? zEnd : zLeft; + unsigned char const chEol = (unsigned char)'\n'; + int state = 0 /* 0==looking for opening @ + ** 1==looking for closing @ */; + if( 0 ){ + g_warn("zLeft=%d %c", (int)*zLeft, *zLeft); + } +#define tflush \ + if(z>zEnd) z=zEnd; \ + if(zLefttoken; + + assert(t->zBegin); + assert(t->zEnd > t->zBegin); + if(!t->zPos) t->zPos = t->zBegin; + t->args.pKw = 0; + t->args.argc = 0; + *tok = CmppToken_empty; + if( !CmppTokenizer__delim_search(t) ){ + return 0; + } + /* Split t->token into arguments for the line's keyword */ + int i, argc = 0, prevChar = 0; + const unsigned tokLen = (unsigned)(tok->zEnd - tok->zBegin); + unsigned char * zKwd; + unsigned char * zEsc; + unsigned char * zz; + + assert(TT_Line==tok->ttype); + g_debug(2,("token @ line %u len=%u [[[%.*s]]]\n", + tok->lineNo, tokLen, tokLen, tok->zBegin)); + zKwd = &t->args.lineBuf[0]; + memcpy(zKwd, tok->zBegin, tokLen); + memset(zKwd + tokLen, 0, sizeof(t->args.lineBuf) - tokLen); + for( zEsc = 0, zz = zKwd; *zz; ++zz ){ + /* Convert backslash-escaped newlines to whitespace */ + switch((int)*zz){ + case (int)'\\': + if(zEsc) zEsc = 0; + else zEsc = zz; + break; + case (int)'\n': + assert(zEsc && "Should not have an unescaped newline?"); + if(zEsc==zz-1){ + *zEsc = (unsigned char)' '; + /* FIXME?: memmove() lnBuf content one byte to the left here + ** to collapse backslash and newline into a single + ** byte. Also consider collapsing all leading space on the + ** next line. (Much later: or just collapse the output as we go, + ** effectively shrinking the line.) */ + } + zEsc = 0; + *zz = (unsigned char)' '; + break; + default: + zEsc = 0; + break; + } + } + t->args.argv[argc++] = zKwd; + for( zz = zKwd; *zz; ++zz ){ + if(isspace(*zz)){ + *zz = 0; + break; + } + } + t->args.pKw = CmppKeyword_search((char const *)zKwd); + if(!t->args.pKw){ + fatal("Unknown keyword '%s' at line %u\n", (char const *)zKwd, + tok->lineNo); + } + for( ++zz ; *zz && isspace(*zz); ++zz ){} + if(t->args.pKw->bTokenize){ + for( ; *zz; prevChar = *zz, ++zz ){ + /* Split string into word-shaped tokens. + ** TODO ?= quoted strings, for the sake of the + ** #error keyword. */ + if(isspace(*zz)){ + assert(zz!=zKwd && "Leading space was stripped earlier."); + *zz = 0; + }else{ + if(argc == (int)CmppArgs_Max){ + fatal("Too many arguments @ line %u: %.*s", + tok->lineNo, tokLen, tok->zBegin); + }else if(zz>zKwd && !prevChar){ + t->args.argv[argc++] = zz; + } + } + } + }else{ + /* Treat rest of line as one token */ + if(*zz) t->args.argv[argc++] = zz; + } + tok->ttype = t->args.pKw->ttype; + if(g.flags.doDebug>1){ + for(i = 0; i < argc; ++i){ + g_debug(0,("line %u arg #%d=%s\n", + tok->lineNo, i, + (char const *)t->args.argv[i])); + } + } + t->args.argc = argc; + return 1; +} + +/* Internal error reporting helper for cmpp_keyword_f() impls. */ +static CMPP_NORETURN void cmpp_kwd__err_(char const *zFile, int line, + CmppKeyword const * pKw, + CmppTokenizer const *t, + char const *zFmt, ...){ + va_list va; + g_stderr("%s @ %s line %u:", + pKw->zName, t->zName, t->token.lineNo); + va_start(va, zFmt); + g.tok = 0 /* stop fatalv__base() from duplicating the file info */; + fatalv__base(zFile, line, zFmt, va); + /* not reached */ + va_end(va); +} +#define cmpp_kwd__err(...) cmpp_kwd__err_(__FILE__,__LINE__, __VA_ARGS__) +#define cmpp_t__err(T,...) cmpp_kwd__err_(__FILE__,__LINE__, (T)->args.pKw, (T), __VA_ARGS__) + +/* No-op cmpp_keyword_f() impl. */ +static void cmpp_kwd_noop(CmppKeyword const * pKw, CmppTokenizer *t){ + (void)pKw; + (void)t; +} + +/* #error impl. */ +static void cmpp_kwd_error(CmppKeyword const * pKw, CmppTokenizer *t){ + if(CT_skip(t)) return; + else{ + assert(t->args.argc < 3); + const char *zBegin = t->args.argc>1 + ? (const char *)t->args.argv[1] : 0; + cmpp_t__err(t, "%s", zBegin ? zBegin : "(no additional info)"); + } +} + +/* Impl. for #define, #undef */ +static void cmpp_kwd_define(CmppKeyword const * pKw, CmppTokenizer *t){ + if(CT_skip(t)) return; + if(t->args.argc<2){ + cmpp_kwd__err(pKw, t, "Expecting one or more arguments"); + }else{ + int i = 1; + for( ; i < t->args.argc; ++i){ + char const * const zArg = (char const *)t->args.argv[i]; + cmpp_affirm_legal_key(zArg, -1); + if( TT_Define==pKw->ttype ){ + db_define_add( zArg, NULL ); + }else{ + db_define_rm( zArg ); + } + } + } +} + +static int cmpp_val_matches(char const *zGlob, char const *zRhs){ + return 0==sqlite3_strglob(zGlob, zRhs); +} + +typedef int (*cmpp_vcmp_f)(char const *zLhs, char const *zRhs); + +/* +** Accepts a key in the form X or X=Y. In the former case, it uses +** db_define_get_bool(kvp->k) to determine its truthiness, else it +** compares the kvp->v part to kvp->k's defined value to determine +** truthiness. +** +** Unless... +** +** If bCheckDefined is true is true then (A) it returns true if the +** value is defined and (B) fails fatally if given an X=Y-format key. +** +** Returns true if zKey evals to true, else false. +*/ +//static +int cmpp_kvp_truth(CmppKeyword const * const pKw, + CmppTokenizer const * const t, + cmpp_kvp const * const kvp, + int bCheckDefined){ + int buul = 0; + if( kvp->v.z ){ + if( bCheckDefined ){ + cmpp_kwd__err(pKw, t, "Value part is not legal for " + "is-defined checks: %.s", + kvp->k.n, kvp->k.z); + } + char * zVal = 0; + unsigned int nVal = 0; + buul = db_define_get(kvp->k.z, (int)kvp->k.n, &zVal, &nVal); + //g_debug(0,("checking key[%.*s]=%.*s\n", (zEq-zKey), zKey, nVal, zVal)); + if( kvp->v.n && nVal ){ + /* FIXME? do this with a query */ + /*g_debug(0,("if get-define [%.*s]=[%.*s] zValPart=%s\n", + (zEq-zKey), zKey, + nVal, zVal, zValPart));*/ + buul = cmpp_val_matches(kvp->v.z, zVal); + //g_debug(0,("buul=%d\n", buul)); + }else{ + assert( 0==kvp->v.n || 0==nVal ); + buul = kvp->v.n == nVal; + } + db_free(zVal); + }else{ + if( bCheckDefined ){ + buul = db_define_has(kvp->k.z, kvp->k.n); + }else{ + buul = db_define_get_bool(kvp->k.z, kvp->k.n); + } + } + return buul; +} + +#if 0 +/* +** A thin proxy for cmpp_kvp_truth(). +*/ +static int cmpp_key_truth(CmppKeyword const * pKw, + CmppTokenizer const * t, + char const *zKey, int bCheckDefined){ + cmpp_kvp kvp = cmpp_kvp_empty; + cmpp_kvp_parse(&kvp, zKey, -1, cmpp_kvp_op_eq1); + return cmpp_kvp_truth(pKw, t, &kvp, bCheckDefined); +} +#endif + +//static +cmpp_kvp_op_e cmpp_t_is_op(CmppTokenizer const * t, int arg){ + if( t->args.argc > arg ){ + char const * const z = (char const *)t->args.argv[arg]; +#define E(N,S) if( strcmp(S,z) ) return cmpp_kvp_op_ ## N; else + cmpp_kvp_op_map(E) +#undef E + if(0) {} + } + return cmpp_kvp_op_none; +} + +/* +** A single part of an #if-type expression. They are parsed from +** CmppTokenizer::args in this form: +** +** not* defined{0,1} key[=[value]] +*/ +struct CmppExprDef { + /* The key part of the input. */ + cmpp_kvp kvp; + struct { + int ndx; + int next; + } arg; + CmppTokenizer const * tizer; + /* Set to 0 or 1 depending how many "not" are parsed. */ + unsigned char bNegated; + /* Set to 1 if "defined" is parsed. */ + unsigned char bCheckDefined; +}; +typedef struct CmppExprDef CmppExprDef; +#define CmppExprDef_empty_m {cmpp_kvp_empty_m,{0,0},0,0,0} +static const CmppExprDef CmppExprDef_empty = CmppExprDef_empty_m; + +/* +** Evaluate cep to true or false and return that value: +** +** If cep->bCheckDefined, return the result of db_define_has(). +** +** Else if cep->kvp.v.z is not NULL then fetch the define's value +** and return the result of cmpp_val_matches(cep->kvp.v.z,thatValue). +** +** Else return the result of db_define_get_bool(). +** +** The returned result accounts for cep->bNegated. +*/ +static int CmppExprDef_eval(CmppExprDef const * cep){ + int buul = 0; + + if( cep->bCheckDefined ){ + assert( !cep->kvp.v.n ); + buul = db_define_has(cep->kvp.k.z, (int)cep->kvp.k.n); + }else if( cep->kvp.v.z ){ + unsigned nVal = 0; + char * zVal = 0; + buul = db_define_get(cep->kvp.k.z, cep->kvp.k.n, &zVal, &nVal); + if( nVal ){ + buul = cmpp_val_matches(cep->kvp.v.z, zVal); + } + db_free(zVal); + }else{ + buul = db_define_get_bool(cep->kvp.k.z, cep->kvp.k.n); + } + return cep->bNegated ? !buul : buul; +} + +/* +** Expects t->args, starting at t->args.argv[startArg], to parse to +** one CmmpExprDef. It clears cep and repopulates it with info about +** the parse. Fails fatally on a parse error. +** +** Returns true if it reads one, false if it doesn't, and fails fatally +** if what it tries to parse is not empty but is not a CmppExprDef. +** +** Specifically, it parses: +** +** not+ defined? Word[=value] +** +*/ +static int CmppExprDef_read_one(CmppKeyword const * pKw, + CmppTokenizer const * t, + int startArg, CmppExprDef * cep){ + char const *zKey = 0; + *cep = CmppExprDef_empty; + cep->arg.ndx = startArg; + assert( t->args.pKw ); + assert( t->args.pKw==pKw ); + cep->tizer = t; + for(int i = startArg; !zKey && iargs.argc; ++i ){ + char const * z = (char const *)t->args.argv[i]; + if( 0==strcmp(z, "not") ){ + cep->bNegated = !cep->bNegated; + }else if( 0==strcmp(z,"defined") ){ + if( cep->bCheckDefined ){ + cmpp_kwd__err(pKw, t, + "Cannot use 'defined' more than once"); + } + cep->bCheckDefined = 1; + }else{ + assert( !zKey ); + cmpp_kvp_parse(&cep->kvp, z, -1, cmpp_kvp_op_eq1); + if( cep->bCheckDefined && cep->kvp.v.z ){ + cmpp_kwd__err(pKw, t, "Cannot use X=Y keys with 'defined'"); + cep->arg.next = ++i; + } + return 1; + } + } + return 0; +} + +/* +** Evals pStart and then proceeds to process any remaining arguments +** in t->args as RHS expressions. Returns the result of the expression +** as a bool. +** +** Specifically, it parses: +** +** and|or CmppExprDef +** +** Where CmppExprDef is the result of CmppExprDef_read_one(). +*/ +static int CmppExprDef_parse_cond(CmppKeyword const *pKw, + CmppTokenizer *t, + CmppExprDef const * pStart){ + enum { Op_none = 0, Op_And, Op_Or }; + int lhs = CmppExprDef_eval(pStart); + int op = Op_none; + int i = pStart->arg.next; + for( ; i < t->args.argc; ++i ){ + CmppExprDef eNext = CmppExprDef_empty; + char const *z = (char const *)t->args.argv[i]; + if( 0==strcmp("and",z) ){ + if( Op_none!=op ) goto multiple_ops; + op = Op_And; + continue; + }else if( 0==strcmp("or",z) ){ + if( Op_none!=op ) goto multiple_ops; + op = Op_Or; + continue; + }else if( !CmppExprDef_read_one(pKw, t, i, &eNext) ){ + if( Op_none!=op ){ + cmpp_t__err(t, "Stray operator: %s",z); + } + } + assert( eNext.kvp.k.z ); + int const rhs = CmppExprDef_eval(&eNext); + switch( op ){ + case Op_none: break; + case Op_And: lhs = lhs && rhs; break; + case Op_Or: lhs = lhs || rhs; break; + default: + assert(!"cannot happen"); + fatal("this cannot happen"); + } + op = Op_none; + } + if( Op_none!=op ){ + cmpp_t__err(t, "Extra operator at end of expression"); + }else if( i < t->args.argc ){ + assert(!"cannot happen"); + cmpp_kwd__err(t->args.pKw, t, "Unhandled extra arguments"); + }else{ + return lhs; + } + assert(!"not reached"); +multiple_ops: + cmpp_t__err(t,"Cannot have multiple operators"); + return 0 /* not reached */; +} + +/* Impl. for #if, #elif, #assert. */ +static void cmpp_kwd_if(CmppKeyword const * pKw, CmppTokenizer *t){ + CmppParseState tmpState = TS_Start; + CmppExprDef cep = CmppExprDef_empty; + //int buul = 0; + assert( TT_If==pKw->ttype + || TT_Elif==pKw->ttype + || TT_Assert==pKw->ttype); + if(t->args.argc<2){ + cmpp_kwd__err(pKw, t, "Expecting an argument"); + } + CmppExprDef_read_one(pKw, t, 1, &cep); + if( !cep.kvp.k.z ){ + cmpp_kwd__err(pKw, t, "Missing key argument"); + } + /*g_debug(0,("%s %s level %u pstate=%d bNot=%d bCheckDefined=%d\n", + pKw->zName, zKey, t->level.ndx, (int)CT_pstate(t), + bNot, bCheckDefined));*/ + switch(pKw->ttype){ + case TT_Assert: + break; + case TT_Elif: + switch(CT_pstate(t)){ + case TS_If: break; + case TS_IfPassed: CT_level(t).flags |= CmppLevel_F_ELIDE; return; + default: + cmpp_kwd__err(pKw, t, "'%s' used out of context", + pKw->zName); + } + break; + case TT_If: + CmppLevel_push(t); + break; + default: + assert(!"cannot happen"); + cmpp_kwd__err(pKw, t, "Unexpected keyword token type"); + break; + } + if( CmppExprDef_parse_cond( pKw, t, &cep ) ){ + CT_pstate(t) = tmpState = TS_IfPassed; + CT_skipLevel(t) = 0; + }else{ + if( TT_Assert==pKw->ttype ){ + cmpp_kwd__err(pKw, t, "Assertion failed: %s", + /* fixme: emit the whole line. We don't have it + handy in a readily-printable form. */ + cep.kvp.k.z); + } + CT_pstate(t) = TS_If /* also for TT_Elif */; + CT_skipLevel(t) = 1; + g_debug(3,("setting CT_skipLevel = 1 @ level %d\n", t->level.ndx)); + } + if( TT_If==pKw->ttype ){ + unsigned const lvlIf = t->level.ndx; + CmppToken const lvlToken = CT_level(t).token; + while(cmpp_next_keyword_line(t)){ + cmpp_process_keyword(t); + if(lvlIf > t->level.ndx){ + assert(TT_Endif == t->token.ttype); + break; + } +#if 0 + if(TS_IfPassed==tmpState){ + tmpState = TS_Start; + t->level.stack[lvlIf].flags |= CmppLevel_F_ELIDE; + g_debug(1,("Setting ELIDE for TS_IfPassed @ lv %d (lvlIf=%d)\n", t->level.ndx, lvlIf)); + } +#endif + } + if(lvlIf <= t->level.ndx){ + cmpp_kwd__err(pKw, t, + "Input ended inside an unterminated %sif " + "opened at [%s] line %u", + g.delim.z, t->zName, lvlToken.lineNo); + } + } +} + +/* Impl. for #else. */ +static void cmpp_kwd_else(CmppKeyword const * pKw, CmppTokenizer *t){ + if(t->args.argc>1){ + cmpp_kwd__err(pKw, t, "Expecting no arguments"); + } + switch(CT_pstate(t)){ + case TS_IfPassed: CT_skipLevel(t) = 1; break; + case TS_If: CT_skipLevel(t) = 0; break; + default: + cmpp_kwd__err(pKw, t, "'%s' with no matching 'if'", + pKw->zName); + } + /*g_debug(0,("else flags=0x%02x skipLevel=%u\n", + CT_level(t).flags, CT_level(t).skipLevel));*/ + CT_pstate(t) = TS_Else; +} + +/* Impl. for #endif. */ +static void cmpp_kwd_endif(CmppKeyword const * pKw, CmppTokenizer *t){ + /* Maintenance reminder: we ignore all arguments after the endif + ** to allow for constructs like: + ** + ** #endif // foo + ** + ** in a manner which does not require a specific comment style */ + switch(CT_pstate(t)){ + case TS_Else: + case TS_If: + case TS_IfPassed: + break; + default: + cmpp_kwd__err(pKw, t, "'%s' with no matching 'if'", + pKw->zName); + } + CmppLevel_pop(t); +} + +/* Impl. for #include. */ +static void cmpp_kwd_include(CmppKeyword const * pKw, CmppTokenizer *t){ + char const * zFile; + char * zResolved; + if(CT_skip(t)) return; + else if(t->args.argc!=2){ + cmpp_kwd__err(pKw, t, "Expecting exactly 1 filename argument"); + } + zFile = (const char *)t->args.argv[1]; + if(db_including_has(zFile)){ + /* Note that different spellings of the same filename + ** will elude this check, but that seems okay, as different + ** spellings means that we're not re-running the exact same + ** invocation. We might want some other form of multi-include + ** protection, rather than this, however. There may well be + ** sensible uses for recursion. */ + cmpp_t__err(t, "Recursive include of file: %s", zFile); + } + zResolved = db_include_search(zFile); + if(zResolved){ + db_including_add(zFile, t->zName, t->token.lineNo); + cmpp_process_file(zResolved); + db_include_rm(zFile); + db_free(zResolved); + }else{ + cmpp_t__err(t, "file not found: %s", zFile); + } +} + + +static void cmpp_dump_defines( FILE * fp, int bIndent ){ + sqlite3_stmt * const q = g_stmt(GStmt_defSelAll); + while( SQLITE_ROW==sqlite3_step(q) ){ + unsigned char const * zK = sqlite3_column_text(q, 0); + unsigned char const * zV = sqlite3_column_text(q, 1); + int const nK = sqlite3_column_bytes(q, 0); + int const nV = sqlite3_column_bytes(q, 1); + fprintf(fp, "%s%.*s = %.*s\n", + bIndent ? "\t" : "", nK, zK, nV, zV); + } + g_stmt_reset(q); +} + +/* Impl. for #pragma. */ +static void cmpp_kwd_pragma(CmppKeyword const * pKw, CmppTokenizer *t){ + const char * zArg; + if(CT_skip(t)) return; + else if(t->args.argc<2){ + cmpp_kwd__err(pKw, t, "Expecting an argument"); + } + zArg = (const char *)t->args.argv[1]; +#define M(X) 0==strcmp(zArg,X) + if(M("defines")){ + cmpp_dump_defines(stderr, 1); + } + else if(M("chomp-F")){ + g.flags.chompF = 1; + }else if(M("no-chomp-F")){ + g.flags.chompF = 0; + } +#if 0 + /* now done by cmpp_kwd_at_policy() */ + else if(M("@")){ + if(t->args.argc>2){ + g.flags.atPolicy = + AtPolicy_fromStr((char const *)t->args.argv[2], 1); + }else{ + g.flags.atPolicy = AT_DEFAULT; + } + }else if(M("no-@")){ + g.flags.atPolicy = AT_OFF; + } +#endif + else{ + cmpp_kwd__err(pKw, t, "Unknown pragma: %s", zArg); + } +#undef M +} + +static void db_step_reset(sqlite3_stmt * const q, char const * zErrTip){ + db_affirm_rc(sqlite3_step(q), zErrTip); + g_stmt_reset(q); +} + +static void cmpp_sp_begin(CmppTokenizer * const t){ + db_step_reset(g_stmt(GStmt_spBegin), "Starting savepoint"); + ++t->nSavepoint; +} + +static void cmpp_sp_rollback(CmppTokenizer * const t){ + if( !t->nSavepoint ){ + cmpp_t__err(t, "Cannot roll back: no active savepoint"); + } + db_step_reset(g_stmt(GStmt_spRollback), + "Rolling back savepoint"); + db_step_reset(g_stmt(GStmt_spRelease), + "Releasing rolled-back savepoint"); + --t->nSavepoint; +} + +static void cmpp_sp_commit(CmppTokenizer * const t){ + if( !t->nSavepoint ){ + cmpp_t__err(t, "Cannot commit: no active savepoint"); + } + db_step_reset(g_stmt(GStmt_spRelease), "Rolling back savepoint"); + --t->nSavepoint; +} + +void CmppTokenizer_cleanup(CmppTokenizer * const t){ + while( t->nSavepoint ){ + cmpp_sp_rollback(t); + } +} + +/* Impl. for #savepoint. */ +static void cmpp_kwd_savepoint(CmppKeyword const * pKw, CmppTokenizer *t){ + const char * zArg; + if(CT_skip(t)) return; + else if(t->args.argc!=2){ + cmpp_kwd__err(pKw, t, "Expecting one argument"); + } + zArg = (const char *)t->args.argv[1]; +#define M(X) 0==strcmp(zArg,X) + if(M("begin")){ + cmpp_sp_begin(t); + }else if(M("rollback")){ + cmpp_sp_rollback(t); + }else if(M("commit")){ + cmpp_sp_commit(t); + }else{ + cmpp_kwd__err(pKw, t, "Unknown savepoint option: %s", zArg); + } +#undef SP_NAME +#undef M +} + +/* #stder impl. */ +static void cmpp_kwd_stderr(CmppKeyword const * pKw, CmppTokenizer *t){ + if(CT_skip(t)) return; + else{ + const char *zBegin = t->args.argc>1 + ? (const char *)t->args.argv[1] : 0; + if(zBegin){ + g_stderr("%s:%u: %s\n", t->zName, t->token.lineNo, zBegin); + }else{ + g_stderr("%s:%u: (no %.*s%s argument)\n", + t->zName, t->token.lineNo, + g.delim.n, g.delim.z, pKw->zName); + } + } +} + +/* Impl. for the @ policy. */ +static void cmpp_kwd_at_policy(CmppKeyword const * pKw, CmppTokenizer *t){ + if(CT_skip(t)) return; + else if(t->args.argc<2){ + g.flags.atPolicy = AT_DEFAULT; + }else{ + g.flags.atPolicy = AtPolicy_fromStr((char const*)t->args.argv[1], 1); + } +} + + +#if 0 +/* Impl. for dummy placeholder. */ +static void cmpp_kwd_todo(CmppKeyword const * pKw, CmppTokenizer *t){ + (void)t; + g_debug(0,("TODO: keyword handler for %s\n", pKw->zName)); +} +#endif + +CmppKeyword aKeywords[] = { +/* Keep these sorted by zName */ +#define S(NAME) DStrings.NAME.z, DStrings.NAME.n + {S(Comment), 0, TT_Comment, cmpp_kwd_noop}, + {S(AtPolicy), 1, TT_AtPolicy, cmpp_kwd_at_policy}, + {S(Assert),1, TT_Assert, cmpp_kwd_if}, + {S(Define), 1, TT_Define, cmpp_kwd_define}, + {S(Elif), 1, TT_Elif, cmpp_kwd_if}, + {S(Else), 1, TT_Else, cmpp_kwd_else}, + {S(Endif), 0, TT_Endif, cmpp_kwd_endif}, + {S(Error), 0, TT_Error, cmpp_kwd_error}, + {S(If), 1, TT_If, cmpp_kwd_if}, + {S(Include), 0, TT_Include, cmpp_kwd_include}, + {S(Pragma), 1, TT_Pragma, cmpp_kwd_pragma}, + {S(Savepoint), 1, TT_Savepoint, cmpp_kwd_savepoint}, + {S(Stderr), 0, TT_Stderr, cmpp_kwd_stderr}, + {S(Undef), 1, TT_Undef, cmpp_kwd_define}, +#undef S + {0,0,TT_Invalid, 0} +}; + +static int cmpp_CmppKeyword(const void *p1, const void *p2){ + char const * zName = (const char *)p1; + CmppKeyword const * kw = (CmppKeyword const *)p2; + return strcmp(zName, kw->zName); +} + +CmppKeyword const * CmppKeyword_search(const char *zName){ + return (CmppKeyword const *)bsearch(zName, &aKeywords[0], + sizeof(aKeywords)/sizeof(aKeywords[0]) - 1, + sizeof(aKeywords[0]), + cmpp_CmppKeyword); +} + +void cmpp_process_keyword(CmppTokenizer * const t){ + assert(t->args.pKw); + assert(t->args.argc); + t->args.pKw->xCall(t->args.pKw, t); + t->args.pKw = 0; + t->args.argc = 0; +} + +void cmpp_process_string(const char * zName, + unsigned char const * zIn, + int nIn){ + nIn = cmpp__strlenu(zIn, nIn); + if( !nIn ) return; + CmppTokenizer const * const oldTok = g.tok; + CmppTokenizer ct = CmppTokenizer_empty; + ct.zName = zName; + ct.zBegin = zIn; + ct.zEnd = zIn + nIn; + while(cmpp_next_keyword_line(&ct)){ + cmpp_process_keyword(&ct); + } + if(0!=ct.level.ndx){ + CmppLevel const * const lv = CmppLevel_get(&ct); + fatal("Input ended inside an unterminated nested construct " + "opened at [%s] line %u", zName, lv->token.lineNo); + } + CmppTokenizer_cleanup(&ct); + g.tok = oldTok; +} + +void cmpp_process_file(const char * zName){ + FileWrapper fw = FileWrapper_empty; + FileWrapper_open(&fw, zName, "r"); + g_FileWrapper_link(&fw); + FileWrapper_slurp(&fw); + g_debug(1,("Read %u byte(s) from [%s]\n", fw.nContent, fw.zName)); + if( fw.zContent ){ + cmpp_process_string(zName, fw.zContent, fw.nContent); + } + g_FileWrapper_close(&fw); +} + + +void fatalv__base(char const *zFile, int line, + char const *zFmt, va_list va){ + FILE * const fp = stderr; + fflush(stdout); + fputc('\n', fp); + if( g.flags.doDebug ){ + fprintf(fp, "%s: ", g.zArgv0); + if( zFile ){ + fprintf(fp, "%s:%d ",zFile, line); + } + } + if( g.tok ){ + fprintf(fp,"@%s:%d: ", + (g.tok->zName && 0==strcmp("-",g.tok->zName)) + ? "" + : g.tok->zName, + g.tok->lineNo); + } + if(zFmt && *zFmt){ + vfprintf(fp, zFmt, va); + } + fputc('\n', fp); + fflush(fp); + exit(1); +} + +void fatal__base(char const *zFile, int line, + char const *zFmt, ...){ + va_list va; + va_start(va, zFmt); + fatalv__base(zFile, line, zFmt, va); + va_end(va); +} + +#undef CT_level +#undef CT_pstate +#undef CT_skipLevel +#undef CT_skip +#undef CLvl_skip + +static void usage(int isErr){ + FILE * const fOut = isErr ? stderr : stdout; + fprintf(fOut, "Usage: %s [flags] [infile...]\n", g.zArgv0); + fprintf(fOut, + "Flags and filenames may be in any order and " + "they are processed in that order.\n" + "\nFlags:\n"); +#define GAP " " +#define arg(F,D) fprintf(fOut,"\n %s\n" GAP "%s\n",F, D) + arg("-o|--outfile FILE","Send output to FILE (default=- (stdout)).\n" + GAP "Because arguments are processed in order, this should\n" + GAP "normally be given before -f."); + arg("-f|--file FILE","Process FILE (default=- (stdin)).\n" + GAP "All non-flag arguments are assumed to be the input files."); + arg("-DXYZ[=value]","Define XYZ to the given value (default=1)."); + arg("-UXYZ","Undefine all defines matching glob XYZ."); + arg("-IXYZ","Add dir XYZ to the " CMPP_DEFAULT_DELIM "include path."); + arg("-FXYZ=filename", + "Define XYZ to the raw contents of the given file.\n" + GAP "The file is not processed as by " CMPP_DEFAULT_DELIM"include\n" + GAP "Maybe it should be. Or maybe we need a new flag for that."); + arg("-d|--delimiter VALUE", "Set keyword delimiter to VALUE " + "(default=" CMPP_DEFAULT_DELIM ")."); + arg("--@policy retain|elide|error|off", + "Specifies how to handle @tokens@ (default=off).\n" + GAP "off = do not look for @tokens@\n" + GAP "retain = parse @tokens@ and retain any undefined ones\n" + GAP "elide = parse @tokens@ and elide any undefined ones\n" + GAP "error = parse @tokens@ and error out for any undefined ones" + ); + arg("-@", "Equivalent to --@policy=error."); + arg("-no-@", "Equivalent to --@policy=off (the default)."); + arg("--sql-trace", "Send a trace of all SQL to stderr."); + arg("--sql-trace-x", + "Like --sql-trace but expand all bound values in the SQL."); + arg("--no-sql-trace", "Disable SQL tracing (default)."); + arg("--chomp-F", "One trailing newline is trimmed from files " + "read via -FXYZ=filename."); + arg("--no-chomp-F", "Disable --chomp-F (default)."); +#undef arg +#undef GAP + fputs("\nFlags which require a value accept either " + "--flag=value or --flag value.\n\n",fOut); +} + +/* +** Expects that *ndx points to the current argv entry and that it is a +** flag which expects a value. This function checks for --flag=val and +** (--flag val) forms. If a value is found then *ndx is adjusted (if +** needed) to point to the next argument after the value and *zVal is +** pointed to the value. If no value is found then it fails fatally. +*/ +static void get_flag_val(int argc, char const * const * argv, int * ndx, + char const **zVal){ + char const * zEq = strchr(argv[*ndx], '='); + if( zEq ){ + *zVal = zEq+1; + return; + } + if(*ndx+1>=argc){ + fatal("Missing value for flag '%s'", argv[*ndx]); + } + *zVal = argv[++*ndx]; +} + +static int arg_is_flag( char const *zFlag, char const *zArg, + char const **zValIfEqX ){ + *zValIfEqX = 0; + if( 0==strcmp(zFlag, zArg) ) return 1; + char const * z = strchr(zArg,'='); + if( z && z>zArg ){ + /* compare the part before the '=' */ + if( 0==strncmp(zFlag, zArg, z-zArg) ){ + if( !zFlag[z-zArg] ){ + *zValIfEqX = z+1; + return 1; + } + /* Else it was a prefix match. */ + } + } + return 0; +} + +int main(int argc, char const * const * argv){ + int rc = 0; + int inclCount = 0; + int nFile = 0; + int ndxTrace = 0; + int expandMode = 0; + char const * zVal = 0; +#define ARGVAL if( !zVal ) get_flag_val(argc, argv, &i, &zVal) +#define M(X) arg_is_flag(X, zArg, &zVal) +#define ISFLAG(X) else if(M(X)) +#define ISFLAG2(X,Y) else if(M(X) || M(Y)) +#define NOVAL if( zVal ) fatal("Unexpected value for %s", zArg) +#define g_out_open \ + if(!g.out.pFile) FileWrapper_open(&g.out, "-", "w"); \ + if(!inclCount){ db_include_dir_add("."); ++inclCount; } (void)0 + + g.zArgv0 = argv[0]; +#define DOIT if(doIt) + for(int doIt = 0; doIt<2; ++doIt){ + /** + Loop through the flags twice. The first time we just validate + and look for --help/-?. The second time we process the flags. + This approach allows us to easily chain multiple files and + flags: + + ./c-pp -Dfoo -o foo x.y -Ufoo -Dbar -o bar x.y + */ + DOIT{ + atexit(cmpp_atexit); + if( 1==ndxTrace ){ + /* Ensure that we start with tracing in the early stage if + --sql-trace is the first arg, in order to log schema + setup. */ + g.sqlTrace.pFile = stderr; + g.sqlTrace.expandSql = expandMode; + } + cmpp_initdb(); + } + for(int i = 1; i < argc; ++i){ + int negate = 0; + char const * zArg = argv[i]; + //g_stderr("i=%d zArg=%s\n", i, zArg); + zVal = 0; + while('-'==*zArg) ++zArg; + if(zArg==argv[i]/*not a flag*/){ + zVal = zArg; + goto do_infile; + } + if( 0==strncmp(zArg,"no-",3) ){ + zArg += 3; + negate = 1; + } + ISFLAG2("?","help"){ + NOVAL; + usage(0); + goto end; + }else if('D'==*zArg){ + ++zArg; + if(!*zArg) fatal("Missing key for -D"); + DOIT { + db_define_add(zArg, 0); + } + }else if('F'==*zArg){ + ++zArg; + if(!*zArg) fatal("Missing key for -F"); + DOIT { + db_define_add_file(zArg); + } + }else if('U'==*zArg){ + ++zArg; + if(!*zArg) fatal("Missing key for -U"); + DOIT { + db_define_rm(zArg); + } + }else if('I'==*zArg){ + ++zArg; + if(!*zArg) fatal("Missing directory for -I"); + DOIT { + db_include_dir_add(zArg); + ++inclCount; + } + } + ISFLAG2("o","outfile"){ + ARGVAL; + DOIT { + FileWrapper_open(&g.out, zVal, "w"); + } + } + ISFLAG2("f","file"){ + ARGVAL; + do_infile: + DOIT { + ++nFile; + g_out_open; + cmpp_process_file(zVal); + } + } + ISFLAG("e"){ + ARGVAL; + DOIT { + ++nFile; + g_out_open; + cmpp_process_string("-e script", ustr_c(zVal), -1); + } + } + ISFLAG("@"){ + NOVAL; + DOIT { + assert( AT_DEFAULT!=AT_OFF ); + g.flags.atPolicy = negate ? AT_OFF : AT_DEFAULT; + } + } + ISFLAG("@policy"){ + AtPolicy aup; + ARGVAL; + aup = AtPolicy_fromStr(zVal, 1); + DOIT { + g.flags.atPolicy = aup; + } + } + ISFLAG("debug"){ + NOVAL; + g.flags.doDebug += negate ? -1 : 1; + } + ISFLAG("sql-trace"){ + NOVAL; + /* Needs to be set before the start of the second pass, when + the db is inited. */ + g.sqlTrace.expandSql = 0; + DOIT { + g.sqlTrace.pFile = negate ? (FILE*)0 : stderr; + }else if( !ndxTrace && !negate ){ + ndxTrace = i; + expandMode = 0; + } + } + ISFLAG("sql-trace-x"){ + NOVAL; + g.sqlTrace.expandSql = 1; + DOIT { + g.sqlTrace.pFile = negate ? (FILE*)0 : stderr; + }else if( !ndxTrace && !negate ){ + ndxTrace = i; + expandMode = 1; + } + } + ISFLAG("chomp-F"){ + NOVAL; + DOIT g.flags.chompF = !negate; + } + ISFLAG2("d","delimiter"){ + ARGVAL; + if( !doIt ){ + g.delim.z = zVal; + g.delim.n = (unsigned short)strlen(zVal); + if(!g.delim.n) fatal("Keyword delimiter may not be empty."); + } + } + ISFLAG2("dd", "dump-defines"){ + DOIT { + FILE * const fp = stderr; + fprintf(fp, "All %sdefine entries:\n", g.delim.z); + cmpp_dump_defines(fp, 1); + } + } + else{ + fatal("Unhandled flag: %s", argv[i]); + } + } + DOIT { + if(!nFile){ + if(!g.out.zName) g.out.zName = "-"; + if(!inclCount){ + db_include_dir_add("."); + ++inclCount; + } + FileWrapper_open(&g.out, g.out.zName, "w"); + cmpp_process_file("-"); + } + } + } + end: + g_cleanup(1); + return rc ? EXIT_FAILURE : EXIT_SUCCESS; +} diff --git a/ext/wasm/common/SqliteTestUtil.js b/ext/wasm/common/SqliteTestUtil.js new file mode 100644 index 0000000000..2c17824c53 --- /dev/null +++ b/ext/wasm/common/SqliteTestUtil.js @@ -0,0 +1,235 @@ +/* + 2022-05-22 + + The author disclaims copyright to this source code. In place of a + legal notice, here is a blessing: + + * May you do good and not evil. + * May you find forgiveness for yourself and forgive others. + * May you share freely, never taking more than you give. + + *********************************************************************** + + This file contains bootstrapping code used by various test scripts + which live in this file's directory. +*/ +'use strict'; +(function(self){ + /* querySelectorAll() proxy */ + const EAll = function(/*[element=document,] cssSelector*/){ + return (arguments.length>1 ? arguments[0] : document) + .querySelectorAll(arguments[arguments.length-1]); + }; + /* querySelector() proxy */ + const E = function(/*[element=document,] cssSelector*/){ + return (arguments.length>1 ? arguments[0] : document) + .querySelector(arguments[arguments.length-1]); + }; + + /** + Helpers for writing sqlite3-specific tests. + */ + self.SqliteTestUtil = { + /** Running total of the number of tests run via + this API. */ + counter: 0, + /** + If expr is a function, it is called and its result + is returned, coerced to a bool, else expr, coerced to + a bool, is returned. + */ + toBool: function(expr){ + return (expr instanceof Function) ? !!expr() : !!expr; + }, + /** abort() if expr is false. If expr is a function, it + is called and its result is evaluated. + */ + assert: function f(expr, msg){ + if(!f._){ + f._ = ('undefined'===typeof abort + ? (msg)=>{throw new Error(msg)} + : abort); + } + ++this.counter; + if(!this.toBool(expr)){ + f._(msg || "Assertion failed."); + } + return this; + }, + /** Identical to assert() but throws instead of calling + abort(). */ + affirm: function(expr, msg){ + ++this.counter; + if(!this.toBool(expr)) throw new Error(msg || "Affirmation failed."); + return this; + }, + /** Calls f() and squelches any exception it throws. If it + does not throw, this function throws. */ + mustThrow: function(f, msg){ + ++this.counter; + let err; + try{ f(); } catch(e){err=e;} + if(!err) throw new Error(msg || "Expected exception."); + return this; + }, + /** + Works like mustThrow() but expects filter to be a regex, + function, or string to match/filter the resulting exception + against. If f() does not throw, this test fails and an Error is + thrown. If filter is a regex, the test passes if + filter.test(error.message) passes. If it's a function, the test + passes if filter(error) returns truthy. If it's a string, the + test passes if the filter matches the exception message + precisely. In all other cases the test fails, throwing an + Error. + + If it throws, msg is used as the error report unless it's falsy, + in which case a default is used. + */ + mustThrowMatching: function(f, filter, msg){ + ++this.counter; + let err; + try{ f(); } catch(e){err=e;} + if(!err) throw new Error(msg || "Expected exception."); + let pass = false; + if(filter instanceof RegExp) pass = filter.test(err.message); + else if(filter instanceof Function) pass = filter(err); + else if('string' === typeof filter) pass = (err.message === filter); + if(!pass){ + throw new Error(msg || ("Filter rejected this exception: "+err.message)); + } + return this; + }, + /** Throws if expr is truthy or expr is a function and expr() + returns truthy. */ + throwIf: function(expr, msg){ + ++this.counter; + if(this.toBool(expr)) throw new Error(msg || "throwIf() failed"); + return this; + }, + /** Throws if expr is falsy or expr is a function and expr() + returns falsy. */ + throwUnless: function(expr, msg){ + ++this.counter; + if(!this.toBool(expr)) throw new Error(msg || "throwUnless() failed"); + return this; + }, + + /** + Parses window.location.search-style string into an object + containing key/value pairs of URL arguments (already + urldecoded). The object is created using Object.create(null), + so contains only parsed-out properties and has no prototype + (and thus no inherited properties). + + If the str argument is not passed (arguments.length==0) then + window.location.search.substring(1) is used by default. If + neither str is passed in nor window exists then false is returned. + + On success it returns an Object containing the key/value pairs + parsed from the string. Keys which have no value are treated + has having the boolean true value. + + Pedantic licensing note: this code has appeared in other source + trees, but was originally written by the same person who pasted + it into those trees. + */ + processUrlArgs: function(str) { + if( 0 === arguments.length ) { + if( ('undefined' === typeof window) || + !window.location || + !window.location.search ) return false; + else str = (''+window.location.search).substring(1); + } + if( ! str ) return false; + str = (''+str).split(/#/,2)[0]; // remove #... to avoid it being added as part of the last value. + const args = Object.create(null); + const sp = str.split(/&+/); + const rx = /^([^=]+)(=(.+))?/; + var i, m; + for( i in sp ) { + m = rx.exec( sp[i] ); + if( ! m ) continue; + args[decodeURIComponent(m[1])] = (m[3] ? decodeURIComponent(m[3]) : true); + } + return args; + } + }; + + /** + This is a module object for use with the emscripten-installed + sqlite3InitModule() factory function. + */ + self.sqlite3TestModule = { + /** + Array of functions to call after Emscripten has initialized the + wasm module. Each gets passed the Emscripten module object + (which is _this_ object). + */ + postRun: [ + /* function(theModule){...} */ + ], + //onRuntimeInitialized: function(){}, + /* Proxy for C-side stdout output. */ + print: (...args)=>{console.log(...args)}, + /* Proxy for C-side stderr output. */ + printErr: (...args)=>{console.error(...args)}, + /** + Called by the Emscripten module init bits to report loading + progress. It gets passed an empty argument when loading is done + (after onRuntimeInitialized() and any this.postRun callbacks + have been run). + */ + setStatus: function f(text){ + if(!f.last){ + f.last = { text: '', step: 0 }; + f.ui = { + status: E('#module-status'), + progress: E('#module-progress'), + spinner: E('#module-spinner') + }; + } + if(text === f.last.text) return; + f.last.text = text; + if(f.ui.progress){ + f.ui.progress.value = f.last.step; + f.ui.progress.max = f.last.step + 1; + } + ++f.last.step; + if(text) { + f.ui.status.classList.remove('hidden'); + f.ui.status.innerText = text; + }else{ + if(f.ui.progress){ + f.ui.progress.remove(); + f.ui.spinner.remove(); + delete f.ui.progress; + delete f.ui.spinner; + } + f.ui.status.classList.add('hidden'); + } + }, + /** + Config options used by the Emscripten-dependent initialization + which happens via this.initSqlite3(). This object gets + (indirectly) passed to sqlite3ApiBootstrap() to configure the + sqlite3 API. + */ + sqlite3ApiConfig: { + wasmfsOpfsDir: "/opfs" + }, + /** + Intended to be called by apps which need to call the + Emscripten-installed sqlite3InitModule() routine. This function + temporarily installs this.sqlite3ApiConfig into the self + object, calls it sqlite3InitModule(), and removes + self.sqlite3ApiConfig after initialization is done. Returns the + promise from sqlite3InitModule(), and the next then() handler + will get the sqlite3 API object as its argument. + */ + initSqlite3: function(){ + self.sqlite3ApiConfig = this.sqlite3ApiConfig; + return self.sqlite3InitModule(this).finally(()=>delete self.sqlite3ApiConfig); + } + }; +})(self/*window or worker*/); diff --git a/ext/wasm/common/emscripten.css b/ext/wasm/common/emscripten.css new file mode 100644 index 0000000000..d8f82c73b2 --- /dev/null +++ b/ext/wasm/common/emscripten.css @@ -0,0 +1,24 @@ +/* emscripten-related styling, used during the module load/intialization processes... */ +.emscripten { padding-right: 0; margin-left: auto; margin-right: auto; display: block; } +div.emscripten { text-align: center; } +div.emscripten_border { border: 1px solid black; } +#module-spinner { overflow: visible; } +#module-spinner > * { + margin-top: 1em; +} +.spinner { + height: 50px; + width: 50px; + margin: 0px auto; + animation: rotation 0.8s linear infinite; + border-left: 10px solid rgb(0,150,240); + border-right: 10px solid rgb(0,150,240); + border-bottom: 10px solid rgb(0,150,240); + border-top: 10px solid rgb(100,0,200); + border-radius: 100%; + background-color: rgb(200,100,250); +} +@keyframes rotation { + from {transform: rotate(0deg);} + to {transform: rotate(360deg);} +} diff --git a/ext/wasm/common/testing.css b/ext/wasm/common/testing.css new file mode 100644 index 0000000000..a9be76764c --- /dev/null +++ b/ext/wasm/common/testing.css @@ -0,0 +1,102 @@ +body { + display: flex; + flex-direction: column; + flex-wrap: wrap; +} +textarea { + font-family: monospace; +} +header { + font-size: 130%; + font-weight: bold; +} +.hidden, .initially-hidden { + position: absolute !important; + opacity: 0 !important; + pointer-events: none !important; + display: none !important; +} +fieldset.options { + font-size: 75%; +} +fieldset > legend { + padding: 0 0.5em; +} +span.labeled-input { + padding: 0.25em; + margin: 0.25em 0.5em; + border-radius: 0.25em; + white-space: nowrap; + background: #0002; +} +.center { text-align: center; } +.error { + color: red; + background-color: yellow; +} +.strong { font-weight: 700 } +.warning { color: firebrick; } +.green { color: darkgreen; } +.tests-pass { background-color: green; color: white } +.tests-fail { background-color: red; color: yellow } +.faded { opacity: 0.5; } +.group-start { + color: blue; + background-color: skyblue; + font-weight: bold; + border-top: 1px dotted blue; + padding: 0.5em; + margin-top: 0.5em; +} +.group-end { + padding: 0.5em; + margin-bottom: 0.25em; + /*border-bottom: 1px dotted blue;*/ +} +.group-end.green { + background: lightgreen; + border-bottom: 1px dotted green; +} +.one-test-line, .skipping-group { + margin-left: 3em; +} +.skipping-test, .skipping-group { + padding: 0.25em 0.5em; + background-color: #ffff73; +} +.skipping-test { + margin-left: 6em; +} +.one-test-summary { + margin-left: 6em; +} +.full-test-summary { + padding-bottom: 0.5em; + padding-top: 0.5em; + border-top: 1px solid black; +} +.input-wrapper { + white-space: nowrap; + display: flex; + align-items: center; +} +#test-output { + border: 1px inset; + border-radius: 0.25em; + padding: 0.25em; + /*max-height: 30em;*/ + overflow: auto; + white-space: break-spaces; + display: flex; flex-direction: column; + font-family: monospace; +} +#test-output.reverse { + flex-direction: column-reverse; +} +label[for] { cursor: pointer } + +h1 { + border-radius: 0.25em; + padding: 0.15em 0.25em; +} +h1:first-of-type {margin: 0 0 0.5em 0;} diff --git a/ext/wasm/common/whwasmutil.js b/ext/wasm/common/whwasmutil.js new file mode 100644 index 0000000000..1c678f31f6 --- /dev/null +++ b/ext/wasm/common/whwasmutil.js @@ -0,0 +1,2522 @@ +/** + 2022-07-08 + + The author disclaims copyright to this source code. In place of a + legal notice, here is a blessing: + + * May you do good and not evil. + * May you find forgiveness for yourself and forgive others. + * May you share freely, never taking more than you give. + + *********************************************************************** + + This code is developed in conjunction with the Jaccwabyt project: + + https://fossil.wanderinghorse.net/r/jaccwabyt + + More specifically: + + https://fossil.wanderinghorse.net/r/jaccwabyt/file/common/whwasmutil.js + + and SQLite: + + https://sqlite.org + + This file is kept in sync between both of those trees. +*/ +/** + The primary goal of this function is to replace, where possible, + Emscripten-generated glue code with equivalent utility code which + can be used in arbitrary WASM environments built with toolchains + other than Emscripten. To that end, it populates the given object + with various WASM-specific APIs. These APIs work with both 32- and + 64-bit WASM builds. + + Forewarning: this API explicitly targets only browser environments. + If a given non-browser environment has the capabilities needed for + a given feature (e.g. TextEncoder), great, but it does not go out + of its way to account for them and does not provide compatibility + crutches for them. + + Intended usage: + + ``` + const target = {}; // ... some object ... + globalThis.WhWasmUtilInstaller(target); + delete globalThis.WhWasmUtilInstaller; + ``` + + The `target` object then holds the APIs. The caller may set certain + properties on it, before calling this, to configure it, as + documented below. + + The global-scope symbol for this function is intended only to + provide an easy way to make it available to 3rd-party scripts and + "should" be deleted after calling it. That symbol is _not_ used + within the library. + + It currently offers alternatives to the following + Emscripten-generated APIs: + + - OPTIONALLY memory allocation, but how this gets imported is + environment-specific. Most of the following features only work + if allocation is available. + + - WASM-exported "indirect function table" access and + manipulation. e.g. creating new WASM-side functions using JS + functions, analog to Emscripten's addFunction() and + uninstallFunction() but slightly different and with more useful + lifetime semantics. + + - Get/set specific heap memory values, analog to Emscripten's + getValue() and setValue(). + + - String length counting in UTF-8 bytes (C-style and JS strings). + + - JS string to C-string conversion and vice versa, analog to + Emscripten's stringToUTF8Array() and friends, but with slighter + different interfaces. + + - JS string to Uint8Array conversion, noting that browsers actually + already have this built in via TextEncoder. + + - "Scoped" allocation, such that allocations made inside of a given + explicit scope will be automatically cleaned up when the scope is + closed. This is fundamentally similar to Emscripten's + stackAlloc() and friends but uses the heap instead of the stack + because access to the stack requires C code. + + - Create JS wrappers for WASM functions, analog to Emscripten's + ccall() and cwrap() functions, except that the automatic + conversions for function arguments and return values can be + easily customized by the client by assigning custom function + signature type names to conversion functions. Essentially, + it's ccall() and cwrap() on steroids. + + How to install... + + Passing an object to this function will install this library's + functionality into that object. It returns its argument. + + After installation, client code "should" delete this function's + global symbol (if any). + + This code requires that the target object have the following + properties, though they needn't be available until the first time + one of the installed APIs is used (as opposed to when this function + is called) except where explicitly noted: + + - `exports` must be a property of the target object OR a property + of `target.instance` (a WebAssembly.Module instance) and it must + contain the symbols exported by the WASM module associated with + this code. In an Enscripten environment it must be set to + `Module['asm']` (versions <=3.1.43) or `wasmExports` (versions + >=3.1.44). The exports object must contain a minimum of the + following symbols: + + - `memory`: a WebAssembly.Memory object representing the WASM + memory. _Alternately_, the `memory` property can be set as + `target.memory`, in particular if the WASM heap memory is + initialized in JS an _imported_ into WASM, as opposed to being + initialized in WASM and exported to JS. + + - `__indirect_function_table`: the WebAssembly.Table object which + holds WASM-exported functions. This API does not strictly + require that the table be able to grow but it will throw if its + `installFunction()` is called and the table cannot grow. + + - `functionTable`: WebAssembly.Table object holding the indirect + function call table. If not set then + `exports.__indirect_function_table` is assumed. Achtung: this + property gets replaced by a function with the same name (because + this API used that name before this config option was added). + + In order to simplify downstream usage, if `target.exports` is not + set when this is called then a property access interceptor + (read-only, configurable, enumerable) gets installed as `exports` + which resolves to `target.instance.exports`, noting that the latter + property need not exist until the first time `target.exports` is + accessed. + + Some APIs _optionally_ make use of the `bigIntEnabled` property of + the target object. It "should" be set to true if the WASM + environment is compiled with BigInt support, else it must be + false. If it is false, certain BigInt-related features will trigger + an exception if invoked. This property, if not set when this is + called, will get a default value of true only if the BigInt64Array + constructor is available, else it will default to false. Having + the BigInt type is not sufficient for full int64 integration with + WASM: the target WASM file must also have been built with that + support. In Emscripten that's done using the `-sWASM_BIGINT` flag. + + Some optional APIs require that the target have the following + methods: + + - 'alloc()` must behave like C's `malloc()`, allocating N bytes of + memory and returning its pointer. In Emscripten this is + conventionally made available via `Module['_malloc']`. This API + requires that the alloc routine throw on allocation error, as + opposed to returning null or 0. + + - 'dealloc()` must behave like C's `free()`, accepting either a + pointer returned from its allocation counterpart or the values + null/0 (for which it must be a no-op). In Emscripten this is + conventionally made available via `Module['_free']`. + + APIs which require allocation routines are explicitly documented as + such and/or have "alloc" in their names. + + Optional configuration values which may be set on target before + calling this: + + - `pointerIR`: an IR-format string for the WASM environment's + pointer size. If set it must be either 'i32' or 'i64'. If not + set, it gets set to whatever this code thinks the pointer size + is. Modifying it after this call has no effect. A reliable + way to get this value is (typeof X()), where X is a function + from target.exports which returns an innocuous pointer. + + - `pointerSize`: if set, it must be one of 4 or 8 and must + correspond to the value of `pointerIR`. If not set, it gets set + to whatever this code thinks the pointer size is (4 unless + `pointerIR` is 'i64'). If `pointerSize` is set but `pointerIR` + is not, `pointerIR` gets set appropriately, and vice versa. + + When building with Emscripten's -sMEMORY64=1, `pointerIR` must be + set to 'i64' and/or `pointerSize` must be set to 8. + + After calling this, the pointerIR and pointerSize properties are + replaced with a read-only Object member named target.ptr. It + contains the following read-only helper methods and properties to + assist in using WASM pointers without having to know what type they + are: + + - `size` = pointerSize + + - `ir` = pointerIR + + - `null` = a "null" pointer of type Number or BigInt. Equivalent to + one of Number(0) or BigInt(0). This value compares === to + WASM NULL pointers. + + - `coerce(arg)` = equivalent to one of Number(arg) or BigInt(arg||0). + + - `add(...args)` = performs "pointer arithmetic" (`wasmPtr+offset` + does not work in 64-bit builds unless all operands are of type + BigInt). Adds up all of its arguments, accounting for whether + each is a Number of BigInt, and returns either a Number or + BigInt. + + - `addn(...args)` = like `add()` but always returns its result as a + Number. Equivalent to Number(add(...)). + + ------------------------------------------------------------ + Design notes: + + - This function should probably take a config object and return the + newly-created (or config-provided) target. The current approach + seemed better at the time. +*/ +globalThis.WhWasmUtilInstaller = function(target){ + 'use strict'; + if(undefined===target.bigIntEnabled){ + target.bigIntEnabled = !!globalThis['BigInt64Array']; + } + + /** Throws a new Error, the message of which is the concatenation of + all args with a space between each. */ + const toss = (...args)=>{throw new Error(args.join(' '))}; + + /** + As of 2025-09-21, this library works with 64-bit WASM modules + built with Emscripten's -sMEMORY64=1. + */ + if( target.pointerSize && !target.pointerIR ){ + target.pointerIR = (4===target.pointerSize ? 'i32' : 'i64'); + } + const __ptrIR = (target.pointerIR ??= 'i32'); + const __ptrSize = (target.pointerSize ??= + ('i32'===__ptrIR ? 4 : ('i64'===__ptrIR ? 8 : 0))); + delete target.pointerSize; + delete target.pointerIR; + + if( 'i32'!==__ptrIR && 'i64'!==__ptrIR ){ + toss("Invalid pointerIR:",__ptrIR); + }else if( 8!==__ptrSize && 4!==__ptrSize ){ + toss("Invalid pointerSize:",__ptrSize); + } + + /** Either BigInt or, if !target.bigIntEnabled, a function which + throws complaining that BigInt is not enabled. */ + const __BigInt = target.bigIntEnabled + ? (v)=>BigInt(v || 0) + : (v)=>toss("BigInt support is disabled in this build."); + + const __Number = (v)=>Number(v||0)/*treat undefined the same as null*/; + + /** + If target.ptr.ir==='i32' then this is equivalent to + Number(v||0) else it's equivalent to BigInt(v||0), throwing + if BigInt support is disabled. + + Why? Because Number(null)===0, but BigInt(null) throws. We + perform the same for Number to allow the undefined value to be + treated as a NULL WASM pointer, primarily to reduce friction in + many SQLite3 bindings which have long relied on that. + */ + const __asPtrType = (4===__ptrSize) ? __Number : __BigInt; + + /** + The number 0 as either type Number or BigInt, depending on + target.ptr.ir. + */ + const __NullPtr = __asPtrType(0); + + /** + Expects any number of numeric arguments, each one of either type + Number or BigInt. It sums them up (from an implicit starting + point of 0 or 0n) and returns them as a number of the same type + which target.ptr.coerce() uses. + + This is a workaround for not being able to mix Number/BigInt in + addition/subtraction expressions (which we frequently need for + calculating pointer offsets). + */ + const __ptrAdd = function(...args){ + let rc = __asPtrType(0); + for(const v of args) rc += __asPtrType(v); + return rc; + }; + + /** Set up target.ptr... */ + { + const __ptr = Object.create(null); + Object.defineProperty(target, 'ptr', { + enumerable: true, + get: ()=>__ptr, + set: ()=>toss("The ptr property is read-only.") + }); + (function f(name, val){ + Object.defineProperty(__ptr, name, { + enumerable: true, + get: ()=>val, + set: ()=>toss("ptr["+name+"] is read-only.") + }); + return f; + })( + 'null', __NullPtr + )( + 'size', __ptrSize + )( + 'ir', __ptrIR + )( + 'coerce', __asPtrType + )( + 'add', __ptrAdd + )( + 'addn', (4===__ptrIR) ? __ptrAdd : (...args)=>Number(__ptrAdd(...args)) + ); + } + + if(!target.exports){ + Object.defineProperty(target, 'exports', { + enumerable: true, configurable: true, + get: ()=>(target.instance?.exports) + }); + } + + /** Stores various cached state. */ + const cache = Object.create(null); + /** Previously-recorded size of cache.memory.buffer, noted so that + we can recreate the view objects if the heap grows. */ + cache.heapSize = 0; + /** WebAssembly.Memory object extracted from target.memory or + target.exports.memory the first time heapWrappers() is + called. */ + cache.memory = null; + /** uninstallFunction() puts table indexes in here for reuse and + installFunction() extracts them. */ + cache.freeFuncIndexes = []; + /** + List-of-lists used by scopedAlloc() and friends. + */ + cache.scopedAlloc = []; + + /** Push the pointer ptr to the current cache.scopedAlloc list + (which must already exist) and return ptr. */ + cache.scopedAlloc.pushPtr = (ptr)=>{ + cache.scopedAlloc[cache.scopedAlloc.length-1].push(ptr); + return ptr; + }; + + cache.utf8Decoder = new TextDecoder(); + cache.utf8Encoder = new TextEncoder('utf-8'); + + /** + For the given IR-like string in the set ('i8', 'i16', 'i32', + 'f32', 'float', 'i64', 'f64', 'double', '*'), or any string value + ending in '*', returns the sizeof for that value + (target.ptr.size in the latter case). For any other value, it + returns the undefined value. + */ + target.sizeofIR = (n)=>{ + switch(n){ + case 'i8': return 1; + case 'i16': return 2; + case 'i32': case 'f32': case 'float': return 4; + case 'i64': case 'f64': case 'double': return 8; + case '*': return __ptrSize; + default: + return (''+n).endsWith('*') ? __ptrSize : undefined; + } + }; + + /** + If (cache.heapSize !== cache.memory.buffer.byteLength), i.e. if + the heap has grown since the last call, updates cache.HEAPxyz. + Returns the cache object. + */ + const heapWrappers = function(){ + if(!cache.memory){ + cache.memory = (target.memory instanceof WebAssembly.Memory) + ? target.memory : target.exports.memory; + }else if(cache.heapSize === cache.memory.buffer.byteLength){ + return cache; + } + // heap is newly-acquired or has been resized.... + const b = cache.memory.buffer; + cache.HEAP8 = new Int8Array(b); cache.HEAP8U = new Uint8Array(b); + cache.HEAP16 = new Int16Array(b); cache.HEAP16U = new Uint16Array(b); + cache.HEAP32 = new Int32Array(b); cache.HEAP32U = new Uint32Array(b); + cache.HEAP32F = new Float32Array(b); cache.HEAP64F = new Float64Array(b); + if(target.bigIntEnabled){ + if( 'undefined'!==typeof BigInt64Array ){ + cache.HEAP64 = new BigInt64Array(b); cache.HEAP64U = new BigUint64Array(b); + }else{ + toss("BigInt support is enabled, but the BigInt64Array type is missing."); + } + } + cache.heapSize = b.byteLength; + return cache; + }; + + /** Convenience equivalent of this.heapForSize(8,false). */ + target.heap8 = ()=>heapWrappers().HEAP8; + + /** Convenience equivalent of this.heapForSize(8,true). */ + target.heap8u = ()=>heapWrappers().HEAP8U; + + /** Convenience equivalent of this.heapForSize(16,false). */ + target.heap16 = ()=>heapWrappers().HEAP16; + + /** Convenience equivalent of this.heapForSize(16,true). */ + target.heap16u = ()=>heapWrappers().HEAP16U; + + /** Convenience equivalent of this.heapForSize(32,false). */ + target.heap32 = ()=>heapWrappers().HEAP32; + + /** Convenience equivalent of this.heapForSize(32,true). */ + target.heap32u = ()=>heapWrappers().HEAP32U; + + /** + Requires n to be one of: + + - integer 8, 16, or 32. + - A integer-type TypedArray constructor: Int8Array, Int16Array, + Int32Array, or their Uint counterparts. + + If this.bigIntEnabled is true, it also accepts the value 64 or a + BigInt64Array/BigUint64Array, else it throws if passed 64 or one + of those constructors. + + Returns an integer-based TypedArray view of the WASM heap memory + buffer associated with the given block size. If passed an integer + as the first argument and unsigned is truthy then the "U" + (unsigned) variant of that view is returned, else the signed + variant is returned. If passed a TypedArray value, the 2nd + argument is ignored. Float32Array and Float64Array views are not + supported by this function. + + Growth of the heap will invalidate any references to this heap, + so do not hold a reference longer than needed and do not use a + reference after any operation which may allocate. Instead, + re-fetch the reference by calling this function again. + + Throws if passed an invalid n. + */ + target.heapForSize = function(n,unsigned = true){ + let ctor; + const c = (cache.memory && cache.heapSize === cache.memory.buffer.byteLength) + ? cache : heapWrappers(); + switch(n){ + case Int8Array: return c.HEAP8; case Uint8Array: return c.HEAP8U; + case Int16Array: return c.HEAP16; case Uint16Array: return c.HEAP16U; + case Int32Array: return c.HEAP32; case Uint32Array: return c.HEAP32U; + case 8: return unsigned ? c.HEAP8U : c.HEAP8; + case 16: return unsigned ? c.HEAP16U : c.HEAP16; + case 32: return unsigned ? c.HEAP32U : c.HEAP32; + case 64: + if(c.HEAP64) return unsigned ? c.HEAP64U : c.HEAP64; + break; + default: + if(target.bigIntEnabled){ + if(n===globalThis['BigUint64Array']) return c.HEAP64U; + else if(n===globalThis['BigInt64Array']) return c.HEAP64; + break; + } + } + toss("Invalid heapForSize() size: expecting 8, 16, 32,", + "or (if BigInt is enabled) 64."); + }; + + const __funcTable = target.functionTable; + delete target.functionTable; + + /** + Returns the WASM-exported "indirect function table". + */ + target.functionTable = __funcTable + ? ()=>__funcTable + : ()=>target.exports.__indirect_function_table + /** -----------------^^^^^ "seems" to be a standardized export name. + From Emscripten release notes from 2020-09-10: + - Use `__indirect_function_table` as the import name for the + table, which is what LLVM does. + + We must delay access to target.exports until after the library + is bootstrapped. + */; + + /** + Given a function pointer, returns the WASM function table entry + if found, else returns a falsy value: undefined if fptr is out of + range or null if it's in range but the table entry is empty. + */ + target.functionEntry = function(fptr){ + const ft = target.functionTable(); + //console.debug("functionEntry(",arguments,")", __asPtrType(fptr)); + //-sMEMORY64=1: we get a BigInt fptr and ft.get() wants a BigInt. + //-sMEMORY64=2: we get a Number fptr and ft.get() wants a Number. + return fptr < ft.length ? ft.get(__asPtrType(fptr)) : undefined; + }; + + /** + Creates a WASM function which wraps the given JS function and + returns the JS binding of that WASM function. The signature + string must be the Jaccwabyt-format or Emscripten + addFunction()-format function signature string. In short: in may + have one of the following formats: + + - Emscripten: `"x..."`, where the first x is a letter representing + the result type and subsequent letters represent the argument + types. Functions with no arguments have only a single + letter. + + - Jaccwabyt: `"x(...)"` where `x` is the letter representing the + result type and letters in the parens (if any) represent the + argument types. Functions with no arguments use `x()`. + + Supported letters: + + - `i` = int32 + - `p` = int32 or int64 ("pointer"), depending on target.ptr.size + - `j` = int64 + - `f` = float32 + - `d` = float64 + - `v` = void, only legal for use as the result type + + It throws if an invalid signature letter is used. + + Jaccwabyt-format signatures support some additional letters which + have no special meaning here but (in this context) act as aliases + for other letters: + + - `s`, `P`: same as `p` + + Sidebar: this code is developed together with Jaccwabyt, thus the + support for its signature format. + + The arguments may be supplied in either order: (func,sig) or + (sig,func). + */ + target.jsFuncToWasm = function f(func, sig){ + /** Attribution: adapted up from Emscripten-generated glue code, + refactored primarily for efficiency's sake, eliminating + call-local functions and superfluous temporary arrays. */ + if(!f._){/*static init...*/ + f._ = { + /* Map of signature letters to type IR values */ + sigTypes: Object.assign(Object.create(null),{ + i: 'i32', p: __ptrIR, P: __ptrIR, s: __ptrIR, + j: 'i64', f: 'f32', d: 'f64' + }), + + /* Map of type IR values to WASM type code values */ + typeCodes: Object.assign(Object.create(null),{ + f64: 0x7c, f32: 0x7d, i64: 0x7e, i32: 0x7f + }), + + /** Encodes n, which must be <2^14 (16384), into target array + tgt, as a little-endian value, using the given method + ('push' or 'unshift'). */ + uleb128Encode: (tgt, method, n)=>{ + if(n<128) tgt[method](n); + else tgt[method]( (n % 128) | 128, n>>7); + }, + + /** Intentionally-lax pattern for Jaccwabyt-format function + pointer signatures, the intent of which is simply to + distinguish them from Emscripten-format signatures. The + downstream checks are less lax. */ + rxJSig: /^(\w)\((\w*)\)$/, + + /** Returns the parameter-value part of the given signature + string. */ + sigParams: (sig)=>{ + const m = f._.rxJSig.exec(sig); + return m ? m[2] : sig.substr(1); + }, + + /** Returns the IR value for the given letter or throws + if the letter is invalid. */ + letterType: (x)=>f._.sigTypes[x] || toss("Invalid signature letter:",x), + + /** Pushes the WASM data type code for the given signature + letter to the given target array. Throws if letter is + invalid. */ + pushSigType: (dest, letter)=>dest.push(f._.typeCodes[f._.letterType(letter)]) + + /** Returns an object describing the result type and parameter + type(s) of the given function signature, or throws if the + signature is invalid. */ + /******** // only valid for use with the WebAssembly.Function ctor, which + // is not yet documented on MDN. + sigToWasm: function(sig){ + const rc = {parameters:[], results: []}; + if('v'!==sig[0]) rc.results.push(f.sigTypes(sig[0])); + for(const x of f._.sigParams(sig)){ + rc.parameters.push(f._.typeCodes(x)); + } + return rc; + },************/ + }; + }/*static init*/ + if('string'===typeof func){ + const x = sig; + sig = func; + func = x; + } + const _ = f._; + const sigParams = _.sigParams(sig); + const wasmCode = [0x01/*count: 1*/, 0x60/*function*/]; + _.uleb128Encode(wasmCode, 'push', sigParams.length); + for(const x of sigParams) _.pushSigType(wasmCode, x); + if('v'===sig[0]) wasmCode.push(0); + else{ + wasmCode.push(1); + _.pushSigType(wasmCode, sig[0]); + } + _.uleb128Encode(wasmCode, 'unshift', wasmCode.length)/* type section length */; + wasmCode.unshift( + 0x00, 0x61, 0x73, 0x6d, /* magic: "\0asm" */ + 0x01, 0x00, 0x00, 0x00, /* version: 1 */ + 0x01 /* type section code */ + ); + wasmCode.push( + /* import section: */ 0x02, 0x07, + /* (import "e" "f" (func 0 (type 0))): */ + 0x01, 0x01, 0x65, 0x01, 0x66, 0x00, 0x00, + /* export section: */ 0x07, 0x05, + /* (export "f" (func 0 (type 0))): */ + 0x01, 0x01, 0x66, 0x00, 0x00 + ); + return (new WebAssembly.Instance( + new WebAssembly.Module(new Uint8Array(wasmCode)), { + e: { f: func } + })).exports['f']; + }/*jsFuncToWasm()*/; + + /** + Documented as target.installFunction() except for the 3rd + argument: if truthy, the newly-created function pointer + is stashed in the current scoped-alloc scope and will be + cleaned up at the matching scopedAllocPop(), else it + is not stashed there. + */ + const __installFunction = function f(func, sig, scoped){ + if(scoped && !cache.scopedAlloc.length){ + toss("No scopedAllocPush() scope is active."); + } + if('string'===typeof func){ + const x = sig; + sig = func; + func = x; + } + if('string'!==typeof sig || !(func instanceof Function)){ + toss("Invalid arguments: expecting (function,signature) "+ + "or (signature,function)."); + } + const ft = target.functionTable(); + const oldLen = __asPtrType(ft.length); + let ptr; + while(cache.freeFuncIndexes.length){ + ptr = cache.freeFuncIndexes.pop(); + if(ft.get(ptr)){ /* Table was modified via a different API */ + ptr = null; + continue; + }else{ + break; + } + } + if(!ptr){ + ptr = __asPtrType(oldLen); + ft.grow(__asPtrType(1)); + } + try{ + /*this will only work if func is a WASM-exported function*/ + ft.set(ptr, func); + if(scoped){ + cache.scopedAlloc.pushPtr(ptr); + } + return ptr; + }catch(e){ + if(!(e instanceof TypeError)){ + if(ptr===oldLen) cache.freeFuncIndexes.push(oldLen); + throw e; + } + } + // It's not a WASM-exported function, so compile one... + try { + const fptr = target.jsFuncToWasm(func, sig); + ft.set(ptr, fptr); + if(scoped){ + cache.scopedAlloc.pushPtr(ptr); + } + }catch(e){ + if(ptr===oldLen) cache.freeFuncIndexes.push(oldLen); + throw e; + } + return ptr; + }; + + /** + Expects a JS function and signature, exactly as for + this.jsFuncToWasm(). It uses that function to create a + WASM-exported function, installs that function to the next + available slot of this.functionTable(), and returns the + function's index in that table (which acts as a pointer to that + function). The returned pointer can be passed to + uninstallFunction() to uninstall it and free up the table slot + for reuse. + + If passed (string,function) arguments then it treats the first + argument as the signature and second as the function. + + As a special case, if the passed-in function is a WASM-exported + function then the signature argument is ignored and func is + installed as-is, without requiring re-compilation/re-wrapping. + + This function will propagate an exception if + WebAssembly.Table.grow() throws or this.jsFuncToWasm() throws. + The former case can happen in an Emscripten-compiled environment + when building without Emscripten's `-sALLOW_TABLE_GROWTH` flag. + + Sidebar: this function differs from Emscripten's addFunction() + _primarily_ in that it does not share that function's + undocumented behavior of reusing a function if it's passed to + addFunction() more than once, which leads to uninstallFunction() + breaking clients which do not take care to avoid that case: + + https://github.com/emscripten-core/emscripten/issues/17323 + */ + target.installFunction = (func, sig)=>__installFunction(func, sig, false); + + /** + Works exactly like installFunction() but requires that a + scopedAllocPush() is active and uninstalls the given function + when that alloc scope is popped via scopedAllocPop(). + This is used for implementing JS/WASM function bindings which + should only persist for the life of a call into a single + C-side function. + */ + target.scopedInstallFunction = (func, sig)=>__installFunction(func, sig, true); + + /** + Requires a pointer value previously returned from + this.installFunction(). Removes that function from the WASM + function table, marks its table slot as free for re-use, and + returns that function. It is illegal to call this before + installFunction() has been called and results are undefined if + ptr was not returned by that function. The returned function + may be passed back to installFunction() to reinstall it. + + To simplify certain use cases, if passed a falsy non-0 value + (noting that 0 is a valid function table index), this function + has no side effects and returns undefined. + */ + target.uninstallFunction = function(ptr){ + if(!ptr && 0!==ptr) return undefined; + const fi = cache.freeFuncIndexes; + const ft = target.functionTable(); + fi.push(ptr); + const rc = ft.get(ptr); + ft.set(ptr, null); + return rc; + }; + + /** + Given a WASM heap memory address and a data type name in the form + (i8, i16, i32, i64, float (or f32), double (or f64)), this + fetches the numeric value from that address and returns it as a + number or, for the case of type='i64', a BigInt (with the caveat + BigInt will trigger an exception if this.bigIntEnabled is + falsy). Throws if given an invalid type. + + If the first argument is an array, it is treated as an array of + addresses and the result is an array of the values from each of + those address, using the same 2nd argument for determining the + value type to fetch. + + As a special case, if type ends with a `*`, it is considered to + be a pointer type and is treated as the WASM numeric type + appropriate for the pointer size (==this.ptr.ir). + + While possibly not obvious, this routine and its poke() + counterpart are how pointer-to-value _output_ parameters in + WASM-compiled C code can be interacted with: + + ``` + const ptr = alloc(4); + poke32(ptr, 0); // clear the ptr's value + aCFuncWithOutputPtrToInt32Arg(ptr); // e.g. void foo(int *x); + const result = peek32(ptr); // fetch ptr's value + dealloc(ptr); + ``` + + scopedAlloc() and friends can be used to make handling of + `ptr` safe against leaks in the case of an exception: + + ``` + let result; + const scope = scopedAllocPush(); + try{ + const ptr = scopedAlloc(4); + poke32(ptr, 0); + aCFuncWithOutputPtrArg(ptr); + result = peek32(ptr); + }finally{ + scopedAllocPop(scope); + } + ``` + + As a rule poke() must be called to set (typically zero out) the + pointer's value, else it will contain an essentially random + value. + + ACHTUNG: calling this often, e.g. in a loop, can have a noticably + painful impact on performance. Rather than doing so, use + heapForSize() to fetch the heap object and read directly from it. + + ACHTUNG #2: ptr may be a BigInt (and generally will be in 64-bit + builds) but this function must coerce it into a Number in order + to access the heap's contents. Ergo: BitInts outside of the + (extrardinarily genereous) address range exposed to browser-side + WASM may cause misbehavior. + + See also: poke() + */ + target.peek = function f(ptr, type='i8'){ + if(type.endsWith('*')) type = __ptrIR; + const c = (cache.memory && cache.heapSize === cache.memory.buffer.byteLength) + ? cache : heapWrappers(); + const list = Array.isArray(ptr) ? [] : undefined; + let rc; + do{ + if(list) ptr = arguments[0].shift(); + switch(type){ + case 'i1': + case 'i8': rc = c.HEAP8[Number(ptr/*tag:64bit*/)>>0]; break; + case 'i16': rc = c.HEAP16[Number(ptr/*tag:64bit*/)>>1]; break; + case 'i32': rc = c.HEAP32[Number(ptr/*tag:64bit*/)>>2]; break; + case 'float': case 'f32': rc = c.HEAP32F[Number(ptr/*tag:64bit*/)>>2]; break; + case 'double': case 'f64': rc = Number(c.HEAP64F[Number(ptr/*tag:64bit*/)>>3]); break; + case 'i64': + if(c.HEAP64){ + rc = __BigInt(c.HEAP64[Number(ptr/*tag:64bit*/)>>3]); + break; + } + /* fallthru */ + default: + toss('Invalid type for peek():',type); + } + if(list) list.push(rc); + }while(list && arguments[0].length); + return list || rc; + }; + + /** + The counterpart of peek(), this sets a numeric value at the given + WASM heap address, using the 3rd argument to define how many + bytes are written. Throws if given an invalid type. See peek() + for details about the `type` argument. If the 3rd argument ends + with `*` then it is treated as a pointer type and this function + behaves as if the 3rd argument were this.ptr.ir. + + If the first argument is an array, it is treated like a list + of pointers and the given value is written to each one. + + Returns `this`. (Prior to 2022-12-09 it returned this function.) + + ACHTUNG #1: see peek()'s ACHTUNG #1. + + ACHTUNG #2: see peek()'s ACHTUNG #2. + */ + target.poke = function(ptr, value, type='i8'){ + if (type.endsWith('*')) type = __ptrIR; + const c = (cache.memory && cache.heapSize === cache.memory.buffer.byteLength) + ? cache : heapWrappers(); + for(const p of (Array.isArray(ptr) ? ptr : [ptr])){ + switch (type) { + case 'i1': + case 'i8': c.HEAP8[Number(p/*tag:64bit*/)>>0] = value; continue; + case 'i16': c.HEAP16[Number(p/*tag:64bit*/)>>1] = value; continue; + case 'i32': c.HEAP32[Number(p/*tag:64bit*/)>>2] = value; continue; + case 'float': case 'f32': c.HEAP32F[Number(p/*tag:64bit*/)>>2] = value; continue; + case 'double': case 'f64': c.HEAP64F[Number(p/*tag:64bit*/)>>3] = value; continue; + case 'i64': + if(c.HEAP64){ + c.HEAP64[Number(p/*tag:64bit*/)>>3] = __BigInt(value); + continue; + } + /* fallthru */ + default: + toss('Invalid type for poke(): ' + type); + } + } + return this; + }; + + /** + Convenience form of peek() intended for fetching + pointer-to-pointer values. If passed a single non-array argument + it returns the value of that one pointer address. If passed + multiple arguments, or a single array of arguments, it returns an + array of their values. + */ + target.peekPtr = (...ptr)=>target.peek( (1===ptr.length ? ptr[0] : ptr), __ptrIR ); + + /** + A variant of poke() intended for setting pointer-to-pointer + values. Its differences from poke() are that (1) it defaults to a + value of 0 and (2) it always writes to the pointer-sized heap + view. + */ + target.pokePtr = (ptr, value=0)=>target.poke(ptr, value, __ptrIR); + + /** + Convenience form of peek() intended for fetching i8 values. If + passed a single non-array argument it returns the value of that + one pointer address. If passed multiple arguments, or a single + array of arguments, it returns an array of their values. + */ + target.peek8 = (...ptr)=>target.peek( (1===ptr.length ? ptr[0] : ptr), 'i8' ); + /** + Convenience form of poke() intended for setting individual bytes. + Its difference from poke() is that it always writes to the + i8-sized heap view. + */ + target.poke8 = (ptr, value)=>target.poke(ptr, value, 'i8'); + /** i16 variant of peek8(). */ + target.peek16 = (...ptr)=>target.peek( (1===ptr.length ? ptr[0] : ptr), 'i16' ); + /** i16 variant of poke8(). */ + target.poke16 = (ptr, value)=>target.poke(ptr, value, 'i16'); + /** i32 variant of peek8(). */ + target.peek32 = (...ptr)=>target.peek( (1===ptr.length ? ptr[0] : ptr), 'i32' ); + /** i32 variant of poke8(). */ + target.poke32 = (ptr, value)=>target.poke(ptr, value, 'i32'); + /** i64 variant of peek8(). Will throw if this build is not + configured for BigInt support. */ + target.peek64 = (...ptr)=>target.peek( (1===ptr.length ? ptr[0] : ptr), 'i64' ); + /** i64 variant of poke8(). Will throw if this build is not + configured for BigInt support. Note that this returns + a BigInt-type value, not a Number-type value. */ + target.poke64 = (ptr, value)=>target.poke(ptr, value, 'i64'); + /** f32 variant of peek8(). */ + target.peek32f = (...ptr)=>target.peek( (1===ptr.length ? ptr[0] : ptr), 'f32' ); + /** f32 variant of poke8(). */ + target.poke32f = (ptr, value)=>target.poke(ptr, value, 'f32'); + /** f64 variant of peek8(). */ + target.peek64f = (...ptr)=>target.peek( (1===ptr.length ? ptr[0] : ptr), 'f64' ); + /** f64 variant of poke8(). */ + target.poke64f = (ptr, value)=>target.poke(ptr, value, 'f64'); + + /** Deprecated alias for getMemValue() */ + target.getMemValue = target.peek; + /** Deprecated alias for peekPtr() */ + target.getPtrValue = target.peekPtr; + /** Deprecated alias for poke() */ + target.setMemValue = target.poke; + /** Deprecated alias for pokePtr() */ + target.setPtrValue = target.pokePtr; + + /** + Returns true if the given value appears to be legal for use as + a WASM pointer value. Its _range_ of values is not (cannot be) + validated except to ensure that it is a 32-bit integer with a + value of 0 or greater. Likewise, it cannot verify whether the + value actually refers to allocated memory in the WASM heap. + + Whether or not null or undefined are legal are context-dependent. + They generally are legal but this function does not treat them as + such because they're not strictly legal for passing as-is as WASM + integer arguments. + */ + target.isPtr32 = (ptr)=>( + 'number'===typeof ptr && ptr>=0 && ptr===(ptr|0) + ); + + /** 64-bit counterpart of isPtr32() and falls back to that function + if ptr is not a BigInt. */ + target.isPtr64 = (ptr)=>( + ('bigint'===typeof ptr) ? ptr >= 0 : target.isPtr32(ptr) + ); + + /** + isPtr() is an alias for isPtr32() or isPtr64(), depending on the + value of target.ptr.size. + */ + target.isPtr = (4===__ptrSize) ? target.isPtr32 : target.isPtr64; + + /** + Expects ptr to be a pointer into the WASM heap memory which + refers to a NUL-terminated C-style string encoded as UTF-8. + Returns the length, in bytes, of the string, as for `strlen(3)`. + As a special case, if !ptr or if it's not a pointer then it + returns `null`. Throws if ptr is out of range for + target.heap8u(). + */ + target.cstrlen = function(ptr){ + if(!ptr || !target.isPtr(ptr)) return null; + ptr = Number(ptr) /*tag:64bit*/; + const h = heapWrappers().HEAP8U; + let pos = ptr; + for( ; h[pos] !== 0; ++pos ){} + return Number(pos - ptr); + }; + + /** Internal helper to use in operations which need to distinguish + between TypedArrays which are backed by a SharedArrayBuffer + from those which are not. */ + const __SAB = ('undefined'===typeof SharedArrayBuffer) + ? function(){/*dummy class*/} : SharedArrayBuffer; + /** Returns true if the given TypedArray object is backed by a + SharedArrayBuffer, else false. */ + const isSharedTypedArray = (aTypedArray)=>(aTypedArray.buffer instanceof __SAB); + + target.isSharedTypedArray = isSharedTypedArray; + + /** + Returns either aTypedArray.slice(begin,end) (if + aTypedArray.buffer is a SharedArrayBuffer) or + aTypedArray.subarray(begin,end) (if it's not). + + This distinction is important for APIs which don't like to + work on SABs, e.g. TextDecoder, and possibly for our + own APIs which work on memory ranges which "might" be + modified by other threads while they're working. + + begin and end may be of type Number or (in 64-bit builds) BigInt + (which get coerced to Numbers). + */ + const typedArrayPart = (aTypedArray, begin, end)=>{ + if( 8===__ptrSize ){ + // slice() and subarray() do not like BigInt args. + if( 'bigint'===typeof begin ) begin = Number(begin); + if( 'bigint'===typeof end ) end = Number(end); + } + return isSharedTypedArray(aTypedArray) + ? aTypedArray.slice(begin, end) + : aTypedArray.subarray(begin, end); + }; + + target.typedArrayPart = typedArrayPart; + + /** + Uses TextDecoder to decode the given half-open range of the given + TypedArray to a string. This differs from a simple call to + TextDecoder in that it accounts for whether the first argument is + backed by a SharedArrayBuffer or not, and can work more + efficiently if it's not (TextDecoder refuses to act upon an SAB). + + If begin/end are not provided or are falsy then each defaults to + the start/end of the array. + */ + const typedArrayToString = (typedArray, begin, end)=> + cache.utf8Decoder.decode( + typedArrayPart(typedArray, begin, end) + ); + + target.typedArrayToString = typedArrayToString; + + /** + Expects ptr to be a pointer into the WASM heap memory which + refers to a NUL-terminated C-style string encoded as UTF-8. This + function counts its byte length using cstrlen() then returns a + JS-format string representing its contents. As a special case, if + ptr is falsy or not a pointer, `null` is returned. + */ + target.cstrToJs = function(ptr){ + const n = target.cstrlen(ptr); + return n + ? typedArrayToString(heapWrappers().HEAP8U, Number(ptr), Number(ptr)+n) + : (null===n ? n : ""); + }; + + /** + Given a JS string, this function returns its UTF-8 length in + bytes. Returns null if str is not a string. + */ + target.jstrlen = function(str){ + /** Attribution: derived from Emscripten's lengthBytesUTF8() */ + if('string'!==typeof str) return null; + const n = str.length; + let len = 0; + for(let i = 0; i < n; ++i){ + let u = str.charCodeAt(i); + if(u>=0xd800 && u<=0xdfff){ + u = 0x10000 + ((u & 0x3FF) << 10) | (str.charCodeAt(++i) & 0x3FF); + } + if(u<=0x7f) ++len; + else if(u<=0x7ff) len += 2; + else if(u<=0xffff) len += 3; + else len += 4; + } + return len; + }; + + /** + Encodes the given JS string as UTF8 into the given TypedArray + tgt, starting at the given offset and writing, at most, maxBytes + bytes (including the NUL terminator if addNul is true, else no + NUL is added). If it writes any bytes at all and addNul is true, + it always NUL-terminates the output, even if doing so means that + the NUL byte is all that it writes. + + If maxBytes is negative (the default) then it is treated as the + remaining length of tgt, starting at the given offset. + + If writing the last character would surpass the maxBytes count + because the character is multi-byte, that character will not be + written (as opposed to writing a truncated multi-byte character). + This can lead to it writing as many as 3 fewer bytes than + maxBytes specifies. + + Returns the number of bytes written to the target, _including_ + the NUL terminator (if any). If it returns 0, it wrote nothing at + all, which can happen if: + + - str is empty and addNul is false. + - offset < 0. + - maxBytes == 0. + - maxBytes is less than the byte length of a multi-byte str[0]. + + Throws if tgt is not an Int8Array or Uint8Array. + + Design notes: + + - In C's strcpy(), the destination pointer is the first + argument. That is not the case here primarily because the 3rd+ + arguments are all referring to the destination, so it seems to + make sense to have them grouped with it. + + - Emscripten's counterpart of this function (stringToUTF8Array()) + returns the number of bytes written sans NUL terminator. That + is, however, ambiguous: str.length===0 or maxBytes===(0 or 1) + all cause 0 to be returned. + */ + target.jstrcpy = function(jstr, tgt, offset = 0, maxBytes = -1, addNul = true){ + /** Attribution: the encoding bits are taken from Emscripten's + stringToUTF8Array(). */ + if(!tgt || (!(tgt instanceof Int8Array) && !(tgt instanceof Uint8Array))){ + toss("jstrcpy() target must be an Int8Array or Uint8Array."); + } + maxBytes = Number(maxBytes)/*tag:64bit*/; + offset = Number(offset)/*tag:64bit*/; + if(maxBytes<0) maxBytes = tgt.length - offset; + if(!(maxBytes>0) || !(offset>=0)) return 0; + let i = 0, max = jstr.length; + const begin = offset, end = offset + maxBytes - (addNul ? 1 : 0); + for(; i < max && offset < end; ++i){ + let u = jstr.charCodeAt(i); + if(u>=0xd800 && u<=0xdfff){ + u = 0x10000 + ((u & 0x3FF) << 10) | (jstr.charCodeAt(++i) & 0x3FF); + } + if(u<=0x7f){ + if(offset >= end) break; + tgt[offset++] = u; + }else if(u<=0x7ff){ + if(offset + 1 >= end) break; + tgt[offset++] = 0xC0 | (u >> 6); + tgt[offset++] = 0x80 | (u & 0x3f); + }else if(u<=0xffff){ + if(offset + 2 >= end) break; + tgt[offset++] = 0xe0 | (u >> 12); + tgt[offset++] = 0x80 | ((u >> 6) & 0x3f); + tgt[offset++] = 0x80 | (u & 0x3f); + }else{ + if(offset + 3 >= end) break; + tgt[offset++] = 0xf0 | (u >> 18); + tgt[offset++] = 0x80 | ((u >> 12) & 0x3f); + tgt[offset++] = 0x80 | ((u >> 6) & 0x3f); + tgt[offset++] = 0x80 | (u & 0x3f); + } + } + if(addNul) tgt[offset++] = 0; + return offset - begin; + }; + + /** + Works similarly to C's strncpy(), copying, at most, n bytes (not + characters) from srcPtr to tgtPtr. It copies until n bytes have + been copied or a 0 byte is reached in src. _Unlike_ strncpy(), it + returns the number of bytes it assigns in tgtPtr, _including_ the + NUL byte (if any). If n is reached before a NUL byte in srcPtr, + tgtPtr will _not_ be NULL-terminated. If a NUL byte is reached + before n bytes are copied, tgtPtr will be NUL-terminated. + + If n is negative, cstrlen(srcPtr)+1 is used to calculate it, the + +1 being for the NUL byte. + + Throws if tgtPtr or srcPtr are falsy. Results are undefined if: + + - either is not a pointer into the WASM heap or + + - srcPtr is not NUL-terminated AND n is less than srcPtr's + logical length. + + ACHTUNG: it is possible to copy partial multi-byte characters + this way, and converting such strings back to JS strings will + have undefined results. + */ + target.cstrncpy = function(tgtPtr, srcPtr, n){ + if(!tgtPtr || !srcPtr) toss("cstrncpy() does not accept NULL strings."); + if(n<0) n = target.cstrlen(strPtr)+1; + else if(!(n>0)) return 0; + const heap = target.heap8u(); + let i = 0, ch; + const tgtNumber = Number(tgtPtr), srcNumber = Number(srcPtr)/*tag:64bit*/; + for(; i < n && (ch = heap[srcNumber+i]); ++i){ + heap[tgtNumber+i] = ch; + } + if(i{ + return cache.utf8Encoder.encode(addNul ? (str+"\0") : str); + }; + + const __affirmAlloc = (obj,funcName)=>{ + if(!(obj.alloc instanceof Function) || + !(obj.dealloc instanceof Function)){ + toss("Object is missing alloc() and/or dealloc() function(s)", + "required by",funcName+"()."); + } + }; + + const __allocCStr = function(jstr, returnWithLength, allocator, funcName){ + __affirmAlloc(target, funcName); + if('string'!==typeof jstr) return null; + const u = cache.utf8Encoder.encode(jstr), + ptr = allocator(u.length+1); + let toFree = ptr; + try{ + const heap = heapWrappers().HEAP8U; + heap.set(u, Number(ptr)); + heap[__ptrAdd(ptr, u.length)] = 0; + toFree = __NullPtr; + return returnWithLength ? [ptr, u.length] : ptr; + }finally{ + if( toFree ) target.dealloc(toFree); + } + }; + + /** + Uses target.alloc() to allocate enough memory for jstrlen(jstr)+1 + bytes of memory, copies jstr to that memory using jstrcpy(), + NUL-terminates it, and returns the pointer to that C-string. + Ownership of the pointer is transfered to the caller, who must + eventually pass the pointer to dealloc() to free it. + + If passed a truthy 2nd argument then its return semantics change: + it returns [ptr,n], where ptr is the C-string's pointer and n is + its cstrlen(). + + Throws if `target.alloc` or `target.dealloc` are not functions. + */ + target.allocCString = + (jstr, returnWithLength=false)=>__allocCStr(jstr, returnWithLength, + target.alloc, 'allocCString()'); + + /** + Starts an "allocation scope." All allocations made using + scopedAlloc() are recorded in this scope and are freed when the + value returned from this function is passed to + scopedAllocPop(). + + This family of functions requires that the API's object have both + `alloc()` and `dealloc()` methods, else this function will throw. + + Intended usage: + + ``` + const scope = scopedAllocPush(); + try { + const ptr1 = scopedAlloc(100); + const ptr2 = scopedAlloc(200); + const ptr3 = scopedAlloc(300); + ... + // Note that only allocations made via scopedAlloc() + // are managed by this allocation scope. + }finally{ + scopedAllocPop(scope); + } + ``` + + The value returned by this function must be treated as opaque by + the caller, suitable _only_ for passing to scopedAllocPop(). + Its type and value are not part of this function's API and may + change in any given version of this code. + + `scopedAlloc.level` can be used to determine how many scoped + alloc levels are currently active. + */ + target.scopedAllocPush = function(){ + __affirmAlloc(target, 'scopedAllocPush'); + const a = []; + cache.scopedAlloc.push(a); + return a; + }; + + /** + Cleans up all allocations made using scopedAlloc() in the context + of the given opaque state object, which must be a value returned + by scopedAllocPush(). See that function for an example of how to + use this function. It also uninstalls any WASM functions + installed with scopedInstallFunction(). + + Though scoped allocations are managed like a stack, this API + behaves properly if allocation scopes are popped in an order + other than the order they were pushed. The intent is that it + _always_ be used in a stack-like manner. + + If called with no arguments, it pops the most recent + scopedAllocPush() result: + + ``` + scopedAllocPush(); + try{ ... } finally { scopedAllocPop(); } + ``` + + It's generally recommended that it be passed an explicit argument + to help ensure that push/push are used in matching pairs, but in + trivial code that may be a non-issue. + */ + target.scopedAllocPop = function(state){ + __affirmAlloc(target, 'scopedAllocPop'); + const n = arguments.length + ? cache.scopedAlloc.indexOf(state) + : cache.scopedAlloc.length-1; + if(n<0) toss("Invalid state object for scopedAllocPop()."); + if(0===arguments.length) state = cache.scopedAlloc[n]; + cache.scopedAlloc.splice(n,1); + for(let p; (p = state.pop()); ){ + if(target.functionEntry(p)){ + //console.warn("scopedAllocPop() uninstalling function",p); + target.uninstallFunction(p); + }else{ + target.dealloc(p); + } + } + }; + + /** + Allocates n bytes of memory using this.alloc() and records that + fact in the state for the most recent call of scopedAllocPush(). + Ownership of the memory is given to scopedAllocPop(), which + will clean it up when it is called. The memory _must not_ be + passed to this.dealloc(). Throws if this API object is missing + the required `alloc()` or `dealloc()` functions or no scoped + alloc is active. + + See scopedAllocPush() for an example of how to use this function. + + The `level` property of this function can be queried to query how + many scoped allocation levels are currently active. + + See also: scopedAllocPtr(), scopedAllocCString() + */ + target.scopedAlloc = function(n){ + if(!cache.scopedAlloc.length){ + toss("No scopedAllocPush() scope is active."); + } + const p = __asPtrType(target.alloc(n)); + return cache.scopedAlloc.pushPtr(p); + }; + + Object.defineProperty(target.scopedAlloc, 'level', { + configurable: false, enumerable: false, + get: ()=>cache.scopedAlloc.length, + set: ()=>toss("The 'active' property is read-only.") + }); + + /** + Works identically to allocCString() except that it allocates the + memory using scopedAlloc(). + + Will throw if no scopedAllocPush() call is active. + */ + target.scopedAllocCString = + (jstr, returnWithLength=false)=>__allocCStr(jstr, returnWithLength, + target.scopedAlloc, + 'scopedAllocCString()'); + + // impl for allocMainArgv() and scopedAllocMainArgv(). + const __allocMainArgv = function(isScoped, list){ + const pList = target[ + isScoped ? 'scopedAlloc' : 'alloc' + ]((list.length + 1) * target.ptr.size); + let i = 0; + list.forEach((e)=>{ + target.pokePtr(__ptrAdd(pList, target.ptr.size * i++), + target[ + isScoped ? 'scopedAllocCString' : 'allocCString' + ](""+e)); + }); + target.pokePtr(__ptrAdd(pList, target.ptr.size * i), 0); + return pList; + }; + + /** + Creates an array, using scopedAlloc(), suitable for passing to a + C-level main() routine. The input is a collection with a length + property and a forEach() method. A block of memory + (list.length+1) entries long is allocated and each pointer-sized + block of that memory is populated with a scopedAllocCString() + conversion of the (""+value) of each element, with the exception + that the final entry is a NULL pointer. Returns a pointer to the + start of the list, suitable for passing as the 2nd argument to a + C-style main() function. + + Throws if scopedAllocPush() is not active. + + Design note: the returned array is allocated with an extra NULL + pointer entry to accommodate certain APIs, but client code which + does not need that functionality should treat the returned array + as list.length entries long. + */ + target.scopedAllocMainArgv = (list)=>__allocMainArgv(true, list); + + /** + Identical to scopedAllocMainArgv() but uses alloc() instead of + scopedAlloc(). + */ + target.allocMainArgv = (list)=>__allocMainArgv(false, list); + + /** + Expects to be given a C-style string array and its length. It + returns a JS array of strings and/or nulls: any entry in the + pArgv array which is NULL results in a null entry in the result + array. If argc is 0 then an empty array is returned. + + Results are undefined if any entry in the first argc entries of + pArgv are neither 0 (NULL) nor legal UTF-format C strings. + + To be clear, the expected C-style arguments to be passed to this + function are `(int, char **)` (optionally const-qualified). + */ + target.cArgvToJs = (argc, pArgv)=>{ + const list = []; + for(let i = 0; i < argc; ++i){ + const arg = target.peekPtr(__ptrAdd(pArgv, target.ptr.size * i)); + list.push( arg ? target.cstrToJs(arg) : null ); + } + return list; + }; + + /** + Wraps function call func() in a scopedAllocPush() and + scopedAllocPop() block, such that all calls to scopedAlloc() and + friends from within that call will have their memory freed + automatically when func() returns. If func throws or propagates + an exception, the scope is still popped, otherwise it returns the + result of calling func(). + */ + target.scopedAllocCall = function(func){ + target.scopedAllocPush(); + try{ return func() } finally{ target.scopedAllocPop() } + }; + + /** Internal impl for allocPtr() and scopedAllocPtr(). */ + const __allocPtr = function(howMany, safePtrSize, method){ + __affirmAlloc(target, method); + const pIr = safePtrSize ? 'i64' : __ptrIR; + let m = target[method](howMany * (safePtrSize ? 8 : __ptrSize)); + target.poke(m, 0, pIr) + if(1===howMany){ + return m; + } + const a = [m]; + for(let i = 1; i < howMany; ++i){ + m = __ptrAdd(m, (safePtrSize ? 8 : __ptrSize)); + a[i] = m; + target.poke(m, 0, pIr); + } + return a; + }; + + /** + Allocates one or more pointers as a single chunk of memory and + zeroes them out. + + The first argument is the number of pointers to allocate. The + second specifies whether they should use a "safe" pointer size (8 + bytes) or whether they may use the default pointer size + (typically 4 but also possibly 8). + + How the result is returned depends on its first argument: if + passed 1, it returns the allocated memory address. If passed more + than one then an array of pointer addresses is returned, which + can optionally be used with "destructuring assignment" like this: + + ``` + const [p1, p2, p3] = allocPtr(3); + ``` + + ACHTUNG: when freeing the memory, pass only the _first_ result + value to dealloc(). The others are part of the same memory chunk + and must not be freed separately. + + The reason for the 2nd argument is... + + When one of the returned pointers will refer to a 64-bit value, + e.g. a double or int64, and that value must be written or fetched, + e.g. using poke() or peek(), it is important that + the pointer in question be aligned to an 8-byte boundary or else + it will not be fetched or written properly and will corrupt or + read neighboring memory. It is only safe to pass false when the + client code is certain that it will only get/fetch 4-byte values + (or smaller). + */ + target.allocPtr = + (howMany=1, safePtrSize=true)=>__allocPtr(howMany, safePtrSize, 'alloc'); + + /** + Identical to allocPtr() except that it allocates using scopedAlloc() + instead of alloc(). + */ + target.scopedAllocPtr = + (howMany=1, safePtrSize=true)=>__allocPtr(howMany, safePtrSize, 'scopedAlloc'); + + /** + If target.exports[name] exists, it is returned, else an + exception is thrown. + */ + target.xGet = function(name){ + return target.exports[name] || toss("Cannot find exported symbol:",name); + }; + + const __argcMismatch = + (f,n)=>toss(f+"() requires",n,"argument(s)."); + + /** + Looks up a WASM-exported function named fname from + target.exports. If found, it is called, passed all remaining + arguments, and its return value is returned to xCall's caller. If + not found, an exception is thrown. This function does no + conversion of argument or return types, but see xWrap() and + xCallWrapped() for variants which do. + + If the first argument is a function is is assumed to be + a WASM-bound function and is used as-is instead of looking up + the function via xGet(). + + As a special case, if passed only 1 argument after the name and + that argument in an Array, that array's entries become the + function arguments. (This is not an ambiguous case because it's + not legal to pass an Array object to a WASM function.) + */ + target.xCall = function(fname, ...args){ + const f = (fname instanceof Function) ? fname : target.xGet(fname); + if(!(f instanceof Function)) toss("Exported symbol",fname,"is not a function."); + if(f.length!==args.length){ + __argcMismatch(((f===fname) ? f.name : fname),f.length) + /* This is arguably over-pedantic but we want to help clients keep + from shooting themselves in the foot when calling C APIs. */; + } + return (2===arguments.length && Array.isArray(arguments[1])) + ? f.apply(null, arguments[1]) + : f.apply(null, args); + }; + + /** + State for use with xWrap(). + */ + cache.xWrap = Object.create(null); + cache.xWrap.convert = Object.create(null); + /** Map of type names to argument conversion functions. */ + cache.xWrap.convert.arg = new Map; + /** Map of type names to return result conversion functions. */ + cache.xWrap.convert.result = new Map; + /** Scope-local convenience aliases. */ + const xArg = cache.xWrap.convert.arg, xResult = cache.xWrap.convert.result; + + const __xArgPtr = __asPtrType; + xArg + .set('i64', __BigInt) + .set('i32', (i)=>i|0) + .set('i16', (i)=>((i | 0) & 0xFFFF)) + .set('i8', (i)=>((i | 0) & 0xFF)) + .set('f32', (i)=>Number(i).valueOf()) + .set('float', xArg.get('f32')) + .set('f64', xArg.get('f32')) + .set('double', xArg.get('f64')) + .set('int', xArg.get('i32')) + .set('null', (i)=>i) + .set(null, xArg.get('null')) + .set('**', __xArgPtr) + .set('*', __xArgPtr) + ; + xResult.set('*', __xArgPtr) + .set('pointer', __xArgPtr) + .set('number', (v)=>Number(v)) + .set('void', (v)=>undefined) + .set(undefined, xResult.get('void')) + .set('null', (v)=>v) + .set(null, xResult.get('null')) + ; + + /* Copy xArg[...] handlers to xResult[...] for cases which have + identical semantics. Also add pointer-style variants of those + xArg entries to both xArg and xResult. */ + for(const t of [ + 'i8', 'i16', 'i32', 'i64', 'int', + 'f32', 'float', 'f64', 'double' + ]){ + xArg.set(t+'*', __xArgPtr); + xResult.set(t+'*', __xArgPtr); + xResult.set( + t, xArg.get(t) + || toss("Maintenance required: missing arg converter for",t) + ); + } + + /** + In order for args of type string to work in various contexts in + the sqlite3 API, we need to pass them on as, variably, a C-string + or a pointer value. Thus for ARGs of type 'string' and + '*'/'pointer' we behave differently depending on whether the + argument is a string or not: + + - If v is a string, scopeAlloc() a new C-string from it and return + that temp string's pointer. + + - Else return the value from the arg adapter defined for + target.ptr.ir. + + TODO? Permit an Int8Array/Uint8Array and convert it to a string? + Would that be too much magic concentrated in one place, ready to + backfire? We handle that at the client level in sqlite3 with a + custom argument converter. + */ + const __xArgString = (v)=>{ + return ('string'===typeof v) + ? target.scopedAllocCString(v) + : __asPtrType(v); + }; + + xArg.set('string', __xArgString) + .set('utf8', __xArgString) + // (much later: why did we do this?) .set('pointer', __xArgString) + ; + + xResult + .set('string', (i)=>target.cstrToJs(i)) + .set('utf8', xResult.get('string')) + .set('string:dealloc', (i)=>{ + try { return i ? target.cstrToJs(i) : null } + finally{ target.dealloc(i) } + }) + .set('utf8:dealloc', xResult.get('string:dealloc')) + .set('json', (i)=>JSON.parse(target.cstrToJs(i))) + .set('json:dealloc', (i)=>{ + try{ return i ? JSON.parse(target.cstrToJs(i)) : null } + finally{ target.dealloc(i) } + }); + + /** + Internal-use-only base class for FuncPtrAdapter and potentially + additional stateful argument adapter classes. + + Its main interface (convertArg()) is strictly internal, not to be + exposed to client code, as it may still need re-shaping. Only the + constructors of concrete subclasses should be exposed to clients, + and those in such a way that does not hinder internal redesign of + the convertArg() interface. + */ + const AbstractArgAdapter = class { + constructor(opt){ + this.name = opt.name || 'unnamed adapter'; + } + /** + Gets called via xWrap() to "convert" v to whatever type + this specific class supports. + + argIndex is the argv index of _this_ argument in the + being-xWrap()'d call. argv is the current argument list + undergoing xWrap() argument conversion. argv entries to the + left of argIndex will have already undergone transformation and + those to the right will not have (they will have the values the + client-level code passed in, awaiting conversion). The RHS + indexes must never be relied upon for anything because their + types are indeterminate, whereas the LHS values will be + WASM-compatible values by the time this is called. + + The reason for the argv and argIndex arguments is that we + frequently need more context than v for a specific conversion, + and that context invariably lies in the LHS arguments of v. + Examples of how this is useful can be found in FuncPtrAdapter. + */ + convertArg(v,argv,argIndex){ + toss("AbstractArgAdapter must be subclassed."); + } + }; + + /** + This type is recognized by xWrap() as a proxy for converting a JS + function to a C-side function, either permanently, for the + duration of a single call into the C layer, or semi-contextual, + where it may keep track of a single binding for a given context + and uninstall the binding if it's replaced. + + The constructor requires an options object with these properties: + + - name (optional): string describing the function binding. This + is solely for debugging and error-reporting purposes. If not + provided, an empty string is assumed. + + - signature: a function signature string compatible with + jsFuncToWasm(). + + - bindScope (string): one of ('transient', 'context', + 'singleton', 'permanent'). Bind scopes are: + + - 'transient': it will convert JS functions to WASM only for + the duration of the xWrap()'d function call, using + scopedInstallFunction(). Before that call returns, the + WASM-side binding will be uninstalled. + + - 'singleton': holds one function-pointer binding for this + instance. If it's called with a different function pointer, + it uninstalls the previous one after converting the new + value. This is only useful for use with "global" functions + which do not rely on any state other than this function + pointer. If the being-converted function pointer is intended + to be mapped to some sort of state object (e.g. an + `sqlite3*`) then "context" (see below) is the proper mode. + + - 'context': similar to singleton mode but for a given + "context", where the context is a key provided by the user + and possibly dependent on a small amount of call-time + context. This mode is the default if bindScope is _not_ set + but a property named contextKey (described below) is. + + - 'permanent': the function is installed and left there + forever. There is no way to recover its pointer address + later on for cleanup purposes. i.e. it effectively leaks. + + - callProxy (function): if set, this must be a function which + will act as a proxy for any "converted" JS function. It is + passed the being-converted function value and must return + either that function or a function which acts on its + behalf. The returned function will be the one which gets + installed into the WASM function table. The proxy must perform + any required argument conversion (it will be called from C + code, so will receive C-format arguments) before passing them + on to the being-converted function. Whether or not the proxy + itself must return a value depends on the context. If it does, + it must be a WASM-friendly value, as it will be returning from + a call made from WASM code. + + - contextKey (function): is only used if bindScope is 'context' + or if bindScope is not set and this function is, in which case + a bindScope of 'context' is assumed. This function gets bound + to this object, so its "this" is this object. It gets passed + (argv,argIndex), where argIndex is the index of _this_ function + in its _wrapping_ function's arguments, and argv is the + _current_ still-being-xWrap()-processed args array. (Got all + that?) When thisFunc(argv,argIndex) is called by xWrap(), all + arguments in argv to the left of argIndex will have been + processed by xWrap() by the time this is called. argv[argIndex] + will be the value the user passed in to the xWrap()'d function + for the argument this FuncPtrAdapter is mapped to. Arguments to + the right of argv[argIndex] will not yet have been converted + before this is called. The function must return a key which + uniquely identifies this function mapping context for _this_ + FuncPtrAdapter instance (other instances are not considered), + taking into account that C functions often take some sort of + state object as one or more of their arguments. As an example, + if the xWrap()'d function takes `(int,T*,functionPtr,X*)` then + this FuncPtrAdapter instance is argv[2], and contextKey(argv,2) + might return 'T@'+argv[1], or even just argv[1]. Note, + however, that the (`X*`) argument will not yet have been + processed by the time this is called and should not be used as + part of that key because its pre-conversion data type might be + unpredictable. Similarly, care must be taken with C-string-type + arguments: those to the left in argv will, when this is called, + be WASM pointers, whereas those to the right might (and likely + do) have another data type. When using C-strings in keys, never + use their pointers in the key because most C-strings in this + constellation are transient. Conversely, the pointer address + makes an ideal key for longer-lived native pointer types. + + Yes, that ^^^ is quite awkward, but it's what we have. In + context, as it were, it actually makes some sense, but one must + look under its hook a bit to understand why it's shaped the + way it is. + + The constructor only saves the above state for later, and does + not actually bind any functions. The conversion, if any, is + performed when its convertArg() method is called via xWrap(). + + Shortcomings: + + - These "reverse" bindings, i.e. calling into a JS-defined + function from a WASM-defined function (the generated proxy + wrapper), lack all type conversion support. That means, for + example, that... + + - Function pointers which include C-string arguments may still + need a level of hand-written wrappers around them, depending on + how they're used, in order to provide the client with JS + strings. Alternately, clients will need to perform such + conversions on their own, e.g. using cstrToJs(). The purpose of + the callProxy() method is to account for such cases. + */ + xArg.FuncPtrAdapter = class FuncPtrAdapter extends AbstractArgAdapter { + constructor(opt) { + super(opt); + if(xArg.FuncPtrAdapter.warnOnUse){ + console.warn('xArg.FuncPtrAdapter is an internal-only API', + 'and is not intended to be invoked from', + 'client-level code. Invoked with:',opt); + } + this.name = opt.name || "unnamed"; + this.signature = opt.signature; + if(opt.contextKey instanceof Function){ + this.contextKey = opt.contextKey; + if(!opt.bindScope) opt.bindScope = 'context'; + } + this.bindScope = opt.bindScope + || toss("FuncPtrAdapter options requires a bindScope (explicit or implied)."); + if(FuncPtrAdapter.bindScopes.indexOf(opt.bindScope)<0){ + toss("Invalid options.bindScope ("+opt.bindMod+") for FuncPtrAdapter. "+ + "Expecting one of: ("+FuncPtrAdapter.bindScopes.join(', ')+')'); + } + this.isTransient = 'transient'===this.bindScope; + this.isContext = 'context'===this.bindScope; + this.isPermanent = 'permanent'===this.bindScope; + this.singleton = ('singleton'===this.bindScope) ? [] : undefined; + //console.warn("FuncPtrAdapter()",opt,this); + this.callProxy = (opt.callProxy instanceof Function) + ? opt.callProxy : undefined; + } + + /** + The static class members are defined outside of the class to + work around an emcc toolchain build problem: one of the tools + in emsdk v3.1.42 does not support the static keyword. + */ + + /* Dummy impl. Overwritten per-instance as needed. */ + contextKey(argv,argIndex){ + return this; + } + + /** + Returns this object's mapping for the given context key, in the + form of an an array, creating the mapping if needed. The key + may be anything suitable for use in a Map. + + The returned array is intended to be used as a pair of + [JSValue, WasmFuncPtr], where the first element is one passed + to this.convertArg() and the second is its WASM form. + */ + contextMap(key){ + const cm = (this.__cmap || (this.__cmap = new Map)); + let rc = cm.get(key); + if(undefined===rc) cm.set(key, (rc = [])); + return rc; + } + + /** + Gets called via xWrap() to "convert" v to a WASM-bound function + pointer. If v is one of (a WASM pointer, null, undefined) then + (v||0) is returned and any earlier function installed by this + mapping _might_, depending on how it's bound, be uninstalled. + If v is not one of those types, it must be a Function, for + which this method creates (if needed) a WASM function binding + and returns the WASM pointer to that binding. + + If this instance is not in 'transient' mode, it will remember + the binding for at least the next call, to avoid recreating the + function binding unnecessarily. + + If it's passed a pointer(ish) value for v, it assumes it's a + WASM function pointer and does _not_ perform any function + binding, so this object's bindMode is irrelevant/ignored for + such cases. + + See the parent class's convertArg() docs for details on what + exactly the 2nd and 3rd arguments are. + */ + convertArg(v,argv,argIndex){ + let pair = this.singleton; + if(!pair && this.isContext){ + pair = this.contextMap(this.contextKey(argv,argIndex)); + //FuncPtrAdapter.debugOut(this.name, this.signature, "contextKey() =",this.contextKey(argv,argIndex), pair); + } + if( 0 ){ + FuncPtrAdapter.debugOut("FuncPtrAdapter.convertArg()",this.name, + 'signature =',this.signature, + 'transient ?=',this.transient, + 'pair =',pair, + 'v =',v); + } + if(pair && 2===pair.length && pair[0]===v){ + /* We have already handled this function. */ + return pair[1]; + } + if(v instanceof Function){ + /* Install a WASM binding and return its pointer. */ + //FuncPtrAdapter.debugOut("FuncPtrAdapter.convertArg()",this.name,this.signature,this.transient,v,pair); + if(this.callProxy){ + v = this.callProxy(v); + } + const fp = __installFunction(v, this.signature, this.isTransient); + if(FuncPtrAdapter.debugFuncInstall){ + FuncPtrAdapter.debugOut("FuncPtrAdapter installed", this, + this.contextKey(argv,argIndex), '@'+fp, v); + } + if(pair){ + /* Replace existing stashed mapping */ + if(pair[1]){ + if(FuncPtrAdapter.debugFuncInstall){ + FuncPtrAdapter.debugOut("FuncPtrAdapter uninstalling", this, + this.contextKey(argv,argIndex), '@'+pair[1], v); + } + try{ + /* Because the pending native call might rely on the + pointer we're replacing, e.g. as is normally the case + with sqlite3's xDestroy() methods, we don't + immediately uninstall but instead add its pointer to + the scopedAlloc stack, which will be cleared when the + xWrap() mechanism is done calling the native + function. We're relying very much here on xWrap() + having pushed an alloc scope. + */ + cache.scopedAlloc.pushPtr(pair[1]); + } + catch(e){/*ignored*/} + } + pair[0] = arguments[0] || __NullPtr/*the original v*/; + pair[1] = fp; + } + return fp; + }else if(target.isPtr(v) || null===v || undefined===v){ + if(pair && pair[1] && pair[1]!==v){ + /* uninstall stashed mapping and replace stashed mapping with v. */ + if(FuncPtrAdapter.debugFuncInstall){ + FuncPtrAdapter.debugOut("FuncPtrAdapter uninstalling", this, + this.contextKey(argv,argIndex), '@'+pair[1], v); + } + try{cache.scopedAlloc.pushPtr(pair[1]);/*see notes above*/} + catch(e){/*ignored*/} + pair[0] = pair[1] = (v || __NullPtr); + } + return v || __NullPtr; + }else{ + throw new TypeError("Invalid FuncPtrAdapter argument type. "+ + "Expecting a function pointer or a "+ + (this.name ? this.name+' ' : '')+ + "function matching signature "+ + this.signature+"."); + } + }/*convertArg()*/ + }/*FuncPtrAdapter*/; + + /** If true, the constructor emits a warning. The intent is that + this be set to true after bootstrapping of the higher-level + client library is complete, to warn downstream clients that + they shouldn't be relying on this implementation detail which + does not have a stable interface. */ + xArg.FuncPtrAdapter.warnOnUse = false; + + /** If true, convertArg() will call FuncPtrAdapter.debugOut() when + it (un)installs a function binding to/from WASM. Note that + deinstallation of bindScope=transient bindings happens via + scopedAllocPop() so will not be output. */ + xArg.FuncPtrAdapter.debugFuncInstall = false; + + /** Function used for debug output. */ + xArg.FuncPtrAdapter.debugOut = console.debug.bind(console); + + /** + List of legal values for the FuncPtrAdapter bindScope config + option. + */ + xArg.FuncPtrAdapter.bindScopes = [ + 'transient', 'context', 'singleton', 'permanent' + ]; + + /** Throws if xArg.get(t) returns falsy. */ + const __xArgAdapterCheck = + (t)=>xArg.get(t) || toss("Argument adapter not found:",t); + + /** Throws if xResult.get(t) returns falsy. */ + const __xResultAdapterCheck = + (t)=>xResult.get(t) || toss("Result adapter not found:",t); + + /** + Fetches the xWrap() argument adapter mapped to t, calls it, + passing in all remaining arguments, and returns the result. + Throws if t is not mapped to an argument converter. + */ + cache.xWrap.convertArg = (t,...args)=>__xArgAdapterCheck(t)(...args); + /** + Identical to convertArg() except that it does not perform + an is-defined check on the mapping to t before invoking it. + */ + cache.xWrap.convertArgNoCheck = (t,...args)=>xArg.get(t)(...args); + + /** + Fetches the xWrap() result adapter mapped to t, calls it, passing + it v, and returns the result. Throws if t is not mapped to an + argument converter. + */ + cache.xWrap.convertResult = + (t,v)=>(null===t ? v : (t ? __xResultAdapterCheck(t)(v) : undefined)); + /** + Identical to convertResult() except that it does not perform an + is-defined check on the mapping to t before invoking it. + */ + cache.xWrap.convertResultNoCheck = + (t,v)=>(null===t ? v : (t ? xResult.get(t)(v) : undefined)); + + /** + Creates a wrapper for another function which converts the arguments + of the wrapper to argument types accepted by the wrapped function, + then converts the wrapped function's result to another form + for the wrapper. + + The first argument must be one of: + + - A JavaScript function. + - The name of a WASM-exported function. xGet() is used to fetch + the exported function, which throws if it's not found. + - A pointer into the indirect function table. e.g. a pointer + returned from target.installFunction(). + + It returns either the passed-in function or a wrapper for that + function which converts the JS-side argument types into WASM-side + types and converts the result type. + + The second argument, `resultType`, describes the conversion for + the wrapped functions result. A literal `null` or the string + `'null'` both mean to return the original function's value as-is + (mnemonic: there is "null" conversion going on). Literal + `undefined` or the string `"void"` both mean to ignore the + function's result and return `undefined`. Aside from those two + special cases, the `resultType` value may be one of the values + described below or any mapping installed by the client using + xWrap.resultAdapter(). + + If passed 3 arguments and the final one is an array, that array + must contain a list of type names (see below) for adapting the + arguments from JS to WASM. If passed 2 arguments, more than 3, + or the 3rd is not an array, all arguments after the 2nd (if any) + are treated as type names. i.e.: + + ``` + xWrap('funcname', 'i32', 'string', 'f64'); + // is equivalent to: + xWrap('funcname', 'i32', ['string', 'f64']); + ``` + + This function enforces that the given list of arguments has the + same arity as the being-wrapped function (as defined by its + `length` property) and it will throw if that is not the case. + Similarly, the created wrapper will throw if passed a differing + argument count. The intent of that strictness is to help catch + coding errors in using JS-bound WASM functions earlier rather + than laer. + + Type names are symbolic names which map the arguments to an + adapter function to convert, if needed, the value before passing + it on to WASM or to convert a return result from WASM. The list + of pre-defined names: + + - `i8`, `i16`, `i32` (args and results): all integer conversions + which convert their argument to an integer and truncate it to + the given bit length. + + - `*`, `**`, and `pointer` (args): are assumed to be WASM pointer + values and are returned coerced to an appropriately-sized + pointer value (i32 or i64). Non-numeric values will coerce to 0 + and out-of-range values will have undefined results (just as + with any pointer misuse). + + - `*` and `pointer` (results): aliases for the current + WASM pointer numeric type. + + - `**` (args): is simply a descriptive alias for the WASM pointer + type. It's primarily intended to mark output-pointer arguments, + noting that JS's view of WASM does not distinguish between + pointers and pointers-to-pointers, so all such interpretation + of `**`, as distinct from `*`, necessarily happens at the + client level. + + - `NumType*` (args): a type name in this form, where T is + the name of a numeric mapping, e.g. 'int16' or 'double', + is treated like `*`. + + - `i64` (args and results): passes the value to BigInt() to + convert it to an int64. This conversion will if bigIntEnabled + is falsy. + + - `f32` (`float`), `f64` (`double`) (args and results): pass + their argument to Number(). i.e. the adapter does not currently + distinguish between the two types of floating-point numbers. + + - `number` (results): converts the result to a JS Number using + Number(theValue). This is for result conversions only, as it's + not possible to generically know which type of number to + convert arguments to. + + Non-numeric conversions include: + + - `null` literal or `"null"` string (args and results): perform + no translation and pass the arg on as-is. This is primarily + useful for results but may have a use or two for arguments. + + - `string` or `utf8` (args): has two different semantics in order + to accommodate various uses of certain C APIs + (e.g. output-style strings)... + + - If the arg is a JS string, it creates a _temporary_ + UTF-8-encoded C-string to pass to the exported function, + cleaning it up before the wrapper returns. If a long-lived + C-string pointer is required, that requires client-side code + to create the string then pass its pointer to the function. + + - Else the arg is assumed to be a pointer to a string the + client has already allocated and it's passed on as + a WASM pointer. + + - `string` or `utf8` (results): treats the result value as a + const C-string, encoded as UTF-8, copies it to a JS string, + and returns that JS string. + + - `string:dealloc` or `utf8:dealloc` (results): treats the result + value as a non-const UTF-8 C-string, ownership of which has + just been transfered to the caller. It copies the C-string to a + JS string, frees the C-string using dealloc(), and returns the + JS string. If such a result value is NULL, the JS result is + `null`. Achtung: when using an API which returns results from a + specific allocator, e.g. `my_malloc()`, this conversion _is not + legal_. Instead, an equivalent conversion which uses the + appropriate deallocator is required. For example: + +```js + target.xWrap.resultAdapter('string:my_free',(i)=>{ + try { return i ? target.cstrToJs(i) : null; } + finally{ target.exports.my_free(i); } + }; +``` + + - `json` (results): treats the result as a const C-string and + returns the result of passing the converted-to-JS string to + JSON.parse(). Returns `null` if the C-string is a NULL pointer. + + - `json:dealloc` (results): works exactly like `string:dealloc` but + returns the same thing as the `json` adapter. Note the + warning in `string:dealloc` regarding matching allocators and + deallocators. + + The type names for results and arguments are validated when + xWrap() is called and any unknown names will trigger an + exception. + + Clients may map their own result and argument adapters using + xWrap.resultAdapter() and xWrap.argAdapter(), noting that not all + type conversions are valid for both arguments _and_ result types + as they often have different memory ownership requirements. + + Design note: the ability to pass in a JS function as the first + argument is of relatively limited use, primarily for testing + argument and result converters. JS functions, by and large, will + not want to deal with C-type arguments. + + TODOs: + + - Figure out how/whether we can (semi-)transparently handle + pointer-type _output_ arguments. Those currently require + explicit handling by allocating pointers, assigning them before + the call using poke(), and fetching them with + peek() after the call. We may be able to automate some + or all of that. + + - Figure out whether it makes sense to extend the arg adapter + interface such that each arg adapter gets an array containing + the results of the previous arguments in the current call. That + might allow some interesting type-conversion feature. Use case: + handling of the final argument to sqlite3_prepare_v2() depends + on the type (pointer vs JS string) of its 2nd + argument. Currently that distinction requires hand-writing a + wrapper for that function. That case is unusual enough that + abstracting it into this API (and taking on the associated + costs) may well not make good sense. + */ + target.xWrap = function callee(fArg, resultType, ...argTypes){ + if(3===arguments.length && Array.isArray(arguments[2])){ + argTypes = arguments[2]; + } + if(target.isPtr(fArg)){ + fArg = target.functionEntry(fArg) + || toss("Function pointer not found in WASM function table."); + } + const fIsFunc = (fArg instanceof Function); + const xf = fIsFunc ? fArg : target.xGet(fArg); + if(fIsFunc) fArg = xf.name || 'unnamed function'; + if(argTypes.length!==xf.length) __argcMismatch(fArg, xf.length); + if( 0===xf.length + && (null===resultType || 'null'===resultType) ){ + /* Func taking no args with an as-is return. We don't need a wrapper. */ + return xf; + } + /*Verify the arg type conversions are valid...*/; + __xResultAdapterCheck(resultType); + for(const t of argTypes){ + if(t instanceof AbstractArgAdapter) xArg.set(t, (...args)=>t.convertArg(...args)); + else __xArgAdapterCheck(t); + } + const cxw = cache.xWrap; + if(0===xf.length){ + // No args to convert, so we can create a simpler wrapper... + return (...args)=>(args.length + ? __argcMismatch(fArg, xf.length) + : cxw.convertResult(resultType, xf.call(null))); + } + return function(...args){ + if(args.length!==xf.length) __argcMismatch(fArg, xf.length); + const scope = target.scopedAllocPush(); + try{ + /* + Maintenance reminder re. arguments passed to convertArg(): + The public interface of argument adapters is that they take + ONE argument and return a (possibly) converted result for + it. The passing-on of arguments after the first is an + internal implementation detail for the sake of + AbstractArgAdapter, and not to be relied on or documented + for other cases. The fact that this is how + AbstractArgAdapter.convertArgs() gets its 2nd+ arguments, + and how FuncPtrAdapter.contextKey() gets its args, is also + an implementation detail and subject to change. i.e. the + public interface of 1 argument is stable. The fact that any + arguments may be passed in after that one, and what those + arguments are, is _not_ part of the public interface and is + _not_ stable. + + Maintenance reminder: the Ember framework modifies the core + Array type, breaking for-in loops: + + https://sqlite.org/forum/forumpost/b549992634b55104 + */ + let i = 0; + if( callee.debug ){ + console.debug("xWrap() preparing: resultType ",resultType, 'xf',xf,"argTypes",argTypes,"args",args); + } + for(; i < args.length; ++i) args[i] = cxw.convertArgNoCheck( + argTypes[i], args[i], args, i + ); + if( callee.debug ){ + console.debug("xWrap() calling: resultType ",resultType, 'xf',xf,"argTypes",argTypes,"args",args); + } + return cxw.convertResultNoCheck(resultType, xf.apply(null,args)); + }finally{ + target.scopedAllocPop(scope); + } + }; + }/*xWrap()*/; + + /** + Internal impl for xWrap.resultAdapter() and argAdapter(). + + func = one of xWrap.resultAdapter or xWrap.argAdapter. + + argc = the number of args in the wrapping call to this + function. + + typeName = the first arg to the wrapping function. + + adapter = the second arg to the wrapping function. + + modeName = a descriptive name of the wrapping function for + error-reporting purposes. + + xcvPart = one of xResult or xArg. + + This acts as either a getter (if 1===argc) or setter (if + 2===argc) for the given adapter. Returns func on success or + throws if (A) called with 2 args but adapter is-not-a Function or + (B) typeName is not a string or (C) argc is not one of (1, 2). + */ + const __xAdapter = function(func, argc, typeName, adapter, modeName, xcvPart){ + if('string'===typeof typeName){ + if(1===argc) return xcvPart.get(typeName); + else if(2===argc){ + if(!adapter){ + xcvPart.delete(typeName); + return func; + }else if(!(adapter instanceof Function)){ + toss(modeName,"requires a function argument."); + } + xcvPart.set(typeName, adapter); + return func; + } + } + toss("Invalid arguments to",modeName); + }; + + /** + Gets, sets, or removes a result value adapter for use with + xWrap(). If passed only 1 argument, the adapter function for the + given type name is returned. If the second argument is explicit + falsy (as opposed to defaulted), the adapter named by the first + argument is removed. If the 2nd argument is not falsy, it must be + a function which takes one value and returns a value appropriate + for the given type name. The adapter may throw if its argument is + not of a type it can work with. This function throws for invalid + arguments. + + Example: + + ``` + xWrap.resultAdapter('twice',(v)=>v+v); + ``` + + Result adapters MUST NOT use the scopedAlloc() family of APIs to + allocate a result value. xWrap()-generated wrappers run in the + context of scopedAllocPush() so that argument adapters can easily + convert, e.g., to C-strings, and have them cleaned up + automatically before the wrapper returns to the caller. Likewise, + if a _result_ adapter uses scoped allocation, the result will be + freed before the wrapper returns, leading to chaos and undefined + behavior. + + When called as a setter, this function returns itself. + */ + target.xWrap.resultAdapter = function f(typeName, adapter){ + return __xAdapter(f, arguments.length, typeName, adapter, + 'resultAdapter()', xResult); + }; + + /** + Functions identically to xWrap.resultAdapter() but applies to + call argument conversions instead of result value conversions. + + xWrap()-generated wrappers perform argument conversion in the + context of a scopedAllocPush(), so any memory allocation + performed by argument adapters really, really, really should be + made using the scopedAlloc() family of functions unless + specifically necessary. For example: + + ``` + xWrap.argAdapter('my-string', function(v){ + return ('string'===typeof v) + ? myWasmObj.scopedAllocCString(v) : null; + }; + ``` + + Contrariwise, _result_ adapters _must not_ use scopedAlloc() to + allocate results because they would be freed before the + xWrap()-created wrapper returns. + + It is perfectly legitimate to use these adapters to perform + argument validation, as opposed (or in addition) to conversion. + When used that way, they should throw for invalid arguments. + */ + target.xWrap.argAdapter = function f(typeName, adapter){ + return __xAdapter(f, arguments.length, typeName, adapter, + 'argAdapter()', xArg); + }; + + target.xWrap.FuncPtrAdapter = xArg.FuncPtrAdapter; + + /** + Functions like xCall() but performs argument and result type + conversions as for xWrap(). The first, second, and third + arguments are as documented for xWrap(), except that the 3rd + argument may be either a falsy value or empty array to represent + nullary functions. The 4th+ arguments are arguments for the call, + with the special case that if the 4th argument is an array, it is + used as the arguments for the call. Returns the converted result + of the call. + + This is just a thin wrapper around xWrap(). If the given function + is to be called more than once, it's more efficient to use + xWrap() to create a wrapper, then to call that wrapper as many + times as needed. For one-shot calls, however, this variant is + simpler. + */ + target.xCallWrapped = function(fArg, resultType, argTypes, ...args){ + if(Array.isArray(arguments[3])) args = arguments[3]; + return target.xWrap(fArg, resultType, argTypes||[]).apply(null, args||[]); + }; + + /** + This function is ONLY exposed in the public API to facilitate + testing. It should not be used in application-level code, only + in test code. + + Expects to be given (typeName, value) and returns a conversion + of that value as has been registered using argAdapter(). + It throws if no adapter is found. + + ACHTUNG: the adapter may require that a scopedAllocPush() is + active and it may allocate memory within that scope. It may also + require additional arguments, depending on the type of + conversion. + */ + target.xWrap.testConvertArg = cache.xWrap.convertArg; + + /** + This function is ONLY exposed in the public API to facilitate + testing. It should not be used in application-level code, only + in test code. + + Expects to be given (typeName, value) and returns a conversion + of that value as has been registered using resultAdapter(). + It throws if no adapter is found. + + ACHTUNG: the adapter may allocate memory which the caller may need + to know how to free. + */ + target.xWrap.testConvertResult = cache.xWrap.convertResult; + + return target; +}; + +/** + yawl (Yet Another Wasm Loader) provides very basic wasm loader. + It requires a config object: + + - `uri`: required URI of the WASM file to load. + + - `onload(loadResult)`: optional callback. Its argument is an + object described in more detail below. + + - `imports`: optional imports object for + WebAssembly.instantiate[Streaming](). The default is an empty + set of imports. If the module requires any imports, this object + must include them. + + - `wasmUtilTarget`: optional object suitable for passing to + WhWasmUtilInstaller(). If set, it gets passed to that function + before the returned promise resolves. This function sets several + properties on it before passing it on to that function (which + sets many more): + + - `module`, `instance`: the properties from the + instantiate[Streaming]() result. + + - If `instance.exports.memory` is _not_ set then it requires that + `config.imports.env.memory` be set (else it throws), and + assigns that to `wasmUtilTarget.memory`. + + - If `wasmUtilTarget.alloc` is not set and + `instance.exports.malloc` is, it installs + `wasmUtilTarget.alloc()` and `wasmUtilTarget.dealloc()` + wrappers for the exports `malloc` and `free` functions. + + It returns a function which, when called, initiates loading of the + module and returns a Promise. When that Promise resolves, it calls + the `config.onload` callback (if set) and passes it `(loadResult)`, + where `loadResult` is derived from the result of + WebAssembly.instantiate[Streaming](), an object in the form: + + ``` + { + module: a WebAssembly.Module, + instance: a WebAssembly.Instance, + config: the config arg to this function + } + ``` + + (The initial `then()` attached to the promise gets only that + object, and not the `config` object, thus the potential need for a + `config.onload` handler.) + + Error handling is up to the caller, who may attach a `catch()` call + to the promise. +*/ +globalThis.WhWasmUtilInstaller.yawl = function(config){ + const wfetch = ()=>fetch(config.uri, {credentials: 'same-origin'}); + const wui = this; + const finalThen = function(arg){ + //log("finalThen()",arg); + if(config.wasmUtilTarget){ + const toss = (...args)=>{throw new Error(args.join(' '))}; + const tgt = config.wasmUtilTarget; + tgt.module = arg.module; + tgt.instance = arg.instance; + //tgt.exports = tgt.instance.exports; + if(!tgt.instance.exports.memory){ + /** + WhWasmUtilInstaller requires either tgt.exports.memory + (exported from WASM) or tgt.memory (JS-provided memory + imported into WASM). + */ + tgt.memory = config?.imports?.env?.memory + || toss("Missing 'memory' object!"); + } + if(!tgt.alloc && arg.instance.exports.malloc){ + const exports = arg.instance.exports; + tgt.alloc = function(n){ + return exports.malloc(n) || toss("Allocation of",n,"bytes failed."); + }; + tgt.dealloc = function(m){exports.free(m)}; + } + wui(tgt); + } + arg.config = config; + if(config.onload) config.onload(arg); + return arg /* for any then() handler attached to + yetAnotherWasmLoader()'s return value */; + }; + const loadWasm = WebAssembly.instantiateStreaming + ? ()=>WebAssembly + .instantiateStreaming(wfetch(), config.imports||{}) + .then(finalThen) + : ()=> wfetch()// Safari < v15 + .then(response => response.arrayBuffer()) + .then(bytes => WebAssembly.instantiate(bytes, config.imports||{})) + .then(finalThen) + ; + return loadWasm; +}.bind(globalThis.WhWasmUtilInstaller)/*yawl()*/; diff --git a/ext/wasm/config.make.in b/ext/wasm/config.make.in new file mode 100644 index 0000000000..4c8d7893b3 --- /dev/null +++ b/ext/wasm/config.make.in @@ -0,0 +1,16 @@ +# config.make.in gets filtered by the top-most configure script to +# create config.make. +bin.bash = @BIN_BASH@ +bin.emcc = @EMCC_WRAPPER@ +bin.wasm-strip = @BIN_WASM_STRIP@ +bin.wasm-opt = @BIN_WASM_OPT@ + +SHELL = $(bin.bash) + +# The following overrides can be uncommented to test various +# validation and if/else branches the makefile code: +# +#bin.bash = +#bin.emcc = +#bin.wasm-strip = +#bin.wasm-opt = diff --git a/ext/wasm/demo-123-worker.html b/ext/wasm/demo-123-worker.html new file mode 100644 index 0000000000..692203d71e --- /dev/null +++ b/ext/wasm/demo-123-worker.html @@ -0,0 +1,44 @@ + + + + + + + Hello, sqlite3 + + + +

    1-2-sqlite3 worker demo

    + + + diff --git a/ext/wasm/demo-123.html b/ext/wasm/demo-123.html new file mode 100644 index 0000000000..2046b076d4 --- /dev/null +++ b/ext/wasm/demo-123.html @@ -0,0 +1,24 @@ + + + + + + + Hello, sqlite3 + + + +

    1-2-sqlite3 demo

    + + + + diff --git a/ext/wasm/demo-123.js b/ext/wasm/demo-123.js new file mode 100644 index 0000000000..9f90ca7568 --- /dev/null +++ b/ext/wasm/demo-123.js @@ -0,0 +1,290 @@ +/* + 2022-09-19 + + The author disclaims copyright to this source code. In place of a + legal notice, here is a blessing: + + * May you do good and not evil. + * May you find forgiveness for yourself and forgive others. + * May you share freely, never taking more than you give. + + *********************************************************************** + + A basic demonstration of the SQLite3 "OO#1" API. +*/ +'use strict'; +(function(){ + /** + Set up our output channel differently depending + on whether we are running in a worker thread or + the main (UI) thread. + */ + let logHtml; + if(globalThis.window === globalThis /* UI thread */){ + console.log("Running demo from main UI thread."); + logHtml = function(cssClass,...args){ + const ln = document.createElement('div'); + if(cssClass) ln.classList.add(cssClass); + ln.append(document.createTextNode(args.join(' '))); + document.body.append(ln); + }; + }else{ /* Worker thread */ + console.log("Running demo from Worker thread."); + logHtml = function(cssClass,...args){ + postMessage({ + type:'log', + payload:{cssClass, args} + }); + }; + } + const log = (...args)=>logHtml('',...args); + const warn = (...args)=>logHtml('warning',...args); + const error = (...args)=>logHtml('error',...args); + + const demo1 = function(sqlite3){ + const capi = sqlite3.capi/*C-style API*/, + oo = sqlite3.oo1/*high-level OO API*/; + log("sqlite3 version",capi.sqlite3_libversion(), capi.sqlite3_sourceid()); + const db = new oo.DB("/mydb.sqlite3",'ct'); + log("transient db =",db.filename); + /** + Never(!) rely on garbage collection to clean up DBs and + (especially) prepared statements. Always wrap their lifetimes + in a try/finally construct, as demonstrated below. By and + large, client code can entirely avoid lifetime-related + complications of prepared statement objects by using the + DB.exec() method for SQL execution. + */ + try { + log("Create a table..."); + db.exec("CREATE TABLE IF NOT EXISTS t(a,b)"); + //Equivalent: + db.exec({ + sql:"CREATE TABLE IF NOT EXISTS t(a,b)" + // ... numerous other options ... + }); + // SQL can be either a string or a byte array + // or an array of strings which get concatenated + // together as-is (so be sure to end each statement + // with a semicolon). + + log("Insert some data using exec()..."); + let i; + for( i = 20; i <= 25; ++i ){ + db.exec({ + sql: "insert into t(a,b) values (?,?)", + // bind by parameter index... + bind: [i, i*2] + }); + db.exec({ + sql: "insert into t(a,b) values ($a,$b)", + // bind by parameter name... + bind: {$a: i * 10, $b: i * 20} + }); + } + + log("Insert using a prepared statement..."); + let q = db.prepare([ + // SQL may be a string or array of strings + // (concatenated w/o separators). + "insert into t(a,b) ", + "values(?,?)" + ]); + try { + for( i = 100; i < 103; ++i ){ + q.bind( [i, i*2] ).step(); + q.reset(); + } + // Equivalent... + for( i = 103; i <= 105; ++i ){ + q.bind(1, i).bind(2, i*2).stepReset(); + } + }finally{ + q.finalize(); + } + + log("Query data with exec() using rowMode 'array'..."); + db.exec({ + sql: "select a from t order by a limit 3", + rowMode: 'array', // 'array' (default), 'object', or 'stmt' + callback: function(row){ + log("row ",++this.counter,"=",row); + }.bind({counter: 0}) + }); + + log("Query data with exec() using rowMode 'object'..."); + db.exec({ + sql: "select a as aa, b as bb from t order by aa limit 3", + rowMode: 'object', + callback: function(row){ + log("row ",++this.counter,"=",JSON.stringify(row)); + }.bind({counter: 0}) + }); + + log("Query data with exec() using rowMode 'stmt'..."); + db.exec({ + sql: "select a from t order by a limit 3", + rowMode: 'stmt', + callback: function(row){ + log("row ",++this.counter,"get(0) =",row.get(0)); + }.bind({counter: 0}) + }); + + log("Query data with exec() using rowMode INTEGER (result column index)..."); + db.exec({ + sql: "select a, b from t order by a limit 3", + rowMode: 1, // === result column 1 + callback: function(row){ + log("row ",++this.counter,"b =",row); + }.bind({counter: 0}) + }); + + log("Query data with exec() using rowMode $COLNAME (result column name)..."); + db.exec({ + sql: "select a a, b from t order by a limit 3", + rowMode: '$a', + callback: function(value){ + log("row ",++this.counter,"a =",value); + }.bind({counter: 0}) + }); + + log("Query data with exec() without a callback..."); + let resultRows = []; + db.exec({ + sql: "select a, b from t order by a limit 3", + rowMode: 'object', + resultRows: resultRows + }); + log("Result rows:",JSON.stringify(resultRows,undefined,2)); + + log("Create a scalar UDF..."); + db.createFunction({ + name: 'twice', + xFunc: function(pCx, arg){ // note the call arg count + return arg + arg; + } + }); + log("Run scalar UDF and collect result column names..."); + let columnNames = []; + db.exec({ + sql: "select a, twice(a), twice(''||a) from t order by a desc limit 3", + columnNames: columnNames, + rowMode: 'stmt', + callback: function(row){ + log("a =",row.get(0), "twice(a) =", row.get(1), + "twice(''||a) =",row.get(2)); + } + }); + log("Result column names:",columnNames); + + try{ + log("The following use of the twice() UDF will", + "fail because of incorrect arg count..."); + db.exec("select twice(1,2,3)"); + }catch(e){ + warn("Got expected exception:",e.message); + } + + try { + db.transaction( function(D) { + D.exec("delete from t"); + log("In transaction: count(*) from t =",db.selectValue("select count(*) from t")); + throw new sqlite3.SQLite3Error("Demonstrating transaction() rollback"); + }); + }catch(e){ + if(e instanceof sqlite3.SQLite3Error){ + log("Got expected exception from db.transaction():",e.message); + log("count(*) from t =",db.selectValue("select count(*) from t")); + }else{ + throw e; + } + } + + try { + db.savepoint( function(D) { + D.exec("delete from t"); + log("In savepoint: count(*) from t =",db.selectValue("select count(*) from t")); + D.savepoint(function(DD){ + const rows = []; + DD.exec({ + sql: ["insert into t(a,b) values(99,100);", + "select count(*) from t"], + rowMode: 0, + resultRows: rows + }); + log("In nested savepoint. Row count =",rows[0]); + throw new sqlite3.SQLite3Error("Demonstrating nested savepoint() rollback"); + }) + }); + }catch(e){ + if(e instanceof sqlite3.SQLite3Error){ + log("Got expected exception from nested db.savepoint():",e.message); + log("count(*) from t =",db.selectValue("select count(*) from t")); + }else{ + throw e; + } + } + }finally{ + db.close(); + } + + log("That's all, folks!"); + + /** + Some of the features of the OO API not demonstrated above... + + - get change count (total or statement-local, 32- or 64-bit) + - get a DB's file name + + Misc. Stmt features: + + - Various forms of bind() + - clearBindings() + - reset() + - Various forms of step() + - Variants of get() for explicit type treatment/conversion, + e.g. getInt(), getFloat(), getBlob(), getJSON() + - getColumnName(ndx), getColumnNames() + - getParamIndex(name) + */ + }/*demo1()*/; + + log("Loading and initializing sqlite3 module..."); + if(globalThis.window!==globalThis) /*worker thread*/{ + /* + If sqlite3.js is in a directory other than this script, in order + to get sqlite3.js to resolve sqlite3.wasm properly, we have to + explicitly tell it where sqlite3.js is being loaded from. We do + that by passing the `sqlite3.dir=theDirName` URL argument to + _this_ script. That URL argument will be seen by the JS/WASM + loader and it will adjust the sqlite3.wasm path accordingly. If + sqlite3.js/.wasm are in the same directory as this script then + that's not needed. + + URL arguments passed as part of the filename via importScripts() + are simply lost, and such scripts see the globalThis.location of + _this_ script. + */ + let sqlite3Js = 'sqlite3.js'; + const urlParams = new URL(globalThis.location.href).searchParams; + if(urlParams.has('sqlite3.dir')){ + sqlite3Js = urlParams.get('sqlite3.dir') + '/' + sqlite3Js; + } + importScripts(sqlite3Js); + } + globalThis.sqlite3InitModule({ + /* We can redirect any stdout/stderr from the module like so, but + note that doing so makes use of Emscripten-isms, not + well-defined sqlite APIs. */ + print: log, + printErr: error + }).then(function(sqlite3){ + //console.log('sqlite3 =',sqlite3); + log("Done initializing. Running demo..."); + try { + demo1(sqlite3); + }catch(e){ + error("Exception:",e.message); + } + }); +})(); diff --git a/ext/wasm/demo-jsstorage.html b/ext/wasm/demo-jsstorage.html new file mode 100644 index 0000000000..79f4a3b4be --- /dev/null +++ b/ext/wasm/demo-jsstorage.html @@ -0,0 +1,49 @@ + + + + + + + + + sqlite3-kvvfs.js tests + + +
    sqlite3-kvvfs.js tests
    + +
    +
    +
    Initializing app...
    +
    + On a slow internet connection this may take a moment. If this + message displays for "a long time", intialization may have + failed and the JavaScript console may contain clues as to why. +
    +
    +
    Downloading...
    +
    + +
    +
    + Options +
    + + + + + +
    +
    +
    + + + + + + diff --git a/ext/wasm/demo-jsstorage.js b/ext/wasm/demo-jsstorage.js new file mode 100644 index 0000000000..587aa9cc58 --- /dev/null +++ b/ext/wasm/demo-jsstorage.js @@ -0,0 +1,114 @@ +/* + 2022-09-12 + + The author disclaims copyright to this source code. In place of a + legal notice, here is a blessing: + + * May you do good and not evil. + * May you find forgiveness for yourself and forgive others. + * May you share freely, never taking more than you give. + + *********************************************************************** + + A basic test script for sqlite3.wasm with kvvfs support. This file + must be run in main JS thread and sqlite3.js must have been loaded + before it. +*/ +'use strict'; +(function(){ + const T = self.SqliteTestUtil; + const toss = function(...args){throw new Error(args.join(' '))}; + const debug = console.debug.bind(console); + const eOutput = document.querySelector('#test-output'); + const logC = console.log.bind(console) + const logE = function(domElement){ + eOutput.append(domElement); + }; + const logHtml = function(cssClass,...args){ + const ln = document.createElement('div'); + if(cssClass) ln.classList.add(cssClass); + ln.append(document.createTextNode(args.join(' '))); + logE(ln); + } + const log = function(...args){ + logC(...args); + logHtml('',...args); + }; + const warn = function(...args){ + logHtml('warning',...args); + }; + const error = function(...args){ + logHtml('error',...args); + }; + + const runTests = function(sqlite3){ + const capi = sqlite3.capi, + oo = sqlite3.oo1, + wasm = sqlite3.wasm; + log("Loaded module:",capi.sqlite3_libversion(), capi.sqlite3_sourceid()); + T.assert( 0 !== capi.sqlite3_vfs_find(null) ); + if(!capi.sqlite3_vfs_find('kvvfs')){ + error("This build is not kvvfs-capable."); + return; + } + + const dbStorage = 0 ? 'session' : 'local'; + const theStore = 's'===dbStorage[0] ? sessionStorage : localStorage; + const db = new oo.JsStorageDb( dbStorage ); + // Or: oo.DB(dbStorage, 'c', 'kvvfs') + log("db.storageSize():",db.storageSize()); + document.querySelector('#btn-clear-storage').addEventListener('click',function(){ + const sz = db.clearStorage(); + log("kvvfs",db.filename+"Storage cleared:",sz,"entries."); + }); + document.querySelector('#btn-clear-log').addEventListener('click',function(){ + eOutput.innerText = ''; + }); + document.querySelector('#btn-init-db').addEventListener('click',function(){ + try{ + const saveSql = []; + db.exec({ + sql: ["drop table if exists t;", + "create table if not exists t(a);", + "insert into t(a) values(?),(?),(?)"], + bind: [performance.now() >> 0, + (performance.now() * 2) >> 0, + (performance.now() / 2) >> 0], + saveSql + }); + console.log("saveSql =",saveSql,theStore); + log("DB (re)initialized."); + }catch(e){ + error(e.message); + } + }); + const btnSelect = document.querySelector('#btn-select1'); + btnSelect.addEventListener('click',function(){ + log("DB rows:"); + try{ + db.exec({ + sql: "select * from t order by a", + rowMode: 0, + callback: (v)=>log(v) + }); + }catch(e){ + error(e.message); + } + }); + document.querySelector('#btn-storage-size').addEventListener('click',function(){ + log("size.storageSize(",dbStorage,") says", db.storageSize(), + "bytes"); + }); + log("Storage backend:",db.filename); + if(0===db.selectValue('select count(*) from sqlite_master')){ + log("DB is empty. Use the init button to populate it."); + }else{ + log("DB contains data from a previous session. Use the Clear Storage button to delete it."); + btnSelect.click(); + } + }; + + sqlite3InitModule(self.sqlite3TestModule).then((sqlite3)=>{ + runTests(sqlite3); + }); +})(); diff --git a/ext/wasm/demo-worker1-promiser.c-pp.html b/ext/wasm/demo-worker1-promiser.c-pp.html new file mode 100644 index 0000000000..e0b487bdf3 --- /dev/null +++ b/ext/wasm/demo-worker1-promiser.c-pp.html @@ -0,0 +1,42 @@ + + + + + + + + +//#if target=es6-module + worker-promise (via ESM) tests +//#else + worker-promise tests +//#endif + + +
    worker-promise tests
    + +
    +
    +
    Initializing app...
    +
    + On a slow internet connection this may take a moment. If this + message displays for "a long time", intialization may have + failed and the JavaScript console may contain clues as to why. +
    +
    +
    Downloading...
    +
    + +
    +
    Most stuff on this page happens in the dev console.
    +
    +
    + +//#if target=es6-module + +//#else + + +//#endif + + diff --git a/ext/wasm/demo-worker1-promiser.c-pp.js b/ext/wasm/demo-worker1-promiser.c-pp.js new file mode 100644 index 0000000000..c129e21281 --- /dev/null +++ b/ext/wasm/demo-worker1-promiser.c-pp.js @@ -0,0 +1,288 @@ +/* + 2022-08-23 + + The author disclaims copyright to this source code. In place of a + legal notice, here is a blessing: + + * May you do good and not evil. + * May you find forgiveness for yourself and forgive others. + * May you share freely, never taking more than you give. + + *********************************************************************** + + Demonstration of the sqlite3 Worker API #1 Promiser: a Promise-based + proxy for for the sqlite3 Worker #1 API. +*/ +//#if target:es6-module +import {default as promiserFactory} from "./jswasm/sqlite3-worker1-promiser.mjs"; +//#else +"use strict"; +const promiserFactory = globalThis.sqlite3Worker1Promiser.v2; +delete globalThis.sqlite3Worker1Promiser; +//#endif +(async function(){ + const T = globalThis.SqliteTestUtil; + const eOutput = document.querySelector('#test-output'); + const warn = console.warn.bind(console); + const error = console.error.bind(console); + const log = console.log.bind(console); + const logHtml = async function(cssClass,...args){ + log.apply(this, args); + const ln = document.createElement('div'); + if(cssClass) ln.classList.add(cssClass); + ln.append(document.createTextNode(args.join(' '))); + eOutput.append(ln); + }; + + let startTime; + const testCount = async ()=>{ + logHtml("","Total test count:",T.counter+". Total time =",(performance.now() - startTime),"ms"); + }; + + const promiserConfig = { +//#if not target:es6-module + /** + The v1 interfaces uses an onready function. The v2 interface optionally + accepts one but does not require it. If provided, it is called _before_ + the promise is resolved, and the promise is rejected if onready() throws. + */ + onready: function(f){ + /* f === the function returned by promiserFactory(). + Ostensibly (f === workerPromise) but this function is + called before the promiserFactory() Promise resolves, so + before workerPromise is set. */ + console.warn("This is the v2 interface - you don't need an onready() function."); + }, +//#endif + debug: 1 ? undefined : (...args)=>console.debug('worker debug',...args), + onunhandled: function(ev){ + error("Unhandled worker message:",ev.data); + }, + onerror: function(ev){ + error("worker1 error:",ev); + } + }; + const workerPromise = await promiserFactory(promiserConfig) + .then((func)=>{ + console.log("Init complete. Starting tests momentarily."); + globalThis.sqlite3TestModule.setStatus(null)/*hide the HTML-side is-loading spinner*/; + return func; + }); + + const wtest = async function(msgType, msgArgs, callback){ + if(2===arguments.length && 'function'===typeof msgArgs){ + callback = msgArgs; + msgArgs = undefined; + } + const p = 1 + ? workerPromise({type: msgType, args:msgArgs}) + : workerPromise(msgType, msgArgs); + return callback ? p.then(callback).finally(testCount) : p; + }; + + let sqConfig; + const runTests = async function(){ + const dbFilename = '/testing2.sqlite3'; + startTime = performance.now(); + + await wtest('config-get', (ev)=>{ + const r = ev.result; + log('sqlite3.config subset:', r); + T.assert('boolean' === typeof r.bigIntEnabled); + sqConfig = r; + }); + logHtml('', + "Sending 'open' message and waiting for its response before continuing..."); + + await wtest('open', { + filename: dbFilename, + simulateError: 0 /* if true, fail the 'open' */, + }, function(ev){ + const r = ev.result; + log("then open result",r); + T.assert(ev.dbId === r.dbId) + .assert(ev.messageId) + .assert('string' === typeof r.vfs); + promiserConfig.dbId = ev.dbId; + }).then(runTests2); + }; + + const runTests2 = async function(){ + const mustNotReach = ()=>toss("This is not supposed to be reached."); + + await wtest('exec',{ + sql: ["create table t(a,b)", + "insert into t(a,b) values(1,2),(3,4),(5,6)" + ].join(';'), + resultRows: [], columnNames: [], + lastInsertRowId: true, + countChanges: sqConfig.bigIntEnabled ? 64 : true + }, function(ev){ + ev = ev.result; + T.assert(0===ev.resultRows.length) + .assert(0===ev.columnNames.length) + .assert(sqConfig.bigIntEnabled + ? (3n===ev.changeCount) + : (3===ev.changeCount)) + .assert('bigint'===typeof ev.lastInsertRowId) + .assert(ev.lastInsertRowId>=3); + }); + + await wtest('exec',{ + sql: 'select a a, b b from t order by a', + resultRows: [], columnNames: [], + }, function(ev){ + ev = ev.result; + T.assert(3===ev.resultRows.length) + .assert(1===ev.resultRows[0][0]) + .assert(6===ev.resultRows[2][1]) + .assert(2===ev.columnNames.length) + .assert('b'===ev.columnNames[1]); + }); + + await wtest('exec',{ + sql: 'select a a, b b from t order by a', + resultRows: [], columnNames: [], + rowMode: 'object', + countChanges: true + }, function(ev){ + ev = ev.result; + T.assert(3===ev.resultRows.length) + .assert(1===ev.resultRows[0].a) + .assert(6===ev.resultRows[2].b) + .assert(0===ev.changeCount); + }); + + await wtest( + 'exec', + {sql:'intentional_error'}, + mustNotReach + ).catch((e)=>{ + warn("Intentional error:",e); + }); + + await wtest('exec',{ + sql:'select 1 union all select 3', + resultRows: [] + }, function(ev){ + ev = ev.result; + T.assert(2 === ev.resultRows.length) + .assert(1 === ev.resultRows[0][0]) + .assert(3 === ev.resultRows[1][0]) + .assert(undefined === ev.changeCount); + }); + + const resultRowTest1 = function f(ev){ + if(undefined === f.counter) f.counter = 0; + if(null === ev.rowNumber){ + /* End of result set. */ + T.assert(undefined === ev.row) + .assert(2===ev.columnNames.length) + .assert('a'===ev.columnNames[0]) + .assert('B'===ev.columnNames[1]); + }else{ + T.assert(ev.rowNumber > 0); + ++f.counter; + } + log("exec() result row:",ev); + T.assert(null === ev.rowNumber || 'number' === typeof ev.row.B); + }; + await wtest('exec',{ + sql: 'select a a, b B from t order by a limit 3', + callback: resultRowTest1, + rowMode: 'object' + }, function(ev){ + T.assert(3===resultRowTest1.counter); + resultRowTest1.counter = 0; + }); + + const resultRowTest2 = function f(ev){ + if(null === ev.rowNumber){ + /* End of result set. */ + T.assert(undefined === ev.row) + .assert(1===ev.columnNames.length) + .assert('a'===ev.columnNames[0]) + }else{ + T.assert(ev.rowNumber > 0); + f.counter = ev.rowNumber; + } + log("exec() result row:",ev); + T.assert(null === ev.rowNumber || 'number' === typeof ev.row); + }; + await wtest('exec',{ + sql: 'select a a from t limit 3', + callback: resultRowTest2, + rowMode: 0 + }, function(ev){ + T.assert(3===resultRowTest2.counter); + }); + + const resultRowTest3 = function f(ev){ + if(null === ev.rowNumber){ + T.assert(3===ev.columnNames.length) + .assert('foo'===ev.columnNames[0]) + .assert('bar'===ev.columnNames[1]) + .assert('baz'===ev.columnNames[2]); + }else{ + f.counter = ev.rowNumber; + T.assert('number' === typeof ev.row); + } + }; + await wtest('exec',{ + sql: "select 'foo' foo, a bar, 'baz' baz from t limit 2", + callback: resultRowTest3, + columnNames: [], + rowMode: '$bar' + }, function(ev){ + log("exec() result row:",ev); + T.assert(2===resultRowTest3.counter); + }); + + await wtest('exec',{ + sql:[ + 'pragma foreign_keys=0;', + // ^^^ arbitrary query with no result columns + 'select a, b from t order by a desc; select a from t;' + // exec() only honors SELECT results from the first + // statement with result columns (regardless of whether + // it has any rows). + ], + rowMode: 1, + resultRows: [] + },function(ev){ + const rows = ev.result.resultRows; + T.assert(3===rows.length). + assert(6===rows[0]); + }); + + await wtest('exec',{sql: 'delete from t where a>3'}); + + await wtest('exec',{ + sql: 'select count(a) from t', + resultRows: [] + },function(ev){ + ev = ev.result; + T.assert(1===ev.resultRows.length) + .assert(2===ev.resultRows[0][0]); + }); + + await wtest('export', function(ev){ + ev = ev.result; + T.assert('string' === typeof ev.filename) + .assert(ev.byteArray instanceof Uint8Array) + .assert(ev.byteArray.length > 1024) + .assert('application/x-sqlite3' === ev.mimetype); + }); + + /***** close() tests must come last. *****/ + await wtest('close',{},function(ev){ + T.assert('string' === typeof ev.result.filename); + }); + + await wtest('close', (ev)=>{ + T.assert(undefined === ev.result.filename); + }).finally(()=>logHtml('',"That's all, folks!")); + }/*runTests2()*/; + + runTests(); +})(); diff --git a/ext/wasm/demo-worker1.html b/ext/wasm/demo-worker1.html new file mode 100644 index 0000000000..c766ffd445 --- /dev/null +++ b/ext/wasm/demo-worker1.html @@ -0,0 +1,34 @@ + + + + + + + + + + sqlite3-worker1.js tests + + +
    sqlite3-worker1.js tests
    + +
    +
    +
    Initializing app...
    +
    + On a slow internet connection this may take a moment. If this + message displays for "a long time", intialization may have + failed and the JavaScript console may contain clues as to why. +
    +
    +
    Downloading...
    +
    + +
    +
    Most stuff on this page happens in the dev console.
    +
    +
    + + + + diff --git a/ext/wasm/demo-worker1.js b/ext/wasm/demo-worker1.js new file mode 100644 index 0000000000..1a05cc7ac2 --- /dev/null +++ b/ext/wasm/demo-worker1.js @@ -0,0 +1,348 @@ +/* + 2022-05-22 + + The author disclaims copyright to this source code. In place of a + legal notice, here is a blessing: + + * May you do good and not evil. + * May you find forgiveness for yourself and forgive others. + * May you share freely, never taking more than you give. + + *********************************************************************** + + A basic test script for sqlite3-worker1.js. + + Note that the wrapper interface demonstrated in + demo-worker1-promiser.js is much easier to use from client code, as it + lacks the message-passing acrobatics demonstrated in this file. +*/ +'use strict'; +(function(){ + const T = self.SqliteTestUtil; + const SW = new Worker("jswasm/sqlite3-worker1.js"); + const DbState = { + id: undefined + }; + const eOutput = document.querySelector('#test-output'); + const log = console.log.bind(console); + const logHtml = function(cssClass,...args){ + log.apply(this, args); + const ln = document.createElement('div'); + if(cssClass) ln.classList.add(cssClass); + ln.append(document.createTextNode(args.join(' '))); + eOutput.append(ln); + }; + const warn = console.warn.bind(console); + const error = console.error.bind(console); + const toss = (...args)=>{throw new Error(args.join(' '))}; + + SW.onerror = function(event){ + error("onerror",event); + }; + + let startTime; + + /** + A queue for callbacks which are to be run in response to async + DB commands. See the notes in runTests() for why we need + this. The event-handling plumbing of this file requires that + any DB command which includes a `messageId` property also have + a queued callback entry, as the existence of that property in + response payloads is how it knows whether or not to shift an + entry off of the queue. + */ + const MsgHandlerQueue = { + queue: [], + id: 0, + push: function(type,callback){ + this.queue.push(callback); + return type + '-' + (++this.id); + }, + shift: function(){ + return this.queue.shift(); + } + }; + + const testCount = ()=>{ + logHtml("","Total test count:",T.counter+". Total time =",(performance.now() - startTime),"ms"); + }; + + const logEventResult = function(ev){ + const evd = ev.result; + logHtml(evd.errorClass ? 'error' : '', + "runOneTest",ev.messageId,"Worker time =", + (ev.workerRespondTime - ev.workerReceivedTime),"ms.", + "Round-trip event time =", + (performance.now() - ev.departureTime),"ms.", + (evd.errorClass ? evd.message : "")//, JSON.stringify(evd) + ); + }; + + const runOneTest = function(eventType, eventArgs, callback){ + T.assert(eventArgs && 'object'===typeof eventArgs); + /* ^^^ that is for the testing and messageId-related code, not + a hard requirement of all of the Worker-exposed APIs. */ + const messageId = MsgHandlerQueue.push(eventType,function(ev){ + logEventResult(ev); + if(callback instanceof Function){ + callback(ev); + testCount(); + } + }); + const msg = { + type: eventType, + args: eventArgs, + dbId: DbState.id, + messageId: messageId, + departureTime: performance.now() + }; + log("Posting",eventType,"message to worker dbId="+(DbState.id||'default')+':',msg); + SW.postMessage(msg); + }; + + /** Methods which map directly to onmessage() event.type keys. + They get passed the inbound event.data. */ + const dbMsgHandler = { + open: function(ev){ + DbState.id = ev.dbId; + log("open result",ev); + }, + exec: function(ev){ + log("exec result",ev); + }, + export: function(ev){ + log("export result",ev); + }, + error: function(ev){ + error("ERROR from the worker:",ev); + logEventResult(ev); + }, + resultRowTest1: function f(ev){ + if(undefined === f.counter) f.counter = 0; + if(null === ev.rowNumber){ + /* End of result set. */ + T.assert(undefined === ev.row) + .assert(Array.isArray(ev.columnNames)) + .assert(ev.columnNames.length); + }else{ + T.assert(ev.rowNumber > 0); + ++f.counter; + } + //log("exec() result row:",ev); + T.assert(null === ev.rowNumber || 'number' === typeof ev.row.b); + } + }; + + /** + "The problem" now is that the test results are async. We + know, however, that the messages posted to the worker will + be processed in the order they are passed to it, so we can + create a queue of callbacks to handle them. The problem + with that approach is that it's not error-handling + friendly, in that an error can cause us to bypass a result + handler queue entry. We have to perform some extra + acrobatics to account for that. + + Problem #2 is that we cannot simply start posting events: we + first have to post an 'open' event, wait for it to respond, and + collect its db ID before continuing. If we don't wait, we may + well fire off 10+ messages before the open actually responds. + */ + const runTests2 = function(){ + const mustNotReach = ()=>{ + throw new Error("This is not supposed to be reached."); + }; + runOneTest('exec',{ + sql: ["create table t(a,b);", + "insert into t(a,b) values(1,2),(3,4),(5,6)" + ], + lastInsertRowId: true, + resultRows: [], columnNames: [] + }, function(ev){ + ev = ev.result; + T.assert(0===ev.resultRows.length) + .assert(0===ev.columnNames.length) + .assert('bigint'===typeof ev.lastInsertRowId) + .assert(ev.lastInsertRowId>=3); + }); + runOneTest('exec',{ + sql: 'select a a, b b from t order by a', + resultRows: [], columnNames: [], saveSql:[] + }, function(ev){ + ev = ev.result; + T.assert(3===ev.resultRows.length) + .assert(1===ev.resultRows[0][0]) + .assert(6===ev.resultRows[2][1]) + .assert(2===ev.columnNames.length) + .assert('b'===ev.columnNames[1]); + }); + //if(1){ error("Returning prematurely for testing."); return; } + runOneTest('exec',{ + sql: 'select a a, b b from t order by a', + resultRows: [], columnNames: [], + rowMode: 'object' + }, function(ev){ + ev = ev.result; + T.assert(3===ev.resultRows.length) + .assert(1===ev.resultRows[0].a) + .assert(6===ev.resultRows[2].b) + }); + runOneTest('exec',{sql:'intentional_error'}, mustNotReach); + // Ensure that the message-handler queue survives ^^^ that error... + runOneTest('exec',{ + sql:'select 1', + resultRows: [], + //rowMode: 'array', // array is the default in the Worker interface + }, function(ev){ + ev = ev.result; + T.assert(1 === ev.resultRows.length) + .assert(1 === ev.resultRows[0][0]); + }); + runOneTest('exec',{ + sql: 'select a a, b b from t order by a', + callback: 'resultRowTest1', + rowMode: 'object' + }, function(ev){ + T.assert(3===dbMsgHandler.resultRowTest1.counter); + dbMsgHandler.resultRowTest1.counter = 0; + }); + runOneTest('exec',{ + sql:[ + "pragma foreign_keys=0;", + // ^^^ arbitrary query with no result columns + "select a, b from t order by a desc;", + "select a from t;" + // exec() only honors SELECT results from the first + // statement with result columns (regardless of whether + // it has any rows). + ], + rowMode: 1, + resultRows: [] + },function(ev){ + const rows = ev.result.resultRows; + T.assert(3===rows.length). + assert(6===rows[0]); + }); + runOneTest('exec',{sql: 'delete from t where a>3'}); + runOneTest('exec',{ + sql: 'select count(a) from t', + resultRows: [] + },function(ev){ + ev = ev.result; + T.assert(1===ev.resultRows.length) + .assert(2===ev.resultRows[0][0]); + }); + runOneTest('export',{}, function(ev){ + ev = ev.result; + log("export result:",ev); + T.assert('string' === typeof ev.filename) + .assert(ev.byteArray instanceof Uint8Array) + .assert(ev.byteArray.length > 1024) + .assert('application/x-sqlite3' === ev.mimetype); + }); + /***** close() tests must come last. *****/ + runOneTest('close',{unlink:true},function(ev){ + ev = ev.result; + T.assert('string' === typeof ev.filename); + }); + runOneTest('close',{unlink:true},function(ev){ + ev = ev.result; + T.assert(undefined === ev.filename); + logHtml('warning',"This is the final test."); + }); + logHtml('warning',"Finished posting tests. Waiting on async results."); + }; + + const runTests = function(){ + /** + Design decision time: all remaining tests depend on the 'open' + command having succeeded. In order to support multiple DBs, the + upcoming commands ostensibly have to know the ID of the DB they + want to talk to. We have two choices: + + 1) We run 'open' and wait for its response, which contains the + db id. + + 2) We have the Worker automatically use the current "default + db" (the one which was most recently opened) if no db id is + provided in the message. When we do this, the main thread may + well fire off _all_ of the test messages before the 'open' + actually responds, but because the messages are handled on a + FIFO basis, those after the initial 'open' will pick up the + "default" db. However, if the open fails, then all pending + messages (until next next 'open', at least) except for 'close' + will fail and we have no way of cancelling them once they've + been posted to the worker. + + Which approach we use below depends on the boolean value of + waitForOpen. + */ + const waitForOpen = 1, + simulateOpenError = 0 /* if true, the remaining tests will + all barf if waitForOpen is + false. */; + logHtml('', + "Sending 'open' message and",(waitForOpen ? "" : "NOT ")+ + "waiting for its response before continuing."); + startTime = performance.now(); + runOneTest('open', { + filename:'testing2.sqlite3', + simulateError: simulateOpenError + }, function(ev){ + log("open result",ev); + T.assert('testing2.sqlite3'===ev.result.filename) + .assert(ev.dbId) + .assert(ev.messageId) + .assert('string' === typeof ev.result.vfs); + DbState.id = ev.dbId; + if(waitForOpen) setTimeout(runTests2, 0); + }); + if(!waitForOpen) runTests2(); + }; + + SW.onmessage = function(ev){ + if(!ev.data || 'object'!==typeof ev.data){ + warn("Unknown sqlite3-worker message type:",ev); + return; + } + ev = ev.data/*expecting a nested object*/; + //log("main window onmessage:",ev); + if(ev.result && ev.messageId){ + /* We're expecting a queued-up callback handler. */ + const f = MsgHandlerQueue.shift(); + if('error'===ev.type){ + dbMsgHandler.error(ev); + return; + } + T.assert(f instanceof Function); + f(ev); + return; + } + switch(ev.type){ + case 'sqlite3-api': + switch(ev.result){ + case 'worker1-ready': + log("Message:",ev); + self.sqlite3TestModule.setStatus(null); + runTests(); + return; + default: + warn("Unknown sqlite3-api message type:",ev); + return; + } + default: + if(dbMsgHandler.hasOwnProperty(ev.type)){ + try{dbMsgHandler[ev.type](ev);} + catch(err){ + error("Exception while handling db result message", + ev,":",err); + } + return; + } + warn("Unknown sqlite3-api message type:",ev); + } + }; + log("Init complete, but async init bits may still be running."); + log("Installing Worker into global scope SW for dev purposes."); + self.SW = SW; +})(); diff --git a/ext/wasm/example_extra_init.c b/ext/wasm/example_extra_init.c new file mode 100644 index 0000000000..b91d757cd5 --- /dev/null +++ b/ext/wasm/example_extra_init.c @@ -0,0 +1,23 @@ +/* +** If the canonical build process finds the file +** sqlite3_wasm_extra_init.c in the main wasm build directory, it +** arranges to include that file in the build of sqlite3.wasm and +** defines SQLITE_EXTRA_INIT=sqlite3_wasm_extra_init. +** +** The C file must define the function sqlite3_wasm_extra_init() with +** this signature: +** +** int sqlite3_wasm_extra_init(const char *) +** +** and the sqlite3 library will call it with an argument of NULL one +** time during sqlite3_initialize(). If it returns non-0, +** initialization of the library will fail. +*/ + +#include "sqlite3.h" +#include + +int sqlite3_wasm_extra_init(const char *z){ + fprintf(stderr,"%s: %s()\n", __FILE__, __func__); + return 0; +} diff --git a/ext/wasm/fiddle/fiddle-worker.js b/ext/wasm/fiddle/fiddle-worker.js new file mode 100644 index 0000000000..a5f3e25b72 --- /dev/null +++ b/ext/wasm/fiddle/fiddle-worker.js @@ -0,0 +1,390 @@ +/* + 2022-05-20 + + The author disclaims copyright to this source code. In place of a + legal notice, here is a blessing: + + * May you do good and not evil. + * May you find forgiveness for yourself and forgive others. + * May you share freely, never taking more than you give. + + *********************************************************************** + + This is the JS Worker file for the sqlite3 fiddle app. It loads the + sqlite3 wasm module and offers access to the db via the Worker + message-passing interface. + + Forewarning: this API is still very much Under Construction and + subject to any number of changes as experience reveals what those + need to be. + + Because we can have only a single message handler, as opposed to an + arbitrary number of discrete event listeners like with DOM elements, + we have to define a lower-level message API. Messages abstractly + look like: + + { type: string, data: type-specific value } + + Where 'type' is used for dispatching and 'data' is a + 'type'-dependent value. + + The 'type' values expected by each side of the main/worker + connection vary. The types are described below but subject to + change at any time as this experiment evolves. + + Workers-to-Main types + + - stdout, stderr: indicate stdout/stderr output from the wasm + layer. The data property is the string of the output, noting + that the emscripten binding emits these one line at a time. Thus, + if a C-side puts() emits multiple lines in a single call, the JS + side will see that as multiple calls. Example: + + {type:'stdout', data: 'Hi, world.'} + + - module: Status text. This is intended to alert the main thread + about module loading status so that, e.g., the main thread can + update a progress widget and DTRT when the module is finished + loading and available for work. Status messages come in the form + + {type:'module', data:{ + type:'status', + data: {text:string|null, step:1-based-integer} + } + + with an incrementing step value for each subsequent message. When + the module loading is complete, a message with a text value of + null is posted. + + - working: data='start'|'end'. Indicates that work is about to be + sent to the module or has just completed. This can be used, e.g., + to disable UI elements which should not be activated while work + is pending. Example: + + {type:'working', data:'start'} + + Main-to-Worker types: + + - shellExec: data=text to execute as if it had been entered in the + sqlite3 CLI shell app (as opposed to sqlite3_exec()). This event + causes the worker to emit a 'working' event (data='start') before + it starts and a 'working' event (data='end') when it finished. If + called while work is currently being executed it emits stderr + message instead of doing actual work, as the underlying db cannot + handle concurrent tasks. Example: + + {type:'shellExec', data: 'select * from sqlite_master'} + + - More TBD as the higher-level db layer develops. +*/ + +/* + Apparent browser(s) bug: console messages emitted may be duplicated + in the console, even though they're provably only run once. See: + + https://stackoverflow.com/questions/49659464 + + Noting that it happens in Firefox as well as Chrome. Harmless but + annoying. +*/ +"use strict"; +(function(){ + /** + Posts a message in the form {type,data}. If passed more than 2 + args, the 3rd must be an array of "transferable" values to pass + as the 2nd argument to postMessage(). */ + const wMsg = + (type,data,transferables)=>{ + postMessage({type, data}, transferables || []); + }; + const stdout = (...args)=>wMsg('stdout', args); + const stderr = (...args)=>wMsg('stderr', args); + const toss = (...args)=>{ + throw new Error(args.join(' ')); + }; + const fixmeOPFS = "(FIXME: won't work with OPFS-over-sqlite3_vfs.)"; + let sqlite3 /* gets assigned when the wasm module is loaded */; + + self.onerror = function(/*message, source, lineno, colno, error*/) { + const err = arguments[4]; + if(err && 'ExitStatus'==err.name){ + /* This is relevant for the sqlite3 shell binding but not the + lower-level binding. */ + fiddleModule.isDead = true; + stderr("FATAL ERROR:", err.message); + stderr("Restarting the app requires reloading the page."); + wMsg('error', err); + } + console.error(err); + fiddleModule.setStatus('Exception thrown, see JavaScript console: '+err); + }; + + const Sqlite3Shell = { + /** Returns the name of the currently-opened db. */ + dbFilename: function f(){ + if(!f._) f._ = sqlite3.wasm.xWrap('fiddle_db_filename', "string", ['string']); + return f._(0); + }, + dbHandle: function f(){ + if(!f._) f._ = sqlite3.wasm.xWrap("fiddle_db_handle", "sqlite3*"); + return f._(); + }, + dbIsOpfs: function f(){ + return sqlite3.opfs && sqlite3.capi.sqlite3_js_db_uses_vfs( + this.dbHandle(), "opfs" + ); + }, + runMain: function f(){ + if(f.argv) return 0===f.argv.rc; + const dbName = "/fiddle.sqlite3"; + f.argv = [ + 'sqlite3-fiddle.wasm', + '-bail', '-safe', + dbName + /* Reminder: because of how we run fiddle, we have to ensure + that any argv strings passed to its main() are valid until + the wasm environment shuts down. */ + ]; + const capi = sqlite3.capi, wasm = sqlite3.wasm; + /* We need to call sqlite3_shutdown() in order to avoid numerous + legitimate warnings from the shell about it being initialized + after sqlite3_initialize() has been called. This means, + however, that any initialization done by the JS code may need + to be re-done (e.g. re-registration of dynamically-loaded + VFSes). We need a more generic approach to running such + init-level code. */ + capi.sqlite3_shutdown(); + f.argv.pArgv = wasm.allocMainArgv(f.argv); + f.argv.rc = wasm.exports.fiddle_main( + f.argv.length, f.argv.pArgv + ); + if(f.argv.rc){ + stderr("Fatal error initializing sqlite3 shell."); + fiddleModule.isDead = true; + return false; + } + wMsg('sqlite-version', { + lib: capi.sqlite3_libversion(), + srcId: capi.sqlite3_sourceid() + }); + stdout("WASM pointer size: "+wasm.ptr.size); + stdout('Welcome to the "fiddle" shell. Tap the About button for more info.'); + if(capi.sqlite3_vfs_find("opfs")){ + stdout("\nOPFS is available. To open a persistent db, use:\n\n", + " .open file:name?vfs=opfs\n\nbut note that some", + "features (e.g. upload) do not yet work with OPFS."); + } + stdout('\nEnter ".help" for usage hints.'); + this.exec([ // initialization commands... + '.nullvalue NULL', + '.headers on' + ].join('\n')); + return true; + }, + /** + Runs the given text through the shell as if it had been typed + in by a user. Fires a working/start event before it starts and + working/end event when it finishes. + */ + exec: function f(sql){ + if(!f._){ + if(!this.runMain()) return; + f._ = sqlite3.wasm.xWrap('fiddle_exec', undefined, ['string']); + } + if(fiddleModule.isDead){ + stderr("shell module has exit()ed. Cannot run SQL."); + return; + } + wMsg('working','start'); + try { + if(f._running){ + stderr('Cannot run multiple commands concurrently.'); + }else if(sql){ + if(Array.isArray(sql)) sql = sql.join(''); + f._running = true; + //stdout("calling native exec:",sql); + f._(sql); + //stdout("returned from native exec",sql); + } + }finally{ + delete f._running; + wMsg('working','end'); + wMsg('wasm-info', { + pointerSize: sqlite3.wasm.ptr.size, + heapSize: sqlite3.wasm.heap8().byteLength + }); + } + }, + resetDb: function f(){ + if(!f._) f._ = sqlite3.wasm.xWrap('fiddle_reset_db', undefined); + stdout("Resetting database."); + f._(); + stdout("Reset",this.dbFilename()); + }, + /* Interrupt can't work: this Worker is tied up working, so won't get the + interrupt event which would be needed to perform the interrupt. */ + interrupt: function f(){ + if(!f._) f._ = sqlite3.wasm.xWrap('fiddle_interrupt', undefined); + stdout("Requesting interrupt."); + f._(); + } + }; + + self.onmessage = function f(ev){ + ev = ev.data; + if(!f.cache){ + f.cache = { + prevFilename: null + }; + } + //console.debug("worker: onmessage.data",ev); + switch(ev.type){ + case 'shellExec': Sqlite3Shell.exec(ev.data); return; + case 'db-reset': Sqlite3Shell.resetDb(); return; + case 'interrupt': Sqlite3Shell.interrupt(); return; + /** Triggers the export of the current db. Fires an + event in the form: + + {type:'db-export', + data:{ + filename: name of db, + buffer: contents of the db file (Uint8Array), + error: on error, a message string and no buffer property. + } + } + */ + case 'db-export': { + const fn = Sqlite3Shell.dbFilename(); + stdout("Exporting",fn+"."); + const fn2 = fn ? fn.split(/[/\\]/).pop() : null; + try{ + if(!fn2) toss("DB appears to be closed."); + const buffer = sqlite3.capi.sqlite3_js_db_export( + Sqlite3Shell.dbHandle() + ); + wMsg('db-export',{filename: fn2, buffer: buffer.buffer}, [buffer.buffer]); + }catch(e){ + console.error("Export failed:",e); + /* Post a failure message so that UI elements disabled + during the export can be re-enabled. */ + wMsg('db-export',{ + filename: fn, + error: e.message + }); + } + return; + } + case 'open': { + /* Expects: { + buffer: ArrayBuffer | Uint8Array, + filename: the filename for the db. Any dir part is stripped. + } + */ + const opt = ev.data; + let buffer = opt.buffer; + stderr('open():',fixmeOPFS); + if(buffer instanceof ArrayBuffer){ + buffer = new Uint8Array(buffer); + }else if(!(buffer instanceof Uint8Array)){ + stderr("'open' expects {buffer:Uint8Array} containing an uploaded db."); + return; + } + buffer.set([1,1], 18)/*force db out of WAL mode*/; + const fn = ( + opt.filename + ? opt.filename.split(/[/\\]/).pop().replace(/["'\s]/g,'_') + : ("db-"+((Math.random() * 10000000) | 0)+ + "-"+((Math.random() * 10000000) | 0)+".sqlite3") + ); + try { + /* We cannot delete the existing db file until the new one + is installed, which means that we risk overflowing our + quota (if any) by having both the previous and current + db briefly installed in the virtual filesystem. */ + const fnAbs = '/'+fn; + const oldName = Sqlite3Shell.dbFilename(); + if(oldName && oldName===fnAbs){ + /* We cannot create the replacement file while the current file + is opened, nor does the shell have a .close command, so we + must temporarily switch to another db... */ + Sqlite3Shell.exec('.open :memory:'); + fiddleModule.FS.unlink(fnAbs); + } + fiddleModule.FS.createDataFile("/", fn, buffer, true, true); + Sqlite3Shell.exec('.open '+fnAbs); + if(oldName && oldName!==fnAbs){ + try{fiddleModule.fsUnlink(oldName)} + catch(e){/*ignored*/} + } + stdout("Replaced DB with",fn+"."); + }catch(e){ + stderr("Error installing db",fn+":",e.message); + } + return; + } + }; + sqlite3.config.warn("Unknown fiddle-worker message type:",ev); + }; + + /** + emscripten module for use with build mode -sMODULARIZE. + */ + const fiddleModule = { + print: stdout, + printErr: stderr, + /** + Intercepts status updates from the emscripting module init + and fires worker events with a type of 'status' and a + payload of: + + { + text: string | null, // null at end of load process + step: integer // starts at 1, increments 1 per call + } + + We have no way of knowing in advance how many steps will + be processed/posted, so creating a "percentage done" view is + not really practical. One can be approximated by giving it a + current value of message.step and max value of message.step+1, + though. + + When work is finished, a message with a text value of null is + submitted. + + After a message with text==null is posted, the module may later + post messages about fatal problems, e.g. an exit() being + triggered, so it is recommended that UI elements for posting + status messages not be outright removed from the DOM when + text==null, and that they instead be hidden until/unless + text!=null. + */ + setStatus: function f(text){ + if(!f.last) f.last = { step: 0, text: '' }; + else if(text === f.last.text) return; + f.last.text = text; + wMsg('module',{ + type:'status', + data:{step: ++f.last.step, text: text||null} + }); + } + }; + + importScripts('fiddle-module.js'+self.location.search); + /** + initFiddleModule() is installed via fiddle-module.js due to + building with: + + emcc ... -sMODULARIZE=1 -sEXPORT_NAME=initFiddleModule + */ + sqlite3InitModule(fiddleModule).then((_sqlite3)=>{ + sqlite3 = _sqlite3; + sqlite3.config.warn("Installing sqlite3 module globally (in Worker)", + "for use in the dev console.", sqlite3); + globalThis.sqlite3 = sqlite3; + const dbVfs = sqlite3.wasm.xWrap('fiddle_db_vfs', "*", ['string']); + fiddleModule.fsUnlink = (fn)=>fiddleModule.FS.unlink(fn); + wMsg('fiddle-ready'); + }).catch(e=>{ + console.error("Fiddle worker init failed:",e); + }); +})(); diff --git a/ext/wasm/fiddle/fiddle.js b/ext/wasm/fiddle/fiddle.js new file mode 100644 index 0000000000..84f125030c --- /dev/null +++ b/ext/wasm/fiddle/fiddle.js @@ -0,0 +1,894 @@ +/* + 2022-05-20 + + The author disclaims copyright to this source code. In place of a + legal notice, here is a blessing: + + * May you do good and not evil. + * May you find forgiveness for yourself and forgive others. + * May you share freely, never taking more than you give. + + *********************************************************************** + + This is the main entry point for the sqlite3 fiddle app. It sets up the + various UI bits, loads a Worker for the db connection, and manages the + communication between the UI and worker. +*/ +(function(){ + 'use strict'; + /* Recall that the 'self' symbol, except where locally + overwritten, refers to the global window or worker object. */ + + const storage = (function(NS/*namespace object in which to store this module*/){ + /* Pedantic licensing note: this code originated in the Fossil SCM + source tree, where it has a different license, but the person who + ported it into sqlite is the same one who wrote it for fossil. */ + 'use strict'; + NS = NS||{}; + + /** + This module provides a basic wrapper around localStorage + or sessionStorage or a dummy proxy object if neither + of those are available. + */ + const tryStorage = function f(obj){ + if(!f.key) f.key = 'storage.access.check'; + try{ + obj.setItem(f.key, 'f'); + const x = obj.getItem(f.key); + obj.removeItem(f.key); + if(x!=='f') throw new Error(f.key+" failed") + return obj; + }catch(e){ + return undefined; + } + }; + + /** Internal storage impl for this module. */ + const $storage = + tryStorage(window.localStorage) + || tryStorage(window.sessionStorage) + || tryStorage({ + // A basic dummy xyzStorage stand-in + $$$:{}, + setItem: function(k,v){this.$$$[k]=v}, + getItem: function(k){ + return this.$$$.hasOwnProperty(k) ? this.$$$[k] : undefined; + }, + removeItem: function(k){delete this.$$$[k]}, + clear: function(){this.$$$={}} + }); + + /** + For the dummy storage we need to differentiate between + $storage and its real property storage for hasOwnProperty() + to work properly... + */ + const $storageHolder = $storage.hasOwnProperty('$$$') ? $storage.$$$ : $storage; + + /** + A prefix which gets internally applied to all storage module + property keys so that localStorage and sessionStorage across the + same browser profile instance do not "leak" across multiple apps + being hosted by the same origin server. Such cross-polination is + still there but, with this key prefix applied, it won't be + immediately visible via the storage API. + + With this in place we can justify using localStorage instead of + sessionStorage. + + One implication of using localStorage and sessionStorage is that + their scope (the same "origin" and client application/profile) + allows multiple apps on the same origin to use the same + storage. Thus /appA/foo could then see changes made via + /appB/foo. The data do not cross user- or browser boundaries, + though, so it "might" arguably be called a + feature. storageKeyPrefix was added so that we can sandbox that + state for each separate app which shares an origin. + + See: https://fossil-scm.org/forum/forumpost/4afc4d34de + + Sidebar: it might seem odd to provide a key prefix and stick all + properties in the topmost level of the storage object. We do that + because adding a layer of object to sandbox each app would mean + (de)serializing that whole tree on every storage property change. + e.g. instead of storageObject.projectName.foo we have + storageObject[storageKeyPrefix+'foo']. That's soley for + efficiency's sake (in terms of battery life and + environment-internal storage-level effort). + */ + const storageKeyPrefix = ( + $storageHolder===$storage/*localStorage or sessionStorage*/ + ? ( + (NS.config ? + (NS.config.projectCode || NS.config.projectName + || NS.config.shortProjectName) + : false) + || window.location.pathname + )+'::' : ( + '' /* transient storage */ + ) + ); + + /** + A proxy for localStorage or sessionStorage or a + page-instance-local proxy, if neither one is available. + + Which exact storage implementation is uses is unspecified, and + apps must not rely on it. + */ + NS.storage = { + storageKeyPrefix: storageKeyPrefix, + /** Sets the storage key k to value v, implicitly converting + it to a string. */ + set: (k,v)=>$storage.setItem(storageKeyPrefix+k,v), + /** Sets storage key k to JSON.stringify(v). */ + setJSON: (k,v)=>$storage.setItem(storageKeyPrefix+k,JSON.stringify(v)), + /** Returns the value for the given storage key, or + dflt if the key is not found in the storage. */ + get: (k,dflt)=>$storageHolder.hasOwnProperty( + storageKeyPrefix+k + ) ? $storage.getItem(storageKeyPrefix+k) : dflt, + /** Returns true if the given key has a value of "true". If the + key is not found, it returns true if the boolean value of dflt + is "true". (Remember that JS persistent storage values are all + strings.) */ + getBool: function(k,dflt){ + return 'true'===this.get(k,''+(!!dflt)); + }, + /** Returns the JSON.parse()'d value of the given + storage key's value, or dflt is the key is not + found or JSON.parse() fails. */ + getJSON: function f(k,dflt){ + try { + const x = this.get(k,f); + return x===f ? dflt : JSON.parse(x); + } + catch(e){return dflt} + }, + /** Returns true if the storage contains the given key, + else false. */ + contains: (k)=>$storageHolder.hasOwnProperty(storageKeyPrefix+k), + /** Removes the given key from the storage. Returns this. */ + remove: function(k){ + $storage.removeItem(storageKeyPrefix+k); + return this; + }, + /** Clears ALL keys from the storage. Returns this. */ + clear: function(){ + this.keys().forEach((k)=>$storage.removeItem(/*w/o prefix*/k)); + return this; + }, + /** Returns an array of all keys currently in the storage. */ + keys: ()=>Object.keys($storageHolder).filter((v)=>(v||'').startsWith(storageKeyPrefix)), + /** Returns true if this storage is transient (only available + until the page is reloaded), indicating that fileStorage + and sessionStorage are unavailable. */ + isTransient: ()=>$storageHolder!==$storage, + /** Returns a symbolic name for the current storage mechanism. */ + storageImplName: function(){ + if($storage===window.localStorage) return 'localStorage'; + else if($storage===window.sessionStorage) return 'sessionStorage'; + else return 'transient'; + }, + + /** + Returns a brief help text string for the currently-selected + storage type. + */ + storageHelpDescription: function(){ + return { + localStorage: "Browser-local persistent storage with an "+ + "unspecified long-term lifetime (survives closing the browser, "+ + "but maybe not a browser upgrade).", + sessionStorage: "Storage local to this browser tab, "+ + "lost if this tab is closed.", + "transient": "Transient storage local to this invocation of this page." + }[this.storageImplName()]; + } + }; + return NS.storage; + })({})/*storage API setup*/; + + + /** Name of the stored copy of SqliteFiddle.config. */ + const configStorageKey = 'sqlite3-fiddle-config'; + + /** + The SqliteFiddle object is intended to be the primary + app-level object for the main-thread side of the sqlite + fiddle application. It uses a worker thread to load the + sqlite WASM module and communicate with it. + */ + const SF/*local convenience alias*/ + = window.SqliteFiddle/*canonical name*/ = { + /* Config options. */ + config: { + /* If true, SqliteFiddle.echo() will auto-scroll the + output widget to the bottom when it receives output, + else it won't. */ + autoScrollOutput: true, + /* If true, the output area will be cleared before each + command is run, else it will not. */ + autoClearOutput: false, + /* If true, SqliteFiddle.echo() will echo its output to + the console, in addition to its normal output widget. + That slows it down but is useful for testing. */ + echoToConsole: false, + /* If true, display input/output areas side-by-side. */ + sideBySide: true, + /* If true, swap positions of the input/output areas. */ + swapInOut: false + }, + /** + Emits the given text, followed by a line break, to the + output widget. If given more than one argument, they are + join()'d together with a space between each. As a special + case, if passed a single array, that array is used in place + of the arguments array (this is to facilitate receiving + lists of arguments via worker events). + */ + echo: function f(text) { + /* Maintenance reminder: we currently require/expect a textarea + output element. It might be nice to extend this to behave + differently if the output element is a non-textarea element, + in which case it would need to append the given text as a TEXT + node and add a line break. */ + if(!f._){ + f._ = document.getElementById('output'); + f._.value = ''; // clear browser cache + } + if(arguments.length > 1) text = Array.prototype.slice.call(arguments).join(' '); + else if(1===arguments.length && Array.isArray(text)) text = text.join(' '); + // These replacements are necessary if you render to raw HTML + //text = text.replace(/&/g, "&"); + //text = text.replace(//g, ">"); + //text = text.replace('\n', '
    ', 'g'); + if(null===text){/*special case: clear output*/ + f._.value = ''; + return; + }else if(this.echo._clearPending){ + delete this.echo._clearPending; + f._.value = ''; + } + if(this.config.echoToConsole) console.log(text); + if(this.jqTerm) this.jqTerm.echo(text); + f._.value += text + "\n"; + if(this.config.autoScrollOutput){ + f._.scrollTop = f._.scrollHeight; + } + }, + _msgMap: {}, + /** Adds a worker message handler for messages of the given + type. */ + addMsgHandler: function f(type,callback){ + if(Array.isArray(type)){ + type.forEach((t)=>this.addMsgHandler(t, callback)); + return this; + } + (this._msgMap.hasOwnProperty(type) + ? this._msgMap[type] + : (this._msgMap[type] = [])).push(callback); + return this; + }, + /** Given a worker message, runs all handlers for msg.type. */ + runMsgHandlers: function(msg){ + const list = (this._msgMap.hasOwnProperty(msg.type) + ? this._msgMap[msg.type] : false); + if(!list){ + console.warn("No handlers found for message type:",msg); + return false; + } + //console.debug("runMsgHandlers",msg); + list.forEach((f)=>f(msg)); + return true; + }, + /** Removes all message handlers for the given message type. */ + clearMsgHandlers: function(type){ + delete this._msgMap[type]; + return this; + }, + /* Posts a message in the form {type, data} to the db worker. Returns this. */ + wMsg: function(type,data,transferables){ + this.worker.postMessage({type, data}, transferables || []); + return this; + }, + /** + Prompts for confirmation and, if accepted, deletes + all content and tables in the (transient) database. + */ + resetDb: function(){ + if(window.confirm("Really destroy all content and tables " + +"in the (transient) db?")){ + this.wMsg('db-reset'); + } + return this; + }, + /** Stores this object's config in the browser's storage. */ + storeConfig: function(){ + storage.setJSON(configStorageKey,this.config); + } + }; + + if(1){ /* Restore SF.config */ + const storedConfig = storage.getJSON(configStorageKey); + if(storedConfig){ + /* Copy all properties to SF.config which are currently in + storedConfig. We don't bother copying any other + properties: those have been removed from the app in the + meantime. */ + Object.keys(SF.config).forEach(function(k){ + if(storedConfig.hasOwnProperty(k)){ + SF.config[k] = storedConfig[k]; + } + }); + } + } + + SF.worker = new Worker('fiddle-worker.js'+self.location.search); + SF.worker.onmessage = (ev)=>SF.runMsgHandlers(ev.data); + SF.addMsgHandler(['stdout', 'stderr'], (ev)=>SF.echo(ev.data)); + SF.addMsgHandler('sqlite-version', (ev)=>{ + const v = ev.data; + const a = E('#sqlite-version-link'); + const li = v.srcId.split(' ')/*DATE TIME HASH*/; + a.setAttribute('href', + //'https://sqlite.org/src/timeline/?c='+li[2].substr(0,20) + 'https://sqlite.org/src/info/'+li[2].substr(0,20) + ); + a.setAttribute('target', '_blank'); + a.innerText = [ + v.lib, + v.srcId.substr(0,34) + ].join(' '); + SF.echo("SQLite version",a.innerText); + }); + SF.addMsgHandler('wasm-info', (ev)=>{ + const v = ev.data; + SF.e.wasmInfo.innerText = 'WASM: '+( + 4===v.pointerSize ? 32 : 64 + )+'-bit' + //+' heap size: '+Number(v.heapSize) + // Heap size is not changing even when loading a huge db? + ; + }); + + /* querySelectorAll() proxy */ + const EAll = function(/*[element=document,] cssSelector*/){ + return (arguments.length>1 ? arguments[0] : document) + .querySelectorAll(arguments[arguments.length-1]); + }; + /* querySelector() proxy */ + const E = function(/*[element=document,] cssSelector*/){ + return (arguments.length>1 ? arguments[0] : document) + .querySelector(arguments[arguments.length-1]); + }; + + /** Handles status updates from the Emscripten Module object. */ + SF.addMsgHandler('module', function f(ev){ + ev = ev.data; + if('status'!==ev.type){ + console.warn("Unexpected module-type message:",ev); + return; + } + if(!f.ui){ + f.ui = { + status: E('#module-status'), + progress: E('#module-progress'), + spinner: E('#module-spinner') + }; + } + const msg = ev.data; + if(f.ui.progres){ + progress.value = msg.step; + progress.max = msg.step + 1/*we don't know how many steps to expect*/; + } + if(1==msg.step){ + f.ui.progress.classList.remove('hidden'); + f.ui.spinner.classList.remove('hidden'); + } + if(msg.text){ + f.ui.status.classList.remove('hidden'); + f.ui.status.innerText = msg.text; + }else{ + if(f.ui.progress){ + f.ui.progress.remove(); + f.ui.spinner.remove(); + delete f.ui.progress; + delete f.ui.spinner; + } + f.ui.status.classList.add('hidden'); + /* The module can post messages about fatal problems, + e.g. an exit() being triggered or assertion failure, + after the last "load" message has arrived, so + leave f.ui.status and message listener intact. */ + } + }); + + /** + The 'fiddle-ready' event is fired (with no payload) when the + wasm module has finished loading. Interestingly, that happens + _before_ the final module:status event */ + SF.addMsgHandler('fiddle-ready', function(){ + SF.clearMsgHandlers('fiddle-ready'); + self.onSFLoaded(); + }); + + SF.e ={ + about: E('#view-about'), + split: E('#view-split'), + wasmInfo: E('#opt-wasm-info'), + terminal: E('#view-terminal'), + hideInTerminal: EAll('.hide-in-terminal' + /* Elements to hide when in terminal mode */) + }; + SF.eViews = EAll('.app-view'); + SF.setMainView = function(eMain){ + if( SF.e.main === eMain ) return; + SF.eViews.forEach((e)=>{ + if( e===eMain ) e.classList.remove('hidden'); + else e.classList.add('hidden'); + }); + SF.e.hideInTerminal.forEach(e=>{ + if( eMain === SF.e.terminal ) e.classList.add('hidden'); + else e.classList.remove('hidden'); + }); + SF.e.main = eMain; + SF.ForceResizeKludge(); + }; + + /** Toggle the "About" view on and off. */ + SF.toggleAbout = function(){ + if( SF.e.about.classList.contains('hidden') ){ + SF.e.about.$returnTo = SF.e.main; + SF.setMainView( SF.e.about ); + }else{ + const e = SF.e.about.$returnTo; + delete SF.e.about.$returnTo; + SF.setMainView( e ); + } + }; + + /** + Given a DOM element, this routine measures its "effective + height", which is the bounding top/bottom range of this element + and all of its children, recursively. For some DOM structure + cases, a parent may have a reported height of 0 even though + children have non-0 sizes. + + Returns 0 if !e or if the element really has no height. + */ + const effectiveHeight = function f(e){ + if(!e) return 0; + if(!f.measure){ + f.measure = function callee(e, depth){ + if(!e) return; + const m = e.getBoundingClientRect(); + if(0===depth){ + callee.top = m.top; + callee.bottom = m.bottom; + }else{ + callee.top = m.top ? Math.min(callee.top, m.top) : callee.top; + callee.bottom = Math.max(callee.bottom, m.bottom); + } + Array.prototype.forEach.call(e.children,(e)=>callee(e,depth+1)); + if(0===depth){ + //console.debug("measure() height:",e.className, callee.top, callee.bottom, (callee.bottom - callee.top)); + f.extra += callee.bottom - callee.top; + } + return f.extra; + }; + } + f.extra = 0; + f.measure(e,0); + return f.extra; + }; + + /** + Returns a function, that, as long as it continues to be invoked, + will not be triggered. The function will be called after it stops + being called for N milliseconds. If `immediate` is passed, call + the callback immediately and hinder future invocations until at + least the given time has passed. + + If passed only 1 argument, or passed a falsy 2nd argument, + the default wait time set in this function's $defaultDelay + property is used. + + Source: underscore.js, by way of https://davidwalsh.name/javascript-debounce-function + */ + const debounce = function f(func, wait, immediate) { + var timeout; + if(!wait) wait = f.$defaultDelay; + return function() { + const context = this, args = Array.prototype.slice.call(arguments); + const later = function() { + timeout = undefined; + if(!immediate) func.apply(context, args); + }; + const callNow = immediate && !timeout; + clearTimeout(timeout); + timeout = setTimeout(later, wait); + if(callNow) func.apply(context, args); + }; + }; + debounce.$defaultDelay = 500 /*arbitrary*/; + + SF.ForceResizeKludge = (function(){ + /* Workaround for Safari mayhem regarding use of vh CSS + units.... We cannot use vh units to set the main view + size because Safari chokes on that, so we calculate + that height here. Larger than ~95% is too big for + Firefox on Android, causing the input area to move + off-screen. */ + const eVisibles = EAll('.app-view'); + const elemsToCount = [ + /* Elements which we need to always count in the + visible body size. */ + E('body > header'), + E('body > footer'), + E('body > fieldset.options') + ]; + const resized = function f(){ + if(f.$disabled) return; + const wh = window.innerHeight; + var ht; + var extra = 0; + elemsToCount.forEach((e)=>e ? extra += effectiveHeight(e) : false); + ht = wh - extra; + eVisibles.forEach(function(e){ + e.style.height = + e.style.maxHeight = [ + "calc(", (ht>=100 ? ht : 100), "px", + " - 2em"/*fudge value*/,")" + /* ^^^^ hypothetically not needed, but both + Chrome/FF on Linux will force scrollbars on the + body if this value is too small. */ + ].join(''); + }); + }; + resized.$disabled = true/*gets deleted when setup is finished*/; + window.addEventListener('resize', debounce(resized, 250), false); + return resized; + })(); + + /** + Performs all app initialization which must wait until after the + worker module is loaded. This function removes itself when it's + called. + */ + self.onSFLoaded = function(){ + delete this.onSFLoaded; + + // Unhide all elements which start out hidden + EAll('.initially-hidden').forEach((e)=>e.classList.remove('initially-hidden')); + SF.e.main = EAll('.app-view:not(.hidden)')[0] + /** The main view widget. Initially the first non-hidden + .app-view element. */; + if( (new URL(self.location.href).searchParams).has('about') ){ + SF.toggleAbout() /* for use while editing the About page */; + } + E('#btn-reset').addEventListener('click',()=>SF.resetDb()); + EAll('#btn-about, #btn-about-close').forEach((e)=>{ + e.addEventListener('click',()=>SF.toggleAbout()) + }); + const taInput = E('#input'); + const btnClearIn = E('#btn-clear'); + const selectExamples = E('#select-examples'); + btnClearIn.addEventListener('click',function(){ + taInput.value = ''; + selectExamples.selectedIndex = 0; + },false); + // Ctrl-enter and shift-enter both run the current SQL. + taInput.addEventListener('keydown',function(ev){ + if((ev.ctrlKey || ev.shiftKey) && 13 === ev.keyCode){ + ev.preventDefault(); + ev.stopPropagation(); + btnShellExec.click(); + } + }, false); + const taOutput = E('#output'); + const btnClearOut = E('#btn-clear-output'); + btnClearOut.addEventListener('click',function(){ + taOutput.value = ''; + if(SF.jqTerm) SF.jqTerm.clear(); + },false); + const btnShellExec = E('#btn-shell-exec'); + btnShellExec.addEventListener('click',function(ev){ + let sql; + ev.preventDefault(); + if(taInput.selectionStart e.addEventListener('click', cmdClick, false) + ); + + btnInterrupt.addEventListener('click',function(){ + SF.wMsg('interrupt'); + }); + + /** Initiate a download of the db. */ + const btnExport = E('#btn-export'); + const eLoadDb = E('#load-db'); + const btnLoadDb = E('#btn-load-db'); + btnLoadDb.addEventListener('click', ()=>eLoadDb.click()); + /** + Enables (if passed true) or disables all UI elements which + "might," if timed "just right," interfere with an + in-progress db import/export/exec operation. + */ + const enableMutatingElements = function f(enable){ + if(!f._elems){ + f._elems = [ + /* UI elements to disable while import/export are + running. Normally the export is fast enough + that this won't matter, but we really don't + want to be reading (from outside of sqlite) the + db when the user taps btnShellExec. */ + btnShellExec, btnExport, eLoadDb + ]; + } + f._elems.forEach( enable + ? (e)=>e.removeAttribute('disabled') + : (e)=>e.setAttribute('disabled','disabled') ); + }; + btnExport.addEventListener('click',function(){ + enableMutatingElements(false); + SF.wMsg('db-export'); + }); + SF.addMsgHandler('db-export', function(ev){ + enableMutatingElements(true); + ev = ev.data; + if(ev.error){ + SF.echo("Export failed:",ev.error); + return; + } + const blob = new Blob([ev.buffer], + {type:"application/x-sqlite3"}); + const a = document.createElement('a'); + document.body.appendChild(a); + a.href = window.URL.createObjectURL(blob); + a.download = ev.filename; + a.addEventListener('click',function(){ + setTimeout(function(){ + SF.echo("Exported (possibly auto-downloaded):",ev.filename); + window.URL.revokeObjectURL(a.href); + a.remove(); + },500); + }); + a.click(); + }); + /** + Handle load/import of an external db file. + */ + eLoadDb.addEventListener('change',function(){ + const f = this.files[0]; + const r = new FileReader(); + const status = {loaded: 0, total: 0}; + enableMutatingElements(false); + r.addEventListener('loadstart', function(){ + SF.echo("Loading",f.name,"..."); + }); + r.addEventListener('progress', function(ev){ + SF.echo("Loading progress:",ev.loaded,"of",ev.total,"bytes."); + }); + const that = this; + r.addEventListener('load', function(){ + enableMutatingElements(true); + SF.echo("Loaded",f.name+". Opening db..."); + SF.wMsg('open',{ + filename: f.name, + buffer: this.result + }, [this.result]); + }); + r.addEventListener('error',function(){ + enableMutatingElements(true); + SF.echo("Loading",f.name,"failed for unknown reasons."); + }); + r.addEventListener('abort',function(){ + enableMutatingElements(true); + SF.echo("Cancelled loading of",f.name+"."); + }); + r.readAsArrayBuffer(f); + }); + + EAll('fieldset.collapsible').forEach(function(fs){ + const btnToggle = E(fs,'legend > .fieldset-toggle'), + content = EAll(fs,':scope > div'); + btnToggle.addEventListener('click', function(){ + fs.classList.toggle('collapsed'); + content.forEach((d)=>d.classList.toggle('hidden')); + }, false); + }); + + /** Set up a selection list of examples */ + (function(){ + const xElem = E('#select-examples'); + const examples = [ + {name: "Help", sql: [ + "-- ================================================\n", + "-- Use ctrl-enter or shift-enter to execute sqlite3\n", + "-- shell commands and SQL.\n", + "-- If a subset of the text is currently selected,\n", + "-- only that part is executed.\n", + "-- ================================================\n", + ".help\n" + ]}, + //{name: "Timer on", sql: ".timer on"}, + // ^^^ re-enable if emscripten re-enables getrusage() + {name: "Box Mode", sql: ".mode box"}, + {name: "Setup table T", sql:[ + ".nullvalue NULL\n", + "CREATE TABLE t(a,b);\n", + "INSERT INTO t(a,b) VALUES('abc',123),('def',456),(NULL,789),('ghi',012);\n", + "SELECT * FROM t;\n" + ]}, + {name: "sqlite_schema", sql: "select * from sqlite_schema"}, + {name: "Mandelbrot", sql:[ + "WITH RECURSIVE", + " xaxis(x) AS (VALUES(-2.0) UNION ALL SELECT x+0.05 FROM xaxis WHERE x<1.2),\n", + " yaxis(y) AS (VALUES(-1.0) UNION ALL SELECT y+0.1 FROM yaxis WHERE y<1.0),\n", + " m(iter, cx, cy, x, y) AS (\n", + " SELECT 0, x, y, 0.0, 0.0 FROM xaxis, yaxis\n", + " UNION ALL\n", + " SELECT iter+1, cx, cy, x*x-y*y + cx, 2.0*x*y + cy FROM m \n", + " WHERE (x*x + y*y) < 4.0 AND iter<28\n", + " ),\n", + " m2(iter, cx, cy) AS (\n", + " SELECT max(iter), cx, cy FROM m GROUP BY cx, cy\n", + " ),\n", + " a(t) AS (\n", + " SELECT group_concat( substr(' .+*#', 1+min(iter/7,4), 1), '') \n", + " FROM m2 GROUP BY cy\n", + " )\n", + "SELECT group_concat(rtrim(t),x'0a') as Mandelbrot FROM a;\n", + ]}, + {name: "JSON pretty-print", + sql: "select json_pretty(json_object('ex',json('[52,3.14159]')))" + }, + {name: "JSON pretty-print (with tabs)", + sql: "select json_pretty(json_object('ex',json('[52,3.14159]')),char(0x09))" + } + ]; + const newOpt = function(lbl,val){ + const o = document.createElement('option'); + if(Array.isArray(val)) val = val.join(''); + o.value = val; + if(!val) o.setAttribute('disabled',true); + o.appendChild(document.createTextNode(lbl)); + xElem.appendChild(o); + }; + newOpt("Examples (replaces input!)"); + examples.forEach((o)=>newOpt(o.name, o.sql)); + //xElem.setAttribute('disabled',true); + xElem.selectedIndex = 0; + xElem.addEventListener('change', function(){ + taInput.value = '-- ' + + this.selectedOptions[0].innerText + + '\n' + this.value; + SF.dbExec(this.value); + }); + })()/* example queries */; + + //SF.echo(null/*clear any output generated by the init process*/); + if(window.jQuery && window.jQuery.terminal && SF.e.terminal){ + /* Set up the terminal-style view... */ + const jqeTerm = window.jQuery(SF.e.terminal).empty(); + SF.jqTerm = jqeTerm.terminal(SF.dbExec.bind(SF),{ + prompt: 'sqlite> ', + greetings: false /* note that the docs incorrectly call this 'greeting' */ + }); + EAll('.unhide-if-terminal-available').forEach(e=>{ + e.classList.remove('hidden'); + }); + EAll('.remove-if-terminal-available').forEach(e=>{ + e.parentElement.removeChild(e); + }); + /* Set up a button to toggle the views... */ + const ePlaceholder = E('#terminal-button-placeholder'); + ePlaceholder.classList.add('labeled-input'); + ePlaceholder.classList.remove('hidden'); + const btnToggleView = document.createElement('button'); + btnToggleView.innerText = "Toggle view"; + ePlaceholder.appendChild( btnToggleView ); + btnToggleView.addEventListener('click',function f(){ + const terminalOn = document.body.classList.toggle('terminal-mode'); + SF.setMainView( terminalOn ? SF.e.terminal : SF.e.split ); + }, false); + btnToggleView.click()/*default to terminal view*/; + } + const urlParams = new URL(self.location.href).searchParams; + SF.dbExec(urlParams.get('sql') || null); + delete SF.ForceResizeKludge.$disabled; + SF.ForceResizeKludge(); + }/*onSFLoaded()*/; +})(); diff --git a/ext/wasm/fiddle/index.html b/ext/wasm/fiddle/index.html new file mode 100644 index 0000000000..378cb39027 --- /dev/null +++ b/ext/wasm/fiddle/index.html @@ -0,0 +1,395 @@ + + + + + + SQLite3 Fiddle + + + + + + +
    + SQLite3 Fiddle + + Powered by + SQLite3 + +
    + +
    +
    +
    Initializing app...
    +
    + On a slow internet connection this may take a moment. If this + message displays for "a long time", intialization may have + failed and the JavaScript console may contain clues as to why. +
    +
    +
    Downloading...
    +
    + +
    + + + +
    +
    +
    +
    + + + + +
    +
    +
    +
    +
    + + + +
    +
    +
    +
    +
    + + + +
    + Options +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + WASM: ??? + +
    +
    + + + + diff --git a/ext/wasm/index-dist.html b/ext/wasm/index-dist.html new file mode 100644 index 0000000000..6c126e841d --- /dev/null +++ b/ext/wasm/index-dist.html @@ -0,0 +1,116 @@ + + + + + + + sqlite3 WASM Demo Page Index + + + +
    sqlite3 WASM demo pages
    +
    +
    Below is the list of demo pages for the sqlite3 WASM + builds. The intent is that this page be run + using the functional equivalent of:
    +
    althttpd -enable-sab -page index.html
    +
    and the individual pages be started in their own tab. + Warnings and Caveats: +
      +
    • All of these pages must be served via an HTTP + server. Browsers do not support loading WASM files via + file:// URLs.
    • +
    • Any OPFS-related pages or tests require: +
        +
      • An OPFS-capable browser released after February + 2023. Some tests will work with Chromium-based browsers + going back to around v102.
      • +
      • That the web server emit the so-called + COOP + and + COEP + headers. althttpd requires the + -enable-sab flag for that. +
      • +
      +
    • +
    +
    +
    The tests and demos... +
      +
    • Core-most tests +
        +
      • tester1: Core unit and + regression tests for the various APIs and surrounding + utility code.
      • +
      • tester1-worker: same thing + but running in a Worker.
      • +
      • tester1-esm: same as + tester1 but loads sqlite3 in the main thread via + an ES6 module. +
      • +
      • tester1-worker?esm: + same as tester1-esm but loads a Worker Module which + then loads the sqlite3 API via an ES6 module. Note that + not all browsers permit loading modules in Worker + threads. +
      • +
      +
    • +
    • Higher-level apps and demos... +
        +
      • demo-123 provides a + no-nonsense example of adding sqlite3 support to a web + page in the UI thread.
      • +
      • demo-123-worker is + the same as demo-123 but loads and runs + sqlite3 from a Worker thread.
      • +
      • demo-jsstorage: very basic + demo of using the key-value VFS for storing a persistent db + in JS localStorage or sessionStorage.
      • +
      • demo-worker1: + Worker-based wrapper of the OO API #1. Its Promise-based + wrapper is significantly easier to use, however.
      • +
      • demo-worker1-promiser: + a demo of the Promise-based wrapper of the Worker1 API.
      • +
      • module-symbols gives + a high-level overview of the symbols exposed by the JS + module.
      • +
      +
    • +
    +
    + + + + diff --git a/ext/wasm/index.html b/ext/wasm/index.html new file mode 100644 index 0000000000..55e4cdb75c --- /dev/null +++ b/ext/wasm/index.html @@ -0,0 +1,180 @@ + + + + + + + + sqlite3 WASM Testing Page Index + + + +
    sqlite3 WASM test pages
    +
    +
    Below is the list of test pages for the sqlite3 WASM + builds. All of them require that this directory have been + "make"d first. The intent is that this page be run + using:
    +
    althttpd -enable-sab -page index.html
    +
    and the individual tests be started in their own tab. + Warnings and Caveats: +
      +
    • All of these pages must be served via an HTTP + server. Browsers do not support loading WASM files via + file:// URLs.
    • +
    • Any OPFS-related pages or tests require: +
        +
      • An OPFS-capable browser released after February + 2023. Some tests will work with Chromium-based browsers + going back to around v102.
      • +
      • That the web server emit the so-called + COOP + and + COEP + headers. althttpd requires the + -enable-sab flag for that. +
      • +
      +
    • +
    +
    +
    The tests and demos... +
      +
    • Core-most tests +
        +
      • tester1 (32-bit + 64-bit): Core unit and + regression tests for the various APIs and surrounding + utility code.
      • +
      • tester1-esm (32-bit + 64-bit): same as + tester1 but loads sqlite3 in the main thread via + an ES6 module. +
      • +
      • tester1-worker (32-bit + 64-bit): same thing + but running in a Worker.
      • +
      • tester1-worker ESM (32-bit + 64-bit): same as + tester1-worker but loads a Worker Module which then + loads the sqlite3 API via an ES6 module. Not all + browsers permit loading modules in Worker threads. +
      • +
      +
    • +
    • High-level apps and demos... +
        +
      • fiddle is an HTML front-end + to a wasm build of the sqlite3 shell.
      • +
      • demo-123 provides a + no-nonsense example of adding sqlite3 support to a web + page in the UI thread.
      • +
      • demo-123-worker is + the same as demo-123 but loads and runs + sqlite3 from a Worker thread.
      • +
      • demo-jsstorage: very basic + demo of using the key-value VFS for storing a persistent db + in JS localStorage or sessionStorage.
      • +
      • demo-worker1: + Worker-based wrapper of the OO API #1. Its Promise-based + wrapper is significantly easier to use, however.
      • +
      • demo-worker1-promiser: + a demo of the Promise-based wrapper of the Worker1 API.
      • +
      • demo-worker1-promiser-esm: + same as the previous demo except loads the promiser from an ESM module.
      • +
      +
    • +
    • speedtest1 ports (sqlite3's primary benchmarking tool)... +
        +
      • speedtest1 + (32-bit, + 64-bit): + a main-thread WASM build of speedtest1.
      • +
      • speedtest1?vfs=kvvfs + (32-bit, + 64-bit): + speedtest1 with the kvvfs.
      • +
      • speedtest1-worker + (32-bit, + 64-bit): + an interactive Worker-thread variant of speedtest1.
      • +
      • speedtest1-worker?vfs=opfs + (32-bit, + 64-bit): + speedtest1-worker with the + OPFS VFS preselected and configured for a moderate workload.
      • +
      • speedtest1-worker?vfs=opfs-sahpool + (32-bit, + 64-bit): + speedtest1-worker with the OPFS-SAHPOOL VFS preselected + and configured for a moderate workload. +
      • +
      +
    • +
    • The obligatory "misc." category... +
        +
      • module-symbols gives + a high-level overview of the symbols exposed by the JS + module.
      • + +
      • test-opfs-vfs + (same + with verbose output and sanity-checking tests) is an + sqlite3_vfs OPFS proxy using SharedArrayBuffer and the + Atomics APIs to regulate communication between the + synchronous sqlite3_vfs interface and the async OPFS + impl. +
      • +
      • OPFS concurrency + tests using multiple workers. +
      • +
      • OPFS SAHPool cooperative semi-concurrency + demonstrates usage of the OPFS SAHPool VFS's "pause" feature to coordinate + access to a database. +
      • +
      • SQLTester is + another testing tool for the library but making use of + it requires tests from an external, proprietary + project. Despite not being useful to the general public, + a link to it is in this list so that its developer has + easy access to it. +
      • +
      +
    • +
    • WASMFS-specific tests which require that + the WASMFS build is available on this server (it is not by + default) and that this server emits the COOP/COEP headers. + +
    • + +
    +
    + + + + diff --git a/ext/wasm/jaccwabyt/jaccwabyt.js b/ext/wasm/jaccwabyt/jaccwabyt.js new file mode 100644 index 0000000000..8ea08e2136 --- /dev/null +++ b/ext/wasm/jaccwabyt/jaccwabyt.js @@ -0,0 +1,737 @@ +/** + 2022-06-30 + + The author disclaims copyright to this source code. In place of a + legal notice, here is a blessing: + + * May you do good and not evil. + * May you find forgiveness for yourself and forgive others. + * May you share freely, never taking more than you give. + + *********************************************************************** + + The Jaccwabyt API is documented in detail in an external file, + _possibly_ called jaccwabyt.md in the same directory as this file. + + Project homes: + - https://fossil.wanderinghorse.net/r/jaccwabyt + - https://sqlite.org/src/dir/ext/wasm/jaccwabyt +*/ +'use strict'; +globalThis.Jaccwabyt = function StructBinderFactory(config){ +/* ^^^^ it is recommended that clients move that object into wherever + they'd like to have it and delete the globalThis-held copy. This + API does not require the global reference - it is simply installed + as a convenience for connecting these bits to other co-developed + code before it gets removed from the global namespace. +*/ + + /** Throws a new Error, the message of which is the concatenation + all args with a space between each. */ + const toss = (...args)=>{throw new Error(args.join(' '))}; + + if(!(config.heap instanceof WebAssembly.Memory) + && !(config.heap instanceof Function)){ + toss("config.heap must be WebAssembly.Memory instance or a function."); + } + ['alloc','dealloc'].forEach(function(k){ + (config[k] instanceof Function) || + toss("Config option '"+k+"' must be a function."); + }); + const __heap = config.heap; + const SBF = StructBinderFactory; + const heap = __heap ? __heap : ()=>new Uint8Array(__heap.buffer), + alloc = config.alloc, + dealloc = config.dealloc, + log = config.log || console.debug.bind(console), + memberPrefix = (config.memberPrefix || ""), + memberSuffix = (config.memberSuffix || ""), + BigInt = globalThis['BigInt'], + BigInt64Array = globalThis['BigInt64Array'], + bigIntEnabled = config.bigIntEnabled ?? !!BigInt64Array, + ptrIR = config.pointerIR + || config.ptrIR/*deprecated*/ + || 'i32', + /* Undocumented (on purpose) config options: */ + ptrSize = config.ptrSize/*deprecated*/ + || ('i32'===ptrIR ? 4 : 8) + ; + + if(ptrIR!=='i32' && ptrIR!=='i64') toss("Invalid pointer representation:",ptrIR); + if(ptrSize!==4 && ptrSize!==8) toss("Invalid pointer size:",ptrSize); + + /** Either BigInt or, if !bigIntEnabled, a function which + throws complaining that BigInt is not enabled. */ + const __BigInt = (bigIntEnabled && BigInt) + ? (v)=>BigInt(v || 0) + : (v)=>toss("BigInt support is disabled in this build."); + const __asPtrType = ('i32'==ptrIR) ? Number : __BigInt; + const __NullPtr = __asPtrType(0); + + /** + Expects any number of numeric arguments, each one of either type + Number or BigInt. It sums them up (from an implicit starting + point of 0 or 0n) and returns them as a number of the same type + which target.asPtrType() uses. + + This is a workaround for not being able to mix Number/BigInt in + addition/subtraction expressions (which we frequently need for + calculating pointer offsets). + */ + const __ptrAdd = function(...args){ + let rc = __NullPtr; + for( let i = 0; i < args.length; ++i ){ + rc += __asPtrType(args[i]); + } + return rc; + }; + + if(!SBF.debugFlags){ + SBF.__makeDebugFlags = function(deriveFrom=null){ + /* This is disgustingly overengineered. :/ */ + if(deriveFrom && deriveFrom.__flags) deriveFrom = deriveFrom.__flags; + const f = function f(flags){ + if(0===arguments.length){ + return f.__flags; + } + if(flags<0){ + delete f.__flags.getter; delete f.__flags.setter; + delete f.__flags.alloc; delete f.__flags.dealloc; + }else{ + f.__flags.getter = 0!==(0x01 & flags); + f.__flags.setter = 0!==(0x02 & flags); + f.__flags.alloc = 0!==(0x04 & flags); + f.__flags.dealloc = 0!==(0x08 & flags); + } + return f._flags; + }; + Object.defineProperty(f,'__flags', { + iterable: false, writable: false, + value: Object.create(deriveFrom) + }); + if(!deriveFrom) f(0); + return f; + }; + SBF.debugFlags = SBF.__makeDebugFlags(); + }/*static init*/ + + const isLittleEndian = (function() { + const buffer = new ArrayBuffer(2); + new DataView(buffer).setInt16(0, 256, true /* littleEndian */); + // Int16Array uses the platform's endianness. + return new Int16Array(buffer)[0] === 256; + })(); + + /** + Some terms used in the internal docs: + + StructType: a struct-wrapping class generated by this + framework. + + DEF: struct description object. + + SIG: struct member signature string. + */ + + /** True if SIG s looks like a function signature, else + false. */ + const isFuncSig = (s)=>'('===s[1]; + /** True if SIG s is-a pointer signature. */ + const isPtrSig = (s)=>'p'===s || 'P'===s; + const isAutoPtrSig = (s)=>'P'===s /*EXPERIMENTAL*/; + /** Returns p if SIG s is a function SIG, else returns s[0]. */ + const sigLetter = (s)=>isFuncSig(s) ? 'p' : s[0]; + /** Returns the WASM IR form of the Emscripten-conventional letter + at SIG s[0]. Throws for an unknown SIG. */ + const sigIR = function(s){ + switch(sigLetter(s)){ + case 'c': case 'C': return 'i8'; + case 'i': return 'i32'; + case 'p': case 'P': case 's': return ptrIR; + case 'j': return 'i64'; + case 'f': return 'float'; + case 'd': return 'double'; + } + toss("Unhandled signature IR:",s); + }; + + const affirmBigIntArray = BigInt64Array + ? ()=>true : ()=>toss('BigInt64Array is not available.'); + /** Returns the name of a DataView getter method corresponding + to the given SIG. */ + const sigDVGetter = function(s){ + switch(sigLetter(s)) { + case 'p': case 'P': case 's': { + switch(ptrSize){ + case 4: return 'getInt32'; + case 8: return affirmBigIntArray() && 'getBigInt64'; + } + break; + } + case 'i': return 'getInt32'; + case 'c': return 'getInt8'; + case 'C': return 'getUint8'; + case 'j': return affirmBigIntArray() && 'getBigInt64'; + case 'f': return 'getFloat32'; + case 'd': return 'getFloat64'; + } + toss("Unhandled DataView getter for signature:",s); + }; + /** Returns the name of a DataView setter method corresponding + to the given SIG. */ + const sigDVSetter = function(s){ + switch(sigLetter(s)){ + case 'p': case 'P': case 's': { + switch(ptrSize){ + case 4: return 'setInt32'; + case 8: return affirmBigIntArray() && 'setBigInt64'; + } + break; + } + case 'i': return 'setInt32'; + case 'c': return 'setInt8'; + case 'C': return 'setUint8'; + case 'j': return affirmBigIntArray() && 'setBigInt64'; + case 'f': return 'setFloat32'; + case 'd': return 'setFloat64'; + } + toss("Unhandled DataView setter for signature:",s); + }; + + /** + Returns a factory for either Number or BigInt, depending on the + given SIG. This constructor is used in property setters to coerce + the being-set value to the correct pointer size. + */ + const sigDVSetWrapper = function(s){ + switch(sigLetter(s)) { + case 'i': case 'f': case 'c': case 'C': case 'd': return Number; + case 'j': return __BigInt; + case 'p': case 'P': case 's': + switch(ptrSize){ + case 4: return Number; + case 8: return __BigInt; + } + break; + } + toss("Unhandled DataView set wrapper for signature:",s); + }; + + /** Returns the given struct and member name in a form suitable for + debugging and error output. */ + const sPropName = (s,k)=>s+'::'+k; + + const __propThrowOnSet = function(structName,propName){ + return ()=>toss(sPropName(structName,propName),"is read-only."); + }; + + /** + In order to completely hide StructBinder-bound struct pointers + from JS code, we store them in a scope-local WeakMap which maps + the struct-bound objects to their WASM pointers. The pointers are + accessible via boundObject.pointer, which is gated behind a + property interceptor, but are not exposed anywhere else in the + object. + */ + const __instancePointerMap = new WeakMap(); + + /** Property name for the pointer-is-external marker. */ + const xPtrPropName = '(pointer-is-external)'; + + const __isPtr32 = (ptr)=>('number'===typeof ptr && (ptr===(ptr|0)) && ptr>=0); + const __isPtr64 = (ptr)=>( + ('bigint'===typeof ptr && ptr >= 0) || __isPtr32(ptr) + ); + + /** + isPtr() is an alias for isPtr32() or isPtr64(), depending on + ptrSize. + */ + const __isPtr = (4===ptrSize) ? __isPtr32 : __isPtr64; + + /** Frees the obj.pointer memory and clears the pointer + property. */ + const __freeStruct = function(ctor, obj, m){ + if(!m) m = __instancePointerMap.get(obj); + if(m) { + __instancePointerMap.delete(obj); + if(Array.isArray(obj.ondispose)){ + let x; + while((x = obj.ondispose.shift())){ + try{ + if(x instanceof Function) x.call(obj); + else if(x instanceof StructType) x.dispose(); + else if(__isPtr(x)) dealloc(x); + // else ignore. Strings are permitted to annotate entries + // to assist in debugging. + }catch(e){ + console.warn("ondispose() for",ctor.structName,'@', + m,'threw. NOT propagating it.',e); + } + } + }else if(obj.ondispose instanceof Function){ + try{obj.ondispose()} + catch(e){ + /*do not rethrow: destructors must not throw*/ + console.warn("ondispose() for",ctor.structName,'@', + m,'threw. NOT propagating it.',e); + } + } + delete obj.ondispose; + if(ctor.debugFlags.__flags.dealloc){ + log("debug.dealloc:",(obj[xPtrPropName]?"EXTERNAL":""), + ctor.structName,"instance:", + ctor.structInfo.sizeof,"bytes @"+m); + } + if(!obj[xPtrPropName]) dealloc(m); + } + }; + + /** Returns a skeleton for a read-only property accessor wrapping + value v. */ + const rop = (v)=>{return {configurable: false, writable: false, + iterable: false, value: v}}; + + /** Allocates obj's memory buffer based on the size defined in + ctor.structInfo.sizeof. */ + const __allocStruct = function(ctor, obj, m){ + let fill = !m; + if(m) Object.defineProperty(obj, xPtrPropName, rop(m)); + else{ + m = alloc(ctor.structInfo.sizeof); + if(!m) toss("Allocation of",ctor.structName,"structure failed."); + } + try { + if(ctor.debugFlags.__flags.alloc){ + log("debug.alloc:",(fill?"":"EXTERNAL"), + ctor.structName,"instance:", + ctor.structInfo.sizeof,"bytes @"+m); + } + if(fill){ + heap().fill(0, Number(m), Number(m) + ctor.structInfo.sizeof); + } + __instancePointerMap.set(obj, m); + }catch(e){ + __freeStruct(ctor, obj, m); + throw e; + } + }; + /** Gets installed as the memoryDump() method of all structs. */ + const __memoryDump = function(){ + const p = this.pointer; + return p + ? new Uint8Array(heap().slice(Number(p), Number(p) + this.structInfo.sizeof)) + : null; + }; + + const __memberKey = (k)=>memberPrefix + k + memberSuffix; + const __memberKeyProp = rop(__memberKey); + + /** + Looks up a struct member in structInfo.members. Throws if found + if tossIfNotFound is true, else returns undefined if not + found. The given name may be either the name of the + structInfo.members key (faster) or the key as modified by the + memberPrefix and memberSuffix settings. + */ + const __lookupMember = function(structInfo, memberName, tossIfNotFound=true){ + let m = structInfo.members[memberName]; + if(!m && (memberPrefix || memberSuffix)){ + // Check for a match on members[X].key + for(const v of Object.values(structInfo.members)){ + if(v.key===memberName){ m = v; break; } + } + if(!m && tossIfNotFound){ + toss(sPropName(structInfo.name,memberName),'is not a mapped struct member.'); + } + } + return m; + }; + + /** + Uses __lookupMember(obj.structInfo,memberName) to find a member, + throwing if not found. Returns its signature, either in this + framework's native format or in Emscripten format. + */ + const __memberSignature = function f(obj,memberName,emscriptenFormat=false){ + if(!f._) f._ = (x)=>x.replace(/[^vipPsjrdcC]/g,"").replace(/[pPscC]/g,'i'); + const m = __lookupMember(obj.structInfo, memberName, true); + return emscriptenFormat ? f._(m.signature) : m.signature; + }; + + const __ptrPropDescriptor = { + configurable: false, enumerable: false, + get: function(){return __instancePointerMap.get(this)}, + set: ()=>toss("Cannot assign the 'pointer' property of a struct.") + // Reminder: leaving `set` undefined makes assignments + // to the property _silently_ do nothing. Current unit tests + // rely on it throwing, though. + }; + + /** Impl of X.memberKeys() for StructType and struct ctors. */ + const __structMemberKeys = rop(function(){ + const a = []; + for(const k of Object.keys(this.structInfo.members)){ + a.push(this.memberKey(k)); + } + return a; + }); + + const __utf8Decoder = new TextDecoder('utf-8'); + const __utf8Encoder = new TextEncoder(); + /** Internal helper to use in operations which need to distinguish + between SharedArrayBuffer heap memory and non-shared heap. */ + const __SAB = ('undefined'===typeof SharedArrayBuffer) + ? function(){} : SharedArrayBuffer; + const __utf8Decode = function(arrayBuffer, begin, end){ + if( 8===ptrSize ){ + begin = Number(begin); + end = Number(end); + } + return __utf8Decoder.decode( + (arrayBuffer.buffer instanceof __SAB) + ? arrayBuffer.slice(begin, end) + : arrayBuffer.subarray(begin, end) + ); + }; + + /** + Uses __lookupMember() to find the given obj.structInfo key. + Returns that member if it is a string, else returns false. If the + member is not found, throws if tossIfNotFound is true, else + returns false. + */ + const __memberIsString = function(obj,memberName, tossIfNotFound=false){ + const m = __lookupMember(obj.structInfo, memberName, tossIfNotFound); + return (m && 1===m.signature.length && 's'===m.signature[0]) ? m : false; + }; + + /** + Given a member description object, throws if member.signature is + not valid for assigning to or interpretation as a C-style string. + It optimistically assumes that any signature of (i,p,s) is + C-string compatible. + */ + const __affirmCStringSignature = function(member){ + if('s'===member.signature) return; + toss("Invalid member type signature for C-string value:", + JSON.stringify(member)); + }; + + /** + Looks up the given member in obj.structInfo. If it has a + signature of 's' then it is assumed to be a C-style UTF-8 string + and a decoded copy of the string at its address is returned. If + the signature is of any other type, it throws. If an s-type + member's address is 0, `null` is returned. + */ + const __memberToJsString = function f(obj,memberName){ + const m = __lookupMember(obj.structInfo, memberName, true); + __affirmCStringSignature(m); + const addr = obj[m.key]; + //log("addr =",addr,memberName,"m =",m); + if(!addr) return null; + let pos = addr; + const mem = heap(); + for( ; mem[pos]!==0; ++pos ) { + //log("mem[",pos,"]",mem[pos]); + }; + //log("addr =",addr,"pos =",pos); + return (addr===pos) ? "" : __utf8Decode(mem, addr, pos); + }; + + /** + Adds value v to obj.ondispose, creating ondispose, + or converting it to an array, if needed. + */ + const __addOnDispose = function(obj, ...v){ + if(obj.ondispose){ + if(!Array.isArray(obj.ondispose)){ + obj.ondispose = [obj.ondispose]; + } + }else{ + obj.ondispose = []; + } + obj.ondispose.push(...v); + }; + + /** + Allocates a new UTF-8-encoded, NUL-terminated copy of the given + JS string and returns its address relative to heap(). If + allocation returns 0 this function throws. Ownership of the + memory is transfered to the caller, who must eventually pass it + to the configured dealloc() function. + */ + const __allocCString = function(str){ + const u = __utf8Encoder.encode(str); + const mem = alloc(u.length+1); + if(!mem) toss("Allocation error while duplicating string:",str); + const h = heap(); + //let i = 0; + //for( ; i < u.length; ++i ) h[mem + i] = u[i]; + h.set(u, Number(mem)); + h[__ptrAdd(mem, u.length)] = 0; + //log("allocCString @",mem," =",u); + return mem; + }; + + /** + Sets the given struct member of obj to a dynamically-allocated, + UTF-8-encoded, NUL-terminated copy of str. It is up to the caller + to free any prior memory, if appropriate. The newly-allocated + string is added to obj.ondispose so will be freed when the object + is disposed. + + The given name may be either the name of the structInfo.members + key (faster) or the key as modified by the memberPrefix and + memberSuffix settings. + */ + const __setMemberCString = function(obj, memberName, str){ + const m = __lookupMember(obj.structInfo, memberName, true); + __affirmCStringSignature(m); + /* Potential TODO: if obj.ondispose contains obj[m.key] then + dealloc that value and clear that ondispose entry */ + const mem = __allocCString(str); + obj[m.key] = mem; + __addOnDispose(obj, mem); + return obj; + }; + + /** + Prototype for all StructFactory instances (the constructors + returned from StructBinder). + */ + const StructType = function ctor(structName, structInfo){ + if(arguments[2]!==rop){ + toss("Do not call the StructType constructor", + "from client-level code."); + } + Object.defineProperties(this,{ + //isA: rop((v)=>v instanceof ctor), + structName: rop(structName), + structInfo: rop(structInfo) + }); + }; + + /** + Properties inherited by struct-type-specific StructType instances + and (indirectly) concrete struct-type instances. + */ + StructType.prototype = Object.create(null, { + dispose: rop(function(){__freeStruct(this.constructor, this)}), + lookupMember: rop(function(memberName, tossIfNotFound=true){ + return __lookupMember(this.structInfo, memberName, tossIfNotFound); + }), + memberToJsString: rop(function(memberName){ + return __memberToJsString(this, memberName); + }), + memberIsString: rop(function(memberName, tossIfNotFound=true){ + return __memberIsString(this, memberName, tossIfNotFound); + }), + memberKey: __memberKeyProp, + memberKeys: __structMemberKeys, + memberSignature: rop(function(memberName, emscriptenFormat=false){ + return __memberSignature(this, memberName, emscriptenFormat); + }), + memoryDump: rop(__memoryDump), + pointer: __ptrPropDescriptor, + setMemberCString: rop(function(memberName, str){ + return __setMemberCString(this, memberName, str); + }) + }); + // Function-type non-Property inherited members + Object.assign(StructType.prototype,{ + addOnDispose: function(...v){ + __addOnDispose(this,...v); + return this; + } + }); + + /** + "Static" properties for StructType. + */ + Object.defineProperties(StructType, { + allocCString: rop(__allocCString), + isA: rop((v)=>v instanceof StructType), + hasExternalPointer: rop((v)=>(v instanceof StructType) && !!v[xPtrPropName]), + memberKey: __memberKeyProp + }); + + /** + Pass this a StructBinder-generated prototype, and the struct + member description object. It will define property accessors for + proto[memberKey] which read from/write to memory in + this.pointer. It modifies descr to make certain downstream + operations much simpler. + */ + const makeMemberWrapper = function f(ctor,name, descr){ + if(!f._){ + /*cache all available getters/setters/set-wrappers for + direct reuse in each accessor function. */ + f._ = {getters: {}, setters: {}, sw:{}}; + const a = ['i','c','C','p','P','s','f','d','v()']; + if(bigIntEnabled) a.push('j'); + a.forEach(function(v){ + //const ir = sigIR(v); + f._.getters[v] = sigDVGetter(v) /* DataView[MethodName] values for GETTERS */; + f._.setters[v] = sigDVSetter(v) /* DataView[MethodName] values for SETTERS */; + f._.sw[v] = sigDVSetWrapper(v) /* BigInt or Number ctor to wrap around values + for conversion */; + }); + const rxSig1 = /^[ipPsjfdcC]$/, + rxSig2 = /^[vipPsjfdcC]\([ipPsjfdcC]*\)$/; + f.sigCheck = function(obj, name, key,sig){ + if(Object.prototype.hasOwnProperty.call(obj, key)){ + toss(obj.structName,'already has a property named',key+'.'); + } + rxSig1.test(sig) || rxSig2.test(sig) + || toss("Malformed signature for", + sPropName(obj.structName,name)+":",sig); + }; + } + const key = ctor.memberKey(name); + f.sigCheck(ctor.prototype, name, key, descr.signature); + descr.key = key; + descr.name = name; + const sigGlyph = sigLetter(descr.signature); + const xPropName = sPropName(ctor.prototype.structName,key); + const dbg = ctor.prototype.debugFlags.__flags; + /* + TODO?: set prototype of descr to an object which can set/fetch + its preferred representation, e.g. conversion to string or mapped + function. Advantage: we can avoid doing that via if/else if/else + in the get/set methods. + */ + const prop = Object.create(null); + prop.configurable = false; + prop.enumerable = false; + prop.get = function(){ + if(dbg.getter){ + log("debug.getter:",f._.getters[sigGlyph],"for", sigIR(sigGlyph), + xPropName,'@', this.pointer,'+',descr.offset,'sz',descr.sizeof); + } + let rc = ( + new DataView(heap().buffer, Number(this.pointer) + descr.offset, descr.sizeof) + )[f._.getters[sigGlyph]](0, isLittleEndian); + if(dbg.getter) log("debug.getter:",xPropName,"result =",rc); + return rc; + }; + if(descr.readOnly){ + prop.set = __propThrowOnSet(ctor.prototype.structName,key); + }else{ + prop.set = function(v){ + if(dbg.setter){ + log("debug.setter:",f._.setters[sigGlyph],"for", sigIR(sigGlyph), + xPropName,'@', this.pointer,'+',descr.offset,'sz',descr.sizeof, v); + } + if(!this.pointer){ + toss("Cannot set struct property on disposed instance."); + } + if(null===v) v = __NullPtr; + else while(!__isPtr(v)){ + if(isAutoPtrSig(descr.signature) && (v instanceof StructType)){ + // It's a struct instance: let's store its pointer value! + v = v.pointer || __NullPtr; + if(dbg.setter) log("debug.setter:",xPropName,"resolved to",v); + break; + } + toss("Invalid value for pointer-type",xPropName+'.'); + } + ( + new DataView(heap().buffer, Number(this.pointer) + descr.offset, + descr.sizeof) + )[f._.setters[sigGlyph]](0, f._.sw[sigGlyph](v), isLittleEndian); + }; + } + Object.defineProperty(ctor.prototype, key, prop); + }/*makeMemberWrapper*/; + + /** + The main factory function which will be returned to the + caller. + */ + const StructBinder = function StructBinder(structName, structInfo){ + if(1===arguments.length){ + structInfo = structName; + structName = structInfo.name; + }else if(!structInfo.name){ + structInfo.name = structName; + } + if(!structName) toss("Struct name is required."); + let lastMember = false; + Object.keys(structInfo.members).forEach((k)=>{ + // Sanity checks of sizeof/offset info... + const m = structInfo.members[k]; + if(!m.sizeof) toss(structName,"member",k,"is missing sizeof."); + else if(m.sizeof===1){ + (m.signature === 'c' || m.signature === 'C') || + toss("Unexpected sizeof==1 member", + sPropName(structInfo.name,k), + "with signature",m.signature); + }else{ + // sizes and offsets of size-1 members may be odd values, but + // others may not. + if(0!==(m.sizeof%4)){ + console.warn("Invalid struct member description =",m,"from",structInfo); + toss(structName,"member",k,"sizeof is not aligned. sizeof="+m.sizeof); + } + if(0!==(m.offset%4)){ + console.warn("Invalid struct member description =",m,"from",structInfo); + toss(structName,"member",k,"offset is not aligned. offset="+m.offset); + } + } + if(!lastMember || lastMember.offset < m.offset) lastMember = m; + }); + if(!lastMember) toss("No member property descriptions found."); + else if(structInfo.sizeof < lastMember.offset+lastMember.sizeof){ + toss("Invalid struct config:",structName, + "max member offset ("+lastMember.offset+") ", + "extends past end of struct (sizeof="+structInfo.sizeof+")."); + } + const debugFlags = rop(SBF.__makeDebugFlags(StructBinder.debugFlags)); + /** Constructor for the StructCtor. */ + const zeroAsPtr = __asPtrType(0); + const StructCtor = function StructCtor(externalMemory){ + externalMemory = __asPtrType(externalMemory); + //console.warn("externalMemory",externalMemory,arguments[0]); + if(!(this instanceof StructCtor)){ + toss("The",structName,"constructor may only be called via 'new'."); + }else if(arguments.length){ + if(Number.isNaN(externalMemory) || externalMemory<=zeroAsPtr){ + toss("Invalid pointer value",arguments[0],"for",structName,"constructor."); + } + __allocStruct(StructCtor, this, externalMemory); + }else{ + __allocStruct(StructCtor, this); + } + }; + Object.defineProperties(StructCtor,{ + debugFlags: debugFlags, + isA: rop((v)=>v instanceof StructCtor), + memberKey: __memberKeyProp, + memberKeys: __structMemberKeys, + methodInfoForKey: rop(function(mKey){ + }), + structInfo: rop(structInfo), + structName: rop(structName) + }); + StructCtor.prototype = new StructType(structName, structInfo, rop); + Object.defineProperties(StructCtor.prototype,{ + debugFlags: debugFlags, + constructor: rop(StructCtor) + /*if we assign StructCtor.prototype and don't do + this then StructCtor!==instance.constructor!*/ + }); + Object.keys(structInfo.members).forEach( + (name)=>makeMemberWrapper(StructCtor, name, structInfo.members[name]) + ); + return StructCtor; + }; + StructBinder.StructType = StructType; + StructBinder.config = config; + StructBinder.allocCString = __allocCString; + if(!StructBinder.debugFlags){ + StructBinder.debugFlags = SBF.__makeDebugFlags(SBF.debugFlags); + } + return StructBinder; +}/*StructBinderFactory*/; diff --git a/ext/wasm/jaccwabyt/jaccwabyt.md b/ext/wasm/jaccwabyt/jaccwabyt.md new file mode 100644 index 0000000000..5ec3151d50 --- /dev/null +++ b/ext/wasm/jaccwabyt/jaccwabyt.md @@ -0,0 +1,1081 @@ +Jaccwabyt 🐇 +============================================================ + +**Jaccwabyt**: _JavaScript ⇄ C Struct Communication via WASM Byte +Arrays_ + +Welcome to Jaccwabyt, a JavaScript API which creates bindings for +WASM-compiled C structs, defining them in such a way that changes to +their state in JS are visible in C/WASM, and vice versa, permitting +two-way interchange of struct state with very little user-side +friction. + +(If that means nothing to you, neither will the rest of this page!) + +**Browser compatibility**: this library requires a _recent_ browser +and makes no attempt whatsoever to accommodate "older" or +lesser-capable ones, where "recent," _very roughly_, means released in +mid-2018 or later, with late 2021 releases required for some optional +features in some browsers (e.g. [BigInt64Array][] in Safari). It also +relies on a couple non-standard, but widespread, features, namely +[TextEncoder][] and [TextDecoder][]. It is developed primarily on +Firefox and Chrome on Linux and all claims of Safari compatibility +are based solely on feature compatibility tables provided at +[MDN][]. + +**Non-browser compatibility**: this code does not target non-browser +JS engines and is completely untested on them. That said, it "might +work". + +**64-bit WASM:** as of 2025-09-21 this API supports 64-bit WASM builds +but it has to be configured for it (see [](#api-binderfactory) for +details). + +**Formalities:** + +- Author: [Stephan Beal][sgb] +- Project Homes: + - \ + Is the primary home but... + - \ + ... most development happens here. + +The license for both this documentation and the software it documents +is the same as [sqlite3][], the project from which this spinoff +project was spawned: + +----- + +> 2022-06-30: +> +> The author disclaims copyright to this source code. In place of a +> legal notice, here is a blessing: +> +> - May you do good and not evil. +> - May you find forgiveness for yourself and forgive others. +> - May you share freely, never taking more than you give. + +----- + + +Table of Contents +============================================================ + +- [Overview](#overview) + - [Architecture](#architecture) +- [Creating and Binding Structs](#creating-binding) + - [Step 1: Configure Jaccwabyt](#step-1) + - [Step 2: Struct Description](#step-2) + - [`P` vs `p`](#step-2-pvsp) + - [Step 3: Binding a Struct](#step-3) + - [Step 4: Creating, Using, and Destroying Instances](#step-4) +- APIs + - [Struct Binder Factory](#api-binderfactory) + - [Struct Binder](#api-structbinder) + - [Struct Type](#api-structtype) + - [Struct Constructors](#api-structctor) + - [Struct Protypes](#api-structprototype) + - [Struct Instances](#api-structinstance) +- Appendices + - [Appendix A: Limitations, TODOs, etc.](#appendix-a) + - [Appendix D: Debug Info](#appendix-d) + - [Appendix G: Generating Struct Descriptions](#appendix-g) + + +Overview +============================================================ + +Management summary: this JavaScript-only framework provides limited +two-way bindings between C structs and JavaScript objects, such that +changes to the struct in one environment are visible in the other. + +Details... + +It works by creating JavaScript proxies for C structs. Reads and +writes of the JS-side members are marshaled through a flat byte array +allocated from the WASM heap. As that heap is shared with the C-side +code, and the memory block is written using the same approach C does, +that byte array can be used to access and manipulate a given struct +instance from both JS and C. + +Motivating use case: this API was initially developed as an +experiment to determine whether it would be feasible to implement, +completely in JS, custom "VFS" and "virtual table" objects for the +WASM build of [sqlite3][]. Doing so was going to require some form of +two-way binding of several structs. Once the proof of concept was +demonstrated, a rabbit hole appeared and _down we went_... It has +since grown beyond its humble proof-of-concept origins and is believed +to be a useful (or at least interesting) tool for mixed JS/C +applications. + +Portability notes: + +- These docs sometimes use [Emscripten][] as a point of reference + because it is the most widespread WASM toolchain, but this code is + specifically designed to be usable in arbitrary WASM environments. + It abstracts away a few Emscripten-specific features into + configurable options. Similarly, the build tree requires Emscripten + but Jaccwabyt does not have any hard Emscripten dependencies. +- This code is encapsulated into a single JavaScript function. It + should be trivial to copy/paste into arbitrary WASM/JS-using + projects. +- The source tree includes C code, but only for testing and + demonstration purposes. It is not part of the core distributable. + + +Architecture +------------------------------------------------------------ + + + +```pikchr +BSBF: box rad 0.3*boxht "StructBinderFactory" fit fill lightblue +BSB: box same "StructBinder" fit at 0.75 e of 0.7 s of BSBF.c +BST: box same "StructType" fit at 1.5 e of BSBF +BSC: box same "Struct" "Ctor" fit at 1.5 s of BST +BSI: box same "Struct" "Instances" fit at 1 right of BSB.e +BC: box same at 0.25 right of 1.6 e of BST "C Structs" fit fill lightgrey + +arrow -> from BSBF.s to BSB.w "Generates" aligned above +arrow -> from BSB.n to BST.sw "Contains" aligned above +arrow -> from BSB.s to BSC.nw "Generates" aligned below +arrow -> from BSC.ne to BSI.s "Constructs" aligned below +arrow <- from BST.se to BSI.n "Inherits" aligned above +arrow <-> from BSI.e to BC.s dotted "Shared" aligned above "Memory" aligned below +arrow -> from BST.e to BC.w dotted "Mirrors Struct" aligned above "Model From" aligned below +arrow -> from BST.s to BSC.n "Prototype of" aligned above +``` + +Its major classes and functions are: + +- **[StructBinderFactory][StructBinderFactory]** is a factory function which + accepts a configuration object to customize it for a given WASM + environment. A client will typically call this only one time, with + an appropriate configuration, to generate a single... +- **[StructBinder][]** is a factory function which converts an + arbitrary number struct descriptions into... +- **[StructTypes][StructCtors]** are constructors, one per struct + description, which inherit from + **[`StructBinder.StructType`][StructType]** and are used to instantiate... +- **[Struct instances][StructInstance]** are objects representing + individual instances of generated struct types. + +An app may have any number of StructBinders, but will typically +need only one. Each StructBinder is effectively a separate +namespace for struct creation. + + + +Creating and Binding Structs +============================================================ + +From the amount of documentation provided, it may seem that +creating and using struct bindings is a daunting task, but it +essentially boils down to: + +1. [Confire Jaccwabyt for your WASM environment](#step-1). This is a + one-time task per project and results is a factory function which + can create new struct bindings. +2. [Create a JSON-format description of your C structs](#step-2). This is + required once for each struct and required updating if the C + structs change. +3. [Feed (2) to the function generated by (1)](#step-3) to create JS + constuctor functions for each struct. This is done at runtime, as + opposed to during a build-process step, and can be set up in such a + way that it does not require any maintenance after its initial + setup. +4. [Create and use instances of those structs](#step-4). + +Detailed instructions for each of those steps follows... + + +Step 1: Configure Jaccwabyt for the Environment +------------------------------------------------------------ + +Jaccwabyt's highest-level API is a single function. It creates a +factory for processing struct descriptions, but does not process any +descriptions itself. This level of abstraction exist primarily so that +the struct-specific factories can be configured for a given WASM +environment. Its usage looks like: + +> +```javascript +const MyBinder = StructBinderFactory({ + // These config options are all required: + heap: WebAssembly.Memory instance or a function which returns + a Uint8Array or Int8Array view of the WASM memory, + alloc: function(howMuchMemory){...}, + dealloc: function(pointerToFree){...}, + pointerIR: 'i32' or 'i64' // WASM pointer type - default = 'i32' +}); +``` + +See [](#api-binderfactory) for full details about the config options. + +It also offers a number of other settings, but all are optional except +for the ones shown above. Those three config options abstract away +details which are specific to a given WASM environment. They provide +the WASM "heap" memory, the memory allocator, and the deallocator. In +a conventional Emscripten setup, that config might simply look like: + +> +```javascript +{ + heap: Module?.asm?.memory || Module['wasmMemory'], + //Or: + // heap: ()=>Module['HEAP8'], + alloc: (n)=>Module['_malloc'](n), + dealloc: (m)=>Module['_free'](m) +} +``` + +The StructBinder factory function returns a function which can then be +used to create bindings for our structs. + + +Step 2: Create a Struct Description +------------------------------------------------------------ + +The primary input for this framework is a JSON-compatible construct +which describes a struct we want to bind. For example, given this C +struct: + +> +```c +// C-side: +struct Foo { + int member1; + void * member2; + int64_t member3; +}; +``` + +Its JSON description looks like: + +> +```json +{ + "name": "Foo", + "sizeof": 16, + "members": { + "member1": {"offset": 0,"sizeof": 4,"signature": "i"}, + "member2": {"offset": 4,"sizeof": 4,"signature": "p"}, + "member3": {"offset": 8,"sizeof": 8,"signature": "j"} + } +} +``` + +These data _must_ match up with the C-side definition of the struct +(if any). See [Appendix G][appendix-g] for one way to easily generate +these from C code. + +Each entry in the `members` object maps the member's name to +its low-level layout: + +- `offset`: the byte offset from the start of the struct, as reported + by C's `offsetof()` feature. +- `sizeof`: as reported by C's `sizeof()`. +- `signature`: described below. +- `readOnly`: optional. If set to true, the binding layer will + throw if JS code tries to set that property. + +The order of the `members` entries is not important: their memory +layout is determined by their `offset` and `sizeof` members. The +`name` property is technically optional, but one of the steps in the +binding process requires that either it be passed an explicit name or +there be one in the struct description. The names of the `members` +entries need not match their C counterparts. Project conventions may +call for giving them different names in the JS side and the +[StructBinderFactory][] can be configured to automatically add a +prefix and/or suffix to their names. + +Nested structs are as-yet unsupported by this tool. + +Struct member "signatures" describe the data types of the members and +are an extended variant of the format used by Emscripten's +`addFunction()`. A signature for a non-function-pointer member, or +function pointer member which is to be modelled as an opaque pointer, +is a single letter. A signature for a function pointer may also be +modelled as a series of letters describing the call signature. The +supported letters are: + +- **`v`** = `void` (only used as return type for function pointer members) +- **`i`** = `int32` (4 bytes) +- **`j`** = `int64` (8 bytes) is only really usable if this code is built + with BigInt support (e.g. using the Emscripten `-sWASM_BIGINT` build + flag). Without that, this API may throw when encountering the `j` + signature entry. +- **`f`** = `float` (4 bytes) +- **`d`** = `double` (8 bytes) +- **`c`** = `int8` (1 byte) char - see notes below! +- **`C`** = `uint8` (1 byte) unsigned char - see notes below! +- **`p`** = `int32` (see notes below!) +- **`P`** = Like `p` but with extra handling. Described below. +- **`s`** = like `int32` but is a _hint_ that it's a pointer to a + string so that _some_ (very limited) contexts may treat it as such, + noting that such algorithms must, for lack of information to the + contrary, assume both that the encoding is UTF-8 and that the + pointer's member is NUL-terminated. If that is _not_ the case for a + given string member, do not use `s`: use `i` or `p` instead and do + any string handling yourself. + +Noting that: + +- **All of these types are numeric**. Attempting to set any + struct-bound property to a non-numeric value will trigger an + exception except in cases explicitly noted otherwise. +- **"Char" types**: WASM does not define an `int8` type, nor does its + JS representation distinguish between signed and unsigned. This API + treats `c` as `int8` and `C` as `uint8` for purposes of getting and + setting values when using the `DataView` class. It is _not_ + recommended that client code use these types in new WASM-capable + code, but they were added for the sake of binding some immutable + legacy code to WASM. + +> Sidebar: Emscripten's public docs do not mention `p`, but their +generated code includes `p` as an alias for `i`, presumably to mean +"pointer". Though `i` is legal for pointer types in the signature, `p` +is more descriptive, so this framework encourages the use of `p` for +pointer-type members. Using `p` for pointers also helps future-proof +the signatures against the eventuality that WASM eventually supports +64-bit pointers. Note that sometimes `p` _really_ means a +pointer-to-pointer. We simply have to be aware of when we need to deal +with pointers and pointers-to-pointers in JS code. + +> Trivia: this API treates `p` as distinctly different from `i` in +some contexts, so its use is encouraged for pointer types. + +Signatures in the form `x(...)` denote function-pointer members and +`x` denotes non-function members. Functions with no arguments use the +form `x()`. For function-type signatures, the strings are formulated +such that they can be passed to Emscripten's `addFunction()` after +stripping out the `(` and `)` characters. For good measure, to match +the public Emscripten docs, `p`, `c`, and `C`, should also be replaced +with `i`. In JavaScript that might look like: + +> +``` +signature.replace(/[^vipPsjfdcC]/g,'').replace(/[pPscC]/g,'i'); +``` + + +### `P` vs `p` in Method Signatures + +*This support is experimental and subject to change.* + +The method signature letter `p` means "pointer," which, in WASM, means +either int32 or int64. `p` is treated as a point for most contexts, +while still also being a separate type for function signature purposes +(analog to how pointers in C are just a special use of unsigned +numbers). A capital `P` changes the semantics of plain member pointers +(but not, as of this writing, function pointer members) as follows: + +When a `P`-type member is **set** via `myStruct.x=y`, if +[`(y instanceof StructType)`][StructType] then the value of `y.pointer` is +stored in `myStruct.x`. If `y` is neither a pointer nor a +[StructType][], an exception is triggered (regardless of whether `p` +or `P` is used). + + + +Step 3: Binding the Struct +------------------------------------------------------------ + +We can now use the results of steps 1 and 2: + +> +```javascript +const MyStruct = MyBinder(myStructDescription); +``` + +That creates a new constructor function, `MyStruct`, which can be used +to instantiate new instances. The binder will throw if it encounters +any problems. + +That's all there is to it. + +> Sidebar: that function may modify the struct description object +and/or its sub-objects, or may even replace sub-objects, in order to +simplify certain later operations. If that is not desired, then feed +it a copy of the original, e.g. by passing it +`JSON.parse(JSON.stringify(structDefinition))`. + + +Step 4: Creating, Using, and Destroying Struct Instances +------------------------------------------------------------ + +Now that we have our constructor... + +> +```javascript +const my = new MyStruct(); +``` + +It is important to understand that creating a new instance allocates +memory on the WASM heap. We must not simply rely on garbage collection +to clean up the instances because doing so will not free up the WASM +heap memory. The correct way to free up that memory is to use the +object's `dispose()` method. + +The following usage pattern offers one way to easily ensure proper +cleanup of struct instances: + +> +```javascript +const my = new MyStruct(); +try { + console.log(my.member1, my.member2, my.member3); + my.member1 = 12; + assert(12 === my.member1); + /* ^^^ it may seem silly to test that, but recall that assigning that + property encodes the value into a byte array in heap memory, not + a normal JS property. Similarly, fetching the property decodes it + from the byte array. */ + // Pass the struct to C code which takes a MyStruct pointer: + aCFunction( my.pointer ); +} finally { + my.dispose(); +} +``` + +> Sidebar: the `finally` block will be run no matter how the `try` +exits, whether it runs to completion, propagates an exception, or uses +flow-control keywords like `return` or `break`. It is perfectly legal +to use `try`/`finally` without a `catch`, and doing so is an ideal +match for the memory management requirements of Jaccwaby-bound struct +instances. + +It is often useful to wrap an existing instance of a C-side struct +without taking over ownership of its memory. That can be achieved by +simply passing a pointer to the constructor. For example: + +```js +const m = new MyStruct( functionReturningASharedPtr() ); +// calling m.dispose() will _not_ free the wrapped C-side instance +// but will trigger any ondispose handler. +``` + +Now that we have struct instances, there are a number of things we +can do with them, as covered in the rest of this document. + + + +API Reference +============================================================ + + +API: Binder Factory +------------------------------------------------------------ + +This is the top-most function of the API, from which all other +functions and types are generated. The binder factory's signature is: + +> +``` +Function StructBinderFactory(object configOptions); +``` + +It returns a function which these docs refer to as a [StructBinder][] +(covered in the next section). It throws on error. + +The binder factory supports the following options in its +configuration object argument: + +- `pointerIR` (Added 2025-09-21) + Optionally specify the WASM pointer size with the string `'i32'` or + `'i64'`, defaulting to the former. When using with 64-bit WASM + builds, this must be set to `'i64'` by the client. Any other value + triggers an exception. + +- `heap` + Must be either a `WebAssembly.Memory` instance representing the WASM + heap memory OR a function which returns an Int8Array or Uint8Array + view of the WASM heap. In the latter case the function should, if + appropriate for the environment, account for the heap being able to + grow. Jaccwabyt uses this property in such a way that it is legal + for the WASM heap to grow at runtime. + +- `alloc` + Must be a function semantically compatible with Emscripten's + `Module._malloc()`. That is, it is passed the number of bytes to + allocate and it returns a pointer. On allocation failure it may + either return 0 or throw an exception. This API will throw an + exception if allocation fails or will propagate whatever exception + the allocator throws. The allocator _must_ use the same heap as the + `heap` config option. + +- `dealloc` + Must be a function semantically compatible with Emscripten's + `Module._free()`. That is, it takes a pointer returned from + `alloc()` and releases that memory. It must never throw and must + accept a value of 0/null to mean "do nothing" (noting that 0 is + _technically_ a legal memory address in WASM, but that seems like a + design flaw). + +- `bigIntEnabled` (bool=true if BigInt64Array is available, else false) + If true, the WASM bits this code is used with must have been + compiled with int64 support (e.g. using Emscripten's `-sWASM_BIGINT` + flag). If that's not the case, this flag should be set to false. If + it's enabled, BigInt support is assumed to work and certain extra + features are enabled. Trying to use features which requires BigInt + when it is disabled (e.g. using 64-bit integer types) will trigger + an exception. + +- `memberPrefix` and `memberSuffix` (string="") + If set, struct-defined properties get bound to JS with this string + as a prefix resp. suffix. This can be used to avoid symbol name + collisions between the struct-side members and the JS-side ones + and/or to make more explicit which object-level properties belong to + the struct mapping and which to the JS side. This does not modify + the values in the struct description objects, just the property + names through which they are accessed via property access operations + and the various a [StructInstance][] APIs (noting that the latter + tend to permit both the original names and the names as modified by + these settings). + +- `log` + Optional function used for debugging output. By default + `console.debug` is used but by default no debug output is generated. + This API assumes that the function will space-separate each argument + (like `console.debug` does). See [Appendix D](#appendix-d) for info + about enabling debugging output. + + +API: Struct Binder +------------------------------------------------------------ + +Struct Binders are factories which are created by the +[StructBinderFactory][]. A given Struct Binder can process any number +of distinct structs. In a typical setup, an app will have ony one +shared Binder Factory and one Struct Binder. Struct Binders which are +created via different [StructBinderFactory][] calls are unrelated to each +other, sharing no state except, perhaps, indirectly via +[StructBinderFactory][] configuration (e.g. the memory heap). + +These factories have two call signatures: + +> +```javascript +Function StructBinder([string structName,] object structDescription) +``` + +If the struct description argument has a `name` property then the name +argument is optional, otherwise it is required. + +The returned object is a constructor for instances of the struct +described by its argument(s), each of which derives from +a separate [StructType][] instance. + +The Struct Binder has the following members: + +- `allocCString(str)` + Allocates a new UTF-8-encoded, NUL-terminated copy of the given JS + string and returns its address relative to `config.heap()`. If + allocation returns 0 this function throws. Ownership of the memory + is transfered to the caller, who must eventually pass it to the + configured `config.dealloc()` function. + +- `config` + The configuration object passed to the [StructBinderFactory][], + primarily for accessing the memory (de)allocator and memory. Modifying + any of its "significant" configuration values may have undefined + results. + + +API: Struct Type +------------------------------------------------------------ + +The StructType class is a property of the [StructBinder][] function. + +Each constructor created by a [StructBinder][] inherits from _its own +instance_ of the StructType class, which contains state specific to +that struct type (e.g. the struct name and description metadata). +StructTypes which are created via different [StructBinder][] instances +are unrelated to each other, sharing no state except [StructBinderFactory][] +config options. + +The StructType constructor cannot be called from client code. It is +only called by the [StructBinder][]-generated +[constructors][StructCtors]. The `StructBinder.StructType` object +has the following "static" properties (^Which are accessible from +individual instances via `theInstance.constructor`.): + +- `addOnDispose(...value)`\ + If this object has no `ondispose` property, this function creates it + as an array and pushes the given value(s) onto it. If the object has + a function-typed `ondispose` property, this call replaces it with an + array and moves that function into the array. In all other cases, + `ondispose` is assumed to be an array and the argument(s) is/are + appended to it. Returns `this`. + +- `allocCString(str)` + Identical to the [StructBinder][] method of the same name. + +- `hasExternalPointer(object)` + Returns true if the given object's `pointer` member refers to an + "external" object. That is the case when a pointer is passed to a + [struct's constructor][StructCtors]. If true, the memory is owned by + someone other than the object and must outlive the object. + +- `isA(value)` + Returns true if its argument is a StructType instance _from the same + [StructBinder][]_ as this StructType. + +- `memberKey(string)` + Returns the given string wrapped in the configured `memberPrefix` + and `memberSuffix` values. e.g. if passed `"x"` and `memberPrefix` + is `"$"` then it returns `"$x"`. This does not verify that the + property is actually a struct a member, it simply transforms the + given string. TODO(?): add a 2nd parameter indicating whether it + should validate that it's a known member name. + +The base StructType prototype has the following members, all of which +are inherited by [struct instances](#api-structinstance) and may only +legally be called on concrete struct instances unless noted otherwise: + +- `dispose()` + Frees, if appropriate, the WASM-allocated memory which is allocated + by the constructor. If this is not called before the JS engine + cleans up the object, a leak in the WASM heap memory pool will result. + When `dispose()` is called, if the object has a property named `ondispose` + then it is treated as follows: + - If it is a function, it is called with the struct object as its `this`. + That method must not throw - if it does, the exception will be + ignored. + - If it is an array, it may contain functions, pointers, other + [StructType] instances, and/or JS strings. If an entry is a + function, it is called as described above. If it's a number, it's + assumed to be a pointer and is passed to the `dealloc()` function + configured for the parent [StructBinder][]. If it's a + [StructType][] instance then its `dispose()` method is called. If + it's a JS string, it's assumed to be a helpful description of the + next entry in the list and is simply ignored. Strings are + supported primarily for use as debugging information. + - Some struct APIs will manipulate the `ondispose` member, creating + it as an array or converting it from a function to array as + needed. + +- `lookupMember(memberName,throwIfNotFound=true)` + Given the name of a mapped struct member, it returns the member + description object. If not found, it either throws (if the 2nd + argument is true) or returns `undefined` (if the second argument is + false). The first argument may be either the member name as it is + mapped in the struct description or that same name with the + configured `memberPrefix` and `memberSuffix` applied, noting that + the lookup in the former case is faster.\ + This method may be called directly on the prototype, without a + struct instance. + +- `memberToJsString(memberName)` + Uses `this.lookupMember(memberName,true)` to look up the given + member. If its signature is `s` then it is assumed to refer to a + NUL-terminated, UTF-8-encoded string and its memory is decoded as + such. If its signature is not one of those then an exception is + thrown. If its address is 0, `null` is returned. See also: + `setMemberCString()`. + +- `memberIsString(memberName [,throwIfNotFound=true])` + Uses `this.lookupMember(memberName,throwIfNotFound)` to look up the + given member. Returns the member description object if the member + has a signature of `s`, else returns false. If the given member is + not found, it throws if the 2nd argument is true, else it returns + false. + +- `memberKey(string)` + Works identically to `StructBinder.StructType.memberKey()`. + +- `memberKeys()` + Returns an array of the names of the properties of this object + which refer to C-side struct counterparts. + +- `memberSignature(memberName [,emscriptenFormat=false])` + Returns the signature for a given a member property, either in this + framework's format or, if passed a truthy 2nd argument, in a format + suitable for the 2nd argument to Emscripten's `addFunction()`. + Throws if the first argument does not resolve to a struct-bound + member name. The member name is resolved using `this.lookupMember()` + and throws if the member is found mapped. + +- `memoryDump()` + Returns a Uint8Array which contains the current state of this + object's raw memory buffer. Potentially useful for debugging, but + not much else. The memory is necessarily, for compatibility with C, + written in the host platform's endianness. Since all WASM is + little-endian, it's the same everywhere. Even so: it should not be + used as a persistent serialization format because (A) any changes to + the struct will invalidate older serialized data and (B) serializing + member pointers is useless. + +- `setMemberCString(memberName,str)` + Uses `StructType.allocCString()` to allocate a new C-style string, + assign it to the given member, and add the new string to this + object's `ondispose` list for cleanup when `this.dispose()` is + called. This function throws if `lookupMember()` fails for the given + member name, if allocation of the string fails, or if the member has + a signature value of anything other than `s`. Returns `this`. + *Achtung*: calling this repeatedly will not immediately free the + previous values because this code cannot know whether they are in + use in other places, namely C. Instead, each time this is called, + the prior value is retained in the `ondispose` list for cleanup when + the struct is disposed of. Because of the complexities and general + uncertainties of memory ownership and lifetime in such + constellations, it is recommended that the use of C-string members + from JS be kept to a minimum or that the relationship be one-way: + let C manage the strings and only fetch them from JS using, e.g., + `memberToJsString()`. + + + +API: Struct Constructors +------------------------------------------------------------ + +Struct constructors (the functions returned from [StructBinder][]) +are used for, intuitively enough, creating new instances of a given +struct type: + +> +``` +const x = new MyStruct; +``` + +Normally they should be passed no arguments, but they optionally +accept a single argument: a WASM heap pointer address of memory +which the object will use for storage. It does _not_ take over +ownership of that memory and that memory must be valid at +for least as long as this struct instance. This is used, for example, +to proxy static/shared C-side instances: + +> +``` +const x = new MyStruct( someCFuncWhichReturnsAMyStructPointer() ); +... +x.dispose(); // does NOT free the memory +``` + +The JS-side construct does not own the memory in that case and has no +way of knowing when the C-side struct is destroyed. Results are +specifically undefined if the JS-side struct is used after the C-side +struct's member is freed. + +> Potential TODO: add a way of passing ownership of the C-side struct +to the JS-side object. e.g. maybe simply pass `true` as the second +argument to tell the constructor to take over ownership. Currently the +pointer can be taken over using something like +`myStruct.ondispose=[myStruct.pointer]` immediately after creation. + +These constructors have the following "static" members: + +- `isA(value)` + Returns true if its argument was created by this constructor. + +- `memberKey(string)` + Works exactly as documented for [StructType][]. + +- `memberKeys(string)` + Works exactly as documented for [StructType][]. + +- `structInfo` + The structure description passed to [StructBinder][] when this + constructor was generated. + +- `structName` + The structure name passed to [StructBinder][] when this constructor + was generated. + + + +API: Struct Prototypes +------------------------------------------------------------ + +The prototypes of structs created via [the constructors described in +the previous section][StructCtors] are each a struct-type-specific +instance of [StructType][] and add the following struct-type-specific +properties to the mix: + +- `structInfo` + The struct description metadata, as it was given to the + [StructBinder][] which created this class. + +- `structName` + The name of the struct, as it was given to the [StructBinder][] which + created this class. + + +API: Struct Instances +------------------------------------------------------------------------ + +Instances of structs created via [the constructors described +above][StructCtors] each have the following instance-specific state in +common: + +- `pointer` + A read-only numeric property which is the "pointer" returned by the + configured allocator when this object is constructed. After + `dispose()` (inherited from [StructType][]) is called, this property + has the `undefined` value. When calling C-side code which takes a + pointer to a struct of this type, simply pass it `myStruct.pointer`. + + +Appendices +============================================================ + + +Appendix A: Limitations, TODOs, and Non-TODOs +------------------------------------------------------------ + +- This library only supports the basic set of member types supported + by WASM: numbers (which includes pointers). Nested structs are not + handled except that a member may be a _pointer_ to such a + struct. Whether or not it ever will depends entirely on whether its + developer ever needs that support. Conversion of strings between + JS and C requires infrastructure specific to each WASM environment + and is not directly supported by this library. + +- Binding functions to struct instances, such that C can see and call + JS-defined functions, is not as transparent as it really could be, + due to [shortcomings in the Emscripten + `addFunction()`/`removeFunction()` + interfaces](https://github.com/emscripten-core/emscripten/issues/17323). Until + a replacement for that API can be written, this support will be + quite limited. It _is_ possible to bind a JS-defined function to a + C-side function pointer and call that function from C. What's + missing is easier-to-use/more transparent support for doing so. + - In the meantime, a [standalone + subproject](/file/common/whwasmutil.js) of Jaccwabyt provides such a + binding mechanism, but integrating it directly with Jaccwabyt would + not only more than double its size but somehow feels inappropriate, so + experimentation is in order for how to offer that capability via + completely optional [StructBinderFactory][] config options. + +- It "might be interesting" to move access of the C-bound members into + a sub-object. e.g., from JS they might be accessed via + `myStructInstance.s.structMember`. The main advantage is that it would + eliminate any potential confusion about which members are part of + the C struct and which exist purely in JS. "The problem" with that + is that it requires internally mapping the `s` member back to the + object which contains it, which makes the whole thing more costly + and adds one more moving part which can break. Even so, it's + something to try out one rainy day. Maybe even make it optional and + make the `s` name configurable via the [StructBinderFactory][] + options. (Over-engineering is an arguably bad habit of mine.) + +- It "might be interesting" to offer (de)serialization support. It + would be very limited, e.g. we can't serialize arbitrary pointers in + any meaningful way, but "might" be useful for structs which contain + only numeric or C-string state. As it is, it's easy enough for + client code to write wrappers for that and handle the members in + ways appropriate to their apps. Any impl provided in this library + would have the shortcoming that it may inadvertently serialize + pointers (since they're just integers), resulting in potential chaos + after deserialization. Perhaps the struct description can be + extended to tag specific members as serializable and how to + serialize them. + + +Appendix D: Debug Info +------------------------------------------------------------ + +The [StructBinderFactory][], [StructBinder][], and [StructType][] classes +all have the following "unsupported" method intended primarily +to assist in their own development, as opposed to being for use in +client code: + +- `debugFlags(flags)` (integer) + An "unsupported" debugging option which may change or be removed at + any time. Its argument is a set of flags to enable/disable certain + debug/tracing output for property accessors: 0x01 for getters, 0x02 + for setters, 0x04 for allocations, 0x08 for deallocations. Pass 0 to + disable all flags and pass a negative value to _completely_ clear + all flags. The latter has the side effect of telling the flags to be + inherited from the next-higher-up class in the hierarchy, with + [StructBinderFactory][] being top-most, followed by [StructBinder][], then + [StructType][]. + + + +Appendix G: Generating Struct Descriptions From C +------------------------------------------------------------ + +Struct definitions are _ideally_ generated from WASM-compiled C, as +opposed to simply guessing the sizeofs and offsets, so that the sizeof +and offset information can be collected using C's `sizeof()` and +`offsetof()` features (noting that struct padding may impact offsets +in ways which might not be immediately obvious, so writing them by +hand is _most certainly not recommended_). + +How exactly the desciption is generated is necessarily +project-dependent. It's tempting say, "oh, that's easy! We'll just +write it by hand!" but that would be folly. The struct sizes and byte +offsets into the struct _must_ be precisely how C-side code sees the +struct or the runtime results are completely undefined. + +The approach used in developing and testing _this_ software is... + +Below is a complete copy/pastable example of how we can use a small +set of macros to generate struct descriptions from C99 or later into +static string memory. Simply add such a file to your WASM build, +arrange for its function to be exported[^export-func], and call it +from JS (noting that it requires environment-specific JS glue to +convert the returned pointer to a JS-side string). Use `JSON.parse()` +to process it, then feed the included struct descriptions into the +binder factory at your leisure. + +------------------------------------------------------------ + +```c +#include /* memset() */ +#include /* offsetof() */ +#include /* snprintf() */ +#include /* int64_t */ +#include + +struct ExampleStruct { + int v4; + void * ppV; + int64_t v8; + void (*xFunc)(void*); +}; +typedef struct ExampleStruct ExampleStruct; + +const char * wasm__ctype_json(void){ + static char strBuf[512 * 8] = {0} + /* Static buffer which must be sized large enough for + our JSON. The string-generation macros try very + hard to assert() if this buffer is too small. */; + int n = 0, structCount = 0 /* counters for the macros */; + char * pos = &strBuf[1] + /* Write-position cursor. Skip the first byte for now to help + protect against a small race condition */; + char const * const zEnd = pos + sizeof(strBuf) + /* one-past-the-end cursor (virtual EOF) */; + if(strBuf[0]) return strBuf; // Was set up in a previous call. + + //////////////////////////////////////////////////////////////////// + // First we need to build up our macro framework... + + //////////////////////////////////////////////////////////////////// + // Core output-generating macros... +#define lenCheck assert(pos < zEnd - 100) +#define outf(format,...) \ + pos += snprintf(pos, ((size_t)(zEnd - pos)), format, __VA_ARGS__); \ + lenCheck +#define out(TXT) outf("%s",TXT) +#define CloseBrace(LEVEL) \ + assert(LEVEL<5); memset(pos, '}', LEVEL); pos+=LEVEL; lenCheck + + //////////////////////////////////////////////////////////////////// + // Macros for emitting StructBinders... +#define StructBinder__(TYPE) \ + n = 0; \ + outf("%s{", (structCount++ ? ", " : "")); \ + out("\"name\": \"" # TYPE "\","); \ + outf("\"sizeof\": %d", (int)sizeof(TYPE)); \ + out(",\"members\": {"); +#define StructBinder_(T) StructBinder__(T) +// ^^^ extra indirection needed to expand CurrentStruct +#define StructBinder StructBinder_(CurrentStruct) +#define _StructBinder CloseBrace(2) +#define M(MEMBER,SIG) \ + outf("%s\"%s\": " \ + "{\"offset\":%d,\"sizeof\": %d,\"signature\":\"%s\"}", \ + (n++ ? ", " : ""), #MEMBER, \ + (int)offsetof(CurrentStruct,MEMBER), \ + (int)sizeof(((CurrentStruct*)0)->MEMBER), \ + SIG) + // End of macros. + //////////////////////////////////////////////////////////////////// + + //////////////////////////////////////////////////////////////////// + // With that out of the way, we can do what we came here to do. + out("\"structs\": ["); { + +// For each struct description, do... +#define CurrentStruct ExampleStruct + StructBinder { + M(v4,"i"); + M(ppV,"p"); + M(v8,"j"); + M(xFunc,"v(p)"); + } _StructBinder; +#undef CurrentStruct + + } out( "]"/*structs*/); + //////////////////////////////////////////////////////////////////// + // Done! Finalize the output... + out("}"/*top-level wrapper*/); + *pos = 0; + strBuf[0] = '{'/*end of the race-condition workaround*/; + return strBuf; + +// If this file will ever be concatenated or #included with others, +// it's good practice to clean up our macros: +#undef StructBinder +#undef StructBinder_ +#undef StructBinder__ +#undef M +#undef _StructBinder +#undef CloseBrace +#undef out +#undef outf +#undef lenCheck +} +``` + +------------------------------------------------------------ + + + +[sqlite3]: https://sqlite.org +[emscripten]: https://emscripten.org +[sgb]: https://wanderinghorse.net/home/stephan/ +[appendix-g]: #appendix-g +[StructBinderFactory]: #api-binderfactory +[StructCtors]: #api-structctor +[StructType]: #api-structtype +[StructBinder]: #api-structbinder +[StructInstance]: #api-structinstance +[^export-func]: In Emscripten, add its name, prefixed with `_`, to the + project's `EXPORT_FUNCTIONS` list. +[BigInt64Array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt64Array +[TextDecoder]: https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder +[TextEncoder]: https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder +[MDN]: https://developer.mozilla.org/docs/Web/API diff --git a/ext/wasm/mkdist.sh b/ext/wasm/mkdist.sh new file mode 100755 index 0000000000..84780668b8 --- /dev/null +++ b/ext/wasm/mkdist.sh @@ -0,0 +1,210 @@ +#!/usr/bin/env bash +### +# +# Builds the SQLite JS/WASM distribution zip file. +# +# Usage: $0 build-name ?flags? +# +# build-name is the dir/archive name prefix for the +# build and defaults to sqlite-wasm. +# +# -?|--help = Show this text +# +# -0 = Use -O0 instead of ${optFlag} +# +# -1 = Use make -j1 instead of ${makeFlag} +# +# -64 = Include 64-bit builds +# +# --noclean = do not run 'make clean' first +# +# --snapshot = gives the archive name a distinctive suffix +# +### + +function die(){ + local rc=$1 + shift + echo "Error: $@" 1>&2 + exit $rc +} + +dirTop=../.. +buildName= +b64=0 +optFlag=-Oz +clean=1 +makeFlag=-j4 +snapshotSuffix= +for arg in $@; do + case $arg in + + -64) b64=1 + ;; + + -0) optFlag=-O0 + ;; + + -1) makeFlag= + ;; + + --noclean) clean=0 + ;; + + --snapshot) + snapshotSuffix=$(date +%Y%m%d) + ;; + + -?|--help) + sed -n -e '2,/^###/p' $0 + exit + ;; + + *) if [[ x != x${buildName} ]]; then + die 1 "Unhandled argument: $arg" + fi + buildName=$arg + ;; + esac +done + +make= +for i in gmake make; do + make=$(which $i 2>/dev/null) + [[ x != x${make} ]] && break +done +[[ x = x$make ]] && die 127 "Cannot find make" + + +[[ x = x${buildName} ]] && buildName=sqlite-wasm + +buildName=${buildName}${snapshotSuffix} + +echo "Creating the SQLite wasm dist bundle..." + +# +# Generates files which, when built, will also build all of the pieces +# neaded for the dist bundle. +# +tgtFiles=( + demo-worker1-promiser.html + demo-worker1-promiser.js + demo-worker1-promiser-esm.html + demo-worker1-promiser.mjs + + tester1.html + tester1-esm.html + tester1-worker.html + tester1.js + tester1.mjs +) + +if [[ 1 = $b64 ]]; then + tgtFiles+=( + tester1-64bit.html + tester1-esm-64bit.html + tester1-worker-64bit.html + tester1-64bit.js + tester1-64bit.mjs + ) +fi + +[[ 1 = $clean ]] && $make clean +$make $makeFlag \ + t-version-info t-stripccomments \ + ${tgtFiles[@]} \ + "emcc_opt=${optFlag}" || die $? + +dirTmp=d.dist +rm -fr $dirTmp +mkdir -p $dirTmp/jswasm || die $? +mkdir -p $dirTmp/common || die $? + +# Static files for the top-most dir: +fTop=( + demo-123.html + demo-123-worker.html + demo-123.js + + demo-worker1.html + demo-worker1.js + + demo-jsstorage.html + demo-jsstorage.js + + module-symbols.html +) + +# Files for the jswasm subdir sans jswasm prefix: +# +# fJ1 = JS files to stripccomments -k on +# fJ2 = JS files to stripccomments -k -k on +fJ1=( + sqlite3-opfs-async-proxy.js + sqlite3-worker1.js + sqlite3-worker1.mjs + sqlite3-worker1-bundler-friendly.mjs + sqlite3-worker1-promiser.js + sqlite3-worker1-promiser.mjs + sqlite3-worker1-promiser-bundler-friendly.mjs +) +fJ2=( + sqlite3.js + sqlite3.mjs +) + +# fW = list of wasm files to copy from jswasm/. +fW=(sqlite3.wasm) +if [[ 1 = $b64 ]]; then + fW+=(sqlite3-64bit.wasm) +fi + +function fcp() { + cp -p $@ || die $? + chmod +w ${@: -1} +} + +function scc(){ + ${dirTop}/tool/stripccomments $@ || die $? +} + +jw=jswasm +fcp ${tgtFiles[@]} $dirTmp/. +fcp README-dist.txt $dirTmp/README.txt +fcp index-dist.html $dirTmp/index.html +fcp common/*.css common/SqliteTestUtil.js $dirTmp/common/. + +for i in ${fTop[@]}; do + fcp $i $dirTmp/. +done + +for i in ${fW[@]}; do + fcp $jw/$i $dirTmp/$jw/. +done + +for i in ${fJ1[@]}; do + scc -k < $jw/$i > $dirTmp/$jw/$i || die $? +done + +for i in ${fJ2[@]}; do + scc -k -k < $jw/$i > $dirTmp/$jw/$i || die $? +done + +# +# Done copying files. Now zip it up... +# +svi=./version-info +vnum=$($svi --download-version) +[ "" = "$vnum" ] && die "version number is empty!" +vdir=${buildName}-${vnum} +fzip=${vdir}.zip +rm -fr ${vdir} ${fzip} +mv $dirTmp $vdir || die $? +zip -rq9 $fzip $(find $vdir -type f | sort) || die $? +ls -la $fzip +unzip -lv $fzip || die $? +cat < +#include +#include + +#define pf printf +#define ps puts + +/* Separator to help eyeballs find the different output sections */ +#define zBanner \ + "\n########################################################################\n" + +/* +** Flags for use with BuildDef::flags. +** +** Maintenance reminder: do not combine flags within this enum, +** e.g. F_BUNDLER_FRIENDLY=0x02|F_ESM, as that will lead +** to breakage in some of the flag checks. +*/ +enum BuildDefFlags { + /* Indicates an ESM module build. */ + F_ESM = 0x01, + /* Indicates a "bundler-friendly" build mode. */ + F_BUNDLER_FRIENDLY = 1<<1, + /* Indicates that this build is unsupported. Such builds are not + ** added to the 'all' target. The unsupported builds exist primarily + ** for experimentation's sake. */ + F_UNSUPPORTED = 1<<2, + /* Elide this build from the 'all' target. */ + F_NOT_IN_ALL = 1<<3, + /* If it's a 64-bit build. */ + F_64BIT = 1<<4, + /* Indicates a node.js-for-node.js build (untested and + ** unsupported). */ + F_NODEJS = 1<<5, + /* Indicates a wasmfs build (untested and unsupported). */ + F_WASMFS = 1<<6, + + /* + ** Which compiled files from $(dir.dout)/buildName/*.{js,mjs,wasm} + ** to copy to $(dir.dout) after creating them. This should only be + ** applied to builds which result in end-user deliverables. Some + ** builds, like the bundler-friendly ones, are a hybrid: we keep + ** only their JS file and patch their JS to use the WASM file from a + ** canonical build which uses that same WASM file. Reusing X.wasm + ** that way can only work for builds which are processed identically + ** by Emscripten. For a given set of C flags (as opposed to + ** JS-influencing flags), all builds of X.js and Y.js will produce + ** identical X.wasm and Y.wasm files. Their JS files may well + ** differ, however. + */ + CP_JS = 1 << 30, + CP_WASM = 1 << 31, + CP_ALL = CP_JS | CP_WASM +}; + +/* +** Info needed for building one concrete JS/WASM combination.. +** +** Notes about Emscripten builds... +** +** When emcc processes X.js it also generates X.wasm and hard-codes +** the name "X.wasm" into the JS file (it has to - there's no reliable +** way to derive that name at runtime for certain modes of loading the +** WASM file). Because we only need two sqlite3.wasm files (one each +** for 32- and 64-bit), the build then copies just those into the +** final build directory $(dir.dout). +** +** To keep parallel builds from stepping on each other, each distinct +** build goes into its own subdir $(dir.dout.BuildName)[^1], i.e. +** $(dir.dout)/BuildName. Builds which produce deliverables we'd like +** to keep/distribute copy their final results into the build dir +** $(dir.dout). See the notes for the CP_JS enum entry for more +** details on that. +** +** The final result of each build is a pair of JS/WASM files, but +** getting there requires generation of several files, primarily as +** inputs for specific Emscripten flags: +** +** --pre-js = file gets injected after Emscripten's earliest starting +** point, enabling limited customization of Emscripten's +** behavior. This code lives/runs within the generated sqlite3InitModule(). +** +** --post-js = gets injected after Emscripten's main work, but still +** within the body of sqlite3InitModule(). +** +** --extern-pre-js = gets injected before sqlite3InitModule(), in the +** global scope. We inject the license and version info here. +** +** --extern-post-js = gets injected immediately after +** sqlite3InitModule(), in the global scope. In this step we replace +** sqlite3InitModule() with a slightly customized, the main purpose of +** which is to (A) give us (not Emscripten) control over the arguments +** it accepts and (B) to run the library bootstrap step. +** +** Then there's sqlite3-api.BuildName.js, which is the entire SQLite3 +** JS API (generated from the list defined in $(sqlite3-api.jses)). It +** gets sandwitched inside --post-js. +** +** Each of those inputs has to be generated before passing them on to +** Emscripten so that any build-specific capabilities can get filtered +** in or out (using ./c-pp.c). +** +** [^1]: The legal BuildNames are in this file's BuildDef_map macro. +*/ +struct BuildDef { + /* + ** Base name of output JS and WASM files. The X part of X.js and + ** X.wasm. + */ + const char *zBaseName; + /* + ** A glyph to use in log messages for this build, intended to help + ** the eyes distinguish the build lines more easily in parallel + ** builds. + ** + ** The convention for 32- vs 64-bit pairs is to give them similar + ** emoji, e.g. a cookie for 32-bit and a donut or cake for 64. + ** Alternately, the same emoji a "64" suffix, excep that that throws + ** off the output alignment in parallel builds ;). + */ + const char *zEmo; + /* + ** If the build needs its x.wasm renamed in its x.{js,mjs} then this + ** must hold the base name to rename it to. Typically "sqlite3" or + ** "sqlite3-64bit". This is the case for builds which are named + ** something like sqlite3-foo-bar but can use the vanilla + ** sqlite3.wasm file. In such cases we don't need the extra + ** sqlite3-foo-bar.wasm which Emscripten (necessarily) creates when + ** compiling the module, so we patch (at build-time) the JS file to + ** use this name instead sqlite3-foo-bar. + */ + const char *zDotWasm; + const char *zCmppD; /* Extra -D... flags for c-pp */ + const char *zEmcc; /* Full flags for emcc. Normally NULL for default. */ + const char *zEmccExtra; /* Extra flags for emcc */ + const char *zDeps; /* Extra deps */ + const char *zEnv; /* emcc -sENVIRONMENT=... value */ + /* + ** Makefile code "ifeq (...)". If set, this build is enclosed in a + ** $zIfCond/endif block. + */ + const char *zIfCond; /* makefile "ifeq (...)" or similar */ + int flags; /* Flags from BuildDefFlags */ +}; +typedef struct BuildDef BuildDef; + +/* +** List of distinct library builds. Each one has to be set up in +** oBuildDefs. See the next comment block. +** +** Many makefile vars use these identifiers for naming stuff, e.g.: +** +** out.NAME.js = output JS file for the build named NAME +** out.NAME.wasm = output WASM file for the build named NAME +** logtag.NAME = Used for decorating log output +** +** etc. +***/ +#define BuildDefs_map(E) \ + E(vanilla) E(vanilla64) \ + E(esm) E(esm64) \ + E(bundler) E(bundler64) \ + E(speedtest1) E(speedtest164) \ + E(node) E(node64) \ + E(wasmfs) + +/* +** The set of WASM builds for the library (as opposed to the apps +** (fiddle, speedtest1)). Their order in BuildDefs_map is mostly +** insignificant, but some makefile vars used by some builds are set +** up by prior builds. Because of that, the (sqlite3, vanilla), +** (sqlite3, esm), and (sqlite3, bundler-friendly) builds should be +** defined first (in that order). +*/ +struct BuildDefs { +#define E(N) BuildDef N; + BuildDefs_map(E) +#undef E +}; +typedef struct BuildDefs BuildDefs; + +const BuildDefs oBuildDefs = { + /* + ** The canonical build, against which all others are compared and + ** contrasted. This is the one we post downloads for. + ** + ** This one's zBaseName and zEnv MUST be non-NULL so it can be used + ** as a default for all others + */ + .vanilla = { + .zEmo = "🍦", + .zBaseName = "sqlite3", + .zDotWasm = 0, + .zCmppD = 0, + .zEmcc = 0, + .zEmccExtra = 0, + .zEnv = "web,worker", + .zDeps = 0, + .zIfCond = 0, + .flags = CP_ALL + }, + + /* The canonical build in 64-bit. */ + .vanilla64 = { + .zEmo = "🍨", + .zBaseName = "sqlite3-64bit", + .zDotWasm = 0, + .zCmppD = 0, + .zEmcc = 0, + .zEmccExtra = "-sMEMORY64=1 -sWASM_BIGINT=1", + .zEnv = 0, + .zDeps = 0, + .zIfCond = 0, + .flags = CP_ALL | F_64BIT + }, + + /* The canonical esm build. */ + .esm = { + .zEmo = "🍬", + .zBaseName = "sqlite3", + .zDotWasm = 0, + .zCmppD = "-Dtarget:es6-module", + .zEmcc = 0, + .zEmccExtra = 0, + .zEnv = 0, + .zDeps = 0, + .zIfCond = 0, + .flags = CP_JS | F_ESM + }, + + /* The canonical esm build in 64-bit. */ + .esm64 = { + .zEmo = "🍫", + .zBaseName = "sqlite3-64bit", + .zDotWasm = 0, + .zCmppD = "-Dtarget:es6-module", + .zEmcc = 0, + .zEmccExtra = "-sMEMORY64=1 -sWASM_BIGINT=1", + .zEnv = 0, + .zDeps = 0, + .zIfCond = 0, + .flags = CP_JS | F_ESM | F_64BIT + }, + + /* speedtest1, our primary benchmarking tool */ + .speedtest1 = { + .zEmo = "🛼", + .zBaseName = "speedtest1", + .zDotWasm = 0, + .zCmppD = 0, + .zEmcc = + "$(emcc.speedtest1)" + " $(emcc.speedtest1.common)" + " $(pre-post.speedtest1.flags)" + " $(cflags.common)" + " -DSQLITE_SPEEDTEST1_WASM" + " $(SQLITE_OPT)" + " -USQLITE_WASM_BARE_BONES" + " -USQLITE_C -DSQLITE_C=$(sqlite3.canonical.c)" + " $(speedtest1.exit-runtime0)" + " $(speedtest1.c.in)" + " -lm", + .zEmccExtra = 0, + .zEnv = 0, + .zDeps = + "$(speedtest1.c.in)" + " $(EXPORTED_FUNCTIONS.speedtest1)", + .zIfCond = 0, + .flags = CP_ALL + }, + + /* speedtest1 64-bit */ + .speedtest164 = { + .zEmo = "🛼64", + .zBaseName = "speedtest1-64bit", + .zDotWasm = 0, + .zCmppD = 0, + .zEmcc = + "$(emcc.speedtest1)" + " $(emcc.speedtest1.common)" + " -sMEMORY64=1 -sWASM_BIGINT=1" + " $(pre-post.speedtest164.flags)" + " $(cflags.common)" + " -DSQLITE_SPEEDTEST1_WASM" + " $(SQLITE_OPT)" + " -USQLITE_WASM_BARE_BONES" + " -USQLITE_C -DSQLITE_C=$(sqlite3.canonical.c)" + " $(speedtest1.exit-runtime0)" + " $(speedtest1.c.in)" + " -lm", + .zEmccExtra = 0, + .zEmccExtra = 0, + .zEnv = 0, + .zDeps = + "$(speedtest1.c.in)" + " $(EXPORTED_FUNCTIONS.speedtest1)", + .zIfCond = 0, + .flags = CP_ALL | F_NOT_IN_ALL + }, + + /* + ** Core bundler-friendly build. Untested and "not really" supported, + ** but required by the downstream npm subproject. + ** + ** Testing these requires special-purpose node-based tools and + ** custom test apps, none of which we have/use. So we can pass them + ** off as-is to the npm subproject and they spot failures pretty + ** quickly ;). + */ + .bundler = { + .zEmo = "👛", + .zBaseName = "sqlite3-bundler-friendly", + .zDotWasm = "sqlite3", + .zCmppD = "$(c-pp.D.esm) -Dtarget:es6-bundler-friendly", + .zEmcc = 0, + .zEmccExtra = 0, + .zEnv = 0, + .zDeps = 0, + .zIfCond = 0, + .flags = CP_JS | F_BUNDLER_FRIENDLY | F_ESM + //| F_NOT_IN_ALL + }, + + /* 64-bit bundler-friendly. */ + .bundler64 = { + .zEmo = "📦", + .zBaseName = "sqlite3-bundler-friendly-64bit", + .zDotWasm = "sqlite3-64bit", + .zCmppD = "$(c-pp.D.bundler)", + .zEmcc = 0, + .zEmccExtra = "-sMEMORY64=1", + .zEnv = 0, + .zDeps = 0, + .zIfCond = 0, + .flags = CP_JS | F_ESM | F_BUNDLER_FRIENDLY | F_64BIT | F_NOT_IN_ALL + }, + + /* + ** We neither build node builds on a regular basis nor test them at + ** all. They are fully unsupported. Also, our JS targets only + ** browsers. + */ + .node = { + .zEmo = "🍟", + .zBaseName = "sqlite3-node", + .zDotWasm = 0, + .zCmppD = "-Dtarget:node $(c-pp.D.bundler)", + .zEmcc = 0, + .zEmccExtra = 0, + .zEnv = "node" + /* Adding ",node" to the zEnv list for the other builds causes + ** Emscripten to generate code which confuses node: it cannot + ** reliably determine whether the build is for a browser or for + ** node. */, + .zDeps = 0, + .zIfCond = 0, + .flags = CP_ALL | F_UNSUPPORTED | F_NODEJS + }, + + /* 64-bit node. */ + .node64 = { + .zEmo = "🍔", + .zBaseName = "sqlite3-node-64bit", + .zDotWasm = 0, + .zCmppD = "-Dtarget:node $(c-pp.D.bundler)", + .zEmcc = 0, + .zEmccExtra = 0, + .zEnv = "node", + .zDeps = 0, + .zIfCond = 0, + .flags = CP_ALL | F_UNSUPPORTED | F_NODEJS | F_64BIT + }, + + /* Entirely unsupported. */ + .wasmfs = { + .zEmo = "💿", + .zBaseName = "sqlite3-wasmfs", + .zDotWasm = 0, + .zCmppD = "$(c-pp.D.bundler) -Dwasmfs", + .zEmcc = 0, + .zEmccExtra = + "-sEXPORT_ES6 -sUSE_ES6_IMPORT_META" + " -sUSE_CLOSURE_COMPILER=0" + " -pthread -sWASMFS -sPTHREAD_POOL_SIZE=1" + " -sERROR_ON_UNDEFINED_SYMBOLS=0 -sLLD_REPORT_UNDEFINED" + " -DSQLITE_ENABLE_WASMFS" + , + .zEnv = 0, + .zDeps = 0, + .zIfCond = "ifeq (1,$(wasmfs.enable))", + .flags = CP_ALL | F_UNSUPPORTED | F_WASMFS | F_ESM + } +}; + +/* +** Emits common vars needed by the rest of the emitted code (but not +** needed by makefile code outside of these generated pieces). +*/ +static void mk_prologue(void){ + /* A 0-terminated list of makefile vars which we expect to have been + ** set up by this point in the build process. */ + char const * aRequiredVars[] = { + "dir.top", + "dir.api", "dir.dout", "dir.tmp", + "dir.fiddle", "dir.fiddle.debug", + /*"just-testing",*/ + 0 + }; + char const * zVar; + int i; + ps(zBanner "# Build setup sanity checks..."); + for( i = 0; (zVar = aRequiredVars[i]); ++i ){ + pf("ifeq (,$(%s))\n", zVar); + pf(" $(error build process error: expecting make var $$(%s) to " + "have been set up by now)\n", zVar); + ps("endif"); + } + + ps("define label.unsupported-build\n" + "$(emo.fire)$(emo.fire)$(emo.fire)Unsupported build:" + " use at your own risk!\n" + "endef"); + + ps(zBanner + /** $1 = build name */ + "b.call.wasm-strip = " + "echo '[$(emo.b.$(1)) $(out.$(1).wasm)] $(emo.strip) wasm-strip'; " + "$(bin.wasm-strip) $(out.$(1).wasm)\n" + ); + + pf(zBanner + "define b.do.emcc\n" + /* $1 = build name */ + "$(bin.emcc) -o $@ $(emcc_opt_full) $(emcc.flags) " + "$(emcc.jsflags) -sENVIRONMENT=$(emcc.environment.$(1)) " + " $(pre-post.$(1).flags) " + " $(emcc.flags.$(1)) " + " $(cflags.common) $(cflags.$(1)) " + " $(SQLITE_OPT) " + " $(cflags.wasm_extra_init) $(sqlite3-wasm.c.in)\n" + "endef\n" + ); + + { + /* b.do.wasm-opt + ** + ** $1 = build name + ** + ** Runs $(out.$(1).wasm) through $(bin.wasm-opt) + */ + const char * zOptFlags = + /* + ** Flags for wasm-opt. It has many, many, MANY "passes" options + ** and the ones which appear here were selected solely on the + ** basis of trial and error. + ** + ** All wasm file size savings/costs mentioned below are based on + ** the vanilla build of sqlite3.wasm with -Oz (our shipping + ** configuration). Comments like "saves nothing" may not be + ** technically correct: "nothing" means "some neglible amount." + ** + ** Note that performance gains/losses are _not_ taken into + ** account here: only wasm file size. + */ + "--enable-bulk-memory-opt " /* required */ + "--all-features " /* required */ + "--post-emscripten " /* Saves roughly 12kb */ + "--strip-debug " /* We already wasm-strip, but in + ** case this environment has no + ** wasm-strip... */ + /* + ** The rest are trial-and-error. See wasm-opt --help and search + ** for "Optimization passes" to find the full list. + ** + ** With many flags this gets unusuably slow. + */ + /*"--converge " saves nothing for the options we're using */ + /*"--dce " saves nothing */ + /*"--directize " saves nothing */ + /*"--gsi " no: requires --closed-world flag, which does not + ** sound like something we want. */ + /*"--gufa --gufa-cast-all --gufa-optimizing " costs roughly 2kb */ + /*"--heap-store-optimization " saves nothing */ + /*"--heap2local " saves nothing */ + //"--inlining --inlining-optimizing " costs roughly 3kb */ + "--local-cse " /* saves roughly 1kb */ + /*"--once-reduction " saves nothing */ + /*"--remove-memory-init " presumably a performance tweak */ + /*"--remove-unused-names " saves nothing */ + /*"--safe-heap "*/ + /*"--vacuum " saves nothing */ + ; + ps(zBanner + "# post-compilation WASM file optimization"); + + /* b.do.wasm-opt $1 = build name*/ + ps("ifeq (,$(bin.wasm-opt))"); { + ps("b.do.wasm-opt = echo '$(logtag.$(1)) wasm-opt not available'"); + } + ps("else"); { + ps("define b.do.wasm-opt"); + pf( + "echo '[$(emo.b.$(1)) $(out.$(1).wasm)] $(emo.wasm-opt) $(bin.wasm-opt)';\\\n" + "\ttmpfile=$(dir.dout.$(1))/wasm-opt-tmp.$(1).wasm; \\\n" + "\trm -f $$tmpfile; \\\n" + "\tif $(bin.wasm-opt) $(out.$(1).wasm) " + "-o $$tmpfile \\\n" + "\t\t%s; then \\\n" + "\t\tmv $$tmpfile $(out.$(1).wasm); \\\n" +#if 0 + "\t\techo -n 'After wasm-opt: '; \\\n" + "\t\tls -l $(1); \\\n" +#endif + /* It's very likely that the set of wasm-opt flags varies from + ** version to version, so we'll ignore any errors here. */ + "\telse \\\n" + "\t\trm -f $$tmpfile; \\\n" + "\t\techo '$(logtag.$(1)) $(emo.fire) ignoring wasm-opt failure'; \\\n" + "\tfi\n", + zOptFlags + ); + ps("endef"); + } + ps("endif"); + } + + ps("more: all"); +} + +/* +** WASM_CUSTOM_INSTANTIATE changes how the JS pieces load the .wasm +** file from the .js file. When set, our JS takes over that step from +** Emscripten. Both modes are functionally equivalent but +** customization gives us access to wasm module state which we don't +** otherwise have. That said, the library also does not _need_ that +** state, so we don't _need_ to customize that step. +*/ +#if !defined(WASM_CUSTOM_INSTANTIATE) +# define WASM_CUSTOM_INSTANTIATE 0 +#elif (WASM_CUSTOM_INSTANTIATE+0)==0 +# undef WASM_CUSTOM_INSTANTIATE +# define WASM_CUSTOM_INSTANTIATE 0 +#endif + +#if WASM_CUSTOM_INSTANTIATE +/* c-pp -D... flags for the custom instantiateWasm(). */ +#define C_PP_D_CUSTOM_INSTANTIATE " -DModule.instantiateWasm " +#else +#define C_PP_D_CUSTOM_INSTANTIATE +#endif + +static char const * BuildDef_jsext(const BuildDef * pB){ + return (F_ESM & pB->flags) ? ".mjs" : ".js"; +} + +static char const * BuildDef_basename(const BuildDef * pB){ + return pB->zBaseName ? pB->zBaseName : oBuildDefs.vanilla.zBaseName; +} + +/* +** Emits makefile code for setting up values for the --pre-js=FILE, +** --post-js=FILE, and --extern-post-js=FILE emcc flags, as well as +** populating those files. This is necessary for any builds which +** embed the library's JS parts of this build (as opposed to parts +** which do not use the library-level code). +** +** pB may be NULL. +*/ +static void mk_pre_post(char const *zBuildName, BuildDef const * pB){ + char const * const zBaseName = pB + ? BuildDef_basename(pB) : 0; + + assert( zBuildName ); + pf("%s# Begin --pre/--post flags for %s\n", zBanner, zBuildName); + + ps("# --pre-js=..."); + pf("pre-js.%s.js = $(dir.tmp)/pre-js.%s.js\n", + zBuildName, zBuildName); + + if( 0==WASM_CUSTOM_INSTANTIATE || !pB ){ + pf("$(eval $(call b.c-pp.target," + "%s," + "$(pre-js.in.js)," + "$(pre-js.%s.js)," + "$(c-pp.D.%s)" + "))", + zBuildName, zBuildName, zBuildName); + }else{ + char const *zWasmFile = pB->zDotWasm + ? pB->zDotWasm + : pB->zBaseName; + /* + ** See BuildDef::zDotWasm for _why_ we do this. _What_ we're doing + ** is generate $(pre-js.BUILDNAME.js) as above, but: + ** + ** 1) Add an extra -D... flag to activate the custom + ** Module.intantiateWasm() in the JS code. + ** + ** 2) Amend the generated pre-js.js with the name of the WASM + ** file which should be loaded. That tells the custom + ** Module.instantiateWasm() to use that file instead of + ** the default. + */ + pf("$(pre-js.%s.js): $(pre-js.in.js) $(bin.c-pp) $(MAKEFILE_LIST)", + zBuildName); + if( pB->zDotWasm ){ + pf(" $(dir.dout)/%s.wasm" /* This .wasm is from some other + build, so this may trigger a full + build of the reference copy. */, + pB->zDotWasm); + } + ps(""); + pf("\t@$(call b.c-pp.shcmd," + "%s," + "$(pre-js.in.js)," + "$(pre-js.%s.js)," + "$(c-pp.D.%s)" C_PP_D_CUSTOM_INSTANTIATE + ")\n", + zBuildName, zBuildName, zBuildName); + } + + ps("\n# --post-js=..."); + pf("post-js.%s.js = $(dir.tmp)/post-js.%s.js\n", + zBuildName, zBuildName); + pf("post-js.%s.in =" + " $(dir.api)/post-js-header.js" + " $(sqlite3-api.%s.js)" + " $(dir.api)/post-js-footer.js\n", + zBuildName, zBuildName); + + pf("$(eval $(call b.c-pp.target," + "%s," + "$(post-js.%s.in)," + "$(post-js.%s.js)," + "$(c-pp.D.%s)" + "))\n", + zBuildName, zBuildName, zBuildName, zBuildName); + + pf("$(post-js.%s.js): $(post-js.%s.in) $(bin.c-pp)\n", + zBuildName, zBuildName); + + ps("\n# --extern-post-js=..."); + pf("extern-post-js.%s.js = $(dir.tmp)/extern-post-js.%s.js\n", + zBuildName, zBuildName); + if( 0!=WASM_CUSTOM_INSTANTIATE && zBaseName ){ + pf("$(eval $(call b.c-pp.target," + "%s," + "$(extern-post-js.in.js)," + "$(extern-post-js.%s.js)," + "$(c-pp.D.%s) --@policy=error -Dsqlite3.wasm=%s.wasm" + "))", + zBuildName, zBuildName, zBuildName, + zBaseName); + }else{ + pf("$(eval $(call b.c-pp.target," + "%s," + "$(extern-post-js.in.js)," + "$(extern-post-js.%s.js)," + "$(c-pp.D.%s)" + "))", + zBuildName, zBuildName, zBuildName); + } + + ps("\n# --pre/post misc..."); + /* Combined flags for use with emcc... */ + pf("pre-post.%s.flags = " + "--extern-pre-js=$(sqlite3-license-version.js) " + "--pre-js=$(pre-js.%s.js) " + "--post-js=$(post-js.%s.js) " + "--extern-post-js=$(extern-post-js.%s.js)\n", + zBuildName, zBuildName, zBuildName, zBuildName); + + + /* Set up deps... */ + pf("pre-post.%s.deps = " + "$(pre-post-jses.common.deps) " + "$(post-js.%s.js) $(extern-post-js.%s.js) " + "$(dir.tmp)/pre-js.%s.js\n", + zBuildName, zBuildName, zBuildName, zBuildName); + pf("# End --pre/--post flags for %s%s", zBuildName, zBanner); +} + +static void emit_compile_start(char const *zBuildName){ + pf("\t@$(call b.mkdir@);" + " $(call b.echo,%s,$(emo.compile) building ...)\n", + zBuildName); +} + +static void emit_logtag(char const *zBuildName){ +#if 1 + pf("logtag.%s ?= [$(emo.b.%s)$(if $@, $@,)]:\n", + zBuildName, zBuildName); +#else + pf("logtag.%s ?= [$(emo.b.%s) [%s] $@]:\n", + zBuildName, zBuildName, zBuildName); +#endif + pf("$(info $(logtag.%s) Setting up target b-%s)\n", + zBuildName, zBuildName ); +} + +/** + Emit rules for sqlite3-api.${zBuildName}.js. +*/ +static void emit_api_js(char const *zBuildName){ + pf("sqlite3-api.%s.js = $(dir.tmp)/sqlite3-api.%s.js\n", + zBuildName, zBuildName); + pf("$(eval $(call b.c-pp.target," + "%s," + "$(sqlite3-api.jses)," + "$(sqlite3-api.%s.js)," + "$(c-pp.D.%s)" + "))\n", + zBuildName, zBuildName, zBuildName); + pf("$(out.%s.js): $(sqlite3-api.%s.js)\n", + zBuildName, zBuildName); +} + +/* +** Emits makefile code for one build of the library. +*/ +static void mk_lib_mode(const char *zBuildName, const BuildDef * pB){ + const char * zJsExt = BuildDef_jsext(pB); + char const * const zBaseName = BuildDef_basename(pB); + + assert( oBuildDefs.vanilla.zEnv ); + assert( zBaseName ); + + pf("%s# Begin build [%s%s]. flags=0x%02x\n", zBanner, + pB->zEmo, zBuildName, pB->flags); + pf("# zCmppD=%s\n# zBaseName=%s\n", + pB->zCmppD ? pB->zCmppD : "", zBaseName); + pf("b.names += %s\n" + "emo.b.%s = %s\n", + zBuildName, zBuildName, pB->zEmo); + emit_logtag(zBuildName); + + if( pB->zIfCond ){ + pf("%s\n", pB->zIfCond ); + } + + pf("dir.dout.%s ?= $(dir.dout)/%s\n", zBuildName, zBuildName); + + pf("out.%s.base ?= $(dir.dout.%s)/%s\n", + zBuildName, zBuildName, zBaseName); + pf("out.%s.js ?= $(dir.dout.%s)/%s%s\n", + zBuildName, zBuildName, zBaseName, zJsExt); + pf("out.%s.wasm ?= $(dir.dout.%s)/%s.wasm\n", + //"$(basename $@).wasm" + zBuildName, zBuildName, zBaseName); + + pf("dir.dout.%s ?= $(dir.dout)/%s\n", zBuildName, zBuildName); + pf("out.%s.base ?= $(dir.dout.%s)/%s\n", + zBuildName, zBuildName, zBaseName); + + pf("c-pp.D.%s ?= %s\n", zBuildName, pB->zCmppD ? pB->zCmppD : ""); + if( pB->flags & F_64BIT ){ + pf("c-pp.D.%s += $(c-pp.D.64bit)\n", zBuildName); + } + + pf("emcc.environment.%s ?= %s\n", zBuildName, + pB->zEnv ? pB->zEnv : oBuildDefs.vanilla.zEnv); + if( pB->zEmccExtra ){ + pf("emcc.flags.%s = %s\n", zBuildName, pB->zEmccExtra); + } + + if( pB->zDeps ){ + pf("deps.%s += %s\n", zBuildName, pB->zDeps); + } + + emit_api_js(zBuildName); + mk_pre_post(zBuildName, pB); + + { /* build it... */ + pf(zBanner + "$(out.%s.js): $(MAKEFILE_LIST) $(sqlite3-wasm.c.in)" + " $(EXPORTED_FUNCTIONS.api) $(deps.%s)" + " $(bin.mkwb) $(pre-post.%s.deps)" + "\n", + zBuildName, zBuildName, zBuildName); + + emit_compile_start(zBuildName); + + if( F_UNSUPPORTED & pB->flags ){ + pf("\t@echo '$(logtag.%s) $(label.unsupported-build)'\n", + zBuildName); + } + + /* emcc ... */ + { + pf("\t$(b.cmd@)$(bin.emcc) -o $@ "); + if( pB->zEmcc ){ + pf("%s $(emcc.flags.%s)\n", + pB->zEmcc, zBuildName); + }else{ + pf("$(emcc_opt_full) $(emcc.flags)" + " $(emcc.jsflags)" + " -sENVIRONMENT=$(emcc.environment.%s)" + " $(pre-post.%s.flags)" + " $(emcc.flags.%s)" + " $(cflags.common)" + " $(cflags.%s)" + " $(SQLITE_OPT)" + " $(cflags.wasm_extra_init) $(sqlite3-wasm.c.in)\n", + zBuildName, zBuildName, zBuildName, zBuildName + ); + } + } + + { /* Post-compilation transformations and copying to + $(dir.dout)... */ + + /* Avoid a 3rd occurrence of the bug fixed by 65798c09a00662a3, + ** which was (in two cases) caused by makefile refactoring and + ** not recognized until after a release was made with the broken + ** sqlite3-bundler-friendly.mjs (which is used by the npm + ** subproject but is otherwise untested/unsupported): */ + pf("\t@if grep -e '^ *importScripts(' $@; " + "then echo '$(logtag.%s) $(emo.bug)$(emo.fire): " + "bug fixed in 65798c09a00662a3 has re-appeared'; " + "exit 1; fi;\n", zBuildName); + + if( (F_ESM & pB->flags) || (F_NODEJS & pB->flags) ){ + pf("\t@$(call b.call.patch-export-default,1,%d,$(logtag.%s))\n", + (F_WASMFS & pB->flags) ? 1 : 0, + zBuildName + ); + } + + pf("\t@chmod -x $(out.%s.wasm)\n", zBuildName + /* althttpd will automatically try to execute wasm files + if they have the +x bit set. Why that bit is set + at all is a mystery. */); + pf("\t@$(call b.call.wasm-strip,%s)\n", zBuildName); + + pf("\t@$(call b.do.wasm-opt,%s)\n", zBuildName); + pf("\t@$(call b.strip-js-emcc-bindings,$(logtag.%s))\n", zBuildName); + + if( CP_JS & pB->flags ){ + /* + ** $(bin.emcc) will write out $@ and will create a like-named + ** .wasm file. The resulting .wasm and .js/.mjs files are + ** identical across all builds which have the same pB->zEmmc + ** and/or pB->zEmccExtra. + ** + ** For the final deliverables we copy one or both of those + ** js/wasm files to $(dir.dout) (the top-most build target + ** dir). We only copy the wasm file for the "base-most" builds + ** and recycle those for the rest of the builds. The catch is: + ** that .wasm file name gets hard-coded into $@ so we need, + ** for cases in which we "recycle" a .wasm file from another + ** build, to patch the name to pB->zDotWasm when copying to + ** $(dir.dout). + */ + if( pB->zDotWasm ){ + pf("\t@echo '$(logtag.%s) $(emo.disk) " + "s/\"%s.wasm\"/\"%s.wasm\"/g " + "in $(dir.dout)/$(notdir $@)'; \\\n" + "sed" + " -e 's/\"%s.wasm\"/\"%s.wasm\"/g'" + " -e \"s/'%s.wasm'/'%s.wasm'/g\"" + " $@ > $(dir.dout)/$(notdir $@);\n", + zBuildName, + zBaseName, pB->zDotWasm, + zBaseName, pB->zDotWasm, + zBaseName, pB->zDotWasm); + }else{ + pf("\t@$(call b.cp,%s,$@,$(dir.dout))\n", + zBuildName); + } + } + if( CP_WASM & pB->flags ){ + pf("\t@$(call b.cp,%s,$(basename $@).wasm,$(dir.dout))\n", + zBuildName + //"\techo '[%s $(out.%s.wasm)] $(emo.disk) $(dir.dout)/$(notdir $(out.%s.wasm))' + //pB->zEmo, zBuildName + ); + } + + } + } + pf("\t@$(call b.echo,%s,$(emo.done) done!%s)\n", + zBuildName, + (F_UNSUPPORTED & pB->flags) + ? " $(label.unsupported-build)" + : ""); + + pf("\n%dbit: $(out.%s.js)\n" + "$(out.%s.wasm): $(out.%s.js)\n" + "b-%s: $(out.%s.js) $(out.%s.wasm)\n", + (F_64BIT & pB->flags) ? 64 : 32, zBuildName, + zBuildName, zBuildName, + zBuildName, zBuildName, zBuildName); + + if( CP_JS & pB->flags ){ + pf("$(dir.dout)/%s%s: $(out.%s.js)\n", + zBaseName, zJsExt, zBuildName + ); + } + + if( CP_WASM & pB->flags ){ + pf("$(dir.dout)/%s.wasm: $(out.%s.wasm)\n", + zBaseName, zBuildName + ); + } + + pf("%s: $(out.%s.js)\n", + 0==((F_UNSUPPORTED | F_NOT_IN_ALL) & pB->flags) + ? "all" : "more", zBuildName); + + if( pB->zIfCond ){ + pf("else\n" + "$(info $(logtag.%s) $(emo.stop) disabled by condition: %s)\n" + "endif\n", + zBuildName, pB->zIfCond); + } + pf("# End build [%s]%s", zBuildName, zBanner); +} + + +static void emit_gz(char const *zBuildName, + char const *zFileExt){ + pf("\n$(out.%s.%s).gz: $(out.%s.%s)\n" + "\t@$(call b.echo,%s,$(emo.disk))\n" + "\t@gzip < $< > $@\n", + zBuildName, zFileExt, + zBuildName, zFileExt, + zBuildName); +} + +/* +** Emits rules for the fiddle builds. +*/ +static void mk_fiddle(void){ + for(int i = 0; i < 2; ++i ){ + /* 0==normal, 1==debug */ + int const isDebug = i>0; + const char * const zBuildName = i ? "fiddle.debug" : "fiddle"; + + pf(zBanner "# Begin build %s\n", zBuildName); + if( isDebug ){ + pf("emo.b.%s = $(emo.b.fiddle)$(emo.bug)\n", zBuildName); + }else{ + pf("emo.b.fiddle = 🎻\n"); + } + emit_logtag(zBuildName); + + pf("dir.%s = %s\n" + "out.%s.js = $(dir.%s)/fiddle-module.js\n" + "out.%s.wasm = $(dir.%s)/fiddle-module.wasm\n" + "$(out.%s.wasm): $(out.%s.js)\n", + zBuildName, zBuildName, + zBuildName, zBuildName, + zBuildName, zBuildName, + zBuildName, zBuildName); + + emit_api_js(zBuildName); + mk_pre_post(zBuildName, 0); + + {/* emcc */ + pf("$(out.%s.js): $(MAKEFILE_LIST) " + "$(EXPORTED_FUNCTIONS.fiddle) " + "$(fiddle.c.in) " + "$(pre-post.%s.deps)\n", + zBuildName, zBuildName); + emit_compile_start(zBuildName); + pf("\t$(b.cmd@)$(bin.emcc) -o $@" + " $(emcc.flags.%s)" /* set in fiddle.make */ + " $(pre-post.%s.flags)" + " $(fiddle.c.in)" + "\n", + zBuildName, zBuildName); + pf("\t@chmod -x $(out.%s.wasm)\n", zBuildName); + pf("\t@$(call b.call.wasm-strip,%s)\n", zBuildName); + pf("\t@$(call b.strip-js-emcc-bindings,$(logtag.%s))\n", + zBuildName); + pf("\t@$(call b.cp," + "%s," + "$(dir.api)/sqlite3-opfs-async-proxy.js," + "$(dir $@))\n", zBuildName); + if( isDebug ){ + pf("\t@$(call b.cp,%s," + "$(dir.fiddle)/index.html " + "$(dir.fiddle)/fiddle.js " + "$(dir.fiddle)/fiddle-worker.js," + "$(dir $@)" + ")\n", + zBuildName); + } + pf("\t@$(call b.echo,%s,$(emo.done) done!)\n", zBuildName); + } + + pf("\n%s: $(out.%s.wasm)\n", isDebug ? "more" : "all", zBuildName); + + /* Compress fiddle files. We handle each file separately, rather + than compressing them in a loop in the previous target, to help + avoid that hand-edited files, like fiddle-worker.js, do not end + up with stale .gz files (which althttpd will then serve instead + of the up-to-date uncompressed one). */ + emit_gz(zBuildName, "js"); + emit_gz(zBuildName, "wasm"); + + pf("\n%s: $(out.%s.js).gz $(out.%s.wasm).gz\n" + "b-%s: %s\n", + zBuildName, zBuildName, zBuildName, + zBuildName, zBuildName); + if( isDebug ){ + ps("fiddle-debug: fiddle.debug"); /* older name */ + }else{ + ps("all: b-fiddle"); + } + pf("# End %s" zBanner, zBuildName); + } +} + +int main(int argc, char const ** argv){ + int rc = 0; + const BuildDef *pB; + pf("# What follows was GENERATED by %s. Edit at your own risk.\n", __FILE__); + + if(argc>1){ + /* + ** Only emit the rules for the given list of builds, sans prologue + ** (unless the arg "prologue" is given). Intended only for + ** debugging, not actual makefile generation. + */ + for( int i = 1; i < argc; ++i ){ + char const * const zArg = argv[i]; +#define E(N) if(0==strcmp(#N, zArg)) {mk_lib_mode(# N, &oBuildDefs.N);} else /**/ + BuildDefs_map(E) if( 0==strcmp("prologue",zArg) ){ + mk_prologue(); + }else { + fprintf(stderr,"Unkown build name: %s\n", zArg); + rc = 1; + break; + } +#undef E + } + }else{ + /* + ** Emit the whole shebang... + */ + mk_prologue(); +#define E(N) mk_lib_mode(# N, &oBuildDefs.N); + BuildDefs_map(E) +#undef E + mk_fiddle(); + } + return rc; +} diff --git a/ext/wasm/module-symbols.html b/ext/wasm/module-symbols.html new file mode 100644 index 0000000000..09a8a41e13 --- /dev/null +++ b/ext/wasm/module-symbols.html @@ -0,0 +1,541 @@ + + + + + + + sqlite3 Module Symbols + + + +
    + + +

    Loading WASM module... + If this takes "a long time" it may have failed and the browser's + dev console may contain hints as to why. +

    + +

    + This page lists the SQLite3 APIs exported + by sqlite3.wasm and exposed to clients + by sqlite3.js. These lists are generated dynamically + by loading the JS/WASM module and introspecting it, with the following + caveats: +

    + +
      +
    • Some APIs are explicitly filtered out of these lists because + they are strictly for internal use within the JS/WASM APIs and + its own test code. +
    • +
    • This page runs in the main UI thread so cannot see features + which are only available in a Worker thread. If this page were + to function via a Worker, it would not be able to see + functionality only available in the main thread. Either way, it + would be missing certain APIs. +
    • +
    + +
    + +

    This page exposes a global symbol named sqlite3 + which can be inspected using the browser's dev tools. +

    + +

    Jump to...

    + + + +

    sqlite3 Namespace

    +

    + The sqlite3 namespace object exposes the following... +

    +
    + + +

    sqlite3.version Object

    +

    + The sqlite3.version object exposes the following... +

    +
    + + +

    sqlite3_...() Function List

    + +

    The sqlite3.capi namespace exposes the following + sqlite3_...() + functions... +

    +
    +

    + = function is specific to the JS/WASM + bindings, not part of the C API. +

    + + +

    SQLITE_... Constants

    + +

    The sqlite3.capi namespace exposes the following + SQLITE_... + constants... +

    +
    + + +

    sqlite3.oo1 Namespace

    +

    + The sqlite3.oo1 namespace exposes the following... +

    +
    + + +

    sqlite3.wasm Namespace

    +

    + The sqlite3.wasm namespace exposes the + following... +

    +
    + + +

    sqlite3.wasm.pstack Namespace

    +

    + The sqlite3.wasm.pstack namespace exposes the + following... +

    +
    + + +

    sqlite3.wasm.ptr Namespace

    +

    + The sqlite3.wasm.ptr namespace exposes the + following... +

    +
    + + +

    Compilation Options

    +

    + SQLITE_... compilation options used in this build + of sqlite3.wasm... +

    +
    + +
    + + +
    + diff --git a/ext/wasm/scratchpad-wasmfs.html b/ext/wasm/scratchpad-wasmfs.html new file mode 100644 index 0000000000..c37febff18 --- /dev/null +++ b/ext/wasm/scratchpad-wasmfs.html @@ -0,0 +1,30 @@ + + + + + + + + + sqlite3 WASMFS/OPFS Main-thread Scratchpad + + +
    sqlite3 WASMFS/OPFS Main-thread Scratchpad
    +

    Scratchpad/test app for the WASMF/OPFS integration in the + main window thread. This page requires that the sqlite3 API have + been built with WASMFS support. If OPFS support is available then + it "should" persist a database across reloads (watch the dev console + output), otherwise it will not. +

    +

    All stuff on this page happens in the dev console.

    +
    +
    + + + diff --git a/ext/wasm/scratchpad-wasmfs.mjs b/ext/wasm/scratchpad-wasmfs.mjs new file mode 100644 index 0000000000..d6b69a1d6e --- /dev/null +++ b/ext/wasm/scratchpad-wasmfs.mjs @@ -0,0 +1,70 @@ +/* + 2022-05-22 + + The author disclaims copyright to this source code. In place of a + legal notice, here is a blessing: + + * May you do good and not evil. + * May you find forgiveness for yourself and forgive others. + * May you share freely, never taking more than you give. + + *********************************************************************** + + A basic test script for sqlite3-api.js. This file must be run in + main JS thread and sqlite3.js must have been loaded before it. +*/ +import sqlite3InitModule from './jswasm/sqlite3-wasmfs.mjs'; +//console.log('sqlite3InitModule =',sqlite3InitModule); +const toss = function(...args){throw new Error(args.join(' '))}; +const log = console.log.bind(console), + warn = console.warn.bind(console), + error = console.error.bind(console); + +const stdout = log; +const stderr = error; + +const test1 = function(db){ + db.exec("create table if not exists t(a);") + .transaction(function(db){ + db.prepare("insert into t(a) values(?)") + .bind(new Date().getTime()) + .stepFinalize(); + stdout("Number of values in table t:", + db.selectValue("select count(*) from t")); + }); +}; + +const runTests = function(sqlite3){ + const capi = sqlite3.capi, + oo = sqlite3.oo1, + wasm = sqlite3.wasm; + stdout("Loaded module:",sqlite3); + stdout("Loaded sqlite3:",capi.sqlite3_libversion(), capi.sqlite3_sourceid()); + const persistentDir = capi.sqlite3_wasmfs_opfs_dir(); + if(persistentDir){ + stdout("Persistent storage dir:",persistentDir); + }else{ + stderr("No persistent storage available."); + } + const startTime = performance.now(); + let db; + try { + db = new oo.DB(persistentDir+'/foo.db'); + stdout("DB filename:",db.filename); + const banner1 = '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>', + banner2 = '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<'; + [ + test1 + ].forEach((f)=>{ + const n = performance.now(); + stdout(banner1,"Running",f.name+"()..."); + f(db, sqlite3); + stdout(banner2,f.name+"() took ",(performance.now() - n),"ms"); + }); + }finally{ + if(db) db.close(); + } + stdout("Total test time:",(performance.now() - startTime),"ms"); +}; + +sqlite3InitModule().then(runTests); diff --git a/ext/wasm/speedtest1-wasmfs.html b/ext/wasm/speedtest1-wasmfs.html new file mode 100644 index 0000000000..c018583d0f --- /dev/null +++ b/ext/wasm/speedtest1-wasmfs.html @@ -0,0 +1,55 @@ + + + + + + + + + speedtest1-wasmfs.wasm + + +
    speedtest1-wasmfs.wasm
    + +
    Achtung: running it with the dev tools open may + drastically slow it down. For faster results, keep the dev + tools closed when running it! +
    +
    + + + diff --git a/ext/wasm/speedtest1-wasmfs.mjs b/ext/wasm/speedtest1-wasmfs.mjs new file mode 100644 index 0000000000..0e46806678 --- /dev/null +++ b/ext/wasm/speedtest1-wasmfs.mjs @@ -0,0 +1,90 @@ +import sqlite3InitModule from './jswasm/sqlite3-wasmfs.mjs'; +const wMsg = (type,...args)=>{ + postMessage({type, args}); +}; +wMsg('log',"speedtest1-wasmfs starting..."); +/** + If this environment contains OPFS, this function initializes it and + returns the name of the dir on which OPFS is mounted, else it returns + an empty string. +*/ +const wasmfsDir = function f(wasmUtil,dirName="/opfs"){ + if(undefined !== f._) return f._; + if( !globalThis.FileSystemHandle + || !globalThis.FileSystemDirectoryHandle + || !globalThis.FileSystemFileHandle){ + return f._ = ""; + } + try{ + if(0===wasmUtil.xCallWrapped( + 'sqlite3__wasm_init_wasmfs', 'i32', ['string'], dirName + )){ + return f._ = dirName; + }else{ + return f._ = ""; + } + }catch(e){ + // sqlite3_wasm_init_wasmfs() is not available + return f._ = ""; + } +}; +wasmfsDir._ = undefined; + +const log = (...args)=>wMsg('log',...args); +const logErr = (...args)=>wMsg('logErr',...args); + +const runTests = function(sqlite3){ + console.log("Module inited.",sqlite3); + const wasm = sqlite3.wasm; + const __unlink = wasm.xWrap("sqlite3__wasm_vfs_unlink", "int", ["*","string"]); + const unlink = (fn)=>__unlink(0,fn); + const pDir = wasmfsDir(wasm); + if(pDir) log("Persistent storage:",pDir); + else{ + logErr("Expecting persistent storage in this build."); + return; + } + const scope = wasm.scopedAllocPush(); + const dbFile = pDir+"/speedtest1.db"; + const urlParams = new URL(globalThis.location.href).searchParams; + const argv = ["speedtest1"]; + if(urlParams.has('flags')){ + argv.push(...(urlParams.get('flags').split(','))); + let i = argv.indexOf('--vfs'); + if(i>=0) argv.splice(i,2); + }else{ + argv.push( + "--singlethread", + "--nomutex", + //"--nosync", + "--nomemstat", + "--size", "10" + ); + } + + if(argv.indexOf('--memdb')>=0){ + logErr("WARNING: --memdb flag trumps db filename."); + } + argv.push("--big-transactions"/*important for tests 410 and 510!*/, + dbFile); + //log("argv =",argv); + // These log messages are not emitted to the UI until after main() returns. Fixing that + // requires moving the main() call and related cleanup into a timeout handler. + if(pDir) unlink(dbFile); + log("Starting native app:\n ",argv.join(' ')); + log("This will take a while and the browser might warn about the runaway JS.", + "Give it time..."); + setTimeout(function(){ + if(pDir) unlink(dbFile); + wasm.xCall('wasm_main', argv.length, + wasm.scopedAllocMainArgv(argv)); + wasm.scopedAllocPop(scope); + if(pDir) unlink(dbFile); + log("Done running native main()"); + }, 25); +}/*runTests()*/; + +sqlite3InitModule({ + print: log, + printErr: logErr +}).then(runTests); diff --git a/ext/wasm/speedtest1-worker.html b/ext/wasm/speedtest1-worker.html new file mode 100644 index 0000000000..c207e51824 --- /dev/null +++ b/ext/wasm/speedtest1-worker.html @@ -0,0 +1,373 @@ + + + + + + + + + speedtest1.wasm Worker + + +
    speedtest1.wasm Worker
    + + +
    +
    +
    Initializing app...
    +
    + On a slow internet connection this may take a moment. If this + message displays for "a long time", intialization may have + failed and the JavaScript console may contain clues as to why. +
    +
    +
    Downloading...
    +
    + +
    + +
    + + + + +
    +
    +
    +
    + Tips: +
      +
    • Control-click the flags to (de)select multiple flags.
    • +
    • The --big-transactions flag is important for two + of the bigger tests. Without it, those tests create many + thousands of implicit transactions, reducing the affected + tests to an absolute crawl, in particular with OPFS. +
    • +
    • The easiest way to try different optimization levels is, + from this directory: +
      $ rm -f jswasm/speedtest1.js; make -e emcc_opt='-O2' speedtest1
      + Then reload this page. -O2 seems to consistently produce the fastest results. +
    • +
    +
    + + + + diff --git a/ext/wasm/speedtest1-worker.js b/ext/wasm/speedtest1-worker.js new file mode 100644 index 0000000000..ba11fd163d --- /dev/null +++ b/ext/wasm/speedtest1-worker.js @@ -0,0 +1,128 @@ +'use strict'; +(function(){ + let speedtestJs = 'speedtest1.js'; + const urlParams = new URL(self.location.href).searchParams; + if(urlParams.has('sqlite3.dir')){ + speedtestJs = urlParams.get('sqlite3.dir') + '/' + speedtestJs; + } + importScripts(speedtestJs); + /** + If this build includes WASMFS, this function initializes it and + returns the name of the dir on which OPFS is mounted, else it + returns an empty string. + */ + const wasmfsDir = function f(wasmUtil){ + if(undefined !== f._) return f._; + const pdir = '/opfs'; + if( !self.FileSystemHandle + || !self.FileSystemDirectoryHandle + || !self.FileSystemFileHandle){ + return f._ = ""; + } + try{ + if(0===wasmUtil.xCallWrapped( + 'sqlite3_wasm_init_wasmfs', 'i32', ['string'], pdir + )){ + return f._ = pdir; + }else{ + return f._ = ""; + } + }catch(e){ + // sqlite3_wasm_init_wasmfs() is not available + return f._ = ""; + } + }; + wasmfsDir._ = undefined; + + const mPost = function(msgType,payload){ + postMessage({type: msgType, data: payload}); + }; + + const App = Object.create(null); + App.logBuffer = []; + const logMsg = (type,msgArgs)=>{ + const msg = msgArgs.join(' '); + App.logBuffer.push(msg); + mPost(type,msg); + }; + const log = (...args)=>logMsg('stdout',args); + const logErr = (...args)=>logMsg('stderr',args); + const realSahName = 'opfs-sahpool-speedtest1'; + + const runSpeedtest = async function(cliFlagsArray){ + const scope = App.wasm.scopedAllocPush(); + const dbFile = App.pDir+"/speedtest1.sqlite3"; + try{ + const argv = [ + "speedtest1.wasm", ...cliFlagsArray, dbFile + ]; + App.logBuffer.length = 0; + const ndxSahPool = argv.indexOf('opfs-sahpool'); + if(ndxSahPool>0){ + argv[ndxSahPool] = realSahName; + log("Updated argv for opfs-sahpool: --vfs",realSahName); + } + mPost('run-start', [...argv]); + if(App.sqlite3.installOpfsSAHPoolVfs + && !App.sqlite3.$SAHPoolUtil + && ndxSahPool>0){ + log("Installing opfs-sahpool as",realSahName,"..."); + await App.sqlite3.installOpfsSAHPoolVfs({ + name: realSahName, + initialCapacity: 3, + clearOnInit: true, + verbosity: 2 + }).then(PoolUtil=>{ + log("opfs-sahpool successfully installed as",PoolUtil.vfsName); + App.sqlite3.$SAHPoolUtil = PoolUtil; + //console.log("sqlite3.oo1.OpfsSAHPoolDb =", App.sqlite3.oo1.OpfsSAHPoolDb); + }); + } + App.wasm.xCall('wasm_main', argv.length, + App.wasm.scopedAllocMainArgv(argv)); + log("WASM heap size:",App.wasm.heap8().byteLength,"bytes"); + log("WASM pointer size:",App.wasm.ptr.size); + + }catch(e){ + mPost('error',e.message); + }finally{ + App.wasm.scopedAllocPop(scope); + mPost('run-end', App.logBuffer.join('\n')); + App.logBuffer.length = 0; + } + }; + + self.onmessage = function(msg){ + msg = msg.data; + switch(msg.type){ + case 'run': + runSpeedtest(msg.data || []) + .catch(e=>mPost('error',e)); + break; + default: + logErr("Unhandled worker message type:",msg.type); + break; + } + }; + + const EmscriptenModule = { + print: log, + printErr: logErr, + setStatus: (text)=>mPost('load-status',text) + }; + log("Initializing speedtest1 module..."); + self.sqlite3InitModule(EmscriptenModule).then(async (sqlite3)=>{ + const S = globalThis.S = App.sqlite3 = sqlite3; + log("Loaded speedtest1 module. Setting up..."); + App.pDir = wasmfsDir(S.wasm); + App.wasm = S.wasm; + log("WASM heap size:",sqlite3.wasm.heap8().byteLength,"bytes"); + log("WASM pointer size:",sqlite3.wasm.ptr.size); + //if(App.pDir) log("Persistent storage:",pDir); + //else log("Using transient storage."); + mPost('ready',true); + log("Registered VFSes:", ...S.capi.sqlite3_js_vfs_list()); + }).catch(e=>{ + logErr(e); + }); +})(); diff --git a/ext/wasm/speedtest1.html b/ext/wasm/speedtest1.html new file mode 100644 index 0000000000..cce6171850 --- /dev/null +++ b/ext/wasm/speedtest1.html @@ -0,0 +1,176 @@ + + + + + + + + + speedtest1.wasm + + +
    speedtest1.wasm
    + + +
    +
    +
    Initializing app...
    +
    + On a slow internet connection this may take a moment. If this + message displays for "a long time", intialization may have + failed and the JavaScript console may contain clues as to why. +
    +
    +
    Downloading...
    +
    + +
    +
    This page starts running the main exe when it loads, which will + block the UI until it finishes!
    + +
    Achtung: running it with the dev tools open may + drastically slow it down. For faster results, keep the dev + tools closed when running it! +
    +
    Output is delayed/buffered because we cannot update the UI while the + speedtest is running. Output will appear below when ready... +
    + + + + + diff --git a/ext/wasm/split-speedtest1-script.sh b/ext/wasm/split-speedtest1-script.sh new file mode 100755 index 0000000000..e072d08a1e --- /dev/null +++ b/ext/wasm/split-speedtest1-script.sh @@ -0,0 +1,17 @@ +#!/bin/bash +# Expects $1 to be a (speedtest1 --script) output file. Output is a +# series of SQL files extracted from that file. +infile=${1:?arg = speedtest1 --script output file} +testnums=$(grep -e '^-- begin test' "$infile" | cut -d' ' -f4) +if [ x = "x${testnums}" ]; then + echo "Could not parse any begin/end blocks out of $infile" 1>&2 + exit 1 +fi +odir=${infile%%/*} +if [ "$odir" = "$infile" ]; then odir="."; fi +#echo testnums=$testnums +for n in $testnums; do + ofile=$odir/$(printf "speedtest1-%03d.sql" $n) + sed -n -e "/^-- begin test $n /,/^-- end test $n\$/p" $infile > $ofile + echo -e "$n\t$ofile" +done diff --git a/ext/wasm/sql/000-mandelbrot.sql b/ext/wasm/sql/000-mandelbrot.sql new file mode 100644 index 0000000000..3aa5f57156 --- /dev/null +++ b/ext/wasm/sql/000-mandelbrot.sql @@ -0,0 +1,17 @@ +WITH RECURSIVE + xaxis(x) AS (VALUES(-2.0) UNION ALL SELECT x+0.05 FROM xaxis WHERE x<1.2), + yaxis(y) AS (VALUES(-1.0) UNION ALL SELECT y+0.1 FROM yaxis WHERE y<1.0), + m(iter, cx, cy, x, y) AS ( + SELECT 0, x, y, 0.0, 0.0 FROM xaxis, yaxis + UNION ALL + SELECT iter+1, cx, cy, x*x-y*y + cx, 2.0*x*y + cy FROM m + WHERE (x*x + y*y) < 4.0 AND iter<28 + ), + m2(iter, cx, cy) AS ( + SELECT max(iter), cx, cy FROM m GROUP BY cx, cy + ), + a(t) AS ( + SELECT group_concat( substr(' .+*#', 1+min(iter/7,4), 1), '') + FROM m2 GROUP BY cy + ) +SELECT group_concat(rtrim(t),x'0a') as Mandelbrot FROM a; diff --git a/ext/wasm/sql/001-sudoku.sql b/ext/wasm/sql/001-sudoku.sql new file mode 100644 index 0000000000..53661b1c35 --- /dev/null +++ b/ext/wasm/sql/001-sudoku.sql @@ -0,0 +1,28 @@ +WITH RECURSIVE + input(sud) AS ( + VALUES('53..7....6..195....98....6.8...6...34..8.3..17...2...6.6....28....419..5....8..79') + ), + digits(z, lp) AS ( + VALUES('1', 1) + UNION ALL SELECT + CAST(lp+1 AS TEXT), lp+1 FROM digits WHERE lp<9 + ), + x(s, ind) AS ( + SELECT sud, instr(sud, '.') FROM input + UNION ALL + SELECT + substr(s, 1, ind-1) || z || substr(s, ind+1), + instr( substr(s, 1, ind-1) || z || substr(s, ind+1), '.' ) + FROM x, digits AS z + WHERE ind>0 + AND NOT EXISTS ( + SELECT 1 + FROM digits AS lp + WHERE z.z = substr(s, ((ind-1)/9)*9 + lp, 1) + OR z.z = substr(s, ((ind-1)%9) + (lp-1)*9 + 1, 1) + OR z.z = substr(s, (((ind-1)/3) % 3) * 3 + + ((ind-1)/27) * 27 + lp + + ((lp-1) / 3) * 6, 1) + ) + ) +SELECT s FROM x WHERE ind=0; diff --git a/ext/wasm/test-opfs-vfs.html b/ext/wasm/test-opfs-vfs.html new file mode 100644 index 0000000000..235ef51e9f --- /dev/null +++ b/ext/wasm/test-opfs-vfs.html @@ -0,0 +1,26 @@ + + + + + + + + + Async-behind-Sync experiment + + +
    Async-behind-Sync sqlite3_vfs
    +
    This performs a sanity test of the "opfs" sqlite3_vfs. + See the dev console for all output. +
    +
    + Use this link to delete the persistent OPFS-side db (if any). +
    +
    + + + diff --git a/ext/wasm/test-opfs-vfs.js b/ext/wasm/test-opfs-vfs.js new file mode 100644 index 0000000000..96d0eacfc9 --- /dev/null +++ b/ext/wasm/test-opfs-vfs.js @@ -0,0 +1,85 @@ +/* + 2022-09-17 + + The author disclaims copyright to this source code. In place of a + legal notice, here is a blessing: + + * May you do good and not evil. + * May you find forgiveness for yourself and forgive others. + * May you share freely, never taking more than you give. + + *********************************************************************** + + A testing ground for the OPFS VFS. +*/ +'use strict'; +const tryOpfsVfs = async function(sqlite3){ + const toss = function(...args){throw new Error(args.join(' '))}; + const logPrefix = "OPFS tester:"; + const log = (...args)=>console.log(logPrefix,...args); + const warn = (...args)=>console.warn(logPrefix,...args); + const error = (...args)=>console.error(logPrefix,...args); + const opfs = sqlite3.opfs; + log("tryOpfsVfs()"); + if(!sqlite3.opfs){ + const e = new Error("OPFS is not available."); + error(e); + throw e; + } + const capi = sqlite3.capi; + const pVfs = capi.sqlite3_vfs_find("opfs") || toss("Missing 'opfs' VFS."); + const oVfs = new capi.sqlite3_vfs(pVfs); + log("OPFS VFS:",pVfs, oVfs); + + const wait = async (ms)=>{ + return new Promise((resolve)=>setTimeout(resolve, ms)); + }; + + const urlArgs = new URL(self.location.href).searchParams; + const dbFile = "my-persistent.db"; + if(urlArgs.has('delete')) sqlite3.opfs.unlink(dbFile); + + const db = new sqlite3.oo1.OpfsDb(dbFile,'ct'); + log("db file:",db.filename); + try{ + if(opfs.entryExists(dbFile)){ + let n = db.selectValue("select count(*) from sqlite_schema"); + log("Persistent data found. sqlite_schema entry count =",n); + } + db.transaction((db)=>{ + db.exec({ + sql:[ + "create table if not exists t(a);", + "insert into t(a) values(?),(?),(?);", + ], + bind: [performance.now() | 0, + (performance.now() |0) / 2, + (performance.now() |0) / 4] + }); + }); + log("count(*) from t =",db.selectValue("select count(*) from t")); + + // Some sanity checks of the opfs utility functions... + const testDir = '/sqlite3-opfs-'+opfs.randomFilename(12); + const aDir = testDir+'/test/dir'; + await opfs.mkdir(aDir) || toss("mkdir failed"); + await opfs.mkdir(aDir) || toss("mkdir must pass if the dir exists"); + await opfs.unlink(testDir+'/test') && toss("delete 1 should have failed (dir not empty)"); + //await opfs.entryExists(testDir) + await opfs.unlink(testDir+'/test/dir') || toss("delete 2 failed"); + await opfs.unlink(testDir+'/test/dir') && toss("delete 2b should have failed (dir already deleted)"); + await opfs.unlink(testDir, true) || toss("delete 3 failed"); + await opfs.entryExists(testDir) && toss("entryExists(",testDir,") should have failed"); + }finally{ + db.close(); + } + + log("Done!"); +}/*tryOpfsVfs()*/; + +importScripts('jswasm/sqlite3.js'); +self.sqlite3InitModule() + .then((sqlite3)=>tryOpfsVfs(sqlite3)) + .catch((e)=>{ + console.error("Error initializing module:",e); + }); diff --git a/ext/wasm/tester1-worker.c-pp.html b/ext/wasm/tester1-worker.c-pp.html new file mode 100644 index 0000000000..e461b6cbff --- /dev/null +++ b/ext/wasm/tester1-worker.c-pp.html @@ -0,0 +1,88 @@ + +//#@policy error + + + + + + + + sqlite3 tester #1: Worker thread (@bitness@-bit WASM) + + + +

    sqlite3 tester #1: Worker thread (@bitness@-bit WASM)

    + +
    + + +
    +
    + + + diff --git a/ext/wasm/tester1.c-pp.html b/ext/wasm/tester1.c-pp.html new file mode 100644 index 0000000000..95fe52219e --- /dev/null +++ b/ext/wasm/tester1.c-pp.html @@ -0,0 +1,36 @@ + +//#@policy error + + + + + + + + sqlite3 tester #1: @title@ (@bitness@-bit WASM) + + +

    + +
    + + +
    +
    + +//#if target:es6-module + +//#else + + +//#endif + + diff --git a/ext/wasm/tester1.c-pp.js b/ext/wasm/tester1.c-pp.js new file mode 100644 index 0000000000..f72e0803fc --- /dev/null +++ b/ext/wasm/tester1.c-pp.js @@ -0,0 +1,4027 @@ +/* + 2022-10-12 + + The author disclaims copyright to this source code. In place of a + legal notice, here is a blessing: + + * May you do good and not evil. + * May you find forgiveness for yourself and forgive others. + * May you share freely, never taking more than you give. + + *********************************************************************** + + Main functional and regression tests for the sqlite3 WASM API. + + This mini-framework works like so: + + This script adds a series of test groups, each of which contains an + arbitrary number of tests, into a queue. After loading of the + sqlite3 WASM/JS module is complete, that queue is processed. If any + given test fails, the whole thing fails. This script is built such + that it can run from the main UI thread or worker thread. Test + groups and individual tests can be assigned a predicate function + which determines whether to run them or not, and this is + specifically intended to be used to toggle certain tests on or off + for the main/worker threads or the availability (or not) of + optional features such as int64 support. + + Each test group defines a single state object which gets applied as + the test functions' `this` for all tests in that group. Test + functions can use that to, e.g., set up a db in an early test and + close it in a later test. Each test gets passed the sqlite3 + namespace object as its only argument. +*/ +/* + This file is intended to be processed by c-pp to inject (or not) + code specific to ES6 modules which is illegal in non-module code. + + Non-ES6 module build and ES6 module for the main-thread: + + ./c-pp -f tester1.c-pp.js -o tester1.js + + ES6 worker module build: + + ./c-pp -f tester1.c-pp.js -o tester1-esm.mjs -Dtarget:es6-module +*/ +//#@policy error +//#if target:es6-module +import {default as sqlite3InitModule} from "@sqlite3.js@"; +globalThis.sqlite3InitModule = sqlite3InitModule; +//#else +'use strict'; +//#endif +(function(self){ + /** + Set up our output channel differently depending + on whether we are running in a worker thread or + the main (UI) thread. + */ + let logClass; + /* Predicate for tests/groups. */ + const isUIThread = ()=>(globalThis.window===self && globalThis.document); + /* Predicate for tests/groups. */ + const isWorker = ()=>!isUIThread(); + /* Predicate for tests/groups. */ + const testIsTodo = ()=>false; + const haveWasmCTests = ()=>{ + return !!wasm.exports.sqlite3__wasm_test_intptr; + }; + const hasOpfs = ()=>{ + return globalThis.FileSystemHandle + && globalThis.FileSystemDirectoryHandle + && globalThis.FileSystemFileHandle + && globalThis.FileSystemFileHandle.prototype.createSyncAccessHandle + && navigator?.storage?.getDirectory; + }; + + let SQLite3 /* populated after module load */; + + { + const mapToString = (v)=>{ + switch(typeof v){ + case 'number': case 'string': case 'boolean': + case 'undefined': case 'bigint': + return ''+v; + default: break; + } + if(null===v) return 'null'; + if(v instanceof Error){ + v = { + message: v.message, + stack: v.stack, + errorClass: v.name + }; + } + return JSON.stringify(v,undefined,2); + }; + const normalizeArgs = (args)=>args.map(mapToString); + if( isUIThread() ){ + console.log("Running in the UI thread."); + const logTarget = document.querySelector('#test-output'); + logClass = function(cssClass,...args){ + const ln = document.createElement('div'); + if(cssClass){ + for(const c of (Array.isArray(cssClass) ? cssClass : [cssClass])){ + ln.classList.add(c); + } + } + ln.append(document.createTextNode(normalizeArgs(args).join(' '))); + logTarget.append(ln); + }; + const cbReverse = document.querySelector('#cb-log-reverse'); + const cbReverseKey = 'tester1:cb-log-reverse'; + const cbReverseIt = ()=>{ + logTarget.classList[cbReverse.checked ? 'add' : 'remove']('reverse'); + localStorage.setItem(cbReverseKey, cbReverse.checked ? 1 : 0); + }; + cbReverse.addEventListener('change', cbReverseIt, true); + if(localStorage.getItem(cbReverseKey)){ + cbReverse.checked = !!(+localStorage.getItem(cbReverseKey)); + } + cbReverseIt(); + }else{ /* Worker thread */ + console.log("Running in a Worker thread."); + logClass = function(cssClass,...args){ + postMessage({ + type:'log', + payload:{cssClass, args: normalizeArgs(args)} + }); + }; + } + } + const reportFinalTestStatus = function(pass){ + if(isUIThread()){ + let e = document.querySelector('#color-target'); + e.classList.add(pass ? 'tests-pass' : 'tests-fail'); + e = document.querySelector('title'); + e.innerText = (pass ? 'PASS' : 'FAIL') + ': ' + e.innerText; + }else{ + postMessage({type:'test-result', payload:{pass}}); + } + TestUtil.checkHeapSize(true); + }; + const log = (...args)=>{ + //console.log(...args); + logClass('',...args); + } + const warn = (...args)=>{ + console.warn(...args); + logClass('warning',...args); + } + const error = (...args)=>{ + console.error(...args); + logClass('error',...args); + }; + + const toss = (...args)=>{ + error(...args); + throw new Error(args.join(' ')); + }; + const tossQuietly = (...args)=>{ + throw new Error(args.join(' ')); + }; + + const roundMs = (ms)=>Math.round(ms*100)/100; + + const looksLikePtr = (v,positive=true)=> positive ? v>0 : v>=0; + + /** + Helpers for writing sqlite3-specific tests. + */ + const TestUtil = { + /** Running total of the number of tests run via + this API. */ + counter: 0, + /** + If expr is a function, it is called and its result + is returned, coerced to a bool, else expr, coerced to + a bool, is returned. + */ + toBool: function(expr){ + return (expr instanceof Function) ? !!expr() : !!expr; + }, + /** Throws if expr is false. If expr is a function, it is called + and its result is evaluated. If passed multiple arguments, + those after the first are a message string which get applied + as an exception message if the assertion fails. The message + arguments are concatenated together with a space between each. + */ + assert: function f(expr, ...msg){ + ++this.counter; + if(!this.toBool(expr)){ + throw new Error(msg.length ? msg.join(' ') : "Assertion failed."); + } + return this; + }, + /** Calls f() and squelches any exception it throws. If it + does not throw, this function throws. */ + mustThrow: function(f, msg){ + ++this.counter; + let err; + try{ f(); } catch(e){err=e;} + if(!err) throw new Error(msg || "Expected exception."); + return this; + }, + /** + Works like mustThrow() but expects filter to be a regex, + function, or string to match/filter the resulting exception + against. If f() does not throw, this test fails and an Error is + thrown. If filter is a regex, the test passes if + filter.test(error.message) passes. If it's a function, the test + passes if filter(error) returns truthy. If it's a string, the + test passes if the filter matches the exception message + precisely. In all other cases the test fails, throwing an + Error. + + If it throws, msg is used as the error report unless it's falsy, + in which case a default is used. + */ + mustThrowMatching: function(f, filter, msg){ + ++this.counter; + let err; + try{ f(); } catch(e){err=e;} + if(!err) throw new Error(msg || "Expected exception."); + let pass = false; + if(filter instanceof RegExp) pass = filter.test(err.message); + else if(filter instanceof Function) pass = filter(err); + else if('string' === typeof filter) pass = (err.message === filter); + if(!pass){ + throw new Error(msg || ("Filter rejected this exception: <<"+err.message+">>")); + } + return this; + }, + /** Throws if expr is truthy or expr is a function and expr() + returns truthy. */ + throwIf: function(expr, msg){ + ++this.counter; + if(this.toBool(expr)) throw new Error(msg || "throwIf() failed"); + return this; + }, + /** Throws if expr is falsy or expr is a function and expr() + returns falsy. */ + throwUnless: function(expr, msg){ + ++this.counter; + if(!this.toBool(expr)) throw new Error(msg || "throwUnless() failed"); + return this; + }, + eqApprox: (v1,v2,factor=0.05)=>(v1>=(v2-factor) && v1<=(v2+factor)), + TestGroup: (function(){ + let groupCounter = 0; + const TestGroup = function(name, predicate){ + this.number = ++groupCounter; + this.name = name; + this.predicate = predicate; + this.tests = []; + }; + TestGroup.prototype = { + addTest: function(testObj){ + this.tests.push(testObj); + return this; + }, + run: async function(sqlite3){ + logClass('group-start',"Group #"+this.number+':',this.name); + if(this.predicate){ + const p = this.predicate(sqlite3); + if(!p || 'string'===typeof p){ + logClass(['warning','skipping-group'], + "SKIPPING group:", p ? p : "predicate says to" ); + return; + } + } + const assertCount = TestUtil.counter; + const groupState = Object.create(null); + const skipped = []; + let runtime = 0, i = 0; + for(const t of this.tests){ + ++i; + const n = this.number+"."+i; + logClass('one-test-line', n+":", t.name); + if(t.predicate){ + const p = t.predicate(sqlite3); + if(!p || 'string'===typeof p){ + logClass(['warning','skipping-test'], + "SKIPPING:", p ? p : "predicate says to" ); + skipped.push( n+': '+t.name ); + continue; + } + } + const tc = TestUtil.counter, now = performance.now(); + let rc = t.test.call(groupState, sqlite3); + /*if(rc instanceof Promise){ + rc = rc.catch((e)=>{ + error("Test failure:",e); + throw e; + }); + }*/ + await rc; + const then = performance.now(); + runtime += then - now; + logClass(['faded','one-test-summary'], + TestUtil.counter - tc, 'assertion(s) in', + roundMs(then-now),'ms'); + TestUtil.checkHeapSize(); + } + logClass(['green','group-end'], + "#"+this.number+":", + (TestUtil.counter - assertCount), + "assertion(s) in",roundMs(runtime),"ms"); + if(0 && skipped.length){ + logClass('warning',"SKIPPED test(s) in group",this.number+":",skipped); + } + } + }; + return TestGroup; + })()/*TestGroup*/, + testGroups: [], + currentTestGroup: undefined, + addGroup: function(name, predicate){ + this.testGroups.push( this.currentTestGroup = + new this.TestGroup(name, predicate) ); + return this; + }, + addTest: function(name, callback){ + let predicate; + if(1===arguments.length){ + this.currentTestGroup.addTest(arguments[0]); + }else{ + this.currentTestGroup.addTest({ + name, predicate, test: callback + }); + } + return this; + }, + runTests: async function(sqlite3){ + return new Promise(async function(pok,pnok){ + try { + let runtime = 0; + for(let g of this.testGroups){ + const now = performance.now(); + await g.run(sqlite3); + runtime += performance.now() - now; + } + logClass(['strong','green','full-test-summary'], + "Done running tests.",TestUtil.counter,"assertions in", + roundMs(runtime),'ms'); + pok(); + reportFinalTestStatus(true); + }catch(e){ + error(e); + pnok(e); + reportFinalTestStatus(false); + } + }.bind(this)); + }, + + checkHeapSize: function(force=false){ + const heapSize = SQLite3.wasm.heap8().byteLength; + if( force || heapSize !== TestUtil.lastHeapSize ){ + TestUtil.lastHeapSize = heapSize; + log('WASM heap size:', heapSize,'bytes'); + } + } + }/*TestUtil*/; + const T = TestUtil; + T.g = T.addGroup; + T.t = T.addTest; + let capi, wasm/*assigned after module init*/; + const sahPoolConfig = { + name: 'opfs-sahpool-tester1', + clearOnInit: true, + initialCapacity: 6 + }; + //////////////////////////////////////////////////////////////////////// + // End of infrastructure setup. Now define the tests... + //////////////////////////////////////////////////////////////////////// + + //////////////////////////////////////////////////////////////////// + T.g('Basic sanity checks') + .t({ + name:'sqlite3_config()', + test:function(sqlite3){ + for(const k of [ + 'SQLITE_CONFIG_GETMALLOC', 'SQLITE_CONFIG_URI' + ]){ + T.assert(capi[k] > 0); + } + T.assert(capi.SQLITE_MISUSE===capi.sqlite3_config( + capi.SQLITE_CONFIG_URI, 1 + ), "MISUSE because the library has already been initialized."); + T.assert(capi.SQLITE_MISUSE === capi.sqlite3_config( + // not enough args + capi.SQLITE_CONFIG_GETMALLOC + )); + T.assert(capi.SQLITE_NOTFOUND === capi.sqlite3_config( + // unhandled-in-JS config option + capi.SQLITE_CONFIG_GETMALLOC, 1 + )); + if(0){ + log("We cannot _fully_ test sqlite3_config() after the library", + "has been initialized (which it necessarily has been to", + "set up various bindings) and we cannot shut it down ", + "without losing the VFS registrations."); + T.assert(0 === capi.sqlite3_config( + capi.SQLITE_CONFIG_URI, 1 + )); + } + } + })/*sqlite3_config()*/ + + //////////////////////////////////////////////////////////////////// + .t({ + name: "JS wasm-side allocator", + test: function(sqlite3){ + if(sqlite3.config.useStdAlloc){ + warn("Using system allocator. This violates the docs and", + "may cause grief with certain APIs", + "(e.g. sqlite3_deserialize())."); + T.assert(wasm.alloc.impl === wasm.exports.malloc) + .assert(wasm.dealloc === wasm.exports.free) + .assert(wasm.realloc.impl === wasm.exports.realloc); + }else{ + T.assert(wasm.alloc.impl === wasm.exports.sqlite3_malloc) + .assert(wasm.dealloc.impl === wasm.exports.sqlite3_free) + .assert(wasm.realloc.impl === wasm.exports.sqlite3_realloc); + } + } + }) + .t('Namespace object checks', function(sqlite3){ + const wasmCtypes = wasm.ctype; + T.assert(wasmCtypes.structs[0].name==='sqlite3_vfs'). + assert(wasmCtypes.structs[0].members.szOsFile.sizeof>=4). + assert(wasmCtypes.structs[1/*sqlite3_io_methods*/ + ].members.xFileSize.offset>0); + [ /* Spot-check a handful of constants to make sure they got installed... */ + 'SQLITE_SCHEMA','SQLITE_NULL','SQLITE_UTF8', + 'SQLITE_STATIC', 'SQLITE_DIRECTONLY', + 'SQLITE_OPEN_CREATE', 'SQLITE_OPEN_DELETEONCLOSE' + ].forEach((k)=>T.assert('number' === typeof capi[k])); + [/* Spot-check a few of the WASM API methods. */ + 'alloc', 'dealloc', 'installFunction' + ].forEach((k)=>T.assert(wasm[k] instanceof Function)); + + T.assert(capi.sqlite3_errstr(capi.SQLITE_IOERR_ACCESS).indexOf("I/O")>=0). + assert(capi.sqlite3_errstr(capi.SQLITE_CORRUPT).indexOf('malformed')>0). + assert(capi.sqlite3_errstr(capi.SQLITE_OK) === 'not an error'); + + try { + throw new sqlite3.WasmAllocError; + }catch(e){ + T.assert(e instanceof Error) + .assert(e instanceof sqlite3.WasmAllocError) + .assert("Allocation failed." === e.message); + } + try { + throw new sqlite3.WasmAllocError("test",{ + cause: 3 + }); + }catch(e){ + T.assert(3 === e.cause) + .assert("test" === e.message); + } + try {throw new sqlite3.WasmAllocError("test","ing",".")} + catch(e){T.assert("test ing ." === e.message)} + + try{ throw new sqlite3.SQLite3Error(capi.SQLITE_SCHEMA) } + catch(e){ + T.assert('SQLITE_SCHEMA' === e.message) + .assert(capi.SQLITE_SCHEMA === e.resultCode); + } + try{ sqlite3.SQLite3Error.toss(capi.SQLITE_CORRUPT,{cause: true}) } + catch(e){ + T.assert('SQLITE_CORRUPT' === e.message) + .assert(capi.SQLITE_CORRUPT === e.resultCode) + .assert(true===e.cause); + } + try{ sqlite3.SQLite3Error.toss("resultCode check") } + catch(e){ + T.assert(capi.SQLITE_ERROR === e.resultCode) + .assert('resultCode check' === e.message); + } + }) + //////////////////////////////////////////////////////////////////// + .t('strglob/strlike', function(sqlite3){ + T.assert(0===capi.sqlite3_strglob("*.txt", "foo.txt")). + assert(0!==capi.sqlite3_strglob("*.txt", "foo.xtx")). + assert(0===capi.sqlite3_strlike("%.txt", "foo.txt", 0)). + assert(0!==capi.sqlite3_strlike("%.txt", "foo.xtx", 0)); + }) + + //////////////////////////////////////////////////////////////////// + ;/*end of basic sanity checks*/ + + //////////////////////////////////////////////////////////////////// + T.g('C/WASM Utilities') + .t('sqlite3.wasm namespace', function(sqlite3){ + // TODO: break this into smaller individual test functions. + const w = wasm; + const chr = (x)=>x.charCodeAt(0); + //log("heap getters..."); + { + const li = [8, 16, 32]; + if(w.bigIntEnabled) li.push(64); + for(const n of li){ + const bpe = n/8; + const s = w.heapForSize(n,false); + T.assert(bpe===s.BYTES_PER_ELEMENT). + assert(w.heapForSize(s.constructor) === s); + const u = w.heapForSize(n,true); + T.assert(bpe===u.BYTES_PER_ELEMENT). + assert(s!==u). + assert(w.heapForSize(u.constructor) === u); + } + } + + // alloc(), realloc(), allocFromTypedArray() + { + let m = w.alloc(14); + let m2 = w.realloc(m, 16); + T.assert(m === m2/* because of alignment */); + let x = w.realloc(m, 0); + T.assert(w.ptr.null === x); + m = m2 = 0; + + // Check allocation limits and allocator's responses... + T.assert('number' === typeof sqlite3.capi.SQLITE_MAX_ALLOCATION_SIZE); + if(!sqlite3.config.useStdAlloc){ + const tooMuch = sqlite3.capi.SQLITE_MAX_ALLOCATION_SIZE + 1, + isAllocErr = (e)=>e instanceof sqlite3.WasmAllocError; + T.mustThrowMatching(()=>w.alloc(tooMuch), isAllocErr) + .assert(w.ptr.null === w.alloc.impl(tooMuch)) + .mustThrowMatching(()=>w.realloc(0, tooMuch), isAllocErr) + .assert(w.ptr.null === w.realloc.impl(wasm.ptr.null, tooMuch)); + } + + // Check allocFromTypedArray()... + const byteList = [11,22,33] + const u = new Uint8Array(byteList); + m = w.allocFromTypedArray(u); + let mAsNumber = Number(m); + for(let i = 0; i < u.length; ++i){ + T.assert(u[i] === byteList[i]) + .assert(u[i] === w.peek8(mAsNumber + i)); + } + w.dealloc(m); + m = w.allocFromTypedArray(u.buffer); + mAsNumber = Number(m); + for(let i = 0; i < u.length; ++i){ + T.assert(u[i] === byteList[i]) + .assert(u[i] === w.peek8(mAsNumber + i)); + } + + w.dealloc(m); + T.mustThrowMatching( + ()=>w.allocFromTypedArray(1), + 'Value is not of a supported TypedArray type.' + ); + } + + { // Test peekXYZ()/pokeXYZ()... + const m = w.alloc(8); + T.assert( 17 === w.poke8(m,17).peek8(m) ) + .assert( 31987 === w.poke16(m,31987).peek16(m) ) + .assert( 345678 === w.poke32(m,345678).peek32(m) ) + .assert( + T.eqApprox( 345678.9, w.poke32f(m,345678.9).peek32f(m) ) + ).assert( + T.eqApprox( 4567890123.4, w.poke64f(m, 4567890123.4).peek64f(m) ) + ); + if(w.bigIntEnabled){ + T.assert( + BigInt(Number.MAX_SAFE_INTEGER) === + w.poke64(m, Number.MAX_SAFE_INTEGER).peek64(m) + ); + } + w.dealloc(m); + } + + // isPtr32() + { + const ip = w.isPtr32; + T.assert(ip(0)) + .assert(!ip(-1)) + .assert(!ip(1.1)) + .assert(!ip(0xffffffff)) + .assert(ip(0x7fffffff)) + .assert(!ip()) + .assert(!ip(null)/*might change: under consideration*/) + ; + } + + //log("jstrlen()..."); + { + T.assert(3 === w.jstrlen("abc")).assert(4 === w.jstrlen("äbc")); + } + + //log("jstrcpy()..."); + { + const fillChar = 10; + let ua = new Uint8Array(8), rc, + refill = ()=>ua.fill(fillChar); + refill(); + rc = w.jstrcpy("hello", ua); + T.assert(6===rc).assert(0===ua[5]).assert(chr('o')===ua[4]); + refill(); + ua[5] = chr('!'); + rc = w.jstrcpy("HELLO", ua, 0, -1, false); + T.assert(5===rc).assert(chr('!')===ua[5]).assert(chr('O')===ua[4]); + refill(); + rc = w.jstrcpy("the end", ua, 4); + //log("rc,ua",rc,ua); + T.assert(4===rc).assert(0===ua[7]). + assert(chr('e')===ua[6]).assert(chr('t')===ua[4]); + refill(); + rc = w.jstrcpy("the end", ua, 4, -1, false); + T.assert(4===rc).assert(chr(' ')===ua[7]). + assert(chr('e')===ua[6]).assert(chr('t')===ua[4]); + refill(); + rc = w.jstrcpy("", ua, 0, 1, true); + //log("rc,ua",rc,ua); + T.assert(1===rc).assert(0===ua[0]); + refill(); + rc = w.jstrcpy("x", ua, 0, 1, true); + //log("rc,ua",rc,ua); + T.assert(1===rc).assert(0===ua[0]); + refill(); + rc = w.jstrcpy('äbä', ua, 0, 1, true); + T.assert(1===rc, 'Must not write partial multi-byte char.') + .assert(0===ua[0]); + refill(); + rc = w.jstrcpy('äbä', ua, 0, 2, true); + T.assert(1===rc, 'Must not write partial multi-byte char.') + .assert(0===ua[0]); + refill(); + rc = w.jstrcpy('äbä', ua, 0, 2, false); + T.assert(2===rc).assert(fillChar!==ua[1]).assert(fillChar===ua[2]); + }/*jstrcpy()*/ + + //log("cstrncpy()..."); + { + const scope = w.scopedAllocPush(); + try { + let cStr = w.scopedAllocCString("hello"); + const n = w.cstrlen(cStr); + const nPtr = w.ptr.coerce(n); + let cpy = w.scopedAlloc(n+10); + let rc = w.cstrncpy(cpy, cStr, n+10); + T.assert(n+1 === rc). + assert("hello" === w.cstrToJs(cpy)). + assert(chr('o') === w.peek8( w.ptr.add(cpy,nPtr, -1))). + assert(0 === w.peek8( w.ptr.add(cpy,nPtr) ) ); + let cStr2 = w.scopedAllocCString("HI!!!"); + rc = w.cstrncpy(cpy, cStr2, 3); + T.assert(3===rc). + assert("HI!lo" === w.cstrToJs(cpy)). + assert(chr('!') === w.peek8( w.ptr.add(cpy, 2) )). + assert(chr('l') === w.peek8( w.ptr.add(cpy, 3) ) ); + }finally{ + w.scopedAllocPop(scope); + } + } + + //log("jstrToUintArray()..."); + { + let a = w.jstrToUintArray("hello", false); + T.assert(5===a.byteLength).assert(chr('o')===a[4]); + a = w.jstrToUintArray("hello", true); + T.assert(6===a.byteLength).assert(chr('o')===a[4]).assert(0===a[5]); + a = w.jstrToUintArray("äbä", false); + T.assert(5===a.byteLength).assert(chr('b')===a[2]); + a = w.jstrToUintArray("äbä", true); + T.assert(6===a.byteLength).assert(chr('b')===a[2]).assert(0===a[5]); + } + + //log("allocCString()..."); + { + const jstr = "hällo, world!"; + const [cstr, n] = w.allocCString(jstr, true); + T.assert(14 === n) + .assert(0===w.peek8(w.ptr.add(cstr,n))) + .assert(chr('!')===w.peek8(w.ptr.add(cstr,n,-1))); + w.dealloc(cstr); + } + + //log("scopedAlloc() and friends..."); + { + const alloc = w.alloc, dealloc = w.dealloc; + w.alloc = w.dealloc = null; + T.assert(!w.scopedAlloc.level) + .mustThrowMatching(()=>w.scopedAlloc(1), /^No scopedAllocPush/) + .mustThrowMatching(()=>w.scopedAllocPush(), /missing alloc/); + w.alloc = alloc; + T.mustThrowMatching(()=>w.scopedAllocPush(), /missing alloc/); + w.dealloc = dealloc; + T.mustThrowMatching(()=>w.scopedAllocPop(), /^Invalid state/) + .mustThrowMatching(()=>w.scopedAlloc(1), /^No scopedAllocPush/) + .mustThrowMatching(()=>w.scopedAlloc.level=0, /read-only/); + const asc = w.scopedAllocPush(); + let asc2; + try { + const p1 = w.scopedAlloc(16), + p2 = w.scopedAlloc(16); + T.assert(1===w.scopedAlloc.level) + .assert(looksLikePtr(p1)) + .assert(looksLikePtr(p2)) + .assert(asc[0] === p1) + .assert(asc[1]===p2); + asc2 = w.scopedAllocPush(); + const p3 = w.scopedAlloc(16); + T.assert(2===w.scopedAlloc.level) + .assert(looksLikePtr(p3)) + .assert(2===asc.length) + .assert(p3===asc2[0]); + + const [z1, z2, z3] = w.scopedAllocPtr(3); + T.assert(typeof w.ptr.null===typeof z1).assert(z2>z1).assert(z3>z2) + .assert(w.ptr.null===w.peekPtr(z1), 'allocPtr() must zero the targets') + .assert(w.ptr.null===w.peekPtr(z3)); + }finally{ + // Pop them in "incorrect" order to make sure they behave: + w.scopedAllocPop(asc); + T.assert(0===asc.length); + T.mustThrowMatching(()=>w.scopedAllocPop(asc), + /^Invalid state object/); + if(asc2){ + T.assert(2===asc2.length,'Should be p3 and z1'); + w.scopedAllocPop(asc2); + T.assert(0===asc2.length); + T.mustThrowMatching(()=>w.scopedAllocPop(asc2), + /^Invalid state object/); + } + } + T.assert(0===w.scopedAlloc.level); + w.scopedAllocCall(function(){ + T.assert(1===w.scopedAlloc.level); + const [cstr, n] = w.scopedAllocCString("hello, world", true); + T.assert(12 === n) + .assert(0===w.peek8( w.ptr.add(cstr,n) )) + .assert(chr('d')===w.peek8( w.ptr.add(cstr, n, -1) )); + }); + }/*scopedAlloc()*/ + + //log("xCall()..."); + { + const pJson = w.xCall('sqlite3__wasm_enum_json'); + T.assert(looksLikePtr(pJson)).assert(w.cstrlen(pJson)>300); + } + + //log("xWrap()..."); + { + T.mustThrowMatching(()=>w.xWrap('sqlite3_libversion',null,'i32'), + /requires 0 arg/). + assert(w.xWrap.resultAdapter('i32') instanceof Function). + assert(w.xWrap.argAdapter('i32') instanceof Function); + let fw = w.xWrap('sqlite3_libversion','utf8'); + T.mustThrowMatching(()=>fw(1), /requires 0 arg/); + let rc = fw(); + T.assert('string'===typeof rc).assert(rc.length>5); + rc = w.xCallWrapped('sqlite3__wasm_enum_json','*'); + T.assert(rc>0 && looksLikePtr(rc)); + rc = w.xCallWrapped('sqlite3__wasm_enum_json','utf8'); + T.assert('string'===typeof rc).assert(rc.length>300); + + + { // 'string:static' argAdapter() sanity checks... + let argAd = w.xWrap.argAdapter('string:static'); + let p0 = argAd('foo'), p1 = argAd('bar'); + T.assert(w.isPtr(p0) && w.isPtr(p1)) + .assert(p0 !== p1) + .assert(p0 === argAd('foo')) + .assert(p1 === argAd('bar')); + } + + // 'string:flexible' argAdapter() sanity checks... + w.scopedAllocCall(()=>{ + const toFlexStr = w.xWrap.argAdapter('string:flexible'); + const cj = (v)=>w.cstrToJs(toFlexStr(v)); + //console.debug("toFlexStr(new Uint8Array([72, 73]))",toFlexStr(new Uint8Array([72, 73]))); + T.assert('Hi' === cj('Hi')) + .assert('hi' === cj(['h','i'])) + .assert('HI' === cj(new Uint8Array([72, 73]))); + }); + + // jsFuncToWasm() + { + const fsum3 = (x,y,z)=>x+y+z; + fw = w.jsFuncToWasm('i(iii)', fsum3); + T.assert(fw instanceof Function) + .assert( fsum3 !== fw ) + .assert( 3 === fw.length ) + .assert( 6 === fw(1,2,3) ); + T.mustThrowMatching( ()=>w.jsFuncToWasm('x()', function(){}), + 'Invalid signature letter: x'); + } + + // xWrap(Function,...) + { + let fp; + try { + const fmy = function fmy(i,s,d){ + if(fmy.debug) log("fmy(",...arguments,")"); + T.assert( 3 === i ) + .assert( w.isPtr(s) ) + .assert( w.cstrToJs(s) === 'a string' ) + .assert( T.eqApprox(1.2, d) ); + return w.allocCString("hi"); + }; + fmy.debug = false; + const xwArgs = ['string:dealloc', ['i32', 'string', 'f64']]; + fw = w.xWrap(fmy, ...xwArgs); + const fmyArgs = [3, 'a string', 1.2]; + let rc = fw(...fmyArgs); + T.assert( 'hi' === rc ); + if(0){ + /* Retain this as a "reminder to self"... + + This extra level of indirection does not work: the + string argument is ending up as a null in fmy() but + the numeric arguments are making their ways through + + What's happening is: installFunction() is creating a + WASM-compatible function instance. When we pass a JS string + into there it's getting coerced into `null` before being passed + on to the lower-level wrapper. + */ + fmy.debug = true; + fp = wasm.installFunction('i(isd)', fw); + fw = w.functionEntry(fp); + rc = fw(...fmyArgs); + log("rc =",rc); + T.assert( 'hi' === rc ); + // Similarly, this does not work: + //let fpw = w.xWrap(fp, null, [null,null,null]); + //rc = fpw(...fmyArgs); + //log("rc =",rc); + //T.assert( 'hi' === rc ); + } + }finally{ + wasm.uninstallFunction(fp); + } + } + + if(haveWasmCTests()){ + if(!sqlite3.config.useStdAlloc){ + fw = w.xWrap('sqlite3__wasm_test_str_hello', 'utf8:dealloc',['i32']); + rc = fw(0); + T.assert('hello'===rc); + rc = fw(1); + T.assert(null===rc); + } + + if(w.bigIntEnabled){ + w.xWrap.resultAdapter('thrice', (v)=>3n*BigInt(v)); + w.xWrap.argAdapter('twice', (v)=>2n*BigInt(v)); + fw = w.xWrap('sqlite3__wasm_test_int64_times2','thrice','twice'); + rc = fw(1); + T.assert(12n===rc); + + w.scopedAllocCall(function(){ + const pI1 = w.scopedAlloc(w.ptr.size), pI2 = w.ptr.add(pI1, w.ptr.size); + w.pokePtr([pI1, pI2], w.ptr.null); + const f = w.xWrap('sqlite3__wasm_test_int64_minmax',undefined,['i64*','i64*']); + const [r1, r2] = w.peek64([pI1, pI2]); + T.assert(!Number.isSafeInteger(r1)).assert(!Number.isSafeInteger(r2)); + }); + } + } + }/*xWrap()*/ + }/*WhWasmUtil*/) + + //////////////////////////////////////////////////////////////////// + .t({ + name: 'sqlite3.StructBinder (jaccwabyt🐇)', + predicate: (sqlite3)=>!!sqlite3.wasm.exports.sqlite3__wasm_test_struct + || "Built without SQLITE_WASM_ENABLE_C_TESTS", + test: function(sqlite3){ + const S = sqlite3, W = S.wasm; + const MyStructDef = { + sizeof: 0, members: {} + }; + const addMember = function(tgt, name, member){ + member.offset = tgt.sizeof; + tgt.sizeof += member.sizeof; + tgt.members[name] = member; + }; + const msd = MyStructDef; + addMember(msd, 'p4', {sizeof: 4, signature: "i"}); + addMember(msd, 'pP', {sizeof: wasm.ptr.size, signature: "P"}); + addMember(msd, 'ro', { + sizeof: 4, + signature: "i", + readOnly: true + }); + addMember(msd, 'cstr', { + sizeof: wasm.ptr.size, + signature: "s" + }); + if(W.bigIntEnabled){ + addMember(msd, 'p8', {sizeof: 8, signature: "j"}); + } + const StructType = S.StructBinder.StructType; + const K = S.StructBinder('my_struct',MyStructDef); + //K.debugFlags(0x03); + T.mustThrowMatching(()=>K(), /via 'new'/). + mustThrowMatching(()=>new K('hi'), (err)=>{ + return /^Invalid pointer/.test(err.message)/*32-bit*/ + || /.*bigint.*/i.test(err.message)/*64-bit*/; + }); + const k1 = new K(), k2 = new K(); + try { + T.assert(k1.constructor === K). + assert(K.isA(k1)). + assert(k1 instanceof K). + assert(K.prototype.lookupMember('p4').key === '$p4'). + assert(K.prototype.lookupMember('$p4').name === 'p4'). + mustThrowMatching(()=>K.prototype.lookupMember('nope'), /not a mapped/). + assert(undefined === K.prototype.lookupMember('nope',false)). + assert(k1 instanceof StructType). + assert(StructType.isA(k1)). + mustThrowMatching(()=>k1.$ro = 1, /read-only/); + Object.keys(MyStructDef.members).forEach(function(key){ + key = K.memberKey(key); + T.assert(0 == k1[key], + "Expecting allocation to zero the memory "+ + "for "+key+" but got: "+k1[key]+ + " from "+k1.memoryDump()); + }); + T.assert(looksLikePtr(k1.pointer)). + mustThrowMatching(()=>k1.pointer = 1, /pointer/); + k1.$p4 = 1; k1.$pP = 2; + T.assert(1 == k1.$p4).assert(2 == k1.$pP); + if(MyStructDef.members.$p8){ + k1.$p8 = 1/*must not throw despite not being a BigInt*/; + k1.$p8 = BigInt(Number.MAX_SAFE_INTEGER * 2); + T.assert(BigInt(2 * Number.MAX_SAFE_INTEGER) === k1.$p8); + } + T.assert(!k1.ondispose); + k1.setMemberCString('cstr', "A C-string."); + T.assert(Array.isArray(k1.ondispose)). + assert(k1.ondispose[0] === k1.$cstr). + assert(looksLikePtr(k1.$cstr)). + assert('A C-string.' === k1.memberToJsString('cstr')); + k1.$pP = k2; + T.assert(k1.$pP === k2.pointer); + k1.$pP = null/*null is special-cased to 0.*/; + T.assert(0==k1.$pP); + let ptr = k1.pointer; + k1.dispose(); + T.assert(undefined === k1.pointer). + mustThrowMatching(()=>{k1.$pP=1}, /disposed instance/); + }finally{ + k1.dispose(); + k2.dispose(); + } + + if(!W.bigIntEnabled){ + log("Skipping WasmTestStruct tests: BigInt not enabled."); + return; + } + + const WTStructDesc = + W.ctype.structs.filter((e)=>'WasmTestStruct'===e.name)[0]; + const autoResolvePtr = true /* EXPERIMENTAL */; + if(autoResolvePtr){ + WTStructDesc.members.ppV.signature = 'P'; + } + const WTStruct = S.StructBinder(WTStructDesc); + //log(WTStruct.structName, WTStruct.structInfo); + const wts = new WTStruct(); + //log("WTStruct.prototype keys:",Object.keys(WTStruct.prototype)); + try{ + T.assert(wts.constructor === WTStruct). + assert(WTStruct.memberKeys().indexOf('$ppV')>=0). + assert(wts.memberKeys().indexOf('$v8')>=0). + assert(!K.isA(wts)). + assert(WTStruct.isA(wts)). + assert(wts instanceof WTStruct). + assert(wts instanceof StructType). + assert(StructType.isA(wts)). + assert(looksLikePtr(wts.pointer)).assert(0==wts.$v4).assert(0n===wts.$v8). + assert(0==wts.$ppV).assert(0==wts.$xFunc); + const testFunc = 1 + ? W.xGet('sqlite3__wasm_test_struct'/*name gets mangled in -O3 builds!*/) + : W.xWrap('sqlite3__wasm_test_struct', undefined, '*'); + let counter = 0; + //log("wts.pointer =",wts.pointer); + const wtsFunc = function(arg){ + /*log("This from a JS function called from C, "+ + "which itself was called from JS. arg =",arg);*/ + ++counter; + if(3===counter){ + tossQuietly("Testing exception propagation."); + } + } + wts.$v4 = 10; wts.$v8 = 20; + wts.$xFunc = W.installFunction(wtsFunc, wts.memberSignature('xFunc')) + //console.debug("wts.memberSignature('xFunc')",wts.memberSignature('xFunc')); + //console.debug("wts.$xFunc",wts.$xFunc, W.functionEntry(wts.$xFunc)); + T.assert(0===counter).assert(10 === wts.$v4).assert(20n === wts.$v8) + .assert(0 == wts.$ppV).assert(looksLikePtr(wts.$xFunc)) + .assert(0 == wts.$cstr) + .assert(wts.memberIsString('$cstr')) + .assert(!wts.memberIsString('$v4')) + .assert(null === wts.memberToJsString('$cstr')) + .assert(W.functionEntry(wts.$xFunc) instanceof Function); + /* It might seem silly to assert that the values match + what we just set, but recall that all of those property + reads and writes are, via property interceptors, + actually marshaling their data to/from a raw memory + buffer, so merely reading them back is actually part of + testing the struct-wrapping API. */ + + if( 0 ){ + console.debug("wts",wts,"wts.pointer",wts.pointer, + "testFunc",testFunc/*FF v142 emits the wrong function here!*/); + } + testFunc(wts.pointer); + //log("wts.pointer, wts.$ppV",wts.pointer, wts.$ppV); + T.assert(1===counter).assert(20 === wts.$v4).assert(40n === wts.$v8) + .assert(wts.$ppV === wts.pointer) + .assert('string' === typeof wts.memberToJsString('cstr')) + .assert(wts.memberToJsString('cstr') === wts.memberToJsString('$cstr')) + .mustThrowMatching(()=>wts.memberToJsString('xFunc'), + /Invalid member type signature for C-string/) + ; + testFunc(wts.pointer); + T.assert(2===counter).assert(40 === wts.$v4).assert(80n === wts.$v8) + .assert(wts.$ppV === wts.pointer); + /** The 3rd call to wtsFunc throw from JS, which is called + from C, which is called from JS. Let's ensure that + that exception propagates back here... */ + T.mustThrowMatching(()=>testFunc(wts.pointer),/^Testing/); + W.uninstallFunction(wts.$xFunc); + wts.$xFunc = 0; + wts.$ppV = 0; + T.assert(!wts.$ppV); + //WTStruct.debugFlags(0x03); + wts.$ppV = wts; + T.assert(wts.pointer === wts.$ppV) + wts.setMemberCString('cstr', "A C-string."); + T.assert(Array.isArray(wts.ondispose)). + assert(wts.ondispose[0] === wts.$cstr). + assert('A C-string.' === wts.memberToJsString('cstr')); + const ptr = wts.pointer; + wts.dispose(); + T.assert(ptr).assert(undefined === wts.pointer); + }finally{ + wts.dispose(); + } + + if(1){ // ondispose of other struct instances + const s1 = new WTStruct, s2 = new WTStruct, s3 = new WTStruct; + T.assert(s1.lookupMember instanceof Function) + .assert(s1.addOnDispose instanceof Function); + s1.addOnDispose(s2,"testing variadic args"); + T.assert(2===s1.ondispose.length); + s2.addOnDispose(s3); + s1.dispose(); + T.assert(!s2.pointer,"Expecting s2 to be ondispose'd by s1."); + T.assert(!s3.pointer,"Expecting s3 to be ondispose'd by s2."); + } + } + }/*StructBinder*/) + + //////////////////////////////////////////////////////////////////// + .t('sqlite3.wasm.pstack', function(sqlite3){ + const P = wasm.pstack; + const isAllocErr = (e)=>e instanceof sqlite3.WasmAllocError; + const stack = P.pointer; + T.assert(0===Number(stack) % 8 /* must be 8-byte aligned */); + try{ + const remaining = P.remaining; + T.assert(P.quota >= 4096) + .assert(remaining === P.quota) + .mustThrowMatching(()=>P.alloc(0), isAllocErr) + .mustThrowMatching(()=>P.alloc(-1), isAllocErr) + .mustThrowMatching( + ()=>P.alloc('i33'), + (e)=>e instanceof sqlite3.WasmAllocError + ); + ; + let p1 = P.alloc(12); + T.assert(p1 == Number(stack) - 16/*8-byte aligned*/) + .assert(P.pointer === p1); + let p2 = P.alloc(7); + T.assert(p2 == Number(p1)-8/*8-byte aligned, stack grows downwards*/) + .mustThrowMatching(()=>P.alloc(remaining), isAllocErr) + .assert(24 == Number(stack) - Number(p2)) + .assert(P.pointer === p2); + let n = remaining - (Number(stack) - Number(p2)); + let p3 = P.alloc(n); + T.assert(p3 == Number(stack)-Number(remaining)) + .mustThrowMatching(()=>P.alloc(1), isAllocErr); + }finally{ + P.restore(stack); + } + + T.assert(P.pointer === stack); + try { + const [p1, p2, p3] = P.allocChunks(3,'i32'); + let sPos = wasm.ptr.add(stack,-16)/*pstack alloc always rounds to multiple of 8*/; + T.assert(P.pointer === sPos) + .assert(p1 === sPos) + .assert(p2 == Number(p1) + 4) + .assert(p3 == Number(p2) + 4); + T.mustThrowMatching(()=>P.allocChunks(1024, 1024 * 16), + (e)=>e instanceof sqlite3.WasmAllocError) + }finally{ + P.restore(stack); + } + + T.assert(P.pointer === stack); + try { + let [p1, p2, p3] = P.allocPtr(3,false); + let sPos = wasm.ptr.add(stack, + -(4===wasm.ptr.size + ? 16/*pstack alloc always rounds to multiple of 8*/ + : 24)); + T.assert(P.pointer === p1) + .assert(p1 === sPos) + .assert(p2 == Number(p1) + wasm.ptr.size) + .assert(p3 == Number(p2) + wasm.ptr.size); + [p1, p2, p3] = P.allocPtr(3); + T.assert(P.pointer === wasm.ptr.add(sPos, -24)/*3 x 8 bytes*/) + .assert(p2 == Number(p1) + 8) + .assert(p3 == Number(p2) + 8); + p1 = P.allocPtr(); + T.assert(looksLikePtr(p1)); + }finally{ + P.restore(stack); + } + }/*pstack tests*/) + //////////////////////////////////////////////////////////////////// + ;/*end of C/WASM utils checks*/ + + T.g('sqlite3_randomness()') + .t('To memory buffer', function(sqlite3){ + const stack = wasm.pstack.pointer; + try{ + const n = 520; + const p = wasm.pstack.alloc(n); + T.assert(0==wasm.peek8(p)) + .assert(0==wasm.peek8(wasm.ptr.add(p,n,-1))); + T.assert(undefined === capi.sqlite3_randomness(n - 10, p)); + let j, check = 0; + const heap = wasm.heap8u(); + for(j = 0; j < 10 && 0===check; ++j){ + check += heap[wasm.ptr.add(p, j)]; + } + T.assert(check > 0); + check = 0; + // Ensure that the trailing bytes were not modified... + for(j = n - 10; j < n && 0===check; ++j){ + check += heap[wasm.ptr.add(p, j)]; + } + T.assert(0===check); + }finally{ + wasm.pstack.restore(stack); + } + }) + .t('To byte array', function(sqlite3){ + const ta = new Uint8Array(117); + let i, n = 0; + for(i=0; i0); + const t0 = new Uint8Array(0); + T.assert(t0 === capi.sqlite3_randomness(t0), + "0-length array is a special case"); + }) + ;/*end sqlite3_randomness() checks*/ + + //////////////////////////////////////////////////////////////////////// + T.g('sqlite3.oo1') + .t({ + name:'Create db', + //predicate: (sqlite3)=> + test: function(sqlite3){ + const dbFile = '/tester1.db'; + sqlite3.util.sqlite3__wasm_vfs_unlink(0, dbFile); + const db = this.db = new sqlite3.oo1.DB(dbFile, 0 ? 'ct' : 'c'); + db.onclose = { + disposeAfter: [], + disposeBefore: [ + (db)=>{ + //console.debug("db.onclose.before dropping modules"); + //sqlite3.capi.sqlite3_drop_modules(db.pointer, 0); + } + ], + before: function(db){ + while(this.disposeBefore.length){ + const v = this.disposeBefore.shift(); + console.debug("db.onclose.before cleaning up:",v); + if(wasm.isPtr(v)) wasm.dealloc(v); + else if(v instanceof sqlite3.StructBinder.StructType){ + v.dispose(); + }else if(v instanceof Function){ + try{ v(db) } catch(e){ + console.warn("beforeDispose() callback threw:",e); + } + } + } + }, + after: function(){ + while(this.disposeAfter.length){ + const v = this.disposeAfter.shift(); + console.debug("db.onclose.after cleaning up:",v); + if(wasm.isPtr(v)) wasm.dealloc(v); + else if(v instanceof sqlite3.StructBinder.StructType){ + v.dispose(); + }else if(v instanceof Function){ + try{v()} catch(e){/*ignored*/} + } + } + } + }; + + T.assert(wasm.isPtr(db.pointer)) + .mustThrowMatching(()=>db.pointer=1, /read-only/) + .assert(0===sqlite3.capi.sqlite3_extended_result_codes(db.pointer,1)) + .assert('main'===db.dbName(0)) + .assert('string' === typeof db.dbVfsName()) + .assert(db.pointer === wasm.xWrap.testConvertArg('sqlite3*',db)); + // Custom db error message handling via sqlite3_prepare_v2/v3() + let rc = capi.sqlite3_prepare_v3(db.pointer, {/*invalid*/}, -1, 0, null, null); + T.assert(capi.SQLITE_MISUSE === rc) + .assert(0 === capi.sqlite3_errmsg(db.pointer).indexOf("Invalid SQL")) + .assert(dbFile === db.dbFilename()) + .assert(!db.dbFilename('nope')); + //Sanity check DB.checkRc()... + let ex; + try{db.checkRc(rc)} + catch(e){ex = e} + T.assert(ex instanceof sqlite3.SQLite3Error) + .assert(capi.SQLITE_MISUSE===ex.resultCode) + .assert(0===ex.message.indexOf("SQLITE_MISUSE: sqlite3 result code")) + .assert(ex.message.indexOf("Invalid SQL")>0); + T.assert(db === db.checkRc(0)) + .assert(db === sqlite3.oo1.DB.checkRc(db,0)) + .assert(null === sqlite3.oo1.DB.checkRc(null,0)); + this.progressHandlerCount = 0; + if( wasm.compileOptionUsed('OMIT_PROGRESS_CALLBACK') ){ + T.assert( !capi.sqlite3_progress_handler ); + }else{ + T.assert( !!capi.sqlite3_progress_handler ); + capi.sqlite3_progress_handler(db, 5, (p)=>{ + ++this.progressHandlerCount; + return 0; + }, 0); + } + } + }) + + //////////////////////////////////////////////////////////////////// + .t({ + name: "oo1.DB/Stmt.wrapDbHandle()", + test: function(sqlite3){ + /* Maintenance reminder: this function is early in the list to + demonstrate that the wrappers for this.db created by this + function do not interfere with downstream tests, e.g. by + closing this.db.pointer. */ + //sqlite3.config.debug("Proxying",this.db); + const misuseMsg = "SQLITE_MISUSE: Argument must be a WASM sqlite3 pointer"; + T.mustThrowMatching(()=>sqlite3.oo1.DB.wrapHandle(this.db), misuseMsg) + .mustThrowMatching(()=>sqlite3.oo1.DB.wrapHandle(0), misuseMsg); + let dw = sqlite3.oo1.DB.wrapHandle(this.db.pointer); + //sqlite3.config.debug('dw',dw); + T.assert( dw, '!!dw' ) + .assert( dw instanceof sqlite3.oo1.DB, 'dw is-a oo1.DB' ) + .assert( dw.pointer, 'dw.pointer' ) + .assert( dw.pointer === this.db.pointer, 'dw.pointer===db.pointer' ) + .assert( dw.filename === this.db.filename, 'dw.filename===db.filename' ); + + T.assert( dw === dw.exec("select 1") ); + let q; + try { + q = dw.prepare("select 1"); + T.assert( q.step() ) + .assert( !q.step() ); + }finally{ + if( q ) q.finalize(); + } + dw.close(); + T.assert( !dw.pointer ) + .assert( this.db === this.db.exec("select 1") ); + dw = undefined; + + let pDb = 0, pStmt = 0; + const stack = wasm.pstack.pointer; + try { + const ppOut = wasm.pstack.allocPtr(); + T.assert( 0==wasm.peekPtr(ppOut) ); + let rc = capi.sqlite3_open_v2( ":memory:", ppOut, + capi.SQLITE_OPEN_CREATE + | capi.SQLITE_OPEN_READWRITE, + 0); + T.assert( 0===rc, 'open_v2()' ); + pDb = wasm.peekPtr(ppOut); + wasm.pokePtr(ppOut, 0); + T.assert( pDb>0, 'pDb>0' ); + const pTmp = pDb; + dw = sqlite3.oo1.DB.wrapHandle(pDb, true); + pDb = 0; + //sqlite3.config.debug("dw",dw); + T.assert( pTmp===dw.pointer, 'pTmp===dw.pointer' ); + T.assert( dw.filename === "", "dw.filename == "+dw.filename ); + let q = dw.prepare("select 1"); + try { + T.assert( q.step(), "step()" ); + T.assert( !q.step(), "!step()" ); + }finally{ + q.finalize(); + q = undefined; + } + T.assert( dw===dw.exec("select 1") ); + dw.affirmOpen(); + const select1 = "select 1"; + rc = capi.sqlite3_prepare_v2( dw, select1, -1, ppOut, 0 ); + T.assert( 0===rc, 'prepare_v2() rc='+rc ); + pStmt = wasm.peekPtr(ppOut); + T.assert( pStmt && wasm.isPtr(pStmt), 'pStmt is valid?' ); + try { + //log( "capi.sqlite3_sql() =",capi.sqlite3_sql(pStmt)); + T.assert( select1 === capi.sqlite3_sql(pStmt), 'SQL mismatch' ); + q = sqlite3.oo1.Stmt.wrapHandle(dw, pStmt, false); + //log("q@"+pStmt+" does not own handle"); + T.assert( q.step(), "step()" ) + .assert( !q.step(), "!step()" ); + q.finalize(); + q = undefined; + T.assert( select1 === capi.sqlite3_sql(pStmt), 'SQL mismatch' + /* This will fail if we've mismanaged pStmt's lifetime */); + q = sqlite3.oo1.Stmt.wrapHandle(dw, pStmt, true); + pStmt = 0; + q.reset(); + T.assert( q.step(), "step()" ) + .assert( !q.step(), "!step()" ); + }finally{ + if( pStmt ) capi.sqlite3_finalize(pStmt) + if( q ) q.finalize(); + } + + }finally{ + wasm.pstack.restore(stack); + if( pDb ){ capi.sqlite3_close_v2(pDb); } + else if( dw ){ dw.close(); } + } + } + })/*oo1.DB/Stmt.wrapHandle()*/ + + //////////////////////////////////////////////////////////////////// + .t('sqlite3_db_config() and sqlite3_status()', function(sqlite3){ + let rc = capi.sqlite3_db_config(this.db, capi.SQLITE_DBCONFIG_LEGACY_ALTER_TABLE, 0, 0); + T.assert(0===rc); + rc = capi.sqlite3_db_config(this.db, capi.SQLITE_DBCONFIG_MAX+1, 0); + T.assert(capi.SQLITE_MISUSE === rc); + rc = capi.sqlite3_db_config(this.db, capi.SQLITE_DBCONFIG_MAINDBNAME, "main"); + T.assert(0 === rc); + const stack = wasm.pstack.pointer; + try { + const [pCur, pHi] = wasm.pstack.allocChunks(2,'i64'); + wasm.poke32([pCur, pHi], 0); + let [vCur, vHi] = wasm.peek32(pCur, pHi); + T.assert(0===vCur).assert(0===vHi); + rc = capi.sqlite3_status(capi.SQLITE_STATUS_MEMORY_USED, + pCur, pHi, 0); + [vCur, vHi] = wasm.peek32(pCur, pHi); + //console.warn("i32 vCur,vHi",vCur,vHi); + T.assert(0 === rc).assert(vCur > 0).assert(vHi >= vCur); + if(wasm.bigIntEnabled){ + // Again in 64-bit. Recall that pCur and pHi are allocated + // large enough to account for this re-use. + wasm.poke64([pCur, pHi], 0); + rc = capi.sqlite3_status64(capi.SQLITE_STATUS_MEMORY_USED, + pCur, pHi, 0); + [vCur, vHi] = wasm.peek64([pCur, pHi]); + //console.warn("i64 vCur,vHi",vCur,vHi); + T.assert(0 === rc).assert(vCur > 0).assert(vHi >= vCur); + } + }finally{ + wasm.pstack.restore(stack); + } + + capi.sqlite3_db_config(this.db, capi.SQLITE_DBCONFIG_ENABLE_COMMENTS, 0, null); + T.mustThrow(()=>this.db.exec("select 1 /* with comments */"), "SQL comments are disallowed"); + capi.sqlite3_db_config(this.db, capi.SQLITE_DBCONFIG_ENABLE_COMMENTS, 1, null); + this.db.exec("select 1 /* with comments */"); + /* SQLITE_DBCONFIG_ENABLE_ATTACH_... are in the ATTACH-specific tests */ + }) + + //////////////////////////////////////////////////////////////////// + .t('DB.Stmt', function(sqlite3){ + let st = this.db.prepare( + new TextEncoder('utf-8').encode("select 3 as a") + ); + let rc; + try { + T.assert(wasm.isPtr(st.pointer)) + .mustThrowMatching(()=>st.pointer=1, /read-only/) + .assert(1===this.db.openStatementCount()) + .assert( + capi.sqlite3_stmt_status( + st, capi.SQLITE_STMTSTATUS_RUN, 0 + ) === 0) + .assert('a' === st.getColumnName(0)) + .mustThrowMatching(()=>st.columnCount=2, + /columnCount property is read-only/) + .assert(1===st.columnCount) + .assert(0===st.parameterCount) + .assert(0===capi.sqlite3_bind_parameter_count(st)) + .mustThrow(()=>st.bind(1,null)) + .assert(true===st.step()) + .assert(3 === st.get(0)) + .mustThrow(()=>st.get(1)) + .mustThrow(()=>st.get(0,~capi.SQLITE_INTEGER)) + .assert(3 === st.get(0,capi.SQLITE_INTEGER)) + .assert(3 === st.getInt(0)) + .assert('3' === st.get(0,capi.SQLITE_TEXT)) + .assert('3' === st.getString(0)) + .assert(3.0 === st.get(0,capi.SQLITE_FLOAT)) + .assert(3.0 === st.getFloat(0)) + .assert(3 === st.get({}).a) + .assert(3 === st.get([])[0]) + .assert(3 === st.getJSON(0)) + .assert(st.get(0,capi.SQLITE_BLOB) instanceof Uint8Array) + .assert(1===st.get(0,capi.SQLITE_BLOB).length) + .assert(st.getBlob(0) instanceof Uint8Array) + .assert('3'.charCodeAt(0) === st.getBlob(0)[0]) + .assert(false===st.step()) + .mustThrowMatching(()=>st.get(0), + "Stmt.step() has not (recently) returned true.") + .assert( + capi.sqlite3_stmt_status( + st, capi.SQLITE_STMTSTATUS_RUN, 0 + ) > 0); + + T.assert(this.progressHandlerCount>0 + || wasm.compileOptionUsed('OMIT_PROGRESS_CALLBACK'), + "Expecting progress callback."); + }finally{ + rc = st.finalize(); + } + T.assert(!st.pointer) + .assert(0===this.db.openStatementCount()) + .assert(0===rc); + + T.mustThrowMatching(()=>new sqlite3.oo1.Stmt("hi"), function(err){ + return (err instanceof sqlite3.SQLite3Error) + && capi.SQLITE_MISUSE === err.resultCode + && 0 < err.message.indexOf("Do not call the Stmt constructor directly.") + }); + }) + + //////////////////////////////////////////////////////////////////////// + .t('sqlite3_js_...()', function(){ + const db = this.db; + if(1){ + const vfsList = capi.sqlite3_js_vfs_list(); + T.assert(vfsList.length>1); + wasm.scopedAllocCall(()=>{ + const vfsArg = (v)=>wasm.xWrap.testConvertArg('sqlite3_vfs*',v); + for(const v of vfsList){ + T.assert('string' === typeof v); + const pVfs = capi.sqlite3_vfs_find(v); + T.assert(wasm.isPtr(pVfs)) + .assert(pVfs===vfsArg(v)); + const vfs = new capi.sqlite3_vfs(pVfs); + try { T.assert(vfsArg(vfs)===pVfs) } + finally{ vfs.dispose() } + } + }); + } + /** + Trivia: the magic db name ":memory:" does not actually use the + "memdb" VFS unless "memdb" is _explicitly_ provided as the VFS + name. Instead, it uses the default VFS with an in-memory btree. + Thus this.db's VFS may not be memdb even though it's an in-memory + db. + */ + const pVfsMem = capi.sqlite3_vfs_find('memdb'), + pVfsDflt = capi.sqlite3_vfs_find(0), + pVfsDb = capi.sqlite3_js_db_vfs(db.pointer); + T.assert(pVfsMem > 0) + .assert(pVfsDflt > 0) + .assert(pVfsDb > 0) + .assert(pVfsMem !== pVfsDflt + /* memdb lives on top of the default vfs */) + .assert(pVfsDb === pVfsDflt || pVfsDb === pVfsMem) + ; + /*const vMem = new capi.sqlite3_vfs(pVfsMem), + vDflt = new capi.sqlite3_vfs(pVfsDflt), + vDb = new capi.sqlite3_vfs(pVfsDb);*/ + const duv = capi.sqlite3_js_db_uses_vfs; + T.assert(pVfsDflt === duv(db.pointer, 0) + || pVfsMem === duv(db.pointer,0)) + .assert(!duv(db.pointer, "foo")) + ; + }/*sqlite3_js_...()*/) + + //////////////////////////////////////////////////////////////////// + .t('Table t', function(sqlite3){ + const db = this.db; + let list = []; + this.progressHandlerCount = 0; + //wasm.xWrap.debug = true; + let rc = db.exec({ + sql:['CREATE TABLE t(a,b);', + // ^^^ using TEMP TABLE breaks the db export test + "INSERT INTO t(a,b) VALUES(1,2),(3,4),", + "(?,?)"/*intentionally missing semicolon to test for + off-by-one bug in string-to-WASM conversion*/], + saveSql: list, + bind: [5,6] + }); + //debug("Exec'd SQL:", list); + T.assert(rc === db) + .assert(2 === list.length) + .assert('string'===typeof list[1]) + .assert(3===db.changes()) + .assert(this.progressHandlerCount > 0 + || wasm.compileOptionUsed('OMIT_PROGRESS_CALLBACK'), + "Expecting progress callback.") + if(wasm.bigIntEnabled){ + T.assert(3n===db.changes(false,true)); + } + rc = db.exec({ + sql: "INSERT INTO t(a,b) values('blob',X'6869') RETURNING 13", + rowMode: 0 + }); + T.assert(Array.isArray(rc)) + .assert(1===rc.length) + .assert(13 === rc[0]) + .assert(1===db.changes()); + + let vals = db.selectValues('select a from t order by a limit 2'); + T.assert( 2 === vals.length ) + .assert( 1===vals[0] && 3===vals[1] ); + vals = db.selectValues('select a from t order by a limit $L', + {$L:2}, capi.SQLITE_TEXT); + T.assert( 2 === vals.length ) + .assert( '1'===vals[0] && '3'===vals[1] ); + vals = undefined; + + let blob = db.selectValue("select b from t where a='blob'"); + T.assert(blob instanceof Uint8Array). + assert(0x68===blob[0] && 0x69===blob[1]); + blob = null; + let counter = 0, colNames = []; + list.length = 0; + db.exec(new TextEncoder('utf-8').encode("SELECT a a, b b FROM t"),{ + rowMode: 'object', + resultRows: list, + columnNames: colNames, + _myState: 3 /* Accessible from the callback */, + callback: function(row,stmt){ + ++counter; + T.assert( + 3 === this._myState + /* Recall that "this" is the options object. */ + ).assert( + this.columnNames===colNames + ).assert( + this.columnNames[0]==='a' && this.columnNames[1]==='b' + ).assert( + (row.a%2 && row.a<6) || 'blob'===row.a + ); + } + }); + T.assert(2 === colNames.length) + .assert('a' === colNames[0]) + .assert(4 === counter) + .assert(4 === list.length); + colNames = []; + db.exec({ + /* Ensure that columnNames is populated for empty result sets. */ + sql: "SELECT a a, b B FROM t WHERE 0", + columnNames: colNames + }); + T.assert(2===colNames.length) + .assert('a'===colNames[0] && 'B'===colNames[1]); + list.length = 0; + db.exec("SELECT a a, b b FROM t",{ + rowMode: 'array', + callback: function(row,stmt){ + ++counter; + T.assert(Array.isArray(row)) + .assert((0===row[1]%2 && row[1]<7) + || (row[1] instanceof Uint8Array)); + } + }); + T.assert(8 === counter); + T.assert(Number.MIN_SAFE_INTEGER === + db.selectValue("SELECT "+Number.MIN_SAFE_INTEGER)). + assert(Number.MAX_SAFE_INTEGER === + db.selectValue("SELECT "+Number.MAX_SAFE_INTEGER)); + counter = 0; + let rv = db.exec({ + sql: "SELECT a FROM t", + callback: ()=>(1===++counter), + }); + T.assert(db === rv) + .assert(2===counter, + "Expecting exec step() loop to stop if callback returns false."); + /** If exec() is passed neither callback nor returnValue but + is passed an explicit rowMode then the default returnValue + is the whole result set, as if an empty resultRows option + had been passed. */ + rv = db.exec({ + sql: "SELECT -1 UNION ALL SELECT -2 UNION ALL SELECT -3 ORDER BY 1 DESC", + rowMode: 0 + }); + T.assert(Array.isArray(rv)).assert(3===rv.length) + .assert(-1===rv[0]).assert(-3===rv[2]); + rv = db.exec("SELECT 1 WHERE 0",{rowMode: 0}); + T.assert(Array.isArray(rv)).assert(0===rv.length); + if(wasm.bigIntEnabled && haveWasmCTests()){ + const mI = wasm.xCall('sqlite3__wasm_test_int64_max'); + const b = BigInt(Number.MAX_SAFE_INTEGER * 2); + T.assert(b === db.selectValue("SELECT "+b)). + assert(b === db.selectValue("SELECT ?", b)). + assert(mI == db.selectValue("SELECT $x", {$x:mI})); + }else{ + /* Curiously, the JS spec seems to be off by one with the definitions + of MIN/MAX_SAFE_INTEGER: + + https://github.com/emscripten-core/emscripten/issues/17391 */ + T.mustThrow(()=>db.selectValue("SELECT "+(Number.MAX_SAFE_INTEGER+1))). + mustThrow(()=>db.selectValue("SELECT "+(Number.MIN_SAFE_INTEGER-1))); + } + + let st = db.prepare("update t set b=:b where a='blob'"); + try { + T.assert(0===st.columnCount) + .assert(1===st.parameterCount) + .assert(1===capi.sqlite3_bind_parameter_count(st)) + .assert( false===st.isReadOnly() ); + const ndx = st.getParamIndex(':b'); + T.assert(1===ndx); + st.bindAsBlob(ndx, "ima blob") + /*step() skipped intentionally*/.reset(true); + } finally { + T.assert(0===st.finalize()) + .assert(undefined===st.finalize()); + } + + try { + db.prepare("/*empty SQL*/"); + toss("Must not be reached."); + }catch(e){ + T.assert(e instanceof sqlite3.SQLite3Error) + .assert(0==e.message.indexOf('Cannot prepare empty')); + } + + counter = 0; + db.exec({ + // Check for https://sqlite.org/forum/forumpost/895425b49a + sql: "pragma table_info('t')", + rowMode: 'object', + callback: function(row){ + ++counter; + T.assert(row.name==='a' || row.name==='b'); + } + }); + T.assert(2===counter); + })/*setup table T*/ + + //////////////////////////////////////////////////////////////////// + .t({ + name: "sqlite3_set_authorizer()", + predicate: ()=>!!wasm.exports.sqlite3_set_authorizer || "Missing sqlite3_set_authorizer()", + test:function(sqlite3){ + T.assert(capi.SQLITE_IGNORE>0) + .assert(capi.SQLITE_DENY>0); + const db = this.db; + const ssa = capi.sqlite3_set_authorizer; + const n = db.selectValue('select count(*) from t'); + T.assert(n>0); + let authCount = 0; + let rc = ssa(db, function(pV, iCode, s0, s1, s2, s3){ + ++authCount; + return capi.SQLITE_IGNORE; + }, 0); + T.assert(0===rc) + .assert( + undefined === db.selectValue('select count(*) from t') + /* Note that the count() never runs, so we get undefined + instead of 0. */ + ) + .assert(authCount>0); + authCount = 0; + db.exec("update t set a=-9999"); + T.assert(authCount>0); + /* Reminder: we don't use DELETE because, from the C API docs: + + "If the action code is [SQLITE_DELETE] and the callback + returns [SQLITE_IGNORE] then the [DELETE] operation proceeds + but the [truncate optimization] is disabled and all rows are + deleted individually." + */ + rc = ssa(db, null, 0); + authCount = 0; + T.assert(-9999 != db.selectValue('select a from t')) + .assert(0===authCount); + rc = ssa(db, function(pV, iCode, s0, s1, s2, s3){ + ++authCount; + return capi.SQLITE_DENY; + }, 0); + T.assert(0===rc); + let err; + try{ db.exec("select 1 from t") } + catch(e){ err = e } + T.assert(err instanceof sqlite3.SQLite3Error) + .assert(err.message.indexOf('not authorized'>0)) + .assert(1===authCount); + authCount = 0; + rc = ssa(db, function(...args){ + ++authCount; + return capi.SQLITE_OK; + }, 0); + T.assert(0===rc); + T.assert(n === db.selectValue('select count(*) from t')) + .assert(authCount>0); + authCount = 0; + rc = ssa(db, function(pV, iCode, s0, s1, s2, s3){ + ++authCount; + throw new Error("Testing catching of authorizer."); + }, 0); + T.assert(0===rc); + authCount = 0; + err = undefined; + try{ db.exec("select 1 from t") } + catch(e){err = e} + T.assert(err instanceof Error) + .assert(err.message.indexOf('not authorized')>0) + /* Note that the thrown message is trumped/overwritten + by the authorizer process. */ + .assert(1===authCount); + rc = ssa(db, 0, 0); + authCount = 0; + T.assert(0===rc); + T.assert(n === db.selectValue('select count(*) from t')) + .assert(0===authCount); + } + })/*sqlite3_set_authorizer()*/ + + //////////////////////////////////////////////////////////////////////// + .t("sqlite3_table_column_metadata()", function(sqlite3){ + const stack = wasm.pstack.pointer; + try{ + const [pzDT, pzColl, pNotNull, pPK, pAuto] = + wasm.pstack.allocPtr(5); + const rc = capi.sqlite3_table_column_metadata( + this.db, "main", "t", "rowid", + pzDT, pzColl, pNotNull, pPK, pAuto + ); + T.assert(0===rc) + .assert("INTEGER"===wasm.cstrToJs(wasm.peekPtr(pzDT))) + .assert("BINARY"===wasm.cstrToJs(wasm.peekPtr(pzColl))) + .assert(0===wasm.peek32(pNotNull)) + .assert(1===wasm.peek32(pPK)) + .assert(0===wasm.peek32(pAuto)) + }finally{ + wasm.pstack.restore(stack); + } + }) + + //////////////////////////////////////////////////////////////////////// + .t('selectArray/Object()', function(sqlite3){ + const db = this.db; + let rc = db.selectArray('select a, b from t where a=?', 5); + T.assert(Array.isArray(rc)) + .assert(2===rc.length) + .assert(5===rc[0] && 6===rc[1]); + rc = db.selectArray('select a, b from t where b=-1'); + T.assert(undefined === rc); + rc = db.selectObject('select a A, b b from t where b=?', 6); + T.assert(rc && 'object'===typeof rc) + .assert(5===rc.A) + .assert(6===rc.b); + rc = db.selectArray('select a, b from t where b=-1'); + T.assert(undefined === rc); + }) + //////////////////////////////////////////////////////////////////////// + .t('selectArrays/Objects()', function(sqlite3){ + const db = this.db; + const sql = 'select a, b from t where a=? or b=? order by a'; + let rc = db.selectArrays(sql, [1, 4]); + T.assert(Array.isArray(rc)) + .assert(2===rc.length) + .assert(2===rc[0].length) + .assert(1===rc[0][0]) + .assert(2===rc[0][1]) + .assert(3===rc[1][0]) + .assert(4===rc[1][1]) + rc = db.selectArrays(sql, [99,99]); + T.assert(Array.isArray(rc)).assert(0===rc.length); + rc = db.selectObjects(sql, [1,4]); + T.assert(Array.isArray(rc)) + .assert(2===rc.length) + .assert('object' === typeof rc[1]) + .assert(1===rc[0].a) + .assert(2===rc[0].b) + .assert(3===rc[1].a) + .assert(4===rc[1].b); + }) + //////////////////////////////////////////////////////////////////////// + .t('selectArray/Object/Values() via INSERT/UPDATE...RETURNING', function(sqlite3){ + let rc = this.db.selectObject("INSERT INTO t(a,b) VALUES(83,84) RETURNING a as AA"); + T.assert(83===rc.AA); + rc = this.db.selectArray("UPDATE T set a=85 WHERE a=83 RETURNING b as BB"); + T.assert(Array.isArray(rc)).assert(84===rc[0]); + //log("select * from t:",this.db.selectObjects("select * from t order by a")); + rc = this.db.selectValues("UPDATE T set a=a*1 RETURNING a"); + T.assert(Array.isArray(rc)) + .assert(5 === rc.length) + .assert('number'===typeof rc[0]) + .assert(rc[0]|0 === rc[0] /* is small integer */); + }) + //////////////////////////////////////////////////////////////////////// + .t({ + name: 'sqlite3_js_db_export()', + predicate: ()=>true, + test: function(sqlite3){ + const db = this.db; + const xp = capi.sqlite3_js_db_export(db.pointer); + T.assert(xp instanceof Uint8Array) + .assert(xp.byteLength>0) + .assert(0 === xp.byteLength % 512); + this.dbExport = xp; + } + }/*sqlite3_js_db_export()*/) + .t({ + name: 'sqlite3_js_posix_create_file()', + predicate: ()=>true, + test: function(sqlite3){ + const db = this.db; + const filename = "sqlite3_js_posix_create_file.db"; + capi.sqlite3_js_posix_create_file(filename, this.dbExport); + delete this.dbExport; + const db2 = new sqlite3.oo1.DB(filename,'r'); + try { + const sql = "select count(*) from t"; + const n = db.selectValue(sql); + T.assert(n>0 && db2.selectValue(sql) === n); + }finally{ + db2.close(); + sqlite3.util.sqlite3__wasm_vfs_unlink(0, filename); + } + } + }/*sqlite3_js_posix_create_file()*/) + + //////////////////////////////////////////////////////////////////// + .t({ + name:'Scalar UDFs', + test: function(sqlite3){ + const db = this.db; + db.createFunction( + "foo", + 1 ? (pCx,a,b)=>a+b + : (pCx,a,b)=>{ + /*return sqlite3.capi.sqlite3_result_error_js( + db, sqlite3.capi.SQLITE_ERROR, "foo???" + );*/ + console.debug("foo UDF", pCx, a, b); + return Number(a)+Number(b); + } + ); + T.assert(7===db.selectValue("select foo(3,4)")). + assert(5===db.selectValue("select foo(3,?)",2)). + assert(5===db.selectValue("select foo(?,?2)",[1,4])). + assert(5===db.selectValue("select foo($a,$b)",{$a:0,$b:5})); + db.createFunction("bar", { + arity: -1, + xFunc: (pCx,...args)=>{ + T.assert(db.pointer === capi.sqlite3_context_db_handle(pCx)); + let rc = 0; + for(const v of args) rc += v; + return rc; + } + }).createFunction({ + name: "asis", + xFunc: (pCx,arg)=>arg + }); + T.assert(0===db.selectValue("select bar()")). + assert(1===db.selectValue("select bar(1)")). + assert(3===db.selectValue("select bar(1,2)")). + assert(-1===db.selectValue("select bar(1,2,-4)")). + assert('hi' === db.selectValue("select asis('hi')")). + assert('hi' === db.selectValue("select ?",'hi')). + assert(null === db.selectValue("select null")). + assert(null === db.selectValue("select asis(null)")). + assert(1 === db.selectValue("select ?",1)). + assert(2 === db.selectValue("select ?",[2])). + assert(3 === db.selectValue("select $a",{$a:3})). + assert(T.eqApprox(3.1,db.selectValue("select 3.0 + 0.1"))). + assert(T.eqApprox(1.3,db.selectValue("select asis(1 + 0.3)"))); + + let blobArg = new Uint8Array([0x68, 0x69]); + let blobRc = db.selectValue( + "select asis(?1)", + blobArg.buffer/*confirm that ArrayBuffer is handled as a Uint8Array*/ + ); + T.assert(blobRc instanceof Uint8Array). + assert(2 === blobRc.length). + assert(0x68==blobRc[0] && 0x69==blobRc[1]); + blobRc = db.selectValue("select asis(X'6869')"); + T.assert(blobRc instanceof Uint8Array). + assert(2 === blobRc.length). + assert(0x68==blobRc[0] && 0x69==blobRc[1]); + + blobArg = new Int8Array([0x68, 0x69]); + //debug("blobArg=",blobArg); + blobRc = db.selectValue("select asis(?1)", blobArg); + T.assert(blobRc instanceof Uint8Array). + assert(2 === blobRc.length); + //debug("blobRc=",blobRc); + T.assert(0x68==blobRc[0] && 0x69==blobRc[1]); + + let rc = sqlite3.capi.sqlite3_create_function_v2( + this.db, "foo", 0, -1, 0, 0, 0, 0, 0 + ); + T.assert( + sqlite3.capi.SQLITE_FORMAT === rc, + "For invalid eTextRep argument." + ); + rc = sqlite3.capi.sqlite3_create_function_v2(this.db, "foo", 0); + T.assert( + sqlite3.capi.SQLITE_MISUSE === rc, + "For invalid arg count." + ); + + /* Confirm that we can map and unmap the same function with + multiple arities... */ + const fCounts = [0,0]; + const fArityCheck = function(pCx){ + return ++fCounts[arguments.length-1]; + }; + //wasm.xWrap.FuncPtrAdapter.debugFuncInstall = true; + rc = capi.sqlite3_create_function_v2( + db, "nary", 0, capi.SQLITE_UTF8, 0, fArityCheck, 0, 0, 0 + ); + T.assert( 0===rc ); + rc = capi.sqlite3_create_function_v2( + db, "nary", 1, capi.SQLITE_UTF8, 0, fArityCheck, 0, 0, 0 + ); + T.assert( 0===rc ); + const sqlFArity0 = "select nary()"; + const sqlFArity1 = "select nary(1)"; + T.assert( 1 === db.selectValue(sqlFArity0) ) + .assert( 1 === fCounts[0] ).assert( 0 === fCounts[1] ); + T.assert( 1 === db.selectValue(sqlFArity1) ) + .assert( 1 === fCounts[0] ).assert( 1 === fCounts[1] ); + capi.sqlite3_create_function_v2( + db, "nary", 0, capi.SQLITE_UTF8, 0, 0, 0, 0, 0 + ); + T.mustThrowMatching((()=>db.selectValue(sqlFArity0)), + (e)=>((e instanceof sqlite3.SQLite3Error) + && e.message.indexOf("wrong number of arguments")>0), + "0-arity variant was uninstalled."); + T.assert( 2 === db.selectValue(sqlFArity1) ) + .assert( 1 === fCounts[0] ).assert( 2 === fCounts[1] ); + capi.sqlite3_create_function_v2( + db, "nary", 1, capi.SQLITE_UTF8, 0, 0, 0, 0, 0 + ); + T.mustThrowMatching((()=>db.selectValue(sqlFArity1)), + (e)=>((e instanceof sqlite3.SQLite3Error) + && e.message.indexOf("no such function")>0), + "1-arity variant was uninstalled."); + //wasm.xWrap.FuncPtrAdapter.debugFuncInstall = false; + } + }) + + //////////////////////////////////////////////////////////////////// + .t({ + name: 'Aggregate UDFs', + //predicate: ()=>false, + test: function(sqlite3){ + const db = this.db; + const sjac = capi.sqlite3_js_aggregate_context; + db.createFunction({ + name: 'summer', + xStep: (pCtx, n)=>{ + const ac = sjac(pCtx, 4); + wasm.poke32(ac, wasm.peek32(ac) + Number(n)); + }, + xFinal: (pCtx)=>{ + const ac = sjac(pCtx, 0); + return ac ? wasm.peek32(ac) : 0; + } + }); + let v = db.selectValue([ + "with cte(v) as (", + "select 3 union all select 5 union all select 7", + ") select summer(v), summer(v+1) from cte" + /* ------------------^^^^^^^^^^^ ensures that we're handling + sqlite3_aggregate_context() properly. */ + ]); + T.assert(15===v); + T.mustThrowMatching(()=>db.selectValue("select summer(1,2)"), + /wrong number of arguments/); + + db.createFunction({ + name: 'summerN', + arity: -1, + xStep: (pCtx, ...args)=>{ + const ac = sjac(pCtx, 4); + let sum = wasm.peek32(ac); + for(const v of args) sum += Number(v); + wasm.poke32(ac, sum); + }, + xFinal: (pCtx)=>{ + const ac = sjac(pCtx, 0); + capi.sqlite3_result_int( pCtx, ac ? wasm.peek32(ac) : 0 ); + // xFinal() may either return its value directly or call + // sqlite3_result_xyz() and return undefined. Both are + // functionally equivalent. + } + }); + T.assert(18===db.selectValue('select summerN(1,8,9), summerN(2,3,4)')); + T.mustThrowMatching(()=>{ + db.createFunction('nope',{ + xFunc: ()=>{}, xStep: ()=>{} + }); + }, /scalar or aggregate\?/); + T.mustThrowMatching(()=>{ + db.createFunction('nope',{xStep: ()=>{}}); + }, /Missing xFinal/); + T.mustThrowMatching(()=>{ + db.createFunction('nope',{xFinal: ()=>{}}); + }, /Missing xStep/); + T.mustThrowMatching(()=>{ + db.createFunction('nope',{}); + }, /Missing function-type properties/); + T.mustThrowMatching(()=>{ + db.createFunction('nope',{xFunc:()=>{}, xDestroy:'nope'}); + }, /xDestroy property must be a function/); + T.mustThrowMatching(()=>{ + db.createFunction('nope',{xFunc:()=>{}, pApp:'nope'}); + }, /Invalid value for pApp/); + } + }/*aggregate UDFs*/) + + //////////////////////////////////////////////////////////////////////// + .t({ + name: 'Aggregate UDFs (64-bit)', + predicate: ()=>wasm.bigIntEnabled, + //predicate: ()=>false, + test: function(sqlite3){ + const db = this.db; + const sjac = capi.sqlite3_js_aggregate_context; + db.createFunction({ + name: 'summer64', + xStep: (pCtx, n)=>{ + const ac = sjac(pCtx, 8); + wasm.poke64(ac, wasm.peek64(ac) + BigInt(n)); + }, + xFinal: (pCtx)=>{ + const ac = sjac(pCtx, 0); + return ac ? wasm.peek64(ac) : 0n; + } + }); + let v = db.selectValue([ + "with cte(v) as (", + "select 9007199254740991 union all select 1 union all select 2", + ") select summer64(v), summer64(v+1) from cte" + ]); + T.assert(9007199254740994n===v); + } + }/*aggregate UDFs*/) + + //////////////////////////////////////////////////////////////////// + .t({ + name: 'Window UDFs', + predicate: (sqlite3)=>!!sqlite3.wasm.exports.sqlite3_create_window_function + /*!sqlite3.wasm.compileOptionUsed('OMIT_WINDOWFUNC')*/ + || "Missing window functions", + test: function(){ + /* Example window function, table, and results taken from: + https://sqlite.org/windowfunctions.html#udfwinfunc */ + const db = this.db; + const sjac = (cx,n=4)=>capi.sqlite3_js_aggregate_context(cx,n); + const xValueFinal = (pCtx)=>{ + const ac = sjac(pCtx, 0); + return ac ? wasm.peek32(ac) : 0; + }; + const xStepInverse = (pCtx, n)=>{ + const ac = sjac(pCtx); + wasm.poke32(ac, wasm.peek32(ac) + Number(n)); + }; + db.createFunction({ + name: 'winsumint', + xStep: (pCtx, n)=>xStepInverse(pCtx, n), + xInverse: (pCtx, n)=>xStepInverse(pCtx, -n), + xFinal: xValueFinal, + xValue: xValueFinal + }); + db.exec([ + "CREATE TEMP TABLE twin(x, y); INSERT INTO twin VALUES", + "('a', 4),('b', 5),('c', 3),('d', 8),('e', 1)" + ]); + let rc = db.exec({ + returnValue: 'resultRows', + sql:[ + "SELECT x, winsumint(y) OVER (", + "ORDER BY x ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING", + ") AS sum_y ", + "FROM twin ORDER BY x;" + ] + }); + T.assert(Array.isArray(rc)) + .assert(5 === rc.length); + let count = 0; + for(const row of rc){ + switch(++count){ + case 1: T.assert('a'===row[0] && 9===row[1]); break; + case 2: T.assert('b'===row[0] && 12===row[1]); break; + case 3: T.assert('c'===row[0] && 16===row[1]); break; + case 4: T.assert('d'===row[0] && 12===row[1]); break; + case 5: T.assert('e'===row[0] && 9===row[1]); break; + default: toss("Too many rows to window function."); + } + } + const resultRows = []; + rc = db.exec({ + resultRows, + returnValue: 'resultRows', + sql:[ + "SELECT x, winsumint(y) OVER (", + "ORDER BY x ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING", + ") AS sum_y ", + "FROM twin ORDER BY x;" + ] + }); + T.assert(rc === resultRows) + .assert(5 === rc.length); + + rc = db.exec({ + returnValue: 'saveSql', + sql: "select 1; select 2; -- empty\n; select 3" + }); + T.assert(Array.isArray(rc)) + .assert(3===rc.length) + .assert('select 1;' === rc[0]) + .assert('select 2;' === rc[1]) + .assert('-- empty\n; select 3' === rc[2] + /* Strange but true. */); + T.mustThrowMatching(()=>{ + db.exec({sql:'', returnValue: 'nope'}); + }, /^Invalid returnValue/); + + db.exec("DROP TABLE twin"); + } + }/*window UDFs*/) + + //////////////////////////////////////////////////////////////////// + .t("ATTACH", function(sqlite3){ + const db = this.db; + const resultRows = []; + db.exec({ + sql:new TextEncoder('utf-8').encode([ + // ^^^ testing string-vs-typedarray handling in exec() + "attach 'session' as foo;", + "create table foo.bar(a);", + "insert into foo.bar(a) values(1),(2),(3);", + "select a from foo.bar order by a;" + ].join('')), + rowMode: 0, + resultRows + }); + T.assert(3===resultRows.length) + .assert(2===resultRows[1]); + T.assert(2===db.selectValue('select a from foo.bar where a>1 order by a')); + + /** Demonstrate the JS-simplified form of the sqlite3_exec() callback... */ + let colCount = 0, rowCount = 0; + let rc = capi.sqlite3_exec( + db, "select a, a*2 from foo.bar", function(aVals, aNames){ + //console.warn("execCallback(",arguments,")"); + colCount = aVals.length; + ++rowCount; + T.assert(2===aVals.length) + .assert(2===aNames.length) + .assert(+(aVals[1]) === 2 * +(aVals[0])); + }, 0, 0 + ); + T.assert(0===rc).assert(3===rowCount).assert(2===colCount); + rc = capi.sqlite3_exec( + db.pointer, "select a from foo.bar", ()=>{ + tossQuietly("Testing throwing from exec() callback."); + }, 0, 0 + ); + T.assert(capi.SQLITE_ABORT === rc); + + /* Demonstrate how to get access to the "full" callback + signature, as opposed to the simplified JS-specific one... */ + rowCount = colCount = 0; + const pCb = wasm.installFunction('i(pipp)', function(pVoid,nCols,aVals,aCols){ + /* Tip: wasm.cArgvToJs() can be used to convert aVals and + aCols to arrays: const vals = wasm.cArgvToJs(nCols, + aVals); */ + ++rowCount; + colCount = nCols; + T.assert(2 === nCols) + .assert(wasm.isPtr(pVoid)) + .assert(wasm.isPtr(aVals)) + .assert(wasm.isPtr(aCols)) + .assert(+wasm.cstrToJs(wasm.peekPtr(wasm.ptr.add(aVals, wasm.ptr.size))) + === 2 * +wasm.cstrToJs(wasm.peekPtr(aVals))); + return 0; + }); + try { + T.assert(wasm.isPtr(pCb)); + rc = capi.sqlite3_exec( + db, new TextEncoder('utf-8').encode("select a, a*2 from foo.bar"), + pCb, 0, 0 + ); + T.assert(0===rc) + .assert(3===rowCount) + .assert(2===colCount); + }finally{ + wasm.uninstallFunction(pCb); + } + + // Demonstrate that an OOM result does not propagate through sqlite3_exec()... + rc = capi.sqlite3_exec( + db, ["select a,"," a*2 from foo.bar"], (aVals, aNames)=>{ + sqlite3.WasmAllocError.toss("just testing"); + }, 0, 0 + ); + T.assert(capi.SQLITE_ABORT === rc); + + db.exec("detach foo"); + T.mustThrow(()=>db.exec("select * from foo.bar"), + "Because foo is no longer attached."); + + /* SQLITE_DBCONFIG_ENABLE_ATTACH_CREATE/WRITE... */ + const db2 = new sqlite3.oo1.DB(); + try{ + capi.sqlite3_db_config(db2, capi.SQLITE_DBCONFIG_ENABLE_ATTACH_CREATE, 0, null); + T.mustThrow(()=>db2.exec("attach 'attached.db' as foo"), + "Cannot create a new db via ATTACH"); + capi.sqlite3_db_config(db2, capi.SQLITE_DBCONFIG_ENABLE_ATTACH_CREATE, 1, null); + db2.exec([ + "attach 'attached.db' as foo;", + "create table foo.t(a);", + "insert into foo.t(a) values(1);", + "detach foo;" + ]); + capi.sqlite3_db_config(db2, capi.SQLITE_DBCONFIG_ENABLE_ATTACH_WRITE, 0, null); + db2.exec("attach 'attached.db' as foo"); + T.mustThrow(()=>db2.exec("insert into foo.t(a) values(2)"), + "ATTACH_WRITE is false"); + capi.sqlite3_db_config(db2, capi.SQLITE_DBCONFIG_ENABLE_ATTACH_WRITE, 1, null); + db2.exec([ + "detach foo;", + "attach 'attached.db' as foo;", + "insert into foo.t(a) values(2);", + "drop table foo.t;", + "detach foo" + ]); + }finally{ + db2.close(); + } + })/*ATTACH tests*/ + + //////////////////////////////////////////////////////////////////// + .t("Read-only", function(sqlite3){ + T.assert( 0===capi.sqlite3_db_readonly(this.db, "main") ); + const db = new sqlite3.oo1.DB('file://'+this.db.filename+'?mode=ro'); + T.assert( 1===capi.sqlite3_db_readonly(db, "main") ); + T.assert( -1===capi.sqlite3_db_readonly(db, "nope") ); + db.close(); + }) + + //////////////////////////////////////////////////////////////////// + .t({ + name: 'C-side WASM tests', + predicate: ()=>(haveWasmCTests() || "Not compiled in."), + test: function(){ + const w = wasm, db = this.db; + const stack = w.scopedAllocPush(); + let ptrInt; + const origValue = 512; + try{ + ptrInt = w.scopedAlloc(4); + w.poke32(ptrInt,origValue); + const cf = w.xGet('sqlite3__wasm_test_intptr'); + const oldPtrInt = ptrInt; + T.assert(origValue === w.peek32(ptrInt)); + const rc = cf(ptrInt); + T.assert(2*origValue === rc). + assert(rc === w.peek32(ptrInt)). + assert(oldPtrInt === ptrInt); + const pi64 = w.scopedAlloc(8)/*ptr to 64-bit integer*/; + const o64 = 0x010203040506/*>32-bit integer*/; + if(w.bigIntEnabled){ + w.poke64(pi64, o64); + //log("pi64 =",pi64, "o64 = 0x",o64.toString(16), o64); + const v64 = ()=>w.peek64(pi64) + T.assert(v64() == o64); + //T.assert(o64 === w.peek64(pi64)); + const cf64w = w.xGet('sqlite3__wasm_test_int64ptr'); + cf64w(pi64); + T.assert(v64() == BigInt(2 * o64)); + cf64w(pi64); + T.assert(v64() == BigInt(4 * o64)); + + const biTimes2 = w.xGet('sqlite3__wasm_test_int64_times2'); + T.assert(BigInt(2 * o64) === + biTimes2(BigInt(o64)/*explicit conv. required to avoid TypeError + in the call :/ */)); + + const pMin = w.scopedAlloc(16); + const pMax = w.ptr.add(pMin, 8); + const g64 = (p)=>w.peek64(p); + w.poke64([pMin, pMax], 0); + const minMaxI64 = [ + w.xCall('sqlite3__wasm_test_int64_min'), + w.xCall('sqlite3__wasm_test_int64_max') + ]; + T.assert(minMaxI64[0] < BigInt(Number.MIN_SAFE_INTEGER)). + assert(minMaxI64[1] > BigInt(Number.MAX_SAFE_INTEGER)); + //log("int64_min/max() =",minMaxI64, typeof minMaxI64[0]); + w.xCall('sqlite3__wasm_test_int64_minmax', pMin, pMax); + T.assert(g64(pMin) === minMaxI64[0], "int64 mismatch"). + assert(g64(pMax) === minMaxI64[1], "int64 mismatch"); + //log("pMin",g64(pMin), "pMax",g64(pMax)); + w.poke64(pMin, minMaxI64[0]); + T.assert(g64(pMin) === minMaxI64[0]). + assert(minMaxI64[0] === db.selectValue("select ?",g64(pMin))). + assert(minMaxI64[1] === db.selectValue("select ?",g64(pMax))); + const rxRange = /too big/; + T.mustThrowMatching(()=>{db.prepare("select ?").bind(minMaxI64[0] - BigInt(1))}, + rxRange). + mustThrowMatching(()=>{db.prepare("select ?").bind(minMaxI64[1] + BigInt(1))}, + (e)=>rxRange.test(e.message)); + }else{ + log("No BigInt support. Skipping related tests."); + log("\"The problem\" here is that we can manipulate, at the byte level,", + "heap memory to set 64-bit values, but we can't get those values", + "back into JS because of the lack of 64-bit integer support."); + } + }finally{ + //const x = w.scopedAlloc(1), y = w.scopedAlloc(1), z = w.scopedAlloc(1); + //log("x=",x,"y=",y,"z=",z); // just looking at the alignment + w.scopedAllocPop(stack); + } + } + }/* jaccwabyt-specific tests */) + + //////////////////////////////////////////////////////////////////////// + .t({ + name: 'virtual table #1: eponymous w/ manual exception handling', + predicate: (sqlite3)=>(!!sqlite3.capi.sqlite3_vtab || "Missing vtab support"), + test: function(sqlite3){ + const VT = sqlite3.vtab; + const tmplCols = Object.assign(Object.create(null),{ + A: 0, B: 1 + }); + /** + The vtab demonstrated here is a JS-ification of + ext/misc/templatevtab.c. + */ + const tmplMod = new sqlite3.capi.sqlite3_module(); + T.assert(!tmplMod.$xUpdate); + const dbg = 1 ? ()=>{} : sqlite3.config.debug; + //tmplMod.debugFlags(0x03); + tmplMod.setupModule({ + catchExceptions: false, + methods: { + xConnect: function(pDb, pAux, argc, argv, ppVtab, pzErr){ + dbg("xConnect",...arguments); + try{ + const args = wasm.cArgvToJs(argc, argv); + T.assert(args.length>=3) + .assert(args[0] === 'testvtab') + .assert(args[1] === 'main') + .assert(args[2] === 'testvtab'); + const rc = capi.sqlite3_declare_vtab( + pDb, "CREATE TABLE ignored(a,b)" + ); + if(0===rc){ + const t = VT.xVtab.create(ppVtab); + T.assert(t === VT.xVtab.get(wasm.peekPtr(ppVtab))); + } + return rc; + }catch(e){ + if(!(e instanceof sqlite3.WasmAllocError)){ + wasm.dealloc(wasm.peekPtr, pzErr); + wasm.pokePtr(pzErr, wasm.allocCString(e.message)); + } + return VT.xError('xConnect',e); + } + }, + xCreate: true /* just for testing. Will be removed afterwards. */, + xDisconnect: function(pVtab){ + dbg("xDisconnect",...arguments); + try { + VT.xVtab.unget(pVtab).dispose(); + return 0; + }catch(e){ + return VT.xError('xDisconnect',e); + } + }, + xOpen: function(pVtab, ppCursor){ + dbg("xOpen",...arguments); + try{ + const t = VT.xVtab.get(pVtab), + c = VT.xCursor.create(ppCursor); + T.assert(t instanceof capi.sqlite3_vtab) + .assert(c instanceof capi.sqlite3_vtab_cursor); + c._rowId = 0; + return 0; + }catch(e){ + return VT.xError('xOpen',e); + } + }, + xClose: function(pCursor){ + dbg("xClose",...arguments); + try{ + const c = VT.xCursor.unget(pCursor); + T.assert(c instanceof capi.sqlite3_vtab_cursor) + .assert(!VT.xCursor.get(pCursor)); + c.dispose(); + return 0; + }catch(e){ + return VT.xError('xClose',e); + } + }, + xNext: function(pCursor){ + dbg("xNext",...arguments); + try{ + const c = VT.xCursor.get(pCursor); + ++c._rowId; + return 0; + }catch(e){ + return VT.xError('xNext',e); + } + }, + xColumn: function(pCursor, pCtx, iCol){ + dbg("xColumn",...arguments); + try{ + const c = VT.xCursor.get(pCursor); + switch(iCol){ + case tmplCols.A: + capi.sqlite3_result_int(pCtx, 1000 + c._rowId); + break; + case tmplCols.B: + capi.sqlite3_result_int(pCtx, 2000 + c._rowId); + break; + default: sqlite3.SQLite3Error.toss("Invalid column id",iCol); + } + return 0; + }catch(e){ + return VT.xError('xColumn',e); + } + }, + xRowid: function(pCursor, ppRowid64){ + dbg("xRowid",...arguments); + try{ + const c = VT.xCursor.get(pCursor); + VT.xRowid(ppRowid64, c._rowId); + return 0; + }catch(e){ + return VT.xError('xRowid',e); + } + }, + xEof: function(pCursor){ + dbg("xEof",...arguments); + const c = VT.xCursor.get(pCursor), + rc = c._rowId>=10; + return rc; + }, + xFilter: function(pCursor, idxNum, idxCStr, + argc, argv/* [sqlite3_value* ...] */){ + dbg("xFilter",...arguments); + try{ + const c = VT.xCursor.get(pCursor); + c._rowId = 0; + const list = capi.sqlite3_values_to_js(argc, argv); + T.assert(argc === list.length); + //log(argc,"xFilter value(s):",list); + return 0; + }catch(e){ + return VT.xError('xFilter',e); + } + }, + xBestIndex: function(pVtab, pIdxInfo){ + dbg("xBestIndex",...arguments); + try{ + //const t = VT.xVtab.get(pVtab); + const sii = capi.sqlite3_index_info; + const pii = new sii(pIdxInfo); + pii.$estimatedRows = 10; + pii.$estimatedCost = 10.0; + //log("xBestIndex $nConstraint =",pii.$nConstraint); + if(pii.$nConstraint>0){ + // Validate nthConstraint() and nthConstraintUsage() + const max = pii.$nConstraint; + for(let i=0; i < max; ++i ){ + let v = pii.nthConstraint(i,true); + T.assert(wasm.isPtr(v)); + v = pii.nthConstraint(i); + T.assert(v instanceof sii.sqlite3_index_constraint) + .assert(v.pointer >= pii.$aConstraint); + v.dispose(); + v = pii.nthConstraintUsage(i,true); + T.assert(wasm.isPtr(v)); + v = pii.nthConstraintUsage(i); + T.assert(v instanceof sii.sqlite3_index_constraint_usage) + .assert(v.pointer >= pii.$aConstraintUsage); + v.$argvIndex = i;//just to get some values into xFilter + v.dispose(); + } + } + //log("xBestIndex $nOrderBy =",pii.$nOrderBy); + if(pii.$nOrderBy>0){ + // Validate nthOrderBy() + const max = pii.$nOrderBy; + for(let i=0; i < max; ++i ){ + let v = pii.nthOrderBy(i,true); + T.assert(wasm.isPtr(v)); + v = pii.nthOrderBy(i); + T.assert(v instanceof sii.sqlite3_index_orderby) + .assert(v.pointer >= pii.$aOrderBy); + v.dispose(); + } + } + pii.dispose(); + return 0; + }catch(e){ + return VT.xError('xBestIndex',e); + } + } + } + }); + this.db.onclose.disposeAfter.push(tmplMod); + T.assert(!tmplMod.$xUpdate) + .assert(wasm.isPtr(tmplMod.$xRowid)) + .assert(wasm.isPtr(tmplMod.$xCreate)) + .assert(tmplMod.$xCreate === tmplMod.$xConnect, + "setup() must make these equivalent and "+ + "installMethods() must avoid re-compiling identical functions"); + tmplMod.$xCreate = wasm.ptr.null /* make tmplMod eponymous-only */; + let rc = capi.sqlite3_create_module( + this.db, "testvtab", tmplMod, 0 + ); + this.db.checkRc(rc); + const list = this.db.selectArrays( + "SELECT a,b FROM testvtab where a<9999 and b>1 order by a, b" + /* Query is shaped so that it will ensure that some constraints + end up in xBestIndex(). */ + ); + T.assert(10===list.length) + .assert(1000===list[0][0]) + .assert(2009===list[list.length-1][1]); + } + })/*custom vtab #1*/ + + //////////////////////////////////////////////////////////////////////// + .t({ + name: 'virtual table #2: non-eponymous w/ automated exception wrapping', + predicate: (sqlite3)=>!!sqlite3.capi.sqlite3_vtab || "Missing vtab support", + test: function(sqlite3){ + const VT = sqlite3.vtab; + const tmplCols = Object.assign(Object.create(null),{ + A: 0, B: 1 + }); + /** + The vtab demonstrated here is a JS-ification of + ext/misc/templatevtab.c. + */ + let throwOnCreate = 1 ? 0 : capi.SQLITE_CANTOPEN + /* ^^^ just for testing exception wrapping. Note that sqlite + always translates errors from a vtable to a generic + SQLITE_ERROR unless it's from xConnect()/xCreate() and that + callback sets an error string. */; + const vtabTrace = 1 + ? ()=>{} + : (methodName,...args)=>console.debug('sqlite3_module::'+methodName+'():',...args); + const modConfig = { + /* catchExceptions changes how the methods are wrapped */ + catchExceptions: true, + name: "vtab2test", + methods:{ + xCreate: function(pDb, pAux, argc, argv, ppVtab, pzErr){ + vtabTrace("xCreate",...arguments); + if(throwOnCreate){ + sqlite3.SQLite3Error.toss( + throwOnCreate, + "Throwing a test exception." + ); + } + const args = wasm.cArgvToJs(argc, argv); + vtabTrace("xCreate","argv:",args); + T.assert(args.length>=3); + const rc = capi.sqlite3_declare_vtab( + pDb, "CREATE TABLE ignored(a,b)" + ); + if(0===rc){ + const t = VT.xVtab.create(ppVtab); + T.assert(t === VT.xVtab.get(wasm.peekPtr(ppVtab))); + vtabTrace("xCreate",...arguments," ppVtab =",t.pointer); + } + return rc; + }, + xConnect: true, + xDestroy: function(pVtab){ + vtabTrace("xDestroy/xDisconnect",pVtab); + VT.xVtab.dispose(pVtab); + }, + xDisconnect: true, + xOpen: function(pVtab, ppCursor){ + const t = VT.xVtab.get(pVtab), + c = VT.xCursor.create(ppCursor); + T.assert(t instanceof capi.sqlite3_vtab) + .assert(c instanceof capi.sqlite3_vtab_cursor); + vtabTrace("xOpen",...arguments," cursor =",c.pointer); + c._rowId = 0; + }, + xClose: function(pCursor){ + vtabTrace("xClose",...arguments); + const c = VT.xCursor.unget(pCursor); + T.assert(c instanceof capi.sqlite3_vtab_cursor) + .assert(!VT.xCursor.get(pCursor)); + c.dispose(); + }, + xNext: function(pCursor){ + vtabTrace("xNext",...arguments); + const c = VT.xCursor.get(pCursor); + ++c._rowId; + }, + xColumn: function(pCursor, pCtx, iCol){ + vtabTrace("xColumn",...arguments); + const c = VT.xCursor.get(pCursor); + switch(iCol){ + case tmplCols.A: + capi.sqlite3_result_int(pCtx, 1000 + c._rowId); + break; + case tmplCols.B: + capi.sqlite3_result_int(pCtx, 2000 + c._rowId); + break; + default: sqlite3.SQLite3Error.toss("Invalid column id",iCol); + } + }, + xRowid: function(pCursor, ppRowid64){ + vtabTrace("xRowid",...arguments); + const c = VT.xCursor.get(pCursor); + VT.xRowid(ppRowid64, c._rowId); + }, + xEof: function(pCursor){ + vtabTrace("xEof",...arguments); + return VT.xCursor.get(pCursor)._rowId>=10; + }, + xFilter: function(pCursor, idxNum, idxCStr, + argc, argv/* [sqlite3_value* ...] */){ + vtabTrace("xFilter",...arguments); + const c = VT.xCursor.get(pCursor); + c._rowId = 0; + const list = capi.sqlite3_values_to_js(argc, argv); + T.assert(argc === list.length); + }, + xBestIndex: function(pVtab, pIdxInfo){ + vtabTrace("xBestIndex",...arguments); + //const t = VT.xVtab.get(pVtab); + const pii = VT.xIndexInfo(pIdxInfo); + pii.$estimatedRows = 10; + pii.$estimatedCost = 10.0; + pii.dispose(); + } + }/*methods*/ + }; + const tmplMod = VT.setupModule(modConfig); + T.assert(1===tmplMod.$iVersion); + this.db.onclose.disposeAfter.push(tmplMod); + this.db.checkRc(capi.sqlite3_create_module( + this.db.pointer, modConfig.name, tmplMod.pointer, 0 + )); + this.db.exec([ + "create virtual table testvtab2 using ", + modConfig.name, + "(arg1 blah, arg2 bloop)" + ]); + if(0){ + /* If we DROP TABLE then xDestroy() is called. If the + vtab is instead destroyed when the db is closed, + xDisconnect() is called. */ + this.db.onclose.disposeBefore.push(function(db){ + console.debug("Explicitly dropping testvtab2 via disposeBefore handler..."); + db.exec( + /** DROP TABLE is the only way to get xDestroy() to be called. */ + "DROP TABLE testvtab2" + ); + }); + } + let list = this.db.selectArrays( + "SELECT a,b FROM testvtab2 where a<9999 and b>1 order by a, b" + /* Query is shaped so that it will ensure that some + constraints end up in xBestIndex(). */ + ); + T.assert(10===list.length) + .assert(1000===list[0][0]) + .assert(2009===list[list.length-1][1]); + + list = this.db.selectArrays( + "SELECT a,b FROM testvtab2 where a<9999 and b>1 order by b, a limit 5" + ); + T.assert(5===list.length) + .assert(1000===list[0][0]) + .assert(2004===list[list.length-1][1]); + + // Call it as a table-valued function... + list = this.db.selectArrays([ + "SELECT a,b FROM ", modConfig.name, + " where a<9999 and b>1 order by b, a limit 1" + ]); + T.assert(1===list.length) + .assert(1000===list[0][0]) + .assert(2000===list[0][1]); + } + })/*custom vtab #2*/ + //////////////////////////////////////////////////////////////////////// + .t('Custom collation', function(sqlite3){ + let collationCounter = 0; + let myCmp = function(pArg,n1,p1,n2,p2){ + //int (*)(void*,int,const void*,int,const void*) + ++collationCounter; + const rc = wasm.exports.sqlite3_strnicmp(p1,p2,(n1this.db.checkRc(rc), + /SQLITE_UTF8 is the only supported encoding./); + /* + We need to ensure that replacing that collation function does + the right thing. We don't have a handle to the underlying WASM + pointer from here, so cannot verify (without digging through + internal state) that the old one gets uninstalled, but we can + verify that a new one properly replaces it. (That said, + console.warn() output has shown that the uninstallation does + happen.) + */ + collationCounter = 0; + myCmp = function(pArg,n1,p1,n2,p2){ + --collationCounter; + return 0; + }; + rc = capi.sqlite3_create_collation_v2(this.db, "MYCOLLATION", capi.SQLITE_UTF8, + 0, myCmp, 0); + this.db.checkRc(rc); + rc = this.db.selectValue("select 'hi' = 'HI' collate mycollation"); + T.assert(rc>0).assert(-1===collationCounter); + rc = this.db.selectValue("select 'a' = 'b' collate mycollation"); + T.assert(rc>0).assert(-2===collationCounter); + rc = capi.sqlite3_create_collation_v2(this.db, "MYCOLLATION", capi.SQLITE_UTF8, + 0, null, 0); + this.db.checkRc(rc); + rc = 0; + try { + this.db.selectValue("select 'a' = 'b' collate mycollation"); + }catch(e){ + /* Why is e.resultCode not automatically an extended result + code? The DB() class enables those automatically. */ + rc = sqlite3.capi.sqlite3_extended_errcode(this.db); + } + T.assert(capi.SQLITE_ERROR_MISSING_COLLSEQ === rc); + })/*custom collation*/ + + //////////////////////////////////////////////////////////////////////// + .t('Close db', function(){ + T.assert(this.db).assert(wasm.isPtr(this.db.pointer)); + //wasm.sqlite3__wasm_db_reset(this.db); // will leak virtual tables! + this.db.close(); + T.assert(!this.db.pointer); + }) + ;/* end of oo1 checks */ + + //////////////////////////////////////////////////////////////////////// + T.g('kvvfs') + .t({ + name: 'kvvfs is disabled in worker', + predicate: ()=>(isWorker() || "test is only valid in a Worker"), + test: function(sqlite3){ + T.assert( + !capi.sqlite3_vfs_find('kvvfs'), + "Expecting kvvfs to be unregistered." + ); + } + }) + .t({ + name: 'kvvfs in main thread', + predicate: ()=>(isUIThread() + || "local/sessionStorage are unavailable in a Worker"), + test: function(sqlite3){ + const filename = this.kvvfsDbFile = 'session'; + const pVfs = capi.sqlite3_vfs_find('kvvfs'); + T.assert(looksLikePtr(pVfs)); + const JDb = this.JDb = sqlite3.oo1.JsStorageDb; + const unlink = this.kvvfsUnlink = ()=>JDb.clearStorage(this.kvvfsDbFile); + unlink(); + let db = new JDb(filename); + try { + db.exec([ + 'create table kvvfs(a);', + 'insert into kvvfs(a) values(1),(2),(3)' + ]); + T.assert(3 === db.selectValue('select count(*) from kvvfs')); + db.close(); + db = new JDb(filename); + db.exec('insert into kvvfs(a) values(4),(5),(6)'); + T.assert(6 === db.selectValue('select count(*) from kvvfs')); + }finally{ + db.close(); + } + } + }/*kvvfs sanity checks*/) +//#if enable-see + .t({ + name: 'kvvfs with SEE encryption', + predicate: ()=>(isUIThread() + || "Only available in main thread."), + test: function(sqlite3){ + this.kvvfsUnlink(); + let initDb = true; + const tryKey = function(keyKey, key, expectCount){ + let db; + //console.debug('tryKey()',arguments); + const ctoropt = { + filename: this.kvvfsDbFile + //vfs: 'kvvfs' + //,flags: 'ct' + }; + try { + if (initDb) { + initDb = false; + db = new this.JDb({ + ...ctoropt, + [keyKey]: key + }); + db.exec([ + "drop table if exists t;", + "create table t(a);" + ]); + db.close(); + // Ensure that it's actually encrypted... + let err; + try { + db = new this.JDb(ctoropt); + T.assert(db, 'db opened') /* opening is fine, but... */; + db.exec("select 1 from sqlite_schema"); + console.warn("(should not be reached) sessionStorage =", sessionStorage); + } catch (e) { + err = e; + } finally { + db.close() + } + T.assert(err, "Expecting an exception") + .assert(sqlite3.capi.SQLITE_NOTADB == err.resultCode, + "Expecting NOTADB"); + }/*initDb*/ + //console.debug('tryKey()',arguments); + db = new sqlite3.oo1.DB({ + ...ctoropt, + vfs: 'kvvfs', + [keyKey]: key + }); + db.exec("insert into t(a) values (1),(2)"); + T.assert(expectCount === db.selectValue('select sum(a) from t')); + } finally { + if (db) db.close(); + } + }.bind(this); + const hexFoo = new Uint8Array([0x66,0x6f,0x6f]/*=="foo"*/); + tryKey('textkey', 'foo', 3); + T.assert( !initDb ); + tryKey('textkey', 'foo', 6); + this.kvvfsUnlink(); + initDb = true; + tryKey('key', 'foo', 3); + T.assert( !initDb ); + tryKey('key', hexFoo, 6); + this.kvvfsUnlink(); + initDb = true; + tryKey('hexkey', hexFoo, 3); + T.assert( !initDb ); + tryKey('hexkey', hexFoo, 6); + this.kvvfsUnlink(); + } + })/*kvvfs with SEE*/ +//#endif enable-see + ;/* end kvvfs tests */ + + //////////////////////////////////////////////////////////////////////// + T.g('Hook APIs') + .t({ + name: "sqlite3_commit/rollback/update_hook()", + predicate: ()=>wasm.bigIntEnabled || "Update hook requires int64", + test: function(sqlite3){ + let countCommit = 0, countRollback = 0;; + const db = new sqlite3.oo1.DB(':memory:',1 ? 'c' : 'ct'); + let rc = capi.sqlite3_commit_hook(db, (p)=>{ + //console.debug("commit hook",arguments); + ++countCommit; + return (17 == p) ? 0 : capi.SQLITE_ERROR; + }, 17); + T.assert( wasm.ptr.null === rc ); + + // Commit hook... + T.assert( 0!=capi.sqlite3_get_autocommit(db) ); + db.exec("BEGIN; SELECT 1; COMMIT"); + T.assert(0 === countCommit, + "No-op transactions (mostly) do not trigger commit hook."); + db.exec("BEGIN EXCLUSIVE; SELECT 1; COMMIT"); + T.assert(1 === countCommit, + "But EXCLUSIVE transactions do."); + db.transaction((d)=>{ + T.assert( 0==capi.sqlite3_get_autocommit(db) ); + d.exec("create table t(a)"); + }); + T.assert(2 === countCommit); + T.assert(17 == capi.sqlite3_commit_hook(db, 0, 0)); + + // Rollback hook: + rc = capi.sqlite3_rollback_hook(db, (p)=>{ + ++countRollback; + T.assert( 21 == p ); + }, 21); + T.assert( wasm.ptr.null===rc ); + T.mustThrowMatching(()=>{ + db.transaction('drop table t',()=>{}) + }, (e)=>{ + return (capi.SQLITE_MISUSE === e.resultCode) + && ( e.message.indexOf('Invalid argument') > 0 ); + }); + T.assert(0 === countRollback, "Transaction was not started."); + T.mustThrowMatching(()=>{ + db.transaction('immediate', ()=>{ + sqlite3.SQLite3Error.toss(capi.SQLITE_FULL,'testing rollback hook'); + }); + }, (e)=>{ + //console.error("transaction error:",e); + return capi.SQLITE_FULL === e.resultCode + }); + T.assert(1 === countRollback); + T.assert(21 == capi.sqlite3_rollback_hook(db, 0, 0)); + + // Update hook... + const countUpdate = Object.create(null); + capi.sqlite3_update_hook(db, (p,op,dbName,tbl,rowid)=>{ + T.assert('main' === dbName.toLowerCase()) + .assert('t' === tbl.toLowerCase()) + .assert(33==p) + .assert('bigint' === typeof rowid); + switch(op){ + case capi.SQLITE_INSERT: + case capi.SQLITE_UPDATE: + case capi.SQLITE_DELETE: + countUpdate[op] = (countUpdate[op]||0) + 1; + break; + default: toss("Unexpected hook operator:",op); + } + }, 33); + db.transaction((d)=>{ + db.exec([ + "insert into t(a) values(1);", + "update t set a=2;", + "update t set a=3;", + "delete from t where a=3;" + // update hook is not called for an unqualified DELETE + ]); + }); + T.assert(1 === countRollback) + .assert(2 === countCommit) + .assert(1 === countUpdate[capi.SQLITE_INSERT]) + .assert(2 === countUpdate[capi.SQLITE_UPDATE]) + .assert(1 === countUpdate[capi.SQLITE_DELETE]); + //wasm.xWrap.FuncPtrAdapter.debugFuncInstall = true; + T.assert(33 == capi.sqlite3_update_hook(db, 0, 0)); + //wasm.xWrap.FuncPtrAdapter.debugFuncInstall = false; + db.close(); + } + })/* commit/rollback/update hooks */ + .t({ + name: "sqlite3_preupdate_hook()", + predicate: ()=>capi.sqlite3_preupdate_hook || "Missing pre-update hook API", + test: function(sqlite3){ + const db = new sqlite3.oo1.DB(':memory:', 1 ? 'c' : 'ct'); + const countHook = Object.create(null); + let rc = capi.sqlite3_preupdate_hook( + db, function(p, pDb, op, zDb, zTbl, iKey1, iKey2){ + T.assert(9 == p) + .assert(db.pointer === pDb) + .assert(1 === capi.sqlite3_preupdate_count(pDb)) + .assert( 0 > capi.sqlite3_preupdate_blobwrite(pDb) ); + countHook[op] = (countHook[op]||0) + 1; + switch(op){ + case capi.SQLITE_INSERT: + case capi.SQLITE_UPDATE: + T.assert('number' === typeof capi.sqlite3_preupdate_new_js(pDb, 0)); + break; + case capi.SQLITE_DELETE: + T.assert('number' === typeof capi.sqlite3_preupdate_old_js(pDb, 0)); + break; + default: toss("Unexpected hook operator:",op); + } + }, + 9 + ); + T.assert( 0==rc ); + db.transaction((d)=>{ + d.exec([ + "create table t(a);", + "insert into t(a) values(1);", + "update t set a=2;", + "update t set a=3;", + "delete from t where a=3" + ]); + }); + T.assert(1 === countHook[capi.SQLITE_INSERT]) + .assert(2 === countHook[capi.SQLITE_UPDATE]) + .assert(1 === countHook[capi.SQLITE_DELETE]); + //wasm.xWrap.FuncPtrAdapter.debugFuncInstall = true; + T.assert( !!capi.sqlite3_preupdate_hook(db, 0, 0) ); + //wasm.xWrap.FuncPtrAdapter.debugFuncInstall = false; + T.assert( !capi.sqlite3_preupdate_hook(db, 0, 0) ); + db.close(); + } + })/*pre-update hooks*/ + ;/*end hook API tests*/ + + //////////////////////////////////////////////////////////////////////// + T.g('Auto-extension API') + .t({ + name: "Auto-extension sanity checks.", + test: function(sqlite3){ + let counter = 0; + const fp = wasm.installFunction('i(ppp)', function(pDb,pzErr,pApi){ + ++counter; + return 0; + }); + (new sqlite3.oo1.DB()).close(); + T.assert( 0===counter ); + capi.sqlite3_auto_extension(fp); + (new sqlite3.oo1.DB()).close(); + T.assert( 1===counter ); + (new sqlite3.oo1.DB()).close(); + T.assert( 2===counter ); + capi.sqlite3_cancel_auto_extension(fp); + wasm.uninstallFunction(fp); + (new sqlite3.oo1.DB()).close(); + T.assert( 2===counter ); + } + }); + + //////////////////////////////////////////////////////////////////////// + T.g('Session API') + .t({ + name: 'Session API sanity checks', + predicate: ()=>!!capi.sqlite3changegroup_add || "Missing session API", + test: function(sqlite3){ + //warn("The session API tests could use some expansion."); + const db1 = new sqlite3.oo1.DB(), db2 = new sqlite3.oo1.DB(); + const sqlInit = [ + "create table t(rowid INTEGER PRIMARY KEY,a,b); ", + "insert into t(rowid,a,b) values", + "(1,'a1','b1'),", + "(2,'a2','b2'),", + "(3,'a3','b3');" + ].join(''); + db1.exec(sqlInit); + db2.exec(sqlInit); + T.assert(3 === db1.selectValue("select count(*) from t")) + .assert('b3' === db1.selectValue('select b from t where rowid=3')); + const stackPtr = wasm.pstack.pointer; + try{ + let ppOut = wasm.pstack.allocPtr(); + let rc = capi.sqlite3session_create(db1, "main", ppOut); + T.assert(0===rc); + let pSession = wasm.peekPtr(ppOut); + T.assert(pSession && wasm.isPtr(pSession)); + capi.sqlite3session_table_filter(pSession, (pCtx, tbl)=>{ + T.assert('t' === tbl).assert( 99 === pCtx ); + return 1; + }, 99); + db1.exec([ + "update t set b='bTwo' where rowid=2;", + "update t set a='aThree' where rowid=3;", + "delete from t where rowid=1;", + "insert into t(rowid,a,b) values(4,'a4','b4')" + ]); + T.assert('bTwo' === db1.selectValue("select b from t where rowid=2")) + .assert(undefined === db1.selectValue('select a from t where rowid=1')) + .assert('b4' === db1.selectValue('select b from t where rowid=4')) + .assert(3 === db1.selectValue('select count(*) from t')); + + const testSessionEnable = + false /* it's not yet clear whether these test failures are + broken tests or broken bindings. */; + if(testSessionEnable){ + rc = capi.sqlite3session_enable(pSession, 0); + T.assert( 0 === rc ) + .assert( 0 === capi.sqlite3session_enable(pSession, -1) ); + db1.exec("delete from t where rowid=2;"); + rc = capi.sqlite3session_enable(pSession, 1); + T.assert( rc > 0 ) + .assert( capi.sqlite3session_enable(pSession, -1) > 0 ) + .assert(undefined === db1.selectValue('select a from t where rowid=2')); + }else{ + //warn("sqlite3session_enable() tests are currently disabled."); + } + let db1Count = db1.selectValue("select count(*) from t"); + T.assert( db1Count === (testSessionEnable ? 2 : 3) ); + + /* Capture changeset and destroy session. */ + let pnChanges = wasm.pstack.alloc('i32'), + ppChanges = wasm.pstack.allocPtr(); + rc = capi.sqlite3session_changeset(pSession, pnChanges, ppChanges); + T.assert( 0 === rc ); + capi.sqlite3session_delete(pSession); + pSession = 0; + const pChanges = wasm.peekPtr(ppChanges), + nChanges = wasm.peek32(pnChanges); + T.assert( pChanges && wasm.isPtr( pChanges ) ) + .assert( nChanges > 0 ); + + /* Revert db1 via an inverted changeset, but keep pChanges + and nChanges for application to db2. */ + rc = capi.sqlite3changeset_invert( nChanges, pChanges, pnChanges, ppChanges ); + T.assert( 0 === rc ); + rc = capi.sqlite3changeset_apply( + db1, wasm.peek32(pnChanges), wasm.peekPtr(ppChanges), 0, (pCtx, eConflict, pIter)=>{ + return 1; + }, 0 + ); + T.assert( 0 === rc ); + wasm.dealloc( wasm.peekPtr(ppChanges) ); + pnChanges = ppChanges = 0; + T.assert('b2' === db1.selectValue("select b from t where rowid=2")) + .assert('a1' === db1.selectValue('select a from t where rowid=1')) + .assert(undefined === db1.selectValue('select b from t where rowid=4')); + db1Count = db1.selectValue("select count(*) from t"); + T.assert(3 === db1Count); + + /* Apply pre-reverted changeset (pChanges, nChanges) to + db2... */ + rc = capi.sqlite3changeset_apply( + db2, nChanges, pChanges, 0, (pCtx, eConflict, pIter)=>{ + return pCtx ? 1 : 0 + }, 1 + ); + wasm.dealloc( pChanges ); + T.assert( 0 === rc ) + .assert( 'b4' === db2.selectValue('select b from t where rowid=4') ) + .assert( 'aThree' === db2.selectValue('select a from t where rowid=3') ) + .assert( undefined === db2.selectValue('select b from t where rowid=1') ); + if(testSessionEnable){ + T.assert( (undefined === db2.selectValue('select b from t where rowid=2')), + "But... the session was disabled when rowid=2 was deleted?" ); + log("rowids from db2.t:",db2.selectValues('select rowid from t order by rowid')); + T.assert( 3 === db2.selectValue('select count(*) from t') ); + }else{ + T.assert( 'bTwo' === db2.selectValue('select b from t where rowid=2') ) + .assert( 3 === db2.selectValue('select count(*) from t') ); + } + }finally{ + wasm.pstack.restore(stackPtr); + db1.close(); + db2.close(); + } + } + })/*session API sanity tests*/ + ;/*end of session API group*/; + + //////////////////////////////////////////////////////////////////////// + T.g('OPFS: Origin-Private File System', + (sqlite3)=>(sqlite3.capi.sqlite3_vfs_find("opfs") + || 'requires "opfs" VFS')) + .t({ + name: 'OPFS db sanity checks', + test: async function(sqlite3){ + T.assert(capi.sqlite3_vfs_find('opfs')); + const opfs = sqlite3.opfs; + const filename = this.opfsDbFile = '/dir/sqlite3-tester1.db'; + const fileUri = 'file://'+filename+'?delete-before-open=1'; + const initSql = [ + 'create table p(a);', + 'insert into p(a) values(1),(2),(3)' + ]; + let db = new sqlite3.oo1.OpfsDb(fileUri); + try { + db.exec(initSql); + T.assert(3 === db.selectValue('select count(*) from p')); + db.close(); + db = new sqlite3.oo1.OpfsDb(filename); + db.exec('insert into p(a) values(4),(5),(6)'); + T.assert(6 === db.selectValue('select count(*) from p')); + this.opfsDbExport = capi.sqlite3_js_db_export(db); + T.assert(this.opfsDbExport instanceof Uint8Array) + .assert(this.opfsDbExport.byteLength>0 + && 0===this.opfsDbExport.byteLength % 512); + }finally{ + db.close(); + } + T.assert(await opfs.entryExists(filename)); + try { + db = new sqlite3.oo1.OpfsDb(fileUri); + db.exec(initSql) /* will throw if delete-before-open did not work */; + T.assert(3 === db.selectValue('select count(*) from p')); + }finally{ + if(db) db.close(); + } + } + }/*OPFS db sanity checks*/) + .t({ + name: 'OPFS import', + test: async function(sqlite3){ + let db; + const filename = this.opfsDbFile; + try { + const exp = this.opfsDbExport; + delete this.opfsDbExport; + this.opfsImportSize = await sqlite3.oo1.OpfsDb.importDb(filename, exp); + db = new sqlite3.oo1.OpfsDb(this.opfsDbFile); + T.assert(6 === db.selectValue('select count(*) from p')). + assert( this.opfsImportSize == exp.byteLength ); + db.close(); + this.opfsUnlink = + (fn=filename)=>sqlite3.util.sqlite3__wasm_vfs_unlink("opfs",fn); + this.opfsUnlink(filename); + T.assert(!(await sqlite3.opfs.entryExists(filename))); + // Try again with a function as an input source: + let cursor = 0; + const blockSize = 512, end = exp.byteLength; + const reader = async function(){ + if(cursor >= exp.byteLength){ + return undefined; + } + const rv = exp.subarray(cursor, cursor+blockSize>end ? end : cursor+blockSize); + cursor += blockSize; + return rv; + }; + this.opfsImportSize = await sqlite3.oo1.OpfsDb.importDb(filename, reader); + db = new sqlite3.oo1.OpfsDb(this.opfsDbFile); + T.assert(6 === db.selectValue('select count(*) from p')). + assert( this.opfsImportSize == exp.byteLength ); + }finally{ + if(db) db.close(); + } + } + }/*OPFS export/import*/) + .t({ + name: '(Internal-use) OPFS utility APIs', + test: async function(sqlite3){ + const filename = this.opfsDbFile; + const unlink = this.opfsUnlink; + T.assert(filename && !!unlink); + delete this.opfsDbFile; + delete this.opfsUnlink; + /************************************************************** + ATTENTION CLIENT-SIDE USERS: sqlite3.opfs is NOT intended + for client-side use. It is only for this project's own + internal use. Its APIs are subject to change or removal at + any time. + ***************************************************************/ + const opfs = sqlite3.opfs; + const fSize = this.opfsImportSize; + delete this.opfsImportSize; + let sh; + try{ + T.assert(await opfs.entryExists(filename)); + const [dirHandle, filenamePart] = await opfs.getDirForFilename(filename, false); + const fh = await dirHandle.getFileHandle(filenamePart); + sh = await fh.createSyncAccessHandle(); + T.assert(fSize === await sh.getSize()); + await sh.close(); + sh = undefined; + unlink(); + T.assert(!(await opfs.entryExists(filename))); + }finally{ + if(sh) await sh.close(); + unlink(); + } + + // Some sanity checks of the opfs utility functions... + const testDir = '/sqlite3-opfs-'+opfs.randomFilename(12); + const aDir = testDir+'/test/dir'; + T.assert(await opfs.mkdir(aDir), "mkdir failed") + .assert(await opfs.mkdir(aDir), "mkdir must pass if the dir exists") + .assert(!(await opfs.unlink(testDir+'/test')), "delete 1 should have failed (dir not empty)") + .assert((await opfs.unlink(testDir+'/test/dir')), "delete 2 failed") + .assert(!(await opfs.unlink(testDir+'/test/dir')), + "delete 2b should have failed (dir already deleted)") + .assert((await opfs.unlink(testDir, true)), "delete 3 failed") + .assert(!(await opfs.entryExists(testDir)), + "entryExists(",testDir,") should have failed"); + } + }/*OPFS util sanity checks*/) +//#if enable-see + .t({ + name: 'OPFS with SEE encryption', + test: function(sqlite3){ + const dbFile = 'file:///sqlite3-see.edb'; + const dbCtor = sqlite3.oo1.OpfsDb; + const hexFoo = new Uint8Array([0x66,0x6f,0x6f]/*=="foo"*/); + let initDb = true; + const tryKey = function(keyKey, key, expectCount){ + let db; + //console.debug('tryKey()',arguments); + const ctoropt = { + filename: dbFile, + flags: 'c' + }; + try { + if (initDb) { + initDb = false; + const opt = { + ...ctoropt, + [keyKey]: key + }; + opt.filename += '?delete-before-open=1'; + db = new dbCtor(opt); + db.exec([ + "drop table if exists t;", + "create table t(a);" + ]); + db.close(); + // Ensure that it's actually encrypted... + let err; + try { + db = new dbCtor(ctoropt); + T.assert(db, 'db opened') /* opening is fine, but... */; + const rv = db.exec({ + sql:"select count(*) from sqlite_schema", + returnValue: 'resultRows' + }); + console.warn("(should not be reached) rv =",rv); + } catch (e) { + err = e; + } finally { + db.close() + } + T.assert(err, "Expecting an exception") + .assert(sqlite3.capi.SQLITE_NOTADB == err.resultCode, + "Expecting NOTADB"); + }/*initDb*/ + db = new dbCtor({ + ...ctoropt, + [keyKey]: key + }); + db.exec("insert into t(a) values (1),(2)"); + T.assert(expectCount === db.selectValue('select sum(a) from t')); + } finally { + if (db) db.close(); + } + }; + tryKey('textkey', 'foo', 3); + T.assert( !initDb ); + tryKey('textkey', 'foo', 6); + initDb = true; + tryKey('key', 'foo', 3); + T.assert( !initDb ); + tryKey('key', hexFoo, 6); + initDb = true; + tryKey('hexkey', hexFoo, 3); + T.assert( !initDb ); + tryKey('hexkey', hexFoo, 6); + } + })/*OPFS with SEE*/ +//#endif enable-see + ;/* end OPFS tests */ + + //////////////////////////////////////////////////////////////////////// + T.g('OPFS SyncAccessHandle Pool VFS', + (sqlite3)=>(hasOpfs() || "requires OPFS APIs")) + .t({ + name: 'SAH sanity checks', + test: async function(sqlite3){ + T.assert(!sqlite3.capi.sqlite3_vfs_find(sahPoolConfig.name)) + .assert(sqlite3.capi.sqlite3_js_vfs_list().indexOf(sahPoolConfig.name) < 0) + const inst = sqlite3.installOpfsSAHPoolVfs, + catcher = (e)=>{ + error("Cannot load SAH pool VFS.", + "This might not be a problem,", + "depending on the environment."); + return false; + }; + let u1, u2; + // Ensure that two immediately-consecutive installations + // resolve to the same Promise instead of triggering + // a locking error. + const P1 = inst(sahPoolConfig).then(u=>u1 = u).catch(catcher), + P2 = inst(sahPoolConfig).then(u=>u2 = u).catch(catcher); + await Promise.all([P1, P2]); + if(!(await P1)) return; + T.assert(u1 === u2) + .assert(sahPoolConfig.name === u1.vfsName) + .assert(sqlite3.capi.sqlite3_vfs_find(sahPoolConfig.name)) + .assert(u1.getCapacity() >= sahPoolConfig.initialCapacity + /* If a test fails before we get to nuke the VFS, we + can have more than the initial capacity on the next + run. */) + .assert(u1.getCapacity() + 2 === (await u2.addCapacity(2))) + .assert(2 === (await u2.reduceCapacity(2))) + .assert(sqlite3.capi.sqlite3_js_vfs_list().indexOf(sahPoolConfig.name) >= 0); + + T.assert(0 === u1.getFileCount()); + const dbName = '/foo.db'; + //wasm.xWrap.debug = true; + let db = new u1.OpfsSAHPoolDb(dbName); + T.assert(db instanceof sqlite3.oo1.DB) + .assert(1 === u1.getFileCount()); + db.exec([ + 'pragma locking_mode=exclusive;', + 'pragma journal_mode=wal;' + /* WAL mode only works in this VFS if locking_mode=exclusive + is invoked prior to the first db access, as this build + does not have the shared-memory APIs needed for WAL without + exclusive-mode locking. See: + + https://sqlite.org/wal.html#use_of_wal_without_shared_memory + + Note that WAL mode here DOES NOT add any concurrency capabilities + to this VFS, but it MAY provide slightly improved performance + over the other journaling modes. + */, + 'create table t(a);', + 'insert into t(a) values(1),(2),(3)' + ]); + T.assert(2 === u1.getFileCount() /* one is the journal file */) + .assert(3 === db.selectValue('select count(*) from t')) + .assert( + 'wal'===db.selectValue('pragma journal_mode') + || wasm.compileOptionUsed('OMIT_WAL') + ); + db.close(); + T.assert(1 === u1.getFileCount()); + db = new u2.OpfsSAHPoolDb(dbName); + T.assert(1 === u1.getFileCount()) + .mustThrowMatching( + ()=>u1.pauseVfs(), + (err)=>{ + return capi.SQLITE_MISUSE===err.resultCode + && /^SQLITE_MISUSE: Cannot pause VFS /.test(err.message); + }, + "Cannot pause VFS with opened db." + ); + db.close(); + T.assert( u2===u2.pauseVfs() ) + .assert( u2.isPaused() ) + .assert( !capi.sqlite3_vfs_find(u2.vfsName) ) + .mustThrowMatching(()=>new u2.OpfsSAHPoolDb(dbName), + /.+no such vfs: .+/, + "VFS is not available") + .assert( u2===await u2.unpauseVfs() ) + .assert( u2===await u1.unpauseVfs(), "unpause is a no-op if the VFS is not paused" ) + .assert( !!capi.sqlite3_vfs_find(u2.vfsName) ); + const fileNames = u1.getFileNames(); + T.assert(1 === fileNames.length) + .assert(dbName === fileNames[0]) + .assert(1 === u1.getFileCount()) + + if(1){ // test exportFile() and importDb() + const dbytes = u1.exportFile(dbName); + T.assert(dbytes.length >= 4096); + const dbName2 = '/exported.db'; + let nWrote = u1.importDb(dbName2, dbytes); + T.assert( 2 == u1.getFileCount() ) + .assert( dbytes.byteLength == nWrote ); + let db2 = new u1.OpfsSAHPoolDb(dbName2); + T.assert(db2 instanceof sqlite3.oo1.DB) + .assert('wal' !== db2.selectValue("pragma journal_mode") + /* importDb() unsets the WAL-mode header for + historical reasons. Because clients must + explicitly enable pragma locking_mode=exclusive + before using WAL, that behavior is retained. */) + .assert(3 === db2.selectValue('select count(*) from t')); + db2.close(); + T.assert(true === u1.unlink(dbName2)) + .assert(false === u1.unlink(dbName2)) + .assert(1 === u1.getFileCount()) + .assert(1 === u1.getFileNames().length); + // Try again with a function as an input source: + let cursor = 0; + const blockSize = 1024, end = dbytes.byteLength; + const reader = async function(){ + if(cursor >= dbytes.byteLength){ + return undefined; + } + const rv = dbytes.subarray(cursor, cursor+blockSize>end ? end : cursor+blockSize); + cursor += blockSize; + return rv; + }; + nWrote = await u1.importDb(dbName2, reader); + T.assert( 2 == u1.getFileCount() ); + db2 = new u1.OpfsSAHPoolDb(dbName2); + T.assert(db2 instanceof sqlite3.oo1.DB) + .assert(3 === db2.selectValue('select count(*) from t')); + db2.close(); + T.assert(true === u1.unlink(dbName2)) + .assert(dbytes.byteLength == nWrote); + } + + T.assert(true === u1.unlink(dbName)) + .assert(false === u1.unlink(dbName)) + .assert(0 === u1.getFileCount()) + .assert(0 === u1.getFileNames().length); + + // Demonstrate that two SAH pools can coexist so long as + // they have different names. + const conf2 = JSON.parse(JSON.stringify(sahPoolConfig)); + conf2.name += '-test2'; + const POther = await inst(conf2); + //log("Installed second SAH instance as",conf2.name); + T.assert(0 === POther.getFileCount()) + .assert(true === await POther.removeVfs()); + + if(0){ + /* Enable this block to inspect vfs's contents via the dev + console or OPFS Explorer browser extension. The + following bits will remove them. */ + return; + } + T.assert(true === await u2.removeVfs()) + .assert(false === await u1.removeVfs()) + .assert(!sqlite3.capi.sqlite3_vfs_find(sahPoolConfig.name)); + + let cErr, u3; + conf2.$testThrowPhase2 = new Error("Testing throwing during init."); + conf2.name = sahPoolConfig.name+'-err'; + const P3 = await inst(conf2).then(u=>u3 = u).catch((e)=>cErr=e); + T.assert(P3 === conf2.$testThrowPhase2) + .assert(cErr === P3) + .assert(undefined === u3) + .assert(!sqlite3.capi.sqlite3_vfs_find(conf2.name)); + delete conf2.$testThrowPhase2; + T.assert(cErr === await inst(conf2).catch(e=>e), + "Init result is cached even if it failed"); + + /* Ensure that the forceReinitIfPreviouslyFailed fallback bypasses + the VFS init cache... */ + cErr = u3 = undefined; + conf2.forceReinitIfPreviouslyFailed = true; + conf2.verbosity = 3; + const P3b = await inst(conf2).then(u=>u3 = u).catch((e)=>cErr=e); + T.assert(undefined === cErr) + .assert(P3b === u3) + .assert(P3b === await inst(conf2)) + .assert(true === await u3.removeVfs()) + .assert(false === await P3b.removeVfs()); + } + }/*OPFS SAH Pool sanity checks*/) +//#if enable-see + .t({ + name: 'OPFS SAHPool with SEE encryption', + test: async function(sqlite3){ + const inst = sqlite3.installOpfsSAHPoolVfs, + catcher = (e)=>{ + error("Cannot load SAH pool VFS.", + "This might not be a problem,", + "depending on the environment."); + return false; + }; + const poolConfig = { + name: 'opfs-sahpool-see', + clearOnInit: true, + initialCapacity: 6 + } + let poolUtil; + const P1 = await inst(poolConfig).then(u=>poolUtil = u).catch(catcher); + const dbFile = '/sqlite3-see.edb'; + const dbCtor = poolUtil.OpfsSAHPoolDb; + const hexFoo = new Uint8Array([0x66,0x6f,0x6f]/*=="foo"*/); + let initDb = true; + const tryKey = function(keyKey, key, expectCount){ + let db; + //console.debug('tryKey()',arguments); + const ctoropt = { + filename: dbFile, + flags: 'c' + }; + try { + if (initDb) { + initDb = false; + poolUtil.unlink(dbFile); + db = new dbCtor({ + ...ctoropt, + [keyKey]: key + }); + db.exec([ + "drop table if exists t;", + "create table t(a);" + ]); + db.close(); + // Ensure that it's actually encrypted... + let err; + try { + db = new dbCtor(ctoropt); + T.assert(db, 'db opened') /* opening is fine, but... */; + const rv = db.exec({ + sql:"select count(*) from sqlite_schema", + returnValue: 'resultRows' + }); + console.warn("(should not be reached) rv =",rv); + } catch (e) { + err = e; + } finally { + db.close() + } + T.assert(err, "Expecting an exception") + .assert(sqlite3.capi.SQLITE_NOTADB == err.resultCode, + "Expecting NOTADB"); + }/*initDb*/ + db = new dbCtor({ + ...ctoropt, + [keyKey]: key + }); + db.exec("insert into t(a) values (1),(2)"); + T.assert(expectCount === db.selectValue('select sum(a) from t')); + } finally { + if (db) db.close(); + } + }; + tryKey('textkey', 'foo', 3); + T.assert( !initDb ); + tryKey('textkey', 'foo', 6); + initDb = true; + tryKey('key', 'foo', 3); + T.assert( !initDb ); + tryKey('key', hexFoo, 6); + initDb = true; + tryKey('hexkey', hexFoo, 3); + T.assert( !initDb ); + tryKey('hexkey', hexFoo, 6); + poolUtil.removeVfs(); + } + })/*opfs-sahpool with SEE*/ +//#endif enable-see + ; + + //////////////////////////////////////////////////////////////////////// + T.g('Misc. APIs') + .t('bind_parameter_...', function(sqlite3){ + const db = new sqlite3.oo1.DB(); + db.exec("create table t(a)"); + const stmt = db.prepare("insert into t(a) values($a)"); + T.assert( 1===capi.sqlite3_bind_parameter_count(stmt) ) + .assert( 1===stmt.parameterCount ) + .assert( 1===capi.sqlite3_bind_parameter_index(stmt, "$a") ) + .assert( 0===capi.sqlite3_bind_parameter_index(stmt, ":a") ) + .assert( 1===stmt.getParamIndex("$a") ) + .assert( 0===stmt.getParamIndex(":a") ) + .assert( "$a"===capi.sqlite3_bind_parameter_name(stmt, 1) ) + .assert( null===capi.sqlite3_bind_parameter_name(stmt, 0) ) + .assert( "$a"===stmt.getParamName(1) ) + .assert( null===stmt.getParamName(0) ); + stmt.finalize(); + db.close(); + }) + + /** + Ensure that certain Stmt members throw when called + via DB.exec(). + */ + .t('locked-by-exec() APIs', function(sqlite3){ + const db = new sqlite3.oo1.DB(); + db.exec("create table t(a);insert into t(a) values(1);"); + let checkCount = 0; + const checkOp = function(op){ + ++checkCount; + T.mustThrowMatching(() => { + db.exec({ + sql: "select ?1", + bind: op, + callback: (row, stmt) => { + switch (row[0]) { + case 'bind': stmt.bind(1); break; + case 'finalize': + case 'clearBindings': + case 'reset': + case 'step': stmt[op](); break; + } + } + }); + }, /^Operation is illegal when statement is locked.*/) + }; + try{ + checkOp('bind'); + checkOp('finalize'); + checkOp('clearBindings'); + checkOp('reset'); + checkOp('step'); + T.assert(5===checkCount); + }finally{ + db.close(); + } + }) + + //////////////////////////////////////////////////////////////////// + .t("Misc. stmt_...", function(sqlite3){ + const db = new sqlite3.oo1.DB(); + db.exec("create table t(a doggiebiscuits); insert into t(a) values(123)"); + const stmt = db.prepare("select a, a+1 from t"); + T.assert( stmt.isReadOnly() ) + .assert( 0===capi.sqlite3_stmt_isexplain(stmt) ) + .assert( 0===capi.sqlite3_stmt_explain(stmt, 1) ) + .assert( 0!==capi.sqlite3_stmt_isexplain(stmt) ) + .assert( 0===capi.sqlite3_stmt_explain(stmt, 2) ) + .assert( 0!==capi.sqlite3_stmt_isexplain(stmt) ) + .assert( 0===capi.sqlite3_stmt_explain(stmt, 0) ) + .assert( 0===capi.sqlite3_stmt_isexplain(stmt) ); + let n = 0; + while( capi.SQLITE_ROW === capi.sqlite3_step(stmt) ){ + ++n; + T.assert( 0!==capi.sqlite3_stmt_explain(stmt, 1), + "Because stmt is busy" ) + .assert( capi.sqlite3_stmt_busy(stmt) ) + .assert( stmt.isBusy() ) + .assert( 0!==capi.sqlite3_stmt_readonly(stmt) ) + .assert( true===stmt.isReadOnly() ); + const sv = capi.sqlite3_column_value(stmt, 0); + T.assert( 123===capi.sqlite3_value_int(sv) ) + .assert( "doggiebiscuits"===capi.sqlite3_column_decltype(stmt,0) ) + .assert( null===capi.sqlite3_column_decltype(stmt,1) ); + } + T.assert( 1===n ) + .assert( 0===capi.sqlite3_stmt_busy(stmt) ) + .assert( !stmt.isBusy() ); + + if( wasm.exports.sqlite3_column_origin_name ){ + log("Column metadata APIs enabled"); + T.assert( "t" === capi.sqlite3_column_table_name(stmt, 0)) + .assert("a" === capi.sqlite3_column_origin_name(stmt, 0)) + .assert("main" === capi.sqlite3_column_database_name(stmt, 0)) + }else{ + log("Column metadata APIs not enabled"); + } // column metadata APIs + + stmt.finalize(); + db.close(); + }) + + //////////////////////////////////////////////////////////////////// + .t("interrupt", function(sqlite3){ + const db = new sqlite3.oo1.DB(); + T.assert( 0===capi.sqlite3_is_interrupted(db) ); + capi.sqlite3_interrupt(db); + T.assert( 0!==capi.sqlite3_is_interrupted(db) ); + db.close(); + }) + + //////////////////////////////////////////////////////////////////// + .t("sqlite3_set_errmsg()", function(sqlite3){ + /* Added in 3.51.0 */ + const db = new sqlite3.oo1.DB();//(':memory:','wt'); + try{ + const capi = sqlite3.capi; + const sse = capi.sqlite3_set_errmsg, + sec = capi.sqlite3_errcode, + sem = capi.sqlite3_errmsg; + T.assert( 0===sec(db) ) + .assert( "not an error"===sem(db) ); + let rc = sse(db, capi.SQLITE_RANGE, "nope"); + T.assert( 0==rc ) + .assert( capi.SQLITE_RANGE===sec(db) ) + .assert( "nope"===sem(db) ); + rc = sse(0, 0, 0); + T.assert( capi.SQLITE_MISUSE===rc ); + rc = sse(db, 0, 0); + T.assert( 0===rc ) + .assert( 0===sec(db) ) + .assert( "not an error"===sem(db) ); + }finally{ + db.close(); + } + }); + ; + + //////////////////////////////////////////////////////////////////// + T.g('Bug Reports') + .t({ + name: 'Delete via bound parameter in subquery', + predicate: ()=>wasm.compileOptionUsed('ENABLE_FTS5') || "Missing FTS5", + test: function(sqlite3){ + /** + Testing https://sqlite.org/forum/forumpost/40ce55bdf5 with + the exception that that post uses "external content" for + the FTS index. This isn't testing a fix, just confirming + that the bug report is not really a bug. + */ + const db = new sqlite3.oo1.DB();//(':memory:','wt'); + db.exec([ + "create virtual table f using fts5 (path);", + "insert into f(path) values('abc'),('def'),('ghi');" + ]); + const fetchEm = ()=> db.exec({ + sql: "SELECT * FROM f order by path", + rowMode: 'array' + }); + /*const dump = function(lbl){ + let rc = fetchEm(); + log((lbl ? (lbl+' results') : ''),rc); + };*/ + //dump('Full fts table'); + let rc = fetchEm(); + T.assert(3===rc.length); + db.exec( + ["delete from f where rowid in (", + "select rowid from f where path = :path", + ")"], + {bind: {":path": "def"}} + ); + //dump('After deleting one entry via subquery'); + rc = fetchEm(); + T.assert(2===rc.length) + .assert('abcghi'===rc.join('')); + //log('rc =',rc); + db.close(); + } + }) + .t({ + name: 'r/o connection recovery from write op error', + predicate: ()=>hasOpfs() || "Requires OPFS to reproduce", + //predicate: ()=>false, + test: async function(sqlite3){ + /* https://sqlite.org/forum/forumpost/cf37d5ff1182c31081 + + The "opfs" VFS (but not SAHPool) was formerly misbehaving + after a write attempt was made on a db opened with + mode=ro. This test ensures that that behavior is fixed and + compares that behavior with other VFSes. */ + const tryOne = function(vfsName,descr){ + const uri = 'file:///foo.db'; + let db = new sqlite3.oo1.DB(uri + (vfsName ? '?vfs='+vfsName : '')); + db.exec([ + "drop table if exists t;", + "create table t(a);", + "insert into t(a) values('abc'),('def'),('ghi');" + ]); + db.close(); + db = new sqlite3.oo1.DB(uri+'?mode=ro'+ + (vfsName ? '&vfs='+vfsName : '')); + let err; + try { + db.exec('insert into t(a) values(1)'); + }catch(e){ + err = e; + } + T.assert(err && (err.message.indexOf('SQLITE_READONLY')===0)); + try{ + db.exec('select a from t'); + }finally{ + db.close(); + } + }; + const poolConfig = JSON.parse(JSON.stringify(sahPoolConfig)); + poolConfig.name = 'opfs-sahpool-cf37d5ff11'; + let poolUtil; + await sqlite3.installOpfsSAHPoolVfs(poolConfig).then(p=>poolUtil=p); + T.assert(!!sqlite3.capi.sqlite3_vfs_find(poolConfig.name), "Expecting to find just-registered VFS"); + try{ + tryOne(false, "Emscripten filesystem"); + tryOne(poolConfig.name); + tryOne('opfs'); + }finally{ + await poolUtil.removeVfs(); + } + } + }) + .t({ + /* https://github.com/sqlite/sqlite-wasm/issues/92 */ + name: 'sqlite3_set_auxdata() binding signature', + test: function(sqlite3){ + const db = new sqlite3.oo1.DB(); + const stack = wasm.pstack.pointer; + const pAux = wasm.pstack.alloc(4); + let pAuxDestructed = 0; + const pAuxDtor = wasm.installFunction('v(p)', function(ptr){ + //log("freeing auxdata"); + ++pAuxDestructed; + }); + let pAuxDtorDestructed = false; + db.onclose = { + after: ()=>{ + pAuxDtorDestructed = true; + wasm.uninstallFunction(pAuxDtor); + } + }; + let nAuxSet = 0 /* how many times we set aux data */; + let nAuxReused = 0 /* how many times we reused aux data */; + try{ + db.createFunction("auxtest",{ + xFunc: function(pCx, x, y){ + T.assert(wasm.isPtr(pCx)); + const localAux = capi.sqlite3_get_auxdata(pCx, 0); + if( !localAux ){ + //log("setting auxdata"); + /** + We do not currently an automated way to clean up + auxdata finalizer functions (the 4th argument to + sqlite3_set_auxdata()) which get automatically + converted from JS to WASM. Because of that, enabling + automated conversions here would lead to leaks more + often than not. Instead, follow the pattern show in + this function: use wasm.installFunction() to create + the function, then pass the resulting function + pointer this function, and cleanup (at some point) + using wasm.uninstallFunction(). + */ + ++nAuxSet; + capi.sqlite3_set_auxdata(pCx, 0, pAux, pAuxDtor); + }else{ + //log("reusing auxdata",localAux); + T.assert(pAux===localAux); + ++nAuxReused; + } + return x; + } + }); + db.exec([ + "create table t(a);", + "insert into t(a) values(1),(2),(1);", + "select auxtest(1,a), auxtest(1,a) from t order by a" + ]); + }finally{ + db.close(); + wasm.pstack.restore(stack); + } + T.assert(nAuxSet>0).assert(nAuxReused>0) + .assert(6===nAuxReused+nAuxSet); + T.assert(pAuxDestructed>0); + T.assert(pAuxDtorDestructed); + } + }) + ;/*end of Bug Reports group*/; + + //////////////////////////////////////////////////////////////////////// + log("Loading and initializing sqlite3 WASM module..."); + if(0){ + globalThis.sqlite3ApiConfig = { + debug: ()=>{}, + log: ()=>{}, + warn: ()=>{}, + error: ()=>{} + } + } +//#if not target:es6-module + if(!globalThis.sqlite3InitModule && !isUIThread()){ + /* Vanilla worker, as opposed to an ES6 module worker */ + /* + If sqlite3.js is in a directory other than this script, in order + to get sqlite3.js to resolve sqlite3.wasm properly, we have to + explicitly tell it where sqlite3.js is being loaded from. We do + that by passing the `sqlite3.dir=theDirName` URL argument to + _this_ script. That URL argument will be seen by the JS/WASM + loader and it will adjust the sqlite3.wasm path accordingly. If + sqlite3.js/.wasm are in the same directory as this script then + that's not needed. + + URL arguments passed as part of the filename via importScripts() + are simply lost, and such scripts see the globalThis.location of + _this_ script. + */ + let sqlite3Js = '@sqlite3.js@' + .split('/').pop()/*the build-injected name has a dir part and + we specifically want to test the following + support for locating the wasm, so remove + that dir part. */; + const urlParams = new URL(globalThis.location.href).searchParams; + if(urlParams.has('sqlite3.dir')){ + sqlite3Js = urlParams.get('sqlite3.dir') + '/' + sqlite3Js; + } + importScripts(sqlite3Js); + } +//#endif + globalThis.sqlite3InitModule.__isUnderTest = + true /* disables certain API-internal cleanup so that we can + test internal APIs from here */; + globalThis.sqlite3InitModule({ + print: log, + printErr: error + }).then(async function(sqlite3){ + TestUtil.assert(!!sqlite3.util); + log("Done initializing WASM/JS bits. Running tests..."); + sqlite3.config.warn("Installing sqlite3 bits as global S for local dev/test purposes."); + globalThis.S = sqlite3; + /*await sqlite3.installOpfsSAHPoolVfs(sahPoolConfig) + .then((u)=>log("Loaded",u.vfsName,"VFS")) + .catch(e=>{ + log("Cannot install OpfsSAHPool.",e); + });*/ + capi = sqlite3.capi; + wasm = sqlite3.wasm; + log("sqlite3 version:",capi.sqlite3_libversion(), + capi.sqlite3_sourceid()); + if(wasm.bigIntEnabled){ + log("BigInt/int64 support is enabled."); + }else{ + logClass('warning',"BigInt/int64 support is disabled."); + } + if(haveWasmCTests()){ + log("sqlite3__wasm_test_...() APIs are available."); + }else{ + logClass('warning',"sqlite3__wasm_test_...() APIs unavailable."); + } + log("registered vfs list =",capi.sqlite3_js_vfs_list().join(', ')); + SQLite3 = sqlite3; + log("WASM pointer size:",wasm.ptr.size,"bytes."); + TestUtil.checkHeapSize(); + TestUtil.runTests(sqlite3); + }); +})(self); diff --git a/ext/wasm/tests/opfs/concurrency/index.html b/ext/wasm/tests/opfs/concurrency/index.html new file mode 100644 index 0000000000..54ed04a4f6 --- /dev/null +++ b/ext/wasm/tests/opfs/concurrency/index.html @@ -0,0 +1,52 @@ + + + + + + + + sqlite3 OPFS Worker concurrency tester + + + +

    +

    + OPFS concurrency tester using multiple independent Workers. + Disclaimer: concurrency in OPFS is currently a pain point! +

    +

    + URL flags: pass a number of workers using + the workers=N URL flag. Set the time between each + workload with interval=N (milliseconds). Set the + number of worker iterations with iterations=N. + Enable OPFS VFS verbosity with verbose=1-3 (output + goes to the dev console). Disable/enable "unlock ASAP" mode + (higher concurrency, lower speed) with unlock-asap=0-1. +

    +

    Achtung: if it does not start to do anything within a couple of + seconds, check the dev console: Chrome sometimes fails to load + the wasm module due to "cannot allocate WasmMemory." Closing and + re-opening the tab usually resolves it, but sometimes restarting + the browser is required. +

    +

    + Links for various testing options:

    +

    +
    + + +
    +
    + + + + diff --git a/ext/wasm/tests/opfs/concurrency/test.js b/ext/wasm/tests/opfs/concurrency/test.js new file mode 100644 index 0000000000..1848901afe --- /dev/null +++ b/ext/wasm/tests/opfs/concurrency/test.js @@ -0,0 +1,161 @@ +(async function(self){ + + const logCss = (function(){ + const mapToString = (v)=>{ + switch(typeof v){ + case 'number': case 'string': case 'boolean': + case 'undefined': case 'bigint': + return ''+v; + default: break; + } + if(null===v) return 'null'; + if(v instanceof Error){ + v = { + message: v.message, + stack: v.stack, + errorClass: v.name + }; + } + return JSON.stringify(v,undefined,2); + }; + const normalizeArgs = (args)=>args.map(mapToString); + const logTarget = document.querySelector('#test-output'); + const logCss = function(cssClass,...args){ + const ln = document.createElement('div'); + if(cssClass){ + for(const c of (Array.isArray(cssClass) ? cssClass : [cssClass])){ + ln.classList.add(c); + } + } + ln.append(document.createTextNode(normalizeArgs(args).join(' '))); + logTarget.append(ln); + }; + const cbReverse = document.querySelector('#cb-log-reverse'); + const cbReverseKey = 'tester1:cb-log-reverse'; + const cbReverseIt = ()=>{ + logTarget.classList[cbReverse.checked ? 'add' : 'remove']('reverse'); + localStorage.setItem(cbReverseKey, cbReverse.checked ? 1 : 0); + }; + cbReverse.addEventListener('change', cbReverseIt, true); + if(localStorage.getItem(cbReverseKey)){ + cbReverse.checked = !!(+localStorage.getItem(cbReverseKey)); + } + cbReverseIt(); + return logCss; + })(); + const stdout = (...args)=>logCss('',...args); + const stderr = (...args)=>logCss('error',...args); + + const wait = async (ms)=>{ + return new Promise((resolve)=>setTimeout(resolve,ms)); + }; + + const urlArgsJs = new URL(document.currentScript.src).searchParams; + const urlArgsHtml = new URL(self.location.href).searchParams; + const options = Object.create(null); + options.sqlite3Dir = urlArgsJs.get('sqlite3.dir'); + options.workerCount = ( + urlArgsHtml.has('workers') ? +urlArgsHtml.get('workers') : 3 + ) || 4; + options.opfsVerbose = ( + urlArgsHtml.has('verbose') ? +urlArgsHtml.get('verbose') : 1 + ) || 1; + options.interval = ( + urlArgsHtml.has('interval') ? +urlArgsHtml.get('interval') : 1000 + ) || 1000; + options.iterations = ( + urlArgsHtml.has('iterations') ? +urlArgsHtml.get('iterations') : 10 + ) || 10; + options.unlockAsap = ( + urlArgsHtml.has('unlock-asap') ? +urlArgsHtml.get('unlock-asap') : 0 + ) || 0; + options.noUnlink = !!urlArgsHtml.has('no-unlink'); + const workers = []; + workers.post = (type,...args)=>{ + for(const w of workers) w.postMessage({type, payload:args}); + }; + workers.counts = {loaded: 0, passed: 0, failed: 0}; + const checkFinished = function(){ + if(workers.counts.passed + workers.counts.failed !== workers.length){ + return; + } + if(workers.counts.failed>0){ + logCss('tests-fail',"Finished with",workers.counts.failed,"failure(s)."); + }else{ + logCss('tests-pass',"All",workers.length,"workers finished."); + } + }; + workers.onmessage = function(msg){ + msg = msg.data; + const prefix = 'Worker #'+msg.worker+':'; + switch(msg.type){ + case 'loaded': + stdout(prefix,"loaded"); + if(++workers.counts.loaded === workers.length){ + stdout("All",workers.length,"workers loaded. Telling them to run..."); + workers.post('run'); + } + break; + case 'stdout': stdout(prefix,...msg.payload); break; + case 'stderr': stderr(prefix,...msg.payload); break; + case 'error': stderr(prefix,"ERROR:",...msg.payload); break; + case 'finished': + ++workers.counts.passed; + logCss('tests-pass',prefix,...msg.payload); + checkFinished(); + break; + case 'failed': + ++workers.counts.failed; + logCss('tests-fail',prefix,"FAILED:",...msg.payload); + checkFinished(); + break; + default: logCss('error',"Unhandled message type:",msg); break; + } + }; + + /* Set up links to launch this tool with various combinations of + flags... */ + const eTestLinks = document.querySelector('#testlinks'); + const optArgs = function(obj){ + const li = []; + for(const k of ['interval','iterations','workers','verbose','unlock-asap']){ + if( obj.hasOwnProperty(k) ) li.push(k+'='+obj[k]); + } + return li.join('&'); + }; + for(const opt of [ + {interval: 1000, workers: 5, iterations: 30}, + {interval: 500, workers: 5, iterations: 30}, + {interval: 250, workers: 3, iterations: 30}, + {interval: 600, workers: 5, iterations: 100} + ]){ + const li = document.createElement('li'); + eTestLinks.appendChild(li); + const a = document.createElement('a'); + li.appendChild(a); + const args = optArgs(opt); + a.setAttribute('href', '?'+args); + a.innerText = args; + } + + stdout("Launching",options.workerCount,"workers. Options:",options); + workers.uri = ( + 'worker.js?' + + 'sqlite3.dir='+options.sqlite3Dir + + '&interval='+options.interval + + '&iterations='+options.iterations + + '&opfs-verbose='+options.opfsVerbose + + '&opfs-unlock-asap='+options.unlockAsap + ); + for(let i = 0; i < options.workerCount; ++i){ + stdout("Launching worker..."); + workers.push(new Worker( + workers.uri+'&workerId='+(i+1)+( + (i || options.noUnlink) ? '' : '&unlink-db' + ) + )); + } + // Have to delay onmessage assignment until after the loop + // to avoid that early workers get an undue head start. + workers.forEach((w)=>w.onmessage = workers.onmessage); +})(self); diff --git a/ext/wasm/tests/opfs/concurrency/worker.js b/ext/wasm/tests/opfs/concurrency/worker.js new file mode 100644 index 0000000000..5d28bedee0 --- /dev/null +++ b/ext/wasm/tests/opfs/concurrency/worker.js @@ -0,0 +1,113 @@ +importScripts( + (new URL(self.location.href).searchParams).get('sqlite3.dir') + '/sqlite3.js' +); +self.sqlite3InitModule().then(async function(sqlite3){ + const urlArgs = new URL(self.location.href).searchParams; + const options = { + workerName: urlArgs.get('workerId') || Math.round(Math.random()*10000), + unlockAsap: urlArgs.get('opfs-unlock-asap') || 0 /*EXPERIMENTAL*/ + }; + const wPost = (type,...payload)=>{ + postMessage({type, worker: options.workerName, payload}); + }; + const stdout = (...args)=>wPost('stdout',...args); + const stderr = (...args)=>wPost('stderr',...args); + if(!sqlite3.opfs){ + stderr("OPFS support not detected. Aborting."); + return; + } + + const wait = async (ms)=>{ + return new Promise((resolve)=>setTimeout(resolve,ms)); + }; + + const dbName = 'concurrency-tester.db'; + if(urlArgs.has('unlink-db')){ + await sqlite3.opfs.unlink(dbName); + stdout("Unlinked",dbName); + } + wPost('loaded'); + let db; + const interval = Object.assign(Object.create(null),{ + delay: urlArgs.has('interval') ? (+urlArgs.get('interval') || 750) : 750, + handle: undefined, + count: 0 + }); + const finish = ()=>{ + if(db){ + if(!db.pointer) return; + db.close(); + } + if(interval.error){ + wPost('failed',"Ending work after interval #"+interval.count, + "due to error:",interval.error); + }else{ + wPost('finished',"Ending work after",interval.count,"intervals."); + } + }; + const run = async function(){ + db = new sqlite3.oo1.OpfsDb({ + filename: 'file:'+dbName+'?opfs-unlock-asap='+options.unlockAsap, + flags: 'c' + }); + sqlite3.capi.sqlite3_busy_timeout(db.pointer, 5000); + db.transaction((db)=>{ + db.exec([ + "create table if not exists t1(w TEXT UNIQUE ON CONFLICT REPLACE,v);", + "create table if not exists t2(w TEXT UNIQUE ON CONFLICT REPLACE,v);" + ]); + }); + + const maxIterations = + urlArgs.has('iterations') ? (+urlArgs.get('iterations') || 10) : 10; + stdout("Starting interval-based db updates with delay of",interval.delay,"ms."); + const doWork = async ()=>{ + const tm = new Date().getTime(); + ++interval.count; + const prefix = "v(#"+interval.count+")"; + stdout("Setting",prefix,"=",tm); + try{ + db.exec({ + sql:"INSERT OR REPLACE INTO t1(w,v) VALUES(?,?)", + bind: [options.workerName, new Date().getTime()] + }); + //stdout("Set",prefix); + }catch(e){ + interval.error = e; + } + }; + if(1){/*use setInterval()*/ + setTimeout(async function timer(){ + await doWork(); + if(interval.error || maxIterations === interval.count){ + finish(); + }else{ + setTimeout(timer, interval.delay); + } + }, interval.delay); + }else{ + /*This approach provides no concurrency whatsoever: each worker + is run to completion before any others can work.*/ + let i; + for(i = 0; i < maxIterations; ++i){ + await doWork(); + if(interval.error) break; + await wait(interval.ms); + } + finish(); + } + }/*run()*/; + + self.onmessage = function({data}){ + switch(data.type){ + case 'run': run().catch((e)=>{ + if(!interval.error) interval.error = e; + finish(); + }); + break; + default: + stderr("Unhandled message type '"+data.type+"'."); + break; + } + }; +}); diff --git a/ext/wasm/tests/opfs/sahpool/digest-worker.js b/ext/wasm/tests/opfs/sahpool/digest-worker.js new file mode 100644 index 0000000000..28b3c1673f --- /dev/null +++ b/ext/wasm/tests/opfs/sahpool/digest-worker.js @@ -0,0 +1,94 @@ +/* + 2025-01-31 + + The author disclaims copyright to this source code. In place of a + legal notice, here is a blessing: + + * May you do good and not evil. + * May you find forgiveness for yourself and forgive others. + * May you share freely, never taking more than you give. + + *********************************************************************** + + This file is part of testing the OPFS SAHPool VFS's computeDigest() + fix. See ./digest.html for the details. +*/ +const clog = console.log.bind(console); +const wPost = (type,...args)=>postMessage({type, payload:args}); +const log = (...args)=>{ + clog("Worker:",...args); + wPost('log',...args); +} + +const hasOpfs = ()=>{ + return globalThis.FileSystemHandle + && globalThis.FileSystemDirectoryHandle + && globalThis.FileSystemFileHandle + && globalThis.FileSystemFileHandle.prototype.createSyncAccessHandle + && navigator?.storage?.getDirectory; +}; +if( !hasOpfs() ){ + wPost('error',"OPFS not detected"); + throw new Error("OPFS not detected"); +} + +clog("Importing sqlite3..."); +const searchParams = new URL(self.location.href).searchParams; +importScripts(searchParams.get('sqlite3.dir') + '/sqlite3.js'); + +const runTests = function(sqlite3, poolUtil){ + const fname = '/my.db'; + let db = new poolUtil.OpfsSAHPoolDb(fname); + let n = (new Date()).valueOf(); + try { + db.exec([ + "create table if not exists t(a);" + ]); + db.exec({ + sql: "insert into t(a) values(?)", + bind: n++ + }); + log(fname,"record count: ",db.selectValue("select count(*) from t")); + }finally{ + db.close(); + } + + db = new poolUtil.OpfsSAHPoolDb(fname); + try { + db.exec({ + sql: "insert into t(a) values(?)", + bind: n++ + }); + log(fname,"record count: ",db.selectValue("select count(*) from t")); + }finally{ + db.close(); + } + + const fname2 = '/my2.db'; + db = new poolUtil.OpfsSAHPoolDb(fname2); + try { + db.exec([ + "create table if not exists t(a);" + ]); + db.exec({ + sql: "insert into t(a) values(?)", + bind: n++ + }); + log(fname2,"record count: ",db.selectValue("select count(*) from t")); + }finally{ + db.close(); + } +}; + +globalThis.sqlite3InitModule().then(async function(sqlite3){ + log("sqlite3 version:",sqlite3.version); + const sahPoolConfig = { + name: 'opfs-sahpool-digest', + clearOnInit: false, + initialCapacity: 6 + }; + return sqlite3.installOpfsSAHPoolVfs(sahPoolConfig).then(poolUtil=>{ + log('vfs acquired'); + runTests(sqlite3, poolUtil); + }); +}); diff --git a/ext/wasm/tests/opfs/sahpool/digest.html b/ext/wasm/tests/opfs/sahpool/digest.html new file mode 100644 index 0000000000..daa1f77287 --- /dev/null +++ b/ext/wasm/tests/opfs/sahpool/digest.html @@ -0,0 +1,151 @@ + + + + + + + + + sqlite3 tester: OpfsSAHPool Digest + + +

    + +

    + This is a test app for the digest calculation of the OPFS + SAHPool VFS. It requires running it with a new database created using + v3.49.0 or older, then running it again with a newer version, then + again with 3.49.0 or older. +

    +
    + + +
    +
    + + + diff --git a/ext/wasm/tests/opfs/sahpool/index.html b/ext/wasm/tests/opfs/sahpool/index.html new file mode 100644 index 0000000000..f3d07f456a --- /dev/null +++ b/ext/wasm/tests/opfs/sahpool/index.html @@ -0,0 +1,31 @@ + + + + + + + + + sqlite3 tester: OpfsSAHPool Pausing + + +

    + +

    + This page provides a very basic demonstration of + "pausing" and "unpausing" the OPFS SAHPool VFS such that + multiple pages or workers can use it by coordinating which + handler may have it open at any given time. +

    +
    + + +
    +
    + + + + diff --git a/ext/wasm/tests/opfs/sahpool/sahpool-pausing.js b/ext/wasm/tests/opfs/sahpool/sahpool-pausing.js new file mode 100644 index 0000000000..1aa98d3cb3 --- /dev/null +++ b/ext/wasm/tests/opfs/sahpool/sahpool-pausing.js @@ -0,0 +1,183 @@ +/* + 2025-01-31 + + The author disclaims copyright to this source code. In place of a + legal notice, here is a blessing: + + * May you do good and not evil. + * May you find forgiveness for yourself and forgive others. + * May you share freely, never taking more than you give. + + *********************************************************************** + + These tests are specific to the opfs-sahpool VFS and are limited to + demonstrating its pause/unpause capabilities. + + Most of this file is infrastructure for displaying results to the + user. Search for runTests() to find where the work actually starts. +*/ +'use strict'; +(function(){ + let logClass; + + const mapToString = (v)=>{ + switch(typeof v){ + case 'number': case 'string': case 'boolean': + case 'undefined': case 'bigint': + return ''+v; + default: break; + } + if(null===v) return 'null'; + if(v instanceof Error){ + v = { + message: v.message, + stack: v.stack, + errorClass: v.name + }; + } + return JSON.stringify(v,undefined,2); + }; + const normalizeArgs = (args)=>args.map(mapToString); + const logTarget = document.querySelector('#test-output'); + logClass = function(cssClass,...args){ + const ln = document.createElement('div'); + if(cssClass){ + for(const c of (Array.isArray(cssClass) ? cssClass : [cssClass])){ + ln.classList.add(c); + } + } + ln.append(document.createTextNode(normalizeArgs(args).join(' '))); + logTarget.append(ln); + }; + const cbReverse = document.querySelector('#cb-log-reverse'); + //cbReverse.setAttribute('checked','checked'); + const cbReverseKey = 'tester1:cb-log-reverse'; + const cbReverseIt = ()=>{ + logTarget.classList[cbReverse.checked ? 'add' : 'remove']('reverse'); + //localStorage.setItem(cbReverseKey, cbReverse.checked ? 1 : 0); + }; + cbReverse.addEventListener('change', cbReverseIt, true); + /*if(localStorage.getItem(cbReverseKey)){ + cbReverse.checked = !!(+localStorage.getItem(cbReverseKey)); + }*/ + cbReverseIt(); + + const log = (...args)=>{ + //console.log(...args); + logClass('',...args); + } + const warn = (...args)=>{ + console.warn(...args); + logClass('warning',...args); + } + const error = (...args)=>{ + console.error(...args); + logClass('error',...args); + }; + + const toss = (...args)=>{ + error(...args); + throw new Error(args.join(' ')); + }; + + const endOfWork = (passed=true)=>{ + const eH = document.querySelector('#color-target'); + const eT = document.querySelector('title'); + if(passed){ + log("End of work chain. If you made it this far, you win."); + eH.innerText = 'PASS: '+eH.innerText; + eH.classList.add('tests-pass'); + eT.innerText = 'PASS: '+eT.innerText; + }else{ + eH.innerText = 'FAIL: '+eH.innerText; + eH.classList.add('tests-fail'); + eT.innerText = 'FAIL: '+eT.innerText; + } + }; + + const nextHandlerQueue = []; + + const nextHandler = function(workerId,...msg){ + log(workerId,...msg); + (nextHandlerQueue.shift())(); + }; + + const postThen = function(W, msgType, callback){ + nextHandlerQueue.push(callback); + W.postMessage({type:msgType}); + }; + + /** + Run a series of operations on an sahpool db spanning two workers. + This would arguably be more legible with Promises, but creating a + Promise-based communication channel for this purpose is left as + an exercise for the reader. An example of such a proxy can be + found in the SQLite source tree: + + https://sqlite.org/src/file/ext/wasm/api/sqlite3-worker1-promiser.c-pp.js + */ + const runPyramidOfDoom = function(W1, W2){ + postThen(W1, 'vfs-acquire', function(){ + postThen(W1, 'db-init', function(){ + postThen(W1, 'db-query', function(){ + postThen(W1, 'vfs-pause', function(){ + postThen(W2, 'vfs-acquire', function(){ + postThen(W2, 'db-query', function(){ + postThen(W2, 'vfs-remove', endOfWork); + }); + }); + }); + }); + }); + }); + }; + + const runTests = function(){ + log("Running opfs-sahpool pausing tests..."); + const wjs = 'sahpool-worker.js?sqlite3.dir=../../../jswasm'; + const W1 = new Worker(wjs+'&workerId=w1'), + W2 = new Worker(wjs+'&workerId=w2'); + W1.workerId = 'w1'; + W2.workerId = 'w2'; + let initCount = 0; + const onmessage = function({data}){ + //log("onmessage:",data); + switch(data.type){ + case 'vfs-acquired': + nextHandler(data.workerId, "VFS acquired"); + break; + case 'vfs-paused': + nextHandler(data.workerId, "VFS paused"); + break; + case 'vfs-unpaused': + nextHandler(data.workerId, 'VFS unpaused'); + break; + case 'vfs-removed': + nextHandler(data.workerId, 'VFS removed'); + break; + case 'db-inited': + nextHandler(data.workerId, 'db initialized'); + break; + case 'query-result': + nextHandler(data.workerId, 'query result', data.payload); + break; + case 'log': + log(data.workerId, ':', ...data.payload); + break; + case 'error': + error(data.workerId, ':', ...data.payload); + endOfWork(false); + break; + case 'initialized': + log(data.workerId, ': Worker initialized',...data.payload); + if( 2===++initCount ){ + runPyramidOfDoom(W1, W2); + } + break; + } + }; + W1.onmessage = W2.onmessage = onmessage; + }; + + runTests(); +})(); diff --git a/ext/wasm/tests/opfs/sahpool/sahpool-worker.js b/ext/wasm/tests/opfs/sahpool/sahpool-worker.js new file mode 100644 index 0000000000..592f159551 --- /dev/null +++ b/ext/wasm/tests/opfs/sahpool/sahpool-worker.js @@ -0,0 +1,104 @@ +/* + 2025-01-31 + + The author disclaims copyright to this source code. In place of a + legal notice, here is a blessing: + + * May you do good and not evil. + * May you find forgiveness for yourself and forgive others. + * May you share freely, never taking more than you give. + + *********************************************************************** + + This file is part of sahpool-pausing.js's demonstration of the + pause/unpause feature of the opfs-sahpool VFS. +*/ +const searchParams = new URL(self.location.href).searchParams; +const workerId = searchParams.get('workerId'); +const wPost = (type,...args)=>postMessage({type, workerId, payload:args}); +const log = (...args)=>wPost('log',...args); +let capi, wasm, S, poolUtil; + +const sahPoolConfig = { + name: 'opfs-sahpool-pausable', + clearOnInit: false, + initialCapacity: 3 +}; + +importScripts(searchParams.get('sqlite3.dir') + '/sqlite3.js'); + +const sqlExec = function(sql){ + const db = new poolUtil.OpfsSAHPoolDb('/my.db'); + try{ + return db.exec(sql); + }finally{ + db.close(); + } +}; + +const clog = console.log.bind(console); +globalThis.onmessage = function({data}){ + clog(workerId+": onmessage:",data); + switch(data.type){ + case 'vfs-acquire': + if( poolUtil ){ + poolUtil.unpauseVfs().then(()=>wPost('vfs-unpaused')); + }else{ + S.installOpfsSAHPoolVfs(sahPoolConfig).then(pu=>{ + poolUtil = pu; + wPost('vfs-acquired'); + }); + } + break; + case 'db-init': + try{ + sqlExec([ + "DROP TABLE IF EXISTS mytable;", + "CREATE TABLE mytable(a);", + "INSERT INTO mytable(a) VALUES(11),(22),(33)" + ]); + wPost('db-inited'); + }catch(e){ + wPost('error',e.message); + } + break; + case 'db-query': { + const rc = sqlExec({ + sql: 'select * from mytable order by a', + rowMode: 'array', + returnValue: 'resultRows' + }); + wPost('query-result',rc); + break; + } + case 'vfs-remove': + poolUtil.removeVfs().then(()=>wPost('vfs-removed')); + break; + case 'vfs-pause': + poolUtil.pauseVfs(); + wPost('vfs-paused'); + break; + } +}; + +const hasOpfs = ()=>{ + return globalThis.FileSystemHandle + && globalThis.FileSystemDirectoryHandle + && globalThis.FileSystemFileHandle + && globalThis.FileSystemFileHandle.prototype.createSyncAccessHandle + && navigator?.storage?.getDirectory; +}; +if( !hasOpfs() ){ + wPost('error',"OPFS not detected"); +}else{ + globalThis.sqlite3InitModule().then(async function(sqlite3){ + S = sqlite3; + capi = S.capi; + wasm = S.wasm; + log("sqlite3 version:",capi.sqlite3_libversion(), + capi.sqlite3_sourceid()); + //return sqlite3.installOpfsSAHPoolVfs(sahPoolConfig).then(pu=>poolUtil=pu); + }).then(()=>{ + wPost('initialized'); + }); +} diff --git a/install-sh b/install-sh deleted file mode 100755 index e9de23842d..0000000000 --- a/install-sh +++ /dev/null @@ -1,251 +0,0 @@ -#!/bin/sh -# -# install - install a program, script, or datafile -# This comes from X11R5 (mit/util/scripts/install.sh). -# -# Copyright 1991 by the Massachusetts Institute of Technology -# -# Permission to use, copy, modify, distribute, and sell this software and its -# documentation for any purpose is hereby granted without fee, provided that -# the above copyright notice appear in all copies and that both that -# copyright notice and this permission notice appear in supporting -# documentation, and that the name of M.I.T. not be used in advertising or -# publicity pertaining to distribution of the software without specific, -# written prior permission. M.I.T. makes no representations about the -# suitability of this software for any purpose. It is provided "as is" -# without express or implied warranty. -# -# Calling this script install-sh is preferred over install.sh, to prevent -# `make' implicit rules from creating a file called install from it -# when there is no Makefile. -# -# This script is compatible with the BSD install script, but was written -# from scratch. It can only install one file at a time, a restriction -# shared with many OS's install programs. - - -# set DOITPROG to echo to test this script - -# Don't use :- since 4.3BSD and earlier shells don't like it. -doit="${DOITPROG-}" - - -# put in absolute paths if you don't have them in your path; or use env. vars. - -mvprog="${MVPROG-mv}" -cpprog="${CPPROG-cp}" -chmodprog="${CHMODPROG-chmod}" -chownprog="${CHOWNPROG-chown}" -chgrpprog="${CHGRPPROG-chgrp}" -stripprog="${STRIPPROG-strip}" -rmprog="${RMPROG-rm}" -mkdirprog="${MKDIRPROG-mkdir}" - -transformbasename="" -transform_arg="" -instcmd="$mvprog" -chmodcmd="$chmodprog 0755" -chowncmd="" -chgrpcmd="" -stripcmd="" -rmcmd="$rmprog -f" -mvcmd="$mvprog" -src="" -dst="" -dir_arg="" - -while [ x"$1" != x ]; do - case $1 in - -c) instcmd="$cpprog" - shift - continue;; - - -d) dir_arg=true - shift - continue;; - - -m) chmodcmd="$chmodprog $2" - shift - shift - continue;; - - -o) chowncmd="$chownprog $2" - shift - shift - continue;; - - -g) chgrpcmd="$chgrpprog $2" - shift - shift - continue;; - - -s) stripcmd="$stripprog" - shift - continue;; - - -t=*) transformarg=`echo $1 | sed 's/-t=//'` - shift - continue;; - - -b=*) transformbasename=`echo $1 | sed 's/-b=//'` - shift - continue;; - - *) if [ x"$src" = x ] - then - src=$1 - else - # this colon is to work around a 386BSD /bin/sh bug - : - dst=$1 - fi - shift - continue;; - esac -done - -if [ x"$src" = x ] -then - echo "install: no input file specified" - exit 1 -else - true -fi - -if [ x"$dir_arg" != x ]; then - dst=$src - src="" - - if [ -d $dst ]; then - instcmd=: - chmodcmd="" - else - instcmd=mkdir - fi -else - -# Waiting for this to be detected by the "$instcmd $src $dsttmp" command -# might cause directories to be created, which would be especially bad -# if $src (and thus $dsttmp) contains '*'. - - if [ -f $src -o -d $src ] - then - true - else - echo "install: $src does not exist" - exit 1 - fi - - if [ x"$dst" = x ] - then - echo "install: no destination specified" - exit 1 - else - true - fi - -# If destination is a directory, append the input filename; if your system -# does not like double slashes in filenames, you may need to add some logic - - if [ -d $dst ] - then - dst="$dst"/`basename $src` - else - true - fi -fi - -## this sed command emulates the dirname command -dstdir=`echo $dst | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'` - -# Make sure that the destination directory exists. -# this part is taken from Noah Friedman's mkinstalldirs script - -# Skip lots of stat calls in the usual case. -if [ ! -d "$dstdir" ]; then -defaultIFS=' -' -IFS="${IFS-${defaultIFS}}" - -oIFS="${IFS}" -# Some sh's can't handle IFS=/ for some reason. -IFS='%' -set - `echo ${dstdir} | sed -e 's@/@%@g' -e 's@^%@/@'` -IFS="${oIFS}" - -pathcomp='' - -while [ $# -ne 0 ] ; do - pathcomp="${pathcomp}${1}" - shift - - if [ ! -d "${pathcomp}" ] ; - then - $mkdirprog "${pathcomp}" - else - true - fi - - pathcomp="${pathcomp}/" -done -fi - -if [ x"$dir_arg" != x ] -then - $doit $instcmd $dst && - - if [ x"$chowncmd" != x ]; then $doit $chowncmd $dst; else true ; fi && - if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dst; else true ; fi && - if [ x"$stripcmd" != x ]; then $doit $stripcmd $dst; else true ; fi && - if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dst; else true ; fi -else - -# If we're going to rename the final executable, determine the name now. - - if [ x"$transformarg" = x ] - then - dstfile=`basename $dst` - else - dstfile=`basename $dst $transformbasename | - sed $transformarg`$transformbasename - fi - -# don't allow the sed command to completely eliminate the filename - - if [ x"$dstfile" = x ] - then - dstfile=`basename $dst` - else - true - fi - -# Make a temp file name in the proper directory. - - dsttmp=$dstdir/#inst.$$# - -# Move or copy the file name to the temp name - - $doit $instcmd $src $dsttmp && - - trap "rm -f ${dsttmp}" 0 && - -# and set any options; do chmod last to preserve setuid bits - -# If any of these fail, we abort the whole thing. If we want to -# ignore errors from any of these, just make sure not to ignore -# errors from the above "$doit $instcmd $src $dsttmp" command. - - if [ x"$chowncmd" != x ]; then $doit $chowncmd $dsttmp; else true;fi && - if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dsttmp; else true;fi && - if [ x"$stripcmd" != x ]; then $doit $stripcmd $dsttmp; else true;fi && - if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dsttmp; else true;fi && - -# Now rename the file to the real destination. - - $doit $rmcmd -f $dstdir/$dstfile && - $doit $mvcmd $dsttmp $dstdir/$dstfile - -fi && - - -exit 0 diff --git a/ltmain.sh b/ltmain.sh deleted file mode 100644 index 0f0a2da3f9..0000000000 --- a/ltmain.sh +++ /dev/null @@ -1,11147 +0,0 @@ -#! /bin/sh -## DO NOT EDIT - This file generated from ./build-aux/ltmain.in -## by inline-source v2014-01-03.01 - -# libtool (GNU libtool) 2.4.6 -# Provide generalized library-building support services. -# Written by Gordon Matzigkeit , 1996 - -# Copyright (C) 1996-2015 Free Software Foundation, Inc. -# This is free software; see the source for copying conditions. There is NO -# warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - -# GNU Libtool is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or -# (at your option) any later version. -# -# As a special exception to the GNU General Public License, -# if you distribute this file as part of a program or library that -# is built using GNU Libtool, you may include this file under the -# same distribution terms that you use for the rest of that program. -# -# GNU Libtool is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - - -PROGRAM=libtool -PACKAGE=libtool -VERSION=2.4.6 -package_revision=2.4.6 - - -## ------ ## -## Usage. ## -## ------ ## - -# Run './libtool --help' for help with using this script from the -# command line. - - -## ------------------------------- ## -## User overridable command paths. ## -## ------------------------------- ## - -# After configure completes, it has a better idea of some of the -# shell tools we need than the defaults used by the functions shared -# with bootstrap, so set those here where they can still be over- -# ridden by the user, but otherwise take precedence. - -: ${AUTOCONF="autoconf"} -: ${AUTOMAKE="automake"} - - -## -------------------------- ## -## Source external libraries. ## -## -------------------------- ## - -# Much of our low-level functionality needs to be sourced from external -# libraries, which are installed to $pkgauxdir. - -# Set a version string for this script. -scriptversion=2015-01-20.17; # UTC - -# General shell script boiler plate, and helper functions. -# Written by Gary V. Vaughan, 2004 - -# Copyright (C) 2004-2015 Free Software Foundation, Inc. -# This is free software; see the source for copying conditions. There is NO -# warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 3 of the License, or -# (at your option) any later version. - -# As a special exception to the GNU General Public License, if you distribute -# this file as part of a program or library that is built using GNU Libtool, -# you may include this file under the same distribution terms that you use -# for the rest of that program. - -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNES FOR A PARTICULAR PURPOSE. See the GNU -# General Public License for more details. - -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - -# Please report bugs or propose patches to gary@gnu.org. - - -## ------ ## -## Usage. ## -## ------ ## - -# Evaluate this file near the top of your script to gain access to -# the functions and variables defined here: -# -# . `echo "$0" | ${SED-sed} 's|[^/]*$||'`/build-aux/funclib.sh -# -# If you need to override any of the default environment variable -# settings, do that before evaluating this file. - - -## -------------------- ## -## Shell normalisation. ## -## -------------------- ## - -# Some shells need a little help to be as Bourne compatible as possible. -# Before doing anything else, make sure all that help has been provided! - -DUALCASE=1; export DUALCASE # for MKS sh -if test -n "${ZSH_VERSION+set}" && (emulate sh) >/dev/null 2>&1; then : - emulate sh - NULLCMD=: - # Pre-4.2 versions of Zsh do word splitting on ${1+"$@"}, which - # is contrary to our usage. Disable this feature. - alias -g '${1+"$@"}'='"$@"' - setopt NO_GLOB_SUBST -else - case `(set -o) 2>/dev/null` in *posix*) set -o posix ;; esac -fi - -# NLS nuisances: We save the old values in case they are required later. -_G_user_locale= -_G_safe_locale= -for _G_var in LANG LANGUAGE LC_ALL LC_CTYPE LC_COLLATE LC_MESSAGES -do - eval "if test set = \"\${$_G_var+set}\"; then - save_$_G_var=\$$_G_var - $_G_var=C - export $_G_var - _G_user_locale=\"$_G_var=\\\$save_\$_G_var; \$_G_user_locale\" - _G_safe_locale=\"$_G_var=C; \$_G_safe_locale\" - fi" -done - -# CDPATH. -(unset CDPATH) >/dev/null 2>&1 && unset CDPATH - -# Make sure IFS has a sensible default -sp=' ' -nl=' -' -IFS="$sp $nl" - -# There are apparently some retarded systems that use ';' as a PATH separator! -if test "${PATH_SEPARATOR+set}" != set; then - PATH_SEPARATOR=: - (PATH='/bin;/bin'; FPATH=$PATH; sh -c :) >/dev/null 2>&1 && { - (PATH='/bin:/bin'; FPATH=$PATH; sh -c :) >/dev/null 2>&1 || - PATH_SEPARATOR=';' - } -fi - - - -## ------------------------- ## -## Locate command utilities. ## -## ------------------------- ## - - -# func_executable_p FILE -# ---------------------- -# Check that FILE is an executable regular file. -func_executable_p () -{ - test -f "$1" && test -x "$1" -} - - -# func_path_progs PROGS_LIST CHECK_FUNC [PATH] -# -------------------------------------------- -# Search for either a program that responds to --version with output -# containing "GNU", or else returned by CHECK_FUNC otherwise, by -# trying all the directories in PATH with each of the elements of -# PROGS_LIST. -# -# CHECK_FUNC should accept the path to a candidate program, and -# set $func_check_prog_result if it truncates its output less than -# $_G_path_prog_max characters. -func_path_progs () -{ - _G_progs_list=$1 - _G_check_func=$2 - _G_PATH=${3-"$PATH"} - - _G_path_prog_max=0 - _G_path_prog_found=false - _G_save_IFS=$IFS; IFS=${PATH_SEPARATOR-:} - for _G_dir in $_G_PATH; do - IFS=$_G_save_IFS - test -z "$_G_dir" && _G_dir=. - for _G_prog_name in $_G_progs_list; do - for _exeext in '' .EXE; do - _G_path_prog=$_G_dir/$_G_prog_name$_exeext - func_executable_p "$_G_path_prog" || continue - case `"$_G_path_prog" --version 2>&1` in - *GNU*) func_path_progs_result=$_G_path_prog _G_path_prog_found=: ;; - *) $_G_check_func $_G_path_prog - func_path_progs_result=$func_check_prog_result - ;; - esac - $_G_path_prog_found && break 3 - done - done - done - IFS=$_G_save_IFS - test -z "$func_path_progs_result" && { - echo "no acceptable sed could be found in \$PATH" >&2 - exit 1 - } -} - - -# We want to be able to use the functions in this file before configure -# has figured out where the best binaries are kept, which means we have -# to search for them ourselves - except when the results are already set -# where we skip the searches. - -# Unless the user overrides by setting SED, search the path for either GNU -# sed, or the sed that truncates its output the least. -test -z "$SED" && { - _G_sed_script=s/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb/ - for _G_i in 1 2 3 4 5 6 7; do - _G_sed_script=$_G_sed_script$nl$_G_sed_script - done - echo "$_G_sed_script" 2>/dev/null | sed 99q >conftest.sed - _G_sed_script= - - func_check_prog_sed () - { - _G_path_prog=$1 - - _G_count=0 - printf 0123456789 >conftest.in - while : - do - cat conftest.in conftest.in >conftest.tmp - mv conftest.tmp conftest.in - cp conftest.in conftest.nl - echo '' >> conftest.nl - "$_G_path_prog" -f conftest.sed conftest.out 2>/dev/null || break - diff conftest.out conftest.nl >/dev/null 2>&1 || break - _G_count=`expr $_G_count + 1` - if test "$_G_count" -gt "$_G_path_prog_max"; then - # Best one so far, save it but keep looking for a better one - func_check_prog_result=$_G_path_prog - _G_path_prog_max=$_G_count - fi - # 10*(2^10) chars as input seems more than enough - test 10 -lt "$_G_count" && break - done - rm -f conftest.in conftest.tmp conftest.nl conftest.out - } - - func_path_progs "sed gsed" func_check_prog_sed $PATH:/usr/xpg4/bin - rm -f conftest.sed - SED=$func_path_progs_result -} - - -# Unless the user overrides by setting GREP, search the path for either GNU -# grep, or the grep that truncates its output the least. -test -z "$GREP" && { - func_check_prog_grep () - { - _G_path_prog=$1 - - _G_count=0 - _G_path_prog_max=0 - printf 0123456789 >conftest.in - while : - do - cat conftest.in conftest.in >conftest.tmp - mv conftest.tmp conftest.in - cp conftest.in conftest.nl - echo 'GREP' >> conftest.nl - "$_G_path_prog" -e 'GREP$' -e '-(cannot match)-' conftest.out 2>/dev/null || break - diff conftest.out conftest.nl >/dev/null 2>&1 || break - _G_count=`expr $_G_count + 1` - if test "$_G_count" -gt "$_G_path_prog_max"; then - # Best one so far, save it but keep looking for a better one - func_check_prog_result=$_G_path_prog - _G_path_prog_max=$_G_count - fi - # 10*(2^10) chars as input seems more than enough - test 10 -lt "$_G_count" && break - done - rm -f conftest.in conftest.tmp conftest.nl conftest.out - } - - func_path_progs "grep ggrep" func_check_prog_grep $PATH:/usr/xpg4/bin - GREP=$func_path_progs_result -} - - -## ------------------------------- ## -## User overridable command paths. ## -## ------------------------------- ## - -# All uppercase variable names are used for environment variables. These -# variables can be overridden by the user before calling a script that -# uses them if a suitable command of that name is not already available -# in the command search PATH. - -: ${CP="cp -f"} -: ${ECHO="printf %s\n"} -: ${EGREP="$GREP -E"} -: ${FGREP="$GREP -F"} -: ${LN_S="ln -s"} -: ${MAKE="make"} -: ${MKDIR="mkdir"} -: ${MV="mv -f"} -: ${RM="rm -f"} -: ${SHELL="${CONFIG_SHELL-/bin/sh}"} - - -## -------------------- ## -## Useful sed snippets. ## -## -------------------- ## - -sed_dirname='s|/[^/]*$||' -sed_basename='s|^.*/||' - -# Sed substitution that helps us do robust quoting. It backslashifies -# metacharacters that are still active within double-quoted strings. -sed_quote_subst='s|\([`"$\\]\)|\\\1|g' - -# Same as above, but do not quote variable references. -sed_double_quote_subst='s/\(["`\\]\)/\\\1/g' - -# Sed substitution that turns a string into a regex matching for the -# string literally. -sed_make_literal_regex='s|[].[^$\\*\/]|\\&|g' - -# Sed substitution that converts a w32 file name or path -# that contains forward slashes, into one that contains -# (escaped) backslashes. A very naive implementation. -sed_naive_backslashify='s|\\\\*|\\|g;s|/|\\|g;s|\\|\\\\|g' - -# Re-'\' parameter expansions in output of sed_double_quote_subst that -# were '\'-ed in input to the same. If an odd number of '\' preceded a -# '$' in input to sed_double_quote_subst, that '$' was protected from -# expansion. Since each input '\' is now two '\'s, look for any number -# of runs of four '\'s followed by two '\'s and then a '$'. '\' that '$'. -_G_bs='\\' -_G_bs2='\\\\' -_G_bs4='\\\\\\\\' -_G_dollar='\$' -sed_double_backslash="\ - s/$_G_bs4/&\\ -/g - s/^$_G_bs2$_G_dollar/$_G_bs&/ - s/\\([^$_G_bs]\\)$_G_bs2$_G_dollar/\\1$_G_bs2$_G_bs$_G_dollar/g - s/\n//g" - - -## ----------------- ## -## Global variables. ## -## ----------------- ## - -# Except for the global variables explicitly listed below, the following -# functions in the '^func_' namespace, and the '^require_' namespace -# variables initialised in the 'Resource management' section, sourcing -# this file will not pollute your global namespace with anything -# else. There's no portable way to scope variables in Bourne shell -# though, so actually running these functions will sometimes place -# results into a variable named after the function, and often use -# temporary variables in the '^_G_' namespace. If you are careful to -# avoid using those namespaces casually in your sourcing script, things -# should continue to work as you expect. And, of course, you can freely -# overwrite any of the functions or variables defined here before -# calling anything to customize them. - -EXIT_SUCCESS=0 -EXIT_FAILURE=1 -EXIT_MISMATCH=63 # $? = 63 is used to indicate version mismatch to missing. -EXIT_SKIP=77 # $? = 77 is used to indicate a skipped test to automake. - -# Allow overriding, eg assuming that you follow the convention of -# putting '$debug_cmd' at the start of all your functions, you can get -# bash to show function call trace with: -# -# debug_cmd='eval echo "${FUNCNAME[0]} $*" >&2' bash your-script-name -debug_cmd=${debug_cmd-":"} -exit_cmd=: - -# By convention, finish your script with: -# -# exit $exit_status -# -# so that you can set exit_status to non-zero if you want to indicate -# something went wrong during execution without actually bailing out at -# the point of failure. -exit_status=$EXIT_SUCCESS - -# Work around backward compatibility issue on IRIX 6.5. On IRIX 6.4+, sh -# is ksh but when the shell is invoked as "sh" and the current value of -# the _XPG environment variable is not equal to 1 (one), the special -# positional parameter $0, within a function call, is the name of the -# function. -progpath=$0 - -# The name of this program. -progname=`$ECHO "$progpath" |$SED "$sed_basename"` - -# Make sure we have an absolute progpath for reexecution: -case $progpath in - [\\/]*|[A-Za-z]:\\*) ;; - *[\\/]*) - progdir=`$ECHO "$progpath" |$SED "$sed_dirname"` - progdir=`cd "$progdir" && pwd` - progpath=$progdir/$progname - ;; - *) - _G_IFS=$IFS - IFS=${PATH_SEPARATOR-:} - for progdir in $PATH; do - IFS=$_G_IFS - test -x "$progdir/$progname" && break - done - IFS=$_G_IFS - test -n "$progdir" || progdir=`pwd` - progpath=$progdir/$progname - ;; -esac - - -## ----------------- ## -## Standard options. ## -## ----------------- ## - -# The following options affect the operation of the functions defined -# below, and should be set appropriately depending on run-time para- -# meters passed on the command line. - -opt_dry_run=false -opt_quiet=false -opt_verbose=false - -# Categories 'all' and 'none' are always available. Append any others -# you will pass as the first argument to func_warning from your own -# code. -warning_categories= - -# By default, display warnings according to 'opt_warning_types'. Set -# 'warning_func' to ':' to elide all warnings, or func_fatal_error to -# treat the next displayed warning as a fatal error. -warning_func=func_warn_and_continue - -# Set to 'all' to display all warnings, 'none' to suppress all -# warnings, or a space delimited list of some subset of -# 'warning_categories' to display only the listed warnings. -opt_warning_types=all - - -## -------------------- ## -## Resource management. ## -## -------------------- ## - -# This section contains definitions for functions that each ensure a -# particular resource (a file, or a non-empty configuration variable for -# example) is available, and if appropriate to extract default values -# from pertinent package files. Call them using their associated -# 'require_*' variable to ensure that they are executed, at most, once. -# -# It's entirely deliberate that calling these functions can set -# variables that don't obey the namespace limitations obeyed by the rest -# of this file, in order that that they be as useful as possible to -# callers. - - -# require_term_colors -# ------------------- -# Allow display of bold text on terminals that support it. -require_term_colors=func_require_term_colors -func_require_term_colors () -{ - $debug_cmd - - test -t 1 && { - # COLORTERM and USE_ANSI_COLORS environment variables take - # precedence, because most terminfo databases neglect to describe - # whether color sequences are supported. - test -n "${COLORTERM+set}" && : ${USE_ANSI_COLORS="1"} - - if test 1 = "$USE_ANSI_COLORS"; then - # Standard ANSI escape sequences - tc_reset='' - tc_bold=''; tc_standout='' - tc_red=''; tc_green='' - tc_blue=''; tc_cyan='' - else - # Otherwise trust the terminfo database after all. - test -n "`tput sgr0 2>/dev/null`" && { - tc_reset=`tput sgr0` - test -n "`tput bold 2>/dev/null`" && tc_bold=`tput bold` - tc_standout=$tc_bold - test -n "`tput smso 2>/dev/null`" && tc_standout=`tput smso` - test -n "`tput setaf 1 2>/dev/null`" && tc_red=`tput setaf 1` - test -n "`tput setaf 2 2>/dev/null`" && tc_green=`tput setaf 2` - test -n "`tput setaf 4 2>/dev/null`" && tc_blue=`tput setaf 4` - test -n "`tput setaf 5 2>/dev/null`" && tc_cyan=`tput setaf 5` - } - fi - } - - require_term_colors=: -} - - -## ----------------- ## -## Function library. ## -## ----------------- ## - -# This section contains a variety of useful functions to call in your -# scripts. Take note of the portable wrappers for features provided by -# some modern shells, which will fall back to slower equivalents on -# less featureful shells. - - -# func_append VAR VALUE -# --------------------- -# Append VALUE onto the existing contents of VAR. - - # We should try to minimise forks, especially on Windows where they are - # unreasonably slow, so skip the feature probes when bash or zsh are - # being used: - if test set = "${BASH_VERSION+set}${ZSH_VERSION+set}"; then - : ${_G_HAVE_ARITH_OP="yes"} - : ${_G_HAVE_XSI_OPS="yes"} - # The += operator was introduced in bash 3.1 - case $BASH_VERSION in - [12].* | 3.0 | 3.0*) ;; - *) - : ${_G_HAVE_PLUSEQ_OP="yes"} - ;; - esac - fi - - # _G_HAVE_PLUSEQ_OP - # Can be empty, in which case the shell is probed, "yes" if += is - # useable or anything else if it does not work. - test -z "$_G_HAVE_PLUSEQ_OP" \ - && (eval 'x=a; x+=" b"; test "a b" = "$x"') 2>/dev/null \ - && _G_HAVE_PLUSEQ_OP=yes - -if test yes = "$_G_HAVE_PLUSEQ_OP" -then - # This is an XSI compatible shell, allowing a faster implementation... - eval 'func_append () - { - $debug_cmd - - eval "$1+=\$2" - }' -else - # ...otherwise fall back to using expr, which is often a shell builtin. - func_append () - { - $debug_cmd - - eval "$1=\$$1\$2" - } -fi - - -# func_append_quoted VAR VALUE -# ---------------------------- -# Quote VALUE and append to the end of shell variable VAR, separated -# by a space. -if test yes = "$_G_HAVE_PLUSEQ_OP"; then - eval 'func_append_quoted () - { - $debug_cmd - - func_quote_for_eval "$2" - eval "$1+=\\ \$func_quote_for_eval_result" - }' -else - func_append_quoted () - { - $debug_cmd - - func_quote_for_eval "$2" - eval "$1=\$$1\\ \$func_quote_for_eval_result" - } -fi - - -# func_append_uniq VAR VALUE -# -------------------------- -# Append unique VALUE onto the existing contents of VAR, assuming -# entries are delimited by the first character of VALUE. For example: -# -# func_append_uniq options " --another-option option-argument" -# -# will only append to $options if " --another-option option-argument " -# is not already present somewhere in $options already (note spaces at -# each end implied by leading space in second argument). -func_append_uniq () -{ - $debug_cmd - - eval _G_current_value='`$ECHO $'$1'`' - _G_delim=`expr "$2" : '\(.\)'` - - case $_G_delim$_G_current_value$_G_delim in - *"$2$_G_delim"*) ;; - *) func_append "$@" ;; - esac -} - - -# func_arith TERM... -# ------------------ -# Set func_arith_result to the result of evaluating TERMs. - test -z "$_G_HAVE_ARITH_OP" \ - && (eval 'test 2 = $(( 1 + 1 ))') 2>/dev/null \ - && _G_HAVE_ARITH_OP=yes - -if test yes = "$_G_HAVE_ARITH_OP"; then - eval 'func_arith () - { - $debug_cmd - - func_arith_result=$(( $* )) - }' -else - func_arith () - { - $debug_cmd - - func_arith_result=`expr "$@"` - } -fi - - -# func_basename FILE -# ------------------ -# Set func_basename_result to FILE with everything up to and including -# the last / stripped. -if test yes = "$_G_HAVE_XSI_OPS"; then - # If this shell supports suffix pattern removal, then use it to avoid - # forking. Hide the definitions single quotes in case the shell chokes - # on unsupported syntax... - _b='func_basename_result=${1##*/}' - _d='case $1 in - */*) func_dirname_result=${1%/*}$2 ;; - * ) func_dirname_result=$3 ;; - esac' - -else - # ...otherwise fall back to using sed. - _b='func_basename_result=`$ECHO "$1" |$SED "$sed_basename"`' - _d='func_dirname_result=`$ECHO "$1" |$SED "$sed_dirname"` - if test "X$func_dirname_result" = "X$1"; then - func_dirname_result=$3 - else - func_append func_dirname_result "$2" - fi' -fi - -eval 'func_basename () -{ - $debug_cmd - - '"$_b"' -}' - - -# func_dirname FILE APPEND NONDIR_REPLACEMENT -# ------------------------------------------- -# Compute the dirname of FILE. If nonempty, add APPEND to the result, -# otherwise set result to NONDIR_REPLACEMENT. -eval 'func_dirname () -{ - $debug_cmd - - '"$_d"' -}' - - -# func_dirname_and_basename FILE APPEND NONDIR_REPLACEMENT -# -------------------------------------------------------- -# Perform func_basename and func_dirname in a single function -# call: -# dirname: Compute the dirname of FILE. If nonempty, -# add APPEND to the result, otherwise set result -# to NONDIR_REPLACEMENT. -# value returned in "$func_dirname_result" -# basename: Compute filename of FILE. -# value retuned in "$func_basename_result" -# For efficiency, we do not delegate to the functions above but instead -# duplicate the functionality here. -eval 'func_dirname_and_basename () -{ - $debug_cmd - - '"$_b"' - '"$_d"' -}' - - -# func_echo ARG... -# ---------------- -# Echo program name prefixed message. -func_echo () -{ - $debug_cmd - - _G_message=$* - - func_echo_IFS=$IFS - IFS=$nl - for _G_line in $_G_message; do - IFS=$func_echo_IFS - $ECHO "$progname: $_G_line" - done - IFS=$func_echo_IFS -} - - -# func_echo_all ARG... -# -------------------- -# Invoke $ECHO with all args, space-separated. -func_echo_all () -{ - $ECHO "$*" -} - - -# func_echo_infix_1 INFIX ARG... -# ------------------------------ -# Echo program name, followed by INFIX on the first line, with any -# additional lines not showing INFIX. -func_echo_infix_1 () -{ - $debug_cmd - - $require_term_colors - - _G_infix=$1; shift - _G_indent=$_G_infix - _G_prefix="$progname: $_G_infix: " - _G_message=$* - - # Strip color escape sequences before counting printable length - for _G_tc in "$tc_reset" "$tc_bold" "$tc_standout" "$tc_red" "$tc_green" "$tc_blue" "$tc_cyan" - do - test -n "$_G_tc" && { - _G_esc_tc=`$ECHO "$_G_tc" | $SED "$sed_make_literal_regex"` - _G_indent=`$ECHO "$_G_indent" | $SED "s|$_G_esc_tc||g"` - } - done - _G_indent="$progname: "`echo "$_G_indent" | $SED 's|.| |g'`" " ## exclude from sc_prohibit_nested_quotes - - func_echo_infix_1_IFS=$IFS - IFS=$nl - for _G_line in $_G_message; do - IFS=$func_echo_infix_1_IFS - $ECHO "$_G_prefix$tc_bold$_G_line$tc_reset" >&2 - _G_prefix=$_G_indent - done - IFS=$func_echo_infix_1_IFS -} - - -# func_error ARG... -# ----------------- -# Echo program name prefixed message to standard error. -func_error () -{ - $debug_cmd - - $require_term_colors - - func_echo_infix_1 " $tc_standout${tc_red}error$tc_reset" "$*" >&2 -} - - -# func_fatal_error ARG... -# ----------------------- -# Echo program name prefixed message to standard error, and exit. -func_fatal_error () -{ - $debug_cmd - - func_error "$*" - exit $EXIT_FAILURE -} - - -# func_grep EXPRESSION FILENAME -# ----------------------------- -# Check whether EXPRESSION matches any line of FILENAME, without output. -func_grep () -{ - $debug_cmd - - $GREP "$1" "$2" >/dev/null 2>&1 -} - - -# func_len STRING -# --------------- -# Set func_len_result to the length of STRING. STRING may not -# start with a hyphen. - test -z "$_G_HAVE_XSI_OPS" \ - && (eval 'x=a/b/c; - test 5aa/bb/cc = "${#x}${x%%/*}${x%/*}${x#*/}${x##*/}"') 2>/dev/null \ - && _G_HAVE_XSI_OPS=yes - -if test yes = "$_G_HAVE_XSI_OPS"; then - eval 'func_len () - { - $debug_cmd - - func_len_result=${#1} - }' -else - func_len () - { - $debug_cmd - - func_len_result=`expr "$1" : ".*" 2>/dev/null || echo $max_cmd_len` - } -fi - - -# func_mkdir_p DIRECTORY-PATH -# --------------------------- -# Make sure the entire path to DIRECTORY-PATH is available. -func_mkdir_p () -{ - $debug_cmd - - _G_directory_path=$1 - _G_dir_list= - - if test -n "$_G_directory_path" && test : != "$opt_dry_run"; then - - # Protect directory names starting with '-' - case $_G_directory_path in - -*) _G_directory_path=./$_G_directory_path ;; - esac - - # While some portion of DIR does not yet exist... - while test ! -d "$_G_directory_path"; do - # ...make a list in topmost first order. Use a colon delimited - # list incase some portion of path contains whitespace. - _G_dir_list=$_G_directory_path:$_G_dir_list - - # If the last portion added has no slash in it, the list is done - case $_G_directory_path in */*) ;; *) break ;; esac - - # ...otherwise throw away the child directory and loop - _G_directory_path=`$ECHO "$_G_directory_path" | $SED -e "$sed_dirname"` - done - _G_dir_list=`$ECHO "$_G_dir_list" | $SED 's|:*$||'` - - func_mkdir_p_IFS=$IFS; IFS=: - for _G_dir in $_G_dir_list; do - IFS=$func_mkdir_p_IFS - # mkdir can fail with a 'File exist' error if two processes - # try to create one of the directories concurrently. Don't - # stop in that case! - $MKDIR "$_G_dir" 2>/dev/null || : - done - IFS=$func_mkdir_p_IFS - - # Bail out if we (or some other process) failed to create a directory. - test -d "$_G_directory_path" || \ - func_fatal_error "Failed to create '$1'" - fi -} - - -# func_mktempdir [BASENAME] -# ------------------------- -# Make a temporary directory that won't clash with other running -# libtool processes, and avoids race conditions if possible. If -# given, BASENAME is the basename for that directory. -func_mktempdir () -{ - $debug_cmd - - _G_template=${TMPDIR-/tmp}/${1-$progname} - - if test : = "$opt_dry_run"; then - # Return a directory name, but don't create it in dry-run mode - _G_tmpdir=$_G_template-$$ - else - - # If mktemp works, use that first and foremost - _G_tmpdir=`mktemp -d "$_G_template-XXXXXXXX" 2>/dev/null` - - if test ! -d "$_G_tmpdir"; then - # Failing that, at least try and use $RANDOM to avoid a race - _G_tmpdir=$_G_template-${RANDOM-0}$$ - - func_mktempdir_umask=`umask` - umask 0077 - $MKDIR "$_G_tmpdir" - umask $func_mktempdir_umask - fi - - # If we're not in dry-run mode, bomb out on failure - test -d "$_G_tmpdir" || \ - func_fatal_error "cannot create temporary directory '$_G_tmpdir'" - fi - - $ECHO "$_G_tmpdir" -} - - -# func_normal_abspath PATH -# ------------------------ -# Remove doubled-up and trailing slashes, "." path components, -# and cancel out any ".." path components in PATH after making -# it an absolute path. -func_normal_abspath () -{ - $debug_cmd - - # These SED scripts presuppose an absolute path with a trailing slash. - _G_pathcar='s|^/\([^/]*\).*$|\1|' - _G_pathcdr='s|^/[^/]*||' - _G_removedotparts=':dotsl - s|/\./|/|g - t dotsl - s|/\.$|/|' - _G_collapseslashes='s|/\{1,\}|/|g' - _G_finalslash='s|/*$|/|' - - # Start from root dir and reassemble the path. - func_normal_abspath_result= - func_normal_abspath_tpath=$1 - func_normal_abspath_altnamespace= - case $func_normal_abspath_tpath in - "") - # Empty path, that just means $cwd. - func_stripname '' '/' "`pwd`" - func_normal_abspath_result=$func_stripname_result - return - ;; - # The next three entries are used to spot a run of precisely - # two leading slashes without using negated character classes; - # we take advantage of case's first-match behaviour. - ///*) - # Unusual form of absolute path, do nothing. - ;; - //*) - # Not necessarily an ordinary path; POSIX reserves leading '//' - # and for example Cygwin uses it to access remote file shares - # over CIFS/SMB, so we conserve a leading double slash if found. - func_normal_abspath_altnamespace=/ - ;; - /*) - # Absolute path, do nothing. - ;; - *) - # Relative path, prepend $cwd. - func_normal_abspath_tpath=`pwd`/$func_normal_abspath_tpath - ;; - esac - - # Cancel out all the simple stuff to save iterations. We also want - # the path to end with a slash for ease of parsing, so make sure - # there is one (and only one) here. - func_normal_abspath_tpath=`$ECHO "$func_normal_abspath_tpath" | $SED \ - -e "$_G_removedotparts" -e "$_G_collapseslashes" -e "$_G_finalslash"` - while :; do - # Processed it all yet? - if test / = "$func_normal_abspath_tpath"; then - # If we ascended to the root using ".." the result may be empty now. - if test -z "$func_normal_abspath_result"; then - func_normal_abspath_result=/ - fi - break - fi - func_normal_abspath_tcomponent=`$ECHO "$func_normal_abspath_tpath" | $SED \ - -e "$_G_pathcar"` - func_normal_abspath_tpath=`$ECHO "$func_normal_abspath_tpath" | $SED \ - -e "$_G_pathcdr"` - # Figure out what to do with it - case $func_normal_abspath_tcomponent in - "") - # Trailing empty path component, ignore it. - ;; - ..) - # Parent dir; strip last assembled component from result. - func_dirname "$func_normal_abspath_result" - func_normal_abspath_result=$func_dirname_result - ;; - *) - # Actual path component, append it. - func_append func_normal_abspath_result "/$func_normal_abspath_tcomponent" - ;; - esac - done - # Restore leading double-slash if one was found on entry. - func_normal_abspath_result=$func_normal_abspath_altnamespace$func_normal_abspath_result -} - - -# func_notquiet ARG... -# -------------------- -# Echo program name prefixed message only when not in quiet mode. -func_notquiet () -{ - $debug_cmd - - $opt_quiet || func_echo ${1+"$@"} - - # A bug in bash halts the script if the last line of a function - # fails when set -e is in force, so we need another command to - # work around that: - : -} - - -# func_relative_path SRCDIR DSTDIR -# -------------------------------- -# Set func_relative_path_result to the relative path from SRCDIR to DSTDIR. -func_relative_path () -{ - $debug_cmd - - func_relative_path_result= - func_normal_abspath "$1" - func_relative_path_tlibdir=$func_normal_abspath_result - func_normal_abspath "$2" - func_relative_path_tbindir=$func_normal_abspath_result - - # Ascend the tree starting from libdir - while :; do - # check if we have found a prefix of bindir - case $func_relative_path_tbindir in - $func_relative_path_tlibdir) - # found an exact match - func_relative_path_tcancelled= - break - ;; - $func_relative_path_tlibdir*) - # found a matching prefix - func_stripname "$func_relative_path_tlibdir" '' "$func_relative_path_tbindir" - func_relative_path_tcancelled=$func_stripname_result - if test -z "$func_relative_path_result"; then - func_relative_path_result=. - fi - break - ;; - *) - func_dirname $func_relative_path_tlibdir - func_relative_path_tlibdir=$func_dirname_result - if test -z "$func_relative_path_tlibdir"; then - # Have to descend all the way to the root! - func_relative_path_result=../$func_relative_path_result - func_relative_path_tcancelled=$func_relative_path_tbindir - break - fi - func_relative_path_result=../$func_relative_path_result - ;; - esac - done - - # Now calculate path; take care to avoid doubling-up slashes. - func_stripname '' '/' "$func_relative_path_result" - func_relative_path_result=$func_stripname_result - func_stripname '/' '/' "$func_relative_path_tcancelled" - if test -n "$func_stripname_result"; then - func_append func_relative_path_result "/$func_stripname_result" - fi - - # Normalisation. If bindir is libdir, return '.' else relative path. - if test -n "$func_relative_path_result"; then - func_stripname './' '' "$func_relative_path_result" - func_relative_path_result=$func_stripname_result - fi - - test -n "$func_relative_path_result" || func_relative_path_result=. - - : -} - - -# func_quote_for_eval ARG... -# -------------------------- -# Aesthetically quote ARGs to be evaled later. -# This function returns two values: -# i) func_quote_for_eval_result -# double-quoted, suitable for a subsequent eval -# ii) func_quote_for_eval_unquoted_result -# has all characters that are still active within double -# quotes backslashified. -func_quote_for_eval () -{ - $debug_cmd - - func_quote_for_eval_unquoted_result= - func_quote_for_eval_result= - while test 0 -lt $#; do - case $1 in - *[\\\`\"\$]*) - _G_unquoted_arg=`printf '%s\n' "$1" |$SED "$sed_quote_subst"` ;; - *) - _G_unquoted_arg=$1 ;; - esac - if test -n "$func_quote_for_eval_unquoted_result"; then - func_append func_quote_for_eval_unquoted_result " $_G_unquoted_arg" - else - func_append func_quote_for_eval_unquoted_result "$_G_unquoted_arg" - fi - - case $_G_unquoted_arg in - # Double-quote args containing shell metacharacters to delay - # word splitting, command substitution and variable expansion - # for a subsequent eval. - # Many Bourne shells cannot handle close brackets correctly - # in scan sets, so we specify it separately. - *[\[\~\#\^\&\*\(\)\{\}\|\;\<\>\?\'\ \ ]*|*]*|"") - _G_quoted_arg=\"$_G_unquoted_arg\" - ;; - *) - _G_quoted_arg=$_G_unquoted_arg - ;; - esac - - if test -n "$func_quote_for_eval_result"; then - func_append func_quote_for_eval_result " $_G_quoted_arg" - else - func_append func_quote_for_eval_result "$_G_quoted_arg" - fi - shift - done -} - - -# func_quote_for_expand ARG -# ------------------------- -# Aesthetically quote ARG to be evaled later; same as above, -# but do not quote variable references. -func_quote_for_expand () -{ - $debug_cmd - - case $1 in - *[\\\`\"]*) - _G_arg=`$ECHO "$1" | $SED \ - -e "$sed_double_quote_subst" -e "$sed_double_backslash"` ;; - *) - _G_arg=$1 ;; - esac - - case $_G_arg in - # Double-quote args containing shell metacharacters to delay - # word splitting and command substitution for a subsequent eval. - # Many Bourne shells cannot handle close brackets correctly - # in scan sets, so we specify it separately. - *[\[\~\#\^\&\*\(\)\{\}\|\;\<\>\?\'\ \ ]*|*]*|"") - _G_arg=\"$_G_arg\" - ;; - esac - - func_quote_for_expand_result=$_G_arg -} - - -# func_stripname PREFIX SUFFIX NAME -# --------------------------------- -# strip PREFIX and SUFFIX from NAME, and store in func_stripname_result. -# PREFIX and SUFFIX must not contain globbing or regex special -# characters, hashes, percent signs, but SUFFIX may contain a leading -# dot (in which case that matches only a dot). -if test yes = "$_G_HAVE_XSI_OPS"; then - eval 'func_stripname () - { - $debug_cmd - - # pdksh 5.2.14 does not do ${X%$Y} correctly if both X and Y are - # positional parameters, so assign one to ordinary variable first. - func_stripname_result=$3 - func_stripname_result=${func_stripname_result#"$1"} - func_stripname_result=${func_stripname_result%"$2"} - }' -else - func_stripname () - { - $debug_cmd - - case $2 in - .*) func_stripname_result=`$ECHO "$3" | $SED -e "s%^$1%%" -e "s%\\\\$2\$%%"`;; - *) func_stripname_result=`$ECHO "$3" | $SED -e "s%^$1%%" -e "s%$2\$%%"`;; - esac - } -fi - - -# func_show_eval CMD [FAIL_EXP] -# ----------------------------- -# Unless opt_quiet is true, then output CMD. Then, if opt_dryrun is -# not true, evaluate CMD. If the evaluation of CMD fails, and FAIL_EXP -# is given, then evaluate it. -func_show_eval () -{ - $debug_cmd - - _G_cmd=$1 - _G_fail_exp=${2-':'} - - func_quote_for_expand "$_G_cmd" - eval "func_notquiet $func_quote_for_expand_result" - - $opt_dry_run || { - eval "$_G_cmd" - _G_status=$? - if test 0 -ne "$_G_status"; then - eval "(exit $_G_status); $_G_fail_exp" - fi - } -} - - -# func_show_eval_locale CMD [FAIL_EXP] -# ------------------------------------ -# Unless opt_quiet is true, then output CMD. Then, if opt_dryrun is -# not true, evaluate CMD. If the evaluation of CMD fails, and FAIL_EXP -# is given, then evaluate it. Use the saved locale for evaluation. -func_show_eval_locale () -{ - $debug_cmd - - _G_cmd=$1 - _G_fail_exp=${2-':'} - - $opt_quiet || { - func_quote_for_expand "$_G_cmd" - eval "func_echo $func_quote_for_expand_result" - } - - $opt_dry_run || { - eval "$_G_user_locale - $_G_cmd" - _G_status=$? - eval "$_G_safe_locale" - if test 0 -ne "$_G_status"; then - eval "(exit $_G_status); $_G_fail_exp" - fi - } -} - - -# func_tr_sh -# ---------- -# Turn $1 into a string suitable for a shell variable name. -# Result is stored in $func_tr_sh_result. All characters -# not in the set a-zA-Z0-9_ are replaced with '_'. Further, -# if $1 begins with a digit, a '_' is prepended as well. -func_tr_sh () -{ - $debug_cmd - - case $1 in - [0-9]* | *[!a-zA-Z0-9_]*) - func_tr_sh_result=`$ECHO "$1" | $SED -e 's/^\([0-9]\)/_\1/' -e 's/[^a-zA-Z0-9_]/_/g'` - ;; - * ) - func_tr_sh_result=$1 - ;; - esac -} - - -# func_verbose ARG... -# ------------------- -# Echo program name prefixed message in verbose mode only. -func_verbose () -{ - $debug_cmd - - $opt_verbose && func_echo "$*" - - : -} - - -# func_warn_and_continue ARG... -# ----------------------------- -# Echo program name prefixed warning message to standard error. -func_warn_and_continue () -{ - $debug_cmd - - $require_term_colors - - func_echo_infix_1 "${tc_red}warning$tc_reset" "$*" >&2 -} - - -# func_warning CATEGORY ARG... -# ---------------------------- -# Echo program name prefixed warning message to standard error. Warning -# messages can be filtered according to CATEGORY, where this function -# elides messages where CATEGORY is not listed in the global variable -# 'opt_warning_types'. -func_warning () -{ - $debug_cmd - - # CATEGORY must be in the warning_categories list! - case " $warning_categories " in - *" $1 "*) ;; - *) func_internal_error "invalid warning category '$1'" ;; - esac - - _G_category=$1 - shift - - case " $opt_warning_types " in - *" $_G_category "*) $warning_func ${1+"$@"} ;; - esac -} - - -# func_sort_ver VER1 VER2 -# ----------------------- -# 'sort -V' is not generally available. -# Note this deviates from the version comparison in automake -# in that it treats 1.5 < 1.5.0, and treats 1.4.4a < 1.4-p3a -# but this should suffice as we won't be specifying old -# version formats or redundant trailing .0 in bootstrap.conf. -# If we did want full compatibility then we should probably -# use m4_version_compare from autoconf. -func_sort_ver () -{ - $debug_cmd - - printf '%s\n%s\n' "$1" "$2" \ - | sort -t. -k 1,1n -k 2,2n -k 3,3n -k 4,4n -k 5,5n -k 6,6n -k 7,7n -k 8,8n -k 9,9n -} - -# func_lt_ver PREV CURR -# --------------------- -# Return true if PREV and CURR are in the correct order according to -# func_sort_ver, otherwise false. Use it like this: -# -# func_lt_ver "$prev_ver" "$proposed_ver" || func_fatal_error "..." -func_lt_ver () -{ - $debug_cmd - - test "x$1" = x`func_sort_ver "$1" "$2" | $SED 1q` -} - - -# Local variables: -# mode: shell-script -# sh-indentation: 2 -# eval: (add-hook 'before-save-hook 'time-stamp) -# time-stamp-pattern: "10/scriptversion=%:y-%02m-%02d.%02H; # UTC" -# time-stamp-time-zone: "UTC" -# End: -#! /bin/sh - -# Set a version string for this script. -scriptversion=2014-01-07.03; # UTC - -# A portable, pluggable option parser for Bourne shell. -# Written by Gary V. Vaughan, 2010 - -# Copyright (C) 2010-2015 Free Software Foundation, Inc. -# This is free software; see the source for copying conditions. There is NO -# warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. - -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. - -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - -# Please report bugs or propose patches to gary@gnu.org. - - -## ------ ## -## Usage. ## -## ------ ## - -# This file is a library for parsing options in your shell scripts along -# with assorted other useful supporting features that you can make use -# of too. -# -# For the simplest scripts you might need only: -# -# #!/bin/sh -# . relative/path/to/funclib.sh -# . relative/path/to/options-parser -# scriptversion=1.0 -# func_options ${1+"$@"} -# eval set dummy "$func_options_result"; shift -# ...rest of your script... -# -# In order for the '--version' option to work, you will need to have a -# suitably formatted comment like the one at the top of this file -# starting with '# Written by ' and ending with '# warranty; '. -# -# For '-h' and '--help' to work, you will also need a one line -# description of your script's purpose in a comment directly above the -# '# Written by ' line, like the one at the top of this file. -# -# The default options also support '--debug', which will turn on shell -# execution tracing (see the comment above debug_cmd below for another -# use), and '--verbose' and the func_verbose function to allow your script -# to display verbose messages only when your user has specified -# '--verbose'. -# -# After sourcing this file, you can plug processing for additional -# options by amending the variables from the 'Configuration' section -# below, and following the instructions in the 'Option parsing' -# section further down. - -## -------------- ## -## Configuration. ## -## -------------- ## - -# You should override these variables in your script after sourcing this -# file so that they reflect the customisations you have added to the -# option parser. - -# The usage line for option parsing errors and the start of '-h' and -# '--help' output messages. You can embed shell variables for delayed -# expansion at the time the message is displayed, but you will need to -# quote other shell meta-characters carefully to prevent them being -# expanded when the contents are evaled. -usage='$progpath [OPTION]...' - -# Short help message in response to '-h' and '--help'. Add to this or -# override it after sourcing this library to reflect the full set of -# options your script accepts. -usage_message="\ - --debug enable verbose shell tracing - -W, --warnings=CATEGORY - report the warnings falling in CATEGORY [all] - -v, --verbose verbosely report processing - --version print version information and exit - -h, --help print short or long help message and exit -" - -# Additional text appended to 'usage_message' in response to '--help'. -long_help_message=" -Warning categories include: - 'all' show all warnings - 'none' turn off all the warnings - 'error' warnings are treated as fatal errors" - -# Help message printed before fatal option parsing errors. -fatal_help="Try '\$progname --help' for more information." - - - -## ------------------------- ## -## Hook function management. ## -## ------------------------- ## - -# This section contains functions for adding, removing, and running hooks -# to the main code. A hook is just a named list of of function, that can -# be run in order later on. - -# func_hookable FUNC_NAME -# ----------------------- -# Declare that FUNC_NAME will run hooks added with -# 'func_add_hook FUNC_NAME ...'. -func_hookable () -{ - $debug_cmd - - func_append hookable_fns " $1" -} - - -# func_add_hook FUNC_NAME HOOK_FUNC -# --------------------------------- -# Request that FUNC_NAME call HOOK_FUNC before it returns. FUNC_NAME must -# first have been declared "hookable" by a call to 'func_hookable'. -func_add_hook () -{ - $debug_cmd - - case " $hookable_fns " in - *" $1 "*) ;; - *) func_fatal_error "'$1' does not accept hook functions." ;; - esac - - eval func_append ${1}_hooks '" $2"' -} - - -# func_remove_hook FUNC_NAME HOOK_FUNC -# ------------------------------------ -# Remove HOOK_FUNC from the list of functions called by FUNC_NAME. -func_remove_hook () -{ - $debug_cmd - - eval ${1}_hooks='`$ECHO "\$'$1'_hooks" |$SED "s| '$2'||"`' -} - - -# func_run_hooks FUNC_NAME [ARG]... -# --------------------------------- -# Run all hook functions registered to FUNC_NAME. -# It is assumed that the list of hook functions contains nothing more -# than a whitespace-delimited list of legal shell function names, and -# no effort is wasted trying to catch shell meta-characters or preserve -# whitespace. -func_run_hooks () -{ - $debug_cmd - - case " $hookable_fns " in - *" $1 "*) ;; - *) func_fatal_error "'$1' does not support hook funcions.n" ;; - esac - - eval _G_hook_fns=\$$1_hooks; shift - - for _G_hook in $_G_hook_fns; do - eval $_G_hook '"$@"' - - # store returned options list back into positional - # parameters for next 'cmd' execution. - eval _G_hook_result=\$${_G_hook}_result - eval set dummy "$_G_hook_result"; shift - done - - func_quote_for_eval ${1+"$@"} - func_run_hooks_result=$func_quote_for_eval_result -} - - - -## --------------- ## -## Option parsing. ## -## --------------- ## - -# In order to add your own option parsing hooks, you must accept the -# full positional parameter list in your hook function, remove any -# options that you action, and then pass back the remaining unprocessed -# options in '_result', escaped suitably for -# 'eval'. Like this: -# -# my_options_prep () -# { -# $debug_cmd -# -# # Extend the existing usage message. -# usage_message=$usage_message' -# -s, --silent don'\''t print informational messages -# ' -# -# func_quote_for_eval ${1+"$@"} -# my_options_prep_result=$func_quote_for_eval_result -# } -# func_add_hook func_options_prep my_options_prep -# -# -# my_silent_option () -# { -# $debug_cmd -# -# # Note that for efficiency, we parse as many options as we can -# # recognise in a loop before passing the remainder back to the -# # caller on the first unrecognised argument we encounter. -# while test $# -gt 0; do -# opt=$1; shift -# case $opt in -# --silent|-s) opt_silent=: ;; -# # Separate non-argument short options: -# -s*) func_split_short_opt "$_G_opt" -# set dummy "$func_split_short_opt_name" \ -# "-$func_split_short_opt_arg" ${1+"$@"} -# shift -# ;; -# *) set dummy "$_G_opt" "$*"; shift; break ;; -# esac -# done -# -# func_quote_for_eval ${1+"$@"} -# my_silent_option_result=$func_quote_for_eval_result -# } -# func_add_hook func_parse_options my_silent_option -# -# -# my_option_validation () -# { -# $debug_cmd -# -# $opt_silent && $opt_verbose && func_fatal_help "\ -# '--silent' and '--verbose' options are mutually exclusive." -# -# func_quote_for_eval ${1+"$@"} -# my_option_validation_result=$func_quote_for_eval_result -# } -# func_add_hook func_validate_options my_option_validation -# -# You'll alse need to manually amend $usage_message to reflect the extra -# options you parse. It's preferable to append if you can, so that -# multiple option parsing hooks can be added safely. - - -# func_options [ARG]... -# --------------------- -# All the functions called inside func_options are hookable. See the -# individual implementations for details. -func_hookable func_options -func_options () -{ - $debug_cmd - - func_options_prep ${1+"$@"} - eval func_parse_options \ - ${func_options_prep_result+"$func_options_prep_result"} - eval func_validate_options \ - ${func_parse_options_result+"$func_parse_options_result"} - - eval func_run_hooks func_options \ - ${func_validate_options_result+"$func_validate_options_result"} - - # save modified positional parameters for caller - func_options_result=$func_run_hooks_result -} - - -# func_options_prep [ARG]... -# -------------------------- -# All initialisations required before starting the option parse loop. -# Note that when calling hook functions, we pass through the list of -# positional parameters. If a hook function modifies that list, and -# needs to propogate that back to rest of this script, then the complete -# modified list must be put in 'func_run_hooks_result' before -# returning. -func_hookable func_options_prep -func_options_prep () -{ - $debug_cmd - - # Option defaults: - opt_verbose=false - opt_warning_types= - - func_run_hooks func_options_prep ${1+"$@"} - - # save modified positional parameters for caller - func_options_prep_result=$func_run_hooks_result -} - - -# func_parse_options [ARG]... -# --------------------------- -# The main option parsing loop. -func_hookable func_parse_options -func_parse_options () -{ - $debug_cmd - - func_parse_options_result= - - # this just eases exit handling - while test $# -gt 0; do - # Defer to hook functions for initial option parsing, so they - # get priority in the event of reusing an option name. - func_run_hooks func_parse_options ${1+"$@"} - - # Adjust func_parse_options positional parameters to match - eval set dummy "$func_run_hooks_result"; shift - - # Break out of the loop if we already parsed every option. - test $# -gt 0 || break - - _G_opt=$1 - shift - case $_G_opt in - --debug|-x) debug_cmd='set -x' - func_echo "enabling shell trace mode" - $debug_cmd - ;; - - --no-warnings|--no-warning|--no-warn) - set dummy --warnings none ${1+"$@"} - shift - ;; - - --warnings|--warning|-W) - test $# = 0 && func_missing_arg $_G_opt && break - case " $warning_categories $1" in - *" $1 "*) - # trailing space prevents matching last $1 above - func_append_uniq opt_warning_types " $1" - ;; - *all) - opt_warning_types=$warning_categories - ;; - *none) - opt_warning_types=none - warning_func=: - ;; - *error) - opt_warning_types=$warning_categories - warning_func=func_fatal_error - ;; - *) - func_fatal_error \ - "unsupported warning category: '$1'" - ;; - esac - shift - ;; - - --verbose|-v) opt_verbose=: ;; - --version) func_version ;; - -\?|-h) func_usage ;; - --help) func_help ;; - - # Separate optargs to long options (plugins may need this): - --*=*) func_split_equals "$_G_opt" - set dummy "$func_split_equals_lhs" \ - "$func_split_equals_rhs" ${1+"$@"} - shift - ;; - - # Separate optargs to short options: - -W*) - func_split_short_opt "$_G_opt" - set dummy "$func_split_short_opt_name" \ - "$func_split_short_opt_arg" ${1+"$@"} - shift - ;; - - # Separate non-argument short options: - -\?*|-h*|-v*|-x*) - func_split_short_opt "$_G_opt" - set dummy "$func_split_short_opt_name" \ - "-$func_split_short_opt_arg" ${1+"$@"} - shift - ;; - - --) break ;; - -*) func_fatal_help "unrecognised option: '$_G_opt'" ;; - *) set dummy "$_G_opt" ${1+"$@"}; shift; break ;; - esac - done - - # save modified positional parameters for caller - func_quote_for_eval ${1+"$@"} - func_parse_options_result=$func_quote_for_eval_result -} - - -# func_validate_options [ARG]... -# ------------------------------ -# Perform any sanity checks on option settings and/or unconsumed -# arguments. -func_hookable func_validate_options -func_validate_options () -{ - $debug_cmd - - # Display all warnings if -W was not given. - test -n "$opt_warning_types" || opt_warning_types=" $warning_categories" - - func_run_hooks func_validate_options ${1+"$@"} - - # Bail if the options were screwed! - $exit_cmd $EXIT_FAILURE - - # save modified positional parameters for caller - func_validate_options_result=$func_run_hooks_result -} - - - -## ----------------- ## -## Helper functions. ## -## ----------------- ## - -# This section contains the helper functions used by the rest of the -# hookable option parser framework in ascii-betical order. - - -# func_fatal_help ARG... -# ---------------------- -# Echo program name prefixed message to standard error, followed by -# a help hint, and exit. -func_fatal_help () -{ - $debug_cmd - - eval \$ECHO \""Usage: $usage"\" - eval \$ECHO \""$fatal_help"\" - func_error ${1+"$@"} - exit $EXIT_FAILURE -} - - -# func_help -# --------- -# Echo long help message to standard output and exit. -func_help () -{ - $debug_cmd - - func_usage_message - $ECHO "$long_help_message" - exit 0 -} - - -# func_missing_arg ARGNAME -# ------------------------ -# Echo program name prefixed message to standard error and set global -# exit_cmd. -func_missing_arg () -{ - $debug_cmd - - func_error "Missing argument for '$1'." - exit_cmd=exit -} - - -# func_split_equals STRING -# ------------------------ -# Set func_split_equals_lhs and func_split_equals_rhs shell variables after -# splitting STRING at the '=' sign. -test -z "$_G_HAVE_XSI_OPS" \ - && (eval 'x=a/b/c; - test 5aa/bb/cc = "${#x}${x%%/*}${x%/*}${x#*/}${x##*/}"') 2>/dev/null \ - && _G_HAVE_XSI_OPS=yes - -if test yes = "$_G_HAVE_XSI_OPS" -then - # This is an XSI compatible shell, allowing a faster implementation... - eval 'func_split_equals () - { - $debug_cmd - - func_split_equals_lhs=${1%%=*} - func_split_equals_rhs=${1#*=} - test "x$func_split_equals_lhs" = "x$1" \ - && func_split_equals_rhs= - }' -else - # ...otherwise fall back to using expr, which is often a shell builtin. - func_split_equals () - { - $debug_cmd - - func_split_equals_lhs=`expr "x$1" : 'x\([^=]*\)'` - func_split_equals_rhs= - test "x$func_split_equals_lhs" = "x$1" \ - || func_split_equals_rhs=`expr "x$1" : 'x[^=]*=\(.*\)$'` - } -fi #func_split_equals - - -# func_split_short_opt SHORTOPT -# ----------------------------- -# Set func_split_short_opt_name and func_split_short_opt_arg shell -# variables after splitting SHORTOPT after the 2nd character. -if test yes = "$_G_HAVE_XSI_OPS" -then - # This is an XSI compatible shell, allowing a faster implementation... - eval 'func_split_short_opt () - { - $debug_cmd - - func_split_short_opt_arg=${1#??} - func_split_short_opt_name=${1%"$func_split_short_opt_arg"} - }' -else - # ...otherwise fall back to using expr, which is often a shell builtin. - func_split_short_opt () - { - $debug_cmd - - func_split_short_opt_name=`expr "x$1" : 'x-\(.\)'` - func_split_short_opt_arg=`expr "x$1" : 'x-.\(.*\)$'` - } -fi #func_split_short_opt - - -# func_usage -# ---------- -# Echo short help message to standard output and exit. -func_usage () -{ - $debug_cmd - - func_usage_message - $ECHO "Run '$progname --help |${PAGER-more}' for full usage" - exit 0 -} - - -# func_usage_message -# ------------------ -# Echo short help message to standard output. -func_usage_message () -{ - $debug_cmd - - eval \$ECHO \""Usage: $usage"\" - echo - $SED -n 's|^# || - /^Written by/{ - x;p;x - } - h - /^Written by/q' < "$progpath" - echo - eval \$ECHO \""$usage_message"\" -} - - -# func_version -# ------------ -# Echo version message to standard output and exit. -func_version () -{ - $debug_cmd - - printf '%s\n' "$progname $scriptversion" - $SED -n ' - /(C)/!b go - :more - /\./!{ - N - s|\n# | | - b more - } - :go - /^# Written by /,/# warranty; / { - s|^# || - s|^# *$|| - s|\((C)\)[ 0-9,-]*[ ,-]\([1-9][0-9]* \)|\1 \2| - p - } - /^# Written by / { - s|^# || - p - } - /^warranty; /q' < "$progpath" - - exit $? -} - - -# Local variables: -# mode: shell-script -# sh-indentation: 2 -# eval: (add-hook 'before-save-hook 'time-stamp) -# time-stamp-pattern: "10/scriptversion=%:y-%02m-%02d.%02H; # UTC" -# time-stamp-time-zone: "UTC" -# End: - -# Set a version string. -scriptversion='(GNU libtool) 2.4.6' - - -# func_echo ARG... -# ---------------- -# Libtool also displays the current mode in messages, so override -# funclib.sh func_echo with this custom definition. -func_echo () -{ - $debug_cmd - - _G_message=$* - - func_echo_IFS=$IFS - IFS=$nl - for _G_line in $_G_message; do - IFS=$func_echo_IFS - $ECHO "$progname${opt_mode+: $opt_mode}: $_G_line" - done - IFS=$func_echo_IFS -} - - -# func_warning ARG... -# ------------------- -# Libtool warnings are not categorized, so override funclib.sh -# func_warning with this simpler definition. -func_warning () -{ - $debug_cmd - - $warning_func ${1+"$@"} -} - - -## ---------------- ## -## Options parsing. ## -## ---------------- ## - -# Hook in the functions to make sure our own options are parsed during -# the option parsing loop. - -usage='$progpath [OPTION]... [MODE-ARG]...' - -# Short help message in response to '-h'. -usage_message="Options: - --config show all configuration variables - --debug enable verbose shell tracing - -n, --dry-run display commands without modifying any files - --features display basic configuration information and exit - --mode=MODE use operation mode MODE - --no-warnings equivalent to '-Wnone' - --preserve-dup-deps don't remove duplicate dependency libraries - --quiet, --silent don't print informational messages - --tag=TAG use configuration variables from tag TAG - -v, --verbose print more informational messages than default - --version print version information - -W, --warnings=CATEGORY report the warnings falling in CATEGORY [all] - -h, --help, --help-all print short, long, or detailed help message -" - -# Additional text appended to 'usage_message' in response to '--help'. -func_help () -{ - $debug_cmd - - func_usage_message - $ECHO "$long_help_message - -MODE must be one of the following: - - clean remove files from the build directory - compile compile a source file into a libtool object - execute automatically set library path, then run a program - finish complete the installation of libtool libraries - install install libraries or executables - link create a library or an executable - uninstall remove libraries from an installed directory - -MODE-ARGS vary depending on the MODE. When passed as first option, -'--mode=MODE' may be abbreviated as 'MODE' or a unique abbreviation of that. -Try '$progname --help --mode=MODE' for a more detailed description of MODE. - -When reporting a bug, please describe a test case to reproduce it and -include the following information: - - host-triplet: $host - shell: $SHELL - compiler: $LTCC - compiler flags: $LTCFLAGS - linker: $LD (gnu? $with_gnu_ld) - version: $progname (GNU libtool) 2.4.6 - automake: `($AUTOMAKE --version) 2>/dev/null |$SED 1q` - autoconf: `($AUTOCONF --version) 2>/dev/null |$SED 1q` - -Report bugs to . -GNU libtool home page: . -General help using GNU software: ." - exit 0 -} - - -# func_lo2o OBJECT-NAME -# --------------------- -# Transform OBJECT-NAME from a '.lo' suffix to the platform specific -# object suffix. - -lo2o=s/\\.lo\$/.$objext/ -o2lo=s/\\.$objext\$/.lo/ - -if test yes = "$_G_HAVE_XSI_OPS"; then - eval 'func_lo2o () - { - case $1 in - *.lo) func_lo2o_result=${1%.lo}.$objext ;; - * ) func_lo2o_result=$1 ;; - esac - }' - - # func_xform LIBOBJ-OR-SOURCE - # --------------------------- - # Transform LIBOBJ-OR-SOURCE from a '.o' or '.c' (or otherwise) - # suffix to a '.lo' libtool-object suffix. - eval 'func_xform () - { - func_xform_result=${1%.*}.lo - }' -else - # ...otherwise fall back to using sed. - func_lo2o () - { - func_lo2o_result=`$ECHO "$1" | $SED "$lo2o"` - } - - func_xform () - { - func_xform_result=`$ECHO "$1" | $SED 's|\.[^.]*$|.lo|'` - } -fi - - -# func_fatal_configuration ARG... -# ------------------------------- -# Echo program name prefixed message to standard error, followed by -# a configuration failure hint, and exit. -func_fatal_configuration () -{ - func__fatal_error ${1+"$@"} \ - "See the $PACKAGE documentation for more information." \ - "Fatal configuration error." -} - - -# func_config -# ----------- -# Display the configuration for all the tags in this script. -func_config () -{ - re_begincf='^# ### BEGIN LIBTOOL' - re_endcf='^# ### END LIBTOOL' - - # Default configuration. - $SED "1,/$re_begincf CONFIG/d;/$re_endcf CONFIG/,\$d" < "$progpath" - - # Now print the configurations for the tags. - for tagname in $taglist; do - $SED -n "/$re_begincf TAG CONFIG: $tagname\$/,/$re_endcf TAG CONFIG: $tagname\$/p" < "$progpath" - done - - exit $? -} - - -# func_features -# ------------- -# Display the features supported by this script. -func_features () -{ - echo "host: $host" - if test yes = "$build_libtool_libs"; then - echo "enable shared libraries" - else - echo "disable shared libraries" - fi - if test yes = "$build_old_libs"; then - echo "enable static libraries" - else - echo "disable static libraries" - fi - - exit $? -} - - -# func_enable_tag TAGNAME -# ----------------------- -# Verify that TAGNAME is valid, and either flag an error and exit, or -# enable the TAGNAME tag. We also add TAGNAME to the global $taglist -# variable here. -func_enable_tag () -{ - # Global variable: - tagname=$1 - - re_begincf="^# ### BEGIN LIBTOOL TAG CONFIG: $tagname\$" - re_endcf="^# ### END LIBTOOL TAG CONFIG: $tagname\$" - sed_extractcf=/$re_begincf/,/$re_endcf/p - - # Validate tagname. - case $tagname in - *[!-_A-Za-z0-9,/]*) - func_fatal_error "invalid tag name: $tagname" - ;; - esac - - # Don't test for the "default" C tag, as we know it's - # there but not specially marked. - case $tagname in - CC) ;; - *) - if $GREP "$re_begincf" "$progpath" >/dev/null 2>&1; then - taglist="$taglist $tagname" - - # Evaluate the configuration. Be careful to quote the path - # and the sed script, to avoid splitting on whitespace, but - # also don't use non-portable quotes within backquotes within - # quotes we have to do it in 2 steps: - extractedcf=`$SED -n -e "$sed_extractcf" < "$progpath"` - eval "$extractedcf" - else - func_error "ignoring unknown tag $tagname" - fi - ;; - esac -} - - -# func_check_version_match -# ------------------------ -# Ensure that we are using m4 macros, and libtool script from the same -# release of libtool. -func_check_version_match () -{ - if test "$package_revision" != "$macro_revision"; then - if test "$VERSION" != "$macro_version"; then - if test -z "$macro_version"; then - cat >&2 <<_LT_EOF -$progname: Version mismatch error. This is $PACKAGE $VERSION, but the -$progname: definition of this LT_INIT comes from an older release. -$progname: You should recreate aclocal.m4 with macros from $PACKAGE $VERSION -$progname: and run autoconf again. -_LT_EOF - else - cat >&2 <<_LT_EOF -$progname: Version mismatch error. This is $PACKAGE $VERSION, but the -$progname: definition of this LT_INIT comes from $PACKAGE $macro_version. -$progname: You should recreate aclocal.m4 with macros from $PACKAGE $VERSION -$progname: and run autoconf again. -_LT_EOF - fi - else - cat >&2 <<_LT_EOF -$progname: Version mismatch error. This is $PACKAGE $VERSION, revision $package_revision, -$progname: but the definition of this LT_INIT comes from revision $macro_revision. -$progname: You should recreate aclocal.m4 with macros from revision $package_revision -$progname: of $PACKAGE $VERSION and run autoconf again. -_LT_EOF - fi - - exit $EXIT_MISMATCH - fi -} - - -# libtool_options_prep [ARG]... -# ----------------------------- -# Preparation for options parsed by libtool. -libtool_options_prep () -{ - $debug_mode - - # Option defaults: - opt_config=false - opt_dlopen= - opt_dry_run=false - opt_help=false - opt_mode= - opt_preserve_dup_deps=false - opt_quiet=false - - nonopt= - preserve_args= - - # Shorthand for --mode=foo, only valid as the first argument - case $1 in - clean|clea|cle|cl) - shift; set dummy --mode clean ${1+"$@"}; shift - ;; - compile|compil|compi|comp|com|co|c) - shift; set dummy --mode compile ${1+"$@"}; shift - ;; - execute|execut|execu|exec|exe|ex|e) - shift; set dummy --mode execute ${1+"$@"}; shift - ;; - finish|finis|fini|fin|fi|f) - shift; set dummy --mode finish ${1+"$@"}; shift - ;; - install|instal|insta|inst|ins|in|i) - shift; set dummy --mode install ${1+"$@"}; shift - ;; - link|lin|li|l) - shift; set dummy --mode link ${1+"$@"}; shift - ;; - uninstall|uninstal|uninsta|uninst|unins|unin|uni|un|u) - shift; set dummy --mode uninstall ${1+"$@"}; shift - ;; - esac - - # Pass back the list of options. - func_quote_for_eval ${1+"$@"} - libtool_options_prep_result=$func_quote_for_eval_result -} -func_add_hook func_options_prep libtool_options_prep - - -# libtool_parse_options [ARG]... -# --------------------------------- -# Provide handling for libtool specific options. -libtool_parse_options () -{ - $debug_cmd - - # Perform our own loop to consume as many options as possible in - # each iteration. - while test $# -gt 0; do - _G_opt=$1 - shift - case $_G_opt in - --dry-run|--dryrun|-n) - opt_dry_run=: - ;; - - --config) func_config ;; - - --dlopen|-dlopen) - opt_dlopen="${opt_dlopen+$opt_dlopen -}$1" - shift - ;; - - --preserve-dup-deps) - opt_preserve_dup_deps=: ;; - - --features) func_features ;; - - --finish) set dummy --mode finish ${1+"$@"}; shift ;; - - --help) opt_help=: ;; - - --help-all) opt_help=': help-all' ;; - - --mode) test $# = 0 && func_missing_arg $_G_opt && break - opt_mode=$1 - case $1 in - # Valid mode arguments: - clean|compile|execute|finish|install|link|relink|uninstall) ;; - - # Catch anything else as an error - *) func_error "invalid argument for $_G_opt" - exit_cmd=exit - break - ;; - esac - shift - ;; - - --no-silent|--no-quiet) - opt_quiet=false - func_append preserve_args " $_G_opt" - ;; - - --no-warnings|--no-warning|--no-warn) - opt_warning=false - func_append preserve_args " $_G_opt" - ;; - - --no-verbose) - opt_verbose=false - func_append preserve_args " $_G_opt" - ;; - - --silent|--quiet) - opt_quiet=: - opt_verbose=false - func_append preserve_args " $_G_opt" - ;; - - --tag) test $# = 0 && func_missing_arg $_G_opt && break - opt_tag=$1 - func_append preserve_args " $_G_opt $1" - func_enable_tag "$1" - shift - ;; - - --verbose|-v) opt_quiet=false - opt_verbose=: - func_append preserve_args " $_G_opt" - ;; - - # An option not handled by this hook function: - *) set dummy "$_G_opt" ${1+"$@"}; shift; break ;; - esac - done - - - # save modified positional parameters for caller - func_quote_for_eval ${1+"$@"} - libtool_parse_options_result=$func_quote_for_eval_result -} -func_add_hook func_parse_options libtool_parse_options - - - -# libtool_validate_options [ARG]... -# --------------------------------- -# Perform any sanity checks on option settings and/or unconsumed -# arguments. -libtool_validate_options () -{ - # save first non-option argument - if test 0 -lt $#; then - nonopt=$1 - shift - fi - - # preserve --debug - test : = "$debug_cmd" || func_append preserve_args " --debug" - - case $host in - # Solaris2 added to fix http://debbugs.gnu.org/cgi/bugreport.cgi?bug=16452 - # see also: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59788 - *cygwin* | *mingw* | *pw32* | *cegcc* | *solaris2* | *os2*) - # don't eliminate duplications in $postdeps and $predeps - opt_duplicate_compiler_generated_deps=: - ;; - *) - opt_duplicate_compiler_generated_deps=$opt_preserve_dup_deps - ;; - esac - - $opt_help || { - # Sanity checks first: - func_check_version_match - - test yes != "$build_libtool_libs" \ - && test yes != "$build_old_libs" \ - && func_fatal_configuration "not configured to build any kind of library" - - # Darwin sucks - eval std_shrext=\"$shrext_cmds\" - - # Only execute mode is allowed to have -dlopen flags. - if test -n "$opt_dlopen" && test execute != "$opt_mode"; then - func_error "unrecognized option '-dlopen'" - $ECHO "$help" 1>&2 - exit $EXIT_FAILURE - fi - - # Change the help message to a mode-specific one. - generic_help=$help - help="Try '$progname --help --mode=$opt_mode' for more information." - } - - # Pass back the unparsed argument list - func_quote_for_eval ${1+"$@"} - libtool_validate_options_result=$func_quote_for_eval_result -} -func_add_hook func_validate_options libtool_validate_options - - -# Process options as early as possible so that --help and --version -# can return quickly. -func_options ${1+"$@"} -eval set dummy "$func_options_result"; shift - - - -## ----------- ## -## Main. ## -## ----------- ## - -magic='%%%MAGIC variable%%%' -magic_exe='%%%MAGIC EXE variable%%%' - -# Global variables. -extracted_archives= -extracted_serial=0 - -# If this variable is set in any of the actions, the command in it -# will be execed at the end. This prevents here-documents from being -# left over by shells. -exec_cmd= - - -# A function that is used when there is no print builtin or printf. -func_fallback_echo () -{ - eval 'cat <<_LTECHO_EOF -$1 -_LTECHO_EOF' -} - -# func_generated_by_libtool -# True iff stdin has been generated by Libtool. This function is only -# a basic sanity check; it will hardly flush out determined imposters. -func_generated_by_libtool_p () -{ - $GREP "^# Generated by .*$PACKAGE" > /dev/null 2>&1 -} - -# func_lalib_p file -# True iff FILE is a libtool '.la' library or '.lo' object file. -# This function is only a basic sanity check; it will hardly flush out -# determined imposters. -func_lalib_p () -{ - test -f "$1" && - $SED -e 4q "$1" 2>/dev/null | func_generated_by_libtool_p -} - -# func_lalib_unsafe_p file -# True iff FILE is a libtool '.la' library or '.lo' object file. -# This function implements the same check as func_lalib_p without -# resorting to external programs. To this end, it redirects stdin and -# closes it afterwards, without saving the original file descriptor. -# As a safety measure, use it only where a negative result would be -# fatal anyway. Works if 'file' does not exist. -func_lalib_unsafe_p () -{ - lalib_p=no - if test -f "$1" && test -r "$1" && exec 5<&0 <"$1"; then - for lalib_p_l in 1 2 3 4 - do - read lalib_p_line - case $lalib_p_line in - \#\ Generated\ by\ *$PACKAGE* ) lalib_p=yes; break;; - esac - done - exec 0<&5 5<&- - fi - test yes = "$lalib_p" -} - -# func_ltwrapper_script_p file -# True iff FILE is a libtool wrapper script -# This function is only a basic sanity check; it will hardly flush out -# determined imposters. -func_ltwrapper_script_p () -{ - test -f "$1" && - $lt_truncate_bin < "$1" 2>/dev/null | func_generated_by_libtool_p -} - -# func_ltwrapper_executable_p file -# True iff FILE is a libtool wrapper executable -# This function is only a basic sanity check; it will hardly flush out -# determined imposters. -func_ltwrapper_executable_p () -{ - func_ltwrapper_exec_suffix= - case $1 in - *.exe) ;; - *) func_ltwrapper_exec_suffix=.exe ;; - esac - $GREP "$magic_exe" "$1$func_ltwrapper_exec_suffix" >/dev/null 2>&1 -} - -# func_ltwrapper_scriptname file -# Assumes file is an ltwrapper_executable -# uses $file to determine the appropriate filename for a -# temporary ltwrapper_script. -func_ltwrapper_scriptname () -{ - func_dirname_and_basename "$1" "" "." - func_stripname '' '.exe' "$func_basename_result" - func_ltwrapper_scriptname_result=$func_dirname_result/$objdir/${func_stripname_result}_ltshwrapper -} - -# func_ltwrapper_p file -# True iff FILE is a libtool wrapper script or wrapper executable -# This function is only a basic sanity check; it will hardly flush out -# determined imposters. -func_ltwrapper_p () -{ - func_ltwrapper_script_p "$1" || func_ltwrapper_executable_p "$1" -} - - -# func_execute_cmds commands fail_cmd -# Execute tilde-delimited COMMANDS. -# If FAIL_CMD is given, eval that upon failure. -# FAIL_CMD may read-access the current command in variable CMD! -func_execute_cmds () -{ - $debug_cmd - - save_ifs=$IFS; IFS='~' - for cmd in $1; do - IFS=$sp$nl - eval cmd=\"$cmd\" - IFS=$save_ifs - func_show_eval "$cmd" "${2-:}" - done - IFS=$save_ifs -} - - -# func_source file -# Source FILE, adding directory component if necessary. -# Note that it is not necessary on cygwin/mingw to append a dot to -# FILE even if both FILE and FILE.exe exist: automatic-append-.exe -# behavior happens only for exec(3), not for open(2)! Also, sourcing -# 'FILE.' does not work on cygwin managed mounts. -func_source () -{ - $debug_cmd - - case $1 in - */* | *\\*) . "$1" ;; - *) . "./$1" ;; - esac -} - - -# func_resolve_sysroot PATH -# Replace a leading = in PATH with a sysroot. Store the result into -# func_resolve_sysroot_result -func_resolve_sysroot () -{ - func_resolve_sysroot_result=$1 - case $func_resolve_sysroot_result in - =*) - func_stripname '=' '' "$func_resolve_sysroot_result" - func_resolve_sysroot_result=$lt_sysroot$func_stripname_result - ;; - esac -} - -# func_replace_sysroot PATH -# If PATH begins with the sysroot, replace it with = and -# store the result into func_replace_sysroot_result. -func_replace_sysroot () -{ - case $lt_sysroot:$1 in - ?*:"$lt_sysroot"*) - func_stripname "$lt_sysroot" '' "$1" - func_replace_sysroot_result='='$func_stripname_result - ;; - *) - # Including no sysroot. - func_replace_sysroot_result=$1 - ;; - esac -} - -# func_infer_tag arg -# Infer tagged configuration to use if any are available and -# if one wasn't chosen via the "--tag" command line option. -# Only attempt this if the compiler in the base compile -# command doesn't match the default compiler. -# arg is usually of the form 'gcc ...' -func_infer_tag () -{ - $debug_cmd - - if test -n "$available_tags" && test -z "$tagname"; then - CC_quoted= - for arg in $CC; do - func_append_quoted CC_quoted "$arg" - done - CC_expanded=`func_echo_all $CC` - CC_quoted_expanded=`func_echo_all $CC_quoted` - case $@ in - # Blanks in the command may have been stripped by the calling shell, - # but not from the CC environment variable when configure was run. - " $CC "* | "$CC "* | " $CC_expanded "* | "$CC_expanded "* | \ - " $CC_quoted"* | "$CC_quoted "* | " $CC_quoted_expanded "* | "$CC_quoted_expanded "*) ;; - # Blanks at the start of $base_compile will cause this to fail - # if we don't check for them as well. - *) - for z in $available_tags; do - if $GREP "^# ### BEGIN LIBTOOL TAG CONFIG: $z$" < "$progpath" > /dev/null; then - # Evaluate the configuration. - eval "`$SED -n -e '/^# ### BEGIN LIBTOOL TAG CONFIG: '$z'$/,/^# ### END LIBTOOL TAG CONFIG: '$z'$/p' < $progpath`" - CC_quoted= - for arg in $CC; do - # Double-quote args containing other shell metacharacters. - func_append_quoted CC_quoted "$arg" - done - CC_expanded=`func_echo_all $CC` - CC_quoted_expanded=`func_echo_all $CC_quoted` - case "$@ " in - " $CC "* | "$CC "* | " $CC_expanded "* | "$CC_expanded "* | \ - " $CC_quoted"* | "$CC_quoted "* | " $CC_quoted_expanded "* | "$CC_quoted_expanded "*) - # The compiler in the base compile command matches - # the one in the tagged configuration. - # Assume this is the tagged configuration we want. - tagname=$z - break - ;; - esac - fi - done - # If $tagname still isn't set, then no tagged configuration - # was found and let the user know that the "--tag" command - # line option must be used. - if test -z "$tagname"; then - func_echo "unable to infer tagged configuration" - func_fatal_error "specify a tag with '--tag'" -# else -# func_verbose "using $tagname tagged configuration" - fi - ;; - esac - fi -} - - - -# func_write_libtool_object output_name pic_name nonpic_name -# Create a libtool object file (analogous to a ".la" file), -# but don't create it if we're doing a dry run. -func_write_libtool_object () -{ - write_libobj=$1 - if test yes = "$build_libtool_libs"; then - write_lobj=\'$2\' - else - write_lobj=none - fi - - if test yes = "$build_old_libs"; then - write_oldobj=\'$3\' - else - write_oldobj=none - fi - - $opt_dry_run || { - cat >${write_libobj}T </dev/null` - if test "$?" -eq 0 && test -n "$func_convert_core_file_wine_to_w32_tmp"; then - func_convert_core_file_wine_to_w32_result=`$ECHO "$func_convert_core_file_wine_to_w32_tmp" | - $SED -e "$sed_naive_backslashify"` - else - func_convert_core_file_wine_to_w32_result= - fi - fi -} -# end: func_convert_core_file_wine_to_w32 - - -# func_convert_core_path_wine_to_w32 ARG -# Helper function used by path conversion functions when $build is *nix, and -# $host is mingw, cygwin, or some other w32 environment. Relies on a correctly -# configured wine environment available, with the winepath program in $build's -# $PATH. Assumes ARG has no leading or trailing path separator characters. -# -# ARG is path to be converted from $build format to win32. -# Result is available in $func_convert_core_path_wine_to_w32_result. -# Unconvertible file (directory) names in ARG are skipped; if no directory names -# are convertible, then the result may be empty. -func_convert_core_path_wine_to_w32 () -{ - $debug_cmd - - # unfortunately, winepath doesn't convert paths, only file names - func_convert_core_path_wine_to_w32_result= - if test -n "$1"; then - oldIFS=$IFS - IFS=: - for func_convert_core_path_wine_to_w32_f in $1; do - IFS=$oldIFS - func_convert_core_file_wine_to_w32 "$func_convert_core_path_wine_to_w32_f" - if test -n "$func_convert_core_file_wine_to_w32_result"; then - if test -z "$func_convert_core_path_wine_to_w32_result"; then - func_convert_core_path_wine_to_w32_result=$func_convert_core_file_wine_to_w32_result - else - func_append func_convert_core_path_wine_to_w32_result ";$func_convert_core_file_wine_to_w32_result" - fi - fi - done - IFS=$oldIFS - fi -} -# end: func_convert_core_path_wine_to_w32 - - -# func_cygpath ARGS... -# Wrapper around calling the cygpath program via LT_CYGPATH. This is used when -# when (1) $build is *nix and Cygwin is hosted via a wine environment; or (2) -# $build is MSYS and $host is Cygwin, or (3) $build is Cygwin. In case (1) or -# (2), returns the Cygwin file name or path in func_cygpath_result (input -# file name or path is assumed to be in w32 format, as previously converted -# from $build's *nix or MSYS format). In case (3), returns the w32 file name -# or path in func_cygpath_result (input file name or path is assumed to be in -# Cygwin format). Returns an empty string on error. -# -# ARGS are passed to cygpath, with the last one being the file name or path to -# be converted. -# -# Specify the absolute *nix (or w32) name to cygpath in the LT_CYGPATH -# environment variable; do not put it in $PATH. -func_cygpath () -{ - $debug_cmd - - if test -n "$LT_CYGPATH" && test -f "$LT_CYGPATH"; then - func_cygpath_result=`$LT_CYGPATH "$@" 2>/dev/null` - if test "$?" -ne 0; then - # on failure, ensure result is empty - func_cygpath_result= - fi - else - func_cygpath_result= - func_error "LT_CYGPATH is empty or specifies non-existent file: '$LT_CYGPATH'" - fi -} -#end: func_cygpath - - -# func_convert_core_msys_to_w32 ARG -# Convert file name or path ARG from MSYS format to w32 format. Return -# result in func_convert_core_msys_to_w32_result. -func_convert_core_msys_to_w32 () -{ - $debug_cmd - - # awkward: cmd appends spaces to result - func_convert_core_msys_to_w32_result=`( cmd //c echo "$1" ) 2>/dev/null | - $SED -e 's/[ ]*$//' -e "$sed_naive_backslashify"` -} -#end: func_convert_core_msys_to_w32 - - -# func_convert_file_check ARG1 ARG2 -# Verify that ARG1 (a file name in $build format) was converted to $host -# format in ARG2. Otherwise, emit an error message, but continue (resetting -# func_to_host_file_result to ARG1). -func_convert_file_check () -{ - $debug_cmd - - if test -z "$2" && test -n "$1"; then - func_error "Could not determine host file name corresponding to" - func_error " '$1'" - func_error "Continuing, but uninstalled executables may not work." - # Fallback: - func_to_host_file_result=$1 - fi -} -# end func_convert_file_check - - -# func_convert_path_check FROM_PATHSEP TO_PATHSEP FROM_PATH TO_PATH -# Verify that FROM_PATH (a path in $build format) was converted to $host -# format in TO_PATH. Otherwise, emit an error message, but continue, resetting -# func_to_host_file_result to a simplistic fallback value (see below). -func_convert_path_check () -{ - $debug_cmd - - if test -z "$4" && test -n "$3"; then - func_error "Could not determine the host path corresponding to" - func_error " '$3'" - func_error "Continuing, but uninstalled executables may not work." - # Fallback. This is a deliberately simplistic "conversion" and - # should not be "improved". See libtool.info. - if test "x$1" != "x$2"; then - lt_replace_pathsep_chars="s|$1|$2|g" - func_to_host_path_result=`echo "$3" | - $SED -e "$lt_replace_pathsep_chars"` - else - func_to_host_path_result=$3 - fi - fi -} -# end func_convert_path_check - - -# func_convert_path_front_back_pathsep FRONTPAT BACKPAT REPL ORIG -# Modifies func_to_host_path_result by prepending REPL if ORIG matches FRONTPAT -# and appending REPL if ORIG matches BACKPAT. -func_convert_path_front_back_pathsep () -{ - $debug_cmd - - case $4 in - $1 ) func_to_host_path_result=$3$func_to_host_path_result - ;; - esac - case $4 in - $2 ) func_append func_to_host_path_result "$3" - ;; - esac -} -# end func_convert_path_front_back_pathsep - - -################################################## -# $build to $host FILE NAME CONVERSION FUNCTIONS # -################################################## -# invoked via '$to_host_file_cmd ARG' -# -# In each case, ARG is the path to be converted from $build to $host format. -# Result will be available in $func_to_host_file_result. - - -# func_to_host_file ARG -# Converts the file name ARG from $build format to $host format. Return result -# in func_to_host_file_result. -func_to_host_file () -{ - $debug_cmd - - $to_host_file_cmd "$1" -} -# end func_to_host_file - - -# func_to_tool_file ARG LAZY -# converts the file name ARG from $build format to toolchain format. Return -# result in func_to_tool_file_result. If the conversion in use is listed -# in (the comma separated) LAZY, no conversion takes place. -func_to_tool_file () -{ - $debug_cmd - - case ,$2, in - *,"$to_tool_file_cmd",*) - func_to_tool_file_result=$1 - ;; - *) - $to_tool_file_cmd "$1" - func_to_tool_file_result=$func_to_host_file_result - ;; - esac -} -# end func_to_tool_file - - -# func_convert_file_noop ARG -# Copy ARG to func_to_host_file_result. -func_convert_file_noop () -{ - func_to_host_file_result=$1 -} -# end func_convert_file_noop - - -# func_convert_file_msys_to_w32 ARG -# Convert file name ARG from (mingw) MSYS to (mingw) w32 format; automatic -# conversion to w32 is not available inside the cwrapper. Returns result in -# func_to_host_file_result. -func_convert_file_msys_to_w32 () -{ - $debug_cmd - - func_to_host_file_result=$1 - if test -n "$1"; then - func_convert_core_msys_to_w32 "$1" - func_to_host_file_result=$func_convert_core_msys_to_w32_result - fi - func_convert_file_check "$1" "$func_to_host_file_result" -} -# end func_convert_file_msys_to_w32 - - -# func_convert_file_cygwin_to_w32 ARG -# Convert file name ARG from Cygwin to w32 format. Returns result in -# func_to_host_file_result. -func_convert_file_cygwin_to_w32 () -{ - $debug_cmd - - func_to_host_file_result=$1 - if test -n "$1"; then - # because $build is cygwin, we call "the" cygpath in $PATH; no need to use - # LT_CYGPATH in this case. - func_to_host_file_result=`cygpath -m "$1"` - fi - func_convert_file_check "$1" "$func_to_host_file_result" -} -# end func_convert_file_cygwin_to_w32 - - -# func_convert_file_nix_to_w32 ARG -# Convert file name ARG from *nix to w32 format. Requires a wine environment -# and a working winepath. Returns result in func_to_host_file_result. -func_convert_file_nix_to_w32 () -{ - $debug_cmd - - func_to_host_file_result=$1 - if test -n "$1"; then - func_convert_core_file_wine_to_w32 "$1" - func_to_host_file_result=$func_convert_core_file_wine_to_w32_result - fi - func_convert_file_check "$1" "$func_to_host_file_result" -} -# end func_convert_file_nix_to_w32 - - -# func_convert_file_msys_to_cygwin ARG -# Convert file name ARG from MSYS to Cygwin format. Requires LT_CYGPATH set. -# Returns result in func_to_host_file_result. -func_convert_file_msys_to_cygwin () -{ - $debug_cmd - - func_to_host_file_result=$1 - if test -n "$1"; then - func_convert_core_msys_to_w32 "$1" - func_cygpath -u "$func_convert_core_msys_to_w32_result" - func_to_host_file_result=$func_cygpath_result - fi - func_convert_file_check "$1" "$func_to_host_file_result" -} -# end func_convert_file_msys_to_cygwin - - -# func_convert_file_nix_to_cygwin ARG -# Convert file name ARG from *nix to Cygwin format. Requires Cygwin installed -# in a wine environment, working winepath, and LT_CYGPATH set. Returns result -# in func_to_host_file_result. -func_convert_file_nix_to_cygwin () -{ - $debug_cmd - - func_to_host_file_result=$1 - if test -n "$1"; then - # convert from *nix to w32, then use cygpath to convert from w32 to cygwin. - func_convert_core_file_wine_to_w32 "$1" - func_cygpath -u "$func_convert_core_file_wine_to_w32_result" - func_to_host_file_result=$func_cygpath_result - fi - func_convert_file_check "$1" "$func_to_host_file_result" -} -# end func_convert_file_nix_to_cygwin - - -############################################# -# $build to $host PATH CONVERSION FUNCTIONS # -############################################# -# invoked via '$to_host_path_cmd ARG' -# -# In each case, ARG is the path to be converted from $build to $host format. -# The result will be available in $func_to_host_path_result. -# -# Path separators are also converted from $build format to $host format. If -# ARG begins or ends with a path separator character, it is preserved (but -# converted to $host format) on output. -# -# All path conversion functions are named using the following convention: -# file name conversion function : func_convert_file_X_to_Y () -# path conversion function : func_convert_path_X_to_Y () -# where, for any given $build/$host combination the 'X_to_Y' value is the -# same. If conversion functions are added for new $build/$host combinations, -# the two new functions must follow this pattern, or func_init_to_host_path_cmd -# will break. - - -# func_init_to_host_path_cmd -# Ensures that function "pointer" variable $to_host_path_cmd is set to the -# appropriate value, based on the value of $to_host_file_cmd. -to_host_path_cmd= -func_init_to_host_path_cmd () -{ - $debug_cmd - - if test -z "$to_host_path_cmd"; then - func_stripname 'func_convert_file_' '' "$to_host_file_cmd" - to_host_path_cmd=func_convert_path_$func_stripname_result - fi -} - - -# func_to_host_path ARG -# Converts the path ARG from $build format to $host format. Return result -# in func_to_host_path_result. -func_to_host_path () -{ - $debug_cmd - - func_init_to_host_path_cmd - $to_host_path_cmd "$1" -} -# end func_to_host_path - - -# func_convert_path_noop ARG -# Copy ARG to func_to_host_path_result. -func_convert_path_noop () -{ - func_to_host_path_result=$1 -} -# end func_convert_path_noop - - -# func_convert_path_msys_to_w32 ARG -# Convert path ARG from (mingw) MSYS to (mingw) w32 format; automatic -# conversion to w32 is not available inside the cwrapper. Returns result in -# func_to_host_path_result. -func_convert_path_msys_to_w32 () -{ - $debug_cmd - - func_to_host_path_result=$1 - if test -n "$1"; then - # Remove leading and trailing path separator characters from ARG. MSYS - # behavior is inconsistent here; cygpath turns them into '.;' and ';.'; - # and winepath ignores them completely. - func_stripname : : "$1" - func_to_host_path_tmp1=$func_stripname_result - func_convert_core_msys_to_w32 "$func_to_host_path_tmp1" - func_to_host_path_result=$func_convert_core_msys_to_w32_result - func_convert_path_check : ";" \ - "$func_to_host_path_tmp1" "$func_to_host_path_result" - func_convert_path_front_back_pathsep ":*" "*:" ";" "$1" - fi -} -# end func_convert_path_msys_to_w32 - - -# func_convert_path_cygwin_to_w32 ARG -# Convert path ARG from Cygwin to w32 format. Returns result in -# func_to_host_file_result. -func_convert_path_cygwin_to_w32 () -{ - $debug_cmd - - func_to_host_path_result=$1 - if test -n "$1"; then - # See func_convert_path_msys_to_w32: - func_stripname : : "$1" - func_to_host_path_tmp1=$func_stripname_result - func_to_host_path_result=`cygpath -m -p "$func_to_host_path_tmp1"` - func_convert_path_check : ";" \ - "$func_to_host_path_tmp1" "$func_to_host_path_result" - func_convert_path_front_back_pathsep ":*" "*:" ";" "$1" - fi -} -# end func_convert_path_cygwin_to_w32 - - -# func_convert_path_nix_to_w32 ARG -# Convert path ARG from *nix to w32 format. Requires a wine environment and -# a working winepath. Returns result in func_to_host_file_result. -func_convert_path_nix_to_w32 () -{ - $debug_cmd - - func_to_host_path_result=$1 - if test -n "$1"; then - # See func_convert_path_msys_to_w32: - func_stripname : : "$1" - func_to_host_path_tmp1=$func_stripname_result - func_convert_core_path_wine_to_w32 "$func_to_host_path_tmp1" - func_to_host_path_result=$func_convert_core_path_wine_to_w32_result - func_convert_path_check : ";" \ - "$func_to_host_path_tmp1" "$func_to_host_path_result" - func_convert_path_front_back_pathsep ":*" "*:" ";" "$1" - fi -} -# end func_convert_path_nix_to_w32 - - -# func_convert_path_msys_to_cygwin ARG -# Convert path ARG from MSYS to Cygwin format. Requires LT_CYGPATH set. -# Returns result in func_to_host_file_result. -func_convert_path_msys_to_cygwin () -{ - $debug_cmd - - func_to_host_path_result=$1 - if test -n "$1"; then - # See func_convert_path_msys_to_w32: - func_stripname : : "$1" - func_to_host_path_tmp1=$func_stripname_result - func_convert_core_msys_to_w32 "$func_to_host_path_tmp1" - func_cygpath -u -p "$func_convert_core_msys_to_w32_result" - func_to_host_path_result=$func_cygpath_result - func_convert_path_check : : \ - "$func_to_host_path_tmp1" "$func_to_host_path_result" - func_convert_path_front_back_pathsep ":*" "*:" : "$1" - fi -} -# end func_convert_path_msys_to_cygwin - - -# func_convert_path_nix_to_cygwin ARG -# Convert path ARG from *nix to Cygwin format. Requires Cygwin installed in a -# a wine environment, working winepath, and LT_CYGPATH set. Returns result in -# func_to_host_file_result. -func_convert_path_nix_to_cygwin () -{ - $debug_cmd - - func_to_host_path_result=$1 - if test -n "$1"; then - # Remove leading and trailing path separator characters from - # ARG. msys behavior is inconsistent here, cygpath turns them - # into '.;' and ';.', and winepath ignores them completely. - func_stripname : : "$1" - func_to_host_path_tmp1=$func_stripname_result - func_convert_core_path_wine_to_w32 "$func_to_host_path_tmp1" - func_cygpath -u -p "$func_convert_core_path_wine_to_w32_result" - func_to_host_path_result=$func_cygpath_result - func_convert_path_check : : \ - "$func_to_host_path_tmp1" "$func_to_host_path_result" - func_convert_path_front_back_pathsep ":*" "*:" : "$1" - fi -} -# end func_convert_path_nix_to_cygwin - - -# func_dll_def_p FILE -# True iff FILE is a Windows DLL '.def' file. -# Keep in sync with _LT_DLL_DEF_P in libtool.m4 -func_dll_def_p () -{ - $debug_cmd - - func_dll_def_p_tmp=`$SED -n \ - -e 's/^[ ]*//' \ - -e '/^\(;.*\)*$/d' \ - -e 's/^\(EXPORTS\|LIBRARY\)\([ ].*\)*$/DEF/p' \ - -e q \ - "$1"` - test DEF = "$func_dll_def_p_tmp" -} - - -# func_mode_compile arg... -func_mode_compile () -{ - $debug_cmd - - # Get the compilation command and the source file. - base_compile= - srcfile=$nonopt # always keep a non-empty value in "srcfile" - suppress_opt=yes - suppress_output= - arg_mode=normal - libobj= - later= - pie_flag= - - for arg - do - case $arg_mode in - arg ) - # do not "continue". Instead, add this to base_compile - lastarg=$arg - arg_mode=normal - ;; - - target ) - libobj=$arg - arg_mode=normal - continue - ;; - - normal ) - # Accept any command-line options. - case $arg in - -o) - test -n "$libobj" && \ - func_fatal_error "you cannot specify '-o' more than once" - arg_mode=target - continue - ;; - - -pie | -fpie | -fPIE) - func_append pie_flag " $arg" - continue - ;; - - -shared | -static | -prefer-pic | -prefer-non-pic) - func_append later " $arg" - continue - ;; - - -no-suppress) - suppress_opt=no - continue - ;; - - -Xcompiler) - arg_mode=arg # the next one goes into the "base_compile" arg list - continue # The current "srcfile" will either be retained or - ;; # replaced later. I would guess that would be a bug. - - -Wc,*) - func_stripname '-Wc,' '' "$arg" - args=$func_stripname_result - lastarg= - save_ifs=$IFS; IFS=, - for arg in $args; do - IFS=$save_ifs - func_append_quoted lastarg "$arg" - done - IFS=$save_ifs - func_stripname ' ' '' "$lastarg" - lastarg=$func_stripname_result - - # Add the arguments to base_compile. - func_append base_compile " $lastarg" - continue - ;; - - *) - # Accept the current argument as the source file. - # The previous "srcfile" becomes the current argument. - # - lastarg=$srcfile - srcfile=$arg - ;; - esac # case $arg - ;; - esac # case $arg_mode - - # Aesthetically quote the previous argument. - func_append_quoted base_compile "$lastarg" - done # for arg - - case $arg_mode in - arg) - func_fatal_error "you must specify an argument for -Xcompile" - ;; - target) - func_fatal_error "you must specify a target with '-o'" - ;; - *) - # Get the name of the library object. - test -z "$libobj" && { - func_basename "$srcfile" - libobj=$func_basename_result - } - ;; - esac - - # Recognize several different file suffixes. - # If the user specifies -o file.o, it is replaced with file.lo - case $libobj in - *.[cCFSifmso] | \ - *.ada | *.adb | *.ads | *.asm | \ - *.c++ | *.cc | *.ii | *.class | *.cpp | *.cxx | \ - *.[fF][09]? | *.for | *.java | *.go | *.obj | *.sx | *.cu | *.cup) - func_xform "$libobj" - libobj=$func_xform_result - ;; - esac - - case $libobj in - *.lo) func_lo2o "$libobj"; obj=$func_lo2o_result ;; - *) - func_fatal_error "cannot determine name of library object from '$libobj'" - ;; - esac - - func_infer_tag $base_compile - - for arg in $later; do - case $arg in - -shared) - test yes = "$build_libtool_libs" \ - || func_fatal_configuration "cannot build a shared library" - build_old_libs=no - continue - ;; - - -static) - build_libtool_libs=no - build_old_libs=yes - continue - ;; - - -prefer-pic) - pic_mode=yes - continue - ;; - - -prefer-non-pic) - pic_mode=no - continue - ;; - esac - done - - func_quote_for_eval "$libobj" - test "X$libobj" != "X$func_quote_for_eval_result" \ - && $ECHO "X$libobj" | $GREP '[]~#^*{};<>?"'"'"' &()|`$[]' \ - && func_warning "libobj name '$libobj' may not contain shell special characters." - func_dirname_and_basename "$obj" "/" "" - objname=$func_basename_result - xdir=$func_dirname_result - lobj=$xdir$objdir/$objname - - test -z "$base_compile" && \ - func_fatal_help "you must specify a compilation command" - - # Delete any leftover library objects. - if test yes = "$build_old_libs"; then - removelist="$obj $lobj $libobj ${libobj}T" - else - removelist="$lobj $libobj ${libobj}T" - fi - - # On Cygwin there's no "real" PIC flag so we must build both object types - case $host_os in - cygwin* | mingw* | pw32* | os2* | cegcc*) - pic_mode=default - ;; - esac - if test no = "$pic_mode" && test pass_all != "$deplibs_check_method"; then - # non-PIC code in shared libraries is not supported - pic_mode=default - fi - - # Calculate the filename of the output object if compiler does - # not support -o with -c - if test no = "$compiler_c_o"; then - output_obj=`$ECHO "$srcfile" | $SED 's%^.*/%%; s%\.[^.]*$%%'`.$objext - lockfile=$output_obj.lock - else - output_obj= - need_locks=no - lockfile= - fi - - # Lock this critical section if it is needed - # We use this script file to make the link, it avoids creating a new file - if test yes = "$need_locks"; then - until $opt_dry_run || ln "$progpath" "$lockfile" 2>/dev/null; do - func_echo "Waiting for $lockfile to be removed" - sleep 2 - done - elif test warn = "$need_locks"; then - if test -f "$lockfile"; then - $ECHO "\ -*** ERROR, $lockfile exists and contains: -`cat $lockfile 2>/dev/null` - -This indicates that another process is trying to use the same -temporary object file, and libtool could not work around it because -your compiler does not support '-c' and '-o' together. If you -repeat this compilation, it may succeed, by chance, but you had better -avoid parallel builds (make -j) in this platform, or get a better -compiler." - - $opt_dry_run || $RM $removelist - exit $EXIT_FAILURE - fi - func_append removelist " $output_obj" - $ECHO "$srcfile" > "$lockfile" - fi - - $opt_dry_run || $RM $removelist - func_append removelist " $lockfile" - trap '$opt_dry_run || $RM $removelist; exit $EXIT_FAILURE' 1 2 15 - - func_to_tool_file "$srcfile" func_convert_file_msys_to_w32 - srcfile=$func_to_tool_file_result - func_quote_for_eval "$srcfile" - qsrcfile=$func_quote_for_eval_result - - # Only build a PIC object if we are building libtool libraries. - if test yes = "$build_libtool_libs"; then - # Without this assignment, base_compile gets emptied. - fbsd_hideous_sh_bug=$base_compile - - if test no != "$pic_mode"; then - command="$base_compile $qsrcfile $pic_flag" - else - # Don't build PIC code - command="$base_compile $qsrcfile" - fi - - func_mkdir_p "$xdir$objdir" - - if test -z "$output_obj"; then - # Place PIC objects in $objdir - func_append command " -o $lobj" - fi - - func_show_eval_locale "$command" \ - 'test -n "$output_obj" && $RM $removelist; exit $EXIT_FAILURE' - - if test warn = "$need_locks" && - test "X`cat $lockfile 2>/dev/null`" != "X$srcfile"; then - $ECHO "\ -*** ERROR, $lockfile contains: -`cat $lockfile 2>/dev/null` - -but it should contain: -$srcfile - -This indicates that another process is trying to use the same -temporary object file, and libtool could not work around it because -your compiler does not support '-c' and '-o' together. If you -repeat this compilation, it may succeed, by chance, but you had better -avoid parallel builds (make -j) in this platform, or get a better -compiler." - - $opt_dry_run || $RM $removelist - exit $EXIT_FAILURE - fi - - # Just move the object if needed, then go on to compile the next one - if test -n "$output_obj" && test "X$output_obj" != "X$lobj"; then - func_show_eval '$MV "$output_obj" "$lobj"' \ - 'error=$?; $opt_dry_run || $RM $removelist; exit $error' - fi - - # Allow error messages only from the first compilation. - if test yes = "$suppress_opt"; then - suppress_output=' >/dev/null 2>&1' - fi - fi - - # Only build a position-dependent object if we build old libraries. - if test yes = "$build_old_libs"; then - if test yes != "$pic_mode"; then - # Don't build PIC code - command="$base_compile $qsrcfile$pie_flag" - else - command="$base_compile $qsrcfile $pic_flag" - fi - if test yes = "$compiler_c_o"; then - func_append command " -o $obj" - fi - - # Suppress compiler output if we already did a PIC compilation. - func_append command "$suppress_output" - func_show_eval_locale "$command" \ - '$opt_dry_run || $RM $removelist; exit $EXIT_FAILURE' - - if test warn = "$need_locks" && - test "X`cat $lockfile 2>/dev/null`" != "X$srcfile"; then - $ECHO "\ -*** ERROR, $lockfile contains: -`cat $lockfile 2>/dev/null` - -but it should contain: -$srcfile - -This indicates that another process is trying to use the same -temporary object file, and libtool could not work around it because -your compiler does not support '-c' and '-o' together. If you -repeat this compilation, it may succeed, by chance, but you had better -avoid parallel builds (make -j) in this platform, or get a better -compiler." - - $opt_dry_run || $RM $removelist - exit $EXIT_FAILURE - fi - - # Just move the object if needed - if test -n "$output_obj" && test "X$output_obj" != "X$obj"; then - func_show_eval '$MV "$output_obj" "$obj"' \ - 'error=$?; $opt_dry_run || $RM $removelist; exit $error' - fi - fi - - $opt_dry_run || { - func_write_libtool_object "$libobj" "$objdir/$objname" "$objname" - - # Unlock the critical section if it was locked - if test no != "$need_locks"; then - removelist=$lockfile - $RM "$lockfile" - fi - } - - exit $EXIT_SUCCESS -} - -$opt_help || { - test compile = "$opt_mode" && func_mode_compile ${1+"$@"} -} - -func_mode_help () -{ - # We need to display help for each of the modes. - case $opt_mode in - "") - # Generic help is extracted from the usage comments - # at the start of this file. - func_help - ;; - - clean) - $ECHO \ -"Usage: $progname [OPTION]... --mode=clean RM [RM-OPTION]... FILE... - -Remove files from the build directory. - -RM is the name of the program to use to delete files associated with each FILE -(typically '/bin/rm'). RM-OPTIONS are options (such as '-f') to be passed -to RM. - -If FILE is a libtool library, object or program, all the files associated -with it are deleted. Otherwise, only FILE itself is deleted using RM." - ;; - - compile) - $ECHO \ -"Usage: $progname [OPTION]... --mode=compile COMPILE-COMMAND... SOURCEFILE - -Compile a source file into a libtool library object. - -This mode accepts the following additional options: - - -o OUTPUT-FILE set the output file name to OUTPUT-FILE - -no-suppress do not suppress compiler output for multiple passes - -prefer-pic try to build PIC objects only - -prefer-non-pic try to build non-PIC objects only - -shared do not build a '.o' file suitable for static linking - -static only build a '.o' file suitable for static linking - -Wc,FLAG pass FLAG directly to the compiler - -COMPILE-COMMAND is a command to be used in creating a 'standard' object file -from the given SOURCEFILE. - -The output file name is determined by removing the directory component from -SOURCEFILE, then substituting the C source code suffix '.c' with the -library object suffix, '.lo'." - ;; - - execute) - $ECHO \ -"Usage: $progname [OPTION]... --mode=execute COMMAND [ARGS]... - -Automatically set library path, then run a program. - -This mode accepts the following additional options: - - -dlopen FILE add the directory containing FILE to the library path - -This mode sets the library path environment variable according to '-dlopen' -flags. - -If any of the ARGS are libtool executable wrappers, then they are translated -into their corresponding uninstalled binary, and any of their required library -directories are added to the library path. - -Then, COMMAND is executed, with ARGS as arguments." - ;; - - finish) - $ECHO \ -"Usage: $progname [OPTION]... --mode=finish [LIBDIR]... - -Complete the installation of libtool libraries. - -Each LIBDIR is a directory that contains libtool libraries. - -The commands that this mode executes may require superuser privileges. Use -the '--dry-run' option if you just want to see what would be executed." - ;; - - install) - $ECHO \ -"Usage: $progname [OPTION]... --mode=install INSTALL-COMMAND... - -Install executables or libraries. - -INSTALL-COMMAND is the installation command. The first component should be -either the 'install' or 'cp' program. - -The following components of INSTALL-COMMAND are treated specially: - - -inst-prefix-dir PREFIX-DIR Use PREFIX-DIR as a staging area for installation - -The rest of the components are interpreted as arguments to that command (only -BSD-compatible install options are recognized)." - ;; - - link) - $ECHO \ -"Usage: $progname [OPTION]... --mode=link LINK-COMMAND... - -Link object files or libraries together to form another library, or to -create an executable program. - -LINK-COMMAND is a command using the C compiler that you would use to create -a program from several object files. - -The following components of LINK-COMMAND are treated specially: - - -all-static do not do any dynamic linking at all - -avoid-version do not add a version suffix if possible - -bindir BINDIR specify path to binaries directory (for systems where - libraries must be found in the PATH setting at runtime) - -dlopen FILE '-dlpreopen' FILE if it cannot be dlopened at runtime - -dlpreopen FILE link in FILE and add its symbols to lt_preloaded_symbols - -export-dynamic allow symbols from OUTPUT-FILE to be resolved with dlsym(3) - -export-symbols SYMFILE - try to export only the symbols listed in SYMFILE - -export-symbols-regex REGEX - try to export only the symbols matching REGEX - -LLIBDIR search LIBDIR for required installed libraries - -lNAME OUTPUT-FILE requires the installed library libNAME - -module build a library that can dlopened - -no-fast-install disable the fast-install mode - -no-install link a not-installable executable - -no-undefined declare that a library does not refer to external symbols - -o OUTPUT-FILE create OUTPUT-FILE from the specified objects - -objectlist FILE use a list of object files found in FILE to specify objects - -os2dllname NAME force a short DLL name on OS/2 (no effect on other OSes) - -precious-files-regex REGEX - don't remove output files matching REGEX - -release RELEASE specify package release information - -rpath LIBDIR the created library will eventually be installed in LIBDIR - -R[ ]LIBDIR add LIBDIR to the runtime path of programs and libraries - -shared only do dynamic linking of libtool libraries - -shrext SUFFIX override the standard shared library file extension - -static do not do any dynamic linking of uninstalled libtool libraries - -static-libtool-libs - do not do any dynamic linking of libtool libraries - -version-info CURRENT[:REVISION[:AGE]] - specify library version info [each variable defaults to 0] - -weak LIBNAME declare that the target provides the LIBNAME interface - -Wc,FLAG - -Xcompiler FLAG pass linker-specific FLAG directly to the compiler - -Wl,FLAG - -Xlinker FLAG pass linker-specific FLAG directly to the linker - -XCClinker FLAG pass link-specific FLAG to the compiler driver (CC) - -All other options (arguments beginning with '-') are ignored. - -Every other argument is treated as a filename. Files ending in '.la' are -treated as uninstalled libtool libraries, other files are standard or library -object files. - -If the OUTPUT-FILE ends in '.la', then a libtool library is created, -only library objects ('.lo' files) may be specified, and '-rpath' is -required, except when creating a convenience library. - -If OUTPUT-FILE ends in '.a' or '.lib', then a standard library is created -using 'ar' and 'ranlib', or on Windows using 'lib'. - -If OUTPUT-FILE ends in '.lo' or '.$objext', then a reloadable object file -is created, otherwise an executable program is created." - ;; - - uninstall) - $ECHO \ -"Usage: $progname [OPTION]... --mode=uninstall RM [RM-OPTION]... FILE... - -Remove libraries from an installation directory. - -RM is the name of the program to use to delete files associated with each FILE -(typically '/bin/rm'). RM-OPTIONS are options (such as '-f') to be passed -to RM. - -If FILE is a libtool library, all the files associated with it are deleted. -Otherwise, only FILE itself is deleted using RM." - ;; - - *) - func_fatal_help "invalid operation mode '$opt_mode'" - ;; - esac - - echo - $ECHO "Try '$progname --help' for more information about other modes." -} - -# Now that we've collected a possible --mode arg, show help if necessary -if $opt_help; then - if test : = "$opt_help"; then - func_mode_help - else - { - func_help noexit - for opt_mode in compile link execute install finish uninstall clean; do - func_mode_help - done - } | $SED -n '1p; 2,$s/^Usage:/ or: /p' - { - func_help noexit - for opt_mode in compile link execute install finish uninstall clean; do - echo - func_mode_help - done - } | - $SED '1d - /^When reporting/,/^Report/{ - H - d - } - $x - /information about other modes/d - /more detailed .*MODE/d - s/^Usage:.*--mode=\([^ ]*\) .*/Description of \1 mode:/' - fi - exit $? -fi - - -# func_mode_execute arg... -func_mode_execute () -{ - $debug_cmd - - # The first argument is the command name. - cmd=$nonopt - test -z "$cmd" && \ - func_fatal_help "you must specify a COMMAND" - - # Handle -dlopen flags immediately. - for file in $opt_dlopen; do - test -f "$file" \ - || func_fatal_help "'$file' is not a file" - - dir= - case $file in - *.la) - func_resolve_sysroot "$file" - file=$func_resolve_sysroot_result - - # Check to see that this really is a libtool archive. - func_lalib_unsafe_p "$file" \ - || func_fatal_help "'$lib' is not a valid libtool archive" - - # Read the libtool library. - dlname= - library_names= - func_source "$file" - - # Skip this library if it cannot be dlopened. - if test -z "$dlname"; then - # Warn if it was a shared library. - test -n "$library_names" && \ - func_warning "'$file' was not linked with '-export-dynamic'" - continue - fi - - func_dirname "$file" "" "." - dir=$func_dirname_result - - if test -f "$dir/$objdir/$dlname"; then - func_append dir "/$objdir" - else - if test ! -f "$dir/$dlname"; then - func_fatal_error "cannot find '$dlname' in '$dir' or '$dir/$objdir'" - fi - fi - ;; - - *.lo) - # Just add the directory containing the .lo file. - func_dirname "$file" "" "." - dir=$func_dirname_result - ;; - - *) - func_warning "'-dlopen' is ignored for non-libtool libraries and objects" - continue - ;; - esac - - # Get the absolute pathname. - absdir=`cd "$dir" && pwd` - test -n "$absdir" && dir=$absdir - - # Now add the directory to shlibpath_var. - if eval "test -z \"\$$shlibpath_var\""; then - eval "$shlibpath_var=\"\$dir\"" - else - eval "$shlibpath_var=\"\$dir:\$$shlibpath_var\"" - fi - done - - # This variable tells wrapper scripts just to set shlibpath_var - # rather than running their programs. - libtool_execute_magic=$magic - - # Check if any of the arguments is a wrapper script. - args= - for file - do - case $file in - -* | *.la | *.lo ) ;; - *) - # Do a test to see if this is really a libtool program. - if func_ltwrapper_script_p "$file"; then - func_source "$file" - # Transform arg to wrapped name. - file=$progdir/$program - elif func_ltwrapper_executable_p "$file"; then - func_ltwrapper_scriptname "$file" - func_source "$func_ltwrapper_scriptname_result" - # Transform arg to wrapped name. - file=$progdir/$program - fi - ;; - esac - # Quote arguments (to preserve shell metacharacters). - func_append_quoted args "$file" - done - - if $opt_dry_run; then - # Display what would be done. - if test -n "$shlibpath_var"; then - eval "\$ECHO \"\$shlibpath_var=\$$shlibpath_var\"" - echo "export $shlibpath_var" - fi - $ECHO "$cmd$args" - exit $EXIT_SUCCESS - else - if test -n "$shlibpath_var"; then - # Export the shlibpath_var. - eval "export $shlibpath_var" - fi - - # Restore saved environment variables - for lt_var in LANG LANGUAGE LC_ALL LC_CTYPE LC_COLLATE LC_MESSAGES - do - eval "if test \"\${save_$lt_var+set}\" = set; then - $lt_var=\$save_$lt_var; export $lt_var - else - $lt_unset $lt_var - fi" - done - - # Now prepare to actually exec the command. - exec_cmd=\$cmd$args - fi -} - -test execute = "$opt_mode" && func_mode_execute ${1+"$@"} - - -# func_mode_finish arg... -func_mode_finish () -{ - $debug_cmd - - libs= - libdirs= - admincmds= - - for opt in "$nonopt" ${1+"$@"} - do - if test -d "$opt"; then - func_append libdirs " $opt" - - elif test -f "$opt"; then - if func_lalib_unsafe_p "$opt"; then - func_append libs " $opt" - else - func_warning "'$opt' is not a valid libtool archive" - fi - - else - func_fatal_error "invalid argument '$opt'" - fi - done - - if test -n "$libs"; then - if test -n "$lt_sysroot"; then - sysroot_regex=`$ECHO "$lt_sysroot" | $SED "$sed_make_literal_regex"` - sysroot_cmd="s/\([ ']\)$sysroot_regex/\1/g;" - else - sysroot_cmd= - fi - - # Remove sysroot references - if $opt_dry_run; then - for lib in $libs; do - echo "removing references to $lt_sysroot and '=' prefixes from $lib" - done - else - tmpdir=`func_mktempdir` - for lib in $libs; do - $SED -e "$sysroot_cmd s/\([ ']-[LR]\)=/\1/g; s/\([ ']\)=/\1/g" $lib \ - > $tmpdir/tmp-la - mv -f $tmpdir/tmp-la $lib - done - ${RM}r "$tmpdir" - fi - fi - - if test -n "$finish_cmds$finish_eval" && test -n "$libdirs"; then - for libdir in $libdirs; do - if test -n "$finish_cmds"; then - # Do each command in the finish commands. - func_execute_cmds "$finish_cmds" 'admincmds="$admincmds -'"$cmd"'"' - fi - if test -n "$finish_eval"; then - # Do the single finish_eval. - eval cmds=\"$finish_eval\" - $opt_dry_run || eval "$cmds" || func_append admincmds " - $cmds" - fi - done - fi - - # Exit here if they wanted silent mode. - $opt_quiet && exit $EXIT_SUCCESS - - if test -n "$finish_cmds$finish_eval" && test -n "$libdirs"; then - echo "----------------------------------------------------------------------" - echo "Libraries have been installed in:" - for libdir in $libdirs; do - $ECHO " $libdir" - done - echo - echo "If you ever happen to want to link against installed libraries" - echo "in a given directory, LIBDIR, you must either use libtool, and" - echo "specify the full pathname of the library, or use the '-LLIBDIR'" - echo "flag during linking and do at least one of the following:" - if test -n "$shlibpath_var"; then - echo " - add LIBDIR to the '$shlibpath_var' environment variable" - echo " during execution" - fi - if test -n "$runpath_var"; then - echo " - add LIBDIR to the '$runpath_var' environment variable" - echo " during linking" - fi - if test -n "$hardcode_libdir_flag_spec"; then - libdir=LIBDIR - eval flag=\"$hardcode_libdir_flag_spec\" - - $ECHO " - use the '$flag' linker flag" - fi - if test -n "$admincmds"; then - $ECHO " - have your system administrator run these commands:$admincmds" - fi - if test -f /etc/ld.so.conf; then - echo " - have your system administrator add LIBDIR to '/etc/ld.so.conf'" - fi - echo - - echo "See any operating system documentation about shared libraries for" - case $host in - solaris2.[6789]|solaris2.1[0-9]) - echo "more information, such as the ld(1), crle(1) and ld.so(8) manual" - echo "pages." - ;; - *) - echo "more information, such as the ld(1) and ld.so(8) manual pages." - ;; - esac - echo "----------------------------------------------------------------------" - fi - exit $EXIT_SUCCESS -} - -test finish = "$opt_mode" && func_mode_finish ${1+"$@"} - - -# func_mode_install arg... -func_mode_install () -{ - $debug_cmd - - # There may be an optional sh(1) argument at the beginning of - # install_prog (especially on Windows NT). - if test "$SHELL" = "$nonopt" || test /bin/sh = "$nonopt" || - # Allow the use of GNU shtool's install command. - case $nonopt in *shtool*) :;; *) false;; esac - then - # Aesthetically quote it. - func_quote_for_eval "$nonopt" - install_prog="$func_quote_for_eval_result " - arg=$1 - shift - else - install_prog= - arg=$nonopt - fi - - # The real first argument should be the name of the installation program. - # Aesthetically quote it. - func_quote_for_eval "$arg" - func_append install_prog "$func_quote_for_eval_result" - install_shared_prog=$install_prog - case " $install_prog " in - *[\\\ /]cp\ *) install_cp=: ;; - *) install_cp=false ;; - esac - - # We need to accept at least all the BSD install flags. - dest= - files= - opts= - prev= - install_type= - isdir=false - stripme= - no_mode=: - for arg - do - arg2= - if test -n "$dest"; then - func_append files " $dest" - dest=$arg - continue - fi - - case $arg in - -d) isdir=: ;; - -f) - if $install_cp; then :; else - prev=$arg - fi - ;; - -g | -m | -o) - prev=$arg - ;; - -s) - stripme=" -s" - continue - ;; - -*) - ;; - *) - # If the previous option needed an argument, then skip it. - if test -n "$prev"; then - if test X-m = "X$prev" && test -n "$install_override_mode"; then - arg2=$install_override_mode - no_mode=false - fi - prev= - else - dest=$arg - continue - fi - ;; - esac - - # Aesthetically quote the argument. - func_quote_for_eval "$arg" - func_append install_prog " $func_quote_for_eval_result" - if test -n "$arg2"; then - func_quote_for_eval "$arg2" - fi - func_append install_shared_prog " $func_quote_for_eval_result" - done - - test -z "$install_prog" && \ - func_fatal_help "you must specify an install program" - - test -n "$prev" && \ - func_fatal_help "the '$prev' option requires an argument" - - if test -n "$install_override_mode" && $no_mode; then - if $install_cp; then :; else - func_quote_for_eval "$install_override_mode" - func_append install_shared_prog " -m $func_quote_for_eval_result" - fi - fi - - if test -z "$files"; then - if test -z "$dest"; then - func_fatal_help "no file or destination specified" - else - func_fatal_help "you must specify a destination" - fi - fi - - # Strip any trailing slash from the destination. - func_stripname '' '/' "$dest" - dest=$func_stripname_result - - # Check to see that the destination is a directory. - test -d "$dest" && isdir=: - if $isdir; then - destdir=$dest - destname= - else - func_dirname_and_basename "$dest" "" "." - destdir=$func_dirname_result - destname=$func_basename_result - - # Not a directory, so check to see that there is only one file specified. - set dummy $files; shift - test "$#" -gt 1 && \ - func_fatal_help "'$dest' is not a directory" - fi - case $destdir in - [\\/]* | [A-Za-z]:[\\/]*) ;; - *) - for file in $files; do - case $file in - *.lo) ;; - *) - func_fatal_help "'$destdir' must be an absolute directory name" - ;; - esac - done - ;; - esac - - # This variable tells wrapper scripts just to set variables rather - # than running their programs. - libtool_install_magic=$magic - - staticlibs= - future_libdirs= - current_libdirs= - for file in $files; do - - # Do each installation. - case $file in - *.$libext) - # Do the static libraries later. - func_append staticlibs " $file" - ;; - - *.la) - func_resolve_sysroot "$file" - file=$func_resolve_sysroot_result - - # Check to see that this really is a libtool archive. - func_lalib_unsafe_p "$file" \ - || func_fatal_help "'$file' is not a valid libtool archive" - - library_names= - old_library= - relink_command= - func_source "$file" - - # Add the libdir to current_libdirs if it is the destination. - if test "X$destdir" = "X$libdir"; then - case "$current_libdirs " in - *" $libdir "*) ;; - *) func_append current_libdirs " $libdir" ;; - esac - else - # Note the libdir as a future libdir. - case "$future_libdirs " in - *" $libdir "*) ;; - *) func_append future_libdirs " $libdir" ;; - esac - fi - - func_dirname "$file" "/" "" - dir=$func_dirname_result - func_append dir "$objdir" - - if test -n "$relink_command"; then - # Determine the prefix the user has applied to our future dir. - inst_prefix_dir=`$ECHO "$destdir" | $SED -e "s%$libdir\$%%"` - - # Don't allow the user to place us outside of our expected - # location b/c this prevents finding dependent libraries that - # are installed to the same prefix. - # At present, this check doesn't affect windows .dll's that - # are installed into $libdir/../bin (currently, that works fine) - # but it's something to keep an eye on. - test "$inst_prefix_dir" = "$destdir" && \ - func_fatal_error "error: cannot install '$file' to a directory not ending in $libdir" - - if test -n "$inst_prefix_dir"; then - # Stick the inst_prefix_dir data into the link command. - relink_command=`$ECHO "$relink_command" | $SED "s%@inst_prefix_dir@%-inst-prefix-dir $inst_prefix_dir%"` - else - relink_command=`$ECHO "$relink_command" | $SED "s%@inst_prefix_dir@%%"` - fi - - func_warning "relinking '$file'" - func_show_eval "$relink_command" \ - 'func_fatal_error "error: relink '\''$file'\'' with the above command before installing it"' - fi - - # See the names of the shared library. - set dummy $library_names; shift - if test -n "$1"; then - realname=$1 - shift - - srcname=$realname - test -n "$relink_command" && srcname=${realname}T - - # Install the shared library and build the symlinks. - func_show_eval "$install_shared_prog $dir/$srcname $destdir/$realname" \ - 'exit $?' - tstripme=$stripme - case $host_os in - cygwin* | mingw* | pw32* | cegcc*) - case $realname in - *.dll.a) - tstripme= - ;; - esac - ;; - os2*) - case $realname in - *_dll.a) - tstripme= - ;; - esac - ;; - esac - if test -n "$tstripme" && test -n "$striplib"; then - func_show_eval "$striplib $destdir/$realname" 'exit $?' - fi - - if test "$#" -gt 0; then - # Delete the old symlinks, and create new ones. - # Try 'ln -sf' first, because the 'ln' binary might depend on - # the symlink we replace! Solaris /bin/ln does not understand -f, - # so we also need to try rm && ln -s. - for linkname - do - test "$linkname" != "$realname" \ - && func_show_eval "(cd $destdir && { $LN_S -f $realname $linkname || { $RM $linkname && $LN_S $realname $linkname; }; })" - done - fi - - # Do each command in the postinstall commands. - lib=$destdir/$realname - func_execute_cmds "$postinstall_cmds" 'exit $?' - fi - - # Install the pseudo-library for information purposes. - func_basename "$file" - name=$func_basename_result - instname=$dir/${name}i - func_show_eval "$install_prog $instname $destdir/$name" 'exit $?' - - # Maybe install the static library, too. - test -n "$old_library" && func_append staticlibs " $dir/$old_library" - ;; - - *.lo) - # Install (i.e. copy) a libtool object. - - # Figure out destination file name, if it wasn't already specified. - if test -n "$destname"; then - destfile=$destdir/$destname - else - func_basename "$file" - destfile=$func_basename_result - destfile=$destdir/$destfile - fi - - # Deduce the name of the destination old-style object file. - case $destfile in - *.lo) - func_lo2o "$destfile" - staticdest=$func_lo2o_result - ;; - *.$objext) - staticdest=$destfile - destfile= - ;; - *) - func_fatal_help "cannot copy a libtool object to '$destfile'" - ;; - esac - - # Install the libtool object if requested. - test -n "$destfile" && \ - func_show_eval "$install_prog $file $destfile" 'exit $?' - - # Install the old object if enabled. - if test yes = "$build_old_libs"; then - # Deduce the name of the old-style object file. - func_lo2o "$file" - staticobj=$func_lo2o_result - func_show_eval "$install_prog \$staticobj \$staticdest" 'exit $?' - fi - exit $EXIT_SUCCESS - ;; - - *) - # Figure out destination file name, if it wasn't already specified. - if test -n "$destname"; then - destfile=$destdir/$destname - else - func_basename "$file" - destfile=$func_basename_result - destfile=$destdir/$destfile - fi - - # If the file is missing, and there is a .exe on the end, strip it - # because it is most likely a libtool script we actually want to - # install - stripped_ext= - case $file in - *.exe) - if test ! -f "$file"; then - func_stripname '' '.exe' "$file" - file=$func_stripname_result - stripped_ext=.exe - fi - ;; - esac - - # Do a test to see if this is really a libtool program. - case $host in - *cygwin* | *mingw*) - if func_ltwrapper_executable_p "$file"; then - func_ltwrapper_scriptname "$file" - wrapper=$func_ltwrapper_scriptname_result - else - func_stripname '' '.exe' "$file" - wrapper=$func_stripname_result - fi - ;; - *) - wrapper=$file - ;; - esac - if func_ltwrapper_script_p "$wrapper"; then - notinst_deplibs= - relink_command= - - func_source "$wrapper" - - # Check the variables that should have been set. - test -z "$generated_by_libtool_version" && \ - func_fatal_error "invalid libtool wrapper script '$wrapper'" - - finalize=: - for lib in $notinst_deplibs; do - # Check to see that each library is installed. - libdir= - if test -f "$lib"; then - func_source "$lib" - fi - libfile=$libdir/`$ECHO "$lib" | $SED 's%^.*/%%g'` - if test -n "$libdir" && test ! -f "$libfile"; then - func_warning "'$lib' has not been installed in '$libdir'" - finalize=false - fi - done - - relink_command= - func_source "$wrapper" - - outputname= - if test no = "$fast_install" && test -n "$relink_command"; then - $opt_dry_run || { - if $finalize; then - tmpdir=`func_mktempdir` - func_basename "$file$stripped_ext" - file=$func_basename_result - outputname=$tmpdir/$file - # Replace the output file specification. - relink_command=`$ECHO "$relink_command" | $SED 's%@OUTPUT@%'"$outputname"'%g'` - - $opt_quiet || { - func_quote_for_expand "$relink_command" - eval "func_echo $func_quote_for_expand_result" - } - if eval "$relink_command"; then : - else - func_error "error: relink '$file' with the above command before installing it" - $opt_dry_run || ${RM}r "$tmpdir" - continue - fi - file=$outputname - else - func_warning "cannot relink '$file'" - fi - } - else - # Install the binary that we compiled earlier. - file=`$ECHO "$file$stripped_ext" | $SED "s%\([^/]*\)$%$objdir/\1%"` - fi - fi - - # remove .exe since cygwin /usr/bin/install will append another - # one anyway - case $install_prog,$host in - */usr/bin/install*,*cygwin*) - case $file:$destfile in - *.exe:*.exe) - # this is ok - ;; - *.exe:*) - destfile=$destfile.exe - ;; - *:*.exe) - func_stripname '' '.exe' "$destfile" - destfile=$func_stripname_result - ;; - esac - ;; - esac - func_show_eval "$install_prog\$stripme \$file \$destfile" 'exit $?' - $opt_dry_run || if test -n "$outputname"; then - ${RM}r "$tmpdir" - fi - ;; - esac - done - - for file in $staticlibs; do - func_basename "$file" - name=$func_basename_result - - # Set up the ranlib parameters. - oldlib=$destdir/$name - func_to_tool_file "$oldlib" func_convert_file_msys_to_w32 - tool_oldlib=$func_to_tool_file_result - - func_show_eval "$install_prog \$file \$oldlib" 'exit $?' - - if test -n "$stripme" && test -n "$old_striplib"; then - func_show_eval "$old_striplib $tool_oldlib" 'exit $?' - fi - - # Do each command in the postinstall commands. - func_execute_cmds "$old_postinstall_cmds" 'exit $?' - done - - test -n "$future_libdirs" && \ - func_warning "remember to run '$progname --finish$future_libdirs'" - - if test -n "$current_libdirs"; then - # Maybe just do a dry run. - $opt_dry_run && current_libdirs=" -n$current_libdirs" - exec_cmd='$SHELL "$progpath" $preserve_args --finish$current_libdirs' - else - exit $EXIT_SUCCESS - fi -} - -test install = "$opt_mode" && func_mode_install ${1+"$@"} - - -# func_generate_dlsyms outputname originator pic_p -# Extract symbols from dlprefiles and create ${outputname}S.o with -# a dlpreopen symbol table. -func_generate_dlsyms () -{ - $debug_cmd - - my_outputname=$1 - my_originator=$2 - my_pic_p=${3-false} - my_prefix=`$ECHO "$my_originator" | $SED 's%[^a-zA-Z0-9]%_%g'` - my_dlsyms= - - if test -n "$dlfiles$dlprefiles" || test no != "$dlself"; then - if test -n "$NM" && test -n "$global_symbol_pipe"; then - my_dlsyms=${my_outputname}S.c - else - func_error "not configured to extract global symbols from dlpreopened files" - fi - fi - - if test -n "$my_dlsyms"; then - case $my_dlsyms in - "") ;; - *.c) - # Discover the nlist of each of the dlfiles. - nlist=$output_objdir/$my_outputname.nm - - func_show_eval "$RM $nlist ${nlist}S ${nlist}T" - - # Parse the name list into a source file. - func_verbose "creating $output_objdir/$my_dlsyms" - - $opt_dry_run || $ECHO > "$output_objdir/$my_dlsyms" "\ -/* $my_dlsyms - symbol resolution table for '$my_outputname' dlsym emulation. */ -/* Generated by $PROGRAM (GNU $PACKAGE) $VERSION */ - -#ifdef __cplusplus -extern \"C\" { -#endif - -#if defined __GNUC__ && (((__GNUC__ == 4) && (__GNUC_MINOR__ >= 4)) || (__GNUC__ > 4)) -#pragma GCC diagnostic ignored \"-Wstrict-prototypes\" -#endif - -/* Keep this code in sync between libtool.m4, ltmain, lt_system.h, and tests. */ -#if defined _WIN32 || defined __CYGWIN__ || defined _WIN32_WCE -/* DATA imports from DLLs on WIN32 can't be const, because runtime - relocations are performed -- see ld's documentation on pseudo-relocs. */ -# define LT_DLSYM_CONST -#elif defined __osf__ -/* This system does not cope well with relocations in const data. */ -# define LT_DLSYM_CONST -#else -# define LT_DLSYM_CONST const -#endif - -#define STREQ(s1, s2) (strcmp ((s1), (s2)) == 0) - -/* External symbol declarations for the compiler. */\ -" - - if test yes = "$dlself"; then - func_verbose "generating symbol list for '$output'" - - $opt_dry_run || echo ': @PROGRAM@ ' > "$nlist" - - # Add our own program objects to the symbol list. - progfiles=`$ECHO "$objs$old_deplibs" | $SP2NL | $SED "$lo2o" | $NL2SP` - for progfile in $progfiles; do - func_to_tool_file "$progfile" func_convert_file_msys_to_w32 - func_verbose "extracting global C symbols from '$func_to_tool_file_result'" - $opt_dry_run || eval "$NM $func_to_tool_file_result | $global_symbol_pipe >> '$nlist'" - done - - if test -n "$exclude_expsyms"; then - $opt_dry_run || { - eval '$EGREP -v " ($exclude_expsyms)$" "$nlist" > "$nlist"T' - eval '$MV "$nlist"T "$nlist"' - } - fi - - if test -n "$export_symbols_regex"; then - $opt_dry_run || { - eval '$EGREP -e "$export_symbols_regex" "$nlist" > "$nlist"T' - eval '$MV "$nlist"T "$nlist"' - } - fi - - # Prepare the list of exported symbols - if test -z "$export_symbols"; then - export_symbols=$output_objdir/$outputname.exp - $opt_dry_run || { - $RM $export_symbols - eval "$SED -n -e '/^: @PROGRAM@ $/d' -e 's/^.* \(.*\)$/\1/p' "'< "$nlist" > "$export_symbols"' - case $host in - *cygwin* | *mingw* | *cegcc* ) - eval "echo EXPORTS "'> "$output_objdir/$outputname.def"' - eval 'cat "$export_symbols" >> "$output_objdir/$outputname.def"' - ;; - esac - } - else - $opt_dry_run || { - eval "$SED -e 's/\([].[*^$]\)/\\\\\1/g' -e 's/^/ /' -e 's/$/$/'"' < "$export_symbols" > "$output_objdir/$outputname.exp"' - eval '$GREP -f "$output_objdir/$outputname.exp" < "$nlist" > "$nlist"T' - eval '$MV "$nlist"T "$nlist"' - case $host in - *cygwin* | *mingw* | *cegcc* ) - eval "echo EXPORTS "'> "$output_objdir/$outputname.def"' - eval 'cat "$nlist" >> "$output_objdir/$outputname.def"' - ;; - esac - } - fi - fi - - for dlprefile in $dlprefiles; do - func_verbose "extracting global C symbols from '$dlprefile'" - func_basename "$dlprefile" - name=$func_basename_result - case $host in - *cygwin* | *mingw* | *cegcc* ) - # if an import library, we need to obtain dlname - if func_win32_import_lib_p "$dlprefile"; then - func_tr_sh "$dlprefile" - eval "curr_lafile=\$libfile_$func_tr_sh_result" - dlprefile_dlbasename= - if test -n "$curr_lafile" && func_lalib_p "$curr_lafile"; then - # Use subshell, to avoid clobbering current variable values - dlprefile_dlname=`source "$curr_lafile" && echo "$dlname"` - if test -n "$dlprefile_dlname"; then - func_basename "$dlprefile_dlname" - dlprefile_dlbasename=$func_basename_result - else - # no lafile. user explicitly requested -dlpreopen . - $sharedlib_from_linklib_cmd "$dlprefile" - dlprefile_dlbasename=$sharedlib_from_linklib_result - fi - fi - $opt_dry_run || { - if test -n "$dlprefile_dlbasename"; then - eval '$ECHO ": $dlprefile_dlbasename" >> "$nlist"' - else - func_warning "Could not compute DLL name from $name" - eval '$ECHO ": $name " >> "$nlist"' - fi - func_to_tool_file "$dlprefile" func_convert_file_msys_to_w32 - eval "$NM \"$func_to_tool_file_result\" 2>/dev/null | $global_symbol_pipe | - $SED -e '/I __imp/d' -e 's/I __nm_/D /;s/_nm__//' >> '$nlist'" - } - else # not an import lib - $opt_dry_run || { - eval '$ECHO ": $name " >> "$nlist"' - func_to_tool_file "$dlprefile" func_convert_file_msys_to_w32 - eval "$NM \"$func_to_tool_file_result\" 2>/dev/null | $global_symbol_pipe >> '$nlist'" - } - fi - ;; - *) - $opt_dry_run || { - eval '$ECHO ": $name " >> "$nlist"' - func_to_tool_file "$dlprefile" func_convert_file_msys_to_w32 - eval "$NM \"$func_to_tool_file_result\" 2>/dev/null | $global_symbol_pipe >> '$nlist'" - } - ;; - esac - done - - $opt_dry_run || { - # Make sure we have at least an empty file. - test -f "$nlist" || : > "$nlist" - - if test -n "$exclude_expsyms"; then - $EGREP -v " ($exclude_expsyms)$" "$nlist" > "$nlist"T - $MV "$nlist"T "$nlist" - fi - - # Try sorting and uniquifying the output. - if $GREP -v "^: " < "$nlist" | - if sort -k 3 /dev/null 2>&1; then - sort -k 3 - else - sort +2 - fi | - uniq > "$nlist"S; then - : - else - $GREP -v "^: " < "$nlist" > "$nlist"S - fi - - if test -f "$nlist"S; then - eval "$global_symbol_to_cdecl"' < "$nlist"S >> "$output_objdir/$my_dlsyms"' - else - echo '/* NONE */' >> "$output_objdir/$my_dlsyms" - fi - - func_show_eval '$RM "${nlist}I"' - if test -n "$global_symbol_to_import"; then - eval "$global_symbol_to_import"' < "$nlist"S > "$nlist"I' - fi - - echo >> "$output_objdir/$my_dlsyms" "\ - -/* The mapping between symbol names and symbols. */ -typedef struct { - const char *name; - void *address; -} lt_dlsymlist; -extern LT_DLSYM_CONST lt_dlsymlist -lt_${my_prefix}_LTX_preloaded_symbols[];\ -" - - if test -s "$nlist"I; then - echo >> "$output_objdir/$my_dlsyms" "\ -static void lt_syminit(void) -{ - LT_DLSYM_CONST lt_dlsymlist *symbol = lt_${my_prefix}_LTX_preloaded_symbols; - for (; symbol->name; ++symbol) - {" - $SED 's/.*/ if (STREQ (symbol->name, \"&\")) symbol->address = (void *) \&&;/' < "$nlist"I >> "$output_objdir/$my_dlsyms" - echo >> "$output_objdir/$my_dlsyms" "\ - } -}" - fi - echo >> "$output_objdir/$my_dlsyms" "\ -LT_DLSYM_CONST lt_dlsymlist -lt_${my_prefix}_LTX_preloaded_symbols[] = -{ {\"$my_originator\", (void *) 0}," - - if test -s "$nlist"I; then - echo >> "$output_objdir/$my_dlsyms" "\ - {\"@INIT@\", (void *) <_syminit}," - fi - - case $need_lib_prefix in - no) - eval "$global_symbol_to_c_name_address" < "$nlist" >> "$output_objdir/$my_dlsyms" - ;; - *) - eval "$global_symbol_to_c_name_address_lib_prefix" < "$nlist" >> "$output_objdir/$my_dlsyms" - ;; - esac - echo >> "$output_objdir/$my_dlsyms" "\ - {0, (void *) 0} -}; - -/* This works around a problem in FreeBSD linker */ -#ifdef FREEBSD_WORKAROUND -static const void *lt_preloaded_setup() { - return lt_${my_prefix}_LTX_preloaded_symbols; -} -#endif - -#ifdef __cplusplus -} -#endif\ -" - } # !$opt_dry_run - - pic_flag_for_symtable= - case "$compile_command " in - *" -static "*) ;; - *) - case $host in - # compiling the symbol table file with pic_flag works around - # a FreeBSD bug that causes programs to crash when -lm is - # linked before any other PIC object. But we must not use - # pic_flag when linking with -static. The problem exists in - # FreeBSD 2.2.6 and is fixed in FreeBSD 3.1. - *-*-freebsd2.*|*-*-freebsd3.0*|*-*-freebsdelf3.0*) - pic_flag_for_symtable=" $pic_flag -DFREEBSD_WORKAROUND" ;; - *-*-hpux*) - pic_flag_for_symtable=" $pic_flag" ;; - *) - $my_pic_p && pic_flag_for_symtable=" $pic_flag" - ;; - esac - ;; - esac - symtab_cflags= - for arg in $LTCFLAGS; do - case $arg in - -pie | -fpie | -fPIE) ;; - *) func_append symtab_cflags " $arg" ;; - esac - done - - # Now compile the dynamic symbol file. - func_show_eval '(cd $output_objdir && $LTCC$symtab_cflags -c$no_builtin_flag$pic_flag_for_symtable "$my_dlsyms")' 'exit $?' - - # Clean up the generated files. - func_show_eval '$RM "$output_objdir/$my_dlsyms" "$nlist" "${nlist}S" "${nlist}T" "${nlist}I"' - - # Transform the symbol file into the correct name. - symfileobj=$output_objdir/${my_outputname}S.$objext - case $host in - *cygwin* | *mingw* | *cegcc* ) - if test -f "$output_objdir/$my_outputname.def"; then - compile_command=`$ECHO "$compile_command" | $SED "s%@SYMFILE@%$output_objdir/$my_outputname.def $symfileobj%"` - finalize_command=`$ECHO "$finalize_command" | $SED "s%@SYMFILE@%$output_objdir/$my_outputname.def $symfileobj%"` - else - compile_command=`$ECHO "$compile_command" | $SED "s%@SYMFILE@%$symfileobj%"` - finalize_command=`$ECHO "$finalize_command" | $SED "s%@SYMFILE@%$symfileobj%"` - fi - ;; - *) - compile_command=`$ECHO "$compile_command" | $SED "s%@SYMFILE@%$symfileobj%"` - finalize_command=`$ECHO "$finalize_command" | $SED "s%@SYMFILE@%$symfileobj%"` - ;; - esac - ;; - *) - func_fatal_error "unknown suffix for '$my_dlsyms'" - ;; - esac - else - # We keep going just in case the user didn't refer to - # lt_preloaded_symbols. The linker will fail if global_symbol_pipe - # really was required. - - # Nullify the symbol file. - compile_command=`$ECHO "$compile_command" | $SED "s% @SYMFILE@%%"` - finalize_command=`$ECHO "$finalize_command" | $SED "s% @SYMFILE@%%"` - fi -} - -# func_cygming_gnu_implib_p ARG -# This predicate returns with zero status (TRUE) if -# ARG is a GNU/binutils-style import library. Returns -# with nonzero status (FALSE) otherwise. -func_cygming_gnu_implib_p () -{ - $debug_cmd - - func_to_tool_file "$1" func_convert_file_msys_to_w32 - func_cygming_gnu_implib_tmp=`$NM "$func_to_tool_file_result" | eval "$global_symbol_pipe" | $EGREP ' (_head_[A-Za-z0-9_]+_[ad]l*|[A-Za-z0-9_]+_[ad]l*_iname)$'` - test -n "$func_cygming_gnu_implib_tmp" -} - -# func_cygming_ms_implib_p ARG -# This predicate returns with zero status (TRUE) if -# ARG is an MS-style import library. Returns -# with nonzero status (FALSE) otherwise. -func_cygming_ms_implib_p () -{ - $debug_cmd - - func_to_tool_file "$1" func_convert_file_msys_to_w32 - func_cygming_ms_implib_tmp=`$NM "$func_to_tool_file_result" | eval "$global_symbol_pipe" | $GREP '_NULL_IMPORT_DESCRIPTOR'` - test -n "$func_cygming_ms_implib_tmp" -} - -# func_win32_libid arg -# return the library type of file 'arg' -# -# Need a lot of goo to handle *both* DLLs and import libs -# Has to be a shell function in order to 'eat' the argument -# that is supplied when $file_magic_command is called. -# Despite the name, also deal with 64 bit binaries. -func_win32_libid () -{ - $debug_cmd - - win32_libid_type=unknown - win32_fileres=`file -L $1 2>/dev/null` - case $win32_fileres in - *ar\ archive\ import\ library*) # definitely import - win32_libid_type="x86 archive import" - ;; - *ar\ archive*) # could be an import, or static - # Keep the egrep pattern in sync with the one in _LT_CHECK_MAGIC_METHOD. - if eval $OBJDUMP -f $1 | $SED -e '10q' 2>/dev/null | - $EGREP 'file format (pei*-i386(.*architecture: i386)?|pe-arm-wince|pe-x86-64)' >/dev/null; then - case $nm_interface in - "MS dumpbin") - if func_cygming_ms_implib_p "$1" || - func_cygming_gnu_implib_p "$1" - then - win32_nmres=import - else - win32_nmres= - fi - ;; - *) - func_to_tool_file "$1" func_convert_file_msys_to_w32 - win32_nmres=`eval $NM -f posix -A \"$func_to_tool_file_result\" | - $SED -n -e ' - 1,100{ - / I /{ - s|.*|import| - p - q - } - }'` - ;; - esac - case $win32_nmres in - import*) win32_libid_type="x86 archive import";; - *) win32_libid_type="x86 archive static";; - esac - fi - ;; - *DLL*) - win32_libid_type="x86 DLL" - ;; - *executable*) # but shell scripts are "executable" too... - case $win32_fileres in - *MS\ Windows\ PE\ Intel*) - win32_libid_type="x86 DLL" - ;; - esac - ;; - esac - $ECHO "$win32_libid_type" -} - -# func_cygming_dll_for_implib ARG -# -# Platform-specific function to extract the -# name of the DLL associated with the specified -# import library ARG. -# Invoked by eval'ing the libtool variable -# $sharedlib_from_linklib_cmd -# Result is available in the variable -# $sharedlib_from_linklib_result -func_cygming_dll_for_implib () -{ - $debug_cmd - - sharedlib_from_linklib_result=`$DLLTOOL --identify-strict --identify "$1"` -} - -# func_cygming_dll_for_implib_fallback_core SECTION_NAME LIBNAMEs -# -# The is the core of a fallback implementation of a -# platform-specific function to extract the name of the -# DLL associated with the specified import library LIBNAME. -# -# SECTION_NAME is either .idata$6 or .idata$7, depending -# on the platform and compiler that created the implib. -# -# Echos the name of the DLL associated with the -# specified import library. -func_cygming_dll_for_implib_fallback_core () -{ - $debug_cmd - - match_literal=`$ECHO "$1" | $SED "$sed_make_literal_regex"` - $OBJDUMP -s --section "$1" "$2" 2>/dev/null | - $SED '/^Contents of section '"$match_literal"':/{ - # Place marker at beginning of archive member dllname section - s/.*/====MARK====/ - p - d - } - # These lines can sometimes be longer than 43 characters, but - # are always uninteresting - /:[ ]*file format pe[i]\{,1\}-/d - /^In archive [^:]*:/d - # Ensure marker is printed - /^====MARK====/p - # Remove all lines with less than 43 characters - /^.\{43\}/!d - # From remaining lines, remove first 43 characters - s/^.\{43\}//' | - $SED -n ' - # Join marker and all lines until next marker into a single line - /^====MARK====/ b para - H - $ b para - b - :para - x - s/\n//g - # Remove the marker - s/^====MARK====// - # Remove trailing dots and whitespace - s/[\. \t]*$// - # Print - /./p' | - # we now have a list, one entry per line, of the stringified - # contents of the appropriate section of all members of the - # archive that possess that section. Heuristic: eliminate - # all those that have a first or second character that is - # a '.' (that is, objdump's representation of an unprintable - # character.) This should work for all archives with less than - # 0x302f exports -- but will fail for DLLs whose name actually - # begins with a literal '.' or a single character followed by - # a '.'. - # - # Of those that remain, print the first one. - $SED -e '/^\./d;/^.\./d;q' -} - -# func_cygming_dll_for_implib_fallback ARG -# Platform-specific function to extract the -# name of the DLL associated with the specified -# import library ARG. -# -# This fallback implementation is for use when $DLLTOOL -# does not support the --identify-strict option. -# Invoked by eval'ing the libtool variable -# $sharedlib_from_linklib_cmd -# Result is available in the variable -# $sharedlib_from_linklib_result -func_cygming_dll_for_implib_fallback () -{ - $debug_cmd - - if func_cygming_gnu_implib_p "$1"; then - # binutils import library - sharedlib_from_linklib_result=`func_cygming_dll_for_implib_fallback_core '.idata$7' "$1"` - elif func_cygming_ms_implib_p "$1"; then - # ms-generated import library - sharedlib_from_linklib_result=`func_cygming_dll_for_implib_fallback_core '.idata$6' "$1"` - else - # unknown - sharedlib_from_linklib_result= - fi -} - - -# func_extract_an_archive dir oldlib -func_extract_an_archive () -{ - $debug_cmd - - f_ex_an_ar_dir=$1; shift - f_ex_an_ar_oldlib=$1 - if test yes = "$lock_old_archive_extraction"; then - lockfile=$f_ex_an_ar_oldlib.lock - until $opt_dry_run || ln "$progpath" "$lockfile" 2>/dev/null; do - func_echo "Waiting for $lockfile to be removed" - sleep 2 - done - fi - func_show_eval "(cd \$f_ex_an_ar_dir && $AR x \"\$f_ex_an_ar_oldlib\")" \ - 'stat=$?; rm -f "$lockfile"; exit $stat' - if test yes = "$lock_old_archive_extraction"; then - $opt_dry_run || rm -f "$lockfile" - fi - if ($AR t "$f_ex_an_ar_oldlib" | sort | sort -uc >/dev/null 2>&1); then - : - else - func_fatal_error "object name conflicts in archive: $f_ex_an_ar_dir/$f_ex_an_ar_oldlib" - fi -} - - -# func_extract_archives gentop oldlib ... -func_extract_archives () -{ - $debug_cmd - - my_gentop=$1; shift - my_oldlibs=${1+"$@"} - my_oldobjs= - my_xlib= - my_xabs= - my_xdir= - - for my_xlib in $my_oldlibs; do - # Extract the objects. - case $my_xlib in - [\\/]* | [A-Za-z]:[\\/]*) my_xabs=$my_xlib ;; - *) my_xabs=`pwd`"/$my_xlib" ;; - esac - func_basename "$my_xlib" - my_xlib=$func_basename_result - my_xlib_u=$my_xlib - while :; do - case " $extracted_archives " in - *" $my_xlib_u "*) - func_arith $extracted_serial + 1 - extracted_serial=$func_arith_result - my_xlib_u=lt$extracted_serial-$my_xlib ;; - *) break ;; - esac - done - extracted_archives="$extracted_archives $my_xlib_u" - my_xdir=$my_gentop/$my_xlib_u - - func_mkdir_p "$my_xdir" - - case $host in - *-darwin*) - func_verbose "Extracting $my_xabs" - # Do not bother doing anything if just a dry run - $opt_dry_run || { - darwin_orig_dir=`pwd` - cd $my_xdir || exit $? - darwin_archive=$my_xabs - darwin_curdir=`pwd` - func_basename "$darwin_archive" - darwin_base_archive=$func_basename_result - darwin_arches=`$LIPO -info "$darwin_archive" 2>/dev/null | $GREP Architectures 2>/dev/null || true` - if test -n "$darwin_arches"; then - darwin_arches=`$ECHO "$darwin_arches" | $SED -e 's/.*are://'` - darwin_arch= - func_verbose "$darwin_base_archive has multiple architectures $darwin_arches" - for darwin_arch in $darwin_arches; do - func_mkdir_p "unfat-$$/$darwin_base_archive-$darwin_arch" - $LIPO -thin $darwin_arch -output "unfat-$$/$darwin_base_archive-$darwin_arch/$darwin_base_archive" "$darwin_archive" - cd "unfat-$$/$darwin_base_archive-$darwin_arch" - func_extract_an_archive "`pwd`" "$darwin_base_archive" - cd "$darwin_curdir" - $RM "unfat-$$/$darwin_base_archive-$darwin_arch/$darwin_base_archive" - done # $darwin_arches - ## Okay now we've a bunch of thin objects, gotta fatten them up :) - darwin_filelist=`find unfat-$$ -type f -name \*.o -print -o -name \*.lo -print | $SED -e "$sed_basename" | sort -u` - darwin_file= - darwin_files= - for darwin_file in $darwin_filelist; do - darwin_files=`find unfat-$$ -name $darwin_file -print | sort | $NL2SP` - $LIPO -create -output "$darwin_file" $darwin_files - done # $darwin_filelist - $RM -rf unfat-$$ - cd "$darwin_orig_dir" - else - cd $darwin_orig_dir - func_extract_an_archive "$my_xdir" "$my_xabs" - fi # $darwin_arches - } # !$opt_dry_run - ;; - *) - func_extract_an_archive "$my_xdir" "$my_xabs" - ;; - esac - my_oldobjs="$my_oldobjs "`find $my_xdir -name \*.$objext -print -o -name \*.lo -print | sort | $NL2SP` - done - - func_extract_archives_result=$my_oldobjs -} - - -# func_emit_wrapper [arg=no] -# -# Emit a libtool wrapper script on stdout. -# Don't directly open a file because we may want to -# incorporate the script contents within a cygwin/mingw -# wrapper executable. Must ONLY be called from within -# func_mode_link because it depends on a number of variables -# set therein. -# -# ARG is the value that the WRAPPER_SCRIPT_BELONGS_IN_OBJDIR -# variable will take. If 'yes', then the emitted script -# will assume that the directory where it is stored is -# the $objdir directory. This is a cygwin/mingw-specific -# behavior. -func_emit_wrapper () -{ - func_emit_wrapper_arg1=${1-no} - - $ECHO "\ -#! $SHELL - -# $output - temporary wrapper script for $objdir/$outputname -# Generated by $PROGRAM (GNU $PACKAGE) $VERSION -# -# The $output program cannot be directly executed until all the libtool -# libraries that it depends on are installed. -# -# This wrapper script should never be moved out of the build directory. -# If it is, it will not operate correctly. - -# Sed substitution that helps us do robust quoting. It backslashifies -# metacharacters that are still active within double-quoted strings. -sed_quote_subst='$sed_quote_subst' - -# Be Bourne compatible -if test -n \"\${ZSH_VERSION+set}\" && (emulate sh) >/dev/null 2>&1; then - emulate sh - NULLCMD=: - # Zsh 3.x and 4.x performs word splitting on \${1+\"\$@\"}, which - # is contrary to our usage. Disable this feature. - alias -g '\${1+\"\$@\"}'='\"\$@\"' - setopt NO_GLOB_SUBST -else - case \`(set -o) 2>/dev/null\` in *posix*) set -o posix;; esac -fi -BIN_SH=xpg4; export BIN_SH # for Tru64 -DUALCASE=1; export DUALCASE # for MKS sh - -# The HP-UX ksh and POSIX shell print the target directory to stdout -# if CDPATH is set. -(unset CDPATH) >/dev/null 2>&1 && unset CDPATH - -relink_command=\"$relink_command\" - -# This environment variable determines our operation mode. -if test \"\$libtool_install_magic\" = \"$magic\"; then - # install mode needs the following variables: - generated_by_libtool_version='$macro_version' - notinst_deplibs='$notinst_deplibs' -else - # When we are sourced in execute mode, \$file and \$ECHO are already set. - if test \"\$libtool_execute_magic\" != \"$magic\"; then - file=\"\$0\"" - - qECHO=`$ECHO "$ECHO" | $SED "$sed_quote_subst"` - $ECHO "\ - -# A function that is used when there is no print builtin or printf. -func_fallback_echo () -{ - eval 'cat <<_LTECHO_EOF -\$1 -_LTECHO_EOF' -} - ECHO=\"$qECHO\" - fi - -# Very basic option parsing. These options are (a) specific to -# the libtool wrapper, (b) are identical between the wrapper -# /script/ and the wrapper /executable/ that is used only on -# windows platforms, and (c) all begin with the string "--lt-" -# (application programs are unlikely to have options that match -# this pattern). -# -# There are only two supported options: --lt-debug and -# --lt-dump-script. There is, deliberately, no --lt-help. -# -# The first argument to this parsing function should be the -# script's $0 value, followed by "$@". -lt_option_debug= -func_parse_lt_options () -{ - lt_script_arg0=\$0 - shift - for lt_opt - do - case \"\$lt_opt\" in - --lt-debug) lt_option_debug=1 ;; - --lt-dump-script) - lt_dump_D=\`\$ECHO \"X\$lt_script_arg0\" | $SED -e 's/^X//' -e 's%/[^/]*$%%'\` - test \"X\$lt_dump_D\" = \"X\$lt_script_arg0\" && lt_dump_D=. - lt_dump_F=\`\$ECHO \"X\$lt_script_arg0\" | $SED -e 's/^X//' -e 's%^.*/%%'\` - cat \"\$lt_dump_D/\$lt_dump_F\" - exit 0 - ;; - --lt-*) - \$ECHO \"Unrecognized --lt- option: '\$lt_opt'\" 1>&2 - exit 1 - ;; - esac - done - - # Print the debug banner immediately: - if test -n \"\$lt_option_debug\"; then - echo \"$outputname:$output:\$LINENO: libtool wrapper (GNU $PACKAGE) $VERSION\" 1>&2 - fi -} - -# Used when --lt-debug. Prints its arguments to stdout -# (redirection is the responsibility of the caller) -func_lt_dump_args () -{ - lt_dump_args_N=1; - for lt_arg - do - \$ECHO \"$outputname:$output:\$LINENO: newargv[\$lt_dump_args_N]: \$lt_arg\" - lt_dump_args_N=\`expr \$lt_dump_args_N + 1\` - done -} - -# Core function for launching the target application -func_exec_program_core () -{ -" - case $host in - # Backslashes separate directories on plain windows - *-*-mingw | *-*-os2* | *-cegcc*) - $ECHO "\ - if test -n \"\$lt_option_debug\"; then - \$ECHO \"$outputname:$output:\$LINENO: newargv[0]: \$progdir\\\\\$program\" 1>&2 - func_lt_dump_args \${1+\"\$@\"} 1>&2 - fi - exec \"\$progdir\\\\\$program\" \${1+\"\$@\"} -" - ;; - - *) - $ECHO "\ - if test -n \"\$lt_option_debug\"; then - \$ECHO \"$outputname:$output:\$LINENO: newargv[0]: \$progdir/\$program\" 1>&2 - func_lt_dump_args \${1+\"\$@\"} 1>&2 - fi - exec \"\$progdir/\$program\" \${1+\"\$@\"} -" - ;; - esac - $ECHO "\ - \$ECHO \"\$0: cannot exec \$program \$*\" 1>&2 - exit 1 -} - -# A function to encapsulate launching the target application -# Strips options in the --lt-* namespace from \$@ and -# launches target application with the remaining arguments. -func_exec_program () -{ - case \" \$* \" in - *\\ --lt-*) - for lt_wr_arg - do - case \$lt_wr_arg in - --lt-*) ;; - *) set x \"\$@\" \"\$lt_wr_arg\"; shift;; - esac - shift - done ;; - esac - func_exec_program_core \${1+\"\$@\"} -} - - # Parse options - func_parse_lt_options \"\$0\" \${1+\"\$@\"} - - # Find the directory that this script lives in. - thisdir=\`\$ECHO \"\$file\" | $SED 's%/[^/]*$%%'\` - test \"x\$thisdir\" = \"x\$file\" && thisdir=. - - # Follow symbolic links until we get to the real thisdir. - file=\`ls -ld \"\$file\" | $SED -n 's/.*-> //p'\` - while test -n \"\$file\"; do - destdir=\`\$ECHO \"\$file\" | $SED 's%/[^/]*\$%%'\` - - # If there was a directory component, then change thisdir. - if test \"x\$destdir\" != \"x\$file\"; then - case \"\$destdir\" in - [\\\\/]* | [A-Za-z]:[\\\\/]*) thisdir=\"\$destdir\" ;; - *) thisdir=\"\$thisdir/\$destdir\" ;; - esac - fi - - file=\`\$ECHO \"\$file\" | $SED 's%^.*/%%'\` - file=\`ls -ld \"\$thisdir/\$file\" | $SED -n 's/.*-> //p'\` - done - - # Usually 'no', except on cygwin/mingw when embedded into - # the cwrapper. - WRAPPER_SCRIPT_BELONGS_IN_OBJDIR=$func_emit_wrapper_arg1 - if test \"\$WRAPPER_SCRIPT_BELONGS_IN_OBJDIR\" = \"yes\"; then - # special case for '.' - if test \"\$thisdir\" = \".\"; then - thisdir=\`pwd\` - fi - # remove .libs from thisdir - case \"\$thisdir\" in - *[\\\\/]$objdir ) thisdir=\`\$ECHO \"\$thisdir\" | $SED 's%[\\\\/][^\\\\/]*$%%'\` ;; - $objdir ) thisdir=. ;; - esac - fi - - # Try to get the absolute directory name. - absdir=\`cd \"\$thisdir\" && pwd\` - test -n \"\$absdir\" && thisdir=\"\$absdir\" -" - - if test yes = "$fast_install"; then - $ECHO "\ - program=lt-'$outputname'$exeext - progdir=\"\$thisdir/$objdir\" - - if test ! -f \"\$progdir/\$program\" || - { file=\`ls -1dt \"\$progdir/\$program\" \"\$progdir/../\$program\" 2>/dev/null | $SED 1q\`; \\ - test \"X\$file\" != \"X\$progdir/\$program\"; }; then - - file=\"\$\$-\$program\" - - if test ! -d \"\$progdir\"; then - $MKDIR \"\$progdir\" - else - $RM \"\$progdir/\$file\" - fi" - - $ECHO "\ - - # relink executable if necessary - if test -n \"\$relink_command\"; then - if relink_command_output=\`eval \$relink_command 2>&1\`; then : - else - \$ECHO \"\$relink_command_output\" >&2 - $RM \"\$progdir/\$file\" - exit 1 - fi - fi - - $MV \"\$progdir/\$file\" \"\$progdir/\$program\" 2>/dev/null || - { $RM \"\$progdir/\$program\"; - $MV \"\$progdir/\$file\" \"\$progdir/\$program\"; } - $RM \"\$progdir/\$file\" - fi" - else - $ECHO "\ - program='$outputname' - progdir=\"\$thisdir/$objdir\" -" - fi - - $ECHO "\ - - if test -f \"\$progdir/\$program\"; then" - - # fixup the dll searchpath if we need to. - # - # Fix the DLL searchpath if we need to. Do this before prepending - # to shlibpath, because on Windows, both are PATH and uninstalled - # libraries must come first. - if test -n "$dllsearchpath"; then - $ECHO "\ - # Add the dll search path components to the executable PATH - PATH=$dllsearchpath:\$PATH -" - fi - - # Export our shlibpath_var if we have one. - if test yes = "$shlibpath_overrides_runpath" && test -n "$shlibpath_var" && test -n "$temp_rpath"; then - $ECHO "\ - # Add our own library path to $shlibpath_var - $shlibpath_var=\"$temp_rpath\$$shlibpath_var\" - - # Some systems cannot cope with colon-terminated $shlibpath_var - # The second colon is a workaround for a bug in BeOS R4 sed - $shlibpath_var=\`\$ECHO \"\$$shlibpath_var\" | $SED 's/::*\$//'\` - - export $shlibpath_var -" - fi - - $ECHO "\ - if test \"\$libtool_execute_magic\" != \"$magic\"; then - # Run the actual program with our arguments. - func_exec_program \${1+\"\$@\"} - fi - else - # The program doesn't exist. - \$ECHO \"\$0: error: '\$progdir/\$program' does not exist\" 1>&2 - \$ECHO \"This script is just a wrapper for \$program.\" 1>&2 - \$ECHO \"See the $PACKAGE documentation for more information.\" 1>&2 - exit 1 - fi -fi\ -" -} - - -# func_emit_cwrapperexe_src -# emit the source code for a wrapper executable on stdout -# Must ONLY be called from within func_mode_link because -# it depends on a number of variable set therein. -func_emit_cwrapperexe_src () -{ - cat < -#include -#ifdef _MSC_VER -# include -# include -# include -#else -# include -# include -# ifdef __CYGWIN__ -# include -# endif -#endif -#include -#include -#include -#include -#include -#include -#include -#include - -#define STREQ(s1, s2) (strcmp ((s1), (s2)) == 0) - -/* declarations of non-ANSI functions */ -#if defined __MINGW32__ -# ifdef __STRICT_ANSI__ -int _putenv (const char *); -# endif -#elif defined __CYGWIN__ -# ifdef __STRICT_ANSI__ -char *realpath (const char *, char *); -int putenv (char *); -int setenv (const char *, const char *, int); -# endif -/* #elif defined other_platform || defined ... */ -#endif - -/* portability defines, excluding path handling macros */ -#if defined _MSC_VER -# define setmode _setmode -# define stat _stat -# define chmod _chmod -# define getcwd _getcwd -# define putenv _putenv -# define S_IXUSR _S_IEXEC -#elif defined __MINGW32__ -# define setmode _setmode -# define stat _stat -# define chmod _chmod -# define getcwd _getcwd -# define putenv _putenv -#elif defined __CYGWIN__ -# define HAVE_SETENV -# define FOPEN_WB "wb" -/* #elif defined other platforms ... */ -#endif - -#if defined PATH_MAX -# define LT_PATHMAX PATH_MAX -#elif defined MAXPATHLEN -# define LT_PATHMAX MAXPATHLEN -#else -# define LT_PATHMAX 1024 -#endif - -#ifndef S_IXOTH -# define S_IXOTH 0 -#endif -#ifndef S_IXGRP -# define S_IXGRP 0 -#endif - -/* path handling portability macros */ -#ifndef DIR_SEPARATOR -# define DIR_SEPARATOR '/' -# define PATH_SEPARATOR ':' -#endif - -#if defined _WIN32 || defined __MSDOS__ || defined __DJGPP__ || \ - defined __OS2__ -# define HAVE_DOS_BASED_FILE_SYSTEM -# define FOPEN_WB "wb" -# ifndef DIR_SEPARATOR_2 -# define DIR_SEPARATOR_2 '\\' -# endif -# ifndef PATH_SEPARATOR_2 -# define PATH_SEPARATOR_2 ';' -# endif -#endif - -#ifndef DIR_SEPARATOR_2 -# define IS_DIR_SEPARATOR(ch) ((ch) == DIR_SEPARATOR) -#else /* DIR_SEPARATOR_2 */ -# define IS_DIR_SEPARATOR(ch) \ - (((ch) == DIR_SEPARATOR) || ((ch) == DIR_SEPARATOR_2)) -#endif /* DIR_SEPARATOR_2 */ - -#ifndef PATH_SEPARATOR_2 -# define IS_PATH_SEPARATOR(ch) ((ch) == PATH_SEPARATOR) -#else /* PATH_SEPARATOR_2 */ -# define IS_PATH_SEPARATOR(ch) ((ch) == PATH_SEPARATOR_2) -#endif /* PATH_SEPARATOR_2 */ - -#ifndef FOPEN_WB -# define FOPEN_WB "w" -#endif -#ifndef _O_BINARY -# define _O_BINARY 0 -#endif - -#define XMALLOC(type, num) ((type *) xmalloc ((num) * sizeof(type))) -#define XFREE(stale) do { \ - if (stale) { free (stale); stale = 0; } \ -} while (0) - -#if defined LT_DEBUGWRAPPER -static int lt_debug = 1; -#else -static int lt_debug = 0; -#endif - -const char *program_name = "libtool-wrapper"; /* in case xstrdup fails */ - -void *xmalloc (size_t num); -char *xstrdup (const char *string); -const char *base_name (const char *name); -char *find_executable (const char *wrapper); -char *chase_symlinks (const char *pathspec); -int make_executable (const char *path); -int check_executable (const char *path); -char *strendzap (char *str, const char *pat); -void lt_debugprintf (const char *file, int line, const char *fmt, ...); -void lt_fatal (const char *file, int line, const char *message, ...); -static const char *nonnull (const char *s); -static const char *nonempty (const char *s); -void lt_setenv (const char *name, const char *value); -char *lt_extend_str (const char *orig_value, const char *add, int to_end); -void lt_update_exe_path (const char *name, const char *value); -void lt_update_lib_path (const char *name, const char *value); -char **prepare_spawn (char **argv); -void lt_dump_script (FILE *f); -EOF - - cat <= 0) - && (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) - return 1; - else - return 0; -} - -int -make_executable (const char *path) -{ - int rval = 0; - struct stat st; - - lt_debugprintf (__FILE__, __LINE__, "(make_executable): %s\n", - nonempty (path)); - if ((!path) || (!*path)) - return 0; - - if (stat (path, &st) >= 0) - { - rval = chmod (path, st.st_mode | S_IXOTH | S_IXGRP | S_IXUSR); - } - return rval; -} - -/* Searches for the full path of the wrapper. Returns - newly allocated full path name if found, NULL otherwise - Does not chase symlinks, even on platforms that support them. -*/ -char * -find_executable (const char *wrapper) -{ - int has_slash = 0; - const char *p; - const char *p_next; - /* static buffer for getcwd */ - char tmp[LT_PATHMAX + 1]; - size_t tmp_len; - char *concat_name; - - lt_debugprintf (__FILE__, __LINE__, "(find_executable): %s\n", - nonempty (wrapper)); - - if ((wrapper == NULL) || (*wrapper == '\0')) - return NULL; - - /* Absolute path? */ -#if defined HAVE_DOS_BASED_FILE_SYSTEM - if (isalpha ((unsigned char) wrapper[0]) && wrapper[1] == ':') - { - concat_name = xstrdup (wrapper); - if (check_executable (concat_name)) - return concat_name; - XFREE (concat_name); - } - else - { -#endif - if (IS_DIR_SEPARATOR (wrapper[0])) - { - concat_name = xstrdup (wrapper); - if (check_executable (concat_name)) - return concat_name; - XFREE (concat_name); - } -#if defined HAVE_DOS_BASED_FILE_SYSTEM - } -#endif - - for (p = wrapper; *p; p++) - if (*p == '/') - { - has_slash = 1; - break; - } - if (!has_slash) - { - /* no slashes; search PATH */ - const char *path = getenv ("PATH"); - if (path != NULL) - { - for (p = path; *p; p = p_next) - { - const char *q; - size_t p_len; - for (q = p; *q; q++) - if (IS_PATH_SEPARATOR (*q)) - break; - p_len = (size_t) (q - p); - p_next = (*q == '\0' ? q : q + 1); - if (p_len == 0) - { - /* empty path: current directory */ - if (getcwd (tmp, LT_PATHMAX) == NULL) - lt_fatal (__FILE__, __LINE__, "getcwd failed: %s", - nonnull (strerror (errno))); - tmp_len = strlen (tmp); - concat_name = - XMALLOC (char, tmp_len + 1 + strlen (wrapper) + 1); - memcpy (concat_name, tmp, tmp_len); - concat_name[tmp_len] = '/'; - strcpy (concat_name + tmp_len + 1, wrapper); - } - else - { - concat_name = - XMALLOC (char, p_len + 1 + strlen (wrapper) + 1); - memcpy (concat_name, p, p_len); - concat_name[p_len] = '/'; - strcpy (concat_name + p_len + 1, wrapper); - } - if (check_executable (concat_name)) - return concat_name; - XFREE (concat_name); - } - } - /* not found in PATH; assume curdir */ - } - /* Relative path | not found in path: prepend cwd */ - if (getcwd (tmp, LT_PATHMAX) == NULL) - lt_fatal (__FILE__, __LINE__, "getcwd failed: %s", - nonnull (strerror (errno))); - tmp_len = strlen (tmp); - concat_name = XMALLOC (char, tmp_len + 1 + strlen (wrapper) + 1); - memcpy (concat_name, tmp, tmp_len); - concat_name[tmp_len] = '/'; - strcpy (concat_name + tmp_len + 1, wrapper); - - if (check_executable (concat_name)) - return concat_name; - XFREE (concat_name); - return NULL; -} - -char * -chase_symlinks (const char *pathspec) -{ -#ifndef S_ISLNK - return xstrdup (pathspec); -#else - char buf[LT_PATHMAX]; - struct stat s; - char *tmp_pathspec = xstrdup (pathspec); - char *p; - int has_symlinks = 0; - while (strlen (tmp_pathspec) && !has_symlinks) - { - lt_debugprintf (__FILE__, __LINE__, - "checking path component for symlinks: %s\n", - tmp_pathspec); - if (lstat (tmp_pathspec, &s) == 0) - { - if (S_ISLNK (s.st_mode) != 0) - { - has_symlinks = 1; - break; - } - - /* search backwards for last DIR_SEPARATOR */ - p = tmp_pathspec + strlen (tmp_pathspec) - 1; - while ((p > tmp_pathspec) && (!IS_DIR_SEPARATOR (*p))) - p--; - if ((p == tmp_pathspec) && (!IS_DIR_SEPARATOR (*p))) - { - /* no more DIR_SEPARATORS left */ - break; - } - *p = '\0'; - } - else - { - lt_fatal (__FILE__, __LINE__, - "error accessing file \"%s\": %s", - tmp_pathspec, nonnull (strerror (errno))); - } - } - XFREE (tmp_pathspec); - - if (!has_symlinks) - { - return xstrdup (pathspec); - } - - tmp_pathspec = realpath (pathspec, buf); - if (tmp_pathspec == 0) - { - lt_fatal (__FILE__, __LINE__, - "could not follow symlinks for %s", pathspec); - } - return xstrdup (tmp_pathspec); -#endif -} - -char * -strendzap (char *str, const char *pat) -{ - size_t len, patlen; - - assert (str != NULL); - assert (pat != NULL); - - len = strlen (str); - patlen = strlen (pat); - - if (patlen <= len) - { - str += len - patlen; - if (STREQ (str, pat)) - *str = '\0'; - } - return str; -} - -void -lt_debugprintf (const char *file, int line, const char *fmt, ...) -{ - va_list args; - if (lt_debug) - { - (void) fprintf (stderr, "%s:%s:%d: ", program_name, file, line); - va_start (args, fmt); - (void) vfprintf (stderr, fmt, args); - va_end (args); - } -} - -static void -lt_error_core (int exit_status, const char *file, - int line, const char *mode, - const char *message, va_list ap) -{ - fprintf (stderr, "%s:%s:%d: %s: ", program_name, file, line, mode); - vfprintf (stderr, message, ap); - fprintf (stderr, ".\n"); - - if (exit_status >= 0) - exit (exit_status); -} - -void -lt_fatal (const char *file, int line, const char *message, ...) -{ - va_list ap; - va_start (ap, message); - lt_error_core (EXIT_FAILURE, file, line, "FATAL", message, ap); - va_end (ap); -} - -static const char * -nonnull (const char *s) -{ - return s ? s : "(null)"; -} - -static const char * -nonempty (const char *s) -{ - return (s && !*s) ? "(empty)" : nonnull (s); -} - -void -lt_setenv (const char *name, const char *value) -{ - lt_debugprintf (__FILE__, __LINE__, - "(lt_setenv) setting '%s' to '%s'\n", - nonnull (name), nonnull (value)); - { -#ifdef HAVE_SETENV - /* always make a copy, for consistency with !HAVE_SETENV */ - char *str = xstrdup (value); - setenv (name, str, 1); -#else - size_t len = strlen (name) + 1 + strlen (value) + 1; - char *str = XMALLOC (char, len); - sprintf (str, "%s=%s", name, value); - if (putenv (str) != EXIT_SUCCESS) - { - XFREE (str); - } -#endif - } -} - -char * -lt_extend_str (const char *orig_value, const char *add, int to_end) -{ - char *new_value; - if (orig_value && *orig_value) - { - size_t orig_value_len = strlen (orig_value); - size_t add_len = strlen (add); - new_value = XMALLOC (char, add_len + orig_value_len + 1); - if (to_end) - { - strcpy (new_value, orig_value); - strcpy (new_value + orig_value_len, add); - } - else - { - strcpy (new_value, add); - strcpy (new_value + add_len, orig_value); - } - } - else - { - new_value = xstrdup (add); - } - return new_value; -} - -void -lt_update_exe_path (const char *name, const char *value) -{ - lt_debugprintf (__FILE__, __LINE__, - "(lt_update_exe_path) modifying '%s' by prepending '%s'\n", - nonnull (name), nonnull (value)); - - if (name && *name && value && *value) - { - char *new_value = lt_extend_str (getenv (name), value, 0); - /* some systems can't cope with a ':'-terminated path #' */ - size_t len = strlen (new_value); - while ((len > 0) && IS_PATH_SEPARATOR (new_value[len-1])) - { - new_value[--len] = '\0'; - } - lt_setenv (name, new_value); - XFREE (new_value); - } -} - -void -lt_update_lib_path (const char *name, const char *value) -{ - lt_debugprintf (__FILE__, __LINE__, - "(lt_update_lib_path) modifying '%s' by prepending '%s'\n", - nonnull (name), nonnull (value)); - - if (name && *name && value && *value) - { - char *new_value = lt_extend_str (getenv (name), value, 0); - lt_setenv (name, new_value); - XFREE (new_value); - } -} - -EOF - case $host_os in - mingw*) - cat <<"EOF" - -/* Prepares an argument vector before calling spawn(). - Note that spawn() does not by itself call the command interpreter - (getenv ("COMSPEC") != NULL ? getenv ("COMSPEC") : - ({ OSVERSIONINFO v; v.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); - GetVersionEx(&v); - v.dwPlatformId == VER_PLATFORM_WIN32_NT; - }) ? "cmd.exe" : "command.com"). - Instead it simply concatenates the arguments, separated by ' ', and calls - CreateProcess(). We must quote the arguments since Win32 CreateProcess() - interprets characters like ' ', '\t', '\\', '"' (but not '<' and '>') in a - special way: - - Space and tab are interpreted as delimiters. They are not treated as - delimiters if they are surrounded by double quotes: "...". - - Unescaped double quotes are removed from the input. Their only effect is - that within double quotes, space and tab are treated like normal - characters. - - Backslashes not followed by double quotes are not special. - - But 2*n+1 backslashes followed by a double quote become - n backslashes followed by a double quote (n >= 0): - \" -> " - \\\" -> \" - \\\\\" -> \\" - */ -#define SHELL_SPECIAL_CHARS "\"\\ \001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037" -#define SHELL_SPACE_CHARS " \001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037" -char ** -prepare_spawn (char **argv) -{ - size_t argc; - char **new_argv; - size_t i; - - /* Count number of arguments. */ - for (argc = 0; argv[argc] != NULL; argc++) - ; - - /* Allocate new argument vector. */ - new_argv = XMALLOC (char *, argc + 1); - - /* Put quoted arguments into the new argument vector. */ - for (i = 0; i < argc; i++) - { - const char *string = argv[i]; - - if (string[0] == '\0') - new_argv[i] = xstrdup ("\"\""); - else if (strpbrk (string, SHELL_SPECIAL_CHARS) != NULL) - { - int quote_around = (strpbrk (string, SHELL_SPACE_CHARS) != NULL); - size_t length; - unsigned int backslashes; - const char *s; - char *quoted_string; - char *p; - - length = 0; - backslashes = 0; - if (quote_around) - length++; - for (s = string; *s != '\0'; s++) - { - char c = *s; - if (c == '"') - length += backslashes + 1; - length++; - if (c == '\\') - backslashes++; - else - backslashes = 0; - } - if (quote_around) - length += backslashes + 1; - - quoted_string = XMALLOC (char, length + 1); - - p = quoted_string; - backslashes = 0; - if (quote_around) - *p++ = '"'; - for (s = string; *s != '\0'; s++) - { - char c = *s; - if (c == '"') - { - unsigned int j; - for (j = backslashes + 1; j > 0; j--) - *p++ = '\\'; - } - *p++ = c; - if (c == '\\') - backslashes++; - else - backslashes = 0; - } - if (quote_around) - { - unsigned int j; - for (j = backslashes; j > 0; j--) - *p++ = '\\'; - *p++ = '"'; - } - *p = '\0'; - - new_argv[i] = quoted_string; - } - else - new_argv[i] = (char *) string; - } - new_argv[argc] = NULL; - - return new_argv; -} -EOF - ;; - esac - - cat <<"EOF" -void lt_dump_script (FILE* f) -{ -EOF - func_emit_wrapper yes | - $SED -n -e ' -s/^\(.\{79\}\)\(..*\)/\1\ -\2/ -h -s/\([\\"]\)/\\\1/g -s/$/\\n/ -s/\([^\n]*\).*/ fputs ("\1", f);/p -g -D' - cat <<"EOF" -} -EOF -} -# end: func_emit_cwrapperexe_src - -# func_win32_import_lib_p ARG -# True if ARG is an import lib, as indicated by $file_magic_cmd -func_win32_import_lib_p () -{ - $debug_cmd - - case `eval $file_magic_cmd \"\$1\" 2>/dev/null | $SED -e 10q` in - *import*) : ;; - *) false ;; - esac -} - -# func_suncc_cstd_abi -# !!ONLY CALL THIS FOR SUN CC AFTER $compile_command IS FULLY EXPANDED!! -# Several compiler flags select an ABI that is incompatible with the -# Cstd library. Avoid specifying it if any are in CXXFLAGS. -func_suncc_cstd_abi () -{ - $debug_cmd - - case " $compile_command " in - *" -compat=g "*|*\ -std=c++[0-9][0-9]\ *|*" -library=stdcxx4 "*|*" -library=stlport4 "*) - suncc_use_cstd_abi=no - ;; - *) - suncc_use_cstd_abi=yes - ;; - esac -} - -# func_mode_link arg... -func_mode_link () -{ - $debug_cmd - - case $host in - *-*-cygwin* | *-*-mingw* | *-*-pw32* | *-*-os2* | *-cegcc*) - # It is impossible to link a dll without this setting, and - # we shouldn't force the makefile maintainer to figure out - # what system we are compiling for in order to pass an extra - # flag for every libtool invocation. - # allow_undefined=no - - # FIXME: Unfortunately, there are problems with the above when trying - # to make a dll that has undefined symbols, in which case not - # even a static library is built. For now, we need to specify - # -no-undefined on the libtool link line when we can be certain - # that all symbols are satisfied, otherwise we get a static library. - allow_undefined=yes - ;; - *) - allow_undefined=yes - ;; - esac - libtool_args=$nonopt - base_compile="$nonopt $@" - compile_command=$nonopt - finalize_command=$nonopt - - compile_rpath= - finalize_rpath= - compile_shlibpath= - finalize_shlibpath= - convenience= - old_convenience= - deplibs= - old_deplibs= - compiler_flags= - linker_flags= - dllsearchpath= - lib_search_path=`pwd` - inst_prefix_dir= - new_inherited_linker_flags= - - avoid_version=no - bindir= - dlfiles= - dlprefiles= - dlself=no - export_dynamic=no - export_symbols= - export_symbols_regex= - generated= - libobjs= - ltlibs= - module=no - no_install=no - objs= - os2dllname= - non_pic_objects= - precious_files_regex= - prefer_static_libs=no - preload=false - prev= - prevarg= - release= - rpath= - xrpath= - perm_rpath= - temp_rpath= - thread_safe=no - vinfo= - vinfo_number=no - weak_libs= - single_module=$wl-single_module - func_infer_tag $base_compile - - # We need to know -static, to get the right output filenames. - for arg - do - case $arg in - -shared) - test yes != "$build_libtool_libs" \ - && func_fatal_configuration "cannot build a shared library" - build_old_libs=no - break - ;; - -all-static | -static | -static-libtool-libs) - case $arg in - -all-static) - if test yes = "$build_libtool_libs" && test -z "$link_static_flag"; then - func_warning "complete static linking is impossible in this configuration" - fi - if test -n "$link_static_flag"; then - dlopen_self=$dlopen_self_static - fi - prefer_static_libs=yes - ;; - -static) - if test -z "$pic_flag" && test -n "$link_static_flag"; then - dlopen_self=$dlopen_self_static - fi - prefer_static_libs=built - ;; - -static-libtool-libs) - if test -z "$pic_flag" && test -n "$link_static_flag"; then - dlopen_self=$dlopen_self_static - fi - prefer_static_libs=yes - ;; - esac - build_libtool_libs=no - build_old_libs=yes - break - ;; - esac - done - - # See if our shared archives depend on static archives. - test -n "$old_archive_from_new_cmds" && build_old_libs=yes - - # Go through the arguments, transforming them on the way. - while test "$#" -gt 0; do - arg=$1 - shift - func_quote_for_eval "$arg" - qarg=$func_quote_for_eval_unquoted_result - func_append libtool_args " $func_quote_for_eval_result" - - # If the previous option needs an argument, assign it. - if test -n "$prev"; then - case $prev in - output) - func_append compile_command " @OUTPUT@" - func_append finalize_command " @OUTPUT@" - ;; - esac - - case $prev in - bindir) - bindir=$arg - prev= - continue - ;; - dlfiles|dlprefiles) - $preload || { - # Add the symbol object into the linking commands. - func_append compile_command " @SYMFILE@" - func_append finalize_command " @SYMFILE@" - preload=: - } - case $arg in - *.la | *.lo) ;; # We handle these cases below. - force) - if test no = "$dlself"; then - dlself=needless - export_dynamic=yes - fi - prev= - continue - ;; - self) - if test dlprefiles = "$prev"; then - dlself=yes - elif test dlfiles = "$prev" && test yes != "$dlopen_self"; then - dlself=yes - else - dlself=needless - export_dynamic=yes - fi - prev= - continue - ;; - *) - if test dlfiles = "$prev"; then - func_append dlfiles " $arg" - else - func_append dlprefiles " $arg" - fi - prev= - continue - ;; - esac - ;; - expsyms) - export_symbols=$arg - test -f "$arg" \ - || func_fatal_error "symbol file '$arg' does not exist" - prev= - continue - ;; - expsyms_regex) - export_symbols_regex=$arg - prev= - continue - ;; - framework) - case $host in - *-*-darwin*) - case "$deplibs " in - *" $qarg.ltframework "*) ;; - *) func_append deplibs " $qarg.ltframework" # this is fixed later - ;; - esac - ;; - esac - prev= - continue - ;; - inst_prefix) - inst_prefix_dir=$arg - prev= - continue - ;; - mllvm) - # Clang does not use LLVM to link, so we can simply discard any - # '-mllvm $arg' options when doing the link step. - prev= - continue - ;; - objectlist) - if test -f "$arg"; then - save_arg=$arg - moreargs= - for fil in `cat "$save_arg"` - do -# func_append moreargs " $fil" - arg=$fil - # A libtool-controlled object. - - # Check to see that this really is a libtool object. - if func_lalib_unsafe_p "$arg"; then - pic_object= - non_pic_object= - - # Read the .lo file - func_source "$arg" - - if test -z "$pic_object" || - test -z "$non_pic_object" || - test none = "$pic_object" && - test none = "$non_pic_object"; then - func_fatal_error "cannot find name of object for '$arg'" - fi - - # Extract subdirectory from the argument. - func_dirname "$arg" "/" "" - xdir=$func_dirname_result - - if test none != "$pic_object"; then - # Prepend the subdirectory the object is found in. - pic_object=$xdir$pic_object - - if test dlfiles = "$prev"; then - if test yes = "$build_libtool_libs" && test yes = "$dlopen_support"; then - func_append dlfiles " $pic_object" - prev= - continue - else - # If libtool objects are unsupported, then we need to preload. - prev=dlprefiles - fi - fi - - # CHECK ME: I think I busted this. -Ossama - if test dlprefiles = "$prev"; then - # Preload the old-style object. - func_append dlprefiles " $pic_object" - prev= - fi - - # A PIC object. - func_append libobjs " $pic_object" - arg=$pic_object - fi - - # Non-PIC object. - if test none != "$non_pic_object"; then - # Prepend the subdirectory the object is found in. - non_pic_object=$xdir$non_pic_object - - # A standard non-PIC object - func_append non_pic_objects " $non_pic_object" - if test -z "$pic_object" || test none = "$pic_object"; then - arg=$non_pic_object - fi - else - # If the PIC object exists, use it instead. - # $xdir was prepended to $pic_object above. - non_pic_object=$pic_object - func_append non_pic_objects " $non_pic_object" - fi - else - # Only an error if not doing a dry-run. - if $opt_dry_run; then - # Extract subdirectory from the argument. - func_dirname "$arg" "/" "" - xdir=$func_dirname_result - - func_lo2o "$arg" - pic_object=$xdir$objdir/$func_lo2o_result - non_pic_object=$xdir$func_lo2o_result - func_append libobjs " $pic_object" - func_append non_pic_objects " $non_pic_object" - else - func_fatal_error "'$arg' is not a valid libtool object" - fi - fi - done - else - func_fatal_error "link input file '$arg' does not exist" - fi - arg=$save_arg - prev= - continue - ;; - os2dllname) - os2dllname=$arg - prev= - continue - ;; - precious_regex) - precious_files_regex=$arg - prev= - continue - ;; - release) - release=-$arg - prev= - continue - ;; - rpath | xrpath) - # We need an absolute path. - case $arg in - [\\/]* | [A-Za-z]:[\\/]*) ;; - *) - func_fatal_error "only absolute run-paths are allowed" - ;; - esac - if test rpath = "$prev"; then - case "$rpath " in - *" $arg "*) ;; - *) func_append rpath " $arg" ;; - esac - else - case "$xrpath " in - *" $arg "*) ;; - *) func_append xrpath " $arg" ;; - esac - fi - prev= - continue - ;; - shrext) - shrext_cmds=$arg - prev= - continue - ;; - weak) - func_append weak_libs " $arg" - prev= - continue - ;; - xcclinker) - func_append linker_flags " $qarg" - func_append compiler_flags " $qarg" - prev= - func_append compile_command " $qarg" - func_append finalize_command " $qarg" - continue - ;; - xcompiler) - func_append compiler_flags " $qarg" - prev= - func_append compile_command " $qarg" - func_append finalize_command " $qarg" - continue - ;; - xlinker) - func_append linker_flags " $qarg" - func_append compiler_flags " $wl$qarg" - prev= - func_append compile_command " $wl$qarg" - func_append finalize_command " $wl$qarg" - continue - ;; - *) - eval "$prev=\"\$arg\"" - prev= - continue - ;; - esac - fi # test -n "$prev" - - prevarg=$arg - - case $arg in - -all-static) - if test -n "$link_static_flag"; then - # See comment for -static flag below, for more details. - func_append compile_command " $link_static_flag" - func_append finalize_command " $link_static_flag" - fi - continue - ;; - - -allow-undefined) - # FIXME: remove this flag sometime in the future. - func_fatal_error "'-allow-undefined' must not be used because it is the default" - ;; - - -avoid-version) - avoid_version=yes - continue - ;; - - -bindir) - prev=bindir - continue - ;; - - -dlopen) - prev=dlfiles - continue - ;; - - -dlpreopen) - prev=dlprefiles - continue - ;; - - -export-dynamic) - export_dynamic=yes - continue - ;; - - -export-symbols | -export-symbols-regex) - if test -n "$export_symbols" || test -n "$export_symbols_regex"; then - func_fatal_error "more than one -exported-symbols argument is not allowed" - fi - if test X-export-symbols = "X$arg"; then - prev=expsyms - else - prev=expsyms_regex - fi - continue - ;; - - -framework) - prev=framework - continue - ;; - - -inst-prefix-dir) - prev=inst_prefix - continue - ;; - - # The native IRIX linker understands -LANG:*, -LIST:* and -LNO:* - # so, if we see these flags be careful not to treat them like -L - -L[A-Z][A-Z]*:*) - case $with_gcc/$host in - no/*-*-irix* | /*-*-irix*) - func_append compile_command " $arg" - func_append finalize_command " $arg" - ;; - esac - continue - ;; - - -L*) - func_stripname "-L" '' "$arg" - if test -z "$func_stripname_result"; then - if test "$#" -gt 0; then - func_fatal_error "require no space between '-L' and '$1'" - else - func_fatal_error "need path for '-L' option" - fi - fi - func_resolve_sysroot "$func_stripname_result" - dir=$func_resolve_sysroot_result - # We need an absolute path. - case $dir in - [\\/]* | [A-Za-z]:[\\/]*) ;; - *) - absdir=`cd "$dir" && pwd` - test -z "$absdir" && \ - func_fatal_error "cannot determine absolute directory name of '$dir'" - dir=$absdir - ;; - esac - case "$deplibs " in - *" -L$dir "* | *" $arg "*) - # Will only happen for absolute or sysroot arguments - ;; - *) - # Preserve sysroot, but never include relative directories - case $dir in - [\\/]* | [A-Za-z]:[\\/]* | =*) func_append deplibs " $arg" ;; - *) func_append deplibs " -L$dir" ;; - esac - func_append lib_search_path " $dir" - ;; - esac - case $host in - *-*-cygwin* | *-*-mingw* | *-*-pw32* | *-*-os2* | *-cegcc*) - testbindir=`$ECHO "$dir" | $SED 's*/lib$*/bin*'` - case :$dllsearchpath: in - *":$dir:"*) ;; - ::) dllsearchpath=$dir;; - *) func_append dllsearchpath ":$dir";; - esac - case :$dllsearchpath: in - *":$testbindir:"*) ;; - ::) dllsearchpath=$testbindir;; - *) func_append dllsearchpath ":$testbindir";; - esac - ;; - esac - continue - ;; - - -l*) - if test X-lc = "X$arg" || test X-lm = "X$arg"; then - case $host in - *-*-cygwin* | *-*-mingw* | *-*-pw32* | *-*-beos* | *-cegcc* | *-*-haiku*) - # These systems don't actually have a C or math library (as such) - continue - ;; - *-*-os2*) - # These systems don't actually have a C library (as such) - test X-lc = "X$arg" && continue - ;; - *-*-openbsd* | *-*-freebsd* | *-*-dragonfly* | *-*-bitrig*) - # Do not include libc due to us having libc/libc_r. - test X-lc = "X$arg" && continue - ;; - *-*-rhapsody* | *-*-darwin1.[012]) - # Rhapsody C and math libraries are in the System framework - func_append deplibs " System.ltframework" - continue - ;; - *-*-sco3.2v5* | *-*-sco5v6*) - # Causes problems with __ctype - test X-lc = "X$arg" && continue - ;; - *-*-sysv4.2uw2* | *-*-sysv5* | *-*-unixware* | *-*-OpenUNIX*) - # Compiler inserts libc in the correct place for threads to work - test X-lc = "X$arg" && continue - ;; - esac - elif test X-lc_r = "X$arg"; then - case $host in - *-*-openbsd* | *-*-freebsd* | *-*-dragonfly* | *-*-bitrig*) - # Do not include libc_r directly, use -pthread flag. - continue - ;; - esac - fi - func_append deplibs " $arg" - continue - ;; - - -mllvm) - prev=mllvm - continue - ;; - - -module) - module=yes - continue - ;; - - # Tru64 UNIX uses -model [arg] to determine the layout of C++ - # classes, name mangling, and exception handling. - # Darwin uses the -arch flag to determine output architecture. - -model|-arch|-isysroot|--sysroot) - func_append compiler_flags " $arg" - func_append compile_command " $arg" - func_append finalize_command " $arg" - prev=xcompiler - continue - ;; - - -mt|-mthreads|-kthread|-Kthread|-pthread|-pthreads|--thread-safe \ - |-threads|-fopenmp|-openmp|-mp|-xopenmp|-omp|-qsmp=*) - func_append compiler_flags " $arg" - func_append compile_command " $arg" - func_append finalize_command " $arg" - case "$new_inherited_linker_flags " in - *" $arg "*) ;; - * ) func_append new_inherited_linker_flags " $arg" ;; - esac - continue - ;; - - -multi_module) - single_module=$wl-multi_module - continue - ;; - - -no-fast-install) - fast_install=no - continue - ;; - - -no-install) - case $host in - *-*-cygwin* | *-*-mingw* | *-*-pw32* | *-*-os2* | *-*-darwin* | *-cegcc*) - # The PATH hackery in wrapper scripts is required on Windows - # and Darwin in order for the loader to find any dlls it needs. - func_warning "'-no-install' is ignored for $host" - func_warning "assuming '-no-fast-install' instead" - fast_install=no - ;; - *) no_install=yes ;; - esac - continue - ;; - - -no-undefined) - allow_undefined=no - continue - ;; - - -objectlist) - prev=objectlist - continue - ;; - - -os2dllname) - prev=os2dllname - continue - ;; - - -o) prev=output ;; - - -precious-files-regex) - prev=precious_regex - continue - ;; - - -release) - prev=release - continue - ;; - - -rpath) - prev=rpath - continue - ;; - - -R) - prev=xrpath - continue - ;; - - -R*) - func_stripname '-R' '' "$arg" - dir=$func_stripname_result - # We need an absolute path. - case $dir in - [\\/]* | [A-Za-z]:[\\/]*) ;; - =*) - func_stripname '=' '' "$dir" - dir=$lt_sysroot$func_stripname_result - ;; - *) - func_fatal_error "only absolute run-paths are allowed" - ;; - esac - case "$xrpath " in - *" $dir "*) ;; - *) func_append xrpath " $dir" ;; - esac - continue - ;; - - -shared) - # The effects of -shared are defined in a previous loop. - continue - ;; - - -shrext) - prev=shrext - continue - ;; - - -static | -static-libtool-libs) - # The effects of -static are defined in a previous loop. - # We used to do the same as -all-static on platforms that - # didn't have a PIC flag, but the assumption that the effects - # would be equivalent was wrong. It would break on at least - # Digital Unix and AIX. - continue - ;; - - -thread-safe) - thread_safe=yes - continue - ;; - - -version-info) - prev=vinfo - continue - ;; - - -version-number) - prev=vinfo - vinfo_number=yes - continue - ;; - - -weak) - prev=weak - continue - ;; - - -Wc,*) - func_stripname '-Wc,' '' "$arg" - args=$func_stripname_result - arg= - save_ifs=$IFS; IFS=, - for flag in $args; do - IFS=$save_ifs - func_quote_for_eval "$flag" - func_append arg " $func_quote_for_eval_result" - func_append compiler_flags " $func_quote_for_eval_result" - done - IFS=$save_ifs - func_stripname ' ' '' "$arg" - arg=$func_stripname_result - ;; - - -Wl,*) - func_stripname '-Wl,' '' "$arg" - args=$func_stripname_result - arg= - save_ifs=$IFS; IFS=, - for flag in $args; do - IFS=$save_ifs - func_quote_for_eval "$flag" - func_append arg " $wl$func_quote_for_eval_result" - func_append compiler_flags " $wl$func_quote_for_eval_result" - func_append linker_flags " $func_quote_for_eval_result" - done - IFS=$save_ifs - func_stripname ' ' '' "$arg" - arg=$func_stripname_result - ;; - - -Xcompiler) - prev=xcompiler - continue - ;; - - -Xlinker) - prev=xlinker - continue - ;; - - -XCClinker) - prev=xcclinker - continue - ;; - - # -msg_* for osf cc - -msg_*) - func_quote_for_eval "$arg" - arg=$func_quote_for_eval_result - ;; - - # Flags to be passed through unchanged, with rationale: - # -64, -mips[0-9] enable 64-bit mode for the SGI compiler - # -r[0-9][0-9]* specify processor for the SGI compiler - # -xarch=*, -xtarget=* enable 64-bit mode for the Sun compiler - # +DA*, +DD* enable 64-bit mode for the HP compiler - # -q* compiler args for the IBM compiler - # -m*, -t[45]*, -txscale* architecture-specific flags for GCC - # -F/path path to uninstalled frameworks, gcc on darwin - # -p, -pg, --coverage, -fprofile-* profiling flags for GCC - # -fstack-protector* stack protector flags for GCC - # @file GCC response files - # -tp=* Portland pgcc target processor selection - # --sysroot=* for sysroot support - # -O*, -g*, -flto*, -fwhopr*, -fuse-linker-plugin GCC link-time optimization - # -stdlib=* select c++ std lib with clang - -64|-mips[0-9]|-r[0-9][0-9]*|-xarch=*|-xtarget=*|+DA*|+DD*|-q*|-m*| \ - -t[45]*|-txscale*|-p|-pg|--coverage|-fprofile-*|-F*|@*|-tp=*|--sysroot=*| \ - -O*|-g*|-flto*|-fwhopr*|-fuse-linker-plugin|-fstack-protector*|-stdlib=*) - func_quote_for_eval "$arg" - arg=$func_quote_for_eval_result - func_append compile_command " $arg" - func_append finalize_command " $arg" - func_append compiler_flags " $arg" - continue - ;; - - -Z*) - if test os2 = "`expr $host : '.*\(os2\)'`"; then - # OS/2 uses -Zxxx to specify OS/2-specific options - compiler_flags="$compiler_flags $arg" - func_append compile_command " $arg" - func_append finalize_command " $arg" - case $arg in - -Zlinker | -Zstack) - prev=xcompiler - ;; - esac - continue - else - # Otherwise treat like 'Some other compiler flag' below - func_quote_for_eval "$arg" - arg=$func_quote_for_eval_result - fi - ;; - - # Some other compiler flag. - -* | +*) - func_quote_for_eval "$arg" - arg=$func_quote_for_eval_result - ;; - - *.$objext) - # A standard object. - func_append objs " $arg" - ;; - - *.lo) - # A libtool-controlled object. - - # Check to see that this really is a libtool object. - if func_lalib_unsafe_p "$arg"; then - pic_object= - non_pic_object= - - # Read the .lo file - func_source "$arg" - - if test -z "$pic_object" || - test -z "$non_pic_object" || - test none = "$pic_object" && - test none = "$non_pic_object"; then - func_fatal_error "cannot find name of object for '$arg'" - fi - - # Extract subdirectory from the argument. - func_dirname "$arg" "/" "" - xdir=$func_dirname_result - - test none = "$pic_object" || { - # Prepend the subdirectory the object is found in. - pic_object=$xdir$pic_object - - if test dlfiles = "$prev"; then - if test yes = "$build_libtool_libs" && test yes = "$dlopen_support"; then - func_append dlfiles " $pic_object" - prev= - continue - else - # If libtool objects are unsupported, then we need to preload. - prev=dlprefiles - fi - fi - - # CHECK ME: I think I busted this. -Ossama - if test dlprefiles = "$prev"; then - # Preload the old-style object. - func_append dlprefiles " $pic_object" - prev= - fi - - # A PIC object. - func_append libobjs " $pic_object" - arg=$pic_object - } - - # Non-PIC object. - if test none != "$non_pic_object"; then - # Prepend the subdirectory the object is found in. - non_pic_object=$xdir$non_pic_object - - # A standard non-PIC object - func_append non_pic_objects " $non_pic_object" - if test -z "$pic_object" || test none = "$pic_object"; then - arg=$non_pic_object - fi - else - # If the PIC object exists, use it instead. - # $xdir was prepended to $pic_object above. - non_pic_object=$pic_object - func_append non_pic_objects " $non_pic_object" - fi - else - # Only an error if not doing a dry-run. - if $opt_dry_run; then - # Extract subdirectory from the argument. - func_dirname "$arg" "/" "" - xdir=$func_dirname_result - - func_lo2o "$arg" - pic_object=$xdir$objdir/$func_lo2o_result - non_pic_object=$xdir$func_lo2o_result - func_append libobjs " $pic_object" - func_append non_pic_objects " $non_pic_object" - else - func_fatal_error "'$arg' is not a valid libtool object" - fi - fi - ;; - - *.$libext) - # An archive. - func_append deplibs " $arg" - func_append old_deplibs " $arg" - continue - ;; - - *.la) - # A libtool-controlled library. - - func_resolve_sysroot "$arg" - if test dlfiles = "$prev"; then - # This library was specified with -dlopen. - func_append dlfiles " $func_resolve_sysroot_result" - prev= - elif test dlprefiles = "$prev"; then - # The library was specified with -dlpreopen. - func_append dlprefiles " $func_resolve_sysroot_result" - prev= - else - func_append deplibs " $func_resolve_sysroot_result" - fi - continue - ;; - - # Some other compiler argument. - *) - # Unknown arguments in both finalize_command and compile_command need - # to be aesthetically quoted because they are evaled later. - func_quote_for_eval "$arg" - arg=$func_quote_for_eval_result - ;; - esac # arg - - # Now actually substitute the argument into the commands. - if test -n "$arg"; then - func_append compile_command " $arg" - func_append finalize_command " $arg" - fi - done # argument parsing loop - - test -n "$prev" && \ - func_fatal_help "the '$prevarg' option requires an argument" - - if test yes = "$export_dynamic" && test -n "$export_dynamic_flag_spec"; then - eval arg=\"$export_dynamic_flag_spec\" - func_append compile_command " $arg" - func_append finalize_command " $arg" - fi - - oldlibs= - # calculate the name of the file, without its directory - func_basename "$output" - outputname=$func_basename_result - libobjs_save=$libobjs - - if test -n "$shlibpath_var"; then - # get the directories listed in $shlibpath_var - eval shlib_search_path=\`\$ECHO \"\$$shlibpath_var\" \| \$SED \'s/:/ /g\'\` - else - shlib_search_path= - fi - eval sys_lib_search_path=\"$sys_lib_search_path_spec\" - eval sys_lib_dlsearch_path=\"$sys_lib_dlsearch_path_spec\" - - # Definition is injected by LT_CONFIG during libtool generation. - func_munge_path_list sys_lib_dlsearch_path "$LT_SYS_LIBRARY_PATH" - - func_dirname "$output" "/" "" - output_objdir=$func_dirname_result$objdir - func_to_tool_file "$output_objdir/" - tool_output_objdir=$func_to_tool_file_result - # Create the object directory. - func_mkdir_p "$output_objdir" - - # Determine the type of output - case $output in - "") - func_fatal_help "you must specify an output file" - ;; - *.$libext) linkmode=oldlib ;; - *.lo | *.$objext) linkmode=obj ;; - *.la) linkmode=lib ;; - *) linkmode=prog ;; # Anything else should be a program. - esac - - specialdeplibs= - - libs= - # Find all interdependent deplibs by searching for libraries - # that are linked more than once (e.g. -la -lb -la) - for deplib in $deplibs; do - if $opt_preserve_dup_deps; then - case "$libs " in - *" $deplib "*) func_append specialdeplibs " $deplib" ;; - esac - fi - func_append libs " $deplib" - done - - if test lib = "$linkmode"; then - libs="$predeps $libs $compiler_lib_search_path $postdeps" - - # Compute libraries that are listed more than once in $predeps - # $postdeps and mark them as special (i.e., whose duplicates are - # not to be eliminated). - pre_post_deps= - if $opt_duplicate_compiler_generated_deps; then - for pre_post_dep in $predeps $postdeps; do - case "$pre_post_deps " in - *" $pre_post_dep "*) func_append specialdeplibs " $pre_post_deps" ;; - esac - func_append pre_post_deps " $pre_post_dep" - done - fi - pre_post_deps= - fi - - deplibs= - newdependency_libs= - newlib_search_path= - need_relink=no # whether we're linking any uninstalled libtool libraries - notinst_deplibs= # not-installed libtool libraries - notinst_path= # paths that contain not-installed libtool libraries - - case $linkmode in - lib) - passes="conv dlpreopen link" - for file in $dlfiles $dlprefiles; do - case $file in - *.la) ;; - *) - func_fatal_help "libraries can '-dlopen' only libtool libraries: $file" - ;; - esac - done - ;; - prog) - compile_deplibs= - finalize_deplibs= - alldeplibs=false - newdlfiles= - newdlprefiles= - passes="conv scan dlopen dlpreopen link" - ;; - *) passes="conv" - ;; - esac - - for pass in $passes; do - # The preopen pass in lib mode reverses $deplibs; put it back here - # so that -L comes before libs that need it for instance... - if test lib,link = "$linkmode,$pass"; then - ## FIXME: Find the place where the list is rebuilt in the wrong - ## order, and fix it there properly - tmp_deplibs= - for deplib in $deplibs; do - tmp_deplibs="$deplib $tmp_deplibs" - done - deplibs=$tmp_deplibs - fi - - if test lib,link = "$linkmode,$pass" || - test prog,scan = "$linkmode,$pass"; then - libs=$deplibs - deplibs= - fi - if test prog = "$linkmode"; then - case $pass in - dlopen) libs=$dlfiles ;; - dlpreopen) libs=$dlprefiles ;; - link) libs="$deplibs %DEPLIBS% $dependency_libs" ;; - esac - fi - if test lib,dlpreopen = "$linkmode,$pass"; then - # Collect and forward deplibs of preopened libtool libs - for lib in $dlprefiles; do - # Ignore non-libtool-libs - dependency_libs= - func_resolve_sysroot "$lib" - case $lib in - *.la) func_source "$func_resolve_sysroot_result" ;; - esac - - # Collect preopened libtool deplibs, except any this library - # has declared as weak libs - for deplib in $dependency_libs; do - func_basename "$deplib" - deplib_base=$func_basename_result - case " $weak_libs " in - *" $deplib_base "*) ;; - *) func_append deplibs " $deplib" ;; - esac - done - done - libs=$dlprefiles - fi - if test dlopen = "$pass"; then - # Collect dlpreopened libraries - save_deplibs=$deplibs - deplibs= - fi - - for deplib in $libs; do - lib= - found=false - case $deplib in - -mt|-mthreads|-kthread|-Kthread|-pthread|-pthreads|--thread-safe \ - |-threads|-fopenmp|-openmp|-mp|-xopenmp|-omp|-qsmp=*) - if test prog,link = "$linkmode,$pass"; then - compile_deplibs="$deplib $compile_deplibs" - finalize_deplibs="$deplib $finalize_deplibs" - else - func_append compiler_flags " $deplib" - if test lib = "$linkmode"; then - case "$new_inherited_linker_flags " in - *" $deplib "*) ;; - * ) func_append new_inherited_linker_flags " $deplib" ;; - esac - fi - fi - continue - ;; - -l*) - if test lib != "$linkmode" && test prog != "$linkmode"; then - func_warning "'-l' is ignored for archives/objects" - continue - fi - func_stripname '-l' '' "$deplib" - name=$func_stripname_result - if test lib = "$linkmode"; then - searchdirs="$newlib_search_path $lib_search_path $compiler_lib_search_dirs $sys_lib_search_path $shlib_search_path" - else - searchdirs="$newlib_search_path $lib_search_path $sys_lib_search_path $shlib_search_path" - fi - for searchdir in $searchdirs; do - for search_ext in .la $std_shrext .so .a; do - # Search the libtool library - lib=$searchdir/lib$name$search_ext - if test -f "$lib"; then - if test .la = "$search_ext"; then - found=: - else - found=false - fi - break 2 - fi - done - done - if $found; then - # deplib is a libtool library - # If $allow_libtool_libs_with_static_runtimes && $deplib is a stdlib, - # We need to do some special things here, and not later. - if test yes = "$allow_libtool_libs_with_static_runtimes"; then - case " $predeps $postdeps " in - *" $deplib "*) - if func_lalib_p "$lib"; then - library_names= - old_library= - func_source "$lib" - for l in $old_library $library_names; do - ll=$l - done - if test "X$ll" = "X$old_library"; then # only static version available - found=false - func_dirname "$lib" "" "." - ladir=$func_dirname_result - lib=$ladir/$old_library - if test prog,link = "$linkmode,$pass"; then - compile_deplibs="$deplib $compile_deplibs" - finalize_deplibs="$deplib $finalize_deplibs" - else - deplibs="$deplib $deplibs" - test lib = "$linkmode" && newdependency_libs="$deplib $newdependency_libs" - fi - continue - fi - fi - ;; - *) ;; - esac - fi - else - # deplib doesn't seem to be a libtool library - if test prog,link = "$linkmode,$pass"; then - compile_deplibs="$deplib $compile_deplibs" - finalize_deplibs="$deplib $finalize_deplibs" - else - deplibs="$deplib $deplibs" - test lib = "$linkmode" && newdependency_libs="$deplib $newdependency_libs" - fi - continue - fi - ;; # -l - *.ltframework) - if test prog,link = "$linkmode,$pass"; then - compile_deplibs="$deplib $compile_deplibs" - finalize_deplibs="$deplib $finalize_deplibs" - else - deplibs="$deplib $deplibs" - if test lib = "$linkmode"; then - case "$new_inherited_linker_flags " in - *" $deplib "*) ;; - * ) func_append new_inherited_linker_flags " $deplib" ;; - esac - fi - fi - continue - ;; - -L*) - case $linkmode in - lib) - deplibs="$deplib $deplibs" - test conv = "$pass" && continue - newdependency_libs="$deplib $newdependency_libs" - func_stripname '-L' '' "$deplib" - func_resolve_sysroot "$func_stripname_result" - func_append newlib_search_path " $func_resolve_sysroot_result" - ;; - prog) - if test conv = "$pass"; then - deplibs="$deplib $deplibs" - continue - fi - if test scan = "$pass"; then - deplibs="$deplib $deplibs" - else - compile_deplibs="$deplib $compile_deplibs" - finalize_deplibs="$deplib $finalize_deplibs" - fi - func_stripname '-L' '' "$deplib" - func_resolve_sysroot "$func_stripname_result" - func_append newlib_search_path " $func_resolve_sysroot_result" - ;; - *) - func_warning "'-L' is ignored for archives/objects" - ;; - esac # linkmode - continue - ;; # -L - -R*) - if test link = "$pass"; then - func_stripname '-R' '' "$deplib" - func_resolve_sysroot "$func_stripname_result" - dir=$func_resolve_sysroot_result - # Make sure the xrpath contains only unique directories. - case "$xrpath " in - *" $dir "*) ;; - *) func_append xrpath " $dir" ;; - esac - fi - deplibs="$deplib $deplibs" - continue - ;; - *.la) - func_resolve_sysroot "$deplib" - lib=$func_resolve_sysroot_result - ;; - *.$libext) - if test conv = "$pass"; then - deplibs="$deplib $deplibs" - continue - fi - case $linkmode in - lib) - # Linking convenience modules into shared libraries is allowed, - # but linking other static libraries is non-portable. - case " $dlpreconveniencelibs " in - *" $deplib "*) ;; - *) - valid_a_lib=false - case $deplibs_check_method in - match_pattern*) - set dummy $deplibs_check_method; shift - match_pattern_regex=`expr "$deplibs_check_method" : "$1 \(.*\)"` - if eval "\$ECHO \"$deplib\"" 2>/dev/null | $SED 10q \ - | $EGREP "$match_pattern_regex" > /dev/null; then - valid_a_lib=: - fi - ;; - pass_all) - valid_a_lib=: - ;; - esac - if $valid_a_lib; then - echo - $ECHO "*** Warning: Linking the shared library $output against the" - $ECHO "*** static library $deplib is not portable!" - deplibs="$deplib $deplibs" - else - echo - $ECHO "*** Warning: Trying to link with static lib archive $deplib." - echo "*** I have the capability to make that library automatically link in when" - echo "*** you link to this library. But I can only do this if you have a" - echo "*** shared version of the library, which you do not appear to have" - echo "*** because the file extensions .$libext of this argument makes me believe" - echo "*** that it is just a static archive that I should not use here." - fi - ;; - esac - continue - ;; - prog) - if test link != "$pass"; then - deplibs="$deplib $deplibs" - else - compile_deplibs="$deplib $compile_deplibs" - finalize_deplibs="$deplib $finalize_deplibs" - fi - continue - ;; - esac # linkmode - ;; # *.$libext - *.lo | *.$objext) - if test conv = "$pass"; then - deplibs="$deplib $deplibs" - elif test prog = "$linkmode"; then - if test dlpreopen = "$pass" || test yes != "$dlopen_support" || test no = "$build_libtool_libs"; then - # If there is no dlopen support or we're linking statically, - # we need to preload. - func_append newdlprefiles " $deplib" - compile_deplibs="$deplib $compile_deplibs" - finalize_deplibs="$deplib $finalize_deplibs" - else - func_append newdlfiles " $deplib" - fi - fi - continue - ;; - %DEPLIBS%) - alldeplibs=: - continue - ;; - esac # case $deplib - - $found || test -f "$lib" \ - || func_fatal_error "cannot find the library '$lib' or unhandled argument '$deplib'" - - # Check to see that this really is a libtool archive. - func_lalib_unsafe_p "$lib" \ - || func_fatal_error "'$lib' is not a valid libtool archive" - - func_dirname "$lib" "" "." - ladir=$func_dirname_result - - dlname= - dlopen= - dlpreopen= - libdir= - library_names= - old_library= - inherited_linker_flags= - # If the library was installed with an old release of libtool, - # it will not redefine variables installed, or shouldnotlink - installed=yes - shouldnotlink=no - avoidtemprpath= - - - # Read the .la file - func_source "$lib" - - # Convert "-framework foo" to "foo.ltframework" - if test -n "$inherited_linker_flags"; then - tmp_inherited_linker_flags=`$ECHO "$inherited_linker_flags" | $SED 's/-framework \([^ $]*\)/\1.ltframework/g'` - for tmp_inherited_linker_flag in $tmp_inherited_linker_flags; do - case " $new_inherited_linker_flags " in - *" $tmp_inherited_linker_flag "*) ;; - *) func_append new_inherited_linker_flags " $tmp_inherited_linker_flag";; - esac - done - fi - dependency_libs=`$ECHO " $dependency_libs" | $SED 's% \([^ $]*\).ltframework% -framework \1%g'` - if test lib,link = "$linkmode,$pass" || - test prog,scan = "$linkmode,$pass" || - { test prog != "$linkmode" && test lib != "$linkmode"; }; then - test -n "$dlopen" && func_append dlfiles " $dlopen" - test -n "$dlpreopen" && func_append dlprefiles " $dlpreopen" - fi - - if test conv = "$pass"; then - # Only check for convenience libraries - deplibs="$lib $deplibs" - if test -z "$libdir"; then - if test -z "$old_library"; then - func_fatal_error "cannot find name of link library for '$lib'" - fi - # It is a libtool convenience library, so add in its objects. - func_append convenience " $ladir/$objdir/$old_library" - func_append old_convenience " $ladir/$objdir/$old_library" - elif test prog != "$linkmode" && test lib != "$linkmode"; then - func_fatal_error "'$lib' is not a convenience library" - fi - tmp_libs= - for deplib in $dependency_libs; do - deplibs="$deplib $deplibs" - if $opt_preserve_dup_deps; then - case "$tmp_libs " in - *" $deplib "*) func_append specialdeplibs " $deplib" ;; - esac - fi - func_append tmp_libs " $deplib" - done - continue - fi # $pass = conv - - - # Get the name of the library we link against. - linklib= - if test -n "$old_library" && - { test yes = "$prefer_static_libs" || - test built,no = "$prefer_static_libs,$installed"; }; then - linklib=$old_library - else - for l in $old_library $library_names; do - linklib=$l - done - fi - if test -z "$linklib"; then - func_fatal_error "cannot find name of link library for '$lib'" - fi - - # This library was specified with -dlopen. - if test dlopen = "$pass"; then - test -z "$libdir" \ - && func_fatal_error "cannot -dlopen a convenience library: '$lib'" - if test -z "$dlname" || - test yes != "$dlopen_support" || - test no = "$build_libtool_libs" - then - # If there is no dlname, no dlopen support or we're linking - # statically, we need to preload. We also need to preload any - # dependent libraries so libltdl's deplib preloader doesn't - # bomb out in the load deplibs phase. - func_append dlprefiles " $lib $dependency_libs" - else - func_append newdlfiles " $lib" - fi - continue - fi # $pass = dlopen - - # We need an absolute path. - case $ladir in - [\\/]* | [A-Za-z]:[\\/]*) abs_ladir=$ladir ;; - *) - abs_ladir=`cd "$ladir" && pwd` - if test -z "$abs_ladir"; then - func_warning "cannot determine absolute directory name of '$ladir'" - func_warning "passing it literally to the linker, although it might fail" - abs_ladir=$ladir - fi - ;; - esac - func_basename "$lib" - laname=$func_basename_result - - # Find the relevant object directory and library name. - if test yes = "$installed"; then - if test ! -f "$lt_sysroot$libdir/$linklib" && test -f "$abs_ladir/$linklib"; then - func_warning "library '$lib' was moved." - dir=$ladir - absdir=$abs_ladir - libdir=$abs_ladir - else - dir=$lt_sysroot$libdir - absdir=$lt_sysroot$libdir - fi - test yes = "$hardcode_automatic" && avoidtemprpath=yes - else - if test ! -f "$ladir/$objdir/$linklib" && test -f "$abs_ladir/$linklib"; then - dir=$ladir - absdir=$abs_ladir - # Remove this search path later - func_append notinst_path " $abs_ladir" - else - dir=$ladir/$objdir - absdir=$abs_ladir/$objdir - # Remove this search path later - func_append notinst_path " $abs_ladir" - fi - fi # $installed = yes - func_stripname 'lib' '.la' "$laname" - name=$func_stripname_result - - # This library was specified with -dlpreopen. - if test dlpreopen = "$pass"; then - if test -z "$libdir" && test prog = "$linkmode"; then - func_fatal_error "only libraries may -dlpreopen a convenience library: '$lib'" - fi - case $host in - # special handling for platforms with PE-DLLs. - *cygwin* | *mingw* | *cegcc* ) - # Linker will automatically link against shared library if both - # static and shared are present. Therefore, ensure we extract - # symbols from the import library if a shared library is present - # (otherwise, the dlopen module name will be incorrect). We do - # this by putting the import library name into $newdlprefiles. - # We recover the dlopen module name by 'saving' the la file - # name in a special purpose variable, and (later) extracting the - # dlname from the la file. - if test -n "$dlname"; then - func_tr_sh "$dir/$linklib" - eval "libfile_$func_tr_sh_result=\$abs_ladir/\$laname" - func_append newdlprefiles " $dir/$linklib" - else - func_append newdlprefiles " $dir/$old_library" - # Keep a list of preopened convenience libraries to check - # that they are being used correctly in the link pass. - test -z "$libdir" && \ - func_append dlpreconveniencelibs " $dir/$old_library" - fi - ;; - * ) - # Prefer using a static library (so that no silly _DYNAMIC symbols - # are required to link). - if test -n "$old_library"; then - func_append newdlprefiles " $dir/$old_library" - # Keep a list of preopened convenience libraries to check - # that they are being used correctly in the link pass. - test -z "$libdir" && \ - func_append dlpreconveniencelibs " $dir/$old_library" - # Otherwise, use the dlname, so that lt_dlopen finds it. - elif test -n "$dlname"; then - func_append newdlprefiles " $dir/$dlname" - else - func_append newdlprefiles " $dir/$linklib" - fi - ;; - esac - fi # $pass = dlpreopen - - if test -z "$libdir"; then - # Link the convenience library - if test lib = "$linkmode"; then - deplibs="$dir/$old_library $deplibs" - elif test prog,link = "$linkmode,$pass"; then - compile_deplibs="$dir/$old_library $compile_deplibs" - finalize_deplibs="$dir/$old_library $finalize_deplibs" - else - deplibs="$lib $deplibs" # used for prog,scan pass - fi - continue - fi - - - if test prog = "$linkmode" && test link != "$pass"; then - func_append newlib_search_path " $ladir" - deplibs="$lib $deplibs" - - linkalldeplibs=false - if test no != "$link_all_deplibs" || test -z "$library_names" || - test no = "$build_libtool_libs"; then - linkalldeplibs=: - fi - - tmp_libs= - for deplib in $dependency_libs; do - case $deplib in - -L*) func_stripname '-L' '' "$deplib" - func_resolve_sysroot "$func_stripname_result" - func_append newlib_search_path " $func_resolve_sysroot_result" - ;; - esac - # Need to link against all dependency_libs? - if $linkalldeplibs; then - deplibs="$deplib $deplibs" - else - # Need to hardcode shared library paths - # or/and link against static libraries - newdependency_libs="$deplib $newdependency_libs" - fi - if $opt_preserve_dup_deps; then - case "$tmp_libs " in - *" $deplib "*) func_append specialdeplibs " $deplib" ;; - esac - fi - func_append tmp_libs " $deplib" - done # for deplib - continue - fi # $linkmode = prog... - - if test prog,link = "$linkmode,$pass"; then - if test -n "$library_names" && - { { test no = "$prefer_static_libs" || - test built,yes = "$prefer_static_libs,$installed"; } || - test -z "$old_library"; }; then - # We need to hardcode the library path - if test -n "$shlibpath_var" && test -z "$avoidtemprpath"; then - # Make sure the rpath contains only unique directories. - case $temp_rpath: in - *"$absdir:"*) ;; - *) func_append temp_rpath "$absdir:" ;; - esac - fi - - # Hardcode the library path. - # Skip directories that are in the system default run-time - # search path. - case " $sys_lib_dlsearch_path " in - *" $absdir "*) ;; - *) - case "$compile_rpath " in - *" $absdir "*) ;; - *) func_append compile_rpath " $absdir" ;; - esac - ;; - esac - case " $sys_lib_dlsearch_path " in - *" $libdir "*) ;; - *) - case "$finalize_rpath " in - *" $libdir "*) ;; - *) func_append finalize_rpath " $libdir" ;; - esac - ;; - esac - fi # $linkmode,$pass = prog,link... - - if $alldeplibs && - { test pass_all = "$deplibs_check_method" || - { test yes = "$build_libtool_libs" && - test -n "$library_names"; }; }; then - # We only need to search for static libraries - continue - fi - fi - - link_static=no # Whether the deplib will be linked statically - use_static_libs=$prefer_static_libs - if test built = "$use_static_libs" && test yes = "$installed"; then - use_static_libs=no - fi - if test -n "$library_names" && - { test no = "$use_static_libs" || test -z "$old_library"; }; then - case $host in - *cygwin* | *mingw* | *cegcc* | *os2*) - # No point in relinking DLLs because paths are not encoded - func_append notinst_deplibs " $lib" - need_relink=no - ;; - *) - if test no = "$installed"; then - func_append notinst_deplibs " $lib" - need_relink=yes - fi - ;; - esac - # This is a shared library - - # Warn about portability, can't link against -module's on some - # systems (darwin). Don't bleat about dlopened modules though! - dlopenmodule= - for dlpremoduletest in $dlprefiles; do - if test "X$dlpremoduletest" = "X$lib"; then - dlopenmodule=$dlpremoduletest - break - fi - done - if test -z "$dlopenmodule" && test yes = "$shouldnotlink" && test link = "$pass"; then - echo - if test prog = "$linkmode"; then - $ECHO "*** Warning: Linking the executable $output against the loadable module" - else - $ECHO "*** Warning: Linking the shared library $output against the loadable module" - fi - $ECHO "*** $linklib is not portable!" - fi - if test lib = "$linkmode" && - test yes = "$hardcode_into_libs"; then - # Hardcode the library path. - # Skip directories that are in the system default run-time - # search path. - case " $sys_lib_dlsearch_path " in - *" $absdir "*) ;; - *) - case "$compile_rpath " in - *" $absdir "*) ;; - *) func_append compile_rpath " $absdir" ;; - esac - ;; - esac - case " $sys_lib_dlsearch_path " in - *" $libdir "*) ;; - *) - case "$finalize_rpath " in - *" $libdir "*) ;; - *) func_append finalize_rpath " $libdir" ;; - esac - ;; - esac - fi - - if test -n "$old_archive_from_expsyms_cmds"; then - # figure out the soname - set dummy $library_names - shift - realname=$1 - shift - libname=`eval "\\$ECHO \"$libname_spec\""` - # use dlname if we got it. it's perfectly good, no? - if test -n "$dlname"; then - soname=$dlname - elif test -n "$soname_spec"; then - # bleh windows - case $host in - *cygwin* | mingw* | *cegcc* | *os2*) - func_arith $current - $age - major=$func_arith_result - versuffix=-$major - ;; - esac - eval soname=\"$soname_spec\" - else - soname=$realname - fi - - # Make a new name for the extract_expsyms_cmds to use - soroot=$soname - func_basename "$soroot" - soname=$func_basename_result - func_stripname 'lib' '.dll' "$soname" - newlib=libimp-$func_stripname_result.a - - # If the library has no export list, then create one now - if test -f "$output_objdir/$soname-def"; then : - else - func_verbose "extracting exported symbol list from '$soname'" - func_execute_cmds "$extract_expsyms_cmds" 'exit $?' - fi - - # Create $newlib - if test -f "$output_objdir/$newlib"; then :; else - func_verbose "generating import library for '$soname'" - func_execute_cmds "$old_archive_from_expsyms_cmds" 'exit $?' - fi - # make sure the library variables are pointing to the new library - dir=$output_objdir - linklib=$newlib - fi # test -n "$old_archive_from_expsyms_cmds" - - if test prog = "$linkmode" || test relink != "$opt_mode"; then - add_shlibpath= - add_dir= - add= - lib_linked=yes - case $hardcode_action in - immediate | unsupported) - if test no = "$hardcode_direct"; then - add=$dir/$linklib - case $host in - *-*-sco3.2v5.0.[024]*) add_dir=-L$dir ;; - *-*-sysv4*uw2*) add_dir=-L$dir ;; - *-*-sysv5OpenUNIX* | *-*-sysv5UnixWare7.[01].[10]* | \ - *-*-unixware7*) add_dir=-L$dir ;; - *-*-darwin* ) - # if the lib is a (non-dlopened) module then we cannot - # link against it, someone is ignoring the earlier warnings - if /usr/bin/file -L $add 2> /dev/null | - $GREP ": [^:]* bundle" >/dev/null; then - if test "X$dlopenmodule" != "X$lib"; then - $ECHO "*** Warning: lib $linklib is a module, not a shared library" - if test -z "$old_library"; then - echo - echo "*** And there doesn't seem to be a static archive available" - echo "*** The link will probably fail, sorry" - else - add=$dir/$old_library - fi - elif test -n "$old_library"; then - add=$dir/$old_library - fi - fi - esac - elif test no = "$hardcode_minus_L"; then - case $host in - *-*-sunos*) add_shlibpath=$dir ;; - esac - add_dir=-L$dir - add=-l$name - elif test no = "$hardcode_shlibpath_var"; then - add_shlibpath=$dir - add=-l$name - else - lib_linked=no - fi - ;; - relink) - if test yes = "$hardcode_direct" && - test no = "$hardcode_direct_absolute"; then - add=$dir/$linklib - elif test yes = "$hardcode_minus_L"; then - add_dir=-L$absdir - # Try looking first in the location we're being installed to. - if test -n "$inst_prefix_dir"; then - case $libdir in - [\\/]*) - func_append add_dir " -L$inst_prefix_dir$libdir" - ;; - esac - fi - add=-l$name - elif test yes = "$hardcode_shlibpath_var"; then - add_shlibpath=$dir - add=-l$name - else - lib_linked=no - fi - ;; - *) lib_linked=no ;; - esac - - if test yes != "$lib_linked"; then - func_fatal_configuration "unsupported hardcode properties" - fi - - if test -n "$add_shlibpath"; then - case :$compile_shlibpath: in - *":$add_shlibpath:"*) ;; - *) func_append compile_shlibpath "$add_shlibpath:" ;; - esac - fi - if test prog = "$linkmode"; then - test -n "$add_dir" && compile_deplibs="$add_dir $compile_deplibs" - test -n "$add" && compile_deplibs="$add $compile_deplibs" - else - test -n "$add_dir" && deplibs="$add_dir $deplibs" - test -n "$add" && deplibs="$add $deplibs" - if test yes != "$hardcode_direct" && - test yes != "$hardcode_minus_L" && - test yes = "$hardcode_shlibpath_var"; then - case :$finalize_shlibpath: in - *":$libdir:"*) ;; - *) func_append finalize_shlibpath "$libdir:" ;; - esac - fi - fi - fi - - if test prog = "$linkmode" || test relink = "$opt_mode"; then - add_shlibpath= - add_dir= - add= - # Finalize command for both is simple: just hardcode it. - if test yes = "$hardcode_direct" && - test no = "$hardcode_direct_absolute"; then - add=$libdir/$linklib - elif test yes = "$hardcode_minus_L"; then - add_dir=-L$libdir - add=-l$name - elif test yes = "$hardcode_shlibpath_var"; then - case :$finalize_shlibpath: in - *":$libdir:"*) ;; - *) func_append finalize_shlibpath "$libdir:" ;; - esac - add=-l$name - elif test yes = "$hardcode_automatic"; then - if test -n "$inst_prefix_dir" && - test -f "$inst_prefix_dir$libdir/$linklib"; then - add=$inst_prefix_dir$libdir/$linklib - else - add=$libdir/$linklib - fi - else - # We cannot seem to hardcode it, guess we'll fake it. - add_dir=-L$libdir - # Try looking first in the location we're being installed to. - if test -n "$inst_prefix_dir"; then - case $libdir in - [\\/]*) - func_append add_dir " -L$inst_prefix_dir$libdir" - ;; - esac - fi - add=-l$name - fi - - if test prog = "$linkmode"; then - test -n "$add_dir" && finalize_deplibs="$add_dir $finalize_deplibs" - test -n "$add" && finalize_deplibs="$add $finalize_deplibs" - else - test -n "$add_dir" && deplibs="$add_dir $deplibs" - test -n "$add" && deplibs="$add $deplibs" - fi - fi - elif test prog = "$linkmode"; then - # Here we assume that one of hardcode_direct or hardcode_minus_L - # is not unsupported. This is valid on all known static and - # shared platforms. - if test unsupported != "$hardcode_direct"; then - test -n "$old_library" && linklib=$old_library - compile_deplibs="$dir/$linklib $compile_deplibs" - finalize_deplibs="$dir/$linklib $finalize_deplibs" - else - compile_deplibs="-l$name -L$dir $compile_deplibs" - finalize_deplibs="-l$name -L$dir $finalize_deplibs" - fi - elif test yes = "$build_libtool_libs"; then - # Not a shared library - if test pass_all != "$deplibs_check_method"; then - # We're trying link a shared library against a static one - # but the system doesn't support it. - - # Just print a warning and add the library to dependency_libs so - # that the program can be linked against the static library. - echo - $ECHO "*** Warning: This system cannot link to static lib archive $lib." - echo "*** I have the capability to make that library automatically link in when" - echo "*** you link to this library. But I can only do this if you have a" - echo "*** shared version of the library, which you do not appear to have." - if test yes = "$module"; then - echo "*** But as you try to build a module library, libtool will still create " - echo "*** a static module, that should work as long as the dlopening application" - echo "*** is linked with the -dlopen flag to resolve symbols at runtime." - if test -z "$global_symbol_pipe"; then - echo - echo "*** However, this would only work if libtool was able to extract symbol" - echo "*** lists from a program, using 'nm' or equivalent, but libtool could" - echo "*** not find such a program. So, this module is probably useless." - echo "*** 'nm' from GNU binutils and a full rebuild may help." - fi - if test no = "$build_old_libs"; then - build_libtool_libs=module - build_old_libs=yes - else - build_libtool_libs=no - fi - fi - else - deplibs="$dir/$old_library $deplibs" - link_static=yes - fi - fi # link shared/static library? - - if test lib = "$linkmode"; then - if test -n "$dependency_libs" && - { test yes != "$hardcode_into_libs" || - test yes = "$build_old_libs" || - test yes = "$link_static"; }; then - # Extract -R from dependency_libs - temp_deplibs= - for libdir in $dependency_libs; do - case $libdir in - -R*) func_stripname '-R' '' "$libdir" - temp_xrpath=$func_stripname_result - case " $xrpath " in - *" $temp_xrpath "*) ;; - *) func_append xrpath " $temp_xrpath";; - esac;; - *) func_append temp_deplibs " $libdir";; - esac - done - dependency_libs=$temp_deplibs - fi - - func_append newlib_search_path " $absdir" - # Link against this library - test no = "$link_static" && newdependency_libs="$abs_ladir/$laname $newdependency_libs" - # ... and its dependency_libs - tmp_libs= - for deplib in $dependency_libs; do - newdependency_libs="$deplib $newdependency_libs" - case $deplib in - -L*) func_stripname '-L' '' "$deplib" - func_resolve_sysroot "$func_stripname_result";; - *) func_resolve_sysroot "$deplib" ;; - esac - if $opt_preserve_dup_deps; then - case "$tmp_libs " in - *" $func_resolve_sysroot_result "*) - func_append specialdeplibs " $func_resolve_sysroot_result" ;; - esac - fi - func_append tmp_libs " $func_resolve_sysroot_result" - done - - if test no != "$link_all_deplibs"; then - # Add the search paths of all dependency libraries - for deplib in $dependency_libs; do - path= - case $deplib in - -L*) path=$deplib ;; - *.la) - func_resolve_sysroot "$deplib" - deplib=$func_resolve_sysroot_result - func_dirname "$deplib" "" "." - dir=$func_dirname_result - # We need an absolute path. - case $dir in - [\\/]* | [A-Za-z]:[\\/]*) absdir=$dir ;; - *) - absdir=`cd "$dir" && pwd` - if test -z "$absdir"; then - func_warning "cannot determine absolute directory name of '$dir'" - absdir=$dir - fi - ;; - esac - if $GREP "^installed=no" $deplib > /dev/null; then - case $host in - *-*-darwin*) - depdepl= - eval deplibrary_names=`$SED -n -e 's/^library_names=\(.*\)$/\1/p' $deplib` - if test -n "$deplibrary_names"; then - for tmp in $deplibrary_names; do - depdepl=$tmp - done - if test -f "$absdir/$objdir/$depdepl"; then - depdepl=$absdir/$objdir/$depdepl - darwin_install_name=`$OTOOL -L $depdepl | awk '{if (NR == 2) {print $1;exit}}'` - if test -z "$darwin_install_name"; then - darwin_install_name=`$OTOOL64 -L $depdepl | awk '{if (NR == 2) {print $1;exit}}'` - fi - func_append compiler_flags " $wl-dylib_file $wl$darwin_install_name:$depdepl" - func_append linker_flags " -dylib_file $darwin_install_name:$depdepl" - path= - fi - fi - ;; - *) - path=-L$absdir/$objdir - ;; - esac - else - eval libdir=`$SED -n -e 's/^libdir=\(.*\)$/\1/p' $deplib` - test -z "$libdir" && \ - func_fatal_error "'$deplib' is not a valid libtool archive" - test "$absdir" != "$libdir" && \ - func_warning "'$deplib' seems to be moved" - - path=-L$absdir - fi - ;; - esac - case " $deplibs " in - *" $path "*) ;; - *) deplibs="$path $deplibs" ;; - esac - done - fi # link_all_deplibs != no - fi # linkmode = lib - done # for deplib in $libs - if test link = "$pass"; then - if test prog = "$linkmode"; then - compile_deplibs="$new_inherited_linker_flags $compile_deplibs" - finalize_deplibs="$new_inherited_linker_flags $finalize_deplibs" - else - compiler_flags="$compiler_flags "`$ECHO " $new_inherited_linker_flags" | $SED 's% \([^ $]*\).ltframework% -framework \1%g'` - fi - fi - dependency_libs=$newdependency_libs - if test dlpreopen = "$pass"; then - # Link the dlpreopened libraries before other libraries - for deplib in $save_deplibs; do - deplibs="$deplib $deplibs" - done - fi - if test dlopen != "$pass"; then - test conv = "$pass" || { - # Make sure lib_search_path contains only unique directories. - lib_search_path= - for dir in $newlib_search_path; do - case "$lib_search_path " in - *" $dir "*) ;; - *) func_append lib_search_path " $dir" ;; - esac - done - newlib_search_path= - } - - if test prog,link = "$linkmode,$pass"; then - vars="compile_deplibs finalize_deplibs" - else - vars=deplibs - fi - for var in $vars dependency_libs; do - # Add libraries to $var in reverse order - eval tmp_libs=\"\$$var\" - new_libs= - for deplib in $tmp_libs; do - # FIXME: Pedantically, this is the right thing to do, so - # that some nasty dependency loop isn't accidentally - # broken: - #new_libs="$deplib $new_libs" - # Pragmatically, this seems to cause very few problems in - # practice: - case $deplib in - -L*) new_libs="$deplib $new_libs" ;; - -R*) ;; - *) - # And here is the reason: when a library appears more - # than once as an explicit dependence of a library, or - # is implicitly linked in more than once by the - # compiler, it is considered special, and multiple - # occurrences thereof are not removed. Compare this - # with having the same library being listed as a - # dependency of multiple other libraries: in this case, - # we know (pedantically, we assume) the library does not - # need to be listed more than once, so we keep only the - # last copy. This is not always right, but it is rare - # enough that we require users that really mean to play - # such unportable linking tricks to link the library - # using -Wl,-lname, so that libtool does not consider it - # for duplicate removal. - case " $specialdeplibs " in - *" $deplib "*) new_libs="$deplib $new_libs" ;; - *) - case " $new_libs " in - *" $deplib "*) ;; - *) new_libs="$deplib $new_libs" ;; - esac - ;; - esac - ;; - esac - done - tmp_libs= - for deplib in $new_libs; do - case $deplib in - -L*) - case " $tmp_libs " in - *" $deplib "*) ;; - *) func_append tmp_libs " $deplib" ;; - esac - ;; - *) func_append tmp_libs " $deplib" ;; - esac - done - eval $var=\"$tmp_libs\" - done # for var - fi - - # Add Sun CC postdeps if required: - test CXX = "$tagname" && { - case $host_os in - linux*) - case `$CC -V 2>&1 | sed 5q` in - *Sun\ C*) # Sun C++ 5.9 - func_suncc_cstd_abi - - if test no != "$suncc_use_cstd_abi"; then - func_append postdeps ' -library=Cstd -library=Crun' - fi - ;; - esac - ;; - - solaris*) - func_cc_basename "$CC" - case $func_cc_basename_result in - CC* | sunCC*) - func_suncc_cstd_abi - - if test no != "$suncc_use_cstd_abi"; then - func_append postdeps ' -library=Cstd -library=Crun' - fi - ;; - esac - ;; - esac - } - - # Last step: remove runtime libs from dependency_libs - # (they stay in deplibs) - tmp_libs= - for i in $dependency_libs; do - case " $predeps $postdeps $compiler_lib_search_path " in - *" $i "*) - i= - ;; - esac - if test -n "$i"; then - func_append tmp_libs " $i" - fi - done - dependency_libs=$tmp_libs - done # for pass - if test prog = "$linkmode"; then - dlfiles=$newdlfiles - fi - if test prog = "$linkmode" || test lib = "$linkmode"; then - dlprefiles=$newdlprefiles - fi - - case $linkmode in - oldlib) - if test -n "$dlfiles$dlprefiles" || test no != "$dlself"; then - func_warning "'-dlopen' is ignored for archives" - fi - - case " $deplibs" in - *\ -l* | *\ -L*) - func_warning "'-l' and '-L' are ignored for archives" ;; - esac - - test -n "$rpath" && \ - func_warning "'-rpath' is ignored for archives" - - test -n "$xrpath" && \ - func_warning "'-R' is ignored for archives" - - test -n "$vinfo" && \ - func_warning "'-version-info/-version-number' is ignored for archives" - - test -n "$release" && \ - func_warning "'-release' is ignored for archives" - - test -n "$export_symbols$export_symbols_regex" && \ - func_warning "'-export-symbols' is ignored for archives" - - # Now set the variables for building old libraries. - build_libtool_libs=no - oldlibs=$output - func_append objs "$old_deplibs" - ;; - - lib) - # Make sure we only generate libraries of the form 'libNAME.la'. - case $outputname in - lib*) - func_stripname 'lib' '.la' "$outputname" - name=$func_stripname_result - eval shared_ext=\"$shrext_cmds\" - eval libname=\"$libname_spec\" - ;; - *) - test no = "$module" \ - && func_fatal_help "libtool library '$output' must begin with 'lib'" - - if test no != "$need_lib_prefix"; then - # Add the "lib" prefix for modules if required - func_stripname '' '.la' "$outputname" - name=$func_stripname_result - eval shared_ext=\"$shrext_cmds\" - eval libname=\"$libname_spec\" - else - func_stripname '' '.la' "$outputname" - libname=$func_stripname_result - fi - ;; - esac - - if test -n "$objs"; then - if test pass_all != "$deplibs_check_method"; then - func_fatal_error "cannot build libtool library '$output' from non-libtool objects on this host:$objs" - else - echo - $ECHO "*** Warning: Linking the shared library $output against the non-libtool" - $ECHO "*** objects $objs is not portable!" - func_append libobjs " $objs" - fi - fi - - test no = "$dlself" \ - || func_warning "'-dlopen self' is ignored for libtool libraries" - - set dummy $rpath - shift - test 1 -lt "$#" \ - && func_warning "ignoring multiple '-rpath's for a libtool library" - - install_libdir=$1 - - oldlibs= - if test -z "$rpath"; then - if test yes = "$build_libtool_libs"; then - # Building a libtool convenience library. - # Some compilers have problems with a '.al' extension so - # convenience libraries should have the same extension an - # archive normally would. - oldlibs="$output_objdir/$libname.$libext $oldlibs" - build_libtool_libs=convenience - build_old_libs=yes - fi - - test -n "$vinfo" && \ - func_warning "'-version-info/-version-number' is ignored for convenience libraries" - - test -n "$release" && \ - func_warning "'-release' is ignored for convenience libraries" - else - - # Parse the version information argument. - save_ifs=$IFS; IFS=: - set dummy $vinfo 0 0 0 - shift - IFS=$save_ifs - - test -n "$7" && \ - func_fatal_help "too many parameters to '-version-info'" - - # convert absolute version numbers to libtool ages - # this retains compatibility with .la files and attempts - # to make the code below a bit more comprehensible - - case $vinfo_number in - yes) - number_major=$1 - number_minor=$2 - number_revision=$3 - # - # There are really only two kinds -- those that - # use the current revision as the major version - # and those that subtract age and use age as - # a minor version. But, then there is irix - # that has an extra 1 added just for fun - # - case $version_type in - # correct linux to gnu/linux during the next big refactor - darwin|freebsd-elf|linux|osf|windows|none) - func_arith $number_major + $number_minor - current=$func_arith_result - age=$number_minor - revision=$number_revision - ;; - freebsd-aout|qnx|sunos) - current=$number_major - revision=$number_minor - age=0 - ;; - irix|nonstopux) - func_arith $number_major + $number_minor - current=$func_arith_result - age=$number_minor - revision=$number_minor - lt_irix_increment=no - ;; - esac - ;; - no) - current=$1 - revision=$2 - age=$3 - ;; - esac - - # Check that each of the things are valid numbers. - case $current in - 0|[1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9]|[1-9][0-9][0-9][0-9][0-9]) ;; - *) - func_error "CURRENT '$current' must be a nonnegative integer" - func_fatal_error "'$vinfo' is not valid version information" - ;; - esac - - case $revision in - 0|[1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9]|[1-9][0-9][0-9][0-9][0-9]) ;; - *) - func_error "REVISION '$revision' must be a nonnegative integer" - func_fatal_error "'$vinfo' is not valid version information" - ;; - esac - - case $age in - 0|[1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9]|[1-9][0-9][0-9][0-9][0-9]) ;; - *) - func_error "AGE '$age' must be a nonnegative integer" - func_fatal_error "'$vinfo' is not valid version information" - ;; - esac - - if test "$age" -gt "$current"; then - func_error "AGE '$age' is greater than the current interface number '$current'" - func_fatal_error "'$vinfo' is not valid version information" - fi - - # Calculate the version variables. - major= - versuffix= - verstring= - case $version_type in - none) ;; - - darwin) - # Like Linux, but with the current version available in - # verstring for coding it into the library header - func_arith $current - $age - major=.$func_arith_result - versuffix=$major.$age.$revision - # Darwin ld doesn't like 0 for these options... - func_arith $current + 1 - minor_current=$func_arith_result - xlcverstring="$wl-compatibility_version $wl$minor_current $wl-current_version $wl$minor_current.$revision" - verstring="-compatibility_version $minor_current -current_version $minor_current.$revision" - # On Darwin other compilers - case $CC in - nagfor*) - verstring="$wl-compatibility_version $wl$minor_current $wl-current_version $wl$minor_current.$revision" - ;; - *) - verstring="-compatibility_version $minor_current -current_version $minor_current.$revision" - ;; - esac - ;; - - freebsd-aout) - major=.$current - versuffix=.$current.$revision - ;; - - freebsd-elf) - func_arith $current - $age - major=.$func_arith_result - versuffix=$major.$age.$revision - ;; - - irix | nonstopux) - if test no = "$lt_irix_increment"; then - func_arith $current - $age - else - func_arith $current - $age + 1 - fi - major=$func_arith_result - - case $version_type in - nonstopux) verstring_prefix=nonstopux ;; - *) verstring_prefix=sgi ;; - esac - verstring=$verstring_prefix$major.$revision - - # Add in all the interfaces that we are compatible with. - loop=$revision - while test 0 -ne "$loop"; do - func_arith $revision - $loop - iface=$func_arith_result - func_arith $loop - 1 - loop=$func_arith_result - verstring=$verstring_prefix$major.$iface:$verstring - done - - # Before this point, $major must not contain '.'. - major=.$major - versuffix=$major.$revision - ;; - - linux) # correct to gnu/linux during the next big refactor - func_arith $current - $age - major=.$func_arith_result - versuffix=$major.$age.$revision - ;; - - osf) - func_arith $current - $age - major=.$func_arith_result - versuffix=.$current.$age.$revision - verstring=$current.$age.$revision - - # Add in all the interfaces that we are compatible with. - loop=$age - while test 0 -ne "$loop"; do - func_arith $current - $loop - iface=$func_arith_result - func_arith $loop - 1 - loop=$func_arith_result - verstring=$verstring:$iface.0 - done - - # Make executables depend on our current version. - func_append verstring ":$current.0" - ;; - - qnx) - major=.$current - versuffix=.$current - ;; - - sco) - major=.$current - versuffix=.$current - ;; - - sunos) - major=.$current - versuffix=.$current.$revision - ;; - - windows) - # Use '-' rather than '.', since we only want one - # extension on DOS 8.3 file systems. - func_arith $current - $age - major=$func_arith_result - versuffix=-$major - ;; - - *) - func_fatal_configuration "unknown library version type '$version_type'" - ;; - esac - - # Clear the version info if we defaulted, and they specified a release. - if test -z "$vinfo" && test -n "$release"; then - major= - case $version_type in - darwin) - # we can't check for "0.0" in archive_cmds due to quoting - # problems, so we reset it completely - verstring= - ;; - *) - verstring=0.0 - ;; - esac - if test no = "$need_version"; then - versuffix= - else - versuffix=.0.0 - fi - fi - - # Remove version info from name if versioning should be avoided - if test yes,no = "$avoid_version,$need_version"; then - major= - versuffix= - verstring= - fi - - # Check to see if the archive will have undefined symbols. - if test yes = "$allow_undefined"; then - if test unsupported = "$allow_undefined_flag"; then - if test yes = "$build_old_libs"; then - func_warning "undefined symbols not allowed in $host shared libraries; building static only" - build_libtool_libs=no - else - func_fatal_error "can't build $host shared library unless -no-undefined is specified" - fi - fi - else - # Don't allow undefined symbols. - allow_undefined_flag=$no_undefined_flag - fi - - fi - - func_generate_dlsyms "$libname" "$libname" : - func_append libobjs " $symfileobj" - test " " = "$libobjs" && libobjs= - - if test relink != "$opt_mode"; then - # Remove our outputs, but don't remove object files since they - # may have been created when compiling PIC objects. - removelist= - tempremovelist=`$ECHO "$output_objdir/*"` - for p in $tempremovelist; do - case $p in - *.$objext | *.gcno) - ;; - $output_objdir/$outputname | $output_objdir/$libname.* | $output_objdir/$libname$release.*) - if test -n "$precious_files_regex"; then - if $ECHO "$p" | $EGREP -e "$precious_files_regex" >/dev/null 2>&1 - then - continue - fi - fi - func_append removelist " $p" - ;; - *) ;; - esac - done - test -n "$removelist" && \ - func_show_eval "${RM}r \$removelist" - fi - - # Now set the variables for building old libraries. - if test yes = "$build_old_libs" && test convenience != "$build_libtool_libs"; then - func_append oldlibs " $output_objdir/$libname.$libext" - - # Transform .lo files to .o files. - oldobjs="$objs "`$ECHO "$libobjs" | $SP2NL | $SED "/\.$libext$/d; $lo2o" | $NL2SP` - fi - - # Eliminate all temporary directories. - #for path in $notinst_path; do - # lib_search_path=`$ECHO "$lib_search_path " | $SED "s% $path % %g"` - # deplibs=`$ECHO "$deplibs " | $SED "s% -L$path % %g"` - # dependency_libs=`$ECHO "$dependency_libs " | $SED "s% -L$path % %g"` - #done - - if test -n "$xrpath"; then - # If the user specified any rpath flags, then add them. - temp_xrpath= - for libdir in $xrpath; do - func_replace_sysroot "$libdir" - func_append temp_xrpath " -R$func_replace_sysroot_result" - case "$finalize_rpath " in - *" $libdir "*) ;; - *) func_append finalize_rpath " $libdir" ;; - esac - done - if test yes != "$hardcode_into_libs" || test yes = "$build_old_libs"; then - dependency_libs="$temp_xrpath $dependency_libs" - fi - fi - - # Make sure dlfiles contains only unique files that won't be dlpreopened - old_dlfiles=$dlfiles - dlfiles= - for lib in $old_dlfiles; do - case " $dlprefiles $dlfiles " in - *" $lib "*) ;; - *) func_append dlfiles " $lib" ;; - esac - done - - # Make sure dlprefiles contains only unique files - old_dlprefiles=$dlprefiles - dlprefiles= - for lib in $old_dlprefiles; do - case "$dlprefiles " in - *" $lib "*) ;; - *) func_append dlprefiles " $lib" ;; - esac - done - - if test yes = "$build_libtool_libs"; then - if test -n "$rpath"; then - case $host in - *-*-cygwin* | *-*-mingw* | *-*-pw32* | *-*-os2* | *-*-beos* | *-cegcc* | *-*-haiku*) - # these systems don't actually have a c library (as such)! - ;; - *-*-rhapsody* | *-*-darwin1.[012]) - # Rhapsody C library is in the System framework - func_append deplibs " System.ltframework" - ;; - *-*-netbsd*) - # Don't link with libc until the a.out ld.so is fixed. - ;; - *-*-openbsd* | *-*-freebsd* | *-*-dragonfly*) - # Do not include libc due to us having libc/libc_r. - ;; - *-*-sco3.2v5* | *-*-sco5v6*) - # Causes problems with __ctype - ;; - *-*-sysv4.2uw2* | *-*-sysv5* | *-*-unixware* | *-*-OpenUNIX*) - # Compiler inserts libc in the correct place for threads to work - ;; - *) - # Add libc to deplibs on all other systems if necessary. - if test yes = "$build_libtool_need_lc"; then - func_append deplibs " -lc" - fi - ;; - esac - fi - - # Transform deplibs into only deplibs that can be linked in shared. - name_save=$name - libname_save=$libname - release_save=$release - versuffix_save=$versuffix - major_save=$major - # I'm not sure if I'm treating the release correctly. I think - # release should show up in the -l (ie -lgmp5) so we don't want to - # add it in twice. Is that correct? - release= - versuffix= - major= - newdeplibs= - droppeddeps=no - case $deplibs_check_method in - pass_all) - # Don't check for shared/static. Everything works. - # This might be a little naive. We might want to check - # whether the library exists or not. But this is on - # osf3 & osf4 and I'm not really sure... Just - # implementing what was already the behavior. - newdeplibs=$deplibs - ;; - test_compile) - # This code stresses the "libraries are programs" paradigm to its - # limits. Maybe even breaks it. We compile a program, linking it - # against the deplibs as a proxy for the library. Then we can check - # whether they linked in statically or dynamically with ldd. - $opt_dry_run || $RM conftest.c - cat > conftest.c </dev/null` - $nocaseglob - else - potential_libs=`ls $i/$libnameglob[.-]* 2>/dev/null` - fi - for potent_lib in $potential_libs; do - # Follow soft links. - if ls -lLd "$potent_lib" 2>/dev/null | - $GREP " -> " >/dev/null; then - continue - fi - # The statement above tries to avoid entering an - # endless loop below, in case of cyclic links. - # We might still enter an endless loop, since a link - # loop can be closed while we follow links, - # but so what? - potlib=$potent_lib - while test -h "$potlib" 2>/dev/null; do - potliblink=`ls -ld $potlib | $SED 's/.* -> //'` - case $potliblink in - [\\/]* | [A-Za-z]:[\\/]*) potlib=$potliblink;; - *) potlib=`$ECHO "$potlib" | $SED 's|[^/]*$||'`"$potliblink";; - esac - done - if eval $file_magic_cmd \"\$potlib\" 2>/dev/null | - $SED -e 10q | - $EGREP "$file_magic_regex" > /dev/null; then - func_append newdeplibs " $a_deplib" - a_deplib= - break 2 - fi - done - done - fi - if test -n "$a_deplib"; then - droppeddeps=yes - echo - $ECHO "*** Warning: linker path does not have real file for library $a_deplib." - echo "*** I have the capability to make that library automatically link in when" - echo "*** you link to this library. But I can only do this if you have a" - echo "*** shared version of the library, which you do not appear to have" - echo "*** because I did check the linker path looking for a file starting" - if test -z "$potlib"; then - $ECHO "*** with $libname but no candidates were found. (...for file magic test)" - else - $ECHO "*** with $libname and none of the candidates passed a file format test" - $ECHO "*** using a file magic. Last file checked: $potlib" - fi - fi - ;; - *) - # Add a -L argument. - func_append newdeplibs " $a_deplib" - ;; - esac - done # Gone through all deplibs. - ;; - match_pattern*) - set dummy $deplibs_check_method; shift - match_pattern_regex=`expr "$deplibs_check_method" : "$1 \(.*\)"` - for a_deplib in $deplibs; do - case $a_deplib in - -l*) - func_stripname -l '' "$a_deplib" - name=$func_stripname_result - if test yes = "$allow_libtool_libs_with_static_runtimes"; then - case " $predeps $postdeps " in - *" $a_deplib "*) - func_append newdeplibs " $a_deplib" - a_deplib= - ;; - esac - fi - if test -n "$a_deplib"; then - libname=`eval "\\$ECHO \"$libname_spec\""` - for i in $lib_search_path $sys_lib_search_path $shlib_search_path; do - potential_libs=`ls $i/$libname[.-]* 2>/dev/null` - for potent_lib in $potential_libs; do - potlib=$potent_lib # see symlink-check above in file_magic test - if eval "\$ECHO \"$potent_lib\"" 2>/dev/null | $SED 10q | \ - $EGREP "$match_pattern_regex" > /dev/null; then - func_append newdeplibs " $a_deplib" - a_deplib= - break 2 - fi - done - done - fi - if test -n "$a_deplib"; then - droppeddeps=yes - echo - $ECHO "*** Warning: linker path does not have real file for library $a_deplib." - echo "*** I have the capability to make that library automatically link in when" - echo "*** you link to this library. But I can only do this if you have a" - echo "*** shared version of the library, which you do not appear to have" - echo "*** because I did check the linker path looking for a file starting" - if test -z "$potlib"; then - $ECHO "*** with $libname but no candidates were found. (...for regex pattern test)" - else - $ECHO "*** with $libname and none of the candidates passed a file format test" - $ECHO "*** using a regex pattern. Last file checked: $potlib" - fi - fi - ;; - *) - # Add a -L argument. - func_append newdeplibs " $a_deplib" - ;; - esac - done # Gone through all deplibs. - ;; - none | unknown | *) - newdeplibs= - tmp_deplibs=`$ECHO " $deplibs" | $SED 's/ -lc$//; s/ -[LR][^ ]*//g'` - if test yes = "$allow_libtool_libs_with_static_runtimes"; then - for i in $predeps $postdeps; do - # can't use Xsed below, because $i might contain '/' - tmp_deplibs=`$ECHO " $tmp_deplibs" | $SED "s|$i||"` - done - fi - case $tmp_deplibs in - *[!\ \ ]*) - echo - if test none = "$deplibs_check_method"; then - echo "*** Warning: inter-library dependencies are not supported in this platform." - else - echo "*** Warning: inter-library dependencies are not known to be supported." - fi - echo "*** All declared inter-library dependencies are being dropped." - droppeddeps=yes - ;; - esac - ;; - esac - versuffix=$versuffix_save - major=$major_save - release=$release_save - libname=$libname_save - name=$name_save - - case $host in - *-*-rhapsody* | *-*-darwin1.[012]) - # On Rhapsody replace the C library with the System framework - newdeplibs=`$ECHO " $newdeplibs" | $SED 's/ -lc / System.ltframework /'` - ;; - esac - - if test yes = "$droppeddeps"; then - if test yes = "$module"; then - echo - echo "*** Warning: libtool could not satisfy all declared inter-library" - $ECHO "*** dependencies of module $libname. Therefore, libtool will create" - echo "*** a static module, that should work as long as the dlopening" - echo "*** application is linked with the -dlopen flag." - if test -z "$global_symbol_pipe"; then - echo - echo "*** However, this would only work if libtool was able to extract symbol" - echo "*** lists from a program, using 'nm' or equivalent, but libtool could" - echo "*** not find such a program. So, this module is probably useless." - echo "*** 'nm' from GNU binutils and a full rebuild may help." - fi - if test no = "$build_old_libs"; then - oldlibs=$output_objdir/$libname.$libext - build_libtool_libs=module - build_old_libs=yes - else - build_libtool_libs=no - fi - else - echo "*** The inter-library dependencies that have been dropped here will be" - echo "*** automatically added whenever a program is linked with this library" - echo "*** or is declared to -dlopen it." - - if test no = "$allow_undefined"; then - echo - echo "*** Since this library must not contain undefined symbols," - echo "*** because either the platform does not support them or" - echo "*** it was explicitly requested with -no-undefined," - echo "*** libtool will only create a static version of it." - if test no = "$build_old_libs"; then - oldlibs=$output_objdir/$libname.$libext - build_libtool_libs=module - build_old_libs=yes - else - build_libtool_libs=no - fi - fi - fi - fi - # Done checking deplibs! - deplibs=$newdeplibs - fi - # Time to change all our "foo.ltframework" stuff back to "-framework foo" - case $host in - *-*-darwin*) - newdeplibs=`$ECHO " $newdeplibs" | $SED 's% \([^ $]*\).ltframework% -framework \1%g'` - new_inherited_linker_flags=`$ECHO " $new_inherited_linker_flags" | $SED 's% \([^ $]*\).ltframework% -framework \1%g'` - deplibs=`$ECHO " $deplibs" | $SED 's% \([^ $]*\).ltframework% -framework \1%g'` - ;; - esac - - # move library search paths that coincide with paths to not yet - # installed libraries to the beginning of the library search list - new_libs= - for path in $notinst_path; do - case " $new_libs " in - *" -L$path/$objdir "*) ;; - *) - case " $deplibs " in - *" -L$path/$objdir "*) - func_append new_libs " -L$path/$objdir" ;; - esac - ;; - esac - done - for deplib in $deplibs; do - case $deplib in - -L*) - case " $new_libs " in - *" $deplib "*) ;; - *) func_append new_libs " $deplib" ;; - esac - ;; - *) func_append new_libs " $deplib" ;; - esac - done - deplibs=$new_libs - - # All the library-specific variables (install_libdir is set above). - library_names= - old_library= - dlname= - - # Test again, we may have decided not to build it any more - if test yes = "$build_libtool_libs"; then - # Remove $wl instances when linking with ld. - # FIXME: should test the right _cmds variable. - case $archive_cmds in - *\$LD\ *) wl= ;; - esac - if test yes = "$hardcode_into_libs"; then - # Hardcode the library paths - hardcode_libdirs= - dep_rpath= - rpath=$finalize_rpath - test relink = "$opt_mode" || rpath=$compile_rpath$rpath - for libdir in $rpath; do - if test -n "$hardcode_libdir_flag_spec"; then - if test -n "$hardcode_libdir_separator"; then - func_replace_sysroot "$libdir" - libdir=$func_replace_sysroot_result - if test -z "$hardcode_libdirs"; then - hardcode_libdirs=$libdir - else - # Just accumulate the unique libdirs. - case $hardcode_libdir_separator$hardcode_libdirs$hardcode_libdir_separator in - *"$hardcode_libdir_separator$libdir$hardcode_libdir_separator"*) - ;; - *) - func_append hardcode_libdirs "$hardcode_libdir_separator$libdir" - ;; - esac - fi - else - eval flag=\"$hardcode_libdir_flag_spec\" - func_append dep_rpath " $flag" - fi - elif test -n "$runpath_var"; then - case "$perm_rpath " in - *" $libdir "*) ;; - *) func_append perm_rpath " $libdir" ;; - esac - fi - done - # Substitute the hardcoded libdirs into the rpath. - if test -n "$hardcode_libdir_separator" && - test -n "$hardcode_libdirs"; then - libdir=$hardcode_libdirs - eval "dep_rpath=\"$hardcode_libdir_flag_spec\"" - fi - if test -n "$runpath_var" && test -n "$perm_rpath"; then - # We should set the runpath_var. - rpath= - for dir in $perm_rpath; do - func_append rpath "$dir:" - done - eval "$runpath_var='$rpath\$$runpath_var'; export $runpath_var" - fi - test -n "$dep_rpath" && deplibs="$dep_rpath $deplibs" - fi - - shlibpath=$finalize_shlibpath - test relink = "$opt_mode" || shlibpath=$compile_shlibpath$shlibpath - if test -n "$shlibpath"; then - eval "$shlibpath_var='$shlibpath\$$shlibpath_var'; export $shlibpath_var" - fi - - # Get the real and link names of the library. - eval shared_ext=\"$shrext_cmds\" - eval library_names=\"$library_names_spec\" - set dummy $library_names - shift - realname=$1 - shift - - if test -n "$soname_spec"; then - eval soname=\"$soname_spec\" - else - soname=$realname - fi - if test -z "$dlname"; then - dlname=$soname - fi - - lib=$output_objdir/$realname - linknames= - for link - do - func_append linknames " $link" - done - - # Use standard objects if they are pic - test -z "$pic_flag" && libobjs=`$ECHO "$libobjs" | $SP2NL | $SED "$lo2o" | $NL2SP` - test "X$libobjs" = "X " && libobjs= - - delfiles= - if test -n "$export_symbols" && test -n "$include_expsyms"; then - $opt_dry_run || cp "$export_symbols" "$output_objdir/$libname.uexp" - export_symbols=$output_objdir/$libname.uexp - func_append delfiles " $export_symbols" - fi - - orig_export_symbols= - case $host_os in - cygwin* | mingw* | cegcc*) - if test -n "$export_symbols" && test -z "$export_symbols_regex"; then - # exporting using user supplied symfile - func_dll_def_p "$export_symbols" || { - # and it's NOT already a .def file. Must figure out - # which of the given symbols are data symbols and tag - # them as such. So, trigger use of export_symbols_cmds. - # export_symbols gets reassigned inside the "prepare - # the list of exported symbols" if statement, so the - # include_expsyms logic still works. - orig_export_symbols=$export_symbols - export_symbols= - always_export_symbols=yes - } - fi - ;; - esac - - # Prepare the list of exported symbols - if test -z "$export_symbols"; then - if test yes = "$always_export_symbols" || test -n "$export_symbols_regex"; then - func_verbose "generating symbol list for '$libname.la'" - export_symbols=$output_objdir/$libname.exp - $opt_dry_run || $RM $export_symbols - cmds=$export_symbols_cmds - save_ifs=$IFS; IFS='~' - for cmd1 in $cmds; do - IFS=$save_ifs - # Take the normal branch if the nm_file_list_spec branch - # doesn't work or if tool conversion is not needed. - case $nm_file_list_spec~$to_tool_file_cmd in - *~func_convert_file_noop | *~func_convert_file_msys_to_w32 | ~*) - try_normal_branch=yes - eval cmd=\"$cmd1\" - func_len " $cmd" - len=$func_len_result - ;; - *) - try_normal_branch=no - ;; - esac - if test yes = "$try_normal_branch" \ - && { test "$len" -lt "$max_cmd_len" \ - || test "$max_cmd_len" -le -1; } - then - func_show_eval "$cmd" 'exit $?' - skipped_export=false - elif test -n "$nm_file_list_spec"; then - func_basename "$output" - output_la=$func_basename_result - save_libobjs=$libobjs - save_output=$output - output=$output_objdir/$output_la.nm - func_to_tool_file "$output" - libobjs=$nm_file_list_spec$func_to_tool_file_result - func_append delfiles " $output" - func_verbose "creating $NM input file list: $output" - for obj in $save_libobjs; do - func_to_tool_file "$obj" - $ECHO "$func_to_tool_file_result" - done > "$output" - eval cmd=\"$cmd1\" - func_show_eval "$cmd" 'exit $?' - output=$save_output - libobjs=$save_libobjs - skipped_export=false - else - # The command line is too long to execute in one step. - func_verbose "using reloadable object file for export list..." - skipped_export=: - # Break out early, otherwise skipped_export may be - # set to false by a later but shorter cmd. - break - fi - done - IFS=$save_ifs - if test -n "$export_symbols_regex" && test : != "$skipped_export"; then - func_show_eval '$EGREP -e "$export_symbols_regex" "$export_symbols" > "${export_symbols}T"' - func_show_eval '$MV "${export_symbols}T" "$export_symbols"' - fi - fi - fi - - if test -n "$export_symbols" && test -n "$include_expsyms"; then - tmp_export_symbols=$export_symbols - test -n "$orig_export_symbols" && tmp_export_symbols=$orig_export_symbols - $opt_dry_run || eval '$ECHO "$include_expsyms" | $SP2NL >> "$tmp_export_symbols"' - fi - - if test : != "$skipped_export" && test -n "$orig_export_symbols"; then - # The given exports_symbols file has to be filtered, so filter it. - func_verbose "filter symbol list for '$libname.la' to tag DATA exports" - # FIXME: $output_objdir/$libname.filter potentially contains lots of - # 's' commands, which not all seds can handle. GNU sed should be fine - # though. Also, the filter scales superlinearly with the number of - # global variables. join(1) would be nice here, but unfortunately - # isn't a blessed tool. - $opt_dry_run || $SED -e '/[ ,]DATA/!d;s,\(.*\)\([ \,].*\),s|^\1$|\1\2|,' < $export_symbols > $output_objdir/$libname.filter - func_append delfiles " $export_symbols $output_objdir/$libname.filter" - export_symbols=$output_objdir/$libname.def - $opt_dry_run || $SED -f $output_objdir/$libname.filter < $orig_export_symbols > $export_symbols - fi - - tmp_deplibs= - for test_deplib in $deplibs; do - case " $convenience " in - *" $test_deplib "*) ;; - *) - func_append tmp_deplibs " $test_deplib" - ;; - esac - done - deplibs=$tmp_deplibs - - if test -n "$convenience"; then - if test -n "$whole_archive_flag_spec" && - test yes = "$compiler_needs_object" && - test -z "$libobjs"; then - # extract the archives, so we have objects to list. - # TODO: could optimize this to just extract one archive. - whole_archive_flag_spec= - fi - if test -n "$whole_archive_flag_spec"; then - save_libobjs=$libobjs - eval libobjs=\"\$libobjs $whole_archive_flag_spec\" - test "X$libobjs" = "X " && libobjs= - else - gentop=$output_objdir/${outputname}x - func_append generated " $gentop" - - func_extract_archives $gentop $convenience - func_append libobjs " $func_extract_archives_result" - test "X$libobjs" = "X " && libobjs= - fi - fi - - if test yes = "$thread_safe" && test -n "$thread_safe_flag_spec"; then - eval flag=\"$thread_safe_flag_spec\" - func_append linker_flags " $flag" - fi - - # Make a backup of the uninstalled library when relinking - if test relink = "$opt_mode"; then - $opt_dry_run || eval '(cd $output_objdir && $RM ${realname}U && $MV $realname ${realname}U)' || exit $? - fi - - # Do each of the archive commands. - if test yes = "$module" && test -n "$module_cmds"; then - if test -n "$export_symbols" && test -n "$module_expsym_cmds"; then - eval test_cmds=\"$module_expsym_cmds\" - cmds=$module_expsym_cmds - else - eval test_cmds=\"$module_cmds\" - cmds=$module_cmds - fi - else - if test -n "$export_symbols" && test -n "$archive_expsym_cmds"; then - eval test_cmds=\"$archive_expsym_cmds\" - cmds=$archive_expsym_cmds - else - eval test_cmds=\"$archive_cmds\" - cmds=$archive_cmds - fi - fi - - if test : != "$skipped_export" && - func_len " $test_cmds" && - len=$func_len_result && - test "$len" -lt "$max_cmd_len" || test "$max_cmd_len" -le -1; then - : - else - # The command line is too long to link in one step, link piecewise - # or, if using GNU ld and skipped_export is not :, use a linker - # script. - - # Save the value of $output and $libobjs because we want to - # use them later. If we have whole_archive_flag_spec, we - # want to use save_libobjs as it was before - # whole_archive_flag_spec was expanded, because we can't - # assume the linker understands whole_archive_flag_spec. - # This may have to be revisited, in case too many - # convenience libraries get linked in and end up exceeding - # the spec. - if test -z "$convenience" || test -z "$whole_archive_flag_spec"; then - save_libobjs=$libobjs - fi - save_output=$output - func_basename "$output" - output_la=$func_basename_result - - # Clear the reloadable object creation command queue and - # initialize k to one. - test_cmds= - concat_cmds= - objlist= - last_robj= - k=1 - - if test -n "$save_libobjs" && test : != "$skipped_export" && test yes = "$with_gnu_ld"; then - output=$output_objdir/$output_la.lnkscript - func_verbose "creating GNU ld script: $output" - echo 'INPUT (' > $output - for obj in $save_libobjs - do - func_to_tool_file "$obj" - $ECHO "$func_to_tool_file_result" >> $output - done - echo ')' >> $output - func_append delfiles " $output" - func_to_tool_file "$output" - output=$func_to_tool_file_result - elif test -n "$save_libobjs" && test : != "$skipped_export" && test -n "$file_list_spec"; then - output=$output_objdir/$output_la.lnk - func_verbose "creating linker input file list: $output" - : > $output - set x $save_libobjs - shift - firstobj= - if test yes = "$compiler_needs_object"; then - firstobj="$1 " - shift - fi - for obj - do - func_to_tool_file "$obj" - $ECHO "$func_to_tool_file_result" >> $output - done - func_append delfiles " $output" - func_to_tool_file "$output" - output=$firstobj\"$file_list_spec$func_to_tool_file_result\" - else - if test -n "$save_libobjs"; then - func_verbose "creating reloadable object files..." - output=$output_objdir/$output_la-$k.$objext - eval test_cmds=\"$reload_cmds\" - func_len " $test_cmds" - len0=$func_len_result - len=$len0 - - # Loop over the list of objects to be linked. - for obj in $save_libobjs - do - func_len " $obj" - func_arith $len + $func_len_result - len=$func_arith_result - if test -z "$objlist" || - test "$len" -lt "$max_cmd_len"; then - func_append objlist " $obj" - else - # The command $test_cmds is almost too long, add a - # command to the queue. - if test 1 -eq "$k"; then - # The first file doesn't have a previous command to add. - reload_objs=$objlist - eval concat_cmds=\"$reload_cmds\" - else - # All subsequent reloadable object files will link in - # the last one created. - reload_objs="$objlist $last_robj" - eval concat_cmds=\"\$concat_cmds~$reload_cmds~\$RM $last_robj\" - fi - last_robj=$output_objdir/$output_la-$k.$objext - func_arith $k + 1 - k=$func_arith_result - output=$output_objdir/$output_la-$k.$objext - objlist=" $obj" - func_len " $last_robj" - func_arith $len0 + $func_len_result - len=$func_arith_result - fi - done - # Handle the remaining objects by creating one last - # reloadable object file. All subsequent reloadable object - # files will link in the last one created. - test -z "$concat_cmds" || concat_cmds=$concat_cmds~ - reload_objs="$objlist $last_robj" - eval concat_cmds=\"\$concat_cmds$reload_cmds\" - if test -n "$last_robj"; then - eval concat_cmds=\"\$concat_cmds~\$RM $last_robj\" - fi - func_append delfiles " $output" - - else - output= - fi - - ${skipped_export-false} && { - func_verbose "generating symbol list for '$libname.la'" - export_symbols=$output_objdir/$libname.exp - $opt_dry_run || $RM $export_symbols - libobjs=$output - # Append the command to create the export file. - test -z "$concat_cmds" || concat_cmds=$concat_cmds~ - eval concat_cmds=\"\$concat_cmds$export_symbols_cmds\" - if test -n "$last_robj"; then - eval concat_cmds=\"\$concat_cmds~\$RM $last_robj\" - fi - } - - test -n "$save_libobjs" && - func_verbose "creating a temporary reloadable object file: $output" - - # Loop through the commands generated above and execute them. - save_ifs=$IFS; IFS='~' - for cmd in $concat_cmds; do - IFS=$save_ifs - $opt_quiet || { - func_quote_for_expand "$cmd" - eval "func_echo $func_quote_for_expand_result" - } - $opt_dry_run || eval "$cmd" || { - lt_exit=$? - - # Restore the uninstalled library and exit - if test relink = "$opt_mode"; then - ( cd "$output_objdir" && \ - $RM "${realname}T" && \ - $MV "${realname}U" "$realname" ) - fi - - exit $lt_exit - } - done - IFS=$save_ifs - - if test -n "$export_symbols_regex" && ${skipped_export-false}; then - func_show_eval '$EGREP -e "$export_symbols_regex" "$export_symbols" > "${export_symbols}T"' - func_show_eval '$MV "${export_symbols}T" "$export_symbols"' - fi - fi - - ${skipped_export-false} && { - if test -n "$export_symbols" && test -n "$include_expsyms"; then - tmp_export_symbols=$export_symbols - test -n "$orig_export_symbols" && tmp_export_symbols=$orig_export_symbols - $opt_dry_run || eval '$ECHO "$include_expsyms" | $SP2NL >> "$tmp_export_symbols"' - fi - - if test -n "$orig_export_symbols"; then - # The given exports_symbols file has to be filtered, so filter it. - func_verbose "filter symbol list for '$libname.la' to tag DATA exports" - # FIXME: $output_objdir/$libname.filter potentially contains lots of - # 's' commands, which not all seds can handle. GNU sed should be fine - # though. Also, the filter scales superlinearly with the number of - # global variables. join(1) would be nice here, but unfortunately - # isn't a blessed tool. - $opt_dry_run || $SED -e '/[ ,]DATA/!d;s,\(.*\)\([ \,].*\),s|^\1$|\1\2|,' < $export_symbols > $output_objdir/$libname.filter - func_append delfiles " $export_symbols $output_objdir/$libname.filter" - export_symbols=$output_objdir/$libname.def - $opt_dry_run || $SED -f $output_objdir/$libname.filter < $orig_export_symbols > $export_symbols - fi - } - - libobjs=$output - # Restore the value of output. - output=$save_output - - if test -n "$convenience" && test -n "$whole_archive_flag_spec"; then - eval libobjs=\"\$libobjs $whole_archive_flag_spec\" - test "X$libobjs" = "X " && libobjs= - fi - # Expand the library linking commands again to reset the - # value of $libobjs for piecewise linking. - - # Do each of the archive commands. - if test yes = "$module" && test -n "$module_cmds"; then - if test -n "$export_symbols" && test -n "$module_expsym_cmds"; then - cmds=$module_expsym_cmds - else - cmds=$module_cmds - fi - else - if test -n "$export_symbols" && test -n "$archive_expsym_cmds"; then - cmds=$archive_expsym_cmds - else - cmds=$archive_cmds - fi - fi - fi - - if test -n "$delfiles"; then - # Append the command to remove temporary files to $cmds. - eval cmds=\"\$cmds~\$RM $delfiles\" - fi - - # Add any objects from preloaded convenience libraries - if test -n "$dlprefiles"; then - gentop=$output_objdir/${outputname}x - func_append generated " $gentop" - - func_extract_archives $gentop $dlprefiles - func_append libobjs " $func_extract_archives_result" - test "X$libobjs" = "X " && libobjs= - fi - - save_ifs=$IFS; IFS='~' - for cmd in $cmds; do - IFS=$sp$nl - eval cmd=\"$cmd\" - IFS=$save_ifs - $opt_quiet || { - func_quote_for_expand "$cmd" - eval "func_echo $func_quote_for_expand_result" - } - $opt_dry_run || eval "$cmd" || { - lt_exit=$? - - # Restore the uninstalled library and exit - if test relink = "$opt_mode"; then - ( cd "$output_objdir" && \ - $RM "${realname}T" && \ - $MV "${realname}U" "$realname" ) - fi - - exit $lt_exit - } - done - IFS=$save_ifs - - # Restore the uninstalled library and exit - if test relink = "$opt_mode"; then - $opt_dry_run || eval '(cd $output_objdir && $RM ${realname}T && $MV $realname ${realname}T && $MV ${realname}U $realname)' || exit $? - - if test -n "$convenience"; then - if test -z "$whole_archive_flag_spec"; then - func_show_eval '${RM}r "$gentop"' - fi - fi - - exit $EXIT_SUCCESS - fi - - # Create links to the real library. - for linkname in $linknames; do - if test "$realname" != "$linkname"; then - func_show_eval '(cd "$output_objdir" && $RM "$linkname" && $LN_S "$realname" "$linkname")' 'exit $?' - fi - done - - # If -module or -export-dynamic was specified, set the dlname. - if test yes = "$module" || test yes = "$export_dynamic"; then - # On all known operating systems, these are identical. - dlname=$soname - fi - fi - ;; - - obj) - if test -n "$dlfiles$dlprefiles" || test no != "$dlself"; then - func_warning "'-dlopen' is ignored for objects" - fi - - case " $deplibs" in - *\ -l* | *\ -L*) - func_warning "'-l' and '-L' are ignored for objects" ;; - esac - - test -n "$rpath" && \ - func_warning "'-rpath' is ignored for objects" - - test -n "$xrpath" && \ - func_warning "'-R' is ignored for objects" - - test -n "$vinfo" && \ - func_warning "'-version-info' is ignored for objects" - - test -n "$release" && \ - func_warning "'-release' is ignored for objects" - - case $output in - *.lo) - test -n "$objs$old_deplibs" && \ - func_fatal_error "cannot build library object '$output' from non-libtool objects" - - libobj=$output - func_lo2o "$libobj" - obj=$func_lo2o_result - ;; - *) - libobj= - obj=$output - ;; - esac - - # Delete the old objects. - $opt_dry_run || $RM $obj $libobj - - # Objects from convenience libraries. This assumes - # single-version convenience libraries. Whenever we create - # different ones for PIC/non-PIC, this we'll have to duplicate - # the extraction. - reload_conv_objs= - gentop= - # if reload_cmds runs $LD directly, get rid of -Wl from - # whole_archive_flag_spec and hope we can get by with turning comma - # into space. - case $reload_cmds in - *\$LD[\ \$]*) wl= ;; - esac - if test -n "$convenience"; then - if test -n "$whole_archive_flag_spec"; then - eval tmp_whole_archive_flags=\"$whole_archive_flag_spec\" - test -n "$wl" || tmp_whole_archive_flags=`$ECHO "$tmp_whole_archive_flags" | $SED 's|,| |g'` - reload_conv_objs=$reload_objs\ $tmp_whole_archive_flags - else - gentop=$output_objdir/${obj}x - func_append generated " $gentop" - - func_extract_archives $gentop $convenience - reload_conv_objs="$reload_objs $func_extract_archives_result" - fi - fi - - # If we're not building shared, we need to use non_pic_objs - test yes = "$build_libtool_libs" || libobjs=$non_pic_objects - - # Create the old-style object. - reload_objs=$objs$old_deplibs' '`$ECHO "$libobjs" | $SP2NL | $SED "/\.$libext$/d; /\.lib$/d; $lo2o" | $NL2SP`' '$reload_conv_objs - - output=$obj - func_execute_cmds "$reload_cmds" 'exit $?' - - # Exit if we aren't doing a library object file. - if test -z "$libobj"; then - if test -n "$gentop"; then - func_show_eval '${RM}r "$gentop"' - fi - - exit $EXIT_SUCCESS - fi - - test yes = "$build_libtool_libs" || { - if test -n "$gentop"; then - func_show_eval '${RM}r "$gentop"' - fi - - # Create an invalid libtool object if no PIC, so that we don't - # accidentally link it into a program. - # $show "echo timestamp > $libobj" - # $opt_dry_run || eval "echo timestamp > $libobj" || exit $? - exit $EXIT_SUCCESS - } - - if test -n "$pic_flag" || test default != "$pic_mode"; then - # Only do commands if we really have different PIC objects. - reload_objs="$libobjs $reload_conv_objs" - output=$libobj - func_execute_cmds "$reload_cmds" 'exit $?' - fi - - if test -n "$gentop"; then - func_show_eval '${RM}r "$gentop"' - fi - - exit $EXIT_SUCCESS - ;; - - prog) - case $host in - *cygwin*) func_stripname '' '.exe' "$output" - output=$func_stripname_result.exe;; - esac - test -n "$vinfo" && \ - func_warning "'-version-info' is ignored for programs" - - test -n "$release" && \ - func_warning "'-release' is ignored for programs" - - $preload \ - && test unknown,unknown,unknown = "$dlopen_support,$dlopen_self,$dlopen_self_static" \ - && func_warning "'LT_INIT([dlopen])' not used. Assuming no dlopen support." - - case $host in - *-*-rhapsody* | *-*-darwin1.[012]) - # On Rhapsody replace the C library is the System framework - compile_deplibs=`$ECHO " $compile_deplibs" | $SED 's/ -lc / System.ltframework /'` - finalize_deplibs=`$ECHO " $finalize_deplibs" | $SED 's/ -lc / System.ltframework /'` - ;; - esac - - case $host in - *-*-darwin*) - # Don't allow lazy linking, it breaks C++ global constructors - # But is supposedly fixed on 10.4 or later (yay!). - if test CXX = "$tagname"; then - case ${MACOSX_DEPLOYMENT_TARGET-10.0} in - 10.[0123]) - func_append compile_command " $wl-bind_at_load" - func_append finalize_command " $wl-bind_at_load" - ;; - esac - fi - # Time to change all our "foo.ltframework" stuff back to "-framework foo" - compile_deplibs=`$ECHO " $compile_deplibs" | $SED 's% \([^ $]*\).ltframework% -framework \1%g'` - finalize_deplibs=`$ECHO " $finalize_deplibs" | $SED 's% \([^ $]*\).ltframework% -framework \1%g'` - ;; - esac - - - # move library search paths that coincide with paths to not yet - # installed libraries to the beginning of the library search list - new_libs= - for path in $notinst_path; do - case " $new_libs " in - *" -L$path/$objdir "*) ;; - *) - case " $compile_deplibs " in - *" -L$path/$objdir "*) - func_append new_libs " -L$path/$objdir" ;; - esac - ;; - esac - done - for deplib in $compile_deplibs; do - case $deplib in - -L*) - case " $new_libs " in - *" $deplib "*) ;; - *) func_append new_libs " $deplib" ;; - esac - ;; - *) func_append new_libs " $deplib" ;; - esac - done - compile_deplibs=$new_libs - - - func_append compile_command " $compile_deplibs" - func_append finalize_command " $finalize_deplibs" - - if test -n "$rpath$xrpath"; then - # If the user specified any rpath flags, then add them. - for libdir in $rpath $xrpath; do - # This is the magic to use -rpath. - case "$finalize_rpath " in - *" $libdir "*) ;; - *) func_append finalize_rpath " $libdir" ;; - esac - done - fi - - # Now hardcode the library paths - rpath= - hardcode_libdirs= - for libdir in $compile_rpath $finalize_rpath; do - if test -n "$hardcode_libdir_flag_spec"; then - if test -n "$hardcode_libdir_separator"; then - if test -z "$hardcode_libdirs"; then - hardcode_libdirs=$libdir - else - # Just accumulate the unique libdirs. - case $hardcode_libdir_separator$hardcode_libdirs$hardcode_libdir_separator in - *"$hardcode_libdir_separator$libdir$hardcode_libdir_separator"*) - ;; - *) - func_append hardcode_libdirs "$hardcode_libdir_separator$libdir" - ;; - esac - fi - else - eval flag=\"$hardcode_libdir_flag_spec\" - func_append rpath " $flag" - fi - elif test -n "$runpath_var"; then - case "$perm_rpath " in - *" $libdir "*) ;; - *) func_append perm_rpath " $libdir" ;; - esac - fi - case $host in - *-*-cygwin* | *-*-mingw* | *-*-pw32* | *-*-os2* | *-cegcc*) - testbindir=`$ECHO "$libdir" | $SED -e 's*/lib$*/bin*'` - case :$dllsearchpath: in - *":$libdir:"*) ;; - ::) dllsearchpath=$libdir;; - *) func_append dllsearchpath ":$libdir";; - esac - case :$dllsearchpath: in - *":$testbindir:"*) ;; - ::) dllsearchpath=$testbindir;; - *) func_append dllsearchpath ":$testbindir";; - esac - ;; - esac - done - # Substitute the hardcoded libdirs into the rpath. - if test -n "$hardcode_libdir_separator" && - test -n "$hardcode_libdirs"; then - libdir=$hardcode_libdirs - eval rpath=\" $hardcode_libdir_flag_spec\" - fi - compile_rpath=$rpath - - rpath= - hardcode_libdirs= - for libdir in $finalize_rpath; do - if test -n "$hardcode_libdir_flag_spec"; then - if test -n "$hardcode_libdir_separator"; then - if test -z "$hardcode_libdirs"; then - hardcode_libdirs=$libdir - else - # Just accumulate the unique libdirs. - case $hardcode_libdir_separator$hardcode_libdirs$hardcode_libdir_separator in - *"$hardcode_libdir_separator$libdir$hardcode_libdir_separator"*) - ;; - *) - func_append hardcode_libdirs "$hardcode_libdir_separator$libdir" - ;; - esac - fi - else - eval flag=\"$hardcode_libdir_flag_spec\" - func_append rpath " $flag" - fi - elif test -n "$runpath_var"; then - case "$finalize_perm_rpath " in - *" $libdir "*) ;; - *) func_append finalize_perm_rpath " $libdir" ;; - esac - fi - done - # Substitute the hardcoded libdirs into the rpath. - if test -n "$hardcode_libdir_separator" && - test -n "$hardcode_libdirs"; then - libdir=$hardcode_libdirs - eval rpath=\" $hardcode_libdir_flag_spec\" - fi - finalize_rpath=$rpath - - if test -n "$libobjs" && test yes = "$build_old_libs"; then - # Transform all the library objects into standard objects. - compile_command=`$ECHO "$compile_command" | $SP2NL | $SED "$lo2o" | $NL2SP` - finalize_command=`$ECHO "$finalize_command" | $SP2NL | $SED "$lo2o" | $NL2SP` - fi - - func_generate_dlsyms "$outputname" "@PROGRAM@" false - - # template prelinking step - if test -n "$prelink_cmds"; then - func_execute_cmds "$prelink_cmds" 'exit $?' - fi - - wrappers_required=: - case $host in - *cegcc* | *mingw32ce*) - # Disable wrappers for cegcc and mingw32ce hosts, we are cross compiling anyway. - wrappers_required=false - ;; - *cygwin* | *mingw* ) - test yes = "$build_libtool_libs" || wrappers_required=false - ;; - *) - if test no = "$need_relink" || test yes != "$build_libtool_libs"; then - wrappers_required=false - fi - ;; - esac - $wrappers_required || { - # Replace the output file specification. - compile_command=`$ECHO "$compile_command" | $SED 's%@OUTPUT@%'"$output"'%g'` - link_command=$compile_command$compile_rpath - - # We have no uninstalled library dependencies, so finalize right now. - exit_status=0 - func_show_eval "$link_command" 'exit_status=$?' - - if test -n "$postlink_cmds"; then - func_to_tool_file "$output" - postlink_cmds=`func_echo_all "$postlink_cmds" | $SED -e 's%@OUTPUT@%'"$output"'%g' -e 's%@TOOL_OUTPUT@%'"$func_to_tool_file_result"'%g'` - func_execute_cmds "$postlink_cmds" 'exit $?' - fi - - # Delete the generated files. - if test -f "$output_objdir/${outputname}S.$objext"; then - func_show_eval '$RM "$output_objdir/${outputname}S.$objext"' - fi - - exit $exit_status - } - - if test -n "$compile_shlibpath$finalize_shlibpath"; then - compile_command="$shlibpath_var=\"$compile_shlibpath$finalize_shlibpath\$$shlibpath_var\" $compile_command" - fi - if test -n "$finalize_shlibpath"; then - finalize_command="$shlibpath_var=\"$finalize_shlibpath\$$shlibpath_var\" $finalize_command" - fi - - compile_var= - finalize_var= - if test -n "$runpath_var"; then - if test -n "$perm_rpath"; then - # We should set the runpath_var. - rpath= - for dir in $perm_rpath; do - func_append rpath "$dir:" - done - compile_var="$runpath_var=\"$rpath\$$runpath_var\" " - fi - if test -n "$finalize_perm_rpath"; then - # We should set the runpath_var. - rpath= - for dir in $finalize_perm_rpath; do - func_append rpath "$dir:" - done - finalize_var="$runpath_var=\"$rpath\$$runpath_var\" " - fi - fi - - if test yes = "$no_install"; then - # We don't need to create a wrapper script. - link_command=$compile_var$compile_command$compile_rpath - # Replace the output file specification. - link_command=`$ECHO "$link_command" | $SED 's%@OUTPUT@%'"$output"'%g'` - # Delete the old output file. - $opt_dry_run || $RM $output - # Link the executable and exit - func_show_eval "$link_command" 'exit $?' - - if test -n "$postlink_cmds"; then - func_to_tool_file "$output" - postlink_cmds=`func_echo_all "$postlink_cmds" | $SED -e 's%@OUTPUT@%'"$output"'%g' -e 's%@TOOL_OUTPUT@%'"$func_to_tool_file_result"'%g'` - func_execute_cmds "$postlink_cmds" 'exit $?' - fi - - exit $EXIT_SUCCESS - fi - - case $hardcode_action,$fast_install in - relink,*) - # Fast installation is not supported - link_command=$compile_var$compile_command$compile_rpath - relink_command=$finalize_var$finalize_command$finalize_rpath - - func_warning "this platform does not like uninstalled shared libraries" - func_warning "'$output' will be relinked during installation" - ;; - *,yes) - link_command=$finalize_var$compile_command$finalize_rpath - relink_command=`$ECHO "$compile_var$compile_command$compile_rpath" | $SED 's%@OUTPUT@%\$progdir/\$file%g'` - ;; - *,no) - link_command=$compile_var$compile_command$compile_rpath - relink_command=$finalize_var$finalize_command$finalize_rpath - ;; - *,needless) - link_command=$finalize_var$compile_command$finalize_rpath - relink_command= - ;; - esac - - # Replace the output file specification. - link_command=`$ECHO "$link_command" | $SED 's%@OUTPUT@%'"$output_objdir/$outputname"'%g'` - - # Delete the old output files. - $opt_dry_run || $RM $output $output_objdir/$outputname $output_objdir/lt-$outputname - - func_show_eval "$link_command" 'exit $?' - - if test -n "$postlink_cmds"; then - func_to_tool_file "$output_objdir/$outputname" - postlink_cmds=`func_echo_all "$postlink_cmds" | $SED -e 's%@OUTPUT@%'"$output_objdir/$outputname"'%g' -e 's%@TOOL_OUTPUT@%'"$func_to_tool_file_result"'%g'` - func_execute_cmds "$postlink_cmds" 'exit $?' - fi - - # Now create the wrapper script. - func_verbose "creating $output" - - # Quote the relink command for shipping. - if test -n "$relink_command"; then - # Preserve any variables that may affect compiler behavior - for var in $variables_saved_for_relink; do - if eval test -z \"\${$var+set}\"; then - relink_command="{ test -z \"\${$var+set}\" || $lt_unset $var || { $var=; export $var; }; }; $relink_command" - elif eval var_value=\$$var; test -z "$var_value"; then - relink_command="$var=; export $var; $relink_command" - else - func_quote_for_eval "$var_value" - relink_command="$var=$func_quote_for_eval_result; export $var; $relink_command" - fi - done - relink_command="(cd `pwd`; $relink_command)" - relink_command=`$ECHO "$relink_command" | $SED "$sed_quote_subst"` - fi - - # Only actually do things if not in dry run mode. - $opt_dry_run || { - # win32 will think the script is a binary if it has - # a .exe suffix, so we strip it off here. - case $output in - *.exe) func_stripname '' '.exe' "$output" - output=$func_stripname_result ;; - esac - # test for cygwin because mv fails w/o .exe extensions - case $host in - *cygwin*) - exeext=.exe - func_stripname '' '.exe' "$outputname" - outputname=$func_stripname_result ;; - *) exeext= ;; - esac - case $host in - *cygwin* | *mingw* ) - func_dirname_and_basename "$output" "" "." - output_name=$func_basename_result - output_path=$func_dirname_result - cwrappersource=$output_path/$objdir/lt-$output_name.c - cwrapper=$output_path/$output_name.exe - $RM $cwrappersource $cwrapper - trap "$RM $cwrappersource $cwrapper; exit $EXIT_FAILURE" 1 2 15 - - func_emit_cwrapperexe_src > $cwrappersource - - # The wrapper executable is built using the $host compiler, - # because it contains $host paths and files. If cross- - # compiling, it, like the target executable, must be - # executed on the $host or under an emulation environment. - $opt_dry_run || { - $LTCC $LTCFLAGS -o $cwrapper $cwrappersource - $STRIP $cwrapper - } - - # Now, create the wrapper script for func_source use: - func_ltwrapper_scriptname $cwrapper - $RM $func_ltwrapper_scriptname_result - trap "$RM $func_ltwrapper_scriptname_result; exit $EXIT_FAILURE" 1 2 15 - $opt_dry_run || { - # note: this script will not be executed, so do not chmod. - if test "x$build" = "x$host"; then - $cwrapper --lt-dump-script > $func_ltwrapper_scriptname_result - else - func_emit_wrapper no > $func_ltwrapper_scriptname_result - fi - } - ;; - * ) - $RM $output - trap "$RM $output; exit $EXIT_FAILURE" 1 2 15 - - func_emit_wrapper no > $output - chmod +x $output - ;; - esac - } - exit $EXIT_SUCCESS - ;; - esac - - # See if we need to build an old-fashioned archive. - for oldlib in $oldlibs; do - - case $build_libtool_libs in - convenience) - oldobjs="$libobjs_save $symfileobj" - addlibs=$convenience - build_libtool_libs=no - ;; - module) - oldobjs=$libobjs_save - addlibs=$old_convenience - build_libtool_libs=no - ;; - *) - oldobjs="$old_deplibs $non_pic_objects" - $preload && test -f "$symfileobj" \ - && func_append oldobjs " $symfileobj" - addlibs=$old_convenience - ;; - esac - - if test -n "$addlibs"; then - gentop=$output_objdir/${outputname}x - func_append generated " $gentop" - - func_extract_archives $gentop $addlibs - func_append oldobjs " $func_extract_archives_result" - fi - - # Do each command in the archive commands. - if test -n "$old_archive_from_new_cmds" && test yes = "$build_libtool_libs"; then - cmds=$old_archive_from_new_cmds - else - - # Add any objects from preloaded convenience libraries - if test -n "$dlprefiles"; then - gentop=$output_objdir/${outputname}x - func_append generated " $gentop" - - func_extract_archives $gentop $dlprefiles - func_append oldobjs " $func_extract_archives_result" - fi - - # POSIX demands no paths to be encoded in archives. We have - # to avoid creating archives with duplicate basenames if we - # might have to extract them afterwards, e.g., when creating a - # static archive out of a convenience library, or when linking - # the entirety of a libtool archive into another (currently - # not supported by libtool). - if (for obj in $oldobjs - do - func_basename "$obj" - $ECHO "$func_basename_result" - done | sort | sort -uc >/dev/null 2>&1); then - : - else - echo "copying selected object files to avoid basename conflicts..." - gentop=$output_objdir/${outputname}x - func_append generated " $gentop" - func_mkdir_p "$gentop" - save_oldobjs=$oldobjs - oldobjs= - counter=1 - for obj in $save_oldobjs - do - func_basename "$obj" - objbase=$func_basename_result - case " $oldobjs " in - " ") oldobjs=$obj ;; - *[\ /]"$objbase "*) - while :; do - # Make sure we don't pick an alternate name that also - # overlaps. - newobj=lt$counter-$objbase - func_arith $counter + 1 - counter=$func_arith_result - case " $oldobjs " in - *[\ /]"$newobj "*) ;; - *) if test ! -f "$gentop/$newobj"; then break; fi ;; - esac - done - func_show_eval "ln $obj $gentop/$newobj || cp $obj $gentop/$newobj" - func_append oldobjs " $gentop/$newobj" - ;; - *) func_append oldobjs " $obj" ;; - esac - done - fi - func_to_tool_file "$oldlib" func_convert_file_msys_to_w32 - tool_oldlib=$func_to_tool_file_result - eval cmds=\"$old_archive_cmds\" - - func_len " $cmds" - len=$func_len_result - if test "$len" -lt "$max_cmd_len" || test "$max_cmd_len" -le -1; then - cmds=$old_archive_cmds - elif test -n "$archiver_list_spec"; then - func_verbose "using command file archive linking..." - for obj in $oldobjs - do - func_to_tool_file "$obj" - $ECHO "$func_to_tool_file_result" - done > $output_objdir/$libname.libcmd - func_to_tool_file "$output_objdir/$libname.libcmd" - oldobjs=" $archiver_list_spec$func_to_tool_file_result" - cmds=$old_archive_cmds - else - # the command line is too long to link in one step, link in parts - func_verbose "using piecewise archive linking..." - save_RANLIB=$RANLIB - RANLIB=: - objlist= - concat_cmds= - save_oldobjs=$oldobjs - oldobjs= - # Is there a better way of finding the last object in the list? - for obj in $save_oldobjs - do - last_oldobj=$obj - done - eval test_cmds=\"$old_archive_cmds\" - func_len " $test_cmds" - len0=$func_len_result - len=$len0 - for obj in $save_oldobjs - do - func_len " $obj" - func_arith $len + $func_len_result - len=$func_arith_result - func_append objlist " $obj" - if test "$len" -lt "$max_cmd_len"; then - : - else - # the above command should be used before it gets too long - oldobjs=$objlist - if test "$obj" = "$last_oldobj"; then - RANLIB=$save_RANLIB - fi - test -z "$concat_cmds" || concat_cmds=$concat_cmds~ - eval concat_cmds=\"\$concat_cmds$old_archive_cmds\" - objlist= - len=$len0 - fi - done - RANLIB=$save_RANLIB - oldobjs=$objlist - if test -z "$oldobjs"; then - eval cmds=\"\$concat_cmds\" - else - eval cmds=\"\$concat_cmds~\$old_archive_cmds\" - fi - fi - fi - func_execute_cmds "$cmds" 'exit $?' - done - - test -n "$generated" && \ - func_show_eval "${RM}r$generated" - - # Now create the libtool archive. - case $output in - *.la) - old_library= - test yes = "$build_old_libs" && old_library=$libname.$libext - func_verbose "creating $output" - - # Preserve any variables that may affect compiler behavior - for var in $variables_saved_for_relink; do - if eval test -z \"\${$var+set}\"; then - relink_command="{ test -z \"\${$var+set}\" || $lt_unset $var || { $var=; export $var; }; }; $relink_command" - elif eval var_value=\$$var; test -z "$var_value"; then - relink_command="$var=; export $var; $relink_command" - else - func_quote_for_eval "$var_value" - relink_command="$var=$func_quote_for_eval_result; export $var; $relink_command" - fi - done - # Quote the link command for shipping. - relink_command="(cd `pwd`; $SHELL \"$progpath\" $preserve_args --mode=relink $libtool_args @inst_prefix_dir@)" - relink_command=`$ECHO "$relink_command" | $SED "$sed_quote_subst"` - if test yes = "$hardcode_automatic"; then - relink_command= - fi - - # Only create the output if not a dry run. - $opt_dry_run || { - for installed in no yes; do - if test yes = "$installed"; then - if test -z "$install_libdir"; then - break - fi - output=$output_objdir/${outputname}i - # Replace all uninstalled libtool libraries with the installed ones - newdependency_libs= - for deplib in $dependency_libs; do - case $deplib in - *.la) - func_basename "$deplib" - name=$func_basename_result - func_resolve_sysroot "$deplib" - eval libdir=`$SED -n -e 's/^libdir=\(.*\)$/\1/p' $func_resolve_sysroot_result` - test -z "$libdir" && \ - func_fatal_error "'$deplib' is not a valid libtool archive" - func_append newdependency_libs " ${lt_sysroot:+=}$libdir/$name" - ;; - -L*) - func_stripname -L '' "$deplib" - func_replace_sysroot "$func_stripname_result" - func_append newdependency_libs " -L$func_replace_sysroot_result" - ;; - -R*) - func_stripname -R '' "$deplib" - func_replace_sysroot "$func_stripname_result" - func_append newdependency_libs " -R$func_replace_sysroot_result" - ;; - *) func_append newdependency_libs " $deplib" ;; - esac - done - dependency_libs=$newdependency_libs - newdlfiles= - - for lib in $dlfiles; do - case $lib in - *.la) - func_basename "$lib" - name=$func_basename_result - eval libdir=`$SED -n -e 's/^libdir=\(.*\)$/\1/p' $lib` - test -z "$libdir" && \ - func_fatal_error "'$lib' is not a valid libtool archive" - func_append newdlfiles " ${lt_sysroot:+=}$libdir/$name" - ;; - *) func_append newdlfiles " $lib" ;; - esac - done - dlfiles=$newdlfiles - newdlprefiles= - for lib in $dlprefiles; do - case $lib in - *.la) - # Only pass preopened files to the pseudo-archive (for - # eventual linking with the app. that links it) if we - # didn't already link the preopened objects directly into - # the library: - func_basename "$lib" - name=$func_basename_result - eval libdir=`$SED -n -e 's/^libdir=\(.*\)$/\1/p' $lib` - test -z "$libdir" && \ - func_fatal_error "'$lib' is not a valid libtool archive" - func_append newdlprefiles " ${lt_sysroot:+=}$libdir/$name" - ;; - esac - done - dlprefiles=$newdlprefiles - else - newdlfiles= - for lib in $dlfiles; do - case $lib in - [\\/]* | [A-Za-z]:[\\/]*) abs=$lib ;; - *) abs=`pwd`"/$lib" ;; - esac - func_append newdlfiles " $abs" - done - dlfiles=$newdlfiles - newdlprefiles= - for lib in $dlprefiles; do - case $lib in - [\\/]* | [A-Za-z]:[\\/]*) abs=$lib ;; - *) abs=`pwd`"/$lib" ;; - esac - func_append newdlprefiles " $abs" - done - dlprefiles=$newdlprefiles - fi - $RM $output - # place dlname in correct position for cygwin - # In fact, it would be nice if we could use this code for all target - # systems that can't hard-code library paths into their executables - # and that have no shared library path variable independent of PATH, - # but it turns out we can't easily determine that from inspecting - # libtool variables, so we have to hard-code the OSs to which it - # applies here; at the moment, that means platforms that use the PE - # object format with DLL files. See the long comment at the top of - # tests/bindir.at for full details. - tdlname=$dlname - case $host,$output,$installed,$module,$dlname in - *cygwin*,*lai,yes,no,*.dll | *mingw*,*lai,yes,no,*.dll | *cegcc*,*lai,yes,no,*.dll) - # If a -bindir argument was supplied, place the dll there. - if test -n "$bindir"; then - func_relative_path "$install_libdir" "$bindir" - tdlname=$func_relative_path_result/$dlname - else - # Otherwise fall back on heuristic. - tdlname=../bin/$dlname - fi - ;; - esac - $ECHO > $output "\ -# $outputname - a libtool library file -# Generated by $PROGRAM (GNU $PACKAGE) $VERSION -# -# Please DO NOT delete this file! -# It is necessary for linking the library. - -# The name that we can dlopen(3). -dlname='$tdlname' - -# Names of this library. -library_names='$library_names' - -# The name of the static archive. -old_library='$old_library' - -# Linker flags that cannot go in dependency_libs. -inherited_linker_flags='$new_inherited_linker_flags' - -# Libraries that this one depends upon. -dependency_libs='$dependency_libs' - -# Names of additional weak libraries provided by this library -weak_library_names='$weak_libs' - -# Version information for $libname. -current=$current -age=$age -revision=$revision - -# Is this an already installed library? -installed=$installed - -# Should we warn about portability when linking against -modules? -shouldnotlink=$module - -# Files to dlopen/dlpreopen -dlopen='$dlfiles' -dlpreopen='$dlprefiles' - -# Directory that this library needs to be installed in: -libdir='$install_libdir'" - if test no,yes = "$installed,$need_relink"; then - $ECHO >> $output "\ -relink_command=\"$relink_command\"" - fi - done - } - - # Do a symbolic link so that the libtool archive can be found in - # LD_LIBRARY_PATH before the program is installed. - func_show_eval '( cd "$output_objdir" && $RM "$outputname" && $LN_S "../$outputname" "$outputname" )' 'exit $?' - ;; - esac - exit $EXIT_SUCCESS -} - -if test link = "$opt_mode" || test relink = "$opt_mode"; then - func_mode_link ${1+"$@"} -fi - - -# func_mode_uninstall arg... -func_mode_uninstall () -{ - $debug_cmd - - RM=$nonopt - files= - rmforce=false - exit_status=0 - - # This variable tells wrapper scripts just to set variables rather - # than running their programs. - libtool_install_magic=$magic - - for arg - do - case $arg in - -f) func_append RM " $arg"; rmforce=: ;; - -*) func_append RM " $arg" ;; - *) func_append files " $arg" ;; - esac - done - - test -z "$RM" && \ - func_fatal_help "you must specify an RM program" - - rmdirs= - - for file in $files; do - func_dirname "$file" "" "." - dir=$func_dirname_result - if test . = "$dir"; then - odir=$objdir - else - odir=$dir/$objdir - fi - func_basename "$file" - name=$func_basename_result - test uninstall = "$opt_mode" && odir=$dir - - # Remember odir for removal later, being careful to avoid duplicates - if test clean = "$opt_mode"; then - case " $rmdirs " in - *" $odir "*) ;; - *) func_append rmdirs " $odir" ;; - esac - fi - - # Don't error if the file doesn't exist and rm -f was used. - if { test -L "$file"; } >/dev/null 2>&1 || - { test -h "$file"; } >/dev/null 2>&1 || - test -f "$file"; then - : - elif test -d "$file"; then - exit_status=1 - continue - elif $rmforce; then - continue - fi - - rmfiles=$file - - case $name in - *.la) - # Possibly a libtool archive, so verify it. - if func_lalib_p "$file"; then - func_source $dir/$name - - # Delete the libtool libraries and symlinks. - for n in $library_names; do - func_append rmfiles " $odir/$n" - done - test -n "$old_library" && func_append rmfiles " $odir/$old_library" - - case $opt_mode in - clean) - case " $library_names " in - *" $dlname "*) ;; - *) test -n "$dlname" && func_append rmfiles " $odir/$dlname" ;; - esac - test -n "$libdir" && func_append rmfiles " $odir/$name $odir/${name}i" - ;; - uninstall) - if test -n "$library_names"; then - # Do each command in the postuninstall commands. - func_execute_cmds "$postuninstall_cmds" '$rmforce || exit_status=1' - fi - - if test -n "$old_library"; then - # Do each command in the old_postuninstall commands. - func_execute_cmds "$old_postuninstall_cmds" '$rmforce || exit_status=1' - fi - # FIXME: should reinstall the best remaining shared library. - ;; - esac - fi - ;; - - *.lo) - # Possibly a libtool object, so verify it. - if func_lalib_p "$file"; then - - # Read the .lo file - func_source $dir/$name - - # Add PIC object to the list of files to remove. - if test -n "$pic_object" && test none != "$pic_object"; then - func_append rmfiles " $dir/$pic_object" - fi - - # Add non-PIC object to the list of files to remove. - if test -n "$non_pic_object" && test none != "$non_pic_object"; then - func_append rmfiles " $dir/$non_pic_object" - fi - fi - ;; - - *) - if test clean = "$opt_mode"; then - noexename=$name - case $file in - *.exe) - func_stripname '' '.exe' "$file" - file=$func_stripname_result - func_stripname '' '.exe' "$name" - noexename=$func_stripname_result - # $file with .exe has already been added to rmfiles, - # add $file without .exe - func_append rmfiles " $file" - ;; - esac - # Do a test to see if this is a libtool program. - if func_ltwrapper_p "$file"; then - if func_ltwrapper_executable_p "$file"; then - func_ltwrapper_scriptname "$file" - relink_command= - func_source $func_ltwrapper_scriptname_result - func_append rmfiles " $func_ltwrapper_scriptname_result" - else - relink_command= - func_source $dir/$noexename - fi - - # note $name still contains .exe if it was in $file originally - # as does the version of $file that was added into $rmfiles - func_append rmfiles " $odir/$name $odir/${name}S.$objext" - if test yes = "$fast_install" && test -n "$relink_command"; then - func_append rmfiles " $odir/lt-$name" - fi - if test "X$noexename" != "X$name"; then - func_append rmfiles " $odir/lt-$noexename.c" - fi - fi - fi - ;; - esac - func_show_eval "$RM $rmfiles" 'exit_status=1' - done - - # Try to remove the $objdir's in the directories where we deleted files - for dir in $rmdirs; do - if test -d "$dir"; then - func_show_eval "rmdir $dir >/dev/null 2>&1" - fi - done - - exit $exit_status -} - -if test uninstall = "$opt_mode" || test clean = "$opt_mode"; then - func_mode_uninstall ${1+"$@"} -fi - -test -z "$opt_mode" && { - help=$generic_help - func_fatal_help "you must specify a MODE" -} - -test -z "$exec_cmd" && \ - func_fatal_help "invalid operation mode '$opt_mode'" - -if test -n "$exec_cmd"; then - eval exec "$exec_cmd" - exit $EXIT_FAILURE -fi - -exit $exit_status - - -# The TAGs below are defined such that we never get into a situation -# where we disable both kinds of libraries. Given conflicting -# choices, we go for a static library, that is the most portable, -# since we can't tell whether shared libraries were disabled because -# the user asked for that or because the platform doesn't support -# them. This is particularly important on AIX, because we don't -# support having both static and shared libraries enabled at the same -# time on that platform, so we default to a shared-only configuration. -# If a disable-shared tag is given, we'll fallback to a static-only -# configuration. But we'll never go from static-only to shared-only. - -# ### BEGIN LIBTOOL TAG CONFIG: disable-shared -build_libtool_libs=no -build_old_libs=yes -# ### END LIBTOOL TAG CONFIG: disable-shared - -# ### BEGIN LIBTOOL TAG CONFIG: disable-static -build_old_libs=`case $build_libtool_libs in yes) echo no;; *) echo yes;; esac` -# ### END LIBTOOL TAG CONFIG: disable-static - -# Local Variables: -# mode:shell-script -# sh-indentation:2 -# End: diff --git a/magic.txt b/magic.txt index 65622687cc..f52a7a90ed 100644 --- a/magic.txt +++ b/magic.txt @@ -9,18 +9,18 @@ # PRAGMA application_id = INTEGER; # # INTEGER can be any signed 32-bit integer. That integer is written as -# a 4-byte big-endian integer into offset 68 of the database header. +# a 4-byte big-endian integer into offset 68 of the database header. # # The Monotone application used "PRAGMA user_version=1598903374;" to set # its identifier long before "PRAGMA application_id" became available. # The user_version is very similar to application_id except that it is -# stored at offset 68 instead of offset 60. The application_id pragma +# stored at offset 60 instead of offset 68. The application_id pragma # is preferred. The rule using offset 60 for Monotone is for historical # compatibility only. # 0 string =SQLite\ format\ 3 ->68 belong =0x0f055112 Fossil checkout - ->68 belong =0x0f055113 Fossil global configuration - +>68 belong =0x0f055112 Fossil checkout - +>68 belong =0x0f055113 Fossil global configuration - >68 belong =0x0f055111 Fossil repository - >68 belong =0x42654462 Bentley Systems BeSQLite Database - >68 belong =0x42654c6e Bentley Systems Localization File - @@ -29,4 +29,5 @@ >68 belong =0x47503130 OGC GeoPackage version 1.0 file - >68 belong =0x45737269 Esri Spatially-Enabled Database - >68 belong =0x4d504258 MBTiles tileset - +>68 belong =0x6a035744 TeXnicard card database >0 string =SQLite SQLite3 database diff --git a/main.mk b/main.mk index e9920180d9..c6bdeafaa3 100644 --- a/main.mk +++ b/main.mk @@ -1,83 +1,602 @@ +#!/do/not/make +# ^^^^ help out editors which guess this file's type. ############################################################################### -# The following macros should be defined before this script is -# invoked: +# This is the main makefile for sqlite. It expects to be included from +# a higher-level makefile which configures any dynamic state needed by +# this one (as documented below). # -# TOP The toplevel directory of the source tree. This is the -# directory that contains this "Makefile.in" and the -# "configure.in" script. +# Maintenance reminders: # -# BCC C Compiler and options for use in building executables that -# will run on the platform that is doing the build. +# - This file must remain devoid of GNU Make-isms. i.e. it must be +# POSIX Make compatible. "bmake" (BSD make) is available on most +# Linux systems, so compatibility is relatively easy to test. As a +# harmless exception, this file sometimes uses $(MAKEFILE_LIST) as a +# dependency. That var, in GNU Make, holds a list of all of the +# makefiles currently loaded. # -# THREADLIB Specify any extra linker options needed to make the library -# thread safe +# The variables listed below must be defined before this script is +# invoked. This file will use defaults, very possibly invalid, for any +# which are not defined. +######################################################################## +all: # -# LIBS Extra libraries options +# $(TOP) = # -# OPTS Extra compiler command-line options. +# The top-level directory of the source tree. For canonical builds +# this is the directory that contains this "Makefile.in" and the +# "auto.def" script. For out-of-tree builds, this will differ from +# $(PWD). # -# EXE The suffix to add to executable files. ".exe" for windows -# and "" for Unix. +TOP ?= $(PWD) # -# TCC C Compiler and options for use in building executables that -# will run on the target platform. This is usually the same -# as BCC, unless you are cross-compiling. +# $(PACKAGE_VERSION) = # -# AR Tools used to build a static library. -# RANLIB +# The MAJOR.MINOR.PATCH version number of this build. # -# TCL_FLAGS Extra compiler options needed for programs that use the -# TCL library. +PACKAGE_VERSION ?= # -# LIBTCL Linker options needed to link against the TCL library. +# $(B.cc) = # -# READLINE_FLAGS Compiler options needed for programs that use the -# readline() library. +# C Compiler and options for use in building executables that will run +# on the platform that is doing the build. # -# LIBREADLINE Linker options needed by programs using readline() must -# link against. +B.cc ?= $(CC) # -# Once the macros above are defined, the rest of this make script will -# build the SQLite library and testing tools. +# $(T.cc) = +# +# C Compiler and options for use in building executables that will run +# on the target platform. This is usually the same as B.cc, unless you +# are cross-compiling. Note that it should only contain flags which +# are used by _all_ build targets. Flags needed only by specific +# targets are defined elsewhere and applied on a per-target basis. +# +T.cc ?= $(B.cc) +# +# $(AR) = +# +# Tool used to build a static library from object files, without its +# arguments. $(AR.flags) are its flags for creating a lib. +# +AR ?= ar +AR.flags ?= cr +# +# $(B.exe) = +# +# File extension for executables on the build platform:. .exe for +# Windows and empty everywhere else. +# +B.exe ?= +# +# $(B.dll) and $(B.lib) = +# +# The DLL resp. static library counterparts of $(B.exe). +# +B.dll ?= .so +B.lib ?= .a +# +# $(T.exe) = +# +# File extension for executables on the target platform: .exe for +# Windows and empty everywhere else. +# +T.exe ?= $(B.exe) +# +# $(T.dll) and $(T.lib) = +# +# The DLL resp. static library counterparts of $(T.exe). +# +T.dll ?= $(B.dll) +T.lib ?= $(B.lib) +# +# HAVE_TCL = 1 to enable full tcl support, else 0. +# +HAVE_TCL ?= 0 +# +# $(TCLSH_CMD) = +# +# The canonical tclsh. +# +TCLSH_CMD ?= tclsh +# +# JimTCL is part of the autosetup suite and is suitable for all +# current in-tree code-generation TCL jobs, but it requires that we +# build it with non-default flags. The canonical build tree will, if +# no system-level tclsh is found, also have a ./jimsh0 binary. That +# one is a bare-bones build for the configure process, whereas we need +# to build it with another option enabled for use with the various +# code generators. +# +# JIMSH requires a leading path component, even if it's ./, so that it +# can be used as a shell command. +# +# On Windows platforms, if -DHAVE_REALPATH does not work then try +# -DHAVE__FULLPATH (note the double-underscore). +# +CFLAGS.jimsh ?= -DHAVE_REALPATH +JIMSH ?= ./jimsh$(T.exe) +# +# $(B.tclsh) = +# +# The TCL interpreter for in-tree code generation. May be either the +# in-tree JimTCL ($(JIMSH)) or the canonical TCL ($(TCLSH_CMD)). If +# it's JimTCL, it must be compiled with -DHAVE_REALPATH (Unix) or +# -DHAVE__FULLPATH (Windows) so that the Tcl function [file normalize] +# can work. +# +B.tclsh ?= $(JIMSH) + +# +# Autotools-conventional vars which are (in this tree) used only by +# package installation rules and for generating sqlite3.pc (pkg-config +# data file). +# +# The following ${XYZdir} vars are provided for the sake of clients +# who expect to be able to override these using autotools-conventional +# dir name vars. +# +prefix ?= /usr/local +datadir ?= $(prefix)/share +mandir ?= $(datadir)/man +includedir ?= $(prefix)/include +exec_prefix ?= $(prefix) +bindir ?= $(exec_prefix)/bin +libdir ?= $(exec_prefix)/lib +# This makefile does not use any of: +# sbindir ?= $(exec_prefix)/sbin +# sysconfdir ?= /etc +# sharedstatedir ?= $(prefix)/com +# localstatedir ?= /var +# runstatedir ?= /run +# infodir ?= $(datadir)/info +# libexecdir ?= $(exec_prefix)/libexec +### end of autotools-compatible install dir vars + + +# +# $(LDFLAGS.{feature}) and $(CFLAGS.{feature}) = +# +# Linker resp. C/CPP flags required by a specific feature, e.g. +# $(LDFLAGS.pthread) or $(CFLAGS.readline). +# +# Rather that stuffing all CFLAGS and LDFLAGS into a single set, we +# break them down on a per-feature basis and expect the build targets +# to use the one(s) it needs. +# +LDFLAGS.zlib ?= -lz +LDFLAGS.math ?= -lm +LDFLAGS.rpath ?= -Wl,-rpath -Wl,$(prefix)/lib +LDFLAGS.pthread ?= -lpthread +LDFLAGS.dlopen ?= -ldl +LDFLAGS.shlib ?= -shared +LDFLAGS.rt ?= # nanosleep on some platforms +LDFLAGS.icu ?= # -licui18n -licuuc -licudata +CFLAGS.icu ?= +LDFLAGS.libsqlite3.soname ?= # see https://sqlite.org/src/forumpost/5a3b44f510df8ded +LDFLAGS.libsqlite3.os-specific ?= # see https://sqlite.org/forum/forumpost/9dfd5b8fd525a5d7 +# libreadline (or a workalike): +# To activate readline in the shell: SHELL_OPT = -DHAVE_READLINE=1 +LDFLAGS.readline ?= -lreadline # these vary across platforms +CFLAGS.readline ?= -I$(prefix)/include +# ^^^ When using linenoise instead of readline, do something like: +# SHELL_OPT += -DHAVE_LINENOISE=1 +# CFLAGS.readline = -I$(HOME)/linenoise $(HOME)/linenoise/linenoise.c +# LDFLAGS.readline = # empty + +# +# +# $(INSTALL) = +# +# Tool for installing files and directories. It must be compatible +# with conventional Unix /usr/bin/install. Note that libtool's +# install-sh is _not_ compatible with this because it _moves_ targets +# during installation, which may break the build of targets which are +# built after others are installed. +# +# Maintenance reminder: we specifically do not strip binaries, as +# discussed in https://sqlite.org/forum/forumpost/9a67df63eda9925c. +# +INSTALL ?= install +# +# $(ENABLE_LIB_SHARED) = +# +# 1 if libsqlite3$(T.dll) should be built. +# +ENABLE_LIB_SHARED ?= 1 +# +# $(ENABLE_LIB_STATIC) = +# +# 1 if libsqlite3$(T.lib) should be built. Some components, +# e.g. libtclsqlite3 and some test apps, implicitly require the static +# library and will ignore this preference. +# +ENABLE_LIB_STATIC ?= 1 +# +# $(USE_AMALGAMATION) +# +# 1 if the amalgamation (sqlite3.c/h) should be built/used, otherwise +# the library is built from all of its original source files. +# Certain tools, like sqlite3$(T.exe), require the amalgamation and +# will ignore this preference. +# +USE_AMALGAMATION ?= 1 +# +# $(LINK_TOOLS_DYNAMICALLY) +# +# If 1, certain binaries which typically statically link against +# libsqlite3 or its component object files will instead link against +# the DLL. The caveat is that running such builds from the source tree +# may require that the user specifically prepend "." to their +# $LD_LIBRARY_PATH so that the dynamic linker does not pick up a +# libsqlite3.so from outside the source tree. Alternately, symlinking +# the in-build-tree $(libsqlite3.DLL) to some dir in the system's +# library path will work for giving the apps access to the in-tree +# DLL. +# +LINK_TOOLS_DYNAMICALLY ?= 0 +# +# $(AMALGAMATION_GEN_FLAGS) = +# +# Optional flags for the amalgamation generator. +# +AMALGAMATION_GEN_FLAGS ?= --linemacros=0 +# +# EXTRA_SRC = list of C files to append as-is to the generated +# amalgamation. It should arguably be called AMALGAMATION_EXTRA_SRC +# but this older name is already in use by clients. +# +EXTRA_SRC ?= + +# +# $(OPT_FEATURE_FLAGS) = +# +# Preprocessor flags for enabling and disabling specific libsqlite3 +# features (-DSQLITE_OMIT*, -DSQLITE_ENABLE*). The same set of OMIT +# and ENABLE flags must be passed to the LEMON parser generator and +# the mkkeywordhash tool as well. This is normally set by the +# configure process, and passing a custom value to a +# configure-filtered Makefile may not work. +# +# When using the canonical makefile, add $(OPTIONS)=... on the make +# command line to append additional options to the +# $(OPT_FEATURE_FLAGS). Some flags, because they influence generation +# of the SQL parser, only work if the build is specifically configured +# to account for them. Adding them later, when compiling the +# amalgamation separately, may or may not work. +# +# $(OPTS)=... is another way of influencing C compilation. It is +# distinctly separate from $(OPTIONS) and $(OPT_FEATURE_FLAGS) but, +# like those, $(OPTS) applies to all invocations of $(T.cc) (and some +# invocations of $(B.cc)). The configure process does not set either +# of $(OPTIONS) or $(OPTS). +# +OPT_FEATURE_FLAGS ?= +# +# $(SHELL_OPT) = +# +# CFLAGS specific to the sqlite3 CLI shell app and its close cousins. +# +SHELL_OPT ?= +# +# TCL_CONFIG_SH must, for some of the build targets, refer to a valid +# tclConfig.sh. That script will be used to populate most of the other +# TCL-related vars the build needs. The core library does not require +# TCL, but TCL is needed for running tests and certain tools, e.g. +# sqlite3_analyzer. +# +TCL_CONFIG_SH ?= +# +# $(HAVE_WASI_SDK) = +# +# Set to 1 when building with the WASI SDK. This disables certain +# build targets. It is expected that the invoker sets $(CC), $(LD), +# and $(AR) to their counterparts from the wasi-sdk. +# +HAVE_WASI_SDK ?= 0 +# +# ... and many, many more. Sane defaults are selected where possible. +# +# With the above-described defined, the rest of this make script will +# build the project's deliverables and testing tools. ################################################################################ +all: sqlite3.h sqlite3.c + +######################################################################## +######################################################################## +# Modifying anything after this point should not be necessary for most +# builds. +######################################################################## +######################################################################## + +# +# $(CFLAGS.env) holds the any $(CFLAGS) provided at configure- or +# make-time (the latter overriding the former). +# +# $(CFLAGS) should ideally only contain flags which are relevant for +# all binaries built for the target platform. However, many people +# like to pass it to "make" without realizing that it applies to +# dozens of deliverables, and they override core flags (like -fPIC) +# when doing so. To help work around that, we expect all core-most +# CFLAGS, e.g. -fPIC, to be set in $(CFLAGS.core). That enables people +# to pass their other CFLAGS without triggering, e.g., "recompile with +# -fPIC" errors. +# +# Historical note: the pre-3.48 build does not honor CPPFLAGS passed +# to make, so we do not do so here. Both the legacy and 3.48+ builds +# support CPPFLAGS passed at configure-time, and combines them with +# the configure-time CFLAGS. +# +CFLAGS.core ?= +CFLAGS.env = $(CFLAGS) +T.cc += $(CFLAGS.core) $(CFLAGS.env) + +# +# $(LDFLAGS.configure) represents any LDFLAGS=... the client passes to +# the configure process. The historical build enabled passing-on of +# user-provided LDFLAGS at configure-time but not make-time. That +# behavior is not possible to fully emulate here because this makefile +# is not filtered by the configure script, so we instead +# "soft-enforce" it by using a level of indirection, which clients who +# read this can (but are not advised to!) bypass by passing +# LDFLAGS.configure=... to this makefile. (We do not guaranty this +# variable name to be stable, so do not rely on that capability!) +# +# A significant difference from the legacy build: +# +# The legacy build applied such LDFLAGS to all link operations for all +# deliverables. The 3.48+ build applies them (as of this writing) more +# selectively: search this file LDFLAGS.configure to see where they're +# set. +# +LDFLAGS.configure ?= + +# +# The difference between $(OPT_FEATURE_FLAGS) and $(OPTS) is that the +# former is historically provided by the configure script, whereas +# $(OPTS) is intended to be provided as arguments to the make +# invocation. +# +T.cc += $(OPT_FEATURE_FLAGS) + +# +# Add in any optional global compilation flags on the make command +# line i.e. make "OPTS=-DSQLITE_ENABLE_FOO=1 -DSQLITE_OMIT_FOO=1". +# +T.cc += $(OPTS) + +# +# $(INSTALL) invocation for use with non-executable files. +# +INSTALL.noexec = $(INSTALL) -m 0644 +# ^^^ do not use GNU-specific flags to $(INSTALL), e.g. --mode=... + +# +# T.compile.gcov = gcov-specific compilation flags for the target +# platform. +# +T.compile.gcov ?= +# +# T.link.gcov = gcov-specific link flags for the target platform. +# +T.link.gcov ?= + +# +# $(T.compile) = generic target platform compiler invocation, +# differing only from $(T.cc) in that it appends $(T.compile.gcov), +# which is intended for use with gcov-related flags. +# +T.compile = $(T.cc) $(T.compile.gcov) + +# +# Optionally set by the configure script to include -DSQLITE_DEBUG=1 +# and other debug-related flags. +# +T.cc.TARGET_DEBUG ?= + +# +# Extra CFLAGS for both the core sqlite3 components and extensions. +# +# Define -D_HAVE_SQLITE_CONFIG_H so that the code knows it +# can include the generated sqlite_cfg.h. +# +T.cc.sqlite.extras = -D_HAVE_SQLITE_CONFIG_H -DBUILD_sqlite $(T.cc.TARGET_DEBUG) + +# +# $(T.cc.sqlite) is $(T.cc) plus any flags which are desired for the +# library as a whole, but not necessarily needed for every binary. It +# will normally get initially populated with flags by the +# configure-generated makefile. +# +T.cc.sqlite ?= $(T.compile) $(T.cc.sqlite.extras) + +# +# $(CFLAGS.intree_includes) = -I... flags relevant specifically to +# this tree, including any subdirectories commonly needed for building +# various tools. +# +CFLAGS.intree_includes = \ + -I. -I$(TOP)/src -I$(TOP)/ext/rtree -I$(TOP)/ext/icu \ + -I$(TOP)/ext/fts3 -I$(TOP)/ext/session \ + -I$(TOP)/ext/misc +T.cc.sqlite += $(CFLAGS.intree_includes) + +# +# $(T.cc.extension) = compiler invocation for loadable extensions. +# +T.cc.extension = $(T.compile) -I. -I$(TOP)/src $(T.cc.sqlite.extras) -DSQLITE_CORE + +# +# $(T.link) = compiler invocation for when the target will be an +# executable. +# +# $(T.link.gcov) = optional config-specific flags for $(T.link), +# intended for use with gcov-related flags. +# +T.link = $(T.cc.sqlite) $(T.link.gcov) +# +# $(T.link.shared) = $(T.link) invocation specifically for shared libraries +# +T.link.shared = $(T.link) $(LDFLAGS.shlib) -# This is how we compile # -TCCX = $(TCC) $(OPTS) -I. -I$(TOP)/src -I$(TOP) -TCCX += -I$(TOP)/ext/rtree -I$(TOP)/ext/icu -I$(TOP)/ext/fts3 -TCCX += -I$(TOP)/ext/async -I$(TOP)/ext/userauth -TCCX += -I$(TOP)/ext/fts5 -THREADLIB += $(LIBS) +# $(LDFLAGS.libsqlite3) should be used with any deliverable for which +# any of the following apply: +# +# - Results in building libsqlite3.so +# - Compiles sqlite3.c in to an application +# - Links with libsqlite3.a +# - Links in either of $(LIBOBJSO) or $(LIBOBJS1) +# +# Note that these flags are for the target build platform, not +# necessarily localhost. i.e. it should be used with $(T.cc.sqlite) +# or $(T.link) but not $(B.cc). +# +LDFLAGS.libsqlite3 = \ + $(LDFLAGS.rpath) $(LDFLAGS.pthread) \ + $(LDFLAGS.math) $(LDFLAGS.dlopen) \ + $(LDFLAGS.zlib) $(LDFLAGS.icu) \ + $(LDFLAGS.rt) $(LDFLAGS.configure) + +# +# $(install-dir.XYZ) = dirs for installation. +# +# Design note: these should arguably all be defined with surrounding +# double-quotes so that targets which have spaces in their paths will +# work, but that leads to Make treating the quotes as part of the dir +# name, which in turn leads to it never finding a matching name in the +# filesystem and always invoking ($(INSTALL) -d ...) for them. The +# moral of this story is that spaces in installation paths will break +# the install process. +# +install-dir.bin = $(DESTDIR)$(bindir) +install-dir.lib = $(DESTDIR)$(libdir) +install-dir.include = $(DESTDIR)$(includedir) +install-dir.pkgconfig = $(DESTDIR)$(libdir)/pkgconfig +install-dir.man1 = $(DESTDIR)$(mandir)/man1 +install-dir.all = $(install-dir.bin) $(install-dir.include) \ + $(install-dir.lib) $(install-dir.man1) \ + $(install-dir.pkgconfig) +$(install-dir.all): + @if [ ! -d "$@" ]; then set -x; $(INSTALL) -d "$@"; fi +# ^^^^ on some platforms, install -d fails if the target already exists. + +# +# After jimsh is compiled, we run some sanity checks to ensure that +# it was built in a way compatible with this project's scripts: +# +# 1) Ensure that it was built with realpath() or _fullpath() support. +# Without that flag the [file normalize] command will always resolve +# to an empty string. +# +# 2) Ensure that it is built with -DJIM_COMPAT (which may be +# hard-coded into jimsh0.c). Without this, the [expr] command accepts +# only a single argument. (That said: the real fix for that is to +# update any scripts which still pass multiple arguments to [expr].) +# +$(JIMSH): $(TOP)/autosetup/jimsh0.c + $(B.cc) -o $@ $(CFLAGS.jimsh) $(TOP)/autosetup/jimsh0.c + @if [ x = "x$$($(JIMSH) -e 'file normalize $(JIMSH)' 2>/dev/null)" ]; then \ + echo "$(JIMSH) was built without -DHAVE_REALPATH or -DHAVE__FULLPATH." 1>&2; \ + exit 1; \ + fi + @if [ x3 != "x$$($(JIMSH) -e 'expr 1 + 2' 2>/dev/null)" ]; then \ + echo "$(JIMSH) was built without -DJIM_COMPAT." 1>&2; \ + exit 1; \ + fi +distclean-jimsh: + rm -f $(JIMSH) +distclean: distclean-jimsh + +# +# $(MAKE_SANITY_CHECK) = a set of checks for various make vars which +# must be provided to this file before including it. If any are +# missing, this target fails. It does (almost) no semantic validation, +# only checks to see that appropriate vars are not empty. +# +# Note that $(MAKEFILE_LIST) is a GNU-make-ism but its use is harmless +# in other flavors of Make. +# +MAKE_SANITY_CHECK = .main.mk.checks +$(MAKE_SANITY_CHECK): $(MAKEFILE_LIST) $(TOP)/auto.def + @if [ x = "x$(TOP)" ]; then echo "Missing TOP var" 1>&2; exit 1; fi + @if [ ! -d "$(TOP)" ]; then echo "$(TOP) is not a directory" 1>&2; exit 1; fi + @if [ ! -f "$(TOP)/auto.def" ]; then echo "$(TOP) does not appear to be the top-most source dir" 1>&2; exit 1; fi + @if [ x = "x$(PACKAGE_VERSION)" ]; then echo "PACKAGE_VERSION must be set to the library's X.Y.Z-format version number" 1>&2; exit 1; fi + @if [ x = "x$(B.cc)" ]; then echo "Missing B.cc var" 1>&2; exit 1; fi + @if [ x = "x$(T.cc)" ]; then echo "Missing T.cc var" 1>&2; exit 1; fi + @if [ x = "x$(B.tclsh)" ]; then echo "Missing B.tclsh var" 1>&2; exit 1; fi + @if [ x = "x$(AR)" ]; then echo "Missing AR var" 1>&2; exit 1; fi + touch $@ +clean-sanity-check: + rm -f $(MAKE_SANITY_CHECK) +clean: clean-sanity-check + +# BEGIN SQLCIPHER +SQLCIPHER_OBJ = \ + sqlcipher.o \ + crypto_openssl.o \ + crypto_cc.o + +SQLCIPHER_SRC = \ + $(TOP)/src/sqlcipher.h \ + $(TOP)/src/sqlcipher.c \ + $(TOP)/src/crypto_openssl.c \ + $(TOP)/src/crypto_cc.c + +sqlcipher.o: $(TOP)/src/sqlcipher.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) $(CFLAGS.libsqlite3) -c $(TOP)/src/sqlcipher.c +crypto_openssl.o: $(TOP)/src/crypto_openssl.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) $(CFLAGS.libsqlite3) -c $(TOP)/src/crypto_openssl.c +crypto_cc.o: $(TOP)/src/crypto_cc.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) $(CFLAGS.libsqlite3) -c $(TOP)/src/crypto_cc.c + +# END SQLCIPHER -# Object files for the SQLite library. # -LIBOBJ+= vdbe.o parse.o \ - alter.o analyze.o attach.o auth.o \ +# Object files for the SQLite library (non-amalgamation). +# +LIBOBJS0 = alter.o analyze.o attach.o auth.o \ backup.o bitvec.o btmutex.o btree.o build.o \ - callback.o complete.o ctime.o date.o dbstat.o delete.o expr.o \ - fault.o fkey.o \ - fts3.o fts3_aux.o fts3_expr.o fts3_hash.o fts3_icu.o fts3_porter.o \ - fts3_snippet.o fts3_tokenizer.o fts3_tokenizer1.o \ + callback.o carray.o complete.o ctime.o \ + date.o dbpage.o dbstat.o delete.o \ + expr.o fault.o fkey.o \ + fts3.o fts3_aux.o fts3_expr.o fts3_hash.o fts3_icu.o \ + fts3_porter.o fts3_snippet.o fts3_tokenizer.o fts3_tokenizer1.o \ fts3_tokenize_vtab.o \ - fts3_unicode.o fts3_unicode2.o \ - fts3_write.o fts5.o func.o global.o hash.o \ - icu.o insert.o journal.o json1.o legacy.o loadext.o \ + fts3_unicode.o fts3_unicode2.o fts3_write.o \ + fts5.o \ + func.o global.o hash.o \ + icu.o insert.o json.o legacy.o loadext.o \ main.o malloc.o mem0.o mem1.o mem2.o mem3.o mem5.o \ - memjournal.o \ + memdb.o memjournal.o \ mutex.o mutex_noop.o mutex_unix.o mutex_w32.o \ - notify.o opcodes.o os.o os_unix.o os_win.o \ - pager.o pcache.o pcache1.o pragma.o prepare.o printf.o \ - random.o resolve.o rowset.o rtree.o select.o sqlite3rbu.o status.o \ + notify.o opcodes.o os.o os_kv.o os_unix.o os_win.o \ + pager.o parse.o pcache.o pcache1.o pragma.o prepare.o printf.o \ + random.o resolve.o rowset.o rtree.o \ + sqlite3session.o select.o sqlite3rbu.o status.o stmt.o \ table.o threads.o tokenize.o treeview.o trigger.o \ - update.o userauth.o util.o vacuum.o \ - vdbeapi.o vdbeaux.o vdbeblob.o vdbemem.o vdbesort.o \ - vdbetrace.o wal.o walker.o where.o wherecode.o whereexpr.o \ - utf.o vtab.o + update.o upsert.o utf.o util.o vacuum.o \ + vdbe.o vdbeapi.o vdbeaux.o vdbeblob.o vdbemem.o vdbesort.o \ + vdbetrace.o vdbevtab.o vtab.o \ + wal.o walker.o where.o wherecode.o whereexpr.o \ + window.o $(SQLCIPHER_OBJ) +LIBOBJS = $(LIBOBJS0) + +# +# Object files for the amalgamation. +# +LIBOBJS1 = sqlite3.o +# +# Determine the real value of LIBOBJ based on whether the amalgamation +# is enabled or not. +# +LIBOBJ = $(LIBOBJS$(USE_AMALGAMATION)) +$(LIBOBJ): $(MAKE_SANITY_CHECK) +# # All of the source code files. # -SRC = \ +SRC = $(SQLCIPHER_SRC) \ $(TOP)/src/alter.c \ $(TOP)/src/analyze.c \ $(TOP)/src/attach.c \ @@ -90,9 +609,11 @@ SRC = \ $(TOP)/src/btreeInt.h \ $(TOP)/src/build.c \ $(TOP)/src/callback.c \ + $(TOP)/src/carray.c \ $(TOP)/src/complete.c \ - $(TOP)/src/ctime.c \ + ctime.c \ $(TOP)/src/date.c \ + $(TOP)/src/dbpage.c \ $(TOP)/src/dbstat.c \ $(TOP)/src/delete.c \ $(TOP)/src/expr.c \ @@ -104,7 +625,7 @@ SRC = \ $(TOP)/src/hash.h \ $(TOP)/src/hwtime.h \ $(TOP)/src/insert.c \ - $(TOP)/src/journal.c \ + $(TOP)/src/json.c \ $(TOP)/src/legacy.c \ $(TOP)/src/loadext.c \ $(TOP)/src/main.c \ @@ -114,6 +635,7 @@ SRC = \ $(TOP)/src/mem2.c \ $(TOP)/src/mem3.c \ $(TOP)/src/mem5.c \ + $(TOP)/src/memdb.c \ $(TOP)/src/memjournal.c \ $(TOP)/src/msvc.h \ $(TOP)/src/mutex.c \ @@ -126,6 +648,7 @@ SRC = \ $(TOP)/src/os.h \ $(TOP)/src/os_common.h \ $(TOP)/src/os_setup.h \ + $(TOP)/src/os_kv.c \ $(TOP)/src/os_unix.c \ $(TOP)/src/os_win.c \ $(TOP)/src/os_win.h \ @@ -136,7 +659,7 @@ SRC = \ $(TOP)/src/pcache.h \ $(TOP)/src/pcache1.c \ $(TOP)/src/pragma.c \ - $(TOP)/src/pragma.h \ + pragma.h \ $(TOP)/src/prepare.c \ $(TOP)/src/printf.c \ $(TOP)/src/random.c \ @@ -144,7 +667,7 @@ SRC = \ $(TOP)/src/rowset.c \ $(TOP)/src/select.c \ $(TOP)/src/status.c \ - $(TOP)/src/shell.c \ + $(TOP)/src/shell.c.in \ $(TOP)/src/sqlite.h.in \ $(TOP)/src/sqlite3ext.h \ $(TOP)/src/sqliteInt.h \ @@ -157,6 +680,7 @@ SRC = \ $(TOP)/src/trigger.c \ $(TOP)/src/utf.c \ $(TOP)/src/update.c \ + $(TOP)/src/upsert.c \ $(TOP)/src/util.c \ $(TOP)/src/vacuum.c \ $(TOP)/src/vdbe.c \ @@ -167,6 +691,7 @@ SRC = \ $(TOP)/src/vdbemem.c \ $(TOP)/src/vdbesort.c \ $(TOP)/src/vdbetrace.c \ + $(TOP)/src/vdbevtab.c \ $(TOP)/src/vdbeInt.h \ $(TOP)/src/vtab.c \ $(TOP)/src/vxworks.h \ @@ -176,28 +701,11 @@ SRC = \ $(TOP)/src/where.c \ $(TOP)/src/wherecode.c \ $(TOP)/src/whereexpr.c \ - $(TOP)/src/whereInt.h + $(TOP)/src/whereInt.h \ + $(TOP)/src/window.c # Source code for extensions # -SRC += \ - $(TOP)/ext/fts1/fts1.c \ - $(TOP)/ext/fts1/fts1.h \ - $(TOP)/ext/fts1/fts1_hash.c \ - $(TOP)/ext/fts1/fts1_hash.h \ - $(TOP)/ext/fts1/fts1_porter.c \ - $(TOP)/ext/fts1/fts1_tokenizer.h \ - $(TOP)/ext/fts1/fts1_tokenizer1.c -SRC += \ - $(TOP)/ext/fts2/fts2.c \ - $(TOP)/ext/fts2/fts2.h \ - $(TOP)/ext/fts2/fts2_hash.c \ - $(TOP)/ext/fts2/fts2_hash.h \ - $(TOP)/ext/fts2/fts2_icu.c \ - $(TOP)/ext/fts2/fts2_porter.c \ - $(TOP)/ext/fts2/fts2_tokenizer.h \ - $(TOP)/ext/fts2/fts2_tokenizer.c \ - $(TOP)/ext/fts2/fts2_tokenizer1.c SRC += \ $(TOP)/ext/fts3/fts3.c \ $(TOP)/ext/fts3/fts3.h \ @@ -220,41 +728,17 @@ SRC += \ $(TOP)/ext/icu/sqliteicu.h \ $(TOP)/ext/icu/icu.c SRC += \ - $(TOP)/ext/rtree/sqlite3rtree.h \ $(TOP)/ext/rtree/rtree.h \ - $(TOP)/ext/rtree/rtree.c + $(TOP)/ext/rtree/rtree.c \ + $(TOP)/ext/rtree/geopoly.c SRC += \ - $(TOP)/ext/userauth/userauth.c \ - $(TOP)/ext/userauth/sqlite3userauth.h + $(TOP)/ext/session/sqlite3session.c \ + $(TOP)/ext/session/sqlite3session.h SRC += \ - $(TOP)/ext/rbu/sqlite3rbu.c \ - $(TOP)/ext/rbu/sqlite3rbu.h + $(TOP)/ext/rbu/sqlite3rbu.h \ + $(TOP)/ext/rbu/sqlite3rbu.c SRC += \ - $(TOP)/ext/misc/json1.c - - -# FTS5 things -# -FTS5_HDR = \ - $(TOP)/ext/fts5/fts5.h \ - $(TOP)/ext/fts5/fts5Int.h \ - fts5parse.h - -FTS5_SRC = \ - $(TOP)/ext/fts5/fts5_aux.c \ - $(TOP)/ext/fts5/fts5_buffer.c \ - $(TOP)/ext/fts5/fts5_main.c \ - $(TOP)/ext/fts5/fts5_config.c \ - $(TOP)/ext/fts5/fts5_expr.c \ - $(TOP)/ext/fts5/fts5_hash.c \ - $(TOP)/ext/fts5/fts5_index.c \ - fts5parse.c \ - $(TOP)/ext/fts5/fts5_storage.c \ - $(TOP)/ext/fts5/fts5_tokenize.c \ - $(TOP)/ext/fts5/fts5_unicode2.c \ - $(TOP)/ext/fts5/fts5_varint.c \ - $(TOP)/ext/fts5/fts5_vocab.c \ - + $(TOP)/ext/misc/stmt.c # Generated source code files # @@ -264,30 +748,28 @@ SRC += \ opcodes.h \ parse.c \ parse.h \ + sqlite_cfg.h \ + shell.c \ sqlite3.h - # Source code to the test files. # TESTSRC = \ - $(TOP)/ext/fts3/fts3_term.c \ - $(TOP)/ext/fts3/fts3_test.c \ - $(TOP)/ext/rbu/test_rbu.c \ $(TOP)/src/test1.c \ $(TOP)/src/test2.c \ $(TOP)/src/test3.c \ $(TOP)/src/test4.c \ $(TOP)/src/test5.c \ $(TOP)/src/test6.c \ - $(TOP)/src/test7.c \ $(TOP)/src/test8.c \ $(TOP)/src/test9.c \ $(TOP)/src/test_autoext.c \ - $(TOP)/src/test_async.c \ $(TOP)/src/test_backup.c \ + $(TOP)/src/test_bestindex.c \ $(TOP)/src/test_blob.c \ $(TOP)/src/test_btree.c \ $(TOP)/src/test_config.c \ + $(TOP)/src/test_delete.c \ $(TOP)/src/test_demovfs.c \ $(TOP)/src/test_devsym.c \ $(TOP)/src/test_fs.c \ @@ -297,6 +779,7 @@ TESTSRC = \ $(TOP)/src/test_intarray.c \ $(TOP)/src/test_journal.c \ $(TOP)/src/test_malloc.c \ + $(TOP)/src/test_md5.c \ $(TOP)/src/test_multiplex.c \ $(TOP)/src/test_mutex.c \ $(TOP)/src/test_onefile.c \ @@ -305,55 +788,84 @@ TESTSRC = \ $(TOP)/src/test_quota.c \ $(TOP)/src/test_rtree.c \ $(TOP)/src/test_schema.c \ - $(TOP)/src/test_server.c \ - $(TOP)/src/test_sqllog.c \ $(TOP)/src/test_superlock.c \ $(TOP)/src/test_syscall.c \ + $(TOP)/src/test_tclsh.c \ $(TOP)/src/test_tclvar.c \ $(TOP)/src/test_thread.c \ + $(TOP)/src/test_vdbecov.c \ $(TOP)/src/test_vfs.c \ - $(TOP)/src/test_windirent.c \ - $(TOP)/src/test_wsd.c - -# Extensions to be statically loaded. + $(TOP)/src/test_window.c \ + $(TOP)/src/test_wsd.c \ + $(TOP)/ext/fts3/fts3_term.c \ + $(TOP)/ext/fts3/fts3_test.c \ + $(TOP)/ext/session/test_session.c \ + $(TOP)/ext/recover/sqlite3recover.c \ + $(TOP)/ext/recover/dbdata.c \ + $(TOP)/ext/recover/test_recover.c \ + $(TOP)/ext/intck/test_intck.c \ + $(TOP)/ext/intck/sqlite3intck.c \ + $(TOP)/ext/rbu/test_rbu.c + +# Statically linked extensions # TESTSRC += \ + $(TOP)/ext/expert/sqlite3expert.c \ + $(TOP)/ext/expert/test_expert.c \ $(TOP)/ext/misc/amatch.c \ + $(TOP)/ext/misc/appendvfs.c \ + $(TOP)/ext/misc/basexx.c \ + $(TOP)/ext/misc/cksumvfs.c \ $(TOP)/ext/misc/closure.c \ + $(TOP)/ext/misc/csv.c \ + $(TOP)/ext/misc/decimal.c \ $(TOP)/ext/misc/eval.c \ + $(TOP)/ext/misc/explain.c \ $(TOP)/ext/misc/fileio.c \ $(TOP)/ext/misc/fuzzer.c \ + $(TOP)/ext/fts5/fts5_tcl.c \ + $(TOP)/ext/fts5/fts5_test_mi.c \ + $(TOP)/ext/fts5/fts5_test_tok.c \ $(TOP)/ext/misc/ieee754.c \ + $(TOP)/ext/misc/mmapwarm.c \ $(TOP)/ext/misc/nextchar.c \ - $(TOP)/ext/misc/percentile.c \ + $(TOP)/ext/misc/normalize.c \ + $(TOP)/ext/misc/prefixes.c \ + $(TOP)/ext/misc/qpvtab.c \ + $(TOP)/ext/misc/randomjson.c \ $(TOP)/ext/misc/regexp.c \ + $(TOP)/ext/misc/remember.c \ $(TOP)/ext/misc/series.c \ $(TOP)/ext/misc/spellfix.c \ + $(TOP)/ext/misc/stmtrand.c \ $(TOP)/ext/misc/totype.c \ + $(TOP)/ext/misc/unionvtab.c \ $(TOP)/ext/misc/wholenumber.c \ - $(TOP)/ext/misc/vfslog.c \ - $(TOP)/ext/fts5/fts5_tcl.c \ - $(TOP)/ext/fts5/fts5_test_mi.c \ - $(TOP)/ext/fts5/fts5_test_tok.c - - -#TESTSRC += $(TOP)/ext/fts2/fts2_tokenizer.c -#TESTSRC += $(TOP)/ext/fts3/fts3_tokenizer.c + $(TOP)/ext/misc/zipfile.c \ + $(TOP)/ext/rtree/test_rtreedoc.c +# Source code to the library files needed by the test fixture +# TESTSRC2 = \ $(TOP)/src/attach.c \ $(TOP)/src/backup.c \ + $(TOP)/src/bitvec.c \ $(TOP)/src/btree.c \ $(TOP)/src/build.c \ + $(TOP)/src/carray.c \ + ctime.c \ $(TOP)/src/date.c \ + $(TOP)/src/dbpage.c \ $(TOP)/src/dbstat.c \ $(TOP)/src/expr.c \ $(TOP)/src/func.c \ + $(TOP)/src/global.c \ $(TOP)/src/insert.c \ $(TOP)/src/wal.c \ $(TOP)/src/main.c \ $(TOP)/src/mem5.c \ $(TOP)/src/os.c \ + $(TOP)/src/os_kv.c \ $(TOP)/src/os_unix.c \ $(TOP)/src/os_win.c \ $(TOP)/src/pager.c \ @@ -364,24 +876,29 @@ TESTSRC2 = \ $(TOP)/src/pcache.c \ $(TOP)/src/pcache1.c \ $(TOP)/src/select.c \ - $(TOP)/src/threads.c \ $(TOP)/src/tokenize.c \ + $(TOP)/src/treeview.c \ $(TOP)/src/utf.c \ $(TOP)/src/util.c \ $(TOP)/src/vdbeapi.c \ $(TOP)/src/vdbeaux.c \ $(TOP)/src/vdbe.c \ $(TOP)/src/vdbemem.c \ + $(TOP)/src/vdbetrace.c \ + $(TOP)/src/vdbevtab.c \ $(TOP)/src/where.c \ $(TOP)/src/wherecode.c \ $(TOP)/src/whereexpr.c \ + $(TOP)/src/window.c \ parse.c \ $(TOP)/ext/fts3/fts3.c \ $(TOP)/ext/fts3/fts3_aux.c \ $(TOP)/ext/fts3/fts3_expr.c \ $(TOP)/ext/fts3/fts3_tokenizer.c \ $(TOP)/ext/fts3/fts3_write.c \ - $(TOP)/ext/async/sqlite3async.c + $(TOP)/ext/session/sqlite3session.c \ + $(TOP)/ext/misc/stmt.c \ + fts5.c # Header files used by all library source files. # @@ -401,7 +918,7 @@ HDR = \ $(TOP)/src/pager.h \ $(TOP)/src/pcache.h \ parse.h \ - $(TOP)/src/pragma.h \ + pragma.h \ sqlite3.h \ $(TOP)/src/sqlite3ext.h \ $(TOP)/src/sqliteInt.h \ @@ -409,41 +926,35 @@ HDR = \ $(TOP)/src/vdbe.h \ $(TOP)/src/vdbeInt.h \ $(TOP)/src/vxworks.h \ - $(TOP)/src/whereInt.h + $(TOP)/src/whereInt.h \ + sqlite_cfg.h +# Reminder: sqlite_cfg.h is typically created by the configure script # Header files used by extensions # -EXTHDR += \ - $(TOP)/ext/fts1/fts1.h \ - $(TOP)/ext/fts1/fts1_hash.h \ - $(TOP)/ext/fts1/fts1_tokenizer.h -EXTHDR += \ - $(TOP)/ext/fts2/fts2.h \ - $(TOP)/ext/fts2/fts2_hash.h \ - $(TOP)/ext/fts2/fts2_tokenizer.h EXTHDR += \ $(TOP)/ext/fts3/fts3.h \ $(TOP)/ext/fts3/fts3Int.h \ $(TOP)/ext/fts3/fts3_hash.h \ $(TOP)/ext/fts3/fts3_tokenizer.h EXTHDR += \ - $(TOP)/ext/rtree/rtree.h + $(TOP)/ext/rtree/rtree.h \ + $(TOP)/ext/rtree/geopoly.c EXTHDR += \ $(TOP)/ext/icu/sqliteicu.h EXTHDR += \ - $(TOP)/ext/fts5/fts5Int.h \ - fts5parse.h \ - $(TOP)/ext/fts5/fts5.h -EXTHDR += \ - $(TOP)/ext/userauth/sqlite3userauth.h + $(TOP)/ext/rtree/sqlite3rtree.h -# executables needed for testing +# +# Executables needed for testing # TESTPROGS = \ - testfixture$(EXE) \ - sqlite3$(EXE) \ - sqlite3_analyzer$(EXE) \ - sqldiff$(EXE) + testfixture$(T.exe) \ + sqlite3$(T.exe) \ + sqlite3_analyzer$(T.exe) \ + sqldiff$(T.exe) \ + dbhash$(T.exe) \ + sqltclsh$(T.exe) # Databases containing fuzzer test cases # @@ -451,119 +962,228 @@ FUZZDATA = \ $(TOP)/test/fuzzdata1.db \ $(TOP)/test/fuzzdata2.db \ $(TOP)/test/fuzzdata3.db \ - $(TOP)/test/fuzzdata4.db + $(TOP)/test/fuzzdata4.db \ + $(TOP)/test/fuzzdata5.db \ + $(TOP)/test/fuzzdata6.db \ + $(TOP)/test/fuzzdata7.db \ + $(TOP)/test/fuzzdata8.db +# # Standard options to testfixture # TESTOPTS = --verbose=file --output=test-out.txt +# # Extra compiler options for various shell tools # -SHELL_OPT = -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_FTS5 -FUZZERSHELL_OPT = -DSQLITE_ENABLE_JSON1 -FUZZCHECK_OPT = -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_MEMSYS5 +# Note that some of these will only apply when embedding sqlite3.c +# into the shell, as these flags are not otherwise passed on to the +# library. +SHELL_OPT += -DSQLITE_DQS=0 +SHELL_OPT += -DSQLITE_ENABLE_FTS4 +#SHELL_OPT += -DSQLITE_ENABLE_FTS5 +SHELL_OPT += -DSQLITE_ENABLE_RTREE +SHELL_OPT += -DSQLITE_ENABLE_EXPLAIN_COMMENTS +SHELL_OPT += -DSQLITE_ENABLE_UNKNOWN_SQL_FUNCTION +SHELL_OPT += -DSQLITE_ENABLE_STMTVTAB +SHELL_OPT += -DSQLITE_ENABLE_DBPAGE_VTAB +SHELL_OPT += -DSQLITE_ENABLE_DBSTAT_VTAB +SHELL_OPT += -DSQLITE_ENABLE_BYTECODE_VTAB +SHELL_OPT += -DSQLITE_ENABLE_OFFSET_SQL_FUNC +SHELL_OPT += -DSQLITE_ENABLE_PERCENTILE +SHELL_OPT += -DSQLITE_STRICT_SUBTYPE=1 +FUZZERSHELL_OPT = +FUZZCHECK_OPT += -I$(TOP)/test +FUZZCHECK_OPT += -I$(TOP)/ext/recover +FUZZCHECK_OPT += \ + -DSQLITE_OSS_FUZZ \ + -DSQLITE_ENABLE_BYTECODE_VTAB \ + -DSQLITE_ENABLE_CARRAY \ + -DSQLITE_ENABLE_DBPAGE_VTAB \ + -DSQLITE_ENABLE_DBSTAT_VTAB \ + -DSQLITE_ENABLE_DESERIALIZE \ + -DSQLITE_ENABLE_EXPLAIN_COMMENTS \ + -DSQLITE_ENABLE_FTS3_PARENTHESIS \ + -DSQLITE_ENABLE_FTS4 \ + -DSQLITE_ENABLE_FTS5 \ + -DSQLITE_ENABLE_GEOPOLY \ + -DSQLITE_ENABLE_MATH_FUNCTIONS \ + -DSQLITE_ENABLE_MEMSYS5 \ + -DSQLITE_ENABLE_NORMALIZE \ + -DSQLITE_ENABLE_OFFSET_SQL_FUNC \ + -DSQLITE_ENABLE_PERCENTILE \ + -DSQLITE_ENABLE_PREUPDATE_HOOK \ + -DSQLITE_ENABLE_RTREE \ + -DSQLITE_ENABLE_SESSION \ + -DSQLITE_ENABLE_STMTVTAB \ + -DSQLITE_ENABLE_UNKNOWN_SQL_FUNCTION \ + -DSQLITE_ENABLE_STAT4 \ + -DSQLITE_ENABLE_STMT_SCANSTATUS \ + -DSQLITE_JSON_MAX_DEPTH=500 \ + -DSQLITE_MAX_MEMORY=50000000 \ + -DSQLITE_MAX_MMAP_SIZE=0 \ + -DSQLITE_OMIT_LOAD_EXTENSION \ + -DSQLITE_PRINTF_PRECISION_LIMIT=1000 \ + -DSQLITE_PRIVATE="" \ + -DSQLITE_STRICT_SUBTYPE=1 \ + -DSQLITE_STATIC_RANDOMJSON + +FUZZCHECK_SRC += $(TOP)/test/fuzzcheck.c +FUZZCHECK_SRC += $(TOP)/test/ossfuzz.c +FUZZCHECK_SRC += $(TOP)/test/fuzzinvariants.c +FUZZCHECK_SRC += $(TOP)/ext/recover/dbdata.c +FUZZCHECK_SRC += $(TOP)/ext/recover/sqlite3recover.c +FUZZCHECK_SRC += $(TOP)/test/vt02.c +FUZZCHECK_SRC += $(TOP)/ext/misc/randomjson.c +DBFUZZ_OPT = +ST_OPT = -DSQLITE_OS_KV_OPTIONAL + +$(TCLSH_CMD): +has_tclsh84: + sh $(TOP)/tool/cktclsh.sh 8.4 $(TCLSH_CMD) + touch has_tclsh84 + +has_tclsh85: + sh $(TOP)/tool/cktclsh.sh 8.5 $(TCLSH_CMD) + touch has_tclsh85 -# This is the default Makefile target. The objects listed here -# are what get build when you type just "make" with no arguments. # -all: sqlite3.h libsqlite3.a sqlite3$(EXE) - -libsqlite3.a: $(LIBOBJ) - $(AR) libsqlite3.a $(LIBOBJ) - $(RANLIB) libsqlite3.a - -sqlite3$(EXE): $(TOP)/src/shell.c libsqlite3.a sqlite3.h - $(TCCX) $(READLINE_FLAGS) -o sqlite3$(EXE) $(SHELL_OPT) \ - $(TOP)/src/shell.c libsqlite3.a $(LIBREADLINE) $(TLIBS) $(THREADLIB) - -sqldiff$(EXE): $(TOP)/tool/sqldiff.c sqlite3.c sqlite3.h - $(TCCX) -o sqldiff$(EXE) -DSQLITE_THREADSAFE=0 \ - $(TOP)/tool/sqldiff.c sqlite3.c $(TLIBS) $(THREADLIB) - -srcck1$(EXE): $(TOP)/tool/srcck1.c - $(BCC) -o srcck1$(EXE) $(TOP)/tool/srcck1.c - -sourcetest: srcck1$(EXE) sqlite3.c - ./srcck1 sqlite3.c - -fuzzershell$(EXE): $(TOP)/tool/fuzzershell.c sqlite3.c sqlite3.h - $(TCCX) -o fuzzershell$(EXE) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION \ - $(FUZZERSHELL_OPT) $(TOP)/tool/fuzzershell.c sqlite3.c \ - $(TLIBS) $(THREADLIB) - -fuzzcheck$(EXE): $(TOP)/test/fuzzcheck.c sqlite3.c sqlite3.h - $(TCCX) -o fuzzcheck$(EXE) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION \ - -DSQLITE_ENABLE_MEMSYS5 $(FUZZCHECK_OPT) \ - $(TOP)/test/fuzzcheck.c sqlite3.c $(TLIBS) $(THREADLIB) - -mptester$(EXE): sqlite3.c $(TOP)/mptest/mptest.c - $(TCCX) -o $@ -I. $(TOP)/mptest/mptest.c sqlite3.c \ - $(TLIBS) $(THREADLIB) +# $(T.tcl.env.sh) is a shell script intended for source'ing to set +# various TCL config info in the current shell context: +# +# - All info exported by tclConfig.sh +# +# - TCLLIBDIR = the first entry from TCL's $auto_path which refers to +# an existing dir, then append /sqlite3 to it. If TCLLIBDIR is +# provided via the environment, that value is used instead. +# +# Maintenance reminder: the ./ at the start of the name is required or /bin/sh +# refuses to source it: +# +# . .tclenv.sh ==> .tclenv.sh: not found +# . ./.tclenv.sh ==> fine +# +# It took half an hour to figure that out. +# +T.tcl.env.sh = ./.tclenv.sh +$(T.tcl.env.sh): $(TCLSH_CMD) $(TCL_CONFIG_SH) $(MAKEFILE_LIST) + @if [ x = "x$(TCL_CONFIG_SH)" ]; then \ + echo 'TCL_CONFIG_SH must be set to point to a "tclConfig.sh"' 1>&2; exit 1; \ + fi; \ + if [ x != "x$(TCLLIBDIR)" ]; then \ + echo "# generated by main.mk"; \ + echo TCLLIBDIR="$(TCLLIBDIR)"; \ + else \ + ld= ; \ + for d in `echo "puts stdout \\$$auto_path" | $(TCLSH_CMD)`; do \ + if [ -d "$$d" ]; then ld=$$d; break; fi; \ + done; \ + if [ x = "x$$ld" ]; then echo "Cannot determine TCLLIBDIR" 1>&2; exit 1; fi; \ + echo "# generated by main.mk"; \ + echo "TCLLIBDIR=$$ld/sqlite3$(PACKAGE_VERSION)"; \ + fi > $@; \ + echo ". \"$(TCL_CONFIG_SH)\" || exit \$$?" >> $@; \ + echo "Created $@" -MPTEST1=./mptester$(EXE) mptest1.db $(TOP)/mptest/crash01.test --repeat 20 -MPTEST2=./mptester$(EXE) mptest2.db $(TOP)/mptest/multiwrite01.test --repeat 20 -mptest: mptester$(EXE) - $(MPTEST1) --journalmode DELETE - $(MPTEST2) --journalmode WAL - $(MPTEST1) --journalmode WAL - $(MPTEST2) --journalmode PERSIST - $(MPTEST1) --journalmode PERSIST - $(MPTEST2) --journalmode TRUNCATE - $(MPTEST1) --journalmode TRUNCATE - $(MPTEST2) --journalmode DELETE +# +# $(T.tcl.env.source) is shell code to be run as part of any +# compilation or link step which requires vars from +# $(TCL_CONFIG_SH). All targets which use this should also have a +# dependency on $(T.tcl.env.sh). +# +T.tcl.env.source = . $(T.tcl.env.sh) || exit $$? -sqlite3.o: sqlite3.c - $(TCCX) -I. -c sqlite3.c +# +# $(T.compile.tcl) and $(T.link.tcl) are TCL-specific counterparts for $(T.compile) +# and $(T.link) which first invoke $(T.tcl.env.source). Any targets which used them +# must have a dependency on $(T.tcl.env.sh) +# +T.compile.tcl = $(T.tcl.env.source); $(T.compile) $(CFLAGS.intree_includes) +T.link.tcl = $(T.tcl.env.source); $(T.link) +# # This target creates a directory named "tsrc" and fills it with # copies of all of the C source code and header files needed to # build on the target system. Some of the C source code and header # files are automatically generated. This target takes care of # all that automatic generation. # -target_source: $(SRC) $(TOP)/tool/vdbe-compress.tcl fts5.c +.target_source: $(MAKE_SANITY_CHECK) $(SRC) $(TOP)/tool/vdbe-compress.tcl \ + fts5.c $(B.tclsh) rm -rf tsrc mkdir tsrc cp -f $(SRC) tsrc - rm tsrc/sqlite.h.in tsrc/parse.y - tclsh $(TOP)/tool/vdbe-compress.tcl $(OPTS) vdbe.new - mv vdbe.new tsrc/vdbe.c + rm -f tsrc/sqlite.h.in tsrc/parse.y + $(B.tclsh) $(TOP)/tool/vdbe-compress.tcl $(OPTS) vdbe.new + mv -f vdbe.new tsrc/vdbe.c cp fts5.c fts5.h tsrc - touch target_source + touch .target_source -sqlite3.c: target_source $(TOP)/tool/mksqlite3c.tcl - tclsh $(TOP)/tool/mksqlite3c.tcl - cp tsrc/shell.c tsrc/sqlite3ext.h . - echo '#ifndef USE_SYSTEM_SQLITE' >tclsqlite3.c - cat sqlite3.c >>tclsqlite3.c - echo '#endif /* USE_SYSTEM_SQLITE */' >>tclsqlite3.c - cat $(TOP)/src/tclsqlite.c >>tclsqlite3.c +# +# libsqlite3.DLL.basename = the base name of the resulting DLL. This +# is typically libsqlite3 but varies wildly on Unix-like Windows +# environments (msys, cygwin, and friends). Conversely, the base name +# of the static library ($(libsqlite3.LIB)) is constant on all tested +# platforms. +# +libsqlite3.DLL.basename ?= libsqlite3 +# +# libsqlite3.DLL => the DLL library +# +libsqlite3.DLL = $(libsqlite3.DLL.basename)$(T.dll) +# +# libsqlite3.out.implib => "import library" file generated by the +# --out-implib linker flag. Not commonly used on Unix systems but is +# on the Windows-side Unix-esque environments and typically as a value +# of "libsqlite3.dll.a". It is expected to match the filename, if any, +# provided by the -Wl,--out-implib,FILENAME flag. +# +libsqlite3.out.implib ?= +# +# libsqlite3.LIB => the static library +# +libsqlite3.LIB = libsqlite3$(T.lib) -sqlite3ext.h: target_source - cp tsrc/sqlite3ext.h . +# +# libsqlite3.DLL.install-rules => the suffix of the symoblic name of +# the makefile rules for installing the DLL. Must be one of +# (unix-generic, msys, mingw, cygwin, darwin) and a target named +# install-dll-THATNAME is responsible for DLL installation. +libsqlite3.DLL.install-rules ?= unix-generic -sqlite3.c-debug: target_source $(TOP)/tool/mksqlite3c.tcl - tclsh $(TOP)/tool/mksqlite3c.tcl --linemacros - echo '#ifndef USE_SYSTEM_SQLITE' >tclsqlite3.c - cat sqlite3.c >>tclsqlite3.c - echo '#endif /* USE_SYSTEM_SQLITE */' >>tclsqlite3.c - echo '#line 1 "tclsqlite.c"' >>tclsqlite3.c - cat $(TOP)/src/tclsqlite.c >>tclsqlite3.c +# Rules to build the LEMON compiler generator +# +lemon$(B.exe): $(MAKE_SANITY_CHECK) $(TOP)/tool/lemon.c $(TOP)/tool/lempar.c + $(B.cc) -o $@ $(TOP)/tool/lemon.c + cp $(TOP)/tool/lempar.c . -sqlite3-all.c: sqlite3.c $(TOP)/tool/split-sqlite3c.tcl - tclsh $(TOP)/tool/split-sqlite3c.tcl +# Rules to build the program that generates the source-id +# +mksourceid$(B.exe): $(MAKE_SANITY_CHECK) $(TOP)/tool/mksourceid.c + $(B.cc) -o $@ $(TOP)/tool/mksourceid.c -fts2amal.c: target_source $(TOP)/ext/fts2/mkfts2amal.tcl - tclsh $(TOP)/ext/fts2/mkfts2amal.tcl +sqlite3.h: $(MAKE_SANITY_CHECK) $(TOP)/src/sqlite.h.in \ + $(TOP)/manifest mksourceid$(B.exe) \ + $(TOP)/VERSION $(B.tclsh) + $(B.tclsh) $(TOP)/tool/mksqlite3h.tcl $(TOP) -o sqlite3.h -fts3amal.c: target_source $(TOP)/ext/fts3/mkfts3amal.tcl - tclsh $(TOP)/ext/fts3/mkfts3amal.tcl +sqlite3.c: .target_source sqlite3.h $(TOP)/tool/mksqlite3c.tcl src-verify$(B.exe) \ + $(B.tclsh) $(EXTRA_SRC) + $(B.tclsh) $(TOP)/tool/mksqlite3c.tcl $(AMALGAMATION_GEN_FLAGS) $(EXTRA_SRC) + cp tsrc/sqlite3ext.h . + cp $(TOP)/ext/session/sqlite3session.h . -# Rules to build the LEMON compiler generator -# -lemon: $(TOP)/tool/lemon.c $(TOP)/tool/lempar.c - $(BCC) -o lemon $(TOP)/tool/lemon.c - cp $(TOP)/tool/lempar.c . +sqlite3r.h: sqlite3.h $(B.tclsh) + $(B.tclsh) $(TOP)/tool/mksqlite3h.tcl $(TOP) --enable-recover -o sqlite3r.h + +sqlite3r.c: sqlite3.c sqlite3r.h $(B.tclsh) + cp $(TOP)/ext/recover/sqlite3recover.c tsrc/ + cp $(TOP)/ext/recover/sqlite3recover.h tsrc/ + cp $(TOP)/ext/recover/dbdata.c tsrc/ + $(B.tclsh) $(TOP)/tool/mksqlite3c.tcl --enable-recover $(AMALGAMATION_GEN_FLAGS) $(EXTRA_SRC) + +sqlite3ext.h: .target_source + cp tsrc/sqlite3ext.h . # Rules to build individual *.o files from generated *.c files. This # applies to: @@ -571,229 +1191,971 @@ lemon: $(TOP)/tool/lemon.c $(TOP)/tool/lempar.c # parse.o # opcodes.o # -%.o: %.c $(HDR) - $(TCCX) -c $< +DEPS_OBJ_COMMON = $(MAKE_SANITY_CHECK) $(HDR) +parse.o: parse.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c parse.c + +opcodes.o: opcodes.c + $(T.cc.sqlite) -c opcodes.c # Rules to build individual *.o files from files in the src directory. # -%.o: $(TOP)/src/%.c $(HDR) - $(TCCX) -c $< +alter.o: $(TOP)/src/alter.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/alter.c -tclsqlite.o: $(TOP)/src/tclsqlite.c $(HDR) - $(TCCX) $(TCL_FLAGS) -c $(TOP)/src/tclsqlite.c +analyze.o: $(TOP)/src/analyze.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/analyze.c +attach.o: $(TOP)/src/attach.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/attach.c +auth.o: $(TOP)/src/auth.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/auth.c -# Rules to build opcodes.c and opcodes.h -# -opcodes.c: opcodes.h $(TOP)/tool/mkopcodec.tcl - tclsh $(TOP)/tool/mkopcodec.tcl opcodes.h >opcodes.c +backup.o: $(TOP)/src/backup.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/backup.c -opcodes.h: parse.h $(TOP)/src/vdbe.c $(TOP)/tool/mkopcodeh.tcl - cat parse.h $(TOP)/src/vdbe.c | \ - tclsh $(TOP)/tool/mkopcodeh.tcl >opcodes.h +bitvec.o: $(TOP)/src/bitvec.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/bitvec.c -# Rules to build parse.c and parse.h - the outputs of lemon. -# -parse.h: parse.c +btmutex.o: $(TOP)/src/btmutex.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/btmutex.c -parse.c: $(TOP)/src/parse.y lemon $(TOP)/tool/addopcodes.tcl - cp $(TOP)/src/parse.y . - rm -f parse.h - ./lemon -s $(OPTS) parse.y - mv parse.h parse.h.temp - tclsh $(TOP)/tool/addopcodes.tcl parse.h.temp >parse.h +btree.o: $(TOP)/src/btree.c $(DEPS_OBJ_COMMON) $(TOP)/src/pager.h + $(T.cc.sqlite) -c $(TOP)/src/btree.c -sqlite3.h: $(TOP)/src/sqlite.h.in $(TOP)/manifest.uuid $(TOP)/VERSION $(TOP)/ext/rtree/sqlite3rtree.h - tclsh $(TOP)/tool/mksqlite3h.tcl $(TOP) >sqlite3.h +build.o: $(TOP)/src/build.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/build.c -keywordhash.h: $(TOP)/tool/mkkeywordhash.c - $(BCC) -o mkkeywordhash $(OPTS) $(TOP)/tool/mkkeywordhash.c - ./mkkeywordhash >keywordhash.h +callback.o: $(TOP)/src/callback.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/callback.c +carray.o: $(TOP)/src/carray.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/carray.c +complete.o: $(TOP)/src/complete.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/complete.c -# Rules to build the extension objects. -# -icu.o: $(TOP)/ext/icu/icu.c $(HDR) $(EXTHDR) - $(TCCX) -DSQLITE_CORE -c $(TOP)/ext/icu/icu.c +ctime.c: $(TOP)/tool/mkctimec.tcl $(B.tclsh) + $(B.tclsh) $(TOP)/tool/mkctimec.tcl + +ctime.o: ctime.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c ctime.c + +date.o: $(TOP)/src/date.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/date.c + +dbpage.o: $(TOP)/src/dbpage.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/dbpage.c + +dbstat.o: $(TOP)/src/dbstat.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/dbstat.c + +delete.o: $(TOP)/src/delete.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/delete.c + +expr.o: $(TOP)/src/expr.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/expr.c + +fault.o: $(TOP)/src/fault.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/fault.c + +fkey.o: $(TOP)/src/fkey.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/fkey.c + +func.o: $(TOP)/src/func.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/func.c + +global.o: $(TOP)/src/global.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/global.c + +hash.o: $(TOP)/src/hash.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/hash.c + +insert.o: $(TOP)/src/insert.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/insert.c + +json.o: $(TOP)/src/json.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/json.c + +legacy.o: $(TOP)/src/legacy.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/legacy.c + +loadext.o: $(TOP)/src/loadext.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/loadext.c + +main.o: $(TOP)/src/main.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/main.c + +malloc.o: $(TOP)/src/malloc.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/malloc.c + +mem0.o: $(TOP)/src/mem0.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/mem0.c + +mem1.o: $(TOP)/src/mem1.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/mem1.c + +mem2.o: $(TOP)/src/mem2.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/mem2.c + +mem3.o: $(TOP)/src/mem3.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/mem3.c + +mem5.o: $(TOP)/src/mem5.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/mem5.c + +memdb.o: $(TOP)/src/memdb.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/memdb.c + +memjournal.o: $(TOP)/src/memjournal.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/memjournal.c + +mutex.o: $(TOP)/src/mutex.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/mutex.c + +mutex_noop.o: $(TOP)/src/mutex_noop.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/mutex_noop.c + +mutex_unix.o: $(TOP)/src/mutex_unix.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/mutex_unix.c + +mutex_w32.o: $(TOP)/src/mutex_w32.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/mutex_w32.c -fts2.o: $(TOP)/ext/fts2/fts2.c $(HDR) $(EXTHDR) - $(TCCX) -DSQLITE_CORE -c $(TOP)/ext/fts2/fts2.c +notify.o: $(TOP)/src/notify.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/notify.c -fts2_hash.o: $(TOP)/ext/fts2/fts2_hash.c $(HDR) $(EXTHDR) - $(TCCX) -DSQLITE_CORE -c $(TOP)/ext/fts2/fts2_hash.c +pager.o: $(TOP)/src/pager.c $(DEPS_OBJ_COMMON) $(TOP)/src/pager.h + $(T.cc.sqlite) -c $(TOP)/src/pager.c -fts2_icu.o: $(TOP)/ext/fts2/fts2_icu.c $(HDR) $(EXTHDR) - $(TCCX) -DSQLITE_CORE -c $(TOP)/ext/fts2/fts2_icu.c +pcache.o: $(TOP)/src/pcache.c $(DEPS_OBJ_COMMON) $(TOP)/src/pcache.h + $(T.cc.sqlite) -c $(TOP)/src/pcache.c -fts2_porter.o: $(TOP)/ext/fts2/fts2_porter.c $(HDR) $(EXTHDR) - $(TCCX) -DSQLITE_CORE -c $(TOP)/ext/fts2/fts2_porter.c +pcache1.o: $(TOP)/src/pcache1.c $(DEPS_OBJ_COMMON) $(TOP)/src/pcache.h + $(T.cc.sqlite) -c $(TOP)/src/pcache1.c -fts2_tokenizer.o: $(TOP)/ext/fts2/fts2_tokenizer.c $(HDR) $(EXTHDR) - $(TCCX) -DSQLITE_CORE -c $(TOP)/ext/fts2/fts2_tokenizer.c +os.o: $(TOP)/src/os.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/os.c -fts2_tokenizer1.o: $(TOP)/ext/fts2/fts2_tokenizer1.c $(HDR) $(EXTHDR) - $(TCCX) -DSQLITE_CORE -c $(TOP)/ext/fts2/fts2_tokenizer1.c +os_kv.o: $(TOP)/src/os_kv.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/os_kv.c -fts3.o: $(TOP)/ext/fts3/fts3.c $(HDR) $(EXTHDR) - $(TCCX) -DSQLITE_CORE -c $(TOP)/ext/fts3/fts3.c +os_unix.o: $(TOP)/src/os_unix.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/os_unix.c -fts3_aux.o: $(TOP)/ext/fts3/fts3_aux.c $(HDR) $(EXTHDR) - $(TCCX) -DSQLITE_CORE -c $(TOP)/ext/fts3/fts3_aux.c +os_win.o: $(TOP)/src/os_win.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/os_win.c -fts3_expr.o: $(TOP)/ext/fts3/fts3_expr.c $(HDR) $(EXTHDR) - $(TCCX) -DSQLITE_CORE -c $(TOP)/ext/fts3/fts3_expr.c +pragma.o: $(TOP)/src/pragma.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/pragma.c -fts3_hash.o: $(TOP)/ext/fts3/fts3_hash.c $(HDR) $(EXTHDR) - $(TCCX) -DSQLITE_CORE -c $(TOP)/ext/fts3/fts3_hash.c +prepare.o: $(TOP)/src/prepare.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/prepare.c -fts3_icu.o: $(TOP)/ext/fts3/fts3_icu.c $(HDR) $(EXTHDR) - $(TCCX) -DSQLITE_CORE -c $(TOP)/ext/fts3/fts3_icu.c +printf.o: $(TOP)/src/printf.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/printf.c -fts3_snippet.o: $(TOP)/ext/fts3/fts3_snippet.c $(HDR) $(EXTHDR) - $(TCCX) -DSQLITE_CORE -c $(TOP)/ext/fts3/fts3_snippet.c +random.o: $(TOP)/src/random.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/random.c -fts3_porter.o: $(TOP)/ext/fts3/fts3_porter.c $(HDR) $(EXTHDR) - $(TCCX) -DSQLITE_CORE -c $(TOP)/ext/fts3/fts3_porter.c +resolve.o: $(TOP)/src/resolve.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/resolve.c -fts3_tokenizer.o: $(TOP)/ext/fts3/fts3_tokenizer.c $(HDR) $(EXTHDR) - $(TCCX) -DSQLITE_CORE -c $(TOP)/ext/fts3/fts3_tokenizer.c +rowset.o: $(TOP)/src/rowset.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/rowset.c -fts3_tokenizer1.o: $(TOP)/ext/fts3/fts3_tokenizer1.c $(HDR) $(EXTHDR) - $(TCCX) -DSQLITE_CORE -c $(TOP)/ext/fts3/fts3_tokenizer1.c +select.o: $(TOP)/src/select.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/select.c -fts3_tokenize_vtab.o: $(TOP)/ext/fts3/fts3_tokenize_vtab.c $(HDR) $(EXTHDR) - $(TCCX) -DSQLITE_CORE -c $(TOP)/ext/fts3/fts3_tokenize_vtab.c +status.o: $(TOP)/src/status.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/status.c -fts3_unicode.o: $(TOP)/ext/fts3/fts3_unicode.c $(HDR) $(EXTHDR) - $(TCCX) -DSQLITE_CORE -c $(TOP)/ext/fts3/fts3_unicode.c +sqlite3.o: sqlite3.h sqlite3.c + $(T.cc.sqlite) -c sqlite3.c -fts3_unicode2.o: $(TOP)/ext/fts3/fts3_unicode2.c $(HDR) $(EXTHDR) - $(TCCX) -DSQLITE_CORE -c $(TOP)/ext/fts3/fts3_unicode2.c +table.o: $(TOP)/src/table.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/table.c -fts3_write.o: $(TOP)/ext/fts3/fts3_write.c $(HDR) $(EXTHDR) - $(TCCX) -DSQLITE_CORE -c $(TOP)/ext/fts3/fts3_write.c +threads.o: $(TOP)/src/threads.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/threads.c -fts5.o: fts5.c - $(TCCX) -DSQLITE_CORE -c fts5.c +tokenize.o: $(TOP)/src/tokenize.c keywordhash.h $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/tokenize.c -json1.o: $(TOP)/ext/misc/json1.c - $(TCCX) -DSQLITE_CORE -c $(TOP)/ext/misc/json1.c +treeview.o: $(TOP)/src/treeview.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/treeview.c -rtree.o: $(TOP)/ext/rtree/rtree.c $(HDR) $(EXTHDR) - $(TCCX) -DSQLITE_CORE -c $(TOP)/ext/rtree/rtree.c +trigger.o: $(TOP)/src/trigger.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/trigger.c +update.o: $(TOP)/src/update.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/update.c +upsert.o: $(TOP)/src/upsert.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/upsert.c -fts5parse.c: $(TOP)/ext/fts5/fts5parse.y lemon +utf.o: $(TOP)/src/utf.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/utf.c + +util.o: $(TOP)/src/util.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/util.c + +vacuum.o: $(TOP)/src/vacuum.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/vacuum.c + +vdbe.o: $(TOP)/src/vdbe.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/vdbe.c + +vdbeapi.o: $(TOP)/src/vdbeapi.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/vdbeapi.c + +vdbeaux.o: $(TOP)/src/vdbeaux.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/vdbeaux.c + +vdbeblob.o: $(TOP)/src/vdbeblob.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/vdbeblob.c + +vdbemem.o: $(TOP)/src/vdbemem.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/vdbemem.c + +vdbesort.o: $(TOP)/src/vdbesort.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/vdbesort.c + +vdbetrace.o: $(TOP)/src/vdbetrace.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/vdbetrace.c + +vdbevtab.o: $(TOP)/src/vdbevtab.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/vdbevtab.c + +vtab.o: $(TOP)/src/vtab.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/vtab.c + +wal.o: $(TOP)/src/wal.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/wal.c + +walker.o: $(TOP)/src/walker.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/walker.c + +where.o: $(TOP)/src/where.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/where.c + +wherecode.o: $(TOP)/src/wherecode.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/wherecode.c + +whereexpr.o: $(TOP)/src/whereexpr.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/whereexpr.c + +window.o: $(TOP)/src/window.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) -c $(TOP)/src/window.c + +tclsqlite.o: $(T.tcl.env.sh) $(TOP)/src/tclsqlite.c $(DEPS_OBJ_COMMON) + $(T.compile.tcl) -DUSE_TCL_STUBS=1 $$TCL_INCLUDE_SPEC \ + -c $(TOP)/src/tclsqlite.c + +tclsqlite-shell.o: $(T.tcl.env.sh) $(TOP)/src/tclsqlite.c $(DEPS_OBJ_COMMON) + $(T.compile.tcl) -DTCLSH -o $@ -c $(TOP)/src/tclsqlite.c $$TCL_INCLUDE_SPEC + +tclsqlite-stubs.o: $(T.tcl.env.sh) $(TOP)/src/tclsqlite.c $(DEPS_OBJ_COMMON) + $(T.compile.tcl) -DUSE_TCL_STUBS=1 -o $@ -c $(TOP)/src/tclsqlite.c $$TCL_INCLUDE_SPEC + +# +# STATIC_TCLSQLITE3 = 1 to statically link tclsqlite3, else +# 0. Requires static versions of all requisite libraries. Primarily +# intended for use with static-friendly environments like Alpine +# Linux. It won't work on glibc-based systems. +# +STATIC_TCLSQLITE3 ?= 0 +# +# tclsqlite3.(deps|flags).N = N is $(STATIC_TCLSQLITE3) +# +tclsqlite3.deps.1 = sqlite3.o +tclsqlite3.flags.1 = -static $(tclsqlite3.deps.1) +tclsqlite3.deps.0 = $(libsqlite3.DLL) +tclsqlite3.flags.0 = $(tclsqlite3.deps.0) +tclsqlite3$(T.exe): $(T.tcl.env.sh) tclsqlite-shell.o $(tclsqlite3.deps.$(STATIC_TCLSQLITE3)) + $(T.link.tcl) -o $@ tclsqlite-shell.o \ + $(tclsqlite3.flags.$(STATIC_TCLSQLITE3)) $$TCL_INCLUDE_SPEC $$TCL_LIB_SPEC \ + $(LDFLAGS.libsqlite3) +tclsqlite3$(T.exe)-1: tclsqlite3$(T.exe) +tclsqlite3$(T.exe)-0: +tcl: tclsqlite3$(T.exe)-$(HAVE_TCL) + +# Rules to build opcodes.c and opcodes.h +# +opcodes.c: opcodes.h $(TOP)/tool/mkopcodec.tcl $(B.tclsh) + $(B.tclsh) $(TOP)/tool/mkopcodec.tcl opcodes.h >opcodes.c + +opcodes.h: parse.h $(TOP)/src/vdbe.c \ + $(TOP)/tool/mkopcodeh.tcl $(B.tclsh) + cat parse.h $(TOP)/src/vdbe.c | $(B.tclsh) $(TOP)/tool/mkopcodeh.tcl >opcodes.h + +# Rules to build parse.c and parse.h - the outputs of lemon. +# +parse.h: parse.c + +parse.c: $(TOP)/src/parse.y lemon$(B.exe) + cp $(TOP)/src/parse.y . + ./lemon$(B.exe) $(OPT_FEATURE_FLAGS) $(OPTS) -S parse.y + +pragma.h: $(TOP)/tool/mkpragmatab.tcl $(B.tclsh) + $(B.tclsh) $(TOP)/tool/mkpragmatab.tcl + +sqlite3rc.h: $(TOP)/src/sqlite3.rc $(TOP)/VERSION $(B.tclsh) + echo '#ifndef SQLITE_RESOURCE_VERSION' >$@ + echo -n '#define SQLITE_RESOURCE_VERSION ' >>$@ + cat $(TOP)/VERSION | $(B.tclsh) $(TOP)/tool/replace.tcl exact . , >>$@ + echo '#endif' >>sqlite3rc.h + +mkkeywordhash$(B.exe): $(TOP)/tool/mkkeywordhash.c + $(B.cc) -o $@ $(OPT_FEATURE_FLAGS) $(OPTS) $(TOP)/tool/mkkeywordhash.c +keywordhash.h: mkkeywordhash$(B.exe) + ./mkkeywordhash$(B.exe) > $@ + +# +# sqlite3.c split into many smaller files. +# +sqlite3-all.c: sqlite3.c $(TOP)/tool/split-sqlite3c.tcl $(B.tclsh) + $(B.tclsh) $(TOP)/tool/split-sqlite3c.tcl + +# +# Static libsqlite3 +# +$(libsqlite3.LIB): $(LIBOBJ) + $(AR) $(AR.flags) $@ $(LIBOBJ) +$(libsqlite3.LIB)-1: $(libsqlite3.LIB) +$(libsqlite3.LIB)-0 $(libsqlite3.LIB)-: +lib: $(libsqlite3.LIB)-$(ENABLE_LIB_STATIC) +all: lib + +# +# Dynamic libsqlite3 +# +$(libsqlite3.DLL): $(LIBOBJ) + $(T.link.shared) -o $@ $(LIBOBJ) $(LDFLAGS.libsqlite3) \ + $(LDFLAGS.libsqlite3.os-specific) $(LDFLAGS.libsqlite3.soname) +$(libsqlite3.DLL)-1: $(libsqlite3.DLL) +$(libsqlite3.DLL)-0 $(libsqlite3.DLL)-: +so: $(libsqlite3.DLL)-$(ENABLE_LIB_SHARED) +all: so + +# +# DLL installation... +# +# On most Unix-like platforms, install the $(libsqlite3.DLL) as +# $(libsqlite3.DLL).$(PACKAGE_VERSION) and create symlinks which point +# to it: +# +# - libsqlite3.so.$(PACKAGE_VERSION) +# - libsqlite3.so.0 =symlink-> libsqlite3.so.$(PACKAGE_VERSION) (see below) +# - libsqlite3.so =symlink-> libsqlite3.so.3 +# +# Different rules apply for platforms where $(T.dll)==.dylib and for +# the "Unix on Windows" environments. +# +# The link named libsqlite3.so.0 is provided in an attempt to reduce +# downstream disruption when performing upgrades from pre-3.48 to a +# version 3.48 or higher. That name is considered a legacy remnant +# and may eventually be removed from this installation process. +# +# Historically libtool installed the library like so: +# +# libsqlite3.so -> libsqlite3.so.0.8.6 +# libsqlite3.so.0 -> libsqlite3.so.0.8.6 +# libsqlite3.so.0.8.6 +# +# The historical SQLite build always used a version number of 0.8.6 +# for reasons lost to history but having something to do with libtool +# (which is no longer used in this tree). In order to retain filename +# compatibility for systems which have libraries installed using those +# conventions: +# +# 1) If libsqlite3.so.0.8.6 is found in the target installation +# directory then it is re-linked to point to the newer-style +# names. We cannot retain both the old and new installation because +# they both share the high-level name $(libsqlite3.DLL). The +# down-side of this is that it may upset packaging tools when we +# replace libsqlite3.so (from a legacy package) with a new symlink. +# +# 2) If INSTALL_SO_086_LINK=1 and point (1) does not apply then links +# to the legacy-style names are created. The primary intent of this +# is to enable chains of operations such as the hypothetical (apt +# remove sqlite3-3.47.0 && apt install sqlite3-3.48.0). In such +# cases, condition (1) would never trigger but applications might +# still expect to see the legacy file names. +# +# In either case, libsqlite3.la, if found, is deleted because it would +# contain stale state, referring to non-libtool-generated libraries. +# + +install-dll-out-implib: $(install-dir.lib) $(libsqlite3.DLL) + if [ x != "x$(libsqlite3.out.implib)" ] && [ -f "$(libsqlite3.out.implib)" ]; then \ + $(INSTALL) $(libsqlite3.out.implib) "$(install-dir.lib)"; \ + fi + +install-dll-unix-generic: install-dll-out-implib + $(INSTALL) $(libsqlite3.DLL) "$(install-dir.lib)" + @echo "Setting up $(libsqlite3.DLL) version symlinks..."; \ + cd "$(install-dir.lib)" || exit $$?; \ + rm -f $(libsqlite3.DLL).0 $(libsqlite3.DLL).$(PACKAGE_VERSION) || exit $$?; \ + mv $(libsqlite3.DLL) $(libsqlite3.DLL).$(PACKAGE_VERSION) || exit $$?; \ + ln -s $(libsqlite3.DLL).$(PACKAGE_VERSION) $(libsqlite3.DLL) || exit $$?; \ + ln -s $(libsqlite3.DLL).$(PACKAGE_VERSION) $(libsqlite3.DLL).0 || exit $$?; \ + ls -la $(libsqlite3.DLL) $(libsqlite3.DLL).[a03]*; \ + if [ -e $(libsqlite3.DLL).0.8.6 ]; then \ + echo "ACHTUNG: legacy libtool-compatible install found. Re-linking it..."; \ + rm -f libsqlite3.la $(libsqlite3.DLL).0.8.6 || exit $$?; \ + ln -s $(libsqlite3.DLL).$(PACKAGE_VERSION) $(libsqlite3.DLL).0.8.6 || exit $$?; \ + ls -la $(libsqlite3.DLL).0.8.6; \ + elif [ x1 = "x$(INSTALL_SO_086_LINK)" ]; then \ + echo "ACHTUNG: installing legacy libtool-style links because INSTALL_SO_086_LINK=1"; \ + rm -f libsqlite3.la $(libsqlite3.DLL).0.8.6 || exit $$?; \ + ln -s $(libsqlite3.DLL).$(PACKAGE_VERSION) $(libsqlite3.DLL).0.8.6 || exit $$?; \ + ls -la $(libsqlite3.DLL).0.8.6; \ + fi + +install-dll-msys: install-dll-out-implib $(install-dir.bin) + $(INSTALL) $(libsqlite3.DLL) "$(install-dir.bin)" +# ----------------------------------------------^^^ yes, bin +# Each of {msys,mingw,cygwin} uses a different name for the DLL, but +# that is already accounted for via $(libsqlite3.DLL). +install-dll-mingw: install-dll-msys +install-dll-cygwin: install-dll-msys + +install-dll-darwin: $(install-dir.lib) $(libsqlite3.DLL) + $(INSTALL) $(libsqlite3.DLL) "$(install-dir.lib)" + @echo "Setting up $(libsqlite3.DLL) version symlinks..."; \ + cd "$(install-dir.lib)" || exit $$?; \ + rm -f libsqlite3.0$(T.dll) libsqlite3.$(PACKAGE_VERSION)$(T.dll) || exit $$?; \ + dllname=libsqlite3.$(PACKAGE_VERSION)$(T.dll); \ + mv $(libsqlite3.DLL) $$dllname || exit $$?; \ + ln -s $$dllname $(libsqlite3.DLL) || exit $$?; \ + ln -s $$dllname libsqlite3.0$(T.dll) || exit $$?; \ + ls -la $$dllname $(libsqlite3.DLL) libsqlite3.0$(T.dll) + +install-dll-1: install-dll-$(libsqlite3.DLL.install-rules) +install-dll-0 install-dll-: +install-dll: install-dll-$(ENABLE_LIB_SHARED) +install: install-dll + +# +# Install $(libsqlite3.LIB) +# +install-lib-1: $(install-dir.lib) $(libsqlite3.LIB) + $(INSTALL.noexec) $(libsqlite3.LIB) "$(install-dir.lib)" +install-lib-0 install-lib-: +install-lib: install-lib-$(ENABLE_LIB_STATIC) +install: install-lib + +# +# Install C header files +# +install-headers: sqlite3.h $(install-dir.include) + $(INSTALL.noexec) sqlite3.h "$(TOP)/src/sqlite3ext.h" "$(install-dir.include)" +install: install-headers + +# +# If TCL_EXT_DLL_BASENAME is not set then guess the Tcl extension's +# DLL name depending on the Tcl version. This does not account for +# Cygwin's naming - the canonical build will usually set it, but +# static makefiles importing this one will need to account for that on +# their own. They can do that by setting libtclsqlite3.basename-[89] +# to appropriate names (cygsqlite resp. cygtcl9sqlite). +# +TCL_MAJOR_VERSION ?= 0 +libtclsqlite3.basename-8 ?= libsqlite +libtclsqlite3.basename-9 ?= libtcl9sqlite +TCL_EXT_DLL_BASENAME ?= $(libtclsqlite3.basename-$(TCL_MAJOR_VERSION)) +libtclsqlite3.DLL ?= $(TCL_EXT_DLL_BASENAME)$(PACKAGE_VERSION)$(T.dll) + +# +# libtclsqlite3... +# +pkgIndex.tcl: $(TOP)/main.mk + echo 'package ifneeded sqlite3 $(PACKAGE_VERSION) [list load [file join $$dir $(libtclsqlite3.DLL)] Sqlite3]' > $@ +pkgIndex.tcl-1: pkgIndex.tcl +pkgIndex.tcl-0 pkgIndex.tcl-: +tcl: pkgIndex.tcl-$(HAVE_TCL) + +$(libtclsqlite3.DLL): $(T.tcl.env.sh) tclsqlite.o $(LIBOBJ) + $(T.tcl.env.source); \ + $(T.link.shared) -o $@ tclsqlite.o \ + $$TCL_INCLUDE_SPEC $$TCL_STUB_LIB_SPEC $(LDFLAGS.libsqlite3) \ + $(LIBOBJ) -Wl,-rpath,$$TCLLIBDIR +# ^^^ that rpath bit is defined as TCL_LD_SEARCH_FLAGS in +# tclConfig.sh, but it's defined in such a way as to be useless for a +# _static_ makefile. +$(libtclsqlite3.DLL)-1: $(libtclsqlite3.DLL) +$(libtclsqlite3.DLL)-0 $(libtclsqlite3.DLL)-: +libtcl: $(libtclsqlite3.DLL)-$(HAVE_TCL) +tcl: libtcl +all: tcl + +install-tcl-1: $(libtclsqlite3.DLL) pkgIndex.tcl + $(T.tcl.env.source); \ + $(INSTALL) -d "$(DESTDIR)$$TCLLIBDIR"; \ + $(INSTALL) $(libtclsqlite3.DLL) "$(DESTDIR)$$TCLLIBDIR"; \ + $(INSTALL.noexec) pkgIndex.tcl "$(DESTDIR)$$TCLLIBDIR" +install-tcl-0 install-tcl-: + @echo "TCL support disabled, so not installing $(libtclsqlite3.DLL)" +install-tcl: install-tcl-$(HAVE_TCL) +install: install-tcl + +tclsqlite3.c: sqlite3.c + echo '#ifndef USE_SYSTEM_SQLITE' >tclsqlite3.c + cat sqlite3.c >>tclsqlite3.c + echo '#endif /* USE_SYSTEM_SQLITE */' >>tclsqlite3.c + cat $(TOP)/src/tclsqlite.c >>tclsqlite3.c + +# +# $(CFLAGS.tclextension) = CFLAGS for the tclextension* targets. +# +CFLAGS.tclextension = $(CFLAGS.intree_includes) $(CFLAGS.env) $(OPT_FEATURE_FLAGS) $(OPTS) +# +# Build the SQLite TCL extension in a way that make it compatible +# with whatever version of TCL is running as $TCLSH_CMD, possibly defined +# by --with-tclsh=/path/to/tclsh. +# +tclextension: tclsqlite3.c + $(TCLSH_CMD) $(TOP)/tool/buildtclext.tcl --build-only \ + --tclConfig.sh $(TCL_CONFIG_SH) --cc "$(T.cc)" $(CFLAGS.tclextension) + +# +# Install the SQLite TCL extension in a way that is appropriate for $TCLSH_CMD +# to find it. +# +tclextension-install: tclsqlite3.c + $(TCLSH_CMD) $(TOP)/tool/buildtclext.tcl --destdir "$(DESTDIR)" \ + --tclConfig.sh $(TCL_CONFIG_SH) --cc "$(T.cc)" $(CFLAGS.tclextension) + +# +# Uninstall the SQLite TCL extension that is used by $TCLSH_CMD. +# +tclextension-uninstall: + $(TCLSH_CMD) $(TOP)/tool/buildtclext.tcl --uninstall \ + --tclConfig.sh $(TCL_CONFIG_SH) + +# +# List all installed the SQLite TCL extensions that is are accessible +# by $TCLSH_CMD, including prior versions. +# +tclextension-list: + @ $(TCLSH_CMD) $(TOP)/tool/buildtclext.tcl --info \ + --tclConfig.sh $(TCL_CONFIG_SH) + +# Verify that the SQLite TCL extension that is loaded by default +# in $(TCLSH_CMD) is the same as the version of SQLite for the +# current source tree +# +tclextension-verify: sqlite3.h + @ $(TCLSH_CMD) $(TOP)/tool/buildtclext.tcl --version-check \ + --tclConfig.sh $(TCL_CONFIG_SH) + +# Run all of the tclextension targets in order, ending with uninstall. +tclextension-all: + $(MAKE) tclextension + $(MAKE) tclextension-install + $(MAKE) tclextension-list + $(MAKE) tclextension-verify + $(MAKE) tclextension-uninstall + +# +# FTS5 things +# +FTS5_SRC = \ + $(TOP)/ext/fts5/fts5.h \ + $(TOP)/ext/fts5/fts5Int.h \ + $(TOP)/ext/fts5/fts5_aux.c \ + $(TOP)/ext/fts5/fts5_buffer.c \ + $(TOP)/ext/fts5/fts5_main.c \ + $(TOP)/ext/fts5/fts5_config.c \ + $(TOP)/ext/fts5/fts5_expr.c \ + $(TOP)/ext/fts5/fts5_hash.c \ + $(TOP)/ext/fts5/fts5_index.c \ + fts5parse.c fts5parse.h \ + $(TOP)/ext/fts5/fts5_storage.c \ + $(TOP)/ext/fts5/fts5_tokenize.c \ + $(TOP)/ext/fts5/fts5_unicode2.c \ + $(TOP)/ext/fts5/fts5_varint.c \ + $(TOP)/ext/fts5/fts5_vocab.c \ + +fts5parse.c: $(TOP)/ext/fts5/fts5parse.y lemon$(B.exe) cp $(TOP)/ext/fts5/fts5parse.y . rm -f fts5parse.h - ./lemon $(OPTS) fts5parse.y + ./lemon$(B.exe) $(OPTS) -S fts5parse.y fts5parse.h: fts5parse.c -fts5.c: $(FTS5_SRC) $(FTS5_HDR) - tclsh $(TOP)/ext/fts5/tool/mkfts5c.tcl +fts5.c: $(FTS5_SRC) $(B.tclsh) + $(B.tclsh) $(TOP)/ext/fts5/tool/mkfts5c.tcl cp $(TOP)/ext/fts5/fts5.h . -userauth.o: $(TOP)/ext/userauth/userauth.c $(HDR) $(EXTHDR) - $(TCCX) -DSQLITE_CORE -c $(TOP)/ext/userauth/userauth.c +fts5.o: fts5.c $(DEPS_OBJ_COMMON) $(EXTHDR) + $(T.cc.extension) -c fts5.c -sqlite3rbu.o: $(TOP)/ext/rbu/sqlite3rbu.c $(HDR) $(EXTHDR) - $(TCCX) -DSQLITE_CORE -c $(TOP)/ext/rbu/sqlite3rbu.c +sqlite3rbu.o: $(TOP)/ext/rbu/sqlite3rbu.c $(DEPS_OBJ_COMMON) $(EXTHDR) + $(T.cc.extension) -c $(TOP)/ext/rbu/sqlite3rbu.c -# Rules for building test programs and for running tests # -tclsqlite3: $(TOP)/src/tclsqlite.c libsqlite3.a - $(TCCX) $(TCL_FLAGS) -DTCLSH=1 -o tclsqlite3 \ - $(TOP)/src/tclsqlite.c libsqlite3.a $(LIBTCL) $(THREADLIB) - -sqlite3_analyzer.c: sqlite3.c $(TOP)/src/tclsqlite.c $(TOP)/tool/spaceanal.tcl - echo "#define TCLSH 2" > $@ - echo "#define SQLITE_ENABLE_DBSTAT_VTAB 1" >> $@ - cat sqlite3.c $(TOP)/src/tclsqlite.c >> $@ - echo "static const char *tclsh_main_loop(void){" >> $@ - echo "static const char *zMainloop = " >> $@ - tclsh $(TOP)/tool/tostr.tcl $(TOP)/tool/spaceanal.tcl >> $@ - echo "; return zMainloop; }" >> $@ - -sqlite3_analyzer$(EXE): sqlite3_analyzer.c - $(TCCX) $(TCL_FLAGS) sqlite3_analyzer.c -o $@ $(LIBTCL) $(THREADLIB) - # Rules to build the 'testfixture' application. # +# If using the amalgamation, use sqlite3.c directly to build the test +# fixture. Otherwise link against libsqlite3.a. (This distinction is +# necessary because the test fixture requires non-API symbols which are +# hidden when the library is built via the amalgamation). +# TESTFIXTURE_FLAGS = -DSQLITE_TEST=1 -DSQLITE_CRASH_TEST=1 -TESTFIXTURE_FLAGS += -DSQLITE_SERVER=1 -DSQLITE_PRIVATE="" -DSQLITE_CORE +TESTFIXTURE_FLAGS += -DTCLSH_INIT_PROC=sqlite3TestInit +TESTFIXTURE_FLAGS += -DSQLITE_SERVER=1 -DSQLITE_PRIVATE="" -DSQLITE_CORE +TESTFIXTURE_FLAGS += -DBUILD_sqlite +TESTFIXTURE_FLAGS += -DSQLITE_SERIES_CONSTRAINT_VERIFY=1 +TESTFIXTURE_FLAGS += -DSQLITE_DEFAULT_PAGE_SIZE=1024 +TESTFIXTURE_FLAGS += -DSQLITE_ENABLE_STMTVTAB +TESTFIXTURE_FLAGS += -DSQLITE_ENABLE_DBPAGE_VTAB +TESTFIXTURE_FLAGS += -DSQLITE_ENABLE_BYTECODE_VTAB +TESTFIXTURE_FLAGS += -DSQLITE_ENABLE_CARRAY +TESTFIXTURE_FLAGS += -DSQLITE_ENABLE_PERCENTILE +TESTFIXTURE_FLAGS += -DSQLITE_CKSUMVFS_STATIC +TESTFIXTURE_FLAGS += -DSQLITE_STATIC_RANDOMJSON +TESTFIXTURE_FLAGS += -DSQLITE_STRICT_SUBTYPE=1 + +TESTFIXTURE_SRC0 = $(TESTSRC2) $(libsqlite3.LIB) +TESTFIXTURE_SRC1 = sqlite3.c +TESTFIXTURE_SRC = $(TESTSRC) $(TOP)/src/tclsqlite.c +TESTFIXTURE_SRC += $(TESTFIXTURE_SRC$(USE_AMALGAMATION)) + +testfixture$(T.exe): $(T.tcl.env.sh) has_tclsh85 $(TESTFIXTURE_SRC) + $(T.link.tcl) -DSQLITE_NO_SYNC=1 $(TESTFIXTURE_FLAGS) \ + -o $@ $(TESTFIXTURE_SRC) \ + $$TCL_LIB_SPEC $$TCL_INCLUDE_SPEC \ + $(LDFLAGS.libsqlite3) + +coretestprogs: testfixture$(B.exe) sqlite3$(B.exe) + +testprogs: $(TESTPROGS) srcck1$(B.exe) fuzzcheck$(T.exe) sessionfuzz$(T.exe) + +# A very detailed test running most or all test cases +fulltest: alltest fuzztest + +# Run most or all tcl test cases +alltest: $(TESTPROGS) + ./testfixture$(T.exe) $(TOP)/test/all.test $(TESTOPTS) + +# Really really long testing +soaktest: $(TESTPROGS) + ./testfixture$(T.exe) $(TOP)/test/all.test -soak=1 $(TESTOPTS) -testfixture$(EXE): $(TESTSRC2) libsqlite3.a $(TESTSRC) $(TOP)/src/tclsqlite.c - $(TCCX) $(TCL_FLAGS) -DTCLSH=1 $(TESTFIXTURE_FLAGS) \ - $(TESTSRC) $(TESTSRC2) $(TOP)/src/tclsqlite.c \ - -o testfixture$(EXE) $(LIBTCL) libsqlite3.a $(THREADLIB) +# Do extra testing but not everything. +fulltestonly: $(TESTPROGS) fuzztest + ./testfixture$(T.exe) $(TOP)/test/full.test -amalgamation-testfixture$(EXE): sqlite3.c $(TESTSRC) $(TOP)/src/tclsqlite.c - $(TCCX) $(TCL_FLAGS) -DTCLSH=1 $(TESTFIXTURE_FLAGS) \ - $(TESTSRC) $(TOP)/src/tclsqlite.c sqlite3.c \ - -o testfixture$(EXE) $(LIBTCL) $(THREADLIB) +# +# Fuzz testing +# +# WARNING: When the "fuzztest" target is run by the testrunner.tcl script, +# it does not actually run this code. Instead, it schedules equivalent +# commands. Therefore, if this target is updated, then code in +# testrunner_data.tcl (search for "trd_fuzztest_data") must also be updated. +# +fuzztest: fuzzcheck$(T.exe) $(FUZZDATA) sessionfuzz$(T.exe) + ./fuzzcheck$(T.exe) $(FUZZDATA) + ./sessionfuzz$(T.exe) run $(TOP)/test/sessionfuzz-data1.db -fts3-testfixture$(EXE): sqlite3.c fts3amal.c $(TESTSRC) $(TOP)/src/tclsqlite.c - $(TCCX) $(TCL_FLAGS) -DTCLSH=1 $(TESTFIXTURE_FLAGS) \ - -DSQLITE_ENABLE_FTS3=1 \ - $(TESTSRC) $(TOP)/src/tclsqlite.c sqlite3.c fts3amal.c \ - -o testfixture$(EXE) $(LIBTCL) $(THREADLIB) +valgrindfuzz: fuzzcheck$(T.exe) $(FUZZDATA) sessionfuzz$(T.exe) + valgrind ./fuzzcheck$(T.exe) --cell-size-check --limit-mem 10M $(FUZZDATA) + valgrind ./sessionfuzz$(T.exe) run $(TOP)/test/sessionfuzz-data1.db -fulltest: $(TESTPROGS) fuzztest - ./testfixture$(EXE) $(TOP)/test/all.test $(TESTOPTS) +# +# The veryquick.test TCL tests. +# +tcltest: ./testfixture$(T.exe) + ./testfixture$(T.exe) $(TOP)/test/veryquick.test $(TESTOPTS) -soaktest: $(TESTPROGS) - ./testfixture$(EXE) $(TOP)/test/all.test -soak=1 $(TESTOPTS) +# +# Runs all the same tests cases as the "tcltest" target but uses +# the testrunner.tcl script to run them in multiple cores +# concurrently. +testrunner: testfixture$(T.exe) + ./testfixture$(T.exe) $(TOP)/test/testrunner.tcl -fulltestonly: $(TESTPROGS) fuzztest - ./testfixture$(EXE) $(TOP)/test/full.test $(TESTOPTS) +# +# This is the testing target preferred by the core SQLite developers. +# It runs tests under a standard configuration, regardless of how +# ./configure was run. The devs run "make devtest" prior to each +# check-in, at a minimum. Probably other tests too, but at least this +# one. +# +devtest: srctree-check sourcetest + $(TCLSH_CMD) $(TOP)/test/testrunner.tcl mdevtest $(TSTRNNR_OPTS) -queryplantest: testfixture$(EXE) sqlite3$(EXE) - ./testfixture$(EXE) $(TOP)/test/permutations.test queryplanner $(TESTOPTS) +mdevtest: srctree-check has_tclsh85 + $(TCLSH_CMD) $(TOP)/test/testrunner.tcl mdevtest $(TSTRNNR_OPTS) -fuzztest: fuzzcheck$(EXE) $(FUZZDATA) - ./fuzzcheck$(EXE) $(FUZZDATA) +sdevtest: has_tclsh85 + $(TCLSH_CMD) $(TOP)/test/testrunner.tcl sdevtest $(TSTRNNR_OPTS) -fastfuzztest: fuzzcheck$(EXE) $(FUZZDATA) - ./fuzzcheck$(EXE) --limit-mem 100M $(FUZZDATA) +# Like releasetest, except it omits srctree-check and verify-source so +# that it can be used on a modified source tree. +# +xdevtest: has_tclsh85 + $(TCLSH_CMD) $(TOP)/test/testrunner.tcl release $(TSTRNNR_OPTS) -valgrindfuzz: fuzzcheck$(EXE) $(FUZZDATA) - valgrind ./fuzzcheck$(EXE) --cell-size-check --limit-mem 10M --timeout 600 $(FUZZDATA) +# +# Validate that various generated files in the source tree +# are up-to-date. +# +srctree-check: $(TOP)/tool/srctree-check.tcl + $(TCLSH_CMD) $(TOP)/tool/srctree-check.tcl + +# +# Testing for a release +# +releasetest: srctree-check has_tclsh85 verify-source + $(TCLSH_CMD) $(TOP)/test/testrunner.tcl release $(TSTRNNR_OPTS) -# A very quick test using only testfixture and omitting all the slower -# tests. Designed to run in under 3 minutes on a workstation. # -quicktest: ./testfixture$(EXE) - ./testfixture$(EXE) $(TOP)/test/extraquick.test $(TESTOPTS) +# Minimal testing that runs in less than 3 minutes +# +quicktest: ./testfixture$(T.exe) + ./testfixture$(T.exe) $(TOP)/test/extraquick.test $(TESTOPTS) -# The default test case. Runs most of the faster standard TCL tests, -# and fuzz tests, and sqlite3_analyzer and sqldiff tests. # -test: $(TESTPROGS) sourcetest fastfuzztest - ./testfixture$(EXE) $(TOP)/test/veryquick.test $(TESTOPTS) +# Try to run tests on whatever options are specified by the +# ./configure. The developers seldom use this target. Instead +# they use "make devtest" which runs tests on a standard set of +# options regardless of how SQLite is configured. This "test" +# target is provided for legacy only. +# +test: srctree-check fuzztest sourcetest $(TESTPROGS) tcltest +# # Run a test using valgrind. This can take a really long time # because valgrind is so much slower than a native machine. # valgrindtest: $(TESTPROGS) valgrindfuzz - OMIT_MISUSE=1 valgrind -v \ - ./testfixture$(EXE) $(TOP)/test/permutations.test valgrind $(TESTOPTS) + OMIT_MISUSE=1 valgrind -v ./testfixture$(T.exe) $(TOP)/test/permutations.test valgrind $(TESTOPTS) +# # A very fast test that checks basic sanity. The name comes from # the 60s-era electronics testing: "Turn it on and see if smoke # comes out." # -smoketest: $(TESTPROGS) fuzzcheck$(EXE) - ./testfixture$(EXE) $(TOP)/test/main.test $(TESTOPTS) +smoketest: $(TESTPROGS) fuzzcheck$(T.exe) + ./testfixture$(T.exe) $(TOP)/test/main.test $(TESTOPTS) + +shelltest: + $(TCLSH_CMD) $(TOP)/test/testrunner.tcl release shell + +# +# sqlite3_analyzer.c build depends on $(LINK_TOOLS_DYNAMICALLY). +# +sqlite3_analyzer.c.flags.0 = -DINCLUDE_SQLITE3_C=1 +sqlite3_analyzer.c.flags.1 = +sqlite3_analyzer.c: sqlite3.c $(TOP)/src/tclsqlite.c $(TOP)/tool/spaceanal.tcl \ + $(TOP)/tool/mkccode.tcl $(TOP)/tool/sqlite3_analyzer.c.in + $(B.tclsh) $(TOP)/tool/mkccode.tcl $(TOP)/tool/sqlite3_analyzer.c.in \ + $(sqlite3_analyzer.c.flags.$(LINK_TOOLS_DYNAMICALLY)) \ + $(OPT_FEATURE_FLAGS) \ + > $@ + +# +# sqlite3_analyzer's build mode depends on $(LINK_TOOLS_DYNAMICALLY). +# +sqlite3_analyzer.flags.1 = -L. -lsqlite3 +sqlite3_analyzer.flags.0 = $(LDFLAGS.libsqlite3) +sqlite3_analyzer.deps.1 = $(libsqlite3.DLL) +sqlite3_analyzer.deps.0 = +sqlite3_analyzer$(T.exe): $(T.tcl.env.sh) sqlite3_analyzer.c \ + $(sqlite3_analyzer.deps.$(LINK_TOOLS_DYNAMICALLY)) + $(T.link.tcl) sqlite3_analyzer.c -o $@ \ + $(sqlite3_analyzer.flags.$(LINK_TOOLS_DYNAMICALLY)) \ + $$TCL_LIB_SPEC $$TCL_INCLUDE_SPEC $$TCL_LIBS +# ^^^^ the order of those flags is relevant for +# $(sqlite3_analyzer.flags.1): if the $$TCL_... flags come first they +# can cause the $@ to link to an out-of-tree libsqlite3.so, which may +# or may not fail or otherwise cause confusion. + +sqltclsh.c: sqlite3.c $(TOP)/src/tclsqlite.c $(TOP)/tool/sqltclsh.tcl \ + $(TOP)/ext/misc/appendvfs.c $(TOP)/tool/mkccode.tcl \ + $(TOP)/tool/sqltclsh.c.in + $(B.tclsh) $(TOP)/tool/mkccode.tcl $(TOP)/tool/sqltclsh.c.in >sqltclsh.c + +sqltclsh$(T.exe): $(T.tcl.env.sh) sqltclsh.c + $(T.link.tcl) sqltclsh.c -o $@ $$TCL_INCLUDE_SPEC \ + $(LDFLAGS.libsqlite3) $$TCL_LIB_SPEC $$TCL_LIBS +# xbin: target for generic binaries which aren't usually built. It is +# used primarily for testing the build process. +xbin: sqltclsh$(T.exe) sqlite3_analyzer$(T.exe) + +sqlite3_expert$(T.exe): $(TOP)/ext/expert/sqlite3expert.h $(TOP)/ext/expert/sqlite3expert.c \ + $(TOP)/ext/expert/expert.c sqlite3.c + $(T.link) $(TOP)/ext/expert/sqlite3expert.c \ + $(TOP)/ext/expert/expert.c sqlite3.c -o sqlite3_expert $(LDFLAGS.libsqlite3) +xbin: sqlite3_expert$(T.exe) + +CHECKER_DEPS =\ + $(TOP)/tool/mkccode.tcl \ + sqlite3.c \ + $(TOP)/src/tclsqlite.c \ + $(TOP)/ext/repair/sqlite3_checker.tcl \ + $(TOP)/ext/repair/checkindex.c \ + $(TOP)/ext/repair/checkfreelist.c \ + $(TOP)/ext/misc/btreeinfo.c \ + $(TOP)/ext/repair/sqlite3_checker.c.in + +sqlite3_checker.c: $(CHECKER_DEPS) + $(B.tclsh) $(TOP)/tool/mkccode.tcl $(TOP)/ext/repair/sqlite3_checker.c.in >$@ + +sqlite3_checker$(T.exe): $(T.tcl.env.sh) sqlite3_checker.c + $(T.link.tcl) sqlite3_checker.c -o $@ $$TCL_INCLUDE_SPEC \ + $$TCL_LIB_SPEC $(LDFLAGS.libsqlite3) +xbin: sqlite3_checker$(T.exe) + +dbdump$(T.exe): $(TOP)/ext/misc/dbdump.c sqlite3.o + $(T.link) -DDBDUMP_STANDALONE -o $@ \ + $(TOP)/ext/misc/dbdump.c sqlite3.o $(LDFLAGS.libsqlite3) +xbin: dbdump$(T.exe) + +dbtotxt$(T.exe): $(TOP)/tool/dbtotxt.c + $(T.link)-o $@ $(TOP)/tool/dbtotxt.c $(LDFLAGS.configure) +xbin: dbtotxt$(T.exe) + +showdb$(T.exe): $(TOP)/tool/showdb.c sqlite3.o + $(T.link) -o $@ $(TOP)/tool/showdb.c sqlite3.o $(LDFLAGS.libsqlite3) +xbin: showdb$(T.exe) + +showstat4$(T.exe): $(TOP)/tool/showstat4.c sqlite3.o + $(T.link) -o $@ $(TOP)/tool/showstat4.c sqlite3.o $(LDFLAGS.libsqlite3) +xbin: showstat4$(T.exe) + +showjournal$(T.exe): $(TOP)/tool/showjournal.c sqlite3.o + $(T.link) -o $@ $(TOP)/tool/showjournal.c sqlite3.o $(LDFLAGS.libsqlite3) +xbin: showjournal$(T.exe) + +showwal$(T.exe): $(TOP)/tool/showwal.c sqlite3.o + $(T.link) -o $@ $(TOP)/tool/showwal.c sqlite3.o $(LDFLAGS.libsqlite3) +xbin: showwal$(T.exe) + +showshm$(T.exe): $(TOP)/tool/showshm.c + $(T.link) -o $@ $(TOP)/tool/showshm.c $(LDFLAGS.configure) +xbin: showshm$(T.exe) + +index_usage$(T.exe): $(TOP)/tool/index_usage.c sqlite3.o + $(T.link) $(SHELL_OPT) -o $@ $(TOP)/tool/index_usage.c sqlite3.o \ + $(LDFLAGS.libsqlite3) +xbin: index_usage$(T.exe) + +# Reminder: changeset does not build without -DSQLITE_ENABLE_SESSION +changeset$(T.exe): $(TOP)/ext/session/changeset.c sqlite3.o + $(T.link) -o $@ $(TOP)/ext/session/changeset.c sqlite3.o \ + $(LDFLAGS.libsqlite3) +xbin: changeset$(T.exe) + +changesetfuzz$(T.exe): $(TOP)/ext/session/changesetfuzz.c sqlite3.o + $(T.link) -o $@ $(TOP)/ext/session/changesetfuzz.c sqlite3.o \ + $(LDFLAGS.libsqlite3) +xbin: changesetfuzz$(T.exe) + +rollback-test$(T.exe): $(TOP)/tool/rollback-test.c sqlite3.o + $(T.link) -o $@ $(TOP)/tool/rollback-test.c sqlite3.o $(LDFLAGS.libsqlite3) +xbin: rollback-test$(T.exe) + +atrc$(T.exe): $(TOP)/test/atrc.c sqlite3.o + $(T.link) -o $@ $(TOP)/test/atrc.c sqlite3.o $(LDFLAGS.libsqlite3) +xbin: atrc$(T.exe) + +LogEst$(T.exe): $(TOP)/tool/logest.c sqlite3.h + $(T.link) -I. -o $@ $(TOP)/tool/logest.c $(LDFLAGS.configure) +xbin: LogEst$(T.exe) + +wordcount$(T.exe): $(TOP)/test/wordcount.c sqlite3.o + $(T.link) -o $@ $(TOP)/test/wordcount.c sqlite3.o $(LDFLAGS.libsqlite3) +xbin: wordcount$(T.exe) + +speedtest1$(T.exe): $(TOP)/test/speedtest1.c sqlite3.c Makefile + $(T.link) $(ST_OPT) -o $@ $(TOP)/test/speedtest1.c sqlite3.c \ + $(LDFLAGS.libsqlite3) +xbin: speedtest1$(T.exe) + +startup$(T.exe): $(TOP)/test/startup.c sqlite3.c + $(T.link) -Os -g -USQLITE_THREADSAFE -DSQLITE_THREADSAFE=0 \ + -o $@ $(TOP)/test/startup.c sqlite3.c $(LDFLAGS.libsqlite3) +xbin: startup$(T.exe) + +KV_OPT += -DSQLITE_DIRECT_OVERFLOW_READ + +kvtest$(T.exe): $(TOP)/test/kvtest.c sqlite3.c + $(T.link) $(KV_OPT) -o $@ $(TOP)/test/kvtest.c sqlite3.c \ + $(LDFLAGS.libsqlite3) +xbin: kvtest$(T.exe) + +# +# rbu$(T.exe) requires building with -DSQLITE_ENABLE_RBU, which +# specifically does not have an --enable-rbu flag in the configure +# script. +rbu$(T.exe): $(TOP)/ext/rbu/rbu.c $(TOP)/ext/rbu/sqlite3rbu.c sqlite3.o + $(T.link) -I. -o $@ $(TOP)/ext/rbu/rbu.c sqlite3.o $(LDFLAGS.libsqlite3) + +loadfts$(T.exe): $(TOP)/tool/loadfts.c $(libsqlite3.LIB) + $(T.link) $(TOP)/tool/loadfts.c $(libsqlite3.LIB) \ + -o $@ $(LDFLAGS.libsqlite3) +xbin: loadfts$(T.exe) + +# This target will fail if the SQLite amalgamation contains any exported +# symbols that do not begin with "sqlite3_". It is run as part of the +# releasetest.tcl script. +# +VALIDIDS=' sqlite3(changeset|changegroup|session|rebaser)?_' +checksymbols: sqlite3.o + nm -g --defined-only sqlite3.o + nm -g --defined-only sqlite3.o | egrep -v $(VALIDIDS); test $$? -ne 0 + echo '0 errors out of 1 tests' + +# Build the amalgamation-autoconf package. The amalgamation-tarball target builds +# a tarball named for the version number. Ex: sqlite-autoconf-3110000.tar.gz. +# The snapshot-tarball target builds a tarball named by the SHA3 hash +# +amalgamation-tarball: sqlite3.c sqlite3rc.h + TOP=$(TOP) sh $(TOP)/tool/mkautoconfamal.sh --normal + +snapshot-tarball: sqlite3.c sqlite3rc.h + TOP=$(TOP) sh $(TOP)/tool/mkautoconfamal.sh --snapshot + +# Build a ZIP archive snapshot of the latest check-in. +# +sqlite-src.zip: $(TOP)/tool/mksrczip.tcl + $(TCLSH_CMD) $(TOP)/tool/mksrczip.tcl + +# Build a ZIP archive of the amalgamation +# +sqlite-amalgamation.zip: $(TOP)/tool/mkamalzip.tcl sqlite3.c sqlite3.h shell.c sqlite3ext.h + $(TCLSH_CMD) $(TOP)/tool/mkamalzip.tcl + +# Build all the source code deliverables +# +src-archives: sqlite-amalgamation.zip amalgamation-tarball sqlite-src.zip + ls -ltr *.zip *.tar.gz | tail -3 -# The next two rules are used to support the "threadtest" target. Building +# Build a ZIP archive containing various command-line tools. +# +tool-zip: testfixture$(T.exe) sqlite3$(T.exe) sqldiff$(T.exe) \ + sqlite3_analyzer$(T.exe) sqlite3_rsync$(T.exe) $(TOP)/tool/mktoolzip.tcl + strip sqlite3$(T.exe) sqldiff$(T.exe) sqlite3_analyzer$(T.exe) sqlite3_rsync$(T.exe) + ./testfixture$(T.exe) $(TOP)/tool/mktoolzip.tcl +snapshot-zip: testfixture$(T.exe) sqlite3$(T.exe) sqldiff$(T.exe) \ + sqlite3_analyzer$(T.exe) sqlite3_rsync$(T.exe) $(TOP)/tool/mktoolzip.tcl + strip sqlite3$(T.exe) sqldiff$(T.exe) sqlite3_analyzer$(T.exe) sqlite3_rsync$(T.exe) + ./testfixture$(T.exe) $(TOP)/tool/mktoolzip.tcl --snapshot +clean-tool-zip: + rm -f sqlite-tools-*.zip +clean: clean-tool-zip + +# +# The next few rules are used to support the "threadtest" target. Building # threadtest runs a few thread-safety tests that are implemented in C. This # target is invoked by the releasetest.tcl script. -# +# THREADTEST3_SRC = $(TOP)/test/threadtest3.c \ $(TOP)/test/tt3_checkpoint.c \ $(TOP)/test/tt3_index.c \ @@ -801,116 +2163,401 @@ THREADTEST3_SRC = $(TOP)/test/threadtest3.c \ $(TOP)/test/tt3_stress.c \ $(TOP)/test/tt3_lookaside1.c -threadtest3$(EXE): sqlite3.o $(THREADTEST3_SRC) $(TOP)/src/test_multiplex.c - $(TCCX) $(TOP)/test/threadtest3.c $(TOP)/src/test_multiplex.c sqlite3.o -o $@ $(THREADLIB) +threadtest3$(T.exe): sqlite3.o $(THREADTEST3_SRC) + $(T.link) $(TOP)/test/threadtest3.c $(TOP)/src/test_multiplex.c sqlite3.o \ + -o $@ $(LDFLAGS.libsqlite3) +xbin: threadtest3$(T.exe) -threadtest: threadtest3$(EXE) - ./threadtest3$(EXE) +threadtest: threadtest3$(T.exe) + ./threadtest3$(T.exe) -TEST_EXTENSION = $(SHPREFIX)testloadext.$(SO) -$(TEST_EXTENSION): $(TOP)/src/test_loadext.c - $(MKSHLIB) $(TOP)/src/test_loadext.c -o $(TEST_EXTENSION) +threadtest5: sqlite3.c $(TOP)/test/threadtest5.c + $(T.link) $(TOP)/test/threadtest5.c sqlite3.c -o $@ $(LDFLAGS.libsqlite3) +xbin: threadtest5 -extensiontest: testfixture$(EXE) $(TEST_EXTENSION) - ./testfixture$(EXE) $(TOP)/test/loadext.test +# +# STATIC_CLI_SHELL = 1 to statically link sqlite3$(T.exe), else +# 0. Requires static versions of all requisite libraries. Primarily +# intended for use with static-friendly environments like Alpine +# Linux. +# +STATIC_CLI_SHELL ?= 0 +# +# sqlite3-shell-static.flags.N = N is $(STATIC_CLI_SHELL) +# +sqlite3-shell-static.flags.1 = -static +sqlite3-shell-static.flags.0 = +# +# When building sqlite3$(T.exe) we specifically embed a copy of +# sqlite3.c, and not link to libsqlite3.so or libsqlite3.a, because +# the shell needs to be able to enable arbitrary library features, +# some of which have significant performance impacts. For example,, +# SQLITE_ENABLE_EXPLAIN_COMMENTS has been measured as having a 5.2% +# runtime performance hit, which is fine for use in the shell but is +# not appropriate for the canonical library build. +# +sqlite3$(T.exe): shell.c sqlite3.c + $(T.link) -o $@ \ + shell.c sqlite3.c \ + $(sqlite3-shell-static.flags.$(STATIC_CLI_SHELL)) \ + $(CFLAGS.readline) $(SHELL_OPT) $(CFLAGS.icu) \ + $(LDFLAGS.libsqlite3) $(LDFLAGS.readline) +# +# Build sqlite3$(T.exe) by default except in wasi-sdk builds. Yes, the +# semantics of 0 and 1 are confusingly swapped here. +# +sqlite3$(T.exe)-1: +sqlite3$(T.exe)-0: sqlite3$(T.exe) +all: sqlite3$(T.exe)-$(HAVE_WASI_SDK) -showdb$(EXE): $(TOP)/tool/showdb.c sqlite3.o - $(TCC) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -o showdb$(EXE) \ - $(TOP)/tool/showdb.c sqlite3.o $(THREADLIB) +# The "sqlite3d" CLI is build using separate source files. This +# is useful during development and debugging. +# +sqlite3d$(T.exe): shell.c $(LIBOBJS0) + $(T.link) -o $@ \ + shell.c $(LIBOBJS0) \ + $(CFLAGS.readline) $(SHELL_OPT) \ + $(LDFLAGS.libsqlite3) $(LDFLAGS.readline) + +install-shell-0: sqlite3$(T.exe) $(install-dir.bin) + $(INSTALL) sqlite3$(T.exe) "$(install-dir.bin)" +install-shell-1: +install: install-shell-$(HAVE_WASI_SDK) + +# How to build sqldiff$(T.exe) depends on $(LINK_TOOLS_DYNAMICALLY) +# +sqldiff.0.deps = $(TOP)/tool/sqldiff.c $(TOP)/ext/misc/sqlite3_stdio.h sqlite3.o sqlite3.h +sqldiff.0.rules = $(T.link) -o $@ $(TOP)/tool/sqldiff.c sqlite3.o $(LDFLAGS.libsqlite3) +sqldiff.1.deps = $(TOP)/tool/sqldiff.c $(TOP)/ext/misc/sqlite3_stdio.h $(libsqlite3.DLL) +sqldiff.1.rules = $(T.link) -o $@ $(TOP)/tool/sqldiff.c -L. -lsqlite3 $(LDFLAGS.configure) +sqldiff$(T.exe): $(sqldiff.$(LINK_TOOLS_DYNAMICALLY).deps) + $(sqldiff.$(LINK_TOOLS_DYNAMICALLY).rules) + +install-diff: sqldiff$(T.exe) $(install-dir.bin) + $(INSTALL) sqldiff$(T.exe) "$(install-dir.bin)" +#install: install-diff + +dbhash$(T.exe): $(TOP)/tool/dbhash.c sqlite3.o sqlite3.h + $(T.link) -o $@ $(TOP)/tool/dbhash.c sqlite3.o $(LDFLAGS.libsqlite3) +xbin: dbhash$(T.exe) + +RSYNC_SRC = \ + $(TOP)/tool/sqlite3_rsync.c \ + sqlite3.c + +RSYNC_OPT = \ + -DSQLITE_ENABLE_DBPAGE_VTAB \ + -USQLITE_THREADSAFE \ + -DSQLITE_THREADSAFE=0 \ + -DSQLITE_OMIT_LOAD_EXTENSION \ + -DSQLITE_OMIT_DEPRECATED + +sqlite3_rsync$(T.exe): $(RSYNC_SRC) + $(T.cc.sqlite) -o $@ $(RSYNC_OPT) $(RSYNC_SRC) $(LDFLAGS.libsqlite3) +xbin: sqlite3_rsync$(T.exe) + +install-rsync: sqlite3_rsync$(T.exe) $(install-dir.bin) + $(INSTALL) sqlite3_rsync$(T.exe) "$(install-dir.bin)" +#install: install-rsync + +install-man1: $(install-dir.man1) + $(INSTALL.noexec) "$(TOP)/sqlite3.1" "$(install-dir.man1)" +install: install-man1 -showstat4$(EXE): $(TOP)/tool/showstat4.c sqlite3.o - $(TCC) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -o showstat4$(EXE) \ - $(TOP)/tool/showstat4.c sqlite3.o $(THREADLIB) +# +# sqlite3.pc is typically generated by the configure script but could +# conceivably be generated by hand. +install-pc: sqlite3.pc $(install-dir.pkgconfig) + $(INSTALL.noexec) sqlite3.pc "$(install-dir.pkgconfig)" + +scrub$(T.exe): $(TOP)/ext/misc/scrub.c sqlite3.o + $(T.link) -o $@ -I. -DSCRUB_STANDALONE \ + $(TOP)/ext/misc/scrub.c sqlite3.o $(LDFLAGS.libsqlite3) +xbin: scrub$(T.exe) + +srcck1$(B.exe): $(TOP)/tool/srcck1.c + $(B.cc) -o srcck1$(B.exe) $(TOP)/tool/srcck1.c +xbin: srcck1$(B.exe) + +sourcetest: srcck1$(B.exe) sqlite3.c + ./srcck1$(B.exe) sqlite3.c + +src-verify$(B.exe): $(TOP)/tool/src-verify.c + $(B.cc) -o src-verify$(B.exe) $(TOP)/tool/src-verify.c +xbin: src-verify$(B.exe) + +verify-source: ./src-verify$(B.exe) + ./src-verify$(B.exe) $(TOP) + +fuzzershell$(T.exe): $(TOP)/tool/fuzzershell.c sqlite3.c sqlite3.h + $(T.link) -o $@ $(FUZZERSHELL_OPT) \ + $(TOP)/tool/fuzzershell.c sqlite3.c $(LDFLAGS.libsqlite3) +fuzzy: fuzzershell$(T.exe) +xbin: fuzzershell$(T.exe) + +fuzzcheck$(T.exe): $(FUZZCHECK_SRC) sqlite3.c sqlite3.h $(FUZZCHECK_DEP) + $(T.link) -o $@ $(FUZZCHECK_OPT) $(FUZZCHECK_SRC) sqlite3.c $(LDFLAGS.libsqlite3) +fuzzy: fuzzcheck$(T.exe) +xbin: fuzzcheck$(T.exe) + +# -fsanitize=... flags for fuzzcheck-asan. +CFLAGS.fuzzcheck-asan.fsanitize ?= -fsanitize=address + +fuzzcheck-asan$(T.exe): $(FUZZCHECK_SRC) sqlite3.c sqlite3.h $(FUZZCHECK_DEP) + $(T.link) -o $@ $(CFLAGS.fuzzcheck-asan.fsanitize) $(FUZZCHECK_OPT) $(FUZZCHECK_SRC) \ + sqlite3.c $(LDFLAGS.libsqlite3) +fuzzy: fuzzcheck-asan$(T.exe) +xbin: fuzzcheck-asan$(T.exe) + +fuzzcheck-ubsan$(T.exe): $(FUZZCHECK_SRC) sqlite3.c sqlite3.h $(FUZZCHECK_DEP) + $(T.link) -o $@ -fsanitize=undefined $(FUZZCHECK_OPT) $(FUZZCHECK_SRC) \ + sqlite3.c $(LDFLAGS.libsqlite3) +fuzzy: fuzzcheck-ubsan$(T.exe) +xbin: fuzzcheck-ubsan$(T.exe) + + +ossshell$(T.exe): $(TOP)/test/ossfuzz.c $(TOP)/test/ossshell.c sqlite3.c sqlite3.h + $(T.link) -o $@ $(FUZZCHECK_OPT) $(TOP)/test/ossshell.c \ + $(TOP)/test/ossfuzz.c sqlite3.c $(LDFLAGS.libsqlite3) +fuzzy: ossshell$(T.exe) +xbin: ossshell$(T.exe) + +sessionfuzz$(T.exe): $(TOP)/test/sessionfuzz.c sqlite3.c sqlite3.h + $(T.link) -o $@ $(TOP)/test/sessionfuzz.c $(LDFLAGS.libsqlite3) +fuzzy: sessionfuzz$(T.exe) + +dbfuzz$(T.exe): $(TOP)/test/dbfuzz.c sqlite3.c sqlite3.h + $(T.link) -o $@ $(DBFUZZ_OPT) $(TOP)/test/dbfuzz.c sqlite3.c \ + $(LDFLAGS.libsqlite3) +fuzzy: dbfuzz$(T.exe) +xbin: dbfuzz$(T.exe) + +DBFUZZ2_OPTS = \ + -USQLITE_THREADSAFE \ + -DSQLITE_THREADSAFE=0 \ + -DSQLITE_OMIT_LOAD_EXTENSION \ + -DSQLITE_DEBUG \ + -DSQLITE_ENABLE_DBSTAT_VTAB \ + -DSQLITE_ENABLE_BYTECODE_VTAB \ + -DSQLITE_ENABLE_RTREE \ + -DSQLITE_ENABLE_FTS4 \ + -DSQLITE_ENABLE_FTS5 + +dbfuzz2$(T.exe): $(TOP)/test/dbfuzz2.c sqlite3.c sqlite3.h + $(T.cc) -I. -g -O0 \ + -DSTANDALONE -o dbfuzz2 \ + $(DBFUZZ2_OPTS) $(TOP)/test/dbfuzz2.c sqlite3.c $(LDFLAGS.libsqlite3) + mkdir -p dbfuzz2-dir + cp $(TOP)/test/dbfuzz2-seed* dbfuzz2-dir +fuzzy: dbfuzz2$(T.exe) +xbin: dbfuzz2$(T.exe) + +mptester$(T.exe): $(libsqlite3.LIB) $(TOP)/mptest/mptest.c + $(T.link) -o $@ -I. $(TOP)/mptest/mptest.c $(libsqlite3.LIB) \ + $(LDFLAGS.libsqlite3) +xbin: mptester$(T.exe) + +MPTEST1=./mptester$(T.exe) mptest.db $(TOP)/mptest/crash01.test --repeat 20 +MPTEST2=./mptester$(T.exe) mptest.db $(TOP)/mptest/multiwrite01.test --repeat 20 +mptest: mptester$(T.exe) + rm -f mptest.db + $(MPTEST1) --journalmode DELETE + $(MPTEST2) --journalmode WAL + $(MPTEST1) --journalmode WAL + $(MPTEST2) --journalmode PERSIST + $(MPTEST1) --journalmode PERSIST + $(MPTEST2) --journalmode TRUNCATE + $(MPTEST1) --journalmode TRUNCATE + $(MPTEST2) --journalmode DELETE -showjournal$(EXE): $(TOP)/tool/showjournal.c sqlite3.o - $(TCC) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -o showjournal$(EXE) \ - $(TOP)/tool/showjournal.c sqlite3.o $(THREADLIB) +# Source and header files that shell.c depends on +SHELL_DEP = \ + $(TOP)/src/shell.c.in \ + $(TOP)/ext/expert/sqlite3expert.c \ + $(TOP)/ext/expert/sqlite3expert.h \ + $(TOP)/ext/intck/sqlite3intck.c \ + $(TOP)/ext/intck/sqlite3intck.h \ + $(TOP)/ext/misc/appendvfs.c \ + $(TOP)/ext/misc/base64.c \ + $(TOP)/ext/misc/base85.c \ + $(TOP)/ext/misc/completion.c \ + $(TOP)/ext/misc/decimal.c \ + $(TOP)/ext/misc/fileio.c \ + $(TOP)/ext/misc/ieee754.c \ + $(TOP)/ext/misc/memtrace.c \ + $(TOP)/ext/misc/pcachetrace.c \ + $(TOP)/ext/misc/regexp.c \ + $(TOP)/ext/misc/series.c \ + $(TOP)/ext/misc/sha1.c \ + $(TOP)/ext/misc/shathree.c \ + $(TOP)/ext/misc/sqlar.c \ + $(TOP)/ext/misc/uint.c \ + $(TOP)/ext/misc/vfstrace.c \ + $(TOP)/ext/misc/windirent.h \ + $(TOP)/ext/misc/zipfile.c \ + $(TOP)/ext/recover/dbdata.c \ + $(TOP)/ext/recover/sqlite3recover.c \ + $(TOP)/ext/recover/sqlite3recover.h + + +shell.c: $(SHELL_DEP) $(TOP)/tool/mkshellc.tcl $(B.tclsh) + $(B.tclsh) $(TOP)/tool/mkshellc.tcl shell.c -showwal$(EXE): $(TOP)/tool/showwal.c sqlite3.o - $(TCC) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -o showwal$(EXE) \ - $(TOP)/tool/showwal.c sqlite3.o $(THREADLIB) +# +# Rules to build the extension objects. +# +DEPS_EXT_COMMON = $(DEPS_OBJ_COMMON) $(EXTHDR) +icu.o: $(TOP)/ext/icu/icu.c $(DEPS_EXT_COMMON) + $(T.cc.extension) -c $(TOP)/ext/icu/icu.c $(CFLAGS.icu) -fts3view$(EXE): $(TOP)/ext/fts3/tool/fts3view.c sqlite3.o - $(TCC) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -o fts3view$(EXE) \ - $(TOP)/ext/fts3/tool/fts3view.c sqlite3.o $(THREADLIB) +fts3.o: $(TOP)/ext/fts3/fts3.c $(DEPS_EXT_COMMON) + $(T.cc.extension) -c $(TOP)/ext/fts3/fts3.c -rollback-test$(EXE): $(TOP)/tool/rollback-test.c sqlite3.o - $(TCC) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -o rollback-test$(EXE) \ - $(TOP)/tool/rollback-test.c sqlite3.o $(THREADLIB) +fts3_aux.o: $(TOP)/ext/fts3/fts3_aux.c $(DEPS_EXT_COMMON) + $(T.cc.extension) -c $(TOP)/ext/fts3/fts3_aux.c -LogEst$(EXE): $(TOP)/tool/logest.c sqlite3.h - $(TCC) -o LogEst$(EXE) $(TOP)/tool/logest.c +fts3_expr.o: $(TOP)/ext/fts3/fts3_expr.c $(DEPS_EXT_COMMON) + $(T.cc.extension) -c $(TOP)/ext/fts3/fts3_expr.c -wordcount$(EXE): $(TOP)/test/wordcount.c sqlite3.c - $(TCC) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -o wordcount$(EXE) \ - $(TOP)/test/wordcount.c sqlite3.c +fts3_hash.o: $(TOP)/ext/fts3/fts3_hash.c $(DEPS_EXT_COMMON) + $(T.cc.extension) -c $(TOP)/ext/fts3/fts3_hash.c -speedtest1$(EXE): $(TOP)/test/speedtest1.c sqlite3.o - $(TCC) -I. $(OTAFLAGS) -o speedtest1$(EXE) $(TOP)/test/speedtest1.c sqlite3.o $(THREADLIB) +fts3_icu.o: $(TOP)/ext/fts3/fts3_icu.c $(DEPS_EXT_COMMON) + $(T.cc.extension) -c $(TOP)/ext/fts3/fts3_icu.c $(CFLAGS.icu) -rbu$(EXE): $(TOP)/ext/rbu/rbu.c $(TOP)/ext/rbu/sqlite3rbu.c sqlite3.o - $(TCC) -I. -o rbu$(EXE) $(TOP)/ext/rbu/rbu.c sqlite3.o \ - $(THREADLIB) +fts3_porter.o: $(TOP)/ext/fts3/fts3_porter.c $(DEPS_EXT_COMMON) + $(T.cc.extension) -c $(TOP)/ext/fts3/fts3_porter.c -loadfts: $(TOP)/tool/loadfts.c libsqlite3.a - $(TCC) $(TOP)/tool/loadfts.c libsqlite3.a -o loadfts $(THREADLIB) +fts3_snippet.o: $(TOP)/ext/fts3/fts3_snippet.c $(DEPS_EXT_COMMON) + $(T.cc.extension) -c $(TOP)/ext/fts3/fts3_snippet.c + +fts3_tokenizer.o: $(TOP)/ext/fts3/fts3_tokenizer.c $(DEPS_EXT_COMMON) + $(T.cc.extension) -c $(TOP)/ext/fts3/fts3_tokenizer.c + +fts3_tokenizer1.o: $(TOP)/ext/fts3/fts3_tokenizer1.c $(DEPS_EXT_COMMON) + $(T.cc.extension) -c $(TOP)/ext/fts3/fts3_tokenizer1.c + +fts3_tokenize_vtab.o: $(TOP)/ext/fts3/fts3_tokenize_vtab.c $(DEPS_EXT_COMMON) + $(T.cc.extension) -c $(TOP)/ext/fts3/fts3_tokenize_vtab.c + +fts3_unicode.o: $(TOP)/ext/fts3/fts3_unicode.c $(DEPS_EXT_COMMON) + $(T.cc.extension) -c $(TOP)/ext/fts3/fts3_unicode.c + +fts3_unicode2.o: $(TOP)/ext/fts3/fts3_unicode2.c $(DEPS_EXT_COMMON) + $(T.cc.extension) -c $(TOP)/ext/fts3/fts3_unicode2.c + +fts3_write.o: $(TOP)/ext/fts3/fts3_write.c $(DEPS_EXT_COMMON) + $(T.cc.extension) -c $(TOP)/ext/fts3/fts3_write.c + +rtree.o: $(TOP)/ext/rtree/rtree.c $(DEPS_EXT_COMMON) + $(T.cc.extension) -c $(TOP)/ext/rtree/rtree.c + +sqlite3session.o: $(TOP)/ext/session/sqlite3session.c $(DEPS_EXT_COMMON) + $(T.cc.extension) -c $(TOP)/ext/session/sqlite3session.c + +stmt.o: $(TOP)/ext/misc/stmt.c $(DEPS_EXT_COMMON) + $(T.cc.extension) -c $(TOP)/ext/misc/stmt.c -# This target will fail if the SQLite amalgamation contains any exported -# symbols that do not begin with "sqlite3_". It is run as part of the -# releasetest.tcl script. # -checksymbols: sqlite3.o - nm -g --defined-only sqlite3.o | grep -v " sqlite3_" ; test $$? -ne 0 +# Windows section +# +# 2025-03-03: sqlite3.def and sqlite3.dll might no longer be relevant +# in this particular build, but that's difficult to verify. +# +dll: sqlite3.dll +sqlite3.def: $(LIBOBJ) + echo 'EXPORTS' >sqlite3.def + nm $(LIBOBJ) | grep ' T ' | grep ' _sqlite3_' \ + | sed 's/^.* _//' >>sqlite3.def + +sqlite3.dll: $(LIBOBJ) sqlite3.def + $(T.cc.sqlite) $(LDFLAGS.shlib) -o $@ sqlite3.def \ + -Wl,"--strip-all" $(LIBOBJ) $(LDFLAGS.configure) -# Build the amalgamation-autoconf package. The amalamgation-tarball target builds -# a tarball named for the version number. Ex: sqlite-autoconf-3110000.tar.gz. -# The snapshot-tarball target builds a tarball named by the SHA1 hash # -amalgamation-tarball: sqlite3.c - TOP=$(TOP) sh $(TOP)/tool/mkautoconfamal.sh --normal +# Emit a list of commonly-used targets +# +help: + @echo; echo "Frequently-used high-level make targets:"; echo; \ + echo " - all [default] = builds most components"; \ + echo " - clean = cleans up most build products"; \ + echo " - distclean = cleans up all build products"; \ + echo " - install = installs activated components"; \ + echo; echo "Testing-related targets:"; echo; \ + echo " - test = a number of sanity checks"; \ + echo " - quicktest = minimal tests"; \ + echo " - releasetest = pre-release tests"; \ + echo " - devtest = Minimum tests required before code check-ins"; \ + echo " - mdevtest = A variant of devtest"; \ + echo " - sdevtest = A variant of devtest"; \ + echo " - tcltest = Runs test/veryquick.test"; \ + echo " - testrunner = Like tcltest but spread across multiple cores"; \ + echo " - fuzztest = The core fuzz tester (see target docs for important info)"; \ + echo " - valgrindfuzz = Runs fuzztest under valgrind"; \ + echo " - soaktest = Really, really long tests"; \ + echo " - alltest = Runs most or all TCL tests"; \ + echo -snapshot-tarball: sqlite3.c - TOP=$(TOP) sh $(TOP)/tool/mkautoconfamal.sh --snapshot + +# +# Remove build products sufficient so that subsequent makes will recompile +# everything from scratch. Do not remove: +# +# * test results and test logs +# * output from ./configure +# +# +tidy: + rm -f *.o *.obj *.c *.da *.bb *.bbg gmon.* *.rws sqlite3$(T.exe) + rm -f fts5.h keywordhash.h opcodes.h sqlite3.h sqlite3ext.h sqlite3session.h + rm -rf .libs .deps tsrc .target_source + rm -f lemon$(B.exe) sqlite*.tar.gz + rm -f mkkeywordhash$(B.exe) mksourceid$(B.exe) + rm -f parse.* fts5parse.* + rm -f $(libsqlite3.DLL) $(libsqlite3.LIB) $(libtclsqlite3.DLL) libsqlite3$(T.dll).a + rm -f tclsqlite3$(T.exe) $(TESTPROGS) + rm -f LogEst$(T.exe) fts3view$(T.exe) rollback-test$(T.exe) showdb$(T.exe) + rm -f showjournal$(T.exe) showstat4$(T.exe) showwal$(T.exe) speedtest1$(T.exe) + rm -f wordcount$(T.exe) changeset$(T.exe) version-info$(T.exe) + rm -f *.exp *.vsix pkgIndex.tcl + rm -f sqlite3_analyzer$(T.exe) sqlite3_rsync$(T.exe) sqlite3_expert$(T.exe) + rm -f mptester$(T.exe) rbu$(T.exe) srcck1$(T.exe) + rm -f fuzzershell$(T.exe) fuzzcheck$(T.exe) sqldiff$(T.exe) dbhash$(T.exe) + rm -f dbfuzz$(T.exe) dbfuzz2$(T.exe) + rm -fr dbfuzz2-dir + rm -f fuzzcheck-asan$(T.exe) fuzzcheck-ubsan$(T.exe) ossshell$(T.exe) + rm -f scrub$(T.exe) showshm$(T.exe) sqlite3_checker$(T.exe) loadfts$(T.exe) + rm -f index_usage$(T.exe) kvtest$(T.exe) startup$(T.exe) threadtest3$(T.exe) + rm -f sessionfuzz$(T.exe) changesetfuzz$(T.exe) + rm -f dbdump$(T.exe) dbtotxt$(T.exe) atrc$(T.exe) + rm -f threadtest5$(T.exe) + rm -f src-verify$(B.exe) + rm -f tclsqlite3.c has_tclsh* $(T.tcl.env.sh) + rm -f sqlite3rc.h sqlite3.def + rm -f ctime.c pragma.h + +# +# Removes build products and test logs. Retains ./configure outputs. +# +clean: tidy + rm -rf omittest* testrunner* testdir* + +# +# Clean up everything. No exceptions. From an out-of-tree build which +# starts in an empty directory, this should result in an empty +# directory (assuming the user does not create new files in this +# directory). +# +# The main distclean rules are in Makefile.in. +# +distclean: clean -# Standard install and cleanup targets -# -install: sqlite3 libsqlite3.a sqlite3.h - mv sqlite3 /usr/bin - mv libsqlite3.a /usr/lib - mv sqlite3.h /usr/include - -clean: - rm -f *.o sqlite3 sqlite3.exe libsqlite3.a sqlite3.h opcodes.* - rm -f lemon lemon.exe lempar.c parse.* sqlite*.tar.gz - rm -f mkkeywordhash mkkeywordhash.exe keywordhash.h - rm -f $(PUBLISH) - rm -f *.da *.bb *.bbg gmon.out - rm -rf quota2a quota2b quota2c - rm -rf tsrc target_source - rm -f testloadext.dll libtestloadext.so - rm -f amalgamation-testfixture amalgamation-testfixture.exe - rm -f fts3-testfixture fts3-testfixture.exe - rm -f testfixture testfixture.exe - rm -f threadtest3 threadtest3.exe - rm -f LogEst LogEst.exe - rm -f fts3view fts3view.exe - rm -f rollback-test rollback-test.exe - rm -f showdb showdb.exe - rm -f showjournal showjournal.exe - rm -f showstat4 showstat4.exe - rm -f showwal showwal.exe - rm -f speedtest1 speedtest1.exe - rm -f wordcount wordcount.exe - rm -f rbu rbu.exe - rm -f srcck1 srcck1.exe - rm -f sqlite3.c sqlite3-*.c fts?amal.c tclsqlite3.c - rm -f sqlite3rc.h - rm -f shell.c sqlite3ext.h - rm -f sqlite3_analyzer sqlite3_analyzer.exe sqlite3_analyzer.c - rm -f sqlite-*-output.vsix - rm -f mptester mptester.exe - rm -f fuzzershell fuzzershell.exe - rm -f fuzzcheck fuzzcheck.exe - rm -f sqldiff sqldiff.exe - rm -f fts5.* fts5parse.* +# +# Show important variable settings. +# +show-variables: + @echo "CC = $(CC)" + @echo "B.cc = $(B.cc)" + @echo "T.cc = $(T.cc)" + @echo "T.cc.sqlite = $(T.cc.sqlite)" diff --git a/manifest b/manifest index 6129d48a90..7be9b18d30 100644 --- a/manifest +++ b/manifest @@ -1,505 +1,932 @@ -C Version\s3.11.0 -D 2016-02-15T17:29:24.141 -F Makefile.in 4e90dc1521879022aa9479268a4cd141d1771142 -F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434 -F Makefile.msc 30f075dc4f27a07abb76088946b2944178d85347 -F README.md 8ecc12493ff9f820cdea6520a9016001cb2e59b7 -F VERSION 866588d1edf0ccb5b0d33896974338f97564f719 -F aclocal.m4 a5c22d164aff7ed549d53a90fa56d56955281f50 +C Version\s3.51.2 +D 2026-01-09T17:27:48.405 +F .fossil-settings/binary-glob 61195414528fb3ea9693577e1980230d78a1f8b0a54c78cf1b9b24d0a409ed6a x +F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 +F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea +F LICENSE.md 6bc480fc673fb4acbc4094e77edb326267dd460162d7723c7f30bee2d3d9e97d +F Makefile.in 3ce07126d7e87c7464301482e161fdae6a51d0a2aa06b200b8f0000ef4d6163b +F Makefile.linux-generic bd3e3cacd369821a6241d4ea1967395c962dfe3057e38cb0a435cee0e8b789d0 +F Makefile.msc d4459fad28b388063698cbb7a73bfce8684da998a844a04b21d4b9b10291196a +F README.md dae499194b75deed76a13a4a83c82493f2530331882d7dfe5754d63287d3f8f7 +F VERSION 53fb08d314af314f884da9b33cabad229928aac28b53984a2c38fd4d7dc608ab +F art/icon-243x273.gif 9750b734f82fdb3dc43127753d5e6fbf3b62c9f4e136c2fbf573b2f57ea87af5 +F art/icon-80x90.gif 65509ce3e5f86a9cd64fe7fca2d23954199f31fe44c1e09e208c80fb83d87031 F art/sqlite370.eps aa97a671332b432a54e1d74ff5e8775be34200c2 F art/sqlite370.ico af56c1d00fee7cd4753e8631ed60703ed0fc6e90 F art/sqlite370.jpg d512473dae7e378a67e28ff96a34da7cb331def2 -F autoconf/INSTALL 83e4a25da9fd053c7b3665eaaaf7919707915903 -F autoconf/Makefile.am 29e2a6e8d0c5e32723a48b4faf6b168854dde5f4 -F autoconf/Makefile.msc b865d2c72cf43cbf39913336415556af8ff2e819 -F autoconf/README.first 6c4f34fe115ff55d4e8dbfa3cecf04a0188292f7 -F autoconf/README.txt 7c31da66232f7590bb987cfcd4e2381744b25d24 -F autoconf/configure.ac 72a5e42beb090b32bca580285dc0ab3c4670adb8 -F autoconf/tea/Makefile.in b438a7020446c8a8156e8d97c8914a04833da6fd -F autoconf/tea/README 3e9a3c060f29a44344ab50aec506f4db903fb873 -F autoconf/tea/aclocal.m4 52c47aac44ce0ddb1f918b6993e8beb8eee88f43 -F autoconf/tea/configure.ac 93d43c79e936fb16556e22498177d7e8571efa04 -F autoconf/tea/doc/sqlite3.n e1fe45d4f5286ee3d0ccc877aca2a0def488e9bb +F art/sqlite370.svg 40b7e2fe8aac3add5d56dd86ab8d427a4eca5bcb3fe4f8946cb3794e1821d531 +F auto.def 44a0d1bf09d78355fc88251ccbf8e64e6341fd89c11de68a01c3645e53a2bade +F autoconf/Makefile.fallback 22fe523eb36dfce31e0f6349f782eb084e86a5620b2b0b4f84a2d6133f53f5ac +F autoconf/Makefile.in 118aa2c4d49173672d065fdda19eb8a28642e2c684212d7a626d6db5e6762521 +F autoconf/Makefile.msc 9c1ca648062fd5a4a83ba7590c4422090cccd6399002af0346b7572f086c7483 +F autoconf/README.first f1d3876e9a7852c22f275a6f06814e64934cecbc0b5b9617d64849094c1fd136 +F autoconf/README.txt b749816b8452b3af994dc6d607394bef3df1736d7e09359f1087de8439a52807 +F autoconf/auto.def 3d994f3a9cc9b712dbce92a5708570ddcf3b988141b6eb738f2ed16127a9f0ac +F autoconf/tea/Makefile.in 00e60cf3bf5580f31bfdcf3c914e9ba1831d676948363962de92ce65e5be4431 +F autoconf/tea/README.txt 23475876343498ef2b514cc7510e8f1559a17e8e03fbc7a41c1c8a3b89e7b7e3 +F autoconf/tea/_teaish.tester.tcl.in 8253b44be88e2e3f21de95a65d3a90c2be8e70b7bdd08a5b80e337ba7402f8f1 +F autoconf/tea/auto.def ce95b9450e2fa4ba5dc857e208fe10f4e6f2d737796ac3278aee6079db417529 +F autoconf/tea/configure 993eb27dafb35253965f9c0eb0eeefd113cae0508361c8fd90a4b58c3caf14ec x F autoconf/tea/license.terms 13bd403c9610fd2b76ece0ab50c4c5eda933d523 -F autoconf/tea/pkgIndex.tcl.in 3ef61715cf1c7bdcff56947ffadb26bc991ca39d -F autoconf/tea/tclconfig/install-sh bdd5e293591621ae60d9824d86a4b1c5f22c3d00 -F autoconf/tea/tclconfig/tcl.m4 66ddf0a5d5e4b1d29bff472c0985fd7fa89d0fb5 -F autoconf/tea/win/makefile.vc f89d0184d0eee5f7e356ea407964dcd139939928 -F autoconf/tea/win/nmakehlp.c 247538ad8e8c508f33c03ec1fbd67d3a07ef6291 -F autoconf/tea/win/rules.vc c511f222b80064096b705dbeb97060ee1d6b6d63 -F config.guess 226d9a188c6196f3033ffc651cbc9dcee1a42977 -F config.h.in 42b71ad3fe21c9e88fa59e8458ca1a6bc72eb0c0 -F config.sub 9ebe4c3b3dab6431ece34f16828b594fb420da55 -F configure 12d96e3798e612e0ffa53a7a8c4d7fb1090df80e x -F configure.ac a2224b1162f79848982d3618ac1deffcd94e88ec -F contrib/sqlitecon.tcl 210a913ad63f9f991070821e599d600bd913e0ad -F doc/lemon.html 334dbf6621b8fb8790297ec1abf3cfa4621709d1 -F doc/pager-invariants.txt 27fed9a70ddad2088750c4a2b493b63853da2710 -F doc/vfs-shm.txt e101f27ea02a8387ce46a05be2b1a902a021d37a -F ext/README.txt 913a7bd3f4837ab14d7e063304181787658b14e1 -F ext/async/README.txt e12275968f6fde133a80e04387d0e839b0c51f91 -F ext/async/sqlite3async.c 0f3070cc3f5ede78f2b9361fb3b629ce200d7d74 -F ext/async/sqlite3async.h f489b080af7e72aec0e1ee6f1d98ab6cf2e4dcef -F ext/fts1/README.txt 20ac73b006a70bcfd80069bdaf59214b6cf1db5e -F ext/fts1/ft_hash.c 3927bd880e65329bdc6f506555b228b28924921b -F ext/fts1/ft_hash.h 06df7bba40dadd19597aa400a875dbc2fed705ea -F ext/fts1/fts1.c a39f7d21c2994d27c959ef9c3505c81542c81432 -F ext/fts1/fts1.h 6060b8f62c1d925ea8356cb1a6598073eb9159a6 -F ext/fts1/fts1_hash.c 3196cee866edbebb1c0521e21672e6d599965114 -F ext/fts1/fts1_hash.h e7f0d761353996a8175eda351104acfde23afcb0 -F ext/fts1/fts1_porter.c b1c7304b8988ba3f764a147cdd32043b4913ea7b -F ext/fts1/fts1_tokenizer.h fdea722c38a9f82ed921642981234f666e47919c -F ext/fts1/fts1_tokenizer1.c fd00d1fe4dc30dfc5c64cba695ce34f4af20d2fa -F ext/fts1/fulltext.c 37698e1909584f6d8ea67d1485e3ad39dbf42d19 -F ext/fts1/fulltext.h 08525a47852d1d62a0be81d3fc3fe2d23b094efd -F ext/fts1/simple_tokenizer.c bbfa4e3b2a26ef17d4edc6d98cd4a3f5396d998a -F ext/fts1/tokenizer.h 0c53421b832366d20d720d21ea3e1f6e66a36ef9 -F ext/fts2/README.tokenizers 21e3684ea5a095b55d70f6878b4ce6af5932dfb7 -F ext/fts2/README.txt 8c18f41574404623b76917b9da66fcb0ab38328d -F ext/fts2/fts2.c 72c816a9ae448049fbbe8f18a85698765fc7956c -F ext/fts2/fts2.h da5f76c65163301d1068a971fd32f4119e3c95fa -F ext/fts2/fts2_hash.c 011a1d32de45bb1b519a1fd0048e857d6a843558 -F ext/fts2/fts2_hash.h 1824b99dfd8d0225facbdb26a2c87289b2e7dcf8 -F ext/fts2/fts2_icu.c 51c5cd3c04954badd329fa738c95fcdb717b5188 -F ext/fts2/fts2_porter.c 2cd4a507bf3c3085fe66f59b0f2a325f65aaacf5 -F ext/fts2/fts2_tokenizer.c 3dbe8058e97afb55fff3ea844120ce3208b114cc -F ext/fts2/fts2_tokenizer.h 27a1a99ca2d615cf7e142839b8d79e8751b4529e -F ext/fts2/fts2_tokenizer1.c 07e223eecb483d448313b5f1553a4f299a7fb7a1 -F ext/fts2/mkfts2amal.tcl 974d5d438cb3f7c4a652639262f82418c1e4cff0 -F ext/fts3/README.content fdc666a70d5257a64fee209f97cf89e0e6e32b51 -F ext/fts3/README.syntax a19711dc5458c20734b8e485e75fb1981ec2427a -F ext/fts3/README.tokenizers e0a8b81383ea60d0334d274fadf305ea14a8c314 +F autoconf/tea/pkgIndex.tcl.in e07da6b94561f4aa382bab65b1ccceb04701b97bf59d007c1d1f20a222b22d07 +F autoconf/tea/teaish.tcl 81feb417e718ed75cdd7e2fdf6771f3da80dae97377a90c4d5b62b3754abbf1d +F autoconf/tea/teaish.test.tcl cfe94e1fb79dd078f650295be59843d470125e0cc3a17a1414c1fb8d77f4aea6 +F autosetup/LICENSE 41a26aebdd2cd185d1e2b210f71b7ce234496979f6b35aef2cbf6b80cbed4ce4 +F autosetup/README.autosetup a78ff8c4a3d2636a4268736672a74bf14a82f42687fcf0631a70c516075c031e +F autosetup/README.md ce0f95980a687bb861bd830b76bc4b48513567be5cf5ee7004f4f3439ffe3841 +F autosetup/autosetup b16e44924c197783df67366762dda985b45d49ebc4af15f4054e3ee0e3b65169 x +F autosetup/autosetup-config.guess dfa101c5e8220e864d5e9c72a85e87110df60260d36cb951ad0a85d6d9eaa463 x +F autosetup/autosetup-config.sub a38fb074d0dece01cf919e9fb534a26011608aa8fa606490864295328526cd73 x +F autosetup/autosetup-find-tclsh b08f883f5753cfff1ecb8581f98b314e190b7e3f3059798e274ae5f5aad571af x +F autosetup/autosetup-test-tclsh 749d20defee533a3842139df47d700fc7a334a5da7bdbd444ae5331744b06c5f +F autosetup/cc-db.tcl 6e0ed90146197a5a05b245e649975c07c548e30926b218ca3e1d4dc034b10a7b +F autosetup/cc-lib.tcl 493c5935b5dd3bf9bd4eca89b07c8b1b1a9356d61783035144e21795facf7360 +F autosetup/cc-shared.tcl 163eda58c14cd662fd8a504bd2ad8a716ef4db7015dc1de0095d5de8dd601a4b +F autosetup/cc.tcl c0fcc50ca91deff8741e449ddad05bcd08268bc31177e613a6343bbd1fd3e45f +F autosetup/find_tclconfig.tcl e64886ffe3b982d4df42cd28ed91fe0b5940c2c5785e126c1821baf61bc86a7e +F autosetup/jimsh0.c a57c16e65dcffc9c76e496757cb3f7fb47e01ecbd1631a0a5e01751fc856f049 +F autosetup/pkg-config.tcl 4e635bf39022ff65e0d5434339dd41503ea48fc53822c9c5bde88b02d3d952ba +F autosetup/proj.tcl 6fc14ef82b19b77a95788ffbcfad7989b4e3cb4ce96a21dcb5cf7312f362fba9 +F autosetup/sqlite-config.tcl 5d779fce20c11fde3fe99d157dcd5b5569d729b301141b8dfb8d5aacf9d48cba +F autosetup/system.tcl 51d4be76cd9a9074704b584e5c9cbba616202c8468cf9ba8a4f8294a7ab1dba9 +F autosetup/teaish/README.txt b40071e6f8506500a2f7f71d5fc69e0bf87b9d7678dd9da1e5b4d0acbf40b1ca +F autosetup/teaish/core.tcl e014dd95900c7f9a34e8e0f460f47e94841059827bce8b4c49668b0c7ae3f1a0 +F autosetup/teaish/feature.tcl 18194fb79a24d30e5bbdeab40999616f39278b53a27525349ded033af2fd73be +F autosetup/teaish/tester.tcl 1799514c2652db49561b3386c5242b94534d1663f2cfac861a955e071895fdd0 +F configure 9a00b21dfd13757bbfb8d89b30660a89ec1f8f3a79402b8f9f9b6fc475c3303a x +F contrib/sqlitecon.tcl eb4c6578e08dd353263958da0dc620f8400b869a50d06e271ab0be85a51a08d3 +F doc/F2FS.txt c1d4a0ae9711cfe0e1d8b019d154f1c29e0d3abfe820787ba1e9ed7691160fcd +F doc/compile-for-unix.md c9dce1ddd4bf0d25efccc5c63eb047e78c01ce06a6ff29c73e0a8af4a0f4adbc +F doc/compile-for-windows.md f9e74d74da88f384edd5809f825035e071608f00f7f39c0e448df7b3982f979c +F doc/json-enhancements.md e356fc834781f1f1aa22ee300027a270b2c960122468499bf347bb123ce1ea4f +F doc/jsonb.md acd77fc3a709f51242655ad7803510c886aa8304202fa9cf2abc5f5c4e9d7ae5 +F doc/lemon.html 89ea833a6f71773ab1a9063fbb7fb9b32147bc0b1057b53ecab94a3b30c0aef5 +F doc/pager-invariants.txt 83aa3a4724b2d7970cc3f3461f0295c46d4fc19a835a5781cbb35cb52feb0577 +F doc/tcl-extension-testing.md b88861804fc1eaf83249f8e206334189b61e150c360e1b80d0dcf91af82354f5 +F doc/testrunner.md 5ee928637e03f136a25fef852c5ed975932e31927bd9b05a574424ae18c31019 +F doc/trusted-schema.md 33625008620e879c7bcfbbfa079587612c434fa094d338b08242288d358c3e8a +F doc/vdbesort-memory.md 4da2639c14cd24a31e0af694b1a8dd37eaf277aff3867e9a8cc14046bc49df56 +F doc/vfs-shm.txt 1a55f3f0e7b6745931b117ba5c9df3640d7a0536f532ef0052563100f4416f86 +F doc/wal-lock.md 7db0cd61e2000b545b78ce89b0c2a9a8dd8d64c097839258ac10d7c5c4156ec1 +F ext/README.md 6eb1ac267d917767952ed0ef63f55de003b6a5da433ce1fa389e1a9532e73132 +F ext/expert/README.md b321c2762bb93c18ea102d5a5f7753a4b8bac646cb392b3b437f633caf2020c3 +F ext/expert/expert.c d548d603a4cc9e61f446cc179c120c6713511c413f82a4a32b1e1e69d3f086a4 +F ext/expert/expert1.test 1d2da6606623b57bb47064e02140823ce1daecd4cacbf402c73ad3473d7f000c +F ext/expert/sqlite3expert.c 546010043fbec93544f762de5161b3d553165859e6bd853c4b85c05f93484260 +F ext/expert/sqlite3expert.h ca81efc2679a92373a13a3e76a6138d0310e32be53d6c3bfaedabd158ea8969b +F ext/expert/test_expert.c c395134bd6d4efa594a7d26578a1cb624c4027b79b4b5fcd44736c5ef1f5f725 +F ext/fts3/README.content b9078d0843a094d86af0d48dffbff13c906702b4c3558012e67b9c7cc3bf59ee +F ext/fts3/README.syntax b72477722e9b4fe43f8403227d790a1c94221bfad15c27863a4b36d1052e892b +F ext/fts3/README.tokenizers b92bdeb8b46503f0dd301d364efc5ef59ef9fa8e2758b8e742f39fa93a2e422d F ext/fts3/README.txt 8c18f41574404623b76917b9da66fcb0ab38328d -F ext/fts3/fts3.c e028eb13432f108d2e22cded019fc980700e4e00 +F ext/fts3/fts3.c 4f02858ab845a97bedf06e6cc1fba49a81fe5e00a26df68d0ad0f00a5814fa70 F ext/fts3/fts3.h 3a10a0af180d502cecc50df77b1b22df142817fe -F ext/fts3/fts3Int.h 89d0bd4595a0de384dac78e94b803de12586e8dd -F ext/fts3/fts3_aux.c 9edc3655fcb287f0467d0a4b886a01c6185fe9f1 -F ext/fts3/fts3_expr.c dfd571a24412779ac01f25c01d888c6ef7b2d0ef -F ext/fts3/fts3_hash.c 29b986e43f4e9dd40110eafa377dc0d63c422c60 +F ext/fts3/fts3Int.h ed9b8bc5ed5be402069651e49d4855cb849af706cf3fe68548f58a2c21eefc7f +F ext/fts3/fts3_aux.c 7eab82a9cf0830f6551ba3abfdbe73ed39e322a4d3940ee82fbf723674ecd9f3 +F ext/fts3/fts3_expr.c 5c13796638d8192c388777166075cdc8bc4b6712024cd5b72c31acdbefce5984 +F ext/fts3/fts3_hash.c d9dba473741445789330c7513d4f65737c92df23c3212784312931641814672a F ext/fts3/fts3_hash.h 39cf6874dc239d6b4e30479b1975fe5b22a3caaf -F ext/fts3/fts3_icu.c deb46f7020d87ea7a14a433fb7a7f4bef42a9652 -F ext/fts3/fts3_porter.c 3565faf04b626cddf85f03825e86056a4562c009 -F ext/fts3/fts3_snippet.c 68ae118b0f834ea53d2b89e4087fc0f0b8c4ee4e -F ext/fts3/fts3_term.c 88c55a6fa1a51ab494e33dced0401a6c28791fd7 -F ext/fts3/fts3_test.c 8a3a78c4458b2d7c631fcf4b152a5cd656fa7038 -F ext/fts3/fts3_tokenize_vtab.c a27593ab19657166f6fa5ec073b678cc29a75860 -F ext/fts3/fts3_tokenizer.c 4bd72f767f61c9ce5a7575c844e8d1ed2c3c561a +F ext/fts3/fts3_icu.c 305ce7fb6036484085b5556a9c8e62acdc7763f0f4cdf5fd538212a9f3720116 +F ext/fts3/fts3_porter.c 024417020c57dd1ab39816f5fe6cf45222a857b78a1f6412f040ada1ceabd4ff +F ext/fts3/fts3_snippet.c abe3b2998e7cb6d1ab6019f87f021758a0df3ee4010fe144a174a524cff96fe6 +F ext/fts3/fts3_term.c 6a96027ad364001432545fe43322b6af04ed28bb5619ec51af1f59d0710d6d69 +F ext/fts3/fts3_test.c cc329471e573f95a6ea9fbca87e89dcfa1d355591c80172ffcd759ac521d25d8 +F ext/fts3/fts3_tokenize_vtab.c 66eba6c2baa04b2b15e80d68341b8fd0b4d3831f6b2edb33916a2906ff2d4389 +F ext/fts3/fts3_tokenizer.c defede96b5dd5d658edfae77355b9c31ea65236eedc7bbe1adbc50d645cca5bc F ext/fts3/fts3_tokenizer.h 64c6ef6c5272c51ebe60fc607a896e84288fcbc3 -F ext/fts3/fts3_tokenizer1.c 5c98225a53705e5ee34824087478cf477bdb7004 -F ext/fts3/fts3_unicode.c a93f5edc0aff44ef8b06d7cb55b52026541ca145 -F ext/fts3/fts3_unicode2.c c3d01968d497bd7001e7dc774ba75b372738c057 -F ext/fts3/fts3_write.c f442223e4a1914dc1fc12b65af7e4f2c255fa47c +F ext/fts3/fts3_tokenizer1.c c1de4ae28356ad98ccb8b2e3388a7fdcce7607b5523738c9afb6275dab765154 +F ext/fts3/fts3_unicode.c de426ff05c1c2e7bce161cf6b706638419c3a1d9c2667de9cb9dc0458c18e226 +F ext/fts3/fts3_unicode2.c 416eb7e1e81142703520d284b768ca2751d40e31fa912cae24ba74860532bf0f +F ext/fts3/fts3_write.c 2bee1c5828f6401adffd07ca64260aeb79d64138958273a56de8fa5e8759a0c1 F ext/fts3/fts3speed.tcl b54caf6a18d38174f1a6e84219950d85e98bb1e9 -F ext/fts3/mkfts3amal.tcl 252ecb7fe6467854f2aa237bf2c390b74e71f100 -F ext/fts3/tool/fts3view.c 5d78b668f4e9598af9147f8999632599fb0d9dd5 +F ext/fts3/tool/fts3cov.sh c331d006359456cf6f8f953e37f2b9c7d568f3863f00bb5f7eb87fea4ac01b73 +F ext/fts3/tool/fts3view.c 413c346399159df81f86c4928b7c4a455caab73bfbc8cd68f950f632e5751674 F ext/fts3/unicode/CaseFolding.txt 8c678ca52ecc95e16bc7afc2dbf6fc9ffa05db8c F ext/fts3/unicode/UnicodeData.txt cd07314edb62d49fde34debdaf92fa2aa69011e7 -F ext/fts3/unicode/mkunicode.tcl 2debed3f582d77b3fdd0b8830880250021571fd8 -F ext/fts3/unicode/parseunicode.tcl da577d1384810fb4e2b209bf3313074353193e95 -F ext/fts5/extract_api_docs.tcl a36e54ec777172ddd3f9a88daf593b00848368e0 -F ext/fts5/fts5.h ff9c2782e8ed890b0de2f697a8d63971939e70c7 -F ext/fts5/fts5Int.h fa7c17e5c3ec9c8690387ff962f9dc6aee75e114 -F ext/fts5/fts5_aux.c daa57fb45216491814520bbb587e97bf81ced458 -F ext/fts5/fts5_buffer.c 4c1502d4c956cd092c89ce4480867f9d8bf325cd -F ext/fts5/fts5_config.c 35c5173cae4eb17e82164a7f5aeef56a48903079 -F ext/fts5/fts5_expr.c 8e8e4635f655133eb39018072fc0f0942a2c4337 -F ext/fts5/fts5_hash.c f3a7217c86eb8f272871be5f6aa1b6798960a337 -F ext/fts5/fts5_index.c c6cd5190c7e5dd94151cb17acd939c82e5c7be2d -F ext/fts5/fts5_main.c 0e01ead4e817483e378e7e38e6d902f50b68d29e -F ext/fts5/fts5_storage.c f8343db90d8c95a4d4b52f6676e354b4649ffd6e -F ext/fts5/fts5_tcl.c f8731e0508299bd43f1a2eff7dbeaac870768966 -F ext/fts5/fts5_test_mi.c 1ec66ffdf7632077fbd773b7a6df5153272ec070 -F ext/fts5/fts5_test_tok.c db08af63673c3a7d39f053b36fd6e065017706be -F ext/fts5/fts5_tokenize.c 2ce7b44183538ec46b7907726262ee43ffdd39a8 -F ext/fts5/fts5_unicode2.c b450b209b157d598f7b9df9f837afb75a14c24bf -F ext/fts5/fts5_varint.c a5aceacda04dafcbae725413d7a16818ecd65738 -F ext/fts5/fts5_vocab.c dba72ca393d71c2588548b51380387f6b44c77a8 -F ext/fts5/fts5parse.y 59432ea369f1aa65511bad465f55d31a22f9f223 +F ext/fts3/unicode/mkunicode.tcl cbf5f7b5c8ce8014bad731f246f2e520eece908465de4778c951ca17003381f1 +F ext/fts3/unicode/parseunicode.tcl a981bd6466d12dd17967515801c3ff23f74a281be1a03cf1e6f52a6959fc77eb +F ext/fts5/extract_api_docs.tcl 009cf59c77afa86d137b0cca3e3b1a5efbe2264faa2df233f9a7aa8563926d15 +F ext/fts5/fts5.h ff5d3cc88b29e41612bfb29eb723e29e38973de62ca75ba3e8f94ccb67f5b5f2 +F ext/fts5/fts5Int.h 8d98f8e180fe28d6067e240ed45b9011735d29d5cfb5bac194e1e376baa7c708 +F ext/fts5/fts5_aux.c da4a7a9a11ec15c6df0699d908915a209bcde48f0b04101461316b59f71abffb +F ext/fts5/fts5_buffer.c f1e6d0324d7c55329d340673befc26681a372a4d36086caa8d1ec7d7c53066c7 +F ext/fts5/fts5_config.c e7d8dd062b44a66cd77e5a0f74f23a2354cd1f3f8575afb967b2773c3384f7f8 +F ext/fts5/fts5_expr.c b8c32da1127bafaf10d6b4768b0dcb92285798524bed2d87a8686f99a8e8d259 +F ext/fts5/fts5_hash.c a6266cedd801ab7964fa9e74ebcdda6d30ec6a96107fa24148ec6b7b5b80f6e0 +F ext/fts5/fts5_index.c 4e94cec64da9a61f8763f033fee310d3ce22805e1452fd4190e3f972ec60dfb0 +F ext/fts5/fts5_main.c 42025174a556257287071e90516d3ab8115daf1dd525a301883544469a260014 +F ext/fts5/fts5_storage.c 19bc7c4cbe1e6a2dd9849ef7d84b5ca1fcbf194cefc3e386b901e00e08bf05c2 +F ext/fts5/fts5_tcl.c 7fb5a3d3404099075aaa2457307cb459bbc257c0de3dbd52b1e80a5b503e0329 +F ext/fts5/fts5_test_mi.c 4308d5658cb1f5eee5998dcbaac7d5bdf7a2ef43c8192ca6e0c843f856ccee26 +F ext/fts5/fts5_test_tok.c 3cb0a9b508b30d17ef025ccddd26ae3dc8ddffbe76c057616e59a9aa85d36f3b +F ext/fts5/fts5_tokenize.c 49aea8cc400a690a6c4f83c4cedc67f4f8830c6789c4ee343404f62bcaebca7b +F ext/fts5/fts5_unicode2.c 536a6dae41d16edadd6a6b58c56e2ebbb133f0dfe757562a2edbcdc9b8362e50 +F ext/fts5/fts5_varint.c e64d2113f6e1bfee0032972cffc1207b77af63319746951bf1d09885d1dadf80 +F ext/fts5/fts5_vocab.c 23e263ad94ac357cfffd19bd7e001c3f15c4420fb10fa35b5993142127e780e6 +F ext/fts5/fts5parse.y eb526940f892ade5693f22ffd6c4f2702543a9059942772526eac1fde256bb05 F ext/fts5/mkportersteps.tcl 5acf962d2e0074f701620bb5308155fa1e4a63ba -F ext/fts5/test/fts5_common.tcl 61ff0d1a29d98a91c4553b20b3f410d858834ee9 -F ext/fts5/test/fts5aa.test 7e814df4a0e6c22a6fe2d84f210fdc0b5068a084 -F ext/fts5/test/fts5ab.test 30325a89453280160106be411bba3acf138e6d1b -F ext/fts5/test/fts5ac.test 55cad4275a1f5acabfe14d8442a8046b47e49e5f -F ext/fts5/test/fts5ad.test 36995f0586f30f5602074e012b9224c71ec5171c -F ext/fts5/test/fts5ae.test 612dcb51f4069226791ff14c17dbfb3138c56f20 -F ext/fts5/test/fts5af.test be858a96b1f5de66ba6d64f0021bd8b2408e126c -F ext/fts5/test/fts5ag.test 27180de76c03036be75ee80b93d8c5f540014071 -F ext/fts5/test/fts5ah.test dfb7897711dbcda1dacb038aec310daca139fcf5 -F ext/fts5/test/fts5ai.test 3909d0b949b2afcaae4d5795cd79153da75381df -F ext/fts5/test/fts5aj.test 05b569f5c16ea3098fb1984eec5cf50dbdaae5d8 -F ext/fts5/test/fts5ak.test fb26389985407826f6076bb9f382c67d3db6b5d9 -F ext/fts5/test/fts5al.test 18c277f5986df0a3d9071dfd7128afeb16fe9d5d -F ext/fts5/test/fts5alter.test 6022c61467a82aa11c70822ccad22b328dcf0d04 -F ext/fts5/test/fts5auto.test 401c20e89f1114d733b94809be1e6f893e16c09e -F ext/fts5/test/fts5aux.test 8c687c948cc98e9a94be014df7d518acc1b3b74f -F ext/fts5/test/fts5auxdata.test 141a7cbffcceb1bd2799b4b29c183ff8780d586e -F ext/fts5/test/fts5bigpl.test 04ee0d7eebbebf17c31f5a0b5c5f9494eac3a0cb -F ext/fts5/test/fts5bigtok.test 981b2790f6fa02773c889bd35d42c6b97f80f0f4 -F ext/fts5/test/fts5columnsize.test a8cfef21ffa1c264b9f670a7d94eeaccb5341c07 -F ext/fts5/test/fts5config.test 8b2bc6dcc0eb06fa2b7dd65b2ce2db09e829e873 -F ext/fts5/test/fts5conflict.test 26f4e46c4d31e16221794832a990dc4e30e18de5 -F ext/fts5/test/fts5content.test 9a952c95518a14182dc3b59e3c8fa71cda82a4e1 -F ext/fts5/test/fts5corrupt.test c2ad090192708150d50d961278df10ae7a4b8b62 -F ext/fts5/test/fts5corrupt2.test 26c0a39dd9ff73207e6229f83b50b21d37c7658c -F ext/fts5/test/fts5corrupt3.test b9558d5b0ca44a8b6247fbb5d4a47592a8976892 -F ext/fts5/test/fts5detail.test ef5c690535a797413acaf5ad9b8ab5d49972df69 -F ext/fts5/test/fts5dlidx.test 13871a14641017ae42f6f1055a8067bafd44cb3d -F ext/fts5/test/fts5doclist.test 8edb5b57e5f144030ed74ec00ef6fa4294fed79b -F ext/fts5/test/fts5ea.test b01e3a18cdfabbff8104a96a5242a06a68a998a0 -F ext/fts5/test/fts5eb.test 021aa80b7ac09b964249aa32ced9ee908703e4aa -F ext/fts5/test/fts5fault1.test e09040d3e17b8c0837101e8c79c8a874c4376fb7 -F ext/fts5/test/fts5fault2.test d8c6c7f916ccbdfc10b2c69530e9dd3bc8313232 -F ext/fts5/test/fts5fault3.test d6e9577d4312e331a913c72931bf131704efc8f3 -F ext/fts5/test/fts5fault4.test 532b6dacb963016cbf7003196bd87fb366540277 -F ext/fts5/test/fts5fault5.test 10c13a783de3f42a21e3e53e123b62ed0c3a1618 -F ext/fts5/test/fts5fault6.test 9682664d679643ac6736e90c225526cc84073cda -F ext/fts5/test/fts5fault7.test cb14ea3c1f42394f06f2284abc58eecee6ff8080 -F ext/fts5/test/fts5fault8.test 430837fe6dd0511fd3aea52bd602ac02441bcb58 -F ext/fts5/test/fts5fault9.test e10e395428a9ea0596ebe752ff7123d16ab78e08 -F ext/fts5/test/fts5faultA.test fa5d59c0ff62b7125cd14eee38ded1c46e15a7ea -F ext/fts5/test/fts5full.test 6f6143af0c6700501d9fd597189dfab1555bb741 -F ext/fts5/test/fts5hash.test 06f9309ccb4d5050a131594e9e47d0b21456837d -F ext/fts5/test/fts5integrity.test f5e4f8d284385875068ad0f3e894ce43e9de835d -F ext/fts5/test/fts5matchinfo.test 86569026d20f1ed748236587ce798de8a96615f1 -F ext/fts5/test/fts5merge.test 8f3cdba2ec9c5e7e568246e81b700ad37f764367 -F ext/fts5/test/fts5merge2.test c0cb66eb38a41c26cc5848fb9e50093e0f59ac93 -F ext/fts5/test/fts5near.test b214cddb1c1f1bddf45c75af768f20145f7e71cc -F ext/fts5/test/fts5onepass.test 7ed9608e258132cb8d55e7c479b08676ad68810c -F ext/fts5/test/fts5optimize.test 42741e7c085ee0a1276140a752d4407d97c2c9f5 -F ext/fts5/test/fts5phrase.test f6d1d464da5beb25dc56277aa4f1d6102f0d9a2f -F ext/fts5/test/fts5plan.test 6a55ecbac9890765b0e16f8c421c7e0888cfe436 -F ext/fts5/test/fts5porter.test 7cdc07bef301d70eebbfa75dcaf45c3680e1d0e1 -F ext/fts5/test/fts5porter2.test 2e65633d58a1c525d5af0f6c01e5a59155bb3487 -F ext/fts5/test/fts5prefix.test efd42e00bb8e8a36383f25c838185508681c093f -F ext/fts5/test/fts5query.test f5ec25f5f2fbb70033424113cdffc101b1985a40 -F ext/fts5/test/fts5rank.test 7e9e64eac7245637f6f2033aec4b292aaf611aab -F ext/fts5/test/fts5rebuild.test 03935f617ace91ed23a6099c7c74d905227ff29b -F ext/fts5/test/fts5restart.test c17728fdea26e7d0f617d22ad5b4b2862b994c17 -F ext/fts5/test/fts5rowid.test 16908a99d6efc9ba21081b4f2b86b3fc699839a6 -F ext/fts5/test/fts5simple.test e6fe2fb10a2b9193648b32bbc2caecabdf8c333d -F ext/fts5/test/fts5simple2.test 98377ae1ff7749a42c21fe1a139c1ed312522c46 -F ext/fts5/test/fts5simple3.test 8e71733b3d1b0e695011d02c68ebc5ca40b6124e -F ext/fts5/test/fts5synonym.test 6475d189c2e20d60795808f83e36bf9318708d48 -F ext/fts5/test/fts5synonym2.test aa4c43bd3b691ff80f658cb064f5ab40690e834e -F ext/fts5/test/fts5tok1.test beb894c6f3468f10a574302f69ebe4436b0287c7 +F ext/fts5/test/fts5_common.tcl c5aa7cf7148b6dcffb5b61520ae18212baf169936af734ab265143f59db328fe +F ext/fts5/test/fts5aa.test cf4ff6180873bbc131666ba846ddd90148fcb61c20aad089711d3511cce24300 +F ext/fts5/test/fts5ab.test c7e5c1519afb20366cb40d0179897a3c39d9fc06ba6b9c286d79df0ccd97e2ee +F ext/fts5/test/fts5ac.test 4a73626de86f3d17c95738034880c4f0de8d54741fb943d819b528373657e59b +F ext/fts5/test/fts5ad.test 058e616612964e61d19f70295f0e6eaedceb4b29b1fbf4f859615ef7e779dc22 +F ext/fts5/test/fts5ae.test 3d49edbd50bb0684199a2e7568aeb30d1d29718f5c0f61751983740fa836d15f +F ext/fts5/test/fts5af.test ae81f08b8da4c5f9b3ec1ef538a4ab6b7c278e92fa9058d6dc5d842c5d9771b9 +F ext/fts5/test/fts5ag.test 6667807b5d3fbf460892e756763fbe3d87a2fffe345a06514ba010ca6f6641f7 +F ext/fts5/test/fts5ah.test e1f01314b35745a30e1b494b46045b82005d71cae74f1ebd9f1338566b77f9fc +F ext/fts5/test/fts5ai.test cbe26d78030998f535bc103f37915350b137a822c71a9db439a077d7666a3539 +F ext/fts5/test/fts5aj.test 53c8508dab4acca3e691a4c51eca4b3b018319ab8635e540103d5bbdc91543c9 +F ext/fts5/test/fts5ak.test 25e2f8afdcff30d98ca9dee8c5cacca2f26db17501c9401f16d99ee036f70e8d +F ext/fts5/test/fts5al.test f0e655606771b2b5dbaf70e7f0044d560257cf3531d5eea40df58d0d7add8c39 +F ext/fts5/test/fts5alter.test ebbee06419c2d3cee5ef7ebb5ba6a9996f1aa374035361c0acd37368cc5f64f3 +F ext/fts5/test/fts5auto.test 2278de323172ced485d2844cb1357d00036ac1665f27e70fa1a48ce57bf31c2c +F ext/fts5/test/fts5aux.test 27210687338133b1e9bc0dd669322fca59fd432439f40b126895e2d7c2f899d6 +F ext/fts5/test/fts5aux2.test 4f59ac5e7c06c430a9f4890877e10f7b4708e46897422ee6743d27b0a8d01497 +F ext/fts5/test/fts5auxdata.test 372549088ff792655f73e62b9dfaf4863ce74f5e604c06cffec0b37ce4624161 +F ext/fts5/test/fts5bigid.test 2860854c2561a57594192b00c33a29f91cb85e25f3d6c03b5c2b8f62708f39dd +F ext/fts5/test/fts5bigpl.test 8f09858aab866c33593560e6480b2b6975ae7ff29ca32ad7b77e2da61402f8ef +F ext/fts5/test/fts5bigtok.test 541119e616c637caea925a8c028c37c2c29e94383e00aa2f9198d530724b6e36 +F ext/fts5/test/fts5blob.test 9644a5f917306690e08c5f89a470a3f2489376eaa52026eeca3209d149d6af74 +F ext/fts5/test/fts5cat.test bf67dd335f964482ee658287521b81e2b88697b45eb7f73933e15f198ed447cb +F ext/fts5/test/fts5circref.test 0918c69440a73fff429bc9797b07086fc74d018eb3abb1cf9738980390bb2713 +F ext/fts5/test/fts5colset.test 544f4998cdbfe06a3123887fc0221612e8aa8192cdaff152872f1aadb10e6897 +F ext/fts5/test/fts5columnsize.test 0af91d63985afdf663455d4b572b935238380140d74079eac362760866d3297b +F ext/fts5/test/fts5config.test 017daf10d2642496e97402baa0134de8b5b46b9c37e53c229cd9ab711d21522c +F ext/fts5/test/fts5conflict.test bf6030a77dbb1bedfcc42e589ed7980846c995765d77460551e448b56d741244 +F ext/fts5/test/fts5connect.test 08030168fc96fc278fa81f28654fb7e90566f33aff269c073e19b3ae9126b2f4 +F ext/fts5/test/fts5content.test d5c0c2142e64cb305f0968de70c01f8e59dbc3ecc56520c22e739e5dd99ea3bb +F ext/fts5/test/fts5contentless.test 606f063b29ba0f46d4b79aa36cdd1ef4dab5de53eae8c881d731af75a4894aca +F ext/fts5/test/fts5contentless2.test 70ffe6c611d8f278240da56734df8a77948f04e2739b358439e9bdcf56ced35f +F ext/fts5/test/fts5contentless3.test 75eaae5ad6b284ee447788943974d323228f27cc35a1681da997135cff95bc6a +F ext/fts5/test/fts5contentless4.test ec34dc69ef474ca9997dae6d91e072906e0e9a5a4b05ea89964c863833b6eff8 +F ext/fts5/test/fts5contentless5.test 38cd0392c730dc7090c550321ce3c24ba4c392bc97308b51a4180e9959dca7b5 +F ext/fts5/test/fts5corrupt.test 237fce1c3261bb3a5bec333b0f0dbf5b105ec32627ef14cccbda3cfe13833193 +F ext/fts5/test/fts5corrupt2.test 4a03a158c2cb617c9f76d26b35c1ef2534124bc0bbddcea38dfd5b170ebea27b +F ext/fts5/test/fts5corrupt3.test e489b51b6c4ded2a808e0f78bdbe126f6d369de41a59ac2717878e37fc3db0e8 +F ext/fts5/test/fts5corrupt4.test dc08d19f5b8943e95a7778a7d8da592042504faf18dd93f68f7d7a0d7d7dd733 +F ext/fts5/test/fts5corrupt5.test 73985d4fe6d8f0d5d5c7bcf79ae7c6522c376cd6ad710a0ff2f26e0c2e222abe +F ext/fts5/test/fts5corrupt6.test 2d72db743db7b5d9c9a6d0cfef24d799ed1aa5e8192b66c40e871a37ed9eed06 +F ext/fts5/test/fts5corrupt7.test 814aab492d7a09abb5bfdd81cc66fc206d7f3868f9a3bae91876e02efc466fb3 +F ext/fts5/test/fts5corrupt8.test 0b10750caf8aa23fa1c379ca4caf6130d41454505e4d5315590f4061eedcbe44 +F ext/fts5/test/fts5corruptbig.test 9f95b40fa36e292feceab02b2ef06e21878bfa1ac7afefa138aae05518b51774 +F ext/fts5/test/fts5delete.test 2a5008f8b1174ef41d1974e606928c20e4f9da77d9f8347aed818994d89cced4 +F ext/fts5/test/fts5detail.test 54015e9c43ec4ba542cfb93268abdf280e0300f350efd08ee411284b03595cc4 +F ext/fts5/test/fts5determin.test 1b77879b2ae818b5b71c859e534ee334dac088b7cf3ff3bf76a2c82b1c788d11 +F ext/fts5/test/fts5dlidx.test a7c42b0a74dc7c8aa1a46d586e0aadda4b6cc42c24450f8d3774b21166e93159 +F ext/fts5/test/fts5doclist.test b7cb84758504519746957802db9cd31187bb4e0028b89d9087ba06e26cc4155f +F ext/fts5/test/fts5ea.test cefdf66024550fa7920c03395c71ce5046235ed1a1a7a469d79b19e7aad5afb5 +F ext/fts5/test/fts5eb.test 401f756fdb77083aeba8b696c1e0ad4d834c39dbd6f17e492bb55a2ad64b4296 +F ext/fts5/test/fts5expr.test c7e208813df7a90badc856fde3796da79569b39382e0fdb43042127f3b8e06a7 +F ext/fts5/test/fts5fault1.test d28a65caee75db6897c3cf1358c5230d3bb2a3bf7fb31062c19c7e5382b3d2bd +F ext/fts5/test/fts5fault2.test 69c8fdbef830cd0d450908d4504d5bb86609e255af99c421c20a0756251fe344 +F ext/fts5/test/fts5fault3.test da2f9e3e56ff5740d68ebdd6877c97089e7ed28ddff28a0da87a6afea27e5522 +F ext/fts5/test/fts5fault4.test a5c0e849127c24e1751bc453a817f09a1b8d460e75f9ae4764017e216a870db3 +F ext/fts5/test/fts5fault5.test a336e4e11847de24c9497f80cce18e00bb3fab7fb11f97d04eb9af898900a762 +F ext/fts5/test/fts5fault6.test 40f49976c6ca8927bf7d65d0b8df46009d7ea172e1d4050b294610e7ea0a2979 +F ext/fts5/test/fts5fault7.test 0acbec416edb24b8881f154e99c31e9ccf73f539cfcd164090be139e9e97ed4c +F ext/fts5/test/fts5fault8.test 9353fe6a2a993c3231e09c49b0f4a12c8d306319555ff2ca6672b5b86fe9b0dd +F ext/fts5/test/fts5fault9.test 098e6b894bbdf9b2192f994a30f4043673fb3f338b6b8ab1624c704422f39119 +F ext/fts5/test/fts5faultA.test be4487576bff8c22cee6597d1893b312f306504a8c6ccd3c53ca85af12290c8c +F ext/fts5/test/fts5faultB.test d606bdb8e81aaeb6f41de3fc9fc7ae315733f0903fbff05cf54f5b045b729ab5 +F ext/fts5/test/fts5faultD.test e7ed7895abfe6bc98a5e853826f6b74956e7ba7f594f1860bbf9e504b9647996 +F ext/fts5/test/fts5faultE.test 844586ce71dab4be85bb86880e87b624d089f851654cd22e4710c77eb8ce7075 +F ext/fts5/test/fts5faultF.test 4abef99f86e99d9f0c6460dd68c586a766b6b9f1f660ada55bf2e8266bd1bbc1 +F ext/fts5/test/fts5faultG.test 0544411ffcb3e19b42866f757a8a5e0fb8fef3a62c06f61d14deebc571bb7ea9 +F ext/fts5/test/fts5faultH.test 2b2b5b8cb1b3fd7679f488c06e22af44107fbc6137eaf45b3e771dc7b149312d +F ext/fts5/test/fts5faultI.test 4e3d5a9d3e3b3f17d5e5087ee069414632667719dcfccafd715bc87c72838c72 +F ext/fts5/test/fts5first.test bfd685b96905bf541d99d8644e0a7219d1d833455a08ab64e344071a613b6ba9 +F ext/fts5/test/fts5full.test 97d263c1072f4a560929cca31e70f65d2ae232610e17e6affcf7e979df59547b +F ext/fts5/test/fts5fuzz1.test 238d8c45f3b81342aa384de3e581ff2fa330bf922a7b69e484bbc06051a1080e +F ext/fts5/test/fts5hash.test fd3e0367fbf0b0944d6936fdb22696350f57b9871069c6766251578a103e8a14 +F ext/fts5/test/fts5integrity.test c423ce16fd1ccadcac7fc22f794226b2bb00f5a187c0ab1d9f8502521b1bae05 +F ext/fts5/test/fts5integrity2.test 4c3636615c0201232c44a8105d5cb14fd5499fd0ee3014d7ffd7e83aac76ece8 +F ext/fts5/test/fts5interrupt.test 20d04204d3e341b104c0c24a41596b6393a3a81eba1044c168db0e106f9ac92c +F ext/fts5/test/fts5join.test 48b7ed36956948c5b8456c8bcaa5b087808d99000675918a43c4f51a925f1514 +F ext/fts5/test/fts5lastrowid.test f36298a1fb9f988bde060a274a7ce638faa9c38a31400f8d2d27ea9373e0c4a1 +F ext/fts5/test/fts5leftjoin.test 1c14b51f4d1344a89e488160882f05a2246dd7e70c5cf077c8fb473e03c66338 +F ext/fts5/test/fts5limits.test 8ab67cf5d311c124b6ceb0062d0297767176df4572d955fce79fa43004dff01c +F ext/fts5/test/fts5locale.test 83ba7ee12628b540d3098f39c39c1de0c0440eddff8f7512c8c698d0c4a3ae3c +F ext/fts5/test/fts5matchinfo.test bc9e74157773db7f00aec1e85587f1145956ebdf1672c136f0f04323b2752aa0 +F ext/fts5/test/fts5merge.test 088133e135ef7dcd6701753c95b8b10be3c52fa1a99507933e00756d6437489e +F ext/fts5/test/fts5merge2.test 3ebad1a59d6ad3fb66eff6523a09e95dc6367cbefb3cd73196801dea0425c8e2 +F ext/fts5/test/fts5misc.test 83d6c5101a092c5db8fb631cfdd69a6482e20528b2750427641ac9050d9d0381 +F ext/fts5/test/fts5multi.test a15bc91cdb717492e6e1b66fec1c356cb57386b980c7ba5af1915f97fe878581 +F ext/fts5/test/fts5multiclient.test 5ff811c028d6108045ffef737f1e9f05028af2458e456c0937c1d1b8dea56d45 +F ext/fts5/test/fts5near.test 33d60867581066e5db7016deb5d651628125d7ff4e0233a88175aa5b65874c74 +F ext/fts5/test/fts5onepass.test b56d4109e841c2bc83555c162515748780ea6e0c455c54cf4afd4bd940d14b84 +F ext/fts5/test/fts5optimize.test 264b9101721c17d06d1d174feb743fda3ddc89fad41dee980fef821428258e47 +F ext/fts5/test/fts5optimize2.test 795d4ae5f66a7239cf8d5aef4c2ea96aeb8bcd907bd9be0cfe22064fc71a44ed +F ext/fts5/test/fts5optimize3.test 1653029284e10e0715246819893ba30565c4ead0d0fc470adae92c353ea857d3 +F ext/fts5/test/fts5origintext.test 3b73aa036ce5244bb7c5782c5441b979585bdca026accf75d16026a2a8119c09 +F ext/fts5/test/fts5origintext2.test f4505ff79bf7369f2b8b10b9cef7476049d844e20b37f29cad3a8b8d5ac6f9ba +F ext/fts5/test/fts5origintext3.test 4988b6375acc3bbb0515667765f57e389caf449814af9c1095c053f7de2b4223 +F ext/fts5/test/fts5origintext4.test 0d3ef0a8038f471dbc83001c34fe5f7ae39b571bfc209670771eb28bc0fc50e8 +F ext/fts5/test/fts5origintext5.test ee12b440ec335e5b422d1668aca0051b52ff28b6ee67073e8bbc29f509fd562b +F ext/fts5/test/fts5origintext6.test 09eb1347cb0dceaebbebf3d3e6bd5d24c7c1006efddc2984540450324bbdafa4 +F ext/fts5/test/fts5phrase.test bb2554bb61d15f859678c96dc89a7de415cd5fc3b7b54c29b82a0d0ad138091c +F ext/fts5/test/fts5plan.test f8b0d752a818059a934cdc96c0f77de058a67a0a57bb3a8181d28307ab5b1626 +F ext/fts5/test/fts5porter.test 15b514fac8690b58e99c330efe5bf5615bc43f2fae4a3cca3f923dbaff55a0c0 +F ext/fts5/test/fts5porter2.test 94f0e4351e2c99b4e74f1fae05a4ddf1cb5b926620a8c14554160d075ddc7a59 +F ext/fts5/test/fts5prefix.test c0b7842f1a2d830c0b146cd438a95ea4c5a25635719ed0d973ffe41907338b83 +F ext/fts5/test/fts5prefix2.test a5bb43b8a2687efafa7ac4e5ccff6812015cf8cf18e3086bb0eb3126f30fbbf6 +F ext/fts5/test/fts5query.test 0320a7a4b58a6e3e50ec8910b301649da90ace675001f9e0bf6392750ad4591d +F ext/fts5/test/fts5rank.test 47c1e8e5d84754ff18e012fdd629776088b5a15de41bdd24957581cf084d8a00 +F ext/fts5/test/fts5rebuild.test dc09779fbbe151ab68206a0931c10a611912a7a12c7a85d71c5e48453f2375a5 +F ext/fts5/test/fts5restart.test 9af2084b8e065130037b95f05f3f220bb7973903a7701e2c5fb916dff7cf80c5 +F ext/fts5/test/fts5rowid.test 8632829fec04996832a4cfb4f0bd89721ba65b7e398c1731741bdb63f070e1a3 +F ext/fts5/test/fts5savepoint.test 1447758d7900afe903cef08b4524c5331fb60c1126ae6fba7f4d8704268013c5 +F ext/fts5/test/fts5secure.test a02f771742fb2b1b9bdcb4bf523bcf2d0aa1ff597831d40fe3e72aaa6d0ec40f +F ext/fts5/test/fts5secure2.test 2e961d7eef939f294c56b5d895cac7f1c3a60b934ee2cfd5e5e620bdf1ba6bbc +F ext/fts5/test/fts5secure3.test 6d066828d225b0dbe5db818d4d6165df7bb70210e68a577e858e8762400d5a23 +F ext/fts5/test/fts5secure4.test 0d10a80590c07891478700af7793b232962042677432b9846cf7fc8337b67c97 +F ext/fts5/test/fts5secure5.test c07a68ced5951567ac116c22f2d2aafae497e47fe9fcb6a335c22f9c7a4f2c3a +F ext/fts5/test/fts5secure6.test 74bf04733cc523bccca519bb03d3b4e2ed6f6e3db7c59bf6be82c88a0ac857fd +F ext/fts5/test/fts5secure7.test fd03d0868d64340a1db8615b02e5508fea409de13910114e4f19eaefc120777a +F ext/fts5/test/fts5secure8.test 808ade9d172ed07b24b85c57dd53b6d2b1aba018b4e634d267ce572221de80e0 +F ext/fts5/test/fts5securefault.test c34a28c7cd2f31a8b8907563889e1329a97da975c08df2d951422bcef8e2ebc5 +F ext/fts5/test/fts5simple.test 302cdb4f8a3350b091f4f1bccd82d05610428657f6f9e81c17703ba48267ec40 +F ext/fts5/test/fts5simple2.test d10d963a357b8ec77b99032e4c816459b4dbdb1f6eee25eada7ef3ed245cb2dc +F ext/fts5/test/fts5simple3.test 4e03b82e669dc07bf9c9a04c306fa493764bc49c93e539896d87d88bd374fece +F ext/fts5/test/fts5synonym.test becc8cea6cfc958a50b30c572c68cbfdf7455971d0fe988202ce67638d2c6cf6 +F ext/fts5/test/fts5synonym2.test 58f357b997cf2fedeeb9d0de4db9f880fa96fa2fe27a743bfe7d7b96895bdd87 +F ext/fts5/test/fts5tok1.test 1f7817499f5971450d8c4a652114b3d833393c8134e32422d0af27884ffe9cef F ext/fts5/test/fts5tok2.test dcacb32d4a2a3f0dd3215d4a3987f78ae4be21a2 -F ext/fts5/test/fts5tokenizer.test ea4df698b35cc427ebf2ba22829d0e28386d8c89 -F ext/fts5/test/fts5unicode.test fbef8d8a3b4b88470536cc57604a82ca52e51841 -F ext/fts5/test/fts5unicode2.test c1dd890ba32b7609adba78e420faa847abe43b59 -F ext/fts5/test/fts5unicode3.test 35c3d02aa7acf7d43d8de3bfe32c15ba96e8928e -F ext/fts5/test/fts5unindexed.test e9539d5b78c677315e7ed8ea911d4fd25437c680 -F ext/fts5/test/fts5update.test 57c7012a7919889048947addae10e0613df45529 -F ext/fts5/test/fts5version.test 978f59541d8cef7e8591f8be2115ec5ccb863e2e -F ext/fts5/test/fts5vocab.test 480d780aa6b699816c5066225fbd86f3a0239477 +F ext/fts5/test/fts5tokendata.test 7cad79af82e8e81b7a36b450087233d2fca051bb0d584421afc375d6dd26d6f6 +F ext/fts5/test/fts5tokenizer.test 7937cec672b148223fff8746d21d3e7ed0965fd7caf35ccdc888a005bb452f98 +F ext/fts5/test/fts5tokenizer2.test ddb8b10fbe4b84b2a75812671f127774c1d2e3e2bf82d2e0e4f0bb1cd8a2b2d6 +F ext/fts5/test/fts5tokenizer3.test eea778f7bb7024c3e904e28915f9d53286141671b138722148be22a9c758bdc3 +F ext/fts5/test/fts5trigram.test a55fde7065ae69a0f82c5a7a5bf5286a97de11ae4bff6537fd3e27ca9a01416f +F ext/fts5/test/fts5trigram2.test 6fde9de7f63a6b4aa18dc731be56dbd6be4e755c9b13dcd55479e200d1df0e61 +F ext/fts5/test/fts5ubsan.test 9a2dcf399dc8d0e0de661f0d93884d1d27e5b7f0693cfceb97dd24d818df5dd2 +F ext/fts5/test/fts5umlaut.test a42fe2fe6387c40c49ab27ccbd070e1ae38e07f38d05926482cc0bccac9ad602 +F ext/fts5/test/fts5unicode.test 41898f7e476e6515cd4b737c02a442cda5a580a74509788aa9072a2074948e0e +F ext/fts5/test/fts5unicode2.test 3bbd30152f9f760bf13886e5b1e5ec23ff62f56758ddda5d9c775a6082fb4c7c +F ext/fts5/test/fts5unicode3.test f4891a3dac3b49c3d7c0fdb29566e9eb0ecff35263370c89f9661b1952b20818 +F ext/fts5/test/fts5unicode4.test 6d70dbe56e5179bb1990cfb22e62fdf2aae9458e443ade856e598ce95832fe9b +F ext/fts5/test/fts5unindexed.test 168838d2c385e131120bbf5b516d2432a5fabc4caa2259c932e1d49ae209a4ae +F ext/fts5/test/fts5unindexed2.test 516236eceaac05ace322290a0d3705b4c4ffe4760d8eb9d014d9d27d56dfcc02 +F ext/fts5/test/fts5update.test b8affd796e45c94a4d19ad5c26606ea06065a0f162a9562d9f005b5a80ccf0bc +F ext/fts5/test/fts5update2.test c5baa76799ac605ebb8e5e21035db2014b396cef25c903eb96ba39b1d6f9f046 +F ext/fts5/test/fts5version.test 44ab35566267b7618c090443de2d9ad84f633df5d20bf72e9bad199ae5fced84 +F ext/fts5/test/fts5vocab.test 2a2bdb60d0998fa3124d541b6d30b019504918dc43a6584645b63a24be72f992 +F ext/fts5/test/fts5vocab2.test 4265137a3747b27deb1e2e2bde5654120c6de72bfed3238e67806d85af60fc4c F ext/fts5/tool/fts5speed.tcl b0056f91a55b2d1a3684ec05729de92b042e2f85 -F ext/fts5/tool/fts5txt2db.tcl 1343745b89ca2a1e975c23f836d0cee410052975 +F ext/fts5/tool/fts5txt2db.tcl c0d43c8590656f8240e622b00957b3a0facc49482411a9fdc2870b45c0c82f9f F ext/fts5/tool/loadfts5.tcl 95b03429ee6b138645703c6ca192c3ac96eaf093 -F ext/fts5/tool/mkfts5c.tcl d1c2a9ab8e0ec690a52316f33dd9b1d379942f45 +F ext/fts5/tool/mkfts5c.tcl 135b9e160f8e10211c10c5873d5e8c3eaebd3da9ec56a12ae4db157d4738ffe4 F ext/fts5/tool/showfts5.tcl d54da0e067306663e2d5d523965ca487698e722c -F ext/icu/README.txt d9fbbad0c2f647c3fdf715fc9fd64af53aedfc43 -F ext/icu/icu.c b2732aef0b076e4276d9b39b5a33cec7a05e1413 -F ext/icu/sqliteicu.h 728867a802baa5a96de7495e9689a8e01715ef37 -F ext/misc/amatch.c a1a8f66c29d40bd71b075546ddeddb477b17a2bb -F ext/misc/closure.c 0d2a038df8fbae7f19de42e7c7d71f2e4dc88704 -F ext/misc/compress.c 122faa92d25033d6c3f07c39231de074ab3d2e83 -F ext/misc/eval.c f971962e92ebb8b0a4e6b62949463ee454d88fa2 -F ext/misc/fileio.c d4171c815d6543a9edef8308aab2951413cd8d0f -F ext/misc/fuzzer.c 4c84635c71c26cfa7c2e5848cf49fe2d2cfcd767 -F ext/misc/ieee754.c f190d0cc5182529acb15babd177781be1ac1718c -F ext/misc/json1.c b9c88d5c3b6ecd8c731ffdd7f5b3d902857f8c96 -F ext/misc/nextchar.c 35c8b8baacb96d92abbb34a83a997b797075b342 -F ext/misc/percentile.c bcbee3c061b884eccb80e21651daaae8e1e43c63 -F ext/misc/regexp.c af92cdaa5058fcec1451e49becc7ba44dba023dc -F ext/misc/rot13.c 1ac6f95f99b575907b9b09c81a349114cf9be45a -F ext/misc/series.c b8fb7befd85b3a9b4a10e701b30b2b79ca92b6d4 +F ext/icu/README.txt 1f8d76e10d2385fc77914a14ccd99acfbaf68111dfcf26a360ad9063787f57fb +F ext/icu/icu.c 9837f4611915baad1edbe38222f3ee7d1b5e118ab16fec9ba603720f72c78b2a +F ext/icu/sqliteicu.h fa373836ed5a1ee7478bdf8a1650689294e41d0c89c1daab26e9ae78a32075a8 +F ext/intck/intck1.test 53d885075abeb45aeb1eeffeaa8560b329060835ade4af5c44cf5fcb581c1e63 +F ext/intck/intck2.test a29343a8e65c5c3400e10747f394924f3df95a5b2de94f46e9b5c9b97f5e7339 +F ext/intck/intck_common.tcl a61fd2697ae55b0a3d89847ca0b590c6e0d8ff64bebb70920d93724799894159 +F ext/intck/intckbusy.test d5ed4ef85a4b1dc1dee2484bd14a4bb68529659cca743327df0c775f005fa387 +F ext/intck/intckcorrupt.test f6c302792326fb3db9dcfc70b554c55369bc4b52882eaaf039cfe0b74c821029 +F ext/intck/intckfault.test cff3f75dff74abb3edfcb13f6aa53f6436746ab64b09fe5e2028f051e985efab +F ext/intck/sqlite3intck.c b1c8a86f90fc00741d13314db9c58f7e2f92d1d19c5ad1c6904ec83a6bbd5c96 +F ext/intck/sqlite3intck.h 2b40c38e7063ab822c974c0bd4aed97dabb579ccfe2e180a4639bb3bbef0f1c9 +F ext/intck/test_intck.c 4f9eaadaedccb9df1d26ba41116a0a8e5b0c5556dc3098c8ff68633adcccdea8 +F ext/jni/GNUmakefile 8a94e3a1953b88cf117fb2a5380480feada8b4f5316f02572cab425030a720b4 +F ext/jni/README.md e3fbd47c774683539b7fdc95a667eb9cd6e64d8510f3ee327e7fa0c61c8aa787 +F ext/jni/jar-dist.make 030aaa4ae71dd86e4ec5e7c1e6cd86f9dfa47c4592c070d2e35157e42498e1fa +F ext/jni/src/c/sqlite3-jni.c 3d84a0176af779737ae977ba1c90d2ffe2537b8299c5d9f6552620493f12ac4b +F ext/jni/src/c/sqlite3-jni.h ac180ba9b21978727006c790d3006a95a2402a4c3ec7a0def92707ed89b79945 +F ext/jni/src/org/sqlite/jni/annotation/Experimental.java 8603498634e41d0f7c70f661f64e05df64376562ea8f126829fd1e0cdd47e82b +F ext/jni/src/org/sqlite/jni/annotation/NotNull.java be6cc3e8e114485822331630097cc0f816377e8503af2fc02f9305ff2b353917 +F ext/jni/src/org/sqlite/jni/annotation/Nullable.java 56e3dee1f3f703a545dfdeddc1c3d64d1581172b1ad01ffcae95c18547fafd90 +F ext/jni/src/org/sqlite/jni/annotation/package-info.java 977b374aed9d5853cbf3438ba3b0940abfa2ea4574f702a2448ee143b98ac3ca +F ext/jni/src/org/sqlite/jni/capi/AbstractCollationCallback.java 1afa90d3f236f79cc7fcd2497e111992644f7596fbc8e8bcf7f1908ae00acd6c +F ext/jni/src/org/sqlite/jni/capi/AggregateFunction.java 0e28a0df51368c7127e505f1e9acd92a7e66e035bcd6288463aa691cb300c9af +F ext/jni/src/org/sqlite/jni/capi/AuthorizerCallback.java c045a5b47e02bb5f1af91973814a905f12048c428a3504fbc5266d1c1be3de5a +F ext/jni/src/org/sqlite/jni/capi/AutoExtensionCallback.java 74cc4998a73d6563542ecb90804a3c4f4e828cb4bd69e61226d1a51f4646e759 +F ext/jni/src/org/sqlite/jni/capi/BusyHandlerCallback.java 7b8e19810c42b0ad21a04b5d8c804b32ee5905d137148703f16a75b612c380ca +F ext/jni/src/org/sqlite/jni/capi/CApi.java 3d275f5f4fbdbe4fff15f4d42cf5ff559f9a4897e7373fa99f3b1dc9cf7f849c +F ext/jni/src/org/sqlite/jni/capi/CallbackProxy.java 1b3baf5b772f266e8baf8f35f0ddc5bd87fc8c4927ec69115c46fd6fba6701c3 +F ext/jni/src/org/sqlite/jni/capi/CollationCallback.java e29bcfc540fdd343e2f5cca4d27235113f2886acb13380686756d5cabdfd065a +F ext/jni/src/org/sqlite/jni/capi/CollationNeededCallback.java 5bfa226a8e7a92e804fd52d6e42b4c7b875fa7a94f8e2c330af8cc244a8920ab +F ext/jni/src/org/sqlite/jni/capi/CommitHookCallback.java 482f53dfec9e3ac2a9070d3fceebd56250932aaaf7c4f5bc8de29fc011416e0c +F ext/jni/src/org/sqlite/jni/capi/ConfigLogCallback.java b995ca412f59b631803b93aa5b3684fce62e335d1e123207084c054abfd488d4 +F ext/jni/src/org/sqlite/jni/capi/ConfigSqlLogCallback.java e5723900b6458bc6288f52187090a78ebe0a20f403ac7c887ec9061dfe51aba7 +F ext/jni/src/org/sqlite/jni/capi/NativePointerHolder.java b7036dcb1ef1b39f1f36ac605dde0ff1a24a9a01ade6aa1a605039443e089a61 +F ext/jni/src/org/sqlite/jni/capi/OutputPointer.java 418a82e705ec80b0dabffacfe11b9fab3cb5f2215dcafcfec083eebf5bce9d20 +F ext/jni/src/org/sqlite/jni/capi/PrepareMultiCallback.java 621cd9d887956fb2c99c9be59ee831c00a0b538dbae7313c525cd2936b5d5647 +F ext/jni/src/org/sqlite/jni/capi/PreupdateHookCallback.java efcf57545c5e282d1dd332fa63329b3b218d98f356ef107a9dbe3979be82213a +F ext/jni/src/org/sqlite/jni/capi/ProgressHandlerCallback.java 01bc0c238eed2d5f93c73522cb7849a445cc9098c2ed1e78248fa20ed1cfde5b +F ext/jni/src/org/sqlite/jni/capi/ResultCode.java 8141171f1bcf9f46eef303b9d3c5dc2537a25ad1628f3638398d8a60cacefa7f +F ext/jni/src/org/sqlite/jni/capi/RollbackHookCallback.java e172210a2080e851ebb694c70e9f0bf89284237795e38710a7f5f1b61e3f6787 +F ext/jni/src/org/sqlite/jni/capi/SQLFunction.java 0d1e9afc9ff8a2adb94a155b72385155fa3b8011a5cca0bb3c28468c7131c1a5 +F ext/jni/src/org/sqlite/jni/capi/SQLTester.java 3c0babc067d8560627a9ed1b07979f9d4393464e2282c2fca4832052e982c7bc +F ext/jni/src/org/sqlite/jni/capi/ScalarFunction.java 93b9700fca4c68075ccab12fe0fbbc76c91cafc9f368e835b9bd7cd7732c8615 +F ext/jni/src/org/sqlite/jni/capi/TableColumnMetadata.java 9133bb7685901d2edf07801191284975e33b5583ce09dce1c05202ff91e7bb99 +F ext/jni/src/org/sqlite/jni/capi/Tester1.java 4c3d16fdf6e979f839b2ecdb14d0a0c04bd3d0e41500fc9e8110b588883b140b +F ext/jni/src/org/sqlite/jni/capi/TraceV2Callback.java 0a25e117a0daae3394a77f24713e36d7b44c67d6e6d30e9e1d56a63442eef723 +F ext/jni/src/org/sqlite/jni/capi/UpdateHookCallback.java c8bdf7848e6599115d601bcc9427ff902cb33129b9be32870ac6808e04b6ae56 +F ext/jni/src/org/sqlite/jni/capi/ValueHolder.java 2ce069f3e007fdbbe1f4e507a5a407fc9679da31a0aa40985e6317ed4d5ec7b5 +F ext/jni/src/org/sqlite/jni/capi/WindowFunction.java caf4396f91b2567904cf94bc538a069fd62260d975bd037d15a02a890ed1ef9e +F ext/jni/src/org/sqlite/jni/capi/XDestroyCallback.java 9c8cc33995a3df385feaf476a8306d29dbab611ed4f55da736597357bde68620 +F ext/jni/src/org/sqlite/jni/capi/package-info.java 08ff986a65d2be9162442c82d28a65ce431d826f188520717c2ecb1484d0a50e +F ext/jni/src/org/sqlite/jni/capi/sqlite3.java c6a5c555d163d76663534f2b2cce7cab15325b9852d0f58c6688a85e73ae52f0 +F ext/jni/src/org/sqlite/jni/capi/sqlite3_backup.java 6742b431cd4d77e8000c1f92ec66265a58414c86bf3b0b5fbcb1164e08477227 +F ext/jni/src/org/sqlite/jni/capi/sqlite3_blob.java 59e26ca5254cd4771f467237bcfe2d8deed30a77152fabcd4574fd406c301d63 +F ext/jni/src/org/sqlite/jni/capi/sqlite3_context.java f0ef982009c335c4393ffcb68051809ca1711e4f47bcb8d1d46952f22c01bc22 +F ext/jni/src/org/sqlite/jni/capi/sqlite3_stmt.java 293b5fa7d5b5724c87de544654aca1103d76f3092bc2c8f4360102a65ba25dff +F ext/jni/src/org/sqlite/jni/capi/sqlite3_value.java e1d62a257c13504b46d39d5c21c49cf157ad73fda00cc5f34c931aa008c37049 +F ext/jni/src/org/sqlite/jni/fts5/Fts5.java e94681023785f1eff5399f0ddc82f46b035977d350f14838db659236ebdf6b41 +F ext/jni/src/org/sqlite/jni/fts5/Fts5Context.java 338637e6e5a2cc385d962b220f3c1f475cc371d12ae43d18ef27327b6e6225f7 +F ext/jni/src/org/sqlite/jni/fts5/Fts5ExtensionApi.java 2de029b3a12b16f779f4df6381c5f0cd358dd82bdaf99ec837504a1110b829f3 +F ext/jni/src/org/sqlite/jni/fts5/Fts5PhraseIter.java 28045042d593a1f1b9b80d54ec77cbf1d8a1bc95e442eceefa9a3a6f56600b0e +F ext/jni/src/org/sqlite/jni/fts5/Fts5Tokenizer.java 3c8f677ffb85b8782f865d6fcbc16200b3375d0e3c29ed541a494fde3011bf49 +F ext/jni/src/org/sqlite/jni/fts5/TesterFts5.java 793a5f6f2f581034dc3b503d5023b05e7e38558a80542148b82527dc2a7bf209 +F ext/jni/src/org/sqlite/jni/fts5/XTokenizeCallback.java 1efd1220ea328a32f2d2a1b16c735864159e929480f71daad4de9d5944839167 +F ext/jni/src/org/sqlite/jni/fts5/fts5_api.java a8e88c3783d21cec51b0748568a96653fead88f8f4953376178d9c7385b197ea +F ext/jni/src/org/sqlite/jni/fts5/fts5_extension_function.java 1e721873c62df2eb79f45bbf55b8662625365885b02d1d51915f773de16a90e3 +F ext/jni/src/org/sqlite/jni/fts5/fts5_tokenizer.java 92bdaa3893bd684533004d64ade23d329843f809cd0d0f4f1a2856da6e6b4d90 +F ext/jni/src/org/sqlite/jni/test-script-interpreter.md d7987b432870d23f7c72a7804d099fe5ccfb945f519ac90a33e189297fbbfa1c +F ext/jni/src/org/sqlite/jni/wrapper1/AggregateFunction.java d5c108b02afd3c63c9e5e53f71f85273c1bfdc461ae526e0a0bb2b25e4df6483 +F ext/jni/src/org/sqlite/jni/wrapper1/ScalarFunction.java 326ffba29aab836a6ea189703c3d7fb573305fd93da2d14b0f9e9dcf314c8290 +F ext/jni/src/org/sqlite/jni/wrapper1/SqlFunction.java e920f7a031e04975579240d4a07ac5e4a9d0f8de31b0aa7a4be753c98ae596c9 +F ext/jni/src/org/sqlite/jni/wrapper1/Sqlite.java c82bc00c1988f86246a89f721d3c41f0d952f33f934aa6677ec87f7ca42519a0 +F ext/jni/src/org/sqlite/jni/wrapper1/SqliteException.java 982538ddb4c0719ef87dfa664cd137b09890b546029a7477810bd64d4c47ee35 +F ext/jni/src/org/sqlite/jni/wrapper1/Tester2.java 08f92d52be2cec28a7b4555479cc54b7ebeeb94985256144eeb727154ec3f85b +F ext/jni/src/org/sqlite/jni/wrapper1/ValueHolder.java a84e90c43724a69c2ecebd601bc8e5139f869b7d08cb705c77ef757dacdd0593 +F ext/jni/src/org/sqlite/jni/wrapper1/WindowFunction.java c7d1452f9ff26175b3c19bbf273116cc2846610af68e01756d755f037fe7319f +F ext/jni/src/tests/000-000-sanity.test c3427a0e0ac84d7cbe4c95fdc1cd4b61f9ddcf43443408f3000139478c4dc745 +F ext/jni/src/tests/000-001-ignored.test e17e874c6ab3c437f1293d88093cf06286083b65bf162317f91bbfd92f961b70 +F ext/jni/src/tests/900-001-fts.test bf0ce17a8d082773450e91f2388f5bbb2dfa316d0b676c313c637a91198090f0 +F ext/misc/README.md 6243cdc4d7eb791c41ef0716f3980b8b5f6aa8c61ff76a3958cbf0031c6ebfa7 +F ext/misc/amatch.c 0e0124c1e03ee4cb99b25969f6b7b39c53a847b8bf12279efbcb896b0df1059a +F ext/misc/anycollseq.c 5ffdfde9829eeac52219136ad6aa7cd9a4edb3b15f4f2532de52f4a22525eddb +F ext/misc/appendvfs.c 9642c7a194a2a25dca7ad3e36af24a0a46d7702168c4ad7e59c9f9b0e16a3824 +F ext/misc/base64.c 8dc0a08cee11722822858a62625f1b63e5d5f1adac1cf4492d5732b571e37aa0 +F ext/misc/base85.c ff54cc676c6ec86231f75ecc86ea45416fcb69751dfb79690d5f5da5f7d39867 +F ext/misc/basexx.c 89ad6b76558efbceb627afd5e2ef1d84b2e96d9aaf9b7ecb20e3d00b51be6fcf +F ext/misc/blobio.c a867c4c4617f6ec223a307ebfe0eabb45e0992f74dd47722b96f3e631c0edb2a +F ext/misc/btreeinfo.c 8f5e6da2c82ec2f06ee0216e922370a436dafdbb06ffa7a552203515ff9e7ddf +F ext/misc/cksumvfs.c 9d7d0cf1a8893ac5d48922bfe9f3f217b4a61a6265f559263a02bb2001259913 +F ext/misc/closure.c 5559daf1daf742228431db929d1aa86dd535a4224cc634a81d2fd0d1e6ad7839 +F ext/misc/completion.c c27b64fdd0943c1b7f152376599814cee2641f7d67a7bb9bd2b957c2a64a5591 +F ext/misc/compress.c 8191118b9b73e7796c961790db62d35d9b0fb724b045e005a5713dc9e0795565 +F ext/misc/csv.c d9dab032420d81cdf0abc4b8523711d20a355704f82d958f963c86be48387dd9 +F ext/misc/dbdump.c 678f1b9ae2317b4473f65d03132a2482c3f4b08920799ed80feedd2941a06680 +F ext/misc/decimal.c d4883de142f6dcd36eda23da40b55e2b51374e7b01eb54a7173940191389fc5e +F ext/misc/eval.c 04bc9aada78c888394204b4ed996ab834b99726fb59603b0ee3ed6e049755dc1 +F ext/misc/explain.c 606100185fb90d6a1eade1ed0414d53503c86820d8956a06e3b0a56291894f2b +F ext/misc/fileio.c d80268a5328fe839062a9d3103ea0fc7cacc6d42605959275675cb37867c84f7 +F ext/misc/fossildelta.c 2fc2dd4f34f478df674887db62586b1071c4cd3c9e73ee40f9ee669670e482d1 +F ext/misc/fuzzer.c 6b231352815304ba60d8e9ec2ee73d4918e74d9b76bda8940ba2b64e8777515e +F ext/misc/ieee754.c 176c061c94857b543313959289cb60cf777c999fd002f82b53d194b95e9f347a +F ext/misc/memstat.c 43705d795090efb78c85c736b89251e743c291e23daaa8382fe7a0df2c6a283d +F ext/misc/memtrace.c 7c0d115d2ef716ad0ba632c91e05bd119cb16c1aedf3bec9f06196ead2d5537b +F ext/misc/mmapwarm.c a81af4aaec00f24f308e2f4c19bf1d88f3ac3ce848c36daa7a4cd38145c4080d +F ext/misc/nextchar.c 7877914c2a80c2f181dd04c3dbef550dfb54c93495dc03da2403b5dd58f34edd +F ext/misc/noop.c f1a21cc9b7a4e667e5c8458d80ba680b8bd4315a003f256006046879f679c5a0 +F ext/misc/normalize.c fbb144a861809686ff2b5b6eee8bb2e1207f9bf13ce7376e5273c700a1eafbd5 +F ext/misc/pcachetrace.c f4227ce03fb16aa8d6f321b72dd051097419d7a028a9853af048bee7645cb405 +F ext/misc/percentile.c 72e05a21db20a2fa85264b99515941f00ae698824c9db82d7edfbb16cea8ec80 +F ext/misc/prefixes.c 82645f79229877afab08c8b08ca1e7fa31921280906b90a61c294e4f540cd2a6 +F ext/misc/qpvtab.c fc189e127f68f791af90a487f4460ec91539a716daf45a0c357e963fd47cc06c +F ext/misc/randomjson.c ef835fc64289e76ac4873b85fe12f9463a036168d7683cf2b773e36e6262c4ed +F ext/misc/regexp.c f1f7cfe90fc027b33d2b5ae7d6235eecce69c3aca71c9afce56fec62342c8b44 +F ext/misc/remember.c add730f0f7e7436cd15ea3fd6a90fd83c3f706ab44169f7f048438b7d6baa69c +F ext/misc/rot13.c 51ac5f51e9d5fd811db58a9c23c628ad5f333c173f1fc53c8491a3603d38556c +F ext/misc/scrub.c 2a44b0d44c69584c0580ad2553f6290a307a49df4668941d2812135bfb96a946 +F ext/misc/series.c 22c6d8f00cc1b5089b1b37392e9097e9df9a5db53be86daf9a7669d95bb179f4 +F ext/misc/sha1.c cb5002148c2661b5946f34561701e9105e9d339b713ec8ac057fd888b196dcb9 +F ext/misc/shathree.c fd22d70620f86a0467acfdd3acd8435d5cb54eb1e2d9ff36ae44e389826993df F ext/misc/showauth.c 732578f0fe4ce42d577e1c86dc89dd14a006ab52 -F ext/misc/spellfix.c 525190484b7a9dbc6be646c4842274fff4f27d53 -F ext/misc/totype.c 4a167594e791abeed95e0a8db028822b5e8fe512 -F ext/misc/vfslog.c fe40fab5c077a40477f7e5eba994309ecac6cc95 -F ext/misc/vtshim.c babb0dc2bf116029e3e7c9a618b8a1377045303e -F ext/misc/wholenumber.c 784b12543d60702ebdd47da936e278aa03076212 -F ext/rbu/rbu.c ba3983dceffa0938532e79142f391737513de023 -F ext/rbu/rbu1.test 57601977588603e82700a43c279bd55282ffa482 -F ext/rbu/rbu10.test 046b0980041d30700464a800bbf6733ed2df515d -F ext/rbu/rbu11.test 9bc68c2d3dbeb1720153626e3bd0466dcc017702 -F ext/rbu/rbu12.test bde22ed0004dd5d1888c72a84ae407e574aeae16 -F ext/rbu/rbu13.test 462ff799c4afedc3ef8a47ff818c0ffbf14ae4f2 -F ext/rbu/rbu14.test 01f5dcba904aecadbaea69d4ccdc2ea43dd30560 -F ext/rbu/rbu3.test 8bd4c6b87367c358981b6a47dc3d654fa60bff90 -F ext/rbu/rbu5.test 2e24fee3e615aecd99bbdd46967935a641e866f7 -F ext/rbu/rbu6.test 32e8ed60631b6facdb6366bd2b5f5f25245e7edb -F ext/rbu/rbu7.test fd025d5ba440fcfe151fbb0e3835e1e7fe964fa1 -F ext/rbu/rbu8.test 3bbf2c35d71a843c463efe93946f14ad10c3ede0 -F ext/rbu/rbu9.test 0806d1772c9f4981774ff028de6656e4183082af -F ext/rbu/rbuA.test c1a7b3e2d926b8f8448bb3b4ae787e314ee4b2b3 -F ext/rbu/rbuB.test c25bc325b8072a766e56bb76c001866b405925c2 -F ext/rbu/rbu_common.tcl 0398545fed614f807d5f0ba55a85a51f08ba8f1a -F ext/rbu/rbucrash.test 8d2ed5d4b05fef6c00c2a6b5f7ead71fa172a695 -F ext/rbu/rbudiff.test 6cc806dc36389292f2a8f5842d0103721df4a07d -F ext/rbu/rbufault.test cc0be8d5d392d98b0c2d6a51be377ea989250a89 -F ext/rbu/rbufault2.test 9a7f19edd6ea35c4c9f807d8a3db0a03a5670c06 -F ext/rbu/rbufts.test 828cd689da825f0a7b7c53ffc1f6f7fdb6fa5bda -F ext/rbu/rbusave.test 0f43b6686084f426ddd040b878426452fd2c2f48 -F ext/rbu/sqlite3rbu.c bea954197524631f2691ec272e8a42df8cad01cc -F ext/rbu/sqlite3rbu.h 0bdeb3be211aaba7d85445fa36f4701a25a3dbde -F ext/rbu/test_rbu.c 4a4cdcef4ef9379fc2a21f008805c80b27bcf573 -F ext/rtree/README 6315c0d73ebf0ec40dedb5aa0e942bc8b54e3761 -F ext/rtree/rtree.c 0f9b595bd0debcbedf1d7a63d0e0678d619e6c9c -F ext/rtree/rtree.h 834dbcb82dc85b2481cde6a07cdadfddc99e9b9e -F ext/rtree/rtree1.test 96a80c08440c932cd72aac50660e7af2612d2cda -F ext/rtree/rtree2.test acbb3a4ce0f4fbc2c304d2b4b784cfa161856bba -F ext/rtree/rtree3.test a494da55c30ee0bc9b01a91c80c81b387b22d2dc -F ext/rtree/rtree4.test c8fe384f60ebd49540a5fecc990041bf452eb6e0 -F ext/rtree/rtree5.test 6a510494f12454bf57ef28f45bc7764ea279431e -F ext/rtree/rtree6.test 773a90db2dce6a8353dd0d5b64bca69b29761196 -F ext/rtree/rtree7.test 1fa710b9e6bf997a0c1a537b81be7bb6fded1971 -F ext/rtree/rtree8.test db79c812f9e4a11f9b1f3f9934007884610a713a -F ext/rtree/rtree9.test b5eb13849545dfd271a54ff16784cb00d8792aea -F ext/rtree/rtreeA.test ace05e729a36e342d40cf94e9efc7b4723d9dcdf -F ext/rtree/rtreeB.test c85f9ce78766c4e68b8b89fbf2979ee9cfa82b4e -F ext/rtree/rtreeC.test 90aaaffe2fd4f0dcd12289cad5515f6d41f45ffd -F ext/rtree/rtreeD.test 636630357638f5983701550b37f0f5867130d2ca -F ext/rtree/rtreeE.test 45a147a64a76306172819562309681d8e90f94bb -F ext/rtree/rtreeF.test 66deb9fd1611c7ca2e374adba63debdc2dbb12b4 +F ext/misc/spellfix.c 693c8fd3293087fa821322967a97e59dfa24051e5d2ca7fa85790a4034db6fa4 +F ext/misc/sqlar.c a6175790482328171da47095f87608b48a476d4fac78d8a9ff18b03a2454f634 +F ext/misc/sqlite3_stdio.c 0fe5a45bd332b30aef2b68c64edbe69e31e9c42365b0fa79ce95a034bca6fbb0 +F ext/misc/sqlite3_stdio.h f05eaf5e0258f0573910324a789a9586fc360a57678c57a6d63cfaa2245b6176 +F ext/misc/stmt.c b090086cd6bd6281c21271d38d576eeffe662f0e6b67536352ce32bbaa438321 +F ext/misc/stmtrand.c 59cffa5d8e158943ff1ce078956d8e208e8c04e67307e8f249dece2436dcb7fc +F ext/misc/templatevtab.c 10f15b165b95423ddef593bc5dcb915ec4eb5e0f1066d585e5435a368b8bc22b +F ext/misc/totype.c ba11aac3c0b52c685bd25aa4e0f80c41c624fb1cc5ab763250e09ddc762bc3a8 +F ext/misc/uint.c 327afc166058acf566f33a15bf47c869d2d3564612644d9ff81a23efc8b36039 +F ext/misc/unionvtab.c 716d385256d5fb4beea31b0efede640807e423e85c9784d21d22f0cce010a785 +F ext/misc/urifuncs.c f71360d14fa9e7626b563f1f781c6148109462741c5235ac63ae0f8917b9c751 +F ext/misc/uuid.c 5bb2264c1b64d163efa46509544fd7500cb8769cb7c16dd52052da8d961505cf +F ext/misc/vfslog.c 3932ab932eeb2601dbc4447cb14d445aaa9fbe43b863ef5f014401c3420afd20 +F ext/misc/vfsstat.c 0b23c0a69a2b63dc0ef0af44f9c1fc977300c480a1f7a9814500369d8211f56e +F ext/misc/vfstrace.c 0e4b8b17ac0675ea90f6d168d8214687e06ca3efbc0060aad4814994d82b41fb +F ext/misc/vtablog.c 2d04386c2f5a3bb93bc9ae978f0b7dcd5a264e126abd640dd6d82aa9067cbd48 +F ext/misc/vtshim.c e5bce24ab8c532f4fdc600148718fe1802cb6ed57417f1c1032d8961f72b0e8f +F ext/misc/wholenumber.c 0fa0c082676b7868bf2fa918e911133f2b349bcdceabd1198bba5f65b4fc0668 +F ext/misc/windirent.h 02211ce51f3034c675f2dbf4d228194d51b3ee05734678bad5106fff6292e60c +F ext/misc/zipfile.c 71d3fd3155ed5e738473e286e550cf0bcf346cc2fd63646eaf944e7b40531a1b +F ext/misc/zorder.c bddff2e1b9661a90c95c2a9a9c7ecd8908afab5763256294dd12d609d4664eee +F ext/rbu/rbu.c 801450b24eaf14440d8fd20385aacc751d5c9d6123398df41b1b5aa804bf4ce8 +F ext/rbu/rbu1.test 25870dd7db7eb5597e2b4d6e29e7a7e095abf332660f67d89959552ce8f8f255 +F ext/rbu/rbu10.test 7c22caa32c2ff26983ca8320779a31495a6555737684af7aba3daaf762ef3363 +F ext/rbu/rbu11.test 8584f80ef4be00e6beec4154f638847ffc40b5f2832ffadfbaf558ae40e50cb5 +F ext/rbu/rbu12.test ec63aa7bfc3c65c1d774bf4357ed731723827d211d9d7cb0efa171bbaeeebaf4 +F ext/rbu/rbu13.test 658edbc3325d79252a98b761fde95460e439f80e820ff29e10261e25f870b3b6 +F ext/rbu/rbu14.test 05dac607a62f62102f4db92135979a8a4501143638060019aca08c753822cf39 +F ext/rbu/rbu3.test 4a81517af618c3bf8c72e2d0b81c7c06acb8d176036d63d8e6669b73342306ae +F ext/rbu/rbu5.test e21820b83822ae4c12afc2078a7b6c0523fb0cefe69c8b23c044cea91359e81c +F ext/rbu/rbu6.test db2ff1f832dfc9e34c7910b17e157c2fe0e36024a3fe1119dd6437640dc07c82 +F ext/rbu/rbu7.test 5fa41734613a3ae1bb93d280eb3c341cff5dcc72652ff9ec7fbaa12425eda9c2 +F ext/rbu/rbu8.test 93d45824dab8f68872b6d22acc787ab18ba92ef0fa0d430be37653d0246c7a0d +F ext/rbu/rbu9.test 4b66f0705442711a44b54ef2cc3c59952f1ea15f12e34442681bdb1a6eb33065 +F ext/rbu/rbuA.test 3f8fdd4ae7b9a0571af7361cd88359254f63e445ac4acfe395173e31d7e3fc31 +F ext/rbu/rbuB.test c639803bbc1dc9358afe6abe046dc4d3e9965238b75239b04e3a8e33e3e90f85 +F ext/rbu/rbuC.test 5326ea3954754c68fd518beb70d3e6b6690af53e1a5fa102d650e4110b26b4c5 +F ext/rbu/rbu_common.tcl 15d063397a89aeaf26b4cbdf6f29911b4154a902ba61a40c4f180ab452454a63 +F ext/rbu/rbubusy.test 88298187ad35aac9084436d85ca66b3722f96eaa704a09cfe5f931d452ab7237 +F ext/rbu/rbucollate.test 9852ec5e5ba7f3b04ce849a24ef7298e03ae0f16e58e6031d0f845234559feec +F ext/rbu/rbucrash.test d2b5d619d9281c89cad74401b73b46172daa89906940b1d739c813ddc0cd2cf5 +F ext/rbu/rbucrash2.test 0a1a72223d880215ce2893a3260320c31a9358d23cb124c610e4f0d984a93285 +F ext/rbu/rbudiff.test 8b8b8b569c68fc880134e0fac4bf6b4b7a907aea4cc6eacf7e1d45e1d47b6aac +F ext/rbu/rbudor.test 293a192e668bb8e9c7c9704b080c1086ee17496f768e0f1823049e7d02651d1b +F ext/rbu/rbuexlock.test e07a0875d0b72f7c007e5d5dcf424e9d48a4752a1a9bcee8ee36947e6add6d5b +F ext/rbu/rbuexpr.test 2c91617509c88b6e9030f7bf6ff720df26032fcd801adc25533feae726a57382 +F ext/rbu/rbufault.test c51de14067cfe867849530d3d1718ffeb28522f28d52937f95dd7bc2116eb42e +F ext/rbu/rbufault2.test 8cc8f6298d2d7d20080b2c77e65b607af8b89839f9d87c0972b27e6442edc258 +F ext/rbu/rbufault3.test d14ff46e050816ce43c4ed320a0927712636ac11bf48bfc5f74601f183af5445 +F ext/rbu/rbufault4.test 39fbf093b7e16aae85dc309262ec570d217a1578538c1c74dd621e5451c083d6 +F ext/rbu/rbufts.test df754d2f96c22d1da8b5d685b4a4a49863971920856d17620cef724e3a9b6edd +F ext/rbu/rbumisc.test 6641749e42c83062824c86b3d03a47f8ec35760f341bc023f53e612655b0a8af +F ext/rbu/rbumulti.test 6f6cdd9b3775108aada5216762cbbd7b5d5caa7cb620b3e6e1b8ace81286a2e0 +F ext/rbu/rbupartial.test 4ed7789f47128c8aa7ff58445face8a070cef852993afe03c863913f3cea8729 +F ext/rbu/rbupass.test 2ee86581a441f3b4b449b99a2dc203d5d6a08750dd2ee9ab6a02743e238d3c8a +F ext/rbu/rbuprogress.test db8bb26a8123d35f52acfc3984b56caa31c8fcd1fa3589991b9c8e8a68e64b59 +F ext/rbu/rburename.test 8d8a6a6ba896338d0610658e1f60e8055a181d5913e1e21c41b866a8f15bb7cd +F ext/rbu/rburesume.test 1403752d152b55efb7fc25749c0fccc790061371ec9ffe428cc04f8a69bb834c +F ext/rbu/rbusave.test 588b618dad9d65c4b13d03a79931de82213503fedc26bdf5789c996ecf427fba +F ext/rbu/rbusplit.test a6dedd23cf37bcf2e8646d9d7139846e96d60d92f9bc6d6ba6ca8c24c0bd1f72 +F ext/rbu/rbutemplimit.test 4980df2d4b74f4dd982add8f78809106154ef5a3c4bdce747422ab0b0481e029 +F ext/rbu/rbuvacuum.test 542561741ff2b262e3694bc6012b44694ee62c545845319a06f323783b15311e +F ext/rbu/rbuvacuum2.test 1a9bd41f127be2826de2a65204df9118525a8af8d16e61e6bc63ba3ac0010a23 +F ext/rbu/rbuvacuum3.test 3ce42695fdf21aaa3499e857d7d4253bc499ad759bcd6c9362042c13cd37d8de +F ext/rbu/rbuvacuum4.test ffccd22f67e2d0b380d2889685742159dfe0d19a3880ca3d2d1d69eefaebb205 +F ext/rbu/sqlite3rbu.c c208f72f20784bf2f39244b6cdf8019724a706e1246be289e7621c42aad54695 +F ext/rbu/sqlite3rbu.h e3a5bf21e09ca93ce4e8740e00d6a853e90a697968ec0ea98f40826938bdb68e +F ext/rbu/test_rbu.c 8b6e64e486c28c41ef29f6f4ea6be7b3091958987812784904f5e903f6b56418 +F ext/recover/dbdata.c 10d3c56968a9af6853722a47280805ad1564714d79ea45ac6f7da14bb57fd137 +F ext/recover/recover1.test e16d78e94183562abff569967b18b7c77451d7044365516cd0fe14713a284851 +F ext/recover/recover_common.tcl a61306c1eb45c0c3fc45652c35b2d4ec19729e340bdf65a272ce4c229cefd85a +F ext/recover/recoverbuild.test c74170e0f7b02456af41838afeb5353fdb985a48cc2331d661bbabbca7c6b8e3 +F ext/recover/recoverclobber.test 3ba6c0c373c5c63d17e82eced64c05c57ccaf26c1abe1ca7141334022a79f32e +F ext/recover/recovercorrupt.test 64c081ad1200ae77b447da99eb724785d6bf71715f394543dc7689642e92bf49 +F ext/recover/recovercorrupt2.test 7347ccc9c36a925d99b56689c791423b45294834198f17575183fd500f52d85d +F ext/recover/recovercorrupt3.test 2e7b9a1b528ca23ed382cec6f64e3fcbbd0f8e852add7562397fd8df83f335d5 +F ext/recover/recovercorrupt4.test 3e2794145dad2517c018cb68b96f59d4d55b18b3d6271e1d37852cfd7a30b50c +F ext/recover/recoverfault.test 9d9f88eeb222615a25e7514f234c950d46bee20d24cd8db49d8fff8d650dcfe1 +F ext/recover/recoverfault2.test 730e7371bcda769554d15460cb23126abba1be8eca9539ccabf63623e7bb7e09 +F ext/recover/recoverold.test 68db3d6f85dd2b98e785b6c4da4f5eea4bbe52ccf6674d9a94c7506dc92596aa +F ext/recover/recoverpgsz.test 88766fcb810e52ee05335c456d4e5fb06d02b73d3ccb48c52bf293434305e2b1 +F ext/recover/recoverrowid.test f948bf4024a5f41b0e21b8af80c60564c5b5d78c05a8d64fc00787715ff9f45f +F ext/recover/recoverslowidx.test c90d59c46bb8924a973ac6fbc38f3163cee38cc240256addcab1cf1a322c37dc +F ext/recover/recoversql.test e66d01f95302a223bcd3fd42b5ee58dc2b53d70afa90b0d00e41e4b8eab20486 +F ext/recover/sqlite3recover.c 56c216332ea91233d6d820d429f3384adbec9ecedda67aa98186b691d427cc57 +F ext/recover/sqlite3recover.h 011c799f02deb70ab685916f6f538e6bb32c4e0025e79bfd0e24ff9c74820959 +F ext/recover/test_recover.c 3d0fb1df7823f5bc22a0b93955034d16a2dfa2eb1e443e9a0123a77f120599a3 +F ext/repair/README.md 92f5e8aae749a4dae14f02eea8e1bb42d4db2b6ce5e83dbcdd6b1446997e0c15 +F ext/repair/checkfreelist.c e21f06995ff4efdc1622dcceaea4dcba2caa83ca2f31a1607b98a8509168a996 +F ext/repair/checkindex.c 7639b4f8928f82c10b950169e60cc45a7f6798df0b299771d17bef025736f657 +F ext/repair/sqlite3_checker.c.in 445118c5f7fea958b36fba1b2c464283e60ed4842039ddee3265f1698115ebf7 +F ext/repair/sqlite3_checker.tcl a9a2caa9660567257c177a91124d8c0dccdfa341e25c51e6da7f1fd9e601eafa +F ext/repair/test/README.md 34b2f542cf5be7bffe479242b33ee3492cea30711e447cc4a1a86cb5915f419e +F ext/repair/test/checkfreelist01.test 3e8aa6aeb4007680c94a8d07b41c339aa635cc78249442da72ff3f8297398a69 +F ext/repair/test/checkindex01.test b530f141413b587c9eb78ff734de6bb79bc3515c335096108c12c01bddbadcec +F ext/repair/test/test.tcl 686d76d888dffd021f64260abf29a55c57b2cedfa7fc69150b42b1d6119aac3c +F ext/rtree/README 734aa36238bcd2dee91db5dba107d5fcbdb02396612811377a8ad50f1272b1c1 +F ext/rtree/geopoly.c f0573d5109fdc658a180db0db6eec86ab2a1cf5ce58ec66cbf3356167ea757eb +F ext/rtree/rtree.c 9331997a76b88a9bc04e156bdfd6e2fe35c0aa93bc338ebc6aa0ae470fe4a852 +F ext/rtree/rtree.h 4a690463901cb5e6127cf05eb8e642f127012fd5003830dbc974eca5802d9412 +F ext/rtree/rtree1.test e0608db762b2aadca0ecb6f97396cf66244490adc3ba88f2a292b27be3e1da3e +F ext/rtree/rtree2.test 9d9deddbb16fd0c30c36e6b4fdc3ee3132d765567f0f9432ee71e1303d32603d +F ext/rtree/rtree3.test 272594f88c344e973864008bbe4c71fd3a41a264c097d568593ee7886d83d409 +F ext/rtree/rtree4.test 304de65d484540111b896827e4261815e5dca4ce28eeecd58be648cd73452c4b +F ext/rtree/rtree5.test 49c9041d713d54560b315c2c7ef7207ee287eba1b20f8266968a06f2e55d3142 +F ext/rtree/rtree6.test 2f5ffc69670395c1a84fad7924e2d49e82a25460c5293fb1e54e1aa906f04945 +F ext/rtree/rtree7.test c8fb2e555b128dd0f0bdb520c61380014f497f8a23c40f2e820acc9f9e4fdce5 +F ext/rtree/rtree8.test 4da84c7f328bbdca15052fa13da6e8b8d426433347bf75fc85574c2f5a411a02 +F ext/rtree/rtree9.test fd3c9384ef8aabbc127b3878764070398f136eebc551cd20484b570f2cc1956a +F ext/rtree/rtreeA.test 14e67fccc5b41efbad7ea99d21d11aaa66d2067da7d5b296ee86e4de64391d82 +F ext/rtree/rtreeB.test ab93136c45cf25af78d22665c2a6d75068eef6bf3a710356e4ba8d5f37bed364 +F ext/rtree/rtreeC.test 2978b194d09b13e106bdb0e1c5b408b9d42eb338c1082bf43c87ef43bd626147 +F ext/rtree/rtreeD.test fe46aa7f012e137bd58294409b16c0d43976c3bb92c8f710481e577c4a1100dc +F ext/rtree/rtreeE.test e65d3fc625da1800b412fc8785817327d43ccfec5f5973912d8c9e471928caa9 +F ext/rtree/rtreeF.test 81ffa7ef51c4e4618d497a57328c265bf576990c7070633b623b23cd450ed331 +F ext/rtree/rtreeG.test 1b9ca6e3effb48f4161edaa463ddeaa8fca4b2526d084f9cbf5dbe4e0184939c +F ext/rtree/rtreeH.test c304651ee87dbb60296366d2c802f3043c3d00506c79a5c85bd20f2c9274498b +F ext/rtree/rtreeI.test 608e77f7fde9be5a12eae316baef640fffaafcfa90a3d67443e78123e19c4ca4 +F ext/rtree/rtreeJ.test 93227ccd4d6c328f5ac46a902b8880041509dd2d68f6ce71560f0d8ab5bb507a F ext/rtree/rtree_perf.tcl 6c18c1f23cd48e0f948930c98dfdd37dfccb5195 -F ext/rtree/rtree_util.tcl 06aab2ed5b826545bf215fff90ecb9255a8647ea -F ext/rtree/sqlite3rtree.h 9c5777af3d2921c7b4ae4954e8e5697502289d28 +F ext/rtree/rtree_util.tcl 202ca70df1f0645ef9d5a2170e62d378a28098d9407f0569e85c9c1cf1bd020a +F ext/rtree/rtreecheck.test 84eedb43b25b3edf591125266d0bb1cebdfcdcc9c4a56b27d85bcb63c7dd7558 +F ext/rtree/rtreecirc.test aec664eb21ae943aeb344191407afff5d392d3ae9d12b9a112ced0d9c5de298e +F ext/rtree/rtreeconnect.test 225ad3fcb483d36cbee423a25052a6bbae762c9576ae9268332360c68c170d3d +F ext/rtree/rtreedoc.test d633982d61542f3bc0a0a2df0382a02cc699ac56cbda01130cde6da44a228490 +F ext/rtree/rtreedoc2.test 0102b4e60f1baf0039bef7c1c4b39f1d3d77a68a5741513a0c190db28b884835 +F ext/rtree/rtreedoc3.test 555a878c4d79c4e37fa439a1c3b02ee65d3ebaf75d9e8d96a9c55d66db3efbf8 +F ext/rtree/rtreefuzz001.test 44f680a23dbe00d1061dbde381d711119099846d166580c4381e402b9d62cb74 +F ext/rtree/sqlite3rtree.h 03c8db3261e435fbddcfc961471795cbf12b24e03001d0015b2636b0f3881373 +F ext/rtree/test_rtreedoc.c d20f51d1ad69c72947a4ac72194e5a12e70b3464e7492538fcef66fa871c5081 F ext/rtree/tkt3363.test 142ab96eded44a3615ec79fba98c7bde7d0f96de +F ext/rtree/util/randomshape.tcl 54ee03d0d4a1c621806f7f44d5b78d2db8fac26e0e8687c36c4bd0203b27dbff F ext/rtree/viewrtree.tcl eea6224b3553599ae665b239bd827e182b466024 -F ext/userauth/sqlite3userauth.h 19cb6f0e31316d0ee4afdfb7a85ef9da3333a220 -F ext/userauth/user-auth.txt e6641021a9210364665fe625d067617d03f27b04 -F ext/userauth/userauth.c 5fa3bdb492f481bbc1709fc83c91ebd13460c69e -F install-sh 9d4de14ab9fb0facae2f48780b874848cbf2f895 x -F ltmain.sh 3ff0879076df340d2e23ae905484d8c15d5fdea8 -F magic.txt 8273bf49ba3b0c8559cb2774495390c31fd61c60 -F main.mk 791597b87204f6bd56cdf7b2ab2feeecd608c60d -F mkso.sh fd21c06b063bb16a5d25deea1752c2da6ac3ed83 +F ext/rtree/visual01.txt e9c2564083bcd30ec51b07f881bffbf0e12b50a3f6fced0c222c5c1d2f94ac66 +F ext/session/changeset.c 06b585d977391d498746f002b2d5f9315d0d37888ce9551bd0cb30bfe9a4cf47 +F ext/session/changesetfuzz.c 227076ab0ae4447d742c01ee88a564da6478bbf26b65108bf8fac9cd8b0b24aa +F ext/session/changesetfuzz1.test 15b629004e58d5ffcc852e6842a603775bb64b1ce51254831f3d12b113b616cd +F ext/session/session1.test 5d2502922d38a1579076863827342379a1609ca6bae78c40691a2be1ed1be2aa x +F ext/session/session2.test ee83bb973b9ce17ccce4db931cdcdae65eb40bbb22089b2fe6aa4f6be3b9303f +F ext/session/session3.test 2cc1629cfb880243aec1a7251145e07b78411d851b39b2aa1390704550db8e6a +F ext/session/session4.test 823f6f018fcbb8dacf61e2960f8b3b848d492b094f8b495eae1d9407d9ab7219 +F ext/session/session5.test 716bc6fafd625ce60dfa62ae128971628c1a1169 +F ext/session/session6.test 35279f2ec45448cd2e24a61688219dc6cf7871757716063acf4a8b5455e1e926 +F ext/session/session8.test 326f3273abf9d5d2d7d559eee8f5994c4ea74a5d935562454605e6607ee29904 +F ext/session/session9.test 0c4a8fbe7a5031f50855f020f3408e1f07fd7859f1daa1629eadcec3422072d6 +F ext/session/sessionA.test 1feeab0b8e03527f08f2f1defb442da25480138f +F ext/session/sessionB.test c4fb7f8a688787111606e123a555f18ee04f65bb9f2a4bb2aa71d55ce4e6d02c +F ext/session/sessionC.test f8a5508bc059ae646e5ec9bdbca66ad24bc92fe99fda5790ac57e1f59fce2fdf +F ext/session/sessionD.test 470ff917dc849e2eb78142ade63aaabd729d773833cff0ff01bca0eda68a21ce +F ext/session/sessionE.test b2010949c9d7415306f64e3c2072ddabc4b8250c98478d3c0c4d064bce83111d +F ext/session/sessionF.test d37ed800881e742c208df443537bf29aa49fd56eac520d0f0c6df3e6320f3401 +F ext/session/sessionG.test 3efe388282d641b65485b5462e67851002cd91a282dc95b685d085eb8efdad0a +F ext/session/sessionH.test 71bbff6b1abb2c4ac62b84dee53273c37e0b21e5fde3aed80929403e091ef859 +F ext/session/sessionI.test 11e7b6729fc942982a5104a40132f70a2e964d64d60dc5809b8206465af74822 +F ext/session/session_common.tcl a31f537a929a695a852d241c9434f2847cadf329856401921139fbb03a5a7697 +F ext/session/session_gen.test 942a0002df10da53c45b40b581cc3ed25e7ff42bda1e7ba497273dc2887aa8e6 +F ext/session/session_speed_test.c dcf0ef58d76b70c8fbd9eab3be77cf9deb8bc1638fed8be518b62d6cbdef88b3 +F ext/session/sessionalter.test e852acb3d2357aac7d0b920a2109da758c4331bfdf85b41d39aa3a8c18914f65 +F ext/session/sessionat.test 00c8badb35e43a2f12a716d2734a44d614ff62361979b6b85419035bc04b45ee +F ext/session/sessionbig.test 47c381e7acfabeef17d98519a3080d69151723354d220afa2053852182ca7adf +F ext/session/sessionblob.test 87faf667870b72f08e91969abd9f52a383ab7b514506ee194d64a39d8faff00a +F ext/session/sessionchange.test 6618cb1c1338a4b6df173b6ac42d09623fb71269962abf23ebb7617fe9f45a50 +F ext/session/sessionconflict.test 19e4a53795c4c930bfec49e809311e09b2a9e202d9446e56d7a8b139046a0c07 x +F ext/session/sessiondiff.test e89f7aedcdd89e5ebac3a455224eb553a171e9586fc3e1e6a7b3388d2648ba8d +F ext/session/sessionfault.test c2b43d01213b389a3f518e90775fca2120812ba51e50444c4066962263e45c11 +F ext/session/sessionfault2.test b0d6a7c1d7398a7e800d84657404909c7d385965ea8576dc79ed344c46fbf41c +F ext/session/sessionfault3.test ce0b5d182133935c224d72507dbf1c5be1a1febf7e85d0b0fbd6d2f724b32b96 +F ext/session/sessioninvert.test 9018f6a7387ac745084b6374c5e1aa14d648b372e6e1181cfab3df632b662d26 x +F ext/session/sessionmem.test f2a735db84a3e9e19f571033b725b0b2daf847f3f28b1da55a0c1a4e74f1de09 +F ext/session/sessionnoact.test 4c7ae5c7d351cb5323bca62b6b095592ad24bd90a6713c178b62ab0063d23e19 +F ext/session/sessionnoop.test a9366a36a95ef85f8a3687856ebef46983df399541174cb1ede2ee53b8011bc7 +F ext/session/sessionnoop2.test 2d8146321785bdc7cee8966d984560184cfca83fd46da2ff81ea883d6e977b36 +F ext/session/sessionrebase.test 702378bdcb5062f1106e74457beca8797d09c113a81768734a58b197b5b334e2 +F ext/session/sessionrowid.test 85187c2f1b38861a5844868126f69f9ec62223a03449a98a80600a44396f7363 +F ext/session/sessionsize.test 8fcf4685993c3dbaa46a24183940ab9f5aa9ed0d23e5fb63bfffbdb56134b795 +F ext/session/sessionstat1.test 5e718d5888c0c49bbb33a7a4f816366db85f59f6a4f97544a806421b85dc2dec +F ext/session/sessionwor.test 6fd9a2256442cebde5b2284936ae9e0d54bde692d0f5fd009ecef8511f4cf3fc +F ext/session/sqlite3session.c b3de195ce668cace9b324599bf6255a70290cbfb5451e826e946f3aee6e64c54 +F ext/session/sqlite3session.h 7404723606074fcb2afdc6b72c206072cdb2b7d8ba097ca1559174a80bc26f7a +F ext/session/test_session.c 8766b5973a6323934cb51248f621c3dc87ad2a98f023c3cc280d79e7d78d36fb +F ext/wasm/EXPORTED_FUNCTIONS.fiddle.in 27450c8b8c70875a260aca55435ec927068b34cef801a96205adb81bdcefc65c +F ext/wasm/GNUmakefile 3dc01e673c456d3b752674c9407276e8fef35dec1d304b3cc1de362f019b2a09 +F ext/wasm/README-dist.txt f01081a850ce38a56706af6b481e3a7878e24e42b314cfcd4b129f0f8427066a +F ext/wasm/README.md 2e87804e12c98f1d194b7a06162a88441d33bb443efcfe00dc6565a780d2f259 +F ext/wasm/SQLTester/GNUmakefile e0794f676d55819951bbfae45cc5e8d7818dc460492dc317ce7f0d2eca15caff +F ext/wasm/SQLTester/SQLTester.mjs 6b3c52ed36a5573ca4883176f326332a8d4c0cecf5efd80489528267fa5d9ed4 +F ext/wasm/SQLTester/SQLTester.run.mjs 57f2adb33f43f2784abbf8026c1bfd049d8013af1998e7dcb8b50c89ffc332e0 +F ext/wasm/SQLTester/index.html 64f3435084c7d6139b08d1f2a713828a73f68de2ae6a3112cbb5980d991ba06f +F ext/wasm/SQLTester/touint8array.c 2d5ece04ec1393a6a60c4bf96385bda5e1a10ad49f3038b96460fc5e5aa7e536 +F ext/wasm/api/EXPORTED_FUNCTIONS.sqlite3-core ef34398a903d0a2425fbbfbd4ed2cd596daea55b8515e2617c8dc7ad7c0767dd +F ext/wasm/api/EXPORTED_FUNCTIONS.sqlite3-extras 9eae68943ce91ab145892b31370819c2103525240eb72e0fce53c498b8d8275a +F ext/wasm/api/EXPORTED_FUNCTIONS.sqlite3-see fb29e62082a658f0d81102488414d422c393c4b20cc2f685b216bc566237957b +F ext/wasm/api/EXPORTED_RUNTIME_METHODS.sqlite3-api 1ec3c73e7d66e95529c3c64ac3de2470b0e9e7fbf7a5b41261c367cf4f1b7287 +F ext/wasm/api/README.md f4c0d67caaee21a77b8938c30b5f79667bfc9d0c95d01b51df77ea35ee773884 +F ext/wasm/api/extern-post-js.c-pp.js 205f55aacfc62c580985db5c790300779de3876a76a5c7e1bfb13e71c8b4506b +F ext/wasm/api/extern-pre-js.js cc61c09c7a24a07dbecb4c352453c3985170cec12b4e7e7e7a4d11d43c5c8f41 +F ext/wasm/api/post-js-footer.js 5bd7170b5e8ce7b62102702bbcf47ef7b3b49cd56ed40c043fd990aa715b74ee +F ext/wasm/api/post-js-header.js 79d078aec33d93b640a19c574b504d88bb2446432f38e2fbb3bb8e36da436e70 +F ext/wasm/api/pre-js.c-pp.js a876c6399dff29b6fe9e434036beb89889164cc872334e184291723ecc7cb072 +F ext/wasm/api/sqlite3-api-cleanup.js a3d6b9e449aefbb8bba283c2ba9477e2333a0eeb94a7a26b5bf952736f65a6dd +F ext/wasm/api/sqlite3-api-glue.c-pp.js d2b8263b3ce0cefc6c5a68d0a4d448a9770eda4bf9d9ded9d7eb0198e4ce4da1 +F ext/wasm/api/sqlite3-api-oo1.c-pp.js c4260f3fdc553c56ee530c20cc1119029067b503f0d6d7b472705536cb45aa1d +F ext/wasm/api/sqlite3-api-prologue.js 307583ff39a978c897c4ef4ce53fe231dce5c73dc84785969c81c1ab5960a293 +F ext/wasm/api/sqlite3-api-worker1.c-pp.js 1041dd645e8e821c082b628cd8d9acf70c667430f9d45167569633ffc7567938 +F ext/wasm/api/sqlite3-license-version-header.js 0c807a421f0187e778dc1078f10d2994b915123c1223fe752b60afdcd1263f89 +F ext/wasm/api/sqlite3-opfs-async-proxy.js 9654b565b346dc609b75d15337f20acfa7af7d9d558da1afeb9b6d8eaa404966 +F ext/wasm/api/sqlite3-vfs-helper.c-pp.js 3f828cc66758acb40e9c5b4dcfd87fd478a14c8fb7f0630264e6c7fa0e57515d +F ext/wasm/api/sqlite3-vfs-opfs-sahpool.c-pp.js 26cb41d5a62f46a106b6371eb00fef02de3cdbfaa51338ba087a45f53028e0d0 +F ext/wasm/api/sqlite3-vfs-opfs.c-pp.js 418c33fe284739564daab3c7a7a88882fdd3c99137497900f98eddec1e409af5 +F ext/wasm/api/sqlite3-vtab-helper.c-pp.js 9097074724172e31e56ce20ccd7482259cf72a76124213cbc9469d757676da86 +F ext/wasm/api/sqlite3-wasm.c dd7fc1d535281f0d5d2732bb1b662d1d403a762f07b63c2ea5663053377b2804 +F ext/wasm/api/sqlite3-worker1-promiser.c-pp.js bda1c75bd674a92a0e27cc2f3d46dbbf21e422413f8046814515a0bd7409328a +F ext/wasm/api/sqlite3-worker1.c-pp.js 802d69ead8c38dc1be52c83afbfc77e757da8a91a2e159e7ed3ecda8b8dba2e7 +F ext/wasm/c-pp-lite.c 8fa0148e73782a86274db688c4730e2962cd675af329490493adddaf3322f16f +F ext/wasm/common/SqliteTestUtil.js 7adaeffef757d8708418dc9190f72df22367b531831775804b31598b44f6aa51 +F ext/wasm/common/emscripten.css 11bd104b6c0d597c67d40cc8ecc0a60dae2b965151e3b6a37fa5708bac3acd15 +F ext/wasm/common/testing.css e97549bab24126c24e0daabfe2de9bb478fb0a69fdb2ddd0a73a992c091aad6f +F ext/wasm/common/whwasmutil.js 0d539324097fc83b953e9844267359ba0fd02286caa784ea2f597ced279ea640 +F ext/wasm/config.make.in c424ae1cc3c89274520ad312509d36c4daa34a3fce5d0c688e5f8f4365e1049a +F ext/wasm/demo-123-worker.html a0b58d9caef098a626a1a1db567076fca4245e8d60ba94557ede8684350a81ed +F ext/wasm/demo-123.html 8c70a412ce386bd3796534257935eb1e3ea5c581e5d5aea0490b8232e570a508 +F ext/wasm/demo-123.js c7b3cca50c55841c381a9ca4f9396e5bbdc6114273d0b10a43e378e32e7be5bf +F ext/wasm/demo-jsstorage.html 409c4be4af5f207fb2877160724b91b33ea36a3cd8c204e8da1acb828ffe588e +F ext/wasm/demo-jsstorage.js 42131ddfa18e817d0e39ac63745e9ea31553980a5ebd2222e04d4fac60c19837 +F ext/wasm/demo-worker1-promiser.c-pp.html 635cf90685805e21772a5f7a35d1ace80f98a9ef7c42ff04d7a125ddca7e5db8 +F ext/wasm/demo-worker1-promiser.c-pp.js f40ec65810048e368896be71461028bd10de01e24277208c59266edf23bb9f52 +F ext/wasm/demo-worker1.html 2c178c1890a2beb5a5fecb1453e796d067a4b8d3d2a04d65ca2eb1ab2c68ef5d +F ext/wasm/demo-worker1.js 08720227e98fa5b44761cf6e219269cee3e9dd0421d8d91459535da776950314 +F ext/wasm/example_extra_init.c 2347cd69d19d839ef4e5e77b7855103a7fe3ef2af86f2e8c95839afd8b05862f +F ext/wasm/fiddle/fiddle-worker.js 7798af02e672e088ff192716f80626c8895e19301a65b8af6d5d12b2d13d2451 +F ext/wasm/fiddle/fiddle.js 84fd75967e0af8b69d3dd849818342227d0f81d13db92e0dcbc63649b31a4893 +F ext/wasm/fiddle/index.html a27b8127ef9ecf19612da93b2a6a73bdb3777b5c56b5450bb7200a94bc108ff9 +F ext/wasm/index-dist.html db23748044e286773f2768eec287669501703b5d5f72755e8db73607dc54d290 +F ext/wasm/index.html 54e27db740695ab2cb296e02d42c4c66b3f11b65797340d19fa6590f5b287da1 +F ext/wasm/jaccwabyt/jaccwabyt.js bbac67bc7a79dca34afe6215fd16b27768d84e22273507206f888c117e2ede7d +F ext/wasm/jaccwabyt/jaccwabyt.md 167fc0b624c9bc2c477846e336de9403842d81b1a24fc4d3b24317cb9eba734f +F ext/wasm/mkdist.sh 64d53f469c823ed311f6696f69cec9093f745e467334b34f5ceabdf9de3c5b28 x +F ext/wasm/mkwasmbuilds.c 1b53c4d2a1350c19a96a8cdfbda6a39baea9d2142bfe0cbef0ccb0e898787f47 +F ext/wasm/module-symbols.html e54f42112e0aac2a31f850ab33e7f2630a2ea4f63496f484a12469a2501e07e2 +F ext/wasm/scratchpad-wasmfs.html a3d7388f3c4b263676b58b526846e9d02dfcb4014ff29d3a5040935286af5b96 +F ext/wasm/scratchpad-wasmfs.mjs 66034b9256b218de59248aad796760a1584c1dd842231505895eff00dbd57c63 +F ext/wasm/speedtest1-wasmfs.html 0e9d335a9b5b5fafe6e1bc8dc0f0ca7e22e6eb916682a2d7c36218bb7d67379d +F ext/wasm/speedtest1-wasmfs.mjs 60dd5842f6d2a70a6d0bef12633a11491bde6984aff75a37c2040980d8cbf36a +F ext/wasm/speedtest1-worker.html 068d4190f304fa1c34e6501a1b3a4c32fe8d8dac93c2d0f53d667a1cb386eedc +F ext/wasm/speedtest1-worker.js 958a2d3c710bf8e82567277f656193a0248216db99a3c2c86966124b84309efb +F ext/wasm/speedtest1.html c90d63dfa795f0cb1ad188de587be9024b1ff73b4adc5fdf7efc0d781be94d03 +F ext/wasm/split-speedtest1-script.sh a3e271938d4d14ee49105eb05567c6a69ba4c1f1293583ad5af0cd3a3779e205 x +F ext/wasm/sql/000-mandelbrot.sql 775337a4b80938ac8146aedf88808282f04d02d983d82675bd63d9c2d97a15f0 +F ext/wasm/sql/001-sudoku.sql 35b7cb7239ba5d5f193bc05ec379bcf66891bce6f2a5b3879f2f78d0917299b5 +F ext/wasm/test-opfs-vfs.html 1f2d672f3f3fce810dfd48a8d56914aba22e45c6834e262555e685bce3da8c3f +F ext/wasm/test-opfs-vfs.js 1618670e466f424aa289859fe0ec8ded223e42e9e69b5c851f809baaaca1a00c +F ext/wasm/tester1-worker.c-pp.html 883881eeac14eeeecc8ff22acf9fe0f18a97cacb48be08ebb0bae891ceded584 +F ext/wasm/tester1.c-pp.html 949920126dcf477925d8d540093d9cc374d3ab4c4ddee920c1dcadcf37917306 +F ext/wasm/tester1.c-pp.js 2b014884dadf28928fabcb688746ca87145673eef75e154486505a266203fc15 +F ext/wasm/tests/opfs/concurrency/index.html 657578a6e9ce1e9b8be951549ed93a6a471f4520a99e5b545928668f4285fb5e +F ext/wasm/tests/opfs/concurrency/test.js d08889a5bb6e61937d0b8cbb78c9efbefbf65ad09f510589c779b7cc6a803a88 +F ext/wasm/tests/opfs/concurrency/worker.js 0a8c1a3e6ebb38aabbee24f122693f1fb29d599948915c76906681bb7da1d3d2 +F ext/wasm/tests/opfs/sahpool/digest-worker.js b0ab6218588f1f0a6d15a363b493ceaf29bfb87804d9e0165915a9996377cf79 +F ext/wasm/tests/opfs/sahpool/digest.html 206d08a34dc8bd570b2581d3d9ab3ecad3201b516a598dd096dcf3cf8cd81df8 +F ext/wasm/tests/opfs/sahpool/index.html be736567fd92d3ecb9754c145755037cbbd2bca01385e2732294b53f4c842328 +F ext/wasm/tests/opfs/sahpool/sahpool-pausing.js f264925cfc82155de38cecb3d204c36e0f6991460fff0cb7c15079454679a4e2 +F ext/wasm/tests/opfs/sahpool/sahpool-worker.js bd25a43fc2ab2d1bafd8f2854ad3943ef673f7c3be03e95ecf1612ff6e8e2a61 +F magic.txt 5ade0bc977aa135e79e3faaea894d5671b26107cc91e70783aa7dc83f22f3ba0 +F main.mk 00dd631c66c1f7922b2d691631163899eff1c3d1da780ff37a62f8d997b368e1 F mptest/config01.test 3c6adcbc50b991866855f1977ff172eb6d901271 F mptest/config02.test 4415dfe36c48785f751e16e32c20b077c28ae504 F mptest/crash01.test 61e61469e257df0850df4293d7d4d6c2af301421 F mptest/crash02.subtest f4ef05adcd15d60e5d2bd654204f2c008b519df8 -F mptest/mptest.c aa41ace6dbc5050d76b02548d3521e6bbccae4f0 +F mptest/mptest.c 7ecf39040c1187d9cbc487409bc052f194e93c165f667c55b052690a741d9d71 F mptest/multiwrite01.test dab5c5f8f9534971efce679152c5146da265222d -F spec.template 86a4a43b99ebb3e75e6b9a735d5fd293a24e90ca F sqlite.pc.in 42b7bf0d02e08b9e77734a47798d1a55a9e0716b -F sqlite3.1 fc7ad8990fc8409983309bb80de8c811a7506786 -F sqlite3.pc.in 48fed132e7cb71ab676105d2a4dc77127d8c1f3a -F src/alter.c 1fbb01c26c64528088f1df8015992fefda387889 -F src/analyze.c fbf0e80d83cc893734e872f932f249a056b86e11 -F src/attach.c c16c2648a577fa3def2adfa48c28901376389bc5 -F src/auth.c b56c78ebe40a2110fd361379f7e8162d23f92240 -F src/backup.c 2869a76c03eb393ee795416e2387005553df72bc -F src/bitvec.c 1a78d450a17c5016710eec900bedfc5729bf9bdf -F src/btmutex.c bc87dd3b062cc26edfe79918de2200ccb8d41e73 -F src/btree.c 4c8caaeed7878aafdb607c3d2bcbc365bb0d19a1 -F src/btree.h 368ceeb4bd9312dc8df2ffd64b4b7dbcf4db5f8e -F src/btreeInt.h c18b7d2a3494695133e4e60ee36061d37f45d9a5 -F src/build.c 54866fbafa09d494269bdefc79995eb7207003a6 -F src/callback.c ed6c2a4a712eb7287ff64e20e3c23265dfb8a7ce -F src/complete.c addcd8160b081131005d5bc2d34adf20c1c5c92f -F src/ctime.c 60e135af364d777a9ab41c97e5e89cd224da6198 -F src/date.c ca17321bc17cca8f40e0843edea4fafff974998e -F src/dbstat.c b2ec6793eef97aebb4d171d490a4ffdfa9f2475c -F src/delete.c 48802aa3ee6339f576d074336d3ae1b5f40e240f -F src/expr.c fbf0706199aea23c54efe36b6932d8307c4eb872 -F src/fault.c 160a0c015b6c2629d3899ed2daf63d75754a32bb -F src/fkey.c 08edad1fce30f761f14b3997e89bad58f9f7f4e0 -F src/func.c 86e55fee35b9577e485f47d9dd5c1d34cd513288 -F src/global.c bd5a0af3f30b0c01be6db756c626cd3c33a3d260 -F src/hash.c 4263fbc955f26c2e8cdc0cf214bc42435aa4e4f5 -F src/hash.h c8f3c31722cf3277d03713909761e152a5b81094 -F src/hwtime.h d32741c8f4df852c7d959236615444e2b1063b08 -F src/insert.c 9ca97272e9f74ed0efddf3b4350ee12740cebbef -F src/journal.c b4124532212b6952f42eb2c12fa3c25701d8ba8d -F src/legacy.c b1b0880fc474abfab89e737b0ecfde0bd7a60902 -F src/loadext.c 84996d7d70a605597d79c1f1d7b2012a5fd34f2b -F src/main.c b67a45397b93b7ba8fbd6bfcb03423d245baed05 -F src/malloc.c 337e9808b5231855fe28857950f4f60ae42c417f +F sqlite3.1 1b9c24374a85dfc7eb8fa7c4266ee0db4f9609cceecfc5481cd8307e5af04366 +F sqlite3.pc.in e6dee284fba59ef500092fdc1843df3be8433323a3733c91da96690a50a5b398 +F src/alter.c fc7bbbeb9e89c7124bf5772ce474b333b7bdc18d6e080763211a40fde69fb1da +F src/analyze.c 03bcfc083fc0cccaa9ded93604e1d4244ea245c17285d463ef6a60425fcb247d +F src/attach.c 9af61b63b10ee702b1594ecd24fb8cea0839cfdb6addee52fba26fa879f5db9d +F src/auth.c 54ab9c6c5803b47c0d45b76ce27eff22a03b4b1f767c5945a3a4eb13aa4c78dc +F src/backup.c 5c97e8023aab1ce14a42387eb3ae00ba5a0644569e3476f38661fa6f824c3523 +F src/bitvec.c e242d4496774dfc88fa278177dd23b607dce369ccafb3f61b41638eea2c9b399 +F src/btmutex.c 30dada73a819a1ef5b7583786370dce1842e12e1ad941e4d05ac29695528daea +F src/btree.c cb5b8ceb9baa02a63a2f83dec09c4153e1cfbdf9c2adef5c62c26d2160eeb067 +F src/btree.h e823c46d87f63d904d735a24b76146d19f51f04445ea561f71cc3382fd1307f0 +F src/btreeInt.h 9c0f9ea5c9b5f4dcaea18111d43efe95f2ac276cd86d770dce10fd99ccc93886 +F src/build.c 611e07299d72ff04bbcb9e7109183467e30925d203c3e121ef9bb3cf6876289b +F src/callback.c 3605bbf02bd7ed46c79cd48346db4a32fc51d67624400539c0532f4eead804ad +F src/carray.c ff6081a31878fc34df8fa1052a9cbf17ddc22652544dcb3e2326886ed1053b55 +F src/complete.c a3634ab1e687055cd002e11b8f43eb75c17da23e +F src/date.c e19e0cfff9a41bfdd884c655755f6f00bca4c1a22272b56e0dd6667b7ea893a2 +F src/dbpage.c c9ea81c11727f27e02874611e92773e68e2a90a875ef2404b084564c235fd91f +F src/dbstat.c 73362c0df0f40ad5523a6f5501224959d0976757b511299bf892313e79d14f5c +F src/delete.c 03a77ba20e54f0f42ebd8eddf15411ed6bdb06a2c472ac4b6b336521bf7cea42 +F src/expr.c 28b1cc3d2f147cc888703d5482f9581f17656d02abfa331c34370cb3350776be +F src/fault.c 460f3e55994363812d9d60844b2a6de88826e007 +F src/fkey.c 928ed2517e8732113d2b9821aa37af639688d752f4ea9ac6e0e393d713eeb76f +F src/func.c 0b802107498048d3dcac0b757720bcb8506507ce02159e213ab8161458eb293b +F src/global.c a19e4b1ca1335f560e9560e590fc13081e21f670643367f99cb9e8f9dc7d615b +F src/hash.c 03c8c0f4be9e8bcb6de65aa26d34a61d48a9430747084a69f9469fbb00ea52ca +F src/hash.h 46b92795a95bfefb210f52f0c316e9d7cdbcdd7e7fcfb0d8be796d3a5767cddf +F src/hwtime.h f9c2dfb84dce7acf95ce6d289e46f5f9d3d1afd328e53da8f8e9008e3b3caae6 +F src/in-operator.md 10cd8f4bcd225a32518407c2fb2484089112fd71 +F src/insert.c dfd311b0ac2d4f6359e62013db67799757f4d2cc56cca5c10f4888acfbbfa3fd +F src/json.c fb031340edee159c07ad37dbe668ffe945ed86f525b0eb3822e4a67cbc498a72 +F src/legacy.c d7874bc885906868cd51e6c2156698f2754f02d9eee1bae2d687323c3ca8e5aa +F src/loadext.c a3bc9a2522dc3b960e38b7582d1818f6245a49289387c2c7b19f27bfeabf1e81 +F src/main.c 65d11c17890966d271c925c6cc55e3ba50fa08374633cb99c0dee4719a20915a +F src/malloc.c 410e570b30c26cc36e3372577df50f7a96ee3eed5b2b161c6b6b48773c650c5e F src/mem0.c 6a55ebe57c46ca1a7d98da93aaa07f99f1059645 -F src/mem1.c 6919bcf12f221868ea066eec27e579fed95ce98b -F src/mem2.c f1940d9e91948dd6a908fbb9ce3835c36b5d83c3 -F src/mem3.c 8768ac94694f31ffaf8b4d0ea5dc08af7010a35a -F src/mem5.c 9bf955937b07f8c32541c8a9991f33ce3173d944 -F src/memjournal.c 3eb2c0b51adbd869cb6a44780323f05fa904dc85 -F src/msvc.h d9ba56c6851227ab44b3f228a35f3f5772296495 -F src/mutex.c 8e45800ee78e0cd1f1f3fe8e398853307f4a085c -F src/mutex.h 779d588e3b7756ec3ecf7d78cde1d84aba414f85 +F src/mem1.c 3bb59158c38e05f6270e761a9f435bf19827a264c13d1631c58b84bdc96d73b2 +F src/mem2.c c8bfc9446fd0798bddd495eb5d9dbafa7d4b7287d8c22d50a83ac9daa26d8a75 +F src/mem3.c 30301196cace2a085cbedee1326a49f4b26deff0af68774ca82c1f7c06fda4f6 +F src/mem5.c b7da5c10a726aacacc9ad7cdcb0667deec643e117591cc69cf9b4b9e7f3e96ff +F src/memdb.c a3feb427cdd4036ea2db0ba56d152f14c8212ca760ccb05fb7aa49ff6b897df3 +F src/memjournal.c c283c6c95d940eb9dc70f1863eef3ee40382dbd35e5a1108026e7817c206e8a0 +F src/msvc.h 80b35f95d93bf996ccb3e498535255f2ef1118c78764719a7cd15ab4106ccac9 +F src/mutex.c 06bcd9c3dbf2d9b21fcd182606c00fafb9bfe0287983c8e17acd13d2c81a2fa9 +F src/mutex.h a7b2293c48db5f27007c3bdb21d438873637d12658f5a0bf8ad025bb96803c4a F src/mutex_noop.c 9d4309c075ba9cc7249e19412d3d62f7f94839c4 -F src/mutex_unix.c 27bb6cc49485ee46711a6580ab7b3f1402211d23 -F src/mutex_w32.c 5e6fe1c298fb5a8a15aaed4161d5759311431c17 -F src/notify.c 9711a7575036f0d3040ba61bc6e217f13a9888e7 -F src/os.c 205fa2bad945a0dc7cad48f9f95ea3e8dc5408ff -F src/os.h 3e57a24e2794a94d3cf2342c6d9a884888cd96bf -F src/os_common.h b2f4707a603e36811d9b1a13278bffd757857b85 -F src/os_setup.h c9d4553b5aaa6f73391448b265b89bed0b890faa -F src/os_unix.c 821ed110197175165cf2f50b0930c7ff9a24504c -F src/os_win.c ccf29ddded3e41e506b6bd98c1171aa0963b23f2 -F src/os_win.h eb7a47aa17b26b77eb97e4823f20a00b8bda12ca -F src/pager.c 67cd2fbab58d0e35fed5f81432856f4f0af9fc6d -F src/pager.h f3eb324a3ff2408b28bab7e81c1c55c13720f865 -F src/parse.y d7bff41d460f2df96fb890f36700e85cb0fc5634 -F src/pcache.c 73895411fa6b7bd6f0091212feabbe833b358d23 -F src/pcache.h 4d0ccaad264d360981ec5e6a2b596d6e85242545 -F src/pcache1.c 72f644dc9e1468c72922eff5904048427b817051 -F src/pragma.c 80ee77226d0008d9188356a6cbbe6010866e1bee -F src/pragma.h 64c78a648751b9f4f297276c4eb7507b14b4628c -F src/prepare.c c12b786713df3e8270c0f85f988c5359d8b4d87c -F src/printf.c 63e6fb12bbe702dd664dc3703776c090383a5a26 -F src/random.c ba2679f80ec82c4190062d756f22d0c358180696 -F src/resolve.c 9f7ce3a3c087afb7597b7c916c99126ff3f12f0c -F src/rowset.c 9fe4b3ad7cc00944386bb600233d8f523de07a6e -F src/select.c ff80004a9a6ece891a8d9327a88e7b6e2588ee6d -F src/shell.c 0367440658104bf2ce8d8a9a5a713a4b11c9acbe -F src/sqlite.h.in f80c6ebd85588fc514bfedf3ecb00cec269cb410 -F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8 -F src/sqlite3ext.h dfbe62ffd95b99afe2140d8c35b180d11924072d -F src/sqliteInt.h 04ca5b3cdb3bcf87ba6300d5d36b51498f65f28c -F src/sqliteLimit.h 216557999cb45f2e3578ed53ebefe228d779cb46 -F src/status.c 70912d7be68e9e2dbc4010c93d344af61d4c59ba -F src/table.c 51b46b2a62d1b3a959633d593b89bab5e2c9155e -F src/tclsqlite.c 13debcc6a5ca1217486f8903768c01114fbe8b58 -F src/test1.c 4f1b42699068b7806af3111786f5ad760c2c1ff7 -F src/test2.c 5586f43fcd9a1be0830793cf9d354082c261b25b -F src/test3.c a8887dabbbee3059af338f20d290084a63ed1b0f -F src/test4.c d168f83cc78d02e8d35567bb5630e40dcd85ac1e -F src/test5.c 5a34feec76d9b3a86aab30fd4f6cc9c48cbab4c1 -F src/test6.c 41cacf3b0dd180823919bf9e1fbab287c9266723 -F src/test7.c 9c89a4f1ed6bb13af0ed805b8d782bd83fcd57e3 -F src/test8.c fa262391d3edea6490a71bfaa8fed477ccbbac75 -F src/test9.c bea1e8cf52aa93695487badedd6e1886c321ea60 -F src/test_async.c 21e11293a2f72080eda70e1124e9102044531cd8 -F src/test_autoext.c dea8a01a7153b9adc97bd26161e4226329546e12 -F src/test_backup.c 2e6e6a081870150f20c526a2e9d0d29cda47d803 -F src/test_blob.c e5a7a81d61a780da79101aeb1e60d300af169e07 -F src/test_btree.c 2e9978eca99a9a4bfa8cae949efb00886860a64f -F src/test_config.c 7985332c806d1cece793475c75a6abcccde9d331 -F src/test_demovfs.c 0de72c2c89551629f58486fde5734b7d90758852 -F src/test_devsym.c e7498904e72ba7491d142d5c83b476c4e76993bc -F src/test_fs.c a61f54247fdb843761d709879c3bcd1989b2050c -F src/test_func.c 37453d346cfcf118774efd5bf6187f7e6a7e3254 -F src/test_hexio.c abfdecb6fa58c354623978efceb088ca18e379cd -F src/test_init.c 66b33120ffe9cd853b5a905ec850d51151337b32 -F src/test_intarray.c 870124b95ec4c645d4eb84f15efb7133528fb1a5 -F src/test_intarray.h 9dc57417fb65bc7835cc18548852cc08cc062202 -F src/test_journal.c 5360fbe1d1e4416ca36290562fd5a2e3f70f32aa +F src/mutex_unix.c f7ee5a2061a4c11815a2bf4fc0e2bfa6fb8d9dc89390eb613ca0cec32fc9a3d1 +F src/mutex_w32.c 28f8d480387db5b2ef5248705dd4e19db0cfc12c3ba426695a7d2c45c48e6885 +F src/notify.c 57c2d1a2805d6dee32acd5d250d928ab94e02d76369ae057dee7d445fd64e878 +F src/os.c 509452169d5ea739723e213b8e2481cf0e587f0e88579a912d200db5269f5f6d +F src/os.h 1ff5ae51d339d0e30d8a9d814f4b8f8e448169304d83a7ed9db66a65732f3e63 +F src/os_common.h 6c0eb8dd40ef3e12fe585a13e709710267a258e2c8dd1c40b1948a1d14582e06 +F src/os_kv.c fb7ba8d6204197357f1eb7e1c7450d09c10043bf7e99aba602f4aa46b8fb11a3 +F src/os_setup.h 8efc64eda6a6c2f221387eefc2e7e45fd5a3d5c8337a7a83519ba4fbd2957ae2 +F src/os_unix.c dcf7988ddbdd68619b821c9a722f9377abb46f1d26c9279eb5a50402fd43d749 +F src/os_win.c a89b501fc195085c7d6c9eec7f5bd782625e94bb2a96b000f4d009703df1083f +F src/os_win.h 4c247cdb6d407c75186c94a1e84d5a22cbae4adcec93fcae8d2bc1f956fd1f19 +F src/pager.c cd562b878ea1b44d021ba199abc9d3b54f6b3347500a9fed03f66d6000620945 +F src/pager.h 6137149346e6c8a3ddc1eeb40aee46381e9bc8b0fcc6dda8a1efde993c2275b8 +F src/parse.y 619c3e92a54686c5e47923688c4b9bf7ec534a4690db5677acc28b299c403250 +F src/pcache.c 588cc3c5ccaaadde689ed35ce5c5c891a1f7b1f4d1f56f6cf0143b74d8ee6484 +F src/pcache.h 1497ce1b823cf00094bb0cf3bac37b345937e6f910890c626b16512316d3abf5 +F src/pcache1.c 131ca0daf4e66b4608d2945ae76d6ed90de3f60539afbd5ef9ec65667a5f2fcd +F src/pragma.c ecec75795c1821520266e4f93fa8840cce48979af532db06f085e36a7813860f +F src/prepare.c 2af0b5c1ec787c8eebd21baa9d79caf4a4dc3a18e76ce2edbf2027d706bca37a +F src/printf.c 7297c2aeed4d90d80c5ba82920d9e57b7bfad04b3466be1d7e042db382fe296e +F src/random.c 606b00941a1d7dd09c381d3279a058d771f406c5213c9932bbd93d5587be4b9c +F src/resolve.c 5616fbcf3b833c7c705b24371828215ad0925d0c0073216c4f153348d5753f0a +F src/rowset.c 8432130e6c344b3401a8874c3cb49fefe6873fec593294de077afea2dce5ec97 +F src/select.c fa67ce1dd60744c315791085897c1571ffe5554bf3e0410942b5fc9d7e0a4b56 +F src/shell.c.in 2c7e751795f38bb1855c35b556419cab5b8ba22e0f6758f5a629338065d6b79f +F src/sqlite.h.in c0979f9ac1f5be887397dd2a0bb485636893a81b34d64df85123aae9650c42f2 +F src/sqlite3.rc 015537e6ac1eec6c7050e17b616c2ffe6f70fca241835a84a4f0d5937383c479 +F src/sqlite3ext.h 7f236ca1b175ffe03316d974ef57df79b3938466c28d2f95caef5e08c57f3a52 +F src/sqliteInt.h 88f7fc9ce1630d9a5f7e0a8e1f3287cdc63882fba985c18e7eee1b9f457f59aa +F src/sqliteLimit.h fe70bd8983e5d317a264f2ea97473b359faf3ebb0827877a76813f5cf0cdc364 +F src/status.c 7565d63a79aa2f326339a24a0461a60096d0bd2bce711fefb50b5c89335f3592 +F src/table.c 0f141b58a16de7e2fbe81c308379e7279f4c6b50eb08efeec5892794a0ba30d1 +F src/tclsqlite.c 3c604c49e6cf4211960a9ddb9505280fd22cde32175f40884c641c0f5a286036 +F src/tclsqlite.h 614b3780a62522bc9f8f2b9fb22689e8009958e7aa77e572d0f3149050af348a +F src/test1.c 5d061afe479c7364842e0170be7220dea13389575fa6030d30b3e20bec4e1f75 +F src/test2.c 62f0830958f9075692c29c6de51b495ae8969e1bef85f239ffcd9ba5fb44a5ff +F src/test3.c 432646f581d8af1bb495e58fc98234380250954f5d5535e507fc785eccc3987a +F src/test4.c 0ac87fc13cdb334ab3a71823f99b6c32a6bebe5d603cd6a71d84c823d43a25a0 +F src/test5.c 38fa635a70a94f2aa8b47ecbab15d821386205d27ad4159c3551ab3ba45efa11 +F src/test6.c 9722054d37257459f1b8988e59e7db1dd630bfb291f16b2759764e778a9d1899 +F src/test8.c 206d8f3cc73950d252906656e2646b5de0d580b07187b635fcb3edd8c2c5fbc0 +F src/test9.c df9ddc7db6ef1b8cf745866ee229090779728bcbe660c7f297d3127ab21d92af +F src/test_autoext.c 14d4bbd3d0bd1eec0f6d16b29e28cf1e2d0b020d454835f0721a5f68121ac10f +F src/test_backup.c a2bfd90d2ff2511b8635507bdb30fa9b605ade19c16b533066cae3077f5bdb72 +F src/test_bestindex.c a9428931bec06de830b2630f57a7b1f2711761269f04df62b7aa1affcbce15bb +F src/test_blob.c 77b994e17f2c87055f44fd96c9a206c5a7155bae2cda2769af60c2f3582f962c +F src/test_btree.c 28283787d32b8fa953eb77412ad0de2c9895260e4e5bd5a94b3c7411664f90d5 +F src/test_config.c 18aa596d37de1d5968c439fd58ebf38bc4d9c9d1db63621504e241fde375cecd +F src/test_delete.c d0e8f6dc55cfc98a7c27c057fb88d512260564bf0b611482656c68b8f7f401ed +F src/test_demovfs.c 3efa2adf4f21e10d95521721687d5ca047aea91fa62dd8cc22ac9e5a9c942383 +F src/test_devsym.c 649434ed34d0b03fbd5a6b42df80f0f9a7e53f94dd1710aad5dd8831e91c4e86 +F src/test_fs.c a946408c81231feff4b6a2c18068f20aefe254dc82288687128af8b7520d32cb +F src/test_func.c 858d4dddb7acf88222ebcba7cffb585f6dde83e4a15b838c0d05ccdf8d5219b9 +F src/test_hexio.c a90baa0a8ab5e7cfe2216a61c9a31cfd1f8378353a3d23e25fa94c09aa755bb0 +F src/test_init.c 1649e02448f536e53172f6b1ff873254fe9a0c6c8a4502a2d25c0cc7b11945ea +F src/test_intarray.c 3fcf8ca7bb5c8776ea83f6aa9b66f8df0d1f37a99207b0097c8486f9c15cedbf +F src/test_intarray.h 6c3534641108cd1bea517a8e117dcba237081310a29a4c35bd2190caa8972293 +F src/test_journal.c a0b9709b2f12b1ec819eea8a1176f283bca6d688a6d4a502bd6fd79786f4e287 F src/test_loadext.c 337056bae59f80b9eb00ba82088b39d0f4fe6dfd -F src/test_malloc.c 96df9381a1ff1f6d3805ff7231b9baf1386aaabf -F src/test_multiplex.c 6a088d8d9d4aad4bec45dd8878af11b15900702d -F src/test_multiplex.h c08e4e8f8651f0c5e0509b138ff4d5b43ed1f5d3 -F src/test_mutex.c dbdfaff8580071f2212a0deae3325a93a737819c -F src/test_onefile.c 38f7cbe79d5bafe95bde683cc3a53b8ca16daf10 -F src/test_osinst.c 5423dc1d355f594371f27dd292ca54bd320b8196 -F src/test_pcache.c a5cd24730cb43c5b18629043314548c9169abb00 -F src/test_quota.c 180813f43683be5725458fc1ff13ac455d8e722d +F src/test_malloc.c 55e2b5398f6e184300d91278b8a84768f6a740735adc030411181945d5276da0 +F src/test_md5.c 811a45330c9391933360f998156a8907ee29909c828ab83ac05d329942cbea8f +F src/test_multiplex.c 82f0aa8eee629b6949782cfab8782ed35a9b56dc80d12877af52147f304d22b8 +F src/test_multiplex.h f0ff5b6f4462bfd46dac165d6375b9530d08089b7bcbe75e88e0926110db5363 +F src/test_mutex.c dacae6790956c0d4e705aaed2090227792e291b0496cccd688e9994c1e21f740 +F src/test_onefile.c f31e52e891c5fef6709b9fcef54ce660648a34172423a9cbdf4cbce3ba0049f4 +F src/test_osinst.c 269039d9c0820a02ee928014c30860d57ee757ecda54df42e463d0ca1377b835 +F src/test_pcache.c 496da3f7e2ca66aefbc36bbf22138b1eff43ba0dff175c228b760fa020a37bd0 +F src/test_quota.c 180e87437250bed7e17e4e61c106730939e39fec9be73d28961f27f579a92078 F src/test_quota.h 2a8ad1952d1d2ca9af0ce0465e56e6c023b5e15d -F src/test_rtree.c 43fff4c5a01576d6d213f27472598801a247890c -F src/test_schema.c 2bdba21b82f601da69793e1f1d11bf481a79b091 -F src/test_server.c a2615049954cbb9cfb4a62e18e2f0616e4dc38fe -F src/test_sqllog.c 0d138a8180a312bf996b37fa66da5c5799d4d57b -F src/test_superlock.c 06797157176eb7085027d9dd278c0d7a105e3ec9 -F src/test_syscall.c 2e21ca7f7dc54a028f1967b63f1e76155c356f9b -F src/test_tclvar.c d86412527da65468ee6fa1b8607c65d0af736bc4 -F src/test_thread.c af391ec03d23486dffbcc250b7e58e073f172af9 -F src/test_vfs.c 3b65d42e18b262805716bd96178c81da8f2d9283 -F src/test_vfstrace.c bab9594adc976cbe696ff3970728830b4c5ed698 -F src/test_windirent.c 8f5fada630348558d5745b334702f301da1ffc61 -F src/test_windirent.h b12055cab6227f7be10f5c19296f67c60cc5e2a5 +F src/test_rtree.c d844d746a3cc027247318b970025a927f14772339c991f40e7911583ea5ed0d9 +F src/test_schema.c b06d3ddc3edc173c143878f3edb869dd200d57d918ae2f38820534f9a5e3d7d9 +F src/test_sqllog.c 5abf04865758c0a3915b4ec2b2ee5ab75f74c00e2f05bf503b9083e0ab6829d7 +F src/test_superlock.c 3387fc794a68d8c6b6ed059aabacbfe870dc502c5cf65562f36aac78b4a4d629 +F src/test_syscall.c c5bf039261973135068aa68f4d185a6147333dcf266977989f8245b3a1968f1b +F src/test_tclsh.c c01706ac60bd3176754d3ccd37da74c6ad97c2e14489f8ed71b497c1c0ac0dd4 +F src/test_tclvar.c ae873248a0188459b1c16ca7cc431265dacce524399e8b46725c2b3b7e048424 +F src/test_thread.c 3edb4a5b5aeb1a6e9a275dccc848ac95acab7f496b3e9230f6d2d04953a2b862 +F src/test_vdbecov.c 5c426d9cd2b351f5f9ceb30cabf8c64a63bfcad644c507e0bd9ce2f6ae1a3bf3 +F src/test_vfs.c b4135c1308516adf0dfd494e6d6c33114e03732be899eace0502919b674586b5 +F src/test_window.c 6d80e11fba89a1796525e6f0048ff0c7789aa2c6b0b11c80827dc1437bd8ea72 F src/test_wsd.c 41cadfd9d97fe8e3e4e44f61a4a8ccd6f7ca8fe9 -F src/threads.c bbfb74450643cb5372a43ad4f6cffd7e9dfcecb0 -F src/tokenize.c 813934be70597edfbb685ae08fc4c8b549cf5a1e -F src/treeview.c dc39ccf04e9331237388b9cb73289c9d87ea050b -F src/trigger.c e14840ee0c3e549e758ec9bf3e4146e166002280 -F src/update.c a7eeeaffad59c6506f01303a071dac11de8269ca -F src/utf.c 10cc2519e82e3369344d0969ad4b1a333dc86d18 -F src/util.c 49ce0a65306c1c51d61cb5bc214c71cb62452de6 -F src/vacuum.c 2ddd5cad2a7b9cef7f9e431b8c7771634c6b1701 -F src/vdbe.c c193299e595a13eba247738e22fce25c49346a6c -F src/vdbe.h c743791f723049db94f009e3e30958952bc2d512 -F src/vdbeInt.h 4b69d5451bcadd473e745af53ef1e8abfdce0a79 -F src/vdbeapi.c 9324f6baee1a1b2284c6543e98f916888a81e459 -F src/vdbeaux.c deae5d3bd45da0e57c7d9e1d7436333d142dc3bb -F src/vdbeblob.c 3b570b730109e8f653d9d2081649f6e7015113db -F src/vdbemem.c 68fcfac37dc6601d98c32cc5adee4d39f2c1b7b4 -F src/vdbesort.c ef3c6d1f1a7d44cf67bb2bee59ea3d1fe5bad174 -F src/vdbetrace.c f75c5455d8cf389ef86a8bfdfd3177e0e3692484 -F src/vtab.c bef51b4f693d82b4b0184457faa8625654534091 -F src/vxworks.h d2988f4e5a61a4dfe82c6524dd3d6e4f2ce3cdb9 -F src/wal.c 1dd3f1aeed5765be62f27d7d2e44d46a68d891ab -F src/wal.h 2f7c831cf3b071fa548bf2d5cac640846a7ff19c -F src/walker.c 0f142b5bd3ed2041fc52d773880748b212e63354 -F src/where.c 984084584c10c41e46c89ac027a5cca991bc37e6 -F src/whereInt.h 78b6b4de94db84aecbdc07fe3e38f648eb391e9a -F src/wherecode.c 791a784bbf8749d560fdb0b990b607bc4f44a38d -F src/whereexpr.c de117970b29471177a6901d60ad83a194671dc03 +F src/threads.c 4ae07fa022a3dc7c5beb373cf744a85d3c5c6c3c +F src/tokenize.c cb3294cf23c11106b50d9af6998a6c1bf389b52e15b17698c9fab97bbaa9b37f +F src/treeview.c 3ce7ac9835d2d70cc1c868b01b747ae8a062322e155701e58e3d62ca79aada7a +F src/trigger.c d5cf2541ff048f30b6a0507eb3d1ec4e695c53584e3b2298a5bf248714fe185e +F src/update.c 3e5e7ff66fa19ebe4d1b113d480639a24cc1175adbefabbd1a948a07f28e37cf +F src/upsert.c 215328c3f91623c520ec8672c44323553f12caeb4f01b1090ebdca99fdf7b4f1 +F src/utf.c 7267c3fb9e2467020507601af3354c2446c61f444387e094c779dccd5ca62165 +F src/util.c 36fb1150062957280777655976f3f9a75db236cb8207a0770ceae8d5ec17fcd3 +F src/vacuum.c 1bacdd0a81d2b5dc1c508fbf0d938c89fa78dd8d5b46ec92686d44030d4f4789 +F src/vdbe.c b44c366e83412d3b8c190feb1f029b7d02e1bd69252a57b32f195107f0d03964 +F src/vdbe.h be33bd7b17f2ec92939642416030491508c51071f6c14e27cd195983fec56b63 +F src/vdbeInt.h 2aaeb6df2938b181b4700a9328688a3986f2bba71e8b96f6a80671316618fa49 +F src/vdbeapi.c 869a0da5d855495055f4d35c6ada582f64ce995ce14b26ff9d336274d497266c +F src/vdbeaux.c 908d8a191aed444b2e4c920159249127f3ff67b94c56a16fad1dfdf9c7488f20 +F src/vdbeblob.c b3f0640db9642fbdc88bd6ebcc83d6009514cafc98f062f675f2c8d505d82692 +F src/vdbemem.c 48e562ff27e6386eb8613207ac27d3d98c1f67fdc4775a1ab13759d2c2a1c021 +F src/vdbesort.c b69220f4ea9ffea5fdef34d968c60305444eea909252a81933b54c296d9cca70 +F src/vdbetrace.c 49e689f751505839742f4a243a1a566e57d5c9eaf0d33bbaa26e2de3febf7b41 +F src/vdbevtab.c fc46b9cbd759dc013f0b3724549cc0d71379183c667df3a5988f7e2f1bd485f3 +F src/vtab.c 5437ce986db2f70e639ce8a3fe68dcdfe64b0f1abb14eaebecdabd5e0766cc68 +F src/vxworks.h 9d18819c5235b49c2340a8a4d48195ec5d5afb637b152406de95a9436beeaeab +F src/wal.c 505a98fbc599a971d92cb90371cf54546c404cd61e04fd093e7b0c8ff978f9b6 +F src/wal.h ba252daaa94f889f4b2c17c027e823d9be47ce39da1d3799886bbd51f0490452 +F src/walker.c d5006d6b005e4ea7302ad390957a8d41ed83faa177e412f89bc5600a7462a014 +F src/where.c 287324fe73a0ae8e55b3be89bb2fe4148e3a8394e1e2f10ed2113713a037d8a3 +F src/whereInt.h 8d94cb116c9e06205c3d5ac87af065fc044f8cf08bfdccd94b6ea1c1308e65da +F src/wherecode.c 71c5c6804b7f882dec8ec858758accae02fcfca13df3cc720f1f258e663ec7c5 +F src/whereexpr.c 403a44eeec1a0f0914fccc6a59376b6924bc00ef6728fe6ffce4cf3051b320fc +F src/window.c 538195bbc75bb924e18e368fbd4ed731a3fe3f901351b44f6466ec486f53affe F test/8_3_names.test ebbb5cd36741350040fd28b432ceadf495be25b2 -F test/affinity2.test a6d901b436328bd67a79b41bb0ac2663918fe3bd +F test/affinity2.test 4d7a34d328e58ca2a2d78fd76c27614a41ca7ddf4312ded9c68c04f430b3b47d +F test/affinity3.test 9b7d1133e11d5edd7805573c4ab6f3ba73b0b74a1f280d5b130d4bf3506a93ff F test/aggerror.test a867e273ef9e3d7919f03ef4f0e8c0d2767944f2 -F test/aggnested.test b35b4cd69fc913f90d39a575e171e1116c3a4bb7 +F test/aggfault.test 777f269d0da5b0c2524c7ff6d99ae9a93db4f1b1839a914dd2a12e3035c29829 +F test/aggnested.test 610b0ce2c3e8f3daee25f9752800ee8d785db10da4aa1fbeea0ea1aabaf1d704 +F test/aggorderby.test 7be65e743f82ee49ba62da1c799e59341d23884a99edfe093df0cdfaac94cbbb F test/alias.test 4529fbc152f190268a15f9384a5651bbbabc9d87 -F test/all.test 6ff7b43c2b4b905c74dc4a813d201d0fa64c5783 -F test/alter.test 2facdddf08d0d48e75dc6cc312cd2b030f4835dd -F test/alter2.test 7ea05c7d92ac99349a802ef7ada17294dd647060 -F test/alter3.test b3568d11c38c4599c92f24242eda34144d78dc10 -F test/alter4.test c461150723ac957f3b2214aa0b11552cd72023ec -F test/altermalloc.test e81ac9657ed25c6c5bb09bebfa5a047cd8e4acfc +F test/all.test cf929f721e20960ca9db89471fa44f9176322ba8f25e97193f91881c223643b3 +F test/alter.test 3c00eff1e2036b9f93e9cd0f3d3e63750ac87ecb5bc71b9d7bd07cbf2ac4c494 +F test/alter2.test 7e3d26ab409df52df887b366a63902c3429b935c41cb962fd58ffc25784f2f19 +F test/alter3.test dcdd5f850f30656a45a0f05e41abfb52b74bbf6ccba165d0f7adf6b0116e4fd6 +F test/alter4.test 37cafe164067a6590a0ee4cec780bddbbaa33dc50b11542dcfbe0e65626494fd +F test/alterauth.test 63442ba61ceb0c1eeb63aac1f4f5cebfa509d352276059d27106ae256bafc959 +F test/alterauth2.test 48967abae0494d9a300d1c92473d99fcb66edfcc23579c89322f033f49410adc +F test/altercol.test b43fb5725332f4cf8cff0280605202c1672e808281accea60a066d2ccc5129e5 +F test/altercorrupt.test 2e1d705342cf9d7de884518ddbb053fd52d7e60d2b8869b7b63b2fda68435c12 +F test/alterdropcol.test a653a3945f964d26845ec0cd0a8e74189f46de3119a984c5bc45457da392612e +F test/alterdropcol2.test 527fce683b200d620f560f666c44ae33e22728e990a10a48a543280dfd4b4d41 +F test/alterfault.test 289067108947bedca27534edd4ff251bcd298cf84402d7b24eaa3749305418c6 +F test/alterlegacy.test f38c6d06cda39e1f7b955bbce57f2e3ef5b7cb566d3d1234502093e228c15811 +F test/altermalloc.test 167a47de41b5c638f5f5c6efb59784002b196fff70f98d9b4ed3cd74a3fb80c9 +F test/altermalloc2.test 17fb3724c4b004c469c27dc4ef181608aa644555fbd3f3236767584f73747c81 +F test/altermalloc3.test 8040e486368403f2fdd6fc3998258b499bd4cc2f3ddbb5f8f874cd436f076e81 +F test/alterqf.test 8ec03d776de9c391daa0078ea8f838903bdcfb11dfae4ba3576b48436834ccba +F test/altertab.test 8a2712f9076da5012a002d0b5cc0a421398a5bf61c25bab41b77c427586a7a27 +F test/altertab2.test 0889ba0700cc1cdb7bc7d25975aa61fece34f621de963d0886e2395716b38576 +F test/altertab3.test 471b8898d10bbc6488db9c23dc76811f405de6707d2d342b1b8b6fd1f13cd3c8 +F test/altertrig.test aacc980b657354fe2d3d4d3a004f07d04ccc1a93e5ef82d68a79088c274ddc6b F test/amatch1.test b5ae7065f042b7f4c1c922933f4700add50cdb9f -F test/analyze.test 3eb35a4af972f98422e5dc0586501b17d103d321 -F test/analyze3.test 1dccda46a6c374018af617fba00bfe297a61d442 -F test/analyze4.test eff2df19b8dd84529966420f29ea52edc6b56213 -F test/analyze5.test 765c4e284aa69ca172772aa940946f55629bc8c4 -F test/analyze6.test f1c552ce39cca4ec922a7e4e0e5d0203d6b3281f -F test/analyze7.test bb1409afc9e8629e414387ef048b8e0e3e0bdc4f -F test/analyze8.test c05a461d0a6b05991106467d0c47480f2e709c82 -F test/analyze9.test 88c1f2aa20b614236f03e1cc38c3619e7e8a38b4 -F test/analyzeA.test 3335697f6700c7052295cfd0067fc5b2aacddf9a -F test/analyzeB.test a4c1c3048f6d9e090eb76e83eecb18bcf6d31a70 -F test/analyzeC.test 555a6cc388b9818b6eda6df816f01ce0a75d3a93 -F test/analyzeD.test f3d77cd0fefe2849d784897d52df13beee19271d -F test/analyzeE.test 8684e8ac5722fb97c251887ad97e5d496a98af1d -F test/analyzeF.test 5d1fe1024ba2dfea3c18bede8c1ccef8aba1ab34 -F test/analyzer1.test 498e2ff4b62740c2751c3a2f8b744fe26689fae9 -F test/async.test 1d0e056ba1bb9729283a0f22718d3a25e82c277b -F test/async2.test c0a9bd20816d7d6a2ceca7b8c03d3d69c28ffb8b -F test/async3.test d73a062002376d7edc1fe3edff493edbec1fc2f7 -F test/async4.test 1787e3952128aa10238bf39945126de7ca23685a -F test/async5.test 383ab533fdb9f7ad228cc99ee66e1acb34cc0dc0 -F test/atof1.test ff0b0156fd705b67c506e1f2bfe9e26102bea9bd -F test/attach.test 437107943f14d131cf5efc2ae5305a94d7cb1d58 -F test/attach2.test 0ec5defa340363de6cd50fd595046465e9aaba2d -F test/attach3.test 359eb65d00102cdfcef6fa4e81dc1648f8f80b27 -F test/attach4.test 53bf502f17647c6d6c5add46dda6bac8b6f4665c -F test/attachmalloc.test 3a4bfca9545bfe906a8d2e622de10fbac5b711b0 -F test/auth.test 855233ef26eb3601b6886567ea4e326c72959360 -F test/auth2.test 264c6af53cad9aba5218c68bbe18036e39007bfa -F test/auth3.test 5cfa94ed90c6617c42b7ba4b133fd79678b251c7 -F test/autoinc.test c58912526998a39e11f66b533e23cfabea7f25b7 -F test/autoindex1.test 14b63a9f1e405fe6d5bfc8c8d00249c2ebaf13ea -F test/autoindex2.test af7e595c6864cc6ef5fc38d5db579a3e34940cb8 -F test/autoindex3.test a3be0d1a53a7d2edff208a5e442312957047e972 -F test/autoindex4.test 49d3cd791a9baa16fb461d7ea3de80d019a819cf -F test/autoindex5.test 96f084a5e6024ea07cace5888df3223f3ea86990 -F test/autovacuum.test 941892505d2c0f410a0cb5970dfa1c7c4e5f6e74 +F test/analyze.test 2fb21d7d64748636384e6cb8998dbf83968caf644c07fcb4f76c18f2e7ede94b +F test/analyze3.test c5156cef33f04b90a6b9e9d5d0bbc273a0fb44147d4508407bf1080811e2c6c8 +F test/analyze4.test 68bd069f3ac7ac1e652ddd9f04f57d5606ddb4208450f5297005db7aa0dd707d +F test/analyze5.test fa5131952303ac4146aba101b116b9c8cb89e2637531c334a6df7f7d19dddc0d +F test/analyze6.test 028f5bdfc9e5b5294768fa9a7185b8cd1d019aa7aab5b2f8ee42d7271d9a3b28 +F test/analyze7.test 079d17c495e396bdbd6cc6a083112788a6fbfb3b95c42e760e4270a53c9ead8f +F test/analyze8.test 29ef237d8a59b39cc31c3310134fefe96a690b195e3deed5ecb652839089f15c +F test/analyze9.test 30e1cb99336045a384a11d97900720184333c88174b3b89bc07444ea39e7df19 +F test/analyzeC.test ca20c103809836d15ab7bacb78d3dd9842c7d8743f492e5e5d058b2ffe4cd849 +F test/analyzeD.test 485f621cfd2ef0a8f8ac79672586651bfa495bd899db50461bb4b558400ab3c1 +F test/analyzeE.test d2ec7921c162cdc33ac8e7eb01f9ebf78100610af7c94c8552bbf551de1fb397 +F test/analyzeF.test 40b5cc3ad7b10e81020d7ca86f1417647ecfae7477cfd88acc5aa7ae1068f949 +F test/analyzeG.test 623be33038c49648872746c8dd8b23b5792c08fef173c55e82f1b12fca259852 +F test/analyzer1.test b6a624ec0af92eec209e1328465b66937c8fdf2fb442a3fa45321ddb3700f4aa +F test/atof1.test bd21c4a0e718ab1470de07a2a79f2544d7903be34feebcc80de04beee4807b00 +F test/atomic.test 065a453dde33c77ff586d91ccaa6ed419829d492dbb1a5694b8a09f3f9d7d061 +F test/atomic2.test b6863b4aa552543874f80b42fb3063f1c8c2e3d8e56b6562f00a3cc347b5c1da +F test/atrc.c c388fac43dbba05c804432a7135ae688b32e8f25818e9994ffba4b64cf60c27c +F test/attach.test 12b2a9872c1ce20edf40289f00d82d13faae59ced522d98496ab06ad5c5e0a1c +F test/attach2.test 6d1e3a457ce260d6fc8e5945c07fba6c76dc2aa90e1c701f067b50ee88f7315a +F test/attach3.test c59d92791070c59272e00183b7353eeb94915976 +F test/attach4.test 00e754484859998d124d144de6d114d920f2ed6ca2f961e6a7f4183c714f885e +F test/attachmalloc.test 67309af95c6b765c13e7d2279d7fccbef78e6eb0565d75d51cefd5dc88784549 +F test/auth.test 5b8558a40571ebc55c1581cb7cec3b2348a699542a0a51b83ef21c6a953d95e3 +F test/auth2.test 9eb7fce9f34bf1f50d3f366fb3e606be5a2000a1 +F test/auth3.test 76d20a7fa136d63bcfcf8bcb65c0b1455ed71078d81f22bcd0550d3eb18594ab +F test/autoanalyze1.test b9cc3f32a990fa56669b668d237c6d53e983554ae80c0604992e18869a0b2dec +F test/autoinc.test 9df9930966dbe92c55ef37a4d89112cfd537be0d0596d397177c12db9e581be0 +F test/autoindex1.test 65931519206bbec71948b11e125af0656435a0937973fe5fed70d776a712911f +F test/autoindex2.test 12ef578928102baaa0dc23ad397601a2f4ecb0df +F test/autoindex3.test ca502c8050166ac6107a7b4fe4e951f4d3270a23a958af02b14f1b962b83c4b6 +F test/autoindex4.test 3c2105e9172920e26f950ba3c5823e4972190e022c1e6f260ba476b0af24c593 +F test/autoindex5.test 3fb938cbf4e7f3896563ce04e2a24b0bc653fc6245b4bf3268cd7b20f441d87f +F test/autovacuum.test 00671369bbf96c6a49989a9425f5b78b94075d6a4b031e5e00000c2c32f365df +F test/autovacuum2.test 76f7eb4fe6a6bf6d33a196a7141dba98886d2fb53a268d7feca285d5da4759d7 F test/autovacuum_ioerr2.test 8a367b224183ad801e0e24dcb7d1501f45f244b4 -F test/avtrans.test 0252654f4295ddda3b2cce0e894812259e655a85 -F test/backcompat.test 19a1f337c68419b020a7481dd272a472c4ad8ef4 -F test/backup.test b79299a536a4c6d919094786595b95be56d02014 -F test/backup2.test 34986ef926ea522911a51dfdb2f8e99b7b75ebcf -F test/backup4.test 2a2e4a64388090b152de753fd9e123f28f6a3bd4 +F test/avfs.test 76f59743dc1f5fa533840d1818b420fe1ee45e21c0fd6bbac7942ba677903128 +F test/avtrans.test 7a6eae44763293024b137b53ff824d8500d754dbae060a8d940afbacfc1d4a15 +F test/backcompat.test f2431465ed668f09fc3f6998e56e893a1506ccea6e8b6f409f085f759f431b48 +F test/backup.test 3b08fd4af69f0fa786931103a31f4542b184aba16e239e5f22b18c3c2476697f +F test/backup2.test b153553ee5667b0748b43346b0725fbf80ce1f5544613bf087d669778b60ec56 +F test/backup4.test 8f6fd48e0dfde77b9a3bb26dc471ede3e101df32 F test/backup5.test ee5da6d7fe5082f5b9b0bbfa31d016f52412a2e4 F test/backup_ioerr.test 4c3c7147cee85b024ecf6e150e090c32fdbb5135 -F test/backup_malloc.test 7162d604ec2b4683c4b3799a48657fb8b5e2d450 +F test/backup_malloc.test 0c9abdf74c51e7bedb66d504cd684f28d4bd4027 F test/badutf.test d5360fc31f643d37a973ab0d8b4fb85799c3169f -F test/badutf2.test f5bc7f2d280670ecd79b9cf4f0f1760c607fe51f -F test/bc_common.tcl 3eda41ef9cda7d5f6c205462c96228b301da4191 -F test/between.test 34d375fb5ce1ae283ffe82b6b233e9f38e84fc6c +F test/badutf2.test f310fd3b24a491b6b77bccdf14923b85d6ebcce751068c180d93a6b8ff854399 +F test/basexx1.test 4ae6ddbd92a7ebcabb5d844664c3e755d29fb69c8ddcf0c8d59bbe4e07c23919 +F test/bc_common.tcl c70b896d1d4ce72f769d2c7c1fc15b2cb07559eb2093f2736c8ca51664b29ff5 +F test/bestindex1.test 856a453dff8c68b4568601eed5a8b5e20b4763af9229f3947c215729ed878db0 +F test/bestindex2.test 394ff8fbf34703391247116d6a44e1c50ee7282236ee77909044573cefc37bc0 +F test/bestindex3.test 34bea272b0e0f835651b16a3931dbe7ac927039be6b2e1cb617bbe1d584b492b +F test/bestindex4.test 3039894f2dad50f3a68443dffad1b44c9b067ac03870102df1ce3d9a46ea602e +F test/bestindex5.test a0c90b2dad7836e80a01379e200e5f8ec9476d49b349af02c0dbff2fb75dc98d +F test/bestindex6.test 16942535b551273f3ad9df8d7cc4b7f22b1fcd8882714358859eb049a6f99dd4 +F test/bestindex7.test f094c669a6400777f4d2ddc3ed28e39169f1adb5be3d59b55f22ccf8c414b71e +F test/bestindex8.test b63a4f171a2c83d481bb14c431a8b72e85d27b2ffdaa0435a95d58ca941678f9 +F test/bestindex9.test 1a4b93db117fd8abe74ae9be982f86aa72f01e60cd4ac541e6ede39673a451a0 +F test/bestindexA.test e1b5def6b190797cacf008e6815ffb78fb30261999030d60a728d572eef44c7f +F test/bestindexB.test 328b97b69cd1a20928d5997f9ecb04d2e00f1d18e19ab27f9e9adb44d7bc51ce +F test/bestindexC.test 95b4a527b1a5d07951d731604a6d4cf7e5a806b39cea0e7819d4c9667e11c3fc +F test/bestindexD.test 6a8f6f84990bcf17dfa59652a1f935beddb7afd96f8302830fbc86b0a13df3c3 +F test/bestindexE.test 297f3ea8500a8f3c17d6f78e55bdfee089064c6144ee84a110bd005a03338f49 +F test/between.test e7587149796101cbe8d5f8abae8d2a7b87f04d8226610aa1091615005dcf4d54 F test/bigfile.test aa74f4e5db51c8e54a1d9de9fa65d01d1eb20b59 F test/bigfile2.test 1b489a3a39ae90c7f027b79110d6b4e1dbc71bfc -F test/bigrow.test f0aeb7573dcb8caaafea76454be3ade29b7fc747 -F test/bigsort.test 8299fa9298f4f1e02fc7d2712e8b77d6cd60e5a2 -F test/bind.test 3c7b320969000c441a70952b0b15938fbb66237c +F test/bigmmap.test 6021e205487347c6d7e5a541aa472a4b8efc4e9f4a3799a823b61a8e6616105d +F test/bigrow.test 3e6b189dea465a78306eac67fc6754c1cf90eb1694a25c88c338900c216c1e6b +F test/bigsort.test 997e172009905873c06426145e4b3794c7dfe2d563724cb2fd39d45f319cf3d2 +F test/bind.test 1e136709b306f7ed3192d349c2930d89df6ab621654ad6f1a72381d3fe76f483 +F test/bind2.test 918bc35135f4141809ead7585909cde57d44db90a7a62aef540127148f91aab7 F test/bindxfer.test efecd12c580c14df5f4ad3b3e83c667744a4f7e0 F test/bitvec.test 75894a880520164d73b1305c1c3f96882615e142 F test/blob.test e7ac6c7d3a985cc4678c64f325292529a69ae252 +F test/bloom1.test 3b6277a647ac503b5d5df331037b0c01c40e88cc9537b94eaf2d8aa334ed4c8f F test/boundary1.tcl 6421b2d920d8b09539503a8673339d32f7609eb1 F test/boundary1.test 66d7f4706ccdb42d58eafdb081de07b0eb42d77b F test/boundary2.tcl e34ef4e930cf1083150d4d2c603e146bd3b76bcb @@ -508,181 +935,199 @@ F test/boundary3.tcl 23361e108a125dca9c4080c2feb884fe54d69243 F test/boundary3.test 56ef82096b4329aca2be74fa1e2b0f762ea0eb45 F test/boundary4.tcl 0bb4b1a94f4fc5ae59b79b9a2b7a140c405e2983 F test/boundary4.test 89e02fa66397b8a325d5eb102b5806f961f8ec4b -F test/btree01.test e08b3613540145b353f20c81cb18ead54ff12e0f -F test/btree02.test fe69453d474d8154d19b904157ff1db4812fed99 -F test/btreefault.test c2bcb542685eea44621275cfedbd8a13f65201e3 -F test/busy.test 76b4887f8b9160ba903c1ac22e8ff406ad6ae2f0 +F test/btree01.test 7207a813400798c7403dbfdd0b1eb04e204804257de384dd6f221f3b189cb7fc +F test/btree02.test 7555a5440453d900410160a52554fe6478af4faf53098f7235f1f443d5a1d6cc +F test/btreefault.test a82a23b0578bc587afbf9a622c8f54a54f63762f062ba8a35613cfee38ab42f9 +F test/busy.test caff7164c16ce06a53af51f9e4c2753d4cc64250e00790a5e48b9c4f4be37597 +F test/busy2.test 20823a5d7c42fb257d9f108c66312d90b1bb4ec3d80ba6b4e371073727560f98 F test/cache.test 13bc046b26210471ca6f2889aceb1ea52dc717de F test/cacheflush.test af25bb1509df04c1da10e38d8f322d66eceedf61 -F test/capi2.test 011c16da245fdc0106a2785035de6b242c05e738 -F test/capi3.test bf6f0308bbbba1e770dac13aa08e5c2ac61c7324 +F test/cachespill.test 895997f84a25b323b166aecb69baab2d6380ea98f9e0bcc688c4493c535cfab9 +F test/capi2.test 4ee545824adc3eb33bf57ef89f77440b28188ec3da72e5425ff0fcdba32e8d5a +F test/capi3.test ab90c548969613315605c555a8623f6b56e00e28d451c46a17ef73683c422c70 F test/capi3b.test efb2b9cfd127efa84433cd7a2d72ce0454ae0dc4 -F test/capi3c.test 06f6261f9e9b4ef6f76afcd9900f3665408af1c8 -F test/capi3d.test 485048dc5cd07bc68011e4917ad035ad6047ab82 +F test/capi3c.test 31d3a6778f2d06f2d9222bd7660c41a516d1518a059b069e96ebbeadb5a490f7 +F test/capi3d.test 8b778794af891b0dca3d900bd345fbc8ebd2aa2aae425a9dccdd10d5233dfbde F test/capi3e.test 3d49c01ef2a1a55f41d73cba2b23b5059ec460fe -F test/cast.test 4c275cbdc8202d6f9c54a3596701719868ac7dc3 -F test/cffault.test aadc1f61f8811cb600e3e069acbf8796f472a096 -F test/check.test 85a84bfc4be0e83f668747211c7cd45a6721d485 -F test/close.test 340bd24cc58b16c6bc01967402755027c37eb815 -F test/closure01.test b1703ba40639cfc9b295cf478d70739415eec6a4 +F test/carray01.test 49e2aedfdf2c715bc002d2773cdc1217166679639542c79c8aa4115f06421407 +F test/carray02.test 9d070b54f24a34d1f3b3c552ba34db0375a9d1c4219067416fb07d1595987c9d +F test/carrayfault.test 108a7d83904fc267c448e27c13b2a857c700bd6ddaa2f1e2518be718b159cb6b +F test/cast.test a2a3b32df86e3c0601ffa2e9f028a18796305d251801efea807092dbf374a040 +F test/cffault.test 9d6b20606afe712374952eec4f8fd74b1a8097ef +F test/changes.test 4377d202a487f66fc2822c1bf57c46798c8b2caf7446f4f701723b1dbb6b86f6 +F test/changes2.test 07949edcc732af28cb54276bfb7d99723bccc1e905a423648bf57ac5cb0dc792 +F test/check.test 3a7972ccbaad80d496833da8714d69d9d5d4ce9e7211af1cd2a06ae488a7de12 +F test/checkfault.test da6cb3d50247169efcb20bdf57863a3ccfa1d27d9e55cd324f0680096970f014 +F test/chunksize.test faea11c5d6df9d392252a8dd879e1b1d68c9d3e8b7909cbed8bcec3b60c706f1 +F test/cksumvfs.test 38748166f486c59e8d94f3ee705040031ed3790a4dd69c4a9eef64f623a30ad4 +F test/close.test eccbad8ecd611d974cbf47278c3d4e5874faf02d811338d5d348af42d56d647c +F test/closure01.test 9905883f1b171a4638f98fc764879f154e214a306d3d8daf412a15e7f3a9b1e0 F test/coalesce.test cee0dccb9fbd2d494b77234bccf9dc6c6786eb91 -F test/collate1.test 08c18e7512a5a32c97938854263fa15362eeb846 -F test/collate2.test 9aaa410a00734e48bcb27f3872617d6f69b2a621 +F test/collate1.test 0890fa372753b59eba53832d37328af815f6b8e4b16761823180eeb62c8e8f64 +F test/collate2.test 471c6f74573382b89b0f8b88a05256faa52f7964f9e4799e76708a3b1ece6ba4 F test/collate3.test 89defc49983ddfbf0a0555aca8c0521a676f56a5 F test/collate4.test c953715fb498b87163e3e73dd94356bff1f317bd -F test/collate5.test 65d928034d30d2d263a80f6359f7549ee1598ec6 +F test/collate5.test b1dfeff239ea69ee9225832553f423d37a6184eb730cee06f6846ab4e3c6dbef F test/collate6.test 8be65a182abaac8011a622131486dafb8076e907 F test/collate7.test 8ec29d98f3ee4ccebce6e16ce3863fb6b8c7b868 F test/collate8.test cd9b3d3f999b8520ffaa7cc1647061fc5bab1334 F test/collate9.test 3adcc799229545940df2f25308dd1ad65869145a F test/collateA.test b8218ab90d1fa5c59dcf156efabb1b2599c580d6 -F test/colmeta.test 2c765ea61ee37bc43bbe6d6047f89004e6508eb1 -F test/colname.test 08948a4809d22817e0e5de89c7c0a8bd90cb551b -F test/conflict.test 841bcf7cabbfca39c577eb8411ea8601843b46a8 -F test/conflict2.test a82dd3b9b41fceb5dd6ff0707c5c7ffba208d538 -F test/conflict3.test dec0634c0f31dec9a4b01c63063e939f0cd21b6b +F test/collateB.test 9c840e21f9aead6fc533cea310f0bd202d5c11511088811b7e93ae7b47fccb24 +F test/colmeta.test 248a644cec4c7c371cf1e107fd8fdba708dc290866c572672b6260e3466cce79 +F test/colname.test 0e32125de701c6bd86247194c6f5639b740b4f71a0d88ee1e67ff3bda9ae99ca +F test/columncount.test 6fe99c2f35738b0129357a1cf3fa483f76140f4cd8a89014c88c33c876d2638f +F test/conflict.test 3307ffdf988e04b01c4e942d8aa369a977f085bf629f43a627c9a77f39d65926 +F test/conflict2.test 5557909ce683b1073982f5d1b61dfb1d41e369533bfdaf003180c5bc87282dd1 +F test/conflict3.test 81865d9599609aca394fb3b9cd5f561d4729ea5b176bece3644f6ecb540f88ac F test/contrib01.test 2a1cbc0f2f48955d7d073f725765da6fbceda6b4 -F test/corrupt.test 141c39ea650c1365e85a49e402fa05cb9617fb97 -F test/corrupt2.test cb787825d761b0f869764d6990531382840de872 -F test/corrupt3.test 4b548d0bbe2933bc81d3f54099a05fc4d28aff18 -F test/corrupt4.test b99652079d542b21f4965f6248703b983e40fe80 -F test/corrupt5.test 8ead52af76006f3286e9396cb41898018ccea107 -F test/corrupt6.test 269548d19427ac554c830763b1c5ea54a0252f80 -F test/corrupt7.test e4fa6d6584276679cc1d20c4e58beb9559a4eb85 +F test/corrupt.test 54509b182b1927663e0a425b681b0935a08a01b11d8153a4a9545ed36760ebe2 +F test/corrupt2.test 4ce5eadd51baa1aedb48e141dd885d155946f5c3677bb032547e350ce91b17f4 +F test/corrupt3.test 6a982535d52c8165654cbc79a043cfd0bf02495a5efbf4754295e056fc548539 +F test/corrupt4.test 5fa4559bcfd14afbb99670d463546ba75fb4975c710b7f6dfa592ae90471cce7 +F test/corrupt5.test 387be3250795e2a86e6234745558b80efb248a357d0cd8e53bce75c7463f545d +F test/corrupt6.test fc6a891716139665dae0073b6945e3670bf92568 +F test/corrupt7.test ffa86896fe63a3d00b0a131e1e64f402e4da9f7e5d89609d6501c851e511d73a F test/corrupt8.test 2399dfe40d2c0c63af86706e30f3e6302a8d0516 F test/corrupt9.test 730a3db08d4ab9aa43392ea30d9c2b4879cbff85 -F test/corruptA.test 53e56dafd180addcdadb402244b8cb9771d2ba26 +F test/corruptA.test 112f4b2ae0b95ebf3ea63718642fb969a93acea557ace3a307234d19c245989b F test/corruptB.test 73a8d6c0b9833697ecf16b63e3c5c05c945b5dec -F test/corruptC.test 0c46574f8d4f27ecc799b1b5c4cbf9b1817bce9a -F test/corruptD.test b3c205fac7952b1de645ce44bb02335cd9e3e040 -F test/corruptE.test be8e5088c369fc7979c662cd644efdaafc0f7f6d +F test/corruptC.test 7d6d9e907334ea3ccb7111a0656cafa30a28f8a5f2aaf1c45ad712236302856a +F test/corruptD.test 614320aa519f6bf6c7dd2f581f9513ff7b6826954180cca1a606d0e25ea084a3 +F test/corruptE.test 4143791f2dfb443aec5b7fabfa5821e6063eccc3b49b06f212c2f014715fd476 F test/corruptF.test be9fde98e4c93648f1ba52b74e5318edc8f59fe4 -F test/corruptG.test 1ab3bf97ee7bdba70e0ff3ba2320657df55d1804 -F test/corruptH.test 99ad81a4bda7cc078c589ef7542ecbc64e453c80 -F test/corruptI.test 347babbf970e7947e3f91dccf7a1bec28a1bab04 -F test/corruptJ.test 9e29e7a81ee3b6ac50f77ea7a9e2f3fa03f32d91 -F test/cost.test 1eedbfd868f806f3fa08ff072b04cf270dcf61c8 -F test/count.test cb2e0f934c6eb33670044520748d2ecccd46259c -F test/coveridxscan.test cdb47d01acc4a634a34fd25abe85189e0d0f1e62 -F test/crash.test fb9dc4a02dcba30d4aa5c2c226f98b220b2b959f +F test/corruptG.test adf79b669cbfd19e28c8191a610d083ae53a6d51 +F test/corruptH.test 79801d97ec5c2f9f3c87739aa1ec2eb786f96454 +F test/corruptI.test 9d8cbf6214e492abe9e822e759b9751ae336cec0a6fe3ff3b37bfbd8ff9c22ca +F test/corruptJ.test 4d5ccc4bf959464229a836d60142831ef76a5aa4 +F test/corruptK.test ac13504593d89d69690d45479547616ed12644d42b5cb7eeb2e759a76fc23dcb +F test/corruptL.test 652fc8ac0763a6fd3eb28b951d481924167b2d9936083bcc68253b2274a0c8fe +F test/corruptM.test 7d574320e08c1b36caa3e47262061f186367d593a7e305d35f15289cc2c3e067 +F test/corruptN.test a034bb217bebd8d007625dfb078e76ec3d53515052dbceb68bd47b2c27674d5c +F test/cost.test cc434a026b1e9d0d98137a147e24e5daf1b1ad09e9ff7da63b34c83ddd136d92 +F test/count.test cd4bd531066e8d77ef8fe1e3fc8253d042072e117ccab214b290cf83f1602249 +F test/countofview.test 4088e461a10ee33e69803c177a69aa1d7bba81a9ffc2df66d76465a22ca7fdfc +F test/coveridxscan.test f35c7208dedc4f98e471c569df64c0f95a49f6e072d8dc7c8f99bdee2697de1b +F test/crash.test f699152b8ae759bdf1c19c278b135f5d43fa4b6466e63489cd02edbc94aebad0 F test/crash2.test 5b14d4eb58b880e231361d3b609b216acda86651 F test/crash3.test 8f5de9d32ab9ab95475a9efe7f47a940aa889418 F test/crash4.test fe2821baf37168dc59dd733dcf7dba2a401487bc -F test/crash5.test 05dd3aa9dbb751a22d5cdaf22a9c49b6667aa219 +F test/crash5.test 4aa55e7ac3c4bc511873e457aa65d2827d52da9b51e061511899dadcfe22b1e8 F test/crash6.test 4c56f1e40d0291e1110790a99807aa875b1647ba F test/crash7.test 1a194c4900a255258cf94b7fcbfd29536db572df -F test/crash8.test 61442a9964ab6b124fc5254e4258b45747842e6f +F test/crash8.test 3a0c39c079b441a9b1da22e3edb58817f9dd330c4c3a7f9dd1f9f7c8368ea352 F test/crashM.test d95f59046fa749b0d0822edf18a717788c8f318d F test/crashtest1.c 09c1c7d728ccf4feb9e481671e29dda5669bbcc2 -F test/createtab.test b5de160630b209c4b8925bdcbbaf48cc90b67fe8 -F test/cse.test 277350a26264495e86b1785f34d2d0c8600e021c -F test/ctime.test 7bd009071e242aac4f18521581536b652b789a47 -F test/cursorhint.test 7bc346788390475e77a345da2b92270d04d35856 -F test/date.test 984ac1e3e5e031386866f034006148d3972b4a65 -F test/dbstatus.test 8de104bb5606f19537d23cd553b41349b5ab1204 -F test/dbstatus2.test 10418e62b3db5dca070f0c3eef3ea13946f339c2 -F test/default.test 0cb49b1c315a0d81c81d775e407f66906a2a604d -F test/delete.test e1bcdf8926234e27aac24b346ad83d3329ec8b6f +F test/createtab.test 85cdfdae5c3de331cd888d6c66e1aba575b47c2e3c3cc4a1d6f54140699f5165 +F test/cse.test 00b3aea44b16828833c94fbe92475fd6977583fcb064ae0bc590986812b38d0c +F test/csv01.test 2ab5514005fd308995c8910bc313e47f0368b94213b9d6c27f9a2da78796a091 +F test/ctime.test 340f362f41f92972bbd71f44e10569a5cc694062b692231bd08aa6fe6c1c4773 +F test/cursorhint.test 05cf0febe5c5f8a31f199401fd1c9322249e753950d55f26f9d5aca61408a270 +F test/cursorhint2.test 6f3aa9cb19e7418967a10ec6905209bcbb5968054da855fc36c8beee9ae9c42f +F test/dataversion1.test 6e5e86ac681f0782e766ebcb56c019ae001522d114e0e111e5ebf68ccf2a7bb8 +F test/date.test 328ed63091d34c8a6e8dcec4f999a031ce310f24395b8f0f67f07eaf36cbfd1f +F test/date2.test 7e12ec14aaf4d5e6294b4ba140445b0eca06ea50062a9c3a69c4ee13d0b6f8b1 +F test/date3.test a1b77abf05c6772fe5ca2337cac1398892f2a41e62bce7e6be0f4a08a0e64ae5 +F test/date4.test b5ad22baf7394e008ac59383840159daedd45be31dcf74a3b2450ec0e28955ce +F test/date5.test 14ba189bc4d03efc371dd5302e035764f6633355a3e13acb4a45e7b33530231e +F test/dbdata.test 042f49acff3438f940eeba5868d3af080ae64ddf26ae78f80c92bec3ca7d8603 +F test/dbfuzz.c fc566102f72c8af84ae8077b4faf7f056c571e6fa7a32e98b66e42b7505f47b6 +F test/dbfuzz001.test 6c9a4622029d69dc38926f115864b055cb2f39badd25ec22cbfb130c8ba8e9c3 +F test/dbfuzz2-seed1.db e6225c6f3d7b63f9c5b6867146a5f329d997ab105bee64644dc2b3a2f2aebaee +F test/dbfuzz2.c 4b3c12de4d98b1b2d908ab03d217d4619e47c8b23d5e67f8a6f2b1bdee7cae23 +F test/dblwidth-a.sql eb4141518610e52f931a55a984310075e98dc31eee5a28ae806b1e35377be85a +F test/dbpage.test 63fab1eb026bada121107e53436fa749bbf83281dc9dea17af422f7a5c0f289f +F test/dbpagefault.test ea39de2ca86041a9c6df1135645180a76d0a8da93ac159e2fafe38e39636530b +F test/dbstatus.test 4a4221a883025ffd39696b3d1b3910b928fb097d77e671351acb35f3aed42759 +F test/dbstatus2.test a36518c0f0951d8fd5a3dc36f99948ad1af93fb7fc0d2e03e5bb5a643186cf52 +F test/decimal.test a11b87a2c3294eb4c11b55f3168aeac63d683fa8ada35921a6ec1d511eff7648 +F test/default.test c7124864cded213a3f118bc7e2e26f34b7c36dfa26cf6945cc8b7f5db1191277 +F test/delete.test 2686e1c98d552ef37d79ad55b17b93fe96fad9737786917ce3839767f734c48f F test/delete2.test 3a03f2cca1f9a67ec469915cb8babd6485db43fa F test/delete3.test 555e84a00a99230b7d049d477a324a631126a6ab -F test/delete4.test 3ac0b8d23689ba764c2e8b78c1b56b8f1b942fa2 -F test/descidx1.test 6d03b44c8538fe0eb4924e19fba10cdd8f3c9240 -F test/descidx2.test 9f1a0c83fd57f8667c82310ca21b30a350888b5d -F test/descidx3.test 09ddbe3f5295f482d2f8b687cf6db8bad7acd9a2 +F test/delete4.test 2bcf66fe9978206e8fad1bacb69c00fe05dd1a3f2036c421420f6360f7d18b53 +F test/delete_db.test 096d828493c7907f9ea11a7098ea6a0f73edba89406487d5d6cc2228dc4ab8b0 +F test/descidx1.test edc8adee58d491b06c7157c50364eaf1c3605c9c19f8093cb1ea2b6184f3ac13 +F test/descidx2.test a0ba347037ff3b811f4c6ceca5fd0f9d5d72e74e59f2d9de346a9d2f6ad78298 +F test/descidx3.test 953c831df7ea219c73826dfbf2f6ee02d95040725aa88ccb4fa43d1a1999b926 F test/diskfull.test 106391384780753ea6896b7b4f005d10e9866b6e -F test/distinct.test a1783b960ad8c15a77cd9f207be072898db1026c -F test/distinctagg.test 1a6ef9c87a58669438fc771450d7a72577417376 -F test/e_blobbytes.test 439a945953b35cb6948a552edaec4dc31fd70a05 -F test/e_blobclose.test 4b3c8c60c2171164d472059c73e9f3c1844bb66d -F test/e_blobopen.test e95e1d40f995056f6f322cd5e1a1b83a27e1a145 -F test/e_blobwrite.test 650ded42ee5e0c2dbf263583ce01adf280129599 -F test/e_changes.test fd66105385153dbf21fdb35eb8ef6c3e1eade579 -F test/e_createtable.test d4c6059d44dcd4b636de9aae322766062b471844 +F test/distinct.test 691c9e850b0d0b56b66e7e235453198cb4cf0760e324b7403d3c5abbeab0a014 +F test/distinct2.test 4d6316b6487a0aa5a90bee111575c957e2a5ba5a9be9156febe9533ce78876e8 +F test/distinctagg.test 40d7169ae5846caaf62c6e307d2ca3c333daf9b6f7cde888956a339a97afe85f +F test/e_blobbytes.test 4c01dfe4f12087b92b20705a3fdfded45dc4ed16d5a211fed4e1d2786ba68a52 +F test/e_blobclose.test 692fc02a058476c2222a63d97e3f3b2b809c1842e5525ded7f854d540ac2e075 +F test/e_blobopen.test 29f6055ee453b8e679fe9570c4d3acfedbef821622c5dad16875148c5952ef50 +F test/e_blobwrite.test 3075ff539827576d9a34cbb5a2ac75eb65fb49cd5aadc27686b0719fbf99c156 +F test/e_changes.test 0f8c3e6aab7335cb772d5a3ea34ca4c82f98d0eb896e2eb3add971c16984b405 +F test/e_createtable.test 31b9bcb6ac8876bc7ec342d86d9c231a84c62b442093a6651dfd0fa93650eea3 F test/e_delete.test ab39084f26ae1f033c940b70ebdbbd523dc4962e -F test/e_droptrigger.test 3cd080807622c13e5bbb61fc9a57bd7754da2412 -F test/e_dropview.test 0c9f7f60989164a70a67a9d9c26d1083bc808306 -F test/e_expr.test 03a84a6fa9bd3472112d6bd4599f5269f5f74803 -F test/e_fkey.test a1783fe1f759e1990e6a11adfcf0702dac4d0707 -F test/e_fts3.test 5c02288842e4f941896fd44afdef564dd5fc1459 -F test/e_insert.test 3de217e95094d3d165992a6de1164bbc4bd92dc7 -F test/e_reindex.test 2bebf7b393e519198b7c654407221cf171a439b8 +F test/e_droptrigger.test 235c610f8bf8ec44513e222b9085c7e49fad65ad0c1975ac2577109dd06fd8fa +F test/e_dropview.test 74e405df7fa0f762e0c9445b166fe03955856532e2bb234c372f7c51228d75e7 +F test/e_expr.test 0a1e175caddc78b27306647cb4ce2362c55790190f8cdd178b75fd6262eb8f76 +F test/e_fkey.test feeba6238aeff9d809fb6236b351da8df4ae9bda89e088e54526b31a0cbfeec5 +F test/e_fts3.test 17ba7c373aba4d4f5696ba147ee23fd1a1ef70782af050e03e262ca187c5ee07 +F test/e_insert.test f02f7f17852b2163732c6611d193f84fc67bc641fb4882c77a464076e5eba80e +F test/e_reindex.test 027bb13d2c7e9e865886eed6349f126a273f8037899b636bf5fb53c7fc815921 F test/e_resolve.test a61751c368b109db73df0f20fc75fb47e166b1d8 -F test/e_select.test 52692ff3849541e828ad4661fe3773a9b8711763 +F test/e_select.test 327a15f14068bbd6f647cedc67210f8680fcb2f05e481a0a855fccd2abfa1292 F test/e_select2.test aceb80ab927d46fba5ce7586ebabf23e2bb0604f -F test/e_totalchanges.test b12ee5809d3e63aeb83238dd501a7bca7fd72c10 +F test/e_totalchanges.test c927f7499dc3aa28b9b556b7d6d115a2f0fe41f012b128d16bf1f3b30e9b41e4 F test/e_update.test f46c2554d915c9197548681e8d8c33a267e84528 -F test/e_uri.test eed3eb41b22d051a1164110dacdc778899126e14 -F test/e_vacuum.test 5bfbdc21b65c0abf24398d0ba31dc88d93ca77a9 -F test/e_wal.test ae9a593207a77d711443ee69ffe081fda9243625 -F test/e_walauto.test 280714ddf14e1a47dcbc59d515cd0b026dfd5567 +F test/e_uri.test 86564382132d9c453845eeb5293c7e375487b625900ab56c181a0464908417d8 +F test/e_vacuum.test 89fc48e8beee2f9dfd6de1fbb2edea6542dae9121dc0fbe6313764169e742104 +F test/e_wal.test db7c33642711cf3c7959714b5f012aca08cacfa78da0382f95e849eb3ba66aa4 +F test/e_walauto.test 248af31e73c98df23476a22bdb815524c9dc3ba8 F test/e_walckpt.test 28c371a6bb5e5fe7f31679c1df1763a19d19e8a0 -F test/e_walhook.test da3ea8b3483d1af72190337bda50155a91a4b664 -F test/enc.test e54531cd6bf941ee6760be041dff19a104c7acea -F test/enc2.test 83437a79ba1545a55fb549309175c683fb334473 -F test/enc3.test 6807f7a7740a00361ca8d0ccd66bc60c8dc5f2b6 +F test/e_walhook.test 01b494287ba9e60b70f6ebf3c6c62e0ffe01788e344a4846b08e5de0b344cb66 +F test/emptytable.test a38110becbdfa6325cd65cb588dca658cd885f62 +F test/enc.test b5503a87b31cea8a5084c6e447383f9ca08933bd2f29d97b6b6201081b2343eb +F test/enc2.test 872afe58db772e7dfa1ad8e0759f8cc820e9efc8172d460fae83023101c2e435 +F test/enc3.test 55ef64416d72975c66167310a51dc9fc544ba3ae4858b8d5ab22f4cb6500b087 F test/enc4.test c8f1ce3618508fd0909945beb8b8831feef2c020 -F test/eqp.test 85873fa5816c48915c82c4e74cb5c35a5b48160f -F test/errmsg.test f31592a594b44ee121371d25ddd5d63497bb3401 -F test/eval.test a64c9105d6ff163df7cf09d6ac29cdad5922078c -F test/exclusive.test c7ebbc756eacf544c108b15eed64d7d4e5f86b75 -F test/exclusive2.test 32798111aae78a5deec980eee383213f189df308 +F test/eqp.test 746db9fe11629a0d00328e1721cc2a2e4726d574b677ab14de35fd914f54cc82 +F test/eqp2.test 6e8996148de88f0e7670491e92e712a2920a369b4406f21a27c3c9b6a46b68dd +F test/errmsg.test eae9f091eb39ce7e20305de45d8e5d115b68fa856fba4ea6757b6ca3705ff7f9 +F test/errofst1.test 6da78363739ba8991f498396ab331b5d64e7ab5c4172c12b5884683ef523ac53 +F test/eval.test 73969a2d43a511bf44080c44485a8c4d796b6a4f038d19e491867081155692c0 +F test/exclusive.test 7ff63be7503990921838d5c9f77f6e33e68e48ed1a9d48cd28745bf650bf0747 +F test/exclusive2.test cd70b1d9c6fffd336f9795b711dcc5d9ceba133ad3f7001da3fda63615bdc91e F test/exec.test e949714dc127eaa5ecc7d723efec1ec27118fdd7 -F test/exists.test 8f7b27b61c2fbe5822f0a1f899c715d14e416e30 -F test/expr.test 79c3e7502d9e571553b85f0ecc8ff2ac7d0e4931 -F test/extension01.test 00d13cec817f331a687a243e0e5a2d87b0e358c9 +F test/exists.test 79a75323c78f02bbe9c251ea502a092f9ef63dac +F test/existsexpr.test 75c1c13cda18b53f68c135fe644afc034d72a6de5b0774b0219ca4a6dc4b96b0 +F test/existsexpr2.test dc23e76389eff3d29f6488ff733012a3560cd67ec8cfaecbecd52cced5d5af11 +F test/existsfault.test ff41c11f3052c1bbd4f8dd557802310026253d67d7c4e3a180c16d2f0862973e +F test/expr.test db981f8a85520e99ae20aab7ad2e9b5b0437ed09159b57ced434c672075d2e61 +F test/expr2.test c27327ae9c017a7ff6280123f67aff496f912da74d78c888926d68b46ec75fd8 +F test/exprfault.test da33606d799718e2f8e34efd0e5858884a1ad87f608774c552a7f5517cc27181 +F test/exprfault2.test c49e84273898969af5dbc4fe6a3f4335f14639799f343590336c9ddf84425965 +F test/extension01.test 5de412c66276105901c370770175003381fdcb0c4da7054fa43cf4a31e0bfa3a +F test/external_reader.test 6fdec43eeca23eb32faad1e95a4d1abc402bc8b3db70df12d6fc08a637f4a2b5 F test/extraquick.test cb254400bd42bfb777ff675356aabf3287978f79 -F test/fallocate.test 3e979af17dfa7e5e9dda5eba1a696c04fa9d47f7 -F test/filectrl.test 7c13f96457435238da99aff7343ad6a3a4885787 -F test/filefmt.test cb34663f126cbc2d358af552dcaf5c72769b0146 -F test/fkey1.test 13e3d48236a2b9f5c5ebd232eef9b3ab682a8a2c -F test/fkey2.test f3d27ecba480a348c328965d154214719bb158a9 +F test/fallocate.test 37a62e396a68eeede8f8d2ecf23573a80faceb630788d314d0a073d862616717 +F test/filectrl.test 4b720117388cf6766d0b798e2dddd785953f8f371633b0c0084d2f34cf72336a +F test/filefmt.test f393e80c4b8d493b7a7f8f3809a8425bbf4292af1f5140f01cb1427798a2bbd4 +F test/filter1.test 590f8ba9a0cd0823b80d89ac75c5ce72276189cef9225d2436adaf1ee87f3727 +F test/filter2.tcl 44e525497ce07382915f01bd29ffd0fa49dab3adb87253b5e5103ba8f93393e8 +F test/filter2.test 3cc20eaea2ea1ab245197cc4a62468deb460b78f5aa9bd7d5d3353c2fe569bae +F test/filterfault.test c08fb491d698e8df6c122c98f7db1c65ffcfcad2c1ab0e07fa8a5be1b34eaa8b +F test/fkey1.test e563bcb4cb108ce3f40363cda4f84009dc89a39e2973076e5057ba99fca35378 +F test/fkey2.test 1063d65e5923c054cfb8f0555a92a3ae0fa8c067275a33ee1715bd856cdb304c F test/fkey3.test 76d475c80b84ee7a5d062e56ccb6ea68882e2b49 F test/fkey4.test 86446017011273aad8f9a99c1a65019e7bd9ca9d -F test/fkey5.test 5a373303f201ac03c22ba1ef17a733d3f56e611a -F test/fkey6.test abb59f866c1b44926fd02d1fdd217d831fe04f48 -F test/fkey7.test 72e915890ee4a005daaf3002cb208e8fe973ac13 -F test/fkey8.test 7bd1dd0174a0e29a90c62c517b9e2a410a0b345d +F test/fkey5.test 6727452e163a427147e84e739da18713da553d79f9783559b04fdcd36d5c7421 +F test/fkey6.test 668a7299e75899b0a3342c36df655be57f76a05aca3544bda939a6e676e2f000 +F test/fkey7.test 64fb28da03da5dfe3cdef5967aa7e832c2507bf7fb8f0780cacbca1f2338d031 +F test/fkey8.test 51deda7f1a1448bca95875e4a6e1a3a75b4bd7215e924e845bd60de60e4d84bf F test/fkey_malloc.test 594a7ea1fbab553c036c70813cd8bd9407d63749 -F test/fordelete.test eb93a2f34137bb87bdab88fcab06c0bd92719aff -F test/format4.test 1f0cac8ff3895e9359ed87e41aaabee982a812eb +F test/fordelete.test ba98f14446b310f9c9d935b97ec748753d0144a28b356ba30d1f4f6958fdde5c +F test/fork-test.c 9ac2e6423a1d38df3d6be0e8ac15608b545de21e2b19d9d876254c5931b63edb +F test/format4.test eeae341953db8b6bda7f549044797c3278a6cc345d11ada81471671b654f8ef4 +F test/fp-speed-1.c b37de94eba034e1703668816225f54510ec60fb0685406608cc707afe6b8234d +F test/fpconv1.test d5d8aa0c427533006c112fb1957cdd1ea68c1d0709470dabb9ca02c2e4c06ad8 F test/fts-9fd058691.test 78b887e30ae6816df0e1fed6259de4b5a64ad33c -F test/fts1a.test 46090311f85da51bb33bd5ce84f7948359c6d8d7 -F test/fts1b.test 5d8a01aefbecc8b7442b36c94c05eb7a845462d5 -F test/fts1c.test 85a525ce7428907469b4cce13d5563ce542ce64c -F test/fts1d.test a73deace5c18df4a549b12908bade4f05dcf1a2f -F test/fts1e.test 77244843e925560b5a0b70069c3e7ab62f181ed2 -F test/fts1f.test 2d6cb10d8b7a4e6edc321bbdb3982f1f48774714 -F test/fts1i.test 6bfe08cdfdced063a39a50c8601da65e6274d879 -F test/fts1j.test e3797475796043a161e348c46a309664cac83f7f -F test/fts1k.test 65d3b41487b9f738d11b0f00eca375c0ca6bd970 -F test/fts1l.test 15c119ed2362b2b28d5300c0540a6a43eab66c36 -F test/fts1m.test 2d9ca67b095d49f037a914087cc0a61e89da4f0c -F test/fts1n.test a2317dcd27b1d087ee3878b30e0a59c593c98b7a -F test/fts1o.test fd92f82ea9090dbc2a13d4cd58aafe5afa371b6a -F test/fts1porter.test d86e9c3e0c7f8ff95add6582b4b585fb4e02b96d -F test/fts2.test e3fb95f96a650411574efc136f3fb10eef479ed7 -F test/fts2a.test 473a5c8b473a4e21a8e3fddaed1e59666e0c6ab7 -F test/fts2b.test 964abc0236c849c07ca1ae496bb25c268ae94816 -F test/fts2c.test ffb5a35230ac72c4354535c547965ce6824537c0 -F test/fts2d.test b7eaa671ca9a16997f3e5b158ee777ae21052b0b -F test/fts2e.test 2da13dbc2d009105f42196845c1e1ce136c03d38 -F test/fts2f.test cf84096235991709c1e61caa389632aa0a4f976d -F test/fts2g.test d49d6f6c900e6e20a0fb980ec1cd568dee12af76 -F test/fts2h.test 223af921323b409d4b5b18ff4e51619541b174bb -F test/fts2i.test 1b22451d1f13f7c509baec620dc3a4a754885dd6 -F test/fts2j.test 298fa1670aa21cd445b282d139b70c72e7ade12b -F test/fts2k.test c7ebf4a4937594aa07459e3e1bca1251c1be8659 -F test/fts2l.test 3333336621524cf7d60bb62d6ef6ab69647866ed -F test/fts2m.test 4b30142ead6f3ed076e880a2a464064c5ad58c51 -F test/fts2n.test 12b9c5352128cebd1c6b8395e43788d4b09087c2 -F test/fts2o.test 09cd920d585ebf1c2910b6be869efa286e308a84 -F test/fts2p.test 4b48c35c91e6a7dbf5ac8d1e5691823cc999aafb -F test/fts2q.test b2fbbe038b7a31a52a6079b215e71226d8c6a682 -F test/fts2r.test b154c30b63061d8725e320fba1a39e2201cadd5e -F test/fts2token.test d8070b241a15ff13592a9ae4a8b7c171af6f445a F test/fts3.test 672a040ea57036fb4b6fdc09027c18d7d24ab654 -F test/fts3_common.tcl 99cf6659b87c0f74f55963c2aea03b3a7d66ceb0 -F test/fts3aa.test 6c263a6f8845205ee02550981a94c2e8dc1e7058 +F test/fts3_common.tcl dffad248f9ce090800e272017d2898005c28ee6314fc1dd5550643a02666907a +F test/fts3aa.test 814d60a1ba30b4a71d8f9306a6564bc7b636dd6efacd2ad80306f9b23ef3ebee F test/fts3ab.test 7f6cf260ae80dda064023df8e8e503e9a412b91f F test/fts3ac.test 636ed7486043055d4f126a0e385f2d5a82ebbf63 F test/fts3ad.test e40570cb6f74f059129ad48bcef3d7cbc20dda49 @@ -691,181 +1136,261 @@ F test/fts3af.test d394978c534eabf22dd0837e718b913fd66b499c F test/fts3ag.test c003672a215124df7fc6000036d896f498b26b53 F test/fts3ah.test dc9f66c32c296f1bc8bcc4535126bddfeca62894 F test/fts3ai.test 24058fdc6e9e5102c1fd8459591b114b6a85d285 -F test/fts3aj.test 0ed71e1dd9b03b843a857dc3eb9b15630e0104fc -F test/fts3ak.test bd14deafe9d1586e8e9bf032411026ac4f8c925d +F test/fts3aj.test 1560a7ce5642dc887e8ecfcc4693bcfce1dbb3d1771a735c845f0061e525deb2 +F test/fts3ak.test 36ea92f609efb390cf018cdb5d389c12e62b650abe31cfc88261b252daf88174 F test/fts3al.test 07d64326e79bbdbab20ee87fc3328fbf01641c9f F test/fts3am.test 218aa6ba0dfc50c7c16b2022aac5c6be593d08d8 F test/fts3an.test a49ccadc07a2f7d646ec1b81bc09da2d85a85b18 -F test/fts3ao.test 3e4e3d5e75c076520341d0bdf4eb17c00e8cbde2 -F test/fts3atoken.test 76262be798f23a390717d14266f0df551e52a7ee -F test/fts3auto.test b981fea19b132b4e6878f50d7c1f369b28f68eb9 -F test/fts3aux1.test f8f287a4a73f381f8fa15b6a70f36245f903d221 -F test/fts3aux2.test 7ae2b2c13aefdf4169279a27a5f51780ce57f6ba -F test/fts3b.test e93bbb653e52afde110ad53bbd793f14fe7a8984 +F test/fts3ao.test 266989148fec6d9f1bb6c5382f7aa3dcea0e9cd444576e28dd2b9287ac7dd220 +F test/fts3atoken.test dc2078ce464914efe3a8dfc545dd034a0fc14f2ab425c240471d5a5f1c721400 +F test/fts3atoken2.test aa6664d26277064844ead962f5988c28b091c51b112f36d30fa33bd83fecf906 +F test/fts3auto.test 649aa4c198d7acc5cd6355e19ee073d051c40d9e88a43fc3d88af46bdf3e99d5 +F test/fts3aux1.test 1880eaa75c586cd10f53080479a2b819b3915ae7ce55c4e0ba8f1fe05ac0a6a7 +F test/fts3aux2.test 2459e7fa3e22734aed237d1e2ae192f5541c4d8b218956ad2d90754977bf907f +F test/fts3b.test c15c4a9d04e210d0be67e54ce6a87b927168fbf9c1e3faec8c1a732c366fd491 F test/fts3c.test fc723a9cf10b397fdfc2b32e73c53c8b1ec02958 F test/fts3comp1.test a0f5b16a2df44dd0b15751787130af2183167c0c -F test/fts3conf.test 1c8b8adb0ab28a41b68d1514df44380bd7353402 -F test/fts3corrupt.test 2710b77983cc7789295ddbffea52c1d3b7506dbb -F test/fts3corrupt2.test 6d96efae2f8a6af3eeaf283aba437e6d0e5447ba -F test/fts3cov.test e0fb00d8b715ddae4a94c305992dfc3ef70353d7 -F test/fts3d.test d3e9c8fb75135ada06bf3bab4f9666224965d708 -F test/fts3defer.test 0be4440b73a2e651fc1e472066686d6ada4b9963 -F test/fts3defer2.test c540f5f5c2840f70c68fd9b597df817ec7170468 +F test/fts3conf.test c9cd45433b6787d48a43e84949aa2eb8b3b3d242bac7276731c1476290d31f29 +F test/fts3corrupt.test 6732477c5ace050c5758a40a8b5706c8c0cccd416b9c558e0e15224805a40e57 +F test/fts3corrupt2.test e318f0676e5e78d5a4b702637e2bb25265954c08a1b1e4aaf93c7880bb0c67d0 +F test/fts3corrupt3.test 0d5b69a0998b4adf868cc301fc78f3d0707745f1d984ce044c205cdb764b491f +F test/fts3corrupt4.test c7f414fe29b97a478d15c90382c4ae077a2bbd2283bf8c63bf66dadaaed3edb8 +F test/fts3corrupt5.test 0549f85ec4bd22e992f645f13c59b99d652f2f5e643dac75568bfd23a6db7ed5 +F test/fts3corrupt6.test f417c910254f32c0bc9ead7affa991a1d5aec35b3b32a183ffb05eea78289525 +F test/fts3corrupt7.test 1da31776e24bb91d3c028e663456b61280b121a74496ccf2fef3fe33790ad2b0 +F test/fts3cov.test 1e5ecea0e4c1394cea97adcfb9fd3d2d5998fd563dacf465f413e6c7fa5cffb3 +F test/fts3d.test 2bd8c97bcb9975f2334147173b4872505b6a41359a4f9068960a36afe07a679f +F test/fts3defer.test f4c20e4c7153d20a98ee49ee5f3faef624fefc9a067f8d8d629db380c4d9f1de +F test/fts3defer2.test 3bbe54a7fca7d548bb7ac4f59447ee591070bfbe0c9f3e279defa0b898e9afbb F test/fts3defer3.test dd53fc13223c6d8264a98244e9b19abd35ed71cd F test/fts3drop.test 1b906e293d6773812587b3dc458cb9e8f3f0c297 +F test/fts3dropmod.test 7de242ea1c8a713a8b143ea54468f4b1c4953fa068349e23ac178e2c90c59889 F test/fts3e.test 1f6c6ac9cc8b772ca256e6b22aaeed50c9350851 -F test/fts3expr.test 3401d47b229c4504424caf362cc4ff704cad4162 -F test/fts3expr2.test 18da930352e5693eaa163a3eacf96233b7290d1a +F test/fts3expr.test ebae205a7a89446c32583bcd492dcb817b9f6b31819bb4dde2583bb99c77e526 +F test/fts3expr2.test ef381978b9bdcfc6d4b47c86270ba356a75dbff164ce52ff5d18a5924a788614 F test/fts3expr3.test c4d4a7d6327418428c96e0a3a1137c251b8dfbf8 -F test/fts3expr4.test c39a15d676b14fc439d9bf845aa7bddcf4a74dc3 -F test/fts3expr5.test f9abfffbf5e53d48a33e12a1e8f8ba2c551c9b49 -F test/fts3fault.test da49627b280b210ebc6657f76344c7851f10ce66 -F test/fts3fault2.test f953bb3cf903988172270a9a0aafd5a890b0f98f +F test/fts3expr4.test 6c7675bbdbffe6ffc95e9e861500b8ac3f739c4d004ffda812f138eeb1b45529 +F test/fts3expr5.test a5b9a053becbdb8e973fbf4d6d3abaabeb42d511d1848bd57931f3e0a1cf983e +F test/fts3f.test 8c438d5e1cab526b0021988fb1dc70cf3597b006a33ffd6c955ee89929077fe3 +F test/fts3fault.test 9228f00cd69e2a5d2ed0f06c181981f4f90bd36da9f86b73f3a58b4b23451fd4 +F test/fts3fault2.test 7b2741e5095367238380b0fcdb837f36c24484c7a5f353659b387df63cf039ec +F test/fts3fault3.test ccdd2292dd2d4e21e30fc5f4c8e064f79e516087eec5ff57ab6bc4f6a7714097 F test/fts3first.test dbdedd20914c8d539aa3206c9b34a23775644641 -F test/fts3join.test 34750f3ce1e29b2749eaf0f1be2fa6301c5d50da +F test/fts3fuzz001.test c78afcd8ad712ea0b8d2ed50851a8aab3bc9dc52c64a536291e07112f519357c +F test/fts3integrity.test 0c6fe7353d7b24d78862f4272ee9df4da2f32b3ff30fa3396945cda8119580a8 +F test/fts3join.test de31d304ba479043a7d33d2f201c514b3e1da809da6797d7a58704d00e8da2e6 F test/fts3malloc.test b0e4c133b8d61d4f6d112d8110f8320e9e453ef6 -F test/fts3matchinfo.test ce864e0bd92429df8008f31cf557269ba172482a +F test/fts3matchinfo.test aa66cc50615578b30f6df9984819ae5b702511cf8a94251ec7c594096a703a4a +F test/fts3matchinfo2.test 00144e841704b8debfcdf6097969cd9f2a1cf759e2203cda42583648f2e6bf58 +F test/fts3misc.test 9ec15e7c0b5831a6353bd4c46bf3acdf1360eda5d9f396f667db4d05bcf92ecf F test/fts3near.test 7e3354d46f155a822b59c0e957fd2a70c1d7e905 -F test/fts3offsets.test b85fd382abdc78ebce721d8117bd552dfb75094c +F test/fts3offsets.test eb114b16e4d2495133bc2710d9c05ccd5ac90319718248fdf2dd379cdaa85358 F test/fts3prefix.test fa794eaab0bdae466494947b0b153d7844478ab2 F test/fts3prefix2.test e1f0a822ca661dced7f12ce392e14eaf65609dce -F test/fts3query.test f33eb71a1fe1084ea585eeb7ee76b390729f5170 +F test/fts3query.test 45806a302921b245a9dba5d85c9d51fb98b3f137eea6e6bf6eae4883e041ab72 +F test/fts3rank.test cd99bc83a3c923c8d52afd90d86979cf05fc41849f892faeac3988055ef37b99 F test/fts3rnd.test 1320d8826a845e38a96e769562bf83d7a92a15d0 F test/fts3shared.test 57e26a801f21027b7530da77db54286a6fe4997e -F test/fts3snippet.test 01a4231816e03a0660ae53ba2404fe69012fe0db +F test/fts3snippet.test 560c7f38c5fa591d88e367eac1313b64e503625616708ff61da9d5f52cbf75e5 +F test/fts3snippet2.test 03f6738ab3897bea2ba6be424a0613872e167acbf37a66200d655d737b470f65 F test/fts3sort.test ed34c716a11cc2009a35210e84ad5f9c102362ca -F test/fts3tok1.test 178c050199af8c05299b1ad572514ce1c54b7827 +F test/fts3tok1.test a663f4cac22a9505400bc22aacb818d7055240409c28729669ea7d4cc2120d15 F test/fts3tok_err.test 52273cd193b9036282f7bacb43da78c6be87418d -F test/fts3varint.test 752c08ed5d32c5d7dc211b056f4ed68a76b7e36e -F test/fts4aa.test 10aac8e9d62c7357590acfabe3fad01e9a9ce1cb -F test/fts4check.test c3056eab9524232e4c9bdcd119912947e07bcc1c -F test/fts4content.test 05716af19a899cd70d5cd916c580043c03f30db4 +F test/fts3varint.test 0b84a3fd4eba8a39f3687523804d18f3b322e6d4539a55bf342079c3614f2ada +F test/fts4aa.test 0e6bfd6a81695a39b23e448dda25d864e63dda75bde6949c45ddc95426c6c3f5 +F test/fts4check.test f0ea5e5581951d8ef7a341eea14486daf6c5f516a2f3273b0d5e8cb8a6cd3bd2 +F test/fts4content.test 73bbb123420d2c46ef2fb3b24761e9acdb78b0877179d3a5d7d57aada08066f6 F test/fts4docid.test e33c383cfbdff0284685604d256f347a18fdbf01 -F test/fts4growth.test 60d6bb3f78e25b34f533797dd9f2f9402310a13a +F test/fts4growth.test 289833c34ad45a5e6e6133b53b6a71647231fb89d36ddcb8d9c87211b6721d7f F test/fts4growth2.test 13ad4e76451af6e6906c95cdc725d01b00044269 F test/fts4incr.test 4e353a0bd886ea984e56fce9e77724fc923b8d0d -F test/fts4langid.test 8bd8759e0d4b04d71771544b861193a6841fee84 -F test/fts4merge.test c424309743fdd203f8e56a1f1cd7872cd66cc0ee +F test/fts4intck1.test 54e7f28e34b72fb0c614d414bb1f568154d463c5a00b20944e893df858372ed4 +F test/fts4langid.test 1cc6fe045f094f1695d0b20e6b817a2ce22ec470cb7c6577470cd71ed0256e90 +F test/fts4lastrowid.test 185835895948d5325c7710649824042373b2203149abe8024a9319d25234dfd7 +F test/fts4merge.test 57d093660a5093ae6e9fbd2d17592a88b45bbd66db2703c4b640b28828dbe38b F test/fts4merge2.test 5faa558d1b672f82b847d2a337465fa745e46891 -F test/fts4merge3.test aab02a09f50fe6baaddc2e159c3eabc116d45fc7 -F test/fts4merge4.test d895b1057a7798b67e03455d0fa50e9ea836c47b -F test/fts4noti.test 5553d7bb2e20bf4a06b23e849352efc022ce6309 -F test/fts4onepass.test 7319d61a2ed1325fc54afd0c060a0513b462303a -F test/fts4unicode.test 27378af76394542cf490cf001d8d1505fe55f6a9 +F test/fts4merge3.test 8d9ccb4a3d41c4c617a149d6c4b13ad02de797d0 +F test/fts4merge4.test 66fce89934cd9508cbdc67de486558c34912ffb2e8ffe5c9a1bbb9b8a4408ba7 +F test/fts4merge5.test 69932d85cda8a1c4dcfb742865900ed8fbda51724b8cf9a45bbe226dfd06c596 +F test/fts4min.test 1c11e4bde16674a0c795953509cbc3731a7d9cbd1ddc7f35467bf39d632d749f +F test/fts4noti.test d5d933705b1b1516b67a5e3f8e514ecb19c6522fb3357bb744776d48427c2292 +F test/fts4onepass.test d69ddc4ee3415e40b0c5d1d0408488a87614d4f63ba9c44f3e52db541d6b7cc7 +F test/fts4opt.test 0fd0cc84000743ff2a883b9b84b4a5be07249f0ba790c8848a757164cdd46b2a +F test/fts4record.test a48508f69a84c9287c8019d3a1ae712f5730d8335ffaf8e2101e691d078950bb +F test/fts4rename.test 2e0565ffd92b2c51f1a757df0b8f2ca30537197106fec09e943919801d173692 +F test/fts4umlaut.test fcaca4471de7e78c9d1f7e8976e3e8704d7d8ad979d57a739d00f3f757380429 +F test/fts4unicode.test 82a9c16b68ba2f358a856226bb2ee02f81583797bc4744061c54401bf1a0f4c9 +F test/fts4upfrom.test f25835162c989dffd5e2ef91ec24c4848cc9973093e2d492d1c7b32afac1b49d F test/full.test 6b3c8fb43c6beab6b95438c1675374b95fab245d -F test/func.test ae97561957aba6ca9e3a7b8a13aac41830d701ef -F test/func2.test 772d66227e4e6684b86053302e2d74a2500e1e0f -F test/func3.test d202a7606d23f90988a664e88e268aed1087c11c -F test/func4.test 6beacdfcb0e18c358e6c2dcacf1b65d1fa80955f -F test/func5.test cdd224400bc3e48d891827cc913a57051a426fa4 -F test/fuzz-oss1.test 4912e528ec9cf2f42134456933659d371c9e0d74 -F test/fuzz.test 96083052bf5765e4518c1ba686ce2bab785670d1 +F test/func.test b6b6942b29e1b00120cb4ed00ba8ed8ef561e7ba6ae1c41cc005143c7e6f5bf6 +F test/func2.test 69f6ae3751b4ec765bdc3b803c0a255aa0f693f28f44805bef03e6b4a3fd242f +F test/func3.test 6b22dfada95b778d01dba75aa00fa8e6a1fb90d30b859241638e7540edcfb3ca +F test/func4.test a02e695f62beb31cb092dccf6873ff97543407fff97a5f3ec4da70b5b337bc84 +F test/func5.test 863e6d1bd0013d09c17236f8a13ea34008dd857d87d85a13a673960e4c25d82a +F test/func6.test 3bc89ec0f2605736d3a118f43d25ef58115a7db4dba8ae939a363917d815c0bb +F test/func7.test 7e009275f52c52954c8c028fdb62f8bc16cc47276fcc8753c1d2b22c6e074598 +F test/func8.test c4e2ecacf9f16e47a245e7a25fbabcc7e78f9c7c41a80f158527cdfdc6dd299d +F test/func9.test 32c7d53b9bc99ddd45fad29457add438f2aca43d24920793773a96b13610f552 +F test/fuzz-oss1.test 514dcabb24687818ea949fa6760229eaacad74ca70157743ef36d35bbe01ffb0 +F test/fuzz.test 819ea7e483bcee91209aacbe6f9eaf3287baa1841479ee5f639f57c5e7c42b86 F test/fuzz2.test 76dc35b32b6d6f965259508508abce75a6c4d7e1 -F test/fuzz3.test b47377143f0c80f91ed29d722861077ff34415d5 -F test/fuzz_common.tcl a87dfbb88c2a6b08a38e9a070dabd129e617b45b -F test/fuzz_malloc.test 328f70aaca63adf29b4c6f06505ed0cf57ca7c26 -F test/fuzzcheck.c 3309d793165ca61a9996271cb799694839348f9a -F test/fuzzdata1.db 7ee3227bad0e7ccdeb08a9e6822916777073c664 -F test/fuzzdata2.db f03a420d3b822cc82e4f894ca957618fbe9c4973 +F test/fuzz3.test 70ba57260364b83e964707b9d4b5625284239768ab907dd387c740c0370ce313 +F test/fuzz4.test c229bcdb45518a89e1d208a21343e061503460ac69fae1539320a89f572eb634 +F test/fuzz_common.tcl b7197de6ed1ee8250a4f82d67876f4561b42ee8cbbfc6160dcb66331bad3f830 +F test/fuzz_malloc.test f348276e732e814802e39f042b1f6da6362a610af73a528d8f76898fde6b22f2 +F test/fuzzcheck.c 34a025386f84d818cd3343e69e9d9083091af83153e226d71d4e1c126b5f1dd0 +F test/fuzzdata1.db 3e86d9cf5aea68ddb8e27c02d7dfdaa226347426c7eb814918e4d95475bf8517 +F test/fuzzdata2.db 128b3feeb78918d075c9b14b48610145a0dd4c8d6f1ca7c2870c7e425f5bf31f F test/fuzzdata3.db c6586d3e3cef0fbc18108f9bb649aa77bfc38aba -F test/fuzzdata4.db 1882f0055fb63214d8407ddc7aca9b0b1c59af21 +F test/fuzzdata4.db b502c7d5498261715812dd8b3c2005bad08b3a26e6489414bd13926cd3e42ed2 +F test/fuzzdata5.db e35f64af17ec48926481cfaf3b3855e436bd40d1cfe2d59a9474cb4b748a52a5 +F test/fuzzdata6.db b8725a5f5cf7a3b7241a9038e57ca7e7cc8c3f4d86b44bd770617bda245ab2b0 +F test/fuzzdata7.db 0166b56fd7a6b9636a1d60ef0a060f86ddaecf99400a666bb6e5bbd7199ad1f2 +F test/fuzzdata8.db 8f34ae00d8d5d4747dd80983cf46161065e4f78324dcff3c893506ff8db3a4a6 F test/fuzzer1.test 3d4c4b7e547aba5e5511a2991e3e3d07166cfbb8 F test/fuzzer2.test a85ef814ce071293bce1ad8dffa217cbbaad4c14 -F test/fuzzerfault.test 8792cd77fd5bce765b05d0c8e01b9edcf8af8536 +F test/fuzzerfault.test f64c4aef4c9e9edf1d6dc0d3f1e65dcc81e67c996403c88d14f09b74807a42bc +F test/fuzzinvariants.c 3ddfec7f5b970b018f1a982532de905cf180e0c1e48cd653be9365d3e6177625 +F test/gcfault.test 4ea410ac161e685f17b19e1f606f58514a2850e806c65b846d05f60d436c5b0d +F test/gencol1.test ceb3163b59cb77f4ad57ae4f01a143ce36b06fdd6a8dab1149235db89979ffd8 F test/genesis.tcl 1e2e2e8e5cc4058549a154ff1892fe5c9de19f98 -F test/hexlit.test d7b0a5f41123df1e43985b91b8b2e70f95282d21 +F test/having.test a89236dd8d55aa50c4805f82ac9daf64d477a44d712d8209c118978d0ca21ec9 +F test/hexlit.test 4a6a5f46e3c65c4bf1fa06f5dd5a9507a5627751 F test/hidden.test 23c1393a79e846d68fd902d72c85d5e5dcf98711 -F test/hook.test 162d7cef7a2d2b04839fe14402934e6a1b79442f -F test/icu.test 70df4faca133254c042d02ae342c0a141f2663f4 -F test/ieee754.test 806fc0ce7f305f57e3331eaceeddcfec9339e607 -F test/imposter1.test c3f1db2d3db2c24611a6596a3fc0ffc14f1466c8 -F test/in.test 61a24ae38d4b64ec69f06ccdf022992f68a98176 +F test/hook.test 2d89bf9480646feb8093be3a58ea502d6521906779ed960de31dd9c4502c0541 +F test/hook2.test b9ff3b8c6519fb67f33192f1afe86e7782ee4ac8 +F test/icu.test 8da7d52cd9722c82f33b0466ed915460cb03c23a38f18a9a2d3ff97da9a4a8c0 +F test/ieee754.test 0d3ab84ab2069c9994c833a7cd820ee6037f0cf888e206a4a7fc05f735d5790a +F test/imposter1.test 5a20b2cdeb53e65fc57cdb10a33750bd4ef6259909eaf1972253b9e79f7a3fb2 +F test/in.test edf979bff3244b9e47849e2b43886631354c8213791f42da92216f08012141af F test/in2.test 5d4c61d17493c832f7d2d32bef785119e87bde75 F test/in3.test 3cbf58c87f4052cee3a58b37b6389777505aa0c0 -F test/in4.test d2b38cba404bc4320f4fe1b595b3d163f212c068 -F test/in5.test 1de657472fa9ac2924be25c2c959ac5ca1aae554 -F test/incrblob.test e81846d214f3637622620fbde7cd526781cfe328 -F test/incrblob2.test a5ce5ed1d0b01e2ed347245a21170372528af0a5 -F test/incrblob3.test d8d036fde015d4a159cd3cbae9d29003b37227a4 -F test/incrblob4.test f26502a5697893e5acea268c910f16478c2f0fab -F test/incrblob_err.test af1f12ba60d220c9752073ff2bda2ad59e88960d -F test/incrblobfault.test 280474078f6da9e732cd2a215d3d854969014b6e +F test/in4.test e7b1456d423453884aeb9d68fafe9c8e7be7abddc8f028c04205e67820f10772 +F test/in5.test 4fd79c70dfa0681313e8cdca07f5ff0400bdc0e20f808a5c59eaef1e4b48082a +F test/in6.test f5f40d6816a8bb7c784424b58a10ac38efb76ab29127a2c17399e0cbeeda0e4b +F test/in7.test d9efdee00b074a60c6343993b2eda78bc369ab080dad864513c73f8aca89d566 +F test/incrblob.test c9b96afc292aeff43d6687bcb09b0280aa599822 +F test/incrblob2.test a494c9e848560039a23974b9119cfc2cf3ad3bd15cc2694ee6367ae537ef8f1f +F test/incrblob3.test 67621a04b3084113bf38ce03797d70eca012d9d8f948193b8f655df577b0da6f +F test/incrblob4.test a8d6b5ff04055fcec747a50b78485ebf4fcd80074e0b3cedbe952bde346da54a +F test/incrblob_err.test 89372a28f1d98254f03fed705f9efcd34ef61a674df16d2dbb4726944a2de5e9 +F test/incrblobfault.test de274b1e329169c2c3438f9528994807ea8201ebf38ae9f157d34bf3ec0cc549 F test/incrcorrupt.test 6c567fbf870aa9e91866fe52ce6f200cd548939a -F test/incrvacuum.test d2a6ddf5e429720b5fe502766af747915ccf6c32 -F test/incrvacuum2.test 676c41428765d58f1da7dbe659ef27726d3d30ac -F test/incrvacuum3.test 75256fb1377e7c39ef2de62bfc42bbff67be295a +F test/incrvacuum.test 3fa6145f5e71f603554fd7b8ec3da4290b1341029682313285cb5f9e1893d6ba +F test/incrvacuum2.test 7d26cfda66c7e55898d196de54ac4ec7d86a4e3d +F test/incrvacuum3.test 0bf0ffe7f2cbc87ba1d471e4bbadabbf10dacf8d4ee26b3a072708d575d637a9 F test/incrvacuum_ioerr.test 6ae2f783424e47a0033304808fe27789cf93e635 -F test/index.test fe3c7a1aad82af92623747e9c3f3aa94ccd51238 +F test/index.test 9a060cbb12b70dd0e6bca0404106242703c0827ba58712f46b0d5a3b3968b341 F test/index2.test f835d5e13ca163bd78c4459ca15fd2e4ed487407 -F test/index3.test 81bc47890b8abfb181bc35f8d10b56c069803386 +F test/index3.test 51685f39345462b84fcf77eb8537af847fdf438cc96b05c45d6aaca4e473ade0 F test/index4.test ab92e736d5946840236cd61ac3191f91a7856bf6 F test/index5.test 8621491915800ec274609e42e02a97d67e9b13e7 -F test/index6.test 7102ec371414c42dfb1d5ca37eb4519aa9edc23a -F test/index7.test 9c6765a74fc3fcde7aebc5b3bd40d98df14a527c -F test/indexedby.test 9c4cd331224e57f79fbf411ae245e6272d415985 -F test/indexexpr1.test cb71b6586177b840e28110dd952178bb2bdfedc2 -F test/indexfault.test 31d4ab9a7d2f6e9616933eb079722362a883eb1d +F test/index6.test 656502465b30c3f8de4eb956928aaded02cf36128e8fc8be2b95390fb23e74f5 +F test/index7.test b238344318e0b4e42126717f6554f0e7dfd0b39cecad4b736039b43e1e3b6eb3 +F test/index8.test caa097735c91dbc23d8a402f5e63a2a03c83840ba3928733ed7f9a03f8a912a3 +F test/index9.test 2ac891806a4136ef3e91280477e23114e67575207dc331e6797fa0ed9379f997 +F test/indexA.test 11d84f6995e6e5b9d8315953fb1b6d29772ee7c7803ee9112715e7e4dd3e4974 +F test/indexedby.test 444fb04ce0b21a3daf79f84e6735b49e5a5b3396623b37df5431eb09c8b8f557 +F test/indexexpr1.test d32dba192b8a566a81bb437ee8b6219931458e010c2d94610da775fa9d318f17 +F test/indexexpr2.test 1c382e81ef996d8ae8b834a74f2a9013dddf59214c32201d7c8a656d739f999a +F test/indexexpr3.test 47b91bc7999805c9a34d356f672259bc49295ecc797448511cae554a309b47cd +F test/indexfault.test 98d78a8ff1f5335628b62f886a1cb7c7dac1ef6d48fa39c51ec871c87dce9811 F test/init.test 15c823093fdabbf7b531fe22cf037134d09587a7 -F test/insert.test 38742b5e9601c8f8d76e9b7555f7270288c2d371 +F test/insert.test 97cfb30b83ca1622b9422a1e4c4831b4cb767cf5d654660945036d1e72067e70 F test/insert2.test 4d14b8f1b810a41995f6286b64a6943215d52208 F test/insert3.test 1b7db95a03ad9c5013fdf7d6722b6cd66ee55e30 -F test/insert4.test a20432f1c0fbbcff8f11d0e6ab4acb8c9db58023 +F test/insert4.test 2bf81535a990c969665d66db51fcf76c23499b39893b5109f413d1de4ad34cd3 F test/insert5.test 394f96728d1258f406fe5f5aeb0aaf29487c39a6 -F test/instr.test 737bbf80685232033f3abedc6ae92f75860b5dd2 -F test/intarray.test 066b7d7ac38d25bf96f87f1b017bfc687551cdd4 -F test/interrupt.test dfe9a67a94b0b2d8f70545ba1a6cca10780d71cc -F test/intpkey.test 7506090fc08e028712a8bf47e5f54111947e3844 -F test/io.test 3a7abcef18727cc0f2399e04b0e8903eccae50f8 -F test/ioerr.test 2a24bd6ed5a8b062e64bfe1f6cf94fb25e92210d +F test/insertfault.test ac63d14ea3b49c573673a572f4014b9117383a03e497c58f308b5c776e4a7f74 +F test/instr.test 67ba309e9697c24a304e98a7c8f372456177dd4e32237d2a305e1e05f7bb79c2 +F test/instrfault.test 95e28efade652e6d51ae11b377088fe523a581a07ec428009e152a4dd0e0f44c +F test/intarray.test bb976b0b3df0ebb6a2eddfb61768280440e672beba5460ed49679ea984ccf440 +F test/interrupt.test ac1ef50ec9ab8e4f0e17c47629f82539d4b22558904e321ed5abea2e6187da7a +F test/interrupt2.test e4408ca770a6feafbadb0801e54a0dcd1a8d108d +F test/intpkey.test 7d54711acf553cdd641a40e9c6cfc2bf1a76070074940c1b126442517054320f +F test/intreal.test 68829a8bb073ee1610ca3f8f9e0f99b0371fb36e0fa64862dd5ced4ef03c2343 +F test/io.test d267fdc8915444a45e19841489033ebe70bb69f6db605b00df70be16b2a80f59 +F test/ioerr.test 78552a95d53b9674d85f63bdf3d76a8df70b4d5dba5a6a1b8a1d60f166cd2c6b F test/ioerr2.test 2593563599e2cc6b6b4fcf5878b177bdd5d8df26 F test/ioerr3.test d3cec5e1a11ad6d27527d0d38573fbff14c71bdd F test/ioerr4.test f130fe9e71008577b342b8874d52984bd04ede2c -F test/ioerr5.test 2edfa4fb0f896f733071303b42224df8bedd9da4 +F test/ioerr5.test 5984da7bf74b6540aa356f2ab0c6ae68a6d12039a3d798a9ac6a100abc17d520 F test/ioerr6.test a395a6ab144b26a9e3e21059a1ab6a7149cca65b -F test/join.test f9d4a28dec81c6e9dc21b73518e024d73b5ebf57 -F test/join2.test f2171c265e57ee298a27e57e7051d22962f9f324 +F test/istrue.test e7f285bb70282625c258e866ce6337d4c762922f5a300e1b50f958aef6e7d9c9 +F test/join.test 2fcfd84640cfd9ff48f31b4b0d370c4d5498c355ae4384544668ca54d37ae186 +F test/join2.test f59d63264fb24784ae9c3bc9d867eb569cd6d442da5660f8852effe5c1938c27 F test/join3.test 6f0c774ff1ba0489e6c88a3e77b9d3528fb4fda0 F test/join4.test 1a352e4e267114444c29266ce79e941af5885916 -F test/join5.test 8a5c0be6f0c260a5c7177c3b8f07c7856141038a -F test/join6.test cfe6503791ceb0cbb509966740286ec423cbf10b -F test/journal1.test 69abc726c51b4a0409189f9a85191205297c0577 -F test/journal2.test ae06f566c28552c313ded3fee79a6c69e6d049b1 -F test/journal3.test ff8af941f9e06161d3db1b46bb9f965ff0e7f307 -F test/jrnlmode.test 7864d59cf7f6e552b9b99ba0f38acd167edc10fa -F test/jrnlmode2.test 81610545a4e6ed239ea8fa661891893385e23a1d +F test/join5.test 76f99f69a410241de73fa5cd3f4a6ef469e64bb501a48de2437881792111c589 +F test/join6.test f809c025fa253f9e150c0e9afd4cef8813257bceeb6f46e04041228c9403cc2c +F test/join7.test 2268dcbb54b724391dda3748ea95c60d960607ffeed67885675998e7117697f6 +F test/join8.test d384d63985e3991c404afccadaf3efd1cdf9cd72680167f80e3cb80b95c18c68 +F test/join9.test 9056ddd3b0c0f4f9d658f4521038d9a37dc23ead8ca9a505d0b0db2b6a471e05 +F test/joinA.test 6ac4efdbb1eb9ca398162c5bc5623a757803b04bb4d76453c8563a0bdc2f73bd +F test/joinB.test 1b2ba3fc8568b49411787fccbf540570c148e9b6a53a30f80691cb6268098ded +F test/joinC.test 1f1a602c2127f55f136e2cbd3bf2d26546614bf8cffe5902ec1ac9c07f87f207 +F test/joinD.test 2ce62e7353a0702ca5e70008faf319c1d4686aa19fba34275c6d1da0e960be28 +F test/joinE.test d5d182f3812771e2c0d97c9dcf5dbe4c41c8e21c82560e59358731c4a3981d6b +F test/joinF.test 53dd66158806823ea680dd7543b5406af151b5aafa5cd06a7f3231cd94938127 +F test/joinH.test 1d2fc3190be68525fd9ce749b9468c40ba2930181e52fb5ee6f836051b38effb +F test/joinI.test fc7d24a2b1e444979b83bd92c30ebb975cebb5b9eae4442ce94969bd8d083053 +F test/journal1.test bc61a4228db11bffca118bd358ba4b868524bf080f3532749de6c539656e20fa +F test/journal2.test 9dac6b4ba0ca79c3b21446bbae993a462c2397c4 +F test/journal3.test e5aeff93a7776cf644dbc48dec277655cff80a1cd24689036abc87869b120ea6 +F test/jrnlmode.test 9b5bc01dac22223cb60ec2d5f97acf568d73820794386de5634dcadbea9e1946 +F test/jrnlmode2.test 8759a1d4657c064637f8b079592651530db738419e1d649c6df7048cd724363d F test/jrnlmode3.test 556b447a05be0e0963f4311e95ab1632b11c9eaa -F test/json101.test ef42283f0b60d8bacbc2243448e7c84988578e52 -F test/json102.test bf3fe7a706d30936a76a0f7a0375e1e8e73aff5a -F test/json103.test c5f6b85e69de05f6b3195f9f9d5ce9cd179099a0 +F test/json/README.md de59d5ba0bd2796d797115688630a6405bbf43a2891bad445ac6b9f38b83f236 +F test/json/json-generator.tcl dc0dd0f393800c98658fc4c47eaa6af29d4e17527380cd28656fb261bddc8a3f +F test/json/json-q1.txt 65f9d1cdcc4cffa9823fb73ed936aae5658700cd001fde448f68bfb91c807307 +F test/json/json-speed-check.sh 7d5898808ce7542762318306ae6075a30f5e7ee115c4a409f487e123afe91d88 x +F test/json/jsonb-q1.txt 1e180fe6491efab307e318b22879e3a736ac9a96539bbde7911a13ee5b33abc7 +F test/json101.test cf53254f0f0c1399a01b21fc58fee0e63a12a556be91b9ee9faccdb8b82c083c +F test/json102.test 9b2e5ada10845ff84853b3feaae2ce51ce7145ae458f74c6a6cecc6ef6ee3ae1 +F test/json103.test 355746a6b66aa438f214b4fae454b13068fad2444b5f693e0d538ad1c059b264 +F test/json104.test 1b844a70cddcfa2e4cd81a5db0657b2e61e7f00868310f24f56a9ba0114348c1 +F test/json105.test 043838b56e68f3252a0dcf5be1689016f6f3f05056f8dcfcdc9d074f4d932988 +F test/json106.test 4aed3afd16549045d198a8d9cea00deea96e1f2ecf55864dce96cac558b8abef +F test/json107.test 59054e815c8f6b67d634d44ace421cf975828fb5651c4460aa66015c8e19d562 +F test/json108.test 0a5f1e2d4b35a1bc33052563d2a5ede03052e2099e58cb424547656c898e0f49 +F test/json501.test b95e2d14988b682a5cadf079dd6162f0f85fb74cd59c6b1f1624110104a974eb +F test/json502.test 4ef68e4f272dfb083d4cbceb4e9e51d67ec1186a185e0c13637c50a4dc2f9796 +F test/jsonb01.test f4cdfb4cf5a0c940091b17675ed9583f45add0c938f07d65b0de0e19d3a9a101 F test/keyword1.test 37ef6bba5d2ed5b07ecdd6810571de2956599dff +F test/kvtest.c 6e0228409ea7ca0497dad503fbd109badb5e59545d131014b6aaac68b56f484a F test/lastinsert.test 42e948fd6442f07d60acbd15d33fb86473e0ef63 F test/laststmtchanges.test ae613f53819206b3222771828d024154d51db200 -F test/like.test 81632c437a947bf1f7130b19537da6a1a844806a -F test/like2.test 3b2ee13149ba4a8a60b59756f4e5d345573852da -F test/like3.test 3608a2042b6f922f900fbfd5d3ce4e7eca57f7c4 -F test/limit.test 0c99a27a87b14c646a9d583c7c89fd06c352663e -F test/loadext.test 648cb95f324d1775c54a55c12271b2d1156b633b +F test/lemon-test01.y 70110eff607ab137ccc851edb2bc7e14a6d4f246b5d2d25f82a60b69d87a9ff2 +F test/like.test 0036f8fe548fceabd1496361bfa262f35cf5ed17c794bd15e22e9f3de12e0eb0 +F test/like2.test d3be15fefee3e02fc88942a9b98f26c5339bbdef7783c90023c092c4955fe3d3 +F test/like3.test 3c9be7a0122908d8ead6aa25e47c2b9787eea88e8062926078ea7b3e95c71d99 +F test/limit.test 350f5d03c29e7dff9a2cde016f84f8d368d40bcd02fa2b2a52fa10c4bf3cbfaf +F test/limit2.test 621188fc3e5c3b8d2ef9827e05fa8313792ae563579073136efd25cb65325f1b +F test/literal.test a65dca9fef86e51b8e45544268e37abbd4bb94ba35fd65f6fdcab2f288cd8f79 +F test/literal2.tcl 1499037beaf661aeecdbe48801220a181d805372a64c6128d5f26bb6a4a8f0ce +F test/literal2.test b149e16b5fc9ee6249069a8858ed41052f222014fe0ba7ad43c2fb989c2dada2 +F test/loadext.test 878db71cf74b48250dbe9033bbfe6088ff869db3353ffd4febc68c0cd459959e F test/loadext2.test 0408380b57adca04004247179837a18e866a74f7 -F test/lock.test b984ab9034e7389be0d863fe4e64cbbc4d2028f5 +F test/lock.test 675b4367ec58b21009e46982d071c8259255e69072296b7756ea75fac5425d1a F test/lock2.test 5242d8ac4e2d59c403aebff606af449b455aceff F test/lock3.test f271375930711ae044080f4fe6d6eda930870d00 -F test/lock4.test e175ae13865bc87680607563bafba21f31a26f12 -F test/lock5.test c6c5e0ebcb21c61a572870cc86c0cb9f14cede38 +F test/lock4.test 27143363eda1622f03c133efc8db808fc331afd973486cb571ea71cd717d37b8 +F test/lock5.test 583cae05992af0f66607286917f7d5f8aed3b6053c52df5994efb98f2a8fdbaf F test/lock6.test ad5b387a3a8096afd3c68a55b9535056431b0cf5 F test/lock7.test 49f1eaff1cdc491cc5dee3669f3c671d9f172431 -F test/lock_common.tcl 7ffb45accf6ee91c736df9bafe0806a44358f035 -F test/lookaside.test 90052e87282de256d613fcf8c9cbb845e4001d2f -F test/main.test 16131264ea0c2b93b95201f0c92958e85f2ba11a +F test/lock_common.tcl f0a1f7b8f3fbb8629dc6231613a02841736f86ef72151429d5ffc12c7f613fb3 +F test/lookaside.test 5a828e7256f1ee4da8e1bdaa03373a3ccdb0f1ff98dfa82e9b76cb41a45b1083 +F test/main.test e8752d76233b1c8906cd2c98ad920dba868bd63c87d51d8a2ea5e9cba55dd496 F test/make-where7.tcl 05c16b5d4f5d6512881dfec560cb793915932ef9 -F test/malloc.test 21c213365f2cca95ab2d7dc078dc8525f96065f8 -F test/malloc3.test e3b32c724b5a124b57cb0ed177f675249ad0c66a +F test/malloc.test f7781c8151179fe4b7f743044a737ac2dfd87bf9cc18dd01398caf28d34e09c5 +F test/malloc3.test 6e88bae6312854a4adb4ecc2a6a5ea8c59b4db778b724ba718e1c43fc8c3c136 F test/malloc4.test 957337613002b7058a85116493a262f679f3a261 -F test/malloc5.test 02ed7c5313f0a68d95f2dfca8c8962132bd1f04b +F test/malloc5.test 2e4ad7684a13389a44a840499cd47173a8d05f22f082d7d083eece433a7a64eb F test/malloc6.test 2f039d9821927eacae43e1831f815e157659a151 F test/malloc7.test 7c68a32942858bc715284856c5507446bba88c3a F test/malloc8.test 9b7a3f8cb9cf0b12fff566e80a980b1767bd961d F test/malloc9.test 2307c6ee3703b0a21391f3ea92388b4b73f9105e -F test/mallocA.test 672cd7dedb63771bade3a6f557f851a4ad161d4a +F test/mallocA.test aea76f2dd8bcc2d19748f6b911e876cefda74a563753bf26af046e9d34bb97e6 F test/mallocAll.test 98f1be74bc9f49a858bc4f361fc58e26486798be F test/mallocB.test bc475ab850cda896142ab935bbfbc74c24e51ed6 F test/mallocC.test 3dffe16532f109293ce1ccecd0c31dca55ef08c4 @@ -874,276 +1399,375 @@ F test/mallocE.test db1ed69d7eded1b080952e2a7c37f364ad241b08 F test/mallocF.test 2d5c590ebc2fc7f0dcebdf5aa8498b9aed69107e F test/mallocG.test 0ff91b65c50bdaba680fb75d87fe4ad35bb7934f F test/mallocH.test 79b65aed612c9b3ed2dcdaa727c85895fd1bfbdb -F test/mallocI.test a88c2b9627c8506bf4703d8397420043a786cdb6 +F test/mallocI.test 6c23a71df077fa5d387be90e7e669c5b368ca38a F test/mallocJ.test b5d1839da331d96223e5f458856f8ffe1366f62e -F test/mallocK.test 27cb5566a6e5f2d76f9d4aa2eca45524401fd61e +F test/mallocK.test 25897506da0098cea09b302ff432b0fb6d8002773c1e0fc9732aa8b444bfd455 F test/mallocL.test fb311ff80afddf3b1a75e52289081f4754d901dc -F test/malloc_common.tcl aac62499b76be719fac31e7a3e54a7fd53272e7f +F test/mallocM.test 78bbe9d3da84a5c679123cdb40d7b2010b18fc46e13897e4f253c6ba6fbff134 +F test/malloc_common.tcl 806c50379cf4fa65008cd4d5af18273e5dac8ab62d1d4316c76aa2ecd2e54018 +F test/malloctraceviewer.tcl 3e3ddf11e30d2b20f53aa16aa6615082fb24a100bea61cca7214c927b742eba6 F test/manydb.test 28385ae2087967aa05c38624cec7d96ec74feb3e F test/mem5.test c6460fba403c5703141348cd90de1c294188c68f F test/memdb.test c1f2a343ad14398d5d6debda6ea33e80d0dafcc7 -F test/memleak.test 10b9c6c57e19fc68c32941495e9ba1c50123f6e2 -F test/memsubsys1.test d2b2d6ca37890b26703a2258df8fd66f9869da02 -F test/memsubsys2.test 3e4a8d0c05fd3e5fa92017c64666730a520c7e08 -F test/minmax.test 42fbad0e81afaa6e0de41c960329f2b2c3526efd -F test/minmax2.test b44bae787fc7b227597b01b0ca5575c7cb54d3bc +F test/memdb1.test c737ac9aa5895092332b1dde24fae7ae494b7fcbcd346d22d600891096a3836d +F test/memdb2.test 4ba1fc09e2f51df80d148a540e4a3fa66d0462e91167b27497084de4d1f6b5b4 +F test/memjournal.test 70f3a00c7f84ee2978ad14e831231caa1e7f23915a2c54b4f775a021d5740c6c +F test/memjournal2.test dbc2c5cb5f7b38950f4f6dc3e73fcecf0fcbed3fc32c7ce913bba164d288da1e +F test/memleak.test c7478f1195d64887dd1c677edc39fa03b5bf29024e6dcc5b5cc554d7ed00b01f +F test/memsubsys1.test 86b8158752af9188ed5b32a30674a1ef71183e6bc4e6808e815cd658ca9058a6 +F test/memsubsys2.test 774b93cb09ca50d1b759bb7c645baa2a9ce172edc3a3da67d5150a26a9fc2a08 +F test/merge1.test 7dd9dc6838bcd0623a069485fe3a8dd498a051c16e1877cf84f506c0d6a29b43 +F test/minmax.test 885d89737b955905b52bd5b203f27b9b56d1e399b8155c2a95ab0f3264e0f582 +F test/minmax2.test cf9311babb6f0518d04e42fd6a42c619531c4309a9dd790a2c4e9b3bc595e0de F test/minmax3.test cc1e8b010136db0d01a6f2a29ba5a9f321034354 -F test/minmax4.test 936941484ebdceb8adec7c86b6cd9b6e5e897c1f -F test/misc1.test 48ebfb5b22a6a058f7b7e1df211226dd1d21409c -F test/misc2.test 00d7de54eda90e237fc9a38b9e5ccc769ebf6d4d -F test/misc3.test cf3dda47d5dda3e53fc5804a100d3c82be736c9d -F test/misc4.test 0d8be3466adf123a7791a66ba2bc8e8d229e87f3 -F test/misc5.test f96428ea95b3820aafc6f1c50cf48a09e4597ee1 +F test/minmax4.test 272ca395257f05937dc96441c9dde4bc9fbf116a8d4fa02baeb0d13d50e36c87 +F test/misc1.test e3e36262aff1bd9b8b9bf1eeb3af04adb3fc1e23f0a92dbff708bba9e939ace1 +F test/misc2.test a1a3573cc02662becd967766021d6f16c54684d56df5f227481c7ef0d9df0bd0 +F test/misc3.test 651b88bca19b8ff6a7b6af73dae00c3fd5b3ea5bee0c0d1d91abd4c4b4748718 +F test/misc4.test 10cd6addb2fa9093df4751a1b92b50440175dd5468a6ec84d0386e78f087db0e +F test/misc5.test 02fcaf4d42405be02ec975e946270a50b0282dac98c78303ade0d1392839d2b8 F test/misc6.test 953cc693924d88e6117aeba16f46f0bf5abede91 -F test/misc7.test edd0b63e2ee29a256900b0514f6fff27e19e9bb2 -F test/misc8.test fc2754d38892f7dac30c22db3616c2764f117d66 -F test/misuse.test 3c34719944ba045cc6c188a4852ba04680728912 -F test/mmap1.test 44a5ff1c1bcc7dcf2de50227d1f997e75a8ef1ae -F test/mmap2.test 9d6dd9ddb4ad2379f29cc78f38ce1e63ed418022 -F test/mmap3.test c92273e16eb8d23c1d55c9815b446bb72ef0512e +F test/misc7.test d595599972ec0b436985f0f02f243b68500ffc977b9b3194ec66c0866cfddcab +F test/misc8.test 08d2380bc435486b12161521f225043ac2be26f02471c2c1ea4cac0b1548edbd +F test/misuse.test 859f37014d9824ca66bd90c36372c08c80c51c9593a7cfa8a31d4f92cd4d5b7f +F test/mjournal.test 28a08d5cb5fb5b5702a46e19176e45e964e0800d1f894677169e79f34030e152 +F test/mmap1.test 18de3fd7b70a777af6004ca2feecfcdd3d0be17fa04058e808baf530c94b1a1d +F test/mmap2.test dba452dc7db91e9df10f70bdd73dc4190c7b8ee7b5133b4684f04277ada0b9ac +F test/mmap3.test b3c297e78e6a8520aafcc1a8f140535594c9086e F test/mmap4.test 2e2b4e32555b58da15176e6fe750f17c9dcf7f93 +F test/mmapcorrupt.test 470fb44fe92e99c1d23701d156f8c17865f5b027063c9119dcfdb842791f4465 F test/mmapfault.test d4c9eff9cd8c2dc14bc43e71e042f175b0a26fe3 -F test/multiplex.test efd015ca0b5b4a57dc9535b8feb1273eebeadb60 +F test/mmapwarm.test 2272005969cd17a910077bd5082f70bc1fefad9a875afec7fc9af483898ecaf3 +F test/multiplex.test d74c034e52805f6de8cc5432cef8c9eb774bb64ec29b83a22effc8ca4dac1f08 F test/multiplex2.test 580ca5817c7edbe4cc68fa150609c9473393003a -F test/multiplex3.test d228f59eac91839a977eac19f21d053f03e4d101 +F test/multiplex3.test fac575e0b1b852025575a6a8357701d80933e98b5d2fe6d35ddaa68f92f6a1f7 F test/multiplex4.test e8ae4c4bd70606a5727743241f13b5701990abe4 -F test/mutex1.test e0a44072d98189003deae4b091106f085d94bea8 +F test/mutex1.test 42cb5e244c3a77bb0ef2b967e06fa5e7ba7d32d90a9b20bed98f6f5ede185a25 F test/mutex2.test bfeaeac2e73095b2ac32285d2756e3a65e681660 -F test/nan.test e9648b9d007c7045242af35e11a984d4b169443a -F test/nolock.test 0540dd96f39b8876e3ffdd8814fad0ea425efeee +F test/nan.test 73ea63ab43668313e2f8cc9ef9e9a966672c7934f3ce76926fbe991235d07d91 +F test/nockpt.test 3db354270fc63b6871eebd40285d4c55324fb27be629c958adbff6d7fcaa8e14 +F test/nolock.test f196cf8b8fbea4e2ca345140a2b3f3b0da45c76e +F test/normalize.test f23b6c5926c59548635fcf39678ac613e726121e073dd902a3062fbb83903b72 F test/notify1.test 669b2b743618efdc18ca4b02f45423d5d2304abf F test/notify2.test 2ecabaa1305083856b7c39cf32816b612740c161 -F test/notify3.test 10ff25cde502e72a92053a2f215d64bece4ef934 -F test/notnull.test f8fcf58669ddba79274daa2770d61dfad8274f62 -F test/null.test 0dcce4f04284ec66108c503327ad6d224c0752b3 +F test/notify3.test 796c7b7157f55c93b4e672b724e9c923a6fc6aa72ac419379a623e2350472e22 +F test/notnull.test a37b663d5bb728d66fc182016613fb8e4a0a4bbf3d75b8876a7527f7d4ed3f18 +F test/notnull2.test 5b7dd6e82c409b2d011ad6acf19ae4bf0816a9c69ccf600b529d7405d7c49874 +F test/notnullfault.test fc4bb7845582a2b3db376001ef49118393b1b11abe0d24adb03db057ee2b73d5 +F test/null.test b7ff206a1c60fe01aa2abd33ef9ea83c93727d993ca8a613de86e925c9f2bc6f +F test/nulls1.test 7a5e4346ee4285034100b4cd20e6784f16a9d6c927e44ecdf10034086bbee9c9 F test/numcast.test 5d126f7f581432e86a90d1e35cac625164aec4a1 F test/numindex1.test 20a5450d4b056e48cd5db30e659f13347a099823 -F test/offset1.test f06b83657bcf26f9ce805e67450e189e282143b2 +F test/offset1.test 72cca52482cbd5bc687cfa67aa2566c859081b5a353fd2f9da9bbd3914dea1ef F test/openv2.test 0d3040974bf402e19b7df4b783e447289d7ab394 -F test/orderby1.test 4d22a7c75f6a83fc1f188cc7bb5192285fdf2552 +F test/optfuzz-db01.c 9f2fa80b8f84ebbf1f2e8b13421a4e0477fe300f6686fbd76cac1d2db66e0fdc +F test/optfuzz-db01.txt 21f6bdeadc701cf11528276e2a55c70bfcb846ba42df327f979bd9e7b6ce7041 +F test/optfuzz.c 690430a0bf0ad047d5a168bf52b05b2ee97aedaad8c14337e9eb5050faa64994 +F test/orderby1.test 64d6aac62e68c289d378a18cea9b9941d812b6a26f141a18c9fbde79bd7f28db F test/orderby2.test bc11009f7cd99d96b1b11e57b199b00633eb5b04 -F test/orderby3.test 8619d06a3debdcd80a27c0fdea5c40b468854b99 -F test/orderby4.test 4d39bfbaaa3ae64d026ca2ff166353d2edca4ba4 -F test/orderby5.test 8f08a54836d21fb7c70245360751aedd1c2286fb +F test/orderby3.test 64465d29ab6dabb38731ff24ead2aa0bb262bb06a7b6aa17c6f874a5f24fee94 +F test/orderby4.test daff719d0eead9ae92863a0b5a2e367bfdc7efcc9dfe16d83d667f84df597eff +F test/orderby5.test bd7d9e3380e87e5dcf6ea817ebaab6d15da213c7804b38767e1b3e695e85650b F test/orderby6.test 8b38138ab0972588240b3fca0985d2e400432859 F test/orderby7.test 3d1383d52ade5b9eb3a173b3147fdd296f0202da F test/orderby8.test 23ef1a5d72bd3adcc2f65561c654295d1b8047bd F test/orderby9.test 87fb9548debcc2cd141c5299002dd94672fa76a3 -F test/oserror.test b32dc34f2363ef18532e3a0a7358e3e7e321974f +F test/orderbyA.test df608e59efc2ef50c1eddf1a773b272de3252e9401bfec86d04b52fd973866d5 +F test/orderbyB.test 32576c7b138105bc72f7fbf33bd320ca3a7d303641fc939e0e56af6cba884b3d +F test/oserror.test ee3fad06ec8671c4d047c2c92a567fc2e0e8161caaec7edd6d48325c5ac97f30 +F test/ossfuzz.c b5d232d9717fc999a121c82c4880ae5b9d7fb3ae55d2d87a8da906bc80020906 +F test/ossshell.c f125c5bd16e537a2549aa579b328dd1c59905e7ab1338dfc210e755bb7b69f17 F test/ovfl.test 199c482696defceacee8c8e0e0ef36da62726b2f -F test/pager1.test 1acbdb14c5952a72dd43129cabdbf69aaa3ed1fa -F test/pager2.test 67b8f40ae98112bcdba1f2b2d03ea83266418c71 -F test/pager3.test 3856d9c80839be0668efee1b74811b1b7f7fc95f -F test/pager4.test a122e9e6925d5b23b31e3dfef8c6a44bbf19590e -F test/pagerfault.test ae9ee0db5a30aecda9db8290ce3dd12e5f7bbaa1 +F test/pager1.test b083c2d5d89df8e979658d9320bfc0b9d50b4ef8ae1d9e115a692ff0b9768393 +F test/pager2.test c0ede15952b607f9a38f653acdfa73c19e657958e9104aab1a71950ea7b71831 +F test/pager3.test 4e9a83d6ca0838d7c602c9eb93d1357562d9059c1e02ffb138a8271020838370 +F test/pager4.test b995066c699472614eb5949db5a2e2c51fd463863518afe68675d7fac09216bd +F test/pagerfault.test 43692e660fe480812dc5d44171fdcb8da1a65a644428def1ee9de79edace4028 F test/pagerfault2.test caf4c7facb914fd3b03a17b31ae2b180c8d6ca1f F test/pagerfault3.test 1003fcda009bf48a8e22a516e193b6ef0dd1bbd8 -F test/pageropt.test 6b8f6a123a5572c195ad4ae40f2987007923bbd6 +F test/pageropt.test 84e4cc5cbca285357f7906e99b21be4f2bf5abc0 F test/pagesize.test 5769fc62d8c890a83a503f67d47508dfdc543305 -F test/parser1.test 222b5cbf3e2e659fec1bf7d723488c8b9c94f1d0 +F test/parser1.test 131f4733472252d53d8ed681115257866f55740ab697fa05900d766049348f27 F test/pcache.test c8acbedd3b6fd0f9a7ca887a83b11d24a007972b F test/pcache2.test af7f3deb1a819f77a6d0d81534e97d1cf62cd442 -F test/percentile.test 4243af26b8f3f4555abe166f723715a1f74c77ff -F test/permutations.test 382a43c49f49bafe6fddffe904ea33d6bb3ff33e -F test/pragma.test 507ac7ef2ea5682241ea0ef041799ca70bb5e0bf +F test/pendingrace.test e99efc5ab3584da3dfc8cd6a0ec4e5a42214820574f5ea24ee93f1d84655f463 +F test/percentile.test eaee1ff3e35d5fe933ac98d927c9a7d2f4f1a1a19ee22e7d45e97a6d9ee32077 +F test/permutations.test e6de4f5777f7785737ac3d1d964b8656e5477a134665b2fe8a91884ab9b685b3 +F test/pg_common.tcl 3b27542224db1e713ae387459b5d117c836a5f6e328846922993b6d2b7640d9f +F test/pragma.test 7d07b7bb76e273215d6a20c4f83c3062cc28976c737ccb70a686025801e86c8f F test/pragma2.test e5d5c176360c321344249354c0c16aec46214c9f -F test/pragma3.test 6f849ccffeee7e496d2f2b5e74152306c0b8757c -F test/printf.test b3ff34e73d59124140eaf89f7672e21bc2ca5fcc -F test/printf2.test 0b61566dd1c0f0b802f59dffa228c5dc5aa6b054 +F test/pragma3.test 92a46bbea12322dd94a404f49edcfbfc913a2c98115f0d030a7459bb4712ef31 +F test/pragma4.test 396ef9bff1fb966d41721545ad4b12bfc26aae315f5fe51d9b917828d49e6f8e +F test/pragma5.test 7b33fc43e2e41abf17f35fb73f71b49671a380ea92a6c94b6ce530a25f8d9102 +F test/pragma6.test c5ec577ba087954b4dfa619a3cbe97b155b60a0af487527abe89b10fc17e6512 +F test/pragmafault.test 275edaf3161771d37de60e5c2b412627ac94cef11739236bec12ed1258b240f8 +F test/prefixes.test b524a1c44bffec225b9aec98bd728480352aa8532ac4c15771fb85e8beef65d9 +F test/printf.test 685fec5a0c5af2490ab0632775a301554361d674211d690f5bee0a97b05333de +F test/printf2.test 3f55c1871a5a65507416076f6eb97e738d5210aeda7595a74ee895f2224cce60 F test/progress.test ebab27f670bd0d4eb9d20d49cef96e68141d92fb F test/ptrchng.test ef1aa72d6cf35a2bbd0869a649b744e9d84977fc +F test/pushdown.test 46a626ef1c0ca79b85296ff2e078b9da20a50e9b804b38f441590c3987580ddd F test/queryonly.test 5f653159e0f552f0552d43259890c1089391dcca F test/quick.test 1681febc928d686362d50057c642f77a02c62e57 +F test/quickcheck.test a4b7e878cd97e46108291c409b0bf8214f29e18fddd68a42bc5c1375ad1fb80a F test/quota-glob.test 32901e9eed6705d68ca3faee2a06b73b57cb3c26 -F test/quota.test 36cd78b178c4eb0401d4f25754ef410fbd9df2a7 +F test/quota.test 2747bfdf50d01155c06feb391286bb585f66977feaf5e26e5a37bc00ff7dee7c F test/quota2.test 7dc12e08b11cbc4c16c9ba2aa2e040ea8d8ab4b8 -F test/quote.test 215897dbe8de1a6f701265836d6601cc6ed103e6 +F test/quote.test 7b01b2a261bc26d9821aea9f4941ce1e08191d62fc55ba8862440fb3a59197a4 F test/randexpr1.tcl 40dec52119ed3a2b8b2a773bce24b63a3a746459 F test/randexpr1.test eda062a97e60f9c38ae8d806b03b0ddf23d796df F test/rbu.test 168573d353cd0fd10196b87b0caa322c144ef736 F test/rdonly.test 64e2696c322e3538df0b1ed624e21f9a23ed9ff8 -F test/regexp1.test 497ea812f264d12b6198d6e50a76be4a1973a9d8 -F test/reindex.test 44edd3966b474468b823d481eafef0c305022254 -F test/releasetest.tcl 975449bf742b8bb9025208292208af816a1fcb58 +F test/readonly.test 0d307c335b3421898cfe64a783a376138aa003849b6bff61ee2d21e805bc0051 +F test/recover.test c76d05f33f0271fba0f0752170e03b0ab5952dc61dcea7ab3ba40df03c4c42de +F test/regexp1.test 8f2a8bc1569666e29a4cee6c1a666cd224eb6d50e2470d1dc1df995170f3e0f1 +F test/regexp2.test 64f9726b2ddc71aea06725fcad53231833d038d58b936d49083ace658b370a13 +F test/reindex.test cd9d6021729910ece82267b4f5e1b5ac2911a7566c43b43c176a6a4732e2118d +F test/reservebytes.test 6163640b5a5120c0dee6591481e673a0fa0bf0d12d4da7513bad692c1a49a162 +F test/resetdb.test 54c06f18bc832ac6d6319e5ab23d5c8dd49fdbeec7c696d791682a8006bd5fc3 F test/resolver01.test f4022acafda7f4d40eca94dbf16bc5fc4ac30ceb -F test/rollback.test 458fe73eb3ffdfdf9f6ba3e9b7350a6220414dea -F test/rollback2.test 8435d6ff0f13f51d2a4181c232e706005fa90fc5 +F test/returning1.test cd32517148948859db214dd814354597dd40e7489259590fac1a4f7bf44deb97 +F test/returningfault.test 5f9649d05680357ab077fa6bad574a3eb3f6e4858a6880b25171be516a4efcda +F test/rollback.test 952c4d805bca96adc2be76f621ea22115fe40b330015af36fcc8028c8547fcee +F test/rollback2.test 3f3a4e20401825017df7e7671e9f31b6de5fae5620c2b9b49917f52f8c160a8f F test/rollbackfault.test 0e646aeab8840c399cfbfa43daab46fd609cf04a +F test/round1.test 29c3c9039936ed024d672f003c4d35ee11c14c0acb75c5f7d6188ff16190cfd4 F test/rowallock.test 3f88ec6819489d0b2341c7a7528ae17c053ab7cc F test/rowhash.test 0bc1d31415e4575d10cacf31e1a66b5cc0f8be81 -F test/rowid.test 5b7509f384f4f6fae1af3c8c104c8ca299fea18d +F test/rowid.test d27191b5ce794c05bf61081e8b2c546a1844c1641321dcaf7fb785234256cc8e +F test/rowvalue.test 93474d8e1c496e970bdcc3a7f54ac835adda667d2fd971957b4bce0c0b11707b +F test/rowvalue2.test 060d238b7e5639a7c5630cb5e63e311b44efef2b +F test/rowvalue3.test 103e9a224ca0548dd0d67e439f39c5dd16de4200221a333927372408c025324c +F test/rowvalue4.test bac9326d1e886656650f67c0ec484eb5f452244a8209c6af508e9a862ace08ed +F test/rowvalue5.test 00740304ea6a53a8704640c7405690f0045d5d2a6b4b04dde7bccc14c3068ea7 +F test/rowvalue6.test d19b54feb604d5601f8614b15e214e0774c01087 +F test/rowvalue7.test 06ec0aca725bf683313d03793aa2943bc7f45a901848c7056a9665b769c8fc38 +F test/rowvalue8.test 5900eddad9e2c3c2e26f1a95f74aafc1232ee5e0 +F test/rowvalue9.test 7499a8fd7ca3a3f0e19d94e135355439aa2b596f86b775ca8de79672da2ca378 +F test/rowvalueA.test be8d6ad8b476eb24c151bb20bfd487e0d50c5e99618b7b0e656035069d2fc2cf +F test/rowvaluefault.test 963ae9cdaed30a85a29668dd514e639f3556cae903ee9f172ea972d511c54fff +F test/rowvaluevtab.test cd9747bb3f308086944c07968f547ad6b05022e698d80b9ffbdfe09ce0b8da6f F test/rtree.test 0c8d9dd458d6824e59683c19ab2ffa9ef946f798 F test/run-wordcount.sh 891e89c4c2d16e629cd45951d4ed899ad12afc09 -F test/savepoint.test c671fdbd34cd3bfe1518a777526ada595180cf8d +F test/savepoint.test 6e9804a17767f08432c7a5e738b9a8f4b891d243110b63d3a41d270d3d1378ec F test/savepoint2.test 9b8543940572a2f01a18298c3135ad0c9f4f67d7 F test/savepoint4.test c8f8159ade6d2acd9128be61e1230f1c1edc6cc0 F test/savepoint5.test 0735db177e0ebbaedc39812c8d065075d563c4fd -F test/savepoint6.test f41279c5e137139fa5c21485773332c7adb98cd7 -F test/savepoint7.test db3db281486c925095f305aad09fe806e5188ff3 +F test/savepoint6.test 48a645a7bb3a59a6fcf06a7364cfe5b655c336760de39068f7c241b0fc80d963 +F test/savepoint7.test 24c69af86d750c80d51cf6500fde9270717f2b6e5658f055b5e75af75d5af179 F test/savepointfault.test f044eac64b59f09746c7020ee261734de82bf9b2 -F test/scanstatus.test 5253c219e331318a437f436268e0e82345700285 -F test/schema.test 8f7999be894260f151adf15c2c7540f1c6d6a481 +F test/scanstatus.test b249328caf4d317e71058006872b8012598a5fa045b30bf24a81eeff650ab49e +F test/scanstatus2.test d85d17f2b0b4c013dde95232f7beab749f11f0ef847f5ecffb9486d2f5ecf9f9 +F test/schema.test 5dd11c96ba64744de955315d2e4f8992e447533690153b93377dffb2a5ef5431 F test/schema2.test 906408621ea881fdb496d878b1822572a34e32c5 -F test/schema3.test 1bc1008e1f8cb5654b248c55f27249366eb7ed38 -F test/schema4.test e6a66e20cc69f0e306667c08be7fda3d11707dc5 +F test/schema3.test 8ed4ae66e082cdd8b1b1f22d8549e1e7a0db4527a8e6ee8b6193053ee1e5c9ce +F test/schema4.test 3b26c9fa916abb6dadf894137adcf41b7796f7b9 F test/schema5.test 29699b4421f183c8f0e88bd28ce7d75d13ea653e -F test/securedel.test 21749c32ccc30f1ea9e4b9f33295a6521ec20fa0 +F test/schema6.test e4bd1f23d368695eb9e7b51ef6e02ca0642ea2ab4a52579959826b5e7dce1f9b +F test/schemafault.test 1936bceca55ac82c5efbcc9fc91a1933e45c8d1e1d106b9a7e56c972a5a2a51e +F test/securedel.test 2f70b2449186a1921bd01ec9da407fbfa98c3a7a5521854c300c194b2ff09384 F test/securedel2.test 2d54c28e46eb1fd6902089958b20b1b056c6f1c5 -F test/select1.test be62204d2bd9a5a8a149e9974cfddce893d8f686 +F test/seekscan1.test 31af16e3bb3203d153aea320939c5da97ec44705c2710d153c06a01397d45b09 +F test/select1.test 692e84cfa29c405854c69e8a4027183d64c22952866a123fabbce741a379e889 F test/select2.test 352480e0e9c66eda9c3044e412abdf5be0215b56 -F test/select3.test 2ce595f8fb8e2ac10071d3b4e424cadd4634a054 -F test/select4.test 453631158540e5f685b81cac5b7e8bd8c6b4c5fc -F test/select5.test e758b8ef94f69b111df4cb819008856655dcd535 -F test/select6.test 39eac4a5c03650b2b473c532882273283ee8b7a0 -F test/select7.test 95e370c42d47c3c52377d05e9ffc01ccff7c1f61 +F test/select3.test 152ce3978c8600c70413e921e39e4c5aeb8ff1e96dd722442a9981e1b8afc702 +F test/select4.test 21941409ac6b65bfca83de020afb5d976d802d3d8ad216dc774a3fbbf55fe277 +F test/select5.test 8afc5e5dcdebc2be54472e73ebd9cd1adef1225fd15d37a1c62f969159f390ae +F test/select6.test da91e61d26b8dea4b61e4a862088dd6ab19998f7be22a16a5b0cfe806e597639 +F test/select7.test b825420da8a0b5722fdb77f3369f6396a3d198c46e8787eb26ff9425d4ac9d27 F test/select8.test 8c8f5ae43894c891efc5755ed905467d1d67ad5d -F test/select9.test aebc2bb0c3bc44606125033cbcaac2c8d1f33a95 -F test/selectA.test e452bdb975f488ea46d091382a9185b5853ed2c7 +F test/select9.test f7586b207ce2304ab80dc93d3146469a28fd4403621dd3a82d06644563d3c812 +F test/selectA.test 1da8ce3884c326e11d2855baffb76436b0d7e044404af8a2a70d1399a4ff7e29 F test/selectB.test 954e4e49cf1f896d61794e440669e03a27ceea25 -F test/selectC.test 871fb55d884d3de5943c4057ebd22c2459e71977 -F test/selectD.test b0f02a04ef7737decb24e08be2c39b9664b43394 +F test/selectC.test 38c530b0cc5728b793c3c11f52b52c70290d39822224acd39011c89c1853bd31 +F test/selectD.test 6d1909b49970bf92f45ce657505befcef5fc7cbc13544e18103a316d32189bfb F test/selectE.test a8730ca330fcf40ace158f134f4fe0eb00c7edbf F test/selectF.test 21c94e6438f76537b72532fa9fd4710cdd455fc3 -F test/selectG.test e8600e379589e85e9fefd2fe4d44a4cdd63f6982 -F test/server1.test 46803bd3fe8b99b30dbc5ff38ffc756f5c13a118 -F test/shared.test 1da9dbad400cee0d93f252ccf76e1ae007a63746 +F test/selectG.test 089f7d3d7e6db91566f00b036cb353107a2cca6220eb1cb264085a836dae8840 +F test/selectH.test 0b54599f1917d99568c9b929df22ec6261ed7b6d2f02a46b5945ef81b7871aac +F test/session.test 78fa2365e93d3663a6e933f86e7afc395adf18be +F test/sessionfuzz-data1.db 1f8d5def831f19b1c74571037f0d53a588ea49a6c4ca2a028fc0c27ef896dbcb +F test/sessionfuzz.c f693b8827034a3bed7616d89c65fb4fe8b7ff3c0f000c6ea6beda69b7f1aced3 +F test/shared.test 50bd8091735b272732125928c363476a17b5fb264835de7d19e90c72055c888b F test/shared2.test 03eb4a8d372e290107d34b6ce1809919a698e879 -F test/shared3.test ab693f9b6e156b8bfb2a0ad94f29fe69602a5d38 +F test/shared3.test cb92d083003ddf0f313166e494ec2fcafa55fdebf648628923ded3169dba8850 F test/shared4.test c75f476804e76e26bf6fa0e7b421fb0ca7d07558 -F test/shared6.test 866bb4982c45ce216c61ded5e8fde4e7e2f3ffa9 +F test/shared6.test 104e1e25b4c4f47aaccca7dba75b3d87bb505b46b009af03ae49bf55b7c4976c F test/shared7.test a81e99f83e6c51b02ac99c96fb3a2a7b5978c956 -F test/shared8.test 00a07bf5e1337ecf72e94542bdefdc330d7a2538 -F test/shared9.test 5f2a8f79b4d6c7d107a01ffa1ed05ae7e6333e21 -F test/sharedA.test 0cdf1a76dfa00e6beee66af5b534b1e8df2720f5 -F test/sharedB.test 16cc7178e20965d75278f410943109b77b2e645e -F test/shared_err.test 2f2aee20db294b9924e81f6ccbe60f19e21e8506 +F test/shared8.test 933ed7d71f598bb6c7a8c192a3cd30f2562fdccf514df383798599c34ffa672f +F test/shared9.test 600a257fe9d8b0272746b230e761aa1bd8802ca4cf3ba5b2136b9204f3d51efa +F test/sharedA.test 64bdd21216dda2c6a3bd3475348ccdc108160f34682c97f2f51c19fc0e212700 +F test/sharedB.test 1a84863d7a2204e0d42f2e1606577c5e92e4473fa37ea0f5bdf829e4bf8ee707 +F test/shared_err.test 32634e404a3317eeb94abc7a099c556a346fdb8fb3858dbe222a4cbb8926a939 F test/sharedlock.test 5ede3c37439067c43b0198f580fd374ebf15d304 -F test/shell1.test ce5e744870387164703bf2dee2cc9753e4a71513 -F test/shell2.test 12b8bf901b0e3a8ac58cf5c0c63a0a388d4d1862 -F test/shell3.test 5e8545ec72c4413a0e8d4c6be56496e3c257ca29 -F test/shell4.test ddf0a99044e2245a87fc17423e3aaa1445b3243b -F test/shell5.test c04e9f9f948305706b88377c464c7f08ce7479f9 +F test/shell1.test ebe953d64c937ad42a0f33170ac0d2d2568faae26813fc7a95203756446d54aa +F test/shell2.test ab23f01ea2347e4b72bb2399af7ee82aa00f9c059141749f7c4064abca5ad728 +F test/shell3.test 603b448e917537cf77be0f265c05c6f63bc677c63a533c8e96aae923b56f4a0e +F test/shell4.test 03593fa7908a55f255916ffeda707cdf55680c777736e3da62b1d78cde0d684d +F test/shell5.test d17e7927ab8b7f720efbdd9b5d05fceb6c3c56c25917901b315400214bf24ef4 +F test/shell6.test e3b883b61d4916b6906678a35f9d19054861123ad91b856461e0a456273bdbb8 +F test/shell7.test 43fd8e511c533bab5232e95c7b4be93b243451709e89582600d4b6e67693d5c3 +F test/shell8.test 641cf21a99c59404c24e3062923734951c4099a6b6b6520de00cf7a1249ee871 +F test/shell9.test 8742a5b390cdcef6369f5aa223e415aa4255a4129ef249b177887dc635a87209 +F test/shellA.test 4ecff8b7b2c0122ba8174abfbcc4b0f59e44d80f2a911068f8cd4cfc6661032d +F test/shmlock.test 9f1f729a7fe2c46c88b156af819ac9b72c0714ac6f7246638a73c5752b5fd13c F test/shortread1.test bb591ef20f0fd9ed26d0d12e80eee6d7ac8897a3 F test/show_speedtest1_rtree.tcl 32e6c5f073d7426148a6936a0408f4b5b169aba5 -F test/shrink.test 1b4330b1fd9e818c04726d45cb28db73087535ce +F test/shrink.test 2668e607dcdfa19c52828c09b69685b38da793856582ae31debf79d90c7bbbdc F test/sidedelete.test f0ad71abe6233e3b153100f3b8d679b19a488329 -F test/skipscan1.test d37a75b4be4eb9dedeb69b4f38b1d0a74b5021d7 -F test/skipscan2.test d1d1450952b7275f0b0a3a981f0230532743951a +F test/skipscan1.test 9cbbb6575517b15292bd87ee85b853bbd3cd4b4735d69b0f083020cec16ff304 +F test/skipscan2.test b032ed3e0ba5caa4df6c43ef22c31566aac67783bc031869155989a7ccdb5bd5 F test/skipscan3.test ec5bab3f81c7038b43450e7b3062e04a198bdbb5 -F test/skipscan5.test 67817a4b6857c47e0e33ba3e506da6f23ef68de2 -F test/skipscan6.test 5866039d03a56f5bd0b3d172a012074a1d90a15b -F test/snapshot.test 5ec4651d16c3d1eb6c010d102febe32b3614bf56 -F test/snapshot_fault.test 25973aeb1b86a280800e0bcf1eb5ce70e9ef57ab -F test/soak.test 0b5b6375c9f4110c828070b826b3b4b0bb65cd5f +F test/skipscan5.test 0672103fd2c8f96bd114133f356192b35ece45c794fe3677e1d9e5e3104a608e +F test/skipscan6.test e2b256cf5d538a605beb97dc97ca5e2836dfc24c5e1d9b7a09e13c069a3b8b49 +F test/snapshot.test a504f2e7009f512ef66c719f0ea1c55a556bdaf1e1312c80a04d46fc1a3e9632 +F test/snapshot2.test 8d6ff5dd9cc503f6e12d408a30409c3f9c653507b24408d9cd7195931c89bc54 +F test/snapshot3.test 2e0328ba019aa981848e10aded4d7dcd6094ec1f9c6290a34ab18415be0c44eb +F test/snapshot4.test d4e9347ef2fcabc491fc893506c7bbaf334da3be111d6eb4f3a97cc623b78322 +F test/snapshot_fault.test 129234ceb9b26a0e1000e8563a16e790f5c1412354e70749cbd78c3d5d07d60a +F test/snapshot_up.test 77dc7853bfb2b4fa249f76e1714cfa1e596826165d9ef22c06ac3a0b7b778d9a +F test/soak.test 18944cf21b94a7fe0df02016a6ee1e9632bc4e8d095a0cb49d95e15d5cca2d5c F test/softheap1.test 843cd84db9891b2d01b9ab64cef3e9020f98d087 -F test/sort.test c2adc635c2564241fefec0b3a68391ef6868fd3b -F test/sort2.test cc23b7c19d684657559e8a55b02f7fcee03851d0 +F test/sort.test f86751134159abb5e5fd4381a0d7038c91013638cd1e3fa1d7850901f6df6196 +F test/sort2.test 2f8c66402a03adebe77ce7aafca129fbf32df27d6c9b8f7a9f1b958e39f56da8 F test/sort3.test 1480ed7c4c157682542224e05e3b75faf4a149e5 -F test/sort4.test 5c34d9623a4ae5921d956dfa2b70e77ed0fc6e5c -F test/sort5.test d3041ce3c475aa04142a959ae56ef6593f98a99f +F test/sort4.test c7a88629aecc8eec3c919eda54b221da5cf7a1b48f0cd372e7e832188d6737d8 +F test/sort5.test 6b43ae0e2169b5ceed441844492e55ba7f1ae0790528395ddf7888ab3094525d +F test/sorterref.test 9a606c86a4c682db5eeaaefa0565b52102778db53e48ca7101cd4f9ebcc0ad94 F test/sortfault.test d4ccf606a0c77498e2beb542764fd9394acb4d66 -F test/speed1.test f2974a91d79f58507ada01864c0e323093065452 +F test/speed1.test 0381cfd05e5e7ccfd5eb570976f9075c67ab3e34991a1addf80663b184395219 F test/speed1p.explain d841e650a04728b39e6740296b852dccdca9b2cb -F test/speed1p.test b180e98609c7677382cf618c0ec9b69f789033a8 +F test/speed1p.test 7191cec2aaf8876317bec58cf9c0f3750ab8b9bc23fc8a4000b77da578c7aadc F test/speed2.test 53177056baf6556dcbdcf032bbdfc41c1aa74ded -F test/speed3.test d32043614c08c53eafdc80f33191d5bd9b920523 +F test/speed3.test 694affeb9100526007436334cf7d08f3d74b85ef F test/speed4.test abc0ad3399dcf9703abed2fff8705e4f8e416715 F test/speed4p.explain 6b5f104ebeb34a038b2f714150f51d01143e59aa -F test/speed4p.test 0e51908951677de5a969b723e03a27a1c45db38b -F test/speedtest1.c f8bf04214e7b5f745feea99f7bde68b1c4870666 -F test/spellfix.test f9c1f431e2c096c8775fec032952320c0e4700db +F test/speed4p.test 377a0c48e5a92e0b11c1c5ebb1bc9d83a7312c922bc0cb05970ef5d6a96d1f0c +F test/speedtest.md ee958457ae1b729d9715ae33c0320600000bf1d9ddea1a88dcf79f56729d6fad +F test/speedtest.tcl 6b66974d833d35a63d0e9ec344e0ffa92fbbfac83e173556f700a61cb3be96fc x +F test/speedtest1.c 6c01252e66f46de0b6b8d5316e03521e2151782104f3608c10262aa5dce85721 +F test/spellfix.test 951a6405d49d1a23d6b78027d3877b4a33eeb8221dcab5704b499755bb4f552e F test/spellfix2.test dfc8f519a3fc204cb2dfa8b4f29821ae90f6f8c3 -F test/spellfix3.test f7bf7b3482971473d32b6b00f6944c5c066cff97 -F test/sqldiff1.test 8f6bc7c6a5b3585d350d779c6078869ba402f8f5 -F test/sqllimits1.test a74ee2a3740b9f9c2437c246d8fb77354862a142 +F test/spellfix3.test 0f9efaaa502a0e0a09848028518a6fb096c8ad33 +F test/spellfix4.test 51c7c26514ade169855c66bcf130bd5acfb4d7fd090cc624645ab275ae6a41fb +F test/sqldiff1.test 1b7ab4f312442c5cc6b3a5f299fa8ca051416d1dd173cb1126fd51bf64f2c3fb +F test/sqllimits1.test 408131e4975d61868711c83f101a56d4602313cc5cae88d3eee81c1da364fd89 F test/sqllog.test 6af6cb0b09f4e44e1917e06ce85be7670302517a -F test/stat.test acc91e80517fff447ae8adcfd953cfdaa5efc0af -F test/statfault.test f525a7bf633e50afd027700e9a486090684b1ac1 -F test/stmt.test 25d64e3dbf9a3ce89558667d7f39d966fe2a71b9 -F test/subquery.test d7268d193dd33d5505df965399d3a594e76ae13f -F test/subquery2.test 438f8a7da1457277b22e4176510f7659b286995f -F test/subselect.test d24fd8757daf97dafd2e889c73ea4c4272dcf4e4 -F test/substr.test 18f57c4ca8a598805c4d64e304c418734d843c1a -F test/subtype1.test 7fe09496352f97053af1437150751be2d0a0cae8 -F test/superlock.test 1cde669f68d2dd37d6c9bd35eee1d95491ae3fc2 -F test/symlink.test c9ebe7330d228249e447038276bfc8a7b22f4849 -F test/sync.test 2f607e1821aa3af3c5c53b58835c05e511c95899 -F test/syscall.test f59ba4e25f7ba4a4c031026cc2ef8b6e4b4c639c +F test/starschema1.test f5388cd32527ab18d3f98f9e3402ec780f6a186e04e0d9c8531d7568ee734e11 +F test/startup.c 1beb5ca66fcc0fce95c3444db9d1674f90fc605499a574ae2434dcfc10d22805 +F test/stat.test 123212a20ceb496893d5254a5f6c76442ce549fdc08d1702d8288a2bbaac8408 +F test/statfault.test 064f43379e4992b5221b7d9ac887c313b3191f85cce605d78e416fc4045da64e +F test/stmt.test 54ed2cc0764bf3e48a058331813c3dbd19fc1d0827c3d8369914a5d8f564ec75 +F test/stmtrand.test 340e2ea4841c5cdc02d36e33739769c5d907ab529b12bb535407def0e413ca17 +F test/stmtvtab1.test 6873dfb24f8e79cbb5b799b95c2e4349060eb7a3b811982749a84b359468e2d5 +F test/strict1.test a7f9091603fe71cdc62baab0766684cba12a97ec69bfbb70be965532669cd77a +F test/strict2.test b22c7a98b5000aef937f1990776497f0e979b1a23bc4f63e2d53b00e59b20070 +F test/subjournal.test 8d4e2572c0ee9a15549f0d8e40863161295107e52f07a3e8012a2e1fdd093c49 +F test/subquery.test 23087f9b1c15ab9cc5231d04946bdebc51db527c95eb9d7434a2222127e17a84 +F test/subquery2.test ab96ff3fa9c4e3dce0d699f74e61c50250ed4335bc8f400e127707d552a8999e +F test/subselect.test 0966aa8e720224dbd6a5e769a3ec2a723e332303 +F test/substr.test a673e3763e247e9b5e497a6cacbaf3da2bd8ec8921c0677145c109f2e633f36b +F test/subtype1.test 96fd2a59bfc845c955b5f339d23b37ef4d50de5f8a04acd1450a68605fa2e3e7 +F test/superlock.test 85256830339a6871ce36a2ef591c3f67716a701b5497788fb2068b90159c2442 +F test/swarmvtab.test 250231404fcac88f61a6c147bb0e3a118ed879278cd3ccb0ae2d3a729e1e8e26 +F test/swarmvtab2.test c948cb2fdfc5b01d85e8f6d6504854202dc1a0782ab2a0ed61538f27cbd0aa5c +F test/swarmvtab3.test 41a3ab47cb7a834d4e5336425103b617410a67bb95d335ef536f887587ece073 +F test/swarmvtabfault.test 8a67a9f27c61073a47990829e92bc0c64420a807cb642b15a25f6c788210ed95 +F test/symlink.test 60e16915cd0ee068244563f354ae012149cf7541e922025e31ac613e3fa3e389 +F test/symlink2.test 3cf7f09dcf3cf74541f5fdb1ede3c731e4e35d2018d85efc61e32ac114435ce3 +F test/sync.test a619e407ede58a7b6e3e44375328628559fc9695a9c24c47cb5690a866b0031b +F test/sync2.test 06152269ed73128782c450c355988fe8dd794d305833af75e1a5e79edd4dae47 +F test/syscall.test a067468b43b8cb2305e9f9fe414e5f40c875bb5d2cba5f00b8154396e95fcf37 F test/sysfault.test c9f2b0d8d677558f74de750c75e12a5454719d04 -F test/tabfunc01.test cc33684f9480fcf1fd5ce287ac28d22971cad1cc -F test/table.test b708f3e5fa2542fa51dfab21fc07b36ea445cb2f -F test/tableapi.test 2674633fa95d80da917571ebdd759a14d9819126 +F test/tabfunc01.test 56eeae736217204bb1d9f9ef38340d48058f809b64249217cf77ff4ba600cc21 +F test/table.test e87294bf1c80bfd7792142b84ab32ea5beb4f3f71e535d7fb263a6b2068377bf +F test/tableapi.test e37c33e6be2276e3a96bb54b00eea7f321277115d10e5b30fdb52a112b432750 F test/tableopts.test dba698ba97251017b7c80d738c198d39ab747930 -F test/tclsqlite.test 7fb866443c7deceed22b63948ccd6f76b52ad054 -F test/tempdb.test 19d0f66e2e3eeffd68661a11c83ba5e6ace9128c +F test/tclsqlite.test 3f697424cfc1cdc9c076ec0cadb0e700f059400a3e3ce134b7d856fc9f880e1c +F test/tempdb.test 4cdaa23ddd8acb4d79cbb1b68ccdfd09b0537aaba909ca69a876157c2a2cbd08 +F test/tempdb2.test 353864e96fd3ae2f70773d0ffbf8b1fe48589b02c2ec05013b540879410c3440 +F test/tempfault.test 0c0d349c9a99bf5f374655742577f8712c647900 F test/temptable.test d2c9b87a54147161bcd1822e30c1d1cd891e5b30 -F test/temptrigger.test 8ec228b0db5d7ebc4ee9b458fc28cb9e7873f5e1 -F test/tester.tcl 462376b478c1429030911b4cb7c8c517ef1fbd9b -F test/thread001.test 9f22fd3525a307ff42a326b6bc7b0465be1745a5 -F test/thread002.test e630504f8a06c00bf8bbe68528774dd96aeb2e58 +F test/temptable2.test 76821347810ecc88203e6ef0dd6897b6036ac788e9dd3e6b04fd4d1631311a16 +F test/temptable3.test d11a0974e52b347e45ee54ef1923c91ed91e4637 +F test/temptrigger.test 38f0ca479b1822d3117069e014daabcaacefffcc +F test/tester.tcl 463ae33b8bf75ac77451df19bd65e7c415c2e9891227c7c9e657d0a2d8e1074a +F test/testloadext.c 862b848783eaed9985fbce46c65cd214664376b549fae252b364d5d1ef350a27 +F test/testrunner.tcl 60d7efa1816c5dfc37df3e3454b94b9042c0c8c50b27ae296d4a797cd309ace6 x +F test/testrunner_data.tcl c507a9afa911c03446ed90442ffd4a98aca02882c3d51bd1177c24795674def8 +F test/testrunner_estwork.tcl 7927a84327259a32854926f68a75292e33a61e7e052fdbfcb01f18696c99c724 +F test/thread001.test a0985c117eab62c0c65526e9fa5d1360dd1cac5b03bde223902763274ce21899 +F test/thread002.test c24c83408e35ba5a952a3638b7ac03ccdf1ce4409289c54a050ac4c5f1de7502 F test/thread003.test ee4c9efc3b86a6a2767516a37bd64251272560a7 F test/thread004.test f51dfc3936184aaf73ee85f315224baad272a87f F test/thread005.test 50d10b5684399676174bd96c94ad4250b1a2c8b6 F test/thread1.test df115faa10a4ba1d456e9d4d9ec165016903eae4 F test/thread2.test f35d2106452b77523b3a2b7d1dcde2e5ee8f9e46 -F test/thread_common.tcl 334639cadcb9f912bf82aa73f49efd5282e6cadd +F test/thread3.test a12656a56cdf67acb6a2ff7638826c6d6a645f79909d86df521045ad31cf547d +F test/thread_common.tcl b3b19a769fe30ef5537cdfa60acd49b78f771301627720d1add2d3bac77d9039 F test/threadtest1.c 6029d9c5567db28e6dc908a0c63099c3ba6c383b F test/threadtest2.c a70a8e94bef23339d34226eb9521015ef99f4df8 -F test/threadtest3.c 38a612ea62854349ed66372f330a40d73c5cf956 +F test/threadtest3.c 655bff6c0895ec03f014126aa65e808fac9aae8c5a7a7da58a510cbe8b43b781 F test/threadtest4.c c1e67136ceb6c7ec8184e56ac61db28f96bd2925 +F test/threadtest5.c 9b4d782c58d8915d7e955ff8051f3d03628bda0d33b82971ea8c0f2f2808c421 +F test/time-wordcount.sh 8e0b0f8109367827ad5d58f5cc849705731e4b90 +F test/timediff1.test d982b2b5f1b22f58380c5db94ea5b17518d50ad0c55583cf0ecfa0b176e20888 F test/tkt-02a8e81d44.test 6c80d9c7514e2a42d4918bf87bf6bc54f379110c -F test/tkt-26ff0c2d1e.test 888324e751512972c6e0d1a09df740d8f5aaf660 +F test/tkt-18458b1a.test 6a62cb1ee50fa3c620da59e3a6f531eb38fceaf7e2166203816b724524e6f1d6 +F test/tkt-26ff0c2d1e.test c15bec890c4d226c0da2f35ff30f9e84c169cfef90e73a8cb5cec11d723dfa96 F test/tkt-2a5629202f.test 0521bd25658428baa26665aa53ffed9367d33af2 -F test/tkt-2d1a5c67d.test d371279946622698ab393ff88cad9f5f6d82960b +F test/tkt-2d1a5c67d.test 92bf2a2de5757d2d24ef554f8a6a38476a6735074e32dc28c775b5b9a23f96a3 F test/tkt-2ea2425d34.test 1cf13e6f75d149b3209a0cb32927a82d3d79fb28 F test/tkt-31338dca7e.test 6fb8807851964da0d24e942f2e19c7c705b9fb58 -F test/tkt-313723c356.test c47f8a9330523e6f35698bf4489bcb29609b53ac -F test/tkt-385a5b56b9.test c0a06ada41d7f06b1686da0e718553f853771d1e +F test/tkt-313723c356.test 4b306ad45c736cedf2f5221f6155b92143244b6d +F test/tkt-385a5b56b9.test 81d7c8ef2d2a378323ba2190c1f02f91022fdff0d8d0b84c04954d02c85ef374 F test/tkt-38cb5df375.test f3cc8671f1eb604d4ae9cf886ed4366bec656678 F test/tkt-3998683a16.test 6d1d04d551ed1704eb3396ca87bb9ccc8c5c1eb7 -F test/tkt-3a77c9714e.test b08bca26de1140bdf004a37716582a43d7bd8be8 +F test/tkt-3a77c9714e.test 90e3e8455ee945a4076d4c44062b8845708af24a880355328fe7008f2047c9f0 F test/tkt-3fe897352e.test 27e26eb0f1811aeba4d65aba43a4c52e99da5e70 F test/tkt-4a03edc4c8.test 91c0e135888cdc3d4eea82406a44b05c8c1648d0 F test/tkt-4c86b126f2.test cbcc611becd0396890169ab23102dd70048bbc9a F test/tkt-4dd95f6943.test 3d0ce415d2ee15d3d564121960016b9c7be79407 F test/tkt-4ef7e3cfca.test 3965ae11cc9cf6e334f9d7d3c1e20bf8d56254b1 F test/tkt-54844eea3f.test a12b851128f46a695e4e378cca67409b9b8f5894 -F test/tkt-5d863f876e.test c9f36ca503fa154a3655f92a69d2c30da1747bfa +F test/tkt-5d863f876e.test 726e76d725f6fe0eb2fc8a522b721b79807380ee F test/tkt-5e10420e8d.test 904d1687b3c06d43e5b3555bbcf6802e7c0ffd84 F test/tkt-5ee23731f.test 9db6e1d7209dc0794948b260d6f82b2b1de83a9f F test/tkt-6bfb98dfc0.test 24780633627b5cfc0635a5500c2389ebfb563336 F test/tkt-752e1646fc.test ea78d88d14fe9866bdd991c634483334639e13bf -F test/tkt-78e04e52ea.test 813779f8888f3ca226df656c4eef078f9635f3c9 -F test/tkt-7a31705a7e6.test e75a2bba4eec801b92c8040eb22096ac6d35e844 -F test/tkt-7bbfb7d442.test 7b2cd79c7a17ae6750e75ec1a7846712a69c9d18 +F test/tkt-78e04e52ea.test b731f2ab7d1c2482ac5152097da02ef4805a45147ba9498d3cd9da27072f34d1 +F test/tkt-7a31705a7e6.test 9e9c057b6a9497c8f7ba7b16871029414ccf6550e7345d9085d6d71c9a56bb6f +F test/tkt-7bbfb7d442.test e87b59e620700b5a52ecd92f05d56686c1cad9e1aa17456eada55e0bb821b698 F test/tkt-80ba201079.test 105a721e6aad0ae3c5946d7615d1e4d03f6145b8 -F test/tkt-80e031a00f.test f50046f474ecf67ad5c50cd9200da04ff887d7cd -F test/tkt-8454a207b9.test c583a9f814a82a2b5ba95207f55001c9f0cd816c +F test/tkt-80e031a00f.test 7c93af53f43527f50020983a4bcc39b077e77c7362d7af8e04a5fd155fe5e829 +F test/tkt-8454a207b9.test ead80b7a01438ca1436cee029694a96c821346cf1e24f06de12f8172e139ddbb F test/tkt-868145d012.test a5f941107ece6a64410ca4755c6329b7eb57a356 F test/tkt-8c63ff0ec.test 258b7fc8d7e4e1cb5362c7d65c143528b9c4cbed F test/tkt-91e2e8ba6f.test 08c4f94ae07696b05c9b822da0b4e5337a2f54c5 -F test/tkt-94c04eaadb.test f738c57c7f68ab8be1c054415af7774617cb6223 +F test/tkt-99378177930f87bd.test 9d6cff39b50d062c813ae1cb0ebbd1b7acf81ecc23ae5d5215e5bb05667dc137 F test/tkt-9a8b09f8e6.test b2ef151d0984b2ebf237760dbeaa50724e5a0667 -F test/tkt-9d68c883.test 458f7d82a523d7644b54b497c986378a7d8c8b67 +F test/tkt-9d68c883.test 16f7cb96781ba579bc2e19bb14b4ad609d9774b6 F test/tkt-9f2eb3abac.test cb6123ac695a08b4454c3792fbe85108f67fabf8 F test/tkt-a7b7803e.test 159ef554234fa1f9fb318c751b284bd1cf858da4 -F test/tkt-a8a0d2996a.test eb597379dbcefa24765763d7f682c00cb5924fa9 +F test/tkt-a7debbe0.test e295fa83cd4416a8ca37b354eb5fadefc5e81fb55253db538d35261fe9c95067 +F test/tkt-a8a0d2996a.test 002e1cde8fc30c39611b52cf981c88200b858765748556822da72e0d32fac73e F test/tkt-b1d3a2e531.test 8f7576e41ca179289ee1a8fee28386fd8e4b0550 F test/tkt-b351d95f9.test d14a503c414c5c58fdde3e80f9a3cfef986498c0 F test/tkt-b72787b1.test a95e8cdad0b98af1853ac7f0afd4ab27b77bf5f3 -F test/tkt-b75a9ca6b0.test 97cc2d5eeaf82799eb42138c0a1ff64370238ce4 +F test/tkt-b75a9ca6b0.test dc6a853c242f7d0326564ae32e9e5eb462b5e8d2bc5b01ea3b18fd24f8e5894b F test/tkt-ba7cbfaedc.test b4c0deccc12aeb55cfdb57935b16b5d67c5a9877 -F test/tkt-bd484a090c.test 60460bf946f79a79712b71f202eda501ca99b898 +F test/tkt-bd484a090c.test e6af3e3a4242cd8f1c91c736364f09075d8e33e3b86f6492a1ee36278ea71b61 F test/tkt-bdc6bbbb38.test fc38bb09bdd440e3513a1f5f98fc60a075182d7d F test/tkt-c48d99d690.test ba61977d62ab612fc515b3c488a6fbd6464a2447 -F test/tkt-cbd054fa6b.test 06ccd57af3c0c7895d0f7dc844f13c51f8258885 +F test/tkt-c694113d5.test 82c461924ada5c14866c47e85535b0b0923ba16a2e907e370061a5ca77f65d77 +F test/tkt-cbd054fa6b.test 6ec9f1a5721fba74a83397683c50f472df68a0a749d193a537264eda3ad6d113 F test/tkt-d11f09d36e.test d999b548fef885d1d1afa49a0e8544ecf436869d F test/tkt-d635236375.test 9d37e988b47d87505bc9445be0ca447002df5d09 F test/tkt-d82e3f3721.test bcc0dfba658d15bab30fd4a9320c9e35d214ce30 F test/tkt-f3e5abed55.test d5a0126118142d13e27f6ce9f4c47096e9321c00 -F test/tkt-f67b41381a.test a23bc124c981662db712167bacd0ed8ad11abac9 +F test/tkt-f67b41381a.test 9120eab5e949969a29087e01bf57ac6a52b6c06c16be41091a74c2a863ffc660 F test/tkt-f777251dc7a.test d1a8fc3eefb7a9e64d19ff24d5c8c94c34a632fb -F test/tkt-f7b4edec.test d998a08ff2b18b7f62edce8e3044317c45efe6c7 +F test/tkt-f7b4edec.test a0d9cf5023af8bfc066e71128f325fd4831c6c6761cad35e451d35c8492f5cf1 F test/tkt-f973c7ac31.test 28ef85c7f015477916795246d8286aeda39d4ead F test/tkt-fa7bf5ec.test 9102dfea58aa371d78969da735f9392c57e2e035 F test/tkt-fc62af4523.test 72825d3febdedcd5593a27989fc05accdbfc2bb4 @@ -1181,7 +1805,7 @@ F test/tkt2817.test f31839e01f4243cff7399ef654d3af3558cb8d8d F test/tkt2820.test 39940276b3436d125deb7d8ebeee053e4cf13213 F test/tkt2822.test f391776423a7c0d0949edfce375708bfb0f3141e F test/tkt2832.test a9b0b74a02dca166a04d9e37739c414b10929caa -F test/tkt2854.test e432965db29e27e16f539b2ba7f502eb2ccc49af +F test/tkt2854.test 47a2ae03bf36812f675ec06806a7b958e0de75312261dd8280c187f4a4a8281a F test/tkt2920.test a8737380e4ae6424e00c0273dc12775704efbebf F test/tkt2927.test 4752868b9eeeb07a217f7f19f4cbaac98d6d086d F test/tkt2942.test c5c87d179799ca6d1fbe83c815510b87cd5ec7ce @@ -1189,15 +1813,15 @@ F test/tkt3080.test 1bca7579260920a66b4dd7e196e807c0f25ff804 F test/tkt3093.test fbdbc5b4969244ad11f540759003e361fcaf391f F test/tkt3121.test 536df66a02838c26a12fe98639354ca1290ca68b F test/tkt3201.test f1500ccecc0d578dc4cde7d3242008297c4d59b3 -F test/tkt3292.test 962465a0984a3b8c757efe59c2c59144871ee1dd +F test/tkt3292.test 7bad4423cf5eb075dbb58511d66d46fe816744754c9f0050ae60157f71a4fca7 F test/tkt3298.test 20fd8773b825cb602e033aa04f8602e1ebdcd93c -F test/tkt3334.test ea13a53cb176e90571a76c86605b14a09efe366d +F test/tkt3334.test 9756631e3c4aa3c416362c279e3c0953a83b7ca8274cb81a13264bb56296d8b0 F test/tkt3346.test 6f67c3ed7db94dfc5df4f5f0b63809a1f611e01a F test/tkt3357.test 77c37c6482b526fe89941ce951c22d011f5922ed F test/tkt3419.test 1bbf36d7ea03b638c15804251287c2391f5c1f6b F test/tkt3424.test 61f831bd2b071bd128fa5d00fbda57e656ca5812 -F test/tkt3442.test 53840ec5325bb94544792aad4c20476f81dc26b1 -F test/tkt3457.test 44e980fe5334753dcc27b94fa4deabc485a92f74 +F test/tkt3442.test c9d95b4c8f4f35a51b523f35d2afd0ce124937812af296545ad551ff763504fd +F test/tkt3457.test adf048188761581124a2f0f91f9d23a5b76fb425270ecbfd73c9c7949fa10786 F test/tkt3461.test 228ea328a5a21e8663f80ee3d212a6ad92549a19 F test/tkt3493.test 1686cbde85f8721fc1bdc0ee72f2ef2f63139218 F test/tkt3508.test d75704db9501625c7f7deec119fcaf1696aefb7d @@ -1207,7 +1831,7 @@ F test/tkt3541.test 5dc257bde9bc833ab9cc6844bf170b998dbb950a F test/tkt3554.test f599967f279077bace39220cbe76085c7b423725 F test/tkt3581.test 1966b7193f1e3f14951cce8c66907ae69454e9a3 F test/tkt35xx.test f38c1b03713179d414969187c941466e44945b35 -F test/tkt3630.test 929f64852103054125200bc825c316d5f75d42f7 +F test/tkt3630.test 9a934c58c259f89a0ae6bb6bb846c56285a6fd0f F test/tkt3718.test 3b59dcb5c4e7754dacd91e7fd353a61492cc402a F test/tkt3731.test 0c5f4cbffe102d43c3b2188af91a9e36348f974b F test/tkt3757.test 10cd679a88675c880533083fc79ac04324525595 @@ -1215,223 +1839,342 @@ F test/tkt3761.test b95ea9c98f21cf91325f18a984887e62caceab33 F test/tkt3762.test 4d439ff7abdc8d9323150269d182c37c2d514576 F test/tkt3773.test 7bca904d2a647a6a4a291bd86d7fd7c73855b789 F test/tkt3791.test a6624b9a80b216a26cf473607f42f3e51898c267 -F test/tkt3793.test d90ffd75c52413908d15e1c44fc2ea9c80fcc449 -F test/tkt3810.test 90fa0635dfa7da9680c8cd3513350a49b3a8ae12 +F test/tkt3793.test 9ee9c6f300adce634e8990a131ad5e20e22241643a1240c6b372b351205ef7cb +F test/tkt3810.test 3a3be9965d1861bd84019875851ad5ea90fd8d76b638361514a36a48ea53191b F test/tkt3824.test 150aa00bb6220672e5f0eb14dc8eaa36750425f0 F test/tkt3832.test 2300d10d57562b89875b72148338ac3e14f8847d F test/tkt3838.test 292e72489101cd1320d7278dc111c173ebf334d4 -F test/tkt3841.test 4659845bc53f809a5932c61c6ce8c5bb9d6b947f +F test/tkt3841.test c4be3870f777f82aa788a588e40b4fb6627c3874e19f336d0d92894f929ffbfe F test/tkt3871.test d921703d07c68f4fd5312073215a17fa34b0401d F test/tkt3879.test 2ad5bef2c87e9991ce941e054c31abe26ef7fb90 F test/tkt3911.test 74cd324f3ba653040cc6d94cc4857b290d12d633 F test/tkt3918.test ea78bf164e4d55cbde0d83c671ef6fbe930a0032 F test/tkt3922.test f26be40ab4fe6c00795629bd2006d96e270d9b1a F test/tkt3929.test cdf67acf5aa936ec4ffead81db87f8a71fe40e59 -F test/tkt3935.test e15261fedb9e30a4305a311da614a5d8e693c767 +F test/tkt3935.test 1ffcfffc148df51c8a01d1b3efae2d6c44cbeb0af1e0c5b88f4afe9a86d4ddb6 F test/tkt3992.test f3e7d548ac26f763b47bc0f750da3d03c81071da F test/tkt3997.test a335fa41ca3985660a139df7b734a26ef53284bd -F test/tkt4018.test 7c2c9ba4df489c676a0a7a0e809a1fb9b2185bd1 +F test/tkt4018.test 18dbc6617f7a4b90e938d1bd6d26ad18daafaf08 F test/tokenize.test ce430a7aed48fc98301611429595883fdfcab5d7 -F test/tpch01.test 04adbf8d8300fa60a222f28d901abd76e7be6dd4 -F test/trace.test 6f676313e3ebd2a50585036d2f212a3319dd5836 +F test/tpch01.test 4479008f85f6f8f25f7ab2cb305d665752b4727fa28a8df3d8e0ad46520c62ff +F test/trace.test a659a9862957f4789e37a92b3bf6d2caf5c86b02cdeefc41e850ae53acf6992a F test/trace2.test f5cb67ad3bc09e0c58e8cca78dfd0b5639259983 -F test/trans.test 6e1b4c6a42dba31bd65f8fa5e61a2708e08ddde6 +F test/trace3.test 2deeac66359c9f007f0fc9fb6336994a5d68fc1a65129f322a9e9546fd537d0a +F test/trans.test 45f6f9ab6f66a7b5744f1caac06b558f95da62501916906cf55586a896f9f439 F test/trans2.test 62bd045bfc7a1c14c5ba83ba64d21ade31583f76 F test/trans3.test 91a100e5412b488e22a655fe423a14c26403ab94 -F test/transitive1.test 293300f46916569f08875cdb2fe2134be2c27677 -F test/trigger1.test ea9624cc1dae05645469df6119fa815f9e6f1e8c -F test/trigger2.test 5cd7d69a7ba1143ee045e4ae2963ff32ae4c87a6 +F test/transitive1.test f8ee983600b33d167da1885657f064aec404e1c0d0bc8765fdf163f4c749237a +F test/trigger1.test 141bd2bbfa82fa91aed7391f50517373c9823ff8f55af35e56313dbc75112ca1 +F test/trigger2.test 30fcb3a6aa6782020d47968735ee6086ed795f73a7affa9406c8d5a36e7b5265 F test/trigger3.test aa640bb2bbb03edd5ff69c055117ea088f121945 F test/trigger4.test 74700b76ebf3947b2f7a92405141eb2cf2a5d359 F test/trigger5.test 619391a3e9fc194081d22cefd830d811e7badf83 F test/trigger6.test 0e411654f122552da6590f0b4e6f781048a4a9b9 -F test/trigger7.test 799c9d2561facef70340cc9e0dee98aee9b5bdfe +F test/trigger7.test e7ce54bfda67a88d778aea42544e151c465547a7e617127b6914c2221a6d53c1 F test/trigger8.test 30cb0530bd7c4728055420e3f739aa00412eafa4 -F test/trigger9.test 2226ec795a33b0460ab5cf8891e9054cc7edef41 -F test/triggerA.test fe5597f47ee21bacb4936dc827994ed94161e332 +F test/trigger9.test 1724b595661da3dd3c8d79f0ebae818132a39e65c241bad2049f66952b1dc29d +F test/triggerA.test 837be862d8721f903dba3f3ceff05b32e0bee5214cf6ea3da5fadf12d3650e9d F test/triggerB.test 56780c031b454abac2340dbb3b71ac5c56c3d7fe -F test/triggerC.test 302d8995f5ffe63bbc15053abb3ef7a39cf5a092 +F test/triggerC.test 29f5a28d0fe39e6e2c01f6e1f53f08c0955170ae10a63ad023e33cb0a1682a51 F test/triggerD.test 8e7f3921a92a5797d472732108109e44575fa650 -F test/triggerE.test 15fa63f1097db1f83dd62d121616006978063d1f -F test/tt3_checkpoint.c 9e75cf7c1c364f52e1c47fd0f14c4340a9db0fe1 +F test/triggerE.test 612969cb57a4ef792059ad6d01af0117e1ae862c283753ffcc9a6428642b22ee +F test/triggerF.test 5d76f0a8c428ff87a4d5ed52da06f6096a2c787a1e21b846111dfac4123de3ad +F test/triggerG.test b4e3fbccde6cf8995177cd6cad880256c8c00e407e07d8c67149f46106292a2c +F test/triggerupfrom.test d1f9e56090408115c522bee626cc33a2f3370f627a5e341d832589d72e3aa271 +F test/trustschema1.test d2996bb284859c99956ac706160eab9f086919da738d19bfef3ac431cce8fd47 +F test/tt3_checkpoint.c ac7ca661d739280c89d9c253897df64a59a49369bd1247207ac0f655b622579d F test/tt3_index.c 39eec10a35f57672225be4d182862152896dee4a F test/tt3_lookaside1.c 0377e202c3c2a50d688cb65ba203afeda6fafeb9 -F test/tt3_stress.c c57d804716165811d979d4a719e05baccd79277f -F test/tt3_vacuum.c 1753f45917699c9c1f66b64c717a717c9379f776 +F test/tt3_shared.c b37d22defc944a2ac4c91c927fd06c1d48cd51e2ce9d004fe868625bd2399f93 +F test/tt3_stress.c f9a769ca8b026ecc76ee93ca8c9700a5619f8e51c581107c4053ba6ac97f616f +F test/tt3_vacuum.c 71b254cde1fc49d6c8c44efd54f4668f3e57d7b3a8f4601ade069f75a999ba39 F test/types.test bf816ce73c7dfcfe26b700c19f97ef4050d194ff -F test/types2.test 3555aacf8ed8dc883356e59efc314707e6247a84 -F test/types3.test 99e009491a54f4dc02c06bdbc0c5eea56ae3e25a +F test/types2.test 1aeb81976841a91eef292723649b5c4fe3bc3cac +F test/types3.test c60e89c4d6babe44b23a2ea0090f3044e549403b20648b1c6bb65a69fea5f1ed +F test/unhex.test b7f1b806207cb77fa31c3e434fe92fba524464e3e9356809bfcc28f15af1a8b7 +F test/unionall.test 04d30726c5056f84f92b3a12bf8d8a1dbbe807d1ddc8af95def09e6ef2dd91e3 +F test/unionall2.test 71e8fa08d5699d50dc9f9dc0c9799c2e7a6bb7931a330d369307a4df7f157fa1 +F test/unionallfault.test 652bfbb630e6c43135965dc1e8f0a9a791da83aec885d626a632fe1909c56f73 +F test/unionvtab.test e1704ab1b4c1bb3ffc9da4681f8e85a0b909fd80b937984fc94b27415ac8e5a4 +F test/unionvtabfault.test e8759f3d14fb938ce9657e2342db34aeac0fb9bc1692b0d1ebb0069630151d06 F test/unique.test 93f8b2ef5ea51b9495f8d6493429b1fd0f465264 F test/unique2.test 3674e9f2a3f1fbbfd4772ac74b7a97090d0f77d2 -F test/unixexcl.test cd6c765f75e50e8e2c2ba763149e5d340ea19825 -F test/unordered.test ca7adce0419e4ca0c50f039885e76ed2c531eda8 -F test/update.test 6c68446b8a0a33d522a7c72b320934596a2d7d32 -F test/uri.test 6630ecbdea2aac10df3c89dbae2243f4c2c353e4 -F test/userauth01.test e740a2697a7b40d7c5003a7d7edaee16acd349a9 -F test/utf16align.test 54cd35a27c005a9b6e7815d887718780b6a462ae -F test/vacuum.test ce91c39f7f91a4273bf620efad21086b5aa6ef1d -F test/vacuum2.test aa048abee196c16c9ba308465494009057b79f9b -F test/vacuum3.test 77ecdd54592b45a0bcb133339f99f1ae0ae94d0d -F test/vacuum4.test d3f8ecff345f166911568f397d2432c16d2867d9 -F test/varint.test ab7b110089a08b9926ed7390e7e97bdefeb74102 +F test/unixexcl.test d2366ef2d3d95249314307861d748924d9ab4f24305541159a08be61ccd4a9ee +F test/unordered.test 0edaf3411d300693bca595897c5201421c6c5ec787990a1dfe2f7f60ae93f1e2 +F test/update.test 258dcf26d401177d3cb7fdf0beab14d671dbe72e8ff6e0435fd85a08fcd57bd9 +F test/update2.test 67455bc61fcbcf96923c45b3bc4f87bc72be7d67575ad35f134906148c7b06d3 +F test/upfrom1.tcl 8859d9d437f03b44174c4524a7a734a391fd4526fcff65be08285dafc9dc9041 +F test/upfrom1.test 8cb06689e99cd707d884faa16da0e8eb26ff658bb01c47ddf72fadade666e6e1 +F test/upfrom2.test 66f3ebf721b3cebd922faee5c386bf244f816d416b57c000753ff51af62328a1 +F test/upfrom3.test 6130f24ebf97f5ea865e5d2a14a2d543fe5428a62e87cc60f62d875e45c1f5f0 +F test/upfrom4.test 78f742a6577c91a7a55c64edb8811004e7c6aa99b8d57b2320f70a918c357807 +F test/upfromfault.test 3a10075a0043f0c4fad6614b2c371f88a8ba5a4acab68b907438413865d6a8d6 +F test/upsert1.test 77e3cbabd6b5c773056aca39bda7a690901f1dbd08b0a26ee3b5aec9e9e26198 +F test/upsert2.test 720e94d09f7362a282bc69b3c6b83d51daeaaf0440eb4920a08b86518b8c7496 +F test/upsert3.test 88d7d590a1948a9cb6eac1b54b0642f67a9f35a1fc0f19b200e97d5d39e3179c +F test/upsert4.test 25d2a1da92f149331ae0c51ca6e3eee78189577585eab92de149900d62994fa5 +F test/upsert5.test 9953b180d02d1369cdbb6c73c900834e5fef8cb78e98e07511c8762ec21cc176 +F test/upsertfault.test f21ca47740841fdb4d61acfa7b17646d773e67724fe8c185b71c018db8a94b35 +F test/uri.test 1250724af9beeed2d6c3716f5b990c483200c54f408d3c0ec9543a3c7961f8fc +F test/uri2.test 9d3ba7a53ee167572d53a298ee4a5d38ec4a8fb7 +F test/utf16align.test 9fde0bb5d3a821594aa68c6829ab9c5453a084384137ebb9f6153e2d678039da +F test/vacuum-into.test 5a489714feecfdabfc7b293be4111564a173dee92c0d6818dd0207f3ade65783 +F test/vacuum.test f3b2257a4fcd659513c866a5d9e5f70999cc58fd5d74e979290385fa350b79ee +F test/vacuum2.test 9fd45ce6ce29f5614c249e03938d3567c06a9e772d4f155949f8eafe2d8af520 +F test/vacuum3.test d9d9a04ee58c485b94694fd4f68cffaba49c32234fdefe1ac1a622c5e17d4ce3 +F test/vacuum4.test 7ea76b769fffeb41f925303b04cbcf5a5bbeabe55e4c60ae754ff24eeeb7c010 +F test/vacuum5.test 263b144d537e92ad8e9ca8a73cc6e1583f41cfd0dda9432b87f7806174a2f48c +F test/vacuum6.test b137b04bf3392d3f5c3b8fda0ce85a6775a70ca112f6559f74ff52dc9ce042fd +F test/vacuummem.test 4b30f5b95a9ff86e9d5c20741e50a898b2dc10b0962a3211571eb165357003fb +F test/values.test 0eda08a6ce6545f1ab012dff4cc72a7dd0fee2510f42444136bb2b2b5ed84bc0 +F test/valuesfault.test 2ef23ed965e3bd08e268cdc38a0d11653390ddbbe1e8e2e98d16f55edd30f6e8 +F test/varint.test bbce22cda8fc4d135bcc2b589574be8410614e62 F test/veryquick.test 57ab846bacf7b90cf4e9a672721ea5c5b669b661 -F test/view.test f6c3a39e0c819891265e1d0754e99960d81ef6c9 -F test/vtab1.test 7c4b81abd88361ada9cbe414c459efca26be6bda -F test/vtab2.test f8cd1bb9aba7143eba97812d9617880a36d247ad +F test/view.test 3c23d7a068e9e4a0c4e6907498042772adea725f0630c3d9638ffd4e5a08b92b +F test/view2.test db32c8138b5b556f610b35dfddd38c5a58a292f07fda5281eedb0851b2672679 +F test/view3.test ad8a8290ee2b55ff6ce66c9ef1ce3f1e47926273a3814e1c425293e128a95456 +F test/vt02.c 5b44ac67b1a283fedecf2d6e2ceda61e7a157f01d44dcb4490dcb1e87d057060 +F test/vt100-a.sql 631eeab18c5adb531bab79aecf64eee3934b42c75a309ee395c814717a6a7651 +F test/vtab1.test 09a72330d0f31eda2ffaa828b06a6b917fb86250ee72de0301570af725774c07 +F test/vtab2.test 14d4ab26cee13ba6cf5c5601b158e4f57552d3b055cdd9406cf7f711e9c84082 F test/vtab3.test b45f47d20f225ccc9c28dc915d92740c2dee311e F test/vtab4.test 8e73ed268f3d596bc3590f45fc948fb40f28e9c3 F test/vtab5.test 889f444970393c73f1e077e2bdc5d845e157a391 -F test/vtab6.test d2986cf418dc51e7fb81d12366bea2caa8b812df -F test/vtab7.test ae560ebea870ed04e9aa4177cc302f910faaabb5 +F test/vtab6.test 2525a2fe2e44ccbed1d758cb2977fb8ab8f07d5312ed8d8799d3529647d11f2f +F test/vtab7.test 70c6f4a1d6177144a8236e4172d5fba92e683440374664ad1f04851fbb335d3c F test/vtab8.test e19fa4a538fcd1bb66c22825fa8f71618fb13583 F test/vtab9.test ea58d2b95d61955f87226381716b2d0b1d4e4f9b -F test/vtabA.test 1317f06a03597eee29f40a49b6c21e1aaba4285f +F test/vtabA.test 325a77e7f0f80aa78ab388875c0ad6fb853acc6ac54d85514650b0ae15da24ff F test/vtabB.test 04df5dc531b9f44d9ca65b9c1b79f12b5922a796 F test/vtabC.test 4528f459a13136f982e75614d120aef165f17292 F test/vtabD.test 05b3f1d77117271671089e48719524b676842e96 -F test/vtabE.test d5024aa42754962f6bb0afd261681686488e7afe -F test/vtabF.test fd5ad376f5a34fe0891df1f3cddb4fe7c3eb077e -F test/vtabH.test 5f5157a1501d9889ec35c1a1832f69612dd31444 +F test/vtabE.test 2a143fe75a11275781d1fd1988d86b66a3f69cb98f4add62e3da8fd0f637b45f +F test/vtabF.test 1918844c7c902f6a16c8dacf1ec8f84886d6e78b +F test/vtabH.test 3fe4c1cbc93a85971990f379116255e2d4f14b583be4d0b9cca67e90226041f1 F test/vtabI.test 751b07636700dbdea328e4265b6077ccd6811a3f -F test/vtab_alter.test 9e374885248f69e251bdaacf480b04a197f125e5 -F test/vtab_err.test 0d4d8eb4def1d053ac7c5050df3024fd47a3fbd8 +F test/vtabJ.test a6aef49d558af90fae10565b29501f82a95781cb4f797f2d13e2d19f9b6bc77b +F test/vtabK.test 13293177528fada1235c0112db0d187d754af1355c5a39371abd365104e3afbf +F test/vtabL.test c7b7f537978005d063fa2f53a3cd5a46ecf651ecd19970cb9ed4203698398deb +F test/vtab_alter.test 736e66fb5ec7b4fee58229aa3ada2f27ec58bc58c00edae4836890c3784c6783 +F test/vtab_err.test dcc8b7b9cb67522b3fe7a272c73856829dae4ab7fdb30399aea1b6981bda2b65 F test/vtab_shared.test 5253bff2355a9a3f014c15337da7e177ab0ef8ad -F test/wal.test 0148c8b3421a25fdb4d9c160e84a681d0646371b -F test/wal2.test 1f841d2048080d32f552942e333fd99ce541dada -F test/wal3.test 5dd734147f1f8f958c5261a1f2775d346d7013ce +F test/vtabdistinct.test 7688f0889358f849fd60bbfde1ded38b014b18066076d4bfbb75395804dfe072 +F test/vtabdrop.test 65d4cf6722972e5499bdaf0c0d70ee3b8133944a4e4bc31862563f32a7edca12 +F test/vtabrhs1.test 9b5ecbc74a689500c33a4b2b36761f9bcc22fcc4e3f9d21066ee0c9c74cf5f6c +F test/wal.test 519c550255c78f55959e9159b93ebbfad2b4e9f36f5b76284da41f572f9d27da +F test/wal2.test f058016abe4627d2664db4b4b87990298d925e66a4c5a2c8e674a0ff6f4c841d +F test/wal3.test 5de023bb862fd1eb9d2ad26fa8d9c43abb5370582e5b08b2ae0d6f93661bc310 F test/wal4.test 4744e155cd6299c6bd99d3eab1c82f77db9cdb3c -F test/wal5.test 88b5d9a6a3d1532497ee9f4296f010d66f07e33c -F test/wal6.test a9d6aa635b9d63607dabdc11406f5f96ca986635 -F test/wal64k.test 163655ecd2cb8afef4737cac2a40fdd2eeaf20b8 +F test/wal5.test 9c11da7aeccd83a46d79a556ad11a18d3cb15aa9 +F test/wal6.test 6a773eff47b989c5142d17f2a7778c02d8260149a648d44ef8345aa080e428e3 +F test/wal64k.test bb8c52f0140aae1de877ffed86e2a97d903f98cf9ac263f185d51c58cde92327 F test/wal7.test 2ae8f427d240099cc4b2dfef63cff44e2a68a1bd -F test/wal8.test 75c42e1bc4545c277fed212f8fc9b7723cd02216 +F test/wal8.test d9df3fba4caad5854ed69ed673c68482514203c8 F test/wal9.test 378e76a9ad09cd9bee06c172ad3547b0129a6750 -F test/wal_common.tcl a98f17fba96206122eff624db0ab13ec377be4fe -F test/walbak.test b9f68e39646375c2b877be906babcc15d38b4877 +F test/wal_common.tcl 204d1721ac13c5e0c7fae6380315b5ab7f4e8423f580d826c5e9df1995cb018d +F test/walbak.test 018d4e5a3d45c6298d11b99f09a8ef6876527946 F test/walbig.test f437473a16cfb314867c6b5d1dbcd519e73e3434 -F test/walblock.test be48f3a75eff0b4456209f26b3ce186c2015497d -F test/walcksum.test bb234a1bb42248b3515d992b719708015c384278 +F test/walblock.test 6bb472e82730e7e4e81395e907a01d8cfc2bd9e1f01f8a9184ca572e2955a4bf +F test/walckptnoop.test b13a2c3140f2c913cfd422d9a224544757d04b8b14ab4c267ab9910467c0b9be +F test/walcksum.test 50e204500eed9c691b6045e467bb2923f49aa93d8adf315e2be135fdb202c1c2 F test/walcrash.test 21038858cc552077b0522f50b0fa87e38139306a F test/walcrash2.test a0edab4e5390f03b99a790de89aad15d6ec70b36 F test/walcrash3.test e426aa58122d20f2b9fbe9a507f9eb8cab85b8af -F test/walfault.test 1f8389f7709877e9b4cc679033d71d6fe529056b +F test/walcrash4.test 93d8825e9d0b1b183e3a73ee67e5c8fc9d86cdaf1f3a03bcc960c3aeede28001 +F test/walfault.test 09b8ad7e52d2f54bce50e31aa7ea51412bb9f70ac13c74e669ddcd8b48b0d98d +F test/walfault2.test e039ac66c78d5561683cacde04097213cdad3b58e2b3f3fe1112862217bfd915 F test/walhook.test ed00a40ba7255da22d6b66433ab61fab16a63483 -F test/walmode.test 4022fe03ae6e830583672caa101f046438a0473c -F test/walnoshm.test 84ca10c544632a756467336b7c3b864d493ee496 +F test/walmode.test 2a5530972948948a17211e070263fcf25ef1ca4e06d742a32d81a470b91441dc +F test/walnoshm.test 844b3eb7d8e8ee76c834ef723babec57b0be51fa52ef7e321c289ed0fe3cddc2 F test/waloverwrite.test dad2f26567f1b45174e54fbf9a8dc1cb876a7f03 -F test/walpersist.test 8c6b7e3ec1ba91b5e4dc4e0921d6d3f87cd356a6 -F test/walprotocol.test 059cb75484a1ecf6357a2c1b3324b8156749221e -F test/walro.test 34422d1d95aaff0388f0791ec20edb34e2a3ed57 -F test/walshared.test 0befc811dcf0b287efae21612304d15576e35417 -F test/walslow.test c05c68d4dc2700a982f89133ce103a1a84cc285f -F test/walthread.test de8dbaf6d9e41481c460ba31ca61e163d7348f8e -F test/where.test 9902a3d84e9bc80357a2c54ed0e76c0d6d04a867 -F test/where2.test af78c55589cbc82d793449493adba0dc3d659f23 -F test/where3.test 1ad55ba900bd7747f98b6082e65bd3e442c5004e +F test/walpersist.test 8d78a1ec91299163451417b451a2bac3481f8eb9f455b1ca507a6625c927ca6e +F test/walprotocol.test 1b3f922125e341703f6e946d77fdc564d38fb3e07a9385cfdc6c99cac1ecf878 +F test/walprotocol2.test 7d3b6b4bf0b12f8007121b1e6ef714bc99101fb3b48e46371df1db868eebc131 +F test/walro.test 78a84bc0fdae1385c06b017215c426b6845734d6a5a3ac75c918dd9b801b1b9d +F test/walro2.test 33955a6fd874dd9724005e17f77fef89d334b3171454a1256fe4941a96766cdc +F test/walrofault.test c70cb6e308c443867701856cce92ad8288cd99488fa52afab77cca6cfd51af68 +F test/walseh1.test bae700eb99519b6d5cd3f893c04759accc5a59c391d4189fe4dd6995a533442b +F test/walsetlk.test 9079cd8ef82570b8cf0067f31e049a72bec353fb2d5f0cc88f1736dc42ba9704 +F test/walsetlk2.test 4a67823b1e759ac5a4fe78a83c1f857c3c5761bf8d755421c8b55907957f23dd +F test/walsetlk3.test 1b82bd92dea7e58f498b4399b0b3d26773dd8ac5c74205ce4a23c207cb8e85fe +F test/walsetlk_recover.test adccbffc59e365063a4efd2da6b661ae2fcf15d775b6719fe46acd87face08ff +F test/walsetlk_snapshot.test 86d5588380f9927d8fcbbd75133b0a34fddf959378d6823c6f164a390123f70a +F test/walshared.test 42e3808582504878af237ea02c42ca793e8a0efaa19df7df26ac573370dbc7a3 +F test/walslow.test 0c51843836c9dcf40a5ac05aa781bfb977b396ee2c872d92bd48b79d5dd9aa23 +F test/walthread.test d562f51a61191ccfab64940df7aa1cef87c902fa5ab742590ef7f859dfe6a44b +F test/walvfs.test e1a6ad0f3c78e98b55c3d5f0889cf366cc0d0a1cb2bccb44ac9ec67384adc4a1 +F test/where.test 5087c72d26fd075a1644c8512be9fe18de9bf2d2b0754f7fd9b74a1c6540c4fc +F test/where2.test 52237a8cb27ebbf6583469429bb5733d1b94ac37d09c3dbd0f487952ed0ab3f8 +F test/where3.test 4ccb156ae33de86414a52775a6f590a9d60ba2cbc7a93a24fa331b7bcf5b6030 F test/where4.test 4a371bfcc607f41d233701bdec33ac2972908ba8 F test/where5.test fdf66f96d29a064b63eb543e28da4dfdccd81ad2 F test/where6.test 5da5a98cec820d488e82708301b96cb8c18a258b -F test/where7.test f520bcec2c3d12dc4615623b06b2aec7c2d67e94 -F test/where8.test 98eedca0d375fb400b8377269c4b4686582dfb45 -F test/where9.test 729c3ba9b47e8f9f1aab96bae7dad2a524f1d1a2 -F test/whereA.test 4d253178d135ec46d1671e440cd8f2b916aa6e6b +F test/where7.test 15041c7a5838f3bac98f3fb933709674a0b59367664e88fafaf105ff7416eb07 +F test/where8.test 461ca40265ed996a6305da99bb024b0e41602bb586acf544c08f95922358e49f +F test/where9.test 2db942671a002621eff4f713e347bb25243295f79d8990297cd160bebcfde3f7 +F test/whereA.test 9d1077b117f1b68d5f739d94f36956c36cf995eb87bb19b77b2e81af020edd20 F test/whereB.test 0def95db3bdec220a731c7e4bec5930327c1d8c5 F test/whereC.test cae295158703cb3fc23bf1a108a9ab730efff0f6 -F test/whereD.test 51366b07cb6f546cd30cc803f7e754f063b940de -F test/whereE.test b3a055eef928c992b0a33198a7b8dc10eea5ad2f -F test/whereF.test 5b2ba0dbe8074aa13e416b37c753991f0a2492d7 -F test/whereG.test dde4c52a97385a55be6a7cd46be8373f0cf35501 +F test/whereD.test c1c335e914e28b122e000e9310f02d2be83e1c9dbca2e29f46bd732703944d1b +F test/whereE.test 7a727b5d5b6bc8fa4cef5206e90cc0363e55ca7f0566f6fbad0206e43170f59e +F test/whereF.test 926b65519608e3f2aa28720822b9154fb5c7b13519dd78194f434a511ab3dac5 +F test/whereG.test 875d020ac0a47828b31e36c54f1bf0cf81c9ea43b257bc21286eca1fe9a4880b F test/whereH.test e4b07f7a3c2f5d31195cd33710054c78667573b2 -F test/whereI.test eab5b226bbc344ac70d7dc09b963a064860ae6d7 -F test/whereJ.test 55a3221706a7ab706293f17cc8f96da563bf0767 -F test/whereK.test f8e3cf26a8513ecc7f514f54df9f0572c046c42b -F test/wherefault.test 1374c3aa198388925246475f84ad4cd5f9528864 -F test/wherelimit.test 5e9fd41e79bb2b2d588ed999d641d9c965619b31 -F test/wild001.test bca33f499866f04c24510d74baf1e578d4e44b1c -F test/win32heap.test ea19770974795cff26e11575e12d422dbd16893c -F test/win32lock.test fbf107c91d8f5512be5a5b87c4c42ab9fdd54972 -F test/win32longpath.test 169c75a3b2e43481f4a62122510210c67b08f26d -F test/with1.test cef099a491eac9874f2c28bd2dc86394fb3e47b3 -F test/with2.test 2b40da883658eb74ad8ad06afabe11a408e7fb87 -F test/with3.test 511bacdbe41c49cf34f9fd1bd3245fe1575bca98 +F test/whereI.test c4bb7e2ca56d49bd8ab5c7bd085b8b83e353922b46904d68aefb3c7468643581 +F test/whereJ.test fc05e374cc9f2dc204148d6c06822c380ad388895fe97a6d335b94a26a08aecf +F test/whereK.test 0270ab7f04ba5436fb9156d31d642a1c82727f4c4bfe5ba90d435c78cf44684a +F test/whereL.test cb115604cc9bd61acbc99a1f1df0eb1ea7a7875a77fef25ba9282f01d10283e1 +F test/whereM.test 0dbc9998783458ddcf3cc078ca7c2951d8b2677d472ecf0028f449ed327c0250 +F test/whereN.test 63a3584b71acfb6963416de82f26c6b1644abc5ca6080c76546b9246734c8803 +F test/wherefault.test 6cf2a9c5712952d463d3f45ebee7f6caf400984df51a195d884cfb7eb0e837a7 +F test/wherelfault.test 9012e4ef5259058b771606616bd007af5d154e64cc25fa9fd4170f6411db44e3 +F test/wherelimit.test afb46397c6d7e964e6e294ba3569864a0c570fe3807afc634236c2b752372f31 +F test/wherelimit2.test b9e4bfe7b4d7c2f85f99cf2bd2c51369378d04b1f3d1b60557423752003bfd90 +F test/wherelimit3.test 22d73e046870cf8bbe15573eda6b432b07ebe64a88711f9f849c6b3667c1fae6 +F test/widetab1.test c296a98e123762de79917350e45fa33fdf88577a2571eb3a64c8bf7e44ef74d1 +F test/win32heap.test 1ec2ce646aee491ec23bfcdfd005b33c79f13bf91467966f374a76ffe7c7e85f +F test/win32lock.test e56d7a9b6cf9d5f3867c2dd19ff36c5326881e4038c6867610ecb3a9868ea4eb +F test/win32longpath.test 0f9837039b306735c13521c5f25b6ed42937b600dace58e28a3d2f8baf429b6a +F test/win32nolock.test 95854dc0206b8a95e4aee15a76acc082767b38f079b2e24676aed6cbb0f32798 +F test/window1.test b46d28b9698559e66aa4adafd8074b940faee498bf0c4fbdb62548bfcccc67e7 +F test/window2.tcl 492c125fa550cda1dd3555768a2303b3effbeceee215293adf8871efc25f1476 +F test/window2.test e466a88bd626d66edc3d352d7d7e1d5531e0079b549ba44efb029d1fbff9fd3c +F test/window3.tcl acea6e86a4324a210fd608d06741010ca83ded9fde438341cb978c49928faf03 +F test/window3.test 330733bcca73aba4ddae7a1011f2a2120ef7a0c68d8155854e08677417b8dbd0 +F test/window4.tcl 6f85307eb67242b654d051f7da32a996a66aee039a09c5ae358541aa61720742 +F test/window4.test fbead87f681400ac07ef3555e0488b544a47d35491f8bf09a7474b6f76ce9b4e +F test/window5.test d328dd18221217c49c144181975eea17339eaeaf0e9aa558cee3afb84652821e +F test/window6.test 311de885bd7e453134fa6747680bfb4a1be87c91720bf58703db945891e7d30b +F test/window7.tcl 6a1210f05d40ec89c22960213a22cd3f98d4e2f2eb20646c83c8c30d4d76108f +F test/window7.test 1d31276961ae7801edc72173edaf7593e3cbc79c06d1f1f09e20d8418af403cd +F test/window8.tcl c57364e64d816f6e26df60437e1202e2c1031c7b818a1a67535d1006862a026a +F test/window8.test 3d931e58802b8ab8063da00f0cf30aa3351640238a952c0efb5a129e2349a4bb +F test/window9.test 7b98a7916dd87763ea35f56ea023e3b29e99744582204ccf2937a3bac411cd4d +F test/windowA.test 6d63dc1260daa17141a55007600581778523a8b420629f1282d2acfc36af23be +F test/windowB.test aad7c31739999f68a98a813cfd78390918fc70f56d2d925317a1523cab548ecf +F test/windowC.test 6fd75f5bb2f1343d34e470e36e68f0ff638d8a42f6aa7d99471261b31a0d42f2 +F test/windowD.test 65cf5a765fb8072450e8a0de2979ce7f09a38d87724fe1280c6444073e3da49b +F test/windowE.test d045a5fbaaf50ecac9483e1249dd317ba4f9d189c405a730ba6effdefb87b94f +F test/windowerr.tcl f5acd6fbc210d7b5546c0e879d157888455cd4a17a1d3f28f07c1c8a387019e0 +F test/windowerr.test a8b752402109c15aa1c5efe1b93ccb0ce1ef84fa964ae1cd6684dd0b3cc1819b +F test/windowfault.test 15094c1529424e62f798bc679e3fe9dfab6e8ba2f7dfe8c923b6248c31660a7c +F test/windowpushd.test c420e2265f0e09a0e798d0513a660d71b51602088d81b3dbd038918ee1339dcc +F test/with1.test 1ee171d7c306ab8b0771f3511d870f56c735607729836585bbceb1fc2f47e0b1 +F test/with2.test 181674a6cc86a601ca2ac052741cdfad5b529e07e870435d2f6cdb92d589ff17 +F test/with3.test e30369ea27aa27eb1bda4c5e510c8a9f782c8afd2ab99d1a02b8a7f25a5d3e65 +F test/with4.test 257be66c0c67fee1defbbac0f685c3465e2cad037f21ce65f23f86084f198205 +F test/with5.test 6248213c41fab36290b5b73aa3f937309dfba337004d9d8434c3fabc8c7d4be8 +F test/with6.test 281e4861b5e517f6c3c2f08517a520c1e2ee7c11966545d3901f258a4fe8ef76 F test/withM.test 693b61765f2b387b5e3e24a4536e2e82de15ff64 -F test/without_rowid1.test 1a7b9bd51b899928d327052df9741d2fe8dbe701 +F test/without_rowid1.test f6e75e32821eb423ac3812434d12bdd8098f17e3b2206da61575e1db77f82428 F test/without_rowid2.test af260339f79d13cb220288b67cd287fbcf81ad99 -F test/without_rowid3.test aad4f9d383e199349b6c7e508a778f7dff5dff79 +F test/without_rowid3.test 39ab0dd773eaa62e59b17093f875327630f54c4145458f6d2b053d68d4b2f67b F test/without_rowid4.test 4e08bcbaee0399f35d58b5581881e7a6243d458a -F test/without_rowid5.test 89b1c587bd92a0590e440da33e7666bf4891572a -F test/without_rowid6.test 1f99644e6508447fb050f73697350c7ceca3392e -F test/wordcount.c 2a0a6c0d0e8e8bbbac1f06d72a6791828c37c0cf -F test/zeroblob.test 3857870fe681b8185654414a9bccfde80b62a0fa -F test/zerodamage.test cf6748bad89553cc1632be51a6f54e487e4039ac -F tool/GetFile.cs a15e08acb5dd7539b75ba23501581d7c2b462cb5 -F tool/GetTclKit.bat 629d87562e0487c386db630033931d12d62e6372 -F tool/addopcodes.tcl 4ca9c3ef196f08da30add5d07ce0c9458dc8c633 -F tool/build-all-msvc.bat 31866578036cd1d962628059b0760d407c3ce4d8 x -F tool/build-shell.sh 950f47c6174f1eea171319438b93ba67ff5bf367 -F tool/cg_anno.tcl 692ce4b8693d59e3a3de77ca97f4139ecfa641b0 x +F test/without_rowid5.test f14298eb5ac8013894b75141c3f4f5f325a6ad0eded55516eef72c49e60a67cb +F test/without_rowid6.test efbd7add62c59bf5ca97bf8da674e734e6a70ef979234e816166824b4d258f68 +F test/without_rowid7.test 747d3fbef7108d416d6416694b03d8ff046dc9cc82b3d911715feb472444b95a +F test/wordcount.c d721a4b6fae93e6e33449700bce1686bc23257c27425bc3ef1599dc912adec66 +F test/writecrash.test 13520af28f376bfc8c0bcd130efc1fff20bb165198e8b94cf153f1f754154bb9 +F test/zeroblob.test 7b74cefc7b281dfa2b07cd237987fbe94b4a2037a7771e9e83f2d5f608b1d99e +F test/zeroblobfault.test 861d8191a0d944dfebb3cb4d2c5b4e46a5a119eaec5a63dd996c2389f8063441 +F test/zerodamage.test 9c41628db7e8d9e8a0181e59ea5f189df311a9f6ce99cc376dc461f66db6f8dc +F test/zipfile.test c52db63e31a66ae4245affa3e4e65e302442a87e5fd5f2ad29060bc849a83480 +F test/zipfile2.test a577e0775e32ef8972e7d5e9a45bc071a5ae061b5b965a08c9c4b709ad036a25 +F test/zipfilefault.test 44d4d7a7f7cca7521d569d7f71026b241d65a6b1757aa409c1a168827edbbc2c +F tool/GetFile.cs 47852aa0d806fe47ed1ac5138bdce7f000fe87aaa7f28107d0cb1e26682aeb44 +F tool/GetTclKit.bat d84033c6a93dfe735d247f48ba00292a1cc284dcf69963e5e672444e04534bbf +F tool/Replace.cs 02c67258801c2fb5f63231e0ac0f220b4b36ba91 +F tool/build-all-msvc.bat 1960a7a3e5d8176c4329e31476f6e3dfa9543675355fa9020a569f4452628458 x +F tool/build-shell.sh 369c4b171cc877ad974fef691e4da782b4c1e99fe8f4361316c735f64d49280f +F tool/buildtclext.tcl d09b753d7858314104eeaf5f4def85d35784470279809e47a633f142226f2b3f +F tool/cg_anno.tcl c1f875f5a4c9caca3d59937b16aff716f8b1883935f1b4c9ae23124705bc8099 x F tool/checkSpacing.c 810e51703529a204fc4e1eb060e9ab663e3c06d2 +F tool/cktclsh.sh 6075eef9c6b9ba4b38fef2ca2a66d25f2311bd3c610498d18a9b01f861629cca +F tool/cp.tcl 9a0d663ad45828de13763ee7ca0200f31f56c6d742cf104a56ae80e027c242d8 +F tool/custom.txt 24ed55e71c5edae0067ba159bbf09240d58b160331f7716e95816cd3aa0ba5c4 +F tool/dbhash.c 5da0c61032d23d74f2ab84ffc5740f0e8abec94f2c45c0b4306be7eb3ae96df0 +F tool/dbtotxt.c ca48d34eaca6d6b6e4bd6a7be2b72caf34475869054240244c60fa7e69a518d6 +F tool/dbtotxt.md c9a57af8739957ef36d2cfad5c4b1443ff3688ed33e4901ee200c8b651f43f3c +F tool/emcc.sh.in 41a049468c8155433e37e656ba5bae063a000768b1d627025f277732c4e7c4a4 +F tool/enlargedb.c 3e8b2612b985cfa7e3e8800031ee191b43ae80de96abb5abbd5eada62651ee21 +F tool/extract-sqlite3h.tcl 069ceab0cee26cba99952bfa08c0b23e35941c837acabe143f0c355d96c9e2eb x F tool/extract.c 054069d81b095fbdc189a6f5d4466e40380505e2 -F tool/fast_vacuum.c 5ba0d6f5963a0a63bdc42840f678bad75b2ebce1 +F tool/fast_vacuum.c c129ae2924a48310c7b766810391da9e8fda532b9f6bd3f9a9e3a799a1b42af9 F tool/fragck.tcl 5265a95126abcf6ab357f7efa544787e5963f439 -F tool/fuzzershell.c 94019b185caceffc9f7c7b678a6489e42bc2aefa -F tool/genfkey.README cf68fddd4643bbe3ff8e31b8b6d8b0a1b85e20f4 -F tool/genfkey.test 4196a8928b78f51d54ef58e99e99401ab2f0a7e5 +F tool/fuzzershell.c 41480c8a1e4749351f381431ecfdfceba645396c5d836f8d26b51a33c4a21b33 +F tool/genfkey.README e550911fa984c8255ebed2ef97824125d83806eb5232582700de949edf836eb1 +F tool/genfkey.test b6afd7b825d797a1e1274f519ab5695373552ecad5cd373530c63533638a5a4f F tool/getlock.c f4c39b651370156cae979501a7b156bdba50e7ce -F tool/lemon.c 799e73e19a33b8dd7767a7fa34618ed2a9c2397d -F tool/lempar.c 3ec1463a034b37d87d782be5f6b8b10a3b1ecbe7 -F tool/loadfts.c c3c64e4d5e90e8ba41159232c2189dba4be7b862 -F tool/logest.c eef612f8adf4d0993dafed0416064cf50d5d33c6 -F tool/mkautoconfamal.sh c78caa3214f25dc28ea157b5a82abb311f209906 -F tool/mkkeywordhash.c f7f3b342211ac6a14258b9726d5b97cf4f548f22 -F tool/mkmsvcmin.tcl d57e6efc9428605f5418d0b235721ddf7b5d9c0b -F tool/mkopcodec.tcl d1b6362bd3aa80d5520d4d6f3765badf01f6c43c -F tool/mkopcodeh.tcl 385c62d78c38b2d92146dcb5abd319dbbc33506d -F tool/mkopts.tcl 66ac10d240cc6e86abd37dc908d50382f84ff46e -F tool/mkpragmatab.tcl f0d5bb266d1d388cf86fce5ba01a891e95d72d41 -F tool/mkspeedsql.tcl a1a334d288f7adfe6e996f2e712becf076745c97 -F tool/mksqlite3c-noext.tcl 87240b09c20042999b41d5fabe091b7111287835 -F tool/mksqlite3c.tcl b66b4170f693602cd6985aed15d9509fe2f18c84 -F tool/mksqlite3h.tcl 1d41ab59bffb025121f75b76e183125ce41b3ec8 -F tool/mksqlite3internalh.tcl eb994013e833359137eb53a55acdad0b5ae1049b -F tool/mkvsix.tcl fbeb0af7cffdf64e0fba6d65e2e5120dc14595f4 -F tool/offsets.c fe4262fdfa378e8f5499a42136d17bf3b98f6091 -F tool/omittest.tcl 34d7ac01fe4fd18e3637f64abe12c40eca0f6b97 -F tool/pagesig.c ff0ca355fd3c2398e933da5e22439bbff89b803b -F tool/replace.tcl 7727c60a04299b65a92f5e1590896fea0f25b9e0 -F tool/restore_jrnl.tcl 6957a34f8f1f0f8285e07536225ec3b292a9024a +F tool/index_usage.c f62a0c701b2c7ff2f3e21d206f093c123f222dbf07136a10ffd1ca15a5c706c5 +F tool/lemon.c 8f6c122e5727cb0e5f302b8efc91489b1947a8d98206d7a1b1cfc0ed685b6e7c +F tool/lempar.c bdffd3b233a4e4e78056c9c01fadd2bb3fe902435abde3bce3d769fdf0d5cca2 +F tool/libvers.c caafc3b689638a1d88d44bc5f526c2278760d9b9 +F tool/loadfts.c 63412f9790e5e8538fbde0b4f6db154aaaf80f7a10a01e3c94d14b773a8dd5a6 +F tool/logest.c c34e5944318415de513d29a6098df247a9618c96d83c38d4abd88641fe46e669 +F tool/max-limits.c cbb635fbb37ae4d05f240bfb5b5270bb63c54439 +F tool/merge-test.tcl de76b62f2de2a92d4c1ca4f976bce0aea6899e0229e250479b229b2a1914b176 +F tool/mkamalzip.tcl 8aa5ebe7973c8b8774062d34e15fea9815c4cc2ceea3a9b184695f005910876a +F tool/mkautoconfamal.sh 647dada5e34c466bef62a4408e1c99a7e5e1922805479dd57944f33f9803f2f8 +F tool/mkccode.tcl c42a8f8cf78f92e83795d5447460dbce7aaf78a3bbf9082f1507dc71a3665f3c x +F tool/mkctimec.tcl 3fb5cad05922f5da61262cb6bcd5868a34e94a49ca8833ae2d7796e7df075576 x +F tool/mkkeywordhash.c 6b0be901c47f9ad42215fc995eb2f4384ac49213b1fba395102ec3e999acf559 +F tool/mkmsvcmin.tcl d76c45efda1cce2d4005bcea7b8a22bb752e3256009f331120fb4fecb14ebb7a +F tool/mkopcodec.tcl 33d20791e191df43209b77d37f0ff0904620b28465cca6990cf8d60da61a07ef +F tool/mkopcodeh.tcl 2b4e6967a670ef21bf53a164964c35c6163277d002a4c6f56fa231d68c88d023 +F tool/mkopts.tcl 680f785fdb09729fd9ac50632413da4eadbdf9071535e3f26d03795828ab07fa +F tool/mkpragmatab.tcl 3801ce32f8c55fe63a3b279f231fb26c2c1a2ea9a09d2dd599239d87a609acec +F tool/mkshellc.tcl bab0a72a68384181a5706712dfdf6815f6526446d4e8aacace2de5e80cda91b2 +F tool/mksourceid.c 36aa8020014aed0836fd13c51d6dc9219b0df1761d6b5f58ff5b616211b079b9 +F tool/mksqlite3c-noext.tcl 351c55256213154cabb051a3c870ef9f4487de905015141ae50dc7578a901b84 +F tool/mksqlite3c.tcl 7a268139158e5deef27a370bc2f8db6ccf100c1ad7ac5e5b23743c0fd354f609 +F tool/mksqlite3h.tcl ef6831c97e6e638d2324863e8125306baea239b23defd75da77edffa3b620e81 +F tool/mksqlite3internalh.tcl 46ef6ed6ccd3c36e23051109dd25085d8edef3887635cea25afa81c4adf4d4db +F tool/mksrczip.tcl 81efd9974dbb36005383f2cd655520057a2ae5aa85ac2441a80c7c28f803ac52 +F tool/mktoolzip.tcl c9f388b2b0751982aef29a0c1d80f7959dbe38065ebb4421c39844e92622b086 +F tool/mkvsix.tcl 67b40996a50f985a573278eea32fc5a5eb6110bdf14d33f1d8086e48c69e540a +F tool/mkwinarm64ec.tcl 171f79234fa53552a129b360356df5599fdab15239caffb3d29c571292728033 +F tool/offsets.c 8ed2b344d33f06e71366a9b93ccedaa38c096cc1dbd4c3c26ad08c6115285845 +F tool/omittest-msvc.tcl d6b8f501ac1d7798c4126065030f89812379012cad98a1735d6d7221492abc08 +F tool/omittest.tcl bec70ef0e16255c8d9eb06ecd7edf823c07a60a836186cdbce3528fb34b67995 +F tool/opcodesum.tcl 740ed206ba8c5040018988129abbf3089a0ccf4a +F tool/pagesig.c f98909b4168d9cac11a2de7f031adea0e2f3131faa7515a72807c03ec58eafeb +F tool/replace.tcl 511c61acfe563dfb58675efb4628bb158a13d48ff8322123ac447e9d25a82d9a +F tool/restore_jrnl.tcl 1079ecba47cc82fa82115b81c1f68097ab1f956f357ee8da5fc4b2589af6bd98 F tool/rollback-test.c 9fc98427d1e23e84429d7e6d07d9094fbdec65a5 -F tool/run-speed-test.sh 0ae485af4fe9f826e2b494be8c81f8ca9e222a4a -F tool/showdb.c 82dca79a999b2701c62417636345e9974151fdad +F tool/showdb.c 3956d71e5193162609a60e8c9edfcf09274c00cfea2b1d221261427adb2b5cca F tool/showjournal.c 5bad7ae8784a43d2b270d953060423b8bd480818 -F tool/showlocks.c 9920bcc64f58378ff1118caead34147201f48c68 -F tool/showstat4.c bda40d6e395df7edb6e9ea630784d3d762c35b4b -F tool/showwal.c ec79959834f7b21f1e0a2aa52bb7c056d2203977 -F tool/soak1.tcl 8d407956e1a45b485a8e072470a3e629a27037fe -F tool/spaceanal.tcl 93c1fdc9733c525b17a2024c7df193daa002e037 -F tool/speedtest.tcl 06c76698485ccf597b9e7dbb1ac70706eb873355 -F tool/speedtest16.c ecb6542862151c3e6509bbc00509b234562ae81e -F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff -F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224 -F tool/speedtest8inst1.c 7ce07da76b5e745783e703a834417d725b7d45fd -F tool/split-sqlite3c.tcl d9be87f1c340285a3e081eb19b4a247981ed290c -F tool/sqldiff.c 5a26205111e6fa856d9b1535b1637744dcdb930b -F tool/srcck1.c 4c39bdfa9a92edd20233ee720df84dbeb2417602 +F tool/showlocks.c 9cc5e66d4ebbf2d194f39db2527ece92077e86ae627ddd233ee48e16e8142564 +F tool/showshm.c a0ab6ec32dd1f11218ca2a4018f8fb875b59414801ab8ceed8b2e69b7b45a809 +F tool/showstat4.c b706fcbc4cd1a6e4a73ac32549afc4b460479d650402d64b23e8d813516e8de4 +F tool/showwal.c 11eca547980a066b081f512636151233350ac679f29ecf4ebfce7f4530230b3d +F tool/soak1.tcl a3892082ed1079671565c044e93b55c3c7f38829aedf53cc597c65d23ffdaddf +F tool/spaceanal.tcl 1f83962090a6b60e1d7bf92495d643e622bef9fe82ea3f2d22350dcbce9a12d0 +F tool/spellsift.tcl 52b4b04dc4333c7ab024f09d9d66ed6b6f7c6eb00b38497a09f338fa55d40618 x +F tool/split-sqlite3c.tcl 4969fd642dad0ea483e4e104163021d92baf98f6a8eac981fe48525f9b873430 +F tool/sqldiff.c 134be7866be19f8beb32043d5aea5657f01aaeae2df8d33d758ff722c78666b9 +F tool/sqlite3_analyzer.c.in 14f02cb5ec3c264cd6107d1f1dad77092b1cf440fc196c30b69ae87b56a1a43b +F tool/sqlite3_rsync.c d0e58a1e49fe2192c3ee0b697aed182d502bebfe5b4b406ba6b2baa52a04ecbe +F tool/sqltclsh.c.in 1bcc2e9da58fadf17b0bf6a50e68c1159e602ce057210b655d50bad5aaaef898 +F tool/sqltclsh.tcl 862f4cf1418df5e1315b5db3b5ebe88969e2a784525af5fbf9596592f14ed848 +F tool/src-verify.c 6c655d9a8d6b30f3648fc78a79bf3838ed68f8543869d380c43ea9f17b3b8501 +F tool/srcck1.c 559e703c6cca1d70398bdba1d7f91036c1a71adf718a1aaa6401a562ccaed154 +F tool/srctree-check.tcl fa4d82dd3e8a38d5cbce7d6ade8abef2f42b9eca0394484d521dc8d086739460 F tool/stack_usage.tcl f8e71b92cdb099a147dad572375595eae55eca43 +F tool/stripccomments.c 68d2aa8cb504439f541ce66b8f128067612bdd16f5fb7bfe540f3fcb67c9c197 F tool/symbols-mingw.sh 4dbcea7e74768305384c9fd2ed2b41bbf9f0414d -F tool/symbols.sh fec58532668296d7c7dc48be9c87f75ccdb5814f -F tool/tostr.tcl 96022f35ada2194f6f8ccf6fd95809e90ed277c4 +F tool/symbols.sh 1612bd947750e21e7b47befad5f6b3825b06cce0705441f903bf35ced65ae9b9 +F tool/tclConfigShToMake.sh 7c065d81c2d178e15e45a77372c6e5a38b5a1b08755301cd6f20a3a862db7312 x F tool/varint.c 5d94cb5003db9dbbcbcc5df08d66f16071aee003 -F tool/vdbe-compress.tcl 5926c71f9c12d2ab73ef35c29376e756eb68361c -F tool/vdbe_profile.tcl 246d0da094856d72d2c12efec03250d71639d19f -F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4 -F tool/warnings.sh ef6ebc6fd8d2dc35db3b622015c16a023d4fef4f +F tool/vdbe-compress.tcl fa2f37ab39b2a0087fafb6a7f3ce19503e25e624ffa8ed9951717ab72920c088 +F tool/vdbe_profile.tcl 3ac5a4a9449f4baf77059358ea050db3e34395ccf59c5464d29b91746d5b961e +F tool/version-info.c 33d0390ef484b3b1cb685d59362be891ea162123cea181cb8e6d2cf6ddf2700c +F tool/warnings-clang.sh bbf6a1e685e534c92ec2bfba5b1745f34fb6f0bc2a362850723a9ee87c1b31a7 +F tool/warnings.sh d924598cf2f55a4ecbc2aeb055c10bd5f48114793e7ba25f9585435da29e7e98 F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f -P f694e60a79024967a3f4574e3928f0b28589d381 -R f2e9debe8cf42a0179962a66a44645cf -T +bgcolor * #d0c0ff +P 10e11b9c539a8be50fd93bdf7cf5afe97d9757ce8577cac58426a1b218063e47 +R ec4161915fdee7c9c786931b31f64669 T +sym-release * -T +sym-version-3.11.0 * +T +sym-version-3.51.2 * U drh -Z e719a8e2c047e401006dde85de2084bf -# Remove this line to create a well-formed manifest. +Z 6e3a819551f499011e2275a586e8d608 +# Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.tags b/manifest.tags new file mode 100644 index 0000000000..c644e06f90 --- /dev/null +++ b/manifest.tags @@ -0,0 +1,4 @@ +branch branch-3.51 +tag release +tag branch-3.51 +tag version-3.51.2 diff --git a/manifest.uuid b/manifest.uuid index 5e7431263c..75ccd2e605 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -3d862f207e3adc00f78066799ac5a8c282430a5f +b270f8339eb13b504d0b2ba154ebca966b7dde08e40c3ed7d559749818cb2075 diff --git a/mkso.sh b/mkso.sh deleted file mode 100644 index 6f2e8e25ed..0000000000 --- a/mkso.sh +++ /dev/null @@ -1,32 +0,0 @@ -#!/bin/sh -# -# This script is used to compile SQLite into a shared library on Linux. -# -# Two separate shared libraries are generated. "sqlite3.so" is the core -# library. "tclsqlite3.so" contains the TCL bindings and is the -# library that is loaded into TCL in order to run SQLite. -# -make target_source -cd tsrc -rm shell.c -TCLDIR=/home/drh/tcltk/846/linux/846linux -TCLSTUBLIB=$TCLDIR/libtclstub8.4g.a -OPTS='-DUSE_TCL_STUBS=1 -DNDEBUG=1 -DHAVE_DLOPEN=1' -OPTS="$OPTS -DSQLITE_THREADSAFE=1" -OPTS="$OPTS -DSQLITE_ENABLE_FTS3=1" -OPTS="$OPTS -DSQLITE_ENABLE_COLUMN_METADATA=1" -for i in *.c; do - if test $i != 'keywordhash.c'; then - CMD="cc -fPIC $OPTS -O2 -I. -I$TCLDIR -c $i" - echo $CMD - $CMD - fi -done -echo gcc -shared *.o $TCLSTUBLIB -o tclsqlite3.so -gcc -shared *.o $TCLSTUBLIB -o tclsqlite3.so -strip tclsqlite3.so -rm tclsqlite.c tclsqlite.o -echo gcc -shared *.o -o sqlite3.so -gcc -shared *.o -o sqlite3.so -strip sqlite3.so -cd .. diff --git a/mptest/mptest.c b/mptest/mptest.c index 5022b009e6..25a1bcc821 100644 --- a/mptest/mptest.c +++ b/mptest/mptest.c @@ -1037,6 +1037,10 @@ static void runScript( for(k=(int)strlen(zFilename)-1; k>=0 && !isDirSep(zFilename[k]); k--){} if( k>0 ){ zNewFile = zToDel = sqlite3_mprintf("%.*s/%s", k,zFilename,zNewFile); + if( zNewFile==0 ){ + fprintf(stderr, "out of memory\n"); + abort(); + } } } zNewScript = readFile(zNewFile); diff --git a/spec.template b/spec.template deleted file mode 100644 index 6cc7ab2eab..0000000000 --- a/spec.template +++ /dev/null @@ -1,67 +0,0 @@ -%define name sqlite -%define version SQLITE_VERSION -%define release 1 - -Name: %{name} -Summary: SQLite is a C library that implements an embeddable SQL database engine -Version: %{version} -Release: %{release} -Source: %{name}-%{version}.tar.gz -Group: System/Libraries -URL: http://www.sqlite.org/ -License: Public Domain -BuildRoot: %{_tmppath}/%{name}-%{version}-root - -%description -SQLite is a software library that implements a self-contained, serverless, -zero-configuration, transactional SQL database engine. -Programs that link with the SQLite library can have SQL database access -without running a separate RDBMS process. The distribution comes with a -standalone command-line access program (sqlite) that can be used to -administer an SQLite database and which serves as an example of how to -use the SQLite library. - -%package -n %{name}-devel -Summary: Header files and libraries for developing apps which will use sqlite -Group: Development/C -Requires: %{name} = %{version}-%{release} - -%description -n %{name}-devel -The sqlite-devel package contains the header files and libraries needed -to develop programs that use the SQLite database library. - -%prep -%setup -q -n %{name} - -%build -CFLAGS="%optflags -DNDEBUG=1" CXXFLAGS="%optflags -DNDEBUG=1" ./configure --prefix=%{_prefix} - -make -make doc - -%install -install -d $RPM_BUILD_ROOT/%{_prefix} -install -d $RPM_BUILD_ROOT/%{_prefix}/bin -install -d $RPM_BUILD_ROOT/%{_prefix}/include -install -d $RPM_BUILD_ROOT/%{_prefix}/lib -make install prefix=$RPM_BUILD_ROOT/%{_prefix} - -%post -p /sbin/ldconfig - -%postun -p /sbin/ldconfig - -%clean -rm -fr $RPM_BUILD_ROOT - -%files -%defattr(-, root, root) -%{_libdir}/*.so* -%{_bindir}/* - -%files -n %{name}-devel -%defattr(-, root, root) -%{_libdir}/pkgconfig/sqlite3.pc -%{_libdir}/*.a -%{_libdir}/*.la -%{_includedir}/* -%doc doc/* diff --git a/sqlcipher-resources/PrivacyInfo.xcprivacy b/sqlcipher-resources/PrivacyInfo.xcprivacy new file mode 100644 index 0000000000..60b27a68ca --- /dev/null +++ b/sqlcipher-resources/PrivacyInfo.xcprivacy @@ -0,0 +1,32 @@ + + + + + NSPrivacyCollectedDataTypes + + NSPrivacyAccessedAPITypes + + + NSPrivacyAccessedAPIType + NSPrivacyAccessedAPICategoryDiskSpace + NSPrivacyAccessedAPITypeReasons + + E174.1 + + + + NSPrivacyAccessedAPIType + NSPrivacyAccessedAPICategoryFileTimestamp + NSPrivacyAccessedAPITypeReasons + + C617.1 + 3B52.1 + + + + NSPrivacyTrackingDomains + + NSPrivacyTracking + + + diff --git a/sqlcipher-1.1.8-testkey.db b/sqlcipher-resources/sqlcipher-1.1.8-testkey.db similarity index 100% rename from sqlcipher-1.1.8-testkey.db rename to sqlcipher-resources/sqlcipher-1.1.8-testkey.db diff --git a/sqlcipher-2.0-be-testkey.db b/sqlcipher-resources/sqlcipher-2.0-be-testkey.db similarity index 100% rename from sqlcipher-2.0-be-testkey.db rename to sqlcipher-resources/sqlcipher-2.0-be-testkey.db diff --git a/sqlcipher-2.0-beta-testkey.db b/sqlcipher-resources/sqlcipher-2.0-beta-testkey.db similarity index 100% rename from sqlcipher-2.0-beta-testkey.db rename to sqlcipher-resources/sqlcipher-2.0-beta-testkey.db diff --git a/sqlcipher-2.0-le-testkey.db b/sqlcipher-resources/sqlcipher-2.0-le-testkey.db similarity index 100% rename from sqlcipher-2.0-le-testkey.db rename to sqlcipher-resources/sqlcipher-2.0-le-testkey.db diff --git a/sqlcipher-3.0-testkey.db b/sqlcipher-resources/sqlcipher-3.0-testkey.db similarity index 100% rename from sqlcipher-3.0-testkey.db rename to sqlcipher-resources/sqlcipher-3.0-testkey.db diff --git a/sqlcipher-resources/sqlcipher-4.0-testkey.db b/sqlcipher-resources/sqlcipher-4.0-testkey.db new file mode 100644 index 0000000000..b8db5378cb Binary files /dev/null and b/sqlcipher-resources/sqlcipher-4.0-testkey.db differ diff --git a/sqlcipher.1 b/sqlcipher.1 index 6a08486033..c9a310057d 100644 --- a/sqlcipher.1 +++ b/sqlcipher.1 @@ -2,7 +2,7 @@ .\" First parameter, NAME, should be all caps .\" Second parameter, SECTION, should be 1-8, maybe w/ subsection .\" other parameters are allowed: see man(7), man(1) -.TH SQLCIPHER 1 "Fri Oct 31 10:41:31 EDT 2014" +.TH SQLCIPHER 1 "Fri Aug 11 23:50:12 CET 2023" .\" Please adjust this date whenever revising the manpage. .\" .\" Some roff macros, for reference: @@ -17,7 +17,7 @@ .\" for manpage-specific macros, see man(7) .SH NAME .B sqlcipher -\- A command line interface for SQLCipher version 2 +\- A command line interface for SQLCipher version 3 .SH SYNOPSIS .B sqlcipher @@ -49,9 +49,9 @@ a table named "memos" and insert a couple of records into that table: $ .B sqlcipher mydata.db .br -SQLite version 3.8.8 +SQLCipher version 3.43.0 2023-08-11 17:45:23 .br -Enter ".help" for instructions +Enter ".help" for usage hints. .br sqlite> .B create table memos(text, priority INTEGER); @@ -108,141 +108,13 @@ sqlite> .B .help .nf .tr %. -%backup ?DB? FILE Backup DB (default "main") to FILE -%bail on|off Stop after hitting an error. Default OFF -%clone NEWDB Clone data into NEWDB from the existing database -%databases List names and files of attached databases -%dump ?TABLE? ... Dump the database in an SQL text format - If TABLE specified, only dump tables matching - LIKE pattern TABLE. -%echo on|off Turn command echo on or off -%eqp on|off Enable or disable automatic EXPLAIN QUERY PLAN -%exit Exit this program -%explain ?on|off? Turn output mode suitable for EXPLAIN on or off. - With no args, it turns EXPLAIN on. -%fullschema Show schema and the content of sqlite_stat tables -%headers on|off Turn display of headers on or off -%help Show this message -%import FILE TABLE Import data from FILE into TABLE -%indices ?TABLE? Show names of all indices - If TABLE specified, only show indices for tables - matching LIKE pattern TABLE. -%load FILE ?ENTRY? Load an extension library -%log FILE|off Turn logging on or off. FILE can be stderr/stdout -%mode MODE ?TABLE? Set output mode where MODE is one of: - csv Comma-separated values - column Left-aligned columns. (See .width) - html HTML code - insert SQL insert statements for TABLE - line One value per line - list Values delimited by .separator string - tabs Tab-separated values - tcl TCL list elements -%nullvalue STRING Use STRING in place of NULL values -%once FILENAME Output for the next SQL command only to FILENAME -%open ?FILENAME? Close existing database and reopen FILENAME -%output ?FILENAME? Send output to FILENAME or stdout -%print STRING... Print literal STRING -%prompt MAIN CONTINUE Replace the standard prompts -%quit Exit this program -%read FILENAME Execute SQL in FILENAME -%restore ?DB? FILE Restore content of DB (default "main") from FILE -%save FILE Write in-memory database into FILE -%schema ?TABLE? Show the CREATE statements - If TABLE specified, only show tables matching - LIKE pattern TABLE. -%separator STRING ?NL? Change separator used by output mode and .import - NL is the end-of-line mark for CSV -%shell CMD ARGS... Run CMD ARGS... in a system shell -%show Show the current values for various settings -%stats on|off Turn stats on or off -%system CMD ARGS... Run CMD ARGS... in a system shell -%tables ?TABLE? List names of tables - If TABLE specified, only list tables matching - LIKE pattern TABLE. -%timeout MS Try opening locked tables for MS milliseconds -%timer on|off Turn SQL timer on or off -%trace FILE|off Output each SQL statement as it is run -%vfsname ?AUX? Print the name of the VFS stack -%width NUM1 NUM2 ... Set column widths for "column" mode - Negative values right-justify -sqlite> +... .sp .fi -.SH OPTIONS -.B sqlcipher -has the following options: -.TP -.B \-bail -Stop after hitting an error. -.TP -.B \-batch -Force batch I/O. -.TP -.B \-column -Query results will be displayed in a table like form, using -whitespace characters to separate the columns and align the -output. -.TP -.BI \-cmd\ command -run -.I command -before reading stdin -.TP -.B \-csv -Set output mode to CSV (comma separated values). -.TP -.B \-echo -Print commands before execution. -.TP -.BI \-init\ file -Read and execute commands from -.I file -, which can contain a mix of SQL statements and meta-commands. -.TP -.B \-[no]header -Turn headers on or off. -.TP -.B \-help -Show help on options and exit. -.TP -.B \-html -Query results will be output as simple HTML tables. -.TP -.B \-interactive -Force interactive I/O. -.TP -.B \-line -Query results will be displayed with one value per line, rows -separated by a blank line. Designed to be easily parsed by -scripts or other programs -.TP -.B \-list -Query results will be displayed with the separator (|, by default) -character between each field value. The default. -.TP -.BI \-mmap\ N -Set default mmap size to -.I N -\. -.TP -.BI \-nullvalue\ string -Set string used to represent NULL values. Default is '' -(empty string). -.TP -.BI \-separator\ separator -Set output field separator. Default is '|'. -.TP -.B \-stats -Print memory stats before each finalize. -.TP -.B \-version -Show SQLite version. -.TP -.BI \-vfs\ name -Use -.I name -as the default VFS. + +The available commands differ by version and build options, so they +are not listed here. Please refer to your local copy for all available +options. .SH INIT FILE @@ -265,22 +137,24 @@ continue prompt = " ...> " .sp .fi -o If the file +o If the file +.B ${XDG_CONFIG_HOME}/sqlcipher/sqliterc +or .B ~/.sqliterc -exists, it is processed first. -can be found in the user's home directory, it is -read and processed. It should generally only contain meta-commands. +exists, the first of those to be found is processed during startup. +It should generally only contain meta-commands. o If the -init option is present, the specified file is processed. o All other command line options are processed. .SH SEE ALSO -https://www.zetetic.net/sqlcipher +https://zetetic.net/sqlcipher + .br -The sqlite3-doc package. +The sqlcipher-doc package. .SH AUTHOR This manual page was originally written by Andreas Rottmann , for the Debian GNU/Linux system (but may be used -by others). It was subsequently revised by Bill Bumgarner and -further updated by Laszlo Boszormenyi . +by others). It was subsequently revised by Bill Bumgarner , +Laszlo Boszormenyi , and the sqlcipher developers. diff --git a/sqlcipher.xcodeproj/project.pbxproj b/sqlcipher.xcodeproj/project.pbxproj deleted file mode 100644 index b15edccc5b..0000000000 --- a/sqlcipher.xcodeproj/project.pbxproj +++ /dev/null @@ -1,1312 +0,0 @@ -// !$*UTF8*$! -{ - archiveVersion = 1; - classes = { - }; - objectVersion = 46; - objects = { - -/* Begin PBXAggregateTarget section */ - 9069D08B0FCE185A0042E34C /* amalgamation */ = { - isa = PBXAggregateTarget; - buildConfigurationList = 9069D0900FCE18970042E34C /* Build configuration list for PBXAggregateTarget "amalgamation" */; - buildPhases = ( - 9069D08A0FCE185A0042E34C /* ShellScript */, - ); - dependencies = ( - ); - name = amalgamation; - productName = amalgamation; - }; -/* End PBXAggregateTarget section */ - -/* Begin PBXBuildFile section */ - 4C0041A61BFC3A5000ED2AD5 /* Security.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4C0041A51BFC3A5000ED2AD5 /* Security.framework */; }; - 4C0041AB1BFC3A7E00ED2AD5 /* SQLCipher.h in Headers */ = {isa = PBXBuildFile; fileRef = 4C0041A91BFC3A7E00ED2AD5 /* SQLCipher.h */; settings = {ATTRIBUTES = (Public, ); }; }; - 4C0041BC1BFC3C7500ED2AD5 /* Security.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4C0041BB1BFC3C7500ED2AD5 /* Security.framework */; }; - 4C0041BD1BFC3C8300ED2AD5 /* SQLCipher.h in Headers */ = {isa = PBXBuildFile; fileRef = 4C0041A91BFC3A7E00ED2AD5 /* SQLCipher.h */; settings = {ATTRIBUTES = (Public, ); }; }; - 4C0041CE1BFC3D3900ED2AD5 /* Security.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4C0041CD1BFC3D3900ED2AD5 /* Security.framework */; }; - 4C0041CF1BFC3D4000ED2AD5 /* SQLCipher.h in Headers */ = {isa = PBXBuildFile; fileRef = 4C0041A91BFC3A7E00ED2AD5 /* SQLCipher.h */; settings = {ATTRIBUTES = (Public, ); }; }; - 4C0041E11BFC3F0800ED2AD5 /* Security.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4C0041E01BFC3F0800ED2AD5 /* Security.framework */; }; - 4C0041E21BFC3F5A00ED2AD5 /* SQLCipher.h in Headers */ = {isa = PBXBuildFile; fileRef = 4C0041A91BFC3A7E00ED2AD5 /* SQLCipher.h */; settings = {ATTRIBUTES = (Public, ); }; }; - 4C0041FA1BFC46A600ED2AD5 /* sqlite3.h in Headers */ = {isa = PBXBuildFile; fileRef = 4C0041F91BFC46A600ED2AD5 /* sqlite3.h */; settings = {ATTRIBUTES = (Public, ); }; }; - 4C0041FB1BFC46A600ED2AD5 /* sqlite3.h in Headers */ = {isa = PBXBuildFile; fileRef = 4C0041F91BFC46A600ED2AD5 /* sqlite3.h */; settings = {ATTRIBUTES = (Public, ); }; }; - 4C0041FC1BFC46A600ED2AD5 /* sqlite3.h in Headers */ = {isa = PBXBuildFile; fileRef = 4C0041F91BFC46A600ED2AD5 /* sqlite3.h */; settings = {ATTRIBUTES = (Public, ); }; }; - 4C0041FD1BFC46A600ED2AD5 /* sqlite3.h in Headers */ = {isa = PBXBuildFile; fileRef = 4C0041F91BFC46A600ED2AD5 /* sqlite3.h */; settings = {ATTRIBUTES = (Public, ); }; }; - 4C0041FE1BFC474700ED2AD5 /* sqlite3.c in Sources */ = {isa = PBXBuildFile; fileRef = 9069D0A20FCE1A4D0042E34C /* sqlite3.c */; }; - 4C0041FF1BFC474700ED2AD5 /* sqlite3.c in Sources */ = {isa = PBXBuildFile; fileRef = 9069D0A20FCE1A4D0042E34C /* sqlite3.c */; }; - 4C0042001BFC474700ED2AD5 /* sqlite3.c in Sources */ = {isa = PBXBuildFile; fileRef = 9069D0A20FCE1A4D0042E34C /* sqlite3.c */; }; - 4C0042011BFC474800ED2AD5 /* sqlite3.c in Sources */ = {isa = PBXBuildFile; fileRef = 9069D0A20FCE1A4D0042E34C /* sqlite3.c */; }; - 9069D0A30FCE1A4D0042E34C /* sqlite3.c in Sources */ = {isa = PBXBuildFile; fileRef = 9069D0A20FCE1A4D0042E34C /* sqlite3.c */; }; -/* End PBXBuildFile section */ - -/* Begin PBXContainerItemProxy section */ - 289BE0E7180C4930003E52DA /* PBXContainerItemProxy */ = { - isa = PBXContainerItemProxy; - containerPortal = 08FB7793FE84155DC02AAC07 /* Project object */; - proxyType = 1; - remoteGlobalIDString = 9069D08B0FCE185A0042E34C; - remoteInfo = amalgamation; - }; - 4C0041A11BFC392D00ED2AD5 /* PBXContainerItemProxy */ = { - isa = PBXContainerItemProxy; - containerPortal = 08FB7793FE84155DC02AAC07 /* Project object */; - proxyType = 1; - remoteGlobalIDString = 9069D08B0FCE185A0042E34C; - remoteInfo = amalgamation; - }; - 4C0041B91BFC3C6E00ED2AD5 /* PBXContainerItemProxy */ = { - isa = PBXContainerItemProxy; - containerPortal = 08FB7793FE84155DC02AAC07 /* Project object */; - proxyType = 1; - remoteGlobalIDString = 9069D08B0FCE185A0042E34C; - remoteInfo = amalgamation; - }; - 4C0041CB1BFC3D3000ED2AD5 /* PBXContainerItemProxy */ = { - isa = PBXContainerItemProxy; - containerPortal = 08FB7793FE84155DC02AAC07 /* Project object */; - proxyType = 1; - remoteGlobalIDString = 9069D08B0FCE185A0042E34C; - remoteInfo = amalgamation; - }; - 4C0041DE1BFC3F0100ED2AD5 /* PBXContainerItemProxy */ = { - isa = PBXContainerItemProxy; - containerPortal = 08FB7793FE84155DC02AAC07 /* Project object */; - proxyType = 1; - remoteGlobalIDString = 9069D08B0FCE185A0042E34C; - remoteInfo = amalgamation; - }; -/* End PBXContainerItemProxy section */ - -/* Begin PBXFileReference section */ - 4C0041981BFC382400ED2AD5 /* SQLCipher.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SQLCipher.framework; sourceTree = BUILT_PRODUCTS_DIR; }; - 4C0041A51BFC3A5000ED2AD5 /* Security.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Security.framework; path = System/Library/Frameworks/Security.framework; sourceTree = SDKROOT; }; - 4C0041A81BFC3A7E00ED2AD5 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = Info.plist; path = macosx/Info.plist; sourceTree = ""; }; - 4C0041A91BFC3A7E00ED2AD5 /* SQLCipher.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SQLCipher.h; path = macosx/SQLCipher.h; sourceTree = ""; }; - 4C0041B11BFC3B2200ED2AD5 /* SQLCipher.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SQLCipher.framework; sourceTree = BUILT_PRODUCTS_DIR; }; - 4C0041BB1BFC3C7500ED2AD5 /* Security.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Security.framework; path = Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks/Security.framework; sourceTree = DEVELOPER_DIR; }; - 4C0041C31BFC3CC000ED2AD5 /* SQLCipher.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SQLCipher.framework; sourceTree = BUILT_PRODUCTS_DIR; }; - 4C0041CD1BFC3D3900ED2AD5 /* Security.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Security.framework; path = Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS9.0.sdk/System/Library/Frameworks/Security.framework; sourceTree = DEVELOPER_DIR; }; - 4C0041D01BFC3D9A00ED2AD5 /* Info-tvOS.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; name = "Info-tvOS.plist"; path = "macosx/Info-tvOS.plist"; sourceTree = ""; }; - 4C0041D61BFC3E2500ED2AD5 /* SQLCipher.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SQLCipher.framework; sourceTree = BUILT_PRODUCTS_DIR; }; - 4C0041E01BFC3F0800ED2AD5 /* Security.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Security.framework; path = Platforms/WatchOS.platform/Developer/SDKs/WatchOS2.0.sdk/System/Library/Frameworks/Security.framework; sourceTree = DEVELOPER_DIR; }; - 4C0041F91BFC46A600ED2AD5 /* sqlite3.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = sqlite3.h; sourceTree = ""; }; - 9069D0A20FCE1A4D0042E34C /* sqlite3.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = sqlite3.c; sourceTree = ""; }; - D2AAC046055464E500DB518D /* libsqlcipher.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libsqlcipher.a; sourceTree = BUILT_PRODUCTS_DIR; }; -/* End PBXFileReference section */ - -/* Begin PBXFrameworksBuildPhase section */ - 4C0041941BFC382400ED2AD5 /* Frameworks */ = { - isa = PBXFrameworksBuildPhase; - buildActionMask = 2147483647; - files = ( - ); - runOnlyForDeploymentPostprocessing = 0; - }; - 4C0041AD1BFC3B2200ED2AD5 /* Frameworks */ = { - isa = PBXFrameworksBuildPhase; - buildActionMask = 2147483647; - files = ( - 4C0041BC1BFC3C7500ED2AD5 /* Security.framework in Frameworks */, - ); - runOnlyForDeploymentPostprocessing = 0; - }; - 4C0041BF1BFC3CC000ED2AD5 /* Frameworks */ = { - isa = PBXFrameworksBuildPhase; - buildActionMask = 2147483647; - files = ( - 4C0041CE1BFC3D3900ED2AD5 /* Security.framework in Frameworks */, - ); - runOnlyForDeploymentPostprocessing = 0; - }; - 4C0041D21BFC3E2500ED2AD5 /* Frameworks */ = { - isa = PBXFrameworksBuildPhase; - buildActionMask = 2147483647; - files = ( - 4C0041E11BFC3F0800ED2AD5 /* Security.framework in Frameworks */, - ); - runOnlyForDeploymentPostprocessing = 0; - }; - D289987405E68DCB004EDB86 /* Frameworks */ = { - isa = PBXFrameworksBuildPhase; - buildActionMask = 2147483647; - files = ( - 4C0041A61BFC3A5000ED2AD5 /* Security.framework in Frameworks */, - ); - runOnlyForDeploymentPostprocessing = 0; - }; -/* End PBXFrameworksBuildPhase section */ - -/* Begin PBXGroup section */ - 08FB7794FE84155DC02AAC07 /* sqlcipher */ = { - isa = PBXGroup; - children = ( - 08FB7795FE84155DC02AAC07 /* Source */, - 4C0041A41BFC3A3800ED2AD5 /* Supporting Files */, - 4C0041A71BFC3A5400ED2AD5 /* Frameworks */, - 1AB674ADFE9D54B511CA2CBB /* Products */, - ); - name = sqlcipher; - sourceTree = ""; - }; - 08FB7795FE84155DC02AAC07 /* Source */ = { - isa = PBXGroup; - children = ( - 9069D0A20FCE1A4D0042E34C /* sqlite3.c */, - 4C0041F91BFC46A600ED2AD5 /* sqlite3.h */, - ); - name = Source; - sourceTree = ""; - }; - 1AB674ADFE9D54B511CA2CBB /* Products */ = { - isa = PBXGroup; - children = ( - D2AAC046055464E500DB518D /* libsqlcipher.a */, - 4C0041981BFC382400ED2AD5 /* SQLCipher.framework */, - 4C0041B11BFC3B2200ED2AD5 /* SQLCipher.framework */, - 4C0041C31BFC3CC000ED2AD5 /* SQLCipher.framework */, - 4C0041D61BFC3E2500ED2AD5 /* SQLCipher.framework */, - ); - name = Products; - sourceTree = ""; - }; - 4C0041A41BFC3A3800ED2AD5 /* Supporting Files */ = { - isa = PBXGroup; - children = ( - 4C0041A91BFC3A7E00ED2AD5 /* SQLCipher.h */, - 4C0041A81BFC3A7E00ED2AD5 /* Info.plist */, - 4C0041D01BFC3D9A00ED2AD5 /* Info-tvOS.plist */, - ); - name = "Supporting Files"; - sourceTree = ""; - }; - 4C0041A71BFC3A5400ED2AD5 /* Frameworks */ = { - isa = PBXGroup; - children = ( - 4C0041E01BFC3F0800ED2AD5 /* Security.framework */, - 4C0041CD1BFC3D3900ED2AD5 /* Security.framework */, - 4C0041BB1BFC3C7500ED2AD5 /* Security.framework */, - 4C0041A51BFC3A5000ED2AD5 /* Security.framework */, - ); - name = Frameworks; - sourceTree = ""; - }; -/* End PBXGroup section */ - -/* Begin PBXHeadersBuildPhase section */ - 4C0041951BFC382400ED2AD5 /* Headers */ = { - isa = PBXHeadersBuildPhase; - buildActionMask = 2147483647; - files = ( - 4C0041FA1BFC46A600ED2AD5 /* sqlite3.h in Headers */, - 4C0041AB1BFC3A7E00ED2AD5 /* SQLCipher.h in Headers */, - ); - runOnlyForDeploymentPostprocessing = 0; - }; - 4C0041AE1BFC3B2200ED2AD5 /* Headers */ = { - isa = PBXHeadersBuildPhase; - buildActionMask = 2147483647; - files = ( - 4C0041FB1BFC46A600ED2AD5 /* sqlite3.h in Headers */, - 4C0041BD1BFC3C8300ED2AD5 /* SQLCipher.h in Headers */, - ); - runOnlyForDeploymentPostprocessing = 0; - }; - 4C0041C01BFC3CC000ED2AD5 /* Headers */ = { - isa = PBXHeadersBuildPhase; - buildActionMask = 2147483647; - files = ( - 4C0041FC1BFC46A600ED2AD5 /* sqlite3.h in Headers */, - 4C0041CF1BFC3D4000ED2AD5 /* SQLCipher.h in Headers */, - ); - runOnlyForDeploymentPostprocessing = 0; - }; - 4C0041D31BFC3E2500ED2AD5 /* Headers */ = { - isa = PBXHeadersBuildPhase; - buildActionMask = 2147483647; - files = ( - 4C0041FD1BFC46A600ED2AD5 /* sqlite3.h in Headers */, - 4C0041E21BFC3F5A00ED2AD5 /* SQLCipher.h in Headers */, - ); - runOnlyForDeploymentPostprocessing = 0; - }; - D2AAC043055464E500DB518D /* Headers */ = { - isa = PBXHeadersBuildPhase; - buildActionMask = 2147483647; - files = ( - ); - runOnlyForDeploymentPostprocessing = 0; - }; -/* End PBXHeadersBuildPhase section */ - -/* Begin PBXNativeTarget section */ - 4C0041971BFC382400ED2AD5 /* SQLCipher iOS */ = { - isa = PBXNativeTarget; - buildConfigurationList = 4C00419D1BFC382500ED2AD5 /* Build configuration list for PBXNativeTarget "SQLCipher iOS" */; - buildPhases = ( - 4C0041931BFC382400ED2AD5 /* Sources */, - 4C0041941BFC382400ED2AD5 /* Frameworks */, - 4C0041951BFC382400ED2AD5 /* Headers */, - 4C0041961BFC382400ED2AD5 /* Resources */, - ); - buildRules = ( - ); - dependencies = ( - 4C0041A21BFC392D00ED2AD5 /* PBXTargetDependency */, - ); - name = "SQLCipher iOS"; - productName = SQLCipher; - productReference = 4C0041981BFC382400ED2AD5 /* SQLCipher.framework */; - productType = "com.apple.product-type.framework"; - }; - 4C0041B01BFC3B2200ED2AD5 /* SQLCipher OSX */ = { - isa = PBXNativeTarget; - buildConfigurationList = 4C0041B61BFC3B2200ED2AD5 /* Build configuration list for PBXNativeTarget "SQLCipher OSX" */; - buildPhases = ( - 4C0041AC1BFC3B2200ED2AD5 /* Sources */, - 4C0041AD1BFC3B2200ED2AD5 /* Frameworks */, - 4C0041AE1BFC3B2200ED2AD5 /* Headers */, - 4C0041AF1BFC3B2200ED2AD5 /* Resources */, - ); - buildRules = ( - ); - dependencies = ( - 4C0041BA1BFC3C6E00ED2AD5 /* PBXTargetDependency */, - ); - name = "SQLCipher OSX"; - productName = "SQLCipher OSX"; - productReference = 4C0041B11BFC3B2200ED2AD5 /* SQLCipher.framework */; - productType = "com.apple.product-type.framework"; - }; - 4C0041C21BFC3CC000ED2AD5 /* SQLCipher tvOS */ = { - isa = PBXNativeTarget; - buildConfigurationList = 4C0041C81BFC3CC000ED2AD5 /* Build configuration list for PBXNativeTarget "SQLCipher tvOS" */; - buildPhases = ( - 4C0041BE1BFC3CC000ED2AD5 /* Sources */, - 4C0041BF1BFC3CC000ED2AD5 /* Frameworks */, - 4C0041C01BFC3CC000ED2AD5 /* Headers */, - 4C0041C11BFC3CC000ED2AD5 /* Resources */, - ); - buildRules = ( - ); - dependencies = ( - 4C0041CC1BFC3D3000ED2AD5 /* PBXTargetDependency */, - ); - name = "SQLCipher tvOS"; - productName = "SQLCipher tvOS"; - productReference = 4C0041C31BFC3CC000ED2AD5 /* SQLCipher.framework */; - productType = "com.apple.product-type.framework"; - }; - 4C0041D51BFC3E2500ED2AD5 /* SQLCipher watchOS */ = { - isa = PBXNativeTarget; - buildConfigurationList = 4C0041DB1BFC3E2600ED2AD5 /* Build configuration list for PBXNativeTarget "SQLCipher watchOS" */; - buildPhases = ( - 4C0041D11BFC3E2500ED2AD5 /* Sources */, - 4C0041D21BFC3E2500ED2AD5 /* Frameworks */, - 4C0041D31BFC3E2500ED2AD5 /* Headers */, - 4C0041D41BFC3E2500ED2AD5 /* Resources */, - ); - buildRules = ( - ); - dependencies = ( - 4C0041DF1BFC3F0100ED2AD5 /* PBXTargetDependency */, - ); - name = "SQLCipher watchOS"; - productName = SQLCipher; - productReference = 4C0041D61BFC3E2500ED2AD5 /* SQLCipher.framework */; - productType = "com.apple.product-type.framework"; - }; - D2AAC045055464E500DB518D /* sqlcipher */ = { - isa = PBXNativeTarget; - buildConfigurationList = 1DEB91EB08733DB70010E9CD /* Build configuration list for PBXNativeTarget "sqlcipher" */; - buildPhases = ( - D2AAC043055464E500DB518D /* Headers */, - D2AAC044055464E500DB518D /* Sources */, - D289987405E68DCB004EDB86 /* Frameworks */, - ); - buildRules = ( - ); - dependencies = ( - 289BE0E8180C4930003E52DA /* PBXTargetDependency */, - ); - name = sqlcipher; - productName = sqlcipher; - productReference = D2AAC046055464E500DB518D /* libsqlcipher.a */; - productType = "com.apple.product-type.library.static"; - }; -/* End PBXNativeTarget section */ - -/* Begin PBXProject section */ - 08FB7793FE84155DC02AAC07 /* Project object */ = { - isa = PBXProject; - attributes = { - LastUpgradeCheck = 0710; - TargetAttributes = { - 4C0041971BFC382400ED2AD5 = { - CreatedOnToolsVersion = 7.1; - }; - 4C0041B01BFC3B2200ED2AD5 = { - CreatedOnToolsVersion = 7.1; - }; - 4C0041C21BFC3CC000ED2AD5 = { - CreatedOnToolsVersion = 7.1; - }; - 4C0041D51BFC3E2500ED2AD5 = { - CreatedOnToolsVersion = 7.1; - }; - }; - }; - buildConfigurationList = 1DEB91EF08733DB70010E9CD /* Build configuration list for PBXProject "sqlcipher" */; - compatibilityVersion = "Xcode 3.2"; - developmentRegion = English; - hasScannedForEncodings = 1; - knownRegions = ( - English, - Japanese, - French, - German, - ); - mainGroup = 08FB7794FE84155DC02AAC07 /* sqlcipher */; - projectDirPath = ""; - projectRoot = ""; - targets = ( - 4C0041971BFC382400ED2AD5 /* SQLCipher iOS */, - 4C0041B01BFC3B2200ED2AD5 /* SQLCipher OSX */, - 4C0041C21BFC3CC000ED2AD5 /* SQLCipher tvOS */, - 4C0041D51BFC3E2500ED2AD5 /* SQLCipher watchOS */, - D2AAC045055464E500DB518D /* sqlcipher */, - 9069D08B0FCE185A0042E34C /* amalgamation */, - ); - }; -/* End PBXProject section */ - -/* Begin PBXResourcesBuildPhase section */ - 4C0041961BFC382400ED2AD5 /* Resources */ = { - isa = PBXResourcesBuildPhase; - buildActionMask = 2147483647; - files = ( - ); - runOnlyForDeploymentPostprocessing = 0; - }; - 4C0041AF1BFC3B2200ED2AD5 /* Resources */ = { - isa = PBXResourcesBuildPhase; - buildActionMask = 2147483647; - files = ( - ); - runOnlyForDeploymentPostprocessing = 0; - }; - 4C0041C11BFC3CC000ED2AD5 /* Resources */ = { - isa = PBXResourcesBuildPhase; - buildActionMask = 2147483647; - files = ( - ); - runOnlyForDeploymentPostprocessing = 0; - }; - 4C0041D41BFC3E2500ED2AD5 /* Resources */ = { - isa = PBXResourcesBuildPhase; - buildActionMask = 2147483647; - files = ( - ); - runOnlyForDeploymentPostprocessing = 0; - }; -/* End PBXResourcesBuildPhase section */ - -/* Begin PBXShellScriptBuildPhase section */ - 9069D08A0FCE185A0042E34C /* ShellScript */ = { - isa = PBXShellScriptBuildPhase; - buildActionMask = 2147483647; - files = ( - ); - inputPaths = ( - ); - outputPaths = ( - "$(SRCROOT)/sqlite3.c", - ); - runOnlyForDeploymentPostprocessing = 0; - shellPath = /bin/sh; - shellScript = "./configure --enable-tempstore=yes --with-crypto-lib=commoncrypto CFLAGS=\"-DSQLITE_HAS_CODEC -DSQLITE_TEMP_STORE=2\"\nmake sqlite3.c\nexit 0"; - }; -/* End PBXShellScriptBuildPhase section */ - -/* Begin PBXSourcesBuildPhase section */ - 4C0041931BFC382400ED2AD5 /* Sources */ = { - isa = PBXSourcesBuildPhase; - buildActionMask = 2147483647; - files = ( - 4C0041FE1BFC474700ED2AD5 /* sqlite3.c in Sources */, - ); - runOnlyForDeploymentPostprocessing = 0; - }; - 4C0041AC1BFC3B2200ED2AD5 /* Sources */ = { - isa = PBXSourcesBuildPhase; - buildActionMask = 2147483647; - files = ( - 4C0041FF1BFC474700ED2AD5 /* sqlite3.c in Sources */, - ); - runOnlyForDeploymentPostprocessing = 0; - }; - 4C0041BE1BFC3CC000ED2AD5 /* Sources */ = { - isa = PBXSourcesBuildPhase; - buildActionMask = 2147483647; - files = ( - 4C0042001BFC474700ED2AD5 /* sqlite3.c in Sources */, - ); - runOnlyForDeploymentPostprocessing = 0; - }; - 4C0041D11BFC3E2500ED2AD5 /* Sources */ = { - isa = PBXSourcesBuildPhase; - buildActionMask = 2147483647; - files = ( - 4C0042011BFC474800ED2AD5 /* sqlite3.c in Sources */, - ); - runOnlyForDeploymentPostprocessing = 0; - }; - D2AAC044055464E500DB518D /* Sources */ = { - isa = PBXSourcesBuildPhase; - buildActionMask = 2147483647; - files = ( - 9069D0A30FCE1A4D0042E34C /* sqlite3.c in Sources */, - ); - runOnlyForDeploymentPostprocessing = 0; - }; -/* End PBXSourcesBuildPhase section */ - -/* Begin PBXTargetDependency section */ - 289BE0E8180C4930003E52DA /* PBXTargetDependency */ = { - isa = PBXTargetDependency; - target = 9069D08B0FCE185A0042E34C /* amalgamation */; - targetProxy = 289BE0E7180C4930003E52DA /* PBXContainerItemProxy */; - }; - 4C0041A21BFC392D00ED2AD5 /* PBXTargetDependency */ = { - isa = PBXTargetDependency; - target = 9069D08B0FCE185A0042E34C /* amalgamation */; - targetProxy = 4C0041A11BFC392D00ED2AD5 /* PBXContainerItemProxy */; - }; - 4C0041BA1BFC3C6E00ED2AD5 /* PBXTargetDependency */ = { - isa = PBXTargetDependency; - target = 9069D08B0FCE185A0042E34C /* amalgamation */; - targetProxy = 4C0041B91BFC3C6E00ED2AD5 /* PBXContainerItemProxy */; - }; - 4C0041CC1BFC3D3000ED2AD5 /* PBXTargetDependency */ = { - isa = PBXTargetDependency; - target = 9069D08B0FCE185A0042E34C /* amalgamation */; - targetProxy = 4C0041CB1BFC3D3000ED2AD5 /* PBXContainerItemProxy */; - }; - 4C0041DF1BFC3F0100ED2AD5 /* PBXTargetDependency */ = { - isa = PBXTargetDependency; - target = 9069D08B0FCE185A0042E34C /* amalgamation */; - targetProxy = 4C0041DE1BFC3F0100ED2AD5 /* PBXContainerItemProxy */; - }; -/* End PBXTargetDependency section */ - -/* Begin XCBuildConfiguration section */ - 1DEB91EC08733DB70010E9CD /* Debug */ = { - isa = XCBuildConfiguration; - buildSettings = { - ALWAYS_SEARCH_USER_PATHS = NO; - COMBINE_HIDPI_IMAGES = YES; - COPY_PHASE_STRIP = NO; - GCC_DYNAMIC_NO_PIC = NO; - GCC_MODEL_TUNING = G5; - GCC_OPTIMIZATION_LEVEL = 0; - HEADER_SEARCH_PATHS = "$(PROJECT_DIR)"; - INSTALL_PATH = /usr/local/lib; - OTHER_CFLAGS = ( - "-DSQLITE_HAS_CODEC", - "-DSQLITE_TEMP_STORE=2", - "-DSQLITE_THREADSAFE", - "-DSQLCIPHER_CRYPTO_CC", - ); - "OTHER_CFLAGS[arch=armv6]" = ( - "-mno-thumb", - "-DSQLITE_HAS_CODEC", - "-DSQLITE_TEMP_STORE=2", - "-DSQLITE_THREADSAFE", - "-DSQLCIPHER_CRYPTO_CC", - ); - OTHER_LDFLAGS = ""; - PRODUCT_NAME = sqlcipher; - }; - name = Debug; - }; - 1DEB91ED08733DB70010E9CD /* Release */ = { - isa = XCBuildConfiguration; - buildSettings = { - ALWAYS_SEARCH_USER_PATHS = NO; - COMBINE_HIDPI_IMAGES = YES; - DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; - GCC_MODEL_TUNING = G5; - HEADER_SEARCH_PATHS = "$(PROJECT_DIR)"; - INSTALL_PATH = /usr/local/lib; - OTHER_CFLAGS = ( - "-DSQLITE_HAS_CODEC", - "-DNDEBUG", - "-DSQLITE_OS_UNIX=1", - "-DSQLITE_TEMP_STORE=2", - "-DSQLITE_THREADSAFE", - "-DSQLCIPHER_CRYPTO_CC", - ); - "OTHER_CFLAGS[arch=armv6]" = ( - "-mno-thumb", - "-DSQLITE_HAS_CODEC", - "-DNDEBUG", - "-DSQLITE_OS_UNIX=1", - "-DSQLITE_TEMP_STORE=2", - "-DSQLITE_THREADSAFE", - "-DSQLCIPHER_CRYPTO_CC", - ); - OTHER_LDFLAGS = ""; - PRODUCT_NAME = sqlcipher; - }; - name = Release; - }; - 1DEB91F008733DB70010E9CD /* Debug */ = { - isa = XCBuildConfiguration; - buildSettings = { - "ARCHS[sdk=iphoneos*]" = ( - "$(ARCHS_STANDARD)", - armv7s, - ); - "ARCHS[sdk=iphonesimulator*]" = ( - "$(ARCHS_STANDARD)", - armv7s, - ); - "ARCHS[sdk=macosx*]" = ( - x86_64, - i386, - ); - ENABLE_TESTABILITY = YES; - GCC_C_LANGUAGE_STANDARD = c99; - GCC_OPTIMIZATION_LEVEL = 0; - GCC_WARN_ABOUT_RETURN_TYPE = YES; - GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 4.3; - "IPHONEOS_DEPLOYMENT_TARGET[arch=arm64]" = 7.0; - ONLY_ACTIVE_ARCH = YES; - SDKROOT = iphoneos; - SKIP_INSTALL = YES; - SUPPORTED_PLATFORMS = "iphonesimulator macosx iphoneos"; - VALID_ARCHS = "arm64 armv7 armv7s x86_64 i386"; - }; - name = Debug; - }; - 1DEB91F108733DB70010E9CD /* Release */ = { - isa = XCBuildConfiguration; - buildSettings = { - "ARCHS[sdk=iphoneos*]" = ( - "$(ARCHS_STANDARD)", - armv7s, - ); - "ARCHS[sdk=iphonesimulator*]" = ( - "$(ARCHS_STANDARD)", - armv7s, - ); - "ARCHS[sdk=macosx*]" = ( - i386, - x86_64, - ); - GCC_C_LANGUAGE_STANDARD = c99; - GCC_WARN_ABOUT_RETURN_TYPE = YES; - GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 4.3; - "IPHONEOS_DEPLOYMENT_TARGET[arch=arm64]" = 7.0; - SDKROOT = iphoneos; - SKIP_INSTALL = YES; - SUPPORTED_PLATFORMS = "iphonesimulator macosx iphoneos"; - VALID_ARCHS = "arm64 armv7 armv7s x86_64 i386"; - }; - name = Release; - }; - 4C00419E1BFC382500ED2AD5 /* Debug */ = { - isa = XCBuildConfiguration; - buildSettings = { - ALWAYS_SEARCH_USER_PATHS = NO; - "ARCHS[sdk=iphonesimulator*]" = "$(ARCHS_STANDARD)"; - "ARCHS[sdk=macosx*]" = "$(ARCHS_STANDARD)"; - BITCODE_GENERATION_MODE = marker; - CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; - CLANG_CXX_LIBRARY = "libc++"; - CLANG_ENABLE_MODULES = YES; - CLANG_ENABLE_OBJC_ARC = YES; - CLANG_WARN_BOOL_CONVERSION = YES; - CLANG_WARN_CONSTANT_CONVERSION = YES; - CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; - CLANG_WARN_EMPTY_BODY = YES; - CLANG_WARN_ENUM_CONVERSION = YES; - CLANG_WARN_INT_CONVERSION = YES; - CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; - CLANG_WARN_UNREACHABLE_CODE = YES; - CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; - CODE_SIGN_IDENTITY = "iPhone Developer"; - "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; - COPY_PHASE_STRIP = NO; - CURRENT_PROJECT_VERSION = 1; - DEBUG_INFORMATION_FORMAT = dwarf; - DEFINES_MODULE = YES; - DYLIB_COMPATIBILITY_VERSION = 1; - DYLIB_CURRENT_VERSION = 1; - DYLIB_INSTALL_NAME_BASE = "@rpath"; - ENABLE_STRICT_OBJC_MSGSEND = YES; - ENABLE_TESTABILITY = YES; - GCC_C_LANGUAGE_STANDARD = gnu99; - GCC_DYNAMIC_NO_PIC = NO; - GCC_NO_COMMON_BLOCKS = YES; - GCC_PREPROCESSOR_DEFINITIONS = ( - "DEBUG=1", - "$(inherited)", - ); - GCC_WARN_64_TO_32_BIT_CONVERSION = YES; - GCC_WARN_ABOUT_DEPRECATED_FUNCTIONS = YES; - GCC_WARN_ABOUT_INVALID_OFFSETOF_MACRO = NO; - GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; - GCC_WARN_CHECK_SWITCH_STATEMENTS = YES; - GCC_WARN_MISSING_PARENTHESES = YES; - GCC_WARN_TYPECHECK_CALLS_TO_PRINTF = YES; - GCC_WARN_UNDECLARED_SELECTOR = YES; - GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; - GCC_WARN_UNUSED_FUNCTION = YES; - GCC_WARN_UNUSED_VALUE = YES; - GCC_WARN_UNUSED_VARIABLE = YES; - INFOPLIST_FILE = macosx/Info.plist; - INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; - IPHONEOS_DEPLOYMENT_TARGET = 8.0; - LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; - MACOSX_DEPLOYMENT_TARGET = 10.9; - MTL_ENABLE_DEBUG_INFO = YES; - OTHER_CFLAGS = ( - "-DSQLITE_HAS_CODEC", - "-DSQLITE_TEMP_STORE=2", - "-DSQLITE_THREADSAFE", - "-DSQLCIPHER_CRYPTO_CC", - "-Wno-ambiguous-macro", - "-Wno-#warnings", - "-Wno-conversion", - "-Wno-unused-const-variable", - "-Wno-unused-function", - "-Wno-unreachable-code", - ); - PRODUCT_BUNDLE_IDENTIFIER = net.zetetic.SQLCipher; - PRODUCT_NAME = SQLCipher; - SKIP_INSTALL = YES; - SUPPORTED_PLATFORMS = "iphonesimulator iphoneos"; - SWIFT_OPTIMIZATION_LEVEL = "-Onone"; - TARGETED_DEVICE_FAMILY = "1,2"; - TVOS_DEPLOYMENT_TARGET = 9.0; - VALID_ARCHS = "arm64 armv7 armv7s"; - VERSIONING_SYSTEM = "apple-generic"; - VERSION_INFO_PREFIX = ""; - WATCHOS_DEPLOYMENT_TARGET = 2.0; - }; - name = Debug; - }; - 4C00419F1BFC382500ED2AD5 /* Release */ = { - isa = XCBuildConfiguration; - buildSettings = { - ALWAYS_SEARCH_USER_PATHS = NO; - "ARCHS[sdk=iphoneos*]" = "$(ARCHS_STANDARD)"; - "ARCHS[sdk=iphonesimulator*]" = "$(ARCHS_STANDARD)"; - "ARCHS[sdk=macosx*]" = "$(ARCHS_STANDARD)"; - BITCODE_GENERATION_MODE = bitcode; - CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; - CLANG_CXX_LIBRARY = "libc++"; - CLANG_ENABLE_MODULES = YES; - CLANG_ENABLE_OBJC_ARC = YES; - CLANG_WARN_BOOL_CONVERSION = YES; - CLANG_WARN_CONSTANT_CONVERSION = YES; - CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; - CLANG_WARN_EMPTY_BODY = YES; - CLANG_WARN_ENUM_CONVERSION = YES; - CLANG_WARN_INT_CONVERSION = YES; - CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; - CLANG_WARN_UNREACHABLE_CODE = YES; - CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; - CODE_SIGN_IDENTITY = "iPhone Developer"; - "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; - COPY_PHASE_STRIP = NO; - CURRENT_PROJECT_VERSION = 1; - DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; - DEFINES_MODULE = YES; - DYLIB_COMPATIBILITY_VERSION = 1; - DYLIB_CURRENT_VERSION = 1; - DYLIB_INSTALL_NAME_BASE = "@rpath"; - ENABLE_NS_ASSERTIONS = NO; - ENABLE_STRICT_OBJC_MSGSEND = YES; - GCC_C_LANGUAGE_STANDARD = gnu99; - GCC_NO_COMMON_BLOCKS = YES; - GCC_WARN_64_TO_32_BIT_CONVERSION = YES; - GCC_WARN_ABOUT_DEPRECATED_FUNCTIONS = YES; - GCC_WARN_ABOUT_INVALID_OFFSETOF_MACRO = NO; - GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; - GCC_WARN_CHECK_SWITCH_STATEMENTS = YES; - GCC_WARN_MISSING_PARENTHESES = YES; - GCC_WARN_TYPECHECK_CALLS_TO_PRINTF = YES; - GCC_WARN_UNDECLARED_SELECTOR = YES; - GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; - GCC_WARN_UNUSED_FUNCTION = YES; - GCC_WARN_UNUSED_VALUE = YES; - GCC_WARN_UNUSED_VARIABLE = YES; - INFOPLIST_FILE = macosx/Info.plist; - INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; - IPHONEOS_DEPLOYMENT_TARGET = 8.0; - LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; - MACOSX_DEPLOYMENT_TARGET = 10.9; - MTL_ENABLE_DEBUG_INFO = NO; - OTHER_CFLAGS = ( - "-DSQLITE_HAS_CODEC", - "-DSQLITE_TEMP_STORE=2", - "-DSQLITE_THREADSAFE", - "-DSQLCIPHER_CRYPTO_CC", - "-Wno-ambiguous-macro", - "-Wno-#warnings", - "-Wno-conversion", - "-Wno-unused-const-variable", - "-Wno-unused-function", - "-Wno-unreachable-code", - ); - PRODUCT_BUNDLE_IDENTIFIER = net.zetetic.SQLCipher; - PRODUCT_NAME = SQLCipher; - SKIP_INSTALL = YES; - SUPPORTED_PLATFORMS = "iphonesimulator iphoneos"; - TARGETED_DEVICE_FAMILY = "1,2"; - TVOS_DEPLOYMENT_TARGET = 9.0; - VALIDATE_PRODUCT = YES; - VALID_ARCHS = "arm64 armv7 armv7s"; - VERSIONING_SYSTEM = "apple-generic"; - VERSION_INFO_PREFIX = ""; - WATCHOS_DEPLOYMENT_TARGET = 2.0; - }; - name = Release; - }; - 4C0041B71BFC3B2200ED2AD5 /* Debug */ = { - isa = XCBuildConfiguration; - buildSettings = { - ALWAYS_SEARCH_USER_PATHS = NO; - "ARCHS[sdk=iphoneos*]" = "$(ARCHS_STANDARD)"; - "ARCHS[sdk=iphonesimulator*]" = "$(ARCHS_STANDARD)"; - "ARCHS[sdk=macosx*]" = "$(ARCHS_STANDARD)"; - BITCODE_GENERATION_MODE = marker; - CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; - CLANG_CXX_LIBRARY = "libc++"; - CLANG_ENABLE_MODULES = YES; - CLANG_ENABLE_OBJC_ARC = YES; - CLANG_WARN_BOOL_CONVERSION = YES; - CLANG_WARN_CONSTANT_CONVERSION = YES; - CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; - CLANG_WARN_EMPTY_BODY = YES; - CLANG_WARN_ENUM_CONVERSION = YES; - CLANG_WARN_INT_CONVERSION = YES; - CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; - CLANG_WARN_UNREACHABLE_CODE = YES; - CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; - CODE_SIGN_IDENTITY = ""; - COMBINE_HIDPI_IMAGES = YES; - COPY_PHASE_STRIP = NO; - CURRENT_PROJECT_VERSION = 1; - DEBUG_INFORMATION_FORMAT = dwarf; - DEFINES_MODULE = YES; - DYLIB_COMPATIBILITY_VERSION = 1; - DYLIB_CURRENT_VERSION = 1; - DYLIB_INSTALL_NAME_BASE = "@rpath"; - ENABLE_STRICT_OBJC_MSGSEND = YES; - FRAMEWORK_VERSION = A; - GCC_C_LANGUAGE_STANDARD = gnu99; - GCC_DYNAMIC_NO_PIC = NO; - GCC_NO_COMMON_BLOCKS = YES; - GCC_PREPROCESSOR_DEFINITIONS = ( - "DEBUG=1", - "$(inherited)", - ); - GCC_WARN_64_TO_32_BIT_CONVERSION = YES; - GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; - GCC_WARN_UNDECLARED_SELECTOR = YES; - GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; - GCC_WARN_UNUSED_FUNCTION = YES; - INFOPLIST_FILE = macosx/Info.plist; - INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; - IPHONEOS_DEPLOYMENT_TARGET = 8.0; - LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/Frameworks"; - MACOSX_DEPLOYMENT_TARGET = 10.9; - MTL_ENABLE_DEBUG_INFO = YES; - OTHER_CFLAGS = ( - "-DSQLITE_HAS_CODEC", - "-DSQLITE_TEMP_STORE=2", - "-DSQLITE_THREADSAFE", - "-DSQLCIPHER_CRYPTO_CC", - "-Wno-ambiguous-macro", - "-Wno-#warnings", - "-Wno-conversion", - "-Wno-unused-const-variable", - "-Wno-unused-function", - "-Wno-unreachable-code", - ); - PRODUCT_BUNDLE_IDENTIFIER = net.zetetic.SQLCipher; - PRODUCT_NAME = SQLCipher; - SDKROOT = macosx; - SKIP_INSTALL = YES; - SUPPORTED_PLATFORMS = macosx; - SWIFT_OPTIMIZATION_LEVEL = "-Onone"; - TVOS_DEPLOYMENT_TARGET = 9.0; - VALID_ARCHS = "i386 x86_64"; - VERSIONING_SYSTEM = "apple-generic"; - VERSION_INFO_PREFIX = ""; - WATCHOS_DEPLOYMENT_TARGET = 2.0; - }; - name = Debug; - }; - 4C0041B81BFC3B2200ED2AD5 /* Release */ = { - isa = XCBuildConfiguration; - buildSettings = { - ALWAYS_SEARCH_USER_PATHS = NO; - "ARCHS[sdk=iphoneos*]" = "$(ARCHS_STANDARD)"; - "ARCHS[sdk=iphonesimulator*]" = "$(ARCHS_STANDARD)"; - "ARCHS[sdk=macosx*]" = "$(ARCHS_STANDARD)"; - BITCODE_GENERATION_MODE = bitcode; - CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; - CLANG_CXX_LIBRARY = "libc++"; - CLANG_ENABLE_MODULES = YES; - CLANG_ENABLE_OBJC_ARC = YES; - CLANG_WARN_BOOL_CONVERSION = YES; - CLANG_WARN_CONSTANT_CONVERSION = YES; - CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; - CLANG_WARN_EMPTY_BODY = YES; - CLANG_WARN_ENUM_CONVERSION = YES; - CLANG_WARN_INT_CONVERSION = YES; - CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; - CLANG_WARN_UNREACHABLE_CODE = YES; - CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; - CODE_SIGN_IDENTITY = ""; - COMBINE_HIDPI_IMAGES = YES; - COPY_PHASE_STRIP = NO; - CURRENT_PROJECT_VERSION = 1; - DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; - DEFINES_MODULE = YES; - DYLIB_COMPATIBILITY_VERSION = 1; - DYLIB_CURRENT_VERSION = 1; - DYLIB_INSTALL_NAME_BASE = "@rpath"; - ENABLE_NS_ASSERTIONS = NO; - ENABLE_STRICT_OBJC_MSGSEND = YES; - FRAMEWORK_VERSION = A; - GCC_C_LANGUAGE_STANDARD = gnu99; - GCC_NO_COMMON_BLOCKS = YES; - GCC_WARN_64_TO_32_BIT_CONVERSION = YES; - GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; - GCC_WARN_UNDECLARED_SELECTOR = YES; - GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; - GCC_WARN_UNUSED_FUNCTION = YES; - INFOPLIST_FILE = macosx/Info.plist; - INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; - IPHONEOS_DEPLOYMENT_TARGET = 8.0; - LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/Frameworks"; - MACOSX_DEPLOYMENT_TARGET = 10.9; - MTL_ENABLE_DEBUG_INFO = NO; - OTHER_CFLAGS = ( - "-DSQLITE_HAS_CODEC", - "-DSQLITE_TEMP_STORE=2", - "-DSQLITE_THREADSAFE", - "-DSQLCIPHER_CRYPTO_CC", - "-Wno-ambiguous-macro", - "-Wno-#warnings", - "-Wno-conversion", - "-Wno-unused-const-variable", - "-Wno-unused-function", - "-Wno-unreachable-code", - ); - PRODUCT_BUNDLE_IDENTIFIER = net.zetetic.SQLCipher; - PRODUCT_NAME = SQLCipher; - SDKROOT = macosx; - SKIP_INSTALL = YES; - SUPPORTED_PLATFORMS = macosx; - TVOS_DEPLOYMENT_TARGET = 9.0; - VALID_ARCHS = "i386 x86_64"; - VERSIONING_SYSTEM = "apple-generic"; - VERSION_INFO_PREFIX = ""; - WATCHOS_DEPLOYMENT_TARGET = 2.0; - }; - name = Release; - }; - 4C0041C91BFC3CC000ED2AD5 /* Debug */ = { - isa = XCBuildConfiguration; - buildSettings = { - ALWAYS_SEARCH_USER_PATHS = NO; - "ARCHS[sdk=iphoneos*]" = "$(ARCHS_STANDARD)"; - "ARCHS[sdk=iphonesimulator*]" = "$(ARCHS_STANDARD)"; - "ARCHS[sdk=macosx*]" = "$(ARCHS_STANDARD)"; - BITCODE_GENERATION_MODE = marker; - CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; - CLANG_CXX_LIBRARY = "libc++"; - CLANG_ENABLE_MODULES = YES; - CLANG_ENABLE_OBJC_ARC = YES; - CLANG_WARN_BOOL_CONVERSION = YES; - CLANG_WARN_CONSTANT_CONVERSION = YES; - CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; - CLANG_WARN_EMPTY_BODY = YES; - CLANG_WARN_ENUM_CONVERSION = YES; - CLANG_WARN_INT_CONVERSION = YES; - CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; - CLANG_WARN_UNREACHABLE_CODE = YES; - CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; - COPY_PHASE_STRIP = NO; - CURRENT_PROJECT_VERSION = 1; - DEBUG_INFORMATION_FORMAT = dwarf; - DEFINES_MODULE = YES; - DYLIB_COMPATIBILITY_VERSION = 1; - DYLIB_CURRENT_VERSION = 1; - DYLIB_INSTALL_NAME_BASE = "@rpath"; - ENABLE_STRICT_OBJC_MSGSEND = YES; - GCC_C_LANGUAGE_STANDARD = gnu99; - GCC_DYNAMIC_NO_PIC = NO; - GCC_NO_COMMON_BLOCKS = YES; - GCC_PREPROCESSOR_DEFINITIONS = ( - "DEBUG=1", - "$(inherited)", - ); - GCC_WARN_64_TO_32_BIT_CONVERSION = YES; - GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; - GCC_WARN_UNDECLARED_SELECTOR = YES; - GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; - GCC_WARN_UNUSED_FUNCTION = YES; - INFOPLIST_FILE = "macosx/Info-tvOS.plist"; - INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; - IPHONEOS_DEPLOYMENT_TARGET = 8.0; - LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; - MACOSX_DEPLOYMENT_TARGET = 10.9; - MTL_ENABLE_DEBUG_INFO = YES; - OTHER_CFLAGS = ( - "-DSQLITE_HAS_CODEC", - "-DSQLITE_TEMP_STORE=2", - "-DSQLITE_THREADSAFE", - "-DSQLCIPHER_CRYPTO_CC", - "-Wno-ambiguous-macro", - "-Wno-#warnings", - "-Wno-conversion", - "-Wno-unused-const-variable", - "-Wno-unused-function", - "-Wno-unreachable-code", - ); - PRODUCT_BUNDLE_IDENTIFIER = net.zetetic.SQLCipher; - PRODUCT_NAME = SQLCipher; - SDKROOT = appletvos; - SKIP_INSTALL = YES; - SUPPORTED_PLATFORMS = "appletvsimulator appletvos"; - SWIFT_OPTIMIZATION_LEVEL = "-Onone"; - TARGETED_DEVICE_FAMILY = 3; - TVOS_DEPLOYMENT_TARGET = 9.0; - VALID_ARCHS = arm64; - VERSIONING_SYSTEM = "apple-generic"; - VERSION_INFO_PREFIX = ""; - WATCHOS_DEPLOYMENT_TARGET = 2.0; - }; - name = Debug; - }; - 4C0041CA1BFC3CC000ED2AD5 /* Release */ = { - isa = XCBuildConfiguration; - buildSettings = { - ALWAYS_SEARCH_USER_PATHS = NO; - "ARCHS[sdk=iphoneos*]" = "$(ARCHS_STANDARD)"; - "ARCHS[sdk=iphonesimulator*]" = "$(ARCHS_STANDARD)"; - "ARCHS[sdk=macosx*]" = "$(ARCHS_STANDARD)"; - BITCODE_GENERATION_MODE = bitcode; - CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; - CLANG_CXX_LIBRARY = "libc++"; - CLANG_ENABLE_MODULES = YES; - CLANG_ENABLE_OBJC_ARC = YES; - CLANG_WARN_BOOL_CONVERSION = YES; - CLANG_WARN_CONSTANT_CONVERSION = YES; - CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; - CLANG_WARN_EMPTY_BODY = YES; - CLANG_WARN_ENUM_CONVERSION = YES; - CLANG_WARN_INT_CONVERSION = YES; - CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; - CLANG_WARN_UNREACHABLE_CODE = YES; - CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; - COPY_PHASE_STRIP = NO; - CURRENT_PROJECT_VERSION = 1; - DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; - DEFINES_MODULE = YES; - DYLIB_COMPATIBILITY_VERSION = 1; - DYLIB_CURRENT_VERSION = 1; - DYLIB_INSTALL_NAME_BASE = "@rpath"; - ENABLE_NS_ASSERTIONS = NO; - ENABLE_STRICT_OBJC_MSGSEND = YES; - GCC_C_LANGUAGE_STANDARD = gnu99; - GCC_NO_COMMON_BLOCKS = YES; - GCC_WARN_64_TO_32_BIT_CONVERSION = YES; - GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; - GCC_WARN_UNDECLARED_SELECTOR = YES; - GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; - GCC_WARN_UNUSED_FUNCTION = YES; - INFOPLIST_FILE = "macosx/Info-tvOS.plist"; - INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; - IPHONEOS_DEPLOYMENT_TARGET = 8.0; - LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; - MACOSX_DEPLOYMENT_TARGET = 10.9; - MTL_ENABLE_DEBUG_INFO = NO; - OTHER_CFLAGS = ( - "-DSQLITE_HAS_CODEC", - "-DSQLITE_TEMP_STORE=2", - "-DSQLITE_THREADSAFE", - "-DSQLCIPHER_CRYPTO_CC", - "-Wno-ambiguous-macro", - "-Wno-#warnings", - "-Wno-conversion", - "-Wno-unused-const-variable", - "-Wno-unused-function", - "-Wno-unreachable-code", - ); - PRODUCT_BUNDLE_IDENTIFIER = net.zetetic.SQLCipher; - PRODUCT_NAME = SQLCipher; - SDKROOT = appletvos; - SKIP_INSTALL = YES; - SUPPORTED_PLATFORMS = "appletvsimulator appletvos"; - TARGETED_DEVICE_FAMILY = 3; - TVOS_DEPLOYMENT_TARGET = 9.0; - VALIDATE_PRODUCT = YES; - VALID_ARCHS = arm64; - VERSIONING_SYSTEM = "apple-generic"; - VERSION_INFO_PREFIX = ""; - WATCHOS_DEPLOYMENT_TARGET = 2.0; - }; - name = Release; - }; - 4C0041DC1BFC3E2600ED2AD5 /* Debug */ = { - isa = XCBuildConfiguration; - buildSettings = { - ALWAYS_SEARCH_USER_PATHS = NO; - APPLICATION_EXTENSION_API_ONLY = YES; - "ARCHS[sdk=iphoneos*]" = "$(ARCHS_STANDARD)"; - "ARCHS[sdk=iphonesimulator*]" = "$(ARCHS_STANDARD)"; - "ARCHS[sdk=macosx*]" = "$(ARCHS_STANDARD)"; - BITCODE_GENERATION_MODE = marker; - CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; - CLANG_CXX_LIBRARY = "libc++"; - CLANG_ENABLE_MODULES = YES; - CLANG_ENABLE_OBJC_ARC = YES; - CLANG_WARN_BOOL_CONVERSION = YES; - CLANG_WARN_CONSTANT_CONVERSION = YES; - CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; - CLANG_WARN_EMPTY_BODY = YES; - CLANG_WARN_ENUM_CONVERSION = YES; - CLANG_WARN_INT_CONVERSION = YES; - CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; - CLANG_WARN_UNREACHABLE_CODE = YES; - CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; - COPY_PHASE_STRIP = NO; - CURRENT_PROJECT_VERSION = 1; - DEBUG_INFORMATION_FORMAT = dwarf; - DEFINES_MODULE = YES; - DYLIB_COMPATIBILITY_VERSION = 1; - DYLIB_CURRENT_VERSION = 1; - DYLIB_INSTALL_NAME_BASE = "@rpath"; - ENABLE_STRICT_OBJC_MSGSEND = YES; - GCC_C_LANGUAGE_STANDARD = gnu99; - GCC_DYNAMIC_NO_PIC = NO; - GCC_NO_COMMON_BLOCKS = YES; - GCC_PREPROCESSOR_DEFINITIONS = ( - "DEBUG=1", - "$(inherited)", - ); - GCC_WARN_64_TO_32_BIT_CONVERSION = YES; - GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; - GCC_WARN_UNDECLARED_SELECTOR = YES; - GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; - GCC_WARN_UNUSED_FUNCTION = YES; - INFOPLIST_FILE = macosx/Info.plist; - INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; - IPHONEOS_DEPLOYMENT_TARGET = 8.0; - LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; - MACOSX_DEPLOYMENT_TARGET = 10.9; - MTL_ENABLE_DEBUG_INFO = YES; - OTHER_CFLAGS = ( - "-DSQLITE_HAS_CODEC", - "-DSQLITE_TEMP_STORE=2", - "-DSQLITE_THREADSAFE", - "-DSQLCIPHER_CRYPTO_CC", - "-Wno-ambiguous-macro", - "-Wno-#warnings", - "-Wno-conversion", - "-Wno-unused-const-variable", - "-Wno-unused-function", - "-Wno-unreachable-code", - ); - PRODUCT_BUNDLE_IDENTIFIER = net.zetetic.SQLCipher; - PRODUCT_NAME = SQLCipher; - SDKROOT = watchos; - SKIP_INSTALL = YES; - SUPPORTED_PLATFORMS = "watchsimulator watchos"; - SWIFT_OPTIMIZATION_LEVEL = "-Onone"; - TARGETED_DEVICE_FAMILY = 4; - TVOS_DEPLOYMENT_TARGET = 9.0; - VALID_ARCHS = armv7k; - VERSIONING_SYSTEM = "apple-generic"; - VERSION_INFO_PREFIX = ""; - WATCHOS_DEPLOYMENT_TARGET = 2.0; - }; - name = Debug; - }; - 4C0041DD1BFC3E2600ED2AD5 /* Release */ = { - isa = XCBuildConfiguration; - buildSettings = { - ALWAYS_SEARCH_USER_PATHS = NO; - APPLICATION_EXTENSION_API_ONLY = YES; - "ARCHS[sdk=iphoneos*]" = "$(ARCHS_STANDARD)"; - "ARCHS[sdk=iphonesimulator*]" = "$(ARCHS_STANDARD)"; - "ARCHS[sdk=macosx*]" = "$(ARCHS_STANDARD)"; - BITCODE_GENERATION_MODE = bitcode; - CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; - CLANG_CXX_LIBRARY = "libc++"; - CLANG_ENABLE_MODULES = YES; - CLANG_ENABLE_OBJC_ARC = YES; - CLANG_WARN_BOOL_CONVERSION = YES; - CLANG_WARN_CONSTANT_CONVERSION = YES; - CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; - CLANG_WARN_EMPTY_BODY = YES; - CLANG_WARN_ENUM_CONVERSION = YES; - CLANG_WARN_INT_CONVERSION = YES; - CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; - CLANG_WARN_UNREACHABLE_CODE = YES; - CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; - COPY_PHASE_STRIP = NO; - CURRENT_PROJECT_VERSION = 1; - DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; - DEFINES_MODULE = YES; - DYLIB_COMPATIBILITY_VERSION = 1; - DYLIB_CURRENT_VERSION = 1; - DYLIB_INSTALL_NAME_BASE = "@rpath"; - ENABLE_NS_ASSERTIONS = NO; - ENABLE_STRICT_OBJC_MSGSEND = YES; - GCC_C_LANGUAGE_STANDARD = gnu99; - GCC_NO_COMMON_BLOCKS = YES; - GCC_WARN_64_TO_32_BIT_CONVERSION = YES; - GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; - GCC_WARN_UNDECLARED_SELECTOR = YES; - GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; - GCC_WARN_UNUSED_FUNCTION = YES; - INFOPLIST_FILE = macosx/Info.plist; - INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; - IPHONEOS_DEPLOYMENT_TARGET = 8.0; - LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; - MACOSX_DEPLOYMENT_TARGET = 10.9; - MTL_ENABLE_DEBUG_INFO = NO; - OTHER_CFLAGS = ( - "-DSQLITE_HAS_CODEC", - "-DSQLITE_TEMP_STORE=2", - "-DSQLITE_THREADSAFE", - "-DSQLCIPHER_CRYPTO_CC", - "-Wno-ambiguous-macro", - "-Wno-#warnings", - "-Wno-conversion", - "-Wno-unused-const-variable", - "-Wno-unused-function", - "-Wno-unreachable-code", - ); - PRODUCT_BUNDLE_IDENTIFIER = net.zetetic.SQLCipher; - PRODUCT_NAME = SQLCipher; - SDKROOT = watchos; - SKIP_INSTALL = YES; - SUPPORTED_PLATFORMS = "watchsimulator watchos"; - TARGETED_DEVICE_FAMILY = 4; - TVOS_DEPLOYMENT_TARGET = 9.0; - VALIDATE_PRODUCT = YES; - VALID_ARCHS = armv7k; - VERSIONING_SYSTEM = "apple-generic"; - VERSION_INFO_PREFIX = ""; - WATCHOS_DEPLOYMENT_TARGET = 2.0; - }; - name = Release; - }; - 9069D08C0FCE185A0042E34C /* Debug */ = { - isa = XCBuildConfiguration; - buildSettings = { - COMBINE_HIDPI_IMAGES = YES; - COPY_PHASE_STRIP = NO; - GCC_DYNAMIC_NO_PIC = NO; - GCC_OPTIMIZATION_LEVEL = 0; - PRODUCT_NAME = amalgamation; - SDKROOT = macosx; - SUPPORTED_PLATFORMS = macosx; - }; - name = Debug; - }; - 9069D08D0FCE185A0042E34C /* Release */ = { - isa = XCBuildConfiguration; - buildSettings = { - COMBINE_HIDPI_IMAGES = YES; - COPY_PHASE_STRIP = YES; - DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; - PRODUCT_NAME = amalgamation; - SDKROOT = macosx; - SUPPORTED_PLATFORMS = macosx; - ZERO_LINK = NO; - }; - name = Release; - }; -/* End XCBuildConfiguration section */ - -/* Begin XCConfigurationList section */ - 1DEB91EB08733DB70010E9CD /* Build configuration list for PBXNativeTarget "sqlcipher" */ = { - isa = XCConfigurationList; - buildConfigurations = ( - 1DEB91EC08733DB70010E9CD /* Debug */, - 1DEB91ED08733DB70010E9CD /* Release */, - ); - defaultConfigurationIsVisible = 0; - defaultConfigurationName = Release; - }; - 1DEB91EF08733DB70010E9CD /* Build configuration list for PBXProject "sqlcipher" */ = { - isa = XCConfigurationList; - buildConfigurations = ( - 1DEB91F008733DB70010E9CD /* Debug */, - 1DEB91F108733DB70010E9CD /* Release */, - ); - defaultConfigurationIsVisible = 0; - defaultConfigurationName = Release; - }; - 4C00419D1BFC382500ED2AD5 /* Build configuration list for PBXNativeTarget "SQLCipher iOS" */ = { - isa = XCConfigurationList; - buildConfigurations = ( - 4C00419E1BFC382500ED2AD5 /* Debug */, - 4C00419F1BFC382500ED2AD5 /* Release */, - ); - defaultConfigurationIsVisible = 0; - defaultConfigurationName = Release; - }; - 4C0041B61BFC3B2200ED2AD5 /* Build configuration list for PBXNativeTarget "SQLCipher OSX" */ = { - isa = XCConfigurationList; - buildConfigurations = ( - 4C0041B71BFC3B2200ED2AD5 /* Debug */, - 4C0041B81BFC3B2200ED2AD5 /* Release */, - ); - defaultConfigurationIsVisible = 0; - defaultConfigurationName = Release; - }; - 4C0041C81BFC3CC000ED2AD5 /* Build configuration list for PBXNativeTarget "SQLCipher tvOS" */ = { - isa = XCConfigurationList; - buildConfigurations = ( - 4C0041C91BFC3CC000ED2AD5 /* Debug */, - 4C0041CA1BFC3CC000ED2AD5 /* Release */, - ); - defaultConfigurationIsVisible = 0; - defaultConfigurationName = Release; - }; - 4C0041DB1BFC3E2600ED2AD5 /* Build configuration list for PBXNativeTarget "SQLCipher watchOS" */ = { - isa = XCConfigurationList; - buildConfigurations = ( - 4C0041DC1BFC3E2600ED2AD5 /* Debug */, - 4C0041DD1BFC3E2600ED2AD5 /* Release */, - ); - defaultConfigurationIsVisible = 0; - defaultConfigurationName = Release; - }; - 9069D0900FCE18970042E34C /* Build configuration list for PBXAggregateTarget "amalgamation" */ = { - isa = XCConfigurationList; - buildConfigurations = ( - 9069D08C0FCE185A0042E34C /* Debug */, - 9069D08D0FCE185A0042E34C /* Release */, - ); - defaultConfigurationIsVisible = 0; - defaultConfigurationName = Release; - }; -/* End XCConfigurationList section */ - }; - rootObject = 08FB7793FE84155DC02AAC07 /* Project object */; -} diff --git a/sqlcipher.xcodeproj/xcshareddata/xcschemes/SQLCipher OSX.xcscheme b/sqlcipher.xcodeproj/xcshareddata/xcschemes/SQLCipher OSX.xcscheme deleted file mode 100644 index a1b6956eb0..0000000000 --- a/sqlcipher.xcodeproj/xcshareddata/xcschemes/SQLCipher OSX.xcscheme +++ /dev/null @@ -1,80 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/sqlcipher.xcodeproj/xcshareddata/xcschemes/SQLCipher iOS.xcscheme b/sqlcipher.xcodeproj/xcshareddata/xcschemes/SQLCipher iOS.xcscheme deleted file mode 100644 index a988b2bcd4..0000000000 --- a/sqlcipher.xcodeproj/xcshareddata/xcschemes/SQLCipher iOS.xcscheme +++ /dev/null @@ -1,80 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/sqlcipher.xcodeproj/xcshareddata/xcschemes/SQLCipher tvOS.xcscheme b/sqlcipher.xcodeproj/xcshareddata/xcschemes/SQLCipher tvOS.xcscheme deleted file mode 100644 index 9104d67653..0000000000 --- a/sqlcipher.xcodeproj/xcshareddata/xcschemes/SQLCipher tvOS.xcscheme +++ /dev/null @@ -1,80 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/sqlcipher.xcodeproj/xcshareddata/xcschemes/SQLCipher watchOS.xcscheme b/sqlcipher.xcodeproj/xcshareddata/xcschemes/SQLCipher watchOS.xcscheme deleted file mode 100644 index db6e6d9502..0000000000 --- a/sqlcipher.xcodeproj/xcshareddata/xcschemes/SQLCipher watchOS.xcscheme +++ /dev/null @@ -1,80 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/sqlite3.1 b/sqlite3.1 new file mode 100644 index 0000000000..e4ba5a0cb8 --- /dev/null +++ b/sqlite3.1 @@ -0,0 +1,181 @@ +.\" Hey, EMACS: -*- nroff -*- +.\" First parameter, NAME, should be all caps +.\" Second parameter, SECTION, should be 1-8, maybe w/ subsection +.\" other parameters are allowed: see man(7), man(1) +.TH SQLITE3 1 "Fri Aug 11 23:50:12 CET 2023" +.\" Please adjust this date whenever revising the manpage. +.\" +.\" Some roff macros, for reference: +.\" .nh disable hyphenation +.\" .hy enable hyphenation +.\" .ad l left justify +.\" .ad b justify to both left and right margins +.\" .nf disable filling +.\" .fi enable filling +.\" .br insert line break +.\" .sp insert n+1 empty lines +.\" for manpage-specific macros, see man(7) +.SH NAME +.B sqlite3 +\- A command line interface for SQLite version 3 + +.SH SYNOPSIS +.B sqlite3 +.RI [ options ] +.RI [ databasefile ] +.RI [ SQL ] + +.SH SUMMARY +.PP +.B sqlite3 +is a terminal-based front-end to the SQLite library that can evaluate +queries interactively and display the results in multiple formats. +.B sqlite3 +can also be used within shell scripts and other applications to provide +batch processing features. + +.SH DESCRIPTION +To start a +.B sqlite3 +interactive session, invoke the +.B sqlite3 +command and optionally provide the name of a database file. If the +database file does not exist, it will be created. If the database file +does exist, it will be opened. + +For example, to create a new database file named "mydata.db", create +a table named "memos" and insert a couple of records into that table: +.sp +$ +.B sqlite3 mydata.db +.br +SQLite version 3.43.0 2023-08-11 17:45:23 +.br +Enter ".help" for usage hints. +.br +sqlite> +.B create table memos(text, priority INTEGER); +.br +sqlite> +.B insert into memos values('deliver project description', 10); +.br +sqlite> +.B insert into memos values('lunch with Christine', 100); +.br +sqlite> +.B select * from memos; +.br +deliver project description|10 +.br +lunch with Christine|100 +.br +sqlite> +.sp + +If no database name is supplied, the ATTACH sql command can be used +to attach to existing or create new database files. ATTACH can also +be used to attach to multiple databases within the same interactive +session. This is useful for migrating data between databases, +possibly changing the schema along the way. + +Optionally, a SQL statement or set of SQL statements can be supplied as +a single argument. Multiple statements should be separated by +semi-colons. + +For example: +.sp +$ +.B sqlite3 -line mydata.db 'select * from memos where priority > 20;' +.br + text = lunch with Christine +.br +priority = 100 +.br +.sp + +.SS SQLITE META-COMMANDS +.PP +The interactive interpreter offers a set of meta-commands that can be +used to control the output format, examine the currently attached +database files, or perform administrative operations upon the +attached databases (such as rebuilding indices). Meta-commands are +always prefixed with a dot (.). + +A list of available meta-commands can be viewed at any time by issuing +the '.help' command. For example: +.sp +sqlite> +.B .help +.nf +.tr %. +... +.sp +.fi + +The available commands differ by version and build options, so they +are not listed here. Please refer to your local copy for all available +options. + + +.SH INIT FILE +.B sqlite3 +reads an initialization file to set the configuration of the +interactive environment. Throughout initialization, any previously +specified setting can be overridden. The sequence of initialization is +as follows: + +o The default configuration is established as follows: + +.sp +.nf +.cc | +mode = LIST +separator = "|" +main prompt = "sqlite> " +continue prompt = " ...> " +|cc . +.sp +.fi + +o If the environment variable XDG_CONFIG_HOME is set then +.B ${XDG_CONFIG_HOME}/sqlite3/sqliterc +is checked, else +.B ~/.config/sqlite3/sqliterc +is checked. If the selected file does not exist then the fallback of +.B ~/.sqliterc +is used. It should generally only contain meta-commands. + +o If the -init option is present, the specified file is processed. + +o All other command line options are processed. + +.SH HISTORY FILE +.B sqlite3 +may be configured to use a history file to save SQL statements and +meta-commands entered interactively. These statements and commands can be +retrieved, edited and, reused at the main and continue prompts. If the +environment variable +.B SQLITE_HISTORY +is set, it will be used as the name of the history file, whether it +already exists or not. If it is not set but the XDG_STATE_HOME +environment variable is then +.B ${XDG_STATE_HOME}/sqlite_history +is used. If XDG_STATE_HOME is not set then +.B ~/.local/state/sqlite_history +is used. If the selected file does not exist then +.B ~/.sqlite_history +will be used as the history file. If any history file is found, it +will be written if the shell exits interactive mode normally, +regardless of whether it existed previously, though saving will +silently fail if the history file's directory does not exist. +.SH SEE ALSO +https://sqlite.org/cli.html +.br +https://sqlite.org/fiddle (a WebAssembly build of the CLI app) +.br +The sqlite3-doc package. +.SH AUTHOR +This manual page was originally written by Andreas Rottmann +, for the Debian GNU/Linux system (but may be used +by others). It was subsequently revised by Bill Bumgarner , +Laszlo Boszormenyi , and the sqlite3 developers. diff --git a/sqlcipher.pc.in b/sqlite3.pc.in similarity index 53% rename from sqlcipher.pc.in rename to sqlite3.pc.in index a8789bd39e..723dd51563 100644 --- a/sqlcipher.pc.in +++ b/sqlite3.pc.in @@ -5,9 +5,9 @@ exec_prefix=@exec_prefix@ libdir=@libdir@ includedir=@includedir@ -Name: SQLCipher +Name: SQLite Description: SQL database engine Version: @PACKAGE_VERSION@ -Libs: -L${libdir} -lsqlcipher -Libs.private: @LIBS@ -Cflags: -I${includedir}/sqlcipher +Libs: -L${libdir} -lsqlite3 +Libs.private: @LDFLAGS_MATH@ @LDFLAGS_ZLIB@ @LDFLAGS_DLOPEN@ @LDFLAGS_PTHREAD@ @LDFLAGS_ICU@ +Cflags: -I${includedir} diff --git a/src/alter.c b/src/alter.c index 34221777a7..a7255e75ef 100644 --- a/src/alter.c +++ b/src/alter.c @@ -20,377 +20,106 @@ */ #ifndef SQLITE_OMIT_ALTERTABLE - -/* -** This function is used by SQL generated to implement the -** ALTER TABLE command. The first argument is the text of a CREATE TABLE or -** CREATE INDEX command. The second is a table name. The table name in -** the CREATE TABLE or CREATE INDEX statement is replaced with the third -** argument and the result returned. Examples: -** -** sqlite_rename_table('CREATE TABLE abc(a, b, c)', 'def') -** -> 'CREATE TABLE def(a, b, c)' -** -** sqlite_rename_table('CREATE INDEX i ON abc(a)', 'def') -** -> 'CREATE INDEX i ON def(a, b, c)' -*/ -static void renameTableFunc( - sqlite3_context *context, - int NotUsed, - sqlite3_value **argv -){ - unsigned char const *zSql = sqlite3_value_text(argv[0]); - unsigned char const *zTableName = sqlite3_value_text(argv[1]); - - int token; - Token tname; - unsigned char const *zCsr = zSql; - int len = 0; - char *zRet; - - sqlite3 *db = sqlite3_context_db_handle(context); - - UNUSED_PARAMETER(NotUsed); - - /* The principle used to locate the table name in the CREATE TABLE - ** statement is that the table name is the first non-space token that - ** is immediately followed by a TK_LP or TK_USING token. - */ - if( zSql ){ - do { - if( !*zCsr ){ - /* Ran out of input before finding an opening bracket. Return NULL. */ - return; - } - - /* Store the token that zCsr points to in tname. */ - tname.z = (char*)zCsr; - tname.n = len; - - /* Advance zCsr to the next token. Store that token type in 'token', - ** and its length in 'len' (to be used next iteration of this loop). - */ - do { - zCsr += len; - len = sqlite3GetToken(zCsr, &token); - } while( token==TK_SPACE ); - assert( len>0 ); - } while( token!=TK_LP && token!=TK_USING ); - - zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", (int)(((u8*)tname.z) - zSql), - zSql, zTableName, tname.z+tname.n); - sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC); - } -} - /* -** This C function implements an SQL user function that is used by SQL code -** generated by the ALTER TABLE ... RENAME command to modify the definition -** of any foreign key constraints that use the table being renamed as the -** parent table. It is passed three arguments: -** -** 1) The complete text of the CREATE TABLE statement being modified, -** 2) The old name of the table being renamed, and -** 3) The new name of the table being renamed. -** -** It returns the new CREATE TABLE statement. For example: +** Parameter zName is the name of a table that is about to be altered +** (either with ALTER TABLE ... RENAME TO or ALTER TABLE ... ADD COLUMN). +** If the table is a system table, this function leaves an error message +** in pParse->zErr (system tables may not be altered) and returns non-zero. ** -** sqlite_rename_parent('CREATE TABLE t1(a REFERENCES t2)', 't2', 't3') -** -> 'CREATE TABLE t1(a REFERENCES t3)' -*/ -#ifndef SQLITE_OMIT_FOREIGN_KEY -static void renameParentFunc( - sqlite3_context *context, - int NotUsed, - sqlite3_value **argv -){ - sqlite3 *db = sqlite3_context_db_handle(context); - char *zOutput = 0; - char *zResult; - unsigned char const *zInput = sqlite3_value_text(argv[0]); - unsigned char const *zOld = sqlite3_value_text(argv[1]); - unsigned char const *zNew = sqlite3_value_text(argv[2]); - - unsigned const char *z; /* Pointer to token */ - int n; /* Length of token z */ - int token; /* Type of token */ - - UNUSED_PARAMETER(NotUsed); - if( zInput==0 || zOld==0 ) return; - for(z=zInput; *z; z=z+n){ - n = sqlite3GetToken(z, &token); - if( token==TK_REFERENCES ){ - char *zParent; - do { - z += n; - n = sqlite3GetToken(z, &token); - }while( token==TK_SPACE ); - - if( token==TK_ILLEGAL ) break; - zParent = sqlite3DbStrNDup(db, (const char *)z, n); - if( zParent==0 ) break; - sqlite3Dequote(zParent); - if( 0==sqlite3StrICmp((const char *)zOld, zParent) ){ - char *zOut = sqlite3MPrintf(db, "%s%.*s\"%w\"", - (zOutput?zOutput:""), (int)(z-zInput), zInput, (const char *)zNew - ); - sqlite3DbFree(db, zOutput); - zOutput = zOut; - zInput = &z[n]; - } - sqlite3DbFree(db, zParent); - } - } - - zResult = sqlite3MPrintf(db, "%s%s", (zOutput?zOutput:""), zInput), - sqlite3_result_text(context, zResult, -1, SQLITE_DYNAMIC); - sqlite3DbFree(db, zOutput); -} -#endif - -#ifndef SQLITE_OMIT_TRIGGER -/* This function is used by SQL generated to implement the -** ALTER TABLE command. The first argument is the text of a CREATE TRIGGER -** statement. The second is a table name. The table name in the CREATE -** TRIGGER statement is replaced with the third argument and the result -** returned. This is analagous to renameTableFunc() above, except for CREATE -** TRIGGER, not CREATE INDEX and CREATE TABLE. -*/ -static void renameTriggerFunc( - sqlite3_context *context, - int NotUsed, - sqlite3_value **argv -){ - unsigned char const *zSql = sqlite3_value_text(argv[0]); - unsigned char const *zTableName = sqlite3_value_text(argv[1]); - - int token; - Token tname; - int dist = 3; - unsigned char const *zCsr = zSql; - int len = 0; - char *zRet; - sqlite3 *db = sqlite3_context_db_handle(context); - - UNUSED_PARAMETER(NotUsed); - - /* The principle used to locate the table name in the CREATE TRIGGER - ** statement is that the table name is the first token that is immediately - ** preceded by either TK_ON or TK_DOT and immediately followed by one - ** of TK_WHEN, TK_BEGIN or TK_FOR. - */ - if( zSql ){ - do { - - if( !*zCsr ){ - /* Ran out of input before finding the table name. Return NULL. */ - return; - } - - /* Store the token that zCsr points to in tname. */ - tname.z = (char*)zCsr; - tname.n = len; - - /* Advance zCsr to the next token. Store that token type in 'token', - ** and its length in 'len' (to be used next iteration of this loop). - */ - do { - zCsr += len; - len = sqlite3GetToken(zCsr, &token); - }while( token==TK_SPACE ); - assert( len>0 ); - - /* Variable 'dist' stores the number of tokens read since the most - ** recent TK_DOT or TK_ON. This means that when a WHEN, FOR or BEGIN - ** token is read and 'dist' equals 2, the condition stated above - ** to be met. - ** - ** Note that ON cannot be a database, table or column name, so - ** there is no need to worry about syntax like - ** "CREATE TRIGGER ... ON ON.ON BEGIN ..." etc. - */ - dist++; - if( token==TK_DOT || token==TK_ON ){ - dist = 0; - } - } while( dist!=2 || (token!=TK_WHEN && token!=TK_FOR && token!=TK_BEGIN) ); - - /* Variable tname now contains the token that is the old table-name - ** in the CREATE TRIGGER statement. - */ - zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", (int)(((u8*)tname.z) - zSql), - zSql, zTableName, tname.z+tname.n); - sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC); - } -} -#endif /* !SQLITE_OMIT_TRIGGER */ - -/* -** Register built-in functions used to help implement ALTER TABLE +** Or, if zName is not a system table, zero is returned. */ -void sqlite3AlterFunctions(void){ - static SQLITE_WSD FuncDef aAlterTableFuncs[] = { - FUNCTION(sqlite_rename_table, 2, 0, 0, renameTableFunc), -#ifndef SQLITE_OMIT_TRIGGER - FUNCTION(sqlite_rename_trigger, 2, 0, 0, renameTriggerFunc), -#endif -#ifndef SQLITE_OMIT_FOREIGN_KEY - FUNCTION(sqlite_rename_parent, 3, 0, 0, renameParentFunc), +static int isAlterableTable(Parse *pParse, Table *pTab){ + if( 0==sqlite3StrNICmp(pTab->zName, "sqlite_", 7) +#ifndef SQLITE_OMIT_VIRTUALTABLE + || (pTab->tabFlags & TF_Eponymous)!=0 + || ( (pTab->tabFlags & TF_Shadow)!=0 + && sqlite3ReadOnlyShadowTables(pParse->db) + ) #endif - }; - int i; - FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions); - FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aAlterTableFuncs); - - for(i=0; i OR name= OR ... -** -** If argument zWhere is NULL, then a pointer string containing the text -** "name=" is returned, where is the quoted version -** of the string passed as argument zConstant. The returned buffer is -** allocated using sqlite3DbMalloc(). It is the responsibility of the -** caller to ensure that it is eventually freed. -** -** If argument zWhere is not NULL, then the string returned is -** " OR name=", where is the contents of zWhere. -** In this case zWhere is passed to sqlite3DbFree() before returning. -** -*/ -static char *whereOrName(sqlite3 *db, char *zWhere, char *zConstant){ - char *zNew; - if( !zWhere ){ - zNew = sqlite3MPrintf(db, "name=%Q", zConstant); - }else{ - zNew = sqlite3MPrintf(db, "%s OR name=%Q", zWhere, zConstant); - sqlite3DbFree(db, zWhere); - } - return zNew; -} - -#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER) -/* -** Generate the text of a WHERE expression which can be used to select all -** tables that have foreign key constraints that refer to table pTab (i.e. -** constraints for which pTab is the parent table) from the sqlite_master -** table. -*/ -static char *whereForeignKeys(Parse *pParse, Table *pTab){ - FKey *p; - char *zWhere = 0; - for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){ - zWhere = whereOrName(pParse->db, zWhere, p->pFrom->zName); + ){ + sqlite3ErrorMsg(pParse, "table %s may not be altered", pTab->zName); + return 1; } - return zWhere; + return 0; } -#endif /* -** Generate the text of a WHERE expression which can be used to select all -** temporary triggers on table pTab from the sqlite_temp_master table. If -** table pTab has no temporary triggers, or is itself stored in the -** temporary database, NULL is returned. +** Generate code to verify that the schemas of database zDb and, if +** bTemp is not true, database "temp", can still be parsed. This is +** called at the end of the generation of an ALTER TABLE ... RENAME ... +** statement to ensure that the operation has not rendered any schema +** objects unusable. */ -static char *whereTempTriggers(Parse *pParse, Table *pTab){ - Trigger *pTrig; - char *zWhere = 0; - const Schema *pTempSchema = pParse->db->aDb[1].pSchema; /* Temp db schema */ +static void renameTestSchema( + Parse *pParse, /* Parse context */ + const char *zDb, /* Name of db to verify schema of */ + int bTemp, /* True if this is the temp db */ + const char *zWhen, /* "when" part of error message */ + int bNoDQS /* Do not allow DQS in the schema */ +){ + pParse->colNamesSet = 1; + sqlite3NestedParse(pParse, + "SELECT 1 " + "FROM \"%w\"." LEGACY_SCHEMA_TABLE " " + "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X'" + " AND sql NOT LIKE 'create virtual%%'" + " AND sqlite_rename_test(%Q, sql, type, name, %d, %Q, %d)=NULL ", + zDb, + zDb, bTemp, zWhen, bNoDQS + ); - /* If the table is not located in the temp-db (in which case NULL is - ** returned, loop through the tables list of triggers. For each trigger - ** that is not part of the temp-db schema, add a clause to the WHERE - ** expression being built up in zWhere. - */ - if( pTab->pSchema!=pTempSchema ){ - sqlite3 *db = pParse->db; - for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){ - if( pTrig->pSchema==pTempSchema ){ - zWhere = whereOrName(db, zWhere, pTrig->zName); - } - } - } - if( zWhere ){ - char *zNew = sqlite3MPrintf(pParse->db, "type='trigger' AND (%s)", zWhere); - sqlite3DbFree(pParse->db, zWhere); - zWhere = zNew; + if( bTemp==0 ){ + sqlite3NestedParse(pParse, + "SELECT 1 " + "FROM temp." LEGACY_SCHEMA_TABLE " " + "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X'" + " AND sql NOT LIKE 'create virtual%%'" + " AND sqlite_rename_test(%Q, sql, type, name, 1, %Q, %d)=NULL ", + zDb, zWhen, bNoDQS + ); } - return zWhere; } /* -** Generate code to drop and reload the internal representation of table -** pTab from the database, including triggers and temporary triggers. -** Argument zName is the name of the table in the database schema at -** the time the generated code is executed. This can be different from -** pTab->zName if this function is being called to code part of an -** "ALTER TABLE RENAME TO" statement. +** Generate VM code to replace any double-quoted strings (but not double-quoted +** identifiers) within the "sql" column of the sqlite_schema table in +** database zDb with their single-quoted equivalents. If argument bTemp is +** not true, similarly update all SQL statements in the sqlite_schema table +** of the temp db. */ -static void reloadTableSchema(Parse *pParse, Table *pTab, const char *zName){ - Vdbe *v; - char *zWhere; - int iDb; /* Index of database containing pTab */ -#ifndef SQLITE_OMIT_TRIGGER - Trigger *pTrig; -#endif - - v = sqlite3GetVdbe(pParse); - if( NEVER(v==0) ) return; - assert( sqlite3BtreeHoldsAllMutexes(pParse->db) ); - iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); - assert( iDb>=0 ); - -#ifndef SQLITE_OMIT_TRIGGER - /* Drop any table triggers from the internal schema. */ - for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){ - int iTrigDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema); - assert( iTrigDb==iDb || iTrigDb==1 ); - sqlite3VdbeAddOp4(v, OP_DropTrigger, iTrigDb, 0, 0, pTrig->zName, 0); - } -#endif - - /* Drop the table and index from the internal schema. */ - sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0); - - /* Reload the table, index and permanent trigger schemas. */ - zWhere = sqlite3MPrintf(pParse->db, "tbl_name=%Q", zName); - if( !zWhere ) return; - sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere); - -#ifndef SQLITE_OMIT_TRIGGER - /* Now, if the table is not stored in the temp database, reload any temp - ** triggers. Don't use IN(...) in case SQLITE_OMIT_SUBQUERY is defined. - */ - if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){ - sqlite3VdbeAddParseSchemaOp(v, 1, zWhere); +static void renameFixQuotes(Parse *pParse, const char *zDb, int bTemp){ + sqlite3NestedParse(pParse, + "UPDATE \"%w\"." LEGACY_SCHEMA_TABLE + " SET sql = sqlite_rename_quotefix(%Q, sql)" + "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X'" + " AND sql NOT LIKE 'create virtual%%'" , zDb, zDb + ); + if( bTemp==0 ){ + sqlite3NestedParse(pParse, + "UPDATE temp." LEGACY_SCHEMA_TABLE + " SET sql = sqlite_rename_quotefix('temp', sql)" + "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X'" + " AND sql NOT LIKE 'create virtual%%'" + ); } -#endif } /* -** Parameter zName is the name of a table that is about to be altered -** (either with ALTER TABLE ... RENAME TO or ALTER TABLE ... ADD COLUMN). -** If the table is a system table, this function leaves an error message -** in pParse->zErr (system tables may not be altered) and returns non-zero. -** -** Or, if zName is not a system table, zero is returned. +** Generate code to reload the schema for database iDb. And, if iDb!=1, for +** the temp database as well. */ -static int isSystemTable(Parse *pParse, const char *zName){ - if( sqlite3Strlen30(zName)>6 && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){ - sqlite3ErrorMsg(pParse, "table %s may not be altered", zName); - return 1; +static void renameReloadSchema(Parse *pParse, int iDb, u16 p5){ + Vdbe *v = pParse->pVdbe; + if( v ){ + sqlite3ChangeCookie(pParse, iDb); + sqlite3VdbeAddParseSchemaOp(pParse->pVdbe, iDb, 0, p5); + if( iDb!=1 ) sqlite3VdbeAddParseSchemaOp(pParse->pVdbe, 1, 0, p5); } - return 0; } /* -** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy" -** command. +** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy" +** command. */ void sqlite3AlterRenameTable( Parse *pParse, /* Parser context. */ @@ -400,18 +129,13 @@ void sqlite3AlterRenameTable( int iDb; /* Database that contains the table */ char *zDb; /* Name of database iDb */ Table *pTab; /* Table being renamed */ - char *zName = 0; /* NULL-terminated version of pName */ + char *zName = 0; /* NULL-terminated version of pName */ sqlite3 *db = pParse->db; /* Database connection */ int nTabName; /* Number of UTF-8 characters in zTabName */ const char *zTabName; /* Original name of the table */ Vdbe *v; -#ifndef SQLITE_OMIT_TRIGGER - char *zWhere = 0; /* Where clause to locate temp triggers */ -#endif VTable *pVTab = 0; /* Non-zero if this is a v-tab with an xRename() */ - int savedDbFlags; /* Saved value of db->flags */ - savedDbFlags = db->flags; if( NEVER(db->mallocFailed) ) goto exit_rename_table; assert( pSrc->nSrc==1 ); assert( sqlite3BtreeHoldsAllMutexes(pParse->db) ); @@ -419,8 +143,7 @@ void sqlite3AlterRenameTable( pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]); if( !pTab ) goto exit_rename_table; iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); - zDb = db->aDb[iDb].zName; - db->flags |= SQLITE_PreferBuiltin; + zDb = db->aDb[iDb].zDbSName; /* Get a NULL terminated version of the new table name. */ zName = sqlite3NameFromToken(db, pName); @@ -429,8 +152,11 @@ void sqlite3AlterRenameTable( /* Check that a table or index named 'zName' does not already exist ** in database iDb. If so, this is an error. */ - if( sqlite3FindTable(db, zName, zDb) || sqlite3FindIndex(db, zName, zDb) ){ - sqlite3ErrorMsg(pParse, + if( sqlite3FindTable(db, zName, zDb) + || sqlite3FindIndex(db, zName, zDb) + || sqlite3IsShadowTableOf(db, pTab, zName) + ){ + sqlite3ErrorMsg(pParse, "there is already another table or index with this name: %s", zName); goto exit_rename_table; } @@ -438,15 +164,15 @@ void sqlite3AlterRenameTable( /* Make sure it is not a system table being altered, or a reserved name ** that the table is being renamed to. */ - if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){ + if( SQLITE_OK!=isAlterableTable(pParse, pTab) ){ goto exit_rename_table; } - if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ goto - exit_rename_table; + if( SQLITE_OK!=sqlite3CheckObjectName(pParse,zName,"table",zName) ){ + goto exit_rename_table; } #ifndef SQLITE_OMIT_VIEW - if( pTab->pSelect ){ + if( IsView(pTab) ){ sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName); goto exit_rename_table; } @@ -471,78 +197,50 @@ void sqlite3AlterRenameTable( } #endif - /* Begin a transaction for database iDb. - ** Then modify the schema cookie (since the ALTER TABLE modifies the - ** schema). Open a statement transaction if the table is a virtual - ** table. - */ + /* Begin a transaction for database iDb. Then modify the schema cookie + ** (since the ALTER TABLE modifies the schema). Call sqlite3MayAbort(), + ** as the scalar functions (e.g. sqlite_rename_table()) invoked by the + ** nested SQL may raise an exception. */ v = sqlite3GetVdbe(pParse); if( v==0 ){ goto exit_rename_table; } - sqlite3BeginWriteOperation(pParse, pVTab!=0, iDb); - sqlite3ChangeCookie(pParse, iDb); - - /* If this is a virtual table, invoke the xRename() function if - ** one is defined. The xRename() callback will modify the names - ** of any resources used by the v-table implementation (including other - ** SQLite tables) that are identified by the name of the virtual table. - */ -#ifndef SQLITE_OMIT_VIRTUALTABLE - if( pVTab ){ - int i = ++pParse->nMem; - sqlite3VdbeLoadString(v, i, zName); - sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pVTab, P4_VTAB); - sqlite3MayAbort(pParse); - } -#endif + sqlite3MayAbort(pParse); /* figure out how many UTF-8 characters are in zName */ zTabName = pTab->zName; nTabName = sqlite3Utf8CharLen(zTabName, -1); -#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER) - if( db->flags&SQLITE_ForeignKeys ){ - /* If foreign-key support is enabled, rewrite the CREATE TABLE - ** statements corresponding to all child tables of foreign key constraints - ** for which the renamed table is the parent table. */ - if( (zWhere=whereForeignKeys(pParse, pTab))!=0 ){ - sqlite3NestedParse(pParse, - "UPDATE \"%w\".%s SET " - "sql = sqlite_rename_parent(sql, %Q, %Q) " - "WHERE %s;", zDb, SCHEMA_TABLE(iDb), zTabName, zName, zWhere); - sqlite3DbFree(db, zWhere); - } - } -#endif + /* Rewrite all CREATE TABLE, INDEX, TRIGGER or VIEW statements in + ** the schema to use the new table name. */ + sqlite3NestedParse(pParse, + "UPDATE \"%w\"." LEGACY_SCHEMA_TABLE " SET " + "sql = sqlite_rename_table(%Q, type, name, sql, %Q, %Q, %d) " + "WHERE (type!='index' OR tbl_name=%Q COLLATE nocase)" + "AND name NOT LIKE 'sqliteX_%%' ESCAPE 'X'" + , zDb, zDb, zTabName, zName, (iDb==1), zTabName + ); - /* Modify the sqlite_master table to use the new table name. */ + /* Update the tbl_name and name columns of the sqlite_schema table + ** as required. */ sqlite3NestedParse(pParse, - "UPDATE %Q.%s SET " -#ifdef SQLITE_OMIT_TRIGGER - "sql = sqlite_rename_table(sql, %Q), " -#else - "sql = CASE " - "WHEN type = 'trigger' THEN sqlite_rename_trigger(sql, %Q)" - "ELSE sqlite_rename_table(sql, %Q) END, " -#endif + "UPDATE %Q." LEGACY_SCHEMA_TABLE " SET " "tbl_name = %Q, " "name = CASE " "WHEN type='table' THEN %Q " - "WHEN name LIKE 'sqlite_autoindex%%' AND type='index' THEN " + "WHEN name LIKE 'sqliteX_autoindex%%' ESCAPE 'X' " + " AND type='index' THEN " "'sqlite_autoindex_' || %Q || substr(name,%d+18) " "ELSE name END " "WHERE tbl_name=%Q COLLATE nocase AND " - "(type='table' OR type='index' OR type='trigger');", - zDb, SCHEMA_TABLE(iDb), zName, zName, zName, -#ifndef SQLITE_OMIT_TRIGGER - zName, -#endif - zName, nTabName, zTabName + "(type='table' OR type='index' OR type='trigger');", + zDb, + zName, zName, zName, + nTabName, zTabName ); #ifndef SQLITE_OMIT_AUTOINCREMENT - /* If the sqlite_sequence table exists in this database, then update + /* If the sqlite_sequence table exists in this database, then update ** it with the new table name. */ if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){ @@ -552,40 +250,56 @@ void sqlite3AlterRenameTable( } #endif -#ifndef SQLITE_OMIT_TRIGGER - /* If there are TEMP triggers on this table, modify the sqlite_temp_master - ** table. Don't do this if the table being ALTERed is itself located in - ** the temp database. - */ - if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){ - sqlite3NestedParse(pParse, - "UPDATE sqlite_temp_master SET " - "sql = sqlite_rename_trigger(sql, %Q), " - "tbl_name = %Q " - "WHERE %s;", zName, zName, zWhere); - sqlite3DbFree(db, zWhere); + /* If the table being renamed is not itself part of the temp database, + ** edit view and trigger definitions within the temp database + ** as required. */ + if( iDb!=1 ){ + sqlite3NestedParse(pParse, + "UPDATE sqlite_temp_schema SET " + "sql = sqlite_rename_table(%Q, type, name, sql, %Q, %Q, 1), " + "tbl_name = " + "CASE WHEN tbl_name=%Q COLLATE nocase AND " + " sqlite_rename_test(%Q, sql, type, name, 1, 'after rename', 0) " + "THEN %Q ELSE tbl_name END " + "WHERE type IN ('view', 'trigger')" + , zDb, zTabName, zName, zTabName, zDb, zName); } -#endif -#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER) - if( db->flags&SQLITE_ForeignKeys ){ - FKey *p; - for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){ - Table *pFrom = p->pFrom; - if( pFrom!=pTab ){ - reloadTableSchema(pParse, p->pFrom, pFrom->zName); - } - } + /* If this is a virtual table, invoke the xRename() function if + ** one is defined. The xRename() callback will modify the names + ** of any resources used by the v-table implementation (including other + ** SQLite tables) that are identified by the name of the virtual table. + */ +#ifndef SQLITE_OMIT_VIRTUALTABLE + if( pVTab ){ + int i = ++pParse->nMem; + sqlite3VdbeLoadString(v, i, zName); + sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pVTab, P4_VTAB); } #endif - /* Drop and reload the internal table schema. */ - reloadTableSchema(pParse, pTab, zName); + renameReloadSchema(pParse, iDb, INITFLAG_AlterRename); + renameTestSchema(pParse, zDb, iDb==1, "after rename", 0); exit_rename_table: sqlite3SrcListDelete(db, pSrc); sqlite3DbFree(db, zName); - db->flags = savedDbFlags; +} + +/* +** Write code that will raise an error if the table described by +** zDb and zTab is not empty. +*/ +static void sqlite3ErrorIfNotEmpty( + Parse *pParse, /* Parsing context */ + const char *zDb, /* Schema holding the table */ + const char *zTab, /* Table to check for empty */ + const char *zErr /* Error message text */ +){ + sqlite3NestedParse(pParse, + "SELECT raise(ABORT,%Q) FROM \"%w\".\"%w\"", + zErr, zDb, zTab + ); } /* @@ -606,20 +320,22 @@ void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){ Column *pCol; /* The new column */ Expr *pDflt; /* Default value for the new column */ sqlite3 *db; /* The database connection; */ - Vdbe *v = pParse->pVdbe; /* The prepared statement under construction */ + Vdbe *v; /* The prepared statement under construction */ + int r1; /* Temporary registers */ db = pParse->db; - if( pParse->nErr || db->mallocFailed ) return; - assert( v!=0 ); + assert( db->pParse==pParse ); + if( pParse->nErr ) return; + assert( db->mallocFailed==0 ); pNew = pParse->pNewTable; assert( pNew ); assert( sqlite3BtreeHoldsAllMutexes(db) ); iDb = sqlite3SchemaToIndex(db, pNew->pSchema); - zDb = db->aDb[iDb].zName; + zDb = db->aDb[iDb].zDbSName; zTab = &pNew->zName[16]; /* Skip the "sqlite_altertab_" prefix on the name */ pCol = &pNew->aCol[pNew->nCol-1]; - pDflt = pCol->pDflt; + pDflt = sqlite3ColumnExpr(pNew, pCol); pTab = sqlite3FindTable(db, zTab, zDb); assert( pTab ); @@ -630,13 +346,6 @@ void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){ } #endif - /* If the default value for the new column was specified with a - ** literal NULL, then set pDflt to 0. This simplifies checking - ** for an SQL NULL default below. - */ - if( pDflt && pDflt->op==TK_NULL ){ - pDflt = 0; - } /* Check that the new column is not specified as PRIMARY KEY or UNIQUE. ** If there is a NOT NULL constraint, then the default value for the @@ -647,84 +356,125 @@ void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){ return; } if( pNew->pIndex ){ - sqlite3ErrorMsg(pParse, "Cannot add a UNIQUE column"); - return; - } - if( (db->flags&SQLITE_ForeignKeys) && pNew->pFKey && pDflt ){ - sqlite3ErrorMsg(pParse, - "Cannot add a REFERENCES column with non-NULL default value"); - return; - } - if( pCol->notNull && !pDflt ){ - sqlite3ErrorMsg(pParse, - "Cannot add a NOT NULL column with default value NULL"); + sqlite3ErrorMsg(pParse, + "Cannot add a UNIQUE column"); return; } - - /* Ensure the default expression is something that sqlite3ValueFromExpr() - ** can handle (i.e. not CURRENT_TIME etc.) - */ - if( pDflt ){ - sqlite3_value *pVal = 0; - int rc; - rc = sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_BLOB, &pVal); - assert( rc==SQLITE_OK || rc==SQLITE_NOMEM ); - if( rc!=SQLITE_OK ){ - assert( db->mallocFailed == 1 ); - return; + if( (pCol->colFlags & COLFLAG_GENERATED)==0 ){ + /* If the default value for the new column was specified with a + ** literal NULL, then set pDflt to 0. This simplifies checking + ** for an SQL NULL default below. + */ + assert( pDflt==0 || pDflt->op==TK_SPAN ); + if( pDflt && pDflt->pLeft->op==TK_NULL ){ + pDflt = 0; } - if( !pVal ){ - sqlite3ErrorMsg(pParse, "Cannot add a column with non-constant default"); - return; + assert( IsOrdinaryTable(pNew) ); + if( (db->flags&SQLITE_ForeignKeys) && pNew->u.tab.pFKey && pDflt ){ + sqlite3ErrorIfNotEmpty(pParse, zDb, zTab, + "Cannot add a REFERENCES column with non-NULL default value"); + } + if( pCol->notNull && !pDflt ){ + sqlite3ErrorIfNotEmpty(pParse, zDb, zTab, + "Cannot add a NOT NULL column with default value NULL"); + } + + + /* Ensure the default expression is something that sqlite3ValueFromExpr() + ** can handle (i.e. not CURRENT_TIME etc.) + */ + if( pDflt ){ + sqlite3_value *pVal = 0; + int rc; + rc = sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_BLOB, &pVal); + assert( rc==SQLITE_OK || rc==SQLITE_NOMEM ); + if( rc!=SQLITE_OK ){ + assert( db->mallocFailed == 1 ); + return; + } + if( !pVal ){ + sqlite3ErrorIfNotEmpty(pParse, zDb, zTab, + "Cannot add a column with non-constant default"); + } + sqlite3ValueFree(pVal); } - sqlite3ValueFree(pVal); + }else if( pCol->colFlags & COLFLAG_STORED ){ + sqlite3ErrorIfNotEmpty(pParse, zDb, zTab, "cannot add a STORED column"); } + /* Modify the CREATE TABLE statement. */ zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n); if( zCol ){ char *zEnd = &zCol[pColDef->n-1]; - int savedDbFlags = db->flags; while( zEnd>zCol && (*zEnd==';' || sqlite3Isspace(*zEnd)) ){ *zEnd-- = '\0'; } - db->flags |= SQLITE_PreferBuiltin; - sqlite3NestedParse(pParse, - "UPDATE \"%w\".%s SET " - "sql = substr(sql,1,%d) || ', ' || %Q || substr(sql,%d) " - "WHERE type = 'table' AND name = %Q", - zDb, SCHEMA_TABLE(iDb), pNew->addColOffset, zCol, pNew->addColOffset+1, + /* substr() operations on characters, but addColOffset is in bytes. So we + ** have to use printf() to translate between these units: */ + assert( IsOrdinaryTable(pTab) ); + assert( IsOrdinaryTable(pNew) ); + sqlite3NestedParse(pParse, + "UPDATE \"%w\"." LEGACY_SCHEMA_TABLE " SET " + "sql = printf('%%.%ds, ',sql) || %Q" + " || substr(sql,1+length(printf('%%.%ds',sql))) " + "WHERE type = 'table' AND name = %Q", + zDb, pNew->u.tab.addColOffset, zCol, pNew->u.tab.addColOffset, zTab ); sqlite3DbFree(db, zCol); - db->flags = savedDbFlags; } - /* If the default value of the new column is NULL, then the file - ** format to 2. If the default value of the new column is not NULL, - ** the file format be 3. Back when this feature was first added - ** in 2006, we went to the trouble to upgrade the file format to the - ** minimum support values. But 10-years on, we can assume that all - ** extent versions of SQLite support file-format 4, so we always and - ** unconditionally upgrade to 4. - */ - sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, - SQLITE_MAX_FILE_FORMAT); - - /* Reload the schema of the modified table. */ - reloadTableSchema(pParse, pTab, pTab->zName); + v = sqlite3GetVdbe(pParse); + if( v ){ + /* Make sure the schema version is at least 3. But do not upgrade + ** from less than 3 to 4, as that will corrupt any preexisting DESC + ** index. + */ + r1 = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, r1, BTREE_FILE_FORMAT); + sqlite3VdbeUsesBtree(v, iDb); + sqlite3VdbeAddOp2(v, OP_AddImm, r1, -2); + sqlite3VdbeAddOp2(v, OP_IfPos, r1, sqlite3VdbeCurrentAddr(v)+2); + VdbeCoverage(v); + sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, 3); + sqlite3ReleaseTempReg(pParse, r1); + + /* Reload the table definition */ + renameReloadSchema(pParse, iDb, INITFLAG_AlterAdd); + + /* Verify that constraints are still satisfied */ + if( pNew->pCheck!=0 + || (pCol->notNull && (pCol->colFlags & COLFLAG_GENERATED)!=0) + || (pTab->tabFlags & TF_Strict)!=0 + ){ + sqlite3NestedParse(pParse, + "SELECT CASE WHEN quick_check GLOB 'CHECK*'" + " THEN raise(ABORT,'CHECK constraint failed')" + " WHEN quick_check GLOB 'non-* value in*'" + " THEN raise(ABORT,'type mismatch on DEFAULT')" + " ELSE raise(ABORT,'NOT NULL constraint failed')" + " END" + " FROM pragma_quick_check(%Q,%Q)" + " WHERE quick_check GLOB 'CHECK*'" + " OR quick_check GLOB 'NULL*'" + " OR quick_check GLOB 'non-* value in*'", + zTab, zDb + ); + } + } } /* ** This function is called by the parser after the table-name in -** an "ALTER TABLE ADD" statement is parsed. Argument +** an "ALTER TABLE ADD" statement is parsed. Argument ** pSrc is the full-name of the table being altered. ** ** This routine makes a (partial) copy of the Table structure ** for the table being altered and sets Parse.pNewTable to point ** to it. Routines called by the parser as the column definition -** is parsed (i.e. sqlite3AddColumn()) add the new Column data to -** the copy. The copy of the Table structure is deleted by tokenize.c +** is parsed (i.e. sqlite3AddColumn()) add the new Column data to +** the copy. The copy of the Table structure is deleted by tokenize.c ** after parsing is finished. ** ** Routine sqlite3AlterFinishAddColumn() will be called to complete @@ -733,7 +483,6 @@ void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){ void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){ Table *pNew; Table *pTab; - Vdbe *v; int iDb; int i; int nAlloc; @@ -754,15 +503,17 @@ void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){ #endif /* Make sure this is not an attempt to ALTER a view. */ - if( pTab->pSelect ){ + if( IsView(pTab) ){ sqlite3ErrorMsg(pParse, "Cannot add a column to a view"); goto exit_begin_add_column; } - if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){ + if( SQLITE_OK!=isAlterableTable(pParse, pTab) ){ goto exit_begin_add_column; } - assert( pTab->addColOffset>0 ); + sqlite3MayAbort(pParse); + assert( IsOrdinaryTable(pTab) ); + assert( pTab->u.tab.addColOffset>0 ); iDb = sqlite3SchemaToIndex(db, pTab->pSchema); /* Put a copy of the Table struct in Parse.pNewTable for the @@ -775,38 +526,1804 @@ void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){ pNew = (Table*)sqlite3DbMallocZero(db, sizeof(Table)); if( !pNew ) goto exit_begin_add_column; pParse->pNewTable = pNew; - pNew->nRef = 1; + pNew->nTabRef = 1; pNew->nCol = pTab->nCol; assert( pNew->nCol>0 ); nAlloc = (((pNew->nCol-1)/8)*8)+8; assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 ); - pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc); + pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*(u32)nAlloc); pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName); if( !pNew->aCol || !pNew->zName ){ assert( db->mallocFailed ); goto exit_begin_add_column; } - memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol); + memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*(size_t)pNew->nCol); for(i=0; inCol; i++){ Column *pCol = &pNew->aCol[i]; - pCol->zName = sqlite3DbStrDup(db, pCol->zName); - pCol->zColl = 0; - pCol->zType = 0; - pCol->pDflt = 0; - pCol->zDflt = 0; + pCol->zCnName = sqlite3DbStrDup(db, pCol->zCnName); + pCol->hName = sqlite3StrIHash(pCol->zCnName); } + assert( IsOrdinaryTable(pNew) ); + pNew->u.tab.pDfltList = sqlite3ExprListDup(db, pTab->u.tab.pDfltList, 0); pNew->pSchema = db->aDb[iDb].pSchema; - pNew->addColOffset = pTab->addColOffset; - pNew->nRef = 1; - - /* Begin a transaction and increment the schema cookie. */ - sqlite3BeginWriteOperation(pParse, 0, iDb); - v = sqlite3GetVdbe(pParse); - if( !v ) goto exit_begin_add_column; - sqlite3ChangeCookie(pParse, iDb); + pNew->u.tab.addColOffset = pTab->u.tab.addColOffset; + assert( pNew->nTabRef==1 ); exit_begin_add_column: sqlite3SrcListDelete(db, pSrc); return; } + +/* +** Parameter pTab is the subject of an ALTER TABLE ... RENAME COLUMN +** command. This function checks if the table is a view or virtual +** table (columns of views or virtual tables may not be renamed). If so, +** it loads an error message into pParse and returns non-zero. +** +** Or, if pTab is not a view or virtual table, zero is returned. +*/ +#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) +static int isRealTable(Parse *pParse, Table *pTab, int bDrop){ + const char *zType = 0; +#ifndef SQLITE_OMIT_VIEW + if( IsView(pTab) ){ + zType = "view"; + } +#endif +#ifndef SQLITE_OMIT_VIRTUALTABLE + if( IsVirtual(pTab) ){ + zType = "virtual table"; + } +#endif + if( zType ){ + sqlite3ErrorMsg(pParse, "cannot %s %s \"%s\"", + (bDrop ? "drop column from" : "rename columns of"), + zType, pTab->zName + ); + return 1; + } + return 0; +} +#else /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */ +# define isRealTable(x,y,z) (0) +#endif + +/* +** Handles the following parser reduction: +** +** cmd ::= ALTER TABLE pSrc RENAME COLUMN pOld TO pNew +*/ +void sqlite3AlterRenameColumn( + Parse *pParse, /* Parsing context */ + SrcList *pSrc, /* Table being altered. pSrc->nSrc==1 */ + Token *pOld, /* Name of column being changed */ + Token *pNew /* New column name */ +){ + sqlite3 *db = pParse->db; /* Database connection */ + Table *pTab; /* Table being updated */ + int iCol; /* Index of column being renamed */ + char *zOld = 0; /* Old column name */ + char *zNew = 0; /* New column name */ + const char *zDb; /* Name of schema containing the table */ + int iSchema; /* Index of the schema */ + int bQuote; /* True to quote the new name */ + + /* Locate the table to be altered */ + pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]); + if( !pTab ) goto exit_rename_column; + + /* Cannot alter a system table */ + if( SQLITE_OK!=isAlterableTable(pParse, pTab) ) goto exit_rename_column; + if( SQLITE_OK!=isRealTable(pParse, pTab, 0) ) goto exit_rename_column; + + /* Which schema holds the table to be altered */ + iSchema = sqlite3SchemaToIndex(db, pTab->pSchema); + assert( iSchema>=0 ); + zDb = db->aDb[iSchema].zDbSName; + +#ifndef SQLITE_OMIT_AUTHORIZATION + /* Invoke the authorization callback. */ + if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){ + goto exit_rename_column; + } +#endif + + /* Make sure the old name really is a column name in the table to be + ** altered. Set iCol to be the index of the column being renamed */ + zOld = sqlite3NameFromToken(db, pOld); + if( !zOld ) goto exit_rename_column; + iCol = sqlite3ColumnIndex(pTab, zOld); + if( iCol<0 ){ + sqlite3ErrorMsg(pParse, "no such column: \"%T\"", pOld); + goto exit_rename_column; + } + + /* Ensure the schema contains no double-quoted strings */ + renameTestSchema(pParse, zDb, iSchema==1, "", 0); + renameFixQuotes(pParse, zDb, iSchema==1); + + /* Do the rename operation using a recursive UPDATE statement that + ** uses the sqlite_rename_column() SQL function to compute the new + ** CREATE statement text for the sqlite_schema table. + */ + sqlite3MayAbort(pParse); + zNew = sqlite3NameFromToken(db, pNew); + if( !zNew ) goto exit_rename_column; + assert( pNew->n>0 ); + bQuote = sqlite3Isquote(pNew->z[0]); + sqlite3NestedParse(pParse, + "UPDATE \"%w\"." LEGACY_SCHEMA_TABLE " SET " + "sql = sqlite_rename_column(sql, type, name, %Q, %Q, %d, %Q, %d, %d) " + "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X' " + " AND (type != 'index' OR tbl_name = %Q)", + zDb, + zDb, pTab->zName, iCol, zNew, bQuote, iSchema==1, + pTab->zName + ); + + sqlite3NestedParse(pParse, + "UPDATE temp." LEGACY_SCHEMA_TABLE " SET " + "sql = sqlite_rename_column(sql, type, name, %Q, %Q, %d, %Q, %d, 1) " + "WHERE type IN ('trigger', 'view')", + zDb, pTab->zName, iCol, zNew, bQuote + ); + + /* Drop and reload the database schema. */ + renameReloadSchema(pParse, iSchema, INITFLAG_AlterRename); + renameTestSchema(pParse, zDb, iSchema==1, "after rename", 1); + + exit_rename_column: + sqlite3SrcListDelete(db, pSrc); + sqlite3DbFree(db, zOld); + sqlite3DbFree(db, zNew); + return; +} + +/* +** Each RenameToken object maps an element of the parse tree into +** the token that generated that element. The parse tree element +** might be one of: +** +** * A pointer to an Expr that represents an ID +** * The name of a table column in Column.zName +** +** A list of RenameToken objects can be constructed during parsing. +** Each new object is created by sqlite3RenameTokenMap(). +** As the parse tree is transformed, the sqlite3RenameTokenRemap() +** routine is used to keep the mapping current. +** +** After the parse finishes, renameTokenFind() routine can be used +** to look up the actual token value that created some element in +** the parse tree. +*/ +struct RenameToken { + const void *p; /* Parse tree element created by token t */ + Token t; /* The token that created parse tree element p */ + RenameToken *pNext; /* Next is a list of all RenameToken objects */ +}; + +/* +** The context of an ALTER TABLE RENAME COLUMN operation that gets passed +** down into the Walker. +*/ +typedef struct RenameCtx RenameCtx; +struct RenameCtx { + RenameToken *pList; /* List of tokens to overwrite */ + int nList; /* Number of tokens in pList */ + int iCol; /* Index of column being renamed */ + Table *pTab; /* Table being ALTERed */ + const char *zOld; /* Old column name */ +}; + +#ifdef SQLITE_DEBUG +/* +** This function is only for debugging. It performs two tasks: +** +** 1. Checks that pointer pPtr does not already appear in the +** rename-token list. +** +** 2. Dereferences each pointer in the rename-token list. +** +** The second is most effective when debugging under valgrind or +** address-sanitizer or similar. If any of these pointers no longer +** point to valid objects, an exception is raised by the memory-checking +** tool. +** +** The point of this is to prevent comparisons of invalid pointer values. +** Even though this always seems to work, it is undefined according to the +** C standard. Example of undefined comparison: +** +** sqlite3_free(x); +** if( x==y ) ... +** +** Technically, as x no longer points into a valid object or to the byte +** following a valid object, it may not be used in comparison operations. +*/ +static void renameTokenCheckAll(Parse *pParse, const void *pPtr){ + assert( pParse==pParse->db->pParse ); + assert( pParse->db->mallocFailed==0 || pParse->nErr!=0 ); + if( pParse->nErr==0 ){ + const RenameToken *p; + u32 i = 1; + for(p=pParse->pRename; p; p=p->pNext){ + if( p->p ){ + assert( p->p!=pPtr ); + i += *(u8*)(p->p) | 1; + } + } + assert( i>0 ); + } +} +#else +# define renameTokenCheckAll(x,y) +#endif + +/* +** Remember that the parser tree element pPtr was created using +** the token pToken. +** +** In other words, construct a new RenameToken object and add it +** to the list of RenameToken objects currently being built up +** in pParse->pRename. +** +** The pPtr argument is returned so that this routine can be used +** with tail recursion in tokenExpr() routine, for a small performance +** improvement. +*/ +const void *sqlite3RenameTokenMap( + Parse *pParse, + const void *pPtr, + const Token *pToken +){ + RenameToken *pNew; + assert( pPtr || pParse->db->mallocFailed ); + renameTokenCheckAll(pParse, pPtr); + if( ALWAYS(pParse->eParseMode!=PARSE_MODE_UNMAP) ){ + pNew = sqlite3DbMallocZero(pParse->db, sizeof(RenameToken)); + if( pNew ){ + pNew->p = pPtr; + pNew->t = *pToken; + pNew->pNext = pParse->pRename; + pParse->pRename = pNew; + } + } + + return pPtr; +} + +/* +** It is assumed that there is already a RenameToken object associated +** with parse tree element pFrom. This function remaps the associated token +** to parse tree element pTo. +*/ +void sqlite3RenameTokenRemap(Parse *pParse, const void *pTo, const void *pFrom){ + RenameToken *p; + renameTokenCheckAll(pParse, pTo); + for(p=pParse->pRename; p; p=p->pNext){ + if( p->p==pFrom ){ + p->p = pTo; + break; + } + } +} + +/* +** Walker callback used by sqlite3RenameExprUnmap(). +*/ +static int renameUnmapExprCb(Walker *pWalker, Expr *pExpr){ + Parse *pParse = pWalker->pParse; + sqlite3RenameTokenRemap(pParse, 0, (const void*)pExpr); + if( ExprUseYTab(pExpr) ){ + sqlite3RenameTokenRemap(pParse, 0, (const void*)&pExpr->y.pTab); + } + return WRC_Continue; +} + +/* +** Iterate through the Select objects that are part of WITH clauses attached +** to select statement pSelect. +*/ +static void renameWalkWith(Walker *pWalker, Select *pSelect){ + With *pWith = pSelect->pWith; + if( pWith ){ + Parse *pParse = pWalker->pParse; + int i; + With *pCopy = 0; + assert( pWith->nCte>0 ); + if( (pWith->a[0].pSelect->selFlags & SF_Expanded)==0 ){ + /* Push a copy of the With object onto the with-stack. We use a copy + ** here as the original will be expanded and resolved (flags SF_Expanded + ** and SF_Resolved) below. And the parser code that uses the with-stack + ** fails if the Select objects on it have already been expanded and + ** resolved. */ + pCopy = sqlite3WithDup(pParse->db, pWith); + pCopy = sqlite3WithPush(pParse, pCopy, 1); + } + for(i=0; inCte; i++){ + Select *p = pWith->a[i].pSelect; + NameContext sNC; + memset(&sNC, 0, sizeof(sNC)); + sNC.pParse = pParse; + if( pCopy ) sqlite3SelectPrep(sNC.pParse, p, &sNC); + if( sNC.pParse->db->mallocFailed ) return; + sqlite3WalkSelect(pWalker, p); + sqlite3RenameExprlistUnmap(pParse, pWith->a[i].pCols); + } + if( pCopy && pParse->pWith==pCopy ){ + pParse->pWith = pCopy->pOuter; + } + } +} + +/* +** Unmap all tokens in the IdList object passed as the second argument. +*/ +static void unmapColumnIdlistNames( + Parse *pParse, + const IdList *pIdList +){ + int ii; + assert( pIdList!=0 ); + for(ii=0; iinId; ii++){ + sqlite3RenameTokenRemap(pParse, 0, (const void*)pIdList->a[ii].zName); + } +} + +/* +** Walker callback used by sqlite3RenameExprUnmap(). +*/ +static int renameUnmapSelectCb(Walker *pWalker, Select *p){ + Parse *pParse = pWalker->pParse; + int i; + if( pParse->nErr ) return WRC_Abort; + testcase( p->selFlags & SF_View ); + testcase( p->selFlags & SF_CopyCte ); + if( p->selFlags & (SF_View|SF_CopyCte) ){ + return WRC_Prune; + } + if( ALWAYS(p->pEList) ){ + ExprList *pList = p->pEList; + for(i=0; inExpr; i++){ + if( pList->a[i].zEName && pList->a[i].fg.eEName==ENAME_NAME ){ + sqlite3RenameTokenRemap(pParse, 0, (void*)pList->a[i].zEName); + } + } + } + if( ALWAYS(p->pSrc) ){ /* Every Select as a SrcList, even if it is empty */ + SrcList *pSrc = p->pSrc; + for(i=0; inSrc; i++){ + sqlite3RenameTokenRemap(pParse, 0, (void*)pSrc->a[i].zName); + if( pSrc->a[i].fg.isUsing==0 ){ + sqlite3WalkExpr(pWalker, pSrc->a[i].u3.pOn); + }else{ + unmapColumnIdlistNames(pParse, pSrc->a[i].u3.pUsing); + } + } + } + + renameWalkWith(pWalker, p); + return WRC_Continue; +} + +/* +** Remove all nodes that are part of expression pExpr from the rename list. +*/ +void sqlite3RenameExprUnmap(Parse *pParse, Expr *pExpr){ + u8 eMode = pParse->eParseMode; + Walker sWalker; + memset(&sWalker, 0, sizeof(Walker)); + sWalker.pParse = pParse; + sWalker.xExprCallback = renameUnmapExprCb; + sWalker.xSelectCallback = renameUnmapSelectCb; + pParse->eParseMode = PARSE_MODE_UNMAP; + sqlite3WalkExpr(&sWalker, pExpr); + pParse->eParseMode = eMode; +} + +/* +** Remove all nodes that are part of expression-list pEList from the +** rename list. +*/ +void sqlite3RenameExprlistUnmap(Parse *pParse, ExprList *pEList){ + if( pEList ){ + int i; + Walker sWalker; + memset(&sWalker, 0, sizeof(Walker)); + sWalker.pParse = pParse; + sWalker.xExprCallback = renameUnmapExprCb; + sqlite3WalkExprList(&sWalker, pEList); + for(i=0; inExpr; i++){ + if( ALWAYS(pEList->a[i].fg.eEName==ENAME_NAME) ){ + sqlite3RenameTokenRemap(pParse, 0, (void*)pEList->a[i].zEName); + } + } + } +} + +/* +** Free the list of RenameToken objects given in the second argument +*/ +static void renameTokenFree(sqlite3 *db, RenameToken *pToken){ + RenameToken *pNext; + RenameToken *p; + for(p=pToken; p; p=pNext){ + pNext = p->pNext; + sqlite3DbFree(db, p); + } +} + +/* +** Search the Parse object passed as the first argument for a RenameToken +** object associated with parse tree element pPtr. If found, return a pointer +** to it. Otherwise, return NULL. +** +** If the second argument passed to this function is not NULL and a matching +** RenameToken object is found, remove it from the Parse object and add it to +** the list maintained by the RenameCtx object. +*/ +static RenameToken *renameTokenFind( + Parse *pParse, + struct RenameCtx *pCtx, + const void *pPtr +){ + RenameToken **pp; + if( NEVER(pPtr==0) ){ + return 0; + } + for(pp=&pParse->pRename; (*pp); pp=&(*pp)->pNext){ + if( (*pp)->p==pPtr ){ + RenameToken *pToken = *pp; + if( pCtx ){ + *pp = pToken->pNext; + pToken->pNext = pCtx->pList; + pCtx->pList = pToken; + pCtx->nList++; + } + return pToken; + } + } + return 0; +} + +/* +** This is a Walker select callback. It does nothing. It is only required +** because without a dummy callback, sqlite3WalkExpr() and similar do not +** descend into sub-select statements. +*/ +static int renameColumnSelectCb(Walker *pWalker, Select *p){ + if( p->selFlags & (SF_View|SF_CopyCte) ){ + testcase( p->selFlags & SF_View ); + testcase( p->selFlags & SF_CopyCte ); + return WRC_Prune; + } + renameWalkWith(pWalker, p); + return WRC_Continue; +} + +/* +** This is a Walker expression callback. +** +** For every TK_COLUMN node in the expression tree, search to see +** if the column being references is the column being renamed by an +** ALTER TABLE statement. If it is, then attach its associated +** RenameToken object to the list of RenameToken objects being +** constructed in RenameCtx object at pWalker->u.pRename. +*/ +static int renameColumnExprCb(Walker *pWalker, Expr *pExpr){ + RenameCtx *p = pWalker->u.pRename; + if( pExpr->op==TK_TRIGGER + && pExpr->iColumn==p->iCol + && pWalker->pParse->pTriggerTab==p->pTab + ){ + renameTokenFind(pWalker->pParse, p, (void*)pExpr); + }else if( pExpr->op==TK_COLUMN + && pExpr->iColumn==p->iCol + && ALWAYS(ExprUseYTab(pExpr)) + && p->pTab==pExpr->y.pTab + ){ + renameTokenFind(pWalker->pParse, p, (void*)pExpr); + } + return WRC_Continue; +} + +/* +** The RenameCtx contains a list of tokens that reference a column that +** is being renamed by an ALTER TABLE statement. Return the "last" +** RenameToken in the RenameCtx and remove that RenameToken from the +** RenameContext. "Last" means the last RenameToken encountered when +** the input SQL is parsed from left to right. Repeated calls to this routine +** return all column name tokens in the order that they are encountered +** in the SQL statement. +*/ +static RenameToken *renameColumnTokenNext(RenameCtx *pCtx){ + RenameToken *pBest = pCtx->pList; + RenameToken *pToken; + RenameToken **pp; + + for(pToken=pBest->pNext; pToken; pToken=pToken->pNext){ + if( pToken->t.z>pBest->t.z ) pBest = pToken; + } + for(pp=&pCtx->pList; *pp!=pBest; pp=&(*pp)->pNext); + *pp = pBest->pNext; + + return pBest; +} + +/* +** An error occurred while parsing or otherwise processing a database +** object (either pParse->pNewTable, pNewIndex or pNewTrigger) as part of an +** ALTER TABLE RENAME COLUMN program. The error message emitted by the +** sub-routine is currently stored in pParse->zErrMsg. This function +** adds context to the error message and then stores it in pCtx. +*/ +static void renameColumnParseError( + sqlite3_context *pCtx, + const char *zWhen, + sqlite3_value *pType, + sqlite3_value *pObject, + Parse *pParse +){ + const char *zT = (const char*)sqlite3_value_text(pType); + const char *zN = (const char*)sqlite3_value_text(pObject); + char *zErr; + + zErr = sqlite3MPrintf(pParse->db, "error in %s %s%s%s: %s", + zT, zN, (zWhen[0] ? " " : ""), zWhen, + pParse->zErrMsg + ); + sqlite3_result_error(pCtx, zErr, -1); + sqlite3DbFree(pParse->db, zErr); +} + +/* +** For each name in the the expression-list pEList (i.e. each +** pEList->a[i].zName) that matches the string in zOld, extract the +** corresponding rename-token from Parse object pParse and add it +** to the RenameCtx pCtx. +*/ +static void renameColumnElistNames( + Parse *pParse, + RenameCtx *pCtx, + const ExprList *pEList, + const char *zOld +){ + if( pEList ){ + int i; + for(i=0; inExpr; i++){ + const char *zName = pEList->a[i].zEName; + if( ALWAYS(pEList->a[i].fg.eEName==ENAME_NAME) + && ALWAYS(zName!=0) + && 0==sqlite3_stricmp(zName, zOld) + ){ + renameTokenFind(pParse, pCtx, (const void*)zName); + } + } + } +} + +/* +** For each name in the the id-list pIdList (i.e. each pIdList->a[i].zName) +** that matches the string in zOld, extract the corresponding rename-token +** from Parse object pParse and add it to the RenameCtx pCtx. +*/ +static void renameColumnIdlistNames( + Parse *pParse, + RenameCtx *pCtx, + const IdList *pIdList, + const char *zOld +){ + if( pIdList ){ + int i; + for(i=0; inId; i++){ + const char *zName = pIdList->a[i].zName; + if( 0==sqlite3_stricmp(zName, zOld) ){ + renameTokenFind(pParse, pCtx, (const void*)zName); + } + } + } +} + + +/* +** Parse the SQL statement zSql using Parse object (*p). The Parse object +** is initialized by this function before it is used. +*/ +static int renameParseSql( + Parse *p, /* Memory to use for Parse object */ + const char *zDb, /* Name of schema SQL belongs to */ + sqlite3 *db, /* Database handle */ + const char *zSql, /* SQL to parse */ + int bTemp /* True if SQL is from temp schema */ +){ + int rc; + u64 flags; + + sqlite3ParseObjectInit(p, db); + if( zSql==0 ){ + return SQLITE_NOMEM; + } + if( sqlite3StrNICmp(zSql,"CREATE ",7)!=0 ){ + return SQLITE_CORRUPT_BKPT; + } + if( bTemp ){ + db->init.iDb = 1; + }else{ + int iDb = sqlite3FindDbName(db, zDb); + assert( iDb>=0 && iDb<=0xff ); + db->init.iDb = (u8)iDb; + } + p->eParseMode = PARSE_MODE_RENAME; + p->db = db; + p->nQueryLoop = 1; + flags = db->flags; + testcase( (db->flags & SQLITE_Comments)==0 && strstr(zSql," /* ")!=0 ); + db->flags |= SQLITE_Comments; + rc = sqlite3RunParser(p, zSql); + db->flags = flags; + if( db->mallocFailed ) rc = SQLITE_NOMEM; + if( rc==SQLITE_OK + && NEVER(p->pNewTable==0 && p->pNewIndex==0 && p->pNewTrigger==0) + ){ + rc = SQLITE_CORRUPT_BKPT; + } + +#ifdef SQLITE_DEBUG + /* Ensure that all mappings in the Parse.pRename list really do map to + ** a part of the input string. */ + if( rc==SQLITE_OK ){ + int nSql = sqlite3Strlen30(zSql); + RenameToken *pToken; + for(pToken=p->pRename; pToken; pToken=pToken->pNext){ + assert( pToken->t.z>=zSql && &pToken->t.z[pToken->t.n]<=&zSql[nSql] ); + } + } +#endif + + db->init.iDb = 0; + return rc; +} + +/* +** This function edits SQL statement zSql, replacing each token identified +** by the linked list pRename with the text of zNew. If argument bQuote is +** true, then zNew is always quoted first. If no error occurs, the result +** is loaded into context object pCtx as the result. +** +** Or, if an error occurs (i.e. an OOM condition), an error is left in +** pCtx and an SQLite error code returned. +*/ +static int renameEditSql( + sqlite3_context *pCtx, /* Return result here */ + RenameCtx *pRename, /* Rename context */ + const char *zSql, /* SQL statement to edit */ + const char *zNew, /* New token text */ + int bQuote /* True to always quote token */ +){ + i64 nNew = sqlite3Strlen30(zNew); + i64 nSql = sqlite3Strlen30(zSql); + sqlite3 *db = sqlite3_context_db_handle(pCtx); + int rc = SQLITE_OK; + char *zQuot = 0; + char *zOut; + i64 nQuot = 0; + char *zBuf1 = 0; + char *zBuf2 = 0; + + if( zNew ){ + /* Set zQuot to point to a buffer containing a quoted copy of the + ** identifier zNew. If the corresponding identifier in the original + ** ALTER TABLE statement was quoted (bQuote==1), then set zNew to + ** point to zQuot so that all substitutions are made using the + ** quoted version of the new column name. */ + zQuot = sqlite3MPrintf(db, "\"%w\" ", zNew); + if( zQuot==0 ){ + return SQLITE_NOMEM; + }else{ + nQuot = sqlite3Strlen30(zQuot)-1; + } + + assert( nQuot>=nNew && nSql>=0 && nNew>=0 ); + zOut = sqlite3DbMallocZero(db, (u64)nSql + pRename->nList*(u64)nQuot + 1); + }else{ + assert( nSql>0 ); + zOut = (char*)sqlite3DbMallocZero(db, (2*(u64)nSql + 1) * 3); + if( zOut ){ + zBuf1 = &zOut[nSql*2+1]; + zBuf2 = &zOut[nSql*4+2]; + } + } + + /* At this point pRename->pList contains a list of RenameToken objects + ** corresponding to all tokens in the input SQL that must be replaced + ** with the new column name, or with single-quoted versions of themselves. + ** All that remains is to construct and return the edited SQL string. */ + if( zOut ){ + i64 nOut = nSql; + assert( nSql>0 ); + memcpy(zOut, zSql, (size_t)nSql); + while( pRename->pList ){ + int iOff; /* Offset of token to replace in zOut */ + i64 nReplace; + const char *zReplace; + RenameToken *pBest = renameColumnTokenNext(pRename); + + if( zNew ){ + if( bQuote==0 && sqlite3IsIdChar(*(u8*)pBest->t.z) ){ + nReplace = nNew; + zReplace = zNew; + }else{ + nReplace = nQuot; + zReplace = zQuot; + if( pBest->t.z[pBest->t.n]=='"' ) nReplace++; + } + }else{ + /* Dequote the double-quoted token. Then requote it again, this time + ** using single quotes. If the character immediately following the + ** original token within the input SQL was a single quote ('), then + ** add another space after the new, single-quoted version of the + ** token. This is so that (SELECT "string"'alias') maps to + ** (SELECT 'string' 'alias'), and not (SELECT 'string''alias'). */ + memcpy(zBuf1, pBest->t.z, pBest->t.n); + zBuf1[pBest->t.n] = 0; + sqlite3Dequote(zBuf1); + assert( nSql < 0x15555554 /* otherwise malloc would have failed */ ); + sqlite3_snprintf((int)(nSql*2), zBuf2, "%Q%s", zBuf1, + pBest->t.z[pBest->t.n]=='\'' ? " " : "" + ); + zReplace = zBuf2; + nReplace = sqlite3Strlen30(zReplace); + } + + iOff = (int)(pBest->t.z - zSql); + if( pBest->t.n!=nReplace ){ + memmove(&zOut[iOff + nReplace], &zOut[iOff + pBest->t.n], + nOut - (iOff + pBest->t.n) + ); + nOut += nReplace - pBest->t.n; + zOut[nOut] = '\0'; + } + memcpy(&zOut[iOff], zReplace, nReplace); + sqlite3DbFree(db, pBest); + } + + sqlite3_result_text(pCtx, zOut, -1, SQLITE_TRANSIENT); + sqlite3DbFree(db, zOut); + }else{ + rc = SQLITE_NOMEM; + } + + sqlite3_free(zQuot); + return rc; +} + +/* +** Set all pEList->a[].fg.eEName fields in the expression-list to val. +*/ +static void renameSetENames(ExprList *pEList, int val){ + assert( val==ENAME_NAME || val==ENAME_TAB || val==ENAME_SPAN ); + if( pEList ){ + int i; + for(i=0; inExpr; i++){ + assert( val==ENAME_NAME || pEList->a[i].fg.eEName==ENAME_NAME ); + pEList->a[i].fg.eEName = val&0x3; + } + } +} + +/* +** Resolve all symbols in the trigger at pParse->pNewTrigger, assuming +** it was read from the schema of database zDb. Return SQLITE_OK if +** successful. Otherwise, return an SQLite error code and leave an error +** message in the Parse object. +*/ +static int renameResolveTrigger(Parse *pParse){ + sqlite3 *db = pParse->db; + Trigger *pNew = pParse->pNewTrigger; + TriggerStep *pStep; + NameContext sNC; + int rc = SQLITE_OK; + + memset(&sNC, 0, sizeof(sNC)); + sNC.pParse = pParse; + assert( pNew->pTabSchema ); + pParse->pTriggerTab = sqlite3FindTable(db, pNew->table, + db->aDb[sqlite3SchemaToIndex(db, pNew->pTabSchema)].zDbSName + ); + pParse->eTriggerOp = pNew->op; + /* ALWAYS() because if the table of the trigger does not exist, the + ** error would have been hit before this point */ + if( ALWAYS(pParse->pTriggerTab) ){ + rc = sqlite3ViewGetColumnNames(pParse, pParse->pTriggerTab)!=0; + } + + /* Resolve symbols in WHEN clause */ + if( rc==SQLITE_OK && pNew->pWhen ){ + rc = sqlite3ResolveExprNames(&sNC, pNew->pWhen); + } + + for(pStep=pNew->step_list; rc==SQLITE_OK && pStep; pStep=pStep->pNext){ + if( pStep->pSelect ){ + sqlite3SelectPrep(pParse, pStep->pSelect, &sNC); + if( pParse->nErr ) rc = pParse->rc; + } + if( rc==SQLITE_OK && pStep->zTarget ){ + SrcList *pSrc = sqlite3TriggerStepSrc(pParse, pStep); + if( pSrc ){ + Select *pSel = sqlite3SelectNew( + pParse, pStep->pExprList, pSrc, 0, 0, 0, 0, 0, 0 + ); + if( pSel==0 ){ + pStep->pExprList = 0; + pSrc = 0; + rc = SQLITE_NOMEM; + }else{ + /* pStep->pExprList contains an expression-list used for an UPDATE + ** statement. So the a[].zEName values are the RHS of the + ** "= " clauses of the UPDATE statement. So, before + ** running SelectPrep(), change all the eEName values in + ** pStep->pExprList to ENAME_SPAN (from their current value of + ** ENAME_NAME). This is to prevent any ids in ON() clauses that are + ** part of pSrc from being incorrectly resolved against the + ** a[].zEName values as if they were column aliases. */ + renameSetENames(pStep->pExprList, ENAME_SPAN); + sqlite3SelectPrep(pParse, pSel, 0); + renameSetENames(pStep->pExprList, ENAME_NAME); + rc = pParse->nErr ? SQLITE_ERROR : SQLITE_OK; + assert( pStep->pExprList==0 || pStep->pExprList==pSel->pEList ); + assert( pSrc==pSel->pSrc ); + if( pStep->pExprList ) pSel->pEList = 0; + pSel->pSrc = 0; + sqlite3SelectDelete(db, pSel); + } + if( pStep->pFrom ){ + int i; + for(i=0; ipFrom->nSrc && rc==SQLITE_OK; i++){ + SrcItem *p = &pStep->pFrom->a[i]; + if( p->fg.isSubquery ){ + assert( p->u4.pSubq!=0 ); + sqlite3SelectPrep(pParse, p->u4.pSubq->pSelect, 0); + } + } + } + + if( db->mallocFailed ){ + rc = SQLITE_NOMEM; + } + sNC.pSrcList = pSrc; + if( rc==SQLITE_OK && pStep->pWhere ){ + rc = sqlite3ResolveExprNames(&sNC, pStep->pWhere); + } + if( rc==SQLITE_OK ){ + rc = sqlite3ResolveExprListNames(&sNC, pStep->pExprList); + } + assert( !pStep->pUpsert || (!pStep->pWhere && !pStep->pExprList) ); + if( pStep->pUpsert && rc==SQLITE_OK ){ + Upsert *pUpsert = pStep->pUpsert; + pUpsert->pUpsertSrc = pSrc; + sNC.uNC.pUpsert = pUpsert; + sNC.ncFlags = NC_UUpsert; + rc = sqlite3ResolveExprListNames(&sNC, pUpsert->pUpsertTarget); + if( rc==SQLITE_OK ){ + ExprList *pUpsertSet = pUpsert->pUpsertSet; + rc = sqlite3ResolveExprListNames(&sNC, pUpsertSet); + } + if( rc==SQLITE_OK ){ + rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertWhere); + } + if( rc==SQLITE_OK ){ + rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertTargetWhere); + } + sNC.ncFlags = 0; + } + sNC.pSrcList = 0; + sqlite3SrcListDelete(db, pSrc); + }else{ + rc = SQLITE_NOMEM; + } + } + } + return rc; +} + +/* +** Invoke sqlite3WalkExpr() or sqlite3WalkSelect() on all Select or Expr +** objects that are part of the trigger passed as the second argument. +*/ +static void renameWalkTrigger(Walker *pWalker, Trigger *pTrigger){ + TriggerStep *pStep; + + /* Find tokens to edit in WHEN clause */ + sqlite3WalkExpr(pWalker, pTrigger->pWhen); + + /* Find tokens to edit in trigger steps */ + for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){ + sqlite3WalkSelect(pWalker, pStep->pSelect); + sqlite3WalkExpr(pWalker, pStep->pWhere); + sqlite3WalkExprList(pWalker, pStep->pExprList); + if( pStep->pUpsert ){ + Upsert *pUpsert = pStep->pUpsert; + sqlite3WalkExprList(pWalker, pUpsert->pUpsertTarget); + sqlite3WalkExprList(pWalker, pUpsert->pUpsertSet); + sqlite3WalkExpr(pWalker, pUpsert->pUpsertWhere); + sqlite3WalkExpr(pWalker, pUpsert->pUpsertTargetWhere); + } + if( pStep->pFrom ){ + int i; + SrcList *pFrom = pStep->pFrom; + for(i=0; inSrc; i++){ + if( pFrom->a[i].fg.isSubquery ){ + assert( pFrom->a[i].u4.pSubq!=0 ); + sqlite3WalkSelect(pWalker, pFrom->a[i].u4.pSubq->pSelect); + } + } + } + } +} + +/* +** Free the contents of Parse object (*pParse). Do not free the memory +** occupied by the Parse object itself. +*/ +static void renameParseCleanup(Parse *pParse){ + sqlite3 *db = pParse->db; + Index *pIdx; + if( pParse->pVdbe ){ + sqlite3VdbeFinalize(pParse->pVdbe); + } + sqlite3DeleteTable(db, pParse->pNewTable); + while( (pIdx = pParse->pNewIndex)!=0 ){ + pParse->pNewIndex = pIdx->pNext; + sqlite3FreeIndex(db, pIdx); + } + sqlite3DeleteTrigger(db, pParse->pNewTrigger); + sqlite3DbFree(db, pParse->zErrMsg); + renameTokenFree(db, pParse->pRename); + sqlite3ParseObjectReset(pParse); +} + +/* +** SQL function: +** +** sqlite_rename_column(SQL,TYPE,OBJ,DB,TABLE,COL,NEWNAME,QUOTE,TEMP) +** +** 0. zSql: SQL statement to rewrite +** 1. type: Type of object ("table", "view" etc.) +** 2. object: Name of object +** 3. Database: Database name (e.g. "main") +** 4. Table: Table name +** 5. iCol: Index of column to rename +** 6. zNew: New column name +** 7. bQuote: Non-zero if the new column name should be quoted. +** 8. bTemp: True if zSql comes from temp schema +** +** Do a column rename operation on the CREATE statement given in zSql. +** The iCol-th column (left-most is 0) of table zTable is renamed from zCol +** into zNew. The name should be quoted if bQuote is true. +** +** This function is used internally by the ALTER TABLE RENAME COLUMN command. +** It is only accessible to SQL created using sqlite3NestedParse(). It is +** not reachable from ordinary SQL passed into sqlite3_prepare() unless the +** SQLITE_TESTCTRL_INTERNAL_FUNCTIONS test setting is enabled. +*/ +static void renameColumnFunc( + sqlite3_context *context, + int NotUsed, + sqlite3_value **argv +){ + sqlite3 *db = sqlite3_context_db_handle(context); + RenameCtx sCtx; + const char *zSql = (const char*)sqlite3_value_text(argv[0]); + const char *zDb = (const char*)sqlite3_value_text(argv[3]); + const char *zTable = (const char*)sqlite3_value_text(argv[4]); + int iCol = sqlite3_value_int(argv[5]); + const char *zNew = (const char*)sqlite3_value_text(argv[6]); + int bQuote = sqlite3_value_int(argv[7]); + int bTemp = sqlite3_value_int(argv[8]); + const char *zOld; + int rc; + Parse sParse; + Walker sWalker; + Index *pIdx; + int i; + Table *pTab; +#ifndef SQLITE_OMIT_AUTHORIZATION + sqlite3_xauth xAuth = db->xAuth; +#endif + + UNUSED_PARAMETER(NotUsed); + if( zSql==0 ) return; + if( zTable==0 ) return; + if( zNew==0 ) return; + if( iCol<0 ) return; + sqlite3BtreeEnterAll(db); + pTab = sqlite3FindTable(db, zTable, zDb); + if( pTab==0 || iCol>=pTab->nCol ){ + sqlite3BtreeLeaveAll(db); + return; + } + zOld = pTab->aCol[iCol].zCnName; + memset(&sCtx, 0, sizeof(sCtx)); + sCtx.iCol = ((iCol==pTab->iPKey) ? -1 : iCol); + +#ifndef SQLITE_OMIT_AUTHORIZATION + db->xAuth = 0; +#endif + rc = renameParseSql(&sParse, zDb, db, zSql, bTemp); + + /* Find tokens that need to be replaced. */ + memset(&sWalker, 0, sizeof(Walker)); + sWalker.pParse = &sParse; + sWalker.xExprCallback = renameColumnExprCb; + sWalker.xSelectCallback = renameColumnSelectCb; + sWalker.u.pRename = &sCtx; + + sCtx.pTab = pTab; + if( rc!=SQLITE_OK ) goto renameColumnFunc_done; + if( sParse.pNewTable ){ + if( IsView(sParse.pNewTable) ){ + Select *pSelect = sParse.pNewTable->u.view.pSelect; + pSelect->selFlags &= ~(u32)SF_View; + sParse.rc = SQLITE_OK; + sqlite3SelectPrep(&sParse, pSelect, 0); + rc = (db->mallocFailed ? SQLITE_NOMEM : sParse.rc); + if( rc==SQLITE_OK ){ + sqlite3WalkSelect(&sWalker, pSelect); + } + if( rc!=SQLITE_OK ) goto renameColumnFunc_done; + }else if( IsOrdinaryTable(sParse.pNewTable) ){ + /* A regular table */ + int bFKOnly = sqlite3_stricmp(zTable, sParse.pNewTable->zName); + FKey *pFKey; + sCtx.pTab = sParse.pNewTable; + if( bFKOnly==0 ){ + if( iColnCol ){ + renameTokenFind( + &sParse, &sCtx, (void*)sParse.pNewTable->aCol[iCol].zCnName + ); + } + if( sCtx.iCol<0 ){ + renameTokenFind(&sParse, &sCtx, (void*)&sParse.pNewTable->iPKey); + } + sqlite3WalkExprList(&sWalker, sParse.pNewTable->pCheck); + for(pIdx=sParse.pNewTable->pIndex; pIdx; pIdx=pIdx->pNext){ + sqlite3WalkExprList(&sWalker, pIdx->aColExpr); + } + for(pIdx=sParse.pNewIndex; pIdx; pIdx=pIdx->pNext){ + sqlite3WalkExprList(&sWalker, pIdx->aColExpr); + } +#ifndef SQLITE_OMIT_GENERATED_COLUMNS + for(i=0; inCol; i++){ + Expr *pExpr = sqlite3ColumnExpr(sParse.pNewTable, + &sParse.pNewTable->aCol[i]); + sqlite3WalkExpr(&sWalker, pExpr); + } +#endif + } + + assert( IsOrdinaryTable(sParse.pNewTable) ); + for(pFKey=sParse.pNewTable->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){ + for(i=0; inCol; i++){ + if( bFKOnly==0 && pFKey->aCol[i].iFrom==iCol ){ + renameTokenFind(&sParse, &sCtx, (void*)&pFKey->aCol[i]); + } + if( 0==sqlite3_stricmp(pFKey->zTo, zTable) + && 0==sqlite3_stricmp(pFKey->aCol[i].zCol, zOld) + ){ + renameTokenFind(&sParse, &sCtx, (void*)pFKey->aCol[i].zCol); + } + } + } + } + }else if( sParse.pNewIndex ){ + sqlite3WalkExprList(&sWalker, sParse.pNewIndex->aColExpr); + sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere); + }else{ + /* A trigger */ + TriggerStep *pStep; + rc = renameResolveTrigger(&sParse); + if( rc!=SQLITE_OK ) goto renameColumnFunc_done; + + for(pStep=sParse.pNewTrigger->step_list; pStep; pStep=pStep->pNext){ + if( pStep->zTarget ){ + Table *pTarget = sqlite3LocateTable(&sParse, 0, pStep->zTarget, zDb); + if( pTarget==pTab ){ + if( pStep->pUpsert ){ + ExprList *pUpsertSet = pStep->pUpsert->pUpsertSet; + renameColumnElistNames(&sParse, &sCtx, pUpsertSet, zOld); + } + renameColumnIdlistNames(&sParse, &sCtx, pStep->pIdList, zOld); + renameColumnElistNames(&sParse, &sCtx, pStep->pExprList, zOld); + } + } + } + + + /* Find tokens to edit in UPDATE OF clause */ + if( sParse.pTriggerTab==pTab ){ + renameColumnIdlistNames(&sParse, &sCtx,sParse.pNewTrigger->pColumns,zOld); + } + + /* Find tokens to edit in various expressions and selects */ + renameWalkTrigger(&sWalker, sParse.pNewTrigger); + } + + assert( rc==SQLITE_OK ); + rc = renameEditSql(context, &sCtx, zSql, zNew, bQuote); + +renameColumnFunc_done: + if( rc!=SQLITE_OK ){ + if( rc==SQLITE_ERROR && sqlite3WritableSchema(db) ){ + sqlite3_result_value(context, argv[0]); + }else if( sParse.zErrMsg ){ + renameColumnParseError(context, "", argv[1], argv[2], &sParse); + }else{ + sqlite3_result_error_code(context, rc); + } + } + + renameParseCleanup(&sParse); + renameTokenFree(db, sCtx.pList); +#ifndef SQLITE_OMIT_AUTHORIZATION + db->xAuth = xAuth; +#endif + sqlite3BtreeLeaveAll(db); +} + +/* +** Walker expression callback used by "RENAME TABLE". +*/ +static int renameTableExprCb(Walker *pWalker, Expr *pExpr){ + RenameCtx *p = pWalker->u.pRename; + if( pExpr->op==TK_COLUMN + && ALWAYS(ExprUseYTab(pExpr)) + && p->pTab==pExpr->y.pTab + ){ + renameTokenFind(pWalker->pParse, p, (void*)&pExpr->y.pTab); + } + return WRC_Continue; +} + +/* +** Walker select callback used by "RENAME TABLE". +*/ +static int renameTableSelectCb(Walker *pWalker, Select *pSelect){ + int i; + RenameCtx *p = pWalker->u.pRename; + SrcList *pSrc = pSelect->pSrc; + if( pSelect->selFlags & (SF_View|SF_CopyCte) ){ + testcase( pSelect->selFlags & SF_View ); + testcase( pSelect->selFlags & SF_CopyCte ); + return WRC_Prune; + } + if( NEVER(pSrc==0) ){ + assert( pWalker->pParse->db->mallocFailed ); + return WRC_Abort; + } + for(i=0; inSrc; i++){ + SrcItem *pItem = &pSrc->a[i]; + if( pItem->pSTab==p->pTab ){ + renameTokenFind(pWalker->pParse, p, pItem->zName); + } + } + renameWalkWith(pWalker, pSelect); + + return WRC_Continue; +} + + +/* +** This C function implements an SQL user function that is used by SQL code +** generated by the ALTER TABLE ... RENAME command to modify the definition +** of any foreign key constraints that use the table being renamed as the +** parent table. It is passed three arguments: +** +** 0: The database containing the table being renamed. +** 1. type: Type of object ("table", "view" etc.) +** 2. object: Name of object +** 3: The complete text of the schema statement being modified, +** 4: The old name of the table being renamed, and +** 5: The new name of the table being renamed. +** 6: True if the schema statement comes from the temp db. +** +** It returns the new schema statement. For example: +** +** sqlite_rename_table('main', 'CREATE TABLE t1(a REFERENCES t2)','t2','t3',0) +** -> 'CREATE TABLE t1(a REFERENCES t3)' +*/ +static void renameTableFunc( + sqlite3_context *context, + int NotUsed, + sqlite3_value **argv +){ + sqlite3 *db = sqlite3_context_db_handle(context); + const char *zDb = (const char*)sqlite3_value_text(argv[0]); + const char *zInput = (const char*)sqlite3_value_text(argv[3]); + const char *zOld = (const char*)sqlite3_value_text(argv[4]); + const char *zNew = (const char*)sqlite3_value_text(argv[5]); + int bTemp = sqlite3_value_int(argv[6]); + UNUSED_PARAMETER(NotUsed); + + if( zInput && zOld && zNew ){ + Parse sParse; + int rc; + int bQuote = 1; + RenameCtx sCtx; + Walker sWalker; + +#ifndef SQLITE_OMIT_AUTHORIZATION + sqlite3_xauth xAuth = db->xAuth; + db->xAuth = 0; +#endif + + sqlite3BtreeEnterAll(db); + + memset(&sCtx, 0, sizeof(RenameCtx)); + sCtx.pTab = sqlite3FindTable(db, zOld, zDb); + memset(&sWalker, 0, sizeof(Walker)); + sWalker.pParse = &sParse; + sWalker.xExprCallback = renameTableExprCb; + sWalker.xSelectCallback = renameTableSelectCb; + sWalker.u.pRename = &sCtx; + + rc = renameParseSql(&sParse, zDb, db, zInput, bTemp); + + if( rc==SQLITE_OK ){ + int isLegacy = (db->flags & SQLITE_LegacyAlter); + if( sParse.pNewTable ){ + Table *pTab = sParse.pNewTable; + + if( IsView(pTab) ){ + if( isLegacy==0 ){ + Select *pSelect = pTab->u.view.pSelect; + NameContext sNC; + memset(&sNC, 0, sizeof(sNC)); + sNC.pParse = &sParse; + + assert( pSelect->selFlags & SF_View ); + pSelect->selFlags &= ~(u32)SF_View; + sqlite3SelectPrep(&sParse, pTab->u.view.pSelect, &sNC); + if( sParse.nErr ){ + rc = sParse.rc; + }else{ + sqlite3WalkSelect(&sWalker, pTab->u.view.pSelect); + } + } + }else{ + /* Modify any FK definitions to point to the new table. */ +#ifndef SQLITE_OMIT_FOREIGN_KEY + if( (isLegacy==0 || (db->flags & SQLITE_ForeignKeys)) + && !IsVirtual(pTab) + ){ + FKey *pFKey; + assert( IsOrdinaryTable(pTab) ); + for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){ + if( sqlite3_stricmp(pFKey->zTo, zOld)==0 ){ + renameTokenFind(&sParse, &sCtx, (void*)pFKey->zTo); + } + } + } +#endif + + /* If this is the table being altered, fix any table refs in CHECK + ** expressions. Also update the name that appears right after the + ** "CREATE [VIRTUAL] TABLE" bit. */ + if( sqlite3_stricmp(zOld, pTab->zName)==0 ){ + sCtx.pTab = pTab; + if( isLegacy==0 ){ + sqlite3WalkExprList(&sWalker, pTab->pCheck); + } + renameTokenFind(&sParse, &sCtx, pTab->zName); + } + } + } + + else if( sParse.pNewIndex ){ + renameTokenFind(&sParse, &sCtx, sParse.pNewIndex->zName); + if( isLegacy==0 ){ + sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere); + } + } + +#ifndef SQLITE_OMIT_TRIGGER + else{ + Trigger *pTrigger = sParse.pNewTrigger; + TriggerStep *pStep; + if( 0==sqlite3_stricmp(sParse.pNewTrigger->table, zOld) + && sCtx.pTab->pSchema==pTrigger->pTabSchema + ){ + renameTokenFind(&sParse, &sCtx, sParse.pNewTrigger->table); + } + + if( isLegacy==0 ){ + rc = renameResolveTrigger(&sParse); + if( rc==SQLITE_OK ){ + renameWalkTrigger(&sWalker, pTrigger); + for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){ + if( pStep->zTarget && 0==sqlite3_stricmp(pStep->zTarget, zOld) ){ + renameTokenFind(&sParse, &sCtx, pStep->zTarget); + } + if( pStep->pFrom ){ + int i; + for(i=0; ipFrom->nSrc; i++){ + SrcItem *pItem = &pStep->pFrom->a[i]; + if( 0==sqlite3_stricmp(pItem->zName, zOld) ){ + renameTokenFind(&sParse, &sCtx, pItem->zName); + } + } + } + } + } + } + } +#endif + } + + if( rc==SQLITE_OK ){ + rc = renameEditSql(context, &sCtx, zInput, zNew, bQuote); + } + if( rc!=SQLITE_OK ){ + if( rc==SQLITE_ERROR && sqlite3WritableSchema(db) ){ + sqlite3_result_value(context, argv[3]); + }else if( sParse.zErrMsg ){ + renameColumnParseError(context, "", argv[1], argv[2], &sParse); + }else{ + sqlite3_result_error_code(context, rc); + } + } + + renameParseCleanup(&sParse); + renameTokenFree(db, sCtx.pList); + sqlite3BtreeLeaveAll(db); +#ifndef SQLITE_OMIT_AUTHORIZATION + db->xAuth = xAuth; +#endif + } + + return; +} + +static int renameQuotefixExprCb(Walker *pWalker, Expr *pExpr){ + if( pExpr->op==TK_STRING && (pExpr->flags & EP_DblQuoted) ){ + renameTokenFind(pWalker->pParse, pWalker->u.pRename, (const void*)pExpr); + } + return WRC_Continue; +} + +/* SQL function: sqlite_rename_quotefix(DB,SQL) +** +** Rewrite the DDL statement "SQL" so that any string literals that use +** double-quotes use single quotes instead. +** +** Two arguments must be passed: +** +** 0: Database name ("main", "temp" etc.). +** 1: SQL statement to edit. +** +** The returned value is the modified SQL statement. For example, given +** the database schema: +** +** CREATE TABLE t1(a, b, c); +** +** SELECT sqlite_rename_quotefix('main', +** 'CREATE VIEW v1 AS SELECT "a", "string" FROM t1' +** ); +** +** returns the string: +** +** CREATE VIEW v1 AS SELECT "a", 'string' FROM t1 +** +** If there is a error in the input SQL, then raise an error, except +** if PRAGMA writable_schema=ON, then just return the input string +** unmodified following an error. +*/ +static void renameQuotefixFunc( + sqlite3_context *context, + int NotUsed, + sqlite3_value **argv +){ + sqlite3 *db = sqlite3_context_db_handle(context); + char const *zDb = (const char*)sqlite3_value_text(argv[0]); + char const *zInput = (const char*)sqlite3_value_text(argv[1]); + +#ifndef SQLITE_OMIT_AUTHORIZATION + sqlite3_xauth xAuth = db->xAuth; + db->xAuth = 0; +#endif + + sqlite3BtreeEnterAll(db); + + UNUSED_PARAMETER(NotUsed); + if( zDb && zInput ){ + int rc; + Parse sParse; + rc = renameParseSql(&sParse, zDb, db, zInput, 0); + + if( rc==SQLITE_OK ){ + RenameCtx sCtx; + Walker sWalker; + + /* Walker to find tokens that need to be replaced. */ + memset(&sCtx, 0, sizeof(RenameCtx)); + memset(&sWalker, 0, sizeof(Walker)); + sWalker.pParse = &sParse; + sWalker.xExprCallback = renameQuotefixExprCb; + sWalker.xSelectCallback = renameColumnSelectCb; + sWalker.u.pRename = &sCtx; + + if( sParse.pNewTable ){ + if( IsView(sParse.pNewTable) ){ + Select *pSelect = sParse.pNewTable->u.view.pSelect; + pSelect->selFlags &= ~(u32)SF_View; + sParse.rc = SQLITE_OK; + sqlite3SelectPrep(&sParse, pSelect, 0); + rc = (db->mallocFailed ? SQLITE_NOMEM : sParse.rc); + if( rc==SQLITE_OK ){ + sqlite3WalkSelect(&sWalker, pSelect); + } + }else{ + int i; + sqlite3WalkExprList(&sWalker, sParse.pNewTable->pCheck); +#ifndef SQLITE_OMIT_GENERATED_COLUMNS + for(i=0; inCol; i++){ + sqlite3WalkExpr(&sWalker, + sqlite3ColumnExpr(sParse.pNewTable, + &sParse.pNewTable->aCol[i])); + } +#endif /* SQLITE_OMIT_GENERATED_COLUMNS */ + } + }else if( sParse.pNewIndex ){ + sqlite3WalkExprList(&sWalker, sParse.pNewIndex->aColExpr); + sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere); + }else{ +#ifndef SQLITE_OMIT_TRIGGER + rc = renameResolveTrigger(&sParse); + if( rc==SQLITE_OK ){ + renameWalkTrigger(&sWalker, sParse.pNewTrigger); + } +#endif /* SQLITE_OMIT_TRIGGER */ + } + + if( rc==SQLITE_OK ){ + rc = renameEditSql(context, &sCtx, zInput, 0, 0); + } + renameTokenFree(db, sCtx.pList); + } + if( rc!=SQLITE_OK ){ + if( sqlite3WritableSchema(db) && rc==SQLITE_ERROR ){ + sqlite3_result_value(context, argv[1]); + }else{ + sqlite3_result_error_code(context, rc); + } + } + renameParseCleanup(&sParse); + } + +#ifndef SQLITE_OMIT_AUTHORIZATION + db->xAuth = xAuth; +#endif + + sqlite3BtreeLeaveAll(db); +} + +/* Function: sqlite_rename_test(DB,SQL,TYPE,NAME,ISTEMP,WHEN,DQS) +** +** An SQL user function that checks that there are no parse or symbol +** resolution problems in a CREATE TRIGGER|TABLE|VIEW|INDEX statement. +** After an ALTER TABLE .. RENAME operation is performed and the schema +** reloaded, this function is called on each SQL statement in the schema +** to ensure that it is still usable. +** +** 0: Database name ("main", "temp" etc.). +** 1: SQL statement. +** 2: Object type ("view", "table", "trigger" or "index"). +** 3: Object name. +** 4: True if object is from temp schema. +** 5: "when" part of error message. +** 6: True to disable the DQS quirk when parsing SQL. +** +** The return value is computed as follows: +** +** A. If an error is seen and not in PRAGMA writable_schema=ON mode, +** then raise the error. +** B. Else if a trigger is created and the the table that the trigger is +** attached to is in database zDb, then return 1. +** C. Otherwise return NULL. +*/ +static void renameTableTest( + sqlite3_context *context, + int NotUsed, + sqlite3_value **argv +){ + sqlite3 *db = sqlite3_context_db_handle(context); + char const *zDb = (const char*)sqlite3_value_text(argv[0]); + char const *zInput = (const char*)sqlite3_value_text(argv[1]); + int bTemp = sqlite3_value_int(argv[4]); + int isLegacy = (db->flags & SQLITE_LegacyAlter); + char const *zWhen = (const char*)sqlite3_value_text(argv[5]); + int bNoDQS = sqlite3_value_int(argv[6]); + +#ifndef SQLITE_OMIT_AUTHORIZATION + sqlite3_xauth xAuth = db->xAuth; + db->xAuth = 0; +#endif + + UNUSED_PARAMETER(NotUsed); + + if( zDb && zInput ){ + int rc; + Parse sParse; + u64 flags = db->flags; + if( bNoDQS ) db->flags &= ~(SQLITE_DqsDML|SQLITE_DqsDDL); + rc = renameParseSql(&sParse, zDb, db, zInput, bTemp); + db->flags = flags; + if( rc==SQLITE_OK ){ + if( isLegacy==0 && sParse.pNewTable && IsView(sParse.pNewTable) ){ + NameContext sNC; + memset(&sNC, 0, sizeof(sNC)); + sNC.pParse = &sParse; + sqlite3SelectPrep(&sParse, sParse.pNewTable->u.view.pSelect, &sNC); + if( sParse.nErr ) rc = sParse.rc; + } + + else if( sParse.pNewTrigger ){ + if( isLegacy==0 ){ + rc = renameResolveTrigger(&sParse); + } + if( rc==SQLITE_OK ){ + int i1 = sqlite3SchemaToIndex(db, sParse.pNewTrigger->pTabSchema); + int i2 = sqlite3FindDbName(db, zDb); + if( i1==i2 ){ + /* Handle output case B */ + sqlite3_result_int(context, 1); + } + } + } + } + + if( rc!=SQLITE_OK && zWhen && !sqlite3WritableSchema(db) ){ + /* Output case A */ + renameColumnParseError(context, zWhen, argv[2], argv[3],&sParse); + } + renameParseCleanup(&sParse); + } + +#ifndef SQLITE_OMIT_AUTHORIZATION + db->xAuth = xAuth; +#endif +} + +/* +** The implementation of internal UDF sqlite_drop_column(). +** +** Arguments: +** +** argv[0]: An integer - the index of the schema containing the table +** argv[1]: CREATE TABLE statement to modify. +** argv[2]: An integer - the index of the column to remove. +** +** The value returned is a string containing the CREATE TABLE statement +** with column argv[2] removed. +*/ +static void dropColumnFunc( + sqlite3_context *context, + int NotUsed, + sqlite3_value **argv +){ + sqlite3 *db = sqlite3_context_db_handle(context); + int iSchema = sqlite3_value_int(argv[0]); + const char *zSql = (const char*)sqlite3_value_text(argv[1]); + int iCol = sqlite3_value_int(argv[2]); + const char *zDb = db->aDb[iSchema].zDbSName; + int rc; + Parse sParse; + RenameToken *pCol; + Table *pTab; + const char *zEnd; + char *zNew = 0; + +#ifndef SQLITE_OMIT_AUTHORIZATION + sqlite3_xauth xAuth = db->xAuth; + db->xAuth = 0; +#endif + + UNUSED_PARAMETER(NotUsed); + rc = renameParseSql(&sParse, zDb, db, zSql, iSchema==1); + if( rc!=SQLITE_OK ) goto drop_column_done; + pTab = sParse.pNewTable; + if( pTab==0 || pTab->nCol==1 || iCol>=pTab->nCol ){ + /* This can happen if the sqlite_schema table is corrupt */ + rc = SQLITE_CORRUPT_BKPT; + goto drop_column_done; + } + + pCol = renameTokenFind(&sParse, 0, (void*)pTab->aCol[iCol].zCnName); + if( iColnCol-1 ){ + RenameToken *pEnd; + pEnd = renameTokenFind(&sParse, 0, (void*)pTab->aCol[iCol+1].zCnName); + zEnd = (const char*)pEnd->t.z; + }else{ + assert( IsOrdinaryTable(pTab) ); + zEnd = (const char*)&zSql[pTab->u.tab.addColOffset]; + while( ALWAYS(pCol->t.z[0]!=0) && pCol->t.z[0]!=',' ) pCol->t.z--; + } + + zNew = sqlite3MPrintf(db, "%.*s%s", pCol->t.z-zSql, zSql, zEnd); + sqlite3_result_text(context, zNew, -1, SQLITE_TRANSIENT); + sqlite3_free(zNew); + +drop_column_done: + renameParseCleanup(&sParse); +#ifndef SQLITE_OMIT_AUTHORIZATION + db->xAuth = xAuth; +#endif + if( rc!=SQLITE_OK ){ + sqlite3_result_error_code(context, rc); + } +} + +/* +** This function is called by the parser upon parsing an +** +** ALTER TABLE pSrc DROP COLUMN pName +** +** statement. Argument pSrc contains the possibly qualified name of the +** table being edited, and token pName the name of the column to drop. +*/ +void sqlite3AlterDropColumn(Parse *pParse, SrcList *pSrc, const Token *pName){ + sqlite3 *db = pParse->db; /* Database handle */ + Table *pTab; /* Table to modify */ + int iDb; /* Index of db containing pTab in aDb[] */ + const char *zDb; /* Database containing pTab ("main" etc.) */ + char *zCol = 0; /* Name of column to drop */ + int iCol; /* Index of column zCol in pTab->aCol[] */ + + /* Look up the table being altered. */ + assert( pParse->pNewTable==0 ); + assert( sqlite3BtreeHoldsAllMutexes(db) ); + if( NEVER(db->mallocFailed) ) goto exit_drop_column; + pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]); + if( !pTab ) goto exit_drop_column; + + /* Make sure this is not an attempt to ALTER a view, virtual table or + ** system table. */ + if( SQLITE_OK!=isAlterableTable(pParse, pTab) ) goto exit_drop_column; + if( SQLITE_OK!=isRealTable(pParse, pTab, 1) ) goto exit_drop_column; + + /* Find the index of the column being dropped. */ + zCol = sqlite3NameFromToken(db, pName); + if( zCol==0 ){ + assert( db->mallocFailed ); + goto exit_drop_column; + } + iCol = sqlite3ColumnIndex(pTab, zCol); + if( iCol<0 ){ + sqlite3ErrorMsg(pParse, "no such column: \"%T\"", pName); + goto exit_drop_column; + } + + /* Do not allow the user to drop a PRIMARY KEY column or a column + ** constrained by a UNIQUE constraint. */ + if( pTab->aCol[iCol].colFlags & (COLFLAG_PRIMKEY|COLFLAG_UNIQUE) ){ + sqlite3ErrorMsg(pParse, "cannot drop %s column: \"%s\"", + (pTab->aCol[iCol].colFlags&COLFLAG_PRIMKEY) ? "PRIMARY KEY" : "UNIQUE", + zCol + ); + goto exit_drop_column; + } + + /* Do not allow the number of columns to go to zero */ + if( pTab->nCol<=1 ){ + sqlite3ErrorMsg(pParse, "cannot drop column \"%s\": no other columns exist",zCol); + goto exit_drop_column; + } + + /* Edit the sqlite_schema table */ + iDb = sqlite3SchemaToIndex(db, pTab->pSchema); + assert( iDb>=0 ); + zDb = db->aDb[iDb].zDbSName; +#ifndef SQLITE_OMIT_AUTHORIZATION + /* Invoke the authorization callback. */ + if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, zCol) ){ + goto exit_drop_column; + } +#endif + renameTestSchema(pParse, zDb, iDb==1, "", 0); + renameFixQuotes(pParse, zDb, iDb==1); + sqlite3NestedParse(pParse, + "UPDATE \"%w\"." LEGACY_SCHEMA_TABLE " SET " + "sql = sqlite_drop_column(%d, sql, %d) " + "WHERE (type=='table' AND tbl_name=%Q COLLATE nocase)" + , zDb, iDb, iCol, pTab->zName + ); + + /* Drop and reload the database schema. */ + renameReloadSchema(pParse, iDb, INITFLAG_AlterDrop); + renameTestSchema(pParse, zDb, iDb==1, "after drop column", 1); + + /* Edit rows of table on disk */ + if( pParse->nErr==0 && (pTab->aCol[iCol].colFlags & COLFLAG_VIRTUAL)==0 ){ + int i; + int addr; + int reg; + int regRec; + Index *pPk = 0; + int nField = 0; /* Number of non-virtual columns after drop */ + int iCur; + Vdbe *v = sqlite3GetVdbe(pParse); + iCur = pParse->nTab++; + sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenWrite); + addr = sqlite3VdbeAddOp1(v, OP_Rewind, iCur); VdbeCoverage(v); + reg = ++pParse->nMem; + if( HasRowid(pTab) ){ + sqlite3VdbeAddOp2(v, OP_Rowid, iCur, reg); + pParse->nMem += pTab->nCol; + }else{ + pPk = sqlite3PrimaryKeyIndex(pTab); + pParse->nMem += pPk->nColumn; + for(i=0; inKeyCol; i++){ + sqlite3VdbeAddOp3(v, OP_Column, iCur, i, reg+i+1); + } + nField = pPk->nKeyCol; + } + regRec = ++pParse->nMem; + for(i=0; inCol; i++){ + if( i!=iCol && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ){ + int regOut; + if( pPk ){ + int iPos = sqlite3TableColumnToIndex(pPk, i); + int iColPos = sqlite3TableColumnToIndex(pPk, iCol); + if( iPosnKeyCol ) continue; + regOut = reg+1+iPos-(iPos>iColPos); + }else{ + regOut = reg+1+nField; + } + if( i==pTab->iPKey ){ + sqlite3VdbeAddOp2(v, OP_Null, 0, regOut); + }else{ + char aff = pTab->aCol[i].affinity; + if( aff==SQLITE_AFF_REAL ){ + pTab->aCol[i].affinity = SQLITE_AFF_NUMERIC; + } + sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, i, regOut); + pTab->aCol[i].affinity = aff; + } + nField++; + } + } + if( nField==0 ){ + /* dbsqlfuzz 5f09e7bcc78b4954d06bf9f2400d7715f48d1fef */ + pParse->nMem++; + sqlite3VdbeAddOp2(v, OP_Null, 0, reg+1); + nField = 1; + } + sqlite3VdbeAddOp3(v, OP_MakeRecord, reg+1, nField, regRec); + if( pPk ){ + sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iCur, regRec, reg+1, pPk->nKeyCol); + }else{ + sqlite3VdbeAddOp3(v, OP_Insert, iCur, regRec, reg); + } + sqlite3VdbeChangeP5(v, OPFLAG_SAVEPOSITION); + + sqlite3VdbeAddOp2(v, OP_Next, iCur, addr+1); VdbeCoverage(v); + sqlite3VdbeJumpHere(v, addr); + } + +exit_drop_column: + sqlite3DbFree(db, zCol); + sqlite3SrcListDelete(db, pSrc); +} + +/* +** Register built-in functions used to help implement ALTER TABLE +*/ +void sqlite3AlterFunctions(void){ + static FuncDef aAlterTableFuncs[] = { + INTERNAL_FUNCTION(sqlite_rename_column, 9, renameColumnFunc), + INTERNAL_FUNCTION(sqlite_rename_table, 7, renameTableFunc), + INTERNAL_FUNCTION(sqlite_rename_test, 7, renameTableTest), + INTERNAL_FUNCTION(sqlite_drop_column, 3, dropColumnFunc), + INTERNAL_FUNCTION(sqlite_rename_quotefix,2, renameQuotefixFunc), + }; + sqlite3InsertBuiltinFuncs(aAlterTableFuncs, ArraySize(aAlterTableFuncs)); +} #endif /* SQLITE_ALTER_TABLE */ diff --git a/src/analyze.c b/src/analyze.c index 1e026a7530..2721f25234 100644 --- a/src/analyze.c +++ b/src/analyze.c @@ -27,13 +27,13 @@ ** is between 3.6.18 and 3.7.8, inclusive, and unless SQLite is compiled ** with SQLITE_ENABLE_STAT2. The sqlite_stat2 table is deprecated. ** The sqlite_stat2 table is superseded by sqlite_stat3, which is only -** created and used by SQLite versions 3.7.9 and later and with +** created and used by SQLite versions 3.7.9 through 3.29.0 when ** SQLITE_ENABLE_STAT3 defined. The functionality of sqlite_stat3 -** is a superset of sqlite_stat2. The sqlite_stat4 is an enhanced -** version of sqlite_stat3 and is only available when compiled with -** SQLITE_ENABLE_STAT4 and in SQLite versions 3.8.1 and later. It is -** not possible to enable both STAT3 and STAT4 at the same time. If they -** are both enabled, then STAT4 takes precedence. +** is a superset of sqlite_stat2 and is also now deprecated. The +** sqlite_stat4 is an enhanced version of sqlite_stat3 and is only +** available when compiled with SQLITE_ENABLE_STAT4 and in SQLite +** versions 3.8.1 and later. STAT4 is the only variant that is still +** supported. ** ** For most applications, sqlite_stat1 provides all the statistics required ** for the query planner to make good choices. @@ -144,17 +144,11 @@ #if defined(SQLITE_ENABLE_STAT4) # define IsStat4 1 -# define IsStat3 0 -#elif defined(SQLITE_ENABLE_STAT3) -# define IsStat4 0 -# define IsStat3 1 #else # define IsStat4 0 -# define IsStat3 0 # undef SQLITE_STAT4_SAMPLES # define SQLITE_STAT4_SAMPLES 1 #endif -#define IsStat34 (IsStat3+IsStat4) /* 1 for STAT3 or STAT4. 0 otherwise */ /* ** This routine generates code that opens the sqlite_statN tables. @@ -183,21 +177,22 @@ static void openStatTable( { "sqlite_stat1", "tbl,idx,stat" }, #if defined(SQLITE_ENABLE_STAT4) { "sqlite_stat4", "tbl,idx,neq,nlt,ndlt,sample" }, - { "sqlite_stat3", 0 }, -#elif defined(SQLITE_ENABLE_STAT3) - { "sqlite_stat3", "tbl,idx,neq,nlt,ndlt,sample" }, - { "sqlite_stat4", 0 }, #else - { "sqlite_stat3", 0 }, { "sqlite_stat4", 0 }, #endif + { "sqlite_stat3", 0 }, }; int i; sqlite3 *db = pParse->db; Db *pDb; Vdbe *v = sqlite3GetVdbe(pParse); - int aRoot[ArraySize(aTable)]; + u32 aRoot[ArraySize(aTable)]; u8 aCreateTbl[ArraySize(aTable)]; +#ifdef SQLITE_ENABLE_STAT4 + const int nToOpen = OptimizationEnabled(db,SQLITE_Stat4) ? 2 : 1; +#else + const int nToOpen = 1; +#endif if( v==0 ) return; assert( sqlite3BtreeHoldsAllMutexes(db) ); @@ -210,16 +205,18 @@ static void openStatTable( for(i=0; izName))==0 ){ - if( aTable[i].zCols ){ + aCreateTbl[i] = 0; + if( (pStat = sqlite3FindTable(db, zTab, pDb->zDbSName))==0 ){ + if( iregRoot. This is important ** because the OpenWrite opcode below will be needing it. */ sqlite3NestedParse(pParse, - "CREATE TABLE %Q.%s(%s)", pDb->zName, zTab, aTable[i].zCols + "CREATE TABLE %Q.%s(%s)", pDb->zDbSName, zTab, aTable[i].zCols ); - aRoot[i] = pParse->regRoot; + assert( pParse->isCreate || pParse->nErr ); + aRoot[i] = (u32)pParse->u1.cr.regRoot; aCreateTbl[i] = OPFLAG_P2ISREG; } }else{ @@ -227,24 +224,27 @@ static void openStatTable( ** associated with the table zWhere. If zWhere is NULL, delete the ** entire contents of the table. */ aRoot[i] = pStat->tnum; - aCreateTbl[i] = 0; sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab); if( zWhere ){ sqlite3NestedParse(pParse, "DELETE FROM %Q.%s WHERE %s=%Q", - pDb->zName, zTab, zWhereType, zWhere + pDb->zDbSName, zTab, zWhereType, zWhere ); +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK + }else if( db->xPreUpdateCallback ){ + sqlite3NestedParse(pParse, "DELETE FROM %Q.%s", pDb->zDbSName, zTab); +#endif }else{ /* The sqlite_stat[134] table already exists. Delete all rows. */ - sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb); + sqlite3VdbeAddOp2(v, OP_Clear, (int)aRoot[i], iDb); } } } /* Open the sqlite_stat[134] tables for writing. */ - for(i=0; aTable[i].zCols; i++){ + for(i=0; inRowid ){ sqlite3DbFree(db, p->u.aRowid); @@ -309,8 +315,8 @@ static void sampleClear(sqlite3 *db, Stat4Sample *p){ /* Initialize the BLOB value of a ROWID */ -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 -static void sampleSetRowid(sqlite3 *db, Stat4Sample *p, int n, const u8 *pData){ +#ifdef SQLITE_ENABLE_STAT4 +static void sampleSetRowid(sqlite3 *db, StatSample *p, int n, const u8 *pData){ assert( db!=0 ); if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid); p->u.aRowid = sqlite3DbMallocRawNN(db, n); @@ -325,8 +331,8 @@ static void sampleSetRowid(sqlite3 *db, Stat4Sample *p, int n, const u8 *pData){ /* Initialize the INTEGER value of a ROWID. */ -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 -static void sampleSetRowidInt64(sqlite3 *db, Stat4Sample *p, i64 iRowid){ +#ifdef SQLITE_ENABLE_STAT4 +static void sampleSetRowidInt64(sqlite3 *db, StatSample *p, i64 iRowid){ assert( db!=0 ); if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid); p->nRowid = 0; @@ -338,8 +344,8 @@ static void sampleSetRowidInt64(sqlite3 *db, Stat4Sample *p, i64 iRowid){ /* ** Copy the contents of object (*pFrom) into (*pTo). */ -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 -static void sampleCopy(Stat4Accum *p, Stat4Sample *pTo, Stat4Sample *pFrom){ +#ifdef SQLITE_ENABLE_STAT4 +static void sampleCopy(StatAccum *p, StatSample *pTo, StatSample *pFrom){ pTo->isPSample = pFrom->isPSample; pTo->iCol = pFrom->iCol; pTo->iHash = pFrom->iHash; @@ -355,40 +361,41 @@ static void sampleCopy(Stat4Accum *p, Stat4Sample *pTo, Stat4Sample *pFrom){ #endif /* -** Reclaim all memory of a Stat4Accum structure. +** Reclaim all memory of a StatAccum structure. */ -static void stat4Destructor(void *pOld){ - Stat4Accum *p = (Stat4Accum*)pOld; -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 - int i; - for(i=0; inCol; i++) sampleClear(p->db, p->aBest+i); - for(i=0; imxSample; i++) sampleClear(p->db, p->a+i); - sampleClear(p->db, &p->current); +static void statAccumDestructor(void *pOld){ + StatAccum *p = (StatAccum*)pOld; +#ifdef SQLITE_ENABLE_STAT4 + if( p->mxSample ){ + int i; + for(i=0; inCol; i++) sampleClear(p->db, p->aBest+i); + for(i=0; imxSample; i++) sampleClear(p->db, p->a+i); + sampleClear(p->db, &p->current); + } #endif sqlite3DbFree(p->db, p); } /* -** Implementation of the stat_init(N,K,C) SQL function. The three parameters +** Implementation of the stat_init(N,K,C,L) SQL function. The four parameters ** are: ** N: The number of columns in the index including the rowid/pk (note 1) ** K: The number of columns in the index excluding the rowid/pk. -** C: The number of rows in the index (note 2) +** C: Estimated number of rows in the index +** L: A limit on the number of rows to scan, or 0 for no-limit ** ** Note 1: In the special case of the covering index that implements a ** WITHOUT ROWID table, N is the number of PRIMARY KEY columns, not the ** total number of columns in the table. ** -** Note 2: C is only used for STAT3 and STAT4. -** ** For indexes on ordinary rowid tables, N==K+1. But for indexes on ** WITHOUT ROWID tables, N=K+P where P is the number of columns in the ** PRIMARY KEY of the table. The covering index that implements the ** original WITHOUT ROWID table as N==K as a special case. ** -** This routine allocates the Stat4Accum object in heap memory. The return -** value is a pointer to the Stat4Accum object. The datatype of the -** return value is BLOB, but it is really just a pointer to the Stat4Accum +** This routine allocates the StatAccum object in heap memory. The return +** value is a pointer to the StatAccum object. The datatype of the +** return value is BLOB, but it is really just a pointer to the StatAccum ** object. */ static void statInit( @@ -396,14 +403,15 @@ static void statInit( int argc, sqlite3_value **argv ){ - Stat4Accum *p; + StatAccum *p; int nCol; /* Number of columns in index being sampled */ int nKeyCol; /* Number of key columns */ int nColUp; /* nCol rounded up for alignment */ - int n; /* Bytes of space to allocate */ - sqlite3 *db; /* Database connection */ -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 - int mxSample = SQLITE_STAT4_SAMPLES; + i64 n; /* Bytes of space to allocate */ + sqlite3 *db = sqlite3_context_db_handle(context); /* Database connection */ +#ifdef SQLITE_ENABLE_STAT4 + /* Maximum number of samples. 0 if STAT4 data is not collected */ + int mxSample = OptimizationEnabled(db,SQLITE_Stat4) ?SQLITE_STAT4_SAMPLES :0; #endif /* Decode the three function arguments */ @@ -415,17 +423,17 @@ static void statInit( assert( nKeyCol<=nCol ); assert( nKeyCol>0 ); - /* Allocate the space required for the Stat4Accum object */ + /* Allocate the space required for the StatAccum object */ n = sizeof(*p) - + sizeof(tRowcnt)*nColUp /* Stat4Accum.anEq */ - + sizeof(tRowcnt)*nColUp /* Stat4Accum.anDLt */ -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 - + sizeof(tRowcnt)*nColUp /* Stat4Accum.anLt */ - + sizeof(Stat4Sample)*(nCol+mxSample) /* Stat4Accum.aBest[], a[] */ - + sizeof(tRowcnt)*3*nColUp*(nCol+mxSample) + + sizeof(tRowcnt)*nColUp; /* StatAccum.anDLt */ +#ifdef SQLITE_ENABLE_STAT4 + n += sizeof(tRowcnt)*nColUp; /* StatAccum.anEq */ + if( mxSample ){ + n += sizeof(tRowcnt)*nColUp /* StatAccum.anLt */ + + sizeof(StatSample)*(nCol+mxSample) /* StatAccum.aBest[], a[] */ + + sizeof(tRowcnt)*3*nColUp*(nCol+mxSample); + } #endif - ; - db = sqlite3_context_db_handle(context); p = sqlite3DbMallocZero(db, n); if( p==0 ){ sqlite3_result_error_nomem(context); @@ -433,25 +441,28 @@ static void statInit( } p->db = db; + p->nEst = sqlite3_value_int64(argv[2]); p->nRow = 0; + p->nLimit = sqlite3_value_int(argv[3]); p->nCol = nCol; p->nKeyCol = nKeyCol; + p->nSkipAhead = 0; p->current.anDLt = (tRowcnt*)&p[1]; - p->current.anEq = &p->current.anDLt[nColUp]; -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 - { +#ifdef SQLITE_ENABLE_STAT4 + p->current.anEq = &p->current.anDLt[nColUp]; + p->mxSample = p->nLimit==0 ? mxSample : 0; + if( mxSample ){ u8 *pSpace; /* Allocated space not yet assigned */ int i; /* Used to iterate through p->aSample[] */ p->iGet = -1; - p->mxSample = mxSample; - p->nPSample = (tRowcnt)(sqlite3_value_int64(argv[2])/(mxSample/3+1) + 1); + p->nPSample = (tRowcnt)(p->nEst/(mxSample/3+1) + 1); p->current.anLt = &p->current.anEq[nColUp]; p->iPrn = 0x689e962d*(u32)nCol ^ 0xd0944565*(u32)sqlite3_value_int(argv[2]); - /* Set up the Stat4Accum.a[] and aBest[] arrays */ - p->a = (struct Stat4Sample*)&p->current.anLt[nColUp]; + /* Set up the StatAccum.a[] and aBest[] arrays */ + p->a = (struct StatSample*)&p->current.anLt[nColUp]; p->aBest = &p->a[mxSample]; pSpace = (u8*)(&p->a[mxSample+nCol]); for(i=0; i<(mxSample+nCol); i++){ @@ -471,18 +482,18 @@ static void statInit( ** only the pointer (the 2nd parameter) matters. The size of the object ** (given by the 3rd parameter) is never used and can be any positive ** value. */ - sqlite3_result_blob(context, p, sizeof(*p), stat4Destructor); + sqlite3_result_blob(context, p, sizeof(*p), statAccumDestructor); } static const FuncDef statInitFuncdef = { - 2+IsStat34, /* nArg */ + 4, /* nArg */ SQLITE_UTF8, /* funcFlags */ 0, /* pUserData */ 0, /* pNext */ statInit, /* xSFunc */ 0, /* xFinalize */ + 0, 0, /* xValue, xInverse */ "stat_init", /* zName */ - 0, /* pHash */ - 0 /* pDestructor */ + {0} }; #ifdef SQLITE_ENABLE_STAT4 @@ -498,9 +509,9 @@ static const FuncDef statInitFuncdef = { ** the anEq[] array from pSample->anEq[pSample->iCol+1] onwards are valid. */ static int sampleIsBetterPost( - Stat4Accum *pAccum, - Stat4Sample *pNew, - Stat4Sample *pOld + StatAccum *pAccum, + StatSample *pNew, + StatSample *pOld ){ int nCol = pAccum->nCol; int i; @@ -514,7 +525,7 @@ static int sampleIsBetterPost( } #endif -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 +#ifdef SQLITE_ENABLE_STAT4 /* ** Return true if pNew is to be preferred over pOld. ** @@ -522,9 +533,9 @@ static int sampleIsBetterPost( ** the anEq[] array from pSample->anEq[pSample->iCol] onwards are valid. */ static int sampleIsBetter( - Stat4Accum *pAccum, - Stat4Sample *pNew, - Stat4Sample *pOld + StatAccum *pAccum, + StatSample *pNew, + StatSample *pOld ){ tRowcnt nEqNew = pNew->anEq[pNew->iCol]; tRowcnt nEqOld = pOld->anEq[pOld->iCol]; @@ -533,30 +544,32 @@ static int sampleIsBetter( assert( IsStat4 || (pNew->iCol==0 && pOld->iCol==0) ); if( (nEqNew>nEqOld) ) return 1; -#ifdef SQLITE_ENABLE_STAT4 if( nEqNew==nEqOld ){ if( pNew->iColiCol ) return 1; return (pNew->iCol==pOld->iCol && sampleIsBetterPost(pAccum, pNew, pOld)); } return 0; -#else - return (nEqNew==nEqOld && pNew->iHash>pOld->iHash); -#endif } /* ** Copy the contents of sample *pNew into the p->a[] array. If necessary, ** remove the least desirable sample from p->a[] to make room. */ -static void sampleInsert(Stat4Accum *p, Stat4Sample *pNew, int nEqZero){ - Stat4Sample *pSample = 0; +static void sampleInsert(StatAccum *p, StatSample *pNew, int nEqZero){ + StatSample *pSample = 0; int i; assert( IsStat4 || nEqZero==0 ); -#ifdef SQLITE_ENABLE_STAT4 + /* StatAccum.nMaxEqZero is set to the maximum number of leading 0 + ** values in the anEq[] array of any sample in StatAccum.a[]. In + ** other words, if nMaxEqZero is n, then it is guaranteed that there + ** are no samples with StatSample.anEq[m]==0 for (m>=n). */ + if( nEqZero>p->nMaxEqZero ){ + p->nMaxEqZero = nEqZero; + } if( pNew->isPSample==0 ){ - Stat4Sample *pUpgrade = 0; + StatSample *pUpgrade = 0; assert( pNew->anEq[pNew->iCol]>0 ); /* This sample is being added because the prefix that ends in column @@ -565,7 +578,7 @@ static void sampleInsert(Stat4Accum *p, Stat4Sample *pNew, int nEqZero){ ** this one. Instead, upgrade the priority of the highest priority ** existing sample that shares this prefix. */ for(i=p->nSample-1; i>=0; i--){ - Stat4Sample *pOld = &p->a[i]; + StatSample *pOld = &p->a[i]; if( pOld->anEq[pNew->iCol]==0 ){ if( pOld->isPSample ) return; assert( pOld->iCol>pNew->iCol ); @@ -581,11 +594,10 @@ static void sampleInsert(Stat4Accum *p, Stat4Sample *pNew, int nEqZero){ goto find_new_min; } } -#endif /* If necessary, remove sample iMin to make room for the new sample. */ if( p->nSample>=p->mxSample ){ - Stat4Sample *pMin = &p->a[p->iMin]; + StatSample *pMin = &p->a[p->iMin]; tRowcnt *anEq = pMin->anEq; tRowcnt *anLt = pMin->anLt; tRowcnt *anDLt = pMin->anDLt; @@ -602,10 +614,8 @@ static void sampleInsert(Stat4Accum *p, Stat4Sample *pNew, int nEqZero){ /* The "rows less-than" for the rowid column must be greater than that ** for the last sample in the p->a[] array. Otherwise, the samples would ** be out of order. */ -#ifdef SQLITE_ENABLE_STAT4 assert( p->nSample==0 || pNew->anLt[p->nCol-1] > p->a[p->nSample-1].anLt[p->nCol-1] ); -#endif /* Insert the new sample */ pSample = &p->a[p->nSample]; @@ -615,9 +625,7 @@ static void sampleInsert(Stat4Accum *p, Stat4Sample *pNew, int nEqZero){ /* Zero the first nEqZero entries in the anEq[] array. */ memset(pSample->anEq, 0, sizeof(tRowcnt)*nEqZero); -#ifdef SQLITE_ENABLE_STAT4 - find_new_min: -#endif +find_new_min: if( p->nSample>=p->mxSample ){ int iMin = -1; for(i=0; imxSample; i++){ @@ -630,79 +638,66 @@ static void sampleInsert(Stat4Accum *p, Stat4Sample *pNew, int nEqZero){ p->iMin = iMin; } } -#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ +#endif /* SQLITE_ENABLE_STAT4 */ +#ifdef SQLITE_ENABLE_STAT4 /* ** Field iChng of the index being scanned has changed. So at this point ** p->current contains a sample that reflects the previous row of the ** index. The value of anEq[iChng] and subsequent anEq[] elements are ** correct at this point. */ -static void samplePushPrevious(Stat4Accum *p, int iChng){ -#ifdef SQLITE_ENABLE_STAT4 +static void samplePushPrevious(StatAccum *p, int iChng){ int i; /* Check if any samples from the aBest[] array should be pushed ** into IndexSample.a[] at this point. */ for(i=(p->nCol-2); i>=iChng; i--){ - Stat4Sample *pBest = &p->aBest[i]; + StatSample *pBest = &p->aBest[i]; pBest->anEq[i] = p->current.anEq[i]; if( p->nSamplemxSample || sampleIsBetter(p, pBest, &p->a[p->iMin]) ){ sampleInsert(p, pBest, i); } } - /* Update the anEq[] fields of any samples already collected. */ + /* Check that no sample contains an anEq[] entry with an index of + ** p->nMaxEqZero or greater set to zero. */ for(i=p->nSample-1; i>=0; i--){ int j; - for(j=iChng; jnCol; j++){ - if( p->a[i].anEq[j]==0 ) p->a[i].anEq[j] = p->current.anEq[j]; - } + for(j=p->nMaxEqZero; jnCol; j++) assert( p->a[i].anEq[j]>0 ); } -#endif - -#if defined(SQLITE_ENABLE_STAT3) && !defined(SQLITE_ENABLE_STAT4) - if( iChng==0 ){ - tRowcnt nLt = p->current.anLt[0]; - tRowcnt nEq = p->current.anEq[0]; - /* Check if this is to be a periodic sample. If so, add it. */ - if( (nLt/p->nPSample)!=(nLt+nEq)/p->nPSample ){ - p->current.isPSample = 1; - sampleInsert(p, &p->current, 0); - p->current.isPSample = 0; - }else - - /* Or if it is a non-periodic sample. Add it in this case too. */ - if( p->nSamplemxSample - || sampleIsBetter(p, &p->current, &p->a[p->iMin]) - ){ - sampleInsert(p, &p->current, 0); + /* Update the anEq[] fields of any samples already collected. */ + if( iChngnMaxEqZero ){ + for(i=p->nSample-1; i>=0; i--){ + int j; + for(j=iChng; jnCol; j++){ + if( p->a[i].anEq[j]==0 ) p->a[i].anEq[j] = p->current.anEq[j]; + } } + p->nMaxEqZero = iChng; } -#endif - -#ifndef SQLITE_ENABLE_STAT3_OR_STAT4 - UNUSED_PARAMETER( p ); - UNUSED_PARAMETER( iChng ); -#endif } +#endif /* SQLITE_ENABLE_STAT4 */ /* ** Implementation of the stat_push SQL function: stat_push(P,C,R) ** Arguments: ** -** P Pointer to the Stat4Accum object created by stat_init() +** P Pointer to the StatAccum object created by stat_init() ** C Index of left-most column to differ from previous row ** R Rowid for the current row. Might be a key record for ** WITHOUT ROWID tables. ** -** This SQL function always returns NULL. It's purpose it to accumulate -** statistical data and/or samples in the Stat4Accum object about the -** index being analyzed. The stat_get() SQL function will later be used to -** extract relevant information for constructing the sqlite_statN tables. +** The purpose of this routine is to collect statistical data and/or +** samples from the index being analyzed into the StatAccum object. +** The stat_get() SQL function will be used afterwards to +** retrieve the information gathered. ** -** The R parameter is only used for STAT3 and STAT4 +** This SQL function usually returns NULL, but might return an integer +** if it wants the byte-code to do special processing. +** +** The R parameter is only used for STAT4 */ static void statPush( sqlite3_context *context, @@ -712,7 +707,7 @@ static void statPush( int i; /* The three function arguments */ - Stat4Accum *p = (Stat4Accum*)sqlite3_value_blob(argv[0]); + StatAccum *p = (StatAccum*)sqlite3_value_blob(argv[0]); int iChng = sqlite3_value_int(argv[1]); UNUSED_PARAMETER( argc ); @@ -722,39 +717,44 @@ static void statPush( if( p->nRow==0 ){ /* This is the first call to this function. Do initialization. */ +#ifdef SQLITE_ENABLE_STAT4 for(i=0; inCol; i++) p->current.anEq[i] = 1; +#endif }else{ /* Second and subsequent calls get processed here */ - samplePushPrevious(p, iChng); +#ifdef SQLITE_ENABLE_STAT4 + if( p->mxSample ) samplePushPrevious(p, iChng); +#endif /* Update anDLt[], anLt[] and anEq[] to reflect the values that apply ** to the current row of the index. */ +#ifdef SQLITE_ENABLE_STAT4 for(i=0; icurrent.anEq[i]++; } +#endif for(i=iChng; inCol; i++){ p->current.anDLt[i]++; -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 - p->current.anLt[i] += p->current.anEq[i]; -#endif +#ifdef SQLITE_ENABLE_STAT4 + if( p->mxSample ) p->current.anLt[i] += p->current.anEq[i]; p->current.anEq[i] = 1; +#endif } } - p->nRow++; -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 - if( sqlite3_value_type(argv[2])==SQLITE_INTEGER ){ - sampleSetRowidInt64(p->db, &p->current, sqlite3_value_int64(argv[2])); - }else{ - sampleSetRowid(p->db, &p->current, sqlite3_value_bytes(argv[2]), - sqlite3_value_blob(argv[2])); - } - p->current.iHash = p->iPrn = p->iPrn*1103515245 + 12345; -#endif + p->nRow++; #ifdef SQLITE_ENABLE_STAT4 - { - tRowcnt nLt = p->current.anLt[p->nCol-1]; + if( p->mxSample ){ + tRowcnt nLt; + if( sqlite3_value_type(argv[2])==SQLITE_INTEGER ){ + sampleSetRowidInt64(p->db, &p->current, sqlite3_value_int64(argv[2])); + }else{ + sampleSetRowid(p->db, &p->current, sqlite3_value_bytes(argv[2]), + sqlite3_value_blob(argv[2])); + } + p->current.iHash = p->iPrn = p->iPrn*1103515245 + 12345; + nLt = p->current.anLt[p->nCol-1]; /* Check if this is to be a periodic sample. If so, add it. */ if( (nLt/p->nPSample)!=(nLt+1)/p->nPSample ){ p->current.isPSample = 1; @@ -770,19 +770,24 @@ static void statPush( sampleCopy(p, &p->aBest[i], &p->current); } } - } + }else #endif + if( p->nLimit && p->nRow>(tRowcnt)p->nLimit*(p->nSkipAhead+1) ){ + p->nSkipAhead++; + sqlite3_result_int(context, p->current.anDLt[0]>0); + } } + static const FuncDef statPushFuncdef = { - 2+IsStat34, /* nArg */ + 2+IsStat4, /* nArg */ SQLITE_UTF8, /* funcFlags */ 0, /* pUserData */ 0, /* pNext */ statPush, /* xSFunc */ 0, /* xFinalize */ + 0, 0, /* xValue, xInverse */ "stat_push", /* zName */ - 0, /* pHash */ - 0 /* pDestructor */ + {0} }; #define STAT_GET_STAT1 0 /* "stat" column of stat1 table */ @@ -794,12 +799,18 @@ static const FuncDef statPushFuncdef = { /* ** Implementation of the stat_get(P,J) SQL function. This routine is ** used to query statistical information that has been gathered into -** the Stat4Accum object by prior calls to stat_push(). The P parameter -** has type BLOB but it is really just a pointer to the Stat4Accum object. +** the StatAccum object by prior calls to stat_push(). The P parameter +** has type BLOB but it is really just a pointer to the StatAccum object. ** The content to returned is determined by the parameter J ** which is one of the STAT_GET_xxxx values defined above. ** -** If neither STAT3 nor STAT4 are enabled, then J is always +** The stat_get(P,J) function is not available to generic SQL. It is +** inserted as part of a manually constructed bytecode program. (See +** the callStatGet() routine below.) It is guaranteed that the P +** parameter will always be a pointer to a StatAccum object, never a +** NULL. +** +** If STAT4 is not enabled, then J is always ** STAT_GET_STAT1 and is hence omitted and this routine becomes ** a one-parameter function, stat_get(P), that always returns the ** stat1 table entry information. @@ -809,15 +820,16 @@ static void statGet( int argc, sqlite3_value **argv ){ - Stat4Accum *p = (Stat4Accum*)sqlite3_value_blob(argv[0]); -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 - /* STAT3 and STAT4 have a parameter on this routine. */ + StatAccum *p = (StatAccum*)sqlite3_value_blob(argv[0]); +#ifdef SQLITE_ENABLE_STAT4 + /* STAT4 has a parameter on this routine. */ int eCall = sqlite3_value_int(argv[1]); assert( argc==2 ); assert( eCall==STAT_GET_STAT1 || eCall==STAT_GET_NEQ || eCall==STAT_GET_ROWID || eCall==STAT_GET_NLT || eCall==STAT_GET_NDLT ); + assert( eCall==STAT_GET_STAT1 || p->mxSample ); if( eCall==STAT_GET_STAT1 ) #else assert( argc==1 ); @@ -830,7 +842,7 @@ static void statGet( ** the index. The first integer in the list is the total number of ** entries in the index. There is one additional integer in the list ** for each indexed column. This additional integer is an estimate of - ** the number of rows matched by a stabbing query on the index using + ** the number of rows matched by a equality query on the index using ** a key with the corresponding number of fields. In other words, ** if the index is on columns (a,b) and the sqlite_stat1 value is ** "100 10 2", then SQLite estimates that: @@ -840,40 +852,40 @@ static void statGet( ** * "WHERE a=? AND b=?" matches 2 rows. ** ** If D is the count of distinct values and K is the total number of - ** rows, then each estimate is computed as: + ** rows, then each estimate is usually computed as: ** ** I = (K+D-1)/D + ** + ** In other words, I is K/D rounded up to the next whole integer. + ** However, if I is between 1.0 and 1.1 (in other words if I is + ** close to 1.0 but just a little larger) then do not round up but + ** instead keep the I value at 1.0. */ - char *z; - int i; + sqlite3_str sStat; /* Text of the constructed "stat" line */ + int i; /* Loop counter */ - char *zRet = sqlite3MallocZero( (p->nKeyCol+1)*25 ); - if( zRet==0 ){ - sqlite3_result_error_nomem(context); - return; - } - - sqlite3_snprintf(24, zRet, "%llu", (u64)p->nRow); - z = zRet + sqlite3Strlen30(zRet); + sqlite3StrAccumInit(&sStat, 0, 0, 0, (p->nKeyCol+1)*100); + sqlite3_str_appendf(&sStat, "%llu", + p->nSkipAhead ? (u64)p->nEst : (u64)p->nRow); for(i=0; inKeyCol; i++){ u64 nDistinct = p->current.anDLt[i] + 1; u64 iVal = (p->nRow + nDistinct - 1) / nDistinct; - sqlite3_snprintf(24, z, " %llu", iVal); - z += sqlite3Strlen30(z); - assert( p->current.anEq[i] ); + if( iVal==2 && p->nRow*10 <= nDistinct*11 ) iVal = 1; + sqlite3_str_appendf(&sStat, " %llu", iVal); +#ifdef SQLITE_ENABLE_STAT4 + assert( p->current.anEq[i] || p->nRow==0 ); +#endif } - assert( z[0]=='\0' && z>zRet ); - - sqlite3_result_text(context, zRet, -1, sqlite3_free); + sqlite3ResultStrAccum(context, &sStat); } -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 +#ifdef SQLITE_ENABLE_STAT4 else if( eCall==STAT_GET_ROWID ){ if( p->iGet<0 ){ samplePushPrevious(p, 0); p->iGet = 0; } if( p->iGetnSample ){ - Stat4Sample *pS = p->a + p->iGet; + StatSample *pS = p->a + p->iGet; if( pS->nRowid==0 ){ sqlite3_result_int64(context, pS->u.iRowid); }else{ @@ -883,6 +895,8 @@ static void statGet( } }else{ tRowcnt *aCnt = 0; + sqlite3_str sStat; + int i; assert( p->iGetnSample ); switch( eCall ){ @@ -894,57 +908,68 @@ static void statGet( break; } } - - if( IsStat3 ){ - sqlite3_result_int64(context, (i64)aCnt[0]); - }else{ - char *zRet = sqlite3MallocZero(p->nCol * 25); - if( zRet==0 ){ - sqlite3_result_error_nomem(context); - }else{ - int i; - char *z = zRet; - for(i=0; inCol; i++){ - sqlite3_snprintf(24, z, "%llu ", (u64)aCnt[i]); - z += sqlite3Strlen30(z); - } - assert( z[0]=='\0' && z>zRet ); - z[-1] = '\0'; - sqlite3_result_text(context, zRet, -1, sqlite3_free); - } + sqlite3StrAccumInit(&sStat, 0, 0, 0, p->nCol*100); + for(i=0; inCol; i++){ + sqlite3_str_appendf(&sStat, "%llu ", (u64)aCnt[i]); } + if( sStat.nChar ) sStat.nChar--; + sqlite3ResultStrAccum(context, &sStat); } -#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ +#endif /* SQLITE_ENABLE_STAT4 */ #ifndef SQLITE_DEBUG UNUSED_PARAMETER( argc ); #endif } static const FuncDef statGetFuncdef = { - 1+IsStat34, /* nArg */ + 1+IsStat4, /* nArg */ SQLITE_UTF8, /* funcFlags */ 0, /* pUserData */ 0, /* pNext */ statGet, /* xSFunc */ 0, /* xFinalize */ + 0, 0, /* xValue, xInverse */ "stat_get", /* zName */ - 0, /* pHash */ - 0 /* pDestructor */ + {0} }; -static void callStatGet(Vdbe *v, int regStat4, int iParam, int regOut){ - assert( regOut!=regStat4 && regOut!=regStat4+1 ); -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 - sqlite3VdbeAddOp2(v, OP_Integer, iParam, regStat4+1); +static void callStatGet(Parse *pParse, int regStat, int iParam, int regOut){ +#ifdef SQLITE_ENABLE_STAT4 + sqlite3VdbeAddOp2(pParse->pVdbe, OP_Integer, iParam, regStat+1); #elif SQLITE_DEBUG assert( iParam==STAT_GET_STAT1 ); #else UNUSED_PARAMETER( iParam ); #endif - sqlite3VdbeAddOp4(v, OP_Function0, 0, regStat4, regOut, - (char*)&statGetFuncdef, P4_FUNCDEF); - sqlite3VdbeChangeP5(v, 1 + IsStat34); + assert( regOut!=regStat && regOut!=regStat+1 ); + sqlite3VdbeAddFunctionCall(pParse, 0, regStat, regOut, 1+IsStat4, + &statGetFuncdef, 0); } +#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS +/* Add a comment to the most recent VDBE opcode that is the name +** of the k-th column of the pIdx index. +*/ +static void analyzeVdbeCommentIndexWithColumnName( + Vdbe *v, /* Prepared statement under construction */ + Index *pIdx, /* Index whose column is being loaded */ + int k /* Which column index */ +){ + int i; /* Index of column in the table */ + assert( k>=0 && knColumn ); + i = pIdx->aiColumn[k]; + if( NEVER(i==XN_ROWID) ){ + VdbeComment((v,"%s.rowid",pIdx->zName)); + }else if( i==XN_EXPR ){ + assert( pIdx->bHasExpr ); + VdbeComment((v,"%s.expr(%d)",pIdx->zName, k)); + }else{ + VdbeComment((v,"%s.%s", pIdx->zName, pIdx->pTable->aCol[i].zCnName)); + } +} +#else +# define analyzeVdbeCommentIndexWithColumnName(a,b,c) +#endif /* SQLITE_DEBUG */ + /* ** Generate code to do an analysis of all indices associated with ** a single table. @@ -967,27 +992,33 @@ static void analyzeOneTable( int iDb; /* Index of database containing pTab */ u8 needTableCnt = 1; /* True to count the table */ int regNewRowid = iMem++; /* Rowid for the inserted record */ - int regStat4 = iMem++; /* Register to hold Stat4Accum object */ + int regStat = iMem++; /* Register to hold StatAccum object */ int regChng = iMem++; /* Index of changed index field */ -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 int regRowid = iMem++; /* Rowid argument passed to stat_push() */ -#endif int regTemp = iMem++; /* Temporary use register */ + int regTemp2 = iMem++; /* Second temporary use register */ int regTabname = iMem++; /* Register containing table name */ int regIdxname = iMem++; /* Register containing index name */ int regStat1 = iMem++; /* Value for the stat column of sqlite_stat1 */ int regPrev = iMem; /* MUST BE LAST (see below) */ +#ifdef SQLITE_ENABLE_STAT4 + int doOnce = 1; /* Flag for a one-time computation */ +#endif +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK + Table *pStat1 = 0; +#endif - pParse->nMem = MAX(pParse->nMem, iMem); + sqlite3TouchRegister(pParse, iMem); + assert( sqlite3NoTempsInRange(pParse, regNewRowid, iMem) ); v = sqlite3GetVdbe(pParse); if( v==0 || NEVER(pTab==0) ){ return; } - if( pTab->tnum==0 ){ + if( !IsOrdinaryTable(pTab) ){ /* Do not gather statistics on views or virtual tables */ return; } - if( sqlite3_strlike("sqlite_%", pTab->zName, 0)==0 ){ + if( sqlite3_strlike("sqlite\\_%", pTab->zName, '\\')==0 ){ /* Do not gather statistics on system tables */ return; } @@ -997,11 +1028,23 @@ static void analyzeOneTable( assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); #ifndef SQLITE_OMIT_AUTHORIZATION if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0, - db->aDb[iDb].zName ) ){ + db->aDb[iDb].zDbSName ) ){ return; } #endif +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK + if( db->xPreUpdateCallback ){ + pStat1 = (Table*)sqlite3DbMallocZero(db, sizeof(Table) + 13); + if( pStat1==0 ) return; + pStat1->zName = (char*)&pStat1[1]; + memcpy(pStat1->zName, "sqlite_stat1", 13); + pStat1->nCol = 3; + pStat1->iPKey = -1; + sqlite3VdbeAddOp4(pParse->pVdbe, OP_Noop, 0, 0, 0,(char*)pStat1,P4_DYNAMIC); + } +#endif + /* Establish a read-lock on the table at the shared-cache level. ** Open a read-only cursor on the table. Also allocate a cursor number ** to use for scanning indexes (iIdxCur). No index cursor is opened at @@ -1015,7 +1058,7 @@ static void analyzeOneTable( for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ int nCol; /* Number of columns in pIdx. "N" */ - int addrRewind; /* Address of "OP_Rewind iIdxCur" */ + int addrGotoEnd; /* Address of "OP_Rewind iIdxCur" */ int addrNextRow; /* Address of "next_row:" */ const char *zIdxName; /* Name of the index */ int nColTest; /* Number of columns to test for changes */ @@ -1039,9 +1082,14 @@ static void analyzeOneTable( /* ** Pseudo-code for loop that calls stat_push(): ** - ** Rewind csr - ** if eof(csr) goto end_of_scan; ** regChng = 0 + ** Rewind csr + ** if eof(csr){ + ** stat_init() with count = 0; + ** goto end_of_scan; + ** } + ** count() + ** stat_init() ** goto chng_addr_0; ** ** next_row: @@ -1072,7 +1120,7 @@ static void analyzeOneTable( ** the regPrev array and a trailing rowid (the rowid slot is required ** when building a record to insert into the sample column of ** the sqlite_stat4 table. */ - pParse->nMem = MAX(pParse->nMem, regPrev+nColTest); + sqlite3TouchRegister(pParse, regPrev+nColTest); /* Open a read-only cursor on the index being analyzed. */ assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) ); @@ -1080,40 +1128,41 @@ static void analyzeOneTable( sqlite3VdbeSetP4KeyInfo(pParse, pIdx); VdbeComment((v, "%s", pIdx->zName)); - /* Invoke the stat_init() function. The arguments are: - ** - ** (1) the number of columns in the index including the rowid - ** (or for a WITHOUT ROWID table, the number of PK columns), - ** (2) the number of columns in the key without the rowid/pk - ** (3) the number of rows in the index, - ** - ** - ** The third argument is only used for STAT3 and STAT4 - */ -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 - sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat4+3); -#endif - sqlite3VdbeAddOp2(v, OP_Integer, nCol, regStat4+1); - sqlite3VdbeAddOp2(v, OP_Integer, pIdx->nKeyCol, regStat4+2); - sqlite3VdbeAddOp4(v, OP_Function0, 0, regStat4+1, regStat4, - (char*)&statInitFuncdef, P4_FUNCDEF); - sqlite3VdbeChangeP5(v, 2+IsStat34); - /* Implementation of the following: ** - ** Rewind csr - ** if eof(csr) goto end_of_scan; ** regChng = 0 - ** goto next_push_0; - ** + ** Rewind csr + ** if eof(csr){ + ** stat_init() with count = 0; + ** goto end_of_scan; + ** } + ** count() + ** stat_init() + ** goto chng_addr_0; */ - addrRewind = sqlite3VdbeAddOp1(v, OP_Rewind, iIdxCur); + assert( regTemp2==regStat+4 ); + sqlite3VdbeAddOp2(v, OP_Integer, db->nAnalysisLimit, regTemp2); + + /* Arguments to stat_init(): + ** (1) the number of columns in the index including the rowid + ** (or for a WITHOUT ROWID table, the number of PK columns), + ** (2) the number of columns in the key without the rowid/pk + ** (3) estimated number of rows in the index. */ + sqlite3VdbeAddOp2(v, OP_Integer, nCol, regStat+1); + assert( regRowid==regStat+2 ); + sqlite3VdbeAddOp2(v, OP_Integer, pIdx->nKeyCol, regRowid); + sqlite3VdbeAddOp3(v, OP_Count, iIdxCur, regTemp, + OptimizationDisabled(db, SQLITE_Stat4)); + sqlite3VdbeAddFunctionCall(pParse, 0, regStat+1, regStat, 4, + &statInitFuncdef, 0); + addrGotoEnd = sqlite3VdbeAddOp1(v, OP_Rewind, iIdxCur); VdbeCoverage(v); + sqlite3VdbeAddOp2(v, OP_Integer, 0, regChng); addrNextRow = sqlite3VdbeCurrentAddr(v); if( nColTest>0 ){ - int endDistinctTest = sqlite3VdbeMakeLabel(v); + int endDistinctTest = sqlite3VdbeMakeLabel(pParse); int *aGotoChng; /* Array of jump instruction addresses */ aGotoChng = sqlite3DbMallocRawNN(db, sizeof(int)*nColTest); if( aGotoChng==0 ) continue; @@ -1141,6 +1190,7 @@ static void analyzeOneTable( char *pColl = (char*)sqlite3LocateCollSeq(pParse, pIdx->azColl[i]); sqlite3VdbeAddOp2(v, OP_Integer, i, regChng); sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regTemp); + analyzeVdbeCommentIndexWithColumnName(v,pIdx,i); aGotoChng[i] = sqlite3VdbeAddOp4(v, OP_Ne, regTemp, 0, regPrev+i, pColl, P4_COLLSEQ); sqlite3VdbeChangeP5(v, SQLITE_NULLEQ); @@ -1161,6 +1211,7 @@ static void analyzeOneTable( for(i=0; ipTable); - int j, k, regKey; - regKey = sqlite3GetTempRange(pParse, pPk->nKeyCol); - for(j=0; jnKeyCol; j++){ - k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]); - assert( k>=0 && knCol ); - sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, regKey+j); - VdbeComment((v, "%s", pTab->aCol[pPk->aiColumn[j]].zName)); +#ifdef SQLITE_ENABLE_STAT4 + if( OptimizationEnabled(db, SQLITE_Stat4) ){ + assert( regRowid==(regStat+2) ); + if( HasRowid(pTab) ){ + sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, regRowid); + }else{ + Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable); + int j, k, regKey; + regKey = sqlite3GetTempRange(pParse, pPk->nKeyCol); + for(j=0; jnKeyCol; j++){ + k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[j]); + assert( k>=0 && knColumn ); + sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, regKey+j); + analyzeVdbeCommentIndexWithColumnName(v,pIdx,k); + } + sqlite3VdbeAddOp3(v, OP_MakeRecord, regKey, pPk->nKeyCol, regRowid); + sqlite3ReleaseTempRange(pParse, regKey, pPk->nKeyCol); } - sqlite3VdbeAddOp3(v, OP_MakeRecord, regKey, pPk->nKeyCol, regRowid); - sqlite3ReleaseTempRange(pParse, regKey, pPk->nKeyCol); } #endif - assert( regChng==(regStat4+1) ); - sqlite3VdbeAddOp4(v, OP_Function0, 1, regStat4, regTemp, - (char*)&statPushFuncdef, P4_FUNCDEF); - sqlite3VdbeChangeP5(v, 2+IsStat34); - sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, addrNextRow); VdbeCoverage(v); + assert( regChng==(regStat+1) ); + { + sqlite3VdbeAddFunctionCall(pParse, 1, regStat, regTemp, 2+IsStat4, + &statPushFuncdef, 0); + if( db->nAnalysisLimit ){ + int j1, j2, j3; + j1 = sqlite3VdbeAddOp1(v, OP_IsNull, regTemp); VdbeCoverage(v); + j2 = sqlite3VdbeAddOp1(v, OP_If, regTemp); VdbeCoverage(v); + j3 = sqlite3VdbeAddOp4Int(v, OP_SeekGT, iIdxCur, 0, regPrev, 1); + VdbeCoverage(v); + sqlite3VdbeJumpHere(v, j1); + sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, addrNextRow); VdbeCoverage(v); + sqlite3VdbeJumpHere(v, j2); + sqlite3VdbeJumpHere(v, j3); + }else{ + sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, addrNextRow); VdbeCoverage(v); + } + } /* Add the entry to the stat1 table. */ - callStatGet(v, regStat4, STAT_GET_STAT1, regStat1); + if( pIdx->pPartIdxWhere ){ + /* Partial indexes might get a zero-entry in sqlite_stat1. But + ** an empty table is omitted from sqlite_stat1. */ + sqlite3VdbeJumpHere(v, addrGotoEnd); + addrGotoEnd = 0; + } + callStatGet(pParse, regStat, STAT_GET_STAT1, regStat1); assert( "BBB"[0]==SQLITE_AFF_TEXT ); sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "BBB", 0); sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid); sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regTemp, regNewRowid); +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK + sqlite3VdbeChangeP4(v, -1, (char*)pStat1, P4_TABLE); +#endif sqlite3VdbeChangeP5(v, OPFLAG_APPEND); - /* Add the entries to the stat3 or stat4 table. */ -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 - { + /* Add the entries to the stat4 table. */ +#ifdef SQLITE_ENABLE_STAT4 + if( OptimizationEnabled(db, SQLITE_Stat4) && db->nAnalysisLimit==0 ){ int regEq = regStat1; int regLt = regStat1+1; int regDLt = regStat1+2; @@ -1218,38 +1293,66 @@ static void analyzeOneTable( int addrIsNull; u8 seekOp = HasRowid(pTab) ? OP_NotExists : OP_NotFound; - pParse->nMem = MAX(pParse->nMem, regCol+nCol); + /* No STAT4 data is generated if the number of rows is zero */ + if( addrGotoEnd==0 ){ + sqlite3VdbeAddOp2(v, OP_Cast, regStat1, SQLITE_AFF_INTEGER); + addrGotoEnd = sqlite3VdbeAddOp1(v, OP_IfNot, regStat1); + VdbeCoverage(v); + } + + if( doOnce ){ + int mxCol = nCol; + Index *pX; + + /* Compute the maximum number of columns in any index */ + for(pX=pTab->pIndex; pX; pX=pX->pNext){ + int nColX; /* Number of columns in pX */ + if( !HasRowid(pTab) && IsPrimaryKeyIndex(pX) ){ + nColX = pX->nKeyCol; + }else{ + nColX = pX->nColumn; + } + if( nColX>mxCol ) mxCol = nColX; + } + + /* Allocate space to compute results for the largest index */ + sqlite3TouchRegister(pParse, regCol+mxCol); + doOnce = 0; +#ifdef SQLITE_DEBUG + /* Verify that the call to sqlite3ClearTempRegCache() below + ** really is needed. + ** https://sqlite.org/forum/forumpost/83cb4a95a0 (2023-03-25) + */ + testcase( !sqlite3NoTempsInRange(pParse, regEq, regCol+mxCol) ); +#endif + sqlite3ClearTempRegCache(pParse); /* tag-20230325-1 */ + assert( sqlite3NoTempsInRange(pParse, regEq, regCol+mxCol) ); + } + assert( sqlite3NoTempsInRange(pParse, regEq, regCol+nCol) ); addrNext = sqlite3VdbeCurrentAddr(v); - callStatGet(v, regStat4, STAT_GET_ROWID, regSampleRowid); + callStatGet(pParse, regStat, STAT_GET_ROWID, regSampleRowid); addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull, regSampleRowid); VdbeCoverage(v); - callStatGet(v, regStat4, STAT_GET_NEQ, regEq); - callStatGet(v, regStat4, STAT_GET_NLT, regLt); - callStatGet(v, regStat4, STAT_GET_NDLT, regDLt); + callStatGet(pParse, regStat, STAT_GET_NEQ, regEq); + callStatGet(pParse, regStat, STAT_GET_NLT, regLt); + callStatGet(pParse, regStat, STAT_GET_NDLT, regDLt); sqlite3VdbeAddOp4Int(v, seekOp, iTabCur, addrNext, regSampleRowid, 0); - /* We know that the regSampleRowid row exists because it was read by - ** the previous loop. Thus the not-found jump of seekOp will never - ** be taken */ - VdbeCoverageNeverTaken(v); -#ifdef SQLITE_ENABLE_STAT3 - sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iTabCur, 0, regSample); -#else + VdbeCoverage(v); for(i=0; itblHash); k; k=sqliteHashNext(k)){ Table *pTab = (Table*)sqliteHashData(k); analyzeOneTable(pParse, pTab, 0, iStatCur, iMem, iTab); +#ifdef SQLITE_ENABLE_STAT4 + iMem = sqlite3FirstAvailableRegister(pParse, iMem); +#else + assert( iMem==sqlite3FirstAvailableRegister(pParse,iMem) ); +#endif } loadAnalysis(pParse, iDb); } @@ -1367,27 +1478,14 @@ void sqlite3Analyze(Parse *pParse, Token *pName1, Token *pName2){ if( i==1 ) continue; /* Do not analyze the TEMP database */ analyzeDatabase(pParse, i); } - }else if( pName2->n==0 ){ - /* Form 2: Analyze the database or table named */ - iDb = sqlite3FindDb(db, pName1); - if( iDb>=0 ){ - analyzeDatabase(pParse, iDb); - }else{ - z = sqlite3NameFromToken(db, pName1); - if( z ){ - if( (pIdx = sqlite3FindIndex(db, z, 0))!=0 ){ - analyzeTable(pParse, pIdx->pTable, pIdx); - }else if( (pTab = sqlite3LocateTable(pParse, 0, z, 0))!=0 ){ - analyzeTable(pParse, pTab, 0); - } - sqlite3DbFree(db, z); - } - } + }else if( pName2->n==0 && (iDb = sqlite3FindDb(db, pName1))>=0 ){ + /* Analyze the schema named as the argument */ + analyzeDatabase(pParse, iDb); }else{ - /* Form 3: Analyze the fully qualified table name */ + /* Form 3: Analyze the table or index named as an argument */ iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pTableName); if( iDb>=0 ){ - zDb = db->aDb[iDb].zName; + zDb = pName2->n ? db->aDb[iDb].zDbSName : 0; z = sqlite3NameFromToken(db, pTableName); if( z ){ if( (pIdx = sqlite3FindIndex(db, z, zDb))!=0 ){ @@ -1397,10 +1495,11 @@ void sqlite3Analyze(Parse *pParse, Token *pName1, Token *pName2){ } sqlite3DbFree(db, z); } - } + } + } + if( db->nSqlExec==0 && (v = sqlite3GetVdbe(pParse))!=0 ){ + sqlite3VdbeAddOp0(v, OP_Expire); } - v = sqlite3GetVdbe(pParse); - if( v ) sqlite3VdbeAddOp0(v, OP_Expire); } /* @@ -1430,7 +1529,7 @@ static void decodeIntArray( int i; tRowcnt v; -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 +#ifdef SQLITE_ENABLE_STAT4 if( z==0 ) z = ""; #else assert( z!=0 ); @@ -1441,7 +1540,7 @@ static void decodeIntArray( v = v*10 + c - '0'; z++; } -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 +#ifdef SQLITE_ENABLE_STAT4 if( aOut ) aOut[i] = v; if( aLog ) aLog[i] = sqlite3LogEst(v); #else @@ -1452,7 +1551,7 @@ static void decodeIntArray( #endif if( *z==' ' ) z++; } -#ifndef SQLITE_ENABLE_STAT3_OR_STAT4 +#ifndef SQLITE_ENABLE_STAT4 assert( pIndex!=0 ); { #else if( pIndex ){ @@ -1463,7 +1562,9 @@ static void decodeIntArray( if( sqlite3_strglob("unordered*", z)==0 ){ pIndex->bUnordered = 1; }else if( sqlite3_strglob("sz=[0-9]*", z)==0 ){ - pIndex->szIdxRow = sqlite3LogEst(sqlite3Atoi(z+3)); + int sz = sqlite3Atoi(z+3); + if( sz<2 ) sz = 2; + pIndex->szIdxRow = sqlite3LogEst(sz); }else if( sqlite3_strglob("noskipscan*", z)==0 ){ pIndex->noSkipScan = 1; } @@ -1517,7 +1618,7 @@ static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){ if( pIndex ){ tRowcnt *aiRowEst = 0; int nCol = pIndex->nKeyCol+1; -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 +#ifdef SQLITE_ENABLE_STAT4 /* Index.aiRowEst may already be set here if there are duplicate ** sqlite_stat1 entries for this index. In that case just clobber ** the old data with the new instead of allocating a new array. */ @@ -1529,7 +1630,11 @@ static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){ #endif pIndex->bUnordered = 0; decodeIntArray((char*)z, nCol, aiRowEst, pIndex->aiRowLogEst, pIndex); - if( pIndex->pPartIdxWhere==0 ) pTable->nRowLogEst = pIndex->aiRowLogEst[0]; + pIndex->hasStat1 = 1; + if( pIndex->pPartIdxWhere==0 ){ + pTable->nRowLogEst = pIndex->aiRowLogEst[0]; + pTable->tabFlags |= TF_HasStat1; + } }else{ Index fakeIdx; fakeIdx.szIdxRow = pTable->szTabRow; @@ -1538,6 +1643,7 @@ static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){ #endif decodeIntArray((char*)z, 1, 0, &pTable->nRowLogEst, &fakeIdx); pTable->szTabRow = fakeIdx.szIdxRow; + pTable->tabFlags |= TF_HasStat1; } return 0; @@ -1548,7 +1654,9 @@ static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){ ** and its contents. */ void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){ -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 + assert( db!=0 ); + assert( pIdx!=0 ); +#ifdef SQLITE_ENABLE_STAT4 if( pIdx->aSample ){ int j; for(j=0; jnSample; j++){ @@ -1557,17 +1665,17 @@ void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){ } sqlite3DbFree(db, pIdx->aSample); } - if( db && db->pnBytesFreed==0 ){ + if( db->pnBytesFreed==0 ){ pIdx->nSample = 0; pIdx->aSample = 0; } #else UNUSED_PARAMETER(db); UNUSED_PARAMETER(pIdx); -#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ +#endif /* SQLITE_ENABLE_STAT4 */ } -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 +#ifdef SQLITE_ENABLE_STAT4 /* ** Populate the pIdx->aAvgEq[] array based on the samples currently ** stored in pIdx->aSample[]. @@ -1618,7 +1726,7 @@ static void initAvgEq(Index *pIdx){ } } - if( nDist100>nSum100 ){ + if( nDist100>nSum100 && sumEqlookaside.bDisable ); zSql = sqlite3MPrintf(db, zSql1, zDb); if( !zSql ){ - return SQLITE_NOMEM; + return SQLITE_NOMEM_BKPT; } rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0); sqlite3DbFree(db, zSql); @@ -1682,41 +1788,47 @@ static int loadStatTbl( while( sqlite3_step(pStmt)==SQLITE_ROW ){ int nIdxCol = 1; /* Number of columns in stat4 records */ - char *zIndex; /* Index name */ - Index *pIdx; /* Pointer to the index object */ - int nSample; /* Number of samples */ - int nByte; /* Bytes of space required */ - int i; /* Bytes of space required */ - tRowcnt *pSpace; + char *zIndex; /* Index name */ + Index *pIdx; /* Pointer to the index object */ + int nSample; /* Number of samples */ + i64 nByte; /* Bytes of space required */ + i64 i; /* Bytes of space required */ + tRowcnt *pSpace; /* Available allocated memory space */ + u8 *pPtr; /* Available memory as a u8 for easier manipulation */ zIndex = (char *)sqlite3_column_text(pStmt, 0); if( zIndex==0 ) continue; nSample = sqlite3_column_int(pStmt, 1); pIdx = findIndexOrPrimaryKey(db, zIndex, zDb); - assert( pIdx==0 || bStat3 || pIdx->nSample==0 ); - /* Index.nSample is non-zero at this point if data has already been - ** loaded from the stat4 table. In this case ignore stat3 data. */ - if( pIdx==0 || pIdx->nSample ) continue; - if( bStat3==0 ){ - assert( !HasRowid(pIdx->pTable) || pIdx->nColumn==pIdx->nKeyCol+1 ); - if( !HasRowid(pIdx->pTable) && IsPrimaryKeyIndex(pIdx) ){ - nIdxCol = pIdx->nKeyCol; - }else{ - nIdxCol = pIdx->nColumn; - } + assert( pIdx==0 || pIdx->nSample==0 ); + if( pIdx==0 ) continue; + if( pIdx->aSample!=0 ){ + /* The same index appears in sqlite_stat4 under multiple names */ + continue; + } + assert( !HasRowid(pIdx->pTable) || pIdx->nColumn==pIdx->nKeyCol+1 ); + if( !HasRowid(pIdx->pTable) && IsPrimaryKeyIndex(pIdx) ){ + nIdxCol = pIdx->nKeyCol; + }else{ + nIdxCol = pIdx->nColumn; } pIdx->nSampleCol = nIdxCol; - nByte = sizeof(IndexSample) * nSample; + pIdx->mxSample = nSample; + nByte = ROUND8(sizeof(IndexSample) * nSample); nByte += sizeof(tRowcnt) * nIdxCol * 3 * nSample; nByte += nIdxCol * sizeof(tRowcnt); /* Space for Index.aAvgEq[] */ pIdx->aSample = sqlite3DbMallocZero(db, nByte); if( pIdx->aSample==0 ){ sqlite3_finalize(pStmt); - return SQLITE_NOMEM; + return SQLITE_NOMEM_BKPT; } - pSpace = (tRowcnt*)&pIdx->aSample[nSample]; + pPtr = (u8*)pIdx->aSample; + pPtr += ROUND8(nSample*sizeof(pIdx->aSample[0])); + pSpace = (tRowcnt*)pPtr; + assert( EIGHT_BYTE_ALIGNMENT( pSpace ) ); pIdx->aAvgEq = pSpace; pSpace += nIdxCol; + pIdx->pTable->tabFlags |= TF_HasStat4; for(i=0; iaSample[i].anEq = pSpace; pSpace += nIdxCol; pIdx->aSample[i].anLt = pSpace; pSpace += nIdxCol; @@ -1729,7 +1841,7 @@ static int loadStatTbl( zSql = sqlite3MPrintf(db, zSql2, zDb); if( !zSql ){ - return SQLITE_NOMEM; + return SQLITE_NOMEM_BKPT; } rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0); sqlite3DbFree(db, zSql); @@ -1744,10 +1856,14 @@ static int loadStatTbl( if( zIndex==0 ) continue; pIdx = findIndexOrPrimaryKey(db, zIndex, zDb); if( pIdx==0 ) continue; + if( pIdx->nSample>=pIdx->mxSample ){ + /* Too many slots used because the same index appears in + ** sqlite_stat4 using multiple names */ + continue; + } /* This next condition is true if data has already been loaded from - ** the sqlite_stat4 table. In this case ignore stat3 data. */ + ** the sqlite_stat4 table. */ nCol = pIdx->nSampleCol; - if( bStat3 && nCol>1 ) continue; if( pIdx!=pPrevIdx ){ initAvgEq(pPrevIdx); pPrevIdx = pIdx; @@ -1757,19 +1873,22 @@ static int loadStatTbl( decodeIntArray((char*)sqlite3_column_text(pStmt,2),nCol,pSample->anLt,0,0); decodeIntArray((char*)sqlite3_column_text(pStmt,3),nCol,pSample->anDLt,0,0); - /* Take a copy of the sample. Add two 0x00 bytes the end of the buffer. + /* Take a copy of the sample. Add 8 extra 0x00 bytes the end of the buffer. ** This is in case the sample record is corrupted. In that case, the ** sqlite3VdbeRecordCompare() may read up to two varints past the ** end of the allocated buffer before it realizes it is dealing with - ** a corrupt record. Adding the two 0x00 bytes prevents this from causing + ** a corrupt record. Or it might try to read a large integer from the + ** buffer. In any case, eight 0x00 bytes prevents this from causing ** a buffer overread. */ pSample->n = sqlite3_column_bytes(pStmt, 4); - pSample->p = sqlite3DbMallocZero(db, pSample->n + 2); + pSample->p = sqlite3DbMallocZero(db, pSample->n + 8); if( pSample->p==0 ){ sqlite3_finalize(pStmt); - return SQLITE_NOMEM; + return SQLITE_NOMEM_BKPT; + } + if( pSample->n ){ + memcpy(pSample->p, sqlite3_column_blob(pStmt, 4), pSample->n); } - memcpy(pSample->p, sqlite3_column_blob(pStmt, 4), pSample->n); pIdx->nSample++; } rc = sqlite3_finalize(pStmt); @@ -1778,45 +1897,40 @@ static int loadStatTbl( } /* -** Load content from the sqlite_stat4 and sqlite_stat3 tables into +** Load content from the sqlite_stat4 table into ** the Index.aSample[] arrays of all indices. */ static int loadStat4(sqlite3 *db, const char *zDb){ int rc = SQLITE_OK; /* Result codes from subroutines */ + const Table *pStat4; assert( db->lookaside.bDisable ); - if( sqlite3FindTable(db, "sqlite_stat4", zDb) ){ - rc = loadStatTbl(db, 0, - "SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx", + if( OptimizationEnabled(db, SQLITE_Stat4) + && (pStat4 = sqlite3FindTable(db, "sqlite_stat4", zDb))!=0 + && IsOrdinaryTable(pStat4) + ){ + rc = loadStatTbl(db, + "SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx COLLATE nocase", "SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4", zDb ); } - - if( rc==SQLITE_OK && sqlite3FindTable(db, "sqlite_stat3", zDb) ){ - rc = loadStatTbl(db, 1, - "SELECT idx,count(*) FROM %Q.sqlite_stat3 GROUP BY idx", - "SELECT idx,neq,nlt,ndlt,sqlite_record(sample) FROM %Q.sqlite_stat3", - zDb - ); - } - return rc; } -#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ +#endif /* SQLITE_ENABLE_STAT4 */ /* -** Load the content of the sqlite_stat1 and sqlite_stat3/4 tables. The +** Load the content of the sqlite_stat1 and sqlite_stat4 tables. The ** contents of sqlite_stat1 are used to populate the Index.aiRowEst[] -** arrays. The contents of sqlite_stat3/4 are used to populate the +** arrays. The contents of sqlite_stat4 are used to populate the ** Index.aSample[] arrays. ** ** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR -** is returned. In this case, even if SQLITE_ENABLE_STAT3/4 was defined -** during compilation and the sqlite_stat3/4 table is present, no data is +** is returned. In this case, even if SQLITE_ENABLE_STAT4 was defined +** during compilation and the sqlite_stat4 table is present, no data is ** read from it. ** -** If SQLITE_ENABLE_STAT3/4 was defined during compilation and the +** If SQLITE_ENABLE_STAT4 was defined during compilation and the ** sqlite_stat4 table is not present in the database, SQLITE_ERROR is ** returned. However, in this case, data is read from the sqlite_stat1 ** table (if it is present) before returning. @@ -1829,48 +1943,59 @@ int sqlite3AnalysisLoad(sqlite3 *db, int iDb){ analysisInfo sInfo; HashElem *i; char *zSql; - int rc; + int rc = SQLITE_OK; + Schema *pSchema = db->aDb[iDb].pSchema; + const Table *pStat1; assert( iDb>=0 && iDbnDb ); assert( db->aDb[iDb].pBt!=0 ); /* Clear any prior statistics */ assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); - for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){ + for(i=sqliteHashFirst(&pSchema->tblHash); i; i=sqliteHashNext(i)){ + Table *pTab = sqliteHashData(i); + pTab->tabFlags &= ~TF_HasStat1; + } + for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){ Index *pIdx = sqliteHashData(i); - sqlite3DefaultRowEst(pIdx); -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 + pIdx->hasStat1 = 0; +#ifdef SQLITE_ENABLE_STAT4 sqlite3DeleteIndexSamples(db, pIdx); pIdx->aSample = 0; #endif } - /* Check to make sure the sqlite_stat1 table exists */ + /* Load new statistics out of the sqlite_stat1 table */ sInfo.db = db; - sInfo.zDatabase = db->aDb[iDb].zName; - if( sqlite3FindTable(db, "sqlite_stat1", sInfo.zDatabase)==0 ){ - return SQLITE_ERROR; + sInfo.zDatabase = db->aDb[iDb].zDbSName; + if( (pStat1 = sqlite3FindTable(db, "sqlite_stat1", sInfo.zDatabase)) + && IsOrdinaryTable(pStat1) + ){ + zSql = sqlite3MPrintf(db, + "SELECT tbl,idx,stat FROM %Q.sqlite_stat1", sInfo.zDatabase); + if( zSql==0 ){ + rc = SQLITE_NOMEM_BKPT; + }else{ + rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0); + sqlite3DbFree(db, zSql); + } } - /* Load new statistics out of the sqlite_stat1 table */ - zSql = sqlite3MPrintf(db, - "SELECT tbl,idx,stat FROM %Q.sqlite_stat1", sInfo.zDatabase); - if( zSql==0 ){ - rc = SQLITE_NOMEM; - }else{ - rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0); - sqlite3DbFree(db, zSql); + /* Set appropriate defaults on all indexes not in the sqlite_stat1 table */ + assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); + for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){ + Index *pIdx = sqliteHashData(i); + if( !pIdx->hasStat1 ) sqlite3DefaultRowEst(pIdx); } - /* Load the statistics from the sqlite_stat4 table. */ -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 - if( rc==SQLITE_OK && OptimizationEnabled(db, SQLITE_Stat34) ){ - db->lookaside.bDisable++; +#ifdef SQLITE_ENABLE_STAT4 + if( rc==SQLITE_OK ){ + DisableLookaside; rc = loadStat4(db, sInfo.zDatabase); - db->lookaside.bDisable--; + EnableLookaside; } - for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){ + for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){ Index *pIdx = sqliteHashData(i); sqlite3_free(pIdx->aiRowEst); pIdx->aiRowEst = 0; diff --git a/src/attach.c b/src/attach.c index 2288ac9b62..b442e54512 100644 --- a/src/attach.c +++ b/src/attach.c @@ -45,6 +45,17 @@ static int resolveAttachExpr(NameContext *pName, Expr *pExpr) return rc; } +/* +** Return true if zName points to a name that may be used to refer to +** database iDb attached to handle db. +*/ +int sqlite3DbIsNamed(sqlite3 *db, int iDb, const char *zName){ + return ( + sqlite3StrICmp(db->aDb[iDb].zDbSName, zName)==0 + || (iDb==0 && sqlite3StrICmp("main", zName)==0) + ); +} + /* ** An SQL user-function registered to do the work of an ATTACH statement. The ** three arguments to the function come directly from an attach statement: @@ -55,6 +66,10 @@ static int resolveAttachExpr(NameContext *pName, Expr *pExpr) ** ** If the optional "KEY z" syntax is omitted, an SQL NULL is passed as the ** third argument. +** +** If the db->init.reopenMemdb flags is set, then instead of attaching a +** new database, close the database on db->init.iDb and reopen it as an +** empty MemDB. */ static void attachFunc( sqlite3_context *context, @@ -69,109 +84,145 @@ static void attachFunc( char *zPath = 0; char *zErr = 0; unsigned int flags; - Db *aNew; + Db *aNew; /* New array of Db pointers */ + Db *pNew = 0; /* Db object for the newly attached database */ char *zErrDyn = 0; sqlite3_vfs *pVfs; UNUSED_PARAMETER(NotUsed); - zFile = (const char *)sqlite3_value_text(argv[0]); zName = (const char *)sqlite3_value_text(argv[1]); if( zFile==0 ) zFile = ""; if( zName==0 ) zName = ""; - /* Check for the following errors: - ** - ** * Too many attached databases, - ** * Transaction currently open - ** * Specified database name already being used. - */ - if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){ - zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d", - db->aLimit[SQLITE_LIMIT_ATTACHED] - ); - goto attach_error; - } - if( !db->autoCommit ){ - zErrDyn = sqlite3MPrintf(db, "cannot ATTACH database within transaction"); - goto attach_error; - } - for(i=0; inDb; i++){ - char *z = db->aDb[i].zName; - assert( z && zName ); - if( sqlite3StrICmp(z, zName)==0 ){ - zErrDyn = sqlite3MPrintf(db, "database %s is already in use", zName); - goto attach_error; - } - } +#ifndef SQLITE_OMIT_DESERIALIZE +# define REOPEN_AS_MEMDB(db) (db->init.reopenMemdb) +#else +# define REOPEN_AS_MEMDB(db) (0) +#endif - /* Allocate the new entry in the db->aDb[] array and initialize the schema - ** hash tables. - */ - if( db->aDb==db->aDbStatic ){ - aNew = sqlite3DbMallocRawNN(db, sizeof(db->aDb[0])*3 ); - if( aNew==0 ) return; - memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2); + if( REOPEN_AS_MEMDB(db) ){ + /* This is not a real ATTACH. Instead, this routine is being called + ** from sqlite3_deserialize() to close database db->init.iDb and + ** reopen it as a MemDB */ + Btree *pNewBt = 0; + pVfs = sqlite3_vfs_find("memdb"); + if( pVfs==0 ) return; + rc = sqlite3BtreeOpen(pVfs, "x\0", db, &pNewBt, 0, SQLITE_OPEN_MAIN_DB); + if( rc==SQLITE_OK ){ + Schema *pNewSchema = sqlite3SchemaGet(db, pNewBt); + if( pNewSchema ){ + /* Both the Btree and the new Schema were allocated successfully. + ** Close the old db and update the aDb[] slot with the new memdb + ** values. */ + pNew = &db->aDb[db->init.iDb]; + if( ALWAYS(pNew->pBt) ) sqlite3BtreeClose(pNew->pBt); + pNew->pBt = pNewBt; + pNew->pSchema = pNewSchema; + }else{ + sqlite3BtreeClose(pNewBt); + rc = SQLITE_NOMEM; + } + } + if( rc ) goto attach_error; }else{ - aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) ); - if( aNew==0 ) return; - } - db->aDb = aNew; - aNew = &db->aDb[db->nDb]; - memset(aNew, 0, sizeof(*aNew)); - - /* Open the database file. If the btree is successfully opened, use - ** it to obtain the database schema. At this point the schema may - ** or may not be initialized. - */ - flags = db->openFlags; - rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr); - if( rc!=SQLITE_OK ){ - if( rc==SQLITE_NOMEM ) sqlite3OomFault(db); - sqlite3_result_error(context, zErr, -1); - sqlite3_free(zErr); - return; + /* This is a real ATTACH + ** + ** Check for the following errors: + ** + ** * Too many attached databases, + ** * Transaction currently open + ** * Specified database name already being used. + */ + if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){ + zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d", + db->aLimit[SQLITE_LIMIT_ATTACHED] + ); + goto attach_error; + } + for(i=0; inDb; i++){ + assert( zName ); + if( sqlite3DbIsNamed(db, i, zName) ){ + zErrDyn = sqlite3MPrintf(db, "database %s is already in use", zName); + goto attach_error; + } + } + + /* Allocate the new entry in the db->aDb[] array and initialize the schema + ** hash tables. + */ + if( db->aDb==db->aDbStatic ){ + aNew = sqlite3DbMallocRawNN(db, sizeof(db->aDb[0])*3 ); + if( aNew==0 ) return; + memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2); + }else{ + aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(1+(i64)db->nDb)); + if( aNew==0 ) return; + } + db->aDb = aNew; + pNew = &db->aDb[db->nDb]; + memset(pNew, 0, sizeof(*pNew)); + + /* Open the database file. If the btree is successfully opened, use + ** it to obtain the database schema. At this point the schema may + ** or may not be initialized. + */ + flags = db->openFlags; + rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr); + if( rc!=SQLITE_OK ){ + if( rc==SQLITE_NOMEM ) sqlite3OomFault(db); + sqlite3_result_error(context, zErr, -1); + sqlite3_free(zErr); + return; + } + if( (db->flags & SQLITE_AttachWrite)==0 ){ + flags &= ~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE); + flags |= SQLITE_OPEN_READONLY; + }else if( (db->flags & SQLITE_AttachCreate)==0 ){ + flags &= ~SQLITE_OPEN_CREATE; + } + assert( pVfs ); + flags |= SQLITE_OPEN_MAIN_DB; + rc = sqlite3BtreeOpen(pVfs, zPath, db, &pNew->pBt, 0, flags); + db->nDb++; + pNew->zDbSName = sqlite3DbStrDup(db, zName); } - assert( pVfs ); - flags |= SQLITE_OPEN_MAIN_DB; - rc = sqlite3BtreeOpen(pVfs, zPath, db, &aNew->pBt, 0, flags); - sqlite3_free( zPath ); - db->nDb++; + db->noSharedCache = 0; if( rc==SQLITE_CONSTRAINT ){ rc = SQLITE_ERROR; zErrDyn = sqlite3MPrintf(db, "database is already attached"); }else if( rc==SQLITE_OK ){ Pager *pPager; - aNew->pSchema = sqlite3SchemaGet(db, aNew->pBt); - if( !aNew->pSchema ){ - rc = SQLITE_NOMEM; - }else if( aNew->pSchema->file_format && aNew->pSchema->enc!=ENC(db) ){ + pNew->pSchema = sqlite3SchemaGet(db, pNew->pBt); + if( !pNew->pSchema ){ + rc = SQLITE_NOMEM_BKPT; + }else if( pNew->pSchema->file_format && pNew->pSchema->enc!=ENC(db) ){ zErrDyn = sqlite3MPrintf(db, "attached databases must use the same text encoding as main database"); rc = SQLITE_ERROR; } - sqlite3BtreeEnter(aNew->pBt); - pPager = sqlite3BtreePager(aNew->pBt); + sqlite3BtreeEnter(pNew->pBt); + pPager = sqlite3BtreePager(pNew->pBt); sqlite3PagerLockingMode(pPager, db->dfltLockMode); - sqlite3BtreeSecureDelete(aNew->pBt, + sqlite3BtreeSecureDelete(pNew->pBt, sqlite3BtreeSecureDelete(db->aDb[0].pBt,-1) ); #ifndef SQLITE_OMIT_PAGER_PRAGMAS - sqlite3BtreeSetPagerFlags(aNew->pBt, + sqlite3BtreeSetPagerFlags(pNew->pBt, PAGER_SYNCHRONOUS_FULL | (db->flags & PAGER_FLAGS_MASK)); #endif - sqlite3BtreeLeave(aNew->pBt); + sqlite3BtreeLeave(pNew->pBt); } - aNew->safety_level = 3; - aNew->zName = sqlite3DbStrDup(db, zName); - if( rc==SQLITE_OK && aNew->zName==0 ){ - rc = SQLITE_NOMEM; + pNew->safety_level = SQLITE_DEFAULT_SYNCHRONOUS+1; + if( rc==SQLITE_OK && pNew->zDbSName==0 ){ + rc = SQLITE_NOMEM_BKPT; } - +/* BEGIN SQLCIPHER */ #ifdef SQLITE_HAS_CODEC if( rc==SQLITE_OK ){ - extern int sqlite3CodecAttach(sqlite3*, int, const void*, int); - extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*); + extern int sqlcipherCodecAttach(sqlite3*, int, const void*, int); + extern void sqlcipherCodecGetKey(sqlite3*, int, void**, int*); + extern void sqlcipher_free(void*, sqlite3_uint64); int nKey; char *zKey; int t = sqlite3_value_type(argv[2]); @@ -186,55 +237,73 @@ static void attachFunc( case SQLITE_BLOB: nKey = sqlite3_value_bytes(argv[2]); zKey = (char *)sqlite3_value_blob(argv[2]); - rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey); + /* SQLCipher allows a special case to attach a plaintext database + * to an encrypted database by passing key as an empty string, eg. + * ATTACH DATABASE 'plain.db' AS plain KEY ''; + * In this case, do not attempt to attach a codec to the attached + * database */ + if(nKey && zKey) { + rc = sqlcipherCodecAttach(db, db->nDb-1, zKey, nKey); + } break; case SQLITE_NULL: - /* No key specified. Use the key from the main database */ - sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey); - if( nKey>0 || sqlite3BtreeGetOptimalReserve(db->aDb[0].pBt)>0 ){ - rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey); + /* No key specified. Use the key from URI filename, or if none, + ** use the key from the main database. */ + if( sqlite3CodecQueryParameters(db, zName, zPath)==0 ){ + sqlcipherCodecGetKey(db, 0, (void**)&zKey, &nKey); + if( nKey || sqlite3BtreeGetRequestedReserve(db->aDb[0].pBt)>0 ){ + rc = sqlcipherCodecAttach(db, db->nDb-1, zKey, nKey); + } + if(nKey) sqlcipher_free(zKey, nKey); } break; } } #endif +/* END SQLCIPHER */ + sqlite3_free_filename( zPath ); /* If the file was opened successfully, read the schema for the new database. ** If this fails, or if opening the file failed, then close the file and - ** remove the entry from the db->aDb[] array. i.e. put everything back the way - ** we found it. + ** remove the entry from the db->aDb[] array. i.e. put everything back the + ** way we found it. */ if( rc==SQLITE_OK ){ sqlite3BtreeEnterAll(db); - rc = sqlite3Init(db, &zErrDyn); - sqlite3BtreeLeaveAll(db); - } -#ifdef SQLITE_USER_AUTHENTICATION - if( rc==SQLITE_OK ){ - u8 newAuth = 0; - rc = sqlite3UserAuthCheckLogin(db, zName, &newAuth); - if( newAuthauth.authLevel ){ - rc = SQLITE_AUTH_USER; + db->init.iDb = 0; + db->mDbFlags &= ~(DBFLAG_SchemaKnownOk); +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT + if( db->setlkFlags & SQLITE_SETLK_BLOCK_ON_CONNECT ){ + int val = 1; + sqlite3_file *fd = sqlite3PagerFile(sqlite3BtreePager(pNew->pBt)); + sqlite3OsFileControlHint(fd, SQLITE_FCNTL_BLOCK_ON_CONNECT, &val); } - } #endif - if( rc ){ - int iDb = db->nDb - 1; - assert( iDb>=2 ); - if( db->aDb[iDb].pBt ){ - sqlite3BtreeClose(db->aDb[iDb].pBt); - db->aDb[iDb].pBt = 0; - db->aDb[iDb].pSchema = 0; + if( !REOPEN_AS_MEMDB(db) ){ + rc = sqlite3Init(db, &zErrDyn); } - sqlite3ResetAllSchemasOfConnection(db); - db->nDb = iDb; - if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){ - sqlite3OomFault(db); - sqlite3DbFree(db, zErrDyn); - zErrDyn = sqlite3MPrintf(db, "out of memory"); - }else if( zErrDyn==0 ){ - zErrDyn = sqlite3MPrintf(db, "unable to open database: %s", zFile); + sqlite3BtreeLeaveAll(db); + assert( zErrDyn==0 || rc!=SQLITE_OK ); + } + if( rc ){ + if( ALWAYS(!REOPEN_AS_MEMDB(db)) ){ + int iDb = db->nDb - 1; + assert( iDb>=2 ); + if( db->aDb[iDb].pBt ){ + sqlite3BtreeClose(db->aDb[iDb].pBt); + db->aDb[iDb].pBt = 0; + db->aDb[iDb].pSchema = 0; + } + sqlite3ResetAllSchemasOfConnection(db); + db->nDb = iDb; + if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){ + sqlite3OomFault(db); + sqlite3DbFree(db, zErrDyn); + zErrDyn = sqlite3MPrintf(db, "out of memory"); + }else if( zErrDyn==0 ){ + zErrDyn = sqlite3MPrintf(db, "unable to open database: %s", zFile); + } } goto attach_error; } @@ -267,6 +336,7 @@ static void detachFunc( sqlite3 *db = sqlite3_context_db_handle(context); int i; Db *pDb = 0; + HashElem *pEntry; char zErr[128]; UNUSED_PARAMETER(NotUsed); @@ -275,7 +345,7 @@ static void detachFunc( for(i=0; inDb; i++){ pDb = &db->aDb[i]; if( pDb->pBt==0 ) continue; - if( sqlite3StrICmp(pDb->zName, zName)==0 ) break; + if( sqlite3DbIsNamed(db, i, zName) ) break; } if( i>=db->nDb ){ @@ -286,16 +356,25 @@ static void detachFunc( sqlite3_snprintf(sizeof(zErr),zErr, "cannot detach database %s", zName); goto detach_error; } - if( !db->autoCommit ){ - sqlite3_snprintf(sizeof(zErr), zErr, - "cannot DETACH database within transaction"); - goto detach_error; - } - if( sqlite3BtreeIsInReadTrans(pDb->pBt) || sqlite3BtreeIsInBackup(pDb->pBt) ){ + if( sqlite3BtreeTxnState(pDb->pBt)!=SQLITE_TXN_NONE + || sqlite3BtreeIsInBackup(pDb->pBt) + ){ sqlite3_snprintf(sizeof(zErr),zErr, "database %s is locked", zName); goto detach_error; } + /* If any TEMP triggers reference the schema being detached, move those + ** triggers to reference the TEMP schema itself. */ + assert( db->aDb[1].pSchema ); + pEntry = sqliteHashFirst(&db->aDb[1].pSchema->trigHash); + while( pEntry ){ + Trigger *pTrig = (Trigger*)sqliteHashData(pEntry); + if( pTrig->pTabSchema==pDb->pSchema ){ + pTrig->pTabSchema = pTrig->pSchema; + } + pEntry = sqliteHashNext(pEntry); + } + sqlite3BtreeClose(pDb->pBt); pDb->pBt = 0; pDb->pSchema = 0; @@ -325,21 +404,25 @@ static void codeAttach( sqlite3* db = pParse->db; int regArgs; + if( SQLITE_OK!=sqlite3ReadSchema(pParse) ) goto attach_end; + + if( pParse->nErr ) goto attach_end; memset(&sName, 0, sizeof(NameContext)); sName.pParse = pParse; if( - SQLITE_OK!=(rc = resolveAttachExpr(&sName, pFilename)) || - SQLITE_OK!=(rc = resolveAttachExpr(&sName, pDbname)) || - SQLITE_OK!=(rc = resolveAttachExpr(&sName, pKey)) + SQLITE_OK!=resolveAttachExpr(&sName, pFilename) || + SQLITE_OK!=resolveAttachExpr(&sName, pDbname) || + SQLITE_OK!=resolveAttachExpr(&sName, pKey) ){ goto attach_end; } #ifndef SQLITE_OMIT_AUTHORIZATION - if( pAuthArg ){ + if( ALWAYS(pAuthArg) ){ char *zAuthArg; if( pAuthArg->op==TK_STRING ){ + assert( !ExprHasProperty(pAuthArg, EP_IntValue) ); zAuthArg = pAuthArg->u.zToken; }else{ zAuthArg = 0; @@ -360,11 +443,8 @@ static void codeAttach( assert( v || db->mallocFailed ); if( v ){ - sqlite3VdbeAddOp4(v, OP_Function0, 0, regArgs+3-pFunc->nArg, regArgs+3, - (char *)pFunc, P4_FUNCDEF); - assert( pFunc->nArg==-1 || (pFunc->nArg&0xff)==pFunc->nArg ); - sqlite3VdbeChangeP5(v, (u8)(pFunc->nArg)); - + sqlite3VdbeAddFunctionCall(pParse, 0, regArgs+3-pFunc->nArg, regArgs+3, + pFunc->nArg, pFunc, 0); /* Code an OP_Expire. For an ATTACH statement, set P1 to true (expire this ** statement only). For DETACH, set it to false (expire all existing ** statements). @@ -391,9 +471,9 @@ void sqlite3Detach(Parse *pParse, Expr *pDbname){ 0, /* pNext */ detachFunc, /* xSFunc */ 0, /* xFinalize */ + 0, 0, /* xValue, xInverse */ "sqlite_detach", /* zName */ - 0, /* pHash */ - 0 /* pDestructor */ + {0} }; codeAttach(pParse, SQLITE_DETACH, &detach_func, pDbname, 0, 0, pDbname); } @@ -411,14 +491,78 @@ void sqlite3Attach(Parse *pParse, Expr *p, Expr *pDbname, Expr *pKey){ 0, /* pNext */ attachFunc, /* xSFunc */ 0, /* xFinalize */ + 0, 0, /* xValue, xInverse */ "sqlite_attach", /* zName */ - 0, /* pHash */ - 0 /* pDestructor */ + {0} }; codeAttach(pParse, SQLITE_ATTACH, &attach_func, p, p, pDbname, pKey); } #endif /* SQLITE_OMIT_ATTACH */ +/* +** Expression callback used by sqlite3FixAAAA() routines. +*/ +static int fixExprCb(Walker *p, Expr *pExpr){ + DbFixer *pFix = p->u.pFix; + if( !pFix->bTemp ) ExprSetProperty(pExpr, EP_FromDDL); + if( pExpr->op==TK_VARIABLE ){ + if( pFix->pParse->db->init.busy ){ + pExpr->op = TK_NULL; + }else{ + sqlite3ErrorMsg(pFix->pParse, "%s cannot use variables", pFix->zType); + return WRC_Abort; + } + } + return WRC_Continue; +} + +/* +** Select callback used by sqlite3FixAAAA() routines. +*/ +static int fixSelectCb(Walker *p, Select *pSelect){ + DbFixer *pFix = p->u.pFix; + int i; + SrcItem *pItem; + sqlite3 *db = pFix->pParse->db; + int iDb = sqlite3FindDbName(db, pFix->zDb); + SrcList *pList = pSelect->pSrc; + + if( NEVER(pList==0) ) return WRC_Continue; + for(i=0, pItem=pList->a; inSrc; i++, pItem++){ + if( pFix->bTemp==0 && pItem->fg.isSubquery==0 ){ + if( pItem->fg.fixedSchema==0 && pItem->u4.zDatabase!=0 ){ + if( iDb!=sqlite3FindDbName(db, pItem->u4.zDatabase) ){ + sqlite3ErrorMsg(pFix->pParse, + "%s %T cannot reference objects in database %s", + pFix->zType, pFix->pName, pItem->u4.zDatabase); + return WRC_Abort; + } + sqlite3DbFree(db, pItem->u4.zDatabase); + pItem->fg.notCte = 1; + pItem->fg.hadSchema = 1; + } + pItem->u4.pSchema = pFix->pSchema; + pItem->fg.fromDDL = 1; + pItem->fg.fixedSchema = 1; + } +#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) + if( pList->a[i].fg.isUsing==0 + && sqlite3WalkExpr(&pFix->w, pList->a[i].u3.pOn) + ){ + return WRC_Abort; + } +#endif + } + if( pSelect->pWith ){ + for(i=0; ipWith->nCte; i++){ + if( sqlite3WalkSelect(p, pSelect->pWith->a[i].pSelect) ){ + return WRC_Abort; + } + } + } + return WRC_Continue; +} + /* ** Initialize a DbFixer structure. This routine must be called prior ** to passing the structure to one of the sqliteFixAAAA() routines below. @@ -430,16 +574,21 @@ void sqlite3FixInit( const char *zType, /* "view", "trigger", or "index" */ const Token *pName /* Name of the view, trigger, or index */ ){ - sqlite3 *db; - - db = pParse->db; + sqlite3 *db = pParse->db; assert( db->nDb>iDb ); pFix->pParse = pParse; - pFix->zDb = db->aDb[iDb].zName; + pFix->zDb = db->aDb[iDb].zDbSName; pFix->pSchema = db->aDb[iDb].pSchema; pFix->zType = zType; pFix->pName = pName; - pFix->bVarOnly = (iDb==1); + pFix->bTemp = (iDb==1); + pFix->w.pParse = pParse; + pFix->w.xExprCallback = fixExprCb; + pFix->w.xSelectCallback = fixSelectCb; + pFix->w.xSelectCallback2 = sqlite3WalkWinDefnDummyCallback; + pFix->w.walkerDepth = 0; + pFix->w.eCode = 0; + pFix->w.u.pFix = pFix; } /* @@ -460,104 +609,27 @@ int sqlite3FixSrcList( DbFixer *pFix, /* Context of the fixation */ SrcList *pList /* The Source list to check and modify */ ){ - int i; - const char *zDb; - struct SrcList_item *pItem; - - if( NEVER(pList==0) ) return 0; - zDb = pFix->zDb; - for(i=0, pItem=pList->a; inSrc; i++, pItem++){ - if( pFix->bVarOnly==0 ){ - if( pItem->zDatabase && sqlite3StrICmp(pItem->zDatabase, zDb) ){ - sqlite3ErrorMsg(pFix->pParse, - "%s %T cannot reference objects in database %s", - pFix->zType, pFix->pName, pItem->zDatabase); - return 1; - } - sqlite3DbFree(pFix->pParse->db, pItem->zDatabase); - pItem->zDatabase = 0; - pItem->pSchema = pFix->pSchema; - } -#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) - if( sqlite3FixSelect(pFix, pItem->pSelect) ) return 1; - if( sqlite3FixExpr(pFix, pItem->pOn) ) return 1; -#endif + int res = 0; + if( pList ){ + Select s; + memset(&s, 0, sizeof(s)); + s.pSrc = pList; + res = sqlite3WalkSelect(&pFix->w, &s); } - return 0; + return res; } #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) int sqlite3FixSelect( DbFixer *pFix, /* Context of the fixation */ Select *pSelect /* The SELECT statement to be fixed to one database */ ){ - while( pSelect ){ - if( sqlite3FixExprList(pFix, pSelect->pEList) ){ - return 1; - } - if( sqlite3FixSrcList(pFix, pSelect->pSrc) ){ - return 1; - } - if( sqlite3FixExpr(pFix, pSelect->pWhere) ){ - return 1; - } - if( sqlite3FixExprList(pFix, pSelect->pGroupBy) ){ - return 1; - } - if( sqlite3FixExpr(pFix, pSelect->pHaving) ){ - return 1; - } - if( sqlite3FixExprList(pFix, pSelect->pOrderBy) ){ - return 1; - } - if( sqlite3FixExpr(pFix, pSelect->pLimit) ){ - return 1; - } - if( sqlite3FixExpr(pFix, pSelect->pOffset) ){ - return 1; - } - pSelect = pSelect->pPrior; - } - return 0; + return sqlite3WalkSelect(&pFix->w, pSelect); } int sqlite3FixExpr( DbFixer *pFix, /* Context of the fixation */ Expr *pExpr /* The expression to be fixed to one database */ ){ - while( pExpr ){ - if( pExpr->op==TK_VARIABLE ){ - if( pFix->pParse->db->init.busy ){ - pExpr->op = TK_NULL; - }else{ - sqlite3ErrorMsg(pFix->pParse, "%s cannot use variables", pFix->zType); - return 1; - } - } - if( ExprHasProperty(pExpr, EP_TokenOnly) ) break; - if( ExprHasProperty(pExpr, EP_xIsSelect) ){ - if( sqlite3FixSelect(pFix, pExpr->x.pSelect) ) return 1; - }else{ - if( sqlite3FixExprList(pFix, pExpr->x.pList) ) return 1; - } - if( sqlite3FixExpr(pFix, pExpr->pRight) ){ - return 1; - } - pExpr = pExpr->pLeft; - } - return 0; -} -int sqlite3FixExprList( - DbFixer *pFix, /* Context of the fixation */ - ExprList *pList /* The expression to be fixed to one database */ -){ - int i; - struct ExprList_item *pItem; - if( pList==0 ) return 0; - for(i=0, pItem=pList->a; inExpr; i++, pItem++){ - if( sqlite3FixExpr(pFix, pItem->pExpr) ){ - return 1; - } - } - return 0; + return sqlite3WalkExpr(&pFix->w, pExpr); } #endif @@ -567,17 +639,30 @@ int sqlite3FixTriggerStep( TriggerStep *pStep /* The trigger step be fixed to one database */ ){ while( pStep ){ - if( sqlite3FixSelect(pFix, pStep->pSelect) ){ - return 1; - } - if( sqlite3FixExpr(pFix, pStep->pWhere) ){ + if( sqlite3WalkSelect(&pFix->w, pStep->pSelect) + || sqlite3WalkExpr(&pFix->w, pStep->pWhere) + || sqlite3WalkExprList(&pFix->w, pStep->pExprList) + || sqlite3FixSrcList(pFix, pStep->pFrom) + ){ return 1; } - if( sqlite3FixExprList(pFix, pStep->pExprList) ){ - return 1; +#ifndef SQLITE_OMIT_UPSERT + { + Upsert *pUp; + for(pUp=pStep->pUpsert; pUp; pUp=pUp->pNextUpsert){ + if( sqlite3WalkExprList(&pFix->w, pUp->pUpsertTarget) + || sqlite3WalkExpr(&pFix->w, pUp->pUpsertTargetWhere) + || sqlite3WalkExprList(&pFix->w, pUp->pUpsertSet) + || sqlite3WalkExpr(&pFix->w, pUp->pUpsertWhere) + ){ + return 1; + } + } } +#endif pStep = pStep->pNext; } + return 0; } #endif diff --git a/src/auth.c b/src/auth.c index 9768fc2fc0..9ec2e7d046 100644 --- a/src/auth.c +++ b/src/auth.c @@ -78,7 +78,7 @@ int sqlite3_set_authorizer( sqlite3_mutex_enter(db->mutex); db->xAuth = (sqlite3_xauth)xAuth; db->pAuthArg = pArg; - sqlite3ExpirePreparedStatements(db); + if( db->xAuth ) sqlite3ExpirePreparedStatements(db, 1); sqlite3_mutex_leave(db->mutex); return SQLITE_OK; } @@ -107,21 +107,16 @@ int sqlite3AuthReadCol( const char *zCol, /* Column name */ int iDb /* Index of containing database. */ ){ - sqlite3 *db = pParse->db; /* Database handle */ - char *zDb = db->aDb[iDb].zName; /* Name of attached database */ - int rc; /* Auth callback return code */ + sqlite3 *db = pParse->db; /* Database handle */ + char *zDb = db->aDb[iDb].zDbSName; /* Schema name of attached database */ + int rc; /* Auth callback return code */ - rc = db->xAuth(db->pAuthArg, SQLITE_READ, zTab,zCol,zDb,pParse->zAuthContext -#ifdef SQLITE_USER_AUTHENTICATION - ,db->auth.zAuthUser -#endif - ); + if( db->init.busy ) return SQLITE_OK; + rc = db->xAuth(db->pAuthArg, SQLITE_READ, zTab,zCol,zDb,pParse->zAuthContext); if( rc==SQLITE_DENY ){ - if( db->nDb>2 || iDb!=0 ){ - sqlite3ErrorMsg(pParse, "access to %s.%s.%s is prohibited",zDb,zTab,zCol); - }else{ - sqlite3ErrorMsg(pParse, "access to %s.%s is prohibited", zTab, zCol); - } + char *z = sqlite3_mprintf("%s.%s", zTab, zCol); + if( db->nDb>2 || iDb!=0 ) z = sqlite3_mprintf("%s.%z", zDb, z); + sqlite3ErrorMsg(pParse, "access to %z is prohibited", z); pParse->rc = SQLITE_AUTH; }else if( rc!=SQLITE_IGNORE && rc!=SQLITE_OK ){ sqliteAuthBadReturnCode(pParse); @@ -144,14 +139,15 @@ void sqlite3AuthRead( Schema *pSchema, /* The schema of the expression */ SrcList *pTabList /* All table that pExpr might refer to */ ){ - sqlite3 *db = pParse->db; Table *pTab = 0; /* The table being read */ const char *zCol; /* Name of the column of the table */ int iSrc; /* Index in pTabList->a[] of table being read */ int iDb; /* The index of the database the expression refers to */ int iCol; /* Index of column in table */ - if( db->xAuth==0 ) return; + assert( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER ); + assert( !IN_RENAME_OBJECT ); + assert( pParse->db->xAuth!=0 ); iDb = sqlite3SchemaToIndex(pParse->db, pSchema); if( iDb<0 ){ /* An attempt to read a column out of a subquery or other @@ -159,31 +155,30 @@ void sqlite3AuthRead( return; } - assert( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER ); if( pExpr->op==TK_TRIGGER ){ pTab = pParse->pTriggerTab; }else{ assert( pTabList ); - for(iSrc=0; ALWAYS(iSrcnSrc); iSrc++){ + for(iSrc=0; iSrcnSrc; iSrc++){ if( pExpr->iTable==pTabList->a[iSrc].iCursor ){ - pTab = pTabList->a[iSrc].pTab; + pTab = pTabList->a[iSrc].pSTab; break; } } } iCol = pExpr->iColumn; - if( NEVER(pTab==0) ) return; + if( pTab==0 ) return; if( iCol>=0 ){ assert( iColnCol ); - zCol = pTab->aCol[iCol].zName; + zCol = pTab->aCol[iCol].zCnName; }else if( pTab->iPKey>=0 ){ assert( pTab->iPKeynCol ); - zCol = pTab->aCol[pTab->iPKey].zName; + zCol = pTab->aCol[pTab->iPKey].zCnName; }else{ zCol = "ROWID"; } - assert( iDb>=0 && iDbnDb ); + assert( iDb>=0 && iDbdb->nDb ); if( SQLITE_IGNORE==sqlite3AuthReadCol(pParse, pTab->zName, zCol, iDb) ){ pExpr->op = TK_NULL; } @@ -205,21 +200,26 @@ int sqlite3AuthCheck( sqlite3 *db = pParse->db; int rc; - /* Don't do any authorization checks if the database is initialising + /* Don't do any authorization checks if the database is initializing ** or if the parser is being invoked from within sqlite3_declare_vtab. */ - if( db->init.busy || IN_DECLARE_VTAB ){ + assert( !IN_RENAME_OBJECT || db->xAuth==0 ); + if( db->xAuth==0 || db->init.busy || IN_SPECIAL_PARSE ){ return SQLITE_OK; } - if( db->xAuth==0 ){ - return SQLITE_OK; - } - rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext -#ifdef SQLITE_USER_AUTHENTICATION - ,db->auth.zAuthUser -#endif - ); + /* EVIDENCE-OF: R-43249-19882 The third through sixth parameters to the + ** callback are either NULL pointers or zero-terminated strings that + ** contain additional details about the action to be authorized. + ** + ** The following testcase() macros show that any of the 3rd through 6th + ** parameters can be either NULL or a string. */ + testcase( zArg1==0 ); + testcase( zArg2==0 ); + testcase( zArg3==0 ); + testcase( pParse->zAuthContext==0 ); + + rc = db->xAuth(db->pAuthArg,code,zArg1,zArg2,zArg3,pParse->zAuthContext); if( rc==SQLITE_DENY ){ sqlite3ErrorMsg(pParse, "not authorized"); pParse->rc = SQLITE_AUTH; diff --git a/src/backup.c b/src/backup.c index 1c282242d7..cf3cb445e8 100644 --- a/src/backup.c +++ b/src/backup.c @@ -83,22 +83,15 @@ static Btree *findBtree(sqlite3 *pErrorDb, sqlite3 *pDb, const char *zDb){ int i = sqlite3FindDbName(pDb, zDb); if( i==1 ){ - Parse *pParse; + Parse sParse; int rc = 0; - pParse = sqlite3StackAllocZero(pErrorDb, sizeof(*pParse)); - if( pParse==0 ){ - sqlite3ErrorWithMsg(pErrorDb, SQLITE_NOMEM, "out of memory"); - rc = SQLITE_NOMEM; - }else{ - pParse->db = pDb; - if( sqlite3OpenTempDatabase(pParse) ){ - sqlite3ErrorWithMsg(pErrorDb, pParse->rc, "%s", pParse->zErrMsg); - rc = SQLITE_ERROR; - } - sqlite3DbFree(pErrorDb, pParse->zErrMsg); - sqlite3ParserReset(pParse); - sqlite3StackFree(pErrorDb, pParse); + sqlite3ParseObjectInit(&sParse,pDb); + if( sqlite3OpenTempDatabase(&sParse) ){ + sqlite3ErrorWithMsg(pErrorDb, sParse.rc, "%s", sParse.zErrMsg); + rc = SQLITE_ERROR; } + sqlite3DbFree(pErrorDb, sParse.zErrMsg); + sqlite3ParseObjectReset(&sParse); if( rc ){ return 0; } @@ -118,7 +111,7 @@ static Btree *findBtree(sqlite3 *pErrorDb, sqlite3 *pDb, const char *zDb){ */ static int setDestPgsz(sqlite3_backup *p){ int rc; - rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),-1,0); + rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),0,0); return rc; } @@ -129,7 +122,7 @@ static int setDestPgsz(sqlite3_backup *p){ ** message in database handle db. */ static int checkReadTransaction(sqlite3 *db, Btree *p){ - if( sqlite3BtreeIsInReadTrans(p) ){ + if( sqlite3BtreeTxnState(p)!=SQLITE_TXN_NONE ){ sqlite3ErrorWithMsg(db, SQLITE_ERROR, "destination database is in use"); return SQLITE_ERROR; } @@ -159,6 +152,29 @@ sqlite3_backup *sqlite3_backup_init( } #endif +/* BEGIN SQLCIPHER */ +#ifdef SQLITE_HAS_CODEC + { + extern int sqlcipher_find_db_index(sqlite3*, const char*); + extern void sqlcipherCodecGetKey(sqlite3*, int, void**, int*); + extern void sqlcipher_free(void*, sqlite3_uint64); + int srcNKey, destNKey; + void *zKey; + + sqlcipherCodecGetKey(pSrcDb, sqlcipher_find_db_index(pSrcDb, zSrcDb), &zKey, &srcNKey); + if(srcNKey) sqlcipher_free(zKey, srcNKey); + sqlcipherCodecGetKey(pDestDb, sqlcipher_find_db_index(pDestDb, zDestDb), &zKey, &destNKey); + if(destNKey) sqlcipher_free(zKey, destNKey); + + /* either both databases must be plaintext, or both must be encrypted */ + if((srcNKey == 0 && destNKey > 0) || (srcNKey > 0 && destNKey == 0)) { + sqlite3ErrorWithMsg(pDestDb, SQLITE_ERROR, "backup is not supported with encrypted databases"); + return NULL; + } + } +#endif +/* END SQLCIPHER */ + /* Lock the source database handle. The destination database ** handle is not locked in this routine, but it is locked in ** sqlite3_backup_step(). The user is required to ensure that no @@ -182,7 +198,7 @@ sqlite3_backup *sqlite3_backup_init( ** sqlite3_backup_finish(). */ p = (sqlite3_backup *)sqlite3MallocZero(sizeof(sqlite3_backup)); if( !p ){ - sqlite3Error(pDestDb, SQLITE_NOMEM); + sqlite3Error(pDestDb, SQLITE_NOMEM_BKPT); } } @@ -196,7 +212,6 @@ sqlite3_backup *sqlite3_backup_init( p->isAttached = 0; if( 0==p->pSrc || 0==p->pDest - || setDestPgsz(p)==SQLITE_NOMEM || checkReadTransaction(pDestDb, p->pDest)!=SQLITE_OK ){ /* One (or both) of the named databases did not exist or an OOM @@ -242,13 +257,16 @@ static int backupOnePage( int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest); const int nCopy = MIN(nSrcPgsz, nDestPgsz); const i64 iEnd = (i64)iSrcPg*(i64)nSrcPgsz; +/* BEGIN SQLCIPHER */ #ifdef SQLITE_HAS_CODEC + extern void *sqlcipherPagerGetCodec(Pager*); /* Use BtreeGetReserveNoMutex() for the source b-tree, as although it is ** guaranteed that the shared-mutex is held by this thread, handle ** p->pSrc may not actually be the owner. */ int nSrcReserve = sqlite3BtreeGetReserveNoMutex(p->pSrc); - int nDestReserve = sqlite3BtreeGetOptimalReserve(p->pDest); + int nDestReserve = sqlite3BtreeGetRequestedReserve(p->pDest); #endif +/* END SQLCIPHER */ int rc = SQLITE_OK; i64 iOff; @@ -257,19 +275,14 @@ static int backupOnePage( assert( !isFatalError(p->rc) ); assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ); assert( zSrcData ); + assert( nSrcPgsz==nDestPgsz || sqlite3PagerIsMemdb(pDestPager)==0 ); - /* Catch the case where the destination is an in-memory database and the - ** page sizes of the source and destination differ. - */ - if( nSrcPgsz!=nDestPgsz && sqlite3PagerIsMemdb(pDestPager) ){ - rc = SQLITE_READONLY; - } - +/* BEGIN SQLCIPHER */ #ifdef SQLITE_HAS_CODEC /* Backup is not possible if the page size of the destination is changing ** and a codec is in use. */ - if( nSrcPgsz!=nDestPgsz && sqlite3PagerGetCodec(pDestPager)!=0 ){ + if( nSrcPgsz!=nDestPgsz && sqlcipherPagerGetCodec(pDestPager)!=0 ){ rc = SQLITE_READONLY; } @@ -281,9 +294,10 @@ static int backupOnePage( if( nSrcReserve!=nDestReserve ){ u32 newPgsz = nSrcPgsz; rc = sqlite3PagerSetPagesize(pDestPager, &newPgsz, nSrcReserve); - if( rc==SQLITE_OK && newPgsz!=nSrcPgsz ) rc = SQLITE_READONLY; + if( rc==SQLITE_OK && newPgsz!=(u32)nSrcPgsz ) rc = SQLITE_READONLY; } #endif +/* END SQLCIPHER */ /* This loop runs once for each destination page spanned by the source ** page. For each iteration, variable iOff is set to the byte offset @@ -384,29 +398,42 @@ int sqlite3_backup_step(sqlite3_backup *p, int nPage){ rc = SQLITE_OK; } - /* Lock the destination database, if it is not locked already. */ - if( SQLITE_OK==rc && p->bDestLocked==0 - && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2)) - ){ - p->bDestLocked = 1; - sqlite3BtreeGetMeta(p->pDest, BTREE_SCHEMA_VERSION, &p->iDestSchema); - } - /* If there is no open read-transaction on the source database, open ** one now. If a transaction is opened here, then it will be closed ** before this function exits. */ - if( rc==SQLITE_OK && 0==sqlite3BtreeIsInReadTrans(p->pSrc) ){ - rc = sqlite3BtreeBeginTrans(p->pSrc, 0); + if( rc==SQLITE_OK && SQLITE_TXN_NONE==sqlite3BtreeTxnState(p->pSrc) ){ + rc = sqlite3BtreeBeginTrans(p->pSrc, 0, 0); bCloseTrans = 1; } + /* If the destination database has not yet been locked (i.e. if this + ** is the first call to backup_step() for the current backup operation), + ** try to set its page size to the same as the source database. This + ** is especially important on ZipVFS systems, as in that case it is + ** not possible to create a database file that uses one page size by + ** writing to it with another. */ + if( p->bDestLocked==0 && rc==SQLITE_OK && setDestPgsz(p)==SQLITE_NOMEM ){ + rc = SQLITE_NOMEM; + } + + /* Lock the destination database, if it is not locked already. */ + if( SQLITE_OK==rc && p->bDestLocked==0 + && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2, + (int*)&p->iDestSchema)) + ){ + p->bDestLocked = 1; + } + /* Do not allow backup if the destination database is in WAL mode ** and the page sizes are different between source and destination */ pgszSrc = sqlite3BtreeGetPageSize(p->pSrc); pgszDest = sqlite3BtreeGetPageSize(p->pDest); destMode = sqlite3PagerGetJournalMode(sqlite3BtreePager(p->pDest)); - if( SQLITE_OK==rc && destMode==PAGER_JOURNALMODE_WAL && pgszSrc!=pgszDest ){ + if( SQLITE_OK==rc + && (destMode==PAGER_JOURNALMODE_WAL || sqlite3PagerIsMemdb(pDestPager)) + && pgszSrc!=pgszDest + ){ rc = SQLITE_READONLY; } @@ -581,7 +608,7 @@ int sqlite3_backup_step(sqlite3_backup *p, int nPage){ } if( rc==SQLITE_IOERR_NOMEM ){ - rc = SQLITE_NOMEM; + rc = SQLITE_NOMEM_BKPT; } p->rc = rc; } @@ -616,8 +643,10 @@ int sqlite3_backup_finish(sqlite3_backup *p){ } if( p->isAttached ){ pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc)); + assert( pp!=0 ); while( *pp!=p ){ pp = &(*pp)->pNext; + assert( pp!=0 ); } *pp = p->pNext; } @@ -748,7 +777,7 @@ int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){ sqlite3BtreeEnter(pTo); sqlite3BtreeEnter(pFrom); - assert( sqlite3BtreeIsInTrans(pTo) ); + assert( sqlite3BtreeTxnState(pTo)==SQLITE_TXN_WRITE ); pFd = sqlite3PagerFile(sqlite3BtreePager(pTo)); if( pFd->pMethods ){ i64 nByte = sqlite3BtreeGetPageSize(pFrom)*(i64)sqlite3BtreeLastPage(pFrom); @@ -768,19 +797,21 @@ int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){ b.pDest = pTo; b.iNext = 1; +/* BEGIN SQLCIPHER */ #ifdef SQLITE_HAS_CODEC sqlite3PagerAlignReserve(sqlite3BtreePager(pTo), sqlite3BtreePager(pFrom)); #endif +/* END SQLCIPHER */ /* 0x7FFFFFFF is the hard limit for the number of pages in a database ** file. By passing this as the number of pages to copy to ** sqlite3_backup_step(), we can guarantee that the copy finishes ** within a single call (unless an error occurs). The assert() statement ** checks this assumption - (p->rc) should be set to either SQLITE_DONE - ** or an error code. - */ + ** or an error code. */ sqlite3_backup_step(&b, 0x7FFFFFFF); assert( b.rc!=SQLITE_OK ); + rc = sqlite3_backup_finish(&b); if( rc==SQLITE_OK ){ pTo->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED; @@ -788,7 +819,7 @@ int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){ sqlite3PagerClearCache(sqlite3BtreePager(b.pDest)); } - assert( sqlite3BtreeIsInTrans(pTo)==0 ); + assert( sqlite3BtreeTxnState(pTo)!=SQLITE_TXN_WRITE ); copy_finished: sqlite3BtreeLeave(pFrom); sqlite3BtreeLeave(pTo); diff --git a/src/bitvec.c b/src/bitvec.c index f7f544cff7..7c5fa71d9b 100644 --- a/src/bitvec.c +++ b/src/bitvec.c @@ -17,8 +17,8 @@ ** property. Usually only a few pages are meet either condition. ** So the bitmap is usually sparse and has low cardinality. ** But sometimes (for example when during a DROP of a large table) most -** or all of the pages in a database can get journalled. In those cases, -** the bitmap becomes dense with high cardinality. The algorithm needs +** or all of the pages in a database can get journalled. In those cases, +** the bitmap becomes dense with high cardinality. The algorithm needs ** to handle both cases well. ** ** The size of the bitmap is fixed when the object is created. @@ -39,13 +39,13 @@ /* Size of the Bitvec structure in bytes. */ #define BITVEC_SZ 512 -/* Round the union size down to the nearest pointer boundary, since that's how +/* Round the union size down to the nearest pointer boundary, since that's how ** it will be aligned within the Bitvec struct. */ #define BITVEC_USIZE \ (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*)) -/* Type of the array "element" for the bitmap representation. -** Should be a power of 2, and ideally, evenly divide into BITVEC_USIZE. +/* Type of the array "element" for the bitmap representation. +** Should be a power of 2, and ideally, evenly divide into BITVEC_USIZE. ** Setting this to the "natural word" size of your CPU may improve ** performance. */ #define BITVEC_TELEM u8 @@ -58,16 +58,16 @@ /* Number of u32 values in hash table. */ #define BITVEC_NINT (BITVEC_USIZE/sizeof(u32)) -/* Maximum number of entries in hash table before +/* Maximum number of entries in hash table before ** sub-dividing and re-hashing. */ #define BITVEC_MXHASH (BITVEC_NINT/2) /* Hashing function for the aHash representation. -** Empirical testing showed that the *37 multiplier -** (an arbitrary prime)in the hash function provided +** Empirical testing showed that the *37 multiplier +** (an arbitrary prime)in the hash function provided ** no fewer collisions than the no-op *1. */ #define BITVEC_HASH(X) (((X)*1)%BITVEC_NINT) -#define BITVEC_NPTR (BITVEC_USIZE/sizeof(Bitvec *)) +#define BITVEC_NPTR ((u32)(BITVEC_USIZE/sizeof(Bitvec *))) /* @@ -107,9 +107,10 @@ struct Bitvec { } u; }; + /* ** Create a new bitmap object able to handle bits between 0 and iSize, -** inclusive. Return a pointer to the new object. Return NULL if +** inclusive. Return a pointer to the new object. Return NULL if ** malloc fails. */ Bitvec *sqlite3BitvecCreate(u32 iSize){ @@ -177,7 +178,7 @@ int sqlite3BitvecSet(Bitvec *p, u32 i){ i = i%p->iDivisor; if( p->u.apSub[bin]==0 ){ p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor ); - if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM; + if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM_BKPT; } p = p->u.apSub[bin]; } @@ -188,7 +189,7 @@ int sqlite3BitvecSet(Bitvec *p, u32 i){ h = BITVEC_HASH(i++); /* if there wasn't a hash collision, and this doesn't */ /* completely fill the hash, then just add it without */ - /* worring about sub-dividing and re-hashing. */ + /* worrying about sub-dividing and re-hashing. */ if( !p->u.aHash[h] ){ if (p->nSet<(BITVEC_NINT-1)) { goto bitvec_set_end; @@ -212,11 +213,13 @@ int sqlite3BitvecSet(Bitvec *p, u32 i){ int rc; u32 *aiValues = sqlite3StackAllocRaw(0, sizeof(p->u.aHash)); if( aiValues==0 ){ - return SQLITE_NOMEM; + return SQLITE_NOMEM_BKPT; }else{ memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash)); memset(p->u.apSub, 0, sizeof(p->u.apSub)); - p->iDivisor = (p->iSize + BITVEC_NPTR - 1)/BITVEC_NPTR; + p->iDivisor = p->iSize/BITVEC_NPTR; + if( (p->iSize%BITVEC_NPTR)!=0 ) p->iDivisor++; + if( p->iDivisoriDivisor = BITVEC_NBIT; rc = sqlite3BitvecSet(p, i); for(j=0; jiSize<=BITVEC_NBIT ){ - p->u.aBitmap[i/BITVEC_SZELEM] &= ~(1 << (i&(BITVEC_SZELEM-1))); + p->u.aBitmap[i/BITVEC_SZELEM] &= ~(BITVEC_TELEM)(1<<(i&(BITVEC_SZELEM-1))); }else{ unsigned int j; u32 *aiValues = pBuf; @@ -293,7 +296,53 @@ u32 sqlite3BitvecSize(Bitvec *p){ return p->iSize; } -#ifndef SQLITE_OMIT_BUILTIN_TEST +#ifdef SQLITE_DEBUG +/* +** Show the content of a Bitvec option and its children. Indent +** everything by n spaces. Add x to each bitvec value. +** +** From a debugger such as gdb, one can type: +** +** call sqlite3ShowBitvec(p) +** +** For some Bitvec p and see a recursive view of the Bitvec's content. +*/ +static void showBitvec(Bitvec *p, int n, unsigned x){ + int i; + if( p==0 ){ + printf("NULL\n"); + return; + } + printf("Bitvec 0x%p iSize=%u", p, p->iSize); + if( p->iSize<=BITVEC_NBIT ){ + printf(" bitmap\n"); + printf("%*s bits:", n, ""); + for(i=1; i<=BITVEC_NBIT; i++){ + if( sqlite3BitvecTest(p,i) ) printf(" %u", x+(unsigned)i); + } + printf("\n"); + }else if( p->iDivisor==0 ){ + printf(" hash with %u entries\n", p->nSet); + printf("%*s bits:", n, ""); + for(i=0; iu.aHash[i] ) printf(" %u", x+(unsigned)p->u.aHash[i]); + } + printf("\n"); + }else{ + printf(" sub-bitvec with iDivisor=%u\n", p->iDivisor); + for(i=0; iu.apSub[i]==0 ) continue; + printf("%*s apSub[%d]=", n, "", i); + showBitvec(p->u.apSub[i], n+4, i*p->iDivisor); + } + } +} +void sqlite3ShowBitvec(Bitvec *p){ + showBitvec(p, 0, 0); +} +#endif + +#ifndef SQLITE_UNTESTABLE /* ** Let V[] be an array of unsigned characters sufficient to hold ** up to N bits. Let I be an integer between 0 and N. 0<=I>3] |= (1<<(I&7)) -#define CLEARBIT(V,I) V[I>>3] &= ~(1<<(I&7)) +#define CLEARBIT(V,I) V[I>>3] &= ~(BITVEC_TELEM)(1<<(I&7)) #define TESTBIT(V,I) (V[I>>3]&(1<<(I&7)))!=0 + /* ** This routine runs an extensive test of the Bitvec code. ** @@ -312,7 +362,7 @@ u32 sqlite3BitvecSize(Bitvec *p){ ** by 0, 1, or 3 operands, depending on the opcode. Another ** opcode follows immediately after the last operand. ** -** There are 6 opcodes numbered from 0 through 5. 0 is the +** There are opcodes numbered starting with 0. 0 is the ** "halt" opcode and causes the test to end. ** ** 0 Halt and return the number of errors @@ -321,18 +371,25 @@ u32 sqlite3BitvecSize(Bitvec *p){ ** 3 N Set N randomly chosen bits ** 4 N Clear N randomly chosen bits ** 5 N S X Set N bits from S increment X in array only, not in bitvec +** 6 Invoice sqlite3ShowBitvec() on the Bitvec object so far +** 7 X Show compile-time parameters and the hash of X ** ** The opcodes 1 through 4 perform set and clear operations are performed ** on both a Bitvec object and on a linear array of bits obtained from malloc. ** Opcode 5 works on the linear array only, not on the Bitvec. ** Opcode 5 is used to deliberately induce a fault in order to -** confirm that error detection works. +** confirm that error detection works. Opcodes 6 and greater are +** state output opcodes. Opcodes 6 and greater are no-ops unless +** SQLite has been compiled with SQLITE_DEBUG. ** ** At the conclusion of the test the linear array is compared ** against the Bitvec object. If there are any differences, ** an error is returned. If they are the same, zero is returned. ** ** If a memory allocation error occurs, return -1. +** +** sz is the size of the Bitvec. Or if sz is negative, make the size +** 2*(unsigned)(-sz) and disabled the linear vector check. */ int sqlite3BitvecBuiltinTest(int sz, int *aOp){ Bitvec *pBitvec = 0; @@ -343,18 +400,41 @@ int sqlite3BitvecBuiltinTest(int sz, int *aOp){ /* Allocate the Bitvec to be tested and a linear array of ** bits to act as the reference */ - pBitvec = sqlite3BitvecCreate( sz ); - pV = sqlite3MallocZero( (sz+7)/8 + 1 ); + if( sz<=0 ){ + pBitvec = sqlite3BitvecCreate( 2*(unsigned)(-sz) ); + pV = 0; + }else{ + pBitvec = sqlite3BitvecCreate( sz ); + pV = sqlite3MallocZero( (7+(i64)sz)/8 + 1 ); + } pTmpSpace = sqlite3_malloc64(BITVEC_SZ); - if( pBitvec==0 || pV==0 || pTmpSpace==0 ) goto bitvec_end; + if( pBitvec==0 || pTmpSpace==0 || (pV==0 && sz>0) ) goto bitvec_end; /* NULL pBitvec tests */ sqlite3BitvecSet(0, 1); sqlite3BitvecClear(0, 1, pTmpSpace); /* Run the program */ - pc = 0; + pc = i = 0; while( (op = aOp[pc])!=0 ){ + if( op>=6 ){ +#ifdef SQLITE_DEBUG + if( op==6 ){ + sqlite3ShowBitvec(pBitvec); + }else if( op==7 ){ + printf("BITVEC_SZ = %d (%d by sizeof)\n", + BITVEC_SZ, (int)sizeof(Bitvec)); + printf("BITVEC_USIZE = %d\n", (int)BITVEC_USIZE); + printf("BITVEC_NELEM = %d\n", (int)BITVEC_NELEM); + printf("BITVEC_NBIT = %d\n", (int)BITVEC_NBIT); + printf("BITVEC_NINT = %d\n", (int)BITVEC_NINT); + printf("BITVEC_MXHASH = %d\n", (int)BITVEC_MXHASH); + printf("BITVEC_NPTR = %d\n", (int)BITVEC_NPTR); + } +#endif + pc++; + continue; + } switch( op ){ case 1: case 2: @@ -365,7 +445,7 @@ int sqlite3BitvecBuiltinTest(int sz, int *aOp){ break; } case 3: - case 4: + case 4: default: { nx = 2; sqlite3_randomness(sizeof(i), &i); @@ -376,12 +456,12 @@ int sqlite3BitvecBuiltinTest(int sz, int *aOp){ pc += nx; i = (i & 0x7fffffff)%sz; if( (op & 1)!=0 ){ - SETBIT(pV, (i+1)); + if( pV ) SETBIT(pV, (i+1)); if( op!=5 ){ if( sqlite3BitvecSet(pBitvec, i+1) ) goto bitvec_end; } }else{ - CLEARBIT(pV, (i+1)); + if( pV ) CLEARBIT(pV, (i+1)); sqlite3BitvecClear(pBitvec, i+1, pTmpSpace); } } @@ -391,14 +471,18 @@ int sqlite3BitvecBuiltinTest(int sz, int *aOp){ ** match (rc==0). Change rc to non-zero if a discrepancy ** is found. */ - rc = sqlite3BitvecTest(0,0) + sqlite3BitvecTest(pBitvec, sz+1) - + sqlite3BitvecTest(pBitvec, 0) - + (sqlite3BitvecSize(pBitvec) - sz); - for(i=1; i<=sz; i++){ - if( (TESTBIT(pV,i))!=sqlite3BitvecTest(pBitvec,i) ){ - rc = i; - break; + if( pV ){ + rc = sqlite3BitvecTest(0,0) + sqlite3BitvecTest(pBitvec, sz+1) + + sqlite3BitvecTest(pBitvec, 0) + + (sqlite3BitvecSize(pBitvec) - sz); + for(i=1; i<=sz; i++){ + if( (TESTBIT(pV,i))!=sqlite3BitvecTest(pBitvec,i) ){ + rc = i; + break; + } } + }else{ + rc = 0; } /* Free allocated structure */ @@ -408,4 +492,4 @@ int sqlite3BitvecBuiltinTest(int sz, int *aOp){ sqlite3BitvecDestroy(pBitvec); return rc; } -#endif /* SQLITE_OMIT_BUILTIN_TEST */ +#endif /* SQLITE_UNTESTABLE */ diff --git a/src/btmutex.c b/src/btmutex.c index c1ebff9604..620047c151 100644 --- a/src/btmutex.c +++ b/src/btmutex.c @@ -178,21 +178,29 @@ int sqlite3BtreeHoldsMutex(Btree *p){ ** ** There is a corresponding leave-all procedures. ** -** Enter the mutexes in accending order by BtShared pointer address +** Enter the mutexes in ascending order by BtShared pointer address ** to avoid the possibility of deadlock when two threads with ** two or more btrees in common both try to lock all their btrees ** at the same instant. */ -void sqlite3BtreeEnterAll(sqlite3 *db){ +static void SQLITE_NOINLINE btreeEnterAll(sqlite3 *db){ int i; + u8 skipOk = 1; Btree *p; assert( sqlite3_mutex_held(db->mutex) ); for(i=0; inDb; i++){ p = db->aDb[i].pBt; - if( p ) sqlite3BtreeEnter(p); + if( p && p->sharable ){ + sqlite3BtreeEnter(p); + skipOk = 0; + } } + db->noSharedCache = skipOk; } -void sqlite3BtreeLeaveAll(sqlite3 *db){ +void sqlite3BtreeEnterAll(sqlite3 *db){ + if( db->noSharedCache==0 ) btreeEnterAll(db); +} +static void SQLITE_NOINLINE btreeLeaveAll(sqlite3 *db){ int i; Btree *p; assert( sqlite3_mutex_held(db->mutex) ); @@ -201,6 +209,9 @@ void sqlite3BtreeLeaveAll(sqlite3 *db){ if( p ) sqlite3BtreeLeave(p); } } +void sqlite3BtreeLeaveAll(sqlite3 *db){ + if( db->noSharedCache==0 ) btreeLeaveAll(db); +} #ifndef NDEBUG /* @@ -241,6 +252,7 @@ int sqlite3BtreeHoldsAllMutexes(sqlite3 *db){ int sqlite3SchemaMutexHeld(sqlite3 *db, int iDb, Schema *pSchema){ Btree *p; assert( db!=0 ); + if( db->pVfs==0 && db->nDb==0 ) return 1; if( pSchema ) iDb = sqlite3SchemaToIndex(db, pSchema); assert( iDb>=0 && iDbnDb ); if( !sqlite3_mutex_held(db->mutex) ) return 0; diff --git a/src/btree.c b/src/btree.c index c6f9c34f7b..a931b0d127 100644 --- a/src/btree.c +++ b/src/btree.c @@ -51,7 +51,7 @@ int sqlite3BtreeTrace=1; /* True to enable tracing */ #define BTALLOC_LE 2 /* Allocate any page <= the parameter */ /* -** Macro IfNotOmitAV(x) returns (x) if SQLITE_OMIT_AUTOVACUUM is not +** Macro IfNotOmitAV(x) returns (x) if SQLITE_OMIT_AUTOVACUUM is not ** defined, or 0 if it is. For example: ** ** bIncrVacuum = IfNotOmitAV(pBtShared->incrVacuum); @@ -66,10 +66,10 @@ int sqlite3BtreeTrace=1; /* True to enable tracing */ /* ** A list of BtShared objects that are eligible for participation ** in shared cache. This variable has file scope during normal builds, -** but the test harness needs to access it so we make it global for +** but the test harness needs to access it so we make it global for ** test builds. ** -** Access to this variable is protected by SQLITE_MUTEX_STATIC_MASTER. +** Access to this variable is protected by SQLITE_MUTEX_STATIC_MAIN. */ #ifdef SQLITE_TEST BtShared *SQLITE_WSD sqlite3SharedCacheList = 0; @@ -101,7 +101,7 @@ int sqlite3_enable_shared_cache(int enable){ ** manipulate entries in the BtShared.pLock linked list used to store ** shared-cache table level locks. If the library is compiled with the ** shared-cache feature disabled, then there is only ever one user - ** of each BtShared structure and so this locking is not necessary. + ** of each BtShared structure and so this locking is not necessary. ** So define the lock related functions as no-ops. */ #define querySharedCacheTableLock(a,b,c) SQLITE_OK @@ -112,21 +112,99 @@ int sqlite3_enable_shared_cache(int enable){ #define hasReadConflicts(a, b) 0 #endif +#ifdef SQLITE_DEBUG +/* +** Return and reset the seek counter for a Btree object. +*/ +sqlite3_uint64 sqlite3BtreeSeekCount(Btree *pBt){ + u64 n = pBt->nSeek; + pBt->nSeek = 0; + return n; +} +#endif + +/* +** Implementation of the SQLITE_CORRUPT_PAGE() macro. Takes a single +** (MemPage*) as an argument. The (MemPage*) must not be NULL. +** +** If SQLITE_DEBUG is not defined, then this macro is equivalent to +** SQLITE_CORRUPT_BKPT. Or, if SQLITE_DEBUG is set, then the log message +** normally produced as a side-effect of SQLITE_CORRUPT_BKPT is augmented +** with the page number and filename associated with the (MemPage*). +*/ +#ifdef SQLITE_DEBUG +int corruptPageError(int lineno, MemPage *p){ + char *zMsg; + sqlite3BeginBenignMalloc(); + zMsg = sqlite3_mprintf("database corruption page %u of %s", + p->pgno, sqlite3PagerFilename(p->pBt->pPager, 0) + ); + sqlite3EndBenignMalloc(); + if( zMsg ){ + sqlite3ReportError(SQLITE_CORRUPT, lineno, zMsg); + } + sqlite3_free(zMsg); + return SQLITE_CORRUPT_BKPT; +} +# define SQLITE_CORRUPT_PAGE(pMemPage) corruptPageError(__LINE__, pMemPage) +#else +# define SQLITE_CORRUPT_PAGE(pMemPage) SQLITE_CORRUPT_PGNO(pMemPage->pgno) +#endif + +/* Default value for SHARED_LOCK_TRACE macro if shared-cache is disabled +** or if the lock tracking is disabled. This is always the value for +** release builds. +*/ +#define SHARED_LOCK_TRACE(X,MSG,TAB,TYPE) /*no-op*/ + #ifndef SQLITE_OMIT_SHARED_CACHE +#if 0 +/* ^---- Change to 1 and recompile to enable shared-lock tracing +** for debugging purposes. +** +** Print all shared-cache locks on a BtShared. Debugging use only. +*/ +static void sharedLockTrace( + BtShared *pBt, + const char *zMsg, + int iRoot, + int eLockType +){ + BtLock *pLock; + if( iRoot>0 ){ + printf("%s-%p %u%s:", zMsg, pBt, iRoot, eLockType==READ_LOCK?"R":"W"); + }else{ + printf("%s-%p:", zMsg, pBt); + } + for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){ + printf(" %p/%u%s", pLock->pBtree, pLock->iTable, + pLock->eLock==READ_LOCK ? "R" : "W"); + while( pLock->pNext && pLock->pBtree==pLock->pNext->pBtree ){ + pLock = pLock->pNext; + printf(",%u%s", pLock->iTable, pLock->eLock==READ_LOCK ? "R" : "W"); + } + } + printf("\n"); + fflush(stdout); +} +#undef SHARED_LOCK_TRACE +#define SHARED_LOCK_TRACE(X,MSG,TAB,TYPE) sharedLockTrace(X,MSG,TAB,TYPE) +#endif /* Shared-lock tracing */ + #ifdef SQLITE_DEBUG /* **** This function is only used as part of an assert() statement. *** ** -** Check to see if pBtree holds the required locks to read or write to the +** Check to see if pBtree holds the required locks to read or write to the ** table with root page iRoot. Return 1 if it does and 0 if not. ** -** For example, when writing to a table with root-page iRoot via +** For example, when writing to a table with root-page iRoot via ** Btree connection pBtree: ** ** assert( hasSharedCacheTableLock(pBtree, iRoot, 0, WRITE_LOCK) ); ** -** When writing to an index that resides in a sharable database, the +** When writing to an index that resides in a sharable database, the ** caller should have first obtained a lock specifying the root page of ** the corresponding table. This makes things a bit more complicated, ** as this module treats each table as a separate structure. To determine @@ -148,11 +226,11 @@ static int hasSharedCacheTableLock( BtLock *pLock; /* If this database is not shareable, or if the client is reading - ** and has the read-uncommitted flag set, then no lock is required. + ** and has the read-uncommitted flag set, then no lock is required. ** Return true immediately. */ if( (pBtree->sharable==0) - || (eLockType==READ_LOCK && (pBtree->db->flags & SQLITE_ReadUncommitted)) + || (eLockType==READ_LOCK && (pBtree->db->flags & SQLITE_ReadUncommit)) ){ return 1; } @@ -172,29 +250,33 @@ static int hasSharedCacheTableLock( ** table. */ if( isIndex ){ HashElem *p; + int bSeen = 0; for(p=sqliteHashFirst(&pSchema->idxHash); p; p=sqliteHashNext(p)){ Index *pIdx = (Index *)sqliteHashData(p); - if( pIdx->tnum==(int)iRoot ){ - if( iTab ){ + if( pIdx->tnum==iRoot ){ + if( bSeen ){ /* Two or more indexes share the same root page. There must ** be imposter tables. So just return true. The assert is not ** useful in that case. */ return 1; } iTab = pIdx->pTable->tnum; + bSeen = 1; } } }else{ iTab = iRoot; } - /* Search for the required lock. Either a write-lock on root-page iTab, a + SHARED_LOCK_TRACE(pBtree->pBt,"hasLock",iRoot,eLockType); + + /* Search for the required lock. Either a write-lock on root-page iTab, a ** write-lock on the schema table, or (if the client is reading) a ** read-lock on iTab will suffice. Return 1 if any of these are found. */ for(pLock=pBtree->pBt->pLock; pLock; pLock=pLock->pNext){ - if( pLock->pBtree==pBtree + if( pLock->pBtree==pBtree && (pLock->iTable==iTab || (pLock->eLock==WRITE_LOCK && pLock->iTable==1)) - && pLock->eLock>=eLockType + && pLock->eLock>=eLockType ){ return 1; } @@ -227,9 +309,9 @@ static int hasSharedCacheTableLock( static int hasReadConflicts(Btree *pBtree, Pgno iRoot){ BtCursor *p; for(p=pBtree->pBt->pCursor; p; p=p->pNext){ - if( p->pgnoRoot==iRoot + if( p->pgnoRoot==iRoot && p->pBtree!=pBtree - && 0==(p->pBtree->db->flags & SQLITE_ReadUncommitted) + && 0==(p->pBtree->db->flags & SQLITE_ReadUncommit) ){ return 1; } @@ -239,7 +321,7 @@ static int hasReadConflicts(Btree *pBtree, Pgno iRoot){ #endif /* #ifdef SQLITE_DEBUG */ /* -** Query to see if Btree handle p may obtain a lock of type eLock +** Query to see if Btree handle p may obtain a lock of type eLock ** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return ** SQLITE_OK if the lock may be obtained (by calling ** setSharedCacheTableLock()), or SQLITE_LOCKED if not. @@ -251,15 +333,15 @@ static int querySharedCacheTableLock(Btree *p, Pgno iTab, u8 eLock){ assert( sqlite3BtreeHoldsMutex(p) ); assert( eLock==READ_LOCK || eLock==WRITE_LOCK ); assert( p->db!=0 ); - assert( !(p->db->flags&SQLITE_ReadUncommitted)||eLock==WRITE_LOCK||iTab==1 ); - + assert( !(p->db->flags&SQLITE_ReadUncommit)||eLock==WRITE_LOCK||iTab==1 ); + /* If requesting a write-lock, then the Btree must have an open write - ** transaction on this file. And, obviously, for this to be so there + ** transaction on this file. And, obviously, for this to be so there ** must be an open write transaction on the file itself. */ assert( eLock==READ_LOCK || (p==pBt->pWriter && p->inTrans==TRANS_WRITE) ); assert( eLock==READ_LOCK || pBt->inTransaction==TRANS_WRITE ); - + /* This routine is a no-op if the shared-cache is not enabled */ if( !p->sharable ){ return SQLITE_OK; @@ -274,7 +356,7 @@ static int querySharedCacheTableLock(Btree *p, Pgno iTab, u8 eLock){ } for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){ - /* The condition (pIter->eLock!=eLock) in the following if(...) + /* The condition (pIter->eLock!=eLock) in the following if(...) ** statement is a simplification of: ** ** (eLock==WRITE_LOCK || pIter->eLock==WRITE_LOCK) @@ -301,7 +383,7 @@ static int querySharedCacheTableLock(Btree *p, Pgno iTab, u8 eLock){ #ifndef SQLITE_OMIT_SHARED_CACHE /* ** Add a lock on the table with root-page iTable to the shared-btree used -** by Btree handle p. Parameter eLock must be either READ_LOCK or +** by Btree handle p. Parameter eLock must be either READ_LOCK or ** WRITE_LOCK. ** ** This function assumes the following: @@ -313,7 +395,7 @@ static int querySharedCacheTableLock(Btree *p, Pgno iTab, u8 eLock){ ** with the requested lock (i.e. querySharedCacheTableLock() has ** already been called and returned SQLITE_OK). ** -** SQLITE_OK is returned if the lock is added successfully. SQLITE_NOMEM +** SQLITE_OK is returned if the lock is added successfully. SQLITE_NOMEM ** is returned if a malloc attempt fails. */ static int setSharedCacheTableLock(Btree *p, Pgno iTable, u8 eLock){ @@ -321,17 +403,19 @@ static int setSharedCacheTableLock(Btree *p, Pgno iTable, u8 eLock){ BtLock *pLock = 0; BtLock *pIter; + SHARED_LOCK_TRACE(pBt,"setLock", iTable, eLock); + assert( sqlite3BtreeHoldsMutex(p) ); assert( eLock==READ_LOCK || eLock==WRITE_LOCK ); assert( p->db!=0 ); /* A connection with the read-uncommitted flag set will never try to ** obtain a read-lock using this function. The only read-lock obtained - ** by a connection in read-uncommitted mode is on the sqlite_master + ** by a connection in read-uncommitted mode is on the sqlite_schema ** table, and that lock is obtained in BtreeBeginTrans(). */ - assert( 0==(p->db->flags&SQLITE_ReadUncommitted) || eLock==WRITE_LOCK ); + assert( 0==(p->db->flags&SQLITE_ReadUncommit) || eLock==WRITE_LOCK ); - /* This function should only be called on a sharable b-tree after it + /* This function should only be called on a sharable b-tree after it ** has been determined that no other b-tree holds a conflicting lock. */ assert( p->sharable ); assert( SQLITE_OK==querySharedCacheTableLock(p, iTable, eLock) ); @@ -350,7 +434,7 @@ static int setSharedCacheTableLock(Btree *p, Pgno iTable, u8 eLock){ if( !pLock ){ pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock)); if( !pLock ){ - return SQLITE_NOMEM; + return SQLITE_NOMEM_BKPT; } pLock->iTable = iTable; pLock->pBtree = p; @@ -376,7 +460,7 @@ static int setSharedCacheTableLock(Btree *p, Pgno iTable, u8 eLock){ ** Release all the table locks (locks obtained via calls to ** the setSharedCacheTableLock() procedure) held by Btree object p. ** -** This function assumes that Btree p has an open read or write +** This function assumes that Btree p has an open read or write ** transaction. If it does not, then the BTS_PENDING flag ** may be incorrectly cleared. */ @@ -388,6 +472,8 @@ static void clearAllSharedCacheTableLocks(Btree *p){ assert( p->sharable || 0==*ppIter ); assert( p->inTrans>0 ); + SHARED_LOCK_TRACE(pBt, "clearAllLocks", 0, 0); + while( *ppIter ){ BtLock *pLock = *ppIter; assert( (pBt->btsFlags & BTS_EXCLUSIVE)==0 || pBt->pWriter==pLock->pBtree ); @@ -408,7 +494,7 @@ static void clearAllSharedCacheTableLocks(Btree *p){ pBt->pWriter = 0; pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING); }else if( pBt->nTransaction==2 ){ - /* This function is called when Btree p is concluding its + /* This function is called when Btree p is concluding its ** transaction. If there currently exists a writer, and p is not ** that writer, then the number of locks held by connections other ** than the writer must be about to drop to zero. In this case @@ -426,6 +512,9 @@ static void clearAllSharedCacheTableLocks(Btree *p){ */ static void downgradeAllSharedCacheTableLocks(Btree *p){ BtShared *pBt = p->pBt; + + SHARED_LOCK_TRACE(pBt, "downgradeLocks", 0, 0); + if( pBt->pWriter==p ){ BtLock *pLock; pBt->pWriter = 0; @@ -439,7 +528,9 @@ static void downgradeAllSharedCacheTableLocks(Btree *p){ #endif /* SQLITE_OMIT_SHARED_CACHE */ -static void releasePage(MemPage *pPage); /* Forward reference */ +static void releasePage(MemPage *pPage); /* Forward reference */ +static void releasePageOne(MemPage *pPage); /* Forward reference */ +static void releasePageNotNull(MemPage *pPage); /* Forward reference */ /* ***** This routine is used inside of assert() only **** @@ -450,6 +541,15 @@ static void releasePage(MemPage *pPage); /* Forward reference */ static int cursorHoldsMutex(BtCursor *p){ return sqlite3_mutex_held(p->pBt->mutex); } + +/* Verify that the cursor and the BtShared agree about what is the current +** database connetion. This is important in shared-cache mode. If the database +** connection pointers get out-of-sync, it is possible for routines like +** btreeInitPage() to reference an stale connection pointer that references a +** a connection that has already closed. This routine is used inside assert() +** statements only and for the purpose of double-checking that the btree code +** does keep the database connection pointers up-to-date. +*/ static int cursorOwnsBtShared(BtCursor *p){ assert( cursorHoldsMutex(p) ); return (p->pBtree->db==p->pBt->db); @@ -490,17 +590,18 @@ static void invalidateAllOverflowCache(BtShared *pBt){ */ static void invalidateIncrblobCursors( Btree *pBtree, /* The database file to check */ + Pgno pgnoRoot, /* The table that might be changing */ i64 iRow, /* The rowid that might be changing */ int isClearTable /* True if all rows are being deleted */ ){ BtCursor *p; - if( pBtree->hasIncrblobCur==0 ) return; + assert( pBtree->hasIncrblobCur ); assert( sqlite3BtreeHoldsMutex(pBtree) ); pBtree->hasIncrblobCur = 0; for(p=pBtree->pBt->pCursor; p; p=p->pNext){ if( (p->curFlags & BTCF_Incrblob)!=0 ){ pBtree->hasIncrblobCur = 1; - if( isClearTable || p->info.nKey==iRow ){ + if( p->pgnoRoot==pgnoRoot && (isClearTable || p->info.nKey==iRow) ){ p->eState = CURSOR_INVALID; } } @@ -509,12 +610,12 @@ static void invalidateIncrblobCursors( #else /* Stub function when INCRBLOB is omitted */ - #define invalidateIncrblobCursors(x,y,z) + #define invalidateIncrblobCursors(w,x,y,z) #endif /* SQLITE_OMIT_INCRBLOB */ /* -** Set bit pgno of the BtShared.pHasContent bitvec. This is called -** when a page that previously contained data becomes a free-list leaf +** Set bit pgno of the BtShared.pHasContent bitvec. This is called +** when a page that previously contained data becomes a free-list leaf ** page. ** ** The BtShared.pHasContent bitvec exists to work around an obscure @@ -540,7 +641,7 @@ static void invalidateIncrblobCursors( ** may be lost. In the event of a rollback, it may not be possible ** to restore the database to its original configuration. ** -** The solution is the BtShared.pHasContent bitvec. Whenever a page is +** The solution is the BtShared.pHasContent bitvec. Whenever a page is ** moved to become a free-list leaf page, the corresponding bit is ** set in the bitvec. Whenever a leaf page is extracted from the free-list, ** optimization 2 above is omitted if the corresponding bit is already @@ -553,7 +654,7 @@ static int btreeSetHasContent(BtShared *pBt, Pgno pgno){ assert( pgno<=pBt->nPage ); pBt->pHasContent = sqlite3BitvecCreate(pBt->nPage); if( !pBt->pHasContent ){ - rc = SQLITE_NOMEM; + rc = SQLITE_NOMEM_BKPT; } } if( rc==SQLITE_OK && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){ @@ -571,7 +672,7 @@ static int btreeSetHasContent(BtShared *pBt, Pgno pgno){ */ static int btreeGetHasContent(BtShared *pBt, Pgno pgno){ Bitvec *p = pBt->pHasContent; - return (p && (pgno>sqlite3BitvecSize(p) || sqlite3BitvecTest(p, pgno))); + return p && (pgno>sqlite3BitvecSize(p) || sqlite3BitvecTestNotNull(p, pgno)); } /* @@ -588,51 +689,57 @@ static void btreeClearHasContent(BtShared *pBt){ */ static void btreeReleaseAllCursorPages(BtCursor *pCur){ int i; - for(i=0; i<=pCur->iPage; i++){ - releasePage(pCur->apPage[i]); - pCur->apPage[i] = 0; + if( pCur->iPage>=0 ){ + for(i=0; iiPage; i++){ + releasePageNotNull(pCur->apPage[i]); + } + releasePageNotNull(pCur->pPage); + pCur->iPage = -1; } - pCur->iPage = -1; } /* ** The cursor passed as the only argument must point to a valid entry ** when this function is called (i.e. have eState==CURSOR_VALID). This ** function saves the current cursor key in variables pCur->nKey and -** pCur->pKey. SQLITE_OK is returned if successful or an SQLite error +** pCur->pKey. SQLITE_OK is returned if successful or an SQLite error ** code otherwise. ** ** If the cursor is open on an intkey table, then the integer key ** (the rowid) is stored in pCur->nKey and pCur->pKey is left set to -** NULL. If the cursor is open on a non-intkey table, then pCur->pKey is -** set to point to a malloced buffer pCur->nKey bytes in size containing +** NULL. If the cursor is open on a non-intkey table, then pCur->pKey is +** set to point to a malloced buffer pCur->nKey bytes in size containing ** the key. */ static int saveCursorKey(BtCursor *pCur){ - int rc; + int rc = SQLITE_OK; assert( CURSOR_VALID==pCur->eState ); assert( 0==pCur->pKey ); assert( cursorHoldsMutex(pCur) ); - rc = sqlite3BtreeKeySize(pCur, &pCur->nKey); - assert( rc==SQLITE_OK ); /* KeySize() cannot fail */ - - /* If this is an intKey table, then the above call to BtreeKeySize() - ** stores the integer key in pCur->nKey. In this case this value is - ** all that is required. Otherwise, if pCur is not open on an intKey - ** table, then malloc space for and store the pCur->nKey bytes of key - ** data. */ - if( 0==pCur->curIntKey ){ - void *pKey = sqlite3Malloc( pCur->nKey ); + if( pCur->curIntKey ){ + /* Only the rowid is required for a table btree */ + pCur->nKey = sqlite3BtreeIntegerKey(pCur); + }else{ + /* For an index btree, save the complete key content. It is possible + ** that the current key is corrupt. In that case, it is possible that + ** the sqlite3VdbeRecordUnpack() function may overread the buffer by + ** up to the size of 1 varint plus 1 8-byte value when the cursor + ** position is restored. Hence the 17 bytes of padding allocated + ** below. */ + void *pKey; + pCur->nKey = sqlite3BtreePayloadSize(pCur); + pKey = sqlite3Malloc( ((i64)pCur->nKey) + 9 + 8 ); if( pKey ){ - rc = sqlite3BtreeKey(pCur, 0, (int)pCur->nKey, pKey); + rc = sqlite3BtreePayload(pCur, 0, (int)pCur->nKey, pKey); if( rc==SQLITE_OK ){ + memset(((u8*)pKey)+pCur->nKey, 0, 9+8); pCur->pKey = pKey; }else{ sqlite3_free(pKey); } }else{ - rc = SQLITE_NOMEM; + rc = SQLITE_NOMEM_BKPT; } } assert( !pCur->curIntKey || !pCur->pKey ); @@ -640,11 +747,11 @@ static int saveCursorKey(BtCursor *pCur){ } /* -** Save the current cursor position in the variables BtCursor.nKey +** Save the current cursor position in the variables BtCursor.nKey ** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK. ** ** The caller must ensure that the cursor is valid (has eState==CURSOR_VALID) -** prior to calling this routine. +** prior to calling this routine. */ static int saveCursorPosition(BtCursor *pCur){ int rc; @@ -653,6 +760,9 @@ static int saveCursorPosition(BtCursor *pCur){ assert( 0==pCur->pKey ); assert( cursorHoldsMutex(pCur) ); + if( pCur->curFlags & BTCF_Pinned ){ + return SQLITE_CONSTRAINT_PINNED; + } if( pCur->eState==CURSOR_SKIPNEXT ){ pCur->eState = CURSOR_VALID; }else{ @@ -680,7 +790,7 @@ static int SQLITE_NOINLINE saveCursorsOnList(BtCursor*,Pgno,BtCursor*); ** routine is called just before cursor pExcept is used to modify the ** table, for example in BtreeDelete() or BtreeInsert(). ** -** If there are two or more cursors on the same btree, then all such +** If there are two or more cursors on the same btree, then all such ** cursors should have their BTCF_Multiple flag set. The btreeCursor() ** routine enforces that rule. This routine only needs to be called in ** the uncommon case when pExpect has the BTCF_Multiple flag set. @@ -723,7 +833,7 @@ static int SQLITE_NOINLINE saveCursorsOnList( return rc; } }else{ - testcase( p->iPage>0 ); + testcase( p->iPage>=0 ); btreeReleaseAllCursorPages(p); } } @@ -745,7 +855,7 @@ void sqlite3BtreeClearCursor(BtCursor *pCur){ /* ** In this version of BtreeMoveto, pKey is a packed index record ** such as is generated by the OP_MakeRecord opcode. Unpack the -** record and then call BtreeMovetoUnpacked() to do the work. +** record and then call sqlite3BtreeIndexMoveto() to do the work. */ static int btreeMoveto( BtCursor *pCur, /* Cursor open on the btree to be searched */ @@ -756,52 +866,52 @@ static int btreeMoveto( ){ int rc; /* Status code */ UnpackedRecord *pIdxKey; /* Unpacked index key */ - char aSpace[200]; /* Temp space for pIdxKey - to avoid a malloc */ - char *pFree = 0; if( pKey ){ + KeyInfo *pKeyInfo = pCur->pKeyInfo; assert( nKey==(i64)(int)nKey ); - pIdxKey = sqlite3VdbeAllocUnpackedRecord( - pCur->pKeyInfo, aSpace, sizeof(aSpace), &pFree - ); - if( pIdxKey==0 ) return SQLITE_NOMEM; - sqlite3VdbeRecordUnpack(pCur->pKeyInfo, (int)nKey, pKey, pIdxKey); - if( pIdxKey->nField==0 ){ - sqlite3DbFree(pCur->pKeyInfo->db, pFree); - return SQLITE_CORRUPT_BKPT; + pIdxKey = sqlite3VdbeAllocUnpackedRecord(pKeyInfo); + if( pIdxKey==0 ) return SQLITE_NOMEM_BKPT; + sqlite3VdbeRecordUnpack((int)nKey, pKey, pIdxKey); + if( pIdxKey->nField==0 || pIdxKey->nField>pKeyInfo->nAllField ){ + rc = SQLITE_CORRUPT_BKPT; + }else{ + rc = sqlite3BtreeIndexMoveto(pCur, pIdxKey, pRes); } + sqlite3DbFree(pCur->pKeyInfo->db, pIdxKey); }else{ pIdxKey = 0; - } - rc = sqlite3BtreeMovetoUnpacked(pCur, pIdxKey, nKey, bias, pRes); - if( pFree ){ - sqlite3DbFree(pCur->pKeyInfo->db, pFree); + rc = sqlite3BtreeTableMoveto(pCur, nKey, bias, pRes); } return rc; } /* ** Restore the cursor to the position it was in (or as close to as possible) -** when saveCursorPosition() was called. Note that this call deletes the +** when saveCursorPosition() was called. Note that this call deletes the ** saved position info stored by saveCursorPosition(), so there can be -** at most one effective restoreCursorPosition() call after each +** at most one effective restoreCursorPosition() call after each ** saveCursorPosition(). */ static int btreeRestoreCursorPosition(BtCursor *pCur){ int rc; - int skipNext; + int skipNext = 0; assert( cursorOwnsBtShared(pCur) ); assert( pCur->eState>=CURSOR_REQUIRESEEK ); if( pCur->eState==CURSOR_FAULT ){ return pCur->skipNext; } pCur->eState = CURSOR_INVALID; - rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &skipNext); + if( sqlite3FaultSim(410) ){ + rc = SQLITE_IOERR; + }else{ + rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &skipNext); + } if( rc==SQLITE_OK ){ sqlite3_free(pCur->pKey); pCur->pKey = 0; assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID ); - pCur->skipNext |= skipNext; + if( skipNext ) pCur->skipNext = skipNext; if( pCur->skipNext && pCur->eState==CURSOR_VALID ){ pCur->eState = CURSOR_SKIPNEXT; } @@ -827,13 +937,28 @@ static int btreeRestoreCursorPosition(BtCursor *pCur){ ** back to where it ought to be if this routine returns true. */ int sqlite3BtreeCursorHasMoved(BtCursor *pCur){ - return pCur->eState!=CURSOR_VALID; + assert( EIGHT_BYTE_ALIGNMENT(pCur) + || pCur==sqlite3BtreeFakeValidCursor() ); + assert( offsetof(BtCursor, eState)==0 ); + assert( sizeof(pCur->eState)==1 ); + return CURSOR_VALID != *(u8*)pCur; +} + +/* +** Return a pointer to a fake BtCursor object that will always answer +** false to the sqlite3BtreeCursorHasMoved() routine above. The fake +** cursor returned must not be used with any other Btree interface. +*/ +BtCursor *sqlite3BtreeFakeValidCursor(void){ + static u8 fakeCursor = CURSOR_VALID; + assert( offsetof(BtCursor, eState)==0 ); + return (BtCursor*)&fakeCursor; } /* ** This routine restores a cursor back to its original position after it ** has been moved by some outside activity (such as a btree rebalance or -** a row having been deleted out from under the cursor). +** a row having been deleted out from under the cursor). ** ** On success, the *pDifferentRow parameter is false if the cursor is left ** pointing at exactly the same row. *pDifferntRow is the row the cursor @@ -856,7 +981,6 @@ int sqlite3BtreeCursorRestore(BtCursor *pCur, int *pDifferentRow){ if( pCur->eState!=CURSOR_VALID ){ *pDifferentRow = 1; }else{ - assert( pCur->skipNext==0 ); *pDifferentRow = 0; } return SQLITE_OK; @@ -870,15 +994,32 @@ int sqlite3BtreeCursorRestore(BtCursor *pCur, int *pDifferentRow){ */ void sqlite3BtreeCursorHint(BtCursor *pCur, int eHintType, ...){ /* Used only by system that substitute their own storage engine */ +#ifdef SQLITE_DEBUG + if( ALWAYS(eHintType==BTREE_HINT_RANGE) ){ + va_list ap; + Expr *pExpr; + Walker w; + memset(&w, 0, sizeof(w)); + w.xExprCallback = sqlite3CursorRangeHintExprCheck; + va_start(ap, eHintType); + pExpr = va_arg(ap, Expr*); + w.u.aMem = va_arg(ap, Mem*); + va_end(ap); + assert( pExpr!=0 ); + assert( w.u.aMem!=0 ); + sqlite3WalkExpr(&w, pExpr); + } +#endif /* SQLITE_DEBUG */ } -#endif +#endif /* SQLITE_ENABLE_CURSOR_HINTS */ + /* ** Provide flag hints to the cursor. */ void sqlite3BtreeCursorHintFlags(BtCursor *pCur, unsigned x){ assert( x==BTREE_SEEK_EQ || x==BTREE_BULKLOAD || x==0 ); - pCur->hints = x; + pCur->hints = (u8)x; } @@ -899,7 +1040,7 @@ static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){ if( pgno<2 ) return 0; nPagesPerMapPage = (pBt->usableSize/5)+1; iPtrMap = (pgno-2)/nPagesPerMapPage; - ret = (iPtrMap*nPagesPerMapPage) + 2; + ret = (iPtrMap*nPagesPerMapPage) + 2; if( ret==PENDING_BYTE_PAGE(pBt) ){ ret++; } @@ -926,7 +1067,7 @@ static void ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent, int *pRC){ if( *pRC ) return; assert( sqlite3_mutex_held(pBt->mutex) ); - /* The master-journal page number must never be used as a pointer map page */ + /* The super-journal page number must never be used as a pointer map page */ assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) ); assert( pBt->autoVacuum ); @@ -940,6 +1081,13 @@ static void ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent, int *pRC){ *pRC = rc; return; } + if( ((char*)sqlite3PagerGetExtra(pDbPage))[0]!=0 ){ + /* The first byte of the extra data is the MemPage.isInit byte. + ** If that byte is set, it means this page is also being used + ** as a btree page. */ + *pRC = SQLITE_CORRUPT_BKPT; + goto ptrmap_exit; + } offset = PTRMAP_PTROFFSET(iPtrmap, key); if( offset<0 ){ *pRC = SQLITE_CORRUPT_BKPT; @@ -949,7 +1097,7 @@ static void ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent, int *pRC){ pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage); if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){ - TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent)); + TRACE(("PTRMAP_UPDATE: %u->(%u,%u)\n", key, eType, parent)); *pRC= rc = sqlite3PagerWrite(pDbPage); if( rc==SQLITE_OK ){ pPtrmap[offset] = eType; @@ -995,14 +1143,14 @@ static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){ if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]); sqlite3PagerUnref(pDbPage); - if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_BKPT; + if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_PGNO(iPtrmap); return SQLITE_OK; } #else /* if defined SQLITE_OMIT_AUTOVACUUM */ #define ptrmapPut(w,x,y,z,rc) #define ptrmapGet(w,x,y,z) SQLITE_OK - #define ptrmapPutOvflPtr(x, y, rc) + #define ptrmapPutOvflPtr(x, y, z, rc) #endif /* @@ -1058,6 +1206,25 @@ static SQLITE_NOINLINE void btreeParseCellAdjustSizeForOverflow( pInfo->nSize = (u16)(&pInfo->pPayload[pInfo->nLocal] - pCell) + 4; } +/* +** Given a record with nPayload bytes of payload stored within btree +** page pPage, return the number of bytes of payload stored locally. +*/ +static int btreePayloadToLocal(MemPage *pPage, i64 nPayload){ + int maxLocal; /* Maximum amount of payload held locally */ + maxLocal = pPage->maxLocal; + assert( nPayload>=0 ); + if( nPayload<=maxLocal ){ + return (int)nPayload; + }else{ + int minLocal; /* Minimum amount of payload held locally */ + int surplus; /* Overflow payload available for local storage */ + minLocal = pPage->minLocal; + surplus = (int)(minLocal +(nPayload - minLocal)%(pPage->pBt->usableSize-4)); + return (surplus <= maxLocal) ? surplus : minLocal; + } +} + /* ** The following routines are implementations of the MemPage.xParseCell() ** method. @@ -1124,19 +1291,37 @@ static void btreeParseCellPtr( ** ** pIter += getVarint(pIter, (u64*)&pInfo->nKey); ** - ** The code is inlined to avoid a function call. + ** The code is inlined and the loop is unrolled for performance. + ** This routine is a high-runner. */ iKey = *pIter; if( iKey>=0x80 ){ - u8 *pEnd = &pIter[7]; - iKey &= 0x7f; - while(1){ - iKey = (iKey<<7) | (*++pIter & 0x7f); - if( (*pIter)<0x80 ) break; - if( pIter>=pEnd ){ - iKey = (iKey<<8) | *++pIter; - break; + u8 x; + iKey = (iKey<<7) ^ (x = *++pIter); + if( x>=0x80 ){ + iKey = (iKey<<7) ^ (x = *++pIter); + if( x>=0x80 ){ + iKey = (iKey<<7) ^ 0x10204000 ^ (x = *++pIter); + if( x>=0x80 ){ + iKey = (iKey<<7) ^ 0x4000 ^ (x = *++pIter); + if( x>=0x80 ){ + iKey = (iKey<<7) ^ 0x4000 ^ (x = *++pIter); + if( x>=0x80 ){ + iKey = (iKey<<7) ^ 0x4000 ^ (x = *++pIter); + if( x>=0x80 ){ + iKey = (iKey<<7) ^ 0x4000 ^ (x = *++pIter); + if( x>=0x80 ){ + iKey = (iKey<<8) ^ 0x8000 ^ (*++pIter); + } + } + } + } + } + }else{ + iKey ^= 0x204000; } + }else{ + iKey ^= 0x4000; } } pIter++; @@ -1145,12 +1330,14 @@ static void btreeParseCellPtr( pInfo->nPayload = nPayload; pInfo->pPayload = pIter; testcase( nPayload==pPage->maxLocal ); - testcase( nPayload==pPage->maxLocal+1 ); + testcase( nPayload==(u32)pPage->maxLocal+1 ); + assert( nPayload>=0 ); + assert( pPage->maxLocal <= BT_MAX_LOCAL ); if( nPayload<=pPage->maxLocal ){ /* This is the (easy) common case where the entire payload fits ** on the local page. No overflow is required. */ - pInfo->nSize = nPayload + (u16)(pIter - pCell); + pInfo->nSize = (u16)nPayload + (u16)(pIter - pCell); if( pInfo->nSize<4 ) pInfo->nSize = 4; pInfo->nLocal = (u16)nPayload; }else{ @@ -1182,12 +1369,14 @@ static void btreeParseCellPtrIndex( pInfo->nPayload = nPayload; pInfo->pPayload = pIter; testcase( nPayload==pPage->maxLocal ); - testcase( nPayload==pPage->maxLocal+1 ); + testcase( nPayload==(u32)pPage->maxLocal+1 ); + assert( nPayload>=0 ); + assert( pPage->maxLocal <= BT_MAX_LOCAL ); if( nPayload<=pPage->maxLocal ){ /* This is the (easy) common case where the entire payload fits ** on the local page. No overflow is required. */ - pInfo->nSize = nPayload + (u16)(pIter - pCell); + pInfo->nSize = (u16)nPayload + (u16)(pIter - pCell); if( pInfo->nSize<4 ) pInfo->nSize = 4; pInfo->nLocal = (u16)nPayload; }else{ @@ -1212,10 +1401,12 @@ static void btreeParseCell( ** the space used by the cell pointer. ** ** cellSizePtrNoPayload() => table internal nodes -** cellSizePtr() => all index nodes & table leaf nodes +** cellSizePtrTableLeaf() => table leaf nodes +** cellSizePtr() => index internal nodes +** cellSizeIdxLeaf() => index leaf nodes */ static u16 cellSizePtr(MemPage *pPage, u8 *pCell){ - u8 *pIter = pCell + pPage->childPtrSize; /* For looping over bytes of pCell */ + u8 *pIter = pCell + 4; /* For looping over bytes of pCell */ u8 *pEnd; /* End mark for a varint */ u32 nSize; /* Size value to return */ @@ -1228,6 +1419,7 @@ static u16 cellSizePtr(MemPage *pPage, u8 *pCell){ pPage->xParseCell(pPage, pCell, &debuginfo); #endif + assert( pPage->childPtrSize==4 ); nSize = *pIter; if( nSize>=0x80 ){ pEnd = &pIter[8]; @@ -1237,15 +1429,50 @@ static u16 cellSizePtr(MemPage *pPage, u8 *pCell){ }while( *(pIter)>=0x80 && pIterintKey ){ - /* pIter now points at the 64-bit integer key value, a variable length - ** integer. The following block moves pIter to point at the first byte - ** past the end of the key value. */ - pEnd = &pIter[9]; - while( (*pIter++)&0x80 && pItermaxLocal ); + testcase( nSize==(u32)pPage->maxLocal+1 ); + if( nSize<=pPage->maxLocal ){ + nSize += (u32)(pIter - pCell); + assert( nSize>4 ); + }else{ + int minLocal = pPage->minLocal; + nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4); + testcase( nSize==pPage->maxLocal ); + testcase( nSize==(u32)pPage->maxLocal+1 ); + if( nSize>pPage->maxLocal ){ + nSize = minLocal; + } + nSize += 4 + (u16)(pIter - pCell); + } + assert( nSize==debuginfo.nSize || CORRUPT_DB ); + return (u16)nSize; +} +static u16 cellSizePtrIdxLeaf(MemPage *pPage, u8 *pCell){ + u8 *pIter = pCell; /* For looping over bytes of pCell */ + u8 *pEnd; /* End mark for a varint */ + u32 nSize; /* Size value to return */ + +#ifdef SQLITE_DEBUG + /* The value returned by this function should always be the same as + ** the (CellInfo.nSize) value found by doing a full parse of the + ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of + ** this function verifies that this invariant is not violated. */ + CellInfo debuginfo; + pPage->xParseCell(pPage, pCell, &debuginfo); +#endif + + assert( pPage->childPtrSize==0 ); + nSize = *pIter; + if( nSize>=0x80 ){ + pEnd = &pIter[8]; + nSize &= 0x7f; + do{ + nSize = (nSize<<7) | (*++pIter & 0x7f); + }while( *(pIter)>=0x80 && pItermaxLocal ); - testcase( nSize==pPage->maxLocal+1 ); + testcase( nSize==(u32)pPage->maxLocal+1 ); if( nSize<=pPage->maxLocal ){ nSize += (u32)(pIter - pCell); if( nSize<4 ) nSize = 4; @@ -1253,7 +1480,7 @@ static u16 cellSizePtr(MemPage *pPage, u8 *pCell){ int minLocal = pPage->minLocal; nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4); testcase( nSize==pPage->maxLocal ); - testcase( nSize==pPage->maxLocal+1 ); + testcase( nSize==(u32)pPage->maxLocal+1 ); if( nSize>pPage->maxLocal ){ nSize = minLocal; } @@ -1283,6 +1510,58 @@ static u16 cellSizePtrNoPayload(MemPage *pPage, u8 *pCell){ assert( debuginfo.nSize==(u16)(pIter - pCell) || CORRUPT_DB ); return (u16)(pIter - pCell); } +static u16 cellSizePtrTableLeaf(MemPage *pPage, u8 *pCell){ + u8 *pIter = pCell; /* For looping over bytes of pCell */ + u8 *pEnd; /* End mark for a varint */ + u32 nSize; /* Size value to return */ + +#ifdef SQLITE_DEBUG + /* The value returned by this function should always be the same as + ** the (CellInfo.nSize) value found by doing a full parse of the + ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of + ** this function verifies that this invariant is not violated. */ + CellInfo debuginfo; + pPage->xParseCell(pPage, pCell, &debuginfo); +#endif + + nSize = *pIter; + if( nSize>=0x80 ){ + pEnd = &pIter[8]; + nSize &= 0x7f; + do{ + nSize = (nSize<<7) | (*++pIter & 0x7f); + }while( *(pIter)>=0x80 && pItermaxLocal ); + testcase( nSize==(u32)pPage->maxLocal+1 ); + if( nSize<=pPage->maxLocal ){ + nSize += (u32)(pIter - pCell); + if( nSize<4 ) nSize = 4; + }else{ + int minLocal = pPage->minLocal; + nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4); + testcase( nSize==pPage->maxLocal ); + testcase( nSize==(u32)pPage->maxLocal+1 ); + if( nSize>pPage->maxLocal ){ + nSize = minLocal; + } + nSize += 4 + (u16)(pIter - pCell); + } + assert( nSize==debuginfo.nSize || CORRUPT_DB ); + return (u16)nSize; +} #ifdef SQLITE_DEBUG @@ -1295,17 +1574,24 @@ static u16 cellSize(MemPage *pPage, int iCell){ #ifndef SQLITE_OMIT_AUTOVACUUM /* -** If the cell pCell, part of page pPage contains a pointer -** to an overflow page, insert an entry into the pointer-map -** for the overflow page. +** The cell pCell is currently part of page pSrc but will ultimately be part +** of pPage. (pSrc and pPage are often the same.) If pCell contains a +** pointer to an overflow page, insert an entry into the pointer-map for +** the overflow page that will be valid after pCell has been moved to pPage. */ -static void ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell, int *pRC){ +static void ptrmapPutOvflPtr(MemPage *pPage, MemPage *pSrc, u8 *pCell,int *pRC){ CellInfo info; if( *pRC ) return; assert( pCell!=0 ); pPage->xParseCell(pPage, pCell, &info); if( info.nLocalaDataEnd, pCell, pCell+info.nLocal) ){ + testcase( pSrc!=pPage ); + *pRC = SQLITE_CORRUPT_BKPT; + return; + } + ovfl = get4byte(&pCell[info.nSize-4]); ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC); } } @@ -1313,17 +1599,18 @@ static void ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell, int *pRC){ /* -** Defragment the page given. All Cells are moved to the -** end of the page and all free space is collected into one -** big FreeBlk that occurs in between the header and cell -** pointer array and the cell content area. +** Defragment the page given. This routine reorganizes cells within the +** page so that there are no free-blocks on the free-block list. +** +** Parameter nMaxFrag is the maximum amount of fragmented space that may be +** present in the page after this routine returns. ** ** EVIDENCE-OF: R-44582-60138 SQLite may from time to time reorganize a ** b-tree page so that there are no freeblocks or fragment bytes, all ** unused bytes are contained in the unallocated space region, and all ** cells are packed tightly at the end of the page. */ -static int defragmentPage(MemPage *pPage){ +static int defragmentPage(MemPage *pPage, int nMaxFrag){ int i; /* Loop counter */ int pc; /* Address of the i-th cell */ int hdr; /* Offset to the page header */ @@ -1337,65 +1624,109 @@ static int defragmentPage(MemPage *pPage){ unsigned char *src; /* Source of content */ int iCellFirst; /* First allowable cell index */ int iCellLast; /* Last possible cell index */ - + int iCellStart; /* First cell offset in input */ assert( sqlite3PagerIswriteable(pPage->pDbPage) ); assert( pPage->pBt!=0 ); assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE ); assert( pPage->nOverflow==0 ); assert( sqlite3_mutex_held(pPage->pBt->mutex) ); - temp = 0; - src = data = pPage->aData; + data = pPage->aData; hdr = pPage->hdrOffset; cellOffset = pPage->cellOffset; nCell = pPage->nCell; - assert( nCell==get2byte(&data[hdr+3]) ); + assert( nCell==get2byte(&data[hdr+3]) || CORRUPT_DB ); + iCellFirst = cellOffset + 2*nCell; usableSize = pPage->pBt->usableSize; + + /* This block handles pages with two or fewer free blocks and nMaxFrag + ** or fewer fragmented bytes. In this case it is faster to move the + ** two (or one) blocks of cells using memmove() and add the required + ** offsets to each pointer in the cell-pointer array than it is to + ** reconstruct the entire page. */ + if( (int)data[hdr+7]<=nMaxFrag ){ + int iFree = get2byte(&data[hdr+1]); + if( iFree>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage); + if( iFree ){ + int iFree2 = get2byte(&data[iFree]); + if( iFree2>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage); + if( 0==iFree2 || (data[iFree2]==0 && data[iFree2+1]==0) ){ + u8 *pEnd = &data[cellOffset + nCell*2]; + u8 *pAddr; + int sz2 = 0; + int sz = get2byte(&data[iFree+2]); + int top = get2byte(&data[hdr+5]); + if( top>=iFree ){ + return SQLITE_CORRUPT_PAGE(pPage); + } + if( iFree2 ){ + if( iFree+sz>iFree2 ) return SQLITE_CORRUPT_PAGE(pPage); + sz2 = get2byte(&data[iFree2+2]); + if( iFree2+sz2 > usableSize ) return SQLITE_CORRUPT_PAGE(pPage); + memmove(&data[iFree+sz+sz2], &data[iFree+sz], iFree2-(iFree+sz)); + sz += sz2; + }else if( iFree+sz>usableSize ){ + return SQLITE_CORRUPT_PAGE(pPage); + } + + cbrk = top+sz; + assert( cbrk+(iFree-top) <= usableSize ); + memmove(&data[cbrk], &data[top], iFree-top); + for(pAddr=&data[cellOffset]; pAddriCellLast ){ - return SQLITE_CORRUPT_BKPT; - } - assert( pc>=iCellFirst && pc<=iCellLast ); - size = pPage->xCellSize(pPage, &src[pc]); - cbrk -= size; - if( cbrkusableSize ){ - return SQLITE_CORRUPT_BKPT; - } - assert( cbrk+size<=usableSize && cbrk>=iCellFirst ); - testcase( cbrk+size==usableSize ); - testcase( pc+size==usableSize ); - put2byte(pAddr, cbrk); - if( temp==0 ){ - int x; - if( cbrk==pc ) continue; - temp = sqlite3PagerTempSpace(pPage->pBt->pPager); - x = get2byte(&data[hdr+5]); - memcpy(&temp[x], &data[x], (cbrk+size) - x); - src = temp; + iCellStart = get2byte(&data[hdr+5]); + if( nCell>0 ){ + temp = sqlite3PagerTempSpace(pPage->pBt->pPager); + memcpy(temp, data, usableSize); + src = temp; + for(i=0; iiCellLast ){ + return SQLITE_CORRUPT_PAGE(pPage); + } + assert( pc>=0 && pc<=iCellLast ); + size = pPage->xCellSize(pPage, &src[pc]); + cbrk -= size; + if( cbrkusableSize ){ + return SQLITE_CORRUPT_PAGE(pPage); + } + assert( cbrk+size<=usableSize && cbrk>=iCellStart ); + testcase( cbrk+size==usableSize ); + testcase( pc+size==usableSize ); + put2byte(pAddr, cbrk); + memcpy(&data[cbrk], &src[pc], size); } - memcpy(&data[cbrk], &src[pc], size); + } + data[hdr+7] = 0; + +defragment_out: + assert( pPage->nFree>=0 ); + if( data[hdr+7]+cbrk-iCellFirst!=pPage->nFree ){ + return SQLITE_CORRUPT_PAGE(pPage); } assert( cbrk>=iCellFirst ); put2byte(&data[hdr+5], cbrk); data[hdr+1] = 0; data[hdr+2] = 0; - data[hdr+7] = 0; memset(&data[iCellFirst], 0, cbrk-iCellFirst); assert( sqlite3PagerIswriteable(pPage->pDbPage) ); - if( cbrk-iCellFirst!=pPage->nFree ){ - return SQLITE_CORRUPT_BKPT; - } return SQLITE_OK; } @@ -1414,33 +1745,26 @@ static int defragmentPage(MemPage *pPage){ ** causes the fragmentation count to exceed 60. */ static u8 *pageFindSlot(MemPage *pPg, int nByte, int *pRc){ - const int hdr = pPg->hdrOffset; - u8 * const aData = pPg->aData; - int iAddr = hdr + 1; - int pc = get2byte(&aData[iAddr]); - int x; - int usableSize = pPg->pBt->usableSize; + const int hdr = pPg->hdrOffset; /* Offset to page header */ + u8 * const aData = pPg->aData; /* Page data */ + int iAddr = hdr + 1; /* Address of ptr to pc */ + u8 *pTmp = &aData[iAddr]; /* Temporary ptr into aData[] */ + int pc = get2byte(pTmp); /* Address of a free slot */ + int x; /* Excess size of the slot */ + int maxPC = pPg->pBt->usableSize - nByte; /* Max address for a usable slot */ + int size; /* Size of the free slot */ assert( pc>0 ); - do{ - int size; /* Size of the free slot */ - /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of - ** increasing offset. */ - if( pc>usableSize-4 || pc=0 ){ testcase( x==4 ); testcase( x==3 ); - if( pc < pPg->cellOffset+2*pPg->nCell || size+pc > usableSize ){ - *pRc = SQLITE_CORRUPT_BKPT; - return 0; - }else if( x<4 ){ + if( x<4 ){ /* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total ** number of bytes in fragments may not exceed 60. */ if( aData[hdr+7]>57 ) return 0; @@ -1449,17 +1773,33 @@ static u8 *pageFindSlot(MemPage *pPg, int nByte, int *pRc){ ** fragmented bytes within the page. */ memcpy(&aData[iAddr], &aData[pc], 2); aData[hdr+7] += (u8)x; + return &aData[pc]; + }else if( x+pc > maxPC ){ + /* This slot extends off the end of the usable part of the page */ + *pRc = SQLITE_CORRUPT_PAGE(pPg); + return 0; }else{ /* The slot remains on the free-list. Reduce its size to account - ** for the portion used by the new allocation. */ + ** for the portion used by the new allocation. */ put2byte(&aData[pc+2], x); } return &aData[pc + x]; } iAddr = pc; - pc = get2byte(&aData[pc]); - }while( pc ); - + pTmp = &aData[pc]; + pc = get2byte(pTmp); + if( pc<=iAddr ){ + if( pc ){ + /* The next slot in the chain comes before the current slot */ + *pRc = SQLITE_CORRUPT_PAGE(pPg); + } + return 0; + } + } + if( pc>maxPC+nByte-4 ){ + /* The free slot chain extends off the end of the page */ + *pRc = SQLITE_CORRUPT_PAGE(pPg); + } return 0; } @@ -1476,13 +1816,14 @@ static u8 *pageFindSlot(MemPage *pPg, int nByte, int *pRc){ ** allocation is being made in order to insert a new cell, so we will ** also end up needing a new cell pointer. */ -static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){ +static SQLITE_INLINE int allocateSpace(MemPage *pPage, int nByte, int *pIdx){ const int hdr = pPage->hdrOffset; /* Local cache of pPage->hdrOffset */ u8 * const data = pPage->aData; /* Local cache of pPage->aData */ int top; /* First byte of cell content area */ int rc = SQLITE_OK; /* Integer return code */ + u8 *pTmp; /* Temp ptr into data[] */ int gap; /* First byte of gap between cell pointers and cell content */ - + assert( sqlite3PagerIswriteable(pPage->pDbPage) ); assert( pPage->pBt ); assert( sqlite3_mutex_held(pPage->pBt->mutex) ); @@ -1499,19 +1840,21 @@ static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){ ** then the cell content offset of an empty page wants to be 65536. ** However, that integer is too large to be stored in a 2-byte unsigned ** integer, so a value of 0 is used in its place. */ - top = get2byte(&data[hdr+5]); - assert( top<=(int)pPage->pBt->usableSize ); /* Prevent by getAndInitPage() */ + pTmp = &data[hdr+5]; + top = get2byte(pTmp); if( gap>top ){ if( top==0 && pPage->pBt->usableSize==65536 ){ top = 65536; }else{ - return SQLITE_CORRUPT_BKPT; + return SQLITE_CORRUPT_PAGE(pPage); } + }else if( top>(int)pPage->pBt->usableSize ){ + return SQLITE_CORRUPT_PAGE(pPage); } - /* If there is enough space between gap and top for one more cell pointer - ** array entry offset, and if the freelist is not empty, then search the - ** freelist looking for a free slot big enough to satisfy the request. + /* If there is enough space between gap and top for one more cell pointer, + ** and if the freelist is not empty, then search the + ** freelist looking for a slot big enough to satisfy the request. */ testcase( gap+2==top ); testcase( gap+1==top ); @@ -1519,9 +1862,14 @@ static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){ if( (data[hdr+2] || data[hdr+1]) && gap+2<=top ){ u8 *pSpace = pageFindSlot(pPage, nByte, &rc); if( pSpace ){ - assert( pSpace>=data && (pSpace - data)<65536 ); - *pIdx = (int)(pSpace - data); - return SQLITE_OK; + int g2; + assert( pSpace+nByte<=data+pPage->pBt->usableSize ); + *pIdx = g2 = (int)(pSpace-data); + if( g2<=gap ){ + return SQLITE_CORRUPT_PAGE(pPage); + }else{ + return SQLITE_OK; + } }else if( rc ){ return rc; } @@ -1533,15 +1881,16 @@ static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){ testcase( gap+2+nByte==top ); if( gap+2+nByte>top ){ assert( pPage->nCell>0 || CORRUPT_DB ); - rc = defragmentPage(pPage); + assert( pPage->nFree>=0 ); + rc = defragmentPage(pPage, MIN(4, pPage->nFree - (2+nByte))); if( rc ) return rc; top = get2byteNotZero(&data[hdr+5]); - assert( gap+nByte<=top ); + assert( gap+2+nByte<=top ); } /* Allocate memory from the gap in between the cell pointer array - ** and the cell content area. The btreeInitPage() call has already + ** and the cell content area. The btreeComputeFreeSpace() call has already ** validated the freelist. Given that the freelist is valid, there ** is no way that the allocation can extend off the end of the page. ** The assert() below verifies the previous sentence. @@ -1560,37 +1909,32 @@ static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){ ** ** Adjacent freeblocks are coalesced. ** -** Note that even though the freeblock list was checked by btreeInitPage(), +** Even though the freeblock list was checked by btreeComputeFreeSpace(), ** that routine will not detect overlap between cells or freeblocks. Nor -** does it detect cells or freeblocks that encrouch into the reserved bytes +** does it detect cells or freeblocks that encroach into the reserved bytes ** at the end of the page. So do additional corruption checks inside this ** routine and return SQLITE_CORRUPT if any problems are found. */ -static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){ - u16 iPtr; /* Address of ptr to next freeblock */ - u16 iFreeBlk; /* Address of the next freeblock */ +static int freeSpace(MemPage *pPage, int iStart, int iSize){ + int iPtr; /* Address of ptr to next freeblock */ + int iFreeBlk; /* Address of the next freeblock */ u8 hdr; /* Page header size. 0 or 100 */ - u8 nFrag = 0; /* Reduction in fragmentation */ - u16 iOrigSize = iSize; /* Original value of iSize */ - u32 iLast = pPage->pBt->usableSize-4; /* Largest possible freeblock offset */ - u32 iEnd = iStart + iSize; /* First byte past the iStart buffer */ + int nFrag = 0; /* Reduction in fragmentation */ + int iOrigSize = iSize; /* Original value of iSize */ + int x; /* Offset to cell content area */ + int iEnd = iStart + iSize; /* First byte past the iStart buffer */ unsigned char *data = pPage->aData; /* Page content */ + u8 *pTmp; /* Temporary ptr into data[] */ assert( pPage->pBt!=0 ); assert( sqlite3PagerIswriteable(pPage->pDbPage) ); assert( CORRUPT_DB || iStart>=pPage->hdrOffset+6+pPage->childPtrSize ); - assert( CORRUPT_DB || iEnd <= pPage->pBt->usableSize ); + assert( CORRUPT_DB || iEnd <= (int)pPage->pBt->usableSize ); assert( sqlite3_mutex_held(pPage->pBt->mutex) ); assert( iSize>=4 ); /* Minimum cell size is 4 */ - assert( iStart<=iLast ); - - /* Overwrite deleted information with zeros when the secure_delete - ** option is enabled */ - if( pPage->pBt->btsFlags & BTS_SECURE_DELETE ){ - memset(&data[iStart], 0, iSize); - } + assert( CORRUPT_DB || iStart<=(int)pPage->pBt->usableSize-4 ); - /* The list of freeblocks must be in ascending order. Find the + /* The list of freeblocks must be in ascending order. Find the ** spot on the list where iStart should be inserted. */ hdr = pPage->hdrOffset; @@ -1598,13 +1942,18 @@ static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){ if( data[iPtr+1]==0 && data[iPtr]==0 ){ iFreeBlk = 0; /* Shortcut for the case when the freelist is empty */ }else{ - while( (iFreeBlk = get2byte(&data[iPtr]))>0 && iFreeBlkiLast ) return SQLITE_CORRUPT_BKPT; - assert( iFreeBlk>iPtr || iFreeBlk==0 ); - + if( iFreeBlk>(int)pPage->pBt->usableSize-4 ){ /* TH3: corrupt081.100 */ + return SQLITE_CORRUPT_PAGE(pPage); + } + assert( iFreeBlk>iPtr || iFreeBlk==0 || CORRUPT_DB ); + /* At this point: ** iFreeBlk: First freeblock after iStart, or zero if none ** iPtr: The address of a pointer to iFreeBlk @@ -1613,13 +1962,15 @@ static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){ */ if( iFreeBlk && iEnd+3>=iFreeBlk ){ nFrag = iFreeBlk - iEnd; - if( iEnd>iFreeBlk ) return SQLITE_CORRUPT_BKPT; + if( iEnd>iFreeBlk ) return SQLITE_CORRUPT_PAGE(pPage); iEnd = iFreeBlk + get2byte(&data[iFreeBlk+2]); - if( iEnd > pPage->pBt->usableSize ) return SQLITE_CORRUPT_BKPT; + if( iEnd > (int)pPage->pBt->usableSize ){ + return SQLITE_CORRUPT_PAGE(pPage); + } iSize = iEnd - iStart; iFreeBlk = get2byte(&data[iFreeBlk]); } - + /* If iPtr is another freeblock (that is, if iPtr is not the freelist ** pointer in the page header) then check to see if iStart should be ** coalesced onto the end of iPtr. @@ -1627,27 +1978,36 @@ static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){ if( iPtr>hdr+1 ){ int iPtrEnd = iPtr + get2byte(&data[iPtr+2]); if( iPtrEnd+3>=iStart ){ - if( iPtrEnd>iStart ) return SQLITE_CORRUPT_BKPT; + if( iPtrEnd>iStart ) return SQLITE_CORRUPT_PAGE(pPage); nFrag += iStart - iPtrEnd; iSize = iEnd - iPtr; iStart = iPtr; } } - if( nFrag>data[hdr+7] ) return SQLITE_CORRUPT_BKPT; - data[hdr+7] -= nFrag; + if( nFrag>data[hdr+7] ) return SQLITE_CORRUPT_PAGE(pPage); + data[hdr+7] -= (u8)nFrag; } - if( iStart==get2byte(&data[hdr+5]) ){ + pTmp = &data[hdr+5]; + x = get2byte(pTmp); + if( pPage->pBt->btsFlags & BTS_FAST_SECURE ){ + /* Overwrite deleted information with zeros when the secure_delete + ** option is enabled */ + memset(&data[iStart], 0, iSize); + } + if( iStart<=x ){ /* The new freeblock is at the beginning of the cell content area, ** so just extend the cell content area rather than create another ** freelist entry */ - if( iPtr!=hdr+1 ) return SQLITE_CORRUPT_BKPT; + if( iStart=0 && iSize<=0xffff ); + put2byte(&data[iStart+2], (u16)iSize); } pPage->nFree += iOrigSize; return SQLITE_OK; @@ -1660,70 +2020,83 @@ static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){ ** Only the following combinations are supported. Anything different ** indicates a corrupt database files: ** -** PTF_ZERODATA -** PTF_ZERODATA | PTF_LEAF -** PTF_LEAFDATA | PTF_INTKEY -** PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF +** PTF_ZERODATA (0x02, 2) +** PTF_LEAFDATA | PTF_INTKEY (0x05, 5) +** PTF_ZERODATA | PTF_LEAF (0x0a, 10) +** PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF (0x0d, 13) */ static int decodeFlags(MemPage *pPage, int flagByte){ BtShared *pBt; /* A copy of pPage->pBt */ assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) ); assert( sqlite3_mutex_held(pPage->pBt->mutex) ); - pPage->leaf = (u8)(flagByte>>3); assert( PTF_LEAF == 1<<3 ); - flagByte &= ~PTF_LEAF; - pPage->childPtrSize = 4-4*pPage->leaf; - pPage->xCellSize = cellSizePtr; pBt = pPage->pBt; - if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){ - /* EVIDENCE-OF: R-03640-13415 A value of 5 means the page is an interior - ** table b-tree page. */ - assert( (PTF_LEAFDATA|PTF_INTKEY)==5 ); - /* EVIDENCE-OF: R-20501-61796 A value of 13 means the page is a leaf - ** table b-tree page. */ - assert( (PTF_LEAFDATA|PTF_INTKEY|PTF_LEAF)==13 ); - pPage->intKey = 1; - if( pPage->leaf ){ + pPage->max1bytePayload = pBt->max1bytePayload; + if( flagByte>=(PTF_ZERODATA | PTF_LEAF) ){ + pPage->childPtrSize = 0; + pPage->leaf = 1; + if( flagByte==(PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF) ){ pPage->intKeyLeaf = 1; + pPage->xCellSize = cellSizePtrTableLeaf; pPage->xParseCell = btreeParseCellPtr; + pPage->intKey = 1; + pPage->maxLocal = pBt->maxLeaf; + pPage->minLocal = pBt->minLeaf; + }else if( flagByte==(PTF_ZERODATA | PTF_LEAF) ){ + pPage->intKey = 0; + pPage->intKeyLeaf = 0; + pPage->xCellSize = cellSizePtrIdxLeaf; + pPage->xParseCell = btreeParseCellPtrIndex; + pPage->maxLocal = pBt->maxLocal; + pPage->minLocal = pBt->minLocal; }else{ + pPage->intKey = 0; + pPage->intKeyLeaf = 0; + pPage->xCellSize = cellSizePtrIdxLeaf; + pPage->xParseCell = btreeParseCellPtrIndex; + return SQLITE_CORRUPT_PAGE(pPage); + } + }else{ + pPage->childPtrSize = 4; + pPage->leaf = 0; + if( flagByte==(PTF_ZERODATA) ){ + pPage->intKey = 0; + pPage->intKeyLeaf = 0; + pPage->xCellSize = cellSizePtr; + pPage->xParseCell = btreeParseCellPtrIndex; + pPage->maxLocal = pBt->maxLocal; + pPage->minLocal = pBt->minLocal; + }else if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){ pPage->intKeyLeaf = 0; pPage->xCellSize = cellSizePtrNoPayload; pPage->xParseCell = btreeParseCellPtrNoPayload; + pPage->intKey = 1; + pPage->maxLocal = pBt->maxLeaf; + pPage->minLocal = pBt->minLeaf; + }else{ + pPage->intKey = 0; + pPage->intKeyLeaf = 0; + pPage->xCellSize = cellSizePtr; + pPage->xParseCell = btreeParseCellPtrIndex; + return SQLITE_CORRUPT_PAGE(pPage); } - pPage->maxLocal = pBt->maxLeaf; - pPage->minLocal = pBt->minLeaf; - }else if( flagByte==PTF_ZERODATA ){ - /* EVIDENCE-OF: R-27225-53936 A value of 2 means the page is an interior - ** index b-tree page. */ - assert( (PTF_ZERODATA)==2 ); - /* EVIDENCE-OF: R-16571-11615 A value of 10 means the page is a leaf - ** index b-tree page. */ - assert( (PTF_ZERODATA|PTF_LEAF)==10 ); - pPage->intKey = 0; - pPage->intKeyLeaf = 0; - pPage->xParseCell = btreeParseCellPtrIndex; - pPage->maxLocal = pBt->maxLocal; - pPage->minLocal = pBt->minLocal; - }else{ - /* EVIDENCE-OF: R-47608-56469 Any other value for the b-tree page type is - ** an error. */ - return SQLITE_CORRUPT_BKPT; } - pPage->max1bytePayload = pBt->max1bytePayload; return SQLITE_OK; } /* -** Initialize the auxiliary information for a disk block. -** -** Return SQLITE_OK on success. If we see that the page does -** not contain a well-formed database page, then return -** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not -** guarantee that the page is well-formed. It only shows that -** we failed to detect any corruption. +** Compute the amount of freespace on the page. In other words, fill +** in the pPage->nFree field. */ -static int btreeInitPage(MemPage *pPage){ +static int btreeComputeFreeSpace(MemPage *pPage){ + int pc; /* Address of a freeblock within pPage->aData[] */ + u8 hdr; /* Offset to beginning of page header */ + u8 *data; /* Equal to pPage->aData */ + int usableSize; /* Amount of usable space on each page */ + int nFree; /* Number of unused bytes on the page */ + int top; /* First byte of the cell content area */ + int iCellFirst; /* First allowable cell or freeblock offset */ + int iCellLast; /* Last possible cell or freeblock offset */ assert( pPage->pBt!=0 ); assert( pPage->pBt->db!=0 ); @@ -1731,121 +2104,158 @@ static int btreeInitPage(MemPage *pPage){ assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) ); assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) ); assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) ); + assert( pPage->isInit==1 ); + assert( pPage->nFree<0 ); - if( !pPage->isInit ){ - u16 pc; /* Address of a freeblock within pPage->aData[] */ - u8 hdr; /* Offset to beginning of page header */ - u8 *data; /* Equal to pPage->aData */ - BtShared *pBt; /* The main btree structure */ - int usableSize; /* Amount of usable space on each page */ - u16 cellOffset; /* Offset from start of page to first cell pointer */ - int nFree; /* Number of unused bytes on the page */ - int top; /* First byte of the cell content area */ - int iCellFirst; /* First allowable cell or freeblock offset */ - int iCellLast; /* Last possible cell or freeblock offset */ - - pBt = pPage->pBt; - - hdr = pPage->hdrOffset; - data = pPage->aData; - /* EVIDENCE-OF: R-28594-02890 The one-byte flag at offset 0 indicating - ** the b-tree page type. */ - if( decodeFlags(pPage, data[hdr]) ) return SQLITE_CORRUPT_BKPT; - assert( pBt->pageSize>=512 && pBt->pageSize<=65536 ); - pPage->maskPage = (u16)(pBt->pageSize - 1); - pPage->nOverflow = 0; - usableSize = pBt->usableSize; - pPage->cellOffset = cellOffset = hdr + 8 + pPage->childPtrSize; - pPage->aDataEnd = &data[usableSize]; - pPage->aCellIdx = &data[cellOffset]; - pPage->aDataOfst = &data[pPage->childPtrSize]; - /* EVIDENCE-OF: R-58015-48175 The two-byte integer at offset 5 designates - ** the start of the cell content area. A zero value for this integer is - ** interpreted as 65536. */ - top = get2byteNotZero(&data[hdr+5]); - /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the - ** number of cells on the page. */ - pPage->nCell = get2byte(&data[hdr+3]); - if( pPage->nCell>MX_CELL(pBt) ){ - /* To many cells for a single page. The page must be corrupt */ - return SQLITE_CORRUPT_BKPT; - } - testcase( pPage->nCell==MX_CELL(pBt) ); - /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only - ** possible for a root page of a table that contains no rows) then the - ** offset to the cell content area will equal the page size minus the - ** bytes of reserved space. */ - assert( pPage->nCell>0 || top==usableSize || CORRUPT_DB ); + usableSize = pPage->pBt->usableSize; + hdr = pPage->hdrOffset; + data = pPage->aData; + /* EVIDENCE-OF: R-58015-48175 The two-byte integer at offset 5 designates + ** the start of the cell content area. A zero value for this integer is + ** interpreted as 65536. */ + top = get2byteNotZero(&data[hdr+5]); + iCellFirst = hdr + 8 + pPage->childPtrSize + 2*pPage->nCell; + iCellLast = usableSize - 4; - /* A malformed database page might cause us to read past the end - ** of page when parsing a cell. - ** - ** The following block of code checks early to see if a cell extends - ** past the end of a page boundary and causes SQLITE_CORRUPT to be - ** returned if it does. - */ - iCellFirst = cellOffset + 2*pPage->nCell; - iCellLast = usableSize - 4; - if( pBt->db->flags & SQLITE_CellSizeCk ){ - int i; /* Index into the cell pointer array */ - int sz; /* Size of a cell */ - - if( !pPage->leaf ) iCellLast--; - for(i=0; inCell; i++){ - pc = get2byteAligned(&data[cellOffset+i*2]); - testcase( pc==iCellFirst ); - testcase( pc==iCellLast ); - if( pciCellLast ){ - return SQLITE_CORRUPT_BKPT; - } - sz = pPage->xCellSize(pPage, &data[pc]); - testcase( pc+sz==usableSize ); - if( pc+sz>usableSize ){ - return SQLITE_CORRUPT_BKPT; - } - } - if( !pPage->leaf ) iCellLast++; - } - - /* Compute the total free space on the page - ** EVIDENCE-OF: R-23588-34450 The two-byte integer at offset 1 gives the - ** start of the first freeblock on the page, or is zero if there are no - ** freeblocks. */ - pc = get2byte(&data[hdr+1]); - nFree = data[hdr+7] + top; /* Init nFree to non-freeblock free space */ - while( pc>0 ){ - u16 next, size; - if( pciCellLast ){ - /* EVIDENCE-OF: R-55530-52930 In a well-formed b-tree page, there will - ** always be at least one cell before the first freeblock. - ** - ** Or, the freeblock is off the end of the page - */ - return SQLITE_CORRUPT_BKPT; + /* Compute the total free space on the page + ** EVIDENCE-OF: R-23588-34450 The two-byte integer at offset 1 gives the + ** start of the first freeblock on the page, or is zero if there are no + ** freeblocks. */ + pc = get2byte(&data[hdr+1]); + nFree = data[hdr+7] + top; /* Init nFree to non-freeblock free space */ + if( pc>0 ){ + u32 next, size; + if( pciCellLast ){ + /* Freeblock off the end of the page */ + return SQLITE_CORRUPT_PAGE(pPage); } next = get2byte(&data[pc]); size = get2byte(&data[pc+2]); - if( (next>0 && next<=pc+size+3) || pc+size>usableSize ){ - /* Free blocks must be in ascending order. And the last byte of - ** the free-block must lie on the database page. */ - return SQLITE_CORRUPT_BKPT; - } nFree = nFree + size; + if( next<=pc+size+3 ) break; pc = next; } + if( next>0 ){ + /* Freeblock not in ascending order */ + return SQLITE_CORRUPT_PAGE(pPage); + } + if( pc+size>(unsigned int)usableSize ){ + /* Last freeblock extends past page end */ + return SQLITE_CORRUPT_PAGE(pPage); + } + } - /* At this point, nFree contains the sum of the offset to the start - ** of the cell-content area plus the number of free bytes within - ** the cell-content area. If this is greater than the usable-size - ** of the page, then the page must be corrupted. This check also - ** serves to verify that the offset to the start of the cell-content - ** area, according to the page header, lies within the page. - */ - if( nFree>usableSize ){ - return SQLITE_CORRUPT_BKPT; + /* At this point, nFree contains the sum of the offset to the start + ** of the cell-content area plus the number of free bytes within + ** the cell-content area. If this is greater than the usable-size + ** of the page, then the page must be corrupted. This check also + ** serves to verify that the offset to the start of the cell-content + ** area, according to the page header, lies within the page. + */ + if( nFree>usableSize || nFreenFree = (u16)(nFree - iCellFirst); + return SQLITE_OK; +} + +/* +** Do additional sanity check after btreeInitPage() if +** PRAGMA cell_size_check=ON +*/ +static SQLITE_NOINLINE int btreeCellSizeCheck(MemPage *pPage){ + int iCellFirst; /* First allowable cell or freeblock offset */ + int iCellLast; /* Last possible cell or freeblock offset */ + int i; /* Index into the cell pointer array */ + int sz; /* Size of a cell */ + int pc; /* Address of a freeblock within pPage->aData[] */ + u8 *data; /* Equal to pPage->aData */ + int usableSize; /* Maximum usable space on the page */ + int cellOffset; /* Start of cell content area */ + + iCellFirst = pPage->cellOffset + 2*pPage->nCell; + usableSize = pPage->pBt->usableSize; + iCellLast = usableSize - 4; + data = pPage->aData; + cellOffset = pPage->cellOffset; + if( !pPage->leaf ) iCellLast--; + for(i=0; inCell; i++){ + pc = get2byteAligned(&data[cellOffset+i*2]); + testcase( pc==iCellFirst ); + testcase( pc==iCellLast ); + if( pciCellLast ){ + return SQLITE_CORRUPT_PAGE(pPage); } - pPage->nFree = (u16)(nFree - iCellFirst); - pPage->isInit = 1; + sz = pPage->xCellSize(pPage, &data[pc]); + testcase( pc+sz==usableSize ); + if( pc+sz>usableSize ){ + return SQLITE_CORRUPT_PAGE(pPage); + } + } + return SQLITE_OK; +} + +/* +** Initialize the auxiliary information for a disk block. +** +** Return SQLITE_OK on success. If we see that the page does +** not contain a well-formed database page, then return +** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not +** guarantee that the page is well-formed. It only shows that +** we failed to detect any corruption. +*/ +static int btreeInitPage(MemPage *pPage){ + u8 *data; /* Equal to pPage->aData */ + BtShared *pBt; /* The main btree structure */ + + assert( pPage->pBt!=0 ); + assert( pPage->pBt->db!=0 ); + assert( sqlite3_mutex_held(pPage->pBt->mutex) ); + assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) ); + assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) ); + assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) ); + assert( pPage->isInit==0 ); + + pBt = pPage->pBt; + data = pPage->aData + pPage->hdrOffset; + /* EVIDENCE-OF: R-28594-02890 The one-byte flag at offset 0 indicating + ** the b-tree page type. */ + if( decodeFlags(pPage, data[0]) ){ + return SQLITE_CORRUPT_PAGE(pPage); + } + assert( pBt->pageSize>=512 && pBt->pageSize<=65536 ); + pPage->maskPage = (u16)(pBt->pageSize - 1); + pPage->nOverflow = 0; + pPage->cellOffset = (u16)(pPage->hdrOffset + 8 + pPage->childPtrSize); + pPage->aCellIdx = data + pPage->childPtrSize + 8; + pPage->aDataEnd = pPage->aData + pBt->pageSize; + pPage->aDataOfst = pPage->aData + pPage->childPtrSize; + /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the + ** number of cells on the page. */ + pPage->nCell = get2byte(&data[3]); + if( pPage->nCell>MX_CELL(pBt) ){ + /* To many cells for a single page. The page must be corrupt */ + return SQLITE_CORRUPT_PAGE(pPage); + } + testcase( pPage->nCell==MX_CELL(pBt) ); + /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only + ** possible for a root page of a table that contains no rows) then the + ** offset to the cell content area will equal the page size minus the + ** bytes of reserved space. */ + assert( pPage->nCell>0 + || get2byteNotZero(&data[5])==(int)pBt->usableSize + || CORRUPT_DB ); + pPage->nFree = -1; /* Indicate that this value is yet uncomputed */ + pPage->isInit = 1; + if( pBt->db->flags & SQLITE_CellSizeCk ){ + return btreeCellSizeCheck(pPage); } return SQLITE_OK; } @@ -1857,15 +2267,15 @@ static int btreeInitPage(MemPage *pPage){ static void zeroPage(MemPage *pPage, int flags){ unsigned char *data = pPage->aData; BtShared *pBt = pPage->pBt; - u8 hdr = pPage->hdrOffset; - u16 first; + int hdr = pPage->hdrOffset; + int first; - assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno ); + assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno || CORRUPT_DB ); assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage ); assert( sqlite3PagerGetData(pPage->pDbPage) == data ); assert( sqlite3PagerIswriteable(pPage->pDbPage) ); assert( sqlite3_mutex_held(pBt->mutex) ); - if( pBt->btsFlags & BTS_SECURE_DELETE ){ + if( pBt->btsFlags & BTS_FAST_SECURE ){ memset(&data[hdr], 0, pBt->usableSize - hdr); } data[hdr] = (char)flags; @@ -1875,8 +2285,8 @@ static void zeroPage(MemPage *pPage, int flags){ put2byte(&data[hdr+5], pBt->usableSize); pPage->nFree = (u16)(pBt->usableSize - first); decodeFlags(pPage, flags); - pPage->cellOffset = first; - pPage->aDataEnd = &data[pBt->usableSize]; + pPage->cellOffset = (u16)first; + pPage->aDataEnd = &data[pBt->pageSize]; pPage->aCellIdx = &data[first]; pPage->aDataOfst = &data[pPage->childPtrSize]; pPage->nOverflow = 0; @@ -1901,7 +2311,7 @@ static MemPage *btreePageFromDbPage(DbPage *pDbPage, Pgno pgno, BtShared *pBt){ pPage->hdrOffset = pgno==1 ? 100 : 0; } assert( pPage->aData==sqlite3PagerGetData(pDbPage) ); - return pPage; + return pPage; } /* @@ -1954,78 +2364,55 @@ static MemPage *btreePageLookup(BtShared *pBt, Pgno pgno){ static Pgno btreePagecount(BtShared *pBt){ return pBt->nPage; } -u32 sqlite3BtreeLastPage(Btree *p){ +Pgno sqlite3BtreeLastPage(Btree *p){ assert( sqlite3BtreeHoldsMutex(p) ); - assert( ((p->pBt->nPage)&0x8000000)==0 ); return btreePagecount(p->pBt); } /* ** Get a page from the pager and initialize it. -** -** If pCur!=0 then the page is being fetched as part of a moveToChild() -** call. Do additional sanity checking on the page in this case. -** And if the fetch fails, this routine must decrement pCur->iPage. -** -** The page is fetched as read-write unless pCur is not NULL and is -** a read-only cursor. -** -** If an error occurs, then *ppPage is undefined. It -** may remain unchanged, or it may be set to an invalid value. */ static int getAndInitPage( BtShared *pBt, /* The database file */ Pgno pgno, /* Number of the page to get */ MemPage **ppPage, /* Write the page pointer here */ - BtCursor *pCur, /* Cursor to receive the page, or NULL */ int bReadOnly /* True for a read-only page */ ){ int rc; DbPage *pDbPage; + MemPage *pPage; assert( sqlite3_mutex_held(pBt->mutex) ); - assert( pCur==0 || ppPage==&pCur->apPage[pCur->iPage] ); - assert( pCur==0 || bReadOnly==pCur->curPagerFlags ); - assert( pCur==0 || pCur->iPage>0 ); if( pgno>btreePagecount(pBt) ){ - rc = SQLITE_CORRUPT_BKPT; - goto getAndInitPage_error; + *ppPage = 0; + return SQLITE_CORRUPT_BKPT; } rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, bReadOnly); if( rc ){ - goto getAndInitPage_error; + *ppPage = 0; + return rc; } - *ppPage = (MemPage*)sqlite3PagerGetExtra(pDbPage); - if( (*ppPage)->isInit==0 ){ + pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage); + if( pPage->isInit==0 ){ btreePageFromDbPage(pDbPage, pgno, pBt); - rc = btreeInitPage(*ppPage); + rc = btreeInitPage(pPage); if( rc!=SQLITE_OK ){ - releasePage(*ppPage); - goto getAndInitPage_error; + releasePage(pPage); + *ppPage = 0; + return rc; } } - assert( (*ppPage)->pgno==pgno ); - assert( (*ppPage)->aData==sqlite3PagerGetData(pDbPage) ); - - /* If obtaining a child page for a cursor, we must verify that the page is - ** compatible with the root page. */ - if( pCur && ((*ppPage)->nCell<1 || (*ppPage)->intKey!=pCur->curIntKey) ){ - rc = SQLITE_CORRUPT_BKPT; - releasePage(*ppPage); - goto getAndInitPage_error; - } + assert( pPage->pgno==pgno || CORRUPT_DB ); + assert( pPage->aData==sqlite3PagerGetData(pDbPage) ); + *ppPage = pPage; return SQLITE_OK; - -getAndInitPage_error: - if( pCur ) pCur->iPage--; - testcase( pgno==0 ); - assert( pgno!=0 || rc==SQLITE_CORRUPT ); - return rc; } /* ** Release a MemPage. This should be called once for each prior ** call to btreeGetPage. +** +** Page1 is a special case and must be released using releasePageOne(). */ static void releasePageNotNull(MemPage *pPage){ assert( pPage->aData ); @@ -2039,6 +2426,16 @@ static void releasePageNotNull(MemPage *pPage){ static void releasePage(MemPage *pPage){ if( pPage ) releasePageNotNull(pPage); } +static void releasePageOne(MemPage *pPage){ + assert( pPage!=0 ); + assert( pPage->aData ); + assert( pPage->pBt ); + assert( pPage->pDbPage!=0 ); + assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage ); + assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData ); + assert( sqlite3_mutex_held(pPage->pBt->mutex) ); + sqlite3PagerUnrefPageOne(pPage->pDbPage); +} /* ** Get an unused page. @@ -2091,7 +2488,7 @@ static void pageReinit(DbPage *pData){ ** call to btreeInitPage() will likely return SQLITE_CORRUPT. ** But no harm is done by this. And it is very important that ** btreeInitPage() be called on every btree page so we make - ** the call for every page that comes in for re-initing. */ + ** the call for every page that comes in for re-initializing. */ btreeInitPage(pPage); } } @@ -2109,11 +2506,11 @@ static int btreeInvokeBusyHandler(void *pArg){ /* ** Open a database file. -** +** ** zFilename is the name of the database file. If zFilename is NULL ** then an ephemeral database is created. The ephemeral database might ** be exclusively in memory, or it might use a disk-based memory cache. -** Either way, the ephemeral database will be automatically deleted +** Either way, the ephemeral database will be automatically deleted ** when sqlite3BtreeClose() is called. ** ** If zFilename is ":memory:" then an in-memory database is created @@ -2146,7 +2543,7 @@ int sqlite3BtreeOpen( /* True if opening an ephemeral, temporary database */ const int isTempDb = zFilename==0 || zFilename[0]==0; - /* Set the variable isMemdb to true for an in-memory database, or + /* Set the variable isMemdb to true for an in-memory database, or ** false for a file-based database. */ #ifdef SQLITE_OMIT_MEMORYDB @@ -2176,7 +2573,7 @@ int sqlite3BtreeOpen( } p = sqlite3MallocZero(sizeof(Btree)); if( !p ){ - return SQLITE_NOMEM; + return SQLITE_NOMEM_BKPT; } p->inTrans = TRANS_NONE; p->db = db; @@ -2200,7 +2597,7 @@ int sqlite3BtreeOpen( p->sharable = 1; if( !zFullPathname ){ sqlite3_free(p); - return SQLITE_NOMEM; + return SQLITE_NOMEM_BKPT; } if( isMemdb ){ memcpy(zFullPathname, zFilename, nFilename); @@ -2208,15 +2605,19 @@ int sqlite3BtreeOpen( rc = sqlite3OsFullPathname(pVfs, zFilename, nFullPathname, zFullPathname); if( rc ){ - sqlite3_free(zFullPathname); - sqlite3_free(p); - return rc; + if( rc==SQLITE_OK_SYMLINK ){ + rc = SQLITE_OK; + }else{ + sqlite3_free(zFullPathname); + sqlite3_free(p); + return rc; + } } } #if SQLITE_THREADSAFE mutexOpen = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_OPEN); sqlite3_mutex_enter(mutexOpen); - mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); + mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); sqlite3_mutex_enter(mutexShared); #endif for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){ @@ -2265,14 +2666,17 @@ int sqlite3BtreeOpen( assert( sizeof(u32)==4 ); assert( sizeof(u16)==2 ); assert( sizeof(Pgno)==4 ); - + + /* Suppress false-positive compiler warning from PVS-Studio */ + memset(&zDbHeader[16], 0, 8); + pBt = sqlite3MallocZero( sizeof(*pBt) ); if( pBt==0 ){ - rc = SQLITE_NOMEM; + rc = SQLITE_NOMEM_BKPT; goto btree_open_out; } rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename, - EXTRA_SIZE, flags, vfsFlags, pageReinit); + sizeof(MemPage), flags, vfsFlags, pageReinit); if( rc==SQLITE_OK ){ sqlite3PagerSetMmapLimit(pBt->pPager, db->szMmap); rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader); @@ -2282,14 +2686,16 @@ int sqlite3BtreeOpen( } pBt->openFlags = (u8)flags; pBt->db = db; - sqlite3PagerSetBusyhandler(pBt->pPager, btreeInvokeBusyHandler, pBt); + sqlite3PagerSetBusyHandler(pBt->pPager, btreeInvokeBusyHandler, pBt); p->pBt = pBt; - + pBt->pCursor = 0; pBt->pPage1 = 0; if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY; -#ifdef SQLITE_SECURE_DELETE +#if defined(SQLITE_SECURE_DELETE) pBt->btsFlags |= BTS_SECURE_DELETE; +#elif defined(SQLITE_FAST_SECURE_DELETE) + pBt->btsFlags |= BTS_OVERWRITE; #endif /* EVIDENCE-OF: R-51873-39618 The page size for a database file is ** determined by the 2-byte integer located at an offset of 16 bytes from @@ -2326,18 +2732,18 @@ int sqlite3BtreeOpen( if( rc ) goto btree_open_out; pBt->usableSize = pBt->pageSize - nReserve; assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */ - + #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) /* Add the new BtShared object to the linked list sharable BtShareds. */ + pBt->nRef = 1; if( p->sharable ){ MUTEX_LOGIC( sqlite3_mutex *mutexShared; ) - pBt->nRef = 1; - MUTEX_LOGIC( mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);) + MUTEX_LOGIC( mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN);) if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){ pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST); if( pBt->mutex==0 ){ - rc = SQLITE_NOMEM; + rc = SQLITE_NOMEM_BKPT; goto btree_open_out; } } @@ -2360,12 +2766,12 @@ int sqlite3BtreeOpen( for(i=0; inDb; i++){ if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){ while( pSib->pPrev ){ pSib = pSib->pPrev; } - if( p->pBtpBt ){ + if( (uptr)p->pBt<(uptr)pSib->pBt ){ p->pNext = pSib; p->pPrev = 0; pSib->pPrev = p; }else{ - while( pSib->pNext && pSib->pNext->pBtpBt ){ + while( pSib->pNext && (uptr)pSib->pNext->pBt<(uptr)p->pBt ){ pSib = pSib->pNext; } p->pNext = pSib->pNext; @@ -2385,24 +2791,32 @@ int sqlite3BtreeOpen( btree_open_out: if( rc!=SQLITE_OK ){ if( pBt && pBt->pPager ){ - sqlite3PagerClose(pBt->pPager); + sqlite3PagerClose(pBt->pPager, 0); } sqlite3_free(pBt); sqlite3_free(p); *ppBtree = 0; }else{ + sqlite3_file *pFile; + /* If the B-Tree was successfully opened, set the pager-cache size to the ** default value. Except, when opening on an existing shared pager-cache, ** do not change the pager-cache size. */ if( sqlite3BtreeSchema(p, 0, 0)==0 ){ - sqlite3PagerSetCachesize(p->pBt->pPager, SQLITE_DEFAULT_CACHE_SIZE); + sqlite3BtreeSetCacheSize(p, SQLITE_DEFAULT_CACHE_SIZE); + } + + pFile = sqlite3PagerFile(pBt->pPager); + if( pFile->pMethods ){ + sqlite3OsFileControlHint(pFile, SQLITE_FCNTL_PDB, (void*)&pBt->db); } } if( mutexOpen ){ assert( sqlite3_mutex_held(mutexOpen) ); sqlite3_mutex_leave(mutexOpen); } + assert( rc!=SQLITE_OK || sqlite3BtreeConnectionCount(*ppBtree)>0 ); return rc; } @@ -2414,13 +2828,13 @@ int sqlite3BtreeOpen( */ static int removeFromSharingList(BtShared *pBt){ #ifndef SQLITE_OMIT_SHARED_CACHE - MUTEX_LOGIC( sqlite3_mutex *pMaster; ) + MUTEX_LOGIC( sqlite3_mutex *pMainMtx; ) BtShared *pList; int removed = 0; assert( sqlite3_mutex_notheld(pBt->mutex) ); - MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); ) - sqlite3_mutex_enter(pMaster); + MUTEX_LOGIC( pMainMtx = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); ) + sqlite3_mutex_enter(pMainMtx); pBt->nRef--; if( pBt->nRef<=0 ){ if( GLOBAL(BtShared*,sqlite3SharedCacheList)==pBt ){ @@ -2439,42 +2853,51 @@ static int removeFromSharingList(BtShared *pBt){ } removed = 1; } - sqlite3_mutex_leave(pMaster); + sqlite3_mutex_leave(pMainMtx); return removed; #else + UNUSED_PARAMETER( pBt ); return 1; #endif } /* -** Make sure pBt->pTmpSpace points to an allocation of +** Make sure pBt->pTmpSpace points to an allocation of ** MX_CELL_SIZE(pBt) bytes with a 4-byte prefix for a left-child ** pointer. */ -static void allocateTempSpace(BtShared *pBt){ - if( !pBt->pTmpSpace ){ - pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize ); - - /* One of the uses of pBt->pTmpSpace is to format cells before - ** inserting them into a leaf page (function fillInCell()). If - ** a cell is less than 4 bytes in size, it is rounded up to 4 bytes - ** by the various routines that manipulate binary cells. Which - ** can mean that fillInCell() only initializes the first 2 or 3 - ** bytes of pTmpSpace, but that the first 4 bytes are copied from - ** it into a database page. This is not actually a problem, but it - ** does cause a valgrind error when the 1 or 2 bytes of unitialized - ** data is passed to system call write(). So to avoid this error, - ** zero the first 4 bytes of temp space here. - ** - ** Also: Provide four bytes of initialized space before the - ** beginning of pTmpSpace as an area available to prepend the - ** left-child pointer to the beginning of a cell. - */ - if( pBt->pTmpSpace ){ - memset(pBt->pTmpSpace, 0, 8); - pBt->pTmpSpace += 4; - } - } +static SQLITE_NOINLINE int allocateTempSpace(BtShared *pBt){ + assert( pBt!=0 ); + assert( pBt->pTmpSpace==0 ); + /* This routine is called only by btreeCursor() when allocating the + ** first write cursor for the BtShared object */ + assert( pBt->pCursor!=0 && (pBt->pCursor->curFlags & BTCF_WriteFlag)!=0 ); + pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize ); + if( pBt->pTmpSpace==0 ){ + BtCursor *pCur = pBt->pCursor; + pBt->pCursor = pCur->pNext; /* Unlink the cursor */ + memset(pCur, 0, sizeof(*pCur)); + return SQLITE_NOMEM_BKPT; + } + + /* One of the uses of pBt->pTmpSpace is to format cells before + ** inserting them into a leaf page (function fillInCell()). If + ** a cell is less than 4 bytes in size, it is rounded up to 4 bytes + ** by the various routines that manipulate binary cells. Which + ** can mean that fillInCell() only initializes the first 2 or 3 + ** bytes of pTmpSpace, but that the first 4 bytes are copied from + ** it into a database page. This is not actually a problem, but it + ** does cause a valgrind error when the 1 or 2 bytes of uninitialized + ** data is passed to system call write(). So to avoid this error, + ** zero the first 4 bytes of temp space here. + ** + ** Also: Provide four bytes of initialized space before the + ** beginning of pTmpSpace as an area available to prepend the + ** left-child pointer to the beginning of a cell. + */ + memset(pBt->pTmpSpace, 0, 8); + pBt->pTmpSpace += 4; + return SQLITE_OK; } /* @@ -2493,19 +2916,23 @@ static void freeTempSpace(BtShared *pBt){ */ int sqlite3BtreeClose(Btree *p){ BtShared *pBt = p->pBt; - BtCursor *pCur; /* Close all cursors opened via this handle. */ assert( sqlite3_mutex_held(p->db->mutex) ); sqlite3BtreeEnter(p); - pCur = pBt->pCursor; - while( pCur ){ - BtCursor *pTmp = pCur; - pCur = pCur->pNext; - if( pTmp->pBtree==p ){ - sqlite3BtreeCloseCursor(pTmp); + + /* Verify that no other cursors have this Btree open */ +#ifdef SQLITE_DEBUG + { + BtCursor *pCur = pBt->pCursor; + while( pCur ){ + BtCursor *pTmp = pCur; + pCur = pCur->pNext; + assert( pTmp->pBtree!=p ); + } } +#endif /* Rollback any active transaction and free the handle structure. ** The call to sqlite3BtreeRollback() drops any table-locks held by @@ -2515,7 +2942,7 @@ int sqlite3BtreeClose(Btree *p){ sqlite3BtreeLeave(p); /* If there are still other outstanding references to the shared-btree - ** structure, return now. The remainder of this procedure cleans + ** structure, return now. The remainder of this procedure cleans ** up the shared-btree. */ assert( p->wantToLock==0 && p->locked==0 ); @@ -2526,7 +2953,7 @@ int sqlite3BtreeClose(Btree *p){ ** Clean out and delete the BtShared object. */ assert( !pBt->pCursor ); - sqlite3PagerClose(pBt->pPager); + sqlite3PagerClose(pBt->pPager, p->db); if( pBt->xFreeSchema && pBt->pSchema ){ pBt->xFreeSchema(pBt->pSchema); } @@ -2619,24 +3046,9 @@ int sqlite3BtreeSetPagerFlags( } #endif -/* -** Return TRUE if the given btree is set to safety level 1. In other -** words, return TRUE if no sync() occurs on the disk files. -*/ -int sqlite3BtreeSyncDisabled(Btree *p){ - BtShared *pBt = p->pBt; - int rc; - assert( sqlite3_mutex_held(p->db->mutex) ); - sqlite3BtreeEnter(p); - assert( pBt && pBt->pPager ); - rc = sqlite3PagerNosync(pBt->pPager); - sqlite3BtreeLeave(p); - return rc; -} - /* ** Change the default pages size and the number of reserved bytes per page. -** Or, if the page size has already been fixed, return SQLITE_READONLY +** Or, if the page size has already been fixed, return SQLITE_READONLY ** without changing anything. ** ** The page size must be a power of 2 between 512 and 65536. If the page @@ -2656,24 +3068,27 @@ int sqlite3BtreeSyncDisabled(Btree *p){ */ int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve, int iFix){ int rc = SQLITE_OK; + int x; BtShared *pBt = p->pBt; - assert( nReserve>=-1 && nReserve<=255 ); + assert( nReserve>=0 && nReserve<=255 ); sqlite3BtreeEnter(p); -#if SQLITE_HAS_CODEC - if( nReserve>pBt->optimalReserve ) pBt->optimalReserve = (u8)nReserve; -#endif + pBt->nReserveWanted = (u8)nReserve; + x = pBt->pageSize - pBt->usableSize; + if( x==nReserve && (pageSize==0 || (u32)pageSize==pBt->pageSize) ){ + sqlite3BtreeLeave(p); + return SQLITE_OK; + } + if( nReservebtsFlags & BTS_PAGESIZE_FIXED ){ sqlite3BtreeLeave(p); return SQLITE_READONLY; } - if( nReserve<0 ){ - nReserve = pBt->pageSize - pBt->usableSize; - } assert( nReserve>=0 && nReserve<=255 ); if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE && ((pageSize-1)&pageSize)==0 ){ assert( (pageSize & 7)==0 ); assert( !pBt->pCursor ); + if( nReserve>32 && pageSize==512 ) pageSize = 1024; pBt->pageSize = (u32)pageSize; freeTempSpace(pBt); } @@ -2697,7 +3112,7 @@ int sqlite3BtreeGetPageSize(Btree *p){ ** held. ** ** This is useful in one special case in the backup API code where it is -** known that the shared b-tree mutex is held, but the mutex on the +** known that the shared b-tree mutex is held, but the mutex on the ** database handle that owns *p is not. In this case if sqlite3BtreeEnter() ** were to be called, it might collide with some other operation on the ** database handle that owns *p, causing undefined behavior. @@ -2711,22 +3126,20 @@ int sqlite3BtreeGetReserveNoMutex(Btree *p){ /* ** Return the number of bytes of space at the end of every page that -** are intentually left unused. This is the "reserved" space that is +** are intentionally left unused. This is the "reserved" space that is ** sometimes used by extensions. ** -** If SQLITE_HAS_MUTEX is defined then the number returned is the -** greater of the current reserved space and the maximum requested -** reserve space. +** The value returned is the larger of the current reserve size and +** the latest reserve size requested by SQLITE_FILECTRL_RESERVE_BYTES. +** The amount of reserve can only grow - never shrink. */ -int sqlite3BtreeGetOptimalReserve(Btree *p){ - int n; +int sqlite3BtreeGetRequestedReserve(Btree *p){ + int n1, n2; sqlite3BtreeEnter(p); - n = sqlite3BtreeGetReserveNoMutex(p); -#ifdef SQLITE_HAS_CODEC - if( npBt->optimalReserve ) n = p->pBt->optimalReserve; -#endif + n1 = (int)p->pBt->nReserveWanted; + n2 = sqlite3BtreeGetReserveNoMutex(p); sqlite3BtreeLeave(p); - return n; + return n1>n2 ? n1 : n2; } @@ -2735,8 +3148,8 @@ int sqlite3BtreeGetOptimalReserve(Btree *p){ ** No changes are made if mxPage is 0 or negative. ** Regardless of the value of mxPage, return the maximum page count. */ -int sqlite3BtreeMaxPageCount(Btree *p, int mxPage){ - int n; +Pgno sqlite3BtreeMaxPageCount(Btree *p, Pgno mxPage){ + Pgno n; sqlite3BtreeEnter(p); n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage); sqlite3BtreeLeave(p); @@ -2744,19 +3157,34 @@ int sqlite3BtreeMaxPageCount(Btree *p, int mxPage){ } /* -** Set the BTS_SECURE_DELETE flag if newFlag is 0 or 1. If newFlag is -1, -** then make no changes. Always return the value of the BTS_SECURE_DELETE -** setting after the change. +** Change the values for the BTS_SECURE_DELETE and BTS_OVERWRITE flags: +** +** newFlag==0 Both BTS_SECURE_DELETE and BTS_OVERWRITE are cleared +** newFlag==1 BTS_SECURE_DELETE set and BTS_OVERWRITE is cleared +** newFlag==2 BTS_SECURE_DELETE cleared and BTS_OVERWRITE is set +** newFlag==(-1) No changes +** +** This routine acts as a query if newFlag is less than zero +** +** With BTS_OVERWRITE set, deleted content is overwritten by zeros, but +** freelist leaf pages are not written back to the database. Thus in-page +** deleted content is cleared, but freelist deleted content is not. +** +** With BTS_SECURE_DELETE, operation is like BTS_OVERWRITE with the addition +** that freelist leaf pages are written back into the database, increasing +** the amount of disk I/O. */ int sqlite3BtreeSecureDelete(Btree *p, int newFlag){ int b; if( p==0 ) return 0; sqlite3BtreeEnter(p); + assert( BTS_OVERWRITE==BTS_SECURE_DELETE*2 ); + assert( BTS_FAST_SECURE==(BTS_OVERWRITE|BTS_SECURE_DELETE) ); if( newFlag>=0 ){ - p->pBt->btsFlags &= ~BTS_SECURE_DELETE; - if( newFlag ) p->pBt->btsFlags |= BTS_SECURE_DELETE; - } - b = (p->pBt->btsFlags & BTS_SECURE_DELETE)!=0; + p->pBt->btsFlags &= ~BTS_FAST_SECURE; + p->pBt->btsFlags |= (u16)(BTS_SECURE_DELETE*newFlag); + } + b = (p->pBt->btsFlags & BTS_FAST_SECURE)/BTS_SECURE_DELETE; sqlite3BtreeLeave(p); return b; } @@ -2764,7 +3192,7 @@ int sqlite3BtreeSecureDelete(Btree *p, int newFlag){ /* ** Change the 'auto-vacuum' property of the database. If the 'autoVacuum' ** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it -** is disabled. The default value for the auto-vacuum property is +** is disabled. The default value for the auto-vacuum property is ** determined by the SQLITE_DEFAULT_AUTOVACUUM macro. */ int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){ @@ -2788,7 +3216,7 @@ int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){ } /* -** Return the value of the 'auto-vacuum' property. If auto-vacuum is +** Return the value of the 'auto-vacuum' property. If auto-vacuum is ** enabled 1 is returned. Otherwise 0. */ int sqlite3BtreeGetAutoVacuum(Btree *p){ @@ -2807,6 +3235,36 @@ int sqlite3BtreeGetAutoVacuum(Btree *p){ #endif } +/* +** If the user has not set the safety-level for this database connection +** using "PRAGMA synchronous", and if the safety-level is not already +** set to the value passed to this function as the second parameter, +** set it so. +*/ +#if SQLITE_DEFAULT_SYNCHRONOUS!=SQLITE_DEFAULT_WAL_SYNCHRONOUS \ + && !defined(SQLITE_OMIT_WAL) +static void setDefaultSyncFlag(BtShared *pBt, u8 safety_level){ + sqlite3 *db; + Db *pDb; + if( (db=pBt->db)!=0 && (pDb=db->aDb)!=0 ){ + while( pDb->pBt==0 || pDb->pBt->pBt!=pBt ){ pDb++; } + if( pDb->bSyncSet==0 + && pDb->safety_level!=safety_level + && pDb!=&db->aDb[1] + ){ + pDb->safety_level = safety_level; + sqlite3PagerSetFlags(pBt->pPager, + pDb->safety_level | (db->flags & PAGER_FLAGS_MASK)); + } + } +} +#else +# define setDefaultSyncFlag(pBt,safety_level) +#endif + +/* Forward declaration */ +static int newDatabase(BtShared*); + /* ** Get a reference to pPage1 of the database file. This will @@ -2815,14 +3273,13 @@ int sqlite3BtreeGetAutoVacuum(Btree *p){ ** SQLITE_OK is returned on success. If the file is not a ** well-formed database file, then SQLITE_CORRUPT is returned. ** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM -** is returned if we run out of memory. +** is returned if we run out of memory. */ static int lockBtree(BtShared *pBt){ int rc; /* Result code from subfunctions */ MemPage *pPage1; /* Page 1 of the database file */ - int nPage; /* Number of pages in the database */ - int nPageFile = 0; /* Number of pages in the database file */ - int nPageHeader; /* Number of pages in the database according to hdr */ + u32 nPage; /* Number of pages in the database */ + u32 nPageFile = 0; /* Number of pages in the database file */ assert( sqlite3_mutex_held(pBt->mutex) ); assert( pBt->pPage1==0 ); @@ -2832,13 +3289,16 @@ static int lockBtree(BtShared *pBt){ if( rc!=SQLITE_OK ) return rc; /* Do some checking to help insure the file we opened really is - ** a valid database file. + ** a valid database file. */ - nPage = nPageHeader = get4byte(28+(u8*)pPage1->aData); - sqlite3PagerPagecount(pBt->pPager, &nPageFile); + nPage = get4byte(28+(u8*)pPage1->aData); + sqlite3PagerPagecount(pBt->pPager, (int*)&nPageFile); if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){ nPage = nPageFile; } + if( (pBt->db->flags & SQLITE_ResetDatabase)!=0 ){ + nPage = 0; + } if( nPage>0 ){ u32 pageSize; u32 usableSize; @@ -2866,8 +3326,8 @@ static int lockBtree(BtShared *pBt){ goto page1_init_failed; } - /* If the write version is set to 2, this database should be accessed - ** in WAL mode. If the log is not already open, open it now. Then + /* If the read version is set to 2, this database should be accessed + ** in WAL mode. If the log is not already open, open it now. Then ** return SQLITE_OK and return without populating BtShared.pPage1. ** The caller detects this and calls this function again. This is ** required as the version of page 1 currently in the page1 buffer @@ -2879,11 +3339,16 @@ static int lockBtree(BtShared *pBt){ rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen); if( rc!=SQLITE_OK ){ goto page1_init_failed; - }else if( isOpen==0 ){ - releasePage(pPage1); - return SQLITE_OK; + }else{ + setDefaultSyncFlag(pBt, SQLITE_DEFAULT_WAL_SYNCHRONOUS+1); + if( isOpen==0 ){ + releasePageOne(pPage1); + return SQLITE_OK; + } } rc = SQLITE_NOTADB; + }else{ + setDefaultSyncFlag(pBt, SQLITE_DEFAULT_SYNCHRONOUS+1); } #endif @@ -2903,15 +3368,15 @@ static int lockBtree(BtShared *pBt){ /* EVIDENCE-OF: R-25008-21688 The size of a page is a power of two ** between 512 and 65536 inclusive. */ if( ((pageSize-1)&pageSize)!=0 - || pageSize>SQLITE_MAX_PAGE_SIZE - || pageSize<=256 + || pageSize>SQLITE_MAX_PAGE_SIZE + || pageSize<=256 ){ goto page1_init_failed; } assert( (pageSize & 7)==0 ); /* EVIDENCE-OF: R-59310-51205 The "reserved space" size in the 1-byte ** integer at offset 20 is the number of bytes of space at the end of - ** each page to reserve for extensions. + ** each page to reserve for extensions. ** ** EVIDENCE-OF: R-37497-42412 The size of the reserved region is ** determined by the one-byte unsigned integer found at an offset of 20 @@ -2924,17 +3389,22 @@ static int lockBtree(BtShared *pBt){ ** zero and return SQLITE_OK. The caller will call this function ** again with the correct page-size. */ - releasePage(pPage1); + releasePageOne(pPage1); pBt->usableSize = usableSize; pBt->pageSize = pageSize; + pBt->btsFlags |= BTS_PAGESIZE_FIXED; freeTempSpace(pBt); rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, pageSize-usableSize); return rc; } - if( (pBt->db->flags & SQLITE_RecoveryMode)==0 && nPage>nPageFile ){ - rc = SQLITE_CORRUPT_BKPT; - goto page1_init_failed; + if( nPage>nPageFile ){ + if( sqlite3WritableSchema(pBt->db)==0 ){ + rc = SQLITE_CORRUPT_BKPT; + goto page1_init_failed; + }else{ + nPage = nPageFile; + } } /* EVIDENCE-OF: R-28312-64704 However, the usable size is not allowed to ** be less than 480. In other words, if the page size is 512, then the @@ -2942,6 +3412,7 @@ static int lockBtree(BtShared *pBt){ if( usableSize<480 ){ goto page1_init_failed; } + pBt->btsFlags |= BTS_PAGESIZE_FIXED; pBt->pageSize = pageSize; pBt->usableSize = usableSize; #ifndef SQLITE_OMIT_AUTOVACUUM @@ -2978,7 +3449,7 @@ static int lockBtree(BtShared *pBt){ return SQLITE_OK; page1_init_failed: - releasePage(pPage1); + releasePageOne(pPage1); pBt->pPage1 = 0; return rc; } @@ -3001,7 +3472,7 @@ static int countValidCursors(BtShared *pBt, int wrOnly){ int r = 0; for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ if( (wrOnly==0 || (pCur->curFlags & BTCF_WriteFlag)!=0) - && pCur->eState!=CURSOR_FAULT ) r++; + && pCur->eState!=CURSOR_FAULT ) r++; } return r; } @@ -3010,7 +3481,7 @@ static int countValidCursors(BtShared *pBt, int wrOnly){ /* ** If there are no outstanding cursors and we are not in the middle ** of a transaction but there is a read lock on the database, then -** this routine unrefs the first page of the database file which +** this routine unrefs the first page of the database file which ** has the effect of releasing the read lock. ** ** If there is a transaction in progress, this routine is a no-op. @@ -3023,7 +3494,7 @@ static void unlockBtreeIfUnused(BtShared *pBt){ assert( pPage1->aData ); assert( sqlite3PagerRefcount(pBt->pPager)==1 ); pBt->pPage1 = 0; - releasePageNotNull(pPage1); + releasePageOne(pPage1); } } @@ -3094,8 +3565,8 @@ int sqlite3BtreeNewDb(Btree *p){ ** upgraded to exclusive by calling this routine a second time - the ** exclusivity flag only works for a new transaction. ** -** A write-transaction must be started before attempting any -** changes to the database. None of the following routines +** A write-transaction must be started before attempting any +** changes to the database. None of the following routines ** will work unless a transaction is started first: ** ** sqlite3BtreeCreateTable() @@ -3109,7 +3580,7 @@ int sqlite3BtreeNewDb(Btree *p){ ** If an initial attempt to acquire the lock fails because of lock contention ** and the database was previously unlocked, then invoke the busy handler ** if there is one. But if there was previously a read-lock, do not -** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is +** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is ** returned when there is already a read-lock in order to avoid a deadlock. ** ** Suppose there are two processes A and B. A has a read lock and B has @@ -3120,8 +3591,13 @@ int sqlite3BtreeNewDb(Btree *p){ ** when A already has a read lock, we encourage A to give up and let B ** proceed. */ -int sqlite3BtreeBeginTrans(Btree *p, int wrflag){ +static SQLITE_NOINLINE int btreeBeginTrans( + Btree *p, /* The btree in which to start the transaction */ + int wrflag, /* True to start a write transaction */ + int *pSchemaVersion /* Put schema version number here, if not NULL */ +){ BtShared *pBt = p->pBt; + Pager *pPager = pBt->pPager; int rc = SQLITE_OK; sqlite3BtreeEnter(p); @@ -3136,6 +3612,12 @@ int sqlite3BtreeBeginTrans(Btree *p, int wrflag){ } assert( pBt->inTransaction==TRANS_WRITE || IfNotOmitAV(pBt->bDoTruncate)==0 ); + if( (p->db->flags & SQLITE_ResetDatabase) + && sqlite3PagerIsreadonly(pPager)==0 + ){ + pBt->btsFlags &= ~BTS_READ_ONLY; + } + /* Write transactions are not possible on a read-only database */ if( (pBt->btsFlags & BTS_READ_ONLY)!=0 && wrflag ){ rc = SQLITE_READONLY; @@ -3145,7 +3627,7 @@ int sqlite3BtreeBeginTrans(Btree *p, int wrflag){ #ifndef SQLITE_OMIT_SHARED_CACHE { sqlite3 *pBlock = 0; - /* If another database handle has already opened a write transaction + /* If another database handle has already opened a write transaction ** on this shared-btree structure and a second write transaction is ** requested, return SQLITE_LOCKED. */ @@ -3170,19 +3652,31 @@ int sqlite3BtreeBeginTrans(Btree *p, int wrflag){ } #endif - /* Any read-only or read-write transaction implies a read-lock on - ** page 1. So if some other shared-cache client already has a write-lock + /* Any read-only or read-write transaction implies a read-lock on + ** page 1. So if some other shared-cache client already has a write-lock ** on page 1, the transaction cannot be opened. */ - rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK); + rc = querySharedCacheTableLock(p, SCHEMA_ROOT, READ_LOCK); if( SQLITE_OK!=rc ) goto trans_begun; pBt->btsFlags &= ~BTS_INITIALLY_EMPTY; if( pBt->nPage==0 ) pBt->btsFlags |= BTS_INITIALLY_EMPTY; do { + sqlite3PagerWalDb(pPager, p->db); + +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT + /* If transitioning from no transaction directly to a write transaction, + ** block for the WRITER lock first if possible. */ + if( pBt->pPage1==0 && wrflag ){ + assert( pBt->inTransaction==TRANS_NONE ); + rc = sqlite3PagerWalWriteLock(pPager, 1); + if( rc!=SQLITE_BUSY && rc!=SQLITE_OK ) break; + } +#endif + /* Call lockBtree() until either pBt->pPage1 is populated or ** lockBtree() returns something other than SQLITE_OK. lockBtree() ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after - ** reading page 1 it discovers that the page-size of the database + ** reading page 1 it discovers that the page-size of the database ** file is not pBt->pageSize. In this case lockBtree() will update ** pBt->pageSize to the page-size of the file on disk. */ @@ -3192,18 +3686,35 @@ int sqlite3BtreeBeginTrans(Btree *p, int wrflag){ if( (pBt->btsFlags & BTS_READ_ONLY)!=0 ){ rc = SQLITE_READONLY; }else{ - rc = sqlite3PagerBegin(pBt->pPager,wrflag>1,sqlite3TempInMemory(p->db)); + rc = sqlite3PagerBegin(pPager, wrflag>1, sqlite3TempInMemory(p->db)); if( rc==SQLITE_OK ){ rc = newDatabase(pBt); + }else if( rc==SQLITE_BUSY_SNAPSHOT && pBt->inTransaction==TRANS_NONE ){ + /* if there was no transaction opened when this function was + ** called and SQLITE_BUSY_SNAPSHOT is returned, change the error + ** code to SQLITE_BUSY. */ + rc = SQLITE_BUSY; } } } - + if( rc!=SQLITE_OK ){ + (void)sqlite3PagerWalWriteLock(pPager, 0); unlockBtreeIfUnused(pBt); } +#if defined(SQLITE_ENABLE_SETLK_TIMEOUT) + if( rc==SQLITE_BUSY_TIMEOUT ){ + /* If a blocking lock timed out, break out of the loop here so that + ** the busy-handler is not invoked. */ + break; + } +#endif }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE && btreeInvokeBusyHandler(pBt) ); + sqlite3PagerWalDb(pPager, 0); +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT + if( rc==SQLITE_BUSY_TIMEOUT ) rc = SQLITE_BUSY; +#endif if( rc==SQLITE_OK ){ if( p->inTrans==TRANS_NONE ){ @@ -3232,7 +3743,7 @@ int sqlite3BtreeBeginTrans(Btree *p, int wrflag){ /* If the db-size header field is incorrect (as it may be if an old ** client has been writing the database file), update it now. Doing - ** this sooner rather than later means the database size can safely + ** this sooner rather than later means the database size can safely ** re-read the database size from page 1 if a savepoint or transaction ** rollback occurs within the transaction. */ @@ -3245,20 +3756,46 @@ int sqlite3BtreeBeginTrans(Btree *p, int wrflag){ } } - trans_begun: - if( rc==SQLITE_OK && wrflag ){ - /* This call makes sure that the pager has the correct number of - ** open savepoints. If the second parameter is greater than 0 and - ** the sub-journal is not already open, then it will be opened here. - */ - rc = sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint); + if( rc==SQLITE_OK ){ + if( pSchemaVersion ){ + *pSchemaVersion = get4byte(&pBt->pPage1->aData[40]); + } + if( wrflag ){ + /* This call makes sure that the pager has the correct number of + ** open savepoints. If the second parameter is greater than 0 and + ** the sub-journal is not already open, then it will be opened here. + */ + rc = sqlite3PagerOpenSavepoint(pPager, p->db->nSavepoint); + } } btreeIntegrity(p); sqlite3BtreeLeave(p); return rc; } +int sqlite3BtreeBeginTrans(Btree *p, int wrflag, int *pSchemaVersion){ + BtShared *pBt; + if( p->sharable + || p->inTrans==TRANS_NONE + || (p->inTrans==TRANS_READ && wrflag!=0) + ){ + return btreeBeginTrans(p,wrflag,pSchemaVersion); + } + pBt = p->pBt; + if( pSchemaVersion ){ + *pSchemaVersion = get4byte(&pBt->pPage1->aData[40]); + } + if( wrflag ){ + /* This call makes sure that the pager has the correct number of + ** open savepoints. If the second parameter is greater than 0 and + ** the sub-journal is not already open, then it will be opened here. + */ + return sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint); + }else{ + return SQLITE_OK; + } +} #ifndef SQLITE_OMIT_AUTOVACUUM @@ -3272,20 +3809,17 @@ static int setChildPtrmaps(MemPage *pPage){ int nCell; /* Number of cells in page pPage */ int rc; /* Return code */ BtShared *pBt = pPage->pBt; - u8 isInitOrig = pPage->isInit; Pgno pgno = pPage->pgno; assert( sqlite3_mutex_held(pPage->pBt->mutex) ); - rc = btreeInitPage(pPage); - if( rc!=SQLITE_OK ){ - goto set_child_ptrmaps_out; - } + rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage); + if( rc!=SQLITE_OK ) return rc; nCell = pPage->nCell; for(i=0; ileaf ){ Pgno childPgno = get4byte(pCell); @@ -3298,8 +3832,6 @@ static int setChildPtrmaps(MemPage *pPage){ ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc); } -set_child_ptrmaps_out: - pPage->isInit = isInitOrig; return rc; } @@ -3308,7 +3840,7 @@ static int setChildPtrmaps(MemPage *pPage){ ** that it points to iTo. Parameter eType describes the type of pointer to ** be modified, as follows: ** -** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child +** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child ** page of pPage. ** ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow @@ -3323,16 +3855,15 @@ static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){ if( eType==PTRMAP_OVERFLOW2 ){ /* The pointer is always the first 4 bytes of the page in this case. */ if( get4byte(pPage->aData)!=iFrom ){ - return SQLITE_CORRUPT_BKPT; + return SQLITE_CORRUPT_PAGE(pPage); } put4byte(pPage->aData, iTo); }else{ - u8 isInitOrig = pPage->isInit; int i; int nCell; int rc; - rc = btreeInitPage(pPage); + rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage); if( rc ) return rc; nCell = pPage->nCell; @@ -3341,41 +3872,44 @@ static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){ if( eType==PTRMAP_OVERFLOW1 ){ CellInfo info; pPage->xParseCell(pPage, pCell, &info); - if( info.nLocalaData+pPage->maskPage - && iFrom==get4byte(pCell+info.nSize-4) - ){ - put4byte(pCell+info.nSize-4, iTo); - break; + if( info.nLocal pPage->aData+pPage->pBt->usableSize ){ + return SQLITE_CORRUPT_PAGE(pPage); + } + if( iFrom==get4byte(pCell+info.nSize-4) ){ + put4byte(pCell+info.nSize-4, iTo); + break; + } } }else{ + if( pCell+4 > pPage->aData+pPage->pBt->usableSize ){ + return SQLITE_CORRUPT_PAGE(pPage); + } if( get4byte(pCell)==iFrom ){ put4byte(pCell, iTo); break; } } } - + if( i==nCell ){ - if( eType!=PTRMAP_BTREE || + if( eType!=PTRMAP_BTREE || get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){ - return SQLITE_CORRUPT_BKPT; + return SQLITE_CORRUPT_PAGE(pPage); } put4byte(&pPage->aData[pPage->hdrOffset+8], iTo); } - - pPage->isInit = isInitOrig; } return SQLITE_OK; } /* -** Move the open database page pDbPage to location iFreePage in the +** Move the open database page pDbPage to location iFreePage in the ** database. The pDbPage reference remains valid. ** ** The isCommit flag indicates that there is no need to remember that -** the journal needs to be sync()ed before database page pDbPage->pgno +** the journal needs to be sync()ed before database page pDbPage->pgno ** can be written to. The caller has already promised not to write to that ** page. */ @@ -3392,13 +3926,14 @@ static int relocatePage( Pager *pPager = pBt->pPager; int rc; - assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 || + assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 || eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ); assert( sqlite3_mutex_held(pBt->mutex) ); assert( pDbPage->pBt==pBt ); + if( iDbPage<3 ) return SQLITE_CORRUPT_BKPT; /* Move page iDbPage from its current location to page number iFreePage */ - TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n", + TRACE(("AUTOVACUUM: Moving %u to free page %u (ptr page %u type %u)\n", iDbPage, iFreePage, iPtrPage, eType)); rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit); if( rc!=SQLITE_OK ){ @@ -3457,19 +3992,19 @@ static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8); /* ** Perform a single step of an incremental-vacuum. If successful, return -** SQLITE_OK. If there is no work to do (and therefore no point in -** calling this function again), return SQLITE_DONE. Or, if an error +** SQLITE_OK. If there is no work to do (and therefore no point in +** calling this function again), return SQLITE_DONE. Or, if an error ** occurs, return some other error code. ** -** More specifically, this function attempts to re-organize the database so +** More specifically, this function attempts to re-organize the database so ** that the last page of the file currently in use is no longer in use. ** ** Parameter nFin is the number of pages that this database would contain ** were this function called until it returns SQLITE_DONE. ** -** If the bCommit parameter is non-zero, this function assumes that the -** caller will keep calling incrVacuumStep() until it returns SQLITE_DONE -** or an error. bCommit is passed true for an auto-vacuum-on-commit +** If the bCommit parameter is non-zero, this function assumes that the +** caller will keep calling incrVacuumStep() until it returns SQLITE_DONE +** or an error. bCommit is passed true for an auto-vacuum-on-commit ** operation, or false for an incremental vacuum. */ static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg, int bCommit){ @@ -3500,7 +4035,7 @@ static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg, int bCommit){ if( bCommit==0 ){ /* Remove the page from the files free-list. This is not required ** if bCommit is non-zero. In that case, the free-list will be - ** truncated to zero after this function returns, so it doesn't + ** truncated to zero after this function returns, so it doesn't ** matter if it still contains some garbage entries. */ Pgno iFreePg; @@ -3536,15 +4071,20 @@ static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg, int bCommit){ } do { MemPage *pFreePg; + Pgno dbSize = btreePagecount(pBt); rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iNear, eMode); if( rc!=SQLITE_OK ){ releasePage(pLastPg); return rc; } releasePage(pFreePg); + if( iFreePg>dbSize ){ + releasePage(pLastPg); + return SQLITE_CORRUPT_BKPT; + } }while( bCommit && iFreePg>nFin ); assert( iFreePgpPage1->aData[36]); Pgno nFin = finalDbSize(pBt, nOrig, nFree); - if( nOrig=nOrig ){ rc = SQLITE_CORRUPT_BKPT; }else if( nFree>0 ){ rc = saveAllCursors(pBt, 0, 0); @@ -3630,16 +4170,18 @@ int sqlite3BtreeIncrVacuum(Btree *p){ /* ** This routine is called prior to sqlite3PagerCommit when a transaction ** is committed for an auto-vacuum database. -** -** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages -** the database file should be truncated to during the commit process. -** i.e. the database has been reorganized so that only the first *pnTrunc -** pages are in use. */ -static int autoVacuumCommit(BtShared *pBt){ +static int autoVacuumCommit(Btree *p){ int rc = SQLITE_OK; - Pager *pPager = pBt->pPager; - VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager); ) + Pager *pPager; + BtShared *pBt; + sqlite3 *db; + VVA_ONLY( int nRef ); + + assert( p!=0 ); + pBt = p->pBt; + pPager = pBt->pPager; + VVA_ONLY( nRef = sqlite3PagerRefcount(pPager); ) assert( sqlite3_mutex_held(pBt->mutex) ); invalidateAllOverflowCache(pBt); @@ -3647,6 +4189,7 @@ static int autoVacuumCommit(BtShared *pBt){ if( !pBt->incrVacuum ){ Pgno nFin; /* Number of pages in database after autovacuuming */ Pgno nFree; /* Number of pages on the freelist initially */ + Pgno nVac; /* Number of pages to vacuum */ Pgno iFree; /* The next page to be freed */ Pgno nOrig; /* Database size before freeing */ @@ -3660,18 +4203,42 @@ static int autoVacuumCommit(BtShared *pBt){ } nFree = get4byte(&pBt->pPage1->aData[36]); - nFin = finalDbSize(pBt, nOrig, nFree); + db = p->db; + if( db->xAutovacPages ){ + int iDb; + for(iDb=0; ALWAYS(iDbnDb); iDb++){ + if( db->aDb[iDb].pBt==p ) break; + } + nVac = db->xAutovacPages( + db->pAutovacPagesArg, + db->aDb[iDb].zDbSName, + nOrig, + nFree, + pBt->pageSize + ); + if( nVac>nFree ){ + nVac = nFree; + } + if( nVac==0 ){ + return SQLITE_OK; + } + }else{ + nVac = nFree; + } + nFin = finalDbSize(pBt, nOrig, nVac); if( nFin>nOrig ) return SQLITE_CORRUPT_BKPT; if( nFinnFin && rc==SQLITE_OK; iFree--){ - rc = incrVacuumStep(pBt, nFin, iFree, 1); + rc = incrVacuumStep(pBt, nFin, iFree, nVac==nFree); } if( (rc==SQLITE_DONE || rc==SQLITE_OK) && nFree>0 ){ rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); - put4byte(&pBt->pPage1->aData[32], 0); - put4byte(&pBt->pPage1->aData[36], 0); + if( nVac==nFree ){ + put4byte(&pBt->pPage1->aData[32], 0); + put4byte(&pBt->pPage1->aData[36], 0); + } put4byte(&pBt->pPage1->aData[28], nFin); pBt->bDoTruncate = 1; pBt->nPage = nFin; @@ -3704,25 +4271,25 @@ static int autoVacuumCommit(BtShared *pBt){ ** ** This call is a no-op if no write-transaction is currently active on pBt. ** -** Otherwise, sync the database file for the btree pBt. zMaster points to -** the name of a master journal file that should be written into the -** individual journal file, or is NULL, indicating no master journal file +** Otherwise, sync the database file for the btree pBt. zSuperJrnl points to +** the name of a super-journal file that should be written into the +** individual journal file, or is NULL, indicating no super-journal file ** (single database transaction). ** -** When this is called, the master journal should already have been +** When this is called, the super-journal should already have been ** created, populated with this journal pointer and synced to disk. ** ** Once this is routine has returned, the only thing required to commit ** the write-transaction for this database file is to delete the journal. */ -int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zMaster){ +int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zSuperJrnl){ int rc = SQLITE_OK; if( p->inTrans==TRANS_WRITE ){ BtShared *pBt = p->pBt; sqlite3BtreeEnter(p); #ifndef SQLITE_OMIT_AUTOVACUUM if( pBt->autoVacuum ){ - rc = autoVacuumCommit(pBt); + rc = autoVacuumCommit(p); if( rc!=SQLITE_OK ){ sqlite3BtreeLeave(p); return rc; @@ -3732,7 +4299,7 @@ int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zMaster){ sqlite3PagerTruncateImage(pBt->pPager, pBt->nPage); } #endif - rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, 0); + rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zSuperJrnl, 0); sqlite3BtreeLeave(p); } return rc; @@ -3757,8 +4324,8 @@ static void btreeEndTransaction(Btree *p){ downgradeAllSharedCacheTableLocks(p); p->inTrans = TRANS_READ; }else{ - /* If the handle had any kind of transaction open, decrement the - ** transaction count of the shared btree. If the transaction count + /* If the handle had any kind of transaction open, decrement the + ** transaction count of the shared btree. If the transaction count ** reaches 0, set the shared state to TRANS_NONE. The unlockBtreeIfUnused() ** call below will unlock the pager. */ if( p->inTrans!=TRANS_NONE ){ @@ -3769,7 +4336,7 @@ static void btreeEndTransaction(Btree *p){ } } - /* Set the current transaction state to TRANS_NONE and unlock the + /* Set the current transaction state to TRANS_NONE and unlock the ** pager if this call closed the only read or write transaction. */ p->inTrans = TRANS_NONE; unlockBtreeIfUnused(pBt); @@ -3790,12 +4357,12 @@ static void btreeEndTransaction(Btree *p){ ** the rollback journal (which causes the transaction to commit) and ** drop locks. ** -** Normally, if an error occurs while the pager layer is attempting to +** Normally, if an error occurs while the pager layer is attempting to ** finalize the underlying journal file, this function returns an error and ** the upper layer will attempt a rollback. However, if the second argument -** is non-zero then this b-tree transaction is part of a multi-file -** transaction. In this case, the transaction has already been committed -** (by deleting a master journal file) and the caller will ignore this +** is non-zero then this b-tree transaction is part of a multi-file +** transaction. In this case, the transaction has already been committed +** (by deleting a super-journal file) and the caller will ignore this ** functions return code. So, even if an error occurs in the pager layer, ** reset the b-tree objects internal state to indicate that the write ** transaction has been closed. This is quite safe, as the pager will have @@ -3810,7 +4377,7 @@ int sqlite3BtreeCommitPhaseTwo(Btree *p, int bCleanup){ sqlite3BtreeEnter(p); btreeIntegrity(p); - /* If the handle has a write-transaction open, commit the shared-btrees + /* If the handle has a write-transaction open, commit the shared-btrees ** transaction and set the shared state to TRANS_READ. */ if( p->inTrans==TRANS_WRITE ){ @@ -3823,7 +4390,7 @@ int sqlite3BtreeCommitPhaseTwo(Btree *p, int bCleanup){ sqlite3BtreeLeave(p); return rc; } - p->iDataVersion--; /* Compensate for pPager->iDataVersion++; */ + p->iBDataVersion--; /* Compensate for pPager->iDataVersion++; */ pBt->inTransaction = TRANS_READ; btreeClearHasContent(pBt); } @@ -3859,15 +4426,15 @@ int sqlite3BtreeCommit(Btree *p){ ** ** This routine gets called when a rollback occurs. If the writeOnly ** flag is true, then only write-cursors need be tripped - read-only -** cursors save their current positions so that they may continue -** following the rollback. Or, if writeOnly is false, all cursors are +** cursors save their current positions so that they may continue +** following the rollback. Or, if writeOnly is false, all cursors are ** tripped. In general, writeOnly is false if the transaction being ** rolled back modified the database schema. In this case b-tree root ** pages may be moved or deleted from the database altogether, making ** it unsafe for read cursors to continue. ** -** If the writeOnly flag is true and an error is encountered while -** saving the current position of a read-only cursor, all cursors, +** If the writeOnly flag is true and an error is encountered while +** saving the current position of a read-only cursor, all cursors, ** including all read-cursors are tripped. ** ** SQLITE_OK is returned if successful, or if an error occurs while @@ -3881,7 +4448,6 @@ int sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode, int writeOnly){ if( pBtree ){ sqlite3BtreeEnter(pBtree); for(p=pBtree->pBt->pCursor; p; p=p->pNext){ - int i; if( writeOnly && (p->curFlags & BTCF_WriteFlag)==0 ){ if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){ rc = saveCursorPosition(p); @@ -3895,16 +4461,25 @@ int sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode, int writeOnly){ p->eState = CURSOR_FAULT; p->skipNext = errCode; } - for(i=0; i<=p->iPage; i++){ - releasePage(p->apPage[i]); - p->apPage[i] = 0; - } + btreeReleaseAllCursorPages(p); } sqlite3BtreeLeave(pBtree); } return rc; } +/* +** Set the pBt->nPage field correctly, according to the current +** state of the database. Assume pBt->pPage1 is valid. +*/ +static void btreeSetNPage(BtShared *pBt, MemPage *pPage1){ + int nPage = get4byte(&pPage1->aData[28]); + testcase( nPage==0 ); + if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage); + testcase( pBt->nPage!=(u32)nPage ); + pBt->nPage = nPage; +} + /* ** Rollback the transaction in progress. ** @@ -3950,12 +4525,8 @@ int sqlite3BtreeRollback(Btree *p, int tripCode, int writeOnly){ ** call btreeGetPage() on page 1 again to make ** sure pPage1->aData is set correctly. */ if( btreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){ - int nPage = get4byte(28+(u8*)pPage1->aData); - testcase( nPage==0 ); - if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage); - testcase( pBt->nPage!=nPage ); - pBt->nPage = nPage; - releasePage(pPage1); + btreeSetNPage(pBt, pPage1); + releasePageOne(pPage1); } assert( countValidCursors(pBt, 1)==0 ); pBt->inTransaction = TRANS_READ; @@ -3969,8 +4540,8 @@ int sqlite3BtreeRollback(Btree *p, int tripCode, int writeOnly){ /* ** Start a statement subtransaction. The subtransaction can be rolled -** back independently of the main transaction. You must start a transaction -** before starting a subtransaction. The subtransaction is ended automatically +** back independently of the main transaction. You must start a transaction +** before starting a subtransaction. The subtransaction is ended automatically ** if the main transaction commits or rolls back. ** ** Statement subtransactions are used around individual SQL statements @@ -4007,11 +4578,11 @@ int sqlite3BtreeBeginStmt(Btree *p, int iStatement){ /* ** The second argument to this function, op, is always SAVEPOINT_ROLLBACK ** or SAVEPOINT_RELEASE. This function either releases or rolls back the -** savepoint identified by parameter iSavepoint, depending on the value +** savepoint identified by parameter iSavepoint, depending on the value ** of op. ** ** Normally, iSavepoint is greater than or equal to zero. However, if op is -** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the +** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the ** contents of the entire transaction are rolled back. This is different ** from a normal transaction rollback, as no locks are released and the ** transaction remains open. @@ -4023,18 +4594,22 @@ int sqlite3BtreeSavepoint(Btree *p, int op, int iSavepoint){ assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK ); assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) ); sqlite3BtreeEnter(p); - rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint); + if( op==SAVEPOINT_ROLLBACK ){ + rc = saveAllCursors(pBt, 0, 0); + } + if( rc==SQLITE_OK ){ + rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint); + } if( rc==SQLITE_OK ){ if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){ pBt->nPage = 0; } rc = newDatabase(pBt); - pBt->nPage = get4byte(28 + pBt->pPage1->aData); + btreeSetNPage(pBt, pBt->pPage1); - /* The database size was written into the offset 28 of the header - ** when the transaction started, so we know that the value at offset - ** 28 is nonzero. */ - assert( pBt->nPage>0 ); + /* pBt->nPage might be zero if the database was corrupt when + ** the transaction was started. Otherwise, it must be at least 1. */ + assert( CORRUPT_DB || pBt->nPage>0 ); } sqlite3BtreeLeave(p); } @@ -4070,10 +4645,10 @@ int sqlite3BtreeSavepoint(Btree *p, int op, int iSavepoint){ ** is set. If FORDELETE is set, that is a hint to the implementation that ** this cursor will only be used to seek to and delete entries of an index ** as part of a larger DELETE statement. The FORDELETE hint is not used by -** this implementation. But in a hypothetical alternative storage engine +** this implementation. But in a hypothetical alternative storage engine ** in which index entries are automatically deleted when corresponding table ** rows are deleted, the FORDELETE flag is a hint that all SEEK and DELETE -** operations on this cursor can be no-ops and all READ operations can +** operations on this cursor can be no-ops and all READ operations can ** return a null row (2-bytes: 0x01 0x00). ** ** No checking is done to make sure that page iTable really is the @@ -4085,7 +4660,7 @@ int sqlite3BtreeSavepoint(Btree *p, int op, int iSavepoint){ */ static int btreeCursor( Btree *p, /* The btree */ - int iTable, /* Root page of table to open */ + Pgno iTable, /* Root page of table to open */ int wrFlag, /* 1 to write. 0 read-only */ struct KeyInfo *pKeyInfo, /* First arg to comparison function */ BtCursor *pCur /* Space for new cursor */ @@ -4094,16 +4669,17 @@ static int btreeCursor( BtCursor *pX; /* Looping over other all cursors */ assert( sqlite3BtreeHoldsMutex(p) ); - assert( wrFlag==0 - || wrFlag==BTREE_WRCSR - || wrFlag==(BTREE_WRCSR|BTREE_FORDELETE) + assert( wrFlag==0 + || wrFlag==BTREE_WRCSR + || wrFlag==(BTREE_WRCSR|BTREE_FORDELETE) ); - /* The following assert statements verify that if this is a sharable - ** b-tree database, the connection is holding the required table locks, - ** and that no other connection has any open cursor that conflicts with - ** this lock. */ - assert( hasSharedCacheTableLock(p, iTable, pKeyInfo!=0, (wrFlag?2:1)) ); + /* The following assert statements verify that if this is a sharable + ** b-tree database, the connection is holding the required table locks, + ** and that no other connection has any open cursor that conflicts with + ** this lock. The iTable<1 term disables the check for corrupt schemas. */ + assert( hasSharedCacheTableLock(p, iTable, pKeyInfo!=0, (wrFlag?2:1)) + || iTable<1 ); assert( wrFlag==0 || !hasReadConflicts(p, iTable) ); /* Assert that the caller has opened the required transaction. */ @@ -4112,54 +4688,69 @@ static int btreeCursor( assert( pBt->pPage1 && pBt->pPage1->aData ); assert( wrFlag==0 || (pBt->btsFlags & BTS_READ_ONLY)==0 ); - if( wrFlag ){ - allocateTempSpace(pBt); - if( pBt->pTmpSpace==0 ) return SQLITE_NOMEM; - } - if( iTable==1 && btreePagecount(pBt)==0 ){ - assert( wrFlag==0 ); - iTable = 0; + if( iTable<=1 ){ + if( iTable<1 ){ + return SQLITE_CORRUPT_BKPT; + }else if( btreePagecount(pBt)==0 ){ + assert( wrFlag==0 ); + iTable = 0; + } } /* Now that no other errors can occur, finish filling in the BtCursor ** variables and link the cursor into the BtShared list. */ - pCur->pgnoRoot = (Pgno)iTable; + pCur->pgnoRoot = iTable; pCur->iPage = -1; pCur->pKeyInfo = pKeyInfo; pCur->pBtree = p; pCur->pBt = pBt; - pCur->curFlags = wrFlag ? BTCF_WriteFlag : 0; - pCur->curPagerFlags = wrFlag ? 0 : PAGER_GET_READONLY; + pCur->curFlags = 0; /* If there are two or more cursors on the same btree, then all such ** cursors *must* have the BTCF_Multiple flag set. */ for(pX=pBt->pCursor; pX; pX=pX->pNext){ - if( pX->pgnoRoot==(Pgno)iTable ){ + if( pX->pgnoRoot==iTable ){ pX->curFlags |= BTCF_Multiple; - pCur->curFlags |= BTCF_Multiple; + pCur->curFlags = BTCF_Multiple; } } + pCur->eState = CURSOR_INVALID; pCur->pNext = pBt->pCursor; pBt->pCursor = pCur; - pCur->eState = CURSOR_INVALID; + if( wrFlag ){ + pCur->curFlags |= BTCF_WriteFlag; + pCur->curPagerFlags = 0; + if( pBt->pTmpSpace==0 ) return allocateTempSpace(pBt); + }else{ + pCur->curPagerFlags = PAGER_GET_READONLY; + } return SQLITE_OK; } -int sqlite3BtreeCursor( - Btree *p, /* The btree */ - int iTable, /* Root page of table to open */ - int wrFlag, /* 1 to write. 0 read-only */ - struct KeyInfo *pKeyInfo, /* First arg to xCompare() */ - BtCursor *pCur /* Write new cursor here */ -){ - int rc; - if( iTable<1 ){ - rc = SQLITE_CORRUPT_BKPT; - }else{ - sqlite3BtreeEnter(p); - rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur); - sqlite3BtreeLeave(p); - } +static int btreeCursorWithLock( + Btree *p, /* The btree */ + Pgno iTable, /* Root page of table to open */ + int wrFlag, /* 1 to write. 0 read-only */ + struct KeyInfo *pKeyInfo, /* First arg to comparison function */ + BtCursor *pCur /* Space for new cursor */ +){ + int rc; + sqlite3BtreeEnter(p); + rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur); + sqlite3BtreeLeave(p); return rc; } +int sqlite3BtreeCursor( + Btree *p, /* The btree */ + Pgno iTable, /* Root page of table to open */ + int wrFlag, /* 1 to write. 0 read-only */ + struct KeyInfo *pKeyInfo, /* First arg to xCompare() */ + BtCursor *pCur /* Write new cursor here */ +){ + if( p->sharable ){ + return btreeCursorWithLock(p, iTable, wrFlag, pKeyInfo, pCur); + }else{ + return btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur); + } +} /* ** Return the size of a BtCursor object in bytes. @@ -4173,6 +4764,25 @@ int sqlite3BtreeCursorSize(void){ return ROUND8(sizeof(BtCursor)); } +#ifdef SQLITE_DEBUG +/* +** Return true if and only if the Btree object will be automatically +** closed with the BtCursor closes. This is used within assert() statements +** only. +*/ +int sqlite3BtreeClosesWithCursor( + Btree *pBtree, /* the btree object */ + BtCursor *pCur /* Corresponding cursor */ +){ + BtShared *pBt = pBtree->pBt; + if( (pBt->openFlags & BTREE_SINGLE)==0 ) return 0; + if( pBt->pCursor!=pCur ) return 0; + if( pCur->pNext!=0 ) return 0; + if( pCur->pBtree!=pBtree ) return 0; + return 1; +} +#endif + /* ** Initialize memory that will be converted into a BtCursor object. ** @@ -4182,7 +4792,7 @@ int sqlite3BtreeCursorSize(void){ ** of run-time by skipping the initialization of those elements. */ void sqlite3BtreeCursorZero(BtCursor *p){ - memset(p, 0, offsetof(BtCursor, iPage)); + memset(p, 0, offsetof(BtCursor, BTCURSOR_FIRST_UNINIT)); } /* @@ -4192,10 +4802,8 @@ void sqlite3BtreeCursorZero(BtCursor *p){ int sqlite3BtreeCloseCursor(BtCursor *pCur){ Btree *pBtree = pCur->pBtree; if( pBtree ){ - int i; BtShared *pBt = pCur->pBt; sqlite3BtreeEnter(pBtree); - sqlite3BtreeClearCursor(pCur); assert( pBt->pCursor!=0 ); if( pBt->pCursor==pCur ){ pBt->pCursor = pCur->pNext; @@ -4209,13 +4817,19 @@ int sqlite3BtreeCloseCursor(BtCursor *pCur){ pPrev = pPrev->pNext; }while( ALWAYS(pPrev) ); } - for(i=0; i<=pCur->iPage; i++){ - releasePage(pCur->apPage[i]); - } + btreeReleaseAllCursorPages(pCur); unlockBtreeIfUnused(pBt); sqlite3_free(pCur->aOverflow); - /* sqlite3_free(pCur); */ - sqlite3BtreeLeave(pBtree); + sqlite3_free(pCur->pKey); + if( (pBt->openFlags & BTREE_SINGLE) && pBt->pCursor==0 ){ + /* Since the BtShared is not sharable, there is no need to + ** worry about the missing sqlite3BtreeLeave() call here. */ + assert( pBtree->sharable==0 ); + sqlite3BtreeClose(pBtree); + }else{ + sqlite3BtreeLeave(pBtree); + } + pCur->pBtree = 0; } return SQLITE_OK; } @@ -4229,21 +4843,27 @@ int sqlite3BtreeCloseCursor(BtCursor *pCur){ ** Using this cache reduces the number of calls to btreeParseCell(). */ #ifndef NDEBUG + static int cellInfoEqual(CellInfo *a, CellInfo *b){ + if( a->nKey!=b->nKey ) return 0; + if( a->pPayload!=b->pPayload ) return 0; + if( a->nPayload!=b->nPayload ) return 0; + if( a->nLocal!=b->nLocal ) return 0; + if( a->nSize!=b->nSize ) return 0; + return 1; + } static void assertCellInfo(BtCursor *pCur){ CellInfo info; - int iPage = pCur->iPage; memset(&info, 0, sizeof(info)); - btreeParseCell(pCur->apPage[iPage], pCur->aiIdx[iPage], &info); - assert( CORRUPT_DB || memcmp(&info, &pCur->info, sizeof(info))==0 ); + btreeParseCell(pCur->pPage, pCur->ix, &info); + assert( CORRUPT_DB || cellInfoEqual(&info, &pCur->info) ); } #else #define assertCellInfo(x) #endif static SQLITE_NOINLINE void getCellInfo(BtCursor *pCur){ if( pCur->info.nSize==0 ){ - int iPage = pCur->iPage; pCur->curFlags |= BTCF_ValidNKey; - btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info); + btreeParseCell(pCur->pPage,pCur->ix,&pCur->info); }else{ assertCellInfo(pCur); } @@ -4259,61 +4879,95 @@ int sqlite3BtreeCursorIsValid(BtCursor *pCur){ return pCur && pCur->eState==CURSOR_VALID; } #endif /* NDEBUG */ +int sqlite3BtreeCursorIsValidNN(BtCursor *pCur){ + assert( pCur!=0 ); + return pCur->eState==CURSOR_VALID; +} /* -** Set *pSize to the size of the buffer needed to hold the value of -** the key for the current entry. If the cursor is not pointing -** to a valid entry, *pSize is set to 0. -** -** For a table with the INTKEY flag set, this routine returns the key -** itself, not the number of bytes in the key. -** -** The caller must position the cursor prior to invoking this routine. -** -** This routine cannot fail. It always returns SQLITE_OK. +** Return the value of the integer key or "rowid" for a table btree. +** This routine is only valid for a cursor that is pointing into a +** ordinary table btree. If the cursor points to an index btree or +** is invalid, the result of this routine is undefined. */ -int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){ +i64 sqlite3BtreeIntegerKey(BtCursor *pCur){ assert( cursorHoldsMutex(pCur) ); assert( pCur->eState==CURSOR_VALID ); + assert( pCur->curIntKey ); getCellInfo(pCur); - *pSize = pCur->info.nKey; - return SQLITE_OK; + return pCur->info.nKey; +} + +/* +** Pin or unpin a cursor. +*/ +void sqlite3BtreeCursorPin(BtCursor *pCur){ + assert( (pCur->curFlags & BTCF_Pinned)==0 ); + pCur->curFlags |= BTCF_Pinned; +} +void sqlite3BtreeCursorUnpin(BtCursor *pCur){ + assert( (pCur->curFlags & BTCF_Pinned)!=0 ); + pCur->curFlags &= ~BTCF_Pinned; +} + +/* +** Return the offset into the database file for the start of the +** payload to which the cursor is pointing. +*/ +i64 sqlite3BtreeOffset(BtCursor *pCur){ + assert( cursorHoldsMutex(pCur) ); + assert( pCur->eState==CURSOR_VALID ); + getCellInfo(pCur); + return (i64)pCur->pBt->pageSize*((i64)pCur->pPage->pgno - 1) + + (i64)(pCur->info.pPayload - pCur->pPage->aData); } /* -** Set *pSize to the number of bytes of data in the entry the -** cursor currently points to. +** Return the number of bytes of payload for the entry that pCur is +** currently pointing to. For table btrees, this will be the amount +** of data. For index btrees, this will be the size of the key. ** ** The caller must guarantee that the cursor is pointing to a non-NULL ** valid entry. In other words, the calling procedure must guarantee ** that the cursor has Cursor.eState==CURSOR_VALID. -** -** Failure is not possible. This function always returns SQLITE_OK. -** It might just as well be a procedure (returning void) but we continue -** to return an integer result code for historical reasons. */ -int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){ - assert( cursorOwnsBtShared(pCur) ); +u32 sqlite3BtreePayloadSize(BtCursor *pCur){ + assert( cursorHoldsMutex(pCur) ); assert( pCur->eState==CURSOR_VALID ); - assert( pCur->iPage>=0 ); - assert( pCur->iPageapPage[pCur->iPage]->intKeyLeaf==1 ); getCellInfo(pCur); - *pSize = pCur->info.nPayload; - return SQLITE_OK; + return pCur->info.nPayload; +} + +/* +** Return an upper bound on the size of any record for the table +** that the cursor is pointing into. +** +** This is an optimization. Everything will still work if this +** routine always returns 2147483647 (which is the largest record +** that SQLite can handle) or more. But returning a smaller value might +** prevent large memory allocations when trying to interpret a +** corrupt database. +** +** The current implementation merely returns the size of the underlying +** database file. +*/ +sqlite3_int64 sqlite3BtreeMaxRecordSize(BtCursor *pCur){ + assert( cursorHoldsMutex(pCur) ); + assert( pCur->eState==CURSOR_VALID ); + return pCur->pBt->pageSize * (sqlite3_int64)pCur->pBt->nPage; } /* ** Given the page number of an overflow page in the database (parameter -** ovfl), this function finds the page number of the next page in the +** ovfl), this function finds the page number of the next page in the ** linked list of overflow pages. If possible, it uses the auto-vacuum -** pointer-map data instead of reading the content of page ovfl to do so. +** pointer-map data instead of reading the content of page ovfl to do so. ** ** If an error occurs an SQLite error code is returned. Otherwise: ** -** The page number of the next overflow page in the linked list is -** written to *pPgnoNext. If page ovfl is the last page in its linked -** list, *pPgnoNext is set to zero. +** The page number of the next overflow page in the linked list is +** written to *pPgnoNext. If page ovfl is the last page in its linked +** list, *pPgnoNext is set to zero. ** ** If ppPage is not NULL, and a reference to the MemPage object corresponding ** to page number pOvfl was obtained, then *ppPage is set to point to that @@ -4337,9 +4991,9 @@ static int getOverflowPage( #ifndef SQLITE_OMIT_AUTOVACUUM /* Try to find the next page in the overflow list using the - ** autovacuum pointer-map pages. Guess that the next page in - ** the overflow list is page number (ovfl+1). If that guess turns - ** out to be wrong, fall back to loading the data of page + ** autovacuum pointer-map pages. Guess that the next page in + ** the overflow list is page number (ovfl+1). If that guess turns + ** out to be wrong, fall back to loading the data of page ** number ovfl to determine the next page number. */ if( pBt->autoVacuum ){ @@ -4418,7 +5072,6 @@ static int copyPayload( ** ** 0: The operation is a read. Populate the overflow cache. ** 1: The operation is a write. Populate the overflow cache. -** 2: The operation is a read. Do not populate the overflow cache. ** ** A total of "amt" bytes are read or written beginning at "offset". ** Data is read to or from the buffer pBuf. @@ -4426,13 +5079,13 @@ static int copyPayload( ** The content being read or written might appear on the main page ** or be scattered out on multiple overflow pages. ** -** If the current cursor entry uses one or more overflow pages and the -** eOp argument is not 2, this function may allocate space for and lazily -** populates the overflow page-list cache array (BtCursor.aOverflow). -** Subsequent calls use this cache to make seeking to the supplied offset +** If the current cursor entry uses one or more overflow pages +** this function may allocate space for and lazily populate +** the overflow page-list cache array (BtCursor.aOverflow). +** Subsequent calls use this cache to make seeking to the supplied offset ** more efficient. ** -** Once an overflow page-list cache has been allocated, it may be +** Once an overflow page-list cache has been allocated, it must be ** invalidated if some other cursor writes to the same table, or if ** the cursor is moved to a different row. Additionally, in auto-vacuum ** mode, the following events may invalidate an overflow page-list cache. @@ -4445,35 +5098,38 @@ static int accessPayload( BtCursor *pCur, /* Cursor pointing to entry to read from */ u32 offset, /* Begin reading this far into payload */ u32 amt, /* Read this many bytes */ - unsigned char *pBuf, /* Write the bytes into this buffer */ + unsigned char *pBuf, /* Write the bytes into this buffer */ int eOp /* zero to read. non-zero to write. */ ){ unsigned char *aPayload; int rc = SQLITE_OK; int iIdx = 0; - MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */ + MemPage *pPage = pCur->pPage; /* Btree page of current entry */ BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */ #ifdef SQLITE_DIRECT_OVERFLOW_READ - unsigned char * const pBufStart = pBuf; - int bEnd; /* True if reading to end of data */ + unsigned char * const pBufStart = pBuf; /* Start of original out buffer */ #endif assert( pPage ); + assert( eOp==0 || eOp==1 ); assert( pCur->eState==CURSOR_VALID ); - assert( pCur->aiIdx[pCur->iPage]nCell ); + if( pCur->ix>=pPage->nCell ){ + return SQLITE_CORRUPT_PAGE(pPage); + } assert( cursorHoldsMutex(pCur) ); - assert( eOp!=2 || offset==0 ); /* Always start from beginning for eOp==2 */ getCellInfo(pCur); aPayload = pCur->info.pPayload; -#ifdef SQLITE_DIRECT_OVERFLOW_READ - bEnd = offset+amt==pCur->info.nPayload; -#endif assert( offset+amt <= pCur->info.nPayload ); - if( &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize] ){ - /* Trying to read or write past the end of the data is an error */ - return SQLITE_CORRUPT_BKPT; + assert( aPayload > pPage->aData ); + if( (uptr)(aPayload - pPage->aData) > (pBt->usableSize - pCur->info.nLocal) ){ + /* Trying to read or write past the end of the data is an error. The + ** conditional above is really: + ** &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize] + ** but is recast into its current form to avoid integer overflow problems + */ + return SQLITE_CORRUPT_PAGE(pPage); } /* Check if data must be read/written to/from the btree page itself. */ @@ -4482,7 +5138,7 @@ static int accessPayload( if( a+offset>pCur->info.nLocal ){ a = pCur->info.nLocal - offset; } - rc = copyPayload(&aPayload[offset], pBuf, a, (eOp & 0x01), pPage->pDbPage); + rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage); offset = 0; pBuf += a; amt -= a; @@ -4498,53 +5154,57 @@ static int accessPayload( nextPage = get4byte(&aPayload[pCur->info.nLocal]); /* If the BtCursor.aOverflow[] has not been allocated, allocate it now. - ** Except, do not allocate aOverflow[] for eOp==2. ** ** The aOverflow[] array is sized at one entry for each overflow page ** in the overflow chain. The page number of the first overflow page is ** stored in aOverflow[0], etc. A value of 0 in the aOverflow[] array ** means "not yet known" (the cache is lazily populated). */ - if( eOp!=2 && (pCur->curFlags & BTCF_ValidOvfl)==0 ){ + if( (pCur->curFlags & BTCF_ValidOvfl)==0 ){ int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize; - if( nOvfl>pCur->nOvflAlloc ){ - Pgno *aNew = (Pgno*)sqlite3Realloc( - pCur->aOverflow, nOvfl*2*sizeof(Pgno) - ); + if( pCur->aOverflow==0 + || nOvfl*(int)sizeof(Pgno) > sqlite3MallocSize(pCur->aOverflow) + ){ + Pgno *aNew; + if( sqlite3FaultSim(413) ){ + aNew = 0; + }else{ + aNew = (Pgno*)sqlite3Realloc(pCur->aOverflow, nOvfl*2*sizeof(Pgno)); + } if( aNew==0 ){ - rc = SQLITE_NOMEM; + return SQLITE_NOMEM_BKPT; }else{ - pCur->nOvflAlloc = nOvfl*2; pCur->aOverflow = aNew; } } - if( rc==SQLITE_OK ){ - memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno)); - pCur->curFlags |= BTCF_ValidOvfl; + memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno)); + pCur->curFlags |= BTCF_ValidOvfl; + }else{ + /* Sanity check the validity of the overflow page cache */ + assert( pCur->aOverflow[0]==nextPage + || pCur->aOverflow[0]==0 + || CORRUPT_DB ); + assert( pCur->aOverflow[0]!=0 || pCur->aOverflow[offset/ovflSize]==0 ); + + /* If the overflow page-list cache has been allocated and the + ** entry for the first required overflow page is valid, skip + ** directly to it. + */ + if( pCur->aOverflow[offset/ovflSize] ){ + iIdx = (offset/ovflSize); + nextPage = pCur->aOverflow[iIdx]; + offset = (offset%ovflSize); } } - /* If the overflow page-list cache has been allocated and the - ** entry for the first required overflow page is valid, skip - ** directly to it. - */ - if( (pCur->curFlags & BTCF_ValidOvfl)!=0 - && pCur->aOverflow[offset/ovflSize] - ){ - iIdx = (offset/ovflSize); - nextPage = pCur->aOverflow[iIdx]; - offset = (offset%ovflSize); - } - - for( ; rc==SQLITE_OK && amt>0 && nextPage; iIdx++){ - + assert( rc==SQLITE_OK && amt>0 ); + while( nextPage ){ /* If required, populate the overflow page-list cache. */ - if( (pCur->curFlags & BTCF_ValidOvfl)!=0 ){ - assert( pCur->aOverflow[iIdx]==0 - || pCur->aOverflow[iIdx]==nextPage - || CORRUPT_DB ); - pCur->aOverflow[iIdx] = nextPage; - } + if( nextPage > pBt->nPage ) return SQLITE_CORRUPT_BKPT; + assert( pCur->aOverflow[iIdx]==0 + || pCur->aOverflow[iIdx]==nextPage + || CORRUPT_DB ); + pCur->aOverflow[iIdx] = nextPage; if( offset>=ovflSize ){ /* The only reason to read this page is to obtain the page @@ -4552,11 +5212,7 @@ static int accessPayload( ** data is not required. So first try to lookup the overflow ** page-list cache, if any, then fall back to the getOverflowPage() ** function. - ** - ** Note that the aOverflow[] array must be allocated because eOp!=2 - ** here. If eOp==2, then offset==0 and this branch is never taken. */ - assert( eOp!=2 ); assert( pCur->curFlags & BTCF_ValidOvfl ); assert( pCur->pBtree->db==pBt->db ); if( pCur->aOverflow[iIdx+1] ){ @@ -4569,9 +5225,6 @@ static int accessPayload( /* Need to read this page properly. It contains some of the ** range of data that is being read (eOp==0) or written (eOp!=0). */ -#ifdef SQLITE_DIRECT_OVERFLOW_READ - sqlite3_file *fd; -#endif int a = amt; if( a + offset > ovflSize ){ a = ovflSize - offset; @@ -4580,29 +5233,26 @@ static int accessPayload( #ifdef SQLITE_DIRECT_OVERFLOW_READ /* If all the following are true: ** - ** 1) this is a read operation, and + ** 1) this is a read operation, and ** 2) data is required from the start of this overflow page, and - ** 3) the database is file-backed, and - ** 4) there is no open write-transaction, and - ** 5) the database is not a WAL database, - ** 6) all data from the page is being read. - ** 7) at least 4 bytes have already been read into the output buffer + ** 3) there are no dirty pages in the page-cache + ** 4) the database is file-backed, and + ** 5) the page is not in the WAL file + ** 6) at least 4 bytes have already been read into the output buffer ** ** then data can be read directly from the database file into the ** output buffer, bypassing the page-cache altogether. This speeds ** up loading large records that span many overflow pages. */ - if( (eOp&0x01)==0 /* (1) */ + if( eOp==0 /* (1) */ && offset==0 /* (2) */ - && (bEnd || a==ovflSize) /* (6) */ - && pBt->inTransaction==TRANS_READ /* (4) */ - && (fd = sqlite3PagerFile(pBt->pPager))->pMethods /* (3) */ - && pBt->pPage1->aData[19]==0x01 /* (5) */ - && &pBuf[-4]>=pBufStart /* (7) */ + && sqlite3PagerDirectReadOk(pBt->pPager, nextPage) /* (3,4,5) */ + && &pBuf[-4]>=pBufStart /* (6) */ ){ + sqlite3_file *fd = sqlite3PagerFile(pBt->pPager); u8 aSave[4]; u8 *aWrite = &pBuf[-4]; - assert( aWrite>=pBufStart ); /* hence (7) */ + assert( aWrite>=pBufStart ); /* due to (6) */ memcpy(aSave, aWrite, 4); rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1)); nextPage = get4byte(aWrite); @@ -4613,79 +5263,88 @@ static int accessPayload( { DbPage *pDbPage; rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage, - ((eOp&0x01)==0 ? PAGER_GET_READONLY : 0) + (eOp==0 ? PAGER_GET_READONLY : 0) ); if( rc==SQLITE_OK ){ aPayload = sqlite3PagerGetData(pDbPage); nextPage = get4byte(aPayload); - rc = copyPayload(&aPayload[offset+4], pBuf, a, (eOp&0x01), pDbPage); + rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage); sqlite3PagerUnref(pDbPage); offset = 0; } } amt -= a; + if( amt==0 ) return rc; pBuf += a; } + if( rc ) break; + iIdx++; } } if( rc==SQLITE_OK && amt>0 ){ - return SQLITE_CORRUPT_BKPT; + /* Overflow chain ends prematurely */ + return SQLITE_CORRUPT_PAGE(pPage); } return rc; } /* -** Read part of the key associated with cursor pCur. Exactly -** "amt" bytes will be transferred into pBuf[]. The transfer +** Read part of the payload for the row at which that cursor pCur is currently +** pointing. "amt" bytes will be transferred into pBuf[]. The transfer ** begins at "offset". ** -** The caller must ensure that pCur is pointing to a valid row -** in the table. +** pCur can be pointing to either a table or an index b-tree. +** If pointing to a table btree, then the content section is read. If +** pCur is pointing to an index b-tree then the key section is read. +** +** For sqlite3BtreePayload(), the caller must ensure that pCur is pointing +** to a valid row in the table. For sqlite3BtreePayloadChecked(), the +** cursor might be invalid or might need to be restored before being read. ** ** Return SQLITE_OK on success or an error code if anything goes ** wrong. An error is returned if "offset+amt" is larger than ** the available payload. */ -int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ +int sqlite3BtreePayload(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ assert( cursorHoldsMutex(pCur) ); assert( pCur->eState==CURSOR_VALID ); - assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] ); - assert( pCur->aiIdx[pCur->iPage]apPage[pCur->iPage]->nCell ); + assert( pCur->iPage>=0 && pCur->pPage ); return accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0); } /* -** Read part of the data associated with cursor pCur. Exactly -** "amt" bytes will be transfered into pBuf[]. The transfer -** begins at "offset". -** -** Return SQLITE_OK on success or an error code if anything goes -** wrong. An error is returned if "offset+amt" is larger than -** the available payload. +** This variant of sqlite3BtreePayload() works even if the cursor has not +** in the CURSOR_VALID state. It is only used by the sqlite3_blob_read() +** interface. */ -int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ - int rc; - #ifndef SQLITE_OMIT_INCRBLOB +static SQLITE_NOINLINE int accessPayloadChecked( + BtCursor *pCur, + u32 offset, + u32 amt, + void *pBuf +){ + int rc; if ( pCur->eState==CURSOR_INVALID ){ return SQLITE_ABORT; } -#endif - assert( cursorOwnsBtShared(pCur) ); - rc = restoreCursorPosition(pCur); - if( rc==SQLITE_OK ){ - assert( pCur->eState==CURSOR_VALID ); - assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] ); - assert( pCur->aiIdx[pCur->iPage]apPage[pCur->iPage]->nCell ); - rc = accessPayload(pCur, offset, amt, pBuf, 0); + rc = btreeRestoreCursorPosition(pCur); + return rc ? rc : accessPayload(pCur, offset, amt, pBuf, 0); +} +int sqlite3BtreePayloadChecked(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ + if( pCur->eState==CURSOR_VALID ){ + assert( cursorOwnsBtShared(pCur) ); + return accessPayload(pCur, offset, amt, pBuf, 0); + }else{ + return accessPayloadChecked(pCur, offset, amt, pBuf); } - return rc; } +#endif /* SQLITE_OMIT_INCRBLOB */ /* -** Return a pointer to payload information from the entry that the +** Return a pointer to payload information from the entry that the ** pCur cursor is pointing to. The pointer is to the beginning of ** the key if index btrees (pPage->intKey==0) and is the data for ** table btrees (pPage->intKey==1). The number of bytes of available @@ -4707,18 +5366,23 @@ static const void *fetchPayload( BtCursor *pCur, /* Cursor pointing to entry to read from */ u32 *pAmt /* Write the number of available bytes here */ ){ - u32 amt; - assert( pCur!=0 && pCur->iPage>=0 && pCur->apPage[pCur->iPage]); + int amt; + assert( pCur!=0 && pCur->iPage>=0 && pCur->pPage); assert( pCur->eState==CURSOR_VALID ); assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); assert( cursorOwnsBtShared(pCur) ); - assert( pCur->aiIdx[pCur->iPage]apPage[pCur->iPage]->nCell ); + assert( pCur->ixpPage->nCell || CORRUPT_DB ); assert( pCur->info.nSize>0 ); - assert( pCur->info.pPayload>pCur->apPage[pCur->iPage]->aData || CORRUPT_DB ); - assert( pCur->info.pPayloadapPage[pCur->iPage]->aDataEnd ||CORRUPT_DB); - amt = (int)(pCur->apPage[pCur->iPage]->aDataEnd - pCur->info.pPayload); - if( pCur->info.nLocalinfo.nLocal; - *pAmt = amt; + assert( pCur->info.pPayload>pCur->pPage->aData || CORRUPT_DB ); + assert( pCur->info.pPayloadpPage->aDataEnd ||CORRUPT_DB); + amt = pCur->info.nLocal; + if( amt>(int)(pCur->pPage->aDataEnd - pCur->info.pPayload) ){ + /* There is too little space on the page for the expected amount + ** of local content. Database must be corrupt. */ + assert( CORRUPT_DB ); + amt = MAX(0, (int)(pCur->pPage->aDataEnd - pCur->info.pPayload)); + } + *pAmt = (u32)amt; return (void*)pCur->info.pPayload; } @@ -4737,10 +5401,7 @@ static const void *fetchPayload( ** These routines is used to get quick access to key and data ** in the common case where no overflow pages are used. */ -const void *sqlite3BtreeKeyFetch(BtCursor *pCur, u32 *pAmt){ - return fetchPayload(pCur, pAmt); -} -const void *sqlite3BtreeDataFetch(BtCursor *pCur, u32 *pAmt){ +const void *sqlite3BtreePayloadFetch(BtCursor *pCur, u32 *pAmt){ return fetchPayload(pCur, pAmt); } @@ -4755,8 +5416,7 @@ const void *sqlite3BtreeDataFetch(BtCursor *pCur, u32 *pAmt){ ** vice-versa). */ static int moveToChild(BtCursor *pCur, u32 newPgno){ - BtShared *pBt = pCur->pBt; - + int rc; assert( cursorOwnsBtShared(pCur) ); assert( pCur->eState==CURSOR_VALID ); assert( pCur->iPageinfo.nSize = 0; pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl); + pCur->aiIdx[pCur->iPage] = pCur->ix; + pCur->apPage[pCur->iPage] = pCur->pPage; + pCur->ix = 0; pCur->iPage++; - pCur->aiIdx[pCur->iPage] = 0; - return getAndInitPage(pBt, newPgno, &pCur->apPage[pCur->iPage], - pCur, pCur->curPagerFlags); + rc = getAndInitPage(pCur->pBt, newPgno, &pCur->pPage, pCur->curPagerFlags); + assert( pCur->pPage!=0 || rc!=SQLITE_OK ); + if( rc==SQLITE_OK + && (pCur->pPage->nCell<1 || pCur->pPage->intKey!=pCur->curIntKey) + ){ + releasePage(pCur->pPage); + rc = SQLITE_CORRUPT_PGNO(newPgno); + } + if( rc ){ + pCur->pPage = pCur->apPage[--pCur->iPage]; + } + return rc; } -#if SQLITE_DEBUG +#ifdef SQLITE_DEBUG /* -** Page pParent is an internal (non-leaf) tree page. This function +** Page pParent is an internal (non-leaf) tree page. This function ** asserts that page number iChild is the left-child if the iIdx'th ** cell in page pParent. Or, if iIdx is equal to the total number of ** cells in pParent, that page number iChild is the right-child of @@ -4791,7 +5463,7 @@ static void assertParentIndex(MemPage *pParent, int iIdx, Pgno iChild){ } } #else -# define assertParentIndex(x,y,z) +# define assertParentIndex(x,y,z) #endif /* @@ -4803,19 +5475,23 @@ static void assertParentIndex(MemPage *pParent, int iIdx, Pgno iChild){ ** the largest cell index. */ static void moveToParent(BtCursor *pCur){ + MemPage *pLeaf; assert( cursorOwnsBtShared(pCur) ); assert( pCur->eState==CURSOR_VALID ); assert( pCur->iPage>0 ); - assert( pCur->apPage[pCur->iPage] ); + assert( pCur->pPage ); assertParentIndex( - pCur->apPage[pCur->iPage-1], - pCur->aiIdx[pCur->iPage-1], - pCur->apPage[pCur->iPage]->pgno + pCur->apPage[pCur->iPage-1], + pCur->aiIdx[pCur->iPage-1], + pCur->pPage->pgno ); testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell ); pCur->info.nSize = 0; pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl); - releasePageNotNull(pCur->apPage[pCur->iPage--]); + pCur->ix = pCur->aiIdx[pCur->iPage-1]; + pLeaf = pCur->pPage; + pCur->pPage = pCur->apPage[--pCur->iPage]; + releasePageNotNull(pLeaf); } /* @@ -4823,19 +5499,19 @@ static void moveToParent(BtCursor *pCur){ ** ** If the table has a virtual root page, then the cursor is moved to point ** to the virtual root page instead of the actual root page. A table has a -** virtual root page when the actual root page contains no cells and a +** virtual root page when the actual root page contains no cells and a ** single child page. This can only happen with the table rooted at page 1. ** -** If the b-tree structure is empty, the cursor state is set to -** CURSOR_INVALID. Otherwise, the cursor is set to point to the first -** cell located on the root (or virtual root) page and the cursor state -** is set to CURSOR_VALID. +** If the b-tree structure is empty, the cursor state is set to +** CURSOR_INVALID and this routine returns SQLITE_EMPTY. Otherwise, +** the cursor is set to point to the first cell located on the root +** (or virtual root) page and the cursor state is set to CURSOR_VALID. ** ** If this function returns successfully, it may be assumed that the -** page-header flags indicate that the [virtual] root-page is the expected +** page-header flags indicate that the [virtual] root-page is the expected ** kind of b-tree page (i.e. if when opening the cursor the caller did not ** specify a KeyInfo structure the flags byte is set to 0x05 or 0x0D, -** indicating a table b-tree, or if the caller did specify a KeyInfo +** indicating a table b-tree, or if the caller did specify a KeyInfo ** structure the flags byte is set to 0x02 or 0x0A, indicating an index ** b-tree). */ @@ -4847,52 +5523,59 @@ static int moveToRoot(BtCursor *pCur){ assert( CURSOR_INVALID < CURSOR_REQUIRESEEK ); assert( CURSOR_VALID < CURSOR_REQUIRESEEK ); assert( CURSOR_FAULT > CURSOR_REQUIRESEEK ); - if( pCur->eState>=CURSOR_REQUIRESEEK ){ - if( pCur->eState==CURSOR_FAULT ){ - assert( pCur->skipNext!=SQLITE_OK ); - return pCur->skipNext; - } - sqlite3BtreeClearCursor(pCur); - } + assert( pCur->eState < CURSOR_REQUIRESEEK || pCur->iPage<0 ); + assert( pCur->pgnoRoot>0 || pCur->iPage<0 ); if( pCur->iPage>=0 ){ - while( pCur->iPage ){ - assert( pCur->apPage[pCur->iPage]!=0 ); - releasePageNotNull(pCur->apPage[pCur->iPage--]); + if( pCur->iPage ){ + releasePageNotNull(pCur->pPage); + while( --pCur->iPage ){ + releasePageNotNull(pCur->apPage[pCur->iPage]); + } + pRoot = pCur->pPage = pCur->apPage[0]; + goto skip_init; } }else if( pCur->pgnoRoot==0 ){ pCur->eState = CURSOR_INVALID; - return SQLITE_OK; + return SQLITE_EMPTY; }else{ assert( pCur->iPage==(-1) ); - rc = getAndInitPage(pCur->pBtree->pBt, pCur->pgnoRoot, &pCur->apPage[0], - 0, pCur->curPagerFlags); + if( pCur->eState>=CURSOR_REQUIRESEEK ){ + if( pCur->eState==CURSOR_FAULT ){ + assert( pCur->skipNext!=SQLITE_OK ); + return pCur->skipNext; + } + sqlite3BtreeClearCursor(pCur); + } + rc = getAndInitPage(pCur->pBt, pCur->pgnoRoot, &pCur->pPage, + pCur->curPagerFlags); if( rc!=SQLITE_OK ){ pCur->eState = CURSOR_INVALID; return rc; } pCur->iPage = 0; - pCur->curIntKey = pCur->apPage[0]->intKey; + pCur->curIntKey = pCur->pPage->intKey; } - pRoot = pCur->apPage[0]; - assert( pRoot->pgno==pCur->pgnoRoot ); + pRoot = pCur->pPage; + assert( pRoot->pgno==pCur->pgnoRoot || CORRUPT_DB ); /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is ** NULL, the caller expects a table b-tree. If this is not the case, - ** return an SQLITE_CORRUPT error. + ** return an SQLITE_CORRUPT error. ** ** Earlier versions of SQLite assumed that this test could not fail ** if the root page was already loaded when this function was called (i.e. - ** if pCur->iPage>=0). But this is not so if the database is corrupted - ** in such a way that page pRoot is linked into a second b-tree table + ** if pCur->iPage>=0). But this is not so if the database is corrupted + ** in such a way that page pRoot is linked into a second b-tree table ** (or the freelist). */ assert( pRoot->intKey==1 || pRoot->intKey==0 ); if( pRoot->isInit==0 || (pCur->pKeyInfo==0)!=pRoot->intKey ){ - return SQLITE_CORRUPT_BKPT; + return SQLITE_CORRUPT_PAGE(pCur->pPage); } - pCur->aiIdx[0] = 0; +skip_init: + pCur->ix = 0; pCur->info.nSize = 0; pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidNKey|BTCF_ValidOvfl); @@ -4906,6 +5589,7 @@ static int moveToRoot(BtCursor *pCur){ rc = moveToChild(pCur, subpage); }else{ pCur->eState = CURSOR_INVALID; + rc = SQLITE_EMPTY; } return rc; } @@ -4924,9 +5608,9 @@ static int moveToLeftmost(BtCursor *pCur){ assert( cursorOwnsBtShared(pCur) ); assert( pCur->eState==CURSOR_VALID ); - while( rc==SQLITE_OK && !(pPage = pCur->apPage[pCur->iPage])->leaf ){ - assert( pCur->aiIdx[pCur->iPage]nCell ); - pgno = get4byte(findCell(pPage, pCur->aiIdx[pCur->iPage])); + while( rc==SQLITE_OK && !(pPage = pCur->pPage)->leaf ){ + assert( pCur->ixnCell ); + pgno = get4byte(findCell(pPage, pCur->ix)); rc = moveToChild(pCur, pgno); } return rc; @@ -4949,13 +5633,13 @@ static int moveToRightmost(BtCursor *pCur){ assert( cursorOwnsBtShared(pCur) ); assert( pCur->eState==CURSOR_VALID ); - while( !(pPage = pCur->apPage[pCur->iPage])->leaf ){ + while( !(pPage = pCur->pPage)->leaf ){ pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); - pCur->aiIdx[pCur->iPage] = pPage->nCell; + pCur->ix = pPage->nCell; rc = moveToChild(pCur, pgno); if( rc ) return rc; } - pCur->aiIdx[pCur->iPage] = pPage->nCell-1; + pCur->ix = pPage->nCell-1; assert( pCur->info.nSize==0 ); assert( (pCur->curFlags & BTCF_ValidNKey)==0 ); return SQLITE_OK; @@ -4972,69 +5656,323 @@ int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){ assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); rc = moveToRoot(pCur); if( rc==SQLITE_OK ){ - if( pCur->eState==CURSOR_INVALID ){ - assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 ); - *pRes = 1; - }else{ - assert( pCur->apPage[pCur->iPage]->nCell>0 ); - *pRes = 0; - rc = moveToLeftmost(pCur); - } + assert( pCur->pPage->nCell>0 ); + *pRes = 0; + rc = moveToLeftmost(pCur); + }else if( rc==SQLITE_EMPTY ){ + assert( pCur->pgnoRoot==0 || (pCur->pPage!=0 && pCur->pPage->nCell==0) ); + *pRes = 1; + rc = SQLITE_OK; } return rc; } +/* Set *pRes to 1 (true) if the BTree pointed to by cursor pCur contains zero +** rows of content. Set *pRes to 0 (false) if the table contains content. +** Return SQLITE_OK on success or some error code (ex: SQLITE_NOMEM) if +** something goes wrong. +*/ +int sqlite3BtreeIsEmpty(BtCursor *pCur, int *pRes){ + int rc; + + assert( cursorOwnsBtShared(pCur) ); + assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); + if( pCur->eState==CURSOR_VALID ){ + *pRes = 0; + return SQLITE_OK; + } + rc = moveToRoot(pCur); + if( rc==SQLITE_EMPTY ){ + *pRes = 1; + rc = SQLITE_OK; + }else{ + *pRes = 0; + } + return rc; +} + +#ifdef SQLITE_DEBUG +/* The cursors is CURSOR_VALID and has BTCF_AtLast set. Verify that +** this flags are true for a consistent database. +** +** This routine is is called from within assert() statements only. +** It is an internal verification routine and does not appear in production +** builds. +*/ +static int cursorIsAtLastEntry(BtCursor *pCur){ + int ii; + for(ii=0; iiiPage; ii++){ + if( pCur->aiIdx[ii]!=pCur->apPage[ii]->nCell ) return 0; + } + return pCur->ix==pCur->pPage->nCell-1 && pCur->pPage->leaf!=0; +} +#endif + /* Move the cursor to the last entry in the table. Return SQLITE_OK ** on success. Set *pRes to 0 if the cursor actually points to something ** or set *pRes to 1 if the table is empty. */ +static SQLITE_NOINLINE int btreeLast(BtCursor *pCur, int *pRes){ + int rc = moveToRoot(pCur); + if( rc==SQLITE_OK ){ + assert( pCur->eState==CURSOR_VALID ); + *pRes = 0; + rc = moveToRightmost(pCur); + if( rc==SQLITE_OK ){ + pCur->curFlags |= BTCF_AtLast; + }else{ + pCur->curFlags &= ~BTCF_AtLast; + } + }else if( rc==SQLITE_EMPTY ){ + assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 ); + *pRes = 1; + rc = SQLITE_OK; + } + return rc; +} int sqlite3BtreeLast(BtCursor *pCur, int *pRes){ - int rc; - assert( cursorOwnsBtShared(pCur) ); assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); /* If the cursor already points to the last entry, this is a no-op. */ if( CURSOR_VALID==pCur->eState && (pCur->curFlags & BTCF_AtLast)!=0 ){ -#ifdef SQLITE_DEBUG - /* This block serves to assert() that the cursor really does point - ** to the last entry in the b-tree. */ - int ii; - for(ii=0; iiiPage; ii++){ - assert( pCur->aiIdx[ii]==pCur->apPage[ii]->nCell ); - } - assert( pCur->aiIdx[pCur->iPage]==pCur->apPage[pCur->iPage]->nCell-1 ); - assert( pCur->apPage[pCur->iPage]->leaf ); -#endif + assert( cursorIsAtLastEntry(pCur) || CORRUPT_DB ); + *pRes = 0; return SQLITE_OK; } + return btreeLast(pCur, pRes); +} - rc = moveToRoot(pCur); - if( rc==SQLITE_OK ){ - if( CURSOR_INVALID==pCur->eState ){ - assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 ); - *pRes = 1; - }else{ - assert( pCur->eState==CURSOR_VALID ); +/* Move the cursor so that it points to an entry in a table (a.k.a INTKEY) +** table near the key intKey. Return a success code. +** +** If an exact match is not found, then the cursor is always +** left pointing at a leaf page which would hold the entry if it +** were present. The cursor might point to an entry that comes +** before or after the key. +** +** An integer is written into *pRes which is the result of +** comparing the key with the entry to which the cursor is +** pointing. The meaning of the integer written into +** *pRes is as follows: +** +** *pRes<0 The cursor is left pointing at an entry that +** is smaller than intKey or if the table is empty +** and the cursor is therefore left point to nothing. +** +** *pRes==0 The cursor is left pointing at an entry that +** exactly matches intKey. +** +** *pRes>0 The cursor is left pointing at an entry that +** is larger than intKey. +*/ +int sqlite3BtreeTableMoveto( + BtCursor *pCur, /* The cursor to be moved */ + i64 intKey, /* The table key */ + int biasRight, /* If true, bias the search to the high end */ + int *pRes /* Write search results here */ +){ + int rc; + + assert( cursorOwnsBtShared(pCur) ); + assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); + assert( pRes ); + assert( pCur->pKeyInfo==0 ); + assert( pCur->eState!=CURSOR_VALID || pCur->curIntKey!=0 ); + + /* If the cursor is already positioned at the point we are trying + ** to move to, then just return without doing any work */ + if( pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0 ){ + if( pCur->info.nKey==intKey ){ *pRes = 0; - rc = moveToRightmost(pCur); - if( rc==SQLITE_OK ){ - pCur->curFlags |= BTCF_AtLast; + return SQLITE_OK; + } + if( pCur->info.nKeycurFlags & BTCF_AtLast)!=0 ){ + assert( cursorIsAtLastEntry(pCur) || CORRUPT_DB ); + *pRes = -1; + return SQLITE_OK; + } + /* If the requested key is one more than the previous key, then + ** try to get there using sqlite3BtreeNext() rather than a full + ** binary search. This is an optimization only. The correct answer + ** is still obtained without this case, only a little more slowly. */ + if( pCur->info.nKey+1==intKey ){ + *pRes = 0; + rc = sqlite3BtreeNext(pCur, 0); + if( rc==SQLITE_OK ){ + getCellInfo(pCur); + if( pCur->info.nKey==intKey ){ + return SQLITE_OK; + } + }else if( rc!=SQLITE_DONE ){ + return rc; + } + } + } + } + +#ifdef SQLITE_DEBUG + pCur->pBtree->nSeek++; /* Performance measurement during testing */ +#endif + + rc = moveToRoot(pCur); + if( rc ){ + if( rc==SQLITE_EMPTY ){ + assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 ); + *pRes = -1; + return SQLITE_OK; + } + return rc; + } + assert( pCur->pPage ); + assert( pCur->pPage->isInit ); + assert( pCur->eState==CURSOR_VALID ); + assert( pCur->pPage->nCell > 0 ); + assert( pCur->iPage==0 || pCur->apPage[0]->intKey==pCur->curIntKey ); + assert( pCur->curIntKey ); + + for(;;){ + int lwr, upr, idx, c; + Pgno chldPg; + MemPage *pPage = pCur->pPage; + u8 *pCell; /* Pointer to current cell in pPage */ + + /* pPage->nCell must be greater than zero. If this is the root-page + ** the cursor would have been INVALID above and this for(;;) loop + ** not run. If this is not the root-page, then the moveToChild() routine + ** would have already detected db corruption. Similarly, pPage must + ** be the right kind (index or table) of b-tree page. Otherwise + ** a moveToChild() or moveToRoot() call would have detected corruption. */ + assert( pPage->nCell>0 ); + assert( pPage->intKey ); + lwr = 0; + upr = pPage->nCell-1; + assert( biasRight==0 || biasRight==1 ); + idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */ + for(;;){ + i64 nCellKey; + pCell = findCellPastPtr(pPage, idx); + if( pPage->intKeyLeaf ){ + while( 0x80 <= *(pCell++) ){ + if( pCell>=pPage->aDataEnd ){ + return SQLITE_CORRUPT_PAGE(pPage); + } + } + } + getVarint(pCell, (u64*)&nCellKey); + if( nCellKeyupr ){ c = -1; break; } + }else if( nCellKey>intKey ){ + upr = idx-1; + if( lwr>upr ){ c = +1; break; } }else{ - pCur->curFlags &= ~BTCF_AtLast; + assert( nCellKey==intKey ); + pCur->ix = (u16)idx; + if( !pPage->leaf ){ + lwr = idx; + goto moveto_table_next_layer; + }else{ + pCur->curFlags |= BTCF_ValidNKey; + pCur->info.nKey = nCellKey; + pCur->info.nSize = 0; + *pRes = 0; + return SQLITE_OK; + } } - + assert( lwr+upr>=0 ); + idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2; */ + } + assert( lwr==upr+1 || !pPage->leaf ); + assert( pPage->isInit ); + if( pPage->leaf ){ + assert( pCur->ixpPage->nCell ); + pCur->ix = (u16)idx; + *pRes = c; + rc = SQLITE_OK; + goto moveto_table_finish; } +moveto_table_next_layer: + if( lwr>=pPage->nCell ){ + chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]); + }else{ + chldPg = get4byte(findCell(pPage, lwr)); + } + pCur->ix = (u16)lwr; + rc = moveToChild(pCur, chldPg); + if( rc ) break; + } +moveto_table_finish: + pCur->info.nSize = 0; + assert( (pCur->curFlags & BTCF_ValidOvfl)==0 ); + return rc; +} + +/* +** Compare the "idx"-th cell on the page pPage against the key +** pointing to by pIdxKey using xRecordCompare. Return negative or +** zero if the cell is less than or equal pIdxKey. Return positive +** if unknown. +** +** Return value negative: Cell at pCur[idx] less than pIdxKey +** +** Return value is zero: Cell at pCur[idx] equals pIdxKey +** +** Return value positive: Nothing is known about the relationship +** of the cell at pCur[idx] and pIdxKey. +** +** This routine is part of an optimization. It is always safe to return +** a positive value as that will cause the optimization to be skipped. +*/ +static int indexCellCompare( + MemPage *pPage, + int idx, + UnpackedRecord *pIdxKey, + RecordCompare xRecordCompare +){ + int c; + int nCell; /* Size of the pCell cell in bytes */ + u8 *pCell = findCellPastPtr(pPage, idx); + + nCell = pCell[0]; + if( nCell<=pPage->max1bytePayload ){ + /* This branch runs if the record-size field of the cell is a + ** single byte varint and the record fits entirely on the main + ** b-tree page. */ + testcase( pCell+nCell+1==pPage->aDataEnd ); + c = xRecordCompare(nCell, (void*)&pCell[1], pIdxKey); + }else if( !(pCell[1] & 0x80) + && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal + ){ + /* The record-size field is a 2 byte varint and the record + ** fits entirely on the main b-tree page. */ + testcase( pCell+nCell+2==pPage->aDataEnd ); + c = xRecordCompare(nCell, (void*)&pCell[2], pIdxKey); + }else{ + /* If the record extends into overflow pages, do not attempt + ** the optimization. */ + c = 99; } - return rc; + return c; } -/* Move the cursor so that it points to an entry near the key -** specified by pIdxKey or intKey. Return a success code. -** -** For INTKEY tables, the intKey parameter is used. pIdxKey -** must be NULL. For index tables, pIdxKey is used and intKey -** is ignored. +/* +** Return true (non-zero) if pCur is current pointing to the last +** page of a table. +*/ +static int cursorOnLastPage(BtCursor *pCur){ + int i; + assert( pCur->eState==CURSOR_VALID ); + for(i=0; iiPage; i++){ + MemPage *pPage = pCur->apPage[i]; + if( pCur->aiIdx[i]nCell ) return 0; + } + return 1; +} + +/* Move the cursor so that it points to an entry in an index table +** near the key pIdxKey. Return a success code. ** ** If an exact match is not found, then the cursor is always ** left pointing at a leaf page which would hold the entry if it @@ -5042,28 +5980,26 @@ int sqlite3BtreeLast(BtCursor *pCur, int *pRes){ ** before or after the key. ** ** An integer is written into *pRes which is the result of -** comparing the key with the entry to which the cursor is +** comparing the key with the entry to which the cursor is ** pointing. The meaning of the integer written into ** *pRes is as follows: ** ** *pRes<0 The cursor is left pointing at an entry that -** is smaller than intKey/pIdxKey or if the table is empty +** is smaller than pIdxKey or if the table is empty ** and the cursor is therefore left point to nothing. ** ** *pRes==0 The cursor is left pointing at an entry that -** exactly matches intKey/pIdxKey. +** exactly matches pIdxKey. ** ** *pRes>0 The cursor is left pointing at an entry that -** is larger than intKey/pIdxKey. +** is larger than pIdxKey. ** -** For index tables, the pIdxKey->eqSeen field is set to 1 if there -** exists an entry in the table that exactly matches pIdxKey. +** The pIdxKey->eqSeen field is set to 1 if there +** exists an entry in the table that exactly matches pIdxKey. */ -int sqlite3BtreeMovetoUnpacked( +int sqlite3BtreeIndexMoveto( BtCursor *pCur, /* The cursor to be moved */ UnpackedRecord *pIdxKey, /* Unpacked index key */ - i64 intKey, /* The table key */ - int biasRight, /* If true, bias the search to the high end */ int *pRes /* Write search results here */ ){ int rc; @@ -5072,52 +6008,77 @@ int sqlite3BtreeMovetoUnpacked( assert( cursorOwnsBtShared(pCur) ); assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); assert( pRes ); - assert( (pIdxKey==0)==(pCur->pKeyInfo==0) ); + assert( pCur->pKeyInfo!=0 ); - /* If the cursor is already positioned at the point we are trying - ** to move to, then just return without doing any work */ - if( pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0 - && pCur->curIntKey +#ifdef SQLITE_DEBUG + pCur->pBtree->nSeek++; /* Performance measurement during testing */ +#endif + + xRecordCompare = sqlite3VdbeFindCompare(pIdxKey); + pIdxKey->errCode = 0; + assert( pIdxKey->default_rc==1 + || pIdxKey->default_rc==0 + || pIdxKey->default_rc==-1 + ); + + + /* Check to see if we can skip a lot of work. Two cases: + ** + ** (1) If the cursor is already pointing to the very last cell + ** in the table and the pIdxKey search key is greater than or + ** equal to that last cell, then no movement is required. + ** + ** (2) If the cursor is on the last page of the table and the first + ** cell on that last page is less than or equal to the pIdxKey + ** search key, then we can start the search on the current page + ** without needing to go back to root. + */ + if( pCur->eState==CURSOR_VALID + && pCur->pPage->leaf + && cursorOnLastPage(pCur) ){ - if( pCur->info.nKey==intKey ){ - *pRes = 0; - return SQLITE_OK; + int c; + if( pCur->ix==pCur->pPage->nCell-1 + && (c = indexCellCompare(pCur->pPage,pCur->ix,pIdxKey,xRecordCompare))<=0 + && pIdxKey->errCode==SQLITE_OK + ){ + *pRes = c; + return SQLITE_OK; /* Cursor already pointing at the correct spot */ } - if( (pCur->curFlags & BTCF_AtLast)!=0 && pCur->info.nKeyiPage>0 + && indexCellCompare(pCur->pPage, 0, pIdxKey, xRecordCompare)<=0 + && pIdxKey->errCode==SQLITE_OK + ){ + pCur->curFlags &= ~(BTCF_ValidOvfl|BTCF_AtLast); + if( !pCur->pPage->isInit ){ + return SQLITE_CORRUPT_BKPT; + } + goto bypass_moveto_root; /* Start search on the current page */ } - } - - if( pIdxKey ){ - xRecordCompare = sqlite3VdbeFindCompare(pIdxKey); - pIdxKey->errCode = 0; - assert( pIdxKey->default_rc==1 - || pIdxKey->default_rc==0 - || pIdxKey->default_rc==-1 - ); - }else{ - xRecordCompare = 0; /* All keys are integers */ + pIdxKey->errCode = SQLITE_OK; } rc = moveToRoot(pCur); if( rc ){ + if( rc==SQLITE_EMPTY ){ + assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 ); + *pRes = -1; + return SQLITE_OK; + } return rc; } - assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage] ); - assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->isInit ); - assert( pCur->eState==CURSOR_INVALID || pCur->apPage[pCur->iPage]->nCell>0 ); - if( pCur->eState==CURSOR_INVALID ){ - *pRes = -1; - assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 ); - return SQLITE_OK; - } - assert( pCur->apPage[0]->intKey==pCur->curIntKey ); - assert( pCur->curIntKey || pIdxKey ); + +bypass_moveto_root: + assert( pCur->pPage ); + assert( pCur->pPage->isInit ); + assert( pCur->eState==CURSOR_VALID ); + assert( pCur->pPage->nCell > 0 ); + assert( pCur->curIntKey==0 ); + assert( pIdxKey!=0 ); for(;;){ int lwr, upr, idx, c; Pgno chldPg; - MemPage *pPage = pCur->apPage[pCur->iPage]; + MemPage *pPage = pCur->pPage; u8 *pCell; /* Pointer to current cell in pPage */ /* pPage->nCell must be greater than zero. If this is the root-page @@ -5127,151 +6088,142 @@ int sqlite3BtreeMovetoUnpacked( ** be the right kind (index or table) of b-tree page. Otherwise ** a moveToChild() or moveToRoot() call would have detected corruption. */ assert( pPage->nCell>0 ); - assert( pPage->intKey==(pIdxKey==0) ); + assert( pPage->intKey==0 ); lwr = 0; upr = pPage->nCell-1; - assert( biasRight==0 || biasRight==1 ); - idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */ - pCur->aiIdx[pCur->iPage] = (u16)idx; - if( xRecordCompare==0 ){ - for(;;){ - i64 nCellKey; - pCell = findCellPastPtr(pPage, idx); - if( pPage->intKeyLeaf ){ - while( 0x80 <= *(pCell++) ){ - if( pCell>=pPage->aDataEnd ) return SQLITE_CORRUPT_BKPT; - } + idx = upr>>1; /* idx = (lwr+upr)/2; */ + for(;;){ + int nCell; /* Size of the pCell cell in bytes */ + pCell = findCellPastPtr(pPage, idx); + + /* The maximum supported page-size is 65536 bytes. This means that + ** the maximum number of record bytes stored on an index B-Tree + ** page is less than 16384 bytes and may be stored as a 2-byte + ** varint. This information is used to attempt to avoid parsing + ** the entire cell by checking for the cases where the record is + ** stored entirely within the b-tree page by inspecting the first + ** 2 bytes of the cell. + */ + nCell = pCell[0]; + if( nCell<=pPage->max1bytePayload ){ + /* This branch runs if the record-size field of the cell is a + ** single byte varint and the record fits entirely on the main + ** b-tree page. */ + testcase( pCell+nCell+1==pPage->aDataEnd ); + c = xRecordCompare(nCell, (void*)&pCell[1], pIdxKey); + }else if( !(pCell[1] & 0x80) + && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal + ){ + /* The record-size field is a 2 byte varint and the record + ** fits entirely on the main b-tree page. */ + testcase( pCell+nCell+2==pPage->aDataEnd ); + c = xRecordCompare(nCell, (void*)&pCell[2], pIdxKey); + }else{ + /* The record flows over onto one or more overflow pages. In + ** this case the whole cell needs to be parsed, a buffer allocated + ** and accessPayload() used to retrieve the record into the + ** buffer before VdbeRecordCompare() can be called. + ** + ** If the record is corrupt, the xRecordCompare routine may read + ** up to two varints past the end of the buffer. An extra 18 + ** bytes of padding is allocated at the end of the buffer in + ** case this happens. */ + void *pCellKey; + u8 * const pCellBody = pCell - pPage->childPtrSize; + const int nOverrun = 18; /* Size of the overrun padding */ + pPage->xParseCell(pPage, pCellBody, &pCur->info); + nCell = (int)pCur->info.nKey; + testcase( nCell<0 ); /* True if key size is 2^32 or more */ + testcase( nCell==0 ); /* Invalid key size: 0x80 0x80 0x00 */ + testcase( nCell==1 ); /* Invalid key size: 0x80 0x80 0x01 */ + testcase( nCell==2 ); /* Minimum legal index key size */ + if( nCell<2 || nCell/pCur->pBt->usableSize>pCur->pBt->nPage ){ + rc = SQLITE_CORRUPT_PAGE(pPage); + goto moveto_index_finish; } - getVarint(pCell, (u64*)&nCellKey); - if( nCellKeyupr ){ c = -1; break; } - }else if( nCellKey>intKey ){ - upr = idx-1; - if( lwr>upr ){ c = +1; break; } - }else{ - assert( nCellKey==intKey ); - pCur->curFlags |= BTCF_ValidNKey; - pCur->info.nKey = nCellKey; - pCur->aiIdx[pCur->iPage] = (u16)idx; - if( !pPage->leaf ){ - lwr = idx; - goto moveto_next_layer; - }else{ - *pRes = 0; - rc = SQLITE_OK; - goto moveto_finish; - } + pCellKey = sqlite3Malloc( (u64)nCell+(u64)nOverrun ); + if( pCellKey==0 ){ + rc = SQLITE_NOMEM_BKPT; + goto moveto_index_finish; } - assert( lwr+upr>=0 ); - idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2; */ - } - }else{ - for(;;){ - int nCell; /* Size of the pCell cell in bytes */ - pCell = findCellPastPtr(pPage, idx); - - /* The maximum supported page-size is 65536 bytes. This means that - ** the maximum number of record bytes stored on an index B-Tree - ** page is less than 16384 bytes and may be stored as a 2-byte - ** varint. This information is used to attempt to avoid parsing - ** the entire cell by checking for the cases where the record is - ** stored entirely within the b-tree page by inspecting the first - ** 2 bytes of the cell. - */ - nCell = pCell[0]; - if( nCell<=pPage->max1bytePayload ){ - /* This branch runs if the record-size field of the cell is a - ** single byte varint and the record fits entirely on the main - ** b-tree page. */ - testcase( pCell+nCell+1==pPage->aDataEnd ); - c = xRecordCompare(nCell, (void*)&pCell[1], pIdxKey); - }else if( !(pCell[1] & 0x80) - && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal - ){ - /* The record-size field is a 2 byte varint and the record - ** fits entirely on the main b-tree page. */ - testcase( pCell+nCell+2==pPage->aDataEnd ); - c = xRecordCompare(nCell, (void*)&pCell[2], pIdxKey); - }else{ - /* The record flows over onto one or more overflow pages. In - ** this case the whole cell needs to be parsed, a buffer allocated - ** and accessPayload() used to retrieve the record into the - ** buffer before VdbeRecordCompare() can be called. - ** - ** If the record is corrupt, the xRecordCompare routine may read - ** up to two varints past the end of the buffer. An extra 18 - ** bytes of padding is allocated at the end of the buffer in - ** case this happens. */ - void *pCellKey; - u8 * const pCellBody = pCell - pPage->childPtrSize; - pPage->xParseCell(pPage, pCellBody, &pCur->info); - nCell = (int)pCur->info.nKey; - testcase( nCell<0 ); /* True if key size is 2^32 or more */ - testcase( nCell==0 ); /* Invalid key size: 0x80 0x80 0x00 */ - testcase( nCell==1 ); /* Invalid key size: 0x80 0x80 0x01 */ - testcase( nCell==2 ); /* Minimum legal index key size */ - if( nCell<2 ){ - rc = SQLITE_CORRUPT_BKPT; - goto moveto_finish; - } - pCellKey = sqlite3Malloc( nCell+18 ); - if( pCellKey==0 ){ - rc = SQLITE_NOMEM; - goto moveto_finish; - } - pCur->aiIdx[pCur->iPage] = (u16)idx; - rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 2); - if( rc ){ - sqlite3_free(pCellKey); - goto moveto_finish; - } - c = xRecordCompare(nCell, pCellKey, pIdxKey); + pCur->ix = (u16)idx; + rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0); + memset(((u8*)pCellKey)+nCell,0,nOverrun); /* Fix uninit warnings */ + pCur->curFlags &= ~BTCF_ValidOvfl; + if( rc ){ sqlite3_free(pCellKey); + goto moveto_index_finish; } - assert( - (pIdxKey->errCode!=SQLITE_CORRUPT || c==0) - && (pIdxKey->errCode!=SQLITE_NOMEM || pCur->pBtree->db->mallocFailed) - ); - if( c<0 ){ - lwr = idx+1; - }else if( c>0 ){ - upr = idx-1; - }else{ - assert( c==0 ); - *pRes = 0; - rc = SQLITE_OK; - pCur->aiIdx[pCur->iPage] = (u16)idx; - if( pIdxKey->errCode ) rc = SQLITE_CORRUPT; - goto moveto_finish; - } - if( lwr>upr ) break; - assert( lwr+upr>=0 ); - idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2 */ + c = sqlite3VdbeRecordCompare(nCell, pCellKey, pIdxKey); + sqlite3_free(pCellKey); + } + assert( + (pIdxKey->errCode!=SQLITE_CORRUPT || c==0) + && (pIdxKey->errCode!=SQLITE_NOMEM || pCur->pBtree->db->mallocFailed) + ); + if( c<0 ){ + lwr = idx+1; + }else if( c>0 ){ + upr = idx-1; + }else{ + assert( c==0 ); + *pRes = 0; + rc = SQLITE_OK; + pCur->ix = (u16)idx; + if( pIdxKey->errCode ) rc = SQLITE_CORRUPT_BKPT; + goto moveto_index_finish; } + if( lwr>upr ) break; + assert( lwr+upr>=0 ); + idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2 */ } assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) ); assert( pPage->isInit ); if( pPage->leaf ){ - assert( pCur->aiIdx[pCur->iPage]apPage[pCur->iPage]->nCell ); - pCur->aiIdx[pCur->iPage] = (u16)idx; + assert( pCur->ixpPage->nCell || CORRUPT_DB ); + pCur->ix = (u16)idx; *pRes = c; rc = SQLITE_OK; - goto moveto_finish; + goto moveto_index_finish; } -moveto_next_layer: if( lwr>=pPage->nCell ){ chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]); }else{ chldPg = get4byte(findCell(pPage, lwr)); } + + /* This block is similar to an in-lined version of: + ** + ** pCur->ix = (u16)lwr; + ** rc = moveToChild(pCur, chldPg); + ** if( rc ) break; + */ + pCur->info.nSize = 0; + pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl); + if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){ + return SQLITE_CORRUPT_BKPT; + } pCur->aiIdx[pCur->iPage] = (u16)lwr; - rc = moveToChild(pCur, chldPg); - if( rc ) break; - } -moveto_finish: + pCur->apPage[pCur->iPage] = pCur->pPage; + pCur->ix = 0; + pCur->iPage++; + rc = getAndInitPage(pCur->pBt, chldPg, &pCur->pPage, pCur->curPagerFlags); + if( rc==SQLITE_OK + && (pCur->pPage->nCell<1 || pCur->pPage->intKey!=pCur->curIntKey) + ){ + releasePage(pCur->pPage); + rc = SQLITE_CORRUPT_PGNO(chldPg); + } + if( rc ){ + pCur->pPage = pCur->apPage[--pCur->iPage]; + break; + } + /* + ***** End of in-lined moveToChild() call */ + } +moveto_index_finish: pCur->info.nSize = 0; - pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl); + assert( (pCur->curFlags & BTCF_ValidOvfl)==0 ); return rc; } @@ -5292,10 +6244,37 @@ int sqlite3BtreeEof(BtCursor *pCur){ } /* -** Advance the cursor to the next entry in the database. If -** successful then set *pRes=0. If the cursor -** was already pointing to the last entry in the database before -** this routine was called, then set *pRes=1. +** Return an estimate for the number of rows in the table that pCur is +** pointing to. Return a negative number if no estimate is currently +** available. +*/ +i64 sqlite3BtreeRowCountEst(BtCursor *pCur){ + i64 n; + u8 i; + + assert( cursorOwnsBtShared(pCur) ); + assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); + + /* Currently this interface is only called by the OP_IfSizeBetween + ** opcode and the OP_Count opcode with P3=1. In either case, + ** the cursor will always be valid unless the btree is empty. */ + if( pCur->eState!=CURSOR_VALID ) return 0; + if( NEVER(pCur->pPage->leaf==0) ) return -1; + + n = pCur->pPage->nCell; + for(i=0; iiPage; i++){ + n *= pCur->apPage[i]->nCell+1; + } + return n; +} + +/* +** Advance the cursor to the next entry in the database. +** Return value: +** +** SQLITE_OK success +** SQLITE_DONE cursor is already pointing at the last element +** otherwise some kind of error occurred ** ** The main entry point is sqlite3BtreeNext(). That routine is optimized ** for the common case of merely incrementing the cell counter BtCursor.aiIdx @@ -5303,23 +6282,18 @@ int sqlite3BtreeEof(BtCursor *pCur){ ** routine is called when it is necessary to move to a different page or ** to restore the cursor. ** -** The calling function will set *pRes to 0 or 1. The initial *pRes value -** will be 1 if the cursor being stepped corresponds to an SQL index and -** if this routine could have been skipped if that SQL index had been -** a unique index. Otherwise the caller will have set *pRes to zero. -** Zero is the common case. The btree implementation is free to use the -** initial *pRes value as a hint to improve performance, but the current -** SQLite btree implementation does not. (Note that the comdb2 btree -** implementation does use this hint, however.) +** If bit 0x01 of the F argument in sqlite3BtreeNext(C,F) is 1, then the +** cursor corresponds to an SQL index and this routine could have been +** skipped if the SQL index had been a unique index. The F argument +** is a hint to the implement. SQLite btree implementation does not use +** this hint, but COMDB2 does. */ -static SQLITE_NOINLINE int btreeNext(BtCursor *pCur, int *pRes){ +static SQLITE_NOINLINE int btreeNext(BtCursor *pCur){ int rc; int idx; MemPage *pPage; assert( cursorOwnsBtShared(pCur) ); - assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID ); - assert( *pRes==0 ); if( pCur->eState!=CURSOR_VALID ){ assert( (pCur->curFlags & BTCF_ValidOvfl)==0 ); rc = restoreCursorPosition(pCur); @@ -5327,30 +6301,20 @@ static SQLITE_NOINLINE int btreeNext(BtCursor *pCur, int *pRes){ return rc; } if( CURSOR_INVALID==pCur->eState ){ - *pRes = 1; - return SQLITE_OK; + return SQLITE_DONE; } - if( pCur->skipNext ){ - assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_SKIPNEXT ); + if( pCur->eState==CURSOR_SKIPNEXT ){ pCur->eState = CURSOR_VALID; - if( pCur->skipNext>0 ){ - pCur->skipNext = 0; - return SQLITE_OK; - } - pCur->skipNext = 0; + if( pCur->skipNext>0 ) return SQLITE_OK; } } - pPage = pCur->apPage[pCur->iPage]; - idx = ++pCur->aiIdx[pCur->iPage]; - assert( pPage->isInit ); - - /* If the database file is corrupt, it is possible for the value of idx - ** to be invalid here. This can only occur if a second cursor modifies - ** the page while cursor pCur is holding a reference to it. Which can - ** only happen if the database is corrupt in such a way as to link the - ** page into more than one b-tree structure. */ - testcase( idx>pPage->nCell ); + pPage = pCur->pPage; + idx = ++pCur->ix; + if( sqlite3FaultSim(412) ) pPage->isInit = 0; + if( !pPage->isInit ){ + return SQLITE_CORRUPT_BKPT; + } if( idx>=pPage->nCell ){ if( !pPage->leaf ){ @@ -5360,15 +6324,14 @@ static SQLITE_NOINLINE int btreeNext(BtCursor *pCur, int *pRes){ } do{ if( pCur->iPage==0 ){ - *pRes = 1; pCur->eState = CURSOR_INVALID; - return SQLITE_OK; + return SQLITE_DONE; } moveToParent(pCur); - pPage = pCur->apPage[pCur->iPage]; - }while( pCur->aiIdx[pCur->iPage]>=pPage->nCell ); + pPage = pCur->pPage; + }while( pCur->ix>=pPage->nCell ); if( pPage->intKey ){ - return sqlite3BtreeNext(pCur, pRes); + return sqlite3BtreeNext(pCur, 0); }else{ return SQLITE_OK; } @@ -5379,20 +6342,18 @@ static SQLITE_NOINLINE int btreeNext(BtCursor *pCur, int *pRes){ return moveToLeftmost(pCur); } } -int sqlite3BtreeNext(BtCursor *pCur, int *pRes){ +int sqlite3BtreeNext(BtCursor *pCur, int flags){ MemPage *pPage; + UNUSED_PARAMETER( flags ); /* Used in COMDB2 but not native SQLite */ assert( cursorOwnsBtShared(pCur) ); - assert( pRes!=0 ); - assert( *pRes==0 || *pRes==1 ); - assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID ); + assert( flags==0 || flags==1 ); pCur->info.nSize = 0; pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl); - *pRes = 0; - if( pCur->eState!=CURSOR_VALID ) return btreeNext(pCur, pRes); - pPage = pCur->apPage[pCur->iPage]; - if( (++pCur->aiIdx[pCur->iPage])>=pPage->nCell ){ - pCur->aiIdx[pCur->iPage]--; - return btreeNext(pCur, pRes); + if( pCur->eState!=CURSOR_VALID ) return btreeNext(pCur); + pPage = pCur->pPage; + if( (++pCur->ix)>=pPage->nCell ){ + pCur->ix--; + return btreeNext(pCur); } if( pPage->leaf ){ return SQLITE_OK; @@ -5402,10 +6363,12 @@ int sqlite3BtreeNext(BtCursor *pCur, int *pRes){ } /* -** Step the cursor to the back to the previous entry in the database. If -** successful then set *pRes=0. If the cursor -** was already pointing to the first entry in the database before -** this routine was called, then set *pRes=1. +** Step the cursor to the back to the previous entry in the database. +** Return values: +** +** SQLITE_OK success +** SQLITE_DONE the cursor is already on the first element of the table +** otherwise some kind of error occurred ** ** The main entry point is sqlite3BtreePrevious(). That routine is optimized ** for the common case of merely decrementing the cell counter BtCursor.aiIdx @@ -5413,23 +6376,17 @@ int sqlite3BtreeNext(BtCursor *pCur, int *pRes){ ** helper routine is called when it is necessary to move to a different page ** or to restore the cursor. ** -** The calling function will set *pRes to 0 or 1. The initial *pRes value -** will be 1 if the cursor being stepped corresponds to an SQL index and -** if this routine could have been skipped if that SQL index had been -** a unique index. Otherwise the caller will have set *pRes to zero. -** Zero is the common case. The btree implementation is free to use the -** initial *pRes value as a hint to improve performance, but the current -** SQLite btree implementation does not. (Note that the comdb2 btree -** implementation does use this hint, however.) +** If bit 0x01 of the F argument to sqlite3BtreePrevious(C,F) is 1, then +** the cursor corresponds to an SQL index and this routine could have been +** skipped if the SQL index had been a unique index. The F argument is a +** hint to the implement. The native SQLite btree implementation does not +** use this hint, but COMDB2 does. */ -static SQLITE_NOINLINE int btreePrevious(BtCursor *pCur, int *pRes){ +static SQLITE_NOINLINE int btreePrevious(BtCursor *pCur){ int rc; MemPage *pPage; assert( cursorOwnsBtShared(pCur) ); - assert( pRes!=0 ); - assert( *pRes==0 ); - assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID ); assert( (pCur->curFlags & (BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey))==0 ); assert( pCur->info.nSize==0 ); if( pCur->eState!=CURSOR_VALID ){ @@ -5438,64 +6395,58 @@ static SQLITE_NOINLINE int btreePrevious(BtCursor *pCur, int *pRes){ return rc; } if( CURSOR_INVALID==pCur->eState ){ - *pRes = 1; - return SQLITE_OK; + return SQLITE_DONE; } - if( pCur->skipNext ){ - assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_SKIPNEXT ); + if( CURSOR_SKIPNEXT==pCur->eState ){ pCur->eState = CURSOR_VALID; - if( pCur->skipNext<0 ){ - pCur->skipNext = 0; - return SQLITE_OK; - } - pCur->skipNext = 0; + if( pCur->skipNext<0 ) return SQLITE_OK; } } - pPage = pCur->apPage[pCur->iPage]; - assert( pPage->isInit ); + pPage = pCur->pPage; + if( sqlite3FaultSim(412) ) pPage->isInit = 0; + if( !pPage->isInit ){ + return SQLITE_CORRUPT_BKPT; + } if( !pPage->leaf ){ - int idx = pCur->aiIdx[pCur->iPage]; + int idx = pCur->ix; rc = moveToChild(pCur, get4byte(findCell(pPage, idx))); if( rc ) return rc; rc = moveToRightmost(pCur); }else{ - while( pCur->aiIdx[pCur->iPage]==0 ){ + while( pCur->ix==0 ){ if( pCur->iPage==0 ){ pCur->eState = CURSOR_INVALID; - *pRes = 1; - return SQLITE_OK; + return SQLITE_DONE; } moveToParent(pCur); } assert( pCur->info.nSize==0 ); - assert( (pCur->curFlags & (BTCF_ValidNKey|BTCF_ValidOvfl))==0 ); + assert( (pCur->curFlags & (BTCF_ValidOvfl))==0 ); - pCur->aiIdx[pCur->iPage]--; - pPage = pCur->apPage[pCur->iPage]; + pCur->ix--; + pPage = pCur->pPage; if( pPage->intKey && !pPage->leaf ){ - rc = sqlite3BtreePrevious(pCur, pRes); + rc = sqlite3BtreePrevious(pCur, 0); }else{ rc = SQLITE_OK; } } return rc; } -int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){ +int sqlite3BtreePrevious(BtCursor *pCur, int flags){ assert( cursorOwnsBtShared(pCur) ); - assert( pRes!=0 ); - assert( *pRes==0 || *pRes==1 ); - assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID ); - *pRes = 0; + assert( flags==0 || flags==1 ); + UNUSED_PARAMETER( flags ); /* Used in COMDB2 but not native SQLite */ pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey); pCur->info.nSize = 0; if( pCur->eState!=CURSOR_VALID - || pCur->aiIdx[pCur->iPage]==0 - || pCur->apPage[pCur->iPage]->leaf==0 + || pCur->ix==0 + || pCur->pPage->leaf==0 ){ - return btreePrevious(pCur, pRes); + return btreePrevious(pCur); } - pCur->aiIdx[pCur->iPage]--; + pCur->ix--; return SQLITE_OK; } @@ -5510,7 +6461,7 @@ int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){ ** SQLITE_OK is returned on success. Any other return value indicates ** an error. *ppPage is set to NULL in the event of an error. ** -** If the "nearby" parameter is not 0, then an effort is made to +** If the "nearby" parameter is not 0, then an effort is made to ** locate a page close to the page number "nearby". This can be used in an ** attempt to keep related pages close to each other in the database file, ** which in turn can make database access faster. @@ -5540,8 +6491,8 @@ static int allocateBtreePage( assert( eMode==BTALLOC_ANY || (nearby>0 && IfNotOmitAV(pBt->autoVacuum)) ); pPage1 = pBt->pPage1; mxPage = btreePagecount(pBt); - /* EVIDENCE-OF: R-05119-02637 The 4-byte big-endian integer at offset 36 - ** stores stores the total number of pages on the freelist. */ + /* EVIDENCE-OF: R-21003-45125 The 4-byte big-endian integer at offset 36 + ** stores the total number of pages on the freelist. */ n = get4byte(&pPage1->aData[36]); testcase( n==mxPage-1 ); if( n>=mxPage ){ @@ -5552,7 +6503,7 @@ static int allocateBtreePage( Pgno iTrunk; u8 searchList = 0; /* If the free-list must be searched for 'nearby' */ u32 nSearch = 0; /* Count of the number of search attempts */ - + /* If eMode==BTALLOC_EXACT and a query of the pointer-map ** shows that the page 'nearby' is somewhere on the free-list, then ** the entire-list will be searched for that page. @@ -5601,7 +6552,7 @@ static int allocateBtreePage( } testcase( iTrunk==mxPage ); if( iTrunk>mxPage || nSearch++ > n ){ - rc = SQLITE_CORRUPT_BKPT; + rc = SQLITE_CORRUPT_PGNO(pPrevTrunk ? pPrevTrunk->pgno : 1); }else{ rc = btreeGetUnusedPage(pBt, iTrunk, &pTrunk, 0); } @@ -5615,8 +6566,8 @@ static int allocateBtreePage( ** is the number of leaf page pointers to follow. */ k = get4byte(&pTrunk->aData[4]); if( k==0 && !searchList ){ - /* The trunk has no leaves and the list is not being searched. - ** So extract the trunk page itself and use it as the newly + /* The trunk has no leaves and the list is not being searched. + ** So extract the trunk page itself and use it as the newly ** allocated page */ assert( pPrevTrunk==0 ); rc = sqlite3PagerWrite(pTrunk->pDbPage); @@ -5627,14 +6578,14 @@ static int allocateBtreePage( memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4); *ppPage = pTrunk; pTrunk = 0; - TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); + TRACE(("ALLOCATE: %u trunk - %u free pages left\n", *pPgno, n-1)); }else if( k>(u32)(pBt->usableSize/4 - 2) ){ /* Value of k is out of range. Database corruption */ - rc = SQLITE_CORRUPT_BKPT; + rc = SQLITE_CORRUPT_PGNO(iTrunk); goto end_allocate_page; #ifndef SQLITE_OMIT_AUTOVACUUM - }else if( searchList - && (nearby==iTrunk || (iTrunkaData[0], &pTrunk->aData[0], 4); } }else{ - /* The trunk page is required by the caller but it contains + /* The trunk page is required by the caller but it contains ** pointers to free-list leaves. The first leaf becomes a trunk ** page in this case. */ MemPage *pNewTrunk; Pgno iNewTrunk = get4byte(&pTrunk->aData[8]); - if( iNewTrunk>mxPage ){ - rc = SQLITE_CORRUPT_BKPT; + if( iNewTrunk>mxPage ){ + rc = SQLITE_CORRUPT_PGNO(iTrunk); goto end_allocate_page; } testcase( iNewTrunk==mxPage ); @@ -5693,7 +6644,7 @@ static int allocateBtreePage( } } pTrunk = 0; - TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); + TRACE(("ALLOCATE: %u trunk - %u free pages left\n", *pPgno, n-1)); #endif }else if( k>0 ){ /* Extract a leaf from the trunk */ @@ -5728,18 +6679,18 @@ static int allocateBtreePage( iPage = get4byte(&aData[8+closest*4]); testcase( iPage==mxPage ); - if( iPage>mxPage ){ - rc = SQLITE_CORRUPT_BKPT; + if( iPage>mxPage || iPage<2 ){ + rc = SQLITE_CORRUPT_PGNO(iTrunk); goto end_allocate_page; } testcase( iPage==mxPage ); - if( !searchList - || (iPage==nearby || (iPagepgno, n-1)); rc = sqlite3PagerWrite(pTrunk->pDbPage); if( rc ) goto end_allocate_page; @@ -5775,7 +6726,7 @@ static int allocateBtreePage( ** not set the no-content flag. This causes the pager to load and journal ** the current page content before overwriting it. ** - ** Note that the pager will not actually attempt to load or journal + ** Note that the pager will not actually attempt to load or journal ** content for any page that really does lie past the end of the database ** file on disk. So the effects of disabling the no-content optimization ** here are confined to those pages that lie between the end of the @@ -5795,7 +6746,7 @@ static int allocateBtreePage( ** becomes a new pointer-map page, the second is used by the caller. */ MemPage *pPg = 0; - TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage)); + TRACE(("ALLOCATE: %u from end of file (pointer-map page)\n", pBt->nPage)); assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) ); rc = btreeGetUnusedPage(pBt, pBt->nPage, &pPg, bNoContent); if( rc==SQLITE_OK ){ @@ -5818,10 +6769,10 @@ static int allocateBtreePage( releasePage(*ppPage); *ppPage = 0; } - TRACE(("ALLOCATE: %d from end of file\n", *pPgno)); + TRACE(("ALLOCATE: %u from end of file\n", *pPgno)); } - assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); + assert( CORRUPT_DB || *pPgno!=PENDING_BYTE_PAGE(pBt) ); end_allocate_page: releasePage(pTrunk); @@ -5832,12 +6783,12 @@ static int allocateBtreePage( } /* -** This function is used to add page iPage to the database file free-list. +** This function is used to add page iPage to the database file free-list. ** It is assumed that the page is not already a part of the free-list. ** ** The value passed as the second argument to this function is optional. -** If the caller happens to have a pointer to the MemPage object -** corresponding to page iPage handy, it may pass it as the second value. +** If the caller happens to have a pointer to the MemPage object +** corresponding to page iPage handy, it may pass it as the second value. ** Otherwise, it may pass NULL. ** ** If a pointer to a MemPage object is passed as the second argument, @@ -5845,17 +6796,19 @@ static int allocateBtreePage( */ static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){ MemPage *pTrunk = 0; /* Free-list trunk page */ - Pgno iTrunk = 0; /* Page number of free-list trunk page */ + Pgno iTrunk = 0; /* Page number of free-list trunk page */ MemPage *pPage1 = pBt->pPage1; /* Local reference to page 1 */ MemPage *pPage; /* Page being freed. May be NULL. */ int rc; /* Return Code */ - int nFree; /* Initial number of pages on free-list */ + u32 nFree; /* Initial number of pages on free-list */ assert( sqlite3_mutex_held(pBt->mutex) ); assert( CORRUPT_DB || iPage>1 ); assert( !pMemPage || pMemPage->pgno==iPage ); - if( iPage<2 ) return SQLITE_CORRUPT_BKPT; + if( iPage<2 || iPage>pBt->nPage ){ + return SQLITE_CORRUPT_BKPT; + } if( pMemPage ){ pPage = pMemPage; sqlite3PagerRef(pPage->pDbPage); @@ -5884,7 +6837,7 @@ static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){ /* If the database supports auto-vacuum, write an entry in the pointer-map ** to indicate that the page is free. */ - if( ISAUTOVACUUM ){ + if( ISAUTOVACUUM(pBt) ){ ptrmapPut(pBt, iPage, PTRMAP_FREEPAGE, 0, &rc); if( rc ) goto freepage_out; } @@ -5900,6 +6853,10 @@ static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){ u32 nLeaf; /* Initial number of leaf cells on trunk page */ iTrunk = get4byte(&pPage1->aData[32]); + if( iTrunk>btreePagecount(pBt) ){ + rc = SQLITE_CORRUPT_BKPT; + goto freepage_out; + } rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0); if( rc!=SQLITE_OK ){ goto freepage_out; @@ -5940,14 +6897,14 @@ static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){ } rc = btreeSetHasContent(pBt, iPage); } - TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno)); + TRACE(("FREE-PAGE: %u leaf on trunk page %u\n",pPage->pgno,pTrunk->pgno)); goto freepage_out; } } /* If control flows to this point, then it was not possible to add the ** the page being freed as a leaf page of the first trunk in the free-list. - ** Possibly because the free-list is empty, or possibly because the + ** Possibly because the free-list is empty, or possibly because the ** first trunk in the free-list is full. Either way, the page being freed ** will become the new first trunk page in the free-list. */ @@ -5961,7 +6918,7 @@ static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){ put4byte(pPage->aData, iTrunk); put4byte(&pPage->aData[4], 0); put4byte(&pPage1->aData[32], iPage); - TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", pPage->pgno, iTrunk)); + TRACE(("FREE-PAGE: %u new trunk page replacing %u\n", pPage->pgno, iTrunk)); freepage_out: if( pPage ){ @@ -5978,44 +6935,41 @@ static void freePage(MemPage *pPage, int *pRC){ } /* -** Free any overflow pages associated with the given Cell. Write the -** local Cell size (the number of bytes on the original page, omitting -** overflow) into *pnSize. +** Free the overflow pages associated with the given Cell. */ -static int clearCell( +static SQLITE_NOINLINE int clearCellOverflow( MemPage *pPage, /* The page that contains the Cell */ unsigned char *pCell, /* First byte of the Cell */ - u16 *pnSize /* Write the size of the Cell here */ + CellInfo *pInfo /* Size information about the cell */ ){ - BtShared *pBt = pPage->pBt; - CellInfo info; + BtShared *pBt; Pgno ovflPgno; int rc; int nOvfl; u32 ovflPageSize; assert( sqlite3_mutex_held(pPage->pBt->mutex) ); - pPage->xParseCell(pPage, pCell, &info); - *pnSize = info.nSize; - if( info.nLocal==info.nPayload ){ - return SQLITE_OK; /* No overflow pages. Return without doing anything */ - } - if( pCell+info.nSize-1 > pPage->aData+pPage->maskPage ){ - return SQLITE_CORRUPT_BKPT; /* Cell extends past end of page */ - } - ovflPgno = get4byte(pCell + info.nSize - 4); + assert( pInfo->nLocal!=pInfo->nPayload ); + testcase( pCell + pInfo->nSize == pPage->aDataEnd ); + testcase( pCell + (pInfo->nSize-1) == pPage->aDataEnd ); + if( pCell + pInfo->nSize > pPage->aDataEnd ){ + /* Cell extends past end of page */ + return SQLITE_CORRUPT_PAGE(pPage); + } + ovflPgno = get4byte(pCell + pInfo->nSize - 4); + pBt = pPage->pBt; assert( pBt->usableSize > 4 ); ovflPageSize = pBt->usableSize - 4; - nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize; - assert( nOvfl>0 || - (CORRUPT_DB && (info.nPayload + ovflPageSize)nPayload - pInfo->nLocal + ovflPageSize - 1)/ovflPageSize; + assert( nOvfl>0 || + (CORRUPT_DB && (pInfo->nPayload + ovflPageSize)btreePagecount(pBt) ){ - /* 0 is not a legal page number and page 1 cannot be an - ** overflow page. Therefore if ovflPgno<2 or past the end of the + /* 0 is not a legal page number and page 1 cannot be an + ** overflow page. Therefore if ovflPgno<2 or past the end of the ** file the database must be corrupt. */ return SQLITE_CORRUPT_BKPT; } @@ -6027,11 +6981,11 @@ static int clearCell( if( ( pOvfl || ((pOvfl = btreePageLookup(pBt, ovflPgno))!=0) ) && sqlite3PagerPageRefcount(pOvfl->pDbPage)!=1 ){ - /* There is no reason any cursor should have an outstanding reference + /* There is no reason any cursor should have an outstanding reference ** to an overflow page belonging to a cell that is being deleted/updated. - ** So if there exists more than one reference to this page, then it - ** must not really be an overflow page and the database must be corrupt. - ** It is helpful to detect this before calling freePage2(), as + ** So if there exists more than one reference to this page, then it + ** must not really be an overflow page and the database must be corrupt. + ** It is helpful to detect this before calling freePage2(), as ** freePage2() may zero the page contents if secure-delete mode is ** enabled. If this 'overflow' page happens to be a page that the ** caller is iterating through or using in some other way, this @@ -6051,6 +7005,21 @@ static int clearCell( return SQLITE_OK; } +/* Call xParseCell to compute the size of a cell. If the cell contains +** overflow, then invoke cellClearOverflow to clear out that overflow. +** Store the result code (SQLITE_OK or some error code) in rc. +** +** Implemented as macro to force inlining for performance. +*/ +#define BTREE_CLEAR_CELL(rc, pPage, pCell, sInfo) \ + pPage->xParseCell(pPage, pCell, &sInfo); \ + if( sInfo.nLocal!=sInfo.nPayload ){ \ + rc = clearCellOverflow(pPage, pCell, &sInfo); \ + }else{ \ + rc = SQLITE_OK; \ + } + + /* ** Create the byte sequence used to represent a cell on page pPage ** and write that byte sequence into pCell[]. Overflow pages are @@ -6066,71 +7035,77 @@ static int clearCell( static int fillInCell( MemPage *pPage, /* The page that contains the cell */ unsigned char *pCell, /* Complete text of the cell */ - const void *pKey, i64 nKey, /* The key */ - const void *pData,int nData, /* The data */ - int nZero, /* Extra zero bytes to append to pData */ + const BtreePayload *pX, /* Payload with which to construct the cell */ int *pnSize /* Write cell size here */ ){ int nPayload; const u8 *pSrc; - int nSrc, n, rc; + int nSrc, n, rc, mn; int spaceLeft; - MemPage *pOvfl = 0; - MemPage *pToRelease = 0; + MemPage *pToRelease; unsigned char *pPrior; unsigned char *pPayload; - BtShared *pBt = pPage->pBt; - Pgno pgnoOvfl = 0; + BtShared *pBt; + Pgno pgnoOvfl; int nHeader; assert( sqlite3_mutex_held(pPage->pBt->mutex) ); /* pPage is not necessarily writeable since pCell might be auxiliary ** buffer space that is separate from the pPage buffer area */ - assert( pCellaData || pCell>=&pPage->aData[pBt->pageSize] + assert( pCellaData || pCell>=&pPage->aData[pPage->pBt->pageSize] || sqlite3PagerIswriteable(pPage->pDbPage) ); /* Fill in the header. */ nHeader = pPage->childPtrSize; - nPayload = nData + nZero; - if( pPage->intKeyLeaf ){ + if( pPage->intKey ){ + nPayload = pX->nData + pX->nZero; + pSrc = pX->pData; + nSrc = pX->nData; + assert( pPage->intKeyLeaf ); /* fillInCell() only called for leaves */ nHeader += putVarint32(&pCell[nHeader], nPayload); + nHeader += putVarint(&pCell[nHeader], *(u64*)&pX->nKey); }else{ - assert( nData==0 ); - assert( nZero==0 ); - } - nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey); - - /* Fill in the payload size */ - if( pPage->intKey ){ - pSrc = pData; - nSrc = nData; - nData = 0; - }else{ - assert( nKey<=0x7fffffff && pKey!=0 ); - nPayload = (int)nKey; - pSrc = pKey; - nSrc = (int)nKey; + assert( pX->nKey<=0x7fffffff && pX->pKey!=0 ); + nSrc = nPayload = (int)pX->nKey; + pSrc = pX->pKey; + nHeader += putVarint32(&pCell[nHeader], nPayload); } + + /* Fill in the payload */ + pPayload = &pCell[nHeader]; if( nPayload<=pPage->maxLocal ){ + /* This is the common case where everything fits on the btree page + ** and no overflow pages are required. */ n = nHeader + nPayload; testcase( n==3 ); testcase( n==4 ); - if( n<4 ) n = 4; + if( n<4 ){ + n = 4; + pPayload[nPayload] = 0; + } *pnSize = n; - spaceLeft = nPayload; - pPrior = pCell; - }else{ - int mn = pPage->minLocal; - n = mn + (nPayload - mn) % (pPage->pBt->usableSize - 4); - testcase( n==pPage->maxLocal ); - testcase( n==pPage->maxLocal+1 ); - if( n > pPage->maxLocal ) n = mn; - spaceLeft = n; - *pnSize = n + nHeader + 4; - pPrior = &pCell[nHeader+n]; + assert( nSrc<=nPayload ); + testcase( nSrcminLocal; + n = mn + (nPayload - mn) % (pPage->pBt->usableSize - 4); + testcase( n==pPage->maxLocal ); + testcase( n==pPage->maxLocal+1 ); + if( n > pPage->maxLocal ) n = mn; + spaceLeft = n; + *pnSize = n + nHeader + 4; + pPrior = &pCell[nHeader+n]; + pToRelease = 0; + pgnoOvfl = 0; + pBt = pPage->pBt; /* At this point variables should be set as follows: ** @@ -6144,27 +7119,54 @@ static int fillInCell( ** Use a call to btreeParseCellPtr() to verify that the values above ** were computed correctly. */ -#if SQLITE_DEBUG +#ifdef SQLITE_DEBUG { CellInfo info; pPage->xParseCell(pPage, pCell, &info); assert( nHeader==(int)(info.pPayload - pCell) ); - assert( info.nKey==nKey ); + assert( info.nKey==pX->nKey ); assert( *pnSize == info.nSize ); assert( spaceLeft == info.nLocal ); } #endif /* Write the payload into the local Cell and any extra into overflow pages */ - while( nPayload>0 ){ + while( 1 ){ + n = nPayload; + if( n>spaceLeft ) n = spaceLeft; + + /* If pToRelease is not zero than pPayload points into the data area + ** of pToRelease. Make sure pToRelease is still writeable. */ + assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) ); + + /* If pPayload is part of the data area of pPage, then make sure pPage + ** is still writeable */ + assert( pPayloadaData || pPayload>=&pPage->aData[pBt->pageSize] + || sqlite3PagerIswriteable(pPage->pDbPage) ); + + if( nSrc>=n ){ + memcpy(pPayload, pSrc, n); + }else if( nSrc>0 ){ + n = nSrc; + memcpy(pPayload, pSrc, n); + }else{ + memset(pPayload, 0, n); + } + nPayload -= n; + if( nPayload<=0 ) break; + pPayload += n; + pSrc += n; + nSrc -= n; + spaceLeft -= n; if( spaceLeft==0 ){ + MemPage *pOvfl = 0; #ifndef SQLITE_OMIT_AUTOVACUUM Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */ if( pBt->autoVacuum ){ do{ pgnoOvfl++; - } while( - PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt) + } while( + PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt) ); } #endif @@ -6172,9 +7174,9 @@ static int fillInCell( #ifndef SQLITE_OMIT_AUTOVACUUM /* If the database supports auto-vacuum, and the second or subsequent ** overflow page is being allocated, add an entry to the pointer-map - ** for that page now. + ** for that page now. ** - ** If this is the first overflow page, then write a partial entry + ** If this is the first overflow page, then write a partial entry ** to the pointer-map. If we write nothing to this pointer-map slot, ** then the optimistic overflow chain processing in clearCell() ** may misinterpret the uninitialized values and delete the @@ -6210,34 +7212,6 @@ static int fillInCell( pPayload = &pOvfl->aData[4]; spaceLeft = pBt->usableSize - 4; } - n = nPayload; - if( n>spaceLeft ) n = spaceLeft; - - /* If pToRelease is not zero than pPayload points into the data area - ** of pToRelease. Make sure pToRelease is still writeable. */ - assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) ); - - /* If pPayload is part of the data area of pPage, then make sure pPage - ** is still writeable */ - assert( pPayloadaData || pPayload>=&pPage->aData[pBt->pageSize] - || sqlite3PagerIswriteable(pPage->pDbPage) ); - - if( nSrc>0 ){ - if( n>nSrc ) n = nSrc; - assert( pSrc ); - memcpy(pPayload, pSrc, n); - }else{ - memset(pPayload, 0, n); - } - nPayload -= n; - pPayload += n; - pSrc += n; - nSrc -= n; - spaceLeft -= n; - if( nSrc==0 ){ - nSrc = nData; - pSrc = pData; - } } releasePage(pToRelease); return SQLITE_OK; @@ -6259,18 +7233,20 @@ static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){ int hdr; /* Beginning of the header. 0 most pages. 100 page 1 */ if( *pRC ) return; - - assert( idx>=0 && idxnCell ); + assert( idx>=0 ); + assert( idxnCell ); assert( CORRUPT_DB || sz==cellSize(pPage, idx) ); assert( sqlite3PagerIswriteable(pPage->pDbPage) ); assert( sqlite3_mutex_held(pPage->pBt->mutex) ); + assert( pPage->nFree>=0 ); data = pPage->aData; ptr = &pPage->aCellIdx[2*idx]; + assert( pPage->pBt->usableSize > (u32)(ptr-data) ); pc = get2byte(ptr); hdr = pPage->hdrOffset; - testcase( pc==get2byte(&data[hdr+5]) ); + testcase( pc==(u32)get2byte(&data[hdr+5]) ); testcase( pc+sz==pPage->pBt->usableSize ); - if( pc < (u32)get2byte(&data[hdr+5]) || pc+sz > pPage->pBt->usableSize ){ + if( pc+sz > pPage->pBt->usableSize ){ *pRC = SQLITE_CORRUPT_BKPT; return; } @@ -6287,62 +7263,155 @@ static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){ pPage->nFree = pPage->pBt->usableSize - pPage->hdrOffset - pPage->childPtrSize - 8; }else{ - memmove(ptr, ptr+2, 2*(pPage->nCell - idx)); - put2byte(&data[hdr+3], pPage->nCell); - pPage->nFree += 2; + memmove(ptr, ptr+2, 2*(pPage->nCell - idx)); + put2byte(&data[hdr+3], pPage->nCell); + pPage->nFree += 2; + } +} + +/* +** Insert a new cell on pPage at cell index "i". pCell points to the +** content of the cell. +** +** If the cell content will fit on the page, then put it there. If it +** will not fit, then make a copy of the cell content into pTemp if +** pTemp is not null. Regardless of pTemp, allocate a new entry +** in pPage->apOvfl[] and make it point to the cell content (either +** in pTemp or the original pCell) and also record its index. +** Allocating a new entry in pPage->aCell[] implies that +** pPage->nOverflow is incremented. +** +** The insertCellFast() routine below works exactly the same as +** insertCell() except that it lacks the pTemp and iChild parameters +** which are assumed zero. Other than that, the two routines are the +** same. +** +** Fixes or enhancements to this routine should be reflected in +** insertCellFast()! +*/ +static int insertCell( + MemPage *pPage, /* Page into which we are copying */ + int i, /* New cell becomes the i-th cell of the page */ + u8 *pCell, /* Content of the new cell */ + int sz, /* Bytes of content in pCell */ + u8 *pTemp, /* Temp storage space for pCell, if needed */ + Pgno iChild /* If non-zero, replace first 4 bytes with this value */ +){ + int idx = 0; /* Where to write new cell content in data[] */ + int j; /* Loop counter */ + u8 *data; /* The content of the whole page */ + u8 *pIns; /* The point in pPage->aCellIdx[] where no cell inserted */ + + assert( i>=0 && i<=pPage->nCell+pPage->nOverflow ); + assert( MX_CELL(pPage->pBt)<=10921 ); + assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB ); + assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) ); + assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) ); + assert( sqlite3_mutex_held(pPage->pBt->mutex) ); + assert( sz==pPage->xCellSize(pPage, pCell) || CORRUPT_DB ); + assert( pPage->nFree>=0 ); + assert( iChild>0 ); + if( pPage->nOverflow || sz+2>pPage->nFree ){ + if( pTemp ){ + memcpy(pTemp, pCell, sz); + pCell = pTemp; + } + put4byte(pCell, iChild); + j = pPage->nOverflow++; + /* Comparison against ArraySize-1 since we hold back one extra slot + ** as a contingency. In other words, never need more than 3 overflow + ** slots but 4 are allocated, just to be safe. */ + assert( j < ArraySize(pPage->apOvfl)-1 ); + pPage->apOvfl[j] = pCell; + pPage->aiOvfl[j] = (u16)i; + + /* When multiple overflows occur, they are always sequential and in + ** sorted order. This invariants arise because multiple overflows can + ** only occur when inserting divider cells into the parent page during + ** balancing, and the dividers are adjacent and sorted. + */ + assert( j==0 || pPage->aiOvfl[j-1]<(u16)i ); /* Overflows in sorted order */ + assert( j==0 || i==pPage->aiOvfl[j-1]+1 ); /* Overflows are sequential */ + }else{ + int rc = sqlite3PagerWrite(pPage->pDbPage); + if( NEVER(rc!=SQLITE_OK) ){ + return rc; + } + assert( sqlite3PagerIswriteable(pPage->pDbPage) ); + data = pPage->aData; + assert( &data[pPage->cellOffset]==pPage->aCellIdx ); + rc = allocateSpace(pPage, sz, &idx); + if( rc ){ return rc; } + /* The allocateSpace() routine guarantees the following properties + ** if it returns successfully */ + assert( idx >= 0 ); + assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB ); + assert( idx+sz <= (int)pPage->pBt->usableSize ); + pPage->nFree -= (u16)(2 + sz); + /* In a corrupt database where an entry in the cell index section of + ** a btree page has a value of 3 or less, the pCell value might point + ** as many as 4 bytes in front of the start of the aData buffer for + ** the source page. Make sure this does not cause problems by not + ** reading the first 4 bytes */ + memcpy(&data[idx+4], pCell+4, sz-4); + put4byte(&data[idx], iChild); + pIns = pPage->aCellIdx + i*2; + memmove(pIns+2, pIns, 2*(pPage->nCell - i)); + put2byte(pIns, idx); + pPage->nCell++; + /* increment the cell count */ + if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++; + assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell || CORRUPT_DB ); +#ifndef SQLITE_OMIT_AUTOVACUUM + if( pPage->pBt->autoVacuum ){ + int rc2 = SQLITE_OK; + /* The cell may contain a pointer to an overflow page. If so, write + ** the entry for the overflow page into the pointer map. + */ + ptrmapPutOvflPtr(pPage, pPage, pCell, &rc2); + if( rc2 ) return rc2; + } +#endif } + return SQLITE_OK; } /* -** Insert a new cell on pPage at cell index "i". pCell points to the -** content of the cell. +** This variant of insertCell() assumes that the pTemp and iChild +** parameters are both zero. Use this variant in sqlite3BtreeInsert() +** for performance improvement, and also so that this variant is only +** called from that one place, and is thus inlined, and thus runs must +** faster. ** -** If the cell content will fit on the page, then put it there. If it -** will not fit, then make a copy of the cell content into pTemp if -** pTemp is not null. Regardless of pTemp, allocate a new entry -** in pPage->apOvfl[] and make it point to the cell content (either -** in pTemp or the original pCell) and also record its index. -** Allocating a new entry in pPage->aCell[] implies that -** pPage->nOverflow is incremented. +** Fixes or enhancements to this routine should be reflected into +** the insertCell() routine. */ -static void insertCell( +static int insertCellFast( MemPage *pPage, /* Page into which we are copying */ int i, /* New cell becomes the i-th cell of the page */ u8 *pCell, /* Content of the new cell */ - int sz, /* Bytes of content in pCell */ - u8 *pTemp, /* Temp storage space for pCell, if needed */ - Pgno iChild, /* If non-zero, replace first 4 bytes with this value */ - int *pRC /* Read and write return code from here */ + int sz /* Bytes of content in pCell */ ){ int idx = 0; /* Where to write new cell content in data[] */ int j; /* Loop counter */ u8 *data; /* The content of the whole page */ u8 *pIns; /* The point in pPage->aCellIdx[] where no cell inserted */ - if( *pRC ) return; - assert( i>=0 && i<=pPage->nCell+pPage->nOverflow ); assert( MX_CELL(pPage->pBt)<=10921 ); assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB ); assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) ); assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) ); assert( sqlite3_mutex_held(pPage->pBt->mutex) ); - /* The cell should normally be sized correctly. However, when moving a - ** malformed cell from a leaf page to an interior page, if the cell size - ** wanted to be less than 4 but got rounded up to 4 on the leaf, then size - ** might be less than 8 (leaf-size + pointer) on the interior node. Hence - ** the term after the || in the following assert(). */ - assert( sz==pPage->xCellSize(pPage, pCell) || (sz==8 && iChild>0) ); - if( pPage->nOverflow || sz+2>pPage->nFree ){ - if( pTemp ){ - memcpy(pTemp, pCell, sz); - pCell = pTemp; - } - if( iChild ){ - put4byte(pCell, iChild); - } + assert( sz==pPage->xCellSize(pPage, pCell) || CORRUPT_DB ); + assert( pPage->nFree>=0 ); + assert( pPage->nOverflow==0 ); + if( sz+2>pPage->nFree ){ j = pPage->nOverflow++; - assert( j<(int)(sizeof(pPage->apOvfl)/sizeof(pPage->apOvfl[0])) ); + /* Comparison against ArraySize-1 since we hold back one extra slot + ** as a contingency. In other words, never need more than 3 overflow + ** slots but 4 are allocated, just to be safe. */ + assert( j < ArraySize(pPage->apOvfl)-1 ); pPage->apOvfl[j] = pCell; pPage->aiOvfl[j] = (u16)i; @@ -6356,14 +7425,13 @@ static void insertCell( }else{ int rc = sqlite3PagerWrite(pPage->pDbPage); if( rc!=SQLITE_OK ){ - *pRC = rc; - return; + return rc; } assert( sqlite3PagerIswriteable(pPage->pDbPage) ); data = pPage->aData; assert( &data[pPage->cellOffset]==pPage->aCellIdx ); rc = allocateSpace(pPage, sz, &idx); - if( rc ){ *pRC = rc; return; } + if( rc ){ return rc; } /* The allocateSpace() routine guarantees the following properties ** if it returns successfully */ assert( idx >= 0 ); @@ -6371,30 +7439,109 @@ static void insertCell( assert( idx+sz <= (int)pPage->pBt->usableSize ); pPage->nFree -= (u16)(2 + sz); memcpy(&data[idx], pCell, sz); - if( iChild ){ - put4byte(&data[idx], iChild); - } pIns = pPage->aCellIdx + i*2; memmove(pIns+2, pIns, 2*(pPage->nCell - i)); put2byte(pIns, idx); pPage->nCell++; /* increment the cell count */ if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++; - assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell ); + assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell || CORRUPT_DB ); #ifndef SQLITE_OMIT_AUTOVACUUM if( pPage->pBt->autoVacuum ){ + int rc2 = SQLITE_OK; /* The cell may contain a pointer to an overflow page. If so, write ** the entry for the overflow page into the pointer map. */ - ptrmapPutOvflPtr(pPage, pCell, pRC); + ptrmapPutOvflPtr(pPage, pPage, pCell, &rc2); + if( rc2 ) return rc2; } #endif } + return SQLITE_OK; } +/* +** The following parameters determine how many adjacent pages get involved +** in a balancing operation. NN is the number of neighbors on either side +** of the page that participate in the balancing operation. NB is the +** total number of pages that participate, including the target page and +** NN neighbors on either side. +** +** The minimum value of NN is 1 (of course). Increasing NN above 1 +** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance +** in exchange for a larger degradation in INSERT and UPDATE performance. +** The value of NN appears to give the best results overall. +** +** (Later:) The description above makes it seem as if these values are +** tunable - as if you could change them and recompile and it would all work. +** But that is unlikely. NB has been 3 since the inception of SQLite and +** we have never tested any other value. +*/ +#define NN 1 /* Number of neighbors on either side of pPage */ +#define NB 3 /* (NN*2+1): Total pages involved in the balance */ + /* ** A CellArray object contains a cache of pointers and sizes for a -** consecutive sequence of cells that might be held multiple pages. +** consecutive sequence of cells that might be held on multiple pages. +** +** The cells in this array are the divider cell or cells from the pParent +** page plus up to three child pages. There are a total of nCell cells. +** +** pRef is a pointer to one of the pages that contributes cells. This is +** used to access information such as MemPage.intKey and MemPage.pBt->pageSize +** which should be common to all pages that contribute cells to this array. +** +** apCell[] and szCell[] hold, respectively, pointers to the start of each +** cell and the size of each cell. Some of the apCell[] pointers might refer +** to overflow cells. In other words, some apCel[] pointers might not point +** to content area of the pages. +** +** A szCell[] of zero means the size of that cell has not yet been computed. +** +** The cells come from as many as four different pages: +** +** ----------- +** | Parent | +** ----------- +** / | \ +** / | \ +** --------- --------- --------- +** |Child-1| |Child-2| |Child-3| +** --------- --------- --------- +** +** The order of cells is in the array is for an index btree is: +** +** 1. All cells from Child-1 in order +** 2. The first divider cell from Parent +** 3. All cells from Child-2 in order +** 4. The second divider cell from Parent +** 5. All cells from Child-3 in order +** +** For a table-btree (with rowids) the items 2 and 4 are empty because +** content exists only in leaves and there are no divider cells. +** +** For an index btree, the apEnd[] array holds pointer to the end of page +** for Child-1, the Parent, Child-2, the Parent (again), and Child-3, +** respectively. The ixNx[] array holds the number of cells contained in +** each of these 5 stages, and all stages to the left. Hence: +** +** ixNx[0] = Number of cells in Child-1. +** ixNx[1] = Number of cells in Child-1 plus 1 for first divider. +** ixNx[2] = Number of cells in Child-1 and Child-2 + 1 for 1st divider. +** ixNx[3] = Number of cells in Child-1 and Child-2 + both divider cells +** ixNx[4] = Total number of cells. +** +** For a table-btree, the concept is similar, except only apEnd[0]..apEnd[2] +** are used and they point to the leaf pages only, and the ixNx value are: +** +** ixNx[0] = Number of cells in Child-1. +** ixNx[1] = Number of cells in Child-1 and Child-2. +** ixNx[2] = Total number of cells. +** +** Sometimes when deleting, a child page can have zero cells. In those +** cases, ixNx[] entries with higher indexes, and the corresponding apEnd[] +** entries, shift down. The end result is that each ixNx[] entry should +** be larger than the previous */ typedef struct CellArray CellArray; struct CellArray { @@ -6402,6 +7549,8 @@ struct CellArray { MemPage *pRef; /* Reference page */ u8 **apCell; /* All cells begin balanced */ u16 *szCell; /* Local size of all cells in apCell[] */ + u8 *apEnd[NB*2]; /* MemPage.aDataEnd values */ + int ixNx[NB*2]; /* Index of at which we move to the next apEnd[] */ }; /* @@ -6409,14 +7558,16 @@ struct CellArray { ** computed. */ static void populateCellCache(CellArray *p, int idx, int N){ + MemPage *pRef = p->pRef; + u16 *szCell = p->szCell; assert( idx>=0 && idx+N<=p->nCell ); while( N>0 ){ assert( p->apCell[idx]!=0 ); - if( p->szCell[idx]==0 ){ - p->szCell[idx] = p->pRef->xCellSize(p->pRef, p->apCell[idx]); + if( szCell[idx]==0 ){ + szCell[idx] = pRef->xCellSize(pRef, p->apCell[idx]); }else{ assert( CORRUPT_DB || - p->szCell[idx]==p->pRef->xCellSize(p->pRef, p->apCell[idx]) ); + szCell[idx]==pRef->xCellSize(pRef, p->apCell[idx]) ); } idx++; N--; @@ -6439,53 +7590,78 @@ static u16 cachedCellSize(CellArray *p, int N){ } /* -** Array apCell[] contains pointers to nCell b-tree page cells. The +** Array apCell[] contains pointers to nCell b-tree page cells. The ** szCell[] array contains the size in bytes of each cell. This function ** replaces the current contents of page pPg with the contents of the cell ** array. ** ** Some of the cells in apCell[] may currently be stored in pPg. This -** function works around problems caused by this by making a copy of any +** function works around problems caused by this by making a copy of any ** such cells before overwriting the page data. ** -** The MemPage.nFree field is invalidated by this function. It is the +** The MemPage.nFree field is invalidated by this function. It is the ** responsibility of the caller to set it correctly. */ static int rebuildPage( - MemPage *pPg, /* Edit this page */ + CellArray *pCArray, /* Content to be added to page pPg */ + int iFirst, /* First cell in pCArray to use */ int nCell, /* Final number of cells on page */ - u8 **apCell, /* Array of cells */ - u16 *szCell /* Array of cell sizes */ + MemPage *pPg /* The page to be reconstructed */ ){ const int hdr = pPg->hdrOffset; /* Offset of header on pPg */ u8 * const aData = pPg->aData; /* Pointer to data for pPg */ const int usableSize = pPg->pBt->usableSize; u8 * const pEnd = &aData[usableSize]; - int i; + int i = iFirst; /* Which cell to copy from pCArray*/ + u32 j; /* Start of cell content area */ + int iEnd = i+nCell; /* Loop terminator */ u8 *pCellptr = pPg->aCellIdx; u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager); u8 *pData; + int k; /* Current slot in pCArray->apEnd[] */ + u8 *pSrcEnd; /* Current pCArray->apEnd[k] value */ - i = get2byte(&aData[hdr+5]); - memcpy(&pTmp[i], &aData[i], usableSize - i); + assert( nCell>0 ); + assert( i(u32)usableSize ){ j = 0; } + memcpy(&pTmp[j], &aData[j], usableSize - j); + + assert( pCArray->ixNx[NB*2-1]>i ); + for(k=0; pCArray->ixNx[k]<=i; k++){} + pSrcEnd = pCArray->apEnd[k]; pData = pEnd; - for(i=0; iapCell[i]; + u16 sz = pCArray->szCell[i]; + assert( sz>0 ); + if( SQLITE_WITHIN(pCell,aData+j,pEnd) ){ + if( ((uptr)(pCell+sz))>(uptr)pEnd ) return SQLITE_CORRUPT_BKPT; pCell = &pTmp[pCell - aData]; + }else if( (uptr)(pCell+sz)>(uptr)pSrcEnd + && (uptr)(pCell)<(uptr)pSrcEnd + ){ + return SQLITE_CORRUPT_BKPT; } - pData -= szCell[i]; + + pData -= sz; put2byte(pCellptr, (pData - aData)); pCellptr += 2; if( pData < pCellptr ) return SQLITE_CORRUPT_BKPT; - memcpy(pData, pCell, szCell[i]); - assert( szCell[i]==pPg->xCellSize(pPg, pCell) || CORRUPT_DB ); - testcase( szCell[i]!=pPg->xCellSize(pPg,pCell) ); + memmove(pData, pCell, sz); + assert( sz==pPg->xCellSize(pPg, pCell) || CORRUPT_DB ); + i++; + if( i>=iEnd ) break; + if( pCArray->ixNx[k]<=i ){ + k++; + pSrcEnd = pCArray->apEnd[k]; + } } /* The pPg->nFree field is now set incorrectly. The caller will fix it. */ - pPg->nCell = nCell; + assert( nCell < 10922 ); + pPg->nCell = (u16)nCell; pPg->nOverflow = 0; put2byte(&aData[hdr+1], 0); @@ -6496,12 +7672,11 @@ static int rebuildPage( } /* -** Array apCell[] contains nCell pointers to b-tree cells. Array szCell -** contains the size in bytes of each such cell. This function attempts to -** add the cells stored in the array to page pPg. If it cannot (because -** the page needs to be defragmented before the cells will fit), non-zero -** is returned. Otherwise, if the cells are added successfully, zero is -** returned. +** The pCArray objects contains pointers to b-tree cells and the cell sizes. +** This function attempts to add the cells stored in the array to page pPg. +** If it cannot (because the page needs to be defragmented before the cells +** will fit), non-zero is returned. Otherwise, if the cells are added +** successfully, zero is returned. ** ** Argument pCellptr points to the first entry in the cell-pointer array ** (part of page pPg) to populate. After cell apCell[0] is written to the @@ -6509,7 +7684,7 @@ static int rebuildPage( ** cell in the array. It is the responsibility of the caller to ensure ** that it is safe to overwrite this part of the cell-pointer array. ** -** When this function is called, *ppData points to the start of the +** When this function is called, *ppData points to the start of the ** content area on page pPg. If the size of the content area is extended, ** *ppData is updated to point to the new start of the content area ** before returning. @@ -6517,30 +7692,37 @@ static int rebuildPage( ** Finally, argument pBegin points to the byte immediately following the ** end of the space required by this page for the cell-pointer area (for ** all cells - not just those inserted by the current call). If the content -** area must be extended to before this point in order to accomodate all +** area must be extended to before this point in order to accommodate all ** cells in apCell[], then the cells do not fit and non-zero is returned. */ static int pageInsertArray( MemPage *pPg, /* Page to add cells to */ u8 *pBegin, /* End of cell-pointer array */ - u8 **ppData, /* IN/OUT: Page content -area pointer */ + u8 **ppData, /* IN/OUT: Page content-area pointer */ u8 *pCellptr, /* Pointer to cell-pointer area */ int iFirst, /* Index of first cell to add */ int nCell, /* Number of cells to add to pPg */ CellArray *pCArray /* Array of cells */ ){ - int i; - u8 *aData = pPg->aData; - u8 *pData = *ppData; - int iEnd = iFirst + nCell; + int i = iFirst; /* Loop counter - cell index to insert */ + u8 *aData = pPg->aData; /* Complete page */ + u8 *pData = *ppData; /* Content area. A subset of aData[] */ + int iEnd = iFirst + nCell; /* End of loop. One past last cell to ins */ + int k; /* Current slot in pCArray->apEnd[] */ + u8 *pEnd; /* Maximum extent of cell data */ assert( CORRUPT_DB || pPg->hdrOffset==0 ); /* Never called on page 1 */ - for(i=iFirst; iixNx[NB*2-1]>i ); + for(k=0; pCArray->ixNx[k]<=i ; k++){} + pEnd = pCArray->apEnd[k]; + while( 1 /*Exit by break*/ ){ int sz, rc; u8 *pSlot; - sz = cachedCellSize(pCArray, i); + assert( pCArray->szCell[i]!=0 ); + sz = pCArray->szCell[i]; if( (aData[1]==0 && aData[2]==0) || (pSlot = pageFindSlot(pPg,sz,&rc))==0 ){ + if( (pData - pBegin)apCell[i] will never overlap on a well-formed @@ -6549,20 +7731,33 @@ static int pageInsertArray( assert( (pSlot+sz)<=pCArray->apCell[i] || pSlot>=(pCArray->apCell[i]+sz) || CORRUPT_DB ); + if( (uptr)(pCArray->apCell[i]+sz)>(uptr)pEnd + && (uptr)(pCArray->apCell[i])<(uptr)pEnd + ){ + assert( CORRUPT_DB ); + (void)SQLITE_CORRUPT_BKPT; + return 1; + } memmove(pSlot, pCArray->apCell[i], sz); put2byte(pCellptr, (pSlot - aData)); pCellptr += 2; + i++; + if( i>=iEnd ) break; + if( pCArray->ixNx[k]<=i ){ + k++; + pEnd = pCArray->apEnd[k]; + } } *ppData = pData; return 0; } /* -** Array apCell[] contains nCell pointers to b-tree cells. Array szCell -** contains the size in bytes of each such cell. This function adds the -** space associated with each cell in the array that is currently stored -** within the body of pPg to the pPg free-list. The cell-pointers and other -** fields of the page are not updated. +** The pCArray object contains pointers to b-tree cells and their sizes. +** +** This function adds the space associated with each cell in the array +** that is currently stored within the body of pPg to the pPg free-list. +** The cell-pointers and other fields of the page are not updated. ** ** This function returns the total number of cells added to the free-list. */ @@ -6576,45 +7771,58 @@ static int pageFreeArray( u8 * const pEnd = &aData[pPg->pBt->usableSize]; u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize]; int nRet = 0; - int i; + int i, j; int iEnd = iFirst + nCell; - u8 *pFree = 0; - int szFree = 0; + int nFree = 0; + int aOfst[10]; + int aAfter[10]; for(i=iFirst; iapCell[i]; if( SQLITE_WITHIN(pCell, pStart, pEnd) ){ int sz; + int iAfter; + int iOfst; /* No need to use cachedCellSize() here. The sizes of all cells that ** are to be freed have already been computing while deciding which ** cells need freeing */ sz = pCArray->szCell[i]; assert( sz>0 ); - if( pFree!=(pCell + sz) ){ - if( pFree ){ - assert( pFree>aData && (pFree - aData)<65536 ); - freeSpace(pPg, (u16)(pFree - aData), szFree); + iOfst = (u16)(pCell - aData); + iAfter = iOfst+sz; + for(j=0; jpEnd ) return 0; - }else{ - pFree = pCell; - szFree += sz; + } + if( j>=nFree ){ + if( nFree>=(int)(sizeof(aOfst)/sizeof(aOfst[0])) ){ + for(j=0; jpEnd ) return 0; + nFree++; } nRet++; } } - if( pFree ){ - assert( pFree>aData && (pFree - aData)<65536 ); - freeSpace(pPg, (u16)(pFree - aData), szFree); + for(j=0; jnCell cells starting -** with apCell[iOld]. After balancing, this page should hold nNew cells +** pCArray contains pointers to and sizes of all cells in the page being +** balanced. The current page, pPg, has pPg->nCell cells starting with +** pCArray->apCell[iOld]. After balancing, this page should hold nNew cells ** starting at apCell[iNew]. ** ** This routine makes the necessary adjustments to pPg so that it contains @@ -6646,22 +7854,28 @@ static int editPage( #endif /* Remove cells from the start and end of the page */ + assert( nCell>=0 ); if( iOldnCell) ) return SQLITE_CORRUPT_BKPT; memmove(pPg->aCellIdx, &pPg->aCellIdx[nShift*2], nCell*2); nCell -= nShift; } if( iNewEnd < iOldEnd ){ - nCell -= pageFreeArray(pPg, iNewEnd, iOldEnd - iNewEnd, pCArray); + int nTail = pageFreeArray(pPg, iNewEnd, iOldEnd - iNewEnd, pCArray); + assert( nCell>=nTail ); + nCell -= nTail; } - pData = &aData[get2byteNotZero(&aData[hdr+5])]; + pData = &aData[get2byte(&aData[hdr+5])]; if( pDatapPg->aDataEnd) ) goto editpage_fail; /* Add cells to the start of the page */ if( iNew=0 ); pCellptr = pPg->aCellIdx; memmove(&pCellptr[nAdd*2], pCellptr, nCell*2); if( pageInsertArray( @@ -6676,8 +7890,11 @@ static int editPage( int iCell = (iOld + pPg->aiOvfl[i]) - iNew; if( iCell>=0 && iCellaCellIdx[iCell * 2]; - memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2); + if( nCell>iCell ){ + memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2); + } nCell++; + cachedCellSize(pCArray, iCell+iNew); if( pageInsertArray( pPg, pBegin, &pData, pCellptr, iCell+iNew, 1, pCArray @@ -6686,13 +7903,18 @@ static int editPage( } /* Append cells to the end of the page */ + assert( nCell>=0 ); pCellptr = &pPg->aCellIdx[nCell*2]; if( pageInsertArray( pPg, pBegin, &pData, pCellptr, iNew+nCell, nNew-nCell, pCArray - ) ) goto editpage_fail; + ) + ){ + goto editpage_fail; + } - pPg->nCell = nNew; + assert( nNew < 10922 ); + pPg->nCell = (u16)nNew; pPg->nOverflow = 0; put2byte(&aData[hdr+3], pPg->nCell); @@ -6702,7 +7924,7 @@ static int editPage( for(i=0; iapCell[i+iNew]; int iOff = get2byteAligned(&pPg->aCellIdx[i*2]); - if( pCell>=aData && pCell<&aData[pPg->pBt->usableSize] ){ + if( SQLITE_WITHIN(pCell, aData, &aData[pPg->pBt->usableSize]) ){ pCell = &pTmp[pCell - aData]; } assert( 0==memcmp(pCell, &aData[iOff], @@ -6713,25 +7935,11 @@ static int editPage( return SQLITE_OK; editpage_fail: /* Unable to edit this page. Rebuild it from scratch instead. */ + if( nNew<1 ) return SQLITE_CORRUPT_BKPT; populateCellCache(pCArray, iNew, nNew); - return rebuildPage(pPg, nNew, &pCArray->apCell[iNew], &pCArray->szCell[iNew]); + return rebuildPage(pCArray, iNew, nNew, pPg); } -/* -** The following parameters determine how many adjacent pages get involved -** in a balancing operation. NN is the number of neighbors on either side -** of the page that participate in the balancing operation. NB is the -** total number of pages that participate, including the target page and -** NN neighbors on either side. -** -** The minimum value of NN is 1 (of course). Increasing NN above 1 -** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance -** in exchange for a larger degradation in INSERT and UPDATE performance. -** The value of NN appears to give the best results overall. -*/ -#define NN 1 /* Number of neighbors on either side of pPage */ -#define NB (NN*2+1) /* Total pages involved in the balance */ - #ifndef SQLITE_OMIT_QUICKBALANCE /* @@ -6766,11 +7974,12 @@ static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){ assert( sqlite3_mutex_held(pPage->pBt->mutex) ); assert( sqlite3PagerIswriteable(pParent->pDbPage) ); assert( pPage->nOverflow==1 ); + + if( pPage->nCell==0 ) return SQLITE_CORRUPT_BKPT; /* dbfuzz001.test */ + assert( pPage->nFree>=0 ); + assert( pParent->nFree>=0 ); - /* This error condition is now caught prior to reaching this function */ - if( NEVER(pPage->nCell==0) ) return SQLITE_CORRUPT_BKPT; - - /* Allocate a new page. This page will become the right-sibling of + /* Allocate a new page. This page will become the right-sibling of ** pPage. Make the parent page writable, so that the new divider cell ** may be inserted. If both these operations are successful, proceed. */ @@ -6782,37 +7991,48 @@ static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){ u8 *pCell = pPage->apOvfl[0]; u16 szCell = pPage->xCellSize(pPage, pCell); u8 *pStop; + CellArray b; assert( sqlite3PagerIswriteable(pNew->pDbPage) ); - assert( pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) ); + assert( CORRUPT_DB || pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) ); zeroPage(pNew, PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF); - rc = rebuildPage(pNew, 1, &pCell, &szCell); - if( NEVER(rc) ) return rc; + b.nCell = 1; + b.pRef = pPage; + b.apCell = &pCell; + b.szCell = &szCell; + b.apEnd[0] = pPage->aDataEnd; + b.ixNx[0] = 2; + b.ixNx[NB*2-1] = 0x7fffffff; + rc = rebuildPage(&b, 0, 1, pNew); + if( NEVER(rc) ){ + releasePage(pNew); + return rc; + } pNew->nFree = pBt->usableSize - pNew->cellOffset - 2 - szCell; /* If this is an auto-vacuum database, update the pointer map - ** with entries for the new page, and any pointer from the + ** with entries for the new page, and any pointer from the ** cell on the page to an overflow page. If either of these ** operations fails, the return code is set, but the contents - ** of the parent page are still manipulated by thh code below. + ** of the parent page are still manipulated by the code below. ** That is Ok, at this point the parent page is guaranteed to ** be marked as dirty. Returning an error code will cause a ** rollback, undoing any changes made to the parent page. */ - if( ISAUTOVACUUM ){ + if( ISAUTOVACUUM(pBt) ){ ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno, &rc); if( szCell>pNew->minLocal ){ - ptrmapPutOvflPtr(pNew, pCell, &rc); + ptrmapPutOvflPtr(pNew, pNew, pCell, &rc); } } - + /* Create a divider cell to insert into pParent. The divider cell ** consists of a 4-byte page number (the page number of pPage) and ** a variable length key value (which must be the same value as the ** largest key on pPage). ** - ** To find the largest key value on pPage, first find the right-most - ** cell on pPage. The first two fields of this cell are the + ** To find the largest key value on pPage, first find the right-most + ** cell on pPage. The first two fields of this cell are the ** record-length (a variable length integer at most 32-bits in size) ** and the key value (a variable length integer, may have any value). ** The first of the while(...) loops below skips over the record-length @@ -6826,12 +8046,14 @@ static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){ while( ((*(pOut++) = *(pCell++))&0x80) && pCellnCell, pSpace, (int)(pOut-pSpace), - 0, pPage->pgno, &rc); + if( rc==SQLITE_OK ){ + rc = insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace), + 0, pPage->pgno); + } /* Set the right-child pointer of pParent to point to the new page. */ put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew); - + /* Release the reference to the new page. */ releasePage(pNew); } @@ -6843,7 +8065,7 @@ static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){ #if 0 /* ** This function does not contribute anything to the operation of SQLite. -** it is sometimes activated temporarily while debugging code responsible +** it is sometimes activated temporarily while debugging code responsible ** for setting pointer-map entries. */ static int ptrmapCheckPages(MemPage **apPage, int nPage){ @@ -6858,7 +8080,7 @@ static int ptrmapCheckPages(MemPage **apPage, int nPage){ for(j=0; jnCell; j++){ CellInfo info; u8 *z; - + z = findCell(pPage, j); pPage->xParseCell(pPage, z, &info); if( info.nLocalpgno==1) ? 100 : 0); int rc; int iData; - - + + assert( pFrom->isInit ); assert( pFrom->nFree>=iToHdr ); assert( get2byte(&aFrom[iFromHdr+5]) <= (int)pBt->usableSize ); - + /* Copy the b-tree node content from page pFrom to page pTo. */ iData = get2byte(&aFrom[iFromHdr+5]); memcpy(&aTo[iData], &aFrom[iData], pBt->usableSize-iData); memcpy(&aTo[iToHdr], &aFrom[iFromHdr], pFrom->cellOffset + 2*pFrom->nCell); - + /* Reinitialize page pTo so that the contents of the MemPage structure ** match the new data. The initialization of pTo can actually fail under - ** fairly obscure circumstances, even though it is a copy of initialized + ** fairly obscure circumstances, even though it is a copy of initialized ** page pFrom. */ pTo->isInit = 0; rc = btreeInitPage(pTo); + if( rc==SQLITE_OK ) rc = btreeComputeFreeSpace(pTo); if( rc!=SQLITE_OK ){ *pRC = rc; return; } - + /* If this is an auto-vacuum database, update the pointer-map entries ** for any b-tree or overflow pages that pTo now contains the pointers to. */ - if( ISAUTOVACUUM ){ + if( ISAUTOVACUUM(pBt) ){ *pRC = setChildPtrmaps(pTo); } } @@ -6945,13 +8168,13 @@ static void copyNodeContent(MemPage *pFrom, MemPage *pTo, int *pRC){ ** (hereafter "the page") and up to 2 siblings so that all pages have about the ** same amount of free space. Usually a single sibling on either side of the ** page are used in the balancing, though both siblings might come from one -** side if the page is the first or last child of its parent. If the page +** side if the page is the first or last child of its parent. If the page ** has fewer than 2 siblings (something which can only happen if the page ** is a root page or a child of a root page) then all available siblings ** participate in the balancing. ** -** The number of siblings of the page might be increased or decreased by -** one or two in an effort to keep pages nearly full but not over full. +** The number of siblings of the page might be increased or decreased by +** one or two in an effort to keep pages nearly full but not over full. ** ** Note that when this routine is called, some of the cells on the page ** might not actually be stored in MemPage.aData[]. This can happen @@ -6962,7 +8185,7 @@ static void copyNodeContent(MemPage *pFrom, MemPage *pTo, int *pRC){ ** inserted into or removed from the parent page (pParent). Doing so ** may cause the parent page to become overfull or underfull. If this ** happens, it is the responsibility of the caller to invoke the correct -** balancing routine to fix this problem (see the balance() routine). +** balancing routine to fix this problem (see the balance() routine). ** ** If this routine fails for any reason, it might leave the database ** in a corrupted state. So if this routine fails, the database should @@ -6977,7 +8200,7 @@ static void copyNodeContent(MemPage *pFrom, MemPage *pTo, int *pRC){ ** of the page-size, the aOvflSpace[] buffer is guaranteed to be large ** enough for all overflow cells. ** -** If aOvflSpace is set to a null pointer, this function returns +** If aOvflSpace is set to a null pointer, this function returns ** SQLITE_NOMEM. */ static int balance_nonroot( @@ -7000,7 +8223,7 @@ static int balance_nonroot( int pageFlags; /* Value of pPage->aData[0] */ int iSpace1 = 0; /* First unused byte of aSpace1[] */ int iOvflSpace = 0; /* First unused byte of aOvflSpace[] */ - int szScratch; /* Size of scratch memory requested */ + u64 szScratch; /* Size of scratch memory requested */ MemPage *apOld[NB]; /* pPage and up to two siblings */ MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */ u8 *pRight; /* Location in parent of right-sibling pointer */ @@ -7012,23 +8235,18 @@ static int balance_nonroot( Pgno pgno; /* Temp var to store a page number in */ u8 abDone[NB+2]; /* True after i'th new page is populated */ Pgno aPgno[NB+2]; /* Page numbers of new pages before shuffling */ - Pgno aPgOrder[NB+2]; /* Copy of aPgno[] used for sorting pages */ - u16 aPgFlags[NB+2]; /* flags field of new pages before shuffling */ - CellArray b; /* Parsed information on cells being balanced */ + CellArray b; /* Parsed information on cells being balanced */ memset(abDone, 0, sizeof(abDone)); - b.nCell = 0; - b.apCell = 0; + assert( sizeof(b) - sizeof(b.ixNx) == offsetof(CellArray,ixNx) ); + memset(&b, 0, sizeof(b)-sizeof(b.ixNx[0])); + b.ixNx[NB*2-1] = 0x7fffffff; pBt = pParent->pBt; assert( sqlite3_mutex_held(pBt->mutex) ); assert( sqlite3PagerIswriteable(pParent->pDbPage) ); -#if 0 - TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno)); -#endif - /* At this point pParent may have at most one overflow cell. And if - ** this overflow cell is present, it must be the cell with + ** this overflow cell is present, it must be the cell with ** index iParentIdx. This scenario comes about when this function ** is called (indirectly) from sqlite3BtreeDelete(). */ @@ -7036,14 +8254,15 @@ static int balance_nonroot( assert( pParent->nOverflow==0 || pParent->aiOvfl[0]==iParentIdx ); if( !aOvflSpace ){ - return SQLITE_NOMEM; + return SQLITE_NOMEM_BKPT; } + assert( pParent->nFree>=0 ); - /* Find the sibling pages to balance. Also locate the cells in pParent - ** that divide the siblings. An attempt is made to find NN siblings on - ** either side of pPage. More siblings are taken from one side, however, + /* Find the sibling pages to balance. Also locate the cells in pParent + ** that divide the siblings. An attempt is made to find NN siblings on + ** either side of pPage. More siblings are taken from one side, however, ** if there are fewer than NN siblings on the other side. If pParent - ** has NB or fewer children then all children of pParent are taken. + ** has NB or fewer children then all children of pParent are taken. ** ** This loop also drops the divider cells from the parent page. This ** way, the remainder of the function does not have to deal with any @@ -7055,7 +8274,7 @@ static int balance_nonroot( nxDiv = 0; }else{ assert( bBulk==0 || bBulk==1 ); - if( iParentIdx==0 ){ + if( iParentIdx==0 ){ nxDiv = 0; }else if( iParentIdx==i ){ nxDiv = i-2+bBulk; @@ -7072,15 +8291,24 @@ static int balance_nonroot( } pgno = get4byte(pRight); while( 1 ){ - rc = getAndInitPage(pBt, pgno, &apOld[i], 0, 0); + if( rc==SQLITE_OK ){ + rc = getAndInitPage(pBt, pgno, &apOld[i], 0); + } if( rc ){ memset(apOld, 0, (i+1)*sizeof(MemPage*)); goto balance_cleanup; } - nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow; + if( apOld[i]->nFree<0 ){ + rc = btreeComputeFreeSpace(apOld[i]); + if( rc ){ + memset(apOld, 0, (i)*sizeof(MemPage*)); + goto balance_cleanup; + } + } + nMaxCells += apOld[i]->nCell + ArraySize(pParent->apOvfl); if( (i--)==0 ) break; - if( i+nxDiv==pParent->aiOvfl[0] && pParent->nOverflow ){ + if( pParent->nOverflow && i+nxDiv==pParent->aiOvfl[0] ){ apDiv[i] = pParent->apOvfl[0]; pgno = get4byte(apDiv[i]); szNew[i] = pParent->xCellSize(pParent, apDiv[i]); @@ -7095,22 +8323,20 @@ static int balance_nonroot( ** This is safe because dropping a cell only overwrites the first ** four bytes of it, and this function does not need the first ** four bytes of the divider cell. So the pointer is safe to use - ** later on. + ** later on. ** ** But not if we are in secure-delete mode. In secure-delete mode, ** the dropCell() routine will overwrite the entire cell with zeroes. ** In this case, temporarily copy the cell into the aOvflSpace[] ** buffer. It will be copied out again as soon as the aSpace[] buffer ** is allocated. */ - if( pBt->btsFlags & BTS_SECURE_DELETE ){ + if( pBt->btsFlags & BTS_FAST_SECURE ){ int iOff; + /* If the following if() condition is not true, the db is corrupted. + ** The call to dropCell() below will detect this. */ iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData); - if( (iOff+szNew[i])>(int)pBt->usableSize ){ - rc = SQLITE_CORRUPT_BKPT; - memset(apOld, 0, (i+1)*sizeof(MemPage*)); - goto balance_cleanup; - }else{ + if( (iOff+szNew[i])<=(int)pBt->usableSize ){ memcpy(&aOvflSpace[iOff], apDiv[i], szNew[i]); apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData]; } @@ -7131,12 +8357,10 @@ static int balance_nonroot( + nMaxCells*sizeof(u16) /* b.szCell */ + pBt->pageSize; /* aSpace1 */ - /* EVIDENCE-OF: R-28375-38319 SQLite will never request a scratch buffer - ** that is more than 6 times the database page size. */ - assert( szScratch<=6*(int)pBt->pageSize ); - b.apCell = sqlite3ScratchMalloc( szScratch ); + assert( szScratch<=7*(int)pBt->pageSize ); + b.apCell = sqlite3StackAllocRaw(0, szScratch ); if( b.apCell==0 ){ - rc = SQLITE_NOMEM; + rc = SQLITE_NOMEM_BKPT; goto balance_cleanup; } b.szCell = (u16*)&b.apCell[nMaxCells]; @@ -7169,17 +8393,18 @@ static int balance_nonroot( u16 maskPage = pOld->maskPage; u8 *piCell = aData + pOld->cellOffset; u8 *piEnd; + VVA_ONLY( int nCellAtStart = b.nCell; ) /* Verify that all sibling pages are of the same "type" (table-leaf, ** table-interior, index-leaf, or index-interior). */ if( pOld->aData[0]!=apOld[0]->aData[0] ){ - rc = SQLITE_CORRUPT_BKPT; + rc = SQLITE_CORRUPT_PAGE(pOld); goto balance_cleanup; } /* Load b.apCell[] with pointers to all cells in pOld. If pOld - ** constains overflow cells, include them in the b.apCell[] array + ** contains overflow cells, include them in the b.apCell[] array ** in the correct spot. ** ** Note that when there are multiple overflow cells, it is always the @@ -7197,6 +8422,10 @@ static int balance_nonroot( */ memset(&b.szCell[b.nCell], 0, sizeof(b.szCell[0])*(limit+pOld->nOverflow)); if( pOld->nOverflow>0 ){ + if( NEVER(limitaiOvfl[0]) ){ + rc = SQLITE_CORRUPT_PAGE(pOld); + goto balance_cleanup; + } limit = pOld->aiOvfl[0]; for(j=0; jnCell+pOld->nOverflow) ); cntOld[i] = b.nCell; if( ileaf ){ assert( leafCorrection==0 ); - assert( pOld->hdrOffset==0 ); + assert( pOld->hdrOffset==0 || CORRUPT_DB ); /* The right pointer of the child page pOld becomes the left ** pointer of the divider cell */ memcpy(b.apCell[b.nCell], &pOld->aData[8], 4); @@ -7256,7 +8486,7 @@ static int balance_nonroot( ** Figure out the number of pages needed to hold all b.nCell cells. ** Store this number in "k". Also compute szNew[] which is the total ** size of all cells on the i-th page and cntNew[] which is the index - ** in b.apCell[] of the cell that divides page i from page i+1. + ** in b.apCell[] of the cell that divides page i from page i+1. ** cntNew[k] should equal b.nCell. ** ** Values computed by this block: @@ -7266,13 +8496,23 @@ static int balance_nonroot( ** cntNew[i]: Index in b.apCell[] and b.szCell[] for the first cell to ** the right of the i-th sibling page. ** usableSpace: Number of bytes of space available on each sibling. - ** + ** */ usableSpace = pBt->usableSize - 12 + leafCorrection; - for(i=0; iaDataEnd; + b.ixNx[k] = cntOld[i]; + if( k && b.ixNx[k]==b.ixNx[k-1] ){ + k--; /* Omit b.ixNx[] entry for child pages with no cells */ + } + if( !leafData ){ + k++; + b.apEnd[k] = pParent->aDataEnd; + b.ixNx[k] = cntOld[i]+1; + } + assert( p->nFree>=0 ); szNew[i] = usableSpace - p->nFree; - if( szNew[i]<0 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; } for(j=0; jnOverflow; j++){ szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]); } @@ -7343,15 +8583,17 @@ static int balance_nonroot( d = r + 1 - leafData; (void)cachedCellSize(&b, d); do{ + int szR, szD; assert( d szLeft-(b.szCell[r]+2)) ){ + && (bBulk || szRight+szD+2 > szLeft-(szR+(i==k-1?0:2)))){ break; } - szRight += b.szCell[d] + 2; - szLeft -= b.szCell[r] + 2; + szRight += szD + 2; + szLeft -= szR + 2; cntNew[i-1] = r; r--; d--; @@ -7364,7 +8606,7 @@ static int balance_nonroot( } } - /* Sanity check: For a non-corrupt database file one of the follwing + /* Sanity check: For a non-corrupt database file one of the following ** must be true: ** (1) We found one or more cells (cntNew[0])>0), or ** (2) pPage is a virtual root page. A virtual root page is when @@ -7372,7 +8614,7 @@ static int balance_nonroot( ** that page. */ assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) || CORRUPT_DB); - TRACE(("BALANCE: old: %d(nc=%d) %d(nc=%d) %d(nc=%d)\n", + TRACE(("BALANCE: old: %u(nc=%u) %u(nc=%u) %u(nc=%u)\n", apOld[0]->pgno, apOld[0]->nCell, nOld>=2 ? apOld[1]->pgno : 0, nOld>=2 ? apOld[1]->nCell : 0, nOld>=3 ? apOld[2]->pgno : 0, nOld>=3 ? apOld[2]->nCell : 0 @@ -7389,6 +8631,11 @@ static int balance_nonroot( apOld[i] = 0; rc = sqlite3PagerWrite(pNew->pDbPage); nNew++; + if( sqlite3PagerPageRefcount(pNew->pDbPage)!=1+(i==(iParentIdx-nxDiv)) + && rc==SQLITE_OK + ){ + rc = SQLITE_CORRUPT_BKPT; + } if( rc ) goto balance_cleanup; }else{ assert( i>0 ); @@ -7400,7 +8647,7 @@ static int balance_nonroot( cntOld[i] = b.nCell; /* Set the pointer-map entry for the new sibling page. */ - if( ISAUTOVACUUM ){ + if( ISAUTOVACUUM(pBt) ){ ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc); if( rc!=SQLITE_OK ){ goto balance_cleanup; @@ -7410,52 +8657,49 @@ static int balance_nonroot( } /* - ** Reassign page numbers so that the new pages are in ascending order. + ** Reassign page numbers so that the new pages are in ascending order. ** This helps to keep entries in the disk file in order so that a scan - ** of the table is closer to a linear scan through the file. That in turn + ** of the table is closer to a linear scan through the file. That in turn ** helps the operating system to deliver pages from the disk more rapidly. ** - ** An O(n^2) insertion sort algorithm is used, but since n is never more - ** than (NB+2) (a small constant), that should not be a problem. + ** An O(N*N) sort algorithm is used, but since N is never more than NB+2 + ** (5), that is not a performance concern. ** - ** When NB==3, this one optimization makes the database about 25% faster + ** When NB==3, this one optimization makes the database about 25% faster ** for large insertions and deletions. */ for(i=0; ipgno; - aPgFlags[i] = apNew[i]->pDbPage->flags; - for(j=0; jpgno; + assert( apNew[i]->pDbPage->flags & PGHDR_WRITEABLE ); + assert( apNew[i]->pDbPage->flags & PGHDR_DIRTY ); } - for(i=0; ii ){ - sqlite3PagerRekey(apNew[iBest]->pDbPage, pBt->nPage+iBest+1, 0); - } - sqlite3PagerRekey(apNew[i]->pDbPage, pgno, aPgFlags[iBest]); - apNew[i]->pgno = pgno; + for(i=0; ipgno < apNew[iB]->pgno ) iB = j; } - } - TRACE(("BALANCE: new: %d(%d nc=%d) %d(%d nc=%d) %d(%d nc=%d) " - "%d(%d nc=%d) %d(%d nc=%d)\n", + /* If apNew[i] has a page number that is bigger than any of the + ** subsequence apNew[i] entries, then swap apNew[i] with the subsequent + ** entry that has the smallest page number (which we know to be + ** entry apNew[iB]). + */ + if( iB!=i ){ + Pgno pgnoA = apNew[i]->pgno; + Pgno pgnoB = apNew[iB]->pgno; + Pgno pgnoTemp = (PENDING_BYTE/pBt->pageSize)+1; + u16 fgA = apNew[i]->pDbPage->flags; + u16 fgB = apNew[iB]->pDbPage->flags; + sqlite3PagerRekey(apNew[i]->pDbPage, pgnoTemp, fgB); + sqlite3PagerRekey(apNew[iB]->pDbPage, pgnoA, fgA); + sqlite3PagerRekey(apNew[i]->pDbPage, pgnoB, fgB); + apNew[i]->pgno = pgnoB; + apNew[iB]->pgno = pgnoA; + } + } + + TRACE(("BALANCE: new: %u(%u nc=%u) %u(%u nc=%u) %u(%u nc=%u) " + "%u(%u nc=%u) %u(%u nc=%u)\n", apNew[0]->pgno, szNew[0], cntNew[0], nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0, nNew>=2 ? cntNew[1] - cntNew[0] - !leafData : 0, @@ -7468,17 +8712,24 @@ static int balance_nonroot( )); assert( sqlite3PagerIswriteable(pParent->pDbPage) ); + assert( nNew>=1 && nNew<=ArraySize(apNew) ); + assert( apNew[nNew-1]!=0 ); put4byte(pRight, apNew[nNew-1]->pgno); /* If the sibling pages are not leaves, ensure that the right-child pointer - ** of the right-most new sibling page is set to the value that was + ** of the right-most new sibling page is set to the value that was ** originally in the same field of the right-most old sibling page. */ if( (pageFlags & PTF_LEAF)==0 && nOld!=nNew ){ - MemPage *pOld = (nNew>nOld ? apNew : apOld)[nOld-1]; + MemPage *pOld; + if( nNew>nOld ){ + pOld = apNew[nOld-1]; + }else{ + pOld = apOld[nOld-1]; + } memcpy(&apNew[nNew-1]->aData[8], &pOld->aData[8], 4); } - /* Make any required updates to pointer map entries associated with + /* Make any required updates to pointer map entries associated with ** cells stored on sibling pages following the balance operation. Pointer ** map entries associated with divider cells are set by the insertCell() ** routine. The associated pointer map entries are: @@ -7489,25 +8740,26 @@ static int balance_nonroot( ** b) if the sibling pages are not leaves, the child page associated ** with the cell. ** - ** If the sibling pages are not leaves, then the pointer map entry - ** associated with the right-child of each sibling may also need to be - ** updated. This happens below, after the sibling pages have been + ** If the sibling pages are not leaves, then the pointer map entry + ** associated with the right-child of each sibling may also need to be + ** updated. This happens below, after the sibling pages have been ** populated, not here. */ - if( ISAUTOVACUUM ){ - MemPage *pNew = apNew[0]; - u8 *aOld = pNew->aData; + if( ISAUTOVACUUM(pBt) ){ + MemPage *pOld; + MemPage *pNew = pOld = apNew[0]; int cntOldNext = pNew->nCell + pNew->nOverflow; - int usableSize = pBt->usableSize; int iNew = 0; int iOld = 0; for(i=0; i=0 && iOldnCell + pOld->nOverflow + !leafData; - aOld = pOld->aData; } if( i==cntNew[iNew] ){ pNew = apNew[++iNew]; @@ -7515,20 +8767,20 @@ static int balance_nonroot( } /* Cell pCell is destined for new sibling page pNew. Originally, it - ** was either part of sibling page iOld (possibly an overflow cell), + ** was either part of sibling page iOld (possibly an overflow cell), ** or else the divider cell to the left of sibling page iOld. So, ** if sibling page iOld had the same page number as pNew, and if ** pCell really was a part of sibling page iOld (not a divider or ** overflow cell), we can skip updating the pointer map entries. */ if( iOld>=nNew || pNew->pgno!=aPgno[iOld] - || !SQLITE_WITHIN(pCell,aOld,&aOld[usableSize]) + || !SQLITE_WITHIN(pCell,pOld->aData,pOld->aDataEnd) ){ if( !leafCorrection ){ ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno, &rc); } if( cachedCellSize(&b,i)>pNew->minLocal ){ - ptrmapPutOvflPtr(pNew, pCell, &rc); + ptrmapPutOvflPtr(pNew, pOld, pCell, &rc); } if( rc ) goto balance_cleanup; } @@ -7540,6 +8792,7 @@ static int balance_nonroot( u8 *pCell; u8 *pTemp; int sz; + u8 *pSrcEnd; MemPage *pNew = apNew[i]; j = cntNew[i]; @@ -7551,9 +8804,9 @@ static int balance_nonroot( if( !pNew->leaf ){ memcpy(&pNew->aData[8], pCell, 4); }else if( leafData ){ - /* If the tree is a leaf-data tree, and the siblings are leaves, - ** then there is no divider cell in b.apCell[]. Instead, the divider - ** cell consists of the integer key for the right-most cell of + /* If the tree is a leaf-data tree, and the siblings are leaves, + ** then there is no divider cell in b.apCell[]. Instead, the divider + ** cell consists of the integer key for the right-most cell of ** the sibling-page assembled above only. */ CellInfo info; @@ -7566,14 +8819,14 @@ static int balance_nonroot( pCell -= 4; /* Obscure case for non-leaf-data trees: If the cell at pCell was ** previously stored on a leaf node, and its reported size was 4 - ** bytes, then it may actually be smaller than this + ** bytes, then it may actually be smaller than this ** (see btreeParseCellPtr(), 4 bytes is the minimum size of - ** any cell). But it is important to pass the correct size to + ** any cell). But it is important to pass the correct size to ** insertCell(), so reparse the cell now. ** - ** Note that this can never happen in an SQLite data file, as all - ** cells are at least 4 bytes. It only happens in b-trees used - ** to evaluate "IN (SELECT ...)" and similar clauses. + ** This can only happen for b-trees used to evaluate "IN (SELECT ...)" + ** and WITHOUT ROWID tables with exactly one column which is the + ** primary key. */ if( b.szCell[j]==4 ){ assert(leafCorrection==4); @@ -7583,7 +8836,14 @@ static int balance_nonroot( iOvflSpace += sz; assert( sz<=pBt->maxLocal+23 ); assert( iOvflSpace <= (int)pBt->pageSize ); - insertCell(pParent, nxDiv+i, pCell, sz, pTemp, pNew->pgno, &rc); + assert( b.ixNx[NB*2-1]>j ); + for(k=0; b.ixNx[k]<=j; k++){} + pSrcEnd = b.apEnd[k]; + if( SQLITE_OVERFLOW(pSrcEnd, pCell, pCell+sz) ){ + rc = SQLITE_CORRUPT_BKPT; + goto balance_cleanup; + } + rc = insertCell(pParent, nxDiv+i, pCell, sz, pTemp, pNew->pgno); if( rc!=SQLITE_OK ) goto balance_cleanup; assert( sqlite3PagerIswriteable(pParent->pDbPage) ); } @@ -7613,6 +8873,8 @@ static int balance_nonroot( for(i=1-nNew; i=0 && iPg=1 || i>=0 ); + assert( iPg=0 /* On the upwards pass, or... */ || cntOld[iPg-1]>=cntNew[iPg-1] /* Condition (1) is true */ @@ -7660,8 +8922,8 @@ static int balance_nonroot( ** b-tree structure by one. This is described as the "balance-shallower" ** sub-algorithm in some documentation. ** - ** If this is an auto-vacuum database, the call to copyNodeContent() - ** sets all pointer-map entries corresponding to database image pages + ** If this is an auto-vacuum database, the call to copyNodeContent() + ** sets all pointer-map entries corresponding to database image pages ** for which the pointer is stored within the content being copied. ** ** It is critical that the child page be defragmented before being @@ -7670,15 +8932,16 @@ static int balance_nonroot( ** free space needs to be up front. */ assert( nNew==1 || CORRUPT_DB ); - rc = defragmentPage(apNew[0]); + rc = defragmentPage(apNew[0], -1); testcase( rc!=SQLITE_OK ); - assert( apNew[0]->nFree == - (get2byte(&apNew[0]->aData[5])-apNew[0]->cellOffset-apNew[0]->nCell*2) + assert( apNew[0]->nFree == + (get2byteNotZero(&apNew[0]->aData[5]) - apNew[0]->cellOffset + - apNew[0]->nCell*2) || rc!=SQLITE_OK ); copyNodeContent(apNew[0], pParent, &rc); freePage(apNew[0], &rc); - }else if( ISAUTOVACUUM && !leafCorrection ){ + }else if( ISAUTOVACUUM(pBt) && !leafCorrection ){ /* Fix the pointer map entries associated with the right-child of each ** sibling page. All other pointer map entries have already been taken ** care of. */ @@ -7689,7 +8952,7 @@ static int balance_nonroot( } assert( pParent->isInit ); - TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n", + TRACE(("BALANCE: finished: old=%u new=%u cells=%u\n", nOld, nNew, b.nCell)); /* Free any old pages that were not reused as new pages. @@ -7699,9 +8962,9 @@ static int balance_nonroot( } #if 0 - if( ISAUTOVACUUM && rc==SQLITE_OK && apNew[0]->isInit ){ + if( ISAUTOVACUUM(pBt) && rc==SQLITE_OK && apNew[0]->isInit ){ /* The ptrmapCheckPages() contains assert() statements that verify that - ** all pointer map pages are set correctly. This is helpful while + ** all pointer map pages are set correctly. This is helpful while ** debugging. This is usually disabled because a corrupt database may ** cause an assert() statement to fail. */ ptrmapCheckPages(apNew, nNew); @@ -7713,7 +8976,7 @@ static int balance_nonroot( ** Cleanup before returning. */ balance_cleanup: - sqlite3ScratchFree(b.apCell); + sqlite3StackFree(0, b.apCell); for(i=0; inOverflow>0 ); assert( sqlite3_mutex_held(pBt->mutex) ); - /* Make pRoot, the root page of the b-tree, writable. Allocate a new + /* Make pRoot, the root page of the b-tree, writable. Allocate a new ** page that will become the new right-child of pPage. Copy the contents ** of the node stored on pRoot into the new child page. */ @@ -7761,7 +9024,7 @@ static int balance_deeper(MemPage *pRoot, MemPage **ppChild){ if( rc==SQLITE_OK ){ rc = allocateBtreePage(pBt,&pChild,&pgnoChild,pRoot->pgno,0); copyNodeContent(pRoot, pChild, &rc); - if( ISAUTOVACUUM ){ + if( ISAUTOVACUUM(pBt) ){ ptrmapPut(pBt, pgnoChild, PTRMAP_BTREE, pRoot->pgno, &rc); } } @@ -7772,9 +9035,9 @@ static int balance_deeper(MemPage *pRoot, MemPage **ppChild){ } assert( sqlite3PagerIswriteable(pChild->pDbPage) ); assert( sqlite3PagerIswriteable(pRoot->pDbPage) ); - assert( pChild->nCell==pRoot->nCell ); + assert( pChild->nCell==pRoot->nCell || CORRUPT_DB ); - TRACE(("BALANCE: copy root %d into %d\n", pRoot->pgno, pChild->pgno)); + TRACE(("BALANCE: copy root %u into %u\n", pRoot->pgno, pChild->pgno)); /* Copy the overflow cells from pRoot to pChild */ memcpy(pChild->aiOvfl, pRoot->aiOvfl, @@ -7791,10 +9054,34 @@ static int balance_deeper(MemPage *pRoot, MemPage **ppChild){ return SQLITE_OK; } +/* +** Return SQLITE_CORRUPT if any cursor other than pCur is currently valid +** on the same B-tree as pCur. +** +** This can occur if a database is corrupt with two or more SQL tables +** pointing to the same b-tree. If an insert occurs on one SQL table +** and causes a BEFORE TRIGGER to do a secondary insert on the other SQL +** table linked to the same b-tree. If the secondary insert causes a +** rebalance, that can change content out from under the cursor on the +** first SQL table, violating invariants on the first insert. +*/ +static int anotherValidCursor(BtCursor *pCur){ + BtCursor *pOther; + for(pOther=pCur->pBt->pCursor; pOther; pOther=pOther->pNext){ + if( pOther!=pCur + && pOther->eState==CURSOR_VALID + && pOther->pPage==pCur->pPage + ){ + return SQLITE_CORRUPT_PAGE(pCur->pPage); + } + } + return SQLITE_OK; +} + /* ** The page that pCur currently points to has just been modified in ** some way. This function figures out if this modification means the -** tree needs to be balanced, and if so calls the appropriate balancing +** tree needs to be balanced, and if so calls the appropriate balancing ** routine. Balancing routines are: ** ** balance_quick() @@ -7803,7 +9090,6 @@ static int balance_deeper(MemPage *pRoot, MemPage **ppChild){ */ static int balance(BtCursor *pCur){ int rc = SQLITE_OK; - const int nMin = pCur->pBt->usableSize * 2 / 3; u8 aBalanceQuickSpace[13]; u8 *pFree = 0; @@ -7811,35 +9097,50 @@ static int balance(BtCursor *pCur){ VVA_ONLY( int balance_deeper_called = 0 ); do { - int iPage = pCur->iPage; - MemPage *pPage = pCur->apPage[iPage]; - - if( iPage==0 ){ - if( pPage->nOverflow ){ + int iPage; + MemPage *pPage = pCur->pPage; + + if( NEVER(pPage->nFree<0) && btreeComputeFreeSpace(pPage) ) break; + if( pPage->nOverflow==0 && pPage->nFree*3<=(int)pCur->pBt->usableSize*2 ){ + /* No rebalance required as long as: + ** (1) There are no overflow cells + ** (2) The amount of free space on the page is less than 2/3rds of + ** the total usable space on the page. */ + break; + }else if( (iPage = pCur->iPage)==0 ){ + if( pPage->nOverflow && (rc = anotherValidCursor(pCur))==SQLITE_OK ){ /* The root page of the b-tree is overfull. In this case call the ** balance_deeper() function to create a new child for the root-page ** and copy the current contents of the root-page to it. The ** next iteration of the do-loop will balance the child page. - */ + */ assert( balance_deeper_called==0 ); VVA_ONLY( balance_deeper_called++ ); rc = balance_deeper(pPage, &pCur->apPage[1]); if( rc==SQLITE_OK ){ pCur->iPage = 1; + pCur->ix = 0; pCur->aiIdx[0] = 0; - pCur->aiIdx[1] = 0; - assert( pCur->apPage[1]->nOverflow ); + pCur->apPage[0] = pPage; + pCur->pPage = pCur->apPage[1]; + assert( pCur->pPage->nOverflow ); } }else{ break; } - }else if( pPage->nOverflow==0 && pPage->nFree<=nMin ){ - break; + }else if( sqlite3PagerPageRefcount(pPage->pDbPage)>1 ){ + /* The page being written is not a root page, and there is currently + ** more than one reference to it. This only happens if the page is one + ** of its own ancestor pages. Corruption. */ + rc = SQLITE_CORRUPT_PAGE(pPage); }else{ MemPage * const pParent = pCur->apPage[iPage-1]; int const iIdx = pCur->aiIdx[iPage-1]; rc = sqlite3PagerWrite(pParent->pDbPage); + if( rc==SQLITE_OK && pParent->nFree<0 ){ + rc = btreeComputeFreeSpace(pParent); + } if( rc==SQLITE_OK ){ #ifndef SQLITE_OMIT_QUICKBALANCE if( pPage->intKeyLeaf @@ -7851,17 +9152,17 @@ static int balance(BtCursor *pCur){ /* Call balance_quick() to create a new sibling of pPage on which ** to store the overflow cell. balance_quick() inserts a new cell ** into pParent, which may cause pParent overflow. If this - ** happens, the next iteration of the do-loop will balance pParent + ** happens, the next iteration of the do-loop will balance pParent ** use either balance_nonroot() or balance_deeper(). Until this ** happens, the overflow cell is stored in the aBalanceQuickSpace[] - ** buffer. + ** buffer. ** ** The purpose of the following assert() is to check that only a ** single call to balance_quick() is made for each call to this ** function. If this were not verified, a subtle bug involving reuse ** of the aBalanceQuickSpace[] might sneak in. */ - assert( balance_quick_called==0 ); + assert( balance_quick_called==0 ); VVA_ONLY( balance_quick_called++ ); rc = balance_quick(pParent, pPage, aBalanceQuickSpace); }else @@ -7872,15 +9173,15 @@ static int balance(BtCursor *pCur){ ** modifying the contents of pParent, which may cause pParent to ** become overfull or underfull. The next iteration of the do-loop ** will balance the parent page to correct this. - ** + ** ** If the parent page becomes overfull, the overflow cell or cells - ** are stored in the pSpace buffer allocated immediately below. + ** are stored in the pSpace buffer allocated immediately below. ** A subsequent iteration of the do-loop will deal with this by ** calling balance_nonroot() (balance_deeper() may be called first, ** but it doesn't deal with overflow cells - just moves them to a - ** different page). Once this subsequent call to balance_nonroot() + ** different page). Once this subsequent call to balance_nonroot() ** has completed, it is safe to release the pSpace buffer used by - ** the previous call, as the overflow cell data will have been + ** the previous call, as the overflow cell data will have been ** copied either into the body of a database page or into the new ** pSpace buffer passed to the latter call to balance_nonroot(). */ @@ -7888,9 +9189,9 @@ static int balance(BtCursor *pCur){ rc = balance_nonroot(pParent, iIdx, pSpace, iPage==1, pCur->hints&BTREE_BULKLOAD); if( pFree ){ - /* If pFree is not NULL, it points to the pSpace buffer used + /* If pFree is not NULL, it points to the pSpace buffer used ** by a previous call to balance_nonroot(). Its contents are - ** now stored either on real database pages or within the + ** now stored either on real database pages or within the ** new pSpace buffer, so it may be safely freed here. */ sqlite3PageFree(pFree); } @@ -7908,6 +9209,7 @@ static int balance(BtCursor *pCur){ releasePage(pPage); pCur->iPage--; assert( pCur->iPage>=0 ); + pCur->pPage = pCur->apPage[pCur->iPage]; } }while( rc==SQLITE_OK ); @@ -7917,121 +9219,359 @@ static int balance(BtCursor *pCur){ return rc; } +/* Overwrite content from pX into pDest. Only do the write if the +** content is different from what is already there. +*/ +static int btreeOverwriteContent( + MemPage *pPage, /* MemPage on which writing will occur */ + u8 *pDest, /* Pointer to the place to start writing */ + const BtreePayload *pX, /* Source of data to write */ + int iOffset, /* Offset of first byte to write */ + int iAmt /* Number of bytes to be written */ +){ + int nData = pX->nData - iOffset; + if( nData<=0 ){ + /* Overwriting with zeros */ + int i; + for(i=0; ipDbPage); + if( rc ) return rc; + memset(pDest + i, 0, iAmt - i); + } + }else{ + if( nDatapData) + iOffset, iAmt)!=0 ){ + int rc = sqlite3PagerWrite(pPage->pDbPage); + if( rc ) return rc; + /* In a corrupt database, it is possible for the source and destination + ** buffers to overlap. This is harmless since the database is already + ** corrupt but it does cause valgrind and ASAN warnings. So use + ** memmove(). */ + memmove(pDest, ((u8*)pX->pData) + iOffset, iAmt); + } + } + return SQLITE_OK; +} + +/* +** Overwrite the cell that cursor pCur is pointing to with fresh content +** contained in pX. In this variant, pCur is pointing to an overflow +** cell. +*/ +static SQLITE_NOINLINE int btreeOverwriteOverflowCell( + BtCursor *pCur, /* Cursor pointing to cell to overwrite */ + const BtreePayload *pX /* Content to write into the cell */ +){ + int iOffset; /* Next byte of pX->pData to write */ + int nTotal = pX->nData + pX->nZero; /* Total bytes of to write */ + int rc; /* Return code */ + MemPage *pPage = pCur->pPage; /* Page being written */ + BtShared *pBt; /* Btree */ + Pgno ovflPgno; /* Next overflow page to write */ + u32 ovflPageSize; /* Size to write on overflow page */ + + assert( pCur->info.nLocalinfo.pPayload, pX, + 0, pCur->info.nLocal); + if( rc ) return rc; + + /* Now overwrite the overflow pages */ + iOffset = pCur->info.nLocal; + assert( nTotal>=0 ); + assert( iOffset>=0 ); + ovflPgno = get4byte(pCur->info.pPayload + iOffset); + pBt = pPage->pBt; + ovflPageSize = pBt->usableSize - 4; + do{ + rc = btreeGetPage(pBt, ovflPgno, &pPage, 0); + if( rc ) return rc; + if( sqlite3PagerPageRefcount(pPage->pDbPage)!=1 || pPage->isInit ){ + rc = SQLITE_CORRUPT_PAGE(pPage); + }else{ + if( iOffset+ovflPageSize<(u32)nTotal ){ + ovflPgno = get4byte(pPage->aData); + }else{ + ovflPageSize = nTotal - iOffset; + } + rc = btreeOverwriteContent(pPage, pPage->aData+4, pX, + iOffset, ovflPageSize); + } + sqlite3PagerUnref(pPage->pDbPage); + if( rc ) return rc; + iOffset += ovflPageSize; + }while( iOffsetnData + pX->nZero; /* Total bytes of to write */ + MemPage *pPage = pCur->pPage; /* Page being written */ + + if( pCur->info.pPayload + pCur->info.nLocal > pPage->aDataEnd + || pCur->info.pPayload < pPage->aData + pPage->cellOffset + ){ + return SQLITE_CORRUPT_PAGE(pPage); + } + if( pCur->info.nLocal==nTotal ){ + /* The entire cell is local */ + return btreeOverwriteContent(pPage, pCur->info.pPayload, pX, + 0, pCur->info.nLocal); + }else{ + /* The cell contains overflow content */ + return btreeOverwriteOverflowCell(pCur, pX); + } +} + /* -** Insert a new record into the BTree. The key is given by (pKey,nKey) -** and the data is given by (pData,nData). The cursor is used only to -** define what table the record should be inserted into. The cursor -** is left pointing at a random location. +** Insert a new record into the BTree. The content of the new record +** is described by the pX object. The pCur cursor is used only to +** define what table the record should be inserted into, and is left +** pointing at a random location. +** +** For a table btree (used for rowid tables), only the pX.nKey value of +** the key is used. The pX.pKey value must be NULL. The pX.nKey is the +** rowid or INTEGER PRIMARY KEY of the row. The pX.nData,pData,nZero fields +** hold the content of the row. ** -** For an INTKEY table, only the nKey value of the key is used. pKey is -** ignored. For a ZERODATA table, the pData and nData are both ignored. +** For an index btree (used for indexes and WITHOUT ROWID tables), the +** key is an arbitrary byte sequence stored in pX.pKey,nKey. The +** pX.pData,nData,nZero fields must be zero. ** ** If the seekResult parameter is non-zero, then a successful call to -** MovetoUnpacked() to seek cursor pCur to (pKey, nKey) has already -** been performed. seekResult is the search result returned (a negative -** number if pCur points at an entry that is smaller than (pKey, nKey), or -** a positive value if pCur points at an entry that is larger than -** (pKey, nKey)). -** -** If the seekResult parameter is non-zero, then the caller guarantees that -** cursor pCur is pointing at the existing copy of a row that is to be -** overwritten. If the seekResult parameter is 0, then cursor pCur may -** point to any entry or to no entry at all and so this function has to seek -** the cursor before the new key can be inserted. +** sqlite3BtreeIndexMoveto() to seek cursor pCur to (pKey,nKey) has already +** been performed. In other words, if seekResult!=0 then the cursor +** is currently pointing to a cell that will be adjacent to the cell +** to be inserted. If seekResult<0 then pCur points to a cell that is +** smaller then (pKey,nKey). If seekResult>0 then pCur points to a cell +** that is larger than (pKey,nKey). +** +** If seekResult==0, that means pCur is pointing at some unknown location. +** In that case, this routine must seek the cursor to the correct insertion +** point for (pKey,nKey) before doing the insertion. For index btrees, +** if pX->nMem is non-zero, then pX->aMem contains pointers to the unpacked +** key values and pX->aMem can be used instead of pX->pKey to avoid having +** to decode the key. */ int sqlite3BtreeInsert( BtCursor *pCur, /* Insert data into the table of this cursor */ - const void *pKey, i64 nKey, /* The key of the new record */ - const void *pData, int nData, /* The data of the new record */ - int nZero, /* Number of extra 0 bytes to append to data */ - int appendBias, /* True if this is likely an append */ - int seekResult /* Result of prior MovetoUnpacked() call */ + const BtreePayload *pX, /* Content of the row to be inserted */ + int flags, /* True if this is likely an append */ + int seekResult /* Result of prior IndexMoveto() call */ ){ int rc; int loc = seekResult; /* -1: before desired location +1: after */ int szNew = 0; - int idx; - MemPage *pPage; - Btree *p = pCur->pBtree; - BtShared *pBt = p->pBt; - unsigned char *oldCell; - unsigned char *newCell = 0; - - if( pCur->eState==CURSOR_FAULT ){ - assert( pCur->skipNext!=SQLITE_OK ); - return pCur->skipNext; - } - - assert( cursorOwnsBtShared(pCur) ); - assert( (pCur->curFlags & BTCF_WriteFlag)!=0 - && pBt->inTransaction==TRANS_WRITE - && (pBt->btsFlags & BTS_READ_ONLY)==0 ); - assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) ); + int idx; + MemPage *pPage; + Btree *p = pCur->pBtree; + unsigned char *oldCell; + unsigned char *newCell = 0; - /* Assert that the caller has been consistent. If this cursor was opened - ** expecting an index b-tree, then the caller should be inserting blob - ** keys with no associated data. If the cursor was opened expecting an - ** intkey table, the caller should be inserting integer keys with a - ** blob of associated data. */ - assert( (pKey==0)==(pCur->pKeyInfo==0) ); + assert( (flags & (BTREE_SAVEPOSITION|BTREE_APPEND|BTREE_PREFORMAT))==flags ); + assert( (flags & BTREE_PREFORMAT)==0 || seekResult || pCur->pKeyInfo==0 ); /* Save the positions of any other cursors open on this table. ** ** In some cases, the call to btreeMoveto() below is a no-op. For ** example, when inserting data into a table with auto-generated integer - ** keys, the VDBE layer invokes sqlite3BtreeLast() to figure out the - ** integer key to use. It then calls this function to actually insert the + ** keys, the VDBE layer invokes sqlite3BtreeLast() to figure out the + ** integer key to use. It then calls this function to actually insert the ** data into the intkey B-Tree. In this case btreeMoveto() recognizes ** that the cursor is already where it needs to be and returns without ** doing any work. To avoid thwarting these optimizations, it is important ** not to clear the cursor here. */ if( pCur->curFlags & BTCF_Multiple ){ - rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur); + rc = saveAllCursors(p->pBt, pCur->pgnoRoot, pCur); if( rc ) return rc; + if( loc && pCur->iPage<0 ){ + /* This can only happen if the schema is corrupt such that there is more + ** than one table or index with the same root page as used by the cursor. + ** Which can only happen if the SQLITE_NoSchemaError flag was set when + ** the schema was loaded. This cannot be asserted though, as a user might + ** set the flag, load the schema, and then unset the flag. */ + return SQLITE_CORRUPT_PGNO(pCur->pgnoRoot); + } + } + + /* Ensure that the cursor is not in the CURSOR_FAULT state and that it + ** points to a valid cell. + */ + if( pCur->eState>=CURSOR_REQUIRESEEK ){ + testcase( pCur->eState==CURSOR_REQUIRESEEK ); + testcase( pCur->eState==CURSOR_FAULT ); + rc = moveToRoot(pCur); + if( rc && rc!=SQLITE_EMPTY ) return rc; } + assert( cursorOwnsBtShared(pCur) ); + assert( (pCur->curFlags & BTCF_WriteFlag)!=0 + && p->pBt->inTransaction==TRANS_WRITE + && (p->pBt->btsFlags & BTS_READ_ONLY)==0 ); + assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) ); + + /* Assert that the caller has been consistent. If this cursor was opened + ** expecting an index b-tree, then the caller should be inserting blob + ** keys with no associated data. If the cursor was opened expecting an + ** intkey table, the caller should be inserting integer keys with a + ** blob of associated data. */ + assert( (flags & BTREE_PREFORMAT) || (pX->pKey==0)==(pCur->pKeyInfo==0) ); + if( pCur->pKeyInfo==0 ){ - assert( pKey==0 ); - /* If this is an insert into a table b-tree, invalidate any incrblob + assert( pX->pKey==0 ); + /* If this is an insert into a table b-tree, invalidate any incrblob ** cursors open on the row being replaced */ - invalidateIncrblobCursors(p, nKey, 0); - - /* If the cursor is currently on the last row and we are appending a - ** new row onto the end, set the "loc" to avoid an unnecessary - ** btreeMoveto() call */ - if( (pCur->curFlags&BTCF_ValidNKey)!=0 && nKey>0 - && pCur->info.nKey==nKey-1 ){ - loc = -1; + if( p->hasIncrblobCur ){ + invalidateIncrblobCursors(p, pCur->pgnoRoot, pX->nKey, 0); + } + + /* If BTREE_SAVEPOSITION is set, the cursor must already be pointing + ** to a row with the same key as the new entry being inserted. + */ +#ifdef SQLITE_DEBUG + if( flags & BTREE_SAVEPOSITION ){ + assert( pCur->curFlags & BTCF_ValidNKey ); + assert( pX->nKey==pCur->info.nKey ); + assert( loc==0 ); + } +#endif + + /* On the other hand, BTREE_SAVEPOSITION==0 does not imply + ** that the cursor is not pointing to a row to be overwritten. + ** So do a complete check. + */ + if( (pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey ){ + /* The cursor is pointing to the entry that is to be + ** overwritten */ + assert( pX->nData>=0 && pX->nZero>=0 ); + if( pCur->info.nSize!=0 + && pCur->info.nPayload==(u32)pX->nData+pX->nZero + ){ + /* New entry is the same size as the old. Do an overwrite */ + return btreeOverwriteCell(pCur, pX); + } + assert( loc==0 ); }else if( loc==0 ){ - rc = sqlite3BtreeMovetoUnpacked(pCur, 0, nKey, appendBias, &loc); + /* The cursor is *not* pointing to the cell to be overwritten, nor + ** to an adjacent cell. Move the cursor so that it is pointing either + ** to the cell to be overwritten or an adjacent cell. + */ + rc = sqlite3BtreeTableMoveto(pCur, pX->nKey, + (flags & BTREE_APPEND)!=0, &loc); if( rc ) return rc; } - }else if( loc==0 ){ - rc = btreeMoveto(pCur, pKey, nKey, appendBias, &loc); - if( rc ) return rc; + }else{ + /* This is an index or a WITHOUT ROWID table */ + + /* If BTREE_SAVEPOSITION is set, the cursor must already be pointing + ** to a row with the same key as the new entry being inserted. + */ + assert( (flags & BTREE_SAVEPOSITION)==0 || loc==0 ); + + /* If the cursor is not already pointing either to the cell to be + ** overwritten, or if a new cell is being inserted, if the cursor is + ** not pointing to an immediately adjacent cell, then move the cursor + ** so that it does. + */ + if( loc==0 && (flags & BTREE_SAVEPOSITION)==0 ){ + if( pX->nMem ){ + UnpackedRecord r; + r.pKeyInfo = pCur->pKeyInfo; + r.aMem = pX->aMem; + r.nField = pX->nMem; + r.default_rc = 0; + r.eqSeen = 0; + rc = sqlite3BtreeIndexMoveto(pCur, &r, &loc); + }else{ + rc = btreeMoveto(pCur, pX->pKey, pX->nKey, + (flags & BTREE_APPEND)!=0, &loc); + } + if( rc ) return rc; + } + + /* If the cursor is currently pointing to an entry to be overwritten + ** and the new content is the same as as the old, then use the + ** overwrite optimization. + */ + if( loc==0 ){ + getCellInfo(pCur); + if( pCur->info.nKey==pX->nKey ){ + BtreePayload x2; + x2.pData = pX->pKey; + x2.nData = (int)pX->nKey; assert( pX->nKey<=0x7fffffff ); + x2.nZero = 0; + return btreeOverwriteCell(pCur, &x2); + } + } } - assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) ); + assert( pCur->eState==CURSOR_VALID + || (pCur->eState==CURSOR_INVALID && loc) || CORRUPT_DB ); - pPage = pCur->apPage[pCur->iPage]; - assert( pPage->intKey || nKey>=0 ); + pPage = pCur->pPage; + assert( pPage->intKey || pX->nKey>=0 || (flags & BTREE_PREFORMAT) ); assert( pPage->leaf || !pPage->intKey ); + if( pPage->nFree<0 ){ + if( NEVER(pCur->eState>CURSOR_INVALID) ){ + /* ^^^^^--- due to the moveToRoot() call above */ + rc = SQLITE_CORRUPT_PAGE(pPage); + }else{ + rc = btreeComputeFreeSpace(pPage); + } + if( rc ) return rc; + } - TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n", - pCur->pgnoRoot, nKey, nData, pPage->pgno, + TRACE(("INSERT: table=%u nkey=%lld ndata=%u page=%u %s\n", + pCur->pgnoRoot, pX->nKey, pX->nData, pPage->pgno, loc==0 ? "overwrite" : "new entry")); - assert( pPage->isInit ); - newCell = pBt->pTmpSpace; + assert( pPage->isInit || CORRUPT_DB ); + newCell = p->pBt->pTmpSpace; assert( newCell!=0 ); - rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew); - if( rc ) goto end_insert; + assert( BTREE_PREFORMAT==OPFLAG_PREFORMAT ); + if( flags & BTREE_PREFORMAT ){ + rc = SQLITE_OK; + szNew = p->pBt->nPreformatSize; + if( szNew<4 ){ + szNew = 4; + newCell[3] = 0; + } + if( ISAUTOVACUUM(p->pBt) && szNew>pPage->maxLocal ){ + CellInfo info; + pPage->xParseCell(pPage, newCell, &info); + if( info.nPayload!=info.nLocal ){ + Pgno ovfl = get4byte(&newCell[szNew-4]); + ptrmapPut(p->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, &rc); + if( NEVER(rc) ) goto end_insert; + } + } + }else{ + rc = fillInCell(pPage, newCell, pX, &szNew); + if( rc ) goto end_insert; + } assert( szNew==pPage->xCellSize(pPage, newCell) ); - assert( szNew <= MX_CELL_SIZE(pBt) ); - idx = pCur->aiIdx[pCur->iPage]; + assert( szNew <= MX_CELL_SIZE(p->pBt) ); + idx = pCur->ix; + pCur->info.nSize = 0; if( loc==0 ){ - u16 szOld; - assert( idxnCell ); + CellInfo info; + assert( idx>=0 ); + if( idx>=pPage->nCell ){ + return SQLITE_CORRUPT_PAGE(pPage); + } rc = sqlite3PagerWrite(pPage->pDbPage); if( rc ){ goto end_insert; @@ -8040,19 +9580,45 @@ int sqlite3BtreeInsert( if( !pPage->leaf ){ memcpy(newCell, oldCell, 4); } - rc = clearCell(pPage, oldCell, &szOld); - dropCell(pPage, idx, szOld, &rc); + BTREE_CLEAR_CELL(rc, pPage, oldCell, info); + testcase( pCur->curFlags & BTCF_ValidOvfl ); + invalidateOverflowCache(pCur); + if( info.nSize==szNew && info.nLocal==info.nPayload + && (!ISAUTOVACUUM(p->pBt) || szNewminLocal) + ){ + /* Overwrite the old cell with the new if they are the same size. + ** We could also try to do this if the old cell is smaller, then add + ** the leftover space to the free list. But experiments show that + ** doing that is no faster then skipping this optimization and just + ** calling dropCell() and insertCell(). + ** + ** This optimization cannot be used on an autovacuum database if the + ** new entry uses overflow pages, as the insertCell() call below is + ** necessary to add the PTRMAP_OVERFLOW1 pointer-map entry. */ + assert( rc==SQLITE_OK ); /* clearCell never fails when nLocal==nPayload */ + if( oldCell < pPage->aData+pPage->hdrOffset+10 ){ + return SQLITE_CORRUPT_PAGE(pPage); + } + if( oldCell+szNew > pPage->aDataEnd ){ + return SQLITE_CORRUPT_PAGE(pPage); + } + memcpy(oldCell, newCell, szNew); + return SQLITE_OK; + } + dropCell(pPage, idx, info.nSize, &rc); if( rc ) goto end_insert; }else if( loc<0 && pPage->nCell>0 ){ assert( pPage->leaf ); - idx = ++pCur->aiIdx[pCur->iPage]; + idx = ++pCur->ix; + pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl); }else{ assert( pPage->leaf ); } - insertCell(pPage, idx, newCell, szNew, 0, 0, &rc); + rc = insertCellFast(pPage, idx, newCell, szNew); + assert( pPage->nOverflow==0 || rc==SQLITE_OK ); assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 ); - /* If no error has occurred and pPage has an overflow cell, call balance() + /* If no error has occurred and pPage has an overflow cell, call balance() ** to redistribute the cells within the tree. Since balance() may move ** the cursor, zero the BtCursor.info.nSize and BTCF_ValidNKey ** variables. @@ -8072,26 +9638,152 @@ int sqlite3BtreeInsert( ** larger than the largest existing key, it is possible to insert the ** row without seeking the cursor. This can be a big performance boost. */ - pCur->info.nSize = 0; - if( rc==SQLITE_OK && pPage->nOverflow ){ - pCur->curFlags &= ~(BTCF_ValidNKey); + if( pPage->nOverflow ){ + assert( rc==SQLITE_OK ); + pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl); rc = balance(pCur); /* Must make sure nOverflow is reset to zero even if the balance() - ** fails. Internal data structure corruption will result otherwise. + ** fails. Internal data structure corruption will result otherwise. ** Also, set the cursor state to invalid. This stops saveCursorPosition() ** from trying to save the current position of the cursor. */ - pCur->apPage[pCur->iPage]->nOverflow = 0; + pCur->pPage->nOverflow = 0; pCur->eState = CURSOR_INVALID; + if( (flags & BTREE_SAVEPOSITION) && rc==SQLITE_OK ){ + btreeReleaseAllCursorPages(pCur); + if( pCur->pKeyInfo ){ + assert( pCur->pKey==0 ); + pCur->pKey = sqlite3Malloc( pX->nKey ); + if( pCur->pKey==0 ){ + rc = SQLITE_NOMEM; + }else{ + memcpy(pCur->pKey, pX->pKey, pX->nKey); + } + } + pCur->eState = CURSOR_REQUIRESEEK; + pCur->nKey = pX->nKey; + } } - assert( pCur->apPage[pCur->iPage]->nOverflow==0 ); + assert( pCur->iPage<0 || pCur->pPage->nOverflow==0 ); end_insert: return rc; } /* -** Delete the entry that the cursor is pointing to. +** This function is used as part of copying the current row from cursor +** pSrc into cursor pDest. If the cursors are open on intkey tables, then +** parameter iKey is used as the rowid value when the record is copied +** into pDest. Otherwise, the record is copied verbatim. +** +** This function does not actually write the new value to cursor pDest. +** Instead, it creates and populates any required overflow pages and +** writes the data for the new cell into the BtShared.pTmpSpace buffer +** for the destination database. The size of the cell, in bytes, is left +** in BtShared.nPreformatSize. The caller completes the insertion by +** calling sqlite3BtreeInsert() with the BTREE_PREFORMAT flag specified. +** +** SQLITE_OK is returned if successful, or an SQLite error code otherwise. +*/ +int sqlite3BtreeTransferRow(BtCursor *pDest, BtCursor *pSrc, i64 iKey){ + BtShared *pBt = pDest->pBt; + u8 *aOut = pBt->pTmpSpace; /* Pointer to next output buffer */ + const u8 *aIn; /* Pointer to next input buffer */ + u32 nIn; /* Size of input buffer aIn[] */ + u32 nRem; /* Bytes of data still to copy */ + + getCellInfo(pSrc); + if( pSrc->info.nPayload<0x80 ){ + *(aOut++) = (u8)pSrc->info.nPayload; + }else{ + aOut += sqlite3PutVarint(aOut, pSrc->info.nPayload); + } + if( pDest->pKeyInfo==0 ) aOut += putVarint(aOut, iKey); + nIn = pSrc->info.nLocal; + aIn = pSrc->info.pPayload; + if( aIn+nIn>pSrc->pPage->aDataEnd ){ + return SQLITE_CORRUPT_PAGE(pSrc->pPage); + } + nRem = pSrc->info.nPayload; + if( nIn==nRem && nInpPage->maxLocal ){ + memcpy(aOut, aIn, nIn); + pBt->nPreformatSize = nIn + (int)(aOut - pBt->pTmpSpace); + return SQLITE_OK; + }else{ + int rc = SQLITE_OK; + Pager *pSrcPager = pSrc->pBt->pPager; + u8 *pPgnoOut = 0; + Pgno ovflIn = 0; + DbPage *pPageIn = 0; + MemPage *pPageOut = 0; + u32 nOut; /* Size of output buffer aOut[] */ + + nOut = btreePayloadToLocal(pDest->pPage, pSrc->info.nPayload); + pBt->nPreformatSize = (int)nOut + (int)(aOut - pBt->pTmpSpace); + if( nOutinfo.nPayload ){ + pPgnoOut = &aOut[nOut]; + pBt->nPreformatSize += 4; + } + + if( nRem>nIn ){ + if( aIn+nIn+4>pSrc->pPage->aDataEnd ){ + return SQLITE_CORRUPT_PAGE(pSrc->pPage); + } + ovflIn = get4byte(&pSrc->info.pPayload[nIn]); + } + + do { + nRem -= nOut; + do{ + assert( nOut>0 ); + if( nIn>0 ){ + int nCopy = MIN(nOut, nIn); + memcpy(aOut, aIn, nCopy); + nOut -= nCopy; + nIn -= nCopy; + aOut += nCopy; + aIn += nCopy; + } + if( nOut>0 ){ + sqlite3PagerUnref(pPageIn); + pPageIn = 0; + rc = sqlite3PagerGet(pSrcPager, ovflIn, &pPageIn, PAGER_GET_READONLY); + if( rc==SQLITE_OK ){ + aIn = (const u8*)sqlite3PagerGetData(pPageIn); + ovflIn = get4byte(aIn); + aIn += 4; + nIn = pSrc->pBt->usableSize - 4; + } + } + }while( rc==SQLITE_OK && nOut>0 ); + + if( rc==SQLITE_OK && nRem>0 && ALWAYS(pPgnoOut) ){ + Pgno pgnoNew; + MemPage *pNew = 0; + rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0); + put4byte(pPgnoOut, pgnoNew); + if( ISAUTOVACUUM(pBt) && pPageOut ){ + ptrmapPut(pBt, pgnoNew, PTRMAP_OVERFLOW2, pPageOut->pgno, &rc); + } + releasePage(pPageOut); + pPageOut = pNew; + if( pPageOut ){ + pPgnoOut = pPageOut->aData; + put4byte(pPgnoOut, 0); + aOut = &pPgnoOut[4]; + nOut = MIN(pBt->usableSize - 4, nRem); + } + } + }while( nRem>0 && rc==SQLITE_OK ); + + releasePage(pPageOut); + sqlite3PagerUnref(pPageIn); + return rc; + } +} + +/* +** Delete the entry that the cursor is pointing to. ** ** If the BTREE_SAVEPOSITION bit of the flags parameter is zero, then ** the cursor is left pointing at an arbitrary location after the delete. @@ -8109,15 +9801,14 @@ int sqlite3BtreeInsert( */ int sqlite3BtreeDelete(BtCursor *pCur, u8 flags){ Btree *p = pCur->pBtree; - BtShared *pBt = p->pBt; - int rc; /* Return code */ - MemPage *pPage; /* Page to delete cell from */ - unsigned char *pCell; /* Pointer to cell to delete */ - int iCellIdx; /* Index of cell to delete */ - int iCellDepth; /* Depth of node containing pCell */ - u16 szCell; /* Size of the cell being deleted */ - int bSkipnext = 0; /* Leaf cursor in SKIPNEXT state */ - u8 bPreserve = flags & BTREE_SAVEPOSITION; /* Keep cursor valid */ + BtShared *pBt = p->pBt; + int rc; /* Return code */ + MemPage *pPage; /* Page to delete cell from */ + unsigned char *pCell; /* Pointer to cell to delete */ + int iCellIdx; /* Index of cell to delete */ + int iCellDepth; /* Depth of node containing pCell */ + CellInfo info; /* Size of the cell being deleted */ + u8 bPreserve; /* Keep cursor valid. 2 for CURSOR_SKIPNEXT */ assert( cursorOwnsBtShared(pCur) ); assert( pBt->inTransaction==TRANS_WRITE ); @@ -8125,14 +9816,63 @@ int sqlite3BtreeDelete(BtCursor *pCur, u8 flags){ assert( pCur->curFlags & BTCF_WriteFlag ); assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) ); assert( !hasReadConflicts(p, pCur->pgnoRoot) ); - assert( pCur->aiIdx[pCur->iPage]apPage[pCur->iPage]->nCell ); - assert( pCur->eState==CURSOR_VALID ); assert( (flags & ~(BTREE_SAVEPOSITION | BTREE_AUXDELETE))==0 ); + if( pCur->eState!=CURSOR_VALID ){ + if( pCur->eState>=CURSOR_REQUIRESEEK ){ + rc = btreeRestoreCursorPosition(pCur); + assert( rc!=SQLITE_OK || CORRUPT_DB || pCur->eState==CURSOR_VALID ); + if( rc || pCur->eState!=CURSOR_VALID ) return rc; + }else{ + return SQLITE_CORRUPT_PGNO(pCur->pgnoRoot); + } + } + assert( pCur->eState==CURSOR_VALID ); iCellDepth = pCur->iPage; - iCellIdx = pCur->aiIdx[iCellDepth]; - pPage = pCur->apPage[iCellDepth]; + iCellIdx = pCur->ix; + pPage = pCur->pPage; + if( pPage->nCell<=iCellIdx ){ + return SQLITE_CORRUPT_PAGE(pPage); + } pCell = findCell(pPage, iCellIdx); + if( pPage->nFree<0 && btreeComputeFreeSpace(pPage) ){ + return SQLITE_CORRUPT_PAGE(pPage); + } + if( pCell<&pPage->aCellIdx[pPage->nCell] ){ + return SQLITE_CORRUPT_PAGE(pPage); + } + + /* If the BTREE_SAVEPOSITION bit is on, then the cursor position must + ** be preserved following this delete operation. If the current delete + ** will cause a b-tree rebalance, then this is done by saving the cursor + ** key and leaving the cursor in CURSOR_REQUIRESEEK state before + ** returning. + ** + ** If the current delete will not cause a rebalance, then the cursor + ** will be left in CURSOR_SKIPNEXT state pointing to the entry immediately + ** before or after the deleted entry. + ** + ** The bPreserve value records which path is required: + ** + ** bPreserve==0 Not necessary to save the cursor position + ** bPreserve==1 Use CURSOR_REQUIRESEEK to save the cursor position + ** bPreserve==2 Cursor won't move. Set CURSOR_SKIPNEXT. + */ + bPreserve = (flags & BTREE_SAVEPOSITION)!=0; + if( bPreserve ){ + if( !pPage->leaf + || (pPage->nFree+pPage->xCellSize(pPage,pCell)+2) > + (int)(pBt->usableSize*2/3) + || pPage->nCell==1 /* See dbfuzz001.test for a test case */ + ){ + /* A b-tree rebalance will be required after deleting this entry. + ** Save the cursor key. */ + rc = saveCursorKey(pCur); + if( rc ) return rc; + }else{ + bPreserve = 2; + } + } /* If the page containing the entry to delete is not a leaf page, move ** the cursor to the largest entry in the tree that is smaller than @@ -8142,8 +9882,8 @@ int sqlite3BtreeDelete(BtCursor *pCur, u8 flags){ ** sub-tree headed by the child page of the cell being deleted. This makes ** balancing the tree following the delete operation easier. */ if( !pPage->leaf ){ - int notUsed = 0; - rc = sqlite3BtreePrevious(pCur, ¬Used); + rc = sqlite3BtreePrevious(pCur, 0); + assert( rc!=SQLITE_DONE ); if( rc ) return rc; } @@ -8156,30 +9896,8 @@ int sqlite3BtreeDelete(BtCursor *pCur, u8 flags){ /* If this is a delete operation to remove a row from a table b-tree, ** invalidate any incrblob cursors open on the row being deleted. */ - if( pCur->pKeyInfo==0 ){ - invalidateIncrblobCursors(p, pCur->info.nKey, 0); - } - - /* If the bPreserve flag is set to true, then the cursor position must - ** be preserved following this delete operation. If the current delete - ** will cause a b-tree rebalance, then this is done by saving the cursor - ** key and leaving the cursor in CURSOR_REQUIRESEEK state before - ** returning. - ** - ** Or, if the current delete will not cause a rebalance, then the cursor - ** will be left in CURSOR_SKIPNEXT state pointing to the entry immediately - ** before or after the deleted entry. In this case set bSkipnext to true. */ - if( bPreserve ){ - if( !pPage->leaf - || (pPage->nFree+cellSizePtr(pPage,pCell)+2)>(int)(pBt->usableSize*2/3) - ){ - /* A b-tree rebalance will be required after deleting this entry. - ** Save the cursor key. */ - rc = saveCursorKey(pCur); - if( rc ) return rc; - }else{ - bSkipnext = 1; - } + if( pCur->pKeyInfo==0 && p->hasIncrblobCur ){ + invalidateIncrblobCursors(p, pCur->pgnoRoot, pCur->info.nKey, 0); } /* Make the page containing the entry to be deleted writable. Then free any @@ -8187,8 +9905,8 @@ int sqlite3BtreeDelete(BtCursor *pCur, u8 flags){ ** itself from within the page. */ rc = sqlite3PagerWrite(pPage->pDbPage); if( rc ) return rc; - rc = clearCell(pPage, pCell, &szCell); - dropCell(pPage, iCellIdx, szCell, &rc); + BTREE_CLEAR_CELL(rc, pPage, pCell, info); + dropCell(pPage, iCellIdx, info.nSize, &rc); if( rc ) return rc; /* If the cell deleted was not located on a leaf page, then the cursor @@ -8197,19 +9915,30 @@ int sqlite3BtreeDelete(BtCursor *pCur, u8 flags){ ** node. The cell from the leaf node needs to be moved to the internal ** node to replace the deleted cell. */ if( !pPage->leaf ){ - MemPage *pLeaf = pCur->apPage[pCur->iPage]; + MemPage *pLeaf = pCur->pPage; int nCell; - Pgno n = pCur->apPage[iCellDepth+1]->pgno; + Pgno n; unsigned char *pTmp; + if( pLeaf->nFree<0 ){ + rc = btreeComputeFreeSpace(pLeaf); + if( rc ) return rc; + } + if( iCellDepthiPage-1 ){ + n = pCur->apPage[iCellDepth+1]->pgno; + }else{ + n = pCur->pPage->pgno; + } pCell = findCell(pLeaf, pLeaf->nCell-1); - if( pCell<&pLeaf->aData[4] ) return SQLITE_CORRUPT_BKPT; + if( pCell<&pLeaf->aData[4] ) return SQLITE_CORRUPT_PAGE(pLeaf); nCell = pLeaf->xCellSize(pLeaf, pCell); assert( MX_CELL_SIZE(pBt) >= nCell ); pTmp = pBt->pTmpSpace; assert( pTmp!=0 ); rc = sqlite3PagerWrite(pLeaf->pDbPage); - insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc); + if( rc==SQLITE_OK ){ + rc = insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n); + } dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc); if( rc ) return rc; } @@ -8227,33 +9956,46 @@ int sqlite3BtreeDelete(BtCursor *pCur, u8 flags){ ** on the leaf node first. If the balance proceeds far enough up the ** tree that we can be sure that any problem in the internal node has ** been corrected, so be it. Otherwise, after balancing the leaf node, - ** walk the cursor up the tree to the internal node and balance it as + ** walk the cursor up the tree to the internal node and balance it as ** well. */ - rc = balance(pCur); + assert( pCur->pPage->nOverflow==0 ); + assert( pCur->pPage->nFree>=0 ); + if( pCur->pPage->nFree*3<=(int)pCur->pBt->usableSize*2 ){ + /* Optimization: If the free space is less than 2/3rds of the page, + ** then balance() will always be a no-op. No need to invoke it. */ + rc = SQLITE_OK; + }else{ + rc = balance(pCur); + } if( rc==SQLITE_OK && pCur->iPage>iCellDepth ){ + releasePageNotNull(pCur->pPage); + pCur->iPage--; while( pCur->iPage>iCellDepth ){ releasePage(pCur->apPage[pCur->iPage--]); } + pCur->pPage = pCur->apPage[pCur->iPage]; rc = balance(pCur); } if( rc==SQLITE_OK ){ - if( bSkipnext ){ - assert( bPreserve && (pCur->iPage==iCellDepth || CORRUPT_DB) ); - assert( pPage==pCur->apPage[pCur->iPage] || CORRUPT_DB ); + if( bPreserve>1 ){ + assert( (pCur->iPage==iCellDepth || CORRUPT_DB) ); + assert( pPage==pCur->pPage || CORRUPT_DB ); assert( (pPage->nCell>0 || CORRUPT_DB) && iCellIdx<=pPage->nCell ); pCur->eState = CURSOR_SKIPNEXT; if( iCellIdx>=pPage->nCell ){ pCur->skipNext = -1; - pCur->aiIdx[iCellDepth] = pPage->nCell-1; + pCur->ix = pPage->nCell-1; }else{ pCur->skipNext = 1; } }else{ rc = moveToRoot(pCur); if( bPreserve ){ + btreeReleaseAllCursorPages(pCur); pCur->eState = CURSOR_REQUIRESEEK; } + if( rc==SQLITE_EMPTY ) rc = SQLITE_OK; } } return rc; @@ -8270,12 +10012,12 @@ int sqlite3BtreeDelete(BtCursor *pCur, u8 flags){ ** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys ** BTREE_ZERODATA Used for SQL indices */ -static int btreeCreateTable(Btree *p, int *piTable, int createTabFlags){ +static int btreeCreateTable(Btree *p, Pgno *piTable, int createTabFlags){ BtShared *pBt = p->pBt; MemPage *pRoot; Pgno pgnoRoot; int rc; - int ptfFlags; /* Page-type flage for the root page of new table */ + int ptfFlags; /* Page-type flags for the root page of new table */ assert( sqlite3BtreeHoldsMutex(p) ); assert( pBt->inTransaction==TRANS_WRITE ); @@ -8303,6 +10045,9 @@ static int btreeCreateTable(Btree *p, int *piTable, int createTabFlags){ ** created so far, so the new root-page is (meta[3]+1). */ sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &pgnoRoot); + if( pgnoRoot>btreePagecount(pBt) ){ + return SQLITE_CORRUPT_PGNO(pgnoRoot); + } pgnoRoot++; /* The new root-page may not be allocated on a pointer-map page, or the @@ -8312,8 +10057,7 @@ static int btreeCreateTable(Btree *p, int *piTable, int createTabFlags){ pgnoRoot==PENDING_BYTE_PAGE(pBt) ){ pgnoRoot++; } - assert( pgnoRoot>=3 || CORRUPT_DB ); - testcase( pgnoRoot<3 ); + assert( pgnoRoot>=3 ); /* Allocate a page. The page that currently resides at pgnoRoot will ** be moved to the allocated page (unless the allocated page happens @@ -8350,7 +10094,7 @@ static int btreeCreateTable(Btree *p, int *piTable, int createTabFlags){ } rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage); if( eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){ - rc = SQLITE_CORRUPT_BKPT; + rc = SQLITE_CORRUPT_PGNO(pgnoRoot); } if( rc!=SQLITE_OK ){ releasePage(pRoot); @@ -8376,7 +10120,7 @@ static int btreeCreateTable(Btree *p, int *piTable, int createTabFlags){ } }else{ pRoot = pPageMove; - } + } /* Update the pointer-map and meta-data with the new root-page number. */ ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0, &rc); @@ -8410,10 +10154,10 @@ static int btreeCreateTable(Btree *p, int *piTable, int createTabFlags){ zeroPage(pRoot, ptfFlags); sqlite3PagerUnref(pRoot->pDbPage); assert( (pBt->openFlags & BTREE_SINGLE)==0 || pgnoRoot==2 ); - *piTable = (int)pgnoRoot; + *piTable = pgnoRoot; return SQLITE_OK; } -int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){ +int sqlite3BtreeCreateTable(Btree *p, Pgno *piTable, int flags){ int rc; sqlite3BtreeEnter(p); rc = btreeCreateTable(p, piTable, flags); @@ -8429,26 +10173,27 @@ static int clearDatabasePage( BtShared *pBt, /* The BTree that contains the table */ Pgno pgno, /* Page number to clear */ int freePageFlag, /* Deallocate page if true */ - int *pnChange /* Add number of Cells freed to this counter */ + i64 *pnChange /* Add number of Cells freed to this counter */ ){ MemPage *pPage; int rc; unsigned char *pCell; int i; int hdr; - u16 szCell; + CellInfo info; assert( sqlite3_mutex_held(pBt->mutex) ); if( pgno>btreePagecount(pBt) ){ - return SQLITE_CORRUPT_BKPT; + return SQLITE_CORRUPT_PGNO(pgno); } - rc = getAndInitPage(pBt, pgno, &pPage, 0, 0); + rc = getAndInitPage(pBt, pgno, &pPage, 0); if( rc ) return rc; - if( pPage->bBusy ){ - rc = SQLITE_CORRUPT_BKPT; + if( (pBt->openFlags & BTREE_SINGLE)==0 + && sqlite3PagerPageRefcount(pPage->pDbPage) != (1 + (pgno==1)) + ){ + rc = SQLITE_CORRUPT_PAGE(pPage); goto cleardatabasepage_out; } - pPage->bBusy = 1; hdr = pPage->hdrOffset; for(i=0; inCell; i++){ pCell = findCell(pPage, i); @@ -8456,14 +10201,15 @@ static int clearDatabasePage( rc = clearDatabasePage(pBt, get4byte(pCell), 1, pnChange); if( rc ) goto cleardatabasepage_out; } - rc = clearCell(pPage, pCell, &szCell); + BTREE_CLEAR_CELL(rc, pPage, pCell, info); if( rc ) goto cleardatabasepage_out; } if( !pPage->leaf ){ rc = clearDatabasePage(pBt, get4byte(&pPage->aData[hdr+8]), 1, pnChange); if( rc ) goto cleardatabasepage_out; - }else if( pnChange ){ - assert( pPage->intKey || CORRUPT_DB ); + if( pPage->intKey ) pnChange = 0; + } + if( pnChange ){ testcase( !pPage->intKey ); *pnChange += pPage->nCell; } @@ -8474,7 +10220,6 @@ static int clearDatabasePage( } cleardatabasepage_out: - pPage->bBusy = 0; releasePage(pPage); return rc; } @@ -8488,11 +10233,10 @@ static int clearDatabasePage( ** read cursors on the table. Open write cursors are moved to the ** root of the table. ** -** If pnChange is not NULL, then table iTable must be an intkey table. The -** integer value pointed to by pnChange is incremented by the number of -** entries in the table. +** If pnChange is not NULL, then the integer value pointed to by pnChange +** is incremented by the number of entries in the table. */ -int sqlite3BtreeClearTable(Btree *p, int iTable, int *pnChange){ +int sqlite3BtreeClearTable(Btree *p, int iTable, i64 *pnChange){ int rc; BtShared *pBt = p->pBt; sqlite3BtreeEnter(p); @@ -8504,7 +10248,9 @@ int sqlite3BtreeClearTable(Btree *p, int iTable, int *pnChange){ /* Invalidate all incrblob cursors open on table iTable (assuming iTable ** is the root of a table b-tree - if it is not, the following call is ** a no-op). */ - invalidateIncrblobCursors(p, 0, 1); + if( p->hasIncrblobCur ){ + invalidateIncrblobCursors(p, (Pgno)iTable, 0, 1); + } rc = clearDatabasePage(pBt, (Pgno)iTable, 0, pnChange); } sqlite3BtreeLeave(p); @@ -8529,12 +10275,12 @@ int sqlite3BtreeClearTableOfCursor(BtCursor *pCur){ ** cursors on the table. ** ** If AUTOVACUUM is enabled and the page at iTable is not the last -** root page in the database file, then the last root page +** root page in the database file, then the last root page ** in the database file is moved into the slot formerly occupied by ** iTable and that last slot formerly occupied by the last root page ** is added to the freelist instead of iTable. In this say, all ** root pages are kept at the beginning of the database file, which -** is necessary for AUTOVACUUM to work right. *piMoved is set to the +** is necessary for AUTOVACUUM to work right. *piMoved is set to the ** page number that used to be the last root page in the file before ** the move. If no page gets moved, *piMoved is set to 0. ** The last root page is recorded in meta[3] and the value of @@ -8547,32 +10293,15 @@ static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){ assert( sqlite3BtreeHoldsMutex(p) ); assert( p->inTrans==TRANS_WRITE ); - - /* It is illegal to drop a table if any cursors are open on the - ** database. This is because in auto-vacuum mode the backend may - ** need to move another root-page to fill a gap left by the deleted - ** root page. If an open cursor was using this page a problem would - ** occur. - ** - ** This error is caught long before control reaches this point. - */ - if( NEVER(pBt->pCursor) ){ - sqlite3ConnectionBlocked(p->db, pBt->pCursor->pBtree->db); - return SQLITE_LOCKED_SHAREDCACHE; - } - - /* - ** It is illegal to drop the sqlite_master table on page 1. But again, - ** this error is caught long before reaching this point. - */ - if( NEVER(iTable<2) ){ - return SQLITE_CORRUPT_BKPT; + assert( iTable>=2 ); + if( iTable>btreePagecount(pBt) ){ + return SQLITE_CORRUPT_PGNO(iTable); } - rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0); - if( rc ) return rc; rc = sqlite3BtreeClearTable(p, iTable, 0); - if( rc ){ + if( rc ) return rc; + rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0); + if( NEVER(rc) ){ releasePage(pPage); return rc; } @@ -8589,7 +10318,7 @@ static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){ if( iTable==maxRootPgno ){ /* If the table being dropped is the table with the largest root-page - ** number in the database, put the root page on the free list. + ** number in the database, put the root page on the free list. */ freePage(pPage, &rc); releasePage(pPage); @@ -8598,7 +10327,7 @@ static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){ } }else{ /* The table being dropped does not have the largest root-page - ** number in the database. So move the page that does into the + ** number in the database. So move the page that does into the ** gap left by the deleted root-page. */ MemPage *pMove; @@ -8640,7 +10369,7 @@ static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){ releasePage(pPage); } #endif - return rc; + return rc; } int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){ int rc; @@ -8659,7 +10388,7 @@ int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){ ** is the number of free pages currently in the database. Meta[1] ** through meta[15] are available for use by higher layers. Meta[0] ** is read-only, the others are read/write. -** +** ** The schema layer numbers meta values differently. At the schema ** layer (and the SetCookie and ReadCookie opcodes) the number of ** free pages is not visible. So Cookie[0] is the same as Meta[1]. @@ -8676,12 +10405,12 @@ void sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){ sqlite3BtreeEnter(p); assert( p->inTrans>TRANS_NONE ); - assert( SQLITE_OK==querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK) ); + assert( SQLITE_OK==querySharedCacheTableLock(p, SCHEMA_ROOT, READ_LOCK) ); assert( pBt->pPage1 ); assert( idx>=0 && idx<=15 ); if( idx==BTREE_DATA_VERSION ){ - *pMeta = sqlite3PagerDataVersion(pBt->pPager) + p->iDataVersion; + *pMeta = sqlite3PagerDataVersion(pBt->pPager) + p->iBDataVersion; }else{ *pMeta = get4byte(&pBt->pPage1->aData[36 + idx*4]); } @@ -8725,42 +10454,41 @@ int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){ return rc; } -#ifndef SQLITE_OMIT_BTREECOUNT /* ** The first argument, pCur, is a cursor opened on some b-tree. Count the ** number of entries in the b-tree and write the result to *pnEntry. ** -** SQLITE_OK is returned if the operation is successfully executed. +** SQLITE_OK is returned if the operation is successfully executed. ** Otherwise, if an error is encountered (i.e. an IO error or database ** corruption) an SQLite error code is returned. */ -int sqlite3BtreeCount(BtCursor *pCur, i64 *pnEntry){ +int sqlite3BtreeCount(sqlite3 *db, BtCursor *pCur, i64 *pnEntry){ i64 nEntry = 0; /* Value to return in *pnEntry */ int rc; /* Return code */ - if( pCur->pgnoRoot==0 ){ + rc = moveToRoot(pCur); + if( rc==SQLITE_EMPTY ){ *pnEntry = 0; return SQLITE_OK; } - rc = moveToRoot(pCur); /* Unless an error occurs, the following loop runs one iteration for each - ** page in the B-Tree structure (not including overflow pages). + ** page in the B-Tree structure (not including overflow pages). */ - while( rc==SQLITE_OK ){ + while( rc==SQLITE_OK && !AtomicLoad(&db->u1.isInterrupted) ){ int iIdx; /* Index of child node in parent */ MemPage *pPage; /* Current page of the b-tree */ - /* If this is a leaf page or the tree is not an int-key tree, then + /* If this is a leaf page or the tree is not an int-key tree, then ** this page contains countable entries. Increment the entry counter ** accordingly. */ - pPage = pCur->apPage[pCur->iPage]; + pPage = pCur->pPage; if( pPage->leaf || !pPage->intKey ){ nEntry += pPage->nCell; } - /* pPage is a leaf node. This loop navigates the cursor so that it + /* pPage is a leaf node. This loop navigates the cursor so that it ** points to the first interior cell that it points to the parent of ** the next page in the tree that has not yet been visited. The ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell @@ -8778,16 +10506,16 @@ int sqlite3BtreeCount(BtCursor *pCur, i64 *pnEntry){ return moveToRoot(pCur); } moveToParent(pCur); - }while ( pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell ); + }while ( pCur->ix>=pCur->pPage->nCell ); - pCur->aiIdx[pCur->iPage]++; - pPage = pCur->apPage[pCur->iPage]; + pCur->ix++; + pPage = pCur->pPage; } - /* Descend to the child node of the cell that the cursor currently + /* Descend to the child node of the cell that the cursor currently ** points at. This is the right-child if (iIdx==pPage->nCell). */ - iIdx = pCur->aiIdx[pCur->iPage]; + iIdx = pCur->ix; if( iIdx==pPage->nCell ){ rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8])); }else{ @@ -8798,7 +10526,6 @@ int sqlite3BtreeCount(BtCursor *pCur, i64 *pnEntry){ /* An error has occurred. Return an error code. */ return rc; } -#endif /* ** Return the pager associated with a BTree. This routine is used for @@ -8809,6 +10536,41 @@ Pager *sqlite3BtreePager(Btree *p){ } #ifndef SQLITE_OMIT_INTEGRITY_CHECK +/* +** Record an OOM error during integrity_check +*/ +static void checkOom(IntegrityCk *pCheck){ + pCheck->rc = SQLITE_NOMEM; + pCheck->mxErr = 0; /* Causes integrity_check processing to stop */ + if( pCheck->nErr==0 ) pCheck->nErr++; +} + +/* +** Invoke the progress handler, if appropriate. Also check for an +** interrupt. +*/ +static void checkProgress(IntegrityCk *pCheck){ + sqlite3 *db = pCheck->db; + if( AtomicLoad(&db->u1.isInterrupted) ){ + pCheck->rc = SQLITE_INTERRUPT; + pCheck->nErr++; + pCheck->mxErr = 0; + } +#ifndef SQLITE_OMIT_PROGRESS_CALLBACK + if( db->xProgress ){ + assert( db->nProgressOps>0 ); + pCheck->nStep++; + if( (pCheck->nStep % db->nProgressOps)==0 + && db->xProgress(db->pProgressArg) + ){ + pCheck->rc = SQLITE_INTERRUPT; + pCheck->nErr++; + pCheck->mxErr = 0; + } + } +#endif +} + /* ** Append a message to the error message string. */ @@ -8818,20 +10580,22 @@ static void checkAppendMsg( ... ){ va_list ap; + checkProgress(pCheck); if( !pCheck->mxErr ) return; pCheck->mxErr--; pCheck->nErr++; va_start(ap, zFormat); if( pCheck->errMsg.nChar ){ - sqlite3StrAccumAppend(&pCheck->errMsg, "\n", 1); + sqlite3_str_append(&pCheck->errMsg, "\n", 1); } if( pCheck->zPfx ){ - sqlite3XPrintf(&pCheck->errMsg, pCheck->zPfx, pCheck->v1, pCheck->v2); + sqlite3_str_appendf(&pCheck->errMsg, pCheck->zPfx, + pCheck->v0, pCheck->v1, pCheck->v2); } - sqlite3VXPrintf(&pCheck->errMsg, zFormat, ap); + sqlite3_str_vappendf(&pCheck->errMsg, zFormat, ap); va_end(ap); - if( pCheck->errMsg.accError==STRACCUM_NOMEM ){ - pCheck->mallocFailed = 1; + if( pCheck->errMsg.accError==SQLITE_NOMEM ){ + checkOom(pCheck); } } #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ @@ -8843,7 +10607,8 @@ static void checkAppendMsg( ** corresponds to page iPg is already set. */ static int getPageReferenced(IntegrityCk *pCheck, Pgno iPg){ - assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 ); + assert( pCheck->aPgRef!=0 ); + assert( iPg<=pCheck->nCkPage && sizeof(pCheck->aPgRef[0])==1 ); return (pCheck->aPgRef[iPg/8] & (1 << (iPg & 0x07))); } @@ -8851,7 +10616,8 @@ static int getPageReferenced(IntegrityCk *pCheck, Pgno iPg){ ** Set the bit in the IntegrityCk.aPgRef[] array that corresponds to page iPg. */ static void setPageReferenced(IntegrityCk *pCheck, Pgno iPg){ - assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 ); + assert( pCheck->aPgRef!=0 ); + assert( iPg<=pCheck->nCkPage && sizeof(pCheck->aPgRef[0])==1 ); pCheck->aPgRef[iPg/8] |= (1 << (iPg & 0x07)); } @@ -8865,13 +10631,12 @@ static void setPageReferenced(IntegrityCk *pCheck, Pgno iPg){ ** Also check that the page number is in bounds. */ static int checkRef(IntegrityCk *pCheck, Pgno iPage){ - if( iPage==0 ) return 1; - if( iPage>pCheck->nPage ){ - checkAppendMsg(pCheck, "invalid page number %d", iPage); + if( iPage>pCheck->nCkPage || iPage==0 ){ + checkAppendMsg(pCheck, "invalid page number %u", iPage); return 1; } if( getPageReferenced(pCheck, iPage) ){ - checkAppendMsg(pCheck, "2nd reference to page %d", iPage); + checkAppendMsg(pCheck, "2nd reference to page %u", iPage); return 1; } setPageReferenced(pCheck, iPage); @@ -8880,7 +10645,7 @@ static int checkRef(IntegrityCk *pCheck, Pgno iPage){ #ifndef SQLITE_OMIT_AUTOVACUUM /* -** Check that the entry in the pointer-map for page iChild maps to +** Check that the entry in the pointer-map for page iChild maps to ** page iParent, pointer type ptrType. If not, append an error message ** to pCheck. */ @@ -8896,14 +10661,14 @@ static void checkPtrmap( rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent); if( rc!=SQLITE_OK ){ - if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ) pCheck->mallocFailed = 1; - checkAppendMsg(pCheck, "Failed to read ptrmap key=%d", iChild); + if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ) checkOom(pCheck); + checkAppendMsg(pCheck, "Failed to read ptrmap key=%u", iChild); return; } if( ePtrmapType!=eType || iPtrmapParent!=iParent ){ checkAppendMsg(pCheck, - "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)", + "Bad ptr map entry key=%u expected=(%u,%u) got=(%u,%u)", iChild, eType, iParent, ePtrmapType, iPtrmapParent); } } @@ -8916,40 +10681,35 @@ static void checkPtrmap( static void checkList( IntegrityCk *pCheck, /* Integrity checking context */ int isFreeList, /* True for a freelist. False for overflow page list */ - int iPage, /* Page number for first page in the list */ - int N /* Expected number of pages in the list */ + Pgno iPage, /* Page number for first page in the list */ + u32 N /* Expected number of pages in the list */ ){ int i; - int expected = N; - int iFirst = iPage; - while( N-- > 0 && pCheck->mxErr ){ + u32 expected = N; + int nErrAtStart = pCheck->nErr; + while( iPage!=0 && pCheck->mxErr ){ DbPage *pOvflPage; unsigned char *pOvflData; - if( iPage<1 ){ - checkAppendMsg(pCheck, - "%d of %d pages missing from overflow list starting at %d", - N+1, expected, iFirst); - break; - } if( checkRef(pCheck, iPage) ) break; + N--; if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage, 0) ){ - checkAppendMsg(pCheck, "failed to get page %d", iPage); + checkAppendMsg(pCheck, "failed to get page %u", iPage); break; } pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage); if( isFreeList ){ - int n = get4byte(&pOvflData[4]); + u32 n = (u32)get4byte(&pOvflData[4]); #ifndef SQLITE_OMIT_AUTOVACUUM if( pCheck->pBt->autoVacuum ){ checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0); } #endif - if( n>(int)pCheck->pBt->usableSize/4-2 ){ + if( n>pCheck->pBt->usableSize/4-2 ){ checkAppendMsg(pCheck, - "freelist leaf count too big on page %d", iPage); + "freelist leaf count too big on page %u", iPage); N--; }else{ - for(i=0; ipBt->autoVacuum ){ @@ -8975,10 +10735,12 @@ static void checkList( #endif iPage = get4byte(pOvflData); sqlite3PagerUnref(pOvflPage); - - if( isFreeList && N<(iPage!=0) ){ - checkAppendMsg(pCheck, "free-page count in header is too small"); - } + } + if( N && nErrAtStart==pCheck->nErr ){ + checkAppendMsg(pCheck, + "%s is %u but should be %u", + isFreeList ? "size" : "overflow list length", + expected-N, expected); } } #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ @@ -9001,12 +10763,14 @@ static void checkList( ** property. ** ** This heap is used for cell overlap and coverage testing. Each u32 -** entry represents the span of a cell or freeblock on a btree page. +** entry represents the span of a cell or freeblock on a btree page. ** The upper 16 bits are the index of the first byte of a range and the ** lower 16 bits are the index of the last byte of that range. */ static void btreeHeapInsert(u32 *aHeap, u32 x){ - u32 j, i = ++aHeap[0]; + u32 j, i; + assert( aHeap!=0 ); + i = ++aHeap[0]; aHeap[i] = x; while( (j = i/2)>0 && aHeap[j]>aHeap[i] ){ x = aHeap[j]; @@ -9031,7 +10795,7 @@ static int btreeHeapPull(u32 *aHeap, u32 *pOut){ aHeap[j] = x; i = j; } - return 1; + return 1; } #ifndef SQLITE_OMIT_INTEGRITY_CHECK @@ -9039,7 +10803,7 @@ static int btreeHeapPull(u32 *aHeap, u32 *pOut){ ** Do various sanity checks on a single page of a tree. Return ** the tree depth. Root pages return 0. Parents of root pages ** return 1, and so forth. -** +** ** These checks are done: ** ** 1. Make sure that cells and freeblocks do not overlap @@ -9051,7 +10815,7 @@ static int btreeHeapPull(u32 *aHeap, u32 *pOut){ */ static int checkTreePage( IntegrityCk *pCheck, /* Context for the sanity check */ - int iPage, /* Page number of the page to check */ + Pgno iPage, /* Page number of the page to check */ i64 *piMinKey, /* Write minimum integer primary key here */ i64 maxKey /* Error if integer primary key greater than this */ ){ @@ -9083,15 +10847,18 @@ static int checkTreePage( /* Check that the page exists */ + checkProgress(pCheck); + if( pCheck->mxErr==0 ) goto end_of_check; pBt = pCheck->pBt; usableSize = pBt->usableSize; if( iPage==0 ) return 0; if( checkRef(pCheck, iPage) ) return 0; - pCheck->zPfx = "Page %d: "; + pCheck->zPfx = "Tree %u page %u: "; pCheck->v1 = iPage; - if( (rc = btreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){ + if( (rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0 ){ checkAppendMsg(pCheck, "unable to get the page. error code=%d", rc); + if( rc==SQLITE_IOERR_NOMEM ) pCheck->rc = SQLITE_NOMEM; goto end_of_check; } @@ -9105,11 +10872,16 @@ static int checkTreePage( "btreeInitPage() returns error code %d", rc); goto end_of_check; } + if( (rc = btreeComputeFreeSpace(pPage))!=0 ){ + assert( rc==SQLITE_CORRUPT ); + checkAppendMsg(pCheck, "free space corruption", rc); + goto end_of_check; + } data = pPage->aData; hdr = pPage->hdrOffset; /* Set up for cell analysis */ - pCheck->zPfx = "On tree page %d cell %d: "; + pCheck->zPfx = "Tree %u page %u cell %u: "; contentOffset = get2byteNotZero(&data[hdr+5]); assert( contentOffset<=usableSize ); /* Enforced by btreeInitPage() */ @@ -9117,6 +10889,9 @@ static int checkTreePage( ** number of cells on the page. */ nCell = get2byte(&data[hdr+3]); assert( pPage->nCell==nCell ); + if( pPage->leaf || pPage->intKey==0 ){ + pCheck->nRow += nCell; + } /* EVIDENCE-OF: R-23882-45353 The cell pointer array of a b-tree page ** immediately follows the b-tree page header. */ @@ -9129,7 +10904,7 @@ static int checkTreePage( pgno = get4byte(&data[hdr+8]); #ifndef SQLITE_OMIT_AUTOVACUUM if( pBt->autoVacuum ){ - pCheck->zPfx = "On page %d at right child: "; + pCheck->zPfx = "Tree %u page %u right child: "; checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage); } #endif @@ -9153,7 +10928,7 @@ static int checkTreePage( pc = get2byteAligned(pCellIdx); pCellIdx -= 2; if( pcusableSize-4 ){ - checkAppendMsg(pCheck, "Offset %d out of range %d..%d", + checkAppendMsg(pCheck, "Offset %u out of range %u..%u", pc, contentOffset, usableSize-4); doCoverageCheck = 0; continue; @@ -9172,11 +10947,12 @@ static int checkTreePage( checkAppendMsg(pCheck, "Rowid %lld out of order", info.nKey); } maxKey = info.nKey; + keyCanBeEqual = 0; /* Only the first key on the page may ==maxKey */ } /* Check the content overflow list */ if( info.nPayload>info.nLocal ){ - int nPage; /* Number of pages on the overflow chain */ + u32 nPage; /* Number of pages on the overflow chain */ Pgno pgnoOvfl; /* First page of the overflow chain */ assert( pc + info.nSize - 4 <= usableSize ); nPage = (info.nPayload - info.nLocal + usableSize - 5)/(usableSize - 4); @@ -9227,18 +11003,19 @@ static int checkTreePage( btreeHeapInsert(heap, (pc<<16)|(pc+size-1)); } } + assert( heap!=0 ); /* Add the freeblocks to the min-heap ** ** EVIDENCE-OF: R-20690-50594 The second field of the b-tree page header ** is the offset of the first freeblock, or zero if there are no - ** freeblocks on the page. + ** freeblocks on the page. */ i = get2byte(&data[hdr+1]); while( i>0 ){ int size, j; - assert( (u32)i<=usableSize-4 ); /* Enforced by btreeInitPage() */ + assert( (u32)i<=usableSize-4 ); /* Enforced by btreeComputeFreeSpace() */ size = get2byte(&data[i+2]); - assert( (u32)(i+size)<=usableSize ); /* Enforced by btreeInitPage() */ + assert( (u32)(i+size)<=usableSize ); /* due to btreeComputeFreeSpace() */ btreeHeapInsert(heap, (((u32)i)<<16)|(i+size-1)); /* EVIDENCE-OF: R-58208-19414 The first 2 bytes of a freeblock are a ** big-endian integer which is the offset in the b-tree page of the next @@ -9247,17 +11024,17 @@ static int checkTreePage( j = get2byte(&data[i]); /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of ** increasing offset. */ - assert( j==0 || j>i+size ); /* Enforced by btreeInitPage() */ - assert( (u32)j<=usableSize-4 ); /* Enforced by btreeInitPage() */ + assert( j==0 || j>i+size ); /* Enforced by btreeComputeFreeSpace() */ + assert( (u32)j<=usableSize-4 ); /* Enforced by btreeComputeFreeSpace() */ i = j; } - /* Analyze the min-heap looking for overlap between cells and/or + /* Analyze the min-heap looking for overlap between cells and/or ** freeblocks, and counting the number of untracked bytes in nFrag. - ** + ** ** Each min-heap entry is of the form: (start_address<<16)|end_address. ** There is an implied first entry the covers the page header, the cell ** pointer index, and the gap between the cell pointer index and the start - ** of cell content. + ** of cell content. ** ** The loop below pulls entries from the min-heap in order and compares ** the start_address against the previous end_address. If there is an @@ -9269,7 +11046,7 @@ static int checkTreePage( while( btreeHeapPull(heap,&x) ){ if( (prev&0xffff)>=(x>>16) ){ checkAppendMsg(pCheck, - "Multiple uses for byte %u of page %d", x>>16, iPage); + "Multiple uses for byte %u of page %u", x>>16, iPage); break; }else{ nFrag += (x>>16) - (prev&0xffff) - 1; @@ -9284,7 +11061,7 @@ static int checkTreePage( */ if( heap[0]==0 && nFrag!=data[hdr+7] ){ checkAppendMsg(pCheck, - "Fragmentation of %d bytes reported as %d on page %d", + "Fragmentation of %u bytes reported as %u on page %u", nFrag, data[hdr+7], iPage); } } @@ -9312,99 +11089,146 @@ static int checkTreePage( ** allocation errors, an error message held in memory obtained from ** malloc is returned if *pnErr is non-zero. If *pnErr==0 then NULL is ** returned. If a memory allocation error occurs, NULL is returned. -*/ -char *sqlite3BtreeIntegrityCheck( +** +** If the first entry in aRoot[] is 0, that indicates that the list of +** root pages is incomplete. This is a "partial integrity-check". This +** happens when performing an integrity check on a single table. The +** zero is skipped, of course. But in addition, the freelist checks +** and the checks to make sure every page is referenced are also skipped, +** since obviously it is not possible to know which pages are covered by +** the unverified btrees. Except, if aRoot[1] is 1, then the freelist +** checks are still performed. +*/ +int sqlite3BtreeIntegrityCheck( + sqlite3 *db, /* Database connection that is running the check */ Btree *p, /* The btree to be checked */ - int *aRoot, /* An array of root pages numbers for individual trees */ + Pgno *aRoot, /* An array of root pages numbers for individual trees */ + Mem *aCnt, /* Memory cells to write counts for each tree to */ int nRoot, /* Number of entries in aRoot[] */ int mxErr, /* Stop reporting errors after this many */ - int *pnErr /* Write number of errors seen to this variable */ + int *pnErr, /* OUT: Write number of errors seen to this variable */ + char **pzOut /* OUT: Write the error message string here */ ){ Pgno i; IntegrityCk sCheck; BtShared *pBt = p->pBt; - int savedDbFlags = pBt->db->flags; + u64 savedDbFlags = pBt->db->flags; char zErr[100]; + int bPartial = 0; /* True if not checking all btrees */ + int bCkFreelist = 1; /* True to scan the freelist */ VVA_ONLY( int nRef ); + assert( nRoot>0 ); + assert( aCnt!=0 ); + + /* aRoot[0]==0 means this is a partial check */ + if( aRoot[0]==0 ){ + assert( nRoot>1 ); + bPartial = 1; + if( aRoot[1]!=1 ) bCkFreelist = 0; + } + sqlite3BtreeEnter(p); assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE ); VVA_ONLY( nRef = sqlite3PagerRefcount(pBt->pPager) ); assert( nRef>=0 ); + memset(&sCheck, 0, sizeof(sCheck)); + sCheck.db = db; sCheck.pBt = pBt; sCheck.pPager = pBt->pPager; - sCheck.nPage = btreePagecount(sCheck.pBt); + sCheck.nCkPage = btreePagecount(sCheck.pBt); sCheck.mxErr = mxErr; - sCheck.nErr = 0; - sCheck.mallocFailed = 0; - sCheck.zPfx = 0; - sCheck.v1 = 0; - sCheck.v2 = 0; - sCheck.aPgRef = 0; - sCheck.heap = 0; sqlite3StrAccumInit(&sCheck.errMsg, 0, zErr, sizeof(zErr), SQLITE_MAX_LENGTH); sCheck.errMsg.printfFlags = SQLITE_PRINTF_INTERNAL; - if( sCheck.nPage==0 ){ + if( sCheck.nCkPage==0 ){ goto integrity_ck_cleanup; } - sCheck.aPgRef = sqlite3MallocZero((sCheck.nPage / 8)+ 1); + sCheck.aPgRef = sqlite3MallocZero((sCheck.nCkPage / 8)+ 1); if( !sCheck.aPgRef ){ - sCheck.mallocFailed = 1; + checkOom(&sCheck); goto integrity_ck_cleanup; } sCheck.heap = (u32*)sqlite3PageMalloc( pBt->pageSize ); if( sCheck.heap==0 ){ - sCheck.mallocFailed = 1; + checkOom(&sCheck); goto integrity_ck_cleanup; } i = PENDING_BYTE_PAGE(pBt); - if( i<=sCheck.nPage ) setPageReferenced(&sCheck, i); + if( i<=sCheck.nCkPage ) setPageReferenced(&sCheck, i); /* Check the integrity of the freelist */ - sCheck.zPfx = "Main freelist: "; - checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]), - get4byte(&pBt->pPage1->aData[36])); - sCheck.zPfx = 0; + if( bCkFreelist ){ + sCheck.zPfx = "Freelist: "; + checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]), + get4byte(&pBt->pPage1->aData[36])); + sCheck.zPfx = 0; + } /* Check all the tables. */ +#ifndef SQLITE_OMIT_AUTOVACUUM + if( !bPartial ){ + if( pBt->autoVacuum ){ + Pgno mx = 0; + Pgno mxInHdr; + for(i=0; (int)ipPage1->aData[52]); + if( mx!=mxInHdr ){ + checkAppendMsg(&sCheck, + "max rootpage (%u) disagrees with header (%u)", + mx, mxInHdr + ); + } + }else if( get4byte(&pBt->pPage1->aData[64])!=0 ){ + checkAppendMsg(&sCheck, + "incremental_vacuum enabled with a max rootpage of zero" + ); + } + } +#endif testcase( pBt->db->flags & SQLITE_CellSizeCk ); - pBt->db->flags &= ~SQLITE_CellSizeCk; + pBt->db->flags &= ~(u64)SQLITE_CellSizeCk; for(i=0; (int)iautoVacuum && aRoot[i]>1 ){ - checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0); - } + if( pBt->autoVacuum && aRoot[i]>1 && !bPartial ){ + checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0); + } #endif - checkTreePage(&sCheck, aRoot[i], ¬Used, LARGEST_INT64); + sCheck.v0 = aRoot[i]; + checkTreePage(&sCheck, aRoot[i], ¬Used, LARGEST_INT64); + } + sqlite3MemSetArrayInt64(aCnt, i, sCheck.nRow); } pBt->db->flags = savedDbFlags; /* Make sure every page in the file is referenced */ - for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){ + if( !bPartial ){ + for(i=1; i<=sCheck.nCkPage && sCheck.mxErr; i++){ #ifdef SQLITE_OMIT_AUTOVACUUM - if( getPageReferenced(&sCheck, i)==0 ){ - checkAppendMsg(&sCheck, "Page %d is never used", i); - } + if( getPageReferenced(&sCheck, i)==0 ){ + checkAppendMsg(&sCheck, "Page %u: never used", i); + } #else - /* If the database supports auto-vacuum, make sure no tables contain - ** references to pointer-map pages. - */ - if( getPageReferenced(&sCheck, i)==0 && - (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){ - checkAppendMsg(&sCheck, "Page %d is never used", i); - } - if( getPageReferenced(&sCheck, i)!=0 && - (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){ - checkAppendMsg(&sCheck, "Pointer map page %d is referenced", i); - } + /* If the database supports auto-vacuum, make sure no tables contain + ** references to pointer-map pages. + */ + if( getPageReferenced(&sCheck, i)==0 && + (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){ + checkAppendMsg(&sCheck, "Page %u: never used", i); + } + if( getPageReferenced(&sCheck, i)!=0 && + (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){ + checkAppendMsg(&sCheck, "Page %u: pointer map referenced", i); + } #endif + } } /* Clean up and report errors. @@ -9412,16 +11236,17 @@ char *sqlite3BtreeIntegrityCheck( integrity_ck_cleanup: sqlite3PageFree(sCheck.heap); sqlite3_free(sCheck.aPgRef); - if( sCheck.mallocFailed ){ - sqlite3StrAccumReset(&sCheck.errMsg); - sCheck.nErr++; - } *pnErr = sCheck.nErr; - if( sCheck.nErr==0 ) sqlite3StrAccumReset(&sCheck.errMsg); + if( sCheck.nErr==0 ){ + sqlite3_str_reset(&sCheck.errMsg); + *pzOut = 0; + }else{ + *pzOut = sqlite3StrAccumFinish(&sCheck.errMsg); + } /* Make sure this analysis did not leave any unref() pages. */ assert( nRef==sqlite3PagerRefcount(pBt->pPager) ); sqlite3BtreeLeave(p); - return sqlite3StrAccumFinish(&sCheck.errMsg); + return sCheck.rc; } #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ @@ -9451,18 +11276,19 @@ const char *sqlite3BtreeGetJournalname(Btree *p){ } /* -** Return non-zero if a transaction is active. +** Return one of SQLITE_TXN_NONE, SQLITE_TXN_READ, or SQLITE_TXN_WRITE +** to describe the current transaction state of Btree p. */ -int sqlite3BtreeIsInTrans(Btree *p){ +int sqlite3BtreeTxnState(Btree *p){ assert( p==0 || sqlite3_mutex_held(p->db->mutex) ); - return (p && (p->inTrans==TRANS_WRITE)); + return p ? p->inTrans : 0; } #ifndef SQLITE_OMIT_WAL /* ** Run a checkpoint on the Btree passed as the first argument. ** -** Return SQLITE_LOCKED if this or any other connection has an open +** Return SQLITE_LOCKED if this or any other connection has an open ** transaction on the shared-cache the argument Btree is connected to. ** ** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART. @@ -9475,7 +11301,7 @@ int sqlite3BtreeCheckpoint(Btree *p, int eMode, int *pnLog, int *pnCkpt){ if( pBt->inTransaction!=TRANS_NONE ){ rc = SQLITE_LOCKED; }else{ - rc = sqlite3PagerCheckpoint(pBt->pPager, eMode, pnLog, pnCkpt); + rc = sqlite3PagerCheckpoint(pBt->pPager, p->db, eMode, pnLog, pnCkpt); } sqlite3BtreeLeave(p); } @@ -9484,14 +11310,8 @@ int sqlite3BtreeCheckpoint(Btree *p, int eMode, int *pnLog, int *pnCkpt){ #endif /* -** Return non-zero if a read (or write) transaction is active. +** Return true if there is currently a backup running on Btree p. */ -int sqlite3BtreeIsInReadTrans(Btree *p){ - assert( p ); - assert( sqlite3_mutex_held(p->db->mutex) ); - return p->inTrans!=TRANS_NONE; -} - int sqlite3BtreeIsInBackup(Btree *p){ assert( p ); assert( sqlite3_mutex_held(p->db->mutex) ); @@ -9501,25 +11321,26 @@ int sqlite3BtreeIsInBackup(Btree *p){ /* ** This function returns a pointer to a blob of memory associated with ** a single shared-btree. The memory is used by client code for its own -** purposes (for example, to store a high-level schema associated with +** purposes (for example, to store a high-level schema associated with ** the shared-btree). The btree layer manages reference counting issues. ** ** The first time this is called on a shared-btree, nBytes bytes of memory -** are allocated, zeroed, and returned to the caller. For each subsequent +** are allocated, zeroed, and returned to the caller. For each subsequent ** call the nBytes parameter is ignored and a pointer to the same blob -** of memory returned. +** of memory returned. ** ** If the nBytes parameter is 0 and the blob of memory has not yet been ** allocated, a null pointer is returned. If the blob has already been ** allocated, it is returned as normal. ** -** Just before the shared-btree is closed, the function passed as the -** xFree argument when the memory allocation was made is invoked on the +** Just before the shared-btree is closed, the function passed as the +** xFree argument when the memory allocation was made is invoked on the ** blob of allocated memory. The xFree function should not call sqlite3_free() ** on the memory, the btree layer does that. */ void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){ BtShared *pBt = p->pBt; + assert( nBytes==0 || nBytes==sizeof(Schema) ); sqlite3BtreeEnter(p); if( !pBt->pSchema && nBytes ){ pBt->pSchema = sqlite3DbMallocZero(0, nBytes); @@ -9530,15 +11351,16 @@ void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){ } /* -** Return SQLITE_LOCKED_SHAREDCACHE if another user of the same shared -** btree as the argument handle holds an exclusive lock on the -** sqlite_master table. Otherwise SQLITE_OK. +** Return SQLITE_LOCKED_SHAREDCACHE if another user of the same shared +** btree as the argument handle holds an exclusive lock on the +** sqlite_schema table. Otherwise SQLITE_OK. */ int sqlite3BtreeSchemaLocked(Btree *p){ int rc; + UNUSED_PARAMETER(p); /* only used in DEBUG builds */ assert( sqlite3_mutex_held(p->db->mutex) ); sqlite3BtreeEnter(p); - rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK); + rc = querySharedCacheTableLock(p, SCHEMA_ROOT, READ_LOCK); assert( rc==SQLITE_OK || rc==SQLITE_LOCKED_SHAREDCACHE ); sqlite3BtreeLeave(p); return rc; @@ -9572,11 +11394,11 @@ int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){ #ifndef SQLITE_OMIT_INCRBLOB /* -** Argument pCsr must be a cursor opened for writing on an -** INTKEY table currently pointing at a valid table entry. +** Argument pCsr must be a cursor opened for writing on an +** INTKEY table currently pointing at a valid table entry. ** This function modifies the data stored as part of that entry. ** -** Only the data content may only be modified, it is not possible to +** Only the data content may only be modified, it is not possible to ** change the length of the data stored. If this function is called with ** parameters that attempt to write past the end of the existing data, ** no modifications are made and SQLITE_CORRUPT is returned. @@ -9607,7 +11429,7 @@ int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){ VVA_ONLY(rc =) saveAllCursors(pCsr->pBt, pCsr->pgnoRoot, pCsr); assert( rc==SQLITE_OK ); - /* Check some assumptions: + /* Check some assumptions: ** (a) the cursor is open for writing, ** (b) there is a read/write transaction open, ** (c) the connection holds a write-lock on the table (if required), @@ -9621,12 +11443,12 @@ int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){ && pCsr->pBt->inTransaction==TRANS_WRITE ); assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) ); assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) ); - assert( pCsr->apPage[pCsr->iPage]->intKey ); + assert( pCsr->pPage->intKey ); return accessPayload(pCsr, offset, amt, (unsigned char *)z, 1); } -/* +/* ** Mark this cursor as an incremental blob cursor. */ void sqlite3BtreeIncrblobCursor(BtCursor *pCur){ @@ -9636,14 +11458,14 @@ void sqlite3BtreeIncrblobCursor(BtCursor *pCur){ #endif /* -** Set both the "read version" (single byte at byte offset 18) and +** Set both the "read version" (single byte at byte offset 18) and ** "write version" (single byte at byte offset 19) fields in the database ** header to iVersion. */ int sqlite3BtreeSetVersion(Btree *pBtree, int iVersion){ BtShared *pBt = pBtree->pBt; int rc; /* Return code */ - + assert( iVersion==1 || iVersion==2 ); /* If setting the version fields to 1, do not automatically open the @@ -9652,11 +11474,11 @@ int sqlite3BtreeSetVersion(Btree *pBtree, int iVersion){ pBt->btsFlags &= ~BTS_NO_WAL; if( iVersion==1 ) pBt->btsFlags |= BTS_NO_WAL; - rc = sqlite3BtreeBeginTrans(pBtree, 0); + rc = sqlite3BtreeBeginTrans(pBtree, 0, 0); if( rc==SQLITE_OK ){ u8 *aData = pBt->pPage1->aData; if( aData[18]!=(u8)iVersion || aData[19]!=(u8)iVersion ){ - rc = sqlite3BtreeBeginTrans(pBtree, 2); + rc = sqlite3BtreeBeginTrans(pBtree, 2, 0); if( rc==SQLITE_OK ){ rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); if( rc==SQLITE_OK ){ @@ -9691,6 +11513,17 @@ int sqlite3BtreeIsReadonly(Btree *p){ */ int sqlite3HeaderSizeBtree(void){ return ROUND8(sizeof(MemPage)); } +/* +** If no transaction is active and the database is not a temp-db, clear +** the in-memory pager cache. +*/ +void sqlite3BtreeClearCache(Btree *p){ + BtShared *pBt = p->pBt; + if( pBt->inTransaction==TRANS_NONE ){ + sqlite3PagerClearCache(pBt->pPager); + } +} + #if !defined(SQLITE_OMIT_SHARED_CACHE) /* ** Return true if the Btree passed as the only argument is sharable. @@ -9698,4 +11531,14 @@ int sqlite3HeaderSizeBtree(void){ return ROUND8(sizeof(MemPage)); } int sqlite3BtreeSharable(Btree *p){ return p->sharable; } + +/* +** Return the number of connections to the BtShared object accessed by +** the Btree handle passed as the only argument. For private caches +** this is always 1. For shared caches it may be 1 or greater. +*/ +int sqlite3BtreeConnectionCount(Btree *p){ + testcase( p->sharable ); + return p->pBt->nRef; +} #endif diff --git a/src/btree.h b/src/btree.h index 30522e99e1..96f4c4c607 100644 --- a/src/btree.h +++ b/src/btree.h @@ -13,8 +13,8 @@ ** subsystem. See comments in the source code for a detailed description ** of what each interface routine does. */ -#ifndef _BTREE_H_ -#define _BTREE_H_ +#ifndef SQLITE_BTREE_H +#define SQLITE_BTREE_H /* TODO: This definition is just included so other modules compile. It ** needs to be revisited. @@ -39,6 +39,7 @@ typedef struct Btree Btree; typedef struct BtCursor BtCursor; typedef struct BtShared BtShared; +typedef struct BtreePayload BtreePayload; int sqlite3BtreeOpen( @@ -68,31 +69,40 @@ int sqlite3BtreeSetSpillSize(Btree*,int); int sqlite3BtreeSetMmapLimit(Btree*,sqlite3_int64); #endif int sqlite3BtreeSetPagerFlags(Btree*,unsigned); -int sqlite3BtreeSyncDisabled(Btree*); int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix); int sqlite3BtreeGetPageSize(Btree*); -int sqlite3BtreeMaxPageCount(Btree*,int); -u32 sqlite3BtreeLastPage(Btree*); +Pgno sqlite3BtreeMaxPageCount(Btree*,Pgno); +Pgno sqlite3BtreeLastPage(Btree*); int sqlite3BtreeSecureDelete(Btree*,int); -int sqlite3BtreeGetOptimalReserve(Btree*); +int sqlite3BtreeGetRequestedReserve(Btree*); int sqlite3BtreeGetReserveNoMutex(Btree *p); int sqlite3BtreeSetAutoVacuum(Btree *, int); int sqlite3BtreeGetAutoVacuum(Btree *); -int sqlite3BtreeBeginTrans(Btree*,int); -int sqlite3BtreeCommitPhaseOne(Btree*, const char *zMaster); +int sqlite3BtreeBeginTrans(Btree*,int,int*); +int sqlite3BtreeCommitPhaseOne(Btree*, const char*); int sqlite3BtreeCommitPhaseTwo(Btree*, int); int sqlite3BtreeCommit(Btree*); int sqlite3BtreeRollback(Btree*,int,int); int sqlite3BtreeBeginStmt(Btree*,int); -int sqlite3BtreeCreateTable(Btree*, int*, int flags); -int sqlite3BtreeIsInTrans(Btree*); -int sqlite3BtreeIsInReadTrans(Btree*); +int sqlite3BtreeCreateTable(Btree*, Pgno*, int flags); +int sqlite3BtreeTxnState(Btree*); int sqlite3BtreeIsInBackup(Btree*); + void *sqlite3BtreeSchema(Btree *, int, void(*)(void *)); int sqlite3BtreeSchemaLocked(Btree *pBtree); +#ifndef SQLITE_OMIT_SHARED_CACHE int sqlite3BtreeLockTable(Btree *pBtree, int iTab, u8 isWriteLock); +#endif + +/* Savepoints are named, nestable SQL transactions mostly implemented */ +/* in vdbe.c and pager.c See https://sqlite.org/lang_savepoint.html */ int sqlite3BtreeSavepoint(Btree *, int, int); +/* "Checkpoint" only refers to WAL. See https://sqlite.org/wal.html#ckpt */ +#ifndef SQLITE_OMIT_WAL + int sqlite3BtreeCheckpoint(Btree*, int, int *, int *); +#endif + const char *sqlite3BtreeGetFilename(Btree *); const char *sqlite3BtreeGetJournalname(Btree *); int sqlite3BtreeCopyFile(Btree *, Btree *); @@ -113,7 +123,7 @@ int sqlite3BtreeIncrVacuum(Btree *); #define BTREE_BLOBKEY 2 /* Table has keys only - no data */ int sqlite3BtreeDropTable(Btree*, int, int*); -int sqlite3BtreeClearTable(Btree*, int, int*); +int sqlite3BtreeClearTable(Btree*, int, i64*); int sqlite3BtreeClearTableOfCursor(BtCursor*); int sqlite3BtreeTripAllCursors(Btree*, int, int); @@ -173,7 +183,7 @@ int sqlite3BtreeNewDb(Btree *p); ** reduce network bandwidth. ** ** Note that BTREE_HINT_FLAGS with BTREE_BULKLOAD is the only hint used by -** standard SQLite. The other hints are provided for extentions that use +** standard SQLite. The other hints are provided for extensions that use ** the SQLite parser and code generator but substitute their own storage ** engine. */ @@ -223,12 +233,16 @@ int sqlite3BtreeNewDb(Btree *p); int sqlite3BtreeCursor( Btree*, /* BTree containing table to open */ - int iTable, /* Index of root page */ + Pgno iTable, /* Index of root page */ int wrFlag, /* 1 for writing. 0 for read-only */ struct KeyInfo*, /* First argument to compare function */ BtCursor *pCursor /* Space to write cursor structure */ ); +BtCursor *sqlite3BtreeFakeValidCursor(void); int sqlite3BtreeCursorSize(void); +#ifdef SQLITE_DEBUG +int sqlite3BtreeClosesWithCursor(Btree*,BtCursor*); +#endif void sqlite3BtreeCursorZero(BtCursor*); void sqlite3BtreeCursorHintFlags(BtCursor*, unsigned); #ifdef SQLITE_ENABLE_CURSOR_HINTS @@ -236,54 +250,123 @@ void sqlite3BtreeCursorHint(BtCursor*, int, ...); #endif int sqlite3BtreeCloseCursor(BtCursor*); -int sqlite3BtreeMovetoUnpacked( +int sqlite3BtreeTableMoveto( BtCursor*, - UnpackedRecord *pUnKey, i64 intKey, int bias, int *pRes ); +int sqlite3BtreeIndexMoveto( + BtCursor*, + UnpackedRecord *pUnKey, + int *pRes +); int sqlite3BtreeCursorHasMoved(BtCursor*); int sqlite3BtreeCursorRestore(BtCursor*, int*); int sqlite3BtreeDelete(BtCursor*, u8 flags); -/* Allowed flags for the 2nd argument to sqlite3BtreeDelete() */ +/* Allowed flags for sqlite3BtreeDelete() and sqlite3BtreeInsert() */ #define BTREE_SAVEPOSITION 0x02 /* Leave cursor pointing at NEXT or PREV */ #define BTREE_AUXDELETE 0x04 /* not the primary delete operation */ +#define BTREE_APPEND 0x08 /* Insert is likely an append */ +#define BTREE_PREFORMAT 0x80 /* Inserted data is a preformated cell */ -int sqlite3BtreeInsert(BtCursor*, const void *pKey, i64 nKey, - const void *pData, int nData, - int nZero, int bias, int seekResult); +/* An instance of the BtreePayload object describes the content of a single +** entry in either an index or table btree. +** +** Index btrees (used for indexes and also WITHOUT ROWID tables) contain +** an arbitrary key and no data. These btrees have pKey,nKey set to the +** key and the pData,nData,nZero fields are uninitialized. The aMem,nMem +** fields give an array of Mem objects that are a decomposition of the key. +** The nMem field might be zero, indicating that no decomposition is available. +** +** Table btrees (used for rowid tables) contain an integer rowid used as +** the key and passed in the nKey field. The pKey field is zero. +** pData,nData hold the content of the new entry. nZero extra zero bytes +** are appended to the end of the content when constructing the entry. +** The aMem,nMem fields are uninitialized for table btrees. +** +** Field usage summary: +** +** Table BTrees Index Btrees +** +** pKey always NULL encoded key +** nKey the ROWID length of pKey +** pData data not used +** aMem not used decomposed key value +** nMem not used entries in aMem +** nData length of pData not used +** nZero extra zeros after pData not used +** +** This object is used to pass information into sqlite3BtreeInsert(). The +** same information used to be passed as five separate parameters. But placing +** the information into this object helps to keep the interface more +** organized and understandable, and it also helps the resulting code to +** run a little faster by using fewer registers for parameter passing. +*/ +struct BtreePayload { + const void *pKey; /* Key content for indexes. NULL for tables */ + sqlite3_int64 nKey; /* Size of pKey for indexes. PRIMARY KEY for tabs */ + const void *pData; /* Data for tables. */ + sqlite3_value *aMem; /* First of nMem value in the unpacked pKey */ + u16 nMem; /* Number of aMem[] value. Might be zero */ + int nData; /* Size of pData. 0 if none. */ + int nZero; /* Extra zero data appended after pData,nData */ +}; + +int sqlite3BtreeInsert(BtCursor*, const BtreePayload *pPayload, + int flags, int seekResult); int sqlite3BtreeFirst(BtCursor*, int *pRes); +int sqlite3BtreeIsEmpty(BtCursor *pCur, int *pRes); int sqlite3BtreeLast(BtCursor*, int *pRes); -int sqlite3BtreeNext(BtCursor*, int *pRes); +int sqlite3BtreeNext(BtCursor*, int flags); int sqlite3BtreeEof(BtCursor*); -int sqlite3BtreePrevious(BtCursor*, int *pRes); -int sqlite3BtreeKeySize(BtCursor*, i64 *pSize); -int sqlite3BtreeKey(BtCursor*, u32 offset, u32 amt, void*); -const void *sqlite3BtreeKeyFetch(BtCursor*, u32 *pAmt); -const void *sqlite3BtreeDataFetch(BtCursor*, u32 *pAmt); -int sqlite3BtreeDataSize(BtCursor*, u32 *pSize); -int sqlite3BtreeData(BtCursor*, u32 offset, u32 amt, void*); - -char *sqlite3BtreeIntegrityCheck(Btree*, int *aRoot, int nRoot, int, int*); +int sqlite3BtreePrevious(BtCursor*, int flags); +i64 sqlite3BtreeIntegerKey(BtCursor*); +void sqlite3BtreeCursorPin(BtCursor*); +void sqlite3BtreeCursorUnpin(BtCursor*); +i64 sqlite3BtreeOffset(BtCursor*); +int sqlite3BtreePayload(BtCursor*, u32 offset, u32 amt, void*); +const void *sqlite3BtreePayloadFetch(BtCursor*, u32 *pAmt); +u32 sqlite3BtreePayloadSize(BtCursor*); +sqlite3_int64 sqlite3BtreeMaxRecordSize(BtCursor*); + +int sqlite3BtreeIntegrityCheck( + sqlite3 *db, /* Database connection that is running the check */ + Btree *p, /* The btree to be checked */ + Pgno *aRoot, /* An array of root pages numbers for individual trees */ + sqlite3_value *aCnt, /* OUT: entry counts for each btree in aRoot[] */ + int nRoot, /* Number of entries in aRoot[] */ + int mxErr, /* Stop reporting errors after this many */ + int *pnErr, /* OUT: Write number of errors seen to this variable */ + char **pzOut /* OUT: Write the error message string here */ +); struct Pager *sqlite3BtreePager(Btree*); +i64 sqlite3BtreeRowCountEst(BtCursor*); +#ifndef SQLITE_OMIT_INCRBLOB +int sqlite3BtreePayloadChecked(BtCursor*, u32 offset, u32 amt, void*); int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*); void sqlite3BtreeIncrblobCursor(BtCursor *); +#endif void sqlite3BtreeClearCursor(BtCursor *); int sqlite3BtreeSetVersion(Btree *pBt, int iVersion); int sqlite3BtreeCursorHasHint(BtCursor*, unsigned int mask); int sqlite3BtreeIsReadonly(Btree *pBt); int sqlite3HeaderSizeBtree(void); +#ifdef SQLITE_DEBUG +sqlite3_uint64 sqlite3BtreeSeekCount(Btree*); +#else +# define sqlite3BtreeSeekCount(X) 0 +#endif + #ifndef NDEBUG int sqlite3BtreeCursorIsValid(BtCursor*); #endif +int sqlite3BtreeCursorIsValidNN(BtCursor*); -#ifndef SQLITE_OMIT_BTREECOUNT -int sqlite3BtreeCount(BtCursor *, i64 *); -#endif +int sqlite3BtreeCount(sqlite3*, BtCursor*, i64*); #ifdef SQLITE_TEST int sqlite3BtreeCursorInfo(BtCursor*, int*, int); @@ -294,6 +377,10 @@ void sqlite3BtreeCursorList(Btree*); int sqlite3BtreeCheckpoint(Btree*, int, int *, int *); #endif +int sqlite3BtreeTransferRow(BtCursor*, BtCursor*, i64); + +void sqlite3BtreeClearCache(Btree*); + /* ** If we are not using shared cache, then there is no need to ** use mutexes to access the BtShared structures. So make the @@ -304,11 +391,13 @@ void sqlite3BtreeCursorList(Btree*); void sqlite3BtreeEnterAll(sqlite3*); int sqlite3BtreeSharable(Btree*); void sqlite3BtreeEnterCursor(BtCursor*); + int sqlite3BtreeConnectionCount(Btree*); #else # define sqlite3BtreeEnter(X) # define sqlite3BtreeEnterAll(X) # define sqlite3BtreeSharable(X) 0 # define sqlite3BtreeEnterCursor(X) +# define sqlite3BtreeConnectionCount(X) 1 #endif #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE @@ -333,4 +422,4 @@ void sqlite3BtreeCursorList(Btree*); #endif -#endif /* _BTREE_H_ */ +#endif /* SQLITE_BTREE_H */ diff --git a/src/btreeInt.h b/src/btreeInt.h index 6cd090257c..17e3a1add5 100644 --- a/src/btreeInt.h +++ b/src/btreeInt.h @@ -64,7 +64,7 @@ ** 22 1 Min embedded payload fraction (must be 32) ** 23 1 Min leaf payload fraction (must be 32) ** 24 4 File change counter -** 28 4 Reserved for future use +** 28 4 The size of the database in pages ** 32 4 First freelist page ** 36 4 Number of freelist pages in the file ** 40 60 15 4-byte meta values passed to higher layers @@ -172,7 +172,7 @@ ** byte are used. The integer consists of all bytes that have bit 8 set and ** the first byte with bit 8 clear. The most significant byte of the integer ** appears first. A variable-length integer may not be more than 9 bytes long. -** As a special case, all 8 bytes of the 9th byte are used as data. This +** As a special case, all 8 bits of the 9th byte are used as data. This ** allows a 64-bit integer to be encoded in 9 bytes. ** ** 0x00 becomes 0x00000000 @@ -180,7 +180,7 @@ ** 0x81 0x00 becomes 0x00000080 ** 0x82 0x00 becomes 0x00000100 ** 0x80 0x7f becomes 0x0000007f -** 0x8a 0x91 0xd1 0xac 0x78 becomes 0x12345678 +** 0x81 0x91 0xd1 0xac 0x78 becomes 0x12345678 ** 0x81 0x81 0x81 0x81 0x01 becomes 0x10204081 ** ** Variable length integers are used for rowids and to hold the number of @@ -259,55 +259,50 @@ typedef struct CellInfo CellInfo; #define PTF_LEAF 0x08 /* -** As each page of the file is loaded into memory, an instance of the following -** structure is appended and initialized to zero. This structure stores -** information about the page that is decoded from the raw file page. +** An instance of this object stores information about each a single database +** page that has been loaded into memory. The information in this object +** is derived from the raw on-disk page content. ** -** The pParent field points back to the parent page. This allows us to -** walk up the BTree from any leaf to the root. Care must be taken to -** unref() the parent page pointer when this page is no longer referenced. -** The pageDestructor() routine handles that chore. +** As each database page is loaded into memory, the pager allocates an +** instance of this object and zeros the first 8 bytes. (This is the +** "extra" information associated with each page of the pager.) ** ** Access to all fields of this structure is controlled by the mutex ** stored in MemPage.pBt->mutex. */ struct MemPage { u8 isInit; /* True if previously initialized. MUST BE FIRST! */ - u8 nOverflow; /* Number of overflow cell bodies in aCell[] */ u8 intKey; /* True if table b-trees. False for index b-trees */ u8 intKeyLeaf; /* True if the leaf of an intKey table */ + Pgno pgno; /* Page number for this page */ + /* Only the first 8 bytes (above) are zeroed by pager.c when a new page + ** is allocated. All fields that follow must be initialized before use */ u8 leaf; /* True if a leaf page */ u8 hdrOffset; /* 100 for page 1. 0 otherwise */ u8 childPtrSize; /* 0 if leaf==1. 4 if leaf==0 */ u8 max1bytePayload; /* min(maxLocal,127) */ - u8 bBusy; /* Prevent endless loops on corrupt database files */ + u8 nOverflow; /* Number of overflow cell bodies in aCell[] */ u16 maxLocal; /* Copy of BtShared.maxLocal or BtShared.maxLeaf */ u16 minLocal; /* Copy of BtShared.minLocal or BtShared.minLeaf */ u16 cellOffset; /* Index in aData of first cell pointer */ - u16 nFree; /* Number of free bytes on the page */ + int nFree; /* Number of free bytes on the page. -1 for unknown */ u16 nCell; /* Number of cells on this page, local and ovfl */ u16 maskPage; /* Mask for page offset */ - u16 aiOvfl[5]; /* Insert the i-th overflow cell before the aiOvfl-th + u16 aiOvfl[4]; /* Insert the i-th overflow cell before the aiOvfl-th ** non-overflow cell */ - u8 *apOvfl[5]; /* Pointers to the body of overflow cells */ + u8 *apOvfl[4]; /* Pointers to the body of overflow cells */ BtShared *pBt; /* Pointer to BtShared that this page is part of */ u8 *aData; /* Pointer to disk image of the page data */ - u8 *aDataEnd; /* One byte past the end of usable data */ + u8 *aDataEnd; /* One byte past the end of the entire page - not just + ** the usable space, the entire page. Used to prevent + ** corruption-induced buffer overflow. */ u8 *aCellIdx; /* The cell index area */ u8 *aDataOfst; /* Same as aData for leaves. aData+4 for interior */ DbPage *pDbPage; /* Pager page handle */ u16 (*xCellSize)(MemPage*,u8*); /* cellSizePtr method */ void (*xParseCell)(MemPage*,u8*,CellInfo*); /* btreeParseCell method */ - Pgno pgno; /* Page number for this page */ }; -/* -** The in-memory image of a disk page has the auxiliary information appended -** to the end. EXTRA_SIZE is the number of bytes of space needed to hold -** that extra information. -*/ -#define EXTRA_SIZE sizeof(MemPage) - /* ** A linked list of the following structures is stored at BtShared.pLock. ** Locks are added (or upgraded from READ_LOCK to WRITE_LOCK) when a cursor @@ -356,9 +351,12 @@ struct Btree { u8 hasIncrblobCur; /* True if there are one or more Incrblob cursors */ int wantToLock; /* Number of nested calls to sqlite3BtreeEnter() */ int nBackup; /* Number of backup operations reading this btree */ - u32 iDataVersion; /* Combines with pBt->pPager->iDataVersion */ + u32 iBDataVersion; /* Combines with pBt->pPager->iDataVersion */ Btree *pNext; /* List of other sharable Btrees from the same db */ Btree *pPrev; /* Back pointer of the same list */ +#ifdef SQLITE_DEBUG + u64 nSeek; /* Calls to sqlite3BtreeMovetoUnpacked() */ +#endif #ifndef SQLITE_OMIT_SHARED_CACHE BtLock lock; /* Object used to lock page 1 */ #endif @@ -370,11 +368,25 @@ struct Btree { ** If the shared-data extension is enabled, there may be multiple users ** of the Btree structure. At most one of these may open a write transaction, ** but any number may have active read transactions. +** +** These values must match SQLITE_TXN_NONE, SQLITE_TXN_READ, and +** SQLITE_TXN_WRITE */ #define TRANS_NONE 0 #define TRANS_READ 1 #define TRANS_WRITE 2 +#if TRANS_NONE!=SQLITE_TXN_NONE +# error wrong numeric code for no-transaction +#endif +#if TRANS_READ!=SQLITE_TXN_READ +# error wrong numeric code for read-transaction +#endif +#if TRANS_WRITE!=SQLITE_TXN_WRITE +# error wrong numeric code for write-transaction +#endif + + /* ** An instance of this object represents a single database file. ** @@ -387,7 +399,7 @@ struct Btree { ** ** Fields in this structure are accessed under the BtShared.mutex ** mutex, except for nRef and pNext which are accessed under the -** global SQLITE_MUTEX_STATIC_MASTER mutex. The pPager field +** global SQLITE_MUTEX_STATIC_MAIN mutex. The pPager field ** may not be modified once it is initially set as long as nRef>0. ** The pSchema field may be set once under BtShared.mutex and ** thereafter is unchanged as long as nRef>0. @@ -423,9 +435,7 @@ struct BtShared { #endif u8 inTransaction; /* Transaction state */ u8 max1bytePayload; /* Maximum first byte of cell for a 1-byte payload */ -#ifdef SQLITE_HAS_CODEC - u8 optimalReserve; /* Desired amount of reserved space per page */ -#endif + u8 nReserveWanted; /* Desired number of extra bytes per page */ u16 btsFlags; /* Boolean parameters. See BTS_* macros below */ u16 maxLocal; /* Maximum local payload in non-LEAFDATA tables */ u16 minLocal; /* Minimum local payload in non-LEAFDATA tables */ @@ -446,6 +456,7 @@ struct BtShared { Btree *pWriter; /* Btree with currently open write transaction */ #endif u8 *pTmpSpace; /* Temp space sufficient to hold a single cell */ + int nPreformatSize; /* Size of last cell written by TransferRow() */ }; /* @@ -454,10 +465,12 @@ struct BtShared { #define BTS_READ_ONLY 0x0001 /* Underlying file is readonly */ #define BTS_PAGESIZE_FIXED 0x0002 /* Page size can no longer be changed */ #define BTS_SECURE_DELETE 0x0004 /* PRAGMA secure_delete is enabled */ -#define BTS_INITIALLY_EMPTY 0x0008 /* Database was empty at trans start */ -#define BTS_NO_WAL 0x0010 /* Do not open write-ahead-log files */ -#define BTS_EXCLUSIVE 0x0020 /* pWriter has an exclusive lock */ -#define BTS_PENDING 0x0040 /* Waiting for read-locks to clear */ +#define BTS_OVERWRITE 0x0008 /* Overwrite deleted content with zeros */ +#define BTS_FAST_SECURE 0x000c /* Combination of the previous two */ +#define BTS_INITIALLY_EMPTY 0x0010 /* Database was empty at trans start */ +#define BTS_NO_WAL 0x0020 /* Do not open write-ahead-log files */ +#define BTS_EXCLUSIVE 0x0040 /* pWriter has an exclusive lock */ +#define BTS_PENDING 0x0080 /* Waiting for read-locks to clear */ /* ** An instance of the following structure is used to hold information @@ -483,6 +496,12 @@ struct CellInfo { */ #define BTCURSOR_MAX_DEPTH 20 +/* +** Maximum amount of storage local to a database page, regardless of +** page size. +*/ +#define BT_MAX_LOCAL 65501 /* 65536 - 35 */ + /* ** A cursor is a pointer to a particular entry within a particular ** b-tree within a database file. @@ -498,35 +517,43 @@ struct CellInfo { ** found at self->pBt->mutex. ** ** skipNext meaning: -** eState==SKIPNEXT && skipNext>0: Next sqlite3BtreeNext() is no-op. -** eState==SKIPNEXT && skipNext<0: Next sqlite3BtreePrevious() is no-op. -** eState==FAULT: Cursor fault with skipNext as error code. +** The meaning of skipNext depends on the value of eState: +** +** eState Meaning of skipNext +** VALID skipNext is meaningless and is ignored +** INVALID skipNext is meaningless and is ignored +** SKIPNEXT sqlite3BtreeNext() is a no-op if skipNext>0 and +** sqlite3BtreePrevious() is no-op if skipNext<0. +** REQUIRESEEK restoreCursorPosition() restores the cursor to +** eState=SKIPNEXT if skipNext!=0 +** FAULT skipNext holds the cursor fault error code. */ struct BtCursor { - Btree *pBtree; /* The Btree to which this cursor belongs */ - BtShared *pBt; /* The BtShared this cursor points to */ - BtCursor *pNext; /* Forms a linked list of all cursors */ - Pgno *aOverflow; /* Cache of overflow page locations */ - CellInfo info; /* A parse of the cell we are pointing at */ - i64 nKey; /* Size of pKey, or last integer key */ - void *pKey; /* Saved key that was cursor last known position */ - Pgno pgnoRoot; /* The root page of this tree */ - int nOvflAlloc; /* Allocated size of aOverflow[] array */ - int skipNext; /* Prev() is noop if negative. Next() is noop if positive. - ** Error code if eState==CURSOR_FAULT */ + u8 eState; /* One of the CURSOR_XXX constants (see below) */ u8 curFlags; /* zero or more BTCF_* flags defined below */ u8 curPagerFlags; /* Flags to send to sqlite3PagerGet() */ - u8 eState; /* One of the CURSOR_XXX constants (see below) */ u8 hints; /* As configured by CursorSetHints() */ + int skipNext; /* Prev() is noop if negative. Next() is noop if positive. + ** Error code if eState==CURSOR_FAULT */ + Btree *pBtree; /* The Btree to which this cursor belongs */ + Pgno *aOverflow; /* Cache of overflow page locations */ + void *pKey; /* Saved key that was cursor last known position */ /* All fields above are zeroed when the cursor is allocated. See ** sqlite3BtreeCursorZero(). Fields that follow must be manually ** initialized. */ +#define BTCURSOR_FIRST_UNINIT pBt /* Name of first uninitialized field */ + BtShared *pBt; /* The BtShared this cursor points to */ + BtCursor *pNext; /* Forms a linked list of all cursors */ + CellInfo info; /* A parse of the cell we are pointing at */ + i64 nKey; /* Size of pKey, or last integer key */ + Pgno pgnoRoot; /* The root page of this tree */ i8 iPage; /* Index of current page in apPage */ u8 curIntKey; /* Value of apPage[0]->intKey */ - struct KeyInfo *pKeyInfo; /* Argument passed to comparison function */ - void *padding1; /* Make object size a multiple of 16 */ - u16 aiIdx[BTCURSOR_MAX_DEPTH]; /* Current index in apPage[i] */ - MemPage *apPage[BTCURSOR_MAX_DEPTH]; /* Pages from root to current page */ + u16 ix; /* Current index for apPage[iPage] */ + u16 aiIdx[BTCURSOR_MAX_DEPTH-1]; /* Current index in apPage[i] */ + struct KeyInfo *pKeyInfo; /* Arg passed to comparison function */ + MemPage *pPage; /* Current page */ + MemPage *apPage[BTCURSOR_MAX_DEPTH-1]; /* Stack of parents of current page */ }; /* @@ -535,9 +562,10 @@ struct BtCursor { #define BTCF_WriteFlag 0x01 /* True if a write cursor */ #define BTCF_ValidNKey 0x02 /* True if info.nKey is valid */ #define BTCF_ValidOvfl 0x04 /* True if aOverflow is valid */ -#define BTCF_AtLast 0x08 /* Cursor is pointing ot the last entry */ +#define BTCF_AtLast 0x08 /* Cursor is pointing to the last entry */ #define BTCF_Incrblob 0x10 /* True if an incremental I/O handle */ #define BTCF_Multiple 0x20 /* Maybe another cursor on the same btree */ +#define BTCF_Pinned 0x40 /* Cursor is busy and cannot be moved */ /* ** Potential values for BtCursor.eState. @@ -569,8 +597,8 @@ struct BtCursor { ** Do nothing else with this cursor. Any attempt to use the cursor ** should return the error code stored in BtCursor.skipNext */ -#define CURSOR_INVALID 0 -#define CURSOR_VALID 1 +#define CURSOR_VALID 0 +#define CURSOR_INVALID 1 #define CURSOR_SKIPNEXT 2 #define CURSOR_REQUIRESEEK 3 #define CURSOR_FAULT 4 @@ -578,7 +606,7 @@ struct BtCursor { /* ** The database page the PENDING_BYTE occupies. This page is never used. */ -# define PENDING_BYTE_PAGE(pBt) PAGER_MJ_PGNO(pBt) +#define PENDING_BYTE_PAGE(pBt) ((Pgno)((PENDING_BYTE/((pBt)->pageSize))+1)) /* ** These macros define the location of the pointer-map entry for a @@ -652,15 +680,15 @@ struct BtCursor { ** So, this macro is defined instead. */ #ifndef SQLITE_OMIT_AUTOVACUUM -#define ISAUTOVACUUM (pBt->autoVacuum) +#define ISAUTOVACUUM(pBt) (pBt->autoVacuum) #else -#define ISAUTOVACUUM 0 +#define ISAUTOVACUUM(pBt) 0 #endif /* -** This structure is passed around through all the sanity checking routines -** in order to keep track of some global state information. +** This structure is passed around through all the PRAGMA integrity_check +** checking routines in order to keep track of some global state information. ** ** The aRef[] array is allocated so that there is 1 bit for each page in ** the database. As the integrity-check proceeds, for each page used in @@ -673,14 +701,19 @@ struct IntegrityCk { BtShared *pBt; /* The tree being checked out */ Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */ u8 *aPgRef; /* 1 bit per page in the db (see above) */ - Pgno nPage; /* Number of pages in the database */ + Pgno nCkPage; /* Pages in the database. 0 for partial check */ int mxErr; /* Stop accumulating errors when this reaches zero */ int nErr; /* Number of messages written to zErrMsg so far */ - int mallocFailed; /* A memory allocation error has occurred */ + int rc; /* SQLITE_OK, SQLITE_NOMEM, or SQLITE_INTERRUPT */ + u32 nStep; /* Number of steps into the integrity_check process */ const char *zPfx; /* Error message prefix */ - int v1, v2; /* Values for up to two %d fields in zPfx */ + Pgno v0; /* Value for first %u substitution in zPfx (root page) */ + Pgno v1; /* Value for second %u substitution in zPfx (current pg) */ + int v2; /* Value for third %d substitution in zPfx */ StrAccum errMsg; /* Accumulate the error message text here */ u32 *heap; /* Min-heap used for analyzing cell coverage */ + sqlite3 *db; /* Database connection running the check */ + i64 nRow; /* Number of rows visited in current tree */ }; /* @@ -693,16 +726,14 @@ struct IntegrityCk { /* ** get2byteAligned(), unlike get2byte(), requires that its argument point to a -** two-byte aligned address. get2bytea() is only used for accessing the +** two-byte aligned address. get2byteAligned() is only used for accessing the ** cell addresses in a btree header. */ #if SQLITE_BYTEORDER==4321 # define get2byteAligned(x) (*(u16*)(x)) -#elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \ - && GCC_VERSION>=4008000 +#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4008000 # define get2byteAligned(x) __builtin_bswap16(*(u16*)(x)) -#elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \ - && defined(_MSC_VER) && _MSC_VER>=1300 +#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300 # define get2byteAligned(x) _byteswap_ushort(*(u16*)(x)) #else # define get2byteAligned(x) ((x)[0]<<8 | (x)[1]) diff --git a/src/build.c b/src/build.c index 250dc20d20..de890c2e91 100644 --- a/src/build.c +++ b/src/build.c @@ -30,14 +30,14 @@ ** codeTableLocks() functions. */ struct TableLock { - int iDb; /* The database containing the table to be locked */ - int iTab; /* The root page of the table to be locked */ - u8 isWriteLock; /* True for write lock. False for a read lock */ - const char *zName; /* Name of the table */ + int iDb; /* The database containing the table to be locked */ + Pgno iTab; /* The root page of the table to be locked */ + u8 isWriteLock; /* True for write lock. False for a read lock */ + const char *zLockName; /* Name of the table */ }; /* -** Record the fact that we want to lock a table at run-time. +** Record the fact that we want to lock a table at run-time. ** ** The table to be locked has root page iTab and is found in database iDb. ** A read or a write lock can be taken depending on isWritelock. @@ -46,19 +46,20 @@ struct TableLock { ** code to make the lock occur is generated by a later call to ** codeTableLocks() which occurs during sqlite3FinishCoding(). */ -void sqlite3TableLock( +static SQLITE_NOINLINE void lockTable( Parse *pParse, /* Parsing context */ int iDb, /* Index of the database containing the table to lock */ - int iTab, /* Root page number of the table to be locked */ + Pgno iTab, /* Root page number of the table to be locked */ u8 isWriteLock, /* True for a write lock */ const char *zName /* Name of the table to be locked */ ){ - Parse *pToplevel = sqlite3ParseToplevel(pParse); + Parse *pToplevel; int i; int nBytes; TableLock *p; assert( iDb>=0 ); + pToplevel = sqlite3ParseToplevel(pParse); for(i=0; inTableLock; i++){ p = &pToplevel->aTableLock[i]; if( p->iDb==iDb && p->iTab==iTab ){ @@ -67,6 +68,7 @@ void sqlite3TableLock( } } + assert( pToplevel->nTableLock < 0x7fff0000 ); nBytes = sizeof(TableLock) * (pToplevel->nTableLock+1); pToplevel->aTableLock = sqlite3DbReallocOrFree(pToplevel->db, pToplevel->aTableLock, nBytes); @@ -75,12 +77,23 @@ void sqlite3TableLock( p->iDb = iDb; p->iTab = iTab; p->isWriteLock = isWriteLock; - p->zName = zName; + p->zLockName = zName; }else{ pToplevel->nTableLock = 0; sqlite3OomFault(pToplevel->db); } } +void sqlite3TableLock( + Parse *pParse, /* Parsing context */ + int iDb, /* Index of the database containing the table to lock */ + Pgno iTab, /* Root page number of the table to be locked */ + u8 isWriteLock, /* True for a write lock */ + const char *zName /* Name of the table to be locked */ +){ + if( iDb==1 ) return; + if( !sqlite3BtreeSharable(pParse->db->aDb[iDb].pBt) ) return; + lockTable(pParse, iDb, iTab, isWriteLock, zName); +} /* ** Code an OP_TableLock instruction for each table locked by the @@ -88,16 +101,14 @@ void sqlite3TableLock( */ static void codeTableLocks(Parse *pParse){ int i; - Vdbe *pVdbe; - - pVdbe = sqlite3GetVdbe(pParse); - assert( pVdbe!=0 ); /* sqlite3GetVdbe cannot fail: VDBE already allocated */ + Vdbe *pVdbe = pParse->pVdbe; + assert( pVdbe!=0 ); for(i=0; inTableLock; i++){ TableLock *p = &pParse->aTableLock[i]; int p1 = p->iDb; sqlite3VdbeAddOp4(pVdbe, OP_TableLock, p1, p->iTab, p->isWriteLock, - p->zName, P4_STATIC); + p->zLockName, P4_STATIC); } } #else @@ -130,35 +141,56 @@ int sqlite3DbMaskAllZero(yDbMask m){ void sqlite3FinishCoding(Parse *pParse){ sqlite3 *db; Vdbe *v; + int iDb, i; assert( pParse->pToplevel==0 ); db = pParse->db; + assert( db->pParse==pParse ); if( pParse->nested ) return; - if( db->mallocFailed || pParse->nErr ){ - if( pParse->rc==SQLITE_OK ) pParse->rc = SQLITE_ERROR; + if( pParse->nErr ){ + if( db->mallocFailed ) pParse->rc = SQLITE_NOMEM; return; } + assert( db->mallocFailed==0 ); /* Begin by generating some termination code at the end of the ** vdbe program */ - v = sqlite3GetVdbe(pParse); - assert( !pParse->isMultiWrite + v = pParse->pVdbe; + if( v==0 ){ + if( db->init.busy ){ + pParse->rc = SQLITE_DONE; + return; + } + v = sqlite3GetVdbe(pParse); + if( v==0 ) pParse->rc = SQLITE_ERROR; + } + assert( !pParse->isMultiWrite || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort)); if( v ){ - while( sqlite3VdbeDeletePriorOpcode(v, OP_Close) ){} - sqlite3VdbeAddOp0(v, OP_Halt); - -#if SQLITE_USER_AUTHENTICATION - if( pParse->nTableLock>0 && db->init.busy==0 ){ - sqlite3UserAuthInit(db); - if( db->auth.authLevelrc = SQLITE_AUTH_USER; - sqlite3ErrorMsg(pParse, "user not authenticated"); - return; + if( pParse->bReturning ){ + Returning *pReturning; + int addrRewind; + int reg; + + assert( !pParse->isCreate ); + pReturning = pParse->u1.d.pReturning; + if( pReturning->nRetCol ){ + sqlite3VdbeAddOp0(v, OP_FkCheck); + addrRewind = + sqlite3VdbeAddOp1(v, OP_Rewind, pReturning->iRetCur); + VdbeCoverage(v); + reg = pReturning->iRetReg; + for(i=0; inRetCol; i++){ + sqlite3VdbeAddOp3(v, OP_Column, pReturning->iRetCur, i, reg+i); + } + sqlite3VdbeAddOp2(v, OP_ResultRow, reg, i); + sqlite3VdbeAddOp2(v, OP_Next, pReturning->iRetCur, addrRewind+1); + VdbeCoverage(v); + sqlite3VdbeJumpHere(v, addrRewind); } } -#endif + sqlite3VdbeAddOp0(v, OP_Halt); /* The cookie mask contains one bit for each database file open. ** (Bit 0 is for main, bit 1 is for temp, and so forth.) Bits are @@ -166,131 +198,130 @@ void sqlite3FinishCoding(Parse *pParse){ ** transaction on each used database and to verify the schema cookie ** on each used database. */ - if( db->mallocFailed==0 - && (DbMaskNonZero(pParse->cookieMask) || pParse->pConstExpr) - ){ - int iDb, i; - assert( sqlite3VdbeGetOp(v, 0)->opcode==OP_Init ); - sqlite3VdbeJumpHere(v, 0); - for(iDb=0; iDbnDb; iDb++){ - if( DbMaskTest(pParse->cookieMask, iDb)==0 ) continue; - sqlite3VdbeUsesBtree(v, iDb); - sqlite3VdbeAddOp4Int(v, - OP_Transaction, /* Opcode */ - iDb, /* P1 */ - DbMaskTest(pParse->writeMask,iDb), /* P2 */ - pParse->cookieValue[iDb], /* P3 */ - db->aDb[iDb].pSchema->iGeneration /* P4 */ - ); - if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1); - VdbeComment((v, - "usesStmtJournal=%d", pParse->mayAbort && pParse->isMultiWrite)); - } + assert( pParse->nErr>0 || sqlite3VdbeGetOp(v, 0)->opcode==OP_Init ); + sqlite3VdbeJumpHere(v, 0); + assert( db->nDb>0 ); + iDb = 0; + do{ + Schema *pSchema; + if( DbMaskTest(pParse->cookieMask, iDb)==0 ) continue; + sqlite3VdbeUsesBtree(v, iDb); + pSchema = db->aDb[iDb].pSchema; + sqlite3VdbeAddOp4Int(v, + OP_Transaction, /* Opcode */ + iDb, /* P1 */ + DbMaskTest(pParse->writeMask,iDb), /* P2 */ + pSchema->schema_cookie, /* P3 */ + pSchema->iGeneration /* P4 */ + ); + if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1); + VdbeComment((v, + "usesStmtJournal=%d", pParse->mayAbort && pParse->isMultiWrite)); + }while( ++iDbnDb ); #ifndef SQLITE_OMIT_VIRTUALTABLE - for(i=0; inVtabLock; i++){ - char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]); - sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB); - } - pParse->nVtabLock = 0; + for(i=0; inVtabLock; i++){ + char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]); + sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB); + } + pParse->nVtabLock = 0; #endif - /* Once all the cookies have been verified and transactions opened, - ** obtain the required table-locks. This is a no-op unless the - ** shared-cache feature is enabled. - */ - codeTableLocks(pParse); +#ifndef SQLITE_OMIT_SHARED_CACHE + /* Once all the cookies have been verified and transactions opened, + ** obtain the required table-locks. This is a no-op unless the + ** shared-cache feature is enabled. + */ + if( pParse->nTableLock ) codeTableLocks(pParse); +#endif - /* Initialize any AUTOINCREMENT data structures required. - */ - sqlite3AutoincrementBegin(pParse); - - /* Code constant expressions that where factored out of inner loops */ - if( pParse->pConstExpr ){ - ExprList *pEL = pParse->pConstExpr; - pParse->okConstFactor = 0; - for(i=0; inExpr; i++){ - sqlite3ExprCode(pParse, pEL->a[i].pExpr, pEL->a[i].u.iConstExprReg); - } + /* Initialize any AUTOINCREMENT data structures required. + */ + if( pParse->pAinc ) sqlite3AutoincrementBegin(pParse); + + /* Code constant expressions that were factored out of inner loops. + */ + if( pParse->pConstExpr ){ + ExprList *pEL = pParse->pConstExpr; + pParse->okConstFactor = 0; + for(i=0; inExpr; i++){ + assert( pEL->a[i].u.iConstExprReg>0 ); + sqlite3ExprCode(pParse, pEL->a[i].pExpr, pEL->a[i].u.iConstExprReg); } + } - /* Finally, jump back to the beginning of the executable code. */ - sqlite3VdbeGoto(v, 1); + if( pParse->bReturning ){ + Returning *pRet; + assert( !pParse->isCreate ); + pRet = pParse->u1.d.pReturning; + if( pRet->nRetCol ){ + sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pRet->iRetCur, pRet->nRetCol); + } } - } + /* Finally, jump back to the beginning of the executable code. */ + sqlite3VdbeGoto(v, 1); + } /* Get the VDBE program ready for execution */ - if( v && pParse->nErr==0 && !db->mallocFailed ){ - assert( pParse->iCacheLevel==0 ); /* Disables and re-enables match */ + assert( v!=0 || pParse->nErr ); + assert( db->mallocFailed==0 || pParse->nErr ); + if( pParse->nErr==0 ){ /* A minimum of one cursor is required if autoincrement is used * See ticket [a696379c1f08866] */ - if( pParse->pAinc!=0 && pParse->nTab==0 ) pParse->nTab = 1; + assert( pParse->pAinc==0 || pParse->nTab>0 ); sqlite3VdbeMakeReady(v, pParse); pParse->rc = SQLITE_DONE; }else{ pParse->rc = SQLITE_ERROR; } - - /* We are done with this Parse object. There is no need to de-initialize it */ -#if 0 - pParse->colNamesSet = 0; - pParse->nTab = 0; - pParse->nMem = 0; - pParse->nSet = 0; - pParse->nVar = 0; - DbMaskZero(pParse->cookieMask); -#endif } /* ** Run the parser and code generator recursively in order to generate ** code for the SQL statement given onto the end of the pParse context -** currently under construction. When the parser is run recursively -** this way, the final OP_Halt is not appended and other initialization -** and finalization steps are omitted because those are handling by the -** outermost parser. +** currently under construction. Notes: +** +** * The final OP_Halt is not appended and other initialization +** and finalization steps are omitted because those are handling by the +** outermost parser. ** -** Not everything is nestable. This facility is designed to permit -** INSERT, UPDATE, and DELETE operations against SQLITE_MASTER. Use -** care if you decide to try to use this routine for some other purposes. +** * Built-in SQL functions always take precedence over application-defined +** SQL functions. In other words, it is not possible to override a +** built-in function. */ void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){ va_list ap; char *zSql; - char *zErrMsg = 0; sqlite3 *db = pParse->db; -# define SAVE_SZ (sizeof(Parse) - offsetof(Parse,nVar)) - char saveBuf[SAVE_SZ]; + u32 savedDbFlags = db->mDbFlags; + char saveBuf[PARSE_TAIL_SZ]; if( pParse->nErr ) return; + if( pParse->eParseMode ) return; assert( pParse->nested<10 ); /* Nesting should only be of limited depth */ va_start(ap, zFormat); zSql = sqlite3VMPrintf(db, zFormat, ap); va_end(ap); if( zSql==0 ){ - return; /* A malloc must have failed */ + /* This can result either from an OOM or because the formatted string + ** exceeds SQLITE_LIMIT_LENGTH. In the latter case, we need to set + ** an error */ + if( !db->mallocFailed ) pParse->rc = SQLITE_TOOBIG; + pParse->nErr++; + return; } pParse->nested++; - memcpy(saveBuf, &pParse->nVar, SAVE_SZ); - memset(&pParse->nVar, 0, SAVE_SZ); - sqlite3RunParser(pParse, zSql, &zErrMsg); - sqlite3DbFree(db, zErrMsg); + memcpy(saveBuf, PARSE_TAIL(pParse), PARSE_TAIL_SZ); + memset(PARSE_TAIL(pParse), 0, PARSE_TAIL_SZ); + db->mDbFlags |= DBFLAG_PreferBuiltin; + sqlite3RunParser(pParse, zSql); + db->mDbFlags = savedDbFlags; sqlite3DbFree(db, zSql); - memcpy(&pParse->nVar, saveBuf, SAVE_SZ); + memcpy(PARSE_TAIL(pParse), saveBuf, PARSE_TAIL_SZ); pParse->nested--; } -#if SQLITE_USER_AUTHENTICATION -/* -** Return TRUE if zTable is the name of the system table that stores the -** list of users and their access credentials. -*/ -int sqlite3UserAuthTable(const char *zTable){ - return sqlite3_stricmp(zTable, "sqlite_user")==0; -} -#endif - /* ** Locate the in-memory structure that describes a particular database ** table given the name of that table and (optionally) the name of the @@ -309,19 +340,57 @@ Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){ /* All mutexes are required for schema access. Make sure we hold them. */ assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) ); -#if SQLITE_USER_AUTHENTICATION - /* Only the admin user is allowed to know that the sqlite_user table - ** exists */ - if( db->auth.authLevelnDb; i++){ - int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */ - if( zDatabase!=0 && sqlite3StrICmp(zDatabase, db->aDb[j].zName) ) continue; - assert( sqlite3SchemaMutexHeld(db, j, 0) ); - p = sqlite3HashFind(&db->aDb[j].pSchema->tblHash, zName); - if( p ) break; + if( zDatabase ){ + for(i=0; inDb; i++){ + if( sqlite3StrICmp(zDatabase, db->aDb[i].zDbSName)==0 ) break; + } + if( i>=db->nDb ){ + /* No match against the official names. But always match "main" + ** to schema 0 as a legacy fallback. */ + if( sqlite3StrICmp(zDatabase,"main")==0 ){ + i = 0; + }else{ + return 0; + } + } + p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, zName); + if( p==0 && sqlite3StrNICmp(zName, "sqlite_", 7)==0 ){ + if( i==1 ){ + if( sqlite3StrICmp(zName+7, &PREFERRED_TEMP_SCHEMA_TABLE[7])==0 + || sqlite3StrICmp(zName+7, &PREFERRED_SCHEMA_TABLE[7])==0 + || sqlite3StrICmp(zName+7, &LEGACY_SCHEMA_TABLE[7])==0 + ){ + p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash, + LEGACY_TEMP_SCHEMA_TABLE); + } + }else{ + if( sqlite3StrICmp(zName+7, &PREFERRED_SCHEMA_TABLE[7])==0 ){ + p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, + LEGACY_SCHEMA_TABLE); + } + } + } + }else{ + /* Match against TEMP first */ + p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash, zName); + if( p ) return p; + /* The main database is second */ + p = sqlite3HashFind(&db->aDb[0].pSchema->tblHash, zName); + if( p ) return p; + /* Attached databases are in order of attachment */ + for(i=2; inDb; i++){ + assert( sqlite3SchemaMutexHeld(db, i, 0) ); + p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, zName); + if( p ) break; + } + if( p==0 && sqlite3StrNICmp(zName, "sqlite_", 7)==0 ){ + if( sqlite3StrICmp(zName+7, &PREFERRED_SCHEMA_TABLE[7])==0 ){ + p = sqlite3HashFind(&db->aDb[0].pSchema->tblHash, LEGACY_SCHEMA_TABLE); + }else if( sqlite3StrICmp(zName+7, &PREFERRED_TEMP_SCHEMA_TABLE[7])==0 ){ + p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash, + LEGACY_TEMP_SCHEMA_TABLE); + } + } } return p; } @@ -338,38 +407,63 @@ Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){ */ Table *sqlite3LocateTable( Parse *pParse, /* context in which to report errors */ - int isView, /* True if looking for a VIEW rather than a TABLE */ + u32 flags, /* LOCATE_VIEW or LOCATE_NOERR */ const char *zName, /* Name of the table we are looking for */ const char *zDbase /* Name of the database. Might be NULL */ ){ Table *p; + sqlite3 *db = pParse->db; /* Read the database schema. If an error occurs, leave an error message ** and code in pParse and return NULL. */ - if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ + if( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0 + && SQLITE_OK!=sqlite3ReadSchema(pParse) + ){ return 0; } - p = sqlite3FindTable(pParse->db, zName, zDbase); + p = sqlite3FindTable(db, zName, zDbase); if( p==0 ){ - const char *zMsg = isView ? "no such view" : "no such table"; #ifndef SQLITE_OMIT_VIRTUALTABLE - if( sqlite3FindDbName(pParse->db, zDbase)<1 ){ - /* If zName is the not the name of a table in the schema created using - ** CREATE, then check to see if it is the name of an virtual table that - ** can be an eponymous virtual table. */ - Module *pMod = (Module*)sqlite3HashFind(&pParse->db->aModule, zName); + /* If zName is the not the name of a table in the schema created using + ** CREATE, then check to see if it is the name of an virtual table that + ** can be an eponymous virtual table. */ + if( (pParse->prepFlags & SQLITE_PREPARE_NO_VTAB)==0 && db->init.busy==0 ){ + Module *pMod = (Module*)sqlite3HashFind(&db->aModule, zName); + if( pMod==0 && sqlite3_strnicmp(zName, "pragma_", 7)==0 ){ + pMod = sqlite3PragmaVtabRegister(db, zName); + } +#ifndef SQLITE_OMIT_JSON + if( pMod==0 && sqlite3_strnicmp(zName, "json", 4)==0 ){ + pMod = sqlite3JsonVtabRegister(db, zName); + } +#endif +#ifdef SQLITE_ENABLE_CARRAY + if( pMod==0 && sqlite3_stricmp(zName, "carray")==0 ){ + pMod = sqlite3CarrayRegister(db); + } +#endif if( pMod && sqlite3VtabEponymousTableInit(pParse, pMod) ){ + testcase( pMod->pEpoTab==0 ); return pMod->pEpoTab; } } #endif + if( flags & LOCATE_NOERR ) return 0; + pParse->checkSchema = 1; + }else if( IsVirtual(p) && (pParse->prepFlags & SQLITE_PREPARE_NO_VTAB)!=0 ){ + p = 0; + } + + if( p==0 ){ + const char *zMsg = flags & LOCATE_VIEW ? "no such view" : "no such table"; if( zDbase ){ sqlite3ErrorMsg(pParse, "%s: %s.%s", zMsg, zDbase, zName); }else{ sqlite3ErrorMsg(pParse, "%s: %s", zMsg, zName); } - pParse->checkSchema = 1; + }else{ + assert( HasRowid(p) || p->iPKey<0 ); } return p; @@ -385,23 +479,39 @@ Table *sqlite3LocateTable( ** sqlite3FixSrcList() for details. */ Table *sqlite3LocateTableItem( - Parse *pParse, - int isView, - struct SrcList_item *p + Parse *pParse, + u32 flags, + SrcItem *p ){ const char *zDb; - assert( p->pSchema==0 || p->zDatabase==0 ); - if( p->pSchema ){ - int iDb = sqlite3SchemaToIndex(pParse->db, p->pSchema); - zDb = pParse->db->aDb[iDb].zName; + if( p->fg.fixedSchema ){ + int iDb = sqlite3SchemaToIndex(pParse->db, p->u4.pSchema); + zDb = pParse->db->aDb[iDb].zDbSName; }else{ - zDb = p->zDatabase; + assert( !p->fg.isSubquery ); + zDb = p->u4.zDatabase; } - return sqlite3LocateTable(pParse, isView, p->zName, zDb); + return sqlite3LocateTable(pParse, flags, p->zName, zDb); } /* -** Locate the in-memory structure that describes +** Return the preferred table name for system tables. Translate legacy +** names into the new preferred names, as appropriate. +*/ +const char *sqlite3PreferredTableName(const char *zName){ + if( sqlite3StrNICmp(zName, "sqlite_", 7)==0 ){ + if( sqlite3StrICmp(zName+7, &LEGACY_SCHEMA_TABLE[7])==0 ){ + return PREFERRED_SCHEMA_TABLE; + } + if( sqlite3StrICmp(zName+7, &LEGACY_TEMP_SCHEMA_TABLE[7])==0 ){ + return PREFERRED_TEMP_SCHEMA_TABLE; + } + } + return zName; +} + +/* +** Locate the in-memory structure that describes ** a particular index given the name of that index ** and the name of the database that contains the index. ** Return NULL if not found. @@ -421,7 +531,7 @@ Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const char *zDb){ int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */ Schema *pSchema = db->aDb[j].pSchema; assert( pSchema ); - if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zName) ) continue; + if( zDb && sqlite3DbIsNamed(db, j, zDb)==0 ) continue; assert( sqlite3SchemaMutexHeld(db, j, 0) ); p = sqlite3HashFind(&pSchema->idxHash, zName); if( p ) break; @@ -432,7 +542,7 @@ Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const char *zDb){ /* ** Reclaim the memory used by an index */ -static void freeIndex(sqlite3 *db, Index *p){ +void sqlite3FreeIndex(sqlite3 *db, Index *p){ #ifndef SQLITE_OMIT_ANALYZE sqlite3DeleteIndexSamples(db, p); #endif @@ -440,7 +550,7 @@ static void freeIndex(sqlite3 *db, Index *p){ sqlite3ExprListDelete(db, p->aColExpr); sqlite3DbFree(db, p->zColAff); if( p->isResized ) sqlite3DbFree(db, (void *)p->azColl); -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 +#ifdef SQLITE_ENABLE_STAT4 sqlite3_free(p->aiRowEst); #endif sqlite3DbFree(db, p); @@ -472,9 +582,9 @@ void sqlite3UnlinkAndDeleteIndex(sqlite3 *db, int iDb, const char *zIdxName){ p->pNext = pIndex->pNext; } } - freeIndex(db, pIndex); + sqlite3FreeIndex(db, pIndex); } - db->flags |= SQLITE_InternChanges; + db->mDbFlags |= DBFLAG_SchemaChange; } /* @@ -490,8 +600,8 @@ void sqlite3CollapseDatabaseArray(sqlite3 *db){ for(i=j=2; inDb; i++){ struct Db *pDb = &db->aDb[i]; if( pDb->pBt==0 ){ - sqlite3DbFree(db, pDb->zName); - pDb->zName = 0; + sqlite3DbFree(db, pDb->zDbSName); + pDb->zDbSName = 0; continue; } if( jnSchemaLock is not zero. +** Deferred resets may be run by calling with iDb<0. */ void sqlite3ResetOneSchema(sqlite3 *db, int iDb){ - Db *pDb; + int i; assert( iDbnDb ); - /* Case 1: Reset the single schema identified by iDb */ - pDb = &db->aDb[iDb]; - assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); - assert( pDb->pSchema!=0 ); - sqlite3SchemaClear(pDb->pSchema); + if( iDb>=0 ){ + assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); + DbSetProperty(db, iDb, DB_ResetWanted); + DbSetProperty(db, 1, DB_ResetWanted); + db->mDbFlags &= ~DBFLAG_SchemaKnownOk; + } - /* If any database other than TEMP is reset, then also reset TEMP - ** since TEMP might be holding triggers that reference tables in the - ** other database. - */ - if( iDb!=1 ){ - pDb = &db->aDb[1]; - assert( pDb->pSchema!=0 ); - sqlite3SchemaClear(pDb->pSchema); + if( db->nSchemaLock==0 ){ + for(i=0; inDb; i++){ + if( DbHasProperty(db, i, DB_ResetWanted) ){ + sqlite3SchemaClear(db->aDb[i].pSchema); + } + } } - return; } /* @@ -543,20 +652,104 @@ void sqlite3ResetAllSchemasOfConnection(sqlite3 *db){ for(i=0; inDb; i++){ Db *pDb = &db->aDb[i]; if( pDb->pSchema ){ - sqlite3SchemaClear(pDb->pSchema); + if( db->nSchemaLock==0 ){ + sqlite3SchemaClear(pDb->pSchema); + }else{ + DbSetProperty(db, i, DB_ResetWanted); + } } } - db->flags &= ~SQLITE_InternChanges; + db->mDbFlags &= ~(DBFLAG_SchemaChange|DBFLAG_SchemaKnownOk); sqlite3VtabUnlockList(db); sqlite3BtreeLeaveAll(db); - sqlite3CollapseDatabaseArray(db); + if( db->nSchemaLock==0 ){ + sqlite3CollapseDatabaseArray(db); + } } /* ** This routine is called when a commit occurs. */ void sqlite3CommitInternalChanges(sqlite3 *db){ - db->flags &= ~SQLITE_InternChanges; + db->mDbFlags &= ~DBFLAG_SchemaChange; +} + +/* +** Set the expression associated with a column. This is usually +** the DEFAULT value, but might also be the expression that computes +** the value for a generated column. +*/ +void sqlite3ColumnSetExpr( + Parse *pParse, /* Parsing context */ + Table *pTab, /* The table containing the column */ + Column *pCol, /* The column to receive the new DEFAULT expression */ + Expr *pExpr /* The new default expression */ +){ + ExprList *pList; + assert( IsOrdinaryTable(pTab) ); + pList = pTab->u.tab.pDfltList; + if( pCol->iDflt==0 + || NEVER(pList==0) + || NEVER(pList->nExpriDflt) + ){ + pCol->iDflt = pList==0 ? 1 : pList->nExpr+1; + pTab->u.tab.pDfltList = sqlite3ExprListAppend(pParse, pList, pExpr); + }else{ + sqlite3ExprDelete(pParse->db, pList->a[pCol->iDflt-1].pExpr); + pList->a[pCol->iDflt-1].pExpr = pExpr; + } +} + +/* +** Return the expression associated with a column. The expression might be +** the DEFAULT clause or the AS clause of a generated column. +** Return NULL if the column has no associated expression. +*/ +Expr *sqlite3ColumnExpr(Table *pTab, Column *pCol){ + if( pCol->iDflt==0 ) return 0; + if( !IsOrdinaryTable(pTab) ) return 0; + if( NEVER(pTab->u.tab.pDfltList==0) ) return 0; + if( NEVER(pTab->u.tab.pDfltList->nExpriDflt) ) return 0; + return pTab->u.tab.pDfltList->a[pCol->iDflt-1].pExpr; +} + +/* +** Set the collating sequence name for a column. +*/ +void sqlite3ColumnSetColl( + sqlite3 *db, + Column *pCol, + const char *zColl +){ + i64 nColl; + i64 n; + char *zNew; + assert( zColl!=0 ); + n = sqlite3Strlen30(pCol->zCnName) + 1; + if( pCol->colFlags & COLFLAG_HASTYPE ){ + n += sqlite3Strlen30(pCol->zCnName+n) + 1; + } + nColl = sqlite3Strlen30(zColl) + 1; + zNew = sqlite3DbRealloc(db, pCol->zCnName, nColl+n); + if( zNew ){ + pCol->zCnName = zNew; + memcpy(pCol->zCnName + n, zColl, nColl); + pCol->colFlags |= COLFLAG_HASCOLL; + } +} + +/* +** Return the collating sequence name for a column +*/ +const char *sqlite3ColumnColl(Column *pCol){ + const char *z; + if( (pCol->colFlags & COLFLAG_HASCOLL)==0 ) return 0; + z = pCol->zCnName; + while( *z ){ z++; } + if( pCol->colFlags & COLFLAG_HASTYPE ){ + do{ z++; }while( *z ); + } + return z+1; } /* @@ -567,15 +760,23 @@ void sqlite3DeleteColumnNames(sqlite3 *db, Table *pTable){ int i; Column *pCol; assert( pTable!=0 ); + assert( db!=0 ); if( (pCol = pTable->aCol)!=0 ){ for(i=0; inCol; i++, pCol++){ - sqlite3DbFree(db, pCol->zName); - sqlite3ExprDelete(db, pCol->pDflt); - sqlite3DbFree(db, pCol->zDflt); - sqlite3DbFree(db, pCol->zType); - sqlite3DbFree(db, pCol->zColl); + assert( pCol->zCnName==0 || pCol->hName==sqlite3StrIHash(pCol->zCnName) ); + sqlite3DbFree(db, pCol->zCnName); + } + sqlite3DbNNFreeNN(db, pTable->aCol); + if( IsOrdinaryTable(pTable) ){ + sqlite3ExprListDelete(db, pTable->u.tab.pDfltList); + } + if( db->pnBytesFreed==0 ){ + pTable->aCol = 0; + pTable->nCol = 0; + if( IsOrdinaryTable(pTable) ){ + pTable->u.tab.pDfltList = 0; + } } - sqlite3DbFree(db, pTable->aCol); } } @@ -585,64 +786,84 @@ void sqlite3DeleteColumnNames(sqlite3 *db, Table *pTable){ ** ** This routine just deletes the data structure. It does not unlink ** the table data structure from the hash table. But it does destroy -** memory structures of the indices and foreign keys associated with +** memory structures of the indices and foreign keys associated with ** the table. ** -** The db parameter is optional. It is needed if the Table object +** The db parameter is optional. It is needed if the Table object ** contains lookaside memory. (Table objects in the schema do not use ** lookaside memory, but some ephemeral Table objects do.) Or the ** db parameter can be used with db->pnBytesFreed to measure the memory ** used by the Table object. */ -void sqlite3DeleteTable(sqlite3 *db, Table *pTable){ +static void SQLITE_NOINLINE deleteTable(sqlite3 *db, Table *pTable){ Index *pIndex, *pNext; - TESTONLY( int nLookaside; ) /* Used to verify lookaside not used for schema */ - - assert( !pTable || pTable->nRef>0 ); - - /* Do not delete the table until the reference count reaches zero. */ - if( !pTable ) return; - if( ((!db || db->pnBytesFreed==0) && (--pTable->nRef)>0) ) return; +#ifdef SQLITE_DEBUG /* Record the number of outstanding lookaside allocations in schema Tables - ** prior to doing any free() operations. Since schema Tables do not use - ** lookaside, this number should not change. */ - TESTONLY( nLookaside = (db && (pTable->tabFlags & TF_Ephemeral)==0) ? - db->lookaside.nOut : 0 ); + ** prior to doing any free() operations. Since schema Tables do not use + ** lookaside, this number should not change. + ** + ** If malloc has already failed, it may be that it failed while allocating + ** a Table object that was going to be marked ephemeral. So do not check + ** that no lookaside memory is used in this case either. */ + int nLookaside = 0; + assert( db!=0 ); + if( !db->mallocFailed && (pTable->tabFlags & TF_Ephemeral)==0 ){ + nLookaside = sqlite3LookasideUsed(db, 0); + } +#endif /* Delete all indices associated with this table. */ for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){ pNext = pIndex->pNext; - assert( pIndex->pSchema==pTable->pSchema ); - if( !db || db->pnBytesFreed==0 ){ - char *zName = pIndex->zName; + assert( pIndex->pSchema==pTable->pSchema + || (IsVirtual(pTable) && pIndex->idxType!=SQLITE_IDXTYPE_APPDEF) ); + if( db->pnBytesFreed==0 && !IsVirtual(pTable) ){ + char *zName = pIndex->zName; TESTONLY ( Index *pOld = ) sqlite3HashInsert( &pIndex->pSchema->idxHash, zName, 0 ); assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) ); assert( pOld==pIndex || pOld==0 ); } - freeIndex(db, pIndex); + sqlite3FreeIndex(db, pIndex); } - /* Delete any foreign keys attached to this table. */ - sqlite3FkDelete(db, pTable); + if( IsOrdinaryTable(pTable) ){ + sqlite3FkDelete(db, pTable); + } +#ifndef SQLITE_OMIT_VIRTUALTABLE + else if( IsVirtual(pTable) ){ + sqlite3VtabClear(db, pTable); + } +#endif + else{ + assert( IsView(pTable) ); + sqlite3SelectDelete(db, pTable->u.view.pSelect); + } /* Delete the Table structure itself. */ sqlite3DeleteColumnNames(db, pTable); sqlite3DbFree(db, pTable->zName); sqlite3DbFree(db, pTable->zColAff); - sqlite3SelectDelete(db, pTable->pSelect); sqlite3ExprListDelete(db, pTable->pCheck); -#ifndef SQLITE_OMIT_VIRTUALTABLE - sqlite3VtabClear(db, pTable); -#endif sqlite3DbFree(db, pTable); /* Verify that no lookaside memory was used by schema tables */ - assert( nLookaside==0 || nLookaside==db->lookaside.nOut ); + assert( nLookaside==0 || nLookaside==sqlite3LookasideUsed(db,0) ); } +void sqlite3DeleteTable(sqlite3 *db, Table *pTable){ + /* Do not delete the table until the reference count reaches zero. */ + assert( db!=0 ); + if( !pTable ) return; + if( db->pnBytesFreed==0 && (--pTable->nTabRef)>0 ) return; + deleteTable(db, pTable); +} +void sqlite3DeleteTableGeneric(sqlite3 *db, void *pTable){ + sqlite3DeleteTable(db, (Table*)pTable); +} + /* ** Unlink the given table from the hash tables and the delete the @@ -660,7 +881,7 @@ void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){ pDb = &db->aDb[iDb]; p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName, 0); sqlite3DeleteTable(db, p); - db->flags |= SQLITE_InternChanges; + db->mDbFlags |= DBFLAG_SchemaChange; } /* @@ -676,10 +897,10 @@ void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){ ** are not \000 terminated and are not persistent. The returned string ** is \000 terminated and is persistent. */ -char *sqlite3NameFromToken(sqlite3 *db, Token *pName){ +char *sqlite3NameFromToken(sqlite3 *db, const Token *pName){ char *zName; if( pName ){ - zName = sqlite3DbStrNDup(db, (char*)pName->z, pName->n); + zName = sqlite3DbStrNDup(db, (const char*)pName->z, pName->n); sqlite3Dequote(zName); }else{ zName = 0; @@ -688,13 +909,13 @@ char *sqlite3NameFromToken(sqlite3 *db, Token *pName){ } /* -** Open the sqlite_master table stored in database number iDb for +** Open the sqlite_schema table stored in database number iDb for ** writing. The table is opened using cursor 0. */ -void sqlite3OpenMasterTable(Parse *p, int iDb){ +void sqlite3OpenSchemaTable(Parse *p, int iDb){ Vdbe *v = sqlite3GetVdbe(p); - sqlite3TableLock(p, iDb, MASTER_ROOT, 1, SCHEMA_TABLE(iDb)); - sqlite3VdbeAddOp4Int(v, OP_OpenWrite, 0, MASTER_ROOT, iDb, 5); + sqlite3TableLock(p, iDb, SCHEMA_ROOT, 1, LEGACY_SCHEMA_TABLE); + sqlite3VdbeAddOp4Int(v, OP_OpenWrite, 0, SCHEMA_ROOT, iDb, 5); if( p->nTab==0 ){ p->nTab = 1; } @@ -710,12 +931,11 @@ int sqlite3FindDbName(sqlite3 *db, const char *zName){ int i = -1; /* Database number */ if( zName ){ Db *pDb; - int n = sqlite3Strlen30(zName); for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){ - if( (!OMIT_TEMPDB || i!=1 ) && n==sqlite3Strlen30(pDb->zName) && - 0==sqlite3StrICmp(pDb->zName, zName) ){ - break; - } + if( 0==sqlite3_stricmp(pDb->zDbSName, zName) ) break; + /* "main" is always an acceptable alias for the primary database + ** even if it has been renamed using SQLITE_DBCONFIG_MAINDBNAME. */ + if( i==0 && 0==sqlite3_stricmp("main", zName) ) break; } } return i; @@ -724,7 +944,7 @@ int sqlite3FindDbName(sqlite3 *db, const char *zName){ /* ** The token *pName contains the name of a database (either "main" or ** "temp" or the name of an attached db). This routine returns the -** index of the named database in db->aDb[], or -1 if the named db +** index of the named database in db->aDb[], or -1 if the named db ** does not exist. */ int sqlite3FindDb(sqlite3 *db, Token *pName){ @@ -740,7 +960,7 @@ int sqlite3FindDb(sqlite3 *db, Token *pName){ ** pName1 and pName2. If the table name was fully qualified, for example: ** ** CREATE TABLE xxx.yyy (...); -** +** ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if ** the table name is not fully qualified, i.e.: ** @@ -774,26 +994,70 @@ int sqlite3TwoPartName( return -1; } }else{ - assert( db->init.iDb==0 || db->init.busy ); + assert( db->init.iDb==0 || db->init.busy || IN_SPECIAL_PARSE + || (db->mDbFlags & DBFLAG_Vacuum)!=0); iDb = db->init.iDb; *pUnqual = pName1; } return iDb; } +/* +** True if PRAGMA writable_schema is ON +*/ +int sqlite3WritableSchema(sqlite3 *db){ + testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==0 ); + testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))== + SQLITE_WriteSchema ); + testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))== + SQLITE_Defensive ); + testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))== + (SQLITE_WriteSchema|SQLITE_Defensive) ); + return (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==SQLITE_WriteSchema; +} + /* ** This routine is used to check if the UTF-8 string zName is a legal ** unqualified name for a new schema object (table, index, view or ** trigger). All names are legal except those that begin with the string ** "sqlite_" (in upper, lower or mixed case). This portion of the namespace ** is reserved for internal use. +** +** When parsing the sqlite_schema table, this routine also checks to +** make sure the "type", "name", and "tbl_name" columns are consistent +** with the SQL. */ -int sqlite3CheckObjectName(Parse *pParse, const char *zName){ - if( !pParse->db->init.busy && pParse->nested==0 - && (pParse->db->flags & SQLITE_WriteSchema)==0 - && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){ - sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s", zName); - return SQLITE_ERROR; +int sqlite3CheckObjectName( + Parse *pParse, /* Parsing context */ + const char *zName, /* Name of the object to check */ + const char *zType, /* Type of this object */ + const char *zTblName /* Parent table name for triggers and indexes */ +){ + sqlite3 *db = pParse->db; + if( sqlite3WritableSchema(db) + || db->init.imposterTable + || !sqlite3Config.bExtraSchemaChecks + ){ + /* Skip these error checks for writable_schema=ON */ + return SQLITE_OK; + } + if( db->init.busy ){ + if( sqlite3_stricmp(zType, db->init.azInit[0]) + || sqlite3_stricmp(zName, db->init.azInit[1]) + || sqlite3_stricmp(zTblName, db->init.azInit[2]) + ){ + sqlite3ErrorMsg(pParse, ""); /* corruptSchema() will supply the error */ + return SQLITE_ERROR; + } + }else{ + if( (pParse->nested==0 && 0==sqlite3StrNICmp(zName, "sqlite_", 7)) + || (sqlite3ReadOnlyShadowTables(db) && sqlite3ShadowTableName(db, zName)) + ){ + sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s", + zName); + return SQLITE_ERROR; + } + } return SQLITE_OK; } @@ -808,17 +1072,120 @@ Index *sqlite3PrimaryKeyIndex(Table *pTab){ } /* -** Return the column of index pIdx that corresponds to table -** column iCol. Return -1 if not found. +** Convert an table column number into a index column number. That is, +** for the column iCol in the table (as defined by the CREATE TABLE statement) +** find the (first) offset of that column in index pIdx. Or return -1 +** if column iCol is not used in index pIdx. */ -i16 sqlite3ColumnOfIndex(Index *pIdx, i16 iCol){ +int sqlite3TableColumnToIndex(Index *pIdx, int iCol){ int i; + i16 iCol16; + assert( iCol>=(-1) && iCol<=SQLITE_MAX_COLUMN ); + assert( pIdx->nColumn<=SQLITE_MAX_COLUMN*2 ); + iCol16 = iCol; for(i=0; inColumn; i++){ - if( iCol==pIdx->aiColumn[i] ) return i; + if( iCol16==pIdx->aiColumn[i] ){ + return i; + } } return -1; } +#ifndef SQLITE_OMIT_GENERATED_COLUMNS +/* Convert a storage column number into a table column number. +** +** The storage column number (0,1,2,....) is the index of the value +** as it appears in the record on disk. The true column number +** is the index (0,1,2,...) of the column in the CREATE TABLE statement. +** +** The storage column number is less than the table column number if +** and only there are VIRTUAL columns to the left. +** +** If SQLITE_OMIT_GENERATED_COLUMNS, this routine is a no-op macro. +*/ +i16 sqlite3StorageColumnToTable(Table *pTab, i16 iCol){ + if( pTab->tabFlags & TF_HasVirtual ){ + int i; + for(i=0; i<=iCol; i++){ + if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ) iCol++; + } + } + return iCol; +} +#endif + +#ifndef SQLITE_OMIT_GENERATED_COLUMNS +/* Convert a table column number into a storage column number. +** +** The storage column number (0,1,2,....) is the index of the value +** as it appears in the record on disk. Or, if the input column is +** the N-th virtual column (zero-based) then the storage number is +** the number of non-virtual columns in the table plus N. +** +** The true column number is the index (0,1,2,...) of the column in +** the CREATE TABLE statement. +** +** If the input column is a VIRTUAL column, then it should not appear +** in storage. But the value sometimes is cached in registers that +** follow the range of registers used to construct storage. This +** avoids computing the same VIRTUAL column multiple times, and provides +** values for use by OP_Param opcodes in triggers. Hence, if the +** input column is a VIRTUAL table, put it after all the other columns. +** +** In the following, N means "normal column", S means STORED, and +** V means VIRTUAL. Suppose the CREATE TABLE has columns like this: +** +** CREATE TABLE ex(N,S,V,N,S,V,N,S,V); +** -- 0 1 2 3 4 5 6 7 8 +** +** Then the mapping from this function is as follows: +** +** INPUTS: 0 1 2 3 4 5 6 7 8 +** OUTPUTS: 0 1 6 2 3 7 4 5 8 +** +** So, in other words, this routine shifts all the virtual columns to +** the end. +** +** If SQLITE_OMIT_GENERATED_COLUMNS then there are no virtual columns and +** this routine is a no-op macro. If the pTab does not have any virtual +** columns, then this routine is no-op that always return iCol. If iCol +** is negative (indicating the ROWID column) then this routine return iCol. +*/ +i16 sqlite3TableColumnToStorage(Table *pTab, i16 iCol){ + int i; + i16 n; + assert( iColnCol ); + if( (pTab->tabFlags & TF_HasVirtual)==0 || iCol<0 ) return iCol; + for(i=0, n=0; iaCol[i].colFlags & COLFLAG_VIRTUAL)==0 ) n++; + } + if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ){ + /* iCol is a virtual column itself */ + return pTab->nNVCol + i - n; + }else{ + /* iCol is a normal or stored column */ + return n; + } +} +#endif + +/* +** Insert a single OP_JournalMode query opcode in order to force the +** prepared statement to return false for sqlite3_stmt_readonly(). This +** is used by CREATE TABLE IF NOT EXISTS and similar if the table already +** exists, so that the prepared statement for CREATE TABLE IF NOT EXISTS +** will return false for sqlite3_stmt_readonly() even if that statement +** is a read-only no-op. +*/ +static void sqlite3ForceNotReadOnly(Parse *pParse){ + int iReg = ++pParse->nMem; + Vdbe *v = sqlite3GetVdbe(pParse); + if( v ){ + sqlite3VdbeAddOp3(v, OP_JournalMode, 0, iReg, PAGER_JOURNALMODE_QUERY); + sqlite3VdbeUsesBtree(v, 0); + } +} + /* ** Begin constructing a new table representation in memory. This is ** the first of several action routines that get called in response @@ -852,7 +1219,7 @@ void sqlite3StartTable( Token *pName; /* Unqualified name of the table to create */ if( db->init.busy && db->init.newTnum==1 ){ - /* Special case: Parsing the sqlite_master or sqlite_temp_master schema */ + /* Special case: Parsing the sqlite_schema or sqlite_temp_schema schema */ iDb = db->init.iDb; zName = sqlite3DbStrDup(db, SCHEMA_TABLE(iDb)); pName = pName1; @@ -861,17 +1228,20 @@ void sqlite3StartTable( iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName); if( iDb<0 ) return; if( !OMIT_TEMPDB && isTemp && pName2->n>0 && iDb!=1 ){ - /* If creating a temp table, the name may not be qualified. Unless + /* If creating a temp table, the name may not be qualified. Unless ** the database name is "temp" anyway. */ sqlite3ErrorMsg(pParse, "temporary table name must be unqualified"); return; } if( !OMIT_TEMPDB && isTemp ) iDb = 1; zName = sqlite3NameFromToken(db, pName); + if( IN_RENAME_OBJECT ){ + sqlite3RenameTokenMap(pParse, (void*)zName, pName); + } } pParse->sNameToken = *pName; if( zName==0 ) return; - if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ + if( sqlite3CheckObjectName(pParse, zName, isView?"view":"table", zName) ){ goto begin_table_error; } if( db->init.iDb==1 ) isTemp = 1; @@ -885,7 +1255,7 @@ void sqlite3StartTable( SQLITE_CREATE_VIEW, SQLITE_CREATE_TEMP_VIEW }; - char *zDb = db->aDb[iDb].zName; + char *zDb = db->aDb[iDb].zDbSName; if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){ goto begin_table_error; } @@ -903,18 +1273,20 @@ void sqlite3StartTable( ** and types will be used, so there is no need to test for namespace ** collisions. */ - if( !IN_DECLARE_VTAB ){ - char *zDb = db->aDb[iDb].zName; + if( !IN_SPECIAL_PARSE ){ + char *zDb = db->aDb[iDb].zDbSName; if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ goto begin_table_error; } pTable = sqlite3FindTable(db, zName, zDb); if( pTable ){ if( !noErr ){ - sqlite3ErrorMsg(pParse, "table %T already exists", pName); + sqlite3ErrorMsg(pParse, "%s %T already exists", + (IsView(pTable)? "view" : "table"), pName); }else{ assert( !db->init.busy || CORRUPT_DB ); sqlite3CodeVerifySchema(pParse, iDb); + sqlite3ForceNotReadOnly(pParse); } goto begin_table_error; } @@ -927,34 +1299,27 @@ void sqlite3StartTable( pTable = sqlite3DbMallocZero(db, sizeof(Table)); if( pTable==0 ){ assert( db->mallocFailed ); - pParse->rc = SQLITE_NOMEM; + pParse->rc = SQLITE_NOMEM_BKPT; pParse->nErr++; goto begin_table_error; } pTable->zName = zName; pTable->iPKey = -1; pTable->pSchema = db->aDb[iDb].pSchema; - pTable->nRef = 1; + pTable->nTabRef = 1; +#ifdef SQLITE_DEFAULT_ROWEST + pTable->nRowLogEst = sqlite3LogEst(SQLITE_DEFAULT_ROWEST); +#else pTable->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) ); +#endif assert( pParse->pNewTable==0 ); pParse->pNewTable = pTable; - /* If this is the magic sqlite_sequence table used by autoincrement, - ** then record a pointer to this table in the main database structure - ** so that INSERT can find the table easily. - */ -#ifndef SQLITE_OMIT_AUTOINCREMENT - if( !pParse->nested && strcmp(zName, "sqlite_sequence")==0 ){ - assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); - pTable->pSchema->pSeqTab = pTable; - } -#endif - /* Begin generating the code that will insert the table record into - ** the SQLITE_MASTER table. Note in particular that we must go ahead + ** the schema table. Note in particular that we must go ahead ** and allocate the record number for the table entry now. Before any ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause - ** indices to be created and the table record must come before the + ** indices to be created and the table record must come before the ** indices. Hence, the record number for the table must be allocated ** now. */ @@ -972,11 +1337,12 @@ void sqlite3StartTable( } #endif - /* If the file format and encoding in the database have not been set, + /* If the file format and encoding in the database have not been set, ** set them now. */ - reg1 = pParse->regRowid = ++pParse->nMem; - reg2 = pParse->regRoot = ++pParse->nMem; + assert( pParse->isCreate ); + reg1 = pParse->u1.cr.regRowid = ++pParse->nMem; + reg2 = pParse->u1.cr.regRoot = ++pParse->nMem; reg3 = ++pParse->nMem; sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, reg3, BTREE_FILE_FORMAT); sqlite3VdbeUsesBtree(v, iDb); @@ -987,12 +1353,12 @@ void sqlite3StartTable( sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_TEXT_ENCODING, ENC(db)); sqlite3VdbeJumpHere(v, addr1); - /* This just creates a place-holder record in the sqlite_master table. + /* This just creates a place-holder record in the sqlite_schema table. ** The record created does not contain anything yet. It will be replaced ** by the real entry in code generated at sqlite3EndTable(). ** - ** The rowid for the new entry is left in register pParse->regRowid. - ** The root page number of the new table is left in reg pParse->regRoot. + ** The rowid for the new entry is left in register pParse->u1.cr.regRowid. + ** The root page of the new table is left in reg pParse->u1.cr.regRoot. ** The rowid and root page number values are needed by the code that ** sqlite3EndTable will generate. */ @@ -1002,14 +1368,19 @@ void sqlite3StartTable( }else #endif { - pParse->addrCrTab = sqlite3VdbeAddOp2(v, OP_CreateTable, iDb, reg2); + assert( !pParse->bReturning ); + pParse->u1.cr.addrCrTab = + sqlite3VdbeAddOp3(v, OP_CreateBtree, iDb, reg2, BTREE_INTKEY); } - sqlite3OpenMasterTable(pParse, iDb); + sqlite3OpenSchemaTable(pParse, iDb); sqlite3VdbeAddOp2(v, OP_NewRowid, 0, reg1); sqlite3VdbeAddOp4(v, OP_Blob, 6, reg3, 0, nullRow, P4_STATIC); sqlite3VdbeAddOp3(v, OP_Insert, 0, reg3, reg1); sqlite3VdbeChangeP5(v, OPFLAG_APPEND); sqlite3VdbeAddOp0(v, OP_Close); + }else if( db->init.imposterTable ){ + pTable->tabFlags |= TF_Imposter; + if( db->init.imposterTable>=2 ) pTable->tabFlags |= TF_Readonly; } /* Normal (non-error) return. */ @@ -1017,6 +1388,7 @@ void sqlite3StartTable( /* If an error occurs, we jump here */ begin_table_error: + pParse->checkSchema = 1; sqlite3DbFree(db, zName); return; } @@ -1026,14 +1398,85 @@ void sqlite3StartTable( */ #if SQLITE_ENABLE_HIDDEN_COLUMNS void sqlite3ColumnPropertiesFromName(Table *pTab, Column *pCol){ - if( sqlite3_strnicmp(pCol->zName, "__hidden__", 10)==0 ){ + if( sqlite3_strnicmp(pCol->zCnName, "__hidden__", 10)==0 ){ pCol->colFlags |= COLFLAG_HIDDEN; + if( pTab ) pTab->tabFlags |= TF_HasHidden; }else if( pTab && pCol!=pTab->aCol && (pCol[-1].colFlags & COLFLAG_HIDDEN) ){ pTab->tabFlags |= TF_OOOHidden; } } #endif +/* +** Clean up the data structures associated with the RETURNING clause. +*/ +static void sqlite3DeleteReturning(sqlite3 *db, void *pArg){ + Returning *pRet = (Returning*)pArg; + Hash *pHash; + pHash = &(db->aDb[1].pSchema->trigHash); + sqlite3HashInsert(pHash, pRet->zName, 0); + sqlite3ExprListDelete(db, pRet->pReturnEL); + sqlite3DbFree(db, pRet); +} + +/* +** Add the RETURNING clause to the parse currently underway. +** +** This routine creates a special TEMP trigger that will fire for each row +** of the DML statement. That TEMP trigger contains a single SELECT +** statement with a result set that is the argument of the RETURNING clause. +** The trigger has the Trigger.bReturning flag and an opcode of +** TK_RETURNING instead of TK_SELECT, so that the trigger code generator +** knows to handle it specially. The TEMP trigger is automatically +** removed at the end of the parse. +** +** When this routine is called, we do not yet know if the RETURNING clause +** is attached to a DELETE, INSERT, or UPDATE, so construct it as a +** RETURNING trigger instead. It will then be converted into the appropriate +** type on the first call to sqlite3TriggersExist(). +*/ +void sqlite3AddReturning(Parse *pParse, ExprList *pList){ + Returning *pRet; + Hash *pHash; + sqlite3 *db = pParse->db; + if( pParse->pNewTrigger ){ + sqlite3ErrorMsg(pParse, "cannot use RETURNING in a trigger"); + }else{ + assert( pParse->bReturning==0 || pParse->ifNotExists ); + } + pParse->bReturning = 1; + pRet = sqlite3DbMallocZero(db, sizeof(*pRet)); + if( pRet==0 ){ + sqlite3ExprListDelete(db, pList); + return; + } + assert( !pParse->isCreate ); + pParse->u1.d.pReturning = pRet; + pRet->pParse = pParse; + pRet->pReturnEL = pList; + sqlite3ParserAddCleanup(pParse, sqlite3DeleteReturning, pRet); + testcase( pParse->earlyCleanup ); + if( db->mallocFailed ) return; + sqlite3_snprintf(sizeof(pRet->zName), pRet->zName, + "sqlite_returning_%p", pParse); + pRet->retTrig.zName = pRet->zName; + pRet->retTrig.op = TK_RETURNING; + pRet->retTrig.tr_tm = TRIGGER_AFTER; + pRet->retTrig.bReturning = 1; + pRet->retTrig.pSchema = db->aDb[1].pSchema; + pRet->retTrig.pTabSchema = db->aDb[1].pSchema; + pRet->retTrig.step_list = &pRet->retTStep; + pRet->retTStep.op = TK_RETURNING; + pRet->retTStep.pTrig = &pRet->retTrig; + pRet->retTStep.pExprList = pList; + pHash = &(db->aDb[1].pSchema->trigHash); + assert( sqlite3HashFind(pHash, pRet->zName)==0 + || pParse->nErr || pParse->ifNotExists ); + if( sqlite3HashInsert(pHash, pRet->zName, &pRet->retTrig) + ==&pRet->retTrig ){ + sqlite3OomFault(db); + } +} /* ** Add a new column to the table currently being constructed. @@ -1043,49 +1486,112 @@ void sqlite3ColumnPropertiesFromName(Table *pTab, Column *pCol){ ** first to get things going. Then this routine is called for each ** column. */ -void sqlite3AddColumn(Parse *pParse, Token *pName){ +void sqlite3AddColumn(Parse *pParse, Token sName, Token sType){ Table *p; int i; char *z; + char *zType; Column *pCol; sqlite3 *db = pParse->db; + Column *aNew; + u8 eType = COLTYPE_CUSTOM; + u8 szEst = 1; + char affinity = SQLITE_AFF_BLOB; + if( (p = pParse->pNewTable)==0 ) return; -#if SQLITE_MAX_COLUMN if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){ sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName); return; } -#endif - z = sqlite3NameFromToken(db, pName); - if( z==0 ) return; - for(i=0; inCol; i++){ - if( sqlite3_stricmp(z, p->aCol[i].zName)==0 ){ - sqlite3ErrorMsg(pParse, "duplicate column name: %s", z); - sqlite3DbFree(db, z); - return; + if( !IN_RENAME_OBJECT ) sqlite3DequoteToken(&sName); + + /* Because keywords GENERATE ALWAYS can be converted into identifiers + ** by the parser, we can sometimes end up with a typename that ends + ** with "generated always". Check for this case and omit the surplus + ** text. */ + if( sType.n>=16 + && sqlite3_strnicmp(sType.z+(sType.n-6),"always",6)==0 + ){ + sType.n -= 6; + while( ALWAYS(sType.n>0) && sqlite3Isspace(sType.z[sType.n-1]) ) sType.n--; + if( sType.n>=9 + && sqlite3_strnicmp(sType.z+(sType.n-9),"generated",9)==0 + ){ + sType.n -= 9; + while( sType.n>0 && sqlite3Isspace(sType.z[sType.n-1]) ) sType.n--; } } - if( (p->nCol & 0x7)==0 ){ - Column *aNew; - aNew = sqlite3DbRealloc(db,p->aCol,(p->nCol+8)*sizeof(p->aCol[0])); - if( aNew==0 ){ - sqlite3DbFree(db, z); - return; + + /* Check for standard typenames. For standard typenames we will + ** set the Column.eType field rather than storing the typename after + ** the column name, in order to save space. */ + if( sType.n>=3 ){ + sqlite3DequoteToken(&sType); + for(i=0; iaCol = aNew; } - pCol = &p->aCol[p->nCol]; - memset(pCol, 0, sizeof(p->aCol[0])); - pCol->zName = z; - sqlite3ColumnPropertiesFromName(p, pCol); - - /* If there is no type specified, columns have the default affinity - ** 'BLOB'. If there is a type specified, then sqlite3AddColumnType() will - ** be called next to set pCol->affinity correctly. - */ - pCol->affinity = SQLITE_AFF_BLOB; - pCol->szEst = 1; + + z = sqlite3DbMallocRaw(db, (i64)sName.n + 1 + (i64)sType.n + (sType.n>0) ); + if( z==0 ) return; + if( IN_RENAME_OBJECT ) sqlite3RenameTokenMap(pParse, (void*)z, &sName); + memcpy(z, sName.z, sName.n); + z[sName.n] = 0; + sqlite3Dequote(z); + if( p->nCol && sqlite3ColumnIndex(p, z)>=0 ){ + sqlite3ErrorMsg(pParse, "duplicate column name: %s", z); + sqlite3DbFree(db, z); + return; + } + aNew = sqlite3DbRealloc(db,p->aCol,((i64)p->nCol+1)*sizeof(p->aCol[0])); + if( aNew==0 ){ + sqlite3DbFree(db, z); + return; + } + p->aCol = aNew; + pCol = &p->aCol[p->nCol]; + memset(pCol, 0, sizeof(p->aCol[0])); + pCol->zCnName = z; + pCol->hName = sqlite3StrIHash(z); + sqlite3ColumnPropertiesFromName(p, pCol); + + if( sType.n==0 ){ + /* If there is no type specified, columns have the default affinity + ** 'BLOB' with a default size of 4 bytes. */ + pCol->affinity = affinity; + pCol->eCType = eType; + pCol->szEst = szEst; +#ifdef SQLITE_ENABLE_SORTER_REFERENCES + if( affinity==SQLITE_AFF_BLOB ){ + if( 4>=sqlite3GlobalConfig.szSorterRef ){ + pCol->colFlags |= COLFLAG_SORTERREF; + } + } +#endif + }else{ + zType = z + sqlite3Strlen30(z) + 1; + memcpy(zType, sType.z, sType.n); + zType[sType.n] = 0; + sqlite3Dequote(zType); + pCol->affinity = sqlite3AffinityType(zType, pCol); + pCol->colFlags |= COLFLAG_HASTYPE; + } + if( p->nCol<=0xff ){ + u8 h = pCol->hName % sizeof(p->aHx); + p->aHx[h] = p->nCol; + } p->nCol++; + p->nNVCol++; + assert( pParse->isCreate ); + pParse->u1.cr.constraintName.n = 0; } /* @@ -1096,20 +1602,35 @@ void sqlite3AddColumn(Parse *pParse, Token *pName){ */ void sqlite3AddNotNull(Parse *pParse, int onError){ Table *p; + Column *pCol; p = pParse->pNewTable; if( p==0 || NEVER(p->nCol<1) ) return; - p->aCol[p->nCol-1].notNull = (u8)onError; + pCol = &p->aCol[p->nCol-1]; + pCol->notNull = (u8)onError; + p->tabFlags |= TF_HasNotNull; + + /* Set the uniqNotNull flag on any UNIQUE or PK indexes already created + ** on this column. */ + if( pCol->colFlags & COLFLAG_UNIQUE ){ + Index *pIdx; + for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){ + assert( pIdx->nKeyCol==1 && pIdx->onError!=OE_None ); + if( pIdx->aiColumn[0]==p->nCol-1 ){ + pIdx->uniqNotNull = 1; + } + } + } } /* ** Scan the column type name zType (length nType) and return the ** associated affinity type. ** -** This routine does a case-independent search of zType for the +** This routine does a case-independent search of zType for the ** substrings in the following table. If one of the substrings is ** found, the corresponding affinity is returned. If zType contains -** more than one of the substrings, entries toward the top of -** the table take priority. For example, if zType is 'BLOBINT', +** more than one of the substrings, entries toward the top of +** the table take priority. For example, if zType is 'BLOBINT', ** SQLITE_AFF_INTEGER is returned. ** ** Substring | Affinity @@ -1126,14 +1647,15 @@ void sqlite3AddNotNull(Parse *pParse, int onError){ ** If none of the substrings in the above table are found, ** SQLITE_AFF_NUMERIC is returned. */ -char sqlite3AffinityType(const char *zIn, u8 *pszEst){ +char sqlite3AffinityType(const char *zIn, Column *pCol){ u32 h = 0; char aff = SQLITE_AFF_NUMERIC; const char *zChar = 0; - if( zIn==0 ) return aff; + assert( zIn!=0 ); while( zIn[0] ){ - h = (h<<8) + sqlite3UpperToLower[(*zIn)&0xff]; + u8 x = *(u8*)zIn; + h = (h<<8) + sqlite3UpperToLower[x]; zIn++; if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){ /* CHAR */ aff = SQLITE_AFF_TEXT; @@ -1163,53 +1685,36 @@ char sqlite3AffinityType(const char *zIn, u8 *pszEst){ } } - /* If pszEst is not NULL, store an estimate of the field size. The + /* If pCol is not NULL, store an estimate of the field size. The ** estimate is scaled so that the size of an integer is 1. */ - if( pszEst ){ - *pszEst = 1; /* default size is approx 4 bytes */ + if( pCol ){ + int v = 0; /* default size is approx 4 bytes */ if( aff r=(k/4+1) */ sqlite3GetInt32(zChar, &v); - v = v/4 + 1; - if( v>255 ) v = 255; - *pszEst = v; /* BLOB(k), VARCHAR(k), CHAR(k) -> r=(k/4+1) */ break; } zChar++; } }else{ - *pszEst = 5; /* BLOB, TEXT, CLOB -> r=5 (approx 20 bytes)*/ + v = 16; /* BLOB, TEXT, CLOB -> r=5 (approx 20 bytes)*/ } } +#ifdef SQLITE_ENABLE_SORTER_REFERENCES + if( v>=sqlite3GlobalConfig.szSorterRef ){ + pCol->colFlags |= COLFLAG_SORTERREF; + } +#endif + v = v/4 + 1; + if( v>255 ) v = 255; + pCol->szEst = v; } return aff; } -/* -** This routine is called by the parser while in the middle of -** parsing a CREATE TABLE statement. The pFirst token is the first -** token in the sequence of tokens that describe the type of the -** column currently under construction. pLast is the last token -** in the sequence. Use this information to construct a string -** that contains the typename of the column and store that string -** in zType. -*/ -void sqlite3AddColumnType(Parse *pParse, Token *pType){ - Table *p; - Column *pCol; - - p = pParse->pNewTable; - if( p==0 || NEVER(p->nCol<1) ) return; - pCol = &p->aCol[p->nCol-1]; - assert( pCol->zType==0 || CORRUPT_DB ); - sqlite3DbFree(pParse->db, pCol->zType); - pCol->zType = sqlite3NameFromToken(pParse->db, pType); - pCol->affinity = sqlite3AffinityType(pCol->zType, &pCol->szEst); -} - /* ** The expression is the default value for the most recently added column ** of the table currently under construction. @@ -1220,34 +1725,52 @@ void sqlite3AddColumnType(Parse *pParse, Token *pType){ ** This routine is called by the parser while in the middle of ** parsing a CREATE TABLE statement. */ -void sqlite3AddDefaultValue(Parse *pParse, ExprSpan *pSpan){ +void sqlite3AddDefaultValue( + Parse *pParse, /* Parsing context */ + Expr *pExpr, /* The parsed expression of the default value */ + const char *zStart, /* Start of the default value text */ + const char *zEnd /* First character past end of default value text */ +){ Table *p; Column *pCol; sqlite3 *db = pParse->db; p = pParse->pNewTable; if( p!=0 ){ + int isInit = db->init.busy && db->init.iDb!=1; pCol = &(p->aCol[p->nCol-1]); - if( !sqlite3ExprIsConstantOrFunction(pSpan->pExpr, db->init.busy) ){ + if( !sqlite3ExprIsConstantOrFunction(pExpr, isInit) ){ sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant", - pCol->zName); + pCol->zCnName); +#ifndef SQLITE_OMIT_GENERATED_COLUMNS + }else if( pCol->colFlags & COLFLAG_GENERATED ){ + testcase( pCol->colFlags & COLFLAG_VIRTUAL ); + testcase( pCol->colFlags & COLFLAG_STORED ); + sqlite3ErrorMsg(pParse, "cannot use DEFAULT on a generated column"); +#endif }else{ /* A copy of pExpr is used instead of the original, as pExpr contains - ** tokens that point to volatile memory. The 'span' of the expression - ** is required by pragma table_info. + ** tokens that point to volatile memory. */ - sqlite3ExprDelete(db, pCol->pDflt); - pCol->pDflt = sqlite3ExprDup(db, pSpan->pExpr, EXPRDUP_REDUCE); - sqlite3DbFree(db, pCol->zDflt); - pCol->zDflt = sqlite3DbStrNDup(db, (char*)pSpan->zStart, - (int)(pSpan->zEnd - pSpan->zStart)); + Expr x, *pDfltExpr; + memset(&x, 0, sizeof(x)); + x.op = TK_SPAN; + x.u.zToken = sqlite3DbSpanDup(db, zStart, zEnd); + x.pLeft = pExpr; + x.flags = EP_Skip; + pDfltExpr = sqlite3ExprDup(db, &x, EXPRDUP_REDUCE); + sqlite3DbFree(db, x.u.zToken); + sqlite3ColumnSetExpr(pParse, p, pCol, pDfltExpr); } } - sqlite3ExprDelete(db, pSpan->pExpr); + if( IN_RENAME_OBJECT ){ + sqlite3RenameExprUnmap(pParse, pExpr); + } + sqlite3ExprDelete(db, pExpr); } /* ** Backwards Compatibility Hack: -** +** ** Historical versions of SQLite accepted strings as column names in ** indexes and PRIMARY KEY constraints and in UNIQUE constraints. Example: ** @@ -1258,7 +1781,7 @@ void sqlite3AddDefaultValue(Parse *pParse, ExprSpan *pSpan){ ** accept it. This routine does the necessary conversion. It converts ** the expression given in its argument from a TK_STRING into a TK_ID ** if the expression is just a TK_STRING with an optional COLLATE clause. -** If the epxression is anything other than TK_STRING, the expression is +** If the expression is anything other than TK_STRING, the expression is ** unchanged. */ static void sqlite3StringToId(Expr *p){ @@ -1270,7 +1793,22 @@ static void sqlite3StringToId(Expr *p){ } /* -** Designate the PRIMARY KEY for the table. pList is a list of names +** Tag the given column as being part of the PRIMARY KEY +*/ +static void makeColumnPartOfPrimaryKey(Parse *pParse, Column *pCol){ + pCol->colFlags |= COLFLAG_PRIMKEY; +#ifndef SQLITE_OMIT_GENERATED_COLUMNS + if( pCol->colFlags & COLFLAG_GENERATED ){ + testcase( pCol->colFlags & COLFLAG_VIRTUAL ); + testcase( pCol->colFlags & COLFLAG_STORED ); + sqlite3ErrorMsg(pParse, + "generated columns cannot be part of the PRIMARY KEY"); + } +#endif +} + +/* +** Designate the PRIMARY KEY for the table. pList is a list of names ** of columns that form the primary key. If pList is NULL, then the ** most recently added column of the table is the primary key. ** @@ -1295,20 +1833,20 @@ void sqlite3AddPrimaryKey( int sortOrder /* SQLITE_SO_ASC or SQLITE_SO_DESC */ ){ Table *pTab = pParse->pNewTable; - char *zType = 0; + Column *pCol = 0; int iCol = -1, i; int nTerm; - if( pTab==0 || IN_DECLARE_VTAB ) goto primary_key_exit; + if( pTab==0 ) goto primary_key_exit; if( pTab->tabFlags & TF_HasPrimaryKey ){ - sqlite3ErrorMsg(pParse, + sqlite3ErrorMsg(pParse, "table \"%s\" has more than one primary key", pTab->zName); goto primary_key_exit; } pTab->tabFlags |= TF_HasPrimaryKey; if( pList==0 ){ iCol = pTab->nCol - 1; - pTab->aCol[iCol].colFlags |= COLFLAG_PRIMKEY; - zType = pTab->aCol[iCol].zType; + pCol = &pTab->aCol[iCol]; + makeColumnPartOfPrimaryKey(pParse, pCol); nTerm = 1; }else{ nTerm = pList->nExpr; @@ -1317,38 +1855,38 @@ void sqlite3AddPrimaryKey( assert( pCExpr!=0 ); sqlite3StringToId(pCExpr); if( pCExpr->op==TK_ID ){ - const char *zCName = pCExpr->u.zToken; - for(iCol=0; iColnCol; iCol++){ - if( sqlite3StrICmp(zCName, pTab->aCol[iCol].zName)==0 ){ - pTab->aCol[iCol].colFlags |= COLFLAG_PRIMKEY; - zType = pTab->aCol[iCol].zType; - break; - } + assert( !ExprHasProperty(pCExpr, EP_IntValue) ); + iCol = sqlite3ColumnIndex(pTab, pCExpr->u.zToken); + if( iCol>=0 ){ + pCol = &pTab->aCol[iCol]; + makeColumnPartOfPrimaryKey(pParse, pCol); } } } } if( nTerm==1 - && zType && sqlite3StrICmp(zType, "INTEGER")==0 + && pCol + && pCol->eCType==COLTYPE_INTEGER && sortOrder!=SQLITE_SO_DESC ){ + if( IN_RENAME_OBJECT && pList ){ + Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[0].pExpr); + sqlite3RenameTokenRemap(pParse, &pTab->iPKey, pCExpr); + } pTab->iPKey = iCol; pTab->keyConf = (u8)onError; assert( autoInc==0 || autoInc==1 ); pTab->tabFlags |= autoInc*TF_Autoincrement; - if( pList ) pParse->iPkSortOrder = pList->a[0].sortOrder; + if( pList ) pParse->iPkSortOrder = pList->a[0].fg.sortFlags; + (void)sqlite3HasExplicitNulls(pParse, pList); }else if( autoInc ){ #ifndef SQLITE_OMIT_AUTOINCREMENT sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an " "INTEGER PRIMARY KEY"); #endif }else{ - Index *p; - p = sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0, - 0, sortOrder, 0); - if( p ){ - p->idxType = SQLITE_IDXTYPE_PRIMARYKEY; - } + sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0, + 0, sortOrder, 0, SQLITE_IDXTYPE_PRIMARYKEY); pList = 0; } @@ -1361,8 +1899,10 @@ void sqlite3AddPrimaryKey( ** Add a new CHECK constraint to the table currently under construction. */ void sqlite3AddCheckConstraint( - Parse *pParse, /* Parsing context */ - Expr *pCheckExpr /* The check expression */ + Parse *pParse, /* Parsing context */ + Expr *pCheckExpr, /* The check expression */ + const char *zStart, /* Opening "(" */ + const char *zEnd /* Closing ")" */ ){ #ifndef SQLITE_OMIT_CHECK Table *pTab = pParse->pNewTable; @@ -1371,8 +1911,17 @@ void sqlite3AddCheckConstraint( && !sqlite3BtreeIsReadonly(db->aDb[db->init.iDb].pBt) ){ pTab->pCheck = sqlite3ExprListAppend(pParse, pTab->pCheck, pCheckExpr); - if( pParse->constraintName.n ){ - sqlite3ExprListSetName(pParse, pTab->pCheck, &pParse->constraintName, 1); + assert( pParse->isCreate ); + if( pParse->u1.cr.constraintName.n ){ + sqlite3ExprListSetName(pParse, pTab->pCheck, + &pParse->u1.cr.constraintName, 1); + }else{ + Token t; + for(zStart++; sqlite3Isspace(zStart[0]); zStart++){} + while( sqlite3Isspace(zEnd[-1]) ){ zEnd--; } + t.z = zStart; + t.n = (int)(zEnd - t.z); + sqlite3ExprListSetName(pParse, pTab->pCheck, &t, 1); } }else #endif @@ -1391,7 +1940,7 @@ void sqlite3AddCollateType(Parse *pParse, Token *pToken){ char *zColl; /* Dequoted name of collation sequence */ sqlite3 *db; - if( (p = pParse->pNewTable)==0 ) return; + if( (p = pParse->pNewTable)==0 || IN_RENAME_OBJECT ) return; i = p->nCol-1; db = pParse->db; zColl = sqlite3NameFromToken(db, pToken); @@ -1399,9 +1948,8 @@ void sqlite3AddCollateType(Parse *pParse, Token *pToken){ if( sqlite3LocateCollSeq(pParse, zColl) ){ Index *pIdx; - sqlite3DbFree(db, p->aCol[i].zColl); - p->aCol[i].zColl = zColl; - + sqlite3ColumnSetColl(db, &p->aCol[i], zColl); + /* If the column is declared as " PRIMARY KEY COLLATE ", ** then an index may have been created on this column before the ** collation type was added. Correct this if it is the case. @@ -1409,49 +1957,73 @@ void sqlite3AddCollateType(Parse *pParse, Token *pToken){ for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){ assert( pIdx->nKeyCol==1 ); if( pIdx->aiColumn[0]==i ){ - pIdx->azColl[0] = p->aCol[i].zColl; + pIdx->azColl[0] = sqlite3ColumnColl(&p->aCol[i]); } } - }else{ - sqlite3DbFree(db, zColl); } + sqlite3DbFree(db, zColl); } -/* -** This function returns the collation sequence for database native text -** encoding identified by the string zName, length nName. -** -** If the requested collation sequence is not available, or not available -** in the database native encoding, the collation factory is invoked to -** request it. If the collation factory does not supply such a sequence, -** and the sequence is available in another text encoding, then that is -** returned instead. -** -** If no versions of the requested collations sequence are available, or -** another error occurs, NULL is returned and an error message written into -** pParse. -** -** This routine is a wrapper around sqlite3FindCollSeq(). This routine -** invokes the collation factory if the named collation cannot be found -** and generates an error message. -** -** See also: sqlite3FindCollSeq(), sqlite3GetCollSeq() +/* Change the most recently parsed column to be a GENERATED ALWAYS AS +** column. */ -CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName){ - sqlite3 *db = pParse->db; - u8 enc = ENC(db); - u8 initbusy = db->init.busy; - CollSeq *pColl; - - pColl = sqlite3FindCollSeq(db, enc, zName, initbusy); - if( !initbusy && (!pColl || !pColl->xCmp) ){ - pColl = sqlite3GetCollSeq(pParse, enc, pColl, zName); +void sqlite3AddGenerated(Parse *pParse, Expr *pExpr, Token *pType){ +#ifndef SQLITE_OMIT_GENERATED_COLUMNS + u8 eType = COLFLAG_VIRTUAL; + Table *pTab = pParse->pNewTable; + Column *pCol; + if( pTab==0 ){ + /* generated column in an CREATE TABLE IF NOT EXISTS that already exists */ + goto generated_done; + } + pCol = &(pTab->aCol[pTab->nCol-1]); + if( IN_DECLARE_VTAB ){ + sqlite3ErrorMsg(pParse, "virtual tables cannot use computed columns"); + goto generated_done; + } + if( pCol->iDflt>0 ) goto generated_error; + if( pType ){ + if( pType->n==7 && sqlite3StrNICmp("virtual",pType->z,7)==0 ){ + /* no-op */ + }else if( pType->n==6 && sqlite3StrNICmp("stored",pType->z,6)==0 ){ + eType = COLFLAG_STORED; + }else{ + goto generated_error; + } } - - return pColl; + if( eType==COLFLAG_VIRTUAL ) pTab->nNVCol--; + pCol->colFlags |= eType; + assert( TF_HasVirtual==COLFLAG_VIRTUAL ); + assert( TF_HasStored==COLFLAG_STORED ); + pTab->tabFlags |= eType; + if( pCol->colFlags & COLFLAG_PRIMKEY ){ + makeColumnPartOfPrimaryKey(pParse, pCol); /* For the error message */ + } + if( ALWAYS(pExpr) && pExpr->op==TK_ID ){ + /* The value of a generated column needs to be a real expression, not + ** just a reference to another column, in order for covering index + ** optimizations to work correctly. So if the value is not an expression, + ** turn it into one by adding a unary "+" operator. */ + pExpr = sqlite3PExpr(pParse, TK_UPLUS, pExpr, 0); + } + if( pExpr && pExpr->op!=TK_RAISE ) pExpr->affExpr = pCol->affinity; + sqlite3ColumnSetExpr(pParse, pTab, pCol, pExpr); + pExpr = 0; + goto generated_done; + +generated_error: + sqlite3ErrorMsg(pParse, "error in generated column \"%s\"", + pCol->zCnName); +generated_done: + sqlite3ExprDelete(pParse->db, pExpr); +#else + /* Throw and error for the GENERATED ALWAYS AS clause if the + ** SQLITE_OMIT_GENERATED_COLUMNS compile-time option is used. */ + sqlite3ErrorMsg(pParse, "generated columns not supported"); + sqlite3ExprDelete(pParse->db, pExpr); +#endif } - /* ** Generate code that will increment the schema cookie. ** @@ -1467,13 +2039,16 @@ CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName){ ** set back to prior value. But schema changes are infrequent ** and the probability of hitting the same cookie value is only ** 1 chance in 2^32. So we're safe enough. +** +** IMPLEMENTATION-OF: R-34230-56049 SQLite automatically increments +** the schema-version whenever the schema changes. */ void sqlite3ChangeCookie(Parse *pParse, int iDb){ sqlite3 *db = pParse->db; Vdbe *v = pParse->pVdbe; assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); - sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_SCHEMA_VERSION, - db->aDb[iDb].pSchema->schema_cookie+1); + sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_SCHEMA_VERSION, + (int)(1+(unsigned)db->aDb[iDb].pSchema->schema_cookie)); } /* @@ -1493,14 +2068,14 @@ static int identLength(const char *z){ } /* -** The first parameter is a pointer to an output buffer. The second +** The first parameter is a pointer to an output buffer. The second ** parameter is a pointer to an integer that contains the offset at ** which to write into the output buffer. This function copies the ** nul-terminated string pointed to by the third parameter, zSignedIdent, ** to the specified offset in the buffer and updates *pIdx to refer ** to the first byte after the last byte written before returning. -** -** If the string zSignedIdent consists entirely of alpha-numeric +** +** If the string zSignedIdent consists entirely of alphanumeric ** characters, does not begin with a digit and is not an SQL keyword, ** then it is copied to the output buffer exactly as it is. Otherwise, ** it is quoted using double-quotes. @@ -1534,16 +2109,17 @@ static void identPut(char *z, int *pIdx, char *zSignedIdent){ ** from sqliteMalloc() and must be freed by the calling function. */ static char *createTableStmt(sqlite3 *db, Table *p){ - int i, k, n; + int i, k, len; + i64 n; char *zStmt; char *zSep, *zSep2, *zEnd; Column *pCol; n = 0; for(pCol = p->aCol, i=0; inCol; i++, pCol++){ - n += identLength(pCol->zName) + 5; + n += identLength(pCol->zCnName) + 5; } n += identLength(p->zName); - if( n<50 ){ + if( n<50 ){ zSep = ""; zSep2 = ","; zEnd = ")"; @@ -1558,8 +2134,9 @@ static char *createTableStmt(sqlite3 *db, Table *p){ sqlite3OomFault(db); return 0; } - sqlite3_snprintf(n, zStmt, "CREATE TABLE "); - k = sqlite3Strlen30(zStmt); + assert( n>14 && n<=0x7fffffff ); + memcpy(zStmt, "CREATE TABLE ", 13); + k = 13; identPut(zStmt, &k, p->zName); zStmt[k++] = '('; for(pCol=p->aCol, i=0; inCol; i++, pCol++){ @@ -1568,15 +2145,18 @@ static char *createTableStmt(sqlite3 *db, Table *p){ /* SQLITE_AFF_TEXT */ " TEXT", /* SQLITE_AFF_NUMERIC */ " NUM", /* SQLITE_AFF_INTEGER */ " INT", - /* SQLITE_AFF_REAL */ " REAL" + /* SQLITE_AFF_REAL */ " REAL", + /* SQLITE_AFF_FLEXNUM */ " NUM", }; - int len; const char *zType; - sqlite3_snprintf(n-k, &zStmt[k], zSep); - k += sqlite3Strlen30(&zStmt[k]); + len = sqlite3Strlen30(zSep); + assert( k+lenzName); + identPut(zStmt, &k, pCol->zCnName); + assert( kaffinity-SQLITE_AFF_BLOB >= 0 ); assert( pCol->affinity-SQLITE_AFF_BLOB < ArraySize(azType) ); testcase( pCol->affinity==SQLITE_AFF_BLOB ); @@ -1584,16 +2164,21 @@ static char *createTableStmt(sqlite3 *db, Table *p){ testcase( pCol->affinity==SQLITE_AFF_NUMERIC ); testcase( pCol->affinity==SQLITE_AFF_INTEGER ); testcase( pCol->affinity==SQLITE_AFF_REAL ); - + testcase( pCol->affinity==SQLITE_AFF_FLEXNUM ); + zType = azType[pCol->affinity - SQLITE_AFF_BLOB]; len = sqlite3Strlen30(zType); - assert( pCol->affinity==SQLITE_AFF_BLOB + assert( pCol->affinity==SQLITE_AFF_BLOB + || pCol->affinity==SQLITE_AFF_FLEXNUM || pCol->affinity==sqlite3AffinityType(zType, 0) ); + assert( k+lennColumn>=N ) return SQLITE_OK; + db = pParse->db; + assert( N>0 ); + assert( N <= SQLITE_MAX_COLUMN*2 /* tag-20250221-1 */ ); + testcase( N==2*pParse->db->aLimit[SQLITE_LIMIT_COLUMN] ); assert( pIdx->isResized==0 ); - nByte = (sizeof(char*) + sizeof(i16) + 1)*N; + nByte = (sizeof(char*) + sizeof(LogEst) + sizeof(i16) + 1)*(u64)N; zExtra = sqlite3DbMallocZero(db, nByte); - if( zExtra==0 ) return SQLITE_NOMEM; + if( zExtra==0 ) return SQLITE_NOMEM_BKPT; memcpy(zExtra, pIdx->azColl, sizeof(char*)*pIdx->nColumn); pIdx->azColl = (const char**)zExtra; zExtra += sizeof(char*)*N; + memcpy(zExtra, pIdx->aiRowLogEst, sizeof(LogEst)*(pIdx->nKeyCol+1)); + pIdx->aiRowLogEst = (LogEst*)zExtra; + zExtra += sizeof(LogEst)*N; memcpy(zExtra, pIdx->aiColumn, sizeof(i16)*pIdx->nColumn); pIdx->aiColumn = (i16*)zExtra; zExtra += sizeof(i16)*N; memcpy(zExtra, pIdx->aSortOrder, pIdx->nColumn); pIdx->aSortOrder = (u8*)zExtra; - pIdx->nColumn = N; + pIdx->nColumn = (u16)N; /* See tag-20250221-1 above for proof of safety */ pIdx->isResized = 1; return SQLITE_OK; } @@ -1646,18 +2239,93 @@ static void estimateIndexWidth(Index *pIdx){ for(i=0; inColumn; i++){ i16 x = pIdx->aiColumn[i]; assert( xpTable->nCol ); - wIndex += x<0 ? 1 : aCol[pIdx->aiColumn[i]].szEst; + wIndex += x<0 ? 1 : aCol[x].szEst; } pIdx->szIdxRow = sqlite3LogEst(wIndex*4); } -/* Return true if value x is found any of the first nCol entries of aiCol[] +/* Return true if column number x is any of the first nCol entries of aiCol[]. +** This is used to determine if the column number x appears in any of the +** first nCol entries of an index. */ static int hasColumn(const i16 *aiCol, int nCol, int x){ - while( nCol-- > 0 ) if( x==*(aiCol++) ) return 1; + while( nCol-- > 0 ){ + if( x==*(aiCol++) ){ + return 1; + } + } + return 0; +} + +/* +** Return true if any of the first nKey entries of index pIdx exactly +** match the iCol-th entry of pPk. pPk is always a WITHOUT ROWID +** PRIMARY KEY index. pIdx is an index on the same table. pIdx may +** or may not be the same index as pPk. +** +** The first nKey entries of pIdx are guaranteed to be ordinary columns, +** not a rowid or expression. +** +** This routine differs from hasColumn() in that both the column and the +** collating sequence must match for this routine, but for hasColumn() only +** the column name must match. +*/ +static int isDupColumn(Index *pIdx, int nKey, Index *pPk, int iCol){ + int i, j; + assert( nKey<=pIdx->nColumn ); + assert( iColnColumn,pPk->nKeyCol) ); + assert( pPk->idxType==SQLITE_IDXTYPE_PRIMARYKEY ); + assert( pPk->pTable->tabFlags & TF_WithoutRowid ); + assert( pPk->pTable==pIdx->pTable ); + testcase( pPk==pIdx ); + j = pPk->aiColumn[iCol]; + assert( j!=XN_ROWID && j!=XN_EXPR ); + for(i=0; iaiColumn[i]>=0 || j>=0 ); + if( pIdx->aiColumn[i]==j + && sqlite3StrICmp(pIdx->azColl[i], pPk->azColl[iCol])==0 + ){ + return 1; + } + } return 0; } +/* Recompute the colNotIdxed field of the Index. +** +** colNotIdxed is a bitmask that has a 0 bit representing each indexed +** columns that are within the first 63 columns of the table and a 1 for +** all other bits (all columns that are not in the index). The +** high-order bit of colNotIdxed is always 1. All unindexed columns +** of the table have a 1. +** +** 2019-10-24: For the purpose of this computation, virtual columns are +** not considered to be covered by the index, even if they are in the +** index, because we do not trust the logic in whereIndexExprTrans() to be +** able to find all instances of a reference to the indexed table column +** and convert them into references to the index. Hence we always want +** the actual table at hand in order to recompute the virtual column, if +** necessary. +** +** The colNotIdxed mask is AND-ed with the SrcList.a[].colUsed mask +** to determine if the index is covering index. +*/ +static void recomputeColumnsNotIndexed(Index *pIdx){ + Bitmask m = 0; + int j; + Table *pTab = pIdx->pTable; + for(j=pIdx->nColumn-1; j>=0; j--){ + int x = pIdx->aiColumn[j]; + if( x>=0 && (pTab->aCol[x].colFlags & COLFLAG_VIRTUAL)==0 ){ + testcase( x==BMS-1 ); + testcase( x==BMS-2 ); + if( xcolNotIdxed = ~m; + assert( (pIdx->colNotIdxed>>63)==1 ); /* See note-20221022-a */ +} + /* ** This routine runs at the end of parsing a CREATE TABLE statement that ** has a WITHOUT ROWID clause. The job of this routine is to convert both @@ -1665,66 +2333,85 @@ static int hasColumn(const i16 *aiCol, int nCol, int x){ ** are appropriate for a WITHOUT ROWID table instead of a rowid table. ** Changes include: ** -** (1) Convert the OP_CreateTable into an OP_CreateIndex. There is -** no rowid btree for a WITHOUT ROWID. Instead, the canonical -** data storage is a covering index btree. -** (2) Bypass the creation of the sqlite_master table entry +** (1) Set all columns of the PRIMARY KEY schema object to be NOT NULL. +** (2) Convert P3 parameter of the OP_CreateBtree from BTREE_INTKEY +** into BTREE_BLOBKEY. +** (3) Bypass the creation of the sqlite_schema table entry ** for the PRIMARY KEY as the primary key index is now -** identified by the sqlite_master table entry of the table itself. -** (3) Set the Index.tnum of the PRIMARY KEY Index object in the +** identified by the sqlite_schema table entry of the table itself. +** (4) Set the Index.tnum of the PRIMARY KEY Index object in the ** schema to the rootpage from the main table. -** (4) Set all columns of the PRIMARY KEY schema object to be NOT NULL. ** (5) Add all table columns to the PRIMARY KEY Index object ** so that the PRIMARY KEY is a covering index. The surplus -** columns are part of KeyInfo.nXField and are not used for +** columns are part of KeyInfo.nAllField and are not used for ** sorting or lookup or uniqueness checks. ** (6) Replace the rowid tail on all automatically generated UNIQUE ** indices with the PRIMARY KEY columns. +** +** For virtual tables, only (1) is performed. */ static void convertToWithoutRowidTable(Parse *pParse, Table *pTab){ Index *pIdx; Index *pPk; int nPk; + int nExtra; int i, j; sqlite3 *db = pParse->db; Vdbe *v = pParse->pVdbe; - /* Convert the OP_CreateTable opcode that would normally create the - ** root-page for the table into an OP_CreateIndex opcode. The index - ** created will become the PRIMARY KEY index. + /* Mark every PRIMARY KEY column as NOT NULL (except for imposter tables) + */ + if( !db->init.imposterTable ){ + for(i=0; inCol; i++){ + if( (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY)!=0 + && (pTab->aCol[i].notNull==OE_None) + ){ + pTab->aCol[i].notNull = OE_Abort; + } + } + pTab->tabFlags |= TF_HasNotNull; + } + + /* Convert the P3 operand of the OP_CreateBtree opcode from BTREE_INTKEY + ** into BTREE_BLOBKEY. */ - if( pParse->addrCrTab ){ + assert( !pParse->bReturning ); + if( pParse->u1.cr.addrCrTab ){ assert( v ); - sqlite3VdbeChangeOpcode(v, pParse->addrCrTab, OP_CreateIndex); + sqlite3VdbeChangeP3(v, pParse->u1.cr.addrCrTab, BTREE_BLOBKEY); } /* Locate the PRIMARY KEY index. Or, if this table was originally - ** an INTEGER PRIMARY KEY table, create a new PRIMARY KEY index. + ** an INTEGER PRIMARY KEY table, create a new PRIMARY KEY index. */ if( pTab->iPKey>=0 ){ ExprList *pList; Token ipkToken; - sqlite3TokenInit(&ipkToken, pTab->aCol[pTab->iPKey].zName); - pList = sqlite3ExprListAppend(pParse, 0, + sqlite3TokenInit(&ipkToken, pTab->aCol[pTab->iPKey].zCnName); + pList = sqlite3ExprListAppend(pParse, 0, sqlite3ExprAlloc(db, TK_ID, &ipkToken, 0)); - if( pList==0 ) return; - pList->a[0].sortOrder = pParse->iPkSortOrder; + if( pList==0 ){ + pTab->tabFlags &= ~TF_WithoutRowid; + return; + } + if( IN_RENAME_OBJECT ){ + sqlite3RenameTokenRemap(pParse, pList->a[0].pExpr, &pTab->iPKey); + } + pList->a[0].fg.sortFlags = pParse->iPkSortOrder; assert( pParse->pNewTable==pTab ); - pPk = sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0); - if( pPk==0 ) return; - pPk->idxType = SQLITE_IDXTYPE_PRIMARYKEY; pTab->iPKey = -1; + sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0, + SQLITE_IDXTYPE_PRIMARYKEY); + if( pParse->nErr ){ + pTab->tabFlags &= ~TF_WithoutRowid; + return; + } + assert( db->mallocFailed==0 ); + pPk = sqlite3PrimaryKeyIndex(pTab); + assert( pPk->nKeyCol==1 ); }else{ pPk = sqlite3PrimaryKeyIndex(pTab); - - /* Bypass the creation of the PRIMARY KEY btree and the sqlite_master - ** table entry. This is only required if currently generating VDBE - ** code for a CREATE TABLE (not when parsing one as part of reading - ** a database schema). */ - if( v ){ - assert( db->init.busy==0 ); - sqlite3VdbeChangeOpcode(v, pPk->tnum, OP_Goto); - } + assert( pPk!=0 ); /* ** Remove all redundant columns from the PRIMARY KEY. For example, change @@ -1732,25 +2419,29 @@ static void convertToWithoutRowidTable(Parse *pParse, Table *pTab){ ** code assumes the PRIMARY KEY contains no repeated columns. */ for(i=j=1; inKeyCol; i++){ - if( hasColumn(pPk->aiColumn, j, pPk->aiColumn[i]) ){ + if( isDupColumn(pPk, j, pPk, i) ){ pPk->nColumn--; }else{ + testcase( hasColumn(pPk->aiColumn, j, pPk->aiColumn[i]) ); + pPk->azColl[j] = pPk->azColl[i]; + pPk->aSortOrder[j] = pPk->aSortOrder[i]; pPk->aiColumn[j++] = pPk->aiColumn[i]; } } pPk->nKeyCol = j; } - pPk->isCovering = 1; assert( pPk!=0 ); - nPk = pPk->nKeyCol; + pPk->isCovering = 1; + if( !db->init.imposterTable ) pPk->uniqNotNull = 1; + nPk = pPk->nColumn = pPk->nKeyCol; - /* Make sure every column of the PRIMARY KEY is NOT NULL. (Except, - ** do not enforce this for imposter tables.) */ - if( !db->init.imposterTable ){ - for(i=0; iaCol[pPk->aiColumn[i]].notNull = OE_Abort; - } - pPk->uniqNotNull = 1; + /* Bypass the creation of the PRIMARY KEY btree and the sqlite_schema + ** table entry. This is only required if currently generating VDBE + ** code for a CREATE TABLE (not when parsing one as part of reading + ** a database schema). */ + if( v && pPk->tnum>0 ){ + assert( db->init.busy==0 ); + sqlite3VdbeChangeOpcode(v, (int)pPk->tnum, OP_Goto); } /* The root page of the PRIMARY KEY is the table root page */ @@ -1763,18 +2454,26 @@ static void convertToWithoutRowidTable(Parse *pParse, Table *pTab){ int n; if( IsPrimaryKeyIndex(pIdx) ) continue; for(i=n=0; iaiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ) n++; + if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){ + testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ); + n++; + } } if( n==0 ){ /* This index is a superset of the primary key */ pIdx->nColumn = pIdx->nKeyCol; continue; } - if( resizeIndexObject(db, pIdx, pIdx->nKeyCol+n) ) return; + if( resizeIndexObject(pParse, pIdx, pIdx->nKeyCol+n) ) return; for(i=0, j=pIdx->nKeyCol; iaiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ){ + if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){ + testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ); pIdx->aiColumn[j] = pPk->aiColumn[i]; pIdx->azColl[j] = pPk->azColl[i]; + if( pPk->aSortOrder[i] ){ + /* See ticket https://sqlite.org/src/info/bba7b69f9849b5bf */ + pIdx->bAscKeyBug = 1; + } j++; } } @@ -1784,22 +2483,133 @@ static void convertToWithoutRowidTable(Parse *pParse, Table *pTab){ /* Add all table columns to the PRIMARY KEY index */ - if( nPknCol ){ - if( resizeIndexObject(db, pPk, pTab->nCol) ) return; - for(i=0, j=nPk; inCol; i++){ - if( !hasColumn(pPk->aiColumn, j, i) ){ - assert( jnColumn ); - pPk->aiColumn[j] = i; - pPk->azColl[j] = sqlite3StrBINARY; - j++; - } + nExtra = 0; + for(i=0; inCol; i++){ + if( !hasColumn(pPk->aiColumn, nPk, i) + && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ) nExtra++; + } + if( resizeIndexObject(pParse, pPk, nPk+nExtra) ) return; + for(i=0, j=nPk; inCol; i++){ + if( !hasColumn(pPk->aiColumn, j, i) + && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 + ){ + assert( jnColumn ); + pPk->aiColumn[j] = i; + pPk->azColl[j] = sqlite3StrBINARY; + j++; } - assert( pPk->nColumn==j ); - assert( pTab->nCol==j ); - }else{ - pPk->nColumn = pTab->nCol; + } + assert( pPk->nColumn==j ); + assert( pTab->nNVCol<=j ); + recomputeColumnsNotIndexed(pPk); +} + + +#ifndef SQLITE_OMIT_VIRTUALTABLE +/* +** Return true if pTab is a virtual table and zName is a shadow table name +** for that virtual table. +*/ +int sqlite3IsShadowTableOf(sqlite3 *db, Table *pTab, const char *zName){ + int nName; /* Length of zName */ + Module *pMod; /* Module for the virtual table */ + + if( !IsVirtual(pTab) ) return 0; + nName = sqlite3Strlen30(pTab->zName); + if( sqlite3_strnicmp(zName, pTab->zName, nName)!=0 ) return 0; + if( zName[nName]!='_' ) return 0; + pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->u.vtab.azArg[0]); + if( pMod==0 ) return 0; + if( pMod->pModule->iVersion<3 ) return 0; + if( pMod->pModule->xShadowName==0 ) return 0; + return pMod->pModule->xShadowName(zName+nName+1); +} +#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */ + +#ifndef SQLITE_OMIT_VIRTUALTABLE +/* +** Table pTab is a virtual table. If it the virtual table implementation +** exists and has an xShadowName method, then loop over all other ordinary +** tables within the same schema looking for shadow tables of pTab, and mark +** any shadow tables seen using the TF_Shadow flag. +*/ +void sqlite3MarkAllShadowTablesOf(sqlite3 *db, Table *pTab){ + int nName; /* Length of pTab->zName */ + Module *pMod; /* Module for the virtual table */ + HashElem *k; /* For looping through the symbol table */ + + assert( IsVirtual(pTab) ); + pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->u.vtab.azArg[0]); + if( pMod==0 ) return; + if( NEVER(pMod->pModule==0) ) return; + if( pMod->pModule->iVersion<3 ) return; + if( pMod->pModule->xShadowName==0 ) return; + assert( pTab->zName!=0 ); + nName = sqlite3Strlen30(pTab->zName); + for(k=sqliteHashFirst(&pTab->pSchema->tblHash); k; k=sqliteHashNext(k)){ + Table *pOther = sqliteHashData(k); + assert( pOther->zName!=0 ); + if( !IsOrdinaryTable(pOther) ) continue; + if( pOther->tabFlags & TF_Shadow ) continue; + if( sqlite3StrNICmp(pOther->zName, pTab->zName, nName)==0 + && pOther->zName[nName]=='_' + && pMod->pModule->xShadowName(pOther->zName+nName+1) + ){ + pOther->tabFlags |= TF_Shadow; + } + } +} +#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */ + +#ifndef SQLITE_OMIT_VIRTUALTABLE +/* +** Return true if zName is a shadow table name in the current database +** connection. +** +** zName is temporarily modified while this routine is running, but is +** restored to its original value prior to this routine returning. +*/ +int sqlite3ShadowTableName(sqlite3 *db, const char *zName){ + char *zTail; /* Pointer to the last "_" in zName */ + Table *pTab; /* Table that zName is a shadow of */ + zTail = strrchr(zName, '_'); + if( zTail==0 ) return 0; + *zTail = 0; + pTab = sqlite3FindTable(db, zName, 0); + *zTail = '_'; + if( pTab==0 ) return 0; + if( !IsVirtual(pTab) ) return 0; + return sqlite3IsShadowTableOf(db, pTab, zName); +} +#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */ + + +#ifdef SQLITE_DEBUG +/* +** Mark all nodes of an expression as EP_Immutable, indicating that +** they should not be changed. Expressions attached to a table or +** index definition are tagged this way to help ensure that we do +** not pass them into code generator routines by mistake. +*/ +static int markImmutableExprStep(Walker *pWalker, Expr *pExpr){ + (void)pWalker; + ExprSetVVAProperty(pExpr, EP_Immutable); + return WRC_Continue; +} +static void markExprListImmutable(ExprList *pList){ + if( pList ){ + Walker w; + memset(&w, 0, sizeof(w)); + w.xExprCallback = markImmutableExprStep; + w.xSelectCallback = sqlite3SelectWalkNoop; + w.xSelectCallback2 = 0; + sqlite3WalkExprList(&w, pList); } } +#else +#define markExprListImmutable(X) /* no-op */ +#endif /* SQLITE_DEBUG */ + /* ** This routine is called to report the final ")" that terminates @@ -1809,15 +2619,15 @@ static void convertToWithoutRowidTable(Parse *pParse, Table *pTab){ ** is added to the internal hash tables, assuming no errors have ** occurred. ** -** An entry for the table is made in the master table on disk, unless +** An entry for the table is made in the schema table on disk, unless ** this is a temporary table or db->init.busy==1. When db->init.busy==1 -** it means we are reading the sqlite_master table because we just -** connected to the database or because the sqlite_master table has +** it means we are reading the sqlite_schema table because we just +** connected to the database or because the sqlite_schema table has ** recently changed, so the entry for this table already exists in -** the sqlite_master table. We do not want to create it again. +** the sqlite_schema table. We do not want to create it again. ** ** If the pSelect argument is not NULL, it means that this routine -** was called to create a table generated from a +** was called to create a table generated from a ** "CREATE TABLE ... AS SELECT ..." statement. The column names of ** the new table will match the result set of the SELECT. */ @@ -1825,7 +2635,7 @@ void sqlite3EndTable( Parse *pParse, /* Parse context */ Token *pCons, /* The ',' token after the last column defn. */ Token *pEnd, /* The ')' before options in the CREATE TABLE */ - u8 tabOpts, /* Extra table options. Usually 0. */ + u32 tabOpts, /* Extra table options. Usually 0. */ Select *pSelect /* Select from a "CREATE ... AS SELECT" */ ){ Table *p; /* The new table */ @@ -1836,26 +2646,74 @@ void sqlite3EndTable( if( pEnd==0 && pSelect==0 ){ return; } - assert( !db->mallocFailed ); p = pParse->pNewTable; if( p==0 ) return; - assert( !db->init.busy || !pSelect ); + if( pSelect==0 && sqlite3ShadowTableName(db, p->zName) ){ + p->tabFlags |= TF_Shadow; + } /* If the db->init.busy is 1 it means we are reading the SQL off the - ** "sqlite_master" or "sqlite_temp_master" table on the disk. + ** "sqlite_schema" or "sqlite_temp_schema" table on the disk. ** So do not write to the disk again. Extract the root page number ** for the table from the db->init.newTnum field. (The page number ** should have been put there by the sqliteOpenCb routine.) ** - ** If the root page number is 1, that means this is the sqlite_master + ** If the root page number is 1, that means this is the sqlite_schema ** table itself. So mark it read-only. */ if( db->init.busy ){ + if( pSelect || (!IsOrdinaryTable(p) && db->init.newTnum) ){ + sqlite3ErrorMsg(pParse, ""); + return; + } p->tnum = db->init.newTnum; if( p->tnum==1 ) p->tabFlags |= TF_Readonly; } + /* Special processing for tables that include the STRICT keyword: + ** + ** * Do not allow custom column datatypes. Every column must have + ** a datatype that is one of INT, INTEGER, REAL, TEXT, or BLOB. + ** + ** * If a PRIMARY KEY is defined, other than the INTEGER PRIMARY KEY, + ** then all columns of the PRIMARY KEY must have a NOT NULL + ** constraint. + */ + if( tabOpts & TF_Strict ){ + int ii; + p->tabFlags |= TF_Strict; + for(ii=0; iinCol; ii++){ + Column *pCol = &p->aCol[ii]; + if( pCol->eCType==COLTYPE_CUSTOM ){ + if( pCol->colFlags & COLFLAG_HASTYPE ){ + sqlite3ErrorMsg(pParse, + "unknown datatype for %s.%s: \"%s\"", + p->zName, pCol->zCnName, sqlite3ColumnType(pCol, "") + ); + }else{ + sqlite3ErrorMsg(pParse, "missing datatype for %s.%s", + p->zName, pCol->zCnName); + } + return; + }else if( pCol->eCType==COLTYPE_ANY ){ + pCol->affinity = SQLITE_AFF_BLOB; + } + if( (pCol->colFlags & COLFLAG_PRIMKEY)!=0 + && p->iPKey!=ii + && pCol->notNull == OE_None + ){ + pCol->notNull = OE_Abort; + p->tabFlags |= TF_HasNotNull; + } + } + } + + assert( (p->tabFlags & TF_HasPrimaryKey)==0 + || p->iPKey>=0 || sqlite3PrimaryKeyIndex(p)!=0 ); + assert( (p->tabFlags & TF_HasPrimaryKey)!=0 + || (p->iPKey<0 && sqlite3PrimaryKeyIndex(p)==0) ); + /* Special processing for WITHOUT ROWID Tables */ if( tabOpts & TF_WithoutRowid ){ if( (p->tabFlags & TF_Autoincrement) ){ @@ -1865,12 +2723,11 @@ void sqlite3EndTable( } if( (p->tabFlags & TF_HasPrimaryKey)==0 ){ sqlite3ErrorMsg(pParse, "PRIMARY KEY missing on table %s", p->zName); - }else{ - p->tabFlags |= TF_WithoutRowid | TF_NoVisibleRowid; - convertToWithoutRowidTable(pParse, p); + return; } + p->tabFlags |= TF_WithoutRowid | TF_NoVisibleRowid; + convertToWithoutRowidTable(pParse, p); } - iDb = sqlite3SchemaToIndex(db, p->pSchema); #ifndef SQLITE_OMIT_CHECK @@ -1878,8 +2735,47 @@ void sqlite3EndTable( */ if( p->pCheck ){ sqlite3ResolveSelfReference(pParse, p, NC_IsCheck, 0, p->pCheck); + if( pParse->nErr ){ + /* If errors are seen, delete the CHECK constraints now, else they might + ** actually be used if PRAGMA writable_schema=ON is set. */ + sqlite3ExprListDelete(db, p->pCheck); + p->pCheck = 0; + }else{ + markExprListImmutable(p->pCheck); + } } #endif /* !defined(SQLITE_OMIT_CHECK) */ +#ifndef SQLITE_OMIT_GENERATED_COLUMNS + if( p->tabFlags & TF_HasGenerated ){ + int ii, nNG = 0; + testcase( p->tabFlags & TF_HasVirtual ); + testcase( p->tabFlags & TF_HasStored ); + for(ii=0; iinCol; ii++){ + u32 colFlags = p->aCol[ii].colFlags; + if( (colFlags & COLFLAG_GENERATED)!=0 ){ + Expr *pX = sqlite3ColumnExpr(p, &p->aCol[ii]); + testcase( colFlags & COLFLAG_VIRTUAL ); + testcase( colFlags & COLFLAG_STORED ); + if( sqlite3ResolveSelfReference(pParse, p, NC_GenCol, pX, 0) ){ + /* If there are errors in resolving the expression, change the + ** expression to a NULL. This prevents code generators that operate + ** on the expression from inserting extra parts into the expression + ** tree that have been allocated from lookaside memory, which is + ** illegal in a schema and will lead to errors or heap corruption + ** when the database connection closes. */ + sqlite3ColumnSetExpr(pParse, p, &p->aCol[ii], + sqlite3ExprAlloc(db, TK_NULL, 0, 0)); + } + }else{ + nNG++; + } + } + if( nNG==0 ){ + sqlite3ErrorMsg(pParse, "must have at least one non-generated column"); + return; + } + } +#endif /* Estimate the average row size for the table and for all implied indices */ estimateTableWidth(p); @@ -1888,7 +2784,7 @@ void sqlite3EndTable( } /* If not initializing, then create a record for the new table - ** in the SQLITE_MASTER table of the database. + ** in the schema table of the database. ** ** If this is a TEMPORARY table, write the entry into the auxiliary ** file instead of into the main database file. @@ -1905,10 +2801,10 @@ void sqlite3EndTable( sqlite3VdbeAddOp1(v, OP_Close, 0); - /* + /* ** Initialize zType for the new view or table. */ - if( p->pSelect==0 ){ + if( IsOrdinaryTable(p) ){ /* A regular table */ zType = "table"; zType2 = "TABLE"; @@ -1922,7 +2818,7 @@ void sqlite3EndTable( /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT ** statement to populate the new table. The root-page number for the - ** new table is in register pParse->regRoot. + ** new table is in register pParse->u1.cr.regRoot. ** ** Once the SELECT has been coded by sqlite3Select(), it is in a ** suitable state to query for the column names and types to be used @@ -1941,39 +2837,46 @@ void sqlite3EndTable( int regRowid; /* Rowid of the next row to insert */ int addrInsLoop; /* Top of the loop for inserting rows */ Table *pSelTab; /* A table that describes the SELECT results */ + int iCsr; /* Write cursor on the new table */ + if( IN_SPECIAL_PARSE ){ + pParse->rc = SQLITE_ERROR; + pParse->nErr++; + return; + } + iCsr = pParse->nTab++; regYield = ++pParse->nMem; regRec = ++pParse->nMem; regRowid = ++pParse->nMem; - assert(pParse->nTab==1); sqlite3MayAbort(pParse); - sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb); + assert( pParse->isCreate ); + sqlite3VdbeAddOp3(v, OP_OpenWrite, iCsr, pParse->u1.cr.regRoot, iDb); sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG); - pParse->nTab = 2; addrTop = sqlite3VdbeCurrentAddr(v) + 1; sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop); - sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield); - sqlite3Select(pParse, pSelect, &dest); - sqlite3VdbeEndCoroutine(v, regYield); - sqlite3VdbeJumpHere(v, addrTop - 1); if( pParse->nErr ) return; - pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect); + pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect, SQLITE_AFF_BLOB); if( pSelTab==0 ) return; assert( p->aCol==0 ); - p->nCol = pSelTab->nCol; + p->nCol = p->nNVCol = pSelTab->nCol; p->aCol = pSelTab->aCol; pSelTab->nCol = 0; pSelTab->aCol = 0; sqlite3DeleteTable(db, pSelTab); + sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield); + sqlite3Select(pParse, pSelect, &dest); + if( pParse->nErr ) return; + sqlite3VdbeEndCoroutine(v, regYield); + sqlite3VdbeJumpHere(v, addrTop - 1); addrInsLoop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm); VdbeCoverage(v); sqlite3VdbeAddOp3(v, OP_MakeRecord, dest.iSdst, dest.nSdst, regRec); sqlite3TableAffinity(v, p, 0); - sqlite3VdbeAddOp2(v, OP_NewRowid, 1, regRowid); - sqlite3VdbeAddOp3(v, OP_Insert, 1, regRec, regRowid); + sqlite3VdbeAddOp2(v, OP_NewRowid, iCsr, regRowid); + sqlite3VdbeAddOp3(v, OP_Insert, iCsr, regRec, regRowid); sqlite3VdbeGoto(v, addrInsLoop); sqlite3VdbeJumpHere(v, addrInsLoop); - sqlite3VdbeAddOp1(v, OP_Close, 1); + sqlite3VdbeAddOp1(v, OP_Close, iCsr); } /* Compute the complete text of the CREATE statement */ @@ -1983,26 +2886,27 @@ void sqlite3EndTable( Token *pEnd2 = tabOpts ? &pParse->sLastToken : pEnd; n = (int)(pEnd2->z - pParse->sNameToken.z); if( pEnd2->z[0]!=';' ) n += pEnd2->n; - zStmt = sqlite3MPrintf(db, + zStmt = sqlite3MPrintf(db, "CREATE %s %.*s", zType2, n, pParse->sNameToken.z ); } - /* A slot for the record has already been allocated in the - ** SQLITE_MASTER table. We just need to update that slot with all + /* A slot for the record has already been allocated in the + ** schema table. We just need to update that slot with all ** the information we've collected. */ + assert( pParse->isCreate ); sqlite3NestedParse(pParse, - "UPDATE %Q.%s " - "SET type='%s', name=%Q, tbl_name=%Q, rootpage=#%d, sql=%Q " - "WHERE rowid=#%d", - db->aDb[iDb].zName, SCHEMA_TABLE(iDb), + "UPDATE %Q." LEGACY_SCHEMA_TABLE + " SET type='%s', name=%Q, tbl_name=%Q, rootpage=#%d, sql=%Q" + " WHERE rowid=#%d", + db->aDb[iDb].zDbSName, zType, p->zName, p->zName, - pParse->regRoot, + pParse->u1.cr.regRoot, zStmt, - pParse->regRowid + pParse->u1.cr.regRowid ); sqlite3DbFree(db, zStmt); sqlite3ChangeCookie(pParse, iDb); @@ -2011,13 +2915,13 @@ void sqlite3EndTable( /* Check to see if we need to create an sqlite_sequence table for ** keeping track of autoincrement keys. */ - if( p->tabFlags & TF_Autoincrement ){ + if( (p->tabFlags & TF_Autoincrement)!=0 && !IN_SPECIAL_PARSE ){ Db *pDb = &db->aDb[iDb]; assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); if( pDb->pSchema->pSeqTab==0 ){ sqlite3NestedParse(pParse, "CREATE TABLE %Q.sqlite_sequence(name,seq)", - pDb->zName + pDb->zDbSName ); } } @@ -2025,16 +2929,24 @@ void sqlite3EndTable( /* Reparse everything to update our internal data structures */ sqlite3VdbeAddParseSchemaOp(v, iDb, - sqlite3MPrintf(db, "tbl_name='%q' AND type!='trigger'", p->zName)); + sqlite3MPrintf(db, "tbl_name='%q' AND type!='trigger'", p->zName),0); + + /* Test for cycles in generated columns and illegal expressions + ** in CHECK constraints and in DEFAULT clauses. */ + if( p->tabFlags & TF_HasGenerated ){ + sqlite3VdbeAddOp4(v, OP_SqlExec, 0x0001, 0, 0, + sqlite3MPrintf(db, "SELECT*FROM\"%w\".\"%w\"", + db->aDb[iDb].zDbSName, p->zName), P4_DYNAMIC); + } } - /* Add the table to the in-memory representation of the database. */ if( db->init.busy ){ Table *pOld; Schema *pSchema = p->pSchema; assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); + assert( HasRowid(p) || p->iPKey<0 ); pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName, p); if( pOld ){ assert( p==pOld ); /* Malloc must have failed inside HashInsert() */ @@ -2042,21 +2954,29 @@ void sqlite3EndTable( return; } pParse->pNewTable = 0; - db->flags |= SQLITE_InternChanges; + db->mDbFlags |= DBFLAG_SchemaChange; -#ifndef SQLITE_OMIT_ALTERTABLE - if( !p->pSelect ){ - const char *zName = (const char *)pParse->sNameToken.z; - int nName; - assert( !pSelect && pCons && pEnd ); - if( pCons->z==0 ){ - pCons = pEnd; - } - nName = (int)((const char *)pCons->z - zName); - p->addColOffset = 13 + sqlite3Utf8CharLen(zName, nName); + /* If this is the magic sqlite_sequence table used by autoincrement, + ** then record a pointer to this table in the main database structure + ** so that INSERT can find the table easily. */ + assert( !pParse->nested ); +#ifndef SQLITE_OMIT_AUTOINCREMENT + if( strcmp(p->zName, "sqlite_sequence")==0 ){ + assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); + p->pSchema->pSeqTab = p; } #endif } + +#ifndef SQLITE_OMIT_ALTERTABLE + if( !pSelect && IsOrdinaryTable(p) ){ + assert( pCons && pEnd ); + if( pCons->z==0 ){ + pCons = pEnd; + } + p->u.tab.addColOffset = 13 + (int)(pCons->z - pParse->sNameToken.z); + } +#endif } #ifndef SQLITE_OMIT_VIEW @@ -2089,6 +3009,19 @@ void sqlite3CreateView( sqlite3StartTable(pParse, pName1, pName2, isTemp, 1, 0, noErr); p = pParse->pNewTable; if( p==0 || pParse->nErr ) goto create_view_fail; + + /* Legacy versions of SQLite allowed the use of the magic "rowid" column + ** on a view, even though views do not have rowids. The following flag + ** setting fixes this problem. But the fix can be disabled by compiling + ** with -DSQLITE_ALLOW_ROWID_IN_VIEW in case there are legacy apps that + ** depend upon the old buggy behavior. The ability can also be toggled + ** using sqlite3_config(SQLITE_CONFIG_ROWID_IN_VIEW,...) */ +#ifdef SQLITE_ALLOW_ROWID_IN_VIEW + p->tabFlags |= sqlite3Config.mNoVisibleRowid; /* Optional. Allow by default */ +#else + p->tabFlags |= TF_NoVisibleRowid; /* Never allow rowid in view */ +#endif + sqlite3TwoPartName(pParse, pName1, pName2, &pName); iDb = sqlite3SchemaToIndex(db, p->pSchema); sqlite3FixInit(&sFix, pParse, iDb, "view", pName); @@ -2099,15 +3032,22 @@ void sqlite3CreateView( ** allocated rather than point to the input string - which means that ** they will persist after the current sqlite3_exec() call returns. */ - p->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE); + pSelect->selFlags |= SF_View; + if( IN_RENAME_OBJECT ){ + p->u.view.pSelect = pSelect; + pSelect = 0; + }else{ + p->u.view.pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE); + } p->pCheck = sqlite3ExprListDup(db, pCNames, EXPRDUP_REDUCE); + p->eTabType = TABTYP_VIEW; if( db->mallocFailed ) goto create_view_fail; /* Locate the end of the CREATE VIEW statement. Make sEnd point to ** the end. */ sEnd = pParse->sLastToken; - assert( sEnd.z[0]!=0 ); + assert( sEnd.z[0]!=0 || sEnd.n==0 ); if( sEnd.z[0]!=';' ){ sEnd.z += sEnd.n; } @@ -2119,11 +3059,14 @@ void sqlite3CreateView( sEnd.z = &z[n-1]; sEnd.n = 1; - /* Use sqlite3EndTable() to add the view to the SQLITE_MASTER table */ + /* Use sqlite3EndTable() to add the view to the schema table */ sqlite3EndTable(pParse, 0, &sEnd, 0, 0); create_view_fail: sqlite3SelectDelete(db, pSelect); + if( IN_RENAME_OBJECT ){ + sqlite3RenameExprlistUnmap(pParse, pCNames); + } sqlite3ExprListDelete(db, pCNames); return; } @@ -2132,31 +3075,39 @@ void sqlite3CreateView( #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) /* ** The Table structure pTable is really a VIEW. Fill in the names of -** the columns of the view in the pTable structure. Return the number -** of errors. If an error is seen leave an error message in pParse->zErrMsg. +** the columns of the view in the pTable structure. Return non-zero if +** there are errors. If an error is seen an error message is left +** in pParse->zErrMsg. */ -int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){ +static SQLITE_NOINLINE int viewGetColumnNames(Parse *pParse, Table *pTable){ Table *pSelTab; /* A fake table from which we get the result set */ Select *pSel; /* Copy of the SELECT that implements the view */ int nErr = 0; /* Number of errors encountered */ - int n; /* Temporarily holds the number of cursors assigned */ sqlite3 *db = pParse->db; /* Database connection for malloc errors */ +#ifndef SQLITE_OMIT_VIRTUALTABLE + int rc; +#endif +#ifndef SQLITE_OMIT_AUTHORIZATION sqlite3_xauth xAuth; /* Saved xAuth pointer */ +#endif assert( pTable ); #ifndef SQLITE_OMIT_VIRTUALTABLE - if( sqlite3VtabCallConnect(pParse, pTable) ){ - return SQLITE_ERROR; + if( IsVirtual(pTable) ){ + db->nSchemaLock++; + rc = sqlite3VtabCallConnect(pParse, pTable); + db->nSchemaLock--; + return rc; } - if( IsVirtual(pTable) ) return 0; #endif #ifndef SQLITE_OMIT_VIEW /* A positive nCol means the columns names for this view are - ** already known. + ** already known. This routine is not called unless either the + ** table is virtual or nCol is zero. */ - if( pTable->nCol>0 ) return 0; + assert( pTable->nCol<=0 ); /* A negative nCol is a special marker meaning that we are currently ** trying to compute the column names. If we enter this routine with @@ -2168,7 +3119,7 @@ int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){ ** Actually, the error above is now caught prior to reaching this point. ** But the following test is still important as it does come up ** in the following: - ** + ** ** CREATE TABLE main.ex1(a); ** CREATE TEMP VIEW ex1 AS SELECT a FROM ex1; ** SELECT * FROM temp.ex1; @@ -2186,49 +3137,75 @@ int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){ ** to be permanent. So the computation is done on a copy of the SELECT ** statement that defines the view. */ - assert( pTable->pSelect ); - if( pTable->pCheck ){ - db->lookaside.bDisable++; - sqlite3ColumnsFromExprList(pParse, pTable->pCheck, - &pTable->nCol, &pTable->aCol); - db->lookaside.bDisable--; - }else{ - pSel = sqlite3SelectDup(db, pTable->pSelect, 0); - if( pSel ){ - n = pParse->nTab; - sqlite3SrcListAssignCursors(pParse, pSel->pSrc); - pTable->nCol = -1; - db->lookaside.bDisable++; + assert( IsView(pTable) ); + pSel = sqlite3SelectDup(db, pTable->u.view.pSelect, 0); + if( pSel ){ + u8 eParseMode = pParse->eParseMode; + int nTab = pParse->nTab; + int nSelect = pParse->nSelect; + pParse->eParseMode = PARSE_MODE_NORMAL; + sqlite3SrcListAssignCursors(pParse, pSel->pSrc); + pTable->nCol = -1; + DisableLookaside; #ifndef SQLITE_OMIT_AUTHORIZATION - xAuth = db->xAuth; - db->xAuth = 0; - pSelTab = sqlite3ResultSetOfSelect(pParse, pSel); - db->xAuth = xAuth; + xAuth = db->xAuth; + db->xAuth = 0; + pSelTab = sqlite3ResultSetOfSelect(pParse, pSel, SQLITE_AFF_NONE); + db->xAuth = xAuth; #else - pSelTab = sqlite3ResultSetOfSelect(pParse, pSel); + pSelTab = sqlite3ResultSetOfSelect(pParse, pSel, SQLITE_AFF_NONE); #endif - db->lookaside.bDisable--; - pParse->nTab = n; - if( pSelTab ){ - assert( pTable->aCol==0 ); - pTable->nCol = pSelTab->nCol; - pTable->aCol = pSelTab->aCol; - pSelTab->nCol = 0; - pSelTab->aCol = 0; - sqlite3DeleteTable(db, pSelTab); - assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) ); - }else{ - pTable->nCol = 0; - nErr++; - } - sqlite3SelectDelete(db, pSel); - } else { + pParse->nTab = nTab; + pParse->nSelect = nSelect; + if( pSelTab==0 ){ + pTable->nCol = 0; nErr++; + }else if( pTable->pCheck ){ + /* CREATE VIEW name(arglist) AS ... + ** The names of the columns in the table are taken from + ** arglist which is stored in pTable->pCheck. The pCheck field + ** normally holds CHECK constraints on an ordinary table, but for + ** a VIEW it holds the list of column names. + */ + sqlite3ColumnsFromExprList(pParse, pTable->pCheck, + &pTable->nCol, &pTable->aCol); + if( pParse->nErr==0 + && pTable->nCol==pSel->pEList->nExpr + ){ + assert( db->mallocFailed==0 ); + sqlite3SubqueryColumnTypes(pParse, pTable, pSel, SQLITE_AFF_NONE); + } + }else{ + /* CREATE VIEW name AS... without an argument list. Construct + ** the column names from the SELECT statement that defines the view. + */ + assert( pTable->aCol==0 ); + pTable->nCol = pSelTab->nCol; + pTable->aCol = pSelTab->aCol; + pTable->tabFlags |= (pSelTab->tabFlags & COLFLAG_NOINSERT); + pSelTab->nCol = 0; + pSelTab->aCol = 0; + assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) ); } + pTable->nNVCol = pTable->nCol; + sqlite3DeleteTable(db, pSelTab); + sqlite3SelectDelete(db, pSel); + EnableLookaside; + pParse->eParseMode = eParseMode; + } else { + nErr++; } pTable->pSchema->schemaFlags |= DB_UnresetViews; + if( db->mallocFailed ){ + sqlite3DeleteColumnNames(db, pTable); + } #endif /* SQLITE_OMIT_VIEW */ - return nErr; + return nErr + pParse->nErr; +} +int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){ + assert( pTable!=0 ); + if( !IsVirtual(pTable) && pTable->nCol>0 ) return 0; + return viewGetColumnNames(pParse, pTable); } #endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */ @@ -2242,10 +3219,8 @@ static void sqliteViewResetAll(sqlite3 *db, int idx){ if( !DbHasProperty(db, idx, DB_UnresetViews) ) return; for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){ Table *pTab = sqliteHashData(i); - if( pTab->pSelect ){ + if( IsView(pTab) ){ sqlite3DeleteColumnNames(db, pTab); - pTab->aCol = 0; - pTab->nCol = 0; } } DbClearProperty(db, idx, DB_UnresetViews); @@ -2264,7 +3239,7 @@ static void sqliteViewResetAll(sqlite3 *db, int idx){ ** on tables and/or indices that are the process of being deleted. ** If you are unlucky, one of those deleted indices or tables might ** have the same rootpage number as the real table or index that is -** being moved. So we cannot stop searching after the first match +** being moved. So we cannot stop searching after the first match ** because the first match might be for one of the deleted indices ** or tables and not the table/index that is actually being moved. ** We must continue looping until all tables and indices with @@ -2272,7 +3247,7 @@ static void sqliteViewResetAll(sqlite3 *db, int idx){ ** in order to be certain that we got the right one. */ #ifndef SQLITE_OMIT_AUTOVACUUM -void sqlite3RootPageMoved(sqlite3 *db, int iDb, int iFrom, int iTo){ +void sqlite3RootPageMoved(sqlite3 *db, int iDb, Pgno iFrom, Pgno iTo){ HashElem *pElem; Hash *pHash; Db *pDb; @@ -2298,51 +3273,44 @@ void sqlite3RootPageMoved(sqlite3 *db, int iDb, int iFrom, int iTo){ /* ** Write code to erase the table with root-page iTable from database iDb. -** Also write code to modify the sqlite_master table and internal schema +** Also write code to modify the sqlite_schema table and internal schema ** if a root-page of another table is moved by the btree-layer whilst ** erasing iTable (this can happen with an auto-vacuum database). -*/ +*/ static void destroyRootPage(Parse *pParse, int iTable, int iDb){ Vdbe *v = sqlite3GetVdbe(pParse); int r1 = sqlite3GetTempReg(pParse); - assert( iTable>1 ); + if( iTable<2 ) sqlite3ErrorMsg(pParse, "corrupt schema"); sqlite3VdbeAddOp3(v, OP_Destroy, iTable, r1, iDb); sqlite3MayAbort(pParse); #ifndef SQLITE_OMIT_AUTOVACUUM /* OP_Destroy stores an in integer r1. If this integer ** is non-zero, then it is the root page number of a table moved to - ** location iTable. The following code modifies the sqlite_master table to + ** location iTable. The following code modifies the sqlite_schema table to ** reflect this. ** ** The "#NNN" in the SQL is a special constant that means whatever value ** is in register NNN. See grammar rules associated with the TK_REGISTER ** token for additional information. */ - sqlite3NestedParse(pParse, - "UPDATE %Q.%s SET rootpage=%d WHERE #%d AND rootpage=#%d", - pParse->db->aDb[iDb].zName, SCHEMA_TABLE(iDb), iTable, r1, r1); + sqlite3NestedParse(pParse, + "UPDATE %Q." LEGACY_SCHEMA_TABLE + " SET rootpage=%d WHERE #%d AND rootpage=#%d", + pParse->db->aDb[iDb].zDbSName, iTable, r1, r1); #endif sqlite3ReleaseTempReg(pParse, r1); } /* ** Write VDBE code to erase table pTab and all associated indices on disk. -** Code to update the sqlite_master tables and internal schema definitions +** Code to update the sqlite_schema tables and internal schema definitions ** in case a root-page belonging to another table is moved by the btree layer ** is also added (this can happen with an auto-vacuum database). */ static void destroyTable(Parse *pParse, Table *pTab){ -#ifdef SQLITE_OMIT_AUTOVACUUM - Index *pIdx; - int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); - destroyRootPage(pParse, pTab->tnum, iDb); - for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ - destroyRootPage(pParse, pIdx->tnum, iDb); - } -#else /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM ** is not defined), then it is important to call OP_Destroy on the - ** table and index root-pages in order, starting with the numerically + ** table and index root-pages in order, starting with the numerically ** largest root-page number. This guarantees that none of the root-pages ** to be destroyed is relocated by an earlier OP_Destroy. i.e. if the ** following were coded: @@ -2352,22 +3320,22 @@ static void destroyTable(Parse *pParse, Table *pTab){ ** OP_Destroy 5 0 ** ** and root page 5 happened to be the largest root-page number in the - ** database, then root page 5 would be moved to page 4 by the + ** database, then root page 5 would be moved to page 4 by the ** "OP_Destroy 4 0" opcode. The subsequent "OP_Destroy 5 0" would hit ** a free-list page. */ - int iTab = pTab->tnum; - int iDestroyed = 0; + Pgno iTab = pTab->tnum; + Pgno iDestroyed = 0; while( 1 ){ Index *pIdx; - int iLargest = 0; + Pgno iLargest = 0; if( iDestroyed==0 || iTabpIndex; pIdx; pIdx=pIdx->pNext){ - int iIdx = pIdx->tnum; + Pgno iIdx = pIdx->tnum; assert( pIdx->pSchema==pTab->pSchema ); if( (iDestroyed==0 || (iIdxiLargest ){ iLargest = iIdx; @@ -2382,7 +3350,6 @@ static void destroyTable(Parse *pParse, Table *pTab){ iDestroyed = iLargest; } } -#endif } /* @@ -2396,7 +3363,7 @@ static void sqlite3ClearStatTables( const char *zName /* Name of index or table */ ){ int i; - const char *zDbName = pParse->db->aDb[iDb].zName; + const char *zDbName = pParse->db->aDb[iDb].zDbSName; for(i=1; i<=4; i++){ char zTab[24]; sqlite3_snprintf(sizeof(zTab),zTab,"sqlite_stat%d",i); @@ -2429,12 +3396,12 @@ void sqlite3CodeDropTable(Parse *pParse, Table *pTab, int iDb, int isView){ #endif /* Drop all triggers associated with the table being dropped. Code - ** is generated to remove entries from sqlite_master and/or - ** sqlite_temp_master if required. + ** is generated to remove entries from sqlite_schema and/or + ** sqlite_temp_schema if required. */ pTrigger = sqlite3TriggerList(pParse, pTab); while( pTrigger ){ - assert( pTrigger->pSchema==pTab->pSchema || + assert( pTrigger->pSchema==pTab->pSchema || pTrigger->pSchema==db->aDb[1].pSchema ); sqlite3DropTriggerPtr(pParse, pTrigger); pTrigger = pTrigger->pNext; @@ -2449,21 +3416,22 @@ void sqlite3CodeDropTable(Parse *pParse, Table *pTab, int iDb, int isView){ if( pTab->tabFlags & TF_Autoincrement ){ sqlite3NestedParse(pParse, "DELETE FROM %Q.sqlite_sequence WHERE name=%Q", - pDb->zName, pTab->zName + pDb->zDbSName, pTab->zName ); } #endif - /* Drop all SQLITE_MASTER table and index entries that refer to the - ** table. The program name loops through the master table and deletes + /* Drop all entries in the schema table that refer to the + ** table. The program name loops through the schema table and deletes ** every row that refers to a table of the same name as the one being ** dropped. Triggers are handled separately because a trigger can be ** created in the temp database that refers to a table in another ** database. */ - sqlite3NestedParse(pParse, - "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'", - pDb->zName, SCHEMA_TABLE(iDb), pTab->zName); + sqlite3NestedParse(pParse, + "DELETE FROM %Q." LEGACY_SCHEMA_TABLE + " WHERE tbl_name=%Q and type!='trigger'", + pDb->zDbSName, pTab->zName); if( !isView && !IsVirtual(pTab) ){ destroyTable(pParse, pTab); } @@ -2473,10 +3441,46 @@ void sqlite3CodeDropTable(Parse *pParse, Table *pTab, int iDb, int isView){ */ if( IsVirtual(pTab) ){ sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0); + sqlite3MayAbort(pParse); + } + sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0); + sqlite3ChangeCookie(pParse, iDb); + sqliteViewResetAll(db, iDb); +} + +/* +** Return TRUE if shadow tables should be read-only in the current +** context. +*/ +int sqlite3ReadOnlyShadowTables(sqlite3 *db){ +#ifndef SQLITE_OMIT_VIRTUALTABLE + if( (db->flags & SQLITE_Defensive)!=0 + && db->pVtabCtx==0 + && db->nVdbeExec==0 + && !sqlite3VtabInSync(db) + ){ + return 1; + } +#endif + return 0; +} + +/* +** Return true if it is not allowed to drop the given table +*/ +static int tableMayNotBeDropped(sqlite3 *db, Table *pTab){ + if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){ + if( sqlite3StrNICmp(pTab->zName+7, "stat", 4)==0 ) return 0; + if( sqlite3StrNICmp(pTab->zName+7, "parameters", 10)==0 ) return 0; + return 1; + } + if( (pTab->tabFlags & TF_Shadow)!=0 && sqlite3ReadOnlyShadowTables(db) ){ + return 1; } - sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0); - sqlite3ChangeCookie(pParse, iDb); - sqliteViewResetAll(db, iDb); + if( pTab->tabFlags & TF_Eponymous ){ + return 1; + } + return 0; } /* @@ -2494,13 +3498,19 @@ void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, int noErr){ } assert( pParse->nErr==0 ); assert( pName->nSrc==1 ); + assert( pName->a[0].fg.fixedSchema==0 ); + assert( pName->a[0].fg.isSubquery==0 ); if( sqlite3ReadSchema(pParse) ) goto exit_drop_table; if( noErr ) db->suppressErr++; + assert( isView==0 || isView==LOCATE_VIEW ); pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]); if( noErr ) db->suppressErr--; if( pTab==0 ){ - if( noErr ) sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase); + if( noErr ){ + sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].u4.zDatabase); + sqlite3ForceNotReadOnly(pParse); + } goto exit_drop_table; } iDb = sqlite3SchemaToIndex(db, pTab->pSchema); @@ -2516,7 +3526,7 @@ void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, int noErr){ { int code; const char *zTab = SCHEMA_TABLE(iDb); - const char *zDb = db->aDb[iDb].zName; + const char *zDb = db->aDb[iDb].zDbSName; const char *zArg2 = 0; if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){ goto exit_drop_table; @@ -2547,8 +3557,7 @@ void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, int noErr){ } } #endif - if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 - && sqlite3StrNICmp(pTab->zName, "sqlite_stat", 11)!=0 ){ + if( tableMayNotBeDropped(db, pTab) ){ sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName); goto exit_drop_table; } @@ -2557,24 +3566,26 @@ void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, int noErr){ /* Ensure DROP TABLE is not used on a view, and DROP VIEW is not used ** on a table. */ - if( isView && pTab->pSelect==0 ){ + if( isView && !IsView(pTab) ){ sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName); goto exit_drop_table; } - if( !isView && pTab->pSelect ){ + if( !isView && IsView(pTab) ){ sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName); goto exit_drop_table; } #endif - /* Generate code to remove the table from the master table + /* Generate code to remove the table from the schema table ** on disk. */ v = sqlite3GetVdbe(pParse); if( v ){ sqlite3BeginWriteOperation(pParse, 1, iDb); - sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName); - sqlite3FkDropTable(pParse, pName, pTab); + if( !isView ){ + sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName); + sqlite3FkDropTable(pParse, pName, pTab); + } sqlite3CodeDropTable(pParse, pTab, iDb, isView); } @@ -2610,7 +3621,7 @@ void sqlite3CreateForeignKey( FKey *pFKey = 0; FKey *pNextTo; Table *p = pParse->pNewTable; - int nByte; + i64 nByte; int i; int nCol; char *z; @@ -2623,7 +3634,7 @@ void sqlite3CreateForeignKey( if( pToCol && pToCol->nExpr!=1 ){ sqlite3ErrorMsg(pParse, "foreign key on %s" " should reference only one column of table %T", - p->aCol[iCol].zName, pTo); + p->aCol[iCol].zCnName, pTo); goto fk_end; } nCol = 1; @@ -2635,10 +3646,10 @@ void sqlite3CreateForeignKey( }else{ nCol = pFromCol->nExpr; } - nByte = sizeof(*pFKey) + (nCol-1)*sizeof(pFKey->aCol[0]) + pTo->n + 1; + nByte = SZ_FKEY(nCol) + pTo->n + 1; if( pToCol ){ for(i=0; inExpr; i++){ - nByte += sqlite3Strlen30(pToCol->a[i].zName) + 1; + nByte += sqlite3Strlen30(pToCol->a[i].zEName) + 1; } } pFKey = sqlite3DbMallocZero(db, nByte ); @@ -2646,9 +3657,13 @@ void sqlite3CreateForeignKey( goto fk_end; } pFKey->pFrom = p; - pFKey->pNextFrom = p->pFKey; + assert( IsOrdinaryTable(p) ); + pFKey->pNextFrom = p->u.tab.pFKey; z = (char*)&pFKey->aCol[nCol]; pFKey->zTo = z; + if( IN_RENAME_OBJECT ){ + sqlite3RenameTokenMap(pParse, (void*)z, pTo); + } memcpy(z, pTo->z, pTo->n); z[pTo->n] = 0; sqlite3Dequote(z); @@ -2660,24 +3675,30 @@ void sqlite3CreateForeignKey( for(i=0; inCol; j++){ - if( sqlite3StrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){ + if( sqlite3StrICmp(p->aCol[j].zCnName, pFromCol->a[i].zEName)==0 ){ pFKey->aCol[i].iFrom = j; break; } } if( j>=p->nCol ){ - sqlite3ErrorMsg(pParse, - "unknown column \"%s\" in foreign key definition", - pFromCol->a[i].zName); + sqlite3ErrorMsg(pParse, + "unknown column \"%s\" in foreign key definition", + pFromCol->a[i].zEName); goto fk_end; } + if( IN_RENAME_OBJECT ){ + sqlite3RenameTokenRemap(pParse, &pFKey->aCol[i], pFromCol->a[i].zEName); + } } } if( pToCol ){ for(i=0; ia[i].zName); + int n = sqlite3Strlen30(pToCol->a[i].zEName); pFKey->aCol[i].zCol = z; - memcpy(z, pToCol->a[i].zName, n); + if( IN_RENAME_OBJECT ){ + sqlite3RenameTokenRemap(pParse, z, pToCol->a[i].zEName); + } + memcpy(z, pToCol->a[i].zEName, n); z[n] = 0; z += n+1; } @@ -2687,7 +3708,7 @@ void sqlite3CreateForeignKey( pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff); /* ON UPDATE action */ assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) ); - pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash, + pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash, pFKey->zTo, (void *)pFKey ); if( pNextTo==pFKey ){ @@ -2702,7 +3723,8 @@ void sqlite3CreateForeignKey( /* Link the foreign key to the table as the last step. */ - p->pFKey = pFKey; + assert( IsOrdinaryTable(p) ); + p->u.tab.pFKey = pFKey; pFKey = 0; fk_end: @@ -2723,7 +3745,9 @@ void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){ #ifndef SQLITE_OMIT_FOREIGN_KEY Table *pTab; FKey *pFKey; - if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return; + if( (pTab = pParse->pNewTable)==0 ) return; + if( NEVER(!IsOrdinaryTable(pTab)) ) return; + if( (pFKey = pTab->u.tab.pFKey)==0 ) return; assert( isDeferred==0 || isDeferred==1 ); /* EV: R-30323-21917 */ pFKey->isDeferred = (u8)isDeferred; #endif @@ -2747,7 +3771,7 @@ static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){ int iSorter; /* Cursor opened by OpenSorter (if in use) */ int addr1; /* Address of top of loop */ int addr2; /* Address to jump to for next iteration */ - int tnum; /* Root page of index */ + Pgno tnum; /* Root page of index */ int iPartIdxLabel; /* Jump to this label to skip a row */ Vdbe *v; /* Generate code into this virtual machine */ KeyInfo *pKey; /* KeyInfo for index */ @@ -2757,7 +3781,7 @@ static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){ #ifndef SQLITE_OMIT_AUTHORIZATION if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0, - db->aDb[iDb].zName ) ){ + db->aDb[iDb].zDbSName ) ){ return; } #endif @@ -2768,11 +3792,12 @@ static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){ v = sqlite3GetVdbe(pParse); if( v==0 ) return; if( memRootPage>=0 ){ - tnum = memRootPage; + tnum = (Pgno)memRootPage; }else{ tnum = pIndex->tnum; } pKey = sqlite3KeyInfoOfIndex(pParse, pIndex); + assert( pKey!=0 || pParse->nErr ); /* Open the sorter cursor if we are to use one. */ iSorter = pParse->nTab++; @@ -2784,6 +3809,7 @@ static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){ sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead); addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0); VdbeCoverage(v); regRecord = sqlite3GetTempReg(pParse); + sqlite3MultiWrite(pParse); sqlite3GenerateIndexKey(pParse,pIndex,iTab,regRecord,0,&iPartIdxLabel,0,0); sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord); @@ -2791,25 +3817,42 @@ static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){ sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1); VdbeCoverage(v); sqlite3VdbeJumpHere(v, addr1); if( memRootPage<0 ) sqlite3VdbeAddOp2(v, OP_Clear, tnum, iDb); - sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb, + sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, (int)tnum, iDb, (char *)pKey, P4_KEYINFO); sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0)); addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0); VdbeCoverage(v); - assert( pKey!=0 || db->mallocFailed || pParse->nErr ); - if( IsUniqueIndex(pIndex) && pKey!=0 ){ - int j2 = sqlite3VdbeCurrentAddr(v) + 3; - sqlite3VdbeGoto(v, j2); + if( IsUniqueIndex(pIndex) ){ + int j2 = sqlite3VdbeGoto(v, 1); addr2 = sqlite3VdbeCurrentAddr(v); + sqlite3VdbeVerifyAbortable(v, OE_Abort); sqlite3VdbeAddOp4Int(v, OP_SorterCompare, iSorter, j2, regRecord, pIndex->nKeyCol); VdbeCoverage(v); sqlite3UniqueConstraint(pParse, OE_Abort, pIndex); + sqlite3VdbeJumpHere(v, j2); }else{ + /* Most CREATE INDEX and REINDEX statements that are not UNIQUE can not + ** abort. The exception is if one of the indexed expressions contains a + ** user function that throws an exception when it is evaluated. But the + ** overhead of adding a statement journal to a CREATE INDEX statement is + ** very small (since most of the pages written do not contain content that + ** needs to be restored if the statement aborts), so we call + ** sqlite3MayAbort() for all CREATE INDEX statements. */ + sqlite3MayAbort(pParse); addr2 = sqlite3VdbeCurrentAddr(v); } sqlite3VdbeAddOp3(v, OP_SorterData, iSorter, regRecord, iIdx); - sqlite3VdbeAddOp3(v, OP_Last, iIdx, 0, -1); - sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 0); + if( !pIndex->bAscKeyBug ){ + /* This OP_SeekEnd opcode makes index insert for a REINDEX go much + ** faster by avoiding unnecessary seeks. But the optimization does + ** not work for UNIQUE constraint indexes on WITHOUT ROWID tables + ** with DESC primary keys, since those indexes have there keys in + ** a different order from the main table. + ** See ticket: https://sqlite.org/src/info/bba7b69f9849b5bf + */ + sqlite3VdbeAddOp1(v, OP_SeekEnd, iIdx); + } + sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdx, regRecord); sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); sqlite3ReleaseTempReg(pParse, regRecord); sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2); VdbeCoverage(v); @@ -2829,13 +3872,14 @@ static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){ */ Index *sqlite3AllocateIndexObject( sqlite3 *db, /* Database connection */ - i16 nCol, /* Total number of columns in the index */ + int nCol, /* Total number of columns in the index */ int nExtra, /* Number of bytes of extra space to alloc */ char **ppExtra /* Pointer to the "extra" space */ ){ Index *p; /* Allocated index object */ - int nByte; /* Bytes of space for Index object + arrays */ + i64 nByte; /* Bytes of space for Index object + arrays */ + assert( nCol <= 2*db->aLimit[SQLITE_LIMIT_COLUMN] ); nByte = ROUND8(sizeof(Index)) + /* Index structure */ ROUND8(sizeof(char*)*nCol) + /* Index.azColl */ ROUND8(sizeof(LogEst)*(nCol+1) + /* Index.aiRowLogEst */ @@ -2848,16 +3892,38 @@ Index *sqlite3AllocateIndexObject( p->aiRowLogEst = (LogEst*)pExtra; pExtra += sizeof(LogEst)*(nCol+1); p->aiColumn = (i16*)pExtra; pExtra += sizeof(i16)*nCol; p->aSortOrder = (u8*)pExtra; - p->nColumn = nCol; - p->nKeyCol = nCol - 1; + assert( nCol>0 ); + p->nColumn = (u16)nCol; + p->nKeyCol = (u16)(nCol - 1); *ppExtra = ((char*)p) + nByte; } return p; } /* -** Create a new index for an SQL table. pName1.pName2 is the name of the index -** and pTblList is the name of the table that is to be indexed. Both will +** If expression list pList contains an expression that was parsed with +** an explicit "NULLS FIRST" or "NULLS LAST" clause, leave an error in +** pParse and return non-zero. Otherwise, return zero. +*/ +int sqlite3HasExplicitNulls(Parse *pParse, ExprList *pList){ + if( pList ){ + int i; + for(i=0; inExpr; i++){ + if( pList->a[i].fg.bNulls ){ + u8 sf = pList->a[i].fg.sortFlags; + sqlite3ErrorMsg(pParse, "unsupported use of NULLS %s", + (sf==0 || sf==3) ? "FIRST" : "LAST" + ); + return 1; + } + } + } + return 0; +} + +/* +** Create a new index for an SQL table. pName1.pName2 is the name of the index +** and pTblList is the name of the table that is to be indexed. Both will ** be NULL for a primary key or an index that is created to satisfy a ** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable ** as the table to be indexed. pParse->pNewTable is a table that is @@ -2865,13 +3931,9 @@ Index *sqlite3AllocateIndexObject( ** ** pList is a list of columns to be indexed. pList will be NULL if this ** is a primary key or unique-constraint on the most recent column added -** to the table currently under construction. -** -** If the index is created successfully, return a pointer to the new Index -** structure. This is used by sqlite3AddPrimaryKey() to mark the index -** as the tables primary key (Index.idxType==SQLITE_IDXTYPE_PRIMARYKEY) +** to the table currently under construction. */ -Index *sqlite3CreateIndex( +void sqlite3CreateIndex( Parse *pParse, /* All information about this parse */ Token *pName1, /* First part of index name. May be NULL */ Token *pName2, /* Second part of index name. May be NULL */ @@ -2881,9 +3943,9 @@ Index *sqlite3CreateIndex( Token *pStart, /* The CREATE token that begins this statement */ Expr *pPIWhere, /* WHERE clause for partial indices */ int sortOrder, /* Sort order of primary key when pList==NULL */ - int ifNotExist /* Omit error if index already exists */ + int ifNotExist, /* Omit error if index already exists */ + u8 idxType /* The index type */ ){ - Index *pRet = 0; /* Pointer to return */ Table *pTab = 0; /* Table to be indexed */ Index *pIndex = 0; /* The index to be created */ char *zName = 0; /* Name of the index */ @@ -2901,19 +3963,27 @@ Index *sqlite3CreateIndex( char *zExtra = 0; /* Extra space after the Index object */ Index *pPk = 0; /* PRIMARY KEY index for WITHOUT ROWID tables */ - if( db->mallocFailed || IN_DECLARE_VTAB || pParse->nErr>0 ){ + assert( db->pParse==pParse ); + if( pParse->nErr ){ + goto exit_create_index; + } + assert( db->mallocFailed==0 ); + if( IN_DECLARE_VTAB && idxType!=SQLITE_IDXTYPE_PRIMARYKEY ){ goto exit_create_index; } if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ goto exit_create_index; } + if( sqlite3HasExplicitNulls(pParse, pList) ){ + goto exit_create_index; + } /* ** Find the table that is to be indexed. Return early if not found. */ if( pTblName!=0 ){ - /* Use the two-part index name to determine the database + /* Use the two-part index name to determine the database ** to search for the table. 'Fix' the table name to this db ** before looking up the table. */ @@ -2925,7 +3995,7 @@ Index *sqlite3CreateIndex( #ifndef SQLITE_OMIT_TEMPDB /* If the index name was unqualified, check if the table ** is a temp table. If so, set the database to 1. Do not do this - ** if initialising a database schema. + ** if initializing a database schema. */ if( !db->init.busy ){ pTab = sqlite3SrcListLookup(pParse, pTblName); @@ -2945,7 +4015,7 @@ Index *sqlite3CreateIndex( assert( db->mallocFailed==0 || pTab==0 ); if( pTab==0 ) goto exit_create_index; if( iDb==1 && db->aDb[iDb].pSchema!=pTab->pSchema ){ - sqlite3ErrorMsg(pParse, + sqlite3ErrorMsg(pParse, "cannot create a TEMP index on non-TEMP table \"%s\"", pTab->zName); goto exit_create_index; @@ -2961,18 +4031,15 @@ Index *sqlite3CreateIndex( pDb = &db->aDb[iDb]; assert( pTab!=0 ); - assert( pParse->nErr==0 ); - if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 + if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 && db->init.busy==0 -#if SQLITE_USER_AUTHENTICATION - && sqlite3UserAuthTable(pTab->zName)==0 -#endif - && sqlite3StrNICmp(&pTab->zName[7],"altertab_",9)!=0 ){ + && pTblName!=0 + ){ sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName); goto exit_create_index; } #ifndef SQLITE_OMIT_VIEW - if( pTab->pSelect ){ + if( IsView(pTab) ){ sqlite3ErrorMsg(pParse, "views may not be indexed"); goto exit_create_index; } @@ -2986,10 +4053,10 @@ Index *sqlite3CreateIndex( /* ** Find the name of the index. Make sure there is not already another - ** index or table with the same name. + ** index or table with the same name. ** ** Exception: If we are reading the names of permanent indices from the - ** sqlite_master table (because some other process changed the schema) and + ** sqlite_schema table (because some other process changed the schema) and ** one of the index names collides with the name of a temporary table or ** index, then we will continue to process this index. ** @@ -3001,23 +4068,26 @@ Index *sqlite3CreateIndex( zName = sqlite3NameFromToken(db, pName); if( zName==0 ) goto exit_create_index; assert( pName->z!=0 ); - if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ + if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName,"index",pTab->zName) ){ goto exit_create_index; } - if( !db->init.busy ){ - if( sqlite3FindTable(db, zName, 0)!=0 ){ - sqlite3ErrorMsg(pParse, "there is already a table named %s", zName); - goto exit_create_index; + if( !IN_RENAME_OBJECT ){ + if( !db->init.busy ){ + if( sqlite3FindTable(db, zName, pDb->zDbSName)!=0 ){ + sqlite3ErrorMsg(pParse, "there is already a table named %s", zName); + goto exit_create_index; + } } - } - if( sqlite3FindIndex(db, zName, pDb->zName)!=0 ){ - if( !ifNotExist ){ - sqlite3ErrorMsg(pParse, "index %s already exists", zName); - }else{ - assert( !db->init.busy ); - sqlite3CodeVerifySchema(pParse, iDb); + if( sqlite3FindIndex(db, zName, pDb->zDbSName)!=0 ){ + if( !ifNotExist ){ + sqlite3ErrorMsg(pParse, "index %s already exists", zName); + }else{ + assert( !db->init.busy ); + sqlite3CodeVerifySchema(pParse, iDb); + sqlite3ForceNotReadOnly(pParse); + } + goto exit_create_index; } - goto exit_create_index; } }else{ int n; @@ -3027,13 +4097,20 @@ Index *sqlite3CreateIndex( if( zName==0 ){ goto exit_create_index; } + + /* Automatic index names generated from within sqlite3_declare_vtab() + ** must have names that are distinct from normal automatic index names. + ** The following statement converts "sqlite3_autoindex..." into + ** "sqlite3_butoindex..." in order to make the names distinct. + ** The "vtab_err.test" test demonstrates the need of this statement. */ + if( IN_SPECIAL_PARSE ) zName[7]++; } /* Check for authorization to create an index. */ #ifndef SQLITE_OMIT_AUTHORIZATION - { - const char *zDb = pDb->zName; + if( !IN_RENAME_OBJECT ){ + const char *zDb = pDb->zDbSName; if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iDb), 0, zDb) ){ goto exit_create_index; } @@ -3051,14 +4128,17 @@ Index *sqlite3CreateIndex( */ if( pList==0 ){ Token prevCol; - sqlite3TokenInit(&prevCol, pTab->aCol[pTab->nCol-1].zName); + Column *pCol = &pTab->aCol[pTab->nCol-1]; + pCol->colFlags |= COLFLAG_UNIQUE; + sqlite3TokenInit(&prevCol, pCol->zCnName); pList = sqlite3ExprListAppend(pParse, 0, sqlite3ExprAlloc(db, TK_ID, &prevCol, 0)); if( pList==0 ) goto exit_create_index; assert( pList->nExpr==1 ); - sqlite3ExprListSetSortOrder(pList, sortOrder); + sqlite3ExprListSetSortOrder(pList, sortOrder, SQLITE_SO_UNDEFINED); }else{ sqlite3ExprListCheckLength(pParse, pList, "index"); + if( pParse->nErr ) goto exit_create_index; } /* Figure out how many bytes of space are required to store explicitly @@ -3068,15 +4148,17 @@ Index *sqlite3CreateIndex( Expr *pExpr = pList->a[i].pExpr; assert( pExpr!=0 ); if( pExpr->op==TK_COLLATE ){ + assert( !ExprHasProperty(pExpr, EP_IntValue) ); nExtra += (1 + sqlite3Strlen30(pExpr->u.zToken)); } } - /* - ** Allocate the index structure. + /* + ** Allocate the index structure. */ nName = sqlite3Strlen30(zName); nExtraCol = pPk ? pPk->nKeyCol : 1; + assert( pList->nExpr + nExtraCol <= 32767 /* Fits in i16 */ ); pIndex = sqlite3AllocateIndexObject(db, pList->nExpr + nExtraCol, nName + nExtra + 1, &zExtra); if( db->mallocFailed ){ @@ -3090,7 +4172,7 @@ Index *sqlite3CreateIndex( pIndex->pTable = pTab; pIndex->onError = (u8)onError; pIndex->uniqNotNull = onError!=OE_None; - pIndex->idxType = pName ? SQLITE_IDXTYPE_APPDEF : SQLITE_IDXTYPE_UNIQUE; + pIndex->idxType = idxType; pIndex->pSchema = db->aDb[iDb].pSchema; pIndex->nKeyCol = pList->nExpr; if( pPIWhere ){ @@ -3117,7 +4199,12 @@ Index *sqlite3CreateIndex( ** TODO: Issue a warning if the table primary key is used as part of the ** index key. */ - for(i=0, pListItem=pList->a; inExpr; i++, pListItem++){ + pListItem = pList->a; + if( IN_RENAME_OBJECT ){ + pIndex->aColExpr = pList; + pList = 0; + } + for(i=0; inKeyCol; i++, pListItem++){ Expr *pCExpr; /* The i-th index expression */ int requestedSortOrder; /* ASC or DESC on the i-th expression */ const char *zColl; /* Collation sequence name */ @@ -3133,29 +4220,33 @@ Index *sqlite3CreateIndex( goto exit_create_index; } if( pIndex->aColExpr==0 ){ - ExprList *pCopy = sqlite3ExprListDup(db, pList, 0); - pIndex->aColExpr = pCopy; - if( !db->mallocFailed ){ - assert( pCopy!=0 ); - pListItem = &pCopy->a[i]; - } + pIndex->aColExpr = pList; + pList = 0; } j = XN_EXPR; pIndex->aiColumn[i] = XN_EXPR; pIndex->uniqNotNull = 0; + pIndex->bHasExpr = 1; }else{ j = pCExpr->iColumn; assert( j<=0x7fff ); if( j<0 ){ j = pTab->iPKey; - }else if( pTab->aCol[j].notNull==0 ){ - pIndex->uniqNotNull = 0; + }else{ + if( pTab->aCol[j].notNull==0 ){ + pIndex->uniqNotNull = 0; + } + if( pTab->aCol[j].colFlags & COLFLAG_VIRTUAL ){ + pIndex->bHasVCol = 1; + pIndex->bHasExpr = 1; + } } pIndex->aiColumn[i] = (i16)j; } zColl = 0; if( pListItem->pExpr->op==TK_COLLATE ){ int nColl; + assert( !ExprHasProperty(pListItem->pExpr, EP_IntValue) ); zColl = pListItem->pExpr->u.zToken; nColl = sqlite3Strlen30(zColl) + 1; assert( nExtra>=nColl ); @@ -3164,14 +4255,14 @@ Index *sqlite3CreateIndex( zExtra += nColl; nExtra -= nColl; }else if( j>=0 ){ - zColl = pTab->aCol[j].zColl; + zColl = sqlite3ColumnColl(&pTab->aCol[j]); } if( !zColl ) zColl = sqlite3StrBINARY; if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){ goto exit_create_index; } pIndex->azColl[i] = zColl; - requestedSortOrder = pListItem->sortOrder & sortOrderMask; + requestedSortOrder = pListItem->fg.sortFlags & sortOrderMask; pIndex->aSortOrder[i] = (u8)requestedSortOrder; } @@ -3183,9 +4274,10 @@ Index *sqlite3CreateIndex( for(j=0; jnKeyCol; j++){ int x = pPk->aiColumn[j]; assert( x>=0 ); - if( hasColumn(pIndex->aiColumn, pIndex->nKeyCol, x) ){ - pIndex->nColumn--; + if( isDupColumn(pIndex, pIndex->nKeyCol, pPk, j) ){ + pIndex->nColumn--; }else{ + testcase( hasColumn(pIndex->aiColumn,pIndex->nKeyCol,x) ); pIndex->aiColumn[i] = x; pIndex->azColl[i] = pPk->azColl[j]; pIndex->aSortOrder[i] = pPk->aSortOrder[j]; @@ -3200,6 +4292,21 @@ Index *sqlite3CreateIndex( sqlite3DefaultRowEst(pIndex); if( pParse->pNewTable==0 ) estimateIndexWidth(pIndex); + /* If this index contains every column of its table, then mark + ** it as a covering index */ + assert( HasRowid(pTab) + || pTab->iPKey<0 || sqlite3TableColumnToIndex(pIndex, pTab->iPKey)>=0 ); + recomputeColumnsNotIndexed(pIndex); + if( pTblName!=0 && pIndex->nColumn>=pTab->nCol ){ + pIndex->isCovering = 1; + for(j=0; jnCol; j++){ + if( j==pTab->iPKey ) continue; + if( sqlite3TableColumnToIndex(pIndex,j)>=0 ) continue; + pIndex->isCovering = 0; + break; + } + } + if( pTab==pParse->pNewTable ){ /* This routine has been called to create an automatic index as a ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or @@ -3237,155 +4344,185 @@ Index *sqlite3CreateIndex( if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break; z1 = pIdx->azColl[k]; z2 = pIndex->azColl[k]; - if( z1!=z2 && sqlite3StrICmp(z1, z2) ) break; + if( sqlite3StrICmp(z1, z2) ) break; } if( k==pIdx->nKeyCol ){ if( pIdx->onError!=pIndex->onError ){ /* This constraint creates the same index as a previous ** constraint specified somewhere in the CREATE TABLE statement. - ** However the ON CONFLICT clauses are different. If both this + ** However the ON CONFLICT clauses are different. If both this ** constraint and the previous equivalent constraint have explicit ** ON CONFLICT clauses this is an error. Otherwise, use the ** explicitly specified behavior for the index. */ if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){ - sqlite3ErrorMsg(pParse, + sqlite3ErrorMsg(pParse, "conflicting ON CONFLICT clauses specified", 0); } if( pIdx->onError==OE_Default ){ pIdx->onError = pIndex->onError; } } - pRet = pIdx; + if( idxType==SQLITE_IDXTYPE_PRIMARYKEY ) pIdx->idxType = idxType; + if( IN_RENAME_OBJECT ){ + pIndex->pNext = pParse->pNewIndex; + pParse->pNewIndex = pIndex; + pIndex = 0; + } goto exit_create_index; } } } - /* Link the new Index structure to its table and to the other - ** in-memory database structures. - */ - assert( pParse->nErr==0 ); - if( db->init.busy ){ - Index *p; - assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) ); - p = sqlite3HashInsert(&pIndex->pSchema->idxHash, - pIndex->zName, pIndex); - if( p ){ - assert( p==pIndex ); /* Malloc must have failed */ - sqlite3OomFault(db); - goto exit_create_index; - } - db->flags |= SQLITE_InternChanges; - if( pTblName!=0 ){ - pIndex->tnum = db->init.newTnum; - } - } - - /* If this is the initial CREATE INDEX statement (or CREATE TABLE if the - ** index is an implied index for a UNIQUE or PRIMARY KEY constraint) then - ** emit code to allocate the index rootpage on disk and make an entry for - ** the index in the sqlite_master table and populate the index with - ** content. But, do not do this if we are simply reading the sqlite_master - ** table to parse the schema, or if this index is the PRIMARY KEY index - ** of a WITHOUT ROWID table. - ** - ** If pTblName==0 it means this index is generated as an implied PRIMARY KEY - ** or UNIQUE index in a CREATE TABLE statement. Since the table - ** has just been created, it contains no data and the index initialization - ** step can be skipped. - */ - else if( HasRowid(pTab) || pTblName!=0 ){ - Vdbe *v; - char *zStmt; - int iMem = ++pParse->nMem; - - v = sqlite3GetVdbe(pParse); - if( v==0 ) goto exit_create_index; - - sqlite3BeginWriteOperation(pParse, 1, iDb); + if( !IN_RENAME_OBJECT ){ - /* Create the rootpage for the index using CreateIndex. But before - ** doing so, code a Noop instruction and store its address in - ** Index.tnum. This is required in case this index is actually a - ** PRIMARY KEY and the table is actually a WITHOUT ROWID table. In - ** that case the convertToWithoutRowidTable() routine will replace - ** the Noop with a Goto to jump over the VDBE code generated below. */ - pIndex->tnum = sqlite3VdbeAddOp0(v, OP_Noop); - sqlite3VdbeAddOp2(v, OP_CreateIndex, iDb, iMem); - - /* Gather the complete text of the CREATE INDEX statement into - ** the zStmt variable + /* Link the new Index structure to its table and to the other + ** in-memory database structures. */ - if( pStart ){ - int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n; - if( pName->z[n-1]==';' ) n--; - /* A named index with an explicit CREATE INDEX statement */ - zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s", - onError==OE_None ? "" : " UNIQUE", n, pName->z); - }else{ - /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */ - /* zStmt = sqlite3MPrintf(""); */ - zStmt = 0; + assert( pParse->nErr==0 ); + if( db->init.busy ){ + Index *p; + assert( !IN_SPECIAL_PARSE ); + assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) ); + if( pTblName!=0 ){ + pIndex->tnum = db->init.newTnum; + if( sqlite3IndexHasDuplicateRootPage(pIndex) ){ + sqlite3ErrorMsg(pParse, "invalid rootpage"); + pParse->rc = SQLITE_CORRUPT_BKPT; + goto exit_create_index; + } + } + p = sqlite3HashInsert(&pIndex->pSchema->idxHash, + pIndex->zName, pIndex); + if( p ){ + assert( p==pIndex ); /* Malloc must have failed */ + sqlite3OomFault(db); + goto exit_create_index; + } + db->mDbFlags |= DBFLAG_SchemaChange; } - /* Add an entry in sqlite_master for this index - */ - sqlite3NestedParse(pParse, - "INSERT INTO %Q.%s VALUES('index',%Q,%Q,#%d,%Q);", - db->aDb[iDb].zName, SCHEMA_TABLE(iDb), - pIndex->zName, - pTab->zName, - iMem, - zStmt - ); - sqlite3DbFree(db, zStmt); - - /* Fill the index with data and reparse the schema. Code an OP_Expire - ** to invalidate all pre-compiled statements. + /* If this is the initial CREATE INDEX statement (or CREATE TABLE if the + ** index is an implied index for a UNIQUE or PRIMARY KEY constraint) then + ** emit code to allocate the index rootpage on disk and make an entry for + ** the index in the sqlite_schema table and populate the index with + ** content. But, do not do this if we are simply reading the sqlite_schema + ** table to parse the schema, or if this index is the PRIMARY KEY index + ** of a WITHOUT ROWID table. + ** + ** If pTblName==0 it means this index is generated as an implied PRIMARY KEY + ** or UNIQUE index in a CREATE TABLE statement. Since the table + ** has just been created, it contains no data and the index initialization + ** step can be skipped. */ - if( pTblName ){ - sqlite3RefillIndex(pParse, pIndex, iMem); - sqlite3ChangeCookie(pParse, iDb); - sqlite3VdbeAddParseSchemaOp(v, iDb, - sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName)); - sqlite3VdbeAddOp1(v, OP_Expire, 0); - } + else if( HasRowid(pTab) || pTblName!=0 ){ + Vdbe *v; + char *zStmt; + int iMem = ++pParse->nMem; + + v = sqlite3GetVdbe(pParse); + if( v==0 ) goto exit_create_index; + + sqlite3BeginWriteOperation(pParse, 1, iDb); + + /* Create the rootpage for the index using CreateIndex. But before + ** doing so, code a Noop instruction and store its address in + ** Index.tnum. This is required in case this index is actually a + ** PRIMARY KEY and the table is actually a WITHOUT ROWID table. In + ** that case the convertToWithoutRowidTable() routine will replace + ** the Noop with a Goto to jump over the VDBE code generated below. */ + pIndex->tnum = (Pgno)sqlite3VdbeAddOp0(v, OP_Noop); + sqlite3VdbeAddOp3(v, OP_CreateBtree, iDb, iMem, BTREE_BLOBKEY); + + /* Gather the complete text of the CREATE INDEX statement into + ** the zStmt variable + */ + assert( pName!=0 || pStart==0 ); + if( pStart ){ + int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n; + if( pName->z[n-1]==';' ) n--; + /* A named index with an explicit CREATE INDEX statement */ + zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s", + onError==OE_None ? "" : " UNIQUE", n, pName->z); + }else{ + /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */ + /* zStmt = sqlite3MPrintf(""); */ + zStmt = 0; + } - sqlite3VdbeJumpHere(v, pIndex->tnum); - } + /* Add an entry in sqlite_schema for this index + */ + sqlite3NestedParse(pParse, + "INSERT INTO %Q." LEGACY_SCHEMA_TABLE " VALUES('index',%Q,%Q,#%d,%Q);", + db->aDb[iDb].zDbSName, + pIndex->zName, + pTab->zName, + iMem, + zStmt + ); + sqlite3DbFree(db, zStmt); - /* When adding an index to the list of indices for a table, make - ** sure all indices labeled OE_Replace come after all those labeled - ** OE_Ignore. This is necessary for the correct constraint check - ** processing (in sqlite3GenerateConstraintChecks()) as part of - ** UPDATE and INSERT statements. - */ - if( db->init.busy || pTblName==0 ){ - if( onError!=OE_Replace || pTab->pIndex==0 - || pTab->pIndex->onError==OE_Replace){ - pIndex->pNext = pTab->pIndex; - pTab->pIndex = pIndex; - }else{ - Index *pOther = pTab->pIndex; - while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){ - pOther = pOther->pNext; + /* Fill the index with data and reparse the schema. Code an OP_Expire + ** to invalidate all pre-compiled statements. + */ + if( pTblName ){ + sqlite3RefillIndex(pParse, pIndex, iMem); + sqlite3ChangeCookie(pParse, iDb); + sqlite3VdbeAddParseSchemaOp(v, iDb, + sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName), 0); + sqlite3VdbeAddOp2(v, OP_Expire, 0, 1); } - pIndex->pNext = pOther->pNext; - pOther->pNext = pIndex; + + sqlite3VdbeJumpHere(v, (int)pIndex->tnum); } - pRet = pIndex; + } + if( db->init.busy || pTblName==0 ){ + pIndex->pNext = pTab->pIndex; + pTab->pIndex = pIndex; + pIndex = 0; + } + else if( IN_RENAME_OBJECT ){ + assert( pParse->pNewIndex==0 ); + pParse->pNewIndex = pIndex; pIndex = 0; } /* Clean up before exiting */ exit_create_index: - if( pIndex ) freeIndex(db, pIndex); + if( pIndex ) sqlite3FreeIndex(db, pIndex); + if( pTab ){ + /* Ensure all REPLACE indexes on pTab are at the end of the pIndex list. + ** The list was already ordered when this routine was entered, so at this + ** point at most a single index (the newly added index) will be out of + ** order. So we have to reorder at most one index. */ + Index **ppFrom; + Index *pThis; + for(ppFrom=&pTab->pIndex; (pThis = *ppFrom)!=0; ppFrom=&pThis->pNext){ + Index *pNext; + if( pThis->onError!=OE_Replace ) continue; + while( (pNext = pThis->pNext)!=0 && pNext->onError!=OE_Replace ){ + *ppFrom = pNext; + pThis->pNext = pNext->pNext; + pNext->pNext = pThis; + ppFrom = &pNext->pNext; + } + break; + } +#ifdef SQLITE_DEBUG + /* Verify that all REPLACE indexes really are now at the end + ** of the index list. In other words, no other index type ever + ** comes after a REPLACE index on the list. */ + for(pThis = pTab->pIndex; pThis; pThis=pThis->pNext){ + assert( pThis->onError!=OE_Replace + || pThis->pNext==0 + || pThis->pNext->onError==OE_Replace ); + } +#endif + } sqlite3ExprDelete(db, pPIWhere); sqlite3ExprListDelete(db, pList); sqlite3SrcListDelete(db, pTblName); sqlite3DbFree(db, zName); - return pRet; } /* @@ -3407,17 +4544,33 @@ Index *sqlite3CreateIndex( ** are based on typical values found in actual indices. */ void sqlite3DefaultRowEst(Index *pIdx){ - /* 10, 9, 8, 7, 6 */ - LogEst aVal[] = { 33, 32, 30, 28, 26 }; + /* 10, 9, 8, 7, 6 */ + static const LogEst aVal[] = { 33, 32, 30, 28, 26 }; LogEst *a = pIdx->aiRowLogEst; + LogEst x; int nCopy = MIN(ArraySize(aVal), pIdx->nKeyCol); int i; - /* Set the first entry (number of rows in the index) to the estimated - ** number of rows in the table. Or 10, if the estimated number of rows - ** in the table is less than that. */ - a[0] = pIdx->pTable->nRowLogEst; - if( a[0]<33 ) a[0] = 33; assert( 33==sqlite3LogEst(10) ); + /* Indexes with default row estimates should not have stat1 data */ + assert( !pIdx->hasStat1 ); + + /* Set the first entry (number of rows in the index) to the estimated + ** number of rows in the table, or half the number of rows in the table + ** for a partial index. + ** + ** 2020-05-27: If some of the stat data is coming from the sqlite_stat1 + ** table but other parts we are having to guess at, then do not let the + ** estimated number of rows in the table be less than 1000 (LogEst 99). + ** Failure to do this can cause the indexes for which we do not have + ** stat1 data to be ignored by the query planner. + */ + x = pIdx->pTable->nRowLogEst; + assert( 99==sqlite3LogEst(1000) ); + if( x<99 ){ + pIdx->pTable->nRowLogEst = x = 99; + } + if( pIdx->pPartIdxWhere!=0 ){ x -= 10; assert( 10==sqlite3LogEst(2) ); } + a[0] = x; /* Estimate that a[1] is 10, a[2] is 9, a[3] is 8, a[4] is 7, a[5] is ** 6 and each subsequent value (if any) is 5. */ @@ -3440,20 +4593,23 @@ void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists){ sqlite3 *db = pParse->db; int iDb; - assert( pParse->nErr==0 ); /* Never called with prior errors */ if( db->mallocFailed ){ goto exit_drop_index; } + assert( pParse->nErr==0 ); /* Never called with prior non-OOM errors */ assert( pName->nSrc==1 ); + assert( pName->a[0].fg.fixedSchema==0 ); + assert( pName->a[0].fg.isSubquery==0 ); if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ goto exit_drop_index; } - pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase); + pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].u4.zDatabase); if( pIndex==0 ){ if( !ifExists ){ - sqlite3ErrorMsg(pParse, "no such index: %S", pName, 0); + sqlite3ErrorMsg(pParse, "no such index: %S", pName->a); }else{ - sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase); + sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].u4.zDatabase); + sqlite3ForceNotReadOnly(pParse); } pParse->checkSchema = 1; goto exit_drop_index; @@ -3468,25 +4624,25 @@ void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists){ { int code = SQLITE_DROP_INDEX; Table *pTab = pIndex->pTable; - const char *zDb = db->aDb[iDb].zName; + const char *zDb = db->aDb[iDb].zDbSName; const char *zTab = SCHEMA_TABLE(iDb); if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){ goto exit_drop_index; } - if( !OMIT_TEMPDB && iDb ) code = SQLITE_DROP_TEMP_INDEX; + if( !OMIT_TEMPDB && iDb==1 ) code = SQLITE_DROP_TEMP_INDEX; if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){ goto exit_drop_index; } } #endif - /* Generate code to remove the index and from the master table */ + /* Generate code to remove the index and from the schema table */ v = sqlite3GetVdbe(pParse); if( v ){ sqlite3BeginWriteOperation(pParse, 1, iDb); sqlite3NestedParse(pParse, - "DELETE FROM %Q.%s WHERE name=%Q AND type='index'", - db->aDb[iDb].zName, SCHEMA_TABLE(iDb), pIndex->zName + "DELETE FROM %Q." LEGACY_SCHEMA_TABLE " WHERE name=%Q AND type='index'", + db->aDb[iDb].zDbSName, pIndex->zName ); sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName); sqlite3ChangeCookie(pParse, iDb); @@ -3523,9 +4679,9 @@ void *sqlite3ArrayAllocate( int *pIdx /* Write the index of a new slot here */ ){ char *z; - int n = *pnEntry; + sqlite3_int64 n = *pIdx = *pnEntry; if( (n & (n-1))==0 ){ - int sz = (n==0) ? 1 : 2*n; + sqlite3_int64 sz = (n==0) ? 1 : 2*n; void *pNew = sqlite3DbRealloc(db, pArray, sz*szEntry); if( pNew==0 ){ *pIdx = -1; @@ -3535,7 +4691,6 @@ void *sqlite3ArrayAllocate( } z = (char*)pArray; memset(&z[n * szEntry], 0, szEntry); - *pIdx = n; ++*pnEntry; return pArray; } @@ -3546,24 +4701,26 @@ void *sqlite3ArrayAllocate( ** ** A new IdList is returned, or NULL if malloc() fails. */ -IdList *sqlite3IdListAppend(sqlite3 *db, IdList *pList, Token *pToken){ +IdList *sqlite3IdListAppend(Parse *pParse, IdList *pList, Token *pToken){ + sqlite3 *db = pParse->db; int i; if( pList==0 ){ - pList = sqlite3DbMallocZero(db, sizeof(IdList) ); + pList = sqlite3DbMallocZero(db, SZ_IDLIST(1)); if( pList==0 ) return 0; + }else{ + IdList *pNew; + pNew = sqlite3DbRealloc(db, pList, SZ_IDLIST(pList->nId+1)); + if( pNew==0 ){ + sqlite3IdListDelete(db, pList); + return 0; + } + pList = pNew; } - pList->a = sqlite3ArrayAllocate( - db, - pList->a, - sizeof(pList->a[0]), - &pList->nId, - &i - ); - if( i<0 ){ - sqlite3IdListDelete(db, pList); - return 0; - } + i = pList->nId++; pList->a[i].zName = sqlite3NameFromToken(db, pToken); + if( IN_RENAME_OBJECT && pList->a[i].zName ){ + sqlite3RenameTokenMap(pParse, (void*)pList->a[i].zName, pToken); + } return pList; } @@ -3572,12 +4729,12 @@ IdList *sqlite3IdListAppend(sqlite3 *db, IdList *pList, Token *pToken){ */ void sqlite3IdListDelete(sqlite3 *db, IdList *pList){ int i; + assert( db!=0 ); if( pList==0 ) return; for(i=0; inId; i++){ sqlite3DbFree(db, pList->a[i].zName); } - sqlite3DbFree(db, pList->a); - sqlite3DbFree(db, pList); + sqlite3DbNNFreeNN(db, pList); } /* @@ -3586,13 +4743,25 @@ void sqlite3IdListDelete(sqlite3 *db, IdList *pList){ */ int sqlite3IdListIndex(IdList *pList, const char *zName){ int i; - if( pList==0 ) return -1; + assert( pList!=0 ); for(i=0; inId; i++){ if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i; } return -1; } +/* +** Maximum size of a SrcList object. +** The SrcList object is used to represent the FROM clause of a +** SELECT statement, and the query planner cannot deal with more +** than 64 tables in a join. So any value larger than 64 here +** is sufficient for most uses. Smaller values, like say 10, are +** appropriate for small and memory-limited applications. +*/ +#ifndef SQLITE_MAX_SRCLIST +# define SQLITE_MAX_SRCLIST 200 +#endif + /* ** Expand the space allocated for the given SrcList object by ** creating nExtra new slots beginning at iStart. iStart is zero based. @@ -3609,11 +4778,12 @@ int sqlite3IdListIndex(IdList *pList, const char *zName){ ** the iStart value would be 0. The result then would ** be: nil, nil, nil, A, B. ** -** If a memory allocation fails the SrcList is unchanged. The -** db->mallocFailed flag will be set to true. +** If a memory allocation fails or the SrcList becomes too large, leave +** the original SrcList unchanged, return NULL, and leave an error message +** in pParse. */ SrcList *sqlite3SrcListEnlarge( - sqlite3 *db, /* Database connection to notify of OOM errors */ + Parse *pParse, /* Parsing context into which errors are reported */ SrcList *pSrc, /* The SrcList to be enlarged */ int nExtra, /* Number of new slots to add to pSrc->a[] */ int iStart /* Index in pSrc->a[] of first new slot */ @@ -3629,17 +4799,22 @@ SrcList *sqlite3SrcListEnlarge( /* Allocate additional space if needed */ if( (u32)pSrc->nSrc+nExtra>pSrc->nAlloc ){ SrcList *pNew; - int nAlloc = pSrc->nSrc+nExtra; - int nGot; - pNew = sqlite3DbRealloc(db, pSrc, - sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) ); + sqlite3_int64 nAlloc = 2*(sqlite3_int64)pSrc->nSrc+nExtra; + sqlite3 *db = pParse->db; + + if( pSrc->nSrc+nExtra>=SQLITE_MAX_SRCLIST ){ + sqlite3ErrorMsg(pParse, "too many FROM clause terms, max: %d", + SQLITE_MAX_SRCLIST); + return 0; + } + if( nAlloc>SQLITE_MAX_SRCLIST ) nAlloc = SQLITE_MAX_SRCLIST; + pNew = sqlite3DbRealloc(db, pSrc, SZ_SRCLIST(nAlloc)); if( pNew==0 ){ assert( db->mallocFailed ); - return pSrc; + return 0; } pSrc = pNew; - nGot = (sqlite3DbMallocSize(db, pNew) - sizeof(*pSrc))/sizeof(pSrc->a[0])+1; - pSrc->nAlloc = nGot; + pSrc->nAlloc = nAlloc; } /* Move existing slots that come after the newly inserted slots @@ -3664,7 +4839,8 @@ SrcList *sqlite3SrcListEnlarge( ** Append a new table name to the given SrcList. Create a new SrcList if ** need be. A new entry is created in the SrcList even if pTable is NULL. ** -** A SrcList is returned, or NULL if there is an OOM error. The returned +** A SrcList is returned, or NULL if there is an OOM error or if the +** SrcList grows to large. The returned ** SrcList might be the same as the SrcList that was input or it might be ** a new one. If an OOM error does occurs, then the prior value of pList ** that is input to this routine is automatically freed. @@ -3673,7 +4849,7 @@ SrcList *sqlite3SrcListEnlarge( ** database name prefix. Like this: "database.table". The pDatabase ** points to the table name and the pTable points to the database name. ** The SrcList.a[].zName field is filled with the table name which might -** come from pTable (if pDatabase is NULL) or from pDatabase. +** come from pTable (if pDatabase is NULL) or from pDatabase. ** SrcList.a[].zDatabase is filled with the database name from pTable, ** or with NULL if no database is specified. ** @@ -3695,36 +4871,46 @@ SrcList *sqlite3SrcListEnlarge( ** before being added to the SrcList. */ SrcList *sqlite3SrcListAppend( - sqlite3 *db, /* Connection to notify of malloc failures */ + Parse *pParse, /* Parsing context, in which errors are reported */ SrcList *pList, /* Append to this SrcList. NULL creates a new SrcList */ Token *pTable, /* Table to append */ Token *pDatabase /* Database of the table */ ){ - struct SrcList_item *pItem; + SrcItem *pItem; + sqlite3 *db; assert( pDatabase==0 || pTable!=0 ); /* Cannot have C without B */ - assert( db!=0 ); + assert( pParse!=0 ); + assert( pParse->db!=0 ); + db = pParse->db; if( pList==0 ){ - pList = sqlite3DbMallocRawNN(db, sizeof(SrcList) ); + pList = sqlite3DbMallocRawNN(pParse->db, SZ_SRCLIST(1)); if( pList==0 ) return 0; pList->nAlloc = 1; - pList->nSrc = 0; - } - pList = sqlite3SrcListEnlarge(db, pList, 1, pList->nSrc); - if( db->mallocFailed ){ - sqlite3SrcListDelete(db, pList); - return 0; + pList->nSrc = 1; + memset(&pList->a[0], 0, sizeof(pList->a[0])); + pList->a[0].iCursor = -1; + }else{ + SrcList *pNew = sqlite3SrcListEnlarge(pParse, pList, 1, pList->nSrc); + if( pNew==0 ){ + sqlite3SrcListDelete(db, pList); + return 0; + }else{ + pList = pNew; + } } pItem = &pList->a[pList->nSrc-1]; if( pDatabase && pDatabase->z==0 ){ pDatabase = 0; } + assert( pItem->fg.fixedSchema==0 ); + assert( pItem->fg.isSubquery==0 ); if( pDatabase ){ - Token *pTemp = pDatabase; - pDatabase = pTable; - pTable = pTemp; + pItem->zName = sqlite3NameFromToken(db, pDatabase); + pItem->u4.zDatabase = sqlite3NameFromToken(db, pTable); + }else{ + pItem->zName = sqlite3NameFromToken(db, pTable); + pItem->u4.zDatabase = 0; } - pItem->zName = sqlite3NameFromToken(db, pTable); - pItem->zDatabase = sqlite3NameFromToken(db, pDatabase); return pList; } @@ -3733,40 +4919,130 @@ SrcList *sqlite3SrcListAppend( */ void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){ int i; - struct SrcList_item *pItem; - assert(pList || pParse->db->mallocFailed ); - if( pList ){ + SrcItem *pItem; + assert( pList || pParse->db->mallocFailed ); + if( ALWAYS(pList) ){ for(i=0, pItem=pList->a; inSrc; i++, pItem++){ - if( pItem->iCursor>=0 ) break; + if( pItem->iCursor>=0 ) continue; pItem->iCursor = pParse->nTab++; - if( pItem->pSelect ){ - sqlite3SrcListAssignCursors(pParse, pItem->pSelect->pSrc); + if( pItem->fg.isSubquery ){ + assert( pItem->u4.pSubq!=0 ); + assert( pItem->u4.pSubq->pSelect!=0 ); + assert( pItem->u4.pSubq->pSelect->pSrc!=0 ); + sqlite3SrcListAssignCursors(pParse, pItem->u4.pSubq->pSelect->pSrc); } } } } +/* +** Delete a Subquery object and its substructure. +*/ +void sqlite3SubqueryDelete(sqlite3 *db, Subquery *pSubq){ + assert( pSubq!=0 && pSubq->pSelect!=0 ); + sqlite3SelectDelete(db, pSubq->pSelect); + sqlite3DbFree(db, pSubq); +} + +/* +** Remove a Subquery from a SrcItem. Return the associated Select object. +** The returned Select becomes the responsibility of the caller. +*/ +Select *sqlite3SubqueryDetach(sqlite3 *db, SrcItem *pItem){ + Select *pSel; + assert( pItem!=0 ); + assert( pItem->fg.isSubquery ); + pSel = pItem->u4.pSubq->pSelect; + sqlite3DbFree(db, pItem->u4.pSubq); + pItem->u4.pSubq = 0; + pItem->fg.isSubquery = 0; + return pSel; +} + /* ** Delete an entire SrcList including all its substructure. */ void sqlite3SrcListDelete(sqlite3 *db, SrcList *pList){ int i; - struct SrcList_item *pItem; + SrcItem *pItem; + assert( db!=0 ); if( pList==0 ) return; for(pItem=pList->a, i=0; inSrc; i++, pItem++){ - sqlite3DbFree(db, pItem->zDatabase); - sqlite3DbFree(db, pItem->zName); - sqlite3DbFree(db, pItem->zAlias); + + /* Check invariants on SrcItem */ + assert( !pItem->fg.isIndexedBy || !pItem->fg.isTabFunc ); + assert( !pItem->fg.isCte || !pItem->fg.isIndexedBy ); + assert( !pItem->fg.fixedSchema || !pItem->fg.isSubquery ); + assert( !pItem->fg.isSubquery || (pItem->u4.pSubq!=0 && + pItem->u4.pSubq->pSelect!=0) ); + + if( pItem->zName ) sqlite3DbNNFreeNN(db, pItem->zName); + if( pItem->zAlias ) sqlite3DbNNFreeNN(db, pItem->zAlias); + if( pItem->fg.isSubquery ){ + sqlite3SubqueryDelete(db, pItem->u4.pSubq); + }else if( pItem->fg.fixedSchema==0 && pItem->u4.zDatabase!=0 ){ + sqlite3DbNNFreeNN(db, pItem->u4.zDatabase); + } if( pItem->fg.isIndexedBy ) sqlite3DbFree(db, pItem->u1.zIndexedBy); if( pItem->fg.isTabFunc ) sqlite3ExprListDelete(db, pItem->u1.pFuncArg); - sqlite3DeleteTable(db, pItem->pTab); - sqlite3SelectDelete(db, pItem->pSelect); - sqlite3ExprDelete(db, pItem->pOn); - sqlite3IdListDelete(db, pItem->pUsing); + sqlite3DeleteTable(db, pItem->pSTab); + if( pItem->fg.isUsing ){ + sqlite3IdListDelete(db, pItem->u3.pUsing); + }else if( pItem->u3.pOn ){ + sqlite3ExprDelete(db, pItem->u3.pOn); + } + } + sqlite3DbNNFreeNN(db, pList); +} + +/* +** Attach a Subquery object to pItem->uv.pSubq. Set the +** pSelect value but leave all the other values initialized +** to zero. +** +** A copy of the Select object is made if dupSelect is true, and the +** SrcItem takes responsibility for deleting the copy. If dupSelect is +** false, ownership of the Select passes to the SrcItem. Either way, +** the SrcItem will take responsibility for deleting the Select. +** +** When dupSelect is zero, that means the Select might get deleted right +** away if there is an OOM error. Beware. +** +** Return non-zero on success. Return zero on an OOM error. +*/ +int sqlite3SrcItemAttachSubquery( + Parse *pParse, /* Parsing context */ + SrcItem *pItem, /* Item to which the subquery is to be attached */ + Select *pSelect, /* The subquery SELECT. Must be non-NULL */ + int dupSelect /* If true, attach a copy of pSelect, not pSelect itself.*/ +){ + Subquery *p; + assert( pSelect!=0 ); + assert( pItem->fg.isSubquery==0 ); + if( pItem->fg.fixedSchema ){ + pItem->u4.pSchema = 0; + pItem->fg.fixedSchema = 0; + }else if( pItem->u4.zDatabase!=0 ){ + sqlite3DbFree(pParse->db, pItem->u4.zDatabase); + pItem->u4.zDatabase = 0; + } + if( dupSelect ){ + pSelect = sqlite3SelectDup(pParse->db, pSelect, 0); + if( pSelect==0 ) return 0; + } + p = pItem->u4.pSubq = sqlite3DbMallocRawNN(pParse->db, sizeof(Subquery)); + if( p==0 ){ + sqlite3SelectDelete(pParse->db, pSelect); + return 0; } - sqlite3DbFree(db, pList); + pItem->fg.isSubquery = 1; + p->pSelect = pSelect; + assert( offsetof(Subquery, pSelect)==0 ); + memset(((char*)p)+sizeof(p->pSelect), 0, sizeof(*p)-sizeof(p->pSelect)); + return 1; } + /* ** This routine is called by the parser to add a new term to the ** end of a growing FROM clause. The "p" parameter is the part of @@ -3790,59 +5066,109 @@ SrcList *sqlite3SrcListAppendFromTerm( Token *pDatabase, /* Name of the database containing pTable */ Token *pAlias, /* The right-hand side of the AS subexpression */ Select *pSubquery, /* A subquery used in place of a table name */ - Expr *pOn, /* The ON clause of a join */ - IdList *pUsing /* The USING clause of a join */ + OnOrUsing *pOnUsing /* Either the ON clause or the USING clause */ ){ - struct SrcList_item *pItem; + SrcItem *pItem; sqlite3 *db = pParse->db; - if( !p && (pOn || pUsing) ){ - sqlite3ErrorMsg(pParse, "a JOIN clause is required before %s", - (pOn ? "ON" : "USING") + if( !p && pOnUsing!=0 && (pOnUsing->pOn || pOnUsing->pUsing) ){ + sqlite3ErrorMsg(pParse, "a JOIN clause is required before %s", + (pOnUsing->pOn ? "ON" : "USING") ); goto append_from_error; } - p = sqlite3SrcListAppend(db, p, pTable, pDatabase); - if( p==0 || NEVER(p->nSrc==0) ){ + p = sqlite3SrcListAppend(pParse, p, pTable, pDatabase); + if( p==0 ){ goto append_from_error; } + assert( p->nSrc>0 ); pItem = &p->a[p->nSrc-1]; + assert( (pTable==0)==(pDatabase==0) ); + assert( pItem->zName==0 || pDatabase!=0 ); + if( IN_RENAME_OBJECT && pItem->zName ){ + Token *pToken = (ALWAYS(pDatabase) && pDatabase->z) ? pDatabase : pTable; + sqlite3RenameTokenMap(pParse, pItem->zName, pToken); + } assert( pAlias!=0 ); if( pAlias->n ){ pItem->zAlias = sqlite3NameFromToken(db, pAlias); } - pItem->pSelect = pSubquery; - pItem->pOn = pOn; - pItem->pUsing = pUsing; + assert( pSubquery==0 || pDatabase==0 ); + if( pSubquery ){ + if( sqlite3SrcItemAttachSubquery(pParse, pItem, pSubquery, 0) ){ + if( pSubquery->selFlags & SF_NestedFrom ){ + pItem->fg.isNestedFrom = 1; + } + } + } + assert( pOnUsing==0 || pOnUsing->pOn==0 || pOnUsing->pUsing==0 ); + assert( pItem->fg.isUsing==0 ); + if( pOnUsing==0 ){ + pItem->u3.pOn = 0; + }else if( pOnUsing->pUsing ){ + pItem->fg.isUsing = 1; + pItem->u3.pUsing = pOnUsing->pUsing; + }else{ + pItem->u3.pOn = pOnUsing->pOn; + } return p; - append_from_error: +append_from_error: assert( p==0 ); - sqlite3ExprDelete(db, pOn); - sqlite3IdListDelete(db, pUsing); + sqlite3ClearOnOrUsing(db, pOnUsing); sqlite3SelectDelete(db, pSubquery); return 0; } /* -** Add an INDEXED BY or NOT INDEXED clause to the most recently added +** Add an INDEXED BY or NOT INDEXED clause to the most recently added ** element of the source-list passed as the second argument. */ void sqlite3SrcListIndexedBy(Parse *pParse, SrcList *p, Token *pIndexedBy){ assert( pIndexedBy!=0 ); - if( p && ALWAYS(p->nSrc>0) ){ - struct SrcList_item *pItem = &p->a[p->nSrc-1]; + if( p && pIndexedBy->n>0 ){ + SrcItem *pItem; + assert( p->nSrc>0 ); + pItem = &p->a[p->nSrc-1]; assert( pItem->fg.notIndexed==0 ); assert( pItem->fg.isIndexedBy==0 ); assert( pItem->fg.isTabFunc==0 ); if( pIndexedBy->n==1 && !pIndexedBy->z ){ - /* A "NOT INDEXED" clause was supplied. See parse.y + /* A "NOT INDEXED" clause was supplied. See parse.y ** construct "indexed_opt" for details. */ pItem->fg.notIndexed = 1; }else{ pItem->u1.zIndexedBy = sqlite3NameFromToken(pParse->db, pIndexedBy); - pItem->fg.isIndexedBy = (pItem->u1.zIndexedBy!=0); + pItem->fg.isIndexedBy = 1; + assert( pItem->fg.isCte==0 ); /* No collision on union u2 */ + } + } +} + +/* +** Append the contents of SrcList p2 to SrcList p1 and return the resulting +** SrcList. Or, if an error occurs, return NULL. In all cases, p1 and p2 +** are deleted by this function. +*/ +SrcList *sqlite3SrcListAppendList(Parse *pParse, SrcList *p1, SrcList *p2){ + assert( p1 ); + assert( p2 || pParse->nErr ); + assert( p2==0 || p2->nSrc>=1 ); + testcase( p1->nSrc==0 ); + if( p2 ){ + int nOld = p1->nSrc; + SrcList *pNew = sqlite3SrcListEnlarge(pParse, p1, p2->nSrc, nOld); + if( pNew==0 ){ + sqlite3SrcListDelete(pParse->db, p2); + }else{ + p1 = pNew; + memcpy(&p1->a[nOld], p2->a, p2->nSrc*sizeof(SrcItem)); + assert( nOld==1 || (p2->a[0].fg.jointype & JT_LTORJ)==0 ); + assert( p1->nSrc>=1 ); + p1->a[0].fg.jointype |= (JT_LTORJ & p2->a[0].fg.jointype); + sqlite3DbFree(pParse->db, p2); } } + return p1; } /* @@ -3851,7 +5177,7 @@ void sqlite3SrcListIndexedBy(Parse *pParse, SrcList *p, Token *pIndexedBy){ */ void sqlite3SrcListFuncArgs(Parse *pParse, SrcList *p, ExprList *pList){ if( p ){ - struct SrcList_item *pItem = &p->a[p->nSrc-1]; + SrcItem *pItem = &p->a[p->nSrc-1]; assert( pItem->fg.notIndexed==0 ); assert( pItem->fg.isIndexedBy==0 ); assert( pItem->fg.isTabFunc==0 ); @@ -3876,14 +5202,34 @@ void sqlite3SrcListFuncArgs(Parse *pParse, SrcList *p, ExprList *pList){ ** The operator is "natural cross join". The A and B operands are stored ** in p->a[0] and p->a[1], respectively. The parser initially stores the ** operator with A. This routine shifts that operator over to B. +** +** Additional changes: +** +** * All tables to the left of the right-most RIGHT JOIN are tagged with +** JT_LTORJ (mnemonic: Left Table Of Right Join) so that the +** code generator can easily tell that the table is part of +** the left operand of at least one RIGHT JOIN. */ -void sqlite3SrcListShiftJoinType(SrcList *p){ - if( p ){ - int i; - for(i=p->nSrc-1; i>0; i--){ - p->a[i].fg.jointype = p->a[i-1].fg.jointype; - } +void sqlite3SrcListShiftJoinType(Parse *pParse, SrcList *p){ + (void)pParse; + if( p && p->nSrc>1 ){ + int i = p->nSrc-1; + u8 allFlags = 0; + do{ + allFlags |= p->a[i].fg.jointype = p->a[i-1].fg.jointype; + }while( (--i)>0 ); p->a[0].fg.jointype = 0; + + /* All terms to the left of a RIGHT JOIN should be tagged with the + ** JT_LTORJ flags */ + if( allFlags & JT_RIGHT ){ + for(i=p->nSrc-1; ALWAYS(i>0) && (p->a[i].fg.jointype&JT_RIGHT)==0; i--){} + i--; + assert( i>=0 ); + do{ + p->a[i].fg.jointype |= JT_LTORJ; + }while( (--i)>=0 ); + } } } @@ -3905,7 +5251,16 @@ void sqlite3BeginTransaction(Parse *pParse, int type){ if( !v ) return; if( type!=TK_DEFERRED ){ for(i=0; inDb; i++){ - sqlite3VdbeAddOp2(v, OP_Transaction, i, (type==TK_EXCLUSIVE)+1); + int eTxnType; + Btree *pBt = db->aDb[i].pBt; + if( pBt && sqlite3BtreeIsReadonly(pBt) ){ + eTxnType = 0; /* Read txn */ + }else if( type==TK_EXCLUSIVE ){ + eTxnType = 2; /* Exclusive txn */ + }else{ + eTxnType = 1; /* Write txn */ + } + sqlite3VdbeAddOp2(v, OP_Transaction, i, eTxnType); sqlite3VdbeUsesBtree(v, i); } } @@ -3913,42 +5268,31 @@ void sqlite3BeginTransaction(Parse *pParse, int type){ } /* -** Generate VDBE code for a COMMIT statement. +** Generate VDBE code for a COMMIT or ROLLBACK statement. +** Code for ROLLBACK is generated if eType==TK_ROLLBACK. Otherwise +** code is generated for a COMMIT. */ -void sqlite3CommitTransaction(Parse *pParse){ +void sqlite3EndTransaction(Parse *pParse, int eType){ Vdbe *v; + int isRollback; assert( pParse!=0 ); assert( pParse->db!=0 ); - if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ){ + assert( eType==TK_COMMIT || eType==TK_END || eType==TK_ROLLBACK ); + isRollback = eType==TK_ROLLBACK; + if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, + isRollback ? "ROLLBACK" : "COMMIT", 0, 0) ){ return; } v = sqlite3GetVdbe(pParse); if( v ){ - sqlite3VdbeAddOp1(v, OP_AutoCommit, 1); - } -} - -/* -** Generate VDBE code for a ROLLBACK statement. -*/ -void sqlite3RollbackTransaction(Parse *pParse){ - Vdbe *v; - - assert( pParse!=0 ); - assert( pParse->db!=0 ); - if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ){ - return; - } - v = sqlite3GetVdbe(pParse); - if( v ){ - sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, 1); + sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, isRollback); } } /* ** This function is called by the parser when it parses a command to create, -** release or rollback an SQL savepoint. +** release or rollback an SQL savepoint. */ void sqlite3Savepoint(Parse *pParse, int op, Token *pName){ char *zName = sqlite3NameFromToken(pParse->db, pName); @@ -3975,7 +5319,7 @@ int sqlite3OpenTempDatabase(Parse *pParse){ if( db->aDb[1].pBt==0 && !pParse->explain ){ int rc; Btree *pBt; - static const int flags = + static const int flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_EXCLUSIVE | @@ -3991,7 +5335,7 @@ int sqlite3OpenTempDatabase(Parse *pParse){ } db->aDb[1].pBt = pBt; assert( db->aDb[1].pSchema ); - if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){ + if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, 0, 0) ){ sqlite3OomFault(db); return 1; } @@ -4005,25 +5349,25 @@ int sqlite3OpenTempDatabase(Parse *pParse){ ** will occur at the end of the top-level VDBE and will be generated ** later, by sqlite3FinishCoding(). */ -void sqlite3CodeVerifySchema(Parse *pParse, int iDb){ - Parse *pToplevel = sqlite3ParseToplevel(pParse); - sqlite3 *db = pToplevel->db; - - assert( iDb>=0 && iDbnDb ); - assert( db->aDb[iDb].pBt!=0 || iDb==1 ); - assert( iDb=0 && iDbdb->nDb ); + assert( pToplevel->db->aDb[iDb].pBt!=0 || iDb==1 ); + assert( iDbdb, iDb, 0) ); if( DbMaskTest(pToplevel->cookieMask, iDb)==0 ){ DbMaskSet(pToplevel->cookieMask, iDb); - pToplevel->cookieValue[iDb] = db->aDb[iDb].pSchema->schema_cookie; if( !OMIT_TEMPDB && iDb==1 ){ sqlite3OpenTempDatabase(pToplevel); } } } +void sqlite3CodeVerifySchema(Parse *pParse, int iDb){ + sqlite3CodeVerifySchemaAtToplevel(sqlite3ParseToplevel(pParse), iDb); +} + /* -** If argument zDb is NULL, then call sqlite3CodeVerifySchema() for each +** If argument zDb is NULL, then call sqlite3CodeVerifySchema() for each ** attached database. Otherwise, invoke it for the database named zDb only. */ void sqlite3CodeVerifyNamedSchema(Parse *pParse, const char *zDb){ @@ -4031,7 +5375,7 @@ void sqlite3CodeVerifyNamedSchema(Parse *pParse, const char *zDb){ int i; for(i=0; inDb; i++){ Db *pDb = &db->aDb[i]; - if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zName)) ){ + if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zDbSName)) ){ sqlite3CodeVerifySchema(pParse, i); } } @@ -4052,7 +5396,7 @@ void sqlite3CodeVerifyNamedSchema(Parse *pParse, const char *zDb){ */ void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){ Parse *pToplevel = sqlite3ParseToplevel(pParse); - sqlite3CodeVerifySchema(pParse, iDb); + sqlite3CodeVerifySchemaAtToplevel(pToplevel, iDb); DbMaskSet(pToplevel->writeMask, iDb); pToplevel->isMultiWrite |= setStatement; } @@ -4069,9 +5413,9 @@ void sqlite3MultiWrite(Parse *pParse){ pToplevel->isMultiWrite = 1; } -/* +/* ** The code generator calls this routine if is discovers that it is -** possible to abort a statement prior to completion. In order to +** possible to abort a statement prior to completion. In order to ** perform this abort without corrupting the database, we need to make ** sure that the statement is protected by a statement transaction. ** @@ -4080,7 +5424,7 @@ void sqlite3MultiWrite(Parse *pParse){ ** such that the abort must occur after the multiwrite. This makes ** some statements involving the REPLACE conflict resolution algorithm ** go a little faster. But taking advantage of this time dependency -** makes it more difficult to prove that the code is correct (in +** makes it more difficult to prove that the code is correct (in ** particular, it prevents us from writing an effective ** implementation of sqlite3AssertMayAbort()) and so we have chosen ** to take the safe route and skip the optimization. @@ -4103,8 +5447,10 @@ void sqlite3HaltConstraint( i8 p4type, /* P4_STATIC or P4_TRANSIENT */ u8 p5Errmsg /* P5_ErrMsg type */ ){ - Vdbe *v = sqlite3GetVdbe(pParse); - assert( (errCode&0xff)==SQLITE_CONSTRAINT ); + Vdbe *v; + assert( pParse->pVdbe!=0 ); + v = sqlite3GetVdbe(pParse); + assert( (errCode&0xff)==SQLITE_CONSTRAINT || pParse->nested ); if( onError==OE_Abort ){ sqlite3MayAbort(pParse); } @@ -4125,21 +5471,24 @@ void sqlite3UniqueConstraint( StrAccum errMsg; Table *pTab = pIdx->pTable; - sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0, 200); + sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0, + pParse->db->aLimit[SQLITE_LIMIT_LENGTH]); if( pIdx->aColExpr ){ - sqlite3XPrintf(&errMsg, "index '%q'", pIdx->zName); + sqlite3_str_appendf(&errMsg, "index '%q'", pIdx->zName); }else{ for(j=0; jnKeyCol; j++){ char *zCol; assert( pIdx->aiColumn[j]>=0 ); - zCol = pTab->aCol[pIdx->aiColumn[j]].zName; - if( j ) sqlite3StrAccumAppend(&errMsg, ", ", 2); - sqlite3XPrintf(&errMsg, "%s.%s", pTab->zName, zCol); + zCol = pTab->aCol[pIdx->aiColumn[j]].zCnName; + if( j ) sqlite3_str_append(&errMsg, ", ", 2); + sqlite3_str_appendall(&errMsg, pTab->zName); + sqlite3_str_append(&errMsg, ".", 1); + sqlite3_str_appendall(&errMsg, zCol); } } zErr = sqlite3StrAccumFinish(&errMsg); - sqlite3HaltConstraint(pParse, - IsPrimaryKeyIndex(pIdx) ? SQLITE_CONSTRAINT_PRIMARYKEY + sqlite3HaltConstraint(pParse, + IsPrimaryKeyIndex(pIdx) ? SQLITE_CONSTRAINT_PRIMARYKEY : SQLITE_CONSTRAINT_UNIQUE, onError, zErr, P4_DYNAMIC, P5_ConstraintUnique); } @@ -4151,13 +5500,13 @@ void sqlite3UniqueConstraint( void sqlite3RowidConstraint( Parse *pParse, /* Parsing context */ int onError, /* Conflict resolution algorithm */ - Table *pTab /* The table with the non-unique rowid */ + Table *pTab /* The table with the non-unique rowid */ ){ char *zMsg; int rc; if( pTab->iPKey>=0 ){ zMsg = sqlite3MPrintf(pParse->db, "%s.%s", pTab->zName, - pTab->aCol[pTab->iPKey].zName); + pTab->aCol[pTab->iPKey].zCnName); rc = SQLITE_CONSTRAINT_PRIMARYKEY; }else{ zMsg = sqlite3MPrintf(pParse->db, "%s.rowid", pTab->zName); @@ -4192,13 +5541,15 @@ static int collationMatch(const char *zColl, Index *pIndex){ */ #ifndef SQLITE_OMIT_REINDEX static void reindexTable(Parse *pParse, Table *pTab, char const *zColl){ - Index *pIndex; /* An index associated with pTab */ - - for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){ - if( zColl==0 || collationMatch(zColl, pIndex) ){ - int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); - sqlite3BeginWriteOperation(pParse, 0, iDb); - sqlite3RefillIndex(pParse, pIndex, -1); + if( !IsVirtual(pTab) ){ + Index *pIndex; /* An index associated with pTab */ + + for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){ + if( zColl==0 || collationMatch(zColl, pIndex) ){ + int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); + sqlite3BeginWriteOperation(pParse, 0, iDb); + sqlite3RefillIndex(pParse, pIndex, -1); + } } } } @@ -4278,7 +5629,7 @@ void sqlite3Reindex(Parse *pParse, Token *pName1, Token *pName2){ if( iDb<0 ) return; z = sqlite3NameFromToken(db, pObjName); if( z==0 ) return; - zDb = db->aDb[iDb].zName; + zDb = pName2->n ? db->aDb[iDb].zDbSName : 0; pTab = sqlite3FindTable(db, z, zDb); if( pTab ){ reindexTable(pParse, pTab, 0); @@ -4288,6 +5639,7 @@ void sqlite3Reindex(Parse *pParse, Token *pName1, Token *pName2){ pIndex = sqlite3FindIndex(db, z, zDb); sqlite3DbFree(db, z); if( pIndex ){ + iDb = sqlite3SchemaToIndex(db, pIndex->pTable->pSchema); sqlite3BeginWriteOperation(pParse, 0, iDb); sqlite3RefillIndex(pParse, pIndex, -1); return; @@ -4299,10 +5651,6 @@ void sqlite3Reindex(Parse *pParse, Token *pName1, Token *pName2){ /* ** Return a KeyInfo structure that is appropriate for the given Index. ** -** The KeyInfo structure for an index is cached in the Index object. -** So there might be multiple references to the returned pointer. The -** caller should not try to modify the KeyInfo object. -** ** The caller should invoke sqlite3KeyInfoUnref() on the returned object ** when it has finished using it. */ @@ -4323,9 +5671,27 @@ KeyInfo *sqlite3KeyInfoOfIndex(Parse *pParse, Index *pIdx){ const char *zColl = pIdx->azColl[i]; pKey->aColl[i] = zColl==sqlite3StrBINARY ? 0 : sqlite3LocateCollSeq(pParse, zColl); - pKey->aSortOrder[i] = pIdx->aSortOrder[i]; + pKey->aSortFlags[i] = pIdx->aSortOrder[i]; + assert( 0==(pKey->aSortFlags[i] & KEYINFO_ORDER_BIGNULL) ); } if( pParse->nErr ){ + assert( pParse->rc==SQLITE_ERROR_MISSING_COLLSEQ ); + if( pIdx->bNoQuery==0 + && sqlite3HashFind(&pIdx->pSchema->idxHash, pIdx->zName) + ){ + /* Deactivate the index because it contains an unknown collating + ** sequence. The only way to reactive the index is to reload the + ** schema. Adding the missing collating sequence later does not + ** reactive the index. The application had the chance to register + ** the missing index using the collation-needed callback. For + ** simplicity, SQLite will not give the application a second chance. + ** + ** Except, do not do this if the index is not in the schema hash + ** table. In this case the index is currently being constructed + ** by a CREATE INDEX statement, and retrying will not help. */ + pIdx->bNoQuery = 1; + pParse->rc = SQLITE_ERROR_RETRY; + } sqlite3KeyInfoUnref(pKey); pKey = 0; } @@ -4334,24 +5700,76 @@ KeyInfo *sqlite3KeyInfoOfIndex(Parse *pParse, Index *pIdx){ } #ifndef SQLITE_OMIT_CTE -/* -** This routine is invoked once per CTE by the parser while parsing a -** WITH clause. +/* +** Create a new CTE object */ -With *sqlite3WithAdd( +Cte *sqlite3CteNew( Parse *pParse, /* Parsing context */ - With *pWith, /* Existing WITH clause, or NULL */ Token *pName, /* Name of the common-table */ ExprList *pArglist, /* Optional column name list for the table */ - Select *pQuery /* Query used to initialize the table */ + Select *pQuery, /* Query used to initialize the table */ + u8 eM10d /* The MATERIALIZED flag */ +){ + Cte *pNew; + sqlite3 *db = pParse->db; + + pNew = sqlite3DbMallocZero(db, sizeof(*pNew)); + assert( pNew!=0 || db->mallocFailed ); + + if( db->mallocFailed ){ + sqlite3ExprListDelete(db, pArglist); + sqlite3SelectDelete(db, pQuery); + }else{ + pNew->pSelect = pQuery; + pNew->pCols = pArglist; + pNew->zName = sqlite3NameFromToken(pParse->db, pName); + pNew->eM10d = eM10d; + } + return pNew; +} + +/* +** Clear information from a Cte object, but do not deallocate storage +** for the object itself. +*/ +static void cteClear(sqlite3 *db, Cte *pCte){ + assert( pCte!=0 ); + sqlite3ExprListDelete(db, pCte->pCols); + sqlite3SelectDelete(db, pCte->pSelect); + sqlite3DbFree(db, pCte->zName); +} + +/* +** Free the contents of the CTE object passed as the second argument. +*/ +void sqlite3CteDelete(sqlite3 *db, Cte *pCte){ + assert( pCte!=0 ); + cteClear(db, pCte); + sqlite3DbFree(db, pCte); +} + +/* +** This routine is invoked once per CTE by the parser while parsing a +** WITH clause. The CTE described by the third argument is added to +** the WITH clause of the second argument. If the second argument is +** NULL, then a new WITH argument is created. +*/ +With *sqlite3WithAdd( + Parse *pParse, /* Parsing context */ + With *pWith, /* Existing WITH clause, or NULL */ + Cte *pCte /* CTE to add to the WITH clause */ ){ sqlite3 *db = pParse->db; With *pNew; char *zName; + if( pCte==0 ){ + return pWith; + } + /* Check that the CTE name is unique within this WITH clause. If ** not, store an error in the Parse structure. */ - zName = sqlite3NameFromToken(pParse->db, pName); + zName = pCte->zName; if( zName && pWith ){ int i; for(i=0; inCte; i++){ @@ -4362,24 +5780,18 @@ With *sqlite3WithAdd( } if( pWith ){ - int nByte = sizeof(*pWith) + (sizeof(pWith->a[1]) * pWith->nCte); - pNew = sqlite3DbRealloc(db, pWith, nByte); + pNew = sqlite3DbRealloc(db, pWith, SZ_WITH(pWith->nCte+1)); }else{ - pNew = sqlite3DbMallocZero(db, sizeof(*pWith)); + pNew = sqlite3DbMallocZero(db, SZ_WITH(1)); } assert( (pNew!=0 && zName!=0) || db->mallocFailed ); if( db->mallocFailed ){ - sqlite3ExprListDelete(db, pArglist); - sqlite3SelectDelete(db, pQuery); - sqlite3DbFree(db, zName); + sqlite3CteDelete(db, pCte); pNew = pWith; }else{ - pNew->a[pNew->nCte].pSelect = pQuery; - pNew->a[pNew->nCte].pCols = pArglist; - pNew->a[pNew->nCte].zName = zName; - pNew->a[pNew->nCte].zCteErr = 0; - pNew->nCte++; + pNew->a[pNew->nCte++] = *pCte; + sqlite3DbFree(db, pCte); } return pNew; @@ -4392,12 +5804,12 @@ void sqlite3WithDelete(sqlite3 *db, With *pWith){ if( pWith ){ int i; for(i=0; inCte; i++){ - struct Cte *pCte = &pWith->a[i]; - sqlite3ExprListDelete(db, pCte->pCols); - sqlite3SelectDelete(db, pCte->pSelect); - sqlite3DbFree(db, pCte->zName); + cteClear(db, &pWith->a[i]); } sqlite3DbFree(db, pWith); } } +void sqlite3WithDeleteGeneric(sqlite3 *db, void *pWith){ + sqlite3WithDelete(db, (With*)pWith); +} #endif /* !defined(SQLITE_OMIT_CTE) */ diff --git a/src/callback.c b/src/callback.c index 2b955fdcdb..e6418097f6 100644 --- a/src/callback.c +++ b/src/callback.c @@ -65,50 +65,6 @@ static int synthCollSeq(sqlite3 *db, CollSeq *pColl){ return SQLITE_ERROR; } -/* -** This function is responsible for invoking the collation factory callback -** or substituting a collation sequence of a different encoding when the -** requested collation sequence is not available in the desired encoding. -** -** If it is not NULL, then pColl must point to the database native encoding -** collation sequence with name zName, length nName. -** -** The return value is either the collation sequence to be used in database -** db for collation type name zName, length nName, or NULL, if no collation -** sequence can be found. If no collation is found, leave an error message. -** -** See also: sqlite3LocateCollSeq(), sqlite3FindCollSeq() -*/ -CollSeq *sqlite3GetCollSeq( - Parse *pParse, /* Parsing context */ - u8 enc, /* The desired encoding for the collating sequence */ - CollSeq *pColl, /* Collating sequence with native encoding, or NULL */ - const char *zName /* Collating sequence name */ -){ - CollSeq *p; - sqlite3 *db = pParse->db; - - p = pColl; - if( !p ){ - p = sqlite3FindCollSeq(db, enc, zName, 0); - } - if( !p || !p->xCmp ){ - /* No collation sequence of this type for this encoding is registered. - ** Call the collation factory to see if it can supply us with one. - */ - callCollNeeded(db, enc, zName); - p = sqlite3FindCollSeq(db, enc, zName, 0); - } - if( p && !p->xCmp && synthCollSeq(db, p) ){ - p = 0; - } - assert( !p || p->xCmp ); - if( p==0 ){ - sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName); - } - return p; -} - /* ** This routine is called on a collation sequence before it is used to ** check that it is defined. An undefined collation sequence exists when @@ -121,7 +77,7 @@ CollSeq *sqlite3GetCollSeq( ** from the main database is substituted, if one is available. */ int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){ - if( pColl ){ + if( pColl && pColl->xCmp==0 ){ const char *zName = pColl->zName; sqlite3 *db = pParse->db; CollSeq *p = sqlite3GetCollSeq(pParse, ENC(db), pColl, zName); @@ -157,8 +113,8 @@ static CollSeq *findCollSeqEntry( pColl = sqlite3HashFind(&db->aCollSeq, zName); if( 0==pColl && create ){ - int nName = sqlite3Strlen30(zName); - pColl = sqlite3DbMallocZero(db, 3*sizeof(*pColl) + nName + 1); + int nName = sqlite3Strlen30(zName) + 1; + pColl = sqlite3DbMallocZero(db, 3*sizeof(*pColl) + nName); if( pColl ){ CollSeq *pDel = 0; pColl[0].zName = (char*)&pColl[3]; @@ -168,7 +124,6 @@ static CollSeq *findCollSeqEntry( pColl[2].zName = (char*)&pColl[3]; pColl[2].enc = SQLITE_UTF16BE; memcpy(pColl[0].zName, zName, nName); - pColl[0].zName[nName] = 0; pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, pColl); /* If a malloc() failure occurred in sqlite3HashInsert(), it will @@ -202,20 +157,113 @@ static CollSeq *findCollSeqEntry( ** See also: sqlite3LocateCollSeq(), sqlite3GetCollSeq() */ CollSeq *sqlite3FindCollSeq( - sqlite3 *db, - u8 enc, - const char *zName, - int create + sqlite3 *db, /* Database connection to search */ + u8 enc, /* Desired text encoding */ + const char *zName, /* Name of the collating sequence. Might be NULL */ + int create /* True to create CollSeq if doesn't already exist */ ){ CollSeq *pColl; + assert( SQLITE_UTF8==1 && SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 ); + assert( enc>=SQLITE_UTF8 && enc<=SQLITE_UTF16BE ); if( zName ){ pColl = findCollSeqEntry(db, zName, create); + if( pColl ) pColl += enc-1; }else{ pColl = db->pDfltColl; } - assert( SQLITE_UTF8==1 && SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 ); - assert( enc>=SQLITE_UTF8 && enc<=SQLITE_UTF16BE ); - if( pColl ) pColl += enc-1; + return pColl; +} + +/* +** Change the text encoding for a database connection. This means that +** the pDfltColl must change as well. +*/ +void sqlite3SetTextEncoding(sqlite3 *db, u8 enc){ + assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE ); + db->enc = enc; + /* EVIDENCE-OF: R-08308-17224 The default collating function for all + ** strings is BINARY. + */ + db->pDfltColl = sqlite3FindCollSeq(db, enc, sqlite3StrBINARY, 0); + sqlite3ExpirePreparedStatements(db, 1); +} + +/* +** This function is responsible for invoking the collation factory callback +** or substituting a collation sequence of a different encoding when the +** requested collation sequence is not available in the desired encoding. +** +** If it is not NULL, then pColl must point to the database native encoding +** collation sequence with name zName, length nName. +** +** The return value is either the collation sequence to be used in database +** db for collation type name zName, length nName, or NULL, if no collation +** sequence can be found. If no collation is found, leave an error message. +** +** See also: sqlite3LocateCollSeq(), sqlite3FindCollSeq() +*/ +CollSeq *sqlite3GetCollSeq( + Parse *pParse, /* Parsing context */ + u8 enc, /* The desired encoding for the collating sequence */ + CollSeq *pColl, /* Collating sequence with native encoding, or NULL */ + const char *zName /* Collating sequence name */ +){ + CollSeq *p; + sqlite3 *db = pParse->db; + + p = pColl; + if( !p ){ + p = sqlite3FindCollSeq(db, enc, zName, 0); + } + if( !p || !p->xCmp ){ + /* No collation sequence of this type for this encoding is registered. + ** Call the collation factory to see if it can supply us with one. + */ + callCollNeeded(db, enc, zName); + p = sqlite3FindCollSeq(db, enc, zName, 0); + } + if( p && !p->xCmp && synthCollSeq(db, p) ){ + p = 0; + } + assert( !p || p->xCmp ); + if( p==0 ){ + sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName); + pParse->rc = SQLITE_ERROR_MISSING_COLLSEQ; + } + return p; +} + +/* +** This function returns the collation sequence for database native text +** encoding identified by the string zName. +** +** If the requested collation sequence is not available, or not available +** in the database native encoding, the collation factory is invoked to +** request it. If the collation factory does not supply such a sequence, +** and the sequence is available in another text encoding, then that is +** returned instead. +** +** If no versions of the requested collations sequence are available, or +** another error occurs, NULL is returned and an error message written into +** pParse. +** +** This routine is a wrapper around sqlite3FindCollSeq(). This routine +** invokes the collation factory if the named collation cannot be found +** and generates an error message. +** +** See also: sqlite3FindCollSeq(), sqlite3GetCollSeq() +*/ +CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName){ + sqlite3 *db = pParse->db; + u8 enc = ENC(db); + u8 initbusy = db->init.busy; + CollSeq *pColl; + + pColl = sqlite3FindCollSeq(db, enc, zName, initbusy); + if( !initbusy && (!pColl || !pColl->xCmp) ){ + pColl = sqlite3GetCollSeq(pParse, enc, pColl, zName); + } + return pColl; } @@ -254,12 +302,19 @@ static int matchQuality( u8 enc /* Desired text encoding */ ){ int match; - - /* nArg of -2 is a special case */ - if( nArg==(-2) ) return (p->xSFunc==0) ? 0 : FUNC_PERFECT_MATCH; + assert( p->nArg>=(-4) && p->nArg!=(-2) ); + assert( nArg>=(-2) ); /* Wrong number of arguments means "no match" */ - if( p->nArg!=nArg && p->nArg>=0 ) return 0; + if( p->nArg!=nArg ){ + if( nArg==(-2) ) return p->xSFunc==0 ? 0 : FUNC_PERFECT_MATCH; + if( p->nArg>=0 ) return 0; + /* Special p->nArg values available to built-in functions only: + ** -3 1 or more arguments required + ** -4 2 or more arguments required + */ + if( p->nArg<(-2) && nArg<(-2-p->nArg) ) return 0; + } /* Give a better score to a function with a specific number of arguments ** than to function that accepts any number of arguments. */ @@ -283,15 +338,14 @@ static int matchQuality( ** Search a FuncDefHash for a function with the given name. Return ** a pointer to the matching FuncDef if found, or 0 if there is no match. */ -static FuncDef *functionSearch( - FuncDefHash *pHash, /* Hash table to search */ +FuncDef *sqlite3FunctionSearch( int h, /* Hash of the name */ - const char *zFunc, /* Name of function */ - int nFunc /* Number of bytes in zFunc */ + const char *zFunc /* Name of function */ ){ FuncDef *p; - for(p=pHash->a[h]; p; p=p->pHash){ - if( sqlite3StrNICmp(p->zName, zFunc, nFunc)==0 && p->zName[nFunc]==0 ){ + for(p=sqlite3BuiltinFunctions.a[h]; p; p=p->u.pHash){ + assert( p->funcFlags & SQLITE_FUNC_BUILTIN ); + if( sqlite3StrICmp(p->zName, zFunc)==0 ){ return p; } } @@ -301,23 +355,27 @@ static FuncDef *functionSearch( /* ** Insert a new FuncDef into a FuncDefHash hash table. */ -void sqlite3FuncDefInsert( - FuncDefHash *pHash, /* The hash table into which to insert */ - FuncDef *pDef /* The function definition to insert */ +void sqlite3InsertBuiltinFuncs( + FuncDef *aDef, /* List of global functions to be inserted */ + int nDef /* Length of the apDef[] list */ ){ - FuncDef *pOther; - int nName = sqlite3Strlen30(pDef->zName); - u8 c1 = (u8)pDef->zName[0]; - int h = (sqlite3UpperToLower[c1] + nName) % ArraySize(pHash->a); - pOther = functionSearch(pHash, h, pDef->zName, nName); - if( pOther ){ - assert( pOther!=pDef && pOther->pNext!=pDef ); - pDef->pNext = pOther->pNext; - pOther->pNext = pDef; - }else{ - pDef->pNext = 0; - pDef->pHash = pHash->a[h]; - pHash->a[h] = pDef; + int i; + for(i=0; ipNext!=&aDef[i] ); + aDef[i].pNext = pOther->pNext; + pOther->pNext = &aDef[i]; + }else{ + aDef[i].pNext = 0; + aDef[i].u.pHash = sqlite3BuiltinFunctions.a[h]; + sqlite3BuiltinFunctions.a[h] = &aDef[i]; + } } } @@ -344,8 +402,7 @@ void sqlite3FuncDefInsert( */ FuncDef *sqlite3FindFunction( sqlite3 *db, /* An open database */ - const char *zName, /* Name of the function. Not null-terminated */ - int nName, /* Number of characters in the name */ + const char *zName, /* Name of the function. zero-terminated */ int nArg, /* Number of arguments. -1 means any number */ u8 enc, /* Preferred text encoding */ u8 createFlag /* Create new entry if true and does not otherwise exist */ @@ -354,14 +411,15 @@ FuncDef *sqlite3FindFunction( FuncDef *pBest = 0; /* Best match found so far */ int bestScore = 0; /* Score of best match */ int h; /* Hash value */ + int nName; /* Length of the name */ assert( nArg>=(-2) ); assert( nArg>=(-1) || createFlag==0 ); - h = (sqlite3UpperToLower[(u8)zName[0]] + nName) % ArraySize(db->aFunc.a); + nName = sqlite3Strlen30(zName); /* First search for a match amongst the application-defined functions. */ - p = functionSearch(&db->aFunc, h, zName, nName); + p = (FuncDef*)sqlite3HashFind(&db->aFunc, zName); while( p ){ int score = matchQuality(p, nArg, enc); if( score>bestScore ){ @@ -373,7 +431,7 @@ FuncDef *sqlite3FindFunction( /* If no match is found, search the built-in functions. ** - ** If the SQLITE_PreferBuiltin flag is set, then search the built-in + ** If the DBFLAG_PreferBuiltin flag is set, then search the built-in ** functions even if a prior app-defined function was found. And give ** priority to built-in functions. ** @@ -383,10 +441,10 @@ FuncDef *sqlite3FindFunction( ** new function. But the FuncDefs for built-in functions are read-only. ** So we must not search for built-ins when creating a new function. */ - if( !createFlag && (pBest==0 || (db->flags & SQLITE_PreferBuiltin)!=0) ){ - FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions); + if( !createFlag && (pBest==0 || (db->mDbFlags & DBFLAG_PreferBuiltin)!=0) ){ bestScore = 0; - p = functionSearch(pHash, h, zName, nName); + h = SQLITE_FUNC_HASH(sqlite3UpperToLower[(u8)zName[0]], nName); + p = sqlite3FunctionSearch(h, zName); while( p ){ int score = matchQuality(p, nArg, enc); if( score>bestScore ){ @@ -403,12 +461,21 @@ FuncDef *sqlite3FindFunction( */ if( createFlag && bestScorezName = (char *)&pBest[1]; + FuncDef *pOther; + u8 *z; + pBest->zName = (const char*)&pBest[1]; pBest->nArg = (u16)nArg; pBest->funcFlags = enc; - memcpy(pBest->zName, zName, nName); - pBest->zName[nName] = 0; - sqlite3FuncDefInsert(&db->aFunc, pBest); + memcpy((char*)&pBest[1], zName, nName+1); + for(z=(u8*)pBest->zName; *z; z++) *z = sqlite3UpperToLower[*z]; + pOther = (FuncDef*)sqlite3HashInsert(&db->aFunc, pBest->zName, pBest); + if( pOther==pBest ){ + sqlite3DbFree(db, pBest); + sqlite3OomFault(db); + return 0; + }else{ + pBest->pNext = pOther; + } } if( pBest && (pBest->xSFunc || createFlag) ){ @@ -430,27 +497,30 @@ void sqlite3SchemaClear(void *p){ Hash temp2; HashElem *pElem; Schema *pSchema = (Schema *)p; + sqlite3 xdb; + memset(&xdb, 0, sizeof(xdb)); temp1 = pSchema->tblHash; temp2 = pSchema->trigHash; sqlite3HashInit(&pSchema->trigHash); sqlite3HashClear(&pSchema->idxHash); for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){ - sqlite3DeleteTrigger(0, (Trigger*)sqliteHashData(pElem)); + sqlite3DeleteTrigger(&xdb, (Trigger*)sqliteHashData(pElem)); } + sqlite3HashClear(&temp2); sqlite3HashInit(&pSchema->tblHash); for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){ Table *pTab = sqliteHashData(pElem); - sqlite3DeleteTable(0, pTab); + sqlite3DeleteTable(&xdb, pTab); } sqlite3HashClear(&temp1); sqlite3HashClear(&pSchema->fkeyHash); pSchema->pSeqTab = 0; if( pSchema->schemaFlags & DB_SchemaLoaded ){ pSchema->iGeneration++; - pSchema->schemaFlags &= ~DB_SchemaLoaded; } + pSchema->schemaFlags &= ~(DB_SchemaLoaded|DB_ResetWanted); } /* diff --git a/src/carray.c b/src/carray.c new file mode 100644 index 0000000000..154d107ddf --- /dev/null +++ b/src/carray.c @@ -0,0 +1,527 @@ +/* +** 2016-06-29 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** This file implements a table-valued-function that +** returns the values in a C-language array. +** Examples: +** +** SELECT * FROM carray($ptr,5) +** +** The query above returns 5 integers contained in a C-language array +** at the address $ptr. $ptr is a pointer to the array of integers. +** The pointer value must be assigned to $ptr using the +** sqlite3_bind_pointer() interface with a pointer type of "carray". +** For example: +** +** static int aX[] = { 53, 9, 17, 2231, 4, 99 }; +** int i = sqlite3_bind_parameter_index(pStmt, "$ptr"); +** sqlite3_bind_pointer(pStmt, i, aX, "carray", 0); +** +** There is an optional third parameter to determine the datatype of +** the C-language array. Allowed values of the third parameter are +** 'int32', 'int64', 'double', 'char*', 'struct iovec'. Example: +** +** SELECT * FROM carray($ptr,10,'char*'); +** +** The default value of the third parameter is 'int32'. +** +** HOW IT WORKS +** +** The carray "function" is really a virtual table with the +** following schema: +** +** CREATE TABLE carray( +** value, +** pointer HIDDEN, +** count HIDDEN, +** ctype TEXT HIDDEN +** ); +** +** If the hidden columns "pointer" and "count" are unconstrained, then +** the virtual table has no rows. Otherwise, the virtual table interprets +** the integer value of "pointer" as a pointer to the array and "count" +** as the number of elements in the array. The virtual table steps through +** the array, element by element. +*/ +#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_CARRAY) +#include "sqliteInt.h" +#if defined(_WIN32) || defined(__RTP__) || defined(_WRS_KERNEL) + struct iovec { + void *iov_base; + size_t iov_len; + }; +#else +# include +#endif + +/* +** Names of allowed datatypes +*/ +static const char *azCarrayType[] = { + "int32", "int64", "double", "char*", "struct iovec" +}; + +/* +** Structure used to hold the sqlite3_carray_bind() information +*/ +typedef struct carray_bind carray_bind; +struct carray_bind { + void *aData; /* The data */ + int nData; /* Number of elements */ + int mFlags; /* Control flags */ + void (*xDel)(void*); /* Destructor for aData */ +}; + + +/* carray_cursor is a subclass of sqlite3_vtab_cursor which will +** serve as the underlying representation of a cursor that scans +** over rows of the result +*/ +typedef struct carray_cursor carray_cursor; +struct carray_cursor { + sqlite3_vtab_cursor base; /* Base class - must be first */ + sqlite3_int64 iRowid; /* The rowid */ + void *pPtr; /* Pointer to the array of values */ + sqlite3_int64 iCnt; /* Number of integers in the array */ + unsigned char eType; /* One of the CARRAY_type values */ +}; + +/* +** The carrayConnect() method is invoked to create a new +** carray_vtab that describes the carray virtual table. +** +** Think of this routine as the constructor for carray_vtab objects. +** +** All this routine needs to do is: +** +** (1) Allocate the carray_vtab object and initialize all fields. +** +** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the +** result set of queries against carray will look like. +*/ +static int carrayConnect( + sqlite3 *db, + void *pAux, + int argc, const char *const*argv, + sqlite3_vtab **ppVtab, + char **pzErr +){ + sqlite3_vtab *pNew; + int rc; + +/* Column numbers */ +#define CARRAY_COLUMN_VALUE 0 +#define CARRAY_COLUMN_POINTER 1 +#define CARRAY_COLUMN_COUNT 2 +#define CARRAY_COLUMN_CTYPE 3 + + rc = sqlite3_declare_vtab(db, + "CREATE TABLE x(value,pointer hidden,count hidden,ctype hidden)"); + if( rc==SQLITE_OK ){ + pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) ); + if( pNew==0 ) return SQLITE_NOMEM; + memset(pNew, 0, sizeof(*pNew)); + } + return rc; +} + +/* +** This method is the destructor for carray_cursor objects. +*/ +static int carrayDisconnect(sqlite3_vtab *pVtab){ + sqlite3_free(pVtab); + return SQLITE_OK; +} + +/* +** Constructor for a new carray_cursor object. +*/ +static int carrayOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){ + carray_cursor *pCur; + pCur = sqlite3_malloc( sizeof(*pCur) ); + if( pCur==0 ) return SQLITE_NOMEM; + memset(pCur, 0, sizeof(*pCur)); + *ppCursor = &pCur->base; + return SQLITE_OK; +} + +/* +** Destructor for a carray_cursor. +*/ +static int carrayClose(sqlite3_vtab_cursor *cur){ + sqlite3_free(cur); + return SQLITE_OK; +} + + +/* +** Advance a carray_cursor to its next row of output. +*/ +static int carrayNext(sqlite3_vtab_cursor *cur){ + carray_cursor *pCur = (carray_cursor*)cur; + pCur->iRowid++; + return SQLITE_OK; +} + +/* +** Return values of columns for the row at which the carray_cursor +** is currently pointing. +*/ +static int carrayColumn( + sqlite3_vtab_cursor *cur, /* The cursor */ + sqlite3_context *ctx, /* First argument to sqlite3_result_...() */ + int i /* Which column to return */ +){ + carray_cursor *pCur = (carray_cursor*)cur; + sqlite3_int64 x = 0; + switch( i ){ + case CARRAY_COLUMN_POINTER: return SQLITE_OK; + case CARRAY_COLUMN_COUNT: x = pCur->iCnt; break; + case CARRAY_COLUMN_CTYPE: { + sqlite3_result_text(ctx, azCarrayType[pCur->eType], -1, SQLITE_STATIC); + return SQLITE_OK; + } + default: { + switch( pCur->eType ){ + case CARRAY_INT32: { + int *p = (int*)pCur->pPtr; + sqlite3_result_int(ctx, p[pCur->iRowid-1]); + return SQLITE_OK; + } + case CARRAY_INT64: { + sqlite3_int64 *p = (sqlite3_int64*)pCur->pPtr; + sqlite3_result_int64(ctx, p[pCur->iRowid-1]); + return SQLITE_OK; + } + case CARRAY_DOUBLE: { + double *p = (double*)pCur->pPtr; + sqlite3_result_double(ctx, p[pCur->iRowid-1]); + return SQLITE_OK; + } + case CARRAY_TEXT: { + const char **p = (const char**)pCur->pPtr; + sqlite3_result_text(ctx, p[pCur->iRowid-1], -1, SQLITE_TRANSIENT); + return SQLITE_OK; + } + default: { + const struct iovec *p = (struct iovec*)pCur->pPtr; + assert( pCur->eType==CARRAY_BLOB ); + sqlite3_result_blob(ctx, p[pCur->iRowid-1].iov_base, + (int)p[pCur->iRowid-1].iov_len, SQLITE_TRANSIENT); + return SQLITE_OK; + } + } + } + } + sqlite3_result_int64(ctx, x); + return SQLITE_OK; +} + +/* +** Return the rowid for the current row. In this implementation, the +** rowid is the same as the output value. +*/ +static int carrayRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ + carray_cursor *pCur = (carray_cursor*)cur; + *pRowid = pCur->iRowid; + return SQLITE_OK; +} + +/* +** Return TRUE if the cursor has been moved off of the last +** row of output. +*/ +static int carrayEof(sqlite3_vtab_cursor *cur){ + carray_cursor *pCur = (carray_cursor*)cur; + return pCur->iRowid>pCur->iCnt; +} + +/* +** This method is called to "rewind" the carray_cursor object back +** to the first row of output. +*/ +static int carrayFilter( + sqlite3_vtab_cursor *pVtabCursor, + int idxNum, const char *idxStr, + int argc, sqlite3_value **argv +){ + carray_cursor *pCur = (carray_cursor *)pVtabCursor; + pCur->pPtr = 0; + pCur->iCnt = 0; + switch( idxNum ){ + case 1: { + carray_bind *pBind = sqlite3_value_pointer(argv[0], "carray-bind"); + if( pBind==0 ) break; + pCur->pPtr = pBind->aData; + pCur->iCnt = pBind->nData; + pCur->eType = pBind->mFlags & 0x07; + break; + } + case 2: + case 3: { + pCur->pPtr = sqlite3_value_pointer(argv[0], "carray"); + pCur->iCnt = pCur->pPtr ? sqlite3_value_int64(argv[1]) : 0; + if( idxNum<3 ){ + pCur->eType = CARRAY_INT32; + }else{ + unsigned char i; + const char *zType = (const char*)sqlite3_value_text(argv[2]); + for(i=0; i=sizeof(azCarrayType)/sizeof(azCarrayType[0]) ){ + pVtabCursor->pVtab->zErrMsg = sqlite3_mprintf( + "unknown datatype: %Q", zType); + return SQLITE_ERROR; + }else{ + pCur->eType = i; + } + } + break; + } + } + pCur->iRowid = 1; + return SQLITE_OK; +} + +/* +** SQLite will invoke this method one or more times while planning a query +** that uses the carray virtual table. This routine needs to create +** a query plan for each invocation and compute an estimated cost for that +** plan. +** +** In this implementation idxNum is used to represent the +** query plan. idxStr is unused. +** +** idxNum is: +** +** 1 If only the pointer= constraint exists. In this case, the +** parameter must be bound using sqlite3_carray_bind(). +** +** 2 if the pointer= and count= constraints exist. +** +** 3 if the ctype= constraint also exists. +** +** idxNum is 0 otherwise and carray becomes an empty table. +*/ +static int carrayBestIndex( + sqlite3_vtab *tab, + sqlite3_index_info *pIdxInfo +){ + int i; /* Loop over constraints */ + int ptrIdx = -1; /* Index of the pointer= constraint, or -1 if none */ + int cntIdx = -1; /* Index of the count= constraint, or -1 if none */ + int ctypeIdx = -1; /* Index of the ctype= constraint, or -1 if none */ + unsigned seen = 0; /* Bitmask of == constrainted columns */ + + const struct sqlite3_index_constraint *pConstraint; + pConstraint = pIdxInfo->aConstraint; + for(i=0; inConstraint; i++, pConstraint++){ + if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue; + if( pConstraint->iColumn>=0 ) seen |= 1 << pConstraint->iColumn; + if( pConstraint->usable==0 ) continue; + switch( pConstraint->iColumn ){ + case CARRAY_COLUMN_POINTER: + ptrIdx = i; + break; + case CARRAY_COLUMN_COUNT: + cntIdx = i; + break; + case CARRAY_COLUMN_CTYPE: + ctypeIdx = i; + break; + } + } + if( ptrIdx>=0 ){ + pIdxInfo->aConstraintUsage[ptrIdx].argvIndex = 1; + pIdxInfo->aConstraintUsage[ptrIdx].omit = 1; + pIdxInfo->estimatedCost = (double)1; + pIdxInfo->estimatedRows = 100; + pIdxInfo->idxNum = 1; + if( cntIdx>=0 ){ + pIdxInfo->aConstraintUsage[cntIdx].argvIndex = 2; + pIdxInfo->aConstraintUsage[cntIdx].omit = 1; + pIdxInfo->idxNum = 2; + if( ctypeIdx>=0 ){ + pIdxInfo->aConstraintUsage[ctypeIdx].argvIndex = 3; + pIdxInfo->aConstraintUsage[ctypeIdx].omit = 1; + pIdxInfo->idxNum = 3; + }else if( seen & (1<estimatedCost = (double)2147483647; + pIdxInfo->estimatedRows = 2147483647; + pIdxInfo->idxNum = 0; + } + return SQLITE_OK; +} + +/* +** This following structure defines all the methods for the +** carray virtual table. +*/ +static sqlite3_module carrayModule = { + 0, /* iVersion */ + 0, /* xCreate */ + carrayConnect, /* xConnect */ + carrayBestIndex, /* xBestIndex */ + carrayDisconnect, /* xDisconnect */ + 0, /* xDestroy */ + carrayOpen, /* xOpen - open a cursor */ + carrayClose, /* xClose - close a cursor */ + carrayFilter, /* xFilter - configure scan constraints */ + carrayNext, /* xNext - advance a cursor */ + carrayEof, /* xEof - check for end of scan */ + carrayColumn, /* xColumn - read data */ + carrayRowid, /* xRowid - read data */ + 0, /* xUpdate */ + 0, /* xBegin */ + 0, /* xSync */ + 0, /* xCommit */ + 0, /* xRollback */ + 0, /* xFindMethod */ + 0, /* xRename */ + 0, /* xSavepoint */ + 0, /* xRelease */ + 0, /* xRollbackTo */ + 0, /* xShadow */ + 0 /* xIntegrity */ +}; + +/* +** Destructor for the carray_bind object +*/ +static void carrayBindDel(void *pPtr){ + carray_bind *p = (carray_bind*)pPtr; + if( p->xDel!=SQLITE_STATIC ){ + p->xDel(p->aData); + } + sqlite3_free(p); +} + +/* +** Invoke this interface in order to bind to the single-argument +** version of CARRAY(). +*/ +SQLITE_API int sqlite3_carray_bind( + sqlite3_stmt *pStmt, + int idx, + void *aData, + int nData, + int mFlags, + void (*xDestroy)(void*) +){ + carray_bind *pNew = 0; + int i; + int rc = SQLITE_OK; + + /* Ensure that the mFlags value is acceptable. */ + assert( CARRAY_INT32==0 && CARRAY_INT64==1 && CARRAY_DOUBLE==2 ); + assert( CARRAY_TEXT==3 && CARRAY_BLOB==4 ); + if( mFlagsCARRAY_BLOB ){ + rc = SQLITE_ERROR; + goto carray_bind_error; + } + + pNew = sqlite3_malloc64(sizeof(*pNew)); + if( pNew==0 ){ + rc = SQLITE_NOMEM; + goto carray_bind_error; + } + + pNew->nData = nData; + pNew->mFlags = mFlags; + if( xDestroy==SQLITE_TRANSIENT ){ + sqlite3_int64 sz = nData; + switch( mFlags ){ + case CARRAY_INT32: sz *= 4; break; + case CARRAY_INT64: sz *= 8; break; + case CARRAY_DOUBLE: sz *= 8; break; + case CARRAY_TEXT: sz *= sizeof(char*); break; + default: sz *= sizeof(struct iovec); break; + } + if( mFlags==CARRAY_TEXT ){ + for(i=0; iaData = sqlite3_malloc64( sz ); + if( pNew->aData==0 ){ + rc = SQLITE_NOMEM; + goto carray_bind_error; + } + + if( mFlags==CARRAY_TEXT ){ + char **az = (char**)pNew->aData; + char *z = (char*)&az[nData]; + for(i=0; iaData; + unsigned char *z = (unsigned char*)&p[nData]; + for(i=0; iaData, aData, sz); + } + pNew->xDel = sqlite3_free; + }else{ + pNew->aData = aData; + pNew->xDel = xDestroy; + } + return sqlite3_bind_pointer(pStmt, idx, pNew, "carray-bind", carrayBindDel); + + carray_bind_error: + if( xDestroy!=SQLITE_STATIC && xDestroy!=SQLITE_TRANSIENT ){ + xDestroy(aData); + } + sqlite3_free(pNew); + return rc; +} + +/* +** Invoke this routine to register the carray() function. +*/ +Module *sqlite3CarrayRegister(sqlite3 *db){ + return sqlite3VtabCreateModule(db, "carray", &carrayModule, 0, 0); +} + +#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_CARRAY) */ diff --git a/src/complete.c b/src/complete.c index b120b7e811..bb2c03098e 100644 --- a/src/complete.c +++ b/src/complete.c @@ -281,7 +281,7 @@ int sqlite3_complete16(const void *zSql){ if( zSql8 ){ rc = sqlite3_complete(zSql8); }else{ - rc = SQLITE_NOMEM; + rc = SQLITE_NOMEM_BKPT; } sqlite3ValueFree(pVal); return rc & 0xff; diff --git a/src/crypto.c b/src/crypto.c deleted file mode 100644 index c8941f7ac1..0000000000 --- a/src/crypto.c +++ /dev/null @@ -1,722 +0,0 @@ -/* -** SQLCipher -** http://sqlcipher.net -** -** Copyright (c) 2008 - 2013, ZETETIC LLC -** All rights reserved. -** -** Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in the -** documentation and/or other materials provided with the distribution. -** * Neither the name of the ZETETIC LLC nor the -** names of its contributors may be used to endorse or promote products -** derived from this software without specific prior written permission. -** -** THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY -** EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -** DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY -** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -** -*/ -/* BEGIN SQLCIPHER */ -#ifdef SQLITE_HAS_CODEC - -#include -#include "sqliteInt.h" -#include "btreeInt.h" -#include "crypto.h" - -static const char* codec_get_cipher_version() { - return CIPHER_VERSION; -} - -/* Generate code to return a string value */ -static void codec_vdbe_return_static_string(Parse *pParse, const char *zLabel, const char *value){ - Vdbe *v = sqlite3GetVdbe(pParse); - sqlite3VdbeSetNumCols(v, 1); - sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLabel, SQLITE_STATIC); - sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, value, 0); - sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); -} - -static int codec_set_btree_to_codec_pagesize(sqlite3 *db, Db *pDb, codec_ctx *ctx) { - int rc, page_sz, reserve_sz; - - page_sz = sqlcipher_codec_ctx_get_pagesize(ctx); - reserve_sz = sqlcipher_codec_ctx_get_reservesize(ctx); - - sqlite3_mutex_enter(db->mutex); - db->nextPagesize = page_sz; - - /* before forcing the page size we need to unset the BTS_PAGESIZE_FIXED flag, else - sqliteBtreeSetPageSize will block the change */ - pDb->pBt->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED; - CODEC_TRACE(("codec_set_btree_to_codec_pagesize: sqlite3BtreeSetPageSize() size=%d reserve=%d\n", page_sz, reserve_sz)); - rc = sqlite3BtreeSetPageSize(pDb->pBt, page_sz, reserve_sz, 0); - sqlite3_mutex_leave(db->mutex); - return rc; -} - -static int codec_set_pass_key(sqlite3* db, int nDb, const void *zKey, int nKey, int for_ctx) { - struct Db *pDb = &db->aDb[nDb]; - CODEC_TRACE(("codec_set_pass_key: entered db=%p nDb=%d zKey=%s nKey=%d for_ctx=%d\n", db, nDb, (char *)zKey, nKey, for_ctx)); - if(pDb->pBt) { - codec_ctx *ctx; - sqlite3pager_get_codec(pDb->pBt->pBt->pPager, (void **) &ctx); - if(ctx) return sqlcipher_codec_ctx_set_pass(ctx, zKey, nKey, for_ctx); - } - return SQLITE_ERROR; -} - -int sqlcipher_codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLeft, const char *zRight) { - char *pragma_cipher_deprecated_msg = "PRAGMA cipher command is deprecated, please remove from usage."; - struct Db *pDb = &db->aDb[iDb]; - codec_ctx *ctx = NULL; - int rc; - - if(pDb->pBt) { - sqlite3pager_get_codec(pDb->pBt->pBt->pPager, (void **) &ctx); - } - - CODEC_TRACE(("sqlcipher_codec_pragma: entered db=%p iDb=%d pParse=%p zLeft=%s zRight=%s ctx=%p\n", db, iDb, pParse, zLeft, zRight, ctx)); - - if( sqlite3StrICmp(zLeft, "cipher_fips_status")== 0 && !zRight ){ - if(ctx) { - char *fips_mode_status = sqlite3_mprintf("%d", sqlcipher_codec_fips_status(ctx)); - codec_vdbe_return_static_string(pParse, "cipher_fips_status", fips_mode_status); - sqlite3_free(fips_mode_status); - } - } else - if( sqlite3StrICmp(zLeft, "cipher_store_pass")==0 && zRight ) { - if(ctx) { - sqlcipher_codec_set_store_pass(ctx, sqlite3GetBoolean(zRight, 1)); - } - } else - if( sqlite3StrICmp(zLeft, "cipher_store_pass")==0 && !zRight ) { - if(ctx){ - char *store_pass_value = sqlite3_mprintf("%d", sqlcipher_codec_get_store_pass(ctx)); - codec_vdbe_return_static_string(pParse, "cipher_store_pass", store_pass_value); - sqlite3_free(store_pass_value); - } - } - if( sqlite3StrICmp(zLeft, "cipher_profile")== 0 && zRight ){ - char *profile_status = sqlite3_mprintf("%d", sqlcipher_cipher_profile(db, zRight)); - codec_vdbe_return_static_string(pParse, "cipher_profile", profile_status); - sqlite3_free(profile_status); - } else - if( sqlite3StrICmp(zLeft, "cipher_add_random")==0 && zRight ){ - if(ctx) { - char *add_random_status = sqlite3_mprintf("%d", sqlcipher_codec_add_random(ctx, zRight, sqlite3Strlen30(zRight))); - codec_vdbe_return_static_string(pParse, "cipher_add_random", add_random_status); - sqlite3_free(add_random_status); - } - } else - if( sqlite3StrICmp(zLeft, "cipher_migrate")==0 && !zRight ){ - if(ctx){ - char *migrate_status = sqlite3_mprintf("%d", sqlcipher_codec_ctx_migrate(ctx)); - codec_vdbe_return_static_string(pParse, "cipher_migrate", migrate_status); - sqlite3_free(migrate_status); - } - } else - if( sqlite3StrICmp(zLeft, "cipher_provider")==0 && !zRight ){ - if(ctx) { codec_vdbe_return_static_string(pParse, "cipher_provider", - sqlcipher_codec_get_cipher_provider(ctx)); - } - } else - if( sqlite3StrICmp(zLeft, "cipher_provider_version")==0 && !zRight){ - if(ctx) { codec_vdbe_return_static_string(pParse, "cipher_provider_version", - sqlcipher_codec_get_provider_version(ctx)); - } - } else - if( sqlite3StrICmp(zLeft, "cipher_version")==0 && !zRight ){ - codec_vdbe_return_static_string(pParse, "cipher_version", codec_get_cipher_version()); - }else - if( sqlite3StrICmp(zLeft, "cipher")==0 ){ - if(ctx) { - if( zRight ) { - sqlcipher_codec_ctx_set_cipher(ctx, zRight, 2); // change cipher for both - codec_vdbe_return_static_string(pParse, "cipher", pragma_cipher_deprecated_msg); - sqlite3_log(SQLITE_WARNING, pragma_cipher_deprecated_msg); - return SQLITE_ERROR; - }else { - codec_vdbe_return_static_string(pParse, "cipher", - sqlcipher_codec_ctx_get_cipher(ctx, 2)); - } - } - }else - if( sqlite3StrICmp(zLeft, "rekey_cipher")==0 && zRight ){ - if(ctx) sqlcipher_codec_ctx_set_cipher(ctx, zRight, 1); // change write cipher only - }else - if( sqlite3StrICmp(zLeft,"cipher_default_kdf_iter")==0 ){ - if( zRight ) { - sqlcipher_set_default_kdf_iter(atoi(zRight)); // change default KDF iterations - } else { - char *kdf_iter = sqlite3_mprintf("%d", sqlcipher_get_default_kdf_iter()); - codec_vdbe_return_static_string(pParse, "cipher_default_kdf_iter", kdf_iter); - sqlite3_free(kdf_iter); - } - }else - if( sqlite3StrICmp(zLeft, "kdf_iter")==0 ){ - if(ctx) { - if( zRight ) { - sqlcipher_codec_ctx_set_kdf_iter(ctx, atoi(zRight), 2); // change of RW PBKDF2 iteration - } else { - char *kdf_iter = sqlite3_mprintf("%d", sqlcipher_codec_ctx_get_kdf_iter(ctx, 2)); - codec_vdbe_return_static_string(pParse, "kdf_iter", kdf_iter); - sqlite3_free(kdf_iter); - } - } - }else - if( sqlite3StrICmp(zLeft, "fast_kdf_iter")==0){ - if(ctx) { - if( zRight ) { - sqlcipher_codec_ctx_set_fast_kdf_iter(ctx, atoi(zRight), 2); // change of RW PBKDF2 iteration - } else { - char *fast_kdf_iter = sqlite3_mprintf("%d", sqlcipher_codec_ctx_get_fast_kdf_iter(ctx, 2)); - codec_vdbe_return_static_string(pParse, "fast_kdf_iter", fast_kdf_iter); - sqlite3_free(fast_kdf_iter); - } - } - }else - if( sqlite3StrICmp(zLeft, "rekey_kdf_iter")==0 && zRight ){ - if(ctx) sqlcipher_codec_ctx_set_kdf_iter(ctx, atoi(zRight), 1); // write iterations only - }else - if( sqlite3StrICmp(zLeft,"cipher_page_size")==0 ){ - if(ctx) { - if( zRight ) { - int size = atoi(zRight); - rc = sqlcipher_codec_ctx_set_pagesize(ctx, size); - if(rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, rc); - rc = codec_set_btree_to_codec_pagesize(db, pDb, ctx); - if(rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, rc); - } else { - char * page_size = sqlite3_mprintf("%d", sqlcipher_codec_ctx_get_pagesize(ctx)); - codec_vdbe_return_static_string(pParse, "cipher_page_size", page_size); - sqlite3_free(page_size); - } - } - }else - if( sqlite3StrICmp(zLeft,"cipher_default_page_size")==0 ){ - if( zRight ) { - sqlcipher_set_default_pagesize(atoi(zRight)); - } else { - char *default_page_size = sqlite3_mprintf("%d", sqlcipher_get_default_pagesize()); - codec_vdbe_return_static_string(pParse, "cipher_default_page_size", default_page_size); - sqlite3_free(default_page_size); - } - }else - if( sqlite3StrICmp(zLeft,"cipher_default_use_hmac")==0 ){ - if( zRight ) { - sqlcipher_set_default_use_hmac(sqlite3GetBoolean(zRight,1)); - } else { - char *default_use_hmac = sqlite3_mprintf("%d", sqlcipher_get_default_use_hmac()); - codec_vdbe_return_static_string(pParse, "cipher_default_use_hmac", default_use_hmac); - sqlite3_free(default_use_hmac); - } - }else - if( sqlite3StrICmp(zLeft,"cipher_use_hmac")==0 ){ - if(ctx) { - if( zRight ) { - rc = sqlcipher_codec_ctx_set_use_hmac(ctx, sqlite3GetBoolean(zRight,1)); - if(rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, rc); - /* since the use of hmac has changed, the page size may also change */ - rc = codec_set_btree_to_codec_pagesize(db, pDb, ctx); - if(rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, rc); - } else { - char *hmac_flag = sqlite3_mprintf("%d", sqlcipher_codec_ctx_get_use_hmac(ctx, 2)); - codec_vdbe_return_static_string(pParse, "cipher_use_hmac", hmac_flag); - sqlite3_free(hmac_flag); - } - } - }else - if( sqlite3StrICmp(zLeft,"cipher_hmac_pgno")==0 ){ - if(ctx) { - if(zRight) { - // clear both pgno endian flags - if(sqlite3StrICmp(zRight, "le") == 0) { - sqlcipher_codec_ctx_unset_flag(ctx, CIPHER_FLAG_BE_PGNO); - sqlcipher_codec_ctx_set_flag(ctx, CIPHER_FLAG_LE_PGNO); - } else if(sqlite3StrICmp(zRight, "be") == 0) { - sqlcipher_codec_ctx_unset_flag(ctx, CIPHER_FLAG_LE_PGNO); - sqlcipher_codec_ctx_set_flag(ctx, CIPHER_FLAG_BE_PGNO); - } else if(sqlite3StrICmp(zRight, "native") == 0) { - sqlcipher_codec_ctx_unset_flag(ctx, CIPHER_FLAG_LE_PGNO); - sqlcipher_codec_ctx_unset_flag(ctx, CIPHER_FLAG_BE_PGNO); - } - } else { - if(sqlcipher_codec_ctx_get_flag(ctx, CIPHER_FLAG_LE_PGNO, 2)) { - codec_vdbe_return_static_string(pParse, "cipher_hmac_pgno", "le"); - } else if(sqlcipher_codec_ctx_get_flag(ctx, CIPHER_FLAG_BE_PGNO, 2)) { - codec_vdbe_return_static_string(pParse, "cipher_hmac_pgno", "be"); - } else { - codec_vdbe_return_static_string(pParse, "cipher_hmac_pgno", "native"); - } - } - } - }else - if( sqlite3StrICmp(zLeft,"cipher_hmac_salt_mask")==0 ){ - if(ctx) { - if(zRight) { - if (sqlite3StrNICmp(zRight ,"x'", 2) == 0 && sqlite3Strlen30(zRight) == 5) { - unsigned char mask = 0; - const unsigned char *hex = (const unsigned char *)zRight+2; - cipher_hex2bin(hex,2,&mask); - sqlcipher_set_hmac_salt_mask(mask); - } - } else { - char *hmac_salt_mask = sqlite3_mprintf("%02x", sqlcipher_get_hmac_salt_mask()); - codec_vdbe_return_static_string(pParse, "cipher_hmac_salt_mask", hmac_salt_mask); - sqlite3_free(hmac_salt_mask); - } - } - }else { - return 0; - } - return 1; -} - - -/* - * sqlite3Codec can be called in multiple modes. - * encrypt mode - expected to return a pointer to the - * encrypted data without altering pData. - * decrypt mode - expected to return a pointer to pData, with - * the data decrypted in the input buffer - */ -void* sqlite3Codec(void *iCtx, void *data, Pgno pgno, int mode) { - codec_ctx *ctx = (codec_ctx *) iCtx; - int offset = 0, rc = 0; - int page_sz = sqlcipher_codec_ctx_get_pagesize(ctx); - unsigned char *pData = (unsigned char *) data; - void *buffer = sqlcipher_codec_ctx_get_data(ctx); - void *kdf_salt = sqlcipher_codec_ctx_get_kdf_salt(ctx); - CODEC_TRACE(("sqlite3Codec: entered pgno=%d, mode=%d, page_sz=%d\n", pgno, mode, page_sz)); - - /* call to derive keys if not present yet */ - if((rc = sqlcipher_codec_key_derive(ctx)) != SQLITE_OK) { - sqlcipher_codec_ctx_set_error(ctx, rc); - return NULL; - } - - if(pgno == 1) offset = FILE_HEADER_SZ; /* adjust starting pointers in data page for header offset on first page*/ - - CODEC_TRACE(("sqlite3Codec: switch mode=%d offset=%d\n", mode, offset)); - switch(mode) { - case 0: /* decrypt */ - case 2: - case 3: - if(pgno == 1) memcpy(buffer, SQLITE_FILE_HEADER, FILE_HEADER_SZ); /* copy file header to the first 16 bytes of the page */ - rc = sqlcipher_page_cipher(ctx, CIPHER_READ_CTX, pgno, CIPHER_DECRYPT, page_sz - offset, pData + offset, (unsigned char*)buffer + offset); - if(rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, rc); - memcpy(pData, buffer, page_sz); /* copy buffer data back to pData and return */ - return pData; - break; - case 6: /* encrypt */ - if(pgno == 1) memcpy(buffer, kdf_salt, FILE_HEADER_SZ); /* copy salt to output buffer */ - rc = sqlcipher_page_cipher(ctx, CIPHER_WRITE_CTX, pgno, CIPHER_ENCRYPT, page_sz - offset, pData + offset, (unsigned char*)buffer + offset); - if(rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, rc); - return buffer; /* return persistent buffer data, pData remains intact */ - break; - case 7: - if(pgno == 1) memcpy(buffer, kdf_salt, FILE_HEADER_SZ); /* copy salt to output buffer */ - rc = sqlcipher_page_cipher(ctx, CIPHER_READ_CTX, pgno, CIPHER_ENCRYPT, page_sz - offset, pData + offset, (unsigned char*)buffer + offset); - if(rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, rc); - return buffer; /* return persistent buffer data, pData remains intact */ - break; - default: - return pData; - break; - } -} - -void sqlite3FreeCodecArg(void *pCodecArg) { - codec_ctx *ctx = (codec_ctx *) pCodecArg; - if(pCodecArg == NULL) return; - sqlcipher_codec_ctx_free(&ctx); // wipe and free allocated memory for the context - sqlcipher_deactivate(); /* cleanup related structures, OpenSSL etc, when codec is detatched */ -} - -int sqlite3CodecAttach(sqlite3* db, int nDb, const void *zKey, int nKey) { - struct Db *pDb = &db->aDb[nDb]; - - CODEC_TRACE(("sqlite3CodecAttach: entered nDb=%d zKey=%s, nKey=%d\n", nDb, (char *)zKey, nKey)); - - - if(nKey && zKey && pDb->pBt) { - int rc; - Pager *pPager = pDb->pBt->pBt->pPager; - sqlite3_file *fd = sqlite3Pager_get_fd(pPager); - codec_ctx *ctx; - - sqlcipher_activate(); /* perform internal initialization for sqlcipher */ - - sqlite3_mutex_enter(db->mutex); - - /* point the internal codec argument against the contet to be prepared */ - rc = sqlcipher_codec_ctx_init(&ctx, pDb, pDb->pBt->pBt->pPager, fd, zKey, nKey); - - if(rc != SQLITE_OK) return rc; /* initialization failed, do not attach potentially corrupted context */ - - sqlite3pager_sqlite3PagerSetCodec(sqlite3BtreePager(pDb->pBt), sqlite3Codec, NULL, sqlite3FreeCodecArg, (void *) ctx); - - codec_set_btree_to_codec_pagesize(db, pDb, ctx); - - /* force secure delete. This has the benefit of wiping internal data when deleted - and also ensures that all pages are written to disk (i.e. not skipped by - sqlite3PagerDontWrite optimizations) */ - sqlite3BtreeSecureDelete(pDb->pBt, 1); - - /* if fd is null, then this is an in-memory database and - we dont' want to overwrite the AutoVacuum settings - if not null, then set to the default */ - if(fd != NULL) { - sqlite3BtreeSetAutoVacuum(pDb->pBt, SQLITE_DEFAULT_AUTOVACUUM); - } - sqlite3_mutex_leave(db->mutex); - } - return SQLITE_OK; -} - -void sqlite3_activate_see(const char* in) { - /* do nothing, security enhancements are always active */ -} - -static int sqlcipher_find_db_index(sqlite3 *db, const char *zDb) { - int db_index; - if(zDb == NULL){ - return 0; - } - for(db_index = 0; db_index < db->nDb; db_index++) { - struct Db *pDb = &db->aDb[db_index]; - if(strcmp(pDb->zName, zDb) == 0) { - return db_index; - } - } - return 0; -} - -int sqlite3_key(sqlite3 *db, const void *pKey, int nKey) { - CODEC_TRACE(("sqlite3_key entered: db=%p pKey=%s nKey=%d\n", db, (char *)pKey, nKey)); - return sqlite3_key_v2(db, "main", pKey, nKey); -} - -int sqlite3_key_v2(sqlite3 *db, const char *zDb, const void *pKey, int nKey) { - CODEC_TRACE(("sqlite3_key_v2: entered db=%p zDb=%s pKey=%s nKey=%d\n", db, zDb, (char *)pKey, nKey)); - /* attach key if db and pKey are not null and nKey is > 0 */ - if(db && pKey && nKey) { - int db_index = sqlcipher_find_db_index(db, zDb); - return sqlite3CodecAttach(db, db_index, pKey, nKey); - } - return SQLITE_ERROR; -} - -int sqlite3_rekey(sqlite3 *db, const void *pKey, int nKey) { - CODEC_TRACE(("sqlite3_rekey entered: db=%p pKey=%s nKey=%d\n", db, (char *)pKey, nKey)); - return sqlite3_rekey_v2(db, "main", pKey, nKey); -} - -/* sqlite3_rekey_v2 -** Given a database, this will reencrypt the database using a new key. -** There is only one possible modes of operation - to encrypt a database -** that is already encrpyted. If the database is not already encrypted -** this should do nothing -** The proposed logic for this function follows: -** 1. Determine if the database is already encryptped -** 2. If there is NOT already a key present do nothing -** 3. If there is a key present, re-encrypt the database with the new key -*/ -int sqlite3_rekey_v2(sqlite3 *db, const char *zDb, const void *pKey, int nKey) { - CODEC_TRACE(("sqlite3_rekey_v2: entered db=%p zDb=%s pKey=%s, nKey=%d\n", db, zDb, (char *)pKey, nKey)); - if(db && pKey && nKey) { - int db_index = sqlcipher_find_db_index(db, zDb); - struct Db *pDb = &db->aDb[db_index]; - CODEC_TRACE(("sqlite3_rekey_v2: database pDb=%p db_index:%d\n", pDb, db_index)); - if(pDb->pBt) { - codec_ctx *ctx; - int rc, page_count; - Pgno pgno; - PgHdr *page; - Pager *pPager = pDb->pBt->pBt->pPager; - - sqlite3pager_get_codec(pDb->pBt->pBt->pPager, (void **) &ctx); - - if(ctx == NULL) { - /* there was no codec attached to this database, so this should do nothing! */ - CODEC_TRACE(("sqlite3_rekey_v2: no codec attached to db, exiting\n")); - return SQLITE_OK; - } - - sqlite3_mutex_enter(db->mutex); - - codec_set_pass_key(db, db_index, pKey, nKey, CIPHER_WRITE_CTX); - - /* do stuff here to rewrite the database - ** 1. Create a transaction on the database - ** 2. Iterate through each page, reading it and then writing it. - ** 3. If that goes ok then commit and put ctx->rekey into ctx->key - ** note: don't deallocate rekey since it may be used in a subsequent iteration - */ - rc = sqlite3BtreeBeginTrans(pDb->pBt, 1); /* begin write transaction */ - sqlite3PagerPagecount(pPager, &page_count); - for(pgno = 1; rc == SQLITE_OK && pgno <= (unsigned int)page_count; pgno++) { /* pgno's start at 1 see pager.c:pagerAcquire */ - if(!sqlite3pager_is_mj_pgno(pPager, pgno)) { /* skip this page (see pager.c:pagerAcquire for reasoning) */ - rc = sqlite3PagerGet(pPager, pgno, &page, 0); - if(rc == SQLITE_OK) { /* write page see pager_incr_changecounter for example */ - rc = sqlite3PagerWrite(page); - if(rc == SQLITE_OK) { - sqlite3PagerUnref(page); - } else { - CODEC_TRACE(("sqlite3_rekey_v2: error %d occurred writing page %d\n", rc, pgno)); - } - } else { - CODEC_TRACE(("sqlite3_rekey_v2: error %d occurred getting page %d\n", rc, pgno)); - } - } - } - - /* if commit was successful commit and copy the rekey data to current key, else rollback to release locks */ - if(rc == SQLITE_OK) { - CODEC_TRACE(("sqlite3_rekey_v2: committing\n")); - rc = sqlite3BtreeCommit(pDb->pBt); - sqlcipher_codec_key_copy(ctx, CIPHER_WRITE_CTX); - } else { - CODEC_TRACE(("sqlite3_rekey_v2: rollback\n")); - sqlite3BtreeRollback(pDb->pBt, SQLITE_ABORT_ROLLBACK, 0); - } - - sqlite3_mutex_leave(db->mutex); - } - return SQLITE_OK; - } - return SQLITE_ERROR; -} - -void sqlite3CodecGetKey(sqlite3* db, int nDb, void **zKey, int *nKey) { - struct Db *pDb = &db->aDb[nDb]; - CODEC_TRACE(("sqlite3CodecGetKey: entered db=%p, nDb=%d\n", db, nDb)); - if( pDb->pBt ) { - codec_ctx *ctx; - sqlite3pager_get_codec(pDb->pBt->pBt->pPager, (void **) &ctx); - if(ctx) { - if(sqlcipher_codec_get_store_pass(ctx) == 1) { - sqlcipher_codec_get_pass(ctx, zKey, nKey); - } else { - sqlcipher_codec_get_keyspec(ctx, zKey, nKey); - } - } else { - *zKey = NULL; - *nKey = 0; - } - } -} - -#ifndef OMIT_EXPORT - -/* - * Implementation of an "export" function that allows a caller - * to duplicate the main database to an attached database. This is intended - * as a conveneince for users who need to: - * - * 1. migrate from an non-encrypted database to an encrypted database - * 2. move from an encrypted database to a non-encrypted database - * 3. convert beween the various flavors of encrypted databases. - * - * This implementation is based heavily on the procedure and code used - * in vacuum.c, but is exposed as a function that allows export to any - * named attached database. - */ - -/* -** Finalize a prepared statement. If there was an error, store the -** text of the error message in *pzErrMsg. Return the result code. -** -** Based on vacuumFinalize from vacuum.c -*/ -static int sqlcipher_finalize(sqlite3 *db, sqlite3_stmt *pStmt, char **pzErrMsg){ - int rc; - rc = sqlite3VdbeFinalize((Vdbe*)pStmt); - if( rc ){ - sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db)); - } - return rc; -} - -/* -** Execute zSql on database db. Return an error code. -** -** Based on execSql from vacuum.c -*/ -static int sqlcipher_execSql(sqlite3 *db, char **pzErrMsg, const char *zSql){ - sqlite3_stmt *pStmt; - VVA_ONLY( int rc; ) - if( !zSql ){ - return SQLITE_NOMEM; - } - if( SQLITE_OK!=sqlite3_prepare(db, zSql, -1, &pStmt, 0) ){ - sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db)); - return sqlite3_errcode(db); - } - VVA_ONLY( rc = ) sqlite3_step(pStmt); - assert( rc!=SQLITE_ROW ); - return sqlcipher_finalize(db, pStmt, pzErrMsg); -} - -/* -** Execute zSql on database db. The statement returns exactly -** one column. Execute this as SQL on the same database. -** -** Based on execExecSql from vacuum.c -*/ -static int sqlcipher_execExecSql(sqlite3 *db, char **pzErrMsg, const char *zSql){ - sqlite3_stmt *pStmt; - int rc; - - rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0); - if( rc!=SQLITE_OK ) return rc; - - while( SQLITE_ROW==sqlite3_step(pStmt) ){ - rc = sqlcipher_execSql(db, pzErrMsg, (char*)sqlite3_column_text(pStmt, 0)); - if( rc!=SQLITE_OK ){ - sqlcipher_finalize(db, pStmt, pzErrMsg); - return rc; - } - } - - return sqlcipher_finalize(db, pStmt, pzErrMsg); -} - -/* - * copy database and schema from the main database to an attached database - * - * Based on sqlite3RunVacuum from vacuum.c -*/ -void sqlcipher_exportFunc(sqlite3_context *context, int argc, sqlite3_value **argv) { - sqlite3 *db = sqlite3_context_db_handle(context); - const char* attachedDb = (const char*) sqlite3_value_text(argv[0]); - int saved_flags; /* Saved value of the db->flags */ - int saved_nChange; /* Saved value of db->nChange */ - int saved_nTotalChange; /* Saved value of db->nTotalChange */ - void (*saved_xTrace)(void*,const char*); /* Saved db->xTrace */ - int rc = SQLITE_OK; /* Return code from service routines */ - char *zSql = NULL; /* SQL statements */ - char *pzErrMsg = NULL; - - saved_flags = db->flags; - saved_nChange = db->nChange; - saved_nTotalChange = db->nTotalChange; - saved_xTrace = db->xTrace; - db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks | SQLITE_PreferBuiltin; - db->flags &= ~(SQLITE_ForeignKeys | SQLITE_ReverseOrder); - db->xTrace = 0; - - /* Query the schema of the main database. Create a mirror schema - ** in the temporary database. - */ - zSql = sqlite3_mprintf( - "SELECT 'CREATE TABLE %s.' || substr(sql,14) " - " FROM sqlite_master WHERE type='table' AND name!='sqlite_sequence'" - " AND rootpage>0" - , attachedDb); - rc = (zSql == NULL) ? SQLITE_NOMEM : sqlcipher_execExecSql(db, &pzErrMsg, zSql); - if( rc!=SQLITE_OK ) goto end_of_export; - sqlite3_free(zSql); - - zSql = sqlite3_mprintf( - "SELECT 'CREATE INDEX %s.' || substr(sql,14)" - " FROM sqlite_master WHERE sql LIKE 'CREATE INDEX %%' " - , attachedDb); - rc = (zSql == NULL) ? SQLITE_NOMEM : sqlcipher_execExecSql(db, &pzErrMsg, zSql); - if( rc!=SQLITE_OK ) goto end_of_export; - sqlite3_free(zSql); - - zSql = sqlite3_mprintf( - "SELECT 'CREATE UNIQUE INDEX %s.' || substr(sql,21) " - " FROM sqlite_master WHERE sql LIKE 'CREATE UNIQUE INDEX %%'" - , attachedDb); - rc = (zSql == NULL) ? SQLITE_NOMEM : sqlcipher_execExecSql(db, &pzErrMsg, zSql); - if( rc!=SQLITE_OK ) goto end_of_export; - sqlite3_free(zSql); - - /* Loop through the tables in the main database. For each, do - ** an "INSERT INTO rekey_db.xxx SELECT * FROM main.xxx;" to copy - ** the contents to the temporary database. - */ - zSql = sqlite3_mprintf( - "SELECT 'INSERT INTO %s.' || quote(name) " - "|| ' SELECT * FROM main.' || quote(name) || ';'" - "FROM main.sqlite_master " - "WHERE type = 'table' AND name!='sqlite_sequence' " - " AND rootpage>0" - , attachedDb); - rc = (zSql == NULL) ? SQLITE_NOMEM : sqlcipher_execExecSql(db, &pzErrMsg, zSql); - if( rc!=SQLITE_OK ) goto end_of_export; - sqlite3_free(zSql); - - /* Copy over the sequence table - */ - zSql = sqlite3_mprintf( - "SELECT 'DELETE FROM %s.' || quote(name) || ';' " - "FROM %s.sqlite_master WHERE name='sqlite_sequence' " - , attachedDb, attachedDb); - rc = (zSql == NULL) ? SQLITE_NOMEM : sqlcipher_execExecSql(db, &pzErrMsg, zSql); - if( rc!=SQLITE_OK ) goto end_of_export; - sqlite3_free(zSql); - - zSql = sqlite3_mprintf( - "SELECT 'INSERT INTO %s.' || quote(name) " - "|| ' SELECT * FROM main.' || quote(name) || ';' " - "FROM %s.sqlite_master WHERE name=='sqlite_sequence';" - , attachedDb, attachedDb); - rc = (zSql == NULL) ? SQLITE_NOMEM : sqlcipher_execExecSql(db, &pzErrMsg, zSql); - if( rc!=SQLITE_OK ) goto end_of_export; - sqlite3_free(zSql); - - /* Copy the triggers, views, and virtual tables from the main database - ** over to the temporary database. None of these objects has any - ** associated storage, so all we have to do is copy their entries - ** from the SQLITE_MASTER table. - */ - zSql = sqlite3_mprintf( - "INSERT INTO %s.sqlite_master " - " SELECT type, name, tbl_name, rootpage, sql" - " FROM main.sqlite_master" - " WHERE type='view' OR type='trigger'" - " OR (type='table' AND rootpage=0)" - , attachedDb); - rc = (zSql == NULL) ? SQLITE_NOMEM : sqlcipher_execSql(db, &pzErrMsg, zSql); - if( rc!=SQLITE_OK ) goto end_of_export; - sqlite3_free(zSql); - - zSql = NULL; -end_of_export: - db->flags = saved_flags; - db->nChange = saved_nChange; - db->nTotalChange = saved_nTotalChange; - db->xTrace = saved_xTrace; - - sqlite3_free(zSql); - - if(rc) { - if(pzErrMsg != NULL) { - sqlite3_result_error(context, pzErrMsg, -1); - sqlite3DbFree(db, pzErrMsg); - } else { - sqlite3_result_error(context, sqlite3ErrStr(rc), -1); - } - } -} - -#endif - -/* END SQLCIPHER */ -#endif diff --git a/src/crypto.h b/src/crypto.h deleted file mode 100644 index f58f9d1d04..0000000000 --- a/src/crypto.h +++ /dev/null @@ -1,246 +0,0 @@ -/* -** SQLCipher -** crypto.h developed by Stephen Lombardo (Zetetic LLC) -** sjlombardo at zetetic dot net -** http://zetetic.net -** -** Copyright (c) 2008, ZETETIC LLC -** All rights reserved. -** -** Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in the -** documentation and/or other materials provided with the distribution. -** * Neither the name of the ZETETIC LLC nor the -** names of its contributors may be used to endorse or promote products -** derived from this software without specific prior written permission. -** -** THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY -** EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -** DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY -** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -** -*/ -/* BEGIN SQLCIPHER */ -#ifdef SQLITE_HAS_CODEC -#ifndef CRYPTO_H -#define CRYPTO_H - -#if !defined (SQLCIPHER_CRYPTO_CC) \ - && !defined (SQLCIPHER_CRYPTO_LIBTOMCRYPT) \ - && !defined (SQLCIPHER_CRYPTO_OPENSSL) -#define SQLCIPHER_CRYPTO_OPENSSL -#endif - -#define FILE_HEADER_SZ 16 - -#ifndef CIPHER_VERSION -#ifdef SQLCIPHER_FIPS -#define CIPHER_VERSION "3.4.0 FIPS" -#else -#define CIPHER_VERSION "3.4.0" -#endif -#endif - -#ifndef CIPHER -#define CIPHER "aes-256-cbc" -#endif - -#define CIPHER_DECRYPT 0 -#define CIPHER_ENCRYPT 1 - -#define CIPHER_READ_CTX 0 -#define CIPHER_WRITE_CTX 1 -#define CIPHER_READWRITE_CTX 2 - -#ifndef PBKDF2_ITER -#define PBKDF2_ITER 64000 -#endif - -/* possible flags for cipher_ctx->flags */ -#define CIPHER_FLAG_HMAC 0x01 -#define CIPHER_FLAG_LE_PGNO 0x02 -#define CIPHER_FLAG_BE_PGNO 0x04 - -#ifndef DEFAULT_CIPHER_FLAGS -#define DEFAULT_CIPHER_FLAGS CIPHER_FLAG_HMAC | CIPHER_FLAG_LE_PGNO -#endif - - -/* by default, sqlcipher will use a reduced number of iterations to generate - the HMAC key / or transform a raw cipher key - */ -#ifndef FAST_PBKDF2_ITER -#define FAST_PBKDF2_ITER 2 -#endif - -/* this if a fixed random array that will be xor'd with the database salt to ensure that the - salt passed to the HMAC key derivation function is not the same as that used to derive - the encryption key. This can be overridden at compile time but it will make the resulting - binary incompatible with the default builds when using HMAC. A future version of SQLcipher - will likely allow this to be defined at runtime via pragma */ -#ifndef HMAC_SALT_MASK -#define HMAC_SALT_MASK 0x3a -#endif - -#ifndef CIPHER_MAX_IV_SZ -#define CIPHER_MAX_IV_SZ 16 -#endif - -#ifndef CIPHER_MAX_KEY_SZ -#define CIPHER_MAX_KEY_SZ 64 -#endif - - -#ifdef CODEC_DEBUG -#define CODEC_TRACE(X) {printf X;fflush(stdout);} -#else -#define CODEC_TRACE(X) -#endif - -#ifdef CODEC_DEBUG_PAGEDATA -#define CODEC_HEXDUMP(DESC,BUFFER,LEN) \ - { \ - int __pctr; \ - printf(DESC); \ - for(__pctr=0; __pctr < LEN; __pctr++) { \ - if(__pctr % 16 == 0) printf("\n%05x: ",__pctr); \ - printf("%02x ",((unsigned char*) BUFFER)[__pctr]); \ - } \ - printf("\n"); \ - fflush(stdout); \ - } -#else -#define CODEC_HEXDUMP(DESC,BUFFER,LEN) -#endif - -/* extensions defined in pager.c */ -void sqlite3pager_get_codec(Pager *pPager, void **ctx); -int sqlite3pager_is_mj_pgno(Pager *pPager, Pgno pgno); -sqlite3_file *sqlite3Pager_get_fd(Pager *pPager); -void sqlite3pager_sqlite3PagerSetCodec( - Pager *pPager, - void *(*xCodec)(void*,void*,Pgno,int), - void (*xCodecSizeChng)(void*,int,int), - void (*xCodecFree)(void*), - void *pCodec -); -void sqlite3pager_sqlite3PagerSetError(Pager *pPager, int error); -/* end extensions defined in pager.c */ - -/* -** Simple shared routines for converting hex char strings to binary data - */ -static int cipher_hex2int(char c) { - return (c>='0' && c<='9') ? (c)-'0' : - (c>='A' && c<='F') ? (c)-'A'+10 : - (c>='a' && c<='f') ? (c)-'a'+10 : 0; -} - -static void cipher_hex2bin(const unsigned char *hex, int sz, unsigned char *out){ - int i; - for(i = 0; i < sz; i += 2){ - out[i/2] = (cipher_hex2int(hex[i])<<4) | cipher_hex2int(hex[i+1]); - } -} - -static void cipher_bin2hex(const unsigned char* in, int sz, char *out) { - int i; - for(i=0; i < sz; i++) { - sqlite3_snprintf(3, out + (i*2), "%02x ", in[i]); - } -} - -static int cipher_isHex(const unsigned char *hex, int sz){ - int i; - for(i = 0; i < sz; i++) { - unsigned char c = hex[i]; - if ((c < '0' || c > '9') && - (c < 'A' || c > 'F') && - (c < 'a' || c > 'f')) { - return 0; - } - } - return 1; -} - -/* extensions defined in crypto_impl.c */ -typedef struct codec_ctx codec_ctx; - -/* activation and initialization */ -void sqlcipher_activate(); -void sqlcipher_deactivate(); -int sqlcipher_codec_ctx_init(codec_ctx **, Db *, Pager *, sqlite3_file *, const void *, int); -void sqlcipher_codec_ctx_free(codec_ctx **); -int sqlcipher_codec_key_derive(codec_ctx *); -int sqlcipher_codec_key_copy(codec_ctx *, int); - -/* page cipher implementation */ -int sqlcipher_page_cipher(codec_ctx *, int, Pgno, int, int, unsigned char *, unsigned char *); - -/* context setters & getters */ -void sqlcipher_codec_ctx_set_error(codec_ctx *, int); - -int sqlcipher_codec_ctx_set_pass(codec_ctx *, const void *, int, int); -void sqlcipher_codec_get_keyspec(codec_ctx *, void **zKey, int *nKey); - -int sqlcipher_codec_ctx_set_pagesize(codec_ctx *, int); -int sqlcipher_codec_ctx_get_pagesize(codec_ctx *); -int sqlcipher_codec_ctx_get_reservesize(codec_ctx *); - -void sqlcipher_set_default_pagesize(int page_size); -int sqlcipher_get_default_pagesize(); - -void sqlcipher_set_default_kdf_iter(int iter); -int sqlcipher_get_default_kdf_iter(); - -int sqlcipher_codec_ctx_set_kdf_iter(codec_ctx *, int, int); -int sqlcipher_codec_ctx_get_kdf_iter(codec_ctx *ctx, int); - -void* sqlcipher_codec_ctx_get_kdf_salt(codec_ctx *ctx); - -int sqlcipher_codec_ctx_set_fast_kdf_iter(codec_ctx *, int, int); -int sqlcipher_codec_ctx_get_fast_kdf_iter(codec_ctx *, int); - -int sqlcipher_codec_ctx_set_cipher(codec_ctx *, const char *, int); -const char* sqlcipher_codec_ctx_get_cipher(codec_ctx *ctx, int for_ctx); - -void* sqlcipher_codec_ctx_get_data(codec_ctx *); - -void sqlcipher_exportFunc(sqlite3_context *, int, sqlite3_value **); - -void sqlcipher_set_default_use_hmac(int use); -int sqlcipher_get_default_use_hmac(); - -void sqlcipher_set_hmac_salt_mask(unsigned char mask); -unsigned char sqlcipher_get_hmac_salt_mask(); - -int sqlcipher_codec_ctx_set_use_hmac(codec_ctx *ctx, int use); -int sqlcipher_codec_ctx_get_use_hmac(codec_ctx *ctx, int for_ctx); - -int sqlcipher_codec_ctx_set_flag(codec_ctx *ctx, unsigned int flag); -int sqlcipher_codec_ctx_unset_flag(codec_ctx *ctx, unsigned int flag); -int sqlcipher_codec_ctx_get_flag(codec_ctx *ctx, unsigned int flag, int for_ctx); - -const char* sqlcipher_codec_get_cipher_provider(codec_ctx *ctx); -int sqlcipher_codec_ctx_migrate(codec_ctx *ctx); -int sqlcipher_codec_add_random(codec_ctx *ctx, const char *data, int random_sz); -int sqlcipher_cipher_profile(sqlite3 *db, const char *destination); -static void sqlcipher_profile_callback(void *file, const char *sql, sqlite3_uint64 run_time); -static int sqlcipher_codec_get_store_pass(codec_ctx *ctx); -static void sqlcipher_codec_get_pass(codec_ctx *ctx, void **zKey, int *nKey); -static void sqlcipher_codec_set_store_pass(codec_ctx *ctx, int value); -int sqlcipher_codec_fips_status(codec_ctx *ctx); -const char* sqlcipher_codec_get_provider_version(codec_ctx *ctx); -#endif -#endif -/* END SQLCIPHER */ diff --git a/src/crypto_cc.c b/src/crypto_cc.c index 7890ff8ab0..37669cb46b 100644 --- a/src/crypto_cc.c +++ b/src/crypto_cc.c @@ -31,19 +31,20 @@ /* BEGIN SQLCIPHER */ #ifdef SQLITE_HAS_CODEC #ifdef SQLCIPHER_CRYPTO_CC -#include "crypto.h" #include "sqlcipher.h" #include #include #include -static int sqlcipher_cc_add_random(void *ctx, void *buffer, int length) { +int sqlcipher_cc_setup(sqlcipher_provider *p); + +static int sqlcipher_cc_add_random(void *ctx, const void *buffer, int length) { return SQLITE_OK; } /* generate a defined number of random bytes */ static int sqlcipher_cc_random (void *ctx, void *buffer, int length) { - return (SecRandomCopyBytes(kSecRandomDefault, length, (uint8_t *)buffer) == 0) ? SQLITE_OK : SQLITE_ERROR; + return (SecRandomCopyBytes(kSecRandomDefault, length, (uint8_t *)buffer) == kCCSuccess) ? SQLITE_OK : SQLITE_ERROR; } static const char* sqlcipher_cc_get_provider_name(void *ctx) { @@ -52,53 +53,106 @@ static const char* sqlcipher_cc_get_provider_name(void *ctx) { static const char* sqlcipher_cc_get_provider_version(void *ctx) { #if TARGET_OS_MAC - CFTypeRef version; + /* macOS uses com.apple.security with a lowercase s */ CFBundleRef bundle = CFBundleGetBundleWithIdentifier(CFSTR("com.apple.security")); - if(bundle == NULL) { - return "unknown"; + CFTypeRef bundle_ver; + const char *ver; + + /* if the bundle wasn't identified, try secrurity with a capial S (works for iOS) */ + if(!bundle) { + bundle = CFBundleGetBundleWithIdentifier(CFSTR("com.apple.Security")); } - version = CFBundleGetValueForInfoDictionaryKey(bundle, CFSTR("CFBundleShortVersionString")); - return CFStringGetCStringPtr(version, kCFStringEncodingUTF8); -#else - return "unknown"; + + /* If the bundle was resolved, retrieve the bundle version key then attempt to convert it to a C string. + * Note that it is possible for CFStringGetCString to return NULL (this is warned against extensively in the + * header), so only return a value if the conversion was successful. */ + if( + bundle + && (bundle_ver = CFBundleGetValueForInfoDictionaryKey(bundle, kCFBundleVersionKey)) + && (ver = CFStringGetCStringPtr(bundle_ver, kCFStringEncodingUTF8)) + ) { + return ver; + } + #endif + /* unable to detect the CoreCrypto version, return a fixed string "unknown" */ + return "unknown"; } -static int sqlcipher_cc_hmac(void *ctx, unsigned char *hmac_key, int key_sz, unsigned char *in, int in_sz, unsigned char *in2, int in2_sz, unsigned char *out) { +static int sqlcipher_cc_hmac( + void *ctx, int algorithm, + const unsigned char *hmac_key, int key_sz, + const unsigned char *in, int in_sz, + const unsigned char *in2, int in2_sz, + unsigned char *out +) { CCHmacContext hmac_context; - CCHmacInit(&hmac_context, kCCHmacAlgSHA1, hmac_key, key_sz); + if(in == NULL) return SQLITE_ERROR; + switch(algorithm) { + case SQLCIPHER_HMAC_SHA1: + CCHmacInit(&hmac_context, kCCHmacAlgSHA1, hmac_key, key_sz); + break; + case SQLCIPHER_HMAC_SHA256: + CCHmacInit(&hmac_context, kCCHmacAlgSHA256, hmac_key, key_sz); + break; + case SQLCIPHER_HMAC_SHA512: + CCHmacInit(&hmac_context, kCCHmacAlgSHA512, hmac_key, key_sz); + break; + default: + return SQLITE_ERROR; + } CCHmacUpdate(&hmac_context, in, in_sz); - CCHmacUpdate(&hmac_context, in2, in2_sz); + if(in2 != NULL) CCHmacUpdate(&hmac_context, in2, in2_sz); CCHmacFinal(&hmac_context, out); return SQLITE_OK; } -static int sqlcipher_cc_kdf(void *ctx, const unsigned char *pass, int pass_sz, unsigned char* salt, int salt_sz, int workfactor, int key_sz, unsigned char *key) { - CCKeyDerivationPBKDF(kCCPBKDF2, (const char *)pass, pass_sz, salt, salt_sz, kCCPRFHmacAlgSHA1, workfactor, key, key_sz); +static int sqlcipher_cc_kdf( + void *ctx, int algorithm, + const unsigned char *pass, int pass_sz, + const unsigned char* salt, int salt_sz, + int workfactor, + int key_sz, unsigned char *key +) { + switch(algorithm) { + case SQLCIPHER_HMAC_SHA1: + if(CCKeyDerivationPBKDF(kCCPBKDF2, (const char *)pass, pass_sz, salt, salt_sz, kCCPRFHmacAlgSHA1, workfactor, key, key_sz) != kCCSuccess) return SQLITE_ERROR; + break; + case SQLCIPHER_HMAC_SHA256: + if(CCKeyDerivationPBKDF(kCCPBKDF2, (const char *)pass, pass_sz, salt, salt_sz, kCCPRFHmacAlgSHA256, workfactor, key, key_sz) != kCCSuccess) return SQLITE_ERROR; + break; + case SQLCIPHER_HMAC_SHA512: + if(CCKeyDerivationPBKDF(kCCPBKDF2, (const char *)pass, pass_sz, salt, salt_sz, kCCPRFHmacAlgSHA512, workfactor, key, key_sz) != kCCSuccess) return SQLITE_ERROR; + break; + default: + return SQLITE_ERROR; + } return SQLITE_OK; } -static int sqlcipher_cc_cipher(void *ctx, int mode, unsigned char *key, int key_sz, unsigned char *iv, unsigned char *in, int in_sz, unsigned char *out) { +static int sqlcipher_cc_cipher( + void *ctx, int mode, + const unsigned char *key, int key_sz, + const unsigned char *iv, + const unsigned char *in, int in_sz, + unsigned char *out +) { CCCryptorRef cryptor; size_t tmp_csz, csz; - CCOperation op = mode == CIPHER_ENCRYPT ? kCCEncrypt : kCCDecrypt; + CCOperation op = mode == SQLCIPHER_ENCRYPT ? kCCEncrypt : kCCDecrypt; - CCCryptorCreate(op, kCCAlgorithmAES128, 0, key, kCCKeySizeAES256, iv, &cryptor); - CCCryptorUpdate(cryptor, in, in_sz, out, in_sz, &tmp_csz); + if(CCCryptorCreate(op, kCCAlgorithmAES128, 0, key, kCCKeySizeAES256, iv, &cryptor) != kCCSuccess) return SQLITE_ERROR; + if(CCCryptorUpdate(cryptor, in, in_sz, out, in_sz, &tmp_csz) != kCCSuccess) return SQLITE_ERROR; csz = tmp_csz; out += tmp_csz; - CCCryptorFinal(cryptor, out, in_sz - csz, &tmp_csz); + if(CCCryptorFinal(cryptor, out, in_sz - csz, &tmp_csz) != kCCSuccess) return SQLITE_ERROR; csz += tmp_csz; - CCCryptorRelease(cryptor); - assert(size == csz); + if(CCCryptorRelease(cryptor) != kCCSuccess) return SQLITE_ERROR; + assert(in_sz == csz); return SQLITE_OK; } -static int sqlcipher_cc_set_cipher(void *ctx, const char *cipher_name) { - return SQLITE_OK; -} - static const char* sqlcipher_cc_get_cipher(void *ctx) { return "aes-256-cbc"; } @@ -115,16 +169,20 @@ static int sqlcipher_cc_get_block_sz(void *ctx) { return kCCBlockSizeAES128; } -static int sqlcipher_cc_get_hmac_sz(void *ctx) { - return CC_SHA1_DIGEST_LENGTH; -} - -static int sqlcipher_cc_ctx_copy(void *target_ctx, void *source_ctx) { - return SQLITE_OK; -} - -static int sqlcipher_cc_ctx_cmp(void *c1, void *c2) { - return 1; /* always indicate contexts are the same */ +static int sqlcipher_cc_get_hmac_sz(void *ctx, int algorithm) { + switch(algorithm) { + case SQLCIPHER_HMAC_SHA1: + return CC_SHA1_DIGEST_LENGTH; + break; + case SQLCIPHER_HMAC_SHA256: + return CC_SHA256_DIGEST_LENGTH; + break; + case SQLCIPHER_HMAC_SHA512: + return CC_SHA512_DIGEST_LENGTH; + break; + default: + return 0; + } } static int sqlcipher_cc_ctx_init(void **ctx) { @@ -140,19 +198,18 @@ static int sqlcipher_cc_fips_status(void *ctx) { } int sqlcipher_cc_setup(sqlcipher_provider *p) { + p->init = NULL; + p->shutdown = NULL; p->random = sqlcipher_cc_random; p->get_provider_name = sqlcipher_cc_get_provider_name; p->hmac = sqlcipher_cc_hmac; p->kdf = sqlcipher_cc_kdf; p->cipher = sqlcipher_cc_cipher; - p->set_cipher = sqlcipher_cc_set_cipher; p->get_cipher = sqlcipher_cc_get_cipher; p->get_key_sz = sqlcipher_cc_get_key_sz; p->get_iv_sz = sqlcipher_cc_get_iv_sz; p->get_block_sz = sqlcipher_cc_get_block_sz; p->get_hmac_sz = sqlcipher_cc_get_hmac_sz; - p->ctx_copy = sqlcipher_cc_ctx_copy; - p->ctx_cmp = sqlcipher_cc_ctx_cmp; p->ctx_init = sqlcipher_cc_ctx_init; p->ctx_free = sqlcipher_cc_ctx_free; p->add_random = sqlcipher_cc_add_random; diff --git a/src/crypto_impl.c b/src/crypto_impl.c deleted file mode 100644 index 5a45454ddf..0000000000 --- a/src/crypto_impl.c +++ /dev/null @@ -1,1237 +0,0 @@ -/* -** SQLCipher -** http://sqlcipher.net -** -** Copyright (c) 2008 - 2013, ZETETIC LLC -** All rights reserved. -** -** Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in the -** documentation and/or other materials provided with the distribution. -** * Neither the name of the ZETETIC LLC nor the -** names of its contributors may be used to endorse or promote products -** derived from this software without specific prior written permission. -** -** THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY -** EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -** DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY -** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -** -*/ -/* BEGIN SQLCIPHER */ -#ifdef SQLITE_HAS_CODEC - -#include "sqliteInt.h" -#include "btreeInt.h" -#include "sqlcipher.h" -#include "crypto.h" -#ifndef OMIT_MEMLOCK -#if defined(__unix__) || defined(__APPLE__) || defined(_AIX) -#include -#elif defined(_WIN32) -# include -#endif -#endif - -/* the default implementation of SQLCipher uses a cipher_ctx - to keep track of read / write state separately. The following - struct and associated functions are defined here */ -typedef struct { - int store_pass; - int derive_key; - int kdf_iter; - int fast_kdf_iter; - int key_sz; - int iv_sz; - int block_sz; - int pass_sz; - int reserve_sz; - int hmac_sz; - int keyspec_sz; - unsigned int flags; - unsigned char *key; - unsigned char *hmac_key; - unsigned char *pass; - char *keyspec; - sqlcipher_provider *provider; - void *provider_ctx; -} cipher_ctx; - -static unsigned int default_flags = DEFAULT_CIPHER_FLAGS; -static unsigned char hmac_salt_mask = HMAC_SALT_MASK; -static int default_kdf_iter = PBKDF2_ITER; -static int default_page_size = SQLITE_DEFAULT_PAGE_SIZE; -static unsigned int sqlcipher_activate_count = 0; -static sqlite3_mutex* sqlcipher_provider_mutex = NULL; -static sqlcipher_provider *default_provider = NULL; - -struct codec_ctx { - int kdf_salt_sz; - int page_sz; - unsigned char *kdf_salt; - unsigned char *hmac_kdf_salt; - unsigned char *buffer; - Btree *pBt; - cipher_ctx *read_ctx; - cipher_ctx *write_ctx; - unsigned int skip_read_hmac; - unsigned int need_kdf_salt; -}; - -int sqlcipher_register_provider(sqlcipher_provider *p) { - sqlite3_mutex_enter(sqlcipher_provider_mutex); - if(default_provider != NULL && default_provider != p) { - /* only free the current registerd provider if it has been initialized - and it isn't a pointer to the same provider passed to the function - (i.e. protect against a caller calling register twice for the same provider) */ - sqlcipher_free(default_provider, sizeof(sqlcipher_provider)); - } - default_provider = p; - sqlite3_mutex_leave(sqlcipher_provider_mutex); - return SQLITE_OK; -} - -/* return a pointer to the currently registered provider. This will - allow an application to fetch the current registered provider and - make minor changes to it */ -sqlcipher_provider* sqlcipher_get_provider() { - return default_provider; -} - -void sqlcipher_activate() { - sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER)); - - if(sqlcipher_provider_mutex == NULL) { - /* allocate a new mutex to guard access to the provider */ - sqlcipher_provider_mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); - } - - /* check to see if there is a provider registered at this point - if there no provider registered at this point, register the - default provider */ - if(sqlcipher_get_provider() == NULL) { - sqlcipher_provider *p = sqlcipher_malloc(sizeof(sqlcipher_provider)); -#if defined (SQLCIPHER_CRYPTO_CC) - extern int sqlcipher_cc_setup(sqlcipher_provider *p); - sqlcipher_cc_setup(p); -#elif defined (SQLCIPHER_CRYPTO_LIBTOMCRYPT) - extern int sqlcipher_ltc_setup(sqlcipher_provider *p); - sqlcipher_ltc_setup(p); -#elif defined (SQLCIPHER_CRYPTO_OPENSSL) - extern int sqlcipher_openssl_setup(sqlcipher_provider *p); - sqlcipher_openssl_setup(p); -#else -#error "NO DEFAULT SQLCIPHER CRYPTO PROVIDER DEFINED" -#endif - sqlcipher_register_provider(p); - } - - sqlcipher_activate_count++; /* increment activation count */ - - sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER)); -} - -void sqlcipher_deactivate() { - sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER)); - sqlcipher_activate_count--; - /* if no connections are using sqlcipher, cleanup globals */ - if(sqlcipher_activate_count < 1) { - sqlite3_mutex_enter(sqlcipher_provider_mutex); - if(default_provider != NULL) { - sqlcipher_free(default_provider, sizeof(sqlcipher_provider)); - default_provider = NULL; - } - sqlite3_mutex_leave(sqlcipher_provider_mutex); - - /* last connection closed, free provider mutex*/ - sqlite3_mutex_free(sqlcipher_provider_mutex); - sqlcipher_provider_mutex = NULL; - - sqlcipher_activate_count = 0; /* reset activation count */ - } - sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER)); -} - -/* constant time memset using volitile to avoid having the memset - optimized out by the compiler. - Note: As suggested by Joachim Schipper (joachim.schipper@fox-it.com) -*/ -void* sqlcipher_memset(void *v, unsigned char value, int len) { - int i = 0; - volatile unsigned char *a = v; - - if (v == NULL) return v; - - for(i = 0; i < len; i++) { - a[i] = value; - } - - return v; -} - -/* constant time memory check tests every position of a memory segement - matches a single value (i.e. the memory is all zeros) - returns 0 if match, 1 of no match */ -int sqlcipher_ismemset(const void *v, unsigned char value, int len) { - const unsigned char *a = v; - int i = 0, result = 0; - - for(i = 0; i < len; i++) { - result |= a[i] ^ value; - } - - return (result != 0); -} - -/* constant time memory comparison routine. - returns 0 if match, 1 if no match */ -int sqlcipher_memcmp(const void *v0, const void *v1, int len) { - const unsigned char *a0 = v0, *a1 = v1; - int i = 0, result = 0; - - for(i = 0; i < len; i++) { - result |= a0[i] ^ a1[i]; - } - - return (result != 0); -} - -/** - * Free and wipe memory. Uses SQLites internal sqlite3_free so that memory - * can be countend and memory leak detection works in the test suite. - * If ptr is not null memory will be freed. - * If sz is greater than zero, the memory will be overwritten with zero before it is freed - * If sz is > 0, and not compiled with OMIT_MEMLOCK, system will attempt to unlock the - * memory segment so it can be paged - */ -void sqlcipher_free(void *ptr, int sz) { - if(ptr) { - if(sz > 0) { - sqlcipher_memset(ptr, 0, sz); -#ifndef OMIT_MEMLOCK -#if defined(__unix__) || defined(__APPLE__) - munlock(ptr, sz); -#elif defined(_WIN32) -#if !(defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP || WINAPI_FAMILY == WINAPI_FAMILY_APP)) -VirtualUnlock(ptr, sz); -#endif -#endif -#endif - } - sqlite3_free(ptr); - } -} - -/** - * allocate memory. Uses sqlite's internall malloc wrapper so memory can be - * reference counted and leak detection works. Unless compiled with OMIT_MEMLOCK - * attempts to lock the memory pages so sensitive information won't be swapped - */ -void* sqlcipher_malloc(int sz) { - void *ptr = sqlite3Malloc(sz); - sqlcipher_memset(ptr, 0, sz); -#ifndef OMIT_MEMLOCK - if(ptr) { -#if defined(__unix__) || defined(__APPLE__) - mlock(ptr, sz); -#elif defined(_WIN32) -#if !(defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP || WINAPI_FAMILY == WINAPI_FAMILY_APP)) - VirtualLock(ptr, sz); -#endif -#endif - } -#endif - return ptr; -} - - -/** - * Initialize new cipher_ctx struct. This function will allocate memory - * for the cipher context and for the key - * - * returns SQLITE_OK if initialization was successful - * returns SQLITE_NOMEM if an error occured allocating memory - */ -static int sqlcipher_cipher_ctx_init(cipher_ctx **iCtx) { - int rc; - cipher_ctx *ctx; - *iCtx = (cipher_ctx *) sqlcipher_malloc(sizeof(cipher_ctx)); - ctx = *iCtx; - if(ctx == NULL) return SQLITE_NOMEM; - - ctx->provider = (sqlcipher_provider *) sqlcipher_malloc(sizeof(sqlcipher_provider)); - if(ctx->provider == NULL) return SQLITE_NOMEM; - - /* make a copy of the provider to be used for the duration of the context */ - sqlite3_mutex_enter(sqlcipher_provider_mutex); - memcpy(ctx->provider, default_provider, sizeof(sqlcipher_provider)); - sqlite3_mutex_leave(sqlcipher_provider_mutex); - - if((rc = ctx->provider->ctx_init(&ctx->provider_ctx)) != SQLITE_OK) return rc; - ctx->key = (unsigned char *) sqlcipher_malloc(CIPHER_MAX_KEY_SZ); - ctx->hmac_key = (unsigned char *) sqlcipher_malloc(CIPHER_MAX_KEY_SZ); - if(ctx->key == NULL) return SQLITE_NOMEM; - if(ctx->hmac_key == NULL) return SQLITE_NOMEM; - - /* setup default flags */ - ctx->flags = default_flags; - - return SQLITE_OK; -} - -/** - * Free and wipe memory associated with a cipher_ctx - */ -static void sqlcipher_cipher_ctx_free(cipher_ctx **iCtx) { - cipher_ctx *ctx = *iCtx; - CODEC_TRACE(("cipher_ctx_free: entered iCtx=%p\n", iCtx)); - ctx->provider->ctx_free(&ctx->provider_ctx); - sqlcipher_free(ctx->provider, sizeof(sqlcipher_provider)); - sqlcipher_free(ctx->key, ctx->key_sz); - sqlcipher_free(ctx->hmac_key, ctx->key_sz); - sqlcipher_free(ctx->pass, ctx->pass_sz); - sqlcipher_free(ctx->keyspec, ctx->keyspec_sz); - sqlcipher_free(ctx, sizeof(cipher_ctx)); -} - -/** - * Compare one cipher_ctx to another. - * - * returns 0 if all the parameters (except the derived key data) are the same - * returns 1 otherwise - */ -static int sqlcipher_cipher_ctx_cmp(cipher_ctx *c1, cipher_ctx *c2) { - int are_equal = ( - c1->iv_sz == c2->iv_sz - && c1->kdf_iter == c2->kdf_iter - && c1->fast_kdf_iter == c2->fast_kdf_iter - && c1->key_sz == c2->key_sz - && c1->pass_sz == c2->pass_sz - && c1->flags == c2->flags - && c1->hmac_sz == c2->hmac_sz - && c1->provider->ctx_cmp(c1->provider_ctx, c2->provider_ctx) - && ( - c1->pass == c2->pass - || !sqlcipher_memcmp((const unsigned char*)c1->pass, - (const unsigned char*)c2->pass, - c1->pass_sz) - )); - - CODEC_TRACE(("sqlcipher_cipher_ctx_cmp: entered \ - c1=%p c2=%p \ - c1->iv_sz=%d c2->iv_sz=%d \ - c1->kdf_iter=%d c2->kdf_iter=%d \ - c1->fast_kdf_iter=%d c2->fast_kdf_iter=%d \ - c1->key_sz=%d c2->key_sz=%d \ - c1->pass_sz=%d c2->pass_sz=%d \ - c1->flags=%d c2->flags=%d \ - c1->hmac_sz=%d c2->hmac_sz=%d \ - c1->provider_ctx=%p c2->provider_ctx=%p \ - c1->pass=%p c2->pass=%p \ - c1->pass=%s c2->pass=%s \ - provider->ctx_cmp=%d \ - sqlcipher_memcmp=%d \ - are_equal=%d \ - \n", - c1, c2, - c1->iv_sz, c2->iv_sz, - c1->kdf_iter, c2->kdf_iter, - c1->fast_kdf_iter, c2->fast_kdf_iter, - c1->key_sz, c2->key_sz, - c1->pass_sz, c2->pass_sz, - c1->flags, c2->flags, - c1->hmac_sz, c2->hmac_sz, - c1->provider_ctx, c2->provider_ctx, - c1->pass, c2->pass, - c1->pass, c2->pass, - c1->provider->ctx_cmp(c1->provider_ctx, c2->provider_ctx), - sqlcipher_memcmp((const unsigned char*)c1->pass, - (const unsigned char*)c2->pass, - c1->pass_sz), - are_equal - )); - - return !are_equal; /* return 0 if they are the same, 1 otherwise */ -} - -/** - * Copy one cipher_ctx to another. For instance, assuming that read_ctx is a - * fully initialized context, you could copy it to write_ctx and all yet data - * and pass information across - * - * returns SQLITE_OK if initialization was successful - * returns SQLITE_NOMEM if an error occured allocating memory - */ -static int sqlcipher_cipher_ctx_copy(cipher_ctx *target, cipher_ctx *source) { - void *key = target->key; - void *hmac_key = target->hmac_key; - void *provider = target->provider; - void *provider_ctx = target->provider_ctx; - - CODEC_TRACE(("sqlcipher_cipher_ctx_copy: entered target=%p, source=%p\n", target, source)); - sqlcipher_free(target->pass, target->pass_sz); - sqlcipher_free(target->keyspec, target->keyspec_sz); - memcpy(target, source, sizeof(cipher_ctx)); - - target->key = key; //restore pointer to previously allocated key data - memcpy(target->key, source->key, CIPHER_MAX_KEY_SZ); - - target->hmac_key = hmac_key; //restore pointer to previously allocated hmac key data - memcpy(target->hmac_key, source->hmac_key, CIPHER_MAX_KEY_SZ); - - target->provider = provider; // restore pointer to previouly allocated provider; - memcpy(target->provider, source->provider, sizeof(sqlcipher_provider)); - - target->provider_ctx = provider_ctx; // restore pointer to previouly allocated provider context; - target->provider->ctx_copy(target->provider_ctx, source->provider_ctx); - - if(source->pass && source->pass_sz) { - target->pass = sqlcipher_malloc(source->pass_sz); - if(target->pass == NULL) return SQLITE_NOMEM; - memcpy(target->pass, source->pass, source->pass_sz); - } - if(source->keyspec && source->keyspec_sz) { - target->keyspec = sqlcipher_malloc(source->keyspec_sz); - if(target->keyspec == NULL) return SQLITE_NOMEM; - memcpy(target->keyspec, source->keyspec, source->keyspec_sz); - } - return SQLITE_OK; -} - -/** - * Set the keyspec for the cipher_ctx - * - * returns SQLITE_OK if assignment was successfull - * returns SQLITE_NOMEM if an error occured allocating memory - */ -static int sqlcipher_cipher_ctx_set_keyspec(cipher_ctx *ctx, const unsigned char *key, int key_sz, const unsigned char *salt, int salt_sz) { - - /* free, zero existing pointers and size */ - sqlcipher_free(ctx->keyspec, ctx->keyspec_sz); - ctx->keyspec = NULL; - ctx->keyspec_sz = 0; - - /* establic a hex-formated key specification, containing the raw encryption key and - the salt used to generate it */ - ctx->keyspec_sz = ((key_sz + salt_sz) * 2) + 3; - ctx->keyspec = sqlcipher_malloc(ctx->keyspec_sz); - if(ctx->keyspec == NULL) return SQLITE_NOMEM; - - ctx->keyspec[0] = 'x'; - ctx->keyspec[1] = '\''; - cipher_bin2hex(key, key_sz, ctx->keyspec + 2); - cipher_bin2hex(salt, salt_sz, ctx->keyspec + (key_sz * 2) + 2); - ctx->keyspec[ctx->keyspec_sz - 1] = '\''; - return SQLITE_OK; -} - -int sqlcipher_codec_get_store_pass(codec_ctx *ctx) { - return ctx->read_ctx->store_pass; -} - -void sqlcipher_codec_set_store_pass(codec_ctx *ctx, int value) { - ctx->read_ctx->store_pass = value; -} - -void sqlcipher_codec_get_pass(codec_ctx *ctx, void **zKey, int *nKey) { - *zKey = ctx->read_ctx->pass; - *nKey = ctx->read_ctx->pass_sz; -} - -/** - * Set the passphrase for the cipher_ctx - * - * returns SQLITE_OK if assignment was successfull - * returns SQLITE_NOMEM if an error occured allocating memory - */ -static int sqlcipher_cipher_ctx_set_pass(cipher_ctx *ctx, const void *zKey, int nKey) { - - /* free, zero existing pointers and size */ - sqlcipher_free(ctx->pass, ctx->pass_sz); - ctx->pass = NULL; - ctx->pass_sz = 0; - - if(zKey && nKey) { /* if new password is provided, copy it */ - ctx->pass_sz = nKey; - ctx->pass = sqlcipher_malloc(nKey); - if(ctx->pass == NULL) return SQLITE_NOMEM; - memcpy(ctx->pass, zKey, nKey); - } - return SQLITE_OK; -} - -int sqlcipher_codec_ctx_set_pass(codec_ctx *ctx, const void *zKey, int nKey, int for_ctx) { - cipher_ctx *c_ctx = for_ctx ? ctx->write_ctx : ctx->read_ctx; - int rc; - - if((rc = sqlcipher_cipher_ctx_set_pass(c_ctx, zKey, nKey)) != SQLITE_OK) return rc; - c_ctx->derive_key = 1; - - if(for_ctx == 2) - if((rc = sqlcipher_cipher_ctx_copy( for_ctx ? ctx->read_ctx : ctx->write_ctx, c_ctx)) != SQLITE_OK) - return rc; - - return SQLITE_OK; -} - -int sqlcipher_codec_ctx_set_cipher(codec_ctx *ctx, const char *cipher_name, int for_ctx) { - cipher_ctx *c_ctx = for_ctx ? ctx->write_ctx : ctx->read_ctx; - int rc; - - c_ctx->provider->set_cipher(c_ctx->provider_ctx, cipher_name); - - c_ctx->key_sz = c_ctx->provider->get_key_sz(c_ctx->provider_ctx); - c_ctx->iv_sz = c_ctx->provider->get_iv_sz(c_ctx->provider_ctx); - c_ctx->block_sz = c_ctx->provider->get_block_sz(c_ctx->provider_ctx); - c_ctx->hmac_sz = c_ctx->provider->get_hmac_sz(c_ctx->provider_ctx); - c_ctx->derive_key = 1; - - if(for_ctx == 2) - if((rc = sqlcipher_cipher_ctx_copy( for_ctx ? ctx->read_ctx : ctx->write_ctx, c_ctx)) != SQLITE_OK) - return rc; - - return SQLITE_OK; -} - -const char* sqlcipher_codec_ctx_get_cipher(codec_ctx *ctx, int for_ctx) { - cipher_ctx *c_ctx = for_ctx ? ctx->write_ctx : ctx->read_ctx; - return c_ctx->provider->get_cipher(c_ctx->provider_ctx); -} - -/* set the global default KDF iteration */ -void sqlcipher_set_default_kdf_iter(int iter) { - default_kdf_iter = iter; -} - -int sqlcipher_get_default_kdf_iter() { - return default_kdf_iter; -} - -int sqlcipher_codec_ctx_set_kdf_iter(codec_ctx *ctx, int kdf_iter, int for_ctx) { - cipher_ctx *c_ctx = for_ctx ? ctx->write_ctx : ctx->read_ctx; - int rc; - - c_ctx->kdf_iter = kdf_iter; - c_ctx->derive_key = 1; - - if(for_ctx == 2) - if((rc = sqlcipher_cipher_ctx_copy( for_ctx ? ctx->read_ctx : ctx->write_ctx, c_ctx)) != SQLITE_OK) - return rc; - - return SQLITE_OK; -} - -int sqlcipher_codec_ctx_get_kdf_iter(codec_ctx *ctx, int for_ctx) { - cipher_ctx *c_ctx = for_ctx ? ctx->write_ctx : ctx->read_ctx; - return c_ctx->kdf_iter; -} - -int sqlcipher_codec_ctx_set_fast_kdf_iter(codec_ctx *ctx, int fast_kdf_iter, int for_ctx) { - cipher_ctx *c_ctx = for_ctx ? ctx->write_ctx : ctx->read_ctx; - int rc; - - c_ctx->fast_kdf_iter = fast_kdf_iter; - c_ctx->derive_key = 1; - - if(for_ctx == 2) - if((rc = sqlcipher_cipher_ctx_copy( for_ctx ? ctx->read_ctx : ctx->write_ctx, c_ctx)) != SQLITE_OK) - return rc; - - return SQLITE_OK; -} - -int sqlcipher_codec_ctx_get_fast_kdf_iter(codec_ctx *ctx, int for_ctx) { - cipher_ctx *c_ctx = for_ctx ? ctx->write_ctx : ctx->read_ctx; - return c_ctx->fast_kdf_iter; -} - -/* set the global default flag for HMAC */ -void sqlcipher_set_default_use_hmac(int use) { - if(use) default_flags |= CIPHER_FLAG_HMAC; - else default_flags &= ~CIPHER_FLAG_HMAC; -} - -int sqlcipher_get_default_use_hmac() { - return (default_flags & CIPHER_FLAG_HMAC) != 0; -} - -void sqlcipher_set_hmac_salt_mask(unsigned char mask) { - hmac_salt_mask = mask; -} - -unsigned char sqlcipher_get_hmac_salt_mask() { - return hmac_salt_mask; -} - -/* set the codec flag for whether this individual database should be using hmac */ -int sqlcipher_codec_ctx_set_use_hmac(codec_ctx *ctx, int use) { - int reserve = CIPHER_MAX_IV_SZ; /* base reserve size will be IV only */ - - if(use) reserve += ctx->read_ctx->hmac_sz; /* if reserve will include hmac, update that size */ - - /* calculate the amount of reserve needed in even increments of the cipher block size */ - - reserve = ((reserve % ctx->read_ctx->block_sz) == 0) ? reserve : - ((reserve / ctx->read_ctx->block_sz) + 1) * ctx->read_ctx->block_sz; - - CODEC_TRACE(("sqlcipher_codec_ctx_set_use_hmac: use=%d block_sz=%d md_size=%d reserve=%d\n", - use, ctx->read_ctx->block_sz, ctx->read_ctx->hmac_sz, reserve)); - - - if(use) { - sqlcipher_codec_ctx_set_flag(ctx, CIPHER_FLAG_HMAC); - } else { - sqlcipher_codec_ctx_unset_flag(ctx, CIPHER_FLAG_HMAC); - } - - ctx->write_ctx->reserve_sz = ctx->read_ctx->reserve_sz = reserve; - - return SQLITE_OK; -} - -int sqlcipher_codec_ctx_get_use_hmac(codec_ctx *ctx, int for_ctx) { - cipher_ctx * c_ctx = for_ctx ? ctx->write_ctx : ctx->read_ctx; - return (c_ctx->flags & CIPHER_FLAG_HMAC) != 0; -} - -int sqlcipher_codec_ctx_set_flag(codec_ctx *ctx, unsigned int flag) { - ctx->write_ctx->flags |= flag; - ctx->read_ctx->flags |= flag; - return SQLITE_OK; -} - -int sqlcipher_codec_ctx_unset_flag(codec_ctx *ctx, unsigned int flag) { - ctx->write_ctx->flags &= ~flag; - ctx->read_ctx->flags &= ~flag; - return SQLITE_OK; -} - -int sqlcipher_codec_ctx_get_flag(codec_ctx *ctx, unsigned int flag, int for_ctx) { - cipher_ctx * c_ctx = for_ctx ? ctx->write_ctx : ctx->read_ctx; - return (c_ctx->flags & flag) != 0; -} - -void sqlcipher_codec_ctx_set_error(codec_ctx *ctx, int error) { - CODEC_TRACE(("sqlcipher_codec_ctx_set_error: ctx=%p, error=%d\n", ctx, error)); - sqlite3pager_sqlite3PagerSetError(ctx->pBt->pBt->pPager, error); - ctx->pBt->pBt->db->errCode = error; -} - -int sqlcipher_codec_ctx_get_reservesize(codec_ctx *ctx) { - return ctx->read_ctx->reserve_sz; -} - -void* sqlcipher_codec_ctx_get_data(codec_ctx *ctx) { - return ctx->buffer; -} - -void* sqlcipher_codec_ctx_get_kdf_salt(codec_ctx *ctx) { - return ctx->kdf_salt; -} - -void sqlcipher_codec_get_keyspec(codec_ctx *ctx, void **zKey, int *nKey) { - *zKey = ctx->read_ctx->keyspec; - *nKey = ctx->read_ctx->keyspec_sz; -} - -int sqlcipher_codec_ctx_set_pagesize(codec_ctx *ctx, int size) { - /* attempt to free the existing page buffer */ - sqlcipher_free(ctx->buffer,ctx->page_sz); - ctx->page_sz = size; - - /* pre-allocate a page buffer of PageSize bytes. This will - be used as a persistent buffer for encryption and decryption - operations to avoid overhead of multiple memory allocations*/ - ctx->buffer = sqlcipher_malloc(size); - if(ctx->buffer == NULL) return SQLITE_NOMEM; - - return SQLITE_OK; -} - -int sqlcipher_codec_ctx_get_pagesize(codec_ctx *ctx) { - return ctx->page_sz; -} - -void sqlcipher_set_default_pagesize(int page_size) { - default_page_size = page_size; -} - -int sqlcipher_get_default_pagesize() { - return default_page_size; -} - -int sqlcipher_codec_ctx_init(codec_ctx **iCtx, Db *pDb, Pager *pPager, sqlite3_file *fd, const void *zKey, int nKey) { - int rc; - codec_ctx *ctx; - *iCtx = sqlcipher_malloc(sizeof(codec_ctx)); - ctx = *iCtx; - - if(ctx == NULL) return SQLITE_NOMEM; - - ctx->pBt = pDb->pBt; /* assign pointer to database btree structure */ - - /* allocate space for salt data. Then read the first 16 bytes - directly off the database file. This is the salt for the - key derivation function. If we get a short read allocate - a new random salt value */ - ctx->kdf_salt_sz = FILE_HEADER_SZ; - ctx->kdf_salt = sqlcipher_malloc(ctx->kdf_salt_sz); - if(ctx->kdf_salt == NULL) return SQLITE_NOMEM; - - /* allocate space for separate hmac salt data. We want the - HMAC derivation salt to be different than the encryption - key derivation salt */ - ctx->hmac_kdf_salt = sqlcipher_malloc(ctx->kdf_salt_sz); - if(ctx->hmac_kdf_salt == NULL) return SQLITE_NOMEM; - - - /* - Always overwrite page size and set to the default because the first page of the database - in encrypted and thus sqlite can't effectively determine the pagesize. this causes an issue in - cases where bytes 16 & 17 of the page header are a power of 2 as reported by John Lehman - */ - if((rc = sqlcipher_codec_ctx_set_pagesize(ctx, default_page_size)) != SQLITE_OK) return rc; - - if((rc = sqlcipher_cipher_ctx_init(&ctx->read_ctx)) != SQLITE_OK) return rc; - if((rc = sqlcipher_cipher_ctx_init(&ctx->write_ctx)) != SQLITE_OK) return rc; - - if(fd == NULL || sqlite3OsRead(fd, ctx->kdf_salt, FILE_HEADER_SZ, 0) != SQLITE_OK) { - ctx->need_kdf_salt = 1; - } - - if((rc = sqlcipher_codec_ctx_set_cipher(ctx, CIPHER, 0)) != SQLITE_OK) return rc; - if((rc = sqlcipher_codec_ctx_set_kdf_iter(ctx, default_kdf_iter, 0)) != SQLITE_OK) return rc; - if((rc = sqlcipher_codec_ctx_set_fast_kdf_iter(ctx, FAST_PBKDF2_ITER, 0)) != SQLITE_OK) return rc; - if((rc = sqlcipher_codec_ctx_set_pass(ctx, zKey, nKey, 0)) != SQLITE_OK) return rc; - - /* Note that use_hmac is a special case that requires recalculation of page size - so we call set_use_hmac to perform setup */ - if((rc = sqlcipher_codec_ctx_set_use_hmac(ctx, default_flags & CIPHER_FLAG_HMAC)) != SQLITE_OK) return rc; - - if((rc = sqlcipher_cipher_ctx_copy(ctx->write_ctx, ctx->read_ctx)) != SQLITE_OK) return rc; - - return SQLITE_OK; -} - -/** - * Free and wipe memory associated with a cipher_ctx, including the allocated - * read_ctx and write_ctx. - */ -void sqlcipher_codec_ctx_free(codec_ctx **iCtx) { - codec_ctx *ctx = *iCtx; - CODEC_TRACE(("codec_ctx_free: entered iCtx=%p\n", iCtx)); - sqlcipher_free(ctx->kdf_salt, ctx->kdf_salt_sz); - sqlcipher_free(ctx->hmac_kdf_salt, ctx->kdf_salt_sz); - sqlcipher_free(ctx->buffer, 0); - sqlcipher_cipher_ctx_free(&ctx->read_ctx); - sqlcipher_cipher_ctx_free(&ctx->write_ctx); - sqlcipher_free(ctx, sizeof(codec_ctx)); -} - -/** convert a 32bit unsigned integer to little endian byte ordering */ -static void sqlcipher_put4byte_le(unsigned char *p, u32 v) { - p[0] = (u8)v; - p[1] = (u8)(v>>8); - p[2] = (u8)(v>>16); - p[3] = (u8)(v>>24); -} - -static int sqlcipher_page_hmac(cipher_ctx *ctx, Pgno pgno, unsigned char *in, int in_sz, unsigned char *out) { - unsigned char pgno_raw[sizeof(pgno)]; - /* we may convert page number to consistent representation before calculating MAC for - compatibility across big-endian and little-endian platforms. - - Note: The public release of sqlcipher 2.0.0 to 2.0.6 had a bug where the bytes of pgno - were used directly in the MAC. SQLCipher convert's to little endian by default to preserve - backwards compatibility on the most popular platforms, but can optionally be configured - to use either big endian or native byte ordering via pragma. */ - - if(ctx->flags & CIPHER_FLAG_LE_PGNO) { /* compute hmac using little endian pgno*/ - sqlcipher_put4byte_le(pgno_raw, pgno); - } else if(ctx->flags & CIPHER_FLAG_BE_PGNO) { /* compute hmac using big endian pgno */ - sqlite3Put4byte(pgno_raw, pgno); /* sqlite3Put4byte converts 32bit uint to big endian */ - } else { /* use native byte ordering */ - memcpy(pgno_raw, &pgno, sizeof(pgno)); - } - - /* include the encrypted page data, initialization vector, and page number in HMAC. This will - prevent both tampering with the ciphertext, manipulation of the IV, or resequencing otherwise - valid pages out of order in a database */ - ctx->provider->hmac( - ctx->provider_ctx, ctx->hmac_key, - ctx->key_sz, in, - in_sz, (unsigned char*) &pgno_raw, - sizeof(pgno), out); - return SQLITE_OK; -} - -/* - * ctx - codec context - * pgno - page number in database - * size - size in bytes of input and output buffers - * mode - 1 to encrypt, 0 to decrypt - * in - pointer to input bytes - * out - pouter to output bytes - */ -int sqlcipher_page_cipher(codec_ctx *ctx, int for_ctx, Pgno pgno, int mode, int page_sz, unsigned char *in, unsigned char *out) { - cipher_ctx *c_ctx = for_ctx ? ctx->write_ctx : ctx->read_ctx; - unsigned char *iv_in, *iv_out, *hmac_in, *hmac_out, *out_start; - int size; - - /* calculate some required positions into various buffers */ - size = page_sz - c_ctx->reserve_sz; /* adjust size to useable size and memset reserve at end of page */ - iv_out = out + size; - iv_in = in + size; - - /* hmac will be written immediately after the initialization vector. the remainder of the page reserve will contain - random bytes. note, these pointers are only valid when using hmac */ - hmac_in = in + size + c_ctx->iv_sz; - hmac_out = out + size + c_ctx->iv_sz; - out_start = out; /* note the original position of the output buffer pointer, as out will be rewritten during encryption */ - - CODEC_TRACE(("codec_cipher:entered pgno=%d, mode=%d, size=%d\n", pgno, mode, size)); - CODEC_HEXDUMP("codec_cipher: input page data", in, page_sz); - - /* the key size should never be zero. If it is, error out. */ - if(c_ctx->key_sz == 0) { - CODEC_TRACE(("codec_cipher: error possible context corruption, key_sz is zero for pgno=%d\n", pgno)); - sqlcipher_memset(out, 0, page_sz); - return SQLITE_ERROR; - } - - if(mode == CIPHER_ENCRYPT) { - /* start at front of the reserve block, write random data to the end */ - if(c_ctx->provider->random(c_ctx->provider_ctx, iv_out, c_ctx->reserve_sz) != SQLITE_OK) return SQLITE_ERROR; - } else { /* CIPHER_DECRYPT */ - memcpy(iv_out, iv_in, c_ctx->iv_sz); /* copy the iv from the input to output buffer */ - } - - if((c_ctx->flags & CIPHER_FLAG_HMAC) && (mode == CIPHER_DECRYPT) && !ctx->skip_read_hmac) { - if(sqlcipher_page_hmac(c_ctx, pgno, in, size + c_ctx->iv_sz, hmac_out) != SQLITE_OK) { - sqlcipher_memset(out, 0, page_sz); - CODEC_TRACE(("codec_cipher: hmac operations failed for pgno=%d\n", pgno)); - return SQLITE_ERROR; - } - - CODEC_TRACE(("codec_cipher: comparing hmac on in=%p out=%p hmac_sz=%d\n", hmac_in, hmac_out, c_ctx->hmac_sz)); - if(sqlcipher_memcmp(hmac_in, hmac_out, c_ctx->hmac_sz) != 0) { /* the hmac check failed */ - if(sqlcipher_ismemset(in, 0, page_sz) == 0) { - /* first check if the entire contents of the page is zeros. If so, this page - resulted from a short read (i.e. sqlite attempted to pull a page after the end of the file. these - short read failures must be ignored for autovaccum mode to work so wipe the output buffer - and return SQLITE_OK to skip the decryption step. */ - CODEC_TRACE(("codec_cipher: zeroed page (short read) for pgno %d, encryption but returning SQLITE_OK\n", pgno)); - sqlcipher_memset(out, 0, page_sz); - return SQLITE_OK; - } else { - /* if the page memory is not all zeros, it means the there was data and a hmac on the page. - since the check failed, the page was either tampered with or corrupted. wipe the output buffer, - and return SQLITE_ERROR to the caller */ - CODEC_TRACE(("codec_cipher: hmac check failed for pgno=%d returning SQLITE_ERROR\n", pgno)); - sqlcipher_memset(out, 0, page_sz); - return SQLITE_ERROR; - } - } - } - - c_ctx->provider->cipher(c_ctx->provider_ctx, mode, c_ctx->key, c_ctx->key_sz, iv_out, in, size, out); - - if((c_ctx->flags & CIPHER_FLAG_HMAC) && (mode == CIPHER_ENCRYPT)) { - sqlcipher_page_hmac(c_ctx, pgno, out_start, size + c_ctx->iv_sz, hmac_out); - } - - CODEC_HEXDUMP("codec_cipher: output page data", out_start, page_sz); - - return SQLITE_OK; -} - -/** - * Derive an encryption key for a cipher contex key based on the raw password. - * - * If the raw key data is formated as x'hex' and there are exactly enough hex chars to fill - * the key (i.e 64 hex chars for a 256 bit key) then the key data will be used directly. - - * Else, if the raw key data is formated as x'hex' and there are exactly enough hex chars to fill - * the key and the salt (i.e 92 hex chars for a 256 bit key and 16 byte salt) then it will be unpacked - * as the key followed by the salt. - * - * Otherwise, a key data will be derived using PBKDF2 - * - * returns SQLITE_OK if initialization was successful - * returns SQLITE_ERROR if the key could't be derived (for instance if pass is NULL or pass_sz is 0) - */ -static int sqlcipher_cipher_ctx_key_derive(codec_ctx *ctx, cipher_ctx *c_ctx) { - int rc; - CODEC_TRACE(("cipher_ctx_key_derive: entered c_ctx->pass=%s, c_ctx->pass_sz=%d \ - ctx->kdf_salt=%p ctx->kdf_salt_sz=%d c_ctx->kdf_iter=%d \ - ctx->hmac_kdf_salt=%p, c_ctx->fast_kdf_iter=%d c_ctx->key_sz=%d\n", - c_ctx->pass, c_ctx->pass_sz, ctx->kdf_salt, ctx->kdf_salt_sz, c_ctx->kdf_iter, - ctx->hmac_kdf_salt, c_ctx->fast_kdf_iter, c_ctx->key_sz)); - - - if(c_ctx->pass && c_ctx->pass_sz) { // if pass is not null - - if(ctx->need_kdf_salt) { - if(ctx->read_ctx->provider->random(ctx->read_ctx->provider_ctx, ctx->kdf_salt, FILE_HEADER_SZ) != SQLITE_OK) return SQLITE_ERROR; - ctx->need_kdf_salt = 0; - } - if (c_ctx->pass_sz == ((c_ctx->key_sz * 2) + 3) && sqlite3StrNICmp((const char *)c_ctx->pass ,"x'", 2) == 0 && cipher_isHex(c_ctx->pass + 2, c_ctx->key_sz * 2)) { - int n = c_ctx->pass_sz - 3; /* adjust for leading x' and tailing ' */ - const unsigned char *z = c_ctx->pass + 2; /* adjust lead offset of x' */ - CODEC_TRACE(("cipher_ctx_key_derive: using raw key from hex\n")); - cipher_hex2bin(z, n, c_ctx->key); - } else if (c_ctx->pass_sz == (((c_ctx->key_sz + ctx->kdf_salt_sz) * 2) + 3) && sqlite3StrNICmp((const char *)c_ctx->pass ,"x'", 2) == 0 && cipher_isHex(c_ctx->pass + 2, (c_ctx->key_sz + ctx->kdf_salt_sz) * 2)) { - const unsigned char *z = c_ctx->pass + 2; /* adjust lead offset of x' */ - CODEC_TRACE(("cipher_ctx_key_derive: using raw key from hex\n")); - cipher_hex2bin(z, (c_ctx->key_sz * 2), c_ctx->key); - cipher_hex2bin(z + (c_ctx->key_sz * 2), (ctx->kdf_salt_sz * 2), ctx->kdf_salt); - } else { - CODEC_TRACE(("cipher_ctx_key_derive: deriving key using full PBKDF2 with %d iterations\n", c_ctx->kdf_iter)); - c_ctx->provider->kdf(c_ctx->provider_ctx, c_ctx->pass, c_ctx->pass_sz, - ctx->kdf_salt, ctx->kdf_salt_sz, c_ctx->kdf_iter, - c_ctx->key_sz, c_ctx->key); - } - - /* set the context "keyspec" containing the hex-formatted key and salt to be used when attaching databases */ - if((rc = sqlcipher_cipher_ctx_set_keyspec(c_ctx, c_ctx->key, c_ctx->key_sz, ctx->kdf_salt, ctx->kdf_salt_sz)) != SQLITE_OK) return rc; - - /* if this context is setup to use hmac checks, generate a seperate and different - key for HMAC. In this case, we use the output of the previous KDF as the input to - this KDF run. This ensures a distinct but predictable HMAC key. */ - if(c_ctx->flags & CIPHER_FLAG_HMAC) { - int i; - - /* start by copying the kdf key into the hmac salt slot - then XOR it with the fixed hmac salt defined at compile time - this ensures that the salt passed in to derive the hmac key, while - easy to derive and publically known, is not the same as the salt used - to generate the encryption key */ - memcpy(ctx->hmac_kdf_salt, ctx->kdf_salt, ctx->kdf_salt_sz); - for(i = 0; i < ctx->kdf_salt_sz; i++) { - ctx->hmac_kdf_salt[i] ^= hmac_salt_mask; - } - - CODEC_TRACE(("cipher_ctx_key_derive: deriving hmac key from encryption key using PBKDF2 with %d iterations\n", - c_ctx->fast_kdf_iter)); - - - c_ctx->provider->kdf(c_ctx->provider_ctx, c_ctx->key, c_ctx->key_sz, - ctx->hmac_kdf_salt, ctx->kdf_salt_sz, c_ctx->fast_kdf_iter, - c_ctx->key_sz, c_ctx->hmac_key); - } - - c_ctx->derive_key = 0; - return SQLITE_OK; - }; - return SQLITE_ERROR; -} - -int sqlcipher_codec_key_derive(codec_ctx *ctx) { - /* derive key on first use if necessary */ - if(ctx->read_ctx->derive_key) { - if(sqlcipher_cipher_ctx_key_derive(ctx, ctx->read_ctx) != SQLITE_OK) return SQLITE_ERROR; - } - - if(ctx->write_ctx->derive_key) { - if(sqlcipher_cipher_ctx_cmp(ctx->write_ctx, ctx->read_ctx) == 0) { - /* the relevant parameters are the same, just copy read key */ - if(sqlcipher_cipher_ctx_copy(ctx->write_ctx, ctx->read_ctx) != SQLITE_OK) return SQLITE_ERROR; - } else { - if(sqlcipher_cipher_ctx_key_derive(ctx, ctx->write_ctx) != SQLITE_OK) return SQLITE_ERROR; - } - } - - /* TODO: wipe and free passphrase after key derivation */ - if(ctx->read_ctx->store_pass != 1) { - sqlcipher_cipher_ctx_set_pass(ctx->read_ctx, NULL, 0); - sqlcipher_cipher_ctx_set_pass(ctx->write_ctx, NULL, 0); - } - - return SQLITE_OK; -} - -int sqlcipher_codec_key_copy(codec_ctx *ctx, int source) { - if(source == CIPHER_READ_CTX) { - return sqlcipher_cipher_ctx_copy(ctx->write_ctx, ctx->read_ctx); - } else { - return sqlcipher_cipher_ctx_copy(ctx->read_ctx, ctx->write_ctx); - } -} - -const char* sqlcipher_codec_get_cipher_provider(codec_ctx *ctx) { - return ctx->read_ctx->provider->get_provider_name(ctx->read_ctx); -} - - -static int sqlcipher_check_connection(const char *filename, char *key, int key_sz, char *sql, int *user_version) { - int rc; - sqlite3 *db = NULL; - sqlite3_stmt *statement = NULL; - char *query_user_version = "PRAGMA user_version;"; - - rc = sqlite3_open(filename, &db); - if(rc != SQLITE_OK){ - goto cleanup; - } - rc = sqlite3_key(db, key, key_sz); - if(rc != SQLITE_OK){ - goto cleanup; - } - rc = sqlite3_exec(db, sql, NULL, NULL, NULL); - if(rc != SQLITE_OK){ - goto cleanup; - } - rc = sqlite3_prepare(db, query_user_version, -1, &statement, NULL); - if(rc != SQLITE_OK){ - goto cleanup; - } - rc = sqlite3_step(statement); - if(rc == SQLITE_ROW){ - *user_version = sqlite3_column_int(statement, 0); - rc = SQLITE_OK; - } - -cleanup: - if(statement){ - sqlite3_finalize(statement); - } - if(db){ - sqlite3_close(db); - } - return rc; -} - -int sqlcipher_codec_ctx_migrate(codec_ctx *ctx) { - u32 meta; - int rc = 0; - int command_idx = 0; - int password_sz; - int saved_flags; - int saved_nChange; - int saved_nTotalChange; - void (*saved_xTrace)(void*,const char*); - Db *pDb = 0; - sqlite3 *db = ctx->pBt->db; - const char *db_filename = sqlite3_db_filename(db, "main"); - char *migrated_db_filename = sqlite3_mprintf("%s-migrated", db_filename); - char *pragma_hmac_off = "PRAGMA cipher_use_hmac = OFF;"; - char *pragma_4k_kdf_iter = "PRAGMA kdf_iter = 4000;"; - char *pragma_1x_and_4k; - char *set_user_version; - char *key; - int key_sz; - int user_version = 0; - int upgrade_1x_format = 0; - int upgrade_4k_format = 0; - static const unsigned char aCopy[] = { - BTREE_SCHEMA_VERSION, 1, /* Add one to the old schema cookie */ - BTREE_DEFAULT_CACHE_SIZE, 0, /* Preserve the default page cache size */ - BTREE_TEXT_ENCODING, 0, /* Preserve the text encoding */ - BTREE_USER_VERSION, 0, /* Preserve the user version */ - BTREE_APPLICATION_ID, 0, /* Preserve the application id */ - }; - - - key_sz = ctx->read_ctx->pass_sz + 1; - key = sqlcipher_malloc(key_sz); - memset(key, 0, key_sz); - memcpy(key, ctx->read_ctx->pass, ctx->read_ctx->pass_sz); - - if(db_filename){ - const char* commands[5]; - char *attach_command = sqlite3_mprintf("ATTACH DATABASE '%s-migrated' as migrate KEY '%q';", - db_filename, key); - - int rc = sqlcipher_check_connection(db_filename, key, ctx->read_ctx->pass_sz, "", &user_version); - if(rc == SQLITE_OK){ - CODEC_TRACE(("No upgrade required - exiting\n")); - goto exit; - } - - // Version 2 - check for 4k with hmac format - rc = sqlcipher_check_connection(db_filename, key, ctx->read_ctx->pass_sz, pragma_4k_kdf_iter, &user_version); - if(rc == SQLITE_OK) { - CODEC_TRACE(("Version 2 format found\n")); - upgrade_4k_format = 1; - } - - // Version 1 - check both no hmac and 4k together - pragma_1x_and_4k = sqlite3_mprintf("%s%s", pragma_hmac_off, - pragma_4k_kdf_iter); - rc = sqlcipher_check_connection(db_filename, key, ctx->read_ctx->pass_sz, pragma_1x_and_4k, &user_version); - sqlite3_free(pragma_1x_and_4k); - if(rc == SQLITE_OK) { - CODEC_TRACE(("Version 1 format found\n")); - upgrade_1x_format = 1; - upgrade_4k_format = 1; - } - - if(upgrade_1x_format == 0 && upgrade_4k_format == 0) { - CODEC_TRACE(("Upgrade format not determined\n")); - goto handle_error; - } - - set_user_version = sqlite3_mprintf("PRAGMA migrate.user_version = %d;", user_version); - commands[0] = upgrade_4k_format == 1 ? pragma_4k_kdf_iter : ""; - commands[1] = upgrade_1x_format == 1 ? pragma_hmac_off : ""; - commands[2] = attach_command; - commands[3] = "SELECT sqlcipher_export('migrate');"; - commands[4] = set_user_version; - - for(command_idx = 0; command_idx < ArraySize(commands); command_idx++){ - const char *command = commands[command_idx]; - if(strcmp(command, "") == 0){ - continue; - } - rc = sqlite3_exec(db, command, NULL, NULL, NULL); - if(rc != SQLITE_OK){ - break; - } - } - sqlite3_free(attach_command); - sqlite3_free(set_user_version); - sqlcipher_free(key, key_sz); - - if(rc == SQLITE_OK){ - Btree *pDest; - Btree *pSrc; - int i = 0; - - if( !db->autoCommit ){ - CODEC_TRACE(("cannot migrate from within a transaction")); - goto handle_error; - } - if( db->nVdbeActive>1 ){ - CODEC_TRACE(("cannot migrate - SQL statements in progress")); - goto handle_error; - } - - /* Save the current value of the database flags so that it can be - ** restored before returning. Then set the writable-schema flag, and - ** disable CHECK and foreign key constraints. */ - saved_flags = db->flags; - saved_nChange = db->nChange; - saved_nTotalChange = db->nTotalChange; - saved_xTrace = db->xTrace; - db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks | SQLITE_PreferBuiltin; - db->flags &= ~(SQLITE_ForeignKeys | SQLITE_ReverseOrder); - db->xTrace = 0; - - pDest = db->aDb[0].pBt; - pDb = &(db->aDb[db->nDb-1]); - pSrc = pDb->pBt; - - rc = sqlite3_exec(db, "BEGIN;", NULL, NULL, NULL); - rc = sqlite3BtreeBeginTrans(pSrc, 2); - rc = sqlite3BtreeBeginTrans(pDest, 2); - - assert( 1==sqlite3BtreeIsInTrans(pDest) ); - assert( 1==sqlite3BtreeIsInTrans(pSrc) ); - - sqlite3CodecGetKey(db, db->nDb - 1, (void**)&key, &password_sz); - sqlite3CodecAttach(db, 0, key, password_sz); - sqlite3pager_get_codec(pDest->pBt->pPager, (void**)&ctx); - - ctx->skip_read_hmac = 1; - for(i=0; iskip_read_hmac = 0; - if( rc!=SQLITE_OK ) goto handle_error; - rc = sqlite3BtreeCommit(pDest); - - db->flags = saved_flags; - db->nChange = saved_nChange; - db->nTotalChange = saved_nTotalChange; - db->xTrace = saved_xTrace; - db->autoCommit = 1; - sqlite3BtreeClose(pDb->pBt); - pDb->pBt = 0; - pDb->pSchema = 0; - sqlite3ResetAllSchemasOfConnection(db); - remove(migrated_db_filename); - sqlite3_free(migrated_db_filename); - } else { - CODEC_TRACE(("*** migration failure** \n\n")); - } - - } - goto exit; - - handle_error: - CODEC_TRACE(("An error occurred attempting to migrate the database\n")); - rc = SQLITE_ERROR; - - exit: - return rc; -} - -int sqlcipher_codec_add_random(codec_ctx *ctx, const char *zRight, int random_sz){ - const char *suffix = &zRight[random_sz-1]; - int n = random_sz - 3; /* adjust for leading x' and tailing ' */ - if (n > 0 && - sqlite3StrNICmp((const char *)zRight ,"x'", 2) == 0 && - sqlite3StrNICmp(suffix, "'", 1) == 0 && - n % 2 == 0) { - int rc = 0; - int buffer_sz = n / 2; - unsigned char *random; - const unsigned char *z = (const unsigned char *)zRight + 2; /* adjust lead offset of x' */ - CODEC_TRACE(("sqlcipher_codec_add_random: using raw random blob from hex\n")); - random = sqlcipher_malloc(buffer_sz); - memset(random, 0, buffer_sz); - cipher_hex2bin(z, n, random); - rc = ctx->read_ctx->provider->add_random(ctx->read_ctx->provider_ctx, random, buffer_sz); - sqlcipher_free(random, buffer_sz); - return rc; - } - return SQLITE_ERROR; -} - -int sqlcipher_cipher_profile(sqlite3 *db, const char *destination){ - FILE *f; - if( strcmp(destination,"stdout")==0 ){ - f = stdout; - }else if( strcmp(destination, "stderr")==0 ){ - f = stderr; - }else if( strcmp(destination, "off")==0 ){ - f = 0; - }else{ - f = fopen(destination, "wb"); - if( f==0 ){ - return SQLITE_ERROR; - } - } - sqlite3_profile(db, sqlcipher_profile_callback, f); - return SQLITE_OK; -} - -static void sqlcipher_profile_callback(void *file, const char *sql, sqlite3_uint64 run_time){ - FILE *f = (FILE*)file; - double elapsed = run_time/1000000.0; - if( f ) fprintf(f, "Elapsed time:%.3f ms - %s\n", elapsed, sql); -} - -int sqlcipher_codec_fips_status(codec_ctx *ctx) { - return ctx->read_ctx->provider->fips_status(ctx->read_ctx); -} - -const char* sqlcipher_codec_get_provider_version(codec_ctx *ctx) { - return ctx->read_ctx->provider->get_provider_version(ctx->read_ctx); -} - -#endif -/* END SQLCIPHER */ diff --git a/src/crypto_libtomcrypt.c b/src/crypto_libtomcrypt.c deleted file mode 100644 index 2798dd5729..0000000000 --- a/src/crypto_libtomcrypt.c +++ /dev/null @@ -1,264 +0,0 @@ -/* -** SQLCipher -** http://sqlcipher.net -** -** Copyright (c) 2008 - 2013, ZETETIC LLC -** All rights reserved. -** -** Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in the -** documentation and/or other materials provided with the distribution. -** * Neither the name of the ZETETIC LLC nor the -** names of its contributors may be used to endorse or promote products -** derived from this software without specific prior written permission. -** -** THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY -** EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -** DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY -** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -** -*/ -/* BEGIN SQLCIPHER */ -#ifdef SQLITE_HAS_CODEC -#ifdef SQLCIPHER_CRYPTO_LIBTOMCRYPT -#include "sqliteInt.h" -#include "sqlcipher.h" -#include - -#define FORTUNA_MAX_SZ 32 -static prng_state prng; -static unsigned int ltc_init = 0; -static unsigned int ltc_ref_count = 0; -static sqlite3_mutex* ltc_rand_mutex = NULL; - -static int sqlcipher_ltc_add_random(void *ctx, void *buffer, int length) { - int rc = 0; - int data_to_read = length; - int block_sz = data_to_read < FORTUNA_MAX_SZ ? data_to_read : FORTUNA_MAX_SZ; - const unsigned char * data = (const unsigned char *)buffer; -#ifndef SQLCIPHER_LTC_NO_MUTEX_RAND - sqlite3_mutex_enter(ltc_rand_mutex); -#endif - while(data_to_read > 0){ - rc = fortuna_add_entropy(data, block_sz, &prng); - rc = rc != CRYPT_OK ? SQLITE_ERROR : SQLITE_OK; - if(rc != SQLITE_OK){ - break; - } - data_to_read -= block_sz; - data += block_sz; - block_sz = data_to_read < FORTUNA_MAX_SZ ? data_to_read : FORTUNA_MAX_SZ; - } - fortuna_ready(&prng); -#ifndef SQLCIPHER_LTC_NO_MUTEX_RAND - sqlite3_mutex_leave(ltc_rand_mutex); -#endif - return rc; -} - -static int sqlcipher_ltc_activate(void *ctx) { - unsigned char random_buffer[FORTUNA_MAX_SZ]; -#ifndef SQLCIPHER_LTC_NO_MUTEX_RAND - if(ltc_rand_mutex == NULL){ - ltc_rand_mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); - } - sqlite3_mutex_enter(ltc_rand_mutex); -#endif - sqlcipher_memset(random_buffer, 0, FORTUNA_MAX_SZ); - if(ltc_init == 0) { - if(register_prng(&fortuna_desc) != CRYPT_OK) return SQLITE_ERROR; - if(register_cipher(&rijndael_desc) != CRYPT_OK) return SQLITE_ERROR; - if(register_hash(&sha1_desc) != CRYPT_OK) return SQLITE_ERROR; - if(fortuna_start(&prng) != CRYPT_OK) { - return SQLITE_ERROR; - } - ltc_init = 1; - } - ltc_ref_count++; -#ifndef SQLCIPHER_TEST - sqlite3_randomness(FORTUNA_MAX_SZ, random_buffer); -#endif -#ifndef SQLCIPHER_LTC_NO_MUTEX_RAND - sqlite3_mutex_leave(ltc_rand_mutex); -#endif - if(sqlcipher_ltc_add_random(ctx, random_buffer, FORTUNA_MAX_SZ) != SQLITE_OK) { - return SQLITE_ERROR; - } - sqlcipher_memset(random_buffer, 0, FORTUNA_MAX_SZ); - return SQLITE_OK; -} - -static int sqlcipher_ltc_deactivate(void *ctx) { -#ifndef SQLCIPHER_LTC_NO_MUTEX_RAND - sqlite3_mutex_enter(ltc_rand_mutex); -#endif - ltc_ref_count--; - if(ltc_ref_count == 0){ - fortuna_done(&prng); - sqlcipher_memset((void *)&prng, 0, sizeof(prng)); -#ifndef SQLCIPHER_LTC_NO_MUTEX_RAND - sqlite3_mutex_leave(ltc_rand_mutex); - sqlite3_mutex_free(ltc_rand_mutex); - ltc_rand_mutex = NULL; -#endif - } -#ifndef SQLCIPHER_LTC_NO_MUTEX_RAND - else { - sqlite3_mutex_leave(ltc_rand_mutex); - } -#endif - return SQLITE_OK; -} - -static const char* sqlcipher_ltc_get_provider_name(void *ctx) { - return "libtomcrypt"; -} - -static const char* sqlcipher_ltc_get_provider_version(void *ctx) { - return SCRYPT; -} - -static int sqlcipher_ltc_random(void *ctx, void *buffer, int length) { -#ifndef SQLCIPHER_LTC_NO_MUTEX_RAND - sqlite3_mutex_enter(ltc_rand_mutex); -#endif - fortuna_read(buffer, length, &prng); -#ifndef SQLCIPHER_LTC_NO_MUTEX_RAND - sqlite3_mutex_leave(ltc_rand_mutex); -#endif - return SQLITE_OK; -} - -static int sqlcipher_ltc_hmac(void *ctx, unsigned char *hmac_key, int key_sz, unsigned char *in, int in_sz, unsigned char *in2, int in2_sz, unsigned char *out) { - int rc, hash_idx; - hmac_state hmac; - unsigned long outlen = key_sz; - - hash_idx = find_hash("sha1"); - if((rc = hmac_init(&hmac, hash_idx, hmac_key, key_sz)) != CRYPT_OK) return SQLITE_ERROR; - if((rc = hmac_process(&hmac, in, in_sz)) != CRYPT_OK) return SQLITE_ERROR; - if((rc = hmac_process(&hmac, in2, in2_sz)) != CRYPT_OK) return SQLITE_ERROR; - if((rc = hmac_done(&hmac, out, &outlen)) != CRYPT_OK) return SQLITE_ERROR; - return SQLITE_OK; -} - -static int sqlcipher_ltc_kdf(void *ctx, const unsigned char *pass, int pass_sz, unsigned char* salt, int salt_sz, int workfactor, int key_sz, unsigned char *key) { - int rc, hash_idx; - unsigned long outlen = key_sz; - unsigned long random_buffer_sz = sizeof(char) * 256; - unsigned char *random_buffer = sqlcipher_malloc(random_buffer_sz); - sqlcipher_memset(random_buffer, 0, random_buffer_sz); - - hash_idx = find_hash("sha1"); - if((rc = pkcs_5_alg2(pass, pass_sz, salt, salt_sz, - workfactor, hash_idx, key, &outlen)) != CRYPT_OK) { - return SQLITE_ERROR; - } - if((rc = pkcs_5_alg2(key, key_sz, salt, salt_sz, - 1, hash_idx, random_buffer, &random_buffer_sz)) != CRYPT_OK) { - return SQLITE_ERROR; - } - sqlcipher_ltc_add_random(ctx, random_buffer, random_buffer_sz); - sqlcipher_free(random_buffer, random_buffer_sz); - return SQLITE_OK; -} - -static const char* sqlcipher_ltc_get_cipher(void *ctx) { - return "rijndael"; -} - -static int sqlcipher_ltc_cipher(void *ctx, int mode, unsigned char *key, int key_sz, unsigned char *iv, unsigned char *in, int in_sz, unsigned char *out) { - int rc, cipher_idx; - symmetric_CBC cbc; - - if((cipher_idx = find_cipher(sqlcipher_ltc_get_cipher(ctx))) == -1) return SQLITE_ERROR; - if((rc = cbc_start(cipher_idx, iv, key, key_sz, 0, &cbc)) != CRYPT_OK) return SQLITE_ERROR; - rc = mode == 1 ? cbc_encrypt(in, out, in_sz, &cbc) : cbc_decrypt(in, out, in_sz, &cbc); - if(rc != CRYPT_OK) return SQLITE_ERROR; - cbc_done(&cbc); - return SQLITE_OK; -} - -static int sqlcipher_ltc_set_cipher(void *ctx, const char *cipher_name) { - return SQLITE_OK; -} - -static int sqlcipher_ltc_get_key_sz(void *ctx) { - int cipher_idx = find_cipher(sqlcipher_ltc_get_cipher(ctx)); - return cipher_descriptor[cipher_idx].max_key_length; -} - -static int sqlcipher_ltc_get_iv_sz(void *ctx) { - int cipher_idx = find_cipher(sqlcipher_ltc_get_cipher(ctx)); - return cipher_descriptor[cipher_idx].block_length; -} - -static int sqlcipher_ltc_get_block_sz(void *ctx) { - int cipher_idx = find_cipher(sqlcipher_ltc_get_cipher(ctx)); - return cipher_descriptor[cipher_idx].block_length; -} - -static int sqlcipher_ltc_get_hmac_sz(void *ctx) { - int hash_idx = find_hash("sha1"); - return hash_descriptor[hash_idx].hashsize; -} - -static int sqlcipher_ltc_ctx_copy(void *target_ctx, void *source_ctx) { - return SQLITE_OK; -} - -static int sqlcipher_ltc_ctx_cmp(void *c1, void *c2) { - return 1; -} - -static int sqlcipher_ltc_ctx_init(void **ctx) { - sqlcipher_ltc_activate(NULL); - return SQLITE_OK; -} - -static int sqlcipher_ltc_ctx_free(void **ctx) { - sqlcipher_ltc_deactivate(&ctx); - return SQLITE_OK; -} - -static int sqlcipher_ltc_fips_status(void *ctx) { - return 0; -} - -int sqlcipher_ltc_setup(sqlcipher_provider *p) { - p->activate = sqlcipher_ltc_activate; - p->deactivate = sqlcipher_ltc_deactivate; - p->get_provider_name = sqlcipher_ltc_get_provider_name; - p->random = sqlcipher_ltc_random; - p->hmac = sqlcipher_ltc_hmac; - p->kdf = sqlcipher_ltc_kdf; - p->cipher = sqlcipher_ltc_cipher; - p->set_cipher = sqlcipher_ltc_set_cipher; - p->get_cipher = sqlcipher_ltc_get_cipher; - p->get_key_sz = sqlcipher_ltc_get_key_sz; - p->get_iv_sz = sqlcipher_ltc_get_iv_sz; - p->get_block_sz = sqlcipher_ltc_get_block_sz; - p->get_hmac_sz = sqlcipher_ltc_get_hmac_sz; - p->ctx_copy = sqlcipher_ltc_ctx_copy; - p->ctx_cmp = sqlcipher_ltc_ctx_cmp; - p->ctx_init = sqlcipher_ltc_ctx_init; - p->ctx_free = sqlcipher_ltc_ctx_free; - p->add_random = sqlcipher_ltc_add_random; - p->fips_status = sqlcipher_ltc_fips_status; - p->get_provider_version = sqlcipher_ltc_get_provider_version; - return SQLITE_OK; -} - -#endif -#endif -/* END SQLCIPHER */ diff --git a/src/crypto_openssl.c b/src/crypto_openssl.c index cb4d55e799..828588b2d1 100644 --- a/src/crypto_openssl.c +++ b/src/crypto_openssl.c @@ -32,31 +32,40 @@ #ifdef SQLITE_HAS_CODEC #ifdef SQLCIPHER_CRYPTO_OPENSSL #include "sqliteInt.h" -#include "crypto.h" #include "sqlcipher.h" -#include -#include -#include +#include /* amalgamator: dontcache */ +#include /* amalgamator: dontcache */ +#include /* amalgamator: dontcache */ +#include /* amalgamator: dontcache */ +#include /* amalgamator: dontcache */ +#include /* amalgamator: dontcache */ -typedef struct { - EVP_CIPHER *evp_cipher; -} openssl_ctx; - -static unsigned int openssl_external_init = 0; static unsigned int openssl_init_count = 0; -static sqlite3_mutex* openssl_rand_mutex = NULL; -static int sqlcipher_openssl_add_random(void *ctx, void *buffer, int length) { +static void sqlcipher_openssl_log_errors() { + unsigned long err = 0; + while((err = ERR_get_error()) != 0) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER, "sqlcipher_openssl_log_errors: ERR_get_error() returned %lx: %s", err, ERR_error_string(err, NULL)); + } +} + +static int sqlcipher_openssl_add_random(void *ctx, const void *buffer, int length) { #ifndef SQLCIPHER_OPENSSL_NO_MUTEX_RAND - sqlite3_mutex_enter(openssl_rand_mutex); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_openssl_add_random: entering SQLCIPHER_MUTEX_PROVIDER_RAND"); + sqlite3_mutex_enter(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER_RAND)); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_openssl_add_random: entered SQLCIPHER_MUTEX_PROVIDER_RAND"); #endif RAND_add(buffer, length, 0); #ifndef SQLCIPHER_OPENSSL_NO_MUTEX_RAND - sqlite3_mutex_leave(openssl_rand_mutex); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_openssl_add_random: leaving SQLCIPHER_MUTEX_PROVIDER_RAND"); + sqlite3_mutex_leave(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER_RAND)); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_openssl_add_random: left SQLCIPHER_MUTEX_PROVIDER_RAND"); #endif return SQLITE_OK; } +#define OPENSSL_CIPHER EVP_aes_256_cbc() + /* activate and initialize sqlcipher. Most importantly, this will automatically intialize OpenSSL's EVP system if it hasn't already be externally. Note that this function may be called multiple times as new codecs are intiialized. @@ -67,38 +76,18 @@ static int sqlcipher_openssl_activate(void *ctx) { /* initialize openssl and increment the internal init counter but only if it hasn't been initalized outside of SQLCipher by this program e.g. on startup */ - sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER)); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_openssl_activate: entering SQLCIPHER_MUTEX_PROVIDER_ACTIVATE"); + sqlite3_mutex_enter(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER_ACTIVATE)); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_openssl_activate: entered SQLCIPHER_MUTEX_PROVIDER_ACTIVATE"); - if(openssl_init_count == 0 && EVP_get_cipherbyname(CIPHER) != NULL) { - /* if openssl has not yet been initialized by this library, but - a call to get_cipherbyname works, then the openssl library - has been initialized externally already. */ - openssl_external_init = 1; - } - -#ifdef SQLCIPHER_FIPS - if(!FIPS_mode()){ - if(!FIPS_mode_set(1)){ - ERR_load_crypto_strings(); - ERR_print_errors_fp(stderr); - } - } -#endif - - if(openssl_init_count == 0 && openssl_external_init == 0) { - /* if the library was not externally initialized, then should be now */ - OpenSSL_add_all_algorithms(); - } - -#ifndef SQLCIPHER_OPENSSL_NO_MUTEX_RAND - if(openssl_rand_mutex == NULL) { - /* allocate a mutex to guard against concurrent calls to RAND_bytes() */ - openssl_rand_mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); - } +#if (defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER < 0x10100000L) + ERR_load_crypto_strings(); #endif openssl_init_count++; - sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER)); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_openssl_activate: leaving SQLCIPHER_MUTEX_PROVIDER_ACTIVATE"); + sqlite3_mutex_leave(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER_ACTIVATE)); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_openssl_activate: left SQLCIPHER_MUTEX_PROVIDER_ACTIVATE"); return SQLITE_OK; } @@ -106,24 +95,15 @@ static int sqlcipher_openssl_activate(void *ctx) { freeing the EVP structures on the final deactivation to ensure that OpenSSL memory is cleaned up */ static int sqlcipher_openssl_deactivate(void *ctx) { - sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER)); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_openssl_deactivate: entering SQLCIPHER_MUTEX_PROVIDER_ACTIVATE"); + sqlite3_mutex_enter(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER_ACTIVATE)); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_openssl_deactivate: entered SQLCIPHER_MUTEX_PROVIDER_ACTIVATE"); + openssl_init_count--; - if(openssl_init_count == 0) { - if(openssl_external_init == 0) { - /* if OpenSSL hasn't be initialized externally, and the counter reaches zero - after it's decremented, release EVP memory - Note: this code will only be reached if OpensSSL_add_all_algorithms() - is called by SQLCipher internally. This should prevent SQLCipher from - "cleaning up" openssl when it was initialized externally by the program */ - EVP_cleanup(); - } -#ifndef SQLCIPHER_OPENSSL_NO_MUTEX_RAND - sqlite3_mutex_free(openssl_rand_mutex); - openssl_rand_mutex = NULL; -#endif - } - sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER)); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_openssl_deactivate: leaving SQLCIPHER_MUTEX_PROVIDER_ACTIVATE"); + sqlite3_mutex_leave(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER_ACTIVATE)); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_openssl_deactivate: left SQLCIPHER_MUTEX_PROVIDER_ACTIVATE"); return SQLITE_OK; } @@ -132,7 +112,11 @@ static const char* sqlcipher_openssl_get_provider_name(void *ctx) { } static const char* sqlcipher_openssl_get_provider_version(void *ctx) { +#if (defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER < 0x10100000L) return OPENSSL_VERSION_TEXT; +#else + return OpenSSL_version(OPENSSL_VERSION); +#endif } /* generate a defined number of random bytes */ @@ -145,124 +129,281 @@ static int sqlcipher_openssl_random (void *ctx, void *buffer, int length) { but a more proper solution is that applications setup platform-appropriate thread saftey in openssl externally */ #ifndef SQLCIPHER_OPENSSL_NO_MUTEX_RAND - sqlite3_mutex_enter(openssl_rand_mutex); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_openssl_random: entering SQLCIPHER_MUTEX_PROVIDER_RAND"); + sqlite3_mutex_enter(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER_RAND)); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_openssl_random: entered SQLCIPHER_MUTEX_PROVIDER_RAND"); #endif rc = RAND_bytes((unsigned char *)buffer, length); #ifndef SQLCIPHER_OPENSSL_NO_MUTEX_RAND - sqlite3_mutex_leave(openssl_rand_mutex); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_openssl_random: leaving SQLCIPHER_MUTEX_PROVIDER_RAND"); + sqlite3_mutex_leave(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER_RAND)); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_openssl_random: left SQLCIPHER_MUTEX_PROVIDER_RAND"); #endif - return (rc == 1) ? SQLITE_OK : SQLITE_ERROR; + if(!rc) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER, "sqlcipher_openssl_random: RAND_bytes() returned %d", rc); + sqlcipher_openssl_log_errors(); + return SQLITE_ERROR; + } + return SQLITE_OK; } -static int sqlcipher_openssl_hmac(void *ctx, unsigned char *hmac_key, int key_sz, unsigned char *in, int in_sz, unsigned char *in2, int in2_sz, unsigned char *out) { - HMAC_CTX hctx; - unsigned int outlen; - HMAC_CTX_init(&hctx); - HMAC_Init_ex(&hctx, hmac_key, key_sz, EVP_sha1(), NULL); - HMAC_Update(&hctx, in, in_sz); - HMAC_Update(&hctx, in2, in2_sz); - HMAC_Final(&hctx, out, &outlen); - HMAC_CTX_cleanup(&hctx); - return SQLITE_OK; +static int sqlcipher_openssl_hmac( + void *ctx, int algorithm, + const unsigned char *hmac_key, int key_sz, + const unsigned char *in, int in_sz, + const unsigned char *in2, int in2_sz, + unsigned char *out +) { + int rc = 0; + + size_t outlen; + EVP_MAC *mac = NULL; + EVP_MAC_CTX *hctx = NULL; + OSSL_PARAM sha1[] = { { "digest", OSSL_PARAM_UTF8_STRING, "sha1", 4, 0 }, OSSL_PARAM_END }; + OSSL_PARAM sha256[] = { { "digest", OSSL_PARAM_UTF8_STRING, "sha256", 6, 0 }, OSSL_PARAM_END }; + OSSL_PARAM sha512[] = { { "digest", OSSL_PARAM_UTF8_STRING, "sha512", 6, 0 }, OSSL_PARAM_END }; + + if(in == NULL) goto error; + + mac = EVP_MAC_fetch(NULL, "HMAC", NULL); + if(mac == NULL) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER, "sqlcipher_openssl_hmac: EVP_MAC_fetch for HMAC failed"); + sqlcipher_openssl_log_errors(); + goto error; + } + + hctx = EVP_MAC_CTX_new(mac); + if(hctx == NULL) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER, "sqlcipher_openssl_hmac: EVP_MAC_CTX_new() failed"); + sqlcipher_openssl_log_errors(); + goto error; + } + + switch(algorithm) { + case SQLCIPHER_HMAC_SHA1: + if(!(rc = EVP_MAC_init(hctx, hmac_key, key_sz, sha1))) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER, "sqlcipher_openssl_hmac: EVP_MAC_init() with key size %d and sha1 returned %d", key_sz, rc); + sqlcipher_openssl_log_errors(); + goto error; + } + break; + case SQLCIPHER_HMAC_SHA256: + if(!(rc = EVP_MAC_init(hctx, hmac_key, key_sz, sha256))) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER, "sqlcipher_openssl_hmac: EVP_MAC_init() with key size %d and sha256 returned %d", key_sz, rc); + sqlcipher_openssl_log_errors(); + goto error; + } + break; + case SQLCIPHER_HMAC_SHA512: + if(!(rc = EVP_MAC_init(hctx, hmac_key, key_sz, sha512))) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER, "sqlcipher_openssl_hmac: EVP_MAC_init() with key size %d and sha512 returned %d", key_sz, rc); + sqlcipher_openssl_log_errors(); + goto error; + } + break; + default: + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER, "sqlcipher_openssl_hmac: invalid algorithm %d", algorithm); + goto error; + } + + if(!(rc = EVP_MAC_update(hctx, in, in_sz))) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER, "sqlcipher_openssl_hmac: EVP_MAC_update() on 1st input buffer of %d bytes using algorithm %d returned %d", in_sz, algorithm, rc); + sqlcipher_openssl_log_errors(); + goto error; + } + + if(in2 != NULL) { + if(!(rc = EVP_MAC_update(hctx, in2, in2_sz))) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER, "sqlcipher_openssl_hmac: EVP_MAC_update() on 2nd input buffer of %d bytes using algorithm %d returned %d", in_sz, algorithm, rc); + sqlcipher_openssl_log_errors(); + goto error; + } + } + + if(!(rc = EVP_MAC_final(hctx, NULL, &outlen, 0))) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER, "sqlcipher_openssl_hmac: 1st EVP_MAC_final() for output length calculation using algorithm %d returned %d", algorithm, rc); + sqlcipher_openssl_log_errors(); + goto error; + } + + if(!(rc = EVP_MAC_final(hctx, out, &outlen, outlen))) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER, "sqlcipher_openssl_hmac: 2nd EVP_MAC_final() using algorithm %d returned %d", algorithm, rc); + sqlcipher_openssl_log_errors(); + goto error; + } + + rc = SQLITE_OK; + goto cleanup; + +error: + rc = SQLITE_ERROR; + +cleanup: + if(hctx) EVP_MAC_CTX_free(hctx); + if(mac) EVP_MAC_free(mac); + + return rc; } -static int sqlcipher_openssl_kdf(void *ctx, const unsigned char *pass, int pass_sz, unsigned char* salt, int salt_sz, int workfactor, int key_sz, unsigned char *key) { - PKCS5_PBKDF2_HMAC_SHA1((const char *)pass, pass_sz, salt, salt_sz, workfactor, key_sz, key); - return SQLITE_OK; +static int sqlcipher_openssl_kdf( + void *ctx, int algorithm, + const unsigned char *pass, int pass_sz, + const unsigned char* salt, int salt_sz, + int workfactor, + int key_sz, unsigned char *key +) { + int rc = 0; + + switch(algorithm) { + case SQLCIPHER_HMAC_SHA1: + if(!(rc = PKCS5_PBKDF2_HMAC((const char *)pass, pass_sz, salt, salt_sz, workfactor, EVP_sha1(), key_sz, key))) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER, "sqlcipher_openssl_kdf: PKCS5_PBKDF2_HMAC() for EVP_sha1() workfactor %d and key size %d returned %d", workfactor, key_sz, rc); + sqlcipher_openssl_log_errors(); + goto error; + } + break; + case SQLCIPHER_HMAC_SHA256: + if(!(rc = PKCS5_PBKDF2_HMAC((const char *)pass, pass_sz, salt, salt_sz, workfactor, EVP_sha256(), key_sz, key))) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER, "sqlcipher_openssl_kdf: PKCS5_PBKDF2_HMAC() for EVP_sha256() workfactor %d and key size %d returned %d", workfactor, key_sz, rc); + sqlcipher_openssl_log_errors(); + goto error; + } + break; + case SQLCIPHER_HMAC_SHA512: + if(!(rc = PKCS5_PBKDF2_HMAC((const char *)pass, pass_sz, salt, salt_sz, workfactor, EVP_sha512(), key_sz, key))) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER, "sqlcipher_openssl_kdf: PKCS5_PBKDF2_HMAC() for EVP_sha512() workfactor %d and key size %d returned %d", workfactor, key_sz, rc); + sqlcipher_openssl_log_errors(); + goto error; + } + break; + default: + return SQLITE_ERROR; + } + + rc = SQLITE_OK; + goto cleanup; +error: + rc = SQLITE_ERROR; +cleanup: + return rc; } -static int sqlcipher_openssl_cipher(void *ctx, int mode, unsigned char *key, int key_sz, unsigned char *iv, unsigned char *in, int in_sz, unsigned char *out) { - EVP_CIPHER_CTX ectx; - int tmp_csz, csz; - - EVP_CipherInit(&ectx, ((openssl_ctx *)ctx)->evp_cipher, NULL, NULL, mode); - EVP_CIPHER_CTX_set_padding(&ectx, 0); // no padding - EVP_CipherInit(&ectx, NULL, key, iv, mode); - EVP_CipherUpdate(&ectx, out, &tmp_csz, in, in_sz); +static int sqlcipher_openssl_cipher( + void *ctx, int mode, + const unsigned char *key, int key_sz, + const unsigned char *iv, + const unsigned char *in, int in_sz, + unsigned char *out +) { + int tmp_csz, csz, rc = 0; + EVP_CIPHER_CTX* ectx = EVP_CIPHER_CTX_new(); + if(ectx == NULL) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER, "sqlcipher_openssl_cipher: EVP_CIPHER_CTX_new failed"); + sqlcipher_openssl_log_errors(); + goto error; + } + + if(!(rc = EVP_CipherInit_ex(ectx, OPENSSL_CIPHER, NULL, NULL, NULL, mode))) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER, "sqlcipher_openssl_cipher: EVP_CipherInit_ex for mode %d returned %d", mode, rc); + sqlcipher_openssl_log_errors(); + goto error; + } + + if(!(rc = EVP_CIPHER_CTX_set_padding(ectx, 0))) { /* no padding */ + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER, "sqlcipher_openssl_cipher: EVP_CIPHER_CTX_set_padding 0 returned %d", rc); + sqlcipher_openssl_log_errors(); + goto error; + } + + if(!(rc = EVP_CipherInit_ex(ectx, NULL, NULL, key, iv, mode))) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER, "sqlcipher_openssl_cipher: EVP_CipherInit_ex for mode %d returned %d", mode, rc); + sqlcipher_openssl_log_errors(); + goto error; + } + + if(!(rc = EVP_CipherUpdate(ectx, out, &tmp_csz, in, in_sz))) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER, "sqlcipher_openssl_cipher: EVP_CipherUpdate returned %d", rc); + sqlcipher_openssl_log_errors(); + goto error; + } + csz = tmp_csz; out += tmp_csz; - EVP_CipherFinal(&ectx, out, &tmp_csz); + if(!(rc = EVP_CipherFinal_ex(ectx, out, &tmp_csz))) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER, "sqlcipher_openssl_cipher: EVP_CipherFinal_ex returned %d", rc); + sqlcipher_openssl_log_errors(); + goto error; + } + csz += tmp_csz; - EVP_CIPHER_CTX_cleanup(&ectx); assert(in_sz == csz); - return SQLITE_OK; -} -static int sqlcipher_openssl_set_cipher(void *ctx, const char *cipher_name) { - openssl_ctx *o_ctx = (openssl_ctx *)ctx; - EVP_CIPHER* cipher = (EVP_CIPHER *) EVP_get_cipherbyname(cipher_name); - if(cipher != NULL) { - o_ctx->evp_cipher = cipher; - } - return cipher != NULL ? SQLITE_OK : SQLITE_ERROR; + rc = SQLITE_OK; + goto cleanup; +error: + rc = SQLITE_ERROR; +cleanup: + if(ectx) EVP_CIPHER_CTX_free(ectx); + return rc; } static const char* sqlcipher_openssl_get_cipher(void *ctx) { - return EVP_CIPHER_name(((openssl_ctx *)ctx)->evp_cipher); + return OBJ_nid2sn(EVP_CIPHER_nid(OPENSSL_CIPHER)); } static int sqlcipher_openssl_get_key_sz(void *ctx) { - return EVP_CIPHER_key_length(((openssl_ctx *)ctx)->evp_cipher); + return EVP_CIPHER_key_length(OPENSSL_CIPHER); } static int sqlcipher_openssl_get_iv_sz(void *ctx) { - return EVP_CIPHER_iv_length(((openssl_ctx *)ctx)->evp_cipher); + return EVP_CIPHER_iv_length(OPENSSL_CIPHER); } static int sqlcipher_openssl_get_block_sz(void *ctx) { - return EVP_CIPHER_block_size(((openssl_ctx *)ctx)->evp_cipher); -} - -static int sqlcipher_openssl_get_hmac_sz(void *ctx) { - return EVP_MD_size(EVP_sha1()); + return EVP_CIPHER_block_size(OPENSSL_CIPHER); } -static int sqlcipher_openssl_ctx_copy(void *target_ctx, void *source_ctx) { - memcpy(target_ctx, source_ctx, sizeof(openssl_ctx)); - return SQLITE_OK; -} - -static int sqlcipher_openssl_ctx_cmp(void *c1, void *c2) { - return ((openssl_ctx *)c1)->evp_cipher == ((openssl_ctx *)c2)->evp_cipher; +static int sqlcipher_openssl_get_hmac_sz(void *ctx, int algorithm) { + switch(algorithm) { + case SQLCIPHER_HMAC_SHA1: + return EVP_MD_size(EVP_sha1()); + break; + case SQLCIPHER_HMAC_SHA256: + return EVP_MD_size(EVP_sha256()); + break; + case SQLCIPHER_HMAC_SHA512: + return EVP_MD_size(EVP_sha512()); + break; + default: + return 0; + } } static int sqlcipher_openssl_ctx_init(void **ctx) { - *ctx = sqlcipher_malloc(sizeof(openssl_ctx)); - if(*ctx == NULL) return SQLITE_NOMEM; - sqlcipher_openssl_activate(*ctx); - return SQLITE_OK; + return sqlcipher_openssl_activate(*ctx); } static int sqlcipher_openssl_ctx_free(void **ctx) { - sqlcipher_openssl_deactivate(*ctx); - sqlcipher_free(*ctx, sizeof(openssl_ctx)); - return SQLITE_OK; + return sqlcipher_openssl_deactivate(NULL); } static int sqlcipher_openssl_fips_status(void *ctx) { -#ifdef SQLCIPHER_FIPS - return FIPS_mode(); -#else return 0; -#endif } int sqlcipher_openssl_setup(sqlcipher_provider *p) { - p->activate = sqlcipher_openssl_activate; - p->deactivate = sqlcipher_openssl_deactivate; + p->init = NULL; + p->shutdown = NULL; p->get_provider_name = sqlcipher_openssl_get_provider_name; p->random = sqlcipher_openssl_random; p->hmac = sqlcipher_openssl_hmac; p->kdf = sqlcipher_openssl_kdf; p->cipher = sqlcipher_openssl_cipher; - p->set_cipher = sqlcipher_openssl_set_cipher; p->get_cipher = sqlcipher_openssl_get_cipher; p->get_key_sz = sqlcipher_openssl_get_key_sz; p->get_iv_sz = sqlcipher_openssl_get_iv_sz; p->get_block_sz = sqlcipher_openssl_get_block_sz; p->get_hmac_sz = sqlcipher_openssl_get_hmac_sz; - p->ctx_copy = sqlcipher_openssl_ctx_copy; - p->ctx_cmp = sqlcipher_openssl_ctx_cmp; p->ctx_init = sqlcipher_openssl_ctx_init; p->ctx_free = sqlcipher_openssl_ctx_free; p->add_random = sqlcipher_openssl_add_random; diff --git a/src/ctime.c b/src/ctime.c deleted file mode 100644 index f1bb69c16a..0000000000 --- a/src/ctime.c +++ /dev/null @@ -1,442 +0,0 @@ -/* -** 2010 February 23 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -************************************************************************* -** -** This file implements routines used to report what compile-time options -** SQLite was built with. -*/ - -#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS - -#include "sqliteInt.h" - -/* -** An array of names of all compile-time options. This array should -** be sorted A-Z. -** -** This array looks large, but in a typical installation actually uses -** only a handful of compile-time options, so most times this array is usually -** rather short and uses little memory space. -*/ -static const char * const azCompileOpt[] = { - -/* These macros are provided to "stringify" the value of the define -** for those options in which the value is meaningful. */ -#define CTIMEOPT_VAL_(opt) #opt -#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt) - -#if SQLITE_32BIT_ROWID - "32BIT_ROWID", -#endif -#if SQLITE_4_BYTE_ALIGNED_MALLOC - "4_BYTE_ALIGNED_MALLOC", -#endif -#if SQLITE_CASE_SENSITIVE_LIKE - "CASE_SENSITIVE_LIKE", -#endif -#if SQLITE_CHECK_PAGES - "CHECK_PAGES", -#endif -#if SQLITE_COVERAGE_TEST - "COVERAGE_TEST", -#endif -#if SQLITE_DEBUG - "DEBUG", -#endif -#if SQLITE_DEFAULT_LOCKING_MODE - "DEFAULT_LOCKING_MODE=" CTIMEOPT_VAL(SQLITE_DEFAULT_LOCKING_MODE), -#endif -#if defined(SQLITE_DEFAULT_MMAP_SIZE) && !defined(SQLITE_DEFAULT_MMAP_SIZE_xc) - "DEFAULT_MMAP_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_MMAP_SIZE), -#endif -#if SQLITE_DISABLE_DIRSYNC - "DISABLE_DIRSYNC", -#endif -#if SQLITE_DISABLE_LFS - "DISABLE_LFS", -#endif -#if SQLITE_ENABLE_8_3_NAMES - "ENABLE_8_3_NAMES", -#endif -#if SQLITE_ENABLE_API_ARMOR - "ENABLE_API_ARMOR", -#endif -#if SQLITE_ENABLE_ATOMIC_WRITE - "ENABLE_ATOMIC_WRITE", -#endif -#if SQLITE_ENABLE_CEROD - "ENABLE_CEROD", -#endif -#if SQLITE_ENABLE_COLUMN_METADATA - "ENABLE_COLUMN_METADATA", -#endif -#if SQLITE_ENABLE_DBSTAT_VTAB - "ENABLE_DBSTAT_VTAB", -#endif -#if SQLITE_ENABLE_EXPENSIVE_ASSERT - "ENABLE_EXPENSIVE_ASSERT", -#endif -#if SQLITE_ENABLE_FTS1 - "ENABLE_FTS1", -#endif -#if SQLITE_ENABLE_FTS2 - "ENABLE_FTS2", -#endif -#if SQLITE_ENABLE_FTS3 - "ENABLE_FTS3", -#endif -#if SQLITE_ENABLE_FTS3_PARENTHESIS - "ENABLE_FTS3_PARENTHESIS", -#endif -#if SQLITE_ENABLE_FTS4 - "ENABLE_FTS4", -#endif -#if SQLITE_ENABLE_FTS5 - "ENABLE_FTS5", -#endif -#if SQLITE_ENABLE_ICU - "ENABLE_ICU", -#endif -#if SQLITE_ENABLE_IOTRACE - "ENABLE_IOTRACE", -#endif -#if SQLITE_ENABLE_JSON1 - "ENABLE_JSON1", -#endif -#if SQLITE_ENABLE_LOAD_EXTENSION - "ENABLE_LOAD_EXTENSION", -#endif -#if SQLITE_ENABLE_LOCKING_STYLE - "ENABLE_LOCKING_STYLE=" CTIMEOPT_VAL(SQLITE_ENABLE_LOCKING_STYLE), -#endif -#if SQLITE_ENABLE_MEMORY_MANAGEMENT - "ENABLE_MEMORY_MANAGEMENT", -#endif -#if SQLITE_ENABLE_MEMSYS3 - "ENABLE_MEMSYS3", -#endif -#if SQLITE_ENABLE_MEMSYS5 - "ENABLE_MEMSYS5", -#endif -#if SQLITE_ENABLE_OVERSIZE_CELL_CHECK - "ENABLE_OVERSIZE_CELL_CHECK", -#endif -#if SQLITE_ENABLE_RTREE - "ENABLE_RTREE", -#endif -#if defined(SQLITE_ENABLE_STAT4) - "ENABLE_STAT4", -#elif defined(SQLITE_ENABLE_STAT3) - "ENABLE_STAT3", -#endif -#if SQLITE_ENABLE_UNLOCK_NOTIFY - "ENABLE_UNLOCK_NOTIFY", -#endif -#if SQLITE_ENABLE_UPDATE_DELETE_LIMIT - "ENABLE_UPDATE_DELETE_LIMIT", -#endif -#if SQLITE_HAS_CODEC - "HAS_CODEC", -#endif -#if HAVE_ISNAN || SQLITE_HAVE_ISNAN - "HAVE_ISNAN", -#endif -#if SQLITE_HOMEGROWN_RECURSIVE_MUTEX - "HOMEGROWN_RECURSIVE_MUTEX", -#endif -#if SQLITE_IGNORE_AFP_LOCK_ERRORS - "IGNORE_AFP_LOCK_ERRORS", -#endif -#if SQLITE_IGNORE_FLOCK_LOCK_ERRORS - "IGNORE_FLOCK_LOCK_ERRORS", -#endif -#ifdef SQLITE_INT64_TYPE - "INT64_TYPE", -#endif -#ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS - "LIKE_DOESNT_MATCH_BLOBS", -#endif -#if SQLITE_LOCK_TRACE - "LOCK_TRACE", -#endif -#if defined(SQLITE_MAX_MMAP_SIZE) && !defined(SQLITE_MAX_MMAP_SIZE_xc) - "MAX_MMAP_SIZE=" CTIMEOPT_VAL(SQLITE_MAX_MMAP_SIZE), -#endif -#ifdef SQLITE_MAX_SCHEMA_RETRY - "MAX_SCHEMA_RETRY=" CTIMEOPT_VAL(SQLITE_MAX_SCHEMA_RETRY), -#endif -#if SQLITE_MEMDEBUG - "MEMDEBUG", -#endif -#if SQLITE_MIXED_ENDIAN_64BIT_FLOAT - "MIXED_ENDIAN_64BIT_FLOAT", -#endif -#if SQLITE_NO_SYNC - "NO_SYNC", -#endif -#if SQLITE_OMIT_ALTERTABLE - "OMIT_ALTERTABLE", -#endif -#if SQLITE_OMIT_ANALYZE - "OMIT_ANALYZE", -#endif -#if SQLITE_OMIT_ATTACH - "OMIT_ATTACH", -#endif -#if SQLITE_OMIT_AUTHORIZATION - "OMIT_AUTHORIZATION", -#endif -#if SQLITE_OMIT_AUTOINCREMENT - "OMIT_AUTOINCREMENT", -#endif -#if SQLITE_OMIT_AUTOINIT - "OMIT_AUTOINIT", -#endif -#if SQLITE_OMIT_AUTOMATIC_INDEX - "OMIT_AUTOMATIC_INDEX", -#endif -#if SQLITE_OMIT_AUTORESET - "OMIT_AUTORESET", -#endif -#if SQLITE_OMIT_AUTOVACUUM - "OMIT_AUTOVACUUM", -#endif -#if SQLITE_OMIT_BETWEEN_OPTIMIZATION - "OMIT_BETWEEN_OPTIMIZATION", -#endif -#if SQLITE_OMIT_BLOB_LITERAL - "OMIT_BLOB_LITERAL", -#endif -#if SQLITE_OMIT_BTREECOUNT - "OMIT_BTREECOUNT", -#endif -#if SQLITE_OMIT_BUILTIN_TEST - "OMIT_BUILTIN_TEST", -#endif -#if SQLITE_OMIT_CAST - "OMIT_CAST", -#endif -#if SQLITE_OMIT_CHECK - "OMIT_CHECK", -#endif -#if SQLITE_OMIT_COMPLETE - "OMIT_COMPLETE", -#endif -#if SQLITE_OMIT_COMPOUND_SELECT - "OMIT_COMPOUND_SELECT", -#endif -#if SQLITE_OMIT_CTE - "OMIT_CTE", -#endif -#if SQLITE_OMIT_DATETIME_FUNCS - "OMIT_DATETIME_FUNCS", -#endif -#if SQLITE_OMIT_DECLTYPE - "OMIT_DECLTYPE", -#endif -#if SQLITE_OMIT_DEPRECATED - "OMIT_DEPRECATED", -#endif -#if SQLITE_OMIT_DISKIO - "OMIT_DISKIO", -#endif -#if SQLITE_OMIT_EXPLAIN - "OMIT_EXPLAIN", -#endif -#if SQLITE_OMIT_FLAG_PRAGMAS - "OMIT_FLAG_PRAGMAS", -#endif -#if SQLITE_OMIT_FLOATING_POINT - "OMIT_FLOATING_POINT", -#endif -#if SQLITE_OMIT_FOREIGN_KEY - "OMIT_FOREIGN_KEY", -#endif -#if SQLITE_OMIT_GET_TABLE - "OMIT_GET_TABLE", -#endif -#if SQLITE_OMIT_INCRBLOB - "OMIT_INCRBLOB", -#endif -#if SQLITE_OMIT_INTEGRITY_CHECK - "OMIT_INTEGRITY_CHECK", -#endif -#if SQLITE_OMIT_LIKE_OPTIMIZATION - "OMIT_LIKE_OPTIMIZATION", -#endif -#if SQLITE_OMIT_LOAD_EXTENSION - "OMIT_LOAD_EXTENSION", -#endif -#if SQLITE_OMIT_LOCALTIME - "OMIT_LOCALTIME", -#endif -#if SQLITE_OMIT_LOOKASIDE - "OMIT_LOOKASIDE", -#endif -#if SQLITE_OMIT_MEMORYDB - "OMIT_MEMORYDB", -#endif -#if SQLITE_OMIT_OR_OPTIMIZATION - "OMIT_OR_OPTIMIZATION", -#endif -#if SQLITE_OMIT_PAGER_PRAGMAS - "OMIT_PAGER_PRAGMAS", -#endif -#if SQLITE_OMIT_PRAGMA - "OMIT_PRAGMA", -#endif -#if SQLITE_OMIT_PROGRESS_CALLBACK - "OMIT_PROGRESS_CALLBACK", -#endif -#if SQLITE_OMIT_QUICKBALANCE - "OMIT_QUICKBALANCE", -#endif -#if SQLITE_OMIT_REINDEX - "OMIT_REINDEX", -#endif -#if SQLITE_OMIT_SCHEMA_PRAGMAS - "OMIT_SCHEMA_PRAGMAS", -#endif -#if SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS - "OMIT_SCHEMA_VERSION_PRAGMAS", -#endif -#if SQLITE_OMIT_SHARED_CACHE - "OMIT_SHARED_CACHE", -#endif -#if SQLITE_OMIT_SUBQUERY - "OMIT_SUBQUERY", -#endif -#if SQLITE_OMIT_TCL_VARIABLE - "OMIT_TCL_VARIABLE", -#endif -#if SQLITE_OMIT_TEMPDB - "OMIT_TEMPDB", -#endif -#if SQLITE_OMIT_TRACE - "OMIT_TRACE", -#endif -#if SQLITE_OMIT_TRIGGER - "OMIT_TRIGGER", -#endif -#if SQLITE_OMIT_TRUNCATE_OPTIMIZATION - "OMIT_TRUNCATE_OPTIMIZATION", -#endif -#if SQLITE_OMIT_UTF16 - "OMIT_UTF16", -#endif -#if SQLITE_OMIT_VACUUM - "OMIT_VACUUM", -#endif -#if SQLITE_OMIT_VIEW - "OMIT_VIEW", -#endif -#if SQLITE_OMIT_VIRTUALTABLE - "OMIT_VIRTUALTABLE", -#endif -#if SQLITE_OMIT_WAL - "OMIT_WAL", -#endif -#if SQLITE_OMIT_WSD - "OMIT_WSD", -#endif -#if SQLITE_OMIT_XFER_OPT - "OMIT_XFER_OPT", -#endif -#if SQLITE_PERFORMANCE_TRACE - "PERFORMANCE_TRACE", -#endif -#if SQLITE_PROXY_DEBUG - "PROXY_DEBUG", -#endif -#if SQLITE_RTREE_INT_ONLY - "RTREE_INT_ONLY", -#endif -#if SQLITE_SECURE_DELETE - "SECURE_DELETE", -#endif -#if SQLITE_SMALL_STACK - "SMALL_STACK", -#endif -#if SQLITE_SOUNDEX - "SOUNDEX", -#endif -#if SQLITE_SYSTEM_MALLOC - "SYSTEM_MALLOC", -#endif -#if SQLITE_TCL - "TCL", -#endif -#if defined(SQLITE_TEMP_STORE) && !defined(SQLITE_TEMP_STORE_xc) - "TEMP_STORE=" CTIMEOPT_VAL(SQLITE_TEMP_STORE), -#endif -#if SQLITE_TEST - "TEST", -#endif -#if defined(SQLITE_THREADSAFE) - "THREADSAFE=" CTIMEOPT_VAL(SQLITE_THREADSAFE), -#endif -#if SQLITE_USE_ALLOCA - "USE_ALLOCA", -#endif -#if SQLITE_USER_AUTHENTICATION - "USER_AUTHENTICATION", -#endif -#if SQLITE_WIN32_MALLOC - "WIN32_MALLOC", -#endif -#if SQLITE_ZERO_MALLOC - "ZERO_MALLOC" -#endif -}; - -/* -** Given the name of a compile-time option, return true if that option -** was used and false if not. -** -** The name can optionally begin with "SQLITE_" but the "SQLITE_" prefix -** is not required for a match. -*/ -int sqlite3_compileoption_used(const char *zOptName){ - int i, n; - -#if SQLITE_ENABLE_API_ARMOR - if( zOptName==0 ){ - (void)SQLITE_MISUSE_BKPT; - return 0; - } -#endif - if( sqlite3StrNICmp(zOptName, "SQLITE_", 7)==0 ) zOptName += 7; - n = sqlite3Strlen30(zOptName); - - /* Since ArraySize(azCompileOpt) is normally in single digits, a - ** linear search is adequate. No need for a binary search. */ - for(i=0; i=0 && NisLocal = 0; + p->isUtc = 1; goto zulu_time; }else{ return c!=0; @@ -173,9 +188,12 @@ static int parseTimezone(const char *zDate, DateTime *p){ } zDate += 5; p->tz = sgn*(nMn + nHr*60); + if( p->tz==0 ){ /* Forum post 2025-09-17T10:12:14z */ + p->isLocal = 0; + p->isUtc = 1; + } zulu_time: while( sqlite3Isspace(*zDate) ){ zDate++; } - p->tzSet = 1; return *zDate!=0; } @@ -208,20 +226,31 @@ static int parseHhMmSs(const char *zDate, DateTime *p){ zDate++; } ms /= rScale; + /* Truncate to avoid problems with sub-milliseconds + ** rounding. https://sqlite.org/forum/forumpost/766a2c9231 */ + if( ms>0.999 ) ms = 0.999; } }else{ s = 0; } p->validJD = 0; + p->rawS = 0; p->validHMS = 1; p->h = h; p->m = m; p->s = s + ms; if( parseTimezone(zDate, p) ) return 1; - p->validTZ = (p->tz!=0)?1:0; return 0; } +/* +** Put the DateTime object into its error state. +*/ +static void datetimeError(DateTime *p){ + memset(p, 0, sizeof(*p)); + p->isError = 1; +} + /* ** Convert from YYYY-MM-DD HH:MM:SS to julian day. We always assume ** that the YYYY-MM-DD is according to the Gregorian calendar. @@ -241,27 +270,56 @@ static void computeJD(DateTime *p){ M = 1; D = 1; } + if( Y<-4713 || Y>9999 || p->rawS ){ + datetimeError(p); + return; + } if( M<=2 ){ Y--; M += 12; } - A = Y/100; - B = 2 - A + (A/4); + A = (Y+4800)/100; + B = 38 - A + (A/4); X1 = 36525*(Y+4716)/100; X2 = 306001*(M+1)/10000; p->iJD = (sqlite3_int64)((X1 + X2 + D + B - 1524.5 ) * 86400000); p->validJD = 1; if( p->validHMS ){ - p->iJD += p->h*3600000 + p->m*60000 + (sqlite3_int64)(p->s*1000); - if( p->validTZ ){ + p->iJD += p->h*3600000 + p->m*60000 + (sqlite3_int64)(p->s*1000 + 0.5); + if( p->tz ){ p->iJD -= p->tz*60000; p->validYMD = 0; p->validHMS = 0; - p->validTZ = 0; + p->tz = 0; + p->isUtc = 1; + p->isLocal = 0; } } } +/* +** Given the YYYY-MM-DD information current in p, determine if there +** is day-of-month overflow and set nFloor to the number of days that +** would need to be subtracted from the date in order to bring the +** date back to the end of the month. +*/ +static void computeFloor(DateTime *p){ + assert( p->validYMD || p->isError ); + assert( p->D>=0 && p->D<=31 ); + assert( p->M>=0 && p->M<=12 ); + if( p->D<=28 ){ + p->nFloor = 0; + }else if( (1<M) & 0x15aa ){ + p->nFloor = 0; + }else if( p->M!=2 ){ + p->nFloor = (p->D==31); + }else if( p->Y%4!=0 || (p->Y%100==0 && p->Y%400!=0) ){ + p->nFloor = p->D - 28; + }else{ + p->nFloor = p->D - 29; + } +} + /* ** Parse dates of the form ** @@ -300,12 +358,16 @@ static int parseYyyyMmDd(const char *zDate, DateTime *p){ p->Y = neg ? -Y : Y; p->M = M; p->D = D; - if( p->validTZ ){ + computeFloor(p); + if( p->tz ){ computeJD(p); } return 0; } + +static void clearYMD_HMS_TZ(DateTime *p); /* Forward declaration */ + /* ** Set the time to the current time reported by the VFS. ** @@ -315,12 +377,30 @@ static int setDateTimeToCurrent(sqlite3_context *context, DateTime *p){ p->iJD = sqlite3StmtCurrentTime(context); if( p->iJD>0 ){ p->validJD = 1; + p->isUtc = 1; + p->isLocal = 0; + clearYMD_HMS_TZ(p); return 0; }else{ return 1; } } +/* +** Input "r" is a numeric quantity which might be a julian day number, +** or the number of seconds since 1970. If the value if r is within +** range of a julian day number, install it as such and set validJD. +** If the value is a valid unix timestamp, put it in p->s and set p->rawS. +*/ +static void setRawDateNumber(DateTime *p, double r){ + p->s = r; + p->rawS = 1; + if( r>=0.0 && r<5373484.5 ){ + p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5); + p->validJD = 1; + } +} + /* ** Attempt to parse the given string into a julian day number. Return ** the number of errors. @@ -347,30 +427,55 @@ static int parseDateOrTime( return 0; }else if( parseHhMmSs(zDate, p)==0 ){ return 0; - }else if( sqlite3StrICmp(zDate,"now")==0){ + }else if( sqlite3StrICmp(zDate,"now")==0 && sqlite3NotPureFunc(context) ){ return setDateTimeToCurrent(context, p); - }else if( sqlite3AtoF(zDate, &r, sqlite3Strlen30(zDate), SQLITE_UTF8) ){ - p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5); - p->validJD = 1; + }else if( sqlite3AtoF(zDate, &r, sqlite3Strlen30(zDate), SQLITE_UTF8)>0 ){ + setRawDateNumber(p, r); return 0; + }else if( (sqlite3StrICmp(zDate,"subsec")==0 + || sqlite3StrICmp(zDate,"subsecond")==0) + && sqlite3NotPureFunc(context) ){ + p->useSubsec = 1; + return setDateTimeToCurrent(context, p); } return 1; } +/* The julian day number for 9999-12-31 23:59:59.999 is 5373484.4999999. +** Multiplying this by 86400000 gives 464269060799999 as the maximum value +** for DateTime.iJD. +** +** But some older compilers (ex: gcc 4.2.1 on older Macs) cannot deal with +** such a large integer literal, so we have to encode it. +*/ +#define INT_464269060799999 ((((i64)0x1a640)<<32)|0x1072fdff) + +/* +** Return TRUE if the given julian day number is within range. +** +** The input is the JulianDay times 86400000. +*/ +static int validJulianDay(sqlite3_int64 iJD){ + return iJD>=0 && iJD<=INT_464269060799999; +} + /* ** Compute the Year, Month, and Day from the julian day number. */ static void computeYMD(DateTime *p){ - int Z, A, B, C, D, E, X1; + int Z, alpha, A, B, C, D, E, X1; if( p->validYMD ) return; if( !p->validJD ){ p->Y = 2000; p->M = 1; p->D = 1; + }else if( !validJulianDay(p->iJD) ){ + datetimeError(p); + return; }else{ Z = (int)((p->iJD + 43200000)/86400000); - A = (int)((Z - 1867216.25)/36524.25); - A = Z + 1 + A - (A/4); + alpha = (int)((Z + 32044.75)/36524.25) - 52; + A = Z + 1 + alpha - ((alpha+100)/4) + 25; B = A + 1524; C = (int)((B - 122.1)/365.25); D = (36525*(C&32767))/100; @@ -387,17 +492,15 @@ static void computeYMD(DateTime *p){ ** Compute the Hour, Minute, and Seconds from the julian day number. */ static void computeHMS(DateTime *p){ - int s; + int day_ms, day_min; /* milliseconds, minutes into the day */ if( p->validHMS ) return; computeJD(p); - s = (int)((p->iJD + 43200000) % 86400000); - p->s = s/1000.0; - s = (int)p->s; - p->s -= s; - p->h = s/3600; - s -= p->h*3600; - p->m = s/60; - p->s += s - p->m*60; + day_ms = (int)((p->iJD + 43200000) % 86400000); + p->s = (day_ms % 60000)/1000.0; + day_min = day_ms/60000; + p->m = day_min % 60; + p->h = day_min / 60; + p->rawS = 0; p->validHMS = 1; } @@ -415,9 +518,10 @@ static void computeYMD_HMS(DateTime *p){ static void clearYMD_HMS_TZ(DateTime *p){ p->validYMD = 0; p->validHMS = 0; - p->validTZ = 0; + p->tz = 0; } +#ifndef SQLITE_OMIT_LOCALTIME /* ** On recent Windows platforms, the localtime_s() function is available ** as part of the "Secure CRT". It is essentially equivalent to @@ -436,15 +540,16 @@ static void clearYMD_HMS_TZ(DateTime *p){ #define HAVE_LOCALTIME_S 1 #endif -#ifndef SQLITE_OMIT_LOCALTIME /* ** The following routine implements the rough equivalent of localtime_r() ** using whatever operating-system specific localtime facility that ** is available. This routine returns 0 on success and ** non-zero on any kind of error. ** -** If the sqlite3GlobalConfig.bLocaltimeFault variable is true then this -** routine will always fail. +** If the sqlite3GlobalConfig.bLocaltimeFault variable is non-zero then this +** routine will always fail. If bLocaltimeFault is nonzero and +** sqlite3GlobalConfig.xAltLocaltime is not NULL, then xAltLocaltime() is +** invoked in place of the OS-defined localtime() function. ** ** EVIDENCE-OF: R-62172-00036 In this implementation, the standard C ** library function localtime_r() is used to assist in the calculation of @@ -455,19 +560,35 @@ static int osLocaltime(time_t *t, struct tm *pTm){ #if !HAVE_LOCALTIME_R && !HAVE_LOCALTIME_S struct tm *pX; #if SQLITE_THREADSAFE>0 - sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); + sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); #endif sqlite3_mutex_enter(mutex); pX = localtime(t); -#ifndef SQLITE_OMIT_BUILTIN_TEST - if( sqlite3GlobalConfig.bLocaltimeFault ) pX = 0; +#ifndef SQLITE_UNTESTABLE + if( sqlite3GlobalConfig.bLocaltimeFault ){ + if( sqlite3GlobalConfig.xAltLocaltime!=0 + && 0==sqlite3GlobalConfig.xAltLocaltime((const void*)t,(void*)pTm) + ){ + pX = pTm; + }else{ + pX = 0; + } + } #endif if( pX ) *pTm = *pX; +#if SQLITE_THREADSAFE>0 sqlite3_mutex_leave(mutex); +#endif rc = pX==0; #else -#ifndef SQLITE_OMIT_BUILTIN_TEST - if( sqlite3GlobalConfig.bLocaltimeFault ) return 1; +#ifndef SQLITE_UNTESTABLE + if( sqlite3GlobalConfig.bLocaltimeFault ){ + if( sqlite3GlobalConfig.xAltLocaltime!=0 ){ + return sqlite3GlobalConfig.xAltLocaltime((const void*)t,(void*)pTm); + }else{ + return 1; + } + } #endif #if HAVE_LOCALTIME_R rc = localtime_r(t, pTm)==0; @@ -482,68 +603,100 @@ static int osLocaltime(time_t *t, struct tm *pTm){ #ifndef SQLITE_OMIT_LOCALTIME /* -** Compute the difference (in milliseconds) between localtime and UTC -** (a.k.a. GMT) for the time value p where p is in UTC. If no error occurs, -** return this value and set *pRc to SQLITE_OK. -** -** Or, if an error does occur, set *pRc to SQLITE_ERROR. The returned value -** is undefined in this case. +** Assuming the input DateTime is UTC, move it to its localtime equivalent. */ -static sqlite3_int64 localtimeOffset( - DateTime *p, /* Date at which to calculate offset */ - sqlite3_context *pCtx, /* Write error here if one occurs */ - int *pRc /* OUT: Error code. SQLITE_OK or ERROR */ +static int toLocaltime( + DateTime *p, /* Date at which to calculate offset */ + sqlite3_context *pCtx /* Write error here if one occurs */ ){ - DateTime x, y; time_t t; struct tm sLocal; + int iYearDiff; /* Initialize the contents of sLocal to avoid a compiler warning. */ memset(&sLocal, 0, sizeof(sLocal)); - x = *p; - computeYMD_HMS(&x); - if( x.Y<1971 || x.Y>=2038 ){ + computeJD(p); + if( p->iJD<2108667600*(i64)100000 /* 1970-01-01 */ + || p->iJD>2130141456*(i64)100000 /* 2038-01-18 */ + ){ /* EVIDENCE-OF: R-55269-29598 The localtime_r() C function normally only ** works for years between 1970 and 2037. For dates outside this range, ** SQLite attempts to map the year into an equivalent year within this ** range, do the calculation, then map the year back. */ - x.Y = 2000; - x.M = 1; - x.D = 1; - x.h = 0; - x.m = 0; - x.s = 0.0; - } else { - int s = (int)(x.s + 0.5); - x.s = s; + DateTime x = *p; + computeYMD_HMS(&x); + iYearDiff = (2000 + x.Y%4) - x.Y; + x.Y += iYearDiff; + x.validJD = 0; + computeJD(&x); + t = (time_t)(x.iJD/1000 - 21086676*(i64)10000); + }else{ + iYearDiff = 0; + t = (time_t)(p->iJD/1000 - 21086676*(i64)10000); } - x.tz = 0; - x.validJD = 0; - computeJD(&x); - t = (time_t)(x.iJD/1000 - 21086676*(i64)10000); if( osLocaltime(&t, &sLocal) ){ sqlite3_result_error(pCtx, "local time unavailable", -1); - *pRc = SQLITE_ERROR; - return 0; + return SQLITE_ERROR; } - y.Y = sLocal.tm_year + 1900; - y.M = sLocal.tm_mon + 1; - y.D = sLocal.tm_mday; - y.h = sLocal.tm_hour; - y.m = sLocal.tm_min; - y.s = sLocal.tm_sec; - y.validYMD = 1; - y.validHMS = 1; - y.validJD = 0; - y.validTZ = 0; - computeJD(&y); - *pRc = SQLITE_OK; - return y.iJD - x.iJD; + p->Y = sLocal.tm_year + 1900 - iYearDiff; + p->M = sLocal.tm_mon + 1; + p->D = sLocal.tm_mday; + p->h = sLocal.tm_hour; + p->m = sLocal.tm_min; + p->s = sLocal.tm_sec + (p->iJD%1000)*0.001; + p->validYMD = 1; + p->validHMS = 1; + p->validJD = 0; + p->rawS = 0; + p->tz = 0; + p->isError = 0; + return SQLITE_OK; } #endif /* SQLITE_OMIT_LOCALTIME */ +/* +** The following table defines various date transformations of the form +** +** 'NNN days' +** +** Where NNN is an arbitrary floating-point number and "days" can be one +** of several units of time. +*/ +static const struct { + u8 nName; /* Length of the name */ + char zName[7]; /* Name of the transformation */ + float rLimit; /* Maximum NNN value for this transform */ + float rXform; /* Constant used for this transform */ +} aXformType[] = { + /* 0 */ { 6, "second", 4.6427e+14, 1.0 }, + /* 1 */ { 6, "minute", 7.7379e+12, 60.0 }, + /* 2 */ { 4, "hour", 1.2897e+11, 3600.0 }, + /* 3 */ { 3, "day", 5373485.0, 86400.0 }, + /* 4 */ { 5, "month", 176546.0, 2592000.0 }, + /* 5 */ { 4, "year", 14713.0, 31536000.0 }, +}; + +/* +** If the DateTime p is raw number, try to figure out if it is +** a julian day number of a unix timestamp. Set the p value +** appropriately. +*/ +static void autoAdjustDate(DateTime *p){ + if( !p->rawS || p->validJD ){ + p->rawS = 0; + }else if( p->s>=-21086676*(i64)10000 /* -4713-11-24 12:00:00 */ + && p->s<=(25340230*(i64)10000)+799 /* 9999-12-31 23:59:59 */ + ){ + double r = p->s*1000.0 + 210866760000000.0; + clearYMD_HMS_TZ(p); + p->iJD = (sqlite3_int64)(r + 0.5); + p->validJD = 1; + p->rawS = 0; + } +} + /* ** Process a modifier to a date-time stamp. The modifiers are ** as follows: @@ -554,31 +707,99 @@ static sqlite3_int64 localtimeOffset( ** NNN.NNNN seconds ** NNN months ** NNN years +** +/-YYYY-MM-DD HH:MM:SS.SSS +** ceiling +** floor ** start of month ** start of year ** start of week ** start of day ** weekday N ** unixepoch +** auto ** localtime ** utc +** subsec +** subsecond ** ** Return 0 on success and 1 if there is any kind of error. If the error ** is in a system call (i.e. localtime()), then an error message is written ** to context pCtx. If the error is an unrecognized modifier, no error is ** written to pCtx. */ -static int parseModifier(sqlite3_context *pCtx, const char *zMod, DateTime *p){ +static int parseModifier( + sqlite3_context *pCtx, /* Function context */ + const char *z, /* The text of the modifier */ + int n, /* Length of zMod in bytes */ + DateTime *p, /* The date/time value to be modified */ + int idx /* Parameter index of the modifier */ +){ int rc = 1; - int n; double r; - char *z, zBuf[30]; - z = zBuf; - for(n=0; n1 ) return 1; /* IMP: R-33611-57934 */ + autoAdjustDate(p); + rc = 0; + } + break; + } + case 'c': { + /* + ** ceiling + ** + ** Resolve day-of-month overflow by rolling forward into the next + ** month. As this is the default action, this modifier is really + ** a no-op that is only included for symmetry. See "floor". + */ + if( sqlite3_stricmp(z, "ceiling")==0 ){ + computeJD(p); + clearYMD_HMS_TZ(p); + rc = 0; + p->nFloor = 0; + } + break; + } + case 'f': { + /* + ** floor + ** + ** Resolve day-of-month overflow by rolling back to the end of the + ** previous month. + */ + if( sqlite3_stricmp(z, "floor")==0 ){ + computeJD(p); + p->iJD -= p->nFloor*86400000; + clearYMD_HMS_TZ(p); + rc = 0; + } + break; + } + case 'j': { + /* + ** julianday + ** + ** Always interpret the prior number as a julian-day value. If this + ** is not the first modifier, or if the prior argument is not a numeric + ** value in the allowed range of julian day numbers understood by + ** SQLite (0..5373484.5) then the result will be NULL. + */ + if( sqlite3_stricmp(z, "julianday")==0 ){ + if( idx>1 ) return 1; /* IMP: R-31176-64601 */ + if( p->validJD && p->rawS ){ + rc = 0; + p->rawS = 0; + } + } + break; + } #ifndef SQLITE_OMIT_LOCALTIME case 'l': { /* localtime @@ -586,10 +807,10 @@ static int parseModifier(sqlite3_context *pCtx, const char *zMod, DateTime *p){ ** Assuming the current time value is UTC (a.k.a. GMT), shift it to ** show local time. */ - if( strcmp(z, "localtime")==0 ){ - computeJD(p); - p->iJD += localtimeOffset(p, pCtx, &rc); - clearYMD_HMS_TZ(p); + if( sqlite3_stricmp(z, "localtime")==0 && sqlite3NotPureFunc(pCtx) ){ + rc = p->isLocal ? SQLITE_OK : toLocaltime(p, pCtx); + p->isUtc = 0; + p->isLocal = 1; } break; } @@ -598,29 +819,49 @@ static int parseModifier(sqlite3_context *pCtx, const char *zMod, DateTime *p){ /* ** unixepoch ** - ** Treat the current value of p->iJD as the number of + ** Treat the current value of p->s as the number of ** seconds since 1970. Convert to a real julian day number. */ - if( strcmp(z, "unixepoch")==0 && p->validJD ){ - p->iJD = (p->iJD + 43200)/86400 + 21086676*(i64)10000000; - clearYMD_HMS_TZ(p); - rc = 0; + if( sqlite3_stricmp(z, "unixepoch")==0 && p->rawS ){ + if( idx>1 ) return 1; /* IMP: R-49255-55373 */ + r = p->s*1000.0 + 210866760000000.0; + if( r>=0.0 && r<464269060800000.0 ){ + clearYMD_HMS_TZ(p); + p->iJD = (sqlite3_int64)(r + 0.5); + p->validJD = 1; + p->rawS = 0; + rc = 0; + } } #ifndef SQLITE_OMIT_LOCALTIME - else if( strcmp(z, "utc")==0 ){ - if( p->tzSet==0 ){ - sqlite3_int64 c1; + else if( sqlite3_stricmp(z, "utc")==0 && sqlite3NotPureFunc(pCtx) ){ + if( p->isUtc==0 ){ + i64 iOrigJD; /* Original localtime */ + i64 iGuess; /* Guess at the corresponding utc time */ + int cnt = 0; /* Safety to prevent infinite loop */ + i64 iErr; /* Guess is off by this much */ + computeJD(p); - c1 = localtimeOffset(p, pCtx, &rc); - if( rc==SQLITE_OK ){ - p->iJD -= c1; - clearYMD_HMS_TZ(p); - p->iJD += c1 - localtimeOffset(p, pCtx, &rc); - } - p->tzSet = 1; - }else{ - rc = SQLITE_OK; + iGuess = iOrigJD = p->iJD; + iErr = 0; + do{ + DateTime new; + memset(&new, 0, sizeof(new)); + iGuess -= iErr; + new.iJD = iGuess; + new.validJD = 1; + rc = toLocaltime(&new, pCtx); + if( rc ) return rc; + computeJD(&new); + iErr = new.iJD - iOrigJD; + }while( iErr && cnt++<3 ); + memset(p, 0, sizeof(*p)); + p->iJD = iGuess; + p->validJD = 1; + p->isUtc = 1; + p->isLocal = 0; } + rc = SQLITE_OK; } #endif break; @@ -633,12 +874,12 @@ static int parseModifier(sqlite3_context *pCtx, const char *zMod, DateTime *p){ ** weekday N where 0==Sunday, 1==Monday, and so forth. If the ** date is already on the appropriate weekday, this is a no-op. */ - if( strncmp(z, "weekday ", 8)==0 - && sqlite3AtoF(&z[8], &r, sqlite3Strlen30(&z[8]), SQLITE_UTF8) - && (n=(int)r)==r && n>=0 && r<7 ){ + if( sqlite3_strnicmp(z, "weekday ", 8)==0 + && sqlite3AtoF(&z[8], &r, sqlite3Strlen30(&z[8]), SQLITE_UTF8)>0 + && r>=0.0 && r<7.0 && (n=(int)r)==r ){ sqlite3_int64 Z; computeYMD_HMS(p); - p->validTZ = 0; + p->tz = 0; p->validJD = 0; computeJD(p); Z = ((p->iJD + 129600000)/86400000) % 7; @@ -655,24 +896,39 @@ static int parseModifier(sqlite3_context *pCtx, const char *zMod, DateTime *p){ ** ** Move the date backwards to the beginning of the current day, ** or month or year. + ** + ** subsecond + ** subsec + ** + ** Show subsecond precision in the output of datetime() and + ** unixepoch() and strftime('%s'). */ - if( strncmp(z, "start of ", 9)!=0 ) break; + if( sqlite3_strnicmp(z, "start of ", 9)!=0 ){ + if( sqlite3_stricmp(z, "subsec")==0 + || sqlite3_stricmp(z, "subsecond")==0 + ){ + p->useSubsec = 1; + rc = 0; + } + break; + } + if( !p->validJD && !p->validYMD && !p->validHMS ) break; z += 9; computeYMD(p); p->validHMS = 1; p->h = p->m = 0; p->s = 0.0; - p->validTZ = 0; + p->rawS = 0; + p->tz = 0; p->validJD = 0; - if( strcmp(z,"month")==0 ){ + if( sqlite3_stricmp(z,"month")==0 ){ p->D = 1; rc = 0; - }else if( strcmp(z,"year")==0 ){ - computeYMD(p); + }else if( sqlite3_stricmp(z,"year")==0 ){ p->M = 1; p->D = 1; rc = 0; - }else if( strcmp(z,"day")==0 ){ + }else if( sqlite3_stricmp(z,"day")==0 ){ rc = 0; } break; @@ -690,18 +946,75 @@ static int parseModifier(sqlite3_context *pCtx, const char *zMod, DateTime *p){ case '8': case '9': { double rRounder; - for(n=1; z[n] && z[n]!=':' && !sqlite3Isspace(z[n]); n++){} - if( !sqlite3AtoF(z, &r, n, SQLITE_UTF8) ){ - rc = 1; + int i; + int Y,M,D,h,m,x; + const char *z2 = z; + char z0 = z[0]; + for(n=1; z[n]; n++){ + if( z[n]==':' ) break; + if( sqlite3Isspace(z[n]) ) break; + if( z[n]=='-' ){ + if( n==5 && getDigits(&z[1], "40f", &Y)==1 ) break; + if( n==6 && getDigits(&z[1], "50f", &Y)==1 ) break; + } + } + if( sqlite3AtoF(z, &r, n, SQLITE_UTF8)<=0 ){ + assert( rc==1 ); break; } - if( z[n]==':' ){ + if( z[n]=='-' ){ + /* A modifier of the form (+|-)YYYY-MM-DD adds or subtracts the + ** specified number of years, months, and days. MM is limited to + ** the range 0-11 and DD is limited to 0-30. + */ + if( z0!='+' && z0!='-' ) break; /* Must start with +/- */ + if( n==5 ){ + if( getDigits(&z[1], "40f-20a-20d", &Y, &M, &D)!=3 ) break; + }else{ + assert( n==6 ); + if( getDigits(&z[1], "50f-20a-20d", &Y, &M, &D)!=3 ) break; + z++; + } + if( M>=12 ) break; /* M range 0..11 */ + if( D>=31 ) break; /* D range 0..30 */ + computeYMD_HMS(p); + p->validJD = 0; + if( z0=='-' ){ + p->Y -= Y; + p->M -= M; + D = -D; + }else{ + p->Y += Y; + p->M += M; + } + x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12; + p->Y += x; + p->M -= x*12; + computeFloor(p); + computeJD(p); + p->validHMS = 0; + p->validYMD = 0; + p->iJD += (i64)D*86400000; + if( z[11]==0 ){ + rc = 0; + break; + } + if( sqlite3Isspace(z[11]) + && getDigits(&z[12], "20c:20e", &h, &m)==2 + ){ + z2 = &z[12]; + n = 2; + }else{ + break; + } + } + if( z2[n]==':' ){ /* A modifier of the form (+|-)HH:MM:SS.FFF adds (or subtracts) the ** specified number of hours, minutes, seconds, and fractional seconds ** to the time. The ".FFF" may be omitted. The ":SS.FFF" may be ** omitted. */ - const char *z2 = z; + DateTime tx; sqlite3_int64 day; if( !sqlite3Isdigit(*z2) ) z2++; @@ -711,53 +1024,60 @@ static int parseModifier(sqlite3_context *pCtx, const char *zMod, DateTime *p){ tx.iJD -= 43200000; day = tx.iJD/86400000; tx.iJD -= day*86400000; - if( z[0]=='-' ) tx.iJD = -tx.iJD; + if( z0=='-' ) tx.iJD = -tx.iJD; computeJD(p); clearYMD_HMS_TZ(p); p->iJD += tx.iJD; rc = 0; break; } + + /* If control reaches this point, it means the transformation is + ** one of the forms like "+NNN days". */ z += n; while( sqlite3Isspace(*z) ) z++; n = sqlite3Strlen30(z); - if( n>10 || n<3 ) break; - if( z[n-1]=='s' ){ z[n-1] = 0; n--; } + if( n<3 || n>10 ) break; + if( sqlite3UpperToLower[(u8)z[n-1]]=='s' ) n--; computeJD(p); - rc = 0; + assert( rc==1 ); rRounder = r<0 ? -0.5 : +0.5; - if( n==3 && strcmp(z,"day")==0 ){ - p->iJD += (sqlite3_int64)(r*86400000.0 + rRounder); - }else if( n==4 && strcmp(z,"hour")==0 ){ - p->iJD += (sqlite3_int64)(r*(86400000.0/24.0) + rRounder); - }else if( n==6 && strcmp(z,"minute")==0 ){ - p->iJD += (sqlite3_int64)(r*(86400000.0/(24.0*60.0)) + rRounder); - }else if( n==6 && strcmp(z,"second")==0 ){ - p->iJD += (sqlite3_int64)(r*(86400000.0/(24.0*60.0*60.0)) + rRounder); - }else if( n==5 && strcmp(z,"month")==0 ){ - int x, y; - computeYMD_HMS(p); - p->M += (int)r; - x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12; - p->Y += x; - p->M -= x*12; - p->validJD = 0; - computeJD(p); - y = (int)r; - if( y!=r ){ - p->iJD += (sqlite3_int64)((r - y)*30.0*86400000.0 + rRounder); - } - }else if( n==4 && strcmp(z,"year")==0 ){ - int y = (int)r; - computeYMD_HMS(p); - p->Y += y; - p->validJD = 0; - computeJD(p); - if( y!=r ){ - p->iJD += (sqlite3_int64)((r - y)*365.0*86400000.0 + rRounder); + p->nFloor = 0; + for(i=0; i-aXformType[i].rLimit && rM += (int)r; + x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12; + p->Y += x; + p->M -= x*12; + computeFloor(p); + p->validJD = 0; + r -= (int)r; + break; + } + case 5: { /* Special processing to add years */ + int y = (int)r; + assert( strcmp(aXformType[5].zName,"year")==0 ); + computeYMD_HMS(p); + assert( p->M>=0 && p->M<=12 ); + p->Y += y; + computeFloor(p); + p->validJD = 0; + r -= (int)r; + break; + } + } + computeJD(p); + p->iJD += (sqlite3_int64)(r*1000.0*aXformType[i].rXform + rRounder); + rc = 0; + break; } - }else{ - rc = 1; } clearYMD_HMS_TZ(p); break; @@ -784,17 +1104,17 @@ static int isDate( sqlite3_value **argv, DateTime *p ){ - int i; + int i, n; const unsigned char *z; int eType; memset(p, 0, sizeof(*p)); if( argc==0 ){ + if( !sqlite3NotPureFunc(context) ) return 1; return setDateTimeToCurrent(context, p); } if( (eType = sqlite3_value_type(argv[0]))==SQLITE_FLOAT || eType==SQLITE_INTEGER ){ - p->iJD = (sqlite3_int64)(sqlite3_value_double(argv[0])*86400000.0 + 0.5); - p->validJD = 1; + setRawDateNumber(p, sqlite3_value_double(argv[0])); }else{ z = sqlite3_value_text(argv[0]); if( !z || parseDateOrTime(context, (char*)z, p) ){ @@ -803,7 +1123,16 @@ static int isDate( } for(i=1; iisError || !validJulianDay(p->iJD) ) return 1; + if( argc==1 && p->validYMD && p->D>28 ){ + /* Make sure a YYYY-MM-DD is normalized. + ** Example: 2023-02-31 -> 2023-03-03 */ + assert( p->validJD ); + p->validYMD = 0; } return 0; } @@ -831,6 +1160,28 @@ static void juliandayFunc( } } +/* +** unixepoch( TIMESTRING, MOD, MOD, ...) +** +** Return the number of seconds (including fractional seconds) since +** the unix epoch of 1970-01-01 00:00:00 GMT. +*/ +static void unixepochFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + DateTime x; + if( isDate(context, argc, argv, &x)==0 ){ + computeJD(&x); + if( x.useSubsec ){ + sqlite3_result_double(context, (x.iJD - 21086676*(i64)10000000)/1000.0); + }else{ + sqlite3_result_int64(context, x.iJD/1000 - 21086676*(i64)10000); + } + } +} + /* ** datetime( TIMESTRING, MOD, MOD, ...) ** @@ -843,11 +1194,51 @@ static void datetimeFunc( ){ DateTime x; if( isDate(context, argc, argv, &x)==0 ){ - char zBuf[100]; + int Y, s, n; + char zBuf[32]; computeYMD_HMS(&x); - sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d %02d:%02d:%02d", - x.Y, x.M, x.D, x.h, x.m, (int)(x.s)); - sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); + Y = x.Y; + if( Y<0 ) Y = -Y; + zBuf[1] = '0' + (Y/1000)%10; + zBuf[2] = '0' + (Y/100)%10; + zBuf[3] = '0' + (Y/10)%10; + zBuf[4] = '0' + (Y)%10; + zBuf[5] = '-'; + zBuf[6] = '0' + (x.M/10)%10; + zBuf[7] = '0' + (x.M)%10; + zBuf[8] = '-'; + zBuf[9] = '0' + (x.D/10)%10; + zBuf[10] = '0' + (x.D)%10; + zBuf[11] = ' '; + zBuf[12] = '0' + (x.h/10)%10; + zBuf[13] = '0' + (x.h)%10; + zBuf[14] = ':'; + zBuf[15] = '0' + (x.m/10)%10; + zBuf[16] = '0' + (x.m)%10; + zBuf[17] = ':'; + if( x.useSubsec ){ + s = (int)(1000.0*x.s + 0.5); + zBuf[18] = '0' + (s/10000)%10; + zBuf[19] = '0' + (s/1000)%10; + zBuf[20] = '.'; + zBuf[21] = '0' + (s/100)%10; + zBuf[22] = '0' + (s/10)%10; + zBuf[23] = '0' + (s)%10; + zBuf[24] = 0; + n = 24; + }else{ + s = (int)x.s; + zBuf[18] = '0' + (s/10)%10; + zBuf[19] = '0' + (s)%10; + zBuf[20] = 0; + n = 20; + } + if( x.Y<0 ){ + zBuf[0] = '-'; + sqlite3_result_text(context, zBuf, n, SQLITE_TRANSIENT); + }else{ + sqlite3_result_text(context, &zBuf[1], n-1, SQLITE_TRANSIENT); + } } } @@ -863,10 +1254,33 @@ static void timeFunc( ){ DateTime x; if( isDate(context, argc, argv, &x)==0 ){ - char zBuf[100]; + int s, n; + char zBuf[16]; computeHMS(&x); - sqlite3_snprintf(sizeof(zBuf), zBuf, "%02d:%02d:%02d", x.h, x.m, (int)x.s); - sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); + zBuf[0] = '0' + (x.h/10)%10; + zBuf[1] = '0' + (x.h)%10; + zBuf[2] = ':'; + zBuf[3] = '0' + (x.m/10)%10; + zBuf[4] = '0' + (x.m)%10; + zBuf[5] = ':'; + if( x.useSubsec ){ + s = (int)(1000.0*x.s + 0.5); + zBuf[6] = '0' + (s/10000)%10; + zBuf[7] = '0' + (s/1000)%10; + zBuf[8] = '.'; + zBuf[9] = '0' + (s/100)%10; + zBuf[10] = '0' + (s/10)%10; + zBuf[11] = '0' + (s)%10; + zBuf[12] = 0; + n = 12; + }else{ + s = (int)x.s; + zBuf[6] = '0' + (s/10)%10; + zBuf[7] = '0' + (s)%10; + zBuf[8] = 0; + n = 8; + } + sqlite3_result_text(context, zBuf, n, SQLITE_TRANSIENT); } } @@ -882,29 +1296,108 @@ static void dateFunc( ){ DateTime x; if( isDate(context, argc, argv, &x)==0 ){ - char zBuf[100]; + int Y; + char zBuf[16]; computeYMD(&x); - sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d", x.Y, x.M, x.D); - sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); + Y = x.Y; + if( Y<0 ) Y = -Y; + zBuf[1] = '0' + (Y/1000)%10; + zBuf[2] = '0' + (Y/100)%10; + zBuf[3] = '0' + (Y/10)%10; + zBuf[4] = '0' + (Y)%10; + zBuf[5] = '-'; + zBuf[6] = '0' + (x.M/10)%10; + zBuf[7] = '0' + (x.M)%10; + zBuf[8] = '-'; + zBuf[9] = '0' + (x.D/10)%10; + zBuf[10] = '0' + (x.D)%10; + zBuf[11] = 0; + if( x.Y<0 ){ + zBuf[0] = '-'; + sqlite3_result_text(context, zBuf, 11, SQLITE_TRANSIENT); + }else{ + sqlite3_result_text(context, &zBuf[1], 10, SQLITE_TRANSIENT); + } } } +/* +** Compute the number of days after the most recent January 1. +** +** In other words, compute the zero-based day number for the +** current year: +** +** Jan01 = 0, Jan02 = 1, ..., Jan31 = 30, Feb01 = 31, ... +** Dec31 = 364 or 365. +*/ +static int daysAfterJan01(DateTime *pDate){ + DateTime jan01 = *pDate; + assert( jan01.validYMD ); + assert( jan01.validHMS ); + assert( pDate->validJD ); + jan01.validJD = 0; + jan01.M = 1; + jan01.D = 1; + computeJD(&jan01); + return (int)((pDate->iJD-jan01.iJD+43200000)/86400000); +} + +/* +** Return the number of days after the most recent Monday. +** +** In other words, return the day of the week according +** to this code: +** +** 0=Monday, 1=Tuesday, 2=Wednesday, ..., 6=Sunday. +*/ +static int daysAfterMonday(DateTime *pDate){ + assert( pDate->validJD ); + return (int)((pDate->iJD+43200000)/86400000) % 7; +} + +/* +** Return the number of days after the most recent Sunday. +** +** In other words, return the day of the week according +** to this code: +** +** 0=Sunday, 1=Monday, 2=Tuesday, ..., 6=Saturday +*/ +static int daysAfterSunday(DateTime *pDate){ + assert( pDate->validJD ); + return (int)((pDate->iJD+129600000)/86400000) % 7; +} + /* ** strftime( FORMAT, TIMESTRING, MOD, MOD, ...) ** ** Return a string described by FORMAT. Conversions as follows: ** -** %d day of month +** %d day of month 01-31 +** %e day of month 1-31 ** %f ** fractional seconds SS.SSS +** %F ISO date. YYYY-MM-DD +** %G ISO year corresponding to %V 0000-9999. +** %g 2-digit ISO year corresponding to %V 00-99 ** %H hour 00-24 -** %j day of year 000-366 +** %k hour 0-24 (leading zero converted to space) +** %I hour 01-12 +** %j day of year 001-366 ** %J ** julian day number +** %l hour 1-12 (leading zero converted to space) ** %m month 01-12 ** %M minute 00-59 +** %p "AM" or "PM" +** %P "am" or "pm" +** %R time as HH:MM ** %s seconds since 1970-01-01 ** %S seconds 00-59 -** %w day of week 0-6 sunday==0 -** %W week of year 00-53 +** %T time as HH:MM:SS +** %u day of week 1-7 Monday==1, Sunday==7 +** %w day of week 0-6 Sunday==0, Monday==1 +** %U week of year 00-53 (First Sunday is start of week 01) +** %V week of year 01-53 (First week containing Thursday is week 01) +** %W week of year 00-53 (First Monday is start of week 01) ** %Y year 0000-9999 ** %% % */ @@ -914,131 +1407,161 @@ static void strftimeFunc( sqlite3_value **argv ){ DateTime x; - u64 n; size_t i,j; - char *z; sqlite3 *db; const char *zFmt; - char zBuf[100]; + sqlite3_str sRes; + + if( argc==0 ) return; zFmt = (const char*)sqlite3_value_text(argv[0]); if( zFmt==0 || isDate(context, argc-1, argv+1, &x) ) return; db = sqlite3_context_db_handle(context); - for(i=0, n=1; zFmt[i]; i++, n++){ - if( zFmt[i]=='%' ){ - switch( zFmt[i+1] ){ - case 'd': - case 'H': - case 'm': - case 'M': - case 'S': - case 'W': - n++; - /* fall thru */ - case 'w': - case '%': - break; - case 'f': - n += 8; - break; - case 'j': - n += 3; - break; - case 'Y': - n += 8; - break; - case 's': - case 'J': - n += 50; - break; - default: - return; /* ERROR. return a NULL */ - } - i++; - } - } - testcase( n==sizeof(zBuf)-1 ); - testcase( n==sizeof(zBuf) ); - testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH]+1 ); - testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH] ); - if( n(u64)db->aLimit[SQLITE_LIMIT_LENGTH] ){ - sqlite3_result_error_toobig(context); - return; - }else{ - z = sqlite3DbMallocRawNN(db, (int)n); - if( z==0 ){ - sqlite3_result_error_nomem(context); - return; - } - } + sqlite3StrAccumInit(&sRes, 0, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]); + computeJD(&x); computeYMD_HMS(&x); for(i=j=0; zFmt[i]; i++){ - if( zFmt[i]!='%' ){ - z[j++] = zFmt[i]; - }else{ - i++; - switch( zFmt[i] ){ - case 'd': sqlite3_snprintf(3, &z[j],"%02d",x.D); j+=2; break; - case 'f': { - double s = x.s; - if( s>59.999 ) s = 59.999; - sqlite3_snprintf(7, &z[j],"%06.3f", s); - j += sqlite3Strlen30(&z[j]); - break; - } - case 'H': sqlite3_snprintf(3, &z[j],"%02d",x.h); j+=2; break; - case 'W': /* Fall thru */ - case 'j': { - int nDay; /* Number of days since 1st day of year */ - DateTime y = x; - y.validJD = 0; - y.M = 1; - y.D = 1; - computeJD(&y); - nDay = (int)((x.iJD-y.iJD+43200000)/86400000); - if( zFmt[i]=='W' ){ - int wd; /* 0=Monday, 1=Tuesday, ... 6=Sunday */ - wd = (int)(((x.iJD+43200000)/86400000)%7); - sqlite3_snprintf(3, &z[j],"%02d",(nDay+7-wd)/7); - j += 2; - }else{ - sqlite3_snprintf(4, &z[j],"%03d",nDay+1); - j += 3; - } - break; - } - case 'J': { - sqlite3_snprintf(20, &z[j],"%.16g",x.iJD/86400000.0); - j+=sqlite3Strlen30(&z[j]); - break; - } - case 'm': sqlite3_snprintf(3, &z[j],"%02d",x.M); j+=2; break; - case 'M': sqlite3_snprintf(3, &z[j],"%02d",x.m); j+=2; break; - case 's': { - sqlite3_snprintf(30,&z[j],"%lld", - (i64)(x.iJD/1000 - 21086676*(i64)10000)); - j += sqlite3Strlen30(&z[j]); - break; + char cf; + if( zFmt[i]!='%' ) continue; + if( j59.999) ) s = 59.999; + sqlite3_str_appendf(&sRes, "%06.3f", s); + break; + } + case 'F': { + sqlite3_str_appendf(&sRes, "%04d-%02d-%02d", x.Y, x.M, x.D); + break; + } + case 'G': /* Fall thru */ + case 'g': { + DateTime y = x; + assert( y.validJD ); + /* Move y so that it is the Thursday in the same week as x */ + y.iJD += (3 - daysAfterMonday(&x))*86400000; + y.validYMD = 0; + computeYMD(&y); + if( cf=='g' ){ + sqlite3_str_appendf(&sRes, "%02d", y.Y%100); + }else{ + sqlite3_str_appendf(&sRes, "%04d", y.Y); } - case 'S': sqlite3_snprintf(3,&z[j],"%02d",(int)x.s); j+=2; break; - case 'w': { - z[j++] = (char)(((x.iJD+129600000)/86400000) % 7) + '0'; - break; + break; + } + case 'H': + case 'k': { + sqlite3_str_appendf(&sRes, cf=='H' ? "%02d" : "%2d", x.h); + break; + } + case 'I': /* Fall thru */ + case 'l': { + int h = x.h; + if( h>12 ) h -= 12; + if( h==0 ) h = 12; + sqlite3_str_appendf(&sRes, cf=='I' ? "%02d" : "%2d", h); + break; + } + case 'j': { /* Day of year. Jan01==1, Jan02==2, and so forth */ + sqlite3_str_appendf(&sRes,"%03d",daysAfterJan01(&x)+1); + break; + } + case 'J': { /* Julian day number. (Non-standard) */ + sqlite3_str_appendf(&sRes,"%.16g",x.iJD/86400000.0); + break; + } + case 'm': { + sqlite3_str_appendf(&sRes,"%02d",x.M); + break; + } + case 'M': { + sqlite3_str_appendf(&sRes,"%02d",x.m); + break; + } + case 'p': /* Fall thru */ + case 'P': { + if( x.h>=12 ){ + sqlite3_str_append(&sRes, cf=='p' ? "PM" : "pm", 2); + }else{ + sqlite3_str_append(&sRes, cf=='p' ? "AM" : "am", 2); } - case 'Y': { - sqlite3_snprintf(5,&z[j],"%04d",x.Y); j+=sqlite3Strlen30(&z[j]); - break; + break; + } + case 'R': { + sqlite3_str_appendf(&sRes, "%02d:%02d", x.h, x.m); + break; + } + case 's': { + if( x.useSubsec ){ + sqlite3_str_appendf(&sRes,"%.3f", + (x.iJD - 21086676*(i64)10000000)/1000.0); + }else{ + i64 iS = (i64)(x.iJD/1000 - 21086676*(i64)10000); + sqlite3_str_appendf(&sRes,"%lld",iS); } - default: z[j++] = '%'; break; + break; + } + case 'S': { + sqlite3_str_appendf(&sRes,"%02d",(int)x.s); + break; + } + case 'T': { + sqlite3_str_appendf(&sRes,"%02d:%02d:%02d", x.h, x.m, (int)x.s); + break; + } + case 'u': /* Day of week. 1 to 7. Monday==1, Sunday==7 */ + case 'w': { /* Day of week. 0 to 6. Sunday==0, Monday==1 */ + char c = (char)daysAfterSunday(&x) + '0'; + if( c=='0' && cf=='u' ) c = '7'; + sqlite3_str_appendchar(&sRes, 1, c); + break; + } + case 'U': { /* Week num. 00-53. First Sun of the year is week 01 */ + sqlite3_str_appendf(&sRes,"%02d", + (daysAfterJan01(&x)-daysAfterSunday(&x)+7)/7); + break; + } + case 'V': { /* Week num. 01-53. First week with a Thur is week 01 */ + DateTime y = x; + /* Adjust y so that is the Thursday in the same week as x */ + assert( y.validJD ); + y.iJD += (3 - daysAfterMonday(&x))*86400000; + y.validYMD = 0; + computeYMD(&y); + sqlite3_str_appendf(&sRes,"%02d", daysAfterJan01(&y)/7+1); + break; + } + case 'W': { /* Week num. 00-53. First Mon of the year is week 01 */ + sqlite3_str_appendf(&sRes,"%02d", + (daysAfterJan01(&x)-daysAfterMonday(&x)+7)/7); + break; + } + case 'Y': { + sqlite3_str_appendf(&sRes,"%04d",x.Y); + break; + } + case '%': { + sqlite3_str_appendchar(&sRes, 1, '%'); + break; + } + default: { + sqlite3_str_reset(&sRes); + return; } } } - z[j] = 0; - sqlite3_result_text(context, z, -1, - z==zBuf ? SQLITE_TRANSIENT : SQLITE_DYNAMIC); + if( j=d2.iJD ){ + sign = '+'; + Y = d1.Y - d2.Y; + if( Y ){ + d2.Y = d1.Y; + d2.validJD = 0; + computeJD(&d2); + } + M = d1.M - d2.M; + if( M<0 ){ + Y--; + M += 12; + } + if( M!=0 ){ + d2.M = d1.M; + d2.validJD = 0; + computeJD(&d2); + } + while( d1.iJDd2.iJD ){ + M--; + if( M<0 ){ + M = 11; + Y--; + } + d2.M++; + if( d2.M>12 ){ + d2.M = 1; + d2.Y++; + } + d2.validJD = 0; + computeJD(&d2); + } + d1.iJD = d2.iJD - d1.iJD; + d1.iJD += (u64)1486995408 * (u64)100000; + } + clearYMD_HMS_TZ(&d1); + computeYMD_HMS(&d1); + sqlite3StrAccumInit(&sRes, 0, 0, 0, 100); + sqlite3_str_appendf(&sRes, "%c%04d-%02d-%02d %02d:%02d:%06.3f", + sign, Y, M, d1.D-1, d1.h, d1.m, d1.s); + sqlite3ResultStrAccum(context, &sRes); +} + + /* ** current_timestamp() ** @@ -1103,7 +1735,6 @@ static void currentTimeFunc( ){ time_t t; char *zFormat = (char *)sqlite3_user_data(context); - sqlite3 *db; sqlite3_int64 iT; struct tm *pTm; struct tm sNow; @@ -1118,10 +1749,10 @@ static void currentTimeFunc( #if HAVE_GMTIME_R pTm = gmtime_r(&t, &sNow); #else - sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); + sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN)); pTm = gmtime(&t); if( pTm ) memcpy(&sNow, pTm, sizeof(sNow)); - sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); + sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN)); #endif if( pTm ){ strftime(zBuf, 20, zFormat, &sNow); @@ -1130,19 +1761,54 @@ static void currentTimeFunc( } #endif +#if !defined(SQLITE_OMIT_DATETIME_FUNCS) && defined(SQLITE_DEBUG) +/* +** datedebug(...) +** +** This routine returns JSON that describes the internal DateTime object. +** Used for debugging and testing only. Subject to change. +*/ +static void datedebugFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + DateTime x; + if( isDate(context, argc, argv, &x)==0 ){ + char *zJson; + zJson = sqlite3_mprintf( + "{iJD:%lld,Y:%d,M:%d,D:%d,h:%d,m:%d,tz:%d," + "s:%.3f,validJD:%d,validYMS:%d,validHMS:%d," + "nFloor:%d,rawS:%d,isError:%d,useSubsec:%d," + "isUtc:%d,isLocal:%d}", + x.iJD, x.Y, x.M, x.D, x.h, x.m, x.tz, + x.s, x.validJD, x.validYMD, x.validHMS, + x.nFloor, x.rawS, x.isError, x.useSubsec, + x.isUtc, x.isLocal); + sqlite3_result_text(context, zJson, -1, sqlite3_free); + } +} +#endif /* !SQLITE_OMIT_DATETIME_FUNCS && SQLITE_DEBUG */ + + /* ** This function registered all of the above C functions as SQL ** functions. This should be the only routine in this file with ** external linkage. */ void sqlite3RegisterDateTimeFunctions(void){ - static SQLITE_WSD FuncDef aDateTimeFuncs[] = { + static FuncDef aDateTimeFuncs[] = { #ifndef SQLITE_OMIT_DATETIME_FUNCS - DFUNCTION(julianday, -1, 0, 0, juliandayFunc ), - DFUNCTION(date, -1, 0, 0, dateFunc ), - DFUNCTION(time, -1, 0, 0, timeFunc ), - DFUNCTION(datetime, -1, 0, 0, datetimeFunc ), - DFUNCTION(strftime, -1, 0, 0, strftimeFunc ), + PURE_DATE(julianday, -1, 0, 0, juliandayFunc ), + PURE_DATE(unixepoch, -1, 0, 0, unixepochFunc ), + PURE_DATE(date, -1, 0, 0, dateFunc ), + PURE_DATE(time, -1, 0, 0, timeFunc ), + PURE_DATE(datetime, -1, 0, 0, datetimeFunc ), + PURE_DATE(strftime, -1, 0, 0, strftimeFunc ), + PURE_DATE(timediff, 2, 0, 0, timediffFunc ), +#ifdef SQLITE_DEBUG + PURE_DATE(datedebug, -1, 0, 0, datedebugFunc ), +#endif DFUNCTION(current_time, 0, 0, 0, ctimeFunc ), DFUNCTION(current_timestamp, 0, 0, 0, ctimestampFunc), DFUNCTION(current_date, 0, 0, 0, cdateFunc ), @@ -1152,11 +1818,5 @@ void sqlite3RegisterDateTimeFunctions(void){ STR_FUNCTION(current_timestamp, 0, "%Y-%m-%d %H:%M:%S", 0, currentTimeFunc), #endif }; - int i; - FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions); - FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aDateTimeFuncs); - - for(i=0; idb = db; + } + + *ppVtab = (sqlite3_vtab*)pTab; + return rc; +} + +/* +** Disconnect from or destroy a dbpagevfs virtual table. +*/ +static int dbpageDisconnect(sqlite3_vtab *pVtab){ + sqlite3_free(pVtab); + return SQLITE_OK; +} + +/* +** idxNum: +** +** 0 schema=main, full table scan +** 1 schema=main, pgno=?1 +** 2 schema=?1, full table scan +** 3 schema=?1, pgno=?2 +*/ +static int dbpageBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){ + int i; + int iPlan = 0; + (void)tab; + + /* If there is a schema= constraint, it must be honored. Report a + ** ridiculously large estimated cost if the schema= constraint is + ** unavailable + */ + for(i=0; inConstraint; i++){ + struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[i]; + if( p->iColumn!=DBPAGE_COLUMN_SCHEMA ) continue; + if( p->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue; + if( !p->usable ){ + /* No solution. */ + return SQLITE_CONSTRAINT; + } + iPlan = 2; + pIdxInfo->aConstraintUsage[i].argvIndex = 1; + pIdxInfo->aConstraintUsage[i].omit = 1; + break; + } + + /* If we reach this point, it means that either there is no schema= + ** constraint (in which case we use the "main" schema) or else the + ** schema constraint was accepted. Lower the estimated cost accordingly + */ + pIdxInfo->estimatedCost = 1.0e6; + + /* Check for constraints against pgno */ + for(i=0; inConstraint; i++){ + struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[i]; + if( p->usable && p->iColumn<=0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ ){ + pIdxInfo->estimatedRows = 1; + pIdxInfo->idxFlags = SQLITE_INDEX_SCAN_UNIQUE; + pIdxInfo->estimatedCost = 1.0; + pIdxInfo->aConstraintUsage[i].argvIndex = iPlan ? 2 : 1; + pIdxInfo->aConstraintUsage[i].omit = 1; + iPlan |= 1; + break; + } + } + pIdxInfo->idxNum = iPlan; + + if( pIdxInfo->nOrderBy>=1 + && pIdxInfo->aOrderBy[0].iColumn<=0 + && pIdxInfo->aOrderBy[0].desc==0 + ){ + pIdxInfo->orderByConsumed = 1; + } + return SQLITE_OK; +} + +/* +** Open a new dbpagevfs cursor. +*/ +static int dbpageOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){ + DbpageCursor *pCsr; + + pCsr = (DbpageCursor *)sqlite3_malloc64(sizeof(DbpageCursor)); + if( pCsr==0 ){ + return SQLITE_NOMEM_BKPT; + }else{ + memset(pCsr, 0, sizeof(DbpageCursor)); + pCsr->base.pVtab = pVTab; + pCsr->pgno = 0; + } + + *ppCursor = (sqlite3_vtab_cursor *)pCsr; + return SQLITE_OK; +} + +/* +** Close a dbpagevfs cursor. +*/ +static int dbpageClose(sqlite3_vtab_cursor *pCursor){ + DbpageCursor *pCsr = (DbpageCursor *)pCursor; + if( pCsr->pPage1 ) sqlite3PagerUnrefPageOne(pCsr->pPage1); + sqlite3_free(pCsr); + return SQLITE_OK; +} + +/* +** Move a dbpagevfs cursor to the next entry in the file. +*/ +static int dbpageNext(sqlite3_vtab_cursor *pCursor){ + int rc = SQLITE_OK; + DbpageCursor *pCsr = (DbpageCursor *)pCursor; + pCsr->pgno++; + return rc; +} + +static int dbpageEof(sqlite3_vtab_cursor *pCursor){ + DbpageCursor *pCsr = (DbpageCursor *)pCursor; + return pCsr->pgno > pCsr->mxPgno; +} + +/* +** idxNum: +** +** 0 schema=main, full table scan +** 1 schema=main, pgno=?1 +** 2 schema=?1, full table scan +** 3 schema=?1, pgno=?2 +** +** idxStr is not used +*/ +static int dbpageFilter( + sqlite3_vtab_cursor *pCursor, + int idxNum, const char *idxStr, + int argc, sqlite3_value **argv +){ + DbpageCursor *pCsr = (DbpageCursor *)pCursor; + DbpageTable *pTab = (DbpageTable *)pCursor->pVtab; + int rc; + sqlite3 *db = pTab->db; + Btree *pBt; + + UNUSED_PARAMETER(idxStr); + UNUSED_PARAMETER(argc); + + /* Default setting is no rows of result */ + pCsr->pgno = 1; + pCsr->mxPgno = 0; + + if( idxNum & 2 ){ + const char *zSchema; + assert( argc>=1 ); + zSchema = (const char*)sqlite3_value_text(argv[0]); + pCsr->iDb = sqlite3FindDbName(db, zSchema); + if( pCsr->iDb<0 ) return SQLITE_OK; + }else{ + pCsr->iDb = 0; + } + pBt = db->aDb[pCsr->iDb].pBt; + if( NEVER(pBt==0) ) return SQLITE_OK; + pCsr->pPager = sqlite3BtreePager(pBt); + pCsr->szPage = sqlite3BtreeGetPageSize(pBt); + pCsr->mxPgno = sqlite3BtreeLastPage(pBt); + if( idxNum & 1 ){ + assert( argc>(idxNum>>1) ); + pCsr->pgno = sqlite3_value_int(argv[idxNum>>1]); + if( pCsr->pgno<1 || pCsr->pgno>pCsr->mxPgno ){ + pCsr->pgno = 1; + pCsr->mxPgno = 0; + }else{ + pCsr->mxPgno = pCsr->pgno; + } + }else{ + assert( pCsr->pgno==1 ); + } + if( pCsr->pPage1 ) sqlite3PagerUnrefPageOne(pCsr->pPage1); + rc = sqlite3PagerGet(pCsr->pPager, 1, &pCsr->pPage1, 0); + return rc; +} + +static int dbpageColumn( + sqlite3_vtab_cursor *pCursor, + sqlite3_context *ctx, + int i +){ + DbpageCursor *pCsr = (DbpageCursor *)pCursor; + int rc = SQLITE_OK; + switch( i ){ + case 0: { /* pgno */ + sqlite3_result_int64(ctx, (sqlite3_int64)pCsr->pgno); + break; + } + case 1: { /* data */ + DbPage *pDbPage = 0; + if( pCsr->pgno==(Pgno)((PENDING_BYTE/pCsr->szPage)+1) ){ + /* The pending byte page. Assume it is zeroed out. Attempting to + ** request this page from the page is an SQLITE_CORRUPT error. */ + sqlite3_result_zeroblob(ctx, pCsr->szPage); + }else{ + rc = sqlite3PagerGet(pCsr->pPager, pCsr->pgno, (DbPage**)&pDbPage, 0); + if( rc==SQLITE_OK ){ + sqlite3_result_blob(ctx, sqlite3PagerGetData(pDbPage), pCsr->szPage, + SQLITE_TRANSIENT); + } + sqlite3PagerUnref(pDbPage); + } + break; + } + default: { /* schema */ + sqlite3 *db = sqlite3_context_db_handle(ctx); + sqlite3_result_text(ctx, db->aDb[pCsr->iDb].zDbSName, -1, SQLITE_STATIC); + break; + } + } + return rc; +} + +static int dbpageRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){ + DbpageCursor *pCsr = (DbpageCursor *)pCursor; + *pRowid = pCsr->pgno; + return SQLITE_OK; +} + +/* +** Open write transactions. Since we do not know in advance which database +** files will be written by the sqlite_dbpage virtual table, start a write +** transaction on them all. +** +** Return SQLITE_OK if successful, or an SQLite error code otherwise. +*/ +static int dbpageBeginTrans(DbpageTable *pTab){ + sqlite3 *db = pTab->db; + int rc = SQLITE_OK; + int i; + for(i=0; rc==SQLITE_OK && inDb; i++){ + Btree *pBt = db->aDb[i].pBt; + if( pBt ) rc = sqlite3BtreeBeginTrans(pBt, 1, 0); + } + return rc; +} + +static int dbpageUpdate( + sqlite3_vtab *pVtab, + int argc, + sqlite3_value **argv, + sqlite_int64 *pRowid +){ + DbpageTable *pTab = (DbpageTable *)pVtab; + Pgno pgno; + DbPage *pDbPage = 0; + int rc = SQLITE_OK; + char *zErr = 0; + int iDb; + Btree *pBt; + Pager *pPager; + int szPage; + int isInsert; + + (void)pRowid; + if( pTab->db->flags & SQLITE_Defensive ){ + zErr = "read-only"; + goto update_fail; + } + if( argc==1 ){ + zErr = "cannot delete"; + goto update_fail; + } + if( sqlite3_value_type(argv[0])==SQLITE_NULL ){ + pgno = (Pgno)sqlite3_value_int64(argv[2]); + isInsert = 1; + }else{ + pgno = (Pgno)sqlite3_value_int64(argv[0]); + if( (Pgno)sqlite3_value_int(argv[1])!=pgno ){ + zErr = "cannot insert"; + goto update_fail; + } + isInsert = 0; + } + if( sqlite3_value_type(argv[4])==SQLITE_NULL ){ + iDb = 0; + }else{ + const char *zSchema = (const char*)sqlite3_value_text(argv[4]); + iDb = sqlite3FindDbName(pTab->db, zSchema); + if( iDb<0 ){ + zErr = "no such schema"; + goto update_fail; + } + } + pBt = pTab->db->aDb[iDb].pBt; + if( pgno<1 || NEVER(pBt==0) ){ + zErr = "bad page number"; + goto update_fail; + } + szPage = sqlite3BtreeGetPageSize(pBt); + if( sqlite3_value_type(argv[3])!=SQLITE_BLOB + || sqlite3_value_bytes(argv[3])!=szPage + ){ + if( sqlite3_value_type(argv[3])==SQLITE_NULL && isInsert && pgno>1 ){ + /* "INSERT INTO dbpage($PGNO,NULL)" causes page number $PGNO and + ** all subsequent pages to be deleted. */ + pTab->iDbTrunc = iDb; + pTab->pgnoTrunc = pgno-1; + pgno = 1; + }else{ + zErr = "bad page value"; + goto update_fail; + } + } + + if( dbpageBeginTrans(pTab)!=SQLITE_OK ){ + zErr = "failed to open transaction"; + goto update_fail; + } + + pPager = sqlite3BtreePager(pBt); + rc = sqlite3PagerGet(pPager, pgno, (DbPage**)&pDbPage, 0); + if( rc==SQLITE_OK ){ + const void *pData = sqlite3_value_blob(argv[3]); + if( (rc = sqlite3PagerWrite(pDbPage))==SQLITE_OK && pData ){ + unsigned char *aPage = sqlite3PagerGetData(pDbPage); + memcpy(aPage, pData, szPage); + pTab->pgnoTrunc = 0; + } + } + if( rc!=SQLITE_OK ){ + pTab->pgnoTrunc = 0; + } + sqlite3PagerUnref(pDbPage); + return rc; + +update_fail: + pTab->pgnoTrunc = 0; + sqlite3_free(pVtab->zErrMsg); + pVtab->zErrMsg = sqlite3_mprintf("%s", zErr); + return SQLITE_ERROR; +} + +static int dbpageBegin(sqlite3_vtab *pVtab){ + DbpageTable *pTab = (DbpageTable *)pVtab; + pTab->pgnoTrunc = 0; + return SQLITE_OK; +} + +/* Invoke sqlite3PagerTruncate() as necessary, just prior to COMMIT +*/ +static int dbpageSync(sqlite3_vtab *pVtab){ + DbpageTable *pTab = (DbpageTable *)pVtab; + if( pTab->pgnoTrunc>0 ){ + Btree *pBt = pTab->db->aDb[pTab->iDbTrunc].pBt; + Pager *pPager = sqlite3BtreePager(pBt); + sqlite3BtreeEnter(pBt); + if( pTab->pgnoTruncpgnoTrunc); + } + sqlite3BtreeLeave(pBt); + } + pTab->pgnoTrunc = 0; + return SQLITE_OK; +} + +/* Cancel any pending truncate. +*/ +static int dbpageRollbackTo(sqlite3_vtab *pVtab, int notUsed1){ + DbpageTable *pTab = (DbpageTable *)pVtab; + pTab->pgnoTrunc = 0; + (void)notUsed1; + return SQLITE_OK; +} + +/* +** Invoke this routine to register the "dbpage" virtual table module +*/ +int sqlite3DbpageRegister(sqlite3 *db){ + static sqlite3_module dbpage_module = { + 2, /* iVersion */ + dbpageConnect, /* xCreate */ + dbpageConnect, /* xConnect */ + dbpageBestIndex, /* xBestIndex */ + dbpageDisconnect, /* xDisconnect */ + dbpageDisconnect, /* xDestroy */ + dbpageOpen, /* xOpen - open a cursor */ + dbpageClose, /* xClose - close a cursor */ + dbpageFilter, /* xFilter - configure scan constraints */ + dbpageNext, /* xNext - advance a cursor */ + dbpageEof, /* xEof - check for end of scan */ + dbpageColumn, /* xColumn - read data */ + dbpageRowid, /* xRowid - read data */ + dbpageUpdate, /* xUpdate */ + dbpageBegin, /* xBegin */ + dbpageSync, /* xSync */ + 0, /* xCommit */ + 0, /* xRollback */ + 0, /* xFindMethod */ + 0, /* xRename */ + 0, /* xSavepoint */ + 0, /* xRelease */ + dbpageRollbackTo, /* xRollbackTo */ + 0, /* xShadowName */ + 0 /* xIntegrity */ + }; + return sqlite3_create_module(db, "sqlite_dbpage", &dbpage_module, 0); +} +#elif defined(SQLITE_ENABLE_DBPAGE_VTAB) +int sqlite3DbpageRegister(sqlite3 *db){ return SQLITE_OK; } +#endif /* SQLITE_ENABLE_DBSTAT_VTAB */ diff --git a/src/dbstat.c b/src/dbstat.c index 5e42cdfe38..d635a82975 100644 --- a/src/dbstat.c +++ b/src/dbstat.c @@ -12,7 +12,7 @@ ** ** This file contains an implementation of the "dbstat" virtual table. ** -** The dbstat virtual table is used to extract low-level formatting +** The dbstat virtual table is used to extract low-level storage ** information from an SQLite database in order to implement the ** "sqlite3_analyzer" utility. See the ../tool/spaceanal.tcl script ** for an example implementation. @@ -25,6 +25,15 @@ #if (defined(SQLITE_ENABLE_DBSTAT_VTAB) || defined(SQLITE_TEST)) \ && !defined(SQLITE_OMIT_VIRTUALTABLE) +/* +** The pager and btree modules arrange objects in memory so that there are +** always approximately 200 bytes of addressable memory following each page +** buffer. This way small buffer overreads caused by corrupt database pages +** do not cause undefined behaviour. This module pads each page buffer +** by the following number of bytes for the same purpose. +*/ +#define DBSTAT_PAGE_PADDING_BYTES 256 + /* ** Page paths: ** @@ -56,27 +65,30 @@ ** ** '/1c2/000/' // Left-most child of 451st child of root */ -#define VTAB_SCHEMA \ - "CREATE TABLE xx( " \ - " name STRING, /* Name of table or index */" \ - " path INTEGER, /* Path to page from root */" \ - " pageno INTEGER, /* Page number */" \ - " pagetype STRING, /* 'internal', 'leaf' or 'overflow' */" \ - " ncell INTEGER, /* Cells on page (0 for overflow) */" \ - " payload INTEGER, /* Bytes of payload on this page */" \ - " unused INTEGER, /* Bytes of unused space on this page */" \ - " mx_payload INTEGER, /* Largest payload size of all cells */" \ - " pgoffset INTEGER, /* Offset of page in file */" \ - " pgsize INTEGER, /* Size of the page */" \ - " schema TEXT HIDDEN /* Database schema being analyzed */" \ - ");" - - +static const char zDbstatSchema[] = + "CREATE TABLE x(" + " name TEXT," /* 0 Name of table or index */ + " path TEXT," /* 1 Path to page from root (NULL for agg) */ + " pageno INTEGER," /* 2 Page number (page count for aggregates) */ + " pagetype TEXT," /* 3 'internal', 'leaf', 'overflow', or NULL */ + " ncell INTEGER," /* 4 Cells on page (0 for overflow) */ + " payload INTEGER," /* 5 Bytes of payload on this page */ + " unused INTEGER," /* 6 Bytes of unused space on this page */ + " mx_payload INTEGER," /* 7 Largest payload size of all cells */ + " pgoffset INTEGER," /* 8 Offset of page in file (NULL for agg) */ + " pgsize INTEGER," /* 9 Size of the page (sum for aggregate) */ + " schema TEXT HIDDEN," /* 10 Database schema being analyzed */ + " aggregate BOOLEAN HIDDEN" /* 11 aggregate info for each table */ + ")" +; + +/* Forward reference to data structured used in this module */ typedef struct StatTable StatTable; typedef struct StatCursor StatCursor; typedef struct StatPage StatPage; typedef struct StatCell StatCell; +/* Size information for a single cell within a btree page */ struct StatCell { int nLocal; /* Bytes of local payload */ u32 iChildPg; /* Child node (or 0 if this is a leaf) */ @@ -86,11 +98,11 @@ struct StatCell { int iOvfl; /* Iterates through aOvfl[] */ }; +/* Size information for a single btree page */ struct StatPage { - u32 iPgno; - DbPage *pPg; - int iCell; - + u32 iPgno; /* Page number */ + u8 *aPg; /* Page buffer from sqlite3_malloc() */ + int iCell; /* Current cell */ char *zPath; /* Path to this page */ /* Variables populated by statDecodePage(): */ @@ -99,34 +111,38 @@ struct StatPage { int nUnused; /* Number of unused bytes on page */ StatCell *aCell; /* Array of parsed cells */ u32 iRightChildPg; /* Right-child page number (or 0) */ - int nMxPayload; /* Largest payload of any cell on this page */ + int nMxPayload; /* Largest payload of any cell on the page */ }; +/* The cursor for scanning the dbstat virtual table */ struct StatCursor { - sqlite3_vtab_cursor base; + sqlite3_vtab_cursor base; /* base class. MUST BE FIRST! */ sqlite3_stmt *pStmt; /* Iterates through set of root pages */ - int isEof; /* After pStmt has returned SQLITE_DONE */ + u8 isEof; /* After pStmt has returned SQLITE_DONE */ + u8 isAgg; /* Aggregate results for each table */ int iDb; /* Schema used for this query */ - StatPage aPage[32]; + StatPage aPage[32]; /* Pages in path to current page */ int iPage; /* Current entry in aPage[] */ /* Values to return. */ + u32 iPageno; /* Value of 'pageno' column */ char *zName; /* Value of 'name' column */ char *zPath; /* Value of 'path' column */ - u32 iPageno; /* Value of 'pageno' column */ char *zPagetype; /* Value of 'pagetype' column */ + int nPage; /* Number of pages in current btree */ int nCell; /* Value of 'ncell' column */ - int nPayload; /* Value of 'payload' column */ - int nUnused; /* Value of 'unused' column */ int nMxPayload; /* Value of 'mx_payload' column */ + i64 nUnused; /* Value of 'unused' column */ + i64 nPayload; /* Value of 'payload' column */ i64 iOffset; /* Value of 'pgOffset' column */ - int szPage; /* Value of 'pgSize' column */ + i64 szPage; /* Value of 'pgSize' column */ }; +/* An instance of the DBSTAT virtual table */ struct StatTable { - sqlite3_vtab base; - sqlite3 *db; + sqlite3_vtab base; /* base class. MUST BE FIRST! */ + sqlite3 *db; /* Database connection that owns this vtab */ int iDb; /* Index of database to analyze */ }; @@ -135,7 +151,7 @@ struct StatTable { #endif /* -** Connect to or create a statvfs virtual table. +** Connect to or create a new DBSTAT virtual table. */ static int statConnect( sqlite3 *db, @@ -147,6 +163,7 @@ static int statConnect( StatTable *pTab = 0; int rc = SQLITE_OK; int iDb; + (void)pAux; if( argc>=4 ){ Token nm; @@ -159,10 +176,11 @@ static int statConnect( }else{ iDb = 0; } - rc = sqlite3_declare_vtab(db, VTAB_SCHEMA); + sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY); + rc = sqlite3_declare_vtab(db, zDbstatSchema); if( rc==SQLITE_OK ){ pTab = (StatTable *)sqlite3_malloc64(sizeof(StatTable)); - if( pTab==0 ) rc = SQLITE_NOMEM; + if( pTab==0 ) rc = SQLITE_NOMEM_BKPT; } assert( rc==SQLITE_OK || pTab==0 ); @@ -177,7 +195,7 @@ static int statConnect( } /* -** Disconnect from or destroy a statvfs virtual table. +** Disconnect from or destroy the DBSTAT virtual table. */ static int statDisconnect(sqlite3_vtab *pVtab){ sqlite3_free(pVtab); @@ -185,16 +203,21 @@ static int statDisconnect(sqlite3_vtab *pVtab){ } /* -** There is no "best-index". This virtual table always does a linear -** scan. However, a schema=? constraint should cause this table to -** operate on a different database schema, so check for it. +** Compute the best query strategy and return the result in idxNum. ** -** idxNum is normally 0, but will be 1 if a schema=? constraint exists. +** idxNum-Bit Meaning +** ---------- ---------------------------------------------- +** 0x01 There is a schema=? term in the WHERE clause +** 0x02 There is a name=? term in the WHERE clause +** 0x04 There is an aggregate=? term in the WHERE clause +** 0x08 Output should be ordered by name and path */ static int statBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){ int i; - - pIdxInfo->estimatedCost = 1.0e6; /* Initial cost estimate */ + int iSchema = -1; + int iName = -1; + int iAgg = -1; + (void)tab; /* Look for a valid schema=? constraint. If found, change the idxNum to ** 1 and request the value of that constraint be sent to xFilter. And @@ -202,16 +225,41 @@ static int statBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){ ** used. */ for(i=0; inConstraint; i++){ - if( pIdxInfo->aConstraint[i].usable==0 ) continue; if( pIdxInfo->aConstraint[i].op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue; - if( pIdxInfo->aConstraint[i].iColumn!=10 ) continue; - pIdxInfo->idxNum = 1; - pIdxInfo->estimatedCost = 1.0; - pIdxInfo->aConstraintUsage[i].argvIndex = 1; - pIdxInfo->aConstraintUsage[i].omit = 1; - break; + if( pIdxInfo->aConstraint[i].usable==0 ){ + /* Force DBSTAT table should always be the right-most table in a join */ + return SQLITE_CONSTRAINT; + } + switch( pIdxInfo->aConstraint[i].iColumn ){ + case 0: { /* name */ + iName = i; + break; + } + case 10: { /* schema */ + iSchema = i; + break; + } + case 11: { /* aggregate */ + iAgg = i; + break; + } + } } - + i = 0; + if( iSchema>=0 ){ + pIdxInfo->aConstraintUsage[iSchema].argvIndex = ++i; + pIdxInfo->aConstraintUsage[iSchema].omit = 1; + pIdxInfo->idxNum |= 0x01; + } + if( iName>=0 ){ + pIdxInfo->aConstraintUsage[iName].argvIndex = ++i; + pIdxInfo->idxNum |= 0x02; + } + if( iAgg>=0 ){ + pIdxInfo->aConstraintUsage[iAgg].argvIndex = ++i; + pIdxInfo->idxNum |= 0x04; + } + pIdxInfo->estimatedCost = 1.0; /* Records are always returned in ascending order of (name, path). ** If this will satisfy the client, set the orderByConsumed flag so that @@ -229,13 +277,15 @@ static int statBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){ ) ){ pIdxInfo->orderByConsumed = 1; + pIdxInfo->idxNum |= 0x08; } + pIdxInfo->idxFlags |= SQLITE_INDEX_SCAN_HEX; return SQLITE_OK; } /* -** Open a new statvfs cursor. +** Open a new DBSTAT cursor. */ static int statOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){ StatTable *pTab = (StatTable *)pVTab; @@ -243,7 +293,7 @@ static int statOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){ pCsr = (StatCursor *)sqlite3_malloc64(sizeof(StatCursor)); if( pCsr==0 ){ - return SQLITE_NOMEM; + return SQLITE_NOMEM_BKPT; }else{ memset(pCsr, 0, sizeof(StatCursor)); pCsr->base.pVtab = pVTab; @@ -254,7 +304,7 @@ static int statOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){ return SQLITE_OK; } -static void statClearPage(StatPage *p){ +static void statClearCells(StatPage *p){ int i; if( p->aCell ){ for(i=0; inCell; i++){ @@ -262,25 +312,48 @@ static void statClearPage(StatPage *p){ } sqlite3_free(p->aCell); } - sqlite3PagerUnref(p->pPg); + p->nCell = 0; + p->aCell = 0; +} + +static void statClearPage(StatPage *p){ + u8 *aPg = p->aPg; + statClearCells(p); sqlite3_free(p->zPath); memset(p, 0, sizeof(StatPage)); + p->aPg = aPg; } static void statResetCsr(StatCursor *pCsr){ int i; - sqlite3_reset(pCsr->pStmt); + /* In some circumstances, specifically if an OOM has occurred, the call + ** to sqlite3_reset() may cause the pager to be reset (emptied). It is + ** important that statClearPage() is called to free any page refs before + ** this happens. dbsqlfuzz 9ed3e4e3816219d3509d711636c38542bf3f40b1. */ for(i=0; iaPage); i++){ statClearPage(&pCsr->aPage[i]); + sqlite3_free(pCsr->aPage[i].aPg); + pCsr->aPage[i].aPg = 0; } + sqlite3_reset(pCsr->pStmt); pCsr->iPage = 0; sqlite3_free(pCsr->zPath); pCsr->zPath = 0; pCsr->isEof = 0; } +/* Resize the space-used counters inside of the cursor */ +static void statResetCounts(StatCursor *pCsr){ + pCsr->nCell = 0; + pCsr->nMxPayload = 0; + pCsr->nUnused = 0; + pCsr->nPayload = 0; + pCsr->szPage = 0; + pCsr->nPage = 0; +} + /* -** Close a statvfs cursor. +** Close a DBSTAT cursor. */ static int statClose(sqlite3_vtab_cursor *pCursor){ StatCursor *pCsr = (StatCursor *)pCursor; @@ -290,11 +363,15 @@ static int statClose(sqlite3_vtab_cursor *pCursor){ return SQLITE_OK; } -static void getLocalPayload( +/* +** For a single cell on a btree page, compute the number of bytes of +** content (payload) stored on that page. That is to say, compute the +** number of bytes of content not found on overflow pages. +*/ +static int getLocalPayload( int nUsable, /* Usable bytes per page */ u8 flags, /* Page flags */ - int nTotal, /* Total record (payload) size */ - int *pnLocal /* OUT: Bytes stored locally */ + int nTotal /* Total record (payload) size */ ){ int nLocal; int nMinLocal; @@ -310,9 +387,12 @@ static void getLocalPayload( nLocal = nMinLocal + (nTotal - nMinLocal) % (nUsable - 4); if( nLocal>nMaxLocal ) nLocal = nMinLocal; - *pnLocal = nLocal; + return nLocal; } +/* Populate the StatPage object with information about the all +** cells found on the page currently under analysis. +*/ static int statDecodePage(Btree *pBt, StatPage *p){ int nUnused; int iOff; @@ -320,26 +400,37 @@ static int statDecodePage(Btree *pBt, StatPage *p){ int isLeaf; int szPage; - u8 *aData = sqlite3PagerGetData(p->pPg); + u8 *aData = p->aPg; u8 *aHdr = &aData[p->iPgno==1 ? 100 : 0]; p->flags = aHdr[0]; + if( p->flags==0x0A || p->flags==0x0D ){ + isLeaf = 1; + nHdr = 8; + }else if( p->flags==0x05 || p->flags==0x02 ){ + isLeaf = 0; + nHdr = 12; + }else{ + goto statPageIsCorrupt; + } + if( p->iPgno==1 ) nHdr += 100; p->nCell = get2byte(&aHdr[3]); p->nMxPayload = 0; - - isLeaf = (p->flags==0x0A || p->flags==0x0D); - nHdr = 12 - isLeaf*4 + (p->iPgno==1)*100; + szPage = sqlite3BtreeGetPageSize(pBt); nUnused = get2byte(&aHdr[5]) - nHdr - 2*p->nCell; nUnused += (int)aHdr[7]; iOff = get2byte(&aHdr[1]); while( iOff ){ + int iNext; + if( iOff>=szPage ) goto statPageIsCorrupt; nUnused += get2byte(&aData[iOff+2]); - iOff = get2byte(&aData[iOff]); + iNext = get2byte(&aData[iOff]); + if( iNext0 ) goto statPageIsCorrupt; + iOff = iNext; } p->nUnused = nUnused; p->iRightChildPg = isLeaf ? 0 : sqlite3Get4byte(&aHdr[8]); - szPage = sqlite3BtreeGetPageSize(pBt); if( p->nCell ){ int i; /* Used to iterate through cells */ @@ -349,13 +440,14 @@ static int statDecodePage(Btree *pBt, StatPage *p){ nUsable = szPage - sqlite3BtreeGetReserveNoMutex(pBt); sqlite3BtreeLeave(pBt); p->aCell = sqlite3_malloc64((p->nCell+1) * sizeof(StatCell)); - if( p->aCell==0 ) return SQLITE_NOMEM; + if( p->aCell==0 ) return SQLITE_NOMEM_BKPT; memset(p->aCell, 0, (p->nCell+1) * sizeof(StatCell)); for(i=0; inCell; i++){ StatCell *pCell = &p->aCell[i]; iOff = get2byte(&aData[nHdr+i*2]); + if( iOff=szPage ) goto statPageIsCorrupt; if( !isLeaf ){ pCell->iChildPg = sqlite3Get4byte(&aData[iOff]); iOff += 4; @@ -371,18 +463,21 @@ static int statDecodePage(Btree *pBt, StatPage *p){ iOff += sqlite3GetVarint(&aData[iOff], &dummy); } if( nPayload>(u32)p->nMxPayload ) p->nMxPayload = nPayload; - getLocalPayload(nUsable, p->flags, nPayload, &nLocal); + nLocal = getLocalPayload(nUsable, p->flags, nPayload); + if( nLocal<0 ) goto statPageIsCorrupt; pCell->nLocal = nLocal; - assert( nLocal>=0 ); assert( nPayload>=(u32)nLocal ); assert( nLocal<=(nUsable-35) ); if( nPayload>(u32)nLocal ){ int j; int nOvfl = ((nPayload - nLocal) + nUsable-4 - 1) / (nUsable - 4); + if( iOff+nLocal+4>nUsable || nPayload>0x7fffffff ){ + goto statPageIsCorrupt; + } pCell->nLastOvfl = (nPayload-nLocal) - (nOvfl-1) * (nUsable-4); pCell->nOvfl = nOvfl; pCell->aOvfl = sqlite3_malloc64(sizeof(u32)*nOvfl); - if( pCell->aOvfl==0 ) return SQLITE_NOMEM; + if( pCell->aOvfl==0 ) return SQLITE_NOMEM_BKPT; pCell->aOvfl[0] = sqlite3Get4byte(&aData[iOff+nLocal]); for(j=1; jflags = 0; + statClearCells(p); + return SQLITE_OK; } /* @@ -415,23 +515,57 @@ static void statSizeAndOffset(StatCursor *pCsr){ sqlite3_file *fd; sqlite3_int64 x[2]; - /* The default page size and offset */ - pCsr->szPage = sqlite3BtreeGetPageSize(pBt); - pCsr->iOffset = (i64)pCsr->szPage * (pCsr->iPageno - 1); - - /* If connected to a ZIPVFS backend, override the page size and - ** offset with actual values obtained from ZIPVFS. + /* If connected to a ZIPVFS backend, find the page size and + ** offset from ZIPVFS. */ fd = sqlite3PagerFile(pPager); x[0] = pCsr->iPageno; - if( fd->pMethods!=0 && sqlite3OsFileControl(fd, 230440, &x)==SQLITE_OK ){ + if( sqlite3OsFileControl(fd, 230440, &x)==SQLITE_OK ){ pCsr->iOffset = x[0]; - pCsr->szPage = (int)x[1]; + pCsr->szPage += x[1]; + }else{ + /* Not ZIPVFS: The default page size and offset */ + pCsr->szPage += sqlite3BtreeGetPageSize(pBt); + pCsr->iOffset = (i64)pCsr->szPage * (pCsr->iPageno - 1); } } /* -** Move a statvfs cursor to the next entry in the file. +** Load a copy of the page data for page iPg into the buffer belonging +** to page object pPg. Allocate the buffer if necessary. Return SQLITE_OK +** if successful, or an SQLite error code otherwise. +*/ +static int statGetPage( + Btree *pBt, /* Load page from this b-tree */ + u32 iPg, /* Page number to load */ + StatPage *pPg /* Load page into this object */ +){ + int pgsz = sqlite3BtreeGetPageSize(pBt); + DbPage *pDbPage = 0; + int rc; + + if( pPg->aPg==0 ){ + pPg->aPg = (u8*)sqlite3_malloc(pgsz + DBSTAT_PAGE_PADDING_BYTES); + if( pPg->aPg==0 ){ + return SQLITE_NOMEM_BKPT; + } + memset(&pPg->aPg[pgsz], 0, DBSTAT_PAGE_PADDING_BYTES); + } + + rc = sqlite3PagerGet(sqlite3BtreePager(pBt), iPg, &pDbPage, 0); + if( rc==SQLITE_OK ){ + const u8 *a = sqlite3PagerGetData(pDbPage); + memcpy(pPg->aPg, a, pgsz); + sqlite3PagerUnref(pDbPage); + } + + return rc; +} + +/* +** Move a DBSTAT cursor to the next entry. Normally, the next +** entry will be the next page, but in aggregated mode (pCsr->isAgg!=0), +** the next entry is the next btree. */ static int statNext(sqlite3_vtab_cursor *pCursor){ int rc; @@ -446,7 +580,9 @@ static int statNext(sqlite3_vtab_cursor *pCursor){ pCsr->zPath = 0; statNextRestart: - if( pCsr->aPage[0].pPg==0 ){ + if( pCsr->iPage<0 ){ + /* Start measuring space on the next btree */ + statResetCounts(pCsr); rc = sqlite3_step(pCsr->pStmt); if( rc==SQLITE_ROW ){ int nPage; @@ -456,47 +592,50 @@ static int statNext(sqlite3_vtab_cursor *pCursor){ pCsr->isEof = 1; return sqlite3_reset(pCsr->pStmt); } - rc = sqlite3PagerGet(pPager, iRoot, &pCsr->aPage[0].pPg, 0); + rc = statGetPage(pBt, iRoot, &pCsr->aPage[0]); pCsr->aPage[0].iPgno = iRoot; pCsr->aPage[0].iCell = 0; - pCsr->aPage[0].zPath = z = sqlite3_mprintf("/"); + if( !pCsr->isAgg ){ + pCsr->aPage[0].zPath = z = sqlite3_mprintf("/"); + if( z==0 ) rc = SQLITE_NOMEM_BKPT; + } pCsr->iPage = 0; - if( z==0 ) rc = SQLITE_NOMEM; + pCsr->nPage = 1; }else{ pCsr->isEof = 1; return sqlite3_reset(pCsr->pStmt); } }else{ - - /* Page p itself has already been visited. */ + /* Continue analyzing the btree previously started */ StatPage *p = &pCsr->aPage[pCsr->iPage]; - + if( !pCsr->isAgg ) statResetCounts(pCsr); while( p->iCellnCell ){ StatCell *pCell = &p->aCell[p->iCell]; - if( pCell->iOvflnOvfl ){ - int nUsable; + while( pCell->iOvflnOvfl ){ + int nUsable, iOvfl; sqlite3BtreeEnter(pBt); nUsable = sqlite3BtreeGetPageSize(pBt) - sqlite3BtreeGetReserveNoMutex(pBt); sqlite3BtreeLeave(pBt); - pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0); - pCsr->iPageno = pCell->aOvfl[pCell->iOvfl]; - pCsr->zPagetype = "overflow"; - pCsr->nCell = 0; - pCsr->nMxPayload = 0; - pCsr->zPath = z = sqlite3_mprintf( - "%s%.3x+%.6x", p->zPath, p->iCell, pCell->iOvfl - ); + pCsr->nPage++; + statSizeAndOffset(pCsr); if( pCell->iOvflnOvfl-1 ){ - pCsr->nUnused = 0; - pCsr->nPayload = nUsable - 4; + pCsr->nPayload += nUsable - 4; }else{ - pCsr->nPayload = pCell->nLastOvfl; - pCsr->nUnused = nUsable - 4 - pCsr->nPayload; + pCsr->nPayload += pCell->nLastOvfl; + pCsr->nUnused += nUsable - 4 - pCell->nLastOvfl; } + iOvfl = pCell->iOvfl; pCell->iOvfl++; - statSizeAndOffset(pCsr); - return z==0 ? SQLITE_NOMEM : SQLITE_OK; + if( !pCsr->isAgg ){ + pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0); + pCsr->iPageno = pCell->aOvfl[iOvfl]; + pCsr->zPagetype = "overflow"; + pCsr->zPath = z = sqlite3_mprintf( + "%s%.3x+%.6x", p->zPath, p->iCell, iOvfl + ); + return z==0 ? SQLITE_NOMEM_BKPT : SQLITE_OK; + } } if( p->iRightChildPg ) break; p->iCell++; @@ -504,11 +643,19 @@ static int statNext(sqlite3_vtab_cursor *pCursor){ if( !p->iRightChildPg || p->iCell>p->nCell ){ statClearPage(p); - if( pCsr->iPage==0 ) return statNext(pCursor); pCsr->iPage--; + if( pCsr->isAgg && pCsr->iPage<0 ){ + /* label-statNext-done: When computing aggregate space usage over + ** an entire btree, this is the exit point from this function */ + return SQLITE_OK; + } goto statNextRestart; /* Tail recursion */ } pCsr->iPage++; + if( pCsr->iPage>=ArraySize(pCsr->aPage) ){ + statResetCsr(pCsr); + return SQLITE_CORRUPT_BKPT; + } assert( p==&pCsr->aPage[pCsr->iPage-1] ); if( p->iCell==p->nCell ){ @@ -516,11 +663,14 @@ static int statNext(sqlite3_vtab_cursor *pCursor){ }else{ p[1].iPgno = p->aCell[p->iCell].iChildPg; } - rc = sqlite3PagerGet(pPager, p[1].iPgno, &p[1].pPg, 0); + rc = statGetPage(pBt, p[1].iPgno, &p[1]); + pCsr->nPage++; p[1].iCell = 0; - p[1].zPath = z = sqlite3_mprintf("%s%.3x/", p->zPath, p->iCell); + if( !pCsr->isAgg ){ + p[1].zPath = z = sqlite3_mprintf("%s%.3x/", p->zPath, p->iCell); + if( z==0 ) rc = SQLITE_NOMEM_BKPT; + } p->iCell++; - if( z==0 ) rc = SQLITE_NOMEM; } @@ -550,16 +700,23 @@ static int statNext(sqlite3_vtab_cursor *pCursor){ pCsr->zPagetype = "corrupted"; break; } - pCsr->nCell = p->nCell; - pCsr->nUnused = p->nUnused; - pCsr->nMxPayload = p->nMxPayload; - pCsr->zPath = z = sqlite3_mprintf("%s", p->zPath); - if( z==0 ) rc = SQLITE_NOMEM; + pCsr->nCell += p->nCell; + pCsr->nUnused += p->nUnused; + if( p->nMxPayload>pCsr->nMxPayload ) pCsr->nMxPayload = p->nMxPayload; + if( !pCsr->isAgg ){ + pCsr->zPath = z = sqlite3_mprintf("%s", p->zPath); + if( z==0 ) rc = SQLITE_NOMEM_BKPT; + } nPayload = 0; for(i=0; inCell; i++){ nPayload += p->aCell[i].nLocal; } - pCsr->nPayload = nPayload; + pCsr->nPayload += nPayload; + + /* If computing aggregate space usage by btree, continue with the + ** next page. The loop will exit via the return at label-statNext-done + */ + if( pCsr->isAgg ) goto statNextRestart; } } @@ -571,6 +728,10 @@ static int statEof(sqlite3_vtab_cursor *pCursor){ return pCsr->isEof; } +/* Initialize a cursor according to the query plan idxNum using the +** arguments in argv[0]. See statBestIndex() for a description of the +** meaning of the bits in idxNum. +*/ static int statFilter( sqlite3_vtab_cursor *pCursor, int idxNum, const char *idxStr, @@ -578,39 +739,63 @@ static int statFilter( ){ StatCursor *pCsr = (StatCursor *)pCursor; StatTable *pTab = (StatTable*)(pCursor->pVtab); - char *zSql; - int rc = SQLITE_OK; - char *zMaster; + sqlite3_str *pSql; /* Query of btrees to analyze */ + char *zSql; /* String value of pSql */ + int iArg = 0; /* Count of argv[] parameters used so far */ + int rc = SQLITE_OK; /* Result of this operation */ + const char *zName = 0; /* Only provide analysis of this table */ + (void)argc; + (void)idxStr; - if( idxNum==1 ){ - const char *zDbase = (const char*)sqlite3_value_text(argv[0]); + statResetCsr(pCsr); + sqlite3_finalize(pCsr->pStmt); + pCsr->pStmt = 0; + if( idxNum & 0x01 ){ + /* schema=? constraint is present. Get its value */ + const char *zDbase = (const char*)sqlite3_value_text(argv[iArg++]); pCsr->iDb = sqlite3FindDbName(pTab->db, zDbase); if( pCsr->iDb<0 ){ - sqlite3_free(pCursor->pVtab->zErrMsg); - pCursor->pVtab->zErrMsg = sqlite3_mprintf("no such schema: %s", zDbase); - return pCursor->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM; + pCsr->iDb = 0; + pCsr->isEof = 1; + return SQLITE_OK; } }else{ pCsr->iDb = pTab->iDb; } - statResetCsr(pCsr); - sqlite3_finalize(pCsr->pStmt); - pCsr->pStmt = 0; - zMaster = pCsr->iDb==1 ? "sqlite_temp_master" : "sqlite_master"; - zSql = sqlite3_mprintf( - "SELECT 'sqlite_master' AS name, 1 AS rootpage, 'table' AS type" - " UNION ALL " - "SELECT name, rootpage, type" - " FROM \"%w\".%s WHERE rootpage!=0" - " ORDER BY name", pTab->db->aDb[pCsr->iDb].zName, zMaster); + if( idxNum & 0x02 ){ + /* name=? constraint is present */ + zName = (const char*)sqlite3_value_text(argv[iArg++]); + } + if( idxNum & 0x04 ){ + /* aggregate=? constraint is present */ + pCsr->isAgg = sqlite3_value_double(argv[iArg++])!=0.0; + }else{ + pCsr->isAgg = 0; + } + pSql = sqlite3_str_new(pTab->db); + sqlite3_str_appendf(pSql, + "SELECT * FROM (" + "SELECT 'sqlite_schema' AS name,1 AS rootpage,'table' AS type" + " UNION ALL " + "SELECT name,rootpage,type" + " FROM \"%w\".sqlite_schema WHERE rootpage!=0)", + pTab->db->aDb[pCsr->iDb].zDbSName); + if( zName ){ + sqlite3_str_appendf(pSql, "WHERE name=%Q", zName); + } + if( idxNum & 0x08 ){ + sqlite3_str_appendf(pSql, " ORDER BY name"); + } + zSql = sqlite3_str_finish(pSql); if( zSql==0 ){ - return SQLITE_NOMEM; + return SQLITE_NOMEM_BKPT; }else{ rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pStmt, 0); sqlite3_free(zSql); } if( rc==SQLITE_OK ){ + pCsr->iPage = -1; rc = statNext(pCursor); } return rc; @@ -627,36 +812,50 @@ static int statColumn( sqlite3_result_text(ctx, pCsr->zName, -1, SQLITE_TRANSIENT); break; case 1: /* path */ - sqlite3_result_text(ctx, pCsr->zPath, -1, SQLITE_TRANSIENT); + if( !pCsr->isAgg ){ + sqlite3_result_text(ctx, pCsr->zPath, -1, SQLITE_TRANSIENT); + } break; case 2: /* pageno */ - sqlite3_result_int64(ctx, pCsr->iPageno); + if( pCsr->isAgg ){ + sqlite3_result_int64(ctx, pCsr->nPage); + }else{ + sqlite3_result_int64(ctx, pCsr->iPageno); + } break; case 3: /* pagetype */ - sqlite3_result_text(ctx, pCsr->zPagetype, -1, SQLITE_STATIC); + if( !pCsr->isAgg ){ + sqlite3_result_text(ctx, pCsr->zPagetype, -1, SQLITE_STATIC); + } break; case 4: /* ncell */ - sqlite3_result_int(ctx, pCsr->nCell); + sqlite3_result_int64(ctx, pCsr->nCell); break; case 5: /* payload */ - sqlite3_result_int(ctx, pCsr->nPayload); + sqlite3_result_int64(ctx, pCsr->nPayload); break; case 6: /* unused */ - sqlite3_result_int(ctx, pCsr->nUnused); + sqlite3_result_int64(ctx, pCsr->nUnused); break; case 7: /* mx_payload */ - sqlite3_result_int(ctx, pCsr->nMxPayload); + sqlite3_result_int64(ctx, pCsr->nMxPayload); break; case 8: /* pgoffset */ - sqlite3_result_int64(ctx, pCsr->iOffset); + if( !pCsr->isAgg ){ + sqlite3_result_int64(ctx, pCsr->iOffset); + } break; case 9: /* pgsize */ - sqlite3_result_int(ctx, pCsr->szPage); + sqlite3_result_int64(ctx, pCsr->szPage); break; - default: { /* schema */ + case 10: { /* schema */ sqlite3 *db = sqlite3_context_db_handle(ctx); int iDb = pCsr->iDb; - sqlite3_result_text(ctx, db->aDb[iDb].zName, -1, SQLITE_STATIC); + sqlite3_result_text(ctx, db->aDb[iDb].zDbSName, -1, SQLITE_STATIC); + break; + } + default: { /* aggregate */ + sqlite3_result_int(ctx, pCsr->isAgg); break; } } @@ -694,6 +893,11 @@ int sqlite3DbstatRegister(sqlite3 *db){ 0, /* xRollback */ 0, /* xFindMethod */ 0, /* xRename */ + 0, /* xSavepoint */ + 0, /* xRelease */ + 0, /* xRollbackTo */ + 0, /* xShadowName */ + 0 /* xIntegrity */ }; return sqlite3_create_module(db, "dbstat", &dbstat_module, 0); } diff --git a/src/delete.c b/src/delete.c index 0fe064bc45..8fac7c2f32 100644 --- a/src/delete.c +++ b/src/delete.c @@ -19,59 +19,112 @@ ** (as in the FROM clause of a SELECT statement) in this case it contains ** the name of a single table, as one might find in an INSERT, DELETE, ** or UPDATE statement. Look up that table in the symbol table and -** return a pointer. Set an error message and return NULL if the table +** return a pointer. Set an error message and return NULL if the table ** name is not found or if any other error occurs. ** ** The following fields are initialized appropriate in pSrc: ** -** pSrc->a[0].pTab Pointer to the Table object -** pSrc->a[0].pIndex Pointer to the INDEXED BY index, if there is one +** pSrc->a[0].spTab Pointer to the Table object +** pSrc->a[0].u2.pIBIndex Pointer to the INDEXED BY index, if there is one ** */ Table *sqlite3SrcListLookup(Parse *pParse, SrcList *pSrc){ - struct SrcList_item *pItem = pSrc->a; + SrcItem *pItem = pSrc->a; Table *pTab; - assert( pItem && pSrc->nSrc==1 ); + assert( pItem && pSrc->nSrc>=1 ); pTab = sqlite3LocateTableItem(pParse, 0, pItem); - sqlite3DeleteTable(pParse->db, pItem->pTab); - pItem->pTab = pTab; + if( pItem->pSTab ) sqlite3DeleteTable(pParse->db, pItem->pSTab); + pItem->pSTab = pTab; + pItem->fg.notCte = 1; if( pTab ){ - pTab->nRef++; - } - if( sqlite3IndexedByLookup(pParse, pItem) ){ - pTab = 0; + pTab->nTabRef++; + if( pItem->fg.isIndexedBy && sqlite3IndexedByLookup(pParse, pItem) ){ + pTab = 0; + } } return pTab; } -/* -** Check to make sure the given table is writable. If it is not -** writable, generate an error message and return 1. If it is -** writable return 0; +/* Generate byte-code that will report the number of rows modified +** by a DELETE, INSERT, or UPDATE statement. */ -int sqlite3IsReadOnly(Parse *pParse, Table *pTab, int viewOk){ - /* A table is not writable under the following circumstances: - ** - ** 1) It is a virtual table and no implementation of the xUpdate method - ** has been provided, or - ** 2) It is a system table (i.e. sqlite_master), this call is not - ** part of a nested parse and writable_schema pragma has not - ** been specified. - ** - ** In either case leave an error message in pParse and return non-zero. +void sqlite3CodeChangeCount(Vdbe *v, int regCounter, const char *zColName){ + sqlite3VdbeAddOp0(v, OP_FkCheck); + sqlite3VdbeAddOp2(v, OP_ResultRow, regCounter, 1); + sqlite3VdbeSetNumCols(v, 1); + sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zColName, SQLITE_STATIC); +} + +/* Return true if table pTab is read-only. +** +** A table is read-only if any of the following are true: +** +** 1) It is a virtual table and no implementation of the xUpdate method +** has been provided +** +** 2) A trigger is currently being coded and the table is a virtual table +** that is SQLITE_VTAB_DIRECTONLY or if PRAGMA trusted_schema=OFF and +** the table is not SQLITE_VTAB_INNOCUOUS. +** +** 3) It is a system table (i.e. sqlite_schema), this call is not +** part of a nested parse and writable_schema pragma has not +** been specified +** +** 4) The table is a shadow table, the database connection is in +** defensive mode, and the current sqlite3_prepare() +** is for a top-level SQL statement. +*/ +static int vtabIsReadOnly(Parse *pParse, Table *pTab){ + assert( IsVirtual(pTab) ); + if( sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0 ){ + return 1; + } + + /* Within triggers: + ** * Do not allow DELETE, INSERT, or UPDATE of SQLITE_VTAB_DIRECTONLY + ** virtual tables + ** * Only allow DELETE, INSERT, or UPDATE of non-SQLITE_VTAB_INNOCUOUS + ** virtual tables if PRAGMA trusted_schema=ON. */ - if( ( IsVirtual(pTab) - && sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0 ) - || ( (pTab->tabFlags & TF_Readonly)!=0 - && (pParse->db->flags & SQLITE_WriteSchema)==0 - && pParse->nested==0 ) + if( pParse->pToplevel!=0 + && pTab->u.vtab.p->eVtabRisk > + ((pParse->db->flags & SQLITE_TrustedSchema)!=0) ){ + sqlite3ErrorMsg(pParse, "unsafe use of virtual table \"%s\"", + pTab->zName); + } + return 0; +} +static int tabIsReadOnly(Parse *pParse, Table *pTab){ + sqlite3 *db; + if( IsVirtual(pTab) ){ + return vtabIsReadOnly(pParse, pTab); + } + if( (pTab->tabFlags & (TF_Readonly|TF_Shadow))==0 ) return 0; + db = pParse->db; + if( (pTab->tabFlags & TF_Readonly)!=0 ){ + return sqlite3WritableSchema(db)==0 && pParse->nested==0; + } + assert( pTab->tabFlags & TF_Shadow ); + return sqlite3ReadOnlyShadowTables(db); +} + +/* +** Check to make sure the given table is writable. +** +** If pTab is not writable -> generate an error message and return 1. +** If pTab is writable but other errors have occurred -> return 1. +** If pTab is writable and no prior errors -> return 0; +*/ +int sqlite3IsReadOnly(Parse *pParse, Table *pTab, Trigger *pTrigger){ + if( tabIsReadOnly(pParse, pTab) ){ sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName); return 1; } - #ifndef SQLITE_OMIT_VIEW - if( !viewOk && pTab->pSelect ){ + if( IsView(pTab) + && (pTrigger==0 || (pTrigger->bReturning && pTrigger->pNext==0)) + ){ sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName); return 1; } @@ -90,6 +143,8 @@ void sqlite3MaterializeView( Parse *pParse, /* Parsing context */ Table *pView, /* View definition */ Expr *pWhere, /* Optional WHERE clause to be added */ + ExprList *pOrderBy, /* Optional ORDER BY clause */ + Expr *pLimit, /* Optional LIMIT clause */ int iCur /* Cursor number for ephemeral table */ ){ SelectDest dest; @@ -98,16 +153,17 @@ void sqlite3MaterializeView( sqlite3 *db = pParse->db; int iDb = sqlite3SchemaToIndex(db, pView->pSchema); pWhere = sqlite3ExprDup(db, pWhere, 0); - pFrom = sqlite3SrcListAppend(db, 0, 0, 0); + pFrom = sqlite3SrcListAppend(pParse, 0, 0, 0); if( pFrom ){ assert( pFrom->nSrc==1 ); pFrom->a[0].zName = sqlite3DbStrDup(db, pView->zName); - pFrom->a[0].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zName); - assert( pFrom->a[0].pOn==0 ); - assert( pFrom->a[0].pUsing==0 ); + assert( pFrom->a[0].fg.fixedSchema==0 && pFrom->a[0].fg.isSubquery==0 ); + pFrom->a[0].u4.zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zDbSName); + assert( pFrom->a[0].fg.isUsing==0 ); + assert( pFrom->a[0].u3.pOn==0 ); } - pSel = sqlite3SelectNew(pParse, 0, pFrom, pWhere, 0, 0, 0, - SF_IncludeHidden, 0, 0); + pSel = sqlite3SelectNew(pParse, 0, pFrom, pWhere, 0, 0, pOrderBy, + SF_IncludeHidden, pLimit); sqlite3SelectDestInit(&dest, SRT_EphemTab, iCur); sqlite3Select(pParse, pSel, &dest); sqlite3SelectDelete(db, pSel); @@ -129,81 +185,95 @@ Expr *sqlite3LimitWhere( Expr *pWhere, /* The WHERE clause. May be null */ ExprList *pOrderBy, /* The ORDER BY clause. May be null */ Expr *pLimit, /* The LIMIT clause. May be null */ - Expr *pOffset, /* The OFFSET clause. May be null */ char *zStmtType /* Either DELETE or UPDATE. For err msgs. */ ){ - Expr *pWhereRowid = NULL; /* WHERE rowid .. */ + sqlite3 *db = pParse->db; + Expr *pLhs = NULL; /* LHS of IN(SELECT...) operator */ Expr *pInClause = NULL; /* WHERE rowid IN ( select ) */ - Expr *pSelectRowid = NULL; /* SELECT rowid ... */ - ExprList *pEList = NULL; /* Expression list contaning only pSelectRowid */ + ExprList *pEList = NULL; /* Expression list containing only pSelectRowid*/ SrcList *pSelectSrc = NULL; /* SELECT rowid FROM x ... (dup of pSrc) */ Select *pSelect = NULL; /* Complete SELECT tree */ + Table *pTab; /* Check that there isn't an ORDER BY without a LIMIT clause. */ - if( pOrderBy && (pLimit == 0) ) { + if( pOrderBy && pLimit==0 ) { sqlite3ErrorMsg(pParse, "ORDER BY without LIMIT on %s", zStmtType); - goto limit_where_cleanup_2; + sqlite3ExprDelete(pParse->db, pWhere); + sqlite3ExprListDelete(pParse->db, pOrderBy); + return 0; } /* We only need to generate a select expression if there ** is a limit/offset term to enforce. */ if( pLimit == 0 ) { - /* if pLimit is null, pOffset will always be null as well. */ - assert( pOffset == 0 ); return pWhere; } - /* Generate a select expression tree to enforce the limit/offset + /* Generate a select expression tree to enforce the limit/offset ** term for the DELETE or UPDATE statement. For example: ** DELETE FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1 ** becomes: - ** DELETE FROM table_a WHERE rowid IN ( + ** DELETE FROM table_a WHERE rowid IN ( ** SELECT rowid FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1 ** ); */ - pSelectRowid = sqlite3PExpr(pParse, TK_ROW, 0, 0, 0); - if( pSelectRowid == 0 ) goto limit_where_cleanup_2; - pEList = sqlite3ExprListAppend(pParse, 0, pSelectRowid); - if( pEList == 0 ) goto limit_where_cleanup_2; + pTab = pSrc->a[0].pSTab; + if( HasRowid(pTab) ){ + pLhs = sqlite3PExpr(pParse, TK_ROW, 0, 0); + pEList = sqlite3ExprListAppend( + pParse, 0, sqlite3PExpr(pParse, TK_ROW, 0, 0) + ); + }else{ + Index *pPk = sqlite3PrimaryKeyIndex(pTab); + assert( pPk!=0 ); + assert( pPk->nKeyCol>=1 ); + if( pPk->nKeyCol==1 ){ + const char *zName; + assert( pPk->aiColumn[0]>=0 && pPk->aiColumn[0]nCol ); + zName = pTab->aCol[pPk->aiColumn[0]].zCnName; + pLhs = sqlite3Expr(db, TK_ID, zName); + pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ID, zName)); + }else{ + int i; + for(i=0; inKeyCol; i++){ + Expr *p; + assert( pPk->aiColumn[i]>=0 && pPk->aiColumn[i]nCol ); + p = sqlite3Expr(db, TK_ID, pTab->aCol[pPk->aiColumn[i]].zCnName); + pEList = sqlite3ExprListAppend(pParse, pEList, p); + } + pLhs = sqlite3PExpr(pParse, TK_VECTOR, 0, 0); + if( pLhs ){ + pLhs->x.pList = sqlite3ExprListDup(db, pEList, 0); + } + } + } /* duplicate the FROM clause as it is needed by both the DELETE/UPDATE tree ** and the SELECT subtree. */ - pSelectSrc = sqlite3SrcListDup(pParse->db, pSrc, 0); - if( pSelectSrc == 0 ) { - sqlite3ExprListDelete(pParse->db, pEList); - goto limit_where_cleanup_2; + pSrc->a[0].pSTab = 0; + pSelectSrc = sqlite3SrcListDup(db, pSrc, 0); + pSrc->a[0].pSTab = pTab; + if( pSrc->a[0].fg.isIndexedBy ){ + assert( pSrc->a[0].fg.isCte==0 ); + pSrc->a[0].u2.pIBIndex = 0; + pSrc->a[0].fg.isIndexedBy = 0; + sqlite3DbFree(db, pSrc->a[0].u1.zIndexedBy); + }else if( pSrc->a[0].fg.isCte ){ + pSrc->a[0].u2.pCteUse->nUse++; } /* generate the SELECT expression tree. */ - pSelect = sqlite3SelectNew(pParse,pEList,pSelectSrc,pWhere,0,0, - pOrderBy,0,pLimit,pOffset); - if( pSelect == 0 ) return 0; - - /* now generate the new WHERE rowid IN clause for the DELETE/UDPATE */ - pWhereRowid = sqlite3PExpr(pParse, TK_ROW, 0, 0, 0); - if( pWhereRowid == 0 ) goto limit_where_cleanup_1; - pInClause = sqlite3PExpr(pParse, TK_IN, pWhereRowid, 0, 0); - if( pInClause == 0 ) goto limit_where_cleanup_1; - - pInClause->x.pSelect = pSelect; - pInClause->flags |= EP_xIsSelect; - sqlite3ExprSetHeightAndFlags(pParse, pInClause); - return pInClause; - - /* something went wrong. clean up anything allocated. */ -limit_where_cleanup_1: - sqlite3SelectDelete(pParse->db, pSelect); - return 0; + pSelect = sqlite3SelectNew(pParse, pEList, pSelectSrc, pWhere, 0 ,0, + pOrderBy,0,pLimit + ); -limit_where_cleanup_2: - sqlite3ExprDelete(pParse->db, pWhere); - sqlite3ExprListDelete(pParse->db, pOrderBy); - sqlite3ExprDelete(pParse->db, pLimit); - sqlite3ExprDelete(pParse->db, pOffset); - return 0; + /* now generate the new WHERE rowid IN clause for the DELETE/UPDATE */ + pInClause = sqlite3PExpr(pParse, TK_IN, pLhs, 0); + sqlite3PExprAddSelect(pParse, pInClause, pSelect); + return pInClause; } #endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) */ /* && !defined(SQLITE_OMIT_SUBQUERY) */ @@ -218,11 +288,12 @@ Expr *sqlite3LimitWhere( void sqlite3DeleteFrom( Parse *pParse, /* The parser context */ SrcList *pTabList, /* The table from which we should delete things */ - Expr *pWhere /* The WHERE clause. May be null */ + Expr *pWhere, /* The WHERE clause. May be null */ + ExprList *pOrderBy, /* ORDER BY clause. May be null */ + Expr *pLimit /* LIMIT clause. May be null */ ){ Vdbe *v; /* The virtual database engine */ Table *pTab; /* The table from which records will be deleted */ - const char *zDb; /* Name of database holding pTab */ int i; /* Loop counter */ WhereInfo *pWInfo; /* Information about the WHERE clause */ Index *pIdx; /* For looping over indices of the table */ @@ -234,7 +305,7 @@ void sqlite3DeleteFrom( AuthContext sContext; /* Authorization context */ NameContext sNC; /* Name context to resolve expressions in */ int iDb; /* Database number */ - int memCnt = -1; /* Memory cell used for change counting */ + int memCnt = 0; /* Memory cell used for change counting */ int rcauth; /* Value returned by authorization callback */ int eOnePass; /* ONEPASS_OFF or _SINGLE or _MULTI */ int aiCurOnePass[2]; /* The write cursors opened by WHERE_ONEPASS */ @@ -249,18 +320,21 @@ void sqlite3DeleteFrom( int addrBypass = 0; /* Address of jump over the delete logic */ int addrLoop = 0; /* Top of the delete loop */ int addrEphOpen = 0; /* Instruction to open the Ephemeral table */ - + int bComplex; /* True if there are triggers or FKs or + ** subqueries in the WHERE clause */ + #ifndef SQLITE_OMIT_TRIGGER int isView; /* True if attempting to delete from a view */ Trigger *pTrigger; /* List of table triggers, if required */ - int bComplex; /* True if there are either triggers or FKs */ #endif memset(&sContext, 0, sizeof(sContext)); db = pParse->db; - if( pParse->nErr || db->mallocFailed ){ + assert( db->pParse==pParse ); + if( pParse->nErr ){ goto delete_from_cleanup; } + assert( db->mallocFailed==0 ); assert( pTabList->nSrc==1 ); /* Locate the table which we want to delete. This table has to be @@ -276,31 +350,48 @@ void sqlite3DeleteFrom( */ #ifndef SQLITE_OMIT_TRIGGER pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0); - isView = pTab->pSelect!=0; - bComplex = pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0); + isView = IsView(pTab); #else # define pTrigger 0 # define isView 0 -# define bComplex 0 #endif + bComplex = pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0); #ifdef SQLITE_OMIT_VIEW # undef isView # define isView 0 #endif +#if TREETRACE_ENABLED + if( sqlite3TreeTrace & 0x10000 ){ + sqlite3TreeViewLine(0, "In sqlite3Delete() at %s:%d", __FILE__, __LINE__); + sqlite3TreeViewDelete(pParse->pWith, pTabList, pWhere, + pOrderBy, pLimit, pTrigger); + } +#endif + +#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT + if( !isView ){ + pWhere = sqlite3LimitWhere( + pParse, pTabList, pWhere, pOrderBy, pLimit, "DELETE" + ); + pOrderBy = 0; + pLimit = 0; + } +#endif + /* If pTab is really a view, make sure it has been initialized. */ if( sqlite3ViewGetColumnNames(pParse, pTab) ){ goto delete_from_cleanup; } - if( sqlite3IsReadOnly(pParse, pTab, (pTrigger?1:0)) ){ + if( sqlite3IsReadOnly(pParse, pTab, pTrigger) ){ goto delete_from_cleanup; } iDb = sqlite3SchemaToIndex(db, pTab->pSchema); assert( iDbnDb ); - zDb = db->aDb[iDb].zName; - rcauth = sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb); + rcauth = sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, + db->aDb[iDb].zDbSName); assert( rcauth==SQLITE_OK || rcauth==SQLITE_DENY || rcauth==SQLITE_IGNORE ); if( rcauth==SQLITE_DENY ){ goto delete_from_cleanup; @@ -328,15 +419,19 @@ void sqlite3DeleteFrom( goto delete_from_cleanup; } if( pParse->nested==0 ) sqlite3VdbeCountChanges(v); - sqlite3BeginWriteOperation(pParse, 1, iDb); + sqlite3BeginWriteOperation(pParse, bComplex, iDb); /* If we are trying to delete from a view, realize that view into ** an ephemeral table. */ #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) if( isView ){ - sqlite3MaterializeView(pParse, pTab, pWhere, iTabCur); + sqlite3MaterializeView(pParse, pTab, + pWhere, pOrderBy, pLimit, iTabCur + ); iDataCur = iIdxCur = iTabCur; + pOrderBy = 0; + pLimit = 0; } #endif @@ -352,7 +447,11 @@ void sqlite3DeleteFrom( /* Initialize the counter of the number of rows deleted, if ** we are counting rows. */ - if( db->flags & SQLITE_CountRows ){ + if( (db->flags & SQLITE_CountRows)!=0 + && !pParse->nested + && !pParse->pTriggerTab + && !pParse->bReturning + ){ memCnt = ++pParse->nMem; sqlite3VdbeAddOp2(v, OP_Integer, 0, memCnt); } @@ -360,32 +459,47 @@ void sqlite3DeleteFrom( #ifndef SQLITE_OMIT_TRUNCATE_OPTIMIZATION /* Special case: A DELETE without a WHERE clause deletes everything. ** It is easier just to erase the whole table. Prior to version 3.6.5, - ** this optimization caused the row change count (the value returned by - ** API function sqlite3_count_changes) to be set incorrectly. */ + ** this optimization caused the row change count (the value returned by + ** API function sqlite3_count_changes) to be set incorrectly. + ** + ** The "rcauth==SQLITE_OK" terms is the + ** IMPLEMENTATION-OF: R-17228-37124 If the action code is SQLITE_DELETE and + ** the callback returns SQLITE_IGNORE then the DELETE operation proceeds but + ** the truncate optimization is disabled and all rows are deleted + ** individually. + */ if( rcauth==SQLITE_OK && pWhere==0 && !bComplex && !IsVirtual(pTab) +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK + && db->xPreUpdateCallback==0 +#endif ){ assert( !isView ); sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName); if( HasRowid(pTab) ){ - sqlite3VdbeAddOp4(v, OP_Clear, pTab->tnum, iDb, memCnt, + sqlite3VdbeAddOp4(v, OP_Clear, pTab->tnum, iDb, memCnt ? memCnt : -1, pTab->zName, P4_STATIC); } for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ assert( pIdx->pSchema==pTab->pSchema ); - sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb); + if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){ + sqlite3VdbeAddOp3(v, OP_Clear, pIdx->tnum, iDb, memCnt ? memCnt : -1); + }else{ + sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb); + } } }else #endif /* SQLITE_OMIT_TRUNCATE_OPTIMIZATION */ { u16 wcf = WHERE_ONEPASS_DESIRED|WHERE_DUPLICATES_OK; + if( sNC.ncFlags & NC_Subquery ) bComplex = 1; wcf |= (bComplex ? 0 : WHERE_ONEPASS_MULTIROW); if( HasRowid(pTab) ){ /* For a rowid table, initialize the RowSet to an empty set */ pPk = 0; - nPk = 1; + assert( nPk==1 ); iRowSet = ++pParse->nMem; sqlite3VdbeAddOp2(v, OP_Null, 0, iRowSet); }else{ @@ -400,7 +514,7 @@ void sqlite3DeleteFrom( addrEphOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEphCur, nPk); sqlite3VdbeSetP4KeyInfo(pParse, pPk); } - + /* Construct a query to find the rowid or primary key for every row ** to be deleted, based on the WHERE clause. Set variable eOnePass ** to indicate the strategy used to implement this delete: @@ -409,17 +523,22 @@ void sqlite3DeleteFrom( ** ONEPASS_SINGLE: One-pass approach - at most one row deleted. ** ONEPASS_MULTI: One-pass approach - any number of rows may be deleted. */ - pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, wcf, iTabCur+1); + pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0,0,wcf,iTabCur+1); if( pWInfo==0 ) goto delete_from_cleanup; eOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass); assert( IsVirtual(pTab)==0 || eOnePass!=ONEPASS_MULTI ); - assert( IsVirtual(pTab) || bComplex || eOnePass!=ONEPASS_OFF ); - + assert( IsVirtual(pTab) || bComplex || eOnePass!=ONEPASS_OFF + || OptimizationDisabled(db, SQLITE_OnePass) ); + if( eOnePass!=ONEPASS_SINGLE ) sqlite3MultiWrite(pParse); + if( sqlite3WhereUsesDeferredSeek(pWInfo) ){ + sqlite3VdbeAddOp1(v, OP_FinishSeek, iTabCur); + } + /* Keep track of the number of rows to be deleted */ - if( db->flags & SQLITE_CountRows ){ + if( memCnt ){ sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1); } - + /* Extract the rowid or primary key for the current row */ if( pPk ){ for(i=0; inMem + 1; - iKey = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iTabCur, iKey, 0); - if( iKey>pParse->nMem ) pParse->nMem = iKey; + iKey = ++pParse->nMem; + sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, -1, iKey); } - + if( eOnePass!=ONEPASS_OFF ){ /* For ONEPASS, no need to store the rowid/primary-key. There is only ** one, so just keep it in its register(s) and fall through to the @@ -449,6 +567,7 @@ void sqlite3DeleteFrom( if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iTabCur] = 0; if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iTabCur] = 0; if( addrEphOpen ) sqlite3VdbeChangeToNoop(v, addrEphOpen); + addrBypass = sqlite3VdbeMakeLabel(pParse); }else{ if( pPk ){ /* Add the PK key for this row to the temporary table */ @@ -456,84 +575,82 @@ void sqlite3DeleteFrom( nKey = 0; /* Zero tells OP_Found to use a composite key */ sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, iKey, sqlite3IndexAffinityStr(pParse->db, pPk), nPk); - sqlite3VdbeAddOp2(v, OP_IdxInsert, iEphCur, iKey); + sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iEphCur, iKey, iPk, nPk); }else{ /* Add the rowid of the row to be deleted to the RowSet */ - nKey = 1; /* OP_Seek always uses a single rowid */ + nKey = 1; /* OP_DeferredSeek always uses a single rowid */ sqlite3VdbeAddOp2(v, OP_RowSetAdd, iRowSet, iKey); } - } - - /* If this DELETE cannot use the ONEPASS strategy, this is the - ** end of the WHERE loop */ - if( eOnePass!=ONEPASS_OFF ){ - addrBypass = sqlite3VdbeMakeLabel(v); - }else{ sqlite3WhereEnd(pWInfo); } - - /* Unless this is a view, open cursors for the table we are + + /* Unless this is a view, open cursors for the table we are ** deleting from and all its indices. If this is a view, then the - ** only effect this statement has is to fire the INSTEAD OF + ** only effect this statement has is to fire the INSTEAD OF ** triggers. */ if( !isView ){ int iAddrOnce = 0; if( eOnePass==ONEPASS_MULTI ){ - iAddrOnce = sqlite3CodeOnce(pParse); VdbeCoverage(v); + iAddrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v); } testcase( IsVirtual(pTab) ); sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, OPFLAG_FORDELETE, iTabCur, aToOpen, &iDataCur, &iIdxCur); assert( pPk || IsVirtual(pTab) || iDataCur==iTabCur ); assert( pPk || IsVirtual(pTab) || iIdxCur==iDataCur+1 ); - if( eOnePass==ONEPASS_MULTI ) sqlite3VdbeJumpHere(v, iAddrOnce); + if( eOnePass==ONEPASS_MULTI ){ + sqlite3VdbeJumpHereOrPopInst(v, iAddrOnce); + } } - + /* Set up a loop over the rowids/primary-keys that were found in the ** where-clause loop above. */ if( eOnePass!=ONEPASS_OFF ){ assert( nKey==nPk ); /* OP_Found will use an unpacked key */ if( !IsVirtual(pTab) && aToOpen[iDataCur-iTabCur] ){ - assert( pPk!=0 || pTab->pSelect!=0 ); + assert( pPk!=0 || IsView(pTab) ); sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, addrBypass, iKey, nKey); VdbeCoverage(v); } }else if( pPk ){ addrLoop = sqlite3VdbeAddOp1(v, OP_Rewind, iEphCur); VdbeCoverage(v); - sqlite3VdbeAddOp2(v, OP_RowKey, iEphCur, iKey); + if( IsVirtual(pTab) ){ + sqlite3VdbeAddOp3(v, OP_Column, iEphCur, 0, iKey); + }else{ + sqlite3VdbeAddOp2(v, OP_RowData, iEphCur, iKey); + } assert( nKey==0 ); /* OP_Found will use a composite key */ }else{ addrLoop = sqlite3VdbeAddOp3(v, OP_RowSetRead, iRowSet, 0, iKey); VdbeCoverage(v); assert( nKey==1 ); - } - + } + /* Delete the row */ #ifndef SQLITE_OMIT_VIRTUALTABLE if( IsVirtual(pTab) ){ const char *pVTab = (const char *)sqlite3GetVTable(db, pTab); sqlite3VtabMakeWritable(pParse, pTab); - sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iKey, pVTab, P4_VTAB); - sqlite3VdbeChangeP5(v, OE_Abort); assert( eOnePass==ONEPASS_OFF || eOnePass==ONEPASS_SINGLE ); sqlite3MayAbort(pParse); - if( eOnePass==ONEPASS_SINGLE && sqlite3IsToplevel(pParse) ){ - pParse->isMultiWrite = 0; + if( eOnePass==ONEPASS_SINGLE ){ + sqlite3VdbeAddOp1(v, OP_Close, iTabCur); + if( sqlite3IsToplevel(pParse) ){ + pParse->isMultiWrite = 0; + } } + sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iKey, pVTab, P4_VTAB); + sqlite3VdbeChangeP5(v, OE_Abort); }else #endif { int count = (pParse->nested==0); /* True to count changes */ - int iIdxNoSeek = -1; - if( bComplex==0 && aiCurOnePass[1]!=iDataCur ){ - iIdxNoSeek = aiCurOnePass[1]; - } sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur, - iKey, nKey, count, OE_Default, eOnePass, iIdxNoSeek); + iKey, nKey, count, OE_Default, eOnePass, aiCurOnePass[1]); } - + /* End of the loop over all rowids/primary-keys. */ if( eOnePass!=ONEPASS_OFF ){ sqlite3VdbeResolveLabel(v, addrBypass); @@ -544,15 +661,7 @@ void sqlite3DeleteFrom( }else{ sqlite3VdbeGoto(v, addrLoop); sqlite3VdbeJumpHere(v, addrLoop); - } - - /* Close the cursors open on the table and its indexes. */ - if( !isView && !IsVirtual(pTab) ){ - if( !pPk ) sqlite3VdbeAddOp1(v, OP_Close, iDataCur); - for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){ - sqlite3VdbeAddOp1(v, OP_Close, iIdxCur + i); - } - } + } } /* End non-truncate path */ /* Update the sqlite_sequence table by storing the content of the @@ -563,21 +672,23 @@ void sqlite3DeleteFrom( sqlite3AutoincrementEnd(pParse); } - /* Return the number of rows that were deleted. If this routine is + /* Return the number of rows that were deleted. If this routine is ** generating code because of a call to sqlite3NestedParse(), do not ** invoke the callback function. */ - if( (db->flags&SQLITE_CountRows) && !pParse->nested && !pParse->pTriggerTab ){ - sqlite3VdbeAddOp2(v, OP_ResultRow, memCnt, 1); - sqlite3VdbeSetNumCols(v, 1); - sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows deleted", SQLITE_STATIC); + if( memCnt ){ + sqlite3CodeChangeCount(v, memCnt, "rows deleted"); } delete_from_cleanup: sqlite3AuthContextPop(&sContext); sqlite3SrcListDelete(db, pTabList); sqlite3ExprDelete(db, pWhere); - sqlite3DbFree(db, aToOpen); +#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) + sqlite3ExprListDelete(db, pOrderBy); + sqlite3ExprDelete(db, pLimit); +#endif + if( aToOpen ) sqlite3DbNNFreeNN(db, aToOpen); return; } /* Make sure "isView" and other macros defined above are undefined. Otherwise @@ -618,16 +729,18 @@ void sqlite3DeleteFrom( ** and nPk before reading from it. ** ** If eMode is ONEPASS_MULTI, then this call is being made as part -** of a ONEPASS delete that affects multiple rows. In this case, if -** iIdxNoSeek is a valid cursor number (>=0), then its position should -** be preserved following the delete operation. Or, if iIdxNoSeek is not -** a valid cursor number, the position of iDataCur should be preserved -** instead. +** of a ONEPASS delete that affects multiple rows. In this case, if +** iIdxNoSeek is a valid cursor number (>=0) and is not the same as +** iDataCur, then its position should be preserved following the delete +** operation. Or, if iIdxNoSeek is not a valid cursor number, the +** position of iDataCur should be preserved instead. ** ** iIdxNoSeek: -** If iIdxNoSeek is a valid cursor number (>=0), then it identifies an -** index cursor (from within array of cursors starting at iIdxCur) that -** already points to the index entry to be deleted. +** If iIdxNoSeek is a valid cursor number (>=0) not equal to iDataCur, +** then it identifies an index cursor (from within array of cursors +** starting at iIdxCur) that already points to the index entry to be deleted. +** Except, this optimization is disabled if there are BEFORE triggers since +** the trigger body might have moved the cursor. */ void sqlite3GenerateRowDelete( Parse *pParse, /* Parsing context */ @@ -652,17 +765,17 @@ void sqlite3GenerateRowDelete( VdbeModuleComment((v, "BEGIN: GenRowDel(%d,%d,%d,%d)", iDataCur, iIdxCur, iPk, (int)nPk)); - /* Seek cursor iCur to the row to delete. If this row no longer exists + /* Seek cursor iCur to the row to delete. If this row no longer exists ** (this can happen if a trigger program has already deleted it), do ** not attempt to delete it or fire any DELETE triggers. */ - iLabel = sqlite3VdbeMakeLabel(v); + iLabel = sqlite3VdbeMakeLabel(pParse); opSeek = HasRowid(pTab) ? OP_NotExists : OP_NotFound; if( eMode==ONEPASS_OFF ){ sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk); VdbeCoverageIf(v, opSeek==OP_NotExists); VdbeCoverageIf(v, opSeek==OP_NotFound); } - + /* If there are any triggers to fire, allocate a range of registers to ** use for the old.* references in the triggers. */ if( sqlite3FkRequired(pParse, pTab, 0, 0) || pTrigger ){ @@ -679,54 +792,67 @@ void sqlite3GenerateRowDelete( iOld = pParse->nMem+1; pParse->nMem += (1 + pTab->nCol); - /* Populate the OLD.* pseudo-table register array. These values will be + /* Populate the OLD.* pseudo-table register array. These values will be ** used by any BEFORE and AFTER triggers that exist. */ sqlite3VdbeAddOp2(v, OP_Copy, iPk, iOld); for(iCol=0; iColnCol; iCol++){ testcase( mask!=0xffffffff && iCol==31 ); testcase( mask!=0xffffffff && iCol==32 ); if( mask==0xffffffff || (iCol<=31 && (mask & MASKBIT32(iCol))!=0) ){ - sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, iCol, iOld+iCol+1); + int kk = sqlite3TableColumnToStorage(pTab, iCol); + sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, iCol, iOld+kk+1); } } /* Invoke BEFORE DELETE trigger programs. */ addrStart = sqlite3VdbeCurrentAddr(v); - sqlite3CodeRowTrigger(pParse, pTrigger, + sqlite3CodeRowTrigger(pParse, pTrigger, TK_DELETE, 0, TRIGGER_BEFORE, pTab, iOld, onconf, iLabel ); - /* If any BEFORE triggers were coded, then seek the cursor to the + /* If any BEFORE triggers were coded, then seek the cursor to the ** row to be deleted again. It may be that the BEFORE triggers moved - ** the cursor or of already deleted the row that the cursor was + ** the cursor or already deleted the row that the cursor was ** pointing to. + ** + ** Also disable the iIdxNoSeek optimization since the BEFORE trigger + ** may have moved that cursor. */ if( addrStart=0 ); + iIdxNoSeek = -1; } /* Do FK processing. This call checks that any FK constraints that - ** refer to this table (i.e. constraints attached to other tables) + ** refer to this table (i.e. constraints attached to other tables) ** are not violated by deleting this row. */ sqlite3FkCheck(pParse, pTab, iOld, 0, 0, 0); } /* Delete the index and table entries. Skip this step if pTab is really ** a view (in which case the only effect of the DELETE statement is to - ** fire the INSTEAD OF triggers). */ - if( pTab->pSelect==0 ){ + ** fire the INSTEAD OF triggers). + ** + ** If variable 'count' is non-zero, then this OP_Delete instruction should + ** invoke the update-hook. The pre-update-hook, on the other hand should + ** be invoked unless table pTab is a system table. The difference is that + ** the update-hook is not invoked for rows removed by REPLACE, but the + ** pre-update-hook is. + */ + if( !IsView(pTab) ){ u8 p5 = 0; sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,iIdxNoSeek); sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, (count?OPFLAG_NCHANGE:0)); - if( count ){ - sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_TRANSIENT); + if( pParse->nested==0 || 0==sqlite3_stricmp(pTab->zName, "sqlite_stat1") ){ + sqlite3VdbeAppendP4(v, (char*)pTab, P4_TABLE); } if( eMode!=ONEPASS_OFF ){ sqlite3VdbeChangeP5(v, OPFLAG_AUXDELETE); } - if( iIdxNoSeek>=0 ){ + if( iIdxNoSeek>=0 && iIdxNoSeek!=iDataCur ){ sqlite3VdbeAddOp1(v, OP_Delete, iIdxNoSeek); } if( eMode==ONEPASS_MULTI ) p5 |= OPFLAG_SAVEPOSITION; @@ -735,16 +861,18 @@ void sqlite3GenerateRowDelete( /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to ** handle rows (possibly in other tables) that refer via a foreign key - ** to the row just deleted. */ + ** to the row just deleted. */ sqlite3FkActions(pParse, pTab, 0, iOld, 0, 0); /* Invoke AFTER DELETE trigger programs. */ - sqlite3CodeRowTrigger(pParse, pTrigger, - TK_DELETE, 0, TRIGGER_AFTER, pTab, iOld, onconf, iLabel - ); + if( pTrigger ){ + sqlite3CodeRowTrigger(pParse, pTrigger, + TK_DELETE, 0, TRIGGER_AFTER, pTab, iOld, onconf, iLabel + ); + } /* Jump here if the row had already been deleted before any BEFORE - ** trigger programs were invoked. Or if a trigger program throws a + ** trigger programs were invoked. Or if a trigger program throws a ** RAISE(IGNORE) exception. */ sqlite3VdbeResolveLabel(v, iLabel); VdbeModuleComment((v, "END: GenRowDel()")); @@ -796,6 +924,7 @@ void sqlite3GenerateRowIndexDelete( &iPartIdxLabel, pPrior, r1); sqlite3VdbeAddOp3(v, OP_IdxDelete, iIdxCur+i, r1, pIdx->uniqNotNull ? pIdx->nKeyCol : pIdx->nColumn); + sqlite3VdbeChangeP5(v, 1); /* Cause IdxDelete to error if no entry found */ sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel); pPrior = pIdx; } @@ -828,7 +957,7 @@ void sqlite3GenerateRowIndexDelete( ** its key into the same sequence of registers and if pPrior and pIdx share ** a column in common, then the register corresponding to that column already ** holds the correct value and the loading of that register is skipped. -** This optimization is helpful when doing a DELETE or an INTEGRITY_CHECK +** This optimization is helpful when doing a DELETE or an INTEGRITY_CHECK ** on a table with multiple indices, and especially with the ROWID or ** PRIMARY KEY columns of the index. */ @@ -849,11 +978,13 @@ int sqlite3GenerateIndexKey( if( piPartIdxLabel ){ if( pIdx->pPartIdxWhere ){ - *piPartIdxLabel = sqlite3VdbeMakeLabel(v); - pParse->iSelfTab = iDataCur; - sqlite3ExprCachePush(pParse); - sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, *piPartIdxLabel, + *piPartIdxLabel = sqlite3VdbeMakeLabel(pParse); + pParse->iSelfTab = iDataCur + 1; + sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, *piPartIdxLabel, SQLITE_JUMPIFNULL); + pParse->iSelfTab = 0; + pPrior = 0; /* Ticket a9efb42811fa41ee 2019-11-02; + ** pPartIdxWhere may have corrupted regPrior registers */ }else{ *piPartIdxLabel = 0; } @@ -870,13 +1001,15 @@ int sqlite3GenerateIndexKey( continue; } sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iDataCur, j, regBase+j); - /* If the column affinity is REAL but the number is an integer, then it - ** might be stored in the table as an integer (using a compact - ** representation) then converted to REAL by an OP_RealAffinity opcode. - ** But we are getting ready to store this value back into an index, where - ** it should be converted by to INTEGER again. So omit the OP_RealAffinity - ** opcode if it is present */ - sqlite3VdbeDeletePriorOpcode(v, OP_RealAffinity); + if( pIdx->aiColumn[j]>=0 ){ + /* If the column affinity is REAL but the number is an integer, then it + ** might be stored in the table as an integer (using a compact + ** representation) then converted to REAL by an OP_RealAffinity opcode. + ** But we are getting ready to store this value back into an index, where + ** it should be converted by to INTEGER again. So omit the + ** OP_RealAffinity opcode if it is present */ + sqlite3VdbeDeletePriorOpcode(v, OP_RealAffinity); + } } if( regOut ){ sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regOut); @@ -893,6 +1026,5 @@ int sqlite3GenerateIndexKey( void sqlite3ResolvePartIdxLabel(Parse *pParse, int iLabel){ if( iLabel ){ sqlite3VdbeResolveLabel(pParse->pVdbe, iLabel); - sqlite3ExprCachePop(pParse); } } diff --git a/src/expr.c b/src/expr.c index 8d96ba10cd..fdc05366cf 100644 --- a/src/expr.c +++ b/src/expr.c @@ -14,11 +14,23 @@ */ #include "sqliteInt.h" +/* Forward declarations */ +static void exprCodeBetween(Parse*,Expr*,int,void(*)(Parse*,Expr*,int,int),int); +static int exprCodeVector(Parse *pParse, Expr *p, int *piToFree); + +/* +** Return the affinity character for a single column of a table. +*/ +char sqlite3TableColumnAffinity(const Table *pTab, int iCol){ + if( iCol<0 || NEVER(iCol>=pTab->nCol) ) return SQLITE_AFF_INTEGER; + return pTab->aCol[iCol].affinity; +} + /* ** Return the 'affinity' of the expression pExpr if any. ** ** If pExpr is a column, a reference to a column via an 'AS' alias, -** or a sub-select with a column as the return value, then the +** or a sub-select with a column as the return value, then the ** affinity of that column is returned. Otherwise, 0x00 is returned, ** indicating no affinity for the expression. ** @@ -30,32 +42,126 @@ ** SELECT a AS b FROM t1 WHERE b; ** SELECT * FROM t1 WHERE (select a from t1); */ -char sqlite3ExprAffinity(Expr *pExpr){ +char sqlite3ExprAffinity(const Expr *pExpr){ int op; - pExpr = sqlite3ExprSkipCollate(pExpr); - if( pExpr->flags & EP_Generic ) return 0; op = pExpr->op; - if( op==TK_SELECT ){ - assert( pExpr->flags&EP_xIsSelect ); - return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr); - } + while( 1 /* exit-by-break */ ){ + if( op==TK_COLUMN || (op==TK_AGG_COLUMN && pExpr->y.pTab!=0) ){ + assert( ExprUseYTab(pExpr) ); + assert( pExpr->y.pTab!=0 ); + return sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn); + } + if( op==TK_SELECT ){ + assert( ExprUseXSelect(pExpr) ); + assert( pExpr->x.pSelect!=0 ); + assert( pExpr->x.pSelect->pEList!=0 ); + assert( pExpr->x.pSelect->pEList->a[0].pExpr!=0 ); + return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr); + } #ifndef SQLITE_OMIT_CAST - if( op==TK_CAST ){ - assert( !ExprHasProperty(pExpr, EP_IntValue) ); - return sqlite3AffinityType(pExpr->u.zToken, 0); - } + if( op==TK_CAST ){ + assert( !ExprHasProperty(pExpr, EP_IntValue) ); + return sqlite3AffinityType(pExpr->u.zToken, 0); + } #endif - if( (op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_REGISTER) - && pExpr->pTab!=0 - ){ - /* op==TK_REGISTER && pExpr->pTab!=0 happens when pExpr was originally - ** a TK_COLUMN but was previously evaluated and cached in a register */ - int j = pExpr->iColumn; - if( j<0 ) return SQLITE_AFF_INTEGER; - assert( pExpr->pTab && jpTab->nCol ); - return pExpr->pTab->aCol[j].affinity; + if( op==TK_SELECT_COLUMN ){ + assert( pExpr->pLeft!=0 && ExprUseXSelect(pExpr->pLeft) ); + assert( pExpr->iColumn < pExpr->iTable ); + assert( pExpr->iColumn >= 0 ); + assert( pExpr->iTable==pExpr->pLeft->x.pSelect->pEList->nExpr ); + return sqlite3ExprAffinity( + pExpr->pLeft->x.pSelect->pEList->a[pExpr->iColumn].pExpr + ); + } + if( op==TK_VECTOR + || (op==TK_FUNCTION && pExpr->affExpr==SQLITE_AFF_DEFER) + ){ + assert( ExprUseXList(pExpr) ); + return sqlite3ExprAffinity(pExpr->x.pList->a[0].pExpr); + } + if( ExprHasProperty(pExpr, EP_Skip|EP_IfNullRow) ){ + assert( pExpr->op==TK_COLLATE + || pExpr->op==TK_IF_NULL_ROW + || (pExpr->op==TK_REGISTER && pExpr->op2==TK_IF_NULL_ROW) ); + pExpr = pExpr->pLeft; + op = pExpr->op; + continue; + } + if( op!=TK_REGISTER ) break; + op = pExpr->op2; + if( NEVER( op==TK_REGISTER ) ) break; } - return pExpr->affinity; + return pExpr->affExpr; +} + +/* +** Make a guess at all the possible datatypes of the result that could +** be returned by an expression. Return a bitmask indicating the answer: +** +** 0x01 Numeric +** 0x02 Text +** 0x04 Blob +** +** If the expression must return NULL, then 0x00 is returned. +*/ +int sqlite3ExprDataType(const Expr *pExpr){ + while( pExpr ){ + switch( pExpr->op ){ + case TK_COLLATE: + case TK_IF_NULL_ROW: + case TK_UPLUS: { + pExpr = pExpr->pLeft; + break; + } + case TK_NULL: { + pExpr = 0; + break; + } + case TK_STRING: { + return 0x02; + } + case TK_BLOB: { + return 0x04; + } + case TK_CONCAT: { + return 0x06; + } + case TK_VARIABLE: + case TK_AGG_FUNCTION: + case TK_FUNCTION: { + return 0x07; + } + case TK_COLUMN: + case TK_AGG_COLUMN: + case TK_SELECT: + case TK_CAST: + case TK_SELECT_COLUMN: + case TK_VECTOR: { + int aff = sqlite3ExprAffinity(pExpr); + if( aff>=SQLITE_AFF_NUMERIC ) return 0x05; + if( aff==SQLITE_AFF_TEXT ) return 0x06; + return 0x07; + } + case TK_CASE: { + int res = 0; + int ii; + ExprList *pList = pExpr->x.pList; + assert( ExprUseXList(pExpr) && pList!=0 ); + assert( pList->nExpr > 0); + for(ii=1; iinExpr; ii+=2){ + res |= sqlite3ExprDataType(pList->a[ii].pExpr); + } + if( pList->nExpr % 2 ){ + res |= sqlite3ExprDataType(pList->a[pList->nExpr-1].pExpr); + } + return res; + } + default: { + return 0x01; + } + } /* End of switch(op) */ + } /* End of while(pExpr) */ + return 0x00; } /* @@ -67,7 +173,7 @@ char sqlite3ExprAffinity(Expr *pExpr){ ** and the pExpr parameter is returned unchanged. */ Expr *sqlite3ExprAddCollateToken( - Parse *pParse, /* Parsing context */ + const Parse *pParse, /* Parsing context */ Expr *pExpr, /* Add the "COLLATE" clause to this expression */ const Token *pCollName, /* Name of collating sequence */ int dequote /* True to dequote pCollName */ @@ -82,7 +188,11 @@ Expr *sqlite3ExprAddCollateToken( } return pExpr; } -Expr *sqlite3ExprAddCollateString(Parse *pParse, Expr *pExpr, const char *zC){ +Expr *sqlite3ExprAddCollateString( + const Parse *pParse, /* Parsing context */ + Expr *pExpr, /* Add the "COLLATE" clause to this expression */ + const char *zC /* The collating sequence name */ +){ Token s; assert( zC!=0 ); sqlite3TokenInit(&s, (char*)zC); @@ -90,21 +200,34 @@ Expr *sqlite3ExprAddCollateString(Parse *pParse, Expr *pExpr, const char *zC){ } /* -** Skip over any TK_COLLATE operators and any unlikely() -** or likelihood() function at the root of an expression. +** Skip over any TK_COLLATE operators. */ Expr *sqlite3ExprSkipCollate(Expr *pExpr){ while( pExpr && ExprHasProperty(pExpr, EP_Skip) ){ + assert( pExpr->op==TK_COLLATE ); + pExpr = pExpr->pLeft; + } + return pExpr; +} + +/* +** Skip over any TK_COLLATE operators and/or any unlikely() +** or likelihood() or likely() functions at the root of an +** expression. +*/ +Expr *sqlite3ExprSkipCollateAndLikely(Expr *pExpr){ + while( pExpr && ExprHasProperty(pExpr, EP_Skip|EP_Unlikely) ){ if( ExprHasProperty(pExpr, EP_Unlikely) ){ - assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); + assert( ExprUseXList(pExpr) ); assert( pExpr->x.pList->nExpr>0 ); assert( pExpr->op==TK_FUNCTION ); pExpr = pExpr->x.pList->a[0].pExpr; - }else{ - assert( pExpr->op==TK_COLLATE ); + }else if( pExpr->op==TK_COLLATE ){ pExpr = pExpr->pLeft; + }else{ + break; } - } + } return pExpr; } @@ -112,37 +235,49 @@ Expr *sqlite3ExprSkipCollate(Expr *pExpr){ ** Return the collation sequence for the expression pExpr. If ** there is no defined collating sequence, return NULL. ** +** See also: sqlite3ExprNNCollSeq() +** +** The sqlite3ExprNNCollSeq() works the same exact that it returns the +** default collation if pExpr has no defined collation. +** ** The collating sequence might be determined by a COLLATE operator ** or by the presence of a column with a defined collating sequence. ** COLLATE operators take first precedence. Left operands take ** precedence over right operands. */ -CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){ +CollSeq *sqlite3ExprCollSeq(Parse *pParse, const Expr *pExpr){ sqlite3 *db = pParse->db; CollSeq *pColl = 0; - Expr *p = pExpr; + const Expr *p = pExpr; while( p ){ int op = p->op; - if( p->flags & EP_Generic ) break; + if( op==TK_REGISTER ) op = p->op2; + if( (op==TK_AGG_COLUMN && p->y.pTab!=0) + || op==TK_COLUMN || op==TK_TRIGGER + ){ + int j; + assert( ExprUseYTab(p) ); + assert( p->y.pTab!=0 ); + if( (j = p->iColumn)>=0 ){ + const char *zColl = sqlite3ColumnColl(&p->y.pTab->aCol[j]); + pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0); + } + break; + } if( op==TK_CAST || op==TK_UPLUS ){ p = p->pLeft; continue; } - if( op==TK_COLLATE || (op==TK_REGISTER && p->op2==TK_COLLATE) ){ - pColl = sqlite3GetCollSeq(pParse, ENC(db), 0, p->u.zToken); - break; - } - if( (op==TK_AGG_COLUMN || op==TK_COLUMN - || op==TK_REGISTER || op==TK_TRIGGER) - && p->pTab!=0 + if( op==TK_VECTOR + || (op==TK_FUNCTION && p->affExpr==SQLITE_AFF_DEFER) ){ - /* op==TK_REGISTER && p->pTab!=0 happens when pExpr was originally - ** a TK_COLUMN but was previously evaluated and cached in a register */ - int j = p->iColumn; - if( j>=0 ){ - const char *zColl = p->pTab->aCol[j].zColl; - pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0); - } + assert( ExprUseXList(p) ); + p = p->x.pList->a[0].pExpr; + continue; + } + if( op==TK_COLLATE ){ + assert( !ExprHasProperty(p, EP_IntValue) ); + pColl = sqlite3GetCollSeq(pParse, ENC(db), 0, p->u.zToken); break; } if( p->flags & EP_Collate ){ @@ -151,13 +286,10 @@ CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){ }else{ Expr *pNext = p->pRight; /* The Expr.x union is never used at the same time as Expr.pRight */ - assert( p->x.pList==0 || p->pRight==0 ); - /* p->flags holds EP_Collate and p->pLeft->flags does not. And - ** p->x.pSelect cannot. So if p->x.pLeft exists, it must hold at - ** least one EP_Collate. Thus the following two ALWAYS. */ - if( p->x.pList!=0 && ALWAYS(!ExprHasProperty(p, EP_xIsSelect)) ){ + assert( !ExprUseXList(p) || p->x.pList==0 || p->pRight==0 ); + if( ExprUseXList(p) && p->x.pList!=0 && !db->mallocFailed ){ int i; - for(i=0; ALWAYS(ix.pList->nExpr); i++){ + for(i=0; ix.pList->nExpr; i++){ if( ExprHasProperty(p->x.pList->a[i].pExpr, EP_Collate) ){ pNext = p->x.pList->a[i].pExpr; break; @@ -170,20 +302,46 @@ CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){ break; } } - if( sqlite3CheckCollSeq(pParse, pColl) ){ + if( sqlite3CheckCollSeq(pParse, pColl) ){ pColl = 0; } return pColl; } +/* +** Return the collation sequence for the expression pExpr. If +** there is no defined collating sequence, return a pointer to the +** default collation sequence. +** +** See also: sqlite3ExprCollSeq() +** +** The sqlite3ExprCollSeq() routine works the same except that it +** returns NULL if there is no defined collation. +*/ +CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, const Expr *pExpr){ + CollSeq *p = sqlite3ExprCollSeq(pParse, pExpr); + if( p==0 ) p = pParse->db->pDfltColl; + assert( p!=0 ); + return p; +} + +/* +** Return TRUE if the two expressions have equivalent collating sequences. +*/ +int sqlite3ExprCollSeqMatch(Parse *pParse, const Expr *pE1, const Expr *pE2){ + CollSeq *pColl1 = sqlite3ExprNNCollSeq(pParse, pE1); + CollSeq *pColl2 = sqlite3ExprNNCollSeq(pParse, pE2); + return sqlite3StrICmp(pColl1->zName, pColl2->zName)==0; +} + /* ** pExpr is an operand of a comparison operator. aff2 is the ** type affinity of the other operand. This routine returns the ** type affinity that should be used for the comparison operator. */ -char sqlite3CompareAffinity(Expr *pExpr, char aff2){ +char sqlite3CompareAffinity(const Expr *pExpr, char aff2){ char aff1 = sqlite3ExprAffinity(pExpr); - if( aff1 && aff2 ){ + if( aff1>SQLITE_AFF_NONE && aff2>SQLITE_AFF_NONE ){ /* Both sides of the comparison are columns. If one has numeric ** affinity, use that. Otherwise use no affinity. */ @@ -192,15 +350,10 @@ char sqlite3CompareAffinity(Expr *pExpr, char aff2){ }else{ return SQLITE_AFF_BLOB; } - }else if( !aff1 && !aff2 ){ - /* Neither side of the comparison is a column. Compare the - ** results directly. - */ - return SQLITE_AFF_BLOB; }else{ /* One side is a column, the other is not. Use the columns affinity. */ - assert( aff1==0 || aff2==0 ); - return (aff1 + aff2); + assert( aff1<=SQLITE_AFF_NONE || aff2<=SQLITE_AFF_NONE ); + return (aff1<=SQLITE_AFF_NONE ? aff2 : aff1) | SQLITE_AFF_NONE; } } @@ -208,7 +361,7 @@ char sqlite3CompareAffinity(Expr *pExpr, char aff2){ ** pExpr is a comparison operator. Return the type affinity that should ** be applied to both operands prior to doing the comparison. */ -static char comparisonAffinity(Expr *pExpr){ +static char comparisonAffinity(const Expr *pExpr){ char aff; assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT || pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE || @@ -217,9 +370,9 @@ static char comparisonAffinity(Expr *pExpr){ aff = sqlite3ExprAffinity(pExpr->pLeft); if( pExpr->pRight ){ aff = sqlite3CompareAffinity(pExpr->pRight, aff); - }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){ + }else if( ExprUseXSelect(pExpr) ){ aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff); - }else if( !aff ){ + }else if( aff==0 ){ aff = SQLITE_AFF_BLOB; } return aff; @@ -231,23 +384,26 @@ static char comparisonAffinity(Expr *pExpr){ ** if the index with affinity idx_affinity may be used to implement ** the comparison in pExpr. */ -int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){ +int sqlite3IndexAffinityOk(const Expr *pExpr, char idx_affinity){ char aff = comparisonAffinity(pExpr); - switch( aff ){ - case SQLITE_AFF_BLOB: - return 1; - case SQLITE_AFF_TEXT: - return idx_affinity==SQLITE_AFF_TEXT; - default: - return sqlite3IsNumericAffinity(idx_affinity); + if( affpRight, p->pLeft); + }else{ + return sqlite3BinaryCompareCollSeq(pParse, p->pLeft, p->pRight); + } +} + /* ** Generate code for a comparison operator. */ @@ -295,20 +467,322 @@ static int codeCompare( int opcode, /* The comparison opcode */ int in1, int in2, /* Register holding operands */ int dest, /* Jump here if true. */ - int jumpIfNull /* If true, jump if either operand is NULL */ + int jumpIfNull, /* If true, jump if either operand is NULL */ + int isCommuted /* The comparison has been commuted */ ){ int p5; int addr; CollSeq *p4; - p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight); + if( pParse->nErr ) return 0; + if( isCommuted ){ + p4 = sqlite3BinaryCompareCollSeq(pParse, pRight, pLeft); + }else{ + p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight); + } p5 = binaryCompareP5(pLeft, pRight, jumpIfNull); addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1, (void*)p4, P4_COLLSEQ); - sqlite3VdbeChangeP5(pParse->pVdbe, (u8)p5); + sqlite3VdbeChangeP5(pParse->pVdbe, (u16)p5); return addr; } +/* +** Return true if expression pExpr is a vector, or false otherwise. +** +** A vector is defined as any expression that results in two or more +** columns of result. Every TK_VECTOR node is an vector because the +** parser will not generate a TK_VECTOR with fewer than two entries. +** But a TK_SELECT might be either a vector or a scalar. It is only +** considered a vector if it has two or more result columns. +*/ +int sqlite3ExprIsVector(const Expr *pExpr){ + return sqlite3ExprVectorSize(pExpr)>1; +} + +/* +** If the expression passed as the only argument is of type TK_VECTOR +** return the number of expressions in the vector. Or, if the expression +** is a sub-select, return the number of columns in the sub-select. For +** any other type of expression, return 1. +*/ +int sqlite3ExprVectorSize(const Expr *pExpr){ + u8 op = pExpr->op; + if( op==TK_REGISTER ) op = pExpr->op2; + if( op==TK_VECTOR ){ + assert( ExprUseXList(pExpr) ); + return pExpr->x.pList->nExpr; + }else if( op==TK_SELECT ){ + assert( ExprUseXSelect(pExpr) ); + return pExpr->x.pSelect->pEList->nExpr; + }else{ + return 1; + } +} + +/* +** Return a pointer to a subexpression of pVector that is the i-th +** column of the vector (numbered starting with 0). The caller must +** ensure that i is within range. +** +** If pVector is really a scalar (and "scalar" here includes subqueries +** that return a single column!) then return pVector unmodified. +** +** pVector retains ownership of the returned subexpression. +** +** If the vector is a (SELECT ...) then the expression returned is +** just the expression for the i-th term of the result set, and may +** not be ready for evaluation because the table cursor has not yet +** been positioned. +*/ +Expr *sqlite3VectorFieldSubexpr(Expr *pVector, int i){ + assert( iop==TK_ERROR ); + if( sqlite3ExprIsVector(pVector) ){ + assert( pVector->op2==0 || pVector->op==TK_REGISTER ); + if( pVector->op==TK_SELECT || pVector->op2==TK_SELECT ){ + assert( ExprUseXSelect(pVector) ); + return pVector->x.pSelect->pEList->a[i].pExpr; + }else{ + assert( ExprUseXList(pVector) ); + return pVector->x.pList->a[i].pExpr; + } + } + return pVector; +} + +/* +** Compute and return a new Expr object which when passed to +** sqlite3ExprCode() will generate all necessary code to compute +** the iField-th column of the vector expression pVector. +** +** It is ok for pVector to be a scalar (as long as iField==0). +** In that case, this routine works like sqlite3ExprDup(). +** +** The caller owns the returned Expr object and is responsible for +** ensuring that the returned value eventually gets freed. +** +** The caller retains ownership of pVector. If pVector is a TK_SELECT, +** then the returned object will reference pVector and so pVector must remain +** valid for the life of the returned object. If pVector is a TK_VECTOR +** or a scalar expression, then it can be deleted as soon as this routine +** returns. +** +** A trick to cause a TK_SELECT pVector to be deleted together with +** the returned Expr object is to attach the pVector to the pRight field +** of the returned TK_SELECT_COLUMN Expr object. +*/ +Expr *sqlite3ExprForVectorField( + Parse *pParse, /* Parsing context */ + Expr *pVector, /* The vector. List of expressions or a sub-SELECT */ + int iField, /* Which column of the vector to return */ + int nField /* Total number of columns in the vector */ +){ + Expr *pRet; + if( pVector->op==TK_SELECT ){ + assert( ExprUseXSelect(pVector) ); + /* The TK_SELECT_COLUMN Expr node: + ** + ** pLeft: pVector containing TK_SELECT. Not deleted. + ** pRight: not used. But recursively deleted. + ** iColumn: Index of a column in pVector + ** iTable: 0 or the number of columns on the LHS of an assignment + ** pLeft->iTable: First in an array of register holding result, or 0 + ** if the result is not yet computed. + ** + ** sqlite3ExprDelete() specifically skips the recursive delete of + ** pLeft on TK_SELECT_COLUMN nodes. But pRight is followed, so pVector + ** can be attached to pRight to cause this node to take ownership of + ** pVector. Typically there will be multiple TK_SELECT_COLUMN nodes + ** with the same pLeft pointer to the pVector, but only one of them + ** will own the pVector. + */ + pRet = sqlite3PExpr(pParse, TK_SELECT_COLUMN, 0, 0); + if( pRet ){ + ExprSetProperty(pRet, EP_FullSize); + pRet->iTable = nField; + pRet->iColumn = iField; + pRet->pLeft = pVector; + } + }else{ + if( pVector->op==TK_VECTOR ){ + Expr **ppVector; + assert( ExprUseXList(pVector) ); + ppVector = &pVector->x.pList->a[iField].pExpr; + pVector = *ppVector; + if( IN_RENAME_OBJECT ){ + /* This must be a vector UPDATE inside a trigger */ + *ppVector = 0; + return pVector; + } + } + pRet = sqlite3ExprDup(pParse->db, pVector, 0); + } + return pRet; +} + +/* +** If expression pExpr is of type TK_SELECT, generate code to evaluate +** it. Return the register in which the result is stored (or, if the +** sub-select returns more than one column, the first in an array +** of registers in which the result is stored). +** +** If pExpr is not a TK_SELECT expression, return 0. +*/ +static int exprCodeSubselect(Parse *pParse, Expr *pExpr){ + int reg = 0; +#ifndef SQLITE_OMIT_SUBQUERY + if( pExpr->op==TK_SELECT ){ + reg = sqlite3CodeSubselect(pParse, pExpr); + } +#endif + return reg; +} + +/* +** Argument pVector points to a vector expression - either a TK_VECTOR +** or TK_SELECT that returns more than one column. This function returns +** the register number of a register that contains the value of +** element iField of the vector. +** +** If pVector is a TK_SELECT expression, then code for it must have +** already been generated using the exprCodeSubselect() routine. In this +** case parameter regSelect should be the first in an array of registers +** containing the results of the sub-select. +** +** If pVector is of type TK_VECTOR, then code for the requested field +** is generated. In this case (*pRegFree) may be set to the number of +** a temporary register to be freed by the caller before returning. +** +** Before returning, output parameter (*ppExpr) is set to point to the +** Expr object corresponding to element iElem of the vector. +*/ +static int exprVectorRegister( + Parse *pParse, /* Parse context */ + Expr *pVector, /* Vector to extract element from */ + int iField, /* Field to extract from pVector */ + int regSelect, /* First in array of registers */ + Expr **ppExpr, /* OUT: Expression element */ + int *pRegFree /* OUT: Temp register to free */ +){ + u8 op = pVector->op; + assert( op==TK_VECTOR || op==TK_REGISTER || op==TK_SELECT || op==TK_ERROR ); + if( op==TK_REGISTER ){ + *ppExpr = sqlite3VectorFieldSubexpr(pVector, iField); + return pVector->iTable+iField; + } + if( op==TK_SELECT ){ + assert( ExprUseXSelect(pVector) ); + *ppExpr = pVector->x.pSelect->pEList->a[iField].pExpr; + return regSelect+iField; + } + if( op==TK_VECTOR ){ + assert( ExprUseXList(pVector) ); + *ppExpr = pVector->x.pList->a[iField].pExpr; + return sqlite3ExprCodeTemp(pParse, *ppExpr, pRegFree); + } + return 0; +} + +/* +** Expression pExpr is a comparison between two vector values. Compute +** the result of the comparison (1, 0, or NULL) and write that +** result into register dest. +** +** The caller must satisfy the following preconditions: +** +** if pExpr->op==TK_IS: op==TK_EQ and p5==SQLITE_NULLEQ +** if pExpr->op==TK_ISNOT: op==TK_NE and p5==SQLITE_NULLEQ +** otherwise: op==pExpr->op and p5==0 +*/ +static void codeVectorCompare( + Parse *pParse, /* Code generator context */ + Expr *pExpr, /* The comparison operation */ + int dest, /* Write results into this register */ + u8 op, /* Comparison operator */ + u8 p5 /* SQLITE_NULLEQ or zero */ +){ + Vdbe *v = pParse->pVdbe; + Expr *pLeft = pExpr->pLeft; + Expr *pRight = pExpr->pRight; + int nLeft = sqlite3ExprVectorSize(pLeft); + int i; + int regLeft = 0; + int regRight = 0; + u8 opx = op; + int addrCmp = 0; + int addrDone = sqlite3VdbeMakeLabel(pParse); + int isCommuted = ExprHasProperty(pExpr,EP_Commuted); + + assert( !ExprHasVVAProperty(pExpr,EP_Immutable) ); + if( pParse->nErr ) return; + if( nLeft!=sqlite3ExprVectorSize(pRight) ){ + sqlite3ErrorMsg(pParse, "row value misused"); + return; + } + assert( pExpr->op==TK_EQ || pExpr->op==TK_NE + || pExpr->op==TK_IS || pExpr->op==TK_ISNOT + || pExpr->op==TK_LT || pExpr->op==TK_GT + || pExpr->op==TK_LE || pExpr->op==TK_GE + ); + assert( pExpr->op==op || (pExpr->op==TK_IS && op==TK_EQ) + || (pExpr->op==TK_ISNOT && op==TK_NE) ); + assert( p5==0 || pExpr->op!=op ); + assert( p5==SQLITE_NULLEQ || pExpr->op==op ); + + if( op==TK_LE ) opx = TK_LT; + if( op==TK_GE ) opx = TK_GT; + if( op==TK_NE ) opx = TK_EQ; + + regLeft = exprCodeSubselect(pParse, pLeft); + regRight = exprCodeSubselect(pParse, pRight); + + sqlite3VdbeAddOp2(v, OP_Integer, 1, dest); + for(i=0; 1 /*Loop exits by "break"*/; i++){ + int regFree1 = 0, regFree2 = 0; + Expr *pL = 0, *pR = 0; + int r1, r2; + assert( i>=0 && i0 /* ** Check that argument nHeight is less than or equal to the maximum @@ -319,7 +793,7 @@ int sqlite3ExprCheckHeight(Parse *pParse, int nHeight){ int rc = SQLITE_OK; int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH]; if( nHeight>mxHeight ){ - sqlite3ErrorMsg(pParse, + sqlite3ErrorMsg(pParse, "Expression tree is too large (maximum depth %d)", mxHeight ); rc = SQLITE_ERROR; @@ -336,14 +810,14 @@ int sqlite3ExprCheckHeight(Parse *pParse, int nHeight){ ** to by pnHeight, the second parameter, then set *pnHeight to that ** value. */ -static void heightOfExpr(Expr *p, int *pnHeight){ +static void heightOfExpr(const Expr *p, int *pnHeight){ if( p ){ if( p->nHeight>*pnHeight ){ *pnHeight = p->nHeight; } } } -static void heightOfExprList(ExprList *p, int *pnHeight){ +static void heightOfExprList(const ExprList *p, int *pnHeight){ if( p ){ int i; for(i=0; inExpr; i++){ @@ -351,34 +825,34 @@ static void heightOfExprList(ExprList *p, int *pnHeight){ } } } -static void heightOfSelect(Select *p, int *pnHeight){ - if( p ){ +static void heightOfSelect(const Select *pSelect, int *pnHeight){ + const Select *p; + for(p=pSelect; p; p=p->pPrior){ heightOfExpr(p->pWhere, pnHeight); heightOfExpr(p->pHaving, pnHeight); heightOfExpr(p->pLimit, pnHeight); - heightOfExpr(p->pOffset, pnHeight); heightOfExprList(p->pEList, pnHeight); heightOfExprList(p->pGroupBy, pnHeight); heightOfExprList(p->pOrderBy, pnHeight); - heightOfSelect(p->pPrior, pnHeight); } } /* -** Set the Expr.nHeight variable in the structure passed as an -** argument. An expression with no children, Expr.pList or +** Set the Expr.nHeight variable in the structure passed as an +** argument. An expression with no children, Expr.pList or ** Expr.pSelect member has a height of 1. Any other expression -** has a height equal to the maximum height of any other +** has a height equal to the maximum height of any other ** referenced Expr plus one. ** ** Also propagate EP_Propagate flags up from Expr.x.pList to Expr.flags, ** if appropriate. */ static void exprSetHeight(Expr *p){ - int nHeight = 0; - heightOfExpr(p->pLeft, &nHeight); - heightOfExpr(p->pRight, &nHeight); - if( ExprHasProperty(p, EP_xIsSelect) ){ + int nHeight = p->pLeft ? p->pLeft->nHeight : 0; + if( NEVER(p->pRight) && p->pRight->nHeight>nHeight ){ + nHeight = p->pRight->nHeight; + } + if( ExprUseXSelect(p) ){ heightOfSelect(p->x.pSelect, &nHeight); }else if( p->x.pList ){ heightOfExprList(p->x.pList, &nHeight); @@ -393,7 +867,7 @@ static void exprSetHeight(Expr *p){ ** leave an error in pParse. ** ** Also propagate all EP_Propagate flags from the Expr.x.pList into -** Expr.flags. +** Expr.flags. */ void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){ if( pParse->nErr ) return; @@ -405,7 +879,7 @@ void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){ ** Return the maximum height of any expression tree referenced ** by the select statement passed as an argument. */ -int sqlite3SelectExprHeight(Select *p){ +int sqlite3SelectExprHeight(const Select *p){ int nHeight = 0; heightOfSelect(p, &nHeight); return nHeight; @@ -413,16 +887,26 @@ int sqlite3SelectExprHeight(Select *p){ #else /* ABOVE: Height enforcement enabled. BELOW: Height enforcement off */ /* ** Propagate all EP_Propagate flags from the Expr.x.pList into -** Expr.flags. +** Expr.flags. */ void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){ - if( p && p->x.pList && !ExprHasProperty(p, EP_xIsSelect) ){ + if( pParse->nErr ) return; + if( p && ExprUseXList(p) && p->x.pList ){ p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList); } } #define exprSetHeight(y) #endif /* SQLITE_MAX_EXPR_DEPTH>0 */ +/* +** Set the error offset for an Expr node, if possible. +*/ +void sqlite3ExprSetErrorOffset(Expr *pExpr, int iOfst){ + if( pExpr==0 ) return; + if( NEVER(ExprUseWJoin(pExpr)) ) return; + pExpr->w.iOfst = iOfst; +} + /* ** This routine is the core allocator for Expr nodes. ** @@ -437,14 +921,15 @@ void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){ ** appear to be quoted. If the quotes were of the form "..." (double-quotes) ** then the EP_DblQuoted flag is set on the expression node. ** -** Special case: If op==TK_INTEGER and pToken points to a string that -** can be translated into a 32-bit integer, then the token is not -** stored in u.zToken. Instead, the integer values is written -** into u.iValue and the EP_IntValue flag is set. No extra storage +** Special case (tag-20240227-a): If op==TK_INTEGER and pToken points to +** a string that can be translated into a 32-bit integer, then the token is +** not stored in u.zToken. Instead, the integer values is written +** into u.iValue and the EP_IntValue flag is set. No extra storage ** is allocated to hold the integer text and the dequote flag is ignored. +** See also tag-20240227-b. */ Expr *sqlite3ExprAlloc( - sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */ + sqlite3 *db, /* Handle for sqlite3DbMallocRawNN() */ int op, /* Expression opcode */ const Token *pToken, /* Token argument. Might be NULL */ int dequote /* True to dequote */ @@ -457,7 +942,7 @@ Expr *sqlite3ExprAlloc( if( pToken ){ if( op!=TK_INTEGER || pToken->z==0 || sqlite3GetInt32(pToken->z, &iValue)==0 ){ - nExtra = pToken->n+1; + nExtra = pToken->n+1; /* tag-20240227-a */ assert( iValue>=0 ); } } @@ -468,24 +953,21 @@ Expr *sqlite3ExprAlloc( pNew->iAgg = -1; if( pToken ){ if( nExtra==0 ){ - pNew->flags |= EP_IntValue; + pNew->flags |= EP_IntValue|EP_Leaf|(iValue?EP_IsTrue:EP_IsFalse); pNew->u.iValue = iValue; }else{ - int c; pNew->u.zToken = (char*)&pNew[1]; assert( pToken->z!=0 || pToken->n==0 ); if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n); pNew->u.zToken[pToken->n] = 0; - if( dequote && nExtra>=3 - && ((c = pToken->z[0])=='\'' || c=='"' || c=='[' || c=='`') ){ - sqlite3Dequote(pNew->u.zToken); - if( c=='"' ) pNew->flags |= EP_DblQuoted; + if( dequote && sqlite3Isquote(pNew->u.zToken[0]) ){ + sqlite3DequoteExpr(pNew); } } } #if SQLITE_MAX_EXPR_DEPTH>0 pNew->nHeight = 1; -#endif +#endif } return pNew; } @@ -501,7 +983,7 @@ Expr *sqlite3Expr( ){ Token x; x.z = zToken; - x.n = zToken ? sqlite3Strlen30(zToken) : 0; + x.n = sqlite3Strlen30(zToken); return sqlite3ExprAlloc(db, op, &x, 0); } @@ -522,15 +1004,26 @@ void sqlite3ExprAttachSubtrees( sqlite3ExprDelete(db, pLeft); sqlite3ExprDelete(db, pRight); }else{ + assert( ExprUseXList(pRoot) ); + assert( pRoot->x.pSelect==0 ); if( pRight ){ pRoot->pRight = pRight; pRoot->flags |= EP_Propagate & pRight->flags; +#if SQLITE_MAX_EXPR_DEPTH>0 + pRoot->nHeight = pRight->nHeight+1; + }else{ + pRoot->nHeight = 1; +#endif } if( pLeft ){ pRoot->pLeft = pLeft; pRoot->flags |= EP_Propagate & pLeft->flags; +#if SQLITE_MAX_EXPR_DEPTH>0 + if( pLeft->nHeight>=pRoot->nHeight ){ + pRoot->nHeight = pLeft->nHeight+1; + } +#endif } - exprSetHeight(pRoot); } } @@ -545,71 +1038,121 @@ Expr *sqlite3PExpr( Parse *pParse, /* Parsing context */ int op, /* Expression opcode */ Expr *pLeft, /* Left operand */ - Expr *pRight, /* Right operand */ - const Token *pToken /* Argument token */ + Expr *pRight /* Right operand */ ){ Expr *p; - if( op==TK_AND && pParse->nErr==0 ){ - /* Take advantage of short-circuit false optimization for AND */ - p = sqlite3ExprAnd(pParse->db, pLeft, pRight); - }else{ - p = sqlite3ExprAlloc(pParse->db, op & TKFLG_MASK, pToken, 1); + p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)); + if( p ){ + memset(p, 0, sizeof(Expr)); + p->op = op & 0xff; + p->iAgg = -1; sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight); - } - if( p ) { sqlite3ExprCheckHeight(pParse, p->nHeight); + }else{ + sqlite3ExprDelete(pParse->db, pLeft); + sqlite3ExprDelete(pParse->db, pRight); } return p; } /* -** If the expression is always either TRUE or FALSE (respectively), -** then return 1. If one cannot determine the truth value of the -** expression at compile-time return 0. +** Add pSelect to the Expr.x.pSelect field. Or, if pExpr is NULL (due +** do a memory allocation failure) then delete the pSelect object. +*/ +void sqlite3PExprAddSelect(Parse *pParse, Expr *pExpr, Select *pSelect){ + if( pExpr ){ + pExpr->x.pSelect = pSelect; + ExprSetProperty(pExpr, EP_xIsSelect|EP_Subquery); + sqlite3ExprSetHeightAndFlags(pParse, pExpr); + }else{ + assert( pParse->db->mallocFailed ); + sqlite3SelectDelete(pParse->db, pSelect); + } +} + +/* +** Expression list pEList is a list of vector values. This function +** converts the contents of pEList to a VALUES(...) Select statement +** returning 1 row for each element of the list. For example, the +** expression list: +** +** ( (1,2), (3,4) (5,6) ) +** +** is translated to the equivalent of: +** +** VALUES(1,2), (3,4), (5,6) ** -** This is an optimization. If is OK to return 0 here even if -** the expression really is always false or false (a false negative). -** But it is a bug to return 1 if the expression might have different -** boolean values in different circumstances (a false positive.) +** Each of the vector values in pEList must contain exactly nElem terms. +** If a list element that is not a vector or does not contain nElem terms, +** an error message is left in pParse. ** -** Note that if the expression is part of conditional for a -** LEFT JOIN, then we cannot determine at compile-time whether or not -** is it true or false, so always return 0. +** This is used as part of processing IN(...) expressions with a list +** of vectors on the RHS. e.g. "... IN ((1,2), (3,4), (5,6))". */ -static int exprAlwaysTrue(Expr *p){ - int v = 0; - if( ExprHasProperty(p, EP_FromJoin) ) return 0; - if( !sqlite3ExprIsInteger(p, &v) ) return 0; - return v!=0; -} -static int exprAlwaysFalse(Expr *p){ - int v = 0; - if( ExprHasProperty(p, EP_FromJoin) ) return 0; - if( !sqlite3ExprIsInteger(p, &v) ) return 0; - return v==0; +Select *sqlite3ExprListToValues(Parse *pParse, int nElem, ExprList *pEList){ + int ii; + Select *pRet = 0; + assert( nElem>1 ); + for(ii=0; iinExpr; ii++){ + Select *pSel; + Expr *pExpr = pEList->a[ii].pExpr; + int nExprElem; + if( pExpr->op==TK_VECTOR ){ + assert( ExprUseXList(pExpr) ); + nExprElem = pExpr->x.pList->nExpr; + }else{ + nExprElem = 1; + } + if( nExprElem!=nElem ){ + sqlite3ErrorMsg(pParse, "IN(...) element has %d term%s - expected %d", + nExprElem, nExprElem>1?"s":"", nElem + ); + break; + } + assert( ExprUseXList(pExpr) ); + pSel = sqlite3SelectNew(pParse, pExpr->x.pList, 0, 0, 0, 0, 0, SF_Values,0); + pExpr->x.pList = 0; + if( pSel ){ + if( pRet ){ + pSel->op = TK_ALL; + pSel->pPrior = pRet; + } + pRet = pSel; + } + } + + if( pRet && pRet->pPrior ){ + pRet->selFlags |= SF_MultiValue; + } + sqlite3ExprListDelete(pParse->db, pEList); + return pRet; } /* ** Join two expressions using an AND operator. If either expression is ** NULL, then just return the other expression. ** -** If one side or the other of the AND is known to be false, then instead -** of returning an AND expression, just return a constant expression with -** a value of false. +** If one side or the other of the AND is known to be false, and neither side +** is part of an ON clause, then instead of returning an AND expression, +** just return a constant expression with a value of false. */ -Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){ - if( pLeft==0 ){ +Expr *sqlite3ExprAnd(Parse *pParse, Expr *pLeft, Expr *pRight){ + sqlite3 *db = pParse->db; + if( pLeft==0 ){ return pRight; }else if( pRight==0 ){ return pLeft; - }else if( exprAlwaysFalse(pLeft) || exprAlwaysFalse(pRight) ){ - sqlite3ExprDelete(db, pLeft); - sqlite3ExprDelete(db, pRight); - return sqlite3ExprAlloc(db, TK_INTEGER, &sqlite3IntTokens[0], 0); }else{ - Expr *pNew = sqlite3ExprAlloc(db, TK_AND, 0, 0); - sqlite3ExprAttachSubtrees(db, pNew, pLeft, pRight); - return pNew; + u32 f = pLeft->flags | pRight->flags; + if( (f&(EP_OuterON|EP_InnerON|EP_IsFalse|EP_HasFunc))==EP_IsFalse + && !IN_RENAME_OBJECT + ){ + sqlite3ExprDeferredDelete(pParse, pLeft); + sqlite3ExprDeferredDelete(pParse, pRight); + return sqlite3Expr(db, TK_INTEGER, "0"); + }else{ + return sqlite3PExpr(pParse, TK_AND, pLeft, pRight); + } } } @@ -617,7 +1160,12 @@ Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){ ** Construct a new expression node for a function with multiple ** arguments. */ -Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){ +Expr *sqlite3ExprFunction( + Parse *pParse, /* Parsing context */ + ExprList *pList, /* Argument list */ + const Token *pToken, /* Name of the function */ + int eDistinct /* SF_Distinct or SF_ALL or 0 */ +){ Expr *pNew; sqlite3 *db = pParse->db; assert( pToken ); @@ -626,50 +1174,166 @@ Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){ sqlite3ExprListDelete(db, pList); /* Avoid memory leak when malloc fails */ return 0; } + assert( !ExprHasProperty(pNew, EP_InnerON|EP_OuterON) ); + pNew->w.iOfst = (int)(pToken->z - pParse->zTail); + if( pList + && pList->nExpr > pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG] + && !pParse->nested + ){ + sqlite3ErrorMsg(pParse, "too many arguments on function %T", pToken); + } pNew->x.pList = pList; - assert( !ExprHasProperty(pNew, EP_xIsSelect) ); + ExprSetProperty(pNew, EP_HasFunc); + assert( ExprUseXList(pNew) ); sqlite3ExprSetHeightAndFlags(pParse, pNew); + if( eDistinct==SF_Distinct ) ExprSetProperty(pNew, EP_Distinct); return pNew; } /* -** Assign a variable number to an expression that encodes a wildcard -** in the original SQL statement. -** -** Wildcards consisting of a single "?" are assigned the next sequential -** variable number. +** Report an error when attempting to use an ORDER BY clause within +** the arguments of a non-aggregate function. +*/ +void sqlite3ExprOrderByAggregateError(Parse *pParse, Expr *p){ + sqlite3ErrorMsg(pParse, + "ORDER BY may not be used with non-aggregate %#T()", p + ); +} + +/* +** Attach an ORDER BY clause to a function call. ** -** Wildcards of the form "?nnn" are assigned the number "nnn". We make -** sure "nnn" is not too be to avoid a denial of service attack when -** the SQL statement comes from an external source. +** functionname( arguments ORDER BY sortlist ) +** \_____________________/ \______/ +** pExpr pOrderBy ** -** Wildcards of the form ":aaa", "@aaa", or "$aaa" are assigned the same number -** as the previous instance of the same wildcard. Or if this is the first -** instance of the wildcard, the next sequential variable number is -** assigned. +** The ORDER BY clause is inserted into a new Expr node of type TK_ORDER +** and added to the Expr.pLeft field of the parent TK_FUNCTION node. */ -void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){ +void sqlite3ExprAddFunctionOrderBy( + Parse *pParse, /* Parsing context */ + Expr *pExpr, /* The function call to which ORDER BY is to be added */ + ExprList *pOrderBy /* The ORDER BY clause to add */ +){ + Expr *pOB; sqlite3 *db = pParse->db; - const char *z; - - if( pExpr==0 ) return; - assert( !ExprHasProperty(pExpr, EP_IntValue|EP_Reduced|EP_TokenOnly) ); - z = pExpr->u.zToken; - assert( z!=0 ); - assert( z[0]!=0 ); - if( z[1]==0 ){ - /* Wildcard of the form "?". Assign the next variable number */ - assert( z[0]=='?' ); - pExpr->iColumn = (ynVar)(++pParse->nVar); - }else{ - ynVar x = 0; - u32 n = sqlite3Strlen30(z); + if( NEVER(pOrderBy==0) ){ + assert( db->mallocFailed ); + return; + } + if( pExpr==0 ){ + assert( db->mallocFailed ); + sqlite3ExprListDelete(db, pOrderBy); + return; + } + assert( pExpr->op==TK_FUNCTION ); + assert( pExpr->pLeft==0 ); + assert( ExprUseXList(pExpr) ); + if( pExpr->x.pList==0 || NEVER(pExpr->x.pList->nExpr==0) ){ + /* Ignore ORDER BY on zero-argument aggregates */ + sqlite3ParserAddCleanup(pParse, sqlite3ExprListDeleteGeneric, pOrderBy); + return; + } + if( IsWindowFunc(pExpr) ){ + sqlite3ExprOrderByAggregateError(pParse, pExpr); + sqlite3ExprListDelete(db, pOrderBy); + return; + } + if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){ + sqlite3ErrorMsg(pParse, "too many terms in ORDER BY clause"); + sqlite3ExprListDelete(db, pOrderBy); + return; + } + + pOB = sqlite3ExprAlloc(db, TK_ORDER, 0, 0); + if( pOB==0 ){ + sqlite3ExprListDelete(db, pOrderBy); + return; + } + pOB->x.pList = pOrderBy; + assert( ExprUseXList(pOB) ); + pExpr->pLeft = pOB; + ExprSetProperty(pOB, EP_FullSize); +} + +/* +** Check to see if a function is usable according to current access +** rules: +** +** SQLITE_FUNC_DIRECT - Only usable from top-level SQL +** +** SQLITE_FUNC_UNSAFE - Usable if TRUSTED_SCHEMA or from +** top-level SQL +** +** If the function is not usable, create an error. +*/ +void sqlite3ExprFunctionUsable( + Parse *pParse, /* Parsing and code generating context */ + const Expr *pExpr, /* The function invocation */ + const FuncDef *pDef /* The function being invoked */ +){ + assert( !IN_RENAME_OBJECT ); + assert( (pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE))!=0 ); + if( ExprHasProperty(pExpr, EP_FromDDL) ){ + if( (pDef->funcFlags & SQLITE_FUNC_DIRECT)!=0 + || (pParse->db->flags & SQLITE_TrustedSchema)==0 + ){ + /* Functions prohibited in triggers and views if: + ** (1) tagged with SQLITE_DIRECTONLY + ** (2) not tagged with SQLITE_INNOCUOUS (which means it + ** is tagged with SQLITE_FUNC_UNSAFE) and + ** SQLITE_DBCONFIG_TRUSTED_SCHEMA is off (meaning + ** that the schema is possibly tainted). + */ + sqlite3ErrorMsg(pParse, "unsafe use of %#T()", pExpr); + } + } +} + +/* +** Assign a variable number to an expression that encodes a wildcard +** in the original SQL statement. +** +** Wildcards consisting of a single "?" are assigned the next sequential +** variable number. +** +** Wildcards of the form "?nnn" are assigned the number "nnn". We make +** sure "nnn" is not too big to avoid a denial of service attack when +** the SQL statement comes from an external source. +** +** Wildcards of the form ":aaa", "@aaa", or "$aaa" are assigned the same number +** as the previous instance of the same wildcard. Or if this is the first +** instance of the wildcard, the next sequential variable number is +** assigned. +*/ +void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr, u32 n){ + sqlite3 *db = pParse->db; + const char *z; + ynVar x; + + if( pExpr==0 ) return; + assert( !ExprHasProperty(pExpr, EP_IntValue|EP_Reduced|EP_TokenOnly) ); + z = pExpr->u.zToken; + assert( z!=0 ); + assert( z[0]!=0 ); + assert( n==(u32)sqlite3Strlen30(z) ); + if( z[1]==0 ){ + /* Wildcard of the form "?". Assign the next variable number */ + assert( z[0]=='?' ); + x = (ynVar)(++pParse->nVar); + }else{ + int doAdd = 0; if( z[0]=='?' ){ /* Wildcard of the form "?nnn". Convert "nnn" to an integer and ** use it as the variable number */ i64 i; - int bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8); - pExpr->iColumn = x = (ynVar)i; + int bOk; + if( n==2 ){ /*OPTIMIZATION-IF-TRUE*/ + i = z[1]-'0'; /* The common case of ?N for a single digit N */ + bOk = 1; + }else{ + bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8); + } testcase( i==0 ); testcase( i==1 ); testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 ); @@ -677,78 +1341,144 @@ void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){ if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){ sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d", db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]); - x = 0; + sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr); + return; } - if( i>pParse->nVar ){ - pParse->nVar = (int)i; + x = (ynVar)i; + if( x>pParse->nVar ){ + pParse->nVar = (int)x; + doAdd = 1; + }else if( sqlite3VListNumToName(pParse->pVList, x)==0 ){ + doAdd = 1; } }else{ /* Wildcards like ":aaa", "$aaa" or "@aaa". Reuse the same variable ** number as the prior appearance of the same name, or if the name ** has never appeared before, reuse the same variable number */ - ynVar i; - for(i=0; inzVar; i++){ - if( pParse->azVar[i] && strcmp(pParse->azVar[i],z)==0 ){ - pExpr->iColumn = x = (ynVar)i+1; - break; - } - } - if( x==0 ) x = pExpr->iColumn = (ynVar)(++pParse->nVar); - } - if( x>0 ){ - if( x>pParse->nzVar ){ - char **a; - a = sqlite3DbRealloc(db, pParse->azVar, x*sizeof(a[0])); - if( a==0 ){ - assert( db->mallocFailed ); /* Error reported through mallocFailed */ - return; - } - pParse->azVar = a; - memset(&a[pParse->nzVar], 0, (x-pParse->nzVar)*sizeof(a[0])); - pParse->nzVar = x; - } - if( z[0]!='?' || pParse->azVar[x-1]==0 ){ - sqlite3DbFree(db, pParse->azVar[x-1]); - pParse->azVar[x-1] = sqlite3DbStrNDup(db, z, n); + x = (ynVar)sqlite3VListNameToNum(pParse->pVList, z, n); + if( x==0 ){ + x = (ynVar)(++pParse->nVar); + doAdd = 1; } } - } - if( !pParse->nErr && pParse->nVar>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){ + if( doAdd ){ + pParse->pVList = sqlite3VListAdd(db, pParse->pVList, z, n, x); + } + } + pExpr->iColumn = x; + if( x>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){ sqlite3ErrorMsg(pParse, "too many SQL variables"); + sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr); } } /* ** Recursively delete an expression tree. */ -void sqlite3ExprDelete(sqlite3 *db, Expr *p){ - if( p==0 ) return; - /* Sanity check: Assert that the IntValue is non-negative if it exists */ - assert( !ExprHasProperty(p, EP_IntValue) || p->u.iValue>=0 ); - if( !ExprHasProperty(p, EP_TokenOnly) ){ +static SQLITE_NOINLINE void sqlite3ExprDeleteNN(sqlite3 *db, Expr *p){ + assert( p!=0 ); + assert( db!=0 ); +exprDeleteRestart: + assert( !ExprUseUValue(p) || p->u.iValue>=0 ); + assert( !ExprUseYWin(p) || !ExprUseYSub(p) ); + assert( !ExprUseYWin(p) || p->y.pWin!=0 || db->mallocFailed ); + assert( p->op!=TK_FUNCTION || !ExprUseYSub(p) ); +#ifdef SQLITE_DEBUG + if( ExprHasProperty(p, EP_Leaf) && !ExprHasProperty(p, EP_TokenOnly) ){ + assert( p->pLeft==0 ); + assert( p->pRight==0 ); + assert( !ExprUseXSelect(p) || p->x.pSelect==0 ); + assert( !ExprUseXList(p) || p->x.pList==0 ); + } +#endif + if( !ExprHasProperty(p, (EP_TokenOnly|EP_Leaf)) ){ /* The Expr.x union is never used at the same time as Expr.pRight */ - assert( p->x.pList==0 || p->pRight==0 ); - sqlite3ExprDelete(db, p->pLeft); - sqlite3ExprDelete(db, p->pRight); - if( ExprHasProperty(p, EP_MemToken) ) sqlite3DbFree(db, p->u.zToken); - if( ExprHasProperty(p, EP_xIsSelect) ){ + assert( (ExprUseXList(p) && p->x.pList==0) || p->pRight==0 ); + if( p->pRight ){ + assert( !ExprHasProperty(p, EP_WinFunc) ); + sqlite3ExprDeleteNN(db, p->pRight); + }else if( ExprUseXSelect(p) ){ + assert( !ExprHasProperty(p, EP_WinFunc) ); sqlite3SelectDelete(db, p->x.pSelect); }else{ sqlite3ExprListDelete(db, p->x.pList); +#ifndef SQLITE_OMIT_WINDOWFUNC + if( ExprHasProperty(p, EP_WinFunc) ){ + sqlite3WindowDelete(db, p->y.pWin); + } +#endif + } + if( p->pLeft && p->op!=TK_SELECT_COLUMN ){ + Expr *pLeft = p->pLeft; + if( !ExprHasProperty(p, EP_Static) + && !ExprHasProperty(pLeft, EP_Static) + ){ + /* Avoid unnecessary recursion on unary operators */ + sqlite3DbNNFreeNN(db, p); + p = pLeft; + goto exprDeleteRestart; + }else{ + sqlite3ExprDeleteNN(db, pLeft); + } } } if( !ExprHasProperty(p, EP_Static) ){ - sqlite3DbFree(db, p); + sqlite3DbNNFreeNN(db, p); + } +} +void sqlite3ExprDelete(sqlite3 *db, Expr *p){ + if( p ) sqlite3ExprDeleteNN(db, p); +} +void sqlite3ExprDeleteGeneric(sqlite3 *db, void *p){ + if( ALWAYS(p) ) sqlite3ExprDeleteNN(db, (Expr*)p); +} + +/* +** Clear both elements of an OnOrUsing object +*/ +void sqlite3ClearOnOrUsing(sqlite3 *db, OnOrUsing *p){ + if( p==0 ){ + /* Nothing to clear */ + }else if( p->pOn ){ + sqlite3ExprDeleteNN(db, p->pOn); + }else if( p->pUsing ){ + sqlite3IdListDelete(db, p->pUsing); + } +} + +/* +** Arrange to cause pExpr to be deleted when the pParse is deleted. +** This is similar to sqlite3ExprDelete() except that the delete is +** deferred until the pParse is deleted. +** +** The pExpr might be deleted immediately on an OOM error. +** +** Return 0 if the delete was successfully deferred. Return non-zero +** if the delete happened immediately because of an OOM. +*/ +int sqlite3ExprDeferredDelete(Parse *pParse, Expr *pExpr){ + return 0==sqlite3ParserAddCleanup(pParse, sqlite3ExprDeleteGeneric, pExpr); +} + +/* Invoke sqlite3RenameExprUnmap() and sqlite3ExprDelete() on the +** expression. +*/ +void sqlite3ExprUnmapAndDelete(Parse *pParse, Expr *p){ + if( p ){ + if( IN_RENAME_OBJECT ){ + sqlite3RenameExprUnmap(pParse, p); + } + sqlite3ExprDeleteNN(pParse->db, p); } } /* -** Return the number of bytes allocated for the expression structure +** Return the number of bytes allocated for the expression structure ** passed as the first argument. This is always one of EXPR_FULLSIZE, ** EXPR_REDUCEDSIZE or EXPR_TOKENONLYSIZE. */ -static int exprStructSize(Expr *p){ +static int exprStructSize(const Expr *p){ if( ExprHasProperty(p, EP_TokenOnly) ) return EXPR_TOKENONLYSIZE; if( ExprHasProperty(p, EP_Reduced) ) return EXPR_REDUCEDSIZE; return EXPR_FULLSIZE; @@ -759,14 +1489,14 @@ static int exprStructSize(Expr *p){ ** to store a copy of an expression or expression tree. They differ in ** how much of the tree is measured. ** -** dupedExprStructSize() Size of only the Expr structure +** dupedExprStructSize() Size of only the Expr structure ** dupedExprNodeSize() Size of Expr + space for token ** dupedExprSize() Expr + token + subtree components ** *************************************************************************** ** -** The dupedExprStructSize() function returns two values OR-ed together: -** (1) the space required for a copy of the Expr structure only and +** The dupedExprStructSize() function returns two values OR-ed together: +** (1) the space required for a copy of the Expr structure only and ** (2) the EP_xxx flags that indicate what the structure size should be. ** The return values is always one of: ** @@ -781,25 +1511,24 @@ static int exprStructSize(Expr *p){ ** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size ** (unreduced) Expr objects as they or originally constructed by the parser. ** During expression analysis, extra information is computed and moved into -** later parts of teh Expr object and that extra information might get chopped +** later parts of the Expr object and that extra information might get chopped ** off if the expression is reduced. Note also that it does not work to ** make an EXPRDUP_REDUCE copy of a reduced expression. It is only legal ** to reduce a pristine expression tree from the parser. The implementation ** of dupedExprStructSize() contain multiple assert() statements that attempt ** to enforce this constraint. */ -static int dupedExprStructSize(Expr *p, int flags){ +static int dupedExprStructSize(const Expr *p, int flags){ int nSize; assert( flags==EXPRDUP_REDUCE || flags==0 ); /* Only one flag value allowed */ assert( EXPR_FULLSIZE<=0xfff ); assert( (0xfff & (EP_Reduced|EP_TokenOnly))==0 ); - if( 0==(flags&EXPRDUP_REDUCE) ){ + if( 0==flags || ExprHasProperty(p, EP_FullSize) ){ nSize = EXPR_FULLSIZE; }else{ assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) ); - assert( !ExprHasProperty(p, EP_FromJoin) ); - assert( !ExprHasProperty(p, EP_MemToken) ); - assert( !ExprHasProperty(p, EP_NoReduce) ); + assert( !ExprHasProperty(p, EP_OuterON) ); + assert( !ExprHasVVAProperty(p, EP_NoReduce) ); if( p->pLeft || p->x.pList ){ nSize = EXPR_REDUCEDSIZE | EP_Reduced; }else{ @@ -811,147 +1540,214 @@ static int dupedExprStructSize(Expr *p, int flags){ } /* -** This function returns the space in bytes required to store the copy +** This function returns the space in bytes required to store the copy ** of the Expr structure and a copy of the Expr.u.zToken string (if that ** string is defined.) */ -static int dupedExprNodeSize(Expr *p, int flags){ +static int dupedExprNodeSize(const Expr *p, int flags){ int nByte = dupedExprStructSize(p, flags) & 0xfff; if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){ - nByte += sqlite3Strlen30(p->u.zToken)+1; + nByte += sqlite3Strlen30NN(p->u.zToken)+1; } return ROUND8(nByte); } /* -** Return the number of bytes required to create a duplicate of the -** expression passed as the first argument. The second argument is a -** mask containing EXPRDUP_XXX flags. +** Return the number of bytes required to create a duplicate of the +** expression passed as the first argument. ** ** The value returned includes space to create a copy of the Expr struct ** itself and the buffer referred to by Expr.u.zToken, if any. ** -** If the EXPRDUP_REDUCE flag is set, then the return value includes -** space to duplicate all Expr nodes in the tree formed by Expr.pLeft -** and Expr.pRight variables (but not for any structures pointed to or -** descended from the Expr.x.pList or Expr.x.pSelect variables). +** The return value includes space to duplicate all Expr nodes in the +** tree formed by Expr.pLeft and Expr.pRight, but not any other +** substructure such as Expr.x.pList, Expr.x.pSelect, and Expr.y.pWin. */ -static int dupedExprSize(Expr *p, int flags){ - int nByte = 0; - if( p ){ - nByte = dupedExprNodeSize(p, flags); - if( flags&EXPRDUP_REDUCE ){ - nByte += dupedExprSize(p->pLeft, flags) + dupedExprSize(p->pRight, flags); - } - } +static int dupedExprSize(const Expr *p){ + int nByte; + assert( p!=0 ); + nByte = dupedExprNodeSize(p, EXPRDUP_REDUCE); + if( p->pLeft ) nByte += dupedExprSize(p->pLeft); + if( p->pRight ) nByte += dupedExprSize(p->pRight); + assert( nByte==ROUND8(nByte) ); return nByte; } /* -** This function is similar to sqlite3ExprDup(), except that if pzBuffer -** is not NULL then *pzBuffer is assumed to point to a buffer large enough -** to store the copy of expression p, the copies of p->u.zToken -** (if applicable), and the copies of the p->pLeft and p->pRight expressions, -** if any. Before returning, *pzBuffer is set to the first byte past the -** portion of the buffer copied into by this function. +** An EdupBuf is a memory allocation used to stored multiple Expr objects +** together with their Expr.zToken content. This is used to help implement +** compression while doing sqlite3ExprDup(). The top-level Expr does the +** allocation for itself and many of its decendents, then passes an instance +** of the structure down into exprDup() so that they decendents can have +** access to that memory. */ -static Expr *exprDup(sqlite3 *db, Expr *p, int flags, u8 **pzBuffer){ - Expr *pNew = 0; /* Value to return */ - assert( flags==0 || flags==EXPRDUP_REDUCE ); - assert( db!=0 ); - if( p ){ - const int isReduced = (flags&EXPRDUP_REDUCE); - u8 *zAlloc; - u32 staticFlag = 0; +typedef struct EdupBuf EdupBuf; +struct EdupBuf { + u8 *zAlloc; /* Memory space available for storage */ +#ifdef SQLITE_DEBUG + u8 *zEnd; /* First byte past the end of memory */ +#endif +}; + +/* +** This function is similar to sqlite3ExprDup(), except that if pEdupBuf +** is not NULL then it points to memory that can be used to store a copy +** of the input Expr p together with its p->u.zToken (if any). pEdupBuf +** is updated with the new buffer tail prior to returning. +*/ +static Expr *exprDup( + sqlite3 *db, /* Database connection (for memory allocation) */ + const Expr *p, /* Expr tree to be duplicated */ + int dupFlags, /* EXPRDUP_REDUCE for compression. 0 if not */ + EdupBuf *pEdupBuf /* Preallocated storage space, or NULL */ +){ + Expr *pNew; /* Value to return */ + EdupBuf sEdupBuf; /* Memory space from which to build Expr object */ + u32 staticFlag; /* EP_Static if space not obtained from malloc */ + int nToken = -1; /* Space needed for p->u.zToken. -1 means unknown */ - assert( pzBuffer==0 || isReduced ); + assert( db!=0 ); + assert( p ); + assert( dupFlags==0 || dupFlags==EXPRDUP_REDUCE ); + assert( pEdupBuf==0 || dupFlags==EXPRDUP_REDUCE ); - /* Figure out where to write the new Expr structure. */ - if( pzBuffer ){ - zAlloc = *pzBuffer; - staticFlag = EP_Static; + /* Figure out where to write the new Expr structure. */ + if( pEdupBuf ){ + sEdupBuf.zAlloc = pEdupBuf->zAlloc; +#ifdef SQLITE_DEBUG + sEdupBuf.zEnd = pEdupBuf->zEnd; +#endif + staticFlag = EP_Static; + assert( sEdupBuf.zAlloc!=0 ); + assert( dupFlags==EXPRDUP_REDUCE ); + }else{ + int nAlloc; + if( dupFlags ){ + nAlloc = dupedExprSize(p); + }else if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){ + nToken = sqlite3Strlen30NN(p->u.zToken)+1; + nAlloc = ROUND8(EXPR_FULLSIZE + nToken); }else{ - zAlloc = sqlite3DbMallocRawNN(db, dupedExprSize(p, flags)); + nToken = 0; + nAlloc = ROUND8(EXPR_FULLSIZE); } - pNew = (Expr *)zAlloc; + assert( nAlloc==ROUND8(nAlloc) ); + sEdupBuf.zAlloc = sqlite3DbMallocRawNN(db, nAlloc); +#ifdef SQLITE_DEBUG + sEdupBuf.zEnd = sEdupBuf.zAlloc ? sEdupBuf.zAlloc+nAlloc : 0; +#endif + + staticFlag = 0; + } + pNew = (Expr *)sEdupBuf.zAlloc; + assert( EIGHT_BYTE_ALIGNMENT(pNew) ); - if( pNew ){ - /* Set nNewSize to the size allocated for the structure pointed to - ** by pNew. This is either EXPR_FULLSIZE, EXPR_REDUCEDSIZE or - ** EXPR_TOKENONLYSIZE. nToken is set to the number of bytes consumed - ** by the copy of the p->u.zToken string (if any). - */ - const unsigned nStructSize = dupedExprStructSize(p, flags); - const int nNewSize = nStructSize & 0xfff; - int nToken; + if( pNew ){ + /* Set nNewSize to the size allocated for the structure pointed to + ** by pNew. This is either EXPR_FULLSIZE, EXPR_REDUCEDSIZE or + ** EXPR_TOKENONLYSIZE. nToken is set to the number of bytes consumed + ** by the copy of the p->u.zToken string (if any). + */ + const unsigned nStructSize = dupedExprStructSize(p, dupFlags); + int nNewSize = nStructSize & 0xfff; + if( nToken<0 ){ if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){ nToken = sqlite3Strlen30(p->u.zToken) + 1; }else{ nToken = 0; } - if( isReduced ){ - assert( ExprHasProperty(p, EP_Reduced)==0 ); - memcpy(zAlloc, p, nNewSize); - }else{ - u32 nSize = (u32)exprStructSize(p); - memcpy(zAlloc, p, nSize); - if( nSize= nNewSize+nToken ); + assert( ExprHasProperty(p, EP_Reduced)==0 ); + memcpy(sEdupBuf.zAlloc, p, nNewSize); + }else{ + u32 nSize = (u32)exprStructSize(p); + assert( (int)(sEdupBuf.zEnd - sEdupBuf.zAlloc) >= + (int)EXPR_FULLSIZE+nToken ); + memcpy(sEdupBuf.zAlloc, p, nSize); + if( nSizeflags &= ~(EP_Reduced|EP_TokenOnly|EP_Static|EP_MemToken); - pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly); - pNew->flags |= staticFlag; + /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */ + pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static); + pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly); + pNew->flags |= staticFlag; + ExprClearVVAProperties(pNew); + if( dupFlags ){ + ExprSetVVAProperty(pNew, EP_Immutable); + } + + /* Copy the p->u.zToken string, if any. */ + assert( nToken>=0 ); + if( nToken>0 ){ + char *zToken = pNew->u.zToken = (char*)&sEdupBuf.zAlloc[nNewSize]; + memcpy(zToken, p->u.zToken, nToken); + nNewSize += nToken; + } + sEdupBuf.zAlloc += ROUND8(nNewSize); + + if( ((p->flags|pNew->flags)&(EP_TokenOnly|EP_Leaf))==0 ){ - /* Copy the p->u.zToken string, if any. */ - if( nToken ){ - char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize]; - memcpy(zToken, p->u.zToken, nToken); + /* Fill in the pNew->x.pSelect or pNew->x.pList member. */ + if( ExprUseXSelect(p) ){ + pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, dupFlags); + }else{ + pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, + p->op!=TK_ORDER ? dupFlags : 0); } - if( 0==((p->flags|pNew->flags) & EP_TokenOnly) ){ - /* Fill in the pNew->x.pSelect or pNew->x.pList member. */ - if( ExprHasProperty(p, EP_xIsSelect) ){ - pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, isReduced); - }else{ - pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, isReduced); - } +#ifndef SQLITE_OMIT_WINDOWFUNC + if( ExprHasProperty(p, EP_WinFunc) ){ + pNew->y.pWin = sqlite3WindowDup(db, pNew, p->y.pWin); + assert( ExprHasProperty(pNew, EP_WinFunc) ); } +#endif /* SQLITE_OMIT_WINDOWFUNC */ /* Fill in pNew->pLeft and pNew->pRight. */ - if( ExprHasProperty(pNew, EP_Reduced|EP_TokenOnly) ){ - zAlloc += dupedExprNodeSize(p, flags); - if( ExprHasProperty(pNew, EP_Reduced) ){ - pNew->pLeft = exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc); - pNew->pRight = exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc); - } - if( pzBuffer ){ - *pzBuffer = zAlloc; + if( dupFlags ){ + if( p->op==TK_SELECT_COLUMN ){ + pNew->pLeft = p->pLeft; + assert( p->pRight==0 + || p->pRight==p->pLeft + || ExprHasProperty(p->pLeft, EP_Subquery) ); + }else{ + pNew->pLeft = p->pLeft ? + exprDup(db, p->pLeft, EXPRDUP_REDUCE, &sEdupBuf) : 0; } + pNew->pRight = p->pRight ? + exprDup(db, p->pRight, EXPRDUP_REDUCE, &sEdupBuf) : 0; }else{ - if( !ExprHasProperty(p, EP_TokenOnly) ){ + if( p->op==TK_SELECT_COLUMN ){ + pNew->pLeft = p->pLeft; + assert( p->pRight==0 + || p->pRight==p->pLeft + || ExprHasProperty(p->pLeft, EP_Subquery) ); + }else{ pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0); - pNew->pRight = sqlite3ExprDup(db, p->pRight, 0); } + pNew->pRight = sqlite3ExprDup(db, p->pRight, 0); } - } } + if( pEdupBuf ) memcpy(pEdupBuf, &sEdupBuf, sizeof(sEdupBuf)); + assert( sEdupBuf.zAlloc <= sEdupBuf.zEnd ); return pNew; } /* -** Create and return a deep copy of the object passed as the second +** Create and return a deep copy of the object passed as the second ** argument. If an OOM condition is encountered, NULL is returned ** and the db->mallocFailed flag set. */ #ifndef SQLITE_OMIT_CTE -static With *withDup(sqlite3 *db, With *p){ +With *sqlite3WithDup(sqlite3 *db, With *p){ With *pRet = 0; if( p ){ - int nByte = sizeof(*p) + sizeof(p->a[0]) * (p->nCte-1); + sqlite3_int64 nByte = SZ_WITH(p->nCte); pRet = sqlite3DbMallocZero(db, nByte); if( pRet ){ int i; @@ -960,15 +1756,49 @@ static With *withDup(sqlite3 *db, With *p){ pRet->a[i].pSelect = sqlite3SelectDup(db, p->a[i].pSelect, 0); pRet->a[i].pCols = sqlite3ExprListDup(db, p->a[i].pCols, 0); pRet->a[i].zName = sqlite3DbStrDup(db, p->a[i].zName); + pRet->a[i].eM10d = p->a[i].eM10d; } } } return pRet; } #else -# define withDup(x,y) 0 +# define sqlite3WithDup(x,y) 0 +#endif + +#ifndef SQLITE_OMIT_WINDOWFUNC +/* +** The gatherSelectWindows() procedure and its helper routine +** gatherSelectWindowsCallback() are used to scan all the expressions +** an a newly duplicated SELECT statement and gather all of the Window +** objects found there, assembling them onto the linked list at Select->pWin. +*/ +static int gatherSelectWindowsCallback(Walker *pWalker, Expr *pExpr){ + if( pExpr->op==TK_FUNCTION && ExprHasProperty(pExpr, EP_WinFunc) ){ + Select *pSelect = pWalker->u.pSelect; + Window *pWin = pExpr->y.pWin; + assert( pWin ); + assert( IsWindowFunc(pExpr) ); + assert( pWin->ppThis==0 ); + sqlite3WindowLink(pSelect, pWin); + } + return WRC_Continue; +} +static int gatherSelectWindowsSelectCallback(Walker *pWalker, Select *p){ + return p==pWalker->u.pSelect ? WRC_Continue : WRC_Prune; +} +static void gatherSelectWindows(Select *p){ + Walker w; + w.xExprCallback = gatherSelectWindowsCallback; + w.xSelectCallback = gatherSelectWindowsSelectCallback; + w.xSelectCallback2 = 0; + w.pParse = 0; + w.u.pSelect = p; + sqlite3WalkSelect(&w, p); +} #endif + /* ** The following group of routines make deep copies of expressions, ** expression lists, ID lists, and select statements. The copies can @@ -976,7 +1806,7 @@ static With *withDup(sqlite3 *db, With *p){ ** without effecting the originals. ** ** The expression list, ID, and source lists return by sqlite3ExprListDup(), -** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded +** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded ** by subsequent calls to sqlite*ListAppend() routines. ** ** Any tables that the SrcList might point to are not duplicated. @@ -986,34 +1816,48 @@ static With *withDup(sqlite3 *db, With *p){ ** truncated version of the usual Expr structure that will be stored as ** part of the in-memory representation of the database schema. */ -Expr *sqlite3ExprDup(sqlite3 *db, Expr *p, int flags){ +Expr *sqlite3ExprDup(sqlite3 *db, const Expr *p, int flags){ assert( flags==0 || flags==EXPRDUP_REDUCE ); - return exprDup(db, p, flags, 0); + return p ? exprDup(db, p, flags, 0) : 0; } -ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags){ +ExprList *sqlite3ExprListDup(sqlite3 *db, const ExprList *p, int flags){ ExprList *pNew; - struct ExprList_item *pItem, *pOldItem; + struct ExprList_item *pItem; + const struct ExprList_item *pOldItem; int i; + Expr *pPriorSelectColOld = 0; + Expr *pPriorSelectColNew = 0; assert( db!=0 ); if( p==0 ) return 0; - pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew) ); + pNew = sqlite3DbMallocRawNN(db, sqlite3DbMallocSize(db, p)); if( pNew==0 ) return 0; - pNew->nExpr = i = p->nExpr; - if( (flags & EXPRDUP_REDUCE)==0 ) for(i=1; inExpr; i+=i){} - pNew->a = pItem = sqlite3DbMallocRawNN(db, i*sizeof(p->a[0]) ); - if( pItem==0 ){ - sqlite3DbFree(db, pNew); - return 0; - } + pNew->nExpr = p->nExpr; + pNew->nAlloc = p->nAlloc; + pItem = pNew->a; pOldItem = p->a; for(i=0; inExpr; i++, pItem++, pOldItem++){ Expr *pOldExpr = pOldItem->pExpr; + Expr *pNewExpr; pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags); - pItem->zName = sqlite3DbStrDup(db, pOldItem->zName); - pItem->zSpan = sqlite3DbStrDup(db, pOldItem->zSpan); - pItem->sortOrder = pOldItem->sortOrder; - pItem->done = 0; - pItem->bSpanIsTab = pOldItem->bSpanIsTab; + if( pOldExpr + && pOldExpr->op==TK_SELECT_COLUMN + && (pNewExpr = pItem->pExpr)!=0 + ){ + if( pNewExpr->pRight ){ + pPriorSelectColOld = pOldExpr->pRight; + pPriorSelectColNew = pNewExpr->pRight; + pNewExpr->pLeft = pNewExpr->pRight; + }else{ + if( pOldExpr->pLeft!=pPriorSelectColOld ){ + pPriorSelectColOld = pOldExpr->pLeft; + pPriorSelectColNew = sqlite3ExprDup(db, pPriorSelectColOld, flags); + pNewExpr->pRight = pPriorSelectColNew; + } + pNewExpr->pLeft = pPriorSelectColNew; + } + } + pItem->zEName = sqlite3DbStrDup(db, pOldItem->zEName); + pItem->fg = pOldItem->fg; pItem->u = pOldItem->u; } return pNew; @@ -1021,107 +1865,138 @@ ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags){ /* ** If cursors, triggers, views and subqueries are all omitted from -** the build, then none of the following routines, except for +** the build, then none of the following routines, except for ** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes ** called with a NULL argument. */ #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \ || !defined(SQLITE_OMIT_SUBQUERY) -SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p, int flags){ +SrcList *sqlite3SrcListDup(sqlite3 *db, const SrcList *p, int flags){ SrcList *pNew; int i; - int nByte; assert( db!=0 ); if( p==0 ) return 0; - nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0); - pNew = sqlite3DbMallocRawNN(db, nByte ); + pNew = sqlite3DbMallocRawNN(db, SZ_SRCLIST(p->nSrc) ); if( pNew==0 ) return 0; pNew->nSrc = pNew->nAlloc = p->nSrc; for(i=0; inSrc; i++){ - struct SrcList_item *pNewItem = &pNew->a[i]; - struct SrcList_item *pOldItem = &p->a[i]; + SrcItem *pNewItem = &pNew->a[i]; + const SrcItem *pOldItem = &p->a[i]; Table *pTab; - pNewItem->pSchema = pOldItem->pSchema; - pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase); + pNewItem->fg = pOldItem->fg; + if( pOldItem->fg.isSubquery ){ + Subquery *pNewSubq = sqlite3DbMallocRaw(db, sizeof(Subquery)); + if( pNewSubq==0 ){ + assert( db->mallocFailed ); + pNewItem->fg.isSubquery = 0; + }else{ + memcpy(pNewSubq, pOldItem->u4.pSubq, sizeof(*pNewSubq)); + pNewSubq->pSelect = sqlite3SelectDup(db, pNewSubq->pSelect, flags); + if( pNewSubq->pSelect==0 ){ + sqlite3DbFree(db, pNewSubq); + pNewSubq = 0; + pNewItem->fg.isSubquery = 0; + } + } + pNewItem->u4.pSubq = pNewSubq; + }else if( pOldItem->fg.fixedSchema ){ + pNewItem->u4.pSchema = pOldItem->u4.pSchema; + }else{ + pNewItem->u4.zDatabase = sqlite3DbStrDup(db, pOldItem->u4.zDatabase); + } pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName); pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias); - pNewItem->fg = pOldItem->fg; pNewItem->iCursor = pOldItem->iCursor; - pNewItem->addrFillSub = pOldItem->addrFillSub; - pNewItem->regReturn = pOldItem->regReturn; if( pNewItem->fg.isIndexedBy ){ pNewItem->u1.zIndexedBy = sqlite3DbStrDup(db, pOldItem->u1.zIndexedBy); - } - pNewItem->pIBIndex = pOldItem->pIBIndex; - if( pNewItem->fg.isTabFunc ){ - pNewItem->u1.pFuncArg = + }else if( pNewItem->fg.isTabFunc ){ + pNewItem->u1.pFuncArg = sqlite3ExprListDup(db, pOldItem->u1.pFuncArg, flags); + }else{ + pNewItem->u1.nRow = pOldItem->u1.nRow; + } + pNewItem->u2 = pOldItem->u2; + if( pNewItem->fg.isCte ){ + pNewItem->u2.pCteUse->nUse++; } - pTab = pNewItem->pTab = pOldItem->pTab; + pTab = pNewItem->pSTab = pOldItem->pSTab; if( pTab ){ - pTab->nRef++; + pTab->nTabRef++; + } + if( pOldItem->fg.isUsing ){ + assert( pNewItem->fg.isUsing ); + pNewItem->u3.pUsing = sqlite3IdListDup(db, pOldItem->u3.pUsing); + }else{ + pNewItem->u3.pOn = sqlite3ExprDup(db, pOldItem->u3.pOn, flags); } - pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags); - pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn, flags); - pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing); pNewItem->colUsed = pOldItem->colUsed; } return pNew; } -IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){ +IdList *sqlite3IdListDup(sqlite3 *db, const IdList *p){ IdList *pNew; int i; assert( db!=0 ); if( p==0 ) return 0; - pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew) ); + pNew = sqlite3DbMallocRawNN(db, SZ_IDLIST(p->nId)); if( pNew==0 ) return 0; pNew->nId = p->nId; - pNew->a = sqlite3DbMallocRawNN(db, p->nId*sizeof(p->a[0]) ); - if( pNew->a==0 ){ - sqlite3DbFree(db, pNew); - return 0; - } - /* Note that because the size of the allocation for p->a[] is not - ** necessarily a power of two, sqlite3IdListAppend() may not be called - ** on the duplicate created by this function. */ for(i=0; inId; i++){ struct IdList_item *pNewItem = &pNew->a[i]; - struct IdList_item *pOldItem = &p->a[i]; + const struct IdList_item *pOldItem = &p->a[i]; pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName); - pNewItem->idx = pOldItem->idx; } return pNew; } -Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){ - Select *pNew, *pPrior; +Select *sqlite3SelectDup(sqlite3 *db, const Select *pDup, int flags){ + Select *pRet = 0; + Select *pNext = 0; + Select **pp = &pRet; + const Select *p; + assert( db!=0 ); - if( p==0 ) return 0; - pNew = sqlite3DbMallocRawNN(db, sizeof(*p) ); - if( pNew==0 ) return 0; - pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags); - pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags); - pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags); - pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags); - pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags); - pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags); - pNew->op = p->op; - pNew->pPrior = pPrior = sqlite3SelectDup(db, p->pPrior, flags); - if( pPrior ) pPrior->pNext = pNew; - pNew->pNext = 0; - pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags); - pNew->pOffset = sqlite3ExprDup(db, p->pOffset, flags); - pNew->iLimit = 0; - pNew->iOffset = 0; - pNew->selFlags = p->selFlags & ~SF_UsesEphemeral; - pNew->addrOpenEphm[0] = -1; - pNew->addrOpenEphm[1] = -1; - pNew->nSelectRow = p->nSelectRow; - pNew->pWith = withDup(db, p->pWith); - sqlite3SelectSetName(pNew, p->zSelName); - return pNew; + for(p=pDup; p; p=p->pPrior){ + Select *pNew = sqlite3DbMallocRawNN(db, sizeof(*p) ); + if( pNew==0 ) break; + pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags); + pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags); + pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags); + pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags); + pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags); + pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags); + pNew->op = p->op; + pNew->pNext = pNext; + pNew->pPrior = 0; + pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags); + pNew->iLimit = 0; + pNew->iOffset = 0; + pNew->selFlags = p->selFlags & ~(u32)SF_UsesEphemeral; + pNew->addrOpenEphm[0] = -1; + pNew->addrOpenEphm[1] = -1; + pNew->nSelectRow = p->nSelectRow; + pNew->pWith = sqlite3WithDup(db, p->pWith); +#ifndef SQLITE_OMIT_WINDOWFUNC + pNew->pWin = 0; + pNew->pWinDefn = sqlite3WindowListDup(db, p->pWinDefn); + if( p->pWin && db->mallocFailed==0 ) gatherSelectWindows(pNew); +#endif + pNew->selId = p->selId; + if( db->mallocFailed ){ + /* Any prior OOM might have left the Select object incomplete. + ** Delete the whole thing rather than allow an incomplete Select + ** to be used by the code generator. */ + pNew->pNext = 0; + sqlite3SelectDelete(db, pNew); + break; + } + *pp = pNew; + pp = &pNew->pPrior; + pNext = pNew; + } + return pRet; } #else -Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){ +Select *sqlite3SelectDup(sqlite3 *db, const Select *p, int flags){ assert( p==0 ); return 0; } @@ -1132,65 +2007,177 @@ Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){ ** Add a new element to the end of an expression list. If pList is ** initially NULL, then create a new expression list. ** +** The pList argument must be either NULL or a pointer to an ExprList +** obtained from a prior call to sqlite3ExprListAppend(). +** ** If a memory allocation error occurs, the entire list is freed and ** NULL is returned. If non-NULL is returned, then it is guaranteed ** that the new entry was successfully appended. */ +static const struct ExprList_item zeroItem = {0}; +SQLITE_NOINLINE ExprList *sqlite3ExprListAppendNew( + sqlite3 *db, /* Database handle. Used for memory allocation */ + Expr *pExpr /* Expression to be appended. Might be NULL */ +){ + struct ExprList_item *pItem; + ExprList *pList; + + pList = sqlite3DbMallocRawNN(db, SZ_EXPRLIST(4)); + if( pList==0 ){ + sqlite3ExprDelete(db, pExpr); + return 0; + } + pList->nAlloc = 4; + pList->nExpr = 1; + pItem = &pList->a[0]; + *pItem = zeroItem; + pItem->pExpr = pExpr; + return pList; +} +SQLITE_NOINLINE ExprList *sqlite3ExprListAppendGrow( + sqlite3 *db, /* Database handle. Used for memory allocation */ + ExprList *pList, /* List to which to append. Might be NULL */ + Expr *pExpr /* Expression to be appended. Might be NULL */ +){ + struct ExprList_item *pItem; + ExprList *pNew; + pList->nAlloc *= 2; + pNew = sqlite3DbRealloc(db, pList, SZ_EXPRLIST(pList->nAlloc)); + if( pNew==0 ){ + sqlite3ExprListDelete(db, pList); + sqlite3ExprDelete(db, pExpr); + return 0; + }else{ + pList = pNew; + } + pItem = &pList->a[pList->nExpr++]; + *pItem = zeroItem; + pItem->pExpr = pExpr; + return pList; +} ExprList *sqlite3ExprListAppend( Parse *pParse, /* Parsing context */ ExprList *pList, /* List to which to append. Might be NULL */ Expr *pExpr /* Expression to be appended. Might be NULL */ ){ - sqlite3 *db = pParse->db; - assert( db!=0 ); + struct ExprList_item *pItem; if( pList==0 ){ - pList = sqlite3DbMallocRawNN(db, sizeof(ExprList) ); - if( pList==0 ){ - goto no_mem; - } - pList->nExpr = 0; - pList->a = sqlite3DbMallocRawNN(db, sizeof(pList->a[0])); - if( pList->a==0 ) goto no_mem; - }else if( (pList->nExpr & (pList->nExpr-1))==0 ){ - struct ExprList_item *a; - assert( pList->nExpr>0 ); - a = sqlite3DbRealloc(db, pList->a, pList->nExpr*2*sizeof(pList->a[0])); - if( a==0 ){ - goto no_mem; - } - pList->a = a; + return sqlite3ExprListAppendNew(pParse->db,pExpr); } - assert( pList->a!=0 ); - if( 1 ){ - struct ExprList_item *pItem = &pList->a[pList->nExpr++]; - memset(pItem, 0, sizeof(*pItem)); - pItem->pExpr = pExpr; + if( pList->nAllocnExpr+1 ){ + return sqlite3ExprListAppendGrow(pParse->db,pList,pExpr); } + pItem = &pList->a[pList->nExpr++]; + *pItem = zeroItem; + pItem->pExpr = pExpr; return pList; +} -no_mem: - /* Avoid leaking memory if malloc has failed. */ - sqlite3ExprDelete(db, pExpr); - sqlite3ExprListDelete(db, pList); - return 0; +/* +** pColumns and pExpr form a vector assignment which is part of the SET +** clause of an UPDATE statement. Like this: +** +** (a,b,c) = (expr1,expr2,expr3) +** Or: (a,b,c) = (SELECT x,y,z FROM ....) +** +** For each term of the vector assignment, append new entries to the +** expression list pList. In the case of a subquery on the RHS, append +** TK_SELECT_COLUMN expressions. +*/ +ExprList *sqlite3ExprListAppendVector( + Parse *pParse, /* Parsing context */ + ExprList *pList, /* List to which to append. Might be NULL */ + IdList *pColumns, /* List of names of LHS of the assignment */ + Expr *pExpr /* Vector expression to be appended. Might be NULL */ +){ + sqlite3 *db = pParse->db; + int n; + int i; + int iFirst = pList ? pList->nExpr : 0; + /* pColumns can only be NULL due to an OOM but an OOM will cause an + ** exit prior to this routine being invoked */ + if( NEVER(pColumns==0) ) goto vector_append_error; + if( pExpr==0 ) goto vector_append_error; + + /* If the RHS is a vector, then we can immediately check to see that + ** the size of the RHS and LHS match. But if the RHS is a SELECT, + ** wildcards ("*") in the result set of the SELECT must be expanded before + ** we can do the size check, so defer the size check until code generation. + */ + if( pExpr->op!=TK_SELECT && pColumns->nId!=(n=sqlite3ExprVectorSize(pExpr)) ){ + sqlite3ErrorMsg(pParse, "%d columns assigned %d values", + pColumns->nId, n); + goto vector_append_error; + } + + for(i=0; inId; i++){ + Expr *pSubExpr = sqlite3ExprForVectorField(pParse, pExpr, i, pColumns->nId); + assert( pSubExpr!=0 || db->mallocFailed ); + if( pSubExpr==0 ) continue; + pList = sqlite3ExprListAppend(pParse, pList, pSubExpr); + if( pList ){ + assert( pList->nExpr==iFirst+i+1 ); + pList->a[pList->nExpr-1].zEName = pColumns->a[i].zName; + pColumns->a[i].zName = 0; + } + } + + if( !db->mallocFailed && pExpr->op==TK_SELECT && ALWAYS(pList!=0) ){ + Expr *pFirst = pList->a[iFirst].pExpr; + assert( pFirst!=0 ); + assert( pFirst->op==TK_SELECT_COLUMN ); + + /* Store the SELECT statement in pRight so it will be deleted when + ** sqlite3ExprListDelete() is called */ + pFirst->pRight = pExpr; + pExpr = 0; + + /* Remember the size of the LHS in iTable so that we can check that + ** the RHS and LHS sizes match during code generation. */ + pFirst->iTable = pColumns->nId; + } + +vector_append_error: + sqlite3ExprUnmapAndDelete(pParse, pExpr); + sqlite3IdListDelete(db, pColumns); + return pList; } /* ** Set the sort order for the last element on the given ExprList. */ -void sqlite3ExprListSetSortOrder(ExprList *p, int iSortOrder){ +void sqlite3ExprListSetSortOrder(ExprList *p, int iSortOrder, int eNulls){ + struct ExprList_item *pItem; if( p==0 ) return; - assert( SQLITE_SO_UNDEFINED<0 && SQLITE_SO_ASC>=0 && SQLITE_SO_DESC>0 ); assert( p->nExpr>0 ); - if( iSortOrder<0 ){ - assert( p->a[p->nExpr-1].sortOrder==SQLITE_SO_ASC ); - return; + + assert( SQLITE_SO_UNDEFINED<0 && SQLITE_SO_ASC==0 && SQLITE_SO_DESC>0 ); + assert( iSortOrder==SQLITE_SO_UNDEFINED + || iSortOrder==SQLITE_SO_ASC + || iSortOrder==SQLITE_SO_DESC + ); + assert( eNulls==SQLITE_SO_UNDEFINED + || eNulls==SQLITE_SO_ASC + || eNulls==SQLITE_SO_DESC + ); + + pItem = &p->a[p->nExpr-1]; + assert( pItem->fg.bNulls==0 ); + if( iSortOrder==SQLITE_SO_UNDEFINED ){ + iSortOrder = SQLITE_SO_ASC; + } + pItem->fg.sortFlags = (u8)iSortOrder; + + if( eNulls!=SQLITE_SO_UNDEFINED ){ + pItem->fg.bNulls = 1; + if( iSortOrder!=eNulls ){ + pItem->fg.sortFlags |= KEYINFO_ORDER_BIGNULL; + } } - p->a[p->nExpr-1].sortOrder = (u8)iSortOrder; } /* -** Set the ExprList.a[].zName element of the most recently added item +** Set the ExprList.a[].zEName element of the most recently added item ** on the expression list. ** ** pList might be NULL following an OOM error. But pName should never be @@ -1200,17 +2187,27 @@ void sqlite3ExprListSetSortOrder(ExprList *p, int iSortOrder){ void sqlite3ExprListSetName( Parse *pParse, /* Parsing context */ ExprList *pList, /* List to which to add the span. */ - Token *pName, /* Name to be added */ + const Token *pName, /* Name to be added */ int dequote /* True to cause the name to be dequoted */ ){ assert( pList!=0 || pParse->db->mallocFailed!=0 ); + assert( pParse->eParseMode!=PARSE_MODE_UNMAP || dequote==0 ); if( pList ){ struct ExprList_item *pItem; assert( pList->nExpr>0 ); pItem = &pList->a[pList->nExpr-1]; - assert( pItem->zName==0 ); - pItem->zName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n); - if( dequote && pItem->zName ) sqlite3Dequote(pItem->zName); + assert( pItem->zEName==0 ); + assert( pItem->fg.eEName==ENAME_NAME ); + pItem->zEName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n); + if( dequote ){ + /* If dequote==0, then pName->z does not point to part of a DDL + ** statement handled by the parser. And so no token need be added + ** to the token-map. */ + sqlite3Dequote(pItem->zEName); + if( IN_RENAME_OBJECT ){ + sqlite3RenameTokenMap(pParse, (const void*)pItem->zEName, pName); + } + } } } @@ -1225,17 +2222,18 @@ void sqlite3ExprListSetName( void sqlite3ExprListSetSpan( Parse *pParse, /* Parsing context */ ExprList *pList, /* List to which to add the span. */ - ExprSpan *pSpan /* The span to be added */ + const char *zStart, /* Start of the span */ + const char *zEnd /* End of the span */ ){ sqlite3 *db = pParse->db; assert( pList!=0 || db->mallocFailed!=0 ); if( pList ){ struct ExprList_item *pItem = &pList->a[pList->nExpr-1]; assert( pList->nExpr>0 ); - assert( db->mallocFailed || pItem->pExpr==pSpan->pExpr ); - sqlite3DbFree(db, pItem->zSpan); - pItem->zSpan = sqlite3DbStrNDup(db, (char*)pSpan->zStart, - (int)(pSpan->zEnd - pSpan->zStart)); + if( pItem->zEName==0 ){ + pItem->zEName = sqlite3DbSpanDup(db, zStart, zEnd); + pItem->fg.eEName = ENAME_SPAN; + } } } @@ -1259,18 +2257,23 @@ void sqlite3ExprListCheckLength( /* ** Delete an entire expression list. */ -void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){ - int i; - struct ExprList_item *pItem; - if( pList==0 ) return; - assert( pList->a!=0 || pList->nExpr==0 ); - for(pItem=pList->a, i=0; inExpr; i++, pItem++){ +static SQLITE_NOINLINE void exprListDeleteNN(sqlite3 *db, ExprList *pList){ + int i = pList->nExpr; + struct ExprList_item *pItem = pList->a; + assert( pList->nExpr>0 ); + assert( db!=0 ); + do{ sqlite3ExprDelete(db, pItem->pExpr); - sqlite3DbFree(db, pItem->zName); - sqlite3DbFree(db, pItem->zSpan); - } - sqlite3DbFree(db, pList->a); - sqlite3DbFree(db, pList); + if( pItem->zEName ) sqlite3DbNNFreeNN(db, pItem->zEName); + pItem++; + }while( --i>0 ); + sqlite3DbNNFreeNN(db, pList); +} +void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){ + if( pList ) exprListDeleteNN(db, pList); +} +void sqlite3ExprListDeleteGeneric(sqlite3 *db, void *pList){ + if( ALWAYS(pList) ) exprListDeleteNN(db, (ExprList*)pList); } /* @@ -1280,22 +2283,236 @@ void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){ u32 sqlite3ExprListFlags(const ExprList *pList){ int i; u32 m = 0; - if( pList ){ - for(i=0; inExpr; i++){ - Expr *pExpr = pList->a[i].pExpr; - if( ALWAYS(pExpr) ) m |= pExpr->flags; - } + assert( pList!=0 ); + for(i=0; inExpr; i++){ + Expr *pExpr = pList->a[i].pExpr; + assert( pExpr!=0 ); + m |= pExpr->flags; } return m; } /* -** These routines are Walker callbacks used to check expressions to -** see if they are "constant" for some definition of constant. The -** Walker.eCode value determines the type of "constant" we are looking -** for. -** -** These callback routines are used to implement the following: +** This is a SELECT-node callback for the expression walker that +** always "fails". By "fail" in this case, we mean set +** pWalker->eCode to zero and abort. +** +** This callback is used by multiple expression walkers. +*/ +int sqlite3SelectWalkFail(Walker *pWalker, Select *NotUsed){ + UNUSED_PARAMETER(NotUsed); + pWalker->eCode = 0; + return WRC_Abort; +} + +/* +** Check the input string to see if it is "true" or "false" (in any case). +** +** If the string is.... Return +** "true" EP_IsTrue +** "false" EP_IsFalse +** anything else 0 +*/ +u32 sqlite3IsTrueOrFalse(const char *zIn){ + if( sqlite3StrICmp(zIn, "true")==0 ) return EP_IsTrue; + if( sqlite3StrICmp(zIn, "false")==0 ) return EP_IsFalse; + return 0; +} + + +/* +** If the input expression is an ID with the name "true" or "false" +** then convert it into an TK_TRUEFALSE term. Return non-zero if +** the conversion happened, and zero if the expression is unaltered. +*/ +int sqlite3ExprIdToTrueFalse(Expr *pExpr){ + u32 v; + assert( pExpr->op==TK_ID || pExpr->op==TK_STRING ); + if( !ExprHasProperty(pExpr, EP_Quoted|EP_IntValue) + && (v = sqlite3IsTrueOrFalse(pExpr->u.zToken))!=0 + ){ + pExpr->op = TK_TRUEFALSE; + ExprSetProperty(pExpr, v); + return 1; + } + return 0; +} + +/* +** The argument must be a TK_TRUEFALSE Expr node. Return 1 if it is TRUE +** and 0 if it is FALSE. +*/ +int sqlite3ExprTruthValue(const Expr *pExpr){ + pExpr = sqlite3ExprSkipCollateAndLikely((Expr*)pExpr); + assert( pExpr->op==TK_TRUEFALSE ); + assert( !ExprHasProperty(pExpr, EP_IntValue) ); + assert( sqlite3StrICmp(pExpr->u.zToken,"true")==0 + || sqlite3StrICmp(pExpr->u.zToken,"false")==0 ); + return pExpr->u.zToken[4]==0; +} + +/* +** If pExpr is an AND or OR expression, try to simplify it by eliminating +** terms that are always true or false. Return the simplified expression. +** Or return the original expression if no simplification is possible. +** +** Examples: +** +** (x<10) AND true => (x<10) +** (x<10) AND false => false +** (x<10) AND (y=22 OR false) => (x<10) AND (y=22) +** (x<10) AND (y=22 OR true) => (x<10) +** (y=22) OR true => true +*/ +Expr *sqlite3ExprSimplifiedAndOr(Expr *pExpr){ + assert( pExpr!=0 ); + if( pExpr->op==TK_AND || pExpr->op==TK_OR ){ + Expr *pRight = sqlite3ExprSimplifiedAndOr(pExpr->pRight); + Expr *pLeft = sqlite3ExprSimplifiedAndOr(pExpr->pLeft); + if( ExprAlwaysTrue(pLeft) || ExprAlwaysFalse(pRight) ){ + pExpr = pExpr->op==TK_AND ? pRight : pLeft; + }else if( ExprAlwaysTrue(pRight) || ExprAlwaysFalse(pLeft) ){ + pExpr = pExpr->op==TK_AND ? pLeft : pRight; + } + } + return pExpr; +} + +/* +** Return true if it might be advantageous to compute the right operand +** of expression pExpr first, before the left operand. +** +** Normally the left operand is computed before the right operand. But if +** the left operand contains a subquery and the right does not, then it +** might be more efficient to compute the right operand first. +*/ +static int exprEvalRhsFirst(Expr *pExpr){ + if( ExprHasProperty(pExpr->pLeft, EP_Subquery) + && !ExprHasProperty(pExpr->pRight, EP_Subquery) + ){ + return 1; + }else{ + return 0; + } +} + +/* +** Compute the two operands of a binary operator. +** +** If either operand contains a subquery, then the code strives to +** compute the operand containing the subquery second. If the other +** operand evalutes to NULL, then a jump is made. The address of the +** IsNull operand that does this jump is returned. The caller can use +** this to optimize the computation so as to avoid doing the potentially +** expensive subquery. +** +** If no optimization opportunities exist, return 0. +*/ +static int exprComputeOperands( + Parse *pParse, /* Parsing context */ + Expr *pExpr, /* The comparison expression */ + int *pR1, /* OUT: Register holding the left operand */ + int *pR2, /* OUT: Register holding the right operand */ + int *pFree1, /* OUT: Temp register to free if not zero */ + int *pFree2 /* OUT: Another temp register to free if not zero */ +){ + int addrIsNull; + int r1, r2; + Vdbe *v = pParse->pVdbe; + + assert( v!=0 ); + /* + ** If the left operand contains a (possibly expensive) subquery and the + ** right operand does not and the right operation might be NULL, + ** then compute the right operand first and do an IsNull jump if the + ** right operand evalutes to NULL. + */ + if( exprEvalRhsFirst(pExpr) && sqlite3ExprCanBeNull(pExpr->pRight) ){ + r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, pFree2); + addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull, r2); + VdbeComment((v, "skip left operand")); + VdbeCoverage(v); + }else{ + r2 = 0; /* Silence a false-positive uninit-var warning in MSVC */ + addrIsNull = 0; + } + r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, pFree1); + if( addrIsNull==0 ){ + /* + ** If the right operand contains a subquery and the left operand does not + ** and the left operand might be NULL, then do an IsNull check + ** check on the left operand before computing the right operand. + */ + if( ExprHasProperty(pExpr->pRight, EP_Subquery) + && sqlite3ExprCanBeNull(pExpr->pLeft) + ){ + addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull, r1); + VdbeComment((v, "skip right operand")); + VdbeCoverage(v); + } + r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, pFree2); + } + *pR1 = r1; + *pR2 = r2; + return addrIsNull; +} + +/* +** pExpr is a TK_FUNCTION node. Try to determine whether or not the +** function is a constant function. A function is constant if all of +** the following are true: +** +** (1) It is a scalar function (not an aggregate or window function) +** (2) It has either the SQLITE_FUNC_CONSTANT or SQLITE_FUNC_SLOCHNG +** property. +** (3) All of its arguments are constants +** +** This routine sets pWalker->eCode to 0 if pExpr is not a constant. +** It makes no changes to pWalker->eCode if pExpr is constant. In +** every case, it returns WRC_Abort. +** +** Called as a service subroutine from exprNodeIsConstant(). +*/ +static SQLITE_NOINLINE int exprNodeIsConstantFunction( + Walker *pWalker, + Expr *pExpr +){ + int n; /* Number of arguments */ + ExprList *pList; /* List of arguments */ + FuncDef *pDef; /* The function */ + sqlite3 *db; /* The database */ + + assert( pExpr->op==TK_FUNCTION ); + if( ExprHasProperty(pExpr, EP_TokenOnly) + || (pList = pExpr->x.pList)==0 + ){; + n = 0; + }else{ + n = pList->nExpr; + sqlite3WalkExprList(pWalker, pList); + if( pWalker->eCode==0 ) return WRC_Abort; + } + db = pWalker->pParse->db; + pDef = sqlite3FindFunction(db, pExpr->u.zToken, n, ENC(db), 0); + if( pDef==0 + || pDef->xFinalize!=0 + || (pDef->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG))==0 + || ExprHasProperty(pExpr, EP_WinFunc) + ){ + pWalker->eCode = 0; + return WRC_Abort; + } + return WRC_Prune; +} + + +/* +** These routines are Walker callbacks used to check expressions to +** see if they are "constant" for some definition of constant. The +** Walker.eCode value determines the type of "constant" we are looking +** for. +** +** These callback routines are used to implement the following: ** ** sqlite3ExprIsConstant() pWalker->eCode==1 ** sqlite3ExprIsConstantNotJoin() pWalker->eCode==2 @@ -1305,21 +2522,23 @@ u32 sqlite3ExprListFlags(const ExprList *pList){ ** In all cases, the callbacks set Walker.eCode=0 and abort if the expression ** is found to not be a constant. ** -** The sqlite3ExprIsConstantOrFunction() is used for evaluating expressions -** in a CREATE TABLE statement. The Walker.eCode value is 5 when parsing -** an existing schema and 4 when processing a new statement. A bound -** parameter raises an error for new statements, but is silently converted -** to NULL for existing schemas. This allows sqlite_master tables that +** The sqlite3ExprIsConstantOrFunction() is used for evaluating DEFAULT +** expressions in a CREATE TABLE statement. The Walker.eCode value is 5 +** when parsing an existing schema out of the sqlite_schema table and 4 +** when processing a new CREATE TABLE statement. A bound parameter raises +** an error for new statements, but is silently converted +** to NULL for existing schemas. This allows sqlite_schema tables that ** contain a bound parameter because they were generated by older versions ** of SQLite to be parsed by newer versions of SQLite without raising a ** malformed schema error. */ static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){ + assert( pWalker->eCode>0 ); /* If pWalker->eCode is 2 then any term of the expression that comes from - ** the ON or USING clauses of a left join disqualifies the expression + ** the ON or USING clauses of an outer join disqualifies the expression ** from being considered constant. */ - if( pWalker->eCode==2 && ExprHasProperty(pExpr, EP_FromJoin) ){ + if( pWalker->eCode==2 && ExprHasProperty(pExpr, EP_OuterON) ){ pWalker->eCode = 0; return WRC_Abort; } @@ -1329,13 +2548,24 @@ static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){ ** and either pWalker->eCode==4 or 5 or the function has the ** SQLITE_FUNC_CONST flag. */ case TK_FUNCTION: - if( pWalker->eCode>=4 || ExprHasProperty(pExpr,EP_ConstFunc) ){ + if( (pWalker->eCode>=4 || ExprHasProperty(pExpr,EP_ConstFunc)) + && !ExprHasProperty(pExpr, EP_WinFunc) + ){ + if( pWalker->eCode==5 ) ExprSetProperty(pExpr, EP_FromDDL); return WRC_Continue; + }else if( pWalker->pParse ){ + return exprNodeIsConstantFunction(pWalker, pExpr); }else{ pWalker->eCode = 0; return WRC_Abort; } case TK_ID: + /* Convert "true" or "false" in a DEFAULT clause into the + ** appropriate TK_TRUEFALSE operator */ + if( sqlite3ExprIdToTrueFalse(pExpr) ){ + return WRC_Prune; + } + /* no break */ deliberate_fall_through case TK_COLUMN: case TK_AGG_FUNCTION: case TK_AGG_COLUMN: @@ -1343,17 +2573,28 @@ static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){ testcase( pExpr->op==TK_COLUMN ); testcase( pExpr->op==TK_AGG_FUNCTION ); testcase( pExpr->op==TK_AGG_COLUMN ); + if( ExprHasProperty(pExpr, EP_FixedCol) && pWalker->eCode!=2 ){ + return WRC_Continue; + } if( pWalker->eCode==3 && pExpr->iTable==pWalker->u.iCur ){ return WRC_Continue; - }else{ - pWalker->eCode = 0; - return WRC_Abort; } + /* no break */ deliberate_fall_through + case TK_IF_NULL_ROW: + case TK_REGISTER: + case TK_DOT: + case TK_RAISE: + testcase( pExpr->op==TK_REGISTER ); + testcase( pExpr->op==TK_IF_NULL_ROW ); + testcase( pExpr->op==TK_DOT ); + testcase( pExpr->op==TK_RAISE ); + pWalker->eCode = 0; + return WRC_Abort; case TK_VARIABLE: if( pWalker->eCode==5 ){ /* Silently convert bound parameters that appear inside of CREATE ** statements into a NULL when parsing the CREATE statement text out - ** of the sqlite_master table */ + ** of the sqlite_schema table */ pExpr->op = TK_NULL; }else if( pWalker->eCode==4 ){ /* A bound parameter in a CREATE statement that originates from @@ -1361,25 +2602,22 @@ static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){ pWalker->eCode = 0; return WRC_Abort; } - /* Fall through */ + /* no break */ deliberate_fall_through default: - testcase( pExpr->op==TK_SELECT ); /* selectNodeIsConstant will disallow */ - testcase( pExpr->op==TK_EXISTS ); /* selectNodeIsConstant will disallow */ + testcase( pExpr->op==TK_SELECT ); /* sqlite3SelectWalkFail() disallows */ + testcase( pExpr->op==TK_EXISTS ); /* sqlite3SelectWalkFail() disallows */ return WRC_Continue; } } -static int selectNodeIsConstant(Walker *pWalker, Select *NotUsed){ - UNUSED_PARAMETER(NotUsed); - pWalker->eCode = 0; - return WRC_Abort; -} -static int exprIsConst(Expr *p, int initFlag, int iCur){ +static int exprIsConst(Parse *pParse, Expr *p, int initFlag){ Walker w; - memset(&w, 0, sizeof(w)); w.eCode = initFlag; + w.pParse = pParse; w.xExprCallback = exprNodeIsConstant; - w.xSelectCallback = selectNodeIsConstant; - w.u.iCur = iCur; + w.xSelectCallback = sqlite3SelectWalkFail; +#ifdef SQLITE_DEBUG + w.xSelectCallback2 = sqlite3SelectWalkAssert2; +#endif sqlite3WalkExpr(&w, p); return w.eCode; } @@ -1391,19 +2629,48 @@ static int exprIsConst(Expr *p, int initFlag, int iCur){ ** For the purposes of this function, a double-quoted string (ex: "abc") ** is considered a variable but a single-quoted string (ex: 'abc') is ** a constant. +** +** The pParse parameter may be NULL. But if it is NULL, there is no way +** to determine if function calls are constant or not, and hence all +** function calls will be considered to be non-constant. If pParse is +** not NULL, then a function call might be constant, depending on the +** function and on its parameters. */ -int sqlite3ExprIsConstant(Expr *p){ - return exprIsConst(p, 1, 0); +int sqlite3ExprIsConstant(Parse *pParse, Expr *p){ + return exprIsConst(pParse, p, 1); } /* -** Walk an expression tree. Return non-zero if the expression is constant -** that does no originate from the ON or USING clauses of a join. -** Return 0 if it involves variables or function calls or terms from -** an ON or USING clause. +** Walk an expression tree. Return non-zero if +** +** (1) the expression is constant, and +** (2) the expression does originate in the ON or USING clause +** of a LEFT JOIN, and +** (3) the expression does not contain any EP_FixedCol TK_COLUMN +** operands created by the constant propagation optimization. +** +** When this routine returns true, it indicates that the expression +** can be added to the pParse->pConstExpr list and evaluated once when +** the prepared statement starts up. See sqlite3ExprCodeRunJustOnce(). +*/ +static int sqlite3ExprIsConstantNotJoin(Parse *pParse, Expr *p){ + return exprIsConst(pParse, p, 2); +} + +/* +** This routine examines sub-SELECT statements as an expression is being +** walked as part of sqlite3ExprIsTableConstant(). Sub-SELECTs are considered +** constant as long as they are uncorrelated - meaning that they do not +** contain any terms from outer contexts. */ -int sqlite3ExprIsConstantNotJoin(Expr *p){ - return exprIsConst(p, 2, 0); +static int exprSelectWalkTableConstant(Walker *pWalker, Select *pSelect){ + assert( pSelect!=0 ); + assert( pWalker->eCode==3 || pWalker->eCode==0 ); + if( (pSelect->selFlags & SF_Correlated)!=0 ){ + pWalker->eCode = 0; + return WRC_Abort; + } + return WRC_Prune; } /* @@ -1411,15 +2678,180 @@ int sqlite3ExprIsConstantNotJoin(Expr *p){ ** for any single row of the table with cursor iCur. In other words, the ** expression must not refer to any non-deterministic function nor any ** table other than iCur. +** +** Consider uncorrelated subqueries to be constants if the bAllowSubq +** parameter is true. */ -int sqlite3ExprIsTableConstant(Expr *p, int iCur){ - return exprIsConst(p, 3, iCur); +static int sqlite3ExprIsTableConstant(Expr *p, int iCur, int bAllowSubq){ + Walker w; + w.eCode = 3; + w.pParse = 0; + w.xExprCallback = exprNodeIsConstant; + if( bAllowSubq ){ + w.xSelectCallback = exprSelectWalkTableConstant; + }else{ + w.xSelectCallback = sqlite3SelectWalkFail; +#ifdef SQLITE_DEBUG + w.xSelectCallback2 = sqlite3SelectWalkAssert2; +#endif + } + w.u.iCur = iCur; + sqlite3WalkExpr(&w, p); + return w.eCode; } /* -** Walk an expression tree. Return non-zero if the expression is constant -** or a function call with constant arguments. Return and 0 if there -** are any variables. +** Check pExpr to see if it is an constraint on the single data source +** pSrc = &pSrcList->a[iSrc]. In other words, check to see if pExpr +** constrains pSrc but does not depend on any other tables or data +** sources anywhere else in the query. Return true (non-zero) if pExpr +** is a constraint on pSrc only. +** +** This is an optimization. False negatives will perhaps cause slower +** queries, but false positives will yield incorrect answers. So when in +** doubt, return 0. +** +** To be an single-source constraint, the following must be true: +** +** (1) pExpr cannot refer to any table other than pSrc->iCursor. +** +** (2a) pExpr cannot use subqueries unless the bAllowSubq parameter is +** true and the subquery is non-correlated +** +** (2b) pExpr cannot use non-deterministic functions. +** +** (3) pSrc cannot be part of the left operand for a RIGHT JOIN. +** (Is there some way to relax this constraint?) +** +** (4) If pSrc is the right operand of a LEFT JOIN, then... +** (4a) pExpr must come from an ON clause.. +** (4b) and specifically the ON clause associated with the LEFT JOIN. +** +** (5) If pSrc is the right operand of a LEFT JOIN or the left +** operand of a RIGHT JOIN, then pExpr must be from the WHERE +** clause, not an ON clause. +** +** (6) Either: +** +** (6a) pExpr does not originate in an ON or USING clause, or +** +** (6b) The ON or USING clause from which pExpr is derived is +** not to the left of a RIGHT JOIN (or FULL JOIN). +** +** Without this restriction, accepting pExpr as a single-table +** constraint might move the the ON/USING filter expression +** from the left side of a RIGHT JOIN over to the right side, +** which leads to incorrect answers. See also restriction (9) +** on push-down. +*/ +int sqlite3ExprIsSingleTableConstraint( + Expr *pExpr, /* The constraint */ + const SrcList *pSrcList, /* Complete FROM clause */ + int iSrc, /* Which element of pSrcList to use */ + int bAllowSubq /* Allow non-correlated subqueries */ +){ + const SrcItem *pSrc = &pSrcList->a[iSrc]; + if( pSrc->fg.jointype & JT_LTORJ ){ + return 0; /* rule (3) */ + } + if( pSrc->fg.jointype & JT_LEFT ){ + if( !ExprHasProperty(pExpr, EP_OuterON) ) return 0; /* rule (4a) */ + if( pExpr->w.iJoin!=pSrc->iCursor ) return 0; /* rule (4b) */ + }else{ + if( ExprHasProperty(pExpr, EP_OuterON) ) return 0; /* rule (5) */ + } + if( ExprHasProperty(pExpr, EP_OuterON|EP_InnerON) /* (6a) */ + && (pSrcList->a[0].fg.jointype & JT_LTORJ)!=0 /* Fast pre-test of (6b) */ + ){ + int jj; + for(jj=0; jjw.iJoin==pSrcList->a[jj].iCursor ){ + if( (pSrcList->a[jj].fg.jointype & JT_LTORJ)!=0 ){ + return 0; /* restriction (6) */ + } + break; + } + } + } + /* Rules (1), (2a), and (2b) handled by the following: */ + return sqlite3ExprIsTableConstant(pExpr, pSrc->iCursor, bAllowSubq); +} + + +/* +** sqlite3WalkExpr() callback used by sqlite3ExprIsConstantOrGroupBy(). +*/ +static int exprNodeIsConstantOrGroupBy(Walker *pWalker, Expr *pExpr){ + ExprList *pGroupBy = pWalker->u.pGroupBy; + int i; + + /* Check if pExpr is identical to any GROUP BY term. If so, consider + ** it constant. */ + for(i=0; inExpr; i++){ + Expr *p = pGroupBy->a[i].pExpr; + if( sqlite3ExprCompare(0, pExpr, p, -1)<2 ){ + CollSeq *pColl = sqlite3ExprNNCollSeq(pWalker->pParse, p); + if( sqlite3IsBinary(pColl) ){ + return WRC_Prune; + } + } + } + + /* Check if pExpr is a sub-select. If so, consider it variable. */ + if( ExprUseXSelect(pExpr) ){ + pWalker->eCode = 0; + return WRC_Abort; + } + + return exprNodeIsConstant(pWalker, pExpr); +} + +/* +** Walk the expression tree passed as the first argument. Return non-zero +** if the expression consists entirely of constants or copies of terms +** in pGroupBy that sort with the BINARY collation sequence. +** +** This routine is used to determine if a term of the HAVING clause can +** be promoted into the WHERE clause. In order for such a promotion to work, +** the value of the HAVING clause term must be the same for all members of +** a "group". The requirement that the GROUP BY term must be BINARY +** assumes that no other collating sequence will have a finer-grained +** grouping than binary. In other words (A=B COLLATE binary) implies +** A=B in every other collating sequence. The requirement that the +** GROUP BY be BINARY is stricter than necessary. It would also work +** to promote HAVING clauses that use the same alternative collating +** sequence as the GROUP BY term, but that is much harder to check, +** alternative collating sequences are uncommon, and this is only an +** optimization, so we take the easy way out and simply require the +** GROUP BY to use the BINARY collating sequence. +*/ +int sqlite3ExprIsConstantOrGroupBy(Parse *pParse, Expr *p, ExprList *pGroupBy){ + Walker w; + w.eCode = 1; + w.xExprCallback = exprNodeIsConstantOrGroupBy; + w.xSelectCallback = 0; + w.u.pGroupBy = pGroupBy; + w.pParse = pParse; + sqlite3WalkExpr(&w, p); + return w.eCode; +} + +/* +** Walk an expression tree for the DEFAULT field of a column definition +** in a CREATE TABLE statement. Return non-zero if the expression is +** acceptable for use as a DEFAULT. That is to say, return non-zero if +** the expression is constant or a function call with constant arguments. +** Return and 0 if there are any variables. +** +** isInit is true when parsing from sqlite_schema. isInit is false when +** processing a new CREATE TABLE statement. When isInit is true, parameters +** (such as ? or $abc) in the expression are converted into NULL. When +** isInit is false, parameters raise an error. Parameters should not be +** allowed in a CREATE TABLE statement, but some legacy versions of SQLite +** allowed it, so we need to support it when reading sqlite_schema for +** backwards compatibility. +** +** If isInit is true, set EP_FromDDL on every TK_FUNCTION node. ** ** For the purposes of this function, a double-quoted string (ex: "abc") ** is considered a variable but a single-quoted string (ex: 'abc') is @@ -1427,7 +2859,7 @@ int sqlite3ExprIsTableConstant(Expr *p, int iCur){ */ int sqlite3ExprIsConstantOrFunction(Expr *p, u8 isInit){ assert( isInit==0 || isInit==1 ); - return exprIsConst(p, 4+isInit, 0); + return exprIsConst(0, p, 4+isInit); } #ifdef SQLITE_ENABLE_CURSOR_HINTS @@ -1437,10 +2869,12 @@ int sqlite3ExprIsConstantOrFunction(Expr *p, u8 isInit){ */ int sqlite3ExprContainsSubquery(Expr *p){ Walker w; - memset(&w, 0, sizeof(w)); w.eCode = 1; w.xExprCallback = sqlite3ExprWalkNoop; - w.xSelectCallback = selectNodeIsConstant; + w.xSelectCallback = sqlite3SelectWalkFail; +#ifdef SQLITE_DEBUG + w.xSelectCallback2 = sqlite3SelectWalkAssert2; +#endif sqlite3WalkExpr(&w, p); return w.eCode==0; } @@ -1451,9 +2885,14 @@ int sqlite3ExprContainsSubquery(Expr *p){ ** to fit in a 32-bit integer, return 1 and put the value of the integer ** in *pValue. If the expression is not an integer or if it is too big ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged. +** +** If the pParse pointer is provided, then allow the expression p to be +** a parameter (TK_VARIABLE) that is bound to an integer. +** But if pParse is NULL, then p must be a pure integer literal. */ -int sqlite3ExprIsInteger(Expr *p, int *pValue){ +int sqlite3ExprIsInteger(const Expr *p, int *pValue, Parse *pParse){ int rc = 0; + if( NEVER(p==0) ) return 0; /* Used to only happen following on OOM */ /* If an expression is an integer literal that fits in a signed 32-bit ** integer, then the EP_IntValue flag will have already been set */ @@ -1466,18 +2905,38 @@ int sqlite3ExprIsInteger(Expr *p, int *pValue){ } switch( p->op ){ case TK_UPLUS: { - rc = sqlite3ExprIsInteger(p->pLeft, pValue); + rc = sqlite3ExprIsInteger(p->pLeft, pValue, 0); break; } case TK_UMINUS: { - int v; - if( sqlite3ExprIsInteger(p->pLeft, &v) ){ - assert( v!=(-2147483647-1) ); + int v = 0; + if( sqlite3ExprIsInteger(p->pLeft, &v, 0) ){ + assert( ((unsigned int)v)!=0x80000000 ); *pValue = -v; rc = 1; } break; } + case TK_VARIABLE: { + sqlite3_value *pVal; + if( pParse==0 ) break; + if( NEVER(pParse->pVdbe==0) ) break; + if( (pParse->db->flags & SQLITE_EnableQPSG)!=0 ) break; + sqlite3VdbeSetVarmask(pParse->pVdbe, p->iColumn); + pVal = sqlite3VdbeGetBoundValue(pParse->pReprepare, p->iColumn, + SQLITE_AFF_BLOB); + if( pVal ){ + if( sqlite3_value_type(pVal)==SQLITE_INTEGER ){ + sqlite3_int64 vv = sqlite3_value_int64(pVal); + if( vv == (vv & 0x7fffffff) ){ /* non-negative numbers only */ + *pValue = (int)vv; + rc = 1; + } + } + sqlite3ValueFree(pVal); + } + break; + } default: break; } return rc; @@ -1487,7 +2946,7 @@ int sqlite3ExprIsInteger(Expr *p, int *pValue){ ** Return FALSE if there is no chance that the expression can be NULL. ** ** If the expression might be NULL or if the expression is too complex -** to tell return TRUE. +** to tell return TRUE. ** ** This routine is used as an optimization, to skip OP_IsNull opcodes ** when we know that a value cannot be NULL. Hence, a false positive @@ -1499,7 +2958,11 @@ int sqlite3ExprIsInteger(Expr *p, int *pValue){ */ int sqlite3ExprCanBeNull(const Expr *p){ u8 op; - while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; } + assert( p!=0 ); + while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ + p = p->pLeft; + assert( p!=0 ); + } op = p->op; if( op==TK_REGISTER ) op = p->op2; switch( op ){ @@ -1509,9 +2972,16 @@ int sqlite3ExprCanBeNull(const Expr *p){ case TK_BLOB: return 0; case TK_COLUMN: - assert( p->pTab!=0 ); - return ExprHasProperty(p, EP_CanBeNull) || - (p->iColumn>=0 && p->pTab->aCol[p->iColumn].notNull==0); + assert( ExprUseYTab(p) ); + return ExprHasProperty(p, EP_CanBeNull) + || NEVER(p->y.pTab==0) /* Reference to column of index on expr */ +#ifdef SQLITE_ALLOW_ROWID_IN_VIEW + || (p->iColumn==XN_ROWID && IsView(p->y.pTab)) +#endif + || (p->iColumn>=0 + && p->y.pTab->aCol!=0 /* Possible due to prior error */ + && ALWAYS(p->iColumny.pTab->nCol) + && p->y.pTab->aCol[p->iColumn].notNull==0); default: return 1; } @@ -1529,27 +2999,30 @@ int sqlite3ExprCanBeNull(const Expr *p){ */ int sqlite3ExprNeedsNoAffinityChange(const Expr *p, char aff){ u8 op; + int unaryMinus = 0; if( aff==SQLITE_AFF_BLOB ) return 1; - while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; } + while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ + if( p->op==TK_UMINUS ) unaryMinus = 1; + p = p->pLeft; + } op = p->op; if( op==TK_REGISTER ) op = p->op2; switch( op ){ case TK_INTEGER: { - return aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC; + return aff>=SQLITE_AFF_NUMERIC; } case TK_FLOAT: { - return aff==SQLITE_AFF_REAL || aff==SQLITE_AFF_NUMERIC; + return aff>=SQLITE_AFF_NUMERIC; } case TK_STRING: { - return aff==SQLITE_AFF_TEXT; + return !unaryMinus && aff==SQLITE_AFF_TEXT; } case TK_BLOB: { - return 1; + return !unaryMinus; } case TK_COLUMN: { assert( p->iTable>=0 ); /* p cannot be part of a CHECK constraint */ - return p->iColumn<0 - && (aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC); + return aff>=SQLITE_AFF_NUMERIC && p->iColumn<0; } default: { return 0; @@ -1568,23 +3041,37 @@ int sqlite3IsRowid(const char *z){ } /* -** Return true if we are able to the IN operator optimization on a -** query of the form -** -** x IN (SELECT ...) -** -** Where the SELECT... clause is as specified by the parameter to this -** routine. -** -** The Select object passed in has already been preprocessed and no -** errors have been found. +** Return a pointer to a buffer containing a usable rowid alias for table +** pTab. An alias is usable if there is not an explicit user-defined column +** of the same name. +*/ +const char *sqlite3RowidAlias(Table *pTab){ + const char *azOpt[] = {"_ROWID_", "ROWID", "OID"}; + int ii; + assert( VisibleRowid(pTab) ); + for(ii=0; iix.pSelect; if( p->pPrior ) return 0; /* Not a compound SELECT */ if( p->selFlags & (SF_Distinct|SF_Aggregate) ){ testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct ); @@ -1593,32 +3080,28 @@ static int isCandidateForInOpt(Select *p){ } assert( p->pGroupBy==0 ); /* Has no GROUP BY clause */ if( p->pLimit ) return 0; /* Has no LIMIT clause */ - assert( p->pOffset==0 ); /* No LIMIT means no OFFSET */ if( p->pWhere ) return 0; /* Has no WHERE clause */ pSrc = p->pSrc; assert( pSrc!=0 ); if( pSrc->nSrc!=1 ) return 0; /* Single term in FROM clause */ - if( pSrc->a[0].pSelect ) return 0; /* FROM is not a subquery or view */ - pTab = pSrc->a[0].pTab; - if( NEVER(pTab==0) ) return 0; - assert( pTab->pSelect==0 ); /* FROM clause is not a view */ + if( pSrc->a[0].fg.isSubquery) return 0;/* FROM is not a subquery or view */ + pTab = pSrc->a[0].pSTab; + assert( pTab!=0 ); + assert( !IsView(pTab) ); /* FROM clause is not a view */ if( IsVirtual(pTab) ) return 0; /* FROM clause not a virtual table */ pEList = p->pEList; - if( pEList->nExpr!=1 ) return 0; /* One column in the result set */ - if( pEList->a[0].pExpr->op!=TK_COLUMN ) return 0; /* Result is a column */ - return 1; + assert( pEList!=0 ); + /* All SELECT results must be columns. */ + for(i=0; inExpr; i++){ + Expr *pRes = pEList->a[i].pExpr; + if( pRes->op!=TK_COLUMN ) return 0; + assert( pRes->iTable==pSrc->a[0].iCursor ); /* Not a correlated subquery */ + } + return p; } #endif /* SQLITE_OMIT_SUBQUERY */ -/* -** Code an OP_Once instruction and allocate space for its flag. Return the -** address of the new instruction. -*/ -int sqlite3CodeOnce(Parse *pParse){ - Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */ - return sqlite3VdbeAddOp1(v, OP_Once, pParse->nOnce++); -} - +#ifndef SQLITE_OMIT_SUBQUERY /* ** Generate code that checks the left-most column of index table iCur to see if ** it contains any NULL entries. Cause the register at regHasNull to be set @@ -1634,20 +3117,21 @@ static void sqlite3SetHasNullFlag(Vdbe *v, int iCur, int regHasNull){ VdbeComment((v, "first_entry_in(%d)", iCur)); sqlite3VdbeJumpHere(v, addr1); } +#endif #ifndef SQLITE_OMIT_SUBQUERY /* -** The argument is an IN operator with a list (not a subquery) on the +** The argument is an IN operator with a list (not a subquery) on the ** right-hand side. Return TRUE if that list is constant. */ -static int sqlite3InRhsIsConstant(Expr *pIn){ +static int sqlite3InRhsIsConstant(Parse *pParse, Expr *pIn){ Expr *pLHS; int res; assert( !ExprHasProperty(pIn, EP_xIsSelect) ); pLHS = pIn->pLeft; pIn->pLeft = 0; - res = sqlite3ExprIsConstant(pIn); + res = sqlite3ExprIsConstant(pParse, pIn); pIn->pLeft = pLHS; return res; } @@ -1663,7 +3147,7 @@ static int sqlite3InRhsIsConstant(Expr *pIn){ ** all members of the RHS set, skipping duplicates. ** ** A cursor is opened on the b-tree object that is the RHS of the IN operator -** and pX->iTable is set to the index of that cursor. +** and the *piTab parameter is set to the index of that cursor. ** ** The returned value of this function indicates the b-tree type, as follows: ** @@ -1671,37 +3155,39 @@ static int sqlite3InRhsIsConstant(Expr *pIn){ ** IN_INDEX_INDEX_ASC - The cursor was opened on an ascending index. ** IN_INDEX_INDEX_DESC - The cursor was opened on a descending index. ** IN_INDEX_EPH - The cursor was opened on a specially created and -** populated epheremal table. +** populated ephemeral table. ** IN_INDEX_NOOP - No cursor was allocated. The IN operator must be ** implemented as a sequence of comparisons. ** ** An existing b-tree might be used if the RHS expression pX is a simple ** subquery such as: ** -** SELECT FROM
    +** SELECT , ... FROM
    ** ** If the RHS of the IN operator is a list or a more complex subquery, then ** an ephemeral table might need to be generated from the RHS and then ** pX->iTable made to point to the ephemeral table instead of an -** existing table. +** existing table. In this case, the creation and initialization of the +** ephemeral table might be put inside of a subroutine, the EP_Subrtn flag +** will be set on pX and the pX->y.sub fields will be set to show where +** the subroutine is coded. ** -** The inFlags parameter must contain exactly one of the bits -** IN_INDEX_MEMBERSHIP or IN_INDEX_LOOP. If inFlags contains -** IN_INDEX_MEMBERSHIP, then the generated table will be used for a -** fast membership test. When the IN_INDEX_LOOP bit is set, the -** IN index will be used to loop over all values of the RHS of the -** IN operator. +** The inFlags parameter must contain, at a minimum, one of the bits +** IN_INDEX_MEMBERSHIP or IN_INDEX_LOOP but not both. If inFlags contains +** IN_INDEX_MEMBERSHIP, then the generated table will be used for a fast +** membership test. When the IN_INDEX_LOOP bit is set, the IN index will +** be used to loop over all values of the RHS of the IN operator. ** ** When IN_INDEX_LOOP is used (and the b-tree will be used to iterate ** through the set members) then the b-tree must not contain duplicates. -** An epheremal table must be used unless the selected is guaranteed -** to be unique - either because it is an INTEGER PRIMARY KEY or it -** has a UNIQUE constraint or UNIQUE index. +** An ephemeral table will be created unless the selected columns are guaranteed +** to be unique - either because it is an INTEGER PRIMARY KEY or due to +** a UNIQUE constraint or index. ** -** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used -** for fast set membership tests) then an epheremal table must -** be used unless is an INTEGER PRIMARY KEY or an index can -** be found with as its left-most column. +** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used +** for fast set membership tests) then an ephemeral table must +** be used unless is a single INTEGER PRIMARY KEY column or an +** index can be found with the specified as its left-most. ** ** If the IN_INDEX_NOOP_OK and IN_INDEX_MEMBERSHIP are both set and ** if the RHS of the IN operator is a list (not a subquery) then this @@ -1712,7 +3198,7 @@ static int sqlite3InRhsIsConstant(Expr *pIn){ ** ** When the b-tree is being used for membership tests, the calling function ** might need to know whether or not the RHS side of the IN operator -** contains a NULL. If prRhsHasNull is not a NULL pointer and +** contains a NULL. If prRhsHasNull is not a NULL pointer and ** if there is any chance that the (...) might contain a NULL value at ** runtime, then a register is allocated and the register number written ** to *prRhsHasNull. If there is no chance that the (...) contains a @@ -1722,107 +3208,205 @@ static int sqlite3InRhsIsConstant(Expr *pIn){ ** the value in that register will be NULL if the b-tree contains one or more ** NULL values, and it will be some non-NULL value if the b-tree contains no ** NULL values. +** +** If the aiMap parameter is not NULL, it must point to an array containing +** one element for each column returned by the SELECT statement on the RHS +** of the IN(...) operator. The i'th entry of the array is populated with the +** offset of the index column that matches the i'th column returned by the +** SELECT. For example, if the expression and selected index are: +** +** (?,?,?) IN (SELECT a, b, c FROM t1) +** CREATE INDEX i1 ON t1(b, c, a); +** +** then aiMap[] is populated with {2, 0, 1}. */ #ifndef SQLITE_OMIT_SUBQUERY -int sqlite3FindInIndex(Parse *pParse, Expr *pX, u32 inFlags, int *prRhsHasNull){ +int sqlite3FindInIndex( + Parse *pParse, /* Parsing context */ + Expr *pX, /* The IN expression */ + u32 inFlags, /* IN_INDEX_LOOP, _MEMBERSHIP, and/or _NOOP_OK */ + int *prRhsHasNull, /* Register holding NULL status. See notes */ + int *aiMap, /* Mapping from Index fields to RHS fields */ + int *piTab /* OUT: index to use */ +){ Select *p; /* SELECT to the right of IN operator */ int eType = 0; /* Type of RHS table. IN_INDEX_* */ - int iTab = pParse->nTab++; /* Cursor of the RHS table */ + int iTab; /* Cursor of the RHS table */ int mustBeUnique; /* True if RHS must be unique */ Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */ assert( pX->op==TK_IN ); mustBeUnique = (inFlags & IN_INDEX_LOOP)!=0; + iTab = pParse->nTab++; + + /* If the RHS of this IN(...) operator is a SELECT, and if it matters + ** whether or not the SELECT result contains NULL values, check whether + ** or not NULL is actually possible (it may not be, for example, due + ** to NOT NULL constraints in the schema). If no NULL values are possible, + ** set prRhsHasNull to 0 before continuing. */ + if( prRhsHasNull && ExprUseXSelect(pX) ){ + int i; + ExprList *pEList = pX->x.pSelect->pEList; + for(i=0; inExpr; i++){ + if( sqlite3ExprCanBeNull(pEList->a[i].pExpr) ) break; + } + if( i==pEList->nExpr ){ + prRhsHasNull = 0; + } + } /* Check to see if an existing table or index can be used to - ** satisfy the query. This is preferable to generating a new - ** ephemeral table. - */ - p = (ExprHasProperty(pX, EP_xIsSelect) ? pX->x.pSelect : 0); - if( pParse->nErr==0 && isCandidateForInOpt(p) ){ + ** satisfy the query. This is preferable to generating a new + ** ephemeral table. */ + if( pParse->nErr==0 && (p = isCandidateForInOpt(pX))!=0 ){ sqlite3 *db = pParse->db; /* Database connection */ Table *pTab; /* Table
    . */ - Expr *pExpr; /* Expression */ - i16 iCol; /* Index of column */ - i16 iDb; /* Database idx for pTab */ + int iDb; /* Database idx for pTab */ + ExprList *pEList = p->pEList; + int nExpr = pEList->nExpr; - assert( p ); /* Because of isCandidateForInOpt(p) */ assert( p->pEList!=0 ); /* Because of isCandidateForInOpt(p) */ assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */ assert( p->pSrc!=0 ); /* Because of isCandidateForInOpt(p) */ - pTab = p->pSrc->a[0].pTab; - pExpr = p->pEList->a[0].pExpr; - iCol = (i16)pExpr->iColumn; - + pTab = p->pSrc->a[0].pSTab; + /* Code an OP_Transaction and OP_TableLock for
    . */ iDb = sqlite3SchemaToIndex(db, pTab->pSchema); + assert( iDb>=0 && iDbtnum, 0, pTab->zName); - /* This function is only called from two places. In both cases the vdbe - ** has already been allocated. So assume sqlite3GetVdbe() is always - ** successful here. - */ - assert(v); - if( iCol<0 ){ - int iAddr = sqlite3CodeOnce(pParse); + assert(v); /* sqlite3GetVdbe() has always been previously called */ + if( nExpr==1 && pEList->a[0].pExpr->iColumn<0 ){ + /* The "x IN (SELECT rowid FROM table)" case */ + int iAddr = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v); sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead); eType = IN_INDEX_ROWID; - + ExplainQueryPlan((pParse, 0, + "USING ROWID SEARCH ON TABLE %s FOR IN-OPERATOR",pTab->zName)); sqlite3VdbeJumpHere(v, iAddr); }else{ Index *pIdx; /* Iterator variable */ + int affinity_ok = 1; + int i; - /* The collation sequence used by the comparison. If an index is to - ** be used in place of a temp-table, it must be ordered according - ** to this collation sequence. */ - CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pExpr); - - /* Check that the affinity that will be used to perform the - ** comparison is the same as the affinity of the column. If - ** it is not, it is not possible to use any index. - */ - int affinity_ok = sqlite3IndexAffinityOk(pX, pTab->aCol[iCol].affinity); - - for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){ - if( (pIdx->aiColumn[0]==iCol) - && sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], 0)==pReq - && (!mustBeUnique || (pIdx->nKeyCol==1 && IsUniqueIndex(pIdx))) - ){ - int iAddr = sqlite3CodeOnce(pParse); VdbeCoverage(v); - sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb); - sqlite3VdbeSetP4KeyInfo(pParse, pIdx); - VdbeComment((v, "%s", pIdx->zName)); - assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 ); - eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0]; - - if( prRhsHasNull && !pTab->aCol[iCol].notNull ){ - *prRhsHasNull = ++pParse->nMem; - sqlite3SetHasNullFlag(v, iTab, *prRhsHasNull); - } - sqlite3VdbeJumpHere(v, iAddr); + /* Check that the affinity that will be used to perform each + ** comparison is the same as the affinity of each column in table + ** on the RHS of the IN operator. If it not, it is not possible to + ** use any index of the RHS table. */ + for(i=0; ipLeft, i); + int iCol = pEList->a[i].pExpr->iColumn; + char idxaff = sqlite3TableColumnAffinity(pTab,iCol); /* RHS table */ + char cmpaff = sqlite3CompareAffinity(pLhs, idxaff); + testcase( cmpaff==SQLITE_AFF_BLOB ); + testcase( cmpaff==SQLITE_AFF_TEXT ); + switch( cmpaff ){ + case SQLITE_AFF_BLOB: + break; + case SQLITE_AFF_TEXT: + /* sqlite3CompareAffinity() only returns TEXT if one side or the + ** other has no affinity and the other side is TEXT. Hence, + ** the only way for cmpaff to be TEXT is for idxaff to be TEXT + ** and for the term on the LHS of the IN to have no affinity. */ + assert( idxaff==SQLITE_AFF_TEXT ); + break; + default: + affinity_ok = sqlite3IsNumericAffinity(idxaff); } } - } - } + + if( affinity_ok ){ + /* Search for an existing index that will work for this IN operator */ + for(pIdx=pTab->pIndex; pIdx && eType==0; pIdx=pIdx->pNext){ + Bitmask colUsed; /* Columns of the index used */ + Bitmask mCol; /* Mask for the current column */ + if( pIdx->nColumnpPartIdxWhere!=0 ) continue; + /* Maximum nColumn is BMS-2, not BMS-1, so that we can compute + ** BITMASK(nExpr) without overflowing */ + testcase( pIdx->nColumn==BMS-2 ); + testcase( pIdx->nColumn==BMS-1 ); + if( pIdx->nColumn>=BMS-1 ) continue; + if( mustBeUnique ){ + if( pIdx->nKeyCol>nExpr + ||(pIdx->nColumn>nExpr && !IsUniqueIndex(pIdx)) + ){ + continue; /* This index is not unique over the IN RHS columns */ + } + } + + colUsed = 0; /* Columns of index used so far */ + for(i=0; ipLeft, i); + Expr *pRhs = pEList->a[i].pExpr; + CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pLhs, pRhs); + int j; + + for(j=0; jaiColumn[j]!=pRhs->iColumn ) continue; + assert( pIdx->azColl[j] ); + if( pReq!=0 && sqlite3StrICmp(pReq->zName, pIdx->azColl[j])!=0 ){ + continue; + } + break; + } + if( j==nExpr ) break; + mCol = MASKBIT(j); + if( mCol & colUsed ) break; /* Each column used only once */ + colUsed |= mCol; + if( aiMap ) aiMap[i] = j; + } + + assert( nExpr>0 && nExprzName)); + sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb); + sqlite3VdbeSetP4KeyInfo(pParse, pIdx); + VdbeComment((v, "%s", pIdx->zName)); + assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 ); + eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0]; + + if( prRhsHasNull ){ +#ifdef SQLITE_ENABLE_COLUMN_USED_MASK + i64 mask = (1<nMem; + if( nExpr==1 ){ + sqlite3SetHasNullFlag(v, iTab, *prRhsHasNull); + } + } + sqlite3VdbeJumpHere(v, iAddr); + } + } /* End loop over indexes */ + } /* End if( affinity_ok ) */ + } /* End if not an rowid index */ + } /* End attempt to optimize using an index */ /* If no preexisting index is available for the IN clause ** and IN_INDEX_NOOP is an allowed reply ** and the RHS of the IN operator is a list, not a subquery - ** and the RHS is not contant or has two or fewer terms, + ** and the RHS is not constant or has two or fewer terms, ** then it is not worth creating an ephemeral table to evaluate ** the IN operator so return IN_INDEX_NOOP. */ if( eType==0 && (inFlags & IN_INDEX_NOOP_OK) - && !ExprHasProperty(pX, EP_xIsSelect) - && (!sqlite3InRhsIsConstant(pX) || pX->x.pList->nExpr<=2) + && ExprUseXList(pX) + && (!sqlite3InRhsIsConstant(pParse,pX) || pX->x.pList->nExpr<=2) ){ + pParse->nTab--; /* Back out the allocation of the unused cursor */ + iTab = -1; /* Cursor is not allocated */ eType = IN_INDEX_NOOP; } - if( eType==0 ){ /* Could not find an existing table or index to use as the RHS b-tree. @@ -1833,261 +3417,572 @@ int sqlite3FindInIndex(Parse *pParse, Expr *pX, u32 inFlags, int *prRhsHasNull){ eType = IN_INDEX_EPH; if( inFlags & IN_INDEX_LOOP ){ pParse->nQueryLoop = 0; - if( pX->pLeft->iColumn<0 && !ExprHasProperty(pX, EP_xIsSelect) ){ - eType = IN_INDEX_ROWID; - } }else if( prRhsHasNull ){ *prRhsHasNull = rMayHaveNull = ++pParse->nMem; } - sqlite3CodeSubselect(pParse, pX, rMayHaveNull, eType==IN_INDEX_ROWID); + assert( pX->op==TK_IN ); + sqlite3CodeRhsOfIN(pParse, pX, iTab); + if( rMayHaveNull ){ + sqlite3SetHasNullFlag(v, iTab, rMayHaveNull); + } pParse->nQueryLoop = savedNQueryLoop; - }else{ - pX->iTable = iTab; } + + if( aiMap && eType!=IN_INDEX_INDEX_ASC && eType!=IN_INDEX_INDEX_DESC ){ + int i, n; + n = sqlite3ExprVectorSize(pX->pLeft); + for(i=0; ipLeft; + int nVal = sqlite3ExprVectorSize(pLeft); + Select *pSelect = ExprUseXSelect(pExpr) ? pExpr->x.pSelect : 0; + char *zRet; + + assert( pExpr->op==TK_IN ); + zRet = sqlite3DbMallocRaw(pParse->db, 1+(i64)nVal); + if( zRet ){ + int i; + for(i=0; ipEList->a[i].pExpr, a); + }else{ + zRet[i] = a; + } + } + zRet[nVal] = '\0'; + } + return zRet; +} +#endif + +#ifndef SQLITE_OMIT_SUBQUERY +/* +** Load the Parse object passed as the first argument with an error +** message of the form: ** -** The pExpr parameter describes the expression that contains the IN -** operator or subquery. +** "sub-select returns N columns - expected M" +*/ +void sqlite3SubselectError(Parse *pParse, int nActual, int nExpect){ + if( pParse->nErr==0 ){ + const char *zFmt = "sub-select returns %d columns - expected %d"; + sqlite3ErrorMsg(pParse, zFmt, nActual, nExpect); + } +} +#endif + +/* +** Expression pExpr is a vector that has been used in a context where +** it is not permitted. If pExpr is a sub-select vector, this routine +** loads the Parse object with a message of the form: ** -** If parameter isRowid is non-zero, then expression pExpr is guaranteed -** to be of the form " IN (?, ?, ?)", where is a reference -** to some integer key column of a table B-Tree. In this case, use an -** intkey B-Tree to store the set of IN(...) values instead of the usual -** (slower) variable length keys B-Tree. +** "sub-select returns N columns - expected 1" ** -** If rMayHaveNull is non-zero, that means that the operation is an IN -** (not a SELECT or EXISTS) and that the RHS might contains NULLs. -** All this routine does is initialize the register given by rMayHaveNull -** to NULL. Calling routines will take care of changing this register -** value to non-NULL if the RHS is NULL-free. +** Or, if it is a regular scalar vector: ** -** For a SELECT or EXISTS operator, return the register that holds the -** result. For IN operators or if an error occurs, the return value is 0. +** "row value misused" +*/ +void sqlite3VectorErrorMsg(Parse *pParse, Expr *pExpr){ +#ifndef SQLITE_OMIT_SUBQUERY + if( ExprUseXSelect(pExpr) ){ + sqlite3SubselectError(pParse, pExpr->x.pSelect->pEList->nExpr, 1); + }else +#endif + { + sqlite3ErrorMsg(pParse, "row value misused"); + } +} + +#ifndef SQLITE_OMIT_SUBQUERY +/* +** Scan all previously generated bytecode looking for an OP_BeginSubrtn +** that is compatible with pExpr. If found, add the y.sub values +** to pExpr and return true. If not found, return false. */ +static int findCompatibleInRhsSubrtn( + Parse *pParse, /* Parsing context */ + Expr *pExpr, /* IN operator with RHS that we want to reuse */ + SubrtnSig *pNewSig /* Signature for the IN operator */ +){ + VdbeOp *pOp, *pEnd; + SubrtnSig *pSig; + Vdbe *v; + + if( pNewSig==0 ) return 0; + if( (pParse->mSubrtnSig & (1<<(pNewSig->selId&7)))==0 ) return 0; + assert( pExpr->op==TK_IN ); + assert( !ExprUseYSub(pExpr) ); + assert( ExprUseXSelect(pExpr) ); + assert( pExpr->x.pSelect!=0 ); + assert( (pExpr->x.pSelect->selFlags & SF_All)==0 ); + v = pParse->pVdbe; + assert( v!=0 ); + pOp = sqlite3VdbeGetOp(v, 1); + pEnd = sqlite3VdbeGetLastOp(v); + for(; pOpp4type!=P4_SUBRTNSIG ) continue; + assert( pOp->opcode==OP_BeginSubrtn ); + pSig = pOp->p4.pSubrtnSig; + assert( pSig!=0 ); + if( !pSig->bComplete ) continue; + if( pNewSig->selId!=pSig->selId ) continue; + if( strcmp(pNewSig->zAff,pSig->zAff)!=0 ) continue; + pExpr->y.sub.iAddr = pSig->iAddr; + pExpr->y.sub.regReturn = pSig->regReturn; + pExpr->iTable = pSig->iTable; + ExprSetProperty(pExpr, EP_Subrtn); + return 1; + } + return 0; +} +#endif /* SQLITE_OMIT_SUBQUERY */ + #ifndef SQLITE_OMIT_SUBQUERY -int sqlite3CodeSubselect( +/* +** Generate code that will construct an ephemeral table containing all terms +** in the RHS of an IN operator. The IN operator can be in either of two +** forms: +** +** x IN (4,5,11) -- IN operator with list on right-hand side +** x IN (SELECT a FROM b) -- IN operator with subquery on the right +** +** The pExpr parameter is the IN operator. The cursor number for the +** constructed ephemeral table is returned. The first time the ephemeral +** table is computed, the cursor number is also stored in pExpr->iTable, +** however the cursor number returned might not be the same, as it might +** have been duplicated using OP_OpenDup. +** +** If the LHS expression ("x" in the examples) is a column value, or +** the SELECT statement returns a column value, then the affinity of that +** column is used to build the index keys. If both 'x' and the +** SELECT... statement are columns, then numeric affinity is used +** if either column has NUMERIC or INTEGER affinity. If neither +** 'x' nor the SELECT... statement are columns, then numeric affinity +** is used. +*/ +void sqlite3CodeRhsOfIN( Parse *pParse, /* Parsing context */ - Expr *pExpr, /* The IN, SELECT, or EXISTS operator */ - int rHasNullFlag, /* Register that records whether NULLs exist in RHS */ - int isRowid /* If true, LHS of IN operator is a rowid */ + Expr *pExpr, /* The IN operator */ + int iTab /* Use this cursor number */ ){ - int jmpIfDynamic = -1; /* One-time test address */ - int rReg = 0; /* Register storing resulting */ - Vdbe *v = sqlite3GetVdbe(pParse); - if( NEVER(v==0) ) return 0; - sqlite3ExprCachePush(pParse); - - /* This code must be run in its entirety every time it is encountered - ** if any of the following is true: + int addrOnce = 0; /* Address of the OP_Once instruction at top */ + int addr; /* Address of OP_OpenEphemeral instruction */ + Expr *pLeft; /* the LHS of the IN operator */ + KeyInfo *pKeyInfo = 0; /* Key information */ + int nVal; /* Size of vector pLeft */ + Vdbe *v; /* The prepared statement under construction */ + SubrtnSig *pSig = 0; /* Signature for this subroutine */ + + v = pParse->pVdbe; + assert( v!=0 ); + + /* The evaluation of the IN must be repeated every time it + ** is encountered if any of the following is true: ** ** * The right-hand side is a correlated subquery ** * The right-hand side is an expression list containing variables ** * We are inside a trigger ** - ** If all of the above are false, then we can run this code just once - ** save the results, and reuse the same result on subsequent invocations. + ** If all of the above are false, then we can compute the RHS just once + ** and reuse it many names. */ - if( !ExprHasProperty(pExpr, EP_VarSelect) ){ - jmpIfDynamic = sqlite3CodeOnce(pParse); VdbeCoverage(v); - } - -#ifndef SQLITE_OMIT_EXPLAIN - if( pParse->explain==2 ){ - char *zMsg = sqlite3MPrintf(pParse->db, "EXECUTE %s%s SUBQUERY %d", - jmpIfDynamic>=0?"":"CORRELATED ", - pExpr->op==TK_IN?"LIST":"SCALAR", - pParse->iNextSelectId - ); - sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC); - } -#endif - - switch( pExpr->op ){ - case TK_IN: { - char affinity; /* Affinity of the LHS of the IN */ - int addr; /* Address of OP_OpenEphemeral instruction */ - Expr *pLeft = pExpr->pLeft; /* the LHS of the IN operator */ - KeyInfo *pKeyInfo = 0; /* Key information */ + if( !ExprHasProperty(pExpr, EP_VarSelect) && pParse->iSelfTab==0 ){ + /* Reuse of the RHS is allowed + ** + ** Compute a signature for the RHS of the IN operator to facility + ** finding and reusing prior instances of the same IN operator. + */ + assert( !ExprUseXSelect(pExpr) || pExpr->x.pSelect!=0 ); + if( ExprUseXSelect(pExpr) && (pExpr->x.pSelect->selFlags & SF_All)==0 ){ + pSig = sqlite3DbMallocRawNN(pParse->db, sizeof(pSig[0])); + if( pSig ){ + pSig->selId = pExpr->x.pSelect->selId; + pSig->zAff = exprINAffinity(pParse, pExpr); + } + } - affinity = sqlite3ExprAffinity(pLeft); + /* Check to see if there is a prior materialization of the RHS of + ** this IN operator. If there is, then make use of that prior + ** materialization rather than recomputing it. + */ + if( ExprHasProperty(pExpr, EP_Subrtn) + || findCompatibleInRhsSubrtn(pParse, pExpr, pSig) + ){ + addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v); + if( ExprUseXSelect(pExpr) ){ + ExplainQueryPlan((pParse, 0, "REUSE LIST SUBQUERY %d", + pExpr->x.pSelect->selId)); + } + assert( ExprUseYSub(pExpr) ); + sqlite3VdbeAddOp2(v, OP_Gosub, pExpr->y.sub.regReturn, + pExpr->y.sub.iAddr); + assert( iTab!=pExpr->iTable ); + sqlite3VdbeAddOp2(v, OP_OpenDup, iTab, pExpr->iTable); + sqlite3VdbeJumpHere(v, addrOnce); + if( pSig ){ + sqlite3DbFree(pParse->db, pSig->zAff); + sqlite3DbFree(pParse->db, pSig); + } + return; + } - /* Whether this is an 'x IN(SELECT...)' or an 'x IN()' - ** expression it is handled the same way. An ephemeral table is - ** filled with single-field index keys representing the results - ** from the SELECT or the . - ** - ** If the 'x' expression is a column value, or the SELECT... - ** statement returns a column value, then the affinity of that - ** column is used to build the index keys. If both 'x' and the - ** SELECT... statement are columns, then numeric affinity is used - ** if either column has NUMERIC or INTEGER affinity. If neither - ** 'x' nor the SELECT... statement are columns, then numeric affinity - ** is used. - */ - pExpr->iTable = pParse->nTab++; - addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, !isRowid); - pKeyInfo = isRowid ? 0 : sqlite3KeyInfoAlloc(pParse->db, 1, 1); - - if( ExprHasProperty(pExpr, EP_xIsSelect) ){ - /* Case 1: expr IN (SELECT ...) - ** - ** Generate code to write the results of the select into the temporary - ** table allocated and opened above. - */ - Select *pSelect = pExpr->x.pSelect; - SelectDest dest; - ExprList *pEList; - - assert( !isRowid ); - sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable); - dest.affSdst = (u8)affinity; - assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable ); - pSelect->iLimit = 0; - testcase( pSelect->selFlags & SF_Distinct ); - testcase( pKeyInfo==0 ); /* Caused by OOM in sqlite3KeyInfoAlloc() */ - if( sqlite3Select(pParse, pSelect, &dest) ){ - sqlite3KeyInfoUnref(pKeyInfo); - return 0; - } - pEList = pSelect->pEList; - assert( pKeyInfo!=0 ); /* OOM will cause exit after sqlite3Select() */ - assert( pEList!=0 ); - assert( pEList->nExpr>0 ); - assert( sqlite3KeyInfoIsWriteable(pKeyInfo) ); - pKeyInfo->aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, - pEList->a[0].pExpr); - }else if( ALWAYS(pExpr->x.pList!=0) ){ - /* Case 2: expr IN (exprlist) - ** - ** For each expression, build an index key from the evaluation and - ** store it in the temporary table. If is a column, then use - ** that columns affinity when building index keys. If is not - ** a column, use numeric affinity. - */ - int i; - ExprList *pList = pExpr->x.pList; - struct ExprList_item *pItem; - int r1, r2, r3; + /* Begin coding the subroutine */ + assert( !ExprUseYWin(pExpr) ); + ExprSetProperty(pExpr, EP_Subrtn); + assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) ); + pExpr->y.sub.regReturn = ++pParse->nMem; + pExpr->y.sub.iAddr = + sqlite3VdbeAddOp2(v, OP_BeginSubrtn, 0, pExpr->y.sub.regReturn) + 1; + if( pSig ){ + pSig->bComplete = 0; + pSig->iAddr = pExpr->y.sub.iAddr; + pSig->regReturn = pExpr->y.sub.regReturn; + pSig->iTable = iTab; + pParse->mSubrtnSig = 1 << (pSig->selId&7); + sqlite3VdbeChangeP4(v, -1, (const char*)pSig, P4_SUBRTNSIG); + } + addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v); + } - if( !affinity ){ - affinity = SQLITE_AFF_BLOB; - } - if( pKeyInfo ){ - assert( sqlite3KeyInfoIsWriteable(pKeyInfo) ); - pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft); - } + /* Check to see if this is a vector IN operator */ + pLeft = pExpr->pLeft; + nVal = sqlite3ExprVectorSize(pLeft); - /* Loop through each expression in . */ - r1 = sqlite3GetTempReg(pParse); - r2 = sqlite3GetTempReg(pParse); - if( isRowid ) sqlite3VdbeAddOp2(v, OP_Null, 0, r2); - for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){ - Expr *pE2 = pItem->pExpr; - int iValToIns; - - /* If the expression is not constant then we will need to - ** disable the test that was generated above that makes sure - ** this code only executes once. Because for a non-constant - ** expression we need to rerun this code each time. - */ - if( jmpIfDynamic>=0 && !sqlite3ExprIsConstant(pE2) ){ - sqlite3VdbeChangeToNoop(v, jmpIfDynamic); - jmpIfDynamic = -1; - } + /* Construct the ephemeral table that will contain the content of + ** RHS of the IN operator. + */ + pExpr->iTable = iTab; + addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, nVal); +#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS + if( ExprUseXSelect(pExpr) ){ + VdbeComment((v, "Result of SELECT %u", pExpr->x.pSelect->selId)); + }else{ + VdbeComment((v, "RHS of IN operator")); + } +#endif + pKeyInfo = sqlite3KeyInfoAlloc(pParse->db, nVal, 1); - /* Evaluate the expression and insert it into the temp table */ - if( isRowid && sqlite3ExprIsInteger(pE2, &iValToIns) ){ - sqlite3VdbeAddOp3(v, OP_InsertInt, pExpr->iTable, r2, iValToIns); - }else{ - r3 = sqlite3ExprCodeTarget(pParse, pE2, r1); - if( isRowid ){ - sqlite3VdbeAddOp2(v, OP_MustBeInt, r3, - sqlite3VdbeCurrentAddr(v)+2); - VdbeCoverage(v); - sqlite3VdbeAddOp3(v, OP_Insert, pExpr->iTable, r2, r3); - }else{ - sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1); - sqlite3ExprCacheAffinityChange(pParse, r3, 1); - sqlite3VdbeAddOp2(v, OP_IdxInsert, pExpr->iTable, r2); - } - } + if( ExprUseXSelect(pExpr) ){ + /* Case 1: expr IN (SELECT ...) + ** + ** Generate code to write the results of the select into the temporary + ** table allocated and opened above. + */ + Select *pSelect = pExpr->x.pSelect; + ExprList *pEList = pSelect->pEList; + + ExplainQueryPlan((pParse, 1, "%sLIST SUBQUERY %d", + addrOnce?"":"CORRELATED ", pSelect->selId + )); + /* If the LHS and RHS of the IN operator do not match, that + ** error will have been caught long before we reach this point. */ + if( ALWAYS(pEList->nExpr==nVal) ){ + Select *pCopy; + SelectDest dest; + int i; + int rc; + int addrBloom = 0; + sqlite3SelectDestInit(&dest, SRT_Set, iTab); + dest.zAffSdst = exprINAffinity(pParse, pExpr); + pSelect->iLimit = 0; + if( addrOnce && OptimizationEnabled(pParse->db, SQLITE_BloomFilter) ){ + int regBloom = ++pParse->nMem; + addrBloom = sqlite3VdbeAddOp2(v, OP_Blob, 10000, regBloom); + VdbeComment((v, "Bloom filter")); + dest.iSDParm2 = regBloom; + } + testcase( pSelect->selFlags & SF_Distinct ); + testcase( pKeyInfo==0 ); /* Caused by OOM in sqlite3KeyInfoAlloc() */ + pCopy = sqlite3SelectDup(pParse->db, pSelect, 0); + rc = pParse->db->mallocFailed ? 1 :sqlite3Select(pParse, pCopy, &dest); + sqlite3SelectDelete(pParse->db, pCopy); + sqlite3DbFree(pParse->db, dest.zAffSdst); + if( addrBloom ){ + /* Remember that location of the Bloom filter in the P3 operand + ** of the OP_Once that began this subroutine. tag-202407032019 */ + sqlite3VdbeGetOp(v, addrOnce)->p3 = dest.iSDParm2; + if( dest.iSDParm2==0 ){ + /* If the Bloom filter won't actually be used, keep it small */ + sqlite3VdbeGetOp(v, addrBloom)->p1 = 10; } - sqlite3ReleaseTempReg(pParse, r1); - sqlite3ReleaseTempReg(pParse, r2); } - if( pKeyInfo ){ - sqlite3VdbeChangeP4(v, addr, (void *)pKeyInfo, P4_KEYINFO); + if( rc ){ + sqlite3KeyInfoUnref(pKeyInfo); + return; + } + assert( pKeyInfo!=0 ); /* OOM will cause exit after sqlite3Select() */ + assert( pEList!=0 ); + assert( pEList->nExpr>0 ); + assert( sqlite3KeyInfoIsWriteable(pKeyInfo) ); + for(i=0; iaColl[i] = sqlite3BinaryCompareCollSeq( + pParse, p, pEList->a[i].pExpr + ); } - break; + } + }else if( ALWAYS(pExpr->x.pList!=0) ){ + /* Case 2: expr IN (exprlist) + ** + ** For each expression, build an index key from the evaluation and + ** store it in the temporary table. If is a column, then use + ** that columns affinity when building index keys. If is not + ** a column, use numeric affinity. + */ + char affinity; /* Affinity of the LHS of the IN */ + int i; + ExprList *pList = pExpr->x.pList; + struct ExprList_item *pItem; + int r1, r2; + affinity = sqlite3ExprAffinity(pLeft); + if( affinity<=SQLITE_AFF_NONE ){ + affinity = SQLITE_AFF_BLOB; + }else if( affinity==SQLITE_AFF_REAL ){ + affinity = SQLITE_AFF_NUMERIC; + } + if( pKeyInfo ){ + assert( sqlite3KeyInfoIsWriteable(pKeyInfo) ); + pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft); } - case TK_EXISTS: - case TK_SELECT: - default: { - /* If this has to be a scalar SELECT. Generate code to put the - ** value of this select in a memory cell and record the number - ** of the memory cell in iColumn. If this is an EXISTS, write - ** an integer 0 (not exists) or 1 (exists) into a memory cell - ** and record that memory cell in iColumn. + /* Loop through each expression in . */ + r1 = sqlite3GetTempReg(pParse); + r2 = sqlite3GetTempReg(pParse); + for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){ + Expr *pE2 = pItem->pExpr; + + /* If the expression is not constant then we will need to + ** disable the test that was generated above that makes sure + ** this code only executes once. Because for a non-constant + ** expression we need to rerun this code each time. */ - Select *pSel; /* SELECT statement to encode */ - SelectDest dest; /* How to deal with SELECt result */ - - testcase( pExpr->op==TK_EXISTS ); - testcase( pExpr->op==TK_SELECT ); - assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT ); - - assert( ExprHasProperty(pExpr, EP_xIsSelect) ); - pSel = pExpr->x.pSelect; - sqlite3SelectDestInit(&dest, 0, ++pParse->nMem); - if( pExpr->op==TK_SELECT ){ - dest.eDest = SRT_Mem; - dest.iSdst = dest.iSDParm; - sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iSDParm); - VdbeComment((v, "Init subquery result")); - }else{ - dest.eDest = SRT_Exists; - sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iSDParm); - VdbeComment((v, "Init EXISTS result")); + if( addrOnce && !sqlite3ExprIsConstant(pParse, pE2) ){ + sqlite3VdbeChangeToNoop(v, addrOnce-1); + sqlite3VdbeChangeToNoop(v, addrOnce); + ExprClearProperty(pExpr, EP_Subrtn); + addrOnce = 0; } - sqlite3ExprDelete(pParse->db, pSel->pLimit); - pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, - &sqlite3IntTokens[1]); - pSel->iLimit = 0; - pSel->selFlags &= ~SF_MultiValue; - if( sqlite3Select(pParse, pSel, &dest) ){ - return 0; - } - rReg = dest.iSDParm; - ExprSetVVAProperty(pExpr, EP_NoReduce); - break; + + /* Evaluate the expression and insert it into the temp table */ + sqlite3ExprCode(pParse, pE2, r1); + sqlite3VdbeAddOp4(v, OP_MakeRecord, r1, 1, r2, &affinity, 1); + sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iTab, r2, r1, 1); } + sqlite3ReleaseTempReg(pParse, r1); + sqlite3ReleaseTempReg(pParse, r2); } - - if( rHasNullFlag ){ - sqlite3SetHasNullFlag(v, pExpr->iTable, rHasNullFlag); + if( pSig ) pSig->bComplete = 1; + if( pKeyInfo ){ + sqlite3VdbeChangeP4(v, addr, (void *)pKeyInfo, P4_KEYINFO); } + if( addrOnce ){ + sqlite3VdbeAddOp1(v, OP_NullRow, iTab); + sqlite3VdbeJumpHere(v, addrOnce); + /* Subroutine return */ + assert( ExprUseYSub(pExpr) ); + assert( sqlite3VdbeGetOp(v,pExpr->y.sub.iAddr-1)->opcode==OP_BeginSubrtn + || pParse->nErr ); + sqlite3VdbeAddOp3(v, OP_Return, pExpr->y.sub.regReturn, + pExpr->y.sub.iAddr, 1); + VdbeCoverage(v); + sqlite3ClearTempRegCache(pParse); + } +} +#endif /* SQLITE_OMIT_SUBQUERY */ + +/* +** Generate code for scalar subqueries used as a subquery expression +** or EXISTS operator: +** +** (SELECT a FROM b) -- subquery +** EXISTS (SELECT a FROM b) -- EXISTS subquery +** +** The pExpr parameter is the SELECT or EXISTS operator to be coded. +** +** Return the register that holds the result. For a multi-column SELECT, +** the result is stored in a contiguous array of registers and the +** return value is the register of the left-most result column. +** Return 0 if an error occurs. +*/ +#ifndef SQLITE_OMIT_SUBQUERY +int sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){ + int addrOnce = 0; /* Address of OP_Once at top of subroutine */ + int rReg = 0; /* Register storing resulting */ + Select *pSel; /* SELECT statement to encode */ + SelectDest dest; /* How to deal with SELECT result */ + int nReg; /* Registers to allocate */ + Expr *pLimit; /* New limit expression */ +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS + int addrExplain; /* Address of OP_Explain instruction */ +#endif - if( jmpIfDynamic>=0 ){ - sqlite3VdbeJumpHere(v, jmpIfDynamic); + Vdbe *v = pParse->pVdbe; + assert( v!=0 ); + if( pParse->nErr ) return 0; + testcase( pExpr->op==TK_EXISTS ); + testcase( pExpr->op==TK_SELECT ); + assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT ); + assert( ExprUseXSelect(pExpr) ); + pSel = pExpr->x.pSelect; + + /* If this routine has already been coded, then invoke it as a + ** subroutine. */ + if( ExprHasProperty(pExpr, EP_Subrtn) ){ + ExplainQueryPlan((pParse, 0, "REUSE SUBQUERY %d", pSel->selId)); + assert( ExprUseYSub(pExpr) ); + sqlite3VdbeAddOp2(v, OP_Gosub, pExpr->y.sub.regReturn, + pExpr->y.sub.iAddr); + return pExpr->iTable; } - sqlite3ExprCachePop(pParse); + /* Begin coding the subroutine */ + assert( !ExprUseYWin(pExpr) ); + assert( !ExprHasProperty(pExpr, EP_Reduced|EP_TokenOnly) ); + ExprSetProperty(pExpr, EP_Subrtn); + pExpr->y.sub.regReturn = ++pParse->nMem; + pExpr->y.sub.iAddr = + sqlite3VdbeAddOp2(v, OP_BeginSubrtn, 0, pExpr->y.sub.regReturn) + 1; + + /* The evaluation of the EXISTS/SELECT must be repeated every time it + ** is encountered if any of the following is true: + ** + ** * The right-hand side is a correlated subquery + ** * The right-hand side is an expression list containing variables + ** * We are inside a trigger + ** + ** If all of the above are false, then we can run this code just once + ** save the results, and reuse the same result on subsequent invocations. + */ + if( !ExprHasProperty(pExpr, EP_VarSelect) ){ + addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v); + } + + /* For a SELECT, generate code to put the values for all columns of + ** the first row into an array of registers and return the index of + ** the first register. + ** + ** If this is an EXISTS, write an integer 0 (not exists) or 1 (exists) + ** into a register and return that register number. + ** + ** In both cases, the query is augmented with "LIMIT 1". Any + ** preexisting limit is discarded in place of the new LIMIT 1. + */ + ExplainQueryPlan2(addrExplain, (pParse, 1, "%sSCALAR SUBQUERY %d", + addrOnce?"":"CORRELATED ", pSel->selId)); + sqlite3VdbeScanStatusCounters(v, addrExplain, addrExplain, -1); + nReg = pExpr->op==TK_SELECT ? pSel->pEList->nExpr : 1; + sqlite3SelectDestInit(&dest, 0, pParse->nMem+1); + pParse->nMem += nReg; + if( pExpr->op==TK_SELECT ){ + dest.eDest = SRT_Mem; + if( (pSel->selFlags&SF_Distinct) && pSel->pLimit && pSel->pLimit->pRight ){ + /* If there is both a DISTINCT and an OFFSET clause, then allocate + ** a separate dest.iSdst array for sqlite3Select() and other + ** routines to populate. In this case results will be copied over + ** into the dest.iSDParm array only after OFFSET processing. This + ** ensures that in the case where OFFSET excludes all rows, the + ** dest.iSDParm array is not left populated with the contents of the + ** last row visited - it should be all NULLs if all rows were + ** excluded by OFFSET. */ + dest.iSdst = pParse->nMem+1; + pParse->nMem += nReg; + }else{ + dest.iSdst = dest.iSDParm; + } + dest.nSdst = nReg; + sqlite3VdbeAddOp3(v, OP_Null, 0, dest.iSDParm, pParse->nMem); + VdbeComment((v, "Init subquery result")); + }else{ + dest.eDest = SRT_Exists; + sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iSDParm); + VdbeComment((v, "Init EXISTS result")); + } + if( pSel->pLimit ){ + /* The subquery already has a limit. If the pre-existing limit X is + ** not already integer value 1 or 0, then make the new limit X<>0 so that + ** the new limit is either 1 or 0 */ + Expr *pLeft = pSel->pLimit->pLeft; + if( ExprHasProperty(pLeft, EP_IntValue)==0 + || (pLeft->u.iValue!=1 && pLeft->u.iValue!=0) + ){ + sqlite3 *db = pParse->db; + pLimit = sqlite3Expr(db, TK_INTEGER, "0"); + if( pLimit ){ + pLimit->affExpr = SQLITE_AFF_NUMERIC; + pLimit = sqlite3PExpr(pParse, TK_NE, + sqlite3ExprDup(db, pLeft, 0), pLimit); + } + sqlite3ExprDeferredDelete(pParse, pLeft); + pSel->pLimit->pLeft = pLimit; + } + }else{ + /* If there is no pre-existing limit add a limit of 1 */ + pLimit = sqlite3Expr(pParse->db, TK_INTEGER, "1"); + pSel->pLimit = sqlite3PExpr(pParse, TK_LIMIT, pLimit, 0); + } + pSel->iLimit = 0; + if( sqlite3Select(pParse, pSel, &dest) ){ + pExpr->op2 = pExpr->op; + pExpr->op = TK_ERROR; + return 0; + } + pExpr->iTable = rReg = dest.iSDParm; + ExprSetVVAProperty(pExpr, EP_NoReduce); + if( addrOnce ){ + sqlite3VdbeJumpHere(v, addrOnce); + } + sqlite3VdbeScanStatusRange(v, addrExplain, addrExplain, -1); + + /* Subroutine return */ + assert( ExprUseYSub(pExpr) ); + assert( sqlite3VdbeGetOp(v,pExpr->y.sub.iAddr-1)->opcode==OP_BeginSubrtn + || pParse->nErr ); + sqlite3VdbeAddOp3(v, OP_Return, pExpr->y.sub.regReturn, + pExpr->y.sub.iAddr, 1); + VdbeCoverage(v); + sqlite3ClearTempRegCache(pParse); return rReg; } #endif /* SQLITE_OMIT_SUBQUERY */ +#ifndef SQLITE_OMIT_SUBQUERY +/* +** Expr pIn is an IN(...) expression. This function checks that the +** sub-select on the RHS of the IN() operator has the same number of +** columns as the vector on the LHS. Or, if the RHS of the IN() is not +** a sub-query, that the LHS is a vector of size 1. +*/ +int sqlite3ExprCheckIN(Parse *pParse, Expr *pIn){ + int nVector = sqlite3ExprVectorSize(pIn->pLeft); + if( ExprUseXSelect(pIn) && !pParse->db->mallocFailed ){ + if( nVector!=pIn->x.pSelect->pEList->nExpr ){ + sqlite3SubselectError(pParse, pIn->x.pSelect->pEList->nExpr, nVector); + return 1; + } + }else if( nVector!=1 ){ + sqlite3VectorErrorMsg(pParse, pIn->pLeft); + return 1; + } + return 0; +} +#endif + #ifndef SQLITE_OMIT_SUBQUERY /* ** Generate code for an IN expression. @@ -2095,16 +3990,24 @@ int sqlite3CodeSubselect( ** x IN (SELECT ...) ** x IN (value, value, ...) ** -** The left-hand side (LHS) is a scalar expression. The right-hand side (RHS) -** is an array of zero or more values. The expression is true if the LHS is -** contained within the RHS. The value of the expression is unknown (NULL) -** if the LHS is NULL or if the LHS is not contained within the RHS and the -** RHS contains one or more NULL values. +** The left-hand side (LHS) is a scalar or vector expression. The +** right-hand side (RHS) is an array of zero or more scalar values, or a +** subquery. If the RHS is a subquery, the number of result columns must +** match the number of columns in the vector on the LHS. If the RHS is +** a list of values, the LHS must be a scalar. +** +** The IN operator is true if the LHS value is contained within the RHS. +** The result is false if the LHS is definitely not in the RHS. The +** result is NULL if the presence of the LHS in the RHS cannot be +** determined due to NULLs. ** -** This routine generates code that jumps to destIfFalse if the LHS is not +** This routine generates code that jumps to destIfFalse if the LHS is not ** contained within the RHS. If due to NULLs we cannot determine if the LHS ** is contained in the RHS then jump to destIfNull. If the LHS is contained ** within the RHS then fall through. +** +** See the separate in-operator.md documentation file in the canonical +** SQLite source tree for additional information. */ static void sqlite3ExprCodeIN( Parse *pParse, /* Parsing and code generating context */ @@ -2113,67 +4016,118 @@ static void sqlite3ExprCodeIN( int destIfNull /* Jump here if the results are unknown due to NULLs */ ){ int rRhsHasNull = 0; /* Register that is true if RHS contains NULL values */ - char affinity; /* Comparison affinity to use */ int eType; /* Type of the RHS */ - int r1; /* Temporary use register */ + int rLhs; /* Register(s) holding the LHS values */ Vdbe *v; /* Statement under construction */ - - /* Compute the RHS. After this step, the table with cursor - ** pExpr->iTable will contains the values that make up the RHS. - */ + int *aiMap = 0; /* Map from vector field to index column */ + char *zAff = 0; /* Affinity string for comparisons */ + int nVector; /* Size of vectors for this IN operator */ + int iDummy; /* Dummy parameter to exprCodeVector() */ + Expr *pLeft; /* The LHS of the IN operator */ + int i; /* loop counter */ + int destStep2; /* Where to jump when NULLs seen in step 2 */ + int destStep6 = 0; /* Start of code for Step 6 */ + int addrTruthOp; /* Address of opcode that determines the IN is true */ + int destNotNull; /* Jump here if a comparison is not true in step 6 */ + int addrTop; /* Top of the step-6 loop */ + int iTab = 0; /* Index to use */ + u8 okConstFactor = pParse->okConstFactor; + + assert( !ExprHasVVAProperty(pExpr,EP_Immutable) ); + pLeft = pExpr->pLeft; + if( sqlite3ExprCheckIN(pParse, pExpr) ) return; + zAff = exprINAffinity(pParse, pExpr); + nVector = sqlite3ExprVectorSize(pExpr->pLeft); + aiMap = (int*)sqlite3DbMallocZero(pParse->db, nVector*sizeof(int)); + if( pParse->db->mallocFailed ) goto sqlite3ExprCodeIN_oom_error; + + /* Attempt to compute the RHS. After this step, if anything other than + ** IN_INDEX_NOOP is returned, the table opened with cursor iTab + ** contains the values that make up the RHS. If IN_INDEX_NOOP is returned, + ** the RHS has not yet been coded. */ v = pParse->pVdbe; assert( v!=0 ); /* OOM detected prior to this routine */ VdbeNoopComment((v, "begin IN expr")); eType = sqlite3FindInIndex(pParse, pExpr, IN_INDEX_MEMBERSHIP | IN_INDEX_NOOP_OK, - destIfFalse==destIfNull ? 0 : &rRhsHasNull); + destIfFalse==destIfNull ? 0 : &rRhsHasNull, + aiMap, &iTab); - /* Figure out the affinity to use to create a key from the results - ** of the expression. affinityStr stores a static string suitable for - ** P4 of OP_MakeRecord. - */ - affinity = comparisonAffinity(pExpr); + assert( pParse->nErr || nVector==1 || eType==IN_INDEX_EPH + || eType==IN_INDEX_INDEX_ASC || eType==IN_INDEX_INDEX_DESC + ); +#ifdef SQLITE_DEBUG + /* Confirm that aiMap[] contains nVector integer values between 0 and + ** nVector-1. */ + for(i=0; i from " IN (...)". - */ - sqlite3ExprCachePush(pParse); - r1 = sqlite3GetTempReg(pParse); - sqlite3ExprCode(pParse, pExpr->pLeft, r1); + /* Code the LHS, the from " IN (...)". If the LHS is a + ** vector, then it is stored in an array of nVector registers starting + ** at r1. + ** + ** sqlite3FindInIndex() might have reordered the fields of the LHS vector + ** so that the fields are in the same order as an existing index. The + ** aiMap[] array contains a mapping from the original LHS field order to + ** the field order that matches the RHS index. + ** + ** Avoid factoring the LHS of the IN(...) expression out of the loop, + ** even if it is constant, as OP_Affinity may be used on the register + ** by code generated below. */ + assert( pParse->okConstFactor==okConstFactor ); + pParse->okConstFactor = 0; + rLhs = exprCodeVector(pParse, pLeft, &iDummy); + pParse->okConstFactor = okConstFactor; /* If sqlite3FindInIndex() did not find or create an index that is ** suitable for evaluating the IN operator, then evaluate using a ** sequence of comparisons. + ** + ** This is step (1) in the in-operator.md optimized algorithm. */ if( eType==IN_INDEX_NOOP ){ - ExprList *pList = pExpr->x.pList; - CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft); - int labelOk = sqlite3VdbeMakeLabel(v); + ExprList *pList; + CollSeq *pColl; + int labelOk = sqlite3VdbeMakeLabel(pParse); int r2, regToFree; int regCkNull = 0; int ii; - assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); + assert( nVector==1 ); + assert( ExprUseXList(pExpr) ); + pList = pExpr->x.pList; + pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft); if( destIfNull!=destIfFalse ){ regCkNull = sqlite3GetTempReg(pParse); - sqlite3VdbeAddOp3(v, OP_BitAnd, r1, r1, regCkNull); + sqlite3VdbeAddOp3(v, OP_BitAnd, rLhs, rLhs, regCkNull); } for(ii=0; iinExpr; ii++){ r2 = sqlite3ExprCodeTemp(pParse, pList->a[ii].pExpr, ®ToFree); if( regCkNull && sqlite3ExprCanBeNull(pList->a[ii].pExpr) ){ sqlite3VdbeAddOp3(v, OP_BitAnd, regCkNull, r2, regCkNull); } + sqlite3ReleaseTempReg(pParse, regToFree); if( iinExpr-1 || destIfNull!=destIfFalse ){ - sqlite3VdbeAddOp4(v, OP_Eq, r1, labelOk, r2, + int op = rLhs!=r2 ? OP_Eq : OP_NotNull; + sqlite3VdbeAddOp4(v, op, rLhs, labelOk, r2, (void*)pColl, P4_COLLSEQ); - VdbeCoverageIf(v, iinExpr-1); - VdbeCoverageIf(v, ii==pList->nExpr-1); - sqlite3VdbeChangeP5(v, affinity); + VdbeCoverageIf(v, iinExpr-1 && op==OP_Eq); + VdbeCoverageIf(v, ii==pList->nExpr-1 && op==OP_Eq); + VdbeCoverageIf(v, iinExpr-1 && op==OP_NotNull); + VdbeCoverageIf(v, ii==pList->nExpr-1 && op==OP_NotNull); + sqlite3VdbeChangeP5(v, zAff[0]); }else{ + int op = rLhs!=r2 ? OP_Ne : OP_IsNull; assert( destIfNull==destIfFalse ); - sqlite3VdbeAddOp4(v, OP_Ne, r1, destIfFalse, r2, - (void*)pColl, P4_COLLSEQ); VdbeCoverage(v); - sqlite3VdbeChangeP5(v, affinity | SQLITE_JUMPIFNULL); + sqlite3VdbeAddOp4(v, op, rLhs, destIfFalse, r2, + (void*)pColl, P4_COLLSEQ); + VdbeCoverageIf(v, op==OP_Ne); + VdbeCoverageIf(v, op==OP_IsNull); + sqlite3VdbeChangeP5(v, zAff[0] | SQLITE_JUMPIFNULL); } - sqlite3ReleaseTempReg(pParse, regToFree); } if( regCkNull ){ sqlite3VdbeAddOp2(v, OP_IsNull, regCkNull, destIfNull); VdbeCoverage(v); @@ -2181,78 +4135,141 @@ static void sqlite3ExprCodeIN( } sqlite3VdbeResolveLabel(v, labelOk); sqlite3ReleaseTempReg(pParse, regCkNull); - }else{ - - /* If the LHS is NULL, then the result is either false or NULL depending - ** on whether the RHS is empty or not, respectively. + goto sqlite3ExprCodeIN_finished; + } + + if( eType!=IN_INDEX_ROWID ){ + /* If this IN operator will use an index, then the order of columns in the + ** vector might be different from the order in the index. In that case, + ** we need to reorder the LHS values to be in index order. Run Affinity + ** before reordering the columns, so that the affinity is correct. */ - if( sqlite3ExprCanBeNull(pExpr->pLeft) ){ - if( destIfNull==destIfFalse ){ - /* Shortcut for the common case where the false and NULL outcomes are - ** the same. */ - sqlite3VdbeAddOp2(v, OP_IsNull, r1, destIfNull); VdbeCoverage(v); - }else{ - int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, r1); VdbeCoverage(v); - sqlite3VdbeAddOp2(v, OP_Rewind, pExpr->iTable, destIfFalse); - VdbeCoverage(v); - sqlite3VdbeGoto(v, destIfNull); - sqlite3VdbeJumpHere(v, addr1); + sqlite3VdbeAddOp4(v, OP_Affinity, rLhs, nVector, 0, zAff, nVector); + for(i=0; iiTable, destIfFalse, r1); + } + + + /* Step 2: Check to see if the LHS contains any NULL columns. If the + ** LHS does contain NULLs then the result must be either FALSE or NULL. + ** We will then skip the binary search of the RHS. + */ + if( destIfNull==destIfFalse ){ + destStep2 = destIfFalse; + }else{ + destStep2 = destStep6 = sqlite3VdbeMakeLabel(pParse); + } + for(i=0; ipLeft, i); + if( pParse->nErr ) goto sqlite3ExprCodeIN_oom_error; + if( sqlite3ExprCanBeNull(p) ){ + sqlite3VdbeAddOp2(v, OP_IsNull, rLhs+i, destStep2); VdbeCoverage(v); - }else{ - /* In this case, the RHS is an index b-tree. - */ - sqlite3VdbeAddOp4(v, OP_Affinity, r1, 1, 0, &affinity, 1); - - /* If the set membership test fails, then the result of the - ** "x IN (...)" expression must be either 0 or NULL. If the set - ** contains no NULL values, then the result is 0. If the set - ** contains one or more NULL values, then the result of the - ** expression is also NULL. - */ - assert( destIfFalse!=destIfNull || rRhsHasNull==0 ); - if( rRhsHasNull==0 ){ - /* This branch runs if it is known at compile time that the RHS - ** cannot contain NULL values. This happens as the result - ** of a "NOT NULL" constraint in the database schema. - ** - ** Also run this branch if NULL is equivalent to FALSE - ** for this particular IN operator. - */ - sqlite3VdbeAddOp4Int(v, OP_NotFound, pExpr->iTable, destIfFalse, r1, 1); - VdbeCoverage(v); - }else{ - /* In this branch, the RHS of the IN might contain a NULL and - ** the presence of a NULL on the RHS makes a difference in the - ** outcome. - */ - int addr1; - - /* First check to see if the LHS is contained in the RHS. If so, - ** then the answer is TRUE the presence of NULLs in the RHS does - ** not matter. If the LHS is not contained in the RHS, then the - ** answer is NULL if the RHS contains NULLs and the answer is - ** FALSE if the RHS is NULL-free. - */ - addr1 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, r1, 1); - VdbeCoverage(v); - sqlite3VdbeAddOp2(v, OP_IsNull, rRhsHasNull, destIfNull); - VdbeCoverage(v); - sqlite3VdbeGoto(v, destIfFalse); - sqlite3VdbeJumpHere(v, addr1); + } + } + + /* Step 3. The LHS is now known to be non-NULL. Do the binary search + ** of the RHS using the LHS as a probe. If found, the result is + ** true. + */ + if( eType==IN_INDEX_ROWID ){ + /* In this case, the RHS is the ROWID of table b-tree and so we also + ** know that the RHS is non-NULL. Hence, we combine steps 3 and 4 + ** into a single opcode. */ + assert( nVector==1 ); + sqlite3VdbeAddOp3(v, OP_SeekRowid, iTab, destIfFalse, rLhs); + VdbeCoverage(v); + addrTruthOp = sqlite3VdbeAddOp0(v, OP_Goto); /* Return True */ + }else{ + if( destIfFalse==destIfNull ){ + /* Combine Step 3 and Step 5 into a single opcode */ + if( ExprHasProperty(pExpr, EP_Subrtn) ){ + const VdbeOp *pOp = sqlite3VdbeGetOp(v, pExpr->y.sub.iAddr); + assert( pOp->opcode==OP_Once || pParse->nErr ); + if( pOp->opcode==OP_Once && pOp->p3>0 ){ /* tag-202407032019 */ + assert( OptimizationEnabled(pParse->db, SQLITE_BloomFilter) ); + sqlite3VdbeAddOp4Int(v, OP_Filter, pOp->p3, destIfFalse, + rLhs, nVector); VdbeCoverage(v); + } } + sqlite3VdbeAddOp4Int(v, OP_NotFound, iTab, destIfFalse, + rLhs, nVector); VdbeCoverage(v); + goto sqlite3ExprCodeIN_finished; } + /* Ordinary Step 3, for the case where FALSE and NULL are distinct */ + addrTruthOp = sqlite3VdbeAddOp4Int(v, OP_Found, iTab, 0, + rLhs, nVector); VdbeCoverage(v); + } + + /* Step 4. If the RHS is known to be non-NULL and we did not find + ** an match on the search above, then the result must be FALSE. + */ + if( rRhsHasNull && nVector==1 ){ + sqlite3VdbeAddOp2(v, OP_NotNull, rRhsHasNull, destIfFalse); + VdbeCoverage(v); + } + + /* Step 5. If we do not care about the difference between NULL and + ** FALSE, then just return false. + */ + if( destIfFalse==destIfNull ) sqlite3VdbeGoto(v, destIfFalse); + + /* Step 6: Loop through rows of the RHS. Compare each row to the LHS. + ** If any comparison is NULL, then the result is NULL. If all + ** comparisons are FALSE then the final result is FALSE. + ** + ** For a scalar LHS, it is sufficient to check just the first row + ** of the RHS. + */ + if( destStep6 ) sqlite3VdbeResolveLabel(v, destStep6); + addrTop = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, destIfFalse); + VdbeCoverage(v); + if( nVector>1 ){ + destNotNull = sqlite3VdbeMakeLabel(pParse); + }else{ + /* For nVector==1, combine steps 6 and 7 by immediately returning + ** FALSE if the first comparison is not NULL */ + destNotNull = destIfFalse; } - sqlite3ReleaseTempReg(pParse, r1); - sqlite3ExprCachePop(pParse); + for(i=0; i1 ){ + sqlite3VdbeResolveLabel(v, destNotNull); + sqlite3VdbeAddOp2(v, OP_Next, iTab, addrTop+1); + VdbeCoverage(v); + + /* Step 7: If we reach this point, we know that the result must + ** be false. */ + sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse); + } + + /* Jumps here in order to return true. */ + sqlite3VdbeJumpHere(v, addrTruthOp); + +sqlite3ExprCodeIN_finished: VdbeComment((v, "end IN expr")); +sqlite3ExprCodeIN_oom_error: + sqlite3DbFree(pParse->db, aiMap); + sqlite3DbFree(pParse->db, zAff); } #endif /* SQLITE_OMIT_SUBQUERY */ @@ -2261,7 +4278,7 @@ static void sqlite3ExprCodeIN( ** Generate an instruction that will put the floating point ** value described by z[0..n-1] into register iMem. ** -** The z[] string will probably not be zero-terminated. But the +** The z[] string will probably not be zero-terminated. But the ** z[n] character is guaranteed to be something that does not look ** like the continuation of the number. */ @@ -2296,173 +4313,27 @@ static void codeInteger(Parse *pParse, Expr *pExpr, int negFlag, int iMem){ const char *z = pExpr->u.zToken; assert( z!=0 ); c = sqlite3DecOrHexToI64(z, &value); - if( c==0 || (c==2 && negFlag) ){ - if( negFlag ){ value = c==2 ? SMALLEST_INT64 : -value; } - sqlite3VdbeAddOp4Dup8(v, OP_Int64, 0, iMem, 0, (u8*)&value, P4_INT64); - }else{ + if( (c==3 && !negFlag) || (c==2) || (negFlag && value==SMALLEST_INT64)){ #ifdef SQLITE_OMIT_FLOATING_POINT - sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z); + sqlite3ErrorMsg(pParse, "oversized integer: %s%#T", negFlag?"-":"",pExpr); #else #ifndef SQLITE_OMIT_HEX_INTEGER if( sqlite3_strnicmp(z,"0x",2)==0 ){ - sqlite3ErrorMsg(pParse, "hex literal too big: %s", z); + sqlite3ErrorMsg(pParse, "hex literal too big: %s%#T", + negFlag?"-":"",pExpr); }else #endif { codeReal(v, z, negFlag, iMem); } #endif + }else{ + if( negFlag ){ value = c==3 ? SMALLEST_INT64 : -value; } + sqlite3VdbeAddOp4Dup8(v, OP_Int64, 0, iMem, 0, (u8*)&value, P4_INT64); } } } -/* -** Clear a cache entry. -*/ -static void cacheEntryClear(Parse *pParse, struct yColCache *p){ - if( p->tempReg ){ - if( pParse->nTempRegaTempReg) ){ - pParse->aTempReg[pParse->nTempReg++] = p->iReg; - } - p->tempReg = 0; - } -} - - -/* -** Record in the column cache that a particular column from a -** particular table is stored in a particular register. -*/ -void sqlite3ExprCacheStore(Parse *pParse, int iTab, int iCol, int iReg){ - int i; - int minLru; - int idxLru; - struct yColCache *p; - - /* Unless an error has occurred, register numbers are always positive. */ - assert( iReg>0 || pParse->nErr || pParse->db->mallocFailed ); - assert( iCol>=-1 && iCol<32768 ); /* Finite column numbers */ - - /* The SQLITE_ColumnCache flag disables the column cache. This is used - ** for testing only - to verify that SQLite always gets the same answer - ** with and without the column cache. - */ - if( OptimizationDisabled(pParse->db, SQLITE_ColumnCache) ) return; - - /* First replace any existing entry. - ** - ** Actually, the way the column cache is currently used, we are guaranteed - ** that the object will never already be in cache. Verify this guarantee. - */ -#ifndef NDEBUG - for(i=0, p=pParse->aColCache; iiReg==0 || p->iTable!=iTab || p->iColumn!=iCol ); - } -#endif - - /* Find an empty slot and replace it */ - for(i=0, p=pParse->aColCache; iiReg==0 ){ - p->iLevel = pParse->iCacheLevel; - p->iTable = iTab; - p->iColumn = iCol; - p->iReg = iReg; - p->tempReg = 0; - p->lru = pParse->iCacheCnt++; - return; - } - } - - /* Replace the last recently used */ - minLru = 0x7fffffff; - idxLru = -1; - for(i=0, p=pParse->aColCache; ilrulru; - } - } - if( ALWAYS(idxLru>=0) ){ - p = &pParse->aColCache[idxLru]; - p->iLevel = pParse->iCacheLevel; - p->iTable = iTab; - p->iColumn = iCol; - p->iReg = iReg; - p->tempReg = 0; - p->lru = pParse->iCacheCnt++; - return; - } -} - -/* -** Indicate that registers between iReg..iReg+nReg-1 are being overwritten. -** Purge the range of registers from the column cache. -*/ -void sqlite3ExprCacheRemove(Parse *pParse, int iReg, int nReg){ - int i; - int iLast = iReg + nReg - 1; - struct yColCache *p; - for(i=0, p=pParse->aColCache; iiReg; - if( r>=iReg && r<=iLast ){ - cacheEntryClear(pParse, p); - p->iReg = 0; - } - } -} - -/* -** Remember the current column cache context. Any new entries added -** added to the column cache after this call are removed when the -** corresponding pop occurs. -*/ -void sqlite3ExprCachePush(Parse *pParse){ - pParse->iCacheLevel++; -#ifdef SQLITE_DEBUG - if( pParse->db->flags & SQLITE_VdbeAddopTrace ){ - printf("PUSH to %d\n", pParse->iCacheLevel); - } -#endif -} - -/* -** Remove from the column cache any entries that were added since the -** the previous sqlite3ExprCachePush operation. In other words, restore -** the cache to the state it was in prior the most recent Push. -*/ -void sqlite3ExprCachePop(Parse *pParse){ - int i; - struct yColCache *p; - assert( pParse->iCacheLevel>=1 ); - pParse->iCacheLevel--; -#ifdef SQLITE_DEBUG - if( pParse->db->flags & SQLITE_VdbeAddopTrace ){ - printf("POP to %d\n", pParse->iCacheLevel); - } -#endif - for(i=0, p=pParse->aColCache; iiReg && p->iLevel>pParse->iCacheLevel ){ - cacheEntryClear(pParse, p); - p->iReg = 0; - } - } -} - -/* -** When a cached column is reused, make sure that its register is -** no longer available as a temp register. ticket #3879: that same -** register might be in the cache in multiple places, so be sure to -** get them all. -*/ -static void sqlite3ExprCachePinRegister(Parse *pParse, int iReg){ - int i; - struct yColCache *p; - for(i=0, p=pParse->aColCache; iiReg==iReg ){ - p->tempReg = 0; - } - } -} /* Generate code that will load into register regOut a value that is ** appropriate for the iIdxCol-th column of index pIdx. @@ -2478,47 +4349,106 @@ void sqlite3ExprCodeLoadIndexColumn( if( iTabCol==XN_EXPR ){ assert( pIdx->aColExpr ); assert( pIdx->aColExpr->nExpr>iIdxCol ); - pParse->iSelfTab = iTabCur; + pParse->iSelfTab = iTabCur + 1; sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[iIdxCol].pExpr, regOut); + pParse->iSelfTab = 0; }else{ sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pIdx->pTable, iTabCur, iTabCol, regOut); } } +#ifndef SQLITE_OMIT_GENERATED_COLUMNS +/* +** Generate code that will compute the value of generated column pCol +** and store the result in register regOut +*/ +void sqlite3ExprCodeGeneratedColumn( + Parse *pParse, /* Parsing context */ + Table *pTab, /* Table containing the generated column */ + Column *pCol, /* The generated column */ + int regOut /* Put the result in this register */ +){ + int iAddr; + Vdbe *v = pParse->pVdbe; + int nErr = pParse->nErr; + assert( v!=0 ); + assert( pParse->iSelfTab!=0 ); + if( pParse->iSelfTab>0 ){ + iAddr = sqlite3VdbeAddOp3(v, OP_IfNullRow, pParse->iSelfTab-1, 0, regOut); + }else{ + iAddr = 0; + } + sqlite3ExprCodeCopy(pParse, sqlite3ColumnExpr(pTab,pCol), regOut); + if( (pCol->colFlags & COLFLAG_VIRTUAL)!=0 + && (pTab->tabFlags & TF_Strict)!=0 + ){ + int p3 = 2+(int)(pCol - pTab->aCol); + sqlite3VdbeAddOp4(v, OP_TypeCheck, regOut, 1, p3, (char*)pTab, P4_TABLE); + }else if( pCol->affinity>=SQLITE_AFF_TEXT ){ + sqlite3VdbeAddOp4(v, OP_Affinity, regOut, 1, 0, &pCol->affinity, 1); + } + if( iAddr ) sqlite3VdbeJumpHere(v, iAddr); + if( pParse->nErr>nErr ) pParse->db->errByteOffset = -1; +} +#endif /* SQLITE_OMIT_GENERATED_COLUMNS */ + /* ** Generate code to extract the value of the iCol-th column of a table. */ void sqlite3ExprCodeGetColumnOfTable( - Vdbe *v, /* The VDBE under construction */ + Vdbe *v, /* Parsing context */ Table *pTab, /* The table containing the value */ int iTabCur, /* The table cursor. Or the PK cursor for WITHOUT ROWID */ int iCol, /* Index of the column to extract */ int regOut /* Extract the value into this register */ ){ + Column *pCol; + assert( v!=0 ); + assert( pTab!=0 ); + assert( iCol!=XN_EXPR ); if( iCol<0 || iCol==pTab->iPKey ){ sqlite3VdbeAddOp2(v, OP_Rowid, iTabCur, regOut); + VdbeComment((v, "%s.rowid", pTab->zName)); }else{ - int op = IsVirtual(pTab) ? OP_VColumn : OP_Column; - int x = iCol; - if( !HasRowid(pTab) ){ - x = sqlite3ColumnOfIndex(sqlite3PrimaryKeyIndex(pTab), iCol); + int op; + int x; + if( IsVirtual(pTab) ){ + op = OP_VColumn; + x = iCol; +#ifndef SQLITE_OMIT_GENERATED_COLUMNS + }else if( (pCol = &pTab->aCol[iCol])->colFlags & COLFLAG_VIRTUAL ){ + Parse *pParse = sqlite3VdbeParser(v); + if( pCol->colFlags & COLFLAG_BUSY ){ + sqlite3ErrorMsg(pParse, "generated column loop on \"%s\"", + pCol->zCnName); + }else{ + int savedSelfTab = pParse->iSelfTab; + pCol->colFlags |= COLFLAG_BUSY; + pParse->iSelfTab = iTabCur+1; + sqlite3ExprCodeGeneratedColumn(pParse, pTab, pCol, regOut); + pParse->iSelfTab = savedSelfTab; + pCol->colFlags &= ~COLFLAG_BUSY; + } + return; +#endif + }else if( !HasRowid(pTab) ){ + testcase( iCol!=sqlite3TableColumnToStorage(pTab, iCol) ); + x = sqlite3TableColumnToIndex(sqlite3PrimaryKeyIndex(pTab), iCol); + op = OP_Column; + }else{ + x = sqlite3TableColumnToStorage(pTab,iCol); + testcase( x!=iCol ); + op = OP_Column; } sqlite3VdbeAddOp3(v, op, iTabCur, x, regOut); - } - if( iCol>=0 ){ sqlite3ColumnDefault(v, pTab, iCol, regOut); } } /* ** Generate code that will extract the iColumn-th column from -** table pTab and store the column value in a register. -** -** An effort is made to store the column value in register iReg. This -** is not garanteeed for GetColumn() - the result can be stored in -** any register. But the result is guaranteed to land in register iReg -** for GetColumnToReg(). +** table pTab and store the column value in register iReg. ** ** There must be an open cursor to pTab in iTable when this routine ** is called. If iColumn<0 then code is generated that extracts the rowid. @@ -2531,105 +4461,443 @@ int sqlite3ExprCodeGetColumn( int iReg, /* Store results here */ u8 p5 /* P5 value for OP_Column + FLAGS */ ){ - Vdbe *v = pParse->pVdbe; - int i; - struct yColCache *p; - - for(i=0, p=pParse->aColCache; iiReg>0 && p->iTable==iTable && p->iColumn==iColumn ){ - p->lru = pParse->iCacheCnt++; - sqlite3ExprCachePinRegister(pParse, p->iReg); - return p->iReg; - } - } - assert( v!=0 ); - sqlite3ExprCodeGetColumnOfTable(v, pTab, iTable, iColumn, iReg); + assert( pParse->pVdbe!=0 ); + assert( (p5 & (OPFLAG_NOCHNG|OPFLAG_TYPEOFARG|OPFLAG_LENGTHARG))==p5 ); + assert( IsVirtual(pTab) || (p5 & OPFLAG_NOCHNG)==0 ); + sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pTab, iTable, iColumn, iReg); if( p5 ){ - sqlite3VdbeChangeP5(v, p5); - }else{ - sqlite3ExprCacheStore(pParse, iTable, iColumn, iReg); + VdbeOp *pOp = sqlite3VdbeGetLastOp(pParse->pVdbe); + if( pOp->opcode==OP_Column ) pOp->p5 = p5; + if( pOp->opcode==OP_VColumn ) pOp->p5 = (p5 & OPFLAG_NOCHNG); } return iReg; } -void sqlite3ExprCodeGetColumnToReg( - Parse *pParse, /* Parsing and code generating context */ - Table *pTab, /* Description of the table we are reading from */ - int iColumn, /* Index of the table column */ - int iTable, /* The cursor pointing to the table */ - int iReg /* Store results here */ -){ - int r1 = sqlite3ExprCodeGetColumn(pParse, pTab, iColumn, iTable, iReg, 0); - if( r1!=iReg ) sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, r1, iReg); + +/* +** Generate code to move content from registers iFrom...iFrom+nReg-1 +** over to iTo..iTo+nReg-1. +*/ +void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){ + sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg); } +/* +** Convert a scalar expression node to a TK_REGISTER referencing +** register iReg. The caller must ensure that iReg already contains +** the correct value for the expression. +*/ +void sqlite3ExprToRegister(Expr *pExpr, int iReg){ + Expr *p = sqlite3ExprSkipCollateAndLikely(pExpr); + if( NEVER(p==0) ) return; + if( p->op==TK_REGISTER ){ + assert( p->iTable==iReg ); + }else{ + p->op2 = p->op; + p->op = TK_REGISTER; + p->iTable = iReg; + ExprClearProperty(p, EP_Skip); + } +} /* -** Clear all column cache entries. +** Evaluate an expression (either a vector or a scalar expression) and store +** the result in contiguous temporary registers. Return the index of +** the first register used to store the result. +** +** If the returned result register is a temporary scalar, then also write +** that register number into *piFreeable. If the returned result register +** is not a temporary or if the expression is a vector set *piFreeable +** to 0. */ -void sqlite3ExprCacheClear(Parse *pParse){ - int i; - struct yColCache *p; +static int exprCodeVector(Parse *pParse, Expr *p, int *piFreeable){ + int iResult; + int nResult = sqlite3ExprVectorSize(p); + if( nResult==1 ){ + iResult = sqlite3ExprCodeTemp(pParse, p, piFreeable); + }else{ + *piFreeable = 0; + if( p->op==TK_SELECT ){ +#if SQLITE_OMIT_SUBQUERY + iResult = 0; +#else + iResult = sqlite3CodeSubselect(pParse, p); +#endif + }else{ + int i; + iResult = pParse->nMem+1; + pParse->nMem += nResult; + assert( ExprUseXList(p) ); + for(i=0; ix.pList->a[i].pExpr, i+iResult); + } + } + } + return iResult; +} -#if SQLITE_DEBUG - if( pParse->db->flags & SQLITE_VdbeAddopTrace ){ - printf("CLEAR\n"); +/* +** If the last opcode is a OP_Copy, then set the do-not-merge flag (p5) +** so that a subsequent copy will not be merged into this one. +*/ +static void setDoNotMergeFlagOnCopy(Vdbe *v){ + if( sqlite3VdbeGetLastOp(v)->opcode==OP_Copy ){ + sqlite3VdbeChangeP5(v, 1); /* Tag trailing OP_Copy as not mergeable */ } +} + +/* +** Generate code to implement special SQL functions that are implemented +** in-line rather than by using the usual callbacks. +*/ +static int exprCodeInlineFunction( + Parse *pParse, /* Parsing context */ + ExprList *pFarg, /* List of function arguments */ + int iFuncId, /* Function ID. One of the INTFUNC_... values */ + int target /* Store function result in this register */ +){ + int nFarg; + Vdbe *v = pParse->pVdbe; + assert( v!=0 ); + assert( pFarg!=0 ); + nFarg = pFarg->nExpr; + assert( nFarg>0 ); /* All in-line functions have at least one argument */ + switch( iFuncId ){ + case INLINEFUNC_coalesce: { + /* Attempt a direct implementation of the built-in COALESCE() and + ** IFNULL() functions. This avoids unnecessary evaluation of + ** arguments past the first non-NULL argument. + */ + int endCoalesce = sqlite3VdbeMakeLabel(pParse); + int i; + assert( nFarg>=2 ); + sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target); + for(i=1; ia[i].pExpr, target); + } + setDoNotMergeFlagOnCopy(v); + sqlite3VdbeResolveLabel(v, endCoalesce); + break; + } + case INLINEFUNC_iif: { + Expr caseExpr; + memset(&caseExpr, 0, sizeof(caseExpr)); + caseExpr.op = TK_CASE; + caseExpr.x.pList = pFarg; + return sqlite3ExprCodeTarget(pParse, &caseExpr, target); + } +#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC + case INLINEFUNC_sqlite_offset: { + Expr *pArg = pFarg->a[0].pExpr; + if( pArg->op==TK_COLUMN && pArg->iTable>=0 ){ + sqlite3VdbeAddOp3(v, OP_Offset, pArg->iTable, pArg->iColumn, target); + }else{ + sqlite3VdbeAddOp2(v, OP_Null, 0, target); + } + break; + } #endif - for(i=0, p=pParse->aColCache; iiReg ){ - cacheEntryClear(pParse, p); - p->iReg = 0; + default: { + /* The UNLIKELY() function is a no-op. The result is the value + ** of the first argument. + */ + assert( nFarg==1 || nFarg==2 ); + target = sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target); + break; + } + + /*********************************************************************** + ** Test-only SQL functions that are only usable if enabled + ** via SQLITE_TESTCTRL_INTERNAL_FUNCTIONS + */ +#if !defined(SQLITE_UNTESTABLE) + case INLINEFUNC_expr_compare: { + /* Compare two expressions using sqlite3ExprCompare() */ + assert( nFarg==2 ); + sqlite3VdbeAddOp2(v, OP_Integer, + sqlite3ExprCompare(0,pFarg->a[0].pExpr, pFarg->a[1].pExpr,-1), + target); + break; + } + + case INLINEFUNC_expr_implies_expr: { + /* Compare two expressions using sqlite3ExprImpliesExpr() */ + assert( nFarg==2 ); + sqlite3VdbeAddOp2(v, OP_Integer, + sqlite3ExprImpliesExpr(pParse,pFarg->a[0].pExpr, pFarg->a[1].pExpr,-1), + target); + break; + } + + case INLINEFUNC_implies_nonnull_row: { + /* Result of sqlite3ExprImpliesNonNullRow() */ + Expr *pA1; + assert( nFarg==2 ); + pA1 = pFarg->a[1].pExpr; + if( pA1->op==TK_COLUMN ){ + sqlite3VdbeAddOp2(v, OP_Integer, + sqlite3ExprImpliesNonNullRow(pFarg->a[0].pExpr,pA1->iTable,1), + target); + }else{ + sqlite3VdbeAddOp2(v, OP_Null, 0, target); + } + break; + } + + case INLINEFUNC_affinity: { + /* The AFFINITY() function evaluates to a string that describes + ** the type affinity of the argument. This is used for testing of + ** the SQLite type logic. + */ + const char *azAff[] = { "blob", "text", "numeric", "integer", + "real", "flexnum" }; + char aff; + assert( nFarg==1 ); + aff = sqlite3ExprAffinity(pFarg->a[0].pExpr); + assert( aff<=SQLITE_AFF_NONE + || (aff>=SQLITE_AFF_BLOB && aff<=SQLITE_AFF_FLEXNUM) ); + sqlite3VdbeLoadString(v, target, + (aff<=SQLITE_AFF_NONE) ? "none" : azAff[aff-SQLITE_AFF_BLOB]); + break; } +#endif /* !defined(SQLITE_UNTESTABLE) */ } + return target; } /* -** Record the fact that an affinity change has occurred on iCount -** registers starting with iStart. +** Expression Node callback for sqlite3ExprCanReturnSubtype(). +** +** Only a function call is able to return a subtype. So if the node +** is not a function call, return WRC_Prune immediately. +** +** A function call is able to return a subtype if it has the +** SQLITE_RESULT_SUBTYPE property. +** +** Assume that every function is able to pass-through a subtype from +** one of its argument (using sqlite3_result_value()). Most functions +** are not this way, but we don't have a mechanism to distinguish those +** that are from those that are not, so assume they all work this way. +** That means that if one of its arguments is another function and that +** other function is able to return a subtype, then this function is +** able to return a subtype. */ -void sqlite3ExprCacheAffinityChange(Parse *pParse, int iStart, int iCount){ - sqlite3ExprCacheRemove(pParse, iStart, iCount); +static int exprNodeCanReturnSubtype(Walker *pWalker, Expr *pExpr){ + int n; + FuncDef *pDef; + sqlite3 *db; + if( pExpr->op!=TK_FUNCTION ){ + return WRC_Prune; + } + assert( ExprUseXList(pExpr) ); + db = pWalker->pParse->db; + n = ALWAYS(pExpr->x.pList) ? pExpr->x.pList->nExpr : 0; + pDef = sqlite3FindFunction(db, pExpr->u.zToken, n, ENC(db), 0); + if( NEVER(pDef==0) || (pDef->funcFlags & SQLITE_RESULT_SUBTYPE)!=0 ){ + pWalker->eCode = 1; + return WRC_Prune; + } + return WRC_Continue; } /* -** Generate code to move content from registers iFrom...iFrom+nReg-1 -** over to iTo..iTo+nReg-1. Keep the column cache up-to-date. +** Return TRUE if expression pExpr is able to return a subtype. +** +** A TRUE return does not guarantee that a subtype will be returned. +** It only indicates that a subtype return is possible. False positives +** are acceptable as they only disable an optimization. False negatives, +** on the other hand, can lead to incorrect answers. */ -void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){ - assert( iFrom>=iTo+nReg || iFrom+nReg<=iTo ); - sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg); - sqlite3ExprCacheRemove(pParse, iFrom, nReg); +static int sqlite3ExprCanReturnSubtype(Parse *pParse, Expr *pExpr){ + Walker w; + memset(&w, 0, sizeof(w)); + w.pParse = pParse; + w.xExprCallback = exprNodeCanReturnSubtype; + sqlite3WalkExpr(&w, pExpr); + return w.eCode; +} + + +/* +** Check to see if pExpr is one of the indexed expressions on pParse->pIdxEpr. +** If it is, then resolve the expression by reading from the index and +** return the register into which the value has been read. If pExpr is +** not an indexed expression, then return negative. +*/ +static SQLITE_NOINLINE int sqlite3IndexedExprLookup( + Parse *pParse, /* The parsing context */ + Expr *pExpr, /* The expression to potentially bypass */ + int target /* Where to store the result of the expression */ +){ + IndexedExpr *p; + Vdbe *v; + for(p=pParse->pIdxEpr; p; p=p->pIENext){ + u8 exprAff; + int iDataCur = p->iDataCur; + if( iDataCur<0 ) continue; + if( pParse->iSelfTab ){ + if( p->iDataCur!=pParse->iSelfTab-1 ) continue; + iDataCur = -1; + } + if( sqlite3ExprCompare(0, pExpr, p->pExpr, iDataCur)!=0 ) continue; + assert( p->aff>=SQLITE_AFF_BLOB && p->aff<=SQLITE_AFF_NUMERIC ); + exprAff = sqlite3ExprAffinity(pExpr); + if( (exprAff<=SQLITE_AFF_BLOB && p->aff!=SQLITE_AFF_BLOB) + || (exprAff==SQLITE_AFF_TEXT && p->aff!=SQLITE_AFF_TEXT) + || (exprAff>=SQLITE_AFF_NUMERIC && p->aff!=SQLITE_AFF_NUMERIC) + ){ + /* Affinity mismatch on a generated column */ + continue; + } + + + /* Functions that might set a subtype should not be replaced by the + ** value taken from an expression index if they are themselves an + ** argument to another scalar function or aggregate. + ** https://sqlite.org/forum/forumpost/68d284c86b082c3e */ + if( ExprHasProperty(pExpr, EP_SubtArg) + && sqlite3ExprCanReturnSubtype(pParse, pExpr) + ){ + continue; + } + + v = pParse->pVdbe; + assert( v!=0 ); + if( p->bMaybeNullRow ){ + /* If the index is on a NULL row due to an outer join, then we + ** cannot extract the value from the index. The value must be + ** computed using the original expression. */ + int addr = sqlite3VdbeCurrentAddr(v); + sqlite3VdbeAddOp3(v, OP_IfNullRow, p->iIdxCur, addr+3, target); + VdbeCoverage(v); + sqlite3VdbeAddOp3(v, OP_Column, p->iIdxCur, p->iIdxCol, target); + VdbeComment((v, "%s expr-column %d", p->zIdxName, p->iIdxCol)); + sqlite3VdbeGoto(v, 0); + p = pParse->pIdxEpr; + pParse->pIdxEpr = 0; + sqlite3ExprCode(pParse, pExpr, target); + pParse->pIdxEpr = p; + sqlite3VdbeJumpHere(v, addr+2); + }else{ + sqlite3VdbeAddOp3(v, OP_Column, p->iIdxCur, p->iIdxCol, target); + VdbeComment((v, "%s expr-column %d", p->zIdxName, p->iIdxCol)); + } + return target; + } + return -1; /* Not found */ } -#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST) + /* -** Return true if any register in the range iFrom..iTo (inclusive) -** is used as part of the column cache. +** Expression pExpr is guaranteed to be a TK_COLUMN or equivalent. This +** function checks the Parse.pIdxPartExpr list to see if this column +** can be replaced with a constant value. If so, it generates code to +** put the constant value in a register (ideally, but not necessarily, +** register iTarget) and returns the register number. ** -** This routine is used within assert() and testcase() macros only -** and does not appear in a normal build. +** Or, if the TK_COLUMN cannot be replaced by a constant, zero is +** returned. */ -static int usedAsColumnCache(Parse *pParse, int iFrom, int iTo){ - int i; - struct yColCache *p; - for(i=0, p=pParse->aColCache; iiReg; - if( r>=iFrom && r<=iTo ) return 1; /*NO_TEST*/ +static int exprPartidxExprLookup(Parse *pParse, Expr *pExpr, int iTarget){ + IndexedExpr *p; + for(p=pParse->pIdxPartExpr; p; p=p->pIENext){ + if( pExpr->iColumn==p->iIdxCol && pExpr->iTable==p->iDataCur ){ + Vdbe *v = pParse->pVdbe; + int addr = 0; + int ret; + + if( p->bMaybeNullRow ){ + addr = sqlite3VdbeAddOp1(v, OP_IfNullRow, p->iIdxCur); + } + ret = sqlite3ExprCodeTarget(pParse, p->pExpr, iTarget); + sqlite3VdbeAddOp4(pParse->pVdbe, OP_Affinity, ret, 1, 0, + (const char*)&p->aff, 1); + if( addr ){ + sqlite3VdbeJumpHere(v, addr); + sqlite3VdbeChangeP3(v, addr, ret); + } + return ret; + } } return 0; } -#endif /* SQLITE_DEBUG || SQLITE_COVERAGE_TEST */ /* -** Convert an expression node to a TK_REGISTER +** Generate code that evaluates an AND or OR operator leaving a +** boolean result in a register. pExpr is the AND/OR expression. +** Store the result in the "target" register. Use short-circuit +** evaluation to avoid computing both operands, if possible. +** +** The code generated might require the use of a temporary register. +** If it does, then write the number of that temporary register +** into *pTmpReg. If not, leave *pTmpReg unchanged. */ -static void exprToRegister(Expr *p, int iReg){ - p->op2 = p->op; - p->op = TK_REGISTER; - p->iTable = iReg; - ExprClearProperty(p, EP_Skip); +static SQLITE_NOINLINE int exprCodeTargetAndOr( + Parse *pParse, /* Parsing context */ + Expr *pExpr, /* AND or OR expression to be coded */ + int target, /* Put result in this register, guaranteed */ + int *pTmpReg /* Write a temporary register here */ +){ + int op; /* The opcode. TK_AND or TK_OR */ + int skipOp; /* Opcode for the branch that skips one operand */ + int addrSkip; /* Branch instruction that skips one of the operands */ + int regSS = 0; /* Register holding computed operand when other omitted */ + int r1, r2; /* Registers for left and right operands, respectively */ + Expr *pAlt; /* Alternative, simplified expression */ + Vdbe *v; /* statement being coded */ + + assert( pExpr!=0 ); + op = pExpr->op; + assert( op==TK_AND || op==TK_OR ); + assert( TK_AND==OP_And ); testcase( op==TK_AND ); + assert( TK_OR==OP_Or ); testcase( op==TK_OR ); + assert( pParse->pVdbe!=0 ); + v = pParse->pVdbe; + pAlt = sqlite3ExprSimplifiedAndOr(pExpr); + if( pAlt!=pExpr ){ + r1 = sqlite3ExprCodeTarget(pParse, pAlt, target); + sqlite3VdbeAddOp3(v, OP_And, r1, r1, target); + return target; + } + skipOp = op==TK_AND ? OP_IfNot : OP_If; + if( exprEvalRhsFirst(pExpr) ){ + /* Compute the right operand first. Skip the computation of the left + ** operand if the right operand fully determines the result */ + r2 = regSS = sqlite3ExprCodeTarget(pParse, pExpr->pRight, target); + addrSkip = sqlite3VdbeAddOp1(v, skipOp, r2); + VdbeComment((v, "skip left operand")); + VdbeCoverage(v); + r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, pTmpReg); + }else{ + /* Compute the left operand first */ + r1 = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); + if( ExprHasProperty(pExpr->pRight, EP_Subquery) ){ + /* Skip over the computation of the right operand if the right + ** operand is a subquery and the left operand completely determines + ** the result */ + regSS = r1; + addrSkip = sqlite3VdbeAddOp1(v, skipOp, r1); + VdbeComment((v, "skip right operand")); + VdbeCoverage(v); + }else{ + addrSkip = regSS = 0; + } + r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, pTmpReg); + } + sqlite3VdbeAddOp3(v, op, r2, r1, target); + testcase( (*pTmpReg)==0 ); + if( addrSkip ){ + sqlite3VdbeAddOp2(v, OP_Goto, 0, sqlite3VdbeCurrentAddr(v)+2); + sqlite3VdbeJumpHere(v, addrSkip); + sqlite3VdbeAddOp3(v, OP_Or, regSS, regSS, target); + VdbeComment((v, "short-circut value")); + } + return target; } + + /* ** Generate code into the current Vdbe to evaluate the given ** expression. Attempt to store the results in register "target". @@ -2647,73 +4915,193 @@ int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){ int inReg = target; /* Results stored in register inReg */ int regFree1 = 0; /* If non-zero free this temporary register */ int regFree2 = 0; /* If non-zero free this temporary register */ - int r1, r2, r3, r4; /* Various register numbers */ - sqlite3 *db = pParse->db; /* The database connection */ + int r1, r2; /* Various register numbers */ Expr tempX; /* Temporary expression node */ + int p5 = 0; assert( target>0 && target<=pParse->nMem ); - if( v==0 ){ - assert( pParse->db->mallocFailed ); - return 0; - } + assert( v!=0 ); +expr_code_doover: if( pExpr==0 ){ op = TK_NULL; + }else if( pParse->pIdxEpr!=0 + && !ExprHasProperty(pExpr, EP_Leaf) + && (r1 = sqlite3IndexedExprLookup(pParse, pExpr, target))>=0 + ){ + return r1; }else{ + assert( !ExprHasVVAProperty(pExpr,EP_Immutable) ); op = pExpr->op; } + assert( op!=TK_ORDER ); switch( op ){ case TK_AGG_COLUMN: { AggInfo *pAggInfo = pExpr->pAggInfo; - struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg]; - if( !pAggInfo->directMode ){ - assert( pCol->iMem>0 ); - inReg = pCol->iMem; + struct AggInfo_col *pCol; + assert( pAggInfo!=0 ); + assert( pExpr->iAgg>=0 ); + if( pExpr->iAgg>=pAggInfo->nColumn ){ + /* Happens when the left table of a RIGHT JOIN is null and + ** is using an expression index */ + sqlite3VdbeAddOp2(v, OP_Null, 0, target); +#ifdef SQLITE_VDBE_COVERAGE + /* Verify that the OP_Null above is exercised by tests + ** tag-20230325-2 */ + sqlite3VdbeAddOp3(v, OP_NotNull, target, 1, 20230325); + VdbeCoverageNeverTaken(v); +#endif break; + } + pCol = &pAggInfo->aCol[pExpr->iAgg]; + if( !pAggInfo->directMode ){ + return AggInfoColumnReg(pAggInfo, pExpr->iAgg); }else if( pAggInfo->useSortingIdx ){ + Table *pTab = pCol->pTab; sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab, pCol->iSorterColumn, target); - break; + if( pTab==0 ){ + /* No comment added */ + }else if( pCol->iColumn<0 ){ + VdbeComment((v,"%s.rowid",pTab->zName)); + }else{ + VdbeComment((v,"%s.%s", + pTab->zName, pTab->aCol[pCol->iColumn].zCnName)); + if( pTab->aCol[pCol->iColumn].affinity==SQLITE_AFF_REAL ){ + sqlite3VdbeAddOp1(v, OP_RealAffinity, target); + } + } + return target; + }else if( pExpr->y.pTab==0 ){ + /* This case happens when the argument to an aggregate function + ** is rewritten by aggregateConvertIndexedExprRefToColumn() */ + sqlite3VdbeAddOp3(v, OP_Column, pExpr->iTable, pExpr->iColumn, target); + return target; } /* Otherwise, fall thru into the TK_COLUMN case */ + /* no break */ deliberate_fall_through } case TK_COLUMN: { int iTab = pExpr->iTable; + int iReg; + if( ExprHasProperty(pExpr, EP_FixedCol) ){ + /* This COLUMN expression is really a constant due to WHERE clause + ** constraints, and that constant is coded by the pExpr->pLeft + ** expression. However, make sure the constant has the correct + ** datatype by applying the Affinity of the table column to the + ** constant. + */ + int aff; + iReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft,target); + assert( ExprUseYTab(pExpr) ); + assert( pExpr->y.pTab!=0 ); + aff = sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn); + if( aff>SQLITE_AFF_BLOB ){ + static const char zAff[] = "B\000C\000D\000E\000F"; + assert( SQLITE_AFF_BLOB=='A' ); + assert( SQLITE_AFF_TEXT=='B' ); + sqlite3VdbeAddOp4(v, OP_Affinity, iReg, 1, 0, + &zAff[(aff-'B')*2], P4_STATIC); + } + return iReg; + } if( iTab<0 ){ - if( pParse->ckBase>0 ){ - /* Generating CHECK constraints or inserting into partial index */ - inReg = pExpr->iColumn + pParse->ckBase; - break; + if( pParse->iSelfTab<0 ){ + /* Other columns in the same row for CHECK constraints or + ** generated columns or for inserting into partial index. + ** The row is unpacked into registers beginning at + ** 0-(pParse->iSelfTab). The rowid (if any) is in a register + ** immediately prior to the first column. + */ + Column *pCol; + Table *pTab; + int iSrc; + int iCol = pExpr->iColumn; + assert( ExprUseYTab(pExpr) ); + pTab = pExpr->y.pTab; + assert( pTab!=0 ); + assert( iCol>=XN_ROWID ); + assert( iColnCol ); + if( iCol<0 ){ + return -1-pParse->iSelfTab; + } + pCol = pTab->aCol + iCol; + testcase( iCol!=sqlite3TableColumnToStorage(pTab,iCol) ); + iSrc = sqlite3TableColumnToStorage(pTab, iCol) - pParse->iSelfTab; +#ifndef SQLITE_OMIT_GENERATED_COLUMNS + if( pCol->colFlags & COLFLAG_GENERATED ){ + if( pCol->colFlags & COLFLAG_BUSY ){ + sqlite3ErrorMsg(pParse, "generated column loop on \"%s\"", + pCol->zCnName); + return 0; + } + pCol->colFlags |= COLFLAG_BUSY; + if( pCol->colFlags & COLFLAG_NOTAVAIL ){ + sqlite3ExprCodeGeneratedColumn(pParse, pTab, pCol, iSrc); + } + pCol->colFlags &= ~(COLFLAG_BUSY|COLFLAG_NOTAVAIL); + return iSrc; + }else +#endif /* SQLITE_OMIT_GENERATED_COLUMNS */ + if( pCol->affinity==SQLITE_AFF_REAL ){ + sqlite3VdbeAddOp2(v, OP_SCopy, iSrc, target); + sqlite3VdbeAddOp1(v, OP_RealAffinity, target); + return target; + }else{ + return iSrc; + } }else{ /* Coding an expression that is part of an index where column names ** in the index refer to the table to which the index belongs */ - iTab = pParse->iSelfTab; + iTab = pParse->iSelfTab - 1; } } - inReg = sqlite3ExprCodeGetColumn(pParse, pExpr->pTab, + else if( pParse->pIdxPartExpr + && 0!=(r1 = exprPartidxExprLookup(pParse, pExpr, target)) + ){ + return r1; + } + assert( ExprUseYTab(pExpr) ); + assert( pExpr->y.pTab!=0 ); + iReg = sqlite3ExprCodeGetColumn(pParse, pExpr->y.pTab, pExpr->iColumn, iTab, target, pExpr->op2); - break; + return iReg; } case TK_INTEGER: { codeInteger(pParse, pExpr, 0, target); - break; + return target; + } + case TK_TRUEFALSE: { + sqlite3VdbeAddOp2(v, OP_Integer, sqlite3ExprTruthValue(pExpr), target); + return target; } #ifndef SQLITE_OMIT_FLOATING_POINT case TK_FLOAT: { assert( !ExprHasProperty(pExpr, EP_IntValue) ); codeReal(v, pExpr->u.zToken, 0, target); - break; + return target; } #endif case TK_STRING: { assert( !ExprHasProperty(pExpr, EP_IntValue) ); sqlite3VdbeLoadString(v, target, pExpr->u.zToken); - break; + return target; + } + case TK_NULLS: { + /* Set a range of registers to NULL. pExpr->y.nReg registers starting + ** with target */ + sqlite3VdbeAddOp3(v, OP_Null, 0, target, target + pExpr->y.nReg - 1); + return target; } - case TK_NULL: { + default: { + /* Make NULL the default case so that if a bug causes an illegal + ** Expr node to be passed into this function, it will be handled + ** sanely and not crash. But keep the assert() to bring the problem + ** to the attention of the developers. */ + assert( op==TK_NULL || op==TK_ERROR || pParse->db->mallocFailed ); sqlite3VdbeAddOp2(v, OP_Null, 0, target); - break; + return target; } #ifndef SQLITE_OMIT_BLOB_LITERAL case TK_BLOB: { @@ -2728,7 +5116,7 @@ int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){ assert( z[n]=='\'' ); zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n); sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC); - break; + return target; } #endif case TK_VARIABLE: { @@ -2736,69 +5124,75 @@ int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){ assert( pExpr->u.zToken!=0 ); assert( pExpr->u.zToken[0]!=0 ); sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target); - if( pExpr->u.zToken[1]!=0 ){ - assert( pExpr->u.zToken[0]=='?' - || strcmp(pExpr->u.zToken, pParse->azVar[pExpr->iColumn-1])==0 ); - sqlite3VdbeChangeP4(v, -1, pParse->azVar[pExpr->iColumn-1], P4_STATIC); - } - break; + return target; } case TK_REGISTER: { - inReg = pExpr->iTable; - break; + return pExpr->iTable; } #ifndef SQLITE_OMIT_CAST case TK_CAST: { /* Expressions of the form: CAST(pLeft AS token) */ - inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); - if( inReg!=target ){ - sqlite3VdbeAddOp2(v, OP_SCopy, inReg, target); - inReg = target; - } + sqlite3ExprCode(pParse, pExpr->pLeft, target); + assert( inReg==target ); + assert( !ExprHasProperty(pExpr, EP_IntValue) ); sqlite3VdbeAddOp2(v, OP_Cast, target, sqlite3AffinityType(pExpr->u.zToken, 0)); - testcase( usedAsColumnCache(pParse, inReg, inReg) ); - sqlite3ExprCacheAffinityChange(pParse, inReg, 1); - break; + return inReg; } #endif /* SQLITE_OMIT_CAST */ + case TK_IS: + case TK_ISNOT: + op = (op==TK_IS) ? TK_EQ : TK_NE; + p5 = SQLITE_NULLEQ; + /* fall-through */ case TK_LT: case TK_LE: case TK_GT: case TK_GE: case TK_NE: case TK_EQ: { - r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); - r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); - codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, - r1, r2, inReg, SQLITE_STOREP2); - assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt); - assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le); - assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt); - assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge); - assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq); - assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne); - testcase( regFree1==0 ); - testcase( regFree2==0 ); + Expr *pLeft = pExpr->pLeft; + int addrIsNull = 0; + if( sqlite3ExprIsVector(pLeft) ){ + codeVectorCompare(pParse, pExpr, target, op, p5); + }else{ + if( ExprHasProperty(pExpr, EP_Subquery) && p5!=SQLITE_NULLEQ ){ + addrIsNull = exprComputeOperands(pParse, pExpr, + &r1, &r2, ®Free1, ®Free2); + }else{ + r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); + r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); + } + sqlite3VdbeAddOp2(v, OP_Integer, 1, inReg); + codeCompare(pParse, pLeft, pExpr->pRight, op, r1, r2, + sqlite3VdbeCurrentAddr(v)+2, p5, + ExprHasProperty(pExpr,EP_Commuted)); + assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt); + assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le); + assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt); + assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge); + assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq); + assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne); + if( p5==SQLITE_NULLEQ ){ + sqlite3VdbeAddOp2(v, OP_Integer, 0, inReg); + }else{ + sqlite3VdbeAddOp3(v, OP_ZeroOrNull, r1, inReg, r2); + if( addrIsNull ){ + sqlite3VdbeAddOp2(v, OP_Goto, 0, sqlite3VdbeCurrentAddr(v)+2); + sqlite3VdbeJumpHere(v, addrIsNull); + sqlite3VdbeAddOp2(v, OP_Null, 0, inReg); + } + } + testcase( regFree1==0 ); + testcase( regFree2==0 ); + } break; } - case TK_IS: - case TK_ISNOT: { - testcase( op==TK_IS ); - testcase( op==TK_ISNOT ); - r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); - r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); - op = (op==TK_IS) ? TK_EQ : TK_NE; - codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, - r1, r2, inReg, SQLITE_STOREP2 | SQLITE_NULLEQ); - VdbeCoverageIf(v, op==TK_EQ); - VdbeCoverageIf(v, op==TK_NE); - testcase( regFree1==0 ); - testcase( regFree2==0 ); + case TK_AND: + case TK_OR: { + inReg = exprCodeTargetAndOr(pParse, pExpr, target, ®Free1); break; } - case TK_AND: - case TK_OR: case TK_PLUS: case TK_STAR: case TK_MINUS: @@ -2807,10 +5201,9 @@ int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){ case TK_BITOR: case TK_SLASH: case TK_LSHIFT: - case TK_RSHIFT: + case TK_RSHIFT: case TK_CONCAT: { - assert( TK_AND==OP_And ); testcase( op==TK_AND ); - assert( TK_OR==OP_Or ); testcase( op==TK_OR ); + int addrIsNull; assert( TK_PLUS==OP_Add ); testcase( op==TK_PLUS ); assert( TK_MINUS==OP_Subtract ); testcase( op==TK_MINUS ); assert( TK_REM==OP_Remainder ); testcase( op==TK_REM ); @@ -2820,11 +5213,23 @@ int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){ assert( TK_LSHIFT==OP_ShiftLeft ); testcase( op==TK_LSHIFT ); assert( TK_RSHIFT==OP_ShiftRight ); testcase( op==TK_RSHIFT ); assert( TK_CONCAT==OP_Concat ); testcase( op==TK_CONCAT ); - r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); - r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); + if( ExprHasProperty(pExpr, EP_Subquery) ){ + addrIsNull = exprComputeOperands(pParse, pExpr, + &r1, &r2, ®Free1, ®Free2); + }else{ + r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); + r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); + addrIsNull = 0; + } sqlite3VdbeAddOp3(v, op, r2, r1, target); testcase( regFree1==0 ); testcase( regFree2==0 ); + if( addrIsNull ){ + sqlite3VdbeAddOp2(v, OP_Goto, 0, sqlite3VdbeCurrentAddr(v)+2); + sqlite3VdbeJumpHere(v, addrIsNull); + sqlite3VdbeAddOp2(v, OP_Null, 0, target); + VdbeComment((v, "short-circut value")); + } break; } case TK_UMINUS: { @@ -2832,21 +5237,23 @@ int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){ assert( pLeft ); if( pLeft->op==TK_INTEGER ){ codeInteger(pParse, pLeft, 1, target); + return target; #ifndef SQLITE_OMIT_FLOATING_POINT }else if( pLeft->op==TK_FLOAT ){ assert( !ExprHasProperty(pExpr, EP_IntValue) ); codeReal(v, pLeft->u.zToken, 1, target); + return target; #endif }else{ tempX.op = TK_INTEGER; tempX.flags = EP_IntValue|EP_TokenOnly; tempX.u.iValue = 0; + ExprClearVVAProperties(&tempX); r1 = sqlite3ExprCodeTemp(pParse, &tempX, ®Free1); r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free2); sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target); testcase( regFree2==0 ); } - inReg = target; break; } case TK_BITNOT: @@ -2855,10 +5262,21 @@ int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){ assert( TK_NOT==OP_Not ); testcase( op==TK_NOT ); r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); testcase( regFree1==0 ); - inReg = target; sqlite3VdbeAddOp2(v, op, r1, inReg); break; } + case TK_TRUTH: { + int isTrue; /* IS TRUE or IS NOT TRUE */ + int bNormal; /* IS TRUE or IS FALSE */ + r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); + testcase( regFree1==0 ); + isTrue = sqlite3ExprTruthValue(pExpr->pRight); + bNormal = pExpr->op2==TK_IS; + testcase( isTrue && bNormal); + testcase( !isTrue && bNormal); + sqlite3VdbeAddOp4Int(v, OP_IsTrue, r1, inReg, !isTrue, isTrue ^ bNormal); + break; + } case TK_ISNULL: case TK_NOTNULL: { int addr; @@ -2876,11 +5294,14 @@ int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){ } case TK_AGG_FUNCTION: { AggInfo *pInfo = pExpr->pAggInfo; - if( pInfo==0 ){ + if( pInfo==0 + || NEVER(pExpr->iAgg<0) + || NEVER(pExpr->iAgg>=pInfo->nFunc) + ){ assert( !ExprHasProperty(pExpr, EP_IntValue) ); - sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken); + sqlite3ErrorMsg(pParse, "misuse of aggregate: %#T()", pExpr); }else{ - inReg = pInfo->aFunc[pExpr->iAgg].iMem; + return AggInfoFuncReg(pInfo, pExpr->iAgg); } break; } @@ -2888,60 +5309,53 @@ int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){ ExprList *pFarg; /* List of function arguments */ int nFarg; /* Number of function arguments */ FuncDef *pDef; /* The function definition object */ - int nId; /* Length of the function name in bytes */ const char *zId; /* The function name */ u32 constMask = 0; /* Mask of function arguments that are constant */ int i; /* Loop counter */ + sqlite3 *db = pParse->db; /* The database connection */ u8 enc = ENC(db); /* The text encoding used by this database */ CollSeq *pColl = 0; /* A collating sequence */ - assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); - if( ExprHasProperty(pExpr, EP_TokenOnly) ){ - pFarg = 0; - }else{ - pFarg = pExpr->x.pList; +#ifndef SQLITE_OMIT_WINDOWFUNC + if( ExprHasProperty(pExpr, EP_WinFunc) ){ + return pExpr->y.pWin->regResult; + } +#endif + + if( ConstFactorOk(pParse) + && sqlite3ExprIsConstantNotJoin(pParse,pExpr) + ){ + /* SQL functions can be expensive. So try to avoid running them + ** multiple times if we know they always give the same result */ + return sqlite3ExprCodeRunJustOnce(pParse, pExpr, -1); } + assert( !ExprHasProperty(pExpr, EP_TokenOnly) ); + assert( ExprUseXList(pExpr) ); + pFarg = pExpr->x.pList; nFarg = pFarg ? pFarg->nExpr : 0; assert( !ExprHasProperty(pExpr, EP_IntValue) ); zId = pExpr->u.zToken; - nId = sqlite3Strlen30(zId); - pDef = sqlite3FindFunction(db, zId, nId, nFarg, enc, 0); - if( pDef==0 || pDef->xFinalize!=0 ){ - sqlite3ErrorMsg(pParse, "unknown function: %.*s()", nId, zId); - break; + pDef = sqlite3FindFunction(db, zId, nFarg, enc, 0); +#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION + if( pDef==0 && pParse->explain ){ + pDef = sqlite3FindFunction(db, "unknown", nFarg, enc, 0); } - - /* Attempt a direct implementation of the built-in COALESCE() and - ** IFNULL() functions. This avoids unnecessary evaluation of - ** arguments past the first non-NULL argument. - */ - if( pDef->funcFlags & SQLITE_FUNC_COALESCE ){ - int endCoalesce = sqlite3VdbeMakeLabel(v); - assert( nFarg>=2 ); - sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target); - for(i=1; ia[i].pExpr, target); - sqlite3ExprCachePop(pParse); - } - sqlite3VdbeResolveLabel(v, endCoalesce); +#endif + if( pDef==0 || pDef->xFinalize!=0 ){ + sqlite3ErrorMsg(pParse, "unknown function: %#T()", pExpr); break; } - - /* The UNLIKELY() function is a no-op. The result is the value - ** of the first argument. - */ - if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){ - assert( nFarg>=1 ); - inReg = sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target); - break; + if( (pDef->funcFlags & SQLITE_FUNC_INLINE)!=0 && ALWAYS(pFarg!=0) ){ + assert( (pDef->funcFlags & SQLITE_FUNC_UNSAFE)==0 ); + assert( (pDef->funcFlags & SQLITE_FUNC_DIRECT)==0 ); + return exprCodeInlineFunction(pParse, pFarg, + SQLITE_PTR_TO_INT(pDef->pUserData), target); + }else if( pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE) ){ + sqlite3ExprFunctionUsable(pParse, pExpr, pDef); } for(i=0; ia[i].pExpr) ){ + if( i<32 && sqlite3ExprIsConstant(pParse, pFarg->a[i].pExpr) ){ testcase( i==31 ); constMask |= MASKBIT32(i); } @@ -2957,10 +5371,10 @@ int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){ r1 = sqlite3GetTempRange(pParse, nFarg); } - /* For length() and typeof() functions with a column argument, + /* For length() and typeof() and octet_length() functions, ** set the P5 parameter to the OP_Column opcode to OPFLAG_LENGTHARG - ** or OPFLAG_TYPEOFARG respectively, to avoid unnecessary data - ** loading. + ** or OPFLAG_TYPEOFARG or OPFLAG_BYTELENARG respectively, to avoid + ** unnecessary data loading. */ if( (pDef->funcFlags & (SQLITE_FUNC_LENGTH|SQLITE_FUNC_TYPEOF))!=0 ){ u8 exprOp; @@ -2970,16 +5384,16 @@ int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){ if( exprOp==TK_COLUMN || exprOp==TK_AGG_COLUMN ){ assert( SQLITE_FUNC_LENGTH==OPFLAG_LENGTHARG ); assert( SQLITE_FUNC_TYPEOF==OPFLAG_TYPEOFARG ); - testcase( pDef->funcFlags & OPFLAG_LENGTHARG ); - pFarg->a[0].pExpr->op2 = - pDef->funcFlags & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG); + assert( SQLITE_FUNC_BYTELEN==OPFLAG_BYTELENARG ); + assert( (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG)==OPFLAG_BYTELENARG ); + testcase( (pDef->funcFlags & OPFLAG_BYTELENARG)==OPFLAG_LENGTHARG ); + testcase( (pDef->funcFlags & OPFLAG_BYTELENARG)==OPFLAG_TYPEOFARG ); + testcase( (pDef->funcFlags & OPFLAG_BYTELENARG)==OPFLAG_BYTELENARG); + pFarg->a[0].pExpr->op2 = pDef->funcFlags & OPFLAG_BYTELENARG; } } - sqlite3ExprCachePush(pParse); /* Ticket 2ea2425d34be */ - sqlite3ExprCodeExprList(pParse, pFarg, r1, 0, - SQLITE_ECEL_DUP|SQLITE_ECEL_FACTOR); - sqlite3ExprCachePop(pParse); /* Ticket 2ea2425d34be */ + sqlite3ExprCodeExprList(pParse, pFarg, r1, 0, SQLITE_ECEL_FACTOR); }else{ r1 = 0; } @@ -2992,46 +5406,74 @@ int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){ ** see if it is a column in a virtual table. This is done because ** the left operand of infix functions (the operand we want to ** control overloading) ends up as the second argument to the - ** function. The expression "A glob B" is equivalent to + ** function. The expression "A glob B" is equivalent to ** "glob(B,A). We want to use the A in "A glob B" to test ** for function overloading. But we use the B term in "glob(B,A)". */ - if( nFarg>=2 && (pExpr->flags & EP_InfixFunc) ){ + if( nFarg>=2 && ExprHasProperty(pExpr, EP_InfixFunc) ){ pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr); }else if( nFarg>0 ){ pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr); } #endif if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL ){ - if( !pColl ) pColl = db->pDfltColl; + if( !pColl ) pColl = db->pDfltColl; sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ); } - sqlite3VdbeAddOp4(v, OP_Function0, constMask, r1, target, - (char*)pDef, P4_FUNCDEF); - sqlite3VdbeChangeP5(v, (u8)nFarg); - if( nFarg && constMask==0 ){ - sqlite3ReleaseTempRange(pParse, r1, nFarg); + sqlite3VdbeAddFunctionCall(pParse, constMask, r1, target, nFarg, + pDef, pExpr->op2); + if( nFarg ){ + if( constMask==0 ){ + sqlite3ReleaseTempRange(pParse, r1, nFarg); + }else{ + sqlite3VdbeReleaseRegisters(pParse, r1, nFarg, constMask, 1); + } } - break; + return target; } #ifndef SQLITE_OMIT_SUBQUERY case TK_EXISTS: case TK_SELECT: { + int nCol; testcase( op==TK_EXISTS ); testcase( op==TK_SELECT ); - inReg = sqlite3CodeSubselect(pParse, pExpr, 0, 0); + if( pParse->db->mallocFailed ){ + return 0; + }else if( op==TK_SELECT + && ALWAYS( ExprUseXSelect(pExpr) ) + && (nCol = pExpr->x.pSelect->pEList->nExpr)!=1 + ){ + sqlite3SubselectError(pParse, nCol, 1); + }else{ + return sqlite3CodeSubselect(pParse, pExpr); + } break; } + case TK_SELECT_COLUMN: { + int n; + Expr *pLeft = pExpr->pLeft; + if( pLeft->iTable==0 || pParse->withinRJSubrtn > pLeft->op2 ){ + pLeft->iTable = sqlite3CodeSubselect(pParse, pLeft); + pLeft->op2 = pParse->withinRJSubrtn; + } + assert( pLeft->op==TK_SELECT || pLeft->op==TK_ERROR ); + n = sqlite3ExprVectorSize(pLeft); + if( pExpr->iTable!=n ){ + sqlite3ErrorMsg(pParse, "%d columns assigned %d values", + pExpr->iTable, n); + } + return pLeft->iTable + pExpr->iColumn; + } case TK_IN: { - int destIfFalse = sqlite3VdbeMakeLabel(v); - int destIfNull = sqlite3VdbeMakeLabel(v); + int destIfFalse = sqlite3VdbeMakeLabel(pParse); + int destIfNull = sqlite3VdbeMakeLabel(pParse); sqlite3VdbeAddOp2(v, OP_Null, 0, target); sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull); sqlite3VdbeAddOp2(v, OP_Integer, 1, target); sqlite3VdbeResolveLabel(v, destIfFalse); sqlite3VdbeAddOp2(v, OP_AddImm, target, 0); sqlite3VdbeResolveLabel(v, destIfNull); - break; + return target; } #endif /* SQLITE_OMIT_SUBQUERY */ @@ -3048,34 +5490,30 @@ int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){ ** Z is stored in pExpr->pList->a[1].pExpr. */ case TK_BETWEEN: { - Expr *pLeft = pExpr->pLeft; - struct ExprList_item *pLItem = pExpr->x.pList->a; - Expr *pRight = pLItem->pExpr; - - r1 = sqlite3ExprCodeTemp(pParse, pLeft, ®Free1); - r2 = sqlite3ExprCodeTemp(pParse, pRight, ®Free2); - testcase( regFree1==0 ); - testcase( regFree2==0 ); - r3 = sqlite3GetTempReg(pParse); - r4 = sqlite3GetTempReg(pParse); - codeCompare(pParse, pLeft, pRight, OP_Ge, - r1, r2, r3, SQLITE_STOREP2); VdbeCoverage(v); - pLItem++; - pRight = pLItem->pExpr; - sqlite3ReleaseTempReg(pParse, regFree2); - r2 = sqlite3ExprCodeTemp(pParse, pRight, ®Free2); - testcase( regFree2==0 ); - codeCompare(pParse, pLeft, pRight, OP_Le, r1, r2, r4, SQLITE_STOREP2); - VdbeCoverage(v); - sqlite3VdbeAddOp3(v, OP_And, r3, r4, target); - sqlite3ReleaseTempReg(pParse, r3); - sqlite3ReleaseTempReg(pParse, r4); - break; + exprCodeBetween(pParse, pExpr, target, 0, 0); + return target; + } + case TK_COLLATE: { + if( !ExprHasProperty(pExpr, EP_Collate) ){ + /* A TK_COLLATE Expr node without the EP_Collate tag is a so-called + ** "SOFT-COLLATE" that is added to constraints that are pushed down + ** from outer queries into sub-queries by the WHERE-clause push-down + ** optimization. Clear subtypes as subtypes may not cross a subquery + ** boundary. + */ + assert( pExpr->pLeft ); + sqlite3ExprCode(pParse, pExpr->pLeft, target); + sqlite3VdbeAddOp1(v, OP_ClrSubtype, target); + return target; + }else{ + pExpr = pExpr->pLeft; + goto expr_code_doover; /* 2018-04-28: Prevent deep recursion. */ + } } - case TK_COLLATE: + case TK_SPAN: case TK_UPLUS: { - inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); - break; + pExpr = pExpr->pLeft; + goto expr_code_doover; /* 2018-04-28: Prevent deep recursion. OSSFuzz. */ } case TK_TRIGGER: { @@ -3088,7 +5526,7 @@ int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){ ** ** The expression is implemented using an OP_Param opcode. The p1 ** parameter is set to 0 for an old.rowid reference, or to (i+1) - ** to reference another column of the old.* pseudo-table, where + ** to reference another column of the old.* pseudo-table, where ** i is the index of the column. For a new.rowid reference, p1 is ** set to (n+1), where n is the number of columns in each pseudo-table. ** For a reference to any other column in the new.* pseudo-table, p1 @@ -3102,21 +5540,27 @@ int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){ ** ** p1==0 -> old.rowid p1==3 -> new.rowid ** p1==1 -> old.a p1==4 -> new.a - ** p1==2 -> old.b p1==5 -> new.b + ** p1==2 -> old.b p1==5 -> new.b */ - Table *pTab = pExpr->pTab; - int p1 = pExpr->iTable * (pTab->nCol+1) + 1 + pExpr->iColumn; + Table *pTab; + int iCol; + int p1; + + assert( ExprUseYTab(pExpr) ); + pTab = pExpr->y.pTab; + iCol = pExpr->iColumn; + p1 = pExpr->iTable * (pTab->nCol+1) + 1 + + sqlite3TableColumnToStorage(pTab, iCol); assert( pExpr->iTable==0 || pExpr->iTable==1 ); - assert( pExpr->iColumn>=-1 && pExpr->iColumnnCol ); - assert( pTab->iPKey<0 || pExpr->iColumn!=pTab->iPKey ); + assert( iCol>=-1 && iColnCol ); + assert( pTab->iPKey<0 || iCol!=pTab->iPKey ); assert( p1>=0 && p1<(pTab->nCol*2+2) ); sqlite3VdbeAddOp2(v, OP_Param, p1, target); - VdbeComment((v, "%s.%s -> $%d", + VdbeComment((v, "r[%d]=%s.%s", target, (pExpr->iTable ? "new" : "old"), - (pExpr->iColumn<0 ? "rowid" : pExpr->pTab->aCol[pExpr->iColumn].zName), - target + (pExpr->iColumn<0 ? "rowid" : pExpr->y.pTab->aCol[iCol].zCnName) )); #ifndef SQLITE_OMIT_FLOATING_POINT @@ -3125,15 +5569,57 @@ int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){ ** ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to ** floating point when extracting it from the record. */ - if( pExpr->iColumn>=0 - && pTab->aCol[pExpr->iColumn].affinity==SQLITE_AFF_REAL - ){ + if( iCol>=0 && pTab->aCol[iCol].affinity==SQLITE_AFF_REAL ){ sqlite3VdbeAddOp1(v, OP_RealAffinity, target); } #endif break; } + case TK_VECTOR: { + sqlite3ErrorMsg(pParse, "row value misused"); + break; + } + + /* TK_IF_NULL_ROW Expr nodes are inserted ahead of expressions + ** that derive from the right-hand table of a LEFT JOIN. The + ** Expr.iTable value is the table number for the right-hand table. + ** The expression is only evaluated if that table is not currently + ** on a LEFT JOIN NULL row. + */ + case TK_IF_NULL_ROW: { + int addrINR; + u8 okConstFactor = pParse->okConstFactor; + AggInfo *pAggInfo = pExpr->pAggInfo; + if( pAggInfo ){ + assert( pExpr->iAgg>=0 && pExpr->iAggnColumn ); + if( !pAggInfo->directMode ){ + inReg = AggInfoColumnReg(pAggInfo, pExpr->iAgg); + break; + } + if( pExpr->pAggInfo->useSortingIdx ){ + sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab, + pAggInfo->aCol[pExpr->iAgg].iSorterColumn, + target); + inReg = target; + break; + } + } + addrINR = sqlite3VdbeAddOp3(v, OP_IfNullRow, pExpr->iTable, 0, target); + /* The OP_IfNullRow opcode above can overwrite the result register with + ** NULL. So we have to ensure that the result register is not a value + ** that is suppose to be a constant. Two defenses are needed: + ** (1) Temporarily disable factoring of constant expressions + ** (2) Make sure the computed value really is stored in register + ** "target" and not someplace else. + */ + pParse->okConstFactor = 0; /* note (1) above */ + sqlite3ExprCode(pParse, pExpr->pLeft, target); + assert( target==inReg ); + pParse->okConstFactor = okConstFactor; + sqlite3VdbeJumpHere(v, addrINR); + break; + } /* ** Form A: @@ -3156,7 +5642,7 @@ int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){ ** or if there is no matching Ei, the ELSE term Y, or if there is ** no ELSE term, NULL. */ - default: assert( op==TK_CASE ); { + case TK_CASE: { int endLabel; /* GOTO label for end of CASE stmt */ int nextCase; /* GOTO label for next WHEN clause */ int nExpr; /* 2x number of WHEN terms */ @@ -3166,21 +5652,27 @@ int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){ Expr opCompare; /* The X==Ei expression */ Expr *pX; /* The X expression */ Expr *pTest = 0; /* X==Ei (form A) or just Ei (form B) */ - VVA_ONLY( int iCacheLevel = pParse->iCacheLevel; ) + Expr *pDel = 0; + sqlite3 *db = pParse->db; - assert( !ExprHasProperty(pExpr, EP_xIsSelect) && pExpr->x.pList ); + assert( ExprUseXList(pExpr) && pExpr->x.pList!=0 ); assert(pExpr->x.pList->nExpr > 0); pEList = pExpr->x.pList; aListelem = pEList->a; nExpr = pEList->nExpr; - endLabel = sqlite3VdbeMakeLabel(v); + endLabel = sqlite3VdbeMakeLabel(pParse); if( (pX = pExpr->pLeft)!=0 ){ - tempX = *pX; + pDel = sqlite3ExprDup(db, pX, 0); + if( db->mallocFailed ){ + sqlite3ExprDelete(db, pDel); + break; + } testcase( pX->op==TK_COLUMN ); - exprToRegister(&tempX, sqlite3ExprCodeTemp(pParse, pX, ®Free1)); + sqlite3ExprToRegister(pDel, exprCodeVector(pParse, pDel, ®Free1)); testcase( regFree1==0 ); + memset(&opCompare, 0, sizeof(opCompare)); opCompare.op = TK_EQ; - opCompare.pLeft = &tempX; + opCompare.pLeft = pDel; pTest = &opCompare; /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001: ** The value in regFree1 might get SCopy-ed into the file result. @@ -3189,59 +5681,55 @@ int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){ regFree1 = 0; } for(i=0; iop==TK_COLUMN ); sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL); testcase( aListelem[i+1].pExpr->op==TK_COLUMN ); sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target); sqlite3VdbeGoto(v, endLabel); - sqlite3ExprCachePop(pParse); sqlite3VdbeResolveLabel(v, nextCase); } if( (nExpr&1)!=0 ){ - sqlite3ExprCachePush(pParse); sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target); - sqlite3ExprCachePop(pParse); }else{ sqlite3VdbeAddOp2(v, OP_Null, 0, target); } - assert( db->mallocFailed || pParse->nErr>0 - || pParse->iCacheLevel==iCacheLevel ); + sqlite3ExprDelete(db, pDel); + setDoNotMergeFlagOnCopy(v); sqlite3VdbeResolveLabel(v, endLabel); break; } #ifndef SQLITE_OMIT_TRIGGER case TK_RAISE: { - assert( pExpr->affinity==OE_Rollback - || pExpr->affinity==OE_Abort - || pExpr->affinity==OE_Fail - || pExpr->affinity==OE_Ignore + assert( pExpr->affExpr==OE_Rollback + || pExpr->affExpr==OE_Abort + || pExpr->affExpr==OE_Fail + || pExpr->affExpr==OE_Ignore ); - if( !pParse->pTriggerTab ){ + if( !pParse->pTriggerTab && !pParse->nested ){ sqlite3ErrorMsg(pParse, "RAISE() may only be used within a trigger-program"); return 0; } - if( pExpr->affinity==OE_Abort ){ + if( pExpr->affExpr==OE_Abort ){ sqlite3MayAbort(pParse); } assert( !ExprHasProperty(pExpr, EP_IntValue) ); - if( pExpr->affinity==OE_Ignore ){ - sqlite3VdbeAddOp4( - v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0); + if( pExpr->affExpr==OE_Ignore ){ + sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_OK, OE_Ignore); VdbeCoverage(v); }else{ - sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_TRIGGER, - pExpr->affinity, pExpr->u.zToken, 0, 0); + r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); + sqlite3VdbeAddOp3(v, OP_Halt, + pParse->pTriggerTab ? SQLITE_CONSTRAINT_TRIGGER : SQLITE_ERROR, + pExpr->affExpr, r1); } - break; } #endif @@ -3252,25 +5740,86 @@ int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){ } /* -** Factor out the code of the given expression to initialization time. +** Generate code that will evaluate expression pExpr just one time +** per prepared statement execution. +** +** If the expression uses functions (that might throw an exception) then +** guard them with an OP_Once opcode to ensure that the code is only executed +** once. If no functions are involved, then factor the code out and put it at +** the end of the prepared statement in the initialization section. +** +** If regDest>0 then the result is always stored in that register and the +** result is not reusable. If regDest<0 then this routine is free to +** store the value wherever it wants. The register where the expression +** is stored is returned. When regDest<0, two identical expressions might +** code to the same register, if they do not contain function calls and hence +** are factored out into the initialization section at the end of the +** prepared statement. */ -void sqlite3ExprCodeAtInit( +int sqlite3ExprCodeRunJustOnce( Parse *pParse, /* Parsing context */ Expr *pExpr, /* The expression to code when the VDBE initializes */ - int regDest, /* Store the value in this register */ - u8 reusable /* True if this expression is reusable */ + int regDest /* Store the value in this register */ ){ ExprList *p; assert( ConstFactorOk(pParse) ); + assert( regDest!=0 ); p = pParse->pConstExpr; + if( regDest<0 && p ){ + struct ExprList_item *pItem; + int i; + for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){ + if( pItem->fg.reusable + && sqlite3ExprCompare(0,pItem->pExpr,pExpr,-1)==0 + ){ + return pItem->u.iConstExprReg; + } + } + } pExpr = sqlite3ExprDup(pParse->db, pExpr, 0); - p = sqlite3ExprListAppend(pParse, p, pExpr); - if( p ){ - struct ExprList_item *pItem = &p->a[p->nExpr-1]; - pItem->u.iConstExprReg = regDest; - pItem->reusable = reusable; + if( pExpr!=0 && ExprHasProperty(pExpr, EP_HasFunc) ){ + Vdbe *v = pParse->pVdbe; + int addr; + assert( v ); + addr = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v); + pParse->okConstFactor = 0; + if( !pParse->db->mallocFailed ){ + if( regDest<0 ) regDest = ++pParse->nMem; + sqlite3ExprCode(pParse, pExpr, regDest); + } + pParse->okConstFactor = 1; + sqlite3ExprDelete(pParse->db, pExpr); + sqlite3VdbeJumpHere(v, addr); + }else{ + p = sqlite3ExprListAppend(pParse, p, pExpr); + if( p ){ + struct ExprList_item *pItem = &p->a[p->nExpr-1]; + pItem->fg.reusable = regDest<0; + if( regDest<0 ) regDest = ++pParse->nMem; + pItem->u.iConstExprReg = regDest; + } + pParse->pConstExpr = p; } - pParse->pConstExpr = p; + return regDest; +} + +/* +** Make arrangements to invoke OP_Null on a range of registers +** during initialization. +*/ +SQLITE_NOINLINE void sqlite3ExprNullRegisterRange( + Parse *pParse, /* Parsing context */ + int iReg, /* First register to set to NULL */ + int nReg /* Number of sequential registers to NULL out */ +){ + u8 okConstFactor = pParse->okConstFactor; + Expr t; + memset(&t, 0, sizeof(t)); + t.op = TK_NULLS; + t.y.nReg = nReg; + pParse->okConstFactor = 1; + sqlite3ExprCodeRunJustOnce(pParse, &t, iReg); + pParse->okConstFactor = okConstFactor; } /* @@ -3288,24 +5837,14 @@ void sqlite3ExprCodeAtInit( */ int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){ int r2; - pExpr = sqlite3ExprSkipCollate(pExpr); + pExpr = sqlite3ExprSkipCollateAndLikely(pExpr); if( ConstFactorOk(pParse) + && ALWAYS(pExpr!=0) && pExpr->op!=TK_REGISTER - && sqlite3ExprIsConstantNotJoin(pExpr) + && sqlite3ExprIsConstantNotJoin(pParse, pExpr) ){ - ExprList *p = pParse->pConstExpr; - int i; *pReg = 0; - if( p ){ - struct ExprList_item *pItem; - for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){ - if( pItem->reusable && sqlite3ExprCompare(pItem->pExpr,pExpr,-1)==0 ){ - return pItem->u.iConstExprReg; - } - } - } - r2 = ++pParse->nMem; - sqlite3ExprCodeAtInit(pParse, pExpr, r2, 1); + r2 = sqlite3ExprCodeRunJustOnce(pParse, pExpr, -1); }else{ int r1 = sqlite3GetTempReg(pParse); r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1); @@ -3327,15 +5866,23 @@ int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){ void sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){ int inReg; + assert( pExpr==0 || !ExprHasVVAProperty(pExpr,EP_Immutable) ); assert( target>0 && target<=pParse->nMem ); - if( pExpr && pExpr->op==TK_REGISTER ){ - sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, pExpr->iTable, target); - }else{ - inReg = sqlite3ExprCodeTarget(pParse, pExpr, target); - assert( pParse->pVdbe!=0 || pParse->db->mallocFailed ); - if( inReg!=target && pParse->pVdbe ){ - sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target); + assert( pParse->pVdbe!=0 || pParse->db->mallocFailed ); + if( pParse->pVdbe==0 ) return; + inReg = sqlite3ExprCodeTarget(pParse, pExpr, target); + if( inReg!=target ){ + u8 op; + Expr *pX = sqlite3ExprSkipCollateAndLikely(pExpr); + testcase( pX!=pExpr ); + if( ALWAYS(pX) + && (ExprHasProperty(pX,EP_Subquery) || pX->op==TK_REGISTER) + ){ + op = OP_Copy; + }else{ + op = OP_SCopy; } + sqlite3VdbeAddOp2(pParse->pVdbe, op, inReg, target); } } @@ -3358,42 +5905,20 @@ void sqlite3ExprCodeCopy(Parse *pParse, Expr *pExpr, int target){ ** might choose to code the expression at initialization time. */ void sqlite3ExprCodeFactorable(Parse *pParse, Expr *pExpr, int target){ - if( pParse->okConstFactor && sqlite3ExprIsConstant(pExpr) ){ - sqlite3ExprCodeAtInit(pParse, pExpr, target, 0); + if( pParse->okConstFactor && sqlite3ExprIsConstantNotJoin(pParse,pExpr) ){ + sqlite3ExprCodeRunJustOnce(pParse, pExpr, target); }else{ - sqlite3ExprCode(pParse, pExpr, target); + sqlite3ExprCodeCopy(pParse, pExpr, target); } } -/* -** Generate code that evaluates the given expression and puts the result -** in register target. -** -** Also make a copy of the expression results into another "cache" register -** and modify the expression so that the next time it is evaluated, -** the result is a copy of the cache register. -** -** This routine is used for expressions that are used multiple -** times. They are evaluated once and the results of the expression -** are reused. -*/ -void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){ - Vdbe *v = pParse->pVdbe; - int iMem; - - assert( target>0 ); - assert( pExpr->op!=TK_REGISTER ); - sqlite3ExprCode(pParse, pExpr, target); - iMem = ++pParse->nMem; - sqlite3VdbeAddOp2(v, OP_Copy, target, iMem); - exprToRegister(pExpr, iMem); -} - /* ** Generate code that pushes the value of every element of the given ** expression list into a sequence of registers beginning at target. ** -** Return the number of elements evaluated. +** Return the number of elements evaluated. The number returned will +** usually be pList->nExpr but might be reduced if SQLITE_ECEL_OMITREF +** is defined. ** ** The SQLITE_ECEL_DUP flag prevents the arguments from being ** filled using OP_SCopy. OP_Copy must be used instead. @@ -3404,6 +5929,8 @@ void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){ ** The SQLITE_ECEL_REF flag means that expressions in the list with ** ExprList.a[].u.x.iOrderByCol>0 have already been evaluated and stored ** in registers at srcReg, and so the value can be copied from there. +** If SQLITE_ECEL_OMITREF is also set, then the values with u.x.iOrderByCol>0 +** are simply omitted rather than being copied from srcReg. */ int sqlite3ExprCodeExprList( Parse *pParse, /* Parsing context */ @@ -3423,18 +5950,32 @@ int sqlite3ExprCodeExprList( if( !ConstFactorOk(pParse) ) flags &= ~SQLITE_ECEL_FACTOR; for(pItem=pList->a, i=0; ipExpr; - if( (flags & SQLITE_ECEL_REF)!=0 && (j = pList->a[i].u.x.iOrderByCol)>0 ){ - sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i); - }else if( (flags & SQLITE_ECEL_FACTOR)!=0 && sqlite3ExprIsConstant(pExpr) ){ - sqlite3ExprCodeAtInit(pParse, pExpr, target+i, 0); +#ifdef SQLITE_ENABLE_SORTER_REFERENCES + if( pItem->fg.bSorterRef ){ + i--; + n--; + }else +#endif + if( (flags & SQLITE_ECEL_REF)!=0 && (j = pItem->u.x.iOrderByCol)>0 ){ + if( flags & SQLITE_ECEL_OMITREF ){ + i--; + n--; + }else{ + sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i); + } + }else if( (flags & SQLITE_ECEL_FACTOR)!=0 + && sqlite3ExprIsConstantNotJoin(pParse,pExpr) + ){ + sqlite3ExprCodeRunJustOnce(pParse, pExpr, target+i); }else{ int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i); if( inReg!=target+i ){ VdbeOp *pOp; if( copyOp==OP_Copy - && (pOp=sqlite3VdbeGetOp(v, -1))->opcode==OP_Copy + && (pOp=sqlite3VdbeGetLastOp(v))->opcode==OP_Copy && pOp->p1+pOp->p3+1==inReg && pOp->p2+pOp->p3+1==target+i + && pOp->p5==0 /* The do-not-merge flag must be clear */ ){ pOp->p3++; }else{ @@ -3451,54 +5992,77 @@ int sqlite3ExprCodeExprList( ** ** x BETWEEN y AND z ** -** The above is equivalent to +** The above is equivalent to ** ** x>=y AND x<=z ** ** Code it as such, taking care to do the common subexpression ** elimination of x. +** +** The xJumpIf parameter determines details: +** +** NULL: Store the boolean result in reg[dest] +** sqlite3ExprIfTrue: Jump to dest if true +** sqlite3ExprIfFalse: Jump to dest if false +** +** The jumpIfNull parameter is ignored if xJumpIf is NULL. */ static void exprCodeBetween( Parse *pParse, /* Parsing and code generating context */ Expr *pExpr, /* The BETWEEN expression */ - int dest, /* Jump here if the jump is taken */ - int jumpIfTrue, /* Take the jump if the BETWEEN is true */ + int dest, /* Jump destination or storage location */ + void (*xJump)(Parse*,Expr*,int,int), /* Action to take */ int jumpIfNull /* Take the jump if the BETWEEN is NULL */ ){ Expr exprAnd; /* The AND operator in x>=y AND x<=z */ Expr compLeft; /* The x>=y term */ Expr compRight; /* The x<=z term */ - Expr exprX; /* The x subexpression */ int regFree1 = 0; /* Temporary use register */ + Expr *pDel = 0; + sqlite3 *db = pParse->db; - assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); - exprX = *pExpr->pLeft; - exprAnd.op = TK_AND; - exprAnd.pLeft = &compLeft; - exprAnd.pRight = &compRight; - compLeft.op = TK_GE; - compLeft.pLeft = &exprX; - compLeft.pRight = pExpr->x.pList->a[0].pExpr; - compRight.op = TK_LE; - compRight.pLeft = &exprX; - compRight.pRight = pExpr->x.pList->a[1].pExpr; - exprToRegister(&exprX, sqlite3ExprCodeTemp(pParse, &exprX, ®Free1)); - if( jumpIfTrue ){ - sqlite3ExprIfTrue(pParse, &exprAnd, dest, jumpIfNull); - }else{ - sqlite3ExprIfFalse(pParse, &exprAnd, dest, jumpIfNull); + memset(&compLeft, 0, sizeof(Expr)); + memset(&compRight, 0, sizeof(Expr)); + memset(&exprAnd, 0, sizeof(Expr)); + + assert( ExprUseXList(pExpr) ); + pDel = sqlite3ExprDup(db, pExpr->pLeft, 0); + if( db->mallocFailed==0 ){ + exprAnd.op = TK_AND; + exprAnd.pLeft = &compLeft; + exprAnd.pRight = &compRight; + compLeft.op = TK_GE; + compLeft.pLeft = pDel; + compLeft.pRight = pExpr->x.pList->a[0].pExpr; + compRight.op = TK_LE; + compRight.pLeft = pDel; + compRight.pRight = pExpr->x.pList->a[1].pExpr; + sqlite3ExprToRegister(pDel, exprCodeVector(pParse, pDel, ®Free1)); + if( xJump ){ + xJump(pParse, &exprAnd, dest, jumpIfNull); + }else{ + /* Mark the expression is being from the ON or USING clause of a join + ** so that the sqlite3ExprCodeTarget() routine will not attempt to move + ** it into the Parse.pConstExpr list. We should use a new bit for this, + ** for clarity, but we are out of bits in the Expr.flags field so we + ** have to reuse the EP_OuterON bit. Bummer. */ + pDel->flags |= EP_OuterON; + sqlite3ExprCodeTarget(pParse, &exprAnd, dest); + } + sqlite3ReleaseTempReg(pParse, regFree1); } - sqlite3ReleaseTempReg(pParse, regFree1); + sqlite3ExprDelete(db, pDel); /* Ensure adequate test coverage */ - testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1==0 ); - testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1!=0 ); - testcase( jumpIfTrue==0 && jumpIfNull!=0 && regFree1==0 ); - testcase( jumpIfTrue==0 && jumpIfNull!=0 && regFree1!=0 ); - testcase( jumpIfTrue!=0 && jumpIfNull==0 && regFree1==0 ); - testcase( jumpIfTrue!=0 && jumpIfNull==0 && regFree1!=0 ); - testcase( jumpIfTrue!=0 && jumpIfNull!=0 && regFree1==0 ); - testcase( jumpIfTrue!=0 && jumpIfNull!=0 && regFree1!=0 ); + testcase( xJump==sqlite3ExprIfTrue && jumpIfNull==0 && regFree1==0 ); + testcase( xJump==sqlite3ExprIfTrue && jumpIfNull==0 && regFree1!=0 ); + testcase( xJump==sqlite3ExprIfTrue && jumpIfNull!=0 && regFree1==0 ); + testcase( xJump==sqlite3ExprIfTrue && jumpIfNull!=0 && regFree1!=0 ); + testcase( xJump==sqlite3ExprIfFalse && jumpIfNull==0 && regFree1==0 ); + testcase( xJump==sqlite3ExprIfFalse && jumpIfNull==0 && regFree1!=0 ); + testcase( xJump==sqlite3ExprIfFalse && jumpIfNull!=0 && regFree1==0 ); + testcase( xJump==sqlite3ExprIfFalse && jumpIfNull!=0 && regFree1!=0 ); + testcase( xJump==0 ); } /* @@ -3525,24 +6089,36 @@ void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 ); if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */ if( NEVER(pExpr==0) ) return; /* No way this can happen */ + assert( !ExprHasVVAProperty(pExpr, EP_Immutable) ); op = pExpr->op; switch( op ){ - case TK_AND: { - int d2 = sqlite3VdbeMakeLabel(v); - testcase( jumpIfNull==0 ); - sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL); - sqlite3ExprCachePush(pParse); - sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); - sqlite3VdbeResolveLabel(v, d2); - sqlite3ExprCachePop(pParse); - break; - } + case TK_AND: case TK_OR: { - testcase( jumpIfNull==0 ); - sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); - sqlite3ExprCachePush(pParse); - sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); - sqlite3ExprCachePop(pParse); + Expr *pAlt = sqlite3ExprSimplifiedAndOr(pExpr); + if( pAlt!=pExpr ){ + sqlite3ExprIfTrue(pParse, pAlt, dest, jumpIfNull); + }else{ + Expr *pFirst, *pSecond; + if( exprEvalRhsFirst(pExpr) ){ + pFirst = pExpr->pRight; + pSecond = pExpr->pLeft; + }else{ + pFirst = pExpr->pLeft; + pSecond = pExpr->pRight; + } + if( op==TK_AND ){ + int d2 = sqlite3VdbeMakeLabel(pParse); + testcase( jumpIfNull==0 ); + sqlite3ExprIfFalse(pParse, pFirst, d2, + jumpIfNull^SQLITE_JUMPIFNULL); + sqlite3ExprIfTrue(pParse, pSecond, dest, jumpIfNull); + sqlite3VdbeResolveLabel(v, d2); + }else{ + testcase( jumpIfNull==0 ); + sqlite3ExprIfTrue(pParse, pFirst, dest, jumpIfNull); + sqlite3ExprIfTrue(pParse, pSecond, dest, jumpIfNull); + } + } break; } case TK_NOT: { @@ -3550,40 +6126,67 @@ void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); break; } + case TK_TRUTH: { + int isNot; /* IS NOT TRUE or IS NOT FALSE */ + int isTrue; /* IS TRUE or IS NOT TRUE */ + testcase( jumpIfNull==0 ); + isNot = pExpr->op2==TK_ISNOT; + isTrue = sqlite3ExprTruthValue(pExpr->pRight); + testcase( isTrue && isNot ); + testcase( !isTrue && isNot ); + if( isTrue ^ isNot ){ + sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, + isNot ? SQLITE_JUMPIFNULL : 0); + }else{ + sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, + isNot ? SQLITE_JUMPIFNULL : 0); + } + break; + } + case TK_IS: + case TK_ISNOT: + testcase( op==TK_IS ); + testcase( op==TK_ISNOT ); + op = (op==TK_IS) ? TK_EQ : TK_NE; + jumpIfNull = SQLITE_NULLEQ; + /* no break */ deliberate_fall_through case TK_LT: case TK_LE: case TK_GT: case TK_GE: case TK_NE: case TK_EQ: { - testcase( jumpIfNull==0 ); - r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); - r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); + int addrIsNull; + if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr; + if( ExprHasProperty(pExpr, EP_Subquery) && jumpIfNull!=SQLITE_NULLEQ ){ + addrIsNull = exprComputeOperands(pParse, pExpr, + &r1, &r2, ®Free1, ®Free2); + }else{ + r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); + r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); + addrIsNull = 0; + } codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, - r1, r2, dest, jumpIfNull); + r1, r2, dest, jumpIfNull, ExprHasProperty(pExpr,EP_Commuted)); assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt); assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le); assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt); assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge); - assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq); - assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne); - testcase( regFree1==0 ); - testcase( regFree2==0 ); - break; - } - case TK_IS: - case TK_ISNOT: { - testcase( op==TK_IS ); - testcase( op==TK_ISNOT ); - r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); - r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); - op = (op==TK_IS) ? TK_EQ : TK_NE; - codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, - r1, r2, dest, SQLITE_NULLEQ); - VdbeCoverageIf(v, op==TK_EQ); - VdbeCoverageIf(v, op==TK_NE); + assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); + VdbeCoverageIf(v, op==OP_Eq && jumpIfNull==SQLITE_NULLEQ); + VdbeCoverageIf(v, op==OP_Eq && jumpIfNull!=SQLITE_NULLEQ); + assert(TK_NE==OP_Ne); testcase(op==OP_Ne); + VdbeCoverageIf(v, op==OP_Ne && jumpIfNull==SQLITE_NULLEQ); + VdbeCoverageIf(v, op==OP_Ne && jumpIfNull!=SQLITE_NULLEQ); testcase( regFree1==0 ); testcase( regFree2==0 ); + if( addrIsNull ){ + if( jumpIfNull ){ + sqlite3VdbeChangeP2(v, addrIsNull, dest); + }else{ + sqlite3VdbeJumpHere(v, addrIsNull); + } + } break; } case TK_ISNULL: @@ -3591,20 +6194,21 @@ void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL ); assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL ); r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); + assert( regFree1==0 || regFree1==r1 ); + if( regFree1 ) sqlite3VdbeTypeofColumn(v, r1); sqlite3VdbeAddOp2(v, op, r1, dest); VdbeCoverageIf(v, op==TK_ISNULL); VdbeCoverageIf(v, op==TK_NOTNULL); - testcase( regFree1==0 ); break; } case TK_BETWEEN: { testcase( jumpIfNull==0 ); - exprCodeBetween(pParse, pExpr, dest, 1, jumpIfNull); + exprCodeBetween(pParse, pExpr, dest, sqlite3ExprIfTrue, jumpIfNull); break; } #ifndef SQLITE_OMIT_SUBQUERY case TK_IN: { - int destIfFalse = sqlite3VdbeMakeLabel(v); + int destIfFalse = sqlite3VdbeMakeLabel(pParse); int destIfNull = jumpIfNull ? dest : destIfFalse; sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull); sqlite3VdbeGoto(v, dest); @@ -3613,9 +6217,10 @@ void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ } #endif default: { - if( exprAlwaysTrue(pExpr) ){ + default_expr: + if( ExprAlwaysTrue(pExpr) ){ sqlite3VdbeGoto(v, dest); - }else if( exprAlwaysFalse(pExpr) ){ + }else if( ExprAlwaysFalse(pExpr) ){ /* No-op */ }else{ r1 = sqlite3ExprCodeTemp(pParse, pExpr, ®Free1); @@ -3628,7 +6233,7 @@ void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ } } sqlite3ReleaseTempReg(pParse, regFree1); - sqlite3ReleaseTempReg(pParse, regFree2); + sqlite3ReleaseTempReg(pParse, regFree2); } /* @@ -3650,6 +6255,7 @@ void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 ); if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */ if( pExpr==0 ) return; + assert( !ExprHasVVAProperty(pExpr,EP_Immutable) ); /* The value of pExpr->op and op are related as follows: ** @@ -3683,22 +6289,33 @@ void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ assert( pExpr->op!=TK_GE || op==OP_Lt ); switch( pExpr->op ){ - case TK_AND: { - testcase( jumpIfNull==0 ); - sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); - sqlite3ExprCachePush(pParse); - sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); - sqlite3ExprCachePop(pParse); - break; - } + case TK_AND: case TK_OR: { - int d2 = sqlite3VdbeMakeLabel(v); - testcase( jumpIfNull==0 ); - sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL); - sqlite3ExprCachePush(pParse); - sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); - sqlite3VdbeResolveLabel(v, d2); - sqlite3ExprCachePop(pParse); + Expr *pAlt = sqlite3ExprSimplifiedAndOr(pExpr); + if( pAlt!=pExpr ){ + sqlite3ExprIfFalse(pParse, pAlt, dest, jumpIfNull); + }else{ + Expr *pFirst, *pSecond; + if( exprEvalRhsFirst(pExpr) ){ + pFirst = pExpr->pRight; + pSecond = pExpr->pLeft; + }else{ + pFirst = pExpr->pLeft; + pSecond = pExpr->pRight; + } + if( pExpr->op==TK_AND ){ + testcase( jumpIfNull==0 ); + sqlite3ExprIfFalse(pParse, pFirst, dest, jumpIfNull); + sqlite3ExprIfFalse(pParse, pSecond, dest, jumpIfNull); + }else{ + int d2 = sqlite3VdbeMakeLabel(pParse); + testcase( jumpIfNull==0 ); + sqlite3ExprIfTrue(pParse, pFirst, d2, + jumpIfNull^SQLITE_JUMPIFNULL); + sqlite3ExprIfFalse(pParse, pSecond, dest, jumpIfNull); + sqlite3VdbeResolveLabel(v, d2); + } + } break; } case TK_NOT: { @@ -3706,54 +6323,85 @@ void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); break; } + case TK_TRUTH: { + int isNot; /* IS NOT TRUE or IS NOT FALSE */ + int isTrue; /* IS TRUE or IS NOT TRUE */ + testcase( jumpIfNull==0 ); + isNot = pExpr->op2==TK_ISNOT; + isTrue = sqlite3ExprTruthValue(pExpr->pRight); + testcase( isTrue && isNot ); + testcase( !isTrue && isNot ); + if( isTrue ^ isNot ){ + /* IS TRUE and IS NOT FALSE */ + sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, + isNot ? 0 : SQLITE_JUMPIFNULL); + + }else{ + /* IS FALSE and IS NOT TRUE */ + sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, + isNot ? 0 : SQLITE_JUMPIFNULL); + } + break; + } + case TK_IS: + case TK_ISNOT: + testcase( pExpr->op==TK_IS ); + testcase( pExpr->op==TK_ISNOT ); + op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ; + jumpIfNull = SQLITE_NULLEQ; + /* no break */ deliberate_fall_through case TK_LT: case TK_LE: case TK_GT: case TK_GE: case TK_NE: case TK_EQ: { - testcase( jumpIfNull==0 ); - r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); - r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); + int addrIsNull; + if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr; + if( ExprHasProperty(pExpr, EP_Subquery) && jumpIfNull!=SQLITE_NULLEQ ){ + addrIsNull = exprComputeOperands(pParse, pExpr, + &r1, &r2, ®Free1, ®Free2); + }else{ + r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); + r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); + addrIsNull = 0; + } codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, - r1, r2, dest, jumpIfNull); + r1, r2, dest, jumpIfNull,ExprHasProperty(pExpr,EP_Commuted)); assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt); assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le); assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt); assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge); - assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq); - assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne); - testcase( regFree1==0 ); - testcase( regFree2==0 ); - break; - } - case TK_IS: - case TK_ISNOT: { - testcase( pExpr->op==TK_IS ); - testcase( pExpr->op==TK_ISNOT ); - r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); - r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); - op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ; - codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, - r1, r2, dest, SQLITE_NULLEQ); - VdbeCoverageIf(v, op==TK_EQ); - VdbeCoverageIf(v, op==TK_NE); + assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); + VdbeCoverageIf(v, op==OP_Eq && jumpIfNull!=SQLITE_NULLEQ); + VdbeCoverageIf(v, op==OP_Eq && jumpIfNull==SQLITE_NULLEQ); + assert(TK_NE==OP_Ne); testcase(op==OP_Ne); + VdbeCoverageIf(v, op==OP_Ne && jumpIfNull!=SQLITE_NULLEQ); + VdbeCoverageIf(v, op==OP_Ne && jumpIfNull==SQLITE_NULLEQ); testcase( regFree1==0 ); testcase( regFree2==0 ); + if( addrIsNull ){ + if( jumpIfNull ){ + sqlite3VdbeChangeP2(v, addrIsNull, dest); + }else{ + sqlite3VdbeJumpHere(v, addrIsNull); + } + } break; } case TK_ISNULL: case TK_NOTNULL: { r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); + assert( regFree1==0 || regFree1==r1 ); + if( regFree1 ) sqlite3VdbeTypeofColumn(v, r1); sqlite3VdbeAddOp2(v, op, r1, dest); testcase( op==TK_ISNULL ); VdbeCoverageIf(v, op==TK_ISNULL); testcase( op==TK_NOTNULL ); VdbeCoverageIf(v, op==TK_NOTNULL); - testcase( regFree1==0 ); break; } case TK_BETWEEN: { testcase( jumpIfNull==0 ); - exprCodeBetween(pParse, pExpr, dest, 0, jumpIfNull); + exprCodeBetween(pParse, pExpr, dest, sqlite3ExprIfFalse, jumpIfNull); break; } #ifndef SQLITE_OMIT_SUBQUERY @@ -3761,7 +6409,7 @@ void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ if( jumpIfNull ){ sqlite3ExprCodeIN(pParse, pExpr, dest, dest); }else{ - int destIfNull = sqlite3VdbeMakeLabel(v); + int destIfNull = sqlite3VdbeMakeLabel(pParse); sqlite3ExprCodeIN(pParse, pExpr, dest, destIfNull); sqlite3VdbeResolveLabel(v, destIfNull); } @@ -3769,9 +6417,10 @@ void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ } #endif default: { - if( exprAlwaysFalse(pExpr) ){ + default_expr: + if( ExprAlwaysFalse(pExpr) ){ sqlite3VdbeGoto(v, dest); - }else if( exprAlwaysTrue(pExpr) ){ + }else if( ExprAlwaysTrue(pExpr) ){ /* no-op */ }else{ r1 = sqlite3ExprCodeTemp(pParse, pExpr, ®Free1); @@ -3801,6 +6450,51 @@ void sqlite3ExprIfFalseDup(Parse *pParse, Expr *pExpr, int dest,int jumpIfNull){ sqlite3ExprDelete(db, pCopy); } +/* +** Expression pVar is guaranteed to be an SQL variable. pExpr may be any +** type of expression. +** +** If pExpr is a simple SQL value - an integer, real, string, blob +** or NULL value - then the VDBE currently being prepared is configured +** to re-prepare each time a new value is bound to variable pVar. +** +** Additionally, if pExpr is a simple SQL value and the value is the +** same as that currently bound to variable pVar, non-zero is returned. +** Otherwise, if the values are not the same or if pExpr is not a simple +** SQL value, zero is returned. +** +** If the SQLITE_EnableQPSG flag is set on the database connection, then +** this routine always returns false. +*/ +static SQLITE_NOINLINE int exprCompareVariable( + const Parse *pParse, + const Expr *pVar, + const Expr *pExpr +){ + int res = 2; + int iVar; + sqlite3_value *pL, *pR = 0; + + if( pExpr->op==TK_VARIABLE && pVar->iColumn==pExpr->iColumn ){ + return 0; + } + if( (pParse->db->flags & SQLITE_EnableQPSG)!=0 ) return 2; + sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, SQLITE_AFF_BLOB, &pR); + if( pR ){ + iVar = pVar->iColumn; + sqlite3VdbeSetVarmask(pParse->pVdbe, iVar); + pL = sqlite3VdbeGetBoundValue(pParse->pReprepare, iVar, SQLITE_AFF_BLOB); + if( pL ){ + if( sqlite3_value_type(pL)==SQLITE_TEXT ){ + sqlite3_value_text(pL); /* Make sure the encoding is UTF-8 */ + } + res = sqlite3MemCompare(pL, pR, 0) ? 2 : 0; + } + sqlite3ValueFree(pR); + sqlite3ValueFree(pL); + } + return res; +} /* ** Do a deep comparison of two expression trees. Return 0 if the two @@ -3823,12 +6517,25 @@ void sqlite3ExprIfFalseDup(Parse *pParse, Expr *pExpr, int dest,int jumpIfNull){ ** this routine is used, it does not hurt to get an extra 2 - that ** just might result in some slightly slower code. But returning ** an incorrect 0 or 1 could lead to a malfunction. +** +** If pParse is not NULL and SQLITE_EnableQPSG is off then TK_VARIABLE +** terms in pA with bindings in pParse->pReprepare can be matched against +** literals in pB. The pParse->pVdbe->expmask bitmask is updated for +** each variable referenced. */ -int sqlite3ExprCompare(Expr *pA, Expr *pB, int iTab){ +int sqlite3ExprCompare( + const Parse *pParse, + const Expr *pA, + const Expr *pB, + int iTab +){ u32 combinedFlags; if( pA==0 || pB==0 ){ return pB==pA ? 0 : 2; } + if( pParse && pA->op==TK_VARIABLE ){ + return exprCompareVariable(pParse, pA, pB); + } combinedFlags = pA->flags | pB->flags; if( combinedFlags & EP_IntValue ){ if( (pA->flags&pB->flags&EP_IntValue)!=0 && pA->u.iValue==pB->u.iValue ){ @@ -3836,63 +6543,239 @@ int sqlite3ExprCompare(Expr *pA, Expr *pB, int iTab){ } return 2; } - if( pA->op!=pB->op ){ - if( pA->op==TK_COLLATE && sqlite3ExprCompare(pA->pLeft, pB, iTab)<2 ){ + if( pA->op!=pB->op || pA->op==TK_RAISE ){ + if( pA->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA->pLeft,pB,iTab)<2 ){ return 1; } - if( pB->op==TK_COLLATE && sqlite3ExprCompare(pA, pB->pLeft, iTab)<2 ){ + if( pB->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA,pB->pLeft,iTab)<2 ){ return 1; } - return 2; + if( pA->op==TK_AGG_COLUMN && pB->op==TK_COLUMN + && pB->iTable<0 && pA->iTable==iTab + ){ + /* fall through */ + }else{ + return 2; + } } - if( pA->op!=TK_COLUMN && pA->op!=TK_AGG_COLUMN && pA->u.zToken ){ - if( pA->op==TK_FUNCTION ){ + assert( !ExprHasProperty(pA, EP_IntValue) ); + assert( !ExprHasProperty(pB, EP_IntValue) ); + if( pA->u.zToken ){ + if( pA->op==TK_FUNCTION || pA->op==TK_AGG_FUNCTION ){ if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2; - }else if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){ - return pA->op==TK_COLLATE ? 1 : 2; +#ifndef SQLITE_OMIT_WINDOWFUNC + assert( pA->op==pB->op ); + if( ExprHasProperty(pA,EP_WinFunc)!=ExprHasProperty(pB,EP_WinFunc) ){ + return 2; + } + if( ExprHasProperty(pA,EP_WinFunc) ){ + if( sqlite3WindowCompare(pParse, pA->y.pWin, pB->y.pWin, 1)!=0 ){ + return 2; + } + } +#endif + }else if( pA->op==TK_NULL ){ + return 0; + }else if( pA->op==TK_COLLATE ){ + if( sqlite3_stricmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2; + }else + if( pB->u.zToken!=0 + && pA->op!=TK_COLUMN + && pA->op!=TK_AGG_COLUMN + && strcmp(pA->u.zToken,pB->u.zToken)!=0 + ){ + return 2; + } + } + if( (pA->flags & (EP_Distinct|EP_Commuted)) + != (pB->flags & (EP_Distinct|EP_Commuted)) ) return 2; + if( ALWAYS((combinedFlags & EP_TokenOnly)==0) ){ + if( combinedFlags & EP_xIsSelect ) return 2; + if( (combinedFlags & EP_FixedCol)==0 + && sqlite3ExprCompare(pParse, pA->pLeft, pB->pLeft, iTab) ) return 2; + if( sqlite3ExprCompare(pParse, pA->pRight, pB->pRight, iTab) ) return 2; + if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2; + if( pA->op!=TK_STRING + && pA->op!=TK_TRUEFALSE + && ALWAYS((combinedFlags & EP_Reduced)==0) + ){ + if( pA->iColumn!=pB->iColumn ) return 2; + if( pA->op2!=pB->op2 && pA->op==TK_TRUTH ) return 2; + if( pA->op!=TK_IN && pA->iTable!=pB->iTable && pA->iTable!=iTab ){ + return 2; + } + } + } + return 0; +} + +/* +** Compare two ExprList objects. Return 0 if they are identical, 1 +** if they are certainly different, or 2 if it is not possible to +** determine if they are identical or not. +** +** If any subelement of pB has Expr.iTable==(-1) then it is allowed +** to compare equal to an equivalent element in pA with Expr.iTable==iTab. +** +** This routine might return non-zero for equivalent ExprLists. The +** only consequence will be disabled optimizations. But this routine +** must never return 0 if the two ExprList objects are different, or +** a malfunction will result. +** +** Two NULL pointers are considered to be the same. But a NULL pointer +** always differs from a non-NULL pointer. +*/ +int sqlite3ExprListCompare(const ExprList *pA, const ExprList *pB, int iTab){ + int i; + if( pA==0 && pB==0 ) return 0; + if( pA==0 || pB==0 ) return 1; + if( pA->nExpr!=pB->nExpr ) return 1; + for(i=0; inExpr; i++){ + int res; + Expr *pExprA = pA->a[i].pExpr; + Expr *pExprB = pB->a[i].pExpr; + if( pA->a[i].fg.sortFlags!=pB->a[i].fg.sortFlags ) return 1; + if( (res = sqlite3ExprCompare(0, pExprA, pExprB, iTab)) ) return res; + } + return 0; +} + +/* +** Like sqlite3ExprCompare() except COLLATE operators at the top-level +** are ignored. +*/ +int sqlite3ExprCompareSkip(Expr *pA,Expr *pB, int iTab){ + return sqlite3ExprCompare(0, + sqlite3ExprSkipCollate(pA), + sqlite3ExprSkipCollate(pB), + iTab); +} + +/* +** Return non-zero if Expr p can only be true if pNN is not NULL. +** +** Or if seenNot is true, return non-zero if Expr p can only be +** non-NULL if pNN is not NULL +*/ +static int exprImpliesNotNull( + const Parse *pParse,/* Parsing context */ + const Expr *p, /* The expression to be checked */ + const Expr *pNN, /* The expression that is NOT NULL */ + int iTab, /* Table being evaluated */ + int seenNot /* Return true only if p can be any non-NULL value */ +){ + assert( p ); + assert( pNN ); + if( sqlite3ExprCompare(pParse, p, pNN, iTab)==0 ){ + return pNN->op!=TK_NULL; + } + switch( p->op ){ + case TK_IN: { + if( seenNot && ExprHasProperty(p, EP_xIsSelect) ) return 0; + assert( ExprUseXSelect(p) || (p->x.pList!=0 && p->x.pList->nExpr>0) ); + return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1); + } + case TK_BETWEEN: { + ExprList *pList; + assert( ExprUseXList(p) ); + pList = p->x.pList; + assert( pList!=0 ); + assert( pList->nExpr==2 ); + if( seenNot ) return 0; + if( exprImpliesNotNull(pParse, pList->a[0].pExpr, pNN, iTab, 1) + || exprImpliesNotNull(pParse, pList->a[1].pExpr, pNN, iTab, 1) + ){ + return 1; + } + return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1); + } + case TK_EQ: + case TK_NE: + case TK_LT: + case TK_LE: + case TK_GT: + case TK_GE: + case TK_PLUS: + case TK_MINUS: + case TK_BITOR: + case TK_LSHIFT: + case TK_RSHIFT: + case TK_CONCAT: + seenNot = 1; + /* no break */ deliberate_fall_through + case TK_STAR: + case TK_REM: + case TK_BITAND: + case TK_SLASH: { + if( exprImpliesNotNull(pParse, p->pRight, pNN, iTab, seenNot) ) return 1; + /* no break */ deliberate_fall_through + } + case TK_SPAN: + case TK_COLLATE: + case TK_UPLUS: + case TK_UMINUS: { + return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, seenNot); + } + case TK_TRUTH: { + if( seenNot ) return 0; + if( p->op2!=TK_IS ) return 0; + return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1); } - } - if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2; - if( ALWAYS((combinedFlags & EP_TokenOnly)==0) ){ - if( combinedFlags & EP_xIsSelect ) return 2; - if( sqlite3ExprCompare(pA->pLeft, pB->pLeft, iTab) ) return 2; - if( sqlite3ExprCompare(pA->pRight, pB->pRight, iTab) ) return 2; - if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2; - if( ALWAYS((combinedFlags & EP_Reduced)==0) && pA->op!=TK_STRING ){ - if( pA->iColumn!=pB->iColumn ) return 2; - if( pA->iTable!=pB->iTable - && (pA->iTable!=iTab || NEVER(pB->iTable>=0)) ) return 2; + case TK_BITNOT: + case TK_NOT: { + return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1); } } return 0; } /* -** Compare two ExprList objects. Return 0 if they are identical and -** non-zero if they differ in any way. -** -** If any subelement of pB has Expr.iTable==(-1) then it is allowed -** to compare equal to an equivalent element in pA with Expr.iTable==iTab. -** -** This routine might return non-zero for equivalent ExprLists. The -** only consequence will be disabled optimizations. But this routine -** must never return 0 if the two ExprList objects are different, or -** a malfunction will result. +** Return true if the boolean value of the expression is always either +** FALSE or NULL. +*/ +static int sqlite3ExprIsNotTrue(Expr *pExpr){ + int v; + if( pExpr->op==TK_NULL ) return 1; + if( pExpr->op==TK_TRUEFALSE && sqlite3ExprTruthValue(pExpr)==0 ) return 1; + v = 1; + if( sqlite3ExprIsInteger(pExpr, &v, 0) && v==0 ) return 1; + return 0; +} + +/* +** Return true if the expression is one of the following: ** -** Two NULL pointers are considered to be the same. But a NULL pointer -** always differs from a non-NULL pointer. +** CASE WHEN x THEN y END +** CASE WHEN x THEN y ELSE NULL END +** CASE WHEN x THEN y ELSE false END +** iif(x,y) +** iif(x,y,NULL) +** iif(x,y,false) */ -int sqlite3ExprListCompare(ExprList *pA, ExprList *pB, int iTab){ - int i; - if( pA==0 && pB==0 ) return 0; - if( pA==0 || pB==0 ) return 1; - if( pA->nExpr!=pB->nExpr ) return 1; - for(i=0; inExpr; i++){ - Expr *pExprA = pA->a[i].pExpr; - Expr *pExprB = pB->a[i].pExpr; - if( pA->a[i].sortOrder!=pB->a[i].sortOrder ) return 1; - if( sqlite3ExprCompare(pExprA, pExprB, iTab) ) return 1; +static int sqlite3ExprIsIIF(sqlite3 *db, const Expr *pExpr){ + ExprList *pList; + if( pExpr->op==TK_FUNCTION ){ + const char *z = pExpr->u.zToken; + FuncDef *pDef; + if( (z[0]!='i' && z[0]!='I') ) return 0; + if( pExpr->x.pList==0 ) return 0; + pDef = sqlite3FindFunction(db, z, pExpr->x.pList->nExpr, ENC(db), 0); +#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION + if( pDef==0 ) return 0; +#else + if( NEVER(pDef==0) ) return 0; +#endif + if( (pDef->funcFlags & SQLITE_FUNC_INLINE)==0 ) return 0; + if( SQLITE_PTR_TO_INT(pDef->pUserData)!=INLINEFUNC_iif ) return 0; + }else if( pExpr->op==TK_CASE ){ + if( pExpr->pLeft!=0 ) return 0; + }else{ + return 0; } + pList = pExpr->x.pList; + assert( pList!=0 ); + if( pList->nExpr==2 ) return 1; + if( pList->nExpr==3 && sqlite3ExprIsNotTrue(pList->a[2].pExpr) ) return 1; return 0; } @@ -3901,96 +6784,470 @@ int sqlite3ExprListCompare(ExprList *pA, ExprList *pB, int iTab){ ** true. Return false if we cannot complete the proof or if pE2 might ** be false. Examples: ** -** pE1: x==5 pE2: x==5 Result: true -** pE1: x>0 pE2: x==5 Result: false -** pE1: x=21 pE2: x=21 OR y=43 Result: true -** pE1: x!=123 pE2: x IS NOT NULL Result: true -** pE1: x!=?1 pE2: x IS NOT NULL Result: true -** pE1: x IS NULL pE2: x IS NOT NULL Result: false -** pE1: x IS ?2 pE2: x IS NOT NULL Reuslt: false +** pE1: x==5 pE2: x==5 Result: true +** pE1: x>0 pE2: x==5 Result: false +** pE1: x=21 pE2: x=21 OR y=43 Result: true +** pE1: x!=123 pE2: x IS NOT NULL Result: true +** pE1: x!=?1 pE2: x IS NOT NULL Result: true +** pE1: x IS NULL pE2: x IS NOT NULL Result: false +** pE1: x IS ?2 pE2: x IS NOT NULL Result: false +** pE1: iif(x,y) pE2: x Result: true +** PE1: iif(x,y,0) pE2: x Result: true ** ** When comparing TK_COLUMN nodes between pE1 and pE2, if pE2 has ** Expr.iTable<0 then assume a table number given by iTab. ** +** If pParse is not NULL, then the values of bound variables in pE1 are +** compared against literal values in pE2 and pParse->pVdbe->expmask is +** modified to record which bound variables are referenced. If pParse +** is NULL, then false will be returned if pE1 contains any bound variables. +** ** When in doubt, return false. Returning true might give a performance ** improvement. Returning false might cause a performance reduction, but ** it will always give the correct answer and is hence always safe. */ -int sqlite3ExprImpliesExpr(Expr *pE1, Expr *pE2, int iTab){ - if( sqlite3ExprCompare(pE1, pE2, iTab)==0 ){ +int sqlite3ExprImpliesExpr( + const Parse *pParse, + const Expr *pE1, + const Expr *pE2, + int iTab +){ + if( sqlite3ExprCompare(pParse, pE1, pE2, iTab)==0 ){ return 1; } if( pE2->op==TK_OR - && (sqlite3ExprImpliesExpr(pE1, pE2->pLeft, iTab) - || sqlite3ExprImpliesExpr(pE1, pE2->pRight, iTab) ) + && (sqlite3ExprImpliesExpr(pParse, pE1, pE2->pLeft, iTab) + || sqlite3ExprImpliesExpr(pParse, pE1, pE2->pRight, iTab) ) ){ return 1; } if( pE2->op==TK_NOTNULL - && sqlite3ExprCompare(pE1->pLeft, pE2->pLeft, iTab)==0 - && (pE1->op!=TK_ISNULL && pE1->op!=TK_IS) + && exprImpliesNotNull(pParse, pE1, pE2->pLeft, iTab, 0) ){ return 1; } + if( sqlite3ExprIsIIF(pParse->db, pE1) ){ + return sqlite3ExprImpliesExpr(pParse,pE1->x.pList->a[0].pExpr,pE2,iTab); + } return 0; } +/* This is a helper function to impliesNotNullRow(). In this routine, +** set pWalker->eCode to one only if *both* of the input expressions +** separately have the implies-not-null-row property. +*/ +static void bothImplyNotNullRow(Walker *pWalker, Expr *pE1, Expr *pE2){ + if( pWalker->eCode==0 ){ + sqlite3WalkExpr(pWalker, pE1); + if( pWalker->eCode ){ + pWalker->eCode = 0; + sqlite3WalkExpr(pWalker, pE2); + } + } +} + +/* +** This is the Expr node callback for sqlite3ExprImpliesNonNullRow(). +** If the expression node requires that the table at pWalker->iCur +** have one or more non-NULL column, then set pWalker->eCode to 1 and abort. +** +** pWalker->mWFlags is non-zero if this inquiry is being undertaking on +** behalf of a RIGHT JOIN (or FULL JOIN). That makes a difference when +** evaluating terms in the ON clause of an inner join. +** +** This routine controls an optimization. False positives (setting +** pWalker->eCode to 1 when it should not be) are deadly, but false-negatives +** (never setting pWalker->eCode) is a harmless missed optimization. +*/ +static int impliesNotNullRow(Walker *pWalker, Expr *pExpr){ + testcase( pExpr->op==TK_AGG_COLUMN ); + testcase( pExpr->op==TK_AGG_FUNCTION ); + if( ExprHasProperty(pExpr, EP_OuterON) ) return WRC_Prune; + if( ExprHasProperty(pExpr, EP_InnerON) && pWalker->mWFlags ){ + /* If iCur is used in an inner-join ON clause to the left of a + ** RIGHT JOIN, that does *not* mean that the table must be non-null. + ** But it is difficult to check for that condition precisely. + ** To keep things simple, any use of iCur from any inner-join is + ** ignored while attempting to simplify a RIGHT JOIN. */ + return WRC_Prune; + } + switch( pExpr->op ){ + case TK_ISNOT: + case TK_ISNULL: + case TK_NOTNULL: + case TK_IS: + case TK_VECTOR: + case TK_FUNCTION: + case TK_TRUTH: + case TK_CASE: + testcase( pExpr->op==TK_ISNOT ); + testcase( pExpr->op==TK_ISNULL ); + testcase( pExpr->op==TK_NOTNULL ); + testcase( pExpr->op==TK_IS ); + testcase( pExpr->op==TK_VECTOR ); + testcase( pExpr->op==TK_FUNCTION ); + testcase( pExpr->op==TK_TRUTH ); + testcase( pExpr->op==TK_CASE ); + return WRC_Prune; + + case TK_COLUMN: + if( pWalker->u.iCur==pExpr->iTable ){ + pWalker->eCode = 1; + return WRC_Abort; + } + return WRC_Prune; + + case TK_OR: + case TK_AND: + /* Both sides of an AND or OR must separately imply non-null-row. + ** Consider these cases: + ** 1. NOT (x AND y) + ** 2. x OR y + ** If only one of x or y is non-null-row, then the overall expression + ** can be true if the other arm is false (case 1) or true (case 2). + */ + testcase( pExpr->op==TK_OR ); + testcase( pExpr->op==TK_AND ); + bothImplyNotNullRow(pWalker, pExpr->pLeft, pExpr->pRight); + return WRC_Prune; + + case TK_IN: + /* Beware of "x NOT IN ()" and "x NOT IN (SELECT 1 WHERE false)", + ** both of which can be true. But apart from these cases, if + ** the left-hand side of the IN is NULL then the IN itself will be + ** NULL. */ + if( ExprUseXList(pExpr) && ALWAYS(pExpr->x.pList->nExpr>0) ){ + sqlite3WalkExpr(pWalker, pExpr->pLeft); + } + return WRC_Prune; + + case TK_BETWEEN: + /* In "x NOT BETWEEN y AND z" either x must be non-null-row or else + ** both y and z must be non-null row */ + assert( ExprUseXList(pExpr) ); + assert( pExpr->x.pList->nExpr==2 ); + sqlite3WalkExpr(pWalker, pExpr->pLeft); + bothImplyNotNullRow(pWalker, pExpr->x.pList->a[0].pExpr, + pExpr->x.pList->a[1].pExpr); + return WRC_Prune; + + /* Virtual tables are allowed to use constraints like x=NULL. So + ** a term of the form x=y does not prove that y is not null if x + ** is the column of a virtual table */ + case TK_EQ: + case TK_NE: + case TK_LT: + case TK_LE: + case TK_GT: + case TK_GE: { + Expr *pLeft = pExpr->pLeft; + Expr *pRight = pExpr->pRight; + testcase( pExpr->op==TK_EQ ); + testcase( pExpr->op==TK_NE ); + testcase( pExpr->op==TK_LT ); + testcase( pExpr->op==TK_LE ); + testcase( pExpr->op==TK_GT ); + testcase( pExpr->op==TK_GE ); + /* The y.pTab=0 assignment in wherecode.c always happens after the + ** impliesNotNullRow() test */ + assert( pLeft->op!=TK_COLUMN || ExprUseYTab(pLeft) ); + assert( pRight->op!=TK_COLUMN || ExprUseYTab(pRight) ); + if( (pLeft->op==TK_COLUMN + && ALWAYS(pLeft->y.pTab!=0) + && IsVirtual(pLeft->y.pTab)) + || (pRight->op==TK_COLUMN + && ALWAYS(pRight->y.pTab!=0) + && IsVirtual(pRight->y.pTab)) + ){ + return WRC_Prune; + } + /* no break */ deliberate_fall_through + } + default: + return WRC_Continue; + } +} + +/* +** Return true (non-zero) if expression p can only be true if at least +** one column of table iTab is non-null. In other words, return true +** if expression p will always be NULL or false if every column of iTab +** is NULL. +** +** False negatives are acceptable. In other words, it is ok to return +** zero even if expression p will never be true of every column of iTab +** is NULL. A false negative is merely a missed optimization opportunity. +** +** False positives are not allowed, however. A false positive may result +** in an incorrect answer. +** +** Terms of p that are marked with EP_OuterON (and hence that come from +** the ON or USING clauses of OUTER JOINS) are excluded from the analysis. +** +** This routine is used to check if a LEFT JOIN can be converted into +** an ordinary JOIN. The p argument is the WHERE clause. If the WHERE +** clause requires that some column of the right table of the LEFT JOIN +** be non-NULL, then the LEFT JOIN can be safely converted into an +** ordinary join. +*/ +int sqlite3ExprImpliesNonNullRow(Expr *p, int iTab, int isRJ){ + Walker w; + p = sqlite3ExprSkipCollateAndLikely(p); + if( p==0 ) return 0; + if( p->op==TK_NOTNULL ){ + p = p->pLeft; + }else{ + while( p->op==TK_AND ){ + if( sqlite3ExprImpliesNonNullRow(p->pLeft, iTab, isRJ) ) return 1; + p = p->pRight; + } + } + w.xExprCallback = impliesNotNullRow; + w.xSelectCallback = 0; + w.xSelectCallback2 = 0; + w.eCode = 0; + w.mWFlags = isRJ!=0; + w.u.iCur = iTab; + sqlite3WalkExpr(&w, p); + return w.eCode; +} + /* ** An instance of the following structure is used by the tree walker -** to count references to table columns in the arguments of an -** aggregate function, in order to implement the -** sqlite3FunctionThisSrc() routine. -*/ -struct SrcCount { - SrcList *pSrc; /* One particular FROM clause in a nested query */ - int nThis; /* Number of references to columns in pSrcList */ - int nOther; /* Number of references to columns in other FROM clauses */ +** to determine if an expression can be evaluated by reference to the +** index only, without having to do a search for the corresponding +** table entry. The IdxCover.pIdx field is the index. IdxCover.iCur +** is the cursor for the table. +*/ +struct IdxCover { + Index *pIdx; /* The index to be tested for coverage */ + int iCur; /* Cursor number for the table corresponding to the index */ +}; + +/* +** Check to see if there are references to columns in table +** pWalker->u.pIdxCover->iCur can be satisfied using the index +** pWalker->u.pIdxCover->pIdx. +*/ +static int exprIdxCover(Walker *pWalker, Expr *pExpr){ + if( pExpr->op==TK_COLUMN + && pExpr->iTable==pWalker->u.pIdxCover->iCur + && sqlite3TableColumnToIndex(pWalker->u.pIdxCover->pIdx, pExpr->iColumn)<0 + ){ + pWalker->eCode = 1; + return WRC_Abort; + } + return WRC_Continue; +} + +/* +** Determine if an index pIdx on table with cursor iCur contains will +** the expression pExpr. Return true if the index does cover the +** expression and false if the pExpr expression references table columns +** that are not found in the index pIdx. +** +** An index covering an expression means that the expression can be +** evaluated using only the index and without having to lookup the +** corresponding table entry. +*/ +int sqlite3ExprCoveredByIndex( + Expr *pExpr, /* The index to be tested */ + int iCur, /* The cursor number for the corresponding table */ + Index *pIdx /* The index that might be used for coverage */ +){ + Walker w; + struct IdxCover xcov; + memset(&w, 0, sizeof(w)); + xcov.iCur = iCur; + xcov.pIdx = pIdx; + w.xExprCallback = exprIdxCover; + w.u.pIdxCover = &xcov; + sqlite3WalkExpr(&w, pExpr); + return !w.eCode; +} + + +/* Structure used to pass information throughout the Walker in order to +** implement sqlite3ReferencesSrcList(). +*/ +struct RefSrcList { + sqlite3 *db; /* Database connection used for sqlite3DbRealloc() */ + SrcList *pRef; /* Looking for references to these tables */ + i64 nExclude; /* Number of tables to exclude from the search */ + int *aiExclude; /* Cursor IDs for tables to exclude from the search */ }; /* -** Count the number of references to columns. +** Walker SELECT callbacks for sqlite3ReferencesSrcList(). +** +** When entering a new subquery on the pExpr argument, add all FROM clause +** entries for that subquery to the exclude list. +** +** When leaving the subquery, remove those entries from the exclude list. +*/ +static int selectRefEnter(Walker *pWalker, Select *pSelect){ + struct RefSrcList *p = pWalker->u.pRefSrcList; + SrcList *pSrc = pSelect->pSrc; + i64 i, j; + int *piNew; + if( pSrc->nSrc==0 ) return WRC_Continue; + j = p->nExclude; + p->nExclude += pSrc->nSrc; + piNew = sqlite3DbRealloc(p->db, p->aiExclude, p->nExclude*sizeof(int)); + if( piNew==0 ){ + p->nExclude = 0; + return WRC_Abort; + }else{ + p->aiExclude = piNew; + } + for(i=0; inSrc; i++, j++){ + p->aiExclude[j] = pSrc->a[i].iCursor; + } + return WRC_Continue; +} +static void selectRefLeave(Walker *pWalker, Select *pSelect){ + struct RefSrcList *p = pWalker->u.pRefSrcList; + SrcList *pSrc = pSelect->pSrc; + if( p->nExclude ){ + assert( p->nExclude>=pSrc->nSrc ); + p->nExclude -= pSrc->nSrc; + } +} + +/* This is the Walker EXPR callback for sqlite3ReferencesSrcList(). +** +** Set the 0x01 bit of pWalker->eCode if there is a reference to any +** of the tables shown in RefSrcList.pRef. +** +** Set the 0x02 bit of pWalker->eCode if there is a reference to a +** table is in neither RefSrcList.pRef nor RefSrcList.aiExclude. */ -static int exprSrcCount(Walker *pWalker, Expr *pExpr){ - /* The NEVER() on the second term is because sqlite3FunctionUsesThisSrc() - ** is always called before sqlite3ExprAnalyzeAggregates() and so the - ** TK_COLUMNs have not yet been converted into TK_AGG_COLUMN. If - ** sqlite3FunctionUsesThisSrc() is used differently in the future, the - ** NEVER() will need to be removed. */ - if( pExpr->op==TK_COLUMN || NEVER(pExpr->op==TK_AGG_COLUMN) ){ +static int exprRefToSrcList(Walker *pWalker, Expr *pExpr){ + if( pExpr->op==TK_COLUMN + || pExpr->op==TK_AGG_COLUMN + ){ int i; - struct SrcCount *p = pWalker->u.pSrcCount; - SrcList *pSrc = p->pSrc; + struct RefSrcList *p = pWalker->u.pRefSrcList; + SrcList *pSrc = p->pRef; int nSrc = pSrc ? pSrc->nSrc : 0; for(i=0; iiTable==pSrc->a[i].iCursor ) break; + if( pExpr->iTable==pSrc->a[i].iCursor ){ + pWalker->eCode |= 1; + return WRC_Continue; + } } - if( inThis++; - }else{ - p->nOther++; + for(i=0; inExclude && p->aiExclude[i]!=pExpr->iTable; i++){} + if( i>=p->nExclude ){ + pWalker->eCode |= 2; } } return WRC_Continue; } /* -** Determine if any of the arguments to the pExpr Function reference -** pSrcList. Return true if they do. Also return true if the function -** has no arguments or has only constant arguments. Return false if pExpr -** references columns but not columns of tables found in pSrcList. +** Check to see if pExpr references any tables in pSrcList. +** Possible return values: +** +** 1 pExpr does references a table in pSrcList. +** +** 0 pExpr references some table that is not defined in either +** pSrcList or in subqueries of pExpr itself. +** +** -1 pExpr only references no tables at all, or it only +** references tables defined in subqueries of pExpr itself. +** +** As currently used, pExpr is always an aggregate function call. That +** fact is exploited for efficiency. */ -int sqlite3FunctionUsesThisSrc(Expr *pExpr, SrcList *pSrcList){ +int sqlite3ReferencesSrcList(Parse *pParse, Expr *pExpr, SrcList *pSrcList){ Walker w; - struct SrcCount cnt; - assert( pExpr->op==TK_AGG_FUNCTION ); + struct RefSrcList x; + assert( pParse->db!=0 ); memset(&w, 0, sizeof(w)); - w.xExprCallback = exprSrcCount; - w.u.pSrcCount = &cnt; - cnt.pSrc = pSrcList; - cnt.nThis = 0; - cnt.nOther = 0; + memset(&x, 0, sizeof(x)); + w.xExprCallback = exprRefToSrcList; + w.xSelectCallback = selectRefEnter; + w.xSelectCallback2 = selectRefLeave; + w.u.pRefSrcList = &x; + x.db = pParse->db; + x.pRef = pSrcList; + assert( pExpr->op==TK_AGG_FUNCTION ); + assert( ExprUseXList(pExpr) ); sqlite3WalkExprList(&w, pExpr->x.pList); - return cnt.nThis>0 || cnt.nOther==0; + if( pExpr->pLeft ){ + assert( pExpr->pLeft->op==TK_ORDER ); + assert( ExprUseXList(pExpr->pLeft) ); + assert( pExpr->pLeft->x.pList!=0 ); + sqlite3WalkExprList(&w, pExpr->pLeft->x.pList); + } +#ifndef SQLITE_OMIT_WINDOWFUNC + if( ExprHasProperty(pExpr, EP_WinFunc) ){ + sqlite3WalkExpr(&w, pExpr->y.pWin->pFilter); + } +#endif + if( x.aiExclude ) sqlite3DbNNFreeNN(pParse->db, x.aiExclude); + if( w.eCode & 0x01 ){ + return 1; + }else if( w.eCode ){ + return 0; + }else{ + return -1; + } +} + +/* +** This is a Walker expression node callback. +** +** For Expr nodes that contain pAggInfo pointers, make sure the AggInfo +** object that is referenced does not refer directly to the Expr. If +** it does, make a copy. This is done because the pExpr argument is +** subject to change. +** +** The copy is scheduled for deletion using the sqlite3ExprDeferredDelete() +** which builds on the sqlite3ParserAddCleanup() mechanism. +*/ +static int agginfoPersistExprCb(Walker *pWalker, Expr *pExpr){ + if( ALWAYS(!ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced)) + && pExpr->pAggInfo!=0 + ){ + AggInfo *pAggInfo = pExpr->pAggInfo; + int iAgg = pExpr->iAgg; + Parse *pParse = pWalker->pParse; + sqlite3 *db = pParse->db; + assert( iAgg>=0 ); + if( pExpr->op!=TK_AGG_FUNCTION ){ + if( iAggnColumn + && pAggInfo->aCol[iAgg].pCExpr==pExpr + ){ + pExpr = sqlite3ExprDup(db, pExpr, 0); + if( pExpr && !sqlite3ExprDeferredDelete(pParse, pExpr) ){ + pAggInfo->aCol[iAgg].pCExpr = pExpr; + } + } + }else{ + assert( pExpr->op==TK_AGG_FUNCTION ); + if( ALWAYS(iAggnFunc) + && pAggInfo->aFunc[iAgg].pFExpr==pExpr + ){ + pExpr = sqlite3ExprDup(db, pExpr, 0); + if( pExpr && !sqlite3ExprDeferredDelete(pParse, pExpr) ){ + pAggInfo->aFunc[iAgg].pFExpr = pExpr; + } + } + } + } + return WRC_Continue; +} + +/* +** Initialize a Walker object so that will persist AggInfo entries referenced +** by the tree that is walked. +*/ +void sqlite3AggInfoPersistWalkerInit(Walker *pWalker, Parse *pParse){ + memset(pWalker, 0, sizeof(*pWalker)); + pWalker->pParse = pParse; + pWalker->xExprCallback = agginfoPersistExprCb; + pWalker->xSelectCallback = sqlite3SelectWalkNoop; } /* @@ -4007,7 +7264,7 @@ static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){ &i ); return i; -} +} /* ** Add a new element to the pAggInfo->aFunc[] array. Return the index of @@ -4016,14 +7273,89 @@ static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){ static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){ int i; pInfo->aFunc = sqlite3ArrayAllocate( - db, + db, pInfo->aFunc, sizeof(pInfo->aFunc[0]), &pInfo->nFunc, &i ); return i; -} +} + +/* +** Search the AggInfo object for an aCol[] entry that has iTable and iColumn. +** Return the index in aCol[] of the entry that describes that column. +** +** If no prior entry is found, create a new one and return -1. The +** new column will have an index of pAggInfo->nColumn-1. +*/ +static void findOrCreateAggInfoColumn( + Parse *pParse, /* Parsing context */ + AggInfo *pAggInfo, /* The AggInfo object to search and/or modify */ + Expr *pExpr /* Expr describing the column to find or insert */ +){ + struct AggInfo_col *pCol; + int k; + int mxTerm = pParse->db->aLimit[SQLITE_LIMIT_COLUMN]; + + assert( mxTerm <= SMXV(i16) ); + assert( pAggInfo->iFirstReg==0 ); + pCol = pAggInfo->aCol; + for(k=0; knColumn; k++, pCol++){ + if( pCol->pCExpr==pExpr ) return; + if( pCol->iTable==pExpr->iTable + && pCol->iColumn==pExpr->iColumn + && pExpr->op!=TK_IF_NULL_ROW + ){ + goto fix_up_expr; + } + } + k = addAggInfoColumn(pParse->db, pAggInfo); + if( k<0 ){ + /* OOM on resize */ + assert( pParse->db->mallocFailed ); + return; + } + if( k>mxTerm ){ + sqlite3ErrorMsg(pParse, "more than %d aggregate terms", mxTerm); + k = mxTerm; + } + pCol = &pAggInfo->aCol[k]; + assert( ExprUseYTab(pExpr) ); + pCol->pTab = pExpr->y.pTab; + pCol->iTable = pExpr->iTable; + pCol->iColumn = pExpr->iColumn; + pCol->iSorterColumn = -1; + pCol->pCExpr = pExpr; + if( pAggInfo->pGroupBy && pExpr->op!=TK_IF_NULL_ROW ){ + int j, n; + ExprList *pGB = pAggInfo->pGroupBy; + struct ExprList_item *pTerm = pGB->a; + n = pGB->nExpr; + for(j=0; jpExpr; + if( pE->op==TK_COLUMN + && pE->iTable==pExpr->iTable + && pE->iColumn==pExpr->iColumn + ){ + pCol->iSorterColumn = j; + break; + } + } + } + if( pCol->iSorterColumn<0 ){ + pCol->iSorterColumn = pAggInfo->nSortingColumn++; + } +fix_up_expr: + ExprSetVVAProperty(pExpr, EP_NoReduce); + assert( pExpr->pAggInfo==0 || pExpr->pAggInfo==pAggInfo ); + pExpr->pAggInfo = pAggInfo; + if( pExpr->op==TK_COLUMN ){ + pExpr->op = TK_AGG_COLUMN; + } + assert( k <= SMXV(pExpr->iAgg) ); + pExpr->iAgg = (i16)k; +} /* ** This is the xExprCallback for a tree walker. It is used to @@ -4035,106 +7367,133 @@ static int analyzeAggregate(Walker *pWalker, Expr *pExpr){ NameContext *pNC = pWalker->u.pNC; Parse *pParse = pNC->pParse; SrcList *pSrcList = pNC->pSrcList; - AggInfo *pAggInfo = pNC->pAggInfo; + AggInfo *pAggInfo = pNC->uNC.pAggInfo; + assert( pNC->ncFlags & NC_UAggInfo ); + assert( pAggInfo->iFirstReg==0 ); switch( pExpr->op ){ + default: { + IndexedExpr *pIEpr; + Expr tmp; + assert( pParse->iSelfTab==0 ); + if( (pNC->ncFlags & NC_InAggFunc)==0 ) break; + if( pParse->pIdxEpr==0 ) break; + for(pIEpr=pParse->pIdxEpr; pIEpr; pIEpr=pIEpr->pIENext){ + int iDataCur = pIEpr->iDataCur; + if( iDataCur<0 ) continue; + if( sqlite3ExprCompare(0, pExpr, pIEpr->pExpr, iDataCur)==0 ) break; + } + if( pIEpr==0 ) break; + if( NEVER(!ExprUseYTab(pExpr)) ) break; + for(i=0; inSrc; i++){ + if( pSrcList->a[0].iCursor==pIEpr->iDataCur ) break; + } + if( i>=pSrcList->nSrc ) break; + if( NEVER(pExpr->pAggInfo!=0) ) break; /* Resolved by outer context */ + if( pParse->nErr ){ return WRC_Abort; } + + /* If we reach this point, it means that expression pExpr can be + ** translated into a reference to an index column as described by + ** pIEpr. + */ + memset(&tmp, 0, sizeof(tmp)); + tmp.op = TK_AGG_COLUMN; + tmp.iTable = pIEpr->iIdxCur; + tmp.iColumn = pIEpr->iIdxCol; + findOrCreateAggInfoColumn(pParse, pAggInfo, &tmp); + if( pParse->nErr ){ return WRC_Abort; } + assert( pAggInfo->aCol!=0 ); + assert( tmp.iAggnColumn ); + pAggInfo->aCol[tmp.iAgg].pCExpr = pExpr; + pExpr->pAggInfo = pAggInfo; + pExpr->iAgg = tmp.iAgg; + return WRC_Prune; + } + case TK_IF_NULL_ROW: case TK_AGG_COLUMN: case TK_COLUMN: { testcase( pExpr->op==TK_AGG_COLUMN ); testcase( pExpr->op==TK_COLUMN ); + testcase( pExpr->op==TK_IF_NULL_ROW ); /* Check to see if the column is in one of the tables in the FROM ** clause of the aggregate query */ if( ALWAYS(pSrcList!=0) ){ - struct SrcList_item *pItem = pSrcList->a; + SrcItem *pItem = pSrcList->a; for(i=0; inSrc; i++, pItem++){ - struct AggInfo_col *pCol; assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) ); if( pExpr->iTable==pItem->iCursor ){ - /* If we reach this point, it means that pExpr refers to a table - ** that is in the FROM clause of the aggregate query. - ** - ** Make an entry for the column in pAggInfo->aCol[] if there - ** is not an entry there already. - */ - int k; - pCol = pAggInfo->aCol; - for(k=0; knColumn; k++, pCol++){ - if( pCol->iTable==pExpr->iTable && - pCol->iColumn==pExpr->iColumn ){ - break; - } - } - if( (k>=pAggInfo->nColumn) - && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0 - ){ - pCol = &pAggInfo->aCol[k]; - pCol->pTab = pExpr->pTab; - pCol->iTable = pExpr->iTable; - pCol->iColumn = pExpr->iColumn; - pCol->iMem = ++pParse->nMem; - pCol->iSorterColumn = -1; - pCol->pExpr = pExpr; - if( pAggInfo->pGroupBy ){ - int j, n; - ExprList *pGB = pAggInfo->pGroupBy; - struct ExprList_item *pTerm = pGB->a; - n = pGB->nExpr; - for(j=0; jpExpr; - if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable && - pE->iColumn==pExpr->iColumn ){ - pCol->iSorterColumn = j; - break; - } - } - } - if( pCol->iSorterColumn<0 ){ - pCol->iSorterColumn = pAggInfo->nSortingColumn++; - } - } - /* There is now an entry for pExpr in pAggInfo->aCol[] (either - ** because it was there before or because we just created it). - ** Convert the pExpr to be a TK_AGG_COLUMN referring to that - ** pAggInfo->aCol[] entry. - */ - ExprSetVVAProperty(pExpr, EP_NoReduce); - pExpr->pAggInfo = pAggInfo; - pExpr->op = TK_AGG_COLUMN; - pExpr->iAgg = (i16)k; + findOrCreateAggInfoColumn(pParse, pAggInfo, pExpr); break; } /* endif pExpr->iTable==pItem->iCursor */ } /* end loop over pSrcList */ } - return WRC_Prune; + return WRC_Continue; } case TK_AGG_FUNCTION: { if( (pNC->ncFlags & NC_InAggFunc)==0 && pWalker->walkerDepth==pExpr->op2 + && pExpr->pAggInfo==0 ){ - /* Check to see if pExpr is a duplicate of another aggregate + /* Check to see if pExpr is a duplicate of another aggregate ** function that is already in the pAggInfo structure */ struct AggInfo_func *pItem = pAggInfo->aFunc; + int mxTerm = pParse->db->aLimit[SQLITE_LIMIT_COLUMN]; + assert( mxTerm <= SMXV(i16) ); for(i=0; inFunc; i++, pItem++){ - if( sqlite3ExprCompare(pItem->pExpr, pExpr, -1)==0 ){ + if( NEVER(pItem->pFExpr==pExpr) ) break; + if( sqlite3ExprCompare(0, pItem->pFExpr, pExpr, -1)==0 ){ break; } } - if( i>=pAggInfo->nFunc ){ + if( i>mxTerm ){ + sqlite3ErrorMsg(pParse, "more than %d aggregate terms", mxTerm); + i = mxTerm; + assert( inFunc ); + }else if( i>=pAggInfo->nFunc ){ /* pExpr is original. Make a new entry in pAggInfo->aFunc[] */ u8 enc = ENC(pParse->db); i = addAggInfoFunc(pParse->db, pAggInfo); if( i>=0 ){ + int nArg; assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); pItem = &pAggInfo->aFunc[i]; - pItem->pExpr = pExpr; - pItem->iMem = ++pParse->nMem; - assert( !ExprHasProperty(pExpr, EP_IntValue) ); + pItem->pFExpr = pExpr; + assert( ExprUseUToken(pExpr) ); + nArg = pExpr->x.pList ? pExpr->x.pList->nExpr : 0; pItem->pFunc = sqlite3FindFunction(pParse->db, - pExpr->u.zToken, sqlite3Strlen30(pExpr->u.zToken), - pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0); - if( pExpr->flags & EP_Distinct ){ + pExpr->u.zToken, nArg, enc, 0); + assert( pItem->bOBUnique==0 ); + if( pExpr->pLeft + && (pItem->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL)==0 + ){ + /* The NEEDCOLL test above causes any ORDER BY clause on + ** aggregate min() or max() to be ignored. */ + ExprList *pOBList; + assert( nArg>0 ); + assert( pExpr->pLeft->op==TK_ORDER ); + assert( ExprUseXList(pExpr->pLeft) ); + pItem->iOBTab = pParse->nTab++; + pOBList = pExpr->pLeft->x.pList; + assert( pOBList->nExpr>0 ); + assert( pItem->bOBUnique==0 ); + if( pOBList->nExpr==1 + && nArg==1 + && sqlite3ExprCompare(0,pOBList->a[0].pExpr, + pExpr->x.pList->a[0].pExpr,0)==0 + ){ + pItem->bOBPayload = 0; + pItem->bOBUnique = ExprHasProperty(pExpr, EP_Distinct); + }else{ + pItem->bOBPayload = 1; + } + pItem->bUseSubtype = + (pItem->pFunc->funcFlags & SQLITE_SUBTYPE)!=0; + }else{ + pItem->iOBTab = -1; + } + if( ExprHasProperty(pExpr, EP_Distinct) && !pItem->bOBUnique ){ pItem->iDistinct = pParse->nTab++; }else{ pItem->iDistinct = -1; @@ -4145,6 +7504,7 @@ static int analyzeAggregate(Walker *pWalker, Expr *pExpr){ */ assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) ); ExprSetVVAProperty(pExpr, EP_NoReduce); + assert( i <= SMXV(pExpr->iAgg) ); pExpr->iAgg = (i16)i; pExpr->pAggInfo = pAggInfo; return WRC_Prune; @@ -4155,11 +7515,6 @@ static int analyzeAggregate(Walker *pWalker, Expr *pExpr){ } return WRC_Continue; } -static int analyzeAggregatesInSelect(Walker *pWalker, Select *pSelect){ - UNUSED_PARAMETER(pWalker); - UNUSED_PARAMETER(pSelect); - return WRC_Continue; -} /* ** Analyze the pExpr expression looking for aggregate functions and @@ -4172,10 +7527,12 @@ static int analyzeAggregatesInSelect(Walker *pWalker, Select *pSelect){ */ void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){ Walker w; - memset(&w, 0, sizeof(w)); w.xExprCallback = analyzeAggregate; - w.xSelectCallback = analyzeAggregatesInSelect; + w.xSelectCallback = sqlite3WalkerDepthIncrease; + w.xSelectCallback2 = sqlite3WalkerDepthDecrease; + w.walkerDepth = 0; w.u.pNC = pNC; + w.pParse = 0; assert( pNC->pSrcList!=0 ); sqlite3WalkExpr(&w, pExpr); } @@ -4209,34 +7566,25 @@ int sqlite3GetTempReg(Parse *pParse){ /* ** Deallocate a register, making available for reuse for some other ** purpose. -** -** If a register is currently being used by the column cache, then -** the deallocation is deferred until the column cache line that uses -** the register becomes stale. */ void sqlite3ReleaseTempReg(Parse *pParse, int iReg){ - if( iReg && pParse->nTempRegaTempReg) ){ - int i; - struct yColCache *p; - for(i=0, p=pParse->aColCache; iiReg==iReg ){ - p->tempReg = 1; - return; - } + if( iReg ){ + sqlite3VdbeReleaseRegisters(pParse, iReg, 1, 0, 0); + if( pParse->nTempRegaTempReg) ){ + pParse->aTempReg[pParse->nTempReg++] = iReg; } - pParse->aTempReg[pParse->nTempReg++] = iReg; } } /* -** Allocate or deallocate a block of nReg consecutive registers +** Allocate or deallocate a block of nReg consecutive registers. */ int sqlite3GetTempRange(Parse *pParse, int nReg){ int i, n; + if( nReg==1 ) return sqlite3GetTempReg(pParse); i = pParse->iRangeReg; n = pParse->nRangeReg; if( nReg<=n ){ - assert( !usedAsColumnCache(pParse, i, i+n-1) ); pParse->iRangeReg += nReg; pParse->nRangeReg -= nReg; }else{ @@ -4246,7 +7594,11 @@ int sqlite3GetTempRange(Parse *pParse, int nReg){ return i; } void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){ - sqlite3ExprCacheRemove(pParse, iReg, nReg); + if( nReg==1 ){ + sqlite3ReleaseTempReg(pParse, iReg); + return; + } + sqlite3VdbeReleaseRegisters(pParse, iReg, nReg, 0, 0); if( nReg>pParse->nRangeReg ){ pParse->nRangeReg = nReg; pParse->iRangeReg = iReg; @@ -4255,8 +7607,75 @@ void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){ /* ** Mark all temporary registers as being unavailable for reuse. +** +** Always invoke this procedure after coding a subroutine or co-routine +** that might be invoked from other parts of the code, to ensure that +** the sub/co-routine does not use registers in common with the code that +** invokes the sub/co-routine. */ void sqlite3ClearTempRegCache(Parse *pParse){ pParse->nTempReg = 0; pParse->nRangeReg = 0; } + +/* +** Make sure sufficient registers have been allocated so that +** iReg is a valid register number. +*/ +void sqlite3TouchRegister(Parse *pParse, int iReg){ + if( pParse->nMemnMem = iReg; +} + +#if defined(SQLITE_ENABLE_STAT4) || defined(SQLITE_DEBUG) +/* +** Return the latest reusable register in the set of all registers. +** The value returned is no less than iMin. If any register iMin or +** greater is in permanent use, then return one more than that last +** permanent register. +*/ +int sqlite3FirstAvailableRegister(Parse *pParse, int iMin){ + const ExprList *pList = pParse->pConstExpr; + if( pList ){ + int i; + for(i=0; inExpr; i++){ + if( pList->a[i].u.iConstExprReg>=iMin ){ + iMin = pList->a[i].u.iConstExprReg + 1; + } + } + } + pParse->nTempReg = 0; + pParse->nRangeReg = 0; + return iMin; +} +#endif /* SQLITE_ENABLE_STAT4 || SQLITE_DEBUG */ + +/* +** Validate that no temporary register falls within the range of +** iFirst..iLast, inclusive. This routine is only call from within assert() +** statements. +*/ +#ifdef SQLITE_DEBUG +int sqlite3NoTempsInRange(Parse *pParse, int iFirst, int iLast){ + int i; + if( pParse->nRangeReg>0 + && pParse->iRangeReg+pParse->nRangeReg > iFirst + && pParse->iRangeReg <= iLast + ){ + return 0; + } + for(i=0; inTempReg; i++){ + if( pParse->aTempReg[i]>=iFirst && pParse->aTempReg[i]<=iLast ){ + return 0; + } + } + if( pParse->pConstExpr ){ + ExprList *pList = pParse->pConstExpr; + for(i=0; inExpr; i++){ + int iReg = pList->a[i].u.iConstExprReg; + if( iReg==0 ) continue; + if( iReg>=iFirst && iReg<=iLast ) return 0; + } + } + return 1; +} +#endif /* SQLITE_DEBUG */ diff --git a/src/fault.c b/src/fault.c index c3028c4f93..5b41b60363 100644 --- a/src/fault.c +++ b/src/fault.c @@ -26,7 +26,7 @@ #include "sqliteInt.h" -#ifndef SQLITE_OMIT_BUILTIN_TEST +#ifndef SQLITE_UNTESTABLE /* ** Global variables. @@ -84,4 +84,4 @@ void sqlite3EndBenignMalloc(void){ } } -#endif /* #ifndef SQLITE_OMIT_BUILTIN_TEST */ +#endif /* #ifndef SQLITE_UNTESTABLE */ diff --git a/src/fkey.c b/src/fkey.c index 38fd4f756b..f1117a8845 100644 --- a/src/fkey.c +++ b/src/fkey.c @@ -215,7 +215,9 @@ int sqlite3FkLocateIndex( */ if( pParent->iPKey>=0 ){ if( !zKey ) return 0; - if( !sqlite3StrICmp(pParent->aCol[pParent->iPKey].zName, zKey) ) return 0; + if( !sqlite3StrICmp(pParent->aCol[pParent->iPKey].zCnName, zKey) ){ + return 0; + } } }else if( paiCol ){ assert( nCol>1 ); @@ -225,7 +227,7 @@ int sqlite3FkLocateIndex( } for(pIdx=pParent->pIndex; pIdx; pIdx=pIdx->pNext){ - if( pIdx->nKeyCol==nCol && IsUniqueIndex(pIdx) ){ + if( pIdx->nKeyCol==nCol && IsUniqueIndex(pIdx) && pIdx->pPartIdxWhere==0 ){ /* pIdx is a UNIQUE index (or a PRIMARY KEY) and has the right number ** of columns. If each indexed column corresponds to a foreign key ** column of pFKey, then this index is a winner. */ @@ -257,11 +259,11 @@ int sqlite3FkLocateIndex( /* If the index uses a collation sequence that is different from ** the default collation sequence for the column, this index is ** unusable. Bail out early in this case. */ - zDfltColl = pParent->aCol[iCol].zColl; + zDfltColl = sqlite3ColumnColl(&pParent->aCol[iCol]); if( !zDfltColl ) zDfltColl = sqlite3StrBINARY; if( sqlite3StrICmp(pIdx->azColl[i], zDfltColl) ) break; - zIdxCol = pParent->aCol[iCol].zName; + zIdxCol = pParent->aCol[iCol].zCnName; for(j=0; jaCol[j].zCol, zIdxCol)==0 ){ if( aiCol ) aiCol[i] = pFKey->aCol[j].iFrom; @@ -329,7 +331,13 @@ static void fkLookupParent( int i; /* Iterator variable */ Vdbe *v = sqlite3GetVdbe(pParse); /* Vdbe to add code to */ int iCur = pParse->nTab - 1; /* Cursor number to use */ - int iOk = sqlite3VdbeMakeLabel(v); /* jump here if parent key found */ + int iOk = sqlite3VdbeMakeLabel(pParse); /* jump here if parent key found */ + + sqlite3VdbeVerifyAbortable(v, + (!pFKey->isDeferred + && !(pParse->db->flags & SQLITE_DeferFKs) + && !pParse->pToplevel + && !pParse->isMultiWrite) ? OE_Abort : OE_Ignore); /* If nIncr is less than zero, then check at runtime if there are any ** outstanding constraints to resolve. If there are not, there is no need @@ -343,7 +351,7 @@ static void fkLookupParent( VdbeCoverage(v); } for(i=0; inCol; i++){ - int iReg = aiCol[i] + regData + 1; + int iReg = sqlite3TableColumnToStorage(pFKey->pFrom,aiCol[i]) + regData + 1; sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iOk); VdbeCoverage(v); } @@ -359,7 +367,8 @@ static void fkLookupParent( ** is no matching parent key. Before using MustBeInt, make a copy of ** the value. Otherwise, the value inserted into the child key column ** will have INTEGER affinity applied to it, which may not be correct. */ - sqlite3VdbeAddOp2(v, OP_SCopy, aiCol[0]+1+regData, regTemp); + sqlite3VdbeAddOp2(v, OP_SCopy, + sqlite3TableColumnToStorage(pFKey->pFrom,aiCol[0])+1+regData, regTemp); iMustBeInt = sqlite3VdbeAddOp2(v, OP_MustBeInt, regTemp, 0); VdbeCoverage(v); @@ -381,12 +390,13 @@ static void fkLookupParent( }else{ int nCol = pFKey->nCol; int regTemp = sqlite3GetTempRange(pParse, nCol); - int regRec = sqlite3GetTempReg(pParse); sqlite3VdbeAddOp3(v, OP_OpenRead, iCur, pIdx->tnum, iDb); sqlite3VdbeSetP4KeyInfo(pParse, pIdx); for(i=0; ipFrom, aiCol[i])+1+regData, + regTemp+i); } /* If the parent table is the same as the child table, and we are about @@ -402,8 +412,11 @@ static void fkLookupParent( if( pTab==pFKey->pFrom && nIncr==1 ){ int iJump = sqlite3VdbeCurrentAddr(v) + nCol + 1; for(i=0; iaiColumn[i]+1+regData; + int iChild = sqlite3TableColumnToStorage(pFKey->pFrom,aiCol[i]) + +1+regData; + int iParent = 1+regData; + iParent += sqlite3TableColumnToStorage(pIdx->pTable, + pIdx->aiColumn[i]); assert( pIdx->aiColumn[i]>=0 ); assert( aiCol[i]!=pTab->iPKey ); if( pIdx->aiColumn[i]==pTab->iPKey ){ @@ -415,12 +428,11 @@ static void fkLookupParent( } sqlite3VdbeGoto(v, iOk); } - - sqlite3VdbeAddOp4(v, OP_MakeRecord, regTemp, nCol, regRec, + + sqlite3VdbeAddOp4(v, OP_Affinity, regTemp, nCol, 0, sqlite3IndexAffinityStr(pParse->db,pIdx), nCol); - sqlite3VdbeAddOp4Int(v, OP_Found, iCur, iOk, regRec, 0); VdbeCoverage(v); - - sqlite3ReleaseTempReg(pParse, regRec); + sqlite3VdbeAddOp4Int(v, OP_Found, iCur, iOk, regTemp, nCol); + VdbeCoverage(v); sqlite3ReleaseTempRange(pParse, regTemp, nCol); } } @@ -471,14 +483,14 @@ static Expr *exprTableRegister( if( pExpr ){ if( iCol>=0 && iCol!=pTab->iPKey ){ pCol = &pTab->aCol[iCol]; - pExpr->iTable = regBase + iCol + 1; - pExpr->affinity = pCol->affinity; - zColl = pCol->zColl; + pExpr->iTable = regBase + sqlite3TableColumnToStorage(pTab,iCol) + 1; + pExpr->affExpr = pCol->affinity; + zColl = sqlite3ColumnColl(pCol); if( zColl==0 ) zColl = db->pDfltColl->zName; pExpr = sqlite3ExprAddCollateString(pParse, pExpr, zColl); }else{ pExpr->iTable = regBase; - pExpr->affinity = SQLITE_AFF_INTEGER; + pExpr->affExpr = SQLITE_AFF_INTEGER; } } return pExpr; @@ -496,7 +508,8 @@ static Expr *exprTableColumn( ){ Expr *pExpr = sqlite3Expr(db, TK_COLUMN, 0); if( pExpr ){ - pExpr->pTab = pTab; + assert( ExprUseYTab(pExpr) ); + pExpr->y.pTab = pTab; pExpr->iTable = iCursor; pExpr->iColumn = iCol; } @@ -521,14 +534,10 @@ static Expr *exprTableColumn( ** Operation | FK type | Action taken ** -------------------------------------------------------------------------- ** DELETE immediate Increment the "immediate constraint counter". -** Or, if the ON (UPDATE|DELETE) action is RESTRICT, -** throw a "FOREIGN KEY constraint failed" exception. ** ** INSERT immediate Decrement the "immediate constraint counter". ** ** DELETE deferred Increment the "deferred constraint counter". -** Or, if the ON (UPDATE|DELETE) action is RESTRICT, -** throw a "FOREIGN KEY constraint failed" exception. ** ** INSERT deferred Decrement the "deferred constraint counter". ** @@ -582,10 +591,10 @@ static void fkScanChildren( pLeft = exprTableRegister(pParse, pTab, regData, iCol); iCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom; assert( iCol>=0 ); - zCol = pFKey->pFrom->aCol[iCol].zName; + zCol = pFKey->pFrom->aCol[iCol].zCnName; pRight = sqlite3Expr(db, TK_ID, zCol); - pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight, 0); - pWhere = sqlite3ExprAnd(db, pWhere, pEq); + pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight); + pWhere = sqlite3ExprAnd(pParse, pWhere, pEq); } /* If the child table is the same as the parent table, then add terms @@ -596,8 +605,11 @@ static void fkScanChildren( ** NOT( $current_a==a AND $current_b==b AND ... ) ** ** The first form is used for rowid tables. The second form is used - ** for WITHOUT ROWID tables. In the second form, the primary key is - ** (a,b,...) + ** for WITHOUT ROWID tables. In the second form, the *parent* key is + ** (a,b,...). Either the parent or primary key could be used to + ** uniquely identify the current row, but the parent key is more convenient + ** as the required values have already been loaded into registers + ** by the caller. */ if( pTab==pFKey->pFrom && nIncr>0 ){ Expr *pNe; /* Expression (pLeft != pRight) */ @@ -606,22 +618,21 @@ static void fkScanChildren( if( HasRowid(pTab) ){ pLeft = exprTableRegister(pParse, pTab, regData, -1); pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, -1); - pNe = sqlite3PExpr(pParse, TK_NE, pLeft, pRight, 0); + pNe = sqlite3PExpr(pParse, TK_NE, pLeft, pRight); }else{ Expr *pEq, *pAll = 0; - Index *pPk = sqlite3PrimaryKeyIndex(pTab); assert( pIdx!=0 ); - for(i=0; inKeyCol; i++){ + for(i=0; inKeyCol; i++){ i16 iCol = pIdx->aiColumn[i]; assert( iCol>=0 ); pLeft = exprTableRegister(pParse, pTab, regData, iCol); - pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, iCol); - pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight, 0); - pAll = sqlite3ExprAnd(db, pAll, pEq); + pRight = sqlite3Expr(db, TK_ID, pTab->aCol[iCol].zCnName); + pEq = sqlite3PExpr(pParse, TK_IS, pLeft, pRight); + pAll = sqlite3ExprAnd(pParse, pAll, pEq); } - pNe = sqlite3PExpr(pParse, TK_NOT, pAll, 0, 0); + pNe = sqlite3PExpr(pParse, TK_NOT, pAll, 0); } - pWhere = sqlite3ExprAnd(db, pWhere, pNe); + pWhere = sqlite3ExprAnd(pParse, pWhere, pNe); } /* Resolve the references in the WHERE clause. */ @@ -633,16 +644,18 @@ static void fkScanChildren( /* Create VDBE to loop through the entries in pSrc that match the WHERE ** clause. For each row found, increment either the deferred or immediate ** foreign key constraint counter. */ - pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0, 0, 0, 0); - sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr); - if( pWInfo ){ - sqlite3WhereEnd(pWInfo); + if( pParse->nErr==0 ){ + pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0, 0, 0, 0, 0); + sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr); + if( pWInfo ){ + sqlite3WhereEnd(pWInfo); + } } /* Clean up the WHERE clause constructed above. */ sqlite3ExprDelete(db, pWhere); if( iFkIfZero ){ - sqlite3VdbeJumpHere(v, iFkIfZero); + sqlite3VdbeJumpHereOrPopInst(v, iFkIfZero); } } @@ -683,6 +696,25 @@ static void fkTriggerDelete(sqlite3 *dbMem, Trigger *p){ } } +/* +** Clear the apTrigger[] cache of CASCADE triggers for all foreign keys +** in a particular database. This needs to happen when the schema +** changes. +*/ +void sqlite3FkClearTriggerCache(sqlite3 *db, int iDb){ + HashElem *k; + Hash *pHash = &db->aDb[iDb].pSchema->tblHash; + for(k=sqliteHashFirst(pHash); k; k=sqliteHashNext(k)){ + Table *pTab = sqliteHashData(k); + FKey *pFKey; + if( !IsOrdinaryTable(pTab) ) continue; + for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){ + fkTriggerDelete(db, pFKey->apTrigger[0]); pFKey->apTrigger[0] = 0; + fkTriggerDelete(db, pFKey->apTrigger[1]); pFKey->apTrigger[1] = 0; + } + } +} + /* ** This function is called to generate code that runs when table pTab is ** being dropped from the database. The SrcList passed as the second argument @@ -702,11 +734,12 @@ static void fkTriggerDelete(sqlite3 *dbMem, Trigger *p){ */ void sqlite3FkDropTable(Parse *pParse, SrcList *pName, Table *pTab){ sqlite3 *db = pParse->db; - if( (db->flags&SQLITE_ForeignKeys) && !IsVirtual(pTab) && !pTab->pSelect ){ + if( (db->flags&SQLITE_ForeignKeys) && IsOrdinaryTable(pTab) ){ int iSkip = 0; Vdbe *v = sqlite3GetVdbe(pParse); assert( v ); /* VDBE has already been allocated */ + assert( IsOrdinaryTable(pTab) ); if( sqlite3FkReferences(pTab)==0 ){ /* Search for a deferred foreign key constraint for which this table ** is the child table. If one cannot be found, return without @@ -714,16 +747,16 @@ void sqlite3FkDropTable(Parse *pParse, SrcList *pName, Table *pTab){ ** the entire DELETE if there are no outstanding deferred constraints ** when this statement is run. */ FKey *p; - for(p=pTab->pFKey; p; p=p->pNextFrom){ + for(p=pTab->u.tab.pFKey; p; p=p->pNextFrom){ if( p->isDeferred || (db->flags & SQLITE_DeferFKs) ) break; } if( !p ) return; - iSkip = sqlite3VdbeMakeLabel(v); + iSkip = sqlite3VdbeMakeLabel(pParse); sqlite3VdbeAddOp2(v, OP_FkIfZero, 1, iSkip); VdbeCoverage(v); } pParse->disableTriggers = 1; - sqlite3DeleteFrom(pParse, sqlite3SrcListDup(db, pName, 0), 0); + sqlite3DeleteFrom(pParse, sqlite3SrcListDup(db, pName, 0), 0, 0, 0); pParse->disableTriggers = 0; /* If the DELETE has generated immediate foreign key constraint @@ -736,6 +769,7 @@ void sqlite3FkDropTable(Parse *pParse, SrcList *pName, Table *pTab){ ** constraints are violated. */ if( (db->flags & SQLITE_DeferFKs)==0 ){ + sqlite3VdbeVerifyAbortable(v, OE_Abort); sqlite3VdbeAddOp2(v, OP_FkIfZero, 0, sqlite3VdbeCurrentAddr(v)+2); VdbeCoverage(v); sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_FOREIGNKEY, @@ -802,7 +836,7 @@ static int fkParentIsModified( if( aChange[iKey]>=0 || (iKey==pTab->iPKey && bChngRowid) ){ Column *pCol = &pTab->aCol[iKey]; if( zKey ){ - if( 0==sqlite3StrICmp(pCol->zName, zKey) ) return 1; + if( 0==sqlite3StrICmp(pCol->zCnName, zKey) ) return 1; }else if( pCol->colFlags & COLFLAG_PRIMKEY ){ return 1; } @@ -824,6 +858,7 @@ static int isSetNullAction(Parse *pParse, FKey *pFKey){ if( (p==pFKey->apTrigger[0] && pFKey->aAction[0]==OE_SetNull) || (p==pFKey->apTrigger[1] && pFKey->aAction[1]==OE_SetNull) ){ + assert( (pTop->db->flags & SQLITE_FkNoAction)==0 ); return 1; } } @@ -869,13 +904,14 @@ void sqlite3FkCheck( /* If foreign-keys are disabled, this function is a no-op. */ if( (db->flags&SQLITE_ForeignKeys)==0 ) return; + if( !IsOrdinaryTable(pTab) ) return; iDb = sqlite3SchemaToIndex(db, pTab->pSchema); - zDb = db->aDb[iDb].zName; + zDb = db->aDb[iDb].zDbSName; /* Loop through all the foreign key constraints for which pTab is the ** child table (the table that the foreign key definition is part of). */ - for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){ + for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){ Table *pTo; /* Parent table of foreign key pFKey */ Index *pIdx = 0; /* Index on key columns in pTo */ int *aiFree = 0; @@ -914,7 +950,9 @@ void sqlite3FkCheck( Vdbe *v = sqlite3GetVdbe(pParse); int iJump = sqlite3VdbeCurrentAddr(v) + pFKey->nCol + 1; for(i=0; inCol; i++){ - int iReg = pFKey->aCol[i].iFrom + regOld + 1; + int iFromCol, iReg; + iFromCol = pFKey->aCol[i].iFrom; + iReg = sqlite3TableColumnToStorage(pFKey->pFrom,iFromCol) + regOld+1; sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iJump); VdbeCoverage(v); } sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, -1); @@ -940,7 +978,7 @@ void sqlite3FkCheck( ** values read from the parent table are NULL. */ if( db->xAuth ){ int rcauth; - char *zCol = pTo->aCol[pIdx ? pIdx->aiColumn[i] : pTo->iPKey].zName; + char *zCol = pTo->aCol[pIdx ? pIdx->aiColumn[i] : pTo->iPKey].zCnName; rcauth = sqlite3AuthReadCol(pParse, pTo->zName, zCol, iDb); bIgnore = (rcauth==SQLITE_IGNORE); } @@ -1002,12 +1040,12 @@ void sqlite3FkCheck( /* Create a SrcList structure containing the child table. We need the ** child table as a SrcList for sqlite3WhereBegin() */ - pSrc = sqlite3SrcListAppend(db, 0, 0, 0); + pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0); if( pSrc ){ - struct SrcList_item *pItem = pSrc->a; - pItem->pTab = pFKey->pFrom; + SrcItem *pItem = pSrc->a; + pItem->pSTab = pFKey->pFrom; pItem->zName = pFKey->pFrom->zName; - pItem->pTab->nRef++; + pItem->pSTab->nTabRef++; pItem->iCursor = pParse->nTab++; if( regNew!=0 ){ @@ -1015,6 +1053,8 @@ void sqlite3FkCheck( } if( regOld!=0 ){ int eAction = pFKey->aAction[aChange!=0]; + if( (db->flags & SQLITE_FkNoAction) ) eAction = OE_None; + fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regOld, 1); /* If this is a deferred FK constraint, or a CASCADE or SET NULL ** action applies, then any foreign key violations caused by @@ -1055,10 +1095,10 @@ u32 sqlite3FkOldmask( Table *pTab /* Table being modified */ ){ u32 mask = 0; - if( pParse->db->flags&SQLITE_ForeignKeys ){ + if( pParse->db->flags&SQLITE_ForeignKeys && IsOrdinaryTable(pTab) ){ FKey *p; int i; - for(p=pTab->pFKey; p; p=p->pNextFrom){ + for(p=pTab->u.tab.pFKey; p; p=p->pNextFrom){ for(i=0; inCol; i++) mask |= COLUMN_MASK(p->aCol[i].iFrom); } for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){ @@ -1087,8 +1127,18 @@ u32 sqlite3FkOldmask( ** UPDATE statement modifies the rowid fields of the table. ** ** If any foreign key processing will be required, this function returns -** true. If there is no foreign key related processing, this function -** returns false. +** non-zero. If there is no foreign key related processing, this function +** returns zero. +** +** For an UPDATE, this function returns 2 if: +** +** * There are any FKs for which pTab is the child and the parent table +** and any FK processing at all is required (even of a different FK), or +** +** * the UPDATE modifies one or more parent keys for which the action is +** not "NO ACTION" (i.e. is CASCADE, SET DEFAULT or SET NULL). +** +** Or, assuming some other foreign key processing is required, 1. */ int sqlite3FkRequired( Parse *pParse, /* Parse context */ @@ -1096,29 +1146,41 @@ int sqlite3FkRequired( int *aChange, /* Non-NULL for UPDATE operations */ int chngRowid /* True for UPDATE that affects rowid */ ){ - if( pParse->db->flags&SQLITE_ForeignKeys ){ + int eRet = 1; /* Value to return if bHaveFK is true */ + int bHaveFK = 0; /* If FK processing is required */ + if( pParse->db->flags&SQLITE_ForeignKeys && IsOrdinaryTable(pTab) ){ if( !aChange ){ /* A DELETE operation. Foreign key processing is required if the ** table in question is either the child or parent table for any ** foreign key constraint. */ - return (sqlite3FkReferences(pTab) || pTab->pFKey); + bHaveFK = (sqlite3FkReferences(pTab) || pTab->u.tab.pFKey); }else{ /* This is an UPDATE. Foreign key processing is only required if the ** operation modifies one or more child or parent key columns. */ FKey *p; /* Check if any child key columns are being modified. */ - for(p=pTab->pFKey; p; p=p->pNextFrom){ - if( fkChildIsModified(pTab, p, aChange, chngRowid) ) return 1; + for(p=pTab->u.tab.pFKey; p; p=p->pNextFrom){ + if( fkChildIsModified(pTab, p, aChange, chngRowid) ){ + if( 0==sqlite3_stricmp(pTab->zName, p->zTo) ) eRet = 2; + bHaveFK = 1; + } } /* Check if any parent key columns are being modified. */ for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){ - if( fkParentIsModified(pTab, p, aChange, chngRowid) ) return 1; + if( fkParentIsModified(pTab, p, aChange, chngRowid) ){ + if( (pParse->db->flags & SQLITE_FkNoAction)==0 + && p->aAction[1]!=OE_None + ){ + return 2; + } + bHaveFK = 1; + } } } } - return 0; + return bHaveFK ? eRet : 0; } /* @@ -1130,9 +1192,9 @@ int sqlite3FkRequired( ** ** It returns a pointer to a Trigger structure containing a trigger ** equivalent to the ON UPDATE or ON DELETE action specified by pFKey. -** If the action is "NO ACTION" or "RESTRICT", then a NULL pointer is -** returned (these actions require no special handling by the triggers -** sub-system, code for them is created by fkScanChildren()). +** If the action is "NO ACTION" then a NULL pointer is returned (these actions +** require no special handling by the triggers sub-system, code for them is +** created by fkScanChildren()). ** ** For example, if pFKey is the foreign key and pTab is table "p" in ** the following schema: @@ -1162,6 +1224,10 @@ static Trigger *fkActionTrigger( int iAction = (pChanges!=0); /* 1 for UPDATE, 0 for DELETE */ action = pFKey->aAction[iAction]; + if( (db->flags & SQLITE_FkNoAction) ) action = OE_None; + if( action==OE_Restrict && (db->flags & SQLITE_DeferFKs) ){ + return 0; + } pTrigger = pFKey->apTrigger[iAction]; if( action!=OE_None && !pTrigger ){ @@ -1192,8 +1258,8 @@ static Trigger *fkActionTrigger( assert( pIdx!=0 || (pTab->iPKey>=0 && pTab->iPKeynCol) ); assert( pIdx==0 || pIdx->aiColumn[i]>=0 ); sqlite3TokenInit(&tToCol, - pTab->aCol[pIdx ? pIdx->aiColumn[i] : pTab->iPKey].zName); - sqlite3TokenInit(&tFromCol, pFKey->pFrom->aCol[iFromCol].zName); + pTab->aCol[pIdx ? pIdx->aiColumn[i] : pTab->iPKey].zCnName); + sqlite3TokenInit(&tFromCol, pFKey->pFrom->aCol[iFromCol].zCnName); /* Create the expression "OLD.zToCol = zFromCol". It is important ** that the "OLD.zToCol" term is on the LHS of the = operator, so @@ -1202,11 +1268,10 @@ static Trigger *fkActionTrigger( pEq = sqlite3PExpr(pParse, TK_EQ, sqlite3PExpr(pParse, TK_DOT, sqlite3ExprAlloc(db, TK_ID, &tOld, 0), - sqlite3ExprAlloc(db, TK_ID, &tToCol, 0) - , 0), + sqlite3ExprAlloc(db, TK_ID, &tToCol, 0)), sqlite3ExprAlloc(db, TK_ID, &tFromCol, 0) - , 0); - pWhere = sqlite3ExprAnd(db, pWhere, pEq); + ); + pWhere = sqlite3ExprAnd(pParse, pWhere, pEq); /* For ON UPDATE, construct the next term of the WHEN clause. ** The final WHEN clause will be like this: @@ -1217,14 +1282,12 @@ static Trigger *fkActionTrigger( pEq = sqlite3PExpr(pParse, TK_IS, sqlite3PExpr(pParse, TK_DOT, sqlite3ExprAlloc(db, TK_ID, &tOld, 0), - sqlite3ExprAlloc(db, TK_ID, &tToCol, 0), - 0), + sqlite3ExprAlloc(db, TK_ID, &tToCol, 0)), sqlite3PExpr(pParse, TK_DOT, sqlite3ExprAlloc(db, TK_ID, &tNew, 0), - sqlite3ExprAlloc(db, TK_ID, &tToCol, 0), - 0), - 0); - pWhen = sqlite3ExprAnd(db, pWhen, pEq); + sqlite3ExprAlloc(db, TK_ID, &tToCol, 0)) + ); + pWhen = sqlite3ExprAnd(pParse, pWhen, pEq); } if( action!=OE_Restrict && (action!=OE_Cascade || pChanges) ){ @@ -1232,17 +1295,24 @@ static Trigger *fkActionTrigger( if( action==OE_Cascade ){ pNew = sqlite3PExpr(pParse, TK_DOT, sqlite3ExprAlloc(db, TK_ID, &tNew, 0), - sqlite3ExprAlloc(db, TK_ID, &tToCol, 0) - , 0); + sqlite3ExprAlloc(db, TK_ID, &tToCol, 0)); }else if( action==OE_SetDflt ){ - Expr *pDflt = pFKey->pFrom->aCol[iFromCol].pDflt; + Column *pCol = pFKey->pFrom->aCol + iFromCol; + Expr *pDflt; + if( pCol->colFlags & COLFLAG_GENERATED ){ + testcase( pCol->colFlags & COLFLAG_VIRTUAL ); + testcase( pCol->colFlags & COLFLAG_STORED ); + pDflt = 0; + }else{ + pDflt = sqlite3ColumnExpr(pFKey->pFrom, pCol); + } if( pDflt ){ pNew = sqlite3ExprDup(db, pDflt, 0); }else{ - pNew = sqlite3PExpr(pParse, TK_NULL, 0, 0, 0); + pNew = sqlite3ExprAlloc(db, TK_NULL, 0, 0); } }else{ - pNew = sqlite3PExpr(pParse, TK_NULL, 0, 0, 0); + pNew = sqlite3ExprAlloc(db, TK_NULL, 0, 0); } pList = sqlite3ExprListAppend(pParse, pList, pNew); sqlite3ExprListSetName(pParse, pList, &tFromCol, 0); @@ -1254,26 +1324,33 @@ static Trigger *fkActionTrigger( nFrom = sqlite3Strlen30(zFrom); if( action==OE_Restrict ){ - Token tFrom; + int iDb = sqlite3SchemaToIndex(db, pTab->pSchema); + SrcList *pSrc; Expr *pRaise; - tFrom.z = zFrom; - tFrom.n = nFrom; - pRaise = sqlite3Expr(db, TK_RAISE, "FOREIGN KEY constraint failed"); + pRaise = sqlite3Expr(db, TK_STRING, "FOREIGN KEY constraint failed"), + pRaise = sqlite3PExpr(pParse, TK_RAISE, pRaise, 0); if( pRaise ){ - pRaise->affinity = OE_Abort; + pRaise->affExpr = OE_Abort; + } + pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0); + if( pSrc ){ + assert( pSrc->nSrc==1 ); + pSrc->a[0].zName = sqlite3DbStrDup(db, zFrom); + assert( pSrc->a[0].fg.fixedSchema==0 && pSrc->a[0].fg.isSubquery==0 ); + pSrc->a[0].u4.zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zDbSName); } pSelect = sqlite3SelectNew(pParse, sqlite3ExprListAppend(pParse, 0, pRaise), - sqlite3SrcListAppend(db, 0, &tFrom, 0), + pSrc, pWhere, - 0, 0, 0, 0, 0, 0 + 0, 0, 0, 0, 0 ); pWhere = 0; } /* Disable lookaside memory allocation */ - db->lookaside.bDisable++; + DisableLookaside; pTrigger = (Trigger *)sqlite3DbMallocZero(db, sizeof(Trigger) + /* struct Trigger */ @@ -1289,13 +1366,13 @@ static Trigger *fkActionTrigger( pStep->pExprList = sqlite3ExprListDup(db, pList, EXPRDUP_REDUCE); pStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE); if( pWhen ){ - pWhen = sqlite3PExpr(pParse, TK_NOT, pWhen, 0, 0); + pWhen = sqlite3PExpr(pParse, TK_NOT, pWhen, 0); pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE); } } /* Re-enable the lookaside buffer, if it was disabled earlier. */ - db->lookaside.bDisable--; + EnableLookaside; sqlite3ExprDelete(db, pWhere); sqlite3ExprDelete(db, pWhen); @@ -1306,16 +1383,18 @@ static Trigger *fkActionTrigger( return 0; } assert( pStep!=0 ); + assert( pTrigger!=0 ); switch( action ){ case OE_Restrict: - pStep->op = TK_SELECT; + pStep->op = TK_SELECT; break; case OE_Cascade: if( !pChanges ){ pStep->op = TK_DELETE; break; } + /* no break */ deliberate_fall_through default: pStep->op = TK_UPDATE; } @@ -1369,17 +1448,18 @@ void sqlite3FkDelete(sqlite3 *db, Table *pTab){ FKey *pFKey; /* Iterator variable */ FKey *pNext; /* Copy of pFKey->pNextFrom */ - assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pTab->pSchema) ); - for(pFKey=pTab->pFKey; pFKey; pFKey=pNext){ + assert( IsOrdinaryTable(pTab) ); + assert( db!=0 ); + for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pNext){ + assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pTab->pSchema) ); /* Remove the FK from the fkeyHash hash table. */ - if( !db || db->pnBytesFreed==0 ){ + if( db->pnBytesFreed==0 ){ if( pFKey->pPrevTo ){ pFKey->pPrevTo->pNextTo = pFKey->pNextTo; }else{ - void *p = (void *)pFKey->pNextTo; - const char *z = (p ? pFKey->pNextTo->zTo : pFKey->zTo); - sqlite3HashInsert(&pTab->pSchema->fkeyHash, z, p); + const char *z = (pFKey->pNextTo ? pFKey->pNextTo->zTo : pFKey->zTo); + sqlite3HashInsert(&pTab->pSchema->fkeyHash, z, pFKey->pNextTo); } if( pFKey->pNextTo ){ pFKey->pNextTo->pPrevTo = pFKey->pPrevTo; diff --git a/src/func.c b/src/func.c index 45d960db52..6dac7195a4 100644 --- a/src/func.c +++ b/src/func.c @@ -16,6 +16,9 @@ #include "sqliteInt.h" #include #include +#ifndef SQLITE_OMIT_FLOATING_POINT +#include +#endif #include "vdbeInt.h" /* @@ -35,6 +38,8 @@ static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){ ** iteration of the aggregate loop. */ static void sqlite3SkipAccumulatorLoad(sqlite3_context *context){ + assert( context->isError<=0 ); + context->isError = -1; context->skipFlag = 1; } @@ -76,18 +81,34 @@ static void typeofFunc( int NotUsed, sqlite3_value **argv ){ - const char *z = 0; + static const char *azType[] = { "integer", "real", "text", "blob", "null" }; + int i = sqlite3_value_type(argv[0]) - 1; UNUSED_PARAMETER(NotUsed); - switch( sqlite3_value_type(argv[0]) ){ - case SQLITE_INTEGER: z = "integer"; break; - case SQLITE_TEXT: z = "text"; break; - case SQLITE_FLOAT: z = "real"; break; - case SQLITE_BLOB: z = "blob"; break; - default: z = "null"; break; - } - sqlite3_result_text(context, z, -1, SQLITE_STATIC); + assert( i>=0 && i=0xc0 ){ + while( (*z & 0xc0)==0x80 ){ z++; z0++; } + } + } + sqlite3_result_int(context, (int)(z-z0)); + break; + } + default: { + sqlite3_result_null(context); + break; + } + } +} + +/* +** Implementation of the octet_length() function +*/ +static void bytelengthFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + assert( argc==1 ); + UNUSED_PARAMETER(argc); + switch( sqlite3_value_type(argv[0]) ){ + case SQLITE_BLOB: { + sqlite3_result_int(context, sqlite3_value_bytes(argv[0])); + break; + } + case SQLITE_INTEGER: + case SQLITE_FLOAT: { + i64 m = sqlite3_context_db_handle(context)->enc<=SQLITE_UTF8 ? 1 : 2; + sqlite3_result_int64(context, sqlite3_value_bytes(argv[0])*m); + break; + } + case SQLITE_TEXT: { + if( sqlite3_value_encoding(argv[0])<=SQLITE_UTF8 ){ + sqlite3_result_int(context, sqlite3_value_bytes(argv[0])); + }else{ + sqlite3_result_int(context, sqlite3_value_bytes16(argv[0])); } - sqlite3_result_int(context, len); break; } default: { @@ -130,7 +189,7 @@ static void lengthFunc( ** Implementation of the abs() function. ** ** IMP: R-23979-26855 The abs(X) function returns the absolute value of -** the numeric argument X. +** the numeric argument X. */ static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ assert( argc==1 ); @@ -147,7 +206,7 @@ static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ return; } iVal = -iVal; - } + } sqlite3_result_int64(context, iVal); break; } @@ -193,6 +252,9 @@ static void instrFunc( int typeHaystack, typeNeedle; int N = 1; int isText; + unsigned char firstChar; + sqlite3_value *pC1 = 0; + sqlite3_value *pC2 = 0; UNUSED_PARAMETER(argc); typeHaystack = sqlite3_value_type(argv[0]); @@ -200,28 +262,51 @@ static void instrFunc( if( typeHaystack==SQLITE_NULL || typeNeedle==SQLITE_NULL ) return; nHaystack = sqlite3_value_bytes(argv[0]); nNeedle = sqlite3_value_bytes(argv[1]); - if( typeHaystack==SQLITE_BLOB && typeNeedle==SQLITE_BLOB ){ - zHaystack = sqlite3_value_blob(argv[0]); - zNeedle = sqlite3_value_blob(argv[1]); - isText = 0; - }else{ - zHaystack = sqlite3_value_text(argv[0]); - zNeedle = sqlite3_value_text(argv[1]); - isText = 1; - } - while( nNeedle<=nHaystack && memcmp(zHaystack, zNeedle, nNeedle)!=0 ){ - N++; - do{ - nHaystack--; - zHaystack++; - }while( isText && (zHaystack[0]&0xc0)==0x80 ); - } - if( nNeedle>nHaystack ) N = 0; + if( nNeedle>0 ){ + if( typeHaystack==SQLITE_BLOB && typeNeedle==SQLITE_BLOB ){ + zHaystack = sqlite3_value_blob(argv[0]); + zNeedle = sqlite3_value_blob(argv[1]); + isText = 0; + }else if( typeHaystack!=SQLITE_BLOB && typeNeedle!=SQLITE_BLOB ){ + zHaystack = sqlite3_value_text(argv[0]); + zNeedle = sqlite3_value_text(argv[1]); + isText = 1; + }else{ + pC1 = sqlite3_value_dup(argv[0]); + zHaystack = sqlite3_value_text(pC1); + if( zHaystack==0 ) goto endInstrOOM; + nHaystack = sqlite3_value_bytes(pC1); + pC2 = sqlite3_value_dup(argv[1]); + zNeedle = sqlite3_value_text(pC2); + if( zNeedle==0 ) goto endInstrOOM; + nNeedle = sqlite3_value_bytes(pC2); + isText = 1; + } + if( zNeedle==0 || (nHaystack && zHaystack==0) ) goto endInstrOOM; + firstChar = zNeedle[0]; + while( nNeedle<=nHaystack + && (zHaystack[0]!=firstChar || memcmp(zHaystack, zNeedle, nNeedle)!=0) + ){ + N++; + do{ + nHaystack--; + zHaystack++; + }while( isText && (zHaystack[0]&0xc0)==0x80 ); + } + if( nNeedle>nHaystack ) N = 0; + } sqlite3_result_int(context, N); +endInstr: + sqlite3_value_free(pC1); + sqlite3_value_free(pC2); + return; +endInstrOOM: + sqlite3_result_error_nomem(context); + goto endInstr; } /* -** Implementation of the printf() function. +** Implementation of the printf() (a.k.a. format()) SQL function. */ static void printfFunc( sqlite3_context *context, @@ -240,7 +325,7 @@ static void printfFunc( x.apArg = argv+1; sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]); str.printfFlags = SQLITE_PRINTF_SQLFUNC; - sqlite3XPrintf(&str, zFormat, &x); + sqlite3_str_appendf(&str, zFormat, &x); n = str.nChar; sqlite3_result_text(context, sqlite3StrAccumFinish(&str), n, SQLITE_DYNAMIC); @@ -269,16 +354,10 @@ static void substrFunc( int len; int p0type; i64 p1, p2; - int negP2 = 0; assert( argc==3 || argc==2 ); - if( sqlite3_value_type(argv[1])==SQLITE_NULL - || (argc==3 && sqlite3_value_type(argv[2])==SQLITE_NULL) - ){ - return; - } p0type = sqlite3_value_type(argv[0]); - p1 = sqlite3_value_int(argv[1]); + p1 = sqlite3_value_int64(argv[1]); if( p0type==SQLITE_BLOB ){ len = sqlite3_value_bytes(argv[0]); z = sqlite3_value_blob(argv[0]); @@ -294,28 +373,31 @@ static void substrFunc( } } } -#ifdef SQLITE_SUBSTR_COMPATIBILITY - /* If SUBSTR_COMPATIBILITY is defined then substr(X,0,N) work the same as - ** as substr(X,1,N) - it returns the first N characters of X. This - ** is essentially a back-out of the bug-fix in check-in [5fc125d362df4b8] - ** from 2009-02-02 for compatibility of applications that exploited the - ** old buggy behavior. */ - if( p1==0 ) p1 = 1; /* */ -#endif if( argc==3 ){ - p2 = sqlite3_value_int(argv[2]); - if( p2<0 ){ - p2 = -p2; - negP2 = 1; - } + p2 = sqlite3_value_int64(argv[2]); + if( p2==0 && sqlite3_value_type(argv[2])==SQLITE_NULL ) return; }else{ p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH]; } + if( p1==0 ){ +#ifdef SQLITE_SUBSTR_COMPATIBILITY + /* If SUBSTR_COMPATIBILITY is defined then substr(X,0,N) work the same as + ** as substr(X,1,N) - it returns the first N characters of X. This + ** is essentially a back-out of the bug-fix in check-in [5fc125d362df4b8] + ** from 2009-02-02 for compatibility of applications that exploited the + ** old buggy behavior. */ + p1 = 1; /* */ +#endif + if( sqlite3_value_type(argv[1])==SQLITE_NULL ) return; + } if( p1<0 ){ p1 += len; if( p1<0 ){ - p2 += p1; - if( p2<0 ) p2 = 0; + if( p2<0 ){ + p2 = 0; + }else{ + p2 += p1; + } p1 = 0; } }else if( p1>0 ){ @@ -323,12 +405,13 @@ static void substrFunc( }else if( p2>0 ){ p2--; } - if( negP2 ){ - p1 -= p2; - if( p1<0 ){ - p2 += p1; - p1 = 0; + if( p2<0 ){ + if( p2<-p1 ){ + p2 = p1; + }else{ + p2 = -p2; } + p1 -= p2; } assert( p1>=0 && p2>=0 ); if( p0type!=SQLITE_BLOB ){ @@ -342,9 +425,11 @@ static void substrFunc( sqlite3_result_text64(context, (char*)z, z2-z, SQLITE_TRANSIENT, SQLITE_UTF8); }else{ - if( p1+p2>len ){ + if( p1>=len ){ + p1 = p2 = 0; + }else if( p2>len-p1 ){ p2 = len-p1; - if( p2<0 ) p2 = 0; + assert( p2>0 ); } sqlite3_result_blob64(context, (char*)&z[p1], (u64)p2, SQLITE_TRANSIENT); } @@ -355,13 +440,13 @@ static void substrFunc( */ #ifndef SQLITE_OMIT_FLOATING_POINT static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ - int n = 0; + i64 n = 0; double r; char *zBuf; assert( argc==1 || argc==2 ); if( argc==2 ){ if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return; - n = sqlite3_value_int(argv[1]); + n = sqlite3_value_int64(argv[1]); if( n>30 ) n = 30; if( n<0 ) n = 0; } @@ -371,12 +456,12 @@ static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ ** handle the rounding directly, ** otherwise use printf. */ - if( n==0 && r>=0 && r+4503599627370496.0 ){ + /* The value has no fractional part so there is nothing to round */ + }else if( n==0 ){ + r = (double)((sqlite_int64)(r+(r<0?-0.5:+0.5))); }else{ - zBuf = sqlite3_mprintf("%.*f",n,r); + zBuf = sqlite3_mprintf("%!.*f",(int)n,r); if( zBuf==0 ){ sqlite3_result_error_nomem(context); return; @@ -400,7 +485,7 @@ static void *contextMalloc(sqlite3_context *context, i64 nByte){ sqlite3 *db = sqlite3_context_db_handle(context); assert( nByte>0 ); testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH] ); - testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH]+1 ); + testcase( nByte==(i64)db->aLimit[SQLITE_LIMIT_LENGTH]+1 ); if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){ sqlite3_result_error_toobig(context); z = 0; @@ -466,7 +551,7 @@ static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ #define noopFunc versionFunc /* Substitute function - never called */ /* -** Implementation of random(). Return a random integer. +** Implementation of random(). Return a random integer. */ static void randomFunc( sqlite3_context *context, @@ -477,11 +562,11 @@ static void randomFunc( UNUSED_PARAMETER2(NotUsed, NotUsed2); sqlite3_randomness(sizeof(r), &r); if( r<0 ){ - /* We need to prevent a random number of 0x8000000000000000 + /* We need to prevent a random number of 0x8000000000000000 ** (or -9223372036854775808) since when you do abs() of that ** number of you get the same value back again. To do this ** in a way that is testable, mask the sign bit off of negative - ** values, resulting in a positive value. Then take the + ** values, resulting in a positive value. Then take the ** 2s complement of that positive value. The end result can ** therefore be no less than -9223372036854775807. */ @@ -499,11 +584,11 @@ static void randomBlob( int argc, sqlite3_value **argv ){ - int n; + sqlite3_int64 n; unsigned char *p; assert( argc==1 ); UNUSED_PARAMETER(argc); - n = sqlite3_value_int(argv[0]); + n = sqlite3_value_int64(argv[0]); if( n<1 ){ n = 1; } @@ -519,8 +604,8 @@ static void randomBlob( ** value is the same as the sqlite3_last_insert_rowid() API function. */ static void last_insert_rowid( - sqlite3_context *context, - int NotUsed, + sqlite3_context *context, + int NotUsed, sqlite3_value **NotUsed2 ){ sqlite3 *db = sqlite3_context_db_handle(context); @@ -534,9 +619,9 @@ static void last_insert_rowid( /* ** Implementation of the changes() SQL function. ** -** IMP: R-62073-11209 The changes() SQL function is a wrapper -** around the sqlite3_changes() C/C++ function and hence follows the same -** rules for counting changes. +** IMP: R-32760-32347 The changes() SQL function is a wrapper +** around the sqlite3_changes64() C/C++ function and hence follows the +** same rules for counting changes. */ static void changes( sqlite3_context *context, @@ -545,12 +630,12 @@ static void changes( ){ sqlite3 *db = sqlite3_context_db_handle(context); UNUSED_PARAMETER2(NotUsed, NotUsed2); - sqlite3_result_int(context, sqlite3_changes(db)); + sqlite3_result_int64(context, sqlite3_changes64(db)); } /* ** Implementation of the total_changes() SQL function. The return value is -** the same as the sqlite3_total_changes() API function. +** the same as the sqlite3_total_changes64() API function. */ static void total_changes( sqlite3_context *context, @@ -559,9 +644,9 @@ static void total_changes( ){ sqlite3 *db = sqlite3_context_db_handle(context); UNUSED_PARAMETER2(NotUsed, NotUsed2); - /* IMP: R-52756-41993 This function is a wrapper around the - ** sqlite3_total_changes() C/C++ interface. */ - sqlite3_result_int(context, sqlite3_total_changes(db)); + /* IMP: R-11217-42568 This function is a wrapper around the + ** sqlite3_total_changes64() C/C++ interface. */ + sqlite3_result_int64(context, sqlite3_total_changes64(db)); } /* @@ -576,7 +661,7 @@ struct compareInfo { /* ** For LIKE and GLOB matching on EBCDIC machines, assume that every -** character is exactly one byte in size. Also, provde the Utf8Read() +** character is exactly one byte in size. Also, provide the Utf8Read() ** macro for fast reading of the next character in the common case where ** the next character is ASCII. */ @@ -596,9 +681,19 @@ static const struct compareInfo likeInfoNorm = { '%', '_', 0, 1 }; static const struct compareInfo likeInfoAlt = { '%', '_', 0, 0 }; /* -** Compare two UTF-8 strings for equality where the first string can -** potentially be a "glob" or "like" expression. Return true (1) if they -** are the same and false (0) if they are different. +** Possible error returns from patternMatch() +*/ +#define SQLITE_MATCH 0 +#define SQLITE_NOMATCH 1 +#define SQLITE_NOWILDCARDMATCH 2 + +/* +** Compare two UTF-8 strings for equality where the first string is +** a GLOB or LIKE expression. Return values: +** +** SQLITE_MATCH: Match +** SQLITE_NOMATCH: No match +** SQLITE_NOWILDCARDMATCH: No match in spite of having * or % wildcards. ** ** Globbing rules: ** @@ -618,7 +713,7 @@ static const struct compareInfo likeInfoAlt = { '%', '_', 0, 0 }; ** it the last character in the list. ** ** Like matching rules: -** +** ** '%' Matches any sequence of zero or more characters ** *** '_' Matches any one character @@ -641,75 +736,85 @@ static int patternCompare( u32 matchAll = pInfo->matchAll; /* "*" or "%" */ u8 noCase = pInfo->noCase; /* True if uppercase==lowercase */ const u8 *zEscaped = 0; /* One past the last escaped input char */ - + while( (c = Utf8Read(zPattern))!=0 ){ if( c==matchAll ){ /* Match "*" */ /* Skip over multiple "*" characters in the pattern. If there ** are also "?" characters, skip those as well, but consume a ** single character of the input string for each "?" skipped */ - while( (c=Utf8Read(zPattern)) == matchAll || c == matchOne ){ + while( (c=Utf8Read(zPattern)) == matchAll + || (c == matchOne && matchOne!=0) ){ if( c==matchOne && sqlite3Utf8Read(&zString)==0 ){ - return 0; + return SQLITE_NOWILDCARDMATCH; } } if( c==0 ){ - return 1; /* "*" at the end of the pattern matches */ + return SQLITE_MATCH; /* "*" at the end of the pattern matches */ }else if( c==matchOther ){ if( pInfo->matchSet==0 ){ c = sqlite3Utf8Read(&zPattern); - if( c==0 ) return 0; + if( c==0 ) return SQLITE_NOWILDCARDMATCH; }else{ /* "[...]" immediately follows the "*". We have to do a slow ** recursive search in this case, but it is an unusual case. */ assert( matchOther<0x80 ); /* '[' is a single-byte character */ - while( *zString - && patternCompare(&zPattern[-1],zString,pInfo,matchOther)==0 ){ + while( *zString ){ + int bMatch = patternCompare(&zPattern[-1],zString,pInfo,matchOther); + if( bMatch!=SQLITE_NOMATCH ) return bMatch; SQLITE_SKIP_UTF8(zString); } - return *zString!=0; + return SQLITE_NOWILDCARDMATCH; } } /* At this point variable c contains the first character of the ** pattern string past the "*". Search in the input string for the - ** first matching character and recursively contine the match from + ** first matching character and recursively continue the match from ** that point. ** ** For a case-insensitive search, set variable cx to be the same as ** c but in the other case and search the input string for either ** c or cx. */ - if( c<=0x80 ){ - u32 cx; + if( c<0x80 ){ + char zStop[3]; + int bMatch; if( noCase ){ - cx = sqlite3Toupper(c); - c = sqlite3Tolower(c); + zStop[0] = sqlite3Toupper(c); + zStop[1] = sqlite3Tolower(c); + zStop[2] = 0; }else{ - cx = c; + zStop[0] = c; + zStop[1] = 0; } - while( (c2 = *(zString++))!=0 ){ - if( c2!=c && c2!=cx ) continue; - if( patternCompare(zPattern,zString,pInfo,matchOther) ) return 1; + while(1){ + zString += strcspn((const char*)zString, zStop); + if( zString[0]==0 ) break; + zString++; + bMatch = patternCompare(zPattern,zString,pInfo,matchOther); + if( bMatch!=SQLITE_NOMATCH ) return bMatch; } }else{ + int bMatch; while( (c2 = Utf8Read(zString))!=0 ){ if( c2!=c ) continue; - if( patternCompare(zPattern,zString,pInfo,matchOther) ) return 1; + bMatch = patternCompare(zPattern,zString,pInfo,matchOther); + if( bMatch!=SQLITE_NOMATCH ) return bMatch; } } - return 0; + return SQLITE_NOWILDCARDMATCH; } if( c==matchOther ){ if( pInfo->matchSet==0 ){ c = sqlite3Utf8Read(&zPattern); - if( c==0 ) return 0; + if( c==0 ) return SQLITE_NOMATCH; zEscaped = zPattern; }else{ u32 prior_c = 0; int seen = 0; int invert = 0; c = sqlite3Utf8Read(&zString); - if( c==0 ) return 0; + if( c==0 ) return SQLITE_NOMATCH; c2 = sqlite3Utf8Read(&zPattern); if( c2=='^' ){ invert = 1; @@ -733,34 +838,48 @@ static int patternCompare( c2 = sqlite3Utf8Read(&zPattern); } if( c2==0 || (seen ^ invert)==0 ){ - return 0; + return SQLITE_NOMATCH; } continue; } } c2 = Utf8Read(zString); if( c==c2 ) continue; - if( noCase && c<0x80 && c2<0x80 && sqlite3Tolower(c)==sqlite3Tolower(c2) ){ + if( noCase && sqlite3Tolower(c)==sqlite3Tolower(c2) && c<0x80 && c2<0x80 ){ continue; } if( c==matchOne && zPattern!=zEscaped && c2!=0 ) continue; - return 0; + return SQLITE_NOMATCH; } - return *zString==0; + return *zString==0 ? SQLITE_MATCH : SQLITE_NOMATCH; } /* -** The sqlite3_strglob() interface. +** The sqlite3_strglob() interface. Return 0 on a match (like strcmp()) and +** non-zero if there is no match. */ int sqlite3_strglob(const char *zGlobPattern, const char *zString){ - return patternCompare((u8*)zGlobPattern, (u8*)zString, &globInfo, '[')==0; + if( zString==0 ){ + return zGlobPattern!=0; + }else if( zGlobPattern==0 ){ + return 1; + }else { + return patternCompare((u8*)zGlobPattern, (u8*)zString, &globInfo, '['); + } } /* -** The sqlite3_strlike() interface. +** The sqlite3_strlike() interface. Return 0 on a match and non-zero for +** a miss - like strcmp(). */ int sqlite3_strlike(const char *zPattern, const char *zStr, unsigned int esc){ - return patternCompare((u8*)zPattern, (u8*)zStr, &likeInfoNorm, esc)==0; + if( zStr==0 ){ + return zPattern!=0; + }else if( zPattern==0 ){ + return 1; + }else{ + return patternCompare((u8*)zPattern, (u8*)zStr, &likeInfoNorm, esc); + } } /* @@ -775,7 +894,7 @@ int sqlite3_like_count = 0; /* ** Implementation of the like() SQL function. This function implements -** the build-in LIKE operator. The first argument to the function is the +** the built-in LIKE operator. The first argument to the function is the ** pattern and the second argument is the string. So, the SQL statements: ** ** A LIKE B @@ -786,8 +905,8 @@ int sqlite3_like_count = 0; ** the GLOB operator. */ static void likeFunc( - sqlite3_context *context, - int argc, + sqlite3_context *context, + int argc, sqlite3_value **argv ){ const unsigned char *zA, *zB; @@ -795,6 +914,7 @@ static void likeFunc( int nPat; sqlite3 *db = sqlite3_context_db_handle(context); struct compareInfo *pInfo = sqlite3_user_data(context); + struct compareInfo backupInfo; #ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS if( sqlite3_value_type(argv[0])==SQLITE_BLOB @@ -807,8 +927,6 @@ static void likeFunc( return; } #endif - zB = sqlite3_value_text(argv[0]); - zA = sqlite3_value_text(argv[1]); /* Limit the length of the LIKE or GLOB pattern to avoid problems ** of deep recursion and N*N behavior in patternCompare(). @@ -820,8 +938,6 @@ static void likeFunc( sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1); return; } - assert( zB==sqlite3_value_text(argv[0]) ); /* Encoding did not change */ - if( argc==3 ){ /* The escape character string must consist of a single UTF-8 character. ** Otherwise, return an error. @@ -829,19 +945,28 @@ static void likeFunc( const unsigned char *zEsc = sqlite3_value_text(argv[2]); if( zEsc==0 ) return; if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){ - sqlite3_result_error(context, + sqlite3_result_error(context, "ESCAPE expression must be a single character", -1); return; } escape = sqlite3Utf8Read(&zEsc); + if( escape==pInfo->matchAll || escape==pInfo->matchOne ){ + memcpy(&backupInfo, pInfo, sizeof(backupInfo)); + pInfo = &backupInfo; + if( escape==pInfo->matchAll ) pInfo->matchAll = 0; + if( escape==pInfo->matchOne ) pInfo->matchOne = 0; + } }else{ escape = pInfo->matchSet; } + zB = sqlite3_value_text(argv[0]); + zA = sqlite3_value_text(argv[1]); if( zA && zB ){ #ifdef SQLITE_TEST sqlite3_like_count++; #endif - sqlite3_result_int(context, patternCompare(zB, zA, pInfo, escape)); + sqlite3_result_int(context, + patternCompare(zB, zA, pInfo, escape)==SQLITE_MATCH); } } @@ -933,8 +1058,8 @@ static void compileoptionusedFunc( #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */ /* -** Implementation of the sqlite_compileoption_get() function. -** The result is a string that identifies the compiler options +** Implementation of the sqlite_compileoption_get() function. +** The result is a string that identifies the compiler options ** used to build SQLite. */ #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS @@ -958,43 +1083,46 @@ static void compileoptiongetFunc( ** digits. */ static const char hexdigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', - '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' + '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; /* -** Implementation of the QUOTE() function. This function takes a single -** argument. If the argument is numeric, the return value is the same as -** the argument. If the argument is NULL, the return value is the string -** "NULL". Otherwise, the argument is enclosed in single quotes with -** single-quote escapes. +** Append to pStr text that is the SQL literal representation of the +** value contained in pValue. */ -static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ - assert( argc==1 ); - UNUSED_PARAMETER(argc); - switch( sqlite3_value_type(argv[0]) ){ +void sqlite3QuoteValue(StrAccum *pStr, sqlite3_value *pValue, int bEscape){ + /* As currently implemented, the string must be initially empty. + ** we might relax this requirement in the future, but that will + ** require enhancements to the implementation. */ + assert( pStr!=0 && pStr->nChar==0 ); + + switch( sqlite3_value_type(pValue) ){ case SQLITE_FLOAT: { double r1, r2; - char zBuf[50]; - r1 = sqlite3_value_double(argv[0]); - sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.15g", r1); - sqlite3AtoF(zBuf, &r2, 20, SQLITE_UTF8); - if( r1!=r2 ){ - sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.20e", r1); + const char *zVal; + r1 = sqlite3_value_double(pValue); + sqlite3_str_appendf(pStr, "%!0.15g", r1); + zVal = sqlite3_str_value(pStr); + if( zVal ){ + sqlite3AtoF(zVal, &r2, pStr->nChar, SQLITE_UTF8); + if( r1!=r2 ){ + sqlite3_str_reset(pStr); + sqlite3_str_appendf(pStr, "%!0.20e", r1); + } } - sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); break; } case SQLITE_INTEGER: { - sqlite3_result_value(context, argv[0]); + sqlite3_str_appendf(pStr, "%lld", sqlite3_value_int64(pValue)); break; } case SQLITE_BLOB: { - char *zText = 0; - char const *zBlob = sqlite3_value_blob(argv[0]); - int nBlob = sqlite3_value_bytes(argv[0]); - assert( zBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */ - zText = (char *)contextMalloc(context, (2*(i64)nBlob)+4); - if( zText ){ + char const *zBlob = sqlite3_value_blob(pValue); + i64 nBlob = sqlite3_value_bytes(pValue); + assert( zBlob==sqlite3_value_blob(pValue) ); /* No encoding change */ + sqlite3StrAccumEnlarge(pStr, nBlob*2 + 4); + if( pStr->accError==0 ){ + char *zText = pStr->zText; int i; for(i=0; i>4)&0x0F]; @@ -1004,45 +1132,154 @@ static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ zText[(nBlob*2)+3] = '\0'; zText[0] = 'X'; zText[1] = '\''; - sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT); - sqlite3_free(zText); + pStr->nChar = nBlob*2 + 3; } break; } case SQLITE_TEXT: { - int i,j; - u64 n; - const unsigned char *zArg = sqlite3_value_text(argv[0]); - char *z; - - if( zArg==0 ) return; - for(i=0, n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; } - z = contextMalloc(context, ((i64)i)+((i64)n)+3); - if( z ){ - z[0] = '\''; - for(i=0, j=1; zArg[i]; i++){ - z[j++] = zArg[i]; - if( zArg[i]=='\'' ){ - z[j++] = '\''; - } - } - z[j++] = '\''; - z[j] = 0; - sqlite3_result_text(context, z, j, sqlite3_free); - } + const unsigned char *zArg = sqlite3_value_text(pValue); + sqlite3_str_appendf(pStr, bEscape ? "%#Q" : "%Q", zArg); break; } default: { - assert( sqlite3_value_type(argv[0])==SQLITE_NULL ); - sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC); + assert( sqlite3_value_type(pValue)==SQLITE_NULL ); + sqlite3_str_append(pStr, "NULL", 4); + break; + } + } +} + +/* +** Return true if z[] begins with N hexadecimal digits, and write +** a decoding of those digits into *pVal. Or return false if any +** one of the first N characters in z[] is not a hexadecimal digit. +*/ +static int isNHex(const char *z, int N, u32 *pVal){ + int i; + u32 v = 0; + for(i=0; i0 ){ + memmove(&zOut[j], &zIn[i], n); + j += n; + i += n; + } + if( zIn[i+1]=='\\' ){ + i += 2; + zOut[j++] = '\\'; + }else if( sqlite3Isxdigit(zIn[i+1]) ){ + if( !isNHex(&zIn[i+1], 4, &v) ) goto unistr_error; + i += 5; + j += sqlite3AppendOneUtf8Character(&zOut[j], v); + }else if( zIn[i+1]=='+' ){ + if( !isNHex(&zIn[i+2], 6, &v) ) goto unistr_error; + i += 8; + j += sqlite3AppendOneUtf8Character(&zOut[j], v); + }else if( zIn[i+1]=='u' ){ + if( !isNHex(&zIn[i+2], 4, &v) ) goto unistr_error; + i += 6; + j += sqlite3AppendOneUtf8Character(&zOut[j], v); + }else if( zIn[i+1]=='U' ){ + if( !isNHex(&zIn[i+2], 8, &v) ) goto unistr_error; + i += 10; + j += sqlite3AppendOneUtf8Character(&zOut[j], v); + }else{ + goto unistr_error; + } + } + zOut[j] = 0; + sqlite3_result_text64(context, zOut, j, sqlite3_free, SQLITE_UTF8); + return; + +unistr_error: + sqlite3_free(zOut); + sqlite3_result_error(context, "invalid Unicode escape", -1); + return; +} + + +/* +** Implementation of the QUOTE() function. +** +** The quote(X) function returns the text of an SQL literal which is the +** value of its argument suitable for inclusion into an SQL statement. +** Strings are surrounded by single-quotes with escapes on interior quotes +** as needed. BLOBs are encoded as hexadecimal literals. Strings with +** embedded NUL characters cannot be represented as string literals in SQL +** and hence the returned string literal is truncated prior to the first NUL. +** +** If sqlite3_user_data() is non-zero, then the UNISTR_QUOTE() function is +** implemented instead. The difference is that UNISTR_QUOTE() uses the +** UNISTR() function to escape control characters. +*/ +static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ + sqlite3_str str; + sqlite3 *db = sqlite3_context_db_handle(context); + assert( argc==1 ); + UNUSED_PARAMETER(argc); + sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]); + sqlite3QuoteValue(&str,argv[0],SQLITE_PTR_TO_INT(sqlite3_user_data(context))); + sqlite3_result_text(context, sqlite3StrAccumFinish(&str), str.nChar, + SQLITE_DYNAMIC); + if( str.accError!=SQLITE_OK ){ + sqlite3_result_null(context); + sqlite3_result_error_code(context, str.accError); } } /* ** The unicode() function. Return the integer unicode code-point value -** for the first character of the input string. +** for the first character of the input string. */ static void unicodeFunc( sqlite3_context *context, @@ -1093,6 +1330,7 @@ static void charFunc( *zOut++ = 0x80 + (u8)(c & 0x3F); } \ } + *zOut = 0; sqlite3_result_text64(context, (char*)z, zOut-z, sqlite3_free, SQLITE_UTF8); } @@ -1121,10 +1359,101 @@ static void hexFunc( *(z++) = hexdigits[c&0xf]; } *z = 0; - sqlite3_result_text(context, zHex, n*2, sqlite3_free); + sqlite3_result_text64(context, zHex, (u64)(z-zHex), + sqlite3_free, SQLITE_UTF8); + } +} + +/* +** Buffer zStr contains nStr bytes of utf-8 encoded text. Return 1 if zStr +** contains character ch, or 0 if it does not. +*/ +static int strContainsChar(const u8 *zStr, int nStr, u32 ch){ + const u8 *zEnd = &zStr[nStr]; + const u8 *z = zStr; + while( zaLimit[SQLITE_LIMIT_LENGTH] ); - testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] ); - if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){ - sqlite3_result_error_toobig(context); - sqlite3_free(zOut); - return; - } - zOld = zOut; - zOut = sqlite3_realloc64(zOut, (int)nOut); - if( zOut==0 ){ - sqlite3_result_error_nomem(context); - sqlite3_free(zOld); - return; + if( nRep>nPattern ){ + nOut += nRep - nPattern; + testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] ); + testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] ); + if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){ + sqlite3_result_error_toobig(context); + sqlite3_free(zOut); + return; + } + cntExpand++; + if( (cntExpand&(cntExpand-1))==0 ){ + /* Grow the size of the output buffer only on substitutions + ** whose index is a power of two: 1, 2, 4, 8, 16, 32, ... */ + u8 *zOld; + zOld = zOut; + zOut = sqlite3Realloc(zOut, (int)nOut + (nOut - nStr - 1)); + if( zOut==0 ){ + sqlite3_result_error_nomem(context); + sqlite3_free(zOld); + return; + } + } } memcpy(&zOut[j], zRep, nRep); j += nRep; i += nPattern-1; } } - assert( j+nStr-i+1==nOut ); + assert( j+nStr-i+1<=nOut ); memcpy(&zOut[j], &zStr[i], nStr-i); j += nStr - i; assert( j<=nOut ); @@ -1242,10 +1580,10 @@ static void trimFunc( ){ const unsigned char *zIn; /* Input string */ const unsigned char *zCharSet; /* Set of characters to trim */ - int nIn; /* Number of bytes in input */ + unsigned int nIn; /* Number of bytes in input */ int flags; /* 1: trimleft 2: trimright 3: trim */ int i; /* Loop counter */ - unsigned char *aLen = 0; /* Length of each character in zCharSet */ + unsigned int *aLen = 0; /* Length of each character in zCharSet */ unsigned char **azChar = 0; /* Individual characters in zCharSet */ int nChar; /* Number of characters in zCharSet */ @@ -1254,13 +1592,13 @@ static void trimFunc( } zIn = sqlite3_value_text(argv[0]); if( zIn==0 ) return; - nIn = sqlite3_value_bytes(argv[0]); + nIn = (unsigned)sqlite3_value_bytes(argv[0]); assert( zIn==sqlite3_value_text(argv[0]) ); if( argc==1 ){ - static const unsigned char lenOne[] = { 1 }; + static const unsigned lenOne[] = { 1 }; static unsigned char * const azOne[] = { (u8*)" " }; nChar = 1; - aLen = (u8*)lenOne; + aLen = (unsigned*)lenOne; azChar = (unsigned char **)azOne; zCharSet = 0; }else if( (zCharSet = sqlite3_value_text(argv[1]))==0 ){ @@ -1271,15 +1609,16 @@ static void trimFunc( SQLITE_SKIP_UTF8(z); } if( nChar>0 ){ - azChar = contextMalloc(context, ((i64)nChar)*(sizeof(char*)+1)); + azChar = contextMalloc(context, + ((i64)nChar)*(sizeof(char*)+sizeof(unsigned))); if( azChar==0 ){ return; } - aLen = (unsigned char*)&azChar[nChar]; + aLen = (unsigned*)&azChar[nChar]; for(z=zCharSet, nChar=0; *z; nChar++){ azChar[nChar] = (unsigned char *)z; SQLITE_SKIP_UTF8(z); - aLen[nChar] = (u8)(z - azChar[nChar]); + aLen[nChar] = (unsigned)(z - azChar[nChar]); } } } @@ -1287,7 +1626,7 @@ static void trimFunc( flags = SQLITE_PTR_TO_INT(sqlite3_user_data(context)); if( flags & 1 ){ while( nIn>0 ){ - int len = 0; + unsigned int len = 0; for(i=0; i0 ){ - int len = 0; + unsigned int len = 0; for(i=0; i0 ){ + memcpy(&z[j], zSep, nSep); + j += nSep; + } + memcpy(&z[j], v, k); + j += k; + bNotNull = 1; + } + } + } + z[j] = 0; + assert( j<=n ); + sqlite3_result_text64(context, z, j, sqlite3_free, SQLITE_UTF8); +} + +/* +** The CONCAT(...) function. Generate a string result that is the +** concatentation of all non-null arguments. +*/ +static void concatFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + concatFuncCore(context, argc, argv, 0, ""); +} + +/* +** The CONCAT_WS(separator, ...) function. +** +** Generate a string that is the concatenation of 2nd through the Nth +** argument. Use the first argument (which must be non-NULL) as the +** separator. +*/ +static void concatwsFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + int nSep = sqlite3_value_bytes(argv[0]); + const char *zSep = (const char*)sqlite3_value_text(argv[0]); + if( zSep==0 ) return; + concatFuncCore(context, argc-1, argv+1, nSep, zSep); +} + + +#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION +/* +** The "unknown" function is automatically substituted in place of +** any unrecognized function name when doing an EXPLAIN or EXPLAIN QUERY PLAN +** when the SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION compile-time option is used. +** When the "sqlite3" command-line shell is built using this functionality, +** that allows an EXPLAIN or EXPLAIN QUERY PLAN for complex queries +** involving application-defined functions to be examined in a generic +** sqlite3 shell. +*/ +static void unknownFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + /* no-op */ + (void)context; + (void)argc; + (void)argv; +} +#endif /*SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION*/ + /* IMP: R-25361-16150 This function is omitted from SQLite by default. It ** is only available if the SQLITE_SOUNDEX compile-time option is used @@ -1325,7 +1764,7 @@ static void trimFunc( ** Compute the soundex encoding of a word. ** ** IMP: R-59782-00072 The soundex(X) function returns a string that is the -** soundex encoding of the string X. +** soundex encoding of the string X. */ static void soundexFunc( sqlite3_context *context, @@ -1386,6 +1825,14 @@ static void loadExt(sqlite3_context *context, int argc, sqlite3_value **argv){ sqlite3 *db = sqlite3_context_db_handle(context); char *zErrMsg = 0; + /* Disallow the load_extension() SQL function unless the SQLITE_LoadExtFunc + ** flag is set. See the sqlite3_enable_load_extension() API. + */ + if( (db->flags & SQLITE_LoadExtFunc)==0 ){ + sqlite3_result_error(context, "not authorized", -1); + return; + } + if( argc==2 ){ zProc = (const char *)sqlite3_value_text(argv[1]); }else{ @@ -1405,13 +1852,68 @@ static void loadExt(sqlite3_context *context, int argc, sqlite3_value **argv){ */ typedef struct SumCtx SumCtx; struct SumCtx { - double rSum; /* Floating point sum */ - i64 iSum; /* Integer sum */ + double rSum; /* Running sum as as a double */ + double rErr; /* Error term for Kahan-Babushka-Neumaier summation */ + i64 iSum; /* Running sum as a signed integer */ i64 cnt; /* Number of elements summed */ - u8 overflow; /* True if integer overflow seen */ - u8 approx; /* True if non-integer value was input to the sum */ + u8 approx; /* True if any non-integer value was input to the sum */ + u8 ovrfl; /* Integer overflow seen */ }; +/* +** Do one step of the Kahan-Babushka-Neumaier summation. +** +** https://en.wikipedia.org/wiki/Kahan_summation_algorithm +** +** Variables are marked "volatile" to defeat c89 x86 floating point +** optimizations can mess up this algorithm. +*/ +static void kahanBabuskaNeumaierStep( + volatile SumCtx *pSum, + volatile double r +){ + volatile double s = pSum->rSum; + volatile double t = s + r; + if( fabs(s) > fabs(r) ){ + pSum->rErr += (s - t) + r; + }else{ + pSum->rErr += (r - t) + s; + } + pSum->rSum = t; +} + +/* +** Add a (possibly large) integer to the running sum. +*/ +static void kahanBabuskaNeumaierStepInt64(volatile SumCtx *pSum, i64 iVal){ + if( iVal<=-4503599627370496LL || iVal>=+4503599627370496LL ){ + i64 iBig, iSm; + iSm = iVal % 16384; + iBig = iVal - iSm; + kahanBabuskaNeumaierStep(pSum, iBig); + kahanBabuskaNeumaierStep(pSum, iSm); + }else{ + kahanBabuskaNeumaierStep(pSum, (double)iVal); + } +} + +/* +** Initialize the Kahan-Babaska-Neumaier sum from a 64-bit integer +*/ +static void kahanBabuskaNeumaierInit( + volatile SumCtx *p, + i64 iVal +){ + if( iVal<=-4503599627370496LL || iVal>=+4503599627370496LL ){ + i64 iSm = iVal % 16384; + p->rSum = (double)(iVal - iSm); + p->rErr = (double)iSm; + }else{ + p->rSum = (double)iVal; + p->rErr = 0.0; + } +} + /* ** Routines used to compute the sum, average, and total. ** @@ -1419,7 +1921,7 @@ struct SumCtx { ** that it returns NULL if it sums over no inputs. TOTAL returns ** 0.0 in that case. In addition, TOTAL always returns a float where ** SUM might return an integer if it never encounters a floating point -** value. TOTAL never fails, but SUM might through an exception if +** value. TOTAL never fails, but SUM might throw an exception if ** it overflows an integer. */ static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){ @@ -1431,26 +1933,78 @@ static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){ type = sqlite3_value_numeric_type(argv[0]); if( p && type!=SQLITE_NULL ){ p->cnt++; - if( type==SQLITE_INTEGER ){ - i64 v = sqlite3_value_int64(argv[0]); - p->rSum += v; - if( (p->approx|p->overflow)==0 && sqlite3AddInt64(&p->iSum, v) ){ - p->overflow = 1; + if( p->approx==0 ){ + if( type!=SQLITE_INTEGER ){ + kahanBabuskaNeumaierInit(p, p->iSum); + p->approx = 1; + kahanBabuskaNeumaierStep(p, sqlite3_value_double(argv[0])); + }else{ + i64 x = p->iSum; + if( sqlite3AddInt64(&x, sqlite3_value_int64(argv[0]))==0 ){ + p->iSum = x; + }else{ + p->ovrfl = 1; + kahanBabuskaNeumaierInit(p, p->iSum); + p->approx = 1; + kahanBabuskaNeumaierStepInt64(p, sqlite3_value_int64(argv[0])); + } + } + }else{ + if( type==SQLITE_INTEGER ){ + kahanBabuskaNeumaierStepInt64(p, sqlite3_value_int64(argv[0])); + }else{ + p->ovrfl = 0; + kahanBabuskaNeumaierStep(p, sqlite3_value_double(argv[0])); + } + } + } +} +#ifndef SQLITE_OMIT_WINDOWFUNC +static void sumInverse(sqlite3_context *context, int argc, sqlite3_value**argv){ + SumCtx *p; + int type; + assert( argc==1 ); + UNUSED_PARAMETER(argc); + p = sqlite3_aggregate_context(context, sizeof(*p)); + type = sqlite3_value_numeric_type(argv[0]); + /* p is always non-NULL because sumStep() will have been called first + ** to initialize it */ + if( ALWAYS(p) && type!=SQLITE_NULL ){ + assert( p->cnt>0 ); + p->cnt--; + if( !p->approx ){ + if( sqlite3SubInt64(&p->iSum, sqlite3_value_int64(argv[0])) ){ + p->ovrfl = 1; + p->approx = 1; } + }else if( type==SQLITE_INTEGER ){ + i64 iVal = sqlite3_value_int64(argv[0]); + if( iVal!=SMALLEST_INT64 ){ + kahanBabuskaNeumaierStepInt64(p, -iVal); + }else{ + kahanBabuskaNeumaierStepInt64(p, LARGEST_INT64); + kahanBabuskaNeumaierStepInt64(p, 1); + } }else{ - p->rSum += sqlite3_value_double(argv[0]); - p->approx = 1; + kahanBabuskaNeumaierStep(p, -sqlite3_value_double(argv[0])); } } } +#else +# define sumInverse 0 +#endif /* SQLITE_OMIT_WINDOWFUNC */ static void sumFinalize(sqlite3_context *context){ SumCtx *p; p = sqlite3_aggregate_context(context, 0); if( p && p->cnt>0 ){ - if( p->overflow ){ - sqlite3_result_error(context,"integer overflow",-1); - }else if( p->approx ){ - sqlite3_result_double(context, p->rSum); + if( p->approx ){ + if( p->ovrfl ){ + sqlite3_result_error(context,"integer overflow",-1); + }else if( !sqlite3IsOverflow(p->rErr) ){ + sqlite3_result_double(context, p->rSum+p->rErr); + }else{ + sqlite3_result_double(context, p->rSum); + } }else{ sqlite3_result_int64(context, p->iSum); } @@ -1460,14 +2014,29 @@ static void avgFinalize(sqlite3_context *context){ SumCtx *p; p = sqlite3_aggregate_context(context, 0); if( p && p->cnt>0 ){ - sqlite3_result_double(context, p->rSum/(double)p->cnt); + double r; + if( p->approx ){ + r = p->rSum; + if( !sqlite3IsOverflow(p->rErr) ) r += p->rErr; + }else{ + r = (double)(p->iSum); + } + sqlite3_result_double(context, r/(double)p->cnt); } } static void totalFinalize(sqlite3_context *context){ SumCtx *p; + double r = 0.0; p = sqlite3_aggregate_context(context, 0); - /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */ - sqlite3_result_double(context, p ? p->rSum : (double)0); + if( p ){ + if( p->approx ){ + r = p->rSum; + if( !sqlite3IsOverflow(p->rErr) ) r += p->rErr; + }else{ + r = (double)(p->iSum); + } + } + sqlite3_result_double(context, r); } /* @@ -1477,6 +2046,9 @@ static void totalFinalize(sqlite3_context *context){ typedef struct CountCtx CountCtx; struct CountCtx { i64 n; +#ifdef SQLITE_DEBUG + int bInverse; /* True if xInverse() ever called */ +#endif }; /* @@ -1491,25 +2063,40 @@ static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){ #ifndef SQLITE_OMIT_DEPRECATED /* The sqlite3_aggregate_count() function is deprecated. But just to make - ** sure it still operates correctly, verify that its count agrees with our + ** sure it still operates correctly, verify that its count agrees with our ** internal count when using count(*) and when the total count can be ** expressed as a 32-bit integer. */ - assert( argc==1 || p==0 || p->n>0x7fffffff + assert( argc==1 || p==0 || p->n>0x7fffffff || p->bInverse || p->n==sqlite3_aggregate_count(context) ); #endif -} +} static void countFinalize(sqlite3_context *context){ CountCtx *p; p = sqlite3_aggregate_context(context, 0); sqlite3_result_int64(context, p ? p->n : 0); } +#ifndef SQLITE_OMIT_WINDOWFUNC +static void countInverse(sqlite3_context *ctx, int argc, sqlite3_value **argv){ + CountCtx *p; + p = sqlite3_aggregate_context(ctx, sizeof(*p)); + /* p is always non-NULL since countStep() will have been called first */ + if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && ALWAYS(p) ){ + p->n--; +#ifdef SQLITE_DEBUG + p->bInverse = 1; +#endif + } +} +#else +# define countInverse 0 +#endif /* SQLITE_OMIT_WINDOWFUNC */ /* ** Routines to implement min() and max() aggregate functions. */ static void minmaxStep( - sqlite3_context *context, - int NotUsed, + sqlite3_context *context, + int NotUsed, sqlite3_value **argv ){ Mem *pArg = (Mem *)argv[0]; @@ -1519,7 +2106,7 @@ static void minmaxStep( pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest)); if( !pBest ) return; - if( sqlite3_value_type(argv[0])==SQLITE_NULL ){ + if( sqlite3_value_type(pArg)==SQLITE_NULL ){ if( pBest->flags ) sqlite3SkipAccumulatorLoad(context); }else if( pBest->flags ){ int max; @@ -1545,133 +2132,254 @@ static void minmaxStep( sqlite3VdbeMemCopy(pBest, pArg); } } -static void minMaxFinalize(sqlite3_context *context){ +static void minMaxValueFinalize(sqlite3_context *context, int bValue){ sqlite3_value *pRes; pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0); if( pRes ){ if( pRes->flags ){ sqlite3_result_value(context, pRes); } - sqlite3VdbeMemRelease(pRes); + if( bValue==0 ) sqlite3VdbeMemRelease(pRes); } } +#ifndef SQLITE_OMIT_WINDOWFUNC +static void minMaxValue(sqlite3_context *context){ + minMaxValueFinalize(context, 1); +} +#else +# define minMaxValue 0 +#endif /* SQLITE_OMIT_WINDOWFUNC */ +static void minMaxFinalize(sqlite3_context *context){ + minMaxValueFinalize(context, 0); +} /* ** group_concat(EXPR, ?SEPARATOR?) +** string_agg(EXPR, SEPARATOR) +** +** Content is accumulated in GroupConcatCtx.str with the SEPARATOR +** coming before the EXPR value, except for the first entry which +** omits the SEPARATOR. +** +** It is tragic that the SEPARATOR goes before the EXPR string. The +** groupConcatInverse() implementation would have been easier if the +** SEPARATOR were appended after EXPR. And the order is undocumented, +** so we could change it, in theory. But the old behavior has been +** around for so long that we dare not, for fear of breaking something. */ +typedef struct { + StrAccum str; /* The accumulated concatenation */ +#ifndef SQLITE_OMIT_WINDOWFUNC + int nAccum; /* Number of strings presently concatenated */ + int nFirstSepLength; /* Used to detect separator length change */ + /* If pnSepLengths!=0, refs an array of inter-string separator lengths, + ** stored as actually incorporated into presently accumulated result. + ** (Hence, its slots in use number nAccum-1 between method calls.) + ** If pnSepLengths==0, nFirstSepLength is the length used throughout. + */ + int *pnSepLengths; +#endif +} GroupConcatCtx; + static void groupConcatStep( sqlite3_context *context, int argc, sqlite3_value **argv ){ const char *zVal; - StrAccum *pAccum; + GroupConcatCtx *pGCC; const char *zSep; int nVal, nSep; assert( argc==1 || argc==2 ); if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; - pAccum = (StrAccum*)sqlite3_aggregate_context(context, sizeof(*pAccum)); - - if( pAccum ){ + pGCC = (GroupConcatCtx*)sqlite3_aggregate_context(context, sizeof(*pGCC)); + if( pGCC ){ sqlite3 *db = sqlite3_context_db_handle(context); - int firstTerm = pAccum->mxAlloc==0; - pAccum->mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH]; - if( !firstTerm ){ - if( argc==2 ){ - zSep = (char*)sqlite3_value_text(argv[1]); - nSep = sqlite3_value_bytes(argv[1]); - }else{ - zSep = ","; - nSep = 1; + int firstTerm = pGCC->str.mxAlloc==0; + pGCC->str.mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH]; + if( argc==1 ){ + if( !firstTerm ){ + sqlite3_str_appendchar(&pGCC->str, 1, ','); + } +#ifndef SQLITE_OMIT_WINDOWFUNC + else{ + pGCC->nFirstSepLength = 1; + } +#endif + }else if( !firstTerm ){ + zSep = (char*)sqlite3_value_text(argv[1]); + nSep = sqlite3_value_bytes(argv[1]); + if( zSep ){ + sqlite3_str_append(&pGCC->str, zSep, nSep); } - if( nSep ) sqlite3StrAccumAppend(pAccum, zSep, nSep); +#ifndef SQLITE_OMIT_WINDOWFUNC + else{ + nSep = 0; + } + if( nSep != pGCC->nFirstSepLength || pGCC->pnSepLengths != 0 ){ + int *pnsl = pGCC->pnSepLengths; + if( pnsl == 0 ){ + /* First separator length variation seen, start tracking them. */ + pnsl = (int*)sqlite3_malloc64((pGCC->nAccum+1) * sizeof(int)); + if( pnsl!=0 ){ + int i = 0, nA = pGCC->nAccum-1; + while( inFirstSepLength; + } + }else{ + pnsl = (int*)sqlite3_realloc64(pnsl, pGCC->nAccum * sizeof(int)); + } + if( pnsl!=0 ){ + if( ALWAYS(pGCC->nAccum>0) ){ + pnsl[pGCC->nAccum-1] = nSep; + } + pGCC->pnSepLengths = pnsl; + }else{ + sqlite3StrAccumSetError(&pGCC->str, SQLITE_NOMEM); + } + } +#endif + } +#ifndef SQLITE_OMIT_WINDOWFUNC + else{ + pGCC->nFirstSepLength = sqlite3_value_bytes(argv[1]); } + pGCC->nAccum += 1; +#endif zVal = (char*)sqlite3_value_text(argv[0]); nVal = sqlite3_value_bytes(argv[0]); - if( zVal ) sqlite3StrAccumAppend(pAccum, zVal, nVal); + if( zVal ) sqlite3_str_append(&pGCC->str, zVal, nVal); + } +} + +#ifndef SQLITE_OMIT_WINDOWFUNC +static void groupConcatInverse( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + GroupConcatCtx *pGCC; + assert( argc==1 || argc==2 ); + (void)argc; /* Suppress unused parameter warning */ + if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; + pGCC = (GroupConcatCtx*)sqlite3_aggregate_context(context, sizeof(*pGCC)); + /* pGCC is always non-NULL since groupConcatStep() will have always + ** run first to initialize it */ + if( ALWAYS(pGCC) ){ + int nVS; /* Number of characters to remove */ + /* Must call sqlite3_value_text() to convert the argument into text prior + ** to invoking sqlite3_value_bytes(), in case the text encoding is UTF16 */ + (void)sqlite3_value_text(argv[0]); + nVS = sqlite3_value_bytes(argv[0]); + pGCC->nAccum -= 1; + if( pGCC->pnSepLengths!=0 ){ + assert(pGCC->nAccum >= 0); + if( pGCC->nAccum>0 ){ + nVS += *pGCC->pnSepLengths; + memmove(pGCC->pnSepLengths, pGCC->pnSepLengths+1, + (pGCC->nAccum-1)*sizeof(int)); + } + }else{ + /* If removing single accumulated string, harmlessly over-do. */ + nVS += pGCC->nFirstSepLength; + } + if( nVS>=(int)pGCC->str.nChar ){ + pGCC->str.nChar = 0; + }else{ + pGCC->str.nChar -= nVS; + memmove(pGCC->str.zText, &pGCC->str.zText[nVS], pGCC->str.nChar); + } + if( pGCC->str.nChar==0 ){ + pGCC->str.mxAlloc = 0; + sqlite3_free(pGCC->pnSepLengths); + pGCC->pnSepLengths = 0; + } } } +#else +# define groupConcatInverse 0 +#endif /* SQLITE_OMIT_WINDOWFUNC */ static void groupConcatFinalize(sqlite3_context *context){ - StrAccum *pAccum; - pAccum = sqlite3_aggregate_context(context, 0); - if( pAccum ){ - if( pAccum->accError==STRACCUM_TOOBIG ){ + GroupConcatCtx *pGCC + = (GroupConcatCtx*)sqlite3_aggregate_context(context, 0); + if( pGCC ){ + sqlite3ResultStrAccum(context, &pGCC->str); +#ifndef SQLITE_OMIT_WINDOWFUNC + sqlite3_free(pGCC->pnSepLengths); +#endif + } +} +#ifndef SQLITE_OMIT_WINDOWFUNC +static void groupConcatValue(sqlite3_context *context){ + GroupConcatCtx *pGCC + = (GroupConcatCtx*)sqlite3_aggregate_context(context, 0); + if( pGCC ){ + StrAccum *pAccum = &pGCC->str; + if( pAccum->accError==SQLITE_TOOBIG ){ sqlite3_result_error_toobig(context); - }else if( pAccum->accError==STRACCUM_NOMEM ){ + }else if( pAccum->accError==SQLITE_NOMEM ){ sqlite3_result_error_nomem(context); - }else{ - sqlite3_result_text(context, sqlite3StrAccumFinish(pAccum), -1, - sqlite3_free); + }else if( pGCC->nAccum>0 && pAccum->nChar==0 ){ + sqlite3_result_text(context, "", 1, SQLITE_STATIC); + }else{ + const char *zText = sqlite3_str_value(pAccum); + sqlite3_result_text(context, zText, pAccum->nChar, SQLITE_TRANSIENT); } } } +#else +# define groupConcatValue 0 +#endif /* SQLITE_OMIT_WINDOWFUNC */ /* ** This routine does per-connection function registration. Most ** of the built-in functions above are part of the global function set. ** This routine only deals with those that are not global. */ -void sqlite3RegisterBuiltinFunctions(sqlite3 *db){ +void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3 *db){ int rc = sqlite3_overload_function(db, "MATCH", 2); -/* BEGIN SQLCIPHER */ -#ifdef SQLITE_HAS_CODEC -#ifndef OMIT_EXPORT - extern void sqlcipher_exportFunc(sqlite3_context *, int, sqlite3_value **); -#endif -#endif -/* END SQLCIPHER */ assert( rc==SQLITE_NOMEM || rc==SQLITE_OK ); if( rc==SQLITE_NOMEM ){ sqlite3OomFault(db); } -/* BEGIN SQLCIPHER */ -#ifdef SQLITE_HAS_CODEC -#ifndef OMIT_EXPORT - sqlite3CreateFunc(db, "sqlcipher_export", 1, SQLITE_TEXT, 0, sqlcipher_exportFunc, 0, 0, 0); -#endif -#endif -/* END SQLCIPHER */ } /* -** Set the LIKEOPT flag on the 2-argument function with the given name. -*/ -static void setLikeOptFlag(sqlite3 *db, const char *zName, u8 flagVal){ - FuncDef *pDef; - pDef = sqlite3FindFunction(db, zName, sqlite3Strlen30(zName), - 2, SQLITE_UTF8, 0); - if( ALWAYS(pDef) ){ - pDef->funcFlags |= flagVal; - } -} - -/* -** Register the built-in LIKE and GLOB functions. The caseSensitive +** Re-register the built-in LIKE functions. The caseSensitive ** parameter determines whether or not the LIKE operator is case -** sensitive. GLOB is always case sensitive. +** sensitive. */ void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){ + FuncDef *pDef; struct compareInfo *pInfo; + int flags; + int nArg; if( caseSensitive ){ pInfo = (struct compareInfo*)&likeInfoAlt; + flags = SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE; }else{ pInfo = (struct compareInfo*)&likeInfoNorm; + flags = SQLITE_FUNC_LIKE; + } + for(nArg=2; nArg<=3; nArg++){ + sqlite3CreateFunc(db, "like", nArg, SQLITE_UTF8, pInfo, likeFunc, + 0, 0, 0, 0, 0); + pDef = sqlite3FindFunction(db, "like", nArg, SQLITE_UTF8, 0); + pDef->funcFlags |= flags; + pDef->funcFlags &= ~SQLITE_FUNC_UNSAFE; } - sqlite3CreateFunc(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0); - sqlite3CreateFunc(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0); - sqlite3CreateFunc(db, "glob", 2, SQLITE_UTF8, - (struct compareInfo*)&globInfo, likeFunc, 0, 0, 0); - setLikeOptFlag(db, "glob", SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE); - setLikeOptFlag(db, "like", - caseSensitive ? (SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE) : SQLITE_FUNC_LIKE); } /* ** pExpr points to an expression which implements a function. If ** it is appropriate to apply the LIKE optimization to that function -** then set aWc[0] through aWc[2] to the wildcard characters and -** return TRUE. If the function is not a LIKE-style function then -** return FALSE. +** then set aWc[0] through aWc[2] to the wildcard characters and the +** escape character and then return TRUE. If the function is not a +** LIKE-style function then return FALSE. +** +** The expression "a LIKE b ESCAPE c" is only considered a valid LIKE +** operator if c is a string literal that is exactly one byte in length. +** That one byte is stored in aWc[3]. aWc[3] is set to zero if there is +** no ESCAPE clause. ** ** *pIsNocase is set to true if uppercase and lowercase are equivalent for ** the function (default for LIKE). If the function makes the distinction @@ -1680,16 +2388,19 @@ void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){ */ int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){ FuncDef *pDef; - if( pExpr->op!=TK_FUNCTION - || !pExpr->x.pList - || pExpr->x.pList->nExpr!=2 - ){ + int nExpr; + assert( pExpr!=0 ); + assert( pExpr->op==TK_FUNCTION ); + assert( ExprUseXList(pExpr) ); + if( !pExpr->x.pList ){ return 0; } - assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); - pDef = sqlite3FindFunction(db, pExpr->u.zToken, - sqlite3Strlen30(pExpr->u.zToken), - 2, SQLITE_UTF8, 0); + nExpr = pExpr->x.pList->nExpr; + assert( !ExprHasProperty(pExpr, EP_IntValue) ); + pDef = sqlite3FindFunction(db, pExpr->u.zToken, nExpr, SQLITE_UTF8, 0); +#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION + if( pDef==0 ) return 0; +#endif if( NEVER(pDef==0) || (pDef->funcFlags & SQLITE_FUNC_LIKE)==0 ){ return 0; } @@ -1702,10 +2413,841 @@ int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){ assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll ); assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne ); assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet ); + + if( nExpr<3 ){ + aWc[3] = 0; + }else{ + Expr *pEscape = pExpr->x.pList->a[2].pExpr; + char *zEscape; + if( pEscape->op!=TK_STRING ) return 0; + assert( !ExprHasProperty(pEscape, EP_IntValue) ); + zEscape = pEscape->u.zToken; + if( zEscape[0]==0 || zEscape[1]!=0 ) return 0; + if( zEscape[0]==aWc[0] ) return 0; + if( zEscape[0]==aWc[1] ) return 0; + aWc[3] = zEscape[0]; + } + *pIsNocase = (pDef->funcFlags & SQLITE_FUNC_CASE)==0; return 1; } +/* Mathematical Constants */ +#ifndef M_PI +# define M_PI 3.141592653589793238462643383279502884 +#endif +#ifndef M_LN10 +# define M_LN10 2.302585092994045684017991454684364208 +#endif +#ifndef M_LN2 +# define M_LN2 0.693147180559945309417232121458176568 +#endif + + +/* Extra math functions that require linking with -lm +*/ +#ifdef SQLITE_ENABLE_MATH_FUNCTIONS +/* +** Implementation SQL functions: +** +** ceil(X) +** ceiling(X) +** floor(X) +** +** The sqlite3_user_data() pointer is a pointer to the libm implementation +** of the underlying C function. +*/ +static void ceilingFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + assert( argc==1 ); + switch( sqlite3_value_numeric_type(argv[0]) ){ + case SQLITE_INTEGER: { + sqlite3_result_int64(context, sqlite3_value_int64(argv[0])); + break; + } + case SQLITE_FLOAT: { + double (*x)(double) = (double(*)(double))sqlite3_user_data(context); + sqlite3_result_double(context, x(sqlite3_value_double(argv[0]))); + break; + } + default: { + break; + } + } +} + +/* +** On some systems, ceil() and floor() are intrinsic function. You are +** unable to take a pointer to these functions. Hence, we here wrap them +** in our own actual functions. +*/ +static double xCeil(double x){ return ceil(x); } +static double xFloor(double x){ return floor(x); } + +/* +** Some systems do not have log2() and log10() in their standard math +** libraries. +*/ +#if defined(HAVE_LOG10) && HAVE_LOG10==0 +# define log10(X) (0.4342944819032517867*log(X)) +#endif +#if defined(HAVE_LOG2) && HAVE_LOG2==0 +# define log2(X) (1.442695040888963456*log(X)) +#endif + + +/* +** Implementation of SQL functions: +** +** ln(X) - natural logarithm +** log(X) - log X base 10 +** log10(X) - log X base 10 +** log(B,X) - log X base B +*/ +static void logFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + double x, b, ans; + assert( argc==1 || argc==2 ); + switch( sqlite3_value_numeric_type(argv[0]) ){ + case SQLITE_INTEGER: + case SQLITE_FLOAT: + x = sqlite3_value_double(argv[0]); + if( x<=0.0 ) return; + break; + default: + return; + } + if( argc==2 ){ + switch( sqlite3_value_numeric_type(argv[0]) ){ + case SQLITE_INTEGER: + case SQLITE_FLOAT: + b = log(x); + if( b<=0.0 ) return; + x = sqlite3_value_double(argv[1]); + if( x<=0.0 ) return; + break; + default: + return; + } + ans = log(x)/b; + }else{ + switch( SQLITE_PTR_TO_INT(sqlite3_user_data(context)) ){ + case 1: + ans = log10(x); + break; + case 2: + ans = log2(x); + break; + default: + ans = log(x); + break; + } + } + sqlite3_result_double(context, ans); +} + +/* +** Functions to converts degrees to radians and radians to degrees. +*/ +static double degToRad(double x){ return x*(M_PI/180.0); } +static double radToDeg(double x){ return x*(180.0/M_PI); } + +/* +** Implementation of 1-argument SQL math functions: +** +** exp(X) - Compute e to the X-th power +*/ +static void math1Func( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + int type0; + double v0, ans; + double (*x)(double); + assert( argc==1 ); + type0 = sqlite3_value_numeric_type(argv[0]); + if( type0!=SQLITE_INTEGER && type0!=SQLITE_FLOAT ) return; + v0 = sqlite3_value_double(argv[0]); + x = (double(*)(double))sqlite3_user_data(context); + ans = x(v0); + sqlite3_result_double(context, ans); +} + +/* +** Implementation of 2-argument SQL math functions: +** +** power(X,Y) - Compute X to the Y-th power +*/ +static void math2Func( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + int type0, type1; + double v0, v1, ans; + double (*x)(double,double); + assert( argc==2 ); + type0 = sqlite3_value_numeric_type(argv[0]); + if( type0!=SQLITE_INTEGER && type0!=SQLITE_FLOAT ) return; + type1 = sqlite3_value_numeric_type(argv[1]); + if( type1!=SQLITE_INTEGER && type1!=SQLITE_FLOAT ) return; + v0 = sqlite3_value_double(argv[0]); + v1 = sqlite3_value_double(argv[1]); + x = (double(*)(double,double))sqlite3_user_data(context); + ans = x(v0, v1); + sqlite3_result_double(context, ans); +} + +/* +** Implementation of 0-argument pi() function. +*/ +static void piFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + assert( argc==0 ); + (void)argv; + sqlite3_result_double(context, M_PI); +} + +#endif /* SQLITE_ENABLE_MATH_FUNCTIONS */ + +/* +** Implementation of sign(X) function. +*/ +static void signFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + int type0; + double x; + UNUSED_PARAMETER(argc); + assert( argc==1 ); + type0 = sqlite3_value_numeric_type(argv[0]); + if( type0!=SQLITE_INTEGER && type0!=SQLITE_FLOAT ) return; + x = sqlite3_value_double(argv[0]); + sqlite3_result_int(context, x<0.0 ? -1 : x>0.0 ? +1 : 0); +} + +#if defined(SQLITE_ENABLE_PERCENTILE) +/*********************************************************************** +** This section implements the percentile(Y,P) SQL function and similar. +** Requirements: +** +** (1) The percentile(Y,P) function is an aggregate function taking +** exactly two arguments. +** +** (2) If the P argument to percentile(Y,P) is not the same for every +** row in the aggregate then an error is thrown. The word "same" +** in the previous sentence means that the value differ by less +** than 0.001. +** +** (3) If the P argument to percentile(Y,P) evaluates to anything other +** than a number in the range of 0.0 to 100.0 inclusive then an +** error is thrown. +** +** (4) If any Y argument to percentile(Y,P) evaluates to a value that +** is not NULL and is not numeric then an error is thrown. +** +** (5) If any Y argument to percentile(Y,P) evaluates to plus or minus +** infinity then an error is thrown. (SQLite always interprets NaN +** values as NULL.) +** +** (6) Both Y and P in percentile(Y,P) can be arbitrary expressions, +** including CASE WHEN expressions. +** +** (7) The percentile(Y,P) aggregate is able to handle inputs of at least +** one million (1,000,000) rows. +** +** (8) If there are no non-NULL values for Y, then percentile(Y,P) +** returns NULL. +** +** (9) If there is exactly one non-NULL value for Y, the percentile(Y,P) +** returns the one Y value. +** +** (10) If there N non-NULL values of Y where N is two or more and +** the Y values are ordered from least to greatest and a graph is +** drawn from 0 to N-1 such that the height of the graph at J is +** the J-th Y value and such that straight lines are drawn between +** adjacent Y values, then the percentile(Y,P) function returns +** the height of the graph at P*(N-1)/100. +** +** (11) The percentile(Y,P) function always returns either a floating +** point number or NULL. +** +** (12) The percentile(Y,P) is implemented as a single C99 source-code +** file that compiles into a shared-library or DLL that can be loaded +** into SQLite using the sqlite3_load_extension() interface. +** +** (13) A separate median(Y) function is the equivalent percentile(Y,50). +** +** (14) A separate percentile_cont(Y,P) function is equivalent to +** percentile(Y,P/100.0). In other words, the fraction value in +** the second argument is in the range of 0 to 1 instead of 0 to 100. +** +** (15) A separate percentile_disc(Y,P) function is like +** percentile_cont(Y,P) except that instead of returning the weighted +** average of the nearest two input values, it returns the next lower +** value. So the percentile_disc(Y,P) will always return a value +** that was one of the inputs. +** +** (16) All of median(), percentile(Y,P), percentile_cont(Y,P) and +** percentile_disc(Y,P) can be used as window functions. +** +** Differences from standard SQL: +** +** * The percentile_cont(X,P) function is equivalent to the following in +** standard SQL: +** +** (percentile_cont(P) WITHIN GROUP (ORDER BY X)) +** +** The SQLite syntax is much more compact. The standard SQL syntax +** is also supported if SQLite is compiled with the +** -DSQLITE_ENABLE_ORDERED_SET_AGGREGATES option. +** +** * No median(X) function exists in the SQL standard. App developers +** are expected to write "percentile_cont(0.5)WITHIN GROUP(ORDER BY X)". +** +** * No percentile(Y,P) function exists in the SQL standard. Instead of +** percential(Y,P), developers must write this: +** "percentile_cont(P/100.0) WITHIN GROUP (ORDER BY Y)". Note that +** the fraction parameter to percentile() goes from 0 to 100 whereas +** the fraction parameter in SQL standard percentile_cont() goes from +** 0 to 1. +** +** Implementation notes as of 2024-08-31: +** +** * The regular aggregate-function versions of these routines work +** by accumulating all values in an array of doubles, then sorting +** that array using quicksort before computing the answer. Thus +** the runtime is O(NlogN) where N is the number of rows of input. +** +** * For the window-function versions of these routines, the array of +** inputs is sorted as soon as the first value is computed. Thereafter, +** the array is kept in sorted order using an insert-sort. This +** results in O(N*K) performance where K is the size of the window. +** One can imagine alternative implementations that give O(N*logN*logK) +** performance, but they require more complex logic and data structures. +** The developers have elected to keep the asymptotically slower +** algorithm for now, for simplicity, under the theory that window +** functions are seldom used and when they are, the window size K is +** often small. The developers might revisit that decision later, +** should the need arise. +*/ + +/* The following object is the group context for a single percentile() +** aggregate. Remember all input Y values until the very end. +** Those values are accumulated in the Percentile.a[] array. +*/ +typedef struct Percentile Percentile; +struct Percentile { + u64 nAlloc; /* Number of slots allocated for a[] */ + u64 nUsed; /* Number of slots actually used in a[] */ + char bSorted; /* True if a[] is already in sorted order */ + char bKeepSorted; /* True if advantageous to keep a[] sorted */ + char bPctValid; /* True if rPct is valid */ + double rPct; /* Fraction. 0.0 to 1.0 */ + double *a; /* Array of Y values */ +}; + +/* +** Return TRUE if the input floating-point number is an infinity. +*/ +static int percentIsInfinity(double r){ + sqlite3_uint64 u; + assert( sizeof(u)==sizeof(r) ); + memcpy(&u, &r, sizeof(u)); + return ((u>>52)&0x7ff)==0x7ff; +} + +/* +** Return TRUE if two doubles differ by 0.001 or less. +*/ +static int percentSameValue(double a, double b){ + a -= b; + return a>=-0.001 && a<=0.001; +} + +/* +** Search p (which must have p->bSorted) looking for an entry with +** value y. Return the index of that entry. +** +** If bExact is true, return -1 if the entry is not found. +** +** If bExact is false, return the index at which a new entry with +** value y should be insert in order to keep the values in sorted +** order. The smallest return value in this case will be 0, and +** the largest return value will be p->nUsed. +*/ +static i64 percentBinarySearch(Percentile *p, double y, int bExact){ + i64 iFirst = 0; /* First element of search range */ + i64 iLast = (i64)p->nUsed - 1; /* Last element of search range */ + while( iLast>=iFirst ){ + i64 iMid = (iFirst+iLast)/2; + double x = p->a[iMid]; + if( xy ){ + iLast = iMid - 1; + }else{ + return iMid; + } + } + if( bExact ) return -1; + return iFirst; +} + +/* +** Generate an error for a percentile function. +** +** The error format string must have exactly one occurrence of "%%s()" +** (with two '%' characters). That substring will be replaced by the name +** of the function. +*/ +static void percentError(sqlite3_context *pCtx, const char *zFormat, ...){ + char *zMsg1; + char *zMsg2; + va_list ap; + + va_start(ap, zFormat); + zMsg1 = sqlite3_vmprintf(zFormat, ap); + va_end(ap); + zMsg2 = zMsg1 ? sqlite3_mprintf(zMsg1, sqlite3VdbeFuncName(pCtx)) : 0; + sqlite3_result_error(pCtx, zMsg2, -1); + sqlite3_free(zMsg1); + sqlite3_free(zMsg2); +} + +/* +** The "step" function for percentile(Y,P) is called once for each +** input row. +*/ +static void percentStep(sqlite3_context *pCtx, int argc, sqlite3_value **argv){ + Percentile *p; + double rPct; + int eType; + double y; + assert( argc==2 || argc==1 ); + + if( argc==1 ){ + /* Requirement 13: median(Y) is the same as percentile(Y,50). */ + rPct = 0.5; + }else{ + /* P must be a number between 0 and 100 for percentile() or between + ** 0.0 and 1.0 for percentile_cont() and percentile_disc(). + ** + ** The user-data is an integer which is 10 times the upper bound. + */ + double mxFrac = (SQLITE_PTR_TO_INT(sqlite3_user_data(pCtx))&2)? 100.0 : 1.0; + eType = sqlite3_value_numeric_type(argv[1]); + rPct = sqlite3_value_double(argv[1])/mxFrac; + if( (eType!=SQLITE_INTEGER && eType!=SQLITE_FLOAT) + || rPct<0.0 || rPct>1.0 + ){ + percentError(pCtx, "the fraction argument to %%s()" + " is not between 0.0 and %.1f", + (double)mxFrac); + return; + } + } + + /* Allocate the session context. */ + p = (Percentile*)sqlite3_aggregate_context(pCtx, sizeof(*p)); + if( p==0 ) return; + + /* Remember the P value. Throw an error if the P value is different + ** from any prior row, per Requirement (2). */ + if( !p->bPctValid ){ + p->rPct = rPct; + p->bPctValid = 1; + }else if( !percentSameValue(p->rPct,rPct) ){ + percentError(pCtx, "the fraction argument to %%s()" + " is not the same for all input rows"); + return; + } + + /* Ignore rows for which Y is NULL */ + eType = sqlite3_value_type(argv[0]); + if( eType==SQLITE_NULL ) return; + + /* If not NULL, then Y must be numeric. Otherwise throw an error. + ** Requirement 4 */ + if( eType!=SQLITE_INTEGER && eType!=SQLITE_FLOAT ){ + percentError(pCtx, "input to %%s() is not numeric"); + return; + } + + /* Throw an error if the Y value is infinity or NaN */ + y = sqlite3_value_double(argv[0]); + if( percentIsInfinity(y) ){ + percentError(pCtx, "Inf input to %%s()"); + return; + } + + /* Allocate and store the Y */ + if( p->nUsed>=p->nAlloc ){ + u64 n = p->nAlloc*2 + 250; + double *a = sqlite3_realloc64(p->a, sizeof(double)*n); + if( a==0 ){ + sqlite3_free(p->a); + memset(p, 0, sizeof(*p)); + sqlite3_result_error_nomem(pCtx); + return; + } + p->nAlloc = n; + p->a = a; + } + if( p->nUsed==0 ){ + p->a[p->nUsed++] = y; + p->bSorted = 1; + }else if( !p->bSorted || y>=p->a[p->nUsed-1] ){ + p->a[p->nUsed++] = y; + }else if( p->bKeepSorted ){ + i64 i; + i = percentBinarySearch(p, y, 0); + if( i<(int)p->nUsed ){ + memmove(&p->a[i+1], &p->a[i], (p->nUsed-i)*sizeof(p->a[0])); + } + p->a[i] = y; + p->nUsed++; + }else{ + p->a[p->nUsed++] = y; + p->bSorted = 0; + } +} + +/* +** Interchange two doubles. +*/ +#define SWAP_DOUBLE(X,Y) {double ttt=(X);(X)=(Y);(Y)=ttt;} + +/* +** Sort an array of doubles. +** +** Algorithm: quicksort +** +** This is implemented separately rather than using the qsort() routine +** from the standard library because: +** +** (1) To avoid a dependency on qsort() +** (2) To avoid the function call to the comparison routine for each +** comparison. +*/ +static void percentSort(double *a, unsigned int n){ + int iLt; /* Entries before a[iLt] are less than rPivot */ + int iGt; /* Entries at or after a[iGt] are greater than rPivot */ + int i; /* Loop counter */ + double rPivot; /* The pivot value */ + + assert( n>=2 ); + if( a[0]>a[n-1] ){ + SWAP_DOUBLE(a[0],a[n-1]) + } + if( n==2 ) return; + iGt = n-1; + i = n/2; + if( a[0]>a[i] ){ + SWAP_DOUBLE(a[0],a[i]) + }else if( a[i]>a[iGt] ){ + SWAP_DOUBLE(a[i],a[iGt]) + } + if( n==3 ) return; + rPivot = a[i]; + iLt = i = 1; + do{ + if( a[i]iLt ) SWAP_DOUBLE(a[i],a[iLt]) + iLt++; + i++; + }else if( a[i]>rPivot ){ + do{ + iGt--; + }while( iGt>i && a[iGt]>rPivot ); + SWAP_DOUBLE(a[i],a[iGt]) + }else{ + i++; + } + }while( i=2 ) percentSort(a, iLt); + if( n-iGt>=2 ) percentSort(a+iGt, n-iGt); + +/* Uncomment for testing */ +#if 0 + for(i=0; ibSorted==0 ){ + assert( p->nUsed>1 ); + percentSort(p->a, p->nUsed); + p->bSorted = 1; + } + p->bKeepSorted = 1; + + /* Find and remove the row */ + i = percentBinarySearch(p, y, 1); + if( i>=0 ){ + p->nUsed--; + if( i<(int)p->nUsed ){ + memmove(&p->a[i], &p->a[i+1], (p->nUsed - i)*sizeof(p->a[0])); + } + } +} + +/* +** Compute the final output of percentile(). Clean up all allocated +** memory if and only if bIsFinal is true. +*/ +static void percentCompute(sqlite3_context *pCtx, int bIsFinal){ + Percentile *p; + int settings = SQLITE_PTR_TO_INT(sqlite3_user_data(pCtx))&1; /* Discrete? */ + unsigned i1, i2; + double v1, v2; + double ix, vx; + p = (Percentile*)sqlite3_aggregate_context(pCtx, 0); + if( p==0 ) return; + if( p->a==0 ) return; + if( p->nUsed ){ + if( p->bSorted==0 ){ + assert( p->nUsed>1 ); + percentSort(p->a, p->nUsed); + p->bSorted = 1; + } + ix = p->rPct*(p->nUsed-1); + i1 = (unsigned)ix; + if( settings & 1 ){ + vx = p->a[i1]; + }else{ + i2 = ix==(double)i1 || i1==p->nUsed-1 ? i1 : i1+1; + v1 = p->a[i1]; + v2 = p->a[i2]; + vx = v1 + (v2-v1)*(ix-i1); + } + sqlite3_result_double(pCtx, vx); + } + if( bIsFinal ){ + sqlite3_free(p->a); + memset(p, 0, sizeof(*p)); + }else{ + p->bKeepSorted = 1; + } +} +static void percentFinal(sqlite3_context *pCtx){ + percentCompute(pCtx, 1); +} +static void percentValue(sqlite3_context *pCtx){ + percentCompute(pCtx, 0); +} +/****** End of percentile family of functions ******/ +#endif /* SQLITE_ENABLE_PERCENTILE */ + +#if defined(SQLITE_DEBUG) || defined(SQLITE_ENABLE_FILESTAT) +/* +** Implementation of sqlite_filestat(SCHEMA). +** +** Return JSON text that describes low-level debug/diagnostic information +** about the sqlite3_file object associated with SCHEMA. +*/ +static void filestatFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + sqlite3 *db = sqlite3_context_db_handle(context); + const char *zDbName; + sqlite3_str *pStr; + Btree *pBtree; + + zDbName = (const char*)sqlite3_value_text(argv[0]); + pBtree = sqlite3DbNameToBtree(db, zDbName); + if( pBtree ){ + Pager *pPager; + sqlite3_file *fd; + int rc; + sqlite3BtreeEnter(pBtree); + pPager = sqlite3BtreePager(pBtree); + assert( pPager!=0 ); + fd = sqlite3PagerFile(pPager); + pStr = sqlite3_str_new(db); + if( pStr==0 ){ + sqlite3_result_error_nomem(context); + }else{ + sqlite3_str_append(pStr, "{\"db\":", 6); + rc = sqlite3OsFileControl(fd, SQLITE_FCNTL_FILESTAT, pStr); + if( rc ) sqlite3_str_append(pStr, "null", 4); + fd = sqlite3PagerJrnlFile(pPager); + if( fd && fd->pMethods!=0 ){ + sqlite3_str_appendall(pStr, ",\"journal\":"); + rc = sqlite3OsFileControl(fd, SQLITE_FCNTL_FILESTAT, pStr); + if( rc ) sqlite3_str_append(pStr, "null", 4); + } + sqlite3_str_append(pStr, "}", 1); + sqlite3_result_text(context, sqlite3_str_finish(pStr), -1, + sqlite3_free); + } + sqlite3BtreeLeave(pBtree); + }else{ + sqlite3_result_text(context, "{}", 2, SQLITE_STATIC); + } +} +#endif /* SQLITE_DEBUG || SQLITE_ENABLE_FILESTAT */ + +#ifdef SQLITE_DEBUG +/* +** Implementation of fpdecode(x,y,z) function. +** +** x is a real number that is to be decoded. y is the precision. +** z is the maximum real precision. Return a string that shows the +** results of the sqlite3FpDecode() function. +** +** Used for testing and debugging only, specifically testing and debugging +** of the sqlite3FpDecode() function. This SQL function does not appear +** in production builds. This function is not an API and is subject to +** modification or removal in future versions of SQLite. +*/ +static void fpdecodeFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + FpDecode s; + double x; + int y, z; + char zBuf[100]; + UNUSED_PARAMETER(argc); + assert( argc==3 ); + x = sqlite3_value_double(argv[0]); + y = sqlite3_value_int(argv[1]); + z = sqlite3_value_int(argv[2]); + if( z<=0 ) z = 1; + sqlite3FpDecode(&s, x, y, z); + if( s.isSpecial==2 ){ + sqlite3_snprintf(sizeof(zBuf), zBuf, "NaN"); + }else{ + sqlite3_snprintf(sizeof(zBuf), zBuf, "%c%.*s/%d", s.sign, s.n, s.z, s.iDP); + } + sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); +} +#endif /* SQLITE_DEBUG */ + +#ifdef SQLITE_DEBUG +/* +** Implementation of parseuri(uri,flags) function. +** +** Required Arguments: +** "uri" The URI to parse. +** "flags" Bitmask of flags, as if to sqlite3_open_v2(). +** +** Additional arguments beyond the first two make calls to +** sqlite3_uri_key() for integers and sqlite3_uri_parameter for +** anything else. +** +** The result is a string showing the results of calling sqlite3ParseUri(). +** +** Used for testing and debugging only, specifically testing and debugging +** of the sqlite3ParseUri() function. This SQL function does not appear +** in production builds. This function is not an API and is subject to +** modification or removal in future versions of SQLite. +*/ +static void parseuriFunc( + sqlite3_context *ctx, + int argc, + sqlite3_value **argv +){ + sqlite3_str *pResult; + const char *zVfs; + const char *zUri; + unsigned int flgs; + int rc; + sqlite3_vfs *pVfs = 0; + char *zFile = 0; + char *zErr = 0; + + if( argc<2 ) return; + pVfs = sqlite3_vfs_find(0); + assert( pVfs ); + zVfs = pVfs->zName; + zUri = (const char*)sqlite3_value_text(argv[0]); + if( zUri==0 ) return; + flgs = (unsigned int)sqlite3_value_int(argv[1]); + rc = sqlite3ParseUri(zVfs, zUri, &flgs, &pVfs, &zFile, &zErr); + pResult = sqlite3_str_new(0); + if( pResult ){ + int i; + sqlite3_str_appendf(pResult, "rc=%d", rc); + sqlite3_str_appendf(pResult, ", flags=0x%x", flgs); + sqlite3_str_appendf(pResult, ", vfs=%Q", pVfs ? pVfs->zName: 0); + sqlite3_str_appendf(pResult, ", err=%Q", zErr); + sqlite3_str_appendf(pResult, ", file=%Q", zFile); + if( zFile ){ + const char *z = zFile; + z += sqlite3Strlen30(z)+1; + while( z[0] ){ + sqlite3_str_appendf(pResult, ", %Q", z); + z += sqlite3Strlen30(z)+1; + } + for(i=2; iu.pHash){ + int n = sqlite3Strlen30(p->zName); + int h = p->zName[0] + n; + assert( p->funcFlags & SQLITE_FUNC_BUILTIN ); + printf(" %s(%d)", p->zName, h); + } + printf("\n"); + } + } #endif } diff --git a/src/global.c b/src/global.c index 64966b35d7..65ed82d31d 100644 --- a/src/global.c +++ b/src/global.c @@ -37,7 +37,7 @@ const unsigned char sqlite3UpperToLower[] = { 198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215, 216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233, 234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251, - 252,253,254,255 + 252,253,254,255, #endif #ifdef SQLITE_EBCDIC 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, /* 0x */ @@ -57,7 +57,35 @@ const unsigned char sqlite3UpperToLower[] = { 224,225,162,163,164,165,166,167,168,169,234,235,236,237,238,239, /* Ex */ 240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255, /* Fx */ #endif +/* All of the upper-to-lower conversion data is above. The following +** 18 integers are completely unrelated. They are appended to the +** sqlite3UpperToLower[] array to avoid UBSAN warnings. Here's what is +** going on: +** +** The SQL comparison operators (<>, =, >, <=, <, and >=) are implemented +** by invoking sqlite3MemCompare(A,B) which compares values A and B and +** returns negative, zero, or positive if A is less then, equal to, or +** greater than B, respectively. Then the true false results is found by +** consulting sqlite3aLTb[opcode], sqlite3aEQb[opcode], or +** sqlite3aGTb[opcode] depending on whether the result of compare(A,B) +** is negative, zero, or positive, where opcode is the specific opcode. +** The only works because the comparison opcodes are consecutive and in +** this order: NE EQ GT LE LT GE. Various assert()s throughout the code +** ensure that is the case. +** +** These elements must be appended to another array. Otherwise the +** index (here shown as [256-OP_Ne]) would be out-of-bounds and thus +** be undefined behavior. That's goofy, but the C-standards people thought +** it was a good idea, so here we are. +*/ +/* NE EQ GT LE LT GE */ + 1, 0, 0, 1, 1, 0, /* aLTb[]: Use when compare(A,B) less than zero */ + 0, 1, 0, 1, 0, 1, /* aEQb[]: Use when compare(A,B) equals zero */ + 1, 0, 1, 0, 0, 1 /* aGTb[]: Use when compare(A,B) greater than zero*/ }; +const unsigned char *sqlite3aLTb = &sqlite3UpperToLower[256-OP_Ne]; +const unsigned char *sqlite3aEQb = &sqlite3UpperToLower[256+6-OP_Ne]; +const unsigned char *sqlite3aGTb = &sqlite3UpperToLower[256+12-OP_Ne]; /* ** The following 256 byte lookup table is used to support SQLites built-in @@ -69,7 +97,8 @@ const unsigned char sqlite3UpperToLower[] = { ** isalnum() 0x06 ** isxdigit() 0x08 ** toupper() 0x20 -** SQLite identifier character 0x40 +** SQLite identifier character 0x40 $, _, or non-ascii +** Quote character 0x80 ** ** Bit 0x20 is set if the mapped character requires translation to upper ** case. i.e. if the character is a lower-case ASCII character. @@ -78,24 +107,20 @@ const unsigned char sqlite3UpperToLower[] = { ** ** (x & ~(map[x]&0x20)) ** -** Standard function tolower() is implemented using the sqlite3UpperToLower[] +** The equivalent of tolower() is implemented using the sqlite3UpperToLower[] ** array. tolower() is used more often than toupper() by SQLite. ** -** Bit 0x40 is set if the character non-alphanumeric and can be used in an +** Bit 0x40 is set if the character is non-alphanumeric and can be used in an ** SQLite identifier. Identifiers are alphanumerics, "_", "$", and any ** non-ASCII UTF character. Hence the test for whether or not a character is ** part of an identifier is 0x46. -** -** SQLite's versions are identical to the standard versions assuming a -** locale of "C". They are implemented as macros in sqliteInt.h. */ -#ifdef SQLITE_ASCII const unsigned char sqlite3CtypeMap[256] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00..07 ........ */ 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, /* 08..0f ........ */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 10..17 ........ */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 18..1f ........ */ - 0x01, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, /* 20..27 !"#$%&' */ + 0x01, 0x00, 0x80, 0x00, 0x40, 0x00, 0x00, 0x80, /* 20..27 !"#$%&' */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 28..2f ()*+,-./ */ 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, /* 30..37 01234567 */ 0x0c, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 38..3f 89:;<=>? */ @@ -103,8 +128,8 @@ const unsigned char sqlite3CtypeMap[256] = { 0x00, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x02, /* 40..47 @ABCDEFG */ 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, /* 48..4f HIJKLMNO */ 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, /* 50..57 PQRSTUVW */ - 0x02, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x40, /* 58..5f XYZ[\]^_ */ - 0x00, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x22, /* 60..67 `abcdefg */ + 0x02, 0x02, 0x02, 0x80, 0x00, 0x00, 0x00, 0x40, /* 58..5f XYZ[\]^_ */ + 0x80, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x22, /* 60..67 `abcdefg */ 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, /* 68..6f hijklmno */ 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, /* 70..77 pqrstuvw */ 0x22, 0x22, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, /* 78..7f xyz{|}~. */ @@ -127,7 +152,6 @@ const unsigned char sqlite3CtypeMap[256] = { 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* f0..f7 ........ */ 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40 /* f8..ff ........ */ }; -#endif /* EVIDENCE-OF: R-02982-34736 In order to maintain full backwards ** compatibility for legacy applications, the URI filename capability is @@ -139,17 +163,33 @@ const unsigned char sqlite3CtypeMap[256] = { ** EVIDENCE-OF: R-43642-56306 By default, URI handling is globally ** disabled. The default value may be changed by compiling with the ** SQLITE_USE_URI symbol defined. +** +** URI filenames are enabled by default if SQLITE_HAS_CODEC is +** enabled. */ #ifndef SQLITE_USE_URI -# define SQLITE_USE_URI 0 +/* BEGIN SQLCIPHER */ +# ifdef SQLITE_HAS_CODEC +# define SQLITE_USE_URI 1 +# else +# define SQLITE_USE_URI 0 +# endif +/* END SQLCIPHER */ #endif /* EVIDENCE-OF: R-38720-18127 The default setting is determined by the ** SQLITE_ALLOW_COVERING_INDEX_SCAN compile-time option, or is "on" if ** that compile-time option is omitted. */ -#ifndef SQLITE_ALLOW_COVERING_INDEX_SCAN +#if !defined(SQLITE_ALLOW_COVERING_INDEX_SCAN) # define SQLITE_ALLOW_COVERING_INDEX_SCAN 1 +#else +# if !SQLITE_ALLOW_COVERING_INDEX_SCAN +# error "Compile-time disabling of covering index scan using the\ + -DSQLITE_ALLOW_COVERING_INDEX_SCAN=0 option is deprecated.\ + Contact SQLite developers if this is a problem for you, and\ + delete this #error macro to continue with your build." +# endif #endif /* The minimum PMA size is set to this value multiplied by the database @@ -159,6 +199,47 @@ const unsigned char sqlite3CtypeMap[256] = { # define SQLITE_SORTER_PMASZ 250 #endif +/* Statement journals spill to disk when their size exceeds the following +** threshold (in bytes). 0 means that statement journals are created and +** written to disk immediately (the default behavior for SQLite versions +** before 3.12.0). -1 means always keep the entire statement journal in +** memory. (The statement journal is also always held entirely in memory +** if journal_mode=MEMORY or if temp_store=MEMORY, regardless of this +** setting.) +*/ +#ifndef SQLITE_STMTJRNL_SPILL +# define SQLITE_STMTJRNL_SPILL (64*1024) +#endif + +/* +** The default lookaside-configuration, the format "SZ,N". SZ is the +** number of bytes in each lookaside slot (should be a multiple of 8) +** and N is the number of slots. The lookaside-configuration can be +** changed as start-time using sqlite3_config(SQLITE_CONFIG_LOOKASIDE) +** or at run-time for an individual database connection using +** sqlite3_db_config(db, SQLITE_DBCONFIG_LOOKASIDE); +** +** With the two-size-lookaside enhancement, less lookaside is required. +** The default configuration of 1200,40 actually provides 30 1200-byte slots +** and 93 128-byte slots, which is more lookaside than is available +** using the older 1200,100 configuration without two-size-lookaside. +*/ +#ifndef SQLITE_DEFAULT_LOOKASIDE +# ifdef SQLITE_OMIT_TWOSIZE_LOOKASIDE +# define SQLITE_DEFAULT_LOOKASIDE 1200,100 /* 120KB of memory */ +# else +# define SQLITE_DEFAULT_LOOKASIDE 1200,40 /* 48KB of memory */ +# endif +#endif + + +/* The default maximum size of an in-memory database created using +** sqlite3_deserialize() +*/ +#ifndef SQLITE_MEMDB_DEFAULT_MAXSIZE +# define SQLITE_MEMDB_DEFAULT_MAXSIZE 1073741824 +#endif + /* ** The following singleton contains the global configuration for ** the SQLite library. @@ -169,10 +250,15 @@ SQLITE_WSD struct Sqlite3Config sqlite3Config = { SQLITE_THREADSAFE==1, /* bFullMutex */ SQLITE_USE_URI, /* bOpenUri */ SQLITE_ALLOW_COVERING_INDEX_SCAN, /* bUseCis */ + 0, /* bSmallMalloc */ + 1, /* bExtraSchemaChecks */ +#ifdef SQLITE_DEBUG + 0, /* bJsonSelfcheck */ +#endif 0x7ffffffe, /* mxStrlen */ 0, /* neverCorrupt */ - 128, /* szLookaside */ - 500, /* nLookaside */ + SQLITE_DEFAULT_LOOKASIDE, /* szLookaside, nLookaside */ + SQLITE_STMTJRNL_SPILL, /* nStmtSpill */ {0,0,0,0,0,0,0,0}, /* m */ {0,0,0,0,0,0,0,0,0}, /* mutex */ {0,0,0,0,0,0,0,0,0,0,0,0,0},/* pcache2 */ @@ -181,9 +267,6 @@ SQLITE_WSD struct Sqlite3Config sqlite3Config = { 0, 0, /* mnHeap, mxHeap */ SQLITE_DEFAULT_MMAP_SIZE, /* szMmap */ SQLITE_MAX_MMAP_SIZE, /* mxMmap */ - (void*)0, /* pScratch */ - 0, /* szScratch */ - 0, /* nScratch */ (void*)0, /* pPage */ 0, /* szPage */ SQLITE_DEFAULT_PCACHE_INITSZ, /* nPage */ @@ -208,10 +291,23 @@ SQLITE_WSD struct Sqlite3Config sqlite3Config = { 0, /* xVdbeBranch */ 0, /* pVbeBranchArg */ #endif -#ifndef SQLITE_OMIT_BUILTIN_TEST +#ifndef SQLITE_OMIT_DESERIALIZE + SQLITE_MEMDB_DEFAULT_MAXSIZE, /* mxMemdbSize */ +#endif +#ifndef SQLITE_UNTESTABLE 0, /* xTestCallback */ #endif - 0 /* bLocaltimeFault */ +#ifdef SQLITE_ALLOW_ROWID_IN_VIEW + 0, /* mNoVisibleRowid. 0 == allow rowid-in-view */ +#endif + 0, /* bLocaltimeFault */ + 0, /* xAltLocaltime */ + 0x7ffffffe, /* iOnceResetThreshold */ + SQLITE_DEFAULT_SORTERREF_SIZE, /* szSorterRef */ + 0, /* iPrngSeed */ +#ifdef SQLITE_DEBUG + {0,0,0,0,0,0}, /* aTune */ +#endif }; /* @@ -219,22 +315,33 @@ SQLITE_WSD struct Sqlite3Config sqlite3Config = { ** database connections. After initialization, this table is ** read-only. */ -SQLITE_WSD FuncDefHash sqlite3GlobalFunctions; +FuncDefHash sqlite3BuiltinFunctions; +#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_DEBUG) /* -** Constant tokens for values 0 and 1. +** Counter used for coverage testing. Does not come into play for +** release builds. +** +** Access to this global variable is not mutex protected. This might +** result in TSAN warnings. But as the variable does not exist in +** release builds, that should not be a concern. */ -const Token sqlite3IntTokens[] = { - { "0", 1 }, - { "1", 1 } -}; +unsigned int sqlite3CoverageCounter; +#endif /* SQLITE_COVERAGE_TEST || SQLITE_DEBUG */ +#ifdef VDBE_PROFILE +/* +** The following performance counter can be used in place of +** sqlite3Hwtime() for profiling. This is a no-op on standard builds. +*/ +sqlite3_uint64 sqlite3NProfileCnt = 0; +#endif /* ** The value of the "pending" byte must be 0x40000000 (1 byte past the ** 1-gibabyte boundary) in a compatible database. SQLite never uses ** the database page that contains the pending byte. It never attempts -** to read or write that page. The pending byte page is set assign +** to read or write that page. The pending byte page is set aside ** for use by the VFS layers as space for managing file locks. ** ** During testing, it is often desirable to move the pending byte to @@ -252,6 +359,12 @@ const Token sqlite3IntTokens[] = { int sqlite3PendingByte = 0x40000000; #endif +/* +** Tracing flags set by SQLITE_TESTCTRL_TRACEFLAGS. +*/ +u32 sqlite3TreeTrace = 0; +u32 sqlite3WhereTrace = 0; + #include "opcodes.h" /* ** Properties of opcodes. The OPFLG_INITIALIZER macro is @@ -265,3 +378,33 @@ const unsigned char sqlite3OpcodeProperty[] = OPFLG_INITIALIZER; ** Name of the default collating sequence */ const char sqlite3StrBINARY[] = "BINARY"; + +/* +** Standard typenames. These names must match the COLTYPE_* definitions. +** Adjust the SQLITE_N_STDTYPE value if adding or removing entries. +** +** sqlite3StdType[] The actual names of the datatypes. +** +** sqlite3StdTypeLen[] The length (in bytes) of each entry +** in sqlite3StdType[]. +** +** sqlite3StdTypeAffinity[] The affinity associated with each entry +** in sqlite3StdType[]. +*/ +const unsigned char sqlite3StdTypeLen[] = { 3, 4, 3, 7, 4, 4 }; +const char sqlite3StdTypeAffinity[] = { + SQLITE_AFF_NUMERIC, + SQLITE_AFF_BLOB, + SQLITE_AFF_INTEGER, + SQLITE_AFF_INTEGER, + SQLITE_AFF_REAL, + SQLITE_AFF_TEXT +}; +const char *sqlite3StdType[] = { + "ANY", + "BLOB", + "INT", + "INTEGER", + "REAL", + "TEXT" +}; diff --git a/src/hash.c b/src/hash.c index b5886e0641..35df65808e 100644 --- a/src/hash.c +++ b/src/hash.c @@ -54,9 +54,20 @@ void sqlite3HashClear(Hash *pH){ */ static unsigned int strHash(const char *z){ unsigned int h = 0; - unsigned char c; - while( (c = (unsigned char)*z++)!=0 ){ - h = (h<<3) ^ h ^ sqlite3UpperToLower[c]; + while( z[0] ){ /*OPTIMIZATION-IF-TRUE*/ + /* Knuth multiplicative hashing. (Sorting & Searching, p. 510). + ** 0x9e3779b1 is 2654435761 which is the closest prime number to + ** (2**32)*golden_ratio, where golden_ratio = (sqrt(5) - 1)/2. + ** + ** Only bits 0xdf for ASCII and bits 0xbf for EBCDIC each octet are + ** hashed since the omitted bits determine the upper/lower case difference. + */ +#ifdef SQLITE_EBCDIC + h += 0xbf & (unsigned char)*(z++); +#else + h += 0xdf & (unsigned char)*(z++); +#endif + h *= 0x9e3779b1; } return h; } @@ -93,7 +104,7 @@ static void insertElement( } -/* Resize the hash table so that it cantains "new_size" buckets. +/* Resize the hash table so that it contains "new_size" buckets. ** ** The hash table might fail to resize if sqlite3_malloc() fails or ** if the new size is the same as the prior size. @@ -128,16 +139,16 @@ static int rehash(Hash *pH, unsigned int new_size){ pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht); memset(new_ht, 0, new_size*sizeof(struct _ht)); for(elem=pH->first, pH->first=0; elem; elem = next_elem){ - unsigned int h = strHash(elem->pKey) % new_size; next_elem = elem->next; - insertElement(pH, &new_ht[h], elem); + insertElement(pH, &new_ht[elem->h % new_size], elem); } return 1; } /* This function (for internal use only) locates an element in an -** hash table that matches the given key. The hash for this key is -** also computed and returned in the *pH parameter. +** hash table that matches the given key. If no element is found, +** a pointer to a static null element with HashElem.data==0 is returned. +** If pH is not NULL, then the hash for this key is written to *pH. */ static HashElem *findElementWithHash( const Hash *pH, /* The pH to be searched */ @@ -145,38 +156,38 @@ static HashElem *findElementWithHash( unsigned int *pHash /* Write the hash value here */ ){ HashElem *elem; /* Used to loop thru the element list */ - int count; /* Number of elements left to test */ + unsigned int count; /* Number of elements left to test */ unsigned int h; /* The computed hash */ + static HashElem nullElement = { 0, 0, 0, 0, 0 }; - if( pH->ht ){ + h = strHash(pKey); + if( pH->ht ){ /*OPTIMIZATION-IF-TRUE*/ struct _ht *pEntry; - h = strHash(pKey) % pH->htsize; - pEntry = &pH->ht[h]; + pEntry = &pH->ht[h % pH->htsize]; elem = pEntry->chain; count = pEntry->count; }else{ - h = 0; elem = pH->first; count = pH->count; } - *pHash = h; - while( count-- ){ + if( pHash ) *pHash = h; + while( count ){ assert( elem!=0 ); - if( sqlite3StrICmp(elem->pKey,pKey)==0 ){ + if( h==elem->h && sqlite3StrICmp(elem->pKey,pKey)==0 ){ return elem; } elem = elem->next; + count--; } - return 0; + return &nullElement; } /* Remove a single entry from the hash table given a pointer to that ** element and a hash on the element's key. */ -static void removeElementGivenHash( +static void removeElement( Hash *pH, /* The pH containing "elem" */ - HashElem* elem, /* The element to be removed from the pH */ - unsigned int h /* Hash value for the element */ + HashElem *elem /* The element to be removed from the pH */ ){ struct _ht *pEntry; if( elem->prev ){ @@ -188,12 +199,12 @@ static void removeElementGivenHash( elem->next->prev = elem->prev; } if( pH->ht ){ - pEntry = &pH->ht[h]; + pEntry = &pH->ht[elem->h % pH->htsize]; if( pEntry->chain==elem ){ pEntry->chain = elem->next; } + assert( pEntry->count>0 ); pEntry->count--; - assert( pEntry->count>=0 ); } sqlite3_free( elem ); pH->count--; @@ -209,13 +220,9 @@ static void removeElementGivenHash( ** found, or NULL if there is no match. */ void *sqlite3HashFind(const Hash *pH, const char *pKey){ - HashElem *elem; /* The element that matches key */ - unsigned int h; /* A hash on key */ - assert( pH!=0 ); assert( pKey!=0 ); - elem = findElementWithHash(pH, pKey, &h); - return elem ? elem->data : 0; + return findElementWithHash(pH, pKey, 0)->data; } /* Insert an element into the hash table pH. The key is pKey @@ -240,10 +247,10 @@ void *sqlite3HashInsert(Hash *pH, const char *pKey, void *data){ assert( pH!=0 ); assert( pKey!=0 ); elem = findElementWithHash(pH,pKey,&h); - if( elem ){ + if( elem->data ){ void *old_data = elem->data; if( data==0 ){ - removeElementGivenHash(pH,elem,h); + removeElement(pH,elem); }else{ elem->data = data; elem->pKey = pKey; @@ -254,14 +261,13 @@ void *sqlite3HashInsert(Hash *pH, const char *pKey, void *data){ new_elem = (HashElem*)sqlite3Malloc( sizeof(HashElem) ); if( new_elem==0 ) return data; new_elem->pKey = pKey; + new_elem->h = h; new_elem->data = data; pH->count++; - if( pH->count>=10 && pH->count > 2*pH->htsize ){ - if( rehash(pH, pH->count*2) ){ - assert( pH->htsize>0 ); - h = strHash(pKey) % pH->htsize; - } + if( pH->count>=5 && pH->count > 2*pH->htsize ){ + rehash(pH, pH->count*3); } - insertElement(pH, pH->ht ? &pH->ht[h] : 0, new_elem); + insertElement(pH, pH->ht ? &pH->ht[new_elem->h % pH->htsize] : 0, new_elem); return 0; } + diff --git a/src/hash.h b/src/hash.h index 6dfa4e035c..cff65d6e50 100644 --- a/src/hash.h +++ b/src/hash.h @@ -12,8 +12,8 @@ ** This is the header file for the generic hash-table implementation ** used in SQLite. */ -#ifndef _SQLITE_HASH_H_ -#define _SQLITE_HASH_H_ +#ifndef SQLITE_HASH_H +#define SQLITE_HASH_H /* Forward declarations of structures. */ typedef struct Hash Hash; @@ -45,7 +45,7 @@ struct Hash { unsigned int count; /* Number of entries in this table */ HashElem *first; /* The first element of the array */ struct _ht { /* the hash table */ - int count; /* Number of entries with this hash */ + unsigned int count; /* Number of entries with this hash */ HashElem *chain; /* Pointer to first entry with this hash */ } *ht; }; @@ -60,6 +60,7 @@ struct HashElem { HashElem *next, *prev; /* Next and previous elements in the table */ void *data; /* Data associated with this element */ const char *pKey; /* Key associated with this element */ + unsigned int h; /* hash for pKey */ }; /* @@ -91,6 +92,6 @@ void sqlite3HashClear(Hash*); /* ** Number of entries in a hash table */ -/* #define sqliteHashCount(H) ((H)->count) // NOT USED */ +#define sqliteHashCount(H) ((H)->count) -#endif /* _SQLITE_HASH_H_ */ +#endif /* SQLITE_HASH_H */ diff --git a/src/hwtime.h b/src/hwtime.h index b8bc5a295b..f808fa40eb 100644 --- a/src/hwtime.h +++ b/src/hwtime.h @@ -11,19 +11,20 @@ ****************************************************************************** ** ** This file contains inline asm code for retrieving "high-performance" -** counters for x86 class CPUs. +** counters for x86 and x86_64 class CPUs. */ -#ifndef _HWTIME_H_ -#define _HWTIME_H_ +#ifndef SQLITE_HWTIME_H +#define SQLITE_HWTIME_H /* -** The following routine only works on pentium-class (or newer) processors. +** The following routine only works on Pentium-class (or newer) processors. ** It uses the RDTSC opcode to read the cycle count value out of the ** processor and returns that value. This can be used for high-res ** profiling. */ -#if (defined(__GNUC__) || defined(_MSC_VER)) && \ - (defined(i386) || defined(__i386__) || defined(_M_IX86)) +#if !defined(__STRICT_ANSI__) && \ + (defined(__GNUC__) || defined(_MSC_VER)) && \ + (defined(i386) || defined(__i386__) || defined(_M_IX86)) #if defined(__GNUC__) @@ -44,15 +45,15 @@ #endif -#elif (defined(__GNUC__) && defined(__x86_64__)) +#elif !defined(__STRICT_ANSI__) && (defined(__GNUC__) && defined(__x86_64__)) __inline__ sqlite_uint64 sqlite3Hwtime(void){ - unsigned long val; - __asm__ __volatile__ ("rdtsc" : "=A" (val)); - return val; + unsigned int lo, hi; + __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi)); + return (sqlite_uint64)hi << 32 | lo; } -#elif (defined(__GNUC__) && defined(__ppc__)) +#elif !defined(__STRICT_ANSI__) && (defined(__GNUC__) && defined(__ppc__)) __inline__ sqlite_uint64 sqlite3Hwtime(void){ unsigned long long retval; @@ -69,17 +70,16 @@ #else - #error Need implementation of sqlite3Hwtime() for your platform. - /* - ** To compile without implementing sqlite3Hwtime() for your platform, - ** you can remove the above #error and use the following - ** stub function. You will lose timing support for many - ** of the debugging and testing utilities, but it should at - ** least compile and run. + ** asm() is needed for hardware timing support. Without asm(), + ** disable the sqlite3Hwtime() routine. + ** + ** sqlite3Hwtime() is only used for some obscure debugging + ** and analysis configurations, not in any deliverable, so this + ** should not be a great loss. */ sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); } #endif -#endif /* !defined(_HWTIME_H_) */ +#endif /* !defined(SQLITE_HWTIME_H) */ diff --git a/src/in-operator.md b/src/in-operator.md new file mode 100644 index 0000000000..e9ad2101aa --- /dev/null +++ b/src/in-operator.md @@ -0,0 +1,107 @@ +IN-Operator Implementation Notes +================================ + +## Definitions: + +An IN operator has one of the following formats: + +> + x IN (y1,y2,y3,...,yN) + x IN (subquery) + +The "x" is referred to as the LHS (left-hand side). The list or subquery +on the right is called the RHS (right-hand side). If the RHS is a list +it must be a non-empty list. But if the RHS is a subquery, it can be an +empty set. + +The LHS can be a scalar (a single quantity) or a vector (a list of +two or or more values) or a subquery that returns one or more columns. +We use the term "vector" to mean an actually list of values or a +subquery that returns two or more columns. An isolated value or +a subquery that returns a single columns is called a scalar. + +The RHS can be a subquery that returns a single column, a subquery +that returns two or more columns, or a list of scalars. It is not +currently support for the RHS to be a list of vectors. + +The number of columns for LHS must match the number of columns for +the RHS. If the RHS is a list of values, then the LHS must be a +scalar. If the RHS is a subquery returning N columns, then the LHS +must be a vector of size N. + +NULL values can occur in either or both of the LHS and RHS. +If the LHS contains only +NULL values then we say that it is a "total-NULL". If the LHS contains +some NULL values and some non-NULL values, then it is a "partial-NULL". +For a scalar, there is no difference between a partial-NULL and a total-NULL. +The RHS is a partial-NULL if any row contains a NULL value. The RHS is +a total-NULL if it contains one or more rows that contain only NULL values. +The LHS is called "non-NULL" if it contains no NULL values. The RHS is +called "non-NULL" if it contains no NULL values in any row. + +The result of an IN operator is one of TRUE, FALSE, or NULL. A NULL result +means that it cannot be determined if the LHS is contained in the RHS due +to the presence of NULL values. In some contexts (for example, when the IN +operator occurs in a WHERE clause) +the system only needs a binary result: TRUE or NOT-TRUE. One can also +to define a binary result of FALSE and NOT-FALSE, but +it turns out that no extra optimizations are possible in that case, so if +the FALSE/NOT-FALSE binary is needed, we have to compute the three-state +TRUE/FALSE/NULL result and then combine the TRUE and NULL values into +NOT-FALSE. + +A "NOT IN" operator is computed by first computing the equivalent IN +operator, then interchanging the TRUE and FALSE results. + +## Simple Full-Scan Algorithm + +The following algorithm always compute the correct answer. However, this +algorithm is suboptimal, especially if there are many rows on the RHS. + + 1. Set the null-flag to false + 2. For each row in the RHS: +
      +
    1. Compare the LHS against the RHS +
    2. If the LHS exactly matches the RHS, immediately return TRUE +
    3. If the comparison result is NULL, set the null-flag to true +
    + 3. If the null-flag is true, return NULL. + 4. Return FALSE + +## Optimized Algorithm + +The following procedure computes the same answer as the simple full-scan +algorithm, though it does so with less work in the common case. This +is the algorithm that is implemented in SQLite. + + 1. If the RHS is a constant list of length 1 or 2, then rewrite the + IN operator as a simple expression. Implement + + x IN (y1,y2) + + as if it were + + x=y1 OR x=y2 + + This is the INDEX_NOOP optimization and is only undertaken if the + IN operator is used for membership testing. If the IN operator is + driving a loop, then skip this step entirely. + + 2. Check the LHS to see if it is a partial-NULL and if it is, jump + ahead to step 5. + + 3. Do a binary search of the RHS using the LHS as a probe. If + an exact match is found, return TRUE. + + 4. If the RHS is non-NULL then return FALSE. + + 5. If we do not need to distinguish between FALSE and NULL, + then return FALSE. + + 6. For each row in the RHS, compare that row against the LHS and + if the result is NULL, immediately return NULL. In the case + of a scalar IN operator, we only need to look at the very first + row the RHS because for a scalar RHS, all NULLs will always come + first. If the RHS is empty, this step is a no-op. + + 7. Return FALSE. diff --git a/src/insert.c b/src/insert.c index 7ff884b8c3..f0c56a7a8f 100644 --- a/src/insert.c +++ b/src/insert.c @@ -15,7 +15,7 @@ #include "sqliteInt.h" /* -** Generate code that will +** Generate code that will ** ** (1) acquire a lock for table pTab then ** (2) open pTab as cursor iCur. @@ -32,17 +32,20 @@ void sqlite3OpenTable( ){ Vdbe *v; assert( !IsVirtual(pTab) ); - v = sqlite3GetVdbe(pParse); + assert( pParse->pVdbe!=0 ); + v = pParse->pVdbe; assert( opcode==OP_OpenWrite || opcode==OP_OpenRead ); - sqlite3TableLock(pParse, iDb, pTab->tnum, - (opcode==OP_OpenWrite)?1:0, pTab->zName); + if( !pParse->db->noSharedCache ){ + sqlite3TableLock(pParse, iDb, pTab->tnum, + (opcode==OP_OpenWrite)?1:0, pTab->zName); + } if( HasRowid(pTab) ){ - sqlite3VdbeAddOp4Int(v, opcode, iCur, pTab->tnum, iDb, pTab->nCol); + sqlite3VdbeAddOp4Int(v, opcode, iCur, pTab->tnum, iDb, pTab->nNVCol); VdbeComment((v, "%s", pTab->zName)); }else{ Index *pPk = sqlite3PrimaryKeyIndex(pTab); assert( pPk!=0 ); - assert( pPk->tnum==pTab->tnum ); + assert( pPk->tnum==pTab->tnum || CORRUPT_DB ); sqlite3VdbeAddOp3(v, opcode, iCur, pPk->tnum, iDb); sqlite3VdbeSetP4KeyInfo(pParse, pPk); VdbeComment((v, "%s", pTab->zName)); @@ -51,7 +54,7 @@ void sqlite3OpenTable( /* ** Return a pointer to the column affinity string associated with index -** pIdx. A column affinity string has one character for each column in +** pIdx. A column affinity string has one character for each column in ** the table, according to the affinity of the column: ** ** Character Column affinity @@ -69,88 +72,152 @@ void sqlite3OpenTable( ** is managed along with the rest of the Index structure. It will be ** released when sqlite3DeleteIndex() is called. */ -const char *sqlite3IndexAffinityStr(sqlite3 *db, Index *pIdx){ +static SQLITE_NOINLINE const char *computeIndexAffStr(sqlite3 *db, Index *pIdx){ + /* The first time a column affinity string for a particular index is + ** required, it is allocated and populated here. It is then stored as + ** a member of the Index structure for subsequent use. + ** + ** The column affinity string will eventually be deleted by + ** sqliteDeleteIndex() when the Index structure itself is cleaned + ** up. + */ + int n; + Table *pTab = pIdx->pTable; + pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+1); if( !pIdx->zColAff ){ - /* The first time a column affinity string for a particular index is - ** required, it is allocated and populated here. It is then stored as - ** a member of the Index structure for subsequent use. - ** - ** The column affinity string will eventually be deleted by - ** sqliteDeleteIndex() when the Index structure itself is cleaned - ** up. - */ - int n; - Table *pTab = pIdx->pTable; - pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+1); - if( !pIdx->zColAff ){ - sqlite3OomFault(db); - return 0; + sqlite3OomFault(db); + return 0; + } + for(n=0; nnColumn; n++){ + i16 x = pIdx->aiColumn[n]; + char aff; + if( x>=0 ){ + aff = pTab->aCol[x].affinity; + }else if( x==XN_ROWID ){ + aff = SQLITE_AFF_INTEGER; + }else{ + assert( x==XN_EXPR ); + assert( pIdx->bHasExpr ); + assert( pIdx->aColExpr!=0 ); + aff = sqlite3ExprAffinity(pIdx->aColExpr->a[n].pExpr); } - for(n=0; nnColumn; n++){ - i16 x = pIdx->aiColumn[n]; - if( x>=0 ){ - pIdx->zColAff[n] = pTab->aCol[x].affinity; - }else if( x==XN_ROWID ){ - pIdx->zColAff[n] = SQLITE_AFF_INTEGER; - }else{ - char aff; - assert( x==XN_EXPR ); - assert( pIdx->aColExpr!=0 ); - aff = sqlite3ExprAffinity(pIdx->aColExpr->a[n].pExpr); - if( aff==0 ) aff = SQLITE_AFF_BLOB; - pIdx->zColAff[n] = aff; + if( affSQLITE_AFF_NUMERIC) aff = SQLITE_AFF_NUMERIC; + pIdx->zColAff[n] = aff; + } + pIdx->zColAff[n] = 0; + return pIdx->zColAff; +} +const char *sqlite3IndexAffinityStr(sqlite3 *db, Index *pIdx){ + if( !pIdx->zColAff ) return computeIndexAffStr(db, pIdx); + return pIdx->zColAff; +} + + +/* +** Compute an affinity string for a table. Space is obtained +** from sqlite3DbMalloc(). The caller is responsible for freeing +** the space when done. +*/ +char *sqlite3TableAffinityStr(sqlite3 *db, const Table *pTab){ + char *zColAff; + zColAff = (char *)sqlite3DbMallocRaw(db, pTab->nCol+1); + if( zColAff ){ + int i, j; + for(i=j=0; inCol; i++){ + if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ){ + zColAff[j++] = pTab->aCol[i].affinity; } } - pIdx->zColAff[n] = 0; + do{ + zColAff[j--] = 0; + }while( j>=0 && zColAff[j]<=SQLITE_AFF_BLOB ); } - - return pIdx->zColAff; + return zColAff; } /* +** Make changes to the evolving bytecode to do affinity transformations +** of values that are about to be gathered into a row for table pTab. +** +** For ordinary (legacy, non-strict) tables: +** ----------------------------------------- +** ** Compute the affinity string for table pTab, if it has not already been ** computed. As an optimization, omit trailing SQLITE_AFF_BLOB affinities. ** -** If the affinity exists (if it is no entirely SQLITE_AFF_BLOB values) and -** if iReg>0 then code an OP_Affinity opcode that will set the affinities -** for register iReg and following. Or if affinities exists and iReg==0, +** If the affinity string is empty (because it was all SQLITE_AFF_BLOB entries +** which were then optimized out) then this routine becomes a no-op. +** +** Otherwise if iReg>0 then code an OP_Affinity opcode that will set the +** affinities for register iReg and following. Or if iReg==0, ** then just set the P4 operand of the previous opcode (which should be ** an OP_MakeRecord) to the affinity string. ** ** A column affinity string has one character per column: ** -** Character Column affinity -** ------------------------------ -** 'A' BLOB -** 'B' TEXT -** 'C' NUMERIC -** 'D' INTEGER -** 'E' REAL +** Character Column affinity +** --------- --------------- +** 'A' BLOB +** 'B' TEXT +** 'C' NUMERIC +** 'D' INTEGER +** 'E' REAL +** +** For STRICT tables: +** ------------------ +** +** Generate an appropriate OP_TypeCheck opcode that will verify the +** datatypes against the column definitions in pTab. If iReg==0, that +** means an OP_MakeRecord opcode has already been generated and should be +** the last opcode generated. The new OP_TypeCheck needs to be inserted +** before the OP_MakeRecord. The new OP_TypeCheck should use the same +** register set as the OP_MakeRecord. If iReg>0 then register iReg is +** the first of a series of registers that will form the new record. +** Apply the type checking to that array of registers. */ void sqlite3TableAffinity(Vdbe *v, Table *pTab, int iReg){ int i; - char *zColAff = pTab->zColAff; + char *zColAff; + if( pTab->tabFlags & TF_Strict ){ + if( iReg==0 ){ + /* Move the previous opcode (which should be OP_MakeRecord) forward + ** by one slot and insert a new OP_TypeCheck where the current + ** OP_MakeRecord is found */ + VdbeOp *pPrev; + int p3; + sqlite3VdbeAppendP4(v, pTab, P4_TABLE); + pPrev = sqlite3VdbeGetLastOp(v); + assert( pPrev!=0 ); + assert( pPrev->opcode==OP_MakeRecord || sqlite3VdbeDb(v)->mallocFailed ); + pPrev->opcode = OP_TypeCheck; + p3 = pPrev->p3; + pPrev->p3 = 0; + sqlite3VdbeAddOp3(v, OP_MakeRecord, pPrev->p1, pPrev->p2, p3); + }else{ + /* Insert an isolated OP_Typecheck */ + sqlite3VdbeAddOp2(v, OP_TypeCheck, iReg, pTab->nNVCol); + sqlite3VdbeAppendP4(v, pTab, P4_TABLE); + } + return; + } + zColAff = pTab->zColAff; if( zColAff==0 ){ - sqlite3 *db = sqlite3VdbeDb(v); - zColAff = (char *)sqlite3DbMallocRaw(0, pTab->nCol+1); + zColAff = sqlite3TableAffinityStr(0, pTab); if( !zColAff ){ - sqlite3OomFault(db); + sqlite3OomFault(sqlite3VdbeDb(v)); return; } - - for(i=0; inCol; i++){ - zColAff[i] = pTab->aCol[i].affinity; - } - do{ - zColAff[i--] = 0; - }while( i>=0 && zColAff[i]==SQLITE_AFF_BLOB ); pTab->zColAff = zColAff; } - i = sqlite3Strlen30(zColAff); + assert( zColAff!=0 ); + i = sqlite3Strlen30NN(zColAff); if( i ){ if( iReg ){ sqlite3VdbeAddOp4(v, OP_Affinity, iReg, i, 0, zColAff, i); }else{ + assert( sqlite3VdbeGetLastOp(v)->opcode==OP_MakeRecord + || sqlite3VdbeDb(v)->mallocFailed ); sqlite3VdbeChangeP4(v, -1, zColAff, i); } } @@ -158,9 +225,9 @@ void sqlite3TableAffinity(Vdbe *v, Table *pTab, int iReg){ /* ** Return non-zero if the table pTab in database iDb or any of its indices -** have been opened at any point in the VDBE program. This is used to see if -** a statement of the form "INSERT INTO SELECT ..." can -** run without using a temporary table for the results of the SELECT. +** have been opened at any point in the VDBE program. This is used to see if +** a statement of the form "INSERT INTO SELECT ..." can +** run without using a temporary table for the results of the SELECT. */ static int readsTable(Parse *p, int iDb, Table *pTab){ Vdbe *v = sqlite3GetVdbe(p); @@ -175,7 +242,7 @@ static int readsTable(Parse *p, int iDb, Table *pTab){ assert( pOp!=0 ); if( pOp->opcode==OP_OpenRead && pOp->p3==iDb ){ Index *pIndex; - int tnum = pOp->p2; + Pgno tnum = pOp->p2; if( tnum==pTab->tnum ){ return 1; } @@ -196,11 +263,132 @@ static int readsTable(Parse *p, int iDb, Table *pTab){ return 0; } +/* This walker callback will compute the union of colFlags flags for all +** referenced columns in a CHECK constraint or generated column expression. +*/ +static int exprColumnFlagUnion(Walker *pWalker, Expr *pExpr){ + if( pExpr->op==TK_COLUMN && pExpr->iColumn>=0 ){ + assert( pExpr->iColumn < pWalker->u.pTab->nCol ); + pWalker->eCode |= pWalker->u.pTab->aCol[pExpr->iColumn].colFlags; + } + return WRC_Continue; +} + +#ifndef SQLITE_OMIT_GENERATED_COLUMNS +/* +** All regular columns for table pTab have been puts into registers +** starting with iRegStore. The registers that correspond to STORED +** or VIRTUAL columns have not yet been initialized. This routine goes +** back and computes the values for those columns based on the previously +** computed normal columns. +*/ +void sqlite3ComputeGeneratedColumns( + Parse *pParse, /* Parsing context */ + int iRegStore, /* Register holding the first column */ + Table *pTab /* The table */ +){ + int i; + Walker w; + Column *pRedo; + int eProgress; + VdbeOp *pOp; + + assert( pTab->tabFlags & TF_HasGenerated ); + testcase( pTab->tabFlags & TF_HasVirtual ); + testcase( pTab->tabFlags & TF_HasStored ); + + /* Before computing generated columns, first go through and make sure + ** that appropriate affinity has been applied to the regular columns + */ + sqlite3TableAffinity(pParse->pVdbe, pTab, iRegStore); + if( (pTab->tabFlags & TF_HasStored)!=0 ){ + pOp = sqlite3VdbeGetLastOp(pParse->pVdbe); + if( pOp->opcode==OP_Affinity ){ + /* Change the OP_Affinity argument to '@' (NONE) for all stored + ** columns. '@' is the no-op affinity and those columns have not + ** yet been computed. */ + int ii, jj; + char *zP4 = pOp->p4.z; + assert( zP4!=0 ); + assert( pOp->p4type==P4_DYNAMIC ); + for(ii=jj=0; zP4[jj]; ii++){ + if( pTab->aCol[ii].colFlags & COLFLAG_VIRTUAL ){ + continue; + } + if( pTab->aCol[ii].colFlags & COLFLAG_STORED ){ + zP4[jj] = SQLITE_AFF_NONE; + } + jj++; + } + }else if( pOp->opcode==OP_TypeCheck ){ + /* If an OP_TypeCheck was generated because the table is STRICT, + ** then set the P3 operand to indicate that generated columns should + ** not be checked */ + pOp->p3 = 1; + } + } + + /* Because there can be multiple generated columns that refer to one another, + ** this is a two-pass algorithm. On the first pass, mark all generated + ** columns as "not available". + */ + for(i=0; inCol; i++){ + if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){ + testcase( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ); + testcase( pTab->aCol[i].colFlags & COLFLAG_STORED ); + pTab->aCol[i].colFlags |= COLFLAG_NOTAVAIL; + } + } + + w.u.pTab = pTab; + w.xExprCallback = exprColumnFlagUnion; + w.xSelectCallback = 0; + w.xSelectCallback2 = 0; + + /* On the second pass, compute the value of each NOT-AVAILABLE column. + ** Companion code in the TK_COLUMN case of sqlite3ExprCodeTarget() will + ** compute dependencies and mark remove the COLSPAN_NOTAVAIL mark, as + ** they are needed. + */ + pParse->iSelfTab = -iRegStore; + do{ + eProgress = 0; + pRedo = 0; + for(i=0; inCol; i++){ + Column *pCol = pTab->aCol + i; + if( (pCol->colFlags & COLFLAG_NOTAVAIL)!=0 ){ + int x; + pCol->colFlags |= COLFLAG_BUSY; + w.eCode = 0; + sqlite3WalkExpr(&w, sqlite3ColumnExpr(pTab, pCol)); + pCol->colFlags &= ~COLFLAG_BUSY; + if( w.eCode & COLFLAG_NOTAVAIL ){ + pRedo = pCol; + continue; + } + eProgress = 1; + assert( pCol->colFlags & COLFLAG_GENERATED ); + x = sqlite3TableColumnToStorage(pTab, i) + iRegStore; + sqlite3ExprCodeGeneratedColumn(pParse, pTab, pCol, x); + pCol->colFlags &= ~COLFLAG_NOTAVAIL; + } + } + }while( pRedo && eProgress ); + if( pRedo ){ + sqlite3ErrorMsg(pParse, "generated column loop on \"%s\"", pRedo->zCnName); + } + pParse->iSelfTab = 0; +} +#endif /* SQLITE_OMIT_GENERATED_COLUMNS */ + + #ifndef SQLITE_OMIT_AUTOINCREMENT /* ** Locate or create an AutoincInfo structure associated with table pTab ** which is in database iDb. Return the register number for the register -** that holds the maximum rowid. +** that holds the maximum rowid. Return zero if pTab is not an AUTOINCREMENT +** table. (Also return zero when doing a VACUUM since we do not want to +** update the AUTOINCREMENT counters during a VACUUM.) ** ** There is at most one AutoincInfo structure per table even if the ** same table is autoincremented multiple times due to inserts within @@ -208,11 +396,12 @@ static int readsTable(Parse *p, int iDb, Table *pTab){ ** first use of table pTab. On 2nd and subsequent uses, the original ** AutoincInfo structure is used. ** -** Three memory locations are allocated: +** Four consecutive registers are allocated: ** -** (1) Register to hold the name of the pTab table. -** (2) Register to hold the maximum ROWID of pTab. -** (3) Register to hold the rowid in sqlite_sequence of pTab +** (1) The name of the pTab table. +** (2) The maximum ROWID of pTab. +** (3) The rowid in sqlite_sequence of pTab +** (4) The original value of the max ROWID in pTab, or NULL if none ** ** The 2nd register is the one that is returned. That is all the ** insert routine needs to know about. @@ -223,22 +412,41 @@ static int autoIncBegin( Table *pTab /* The table we are writing to */ ){ int memId = 0; /* Register holding maximum rowid */ - if( pTab->tabFlags & TF_Autoincrement ){ + assert( pParse->db->aDb[iDb].pSchema!=0 ); + if( (pTab->tabFlags & TF_Autoincrement)!=0 + && (pParse->db->mDbFlags & DBFLAG_Vacuum)==0 + ){ Parse *pToplevel = sqlite3ParseToplevel(pParse); AutoincInfo *pInfo; + Table *pSeqTab = pParse->db->aDb[iDb].pSchema->pSeqTab; + + /* Verify that the sqlite_sequence table exists and is an ordinary + ** rowid table with exactly two columns. + ** Ticket d8dc2b3a58cd5dc2918a1d4acb 2018-05-23 */ + if( pSeqTab==0 + || !HasRowid(pSeqTab) + || NEVER(IsVirtual(pSeqTab)) + || pSeqTab->nCol!=2 + ){ + pParse->nErr++; + pParse->rc = SQLITE_CORRUPT_SEQUENCE; + return 0; + } pInfo = pToplevel->pAinc; while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; } if( pInfo==0 ){ pInfo = sqlite3DbMallocRawNN(pParse->db, sizeof(*pInfo)); - if( pInfo==0 ) return 0; + sqlite3ParserAddCleanup(pToplevel, sqlite3DbFree, pInfo); + testcase( pParse->earlyCleanup ); + if( pParse->db->mallocFailed ) return 0; pInfo->pNext = pToplevel->pAinc; pToplevel->pAinc = pInfo; pInfo->pTab = pTab; pInfo->iDb = iDb; pToplevel->nMem++; /* Register to hold name of table */ pInfo->regCtr = ++pToplevel->nMem; /* Max rowid register */ - pToplevel->nMem++; /* Rowid in sqlite_sequence */ + pToplevel->nMem +=2; /* Rowid in sqlite_sequence + orig max val */ } memId = pInfo->regCtr; } @@ -247,7 +455,7 @@ static int autoIncBegin( /* ** This routine generates code that will initialize all of the -** register used by the autoincrement tracker. +** register used by the autoincrement tracker. */ void sqlite3AutoincrementBegin(Parse *pParse){ AutoincInfo *p; /* Information about an AUTOINCREMENT */ @@ -266,15 +474,17 @@ void sqlite3AutoincrementBegin(Parse *pParse){ static const int iLn = VDBE_OFFSET_LINENO(2); static const VdbeOpList autoInc[] = { /* 0 */ {OP_Null, 0, 0, 0}, - /* 1 */ {OP_Rewind, 0, 9, 0}, + /* 1 */ {OP_Rewind, 0, 10, 0}, /* 2 */ {OP_Column, 0, 0, 0}, - /* 3 */ {OP_Ne, 0, 7, 0}, + /* 3 */ {OP_Ne, 0, 9, 0}, /* 4 */ {OP_Rowid, 0, 0, 0}, /* 5 */ {OP_Column, 0, 1, 0}, - /* 6 */ {OP_Goto, 0, 9, 0}, - /* 7 */ {OP_Next, 0, 2, 0}, - /* 8 */ {OP_Integer, 0, 0, 0}, - /* 9 */ {OP_Close, 0, 0, 0} + /* 6 */ {OP_AddImm, 0, 0, 0}, + /* 7 */ {OP_Copy, 0, 0, 0}, + /* 8 */ {OP_Goto, 0, 11, 0}, + /* 9 */ {OP_Next, 0, 2, 0}, + /* 10 */ {OP_Integer, 0, 0, 0}, + /* 11 */ {OP_Close, 0, 0, 0} }; VdbeOp *aOp; pDb = &db->aDb[p->iDb]; @@ -285,14 +495,18 @@ void sqlite3AutoincrementBegin(Parse *pParse){ aOp = sqlite3VdbeAddOpList(v, ArraySize(autoInc), autoInc, iLn); if( aOp==0 ) break; aOp[0].p2 = memId; - aOp[0].p3 = memId+1; + aOp[0].p3 = memId+2; aOp[2].p3 = memId; aOp[3].p1 = memId-1; aOp[3].p3 = memId; aOp[3].p5 = SQLITE_JUMPIFNULL; aOp[4].p2 = memId+1; aOp[5].p3 = memId; - aOp[8].p2 = memId; + aOp[6].p1 = memId; + aOp[7].p2 = memId+2; + aOp[7].p1 = memId; + aOp[10].p2 = memId; + if( pParse->nTab==0 ) pParse->nTab = 1; } } @@ -339,6 +553,8 @@ static SQLITE_NOINLINE void autoIncrementEnd(Parse *pParse){ iRec = sqlite3GetTempReg(pParse); assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) ); + sqlite3VdbeAddOp3(v, OP_Le, memId+2, sqlite3VdbeCurrentAddr(v)+7, memId); + VdbeCoverage(v); sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite); aOp = sqlite3VdbeAddOpList(v, ArraySize(autoIncEnd), autoIncEnd, iLn); if( aOp==0 ) break; @@ -364,6 +580,210 @@ void sqlite3AutoincrementEnd(Parse *pParse){ # define autoIncStep(A,B,C) #endif /* SQLITE_OMIT_AUTOINCREMENT */ +/* +** If argument pVal is a Select object returned by an sqlite3MultiValues() +** that was able to use the co-routine optimization, finish coding the +** co-routine. +*/ +void sqlite3MultiValuesEnd(Parse *pParse, Select *pVal){ + if( ALWAYS(pVal) && pVal->pSrc->nSrc>0 ){ + SrcItem *pItem = &pVal->pSrc->a[0]; + assert( (pItem->fg.isSubquery && pItem->u4.pSubq!=0) || pParse->nErr ); + if( pItem->fg.isSubquery ){ + sqlite3VdbeEndCoroutine(pParse->pVdbe, pItem->u4.pSubq->regReturn); + sqlite3VdbeJumpHere(pParse->pVdbe, pItem->u4.pSubq->addrFillSub - 1); + } + } +} + +/* +** Return true if all expressions in the expression-list passed as the +** only argument are constant. +*/ +static int exprListIsConstant(Parse *pParse, ExprList *pRow){ + int ii; + for(ii=0; iinExpr; ii++){ + if( 0==sqlite3ExprIsConstant(pParse, pRow->a[ii].pExpr) ) return 0; + } + return 1; +} + +/* +** Return true if all expressions in the expression-list passed as the +** only argument are both constant and have no affinity. +*/ +static int exprListIsNoAffinity(Parse *pParse, ExprList *pRow){ + int ii; + if( exprListIsConstant(pParse,pRow)==0 ) return 0; + for(ii=0; iinExpr; ii++){ + Expr *pExpr = pRow->a[ii].pExpr; + assert( pExpr->op!=TK_RAISE ); + assert( pExpr->affExpr==0 ); + if( 0!=sqlite3ExprAffinity(pExpr) ) return 0; + } + return 1; + +} + +/* +** This function is called by the parser for the second and subsequent +** rows of a multi-row VALUES clause. Argument pLeft is the part of +** the VALUES clause already parsed, argument pRow is the vector of values +** for the new row. The Select object returned represents the complete +** VALUES clause, including the new row. +** +** There are two ways in which this may be achieved - by incremental +** coding of a co-routine (the "co-routine" method) or by returning a +** Select object equivalent to the following (the "UNION ALL" method): +** +** "pLeft UNION ALL SELECT pRow" +** +** If the VALUES clause contains a lot of rows, this compound Select +** object may consume a lot of memory. +** +** When the co-routine method is used, each row that will be returned +** by the VALUES clause is coded into part of a co-routine as it is +** passed to this function. The returned Select object is equivalent to: +** +** SELECT * FROM ( +** Select object to read co-routine +** ) +** +** The co-routine method is used in most cases. Exceptions are: +** +** a) If the current statement has a WITH clause. This is to avoid +** statements like: +** +** WITH cte AS ( VALUES('x'), ('y') ... ) +** SELECT * FROM cte AS a, cte AS b; +** +** This will not work, as the co-routine uses a hard-coded register +** for its OP_Yield instructions, and so it is not possible for two +** cursors to iterate through it concurrently. +** +** b) The schema is currently being parsed (i.e. the VALUES clause is part +** of a schema item like a VIEW or TRIGGER). In this case there is no VM +** being generated when parsing is taking place, and so generating +** a co-routine is not possible. +** +** c) There are non-constant expressions in the VALUES clause (e.g. +** the VALUES clause is part of a correlated sub-query). +** +** d) One or more of the values in the first row of the VALUES clause +** has an affinity (i.e. is a CAST expression). This causes problems +** because the complex rules SQLite uses (see function +** sqlite3SubqueryColumnTypes() in select.c) to determine the effective +** affinity of such a column for all rows require access to all values in +** the column simultaneously. +*/ +Select *sqlite3MultiValues(Parse *pParse, Select *pLeft, ExprList *pRow){ + + if( pParse->bHasWith /* condition (a) above */ + || pParse->db->init.busy /* condition (b) above */ + || exprListIsConstant(pParse,pRow)==0 /* condition (c) above */ + || (pLeft->pSrc->nSrc==0 && + exprListIsNoAffinity(pParse,pLeft->pEList)==0) /* condition (d) above */ + || IN_SPECIAL_PARSE + ){ + /* The co-routine method cannot be used. Fall back to UNION ALL. */ + Select *pSelect = 0; + int f = SF_Values | SF_MultiValue; + if( pLeft->pSrc->nSrc ){ + sqlite3MultiValuesEnd(pParse, pLeft); + f = SF_Values; + }else if( pLeft->pPrior ){ + /* In this case set the SF_MultiValue flag only if it was set on pLeft */ + f = (f & pLeft->selFlags); + } + pSelect = sqlite3SelectNew(pParse, pRow, 0, 0, 0, 0, 0, f, 0); + pLeft->selFlags &= ~(u32)SF_MultiValue; + if( pSelect ){ + pSelect->op = TK_ALL; + pSelect->pPrior = pLeft; + pLeft = pSelect; + } + }else{ + SrcItem *p = 0; /* SrcItem that reads from co-routine */ + + if( pLeft->pSrc->nSrc==0 ){ + /* Co-routine has not yet been started and the special Select object + ** that accesses the co-routine has not yet been created. This block + ** does both those things. */ + Vdbe *v = sqlite3GetVdbe(pParse); + Select *pRet = sqlite3SelectNew(pParse, 0, 0, 0, 0, 0, 0, 0, 0); + + /* Ensure the database schema has been read. This is to ensure we have + ** the correct text encoding. */ + if( (pParse->db->mDbFlags & DBFLAG_SchemaKnownOk)==0 ){ + sqlite3ReadSchema(pParse); + } + + if( pRet ){ + SelectDest dest; + Subquery *pSubq; + pRet->pSrc->nSrc = 1; + pRet->pPrior = pLeft->pPrior; + pRet->op = pLeft->op; + if( pRet->pPrior ) pRet->selFlags |= SF_Values; + pLeft->pPrior = 0; + pLeft->op = TK_SELECT; + assert( pLeft->pNext==0 ); + assert( pRet->pNext==0 ); + p = &pRet->pSrc->a[0]; + p->fg.viaCoroutine = 1; + p->iCursor = -1; + assert( !p->fg.isIndexedBy && !p->fg.isTabFunc ); + p->u1.nRow = 2; + if( sqlite3SrcItemAttachSubquery(pParse, p, pLeft, 0) ){ + pSubq = p->u4.pSubq; + pSubq->addrFillSub = sqlite3VdbeCurrentAddr(v) + 1; + pSubq->regReturn = ++pParse->nMem; + sqlite3VdbeAddOp3(v, OP_InitCoroutine, + pSubq->regReturn, 0, pSubq->addrFillSub); + sqlite3SelectDestInit(&dest, SRT_Coroutine, pSubq->regReturn); + + /* Allocate registers for the output of the co-routine. Do so so + ** that there are two unused registers immediately before those + ** used by the co-routine. This allows the code in sqlite3Insert() + ** to use these registers directly, instead of copying the output + ** of the co-routine to a separate array for processing. */ + dest.iSdst = pParse->nMem + 3; + dest.nSdst = pLeft->pEList->nExpr; + pParse->nMem += 2 + dest.nSdst; + + pLeft->selFlags |= SF_MultiValue; + sqlite3Select(pParse, pLeft, &dest); + pSubq->regResult = dest.iSdst; + assert( pParse->nErr || dest.iSdst>0 ); + } + pLeft = pRet; + } + }else{ + p = &pLeft->pSrc->a[0]; + assert( !p->fg.isTabFunc && !p->fg.isIndexedBy ); + p->u1.nRow++; + } + + if( pParse->nErr==0 ){ + Subquery *pSubq; + assert( p!=0 ); + assert( p->fg.isSubquery ); + pSubq = p->u4.pSubq; + assert( pSubq!=0 ); + assert( pSubq->pSelect!=0 ); + assert( pSubq->pSelect->pEList!=0 ); + if( pSubq->pSelect->pEList->nExpr!=pRow->nExpr ){ + sqlite3SelectWrongNumTermsError(pParse, pSubq->pSelect); + }else{ + sqlite3ExprCodeExprList(pParse, pRow, pSubq->regResult, 0, 0); + sqlite3VdbeAddOp1(pParse->pVdbe, OP_Yield, pSubq->regReturn); + } + } + sqlite3ExprListDelete(pParse->db, pRow); + } + + return pLeft; +} /* Forward declaration */ static int xferOptimization( @@ -475,14 +895,13 @@ void sqlite3Insert( Parse *pParse, /* Parser context */ SrcList *pTabList, /* Name of table into which we are inserting */ Select *pSelect, /* A SELECT statement to use as the data source */ - IdList *pColumn, /* Column names corresponding to IDLIST. */ - int onError /* How to handle constraint errors */ + IdList *pColumn, /* Column names corresponding to IDLIST, or NULL. */ + int onError, /* How to handle constraint errors */ + Upsert *pUpsert /* ON CONFLICT clauses for upsert, or NULL */ ){ sqlite3 *db; /* The main database structure */ Table *pTab; /* The table to insert into. aka TABLE */ - char *zTab; /* Name of the table into which we are inserting */ - const char *zDb; /* Name of the database holding this table */ - int i, j, idx; /* Loop counters */ + int i, j; /* Loop counters */ Vdbe *v; /* Generate code into this virtual machine */ Index *pIdx; /* For looping over indices of the table */ int nColumn; /* Number of columns in the data */ @@ -496,12 +915,12 @@ void sqlite3Insert( int addrCont = 0; /* Top of insert loop. Label "C" in templates 3 and 4 */ SelectDest dest; /* Destination for SELECT on rhs of INSERT */ int iDb; /* Index of database holding TABLE */ - Db *pDb; /* The database containing table being inserted into */ u8 useTempTable = 0; /* Store SELECT results in intermediate table */ u8 appendFlag = 0; /* True if the insert is likely to be an append */ u8 withoutRowid; /* 0 for normal table. 1 for WITHOUT ROWID table */ u8 bIdListInOrder; /* True if IDLIST is in table order */ ExprList *pList = 0; /* List of VALUES() to be inserted */ + int iRegStore; /* Register in which to store next column */ /* Register allocations */ int regFromSelect = 0;/* Base register for data coming from SELECT */ @@ -511,6 +930,7 @@ void sqlite3Insert( int regRowid; /* registers holding insert rowid */ int regData; /* register holding first column to insert */ int *aRegIdx = 0; /* One register allocated to each index */ + int *aTabColMap = 0; /* Mapping from pTab columns to pCol entries */ #ifndef SQLITE_OMIT_TRIGGER int isView; /* True if attempting to insert into a view */ @@ -519,10 +939,12 @@ void sqlite3Insert( #endif db = pParse->db; - memset(&dest, 0, sizeof(dest)); - if( pParse->nErr || db->mallocFailed ){ + assert( db->pParse==pParse ); + if( pParse->nErr ){ goto insert_cleanup; } + assert( db->mallocFailed==0 ); + dest.iSDParm = 0; /* Suppress a harmless compiler warning */ /* If the Select object is really just a simple VALUES() list with a ** single row (the common case) then keep that one row of values @@ -538,17 +960,14 @@ void sqlite3Insert( /* Locate the table into which we will be inserting new information. */ assert( pTabList->nSrc==1 ); - zTab = pTabList->a[0].zName; - if( NEVER(zTab==0) ) goto insert_cleanup; pTab = sqlite3SrcListLookup(pParse, pTabList); if( pTab==0 ){ goto insert_cleanup; } iDb = sqlite3SchemaToIndex(db, pTab->pSchema); assert( iDbnDb ); - pDb = &db->aDb[iDb]; - zDb = pDb->zName; - if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb) ){ + if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, + db->aDb[iDb].zDbSName) ){ goto insert_cleanup; } withoutRowid = !HasRowid(pTab); @@ -558,7 +977,7 @@ void sqlite3Insert( */ #ifndef SQLITE_OMIT_TRIGGER pTrigger = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0, &tmask); - isView = pTab->pSelect!=0; + isView = IsView(pTab); #else # define pTrigger 0 # define tmask 0 @@ -570,6 +989,14 @@ void sqlite3Insert( #endif assert( (pTrigger && tmask) || (pTrigger==0 && tmask==0) ); +#if TREETRACE_ENABLED + if( sqlite3TreeTrace & 0x10000 ){ + sqlite3TreeViewLine(0, "In sqlite3Insert() at %s:%d", __FILE__, __LINE__); + sqlite3TreeViewInsert(pParse->pWith, pTabList, pColumn, pSelect, pList, + onError, pUpsert, pTrigger); + } +#endif + /* If pTab is really a view, make sure it has been initialized. ** ViewGetColumnNames() is a no-op if pTab is not a view. */ @@ -579,7 +1006,7 @@ void sqlite3Insert( /* Cannot insert into a read-only table. */ - if( sqlite3IsReadOnly(pParse, pTab, tmask) ){ + if( sqlite3IsReadOnly(pParse, pTab, pTrigger) ){ goto insert_cleanup; } @@ -600,7 +1027,11 @@ void sqlite3Insert( ** ** This is the 2nd template. */ - if( pColumn==0 && xferOptimization(pParse, pTab, pSelect, onError, iDb) ){ + if( pColumn==0 + && pSelect!=0 + && pTrigger==0 + && xferOptimization(pParse, pTab, pSelect, onError, iDb) + ){ assert( !pTrigger ); assert( pList==0 ); goto insert_end; @@ -612,8 +1043,8 @@ void sqlite3Insert( */ regAutoinc = autoIncBegin(pParse, iDb, pTab); - /* Allocate registers for holding the rowid of the new row, - ** the content of the new row, and the assembled row record. + /* Allocate a block registers to hold the rowid and the values + ** for all columns of the new row. */ regRowid = regIns = pParse->nMem+1; pParse->nMem += pTab->nCol + 1; @@ -624,7 +1055,7 @@ void sqlite3Insert( regData = regRowid+1; /* If the INSERT statement included an IDLIST term, then make sure - ** all elements of the IDLIST really are columns of the table and + ** all elements of the IDLIST really are columns of the table and ** remember the column indices. ** ** If the table has an INTEGER PRIMARY KEY column and that column @@ -632,31 +1063,43 @@ void sqlite3Insert( ** the index into IDLIST of the primary key column. ipkColumn is ** the index of the primary key as it appears in IDLIST, not as ** is appears in the original table. (The index of the INTEGER - ** PRIMARY KEY in the original table is pTab->iPKey.) + ** PRIMARY KEY in the original table is pTab->iPKey.) After this + ** loop, if ipkColumn==(-1), that means that integer primary key + ** is unspecified, and hence the table is either WITHOUT ROWID or + ** it will automatically generated an integer primary key. + ** + ** bIdListInOrder is true if the columns in IDLIST are in storage + ** order. This enables an optimization that avoids shuffling the + ** columns into storage order. False negatives are harmless, + ** but false positives will cause database corruption. */ - bIdListInOrder = (pTab->tabFlags & TF_OOOHidden)==0; + bIdListInOrder = (pTab->tabFlags & (TF_OOOHidden|TF_HasStored))==0; if( pColumn ){ + aTabColMap = sqlite3DbMallocZero(db, pTab->nCol*sizeof(int)); + if( aTabColMap==0 ) goto insert_cleanup; for(i=0; inId; i++){ - pColumn->a[i].idx = -1; - } - for(i=0; inId; i++){ - for(j=0; jnCol; j++){ - if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){ - pColumn->a[i].idx = j; - if( i!=j ) bIdListInOrder = 0; - if( j==pTab->iPKey ){ - ipkColumn = i; assert( !withoutRowid ); - } - break; + j = sqlite3ColumnIndex(pTab, pColumn->a[i].zName); + if( j>=0 ){ + if( aTabColMap[j]==0 ) aTabColMap[j] = i+1; + if( i!=j ) bIdListInOrder = 0; + if( j==pTab->iPKey ){ + ipkColumn = i; assert( !withoutRowid ); } - } - if( j>=pTab->nCol ){ +#ifndef SQLITE_OMIT_GENERATED_COLUMNS + if( pTab->aCol[j].colFlags & (COLFLAG_STORED|COLFLAG_VIRTUAL) ){ + sqlite3ErrorMsg(pParse, + "cannot INSERT into generated column \"%s\"", + pTab->aCol[j].zCnName); + goto insert_cleanup; + } +#endif + }else{ if( sqlite3IsRowid(pColumn->a[i].zName) && !withoutRowid ){ ipkColumn = i; bIdListInOrder = 0; }else{ sqlite3ErrorMsg(pParse, "table %S has no column named %s", - pTabList, 0, pColumn->a[i].zName); + pTabList->a, pColumn->a[i].zName); pParse->checkSchema = 1; goto insert_cleanup; } @@ -672,23 +1115,45 @@ void sqlite3Insert( if( pSelect ){ /* Data is coming from a SELECT or from a multi-row VALUES clause. ** Generate a co-routine to run the SELECT. */ - int regYield; /* Register holding co-routine entry-point */ - int addrTop; /* Top of the co-routine */ int rc; /* Result code */ - regYield = ++pParse->nMem; - addrTop = sqlite3VdbeCurrentAddr(v) + 1; - sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop); - sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield); - dest.iSdst = bIdListInOrder ? regData : 0; - dest.nSdst = pTab->nCol; - rc = sqlite3Select(pParse, pSelect, &dest); - regFromSelect = dest.iSdst; - if( rc || db->mallocFailed || pParse->nErr ) goto insert_cleanup; - sqlite3VdbeEndCoroutine(v, regYield); - sqlite3VdbeJumpHere(v, addrTop - 1); /* label B: */ - assert( pSelect->pEList ); - nColumn = pSelect->pEList->nExpr; + if( pSelect->pSrc->nSrc==1 + && pSelect->pSrc->a[0].fg.viaCoroutine + && pSelect->pPrior==0 + ){ + SrcItem *pItem = &pSelect->pSrc->a[0]; + Subquery *pSubq; + assert( pItem->fg.isSubquery ); + pSubq = pItem->u4.pSubq; + dest.iSDParm = pSubq->regReturn; + regFromSelect = pSubq->regResult; + assert( pSubq->pSelect!=0 ); + assert( pSubq->pSelect->pEList!=0 ); + nColumn = pSubq->pSelect->pEList->nExpr; + ExplainQueryPlan((pParse, 0, "SCAN %S", pItem)); + if( bIdListInOrder && nColumn==pTab->nCol ){ + regData = regFromSelect; + regRowid = regData - 1; + regIns = regRowid - (IsVirtual(pTab) ? 1 : 0); + } + }else{ + int addrTop; /* Top of the co-routine */ + int regYield = ++pParse->nMem; + addrTop = sqlite3VdbeCurrentAddr(v) + 1; + sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop); + sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield); + dest.iSdst = bIdListInOrder ? regData : 0; + dest.nSdst = pTab->nCol; + rc = sqlite3Select(pParse, pSelect, &dest); + regFromSelect = dest.iSdst; + assert( db->pParse==pParse ); + if( rc || pParse->nErr ) goto insert_cleanup; + assert( db->mallocFailed==0 ); + sqlite3VdbeEndCoroutine(v, regYield); + sqlite3VdbeJumpHere(v, addrTop - 1); /* label B: */ + assert( pSelect->pEList ); + nColumn = pSelect->pEList->nExpr; + } /* Set useTempTable to TRUE if the result of the SELECT statement ** should be written into a temporary table (template 4). Set to @@ -696,7 +1161,7 @@ void sqlite3Insert( ** the destination table (template 3). ** ** A temp table must be used if the table being updated is also one - ** of the tables being read by the SELECT statement. Also use a + ** of the tables being read by the SELECT statement. Also use a ** temp table in the case of row triggers. */ if( pTrigger || readsTable(pParse, iDb, pTab) ){ @@ -732,7 +1197,7 @@ void sqlite3Insert( sqlite3ReleaseTempReg(pParse, regTempRowid); } }else{ - /* This is the case if the data for the INSERT is coming from a + /* This is the case if the data for the INSERT is coming from a ** single-row VALUES clause */ NameContext sNC; @@ -751,33 +1216,55 @@ void sqlite3Insert( } /* If there is no IDLIST term but the table has an integer primary - ** key, the set the ipkColumn variable to the integer primary key + ** key, the set the ipkColumn variable to the integer primary key ** column index in the original table definition. */ if( pColumn==0 && nColumn>0 ){ ipkColumn = pTab->iPKey; - } +#ifndef SQLITE_OMIT_GENERATED_COLUMNS + if( ipkColumn>=0 && (pTab->tabFlags & TF_HasGenerated)!=0 ){ + testcase( pTab->tabFlags & TF_HasVirtual ); + testcase( pTab->tabFlags & TF_HasStored ); + for(i=ipkColumn-1; i>=0; i--){ + if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){ + testcase( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ); + testcase( pTab->aCol[i].colFlags & COLFLAG_STORED ); + ipkColumn--; + } + } + } +#endif - /* Make sure the number of columns in the source data matches the number - ** of columns to be inserted into the table. - */ - for(i=0; inCol; i++){ - nHidden += (IsHiddenColumn(&pTab->aCol[i]) ? 1 : 0); - } - if( pColumn==0 && nColumn && nColumn!=(pTab->nCol-nHidden) ){ - sqlite3ErrorMsg(pParse, - "table %S has %d columns but %d values were supplied", - pTabList, 0, pTab->nCol-nHidden, nColumn); - goto insert_cleanup; + /* Make sure the number of columns in the source data matches the number + ** of columns to be inserted into the table. + */ + assert( TF_HasHidden==COLFLAG_HIDDEN ); + assert( TF_HasGenerated==COLFLAG_GENERATED ); + assert( COLFLAG_NOINSERT==(COLFLAG_GENERATED|COLFLAG_HIDDEN) ); + if( (pTab->tabFlags & (TF_HasGenerated|TF_HasHidden))!=0 ){ + for(i=0; inCol; i++){ + if( pTab->aCol[i].colFlags & COLFLAG_NOINSERT ) nHidden++; + } + } + if( nColumn!=(pTab->nCol-nHidden) ){ + sqlite3ErrorMsg(pParse, + "table %S has %d columns but %d values were supplied", + pTabList->a, pTab->nCol-nHidden, nColumn); + goto insert_cleanup; + } } if( pColumn!=0 && nColumn!=pColumn->nId ){ sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId); goto insert_cleanup; } - + /* Initialize the count of rows to be inserted */ - if( db->flags & SQLITE_CountRows ){ + if( (db->flags & SQLITE_CountRows)!=0 + && !pParse->nested + && !pParse->pTriggerTab + && !pParse->bReturning + ){ regRowCount = ++pParse->nMem; sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount); } @@ -787,14 +1274,49 @@ void sqlite3Insert( int nIdx; nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, -1, 0, &iDataCur, &iIdxCur); - aRegIdx = sqlite3DbMallocRawNN(db, sizeof(int)*(nIdx+1)); + aRegIdx = sqlite3DbMallocRawNN(db, sizeof(int)*(nIdx+2)); if( aRegIdx==0 ){ goto insert_cleanup; } - for(i=0; ipIndex; ipNext, i++){ + assert( pIdx ); aRegIdx[i] = ++pParse->nMem; + pParse->nMem += pIdx->nColumn; + } + aRegIdx[i] = ++pParse->nMem; /* Register to store the table record */ + } +#ifndef SQLITE_OMIT_UPSERT + if( pUpsert ){ + Upsert *pNx; + if( IsVirtual(pTab) ){ + sqlite3ErrorMsg(pParse, "UPSERT not implemented for virtual table \"%s\"", + pTab->zName); + goto insert_cleanup; + } + if( IsView(pTab) ){ + sqlite3ErrorMsg(pParse, "cannot UPSERT a view"); + goto insert_cleanup; + } + if( sqlite3HasExplicitNulls(pParse, pUpsert->pUpsertTarget) ){ + goto insert_cleanup; } + pTabList->a[0].iCursor = iDataCur; + pNx = pUpsert; + do{ + pNx->pUpsertSrc = pTabList; + pNx->regData = regData; + pNx->iDataCur = iDataCur; + pNx->iIdxCur = iIdxCur; + if( pNx->pUpsertTarget ){ + if( sqlite3UpsertAnalyzeTarget(pParse, pTabList, pNx, pUpsert) ){ + goto insert_cleanup; + } + } + pNx = pNx->pNextUpsert; + }while( pNx!=0 ); } +#endif + /* This is the top of the main insertion loop */ if( useTempTable ){ @@ -818,13 +1340,106 @@ void sqlite3Insert( ** goto C ** D: ... */ + sqlite3VdbeReleaseRegisters(pParse, regData, pTab->nCol, 0, 0); addrInsTop = addrCont = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm); VdbeCoverage(v); + if( ipkColumn>=0 ){ + /* tag-20191021-001: If the INTEGER PRIMARY KEY is being generated by the + ** SELECT, go ahead and copy the value into the rowid slot now, so that + ** the value does not get overwritten by a NULL at tag-20191021-002. */ + sqlite3VdbeAddOp2(v, OP_Copy, regFromSelect+ipkColumn, regRowid); + } } + /* Compute data for ordinary columns of the new entry. Values + ** are written in storage order into registers starting with regData. + ** Only ordinary columns are computed in this loop. The rowid + ** (if there is one) is computed later and generated columns are + ** computed after the rowid since they might depend on the value + ** of the rowid. + */ + nHidden = 0; + iRegStore = regData; assert( regData==regRowid+1 ); + for(i=0; inCol; i++, iRegStore++){ + int k; + u32 colFlags; + assert( i>=nHidden ); + if( i==pTab->iPKey ){ + /* tag-20191021-002: References to the INTEGER PRIMARY KEY are filled + ** using the rowid. So put a NULL in the IPK slot of the record to avoid + ** using excess space. The file format definition requires this extra + ** NULL - we cannot optimize further by skipping the column completely */ + sqlite3VdbeAddOp1(v, OP_SoftNull, iRegStore); + continue; + } + if( ((colFlags = pTab->aCol[i].colFlags) & COLFLAG_NOINSERT)!=0 ){ + nHidden++; + if( (colFlags & COLFLAG_VIRTUAL)!=0 ){ + /* Virtual columns do not participate in OP_MakeRecord. So back up + ** iRegStore by one slot to compensate for the iRegStore++ in the + ** outer for() loop */ + iRegStore--; + continue; + }else if( (colFlags & COLFLAG_STORED)!=0 ){ + /* Stored columns are computed later. But if there are BEFORE + ** triggers, the slots used for stored columns will be OP_Copy-ed + ** to a second block of registers, so the register needs to be + ** initialized to NULL to avoid an uninitialized register read */ + if( tmask & TRIGGER_BEFORE ){ + sqlite3VdbeAddOp1(v, OP_SoftNull, iRegStore); + } + continue; + }else if( pColumn==0 ){ + /* Hidden columns that are not explicitly named in the INSERT + ** get their default value */ + sqlite3ExprCodeFactorable(pParse, + sqlite3ColumnExpr(pTab, &pTab->aCol[i]), + iRegStore); + continue; + } + } + if( pColumn ){ + j = aTabColMap[i]; + assert( j>=0 && j<=pColumn->nId ); + if( j==0 ){ + /* A column not named in the insert column list gets its + ** default value */ + sqlite3ExprCodeFactorable(pParse, + sqlite3ColumnExpr(pTab, &pTab->aCol[i]), + iRegStore); + continue; + } + k = j - 1; + }else if( nColumn==0 ){ + /* This is INSERT INTO ... DEFAULT VALUES. Load the default value. */ + sqlite3ExprCodeFactorable(pParse, + sqlite3ColumnExpr(pTab, &pTab->aCol[i]), + iRegStore); + continue; + }else{ + k = i - nHidden; + } + + if( useTempTable ){ + sqlite3VdbeAddOp3(v, OP_Column, srcTab, k, iRegStore); + }else if( pSelect ){ + if( regFromSelect!=regData ){ + sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+k, iRegStore); + } + }else{ + Expr *pX = pList->a[k].pExpr; + int y = sqlite3ExprCodeTarget(pParse, pX, iRegStore); + if( y!=iRegStore ){ + sqlite3VdbeAddOp2(v, + ExprHasProperty(pX, EP_Subquery) ? OP_Copy : OP_SCopy, y, iRegStore); + } + } + } + + /* Run the BEFORE and INSTEAD OF triggers, if there are any */ - endOfLoop = sqlite3VdbeMakeLabel(v); + endOfLoop = sqlite3VdbeMakeLabel(pParse); if( tmask & TRIGGER_BEFORE ){ int regCols = sqlite3GetTempRange(pParse, pTab->nCol+1); @@ -851,30 +1466,21 @@ void sqlite3Insert( sqlite3VdbeAddOp1(v, OP_MustBeInt, regCols); VdbeCoverage(v); } - /* Cannot have triggers on a virtual table. If it were possible, - ** this block would have to account for hidden column. - */ - assert( !IsVirtual(pTab) ); - - /* Create the new column data - */ - for(i=j=0; inCol; i++){ - if( pColumn ){ - for(j=0; jnId; j++){ - if( pColumn->a[j].idx==i ) break; - } - } - if( (!useTempTable && !pList) || (pColumn && j>=pColumn->nId) - || (pColumn==0 && IsOrdinaryHiddenColumn(&pTab->aCol[i])) ){ - sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regCols+i+1); - }else if( useTempTable ){ - sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, regCols+i+1); - }else{ - assert( pSelect==0 ); /* Otherwise useTempTable is true */ - sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr, regCols+i+1); - } - if( pColumn==0 && !IsOrdinaryHiddenColumn(&pTab->aCol[i]) ) j++; + /* Copy the new data already generated. */ + assert( pTab->nNVCol>0 || pParse->nErr>0 ); + sqlite3VdbeAddOp3(v, OP_Copy, regRowid+1, regCols+1, pTab->nNVCol-1); + +#ifndef SQLITE_OMIT_GENERATED_COLUMNS + /* Compute the new value for generated columns after all other + ** columns have already been computed. This must be done after + ** computing the ROWID in case one of the generated columns + ** refers to the ROWID. */ + if( pTab->tabFlags & TF_HasGenerated ){ + testcase( pTab->tabFlags & TF_HasVirtual ); + testcase( pTab->tabFlags & TF_HasStored ); + sqlite3ComputeGeneratedColumns(pParse, regCols+1, pTab); } +#endif /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger, ** do not attempt any conversions before assembling the record. @@ -886,35 +1492,30 @@ void sqlite3Insert( } /* Fire BEFORE or INSTEAD OF triggers */ - sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_BEFORE, + sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_BEFORE, pTab, regCols-pTab->nCol-1, onError, endOfLoop); sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1); } - /* Compute the content of the next row to insert into a range of - ** registers beginning at regIns. - */ if( !isView ){ if( IsVirtual(pTab) ){ /* The row that the VUpdate opcode will delete: none */ sqlite3VdbeAddOp2(v, OP_Null, 0, regIns); } if( ipkColumn>=0 ){ + /* Compute the new rowid */ if( useTempTable ){ sqlite3VdbeAddOp3(v, OP_Column, srcTab, ipkColumn, regRowid); }else if( pSelect ){ - sqlite3VdbeAddOp2(v, OP_Copy, regFromSelect+ipkColumn, regRowid); + /* Rowid already initialized at tag-20191021-001 */ }else{ - VdbeOp *pOp; - sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regRowid); - pOp = sqlite3VdbeGetOp(v, -1); - if( ALWAYS(pOp) && pOp->opcode==OP_Null && !IsVirtual(pTab) ){ + Expr *pIpk = pList->a[ipkColumn].pExpr; + if( pIpk->op==TK_NULL && !IsVirtual(pTab) ){ + sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc); appendFlag = 1; - pOp->opcode = OP_NewRowid; - pOp->p1 = iDataCur; - pOp->p2 = regRowid; - pOp->p3 = regAutoinc; + }else{ + sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regRowid); } } /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid @@ -940,45 +1541,15 @@ void sqlite3Insert( } autoIncStep(pParse, regAutoinc, regRowid); - /* Compute data for all columns of the new entry, beginning - ** with the first column. - */ - nHidden = 0; - for(i=0; inCol; i++){ - int iRegStore = regRowid+1+i; - if( i==pTab->iPKey ){ - /* The value of the INTEGER PRIMARY KEY column is always a NULL. - ** Whenever this column is read, the rowid will be substituted - ** in its place. Hence, fill this column with a NULL to avoid - ** taking up data space with information that will never be used. - ** As there may be shallow copies of this value, make it a soft-NULL */ - sqlite3VdbeAddOp1(v, OP_SoftNull, iRegStore); - continue; - } - if( pColumn==0 ){ - if( IsHiddenColumn(&pTab->aCol[i]) ){ - j = -1; - nHidden++; - }else{ - j = i - nHidden; - } - }else{ - for(j=0; jnId; j++){ - if( pColumn->a[j].idx==i ) break; - } - } - if( j<0 || nColumn==0 || (pColumn && j>=pColumn->nId) ){ - sqlite3ExprCodeFactorable(pParse, pTab->aCol[i].pDflt, iRegStore); - }else if( useTempTable ){ - sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, iRegStore); - }else if( pSelect ){ - if( regFromSelect!=regData ){ - sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+j, iRegStore); - } - }else{ - sqlite3ExprCode(pParse, pList->a[j].pExpr, iRegStore); - } +#ifndef SQLITE_OMIT_GENERATED_COLUMNS + /* Compute the new value for generated columns after all other + ** columns have already been computed. This must be done after + ** computing the ROWID in case one of the generated columns + ** is derived from the INTEGER PRIMARY KEY. */ + if( pTab->tabFlags & TF_HasGenerated ){ + sqlite3ComputeGeneratedColumns(pParse, regRowid+1, pTab); } +#endif /* Generate code to check constraints and generate index keys and ** do the insertion. @@ -993,25 +1564,46 @@ void sqlite3Insert( }else #endif { - int isReplace; /* Set to true if constraints may cause a replace */ + int isReplace = 0;/* Set to true if constraints may cause a replace */ + int bUseSeek; /* True to use OPFLAG_SEEKRESULT */ sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur, - regIns, 0, ipkColumn>=0, onError, endOfLoop, &isReplace, 0 + regIns, 0, ipkColumn>=0, onError, endOfLoop, &isReplace, 0, pUpsert ); - sqlite3FkCheck(pParse, pTab, 0, regIns, 0, 0); + if( db->flags & SQLITE_ForeignKeys ){ + sqlite3FkCheck(pParse, pTab, 0, regIns, 0, 0); + } + + /* Set the OPFLAG_USESEEKRESULT flag if either (a) there are no REPLACE + ** constraints or (b) there are no triggers and this table is not a + ** parent table in a foreign key constraint. It is safe to set the + ** flag in the second case as if any REPLACE constraint is hit, an + ** OP_Delete or OP_IdxDelete instruction will be executed on each + ** cursor that is disturbed. And these instructions both clear the + ** VdbeCursor.seekResult variable, disabling the OPFLAG_USESEEKRESULT + ** functionality. */ + bUseSeek = (isReplace==0 || !sqlite3VdbeHasSubProgram(v)); sqlite3CompleteInsertion(pParse, pTab, iDataCur, iIdxCur, - regIns, aRegIdx, 0, appendFlag, isReplace==0); + regIns, aRegIdx, 0, appendFlag, bUseSeek + ); } +#ifdef SQLITE_ALLOW_ROWID_IN_VIEW + }else if( pParse->bReturning ){ + /* If there is a RETURNING clause, populate the rowid register with + ** constant value -1, in case one or more of the returned expressions + ** refer to the "rowid" of the view. */ + sqlite3VdbeAddOp2(v, OP_Integer, -1, regRowid); +#endif } /* Update the count of rows that are inserted */ - if( (db->flags & SQLITE_CountRows)!=0 ){ + if( regRowCount ){ sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1); } if( pTrigger ){ /* Code AFTER triggers */ - sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_AFTER, + sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_AFTER, pTab, regData-2-pTab->nCol, onError, endOfLoop); } @@ -1025,18 +1617,21 @@ void sqlite3Insert( sqlite3VdbeAddOp1(v, OP_Close, srcTab); }else if( pSelect ){ sqlite3VdbeGoto(v, addrCont); - sqlite3VdbeJumpHere(v, addrInsTop); - } - - if( !IsVirtual(pTab) && !isView ){ - /* Close all tables opened */ - if( iDataCurpIndex; pIdx; pIdx=pIdx->pNext, idx++){ - sqlite3VdbeAddOp1(v, OP_Close, idx+iIdxCur); +#ifdef SQLITE_DEBUG + /* If we are jumping back to an OP_Yield that is preceded by an + ** OP_ReleaseReg, set the p5 flag on the OP_Goto so that the + ** OP_ReleaseReg will be included in the loop. */ + if( sqlite3VdbeGetOp(v, addrCont-1)->opcode==OP_ReleaseReg ){ + assert( sqlite3VdbeGetOp(v, addrCont)->opcode==OP_Yield ); + sqlite3VdbeChangeP5(v, 1); } +#endif + sqlite3VdbeJumpHere(v, addrInsTop); } +#ifndef SQLITE_OMIT_XFER_OPT insert_end: +#endif /* SQLITE_OMIT_XFER_OPT */ /* Update the sqlite_sequence table by storing the content of the ** maximum rowid counter values recorded while inserting into ** autoincrement tables. @@ -1046,22 +1641,24 @@ void sqlite3Insert( } /* - ** Return the number of rows inserted. If this routine is + ** Return the number of rows inserted. If this routine is ** generating code because of a call to sqlite3NestedParse(), do not ** invoke the callback function. */ - if( (db->flags&SQLITE_CountRows) && !pParse->nested && !pParse->pTriggerTab ){ - sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1); - sqlite3VdbeSetNumCols(v, 1); - sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows inserted", SQLITE_STATIC); + if( regRowCount ){ + sqlite3CodeChangeCount(v, regRowCount, "rows inserted"); } insert_cleanup: sqlite3SrcListDelete(db, pTabList); sqlite3ExprListDelete(db, pList); + sqlite3UpsertDelete(db, pUpsert); sqlite3SelectDelete(db, pSelect); - sqlite3IdListDelete(db, pColumn); - sqlite3DbFree(db, aRegIdx); + if( pColumn ){ + sqlite3IdListDelete(db, pColumn); + sqlite3DbFree(db, aTabColMap); + } + if( aRegIdx ) sqlite3DbNNFreeNN(db, aRegIdx); } /* Make sure "isView" and other macros defined above are undefined. Otherwise @@ -1078,15 +1675,16 @@ void sqlite3Insert( #endif /* -** Meanings of bits in of pWalker->eCode for checkConstraintUnchanged() +** Meanings of bits in of pWalker->eCode for +** sqlite3ExprReferencesUpdatedColumn() */ #define CKCNSTRNT_COLUMN 0x01 /* CHECK constraint uses a changing column */ #define CKCNSTRNT_ROWID 0x02 /* CHECK constraint references the ROWID */ -/* This is the Walker callback from checkConstraintUnchanged(). Set -** bit 0x01 of pWalker->eCode if -** pWalker->eCode to 0 if this expression node references any of the -** columns that are being modifed by an UPDATE statement. +/* This is the Walker callback from sqlite3ExprReferencesUpdatedColumn(). +* Set bit 0x01 of pWalker->eCode if pWalker->eCode to 0 and if this +** expression node references any of the +** columns that are being modified by an UPDATE statement. */ static int checkConstraintExprNode(Walker *pWalker, Expr *pExpr){ if( pExpr->op==TK_COLUMN ){ @@ -1107,12 +1705,21 @@ static int checkConstraintExprNode(Walker *pWalker, Expr *pExpr){ ** only columns that are modified by the UPDATE are those for which ** aiChng[i]>=0, and also the ROWID is modified if chngRowid is true. ** -** Return true if CHECK constraint pExpr does not use any of the +** Return true if CHECK constraint pExpr uses any of the ** changing columns (or the rowid if it is changing). In other words, -** return true if this CHECK constraint can be skipped when validating +** return true if this CHECK constraint must be validated for ** the new row in the UPDATE statement. +** +** 2018-09-15: pExpr might also be an expression for an index-on-expressions. +** The operation of this routine is the same - return true if an only if +** the expression uses one or more of columns identified by the second and +** third arguments. */ -static int checkConstraintUnchanged(Expr *pExpr, int *aiChng, int chngRowid){ +int sqlite3ExprReferencesUpdatedColumn( + Expr *pExpr, /* The expression to be checked */ + int *aiChng, /* aiChng[x]>=0 if column x changed by the UPDATE */ + int chngRowid /* True if UPDATE changes the rowid */ +){ Walker w; memset(&w, 0, sizeof(w)); w.eCode = 0; @@ -1127,9 +1734,73 @@ static int checkConstraintUnchanged(Expr *pExpr, int *aiChng, int chngRowid){ testcase( w.eCode==CKCNSTRNT_COLUMN ); testcase( w.eCode==CKCNSTRNT_ROWID ); testcase( w.eCode==(CKCNSTRNT_ROWID|CKCNSTRNT_COLUMN) ); - return !w.eCode; + return w.eCode!=0; } +/* +** The sqlite3GenerateConstraintChecks() routine usually wants to visit +** the indexes of a table in the order provided in the Table->pIndex list. +** However, sometimes (rarely - when there is an upsert) it wants to visit +** the indexes in a different order. The following data structures accomplish +** this. +** +** The IndexIterator object is used to walk through all of the indexes +** of a table in either Index.pNext order, or in some other order established +** by an array of IndexListTerm objects. +*/ +typedef struct IndexListTerm IndexListTerm; +typedef struct IndexIterator IndexIterator; +struct IndexIterator { + int eType; /* 0 for Index.pNext list. 1 for an array of IndexListTerm */ + int i; /* Index of the current item from the list */ + union { + struct { /* Use this object for eType==0: A Index.pNext list */ + Index *pIdx; /* The current Index */ + } lx; + struct { /* Use this object for eType==1; Array of IndexListTerm */ + int nIdx; /* Size of the array */ + IndexListTerm *aIdx; /* Array of IndexListTerms */ + } ax; + } u; +}; + +/* When IndexIterator.eType==1, then each index is an array of instances +** of the following object +*/ +struct IndexListTerm { + Index *p; /* The index */ + int ix; /* Which entry in the original Table.pIndex list is this index*/ +}; + +/* Return the first index on the list */ +static Index *indexIteratorFirst(IndexIterator *pIter, int *pIx){ + assert( pIter->i==0 ); + if( pIter->eType ){ + *pIx = pIter->u.ax.aIdx[0].ix; + return pIter->u.ax.aIdx[0].p; + }else{ + *pIx = 0; + return pIter->u.lx.pIdx; + } +} + +/* Return the next index from the list. Return NULL when out of indexes */ +static Index *indexIteratorNext(IndexIterator *pIter, int *pIx){ + if( pIter->eType ){ + int i = ++pIter->i; + if( i>=pIter->u.ax.nIdx ){ + *pIx = i; + return 0; + } + *pIx = pIter->u.ax.aIdx[i].ix; + return pIter->u.ax.aIdx[i].p; + }else{ + ++(*pIx); + pIter->u.lx.pIdx = pIter->u.lx.pIdx->pNext; + return pIter->u.lx.pIdx; + } +} + /* ** Generate code to do constraint checks prior to an INSERT or an UPDATE ** on table pTab. @@ -1165,6 +1836,14 @@ static int checkConstraintUnchanged(Expr *pExpr, int *aiChng, int chngRowid){ ** the same as the order of indices on the linked list of indices ** at pTab->pIndex. ** +** (2019-05-07) The generated code also creates a new record for the +** main table, if pTab is a rowid table, and stores that record in the +** register identified by aRegIdx[nIdx] - in other words in the first +** entry of aRegIdx[] past the last index. It is important that the +** record be generated during constraint checks to avoid affinity changes +** to the register content that occur after constraint checks but before +** the new record is inserted. +** ** The caller must have already opened writeable cursors on the main ** table and all applicable indices (that is to say, all indices for which ** aRegIdx[] is not zero). iDataCur is the cursor for the main table when @@ -1225,34 +1904,44 @@ void sqlite3GenerateConstraintChecks( u8 overrideError, /* Override onError to this if not OE_Default */ int ignoreDest, /* Jump to this label on an OE_Ignore resolution */ int *pbMayReplace, /* OUT: Set to true if constraint may cause a replace */ - int *aiChng /* column i is unchanged if aiChng[i]<0 */ + int *aiChng, /* column i is unchanged if aiChng[i]<0 */ + Upsert *pUpsert /* ON CONFLICT clauses, if any. NULL otherwise */ ){ - Vdbe *v; /* VDBE under constrution */ + Vdbe *v; /* VDBE under construction */ Index *pIdx; /* Pointer to one of the indices */ - Index *pPk = 0; /* The PRIMARY KEY index */ + Index *pPk = 0; /* The PRIMARY KEY index for WITHOUT ROWID tables */ sqlite3 *db; /* Database connection */ int i; /* loop counter */ int ix; /* Index loop counter */ int nCol; /* Number of columns */ int onError; /* Conflict resolution strategy */ - int addr1; /* Address of jump instruction */ int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */ int nPkField; /* Number of fields in PRIMARY KEY. 1 for ROWID tables */ - int ipkTop = 0; /* Top of the rowid change constraint check */ - int ipkBottom = 0; /* Bottom of the rowid change constraint check */ - u8 isUpdate; /* True if this is an UPDATE operation */ + Upsert *pUpsertClause = 0; /* The specific ON CONFLICT clause for pIdx */ + u8 isUpdate; /* True if this is an UPDATE operation */ u8 bAffinityDone = 0; /* True if the OP_Affinity operation has been run */ - int regRowid = -1; /* Register holding ROWID value */ + int upsertIpkReturn = 0; /* Address of Goto at end of IPK uniqueness check */ + int upsertIpkDelay = 0; /* Address of Goto to bypass initial IPK check */ + int ipkTop = 0; /* Top of the IPK uniqueness check */ + int ipkBottom = 0; /* OP_Goto at the end of the IPK uniqueness check */ + /* Variables associated with retesting uniqueness constraints after + ** replace triggers fire have run */ + int regTrigCnt; /* Register used to count replace trigger invocations */ + int addrRecheck = 0; /* Jump here to recheck all uniqueness constraints */ + int lblRecheckOk = 0; /* Each recheck jumps to this label if it passes */ + Trigger *pTrigger; /* List of DELETE triggers on the table pTab */ + int nReplaceTrig = 0; /* Number of replace triggers coded */ + IndexIterator sIdxIter; /* Index iterator */ isUpdate = regOldData!=0; db = pParse->db; - v = sqlite3GetVdbe(pParse); + v = pParse->pVdbe; assert( v!=0 ); - assert( pTab->pSelect==0 ); /* This table is not a VIEW */ + assert( !IsView(pTab) ); /* This table is not a VIEW */ nCol = pTab->nCol; - + /* pPk is the PRIMARY KEY index for WITHOUT ROWID tables and NULL for - ** normal rowid tables. nPkField is the number of key fields in the + ** normal rowid tables. nPkField is the number of key fields in the ** pPk index or 1 for a rowid table. In other words, nPkField is the ** number of fields in the true primary key of the table. */ if( HasRowid(pTab) ){ @@ -1269,89 +1958,288 @@ void sqlite3GenerateConstraintChecks( /* Test all NOT NULL constraints. */ - for(i=0; iiPKey ){ - continue; /* ROWID is never NULL */ - } - if( aiChng && aiChng[i]<0 ){ - /* Don't bother checking for NOT NULL on columns that do not change */ - continue; - } - onError = pTab->aCol[i].notNull; - if( onError==OE_None ) continue; /* This column is allowed to be NULL */ - if( overrideError!=OE_Default ){ - onError = overrideError; - }else if( onError==OE_Default ){ - onError = OE_Abort; - } - if( onError==OE_Replace && pTab->aCol[i].pDflt==0 ){ - onError = OE_Abort; - } - assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail - || onError==OE_Ignore || onError==OE_Replace ); - switch( onError ){ - case OE_Abort: - sqlite3MayAbort(pParse); - /* Fall through */ - case OE_Rollback: - case OE_Fail: { - char *zMsg = sqlite3MPrintf(db, "%s.%s", pTab->zName, - pTab->aCol[i].zName); - sqlite3VdbeAddOp4(v, OP_HaltIfNull, SQLITE_CONSTRAINT_NOTNULL, onError, - regNewData+1+i, zMsg, P4_DYNAMIC); - sqlite3VdbeChangeP5(v, P5_ConstraintNotNull); - VdbeCoverage(v); - break; - } - case OE_Ignore: { - sqlite3VdbeAddOp2(v, OP_IsNull, regNewData+1+i, ignoreDest); - VdbeCoverage(v); + if( pTab->tabFlags & TF_HasNotNull ){ + int b2ndPass = 0; /* True if currently running 2nd pass */ + int nSeenReplace = 0; /* Number of ON CONFLICT REPLACE operations */ + int nGenerated = 0; /* Number of generated columns with NOT NULL */ + while(1){ /* Make 2 passes over columns. Exit loop via "break" */ + for(i=0; iaCol[i]; /* The column to check for NOT NULL */ + int isGenerated; /* non-zero if column is generated */ + onError = pCol->notNull; + if( onError==OE_None ) continue; /* No NOT NULL on this column */ + if( i==pTab->iPKey ){ + continue; /* ROWID is never NULL */ + } + isGenerated = pCol->colFlags & COLFLAG_GENERATED; + if( isGenerated && !b2ndPass ){ + nGenerated++; + continue; /* Generated columns processed on 2nd pass */ + } + if( aiChng && aiChng[i]<0 && !isGenerated ){ + /* Do not check NOT NULL on columns that do not change */ + continue; + } + if( overrideError!=OE_Default ){ + onError = overrideError; + }else if( onError==OE_Default ){ + onError = OE_Abort; + } + if( onError==OE_Replace ){ + if( b2ndPass /* REPLACE becomes ABORT on the 2nd pass */ + || pCol->iDflt==0 /* REPLACE is ABORT if no DEFAULT value */ + ){ + testcase( pCol->colFlags & COLFLAG_VIRTUAL ); + testcase( pCol->colFlags & COLFLAG_STORED ); + testcase( pCol->colFlags & COLFLAG_GENERATED ); + onError = OE_Abort; + }else{ + assert( !isGenerated ); + } + }else if( b2ndPass && !isGenerated ){ + continue; + } + assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail + || onError==OE_Ignore || onError==OE_Replace ); + testcase( i!=sqlite3TableColumnToStorage(pTab, i) ); + iReg = sqlite3TableColumnToStorage(pTab, i) + regNewData + 1; + switch( onError ){ + case OE_Replace: { + int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, iReg); + VdbeCoverage(v); + assert( (pCol->colFlags & COLFLAG_GENERATED)==0 ); + nSeenReplace++; + sqlite3ExprCodeCopy(pParse, + sqlite3ColumnExpr(pTab, pCol), iReg); + sqlite3VdbeJumpHere(v, addr1); + break; + } + case OE_Abort: + sqlite3MayAbort(pParse); + /* no break */ deliberate_fall_through + case OE_Rollback: + case OE_Fail: { + char *zMsg = sqlite3MPrintf(db, "%s.%s", pTab->zName, + pCol->zCnName); + testcase( zMsg==0 && db->mallocFailed==0 ); + sqlite3VdbeAddOp3(v, OP_HaltIfNull, SQLITE_CONSTRAINT_NOTNULL, + onError, iReg); + sqlite3VdbeAppendP4(v, zMsg, P4_DYNAMIC); + sqlite3VdbeChangeP5(v, P5_ConstraintNotNull); + VdbeCoverage(v); + break; + } + default: { + assert( onError==OE_Ignore ); + sqlite3VdbeAddOp2(v, OP_IsNull, iReg, ignoreDest); + VdbeCoverage(v); + break; + } + } /* end switch(onError) */ + } /* end loop i over columns */ + if( nGenerated==0 && nSeenReplace==0 ){ + /* If there are no generated columns with NOT NULL constraints + ** and no NOT NULL ON CONFLICT REPLACE constraints, then a single + ** pass is sufficient */ break; } - default: { - assert( onError==OE_Replace ); - addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, regNewData+1+i); - VdbeCoverage(v); - sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regNewData+1+i); - sqlite3VdbeJumpHere(v, addr1); - break; + if( b2ndPass ) break; /* Never need more than 2 passes */ + b2ndPass = 1; +#ifndef SQLITE_OMIT_GENERATED_COLUMNS + if( nSeenReplace>0 && (pTab->tabFlags & TF_HasGenerated)!=0 ){ + /* If any NOT NULL ON CONFLICT REPLACE constraints fired on the + ** first pass, recomputed values for all generated columns, as + ** those values might depend on columns affected by the REPLACE. + */ + sqlite3ComputeGeneratedColumns(pParse, regNewData+1, pTab); } - } - } +#endif + } /* end of 2-pass loop */ + } /* end if( has-not-null-constraints ) */ /* Test all CHECK constraints */ #ifndef SQLITE_OMIT_CHECK if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){ ExprList *pCheck = pTab->pCheck; - pParse->ckBase = regNewData+1; + pParse->iSelfTab = -(regNewData+1); onError = overrideError!=OE_Default ? overrideError : OE_Abort; for(i=0; inExpr; i++){ int allOk; + Expr *pCopy; Expr *pExpr = pCheck->a[i].pExpr; - if( aiChng && checkConstraintUnchanged(pExpr, aiChng, pkChng) ) continue; - allOk = sqlite3VdbeMakeLabel(v); - sqlite3ExprIfTrue(pParse, pExpr, allOk, SQLITE_JUMPIFNULL); + if( aiChng + && !sqlite3ExprReferencesUpdatedColumn(pExpr, aiChng, pkChng) + ){ + /* The check constraints do not reference any of the columns being + ** updated so there is no point it verifying the check constraint */ + continue; + } + if( bAffinityDone==0 ){ + sqlite3TableAffinity(v, pTab, regNewData+1); + bAffinityDone = 1; + } + allOk = sqlite3VdbeMakeLabel(pParse); + sqlite3VdbeVerifyAbortable(v, onError); + pCopy = sqlite3ExprDup(db, pExpr, 0); + if( !db->mallocFailed ){ + sqlite3ExprIfTrue(pParse, pCopy, allOk, SQLITE_JUMPIFNULL); + } + sqlite3ExprDelete(db, pCopy); if( onError==OE_Ignore ){ sqlite3VdbeGoto(v, ignoreDest); }else{ - char *zName = pCheck->a[i].zName; - if( zName==0 ) zName = pTab->zName; - if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-15569-63625 */ + char *zName = pCheck->a[i].zEName; + assert( zName!=0 || pParse->db->mallocFailed ); + if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-26383-51744 */ sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_CHECK, onError, zName, P4_TRANSIENT, P5_ConstraintCheck); } sqlite3VdbeResolveLabel(v, allOk); } + pParse->iSelfTab = 0; } #endif /* !defined(SQLITE_OMIT_CHECK) */ + /* UNIQUE and PRIMARY KEY constraints should be handled in the following + ** order: + ** + ** (1) OE_Update + ** (2) OE_Abort, OE_Fail, OE_Rollback, OE_Ignore + ** (3) OE_Replace + ** + ** OE_Fail and OE_Ignore must happen before any changes are made. + ** OE_Update guarantees that only a single row will change, so it + ** must happen before OE_Replace. Technically, OE_Abort and OE_Rollback + ** could happen in any order, but they are grouped up front for + ** convenience. + ** + ** 2018-08-14: Ticket https://sqlite.org/src/info/908f001483982c43 + ** The order of constraints used to have OE_Update as (2) and OE_Abort + ** and so forth as (1). But apparently PostgreSQL checks the OE_Update + ** constraint before any others, so it had to be moved. + ** + ** Constraint checking code is generated in this order: + ** (A) The rowid constraint + ** (B) Unique index constraints that do not have OE_Replace as their + ** default conflict resolution strategy + ** (C) Unique index that do use OE_Replace by default. + ** + ** The ordering of (2) and (3) is accomplished by making sure the linked + ** list of indexes attached to a table puts all OE_Replace indexes last + ** in the list. See sqlite3CreateIndex() for where that happens. + */ + sIdxIter.eType = 0; + sIdxIter.i = 0; + sIdxIter.u.ax.aIdx = 0; /* Silence harmless compiler warning */ + sIdxIter.u.lx.pIdx = pTab->pIndex; + if( pUpsert ){ + if( pUpsert->pUpsertTarget==0 ){ + /* There is just on ON CONFLICT clause and it has no constraint-target */ + assert( pUpsert->pNextUpsert==0 ); + if( pUpsert->isDoUpdate==0 ){ + /* A single ON CONFLICT DO NOTHING clause, without a constraint-target. + ** Make all unique constraint resolution be OE_Ignore */ + overrideError = OE_Ignore; + pUpsert = 0; + }else{ + /* A single ON CONFLICT DO UPDATE. Make all resolutions OE_Update */ + overrideError = OE_Update; + } + }else if( pTab->pIndex!=0 ){ + /* Otherwise, we'll need to run the IndexListTerm array version of the + ** iterator to ensure that all of the ON CONFLICT conditions are + ** checked first and in order. */ + int nIdx, jj; + u64 nByte; + Upsert *pTerm; + u8 *bUsed; + for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){ + assert( aRegIdx[nIdx]>0 ); + } + sIdxIter.eType = 1; + sIdxIter.u.ax.nIdx = nIdx; + nByte = (sizeof(IndexListTerm)+1)*nIdx + nIdx; + sIdxIter.u.ax.aIdx = sqlite3DbMallocZero(db, nByte); + if( sIdxIter.u.ax.aIdx==0 ) return; /* OOM */ + bUsed = (u8*)&sIdxIter.u.ax.aIdx[nIdx]; + pUpsert->pToFree = sIdxIter.u.ax.aIdx; + for(i=0, pTerm=pUpsert; pTerm; pTerm=pTerm->pNextUpsert){ + if( pTerm->pUpsertTarget==0 ) break; + if( pTerm->pUpsertIdx==0 ) continue; /* Skip ON CONFLICT for the IPK */ + jj = 0; + pIdx = pTab->pIndex; + while( ALWAYS(pIdx!=0) && pIdx!=pTerm->pUpsertIdx ){ + pIdx = pIdx->pNext; + jj++; + } + if( bUsed[jj] ) continue; /* Duplicate ON CONFLICT clause ignored */ + bUsed[jj] = 1; + sIdxIter.u.ax.aIdx[i].p = pIdx; + sIdxIter.u.ax.aIdx[i].ix = jj; + i++; + } + for(jj=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, jj++){ + if( bUsed[jj] ) continue; + sIdxIter.u.ax.aIdx[i].p = pIdx; + sIdxIter.u.ax.aIdx[i].ix = jj; + i++; + } + assert( i==nIdx ); + } + } + + /* Determine if it is possible that triggers (either explicitly coded + ** triggers or FK resolution actions) might run as a result of deletes + ** that happen when OE_Replace conflict resolution occurs. (Call these + ** "replace triggers".) If any replace triggers run, we will need to + ** recheck all of the uniqueness constraints after they have all run. + ** But on the recheck, the resolution is OE_Abort instead of OE_Replace. + ** + ** If replace triggers are a possibility, then + ** + ** (1) Allocate register regTrigCnt and initialize it to zero. + ** That register will count the number of replace triggers that + ** fire. Constraint recheck only occurs if the number is positive. + ** (2) Initialize pTrigger to the list of all DELETE triggers on pTab. + ** (3) Initialize addrRecheck and lblRecheckOk + ** + ** The uniqueness rechecking code will create a series of tests to run + ** in a second pass. The addrRecheck and lblRecheckOk variables are + ** used to link together these tests which are separated from each other + ** in the generate bytecode. + */ + if( (db->flags & (SQLITE_RecTriggers|SQLITE_ForeignKeys))==0 ){ + /* There are not DELETE triggers nor FK constraints. No constraint + ** rechecks are needed. */ + pTrigger = 0; + regTrigCnt = 0; + }else{ + if( db->flags&SQLITE_RecTriggers ){ + pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0); + regTrigCnt = pTrigger!=0 || sqlite3FkRequired(pParse, pTab, 0, 0); + }else{ + pTrigger = 0; + regTrigCnt = sqlite3FkRequired(pParse, pTab, 0, 0); + } + if( regTrigCnt ){ + /* Replace triggers might exist. Allocate the counter and + ** initialize it to zero. */ + regTrigCnt = ++pParse->nMem; + sqlite3VdbeAddOp2(v, OP_Integer, 0, regTrigCnt); + VdbeComment((v, "trigger count")); + lblRecheckOk = sqlite3VdbeMakeLabel(pParse); + addrRecheck = lblRecheckOk; + } + } + /* If rowid is changing, make sure the new rowid does not previously ** exist in the table. */ if( pkChng && pPk==0 ){ - int addrRowidOk = sqlite3VdbeMakeLabel(v); + int addrRowidOk = sqlite3VdbeMakeLabel(pParse); /* Figure out what action to take in case of a rowid collision */ onError = pTab->keyConf; @@ -1361,13 +2249,22 @@ void sqlite3GenerateConstraintChecks( onError = OE_Abort; } - if( isUpdate ){ - /* pkChng!=0 does not mean that the rowid has change, only that - ** it might have changed. Skip the conflict logic below if the rowid - ** is unchanged. */ - sqlite3VdbeAddOp3(v, OP_Eq, regNewData, addrRowidOk, regOldData); - sqlite3VdbeChangeP5(v, SQLITE_NOTNULL); - VdbeCoverage(v); + /* figure out whether or not upsert applies in this case */ + if( pUpsert ){ + pUpsertClause = sqlite3UpsertOfIndex(pUpsert,0); + if( pUpsertClause!=0 ){ + if( pUpsertClause->isDoUpdate==0 ){ + onError = OE_Ignore; /* DO NOTHING is the same as INSERT OR IGNORE */ + }else{ + onError = OE_Update; /* DO UPDATE */ + } + } + if( pUpsertClause!=pUpsert ){ + /* The first ON CONFLICT clause has a conflict target other than + ** the IPK. We have to jump ahead to that first ON CONFLICT clause + ** and then come back here and deal with the IPK afterwards */ + upsertIpkDelay = sqlite3VdbeAddOp0(v, OP_Goto); + } } /* If the response to a rowid conflict is REPLACE but the response @@ -1375,29 +2272,42 @@ void sqlite3GenerateConstraintChecks( ** to defer the running of the rowid conflict checking until after ** the UNIQUE constraints have run. */ - if( onError==OE_Replace && overrideError!=OE_Replace ){ - for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ - if( pIdx->onError==OE_Ignore || pIdx->onError==OE_Fail ){ - ipkTop = sqlite3VdbeAddOp0(v, OP_Goto); - break; - } - } + if( onError==OE_Replace /* IPK rule is REPLACE */ + && onError!=overrideError /* Rules for other constraints are different */ + && pTab->pIndex /* There exist other constraints */ + && !upsertIpkDelay /* IPK check already deferred by UPSERT */ + ){ + ipkTop = sqlite3VdbeAddOp0(v, OP_Goto)+1; + VdbeComment((v, "defer IPK REPLACE until last")); + } + + if( isUpdate ){ + /* pkChng!=0 does not mean that the rowid has changed, only that + ** it might have changed. Skip the conflict logic below if the rowid + ** is unchanged. */ + sqlite3VdbeAddOp3(v, OP_Eq, regNewData, addrRowidOk, regOldData); + sqlite3VdbeChangeP5(v, SQLITE_NOTNULL); + VdbeCoverage(v); } /* Check to see if the new rowid already exists in the table. Skip ** the following conflict logic if it does not. */ + VdbeNoopComment((v, "uniqueness check for ROWID")); + sqlite3VdbeVerifyAbortable(v, onError); sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, addrRowidOk, regNewData); VdbeCoverage(v); - /* Generate code that deals with a rowid collision */ switch( onError ){ default: { onError = OE_Abort; - /* Fall thru into the next case */ + /* no break */ deliberate_fall_through } case OE_Rollback: case OE_Abort: case OE_Fail: { + testcase( onError==OE_Rollback ); + testcase( onError==OE_Abort ); + testcase( onError==OE_Fail ); sqlite3RowidConstraint(pParse, onError, pTab); break; } @@ -1408,10 +2318,10 @@ void sqlite3GenerateConstraintChecks( ** the triggers and remove both the table and index b-tree entries. ** ** Otherwise, if there are no triggers or the recursive-triggers - ** flag is not set, but the table has one or more indexes, call - ** GenerateRowIndexDelete(). This removes the index b-tree entries - ** only. The table b-tree entry will be replaced by the new entry - ** when it is inserted. + ** flag is not set, but the table has one or more indexes, call + ** GenerateRowIndexDelete(). This removes the index b-tree entries + ** only. The table b-tree entry will be replaced by the new entry + ** when it is inserted. ** ** If either GenerateRowDelete() or GenerateRowIndexDelete() is called, ** also invoke MultiWrite() to indicate that this VDBE may require @@ -1424,16 +2334,22 @@ void sqlite3GenerateConstraintChecks( ** to run without a statement journal if there are no indexes on the ** table. */ - Trigger *pTrigger = 0; - if( db->flags&SQLITE_RecTriggers ){ - pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0); - } - if( pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0) ){ + if( regTrigCnt ){ sqlite3MultiWrite(pParse); sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur, - regNewData, 1, 0, OE_Replace, - ONEPASS_SINGLE, -1); + regNewData, 1, 0, OE_Replace, 1, -1); + sqlite3VdbeAddOp2(v, OP_AddImm, regTrigCnt, 1); /* incr trigger cnt */ + nReplaceTrig++; }else{ +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK + assert( HasRowid(pTab) ); + /* This OP_Delete opcode fires the pre-update-hook only. It does + ** not modify the b-tree. It is more efficient to let the coming + ** OP_Insert replace the existing entry than it is to delete the + ** existing entry and then insert a new one. */ + sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, OPFLAG_ISNOOP); + sqlite3VdbeAppendP4(v, pTab, P4_TABLE); +#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ if( pTab->pIndex ){ sqlite3MultiWrite(pParse); sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,-1); @@ -1442,16 +2358,24 @@ void sqlite3GenerateConstraintChecks( seenReplace = 1; break; } +#ifndef SQLITE_OMIT_UPSERT + case OE_Update: { + sqlite3UpsertDoUpdate(pParse, pUpsert, pTab, 0, iDataCur); + /* no break */ deliberate_fall_through + } +#endif case OE_Ignore: { - /*assert( seenReplace==0 );*/ + testcase( onError==OE_Ignore ); sqlite3VdbeGoto(v, ignoreDest); break; } } sqlite3VdbeResolveLabel(v, addrRowidOk); - if( ipkTop ){ + if( pUpsert && pUpsertClause!=pUpsert ){ + upsertIpkReturn = sqlite3VdbeAddOp0(v, OP_Goto); + }else if( ipkTop ){ ipkBottom = sqlite3VdbeAddOp0(v, OP_Goto); - sqlite3VdbeJumpHere(v, ipkTop); + sqlite3VdbeJumpHere(v, ipkTop-1); } } @@ -1462,58 +2386,74 @@ void sqlite3GenerateConstraintChecks( ** This loop also handles the case of the PRIMARY KEY index for a ** WITHOUT ROWID table. */ - for(ix=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, ix++){ - int regIdx; /* Range of registers hold conent for pIdx */ + for(pIdx = indexIteratorFirst(&sIdxIter, &ix); + pIdx; + pIdx = indexIteratorNext(&sIdxIter, &ix) + ){ + int regIdx; /* Range of registers holding content for pIdx */ int regR; /* Range of registers holding conflicting PK */ int iThisCur; /* Cursor for this UNIQUE index */ int addrUniqueOk; /* Jump here if the UNIQUE constraint is satisfied */ + int addrConflictCk; /* First opcode in the conflict check logic */ if( aRegIdx[ix]==0 ) continue; /* Skip indices that do not change */ + if( pUpsert ){ + pUpsertClause = sqlite3UpsertOfIndex(pUpsert, pIdx); + if( upsertIpkDelay && pUpsertClause==pUpsert ){ + sqlite3VdbeJumpHere(v, upsertIpkDelay); + } + } + addrUniqueOk = sqlite3VdbeMakeLabel(pParse); if( bAffinityDone==0 ){ sqlite3TableAffinity(v, pTab, regNewData+1); bAffinityDone = 1; } + VdbeNoopComment((v, "prep index %s", pIdx->zName)); iThisCur = iIdxCur+ix; - addrUniqueOk = sqlite3VdbeMakeLabel(v); + /* Skip partial indices for which the WHERE clause is not true */ if( pIdx->pPartIdxWhere ){ sqlite3VdbeAddOp2(v, OP_Null, 0, aRegIdx[ix]); - pParse->ckBase = regNewData+1; + pParse->iSelfTab = -(regNewData+1); sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, addrUniqueOk, SQLITE_JUMPIFNULL); - pParse->ckBase = 0; + pParse->iSelfTab = 0; } /* Create a record for this index entry as it should appear after ** the insert or update. Store that record in the aRegIdx[ix] register */ - regIdx = sqlite3GetTempRange(pParse, pIdx->nColumn); + regIdx = aRegIdx[ix]+1; for(i=0; inColumn; i++){ int iField = pIdx->aiColumn[i]; int x; if( iField==XN_EXPR ){ - pParse->ckBase = regNewData+1; + pParse->iSelfTab = -(regNewData+1); sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[i].pExpr, regIdx+i); - pParse->ckBase = 0; + pParse->iSelfTab = 0; VdbeComment((v, "%s column %d", pIdx->zName, i)); + }else if( iField==XN_ROWID || iField==pTab->iPKey ){ + x = regNewData; + sqlite3VdbeAddOp2(v, OP_IntCopy, x, regIdx+i); + VdbeComment((v, "rowid")); }else{ - if( iField==XN_ROWID || iField==pTab->iPKey ){ - if( regRowid==regIdx+i ) continue; /* ROWID already in regIdx+i */ - x = regNewData; - regRowid = pIdx->pPartIdxWhere ? -1 : regIdx+i; - }else{ - x = iField + regNewData + 1; - } - sqlite3VdbeAddOp2(v, iField<0 ? OP_IntCopy : OP_SCopy, x, regIdx+i); - VdbeComment((v, "%s", iField<0 ? "rowid" : pTab->aCol[iField].zName)); + testcase( sqlite3TableColumnToStorage(pTab, iField)!=iField ); + x = sqlite3TableColumnToStorage(pTab, iField) + regNewData + 1; + sqlite3VdbeAddOp2(v, OP_SCopy, x, regIdx+i); + VdbeComment((v, "%s", pTab->aCol[iField].zCnName)); } } sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn, aRegIdx[ix]); VdbeComment((v, "for %s", pIdx->zName)); - sqlite3ExprCacheAffinityChange(pParse, regIdx, pIdx->nColumn); +#ifdef SQLITE_ENABLE_NULL_TRIM + if( pIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){ + sqlite3SetMakeRecordP5(v, pIdx->pTable); + } +#endif + sqlite3VdbeReleaseRegisters(pParse, regIdx, pIdx->nColumn, 0, 0); - /* In an UPDATE operation, if this index is the PRIMARY KEY index + /* In an UPDATE operation, if this index is the PRIMARY KEY index ** of a WITHOUT ROWID table and there has been no change the ** primary key, then no collision is possible. The collision detection ** logic below can all be skipped. */ @@ -1524,8 +2464,7 @@ void sqlite3GenerateConstraintChecks( /* Find out what action to take in case there is a uniqueness conflict */ onError = pIdx->onError; - if( onError==OE_None ){ - sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn); + if( onError==OE_None ){ sqlite3VdbeResolveLabel(v, addrUniqueOk); continue; /* pIdx is not a UNIQUE index */ } @@ -1534,13 +2473,49 @@ void sqlite3GenerateConstraintChecks( }else if( onError==OE_Default ){ onError = OE_Abort; } - + + /* Figure out if the upsert clause applies to this index */ + if( pUpsertClause ){ + if( pUpsertClause->isDoUpdate==0 ){ + onError = OE_Ignore; /* DO NOTHING is the same as INSERT OR IGNORE */ + }else{ + onError = OE_Update; /* DO UPDATE */ + } + } + + /* Collision detection may be omitted if all of the following are true: + ** (1) The conflict resolution algorithm is REPLACE + ** (2) The table is a WITHOUT ROWID table + ** (3) There are no secondary indexes on the table + ** (4) No delete triggers need to be fired if there is a conflict + ** (5) No FK constraint counters need to be updated if a conflict occurs. + ** + ** This is not possible for ENABLE_PREUPDATE_HOOK builds, as the row + ** must be explicitly deleted in order to ensure any pre-update hook + ** is invoked. */ + assert( IsOrdinaryTable(pTab) ); +#ifndef SQLITE_ENABLE_PREUPDATE_HOOK + if( (ix==0 && pIdx->pNext==0) /* Condition 3 */ + && pPk==pIdx /* Condition 2 */ + && onError==OE_Replace /* Condition 1 */ + && ( 0==(db->flags&SQLITE_RecTriggers) || /* Condition 4 */ + 0==sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0)) + && ( 0==(db->flags&SQLITE_ForeignKeys) || /* Condition 5 */ + (0==pTab->u.tab.pFKey && 0==sqlite3FkReferences(pTab))) + ){ + sqlite3VdbeResolveLabel(v, addrUniqueOk); + continue; + } +#endif /* ifndef SQLITE_ENABLE_PREUPDATE_HOOK */ + /* Check to see if the new index entry will be unique */ - sqlite3VdbeAddOp4Int(v, OP_NoConflict, iThisCur, addrUniqueOk, - regIdx, pIdx->nKeyCol); VdbeCoverage(v); + sqlite3VdbeVerifyAbortable(v, onError); + addrConflictCk = + sqlite3VdbeAddOp4Int(v, OP_NoConflict, iThisCur, addrUniqueOk, + regIdx, pIdx->nKeyCol); VdbeCoverage(v); /* Generate code to handle collisions */ - regR = (pIdx==pPk) ? regIdx : sqlite3GetTempRange(pParse, nPkField); + regR = pIdx==pPk ? regIdx : sqlite3GetTempRange(pParse, nPkField); if( isUpdate || onError==OE_Replace ){ if( HasRowid(pTab) ){ sqlite3VdbeAddOp2(v, OP_IdxRowid, iThisCur, regR); @@ -1558,16 +2533,16 @@ void sqlite3GenerateConstraintChecks( if( pIdx!=pPk ){ for(i=0; inKeyCol; i++){ assert( pPk->aiColumn[i]>=0 ); - x = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[i]); + x = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[i]); sqlite3VdbeAddOp3(v, OP_Column, iThisCur, x, regR+i); VdbeComment((v, "%s.%s", pTab->zName, - pTab->aCol[pPk->aiColumn[i]].zName)); + pTab->aCol[pPk->aiColumn[i]].zCnName)); } } if( isUpdate ){ - /* If currently processing the PRIMARY KEY of a WITHOUT ROWID + /* If currently processing the PRIMARY KEY of a WITHOUT ROWID ** table, only conflict if the new PRIMARY KEY values are actually - ** different from the old. + ** different from the old. See TH3 withoutrowid04.test. ** ** For a UNIQUE index, only conflict if the PRIMARY KEY values ** of the matched index row are different from the original PRIMARY @@ -1575,7 +2550,7 @@ void sqlite3GenerateConstraintChecks( int addrJump = sqlite3VdbeCurrentAddr(v)+pPk->nKeyCol; int op = OP_Ne; int regCmp = (IsPrimaryKeyIndex(pIdx) ? regIdx : regR); - + for(i=0; inKeyCol; i++){ char *p4 = (char*)sqlite3LocateCollSeq(pParse, pPk->azColl[i]); x = pPk->aiColumn[i]; @@ -1584,7 +2559,8 @@ void sqlite3GenerateConstraintChecks( addrJump = addrUniqueOk; op = OP_Eq; } - sqlite3VdbeAddOp4(v, op, + x = sqlite3TableColumnToStorage(pTab, x); + sqlite3VdbeAddOp4(v, op, regOldData+1+x, addrJump, regCmp+i, p4, P4_COLLSEQ ); sqlite3VdbeChangeP5(v, SQLITE_NOTNULL); @@ -1597,45 +2573,203 @@ void sqlite3GenerateConstraintChecks( /* Generate code that executes if the new index entry is not unique */ assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail - || onError==OE_Ignore || onError==OE_Replace ); + || onError==OE_Ignore || onError==OE_Replace || onError==OE_Update ); switch( onError ){ case OE_Rollback: case OE_Abort: case OE_Fail: { + testcase( onError==OE_Rollback ); + testcase( onError==OE_Abort ); + testcase( onError==OE_Fail ); sqlite3UniqueConstraint(pParse, onError, pIdx); break; } +#ifndef SQLITE_OMIT_UPSERT + case OE_Update: { + sqlite3UpsertDoUpdate(pParse, pUpsert, pTab, pIdx, iIdxCur+ix); + /* no break */ deliberate_fall_through + } +#endif case OE_Ignore: { + testcase( onError==OE_Ignore ); sqlite3VdbeGoto(v, ignoreDest); break; } default: { - Trigger *pTrigger = 0; + int nConflictCk; /* Number of opcodes in conflict check logic */ + assert( onError==OE_Replace ); - sqlite3MultiWrite(pParse); - if( db->flags&SQLITE_RecTriggers ){ - pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0); + nConflictCk = sqlite3VdbeCurrentAddr(v) - addrConflictCk; + assert( nConflictCk>0 || db->mallocFailed ); + testcase( nConflictCk<=0 ); + testcase( nConflictCk>1 ); + if( regTrigCnt ){ + sqlite3MultiWrite(pParse); + nReplaceTrig++; + } + if( pTrigger && isUpdate ){ + sqlite3VdbeAddOp1(v, OP_CursorLock, iDataCur); } sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur, regR, nPkField, 0, OE_Replace, - (pIdx==pPk ? ONEPASS_SINGLE : ONEPASS_OFF), -1); + (pIdx==pPk ? ONEPASS_SINGLE : ONEPASS_OFF), iThisCur); + if( pTrigger && isUpdate ){ + sqlite3VdbeAddOp1(v, OP_CursorUnlock, iDataCur); + } + if( regTrigCnt ){ + int addrBypass; /* Jump destination to bypass recheck logic */ + + sqlite3VdbeAddOp2(v, OP_AddImm, regTrigCnt, 1); /* incr trigger cnt */ + addrBypass = sqlite3VdbeAddOp0(v, OP_Goto); /* Bypass recheck */ + VdbeComment((v, "bypass recheck")); + + /* Here we insert code that will be invoked after all constraint + ** checks have run, if and only if one or more replace triggers + ** fired. */ + sqlite3VdbeResolveLabel(v, lblRecheckOk); + lblRecheckOk = sqlite3VdbeMakeLabel(pParse); + if( pIdx->pPartIdxWhere ){ + /* Bypass the recheck if this partial index is not defined + ** for the current row */ + sqlite3VdbeAddOp2(v, OP_IsNull, regIdx-1, lblRecheckOk); + VdbeCoverage(v); + } + /* Copy the constraint check code from above, except change + ** the constraint-ok jump destination to be the address of + ** the next retest block */ + while( nConflictCk>0 ){ + VdbeOp x; /* Conflict check opcode to copy */ + /* The sqlite3VdbeAddOp4() call might reallocate the opcode array. + ** Hence, make a complete copy of the opcode, rather than using + ** a pointer to the opcode. */ + x = *sqlite3VdbeGetOp(v, addrConflictCk); + if( x.opcode!=OP_IdxRowid ){ + int p2; /* New P2 value for copied conflict check opcode */ + const char *zP4; + if( sqlite3OpcodeProperty[x.opcode]&OPFLG_JUMP ){ + p2 = lblRecheckOk; + }else{ + p2 = x.p2; + } + zP4 = x.p4type==P4_INT32 ? SQLITE_INT_TO_PTR(x.p4.i) : x.p4.z; + sqlite3VdbeAddOp4(v, x.opcode, x.p1, p2, x.p3, zP4, x.p4type); + sqlite3VdbeChangeP5(v, x.p5); + VdbeCoverageIf(v, p2!=x.p2); + } + nConflictCk--; + addrConflictCk++; + } + /* If the retest fails, issue an abort */ + sqlite3UniqueConstraint(pParse, OE_Abort, pIdx); + + sqlite3VdbeJumpHere(v, addrBypass); /* Terminate the recheck bypass */ + } seenReplace = 1; break; } } sqlite3VdbeResolveLabel(v, addrUniqueOk); - sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn); if( regR!=regIdx ) sqlite3ReleaseTempRange(pParse, regR, nPkField); + if( pUpsertClause + && upsertIpkReturn + && sqlite3UpsertNextIsIPK(pUpsertClause) + ){ + sqlite3VdbeGoto(v, upsertIpkDelay+1); + sqlite3VdbeJumpHere(v, upsertIpkReturn); + upsertIpkReturn = 0; + } } + + /* If the IPK constraint is a REPLACE, run it last */ if( ipkTop ){ - sqlite3VdbeGoto(v, ipkTop+1); + sqlite3VdbeGoto(v, ipkTop); + VdbeComment((v, "Do IPK REPLACE")); + assert( ipkBottom>0 ); sqlite3VdbeJumpHere(v, ipkBottom); } - + + /* Recheck all uniqueness constraints after replace triggers have run */ + testcase( regTrigCnt!=0 && nReplaceTrig==0 ); + assert( regTrigCnt!=0 || nReplaceTrig==0 ); + if( nReplaceTrig ){ + sqlite3VdbeAddOp2(v, OP_IfNot, regTrigCnt, lblRecheckOk);VdbeCoverage(v); + if( !pPk ){ + if( isUpdate ){ + sqlite3VdbeAddOp3(v, OP_Eq, regNewData, addrRecheck, regOldData); + sqlite3VdbeChangeP5(v, SQLITE_NOTNULL); + VdbeCoverage(v); + } + sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, addrRecheck, regNewData); + VdbeCoverage(v); + sqlite3RowidConstraint(pParse, OE_Abort, pTab); + }else{ + sqlite3VdbeGoto(v, addrRecheck); + } + sqlite3VdbeResolveLabel(v, lblRecheckOk); + } + + /* Generate the table record */ + if( HasRowid(pTab) ){ + int regRec = aRegIdx[ix]; + sqlite3VdbeAddOp3(v, OP_MakeRecord, regNewData+1, pTab->nNVCol, regRec); + sqlite3SetMakeRecordP5(v, pTab); + if( !bAffinityDone ){ + sqlite3TableAffinity(v, pTab, 0); + } + } + *pbMayReplace = seenReplace; VdbeModuleComment((v, "END: GenCnstCks(%d)", seenReplace)); } +#ifdef SQLITE_ENABLE_NULL_TRIM +/* +** Change the P5 operand on the last opcode (which should be an OP_MakeRecord) +** to be the number of columns in table pTab that must not be NULL-trimmed. +** +** Or if no columns of pTab may be NULL-trimmed, leave P5 at zero. +*/ +void sqlite3SetMakeRecordP5(Vdbe *v, Table *pTab){ + u16 i; + + /* Records with omitted columns are only allowed for schema format + ** version 2 and later (SQLite version 3.1.4, 2005-02-20). */ + if( pTab->pSchema->file_format<2 ) return; + + for(i=pTab->nCol-1; i>0; i--){ + if( pTab->aCol[i].iDflt!=0 ) break; + if( pTab->aCol[i].colFlags & COLFLAG_PRIMKEY ) break; + } + sqlite3VdbeChangeP5(v, i+1); +} +#endif + +/* +** Table pTab is a WITHOUT ROWID table that is being written to. The cursor +** number is iCur, and register regData contains the new record for the +** PK index. This function adds code to invoke the pre-update hook, +** if one is registered. +*/ +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK +static void codeWithoutRowidPreupdate( + Parse *pParse, /* Parse context */ + Table *pTab, /* Table being updated */ + int iCur, /* Cursor number for table */ + int regData /* Data containing new record */ +){ + Vdbe *v = pParse->pVdbe; + int r = sqlite3GetTempReg(pParse); + assert( !HasRowid(pTab) ); + assert( 0==(pParse->db->mDbFlags & DBFLAG_Vacuum) || CORRUPT_DB ); + sqlite3VdbeAddOp2(v, OP_Integer, 0, r); + sqlite3VdbeAddOp4(v, OP_Insert, iCur, regData, r, (char*)pTab, P4_TABLE); + sqlite3VdbeChangeP5(v, OPFLAG_ISNOOP); + sqlite3ReleaseTempReg(pParse, r); +} +#else +# define codeWithoutRowidPreupdate(a,b,c,d) +#endif + /* ** This routine generates code to finish the INSERT or UPDATE operation ** that was started by a prior call to sqlite3GenerateConstraintChecks. @@ -1652,48 +2786,52 @@ void sqlite3CompleteInsertion( int iIdxCur, /* First index cursor */ int regNewData, /* Range of content */ int *aRegIdx, /* Register used by each index. 0 for unused indices */ - int isUpdate, /* True for UPDATE, False for INSERT */ + int update_flags, /* True for UPDATE, False for INSERT */ int appendBias, /* True if this is likely to be an append */ int useSeekResult /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */ ){ Vdbe *v; /* Prepared statements under construction */ Index *pIdx; /* An index being inserted or updated */ u8 pik_flags; /* flag values passed to the btree insert */ - int regData; /* Content registers (after the rowid) */ - int regRec; /* Register holding assembled record for the table */ int i; /* Loop counter */ - u8 bAffinityDone = 0; /* True if OP_Affinity has been run already */ - v = sqlite3GetVdbe(pParse); + assert( update_flags==0 + || update_flags==OPFLAG_ISUPDATE + || update_flags==(OPFLAG_ISUPDATE|OPFLAG_SAVEPOSITION) + ); + + v = pParse->pVdbe; assert( v!=0 ); - assert( pTab->pSelect==0 ); /* This table is not a VIEW */ + assert( !IsView(pTab) ); /* This table is not a VIEW */ for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ + /* All REPLACE indexes are at the end of the list */ + assert( pIdx->onError!=OE_Replace + || pIdx->pNext==0 + || pIdx->pNext->onError==OE_Replace ); if( aRegIdx[i]==0 ) continue; - bAffinityDone = 1; if( pIdx->pPartIdxWhere ){ sqlite3VdbeAddOp2(v, OP_IsNull, aRegIdx[i], sqlite3VdbeCurrentAddr(v)+2); VdbeCoverage(v); } - sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdxCur+i, aRegIdx[i]); - pik_flags = 0; - if( useSeekResult ) pik_flags = OPFLAG_USESEEKRESULT; + pik_flags = (useSeekResult ? OPFLAG_USESEEKRESULT : 0); if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){ - assert( pParse->nested==0 ); pik_flags |= OPFLAG_NCHANGE; + pik_flags |= (update_flags & OPFLAG_SAVEPOSITION); + if( update_flags==0 ){ + codeWithoutRowidPreupdate(pParse, pTab, iIdxCur+i, aRegIdx[i]); + } } + sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iIdxCur+i, aRegIdx[i], + aRegIdx[i]+1, + pIdx->uniqNotNull ? pIdx->nKeyCol: pIdx->nColumn); sqlite3VdbeChangeP5(v, pik_flags); } if( !HasRowid(pTab) ) return; - regData = regNewData + 1; - regRec = sqlite3GetTempReg(pParse); - sqlite3VdbeAddOp3(v, OP_MakeRecord, regData, pTab->nCol, regRec); - if( !bAffinityDone ) sqlite3TableAffinity(v, pTab, 0); - sqlite3ExprCacheAffinityChange(pParse, regData, pTab->nCol); if( pParse->nested ){ pik_flags = 0; }else{ pik_flags = OPFLAG_NCHANGE; - pik_flags |= (isUpdate?OPFLAG_ISUPDATE:OPFLAG_LASTROWID); + pik_flags |= (update_flags?update_flags:OPFLAG_LASTROWID); } if( appendBias ){ pik_flags |= OPFLAG_APPEND; @@ -1701,9 +2839,9 @@ void sqlite3CompleteInsertion( if( useSeekResult ){ pik_flags |= OPFLAG_USESEEKRESULT; } - sqlite3VdbeAddOp3(v, OP_Insert, iDataCur, regRec, regNewData); + sqlite3VdbeAddOp3(v, OP_Insert, iDataCur, aRegIdx[i], regNewData); if( !pParse->nested ){ - sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_TRANSIENT); + sqlite3VdbeAppendP4(v, pTab, P4_TABLE); } sqlite3VdbeChangeP5(v, pik_flags); } @@ -1747,36 +2885,39 @@ int sqlite3OpenTableAndIndices( assert( op==OP_OpenRead || op==OP_OpenWrite ); assert( op==OP_OpenWrite || p5==0 ); + assert( piDataCur!=0 ); + assert( piIdxCur!=0 ); if( IsVirtual(pTab) ){ /* This routine is a no-op for virtual tables. Leave the output - ** variables *piDataCur and *piIdxCur uninitialized so that valgrind - ** can detect if they are used by mistake in the caller. */ + ** variables *piDataCur and *piIdxCur set to illegal cursor numbers + ** for improved error detection. */ + *piDataCur = *piIdxCur = -999; return 0; } iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); - v = sqlite3GetVdbe(pParse); + v = pParse->pVdbe; assert( v!=0 ); if( iBase<0 ) iBase = pParse->nTab; iDataCur = iBase++; - if( piDataCur ) *piDataCur = iDataCur; + *piDataCur = iDataCur; if( HasRowid(pTab) && (aToOpen==0 || aToOpen[0]) ){ sqlite3OpenTable(pParse, iDataCur, iDb, pTab, op); - }else{ + }else if( pParse->db->noSharedCache==0 ){ sqlite3TableLock(pParse, iDb, pTab->tnum, op==OP_OpenWrite, pTab->zName); } - if( piIdxCur ) *piIdxCur = iBase; + *piIdxCur = iBase; for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ int iIdxCur = iBase++; assert( pIdx->pSchema==pTab->pSchema ); + if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){ + *piDataCur = iIdxCur; + p5 = 0; + } if( aToOpen==0 || aToOpen[i+1] ){ sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb); sqlite3VdbeSetP4KeyInfo(pParse, pIdx); - VdbeComment((v, "%s", pIdx->zName)); - } - if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){ - if( piDataCur ) *piDataCur = iIdxCur; - }else{ sqlite3VdbeChangeP5(v, p5); + VdbeComment((v, "%s", pIdx->zName)); } } if( iBase>pParse->nTab ) pParse->nTab = iBase; @@ -1811,7 +2952,7 @@ static int xferCompatibleIndex(Index *pDest, Index *pSrc){ int i; assert( pDest && pSrc ); assert( pDest->pTable!=pSrc->pTable ); - if( pDest->nKeyCol!=pSrc->nKeyCol ){ + if( pDest->nKeyCol!=pSrc->nKeyCol || pDest->nColumn!=pSrc->nColumn ){ return 0; /* Different number of columns */ } if( pDest->onError!=pSrc->onError ){ @@ -1823,7 +2964,7 @@ static int xferCompatibleIndex(Index *pDest, Index *pSrc){ } if( pSrc->aiColumn[i]==XN_EXPR ){ assert( pSrc->aColExpr!=0 && pDest->aColExpr!=0 ); - if( sqlite3ExprCompare(pSrc->aColExpr->a[i].pExpr, + if( sqlite3ExprCompare(0, pSrc->aColExpr->a[i].pExpr, pDest->aColExpr->a[i].pExpr, -1)!=0 ){ return 0; /* Different expressions in the index */ } @@ -1835,7 +2976,7 @@ static int xferCompatibleIndex(Index *pDest, Index *pSrc){ return 0; /* Different collating sequences */ } } - if( sqlite3ExprCompare(pSrc->pPartIdxWhere, pDest->pPartIdxWhere, -1) ){ + if( sqlite3ExprCompare(0, pSrc->pPartIdxWhere, pDest->pPartIdxWhere, -1) ){ return 0; /* Different WHERE clauses */ } @@ -1848,7 +2989,7 @@ static int xferCompatibleIndex(Index *pDest, Index *pSrc){ ** ** INSERT INTO tab1 SELECT * FROM tab2; ** -** The xfer optimization transfers raw records from tab2 over to tab1. +** The xfer optimization transfers raw records from tab2 over to tab1. ** Columns are not decoded and reassembled, which greatly improves ** performance. Raw index records are transferred in the same way. ** @@ -1879,7 +3020,7 @@ static int xferOptimization( ExprList *pEList; /* The result set of the SELECT */ Table *pSrc; /* The table in the FROM clause of SELECT */ Index *pSrcIdx, *pDestIdx; /* Source and destination indices */ - struct SrcList_item *pItem; /* An element of pSelect->pSrc */ + SrcItem *pItem; /* An element of pSelect->pSrc */ int i; /* Loop counter */ int iDbSrc; /* The database of pSrc */ int iSrc, iDest; /* Cursors from source and destination */ @@ -1891,20 +3032,15 @@ static int xferOptimization( int destHasUniqueIdx = 0; /* True if pDest has a UNIQUE index */ int regData, regRowid; /* Registers holding data and rowid */ - if( pSelect==0 ){ - return 0; /* Must be of the form INSERT INTO ... SELECT ... */ - } + assert( pSelect!=0 ); if( pParse->pWith || pSelect->pWith ){ /* Do not attempt to process this query if there are an WITH clauses ** attached to it. Proceeding may generate a false "no such table: xxx" ** error if pSelect reads from a CTE named "xxx". */ return 0; } - if( sqlite3TriggerList(pParse, pDest) ){ - return 0; /* tab1 must not have triggers */ - } #ifndef SQLITE_OMIT_VIRTUALTABLE - if( pDest->tabFlags & TF_Virtual ){ + if( IsVirtual(pDest) ){ return 0; /* tab1 must not be a virtual table */ } #endif @@ -1916,7 +3052,7 @@ static int xferOptimization( if( pSelect->pSrc->nSrc!=1 ){ return 0; /* FROM clause must have exactly one term */ } - if( pSelect->pSrc->a[0].pSelect ){ + if( pSelect->pSrc->a[0].fg.isSubquery ){ return 0; /* FROM clause cannot contain a subquery */ } if( pSelect->pWhere ){ @@ -1933,7 +3069,6 @@ static int xferOptimization( if( pSelect->pLimit ){ return 0; /* SELECT may not have a LIMIT clause */ } - assert( pSelect->pOffset==0 ); /* Must be so if pLimit==0 */ if( pSelect->pPrior ){ return 0; /* SELECT may not be a compound query */ } @@ -1959,19 +3094,15 @@ static int xferOptimization( if( pSrc==0 ){ return 0; /* FROM clause does not contain a real table */ } - if( pSrc==pDest ){ + if( pSrc->tnum==pDest->tnum && pSrc->pSchema==pDest->pSchema ){ + testcase( pSrc!=pDest ); /* Possible due to bad sqlite_schema.rootpage */ return 0; /* tab1 and tab2 may not be the same table */ } if( HasRowid(pDest)!=HasRowid(pSrc) ){ return 0; /* source and destination must both be WITHOUT ROWID or not */ } -#ifndef SQLITE_OMIT_VIRTUALTABLE - if( pSrc->tabFlags & TF_Virtual ){ - return 0; /* tab2 must not be a virtual table */ - } -#endif - if( pSrc->pSelect ){ - return 0; /* tab2 may not be a view */ + if( !IsOrdinaryTable(pSrc) ){ + return 0; /* tab2 may not be a view or virtual table */ } if( pDest->nCol!=pSrc->nCol ){ return 0; /* Number of columns must be the same in tab1 and tab2 */ @@ -1979,31 +3110,78 @@ static int xferOptimization( if( pDest->iPKey!=pSrc->iPKey ){ return 0; /* Both tables must have the same INTEGER PRIMARY KEY */ } + if( (pDest->tabFlags & TF_Strict)!=0 && (pSrc->tabFlags & TF_Strict)==0 ){ + return 0; /* Cannot feed from a non-strict into a strict table */ + } for(i=0; inCol; i++){ Column *pDestCol = &pDest->aCol[i]; Column *pSrcCol = &pSrc->aCol[i]; #ifdef SQLITE_ENABLE_HIDDEN_COLUMNS - if( (db->flags & SQLITE_Vacuum)==0 - && (pDestCol->colFlags | pSrcCol->colFlags) & COLFLAG_HIDDEN + if( (db->mDbFlags & DBFLAG_Vacuum)==0 + && (pDestCol->colFlags | pSrcCol->colFlags) & COLFLAG_HIDDEN ){ return 0; /* Neither table may have __hidden__ columns */ } +#endif +#ifndef SQLITE_OMIT_GENERATED_COLUMNS + /* Even if tables t1 and t2 have identical schemas, if they contain + ** generated columns, then this statement is semantically incorrect: + ** + ** INSERT INTO t2 SELECT * FROM t1; + ** + ** The reason is that generated column values are returned by the + ** the SELECT statement on the right but the INSERT statement on the + ** left wants them to be omitted. + ** + ** Nevertheless, this is a useful notational shorthand to tell SQLite + ** to do a bulk transfer all of the content from t1 over to t2. + ** + ** We could, in theory, disable this (except for internal use by the + ** VACUUM command where it is actually needed). But why do that? It + ** seems harmless enough, and provides a useful service. + */ + if( (pDestCol->colFlags & COLFLAG_GENERATED) != + (pSrcCol->colFlags & COLFLAG_GENERATED) ){ + return 0; /* Both columns have the same generated-column type */ + } + /* But the transfer is only allowed if both the source and destination + ** tables have the exact same expressions for generated columns. + ** This requirement could be relaxed for VIRTUAL columns, I suppose. + */ + if( (pDestCol->colFlags & COLFLAG_GENERATED)!=0 ){ + if( sqlite3ExprCompare(0, + sqlite3ColumnExpr(pSrc, pSrcCol), + sqlite3ColumnExpr(pDest, pDestCol), -1)!=0 ){ + testcase( pDestCol->colFlags & COLFLAG_VIRTUAL ); + testcase( pDestCol->colFlags & COLFLAG_STORED ); + return 0; /* Different generator expressions */ + } + } #endif if( pDestCol->affinity!=pSrcCol->affinity ){ return 0; /* Affinity must be the same on all columns */ } - if( sqlite3_stricmp(pDestCol->zColl, pSrcCol->zColl)!=0 ){ + if( sqlite3_stricmp(sqlite3ColumnColl(pDestCol), + sqlite3ColumnColl(pSrcCol))!=0 ){ return 0; /* Collating sequence must be the same on all columns */ } if( pDestCol->notNull && !pSrcCol->notNull ){ return 0; /* tab2 must be NOT NULL if tab1 is */ } /* Default values for second and subsequent columns need to match. */ - if( i>0 - && ((pDestCol->zDflt==0)!=(pSrcCol->zDflt==0) - || (pDestCol->zDflt && strcmp(pDestCol->zDflt, pSrcCol->zDflt)!=0)) - ){ - return 0; /* Default values must be the same for all columns */ + if( (pDestCol->colFlags & COLFLAG_GENERATED)==0 && i>0 ){ + Expr *pDestExpr = sqlite3ColumnExpr(pDest, pDestCol); + Expr *pSrcExpr = sqlite3ColumnExpr(pSrc, pSrcCol); + assert( pDestExpr==0 || pDestExpr->op==TK_SPAN ); + assert( pDestExpr==0 || !ExprHasProperty(pDestExpr, EP_IntValue) ); + assert( pSrcExpr==0 || pSrcExpr->op==TK_SPAN ); + assert( pSrcExpr==0 || !ExprHasProperty(pSrcExpr, EP_IntValue) ); + if( (pDestExpr==0)!=(pSrcExpr==0) + || (pDestExpr!=0 && strcmp(pDestExpr->u.zToken, + pSrcExpr->u.zToken)!=0) + ){ + return 0; /* Default values must be the same for all columns */ + } } } for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){ @@ -2016,21 +3194,32 @@ static int xferOptimization( if( pSrcIdx==0 ){ return 0; /* pDestIdx has no corresponding index in pSrc */ } + if( pSrcIdx->tnum==pDestIdx->tnum && pSrc->pSchema==pDest->pSchema + && sqlite3FaultSim(411)==SQLITE_OK ){ + /* The sqlite3FaultSim() call allows this corruption test to be + ** bypassed during testing, in order to exercise other corruption tests + ** further downstream. */ + return 0; /* Corrupt schema - two indexes on the same btree */ + } } #ifndef SQLITE_OMIT_CHECK - if( pDest->pCheck && sqlite3ExprListCompare(pSrc->pCheck,pDest->pCheck,-1) ){ + if( pDest->pCheck + && (db->mDbFlags & DBFLAG_Vacuum)==0 + && sqlite3ExprListCompare(pSrc->pCheck,pDest->pCheck,-1) + ){ return 0; /* Tables have different CHECK constraints. Ticket #2252 */ } #endif #ifndef SQLITE_OMIT_FOREIGN_KEY - /* Disallow the transfer optimization if the destination table constains + /* Disallow the transfer optimization if the destination table contains ** any foreign key constraints. This is more restrictive than necessary. - ** But the main beneficiary of the transfer optimization is the VACUUM + ** But the main beneficiary of the transfer optimization is the VACUUM ** command, and the VACUUM command disables foreign key constraints. So ** the extra complication to make this rule less restrictive is probably ** not worth the effort. Ticket [6284df89debdfa61db8073e062908af0c9b6118e] */ - if( (db->flags & SQLITE_ForeignKeys)!=0 && pDest->pFKey!=0 ){ + assert( IsOrdinaryTable(pDest) ); + if( (db->flags & SQLITE_ForeignKeys)!=0 && pDest->u.tab.pFKey!=0 ){ return 0; } #endif @@ -2052,18 +3241,19 @@ static int xferOptimization( iDest = pParse->nTab++; regAutoinc = autoIncBegin(pParse, iDbDest, pDest); regData = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp2(v, OP_Null, 0, regData); regRowid = sqlite3GetTempReg(pParse); sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite); assert( HasRowid(pDest) || destHasUniqueIdx ); - if( (db->flags & SQLITE_Vacuum)==0 && ( + if( (db->mDbFlags & DBFLAG_Vacuum)==0 && ( (pDest->iPKey<0 && pDest->pIndex!=0) /* (1) */ || destHasUniqueIdx /* (2) */ || (onError!=OE_Abort && onError!=OE_Rollback) /* (3) */ )){ /* In some circumstances, we are able to run the xfer optimization ** only if the destination table is initially empty. Unless the - ** SQLITE_Vacuum flag is set, this block generates code to make - ** that determination. If SQLITE_Vacuum is set, then the destination + ** DBFLAG_Vacuum flag is set, this block generates code to make + ** that determination. If DBFLAG_Vacuum is set, then the destination ** table is always empty. ** ** Conditions under which the destination must be empty: @@ -2072,7 +3262,7 @@ static int xferOptimization( ** (If the destination is not initially empty, the rowid fields ** of index entries might need to change.) ** - ** (2) The destination has a unique index. (The xfer optimization + ** (2) The destination has a unique index. (The xfer optimization ** is unable to test uniqueness.) ** ** (3) onError is something other than OE_Abort and OE_Rollback. @@ -2082,25 +3272,47 @@ static int xferOptimization( sqlite3VdbeJumpHere(v, addr1); } if( HasRowid(pSrc) ){ + u8 insFlags; sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead); emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v); if( pDest->iPKey>=0 ){ addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid); - addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid); - VdbeCoverage(v); - sqlite3RowidConstraint(pParse, onError, pDest); - sqlite3VdbeJumpHere(v, addr2); + if( (db->mDbFlags & DBFLAG_Vacuum)==0 ){ + sqlite3VdbeVerifyAbortable(v, onError); + addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid); + VdbeCoverage(v); + sqlite3RowidConstraint(pParse, onError, pDest); + sqlite3VdbeJumpHere(v, addr2); + } autoIncStep(pParse, regAutoinc, regRowid); - }else if( pDest->pIndex==0 ){ + }else if( pDest->pIndex==0 && !(db->mDbFlags & DBFLAG_VacuumInto) ){ addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid); }else{ addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid); assert( (pDest->tabFlags & TF_Autoincrement)==0 ); } - sqlite3VdbeAddOp2(v, OP_RowData, iSrc, regData); - sqlite3VdbeAddOp4(v, OP_Insert, iDest, regData, regRowid, - pDest->zName, 0); - sqlite3VdbeChangeP5(v, OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND); + + if( db->mDbFlags & DBFLAG_Vacuum ){ + sqlite3VdbeAddOp1(v, OP_SeekEnd, iDest); + insFlags = OPFLAG_APPEND|OPFLAG_USESEEKRESULT|OPFLAG_PREFORMAT; + }else{ + insFlags = OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND|OPFLAG_PREFORMAT; + } +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK + if( (db->mDbFlags & DBFLAG_Vacuum)==0 ){ + sqlite3VdbeAddOp3(v, OP_RowData, iSrc, regData, 1); + insFlags &= ~OPFLAG_PREFORMAT; + }else +#endif + { + sqlite3VdbeAddOp3(v, OP_RowCell, iDest, iSrc, regRowid); + } + sqlite3VdbeAddOp3(v, OP_Insert, iDest, regData, regRowid); + if( (db->mDbFlags & DBFLAG_Vacuum)==0 ){ + sqlite3VdbeChangeP4(v, -1, (char*)pDest, P4_TABLE); + } + sqlite3VdbeChangeP5(v, insFlags); + sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1); VdbeCoverage(v); sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0); sqlite3VdbeAddOp2(v, OP_Close, iDest, 0); @@ -2122,38 +3334,44 @@ static int xferOptimization( sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR); VdbeComment((v, "%s", pDestIdx->zName)); addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v); - sqlite3VdbeAddOp2(v, OP_RowKey, iSrc, regData); - if( db->flags & SQLITE_Vacuum ){ + if( db->mDbFlags & DBFLAG_Vacuum ){ /* This INSERT command is part of a VACUUM operation, which guarantees ** that the destination table is empty. If all indexed columns use ** collation sequence BINARY, then it can also be assumed that the - ** index will be populated by inserting keys in strictly sorted + ** index will be populated by inserting keys in strictly sorted ** order. In this case, instead of seeking within the b-tree as part - ** of every OP_IdxInsert opcode, an OP_Last is added before the - ** OP_IdxInsert to seek to the point within the b-tree where each key + ** of every OP_IdxInsert opcode, an OP_SeekEnd is added before the + ** OP_IdxInsert to seek to the point within the b-tree where each key ** should be inserted. This is faster. ** ** If any of the indexed columns use a collation sequence other than - ** BINARY, this optimization is disabled. This is because the user + ** BINARY, this optimization is disabled. This is because the user ** might change the definition of a collation sequence and then run ** a VACUUM command. In that case keys may not be written in strictly ** sorted order. */ for(i=0; inColumn; i++){ const char *zColl = pSrcIdx->azColl[i]; - assert( sqlite3_stricmp(sqlite3StrBINARY, zColl)!=0 - || sqlite3StrBINARY==zColl ); if( sqlite3_stricmp(sqlite3StrBINARY, zColl) ) break; } if( i==pSrcIdx->nColumn ){ - idxInsFlags = OPFLAG_USESEEKRESULT; - sqlite3VdbeAddOp3(v, OP_Last, iDest, 0, -1); + idxInsFlags = OPFLAG_USESEEKRESULT|OPFLAG_PREFORMAT; + sqlite3VdbeAddOp1(v, OP_SeekEnd, iDest); + sqlite3VdbeAddOp2(v, OP_RowCell, iDest, iSrc); } - } - if( !HasRowid(pSrc) && pDestIdx->idxType==2 ){ + }else if( !HasRowid(pSrc) && pDestIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){ idxInsFlags |= OPFLAG_NCHANGE; } - sqlite3VdbeAddOp3(v, OP_IdxInsert, iDest, regData, 1); - sqlite3VdbeChangeP5(v, idxInsFlags); + if( idxInsFlags!=(OPFLAG_USESEEKRESULT|OPFLAG_PREFORMAT) ){ + sqlite3VdbeAddOp3(v, OP_RowData, iSrc, regData, 1); + if( (db->mDbFlags & DBFLAG_Vacuum)==0 + && !HasRowid(pDest) + && IsPrimaryKeyIndex(pDestIdx) + ){ + codeWithoutRowidPreupdate(pParse, pDest, iDest, regData); + } + } + sqlite3VdbeAddOp2(v, OP_IdxInsert, iDest, regData); + sqlite3VdbeChangeP5(v, idxInsFlags|OPFLAG_APPEND); sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1); VdbeCoverage(v); sqlite3VdbeJumpHere(v, addr1); sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0); @@ -2163,6 +3381,7 @@ static int xferOptimization( sqlite3ReleaseTempReg(pParse, regRowid); sqlite3ReleaseTempReg(pParse, regData); if( emptyDestTest ){ + sqlite3AutoincrementEnd(pParse); sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_OK, 0); sqlite3VdbeJumpHere(v, emptyDestTest); sqlite3VdbeAddOp2(v, OP_Close, iDest, 0); diff --git a/src/journal.c b/src/journal.c deleted file mode 100644 index fed27be3e3..0000000000 --- a/src/journal.c +++ /dev/null @@ -1,256 +0,0 @@ -/* -** 2007 August 22 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -************************************************************************* -** -** This file implements a special kind of sqlite3_file object used -** by SQLite to create journal files if the atomic-write optimization -** is enabled. -** -** The distinctive characteristic of this sqlite3_file is that the -** actual on disk file is created lazily. When the file is created, -** the caller specifies a buffer size for an in-memory buffer to -** be used to service read() and write() requests. The actual file -** on disk is not created or populated until either: -** -** 1) The in-memory representation grows too large for the allocated -** buffer, or -** 2) The sqlite3JournalCreate() function is called. -*/ -#ifdef SQLITE_ENABLE_ATOMIC_WRITE -#include "sqliteInt.h" - - -/* -** A JournalFile object is a subclass of sqlite3_file used by -** as an open file handle for journal files. -*/ -struct JournalFile { - sqlite3_io_methods *pMethod; /* I/O methods on journal files */ - int nBuf; /* Size of zBuf[] in bytes */ - char *zBuf; /* Space to buffer journal writes */ - int iSize; /* Amount of zBuf[] currently used */ - int flags; /* xOpen flags */ - sqlite3_vfs *pVfs; /* The "real" underlying VFS */ - sqlite3_file *pReal; /* The "real" underlying file descriptor */ - const char *zJournal; /* Name of the journal file */ -}; -typedef struct JournalFile JournalFile; - -/* -** If it does not already exists, create and populate the on-disk file -** for JournalFile p. -*/ -static int createFile(JournalFile *p){ - int rc = SQLITE_OK; - if( !p->pReal ){ - sqlite3_file *pReal = (sqlite3_file *)&p[1]; - rc = sqlite3OsOpen(p->pVfs, p->zJournal, pReal, p->flags, 0); - if( rc==SQLITE_OK ){ - p->pReal = pReal; - if( p->iSize>0 ){ - assert(p->iSize<=p->nBuf); - rc = sqlite3OsWrite(p->pReal, p->zBuf, p->iSize, 0); - } - if( rc!=SQLITE_OK ){ - /* If an error occurred while writing to the file, close it before - ** returning. This way, SQLite uses the in-memory journal data to - ** roll back changes made to the internal page-cache before this - ** function was called. */ - sqlite3OsClose(pReal); - p->pReal = 0; - } - } - } - return rc; -} - -/* -** Close the file. -*/ -static int jrnlClose(sqlite3_file *pJfd){ - JournalFile *p = (JournalFile *)pJfd; - if( p->pReal ){ - sqlite3OsClose(p->pReal); - } - sqlite3_free(p->zBuf); - return SQLITE_OK; -} - -/* -** Read data from the file. -*/ -static int jrnlRead( - sqlite3_file *pJfd, /* The journal file from which to read */ - void *zBuf, /* Put the results here */ - int iAmt, /* Number of bytes to read */ - sqlite_int64 iOfst /* Begin reading at this offset */ -){ - int rc = SQLITE_OK; - JournalFile *p = (JournalFile *)pJfd; - if( p->pReal ){ - rc = sqlite3OsRead(p->pReal, zBuf, iAmt, iOfst); - }else if( (iAmt+iOfst)>p->iSize ){ - rc = SQLITE_IOERR_SHORT_READ; - }else{ - memcpy(zBuf, &p->zBuf[iOfst], iAmt); - } - return rc; -} - -/* -** Write data to the file. -*/ -static int jrnlWrite( - sqlite3_file *pJfd, /* The journal file into which to write */ - const void *zBuf, /* Take data to be written from here */ - int iAmt, /* Number of bytes to write */ - sqlite_int64 iOfst /* Begin writing at this offset into the file */ -){ - int rc = SQLITE_OK; - JournalFile *p = (JournalFile *)pJfd; - if( !p->pReal && (iOfst+iAmt)>p->nBuf ){ - rc = createFile(p); - } - if( rc==SQLITE_OK ){ - if( p->pReal ){ - rc = sqlite3OsWrite(p->pReal, zBuf, iAmt, iOfst); - }else{ - memcpy(&p->zBuf[iOfst], zBuf, iAmt); - if( p->iSize<(iOfst+iAmt) ){ - p->iSize = (iOfst+iAmt); - } - } - } - return rc; -} - -/* -** Truncate the file. -*/ -static int jrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){ - int rc = SQLITE_OK; - JournalFile *p = (JournalFile *)pJfd; - if( p->pReal ){ - rc = sqlite3OsTruncate(p->pReal, size); - }else if( sizeiSize ){ - p->iSize = size; - } - return rc; -} - -/* -** Sync the file. -*/ -static int jrnlSync(sqlite3_file *pJfd, int flags){ - int rc; - JournalFile *p = (JournalFile *)pJfd; - if( p->pReal ){ - rc = sqlite3OsSync(p->pReal, flags); - }else{ - rc = SQLITE_OK; - } - return rc; -} - -/* -** Query the size of the file in bytes. -*/ -static int jrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){ - int rc = SQLITE_OK; - JournalFile *p = (JournalFile *)pJfd; - if( p->pReal ){ - rc = sqlite3OsFileSize(p->pReal, pSize); - }else{ - *pSize = (sqlite_int64) p->iSize; - } - return rc; -} - -/* -** Table of methods for JournalFile sqlite3_file object. -*/ -static struct sqlite3_io_methods JournalFileMethods = { - 1, /* iVersion */ - jrnlClose, /* xClose */ - jrnlRead, /* xRead */ - jrnlWrite, /* xWrite */ - jrnlTruncate, /* xTruncate */ - jrnlSync, /* xSync */ - jrnlFileSize, /* xFileSize */ - 0, /* xLock */ - 0, /* xUnlock */ - 0, /* xCheckReservedLock */ - 0, /* xFileControl */ - 0, /* xSectorSize */ - 0, /* xDeviceCharacteristics */ - 0, /* xShmMap */ - 0, /* xShmLock */ - 0, /* xShmBarrier */ - 0 /* xShmUnmap */ -}; - -/* -** Open a journal file. -*/ -int sqlite3JournalOpen( - sqlite3_vfs *pVfs, /* The VFS to use for actual file I/O */ - const char *zName, /* Name of the journal file */ - sqlite3_file *pJfd, /* Preallocated, blank file handle */ - int flags, /* Opening flags */ - int nBuf /* Bytes buffered before opening the file */ -){ - JournalFile *p = (JournalFile *)pJfd; - memset(p, 0, sqlite3JournalSize(pVfs)); - if( nBuf>0 ){ - p->zBuf = sqlite3MallocZero(nBuf); - if( !p->zBuf ){ - return SQLITE_NOMEM; - } - }else{ - return sqlite3OsOpen(pVfs, zName, pJfd, flags, 0); - } - p->pMethod = &JournalFileMethods; - p->nBuf = nBuf; - p->flags = flags; - p->zJournal = zName; - p->pVfs = pVfs; - return SQLITE_OK; -} - -/* -** If the argument p points to a JournalFile structure, and the underlying -** file has not yet been created, create it now. -*/ -int sqlite3JournalCreate(sqlite3_file *p){ - if( p->pMethods!=&JournalFileMethods ){ - return SQLITE_OK; - } - return createFile((JournalFile *)p); -} - -/* -** The file-handle passed as the only argument is guaranteed to be an open -** file. It may or may not be of class JournalFile. If the file is a -** JournalFile, and the underlying file on disk has not yet been opened, -** return 0. Otherwise, return 1. -*/ -int sqlite3JournalExists(sqlite3_file *p){ - return (p->pMethods!=&JournalFileMethods || ((JournalFile *)p)->pReal!=0); -} - -/* -** Return the number of bytes required to store a JournalFile that uses vfs -** pVfs to create the underlying on-disk files. -*/ -int sqlite3JournalSize(sqlite3_vfs *pVfs){ - return (pVfs->szOsFile+sizeof(JournalFile)); -} -#endif diff --git a/src/json.c b/src/json.c new file mode 100644 index 0000000000..d0d3c53a23 --- /dev/null +++ b/src/json.c @@ -0,0 +1,5599 @@ +/* +** 2015-08-12 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +** SQLite JSON functions. +** +** This file began as an extension in ext/misc/json1.c in 2015. That +** extension proved so useful that it has now been moved into the core. +** +** The original design stored all JSON as pure text, canonical RFC-8259. +** Support for JSON-5 extensions was added with version 3.42.0 (2023-05-16). +** All generated JSON text still conforms strictly to RFC-8259, but text +** with JSON-5 extensions is accepted as input. +** +** Beginning with version 3.45.0 (circa 2024-01-01), these routines also +** accept BLOB values that have JSON encoded using a binary representation +** called "JSONB". The name JSONB comes from PostgreSQL, however the on-disk +** format for SQLite-JSONB is completely different and incompatible with +** PostgreSQL-JSONB. +** +** Decoding and interpreting JSONB is still O(N) where N is the size of +** the input, the same as text JSON. However, the constant of proportionality +** for JSONB is much smaller due to faster parsing. The size of each +** element in JSONB is encoded in its header, so there is no need to search +** for delimiters using persnickety syntax rules. JSONB seems to be about +** 3x faster than text JSON as a result. JSONB is also tends to be slightly +** smaller than text JSON, by 5% or 10%, but there are corner cases where +** JSONB can be slightly larger. So you are not far mistaken to say that +** a JSONB blob is the same size as the equivalent RFC-8259 text. +** +** +** THE JSONB ENCODING: +** +** Every JSON element is encoded in JSONB as a header and a payload. +** The header is between 1 and 9 bytes in size. The payload is zero +** or more bytes. +** +** The lower 4 bits of the first byte of the header determines the +** element type: +** +** 0: NULL +** 1: TRUE +** 2: FALSE +** 3: INT -- RFC-8259 integer literal +** 4: INT5 -- JSON5 integer literal +** 5: FLOAT -- RFC-8259 floating point literal +** 6: FLOAT5 -- JSON5 floating point literal +** 7: TEXT -- Text literal acceptable to both SQL and JSON +** 8: TEXTJ -- Text containing RFC-8259 escapes +** 9: TEXT5 -- Text containing JSON5 and/or RFC-8259 escapes +** 10: TEXTRAW -- Text containing unescaped syntax characters +** 11: ARRAY +** 12: OBJECT +** +** The other three possible values (13-15) are reserved for future +** enhancements. +** +** The upper 4 bits of the first byte determine the size of the header +** and sometimes also the size of the payload. If X is the first byte +** of the element and if X>>4 is between 0 and 11, then the payload +** will be that many bytes in size and the header is exactly one byte +** in size. Other four values for X>>4 (12-15) indicate that the header +** is more than one byte in size and that the payload size is determined +** by the remainder of the header, interpreted as a unsigned big-endian +** integer. +** +** Value of X>>4 Size integer Total header size +** ------------- -------------------- ----------------- +** 12 1 byte (0-255) 2 +** 13 2 byte (0-65535) 3 +** 14 4 byte (0-4294967295) 5 +** 15 8 byte (0-1.8e19) 9 +** +** The payload size need not be expressed in its minimal form. For example, +** if the payload size is 10, the size can be expressed in any of 5 different +** ways: (1) (X>>4)==10, (2) (X>>4)==12 following by one 0x0a byte, +** (3) (X>>4)==13 followed by 0x00 and 0x0a, (4) (X>>4)==14 followed by +** 0x00 0x00 0x00 0x0a, or (5) (X>>4)==15 followed by 7 bytes of 0x00 and +** a single byte of 0x0a. The shorter forms are preferred, of course, but +** sometimes when generating JSONB, the payload size is not known in advance +** and it is convenient to reserve sufficient header space to cover the +** largest possible payload size and then come back later and patch up +** the size when it becomes known, resulting in a non-minimal encoding. +** +** The value (X>>4)==15 is not actually used in the current implementation +** (as SQLite is currently unable to handle BLOBs larger than about 2GB) +** but is included in the design to allow for future enhancements. +** +** The payload follows the header. NULL, TRUE, and FALSE have no payload and +** their payload size must always be zero. The payload for INT, INT5, +** FLOAT, FLOAT5, TEXT, TEXTJ, TEXT5, and TEXTROW is text. Note that the +** "..." or '...' delimiters are omitted from the various text encodings. +** The payload for ARRAY and OBJECT is a list of additional elements that +** are the content for the array or object. The payload for an OBJECT +** must be an even number of elements. The first element of each pair is +** the label and must be of type TEXT, TEXTJ, TEXT5, or TEXTRAW. +** +** A valid JSONB blob consists of a single element, as described above. +** Usually this will be an ARRAY or OBJECT element which has many more +** elements as its content. But the overall blob is just a single element. +** +** Input validation for JSONB blobs simply checks that the element type +** code is between 0 and 12 and that the total size of the element +** (header plus payload) is the same as the size of the BLOB. If those +** checks are true, the BLOB is assumed to be JSONB and processing continues. +** Errors are only raised if some other miscoding is discovered during +** processing. +** +** Additional information can be found in the doc/jsonb.md file of the +** canonical SQLite source tree. +*/ +#ifndef SQLITE_OMIT_JSON +#include "sqliteInt.h" + +/* JSONB element types +*/ +#define JSONB_NULL 0 /* "null" */ +#define JSONB_TRUE 1 /* "true" */ +#define JSONB_FALSE 2 /* "false" */ +#define JSONB_INT 3 /* integer acceptable to JSON and SQL */ +#define JSONB_INT5 4 /* integer in 0x000 notation */ +#define JSONB_FLOAT 5 /* float acceptable to JSON and SQL */ +#define JSONB_FLOAT5 6 /* float with JSON5 extensions */ +#define JSONB_TEXT 7 /* Text compatible with both JSON and SQL */ +#define JSONB_TEXTJ 8 /* Text with JSON escapes */ +#define JSONB_TEXT5 9 /* Text with JSON-5 escape */ +#define JSONB_TEXTRAW 10 /* SQL text that needs escaping for JSON */ +#define JSONB_ARRAY 11 /* An array */ +#define JSONB_OBJECT 12 /* An object */ + +/* Human-readable names for the JSONB values. The index for each +** string must correspond to the JSONB_* integer above. +*/ +static const char * const jsonbType[] = { + "null", "true", "false", "integer", "integer", + "real", "real", "text", "text", "text", + "text", "array", "object", "", "", "", "" +}; + +/* +** Growing our own isspace() routine this way is twice as fast as +** the library isspace() function, resulting in a 7% overall performance +** increase for the text-JSON parser. (Ubuntu14.10 gcc 4.8.4 x64 with -Os). +*/ +static const char jsonIsSpace[] = { +#ifdef SQLITE_ASCII +/*0 1 2 3 4 5 6 7 8 9 a b c d e f */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, /* 0 */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1 */ + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2 */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 3 */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 4 */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 5 */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 6 */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 7 */ + + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 8 */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 9 */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* a */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* b */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* c */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* d */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* e */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* f */ +#endif +#ifdef SQLITE_EBCDIC +/*0 1 2 3 4 5 6 7 8 9 a b c d e f */ + 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, /* 0 */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1 */ + 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2 */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 3 */ + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 4 */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 5 */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 6 */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 7 */ + + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 8 */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 9 */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* a */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* b */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* c */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* d */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* e */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* f */ +#endif + +}; +#define jsonIsspace(x) (jsonIsSpace[(unsigned char)x]) + +/* +** The set of all space characters recognized by jsonIsspace(). +** Useful as the second argument to strspn(). +*/ +#ifdef SQLITE_ASCII +static const char jsonSpaces[] = "\011\012\015\040"; +#endif +#ifdef SQLITE_EBCDIC +static const char jsonSpaces[] = "\005\045\015\100"; +#endif + + +/* +** Characters that are special to JSON. Control characters, +** '"' and '\\' and '\''. Actually, '\'' is not special to +** canonical JSON, but it is special in JSON-5, so we include +** it in the set of special characters. +*/ +static const char jsonIsOk[256] = { +#ifdef SQLITE_ASCII +/*0 1 2 3 4 5 6 7 8 9 a b c d e f */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0 */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1 */ + 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, /* 2 */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 3 */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4 */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, /* 5 */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6 */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 7 */ + + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 8 */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 9 */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* a */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* b */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* c */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* d */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* e */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 /* f */ +#endif +#ifdef SQLITE_EBCDIC +/*0 1 2 3 4 5 6 7 8 9 a b c d e f */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0 */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1 */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2 */ + 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, /* 3 */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4 */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 5 */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6 */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, /* 7 */ + + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 8 */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 9 */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* a */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* b */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* c */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* d */ + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* e */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 /* f */ +#endif +}; + +/* Objects */ +typedef struct JsonCache JsonCache; +typedef struct JsonString JsonString; +typedef struct JsonParse JsonParse; + +/* +** Magic number used for the JSON parse cache in sqlite3_get_auxdata() +*/ +#define JSON_CACHE_ID (-429938) /* Cache entry */ +#define JSON_CACHE_SIZE 4 /* Max number of cache entries */ + +/* +** jsonUnescapeOneChar() returns this invalid code point if it encounters +** a syntax error. +*/ +#define JSON_INVALID_CHAR 0x99999 + +/* A cache mapping JSON text into JSONB blobs. +** +** Each cache entry is a JsonParse object with the following restrictions: +** +** * The bReadOnly flag must be set +** +** * The aBlob[] array must be owned by the JsonParse object. In other +** words, nBlobAlloc must be non-zero. +** +** * eEdit and delta must be zero. +** +** * zJson must be an RCStr. In other words bJsonIsRCStr must be true. +*/ +struct JsonCache { + sqlite3 *db; /* Database connection */ + int nUsed; /* Number of active entries in the cache */ + JsonParse *a[JSON_CACHE_SIZE]; /* One line for each cache entry */ +}; + +/* An instance of this object represents a JSON string +** under construction. Really, this is a generic string accumulator +** that can be and is used to create strings other than JSON. +** +** If the generated string is longer than will fit into the zSpace[] buffer, +** then it will be an RCStr string. This aids with caching of large +** JSON strings. +*/ +struct JsonString { + sqlite3_context *pCtx; /* Function context - put error messages here */ + char *zBuf; /* Append JSON content here */ + u64 nAlloc; /* Bytes of storage available in zBuf[] */ + u64 nUsed; /* Bytes of zBuf[] currently used */ + u8 bStatic; /* True if zBuf is static space */ + u8 eErr; /* True if an error has been encountered */ + char zSpace[100]; /* Initial static space */ +}; + +/* Allowed values for JsonString.eErr */ +#define JSTRING_OOM 0x01 /* Out of memory */ +#define JSTRING_MALFORMED 0x02 /* Malformed JSONB */ +#define JSTRING_ERR 0x04 /* Error already sent to sqlite3_result */ + +/* The "subtype" set for text JSON values passed through using +** sqlite3_result_subtype() and sqlite3_value_subtype(). +*/ +#define JSON_SUBTYPE 74 /* Ascii for "J" */ + +/* +** Bit values for the flags passed into various SQL function implementations +** via the sqlite3_user_data() value. +*/ +#define JSON_JSON 0x01 /* Result is always JSON */ +#define JSON_SQL 0x02 /* Result is always SQL */ +#define JSON_ABPATH 0x03 /* Allow abbreviated JSON path specs */ +#define JSON_ISSET 0x04 /* json_set(), not json_insert() */ +#define JSON_BLOB 0x08 /* Use the BLOB output format */ + + +/* A parsed JSON value. Lifecycle: +** +** 1. JSON comes in and is parsed into a JSONB value in aBlob. The +** original text is stored in zJson. This step is skipped if the +** input is JSONB instead of text JSON. +** +** 2. The aBlob[] array is searched using the JSON path notation, if needed. +** +** 3. Zero or more changes are made to aBlob[] (via json_remove() or +** json_replace() or json_patch() or similar). +** +** 4. New JSON text is generated from the aBlob[] for output. This step +** is skipped if the function is one of the jsonb_* functions that +** returns JSONB instead of text JSON. +*/ +struct JsonParse { + u8 *aBlob; /* JSONB representation of JSON value */ + u32 nBlob; /* Bytes of aBlob[] actually used */ + u32 nBlobAlloc; /* Bytes allocated to aBlob[]. 0 if aBlob is external */ + char *zJson; /* Json text used for parsing */ + sqlite3 *db; /* The database connection to which this object belongs */ + int nJson; /* Length of the zJson string in bytes */ + u32 nJPRef; /* Number of references to this object */ + u32 iErr; /* Error location in zJson[] */ + u16 iDepth; /* Nesting depth */ + u8 nErr; /* Number of errors seen */ + u8 oom; /* Set to true if out of memory */ + u8 bJsonIsRCStr; /* True if zJson is an RCStr */ + u8 hasNonstd; /* True if input uses non-standard features like JSON5 */ + u8 bReadOnly; /* Do not modify. */ + /* Search and edit information. See jsonLookupStep() */ + u8 eEdit; /* Edit operation to apply */ + int delta; /* Size change due to the edit */ + u32 nIns; /* Number of bytes to insert */ + u32 iLabel; /* Location of label if search landed on an object value */ + u8 *aIns; /* Content to be inserted */ +}; + +/* Allowed values for JsonParse.eEdit */ +#define JEDIT_DEL 1 /* Delete if exists */ +#define JEDIT_REPL 2 /* Overwrite if exists */ +#define JEDIT_INS 3 /* Insert if not exists */ +#define JEDIT_SET 4 /* Insert or overwrite */ + +/* +** Maximum nesting depth of JSON for this implementation. +** +** This limit is needed to avoid a stack overflow in the recursive +** descent parser. A depth of 1000 is far deeper than any sane JSON +** should go. Historical note: This limit was 2000 prior to version 3.42.0 +*/ +#ifndef SQLITE_JSON_MAX_DEPTH +# define JSON_MAX_DEPTH 1000 +#else +# define JSON_MAX_DEPTH SQLITE_JSON_MAX_DEPTH +#endif + +/* +** Allowed values for the flgs argument to jsonParseFuncArg(); +*/ +#define JSON_EDITABLE 0x01 /* Generate a writable JsonParse object */ +#define JSON_KEEPERROR 0x02 /* Return non-NULL even if there is an error */ + +/************************************************************************** +** Forward references +**************************************************************************/ +static void jsonReturnStringAsBlob(JsonString*); +static int jsonArgIsJsonb(sqlite3_value *pJson, JsonParse *p); +static u32 jsonTranslateBlobToText(const JsonParse*,u32,JsonString*); +static void jsonReturnParse(sqlite3_context*,JsonParse*); +static JsonParse *jsonParseFuncArg(sqlite3_context*,sqlite3_value*,u32); +static void jsonParseFree(JsonParse*); +static u32 jsonbPayloadSize(const JsonParse*, u32, u32*); +static u32 jsonUnescapeOneChar(const char*, u32, u32*); + +/************************************************************************** +** Utility routines for dealing with JsonCache objects +**************************************************************************/ + +/* +** Free a JsonCache object. +*/ +static void jsonCacheDelete(JsonCache *p){ + int i; + for(i=0; inUsed; i++){ + jsonParseFree(p->a[i]); + } + sqlite3DbFree(p->db, p); +} +static void jsonCacheDeleteGeneric(void *p){ + jsonCacheDelete((JsonCache*)p); +} + +/* +** Insert a new entry into the cache. If the cache is full, expel +** the least recently used entry. Return SQLITE_OK on success or a +** result code otherwise. +** +** Cache entries are stored in age order, oldest first. +*/ +static int jsonCacheInsert( + sqlite3_context *ctx, /* The SQL statement context holding the cache */ + JsonParse *pParse /* The parse object to be added to the cache */ +){ + JsonCache *p; + + assert( pParse->zJson!=0 ); + assert( pParse->bJsonIsRCStr ); + assert( pParse->delta==0 ); + p = sqlite3_get_auxdata(ctx, JSON_CACHE_ID); + if( p==0 ){ + sqlite3 *db = sqlite3_context_db_handle(ctx); + p = sqlite3DbMallocZero(db, sizeof(*p)); + if( p==0 ) return SQLITE_NOMEM; + p->db = db; + sqlite3_set_auxdata(ctx, JSON_CACHE_ID, p, jsonCacheDeleteGeneric); + p = sqlite3_get_auxdata(ctx, JSON_CACHE_ID); + if( p==0 ) return SQLITE_NOMEM; + } + if( p->nUsed >= JSON_CACHE_SIZE ){ + jsonParseFree(p->a[0]); + memmove(p->a, &p->a[1], (JSON_CACHE_SIZE-1)*sizeof(p->a[0])); + p->nUsed = JSON_CACHE_SIZE-1; + } + assert( pParse->nBlobAlloc>0 ); + pParse->eEdit = 0; + pParse->nJPRef++; + pParse->bReadOnly = 1; + p->a[p->nUsed] = pParse; + p->nUsed++; + return SQLITE_OK; +} + +/* +** Search for a cached translation the json text supplied by pArg. Return +** the JsonParse object if found. Return NULL if not found. +** +** When a match if found, the matching entry is moved to become the +** most-recently used entry if it isn't so already. +** +** The JsonParse object returned still belongs to the Cache and might +** be deleted at any moment. If the caller wants the JsonParse to +** linger, it needs to increment the nPJRef reference counter. +*/ +static JsonParse *jsonCacheSearch( + sqlite3_context *ctx, /* The SQL statement context holding the cache */ + sqlite3_value *pArg /* Function argument containing SQL text */ +){ + JsonCache *p; + int i; + const char *zJson; + int nJson; + + if( sqlite3_value_type(pArg)!=SQLITE_TEXT ){ + return 0; + } + zJson = (const char*)sqlite3_value_text(pArg); + if( zJson==0 ) return 0; + nJson = sqlite3_value_bytes(pArg); + + p = sqlite3_get_auxdata(ctx, JSON_CACHE_ID); + if( p==0 ){ + return 0; + } + for(i=0; inUsed; i++){ + if( p->a[i]->zJson==zJson ) break; + } + if( i>=p->nUsed ){ + for(i=0; inUsed; i++){ + if( p->a[i]->nJson!=nJson ) continue; + if( memcmp(p->a[i]->zJson, zJson, nJson)==0 ) break; + } + } + if( inUsed ){ + if( inUsed-1 ){ + /* Make the matching entry the most recently used entry */ + JsonParse *tmp = p->a[i]; + memmove(&p->a[i], &p->a[i+1], (p->nUsed-i-1)*sizeof(tmp)); + p->a[p->nUsed-1] = tmp; + i = p->nUsed - 1; + } + assert( p->a[i]->delta==0 ); + return p->a[i]; + }else{ + return 0; + } +} + +/************************************************************************** +** Utility routines for dealing with JsonString objects +**************************************************************************/ + +/* Turn uninitialized bulk memory into a valid JsonString object +** holding a zero-length string. +*/ +static void jsonStringZero(JsonString *p){ + p->zBuf = p->zSpace; + p->nAlloc = sizeof(p->zSpace); + p->nUsed = 0; + p->bStatic = 1; +} + +/* Initialize the JsonString object +*/ +static void jsonStringInit(JsonString *p, sqlite3_context *pCtx){ + p->pCtx = pCtx; + p->eErr = 0; + jsonStringZero(p); +} + +/* Free all allocated memory and reset the JsonString object back to its +** initial state. +*/ +static void jsonStringReset(JsonString *p){ + if( !p->bStatic ) sqlite3RCStrUnref(p->zBuf); + jsonStringZero(p); +} + +/* Report an out-of-memory (OOM) condition +*/ +static void jsonStringOom(JsonString *p){ + p->eErr |= JSTRING_OOM; + if( p->pCtx ) sqlite3_result_error_nomem(p->pCtx); + jsonStringReset(p); +} + +/* Enlarge pJson->zBuf so that it can hold at least N more bytes. +** Return zero on success. Return non-zero on an OOM error +*/ +static int jsonStringGrow(JsonString *p, u32 N){ + u64 nTotal = NnAlloc ? p->nAlloc*2 : p->nAlloc+N+10; + char *zNew; + if( p->bStatic ){ + if( p->eErr ) return 1; + zNew = sqlite3RCStrNew(nTotal); + if( zNew==0 ){ + jsonStringOom(p); + return SQLITE_NOMEM; + } + memcpy(zNew, p->zBuf, (size_t)p->nUsed); + p->zBuf = zNew; + p->bStatic = 0; + }else{ + p->zBuf = sqlite3RCStrResize(p->zBuf, nTotal); + if( p->zBuf==0 ){ + p->eErr |= JSTRING_OOM; + jsonStringZero(p); + return SQLITE_NOMEM; + } + } + p->nAlloc = nTotal; + return SQLITE_OK; +} + +/* Append N bytes from zIn onto the end of the JsonString string. +*/ +static SQLITE_NOINLINE void jsonStringExpandAndAppend( + JsonString *p, + const char *zIn, + u32 N +){ + assert( N>0 ); + if( jsonStringGrow(p,N) ) return; + memcpy(p->zBuf+p->nUsed, zIn, N); + p->nUsed += N; +} +static void jsonAppendRaw(JsonString *p, const char *zIn, u32 N){ + if( N==0 ) return; + if( N+p->nUsed >= p->nAlloc ){ + jsonStringExpandAndAppend(p,zIn,N); + }else{ + memcpy(p->zBuf+p->nUsed, zIn, N); + p->nUsed += N; + } +} +static void jsonAppendRawNZ(JsonString *p, const char *zIn, u32 N){ + assert( N>0 ); + if( N+p->nUsed >= p->nAlloc ){ + jsonStringExpandAndAppend(p,zIn,N); + }else{ + memcpy(p->zBuf+p->nUsed, zIn, N); + p->nUsed += N; + } +} + +/* Append formatted text (not to exceed N bytes) to the JsonString. +*/ +static void jsonPrintf(int N, JsonString *p, const char *zFormat, ...){ + va_list ap; + if( (p->nUsed + N >= p->nAlloc) && jsonStringGrow(p, N) ) return; + va_start(ap, zFormat); + sqlite3_vsnprintf(N, p->zBuf+p->nUsed, zFormat, ap); + va_end(ap); + p->nUsed += (int)strlen(p->zBuf+p->nUsed); +} + +/* Append a single character +*/ +static SQLITE_NOINLINE void jsonAppendCharExpand(JsonString *p, char c){ + if( jsonStringGrow(p,1) ) return; + p->zBuf[p->nUsed++] = c; +} +static void jsonAppendChar(JsonString *p, char c){ + if( p->nUsed>=p->nAlloc ){ + jsonAppendCharExpand(p,c); + }else{ + p->zBuf[p->nUsed++] = c; + } +} + +/* Remove a single character from the end of the string +*/ +static void jsonStringTrimOneChar(JsonString *p){ + if( p->eErr==0 ){ + assert( p->nUsed>0 ); + p->nUsed--; + } +} + + +/* Make sure there is a zero terminator on p->zBuf[] +** +** Return true on success. Return false if an OOM prevents this +** from happening. +*/ +static int jsonStringTerminate(JsonString *p){ + jsonAppendChar(p, 0); + jsonStringTrimOneChar(p); + return p->eErr==0; +} + +/* Append a comma separator to the output buffer, if the previous +** character is not '[' or '{'. +*/ +static void jsonAppendSeparator(JsonString *p){ + char c; + if( p->nUsed==0 ) return; + c = p->zBuf[p->nUsed-1]; + if( c=='[' || c=='{' ) return; + jsonAppendChar(p, ','); +} + +/* c is a control character. Append the canonical JSON representation +** of that control character to p. +** +** This routine assumes that the output buffer has already been enlarged +** sufficiently to hold the worst-case encoding plus a nul terminator. +*/ +static void jsonAppendControlChar(JsonString *p, u8 c){ + static const char aSpecial[] = { + 0, 0, 0, 0, 0, 0, 0, 0, 'b', 't', 'n', 0, 'f', 'r', 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 + }; + assert( sizeof(aSpecial)==32 ); + assert( aSpecial['\b']=='b' ); + assert( aSpecial['\f']=='f' ); + assert( aSpecial['\n']=='n' ); + assert( aSpecial['\r']=='r' ); + assert( aSpecial['\t']=='t' ); + assert( c>=0 && cnUsed+7 <= p->nAlloc ); + if( aSpecial[c] ){ + p->zBuf[p->nUsed] = '\\'; + p->zBuf[p->nUsed+1] = aSpecial[c]; + p->nUsed += 2; + }else{ + p->zBuf[p->nUsed] = '\\'; + p->zBuf[p->nUsed+1] = 'u'; + p->zBuf[p->nUsed+2] = '0'; + p->zBuf[p->nUsed+3] = '0'; + p->zBuf[p->nUsed+4] = "0123456789abcdef"[c>>4]; + p->zBuf[p->nUsed+5] = "0123456789abcdef"[c&0xf]; + p->nUsed += 6; + } +} + +/* Append the N-byte string in zIn to the end of the JsonString string +** under construction. Enclose the string in double-quotes ("...") and +** escape any double-quotes or backslash characters contained within the +** string. +** +** This routine is a high-runner. There is a measurable performance +** increase associated with unwinding the jsonIsOk[] loop. +*/ +static void jsonAppendString(JsonString *p, const char *zIn, u32 N){ + u32 k; + u8 c; + const u8 *z = (const u8*)zIn; + if( z==0 ) return; + if( (N+p->nUsed+2 >= p->nAlloc) && jsonStringGrow(p,N+2)!=0 ) return; + p->zBuf[p->nUsed++] = '"'; + while( 1 /*exit-by-break*/ ){ + k = 0; + /* The following while() is the 4-way unwound equivalent of + ** + ** while( k=N ){ + while( k=N ){ + if( k>0 ){ + memcpy(&p->zBuf[p->nUsed], z, k); + p->nUsed += k; + } + break; + } + if( k>0 ){ + memcpy(&p->zBuf[p->nUsed], z, k); + p->nUsed += k; + z += k; + N -= k; + } + c = z[0]; + if( c=='"' || c=='\\' ){ + if( (p->nUsed+N+3 > p->nAlloc) && jsonStringGrow(p,N+3)!=0 ) return; + p->zBuf[p->nUsed++] = '\\'; + p->zBuf[p->nUsed++] = c; + }else if( c=='\'' ){ + p->zBuf[p->nUsed++] = c; + }else{ + if( (p->nUsed+N+7 > p->nAlloc) && jsonStringGrow(p,N+7)!=0 ) return; + jsonAppendControlChar(p, c); + } + z++; + N--; + } + p->zBuf[p->nUsed++] = '"'; + assert( p->nUsednAlloc ); +} + +/* +** Append an sqlite3_value (such as a function parameter) to the JSON +** string under construction in p. +*/ +static void jsonAppendSqlValue( + JsonString *p, /* Append to this JSON string */ + sqlite3_value *pValue /* Value to append */ +){ + switch( sqlite3_value_type(pValue) ){ + case SQLITE_NULL: { + jsonAppendRawNZ(p, "null", 4); + break; + } + case SQLITE_FLOAT: { + jsonPrintf(100, p, "%!0.15g", sqlite3_value_double(pValue)); + break; + } + case SQLITE_INTEGER: { + const char *z = (const char*)sqlite3_value_text(pValue); + u32 n = (u32)sqlite3_value_bytes(pValue); + jsonAppendRaw(p, z, n); + break; + } + case SQLITE_TEXT: { + const char *z = (const char*)sqlite3_value_text(pValue); + u32 n = (u32)sqlite3_value_bytes(pValue); + if( sqlite3_value_subtype(pValue)==JSON_SUBTYPE ){ + jsonAppendRaw(p, z, n); + }else{ + jsonAppendString(p, z, n); + } + break; + } + default: { + JsonParse px; + memset(&px, 0, sizeof(px)); + if( jsonArgIsJsonb(pValue, &px) ){ + jsonTranslateBlobToText(&px, 0, p); + }else if( p->eErr==0 ){ + sqlite3_result_error(p->pCtx, "JSON cannot hold BLOB values", -1); + p->eErr = JSTRING_ERR; + jsonStringReset(p); + } + break; + } + } +} + +/* Make the text in p (which is probably a generated JSON text string) +** the result of the SQL function. +** +** The JsonString is reset. +** +** If pParse and ctx are both non-NULL, then the SQL string in p is +** loaded into the zJson field of the pParse object as a RCStr and the +** pParse is added to the cache. +*/ +static void jsonReturnString( + JsonString *p, /* String to return */ + JsonParse *pParse, /* JSONB source or NULL */ + sqlite3_context *ctx /* Where to cache */ +){ + assert( (pParse!=0)==(ctx!=0) ); + assert( ctx==0 || ctx==p->pCtx ); + if( p->eErr==0 ){ + int flags = SQLITE_PTR_TO_INT(sqlite3_user_data(p->pCtx)); + if( flags & JSON_BLOB ){ + jsonReturnStringAsBlob(p); + }else if( p->bStatic ){ + sqlite3_result_text64(p->pCtx, p->zBuf, p->nUsed, + SQLITE_TRANSIENT, SQLITE_UTF8); + }else if( jsonStringTerminate(p) ){ + if( pParse && pParse->bJsonIsRCStr==0 && pParse->nBlobAlloc>0 ){ + int rc; + pParse->zJson = sqlite3RCStrRef(p->zBuf); + pParse->nJson = p->nUsed; + pParse->bJsonIsRCStr = 1; + rc = jsonCacheInsert(ctx, pParse); + if( rc==SQLITE_NOMEM ){ + sqlite3_result_error_nomem(ctx); + jsonStringReset(p); + return; + } + } + sqlite3_result_text64(p->pCtx, sqlite3RCStrRef(p->zBuf), p->nUsed, + sqlite3RCStrUnref, + SQLITE_UTF8); + }else{ + sqlite3_result_error_nomem(p->pCtx); + } + }else if( p->eErr & JSTRING_OOM ){ + sqlite3_result_error_nomem(p->pCtx); + }else if( p->eErr & JSTRING_MALFORMED ){ + sqlite3_result_error(p->pCtx, "malformed JSON", -1); + } + jsonStringReset(p); +} + +/************************************************************************** +** Utility routines for dealing with JsonParse objects +**************************************************************************/ + +/* +** Reclaim all memory allocated by a JsonParse object. But do not +** delete the JsonParse object itself. +*/ +static void jsonParseReset(JsonParse *pParse){ + assert( pParse->nJPRef<=1 ); + if( pParse->bJsonIsRCStr ){ + sqlite3RCStrUnref(pParse->zJson); + pParse->zJson = 0; + pParse->nJson = 0; + pParse->bJsonIsRCStr = 0; + } + if( pParse->nBlobAlloc ){ + sqlite3DbFree(pParse->db, pParse->aBlob); + pParse->aBlob = 0; + pParse->nBlob = 0; + pParse->nBlobAlloc = 0; + } +} + +/* +** Decrement the reference count on the JsonParse object. When the +** count reaches zero, free the object. +*/ +static void jsonParseFree(JsonParse *pParse){ + if( pParse ){ + if( pParse->nJPRef>1 ){ + pParse->nJPRef--; + }else{ + jsonParseReset(pParse); + sqlite3DbFree(pParse->db, pParse); + } + } +} + +/************************************************************************** +** Utility routines for the JSON text parser +**************************************************************************/ + +/* +** Translate a single byte of Hex into an integer. +** This routine only gives a correct answer if h really is a valid hexadecimal +** character: 0..9a..fA..F. But unlike sqlite3HexToInt(), it does not +** assert() if the digit is not hex. +*/ +static u8 jsonHexToInt(int h){ +#ifdef SQLITE_ASCII + h += 9*(1&(h>>6)); +#endif +#ifdef SQLITE_EBCDIC + h += 9*(1&~(h>>4)); +#endif + return (u8)(h & 0xf); +} + +/* +** Convert a 4-byte hex string into an integer +*/ +static u32 jsonHexToInt4(const char *z){ + u32 v; + v = (jsonHexToInt(z[0])<<12) + + (jsonHexToInt(z[1])<<8) + + (jsonHexToInt(z[2])<<4) + + jsonHexToInt(z[3]); + return v; +} + +/* +** Return true if z[] begins with 2 (or more) hexadecimal digits +*/ +static int jsonIs2Hex(const char *z){ + return sqlite3Isxdigit(z[0]) && sqlite3Isxdigit(z[1]); +} + +/* +** Return true if z[] begins with 4 (or more) hexadecimal digits +*/ +static int jsonIs4Hex(const char *z){ + return jsonIs2Hex(z) && jsonIs2Hex(&z[2]); +} + +/* +** Return the number of bytes of JSON5 whitespace at the beginning of +** the input string z[]. +** +** JSON5 whitespace consists of any of the following characters: +** +** Unicode UTF-8 Name +** U+0009 09 horizontal tab +** U+000a 0a line feed +** U+000b 0b vertical tab +** U+000c 0c form feed +** U+000d 0d carriage return +** U+0020 20 space +** U+00a0 c2 a0 non-breaking space +** U+1680 e1 9a 80 ogham space mark +** U+2000 e2 80 80 en quad +** U+2001 e2 80 81 em quad +** U+2002 e2 80 82 en space +** U+2003 e2 80 83 em space +** U+2004 e2 80 84 three-per-em space +** U+2005 e2 80 85 four-per-em space +** U+2006 e2 80 86 six-per-em space +** U+2007 e2 80 87 figure space +** U+2008 e2 80 88 punctuation space +** U+2009 e2 80 89 thin space +** U+200a e2 80 8a hair space +** U+2028 e2 80 a8 line separator +** U+2029 e2 80 a9 paragraph separator +** U+202f e2 80 af narrow no-break space (NNBSP) +** U+205f e2 81 9f medium mathematical space (MMSP) +** U+3000 e3 80 80 ideographical space +** U+FEFF ef bb bf byte order mark +** +** In addition, comments between '/', '*' and '*', '/' and +** from '/', '/' to end-of-line are also considered to be whitespace. +*/ +static int json5Whitespace(const char *zIn){ + int n = 0; + const u8 *z = (u8*)zIn; + while( 1 /*exit by "goto whitespace_done"*/ ){ + switch( z[n] ){ + case 0x09: + case 0x0a: + case 0x0b: + case 0x0c: + case 0x0d: + case 0x20: { + n++; + break; + } + case '/': { + if( z[n+1]=='*' && z[n+2]!=0 ){ + int j; + for(j=n+3; z[j]!='/' || z[j-1]!='*'; j++){ + if( z[j]==0 ) goto whitespace_done; + } + n = j+1; + break; + }else if( z[n+1]=='/' ){ + int j; + char c; + for(j=n+2; (c = z[j])!=0; j++){ + if( c=='\n' || c=='\r' ) break; + if( 0xe2==(u8)c && 0x80==(u8)z[j+1] + && (0xa8==(u8)z[j+2] || 0xa9==(u8)z[j+2]) + ){ + j += 2; + break; + } + } + n = j; + if( z[n] ) n++; + break; + } + goto whitespace_done; + } + case 0xc2: { + if( z[n+1]==0xa0 ){ + n += 2; + break; + } + goto whitespace_done; + } + case 0xe1: { + if( z[n+1]==0x9a && z[n+2]==0x80 ){ + n += 3; + break; + } + goto whitespace_done; + } + case 0xe2: { + if( z[n+1]==0x80 ){ + u8 c = z[n+2]; + if( c<0x80 ) goto whitespace_done; + if( c<=0x8a || c==0xa8 || c==0xa9 || c==0xaf ){ + n += 3; + break; + } + }else if( z[n+1]==0x81 && z[n+2]==0x9f ){ + n += 3; + break; + } + goto whitespace_done; + } + case 0xe3: { + if( z[n+1]==0x80 && z[n+2]==0x80 ){ + n += 3; + break; + } + goto whitespace_done; + } + case 0xef: { + if( z[n+1]==0xbb && z[n+2]==0xbf ){ + n += 3; + break; + } + goto whitespace_done; + } + default: { + goto whitespace_done; + } + } + } + whitespace_done: + return n; +} + +/* +** Extra floating-point literals to allow in JSON. +*/ +static const struct NanInfName { + char c1; + char c2; + char n; + char eType; + char nRepl; + char *zMatch; + char *zRepl; +} aNanInfName[] = { + { 'i', 'I', 3, JSONB_FLOAT, 7, "inf", "9.0e999" }, + { 'i', 'I', 8, JSONB_FLOAT, 7, "infinity", "9.0e999" }, + { 'n', 'N', 3, JSONB_NULL, 4, "NaN", "null" }, + { 'q', 'Q', 4, JSONB_NULL, 4, "QNaN", "null" }, + { 's', 'S', 4, JSONB_NULL, 4, "SNaN", "null" }, +}; + + +/* +** Report the wrong number of arguments for json_insert(), json_replace() +** or json_set(). +*/ +static void jsonWrongNumArgs( + sqlite3_context *pCtx, + const char *zFuncName +){ + char *zMsg = sqlite3_mprintf("json_%s() needs an odd number of arguments", + zFuncName); + sqlite3_result_error(pCtx, zMsg, -1); + sqlite3_free(zMsg); +} + +/**************************************************************************** +** Utility routines for dealing with the binary BLOB representation of JSON +****************************************************************************/ + +/* +** Expand pParse->aBlob so that it holds at least N bytes. +** +** Return the number of errors. +*/ +static int jsonBlobExpand(JsonParse *pParse, u32 N){ + u8 *aNew; + u64 t; + assert( N>pParse->nBlobAlloc ); + if( pParse->nBlobAlloc==0 ){ + t = 100; + }else{ + t = pParse->nBlobAlloc*2; + } + if( tdb, pParse->aBlob, t); + if( aNew==0 ){ pParse->oom = 1; return 1; } + assert( t<0x7fffffff ); + pParse->aBlob = aNew; + pParse->nBlobAlloc = (u32)t; + return 0; +} + +/* +** If pParse->aBlob is not previously editable (because it is taken +** from sqlite3_value_blob(), as indicated by the fact that +** pParse->nBlobAlloc==0 and pParse->nBlob>0) then make it editable +** by making a copy into space obtained from malloc. +** +** Return true on success. Return false on OOM. +*/ +static int jsonBlobMakeEditable(JsonParse *pParse, u32 nExtra){ + u8 *aOld; + u32 nSize; + assert( !pParse->bReadOnly ); + if( pParse->oom ) return 0; + if( pParse->nBlobAlloc>0 ) return 1; + aOld = pParse->aBlob; + nSize = pParse->nBlob + nExtra; + pParse->aBlob = 0; + if( jsonBlobExpand(pParse, nSize) ){ + return 0; + } + assert( pParse->nBlobAlloc >= pParse->nBlob + nExtra ); + memcpy(pParse->aBlob, aOld, pParse->nBlob); + return 1; +} + +/* Expand pParse->aBlob and append one bytes. +*/ +static SQLITE_NOINLINE void jsonBlobExpandAndAppendOneByte( + JsonParse *pParse, + u8 c +){ + jsonBlobExpand(pParse, pParse->nBlob+1); + if( pParse->oom==0 ){ + assert( pParse->nBlob+1<=pParse->nBlobAlloc ); + pParse->aBlob[pParse->nBlob++] = c; + } +} + +/* Append a single character. +*/ +static void jsonBlobAppendOneByte(JsonParse *pParse, u8 c){ + if( pParse->nBlob >= pParse->nBlobAlloc ){ + jsonBlobExpandAndAppendOneByte(pParse, c); + }else{ + pParse->aBlob[pParse->nBlob++] = c; + } +} + +/* Slow version of jsonBlobAppendNode() that first resizes the +** pParse->aBlob structure. +*/ +static void jsonBlobAppendNode(JsonParse*,u8,u32,const void*); +static SQLITE_NOINLINE void jsonBlobExpandAndAppendNode( + JsonParse *pParse, + u8 eType, + u32 szPayload, + const void *aPayload +){ + if( jsonBlobExpand(pParse, pParse->nBlob+szPayload+9) ) return; + jsonBlobAppendNode(pParse, eType, szPayload, aPayload); +} + + +/* Append a node type byte together with the payload size and +** possibly also the payload. +** +** If aPayload is not NULL, then it is a pointer to the payload which +** is also appended. If aPayload is NULL, the pParse->aBlob[] array +** is resized (if necessary) so that it is big enough to hold the +** payload, but the payload is not appended and pParse->nBlob is left +** pointing to where the first byte of payload will eventually be. +*/ +static void jsonBlobAppendNode( + JsonParse *pParse, /* The JsonParse object under construction */ + u8 eType, /* Node type. One of JSONB_* */ + u32 szPayload, /* Number of bytes of payload */ + const void *aPayload /* The payload. Might be NULL */ +){ + u8 *a; + if( pParse->nBlob+szPayload+9 > pParse->nBlobAlloc ){ + jsonBlobExpandAndAppendNode(pParse,eType,szPayload,aPayload); + return; + } + assert( pParse->aBlob!=0 ); + a = &pParse->aBlob[pParse->nBlob]; + if( szPayload<=11 ){ + a[0] = eType | (szPayload<<4); + pParse->nBlob += 1; + }else if( szPayload<=0xff ){ + a[0] = eType | 0xc0; + a[1] = szPayload & 0xff; + pParse->nBlob += 2; + }else if( szPayload<=0xffff ){ + a[0] = eType | 0xd0; + a[1] = (szPayload >> 8) & 0xff; + a[2] = szPayload & 0xff; + pParse->nBlob += 3; + }else{ + a[0] = eType | 0xe0; + a[1] = (szPayload >> 24) & 0xff; + a[2] = (szPayload >> 16) & 0xff; + a[3] = (szPayload >> 8) & 0xff; + a[4] = szPayload & 0xff; + pParse->nBlob += 5; + } + if( aPayload ){ + pParse->nBlob += szPayload; + memcpy(&pParse->aBlob[pParse->nBlob-szPayload], aPayload, szPayload); + } +} + +/* Change the payload size for the node at index i to be szPayload. +*/ +static int jsonBlobChangePayloadSize( + JsonParse *pParse, + u32 i, + u32 szPayload +){ + u8 *a; + u8 szType; + u8 nExtra; + u8 nNeeded; + int delta; + if( pParse->oom ) return 0; + a = &pParse->aBlob[i]; + szType = a[0]>>4; + if( szType<=11 ){ + nExtra = 0; + }else if( szType==12 ){ + nExtra = 1; + }else if( szType==13 ){ + nExtra = 2; + }else if( szType==14 ){ + nExtra = 4; + }else{ + nExtra = 8; + } + if( szPayload<=11 ){ + nNeeded = 0; + }else if( szPayload<=0xff ){ + nNeeded = 1; + }else if( szPayload<=0xffff ){ + nNeeded = 2; + }else{ + nNeeded = 4; + } + delta = nNeeded - nExtra; + if( delta ){ + u32 newSize = pParse->nBlob + delta; + if( delta>0 ){ + if( newSize>pParse->nBlobAlloc && jsonBlobExpand(pParse, newSize) ){ + return 0; /* OOM error. Error state recorded in pParse->oom. */ + } + a = &pParse->aBlob[i]; + memmove(&a[1+delta], &a[1], pParse->nBlob - (i+1)); + }else{ + memmove(&a[1], &a[1-delta], pParse->nBlob - (i+1-delta)); + } + pParse->nBlob = newSize; + } + if( nNeeded==0 ){ + a[0] = (a[0] & 0x0f) | (szPayload<<4); + }else if( nNeeded==1 ){ + a[0] = (a[0] & 0x0f) | 0xc0; + a[1] = szPayload & 0xff; + }else if( nNeeded==2 ){ + a[0] = (a[0] & 0x0f) | 0xd0; + a[1] = (szPayload >> 8) & 0xff; + a[2] = szPayload & 0xff; + }else{ + a[0] = (a[0] & 0x0f) | 0xe0; + a[1] = (szPayload >> 24) & 0xff; + a[2] = (szPayload >> 16) & 0xff; + a[3] = (szPayload >> 8) & 0xff; + a[4] = szPayload & 0xff; + } + return delta; +} + +/* +** If z[0] is 'u' and is followed by exactly 4 hexadecimal character, +** then set *pOp to JSONB_TEXTJ and return true. If not, do not make +** any changes to *pOp and return false. +*/ +static int jsonIs4HexB(const char *z, int *pOp){ + if( z[0]!='u' ) return 0; + if( !jsonIs4Hex(&z[1]) ) return 0; + *pOp = JSONB_TEXTJ; + return 1; +} + +/* +** Check a single element of the JSONB in pParse for validity. +** +** The element to be checked starts at offset i and must end at on the +** last byte before iEnd. +** +** Return 0 if everything is correct. Return the 1-based byte offset of the +** error if a problem is detected. (In other words, if the error is at offset +** 0, return 1). +*/ +static u32 jsonbValidityCheck( + const JsonParse *pParse, /* Input JSONB. Only aBlob and nBlob are used */ + u32 i, /* Start of element as pParse->aBlob[i] */ + u32 iEnd, /* One more than the last byte of the element */ + u32 iDepth /* Current nesting depth */ +){ + u32 n, sz, j, k; + const u8 *z; + u8 x; + if( iDepth>JSON_MAX_DEPTH ) return i+1; + sz = 0; + n = jsonbPayloadSize(pParse, i, &sz); + if( NEVER(n==0) ) return i+1; /* Checked by caller */ + if( NEVER(i+n+sz!=iEnd) ) return i+1; /* Checked by caller */ + z = pParse->aBlob; + x = z[i] & 0x0f; + switch( x ){ + case JSONB_NULL: + case JSONB_TRUE: + case JSONB_FALSE: { + return n+sz==1 ? 0 : i+1; + } + case JSONB_INT: { + if( sz<1 ) return i+1; + j = i+n; + if( z[j]=='-' ){ + j++; + if( sz<2 ) return i+1; + } + k = i+n+sz; + while( jk ) return j+1; + if( z[j+1]!='.' && z[j+1]!='e' && z[j+1]!='E' ) return j+1; + j++; + } + for(; j0 ) return j+1; + if( x==JSONB_FLOAT && (j==k-1 || !sqlite3Isdigit(z[j+1])) ){ + return j+1; + } + seen = 1; + continue; + } + if( z[j]=='e' || z[j]=='E' ){ + if( seen==2 ) return j+1; + if( j==k-1 ) return j+1; + if( z[j+1]=='+' || z[j+1]=='-' ){ + j++; + if( j==k-1 ) return j+1; + } + seen = 2; + continue; + } + return j+1; + } + if( seen==0 ) return i+1; + return 0; + } + case JSONB_TEXT: { + j = i+n; + k = j+sz; + while( j=k ){ + return j+1; + }else if( strchr("\"\\/bfnrt",z[j+1])!=0 ){ + j++; + }else if( z[j+1]=='u' ){ + if( j+5>=k ) return j+1; + if( !jsonIs4Hex((const char*)&z[j+2]) ) return j+1; + j++; + }else if( x!=JSONB_TEXT5 ){ + return j+1; + }else{ + u32 c = 0; + u32 szC = jsonUnescapeOneChar((const char*)&z[j], k-j, &c); + if( c==JSON_INVALID_CHAR ) return j+1; + j += szC - 1; + } + } + j++; + } + return 0; + } + case JSONB_TEXTRAW: { + return 0; + } + case JSONB_ARRAY: { + u32 sub; + j = i+n; + k = j+sz; + while( jk ) return j+1; + sub = jsonbValidityCheck(pParse, j, j+n+sz, iDepth+1); + if( sub ) return sub; + j += n + sz; + } + assert( j==k ); + return 0; + } + case JSONB_OBJECT: { + u32 cnt = 0; + u32 sub; + j = i+n; + k = j+sz; + while( jk ) return j+1; + if( (cnt & 1)==0 ){ + x = z[j] & 0x0f; + if( xJSONB_TEXTRAW ) return j+1; + } + sub = jsonbValidityCheck(pParse, j, j+n+sz, iDepth+1); + if( sub ) return sub; + cnt++; + j += n + sz; + } + assert( j==k ); + if( (cnt & 1)!=0 ) return j+1; + return 0; + } + default: { + return i+1; + } + } +} + +/* +** Translate a single element of JSON text at pParse->zJson[i] into +** its equivalent binary JSONB representation. Append the translation into +** pParse->aBlob[] beginning at pParse->nBlob. The size of +** pParse->aBlob[] is increased as necessary. +** +** Return the index of the first character past the end of the element parsed, +** or one of the following special result codes: +** +** 0 End of input +** -1 Syntax error or OOM +** -2 '}' seen \ +** -3 ']' seen \___ For these returns, pParse->iErr is set to +** -4 ',' seen / the index in zJson[] of the seen character +** -5 ':' seen / +*/ +static int jsonTranslateTextToBlob(JsonParse *pParse, u32 i){ + char c; + u32 j; + u32 iThis, iStart; + int x; + u8 t; + const char *z = pParse->zJson; +json_parse_restart: + switch( (u8)z[i] ){ + case '{': { + /* Parse object */ + iThis = pParse->nBlob; + jsonBlobAppendNode(pParse, JSONB_OBJECT, pParse->nJson-i, 0); + if( ++pParse->iDepth > JSON_MAX_DEPTH ){ + pParse->iErr = i; + return -1; + } + iStart = pParse->nBlob; + for(j=i+1;;j++){ + u32 iBlob = pParse->nBlob; + x = jsonTranslateTextToBlob(pParse, j); + if( x<=0 ){ + int op; + if( x==(-2) ){ + j = pParse->iErr; + if( pParse->nBlob!=(u32)iStart ) pParse->hasNonstd = 1; + break; + } + j += json5Whitespace(&z[j]); + op = JSONB_TEXT; + if( sqlite3JsonId1(z[j]) + || (z[j]=='\\' && jsonIs4HexB(&z[j+1], &op)) + ){ + int k = j+1; + while( (sqlite3JsonId2(z[k]) && json5Whitespace(&z[k])==0) + || (z[k]=='\\' && jsonIs4HexB(&z[k+1], &op)) + ){ + k++; + } + assert( iBlob==pParse->nBlob ); + jsonBlobAppendNode(pParse, op, k-j, &z[j]); + pParse->hasNonstd = 1; + x = k; + }else{ + if( x!=-1 ) pParse->iErr = j; + return -1; + } + } + if( pParse->oom ) return -1; + t = pParse->aBlob[iBlob] & 0x0f; + if( tJSONB_TEXTRAW ){ + pParse->iErr = j; + return -1; + } + j = x; + if( z[j]==':' ){ + j++; + }else{ + if( jsonIsspace(z[j]) ){ + /* strspn() is not helpful here */ + do{ j++; }while( jsonIsspace(z[j]) ); + if( z[j]==':' ){ + j++; + goto parse_object_value; + } + } + x = jsonTranslateTextToBlob(pParse, j); + if( x!=(-5) ){ + if( x!=(-1) ) pParse->iErr = j; + return -1; + } + j = pParse->iErr+1; + } + parse_object_value: + x = jsonTranslateTextToBlob(pParse, j); + if( x<=0 ){ + if( x!=(-1) ) pParse->iErr = j; + return -1; + } + j = x; + if( z[j]==',' ){ + continue; + }else if( z[j]=='}' ){ + break; + }else{ + if( jsonIsspace(z[j]) ){ + j += 1 + (u32)strspn(&z[j+1], jsonSpaces); + if( z[j]==',' ){ + continue; + }else if( z[j]=='}' ){ + break; + } + } + x = jsonTranslateTextToBlob(pParse, j); + if( x==(-4) ){ + j = pParse->iErr; + continue; + } + if( x==(-2) ){ + j = pParse->iErr; + break; + } + } + pParse->iErr = j; + return -1; + } + jsonBlobChangePayloadSize(pParse, iThis, pParse->nBlob - iStart); + pParse->iDepth--; + return j+1; + } + case '[': { + /* Parse array */ + iThis = pParse->nBlob; + assert( i<=(u32)pParse->nJson ); + jsonBlobAppendNode(pParse, JSONB_ARRAY, pParse->nJson - i, 0); + iStart = pParse->nBlob; + if( pParse->oom ) return -1; + if( ++pParse->iDepth > JSON_MAX_DEPTH ){ + pParse->iErr = i; + return -1; + } + for(j=i+1;;j++){ + x = jsonTranslateTextToBlob(pParse, j); + if( x<=0 ){ + if( x==(-3) ){ + j = pParse->iErr; + if( pParse->nBlob!=iStart ) pParse->hasNonstd = 1; + break; + } + if( x!=(-1) ) pParse->iErr = j; + return -1; + } + j = x; + if( z[j]==',' ){ + continue; + }else if( z[j]==']' ){ + break; + }else{ + if( jsonIsspace(z[j]) ){ + j += 1 + (u32)strspn(&z[j+1], jsonSpaces); + if( z[j]==',' ){ + continue; + }else if( z[j]==']' ){ + break; + } + } + x = jsonTranslateTextToBlob(pParse, j); + if( x==(-4) ){ + j = pParse->iErr; + continue; + } + if( x==(-3) ){ + j = pParse->iErr; + break; + } + } + pParse->iErr = j; + return -1; + } + jsonBlobChangePayloadSize(pParse, iThis, pParse->nBlob - iStart); + pParse->iDepth--; + return j+1; + } + case '\'': { + u8 opcode; + char cDelim; + pParse->hasNonstd = 1; + opcode = JSONB_TEXT; + goto parse_string; + case '"': + /* Parse string */ + opcode = JSONB_TEXT; + parse_string: + cDelim = z[i]; + j = i+1; + while( 1 /*exit-by-break*/ ){ + if( jsonIsOk[(u8)z[j]] ){ + if( !jsonIsOk[(u8)z[j+1]] ){ + j += 1; + }else if( !jsonIsOk[(u8)z[j+2]] ){ + j += 2; + }else{ + j += 3; + continue; + } + } + c = z[j]; + if( c==cDelim ){ + break; + }else if( c=='\\' ){ + c = z[++j]; + if( c=='"' || c=='\\' || c=='/' || c=='b' || c=='f' + || c=='n' || c=='r' || c=='t' + || (c=='u' && jsonIs4Hex(&z[j+1])) ){ + if( opcode==JSONB_TEXT ) opcode = JSONB_TEXTJ; + }else if( c=='\'' || c=='v' || c=='\n' +#ifdef SQLITE_BUG_COMPATIBLE_20250510 + || (c=='0') /* Legacy bug compatible */ +#else + || (c=='0' && !sqlite3Isdigit(z[j+1])) /* Correct implementation */ +#endif + || (0xe2==(u8)c && 0x80==(u8)z[j+1] + && (0xa8==(u8)z[j+2] || 0xa9==(u8)z[j+2])) + || (c=='x' && jsonIs2Hex(&z[j+1])) ){ + opcode = JSONB_TEXT5; + pParse->hasNonstd = 1; + }else if( c=='\r' ){ + if( z[j+1]=='\n' ) j++; + opcode = JSONB_TEXT5; + pParse->hasNonstd = 1; + }else{ + pParse->iErr = j; + return -1; + } + }else if( c<=0x1f ){ + if( c==0 ){ + pParse->iErr = j; + return -1; + } + /* Control characters are not allowed in canonical JSON string + ** literals, but are allowed in JSON5 string literals. */ + opcode = JSONB_TEXT5; + pParse->hasNonstd = 1; + }else if( c=='"' ){ + opcode = JSONB_TEXT5; + } + j++; + } + jsonBlobAppendNode(pParse, opcode, j-1-i, &z[i+1]); + return j+1; + } + case 't': { + if( strncmp(z+i,"true",4)==0 && !sqlite3Isalnum(z[i+4]) ){ + jsonBlobAppendOneByte(pParse, JSONB_TRUE); + return i+4; + } + pParse->iErr = i; + return -1; + } + case 'f': { + if( strncmp(z+i,"false",5)==0 && !sqlite3Isalnum(z[i+5]) ){ + jsonBlobAppendOneByte(pParse, JSONB_FALSE); + return i+5; + } + pParse->iErr = i; + return -1; + } + case '+': { + u8 seenE; + pParse->hasNonstd = 1; + t = 0x00; /* Bit 0x01: JSON5. Bit 0x02: FLOAT */ + goto parse_number; + case '.': + if( sqlite3Isdigit(z[i+1]) ){ + pParse->hasNonstd = 1; + t = 0x03; /* Bit 0x01: JSON5. Bit 0x02: FLOAT */ + seenE = 0; + goto parse_number_2; + } + pParse->iErr = i; + return -1; + case '-': + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + /* Parse number */ + t = 0x00; /* Bit 0x01: JSON5. Bit 0x02: FLOAT */ + parse_number: + seenE = 0; + assert( '-' < '0' ); + assert( '+' < '0' ); + assert( '.' < '0' ); + c = z[i]; + + if( c<='0' ){ + if( c=='0' ){ + if( (z[i+1]=='x' || z[i+1]=='X') && sqlite3Isxdigit(z[i+2]) ){ + assert( t==0x00 ); + pParse->hasNonstd = 1; + t = 0x01; + for(j=i+3; sqlite3Isxdigit(z[j]); j++){} + goto parse_number_finish; + }else if( sqlite3Isdigit(z[i+1]) ){ + pParse->iErr = i+1; + return -1; + } + }else{ + if( !sqlite3Isdigit(z[i+1]) ){ + /* JSON5 allows for "+Infinity" and "-Infinity" using exactly + ** that case. SQLite also allows these in any case and it allows + ** "+inf" and "-inf". */ + if( (z[i+1]=='I' || z[i+1]=='i') + && sqlite3StrNICmp(&z[i+1], "inf",3)==0 + ){ + pParse->hasNonstd = 1; + if( z[i]=='-' ){ + jsonBlobAppendNode(pParse, JSONB_FLOAT, 6, "-9e999"); + }else{ + jsonBlobAppendNode(pParse, JSONB_FLOAT, 5, "9e999"); + } + return i + (sqlite3StrNICmp(&z[i+4],"inity",5)==0 ? 9 : 4); + } + if( z[i+1]=='.' ){ + pParse->hasNonstd = 1; + t |= 0x01; + goto parse_number_2; + } + pParse->iErr = i; + return -1; + } + if( z[i+1]=='0' ){ + if( sqlite3Isdigit(z[i+2]) ){ + pParse->iErr = i+1; + return -1; + }else if( (z[i+2]=='x' || z[i+2]=='X') && sqlite3Isxdigit(z[i+3]) ){ + pParse->hasNonstd = 1; + t |= 0x01; + for(j=i+4; sqlite3Isxdigit(z[j]); j++){} + goto parse_number_finish; + } + } + } + } + + parse_number_2: + for(j=i+1;; j++){ + c = z[j]; + if( sqlite3Isdigit(c) ) continue; + if( c=='.' ){ + if( (t & 0x02)!=0 ){ + pParse->iErr = j; + return -1; + } + t |= 0x02; + continue; + } + if( c=='e' || c=='E' ){ + if( z[j-1]<'0' ){ + if( ALWAYS(z[j-1]=='.') && ALWAYS(j-2>=i) && sqlite3Isdigit(z[j-2]) ){ + pParse->hasNonstd = 1; + t |= 0x01; + }else{ + pParse->iErr = j; + return -1; + } + } + if( seenE ){ + pParse->iErr = j; + return -1; + } + t |= 0x02; + seenE = 1; + c = z[j+1]; + if( c=='+' || c=='-' ){ + j++; + c = z[j+1]; + } + if( c<'0' || c>'9' ){ + pParse->iErr = j; + return -1; + } + continue; + } + break; + } + if( z[j-1]<'0' ){ + if( ALWAYS(z[j-1]=='.') && ALWAYS(j-2>=i) && sqlite3Isdigit(z[j-2]) ){ + pParse->hasNonstd = 1; + t |= 0x01; + }else{ + pParse->iErr = j; + return -1; + } + } + parse_number_finish: + assert( JSONB_INT+0x01==JSONB_INT5 ); + assert( JSONB_FLOAT+0x01==JSONB_FLOAT5 ); + assert( JSONB_INT+0x02==JSONB_FLOAT ); + if( z[i]=='+' ) i++; + jsonBlobAppendNode(pParse, JSONB_INT+t, j-i, &z[i]); + return j; + } + case '}': { + pParse->iErr = i; + return -2; /* End of {...} */ + } + case ']': { + pParse->iErr = i; + return -3; /* End of [...] */ + } + case ',': { + pParse->iErr = i; + return -4; /* List separator */ + } + case ':': { + pParse->iErr = i; + return -5; /* Object label/value separator */ + } + case 0: { + return 0; /* End of file */ + } + case 0x09: + case 0x0a: + case 0x0d: + case 0x20: { + i += 1 + (u32)strspn(&z[i+1], jsonSpaces); + goto json_parse_restart; + } + case 0x0b: + case 0x0c: + case '/': + case 0xc2: + case 0xe1: + case 0xe2: + case 0xe3: + case 0xef: { + j = json5Whitespace(&z[i]); + if( j>0 ){ + i += j; + pParse->hasNonstd = 1; + goto json_parse_restart; + } + pParse->iErr = i; + return -1; + } + case 'n': { + if( strncmp(z+i,"null",4)==0 && !sqlite3Isalnum(z[i+4]) ){ + jsonBlobAppendOneByte(pParse, JSONB_NULL); + return i+4; + } + /* fall-through into the default case that checks for NaN */ + /* no break */ deliberate_fall_through + } + default: { + u32 k; + int nn; + c = z[i]; + for(k=0; khasNonstd = 1; + return i + nn; + } + pParse->iErr = i; + return -1; /* Syntax error */ + } + } /* End switch(z[i]) */ +} + + +/* +** Parse a complete JSON string. Return 0 on success or non-zero if there +** are any errors. If an error occurs, free all memory held by pParse, +** but not pParse itself. +** +** pParse must be initialized to an empty parse object prior to calling +** this routine. +*/ +static int jsonConvertTextToBlob( + JsonParse *pParse, /* Initialize and fill this JsonParse object */ + sqlite3_context *pCtx /* Report errors here */ +){ + int i; + const char *zJson = pParse->zJson; + i = jsonTranslateTextToBlob(pParse, 0); + if( pParse->oom ) i = -1; + if( i>0 ){ +#ifdef SQLITE_DEBUG + assert( pParse->iDepth==0 ); + if( sqlite3Config.bJsonSelfcheck ){ + assert( jsonbValidityCheck(pParse, 0, pParse->nBlob, 0)==0 ); + } +#endif + while( jsonIsspace(zJson[i]) ) i++; + if( zJson[i] ){ + i += json5Whitespace(&zJson[i]); + if( zJson[i] ){ + if( pCtx ) sqlite3_result_error(pCtx, "malformed JSON", -1); + jsonParseReset(pParse); + return 1; + } + pParse->hasNonstd = 1; + } + } + if( i<=0 ){ + if( pCtx!=0 ){ + if( pParse->oom ){ + sqlite3_result_error_nomem(pCtx); + }else{ + sqlite3_result_error(pCtx, "malformed JSON", -1); + } + } + jsonParseReset(pParse); + return 1; + } + return 0; +} + +/* +** The input string pStr is a well-formed JSON text string. Convert +** this into the JSONB format and make it the return value of the +** SQL function. +*/ +static void jsonReturnStringAsBlob(JsonString *pStr){ + JsonParse px; + memset(&px, 0, sizeof(px)); + jsonStringTerminate(pStr); + if( pStr->eErr ){ + sqlite3_result_error_nomem(pStr->pCtx); + return; + } + px.zJson = pStr->zBuf; + px.nJson = pStr->nUsed; + px.db = sqlite3_context_db_handle(pStr->pCtx); + (void)jsonTranslateTextToBlob(&px, 0); + if( px.oom ){ + sqlite3DbFree(px.db, px.aBlob); + sqlite3_result_error_nomem(pStr->pCtx); + }else{ + assert( px.nBlobAlloc>0 ); + assert( !px.bReadOnly ); + sqlite3_result_blob(pStr->pCtx, px.aBlob, px.nBlob, SQLITE_DYNAMIC); + } +} + +/* The byte at index i is a node type-code. This routine +** determines the payload size for that node and writes that +** payload size in to *pSz. It returns the offset from i to the +** beginning of the payload. Return 0 on error. +*/ +static u32 jsonbPayloadSize(const JsonParse *pParse, u32 i, u32 *pSz){ + u8 x; + u32 sz; + u32 n; + assert( i<=pParse->nBlob ); + x = pParse->aBlob[i]>>4; + if( x<=11 ){ + sz = x; + n = 1; + }else if( x==12 ){ + if( i+1>=pParse->nBlob ){ + *pSz = 0; + return 0; + } + sz = pParse->aBlob[i+1]; + n = 2; + }else if( x==13 ){ + if( i+2>=pParse->nBlob ){ + *pSz = 0; + return 0; + } + sz = (pParse->aBlob[i+1]<<8) + pParse->aBlob[i+2]; + n = 3; + }else if( x==14 ){ + if( i+4>=pParse->nBlob ){ + *pSz = 0; + return 0; + } + sz = ((u32)pParse->aBlob[i+1]<<24) + (pParse->aBlob[i+2]<<16) + + (pParse->aBlob[i+3]<<8) + pParse->aBlob[i+4]; + n = 5; + }else{ + if( i+8>=pParse->nBlob + || pParse->aBlob[i+1]!=0 + || pParse->aBlob[i+2]!=0 + || pParse->aBlob[i+3]!=0 + || pParse->aBlob[i+4]!=0 + ){ + *pSz = 0; + return 0; + } + sz = ((u32)pParse->aBlob[i+5]<<24) + (pParse->aBlob[i+6]<<16) + + (pParse->aBlob[i+7]<<8) + pParse->aBlob[i+8]; + n = 9; + } + if( (i64)i+sz+n > pParse->nBlob + && (i64)i+sz+n > pParse->nBlob-pParse->delta + ){ + *pSz = 0; + return 0; + } + *pSz = sz; + return n; +} + + +/* +** Translate the binary JSONB representation of JSON beginning at +** pParse->aBlob[i] into a JSON text string. Append the JSON +** text onto the end of pOut. Return the index in pParse->aBlob[] +** of the first byte past the end of the element that is translated. +** +** If an error is detected in the BLOB input, the pOut->eErr flag +** might get set to JSTRING_MALFORMED. But not all BLOB input errors +** are detected. So a malformed JSONB input might either result +** in an error, or in incorrect JSON. +** +** The pOut->eErr JSTRING_OOM flag is set on a OOM. +*/ +static u32 jsonTranslateBlobToText( + const JsonParse *pParse, /* the complete parse of the JSON */ + u32 i, /* Start rendering at this index */ + JsonString *pOut /* Write JSON here */ +){ + u32 sz, n, j, iEnd; + + n = jsonbPayloadSize(pParse, i, &sz); + if( n==0 ){ + pOut->eErr |= JSTRING_MALFORMED; + return pParse->nBlob+1; + } + switch( pParse->aBlob[i] & 0x0f ){ + case JSONB_NULL: { + jsonAppendRawNZ(pOut, "null", 4); + return i+1; + } + case JSONB_TRUE: { + jsonAppendRawNZ(pOut, "true", 4); + return i+1; + } + case JSONB_FALSE: { + jsonAppendRawNZ(pOut, "false", 5); + return i+1; + } + case JSONB_INT: + case JSONB_FLOAT: { + if( sz==0 ) goto malformed_jsonb; + jsonAppendRaw(pOut, (const char*)&pParse->aBlob[i+n], sz); + break; + } + case JSONB_INT5: { /* Integer literal in hexadecimal notation */ + u32 k = 2; + sqlite3_uint64 u = 0; + const char *zIn = (const char*)&pParse->aBlob[i+n]; + int bOverflow = 0; + if( sz==0 ) goto malformed_jsonb; + if( zIn[0]=='-' ){ + jsonAppendChar(pOut, '-'); + k++; + }else if( zIn[0]=='+' ){ + k++; + } + for(; keErr |= JSTRING_MALFORMED; + break; + }else if( (u>>60)!=0 ){ + bOverflow = 1; + }else{ + u = u*16 + sqlite3HexToInt(zIn[k]); + } + } + jsonPrintf(100,pOut,bOverflow?"9.0e999":"%llu", u); + break; + } + case JSONB_FLOAT5: { /* Float literal missing digits beside "." */ + u32 k = 0; + const char *zIn = (const char*)&pParse->aBlob[i+n]; + if( sz==0 ) goto malformed_jsonb; + if( zIn[0]=='-' ){ + jsonAppendChar(pOut, '-'); + k++; + } + if( zIn[k]=='.' ){ + jsonAppendChar(pOut, '0'); + } + for(; knUsed+sz+2<=pOut->nAlloc || jsonStringGrow(pOut, sz+2)==0 ){ + pOut->zBuf[pOut->nUsed] = '"'; + memcpy(pOut->zBuf+pOut->nUsed+1,(const char*)&pParse->aBlob[i+n],sz); + pOut->zBuf[pOut->nUsed+sz+1] = '"'; + pOut->nUsed += sz+2; + } + break; + } + case JSONB_TEXT5: { + const char *zIn; + u32 k; + u32 sz2 = sz; + zIn = (const char*)&pParse->aBlob[i+n]; + jsonAppendChar(pOut, '"'); + while( sz2>0 ){ + for(k=0; k0 ){ + jsonAppendRawNZ(pOut, zIn, k); + if( k>=sz2 ){ + break; + } + zIn += k; + sz2 -= k; + } + if( zIn[0]=='"' ){ + jsonAppendRawNZ(pOut, "\\\"", 2); + zIn++; + sz2--; + continue; + } + if( zIn[0]<=0x1f ){ + if( pOut->nUsed+7>pOut->nAlloc && jsonStringGrow(pOut,7) ) break; + jsonAppendControlChar(pOut, zIn[0]); + zIn++; + sz2--; + continue; + } + assert( zIn[0]=='\\' ); + assert( sz2>=1 ); + if( sz2<2 ){ + pOut->eErr |= JSTRING_MALFORMED; + break; + } + switch( (u8)zIn[1] ){ + case '\'': + jsonAppendChar(pOut, '\''); + break; + case 'v': + jsonAppendRawNZ(pOut, "\\u000b", 6); + break; + case 'x': + if( sz2<4 ){ + pOut->eErr |= JSTRING_MALFORMED; + sz2 = 2; + break; + } + jsonAppendRawNZ(pOut, "\\u00", 4); + jsonAppendRawNZ(pOut, &zIn[2], 2); + zIn += 2; + sz2 -= 2; + break; + case '0': + jsonAppendRawNZ(pOut, "\\u0000", 6); + break; + case '\r': + if( sz2>2 && zIn[2]=='\n' ){ + zIn++; + sz2--; + } + break; + case '\n': + break; + case 0xe2: + /* '\' followed by either U+2028 or U+2029 is ignored as + ** whitespace. Not that in UTF8, U+2028 is 0xe2 0x80 0x29. + ** U+2029 is the same except for the last byte */ + if( sz2<4 + || 0x80!=(u8)zIn[2] + || (0xa8!=(u8)zIn[3] && 0xa9!=(u8)zIn[3]) + ){ + pOut->eErr |= JSTRING_MALFORMED; + sz2 = 2; + break; + } + zIn += 2; + sz2 -= 2; + break; + default: + jsonAppendRawNZ(pOut, zIn, 2); + break; + } + assert( sz2>=2 ); + zIn += 2; + sz2 -= 2; + } + jsonAppendChar(pOut, '"'); + break; + } + case JSONB_TEXTRAW: { + jsonAppendString(pOut, (const char*)&pParse->aBlob[i+n], sz); + break; + } + case JSONB_ARRAY: { + jsonAppendChar(pOut, '['); + j = i+n; + iEnd = j+sz; + while( jeErr==0 ){ + j = jsonTranslateBlobToText(pParse, j, pOut); + jsonAppendChar(pOut, ','); + } + if( j>iEnd ) pOut->eErr |= JSTRING_MALFORMED; + if( sz>0 ) jsonStringTrimOneChar(pOut); + jsonAppendChar(pOut, ']'); + break; + } + case JSONB_OBJECT: { + int x = 0; + jsonAppendChar(pOut, '{'); + j = i+n; + iEnd = j+sz; + while( jeErr==0 ){ + j = jsonTranslateBlobToText(pParse, j, pOut); + jsonAppendChar(pOut, (x++ & 1) ? ',' : ':'); + } + if( (x & 1)!=0 || j>iEnd ) pOut->eErr |= JSTRING_MALFORMED; + if( sz>0 ) jsonStringTrimOneChar(pOut); + jsonAppendChar(pOut, '}'); + break; + } + + default: { + malformed_jsonb: + pOut->eErr |= JSTRING_MALFORMED; + break; + } + } + return i+n+sz; +} + +/* Context for recursion of json_pretty() +*/ +typedef struct JsonPretty JsonPretty; +struct JsonPretty { + JsonParse *pParse; /* The BLOB being rendered */ + JsonString *pOut; /* Generate pretty output into this string */ + const char *zIndent; /* Use this text for indentation */ + u32 szIndent; /* Bytes in zIndent[] */ + u32 nIndent; /* Current level of indentation */ +}; + +/* Append indentation to the pretty JSON under construction */ +static void jsonPrettyIndent(JsonPretty *pPretty){ + u32 jj; + for(jj=0; jjnIndent; jj++){ + jsonAppendRaw(pPretty->pOut, pPretty->zIndent, pPretty->szIndent); + } +} + +/* +** Translate the binary JSONB representation of JSON beginning at +** pParse->aBlob[i] into a JSON text string. Append the JSON +** text onto the end of pOut. Return the index in pParse->aBlob[] +** of the first byte past the end of the element that is translated. +** +** This is a variant of jsonTranslateBlobToText() that "pretty-prints" +** the output. Extra whitespace is inserted to make the JSON easier +** for humans to read. +** +** If an error is detected in the BLOB input, the pOut->eErr flag +** might get set to JSTRING_MALFORMED. But not all BLOB input errors +** are detected. So a malformed JSONB input might either result +** in an error, or in incorrect JSON. +** +** The pOut->eErr JSTRING_OOM flag is set on a OOM. +*/ +static u32 jsonTranslateBlobToPrettyText( + JsonPretty *pPretty, /* Pretty-printing context */ + u32 i /* Start rendering at this index */ +){ + u32 sz, n, j, iEnd; + const JsonParse *pParse = pPretty->pParse; + JsonString *pOut = pPretty->pOut; + n = jsonbPayloadSize(pParse, i, &sz); + if( n==0 ){ + pOut->eErr |= JSTRING_MALFORMED; + return pParse->nBlob+1; + } + switch( pParse->aBlob[i] & 0x0f ){ + case JSONB_ARRAY: { + j = i+n; + iEnd = j+sz; + jsonAppendChar(pOut, '['); + if( jnIndent++; + while( pOut->eErr==0 ){ + jsonPrettyIndent(pPretty); + j = jsonTranslateBlobToPrettyText(pPretty, j); + if( j>=iEnd ) break; + jsonAppendRawNZ(pOut, ",\n", 2); + } + jsonAppendChar(pOut, '\n'); + pPretty->nIndent--; + jsonPrettyIndent(pPretty); + } + jsonAppendChar(pOut, ']'); + i = iEnd; + break; + } + case JSONB_OBJECT: { + j = i+n; + iEnd = j+sz; + jsonAppendChar(pOut, '{'); + if( jnIndent++; + while( pOut->eErr==0 ){ + jsonPrettyIndent(pPretty); + j = jsonTranslateBlobToText(pParse, j, pOut); + if( j>iEnd ){ + pOut->eErr |= JSTRING_MALFORMED; + break; + } + jsonAppendRawNZ(pOut, ": ", 2); + j = jsonTranslateBlobToPrettyText(pPretty, j); + if( j>=iEnd ) break; + jsonAppendRawNZ(pOut, ",\n", 2); + } + jsonAppendChar(pOut, '\n'); + pPretty->nIndent--; + jsonPrettyIndent(pPretty); + } + jsonAppendChar(pOut, '}'); + i = iEnd; + break; + } + default: { + i = jsonTranslateBlobToText(pParse, i, pOut); + break; + } + } + return i; +} + +/* +** Given that a JSONB_ARRAY object starts at offset i, return +** the number of entries in that array. +*/ +static u32 jsonbArrayCount(JsonParse *pParse, u32 iRoot){ + u32 n, sz, i, iEnd; + u32 k = 0; + n = jsonbPayloadSize(pParse, iRoot, &sz); + iEnd = iRoot+n+sz; + for(i=iRoot+n; n>0 && idelta. +*/ +static void jsonAfterEditSizeAdjust(JsonParse *pParse, u32 iRoot){ + u32 sz = 0; + u32 nBlob; + assert( pParse->delta!=0 ); + assert( pParse->nBlobAlloc >= pParse->nBlob ); + nBlob = pParse->nBlob; + pParse->nBlob = pParse->nBlobAlloc; + (void)jsonbPayloadSize(pParse, iRoot, &sz); + pParse->nBlob = nBlob; + sz += pParse->delta; + pParse->delta += jsonBlobChangePayloadSize(pParse, iRoot, sz); +} + +/* +** If the JSONB at aIns[0..nIns-1] can be expanded (by denormalizing the +** size field) by d bytes, then write the expansion into aOut[] and +** return true. In this way, an overwrite happens without changing the +** size of the JSONB, which reduces memcpy() operations and also make it +** faster and easier to update the B-Tree entry that contains the JSONB +** in the database. +** +** If the expansion of aIns[] by d bytes cannot be (easily) accomplished +** then return false. +** +** The d parameter is guaranteed to be between 1 and 8. +** +** This routine is an optimization. A correct answer is obtained if it +** always leaves the output unchanged and returns false. +*/ +static int jsonBlobOverwrite( + u8 *aOut, /* Overwrite here */ + const u8 *aIns, /* New content */ + u32 nIns, /* Bytes of new content */ + u32 d /* Need to expand new content by this much */ +){ + u32 szPayload; /* Bytes of payload */ + u32 i; /* New header size, after expansion & a loop counter */ + u8 szHdr; /* Size of header before expansion */ + + /* Lookup table for finding the upper 4 bits of the first byte of the + ** expanded aIns[], based on the size of the expanded aIns[] header: + ** + ** 2 3 4 5 6 7 8 9 */ + static const u8 aType[] = { 0xc0, 0xd0, 0, 0xe0, 0, 0, 0, 0xf0 }; + + if( (aIns[0]&0x0f)<=2 ) return 0; /* Cannot enlarge NULL, true, false */ + switch( aIns[0]>>4 ){ + default: { /* aIns[] header size 1 */ + if( ((1<=2 && i<=9 && aType[i-2]!=0 ); + aOut[0] = (aIns[0] & 0x0f) | aType[i-2]; + memcpy(&aOut[i], &aIns[szHdr], nIns-szHdr); + szPayload = nIns - szHdr; + while( 1/*edit-by-break*/ ){ + i--; + aOut[i] = szPayload & 0xff; + if( i==1 ) break; + szPayload >>= 8; + } + assert( (szPayload>>8)==0 ); + return 1; +} + +/* +** Modify the JSONB blob at pParse->aBlob by removing nDel bytes of +** content beginning at iDel, and replacing them with nIns bytes of +** content given by aIns. +** +** nDel may be zero, in which case no bytes are removed. But iDel is +** still important as new bytes will be insert beginning at iDel. +** +** aIns may be zero, in which case space is created to hold nIns bytes +** beginning at iDel, but that space is uninitialized. +** +** Set pParse->oom if an OOM occurs. +*/ +static void jsonBlobEdit( + JsonParse *pParse, /* The JSONB to be modified is in pParse->aBlob */ + u32 iDel, /* First byte to be removed */ + u32 nDel, /* Number of bytes to remove */ + const u8 *aIns, /* Content to insert */ + u32 nIns /* Bytes of content to insert */ +){ + i64 d = (i64)nIns - (i64)nDel; + if( d<0 && d>=(-8) && aIns!=0 + && jsonBlobOverwrite(&pParse->aBlob[iDel], aIns, nIns, (int)-d) + ){ + return; + } + if( d!=0 ){ + if( pParse->nBlob + d > pParse->nBlobAlloc ){ + jsonBlobExpand(pParse, pParse->nBlob+d); + if( pParse->oom ) return; + } + memmove(&pParse->aBlob[iDel+nIns], + &pParse->aBlob[iDel+nDel], + pParse->nBlob - (iDel+nDel)); + pParse->nBlob += d; + pParse->delta += d; + } + if( nIns && aIns ){ + memcpy(&pParse->aBlob[iDel], aIns, nIns); + } +} + +/* +** Return the number of escaped newlines to be ignored. +** An escaped newline is a one of the following byte sequences: +** +** 0x5c 0x0a +** 0x5c 0x0d +** 0x5c 0x0d 0x0a +** 0x5c 0xe2 0x80 0xa8 +** 0x5c 0xe2 0x80 0xa9 +*/ +static u32 jsonBytesToBypass(const char *z, u32 n){ + u32 i = 0; + while( i+10 ); + assert( z[0]=='\\' ); + if( n<2 ){ + *piOut = JSON_INVALID_CHAR; + return n; + } + switch( (u8)z[1] ){ + case 'u': { + u32 v, vlo; + if( n<6 ){ + *piOut = JSON_INVALID_CHAR; + return n; + } + v = jsonHexToInt4(&z[2]); + if( (v & 0xfc00)==0xd800 + && n>=12 + && z[6]=='\\' + && z[7]=='u' + && ((vlo = jsonHexToInt4(&z[8]))&0xfc00)==0xdc00 + ){ + *piOut = ((v&0x3ff)<<10) + (vlo&0x3ff) + 0x10000; + return 12; + }else{ + *piOut = v; + return 6; + } + } + case 'b': { *piOut = '\b'; return 2; } + case 'f': { *piOut = '\f'; return 2; } + case 'n': { *piOut = '\n'; return 2; } + case 'r': { *piOut = '\r'; return 2; } + case 't': { *piOut = '\t'; return 2; } + case 'v': { *piOut = '\v'; return 2; } + case '0': { + /* JSON5 requires that the \0 escape not be followed by a digit. + ** But SQLite did not enforce this restriction in versions 3.42.0 + ** through 3.49.2. That was a bug. But some applications might have + ** come to depend on that bug. Use the SQLITE_BUG_COMPATIBLE_20250510 + ** option to restore the old buggy behavior. */ +#ifdef SQLITE_BUG_COMPATIBLE_20250510 + /* Legacy bug-compatible behavior */ + *piOut = 0; +#else + /* Correct behavior */ + *piOut = (n>2 && sqlite3Isdigit(z[2])) ? JSON_INVALID_CHAR : 0; +#endif + return 2; + } + case '\'': + case '"': + case '/': + case '\\':{ *piOut = z[1]; return 2; } + case 'x': { + if( n<4 ){ + *piOut = JSON_INVALID_CHAR; + return n; + } + *piOut = (jsonHexToInt(z[2])<<4) | jsonHexToInt(z[3]); + return 4; + } + case 0xe2: + case '\r': + case '\n': { + u32 nSkip = jsonBytesToBypass(z, n); + if( nSkip==0 ){ + *piOut = JSON_INVALID_CHAR; + return n; + }else if( nSkip==n ){ + *piOut = 0; + return n; + }else if( z[nSkip]=='\\' ){ + return nSkip + jsonUnescapeOneChar(&z[nSkip], n-nSkip, piOut); + }else{ + int sz = sqlite3Utf8ReadLimited((u8*)&z[nSkip], n-nSkip, piOut); + return nSkip + sz; + } + } + default: { + *piOut = JSON_INVALID_CHAR; + return 2; + } + } +} + + +/* +** Compare two object labels. Return 1 if they are equal and +** 0 if they differ. +** +** In this version, we know that one or the other or both of the +** two comparands contains an escape sequence. +*/ +static SQLITE_NOINLINE int jsonLabelCompareEscaped( + const char *zLeft, /* The left label */ + u32 nLeft, /* Size of the left label in bytes */ + int rawLeft, /* True if zLeft contains no escapes */ + const char *zRight, /* The right label */ + u32 nRight, /* Size of the right label in bytes */ + int rawRight /* True if zRight is escape-free */ +){ + u32 cLeft, cRight; + assert( rawLeft==0 || rawRight==0 ); + while( 1 /*exit-by-return*/ ){ + if( nLeft==0 ){ + cLeft = 0; + }else if( rawLeft || zLeft[0]!='\\' ){ + cLeft = ((u8*)zLeft)[0]; + if( cLeft>=0xc0 ){ + int sz = sqlite3Utf8ReadLimited((u8*)zLeft, nLeft, &cLeft); + zLeft += sz; + nLeft -= sz; + }else{ + zLeft++; + nLeft--; + } + }else{ + u32 n = jsonUnescapeOneChar(zLeft, nLeft, &cLeft); + zLeft += n; + assert( n<=nLeft ); + nLeft -= n; + } + if( nRight==0 ){ + cRight = 0; + }else if( rawRight || zRight[0]!='\\' ){ + cRight = ((u8*)zRight)[0]; + if( cRight>=0xc0 ){ + int sz = sqlite3Utf8ReadLimited((u8*)zRight, nRight, &cRight); + zRight += sz; + nRight -= sz; + }else{ + zRight++; + nRight--; + } + }else{ + u32 n = jsonUnescapeOneChar(zRight, nRight, &cRight); + zRight += n; + assert( n<=nRight ); + nRight -= n; + } + if( cLeft!=cRight ) return 0; + if( cLeft==0 ) return 1; + } +} + +/* +** Compare two object labels. Return 1 if they are equal and +** 0 if they differ. Return -1 if an OOM occurs. +*/ +static int jsonLabelCompare( + const char *zLeft, /* The left label */ + u32 nLeft, /* Size of the left label in bytes */ + int rawLeft, /* True if zLeft contains no escapes */ + const char *zRight, /* The right label */ + u32 nRight, /* Size of the right label in bytes */ + int rawRight /* True if zRight is escape-free */ +){ + if( rawLeft && rawRight ){ + /* Simpliest case: Neither label contains escapes. A simple + ** memcmp() is sufficient. */ + if( nLeft!=nRight ) return 0; + return memcmp(zLeft, zRight, nLeft)==0; + }else{ + return jsonLabelCompareEscaped(zLeft, nLeft, rawLeft, + zRight, nRight, rawRight); + } +} + +/* +** Error returns from jsonLookupStep() +*/ +#define JSON_LOOKUP_ERROR 0xffffffff +#define JSON_LOOKUP_NOTFOUND 0xfffffffe +#define JSON_LOOKUP_PATHERROR 0xfffffffd +#define JSON_LOOKUP_ISERROR(x) ((x)>=JSON_LOOKUP_PATHERROR) + +/* Forward declaration */ +static u32 jsonLookupStep(JsonParse*,u32,const char*,u32); + + +/* This helper routine for jsonLookupStep() populates pIns with +** binary data that is to be inserted into pParse. +** +** In the common case, pIns just points to pParse->aIns and pParse->nIns. +** But if the zPath of the original edit operation includes path elements +** that go deeper, additional substructure must be created. +** +** For example: +** +** json_insert('{}', '$.a.b.c', 123); +** +** The search stops at '$.a' But additional substructure must be +** created for the ".b.c" part of the patch so that the final result +** is: {"a":{"b":{"c"::123}}}. This routine populates pIns with +** the binary equivalent of {"b":{"c":123}} so that it can be inserted. +** +** The caller is responsible for resetting pIns when it has finished +** using the substructure. +*/ +static u32 jsonCreateEditSubstructure( + JsonParse *pParse, /* The original JSONB that is being edited */ + JsonParse *pIns, /* Populate this with the blob data to insert */ + const char *zTail /* Tail of the path that determins substructure */ +){ + static const u8 emptyObject[] = { JSONB_ARRAY, JSONB_OBJECT }; + int rc; + memset(pIns, 0, sizeof(*pIns)); + pIns->db = pParse->db; + if( zTail[0]==0 ){ + /* No substructure. Just insert what is given in pParse. */ + pIns->aBlob = pParse->aIns; + pIns->nBlob = pParse->nIns; + rc = 0; + }else{ + /* Construct the binary substructure */ + pIns->nBlob = 1; + pIns->aBlob = (u8*)&emptyObject[zTail[0]=='.']; + pIns->eEdit = pParse->eEdit; + pIns->nIns = pParse->nIns; + pIns->aIns = pParse->aIns; + rc = jsonLookupStep(pIns, 0, zTail, 0); + pParse->oom |= pIns->oom; + } + return rc; /* Error code only */ +} + +/* +** Search along zPath to find the Json element specified. Return an +** index into pParse->aBlob[] for the start of that element's value. +** +** If the value found by this routine is the value half of label/value pair +** within an object, then set pPath->iLabel to the start of the corresponding +** label, before returning. +** +** Return one of the JSON_LOOKUP error codes if problems are seen. +** +** This routine will also modify the blob. If pParse->eEdit is one of +** JEDIT_DEL, JEDIT_REPL, JEDIT_INS, or JEDIT_SET, then changes might be +** made to the selected value. If an edit is performed, then the return +** value does not necessarily point to the select element. If an edit +** is performed, the return value is only useful for detecting error +** conditions. +*/ +static u32 jsonLookupStep( + JsonParse *pParse, /* The JSON to search */ + u32 iRoot, /* Begin the search at this element of aBlob[] */ + const char *zPath, /* The path to search */ + u32 iLabel /* Label if iRoot is a value of in an object */ +){ + u32 i, j, k, nKey, sz, n, iEnd, rc; + const char *zKey; + u8 x; + + if( zPath[0]==0 ){ + if( pParse->eEdit && jsonBlobMakeEditable(pParse, pParse->nIns) ){ + n = jsonbPayloadSize(pParse, iRoot, &sz); + sz += n; + if( pParse->eEdit==JEDIT_DEL ){ + if( iLabel>0 ){ + sz += iRoot - iLabel; + iRoot = iLabel; + } + jsonBlobEdit(pParse, iRoot, sz, 0, 0); + }else if( pParse->eEdit==JEDIT_INS ){ + /* Already exists, so json_insert() is a no-op */ + }else{ + /* json_set() or json_replace() */ + jsonBlobEdit(pParse, iRoot, sz, pParse->aIns, pParse->nIns); + } + } + pParse->iLabel = iLabel; + return iRoot; + } + if( zPath[0]=='.' ){ + int rawKey = 1; + x = pParse->aBlob[iRoot]; + zPath++; + if( zPath[0]=='"' ){ + zKey = zPath + 1; + for(i=1; zPath[i] && zPath[i]!='"'; i++){ + if( zPath[i]=='\\' && zPath[i+1]!=0 ) i++; + } + nKey = i-1; + if( zPath[i] ){ + i++; + }else{ + return JSON_LOOKUP_PATHERROR; + } + testcase( nKey==0 ); + rawKey = memchr(zKey, '\\', nKey)==0; + }else{ + zKey = zPath; + for(i=0; zPath[i] && zPath[i]!='.' && zPath[i]!='['; i++){} + nKey = i; + if( nKey==0 ){ + return JSON_LOOKUP_PATHERROR; + } + } + if( (x & 0x0f)!=JSONB_OBJECT ) return JSON_LOOKUP_NOTFOUND; + n = jsonbPayloadSize(pParse, iRoot, &sz); + j = iRoot + n; /* j is the index of a label */ + iEnd = j+sz; + while( jaBlob[j] & 0x0f; + if( xJSONB_TEXTRAW ) return JSON_LOOKUP_ERROR; + n = jsonbPayloadSize(pParse, j, &sz); + if( n==0 ) return JSON_LOOKUP_ERROR; + k = j+n; /* k is the index of the label text */ + if( k+sz>=iEnd ) return JSON_LOOKUP_ERROR; + zLabel = (const char*)&pParse->aBlob[k]; + rawLabel = x==JSONB_TEXT || x==JSONB_TEXTRAW; + if( jsonLabelCompare(zKey, nKey, rawKey, zLabel, sz, rawLabel) ){ + u32 v = k+sz; /* v is the index of the value */ + if( ((pParse->aBlob[v])&0x0f)>JSONB_OBJECT ) return JSON_LOOKUP_ERROR; + n = jsonbPayloadSize(pParse, v, &sz); + if( n==0 || v+n+sz>iEnd ) return JSON_LOOKUP_ERROR; + assert( j>0 ); + rc = jsonLookupStep(pParse, v, &zPath[i], j); + if( pParse->delta ) jsonAfterEditSizeAdjust(pParse, iRoot); + return rc; + } + j = k+sz; + if( ((pParse->aBlob[j])&0x0f)>JSONB_OBJECT ) return JSON_LOOKUP_ERROR; + n = jsonbPayloadSize(pParse, j, &sz); + if( n==0 ) return JSON_LOOKUP_ERROR; + j += n+sz; + } + if( j>iEnd ) return JSON_LOOKUP_ERROR; + if( pParse->eEdit>=JEDIT_INS ){ + u32 nIns; /* Total bytes to insert (label+value) */ + JsonParse v; /* BLOB encoding of the value to be inserted */ + JsonParse ix; /* Header of the label to be inserted */ + testcase( pParse->eEdit==JEDIT_INS ); + testcase( pParse->eEdit==JEDIT_SET ); + memset(&ix, 0, sizeof(ix)); + ix.db = pParse->db; + jsonBlobAppendNode(&ix, rawKey?JSONB_TEXTRAW:JSONB_TEXT5, nKey, 0); + pParse->oom |= ix.oom; + rc = jsonCreateEditSubstructure(pParse, &v, &zPath[i]); + if( !JSON_LOOKUP_ISERROR(rc) + && jsonBlobMakeEditable(pParse, ix.nBlob+nKey+v.nBlob) + ){ + assert( !pParse->oom ); + nIns = ix.nBlob + nKey + v.nBlob; + jsonBlobEdit(pParse, j, 0, 0, nIns); + if( !pParse->oom ){ + assert( pParse->aBlob!=0 ); /* Because pParse->oom!=0 */ + assert( ix.aBlob!=0 ); /* Because pPasre->oom!=0 */ + memcpy(&pParse->aBlob[j], ix.aBlob, ix.nBlob); + k = j + ix.nBlob; + memcpy(&pParse->aBlob[k], zKey, nKey); + k += nKey; + memcpy(&pParse->aBlob[k], v.aBlob, v.nBlob); + if( ALWAYS(pParse->delta) ) jsonAfterEditSizeAdjust(pParse, iRoot); + } + } + jsonParseReset(&v); + jsonParseReset(&ix); + return rc; + } + }else if( zPath[0]=='[' ){ + x = pParse->aBlob[iRoot] & 0x0f; + if( x!=JSONB_ARRAY ) return JSON_LOOKUP_NOTFOUND; + n = jsonbPayloadSize(pParse, iRoot, &sz); + k = 0; + i = 1; + while( sqlite3Isdigit(zPath[i]) ){ + k = k*10 + zPath[i] - '0'; + i++; + } + if( i<2 || zPath[i]!=']' ){ + if( zPath[1]=='#' ){ + k = jsonbArrayCount(pParse, iRoot); + i = 2; + if( zPath[2]=='-' && sqlite3Isdigit(zPath[3]) ){ + unsigned int nn = 0; + i = 3; + do{ + nn = nn*10 + zPath[i] - '0'; + i++; + }while( sqlite3Isdigit(zPath[i]) ); + if( nn>k ) return JSON_LOOKUP_NOTFOUND; + k -= nn; + } + if( zPath[i]!=']' ){ + return JSON_LOOKUP_PATHERROR; + } + }else{ + return JSON_LOOKUP_PATHERROR; + } + } + j = iRoot+n; + iEnd = j+sz; + while( jdelta ) jsonAfterEditSizeAdjust(pParse, iRoot); + return rc; + } + k--; + n = jsonbPayloadSize(pParse, j, &sz); + if( n==0 ) return JSON_LOOKUP_ERROR; + j += n+sz; + } + if( j>iEnd ) return JSON_LOOKUP_ERROR; + if( k>0 ) return JSON_LOOKUP_NOTFOUND; + if( pParse->eEdit>=JEDIT_INS ){ + JsonParse v; + testcase( pParse->eEdit==JEDIT_INS ); + testcase( pParse->eEdit==JEDIT_SET ); + rc = jsonCreateEditSubstructure(pParse, &v, &zPath[i+1]); + if( !JSON_LOOKUP_ISERROR(rc) + && jsonBlobMakeEditable(pParse, v.nBlob) + ){ + assert( !pParse->oom ); + jsonBlobEdit(pParse, j, 0, v.aBlob, v.nBlob); + } + jsonParseReset(&v); + if( pParse->delta ) jsonAfterEditSizeAdjust(pParse, iRoot); + return rc; + } + }else{ + return JSON_LOOKUP_PATHERROR; + } + return JSON_LOOKUP_NOTFOUND; +} + +/* +** Convert a JSON BLOB into text and make that text the return value +** of an SQL function. +*/ +static void jsonReturnTextJsonFromBlob( + sqlite3_context *ctx, + const u8 *aBlob, + u32 nBlob +){ + JsonParse x; + JsonString s; + + if( NEVER(aBlob==0) ) return; + memset(&x, 0, sizeof(x)); + x.aBlob = (u8*)aBlob; + x.nBlob = nBlob; + jsonStringInit(&s, ctx); + jsonTranslateBlobToText(&x, 0, &s); + jsonReturnString(&s, 0, 0); +} + + +/* +** Return the value of the BLOB node at index i. +** +** If the value is a primitive, return it as an SQL value. +** If the value is an array or object, return it as either +** JSON text or the BLOB encoding, depending on the eMode flag +** as follows: +** +** eMode==0 JSONB if the JSON_B flag is set in userdata or +** text if the JSON_B flag is omitted from userdata. +** +** eMode==1 Text +** +** eMode==2 JSONB +*/ +static void jsonReturnFromBlob( + JsonParse *pParse, /* Complete JSON parse tree */ + u32 i, /* Index of the node */ + sqlite3_context *pCtx, /* Return value for this function */ + int eMode /* Format of return: text of JSONB */ +){ + u32 n, sz; + int rc; + sqlite3 *db = sqlite3_context_db_handle(pCtx); + + assert( eMode>=0 && eMode<=2 ); + n = jsonbPayloadSize(pParse, i, &sz); + if( n==0 ){ + sqlite3_result_error(pCtx, "malformed JSON", -1); + return; + } + switch( pParse->aBlob[i] & 0x0f ){ + case JSONB_NULL: { + if( sz ) goto returnfromblob_malformed; + sqlite3_result_null(pCtx); + break; + } + case JSONB_TRUE: { + if( sz ) goto returnfromblob_malformed; + sqlite3_result_int(pCtx, 1); + break; + } + case JSONB_FALSE: { + if( sz ) goto returnfromblob_malformed; + sqlite3_result_int(pCtx, 0); + break; + } + case JSONB_INT5: + case JSONB_INT: { + sqlite3_int64 iRes = 0; + char *z; + int bNeg = 0; + char x; + if( sz==0 ) goto returnfromblob_malformed; + x = (char)pParse->aBlob[i+n]; + if( x=='-' ){ + if( sz<2 ) goto returnfromblob_malformed; + n++; + sz--; + bNeg = 1; + } + z = sqlite3DbStrNDup(db, (const char*)&pParse->aBlob[i+n], (int)sz); + if( z==0 ) goto returnfromblob_oom; + rc = sqlite3DecOrHexToI64(z, &iRes); + sqlite3DbFree(db, z); + if( rc==0 ){ + if( iRes<0 ){ + /* A hexadecimal literal with 16 significant digits and with the + ** high-order bit set is a negative integer in SQLite (and hence + ** iRes comes back as negative) but should be interpreted as a + ** positive value if it occurs within JSON. The value is too + ** large to appear as an SQLite integer so it must be converted + ** into floating point. */ + double r; + r = (double)*(sqlite3_uint64*)&iRes; + sqlite3_result_double(pCtx, bNeg ? -r : r); + }else{ + sqlite3_result_int64(pCtx, bNeg ? -iRes : iRes); + } + }else if( rc==3 && bNeg ){ + sqlite3_result_int64(pCtx, SMALLEST_INT64); + }else if( rc==1 ){ + goto returnfromblob_malformed; + }else{ + if( bNeg ){ n--; sz++; } + goto to_double; + } + break; + } + case JSONB_FLOAT5: + case JSONB_FLOAT: { + double r; + char *z; + if( sz==0 ) goto returnfromblob_malformed; + to_double: + z = sqlite3DbStrNDup(db, (const char*)&pParse->aBlob[i+n], (int)sz); + if( z==0 ) goto returnfromblob_oom; + rc = sqlite3AtoF(z, &r, sqlite3Strlen30(z), SQLITE_UTF8); + sqlite3DbFree(db, z); + if( rc<=0 ) goto returnfromblob_malformed; + sqlite3_result_double(pCtx, r); + break; + } + case JSONB_TEXTRAW: + case JSONB_TEXT: { + sqlite3_result_text(pCtx, (char*)&pParse->aBlob[i+n], sz, + SQLITE_TRANSIENT); + break; + } + case JSONB_TEXT5: + case JSONB_TEXTJ: { + /* Translate JSON formatted string into raw text */ + u32 iIn, iOut; + const char *z; + char *zOut; + u32 nOut = sz; + z = (const char*)&pParse->aBlob[i+n]; + zOut = sqlite3DbMallocRaw(db, ((u64)nOut)+1); + if( zOut==0 ) goto returnfromblob_oom; + for(iIn=iOut=0; iIn=2 ); + zOut[iOut++] = (char)(0xc0 | (v>>6)); + zOut[iOut++] = 0x80 | (v&0x3f); + }else if( v<0x10000 ){ + assert( szEscape>=3 ); + zOut[iOut++] = 0xe0 | (v>>12); + zOut[iOut++] = 0x80 | ((v>>6)&0x3f); + zOut[iOut++] = 0x80 | (v&0x3f); + }else if( v==JSON_INVALID_CHAR ){ + /* Silently ignore illegal unicode */ + }else{ + assert( szEscape>=4 ); + zOut[iOut++] = 0xf0 | (v>>18); + zOut[iOut++] = 0x80 | ((v>>12)&0x3f); + zOut[iOut++] = 0x80 | ((v>>6)&0x3f); + zOut[iOut++] = 0x80 | (v&0x3f); + } + iIn += szEscape - 1; + }else{ + zOut[iOut++] = c; + } + } /* end for() */ + assert( iOut<=nOut ); + zOut[iOut] = 0; + sqlite3_result_text(pCtx, zOut, iOut, SQLITE_DYNAMIC); + break; + } + case JSONB_ARRAY: + case JSONB_OBJECT: { + if( eMode==0 ){ + if( (SQLITE_PTR_TO_INT(sqlite3_user_data(pCtx)) & JSON_BLOB)!=0 ){ + eMode = 2; + }else{ + eMode = 1; + } + } + if( eMode==2 ){ + sqlite3_result_blob(pCtx, &pParse->aBlob[i], sz+n, SQLITE_TRANSIENT); + }else{ + jsonReturnTextJsonFromBlob(pCtx, &pParse->aBlob[i], sz+n); + } + break; + } + default: { + goto returnfromblob_malformed; + } + } + return; + +returnfromblob_oom: + sqlite3_result_error_nomem(pCtx); + return; + +returnfromblob_malformed: + sqlite3_result_error(pCtx, "malformed JSON", -1); + return; +} + +/* +** pArg is a function argument that might be an SQL value or a JSON +** value. Figure out what it is and encode it as a JSONB blob. +** Return the results in pParse. +** +** pParse is uninitialized upon entry. This routine will handle the +** initialization of pParse. The result will be contained in +** pParse->aBlob and pParse->nBlob. pParse->aBlob might be dynamically +** allocated (if pParse->nBlobAlloc is greater than zero) in which case +** the caller is responsible for freeing the space allocated to pParse->aBlob +** when it has finished with it. Or pParse->aBlob might be a static string +** or a value obtained from sqlite3_value_blob(pArg). +** +** If the argument is a BLOB that is clearly not a JSONB, then this +** function might set an error message in ctx and return non-zero. +** It might also set an error message and return non-zero on an OOM error. +*/ +static int jsonFunctionArgToBlob( + sqlite3_context *ctx, + sqlite3_value *pArg, + JsonParse *pParse +){ + int eType = sqlite3_value_type(pArg); + static u8 aNull[] = { 0x00 }; + memset(pParse, 0, sizeof(pParse[0])); + pParse->db = sqlite3_context_db_handle(ctx); + switch( eType ){ + default: { + pParse->aBlob = aNull; + pParse->nBlob = 1; + return 0; + } + case SQLITE_BLOB: { + if( !jsonArgIsJsonb(pArg, pParse) ){ + sqlite3_result_error(ctx, "JSON cannot hold BLOB values", -1); + return 1; + } + break; + } + case SQLITE_TEXT: { + const char *zJson = (const char*)sqlite3_value_text(pArg); + int nJson = sqlite3_value_bytes(pArg); + if( zJson==0 ) return 1; + if( sqlite3_value_subtype(pArg)==JSON_SUBTYPE ){ + pParse->zJson = (char*)zJson; + pParse->nJson = nJson; + if( jsonConvertTextToBlob(pParse, ctx) ){ + sqlite3_result_error(ctx, "malformed JSON", -1); + sqlite3DbFree(pParse->db, pParse->aBlob); + memset(pParse, 0, sizeof(pParse[0])); + return 1; + } + }else{ + jsonBlobAppendNode(pParse, JSONB_TEXTRAW, nJson, zJson); + } + break; + } + case SQLITE_FLOAT: { + double r = sqlite3_value_double(pArg); + if( NEVER(sqlite3IsNaN(r)) ){ + jsonBlobAppendNode(pParse, JSONB_NULL, 0, 0); + }else{ + int n = sqlite3_value_bytes(pArg); + const char *z = (const char*)sqlite3_value_text(pArg); + if( z==0 ) return 1; + if( z[0]=='I' ){ + jsonBlobAppendNode(pParse, JSONB_FLOAT, 5, "9e999"); + }else if( z[0]=='-' && z[1]=='I' ){ + jsonBlobAppendNode(pParse, JSONB_FLOAT, 6, "-9e999"); + }else{ + jsonBlobAppendNode(pParse, JSONB_FLOAT, n, z); + } + } + break; + } + case SQLITE_INTEGER: { + int n = sqlite3_value_bytes(pArg); + const char *z = (const char*)sqlite3_value_text(pArg); + if( z==0 ) return 1; + jsonBlobAppendNode(pParse, JSONB_INT, n, z); + break; + } + } + if( pParse->oom ){ + sqlite3_result_error_nomem(ctx); + return 1; + }else{ + return 0; + } +} + +/* +** Generate a bad path error. +** +** If ctx is not NULL then push the error message into ctx and return NULL. +** If ctx is NULL, then return the text of the error message. +*/ +static char *jsonBadPathError( + sqlite3_context *ctx, /* The function call containing the error */ + const char *zPath /* The path with the problem */ +){ + char *zMsg = sqlite3_mprintf("bad JSON path: %Q", zPath); + if( ctx==0 ) return zMsg; + if( zMsg ){ + sqlite3_result_error(ctx, zMsg, -1); + sqlite3_free(zMsg); + }else{ + sqlite3_result_error_nomem(ctx); + } + return 0; +} + +/* argv[0] is a BLOB that seems likely to be a JSONB. Subsequent +** arguments come in pairs where each pair contains a JSON path and +** content to insert or set at that patch. Do the updates +** and return the result. +** +** The specific operation is determined by eEdit, which can be one +** of JEDIT_INS, JEDIT_REPL, or JEDIT_SET. +*/ +static void jsonInsertIntoBlob( + sqlite3_context *ctx, + int argc, + sqlite3_value **argv, + int eEdit /* JEDIT_INS, JEDIT_REPL, or JEDIT_SET */ +){ + int i; + u32 rc = 0; + const char *zPath = 0; + int flgs; + JsonParse *p; + JsonParse ax; + + assert( (argc&1)==1 ); + flgs = argc==1 ? 0 : JSON_EDITABLE; + p = jsonParseFuncArg(ctx, argv[0], flgs); + if( p==0 ) return; + for(i=1; inBlob, ax.aBlob, ax.nBlob); + } + rc = 0; + }else{ + p->eEdit = eEdit; + p->nIns = ax.nBlob; + p->aIns = ax.aBlob; + p->delta = 0; + rc = jsonLookupStep(p, 0, zPath+1, 0); + } + jsonParseReset(&ax); + if( rc==JSON_LOOKUP_NOTFOUND ) continue; + if( JSON_LOOKUP_ISERROR(rc) ) goto jsonInsertIntoBlob_patherror; + } + jsonReturnParse(ctx, p); + jsonParseFree(p); + return; + +jsonInsertIntoBlob_patherror: + jsonParseFree(p); + if( rc==JSON_LOOKUP_ERROR ){ + sqlite3_result_error(ctx, "malformed JSON", -1); + }else{ + jsonBadPathError(ctx, zPath); + } + return; +} + +/* +** If pArg is a blob that seems like a JSONB blob, then initialize +** p to point to that JSONB and return TRUE. If pArg does not seem like +** a JSONB blob, then return FALSE. +** +** For small BLOBs (having no more than 7 bytes of payload) a full +** validity check is done. So for small BLOBs this routine only returns +** true if the value is guaranteed to be a valid JSONB. For larger BLOBs +** (8 byte or more of payload) only the size of the outermost element is +** checked to verify that the BLOB is superficially valid JSONB. +** +** A full JSONB validation is done on smaller BLOBs because those BLOBs might +** also be text JSON that has been incorrectly cast into a BLOB. +** (See tag-20240123-a and https://sqlite.org/forum/forumpost/012136abd5) +** If the BLOB is 9 bytes are larger, then it is not possible for the +** superficial size check done here to pass if the input is really text +** JSON so we do not need to look deeper in that case. +** +** Why we only need to do full JSONB validation for smaller BLOBs: +** +** The first byte of valid JSON text must be one of: '{', '[', '"', ' ', '\n', +** '\r', '\t', '-', or a digit '0' through '9'. Of these, only a subset +** can also be the first byte of JSONB: '{', '[', and digits '3' +** through '9'. In every one of those cases, the payload size is 7 bytes +** or less. So if we do full JSONB validation for every BLOB where the +** payload is less than 7 bytes, we will never get a false positive for +** JSONB on an input that is really text JSON. +*/ +static int jsonArgIsJsonb(sqlite3_value *pArg, JsonParse *p){ + u32 n, sz = 0; + u8 c; + if( sqlite3_value_type(pArg)!=SQLITE_BLOB ) return 0; + p->aBlob = (u8*)sqlite3_value_blob(pArg); + p->nBlob = (u32)sqlite3_value_bytes(pArg); + if( p->nBlob>0 + && ALWAYS(p->aBlob!=0) + && ((c = p->aBlob[0]) & 0x0f)<=JSONB_OBJECT + && (n = jsonbPayloadSize(p, 0, &sz))>0 + && sz+n==p->nBlob + && ((c & 0x0f)>JSONB_FALSE || sz==0) + && (sz>7 + || (c!=0x7b && c!=0x5b && !sqlite3Isdigit(c)) + || jsonbValidityCheck(p, 0, p->nBlob, 1)==0) + ){ + return 1; + } + p->aBlob = 0; + p->nBlob = 0; + return 0; +} + +/* +** Generate a JsonParse object, containing valid JSONB in aBlob and nBlob, +** from the SQL function argument pArg. Return a pointer to the new +** JsonParse object. +** +** Ownership of the new JsonParse object is passed to the caller. The +** caller should invoke jsonParseFree() on the return value when it +** has finished using it. +** +** If any errors are detected, an appropriate error messages is set +** using sqlite3_result_error() or the equivalent and this routine +** returns NULL. This routine also returns NULL if the pArg argument +** is an SQL NULL value, but no error message is set in that case. This +** is so that SQL functions that are given NULL arguments will return +** a NULL value. +*/ +static JsonParse *jsonParseFuncArg( + sqlite3_context *ctx, + sqlite3_value *pArg, + u32 flgs +){ + int eType; /* Datatype of pArg */ + JsonParse *p = 0; /* Value to be returned */ + JsonParse *pFromCache = 0; /* Value taken from cache */ + sqlite3 *db; /* The database connection */ + + assert( ctx!=0 ); + eType = sqlite3_value_type(pArg); + if( eType==SQLITE_NULL ){ + return 0; + } + pFromCache = jsonCacheSearch(ctx, pArg); + if( pFromCache ){ + pFromCache->nJPRef++; + if( (flgs & JSON_EDITABLE)==0 ){ + return pFromCache; + } + } + db = sqlite3_context_db_handle(ctx); +rebuild_from_cache: + p = sqlite3DbMallocZero(db, sizeof(*p)); + if( p==0 ) goto json_pfa_oom; + memset(p, 0, sizeof(*p)); + p->db = db; + p->nJPRef = 1; + if( pFromCache!=0 ){ + u32 nBlob = pFromCache->nBlob; + p->aBlob = sqlite3DbMallocRaw(db, nBlob); + if( p->aBlob==0 ) goto json_pfa_oom; + memcpy(p->aBlob, pFromCache->aBlob, nBlob); + p->nBlobAlloc = p->nBlob = nBlob; + p->hasNonstd = pFromCache->hasNonstd; + jsonParseFree(pFromCache); + return p; + } + if( eType==SQLITE_BLOB ){ + if( jsonArgIsJsonb(pArg,p) ){ + if( (flgs & JSON_EDITABLE)!=0 && jsonBlobMakeEditable(p, 0)==0 ){ + goto json_pfa_oom; + } + return p; + } + /* If the blob is not valid JSONB, fall through into trying to cast + ** the blob into text which is then interpreted as JSON. (tag-20240123-a) + ** + ** This goes against all historical documentation about how the SQLite + ** JSON functions were suppose to work. From the beginning, blob was + ** reserved for expansion and a blob value should have raised an error. + ** But it did not, due to a bug. And many applications came to depend + ** upon this buggy behavior, especially when using the CLI and reading + ** JSON text using readfile(), which returns a blob. For this reason + ** we will continue to support the bug moving forward. + ** See for example https://sqlite.org/forum/forumpost/012136abd5292b8d + */ + } + p->zJson = (char*)sqlite3_value_text(pArg); + p->nJson = sqlite3_value_bytes(pArg); + if( db->mallocFailed ) goto json_pfa_oom; + if( p->nJson==0 ) goto json_pfa_malformed; + assert( p->zJson!=0 ); + if( jsonConvertTextToBlob(p, (flgs & JSON_KEEPERROR) ? 0 : ctx) ){ + if( flgs & JSON_KEEPERROR ){ + p->nErr = 1; + return p; + }else{ + jsonParseFree(p); + return 0; + } + }else{ + int isRCStr = sqlite3ValueIsOfClass(pArg, sqlite3RCStrUnref); + int rc; + if( !isRCStr ){ + char *zNew = sqlite3RCStrNew( p->nJson ); + if( zNew==0 ) goto json_pfa_oom; + memcpy(zNew, p->zJson, p->nJson); + p->zJson = zNew; + p->zJson[p->nJson] = 0; + }else{ + sqlite3RCStrRef(p->zJson); + } + p->bJsonIsRCStr = 1; + rc = jsonCacheInsert(ctx, p); + if( rc==SQLITE_NOMEM ) goto json_pfa_oom; + if( flgs & JSON_EDITABLE ){ + pFromCache = p; + p = 0; + goto rebuild_from_cache; + } + } + return p; + +json_pfa_malformed: + if( flgs & JSON_KEEPERROR ){ + p->nErr = 1; + return p; + }else{ + jsonParseFree(p); + sqlite3_result_error(ctx, "malformed JSON", -1); + return 0; + } + +json_pfa_oom: + jsonParseFree(pFromCache); + jsonParseFree(p); + sqlite3_result_error_nomem(ctx); + return 0; +} + +/* +** Make the return value of a JSON function either the raw JSONB blob +** or make it JSON text, depending on whether the JSON_BLOB flag is +** set on the function. +*/ +static void jsonReturnParse( + sqlite3_context *ctx, + JsonParse *p +){ + int flgs; + if( p->oom ){ + sqlite3_result_error_nomem(ctx); + return; + } + flgs = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx)); + if( flgs & JSON_BLOB ){ + if( p->nBlobAlloc>0 && !p->bReadOnly ){ + sqlite3_result_blob(ctx, p->aBlob, p->nBlob, SQLITE_DYNAMIC); + p->nBlobAlloc = 0; + }else{ + sqlite3_result_blob(ctx, p->aBlob, p->nBlob, SQLITE_TRANSIENT); + } + }else{ + JsonString s; + jsonStringInit(&s, ctx); + p->delta = 0; + jsonTranslateBlobToText(p, 0, &s); + jsonReturnString(&s, p, ctx); + sqlite3_result_subtype(ctx, JSON_SUBTYPE); + } +} + +/**************************************************************************** +** SQL functions used for testing and debugging +****************************************************************************/ + +#if SQLITE_DEBUG +/* +** Decode JSONB bytes in aBlob[] starting at iStart through but not +** including iEnd. Indent the +** content by nIndent spaces. +*/ +static void jsonDebugPrintBlob( + JsonParse *pParse, /* JSON content */ + u32 iStart, /* Start rendering here */ + u32 iEnd, /* Do not render this byte or any byte after this one */ + int nIndent, /* Indent by this many spaces */ + sqlite3_str *pOut /* Generate output into this sqlite3_str object */ +){ + while( iStartaBlob[iStart] & 0x0f; + u32 savedNBlob = pParse->nBlob; + sqlite3_str_appendf(pOut, "%5d:%*s", iStart, nIndent, ""); + if( pParse->nBlobAlloc>pParse->nBlob ){ + pParse->nBlob = pParse->nBlobAlloc; + } + nn = n = jsonbPayloadSize(pParse, iStart, &sz); + if( nn==0 ) nn = 1; + if( sz>0 && xaBlob[iStart+i]); + } + if( n==0 ){ + sqlite3_str_appendf(pOut, " ERROR invalid node size\n"); + iStart = n==0 ? iStart+1 : iEnd; + continue; + } + pParse->nBlob = savedNBlob; + if( iStart+n+sz>iEnd ){ + iEnd = iStart+n+sz; + if( iEnd>pParse->nBlob ){ + if( pParse->nBlobAlloc>0 && iEnd>pParse->nBlobAlloc ){ + iEnd = pParse->nBlobAlloc; + }else{ + iEnd = pParse->nBlob; + } + } + } + sqlite3_str_appendall(pOut," <-- "); + switch( x ){ + case JSONB_NULL: sqlite3_str_appendall(pOut,"null"); break; + case JSONB_TRUE: sqlite3_str_appendall(pOut,"true"); break; + case JSONB_FALSE: sqlite3_str_appendall(pOut,"false"); break; + case JSONB_INT: sqlite3_str_appendall(pOut,"int"); break; + case JSONB_INT5: sqlite3_str_appendall(pOut,"int5"); break; + case JSONB_FLOAT: sqlite3_str_appendall(pOut,"float"); break; + case JSONB_FLOAT5: sqlite3_str_appendall(pOut,"float5"); break; + case JSONB_TEXT: sqlite3_str_appendall(pOut,"text"); break; + case JSONB_TEXTJ: sqlite3_str_appendall(pOut,"textj"); break; + case JSONB_TEXT5: sqlite3_str_appendall(pOut,"text5"); break; + case JSONB_TEXTRAW: sqlite3_str_appendall(pOut,"textraw"); break; + case JSONB_ARRAY: { + sqlite3_str_appendf(pOut,"array, %u bytes\n", sz); + jsonDebugPrintBlob(pParse, iStart+n, iStart+n+sz, nIndent+2, pOut); + showContent = 0; + break; + } + case JSONB_OBJECT: { + sqlite3_str_appendf(pOut, "object, %u bytes\n", sz); + jsonDebugPrintBlob(pParse, iStart+n, iStart+n+sz, nIndent+2, pOut); + showContent = 0; + break; + } + default: { + sqlite3_str_appendall(pOut, "ERROR: unknown node type\n"); + showContent = 0; + break; + } + } + if( showContent ){ + if( sz==0 && x<=JSONB_FALSE ){ + sqlite3_str_append(pOut, "\n", 1); + }else{ + u32 j; + sqlite3_str_appendall(pOut, ": \""); + for(j=iStart+n; jaBlob[j]; + if( c<0x20 || c>=0x7f ) c = '.'; + sqlite3_str_append(pOut, (char*)&c, 1); + } + sqlite3_str_append(pOut, "\"\n", 2); + } + } + iStart += n + sz; + } +} +static void jsonShowParse(JsonParse *pParse){ + sqlite3_str out; + char zBuf[1000]; + if( pParse==0 ){ + printf("NULL pointer\n"); + return; + }else{ + printf("nBlobAlloc = %u\n", pParse->nBlobAlloc); + printf("nBlob = %u\n", pParse->nBlob); + printf("delta = %d\n", pParse->delta); + if( pParse->nBlob==0 ) return; + printf("content (bytes 0..%u):\n", pParse->nBlob-1); + } + sqlite3StrAccumInit(&out, 0, zBuf, sizeof(zBuf), 1000000); + jsonDebugPrintBlob(pParse, 0, pParse->nBlob, 0, &out); + printf("%s", sqlite3_str_value(&out)); + sqlite3_str_reset(&out); +} +#endif /* SQLITE_DEBUG */ + +#ifdef SQLITE_DEBUG +/* +** SQL function: json_parse(JSON) +** +** Parse JSON using jsonParseFuncArg(). Return text that is a +** human-readable dump of the binary JSONB for the input parameter. +*/ +static void jsonParseFunc( + sqlite3_context *ctx, + int argc, + sqlite3_value **argv +){ + JsonParse *p; /* The parse */ + sqlite3_str out; + + assert( argc>=1 ); + sqlite3StrAccumInit(&out, 0, 0, 0, 1000000); + p = jsonParseFuncArg(ctx, argv[0], 0); + if( p==0 ) return; + if( argc==1 ){ + jsonDebugPrintBlob(p, 0, p->nBlob, 0, &out); + sqlite3_result_text64(ctx,out.zText,out.nChar,SQLITE_TRANSIENT,SQLITE_UTF8); + }else{ + jsonShowParse(p); + } + jsonParseFree(p); + sqlite3_str_reset(&out); +} +#endif /* SQLITE_DEBUG */ + +/**************************************************************************** +** Scalar SQL function implementations +****************************************************************************/ + +/* +** Implementation of the json_quote(VALUE) function. Return a JSON value +** corresponding to the SQL value input. Mostly this means putting +** double-quotes around strings and returning the unquoted string "null" +** when given a NULL input. +*/ +static void jsonQuoteFunc( + sqlite3_context *ctx, + int argc, + sqlite3_value **argv +){ + JsonString jx; + UNUSED_PARAMETER(argc); + + jsonStringInit(&jx, ctx); + jsonAppendSqlValue(&jx, argv[0]); + jsonReturnString(&jx, 0, 0); + sqlite3_result_subtype(ctx, JSON_SUBTYPE); +} + +/* +** Implementation of the json_array(VALUE,...) function. Return a JSON +** array that contains all values given in arguments. Or if any argument +** is a BLOB, throw an error. +*/ +static void jsonArrayFunc( + sqlite3_context *ctx, + int argc, + sqlite3_value **argv +){ + int i; + JsonString jx; + + jsonStringInit(&jx, ctx); + jsonAppendChar(&jx, '['); + for(i=0; iaBlob[i] & 0x0f)==JSONB_ARRAY ){ + cnt = jsonbArrayCount(p, i); + } + if( !eErr ) sqlite3_result_int64(ctx, cnt); + jsonParseFree(p); +} + +/* True if the string is all alphanumerics and underscores */ +static int jsonAllAlphanum(const char *z, int n){ + int i; + for(i=0; i"(JSON,PATH) +** "->>"(JSON,PATH) +** +** Return the element described by PATH. Return NULL if that PATH element +** is not found. +** +** If JSON_JSON is set or if more that one PATH argument is supplied then +** always return a JSON representation of the result. If JSON_SQL is set, +** then always return an SQL representation of the result. If neither flag +** is present and argc==2, then return JSON for objects and arrays and SQL +** for all other values. +** +** When multiple PATH arguments are supplied, the result is a JSON array +** containing the result of each PATH. +** +** Abbreviated JSON path expressions are allows if JSON_ABPATH, for +** compatibility with PG. +*/ +static void jsonExtractFunc( + sqlite3_context *ctx, + int argc, + sqlite3_value **argv +){ + JsonParse *p = 0; /* The parse */ + int flags; /* Flags associated with the function */ + int i; /* Loop counter */ + JsonString jx; /* String for array result */ + + if( argc<2 ) return; + p = jsonParseFuncArg(ctx, argv[0], 0); + if( p==0 ) return; + flags = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx)); + jsonStringInit(&jx, ctx); + if( argc>2 ){ + jsonAppendChar(&jx, '['); + } + for(i=1; i and ->> operators accept abbreviated PATH arguments. This + ** is mostly for compatibility with PostgreSQL, but also for + ** convenience. + ** + ** NUMBER ==> $[NUMBER] // PG compatible + ** LABEL ==> $.LABEL // PG compatible + ** [NUMBER] ==> $[NUMBER] // Not PG. Purely for convenience + ** + ** Updated 2024-05-27: If the NUMBER is negative, then PG counts from + ** the right of the array. Hence for negative NUMBER: + ** + ** NUMBER ==> $[#NUMBER] // PG compatible + */ + jsonStringInit(&jx, ctx); + if( sqlite3_value_type(argv[i])==SQLITE_INTEGER ){ + jsonAppendRawNZ(&jx, "[", 1); + if( zPath[0]=='-' ) jsonAppendRawNZ(&jx,"#",1); + jsonAppendRaw(&jx, zPath, nPath); + jsonAppendRawNZ(&jx, "]", 2); + }else if( jsonAllAlphanum(zPath, nPath) ){ + jsonAppendRawNZ(&jx, ".", 1); + jsonAppendRaw(&jx, zPath, nPath); + }else if( zPath[0]=='[' && nPath>=3 && zPath[nPath-1]==']' ){ + jsonAppendRaw(&jx, zPath, nPath); + }else{ + jsonAppendRawNZ(&jx, ".\"", 2); + jsonAppendRaw(&jx, zPath, nPath); + jsonAppendRawNZ(&jx, "\"", 1); + } + jsonStringTerminate(&jx); + j = jsonLookupStep(p, 0, jx.zBuf, 0); + jsonStringReset(&jx); + }else{ + jsonBadPathError(ctx, zPath); + goto json_extract_error; + } + if( jnBlob ){ + if( argc==2 ){ + if( flags & JSON_JSON ){ + jsonStringInit(&jx, ctx); + jsonTranslateBlobToText(p, j, &jx); + jsonReturnString(&jx, 0, 0); + jsonStringReset(&jx); + assert( (flags & JSON_BLOB)==0 ); + sqlite3_result_subtype(ctx, JSON_SUBTYPE); + }else{ + jsonReturnFromBlob(p, j, ctx, 0); + if( (flags & (JSON_SQL|JSON_BLOB))==0 + && (p->aBlob[j]&0x0f)>=JSONB_ARRAY + ){ + sqlite3_result_subtype(ctx, JSON_SUBTYPE); + } + } + }else{ + jsonAppendSeparator(&jx); + jsonTranslateBlobToText(p, j, &jx); + } + }else if( j==JSON_LOOKUP_NOTFOUND ){ + if( argc==2 ){ + goto json_extract_error; /* Return NULL if not found */ + }else{ + jsonAppendSeparator(&jx); + jsonAppendRawNZ(&jx, "null", 4); + } + }else if( j==JSON_LOOKUP_ERROR ){ + sqlite3_result_error(ctx, "malformed JSON", -1); + goto json_extract_error; + }else{ + jsonBadPathError(ctx, zPath); + goto json_extract_error; + } + } + if( argc>2 ){ + jsonAppendChar(&jx, ']'); + jsonReturnString(&jx, 0, 0); + if( (flags & JSON_BLOB)==0 ){ + sqlite3_result_subtype(ctx, JSON_SUBTYPE); + } + } +json_extract_error: + jsonStringReset(&jx); + jsonParseFree(p); + return; +} + +/* +** Return codes for jsonMergePatch() +*/ +#define JSON_MERGE_OK 0 /* Success */ +#define JSON_MERGE_BADTARGET 1 /* Malformed TARGET blob */ +#define JSON_MERGE_BADPATCH 2 /* Malformed PATCH blob */ +#define JSON_MERGE_OOM 3 /* Out-of-memory condition */ + +/* +** RFC-7396 MergePatch for two JSONB blobs. +** +** pTarget is the target. pPatch is the patch. The target is updated +** in place. The patch is read-only. +** +** The original RFC-7396 algorithm is this: +** +** define MergePatch(Target, Patch): +** if Patch is an Object: +** if Target is not an Object: +** Target = {} # Ignore the contents and set it to an empty Object +** for each Name/Value pair in Patch: +** if Value is null: +** if Name exists in Target: +** remove the Name/Value pair from Target +** else: +** Target[Name] = MergePatch(Target[Name], Value) +** return Target +** else: +** return Patch +** +** Here is an equivalent algorithm restructured to show the actual +** implementation: +** +** 01 define MergePatch(Target, Patch): +** 02 if Patch is not an Object: +** 03 return Patch +** 04 else: // if Patch is an Object +** 05 if Target is not an Object: +** 06 Target = {} +** 07 for each Name/Value pair in Patch: +** 08 if Name exists in Target: +** 09 if Value is null: +** 10 remove the Name/Value pair from Target +** 11 else +** 12 Target[name] = MergePatch(Target[Name], Value) +** 13 else if Value is not NULL: +** 14 if Value is not an Object: +** 15 Target[name] = Value +** 16 else: +** 17 Target[name] = MergePatch('{}',value) +** 18 return Target +** | +** ^---- Line numbers referenced in comments in the implementation +*/ +static int jsonMergePatch( + JsonParse *pTarget, /* The JSON parser that contains the TARGET */ + u32 iTarget, /* Index of TARGET in pTarget->aBlob[] */ + const JsonParse *pPatch, /* The PATCH */ + u32 iPatch /* Index of PATCH in pPatch->aBlob[] */ +){ + u8 x; /* Type of a single node */ + u32 n, sz=0; /* Return values from jsonbPayloadSize() */ + u32 iTCursor; /* Cursor position while scanning the target object */ + u32 iTStart; /* First label in the target object */ + u32 iTEndBE; /* Original first byte past end of target, before edit */ + u32 iTEnd; /* Current first byte past end of target */ + u8 eTLabel; /* Node type of the target label */ + u32 iTLabel = 0; /* Index of the label */ + u32 nTLabel = 0; /* Header size in bytes for the target label */ + u32 szTLabel = 0; /* Size of the target label payload */ + u32 iTValue = 0; /* Index of the target value */ + u32 nTValue = 0; /* Header size of the target value */ + u32 szTValue = 0; /* Payload size for the target value */ + + u32 iPCursor; /* Cursor position while scanning the patch */ + u32 iPEnd; /* First byte past the end of the patch */ + u8 ePLabel; /* Node type of the patch label */ + u32 iPLabel; /* Start of patch label */ + u32 nPLabel; /* Size of header on the patch label */ + u32 szPLabel; /* Payload size of the patch label */ + u32 iPValue; /* Start of patch value */ + u32 nPValue; /* Header size for the patch value */ + u32 szPValue; /* Payload size of the patch value */ + + assert( iTarget>=0 && iTargetnBlob ); + assert( iPatch>=0 && iPatchnBlob ); + x = pPatch->aBlob[iPatch] & 0x0f; + if( x!=JSONB_OBJECT ){ /* Algorithm line 02 */ + u32 szPatch; /* Total size of the patch, header+payload */ + u32 szTarget; /* Total size of the target, header+payload */ + n = jsonbPayloadSize(pPatch, iPatch, &sz); + szPatch = n+sz; + sz = 0; + n = jsonbPayloadSize(pTarget, iTarget, &sz); + szTarget = n+sz; + jsonBlobEdit(pTarget, iTarget, szTarget, pPatch->aBlob+iPatch, szPatch); + return pTarget->oom ? JSON_MERGE_OOM : JSON_MERGE_OK; /* Line 03 */ + } + x = pTarget->aBlob[iTarget] & 0x0f; + if( x!=JSONB_OBJECT ){ /* Algorithm line 05 */ + n = jsonbPayloadSize(pTarget, iTarget, &sz); + jsonBlobEdit(pTarget, iTarget+n, sz, 0, 0); + x = pTarget->aBlob[iTarget]; + pTarget->aBlob[iTarget] = (x & 0xf0) | JSONB_OBJECT; + } + n = jsonbPayloadSize(pPatch, iPatch, &sz); + if( NEVER(n==0) ) return JSON_MERGE_BADPATCH; + iPCursor = iPatch+n; + iPEnd = iPCursor+sz; + n = jsonbPayloadSize(pTarget, iTarget, &sz); + if( NEVER(n==0) ) return JSON_MERGE_BADTARGET; + iTStart = iTarget+n; + iTEndBE = iTStart+sz; + + while( iPCursoraBlob[iPCursor] & 0x0f; + if( ePLabelJSONB_TEXTRAW ){ + return JSON_MERGE_BADPATCH; + } + nPLabel = jsonbPayloadSize(pPatch, iPCursor, &szPLabel); + if( nPLabel==0 ) return JSON_MERGE_BADPATCH; + iPValue = iPCursor + nPLabel + szPLabel; + if( iPValue>=iPEnd ) return JSON_MERGE_BADPATCH; + nPValue = jsonbPayloadSize(pPatch, iPValue, &szPValue); + if( nPValue==0 ) return JSON_MERGE_BADPATCH; + iPCursor = iPValue + nPValue + szPValue; + if( iPCursor>iPEnd ) return JSON_MERGE_BADPATCH; + + iTCursor = iTStart; + iTEnd = iTEndBE + pTarget->delta; + while( iTCursoraBlob[iTCursor] & 0x0f; + if( eTLabelJSONB_TEXTRAW ){ + return JSON_MERGE_BADTARGET; + } + nTLabel = jsonbPayloadSize(pTarget, iTCursor, &szTLabel); + if( nTLabel==0 ) return JSON_MERGE_BADTARGET; + iTValue = iTLabel + nTLabel + szTLabel; + if( iTValue>=iTEnd ) return JSON_MERGE_BADTARGET; + nTValue = jsonbPayloadSize(pTarget, iTValue, &szTValue); + if( nTValue==0 ) return JSON_MERGE_BADTARGET; + if( iTValue + nTValue + szTValue > iTEnd ) return JSON_MERGE_BADTARGET; + isEqual = jsonLabelCompare( + (const char*)&pPatch->aBlob[iPLabel+nPLabel], + szPLabel, + (ePLabel==JSONB_TEXT || ePLabel==JSONB_TEXTRAW), + (const char*)&pTarget->aBlob[iTLabel+nTLabel], + szTLabel, + (eTLabel==JSONB_TEXT || eTLabel==JSONB_TEXTRAW)); + if( isEqual ) break; + iTCursor = iTValue + nTValue + szTValue; + } + x = pPatch->aBlob[iPValue] & 0x0f; + if( iTCursoroom) ) return JSON_MERGE_OOM; + }else{ + /* Algorithm line 12 */ + int rc, savedDelta = pTarget->delta; + pTarget->delta = 0; + rc = jsonMergePatch(pTarget, iTValue, pPatch, iPValue); + if( rc ) return rc; + pTarget->delta += savedDelta; + } + }else if( x>0 ){ /* Algorithm line 13 */ + /* No match and patch value is not NULL */ + u32 szNew = szPLabel+nPLabel; + if( (pPatch->aBlob[iPValue] & 0x0f)!=JSONB_OBJECT ){ /* Line 14 */ + jsonBlobEdit(pTarget, iTEnd, 0, 0, szPValue+nPValue+szNew); + if( pTarget->oom ) return JSON_MERGE_OOM; + memcpy(&pTarget->aBlob[iTEnd], &pPatch->aBlob[iPLabel], szNew); + memcpy(&pTarget->aBlob[iTEnd+szNew], + &pPatch->aBlob[iPValue], szPValue+nPValue); + }else{ + int rc, savedDelta; + jsonBlobEdit(pTarget, iTEnd, 0, 0, szNew+1); + if( pTarget->oom ) return JSON_MERGE_OOM; + memcpy(&pTarget->aBlob[iTEnd], &pPatch->aBlob[iPLabel], szNew); + pTarget->aBlob[iTEnd+szNew] = 0x00; + savedDelta = pTarget->delta; + pTarget->delta = 0; + rc = jsonMergePatch(pTarget, iTEnd+szNew,pPatch,iPValue); + if( rc ) return rc; + pTarget->delta += savedDelta; + } + } + } + if( pTarget->delta ) jsonAfterEditSizeAdjust(pTarget, iTarget); + return pTarget->oom ? JSON_MERGE_OOM : JSON_MERGE_OK; +} + + +/* +** Implementation of the json_mergepatch(JSON1,JSON2) function. Return a JSON +** object that is the result of running the RFC 7396 MergePatch() algorithm +** on the two arguments. +*/ +static void jsonPatchFunc( + sqlite3_context *ctx, + int argc, + sqlite3_value **argv +){ + JsonParse *pTarget; /* The TARGET */ + JsonParse *pPatch; /* The PATCH */ + int rc; /* Result code */ + + UNUSED_PARAMETER(argc); + assert( argc==2 ); + pTarget = jsonParseFuncArg(ctx, argv[0], JSON_EDITABLE); + if( pTarget==0 ) return; + pPatch = jsonParseFuncArg(ctx, argv[1], 0); + if( pPatch ){ + rc = jsonMergePatch(pTarget, 0, pPatch, 0); + if( rc==JSON_MERGE_OK ){ + jsonReturnParse(ctx, pTarget); + }else if( rc==JSON_MERGE_OOM ){ + sqlite3_result_error_nomem(ctx); + }else{ + sqlite3_result_error(ctx, "malformed JSON", -1); + } + jsonParseFree(pPatch); + } + jsonParseFree(pTarget); +} + + +/* +** Implementation of the json_object(NAME,VALUE,...) function. Return a JSON +** object that contains all name/value given in arguments. Or if any name +** is not a string or if any value is a BLOB, throw an error. +*/ +static void jsonObjectFunc( + sqlite3_context *ctx, + int argc, + sqlite3_value **argv +){ + int i; + JsonString jx; + const char *z; + u32 n; + + if( argc&1 ){ + sqlite3_result_error(ctx, "json_object() requires an even number " + "of arguments", -1); + return; + } + jsonStringInit(&jx, ctx); + jsonAppendChar(&jx, '{'); + for(i=0; i1 ? JSON_EDITABLE : 0); + if( p==0 ) return; + for(i=1; ieEdit = JEDIT_DEL; + p->delta = 0; + rc = jsonLookupStep(p, 0, zPath+1, 0); + if( JSON_LOOKUP_ISERROR(rc) ){ + if( rc==JSON_LOOKUP_NOTFOUND ){ + continue; /* No-op */ + }else if( rc==JSON_LOOKUP_PATHERROR ){ + jsonBadPathError(ctx, zPath); + }else{ + sqlite3_result_error(ctx, "malformed JSON", -1); + } + goto json_remove_done; + } + } + jsonReturnParse(ctx, p); + jsonParseFree(p); + return; + +json_remove_patherror: + jsonBadPathError(ctx, zPath); + +json_remove_done: + jsonParseFree(p); + return; +} + +/* +** json_replace(JSON, PATH, VALUE, ...) +** +** Replace the value at PATH with VALUE. If PATH does not already exist, +** this routine is a no-op. If JSON or PATH is malformed, throw an error. +*/ +static void jsonReplaceFunc( + sqlite3_context *ctx, + int argc, + sqlite3_value **argv +){ + if( argc<1 ) return; + if( (argc&1)==0 ) { + jsonWrongNumArgs(ctx, "replace"); + return; + } + jsonInsertIntoBlob(ctx, argc, argv, JEDIT_REPL); +} + + +/* +** json_set(JSON, PATH, VALUE, ...) +** +** Set the value at PATH to VALUE. Create the PATH if it does not already +** exist. Overwrite existing values that do exist. +** If JSON or PATH is malformed, throw an error. +** +** json_insert(JSON, PATH, VALUE, ...) +** +** Create PATH and initialize it to VALUE. If PATH already exists, this +** routine is a no-op. If JSON or PATH is malformed, throw an error. +*/ +static void jsonSetFunc( + sqlite3_context *ctx, + int argc, + sqlite3_value **argv +){ + + int flags = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx)); + int bIsSet = (flags&JSON_ISSET)!=0; + + if( argc<1 ) return; + if( (argc&1)==0 ) { + jsonWrongNumArgs(ctx, bIsSet ? "set" : "insert"); + return; + } + jsonInsertIntoBlob(ctx, argc, argv, bIsSet ? JEDIT_SET : JEDIT_INS); +} + +/* +** json_type(JSON) +** json_type(JSON, PATH) +** +** Return the top-level "type" of a JSON string. json_type() raises an +** error if either the JSON or PATH inputs are not well-formed. +*/ +static void jsonTypeFunc( + sqlite3_context *ctx, + int argc, + sqlite3_value **argv +){ + JsonParse *p; /* The parse */ + const char *zPath = 0; + u32 i; + + p = jsonParseFuncArg(ctx, argv[0], 0); + if( p==0 ) return; + if( argc==2 ){ + zPath = (const char*)sqlite3_value_text(argv[1]); + if( zPath==0 ) goto json_type_done; + if( zPath[0]!='$' ){ + jsonBadPathError(ctx, zPath); + goto json_type_done; + } + i = jsonLookupStep(p, 0, zPath+1, 0); + if( JSON_LOOKUP_ISERROR(i) ){ + if( i==JSON_LOOKUP_NOTFOUND ){ + /* no-op */ + }else if( i==JSON_LOOKUP_PATHERROR ){ + jsonBadPathError(ctx, zPath); + }else{ + sqlite3_result_error(ctx, "malformed JSON", -1); + } + goto json_type_done; + } + }else{ + i = 0; + } + sqlite3_result_text(ctx, jsonbType[p->aBlob[i]&0x0f], -1, SQLITE_STATIC); +json_type_done: + jsonParseFree(p); +} + +/* +** json_pretty(JSON) +** json_pretty(JSON, INDENT) +** +** Return text that is a pretty-printed rendering of the input JSON. +** If the argument is not valid JSON, return NULL. +** +** The INDENT argument is text that is used for indentation. If omitted, +** it defaults to four spaces (the same as PostgreSQL). +*/ +static void jsonPrettyFunc( + sqlite3_context *ctx, + int argc, + sqlite3_value **argv +){ + JsonString s; /* The output string */ + JsonPretty x; /* Pretty printing context */ + + memset(&x, 0, sizeof(x)); + x.pParse = jsonParseFuncArg(ctx, argv[0], 0); + if( x.pParse==0 ) return; + x.pOut = &s; + jsonStringInit(&s, ctx); + if( argc==1 || (x.zIndent = (const char*)sqlite3_value_text(argv[1]))==0 ){ + x.zIndent = " "; + x.szIndent = 4; + }else{ + x.szIndent = (u32)strlen(x.zIndent); + } + jsonTranslateBlobToPrettyText(&x, 0); + jsonReturnString(&s, 0, 0); + jsonParseFree(x.pParse); +} + +/* +** json_valid(JSON) +** json_valid(JSON, FLAGS) +** +** Check the JSON argument to see if it is well-formed. The FLAGS argument +** encodes the various constraints on what is meant by "well-formed": +** +** 0x01 Canonical RFC-8259 JSON text +** 0x02 JSON text with optional JSON-5 extensions +** 0x04 Superficially appears to be JSONB +** 0x08 Strictly well-formed JSONB +** +** If the FLAGS argument is omitted, it defaults to 1. Useful values for +** FLAGS include: +** +** 1 Strict canonical JSON text +** 2 JSON text perhaps with JSON-5 extensions +** 4 Superficially appears to be JSONB +** 5 Canonical JSON text or superficial JSONB +** 6 JSON-5 text or superficial JSONB +** 8 Strict JSONB +** 9 Canonical JSON text or strict JSONB +** 10 JSON-5 text or strict JSONB +** +** Other flag combinations are redundant. For example, every canonical +** JSON text is also well-formed JSON-5 text, so FLAG values 2 and 3 +** are the same. Similarly, any input that passes a strict JSONB validation +** will also pass the superficial validation so 12 through 15 are the same +** as 8 through 11 respectively. +** +** This routine runs in linear time to validate text and when doing strict +** JSONB validation. Superficial JSONB validation is constant time, +** assuming the BLOB is already in memory. The performance advantage +** of superficial JSONB validation is why that option is provided. +** Application developers can choose to do fast superficial validation or +** slower strict validation, according to their specific needs. +** +** Only the lower four bits of the FLAGS argument are currently used. +** Higher bits are reserved for future expansion. To facilitate +** compatibility, the current implementation raises an error if any bit +** in FLAGS is set other than the lower four bits. +** +** The original circa 2015 implementation of the JSON routines in +** SQLite only supported canonical RFC-8259 JSON text and the json_valid() +** function only accepted one argument. That is why the default value +** for the FLAGS argument is 1, since FLAGS=1 causes this routine to only +** recognize canonical RFC-8259 JSON text as valid. The extra FLAGS +** argument was added when the JSON routines were extended to support +** JSON5-like extensions and binary JSONB stored in BLOBs. +** +** Return Values: +** +** * Raise an error if FLAGS is outside the range of 1 to 15. +** * Return NULL if the input is NULL +** * Return 1 if the input is well-formed. +** * Return 0 if the input is not well-formed. +*/ +static void jsonValidFunc( + sqlite3_context *ctx, + int argc, + sqlite3_value **argv +){ + JsonParse *p; /* The parse */ + u8 flags = 1; + u8 res = 0; + if( argc==2 ){ + i64 f = sqlite3_value_int64(argv[1]); + if( f<1 || f>15 ){ + sqlite3_result_error(ctx, "FLAGS parameter to json_valid() must be" + " between 1 and 15", -1); + return; + } + flags = f & 0x0f; + } + switch( sqlite3_value_type(argv[0]) ){ + case SQLITE_NULL: { +#ifdef SQLITE_LEGACY_JSON_VALID + /* Incorrect legacy behavior was to return FALSE for a NULL input */ + sqlite3_result_int(ctx, 0); +#endif + return; + } + case SQLITE_BLOB: { + JsonParse py; + memset(&py, 0, sizeof(py)); + if( jsonArgIsJsonb(argv[0], &py) ){ + if( flags & 0x04 ){ + /* Superficial checking only - accomplished by the + ** jsonArgIsJsonb() call above. */ + res = 1; + }else if( flags & 0x08 ){ + /* Strict checking. Check by translating BLOB->TEXT->BLOB. If + ** no errors occur, call that a "strict check". */ + res = 0==jsonbValidityCheck(&py, 0, py.nBlob, 1); + } + break; + } + /* Fall through into interpreting the input as text. See note + ** above at tag-20240123-a. */ + /* no break */ deliberate_fall_through + } + default: { + JsonParse px; + if( (flags & 0x3)==0 ) break; + memset(&px, 0, sizeof(px)); + + p = jsonParseFuncArg(ctx, argv[0], JSON_KEEPERROR); + if( p ){ + if( p->oom ){ + sqlite3_result_error_nomem(ctx); + }else if( p->nErr ){ + /* no-op */ + }else if( (flags & 0x02)!=0 || p->hasNonstd==0 ){ + res = 1; + } + jsonParseFree(p); + }else{ + sqlite3_result_error_nomem(ctx); + } + break; + } + } + sqlite3_result_int(ctx, res); +} + +/* +** json_error_position(JSON) +** +** If the argument is NULL, return NULL +** +** If the argument is BLOB, do a full validity check and return non-zero +** if the check fails. The return value is the approximate 1-based offset +** to the byte of the element that contains the first error. +** +** Otherwise interpret the argument is TEXT (even if it is numeric) and +** return the 1-based character position for where the parser first recognized +** that the input was not valid JSON, or return 0 if the input text looks +** ok. JSON-5 extensions are accepted. +*/ +static void jsonErrorFunc( + sqlite3_context *ctx, + int argc, + sqlite3_value **argv +){ + i64 iErrPos = 0; /* Error position to be returned */ + JsonParse s; + + assert( argc==1 ); + UNUSED_PARAMETER(argc); + memset(&s, 0, sizeof(s)); + s.db = sqlite3_context_db_handle(ctx); + if( jsonArgIsJsonb(argv[0], &s) ){ + iErrPos = (i64)jsonbValidityCheck(&s, 0, s.nBlob, 1); + }else{ + s.zJson = (char*)sqlite3_value_text(argv[0]); + if( s.zJson==0 ) return; /* NULL input or OOM */ + s.nJson = sqlite3_value_bytes(argv[0]); + if( jsonConvertTextToBlob(&s,0) ){ + if( s.oom ){ + iErrPos = -1; + }else{ + /* Convert byte-offset s.iErr into a character offset */ + u32 k; + assert( s.zJson!=0 ); /* Because s.oom is false */ + for(k=0; kzBuf==0 ){ + jsonStringInit(pStr, ctx); + jsonAppendChar(pStr, '['); + }else if( pStr->nUsed>1 ){ + jsonAppendChar(pStr, ','); + } + pStr->pCtx = ctx; + jsonAppendSqlValue(pStr, argv[0]); + } +} +static void jsonArrayCompute(sqlite3_context *ctx, int isFinal){ + JsonString *pStr; + pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0); + if( pStr ){ + int flags; + pStr->pCtx = ctx; + jsonAppendChar(pStr, ']'); + flags = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx)); + if( pStr->eErr ){ + jsonReturnString(pStr, 0, 0); + return; + }else if( flags & JSON_BLOB ){ + jsonReturnStringAsBlob(pStr); + if( isFinal ){ + if( !pStr->bStatic ) sqlite3RCStrUnref(pStr->zBuf); + }else{ + jsonStringTrimOneChar(pStr); + } + return; + }else if( isFinal ){ + sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, + pStr->bStatic ? SQLITE_TRANSIENT : + sqlite3RCStrUnref); + pStr->bStatic = 1; + }else{ + sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT); + jsonStringTrimOneChar(pStr); + } + }else{ + sqlite3_result_text(ctx, "[]", 2, SQLITE_STATIC); + } + sqlite3_result_subtype(ctx, JSON_SUBTYPE); +} +static void jsonArrayValue(sqlite3_context *ctx){ + jsonArrayCompute(ctx, 0); +} +static void jsonArrayFinal(sqlite3_context *ctx){ + jsonArrayCompute(ctx, 1); +} + +#ifndef SQLITE_OMIT_WINDOWFUNC +/* +** This method works for both json_group_array() and json_group_object(). +** It works by removing the first element of the group by searching forward +** to the first comma (",") that is not within a string and deleting all +** text through that comma. +*/ +static void jsonGroupInverse( + sqlite3_context *ctx, + int argc, + sqlite3_value **argv +){ + unsigned int i; + int inStr = 0; + int nNest = 0; + char *z; + char c; + JsonString *pStr; + UNUSED_PARAMETER(argc); + UNUSED_PARAMETER(argv); + pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0); +#ifdef NEVER + /* pStr is always non-NULL since jsonArrayStep() or jsonObjectStep() will + ** always have been called to initialize it */ + if( NEVER(!pStr) ) return; +#endif + z = pStr->zBuf; + for(i=1; inUsed && ((c = z[i])!=',' || inStr || nNest); i++){ + if( c=='"' ){ + inStr = !inStr; + }else if( c=='\\' ){ + i++; + }else if( !inStr ){ + if( c=='{' || c=='[' ) nNest++; + if( c=='}' || c==']' ) nNest--; + } + } + if( inUsed ){ + pStr->nUsed -= i; + memmove(&z[1], &z[i+1], (size_t)pStr->nUsed-1); + z[pStr->nUsed] = 0; + }else{ + pStr->nUsed = 1; + } +} +#else +# define jsonGroupInverse 0 +#endif + + +/* +** json_group_obj(NAME,VALUE) +** +** Return a JSON object composed of all names and values in the aggregate. +*/ +static void jsonObjectStep( + sqlite3_context *ctx, + int argc, + sqlite3_value **argv +){ + JsonString *pStr; + const char *z; + u32 n; + UNUSED_PARAMETER(argc); + pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr)); + if( pStr ){ + z = (const char*)sqlite3_value_text(argv[0]); + n = sqlite3Strlen30(z); + if( pStr->zBuf==0 ){ + jsonStringInit(pStr, ctx); + jsonAppendChar(pStr, '{'); + }else if( pStr->nUsed>1 && z!=0 ){ + jsonAppendChar(pStr, ','); + } + pStr->pCtx = ctx; + if( z!=0 ){ + jsonAppendString(pStr, z, n); + jsonAppendChar(pStr, ':'); + jsonAppendSqlValue(pStr, argv[1]); + } + } +} +static void jsonObjectCompute(sqlite3_context *ctx, int isFinal){ + JsonString *pStr; + pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0); + if( pStr ){ + int flags; + jsonAppendChar(pStr, '}'); + pStr->pCtx = ctx; + flags = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx)); + if( pStr->eErr ){ + jsonReturnString(pStr, 0, 0); + return; + }else if( flags & JSON_BLOB ){ + jsonReturnStringAsBlob(pStr); + if( isFinal ){ + if( !pStr->bStatic ) sqlite3RCStrUnref(pStr->zBuf); + }else{ + jsonStringTrimOneChar(pStr); + } + return; + }else if( isFinal ){ + sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, + pStr->bStatic ? SQLITE_TRANSIENT : + sqlite3RCStrUnref); + pStr->bStatic = 1; + }else{ + sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT); + jsonStringTrimOneChar(pStr); + } + }else{ + sqlite3_result_text(ctx, "{}", 2, SQLITE_STATIC); + } + sqlite3_result_subtype(ctx, JSON_SUBTYPE); +} +static void jsonObjectValue(sqlite3_context *ctx){ + jsonObjectCompute(ctx, 0); +} +static void jsonObjectFinal(sqlite3_context *ctx){ + jsonObjectCompute(ctx, 1); +} + + + +#ifndef SQLITE_OMIT_VIRTUALTABLE +/**************************************************************************** +** The json_each virtual table +****************************************************************************/ +typedef struct JsonParent JsonParent; +struct JsonParent { + u32 iHead; /* Start of object or array */ + u32 iValue; /* Start of the value */ + u32 iEnd; /* First byte past the end */ + u32 nPath; /* Length of path */ + i64 iKey; /* Key for JSONB_ARRAY */ +}; + +typedef struct JsonEachCursor JsonEachCursor; +struct JsonEachCursor { + sqlite3_vtab_cursor base; /* Base class - must be first */ + u32 iRowid; /* The rowid */ + u32 i; /* Index in sParse.aBlob[] of current row */ + u32 iEnd; /* EOF when i equals or exceeds this value */ + u32 nRoot; /* Size of the root path in bytes */ + u8 eType; /* Type of the container for element i */ + u8 bRecursive; /* True for json_tree(). False for json_each() */ + u8 eMode; /* 1 for json_each(). 2 for jsonb_each() */ + u32 nParent; /* Current nesting depth */ + u32 nParentAlloc; /* Space allocated for aParent[] */ + JsonParent *aParent; /* Parent elements of i */ + sqlite3 *db; /* Database connection */ + JsonString path; /* Current path */ + JsonParse sParse; /* Parse of the input JSON */ +}; +typedef struct JsonEachConnection JsonEachConnection; +struct JsonEachConnection { + sqlite3_vtab base; /* Base class - must be first */ + sqlite3 *db; /* Database connection */ + u8 eMode; /* 1 for json_each(). 2 for jsonb_each() */ + u8 bRecursive; /* True for json_tree(). False for json_each() */ +}; + + +/* Constructor for the json_each virtual table */ +static int jsonEachConnect( + sqlite3 *db, + void *pAux, + int argc, const char *const*argv, + sqlite3_vtab **ppVtab, + char **pzErr +){ + JsonEachConnection *pNew; + int rc; + +/* Column numbers */ +#define JEACH_KEY 0 +#define JEACH_VALUE 1 +#define JEACH_TYPE 2 +#define JEACH_ATOM 3 +#define JEACH_ID 4 +#define JEACH_PARENT 5 +#define JEACH_FULLKEY 6 +#define JEACH_PATH 7 +/* The xBestIndex method assumes that the JSON and ROOT columns are +** the last two columns in the table. Should this ever changes, be +** sure to update the xBestIndex method. */ +#define JEACH_JSON 8 +#define JEACH_ROOT 9 + + UNUSED_PARAMETER(pzErr); + UNUSED_PARAMETER(argv); + UNUSED_PARAMETER(argc); + UNUSED_PARAMETER(pAux); + rc = sqlite3_declare_vtab(db, + "CREATE TABLE x(key,value,type,atom,id,parent,fullkey,path," + "json HIDDEN,root HIDDEN)"); + if( rc==SQLITE_OK ){ + pNew = (JsonEachConnection*)sqlite3DbMallocZero(db, sizeof(*pNew)); + *ppVtab = (sqlite3_vtab*)pNew; + if( pNew==0 ) return SQLITE_NOMEM; + sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS); + pNew->db = db; + pNew->eMode = argv[0][4]=='b' ? 2 : 1; + pNew->bRecursive = argv[0][4+pNew->eMode]=='t'; + } + return rc; +} + +/* destructor for json_each virtual table */ +static int jsonEachDisconnect(sqlite3_vtab *pVtab){ + JsonEachConnection *p = (JsonEachConnection*)pVtab; + sqlite3DbFree(p->db, pVtab); + return SQLITE_OK; +} + +/* constructor for a JsonEachCursor object for json_each()/json_tree(). */ +static int jsonEachOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){ + JsonEachConnection *pVtab = (JsonEachConnection*)p; + JsonEachCursor *pCur; + + UNUSED_PARAMETER(p); + pCur = sqlite3DbMallocZero(pVtab->db, sizeof(*pCur)); + if( pCur==0 ) return SQLITE_NOMEM; + pCur->db = pVtab->db; + pCur->eMode = pVtab->eMode; + pCur->bRecursive = pVtab->bRecursive; + jsonStringZero(&pCur->path); + *ppCursor = &pCur->base; + return SQLITE_OK; +} + +/* Reset a JsonEachCursor back to its original state. Free any memory +** held. */ +static void jsonEachCursorReset(JsonEachCursor *p){ + jsonParseReset(&p->sParse); + jsonStringReset(&p->path); + sqlite3DbFree(p->db, p->aParent); + p->iRowid = 0; + p->i = 0; + p->aParent = 0; + p->nParent = 0; + p->nParentAlloc = 0; + p->iEnd = 0; + p->eType = 0; +} + +/* Destructor for a jsonEachCursor object */ +static int jsonEachClose(sqlite3_vtab_cursor *cur){ + JsonEachCursor *p = (JsonEachCursor*)cur; + jsonEachCursorReset(p); + + sqlite3DbFree(p->db, cur); + return SQLITE_OK; +} + +/* Return TRUE if the jsonEachCursor object has been advanced off the end +** of the JSON object */ +static int jsonEachEof(sqlite3_vtab_cursor *cur){ + JsonEachCursor *p = (JsonEachCursor*)cur; + return p->i >= p->iEnd; +} + +/* +** If the cursor is currently pointing at the label of a object entry, +** then return the index of the value. For all other cases, return the +** current pointer position, which is the value. +*/ +static int jsonSkipLabel(JsonEachCursor *p){ + if( p->eType==JSONB_OBJECT ){ + u32 sz = 0; + u32 n = jsonbPayloadSize(&p->sParse, p->i, &sz); + return p->i + n + sz; + }else{ + return p->i; + } +} + +/* +** Append the path name for the current element. +*/ +static void jsonAppendPathName(JsonEachCursor *p){ + assert( p->nParent>0 ); + assert( p->eType==JSONB_ARRAY || p->eType==JSONB_OBJECT ); + if( p->eType==JSONB_ARRAY ){ + jsonPrintf(30, &p->path, "[%lld]", p->aParent[p->nParent-1].iKey); + }else{ + u32 n, sz = 0, k, i; + const char *z; + int needQuote = 0; + n = jsonbPayloadSize(&p->sParse, p->i, &sz); + k = p->i + n; + z = (const char*)&p->sParse.aBlob[k]; + if( sz==0 || !sqlite3Isalpha(z[0]) ){ + needQuote = 1; + }else{ + for(i=0; ipath,".\"%.*s\"", sz, z); + }else{ + jsonPrintf(sz+2,&p->path,".%.*s", sz, z); + } + } +} + +/* Advance the cursor to the next element for json_tree() */ +static int jsonEachNext(sqlite3_vtab_cursor *cur){ + JsonEachCursor *p = (JsonEachCursor*)cur; + int rc = SQLITE_OK; + if( p->bRecursive ){ + u8 x; + u8 levelChange = 0; + u32 n, sz = 0; + u32 i = jsonSkipLabel(p); + x = p->sParse.aBlob[i] & 0x0f; + n = jsonbPayloadSize(&p->sParse, i, &sz); + if( x==JSONB_OBJECT || x==JSONB_ARRAY ){ + JsonParent *pParent; + if( p->nParent>=p->nParentAlloc ){ + JsonParent *pNew; + u64 nNew; + nNew = p->nParentAlloc*2 + 3; + pNew = sqlite3DbRealloc(p->db, p->aParent, sizeof(JsonParent)*nNew); + if( pNew==0 ) return SQLITE_NOMEM; + p->nParentAlloc = (u32)nNew; + p->aParent = pNew; + } + levelChange = 1; + pParent = &p->aParent[p->nParent]; + pParent->iHead = p->i; + pParent->iValue = i; + pParent->iEnd = i + n + sz; + pParent->iKey = -1; + pParent->nPath = (u32)p->path.nUsed; + if( p->eType && p->nParent ){ + jsonAppendPathName(p); + if( p->path.eErr ) rc = SQLITE_NOMEM; + } + p->nParent++; + p->i = i + n; + }else{ + p->i = i + n + sz; + } + while( p->nParent>0 && p->i >= p->aParent[p->nParent-1].iEnd ){ + p->nParent--; + p->path.nUsed = p->aParent[p->nParent].nPath; + levelChange = 1; + } + if( levelChange ){ + if( p->nParent>0 ){ + JsonParent *pParent = &p->aParent[p->nParent-1]; + u32 iVal = pParent->iValue; + p->eType = p->sParse.aBlob[iVal] & 0x0f; + }else{ + p->eType = 0; + } + } + }else{ + u32 n, sz = 0; + u32 i = jsonSkipLabel(p); + n = jsonbPayloadSize(&p->sParse, i, &sz); + p->i = i + n + sz; + } + if( p->eType==JSONB_ARRAY && p->nParent ){ + p->aParent[p->nParent-1].iKey++; + } + p->iRowid++; + return rc; +} + +/* Length of the path for rowid==0 in bRecursive mode. +*/ +static int jsonEachPathLength(JsonEachCursor *p){ + u32 n = p->path.nUsed; + char *z = p->path.zBuf; + if( p->iRowid==0 && p->bRecursive && n>=2 ){ + while( n>1 ){ + n--; + if( z[n]=='[' || z[n]=='.' ){ + u32 x, sz = 0; + char cSaved = z[n]; + z[n] = 0; + assert( p->sParse.eEdit==0 ); + x = jsonLookupStep(&p->sParse, 0, z+1, 0); + z[n] = cSaved; + if( JSON_LOOKUP_ISERROR(x) ) continue; + if( x + jsonbPayloadSize(&p->sParse, x, &sz) == p->i ) break; + } + } + } + return n; +} + +/* Return the value of a column */ +static int jsonEachColumn( + sqlite3_vtab_cursor *cur, /* The cursor */ + sqlite3_context *ctx, /* First argument to sqlite3_result_...() */ + int iColumn /* Which column to return */ +){ + JsonEachCursor *p = (JsonEachCursor*)cur; + switch( iColumn ){ + case JEACH_KEY: { + if( p->nParent==0 ){ + u32 n, j; + if( p->nRoot==1 ) break; + j = jsonEachPathLength(p); + n = p->nRoot - j; + if( n==0 ){ + break; + }else if( p->path.zBuf[j]=='[' ){ + i64 x; + sqlite3Atoi64(&p->path.zBuf[j+1], &x, n-1, SQLITE_UTF8); + sqlite3_result_int64(ctx, x); + }else if( p->path.zBuf[j+1]=='"' ){ + sqlite3_result_text(ctx, &p->path.zBuf[j+2], n-3, SQLITE_TRANSIENT); + }else{ + sqlite3_result_text(ctx, &p->path.zBuf[j+1], n-1, SQLITE_TRANSIENT); + } + break; + } + if( p->eType==JSONB_OBJECT ){ + jsonReturnFromBlob(&p->sParse, p->i, ctx, 1); + }else{ + assert( p->eType==JSONB_ARRAY ); + sqlite3_result_int64(ctx, p->aParent[p->nParent-1].iKey); + } + break; + } + case JEACH_VALUE: { + u32 i = jsonSkipLabel(p); + jsonReturnFromBlob(&p->sParse, i, ctx, p->eMode); + if( (p->sParse.aBlob[i] & 0x0f)>=JSONB_ARRAY ){ + sqlite3_result_subtype(ctx, JSON_SUBTYPE); + } + break; + } + case JEACH_TYPE: { + u32 i = jsonSkipLabel(p); + u8 eType = p->sParse.aBlob[i] & 0x0f; + sqlite3_result_text(ctx, jsonbType[eType], -1, SQLITE_STATIC); + break; + } + case JEACH_ATOM: { + u32 i = jsonSkipLabel(p); + if( (p->sParse.aBlob[i] & 0x0f)sParse, i, ctx, 1); + } + break; + } + case JEACH_ID: { + sqlite3_result_int64(ctx, (sqlite3_int64)p->i); + break; + } + case JEACH_PARENT: { + if( p->nParent>0 && p->bRecursive ){ + sqlite3_result_int64(ctx, p->aParent[p->nParent-1].iHead); + } + break; + } + case JEACH_FULLKEY: { + u64 nBase = p->path.nUsed; + if( p->nParent ) jsonAppendPathName(p); + sqlite3_result_text64(ctx, p->path.zBuf, p->path.nUsed, + SQLITE_TRANSIENT, SQLITE_UTF8); + p->path.nUsed = nBase; + break; + } + case JEACH_PATH: { + u32 n = jsonEachPathLength(p); + sqlite3_result_text64(ctx, p->path.zBuf, n, + SQLITE_TRANSIENT, SQLITE_UTF8); + break; + } + default: { + sqlite3_result_text(ctx, p->path.zBuf, p->nRoot, SQLITE_STATIC); + break; + } + case JEACH_JSON: { + if( p->sParse.zJson==0 ){ + sqlite3_result_blob(ctx, p->sParse.aBlob, p->sParse.nBlob, + SQLITE_TRANSIENT); + }else{ + sqlite3_result_text(ctx, p->sParse.zJson, -1, SQLITE_TRANSIENT); + } + break; + } + } + return SQLITE_OK; +} + +/* Return the current rowid value */ +static int jsonEachRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ + JsonEachCursor *p = (JsonEachCursor*)cur; + *pRowid = p->iRowid; + return SQLITE_OK; +} + +/* The query strategy is to look for an equality constraint on the json +** column. Without such a constraint, the table cannot operate. idxNum is +** 1 if the constraint is found, 3 if the constraint and zRoot are found, +** and 0 otherwise. +*/ +static int jsonEachBestIndex( + sqlite3_vtab *tab, + sqlite3_index_info *pIdxInfo +){ + int i; /* Loop counter or computed array index */ + int aIdx[2]; /* Index of constraints for JSON and ROOT */ + int unusableMask = 0; /* Mask of unusable JSON and ROOT constraints */ + int idxMask = 0; /* Mask of usable == constraints JSON and ROOT */ + const struct sqlite3_index_constraint *pConstraint; + + /* This implementation assumes that JSON and ROOT are the last two + ** columns in the table */ + assert( JEACH_ROOT == JEACH_JSON+1 ); + UNUSED_PARAMETER(tab); + aIdx[0] = aIdx[1] = -1; + pConstraint = pIdxInfo->aConstraint; + for(i=0; inConstraint; i++, pConstraint++){ + int iCol; + int iMask; + if( pConstraint->iColumn < JEACH_JSON ) continue; + iCol = pConstraint->iColumn - JEACH_JSON; + assert( iCol==0 || iCol==1 ); + testcase( iCol==0 ); + iMask = 1 << iCol; + if( pConstraint->usable==0 ){ + unusableMask |= iMask; + }else if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){ + aIdx[iCol] = i; + idxMask |= iMask; + } + } + if( pIdxInfo->nOrderBy>0 + && pIdxInfo->aOrderBy[0].iColumn<0 + && pIdxInfo->aOrderBy[0].desc==0 + ){ + pIdxInfo->orderByConsumed = 1; + } + + if( (unusableMask & ~idxMask)!=0 ){ + /* If there are any unusable constraints on JSON or ROOT, then reject + ** this entire plan */ + return SQLITE_CONSTRAINT; + } + if( aIdx[0]<0 ){ + /* No JSON input. Leave estimatedCost at the huge value that it was + ** initialized to to discourage the query planner from selecting this + ** plan. */ + pIdxInfo->idxNum = 0; + }else{ + pIdxInfo->estimatedCost = 1.0; + i = aIdx[0]; + pIdxInfo->aConstraintUsage[i].argvIndex = 1; + pIdxInfo->aConstraintUsage[i].omit = 1; + if( aIdx[1]<0 ){ + pIdxInfo->idxNum = 1; /* Only JSON supplied. Plan 1 */ + }else{ + i = aIdx[1]; + pIdxInfo->aConstraintUsage[i].argvIndex = 2; + pIdxInfo->aConstraintUsage[i].omit = 1; + pIdxInfo->idxNum = 3; /* Both JSON and ROOT are supplied. Plan 3 */ + } + } + return SQLITE_OK; +} + +/* Start a search on a new JSON string */ +static int jsonEachFilter( + sqlite3_vtab_cursor *cur, + int idxNum, const char *idxStr, + int argc, sqlite3_value **argv +){ + JsonEachCursor *p = (JsonEachCursor*)cur; + const char *zRoot = 0; + u32 i, n, sz; + + UNUSED_PARAMETER(idxStr); + UNUSED_PARAMETER(argc); + jsonEachCursorReset(p); + if( idxNum==0 ) return SQLITE_OK; + memset(&p->sParse, 0, sizeof(p->sParse)); + p->sParse.nJPRef = 1; + p->sParse.db = p->db; + if( jsonArgIsJsonb(argv[0], &p->sParse) ){ + /* We have JSONB */ + }else{ + p->sParse.zJson = (char*)sqlite3_value_text(argv[0]); + p->sParse.nJson = sqlite3_value_bytes(argv[0]); + if( p->sParse.zJson==0 ){ + p->i = p->iEnd = 0; + return SQLITE_OK; + } + if( jsonConvertTextToBlob(&p->sParse, 0) ){ + if( p->sParse.oom ){ + return SQLITE_NOMEM; + } + goto json_each_malformed_input; + } + } + if( idxNum==3 ){ + zRoot = (const char*)sqlite3_value_text(argv[1]); + if( zRoot==0 ) return SQLITE_OK; + if( zRoot[0]!='$' ){ + sqlite3_free(cur->pVtab->zErrMsg); + cur->pVtab->zErrMsg = jsonBadPathError(0, zRoot); + jsonEachCursorReset(p); + return cur->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM; + } + p->nRoot = sqlite3Strlen30(zRoot); + if( zRoot[1]==0 ){ + i = p->i = 0; + p->eType = 0; + }else{ + i = jsonLookupStep(&p->sParse, 0, zRoot+1, 0); + if( JSON_LOOKUP_ISERROR(i) ){ + if( i==JSON_LOOKUP_NOTFOUND ){ + p->i = 0; + p->eType = 0; + p->iEnd = 0; + return SQLITE_OK; + } + sqlite3_free(cur->pVtab->zErrMsg); + cur->pVtab->zErrMsg = jsonBadPathError(0, zRoot); + jsonEachCursorReset(p); + return cur->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM; + } + if( p->sParse.iLabel ){ + p->i = p->sParse.iLabel; + p->eType = JSONB_OBJECT; + }else{ + p->i = i; + p->eType = JSONB_ARRAY; + } + } + jsonAppendRaw(&p->path, zRoot, p->nRoot); + }else{ + i = p->i = 0; + p->eType = 0; + p->nRoot = 1; + jsonAppendRaw(&p->path, "$", 1); + } + p->nParent = 0; + n = jsonbPayloadSize(&p->sParse, i, &sz); + p->iEnd = i+n+sz; + if( (p->sParse.aBlob[i] & 0x0f)>=JSONB_ARRAY && !p->bRecursive ){ + p->i = i + n; + p->eType = p->sParse.aBlob[i] & 0x0f; + p->aParent = sqlite3DbMallocZero(p->db, sizeof(JsonParent)); + if( p->aParent==0 ) return SQLITE_NOMEM; + p->nParent = 1; + p->nParentAlloc = 1; + p->aParent[0].iKey = 0; + p->aParent[0].iEnd = p->iEnd; + p->aParent[0].iHead = p->i; + p->aParent[0].iValue = i; + } + return SQLITE_OK; + +json_each_malformed_input: + sqlite3_free(cur->pVtab->zErrMsg); + cur->pVtab->zErrMsg = sqlite3_mprintf("malformed JSON"); + jsonEachCursorReset(p); + return cur->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM; +} + +/* The methods of the json_each virtual table */ +static sqlite3_module jsonEachModule = { + 0, /* iVersion */ + 0, /* xCreate */ + jsonEachConnect, /* xConnect */ + jsonEachBestIndex, /* xBestIndex */ + jsonEachDisconnect, /* xDisconnect */ + 0, /* xDestroy */ + jsonEachOpen, /* xOpen - open a cursor */ + jsonEachClose, /* xClose - close a cursor */ + jsonEachFilter, /* xFilter - configure scan constraints */ + jsonEachNext, /* xNext - advance a cursor */ + jsonEachEof, /* xEof - check for end of scan */ + jsonEachColumn, /* xColumn - read data */ + jsonEachRowid, /* xRowid - read data */ + 0, /* xUpdate */ + 0, /* xBegin */ + 0, /* xSync */ + 0, /* xCommit */ + 0, /* xRollback */ + 0, /* xFindMethod */ + 0, /* xRename */ + 0, /* xSavepoint */ + 0, /* xRelease */ + 0, /* xRollbackTo */ + 0, /* xShadowName */ + 0 /* xIntegrity */ +}; +#endif /* SQLITE_OMIT_VIRTUALTABLE */ +#endif /* !defined(SQLITE_OMIT_JSON) */ + +/* +** Register JSON functions. +*/ +void sqlite3RegisterJsonFunctions(void){ +#ifndef SQLITE_OMIT_JSON + static FuncDef aJsonFunc[] = { + /* sqlite3_result_subtype() ----, ,--- sqlite3_value_subtype() */ + /* | | */ + /* Uses cache ------, | | ,---- Returns JSONB */ + /* | | | | */ + /* Number of arguments ---, | | | | ,--- Flags */ + /* | | | | | | */ + JFUNCTION(json, 1,1,1, 0,0,0, jsonRemoveFunc), + JFUNCTION(jsonb, 1,1,0, 0,1,0, jsonRemoveFunc), + JFUNCTION(json_array, -1,0,1, 1,0,0, jsonArrayFunc), + JFUNCTION(jsonb_array, -1,0,1, 1,1,0, jsonArrayFunc), + JFUNCTION(json_array_length, 1,1,0, 0,0,0, jsonArrayLengthFunc), + JFUNCTION(json_array_length, 2,1,0, 0,0,0, jsonArrayLengthFunc), + JFUNCTION(json_error_position,1,1,0, 0,0,0, jsonErrorFunc), + JFUNCTION(json_extract, -1,1,1, 0,0,0, jsonExtractFunc), + JFUNCTION(jsonb_extract, -1,1,0, 0,1,0, jsonExtractFunc), + JFUNCTION(->, 2,1,1, 0,0,JSON_JSON, jsonExtractFunc), + JFUNCTION(->>, 2,1,0, 0,0,JSON_SQL, jsonExtractFunc), + JFUNCTION(json_insert, -1,1,1, 1,0,0, jsonSetFunc), + JFUNCTION(jsonb_insert, -1,1,0, 1,1,0, jsonSetFunc), + JFUNCTION(json_object, -1,0,1, 1,0,0, jsonObjectFunc), + JFUNCTION(jsonb_object, -1,0,1, 1,1,0, jsonObjectFunc), + JFUNCTION(json_patch, 2,1,1, 0,0,0, jsonPatchFunc), + JFUNCTION(jsonb_patch, 2,1,0, 0,1,0, jsonPatchFunc), + JFUNCTION(json_pretty, 1,1,0, 0,0,0, jsonPrettyFunc), + JFUNCTION(json_pretty, 2,1,0, 0,0,0, jsonPrettyFunc), + JFUNCTION(json_quote, 1,0,1, 1,0,0, jsonQuoteFunc), + JFUNCTION(json_remove, -1,1,1, 0,0,0, jsonRemoveFunc), + JFUNCTION(jsonb_remove, -1,1,0, 0,1,0, jsonRemoveFunc), + JFUNCTION(json_replace, -1,1,1, 1,0,0, jsonReplaceFunc), + JFUNCTION(jsonb_replace, -1,1,0, 1,1,0, jsonReplaceFunc), + JFUNCTION(json_set, -1,1,1, 1,0,JSON_ISSET, jsonSetFunc), + JFUNCTION(jsonb_set, -1,1,0, 1,1,JSON_ISSET, jsonSetFunc), + JFUNCTION(json_type, 1,1,0, 0,0,0, jsonTypeFunc), + JFUNCTION(json_type, 2,1,0, 0,0,0, jsonTypeFunc), + JFUNCTION(json_valid, 1,1,0, 0,0,0, jsonValidFunc), + JFUNCTION(json_valid, 2,1,0, 0,0,0, jsonValidFunc), +#if SQLITE_DEBUG + JFUNCTION(json_parse, 1,1,0, 0,0,0, jsonParseFunc), +#endif + WAGGREGATE(json_group_array, 1, 0, 0, + jsonArrayStep, jsonArrayFinal, jsonArrayValue, jsonGroupInverse, + SQLITE_SUBTYPE|SQLITE_RESULT_SUBTYPE|SQLITE_UTF8| + SQLITE_DETERMINISTIC), + WAGGREGATE(jsonb_group_array, 1, JSON_BLOB, 0, + jsonArrayStep, jsonArrayFinal, jsonArrayValue, jsonGroupInverse, + SQLITE_SUBTYPE|SQLITE_RESULT_SUBTYPE|SQLITE_UTF8|SQLITE_DETERMINISTIC), + WAGGREGATE(json_group_object, 2, 0, 0, + jsonObjectStep, jsonObjectFinal, jsonObjectValue, jsonGroupInverse, + SQLITE_SUBTYPE|SQLITE_RESULT_SUBTYPE|SQLITE_UTF8|SQLITE_DETERMINISTIC), + WAGGREGATE(jsonb_group_object,2, JSON_BLOB, 0, + jsonObjectStep, jsonObjectFinal, jsonObjectValue, jsonGroupInverse, + SQLITE_SUBTYPE|SQLITE_RESULT_SUBTYPE|SQLITE_UTF8| + SQLITE_DETERMINISTIC) + }; + sqlite3InsertBuiltinFuncs(aJsonFunc, ArraySize(aJsonFunc)); +#endif +} + +#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_JSON) +/* +** Register the JSON table-valued function named zName and return a +** pointer to its Module object. Return NULL if something goes wrong. +*/ +Module *sqlite3JsonVtabRegister(sqlite3 *db, const char *zName){ + unsigned int i; + static const char *azModule[] = { + "json_each", "json_tree", "jsonb_each", "jsonb_tree" + }; + assert( sqlite3HashFind(&db->aModule, zName)==0 ); + for(i=0; imutex); sqlite3Error(db, SQLITE_OK); while( rc==SQLITE_OK && zSql[0] ){ - int nCol; + int nCol = 0; char **azVals = 0; pStmt = 0; @@ -60,9 +60,7 @@ int sqlite3_exec( zSql = zLeftover; continue; } - callbackIsInit = 0; - nCol = sqlite3_column_count(pStmt); while( 1 ){ int i; @@ -73,7 +71,8 @@ int sqlite3_exec( (SQLITE_DONE==rc && !callbackIsInit && db->flags&SQLITE_NullCallback)) ){ if( !callbackIsInit ){ - azCols = sqlite3DbMallocZero(db, 2*nCol*sizeof(const char*) + 1); + nCol = sqlite3_column_count(pStmt); + azCols = sqlite3DbMallocRaw(db, (2*nCol+1)*sizeof(const char*)); if( azCols==0 ){ goto exec_out; } @@ -94,6 +93,7 @@ int sqlite3_exec( goto exec_out; } } + azVals[i] = 0; } if( xCallback(pArg, nCol, azVals, azCols) ){ /* EVIDENCE-OF: R-38229-40159 If the callback function to @@ -126,12 +126,9 @@ int sqlite3_exec( rc = sqlite3ApiExit(db, rc); if( rc!=SQLITE_OK && pzErrMsg ){ - int nErrMsg = 1 + sqlite3Strlen30(sqlite3_errmsg(db)); - *pzErrMsg = sqlite3Malloc(nErrMsg); - if( *pzErrMsg ){ - memcpy(*pzErrMsg, sqlite3_errmsg(db), nErrMsg); - }else{ - rc = SQLITE_NOMEM; + *pzErrMsg = sqlite3DbStrDup(0, sqlite3_errmsg(db)); + if( *pzErrMsg==0 ){ + rc = SQLITE_NOMEM_BKPT; sqlite3Error(db, SQLITE_NOMEM); } }else if( pzErrMsg ){ diff --git a/src/loadext.c b/src/loadext.c index 94298c4763..c5177715e8 100644 --- a/src/loadext.c +++ b/src/loadext.c @@ -18,10 +18,8 @@ #endif #include "sqlite3ext.h" #include "sqliteInt.h" -#include #ifndef SQLITE_OMIT_LOAD_EXTENSION - /* ** Some API routines are omitted when various features are ** excluded from a build of SQLite. Substitute a NULL pointer @@ -53,6 +51,7 @@ # define sqlite3_open16 0 # define sqlite3_prepare16 0 # define sqlite3_prepare16_v2 0 +# define sqlite3_prepare16_v3 0 # define sqlite3_result_error16 0 # define sqlite3_result_text16 0 # define sqlite3_result_text16be 0 @@ -85,13 +84,14 @@ # define sqlite3_declare_vtab 0 # define sqlite3_vtab_config 0 # define sqlite3_vtab_on_conflict 0 +# define sqlite3_vtab_collation 0 #endif #ifdef SQLITE_OMIT_SHARED_CACHE # define sqlite3_enable_shared_cache 0 #endif -#ifdef SQLITE_OMIT_TRACE +#if defined(SQLITE_OMIT_TRACE) || defined(SQLITE_OMIT_DEPRECATED) # define sqlite3_profile 0 # define sqlite3_trace 0 #endif @@ -111,6 +111,10 @@ #define sqlite3_blob_reopen 0 #endif +#if defined(SQLITE_OMIT_TRACE) +# define sqlite3_trace_v2 0 +#endif + /* ** The following structure contains pointers to all SQLite API routines. ** A pointer to this structure is passed into extensions when they are @@ -414,9 +418,121 @@ static const sqlite3_api_routines sqlite3Apis = { /* Version 3.10.0 and later */ sqlite3_status64, sqlite3_strlike, - sqlite3_db_cacheflush + sqlite3_db_cacheflush, + /* Version 3.12.0 and later */ + sqlite3_system_errno, + /* Version 3.14.0 and later */ + sqlite3_trace_v2, + sqlite3_expanded_sql, + /* Version 3.18.0 and later */ + sqlite3_set_last_insert_rowid, + /* Version 3.20.0 and later */ + sqlite3_prepare_v3, + sqlite3_prepare16_v3, + sqlite3_bind_pointer, + sqlite3_result_pointer, + sqlite3_value_pointer, + /* Version 3.22.0 and later */ + sqlite3_vtab_nochange, + sqlite3_value_nochange, + sqlite3_vtab_collation, + /* Version 3.24.0 and later */ + sqlite3_keyword_count, + sqlite3_keyword_name, + sqlite3_keyword_check, + sqlite3_str_new, + sqlite3_str_finish, + sqlite3_str_appendf, + sqlite3_str_vappendf, + sqlite3_str_append, + sqlite3_str_appendall, + sqlite3_str_appendchar, + sqlite3_str_reset, + sqlite3_str_errcode, + sqlite3_str_length, + sqlite3_str_value, + /* Version 3.25.0 and later */ + sqlite3_create_window_function, + /* Version 3.26.0 and later */ +#ifdef SQLITE_ENABLE_NORMALIZE + sqlite3_normalized_sql, +#else + 0, +#endif + /* Version 3.28.0 and later */ + sqlite3_stmt_isexplain, + sqlite3_value_frombind, + /* Version 3.30.0 and later */ +#ifndef SQLITE_OMIT_VIRTUALTABLE + sqlite3_drop_modules, +#else + 0, +#endif + /* Version 3.31.0 and later */ + sqlite3_hard_heap_limit64, + sqlite3_uri_key, + sqlite3_filename_database, + sqlite3_filename_journal, + sqlite3_filename_wal, + /* Version 3.32.0 and later */ + sqlite3_create_filename, + sqlite3_free_filename, + sqlite3_database_file_object, + /* Version 3.34.0 and later */ + sqlite3_txn_state, + /* Version 3.36.1 and later */ + sqlite3_changes64, + sqlite3_total_changes64, + /* Version 3.37.0 and later */ + sqlite3_autovacuum_pages, + /* Version 3.38.0 and later */ + sqlite3_error_offset, +#ifndef SQLITE_OMIT_VIRTUALTABLE + sqlite3_vtab_rhs_value, + sqlite3_vtab_distinct, + sqlite3_vtab_in, + sqlite3_vtab_in_first, + sqlite3_vtab_in_next, +#else + 0, + 0, + 0, + 0, + 0, +#endif + /* Version 3.39.0 and later */ +#ifndef SQLITE_OMIT_DESERIALIZE + sqlite3_deserialize, + sqlite3_serialize, +#else + 0, + 0, +#endif + sqlite3_db_name, + /* Version 3.40.0 and later */ + sqlite3_value_encoding, + /* Version 3.41.0 and later */ + sqlite3_is_interrupted, + /* Version 3.43.0 and later */ + sqlite3_stmt_explain, + /* Version 3.44.0 and later */ + sqlite3_get_clientdata, + sqlite3_set_clientdata, + /* Version 3.50.0 and later */ + sqlite3_setlk_timeout, + /* Version 3.51.0 and later */ + sqlite3_set_errmsg, + sqlite3_db_status64 }; +/* True if x is the directory separator character +*/ +#if SQLITE_OS_WIN +# define DirSep(X) ((X)=='/'||(X)=='\\') +#else +# define DirSep(X) ((X)=='/') +#endif + /* ** Attempt to load an SQLite extension library contained in the file ** zFile. The entry point is zProc. zProc may be 0 in which case a @@ -437,13 +553,14 @@ static int sqlite3LoadExtension( ){ sqlite3_vfs *pVfs = db->pVfs; void *handle; - int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*); + sqlite3_loadext_entry xInit; char *zErrmsg = 0; const char *zEntry; char *zAltEntry = 0; void **aHandle; - u64 nMsg = 300 + sqlite3Strlen30(zFile); + u64 nMsg = strlen(zFile); int ii; + int rc; /* Shared library endings to try if zFile cannot be loaded as written */ static const char *azEndings[] = { @@ -462,8 +579,9 @@ static int sqlite3LoadExtension( /* Ticket #1863. To avoid a creating security problems for older ** applications that relink against newer versions of SQLite, the ** ability to run load_extension is turned off by default. One - ** must call sqlite3_enable_load_extension() to turn on extension - ** loading. Otherwise you get the following error. + ** must call either sqlite3_enable_load_extension(db) or + ** sqlite3_db_config(db, SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, 1, 0) + ** to turn on extension loading. */ if( (db->flags & SQLITE_LoadExtension)==0 ){ if( pzErrMsg ){ @@ -474,28 +592,33 @@ static int sqlite3LoadExtension( zEntry = zProc ? zProc : "sqlite3_extension_init"; + /* tag-20210611-1. Some dlopen() implementations will segfault if given + ** an oversize filename. Most filesystems have a pathname limit of 4K, + ** so limit the extension filename length to about twice that. + ** https://sqlite.org/forum/forumpost/08a0d6d9bf + ** + ** Later (2023-03-25): Save an extra 6 bytes for the filename suffix. + ** See https://sqlite.org/forum/forumpost/24083b579d. + */ + if( nMsg>SQLITE_MAX_PATHLEN ) goto extension_not_found; + + /* Do not allow sqlite3_load_extension() to link to a copy of the + ** running application, by passing in an empty filename. */ + if( nMsg==0 ) goto extension_not_found; + handle = sqlite3OsDlOpen(pVfs, zFile); #if SQLITE_OS_UNIX || SQLITE_OS_WIN for(ii=0; ii=0 && zFile[iFile]!='/'; iFile--){} + for(iFile=ncFile-1; iFile>=0 && !DirSep(zFile[iFile]); iFile--){} iFile++; if( sqlite3_strnicmp(zFile+iFile, "lib", 3)==0 ) iFile += 3; for(iEntry=8; (c = zFile[iFile])!=0 && c!='.'; iFile++){ @@ -527,15 +650,15 @@ static int sqlite3LoadExtension( } memcpy(zAltEntry+iEntry, "_init", 6); zEntry = zAltEntry; - xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*)) - sqlite3OsDlSym(pVfs, handle, zEntry); + xInit = (sqlite3_loadext_entry)sqlite3OsDlSym(pVfs, handle, zEntry); } if( xInit==0 ){ if( pzErrMsg ){ - nMsg += sqlite3Strlen30(zEntry); + nMsg += strlen(zEntry) + 300; *pzErrMsg = zErrmsg = sqlite3_malloc64(nMsg); if( zErrmsg ){ - sqlite3_snprintf(nMsg, zErrmsg, + assert( nMsg<0x7fffffff ); /* zErrmsg would be NULL if not so */ + sqlite3_snprintf((int)nMsg, zErrmsg, "no entry point [%s] in shared library [%s]", zEntry, zFile); sqlite3OsDlError(pVfs, nMsg-1, zErrmsg); } @@ -545,7 +668,9 @@ static int sqlite3LoadExtension( return SQLITE_ERROR; } sqlite3_free(zAltEntry); - if( xInit(db, &zErrmsg, &sqlite3Apis) ){ + rc = xInit(db, &zErrmsg, &sqlite3Apis); + if( rc ){ + if( rc==SQLITE_OK_LOAD_PERMANENTLY ) return SQLITE_OK; if( pzErrMsg ){ *pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg); } @@ -557,7 +682,7 @@ static int sqlite3LoadExtension( /* Append the new shared library handle to the db->aExtension array. */ aHandle = sqlite3DbMallocZero(db, sizeof(handle)*(db->nExtension+1)); if( aHandle==0 ){ - return SQLITE_NOMEM; + return SQLITE_NOMEM_BKPT; } if( db->nExtension>0 ){ memcpy(aHandle, db->aExtension, sizeof(handle)*db->nExtension); @@ -567,6 +692,19 @@ static int sqlite3LoadExtension( db->aExtension[db->nExtension++] = handle; return SQLITE_OK; + +extension_not_found: + if( pzErrMsg ){ + nMsg += 300; + *pzErrMsg = zErrmsg = sqlite3_malloc64(nMsg); + if( zErrmsg ){ + assert( nMsg<0x7fffffff ); /* zErrmsg would be NULL if not so */ + sqlite3_snprintf((int)nMsg, zErrmsg, + "unable to open shared library [%.*s]", SQLITE_MAX_PATHLEN, zFile); + sqlite3OsDlError(pVfs, nMsg-1, zErrmsg); + } + } + return SQLITE_ERROR; } int sqlite3_load_extension( sqlite3 *db, /* Load the extension into this database connection */ @@ -600,39 +738,31 @@ void sqlite3CloseExtensions(sqlite3 *db){ ** default so as not to open security holes in older applications. */ int sqlite3_enable_load_extension(sqlite3 *db, int onoff){ +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; +#endif sqlite3_mutex_enter(db->mutex); if( onoff ){ - db->flags |= SQLITE_LoadExtension; + db->flags |= SQLITE_LoadExtension|SQLITE_LoadExtFunc; }else{ - db->flags &= ~SQLITE_LoadExtension; + db->flags &= ~(u64)(SQLITE_LoadExtension|SQLITE_LoadExtFunc); } sqlite3_mutex_leave(db->mutex); return SQLITE_OK; } -#endif /* SQLITE_OMIT_LOAD_EXTENSION */ - -/* -** The auto-extension code added regardless of whether or not extension -** loading is supported. We need a dummy sqlite3Apis pointer for that -** code if regular extension loading is not available. This is that -** dummy pointer. -*/ -#ifdef SQLITE_OMIT_LOAD_EXTENSION -static const sqlite3_api_routines sqlite3Apis = { 0 }; -#endif - +#endif /* !defined(SQLITE_OMIT_LOAD_EXTENSION) */ /* ** The following object holds the list of automatically loaded ** extensions. ** -** This list is shared across threads. The SQLITE_MUTEX_STATIC_MASTER +** This list is shared across threads. The SQLITE_MUTEX_STATIC_MAIN ** mutex must be held while accessing this list. */ typedef struct sqlite3AutoExtList sqlite3AutoExtList; static SQLITE_WSD struct sqlite3AutoExtList { - u32 nExt; /* Number of entries in aExt[] */ + u32 nExt; /* Number of entries in aExt[] */ void (**aExt)(void); /* Pointers to the extension init functions */ } sqlite3Autoext = { 0, 0 }; @@ -656,8 +786,13 @@ static SQLITE_WSD struct sqlite3AutoExtList { ** Register a statically linked extension that is automatically ** loaded by every new database connection. */ -int sqlite3_auto_extension(void (*xInit)(void)){ +int sqlite3_auto_extension( + void (*xInit)(void) +){ int rc = SQLITE_OK; +#ifdef SQLITE_ENABLE_API_ARMOR + if( xInit==0 ) return SQLITE_MISUSE_BKPT; +#endif #ifndef SQLITE_OMIT_AUTOINIT rc = sqlite3_initialize(); if( rc ){ @@ -667,7 +802,7 @@ int sqlite3_auto_extension(void (*xInit)(void)){ { u32 i; #if SQLITE_THREADSAFE - sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); + sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); #endif wsdAutoextInit; sqlite3_mutex_enter(mutex); @@ -679,7 +814,7 @@ int sqlite3_auto_extension(void (*xInit)(void)){ void (**aNew)(void); aNew = sqlite3_realloc64(wsdAutoext.aExt, nByte); if( aNew==0 ){ - rc = SQLITE_NOMEM; + rc = SQLITE_NOMEM_BKPT; }else{ wsdAutoext.aExt = aNew; wsdAutoext.aExt[wsdAutoext.nExt] = xInit; @@ -701,13 +836,18 @@ int sqlite3_auto_extension(void (*xInit)(void)){ ** Return 1 if xInit was found on the list and removed. Return 0 if xInit ** was not on the list. */ -int sqlite3_cancel_auto_extension(void (*xInit)(void)){ +int sqlite3_cancel_auto_extension( + void (*xInit)(void) +){ #if SQLITE_THREADSAFE - sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); + sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); #endif int i; int n = 0; wsdAutoextInit; +#ifdef SQLITE_ENABLE_API_ARMOR + if( xInit==0 ) return 0; +#endif sqlite3_mutex_enter(mutex); for(i=(int)wsdAutoext.nExt-1; i>=0; i--){ if( wsdAutoext.aExt[i]==xInit ){ @@ -730,7 +870,7 @@ void sqlite3_reset_auto_extension(void){ #endif { #if SQLITE_THREADSAFE - sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); + sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); #endif wsdAutoextInit; sqlite3_mutex_enter(mutex); @@ -750,7 +890,7 @@ void sqlite3AutoLoadExtensions(sqlite3 *db){ u32 i; int go = 1; int rc; - int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*); + sqlite3_loadext_entry xInit; wsdAutoextInit; if( wsdAutoext.nExt==0 ){ @@ -760,19 +900,23 @@ void sqlite3AutoLoadExtensions(sqlite3 *db){ for(i=0; go; i++){ char *zErrmsg; #if SQLITE_THREADSAFE - sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); + sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); +#endif +#ifdef SQLITE_OMIT_LOAD_EXTENSION + const sqlite3_api_routines *pThunk = 0; +#else + const sqlite3_api_routines *pThunk = &sqlite3Apis; #endif sqlite3_mutex_enter(mutex); if( i>=wsdAutoext.nExt ){ xInit = 0; go = 0; }else{ - xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*)) - wsdAutoext.aExt[i]; + xInit = (sqlite3_loadext_entry)wsdAutoext.aExt[i]; } sqlite3_mutex_leave(mutex); zErrmsg = 0; - if( xInit && (rc = xInit(db, &zErrmsg, &sqlite3Apis))!=0 ){ + if( xInit && (rc = xInit(db, &zErrmsg, pThunk))!=0 ){ sqlite3ErrorWithMsg(db, rc, "automatic extension loading failed: %s", zErrmsg); go = 0; diff --git a/src/main.c b/src/main.c index 922af1315a..371ad1f0bd 100644 --- a/src/main.c +++ b/src/main.c @@ -22,31 +22,86 @@ #ifdef SQLITE_ENABLE_RTREE # include "rtree.h" #endif -#ifdef SQLITE_ENABLE_ICU +#if defined(SQLITE_ENABLE_ICU) || defined(SQLITE_ENABLE_ICU_COLLATIONS) # include "sqliteicu.h" #endif -#ifdef SQLITE_ENABLE_JSON1 -int sqlite3Json1Init(sqlite3*); -#endif + +/* +** This is an extension initializer that is a no-op and always +** succeeds, except that it fails if the fault-simulation is set +** to 500. +*/ +static int sqlite3TestExtInit(sqlite3 *db){ + (void)db; + return sqlite3FaultSim(500); +} + + +/* +** Forward declarations of external module initializer functions +** for modules that need them. +*/ #ifdef SQLITE_ENABLE_FTS5 int sqlite3Fts5Init(sqlite3*); #endif +#ifdef SQLITE_ENABLE_STMTVTAB +int sqlite3StmtVtabInit(sqlite3*); +#endif +#ifdef SQLITE_EXTRA_AUTOEXT +int SQLITE_EXTRA_AUTOEXT(sqlite3*); +#endif +/* +** An array of pointers to extension initializer functions for +** built-in extensions. +*/ +static int (*const sqlite3BuiltinExtensions[])(sqlite3*) = { +#ifdef SQLITE_ENABLE_FTS3 + sqlite3Fts3Init, +#endif +#ifdef SQLITE_ENABLE_FTS5 + sqlite3Fts5Init, +#endif +#if defined(SQLITE_ENABLE_ICU) || defined(SQLITE_ENABLE_ICU_COLLATIONS) + sqlite3IcuInit, +#endif +#ifdef SQLITE_ENABLE_RTREE + sqlite3RtreeInit, +#endif +#ifdef SQLITE_ENABLE_DBPAGE_VTAB + sqlite3DbpageRegister, +#endif +#ifdef SQLITE_ENABLE_DBSTAT_VTAB + sqlite3DbstatRegister, +#endif + sqlite3TestExtInit, +#ifdef SQLITE_ENABLE_STMTVTAB + sqlite3StmtVtabInit, +#endif +#ifdef SQLITE_ENABLE_BYTECODE_VTAB + sqlite3VdbeBytecodeVtabInit, +#endif +#ifdef SQLITE_EXTRA_AUTOEXT + SQLITE_EXTRA_AUTOEXT, +#endif +}; #ifndef SQLITE_AMALGAMATION /* IMPLEMENTATION-OF: R-46656-45156 The sqlite3_version[] string constant -** contains the text of SQLITE_VERSION macro. +** contains the text of SQLITE_VERSION macro. */ const char sqlite3_version[] = SQLITE_VERSION; #endif /* IMPLEMENTATION-OF: R-53536-42575 The sqlite3_libversion() function returns -** a pointer to the to the sqlite3_version[] string constant. +** a pointer to the to the sqlite3_version[] string constant. */ const char *sqlite3_libversion(void){ return sqlite3_version; } -/* IMPLEMENTATION-OF: R-63124-39300 The sqlite3_sourceid() function returns a +/* IMPLEMENTATION-OF: R-25063-23286 The sqlite3_sourceid() function returns a ** pointer to a string constant whose value is the same as the -** SQLITE_SOURCE_ID C preprocessor macro. +** SQLITE_SOURCE_ID C preprocessor macro. Except if SQLite is built using +** an edited copy of the amalgamation, then the last four characters of +** the hash might be different from SQLITE_SOURCE_ID. */ const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; } @@ -102,13 +157,13 @@ char *sqlite3_temp_directory = 0; char *sqlite3_data_directory = 0; /* -** Initialize SQLite. +** Initialize SQLite. ** ** This routine must be called to initialize the memory allocation, ** VFS, and mutex subsystems prior to doing any serious work with ** SQLite. But as long as you do not compile with SQLITE_OMIT_AUTOINIT ** this routine will be called automatically by key routines such as -** sqlite3_open(). +** sqlite3_open(). ** ** This routine is a no-op except on its very first call for the process, ** or for the first call after a call to sqlite3_shutdown. @@ -133,7 +188,7 @@ char *sqlite3_data_directory = 0; ** without blocking. */ int sqlite3_initialize(void){ - MUTEX_LOGIC( sqlite3_mutex *pMaster; ) /* The main static mutex */ + MUTEX_LOGIC( sqlite3_mutex *pMainMtx; ) /* The main static mutex */ int rc; /* Result code */ #ifdef SQLITE_EXTRA_INIT int bRunExtraInit = 0; /* Extra initialization needed */ @@ -156,9 +211,12 @@ int sqlite3_initialize(void){ ** must be complete. So isInit must not be set until the very end ** of this routine. */ - if( sqlite3GlobalConfig.isInit ) return SQLITE_OK; + if( sqlite3GlobalConfig.isInit ){ + sqlite3MemoryBarrier(); + return SQLITE_OK; + } - /* Make sure the mutex subsystem is initialized. If unable to + /* Make sure the mutex subsystem is initialized. If unable to ** initialize the mutex subsystem, return early with the error. ** If the system is so sick that we are unable to allocate a mutex, ** there is not much SQLite is going to be able to do. @@ -170,13 +228,13 @@ int sqlite3_initialize(void){ if( rc ) return rc; /* Initialize the malloc() system and the recursive pInitMutex mutex. - ** This operation is protected by the STATIC_MASTER mutex. Note that + ** This operation is protected by the STATIC_MAIN mutex. Note that ** MutexAlloc() is called for a static mutex prior to initializing the ** malloc subsystem - this implies that the allocation of a static ** mutex must not require support from the malloc subsystem. */ - MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); ) - sqlite3_mutex_enter(pMaster); + MUTEX_LOGIC( pMainMtx = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); ) + sqlite3_mutex_enter(pMainMtx); sqlite3GlobalConfig.isMutexInit = 1; if( !sqlite3GlobalConfig.isMallocInit ){ rc = sqlite3MallocInit(); @@ -187,14 +245,14 @@ int sqlite3_initialize(void){ sqlite3GlobalConfig.pInitMutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE); if( sqlite3GlobalConfig.bCoreMutex && !sqlite3GlobalConfig.pInitMutex ){ - rc = SQLITE_NOMEM; + rc = SQLITE_NOMEM_BKPT; } } } if( rc==SQLITE_OK ){ sqlite3GlobalConfig.nRefInitMutex++; } - sqlite3_mutex_leave(pMaster); + sqlite3_mutex_leave(pMainMtx); /* If rc is not SQLITE_OK at this point, then either the malloc ** subsystem could not be initialized or the system failed to allocate @@ -218,7 +276,6 @@ int sqlite3_initialize(void){ */ sqlite3_mutex_enter(sqlite3GlobalConfig.pInitMutex); if( sqlite3GlobalConfig.isInit==0 && sqlite3GlobalConfig.inProgress==0 ){ - FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions); sqlite3GlobalConfig.inProgress = 1; #ifdef SQLITE_ENABLE_SQLLOG { @@ -226,8 +283,8 @@ int sqlite3_initialize(void){ sqlite3_init_sqllog(); } #endif - memset(pHash, 0, sizeof(sqlite3GlobalFunctions)); - sqlite3RegisterGlobalFunctions(); + memset(&sqlite3BuiltinFunctions, 0, sizeof(sqlite3BuiltinFunctions)); + sqlite3RegisterBuiltinFunctions(); if( sqlite3GlobalConfig.isPCacheInit==0 ){ rc = sqlite3PcacheInitialize(); } @@ -235,9 +292,23 @@ int sqlite3_initialize(void){ sqlite3GlobalConfig.isPCacheInit = 1; rc = sqlite3OsInit(); } +#ifndef SQLITE_OMIT_DESERIALIZE + if( rc==SQLITE_OK ){ + rc = sqlite3MemdbInit(); + } +#endif if( rc==SQLITE_OK ){ - sqlite3PCacheBufferSetup( sqlite3GlobalConfig.pPage, + sqlite3PCacheBufferSetup( sqlite3GlobalConfig.pPage, sqlite3GlobalConfig.szPage, sqlite3GlobalConfig.nPage); +#ifdef SQLITE_EXTRA_INIT_MUTEXED + { + int SQLITE_EXTRA_INIT_MUTEXED(const char*); + rc = SQLITE_EXTRA_INIT_MUTEXED(0); + } +#endif + } + if( rc==SQLITE_OK ){ + sqlite3MemoryBarrier(); sqlite3GlobalConfig.isInit = 1; #ifdef SQLITE_EXTRA_INIT bRunExtraInit = 1; @@ -250,14 +321,14 @@ int sqlite3_initialize(void){ /* Go back under the static mutex and clean up the recursive ** mutex to prevent a resource leak. */ - sqlite3_mutex_enter(pMaster); + sqlite3_mutex_enter(pMainMtx); sqlite3GlobalConfig.nRefInitMutex--; if( sqlite3GlobalConfig.nRefInitMutex<=0 ){ assert( sqlite3GlobalConfig.nRefInitMutex==0 ); sqlite3_mutex_free(sqlite3GlobalConfig.pInitMutex); sqlite3GlobalConfig.pInitMutex = 0; } - sqlite3_mutex_leave(pMaster); + sqlite3_mutex_leave(pMainMtx); /* The following is just a sanity check to make sure SQLite has ** been compiled correctly. It is important to run this code, but @@ -267,7 +338,7 @@ int sqlite3_initialize(void){ #ifndef NDEBUG #ifndef SQLITE_OMIT_FLOATING_POINT /* This section of code's only "output" is via assert() statements. */ - if ( rc==SQLITE_OK ){ + if( rc==SQLITE_OK ){ u64 x = (((u64)1)<<63)-1; double y; assert(sizeof(x)==8); @@ -287,7 +358,6 @@ int sqlite3_initialize(void){ rc = SQLITE_EXTRA_INIT(0); } #endif - return rc; } @@ -357,9 +427,21 @@ int sqlite3_config(int op, ...){ va_list ap; int rc = SQLITE_OK; - /* sqlite3_config() shall return SQLITE_MISUSE if it is invoked while - ** the SQLite library is in use. */ - if( sqlite3GlobalConfig.isInit ) return SQLITE_MISUSE_BKPT; + /* sqlite3_config() normally returns SQLITE_MISUSE if it is invoked while + ** the SQLite library is in use. Except, a few selected opcodes + ** are allowed. + */ + if( sqlite3GlobalConfig.isInit ){ + static const u64 mAnytimeConfigOption = 0 + | MASKBIT64( SQLITE_CONFIG_LOG ) + | MASKBIT64( SQLITE_CONFIG_PCACHE_HDRSZ ) + ; + if( op<0 || op>63 || (MASKBIT64(op) & mAnytimeConfigOption)==0 ){ + return SQLITE_MISUSE_BKPT; + } + testcase( op==SQLITE_CONFIG_LOG ); + testcase( op==SQLITE_CONFIG_PCACHE_HDRSZ ); + } va_start(ap, op); switch( op ){ @@ -428,20 +510,15 @@ int sqlite3_config(int op, ...){ break; } case SQLITE_CONFIG_MEMSTATUS: { + assert( !sqlite3GlobalConfig.isInit ); /* Cannot change at runtime */ /* EVIDENCE-OF: R-61275-35157 The SQLITE_CONFIG_MEMSTATUS option takes ** single argument of type int, interpreted as a boolean, which enables ** or disables the collection of memory allocation statistics. */ sqlite3GlobalConfig.bMemstat = va_arg(ap, int); break; } - case SQLITE_CONFIG_SCRATCH: { - /* EVIDENCE-OF: R-08404-60887 There are three arguments to - ** SQLITE_CONFIG_SCRATCH: A pointer an 8-byte aligned memory buffer from - ** which the scratch allocations will be drawn, the size of each scratch - ** allocation (sz), and the maximum number of scratch allocations (N). */ - sqlite3GlobalConfig.pScratch = va_arg(ap, void*); - sqlite3GlobalConfig.szScratch = va_arg(ap, int); - sqlite3GlobalConfig.nScratch = va_arg(ap, int); + case SQLITE_CONFIG_SMALL_MALLOC: { + sqlite3GlobalConfig.bSmallMalloc = va_arg(ap, int); break; } case SQLITE_CONFIG_PAGECACHE: { @@ -459,7 +536,7 @@ int sqlite3_config(int op, ...){ ** a single parameter which is a pointer to an integer and writes into ** that integer the number of extra bytes per page required for each page ** in SQLITE_CONFIG_PAGECACHE. */ - *va_arg(ap, int*) = + *va_arg(ap, int*) = sqlite3HeaderSizeBtree() + sqlite3HeaderSizePcache() + sqlite3HeaderSizePcache1(); @@ -546,7 +623,7 @@ int sqlite3_config(int op, ...){ sqlite3GlobalConfig.nLookaside = va_arg(ap, int); break; } - + /* Record a pointer to the logger function and its first argument. ** The default is NULL. Logging is disabled if the function pointer is ** NULL. @@ -557,8 +634,10 @@ int sqlite3_config(int op, ...){ ** sqlite3GlobalConfig.xLog = va_arg(ap, void(*)(void*,int,const char*)); */ typedef void(*LOGFUNC_t)(void*,int,const char*); - sqlite3GlobalConfig.xLog = va_arg(ap, LOGFUNC_t); - sqlite3GlobalConfig.pLogArg = va_arg(ap, void*); + LOGFUNC_t xLog = va_arg(ap, LOGFUNC_t); + void *pLogArg = va_arg(ap, void*); + AtomicStore(&sqlite3GlobalConfig.xLog, xLog); + AtomicStore(&sqlite3GlobalConfig.pLogArg, pLogArg); break; } @@ -572,7 +651,8 @@ int sqlite3_config(int op, ...){ ** argument of type int. If non-zero, then URI handling is globally ** enabled. If the parameter is zero, then URI handling is globally ** disabled. */ - sqlite3GlobalConfig.bOpenUri = va_arg(ap, int); + int bOpenUri = va_arg(ap, int); + AtomicStore(&sqlite3GlobalConfig.bOpenUri, bOpenUri); break; } @@ -634,6 +714,41 @@ int sqlite3_config(int op, ...){ break; } + case SQLITE_CONFIG_STMTJRNL_SPILL: { + sqlite3GlobalConfig.nStmtSpill = va_arg(ap, int); + break; + } + +#ifdef SQLITE_ENABLE_SORTER_REFERENCES + case SQLITE_CONFIG_SORTERREF_SIZE: { + int iVal = va_arg(ap, int); + if( iVal<0 ){ + iVal = SQLITE_DEFAULT_SORTERREF_SIZE; + } + sqlite3GlobalConfig.szSorterRef = (u32)iVal; + break; + } +#endif /* SQLITE_ENABLE_SORTER_REFERENCES */ + +#ifndef SQLITE_OMIT_DESERIALIZE + case SQLITE_CONFIG_MEMDB_MAXSIZE: { + sqlite3GlobalConfig.mxMemdbSize = va_arg(ap, sqlite3_int64); + break; + } +#endif /* SQLITE_OMIT_DESERIALIZE */ + + case SQLITE_CONFIG_ROWID_IN_VIEW: { + int *pVal = va_arg(ap,int*); +#ifdef SQLITE_ALLOW_ROWID_IN_VIEW + if( 0==*pVal ) sqlite3GlobalConfig.mNoVisibleRowid = TF_NoVisibleRowid; + if( 1==*pVal ) sqlite3GlobalConfig.mNoVisibleRowid = 0; + *pVal = (sqlite3GlobalConfig.mNoVisibleRowid==0); +#else + *pVal = 0; +#endif + break; + } + default: { rc = SQLITE_ERROR; break; @@ -645,67 +760,119 @@ int sqlite3_config(int op, ...){ /* ** Set up the lookaside buffers for a database connection. -** Return SQLITE_OK on success. +** Return SQLITE_OK on success. ** If lookaside is already active, return SQLITE_BUSY. ** ** The sz parameter is the number of bytes in each lookaside slot. -** The cnt parameter is the number of slots. If pStart is NULL the -** space for the lookaside memory is obtained from sqlite3_malloc(). -** If pStart is not NULL then it is sz*cnt bytes of memory to use for -** the lookaside memory. +** The cnt parameter is the number of slots. If pBuf is NULL the +** space for the lookaside memory is obtained from sqlite3_malloc() +** or similar. If pBuf is not NULL then it is sz*cnt bytes of memory +** to use for the lookaside memory. */ -static int setupLookaside(sqlite3 *db, void *pBuf, int sz, int cnt){ +static int setupLookaside( + sqlite3 *db, /* Database connection being configured */ + void *pBuf, /* Memory to use for lookaside. May be NULL */ + int sz, /* Desired size of each lookaside memory slot */ + int cnt /* Number of slots to allocate */ +){ #ifndef SQLITE_OMIT_LOOKASIDE - void *pStart; - if( db->lookaside.nOut ){ + void *pStart; /* Start of the lookaside buffer */ + sqlite3_int64 szAlloc; /* Total space set aside for lookaside memory */ + int nBig; /* Number of full-size slots */ + int nSm; /* Number smaller LOOKASIDE_SMALL-byte slots */ + + if( sqlite3LookasideUsed(db,0)>0 ){ return SQLITE_BUSY; } /* Free any existing lookaside buffer for this handle before - ** allocating a new one so we don't have to have space for + ** allocating a new one so we don't have to have space for ** both at the same time. */ if( db->lookaside.bMalloced ){ sqlite3_free(db->lookaside.pStart); } /* The size of a lookaside slot after ROUNDDOWN8 needs to be larger - ** than a pointer to be useful. + ** than a pointer and small enough to fit in a u16. */ - sz = ROUNDDOWN8(sz); /* IMP: R-33038-09382 */ + sz = ROUNDDOWN8(sz); if( sz<=(int)sizeof(LookasideSlot*) ) sz = 0; - if( cnt<0 ) cnt = 0; - if( sz==0 || cnt==0 ){ + if( sz>65528 ) sz = 65528; + /* Count must be at least 1 to be useful, but not so large as to use + ** more than 0x7fff0000 total bytes for lookaside. */ + if( cnt<1 ) cnt = 0; + if( sz>0 && cnt>(0x7fff0000/sz) ) cnt = 0x7fff0000/sz; + szAlloc = (i64)sz*(i64)cnt; + if( szAlloc==0 ){ sz = 0; pStart = 0; }else if( pBuf==0 ){ sqlite3BeginBenignMalloc(); - pStart = sqlite3Malloc( sz*cnt ); /* IMP: R-61949-35727 */ + pStart = sqlite3Malloc( szAlloc ); sqlite3EndBenignMalloc(); - if( pStart ) cnt = sqlite3MallocSize(pStart)/sz; + if( pStart ) szAlloc = sqlite3MallocSize(pStart); }else{ pStart = pBuf; } +#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE + if( sz>=LOOKASIDE_SMALL*3 ){ + nBig = szAlloc/(3*LOOKASIDE_SMALL+sz); + nSm = (szAlloc - (i64)sz*(i64)nBig)/LOOKASIDE_SMALL; + }else if( sz>=LOOKASIDE_SMALL*2 ){ + nBig = szAlloc/(LOOKASIDE_SMALL+sz); + nSm = (szAlloc - (i64)sz*(i64)nBig)/LOOKASIDE_SMALL; + }else +#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */ + if( sz>0 ){ + nBig = szAlloc/sz; + nSm = 0; + }else{ + nBig = nSm = 0; + } db->lookaside.pStart = pStart; + db->lookaside.pInit = 0; db->lookaside.pFree = 0; db->lookaside.sz = (u16)sz; + db->lookaside.szTrue = (u16)sz; if( pStart ){ int i; LookasideSlot *p; assert( sz > (int)sizeof(LookasideSlot*) ); p = (LookasideSlot*)pStart; - for(i=cnt-1; i>=0; i--){ - p->pNext = db->lookaside.pFree; - db->lookaside.pFree = p; + for(i=0; ipNext = db->lookaside.pInit; + db->lookaside.pInit = p; p = (LookasideSlot*)&((u8*)p)[sz]; } +#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE + db->lookaside.pSmallInit = 0; + db->lookaside.pSmallFree = 0; + db->lookaside.pMiddle = p; + for(i=0; ipNext = db->lookaside.pSmallInit; + db->lookaside.pSmallInit = p; + p = (LookasideSlot*)&((u8*)p)[LOOKASIDE_SMALL]; + } +#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */ + assert( ((uptr)p)<=szAlloc + (uptr)pStart ); db->lookaside.pEnd = p; db->lookaside.bDisable = 0; db->lookaside.bMalloced = pBuf==0 ?1:0; + db->lookaside.nSlot = nBig+nSm; }else{ - db->lookaside.pStart = db; - db->lookaside.pEnd = db; + db->lookaside.pStart = 0; +#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE + db->lookaside.pSmallInit = 0; + db->lookaside.pSmallFree = 0; + db->lookaside.pMiddle = 0; +#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */ + db->lookaside.pEnd = 0; db->lookaside.bDisable = 1; + db->lookaside.sz = 0; db->lookaside.bMalloced = 0; + db->lookaside.nSlot = 0; } + db->lookaside.pTrueEnd = db->lookaside.pEnd; + assert( sqlite3LookasideUsed(db,0)==0 ); #endif /* SQLITE_OMIT_LOOKASIDE */ return SQLITE_OK; } @@ -763,7 +930,7 @@ int sqlite3_db_cacheflush(sqlite3 *db){ sqlite3BtreeEnterAll(db); for(i=0; rc==SQLITE_OK && inDb; i++){ Btree *pBt = db->aDb[i].pBt; - if( pBt && sqlite3BtreeIsInTrans(pBt) ){ + if( pBt && sqlite3BtreeTxnState(pBt)==SQLITE_TXN_WRITE ){ Pager *pPager = sqlite3BtreePager(pBt); rc = sqlite3PagerFlush(pPager); if( rc==SQLITE_BUSY ){ @@ -783,8 +950,20 @@ int sqlite3_db_cacheflush(sqlite3 *db){ int sqlite3_db_config(sqlite3 *db, int op, ...){ va_list ap; int rc; + +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; +#endif + sqlite3_mutex_enter(db->mutex); va_start(ap, op); switch( op ){ + case SQLITE_DBCONFIG_MAINDBNAME: { + /* IMP: R-06824-28531 */ + /* IMP: R-36257-52125 */ + db->aDb[0].zDbSName = va_arg(ap,char*); + rc = SQLITE_OK; + break; + } case SQLITE_DBCONFIG_LOOKASIDE: { void *pBuf = va_arg(ap, void*); /* IMP: R-26835-10964 */ int sz = va_arg(ap, int); /* IMP: R-47871-25994 */ @@ -795,10 +974,30 @@ int sqlite3_db_config(sqlite3 *db, int op, ...){ default: { static const struct { int op; /* The opcode */ - u32 mask; /* Mask of the bit in sqlite3.flags to set/clear */ + u64 mask; /* Mask of the bit in sqlite3.flags to set/clear */ } aFlagOp[] = { - { SQLITE_DBCONFIG_ENABLE_FKEY, SQLITE_ForeignKeys }, - { SQLITE_DBCONFIG_ENABLE_TRIGGER, SQLITE_EnableTrigger }, + { SQLITE_DBCONFIG_ENABLE_FKEY, SQLITE_ForeignKeys }, + { SQLITE_DBCONFIG_ENABLE_TRIGGER, SQLITE_EnableTrigger }, + { SQLITE_DBCONFIG_ENABLE_VIEW, SQLITE_EnableView }, + { SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, SQLITE_Fts3Tokenizer }, + { SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, SQLITE_LoadExtension }, + { SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE, SQLITE_NoCkptOnClose }, + { SQLITE_DBCONFIG_ENABLE_QPSG, SQLITE_EnableQPSG }, + { SQLITE_DBCONFIG_TRIGGER_EQP, SQLITE_TriggerEQP }, + { SQLITE_DBCONFIG_RESET_DATABASE, SQLITE_ResetDatabase }, + { SQLITE_DBCONFIG_DEFENSIVE, SQLITE_Defensive }, + { SQLITE_DBCONFIG_WRITABLE_SCHEMA, SQLITE_WriteSchema| + SQLITE_NoSchemaError }, + { SQLITE_DBCONFIG_LEGACY_ALTER_TABLE, SQLITE_LegacyAlter }, + { SQLITE_DBCONFIG_DQS_DDL, SQLITE_DqsDDL }, + { SQLITE_DBCONFIG_DQS_DML, SQLITE_DqsDML }, + { SQLITE_DBCONFIG_LEGACY_FILE_FORMAT, SQLITE_LegacyFileFmt }, + { SQLITE_DBCONFIG_TRUSTED_SCHEMA, SQLITE_TrustedSchema }, + { SQLITE_DBCONFIG_STMT_SCANSTATUS, SQLITE_StmtScanStatus }, + { SQLITE_DBCONFIG_REVERSE_SCANORDER, SQLITE_ReverseOrder }, + { SQLITE_DBCONFIG_ENABLE_ATTACH_CREATE, SQLITE_AttachCreate }, + { SQLITE_DBCONFIG_ENABLE_ATTACH_WRITE, SQLITE_AttachWrite }, + { SQLITE_DBCONFIG_ENABLE_COMMENTS, SQLITE_Comments }, }; unsigned int i; rc = SQLITE_ERROR; /* IMP: R-42790-23372 */ @@ -806,14 +1005,14 @@ int sqlite3_db_config(sqlite3 *db, int op, ...){ if( aFlagOp[i].op==op ){ int onoff = va_arg(ap, int); int *pRes = va_arg(ap, int*); - int oldFlags = db->flags; + u64 oldFlags = db->flags; if( onoff>0 ){ db->flags |= aFlagOp[i].mask; }else if( onoff==0 ){ - db->flags &= ~aFlagOp[i].mask; + db->flags &= ~(u64)aFlagOp[i].mask; } if( oldFlags!=db->flags ){ - sqlite3ExpirePreparedStatements(db); + sqlite3ExpirePreparedStatements(db, 0); } if( pRes ){ *pRes = (db->flags & aFlagOp[i].mask)!=0; @@ -826,55 +1025,59 @@ int sqlite3_db_config(sqlite3 *db, int op, ...){ } } va_end(ap); + sqlite3_mutex_leave(db->mutex); return rc; } - -/* -** Return true if the buffer z[0..n-1] contains all spaces. -*/ -static int allSpaces(const char *z, int n){ - while( n>0 && z[n-1]==' ' ){ n--; } - return n==0; -} - /* ** This is the default collating function named "BINARY" which is always ** available. -** -** If the padFlag argument is not NULL then space padding at the end -** of strings is ignored. This implements the RTRIM collation. */ static int binCollFunc( - void *padFlag, + void *NotUsed, int nKey1, const void *pKey1, int nKey2, const void *pKey2 ){ int rc, n; + UNUSED_PARAMETER(NotUsed); n = nKey1xCmp!=binCollFunc || strcmp(p->zName,"BINARY")==0 ); + return p==0 || p->xCmp==binCollFunc; +} + +/* +** Another built-in collating sequence: NOCASE. ** ** This collating sequence is intended to be used for "case independent ** comparison". SQLite's knowledge of upper and lower case equivalents @@ -909,10 +1112,25 @@ sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){ return db->lastRowid; } +/* +** Set the value returned by the sqlite3_last_insert_rowid() API function. +*/ +void sqlite3_set_last_insert_rowid(sqlite3 *db, sqlite3_int64 iRowid){ +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) ){ + (void)SQLITE_MISUSE_BKPT; + return; + } +#endif + sqlite3_mutex_enter(db->mutex); + db->lastRowid = iRowid; + sqlite3_mutex_leave(db->mutex); +} + /* ** Return the number of changes in the most recent call to sqlite3_exec(). */ -int sqlite3_changes(sqlite3 *db){ +sqlite3_int64 sqlite3_changes64(sqlite3 *db){ #ifdef SQLITE_ENABLE_API_ARMOR if( !sqlite3SafetyCheckOk(db) ){ (void)SQLITE_MISUSE_BKPT; @@ -921,11 +1139,14 @@ int sqlite3_changes(sqlite3 *db){ #endif return db->nChange; } +int sqlite3_changes(sqlite3 *db){ + return (int)sqlite3_changes64(db); +} /* ** Return the number of changes since the database handle was opened. */ -int sqlite3_total_changes(sqlite3 *db){ +sqlite3_int64 sqlite3_total_changes64(sqlite3 *db){ #ifdef SQLITE_ENABLE_API_ARMOR if( !sqlite3SafetyCheckOk(db) ){ (void)SQLITE_MISUSE_BKPT; @@ -934,6 +1155,9 @@ int sqlite3_total_changes(sqlite3 *db){ #endif return db->nTotalChange; } +int sqlite3_total_changes(sqlite3 *db){ + return (int)sqlite3_total_changes64(db); +} /* ** Close all open savepoints. This function only manipulates fields of the @@ -958,7 +1182,9 @@ void sqlite3CloseSavepoints(sqlite3 *db){ ** with SQLITE_ANY as the encoding. */ static void functionDestroy(sqlite3 *db, FuncDef *p){ - FuncDestructor *pDestructor = p->pDestructor; + FuncDestructor *pDestructor; + assert( (p->funcFlags & SQLITE_FUNC_BUILTIN)==0 ); + pDestructor = p->u.pDestructor; if( pDestructor ){ pDestructor->nRef--; if( pDestructor->nRef==0 ){ @@ -979,7 +1205,7 @@ static void disconnectAllVtab(sqlite3 *db){ sqlite3BtreeEnterAll(db); for(i=0; inDb; i++){ Schema *pSchema = db->aDb[i].pSchema; - if( db->aDb[i].pSchema ){ + if( pSchema ){ for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){ Table *pTab = (Table *)sqliteHashData(p); if( IsVirtual(pTab) ) sqlite3VtabDisconnect(db, pTab); @@ -1001,7 +1227,7 @@ static void disconnectAllVtab(sqlite3 *db){ /* ** Return TRUE if database connection db has unfinalized prepared -** statements or unfinished sqlite3_backup objects. +** statements or unfinished sqlite3_backup objects. */ static int connectionIsBusy(sqlite3 *db){ int j; @@ -1027,6 +1253,9 @@ static int sqlite3Close(sqlite3 *db, int forceZombie){ return SQLITE_MISUSE_BKPT; } sqlite3_mutex_enter(db->mutex); + if( db->mTrace & SQLITE_TRACE_CLOSE ){ + db->trace.xV2(SQLITE_TRACE_CLOSE, db->pTraceArg, db, 0); + } /* Force xDisconnect calls on all virtual tables */ disconnectAllVtab(db); @@ -1057,17 +1286,55 @@ static int sqlite3Close(sqlite3 *db, int forceZombie){ } #endif + while( db->pDbData ){ + DbClientData *p = db->pDbData; + db->pDbData = p->pNext; + assert( p->pData!=0 ); + if( p->xDestructor ) p->xDestructor(p->pData); + sqlite3_free(p); + } + /* Convert the connection into a zombie and then close it. */ - db->magic = SQLITE_MAGIC_ZOMBIE; + db->eOpenState = SQLITE_STATE_ZOMBIE; sqlite3LeaveMutexAndCloseZombie(db); return SQLITE_OK; } +/* +** Return the transaction state for a single databse, or the maximum +** transaction state over all attached databases if zSchema is null. +*/ +int sqlite3_txn_state(sqlite3 *db, const char *zSchema){ + int iDb, nDb; + int iTxn = -1; +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) ){ + (void)SQLITE_MISUSE_BKPT; + return -1; + } +#endif + sqlite3_mutex_enter(db->mutex); + if( zSchema ){ + nDb = iDb = sqlite3FindDbName(db, zSchema); + if( iDb<0 ) nDb--; + }else{ + iDb = 0; + nDb = db->nDb-1; + } + for(; iDb<=nDb; iDb++){ + Btree *pBt = db->aDb[iDb].pBt; + int x = pBt!=0 ? sqlite3BtreeTxnState(pBt) : SQLITE_TXN_NONE; + if( x>iTxn ) iTxn = x; + } + sqlite3_mutex_leave(db->mutex); + return iTxn; +} + /* ** Two variations on the public interface for closing a database ** connection. The sqlite3_close() version returns SQLITE_BUSY and -** leaves the connection option if there are unfinalized prepared +** leaves the connection open if there are unfinalized prepared ** statements or unfinished sqlite3_backups. The sqlite3_close_v2() ** version forces the connection to become a zombie if there are ** unclosed resources, and arranges for deallocation when the last @@ -1093,7 +1360,7 @@ void sqlite3LeaveMutexAndCloseZombie(sqlite3 *db){ ** or if the connection has not yet been closed by sqlite3_close_v2(), ** then just leave the mutex and return. */ - if( db->magic!=SQLITE_MAGIC_ZOMBIE || connectionIsBusy(db) ){ + if( db->eOpenState!=SQLITE_STATE_ZOMBIE || connectionIsBusy(db) ){ sqlite3_mutex_leave(db->mutex); return; } @@ -1127,6 +1394,7 @@ void sqlite3LeaveMutexAndCloseZombie(sqlite3 *db){ /* Clear the TEMP schema separately and last */ if( db->aDb[1].pSchema ){ sqlite3SchemaClear(db->aDb[1].pSchema); + assert( db->aDb[1].pSchema->trigHash.count==0 ); } sqlite3VtabUnlockList(db); @@ -1140,18 +1408,17 @@ void sqlite3LeaveMutexAndCloseZombie(sqlite3 *db){ */ sqlite3ConnectionClosed(db); - for(j=0; jaFunc.a); j++){ - FuncDef *pNext, *pHash, *p; - for(p=db->aFunc.a[j]; p; p=pHash){ - pHash = p->pHash; - while( p ){ - functionDestroy(db, p); - pNext = p->pNext; - sqlite3DbFree(db, p); - p = pNext; - } - } - } + for(i=sqliteHashFirst(&db->aFunc); i; i=sqliteHashNext(i)){ + FuncDef *pNext, *p; + p = sqliteHashData(i); + do{ + functionDestroy(db, p); + pNext = p->pNext; + sqlite3DbFree(db, p); + p = pNext; + }while( p ); + } + sqlite3HashClear(&db->aFunc); for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){ CollSeq *pColl = (CollSeq *)sqliteHashData(i); /* Invoke any destructors registered for collation sequence user data. */ @@ -1166,11 +1433,8 @@ void sqlite3LeaveMutexAndCloseZombie(sqlite3 *db){ #ifndef SQLITE_OMIT_VIRTUALTABLE for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){ Module *pMod = (Module *)sqliteHashData(i); - if( pMod->xDestroy ){ - pMod->xDestroy(pMod->pAux); - } sqlite3VtabEponymousTableClear(db, pMod); - sqlite3DbFree(db, pMod); + sqlite3VtabModuleUnref(db, pMod); } sqlite3HashClear(&db->aModule); #endif @@ -1178,24 +1442,23 @@ void sqlite3LeaveMutexAndCloseZombie(sqlite3 *db){ sqlite3Error(db, SQLITE_OK); /* Deallocates any cached error strings. */ sqlite3ValueFree(db->pErr); sqlite3CloseExtensions(db); -#if SQLITE_USER_AUTHENTICATION - sqlite3_free(db->auth.zAuthUser); - sqlite3_free(db->auth.zAuthPW); -#endif - db->magic = SQLITE_MAGIC_ERROR; + db->eOpenState = SQLITE_STATE_ERROR; /* The temp-database schema is allocated differently from the other schema ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()). ** So it needs to be freed here. Todo: Why not roll the temp schema into - ** the same sqliteMalloc() as the one that allocates the database + ** the same sqliteMalloc() as the one that allocates the database ** structure? */ sqlite3DbFree(db, db->aDb[1].pSchema); + if( db->xAutovacDestr ){ + db->xAutovacDestr(db->pAutovacPagesArg); + } sqlite3_mutex_leave(db->mutex); - db->magic = SQLITE_MAGIC_CLOSED; + db->eOpenState = SQLITE_STATE_CLOSED; sqlite3_mutex_free(db->mutex); - assert( db->lookaside.nOut==0 ); /* Fails on a lookaside memory leak */ + assert( sqlite3LookasideUsed(db,0)==0 ); if( db->lookaside.bMalloced ){ sqlite3_free(db->lookaside.pStart); } @@ -1216,19 +1479,19 @@ void sqlite3RollbackAll(sqlite3 *db, int tripCode){ assert( sqlite3_mutex_held(db->mutex) ); sqlite3BeginBenignMalloc(); - /* Obtain all b-tree mutexes before making any calls to BtreeRollback(). + /* Obtain all b-tree mutexes before making any calls to BtreeRollback(). ** This is important in case the transaction being rolled back has ** modified the database schema. If the b-tree mutexes are not taken ** here, then another shared-cache connection might sneak in between ** the database rollback and schema reset, which can cause false ** corruption reports in some cases. */ sqlite3BtreeEnterAll(db); - schemaChange = (db->flags & SQLITE_InternChanges)!=0 && db->init.busy==0; + schemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0 && db->init.busy==0; for(i=0; inDb; i++){ Btree *p = db->aDb[i].pBt; if( p ){ - if( sqlite3BtreeIsInTrans(p) ){ + if( sqlite3BtreeTxnState(p)==SQLITE_TXN_WRITE ){ inTrans = 1; } sqlite3BtreeRollback(p, tripCode, !schemaChange); @@ -1237,8 +1500,8 @@ void sqlite3RollbackAll(sqlite3 *db, int tripCode){ sqlite3VtabRollback(db); sqlite3EndBenignMalloc(); - if( (db->flags&SQLITE_InternChanges)!=0 && db->init.busy==0 ){ - sqlite3ExpirePreparedStatements(db); + if( schemaChange ){ + sqlite3ExpirePreparedStatements(db, 0); sqlite3ResetAllSchemasOfConnection(db); } sqlite3BtreeLeaveAll(db); @@ -1246,7 +1509,7 @@ void sqlite3RollbackAll(sqlite3 *db, int tripCode){ /* Any deferred constraint violations have now been resolved. */ db->nDeferredCons = 0; db->nDeferredImmCons = 0; - db->flags &= ~SQLITE_DeferFKs; + db->flags &= ~(u64)(SQLITE_DeferFKs|SQLITE_CorruptRdOnly); /* If one has been configured, invoke the rollback-hook callback */ if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){ @@ -1266,6 +1529,10 @@ const char *sqlite3ErrName(int rc){ switch( rc ){ case SQLITE_OK: zName = "SQLITE_OK"; break; case SQLITE_ERROR: zName = "SQLITE_ERROR"; break; + case SQLITE_ERROR_SNAPSHOT: zName = "SQLITE_ERROR_SNAPSHOT"; break; + case SQLITE_ERROR_RETRY: zName = "SQLITE_ERROR_RETRY"; break; + case SQLITE_ERROR_MISSING_COLLSEQ: + zName = "SQLITE_ERROR_MISSING_COLLSEQ"; break; case SQLITE_INTERNAL: zName = "SQLITE_INTERNAL"; break; case SQLITE_PERM: zName = "SQLITE_PERM"; break; case SQLITE_ABORT: zName = "SQLITE_ABORT"; break; @@ -1278,9 +1545,10 @@ const char *sqlite3ErrName(int rc){ case SQLITE_NOMEM: zName = "SQLITE_NOMEM"; break; case SQLITE_READONLY: zName = "SQLITE_READONLY"; break; case SQLITE_READONLY_RECOVERY: zName = "SQLITE_READONLY_RECOVERY"; break; - case SQLITE_READONLY_CANTLOCK: zName = "SQLITE_READONLY_CANTLOCK"; break; + case SQLITE_READONLY_CANTINIT: zName = "SQLITE_READONLY_CANTINIT"; break; case SQLITE_READONLY_ROLLBACK: zName = "SQLITE_READONLY_ROLLBACK"; break; case SQLITE_READONLY_DBMOVED: zName = "SQLITE_READONLY_DBMOVED"; break; + case SQLITE_READONLY_DIRECTORY: zName = "SQLITE_READONLY_DIRECTORY";break; case SQLITE_INTERRUPT: zName = "SQLITE_INTERRUPT"; break; case SQLITE_IOERR: zName = "SQLITE_IOERR"; break; case SQLITE_IOERR_READ: zName = "SQLITE_IOERR_READ"; break; @@ -1318,6 +1586,7 @@ const char *sqlite3ErrName(int rc){ case SQLITE_CANTOPEN_ISDIR: zName = "SQLITE_CANTOPEN_ISDIR"; break; case SQLITE_CANTOPEN_FULLPATH: zName = "SQLITE_CANTOPEN_FULLPATH"; break; case SQLITE_CANTOPEN_CONVPATH: zName = "SQLITE_CANTOPEN_CONVPATH"; break; + case SQLITE_CANTOPEN_SYMLINK: zName = "SQLITE_CANTOPEN_SYMLINK"; break; case SQLITE_PROTOCOL: zName = "SQLITE_PROTOCOL"; break; case SQLITE_EMPTY: zName = "SQLITE_EMPTY"; break; case SQLITE_SCHEMA: zName = "SQLITE_SCHEMA"; break; @@ -1349,6 +1618,7 @@ const char *sqlite3ErrName(int rc){ case SQLITE_NOTICE_RECOVER_WAL: zName = "SQLITE_NOTICE_RECOVER_WAL";break; case SQLITE_NOTICE_RECOVER_ROLLBACK: zName = "SQLITE_NOTICE_RECOVER_ROLLBACK"; break; + case SQLITE_NOTICE_RBU: zName = "SQLITE_NOTICE_RBU"; break; case SQLITE_WARNING: zName = "SQLITE_WARNING"; break; case SQLITE_WARNING_AUTOINDEX: zName = "SQLITE_WARNING_AUTOINDEX"; break; case SQLITE_DONE: zName = "SQLITE_DONE"; break; @@ -1370,10 +1640,10 @@ const char *sqlite3ErrName(int rc){ const char *sqlite3ErrStr(int rc){ static const char* const aMsg[] = { /* SQLITE_OK */ "not an error", - /* SQLITE_ERROR */ "SQL logic error or missing database", + /* SQLITE_ERROR */ "SQL logic error", /* SQLITE_INTERNAL */ 0, /* SQLITE_PERM */ "access permission denied", - /* SQLITE_ABORT */ "callback requested query abort", + /* SQLITE_ABORT */ "query aborted", /* SQLITE_BUSY */ "database is locked", /* SQLITE_LOCKED */ "database table is locked", /* SQLITE_NOMEM */ "out of memory", @@ -1385,17 +1655,23 @@ const char *sqlite3ErrStr(int rc){ /* SQLITE_FULL */ "database or disk is full", /* SQLITE_CANTOPEN */ "unable to open database file", /* SQLITE_PROTOCOL */ "locking protocol", - /* SQLITE_EMPTY */ "table contains no data", + /* SQLITE_EMPTY */ 0, /* SQLITE_SCHEMA */ "database schema has changed", /* SQLITE_TOOBIG */ "string or blob too big", /* SQLITE_CONSTRAINT */ "constraint failed", /* SQLITE_MISMATCH */ "datatype mismatch", - /* SQLITE_MISUSE */ "library routine called out of sequence", + /* SQLITE_MISUSE */ "bad parameter or other API misuse", +#ifdef SQLITE_DISABLE_LFS /* SQLITE_NOLFS */ "large file support is disabled", +#else + /* SQLITE_NOLFS */ 0, +#endif /* SQLITE_AUTH */ "authorization denied", - /* SQLITE_FORMAT */ "auxiliary database format error", - /* SQLITE_RANGE */ "bind or column index out of range", - /* SQLITE_NOTADB */ "file is encrypted or is not a database", + /* SQLITE_FORMAT */ 0, + /* SQLITE_RANGE */ "column index out of range", + /* SQLITE_NOTADB */ "file is not a database", + /* SQLITE_NOTICE */ "notification message", + /* SQLITE_WARNING */ "warning message", }; const char *zErr = "unknown error"; switch( rc ){ @@ -1403,6 +1679,14 @@ const char *sqlite3ErrStr(int rc){ zErr = "abort due to ROLLBACK"; break; } + case SQLITE_ROW: { + zErr = "another row available"; + break; + } + case SQLITE_DONE: { + zErr = "no more rows available"; + break; + } default: { rc &= 0xff; if( ALWAYS(rc>=0) && rcbusyTimeout; + int tmout = db->busyTimeout; int delay, prior; assert( count>=0 ); @@ -1442,16 +1731,18 @@ static int sqliteDefaultBusyCallback( delay = delays[NDELAY-1]; prior = totals[NDELAY-1] + delay*(count-(NDELAY-1)); } - if( prior + delay > timeout ){ - delay = timeout - prior; + if( prior + delay > tmout ){ + delay = tmout - prior; if( delay<=0 ) return 0; } sqlite3OsSleep(db->pVfs, delay*1000); return 1; #else + /* This case for unix systems that lack usleep() support. Sleeping + ** must be done in increments of whole seconds */ sqlite3 *db = (sqlite3 *)ptr; - int timeout = ((sqlite3 *)ptr)->busyTimeout; - if( (count+1)*1000 > timeout ){ + int tmout = ((sqlite3 *)ptr)->busyTimeout; + if( (count+1)*1000 > tmout ){ return 0; } sqlite3OsSleep(db->pVfs, 1000000); @@ -1462,20 +1753,22 @@ static int sqliteDefaultBusyCallback( /* ** Invoke the given busy handler. ** -** This routine is called when an operation failed with a lock. +** This routine is called when an operation failed to acquire a +** lock on VFS file pFile. +** ** If this routine returns non-zero, the lock is retried. If it ** returns 0, the operation aborts with an SQLITE_BUSY error. */ int sqlite3InvokeBusyHandler(BusyHandler *p){ int rc; - if( NEVER(p==0) || p->xFunc==0 || p->nBusy<0 ) return 0; - rc = p->xFunc(p->pArg, p->nBusy); + if( p->xBusyHandler==0 || p->nBusy<0 ) return 0; + rc = p->xBusyHandler(p->pBusyArg, p->nBusy); if( rc==0 ){ p->nBusy = -1; }else{ p->nBusy++; } - return rc; + return rc; } /* @@ -1491,10 +1784,13 @@ int sqlite3_busy_handler( if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; #endif sqlite3_mutex_enter(db->mutex); - db->busyHandler.xFunc = xBusy; - db->busyHandler.pArg = pArg; + db->busyHandler.xBusyHandler = xBusy; + db->busyHandler.pBusyArg = pArg; db->busyHandler.nBusy = 0; db->busyTimeout = 0; +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT + db->setlkTimeout = 0; +#endif sqlite3_mutex_leave(db->mutex); return SQLITE_OK; } @@ -1506,9 +1802,9 @@ int sqlite3_busy_handler( ** be invoked every nOps opcodes. */ void sqlite3_progress_handler( - sqlite3 *db, + sqlite3 *db, int nOps, - int (*xProgress)(void*), + int (*xProgress)(void*), void *pArg ){ #ifdef SQLITE_ENABLE_API_ARMOR @@ -1541,33 +1837,88 @@ int sqlite3_busy_timeout(sqlite3 *db, int ms){ if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; #endif if( ms>0 ){ - sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db); + sqlite3_busy_handler(db, (int(*)(void*,int))sqliteDefaultBusyCallback, + (void*)db); db->busyTimeout = ms; +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT + db->setlkTimeout = ms; +#endif }else{ sqlite3_busy_handler(db, 0, 0); } return SQLITE_OK; } +/* +** Set the setlk timeout value. +*/ +int sqlite3_setlk_timeout(sqlite3 *db, int ms, int flags){ +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT + int iDb; + int bBOC = ((flags & SQLITE_SETLK_BLOCK_ON_CONNECT) ? 1 : 0); +#endif +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; +#endif + if( ms<-1 ) return SQLITE_RANGE; +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT + sqlite3_mutex_enter(db->mutex); + db->setlkTimeout = ms; + db->setlkFlags = flags; + sqlite3BtreeEnterAll(db); + for(iDb=0; iDbnDb; iDb++){ + Btree *pBt = db->aDb[iDb].pBt; + if( pBt ){ + sqlite3_file *fd = sqlite3PagerFile(sqlite3BtreePager(pBt)); + sqlite3OsFileControlHint(fd, SQLITE_FCNTL_BLOCK_ON_CONNECT, (void*)&bBOC); + } + } + sqlite3BtreeLeaveAll(db); + sqlite3_mutex_leave(db->mutex); +#endif +#if !defined(SQLITE_ENABLE_API_ARMOR) && !defined(SQLITE_ENABLE_SETLK_TIMEOUT) + UNUSED_PARAMETER(db); + UNUSED_PARAMETER(flags); +#endif + return SQLITE_OK; +} + /* ** Cause any pending operation to stop at its earliest opportunity. */ void sqlite3_interrupt(sqlite3 *db){ #ifdef SQLITE_ENABLE_API_ARMOR - if( !sqlite3SafetyCheckOk(db) ){ + if( !sqlite3SafetyCheckOk(db) + && (db==0 || db->eOpenState!=SQLITE_STATE_ZOMBIE) + ){ (void)SQLITE_MISUSE_BKPT; return; } #endif - db->u1.isInterrupted = 1; + AtomicStore(&db->u1.isInterrupted, 1); } +/* +** Return true or false depending on whether or not an interrupt is +** pending on connection db. +*/ +int sqlite3_is_interrupted(sqlite3 *db){ +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) + && (db==0 || db->eOpenState!=SQLITE_STATE_ZOMBIE) + ){ + (void)SQLITE_MISUSE_BKPT; + return 0; + } +#endif + return AtomicLoad(&db->u1.isInterrupted)!=0; +} /* ** This function is exactly the same as sqlite3_create_function(), except ** that it is designed to be called by internal code. The difference is ** that if a malloc() fails in sqlite3_create_function(), an error code -** is returned and the mallocFailed flag cleared. +** is returned and the mallocFailed flag cleared. */ int sqlite3CreateFunc( sqlite3 *db, @@ -1578,26 +1929,38 @@ int sqlite3CreateFunc( void (*xSFunc)(sqlite3_context*,int,sqlite3_value **), void (*xStep)(sqlite3_context*,int,sqlite3_value **), void (*xFinal)(sqlite3_context*), + void (*xValue)(sqlite3_context*), + void (*xInverse)(sqlite3_context*,int,sqlite3_value **), FuncDestructor *pDestructor ){ FuncDef *p; - int nName; int extraFlags; assert( sqlite3_mutex_held(db->mutex) ); - if( zFunctionName==0 || - (xSFunc && (xFinal || xStep)) || - (!xSFunc && (xFinal && !xStep)) || - (!xSFunc && (!xFinal && xStep)) || - (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG) || - (255<(nName = sqlite3Strlen30( zFunctionName))) ){ + assert( xValue==0 || xSFunc==0 ); + if( zFunctionName==0 /* Must have a valid name */ + || (xSFunc!=0 && xFinal!=0) /* Not both xSFunc and xFinal */ + || ((xFinal==0)!=(xStep==0)) /* Both or neither of xFinal and xStep */ + || ((xValue==0)!=(xInverse==0)) /* Both or neither of xValue, xInverse */ + || (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG) + || (255funcFlags & SQLITE_FUNC_ENCMASK)==enc && p->nArg==nArg ){ + p = sqlite3FindFunction(db, zFunctionName, nArg, (u8)enc, 0); + if( p && (p->funcFlags & SQLITE_FUNC_ENCMASK)==(u32)enc && p->nArg==nArg ){ if( db->nVdbeActive ){ - sqlite3ErrorWithMsg(db, SQLITE_BUSY, + sqlite3ErrorWithMsg(db, SQLITE_BUSY, "unable to delete/modify user-function due to active statements"); assert( !db->mallocFailed ); return SQLITE_BUSY; }else{ - sqlite3ExpirePreparedStatements(db); + sqlite3ExpirePreparedStatements(db, 0); } + }else if( xSFunc==0 && xFinal==0 ){ + /* Trying to delete a function that does not exist. This is a no-op. + ** https://sqlite.org/forum/forumpost/726219164b */ + return SQLITE_OK; } - p = sqlite3FindFunction(db, zFunctionName, nName, nArg, (u8)enc, 1); + p = sqlite3FindFunction(db, zFunctionName, nArg, (u8)enc, 1); assert(p || db->mallocFailed); if( !p ){ - return SQLITE_NOMEM; + return SQLITE_NOMEM_BKPT; } /* If an older version of the function with a configured destructor is @@ -1655,43 +2035,38 @@ int sqlite3CreateFunc( if( pDestructor ){ pDestructor->nRef++; } - p->pDestructor = pDestructor; + p->u.pDestructor = pDestructor; p->funcFlags = (p->funcFlags & SQLITE_FUNC_ENCMASK) | extraFlags; testcase( p->funcFlags & SQLITE_DETERMINISTIC ); + testcase( p->funcFlags & SQLITE_DIRECTONLY ); p->xSFunc = xSFunc ? xSFunc : xStep; p->xFinalize = xFinal; + p->xValue = xValue; + p->xInverse = xInverse; p->pUserData = pUserData; p->nArg = (u16)nArg; return SQLITE_OK; } /* -** Create new user functions. +** Worker function used by utf-8 APIs that create new functions: +** +** sqlite3_create_function() +** sqlite3_create_function_v2() +** sqlite3_create_window_function() */ -int sqlite3_create_function( - sqlite3 *db, - const char *zFunc, - int nArg, - int enc, - void *p, - void (*xSFunc)(sqlite3_context*,int,sqlite3_value **), - void (*xStep)(sqlite3_context*,int,sqlite3_value **), - void (*xFinal)(sqlite3_context*) -){ - return sqlite3_create_function_v2(db, zFunc, nArg, enc, p, xSFunc, xStep, - xFinal, 0); -} - -int sqlite3_create_function_v2( +static int createFunctionApi( sqlite3 *db, const char *zFunc, int nArg, int enc, void *p, - void (*xSFunc)(sqlite3_context*,int,sqlite3_value **), - void (*xStep)(sqlite3_context*,int,sqlite3_value **), + void (*xSFunc)(sqlite3_context*,int,sqlite3_value**), + void (*xStep)(sqlite3_context*,int,sqlite3_value**), void (*xFinal)(sqlite3_context*), - void (*xDestroy)(void *) + void (*xValue)(sqlite3_context*), + void (*xInverse)(sqlite3_context*,int,sqlite3_value**), + void(*xDestroy)(void*) ){ int rc = SQLITE_ERROR; FuncDestructor *pArg = 0; @@ -1703,19 +2078,23 @@ int sqlite3_create_function_v2( #endif sqlite3_mutex_enter(db->mutex); if( xDestroy ){ - pArg = (FuncDestructor *)sqlite3DbMallocZero(db, sizeof(FuncDestructor)); + pArg = (FuncDestructor *)sqlite3Malloc(sizeof(FuncDestructor)); if( !pArg ){ + sqlite3OomFault(db); xDestroy(p); goto out; } + pArg->nRef = 0; pArg->xDestroy = xDestroy; pArg->pUserData = p; } - rc = sqlite3CreateFunc(db, zFunc, nArg, enc, p, xSFunc, xStep, xFinal, pArg); + rc = sqlite3CreateFunc(db, zFunc, nArg, enc, p, + xSFunc, xStep, xFinal, xValue, xInverse, pArg + ); if( pArg && pArg->nRef==0 ){ - assert( rc!=SQLITE_OK ); + assert( rc!=SQLITE_OK || (xStep==0 && xFinal==0) ); xDestroy(p); - sqlite3DbFree(db, pArg); + sqlite3_free(pArg); } out: @@ -1724,6 +2103,52 @@ int sqlite3_create_function_v2( return rc; } +/* +** Create new user functions. +*/ +int sqlite3_create_function( + sqlite3 *db, + const char *zFunc, + int nArg, + int enc, + void *p, + void (*xSFunc)(sqlite3_context*,int,sqlite3_value **), + void (*xStep)(sqlite3_context*,int,sqlite3_value **), + void (*xFinal)(sqlite3_context*) +){ + return createFunctionApi(db, zFunc, nArg, enc, p, xSFunc, xStep, + xFinal, 0, 0, 0); +} +int sqlite3_create_function_v2( + sqlite3 *db, + const char *zFunc, + int nArg, + int enc, + void *p, + void (*xSFunc)(sqlite3_context*,int,sqlite3_value **), + void (*xStep)(sqlite3_context*,int,sqlite3_value **), + void (*xFinal)(sqlite3_context*), + void (*xDestroy)(void *) +){ + return createFunctionApi(db, zFunc, nArg, enc, p, xSFunc, xStep, + xFinal, 0, 0, xDestroy); +} +int sqlite3_create_window_function( + sqlite3 *db, + const char *zFunc, + int nArg, + int enc, + void *p, + void (*xStep)(sqlite3_context*,int,sqlite3_value **), + void (*xFinal)(sqlite3_context*), + void (*xValue)(sqlite3_context*), + void (*xInverse)(sqlite3_context*,int,sqlite3_value **), + void (*xDestroy)(void *) +){ + return createFunctionApi(db, zFunc, nArg, enc, p, 0, xStep, + xFinal, xValue, xInverse, xDestroy); +} + #ifndef SQLITE_OMIT_UTF16 int sqlite3_create_function16( sqlite3 *db, @@ -1744,7 +2169,7 @@ int sqlite3_create_function16( sqlite3_mutex_enter(db->mutex); assert( !db->mallocFailed ); zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1, SQLITE_UTF16NATIVE); - rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xSFunc,xStep,xFinal,0); + rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xSFunc,xStep,xFinal,0,0,0); sqlite3DbFree(db, zFunc8); rc = sqlite3ApiExit(db, rc); sqlite3_mutex_leave(db->mutex); @@ -1754,11 +2179,33 @@ int sqlite3_create_function16( /* -** Declare that a function has been overloaded by a virtual table. -** -** If the function already exists as a regular global function, then -** this routine is a no-op. If the function does not exist, then create -** a new one that always throws a run-time error. +** The following is the implementation of an SQL function that always +** fails with an error message stating that the function is used in the +** wrong context. The sqlite3_overload_function() API might construct +** SQL function that use this routine so that the functions will exist +** for name resolution but are actually overloaded by the xFindFunction +** method of virtual tables. +*/ +static void sqlite3InvalidFunction( + sqlite3_context *context, /* The function calling context */ + int NotUsed, /* Number of arguments to the function */ + sqlite3_value **NotUsed2 /* Value of each argument */ +){ + const char *zName = (const char*)sqlite3_user_data(context); + char *zErr; + UNUSED_PARAMETER2(NotUsed, NotUsed2); + zErr = sqlite3_mprintf( + "unable to use function %s in the requested context", zName); + sqlite3_result_error(context, zErr, -1); + sqlite3_free(zErr); +} + +/* +** Declare that a function has been overloaded by a virtual table. +** +** If the function already exists as a regular global function, then +** this routine is a no-op. If the function does not exist, then create +** a new one that always throws a run-time error. ** ** When virtual tables intend to provide an overloaded function, they ** should call this routine to make sure the global function exists. @@ -1770,8 +2217,8 @@ int sqlite3_overload_function( const char *zName, int nArg ){ - int nName = sqlite3Strlen30(zName); - int rc = SQLITE_OK; + int rc; + char *zCopy; #ifdef SQLITE_ENABLE_API_ARMOR if( !sqlite3SafetyCheckOk(db) || zName==0 || nArg<-2 ){ @@ -1779,25 +2226,26 @@ int sqlite3_overload_function( } #endif sqlite3_mutex_enter(db->mutex); - if( sqlite3FindFunction(db, zName, nName, nArg, SQLITE_UTF8, 0)==0 ){ - rc = sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8, - 0, sqlite3InvalidFunction, 0, 0, 0); - } - rc = sqlite3ApiExit(db, rc); + rc = sqlite3FindFunction(db, zName, nArg, SQLITE_UTF8, 0)!=0; sqlite3_mutex_leave(db->mutex); - return rc; + if( rc ) return SQLITE_OK; + zCopy = sqlite3_mprintf("%s", zName); + if( zCopy==0 ) return SQLITE_NOMEM; + return sqlite3_create_function_v2(db, zName, nArg, SQLITE_UTF8, + zCopy, sqlite3InvalidFunction, 0, 0, sqlite3_free); } #ifndef SQLITE_OMIT_TRACE /* ** Register a trace function. The pArg from the previously registered trace -** is returned. +** is returned. ** ** A NULL trace function means that no tracing is executes. A non-NULL ** trace is a pointer to a function that is invoked at the start of each ** SQL statement. */ -void *sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){ +#ifndef SQLITE_OMIT_DEPRECATED +void *sqlite3_trace(sqlite3 *db, void(*xTrace)(void*,const char*), void *pArg){ void *pOld; #ifdef SQLITE_ENABLE_API_ARMOR @@ -1808,14 +2256,41 @@ void *sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){ #endif sqlite3_mutex_enter(db->mutex); pOld = db->pTraceArg; - db->xTrace = xTrace; + db->mTrace = xTrace ? SQLITE_TRACE_LEGACY : 0; + db->trace.xLegacy = xTrace; db->pTraceArg = pArg; sqlite3_mutex_leave(db->mutex); return pOld; } +#endif /* SQLITE_OMIT_DEPRECATED */ + +/* Register a trace callback using the version-2 interface. +*/ +int sqlite3_trace_v2( + sqlite3 *db, /* Trace this connection */ + unsigned mTrace, /* Mask of events to be traced */ + int(*xTrace)(unsigned,void*,void*,void*), /* Callback to invoke */ + void *pArg /* Context */ +){ +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) ){ + return SQLITE_MISUSE_BKPT; + } +#endif + sqlite3_mutex_enter(db->mutex); + if( mTrace==0 ) xTrace = 0; + if( xTrace==0 ) mTrace = 0; + db->mTrace = mTrace; + db->trace.xV2 = xTrace; + db->pTraceArg = pArg; + sqlite3_mutex_leave(db->mutex); + return SQLITE_OK; +} + +#ifndef SQLITE_OMIT_DEPRECATED /* -** Register a profile function. The pArg from the previously registered -** profile function is returned. +** Register a profile function. The pArg from the previously registered +** profile function is returned. ** ** A NULL profile function means that no profiling is executes. A non-NULL ** profile is a pointer to a function that is invoked at the conclusion of @@ -1838,9 +2313,12 @@ void *sqlite3_profile( pOld = db->pProfileArg; db->xProfile = xProfile; db->pProfileArg = pArg; + db->mTrace &= SQLITE_TRACE_NONLEGACY_MASK; + if( db->xProfile ) db->mTrace |= SQLITE_TRACE_XPROFILE; sqlite3_mutex_leave(db->mutex); return pOld; } +#endif /* SQLITE_OMIT_DEPRECATED */ #endif /* SQLITE_OMIT_TRACE */ /* @@ -1919,13 +2397,68 @@ void *sqlite3_rollback_hook( return pRet; } +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK +/* +** Register a callback to be invoked each time a row is updated, +** inserted or deleted using this database connection. +*/ +void *sqlite3_preupdate_hook( + sqlite3 *db, /* Attach the hook to this database */ + void(*xCallback)( /* Callback function */ + void*,sqlite3*,int,char const*,char const*,sqlite3_int64,sqlite3_int64), + void *pArg /* First callback argument */ +){ + void *pRet; + +#ifdef SQLITE_ENABLE_API_ARMOR + if( db==0 ){ + return 0; + } +#endif + sqlite3_mutex_enter(db->mutex); + pRet = db->pPreUpdateArg; + db->xPreUpdateCallback = xCallback; + db->pPreUpdateArg = pArg; + sqlite3_mutex_leave(db->mutex); + return pRet; +} +#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ + +/* +** Register a function to be invoked prior to each autovacuum that +** determines the number of pages to vacuum. +*/ +int sqlite3_autovacuum_pages( + sqlite3 *db, /* Attach the hook to this database */ + unsigned int (*xCallback)(void*,const char*,u32,u32,u32), + void *pArg, /* Argument to the function */ + void (*xDestructor)(void*) /* Destructor for pArg */ +){ +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) ){ + if( xDestructor ) xDestructor(pArg); + return SQLITE_MISUSE_BKPT; + } +#endif + sqlite3_mutex_enter(db->mutex); + if( db->xAutovacDestr ){ + db->xAutovacDestr(db->pAutovacPagesArg); + } + db->xAutovacPages = xCallback; + db->pAutovacPagesArg = pArg; + db->xAutovacDestr = xDestructor; + sqlite3_mutex_leave(db->mutex); + return SQLITE_OK; +} + + #ifndef SQLITE_OMIT_WAL /* ** The sqlite3_wal_hook() callback registered by sqlite3_wal_autocheckpoint(). ** Invoke sqlite3_wal_checkpoint if the number of frames in the log file ** is greater than sqlite3.pWalArg cast to an integer (the value configured by ** wal_autocheckpoint()). -*/ +*/ int sqlite3WalDefaultHook( void *pClientData, /* Argument */ sqlite3 *db, /* Connection */ @@ -2011,7 +2544,7 @@ int sqlite3_wal_checkpoint_v2( return SQLITE_OK; #else int rc; /* Return code */ - int iDb = SQLITE_MAX_ATTACHED; /* sqlite3.aDb[] index of db to checkpoint */ + int iDb; /* Schema to checkpoint */ #ifdef SQLITE_ENABLE_API_ARMOR if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; @@ -2028,12 +2561,14 @@ int sqlite3_wal_checkpoint_v2( if( eModeSQLITE_CHECKPOINT_TRUNCATE ){ /* EVIDENCE-OF: R-03996-12088 The M parameter must be a valid checkpoint ** mode: */ - return SQLITE_MISUSE; + return SQLITE_MISUSE_BKPT; } sqlite3_mutex_enter(db->mutex); if( zDb && zDb[0] ){ iDb = sqlite3FindDbName(db, zDb); + }else{ + iDb = SQLITE_MAX_DB; /* This means process all schemas */ } if( iDb<0 ){ rc = SQLITE_ERROR; @@ -2044,6 +2579,13 @@ int sqlite3_wal_checkpoint_v2( sqlite3Error(db, rc); } rc = sqlite3ApiExit(db, rc); + + /* If there are no active statements, clear the interrupt flag at this + ** point. */ + if( db->nVdbeActive==0 ){ + AtomicStore(&db->u1.isInterrupted, 0); + } + sqlite3_mutex_leave(db->mutex); return rc; #endif @@ -2052,7 +2594,7 @@ int sqlite3_wal_checkpoint_v2( /* ** Checkpoint database zDb. If zDb is NULL, or if the buffer zDb points -** to contains a zero-length string, all attached databases are +** to contains a zero-length string, all attached databases are ** checkpointed. */ int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb){ @@ -2066,20 +2608,21 @@ int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb){ ** Run a checkpoint on database iDb. This is a no-op if database iDb is ** not currently open in WAL mode. ** -** If a transaction is open on the database being checkpointed, this -** function returns SQLITE_LOCKED and a checkpoint is not attempted. If -** an error occurs while running the checkpoint, an SQLite error code is +** If a transaction is open on the database being checkpointed, this +** function returns SQLITE_LOCKED and a checkpoint is not attempted. If +** an error occurs while running the checkpoint, an SQLite error code is ** returned (i.e. SQLITE_IOERR). Otherwise, SQLITE_OK. ** ** The mutex on database handle db should be held by the caller. The mutex ** associated with the specific b-tree being checkpointed is taken by ** this function while the checkpoint is running. ** -** If iDb is passed SQLITE_MAX_ATTACHED, then all attached databases are +** If iDb is passed SQLITE_MAX_DB then all attached databases are ** checkpointed. If an error is encountered it is returned immediately - ** no attempt is made to checkpoint any remaining databases. ** -** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART. +** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL, RESTART +** or TRUNCATE. */ int sqlite3Checkpoint(sqlite3 *db, int iDb, int eMode, int *pnLog, int *pnCkpt){ int rc = SQLITE_OK; /* Return code */ @@ -2089,9 +2632,11 @@ int sqlite3Checkpoint(sqlite3 *db, int iDb, int eMode, int *pnLog, int *pnCkpt){ assert( sqlite3_mutex_held(db->mutex) ); assert( !pnLog || *pnLog==-1 ); assert( !pnCkpt || *pnCkpt==-1 ); + testcase( iDb==SQLITE_MAX_ATTACHED ); /* See forum post a006d86f72 */ + testcase( iDb==SQLITE_MAX_DB ); for(i=0; inDb && rc==SQLITE_OK; i++){ - if( i==iDb || iDb==SQLITE_MAX_ATTACHED ){ + if( i==iDb || iDb==SQLITE_MAX_DB ){ rc = sqlite3BtreeCheckpoint(db->aDb[i].pBt, eMode, pnLog, pnCkpt); pnLog = 0; pnCkpt = 0; @@ -2149,17 +2694,17 @@ int sqlite3TempInMemory(const sqlite3 *db){ const char *sqlite3_errmsg(sqlite3 *db){ const char *z; if( !db ){ - return sqlite3ErrStr(SQLITE_NOMEM); + return sqlite3ErrStr(SQLITE_NOMEM_BKPT); } if( !sqlite3SafetyCheckSickOrOk(db) ){ return sqlite3ErrStr(SQLITE_MISUSE_BKPT); } sqlite3_mutex_enter(db->mutex); if( db->mallocFailed ){ - z = sqlite3ErrStr(SQLITE_NOMEM); + z = sqlite3ErrStr(SQLITE_NOMEM_BKPT); }else{ testcase( db->pErr==0 ); - z = (char*)sqlite3_value_text(db->pErr); + z = db->errCode ? (char*)sqlite3_value_text(db->pErr) : 0; assert( !db->mallocFailed ); if( z==0 ){ z = sqlite3ErrStr(db->errCode); @@ -2169,6 +2714,42 @@ const char *sqlite3_errmsg(sqlite3 *db){ return z; } +/* +** Set the error code and error message associated with the database handle. +** +** This routine is intended to be called by outside extensions (ex: the +** Session extension). Internal logic should invoke sqlite3Error() or +** sqlite3ErrorWithMsg() directly. +*/ +int sqlite3_set_errmsg(sqlite3 *db, int errcode, const char *zMsg){ + int rc = SQLITE_OK; + if( !sqlite3SafetyCheckOk(db) ){ + return SQLITE_MISUSE_BKPT; + } + sqlite3_mutex_enter(db->mutex); + if( zMsg ){ + sqlite3ErrorWithMsg(db, errcode, "%s", zMsg); + }else{ + sqlite3Error(db, errcode); + } + rc = sqlite3ApiExit(db, rc); + sqlite3_mutex_leave(db->mutex); + return rc; +} + +/* +** Return the byte offset of the most recent error +*/ +int sqlite3_error_offset(sqlite3 *db){ + int iOffset = -1; + if( db && sqlite3SafetyCheckSickOrOk(db) && db->errCode ){ + sqlite3_mutex_enter(db->mutex); + iOffset = db->errByteOffset; + sqlite3_mutex_leave(db->mutex); + } + return iOffset; +} + #ifndef SQLITE_OMIT_UTF16 /* ** Return UTF-16 encoded English language explanation of the most recent @@ -2179,12 +2760,9 @@ const void *sqlite3_errmsg16(sqlite3 *db){ 'o', 'u', 't', ' ', 'o', 'f', ' ', 'm', 'e', 'm', 'o', 'r', 'y', 0 }; static const u16 misuse[] = { - 'l', 'i', 'b', 'r', 'a', 'r', 'y', ' ', - 'r', 'o', 'u', 't', 'i', 'n', 'e', ' ', - 'c', 'a', 'l', 'l', 'e', 'd', ' ', - 'o', 'u', 't', ' ', - 'o', 'f', ' ', - 's', 'e', 'q', 'u', 'e', 'n', 'c', 'e', 0 + 'b', 'a', 'd', ' ', 'p', 'a', 'r', 'a', 'm', 'e', 't', 'e', 'r', ' ', + 'o', 'r', ' ', 'o', 't', 'h', 'e', 'r', ' ', 'A', 'P', 'I', ' ', + 'm', 'i', 's', 'u', 's', 'e', 0 }; const void *z; @@ -2224,7 +2802,7 @@ int sqlite3_errcode(sqlite3 *db){ return SQLITE_MISUSE_BKPT; } if( !db || db->mallocFailed ){ - return SQLITE_NOMEM; + return SQLITE_NOMEM_BKPT; } return db->errCode & db->errMask; } @@ -2233,10 +2811,13 @@ int sqlite3_extended_errcode(sqlite3 *db){ return SQLITE_MISUSE_BKPT; } if( !db || db->mallocFailed ){ - return SQLITE_NOMEM; + return SQLITE_NOMEM_BKPT; } return db->errCode; } +int sqlite3_system_errno(sqlite3 *db){ + return db ? db->iSysErrno : 0; +} /* ** Return a string that describes the kind of error specified in the @@ -2253,7 +2834,7 @@ const char *sqlite3_errstr(int rc){ */ static int createCollation( sqlite3* db, - const char *zName, + const char *zName, u8 enc, void* pCtx, int(*xCompare)(void*,int,const void*,int,const void*), @@ -2261,7 +2842,7 @@ static int createCollation( ){ CollSeq *pColl; int enc2; - + assert( sqlite3_mutex_held(db->mutex) ); /* If SQLITE_UTF16 is specified as the encoding type, transform this @@ -2278,25 +2859,25 @@ static int createCollation( return SQLITE_MISUSE_BKPT; } - /* Check if this call is removing or replacing an existing collation + /* Check if this call is removing or replacing an existing collation ** sequence. If so, and there are active VMs, return busy. If there ** are no active VMs, invalidate any pre-compiled statements. */ pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 0); if( pColl && pColl->xCmp ){ if( db->nVdbeActive ){ - sqlite3ErrorWithMsg(db, SQLITE_BUSY, + sqlite3ErrorWithMsg(db, SQLITE_BUSY, "unable to delete/modify collation sequence due to active statements"); return SQLITE_BUSY; } - sqlite3ExpirePreparedStatements(db); + sqlite3ExpirePreparedStatements(db, 0); /* If collation sequence pColl was created directly by a call to ** sqlite3_create_collation, and not generated by synthCollSeq(), ** then any copies made by synthCollSeq() need to be invalidated. ** Also, collation destructor - CollSeq.xDel() - function may need ** to be called. - */ + */ if( (pColl->enc & ~SQLITE_UTF16_ALIGNED)==enc2 ){ CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName); int j; @@ -2313,7 +2894,7 @@ static int createCollation( } pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 1); - if( pColl==0 ) return SQLITE_NOMEM; + if( pColl==0 ) return SQLITE_NOMEM_BKPT; pColl->xCmp = xCompare; pColl->pUser = pCtx; pColl->xDel = xDel; @@ -2361,8 +2942,8 @@ static const int aHardLimit[] = { #if SQLITE_MAX_VDBE_OP<40 # error SQLITE_MAX_VDBE_OP must be at least 40 #endif -#if SQLITE_MAX_FUNCTION_ARG<0 || SQLITE_MAX_FUNCTION_ARG>1000 -# error SQLITE_MAX_FUNCTION_ARG must be between 0 and 1000 +#if SQLITE_MAX_FUNCTION_ARG<0 || SQLITE_MAX_FUNCTION_ARG>32767 +# error SQLITE_MAX_FUNCTION_ARG must be between 0 and 32767 #endif #if SQLITE_MAX_ATTACHED<0 || SQLITE_MAX_ATTACHED>125 # error SQLITE_MAX_ATTACHED must be between 0 and 125 @@ -2429,6 +3010,8 @@ int sqlite3_limit(sqlite3 *db, int limitId, int newLimit){ if( newLimit>=0 ){ /* IMP: R-52476-28732 */ if( newLimit>aHardLimit[limitId] ){ newLimit = aHardLimit[limitId]; /* IMP: R-51463-25634 */ + }else if( newLimitaLimit[limitId] = newLimit; } @@ -2445,17 +3028,19 @@ int sqlite3_limit(sqlite3 *db, int limitId, int newLimit){ ** query parameter. The second argument contains the URI (or non-URI filename) ** itself. When this function is called the *pFlags variable should contain ** the default flags to open the database handle with. The value stored in -** *pFlags may be updated before returning if the URI filename contains +** *pFlags may be updated before returning if the URI filename contains ** "cache=xxx" or "mode=xxx" query parameters. ** ** If successful, SQLITE_OK is returned. In this case *ppVfs is set to point to ** the VFS that should be used to open the database file. *pzFile is set to -** point to a buffer containing the name of the file to open. It is the -** responsibility of the caller to eventually call sqlite3_free() to release -** this buffer. +** point to a buffer containing the name of the file to open. The value +** stored in *pzFile is a database name acceptable to sqlite3_uri_parameter() +** and is in the same format as names created using sqlite3_create_filename(). +** The caller must invoke sqlite3_free_filename() (not sqlite3_free()!) on +** the value returned in *pzFile to avoid a memory leak. ** ** If an error occurs, then an SQLite error code is returned and *pzErrMsg -** may be set to point to a buffer containing an English language error +** may be set to point to a buffer containing an English language error ** message. It is the responsibility of the caller to eventually release ** this buffer by calling sqlite3_free(). */ @@ -2463,7 +3048,7 @@ int sqlite3ParseUri( const char *zDefaultVfs, /* VFS to use if no "vfs=xxx" query option */ const char *zUri, /* Nul-terminated URI to parse */ unsigned int *pFlags, /* IN/OUT: SQLITE_OPEN_XXX flags */ - sqlite3_vfs **ppVfs, /* OUT: VFS to use */ + sqlite3_vfs **ppVfs, /* OUT: VFS to use */ char **pzFile, /* OUT: Filename component of URI */ char **pzErrMsg /* OUT: Error message (if rc!=SQLITE_OK) */ ){ @@ -2476,23 +3061,26 @@ int sqlite3ParseUri( assert( *pzErrMsg==0 ); - if( ((flags & SQLITE_OPEN_URI) /* IMP: R-48725-32206 */ - || sqlite3GlobalConfig.bOpenUri) /* IMP: R-51689-46548 */ - && nUri>=5 && memcmp(zUri, "file:", 5)==0 /* IMP: R-57884-37496 */ + if( ((flags & SQLITE_OPEN_URI) /* IMP: R-48725-32206 */ + || AtomicLoad(&sqlite3GlobalConfig.bOpenUri)) /* IMP: R-51689-46548 */ + && nUri>=5 && memcmp(zUri, "file:", 5)==0 /* IMP: R-57884-37496 */ ){ char *zOpt; int eState; /* Parser state when parsing URI */ int iIn; /* Input character index */ int iOut = 0; /* Output character index */ - u64 nByte = nUri+2; /* Bytes of space to allocate */ + u64 nByte = nUri+8; /* Bytes of space to allocate */ - /* Make sure the SQLITE_OPEN_URI flag is set to indicate to the VFS xOpen + /* Make sure the SQLITE_OPEN_URI flag is set to indicate to the VFS xOpen ** method that there may be extra parameters following the file-name. */ flags |= SQLITE_OPEN_URI; for(iIn=0; iIn=0 && octet<256 ); if( octet==0 ){ +#ifndef SQLITE_ENABLE_URI_00_ERROR /* This branch is taken when "%00" appears within the URI. In this ** case we ignore all text in the remainder of the path, name or ** value currently being parsed. So ignore the current character ** and skip to the next "?", "=" or "&", as appropriate. */ - while( (c = zUri[iIn])!=0 && c!='#' + while( (c = zUri[iIn])!=0 && c!='#' && (eState!=0 || c!='?') && (eState!=1 || (c!='=' && c!='&')) && (eState!=2 || c!='&') @@ -2555,6 +3144,12 @@ int sqlite3ParseUri( iIn++; } continue; +#else + /* If ENABLE_URI_00_ERROR is defined, "%00" in a URI is an error. */ + *pzErrMsg = sqlite3_mprintf("unexpected %%00 in uri"); + rc = SQLITE_ERROR; + goto parse_uri_out; +#endif } c = octet; }else if( eState==1 && (c=='&' || c=='=') ){ @@ -2576,10 +3171,9 @@ int sqlite3ParseUri( zFile[iOut++] = c; } if( eState==1 ) zFile[iOut++] = '\0'; - zFile[iOut++] = '\0'; - zFile[iOut++] = '\0'; + memset(zFile+iOut, 0, 4); /* end-of-options + empty journal filenames */ - /* Check if there were any options specified that should be interpreted + /* Check if there were any options specified that should be interpreted ** here. Options that are interpreted here include "vfs" and those that ** correspond to flags that may be passed to the sqlite3_open_v2() ** method. */ @@ -2615,7 +3209,7 @@ int sqlite3ParseUri( if( nOpt==4 && memcmp("mode", zOpt, 4)==0 ){ static struct OpenMode aOpenMode[] = { { "ro", SQLITE_OPEN_READONLY }, - { "rw", SQLITE_OPEN_READWRITE }, + { "rw", SQLITE_OPEN_READWRITE }, { "rwc", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE }, { "memory", SQLITE_OPEN_MEMORY }, { 0, 0 } @@ -2657,11 +3251,14 @@ int sqlite3ParseUri( } }else{ - zFile = sqlite3_malloc64(nUri+2); - if( !zFile ) return SQLITE_NOMEM; - memcpy(zFile, zUri, nUri); - zFile[nUri] = '\0'; - zFile[nUri+1] = '\0'; + zFile = sqlite3_malloc64(nUri+8); + if( !zFile ) return SQLITE_NOMEM_BKPT; + memset(zFile, 0, 4); + zFile += 4; + if( nUri ){ + memcpy(zFile, zUri, nUri); + } + memset(zFile+nUri, 0, 4); flags &= ~SQLITE_OPEN_URI; } @@ -2672,7 +3269,7 @@ int sqlite3ParseUri( } parse_uri_out: if( rc!=SQLITE_OK ){ - sqlite3_free(zFile); + sqlite3_free_filename(zFile); zFile = 0; } *pFlags = flags; @@ -2680,10 +3277,63 @@ int sqlite3ParseUri( return rc; } +/* +** This routine does the core work of extracting URI parameters from a +** database filename for the sqlite3_uri_parameter() interface. +*/ +static const char *uriParameter(const char *zFilename, const char *zParam){ + zFilename += sqlite3Strlen30(zFilename) + 1; + while( ALWAYS(zFilename!=0) && zFilename[0] ){ + int x = strcmp(zFilename, zParam); + zFilename += sqlite3Strlen30(zFilename) + 1; + if( x==0 ) return zFilename; + zFilename += sqlite3Strlen30(zFilename) + 1; + } + return 0; +} + +/* BEGIN SQLCIPHER */ +#if defined(SQLITE_HAS_CODEC) +/* +** Process URI filename query parameters relevant to the SQLite Encryption +** Extension. Return true if any of the relevant query parameters are +** seen and return false if not. +*/ +int sqlite3CodecQueryParameters( + sqlite3 *db, /* Database connection */ + const char *zDb, /* Which schema is being created/attached */ + const char *zUri /* URI filename */ +){ + const char *zKey; + if( zUri==0 ){ + return 0; + }else if( (zKey = uriParameter(zUri, "hexkey"))!=0 && zKey[0] ){ + u8 iByte; + int i; + char zDecoded[40]; + for(i=0, iByte=0; imutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE); if( db->mutex==0 ){ sqlite3_free(db); db = 0; goto opendb_out; } + if( isThreadsafe==0 ){ + sqlite3MutexWarnOnContention(db->mutex); + } } sqlite3_mutex_enter(db->mutex); - db->errMask = 0xff; + db->errMask = (flags & SQLITE_OPEN_EXRESCODE)!=0 ? 0xffffffff : 0xff; db->nDb = 2; - db->magic = SQLITE_MAGIC_BUSY; + db->eOpenState = SQLITE_STATE_BUSY; db->aDb = db->aDbStatic; + db->lookaside.bDisable = 1; + db->lookaside.sz = 0; assert( sizeof(db->aLimit)==sizeof(aHardLimit) ); memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit)); @@ -2789,8 +3430,50 @@ static int openDatabase( db->nextAutovac = -1; db->szMmap = sqlite3GlobalConfig.szMmap; db->nextPagesize = 0; + db->init.azInit = sqlite3StdType; /* Any array of string ptrs will do */ +#ifdef SQLITE_ENABLE_SORTER_MMAP + /* Beginning with version 3.37.0, using the VFS xFetch() API to memory-map + ** the temporary files used to do external sorts (see code in vdbesort.c) + ** is disabled. It can still be used either by defining + ** SQLITE_ENABLE_SORTER_MMAP at compile time or by using the + ** SQLITE_TESTCTRL_SORTER_MMAP test-control at runtime. */ db->nMaxSorterMmap = 0x7FFFFFFF; - db->flags |= SQLITE_ShortColNames | SQLITE_EnableTrigger | SQLITE_CacheSpill +#endif + db->flags |= SQLITE_ShortColNames + | SQLITE_EnableTrigger + | SQLITE_EnableView + | SQLITE_CacheSpill + | SQLITE_AttachCreate + | SQLITE_AttachWrite + | SQLITE_Comments +#if !defined(SQLITE_TRUSTED_SCHEMA) || SQLITE_TRUSTED_SCHEMA+0!=0 + | SQLITE_TrustedSchema +#endif +/* The SQLITE_DQS compile-time option determines the default settings +** for SQLITE_DBCONFIG_DQS_DDL and SQLITE_DBCONFIG_DQS_DML. +** +** SQLITE_DQS SQLITE_DBCONFIG_DQS_DDL SQLITE_DBCONFIG_DQS_DML +** ---------- ----------------------- ----------------------- +** undefined on on +** 3 on on +** 2 on off +** 1 off on +** 0 off off +** +** Legacy behavior is 3 (double-quoted string literals are allowed anywhere) +** and so that is the default. But developers are encouraged to use +** -DSQLITE_DQS=0 (best) or -DSQLITE_DQS=1 (second choice) if possible. +*/ +#if !defined(SQLITE_DQS) +# define SQLITE_DQS 3 +#endif +#if (SQLITE_DQS&1)==1 + | SQLITE_DqsDML +#endif +#if (SQLITE_DQS&2)==2 + | SQLITE_DqsDDL +#endif + #if !defined(SQLITE_DEFAULT_AUTOMATIC_INDEX) || SQLITE_DEFAULT_AUTOMATIC_INDEX | SQLITE_AutoIndex #endif @@ -2814,6 +3497,21 @@ static int openDatabase( #endif #if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK) | SQLITE_CellSizeCk +#endif +#if defined(SQLITE_ENABLE_FTS3_TOKENIZER) + | SQLITE_Fts3Tokenizer +#endif +#if defined(SQLITE_ENABLE_QPSG) + | SQLITE_EnableQPSG +#endif +#if defined(SQLITE_DEFAULT_DEFENSIVE) + | SQLITE_Defensive +#endif +#if defined(SQLITE_DEFAULT_LEGACY_ALTER_TABLE) + | SQLITE_LegacyAlter +#endif +#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) + | SQLITE_StmtScanStatus #endif ; sqlite3HashInit(&db->aCollSeq); @@ -2832,51 +3530,89 @@ static int openDatabase( createCollation(db, sqlite3StrBINARY, SQLITE_UTF16BE, 0, binCollFunc, 0); createCollation(db, sqlite3StrBINARY, SQLITE_UTF16LE, 0, binCollFunc, 0); createCollation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc, 0); - createCollation(db, "RTRIM", SQLITE_UTF8, (void*)1, binCollFunc, 0); + createCollation(db, "RTRIM", SQLITE_UTF8, 0, rtrimCollFunc, 0); if( db->mallocFailed ){ goto opendb_out; } - /* EVIDENCE-OF: R-08308-17224 The default collating function for all - ** strings is BINARY. - */ - db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, sqlite3StrBINARY, 0); - assert( db->pDfltColl!=0 ); - /* Parse the filename/URI argument. */ +#if SQLITE_OS_UNIX && defined(SQLITE_OS_KV_OPTIONAL) + /* Process magic filenames ":localStorage:" and ":sessionStorage:" */ + if( zFilename && zFilename[0]==':' ){ + if( strcmp(zFilename, ":localStorage:")==0 ){ + zFilename = "file:local?vfs=kvvfs"; + flags |= SQLITE_OPEN_URI; + }else if( strcmp(zFilename, ":sessionStorage:")==0 ){ + zFilename = "file:session?vfs=kvvfs"; + flags |= SQLITE_OPEN_URI; + } + } +#endif /* SQLITE_OS_UNIX && defined(SQLITE_OS_KV_OPTIONAL) */ + + /* Parse the filename/URI argument + ** + ** Only allow sensible combinations of bits in the flags argument. + ** Throw an error if any non-sense combination is used. If we + ** do not block illegal combinations here, it could trigger + ** assert() statements in deeper layers. Sensible combinations + ** are: + ** + ** 1: SQLITE_OPEN_READONLY + ** 2: SQLITE_OPEN_READWRITE + ** 6: SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE + */ db->openFlags = flags; - rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg); + assert( SQLITE_OPEN_READONLY == 0x01 ); + assert( SQLITE_OPEN_READWRITE == 0x02 ); + assert( SQLITE_OPEN_CREATE == 0x04 ); + testcase( (1<<(flags&7))==0x02 ); /* READONLY */ + testcase( (1<<(flags&7))==0x04 ); /* READWRITE */ + testcase( (1<<(flags&7))==0x40 ); /* READWRITE | CREATE */ + if( ((1<<(flags&7)) & 0x46)==0 ){ + rc = SQLITE_MISUSE_BKPT; /* IMP: R-18321-05872 */ + }else{ + if( zFilename==0 ) zFilename = ":memory:"; + rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg); + } if( rc!=SQLITE_OK ){ if( rc==SQLITE_NOMEM ) sqlite3OomFault(db); sqlite3ErrorWithMsg(db, rc, zErrMsg ? "%s" : 0, zErrMsg); sqlite3_free(zErrMsg); goto opendb_out; } + assert( db->pVfs!=0 ); +#if SQLITE_OS_KV || defined(SQLITE_OS_KV_OPTIONAL) + if( sqlite3_stricmp(db->pVfs->zName, "kvvfs")==0 ){ + db->temp_store = 2; + } +#endif /* Open the backend database driver */ rc = sqlite3BtreeOpen(db->pVfs, zOpen, db, &db->aDb[0].pBt, 0, flags | SQLITE_OPEN_MAIN_DB); if( rc!=SQLITE_OK ){ if( rc==SQLITE_IOERR_NOMEM ){ - rc = SQLITE_NOMEM; + rc = SQLITE_NOMEM_BKPT; } sqlite3Error(db, rc); goto opendb_out; } sqlite3BtreeEnter(db->aDb[0].pBt); db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt); - if( !db->mallocFailed ) ENC(db) = SCHEMA_ENC(db); + if( !db->mallocFailed ){ + sqlite3SetTextEncoding(db, SCHEMA_ENC(db)); + } sqlite3BtreeLeave(db->aDb[0].pBt); db->aDb[1].pSchema = sqlite3SchemaGet(db, 0); - /* The default safety_level for the main database is 'full'; for the temp - ** database it is 'NONE'. This matches the pager layer defaults. + /* The default safety_level for the main database is FULL; for the temp + ** database it is OFF. This matches the pager layer defaults. */ - db->aDb[0].zName = "main"; - db->aDb[0].safety_level = 3; - db->aDb[1].zName = "temp"; - db->aDb[1].safety_level = 1; + db->aDb[0].zDbSName = "main"; + db->aDb[0].safety_level = SQLITE_DEFAULT_SYNCHRONOUS+1; + db->aDb[1].zDbSName = "temp"; + db->aDb[1].safety_level = PAGER_SYNCHRONOUS_OFF; - db->magic = SQLITE_MAGIC_OPEN; + db->eOpenState = SQLITE_STATE_OPEN; if( db->mallocFailed ){ goto opendb_out; } @@ -2886,12 +3622,18 @@ static int openDatabase( ** is accessed. */ sqlite3Error(db, SQLITE_OK); - sqlite3RegisterBuiltinFunctions(db); + sqlite3RegisterPerConnectionBuiltinFunctions(db); + rc = sqlite3_errcode(db); + + + /* Load compiled-in extensions */ + for(i=0; rc==SQLITE_OK && imallocFailed ){ - extern int sqlite3Fts1Init(sqlite3*); - rc = sqlite3Fts1Init(db); - } -#endif - -#ifdef SQLITE_ENABLE_FTS2 - if( !db->mallocFailed && rc==SQLITE_OK ){ - extern int sqlite3Fts2Init(sqlite3*); - rc = sqlite3Fts2Init(db); - } -#endif - -#ifdef SQLITE_ENABLE_FTS3 /* automatically defined by SQLITE_ENABLE_FTS4 */ - if( !db->mallocFailed && rc==SQLITE_OK ){ - rc = sqlite3Fts3Init(db); - } -#endif - -#ifdef SQLITE_ENABLE_FTS5 - if( !db->mallocFailed && rc==SQLITE_OK ){ - rc = sqlite3Fts5Init(db); - } -#endif - -#ifdef SQLITE_ENABLE_ICU - if( !db->mallocFailed && rc==SQLITE_OK ){ - rc = sqlite3IcuInit(db); - } -#endif - -#ifdef SQLITE_ENABLE_RTREE - if( !db->mallocFailed && rc==SQLITE_OK){ - rc = sqlite3RtreeInit(db); - } -#endif - -#ifdef SQLITE_ENABLE_DBSTAT_VTAB - if( !db->mallocFailed && rc==SQLITE_OK){ - rc = sqlite3DbstatRegister(db); - } -#endif - -#ifdef SQLITE_ENABLE_JSON1 - if( !db->mallocFailed && rc==SQLITE_OK){ - rc = sqlite3Json1Init(db); - } +#ifdef SQLITE_ENABLE_INTERNAL_FUNCTIONS + /* Testing use only!!! The -DSQLITE_ENABLE_INTERNAL_FUNCTIONS=1 compile-time + ** option gives access to internal functions by default. + ** Testing use only!!! */ + db->mDbFlags |= DBFLAG_InternalFunc; #endif /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking @@ -2975,12 +3674,12 @@ static int openDatabase( sqlite3_mutex_leave(db->mutex); } rc = sqlite3_errcode(db); - assert( db!=0 || rc==SQLITE_NOMEM ); - if( rc==SQLITE_NOMEM ){ + assert( db!=0 || (rc&0xff)==SQLITE_NOMEM ); + if( (rc&0xff)==SQLITE_NOMEM ){ sqlite3_close(db); db = 0; }else if( rc!=SQLITE_OK ){ - db->magic = SQLITE_MAGIC_SICK; + db->eOpenState = SQLITE_STATE_SICK; } *ppDb = db; #ifdef SQLITE_ENABLE_SQLLOG @@ -2990,31 +3689,22 @@ static int openDatabase( sqlite3GlobalConfig.xSqllog(pArg, db, zFilename, 0); } #endif +/* BEGIN SQLCIPHER */ #if defined(SQLITE_HAS_CODEC) - if( rc==SQLITE_OK ){ - const char *zHexKey = sqlite3_uri_parameter(zOpen, "hexkey"); - if( zHexKey && zHexKey[0] ){ - u8 iByte; - int i; - char zKey[40]; - for(i=0, iByte=0; imutex); + for(p=db->pDbData; p; p=p->pNext){ + if( strcmp(p->zName, zName)==0 ){ + void *pResult = p->pData; + sqlite3_mutex_leave(db->mutex); + return pResult; + } + } + sqlite3_mutex_leave(db->mutex); + return 0; +} + +/* +** Add new client data to a database connection. +*/ +int sqlite3_set_clientdata( + sqlite3 *db, /* Attach client data to this connection */ + const char *zName, /* Name of the client data */ + void *pData, /* The client data itself */ + void (*xDestructor)(void*) /* Destructor */ +){ + DbClientData *p, **pp; + sqlite3_mutex_enter(db->mutex); + pp = &db->pDbData; + for(p=db->pDbData; p && strcmp(p->zName,zName); p=p->pNext){ + pp = &p->pNext; + } + if( p ){ + assert( p->pData!=0 ); + if( p->xDestructor ) p->xDestructor(p->pData); + if( pData==0 ){ + *pp = p->pNext; + sqlite3_free(p); + sqlite3_mutex_leave(db->mutex); + return SQLITE_OK; + } + }else if( pData==0 ){ + sqlite3_mutex_leave(db->mutex); + return SQLITE_OK; + }else{ + size_t n = strlen(zName); + p = sqlite3_malloc64( SZ_DBCLIENTDATA(n+1) ); + if( p==0 ){ + if( xDestructor ) xDestructor(pData); + sqlite3_mutex_leave(db->mutex); + return SQLITE_NOMEM; + } + memcpy(p->zName, zName, n+1); + p->pNext = db->pDbData; + db->pDbData = p; + } + p->pData = pData; + p->xDestructor = xDestructor; + sqlite3_mutex_leave(db->mutex); + return SQLITE_OK; +} + + #ifndef SQLITE_OMIT_DEPRECATED /* ** This function is now an anachronism. It used to be used to recover from a @@ -3205,7 +3958,7 @@ int sqlite3_get_autocommit(sqlite3 *db){ /* ** The following routines are substitutes for constants SQLITE_CORRUPT, -** SQLITE_MISUSE, SQLITE_CANTOPEN, SQLITE_IOERR and possibly other error +** SQLITE_MISUSE, SQLITE_CANTOPEN, SQLITE_NOMEM and possibly other error ** constants. They serve two purposes: ** ** 1. Serve as a convenient place to set a breakpoint in a debugger @@ -3214,28 +3967,41 @@ int sqlite3_get_autocommit(sqlite3 *db){ ** 2. Invoke sqlite3_log() to provide the source code location where ** a low-level error is first detected. */ +int sqlite3ReportError(int iErr, int lineno, const char *zType){ + sqlite3_log(iErr, "%s at line %d of [%.10s]", + zType, lineno, 20+sqlite3_sourceid()); + return iErr; +} int sqlite3CorruptError(int lineno){ testcase( sqlite3GlobalConfig.xLog!=0 ); - sqlite3_log(SQLITE_CORRUPT, - "database corruption at line %d of [%.10s]", - lineno, 20+sqlite3_sourceid()); - return SQLITE_CORRUPT; + return sqlite3ReportError(SQLITE_CORRUPT, lineno, "database corruption"); } int sqlite3MisuseError(int lineno){ testcase( sqlite3GlobalConfig.xLog!=0 ); - sqlite3_log(SQLITE_MISUSE, - "misuse at line %d of [%.10s]", - lineno, 20+sqlite3_sourceid()); - return SQLITE_MISUSE; + return sqlite3ReportError(SQLITE_MISUSE, lineno, "misuse"); } int sqlite3CantopenError(int lineno){ testcase( sqlite3GlobalConfig.xLog!=0 ); - sqlite3_log(SQLITE_CANTOPEN, - "cannot open file at line %d of [%.10s]", - lineno, 20+sqlite3_sourceid()); - return SQLITE_CANTOPEN; + return sqlite3ReportError(SQLITE_CANTOPEN, lineno, "cannot open file"); } - +#if defined(SQLITE_DEBUG) || defined(SQLITE_ENABLE_CORRUPT_PGNO) +int sqlite3CorruptPgnoError(int lineno, Pgno pgno){ + char zMsg[100]; + sqlite3_snprintf(sizeof(zMsg), zMsg, "database corruption page %d", pgno); + testcase( sqlite3GlobalConfig.xLog!=0 ); + return sqlite3ReportError(SQLITE_CORRUPT, lineno, zMsg); +} +#endif +#ifdef SQLITE_DEBUG +int sqlite3NomemError(int lineno){ + testcase( sqlite3GlobalConfig.xLog!=0 ); + return sqlite3ReportError(SQLITE_NOMEM, lineno, "OOM"); +} +int sqlite3IoerrnomemError(int lineno){ + testcase( sqlite3GlobalConfig.xLog!=0 ); + return sqlite3ReportError(SQLITE_IOERR_NOMEM, lineno, "I/O OOM error"); +} +#endif #ifndef SQLITE_OMIT_DEPRECATED /* @@ -3292,22 +4058,19 @@ int sqlite3_table_column_metadata( /* Locate the table in question */ pTab = sqlite3FindTable(db, zTableName, zDbName); - if( !pTab || pTab->pSelect ){ + if( !pTab || IsView(pTab) ){ pTab = 0; goto error_out; } /* Find the column for which info is requested */ if( zColumnName==0 ){ - /* Query for existance of table only */ + /* Query for existence of table only */ }else{ - for(iCol=0; iColnCol; iCol++){ + iCol = sqlite3ColumnIndex(pTab, zColumnName); + if( iCol>=0 ){ pCol = &pTab->aCol[iCol]; - if( 0==sqlite3StrICmp(pCol->zName, zColumnName) ){ - break; - } - } - if( iCol==pTab->nCol ){ + }else{ if( HasRowid(pTab) && sqlite3IsRowid(zColumnName) ){ iCol = pTab->iPKey; pCol = iCol>=0 ? &pTab->aCol[iCol] : 0; @@ -3321,16 +4084,16 @@ int sqlite3_table_column_metadata( /* The following block stores the meta information that will be returned ** to the caller in local variables zDataType, zCollSeq, notnull, primarykey ** and autoinc. At this point there are two possibilities: - ** - ** 1. The specified column name was rowid", "oid" or "_rowid_" - ** and there is no explicitly declared IPK column. ** - ** 2. The table is not a view and the column name identified an + ** 1. The specified column name was rowid", "oid" or "_rowid_" + ** and there is no explicitly declared IPK column. + ** + ** 2. The table is not a view and the column name identified an ** explicitly declared column. Copy meta information from *pCol. - */ + */ if( pCol ){ - zDataType = pCol->zType; - zCollSeq = pCol->zColl; + zDataType = sqlite3ColumnType(pCol,0); + zCollSeq = sqlite3ColumnColl(pCol); notnull = pCol->notNull!=0; primarykey = (pCol->colFlags & COLFLAG_PRIMKEY)!=0; autoinc = pTab->iPKey==iCol && (pTab->tabFlags & TF_Autoincrement)!=0; @@ -3377,10 +4140,10 @@ int sqlite3_sleep(int ms){ pVfs = sqlite3_vfs_find(0); if( pVfs==0 ) return 0; - /* This function works in milliseconds, but the underlying OsSleep() + /* This function works in milliseconds, but the underlying OsSleep() ** API uses microseconds. Hence the 1000's. */ - rc = (sqlite3OsSleep(pVfs, 1000*ms)/1000); + rc = (sqlite3OsSleep(pVfs, ms<0 ? 0 : 1000*ms)/1000); return rc; } @@ -3426,10 +4189,23 @@ int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){ }else if( op==SQLITE_FCNTL_JOURNAL_POINTER ){ *(sqlite3_file**)pArg = sqlite3PagerJrnlFile(pPager); rc = SQLITE_OK; - }else if( fd->pMethods ){ - rc = sqlite3OsFileControl(fd, op, pArg); + }else if( op==SQLITE_FCNTL_DATA_VERSION ){ + *(unsigned int*)pArg = sqlite3PagerDataVersion(pPager); + rc = SQLITE_OK; + }else if( op==SQLITE_FCNTL_RESERVE_BYTES ){ + int iNew = *(int*)pArg; + *(int*)pArg = sqlite3BtreeGetRequestedReserve(pBtree); + if( iNew>=0 && iNew<=255 ){ + sqlite3BtreeSetPageSize(pBtree, 0, iNew, 0); + } + rc = SQLITE_OK; + }else if( op==SQLITE_FCNTL_RESET_CACHE ){ + sqlite3BtreeClearCache(pBtree); + rc = SQLITE_OK; }else{ - rc = SQLITE_NOTFOUND; + int nSave = db->busyHandler.nBusy; + rc = sqlite3OsFileControl(fd, op, pArg); + db->busyHandler.nBusy = nSave; } sqlite3BtreeLeave(pBtree); } @@ -3442,7 +4218,7 @@ int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){ */ int sqlite3_test_control(int op, ...){ int rc = 0; -#ifdef SQLITE_OMIT_BUILTIN_TEST +#ifdef SQLITE_UNTESTABLE UNUSED_PARAMETER(op); #else va_list ap; @@ -3467,15 +4243,60 @@ int sqlite3_test_control(int op, ...){ break; } - /* - ** Reset the PRNG back to its uninitialized state. The next call - ** to sqlite3_randomness() will reseed the PRNG using a single call - ** to the xRandomness method of the default VFS. + /* sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SEED, int x, sqlite3 *db); + ** + ** Control the seed for the pseudo-random number generator (PRNG) that + ** is built into SQLite. Cases: + ** + ** x!=0 && db!=0 Seed the PRNG to the current value of the + ** schema cookie in the main database for db, or + ** x if the schema cookie is zero. This case + ** is convenient to use with database fuzzers + ** as it allows the fuzzer some control over the + ** the PRNG seed. + ** + ** x!=0 && db==0 Seed the PRNG to the value of x. + ** + ** x==0 && db==0 Revert to default behavior of using the + ** xRandomness method on the primary VFS. + ** + ** This test-control also resets the PRNG so that the new seed will + ** be used for the next call to sqlite3_randomness(). */ - case SQLITE_TESTCTRL_PRNG_RESET: { +#ifndef SQLITE_OMIT_WSD + case SQLITE_TESTCTRL_PRNG_SEED: { + int x = va_arg(ap, int); + int y; + sqlite3 *db = va_arg(ap, sqlite3*); + assert( db==0 || db->aDb[0].pSchema!=0 ); + if( db && (y = db->aDb[0].pSchema->schema_cookie)!=0 ){ x = y; } + sqlite3Config.iPrngSeed = x; sqlite3_randomness(0,0); break; } +#endif + + /* sqlite3_test_control(SQLITE_TESTCTRL_FK_NO_ACTION, sqlite3 *db, int b); + ** + ** If b is true, then activate the SQLITE_FkNoAction setting. If b is + ** false then clear that setting. If the SQLITE_FkNoAction setting is + ** enabled, all foreign key ON DELETE and ON UPDATE actions behave as if + ** they were NO ACTION, regardless of how they are defined. + ** + ** NB: One must usually run "PRAGMA writable_schema=RESET" after + ** using this test-control, before it will take full effect. failing + ** to reset the schema can result in some unexpected behavior. + */ + case SQLITE_TESTCTRL_FK_NO_ACTION: { + sqlite3 *db = va_arg(ap, sqlite3*); + int b = va_arg(ap, int); + if( b ){ + db->flags |= SQLITE_FkNoAction; + }else{ + db->flags &= ~SQLITE_FkNoAction; + } + break; + } /* ** sqlite3_test_control(BITVEC_TEST, size, program) @@ -3504,12 +4325,16 @@ int sqlite3_test_control(int op, ...){ ** sqlite3_test_control(). */ case SQLITE_TESTCTRL_FAULT_INSTALL: { - /* MSVC is picky about pulling func ptrs from va lists. - ** http://support.microsoft.com/kb/47961 + /* A bug in MSVC prevents it from understanding pointers to functions + ** types in the second argument to va_arg(). Work around the problem + ** using a typedef. + ** http://support.microsoft.com/kb/47961 <-- dead hyperlink + ** Search at http://web.archive.org/ to find the 2015-03-16 archive + ** of the link above to see the original text. ** sqlite3GlobalConfig.xTestCallback = va_arg(ap, int(*)(int)); */ - typedef int(*TESTCALLBACKFUNC_t)(int); - sqlite3GlobalConfig.xTestCallback = va_arg(ap, TESTCALLBACKFUNC_t); + typedef int(*sqlite3FaultFuncType)(int); + sqlite3GlobalConfig.xTestCallback = va_arg(ap, sqlite3FaultFuncType); rc = sqlite3FaultSim(0); break; } @@ -3517,7 +4342,7 @@ int sqlite3_test_control(int op, ...){ /* ** sqlite3_test_control(BENIGN_MALLOC_HOOKS, xBegin, xEnd) ** - ** Register hooks to call to indicate which malloc() failures + ** Register hooks to call to indicate which malloc() failures ** are benign. */ case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS: { @@ -3568,6 +4393,29 @@ int sqlite3_test_control(int op, ...){ volatile int x = 0; assert( /*side-effects-ok*/ (x = va_arg(ap,int))!=0 ); rc = x; +#if defined(SQLITE_DEBUG) + /* Invoke these debugging routines so that the compiler does not + ** issue "defined but not used" warnings. */ + if( x==9999 ){ + sqlite3ShowExpr(0); + sqlite3ShowExprList(0); + sqlite3ShowIdList(0); + sqlite3ShowSrcList(0); + sqlite3ShowWith(0); + sqlite3ShowUpsert(0); +#ifndef SQLITE_OMIT_TRIGGER + sqlite3ShowTriggerStep(0); + sqlite3ShowTriggerStepList(0); + sqlite3ShowTrigger(0); + sqlite3ShowTriggerList(0); +#endif +#ifndef SQLITE_OMIT_WINDOWFUNC + sqlite3ShowWindow(0); + sqlite3ShowWinFunc(0); +#endif + sqlite3ShowSelect(0); + } +#endif break; } @@ -3578,7 +4426,7 @@ int sqlite3_test_control(int op, ...){ ** This action provides a run-time test to see how the ALWAYS and ** NEVER macros were defined at compile-time. ** - ** The return value is ALWAYS(X). + ** The return value is ALWAYS(X) if X is true, or 0 if X is false. ** ** The recommended test is X==2. If the return value is 2, that means ** ALWAYS() and NEVER() are both no-op pass-through macros, which is the @@ -3601,7 +4449,7 @@ int sqlite3_test_control(int op, ...){ */ case SQLITE_TESTCTRL_ALWAYS: { int x = va_arg(ap,int); - rc = ALWAYS(x); + rc = x ? ALWAYS(x) : 0; break; } @@ -3615,29 +4463,15 @@ int sqlite3_test_control(int op, ...){ ** 10 little-endian, determined at run-time ** 432101 big-endian, determined at compile-time ** 123410 little-endian, determined at compile-time - */ + */ case SQLITE_TESTCTRL_BYTEORDER: { rc = SQLITE_BYTEORDER*100 + SQLITE_LITTLEENDIAN*10 + SQLITE_BIGENDIAN; break; } - /* sqlite3_test_control(SQLITE_TESTCTRL_RESERVE, sqlite3 *db, int N) - ** - ** Set the nReserve size to N for the main database on the database - ** connection db. - */ - case SQLITE_TESTCTRL_RESERVE: { - sqlite3 *db = va_arg(ap, sqlite3*); - int x = va_arg(ap,int); - sqlite3_mutex_enter(db->mutex); - sqlite3BtreeSetPageSize(db->aDb[0].pBt, 0, x, 0); - sqlite3_mutex_leave(db->mutex); - break; - } - /* sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, sqlite3 *db, int N) ** - ** Enable or disable various optimizations for testing purposes. The + ** Enable or disable various optimizations for testing purposes. The ** argument N is a bitmask of optimizations to be disabled. For normal ** operation N should be 0. The idea is that a test program (like the ** SQL Logic Test or SLT test module) can run the same SQL multiple times @@ -3646,52 +4480,54 @@ int sqlite3_test_control(int op, ...){ */ case SQLITE_TESTCTRL_OPTIMIZATIONS: { sqlite3 *db = va_arg(ap, sqlite3*); - db->dbOptFlags = (u16)(va_arg(ap, int) & 0xffff); + db->dbOptFlags = va_arg(ap, u32); break; } -#ifdef SQLITE_N_KEYWORD - /* sqlite3_test_control(SQLITE_TESTCTRL_ISKEYWORD, const char *zWord) + /* sqlite3_test_control(SQLITE_TESTCTRL_GETOPT, sqlite3 *db, int *N) ** - ** If zWord is a keyword recognized by the parser, then return the - ** number of keywords. Or if zWord is not a keyword, return 0. - ** - ** This test feature is only available in the amalgamation since - ** the SQLITE_N_KEYWORD macro is not defined in this file if SQLite - ** is built using separate source files. + ** Write the current optimization settings into *N. A zero bit means that + ** the optimization is on, and a 1 bit means that the optimization is off. */ - case SQLITE_TESTCTRL_ISKEYWORD: { - const char *zWord = va_arg(ap, const char*); - int n = sqlite3Strlen30(zWord); - rc = (sqlite3KeywordCode((u8*)zWord, n)!=TK_ID) ? SQLITE_N_KEYWORD : 0; + case SQLITE_TESTCTRL_GETOPT: { + sqlite3 *db = va_arg(ap, sqlite3*); + int *pN = va_arg(ap, int*); + *pN = db->dbOptFlags; break; } -#endif - /* sqlite3_test_control(SQLITE_TESTCTRL_SCRATCHMALLOC, sz, &pNew, pFree); + /* sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, onoff, xAlt); + ** + ** If parameter onoff is 1, subsequent calls to localtime() fail. + ** If 2, then invoke xAlt() instead of localtime(). If 0, normal + ** processing. + ** + ** xAlt arguments are void pointers, but they really want to be: ** - ** Pass pFree into sqlite3ScratchFree(). - ** If sz>0 then allocate a scratch buffer into pNew. + ** int xAlt(const time_t*, struct tm*); + ** + ** xAlt should write results in to struct tm object of its 2nd argument + ** and return zero on success, or return non-zero on failure. */ - case SQLITE_TESTCTRL_SCRATCHMALLOC: { - void *pFree, **ppNew; - int sz; - sz = va_arg(ap, int); - ppNew = va_arg(ap, void**); - pFree = va_arg(ap, void*); - if( sz ) *ppNew = sqlite3ScratchMalloc(sz); - sqlite3ScratchFree(pFree); + case SQLITE_TESTCTRL_LOCALTIME_FAULT: { + sqlite3GlobalConfig.bLocaltimeFault = va_arg(ap, int); + if( sqlite3GlobalConfig.bLocaltimeFault==2 ){ + typedef int(*sqlite3LocaltimeType)(const void*,void*); + sqlite3GlobalConfig.xAltLocaltime = va_arg(ap, sqlite3LocaltimeType); + }else{ + sqlite3GlobalConfig.xAltLocaltime = 0; + } break; } - /* sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, int onoff); + /* sqlite3_test_control(SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, sqlite3*); ** - ** If parameter onoff is non-zero, configure the wrappers so that all - ** subsequent calls to localtime() and variants fail. If onoff is zero, - ** undo this setting. + ** Toggle the ability to use internal functions on or off for + ** the database connection given in the argument. */ - case SQLITE_TESTCTRL_LOCALTIME_FAULT: { - sqlite3GlobalConfig.bLocaltimeFault = va_arg(ap, int); + case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS: { + sqlite3 *db = va_arg(ap, sqlite3*); + db->mDbFlags ^= DBFLAG_InternalFunc; break; } @@ -3701,22 +4537,49 @@ int sqlite3_test_control(int op, ...){ ** formed and never corrupt. This flag is clear by default, indicating that ** database files might have arbitrary corruption. Setting the flag during ** testing causes certain assert() statements in the code to be activated - ** that demonstrat invariants on well-formed database files. + ** that demonstrate invariants on well-formed database files. */ case SQLITE_TESTCTRL_NEVER_CORRUPT: { sqlite3GlobalConfig.neverCorrupt = va_arg(ap, int); break; } + /* sqlite3_test_control(SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS, int); + ** + ** Set or clear a flag that causes SQLite to verify that type, name, + ** and tbl_name fields of the sqlite_schema table. This is normally + ** on, but it is sometimes useful to turn it off for testing. + ** + ** 2020-07-22: Disabling EXTRA_SCHEMA_CHECKS also disables the + ** verification of rootpage numbers when parsing the schema. This + ** is useful to make it easier to reach strange internal error states + ** during testing. The EXTRA_SCHEMA_CHECKS setting is always enabled + ** in production. + */ + case SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS: { + sqlite3GlobalConfig.bExtraSchemaChecks = va_arg(ap, int); + break; + } + + /* Set the threshold at which OP_Once counters reset back to zero. + ** By default this is 0x7ffffffe (over 2 billion), but that value is + ** too big to test in a reasonable amount of time, so this control is + ** provided to set a small and easily reachable reset value. + */ + case SQLITE_TESTCTRL_ONCE_RESET_THRESHOLD: { + sqlite3GlobalConfig.iOnceResetThreshold = va_arg(ap, int); + break; + } /* sqlite3_test_control(SQLITE_TESTCTRL_VDBE_COVERAGE, xCallback, ptr); ** - ** Set the VDBE coverage callback function to xCallback with context + ** Set the VDBE coverage callback function to xCallback with context ** pointer ptr. */ case SQLITE_TESTCTRL_VDBE_COVERAGE: { #ifdef SQLITE_VDBE_COVERAGE - typedef void (*branch_callback)(void*,int,u8,u8); + typedef void (*branch_callback)(void*,unsigned int, + unsigned char,unsigned char); sqlite3GlobalConfig.xVdbeBranch = va_arg(ap,branch_callback); sqlite3GlobalConfig.pVdbeBranchArg = va_arg(ap,void*); #endif @@ -3740,13 +4603,15 @@ int sqlite3_test_control(int op, ...){ break; } - /* sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, db, dbName, onOff, tnum); + /* sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, db, dbName, mode, tnum); ** ** This test control is used to create imposter tables. "db" is a pointer ** to the database connection. dbName is the database name (ex: "main" or - ** "temp") which will receive the imposter. "onOff" turns imposter mode on - ** or off. "tnum" is the root page of the b-tree to which the imposter - ** table should connect. + ** "temp") which will receive the imposter. "mode" turns imposter mode on + ** or off. mode==0 means imposter mode is off. mode==1 means imposter mode + ** is on. mode==2 means imposter mode is on but results in an imposter + ** table that is read-only unless writable_schema is on. "tnum" is the + ** root page of the b-tree to which the imposter table should connect. ** ** Enable imposter mode only when the schema has already been parsed. Then ** run a single CREATE TABLE statement to construct the imposter table in @@ -3758,25 +4623,249 @@ int sqlite3_test_control(int op, ...){ */ case SQLITE_TESTCTRL_IMPOSTER: { sqlite3 *db = va_arg(ap, sqlite3*); + int iDb; sqlite3_mutex_enter(db->mutex); - db->init.iDb = sqlite3FindDbName(db, va_arg(ap,const char*)); - db->init.busy = db->init.imposterTable = va_arg(ap,int); - db->init.newTnum = va_arg(ap,int); - if( db->init.busy==0 && db->init.newTnum>0 ){ - sqlite3ResetAllSchemasOfConnection(db); + iDb = sqlite3FindDbName(db, va_arg(ap,const char*)); + if( iDb>=0 ){ + db->init.iDb = iDb; + db->init.busy = db->init.imposterTable = va_arg(ap,int); + db->init.newTnum = va_arg(ap,int); + if( db->init.busy==0 && db->init.newTnum>0 ){ + sqlite3ResetAllSchemasOfConnection(db); + } } sqlite3_mutex_leave(db->mutex); break; } + +#if defined(YYCOVERAGE) + /* sqlite3_test_control(SQLITE_TESTCTRL_PARSER_COVERAGE, FILE *out) + ** + ** This test control (only available when SQLite is compiled with + ** -DYYCOVERAGE) writes a report onto "out" that shows all + ** state/lookahead combinations in the parser state machine + ** which are never exercised. If any state is missed, make the + ** return code SQLITE_ERROR. + */ + case SQLITE_TESTCTRL_PARSER_COVERAGE: { + FILE *out = va_arg(ap, FILE*); + if( sqlite3ParserCoverage(out) ) rc = SQLITE_ERROR; + break; + } +#endif /* defined(YYCOVERAGE) */ + + /* sqlite3_test_control(SQLITE_TESTCTRL_RESULT_INTREAL, sqlite3_context*); + ** + ** This test-control causes the most recent sqlite3_result_int64() value + ** to be interpreted as a MEM_IntReal instead of as an MEM_Int. Normally, + ** MEM_IntReal values only arise during an INSERT operation of integer + ** values into a REAL column, so they can be challenging to test. This + ** test-control enables us to write an intreal() SQL function that can + ** inject an intreal() value at arbitrary places in an SQL statement, + ** for testing purposes. + */ + case SQLITE_TESTCTRL_RESULT_INTREAL: { + sqlite3_context *pCtx = va_arg(ap, sqlite3_context*); + sqlite3ResultIntReal(pCtx); + break; + } + + /* sqlite3_test_control(SQLITE_TESTCTRL_SEEK_COUNT, + ** sqlite3 *db, // Database connection + ** u64 *pnSeek // Write seek count here + ** ); + ** + ** This test-control queries the seek-counter on the "main" database + ** file. The seek-counter is written into *pnSeek and is then reset. + ** The seek-count is only available if compiled with SQLITE_DEBUG. + */ + case SQLITE_TESTCTRL_SEEK_COUNT: { + sqlite3 *db = va_arg(ap, sqlite3*); + u64 *pn = va_arg(ap, sqlite3_uint64*); + *pn = sqlite3BtreeSeekCount(db->aDb->pBt); + (void)db; /* Silence harmless unused variable warning */ + break; + } + + /* sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, op, ptr) + ** + ** "ptr" is a pointer to a u32. + ** + ** op==0 Store the current sqlite3TreeTrace in *ptr + ** op==1 Set sqlite3TreeTrace to the value *ptr + ** op==2 Store the current sqlite3WhereTrace in *ptr + ** op==3 Set sqlite3WhereTrace to the value *ptr + */ + case SQLITE_TESTCTRL_TRACEFLAGS: { + int opTrace = va_arg(ap, int); + u32 *ptr = va_arg(ap, u32*); + switch( opTrace ){ + case 0: *ptr = sqlite3TreeTrace; break; + case 1: sqlite3TreeTrace = *ptr; break; + case 2: *ptr = sqlite3WhereTrace; break; + case 3: sqlite3WhereTrace = *ptr; break; + } + break; + } + + /* sqlite3_test_control(SQLITE_TESTCTRL_LOGEST, + ** double fIn, // Input value + ** int *pLogEst, // sqlite3LogEstFromDouble(fIn) + ** u64 *pInt, // sqlite3LogEstToInt(*pLogEst) + ** int *pLogEst2 // sqlite3LogEst(*pInt) + ** ); + ** + ** Test access for the LogEst conversion routines. + */ + case SQLITE_TESTCTRL_LOGEST: { + double rIn = va_arg(ap, double); + LogEst rLogEst = sqlite3LogEstFromDouble(rIn); + int *pI1 = va_arg(ap,int*); + u64 *pU64 = va_arg(ap,u64*); + int *pI2 = va_arg(ap,int*); + *pI1 = rLogEst; + *pU64 = sqlite3LogEstToInt(rLogEst); + *pI2 = sqlite3LogEst(*pU64); + break; + } + +#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_WSD) + /* sqlite3_test_control(SQLITE_TESTCTRL_TUNE, id, *piValue) + ** + ** If "id" is an integer between 1 and SQLITE_NTUNE then set the value + ** of the id-th tuning parameter to *piValue. If "id" is between -1 + ** and -SQLITE_NTUNE, then write the current value of the (-id)-th + ** tuning parameter into *piValue. + ** + ** Tuning parameters are for use during transient development builds, + ** to help find the best values for constants in the query planner. + ** Access tuning parameters using the Tuning(ID) macro. Set the + ** parameters in the CLI using ".testctrl tune ID VALUE". + ** + ** Transient use only. Tuning parameters should not be used in + ** checked-in code. + */ + case SQLITE_TESTCTRL_TUNE: { + int id = va_arg(ap, int); + int *piValue = va_arg(ap, int*); + if( id>0 && id<=SQLITE_NTUNE ){ + Tuning(id) = *piValue; + }else if( id<0 && id>=-SQLITE_NTUNE ){ + *piValue = Tuning(-id); + }else{ + rc = SQLITE_NOTFOUND; + } + break; + } +#endif + + /* sqlite3_test_control(SQLITE_TESTCTRL_JSON_SELFCHECK, &onOff); + ** + ** Activate or deactivate validation of JSONB that is generated from + ** text. Off by default, as the validation is slow. Validation is + ** only available if compiled using SQLITE_DEBUG. + ** + ** If onOff is initially 1, then turn it on. If onOff is initially + ** off, turn it off. If onOff is initially -1, then change onOff + ** to be the current setting. + */ + case SQLITE_TESTCTRL_JSON_SELFCHECK: { +#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_WSD) + int *pOnOff = va_arg(ap, int*); + if( *pOnOff<0 ){ + *pOnOff = sqlite3Config.bJsonSelfcheck; + }else{ + sqlite3Config.bJsonSelfcheck = (u8)((*pOnOff)&0xff); + } +#endif + break; + } } va_end(ap); -#endif /* SQLITE_OMIT_BUILTIN_TEST */ +#endif /* SQLITE_UNTESTABLE */ return rc; } +/* +** The Pager stores the Database filename, Journal filename, and WAL filename +** consecutively in memory, in that order. The database filename is prefixed +** by four zero bytes. Locate the start of the database filename by searching +** backwards for the first byte following four consecutive zero bytes. +** +** This only works if the filename passed in was obtained from the Pager. +*/ +static const char *databaseName(const char *zName){ + while( zName[-1]!=0 || zName[-2]!=0 || zName[-3]!=0 || zName[-4]!=0 ){ + zName--; + } + return zName; +} + +/* +** Append text z[] to the end of p[]. Return a pointer to the first +** character after then zero terminator on the new text in p[]. +*/ +static char *appendText(char *p, const char *z){ + size_t n = strlen(z); + memcpy(p, z, n+1); + return p+n+1; +} + +/* +** Allocate memory to hold names for a database, journal file, WAL file, +** and query parameters. The pointer returned is valid for use by +** sqlite3_filename_database() and sqlite3_uri_parameter() and related +** functions. +** +** Memory layout must be compatible with that generated by the pager +** and expected by sqlite3_uri_parameter() and databaseName(). +*/ +const char *sqlite3_create_filename( + const char *zDatabase, + const char *zJournal, + const char *zWal, + int nParam, + const char **azParam +){ + sqlite3_int64 nByte; + int i; + char *pResult, *p; + nByte = strlen(zDatabase) + strlen(zJournal) + strlen(zWal) + 10; + for(i=0; i0 ){ zFilename += sqlite3Strlen30(zFilename) + 1; - if( x==0 ) return zFilename; zFilename += sqlite3Strlen30(zFilename) + 1; } - return 0; + return zFilename[0] ? zFilename : 0; } /* @@ -3816,25 +4913,70 @@ sqlite3_int64 sqlite3_uri_int64( ){ const char *z = sqlite3_uri_parameter(zFilename, zParam); sqlite3_int64 v; - if( z && sqlite3DecOrHexToI64(z, &v)==SQLITE_OK ){ + if( z && sqlite3DecOrHexToI64(z, &v)==0 ){ bDflt = v; } return bDflt; } +/* +** Translate a filename that was handed to a VFS routine into the corresponding +** database, journal, or WAL file. +** +** It is an error to pass this routine a filename string that was not +** passed into the VFS from the SQLite core. Doing so is similar to +** passing free() a pointer that was not obtained from malloc() - it is +** an error that we cannot easily detect but that will likely cause memory +** corruption. +*/ +const char *sqlite3_filename_database(const char *zFilename){ + if( zFilename==0 ) return 0; + return databaseName(zFilename); +} +const char *sqlite3_filename_journal(const char *zFilename){ + if( zFilename==0 ) return 0; + zFilename = databaseName(zFilename); + zFilename += sqlite3Strlen30(zFilename) + 1; + while( ALWAYS(zFilename) && zFilename[0] ){ + zFilename += sqlite3Strlen30(zFilename) + 1; + zFilename += sqlite3Strlen30(zFilename) + 1; + } + return zFilename + 1; +} +const char *sqlite3_filename_wal(const char *zFilename){ +#ifdef SQLITE_OMIT_WAL + return 0; +#else + zFilename = sqlite3_filename_journal(zFilename); + if( zFilename ) zFilename += sqlite3Strlen30(zFilename) + 1; + return zFilename; +#endif +} + /* ** Return the Btree pointer identified by zDbName. Return NULL if not found. */ Btree *sqlite3DbNameToBtree(sqlite3 *db, const char *zDbName){ - int i; - for(i=0; inDb; i++){ - if( db->aDb[i].pBt - && (zDbName==0 || sqlite3StrICmp(zDbName, db->aDb[i].zName)==0) - ){ - return db->aDb[i].pBt; - } + int iDb = zDbName ? sqlite3FindDbName(db, zDbName) : 0; + return iDb<0 ? 0 : db->aDb[iDb].pBt; +} + +/* +** Return the name of the N-th database schema. Return NULL if N is out +** of range. +*/ +const char *sqlite3_db_name(sqlite3 *db, int N){ +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) ){ + (void)SQLITE_MISUSE_BKPT; + return 0; + } +#endif + if( N<0 || N>=db->nDb ){ + return 0; + }else{ + return db->aDb[N].zDbSName; } - return 0; } /* @@ -3871,17 +5013,16 @@ int sqlite3_db_readonly(sqlite3 *db, const char *zDbName){ #ifdef SQLITE_ENABLE_SNAPSHOT /* -** Obtain a snapshot handle for the snapshot of database zDb currently +** Obtain a snapshot handle for the snapshot of database zDb currently ** being read by handle db. */ int sqlite3_snapshot_get( - sqlite3 *db, + sqlite3 *db, const char *zDb, sqlite3_snapshot **ppSnapshot ){ int rc = SQLITE_ERROR; #ifndef SQLITE_OMIT_WAL - int iDb; #ifdef SQLITE_ENABLE_API_ARMOR if( !sqlite3SafetyCheckOk(db) ){ @@ -3890,13 +5031,19 @@ int sqlite3_snapshot_get( #endif sqlite3_mutex_enter(db->mutex); - iDb = sqlite3FindDbName(db, zDb); - if( iDb==0 || iDb>1 ){ - Btree *pBt = db->aDb[iDb].pBt; - if( 0==sqlite3BtreeIsInTrans(pBt) ){ - rc = sqlite3BtreeBeginTrans(pBt, 0); - if( rc==SQLITE_OK ){ - rc = sqlite3PagerSnapshotGet(sqlite3BtreePager(pBt), ppSnapshot); + if( db->autoCommit==0 ){ + int iDb = sqlite3FindDbName(db, zDb); + if( iDb==0 || iDb>1 ){ + Btree *pBt = db->aDb[iDb].pBt; + if( SQLITE_TXN_WRITE!=sqlite3BtreeTxnState(pBt) ){ + Pager *pPager = sqlite3BtreePager(pBt); + i64 dummy = 0; + sqlite3PagerSnapshotOpen(pPager, (sqlite3_snapshot*)&dummy); + rc = sqlite3BtreeBeginTrans(pBt, 0, 0); + sqlite3PagerSnapshotOpen(pPager, 0); + if( rc==SQLITE_OK ){ + rc = sqlite3PagerSnapshotGet(sqlite3BtreePager(pBt), ppSnapshot); + } } } } @@ -3907,11 +5054,11 @@ int sqlite3_snapshot_get( } /* -** Open a read-transaction on the snapshot idendified by pSnapshot. +** Open a read-transaction on the snapshot identified by pSnapshot. */ int sqlite3_snapshot_open( - sqlite3 *db, - const char *zDb, + sqlite3 *db, + const char *zDb, sqlite3_snapshot *pSnapshot ){ int rc = SQLITE_ERROR; @@ -3928,11 +5075,29 @@ int sqlite3_snapshot_open( iDb = sqlite3FindDbName(db, zDb); if( iDb==0 || iDb>1 ){ Btree *pBt = db->aDb[iDb].pBt; - if( 0==sqlite3BtreeIsInReadTrans(pBt) ){ - rc = sqlite3PagerSnapshotOpen(sqlite3BtreePager(pBt), pSnapshot); + if( sqlite3BtreeTxnState(pBt)!=SQLITE_TXN_WRITE ){ + Pager *pPager = sqlite3BtreePager(pBt); + int bUnlock = 0; + if( sqlite3BtreeTxnState(pBt)!=SQLITE_TXN_NONE ){ + if( db->nVdbeActive==0 ){ + rc = sqlite3PagerSnapshotCheck(pPager, pSnapshot); + if( rc==SQLITE_OK ){ + bUnlock = 1; + rc = sqlite3BtreeCommit(pBt); + } + } + }else{ + rc = SQLITE_OK; + } + if( rc==SQLITE_OK ){ + rc = sqlite3PagerSnapshotOpen(pPager, pSnapshot); + } if( rc==SQLITE_OK ){ - rc = sqlite3BtreeBeginTrans(pBt, 0); - sqlite3PagerSnapshotOpen(sqlite3BtreePager(pBt), 0); + rc = sqlite3BtreeBeginTrans(pBt, 0, 0); + sqlite3PagerSnapshotOpen(pPager, 0); + } + if( bUnlock ){ + sqlite3PagerSnapshotUnlock(pPager); } } } @@ -3943,6 +5108,38 @@ int sqlite3_snapshot_open( return rc; } +/* +** Recover as many snapshots as possible from the wal file associated with +** schema zDb of database db. +*/ +int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb){ + int rc = SQLITE_ERROR; +#ifndef SQLITE_OMIT_WAL + int iDb; + +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) ){ + return SQLITE_MISUSE_BKPT; + } +#endif + + sqlite3_mutex_enter(db->mutex); + iDb = sqlite3FindDbName(db, zDb); + if( iDb==0 || iDb>1 ){ + Btree *pBt = db->aDb[iDb].pBt; + if( SQLITE_TXN_NONE==sqlite3BtreeTxnState(pBt) ){ + rc = sqlite3BtreeBeginTrans(pBt, 0, 0); + if( rc==SQLITE_OK ){ + rc = sqlite3PagerSnapshotRecover(sqlite3BtreePager(pBt)); + sqlite3BtreeCommit(pBt); + } + } + } + sqlite3_mutex_leave(db->mutex); +#endif /* SQLITE_OMIT_WAL */ + return rc; +} + /* ** Free a snapshot handle obtained from sqlite3_snapshot_get(). */ @@ -3950,3 +5147,55 @@ void sqlite3_snapshot_free(sqlite3_snapshot *pSnapshot){ sqlite3_free(pSnapshot); } #endif /* SQLITE_ENABLE_SNAPSHOT */ + +#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS +/* +** Given the name of a compile-time option, return true if that option +** was used and false if not. +** +** The name can optionally begin with "SQLITE_" but the "SQLITE_" prefix +** is not required for a match. +*/ +int sqlite3_compileoption_used(const char *zOptName){ + int i, n; + int nOpt; + const char **azCompileOpt; + +#ifdef SQLITE_ENABLE_API_ARMOR + if( zOptName==0 ){ + (void)SQLITE_MISUSE_BKPT; + return 0; + } +#endif + + azCompileOpt = sqlite3CompileOptions(&nOpt); + + if( sqlite3StrNICmp(zOptName, "SQLITE_", 7)==0 ) zOptName += 7; + n = sqlite3Strlen30(zOptName); + + /* Since nOpt is normally in single digits, a linear search is + ** adequate. No need for a binary search. */ + for(i=0; i=0 && N0 && (n>mem0.hardLimit || n==0) ){ + n = mem0.hardLimit; + } mem0.alarmThreshold = n; nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); - mem0.nearlyFull = (n>0 && n<=nUsed); + AtomicStore(&mem0.nearlyFull, n>0 && n<=nUsed); sqlite3_mutex_leave(mem0.mutex); excess = sqlite3_memory_used() - n; if( excess>0 ) sqlite3_release_memory((int)(excess & 0x7fffffff)); @@ -122,6 +122,37 @@ void sqlite3_soft_heap_limit(int n){ sqlite3_soft_heap_limit64(n); } +/* +** Set the hard heap-size limit for the library. An argument of zero +** disables the hard heap limit. A negative argument is a no-op used +** to obtain the return value without affecting the hard heap limit. +** +** The return value is the value of the hard heap limit just prior to +** calling this interface. +** +** Setting the hard heap limit will also activate the soft heap limit +** and constrain the soft heap limit to be no more than the hard heap +** limit. +*/ +sqlite3_int64 sqlite3_hard_heap_limit64(sqlite3_int64 n){ + sqlite3_int64 priorLimit; +#ifndef SQLITE_OMIT_AUTOINIT + int rc = sqlite3_initialize(); + if( rc ) return -1; +#endif + sqlite3_mutex_enter(mem0.mutex); + priorLimit = mem0.hardLimit; + if( n>=0 ){ + mem0.hardLimit = n; + if( n=100 - && sqlite3GlobalConfig.nScratch>0 ){ - int i, n, sz; - ScratchFreeslot *pSlot; - sz = ROUNDDOWN8(sqlite3GlobalConfig.szScratch); - sqlite3GlobalConfig.szScratch = sz; - pSlot = (ScratchFreeslot*)sqlite3GlobalConfig.pScratch; - n = sqlite3GlobalConfig.nScratch; - mem0.pScratchFree = pSlot; - mem0.nScratchFree = n; - for(i=0; ipNext = (ScratchFreeslot*)(sz+(char*)pSlot); - pSlot = pSlot->pNext; - } - pSlot->pNext = 0; - mem0.pScratchEnd = (void*)&pSlot[1]; - }else{ - mem0.pScratchEnd = 0; - sqlite3GlobalConfig.pScratch = 0; - sqlite3GlobalConfig.szScratch = 0; - sqlite3GlobalConfig.nScratch = 0; - } if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512 || sqlite3GlobalConfig.nPage<=0 ){ sqlite3GlobalConfig.pPage = 0; @@ -161,6 +169,17 @@ int sqlite3MallocInit(void){ } rc = sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData); if( rc!=SQLITE_OK ) memset(&mem0, 0, sizeof(mem0)); +/* BEGIN SQLCIPHER */ +#ifdef SQLITE_HAS_CODEC + /* install wrapping functions for memory management + that will wipe all memory allocated by SQLite + when freed */ + if( rc==SQLITE_OK ) { + extern void sqlcipher_init_memmethods(void); + sqlcipher_init_memmethods(); + } +#endif +/* END SQLCIPHER */ return rc; } @@ -170,7 +189,7 @@ int sqlite3MallocInit(void){ ** sqlite3_soft_heap_limit(). */ int sqlite3HeapNearlyFull(void){ - return mem0.nearlyFull; + return AtomicLoad(&mem0.nearlyFull); } /* @@ -213,23 +232,57 @@ static void sqlite3MallocAlarm(int nByte){ sqlite3_mutex_enter(mem0.mutex); } +#ifdef SQLITE_DEBUG +/* +** This routine is called whenever an out-of-memory condition is seen, +** It's only purpose to to serve as a breakpoint for gdb or similar +** code debuggers when working on out-of-memory conditions, for example +** caused by PRAGMA hard_heap_limit=N. +*/ +static SQLITE_NOINLINE void test_oom_breakpoint(u64 n){ + static u64 nOomFault = 0; + nOomFault += n; + /* The assert() is never reached in a human lifetime. It is here mostly + ** to prevent code optimizers from optimizing out this function. */ + assert( (nOomFault>>32) < 0xffffffff ); +} +#else +# define test_oom_breakpoint(X) /* No-op for production builds */ +#endif + /* ** Do a memory allocation with statistics and alarms. Assume the ** lock is already held. */ -static int mallocWithAlarm(int n, void **pp){ - int nFull; +static void mallocWithAlarm(int n, void **pp){ void *p; + int nFull; assert( sqlite3_mutex_held(mem0.mutex) ); + assert( n>0 ); + + /* In Firefox (circa 2017-02-08), xRoundup() is remapped to an internal + ** implementation of malloc_good_size(), which must be called in debug + ** mode and specifically when the DMD "Dark Matter Detector" is enabled + ** or else a crash results. Hence, do not attempt to optimize out the + ** following xRoundup() call. */ nFull = sqlite3GlobalConfig.m.xRoundup(n); + sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, n); if( mem0.alarmThreshold>0 ){ sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); if( nUsed >= mem0.alarmThreshold - nFull ){ - mem0.nearlyFull = 1; + AtomicStore(&mem0.nearlyFull, 1); sqlite3MallocAlarm(nFull); + if( mem0.hardLimit ){ + nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); + if( nUsed >= mem0.hardLimit - nFull ){ + test_oom_breakpoint(1); + *pp = 0; + return; + } + } }else{ - mem0.nearlyFull = 0; + AtomicStore(&mem0.nearlyFull, 0); } } p = sqlite3GlobalConfig.m.xMalloc(nFull); @@ -245,21 +298,36 @@ static int mallocWithAlarm(int n, void **pp){ sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1); } *pp = p; - return nFull; } +/* +** Maximum size of any single memory allocation. +** +** This is not a limit on the total amount of memory used. This is +** a limit on the size parameter to sqlite3_malloc() and sqlite3_realloc(). +** +** The upper bound is slightly less than 2GiB: 0x7ffffeff == 2,147,483,391 +** This provides a 256-byte safety margin for defense against 32-bit +** signed integer overflow bugs when computing memory allocation sizes. +** Paranoid applications might want to reduce the maximum allocation size +** further for an even larger safety margin. 0x3fffffff or 0x0fffffff +** or even smaller would be reasonable upper bounds on the size of a memory +** allocations for most applications. +*/ +#ifndef SQLITE_MAX_ALLOCATION_SIZE +# define SQLITE_MAX_ALLOCATION_SIZE 2147483391 +#endif +#if SQLITE_MAX_ALLOCATION_SIZE>2147483391 +# error Maximum size for SQLITE_MAX_ALLOCATION_SIZE is 2147483391 +#endif + /* ** Allocate memory. This routine is like sqlite3_malloc() except that it ** assumes the memory subsystem has already been initialized. */ void *sqlite3Malloc(u64 n){ void *p; - if( n==0 || n>=0x7fffff00 ){ - /* A memory allocation of a number of bytes which is near the maximum - ** signed integer value might cause an integer overflow inside of the - ** xMalloc(). Hence we limit the maximum size to 0x7fffff00, giving - ** 255 bytes of overhead. SQLite itself will never use anything near - ** this amount. The only way to reach the limit is with sqlite3_malloc() */ + if( n==0 || n>SQLITE_MAX_ALLOCATION_SIZE ){ p = 0; }else if( sqlite3GlobalConfig.bMemstat ){ sqlite3_mutex_enter(mem0.mutex); @@ -290,111 +358,12 @@ void *sqlite3_malloc64(sqlite3_uint64 n){ return sqlite3Malloc(n); } -/* -** Each thread may only have a single outstanding allocation from -** xScratchMalloc(). We verify this constraint in the single-threaded -** case by setting scratchAllocOut to 1 when an allocation -** is outstanding clearing it when the allocation is freed. -*/ -#if SQLITE_THREADSAFE==0 && !defined(NDEBUG) -static int scratchAllocOut = 0; -#endif - - -/* -** Allocate memory that is to be used and released right away. -** This routine is similar to alloca() in that it is not intended -** for situations where the memory might be held long-term. This -** routine is intended to get memory to old large transient data -** structures that would not normally fit on the stack of an -** embedded processor. -*/ -void *sqlite3ScratchMalloc(int n){ - void *p; - assert( n>0 ); - - sqlite3_mutex_enter(mem0.mutex); - sqlite3StatusHighwater(SQLITE_STATUS_SCRATCH_SIZE, n); - if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){ - p = mem0.pScratchFree; - mem0.pScratchFree = mem0.pScratchFree->pNext; - mem0.nScratchFree--; - sqlite3StatusUp(SQLITE_STATUS_SCRATCH_USED, 1); - sqlite3_mutex_leave(mem0.mutex); - }else{ - sqlite3_mutex_leave(mem0.mutex); - p = sqlite3Malloc(n); - if( sqlite3GlobalConfig.bMemstat && p ){ - sqlite3_mutex_enter(mem0.mutex); - sqlite3StatusUp(SQLITE_STATUS_SCRATCH_OVERFLOW, sqlite3MallocSize(p)); - sqlite3_mutex_leave(mem0.mutex); - } - sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH); - } - assert( sqlite3_mutex_notheld(mem0.mutex) ); - - -#if SQLITE_THREADSAFE==0 && !defined(NDEBUG) - /* EVIDENCE-OF: R-12970-05880 SQLite will not use more than one scratch - ** buffers per thread. - ** - ** This can only be checked in single-threaded mode. - */ - assert( scratchAllocOut==0 ); - if( p ) scratchAllocOut++; -#endif - - return p; -} -void sqlite3ScratchFree(void *p){ - if( p ){ - -#if SQLITE_THREADSAFE==0 && !defined(NDEBUG) - /* Verify that no more than two scratch allocation per thread - ** is outstanding at one time. (This is only checked in the - ** single-threaded case since checking in the multi-threaded case - ** would be much more complicated.) */ - assert( scratchAllocOut>=1 && scratchAllocOut<=2 ); - scratchAllocOut--; -#endif - - if( SQLITE_WITHIN(p, sqlite3GlobalConfig.pScratch, mem0.pScratchEnd) ){ - /* Release memory from the SQLITE_CONFIG_SCRATCH allocation */ - ScratchFreeslot *pSlot; - pSlot = (ScratchFreeslot*)p; - sqlite3_mutex_enter(mem0.mutex); - pSlot->pNext = mem0.pScratchFree; - mem0.pScratchFree = pSlot; - mem0.nScratchFree++; - assert( mem0.nScratchFree <= (u32)sqlite3GlobalConfig.nScratch ); - sqlite3StatusDown(SQLITE_STATUS_SCRATCH_USED, 1); - sqlite3_mutex_leave(mem0.mutex); - }else{ - /* Release memory back to the heap */ - assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) ); - assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_SCRATCH) ); - sqlite3MemdebugSetType(p, MEMTYPE_HEAP); - if( sqlite3GlobalConfig.bMemstat ){ - int iSize = sqlite3MallocSize(p); - sqlite3_mutex_enter(mem0.mutex); - sqlite3StatusDown(SQLITE_STATUS_SCRATCH_OVERFLOW, iSize); - sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, iSize); - sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1); - sqlite3GlobalConfig.m.xFree(p); - sqlite3_mutex_leave(mem0.mutex); - }else{ - sqlite3GlobalConfig.m.xFree(p); - } - } - } -} - /* ** TRUE if p is a lookaside memory allocation from db */ #ifndef SQLITE_OMIT_LOOKASIDE -static int isLookaside(sqlite3 *db, void *p){ - return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pEnd); +static int isLookaside(sqlite3 *db, const void *p){ + return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pTrueEnd); } #else #define isLookaside(A,B) 0 @@ -404,27 +373,43 @@ static int isLookaside(sqlite3 *db, void *p){ ** Return the size of a memory allocation previously obtained from ** sqlite3Malloc() or sqlite3_malloc(). */ -int sqlite3MallocSize(void *p){ +int sqlite3MallocSize(const void *p){ assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); - return sqlite3GlobalConfig.m.xSize(p); + return sqlite3GlobalConfig.m.xSize((void*)p); +} +static int lookasideMallocSize(sqlite3 *db, const void *p){ +#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE + return plookaside.pMiddle ? db->lookaside.szTrue : LOOKASIDE_SMALL; +#else + return db->lookaside.szTrue; +#endif } -int sqlite3DbMallocSize(sqlite3 *db, void *p){ +int sqlite3DbMallocSize(sqlite3 *db, const void *p){ assert( p!=0 ); - if( db==0 || !isLookaside(db,p) ){ -#if SQLITE_DEBUG - if( db==0 ){ - assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) ); - assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); - }else{ - assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); - assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); - } +#ifdef SQLITE_DEBUG + if( db==0 ){ + assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) ); + assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); + }else if( !isLookaside(db,p) ){ + assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); + assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); + } #endif - return sqlite3GlobalConfig.m.xSize(p); - }else{ - assert( sqlite3_mutex_held(db->mutex) ); - return db->lookaside.sz; + if( db ){ + if( ((uptr)p)<(uptr)(db->lookaside.pTrueEnd) ){ +#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE + if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){ + assert( sqlite3_mutex_held(db->mutex) ); + return LOOKASIDE_SMALL; + } +#endif + if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){ + assert( sqlite3_mutex_held(db->mutex) ); + return db->lookaside.szTrue; + } + } } + return sqlite3GlobalConfig.m.xSize((void*)p); } sqlite3_uint64 sqlite3_msize(void *p){ assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) ); @@ -460,34 +445,89 @@ static SQLITE_NOINLINE void measureAllocationSize(sqlite3 *db, void *p){ /* ** Free memory that might be associated with a particular database -** connection. +** connection. Calling sqlite3DbFree(D,X) for X==0 is a harmless no-op. +** The sqlite3DbFreeNN(D,X) version requires that X be non-NULL. */ -void sqlite3DbFree(sqlite3 *db, void *p){ +void sqlite3DbFreeNN(sqlite3 *db, void *p){ assert( db==0 || sqlite3_mutex_held(db->mutex) ); - if( p==0 ) return; + assert( p!=0 ); if( db ){ + if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){ +#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE + if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){ + LookasideSlot *pBuf = (LookasideSlot*)p; + assert( db->pnBytesFreed==0 ); +#ifdef SQLITE_DEBUG + memset(p, 0xaa, LOOKASIDE_SMALL); /* Trash freed content */ +#endif + pBuf->pNext = db->lookaside.pSmallFree; + db->lookaside.pSmallFree = pBuf; + return; + } +#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */ + if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){ + LookasideSlot *pBuf = (LookasideSlot*)p; + assert( db->pnBytesFreed==0 ); +#ifdef SQLITE_DEBUG + memset(p, 0xaa, db->lookaside.szTrue); /* Trash freed content */ +#endif + pBuf->pNext = db->lookaside.pFree; + db->lookaside.pFree = pBuf; + return; + } + } if( db->pnBytesFreed ){ measureAllocationSize(db, p); return; } - if( isLookaside(db, p) ){ + } + assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); + assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); + assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) ); + sqlite3MemdebugSetType(p, MEMTYPE_HEAP); + sqlite3_free(p); +} +void sqlite3DbNNFreeNN(sqlite3 *db, void *p){ + assert( db!=0 ); + assert( sqlite3_mutex_held(db->mutex) ); + assert( p!=0 ); + if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){ +#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE + if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){ + LookasideSlot *pBuf = (LookasideSlot*)p; + assert( db->pnBytesFreed==0 ); +#ifdef SQLITE_DEBUG + memset(p, 0xaa, LOOKASIDE_SMALL); /* Trash freed content */ +#endif + pBuf->pNext = db->lookaside.pSmallFree; + db->lookaside.pSmallFree = pBuf; + return; + } +#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */ + if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){ LookasideSlot *pBuf = (LookasideSlot*)p; -#if SQLITE_DEBUG - /* Trash all content in the buffer being freed */ - memset(p, 0xaa, db->lookaside.sz); + assert( db->pnBytesFreed==0 ); +#ifdef SQLITE_DEBUG + memset(p, 0xaa, db->lookaside.szTrue); /* Trash freed content */ #endif pBuf->pNext = db->lookaside.pFree; db->lookaside.pFree = pBuf; - db->lookaside.nOut--; return; } } + if( db->pnBytesFreed ){ + measureAllocationSize(db, p); + return; + } assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); - assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) ); sqlite3MemdebugSetType(p, MEMTYPE_HEAP); sqlite3_free(p); } +void sqlite3DbFree(sqlite3 *db, void *p){ + assert( db==0 || sqlite3_mutex_held(db->mutex) ); + if( p ) sqlite3DbFreeNN(db, p); +} /* ** Change the size of an existing memory allocation @@ -516,18 +556,26 @@ void *sqlite3Realloc(void *pOld, u64 nBytes){ if( nOld==nNew ){ pNew = pOld; }else if( sqlite3GlobalConfig.bMemstat ){ + sqlite3_int64 nUsed; sqlite3_mutex_enter(mem0.mutex); sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes); nDiff = nNew - nOld; - if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >= + if( nDiff>0 && (nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)) >= mem0.alarmThreshold-nDiff ){ sqlite3MallocAlarm(nDiff); + if( mem0.hardLimit>0 && nUsed >= mem0.hardLimit - nDiff ){ + sqlite3_mutex_leave(mem0.mutex); + test_oom_breakpoint(1); + return 0; + } } pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT if( pNew==0 && mem0.alarmThreshold>0 ){ sqlite3MallocAlarm((int)nBytes); pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); } +#endif if( pNew ){ nNew = sqlite3MallocSize(pNew); sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld); @@ -631,23 +679,37 @@ void *sqlite3DbMallocRawNN(sqlite3 *db, u64 n){ assert( db!=0 ); assert( sqlite3_mutex_held(db->mutex) ); assert( db->pnBytesFreed==0 ); - if( db->lookaside.bDisable==0 ){ - assert( db->mallocFailed==0 ); - if( n>db->lookaside.sz ){ - db->lookaside.anStat[1]++; - }else if( (pBuf = db->lookaside.pFree)==0 ){ - db->lookaside.anStat[2]++; - }else{ - db->lookaside.pFree = pBuf->pNext; - db->lookaside.nOut++; + if( n>db->lookaside.sz ){ + if( !db->lookaside.bDisable ){ + db->lookaside.anStat[1]++; + }else if( db->mallocFailed ){ + return 0; + } + return dbMallocRawFinish(db, n); + } +#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE + if( n<=LOOKASIDE_SMALL ){ + if( (pBuf = db->lookaside.pSmallFree)!=0 ){ + db->lookaside.pSmallFree = pBuf->pNext; + db->lookaside.anStat[0]++; + return (void*)pBuf; + }else if( (pBuf = db->lookaside.pSmallInit)!=0 ){ + db->lookaside.pSmallInit = pBuf->pNext; db->lookaside.anStat[0]++; - if( db->lookaside.nOut>db->lookaside.mxOut ){ - db->lookaside.mxOut = db->lookaside.nOut; - } return (void*)pBuf; } - }else if( db->mallocFailed ){ - return 0; + } +#endif + if( (pBuf = db->lookaside.pFree)!=0 ){ + db->lookaside.pFree = pBuf->pNext; + db->lookaside.anStat[0]++; + return (void*)pBuf; + }else if( (pBuf = db->lookaside.pInit)!=0 ){ + db->lookaside.pInit = pBuf->pNext; + db->lookaside.anStat[0]++; + return (void*)pBuf; + }else{ + db->lookaside.anStat[2]++; } #else assert( db!=0 ); @@ -671,7 +733,16 @@ void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){ assert( db!=0 ); if( p==0 ) return sqlite3DbMallocRawNN(db, n); assert( sqlite3_mutex_held(db->mutex) ); - if( isLookaside(db,p) && n<=db->lookaside.sz ) return p; + if( ((uptr)p)<(uptr)db->lookaside.pEnd ){ +#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE + if( ((uptr)p)>=(uptr)db->lookaside.pMiddle ){ + if( n<=LOOKASIDE_SMALL ) return p; + }else +#endif + if( ((uptr)p)>=(uptr)db->lookaside.pStart ){ + if( n<=db->lookaside.szTrue ) return p; + } + } return dbReallocFinish(db, p, n); } static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n){ @@ -682,14 +753,14 @@ static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n){ if( isLookaside(db, p) ){ pNew = sqlite3DbMallocRawNN(db, n); if( pNew ){ - memcpy(pNew, p, db->lookaside.sz); + memcpy(pNew, p, lookasideMallocSize(db, p)); sqlite3DbFree(db, p); } }else{ assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); sqlite3MemdebugSetType(p, MEMTYPE_HEAP); - pNew = sqlite3_realloc64(p, n); + pNew = sqlite3Realloc(p, n); if( !pNew ){ sqlite3OomFault(db); } @@ -726,9 +797,8 @@ char *sqlite3DbStrDup(sqlite3 *db, const char *z){ if( z==0 ){ return 0; } - n = sqlite3Strlen30(z) + 1; - assert( (n&0x7fffffff)==n ); - zNew = sqlite3DbMallocRaw(db, (int)n); + n = strlen(z) + 1; + zNew = sqlite3DbMallocRaw(db, n); if( zNew ){ memcpy(zNew, z, n); } @@ -737,11 +807,9 @@ char *sqlite3DbStrDup(sqlite3 *db, const char *z){ char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){ char *zNew; assert( db!=0 ); - if( z==0 ){ - return 0; - } + assert( z!=0 || n==0 ); assert( (n&0x7fffffff)==n ); - zNew = sqlite3DbMallocRawNN(db, n+1); + zNew = z ? sqlite3DbMallocRawNN(db, n+1) : 0; if( zNew ){ memcpy(zNew, z, (size_t)n); zNew[n] = 0; @@ -749,12 +817,31 @@ char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){ return zNew; } +/* +** The text between zStart and zEnd represents a phrase within a larger +** SQL statement. Make a copy of this phrase in space obtained form +** sqlite3DbMalloc(). Omit leading and trailing whitespace. +*/ +char *sqlite3DbSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){ + int n; +#ifdef SQLITE_DEBUG + /* Because of the way the parser works, the span is guaranteed to contain + ** at least one non-space character */ + for(n=0; sqlite3Isspace(zStart[n]); n++){ assert( &zStart[n]mallocFailed, and also ** temporarily disable the lookaside memory allocator and interrupt ** any running VDBEs. +** +** Always return a NULL pointer so that this routine can be invoked using +** +** return sqlite3OomFault(db); +** +** and thereby avoid unnecessary stack frame allocations for the overwhelmingly +** common case where no OOM occurs. */ -void sqlite3OomFault(sqlite3 *db){ +void *sqlite3OomFault(sqlite3 *db){ if( db->mallocFailed==0 && db->bBenignMalloc==0 ){ db->mallocFailed = 1; if( db->nVdbeExec>0 ){ - db->u1.isInterrupted = 1; + AtomicStore(&db->u1.isInterrupted, 1); + } + DisableLookaside; + if( db->pParse ){ + Parse *pParse; + sqlite3ErrorMsg(db->pParse, "out of memory"); + db->pParse->rc = SQLITE_NOMEM_BKPT; + for(pParse=db->pParse->pOuterParse; pParse; pParse = pParse->pOuterParse){ + pParse->nErr++; + pParse->rc = SQLITE_NOMEM; + } } - db->lookaside.bDisable++; } + return 0; } /* @@ -783,19 +887,22 @@ void sqlite3OomFault(sqlite3 *db){ void sqlite3OomClear(sqlite3 *db){ if( db->mallocFailed && db->nVdbeExec==0 ){ db->mallocFailed = 0; - db->u1.isInterrupted = 0; + AtomicStore(&db->u1.isInterrupted, 0); assert( db->lookaside.bDisable>0 ); - db->lookaside.bDisable--; + EnableLookaside; } } /* -** Take actions at the end of an API call to indicate an OOM error +** Take actions at the end of an API call to deal with error codes. */ -static SQLITE_NOINLINE int apiOomError(sqlite3 *db){ - sqlite3OomClear(db); - sqlite3Error(db, SQLITE_NOMEM); - return SQLITE_NOMEM; +static SQLITE_NOINLINE int apiHandleError(sqlite3 *db, int rc){ + if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){ + sqlite3OomClear(db); + sqlite3Error(db, SQLITE_NOMEM); + return SQLITE_NOMEM_BKPT; + } + return rc & db->errMask; } /* @@ -817,8 +924,8 @@ int sqlite3ApiExit(sqlite3* db, int rc){ */ assert( db!=0 ); assert( sqlite3_mutex_held(db->mutex) ); - if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){ - return apiOomError(db); + if( db->mallocFailed || rc ){ + return apiHandleError(db, rc); } - return rc & db->errMask; + return 0; } diff --git a/src/mem1.c b/src/mem1.c index b960ccfd47..12f96beaec 100644 --- a/src/mem1.c +++ b/src/mem1.c @@ -57,7 +57,9 @@ */ #include #include +#ifdef SQLITE_MIGHT_BE_SINGLE_CORE #include +#endif /* SQLITE_MIGHT_BE_SINGLE_CORE */ static malloc_zone_t* _sqliteZone_; #define SQLITE_MALLOC(x) malloc_zone_malloc(_sqliteZone_, (x)) #define SQLITE_FREE(x) malloc_zone_free(_sqliteZone_, (x)); @@ -125,7 +127,9 @@ static malloc_zone_t* _sqliteZone_; */ static void *sqlite3MemMalloc(int nByte){ #ifdef SQLITE_MALLOCSIZE - void *p = SQLITE_MALLOC( nByte ); + void *p; + testcase( ROUND8(nByte)==nByte ); + p = SQLITE_MALLOC( nByte ); if( p==0 ){ testcase( sqlite3GlobalConfig.xLog!=0 ); sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte); @@ -134,7 +138,7 @@ static void *sqlite3MemMalloc(int nByte){ #else sqlite3_int64 *p; assert( nByte>0 ); - nByte = ROUND8(nByte); + testcase( ROUND8(nByte)!=nByte ); p = SQLITE_MALLOC( nByte+8 ); if( p ){ p[0] = nByte; @@ -152,7 +156,7 @@ static void *sqlite3MemMalloc(int nByte){ ** or sqlite3MemRealloc(). ** ** For this low-level routine, we already know that pPrior!=0 since -** cases where pPrior==0 will have been intecepted and dealt with +** cases where pPrior==0 will have been intercepted and dealt with ** by higher-level routines. */ static void sqlite3MemFree(void *pPrior){ @@ -240,27 +244,18 @@ static int sqlite3MemInit(void *NotUsed){ return SQLITE_OK; } len = sizeof(cpuCount); - /* One usually wants to use hw.acctivecpu for MT decisions, but not here */ + /* One usually wants to use hw.activecpu for MT decisions, but not here */ sysctlbyname("hw.ncpu", &cpuCount, &len, NULL, 0); if( cpuCount>1 ){ /* defer MT decisions to system malloc */ _sqliteZone_ = malloc_default_zone(); }else{ - /* only 1 core, use our own zone to contention over global locks, + /* only 1 core, use our own zone to contention over global locks, ** e.g. we have our own dedicated locks */ - bool success; - malloc_zone_t* newzone = malloc_create_zone(4096, 0); - malloc_set_zone_name(newzone, "Sqlite_Heap"); - do{ - success = OSAtomicCompareAndSwapPtrBarrier(NULL, newzone, - (void * volatile *)&_sqliteZone_); - }while(!_sqliteZone_); - if( !success ){ - /* somebody registered a zone first */ - malloc_destroy_zone(newzone); - } + _sqliteZone_ = malloc_create_zone(4096, 0); + malloc_set_zone_name(_sqliteZone_, "Sqlite_Heap"); } -#endif +#endif /* defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC) */ UNUSED_PARAMETER(NotUsed); return SQLITE_OK; } diff --git a/src/mem2.c b/src/mem2.c index 51ea297c6a..04d6298dba 100644 --- a/src/mem2.c +++ b/src/mem2.c @@ -149,7 +149,7 @@ static void adjustStats(int iSize, int increment){ ** This routine checks the guards at either end of the allocation and ** if they are incorrect it asserts. */ -static struct MemBlockHdr *sqlite3MemsysGetHeader(void *pAllocation){ +static struct MemBlockHdr *sqlite3MemsysGetHeader(const void *pAllocation){ struct MemBlockHdr *p; int *pInt; u8 *pU8; @@ -379,7 +379,7 @@ void sqlite3MemSetDefault(void){ ** Set the "type" of an allocation. */ void sqlite3MemdebugSetType(void *p, u8 eType){ - if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){ + if( p && sqlite3GlobalConfig.m.xFree==sqlite3MemFree ){ struct MemBlockHdr *pHdr; pHdr = sqlite3MemsysGetHeader(p); assert( pHdr->iForeGuard==FOREGUARD ); @@ -396,9 +396,9 @@ void sqlite3MemdebugSetType(void *p, u8 eType){ ** ** assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); */ -int sqlite3MemdebugHasType(void *p, u8 eType){ +int sqlite3MemdebugHasType(const void *p, u8 eType){ int rc = 1; - if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){ + if( p && sqlite3GlobalConfig.m.xFree==sqlite3MemFree ){ struct MemBlockHdr *pHdr; pHdr = sqlite3MemsysGetHeader(p); assert( pHdr->iForeGuard==FOREGUARD ); /* Allocation is valid */ @@ -418,9 +418,9 @@ int sqlite3MemdebugHasType(void *p, u8 eType){ ** ** assert( sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) ); */ -int sqlite3MemdebugNoType(void *p, u8 eType){ +int sqlite3MemdebugNoType(const void *p, u8 eType){ int rc = 1; - if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){ + if( p && sqlite3GlobalConfig.m.xFree==sqlite3MemFree ){ struct MemBlockHdr *pHdr; pHdr = sqlite3MemsysGetHeader(p); assert( pHdr->iForeGuard==FOREGUARD ); /* Allocation is valid */ diff --git a/src/mem3.c b/src/mem3.c index 2de028daa9..16463d6a5c 100644 --- a/src/mem3.c +++ b/src/mem3.c @@ -118,16 +118,16 @@ static SQLITE_WSD struct Mem3Global { /* ** The minimum amount of free space that we have seen. */ - u32 mnMaster; + u32 mnKeyBlk; /* - ** iMaster is the index of the master chunk. Most new allocations - ** occur off of this chunk. szMaster is the size (in Mem3Blocks) - ** of the current master. iMaster is 0 if there is not master chunk. - ** The master chunk is not in either the aiHash[] or aiSmall[]. + ** iKeyBlk is the index of the key chunk. Most new allocations + ** occur off of this chunk. szKeyBlk is the size (in Mem3Blocks) + ** of the current key chunk. iKeyBlk is 0 if there is no key chunk. + ** The key chunk is not in either the aiHash[] or aiSmall[]. */ - u32 iMaster; - u32 szMaster; + u32 iKeyBlk; + u32 szKeyBlk; /* ** Array of lists of free blocks according to the block size @@ -263,34 +263,34 @@ static void *memsys3Checkout(u32 i, u32 nBlock){ } /* -** Carve a piece off of the end of the mem3.iMaster free chunk. -** Return a pointer to the new allocation. Or, if the master chunk +** Carve a piece off of the end of the mem3.iKeyBlk free chunk. +** Return a pointer to the new allocation. Or, if the key chunk ** is not large enough, return 0. */ -static void *memsys3FromMaster(u32 nBlock){ +static void *memsys3FromKeyBlk(u32 nBlock){ assert( sqlite3_mutex_held(mem3.mutex) ); - assert( mem3.szMaster>=nBlock ); - if( nBlock>=mem3.szMaster-1 ){ - /* Use the entire master */ - void *p = memsys3Checkout(mem3.iMaster, mem3.szMaster); - mem3.iMaster = 0; - mem3.szMaster = 0; - mem3.mnMaster = 0; + assert( mem3.szKeyBlk>=nBlock ); + if( nBlock>=mem3.szKeyBlk-1 ){ + /* Use the entire key chunk */ + void *p = memsys3Checkout(mem3.iKeyBlk, mem3.szKeyBlk); + mem3.iKeyBlk = 0; + mem3.szKeyBlk = 0; + mem3.mnKeyBlk = 0; return p; }else{ - /* Split the master block. Return the tail. */ + /* Split the key block. Return the tail. */ u32 newi, x; - newi = mem3.iMaster + mem3.szMaster - nBlock; - assert( newi > mem3.iMaster+1 ); - mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = nBlock; - mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x |= 2; + newi = mem3.iKeyBlk + mem3.szKeyBlk - nBlock; + assert( newi > mem3.iKeyBlk+1 ); + mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.prevSize = nBlock; + mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.size4x |= 2; mem3.aPool[newi-1].u.hdr.size4x = nBlock*4 + 1; - mem3.szMaster -= nBlock; - mem3.aPool[newi-1].u.hdr.prevSize = mem3.szMaster; - x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2; - mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x; - if( mem3.szMaster < mem3.mnMaster ){ - mem3.mnMaster = mem3.szMaster; + mem3.szKeyBlk -= nBlock; + mem3.aPool[newi-1].u.hdr.prevSize = mem3.szKeyBlk; + x = mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x & 2; + mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x = mem3.szKeyBlk*4 | x; + if( mem3.szKeyBlk < mem3.mnKeyBlk ){ + mem3.mnKeyBlk = mem3.szKeyBlk; } return (void*)&mem3.aPool[newi]; } @@ -304,13 +304,13 @@ static void *memsys3FromMaster(u32 nBlock){ ** This routine examines all entries on the given list and tries ** to coalesce each entries with adjacent free chunks. ** -** If it sees a chunk that is larger than mem3.iMaster, it replaces -** the current mem3.iMaster with the new larger chunk. In order for -** this mem3.iMaster replacement to work, the master chunk must be +** If it sees a chunk that is larger than mem3.iKeyBlk, it replaces +** the current mem3.iKeyBlk with the new larger chunk. In order for +** this mem3.iKeyBlk replacement to work, the key chunk must be ** linked into the hash tables. That is not the normal state of -** affairs, of course. The calling routine must link the master +** affairs, of course. The calling routine must link the key ** chunk before invoking this routine, then must unlink the (possibly -** changed) master chunk once this routine has finished. +** changed) key chunk once this routine has finished. */ static void memsys3Merge(u32 *pRoot){ u32 iNext, prev, size, i, x; @@ -337,9 +337,9 @@ static void memsys3Merge(u32 *pRoot){ }else{ size /= 4; } - if( size>mem3.szMaster ){ - mem3.iMaster = i; - mem3.szMaster = size; + if( size>mem3.szKeyBlk ){ + mem3.iKeyBlk = i; + mem3.szKeyBlk = size; } } } @@ -388,26 +388,26 @@ static void *memsys3MallocUnsafe(int nByte){ /* STEP 2: ** Try to satisfy the allocation by carving a piece off of the end - ** of the master chunk. This step usually works if step 1 fails. + ** of the key chunk. This step usually works if step 1 fails. */ - if( mem3.szMaster>=nBlock ){ - return memsys3FromMaster(nBlock); + if( mem3.szKeyBlk>=nBlock ){ + return memsys3FromKeyBlk(nBlock); } /* STEP 3: ** Loop through the entire memory pool. Coalesce adjacent free - ** chunks. Recompute the master chunk as the largest free chunk. + ** chunks. Recompute the key chunk as the largest free chunk. ** Then try again to satisfy the allocation by carving a piece off - ** of the end of the master chunk. This step happens very + ** of the end of the key chunk. This step happens very ** rarely (we hope!) */ for(toFree=nBlock*16; toFree<(mem3.nPool*16); toFree *= 2){ memsys3OutOfMemory(toFree); - if( mem3.iMaster ){ - memsys3Link(mem3.iMaster); - mem3.iMaster = 0; - mem3.szMaster = 0; + if( mem3.iKeyBlk ){ + memsys3Link(mem3.iKeyBlk); + mem3.iKeyBlk = 0; + mem3.szKeyBlk = 0; } for(i=0; i=nBlock ){ - return memsys3FromMaster(nBlock); + if( mem3.szKeyBlk ){ + memsys3Unlink(mem3.iKeyBlk); + if( mem3.szKeyBlk>=nBlock ){ + return memsys3FromKeyBlk(nBlock); } } } @@ -448,23 +448,23 @@ static void memsys3FreeUnsafe(void *pOld){ mem3.aPool[i+size-1].u.hdr.size4x &= ~2; memsys3Link(i); - /* Try to expand the master using the newly freed chunk */ - if( mem3.iMaster ){ - while( (mem3.aPool[mem3.iMaster-1].u.hdr.size4x&2)==0 ){ - size = mem3.aPool[mem3.iMaster-1].u.hdr.prevSize; - mem3.iMaster -= size; - mem3.szMaster += size; - memsys3Unlink(mem3.iMaster); - x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2; - mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x; - mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster; + /* Try to expand the key using the newly freed chunk */ + if( mem3.iKeyBlk ){ + while( (mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x&2)==0 ){ + size = mem3.aPool[mem3.iKeyBlk-1].u.hdr.prevSize; + mem3.iKeyBlk -= size; + mem3.szKeyBlk += size; + memsys3Unlink(mem3.iKeyBlk); + x = mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x & 2; + mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x = mem3.szKeyBlk*4 | x; + mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.prevSize = mem3.szKeyBlk; } - x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2; - while( (mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x&1)==0 ){ - memsys3Unlink(mem3.iMaster+mem3.szMaster); - mem3.szMaster += mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x/4; - mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x; - mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster; + x = mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x & 2; + while( (mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.size4x&1)==0 ){ + memsys3Unlink(mem3.iKeyBlk+mem3.szKeyBlk); + mem3.szKeyBlk += mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.size4x/4; + mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x = mem3.szKeyBlk*4 | x; + mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.prevSize = mem3.szKeyBlk; } } } @@ -560,11 +560,11 @@ static int memsys3Init(void *NotUsed){ mem3.aPool = (Mem3Block *)sqlite3GlobalConfig.pHeap; mem3.nPool = (sqlite3GlobalConfig.nHeap / sizeof(Mem3Block)) - 2; - /* Initialize the master block. */ - mem3.szMaster = mem3.nPool; - mem3.mnMaster = mem3.szMaster; - mem3.iMaster = 1; - mem3.aPool[0].u.hdr.size4x = (mem3.szMaster<<2) + 2; + /* Initialize the key block. */ + mem3.szKeyBlk = mem3.nPool; + mem3.mnKeyBlk = mem3.szKeyBlk; + mem3.iKeyBlk = 1; + mem3.aPool[0].u.hdr.size4x = (mem3.szKeyBlk<<2) + 2; mem3.aPool[mem3.nPool].u.hdr.prevSize = mem3.nPool; mem3.aPool[mem3.nPool].u.hdr.size4x = 1; @@ -624,7 +624,7 @@ void sqlite3Memsys3Dump(const char *zFilename){ fprintf(out, "%p %6d bytes checked out\n", &mem3.aPool[i], (size/4)*8-8); }else{ fprintf(out, "%p %6d bytes free%s\n", &mem3.aPool[i], (size/4)*8-8, - i==mem3.iMaster ? " **master**" : ""); + i==mem3.iKeyBlk ? " **key**" : ""); } } for(i=0; i 0x40000000 ) return 0; - for(iFullSz=mem5.szAtom; iFullSz0x10000000 ){ + if( n>0x40000000 ) return 0; + if( n>0x20000000 ) return 0x40000000; + return 0x20000000; + } + for(iFullSz=mem5.szAtom*8; iFullSz=(i64)n ) return iFullSz/2; return iFullSz; } diff --git a/src/memdb.c b/src/memdb.c new file mode 100644 index 0000000000..61a378bb3d --- /dev/null +++ b/src/memdb.c @@ -0,0 +1,936 @@ +/* +** 2016-09-07 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +** This file implements an in-memory VFS. A database is held as a contiguous +** block of memory. +** +** This file also implements interface sqlite3_serialize() and +** sqlite3_deserialize(). +*/ +#include "sqliteInt.h" +#ifndef SQLITE_OMIT_DESERIALIZE + +/* +** Forward declaration of objects used by this utility +*/ +typedef struct sqlite3_vfs MemVfs; +typedef struct MemFile MemFile; +typedef struct MemStore MemStore; + +/* Access to a lower-level VFS that (might) implement dynamic loading, +** access to randomness, etc. +*/ +#define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData)) + +/* Storage for a memdb file. +** +** An memdb object can be shared or separate. Shared memdb objects can be +** used by more than one database connection. Mutexes are used by shared +** memdb objects to coordinate access. Separate memdb objects are only +** connected to a single database connection and do not require additional +** mutexes. +** +** Shared memdb objects have .zFName!=0 and .pMutex!=0. They are created +** using "file:/name?vfs=memdb". The first character of the name must be +** "/" or else the object will be a separate memdb object. All shared +** memdb objects are stored in memdb_g.apMemStore[] in an arbitrary order. +** +** Separate memdb objects are created using a name that does not begin +** with "/" or using sqlite3_deserialize(). +** +** Access rules for shared MemStore objects: +** +** * .zFName is initialized when the object is created and afterwards +** is unchanged until the object is destroyed. So it can be accessed +** at any time as long as we know the object is not being destroyed, +** which means while either the SQLITE_MUTEX_STATIC_VFS1 or +** .pMutex is held or the object is not part of memdb_g.apMemStore[]. +** +** * Can .pMutex can only be changed while holding the +** SQLITE_MUTEX_STATIC_VFS1 mutex or while the object is not part +** of memdb_g.apMemStore[]. +** +** * Other fields can only be changed while holding the .pMutex mutex +** or when the .nRef is less than zero and the object is not part of +** memdb_g.apMemStore[]. +** +** * The .aData pointer has the added requirement that it can can only +** be changed (for resizing) when nMmap is zero. +** +*/ +struct MemStore { + sqlite3_int64 sz; /* Size of the file */ + sqlite3_int64 szAlloc; /* Space allocated to aData */ + sqlite3_int64 szMax; /* Maximum allowed size of the file */ + unsigned char *aData; /* content of the file */ + sqlite3_mutex *pMutex; /* Used by shared stores only */ + int nMmap; /* Number of memory mapped pages */ + unsigned mFlags; /* Flags */ + int nRdLock; /* Number of readers */ + int nWrLock; /* Number of writers. (Always 0 or 1) */ + int nRef; /* Number of users of this MemStore */ + char *zFName; /* The filename for shared stores */ +}; + +/* An open file */ +struct MemFile { + sqlite3_file base; /* IO methods */ + MemStore *pStore; /* The storage */ + int eLock; /* Most recent lock against this file */ +}; + +/* +** File-scope variables for holding the memdb files that are accessible +** to multiple database connections in separate threads. +** +** Must hold SQLITE_MUTEX_STATIC_VFS1 to access any part of this object. +*/ +static struct MemFS { + int nMemStore; /* Number of shared MemStore objects */ + MemStore **apMemStore; /* Array of all shared MemStore objects */ +} memdb_g; + +/* +** Methods for MemFile +*/ +static int memdbClose(sqlite3_file*); +static int memdbRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst); +static int memdbWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst); +static int memdbTruncate(sqlite3_file*, sqlite3_int64 size); +static int memdbSync(sqlite3_file*, int flags); +static int memdbFileSize(sqlite3_file*, sqlite3_int64 *pSize); +static int memdbLock(sqlite3_file*, int); +static int memdbUnlock(sqlite3_file*, int); +/* static int memdbCheckReservedLock(sqlite3_file*, int *pResOut);// not used */ +static int memdbFileControl(sqlite3_file*, int op, void *pArg); +/* static int memdbSectorSize(sqlite3_file*); // not used */ +static int memdbDeviceCharacteristics(sqlite3_file*); +static int memdbFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp); +static int memdbUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p); + +/* +** Methods for MemVfs +*/ +static int memdbOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *); +/* static int memdbDelete(sqlite3_vfs*, const char *zName, int syncDir); */ +static int memdbAccess(sqlite3_vfs*, const char *zName, int flags, int *); +static int memdbFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut); +static void *memdbDlOpen(sqlite3_vfs*, const char *zFilename); +static void memdbDlError(sqlite3_vfs*, int nByte, char *zErrMsg); +static void (*memdbDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void); +static void memdbDlClose(sqlite3_vfs*, void*); +static int memdbRandomness(sqlite3_vfs*, int nByte, char *zOut); +static int memdbSleep(sqlite3_vfs*, int microseconds); +/* static int memdbCurrentTime(sqlite3_vfs*, double*); */ +static int memdbGetLastError(sqlite3_vfs*, int, char *); +static int memdbCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*); + +static sqlite3_vfs memdb_vfs = { + 2, /* iVersion */ + 0, /* szOsFile (set when registered) */ + 1024, /* mxPathname */ + 0, /* pNext */ + "memdb", /* zName */ + 0, /* pAppData (set when registered) */ + memdbOpen, /* xOpen */ + 0, /* memdbDelete, */ /* xDelete */ + memdbAccess, /* xAccess */ + memdbFullPathname, /* xFullPathname */ + memdbDlOpen, /* xDlOpen */ + memdbDlError, /* xDlError */ + memdbDlSym, /* xDlSym */ + memdbDlClose, /* xDlClose */ + memdbRandomness, /* xRandomness */ + memdbSleep, /* xSleep */ + 0, /* memdbCurrentTime, */ /* xCurrentTime */ + memdbGetLastError, /* xGetLastError */ + memdbCurrentTimeInt64, /* xCurrentTimeInt64 */ + 0, /* xSetSystemCall */ + 0, /* xGetSystemCall */ + 0, /* xNextSystemCall */ +}; + +static const sqlite3_io_methods memdb_io_methods = { + 3, /* iVersion */ + memdbClose, /* xClose */ + memdbRead, /* xRead */ + memdbWrite, /* xWrite */ + memdbTruncate, /* xTruncate */ + memdbSync, /* xSync */ + memdbFileSize, /* xFileSize */ + memdbLock, /* xLock */ + memdbUnlock, /* xUnlock */ + 0, /* memdbCheckReservedLock, */ /* xCheckReservedLock */ + memdbFileControl, /* xFileControl */ + 0, /* memdbSectorSize,*/ /* xSectorSize */ + memdbDeviceCharacteristics, /* xDeviceCharacteristics */ + 0, /* xShmMap */ + 0, /* xShmLock */ + 0, /* xShmBarrier */ + 0, /* xShmUnmap */ + memdbFetch, /* xFetch */ + memdbUnfetch /* xUnfetch */ +}; + +/* +** Enter/leave the mutex on a MemStore +*/ +#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE==0 +static void memdbEnter(MemStore *p){ + UNUSED_PARAMETER(p); +} +static void memdbLeave(MemStore *p){ + UNUSED_PARAMETER(p); +} +#else +static void memdbEnter(MemStore *p){ + sqlite3_mutex_enter(p->pMutex); +} +static void memdbLeave(MemStore *p){ + sqlite3_mutex_leave(p->pMutex); +} +#endif + + + +/* +** Close an memdb-file. +** Free the underlying MemStore object when its refcount drops to zero +** or less. +*/ +static int memdbClose(sqlite3_file *pFile){ + MemStore *p = ((MemFile*)pFile)->pStore; + if( p->zFName ){ + int i; +#ifndef SQLITE_MUTEX_OMIT + sqlite3_mutex *pVfsMutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1); +#endif + sqlite3_mutex_enter(pVfsMutex); + for(i=0; ALWAYS(inRef==1 ){ + memdb_g.apMemStore[i] = memdb_g.apMemStore[--memdb_g.nMemStore]; + if( memdb_g.nMemStore==0 ){ + sqlite3_free(memdb_g.apMemStore); + memdb_g.apMemStore = 0; + } + } + break; + } + } + sqlite3_mutex_leave(pVfsMutex); + }else{ + memdbEnter(p); + } + p->nRef--; + if( p->nRef<=0 ){ + if( p->mFlags & SQLITE_DESERIALIZE_FREEONCLOSE ){ + sqlite3_free(p->aData); + } + memdbLeave(p); + sqlite3_mutex_free(p->pMutex); + sqlite3_free(p); + }else{ + memdbLeave(p); + } + return SQLITE_OK; +} + +/* +** Read data from an memdb-file. +*/ +static int memdbRead( + sqlite3_file *pFile, + void *zBuf, + int iAmt, + sqlite_int64 iOfst +){ + MemStore *p = ((MemFile*)pFile)->pStore; + memdbEnter(p); + if( iOfst+iAmt>p->sz ){ + memset(zBuf, 0, iAmt); + if( iOfstsz ) memcpy(zBuf, p->aData+iOfst, p->sz - iOfst); + memdbLeave(p); + return SQLITE_IOERR_SHORT_READ; + } + memcpy(zBuf, p->aData+iOfst, iAmt); + memdbLeave(p); + return SQLITE_OK; +} + +/* +** Try to enlarge the memory allocation to hold at least sz bytes +*/ +static int memdbEnlarge(MemStore *p, sqlite3_int64 newSz){ + unsigned char *pNew; + if( (p->mFlags & SQLITE_DESERIALIZE_RESIZEABLE)==0 || NEVER(p->nMmap>0) ){ + return SQLITE_FULL; + } + if( newSz>p->szMax ){ + return SQLITE_FULL; + } + newSz *= 2; + if( newSz>p->szMax ) newSz = p->szMax; + pNew = sqlite3Realloc(p->aData, newSz); + if( pNew==0 ) return SQLITE_IOERR_NOMEM; + p->aData = pNew; + p->szAlloc = newSz; + return SQLITE_OK; +} + +/* +** Write data to an memdb-file. +*/ +static int memdbWrite( + sqlite3_file *pFile, + const void *z, + int iAmt, + sqlite_int64 iOfst +){ + MemStore *p = ((MemFile*)pFile)->pStore; + memdbEnter(p); + if( NEVER(p->mFlags & SQLITE_DESERIALIZE_READONLY) ){ + /* Can't happen: memdbLock() will return SQLITE_READONLY before + ** reaching this point */ + memdbLeave(p); + return SQLITE_IOERR_WRITE; + } + if( iOfst+iAmt>p->sz ){ + int rc; + if( iOfst+iAmt>p->szAlloc + && (rc = memdbEnlarge(p, iOfst+iAmt))!=SQLITE_OK + ){ + memdbLeave(p); + return rc; + } + if( iOfst>p->sz ) memset(p->aData+p->sz, 0, iOfst-p->sz); + p->sz = iOfst+iAmt; + } + memcpy(p->aData+iOfst, z, iAmt); + memdbLeave(p); + return SQLITE_OK; +} + +/* +** Truncate an memdb-file. +** +** In rollback mode (which is always the case for memdb, as it does not +** support WAL mode) the truncate() method is only used to reduce +** the size of a file, never to increase the size. +*/ +static int memdbTruncate(sqlite3_file *pFile, sqlite_int64 size){ + MemStore *p = ((MemFile*)pFile)->pStore; + int rc = SQLITE_OK; + memdbEnter(p); + if( size>p->sz ){ + /* This can only happen with a corrupt wal mode db */ + rc = SQLITE_CORRUPT; + }else{ + p->sz = size; + } + memdbLeave(p); + return rc; +} + +/* +** Sync an memdb-file. +*/ +static int memdbSync(sqlite3_file *pFile, int flags){ + UNUSED_PARAMETER(pFile); + UNUSED_PARAMETER(flags); + return SQLITE_OK; +} + +/* +** Return the current file-size of an memdb-file. +*/ +static int memdbFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){ + MemStore *p = ((MemFile*)pFile)->pStore; + memdbEnter(p); + *pSize = p->sz; + memdbLeave(p); + return SQLITE_OK; +} + +/* +** Lock an memdb-file. +*/ +static int memdbLock(sqlite3_file *pFile, int eLock){ + MemFile *pThis = (MemFile*)pFile; + MemStore *p = pThis->pStore; + int rc = SQLITE_OK; + if( eLock<=pThis->eLock ) return SQLITE_OK; + memdbEnter(p); + + assert( p->nWrLock==0 || p->nWrLock==1 ); + assert( pThis->eLock<=SQLITE_LOCK_SHARED || p->nWrLock==1 ); + assert( pThis->eLock==SQLITE_LOCK_NONE || p->nRdLock>=1 ); + + if( eLock>SQLITE_LOCK_SHARED && (p->mFlags & SQLITE_DESERIALIZE_READONLY) ){ + rc = SQLITE_READONLY; + }else{ + switch( eLock ){ + case SQLITE_LOCK_SHARED: { + assert( pThis->eLock==SQLITE_LOCK_NONE ); + if( p->nWrLock>0 ){ + rc = SQLITE_BUSY; + }else{ + p->nRdLock++; + } + break; + }; + + case SQLITE_LOCK_RESERVED: + case SQLITE_LOCK_PENDING: { + assert( pThis->eLock>=SQLITE_LOCK_SHARED ); + if( ALWAYS(pThis->eLock==SQLITE_LOCK_SHARED) ){ + if( p->nWrLock>0 ){ + rc = SQLITE_BUSY; + }else{ + p->nWrLock = 1; + } + } + break; + } + + default: { + assert( eLock==SQLITE_LOCK_EXCLUSIVE ); + assert( pThis->eLock>=SQLITE_LOCK_SHARED ); + if( p->nRdLock>1 ){ + rc = SQLITE_BUSY; + }else if( pThis->eLock==SQLITE_LOCK_SHARED ){ + p->nWrLock = 1; + } + break; + } + } + } + if( rc==SQLITE_OK ) pThis->eLock = eLock; + memdbLeave(p); + return rc; +} + +/* +** Unlock an memdb-file. +*/ +static int memdbUnlock(sqlite3_file *pFile, int eLock){ + MemFile *pThis = (MemFile*)pFile; + MemStore *p = pThis->pStore; + if( eLock>=pThis->eLock ) return SQLITE_OK; + memdbEnter(p); + + assert( eLock==SQLITE_LOCK_SHARED || eLock==SQLITE_LOCK_NONE ); + if( eLock==SQLITE_LOCK_SHARED ){ + if( ALWAYS(pThis->eLock>SQLITE_LOCK_SHARED) ){ + p->nWrLock--; + } + }else{ + if( pThis->eLock>SQLITE_LOCK_SHARED ){ + p->nWrLock--; + } + p->nRdLock--; + } + + pThis->eLock = eLock; + memdbLeave(p); + return SQLITE_OK; +} + +#if 0 +/* +** This interface is only used for crash recovery, which does not +** occur on an in-memory database. +*/ +static int memdbCheckReservedLock(sqlite3_file *pFile, int *pResOut){ + *pResOut = 0; + return SQLITE_OK; +} +#endif + + +/* +** File control method. For custom operations on an memdb-file. +*/ +static int memdbFileControl(sqlite3_file *pFile, int op, void *pArg){ + MemStore *p = ((MemFile*)pFile)->pStore; + int rc = SQLITE_NOTFOUND; + memdbEnter(p); + if( op==SQLITE_FCNTL_VFSNAME ){ + *(char**)pArg = sqlite3_mprintf("memdb(%p,%lld)", p->aData, p->sz); + rc = SQLITE_OK; + } + if( op==SQLITE_FCNTL_SIZE_LIMIT ){ + sqlite3_int64 iLimit = *(sqlite3_int64*)pArg; + if( iLimitsz ){ + if( iLimit<0 ){ + iLimit = p->szMax; + }else{ + iLimit = p->sz; + } + } + p->szMax = iLimit; + *(sqlite3_int64*)pArg = iLimit; + rc = SQLITE_OK; + } + memdbLeave(p); + return rc; +} + +#if 0 /* Not used because of SQLITE_IOCAP_POWERSAFE_OVERWRITE */ +/* +** Return the sector-size in bytes for an memdb-file. +*/ +static int memdbSectorSize(sqlite3_file *pFile){ + return 1024; +} +#endif + +/* +** Return the device characteristic flags supported by an memdb-file. +*/ +static int memdbDeviceCharacteristics(sqlite3_file *pFile){ + UNUSED_PARAMETER(pFile); + return SQLITE_IOCAP_ATOMIC | + SQLITE_IOCAP_POWERSAFE_OVERWRITE | + SQLITE_IOCAP_SAFE_APPEND | + SQLITE_IOCAP_SEQUENTIAL; +} + +/* Fetch a page of a memory-mapped file */ +static int memdbFetch( + sqlite3_file *pFile, + sqlite3_int64 iOfst, + int iAmt, + void **pp +){ + MemStore *p = ((MemFile*)pFile)->pStore; + memdbEnter(p); + if( iOfst+iAmt>p->sz || (p->mFlags & SQLITE_DESERIALIZE_RESIZEABLE)!=0 ){ + *pp = 0; + }else{ + p->nMmap++; + *pp = (void*)(p->aData + iOfst); + } + memdbLeave(p); + return SQLITE_OK; +} + +/* Release a memory-mapped page */ +static int memdbUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){ + MemStore *p = ((MemFile*)pFile)->pStore; + UNUSED_PARAMETER(iOfst); + UNUSED_PARAMETER(pPage); + memdbEnter(p); + p->nMmap--; + memdbLeave(p); + return SQLITE_OK; +} + +/* +** Open an mem file handle. +*/ +static int memdbOpen( + sqlite3_vfs *pVfs, + const char *zName, + sqlite3_file *pFd, + int flags, + int *pOutFlags +){ + MemFile *pFile = (MemFile*)pFd; + MemStore *p = 0; + int szName; + UNUSED_PARAMETER(pVfs); + + memset(pFile, 0, sizeof(*pFile)); + szName = sqlite3Strlen30(zName); + if( szName>1 && (zName[0]=='/' || zName[0]=='\\') ){ + int i; +#ifndef SQLITE_MUTEX_OMIT + sqlite3_mutex *pVfsMutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1); +#endif + sqlite3_mutex_enter(pVfsMutex); + for(i=0; izFName,zName)==0 ){ + p = memdb_g.apMemStore[i]; + break; + } + } + if( p==0 ){ + MemStore **apNew; + p = sqlite3Malloc( sizeof(*p) + (i64)szName + 3 ); + if( p==0 ){ + sqlite3_mutex_leave(pVfsMutex); + return SQLITE_NOMEM; + } + apNew = sqlite3Realloc(memdb_g.apMemStore, + sizeof(apNew[0])*(1+(i64)memdb_g.nMemStore) ); + if( apNew==0 ){ + sqlite3_free(p); + sqlite3_mutex_leave(pVfsMutex); + return SQLITE_NOMEM; + } + apNew[memdb_g.nMemStore++] = p; + memdb_g.apMemStore = apNew; + memset(p, 0, sizeof(*p)); + p->mFlags = SQLITE_DESERIALIZE_RESIZEABLE|SQLITE_DESERIALIZE_FREEONCLOSE; + p->szMax = sqlite3GlobalConfig.mxMemdbSize; + p->zFName = (char*)&p[1]; + memcpy(p->zFName, zName, szName+1); + p->pMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); + if( p->pMutex==0 ){ + memdb_g.nMemStore--; + sqlite3_free(p); + sqlite3_mutex_leave(pVfsMutex); + return SQLITE_NOMEM; + } + p->nRef = 1; + memdbEnter(p); + }else{ + memdbEnter(p); + p->nRef++; + } + sqlite3_mutex_leave(pVfsMutex); + }else{ + p = sqlite3Malloc( sizeof(*p) ); + if( p==0 ){ + return SQLITE_NOMEM; + } + memset(p, 0, sizeof(*p)); + p->mFlags = SQLITE_DESERIALIZE_RESIZEABLE | SQLITE_DESERIALIZE_FREEONCLOSE; + p->szMax = sqlite3GlobalConfig.mxMemdbSize; + } + pFile->pStore = p; + if( pOutFlags!=0 ){ + *pOutFlags = flags | SQLITE_OPEN_MEMORY; + } + pFd->pMethods = &memdb_io_methods; + memdbLeave(p); + return SQLITE_OK; +} + +#if 0 /* Only used to delete rollback journals, super-journals, and WAL + ** files, none of which exist in memdb. So this routine is never used */ +/* +** Delete the file located at zPath. If the dirSync argument is true, +** ensure the file-system modifications are synced to disk before +** returning. +*/ +static int memdbDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){ + return SQLITE_IOERR_DELETE; +} +#endif + +/* +** Test for access permissions. Return true if the requested permission +** is available, or false otherwise. +** +** With memdb, no files ever exist on disk. So always return false. +*/ +static int memdbAccess( + sqlite3_vfs *pVfs, + const char *zPath, + int flags, + int *pResOut +){ + UNUSED_PARAMETER(pVfs); + UNUSED_PARAMETER(zPath); + UNUSED_PARAMETER(flags); + *pResOut = 0; + return SQLITE_OK; +} + +/* +** Populate buffer zOut with the full canonical pathname corresponding +** to the pathname in zPath. zOut is guaranteed to point to a buffer +** of at least (INST_MAX_PATHNAME+1) bytes. +*/ +static int memdbFullPathname( + sqlite3_vfs *pVfs, + const char *zPath, + int nOut, + char *zOut +){ + UNUSED_PARAMETER(pVfs); + sqlite3_snprintf(nOut, zOut, "%s", zPath); + return SQLITE_OK; +} + +/* +** Open the dynamic library located at zPath and return a handle. +*/ +static void *memdbDlOpen(sqlite3_vfs *pVfs, const char *zPath){ + return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath); +} + +/* +** Populate the buffer zErrMsg (size nByte bytes) with a human readable +** utf-8 string describing the most recent error encountered associated +** with dynamic libraries. +*/ +static void memdbDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){ + ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg); +} + +/* +** Return a pointer to the symbol zSymbol in the dynamic library pHandle. +*/ +static void (*memdbDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){ + return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym); +} + +/* +** Close the dynamic library handle pHandle. +*/ +static void memdbDlClose(sqlite3_vfs *pVfs, void *pHandle){ + ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle); +} + +/* +** Populate the buffer pointed to by zBufOut with nByte bytes of +** random data. +*/ +static int memdbRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){ + return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut); +} + +/* +** Sleep for nMicro microseconds. Return the number of microseconds +** actually slept. +*/ +static int memdbSleep(sqlite3_vfs *pVfs, int nMicro){ + return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro); +} + +#if 0 /* Never used. Modern cores only call xCurrentTimeInt64() */ +/* +** Return the current time as a Julian Day number in *pTimeOut. +*/ +static int memdbCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){ + return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut); +} +#endif + +static int memdbGetLastError(sqlite3_vfs *pVfs, int a, char *b){ + return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b); +} +static int memdbCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){ + return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p); +} + +/* +** Translate a database connection pointer and schema name into a +** MemFile pointer. +*/ +static MemFile *memdbFromDbSchema(sqlite3 *db, const char *zSchema){ + MemFile *p = 0; + MemStore *pStore; + int rc = sqlite3_file_control(db, zSchema, SQLITE_FCNTL_FILE_POINTER, &p); + if( rc ) return 0; + if( p->base.pMethods!=&memdb_io_methods ) return 0; + pStore = p->pStore; + memdbEnter(pStore); + if( pStore->zFName!=0 ) p = 0; + memdbLeave(pStore); + return p; +} + +/* +** Return the serialization of a database +*/ +unsigned char *sqlite3_serialize( + sqlite3 *db, /* The database connection */ + const char *zSchema, /* Which database within the connection */ + sqlite3_int64 *piSize, /* Write size here, if not NULL */ + unsigned int mFlags /* Maybe SQLITE_SERIALIZE_NOCOPY */ +){ + MemFile *p; + int iDb; + Btree *pBt; + sqlite3_int64 sz; + int szPage = 0; + sqlite3_stmt *pStmt = 0; + unsigned char *pOut; + char *zSql; + int rc; + +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) ){ + (void)SQLITE_MISUSE_BKPT; + return 0; + } +#endif + + if( zSchema==0 ) zSchema = db->aDb[0].zDbSName; + p = memdbFromDbSchema(db, zSchema); + iDb = sqlite3FindDbName(db, zSchema); + if( piSize ) *piSize = -1; + if( iDb<0 ) return 0; + if( p ){ + MemStore *pStore = p->pStore; + assert( pStore->pMutex==0 ); + if( piSize ) *piSize = pStore->sz; + if( mFlags & SQLITE_SERIALIZE_NOCOPY ){ + pOut = pStore->aData; + }else{ + pOut = sqlite3_malloc64( pStore->sz ); + if( pOut ) memcpy(pOut, pStore->aData, pStore->sz); + } + return pOut; + } + pBt = db->aDb[iDb].pBt; + if( pBt==0 ) return 0; + szPage = sqlite3BtreeGetPageSize(pBt); + zSql = sqlite3_mprintf("PRAGMA \"%w\".page_count", zSchema); + rc = zSql ? sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0) : SQLITE_NOMEM; + sqlite3_free(zSql); + if( rc ) return 0; + rc = sqlite3_step(pStmt); + if( rc!=SQLITE_ROW ){ + pOut = 0; + }else{ + sz = sqlite3_column_int64(pStmt, 0)*szPage; + if( sz==0 ){ + sqlite3_reset(pStmt); + sqlite3_exec(db, "BEGIN IMMEDIATE; COMMIT;", 0, 0, 0); + rc = sqlite3_step(pStmt); + if( rc==SQLITE_ROW ){ + sz = sqlite3_column_int64(pStmt, 0)*szPage; + } + } + if( piSize ) *piSize = sz; + if( mFlags & SQLITE_SERIALIZE_NOCOPY ){ + pOut = 0; + }else{ + pOut = sqlite3_malloc64( sz ); + if( pOut ){ + int nPage = sqlite3_column_int(pStmt, 0); + Pager *pPager = sqlite3BtreePager(pBt); + int pgno; + for(pgno=1; pgno<=nPage; pgno++){ + DbPage *pPage = 0; + unsigned char *pTo = pOut + szPage*(sqlite3_int64)(pgno-1); + rc = sqlite3PagerGet(pPager, pgno, (DbPage**)&pPage, 0); + if( rc==SQLITE_OK ){ + memcpy(pTo, sqlite3PagerGetData(pPage), szPage); + }else{ + memset(pTo, 0, szPage); + } + sqlite3PagerUnref(pPage); + } + } + } + } + sqlite3_finalize(pStmt); + return pOut; +} + +/* Convert zSchema to a MemDB and initialize its content. +*/ +int sqlite3_deserialize( + sqlite3 *db, /* The database connection */ + const char *zSchema, /* Which DB to reopen with the deserialization */ + unsigned char *pData, /* The serialized database content */ + sqlite3_int64 szDb, /* Number bytes in the deserialization */ + sqlite3_int64 szBuf, /* Total size of buffer pData[] */ + unsigned mFlags /* Zero or more SQLITE_DESERIALIZE_* flags */ +){ + MemFile *p; + char *zSql; + sqlite3_stmt *pStmt = 0; + int rc; + int iDb; + +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) ){ + return SQLITE_MISUSE_BKPT; + } + if( szDb<0 ) return SQLITE_MISUSE_BKPT; + if( szBuf<0 ) return SQLITE_MISUSE_BKPT; +#endif + + sqlite3_mutex_enter(db->mutex); + if( zSchema==0 ) zSchema = db->aDb[0].zDbSName; + iDb = sqlite3FindDbName(db, zSchema); + testcase( iDb==1 ); + if( iDb<2 && iDb!=0 ){ + rc = SQLITE_ERROR; + goto end_deserialize; + } + zSql = sqlite3_mprintf("ATTACH x AS %Q", zSchema); + if( zSql==0 ){ + rc = SQLITE_NOMEM; + }else{ + rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); + sqlite3_free(zSql); + } + if( rc ) goto end_deserialize; + db->init.iDb = (u8)iDb; + db->init.reopenMemdb = 1; + rc = sqlite3_step(pStmt); + db->init.reopenMemdb = 0; + if( rc!=SQLITE_DONE ){ + rc = SQLITE_ERROR; + goto end_deserialize; + } + p = memdbFromDbSchema(db, zSchema); + if( p==0 ){ + rc = SQLITE_ERROR; + }else{ + MemStore *pStore = p->pStore; + pStore->aData = pData; + pData = 0; + pStore->sz = szDb; + pStore->szAlloc = szBuf; + pStore->szMax = szBuf; + if( pStore->szMaxszMax = sqlite3GlobalConfig.mxMemdbSize; + } + pStore->mFlags = mFlags; + rc = SQLITE_OK; + } + +end_deserialize: + sqlite3_finalize(pStmt); + if( pData && (mFlags & SQLITE_DESERIALIZE_FREEONCLOSE)!=0 ){ + sqlite3_free(pData); + } + sqlite3_mutex_leave(db->mutex); + return rc; +} + +/* +** Return true if the VFS is the memvfs. +*/ +int sqlite3IsMemdb(const sqlite3_vfs *pVfs){ + return pVfs==&memdb_vfs; +} + +/* +** This routine is called when the extension is loaded. +** Register the new VFS. +*/ +int sqlite3MemdbInit(void){ + sqlite3_vfs *pLower = sqlite3_vfs_find(0); + unsigned int sz; + if( NEVER(pLower==0) ) return SQLITE_ERROR; + sz = pLower->szOsFile; + memdb_vfs.pAppData = pLower; + /* The following conditional can only be true when compiled for + ** Windows x86 and SQLITE_MAX_MMAP_SIZE=0. We always leave + ** it in, to be safe, but it is marked as NO_TEST since there + ** is no way to reach it under most builds. */ + if( szendpoint.iOffset ); - + if( (iAmt+iOfst)>p->endpoint.iOffset ){ + return SQLITE_IOERR_SHORT_READ; + } + assert( p->readpoint.iOffset==0 || p->readpoint.pChunk!=0 ); if( p->readpoint.iOffset!=iOfst || iOfst==0 ){ sqlite3_int64 iOff = 0; for(pChunk=p->pFirst; - ALWAYS(pChunk) && (iOff+JOURNAL_CHUNKSIZE)<=iOfst; + ALWAYS(pChunk) && (iOff+p->nChunkSize)<=iOfst; pChunk=pChunk->pNext ){ - iOff += JOURNAL_CHUNKSIZE; + iOff += p->nChunkSize; } }else{ pChunk = p->readpoint.pChunk; + assert( pChunk!=0 ); } - iChunkOffset = (int)(iOfst%JOURNAL_CHUNKSIZE); + iChunkOffset = (int)(iOfst%p->nChunkSize); do { - int iSpace = JOURNAL_CHUNKSIZE - iChunkOffset; - int nCopy = MIN(nRead, (JOURNAL_CHUNKSIZE - iChunkOffset)); - memcpy(zOut, &pChunk->zChunk[iChunkOffset], nCopy); + int iSpace = p->nChunkSize - iChunkOffset; + int nCopy = MIN(nRead, (p->nChunkSize - iChunkOffset)); + memcpy(zOut, (u8*)pChunk->zChunk + iChunkOffset, nCopy); zOut += nCopy; nRead -= iSpace; iChunkOffset = 0; } while( nRead>=0 && (pChunk=pChunk->pNext)!=0 && nRead>0 ); - p->readpoint.iOffset = iOfst+iAmt; + p->readpoint.iOffset = pChunk ? iOfst+iAmt : 0; p->readpoint.pChunk = pChunk; return SQLITE_OK; } +/* +** Free the list of FileChunk structures headed at MemJournal.pFirst. +*/ +static void memjrnlFreeChunks(FileChunk *pFirst){ + FileChunk *pIter; + FileChunk *pNext; + for(pIter=pFirst; pIter; pIter=pNext){ + pNext = pIter->pNext; + sqlite3_free(pIter); + } +} + +/* +** Flush the contents of memory to a real file on disk. +*/ +static int memjrnlCreateFile(MemJournal *p){ + int rc; + sqlite3_file *pReal = (sqlite3_file*)p; + MemJournal copy = *p; + + memset(p, 0, sizeof(MemJournal)); + rc = sqlite3OsOpen(copy.pVfs, copy.zJournal, pReal, copy.flags, 0); + if( rc==SQLITE_OK ){ + int nChunk = copy.nChunkSize; + i64 iOff = 0; + FileChunk *pIter; + for(pIter=copy.pFirst; pIter; pIter=pIter->pNext){ + if( iOff + nChunk > copy.endpoint.iOffset ){ + nChunk = copy.endpoint.iOffset - iOff; + } + rc = sqlite3OsWrite(pReal, (u8*)pIter->zChunk, nChunk, iOff); + if( rc ) break; + iOff += nChunk; + } + if( rc==SQLITE_OK ){ + /* No error has occurred. Free the in-memory buffers. */ + memjrnlFreeChunks(copy.pFirst); + } + } + if( rc!=SQLITE_OK ){ + /* If an error occurred while creating or writing to the file, restore + ** the original before returning. This way, SQLite uses the in-memory + ** journal data to roll back changes made to the internal page-cache + ** before this function was called. */ + sqlite3OsClose(pReal); + *p = copy; + } + return rc; +} + + +/* Forward reference */ +static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size); + /* ** Write data to the file. */ @@ -118,58 +194,93 @@ static int memjrnlWrite( int nWrite = iAmt; u8 *zWrite = (u8 *)zBuf; - /* An in-memory journal file should only ever be appended to. Random - ** access writes are not required by sqlite. - */ - assert( iOfst==p->endpoint.iOffset ); - UNUSED_PARAMETER(iOfst); - - while( nWrite>0 ){ - FileChunk *pChunk = p->endpoint.pChunk; - int iChunkOffset = (int)(p->endpoint.iOffset%JOURNAL_CHUNKSIZE); - int iSpace = MIN(nWrite, JOURNAL_CHUNKSIZE - iChunkOffset); - - if( iChunkOffset==0 ){ - /* New chunk is required to extend the file. */ - FileChunk *pNew = sqlite3_malloc(sizeof(FileChunk)); - if( !pNew ){ - return SQLITE_IOERR_NOMEM; - } - pNew->pNext = 0; - if( pChunk ){ - assert( p->pFirst ); - pChunk->pNext = pNew; - }else{ - assert( !p->pFirst ); - p->pFirst = pNew; - } - p->endpoint.pChunk = pNew; + /* If the file should be created now, create it and write the new data + ** into the file on disk. */ + if( p->nSpill>0 && (iAmt+iOfst)>p->nSpill ){ + int rc = memjrnlCreateFile(p); + if( rc==SQLITE_OK ){ + rc = sqlite3OsWrite(pJfd, zBuf, iAmt, iOfst); } + return rc; + } + + /* If the contents of this write should be stored in memory */ + else{ + /* An in-memory journal file should only ever be appended to. Random + ** access writes are not required. The only exception to this is when + ** the in-memory journal is being used by a connection using the + ** atomic-write optimization. In this case the first 28 bytes of the + ** journal file may be written as part of committing the transaction. */ + assert( iOfst<=p->endpoint.iOffset ); + if( iOfst>0 && iOfst!=p->endpoint.iOffset ){ + memjrnlTruncate(pJfd, iOfst); + } + if( iOfst==0 && p->pFirst ){ + assert( p->nChunkSize>iAmt ); + memcpy((u8*)p->pFirst->zChunk, zBuf, iAmt); + }else{ + while( nWrite>0 ){ + FileChunk *pChunk = p->endpoint.pChunk; + int iChunkOffset = (int)(p->endpoint.iOffset%p->nChunkSize); + int iSpace = MIN(nWrite, p->nChunkSize - iChunkOffset); - memcpy(&p->endpoint.pChunk->zChunk[iChunkOffset], zWrite, iSpace); - zWrite += iSpace; - nWrite -= iSpace; - p->endpoint.iOffset += iSpace; + assert( pChunk!=0 || iChunkOffset==0 ); + if( iChunkOffset==0 ){ + /* New chunk is required to extend the file. */ + FileChunk *pNew = sqlite3_malloc(fileChunkSize(p->nChunkSize)); + if( !pNew ){ + return SQLITE_IOERR_NOMEM_BKPT; + } + pNew->pNext = 0; + if( pChunk ){ + assert( p->pFirst ); + pChunk->pNext = pNew; + }else{ + assert( !p->pFirst ); + p->pFirst = pNew; + } + pChunk = p->endpoint.pChunk = pNew; + } + + assert( pChunk!=0 ); + memcpy((u8*)pChunk->zChunk + iChunkOffset, zWrite, iSpace); + zWrite += iSpace; + nWrite -= iSpace; + p->endpoint.iOffset += iSpace; + } + } } return SQLITE_OK; } /* -** Truncate the file. +** Truncate the in-memory file. */ static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){ MemJournal *p = (MemJournal *)pJfd; - FileChunk *pChunk; - assert(size==0); - UNUSED_PARAMETER(size); - pChunk = p->pFirst; - while( pChunk ){ - FileChunk *pTmp = pChunk; - pChunk = pChunk->pNext; - sqlite3_free(pTmp); + assert( p->endpoint.pChunk==0 || p->endpoint.pChunk->pNext==0 ); + if( sizeendpoint.iOffset ){ + FileChunk *pIter = 0; + if( size==0 ){ + memjrnlFreeChunks(p->pFirst); + p->pFirst = 0; + }else{ + i64 iOff = p->nChunkSize; + for(pIter=p->pFirst; ALWAYS(pIter) && iOffpNext){ + iOff += p->nChunkSize; + } + if( ALWAYS(pIter) ){ + memjrnlFreeChunks(pIter->pNext); + pIter->pNext = 0; + } + } + + p->endpoint.pChunk = pIter; + p->endpoint.iOffset = size; + p->readpoint.pChunk = 0; + p->readpoint.iOffset = 0; } - sqlite3MemJournalOpen(pJfd); return SQLITE_OK; } @@ -177,21 +288,19 @@ static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){ ** Close the file. */ static int memjrnlClose(sqlite3_file *pJfd){ - memjrnlTruncate(pJfd, 0); + MemJournal *p = (MemJournal *)pJfd; + memjrnlFreeChunks(p->pFirst); return SQLITE_OK; } - /* ** Sync the file. ** -** Syncing an in-memory journal is a no-op. And, in fact, this routine -** is never called in a working implementation. This implementation -** exists purely as a contingency, in case some malfunction in some other -** part of SQLite causes Sync to be called by mistake. +** If the real file has been created, call its xSync method. Otherwise, +** syncing an in-memory journal is a no-op. */ -static int memjrnlSync(sqlite3_file *NotUsed, int NotUsed2){ - UNUSED_PARAMETER2(NotUsed, NotUsed2); +static int memjrnlSync(sqlite3_file *pJfd, int flags){ + UNUSED_PARAMETER2(pJfd, flags); return SQLITE_OK; } @@ -230,26 +339,102 @@ static const struct sqlite3_io_methods MemJournalMethods = { }; /* -** Open a journal file. +** Open a journal file. +** +** The behaviour of the journal file depends on the value of parameter +** nSpill. If nSpill is 0, then the journal file is always create and +** accessed using the underlying VFS. If nSpill is less than zero, then +** all content is always stored in main-memory. Finally, if nSpill is a +** positive value, then the journal file is initially created in-memory +** but may be flushed to disk later on. In this case the journal file is +** flushed to disk either when it grows larger than nSpill bytes in size, +** or when sqlite3JournalCreate() is called. +*/ +int sqlite3JournalOpen( + sqlite3_vfs *pVfs, /* The VFS to use for actual file I/O */ + const char *zName, /* Name of the journal file */ + sqlite3_file *pJfd, /* Preallocated, blank file handle */ + int flags, /* Opening flags */ + int nSpill /* Bytes buffered before opening the file */ +){ + MemJournal *p = (MemJournal*)pJfd; + + assert( zName || nSpill<0 || (flags & SQLITE_OPEN_EXCLUSIVE) ); + + /* Zero the file-handle object. If nSpill was passed zero, initialize + ** it using the sqlite3OsOpen() function of the underlying VFS. In this + ** case none of the code in this module is executed as a result of calls + ** made on the journal file-handle. */ + memset(p, 0, sizeof(MemJournal)); + if( nSpill==0 ){ + return sqlite3OsOpen(pVfs, zName, pJfd, flags, 0); + } + + if( nSpill>0 ){ + p->nChunkSize = nSpill; + }else{ + p->nChunkSize = 8 + MEMJOURNAL_DFLT_FILECHUNKSIZE - sizeof(FileChunk); + assert( MEMJOURNAL_DFLT_FILECHUNKSIZE==fileChunkSize(p->nChunkSize) ); + } + + pJfd->pMethods = (const sqlite3_io_methods*)&MemJournalMethods; + p->nSpill = nSpill; + p->flags = flags; + p->zJournal = zName; + p->pVfs = pVfs; + return SQLITE_OK; +} + +/* +** Open an in-memory journal file. */ void sqlite3MemJournalOpen(sqlite3_file *pJfd){ - MemJournal *p = (MemJournal *)pJfd; - assert( EIGHT_BYTE_ALIGNMENT(p) ); - memset(p, 0, sqlite3MemJournalSize()); - p->pMethod = (sqlite3_io_methods*)&MemJournalMethods; + sqlite3JournalOpen(0, 0, pJfd, 0, -1); +} + +#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \ + || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE) +/* +** If the argument p points to a MemJournal structure that is not an +** in-memory-only journal file (i.e. is one that was opened with a +ve +** nSpill parameter or as SQLITE_OPEN_MAIN_JOURNAL), and the underlying +** file has not yet been created, create it now. +*/ +int sqlite3JournalCreate(sqlite3_file *pJfd){ + int rc = SQLITE_OK; + MemJournal *p = (MemJournal*)pJfd; + if( pJfd->pMethods==&MemJournalMethods && ( +#ifdef SQLITE_ENABLE_ATOMIC_WRITE + p->nSpill>0 +#else + /* While this appears to not be possible without ATOMIC_WRITE, the + ** paths are complex, so it seems prudent to leave the test in as + ** a NEVER(), in case our analysis is subtly flawed. */ + NEVER(p->nSpill>0) +#endif +#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE + || (p->flags & SQLITE_OPEN_MAIN_JOURNAL) +#endif + )){ + rc = memjrnlCreateFile(p); + } + return rc; } +#endif /* -** Return true if the file-handle passed as an argument is -** an in-memory journal +** The file-handle passed as the only argument is open on a journal file. +** Return true if this "journal file" is currently stored in heap memory, +** or false otherwise. */ -int sqlite3IsMemJournal(sqlite3_file *pJfd){ - return pJfd->pMethods==&MemJournalMethods; +int sqlite3JournalIsInMemory(sqlite3_file *p){ + return p->pMethods==&MemJournalMethods; } /* -** Return the number of bytes required to store a MemJournal file descriptor. +** Return the number of bytes required to store a JournalFile that uses vfs +** pVfs to create the underlying on-disk files. */ -int sqlite3MemJournalSize(void){ - return sizeof(MemJournal); +int sqlite3JournalSize(sqlite3_vfs *pVfs){ + return MAX(pVfs->szOsFile, (int)sizeof(MemJournal)); } diff --git a/src/msvc.h b/src/msvc.h index 01ebf2b46f..10675e9a71 100644 --- a/src/msvc.h +++ b/src/msvc.h @@ -12,8 +12,8 @@ ** ** This file contains code that is specific to MSVC. */ -#ifndef _MSVC_H_ -#define _MSVC_H_ +#ifndef SQLITE_MSVC_H +#define SQLITE_MSVC_H #if defined(_MSC_VER) #pragma warning(disable : 4054) @@ -33,4 +33,13 @@ #pragma warning(disable : 4706) #endif /* defined(_MSC_VER) */ -#endif /* _MSVC_H_ */ +#if defined(_MSC_VER) && !defined(_WIN64) +#undef SQLITE_4_BYTE_ALIGNED_MALLOC +#define SQLITE_4_BYTE_ALIGNED_MALLOC +#endif /* defined(_MSC_VER) && !defined(_WIN64) */ + +#if !defined(HAVE_LOG2) && defined(_MSC_VER) && _MSC_VER<1800 +#define HAVE_LOG2 0 +#endif /* !defined(HAVE_LOG2) && defined(_MSC_VER) && _MSC_VER<1800 */ + +#endif /* SQLITE_MSVC_H */ diff --git a/src/mutex.c b/src/mutex.c index 6f1bc9767d..62e09cb4fa 100644 --- a/src/mutex.c +++ b/src/mutex.c @@ -26,6 +26,193 @@ static SQLITE_WSD int mutexIsInit = 0; #ifndef SQLITE_MUTEX_OMIT + +#ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS +/* +** This block (enclosed by SQLITE_ENABLE_MULTITHREADED_CHECKS) contains +** the implementation of a wrapper around the system default mutex +** implementation (sqlite3DefaultMutex()). +** +** Most calls are passed directly through to the underlying default +** mutex implementation. Except, if a mutex is configured by calling +** sqlite3MutexWarnOnContention() on it, then if contention is ever +** encountered within xMutexEnter() a warning is emitted via sqlite3_log(). +** +** This type of mutex is used as the database handle mutex when testing +** apps that usually use SQLITE_CONFIG_MULTITHREAD mode. +*/ + +/* +** Type for all mutexes used when SQLITE_ENABLE_MULTITHREADED_CHECKS +** is defined. Variable CheckMutex.mutex is a pointer to the real mutex +** allocated by the system mutex implementation. Variable iType is usually set +** to the type of mutex requested - SQLITE_MUTEX_RECURSIVE, SQLITE_MUTEX_FAST +** or one of the static mutex identifiers. Or, if this is a recursive mutex +** that has been configured using sqlite3MutexWarnOnContention(), it is +** set to SQLITE_MUTEX_WARNONCONTENTION. +*/ +typedef struct CheckMutex CheckMutex; +struct CheckMutex { + int iType; + sqlite3_mutex *mutex; +}; + +#define SQLITE_MUTEX_WARNONCONTENTION (-1) + +/* +** Pointer to real mutex methods object used by the CheckMutex +** implementation. Set by checkMutexInit(). +*/ +static SQLITE_WSD const sqlite3_mutex_methods *pGlobalMutexMethods; + +#ifdef SQLITE_DEBUG +static int checkMutexHeld(sqlite3_mutex *p){ + return pGlobalMutexMethods->xMutexHeld(((CheckMutex*)p)->mutex); +} +static int checkMutexNotheld(sqlite3_mutex *p){ + return pGlobalMutexMethods->xMutexNotheld(((CheckMutex*)p)->mutex); +} +#endif + +/* +** Initialize and deinitialize the mutex subsystem. +*/ +static int checkMutexInit(void){ + pGlobalMutexMethods = sqlite3DefaultMutex(); + return SQLITE_OK; +} +static int checkMutexEnd(void){ + pGlobalMutexMethods = 0; + return SQLITE_OK; +} + +/* +** Allocate a mutex. +*/ +static sqlite3_mutex *checkMutexAlloc(int iType){ + static CheckMutex staticMutexes[] = { + {2, 0}, {3, 0}, {4, 0}, {5, 0}, + {6, 0}, {7, 0}, {8, 0}, {9, 0}, + {10, 0}, {11, 0}, {12, 0}, {13, 0} + }; + CheckMutex *p = 0; + + assert( SQLITE_MUTEX_RECURSIVE==1 && SQLITE_MUTEX_FAST==0 ); + if( iType<2 ){ + p = sqlite3MallocZero(sizeof(CheckMutex)); + if( p==0 ) return 0; + p->iType = iType; + }else{ +#ifdef SQLITE_ENABLE_API_ARMOR + if( iType-2>=ArraySize(staticMutexes) ){ + (void)SQLITE_MISUSE_BKPT; + return 0; + } +#endif + p = &staticMutexes[iType-2]; + } + + if( p->mutex==0 ){ + p->mutex = pGlobalMutexMethods->xMutexAlloc(iType); + if( p->mutex==0 ){ + if( iType<2 ){ + sqlite3_free(p); + } + p = 0; + } + } + + return (sqlite3_mutex*)p; +} + +/* +** Free a mutex. +*/ +static void checkMutexFree(sqlite3_mutex *p){ + assert( SQLITE_MUTEX_RECURSIVE<2 ); + assert( SQLITE_MUTEX_FAST<2 ); + assert( SQLITE_MUTEX_WARNONCONTENTION<2 ); + +#ifdef SQLITE_ENABLE_API_ARMOR + if( ((CheckMutex*)p)->iType<2 ) +#endif + { + CheckMutex *pCheck = (CheckMutex*)p; + pGlobalMutexMethods->xMutexFree(pCheck->mutex); + sqlite3_free(pCheck); + } +#ifdef SQLITE_ENABLE_API_ARMOR + else{ + (void)SQLITE_MISUSE_BKPT; + } +#endif +} + +/* +** Enter the mutex. +*/ +static void checkMutexEnter(sqlite3_mutex *p){ + CheckMutex *pCheck = (CheckMutex*)p; + if( pCheck->iType==SQLITE_MUTEX_WARNONCONTENTION ){ + if( SQLITE_OK==pGlobalMutexMethods->xMutexTry(pCheck->mutex) ){ + return; + } + sqlite3_log(SQLITE_MISUSE, + "illegal multi-threaded access to database connection" + ); + } + pGlobalMutexMethods->xMutexEnter(pCheck->mutex); +} + +/* +** Enter the mutex (do not block). +*/ +static int checkMutexTry(sqlite3_mutex *p){ + CheckMutex *pCheck = (CheckMutex*)p; + return pGlobalMutexMethods->xMutexTry(pCheck->mutex); +} + +/* +** Leave the mutex. +*/ +static void checkMutexLeave(sqlite3_mutex *p){ + CheckMutex *pCheck = (CheckMutex*)p; + pGlobalMutexMethods->xMutexLeave(pCheck->mutex); +} + +sqlite3_mutex_methods const *multiThreadedCheckMutex(void){ + static const sqlite3_mutex_methods sMutex = { + checkMutexInit, + checkMutexEnd, + checkMutexAlloc, + checkMutexFree, + checkMutexEnter, + checkMutexTry, + checkMutexLeave, +#ifdef SQLITE_DEBUG + checkMutexHeld, + checkMutexNotheld +#else + 0, + 0 +#endif + }; + return &sMutex; +} + +/* +** Mark the SQLITE_MUTEX_RECURSIVE mutex passed as the only argument as +** one on which there should be no contention. +*/ +void sqlite3MutexWarnOnContention(sqlite3_mutex *p){ + if( sqlite3GlobalConfig.mutex.xMutexAlloc==checkMutexAlloc ){ + CheckMutex *pCheck = (CheckMutex*)p; + assert( pCheck->iType==SQLITE_MUTEX_RECURSIVE ); + pCheck->iType = SQLITE_MUTEX_WARNONCONTENTION; + } +} +#endif /* ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS */ + /* ** Initialize the mutex system. */ @@ -41,7 +228,11 @@ int sqlite3MutexInit(void){ sqlite3_mutex_methods *pTo = &sqlite3GlobalConfig.mutex; if( sqlite3GlobalConfig.bCoreMutex ){ +#ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS + pFrom = multiThreadedCheckMutex(); +#else pFrom = sqlite3DefaultMutex(); +#endif }else{ pFrom = sqlite3NoopMutex(); } @@ -63,6 +254,7 @@ int sqlite3MutexInit(void){ GLOBAL(int, mutexIsInit) = 1; #endif + sqlite3MemoryBarrier(); return rc; } @@ -155,15 +347,28 @@ void sqlite3_mutex_leave(sqlite3_mutex *p){ /* ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are ** intended for use inside assert() statements. +** +** Because these routines raise false-positive alerts in TSAN, disable +** them (make them always return 1) when compiling with TSAN. */ int sqlite3_mutex_held(sqlite3_mutex *p){ +# if defined(__has_feature) +# if __has_feature(thread_sanitizer) + p = 0; +# endif +# endif assert( p==0 || sqlite3GlobalConfig.mutex.xMutexHeld ); return p==0 || sqlite3GlobalConfig.mutex.xMutexHeld(p); } int sqlite3_mutex_notheld(sqlite3_mutex *p){ +# if defined(__has_feature) +# if __has_feature(thread_sanitizer) + p = 0; +# endif +# endif assert( p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld ); return p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld(p); } -#endif +#endif /* NDEBUG */ #endif /* !defined(SQLITE_MUTEX_OMIT) */ diff --git a/src/mutex.h b/src/mutex.h index 03eb1faadb..a6806a2505 100644 --- a/src/mutex.h +++ b/src/mutex.h @@ -67,4 +67,5 @@ #define MUTEX_LOGIC(X) #else #define MUTEX_LOGIC(X) X +int sqlite3_mutex_held(sqlite3_mutex*); #endif /* defined(SQLITE_MUTEX_OMIT) */ diff --git a/src/mutex_unix.c b/src/mutex_unix.c index 55d08c8052..beae877f98 100644 --- a/src/mutex_unix.c +++ b/src/mutex_unix.c @@ -26,7 +26,7 @@ /* ** The sqlite3_mutex.id, sqlite3_mutex.nRef, and sqlite3_mutex.owner fields -** are necessary under two condidtions: (1) Debug builds and (2) using +** are necessary under two conditions: (1) Debug builds and (2) using ** home-grown mutexes. Encapsulate these conditions into a single #define. */ #if defined(SQLITE_DEBUG) || defined(SQLITE_HOMEGROWN_RECURSIVE_MUTEX) @@ -50,11 +50,12 @@ struct sqlite3_mutex { #endif }; #if SQLITE_MUTEX_NREF -#define SQLITE3_MUTEX_INITIALIZER {PTHREAD_MUTEX_INITIALIZER,0,0,(pthread_t)0,0} +# define SQLITE3_MUTEX_INITIALIZER(id) \ + {PTHREAD_MUTEX_INITIALIZER,id,0,(pthread_t)0,0} #elif defined(SQLITE_ENABLE_API_ARMOR) -#define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0 } +# define SQLITE3_MUTEX_INITIALIZER(id) { PTHREAD_MUTEX_INITIALIZER, id } #else -#define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER } +#define SQLITE3_MUTEX_INITIALIZER(id) { PTHREAD_MUTEX_INITIALIZER } #endif /* @@ -63,7 +64,7 @@ struct sqlite3_mutex { ** there might be race conditions that can cause these routines to ** deliver incorrect results. In particular, if pthread_equal() is ** not an atomic operation, then these routines might delivery -** incorrect results. On most platforms, pthread_equal() is a +** incorrect results. On most platforms, pthread_equal() is a ** comparison of two integers and is therefore atomic. But we are ** told that HPUX is not such a platform. If so, then these routines ** will not always work correctly on HPUX. @@ -111,7 +112,7 @@ static int pthreadMutexEnd(void){ return SQLITE_OK; } **
      **
    • SQLITE_MUTEX_FAST **
    • SQLITE_MUTEX_RECURSIVE -**
    • SQLITE_MUTEX_STATIC_MASTER +**
    • SQLITE_MUTEX_STATIC_MAIN **
    • SQLITE_MUTEX_STATIC_MEM **
    • SQLITE_MUTEX_STATIC_OPEN **
    • SQLITE_MUTEX_STATIC_PRNG @@ -145,24 +146,24 @@ static int pthreadMutexEnd(void){ return SQLITE_OK; } ** ** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc() -** returns a different mutex on every call. But for the static +** returns a different mutex on every call. But for the static ** mutex types, the same mutex is returned on every call that has ** the same type number. */ static sqlite3_mutex *pthreadMutexAlloc(int iType){ static sqlite3_mutex staticMutexes[] = { - SQLITE3_MUTEX_INITIALIZER, - SQLITE3_MUTEX_INITIALIZER, - SQLITE3_MUTEX_INITIALIZER, - SQLITE3_MUTEX_INITIALIZER, - SQLITE3_MUTEX_INITIALIZER, - SQLITE3_MUTEX_INITIALIZER, - SQLITE3_MUTEX_INITIALIZER, - SQLITE3_MUTEX_INITIALIZER, - SQLITE3_MUTEX_INITIALIZER, - SQLITE3_MUTEX_INITIALIZER, - SQLITE3_MUTEX_INITIALIZER, - SQLITE3_MUTEX_INITIALIZER + SQLITE3_MUTEX_INITIALIZER(2), + SQLITE3_MUTEX_INITIALIZER(3), + SQLITE3_MUTEX_INITIALIZER(4), + SQLITE3_MUTEX_INITIALIZER(5), + SQLITE3_MUTEX_INITIALIZER(6), + SQLITE3_MUTEX_INITIALIZER(7), + SQLITE3_MUTEX_INITIALIZER(8), + SQLITE3_MUTEX_INITIALIZER(9), + SQLITE3_MUTEX_INITIALIZER(10), + SQLITE3_MUTEX_INITIALIZER(11), + SQLITE3_MUTEX_INITIALIZER(12), + SQLITE3_MUTEX_INITIALIZER(13) }; sqlite3_mutex *p; switch( iType ){ @@ -180,6 +181,9 @@ static sqlite3_mutex *pthreadMutexAlloc(int iType){ pthread_mutexattr_settype(&recursiveAttr, PTHREAD_MUTEX_RECURSIVE); pthread_mutex_init(&p->mutex, &recursiveAttr); pthread_mutexattr_destroy(&recursiveAttr); +#endif +#if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR) + p->id = SQLITE_MUTEX_RECURSIVE; #endif } break; @@ -188,6 +192,9 @@ static sqlite3_mutex *pthreadMutexAlloc(int iType){ p = sqlite3MallocZero( sizeof(*p) ); if( p ){ pthread_mutex_init(&p->mutex, 0); +#if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR) + p->id = SQLITE_MUTEX_FAST; +#endif } break; } @@ -203,7 +210,7 @@ static sqlite3_mutex *pthreadMutexAlloc(int iType){ } } #if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR) - if( p ) p->id = iType; + assert( p==0 || p->id==iType ); #endif return p; } @@ -216,7 +223,7 @@ static sqlite3_mutex *pthreadMutexAlloc(int iType){ */ static void pthreadMutexFree(sqlite3_mutex *p){ assert( p->nRef==0 ); -#if SQLITE_ENABLE_API_ARMOR +#ifdef SQLITE_ENABLE_API_ARMOR if( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE ) #endif { @@ -250,7 +257,7 @@ static void pthreadMutexEnter(sqlite3_mutex *p){ ** is atomic - that it cannot be deceived into thinking self ** and p->owner are equal if p->owner changes between two values ** that are not equal to self while the comparison is taking place. - ** This implementation also assumes a coherent cache - that + ** This implementation also assumes a coherent cache - that ** separate processes cannot read different values from the same ** address at the same time. If either of these two conditions ** are not met, then the mutexes will fail and problems will result. @@ -293,7 +300,7 @@ static int pthreadMutexTry(sqlite3_mutex *p){ ** is atomic - that it cannot be deceived into thinking self ** and p->owner are equal if p->owner changes between two values ** that are not equal to self while the comparison is taking place. - ** This implementation also assumes a coherent cache - that + ** This implementation also assumes a coherent cache - that ** separate processes cannot read different values from the same ** address at the same time. If either of these two conditions ** are not met, then the mutexes will fail and problems will result. diff --git a/src/mutex_w32.c b/src/mutex_w32.c index 9570bdc0bf..7eb5b50be1 100644 --- a/src/mutex_w32.c +++ b/src/mutex_w32.c @@ -38,9 +38,9 @@ struct sqlite3_mutex { CRITICAL_SECTION mutex; /* Mutex controlling the lock */ int id; /* Mutex type */ #ifdef SQLITE_DEBUG - volatile int nRef; /* Number of enterances */ + volatile int nRef; /* Number of entrances */ volatile DWORD owner; /* Thread holding this mutex */ - volatile int trace; /* True to trace changes */ + volatile LONG trace; /* True to trace changes */ #endif }; @@ -52,10 +52,10 @@ struct sqlite3_mutex { #define SQLITE_W32_MUTEX_INITIALIZER { 0 } #ifdef SQLITE_DEBUG -#define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0, \ +#define SQLITE3_MUTEX_INITIALIZER(id) { SQLITE_W32_MUTEX_INITIALIZER, id, \ 0L, (DWORD)0, 0 } #else -#define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0 } +#define SQLITE3_MUTEX_INITIALIZER(id) { SQLITE_W32_MUTEX_INITIALIZER, id } #endif #ifdef SQLITE_DEBUG @@ -87,8 +87,7 @@ void sqlite3MemoryBarrier(void){ SQLITE_MEMORY_BARRIER; #elif defined(__GNUC__) __sync_synchronize(); -#elif !defined(SQLITE_DISABLE_INTRINSIC) && \ - defined(_MSC_VER) && _MSC_VER>=1300 +#elif MSVC_VERSION>=1400 _ReadWriteBarrier(); #elif defined(MemoryBarrier) MemoryBarrier(); @@ -99,18 +98,18 @@ void sqlite3MemoryBarrier(void){ ** Initialize and deinitialize the mutex subsystem. */ static sqlite3_mutex winMutex_staticMutexes[] = { - SQLITE3_MUTEX_INITIALIZER, - SQLITE3_MUTEX_INITIALIZER, - SQLITE3_MUTEX_INITIALIZER, - SQLITE3_MUTEX_INITIALIZER, - SQLITE3_MUTEX_INITIALIZER, - SQLITE3_MUTEX_INITIALIZER, - SQLITE3_MUTEX_INITIALIZER, - SQLITE3_MUTEX_INITIALIZER, - SQLITE3_MUTEX_INITIALIZER, - SQLITE3_MUTEX_INITIALIZER, - SQLITE3_MUTEX_INITIALIZER, - SQLITE3_MUTEX_INITIALIZER + SQLITE3_MUTEX_INITIALIZER(2), + SQLITE3_MUTEX_INITIALIZER(3), + SQLITE3_MUTEX_INITIALIZER(4), + SQLITE3_MUTEX_INITIALIZER(5), + SQLITE3_MUTEX_INITIALIZER(6), + SQLITE3_MUTEX_INITIALIZER(7), + SQLITE3_MUTEX_INITIALIZER(8), + SQLITE3_MUTEX_INITIALIZER(9), + SQLITE3_MUTEX_INITIALIZER(10), + SQLITE3_MUTEX_INITIALIZER(11), + SQLITE3_MUTEX_INITIALIZER(12), + SQLITE3_MUTEX_INITIALIZER(13) }; static int winMutex_isInit = 0; @@ -172,7 +171,7 @@ static int winMutexEnd(void){ **
        **
      • SQLITE_MUTEX_FAST **
      • SQLITE_MUTEX_RECURSIVE -**
      • SQLITE_MUTEX_STATIC_MASTER +**
      • SQLITE_MUTEX_STATIC_MAIN **
      • SQLITE_MUTEX_STATIC_MEM **
      • SQLITE_MUTEX_STATIC_OPEN **
      • SQLITE_MUTEX_STATIC_PRNG @@ -240,15 +239,15 @@ static sqlite3_mutex *winMutexAlloc(int iType){ } #endif p = &winMutex_staticMutexes[iType-2]; - p->id = iType; #ifdef SQLITE_DEBUG #ifdef SQLITE_WIN32_MUTEX_TRACE_STATIC - p->trace = 1; + InterlockedCompareExchange(&p->trace, 1, 0); #endif #endif break; } } + assert( p==0 || p->id==iType ); return p; } @@ -299,8 +298,8 @@ static void winMutexEnter(sqlite3_mutex *p){ p->owner = tid; p->nRef++; if( p->trace ){ - OSTRACE(("ENTER-MUTEX tid=%lu, mutex=%p (%d), nRef=%d\n", - tid, p, p->trace, p->nRef)); + OSTRACE(("ENTER-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n", + tid, p->id, p, p->trace, p->nRef)); } #endif } @@ -342,8 +341,8 @@ static int winMutexTry(sqlite3_mutex *p){ #endif #ifdef SQLITE_DEBUG if( p->trace ){ - OSTRACE(("TRY-MUTEX tid=%lu, mutex=%p (%d), owner=%lu, nRef=%d, rc=%s\n", - tid, p, p->trace, p->owner, p->nRef, sqlite3ErrName(rc))); + OSTRACE(("TRY-MUTEX tid=%lu, mutex(%d)=%p (%d), owner=%lu, nRef=%d, rc=%s\n", + tid, p->id, p, p->trace, p->owner, p->nRef, sqlite3ErrName(rc))); } #endif return rc; @@ -371,8 +370,8 @@ static void winMutexLeave(sqlite3_mutex *p){ LeaveCriticalSection(&p->mutex); #ifdef SQLITE_DEBUG if( p->trace ){ - OSTRACE(("LEAVE-MUTEX tid=%lu, mutex=%p (%d), nRef=%d\n", - tid, p, p->trace, p->nRef)); + OSTRACE(("LEAVE-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n", + tid, p->id, p, p->trace, p->nRef)); } #endif } diff --git a/src/notify.c b/src/notify.c index 8137226f35..6a4cab8755 100644 --- a/src/notify.c +++ b/src/notify.c @@ -29,12 +29,12 @@ */ #define assertMutexHeld() \ - assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)) ) + assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN)) ) /* ** Head of a linked list of all sqlite3 objects created by this process ** for which either sqlite3.pBlockingConnection or sqlite3.pUnlockConnection -** is not NULL. This variable may only accessed while the STATIC_MASTER +** is not NULL. This variable may only accessed while the STATIC_MAIN ** mutex is held. */ static sqlite3 *SQLITE_WSD sqlite3BlockedList = 0; @@ -108,20 +108,20 @@ static void addToBlockedList(sqlite3 *db){ } /* -** Obtain the STATIC_MASTER mutex. +** Obtain the STATIC_MAIN mutex. */ static void enterMutex(void){ - sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); + sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN)); checkListProperties(0); } /* -** Release the STATIC_MASTER mutex. +** Release the STATIC_MAIN mutex. */ static void leaveMutex(void){ assertMutexHeld(); checkListProperties(0); - sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); + sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN)); } /* @@ -152,6 +152,9 @@ int sqlite3_unlock_notify( ){ int rc = SQLITE_OK; +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; +#endif sqlite3_mutex_enter(db->mutex); enterMutex(); @@ -232,7 +235,7 @@ void sqlite3ConnectionUnlocked(sqlite3 *db){ void *aStatic[16]; /* Starter space for aArg[]. No malloc required */ aArg = aStatic; - enterMutex(); /* Enter STATIC_MASTER mutex */ + enterMutex(); /* Enter STATIC_MAIN mutex */ /* This loop runs once for each entry in the blocked-connections list. */ for(pp=&sqlite3BlockedList; *pp; /* no-op */ ){ @@ -315,7 +318,7 @@ void sqlite3ConnectionUnlocked(sqlite3 *db){ xUnlockNotify(aArg, nArg); } sqlite3_free(aDyn); - leaveMutex(); /* Leave STATIC_MASTER mutex */ + leaveMutex(); /* Leave STATIC_MAIN mutex */ } /* diff --git a/src/os.c b/src/os.c index 90130d0eb7..a9fc732e7e 100644 --- a/src/os.c +++ b/src/os.c @@ -13,9 +13,7 @@ ** This file contains OS interface code that is common to all ** architectures. */ -#define _SQLITE_OS_C_ 1 #include "sqliteInt.h" -#undef _SQLITE_OS_C_ /* ** If we compile with the SQLITE_TEST macro set, then the following block @@ -66,9 +64,9 @@ int sqlite3_open_file_count = 0; #if defined(SQLITE_TEST) int sqlite3_memdebug_vfs_oom_test = 1; #define DO_OS_MALLOC_TEST(x) \ - if (sqlite3_memdebug_vfs_oom_test && (!x || !sqlite3IsMemJournal(x))) { \ + if (sqlite3_memdebug_vfs_oom_test && (!x || !sqlite3JournalIsInMemory(x))) { \ void *pTstAlloc = sqlite3Malloc(10); \ - if (!pTstAlloc) return SQLITE_IOERR_NOMEM; \ + if (!pTstAlloc) return SQLITE_IOERR_NOMEM_BKPT; \ sqlite3_free(pTstAlloc); \ } #else @@ -81,13 +79,11 @@ int sqlite3_memdebug_vfs_oom_test = 1; ** of this would be completely automatic if SQLite were coded using ** C++ instead of plain old C. */ -int sqlite3OsClose(sqlite3_file *pId){ - int rc = SQLITE_OK; +void sqlite3OsClose(sqlite3_file *pId){ if( pId->pMethods ){ - rc = pId->pMethods->xClose(pId); + pId->pMethods->xClose(pId); pId->pMethods = 0; } - return rc; } int sqlite3OsRead(sqlite3_file *id, void *pBuf, int amt, i64 offset){ DO_OS_MALLOC_TEST(id); @@ -102,7 +98,7 @@ int sqlite3OsTruncate(sqlite3_file *id, i64 size){ } int sqlite3OsSync(sqlite3_file *id, int flags){ DO_OS_MALLOC_TEST(id); - return id->pMethods->xSync(id, flags); + return flags ? id->pMethods->xSync(id, flags) : SQLITE_OK; } int sqlite3OsFileSize(sqlite3_file *id, i64 *pSize){ DO_OS_MALLOC_TEST(id); @@ -110,9 +106,11 @@ int sqlite3OsFileSize(sqlite3_file *id, i64 *pSize){ } int sqlite3OsLock(sqlite3_file *id, int lockType){ DO_OS_MALLOC_TEST(id); + assert( lockType>=SQLITE_LOCK_SHARED && lockType<=SQLITE_LOCK_EXCLUSIVE ); return id->pMethods->xLock(id, lockType); } int sqlite3OsUnlock(sqlite3_file *id, int lockType){ + assert( lockType==SQLITE_LOCK_NONE || lockType==SQLITE_LOCK_SHARED ); return id->pMethods->xUnlock(id, lockType); } int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut){ @@ -129,25 +127,35 @@ int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut){ ** routine has no return value since the return value would be meaningless. */ int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){ + if( id->pMethods==0 ) return SQLITE_NOTFOUND; #ifdef SQLITE_TEST - if( op!=SQLITE_FCNTL_COMMIT_PHASETWO ){ + if( op!=SQLITE_FCNTL_COMMIT_PHASETWO + && op!=SQLITE_FCNTL_LOCK_TIMEOUT + && op!=SQLITE_FCNTL_CKPT_DONE + && op!=SQLITE_FCNTL_CKPT_START + ){ /* Faults are not injected into COMMIT_PHASETWO because, assuming SQLite ** is using a regular VFS, it is called after the corresponding ** transaction has been committed. Injecting a fault at this point - ** confuses the test scripts - the COMMIT comand returns SQLITE_NOMEM + ** confuses the test scripts - the COMMIT command returns SQLITE_NOMEM ** but the transaction is committed anyway. ** ** The core must call OsFileControl() though, not OsFileControlHint(), ** as if a custom VFS (e.g. zipvfs) returns an error here, it probably ** means the commit really has failed and an error should be returned - ** to the user. */ + ** to the user. + ** + ** The CKPT_DONE and CKPT_START file-controls are write-only signals + ** to the cksumvfs. Their return code is meaningless and is ignored + ** by the SQLite core, so there is no point in simulating OOMs for them. + */ DO_OS_MALLOC_TEST(id); } #endif return id->pMethods->xFileControl(id, op, pArg); } void sqlite3OsFileControlHint(sqlite3_file *id, int op, void *pArg){ - (void)id->pMethods->xFileControl(id, op, pArg); + if( id->pMethods ) (void)id->pMethods->xFileControl(id, op, pArg); } int sqlite3OsSectorSize(sqlite3_file *id){ @@ -155,8 +163,10 @@ int sqlite3OsSectorSize(sqlite3_file *id){ return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE); } int sqlite3OsDeviceCharacteristics(sqlite3_file *id){ + if( NEVER(id->pMethods==0) ) return 0; return id->pMethods->xDeviceCharacteristics(id); } +#ifndef SQLITE_OMIT_WAL int sqlite3OsShmLock(sqlite3_file *id, int offset, int n, int flags){ return id->pMethods->xShmLock(id, offset, n, flags); } @@ -176,6 +186,7 @@ int sqlite3OsShmMap( DO_OS_MALLOC_TEST(id); return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp); } +#endif /* SQLITE_OMIT_WAL */ #if SQLITE_MAX_MMAP_SIZE>0 /* The real implementation of xFetch and xUnfetch */ @@ -214,14 +225,15 @@ int sqlite3OsOpen( ** down into the VFS layer. Some SQLITE_OPEN_ flags (for example, ** SQLITE_OPEN_FULLMUTEX or SQLITE_OPEN_SHAREDCACHE) are blocked before ** reaching the VFS. */ - rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x87f7f, pFlagsOut); + assert( zPath || (flags & SQLITE_OPEN_EXCLUSIVE) ); + rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x1087f7f, pFlagsOut); assert( rc==SQLITE_OK || pFile->pMethods==0 ); return rc; } int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){ DO_OS_MALLOC_TEST(0); assert( dirSync==0 || dirSync==1 ); - return pVfs->xDelete(pVfs, zPath, dirSync); + return pVfs->xDelete!=0 ? pVfs->xDelete(pVfs, zPath, dirSync) : SQLITE_OK; } int sqlite3OsAccess( sqlite3_vfs *pVfs, @@ -244,6 +256,8 @@ int sqlite3OsFullPathname( } #ifndef SQLITE_OMIT_LOAD_EXTENSION void *sqlite3OsDlOpen(sqlite3_vfs *pVfs, const char *zPath){ + assert( zPath!=0 ); + assert( strlen(zPath)<=SQLITE_MAX_PATHLEN ); /* tag-20210611-1 */ return pVfs->xDlOpen(pVfs, zPath); } void sqlite3OsDlError(sqlite3_vfs *pVfs, int nByte, char *zBufOut){ @@ -257,11 +271,22 @@ void sqlite3OsDlClose(sqlite3_vfs *pVfs, void *pHandle){ } #endif /* SQLITE_OMIT_LOAD_EXTENSION */ int sqlite3OsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){ - return pVfs->xRandomness(pVfs, nByte, zBufOut); + if( sqlite3Config.iPrngSeed ){ + memset(zBufOut, 0, nByte); + if( ALWAYS(nByte>(signed)sizeof(unsigned)) ) nByte = sizeof(unsigned int); + memcpy(zBufOut, &sqlite3Config.iPrngSeed, nByte); + return SQLITE_OK; + }else{ + return pVfs->xRandomness(pVfs, nByte, zBufOut); + } + } int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){ return pVfs->xSleep(pVfs, nMicro); } +int sqlite3OsGetLastError(sqlite3_vfs *pVfs){ + return pVfs->xGetLastError ? pVfs->xGetLastError(pVfs, 0, 0) : 0; +} int sqlite3OsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){ int rc; /* IMPLEMENTATION-OF: R-49045-42493 SQLite will use the xCurrentTimeInt64() @@ -287,25 +312,28 @@ int sqlite3OsOpenMalloc( int flags, int *pOutFlags ){ - int rc = SQLITE_NOMEM; + int rc; sqlite3_file *pFile; pFile = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile); if( pFile ){ rc = sqlite3OsOpen(pVfs, zFile, pFile, flags, pOutFlags); if( rc!=SQLITE_OK ){ sqlite3_free(pFile); + *ppFile = 0; }else{ *ppFile = pFile; } + }else{ + *ppFile = 0; + rc = SQLITE_NOMEM_BKPT; } + assert( *ppFile!=0 || rc!=SQLITE_OK ); return rc; } -int sqlite3OsCloseFree(sqlite3_file *pFile){ - int rc = SQLITE_OK; +void sqlite3OsCloseFree(sqlite3_file *pFile){ assert( pFile ); - rc = sqlite3OsClose(pFile); + sqlite3OsClose(pFile); sqlite3_free(pFile); - return rc; } /* @@ -316,7 +344,7 @@ int sqlite3OsCloseFree(sqlite3_file *pFile){ */ int sqlite3OsInit(void){ void *p = sqlite3_malloc(10); - if( p==0 ) return SQLITE_NOMEM; + if( p==0 ) return SQLITE_NOMEM_BKPT; sqlite3_free(p); return sqlite3_os_init(); } @@ -341,7 +369,7 @@ sqlite3_vfs *sqlite3_vfs_find(const char *zVfs){ if( rc ) return 0; #endif #if SQLITE_THREADSAFE - mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); + mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); #endif sqlite3_mutex_enter(mutex); for(pVfs = vfsList; pVfs; pVfs=pVfs->pNext){ @@ -356,7 +384,7 @@ sqlite3_vfs *sqlite3_vfs_find(const char *zVfs){ ** Unlink a VFS from the linked list */ static void vfsUnlink(sqlite3_vfs *pVfs){ - assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)) ); + assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN)) ); if( pVfs==0 ){ /* No-op */ }else if( vfsList==pVfs ){ @@ -387,7 +415,7 @@ int sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){ if( pVfs==0 ) return SQLITE_MISUSE_BKPT; #endif - MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); ) + MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); ) sqlite3_mutex_enter(mutex); vfsUnlink(pVfs); if( makeDflt || vfsList==0 ){ @@ -406,9 +434,12 @@ int sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){ ** Unregister a VFS so that it is no longer accessible. */ int sqlite3_vfs_unregister(sqlite3_vfs *pVfs){ -#if SQLITE_THREADSAFE - sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); + MUTEX_LOGIC(sqlite3_mutex *mutex;) +#ifndef SQLITE_OMIT_AUTOINIT + int rc = sqlite3_initialize(); + if( rc ) return rc; #endif + MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); ) sqlite3_mutex_enter(mutex); vfsUnlink(pVfs); sqlite3_mutex_leave(mutex); diff --git a/src/os.h b/src/os.h index 2c1b86f913..aeb01721c2 100644 --- a/src/os.h +++ b/src/os.h @@ -33,6 +33,19 @@ # define SET_FULLSYNC(x,y) #endif +/* Maximum pathname length. Note: FILENAME_MAX defined by stdio.h +*/ +#ifndef SQLITE_MAX_PATHLEN +# define SQLITE_MAX_PATHLEN FILENAME_MAX +#endif + +/* Maximum number of symlinks that will be resolved while trying to +** expand a filename in xFullPathname() in the VFS. +*/ +#ifndef SQLITE_MAX_SYMLINK +# define SQLITE_MAX_SYMLINK 200 +#endif + /* ** The default size of a disk sector */ @@ -160,7 +173,7 @@ int sqlite3OsInit(void); /* ** Functions for accessing sqlite3_file methods */ -int sqlite3OsClose(sqlite3_file*); +void sqlite3OsClose(sqlite3_file*); int sqlite3OsRead(sqlite3_file*, void*, int amt, i64 offset); int sqlite3OsWrite(sqlite3_file*, const void*, int amt, i64 offset); int sqlite3OsTruncate(sqlite3_file*, i64 size); @@ -174,10 +187,12 @@ void sqlite3OsFileControlHint(sqlite3_file*,int,void*); #define SQLITE_FCNTL_DB_UNCHANGED 0xca093fa0 int sqlite3OsSectorSize(sqlite3_file *id); int sqlite3OsDeviceCharacteristics(sqlite3_file *id); +#ifndef SQLITE_OMIT_WAL int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **); int sqlite3OsShmLock(sqlite3_file *id, int, int, int); void sqlite3OsShmBarrier(sqlite3_file *id); int sqlite3OsShmUnmap(sqlite3_file *id, int); +#endif /* SQLITE_OMIT_WAL */ int sqlite3OsFetch(sqlite3_file *id, i64, int, void **); int sqlite3OsUnfetch(sqlite3_file *, i64, void *); @@ -197,6 +212,7 @@ void sqlite3OsDlClose(sqlite3_vfs *, void *); #endif /* SQLITE_OMIT_LOAD_EXTENSION */ int sqlite3OsRandomness(sqlite3_vfs *, int, char *); int sqlite3OsSleep(sqlite3_vfs *, int); +int sqlite3OsGetLastError(sqlite3_vfs*); int sqlite3OsCurrentTimeInt64(sqlite3_vfs *, sqlite3_int64*); /* @@ -204,6 +220,6 @@ int sqlite3OsCurrentTimeInt64(sqlite3_vfs *, sqlite3_int64*); ** sqlite3_malloc() to obtain space for the file-handle structure. */ int sqlite3OsOpenMalloc(sqlite3_vfs *, const char *, sqlite3_file **, int,int*); -int sqlite3OsCloseFree(sqlite3_file *); +void sqlite3OsCloseFree(sqlite3_file *); #endif /* _SQLITE_OS_H_ */ diff --git a/src/os_common.h b/src/os_common.h index 1ed4d7a8e1..5b532af0ac 100644 --- a/src/os_common.h +++ b/src/os_common.h @@ -35,12 +35,6 @@ */ #ifdef SQLITE_PERFORMANCE_TRACE -/* -** hwtime.h contains inline assembler code for implementing -** high-performance timing routines. -*/ -#include "hwtime.h" - static sqlite_uint64 g_start; static sqlite_uint64 g_elapsed; #define TIMER_START g_start=sqlite3Hwtime() diff --git a/src/os_kv.c b/src/os_kv.c new file mode 100644 index 0000000000..c2d1f9b7ad --- /dev/null +++ b/src/os_kv.c @@ -0,0 +1,979 @@ +/* +** 2022-09-06 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +** This file contains an experimental VFS layer that operates on a +** Key/Value storage engine where both keys and values must be pure +** text. +*/ +#include +#if SQLITE_OS_KV || (SQLITE_OS_UNIX && defined(SQLITE_OS_KV_OPTIONAL)) + +/***************************************************************************** +** Debugging logic +*/ + +/* SQLITE_KV_TRACE() is used for tracing calls to kvstorage routines. */ +#if 0 +#define SQLITE_KV_TRACE(X) printf X +#else +#define SQLITE_KV_TRACE(X) +#endif + +/* SQLITE_KV_LOG() is used for tracing calls to the VFS interface */ +#if 0 +#define SQLITE_KV_LOG(X) printf X +#else +#define SQLITE_KV_LOG(X) +#endif + + +/* +** Forward declaration of objects used by this VFS implementation +*/ +typedef struct KVVfsFile KVVfsFile; + +/* A single open file. There are only two files represented by this +** VFS - the database and the rollback journal. +*/ +struct KVVfsFile { + sqlite3_file base; /* IO methods */ + const char *zClass; /* Storage class */ + int isJournal; /* True if this is a journal file */ + unsigned int nJrnl; /* Space allocated for aJrnl[] */ + char *aJrnl; /* Journal content */ + int szPage; /* Last known page size */ + sqlite3_int64 szDb; /* Database file size. -1 means unknown */ + char *aData; /* Buffer to hold page data */ +}; +#define SQLITE_KVOS_SZ 133073 + +/* +** Methods for KVVfsFile +*/ +static int kvvfsClose(sqlite3_file*); +static int kvvfsReadDb(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst); +static int kvvfsReadJrnl(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst); +static int kvvfsWriteDb(sqlite3_file*,const void*,int iAmt, sqlite3_int64); +static int kvvfsWriteJrnl(sqlite3_file*,const void*,int iAmt, sqlite3_int64); +static int kvvfsTruncateDb(sqlite3_file*, sqlite3_int64 size); +static int kvvfsTruncateJrnl(sqlite3_file*, sqlite3_int64 size); +static int kvvfsSyncDb(sqlite3_file*, int flags); +static int kvvfsSyncJrnl(sqlite3_file*, int flags); +static int kvvfsFileSizeDb(sqlite3_file*, sqlite3_int64 *pSize); +static int kvvfsFileSizeJrnl(sqlite3_file*, sqlite3_int64 *pSize); +static int kvvfsLock(sqlite3_file*, int); +static int kvvfsUnlock(sqlite3_file*, int); +static int kvvfsCheckReservedLock(sqlite3_file*, int *pResOut); +static int kvvfsFileControlDb(sqlite3_file*, int op, void *pArg); +static int kvvfsFileControlJrnl(sqlite3_file*, int op, void *pArg); +static int kvvfsSectorSize(sqlite3_file*); +static int kvvfsDeviceCharacteristics(sqlite3_file*); + +/* +** Methods for sqlite3_vfs +*/ +static int kvvfsOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *); +static int kvvfsDelete(sqlite3_vfs*, const char *zName, int syncDir); +static int kvvfsAccess(sqlite3_vfs*, const char *zName, int flags, int *); +static int kvvfsFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut); +static void *kvvfsDlOpen(sqlite3_vfs*, const char *zFilename); +static int kvvfsRandomness(sqlite3_vfs*, int nByte, char *zOut); +static int kvvfsSleep(sqlite3_vfs*, int microseconds); +static int kvvfsCurrentTime(sqlite3_vfs*, double*); +static int kvvfsCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*); + +static sqlite3_vfs sqlite3OsKvvfsObject = { + 1, /* iVersion */ + sizeof(KVVfsFile), /* szOsFile */ + 1024, /* mxPathname */ + 0, /* pNext */ + "kvvfs", /* zName */ + 0, /* pAppData */ + kvvfsOpen, /* xOpen */ + kvvfsDelete, /* xDelete */ + kvvfsAccess, /* xAccess */ + kvvfsFullPathname, /* xFullPathname */ + kvvfsDlOpen, /* xDlOpen */ + 0, /* xDlError */ + 0, /* xDlSym */ + 0, /* xDlClose */ + kvvfsRandomness, /* xRandomness */ + kvvfsSleep, /* xSleep */ + kvvfsCurrentTime, /* xCurrentTime */ + 0, /* xGetLastError */ + kvvfsCurrentTimeInt64 /* xCurrentTimeInt64 */ +}; + +/* Methods for sqlite3_file objects referencing a database file +*/ +static sqlite3_io_methods kvvfs_db_io_methods = { + 1, /* iVersion */ + kvvfsClose, /* xClose */ + kvvfsReadDb, /* xRead */ + kvvfsWriteDb, /* xWrite */ + kvvfsTruncateDb, /* xTruncate */ + kvvfsSyncDb, /* xSync */ + kvvfsFileSizeDb, /* xFileSize */ + kvvfsLock, /* xLock */ + kvvfsUnlock, /* xUnlock */ + kvvfsCheckReservedLock, /* xCheckReservedLock */ + kvvfsFileControlDb, /* xFileControl */ + kvvfsSectorSize, /* xSectorSize */ + kvvfsDeviceCharacteristics, /* xDeviceCharacteristics */ + 0, /* xShmMap */ + 0, /* xShmLock */ + 0, /* xShmBarrier */ + 0, /* xShmUnmap */ + 0, /* xFetch */ + 0 /* xUnfetch */ +}; + +/* Methods for sqlite3_file objects referencing a rollback journal +*/ +static sqlite3_io_methods kvvfs_jrnl_io_methods = { + 1, /* iVersion */ + kvvfsClose, /* xClose */ + kvvfsReadJrnl, /* xRead */ + kvvfsWriteJrnl, /* xWrite */ + kvvfsTruncateJrnl, /* xTruncate */ + kvvfsSyncJrnl, /* xSync */ + kvvfsFileSizeJrnl, /* xFileSize */ + kvvfsLock, /* xLock */ + kvvfsUnlock, /* xUnlock */ + kvvfsCheckReservedLock, /* xCheckReservedLock */ + kvvfsFileControlJrnl, /* xFileControl */ + kvvfsSectorSize, /* xSectorSize */ + kvvfsDeviceCharacteristics, /* xDeviceCharacteristics */ + 0, /* xShmMap */ + 0, /* xShmLock */ + 0, /* xShmBarrier */ + 0, /* xShmUnmap */ + 0, /* xFetch */ + 0 /* xUnfetch */ +}; + +/****** Storage subsystem **************************************************/ +#include +#include +#include + +/* Forward declarations for the low-level storage engine +*/ +static int kvstorageWrite(const char*, const char *zKey, const char *zData); +static int kvstorageDelete(const char*, const char *zKey); +static int kvstorageRead(const char*, const char *zKey, char *zBuf, int nBuf); +#define KVSTORAGE_KEY_SZ 32 + +/* Expand the key name with an appropriate prefix and put the result +** in zKeyOut[]. The zKeyOut[] buffer is assumed to hold at least +** KVSTORAGE_KEY_SZ bytes. +*/ +static void kvstorageMakeKey( + const char *zClass, + const char *zKeyIn, + char *zKeyOut +){ + sqlite3_snprintf(KVSTORAGE_KEY_SZ, zKeyOut, "kvvfs-%s-%s", zClass, zKeyIn); +} + +/* Write content into a key. zClass is the particular namespace of the +** underlying key/value store to use - either "local" or "session". +** +** Both zKey and zData are zero-terminated pure text strings. +** +** Return the number of errors. +*/ +static int kvstorageWrite( + const char *zClass, + const char *zKey, + const char *zData +){ + FILE *fd; + char zXKey[KVSTORAGE_KEY_SZ]; + kvstorageMakeKey(zClass, zKey, zXKey); + fd = fopen(zXKey, "wb"); + if( fd ){ + SQLITE_KV_TRACE(("KVVFS-WRITE %-15s (%d) %.50s%s\n", zXKey, + (int)strlen(zData), zData, + strlen(zData)>50 ? "..." : "")); + fputs(zData, fd); + fclose(fd); + return 0; + }else{ + return 1; + } +} + +/* Delete a key (with its corresponding data) from the key/value +** namespace given by zClass. If the key does not previously exist, +** this routine is a no-op. +*/ +static int kvstorageDelete(const char *zClass, const char *zKey){ + char zXKey[KVSTORAGE_KEY_SZ]; + kvstorageMakeKey(zClass, zKey, zXKey); + unlink(zXKey); + SQLITE_KV_TRACE(("KVVFS-DELETE %-15s\n", zXKey)); + return 0; +} + +/* Read the value associated with a zKey from the key/value namespace given +** by zClass and put the text data associated with that key in the first +** nBuf bytes of zBuf[]. The value might be truncated if zBuf is not large +** enough to hold it all. The value put into zBuf must always be zero +** terminated, even if it gets truncated because nBuf is not large enough. +** +** Return the total number of bytes in the data, without truncation, and +** not counting the final zero terminator. Return -1 if the key does +** not exist or its key cannot be read. +** +** If nBuf<=0 then this routine simply returns the size of the data +** without actually reading it. Similarly, if nBuf==1 then it +** zero-terminates zBuf at zBuf[0] and returns the size of the data +** without reading it. +*/ +static int kvstorageRead( + const char *zClass, + const char *zKey, + char *zBuf, + int nBuf +){ + FILE *fd; + struct stat buf; + char zXKey[KVSTORAGE_KEY_SZ]; + kvstorageMakeKey(zClass, zKey, zXKey); + if( access(zXKey, R_OK)!=0 + || stat(zXKey, &buf)!=0 + || !S_ISREG(buf.st_mode) + ){ + SQLITE_KV_TRACE(("KVVFS-READ %-15s (-1)\n", zXKey)); + return -1; + } + if( nBuf<=0 ){ + return (int)buf.st_size; + }else if( nBuf==1 ){ + zBuf[0] = 0; + SQLITE_KV_TRACE(("KVVFS-READ %-15s (%d)\n", zXKey, + (int)buf.st_size)); + return (int)buf.st_size; + } + if( nBuf > buf.st_size + 1 ){ + nBuf = buf.st_size + 1; + } + fd = fopen(zXKey, "rb"); + if( fd==0 ){ + SQLITE_KV_TRACE(("KVVFS-READ %-15s (-1)\n", zXKey)); + return -1; + }else{ + sqlite3_int64 n = fread(zBuf, 1, nBuf-1, fd); + fclose(fd); + zBuf[n] = 0; + SQLITE_KV_TRACE(("KVVFS-READ %-15s (%lld) %.50s%s\n", zXKey, + n, zBuf, n>50 ? "..." : "")); + return (int)n; + } +} + +/* +** An internal level of indirection which enables us to replace the +** kvvfs i/o methods with JavaScript implementations in WASM builds. +** Maintenance reminder: if this struct changes in any way, the JSON +** rendering of its structure must be updated in +** sqlite3-wasm.c:sqlite3__wasm_enum_json(). There are no binary +** compatibility concerns, so it does not need an iVersion +** member. +*/ +typedef struct sqlite3_kvvfs_methods sqlite3_kvvfs_methods; +struct sqlite3_kvvfs_methods { + int (*xRead)(const char *zClass, const char *zKey, char *zBuf, int nBuf); + int (*xWrite)(const char *zClass, const char *zKey, const char *zData); + int (*xDelete)(const char *zClass, const char *zKey); + const int nKeySize; +}; + +/* +** This object holds the kvvfs I/O methods which may be swapped out +** for JavaScript-side implementations in WASM builds. In such builds +** it cannot be const, but in native builds it should be so that +** the compiler can hopefully optimize this level of indirection out. +** That said, kvvfs is intended primarily for use in WASM builds. +** +** This is not explicitly flagged as static because the amalgamation +** build will tag it with SQLITE_PRIVATE. +*/ +#ifndef SQLITE_WASM +const +#endif +sqlite3_kvvfs_methods sqlite3KvvfsMethods = { +kvstorageRead, +kvstorageWrite, +kvstorageDelete, +KVSTORAGE_KEY_SZ +}; + +/****** Utility subroutines ************************************************/ + +/* +** Encode binary into the text encoded used to persist on disk. +** The output text is stored in aOut[], which must be at least +** nData+1 bytes in length. +** +** Return the actual length of the encoded text, not counting the +** zero terminator at the end. +** +** Encoding format +** --------------- +** +** * Non-zero bytes are encoded as upper-case hexadecimal +** +** * A sequence of one or more zero-bytes that are not at the +** beginning of the buffer are encoded as a little-endian +** base-26 number using a..z. "a" means 0. "b" means 1, +** "z" means 25. "ab" means 26. "ac" means 52. And so forth. +** +** * Because there is no overlap between the encoding characters +** of hexadecimal and base-26 numbers, it is always clear where +** one stops and the next begins. +*/ +static int kvvfsEncode(const char *aData, int nData, char *aOut){ + int i, j; + const unsigned char *a = (const unsigned char*)aData; + for(i=j=0; i>4]; + aOut[j++] = "0123456789ABCDEF"[c&0xf]; + }else{ + /* A sequence of 1 or more zeros is stored as a little-endian + ** base-26 number using a..z as the digits. So one zero is "b". + ** Two zeros is "c". 25 zeros is "z", 26 zeros is "ab", 27 is "bb", + ** and so forth. + */ + int k; + for(k=1; i+k0 ){ + aOut[j++] = 'a'+(k%26); + k /= 26; + } + } + } + aOut[j] = 0; + return j; +} + +static const signed char kvvfsHexValue[256] = { + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, + -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 +}; + +/* +** Decode the text encoding back to binary. The binary content is +** written into pOut, which must be at least nOut bytes in length. +** +** The return value is the number of bytes actually written into aOut[]. +*/ +static int kvvfsDecode(const char *a, char *aOut, int nOut){ + int i, j; + int c; + const unsigned char *aIn = (const unsigned char*)a; + i = 0; + j = 0; + while( 1 ){ + c = kvvfsHexValue[aIn[i]]; + if( c<0 ){ + int n = 0; + int mult = 1; + c = aIn[i]; + if( c==0 ) break; + while( c>='a' && c<='z' ){ + n += (c - 'a')*mult; + mult *= 26; + c = aIn[++i]; + } + if( j+n>nOut ) return -1; + memset(&aOut[j], 0, n); + j += n; + if( c==0 || mult==1 ) break; /* progress stalled if mult==1 */ + }else{ + aOut[j] = c<<4; + c = kvvfsHexValue[aIn[++i]]; + if( c<0 ) break; + aOut[j++] += c; + i++; + } + } + return j; +} + +/* +** Decode a complete journal file. Allocate space in pFile->aJrnl +** and store the decoding there. Or leave pFile->aJrnl set to NULL +** if an error is encountered. +** +** The first few characters of the text encoding will be a little-endian +** base-26 number (digits a..z) that is the total number of bytes +** in the decoded journal file image. This base-26 number is followed +** by a single space, then the encoding of the journal. The space +** separator is required to act as a terminator for the base-26 number. +*/ +static void kvvfsDecodeJournal( + KVVfsFile *pFile, /* Store decoding in pFile->aJrnl */ + const char *zTxt, /* Text encoding. Zero-terminated */ + int nTxt /* Bytes in zTxt, excluding zero terminator */ +){ + unsigned int n = 0; + int c, i, mult; + i = 0; + mult = 1; + while( (c = zTxt[i++])>='a' && c<='z' ){ + n += (zTxt[i] - 'a')*mult; + mult *= 26; + } + sqlite3_free(pFile->aJrnl); + pFile->aJrnl = sqlite3_malloc64( n ); + if( pFile->aJrnl==0 ){ + pFile->nJrnl = 0; + return; + } + pFile->nJrnl = n; + n = kvvfsDecode(zTxt+i, pFile->aJrnl, pFile->nJrnl); + if( nnJrnl ){ + sqlite3_free(pFile->aJrnl); + pFile->aJrnl = 0; + pFile->nJrnl = 0; + } +} + +/* +** Read or write the "sz" element, containing the database file size. +*/ +static sqlite3_int64 kvvfsReadFileSize(KVVfsFile *pFile){ + char zData[50]; + zData[0] = 0; + sqlite3KvvfsMethods.xRead(pFile->zClass, "sz", zData, sizeof(zData)-1); + return strtoll(zData, 0, 0); +} +static int kvvfsWriteFileSize(KVVfsFile *pFile, sqlite3_int64 sz){ + char zData[50]; + sqlite3_snprintf(sizeof(zData), zData, "%lld", sz); + return sqlite3KvvfsMethods.xWrite(pFile->zClass, "sz", zData); +} + +/****** sqlite3_io_methods methods ******************************************/ + +/* +** Close an kvvfs-file. +*/ +static int kvvfsClose(sqlite3_file *pProtoFile){ + KVVfsFile *pFile = (KVVfsFile *)pProtoFile; + + SQLITE_KV_LOG(("xClose %s %s\n", pFile->zClass, + pFile->isJournal ? "journal" : "db")); + sqlite3_free(pFile->aJrnl); + sqlite3_free(pFile->aData); + return SQLITE_OK; +} + +/* +** Read from the -journal file. +*/ +static int kvvfsReadJrnl( + sqlite3_file *pProtoFile, + void *zBuf, + int iAmt, + sqlite_int64 iOfst +){ + KVVfsFile *pFile = (KVVfsFile*)pProtoFile; + assert( pFile->isJournal ); + SQLITE_KV_LOG(("xRead('%s-journal',%d,%lld)\n", pFile->zClass, iAmt, iOfst)); + if( pFile->aJrnl==0 ){ + int szTxt = kvstorageRead(pFile->zClass, "jrnl", 0, 0); + char *aTxt; + if( szTxt<=4 ){ + return SQLITE_IOERR; + } + aTxt = sqlite3_malloc64( szTxt+1 ); + if( aTxt==0 ) return SQLITE_NOMEM; + kvstorageRead(pFile->zClass, "jrnl", aTxt, szTxt+1); + kvvfsDecodeJournal(pFile, aTxt, szTxt); + sqlite3_free(aTxt); + if( pFile->aJrnl==0 ) return SQLITE_IOERR; + } + if( iOfst+iAmt>pFile->nJrnl ){ + return SQLITE_IOERR_SHORT_READ; + } + memcpy(zBuf, pFile->aJrnl+iOfst, iAmt); + return SQLITE_OK; +} + +/* +** Read from the database file. +*/ +static int kvvfsReadDb( + sqlite3_file *pProtoFile, + void *zBuf, + int iAmt, + sqlite_int64 iOfst +){ + KVVfsFile *pFile = (KVVfsFile*)pProtoFile; + unsigned int pgno; + int got, n; + char zKey[30]; + char *aData = pFile->aData; + assert( iOfst>=0 ); + assert( iAmt>=0 ); + SQLITE_KV_LOG(("xRead('%s-db',%d,%lld)\n", pFile->zClass, iAmt, iOfst)); + if( iOfst+iAmt>=512 ){ + if( (iOfst % iAmt)!=0 ){ + return SQLITE_IOERR_READ; + } + if( (iAmt & (iAmt-1))!=0 || iAmt<512 || iAmt>65536 ){ + return SQLITE_IOERR_READ; + } + pFile->szPage = iAmt; + pgno = 1 + iOfst/iAmt; + }else{ + pgno = 1; + } + sqlite3_snprintf(sizeof(zKey), zKey, "%u", pgno); + got = sqlite3KvvfsMethods.xRead(pFile->zClass, zKey, + aData, SQLITE_KVOS_SZ-1); + if( got<0 ){ + n = 0; + }else{ + aData[got] = 0; + if( iOfst+iAmt<512 ){ + int k = iOfst+iAmt; + aData[k*2] = 0; + n = kvvfsDecode(aData, &aData[2000], SQLITE_KVOS_SZ-2000); + if( n>=iOfst+iAmt ){ + memcpy(zBuf, &aData[2000+iOfst], iAmt); + n = iAmt; + }else{ + n = 0; + } + }else{ + n = kvvfsDecode(aData, zBuf, iAmt); + } + } + if( nzClass, iAmt, iOfst)); + if( iEnd>=0x10000000 ) return SQLITE_FULL; + if( pFile->aJrnl==0 || pFile->nJrnlaJrnl, iEnd); + if( aNew==0 ){ + return SQLITE_IOERR_NOMEM; + } + pFile->aJrnl = aNew; + if( pFile->nJrnlaJrnl+pFile->nJrnl, 0, iOfst-pFile->nJrnl); + } + pFile->nJrnl = iEnd; + } + memcpy(pFile->aJrnl+iOfst, zBuf, iAmt); + return SQLITE_OK; +} + +/* +** Write into the database file. +*/ +static int kvvfsWriteDb( + sqlite3_file *pProtoFile, + const void *zBuf, + int iAmt, + sqlite_int64 iOfst +){ + KVVfsFile *pFile = (KVVfsFile*)pProtoFile; + unsigned int pgno; + char zKey[30]; + char *aData = pFile->aData; + SQLITE_KV_LOG(("xWrite('%s-db',%d,%lld)\n", pFile->zClass, iAmt, iOfst)); + assert( iAmt>=512 && iAmt<=65536 ); + assert( (iAmt & (iAmt-1))==0 ); + assert( pFile->szPage<0 || pFile->szPage==iAmt ); + pFile->szPage = iAmt; + pgno = 1 + iOfst/iAmt; + sqlite3_snprintf(sizeof(zKey), zKey, "%u", pgno); + kvvfsEncode(zBuf, iAmt, aData); + if( sqlite3KvvfsMethods.xWrite(pFile->zClass, zKey, aData) ){ + return SQLITE_IOERR; + } + if( iOfst+iAmt > pFile->szDb ){ + pFile->szDb = iOfst + iAmt; + } + return SQLITE_OK; +} + +/* +** Truncate an kvvfs-file. +*/ +static int kvvfsTruncateJrnl(sqlite3_file *pProtoFile, sqlite_int64 size){ + KVVfsFile *pFile = (KVVfsFile *)pProtoFile; + SQLITE_KV_LOG(("xTruncate('%s-journal',%lld)\n", pFile->zClass, size)); + assert( size==0 ); + sqlite3KvvfsMethods.xDelete(pFile->zClass, "jrnl"); + sqlite3_free(pFile->aJrnl); + pFile->aJrnl = 0; + pFile->nJrnl = 0; + return SQLITE_OK; +} +static int kvvfsTruncateDb(sqlite3_file *pProtoFile, sqlite_int64 size){ + KVVfsFile *pFile = (KVVfsFile *)pProtoFile; + if( pFile->szDb>size + && pFile->szPage>0 + && (size % pFile->szPage)==0 + ){ + char zKey[50]; + unsigned int pgno, pgnoMax; + SQLITE_KV_LOG(("xTruncate('%s-db',%lld)\n", pFile->zClass, size)); + pgno = 1 + size/pFile->szPage; + pgnoMax = 2 + pFile->szDb/pFile->szPage; + while( pgno<=pgnoMax ){ + sqlite3_snprintf(sizeof(zKey), zKey, "%u", pgno); + sqlite3KvvfsMethods.xDelete(pFile->zClass, zKey); + pgno++; + } + pFile->szDb = size; + return kvvfsWriteFileSize(pFile, size) ? SQLITE_IOERR : SQLITE_OK; + } + return SQLITE_IOERR; +} + +/* +** Sync an kvvfs-file. +*/ +static int kvvfsSyncJrnl(sqlite3_file *pProtoFile, int flags){ + int i, n; + KVVfsFile *pFile = (KVVfsFile *)pProtoFile; + char *zOut; + SQLITE_KV_LOG(("xSync('%s-journal')\n", pFile->zClass)); + if( pFile->nJrnl<=0 ){ + return kvvfsTruncateJrnl(pProtoFile, 0); + } + zOut = sqlite3_malloc64( pFile->nJrnl*2 + 50 ); + if( zOut==0 ){ + return SQLITE_IOERR_NOMEM; + } + n = pFile->nJrnl; + i = 0; + do{ + zOut[i++] = 'a' + (n%26); + n /= 26; + }while( n>0 ); + zOut[i++] = ' '; + kvvfsEncode(pFile->aJrnl, pFile->nJrnl, &zOut[i]); + i = sqlite3KvvfsMethods.xWrite(pFile->zClass, "jrnl", zOut); + sqlite3_free(zOut); + return i ? SQLITE_IOERR : SQLITE_OK; +} +static int kvvfsSyncDb(sqlite3_file *pProtoFile, int flags){ + return SQLITE_OK; +} + +/* +** Return the current file-size of an kvvfs-file. +*/ +static int kvvfsFileSizeJrnl(sqlite3_file *pProtoFile, sqlite_int64 *pSize){ + KVVfsFile *pFile = (KVVfsFile *)pProtoFile; + SQLITE_KV_LOG(("xFileSize('%s-journal')\n", pFile->zClass)); + *pSize = pFile->nJrnl; + return SQLITE_OK; +} +static int kvvfsFileSizeDb(sqlite3_file *pProtoFile, sqlite_int64 *pSize){ + KVVfsFile *pFile = (KVVfsFile *)pProtoFile; + SQLITE_KV_LOG(("xFileSize('%s-db')\n", pFile->zClass)); + if( pFile->szDb>=0 ){ + *pSize = pFile->szDb; + }else{ + *pSize = kvvfsReadFileSize(pFile); + } + return SQLITE_OK; +} + +/* +** Lock an kvvfs-file. +*/ +static int kvvfsLock(sqlite3_file *pProtoFile, int eLock){ + KVVfsFile *pFile = (KVVfsFile *)pProtoFile; + assert( !pFile->isJournal ); + SQLITE_KV_LOG(("xLock(%s,%d)\n", pFile->zClass, eLock)); + + if( eLock!=SQLITE_LOCK_NONE ){ + pFile->szDb = kvvfsReadFileSize(pFile); + } + return SQLITE_OK; +} + +/* +** Unlock an kvvfs-file. +*/ +static int kvvfsUnlock(sqlite3_file *pProtoFile, int eLock){ + KVVfsFile *pFile = (KVVfsFile *)pProtoFile; + assert( !pFile->isJournal ); + SQLITE_KV_LOG(("xUnlock(%s,%d)\n", pFile->zClass, eLock)); + if( eLock==SQLITE_LOCK_NONE ){ + pFile->szDb = -1; + } + return SQLITE_OK; +} + +/* +** Check if another file-handle holds a RESERVED lock on an kvvfs-file. +*/ +static int kvvfsCheckReservedLock(sqlite3_file *pProtoFile, int *pResOut){ + SQLITE_KV_LOG(("xCheckReservedLock\n")); + *pResOut = 0; + return SQLITE_OK; +} + +/* +** File control method. For custom operations on an kvvfs-file. +*/ +static int kvvfsFileControlJrnl(sqlite3_file *pProtoFile, int op, void *pArg){ + SQLITE_KV_LOG(("xFileControl(%d) on journal\n", op)); + return SQLITE_NOTFOUND; +} +static int kvvfsFileControlDb(sqlite3_file *pProtoFile, int op, void *pArg){ + SQLITE_KV_LOG(("xFileControl(%d) on database\n", op)); + if( op==SQLITE_FCNTL_SYNC ){ + KVVfsFile *pFile = (KVVfsFile *)pProtoFile; + int rc = SQLITE_OK; + SQLITE_KV_LOG(("xSync('%s-db')\n", pFile->zClass)); + if( pFile->szDb>0 && 0!=kvvfsWriteFileSize(pFile, pFile->szDb) ){ + rc = SQLITE_IOERR; + } + return rc; + } + return SQLITE_NOTFOUND; +} + +/* +** Return the sector-size in bytes for an kvvfs-file. +*/ +static int kvvfsSectorSize(sqlite3_file *pFile){ + return 512; +} + +/* +** Return the device characteristic flags supported by an kvvfs-file. +*/ +static int kvvfsDeviceCharacteristics(sqlite3_file *pProtoFile){ + return 0; +} + +/****** sqlite3_vfs methods *************************************************/ + +/* +** Open an kvvfs file handle. +*/ +static int kvvfsOpen( + sqlite3_vfs *pProtoVfs, + const char *zName, + sqlite3_file *pProtoFile, + int flags, + int *pOutFlags +){ + KVVfsFile *pFile = (KVVfsFile*)pProtoFile; + if( zName==0 ) zName = ""; + SQLITE_KV_LOG(("xOpen(\"%s\")\n", zName)); + if( strcmp(zName, "local")==0 + || strcmp(zName, "session")==0 + ){ + pFile->isJournal = 0; + pFile->base.pMethods = &kvvfs_db_io_methods; + }else + if( strcmp(zName, "local-journal")==0 + || strcmp(zName, "session-journal")==0 + ){ + pFile->isJournal = 1; + pFile->base.pMethods = &kvvfs_jrnl_io_methods; + }else{ + return SQLITE_CANTOPEN; + } + if( zName[0]=='s' ){ + pFile->zClass = "session"; + }else{ + pFile->zClass = "local"; + } + pFile->aData = sqlite3_malloc64(SQLITE_KVOS_SZ); + if( pFile->aData==0 ){ + return SQLITE_NOMEM; + } + pFile->aJrnl = 0; + pFile->nJrnl = 0; + pFile->szPage = -1; + pFile->szDb = -1; + return SQLITE_OK; +} + +/* +** Delete the file located at zPath. If the dirSync argument is true, +** ensure the file-system modifications are synced to disk before +** returning. +*/ +static int kvvfsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){ + if( strcmp(zPath, "local-journal")==0 ){ + sqlite3KvvfsMethods.xDelete("local", "jrnl"); + }else + if( strcmp(zPath, "session-journal")==0 ){ + sqlite3KvvfsMethods.xDelete("session", "jrnl"); + } + return SQLITE_OK; +} + +/* +** Test for access permissions. Return true if the requested permission +** is available, or false otherwise. +*/ +static int kvvfsAccess( + sqlite3_vfs *pProtoVfs, + const char *zPath, + int flags, + int *pResOut +){ + SQLITE_KV_LOG(("xAccess(\"%s\")\n", zPath)); + if( strcmp(zPath, "local-journal")==0 ){ + *pResOut = sqlite3KvvfsMethods.xRead("local", "jrnl", 0, 0)>0; + }else + if( strcmp(zPath, "session-journal")==0 ){ + *pResOut = sqlite3KvvfsMethods.xRead("session", "jrnl", 0, 0)>0; + }else + if( strcmp(zPath, "local")==0 ){ + *pResOut = sqlite3KvvfsMethods.xRead("local", "sz", 0, 0)>0; + }else + if( strcmp(zPath, "session")==0 ){ + *pResOut = sqlite3KvvfsMethods.xRead("session", "sz", 0, 0)>0; + }else + { + *pResOut = 0; + } + SQLITE_KV_LOG(("xAccess returns %d\n",*pResOut)); + return SQLITE_OK; +} + +/* +** Populate buffer zOut with the full canonical pathname corresponding +** to the pathname in zPath. zOut is guaranteed to point to a buffer +** of at least (INST_MAX_PATHNAME+1) bytes. +*/ +static int kvvfsFullPathname( + sqlite3_vfs *pVfs, + const char *zPath, + int nOut, + char *zOut +){ + size_t nPath; +#ifdef SQLITE_OS_KV_ALWAYS_LOCAL + zPath = "local"; +#endif + nPath = strlen(zPath); + SQLITE_KV_LOG(("xFullPathname(\"%s\")\n", zPath)); + if( nOut +static int kvvfsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){ + static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000; + struct timeval sNow; + (void)gettimeofday(&sNow, 0); /* Cannot fail given valid arguments */ + *pTimeOut = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000; + return SQLITE_OK; +} +#endif /* SQLITE_OS_KV || SQLITE_OS_UNIX */ + +#if SQLITE_OS_KV +/* +** This routine is called initialize the KV-vfs as the default VFS. +*/ +int sqlite3_os_init(void){ + return sqlite3_vfs_register(&sqlite3OsKvvfsObject, 1); +} +int sqlite3_os_end(void){ + return SQLITE_OK; +} +#endif /* SQLITE_OS_KV */ + +#if SQLITE_OS_UNIX && defined(SQLITE_OS_KV_OPTIONAL) +int sqlite3KvvfsInit(void){ + return sqlite3_vfs_register(&sqlite3OsKvvfsObject, 0); +} +#endif diff --git a/src/os_setup.h b/src/os_setup.h index 68de1446ed..2aa4b26b33 100644 --- a/src/os_setup.h +++ b/src/os_setup.h @@ -13,45 +13,79 @@ ** This file contains pre-processor directives related to operating system ** detection and/or setup. */ -#ifndef _OS_SETUP_H_ -#define _OS_SETUP_H_ +#ifndef SQLITE_OS_SETUP_H +#define SQLITE_OS_SETUP_H /* ** Figure out if we are dealing with Unix, Windows, or some other operating ** system. ** -** After the following block of preprocess macros, all of SQLITE_OS_UNIX, -** SQLITE_OS_WIN, and SQLITE_OS_OTHER will defined to either 1 or 0. One of -** the three will be 1. The other two will be 0. +** After the following block of preprocess macros, all of +** +** SQLITE_OS_KV +** SQLITE_OS_OTHER +** SQLITE_OS_UNIX +** SQLITE_OS_WIN +** +** will defined to either 1 or 0. One of them will be 1. The others will be 0. +** If none of the macros are initially defined, then select either +** SQLITE_OS_UNIX or SQLITE_OS_WIN depending on the target platform. +** +** If SQLITE_OS_OTHER=1 is specified at compile-time, then the application +** must provide its own VFS implementation together with sqlite3_os_init() +** and sqlite3_os_end() routines. */ -#if defined(SQLITE_OS_OTHER) -# if SQLITE_OS_OTHER==1 -# undef SQLITE_OS_UNIX +#if SQLITE_OS_KV+1<=1 && SQLITE_OS_OTHER+1<=1 && \ + SQLITE_OS_WIN+1<=1 && SQLITE_OS_UNIX+1<=1 +# if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || \ + defined(__MINGW32__) || defined(__BORLANDC__) +# define SQLITE_OS_WIN 1 # define SQLITE_OS_UNIX 0 -# undef SQLITE_OS_WIN -# define SQLITE_OS_WIN 0 # else -# undef SQLITE_OS_OTHER +# define SQLITE_OS_WIN 0 +# define SQLITE_OS_UNIX 1 # endif #endif -#if !defined(SQLITE_OS_UNIX) && !defined(SQLITE_OS_OTHER) +#if SQLITE_OS_OTHER+1>1 +# undef SQLITE_OS_KV +# define SQLITE_OS_KV 0 +# undef SQLITE_OS_UNIX +# define SQLITE_OS_UNIX 0 +# undef SQLITE_OS_WIN +# define SQLITE_OS_WIN 0 +#endif +#if SQLITE_OS_KV+1>1 +# undef SQLITE_OS_OTHER # define SQLITE_OS_OTHER 0 -# ifndef SQLITE_OS_WIN -# if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || \ - defined(__MINGW32__) || defined(__BORLANDC__) -# define SQLITE_OS_WIN 1 -# define SQLITE_OS_UNIX 0 -# else -# define SQLITE_OS_WIN 0 -# define SQLITE_OS_UNIX 1 -# endif -# else -# define SQLITE_OS_UNIX 0 -# endif -#else -# ifndef SQLITE_OS_WIN -# define SQLITE_OS_WIN 0 -# endif +# undef SQLITE_OS_UNIX +# define SQLITE_OS_UNIX 0 +# undef SQLITE_OS_WIN +# define SQLITE_OS_WIN 0 +# define SQLITE_OMIT_LOAD_EXTENSION 1 +# define SQLITE_OMIT_WAL 1 +# define SQLITE_OMIT_DEPRECATED 1 +# undef SQLITE_TEMP_STORE +# define SQLITE_TEMP_STORE 3 /* Always use memory for temporary storage */ +# define SQLITE_DQS 0 +# define SQLITE_OMIT_SHARED_CACHE 1 +# define SQLITE_OMIT_AUTOINIT 1 +#endif +#if SQLITE_OS_UNIX+1>1 +# undef SQLITE_OS_KV +# define SQLITE_OS_KV 0 +# undef SQLITE_OS_OTHER +# define SQLITE_OS_OTHER 0 +# undef SQLITE_OS_WIN +# define SQLITE_OS_WIN 0 +#endif +#if SQLITE_OS_WIN+1>1 +# undef SQLITE_OS_KV +# define SQLITE_OS_KV 0 +# undef SQLITE_OS_OTHER +# define SQLITE_OS_OTHER 0 +# undef SQLITE_OS_UNIX +# define SQLITE_OS_UNIX 0 #endif -#endif /* _OS_SETUP_H_ */ + +#endif /* SQLITE_OS_SETUP_H */ diff --git a/src/os_unix.c b/src/os_unix.c index fe1fc6af19..d73d899241 100644 --- a/src/os_unix.c +++ b/src/os_unix.c @@ -22,7 +22,7 @@ ** This source file is organized into divisions where the logic for various ** subfunctions is contained within the appropriate division. PLEASE ** KEEP THE STRUCTURE OF THIS FILE INTACT. New code should be placed -** in the correct division and should be clearly labeled. +** in the correct division and should be clearly labelled. ** ** The layout of divisions is as follows: ** @@ -61,7 +61,7 @@ ** Styles 4, 5, and 7 are only available of SQLITE_ENABLE_LOCKING_STYLE ** is defined to 1. The SQLITE_ENABLE_LOCKING_STYLE also enables automatic ** selection of the appropriate locking style based on the filesystem -** where the database is located. +** where the database is located. */ #if !defined(SQLITE_ENABLE_LOCKING_STYLE) # if defined(__APPLE__) @@ -71,17 +71,32 @@ # endif #endif +/* Use pread() and pwrite() if they are available */ +#if defined(__APPLE__) || defined(__linux__) +# define HAVE_PREAD 1 +# define HAVE_PWRITE 1 +#endif +#if defined(HAVE_PREAD64) && defined(HAVE_PWRITE64) +# undef USE_PREAD +# define USE_PREAD64 1 +#elif defined(HAVE_PREAD) && defined(HAVE_PWRITE) +# undef USE_PREAD64 +# define USE_PREAD 1 +#endif + /* ** standard include files. */ -#include -#include +#include /* amalgamator: keep */ +#include /* amalgamator: keep */ #include -#include +#include +#include /* amalgamator: keep */ #include -#include +#include /* amalgamator: keep */ #include -#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 +#if (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) \ + && !defined(SQLITE_WASI) # include #endif @@ -91,13 +106,30 @@ # include #endif /* SQLITE_ENABLE_LOCKING_STYLE */ -#if defined(__APPLE__) && ((__MAC_OS_X_VERSION_MIN_REQUIRED > 1050) || \ - (__IPHONE_OS_VERSION_MIN_REQUIRED > 2000)) -# if (!defined(TARGET_OS_EMBEDDED) || (TARGET_OS_EMBEDDED==0)) \ - && (!defined(TARGET_IPHONE_SIMULATOR) || (TARGET_IPHONE_SIMULATOR==0)) -# define HAVE_GETHOSTUUID 1 -# else -# warning "gethostuuid() is disabled." +/* +** Try to determine if gethostuuid() is available based on standard +** macros. This might sometimes compute the wrong value for some +** obscure platforms. For those cases, simply compile with one of +** the following: +** +** -DHAVE_GETHOSTUUID=0 +** -DHAVE_GETHOSTUUID=1 +** +** None if this matters except when building on Apple products with +** -DSQLITE_ENABLE_LOCKING_STYLE. +*/ +#ifndef HAVE_GETHOSTUUID +# define HAVE_GETHOSTUUID 0 +# if defined(__APPLE__) && ((__MAC_OS_X_VERSION_MIN_REQUIRED > 1050) || \ + (__IPHONE_OS_VERSION_MIN_REQUIRED > 2000)) +# if (!defined(TARGET_OS_EMBEDDED) || (TARGET_OS_EMBEDDED==0)) \ + && (!defined(TARGET_IPHONE_SIMULATOR) || (TARGET_IPHONE_SIMULATOR==0))\ + && (!defined(TARGET_OS_MACCATALYST) || (TARGET_OS_MACCATALYST==0)) +# undef HAVE_GETHOSTUUID +# define HAVE_GETHOSTUUID 1 +# else +# warning "gethostuuid() is disabled." +# endif # endif #endif @@ -122,12 +154,10 @@ #define SQLITE_FSFLAGS_IS_MSDOS 0x1 /* -** If we are to be thread-safe, include the pthreads header and define -** the SQLITE_UNIX_THREADS macro. +** If we are to be thread-safe, include the pthreads header. */ #if SQLITE_THREADSAFE # include -# define SQLITE_UNIX_THREADS 1 #endif /* @@ -154,12 +184,49 @@ */ #define SQLITE_MAX_SYMLINKS 100 +/* +** Remove and stub certain info for WASI (WebAssembly System +** Interface) builds. +*/ +#ifdef SQLITE_WASI +# undef HAVE_FCHMOD +# undef HAVE_FCHOWN +# undef HAVE_MREMAP +# define HAVE_MREMAP 0 +# ifndef SQLITE_DEFAULT_UNIX_VFS +# define SQLITE_DEFAULT_UNIX_VFS "unix-dotfile" + /* ^^^ should SQLITE_DEFAULT_UNIX_VFS be "unix-none"? */ +# endif +# ifndef F_RDLCK +# define F_RDLCK 0 +# define F_WRLCK 1 +# define F_UNLCK 2 +# if __LONG_MAX == 0x7fffffffL +# define F_GETLK 12 +# define F_SETLK 13 +# define F_SETLKW 14 +# else +# define F_GETLK 5 +# define F_SETLK 6 +# define F_SETLKW 7 +# endif +# endif +#else /* !SQLITE_WASI */ +# ifndef HAVE_FCHMOD +# define HAVE_FCHMOD 1 +# endif +#endif /* SQLITE_WASI */ + +#ifdef SQLITE_WASI +# define osGetpid(X) (pid_t)1 +#else /* Always cast the getpid() return type for compatibility with ** kernel modules in VxWorks. */ -#define osGetpid(X) (pid_t)getpid() +# define osGetpid(X) (pid_t)getpid() +#endif /* -** Only set the lastErrno if the error code is a real error and not +** Only set the lastErrno if the error code is a real error and not ** a normal expected return code of SQLITE_BUSY or SQLITE_OK */ #define IS_LOCK_ERROR(x) ((x != SQLITE_OK) && (x != SQLITE_BUSY)) @@ -196,7 +263,7 @@ struct unixFile { unsigned short int ctrlFlags; /* Behavioral bits. UNIXFILE_* flags */ int lastErrno; /* The unix errno from last I/O error */ void *lockingContext; /* Locking style specific state */ - UnixUnusedFd *pUnused; /* Pre-allocated UnixUnusedFd */ + UnixUnusedFd *pPreallocatedUnused; /* Pre-allocated UnixUnusedFd */ const char *zPath; /* Name of the file */ unixShm *pShm; /* Shared memory segment information */ int szChunk; /* Configured by FCNTL_CHUNK_SIZE */ @@ -207,16 +274,18 @@ struct unixFile { sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */ void *pMapRegion; /* Memory mapped region */ #endif -#ifdef __QNXNTO__ int sectorSize; /* Device sector size */ int deviceCharacteristics; /* Precomputed device characteristics */ -#endif #if SQLITE_ENABLE_LOCKING_STYLE int openFlags; /* The flags specified at open() */ #endif #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__) unsigned fsFlags; /* cached details from statfs() */ #endif +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT + unsigned iBusyTimeout; /* Wait this many millisec on locks */ + int bBlockOnConnect; /* True to block for SHARED locks */ +#endif #if OS_VXWORKS struct vxworksFileId *pId; /* Unique file ID */ #endif @@ -226,7 +295,7 @@ struct unixFile { ** whenever any part of the database changes. An assertion fault will ** occur if a file is updated without also updating the transaction ** counter. This test is made to avoid new problems similar to the - ** one described by ticket #3584. + ** one described by ticket #3584. */ unsigned char transCntrChng; /* True if the transaction counter changed */ unsigned char dbUpdate; /* True if any part of database file changed */ @@ -235,7 +304,7 @@ struct unixFile { #endif #ifdef SQLITE_TEST - /* In test mode, increase the size of this structure a bit so that + /* In test mode, increase the size of this structure a bit so that ** it is larger than the struct CrashFile defined in test6.c. */ char aPadding[32]; @@ -254,7 +323,7 @@ static pid_t randomnessPid = 0; #define UNIXFILE_EXCL 0x01 /* Connections from one process only */ #define UNIXFILE_RDONLY 0x02 /* Connection is read only */ #define UNIXFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */ -#ifndef SQLITE_DISABLE_DIRSYNC +#if !defined(SQLITE_DISABLE_DIRSYNC) && !defined(_AIX) # define UNIXFILE_DIRSYNC 0x08 /* Directory sync needed */ #else # define UNIXFILE_DIRSYNC 0x00 @@ -315,6 +384,20 @@ static pid_t randomnessPid = 0; # define lseek lseek64 #endif +#ifdef __linux__ +/* +** Linux-specific IOCTL magic numbers used for controlling F2FS +*/ +#define F2FS_IOCTL_MAGIC 0xf5 +#define F2FS_IOC_START_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 1) +#define F2FS_IOC_COMMIT_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 2) +#define F2FS_IOC_START_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 3) +#define F2FS_IOC_ABORT_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 5) +#define F2FS_IOC_GET_FEATURES _IOR(F2FS_IOCTL_MAGIC, 12, u32) +#define F2FS_FEATURE_ATOMIC_WRITE 0x0004 +#endif /* __linux__ */ + + /* ** Different Unix systems declare open() in different ways. Same use ** open(const char*,int,mode_t). Others use open(const char*,int,...). @@ -366,7 +449,7 @@ static struct unix_syscall { #ifdef __DJGPP__ { "fstat", 0, 0 }, #define osFstat(a,b,c) 0 -#else +#else { "fstat", (sqlite3_syscall_ptr)fstat, 0 }, #define osFstat ((int(*)(int,struct stat*))aSyscall[5].pCurrent) #endif @@ -392,7 +475,7 @@ static struct unix_syscall { #else { "pread64", (sqlite3_syscall_ptr)0, 0 }, #endif -#define osPread64 ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[10].pCurrent) +#define osPread64 ((ssize_t(*)(int,void*,size_t,off64_t))aSyscall[10].pCurrent) { "write", (sqlite3_syscall_ptr)write, 0 }, #define osWrite ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent) @@ -410,11 +493,16 @@ static struct unix_syscall { #else { "pwrite64", (sqlite3_syscall_ptr)0, 0 }, #endif -#define osPwrite64 ((ssize_t(*)(int,const void*,size_t,off_t))\ +#define osPwrite64 ((ssize_t(*)(int,const void*,size_t,off64_t))\ aSyscall[13].pCurrent) +#if defined(HAVE_FCHMOD) { "fchmod", (sqlite3_syscall_ptr)fchmod, 0 }, #define osFchmod ((int(*)(int,mode_t))aSyscall[14].pCurrent) +#else + { "fchmod", (sqlite3_syscall_ptr)0, 0 }, +#define osFchmod(FID,MODE) 0 +#endif #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE { "fallocate", (sqlite3_syscall_ptr)posix_fallocate, 0 }, @@ -442,22 +530,28 @@ static struct unix_syscall { #endif #define osFchown ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent) +#if defined(HAVE_FCHOWN) { "geteuid", (sqlite3_syscall_ptr)geteuid, 0 }, +#else + { "geteuid", (sqlite3_syscall_ptr)0, 0 }, +#endif #define osGeteuid ((uid_t(*)(void))aSyscall[21].pCurrent) -#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 +#if (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) \ + && !defined(SQLITE_WASI) { "mmap", (sqlite3_syscall_ptr)mmap, 0 }, #else { "mmap", (sqlite3_syscall_ptr)0, 0 }, #endif #define osMmap ((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[22].pCurrent) -#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 +#if (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) \ + && !defined(SQLITE_WASI) { "munmap", (sqlite3_syscall_ptr)munmap, 0 }, #else { "munmap", (sqlite3_syscall_ptr)0, 0 }, #endif -#define osMunmap ((void*(*)(void*,size_t))aSyscall[23].pCurrent) +#define osMunmap ((int(*)(void*,size_t))aSyscall[23].pCurrent) #if HAVE_MREMAP && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) { "mremap", (sqlite3_syscall_ptr)mremap, 0 }, @@ -487,9 +581,134 @@ static struct unix_syscall { #endif #define osLstat ((int(*)(const char*,struct stat*))aSyscall[27].pCurrent) +#if defined(__linux__) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE) +# ifdef __ANDROID__ + { "ioctl", (sqlite3_syscall_ptr)(int(*)(int, int, ...))ioctl, 0 }, +#define osIoctl ((int(*)(int,int,...))aSyscall[28].pCurrent) +# else + { "ioctl", (sqlite3_syscall_ptr)ioctl, 0 }, +#define osIoctl ((int(*)(int,unsigned long,...))aSyscall[28].pCurrent) +# endif +#else + { "ioctl", (sqlite3_syscall_ptr)0, 0 }, +#endif + }; /* End of the overrideable system calls */ +#if defined(SQLITE_DEBUG) || defined(SQLITE_ENABLE_FILESTAT) +/* +** Extract Posix Advisory Locking information about file description fd +** from the /proc/PID/fdinfo/FD pseudo-file. Fill the string buffer a[16] +** with characters to indicate which SQLite-relevant locks are held. +** a[16] will be a 15-character zero-terminated string with the following +** schema: +** +** AAA/B.DDD.DDDDD +** +** Each of character A-D will be "w" or "r" or "-" to indicate either a +** write-lock, a read-lock, or no-lock, respectively. The "." and "/" +** characters are delimiters intended to make the string more easily +** readable by humans. Here are the meaning of the specific letters: +** +** AAA -> The main database locks. PENDING_BYTE, RESERVED_BYTE, +** and SHARED_FIRST, respectively. +** +** B -> The deadman switch lock. Offset 128 of the -shm file. +** +** CCC -> WAL locks: WRITE, CKPT, RECOVER +** +** DDDDD -> WAL read-locks 0 through 5 +** +** Note that elements before the "/" apply to the main database file and +** elements after the "/" apply to the -shm file in WAL mode. +** +** Here is another way of thinking about the meaning of the result string: +** +** AAA/B.CCC.DDDDD +** ||| | ||| \___/ +** PENDING--'|| | ||| `----- READ 0-5 +** RESERVED--'| | ||`---- RECOVER +** SHARED ----' | |`----- CKPT +** DMS ------' `------ WRITE +** +** Return SQLITE_OK on success and SQLITE_ERROR_UNABLE if the /proc +** pseudo-filesystem is unavailable. +*/ +static int unixPosixAdvisoryLocks( + int fd, /* The file descriptor to analyze */ + char a[16] /* Write a text description of PALs here */ +){ + int in; + ssize_t n; + char *p, *pNext, *x; + char z[2000]; + + /* 1 */ + /* 012 4 678 01234 */ + memcpy(a, "---/-.---.-----", 16); + sqlite3_snprintf(sizeof(z), z, "/proc/%d/fdinfo/%d", getpid(), fd); + in = osOpen(z, O_RDONLY, 0); + if( in<0 ){ + return SQLITE_ERROR_UNABLE; + } + n = osRead(in, z, sizeof(z)-1); + osClose(in); + if( n<=0 ) return SQLITE_ERROR_UNABLE; + z[n] = 0; + + /* We are looking for lines that begin with "lock:\t". Examples: + ** + ** lock: 1: POSIX ADVISORY READ 494716 08:02:5277597 1073741826 1073742335 + ** lock: 1: POSIX ADVISORY WRITE 494716 08:02:5282282 120 120 + ** lock: 2: POSIX ADVISORY READ 494716 08:02:5282282 123 123 + ** lock: 3: POSIX ADVISORY READ 494716 08:02:5282282 128 128 + */ + pNext = strstr(z, "lock:\t"); + while( pNext ){ + char cType = 0; + sqlite3_int64 iFirst, iLast; + p = pNext+6; + pNext = strstr(p, "lock:\t"); + if( pNext ) pNext[-1] = 0; + if( (x = strstr(p, " READ "))!=0 ){ + cType = 'r'; + x += 6; + }else if( (x = strstr(p, " WRITE "))!=0 ){ + cType = 'w'; + x += 7; + }else{ + continue; + } + x = strrchr(x, ' '); + if( x==0 ) continue; + iLast = strtoll(x+1, 0, 10); + *x = 0; + x = strrchr(p, ' '); + if( x==0 ) continue; + iFirst = strtoll(x+1, 0, 10); + if( iLast>=PENDING_BYTE ){ + if( iFirst<=PENDING_BYTE && iLast>=PENDING_BYTE ) a[0] = cType; + if( iFirst<=PENDING_BYTE+1 && iLast>=PENDING_BYTE+1 ) a[1] = cType; + if( iFirst<=PENDING_BYTE+2 && iLast>=PENDING_BYTE+510 ) a[2] = cType; + }else if( iLast<=128 ){ + if( iFirst<=128 && iLast>=128 ) a[4] = cType; + if( iFirst<=120 && iLast>=120 ) a[6] = cType; + if( iFirst<=121 && iLast>=121 ) a[7] = cType; + if( iFirst<=122 && iLast>=122 ) a[8] = cType; + if( iFirst<=123 && iLast>=123 ) a[10] = cType; + if( iFirst<=124 && iLast>=124 ) a[11] = cType; + if( iFirst<=125 && iLast>=125 ) a[12] = cType; + if( iFirst<=126 && iLast>=126 ) a[13] = cType; + if( iFirst<=127 && iLast>=127 ) a[14] = cType; + } + } + return SQLITE_OK; +} +#else +# define unixPosixAdvisoryLocks(A,B) SQLITE_ERROR_UNABLE +#endif /* SQLITE_DEBUG || SQLITE_ENABLE_FILESTAT */ + /* ** On some systems, calls to fchown() will trigger a message in a security ** log if they come from non-root processes. So avoid calling fchown() if @@ -505,7 +724,7 @@ static int robustFchown(int fd, uid_t uid, gid_t gid){ /* ** This is the xSetSystemCall() method of sqlite3_vfs for all of the -** "unix" VFSes. Return SQLITE_OK opon successfully updating the +** "unix" VFSes. Return SQLITE_OK upon successfully updating the ** system call pointer, or SQLITE_NOTFOUND if there is no configurable ** system call named zName. */ @@ -588,7 +807,7 @@ static const char *unixNextSystemCall(sqlite3_vfs *p, const char *zName){ /* ** Do not accept any file descriptor less than this value, in order to avoid -** opening database file using file descriptors that are commonly used for +** opening database file using file descriptors that are commonly used for ** standard input, output, and error. */ #ifndef SQLITE_MINIMUM_FILE_DESCRIPTOR @@ -626,18 +845,21 @@ static int robust_open(const char *z, int f, mode_t m){ break; } if( fd>=SQLITE_MINIMUM_FILE_DESCRIPTOR ) break; + if( (f & (O_EXCL|O_CREAT))==(O_EXCL|O_CREAT) ){ + (void)osUnlink(z); + } osClose(fd); - sqlite3_log(SQLITE_WARNING, + sqlite3_log(SQLITE_WARNING, "attempt to open \"%s\" as file descriptor %d", z, fd); fd = -1; - if( osOpen("/dev/null", f, m)<0 ) break; + if( osOpen("/dev/null", O_RDONLY, m)<0 ) break; } if( fd>=0 ){ if( m!=0 ){ struct stat statbuf; - if( osFstat(fd, &statbuf)==0 + if( osFstat(fd, &statbuf)==0 && statbuf.st_size==0 - && (statbuf.st_mode&0777)!=m + && (statbuf.st_mode&0777)!=m ){ osFchmod(fd, m); } @@ -651,27 +873,40 @@ static int robust_open(const char *z, int f, mode_t m){ /* ** Helper functions to obtain and relinquish the global mutex. The -** global mutex is used to protect the unixInodeInfo and -** vxworksFileId objects used by this file, all of which may be -** shared by multiple threads. +** global mutex is used to protect the unixInodeInfo objects used by +** this file, all of which may be shared by multiple threads. ** -** Function unixMutexHeld() is used to assert() that the global mutex -** is held when required. This function is only used as part of assert() +** Function unixMutexHeld() is used to assert() that the global mutex +** is held when required. This function is only used as part of assert() ** statements. e.g. ** ** unixEnterMutex() ** assert( unixMutexHeld() ); ** unixEnterLeave() +** +** To prevent deadlock, the global unixBigLock must must be acquired +** before the unixInodeInfo.pLockMutex mutex, if both are held. It is +** OK to get the pLockMutex without holding unixBigLock first, but if +** that happens, the unixBigLock mutex must not be acquired until after +** pLockMutex is released. +** +** OK: enter(unixBigLock), enter(pLockInfo) +** OK: enter(unixBigLock) +** OK: enter(pLockInfo) +** ERROR: enter(pLockInfo), enter(unixBigLock) */ +static sqlite3_mutex *unixBigLock = 0; static void unixEnterMutex(void){ - sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1)); + assert( sqlite3_mutex_notheld(unixBigLock) ); /* Not a recursive mutex */ + sqlite3_mutex_enter(unixBigLock); } static void unixLeaveMutex(void){ - sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1)); + assert( sqlite3_mutex_held(unixBigLock) ); + sqlite3_mutex_leave(unixBigLock); } #ifdef SQLITE_DEBUG static int unixMutexHeld(void) { - return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1)); + return sqlite3_mutex_held(unixBigLock); } #endif @@ -764,7 +999,7 @@ static int lockTrace(int fd, int op, struct flock *p){ static int robust_ftruncate(int h, sqlite3_int64 sz){ int rc; #ifdef __ANDROID__ - /* On Android, ftruncate() always uses 32-bit offsets, even if + /* On Android, ftruncate() always uses 32-bit offsets, even if ** _FILE_OFFSET_BITS=64 is defined. This means it is unsafe to attempt to ** truncate a file to any size larger than 2GiB. Silently ignore any ** such attempts. */ @@ -780,32 +1015,32 @@ static int robust_ftruncate(int h, sqlite3_int64 sz){ ** This routine translates a standard POSIX errno code into something ** useful to the clients of the sqlite3 functions. Specifically, it is ** intended to translate a variety of "try again" errors into SQLITE_BUSY -** and a variety of "please close the file descriptor NOW" errors into +** and a variety of "please close the file descriptor NOW" errors into ** SQLITE_IOERR -** +** ** Errors during initialization of locks, or file system support for locks, ** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately. */ static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) { - assert( (sqliteIOErr == SQLITE_IOERR_LOCK) || - (sqliteIOErr == SQLITE_IOERR_UNLOCK) || + assert( (sqliteIOErr == SQLITE_IOERR_LOCK) || + (sqliteIOErr == SQLITE_IOERR_UNLOCK) || (sqliteIOErr == SQLITE_IOERR_RDLOCK) || (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ); switch (posixError) { - case EACCES: + case EACCES: case EAGAIN: case ETIMEDOUT: case EBUSY: case EINTR: - case ENOLCK: - /* random NFS retry error, unless during file system support + case ENOLCK: + /* random NFS retry error, unless during file system support * introspection, in which it actually means what it says */ return SQLITE_BUSY; - - case EPERM: + + case EPERM: return SQLITE_PERM; - - default: + + default: return sqliteIOErr; } } @@ -820,7 +1055,7 @@ static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) { ** ** A pointer to an instance of the following structure can be used as a ** unique file ID in VxWorks. Each instance of this structure contains -** a copy of the canonical filename. There is also a reference count. +** a copy of the canonical filename. There is also a reference count. ** The structure is reclaimed when the number of pointers to it drops to ** zero. ** @@ -836,11 +1071,12 @@ struct vxworksFileId { }; #if OS_VXWORKS -/* +/* ** All unique filenames are held on a linked list headed by this ** variable: */ static struct vxworksFileId *vxworksFileList = 0; +static sqlite3_mutex *vxworksMutex = 0; /* ** Simplify a filename into its canonical form @@ -906,14 +1142,14 @@ static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){ ** If found, increment the reference count and return a pointer to ** the existing file ID. */ - unixEnterMutex(); + sqlite3_mutex_enter(vxworksMutex); for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){ - if( pCandidate->nName==n + if( pCandidate->nName==n && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0 ){ sqlite3_free(pNew); pCandidate->nRef++; - unixLeaveMutex(); + sqlite3_mutex_leave(vxworksMutex); return pCandidate; } } @@ -923,7 +1159,7 @@ static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){ pNew->nName = n; pNew->pNext = vxworksFileList; vxworksFileList = pNew; - unixLeaveMutex(); + sqlite3_mutex_leave(vxworksMutex); return pNew; } @@ -932,7 +1168,7 @@ static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){ ** the object when the reference count reaches zero. */ static void vxworksReleaseFileId(struct vxworksFileId *pId){ - unixEnterMutex(); + sqlite3_mutex_enter(vxworksMutex); assert( pId->nRef>0 ); pId->nRef--; if( pId->nRef==0 ){ @@ -942,7 +1178,7 @@ static void vxworksReleaseFileId(struct vxworksFileId *pId){ *pp = pId->pNext; sqlite3_free(pId); } - unixLeaveMutex(); + sqlite3_mutex_leave(vxworksMutex); } #endif /* OS_VXWORKS */ /*************** End of Unique File ID Utility Used By VxWorks **************** @@ -1001,7 +1237,7 @@ static void vxworksReleaseFileId(struct vxworksFileId *pId){ ** cnt>0 means there are cnt shared locks on the file. ** ** Any attempt to lock or unlock a file first checks the locking -** structure. The fcntl() system call is only invoked to set a +** structure. The fcntl() system call is only invoked to set a ** POSIX lock if the internal lock structure transitions between ** a locked and an unlocked state. ** @@ -1010,7 +1246,7 @@ static void vxworksReleaseFileId(struct vxworksFileId *pId){ ** If you close a file descriptor that points to a file that has locks, ** all locks on that file that are owned by the current process are ** released. To work around this problem, each unixInodeInfo object -** maintains a count of the number of pending locks on tha inode. +** maintains a count of the number of pending locks on the inode. ** When an attempt is made to close an unixFile, if there are ** other unixFile open on the same inode that are holding locks, the call ** to close() the file descriptor is deferred until all of the locks clear. @@ -1024,7 +1260,7 @@ static void vxworksReleaseFileId(struct vxworksFileId *pId){ ** not posix compliant. Under LinuxThreads, a lock created by thread ** A cannot be modified or overridden by a different thread B. ** Only thread A can modify the lock. Locking behavior is correct -** if the appliation uses the newer Native Posix Thread Library (NPTL) +** if the application uses the newer Native Posix Thread Library (NPTL) ** on linux - with NPTL a lock created by thread A can override locks ** in thread B. But there is no way to know at compile-time which ** threading library is being used. So there is no way to know at @@ -1034,7 +1270,7 @@ static void vxworksReleaseFileId(struct vxworksFileId *pId){ ** ** SQLite used to support LinuxThreads. But support for LinuxThreads ** was dropped beginning with version 3.7.0. SQLite will still work with -** LinuxThreads provided that (1) there is no more than one connection +** LinuxThreads provided that (1) there is no more than one connection ** per database file in the same process and (2) database connections ** do not move across threads. */ @@ -1048,28 +1284,52 @@ struct unixFileId { #if OS_VXWORKS struct vxworksFileId *pId; /* Unique file ID for vxworks. */ #else - ino_t ino; /* Inode number */ + /* We are told that some versions of Android contain a bug that + ** sizes ino_t at only 32-bits instead of 64-bits. (See + ** https://android-review.googlesource.com/#/c/115351/3/dist/sqlite3.c) + ** To work around this, always allocate 64-bits for the inode number. + ** On small machines that only have 32-bit inodes, this wastes 4 bytes, + ** but that should not be a big deal. */ + /* WAS: ino_t ino; */ + u64 ino; /* Inode number */ #endif }; /* ** An instance of the following structure is allocated for each open -** inode. Or, on LinuxThreads, there is one of these structures for -** each inode opened by each thread. +** inode. ** ** A single inode can have multiple file descriptors, so each unixFile ** structure contains a pointer to an instance of this object and this ** object keeps a count of the number of unixFile pointing to it. +** +** Mutex rules: +** +** (1) Only the pLockMutex mutex must be held in order to read or write +** any of the locking fields: +** nShared, nLock, eFileLock, bProcessLock, pUnused +** +** (2) When nRef>0, then the following fields are unchanging and can +** be read (but not written) without holding any mutex: +** fileId, pLockMutex +** +** (3) With the exceptions above, all the fields may only be read +** or written while holding the global unixBigLock mutex. +** +** Deadlock prevention: The global unixBigLock mutex may not +** be acquired while holding the pLockMutex mutex. If both unixBigLock +** and pLockMutex are needed, then unixBigLock must be acquired first. */ struct unixInodeInfo { struct unixFileId fileId; /* The lookup key */ - int nShared; /* Number of SHARED locks held */ - unsigned char eFileLock; /* One of SHARED_LOCK, RESERVED_LOCK etc. */ - unsigned char bProcessLock; /* An exclusive process lock is held */ + sqlite3_mutex *pLockMutex; /* Hold this mutex for... */ + int nShared; /* Number of SHARED locks held */ + int nLock; /* Number of outstanding file locks */ + unsigned char eFileLock; /* One of SHARED_LOCK, RESERVED_LOCK etc. */ + unsigned char bProcessLock; /* An exclusive process lock is held */ + UnixUnusedFd *pUnused; /* Unused file descriptors to close */ int nRef; /* Number of pointers to this structure */ unixShmNode *pShmNode; /* Shared memory associated with this inode */ - int nLock; /* Number of outstanding file locks */ - UnixUnusedFd *pUnused; /* Unused file descriptors to close */ unixInodeInfo *pNext; /* List of all unixInodeInfo objects */ unixInodeInfo *pPrev; /* .... doubly linked */ #if SQLITE_ENABLE_LOCKING_STYLE @@ -1083,8 +1343,26 @@ struct unixInodeInfo { /* ** A lists of all unixInodeInfo objects. +** +** Must hold unixBigLock in order to read or write this variable. +*/ +static unixInodeInfo *inodeList = 0; /* All unixInodeInfo objects */ + +#ifdef SQLITE_DEBUG +/* +** True if the inode mutex (on the unixFile.pFileMutex field) is held, or not. +** This routine is used only within assert() to help verify correct mutex +** usage. */ -static unixInodeInfo *inodeList = 0; +int unixFileMutexHeld(unixFile *pFile){ + assert( pFile->pInode ); + return sqlite3_mutex_held(pFile->pInode->pLockMutex); +} +int unixFileMutexNotheld(unixFile *pFile){ + assert( pFile->pInode ); + return sqlite3_mutex_notheld(pFile->pInode->pLockMutex); +} +#endif /* ** @@ -1097,7 +1375,7 @@ static unixInodeInfo *inodeList = 0; ** strerror_r(). ** ** The first argument passed to the macro should be the error code that -** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN). +** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN). ** The two subsequent arguments should be the name of the OS function that ** failed (e.g. "unlink", "open") and the associated file-system path, ** if any. @@ -1115,7 +1393,7 @@ static int unixLogErrorAtLine( /* If this is not a threadsafe build (SQLITE_THREADSAFE==0), then use ** the strerror() function to obtain the human-readable error message ** equivalent to errno. Otherwise, use strerror_r(). - */ + */ #if SQLITE_THREADSAFE && defined(HAVE_STRERROR_R) char aErr[80]; memset(aErr, 0, sizeof(aErr)); @@ -1123,18 +1401,22 @@ static int unixLogErrorAtLine( /* If STRERROR_R_CHAR_P (set by autoconf scripts) or __USE_GNU is defined, ** assume that the system provides the GNU version of strerror_r() that - ** returns a pointer to a buffer containing the error message. That pointer - ** may point to aErr[], or it may point to some static storage somewhere. - ** Otherwise, assume that the system provides the POSIX version of + ** returns a pointer to a buffer containing the error message. That pointer + ** may point to aErr[], or it may point to some static storage somewhere. + ** Otherwise, assume that the system provides the POSIX version of ** strerror_r(), which always writes an error message into aErr[]. ** ** If the code incorrectly assumes that it is the POSIX version that is ** available, the error message will often be an empty string. Not a - ** huge problem. Incorrectly concluding that the GNU version is available + ** huge problem. Incorrectly concluding that the GNU version is available ** could lead to a segfault though. + ** + ** Forum post 3f13857fa4062301 reports that the Android SDK may use + ** int-type return, depending on its version. */ -#if defined(STRERROR_R_CHAR_P) || defined(__USE_GNU) - zErr = +#if (defined(STRERROR_R_CHAR_P) || defined(__USE_GNU)) \ + && !defined(ANDROID) && !defined(__ANDROID__) + zErr = # endif strerror_r(iErrno, aErr, sizeof(aErr)-1); @@ -1184,12 +1466,13 @@ static void storeLastErrno(unixFile *pFile, int error){ } /* -** Close all file descriptors accumuated in the unixInodeInfo->pUnused list. -*/ +** Close all file descriptors accumulated in the unixInodeInfo->pUnused list. +*/ static void closePendingFds(unixFile *pFile){ unixInodeInfo *pInode = pFile->pInode; UnixUnusedFd *p; UnixUnusedFd *pNext; + assert( unixFileMutexHeld(pFile) ); for(p=pInode->pUnused; p; p=pNext){ pNext = p->pNext; robust_close(pFile, p->fd, __LINE__); @@ -1201,17 +1484,20 @@ static void closePendingFds(unixFile *pFile){ /* ** Release a unixInodeInfo structure previously allocated by findInodeInfo(). ** -** The mutex entered using the unixEnterMutex() function must be held -** when this function is called. +** The global mutex must be held when this routine is called, but the mutex +** on the inode being deleted must NOT be held. */ static void releaseInodeInfo(unixFile *pFile){ unixInodeInfo *pInode = pFile->pInode; assert( unixMutexHeld() ); + assert( unixFileMutexNotheld(pFile) ); if( ALWAYS(pInode) ){ pInode->nRef--; if( pInode->nRef==0 ){ assert( pInode->pShmNode==0 ); + sqlite3_mutex_enter(pInode->pLockMutex); closePendingFds(pFile); + sqlite3_mutex_leave(pInode->pLockMutex); if( pInode->pPrev ){ assert( pInode->pPrev->pNext==pInode ); pInode->pPrev->pNext = pInode->pNext; @@ -1223,6 +1509,7 @@ static void releaseInodeInfo(unixFile *pFile){ assert( pInode->pNext->pPrev==pInode ); pInode->pNext->pPrev = pInode->pPrev; } + sqlite3_mutex_free(pInode->pLockMutex); sqlite3_free(pInode); } } @@ -1233,8 +1520,7 @@ static void releaseInodeInfo(unixFile *pFile){ ** describes that file descriptor. Create a new one if necessary. The ** return value might be uninitialized if an error occurs. ** -** The mutex entered using the unixEnterMutex() function must be held -** when this function is called. +** The global mutex must held when calling this routine. ** ** Return an appropriate error code. */ @@ -1280,6 +1566,10 @@ static int findInodeInfo( storeLastErrno(pFile, errno); return SQLITE_IOERR; } + if( fsync(fd) ){ + storeLastErrno(pFile, errno); + return SQLITE_IOERR_FSYNC; + } rc = osFstat(fd, &statbuf); if( rc!=0 ){ storeLastErrno(pFile, errno); @@ -1293,8 +1583,9 @@ static int findInodeInfo( #if OS_VXWORKS fileId.pId = pFile->pId; #else - fileId.ino = statbuf.st_ino; + fileId.ino = (u64)statbuf.st_ino; #endif + assert( unixMutexHeld() ); pInode = inodeList; while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){ pInode = pInode->pNext; @@ -1302,11 +1593,19 @@ static int findInodeInfo( if( pInode==0 ){ pInode = sqlite3_malloc64( sizeof(*pInode) ); if( pInode==0 ){ - return SQLITE_NOMEM; + return SQLITE_NOMEM_BKPT; } memset(pInode, 0, sizeof(*pInode)); memcpy(&pInode->fileId, &fileId, sizeof(fileId)); + if( sqlite3GlobalConfig.bCoreMutex ){ + pInode->pLockMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); + if( pInode->pLockMutex==0 ){ + sqlite3_free(pInode); + return SQLITE_NOMEM_BKPT; + } + } pInode->nRef = 1; + assert( unixMutexHeld() ); pInode->pNext = inodeList; pInode->pPrev = 0; if( inodeList ) inodeList->pPrev = pInode; @@ -1327,7 +1626,8 @@ static int fileHasMoved(unixFile *pFile){ #else struct stat buf; return pFile->pInode!=0 && - (osStat(pFile->zPath, &buf)!=0 || buf.st_ino!=pFile->pInode->fileId.ino); + (osStat(pFile->zPath, &buf)!=0 + || (u64)buf.st_ino!=pFile->pInode->fileId.ino); #endif } @@ -1344,12 +1644,16 @@ static int fileHasMoved(unixFile *pFile){ static void verifyDbFile(unixFile *pFile){ struct stat buf; int rc; + + /* These verifications occurs for the main database only */ + if( pFile->ctrlFlags & UNIXFILE_NOLOCK ) return; + rc = osFstat(pFile->h, &buf); if( rc!=0 ){ sqlite3_log(SQLITE_WARNING, "cannot fstat db file %s", pFile->zPath); return; } - if( buf.st_nlink==0 && (pFile->ctrlFlags & UNIXFILE_DELETE)==0 ){ + if( buf.st_nlink==0 ){ sqlite3_log(SQLITE_WARNING, "file unlinked while open: %s", pFile->zPath); return; } @@ -1379,7 +1683,7 @@ static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){ assert( pFile ); assert( pFile->eFileLock<=SHARED_LOCK ); - unixEnterMutex(); /* Because pFile->pInode is shared across threads */ + sqlite3_mutex_enter(pFile->pInode->pLockMutex); /* Check if a thread in this process holds such a lock */ if( pFile->pInode->eFileLock>SHARED_LOCK ){ @@ -1403,16 +1707,81 @@ static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){ } } #endif - - unixLeaveMutex(); + + sqlite3_mutex_leave(pFile->pInode->pLockMutex); OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved)); *pResOut = reserved; return rc; } +/* Forward declaration*/ +static int unixSleep(sqlite3_vfs*,int); + +/* +** Set a posix-advisory-lock. +** +** There are two versions of this routine. If compiled with +** SQLITE_ENABLE_SETLK_TIMEOUT then the routine has an extra parameter +** which is a pointer to a unixFile. If the unixFile->iBusyTimeout +** value is set, then it is the number of milliseconds to wait before +** failing the lock. The iBusyTimeout value is always reset back to +** zero on each call. +** +** If SQLITE_ENABLE_SETLK_TIMEOUT is not defined, then do a non-blocking +** attempt to set the lock. +*/ +#ifndef SQLITE_ENABLE_SETLK_TIMEOUT +# define osSetPosixAdvisoryLock(h,x,t) osFcntl(h,F_SETLK,x) +#else +static int osSetPosixAdvisoryLock( + int h, /* The file descriptor on which to take the lock */ + struct flock *pLock, /* The description of the lock */ + unixFile *pFile /* Structure holding timeout value */ +){ + int rc = 0; + + if( pFile->iBusyTimeout==0 ){ + /* unixFile->iBusyTimeout is set to 0. In this case, attempt a + ** non-blocking lock. */ + rc = osFcntl(h,F_SETLK,pLock); + }else{ + /* unixFile->iBusyTimeout is set to greater than zero. In this case, + ** attempt a blocking-lock with a unixFile->iBusyTimeout ms timeout. + ** + ** On systems that support some kind of blocking file lock operation, + ** this block should be replaced by code to attempt a blocking lock + ** with a timeout of unixFile->iBusyTimeout ms. The code below is + ** placeholder code. If SQLITE_TEST is defined, the placeholder code + ** retries the lock once every 1ms until it succeeds or the timeout + ** is reached. Or, if SQLITE_TEST is not defined, the placeholder + ** code attempts a non-blocking lock and sets unixFile->iBusyTimeout + ** to 0. This causes the caller to return SQLITE_BUSY, instead of + ** SQLITE_BUSY_TIMEOUT to SQLite - as required by a VFS that does not + ** support blocking locks. + */ +#ifdef SQLITE_TEST + int tm = pFile->iBusyTimeout; + while( tm>0 ){ + rc = osFcntl(h,F_SETLK,pLock); + if( rc==0 ) break; + unixSleep(0,1000); + tm--; + } +#else + rc = osFcntl(h,F_SETLK,pLock); + pFile->iBusyTimeout = 0; +#endif + /* End of code to replace with real blocking-locks code. */ + } + + return rc; +} +#endif /* SQLITE_ENABLE_SETLK_TIMEOUT */ + + /* -** Attempt to set a system-lock on the file pFile. The lock is +** Attempt to set a system-lock on the file pFile. The lock is ** described by pLock. ** ** If the pFile was opened read/write from unix-excl, then the only lock @@ -1433,17 +1802,17 @@ static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){ static int unixFileLock(unixFile *pFile, struct flock *pLock){ int rc; unixInodeInfo *pInode = pFile->pInode; - assert( unixMutexHeld() ); assert( pInode!=0 ); + assert( sqlite3_mutex_held(pInode->pLockMutex) ); if( (pFile->ctrlFlags & (UNIXFILE_EXCL|UNIXFILE_RDONLY))==UNIXFILE_EXCL ){ if( pInode->bProcessLock==0 ){ struct flock lock; - assert( pInode->nLock==0 ); + /* assert( pInode->nLock==0 ); <-- Not true if unix-excl READONLY used */ lock.l_whence = SEEK_SET; lock.l_start = SHARED_FIRST; lock.l_len = SHARED_SIZE; lock.l_type = F_WRLCK; - rc = osFcntl(pFile->h, F_SETLK, &lock); + rc = osSetPosixAdvisoryLock(pFile->h, &lock, pFile); if( rc<0 ) return rc; pInode->bProcessLock = 1; pInode->nLock++; @@ -1451,11 +1820,25 @@ static int unixFileLock(unixFile *pFile, struct flock *pLock){ rc = 0; } }else{ - rc = osFcntl(pFile->h, F_SETLK, pLock); +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT + if( pFile->bBlockOnConnect && pLock->l_type==F_RDLCK + && pLock->l_start==SHARED_FIRST && pLock->l_len==SHARED_SIZE + ){ + rc = osFcntl(pFile->h, F_SETLKW, pLock); + }else +#endif + rc = osSetPosixAdvisoryLock(pFile->h, pLock, pFile); } return rc; } +#if !defined(SQLITE_WASI) && !defined(SQLITE_OMIT_WAL) +/* Forward reference */ +static int unixIsSharingShmNode(unixFile*); +#else +#define unixIsSharingShmNode(pFile) (0) +#endif + /* ** Lock the file with the lock specified by parameter eFileLock - one ** of the following: @@ -1473,7 +1856,7 @@ static int unixFileLock(unixFile *pFile, struct flock *pLock){ ** ** UNLOCKED -> SHARED ** SHARED -> RESERVED -** SHARED -> (PENDING) -> EXCLUSIVE +** SHARED -> EXCLUSIVE ** RESERVED -> (PENDING) -> EXCLUSIVE ** PENDING -> EXCLUSIVE ** @@ -1485,39 +1868,41 @@ static int unixLock(sqlite3_file *id, int eFileLock){ ** lock transitions in terms of the POSIX advisory shared and exclusive ** lock primitives (called read-locks and write-locks below, to avoid ** confusion with SQLite lock names). The algorithms are complicated - ** slightly in order to be compatible with windows systems simultaneously + ** slightly in order to be compatible with Windows95 systems simultaneously ** accessing the same database file, in case that is ever required. ** - ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved + ** Symbols defined in os.h identify the 'pending byte' and the 'reserved ** byte', each single bytes at well known offsets, and the 'shared byte ** range', a range of 510 bytes at a well known offset. ** ** To obtain a SHARED lock, a read-lock is obtained on the 'pending - ** byte'. If this is successful, a random byte from the 'shared byte - ** range' is read-locked and the lock on the 'pending byte' released. + ** byte'. If this is successful, 'shared byte range' is read-locked + ** and the lock on the 'pending byte' released. (Legacy note: When + ** SQLite was first developed, Windows95 systems were still very common, + ** and Windows95 lacks a shared-lock capability. So on Windows95, a + ** single randomly selected by from the 'shared byte range' is locked. + ** Windows95 is now pretty much extinct, but this work-around for the + ** lack of shared-locks on Windows95 lives on, for backwards + ** compatibility.) ** ** A process may only obtain a RESERVED lock after it has a SHARED lock. ** A RESERVED lock is implemented by grabbing a write-lock on the - ** 'reserved byte'. - ** - ** A process may only obtain a PENDING lock after it has obtained a - ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock - ** on the 'pending byte'. This ensures that no new SHARED locks can be - ** obtained, but existing SHARED locks are allowed to persist. A process - ** does not have to obtain a RESERVED lock on the way to a PENDING lock. - ** This property is used by the algorithm for rolling back a journal file - ** after a crash. + ** 'reserved byte'. ** - ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is - ** implemented by obtaining a write-lock on the entire 'shared byte - ** range'. Since all other locks require a read-lock on one of the bytes - ** within this range, this ensures that no other locks are held on the - ** database. + ** An EXCLUSIVE lock may only be requested after either a SHARED or + ** RESERVED lock is held. An EXCLUSIVE lock is implemented by obtaining + ** a write-lock on the entire 'shared byte range'. Since all other locks + ** require a read-lock on one of the bytes within this range, this ensures + ** that no other locks are held on the database. ** - ** The reason a single byte cannot be used instead of the 'shared byte - ** range' is that some versions of windows do not support read-locks. By - ** locking a random byte from a range, concurrent SHARED locks may exist - ** even if the locking primitive used is always a write-lock. + ** If a process that holds a RESERVED lock requests an EXCLUSIVE, then + ** a PENDING lock is obtained first. A PENDING lock is implemented by + ** obtaining a write-lock on the 'pending byte'. This ensures that no new + ** SHARED locks can be obtained, but existing SHARED locks are allowed to + ** persist. If the call to this function fails to obtain the EXCLUSIVE + ** lock in this case, it holds the PENDING lock instead. The client may + ** then re-attempt the EXCLUSIVE lock later on, after existing SHARED + ** locks have cleared. */ int rc = SQLITE_OK; unixFile *pFile = (unixFile*)id; @@ -1543,7 +1928,7 @@ static int unixLock(sqlite3_file *id, int eFileLock){ /* Make sure the locking sequence is correct. ** (1) We never move from unlocked to anything higher than shared lock. - ** (2) SQLite never explicitly requests a pendig lock. + ** (2) SQLite never explicitly requests a pending lock. ** (3) A shared lock is always held when a reserve lock is requested. */ assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK ); @@ -1552,13 +1937,13 @@ static int unixLock(sqlite3_file *id, int eFileLock){ /* This mutex is needed because pFile->pInode is shared across threads */ - unixEnterMutex(); pInode = pFile->pInode; + sqlite3_mutex_enter(pInode->pLockMutex); /* If some thread using this PID has a lock via a different unixFile* ** handle that precludes the requested lock, return BUSY. */ - if( (pFile->eFileLock!=pInode->eFileLock && + if( (pFile->eFileLock!=pInode->eFileLock && (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK)) ){ rc = SQLITE_BUSY; @@ -1569,7 +1954,7 @@ static int unixLock(sqlite3_file *id, int eFileLock){ ** has a SHARED or RESERVED lock, then increment reference counts and ** return SQLITE_OK. */ - if( eFileLock==SHARED_LOCK && + if( eFileLock==SHARED_LOCK && (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){ assert( eFileLock==SHARED_LOCK ); assert( pFile->eFileLock==0 ); @@ -1587,8 +1972,8 @@ static int unixLock(sqlite3_file *id, int eFileLock){ */ lock.l_len = 1L; lock.l_whence = SEEK_SET; - if( eFileLock==SHARED_LOCK - || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLockeFileLock==RESERVED_LOCK) ){ lock.l_type = (eFileLock==SHARED_LOCK?F_RDLCK:F_WRLCK); lock.l_start = PENDING_BYTE; @@ -1599,6 +1984,9 @@ static int unixLock(sqlite3_file *id, int eFileLock){ storeLastErrno(pFile, tErrno); } goto end_lock; + }else if( eFileLock==EXCLUSIVE_LOCK ){ + pFile->eFileLock = PENDING_LOCK; + pInode->eFileLock = PENDING_LOCK; } } @@ -1626,7 +2014,7 @@ static int unixLock(sqlite3_file *id, int eFileLock){ if( unixFileLock(pFile, &lock) && rc==SQLITE_OK ){ /* This could happen with a network mount */ tErrno = errno; - rc = SQLITE_IOERR_UNLOCK; + rc = SQLITE_IOERR_UNLOCK; } if( rc ){ @@ -1643,6 +2031,14 @@ static int unixLock(sqlite3_file *id, int eFileLock){ /* We are trying for an exclusive lock but another thread in this ** same process is still holding a shared lock. */ rc = SQLITE_BUSY; + }else if( unixIsSharingShmNode(pFile) ){ + /* We are in WAL mode and attempting to delete the SHM and WAL + ** files due to closing the connection or changing out of WAL mode, + ** but another process still holds locks on the SHM file, thus + ** indicating that database locks have been broken, perhaps due + ** to a rogue close(open(dbFile)) or similar. + */ + rc = SQLITE_BUSY; }else{ /* The request was for a RESERVED or EXCLUSIVE lock. It is ** assumed that there is a SHARED or greater lock on the file @@ -1668,7 +2064,7 @@ static int unixLock(sqlite3_file *id, int eFileLock){ } } } - + #ifdef SQLITE_DEBUG /* Set up the transaction-counter change checking flags when @@ -1686,18 +2082,14 @@ static int unixLock(sqlite3_file *id, int eFileLock){ } #endif - if( rc==SQLITE_OK ){ pFile->eFileLock = eFileLock; pInode->eFileLock = eFileLock; - }else if( eFileLock==EXCLUSIVE_LOCK ){ - pFile->eFileLock = PENDING_LOCK; - pInode->eFileLock = PENDING_LOCK; } end_lock: - unixLeaveMutex(); - OSTRACE(("LOCK %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock), + sqlite3_mutex_leave(pInode->pLockMutex); + OSTRACE(("LOCK %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock), rc==SQLITE_OK ? "ok" : "failed")); return rc; } @@ -1708,11 +2100,12 @@ static int unixLock(sqlite3_file *id, int eFileLock){ */ static void setPendingFd(unixFile *pFile){ unixInodeInfo *pInode = pFile->pInode; - UnixUnusedFd *p = pFile->pUnused; + UnixUnusedFd *p = pFile->pPreallocatedUnused; + assert( unixFileMutexHeld(pFile) ); p->pNext = pInode->pUnused; pInode->pUnused = p; pFile->h = -1; - pFile->pUnused = 0; + pFile->pPreallocatedUnused = 0; } /* @@ -1721,11 +2114,11 @@ static void setPendingFd(unixFile *pFile){ ** ** If the locking level of the file descriptor is already at or below ** the requested locking level, this routine is a no-op. -** +** ** If handleNFSUnlock is true, then on downgrading an EXCLUSIVE_LOCK to SHARED ** the byte range is divided into 2 parts and the first part is unlocked then -** set to a read lock, then the other part is simply unlocked. This works -** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to +** set to a read lock, then the other part is simply unlocked. This works +** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to ** remove the write lock on a region when a read lock is set. */ static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){ @@ -1743,8 +2136,8 @@ static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){ if( pFile->eFileLock<=eFileLock ){ return SQLITE_OK; } - unixEnterMutex(); pInode = pFile->pInode; + sqlite3_mutex_enter(pInode->pLockMutex); assert( pInode->nShared!=0 ); if( pFile->eFileLock>SHARED_LOCK ){ assert( pInode->eFileLock==pFile->eFileLock ); @@ -1763,7 +2156,7 @@ static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){ /* downgrading to a shared lock on NFS involves clearing the write lock ** before establishing the readlock - to avoid a race condition we downgrade - ** the lock in 2 blocks, so that part of the range will be covered by a + ** the lock in 2 blocks, so that part of the range will be covered by a ** write lock until the rest is covered by a read lock: ** 1: [WWWWW] ** 2: [....W] @@ -1779,7 +2172,7 @@ static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){ if( handleNFSUnlock ){ int tErrno; /* Error code from system call errors */ off_t divSize = SHARED_SIZE - 1; - + lock.l_type = F_UNLCK; lock.l_whence = SEEK_SET; lock.l_start = SHARED_FIRST; @@ -1821,11 +2214,11 @@ static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){ lock.l_len = SHARED_SIZE; if( unixFileLock(pFile, &lock) ){ /* In theory, the call to unixFileLock() cannot fail because another - ** process is holding an incompatible lock. If it does, this + ** process is holding an incompatible lock. If it does, this ** indicates that the other process is not following the locking ** protocol. If this happens, return SQLITE_IOERR_RDLOCK. Returning - ** SQLITE_BUSY would confuse the upper layer (in practice it causes - ** an assert to fail). */ + ** SQLITE_BUSY would confuse the upper layer (in practice it causes + ** an assert to fail). */ rc = SQLITE_IOERR_RDLOCK; storeLastErrno(pFile, errno); goto end_unlock; @@ -1870,14 +2263,14 @@ static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){ */ pInode->nLock--; assert( pInode->nLock>=0 ); - if( pInode->nLock==0 ){ - closePendingFds(pFile); - } + if( pInode->nLock==0 ) closePendingFds(pFile); } end_unlock: - unixLeaveMutex(); - if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock; + sqlite3_mutex_leave(pInode->pLockMutex); + if( rc==SQLITE_OK ){ + pFile->eFileLock = eFileLock; + } return rc; } @@ -1901,7 +2294,7 @@ static void unixUnmapfile(unixFile *pFd); #endif /* -** This function performs the parts of the "close file" operation +** This function performs the parts of the "close file" operation ** common to all locking schemes. It closes the directory and file ** handles, if they are valid, and sets all fields of the unixFile ** structure to 0. @@ -1937,7 +2330,7 @@ static int closeUnixFile(sqlite3_file *id){ #endif OSTRACE(("CLOSE %-3d\n", pFile->h)); OpenCounter(-1); - sqlite3_free(pFile->pUnused); + sqlite3_free(pFile->pPreallocatedUnused); memset(pFile, 0, sizeof(unixFile)); return SQLITE_OK; } @@ -1948,23 +2341,30 @@ static int closeUnixFile(sqlite3_file *id){ static int unixClose(sqlite3_file *id){ int rc = SQLITE_OK; unixFile *pFile = (unixFile *)id; + unixInodeInfo *pInode = pFile->pInode; + + assert( pInode!=0 ); verifyDbFile(pFile); unixUnlock(id, NO_LOCK); + assert( unixFileMutexNotheld(pFile) ); unixEnterMutex(); /* unixFile.pInode is always valid here. Otherwise, a different close ** routine (e.g. nolockClose()) would be called instead. */ assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 ); - if( ALWAYS(pFile->pInode) && pFile->pInode->nLock ){ + sqlite3_mutex_enter(pInode->pLockMutex); + if( pInode->nLock ){ /* If there are outstanding locks, do not actually close the file just ** yet because that would clear those locks. Instead, add the file - ** descriptor to pInode->pUnused list. It will be automatically closed + ** descriptor to pInode->pUnused list. It will be automatically closed ** when the last lock is cleared. */ setPendingFd(pFile); } + sqlite3_mutex_leave(pInode->pLockMutex); releaseInodeInfo(pFile); + assert( pFile->pShm==0 ); rc = closeUnixFile(id); unixLeaveMutex(); return rc; @@ -2044,26 +2444,22 @@ static int nolockClose(sqlite3_file *id) { /* ** This routine checks if there is a RESERVED lock held on the specified -** file by this or any other process. If such a lock is held, set *pResOut -** to a non-zero value otherwise *pResOut is set to zero. The return value -** is set to SQLITE_OK unless an I/O error occurs during lock checking. -** -** In dotfile locking, either a lock exists or it does not. So in this -** variation of CheckReservedLock(), *pResOut is set to true if any lock -** is held on the file and false if the file is unlocked. +** file by this or any other process. If the caller holds a SHARED +** or greater lock when it is called, then it is assumed that no other +** client may hold RESERVED. Or, if the caller holds no lock, then it +** is assumed another client holds RESERVED if the lock-file exists. */ static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) { - int rc = SQLITE_OK; - int reserved = 0; unixFile *pFile = (unixFile*)id; - SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); - - assert( pFile ); - reserved = osAccess((const char*)pFile->lockingContext, 0)==0; - OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved)); - *pResOut = reserved; - return rc; + + if( pFile->eFileLock>=SHARED_LOCK ){ + *pResOut = 0; + }else{ + *pResOut = osAccess((const char*)pFile->lockingContext, 0)==0; + } + OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, 0, *pResOut)); + return SQLITE_OK; } /* @@ -2112,7 +2508,7 @@ static int dotlockLock(sqlite3_file *id, int eFileLock) { #endif return SQLITE_OK; } - + /* grab an exclusive lock */ rc = osMkdir(zLockFile, 0777); if( rc<0 ){ @@ -2127,8 +2523,8 @@ static int dotlockLock(sqlite3_file *id, int eFileLock) { } } return rc; - } - + } + /* got it, set the type and return ok */ pFile->eFileLock = eFileLock; return rc; @@ -2152,7 +2548,7 @@ static int dotlockUnlock(sqlite3_file *id, int eFileLock) { OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock, pFile->eFileLock, osGetpid(0))); assert( eFileLock<=SHARED_LOCK ); - + /* no-op if possible */ if( pFile->eFileLock==eFileLock ){ return SQLITE_OK; @@ -2165,7 +2561,7 @@ static int dotlockUnlock(sqlite3_file *id, int eFileLock) { pFile->eFileLock = SHARED_LOCK; return SQLITE_OK; } - + /* To fully unlock the database, delete the lock file */ assert( eFileLock==NO_LOCK ); rc = osRmdir(zLockFile); @@ -2177,7 +2573,7 @@ static int dotlockUnlock(sqlite3_file *id, int eFileLock) { rc = SQLITE_IOERR_UNLOCK; storeLastErrno(pFile, tErrno); } - return rc; + return rc; } pFile->eFileLock = NO_LOCK; return SQLITE_OK; @@ -2224,7 +2620,7 @@ static int robust_flock(int fd, int op){ #else # define robust_flock(a,b) flock(a,b) #endif - + /* ** This routine checks if there is a RESERVED lock held on the specified @@ -2233,54 +2629,33 @@ static int robust_flock(int fd, int op){ ** is set to SQLITE_OK unless an I/O error occurs during lock checking. */ static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){ - int rc = SQLITE_OK; - int reserved = 0; +#ifdef SQLITE_DEBUG unixFile *pFile = (unixFile*)id; - +#else + UNUSED_PARAMETER(id); +#endif + SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); - + assert( pFile ); - - /* Check if a thread in this process holds such a lock */ - if( pFile->eFileLock>SHARED_LOCK ){ - reserved = 1; - } - - /* Otherwise see if some other process holds it. */ - if( !reserved ){ - /* attempt to get the lock */ - int lrc = robust_flock(pFile->h, LOCK_EX | LOCK_NB); - if( !lrc ){ - /* got the lock, unlock it */ - lrc = robust_flock(pFile->h, LOCK_UN); - if ( lrc ) { - int tErrno = errno; - /* unlock failed with an error */ - lrc = SQLITE_IOERR_UNLOCK; - storeLastErrno(pFile, tErrno); - rc = lrc; - } - } else { - int tErrno = errno; - reserved = 1; - /* someone else might have it reserved */ - lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); - if( IS_LOCK_ERROR(lrc) ){ - storeLastErrno(pFile, tErrno); - rc = lrc; - } - } - } - OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved)); + assert( pFile->eFileLock<=SHARED_LOCK ); -#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS - if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){ - rc = SQLITE_OK; - reserved=1; - } -#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ - *pResOut = reserved; - return rc; + /* The flock VFS only ever takes exclusive locks (see function flockLock). + ** Therefore, if this connection is holding any lock at all, no other + ** connection may be holding a RESERVED lock. So set *pResOut to 0 + ** in this case. + ** + ** Or, this connection may be holding no lock. In that case, set *pResOut to + ** 0 as well. The caller will then attempt to take an EXCLUSIVE lock on the + ** db in order to roll the hot journal back. If there is another connection + ** holding a lock, that attempt will fail and an SQLITE_BUSY returned to + ** the user. With other VFS, we try to avoid this, in order to allow a reader + ** to proceed while a writer is preparing its transaction. But that won't + ** work with the flock VFS - as it always takes EXCLUSIVE locks - so it is + ** not a problem in this case. */ + *pResOut = 0; + + return SQLITE_OK; } /* @@ -2318,15 +2693,15 @@ static int flockLock(sqlite3_file *id, int eFileLock) { assert( pFile ); - /* if we already have a lock, it is exclusive. + /* if we already have a lock, it is exclusive. ** Just adjust level and punt on outta here. */ if (pFile->eFileLock > NO_LOCK) { pFile->eFileLock = eFileLock; return SQLITE_OK; } - + /* grab an exclusive lock */ - + if (robust_flock(pFile->h, LOCK_EX | LOCK_NB)) { int tErrno = errno; /* didn't get, must be busy */ @@ -2338,10 +2713,10 @@ static int flockLock(sqlite3_file *id, int eFileLock) { /* got it, set the type and return ok */ pFile->eFileLock = eFileLock; } - OSTRACE(("LOCK %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock), + OSTRACE(("LOCK %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock), rc==SQLITE_OK ? "ok" : "failed")); #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS - if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){ + if( (rc & 0xff) == SQLITE_IOERR ){ rc = SQLITE_BUSY; } #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ @@ -2358,23 +2733,23 @@ static int flockLock(sqlite3_file *id, int eFileLock) { */ static int flockUnlock(sqlite3_file *id, int eFileLock) { unixFile *pFile = (unixFile*)id; - + assert( pFile ); OSTRACE(("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock, pFile->eFileLock, osGetpid(0))); assert( eFileLock<=SHARED_LOCK ); - + /* no-op if possible */ if( pFile->eFileLock==eFileLock ){ return SQLITE_OK; } - + /* shared can just be set because we always have an exclusive */ if (eFileLock==SHARED_LOCK) { pFile->eFileLock = eFileLock; return SQLITE_OK; } - + /* no, really, unlock. */ if( robust_flock(pFile->h, LOCK_UN) ){ #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS @@ -2425,14 +2800,14 @@ static int semXCheckReservedLock(sqlite3_file *id, int *pResOut) { unixFile *pFile = (unixFile*)id; SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); - + assert( pFile ); /* Check if a thread in this process holds such a lock */ if( pFile->eFileLock>SHARED_LOCK ){ reserved = 1; } - + /* Otherwise see if some other process holds it. */ if( !reserved ){ sem_t *pSem = pFile->pInode->pSem; @@ -2491,14 +2866,14 @@ static int semXLock(sqlite3_file *id, int eFileLock) { sem_t *pSem = pFile->pInode->pSem; int rc = SQLITE_OK; - /* if we already have a lock, it is exclusive. + /* if we already have a lock, it is exclusive. ** Just adjust level and punt on outta here. */ if (pFile->eFileLock > NO_LOCK) { pFile->eFileLock = eFileLock; rc = SQLITE_OK; goto sem_end_lock; } - + /* lock semaphore now but bail out when already locked. */ if( sem_trywait(pSem)==-1 ){ rc = SQLITE_BUSY; @@ -2528,18 +2903,18 @@ static int semXUnlock(sqlite3_file *id, int eFileLock) { OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock, pFile->eFileLock, osGetpid(0))); assert( eFileLock<=SHARED_LOCK ); - + /* no-op if possible */ if( pFile->eFileLock==eFileLock ){ return SQLITE_OK; } - + /* shared can just be set because we always have an exclusive */ if (eFileLock==SHARED_LOCK) { pFile->eFileLock = eFileLock; return SQLITE_OK; } - + /* no, really unlock. */ if ( sem_post(pSem)==-1 ) { int rc, tErrno = errno; @@ -2547,7 +2922,7 @@ static int semXUnlock(sqlite3_file *id, int eFileLock) { if( IS_LOCK_ERROR(rc) ){ storeLastErrno(pFile, tErrno); } - return rc; + return rc; } pFile->eFileLock = NO_LOCK; return SQLITE_OK; @@ -2561,6 +2936,7 @@ static int semXClose(sqlite3_file *id) { unixFile *pFile = (unixFile*)id; semXUnlock(id, NO_LOCK); assert( pFile ); + assert( unixFileMutexNotheld(pFile) ); unixEnterMutex(); releaseInodeInfo(pFile); unixLeaveMutex(); @@ -2612,7 +2988,7 @@ struct ByteRangeLockPB2 /* ** This is a utility for setting or clearing a bit-range lock on an ** AFP filesystem. -** +** ** Return SQLITE_OK on success, SQLITE_BUSY on failure. */ static int afpSetLock( @@ -2624,14 +3000,14 @@ static int afpSetLock( ){ struct ByteRangeLockPB2 pb; int err; - + pb.unLockFlag = setLockFlag ? 0 : 1; pb.startEndFlag = 0; pb.offset = offset; - pb.length = length; + pb.length = length; pb.fd = pFile->h; - - OSTRACE(("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n", + + OSTRACE(("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n", (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""), offset, length)); err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0); @@ -2666,27 +3042,26 @@ static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){ int reserved = 0; unixFile *pFile = (unixFile*)id; afpLockingContext *context; - + SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); - + assert( pFile ); context = (afpLockingContext *) pFile->lockingContext; if( context->reserved ){ *pResOut = 1; return SQLITE_OK; } - unixEnterMutex(); /* Because pFile->pInode is shared across threads */ - + sqlite3_mutex_enter(pFile->pInode->pLockMutex); /* Check if a thread in this process holds such a lock */ if( pFile->pInode->eFileLock>SHARED_LOCK ){ reserved = 1; } - + /* Otherwise see if some other process holds it. */ if( !reserved ){ /* lock the RESERVED byte */ - int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1); + int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1); if( SQLITE_OK==lrc ){ /* if we succeeded in taking the reserved lock, unlock it to restore ** the original state */ @@ -2699,10 +3074,10 @@ static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){ rc=lrc; } } - - unixLeaveMutex(); + + sqlite3_mutex_leave(pFile->pInode->pLockMutex); OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved)); - + *pResOut = reserved; return rc; } @@ -2736,7 +3111,7 @@ static int afpLock(sqlite3_file *id, int eFileLock){ unixFile *pFile = (unixFile*)id; unixInodeInfo *pInode = pFile->pInode; afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; - + assert( pFile ); OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h, azFileLock(eFileLock), azFileLock(pFile->eFileLock), @@ -2754,33 +3129,33 @@ static int afpLock(sqlite3_file *id, int eFileLock){ /* Make sure the locking sequence is correct ** (1) We never move from unlocked to anything higher than shared lock. - ** (2) SQLite never explicitly requests a pendig lock. + ** (2) SQLite never explicitly requests a pending lock. ** (3) A shared lock is always held when a reserve lock is requested. */ assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK ); assert( eFileLock!=PENDING_LOCK ); assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK ); - + /* This mutex is needed because pFile->pInode is shared across threads */ - unixEnterMutex(); pInode = pFile->pInode; + sqlite3_mutex_enter(pInode->pLockMutex); /* If some thread using this PID has a lock via a different unixFile* ** handle that precludes the requested lock, return BUSY. */ - if( (pFile->eFileLock!=pInode->eFileLock && + if( (pFile->eFileLock!=pInode->eFileLock && (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK)) ){ rc = SQLITE_BUSY; goto afp_end_lock; } - + /* If a SHARED lock is requested, and some thread using this PID already ** has a SHARED or RESERVED lock, then increment reference counts and ** return SQLITE_OK. */ - if( eFileLock==SHARED_LOCK && + if( eFileLock==SHARED_LOCK && (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){ assert( eFileLock==SHARED_LOCK ); assert( pFile->eFileLock==0 ); @@ -2790,12 +3165,12 @@ static int afpLock(sqlite3_file *id, int eFileLock){ pInode->nLock++; goto afp_end_lock; } - + /* A PENDING lock is needed before acquiring a SHARED lock and before ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will ** be released. */ - if( eFileLock==SHARED_LOCK + if( eFileLock==SHARED_LOCK || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLocknShared==0 ); assert( pInode->eFileLock==0 ); - + mask = (sizeof(long)==8) ? LARGEST_INT64 : 0x7fffffff; /* Now get the read-lock SHARED_LOCK */ /* note that the quality of the randomness doesn't matter that much */ - lk = random(); + lk = random(); pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1); - lrc1 = afpSetLock(context->dbPath, pFile, + lrc1 = afpSetLock(context->dbPath, pFile, SHARED_FIRST+pInode->sharedByte, 1, 1); if( IS_LOCK_ERROR(lrc1) ){ lrc1Errno = pFile->lastErrno; } /* Drop the temporary PENDING lock */ lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0); - + if( IS_LOCK_ERROR(lrc1) ) { storeLastErrno(pFile, lrc1Errno); rc = lrc1; @@ -2863,34 +3238,34 @@ static int afpLock(sqlite3_file *id, int eFileLock){ } if (!failed && eFileLock == EXCLUSIVE_LOCK) { /* Acquire an EXCLUSIVE lock */ - - /* Remove the shared lock before trying the range. we'll need to + + /* Remove the shared lock before trying the range. we'll need to ** reestablish the shared lock if we can't get the afpUnlock */ if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST + pInode->sharedByte, 1, 0)) ){ int failed2 = SQLITE_OK; - /* now attemmpt to get the exclusive lock range */ - failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST, + /* now attempt to get the exclusive lock range */ + failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 1); - if( failed && (failed2 = afpSetLock(context->dbPath, pFile, + if( failed && (failed2 = afpSetLock(context->dbPath, pFile, SHARED_FIRST + pInode->sharedByte, 1, 1)) ){ /* Can't reestablish the shared lock. Sqlite can't deal, this is ** a critical I/O error */ - rc = ((failed & SQLITE_IOERR) == SQLITE_IOERR) ? failed2 : + rc = ((failed & 0xff) == SQLITE_IOERR) ? failed2 : SQLITE_IOERR_LOCK; goto afp_end_lock; - } + } }else{ - rc = failed; + rc = failed; } } if( failed ){ rc = failed; } } - + if( rc==SQLITE_OK ){ pFile->eFileLock = eFileLock; pInode->eFileLock = eFileLock; @@ -2898,10 +3273,10 @@ static int afpLock(sqlite3_file *id, int eFileLock){ pFile->eFileLock = PENDING_LOCK; pInode->eFileLock = PENDING_LOCK; } - + afp_end_lock: - unixLeaveMutex(); - OSTRACE(("LOCK %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock), + sqlite3_mutex_leave(pInode->pLockMutex); + OSTRACE(("LOCK %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock), rc==SQLITE_OK ? "ok" : "failed")); return rc; } @@ -2919,9 +3294,6 @@ static int afpUnlock(sqlite3_file *id, int eFileLock) { unixInodeInfo *pInode; afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; int skipShared = 0; -#ifdef SQLITE_TEST - int h = pFile->h; -#endif assert( pFile ); OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock, @@ -2932,15 +3304,12 @@ static int afpUnlock(sqlite3_file *id, int eFileLock) { if( pFile->eFileLock<=eFileLock ){ return SQLITE_OK; } - unixEnterMutex(); pInode = pFile->pInode; + sqlite3_mutex_enter(pInode->pLockMutex); assert( pInode->nShared!=0 ); if( pFile->eFileLock>SHARED_LOCK ){ assert( pInode->eFileLock==pFile->eFileLock ); - SimulateIOErrorBenign(1); - SimulateIOError( h=(-1) ) - SimulateIOErrorBenign(0); - + #ifdef SQLITE_DEBUG /* When reducing a lock such that other processes can start ** reading the database file again, make sure that the @@ -2955,7 +3324,7 @@ static int afpUnlock(sqlite3_file *id, int eFileLock) { || pFile->transCntrChng==1 ); pFile->inNormalWrite = 0; #endif - + if( pFile->eFileLock==EXCLUSIVE_LOCK ){ rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0); if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){ @@ -2968,11 +3337,11 @@ static int afpUnlock(sqlite3_file *id, int eFileLock) { } if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){ rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0); - } + } if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){ rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0); - if( !rc ){ - context->reserved = 0; + if( !rc ){ + context->reserved = 0; } } if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){ @@ -2988,9 +3357,6 @@ static int afpUnlock(sqlite3_file *id, int eFileLock) { unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte; pInode->nShared--; if( pInode->nShared==0 ){ - SimulateIOErrorBenign(1); - SimulateIOError( h=(-1) ) - SimulateIOErrorBenign(0); if( !skipShared ){ rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0); } @@ -3002,33 +3368,39 @@ static int afpUnlock(sqlite3_file *id, int eFileLock) { if( rc==SQLITE_OK ){ pInode->nLock--; assert( pInode->nLock>=0 ); - if( pInode->nLock==0 ){ - closePendingFds(pFile); - } + if( pInode->nLock==0 ) closePendingFds(pFile); } } - - unixLeaveMutex(); - if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock; + + sqlite3_mutex_leave(pInode->pLockMutex); + if( rc==SQLITE_OK ){ + pFile->eFileLock = eFileLock; + } return rc; } /* -** Close a file & cleanup AFP specific locking context +** Close a file & cleanup AFP specific locking context */ static int afpClose(sqlite3_file *id) { int rc = SQLITE_OK; unixFile *pFile = (unixFile*)id; assert( id!=0 ); afpUnlock(id, NO_LOCK); + assert( unixFileMutexNotheld(pFile) ); unixEnterMutex(); - if( pFile->pInode && pFile->pInode->nLock ){ - /* If there are outstanding locks, do not actually close the file just - ** yet because that would clear those locks. Instead, add the file - ** descriptor to pInode->aPending. It will be automatically closed when - ** the last lock is cleared. - */ - setPendingFd(pFile); + if( pFile->pInode ){ + unixInodeInfo *pInode = pFile->pInode; + sqlite3_mutex_enter(pInode->pLockMutex); + if( pInode->nLock ){ + /* If there are outstanding locks, do not actually close the file just + ** yet because that would clear those locks. Instead, add the file + ** descriptor to pInode->aPending. It will be automatically closed when + ** the last lock is cleared. + */ + setPendingFd(pFile); + } + sqlite3_mutex_leave(pInode->pLockMutex); } releaseInodeInfo(pFile); sqlite3_free(pFile->lockingContext); @@ -3066,7 +3438,7 @@ static int nfsUnlock(sqlite3_file *id, int eFileLock){ /* ** The code above is the NFS lock implementation. The code is specific ** to MacOSX and does not work on other unix platforms. No alternative -** is available. +** is available. ** ********************* End of the NFS lock implementation ********************** ******************************************************************************/ @@ -3074,7 +3446,7 @@ static int nfsUnlock(sqlite3_file *id, int eFileLock){ /****************************************************************************** **************** Non-locking sqlite3_file methods ***************************** ** -** The next division contains implementations for all methods of the +** The next division contains implementations for all methods of the ** sqlite3_file object other than the locking methods. The locking ** methods were defined in divisions above (one locking method per ** division). Those methods that are common to all locking modes @@ -3082,15 +3454,9 @@ static int nfsUnlock(sqlite3_file *id, int eFileLock){ */ /* -** Seek to the offset passed as the second argument, then read cnt +** Seek to the offset passed as the second argument, then read cnt ** bytes into pBuf. Return the number of bytes actually read. ** -** NB: If you define USE_PREAD or USE_PREAD64, then it might also -** be necessary to define _XOPEN_SOURCE to be 500. This varies from -** one system to another. Since SQLite does not define USE_PREAD -** in any form by default, we will not attempt to define _XOPEN_SOURCE. -** See tickets #2741 and #2681. -** ** To avoid stomping the errno value on a failed read the lastErrno value ** is set before returning. */ @@ -3144,8 +3510,8 @@ static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){ ** wrong. */ static int unixRead( - sqlite3_file *id, - void *pBuf, + sqlite3_file *id, + void *pBuf, int amt, sqlite3_int64 offset ){ @@ -3155,17 +3521,17 @@ static int unixRead( assert( offset>=0 ); assert( amt>0 ); - /* If this is a database file (not a journal, master-journal or temp + /* If this is a database file (not a journal, super-journal or temp ** file), the bytes in the locking range should never be read or written. */ #if 0 - assert( pFile->pUnused==0 + assert( pFile->pPreallocatedUnused==0 || offset>=PENDING_BYTE+512 - || offset+amt<=PENDING_BYTE + || offset+amt<=PENDING_BYTE ); #endif #if SQLITE_MAX_MMAP_SIZE>0 - /* Deal with as much of this read request as possible by transfering + /* Deal with as much of this read request as possible by transferring ** data from the memory mapping using memcpy(). */ if( offsetmmapSize ){ if( offset+amt <= pFile->mmapSize ){ @@ -3185,7 +3551,24 @@ static int unixRead( if( got==amt ){ return SQLITE_OK; }else if( got<0 ){ - /* lastErrno set by seekAndRead */ + /* pFile->lastErrno has been set by seekAndRead(). + ** Usually we return SQLITE_IOERR_READ here, though for some + ** kinds of errors we return SQLITE_IOERR_CORRUPTFS. The + ** SQLITE_IOERR_CORRUPTFS will be converted into SQLITE_CORRUPT + ** prior to returning to the application by the sqlite3ApiExit() + ** routine. + */ + switch( pFile->lastErrno ){ + case ERANGE: + case EIO: +#ifdef ENXIO + case ENXIO: +#endif +#ifdef EDEVERR + case EDEVERR: +#endif + return SQLITE_IOERR_CORRUPTFS; + } return SQLITE_IOERR_READ; }else{ storeLastErrno(pFile, 0); /* not a system error */ @@ -3198,7 +3581,7 @@ static int unixRead( /* ** Attempt to seek the file-descriptor passed as the first argument to ** absolute offset iOff, then attempt to write nBuf bytes of data from -** pBuf to it. If an error occurs, return -1 and set *piErrno. Otherwise, +** pBuf to it. If an error occurs, return -1 and set *piErrno. Otherwise, ** return the actual number of bytes written (which may be less than ** nBuf). */ @@ -3258,22 +3641,22 @@ static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){ ** or some other error code on failure. */ static int unixWrite( - sqlite3_file *id, - const void *pBuf, + sqlite3_file *id, + const void *pBuf, int amt, - sqlite3_int64 offset + sqlite3_int64 offset ){ unixFile *pFile = (unixFile*)id; int wrote = 0; assert( id ); assert( amt>0 ); - /* If this is a database file (not a journal, master-journal or temp + /* If this is a database file (not a journal, super-journal or temp ** file), the bytes in the locking range should never be read or written. */ #if 0 - assert( pFile->pUnused==0 + assert( pFile->pPreallocatedUnused==0 || offset>=PENDING_BYTE+512 - || offset+amt<=PENDING_BYTE + || offset+amt<=PENDING_BYTE ); #endif @@ -3300,7 +3683,7 @@ static int unixWrite( #endif #if defined(SQLITE_MMAP_READWRITE) && SQLITE_MAX_MMAP_SIZE>0 - /* Deal with as much of this write request as possible by transfering + /* Deal with as much of this write request as possible by transferring ** data from the memory mapping using memcpy(). */ if( offsetmmapSize ){ if( offset+amt <= pFile->mmapSize ){ @@ -3315,7 +3698,7 @@ static int unixWrite( } } #endif - + while( (wrote = seekAndWrite(pFile, offset, pBuf, amt))0 ){ amt -= wrote; offset += wrote; @@ -3381,8 +3764,8 @@ int sqlite3_fullsync_count = 0; ** ** SQLite sets the dataOnly flag if the size of the file is unchanged. ** The idea behind dataOnly is that it should only write the file content -** to disk, not the inode. We only set dataOnly if the file size is -** unchanged since the file size is part of the inode. However, +** to disk, not the inode. We only set dataOnly if the file size is +** unchanged since the file size is part of the inode. However, ** Ted Ts'o tells us that fdatasync() will also write the inode if the ** file size has changed. The only real difference between fdatasync() ** and fsync(), Ted tells us, is that fdatasync() will not flush the @@ -3396,7 +3779,7 @@ static int full_fsync(int fd, int fullSync, int dataOnly){ int rc; /* The following "ifdef/elif/else/" block has the same structure as - ** the one below. It is replicated here solely to avoid cluttering + ** the one below. It is replicated here solely to avoid cluttering ** up the real code with the UNUSED_PARAMETER() macros. */ #ifdef SQLITE_NO_SYNC @@ -3410,7 +3793,7 @@ static int full_fsync(int fd, int fullSync, int dataOnly){ UNUSED_PARAMETER(dataOnly); #endif - /* Record the number of times that we do a normal fsync() and + /* Record the number of times that we do a normal fsync() and ** FULLSYNC. This is used during testing to verify that this procedure ** gets called with the correct arguments. */ @@ -3422,7 +3805,7 @@ static int full_fsync(int fd, int fullSync, int dataOnly){ /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a ** no-op. But go ahead and call fstat() to validate the file ** descriptor as we need a method to provoke a failure during - ** coverate testing. + ** coverage testing. */ #ifdef SQLITE_NO_SYNC { @@ -3436,11 +3819,11 @@ static int full_fsync(int fd, int fullSync, int dataOnly){ rc = 1; } /* If the FULLFSYNC failed, fall back to attempting an fsync(). - ** It shouldn't be possible for fullfsync to fail on the local + ** It shouldn't be possible for fullfsync to fail on the local ** file system (on OSX), so failure indicates that FULLFSYNC - ** isn't supported for this file system. So, attempt an fsync - ** and (for now) ignore the overhead of a superfluous fcntl call. - ** It'd be better to detect fullfsync support once and avoid + ** isn't supported for this file system. So, attempt an fsync + ** and (for now) ignore the overhead of a superfluous fcntl call. + ** It'd be better to detect fullfsync support once and avoid ** the fcntl call every time sync is called. */ if( rc ) rc = fsync(fd); @@ -3450,7 +3833,7 @@ static int full_fsync(int fd, int fullSync, int dataOnly){ ** so currently we default to the macro that redefines fdatasync to fsync */ rc = fsync(fd); -#else +#else rc = fdatasync(fd); #if OS_VXWORKS if( rc==-1 && errno==ENOTSUP ){ @@ -3611,7 +3994,7 @@ static int unixTruncate(sqlite3_file *id, i64 nByte){ #if SQLITE_MAX_MMAP_SIZE>0 /* If the file was just truncated to a size smaller than the currently ** mapped region, reduce the effective mapping size as well. SQLite will - ** use read() and write() to access data beyond this point from now on. + ** use read() and write() to access data beyond this point from now on. */ if( nBytemmapSize ){ pFile->mmapSize = nByte; @@ -3657,8 +4040,8 @@ static int unixFileSize(sqlite3_file *id, i64 *pSize){ static int proxyFileControl(sqlite3_file*,int,void*); #endif -/* -** This function is called to handle the SQLITE_FCNTL_SIZE_HINT +/* +** This function is called to handle the SQLITE_FCNTL_SIZE_HINT ** file-control operation. Enlarge the database to nBytes in size ** (rounded up to the next chunk-size). If the database is already ** nBytes or larger, this routine is a no-op. @@ -3667,7 +4050,7 @@ static int fcntlSizeHint(unixFile *pFile, i64 nByte){ if( pFile->szChunk>0 ){ i64 nSize; /* Required file size */ struct stat buf; /* Used to hold return values of fstat() */ - + if( osFstat(pFile->h, &buf) ){ return SQLITE_IOERR_FSTAT; } @@ -3676,16 +4059,16 @@ static int fcntlSizeHint(unixFile *pFile, i64 nByte){ if( nSize>(i64)buf.st_size ){ #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE - /* The code below is handling the return value of osFallocate() - ** correctly. posix_fallocate() is defined to "returns zero on success, + /* The code below is handling the return value of osFallocate() + ** correctly. posix_fallocate() is defined to "returns zero on success, ** or an error number on failure". See the manpage for details. */ int err; do{ err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size); }while( err==EINTR ); - if( err ) return SQLITE_IOERR_WRITE; + if( err && err!=EINVAL ) return SQLITE_IOERR_WRITE; #else - /* If the OS does not have posix_fallocate(), fake it. Write a + /* If the OS does not have posix_fallocate(), fake it. Write a ** single byte to the last byte in each block that falls entirely ** within the extended region. Then, if required, a single byte ** at offset (nSize-1), to set the size of the file correctly. @@ -3744,6 +4127,13 @@ static void unixModeBit(unixFile *pFile, unsigned char mask, int *pArg){ /* Forward declaration */ static int unixGetTempname(int nBuf, char *zBuf); +#if !defined(SQLITE_WASI) && !defined(SQLITE_OMIT_WAL) + static int unixFcntlExternalReader(unixFile*, int*); +#endif +#if defined(SQLITE_DEBUG) || defined(SQLITE_ENABLE_FILESTAT) + static void unixDescribeShm(sqlite3_str*,unixShm*); +#endif + /* ** Information and control of an open file handle. @@ -3751,6 +4141,26 @@ static int unixGetTempname(int nBuf, char *zBuf); static int unixFileControl(sqlite3_file *id, int op, void *pArg){ unixFile *pFile = (unixFile*)id; switch( op ){ +#if defined(__linux__) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE) + case SQLITE_FCNTL_BEGIN_ATOMIC_WRITE: { + int rc = osIoctl(pFile->h, F2FS_IOC_START_ATOMIC_WRITE); + return rc ? SQLITE_IOERR_BEGIN_ATOMIC : SQLITE_OK; + } + case SQLITE_FCNTL_COMMIT_ATOMIC_WRITE: { + int rc = osIoctl(pFile->h, F2FS_IOC_COMMIT_ATOMIC_WRITE); + return rc ? SQLITE_IOERR_COMMIT_ATOMIC : SQLITE_OK; + } + case SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE: { + int rc = osIoctl(pFile->h, F2FS_IOC_ABORT_VOLATILE_WRITE); + return rc ? SQLITE_IOERR_ROLLBACK_ATOMIC : SQLITE_OK; + } +#endif /* __linux__ && SQLITE_ENABLE_BATCH_ATOMIC_WRITE */ + + case SQLITE_FCNTL_NULL_IO: { + osClose(pFile->h); + pFile->h = -1; + return SQLITE_OK; + } case SQLITE_FCNTL_LOCKSTATE: { *(int*)pArg = pFile->eFileLock; return SQLITE_OK; @@ -3794,6 +4204,26 @@ static int unixFileControl(sqlite3_file *id, int op, void *pArg){ *(int*)pArg = fileHasMoved(pFile); return SQLITE_OK; } +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT + case SQLITE_FCNTL_LOCK_TIMEOUT: { + int iOld = pFile->iBusyTimeout; + int iNew = *(int*)pArg; +#if SQLITE_ENABLE_SETLK_TIMEOUT==1 + pFile->iBusyTimeout = iNew<0 ? 0x7FFFFFFF : (unsigned)iNew; +#elif SQLITE_ENABLE_SETLK_TIMEOUT==2 + pFile->iBusyTimeout = !!(*(int*)pArg); +#else +# error "SQLITE_ENABLE_SETLK_TIMEOUT must be set to 1 or 2" +#endif + *(int*)pArg = iOld; + return SQLITE_OK; + } + case SQLITE_FCNTL_BLOCK_ON_CONNECT: { + int iNew = *(int*)pArg; + pFile->bBlockOnConnect = iNew; + return SQLITE_OK; + } +#endif /* SQLITE_ENABLE_SETLK_TIMEOUT */ #if SQLITE_MAX_MMAP_SIZE>0 case SQLITE_FCNTL_MMAP_SIZE: { i64 newLimit = *(i64*)pArg; @@ -3801,6 +4231,14 @@ static int unixFileControl(sqlite3_file *id, int op, void *pArg){ if( newLimit>sqlite3GlobalConfig.mxMmap ){ newLimit = sqlite3GlobalConfig.mxMmap; } + + /* The value of newLimit may be eventually cast to (size_t) and passed + ** to mmap(). Restrict its value to 2GB if (size_t) is not at least a + ** 64-bit type. */ + if( newLimit>0 && sizeof(size_t)<8 ){ + newLimit = (newLimit & 0x7FFFFFFF); + } + *(i64*)pArg = pFile->mmapSizeMax; if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){ pFile->mmapSizeMax = newLimit; @@ -3829,43 +4267,124 @@ static int unixFileControl(sqlite3_file *id, int op, void *pArg){ return proxyFileControl(id,op,pArg); } #endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */ + + case SQLITE_FCNTL_EXTERNAL_READER: { +#if !defined(SQLITE_WASI) && !defined(SQLITE_OMIT_WAL) + return unixFcntlExternalReader((unixFile*)id, (int*)pArg); +#else + *(int*)pArg = 0; + return SQLITE_OK; +#endif + } + +#if defined(SQLITE_DEBUG) || defined(SQLITE_ENABLE_FILESTAT) + case SQLITE_FCNTL_FILESTAT: { + sqlite3_str *pStr = (sqlite3_str*)pArg; + char aLck[16]; + unixInodeInfo *pInode; + static const char *azLock[] = { "SHARED", "RESERVED", + "PENDING", "EXCLUSIVE" }; + sqlite3_str_appendf(pStr, "{\"h\":%d", pFile->h); + sqlite3_str_appendf(pStr, ",\"vfs\":\"%s\"", pFile->pVfs->zName); + if( pFile->eFileLock ){ + sqlite3_str_appendf(pStr, ",\"eFileLock\":\"%s\"", + azLock[pFile->eFileLock-1]); + if( unixPosixAdvisoryLocks(pFile->h, aLck)==SQLITE_OK ){ + sqlite3_str_appendf(pStr, ",\"pal\":\"%s\"", aLck); + } + } + unixEnterMutex(); + if( pFile->pShm ){ + sqlite3_str_appendall(pStr, ",\"shm\":"); + unixDescribeShm(pStr, pFile->pShm); + } +#if SQLITE_MAX_MMAP_SIZE>0 + if( pFile->mmapSize ){ + sqlite3_str_appendf(pStr, ",\"mmapSize\":%lld", pFile->mmapSize); + sqlite3_str_appendf(pStr, ",\"nFetchOut\":%d", pFile->nFetchOut); + } +#endif + if( (pInode = pFile->pInode)!=0 ){ + sqlite3_str_appendf(pStr, ",\"inode\":{\"nRef\":%d",pInode->nRef); + sqlite3_mutex_enter(pInode->pLockMutex); + sqlite3_str_appendf(pStr, ",\"nShared\":%d", pInode->nShared); + if( pInode->eFileLock ){ + sqlite3_str_appendf(pStr, ",\"eFileLock\":\"%s\"", + azLock[pInode->eFileLock-1]); + } + if( pInode->pUnused ){ + char cSep = '['; + UnixUnusedFd *pUFd = pFile->pInode->pUnused; + sqlite3_str_appendall(pStr, ",\"unusedFd\":"); + while( pUFd ){ + sqlite3_str_appendf(pStr, "%c{\"fd\":%d,\"flags\":%d", + cSep, pUFd->fd, pUFd->flags); + cSep = ','; + if( unixPosixAdvisoryLocks(pUFd->fd, aLck)==SQLITE_OK ){ + sqlite3_str_appendf(pStr, ",\"pal\":\"%s\"", aLck); + } + sqlite3_str_append(pStr, "}", 1); + pUFd = pUFd->pNext; + } + sqlite3_str_append(pStr, "]", 1); + } + sqlite3_mutex_leave(pInode->pLockMutex); + sqlite3_str_append(pStr, "}", 1); + } + unixLeaveMutex(); + sqlite3_str_append(pStr, "}", 1); + return SQLITE_OK; + } +#endif /* SQLITE_DEBUG || SQLITE_ENABLE_FILESTAT */ } return SQLITE_NOTFOUND; } /* -** Return the sector size in bytes of the underlying block device for -** the specified file. This is almost always 512 bytes, but may be -** larger for some devices. +** If pFd->sectorSize is non-zero when this function is called, it is a +** no-op. Otherwise, the values of pFd->sectorSize and +** pFd->deviceCharacteristics are set according to the file-system +** characteristics. ** -** SQLite code assumes this function cannot fail. It also assumes that -** if two files are created in the same file-system directory (i.e. -** a database and its journal file) that the sector size will be the -** same for both. +** There are two versions of this function. One for QNX and one for all +** other systems. */ -#ifndef __QNXNTO__ -static int unixSectorSize(sqlite3_file *NotUsed){ - UNUSED_PARAMETER(NotUsed); - return SQLITE_DEFAULT_SECTOR_SIZE; -} -#endif +#ifndef __QNXNTO__ +static void setDeviceCharacteristics(unixFile *pFd){ + assert( pFd->deviceCharacteristics==0 || pFd->sectorSize!=0 ); + if( pFd->sectorSize==0 ){ +#if defined(__linux__) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE) + int res; + u32 f = 0; -/* -** The following version of unixSectorSize() is optimized for QNX. -*/ -#ifdef __QNXNTO__ + /* Check for support for F2FS atomic batch writes. */ + res = osIoctl(pFd->h, F2FS_IOC_GET_FEATURES, &f); + if( res==0 && (f & F2FS_FEATURE_ATOMIC_WRITE) ){ + pFd->deviceCharacteristics = SQLITE_IOCAP_BATCH_ATOMIC; + } +#endif /* __linux__ && SQLITE_ENABLE_BATCH_ATOMIC_WRITE */ + + /* Set the POWERSAFE_OVERWRITE flag if requested. */ + if( pFd->ctrlFlags & UNIXFILE_PSOW ){ + pFd->deviceCharacteristics |= SQLITE_IOCAP_POWERSAFE_OVERWRITE; + } + pFd->deviceCharacteristics |= SQLITE_IOCAP_SUBPAGE_READ; + + pFd->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE; + } +} +#else #include #include -static int unixSectorSize(sqlite3_file *id){ - unixFile *pFile = (unixFile*)id; +static void setDeviceCharacteristics(unixFile *pFile){ if( pFile->sectorSize == 0 ){ struct statvfs fsInfo; - + /* Set defaults for non-supported filesystems */ pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE; pFile->deviceCharacteristics = 0; if( fstatvfs(pFile->h, &fsInfo) == -1 ) { - return pFile->sectorSize; + return; } if( !strcmp(fsInfo.f_basetype, "tmp") ) { @@ -3900,7 +4419,7 @@ static int unixSectorSize(sqlite3_file *id){ pFile->sectorSize = fsInfo.f_bsize; pFile->deviceCharacteristics = /* full bitset of atomics from max sector size and smaller */ - ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 | + (((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2) | SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind ** so it is ordered */ 0; @@ -3908,7 +4427,7 @@ static int unixSectorSize(sqlite3_file *id){ pFile->sectorSize = fsInfo.f_bsize; pFile->deviceCharacteristics = /* full bitset of atomics from max sector size and smaller */ - ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 | + (((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2) | SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind ** so it is ordered */ 0; @@ -3926,9 +4445,24 @@ static int unixSectorSize(sqlite3_file *id){ pFile->deviceCharacteristics = 0; pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE; } - return pFile->sectorSize; } -#endif /* __QNXNTO__ */ +#endif + +/* +** Return the sector size in bytes of the underlying block device for +** the specified file. This is almost always 512 bytes, but may be +** larger for some devices. +** +** SQLite code assumes this function cannot fail. It also assumes that +** if two files are created in the same file-system directory (i.e. +** a database and its journal file) that the sector size will be the +** same for both. +*/ +static int unixSectorSize(sqlite3_file *id){ + unixFile *pFd = (unixFile*)id; + setDeviceCharacteristics(pFd); + return pFd->sectorSize; +} /* ** Return the device characteristics for the file. @@ -3944,16 +4478,9 @@ static int unixSectorSize(sqlite3_file *id){ ** available to turn it off and URI query parameter available to turn it off. */ static int unixDeviceCharacteristics(sqlite3_file *id){ - unixFile *p = (unixFile*)id; - int rc = 0; -#ifdef __QNXNTO__ - if( p->sectorSize==0 ) unixSectorSize(id); - rc = p->deviceCharacteristics; -#endif - if( p->ctrlFlags & UNIXFILE_PSOW ){ - rc |= SQLITE_IOCAP_POWERSAFE_OVERWRITE; - } - return rc; + unixFile *pFd = (unixFile*)id; + setDeviceCharacteristics(pFd); + return pFd->deviceCharacteristics; } #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 @@ -3961,7 +4488,7 @@ static int unixDeviceCharacteristics(sqlite3_file *id){ /* ** Return the system page size. ** -** This function should not be called directly by other code in this file. +** This function should not be called directly by other code in this file. ** Instead, it should be called via macro osGetpagesize(). */ static int unixGetpagesize(void){ @@ -3976,10 +4503,10 @@ static int unixGetpagesize(void){ #endif /* !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 */ -#ifndef SQLITE_OMIT_WAL +#if !defined(SQLITE_WASI) && !defined(SQLITE_OMIT_WAL) /* -** Object used to represent an shared memory buffer. +** Object used to represent an shared memory buffer. ** ** When multiple threads all reference the same wal-index, each thread ** has its own unixShm object, but they all point to a single instance @@ -3999,28 +4526,50 @@ static int unixGetpagesize(void){ ** nRef ** ** The following fields are read-only after the object is created: -** -** fid +** +** hShm ** zFilename ** -** Either unixShmNode.mutex must be held or unixShmNode.nRef==0 and +** Either unixShmNode.pShmMutex must be held or unixShmNode.nRef==0 and ** unixMutexHeld() is true when reading or writing any other field ** in this structure. +** +** aLock[SQLITE_SHM_NLOCK]: +** This array records the various locks held by clients on each of the +** SQLITE_SHM_NLOCK slots. If the aLock[] entry is set to 0, then no +** locks are held by the process on this slot. If it is set to -1, then +** some client holds an EXCLUSIVE lock on the locking slot. If the aLock[] +** value is set to a positive value, then it is the number of shared +** locks currently held on the slot. +** +** aMutex[SQLITE_SHM_NLOCK]: +** Normally, when SQLITE_ENABLE_SETLK_TIMEOUT is not defined, mutex +** pShmMutex is used to protect the aLock[] array and the right to +** call fcntl() on unixShmNode.hShm to obtain or release locks. +** +** If SQLITE_ENABLE_SETLK_TIMEOUT is defined though, we use an array +** of mutexes - one for each locking slot. To read or write locking +** slot aLock[iSlot], the caller must hold the corresponding mutex +** aMutex[iSlot]. Similarly, to call fcntl() to obtain or release a +** lock corresponding to slot iSlot, mutex aMutex[iSlot] must be held. */ struct unixShmNode { unixInodeInfo *pInode; /* unixInodeInfo that owns this SHM node */ - sqlite3_mutex *mutex; /* Mutex to access this object */ + sqlite3_mutex *pShmMutex; /* Mutex to access this object */ char *zFilename; /* Name of the mmapped file */ - int h; /* Open file descriptor */ + int hShm; /* Open file descriptor */ int szRegion; /* Size of shared-memory regions */ u16 nRegion; /* Size of array apRegion */ u8 isReadonly; /* True if read-only */ + u8 isUnlocked; /* True if no DMS lock held */ char **apRegion; /* Array of mapped shared-memory regions */ int nRef; /* Number of unixShm objects pointing to this */ unixShm *pFirst; /* All unixShm objects pointing to this */ +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT + sqlite3_mutex *aMutex[SQLITE_SHM_NLOCK]; +#endif + int aLock[SQLITE_SHM_NLOCK]; /* # shared locks on slot, -1==excl lock */ #ifdef SQLITE_DEBUG - u8 exclMask; /* Mask of exclusive locks held */ - u8 sharedMask; /* Mask of shared locks held */ u8 nextShmId; /* Next available unixShm.id value */ #endif }; @@ -4032,16 +4581,16 @@ struct unixShmNode { ** The following fields are initialized when this object is created and ** are read-only thereafter: ** -** unixShm.pFile +** unixShm.pShmNode ** unixShm.id ** -** All other fields are read/write. The unixShm.pFile->mutex must be held -** while accessing any read/write fields. +** All other fields are read/write. The unixShm.pShmNode->pShmMutex must +** be held while accessing any read/write fields. */ struct unixShm { unixShmNode *pShmNode; /* The underlying unixShmNode object */ unixShm *pNext; /* Next unixShm with the same unixShmNode */ - u8 hasMutex; /* True if holding the unixShmNode mutex */ + u8 hasMutex; /* True if holding the unixShmNode->pShmMutex */ u8 id; /* Id of this connection within its unixShmNode */ u16 sharedMask; /* Mask of shared locks held */ u16 exclMask; /* Mask of exclusive locks held */ @@ -4053,6 +4602,98 @@ struct unixShm { #define UNIX_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */ #define UNIX_SHM_DMS (UNIX_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */ +#if defined(SQLITE_DEBUG) || defined(SQLITE_ENABLE_FILESTAT) +/* +** Describe the pShm object using JSON. Used for diagnostics only. +*/ +static void unixDescribeShm(sqlite3_str *pStr, unixShm *pShm){ + unixShmNode *pNode = pShm->pShmNode; + char aLck[16]; + sqlite3_str_appendf(pStr, "{\"h\":%d", pNode->hShm); + assert( unixMutexHeld() ); + sqlite3_str_appendf(pStr, ",\"nRef\":%d", pNode->nRef); + sqlite3_str_appendf(pStr, ",\"id\":%d", pShm->id); + sqlite3_str_appendf(pStr, ",\"sharedMask\":%d", pShm->sharedMask); + sqlite3_str_appendf(pStr, ",\"exclMask\":%d", pShm->exclMask); + if( unixPosixAdvisoryLocks(pNode->hShm, aLck)==SQLITE_OK ){ + sqlite3_str_appendf(pStr, ",\"pal\":\"%s\"", aLck); + } + sqlite3_str_append(pStr, "}", 1); +} +#endif /* SQLITE_DEBUG || SQLITE_ENABLE_FILESTAT */ + +/* +** Use F_GETLK to check whether or not there are any readers with open +** wal-mode transactions in other processes on database file pFile. If +** no error occurs, return SQLITE_OK and set (*piOut) to 1 if there are +** such transactions, or 0 otherwise. If an error occurs, return an +** SQLite error code. The final value of *piOut is undefined in this +** case. +*/ +static int unixFcntlExternalReader(unixFile *pFile, int *piOut){ + int rc = SQLITE_OK; + *piOut = 0; + if( pFile->pShm){ + unixShmNode *pShmNode = pFile->pShm->pShmNode; + struct flock f; + + memset(&f, 0, sizeof(f)); + f.l_type = F_WRLCK; + f.l_whence = SEEK_SET; + f.l_start = UNIX_SHM_BASE + 3; + f.l_len = SQLITE_SHM_NLOCK - 3; + + sqlite3_mutex_enter(pShmNode->pShmMutex); + if( osFcntl(pShmNode->hShm, F_GETLK, &f)<0 ){ + rc = SQLITE_IOERR_LOCK; + }else{ + *piOut = (f.l_type!=F_UNLCK); + } + sqlite3_mutex_leave(pShmNode->pShmMutex); + } + + return rc; +} + +/* +** If pFile has a -shm file open and it is sharing that file with some +** other connection, either in the same process or in a separate process, +** then return true. Return false if either pFile does not have a -shm +** file open or if it is the only connection to that -shm file across the +** entire system. +** +** This routine is not required for correct operation. It can always return +** false and SQLite will continue to operate according to spec. However, +** when this routine does its job, it adds extra robustness in cases +** where database file locks have been erroneously deleted in a WAL-mode +** database by doing close(open(DATABASE_PATHNAME)) or similar. +** +** With false negatives, SQLite still operates to spec, though with less +** robustness. With false positives, the last database connection on a +** WAL-mode database will fail to unlink the -wal and -shm files, which +** is annoying but harmless. False positives will also prevent a database +** connection from running "PRAGMA journal_mode=DELETE" in order to take +** the database out of WAL mode, which is perhaps more serious, but is +** still not a disaster. +*/ +static int unixIsSharingShmNode(unixFile *pFile){ + unixShmNode *pShmNode; + struct flock lock; + if( pFile->pShm==0 ) return 0; + if( pFile->ctrlFlags & UNIXFILE_EXCL ) return 0; + pShmNode = pFile->pShm->pShmNode; +#if SQLITE_ATOMIC_INTRINSICS + assert( AtomicLoad(&pShmNode->nRef)==1 ); +#endif + memset(&lock, 0, sizeof(lock)); + lock.l_whence = SEEK_SET; + lock.l_start = UNIX_SHM_DMS; + lock.l_len = 1; + lock.l_type = F_WRLCK; + osFcntl(pShmNode->hShm, F_GETLK, &lock); + return (lock.l_type!=F_UNLCK); +} + /* ** Apply posix advisory locks for all bytes from ofst through ofst+n-1. ** @@ -4069,64 +4710,79 @@ static int unixShmSystemLock( struct flock f; /* The posix advisory locking structure */ int rc = SQLITE_OK; /* Result code form fcntl() */ - /* Access to the unixShmNode object is serialized by the caller */ pShmNode = pFile->pInode->pShmNode; - assert( sqlite3_mutex_held(pShmNode->mutex) || pShmNode->nRef==0 ); + + /* Assert that the parameters are within expected range and that the + ** correct mutex or mutexes are held. */ + assert( pShmNode->nRef>=0 ); + assert( (ofst==UNIX_SHM_DMS && n==1) + || (ofst>=UNIX_SHM_BASE && ofst+n<=(UNIX_SHM_BASE+SQLITE_SHM_NLOCK)) + ); + if( ofst==UNIX_SHM_DMS ){ + assert( pShmNode->nRef>0 || unixMutexHeld() ); + assert( pShmNode->nRef==0 || sqlite3_mutex_held(pShmNode->pShmMutex) ); + }else{ +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT + int ii; + for(ii=ofst-UNIX_SHM_BASE; iiaMutex[ii]) ); + } +#else + assert( sqlite3_mutex_held(pShmNode->pShmMutex) ); + assert( pShmNode->nRef>0 ); +#endif + } /* Shared locks never span more than one byte */ assert( n==1 || lockType!=F_RDLCK ); /* Locks are within range */ assert( n>=1 && n<=SQLITE_SHM_NLOCK ); + assert( ofst>=UNIX_SHM_BASE && ofst<=UNIX_SHM_DMS ); + assert( ofst+n-1<=UNIX_SHM_DMS ); - if( pShmNode->h>=0 ){ + if( pShmNode->hShm>=0 ){ + int res; /* Initialize the locking parameters */ - memset(&f, 0, sizeof(f)); f.l_type = lockType; f.l_whence = SEEK_SET; f.l_start = ofst; f.l_len = n; - - rc = osFcntl(pShmNode->h, F_SETLK, &f); - rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY; + res = osSetPosixAdvisoryLock(pShmNode->hShm, &f, pFile); + if( res==-1 ){ +#if defined(SQLITE_ENABLE_SETLK_TIMEOUT) && SQLITE_ENABLE_SETLK_TIMEOUT==1 + rc = (pFile->iBusyTimeout ? SQLITE_BUSY_TIMEOUT : SQLITE_BUSY); +#else + rc = SQLITE_BUSY; +#endif + } } - /* Update the global lock state and do debug tracing */ + /* Do debug tracing */ #ifdef SQLITE_DEBUG - { u16 mask; OSTRACE(("SHM-LOCK ")); - mask = ofst>31 ? 0xffff : (1<<(ofst+n)) - (1<exclMask &= ~mask; - pShmNode->sharedMask &= ~mask; + OSTRACE(("unlock %d..%d ok\n", ofst, ofst+n-1)); }else if( lockType==F_RDLCK ){ - OSTRACE(("read-lock %d ok", ofst)); - pShmNode->exclMask &= ~mask; - pShmNode->sharedMask |= mask; + OSTRACE(("read-lock %d..%d ok\n", ofst, ofst+n-1)); }else{ assert( lockType==F_WRLCK ); - OSTRACE(("write-lock %d ok", ofst)); - pShmNode->exclMask |= mask; - pShmNode->sharedMask &= ~mask; + OSTRACE(("write-lock %d..%d ok\n", ofst, ofst+n-1)); } }else{ if( lockType==F_UNLCK ){ - OSTRACE(("unlock %d failed", ofst)); + OSTRACE(("unlock %d..%d failed\n", ofst, ofst+n-1)); }else if( lockType==F_RDLCK ){ - OSTRACE(("read-lock failed")); + OSTRACE(("read-lock %d..%d failed\n", ofst, ofst+n-1)); }else{ assert( lockType==F_WRLCK ); - OSTRACE(("write-lock %d failed", ofst)); + OSTRACE(("write-lock %d..%d failed\n", ofst, ofst+n-1)); } } - OSTRACE((" - afterwards %03x,%03x\n", - pShmNode->sharedMask, pShmNode->exclMask)); - } #endif - return rc; + return rc; } /* @@ -4159,18 +4815,23 @@ static void unixShmPurge(unixFile *pFd){ int nShmPerMap = unixShmRegionPerMap(); int i; assert( p->pInode==pFd->pInode ); - sqlite3_mutex_free(p->mutex); + sqlite3_mutex_free(p->pShmMutex); +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT + for(i=0; iaMutex[i]); + } +#endif for(i=0; inRegion; i+=nShmPerMap){ - if( p->h>=0 ){ + if( p->hShm>=0 ){ osMunmap(p->apRegion[i], p->szRegion); }else{ sqlite3_free(p->apRegion[i]); } } sqlite3_free(p->apRegion); - if( p->h>=0 ){ - robust_close(pFd, p->h, __LINE__); - p->h = -1; + if( p->hShm>=0 ){ + robust_close(pFd, p->hShm, __LINE__); + p->hShm = -1; } p->pInode->pShmNode = 0; sqlite3_free(p); @@ -4178,20 +4839,96 @@ static void unixShmPurge(unixFile *pFd){ } /* -** Open a shared-memory area associated with open database file pDbFd. +** The DMS lock has not yet been taken on shm file pShmNode. Attempt to +** take it now. Return SQLITE_OK if successful, or an SQLite error +** code otherwise. +** +** If the DMS cannot be locked because this is a readonly_shm=1 +** connection and no other process already holds a lock, return +** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1. +*/ +static int unixLockSharedMemory(unixFile *pDbFd, unixShmNode *pShmNode){ + struct flock lock; + int rc = SQLITE_OK; + + /* Use F_GETLK to determine the locks other processes are holding + ** on the DMS byte. If it indicates that another process is holding + ** a SHARED lock, then this process may also take a SHARED lock + ** and proceed with opening the *-shm file. + ** + ** Or, if no other process is holding any lock, then this process + ** is the first to open it. In this case take an EXCLUSIVE lock on the + ** DMS byte and truncate the *-shm file to zero bytes in size. Then + ** downgrade to a SHARED lock on the DMS byte. + ** + ** If another process is holding an EXCLUSIVE lock on the DMS byte, + ** return SQLITE_BUSY to the caller (it will try again). An earlier + ** version of this code attempted the SHARED lock at this point. But + ** this introduced a subtle race condition: if the process holding + ** EXCLUSIVE failed just before truncating the *-shm file, then this + ** process might open and use the *-shm file without truncating it. + ** And if the *-shm file has been corrupted by a power failure or + ** system crash, the database itself may also become corrupt. */ + lock.l_whence = SEEK_SET; + lock.l_start = UNIX_SHM_DMS; + lock.l_len = 1; + lock.l_type = F_WRLCK; + if( osFcntl(pShmNode->hShm, F_GETLK, &lock)!=0 ) { + rc = SQLITE_IOERR_LOCK; + }else if( lock.l_type==F_UNLCK ){ + if( pShmNode->isReadonly ){ + pShmNode->isUnlocked = 1; + rc = SQLITE_READONLY_CANTINIT; + }else{ +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT + /* Do not use a blocking lock here. If the lock cannot be obtained + ** immediately, it means some other connection is truncating the + ** *-shm file. And after it has done so, it will not release its + ** lock, but only downgrade it to a shared lock. So no point in + ** blocking here. The call below to obtain the shared DMS lock may + ** use a blocking lock. */ + int iSaveTimeout = pDbFd->iBusyTimeout; + pDbFd->iBusyTimeout = 0; +#endif + rc = unixShmSystemLock(pDbFd, F_WRLCK, UNIX_SHM_DMS, 1); +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT + pDbFd->iBusyTimeout = iSaveTimeout; +#endif + /* The first connection to attach must truncate the -shm file. We + ** truncate to 3 bytes (an arbitrary small number, less than the + ** -shm header size) rather than 0 as a system debugging aid, to + ** help detect if a -shm file truncation is legitimate or is the work + ** or a rogue process. */ + if( rc==SQLITE_OK && robust_ftruncate(pShmNode->hShm, 3) ){ + rc = unixLogError(SQLITE_IOERR_SHMOPEN,"ftruncate",pShmNode->zFilename); + } + } + }else if( lock.l_type==F_WRLCK ){ + rc = SQLITE_BUSY; + } + + if( rc==SQLITE_OK ){ + assert( lock.l_type==F_UNLCK || lock.l_type==F_RDLCK ); + rc = unixShmSystemLock(pDbFd, F_RDLCK, UNIX_SHM_DMS, 1); + } + return rc; +} + +/* +** Open a shared-memory area associated with open database file pDbFd. ** This particular implementation uses mmapped files. ** ** The file used to implement shared-memory is in the same directory ** as the open database file and has the same name as the open database ** file with the "-shm" suffix added. For example, if the database file ** is "/home/user1/config.db" then the file that is created and mmapped -** for shared memory will be called "/home/user1/config.db-shm". +** for shared memory will be called "/home/user1/config.db-shm". ** ** Another approach to is to use files in /dev/shm or /dev/tmp or an ** some other tmpfs mount. But if a file in a different directory ** from the database file is used, then differing access permissions ** or a chroot() might cause two different processes on the same -** database to end up using different files for shared memory - +** database to end up using different files for shared memory - ** meaning that their memory would not really be shared - resulting ** in database corruption. Nevertheless, this tmpfs file usage ** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm" @@ -4215,20 +4952,21 @@ static void unixShmPurge(unixFile *pFd){ static int unixOpenSharedMemory(unixFile *pDbFd){ struct unixShm *p = 0; /* The connection to be opened */ struct unixShmNode *pShmNode; /* The underlying mmapped file */ - int rc; /* Result code */ + int rc = SQLITE_OK; /* Result code */ unixInodeInfo *pInode; /* The inode of fd */ - char *zShmFilename; /* Name of the file used for SHM */ + char *zShm; /* Name of the file used for SHM */ int nShmFilename; /* Size of the SHM filename in bytes */ /* Allocate space for the new unixShm object. */ p = sqlite3_malloc64( sizeof(*p) ); - if( p==0 ) return SQLITE_NOMEM; + if( p==0 ) return SQLITE_NOMEM_BKPT; memset(p, 0, sizeof(*p)); assert( pDbFd->pShm==0 ); /* Check to see if a unixShmNode object already exists. Reuse an existing ** one if present. Create a new one if necessary. */ + assert( unixFileMutexNotheld(pDbFd) ); unixEnterMutex(); pInode = pDbFd->pInode; pShmNode = pInode->pShmNode; @@ -4254,59 +4992,65 @@ static int unixOpenSharedMemory(unixFile *pDbFd){ #endif pShmNode = sqlite3_malloc64( sizeof(*pShmNode) + nShmFilename ); if( pShmNode==0 ){ - rc = SQLITE_NOMEM; + rc = SQLITE_NOMEM_BKPT; goto shm_open_err; } memset(pShmNode, 0, sizeof(*pShmNode)+nShmFilename); - zShmFilename = pShmNode->zFilename = (char*)&pShmNode[1]; + zShm = pShmNode->zFilename = (char*)&pShmNode[1]; #ifdef SQLITE_SHM_DIRECTORY - sqlite3_snprintf(nShmFilename, zShmFilename, + sqlite3_snprintf(nShmFilename, zShm, SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x", (u32)sStat.st_ino, (u32)sStat.st_dev); #else - sqlite3_snprintf(nShmFilename, zShmFilename, "%s-shm", zBasePath); - sqlite3FileSuffix3(pDbFd->zPath, zShmFilename); + sqlite3_snprintf(nShmFilename, zShm, "%s-shm", zBasePath); + sqlite3FileSuffix3(pDbFd->zPath, zShm); #endif - pShmNode->h = -1; + pShmNode->hShm = -1; pDbFd->pInode->pShmNode = pShmNode; pShmNode->pInode = pDbFd->pInode; - pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); - if( pShmNode->mutex==0 ){ - rc = SQLITE_NOMEM; - goto shm_open_err; + if( sqlite3GlobalConfig.bCoreMutex ){ + pShmNode->pShmMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); + if( pShmNode->pShmMutex==0 ){ + rc = SQLITE_NOMEM_BKPT; + goto shm_open_err; + } +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT + { + int ii; + for(ii=0; iiaMutex[ii] = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); + if( pShmNode->aMutex[ii]==0 ){ + rc = SQLITE_NOMEM_BKPT; + goto shm_open_err; + } + } + } +#endif } if( pInode->bProcessLock==0 ){ - int openFlags = O_RDWR | O_CREAT; - if( sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){ - openFlags = O_RDONLY; - pShmNode->isReadonly = 1; + if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){ + pShmNode->hShm = robust_open(zShm, O_RDWR|O_CREAT|O_NOFOLLOW, + (sStat.st_mode&0777)); } - pShmNode->h = robust_open(zShmFilename, openFlags, (sStat.st_mode&0777)); - if( pShmNode->h<0 ){ - rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShmFilename); - goto shm_open_err; + if( pShmNode->hShm<0 ){ + pShmNode->hShm = robust_open(zShm, O_RDONLY|O_NOFOLLOW, + (sStat.st_mode&0777)); + if( pShmNode->hShm<0 ){ + rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShm); + goto shm_open_err; + } + pShmNode->isReadonly = 1; } /* If this process is running as root, make sure that the SHM file ** is owned by the same user that owns the original database. Otherwise, ** the original owner will not be able to connect. */ - robustFchown(pShmNode->h, sStat.st_uid, sStat.st_gid); - - /* Check to see if another process is holding the dead-man switch. - ** If not, truncate the file to zero length. - */ - rc = SQLITE_OK; - if( unixShmSystemLock(pDbFd, F_WRLCK, UNIX_SHM_DMS, 1)==SQLITE_OK ){ - if( robust_ftruncate(pShmNode->h, 0) ){ - rc = unixLogError(SQLITE_IOERR_SHMOPEN, "ftruncate", zShmFilename); - } - } - if( rc==SQLITE_OK ){ - rc = unixShmSystemLock(pDbFd, F_RDLCK, UNIX_SHM_DMS, 1); - } - if( rc ) goto shm_open_err; + robustFchown(pShmNode->hShm, sStat.st_uid, sStat.st_gid); + + rc = unixLockSharedMemory(pDbFd, pShmNode); + if( rc!=SQLITE_OK && rc!=SQLITE_READONLY_CANTINIT ) goto shm_open_err; } } @@ -4323,14 +5067,14 @@ static int unixOpenSharedMemory(unixFile *pDbFd){ ** the cover of the unixEnterMutex() mutex and the pointer from the ** new (struct unixShm) object to the pShmNode has been set. All that is ** left to do is to link the new object into the linked list starting - ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex - ** mutex. + ** at pShmNode->pFirst. This must be done while holding the + ** pShmNode->pShmMutex. */ - sqlite3_mutex_enter(pShmNode->mutex); + sqlite3_mutex_enter(pShmNode->pShmMutex); p->pNext = pShmNode->pFirst; pShmNode->pFirst = p; - sqlite3_mutex_leave(pShmNode->mutex); - return SQLITE_OK; + sqlite3_mutex_leave(pShmNode->pShmMutex); + return rc; /* Jump here on any error */ shm_open_err: @@ -4341,22 +5085,22 @@ static int unixOpenSharedMemory(unixFile *pDbFd){ } /* -** This function is called to obtain a pointer to region iRegion of the -** shared-memory associated with the database file fd. Shared-memory regions -** are numbered starting from zero. Each shared-memory region is szRegion +** This function is called to obtain a pointer to region iRegion of the +** shared-memory associated with the database file fd. Shared-memory regions +** are numbered starting from zero. Each shared-memory region is szRegion ** bytes in size. ** ** If an error occurs, an error code is returned and *pp is set to NULL. ** ** Otherwise, if the bExtend parameter is 0 and the requested shared-memory ** region has not been allocated (by any client, including one running in a -** separate process), then *pp is set to NULL and SQLITE_OK returned. If -** bExtend is non-zero and the requested shared-memory region has not yet +** separate process), then *pp is set to NULL and SQLITE_OK returned. If +** bExtend is non-zero and the requested shared-memory region has not yet ** been allocated, it is allocated by this function. ** ** If the shared-memory region has already been allocated or is allocated by -** this call as described above, then it is mapped into this processes -** address space (if it is not already), *pp is set to point to the mapped +** this call as described above, then it is mapped into this processes +** address space (if it is not already), *pp is set to point to the mapped ** memory and SQLITE_OK returned. */ static int unixShmMap( @@ -4381,11 +5125,16 @@ static int unixShmMap( p = pDbFd->pShm; pShmNode = p->pShmNode; - sqlite3_mutex_enter(pShmNode->mutex); + sqlite3_mutex_enter(pShmNode->pShmMutex); + if( pShmNode->isUnlocked ){ + rc = unixLockSharedMemory(pDbFd, pShmNode); + if( rc!=SQLITE_OK ) goto shmpage_out; + pShmNode->isUnlocked = 0; + } assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 ); assert( pShmNode->pInode==pDbFd->pInode ); - assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 ); - assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 ); + assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 ); + assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 ); /* Minimum number of regions required to be mapped. */ nReqRegion = ((iRegion+nShmPerMap) / nShmPerMap) * nShmPerMap; @@ -4397,16 +5146,16 @@ static int unixShmMap( pShmNode->szRegion = szRegion; - if( pShmNode->h>=0 ){ + if( pShmNode->hShm>=0 ){ /* The requested region is not mapped into this processes address space. ** Check to see if it has been allocated (i.e. if the wal-index file is ** large enough to contain the requested region). */ - if( osFstat(pShmNode->h, &sStat) ){ + if( osFstat(pShmNode->hShm, &sStat) ){ rc = SQLITE_IOERR_SHMSIZE; goto shmpage_out; } - + if( sStat.st_sizeh, iPg*pgsz + pgsz-1, "", 1, &x)!=1 ){ + if( seekAndWriteFd(pShmNode->hShm, iPg*pgsz + pgsz-1,"",1,&x)!=1 ){ const char *zFile = pShmNode->zFilename; rc = unixLogError(SQLITE_IOERR_SHMSIZE, "write", zFile); goto shmpage_out; @@ -4445,7 +5194,7 @@ static int unixShmMap( pShmNode->apRegion, nReqRegion*sizeof(char *) ); if( !apNew ){ - rc = SQLITE_IOERR_NOMEM; + rc = SQLITE_IOERR_NOMEM_BKPT; goto shmpage_out; } pShmNode->apRegion = apNew; @@ -4453,22 +5202,22 @@ static int unixShmMap( int nMap = szRegion*nShmPerMap; int i; void *pMem; - if( pShmNode->h>=0 ){ + if( pShmNode->hShm>=0 ){ pMem = osMmap(0, nMap, - pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE, - MAP_SHARED, pShmNode->h, szRegion*(i64)pShmNode->nRegion + pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE, + MAP_SHARED, pShmNode->hShm, szRegion*(i64)pShmNode->nRegion ); if( pMem==MAP_FAILED ){ rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename); goto shmpage_out; } }else{ - pMem = sqlite3_malloc64(szRegion); + pMem = sqlite3_malloc64(nMap); if( pMem==0 ){ - rc = SQLITE_NOMEM; + rc = SQLITE_NOMEM_BKPT; goto shmpage_out; } - memset(pMem, 0, szRegion); + memset(pMem, 0, nMap); } for(i=0; iisReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY; - sqlite3_mutex_leave(pShmNode->mutex); + sqlite3_mutex_leave(pShmNode->pShmMutex); return rc; } +/* +** Check that the pShmNode->aLock[] array comports with the locking bitmasks +** held by each client. Return true if it does, or false otherwise. This +** is to be used in an assert(). e.g. +** +** assert( assertLockingArrayOk(pShmNode) ); +*/ +#ifdef SQLITE_DEBUG +static int assertLockingArrayOk(unixShmNode *pShmNode){ +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT + return 1; +#else + unixShm *pX; + int aLock[SQLITE_SHM_NLOCK]; + + memset(aLock, 0, sizeof(aLock)); + for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ + int i; + for(i=0; iexclMask & (1<sharedMask & (1<=0 ); + aLock[i]++; + } + } + } + + assert( 0==memcmp(pShmNode->aLock, aLock, sizeof(aLock)) ); + return (memcmp(pShmNode->aLock, aLock, sizeof(aLock))==0); +#endif +} +#endif /* !defined(SQLITE_WASI) && !defined(SQLITE_OMIT_WAL) */ + /* ** Change the lock state for a shared-memory segment. ** -** Note that the relationship between SHAREd and EXCLUSIVE locks is a little +** Note that the relationship between SHARED and EXCLUSIVE locks is a little ** different here than in posix. In xShmLock(), one can go from unlocked ** to shared and back or from unlocked to exclusive and back. But one may ** not go from shared to exclusive or from exclusive to shared. @@ -4504,11 +5288,17 @@ static int unixShmLock( int flags /* What to do with the lock */ ){ unixFile *pDbFd = (unixFile*)fd; /* Connection holding shared memory */ - unixShm *p = pDbFd->pShm; /* The shared memory being locked */ - unixShm *pX; /* For looping over all siblings */ - unixShmNode *pShmNode = p->pShmNode; /* The underlying file iNode */ + unixShm *p; /* The shared memory being locked */ + unixShmNode *pShmNode; /* The underlying file iNode */ int rc = SQLITE_OK; /* Result code */ - u16 mask; /* Mask of locks to take or release */ + u16 mask = (1<<(ofst+n)) - (1<pShm; + if( p==0 ) return SQLITE_IOERR_SHMLOCK; + pShmNode = p->pShmNode; + if( NEVER(pShmNode==0) ) return SQLITE_IOERR_SHMLOCK; + aLock = pShmNode->aLock; assert( pShmNode==pDbFd->pInode->pShmNode ); assert( pShmNode->pInode==pDbFd->pInode ); @@ -4519,92 +5309,174 @@ static int unixShmLock( || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED) || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) ); assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 ); - assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 ); - assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 ); + assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 ); + assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 ); - mask = (1<<(ofst+n)) - (1<1 || mask==(1<mutex); - if( flags & SQLITE_SHM_UNLOCK ){ - u16 allMask = 0; /* Mask of locks held by siblings */ - - /* See if any siblings hold this same lock */ - for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ - if( pX==p ) continue; - assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 ); - allMask |= pX->sharedMask; - } + /* Check that, if this to be a blocking lock, no locks that occur later + ** in the following list than the lock being obtained are already held: + ** + ** 1. Recovery lock (ofst==2). + ** 2. Checkpointer lock (ofst==1). + ** 3. Write lock (ofst==0). + ** 4. Read locks (ofst>=3 && ofstexclMask|p->sharedMask); + assert( (flags & SQLITE_SHM_UNLOCK) || pDbFd->iBusyTimeout==0 || ( + (ofst!=2 || lockMask==0) + && (ofst!=1 || lockMask==0 || lockMask==2) + && (ofst!=0 || lockMask<3) + && (ofst<3 || lockMask<(1<exclMask & mask) + ); + if( ((flags & SQLITE_SHM_UNLOCK) && ((p->exclMask|p->sharedMask) & mask)) + || (flags==(SQLITE_SHM_SHARED|SQLITE_SHM_LOCK) && 0==(p->sharedMask & mask)) + || (flags==(SQLITE_SHM_EXCLUSIVE|SQLITE_SHM_LOCK)) + ){ - /* Undo the local locks */ - if( rc==SQLITE_OK ){ - p->exclMask &= ~mask; - p->sharedMask &= ~mask; - } - }else if( flags & SQLITE_SHM_SHARED ){ - u16 allShared = 0; /* Union of locks held by connections other than "p" */ - - /* Find out which shared locks are already held by sibling connections. - ** If any sibling already holds an exclusive lock, go ahead and return - ** SQLITE_BUSY. - */ - for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ - if( (pX->exclMask & mask)!=0 ){ - rc = SQLITE_BUSY; - break; + /* Take the required mutexes. In SETLK_TIMEOUT mode (blocking locks), if + ** this is an attempt on an exclusive lock use sqlite3_mutex_try(). If any + ** other thread is holding this mutex, then it is either holding or about + ** to hold a lock exclusive to the one being requested, and we may + ** therefore return SQLITE_BUSY to the caller. + ** + ** Doing this prevents some deadlock scenarios. For example, thread 1 may + ** be a checkpointer blocked waiting on the WRITER lock. And thread 2 + ** may be a normal SQL client upgrading to a write transaction. In this + ** case thread 2 does a non-blocking request for the WRITER lock. But - + ** if it were to use sqlite3_mutex_enter() then it would effectively + ** become a (doomed) blocking request, as thread 2 would block until thread + ** 1 obtained WRITER and released the mutex. Since thread 2 already holds + ** a lock on a read-locking slot at this point, this breaks the + ** anti-deadlock rules (see above). */ +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT + int iMutex; + for(iMutex=ofst; iMutexaMutex[iMutex]); + if( rc!=SQLITE_OK ) goto leave_shmnode_mutexes; + }else{ + sqlite3_mutex_enter(pShmNode->aMutex[iMutex]); } - allShared |= pX->sharedMask; } +#else + sqlite3_mutex_enter(pShmNode->pShmMutex); +#endif - /* Get shared locks at the system level, if necessary */ - if( rc==SQLITE_OK ){ - if( (allShared & mask)==0 ){ - rc = unixShmSystemLock(pDbFd, F_RDLCK, ofst+UNIX_SHM_BASE, n); + if( ALWAYS(rc==SQLITE_OK) ){ + if( flags & SQLITE_SHM_UNLOCK ){ + /* Case (a) - unlock. */ + int bUnlock = 1; + assert( (p->exclMask & p->sharedMask)==0 ); + assert( !(flags & SQLITE_SHM_EXCLUSIVE) || (p->exclMask & mask)==mask ); + assert( !(flags & SQLITE_SHM_SHARED) || (p->sharedMask & mask)==mask ); + + /* If this is a SHARED lock being unlocked, it is possible that other + ** clients within this process are holding the same SHARED lock. In + ** this case, set bUnlock to 0 so that the posix lock is not removed + ** from the file-descriptor below. */ + if( flags & SQLITE_SHM_SHARED ){ + assert( n==1 ); + assert( aLock[ofst]>=1 ); + if( aLock[ofst]>1 ){ + bUnlock = 0; + aLock[ofst]--; + p->sharedMask &= ~mask; + } + } + + if( bUnlock ){ + rc = unixShmSystemLock(pDbFd, F_UNLCK, ofst+UNIX_SHM_BASE, n); + if( rc==SQLITE_OK ){ + memset(&aLock[ofst], 0, sizeof(int)*n); + p->sharedMask &= ~mask; + p->exclMask &= ~mask; + } + } + }else if( flags & SQLITE_SHM_SHARED ){ + /* Case (b) - a shared lock. */ + + if( aLock[ofst]<0 ){ + /* An exclusive lock is held by some other connection. BUSY. */ + rc = SQLITE_BUSY; + }else if( aLock[ofst]==0 ){ + rc = unixShmSystemLock(pDbFd, F_RDLCK, ofst+UNIX_SHM_BASE, n); + } + + /* Get the local shared locks */ + if( rc==SQLITE_OK ){ + p->sharedMask |= mask; + aLock[ofst]++; + } }else{ - rc = SQLITE_OK; - } - } - - /* Get the local shared locks */ - if( rc==SQLITE_OK ){ - p->sharedMask |= mask; - } - }else{ - /* Make sure no sibling connections hold locks that will block this - ** lock. If any do, return SQLITE_BUSY right away. - */ - for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ - if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){ - rc = SQLITE_BUSY; - break; - } - } + /* Case (c) - an exclusive lock. */ + int ii; - /* Get the exclusive locks at the system level. Then if successful - ** also mark the local connection as being locked. - */ - if( rc==SQLITE_OK ){ - rc = unixShmSystemLock(pDbFd, F_WRLCK, ofst+UNIX_SHM_BASE, n); - if( rc==SQLITE_OK ){ + assert( flags==(SQLITE_SHM_LOCK|SQLITE_SHM_EXCLUSIVE) ); assert( (p->sharedMask & mask)==0 ); - p->exclMask |= mask; + assert( (p->exclMask & mask)==0 ); + + /* Make sure no sibling connections hold locks that will block this + ** lock. If any do, return SQLITE_BUSY right away. */ + for(ii=ofst; iiexclMask |= mask; + for(ii=ofst; ii=ofst; iMutex--){ + sqlite3_mutex_leave(pShmNode->aMutex[iMutex]); } +#else + sqlite3_mutex_leave(pShmNode->pShmMutex); +#endif } - sqlite3_mutex_leave(pShmNode->mutex); + OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n", p->id, osGetpid(0), p->sharedMask, p->exclMask)); return rc; } /* -** Implement a memory barrier or memory fence on shared memory. +** Implement a memory barrier or memory fence on shared memory. ** ** All loads and stores begun before the barrier must complete before ** any load or store begun after the barrier. @@ -4614,12 +5486,15 @@ static void unixShmBarrier( ){ UNUSED_PARAMETER(fd); sqlite3MemoryBarrier(); /* compiler-defined memory barrier */ + assert( fd->pMethods->xLock==nolockLock + || unixFileMutexNotheld((unixFile*)fd) + ); unixEnterMutex(); /* Also mutex, for redundancy */ unixLeaveMutex(); } /* -** Close a connection to shared-memory. Delete the underlying +** Close a connection to shared-memory. Delete the underlying ** storage if deleteFlag is true. ** ** If there is no shared memory associated with the connection then this @@ -4644,22 +5519,23 @@ static int unixShmUnmap( /* Remove connection p from the set of connections associated ** with pShmNode */ - sqlite3_mutex_enter(pShmNode->mutex); + sqlite3_mutex_enter(pShmNode->pShmMutex); for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){} *pp = p->pNext; /* Free the connection p */ sqlite3_free(p); pDbFd->pShm = 0; - sqlite3_mutex_leave(pShmNode->mutex); + sqlite3_mutex_leave(pShmNode->pShmMutex); /* If pShmNode->nRef has reached 0, then close the underlying ** shared-memory file, too */ + assert( unixFileMutexNotheld(pDbFd) ); unixEnterMutex(); assert( pShmNode->nRef>0 ); pShmNode->nRef--; if( pShmNode->nRef==0 ){ - if( deleteFlag && pShmNode->h>=0 ){ + if( deleteFlag && pShmNode->hShm>=0 ){ osUnlink(pShmNode->zFilename); } unixShmPurge(pDbFd); @@ -4692,7 +5568,7 @@ static void unixUnmapfile(unixFile *pFd){ } /* -** Attempt to set the size of the memory mapping maintained by file +** Attempt to set the size of the memory mapping maintained by file ** descriptor pFd to nNew bytes. Any existing mapping is discarded. ** ** If successful, this function sets the following variables: @@ -4784,14 +5660,14 @@ static void unixRemapfile( /* ** Memory map or remap the file opened by file-descriptor pFd (if the file -** is already mapped, the existing mapping is replaced by the new). Or, if -** there already exists a mapping for this file, and there are still +** is already mapped, the existing mapping is replaced by the new). Or, if +** there already exists a mapping for this file, and there are still ** outstanding xFetch() references to it, this function is a no-op. ** -** If parameter nByte is non-negative, then it is the requested size of -** the mapping to create. Otherwise, if nByte is less than zero, then the +** If parameter nByte is non-negative, then it is the requested size of +** the mapping to create. Otherwise, if nByte is less than zero, then the ** requested size is the size of the file on disk. The actual size of the -** created mapping is either the requested size or the value configured +** created mapping is either the requested size or the value configured ** using SQLITE_FCNTL_MMAP_LIMIT, whichever is smaller. ** ** SQLITE_OK is returned if no error occurs (even if the mapping is not @@ -4832,7 +5708,7 @@ static int unixMapfile(unixFile *pFd, i64 nMap){ ** Finally, if an error does occur, return an SQLite error code. The final ** value of *pp is undefined in this case. ** -** If this function does return a pointer, the caller must eventually +** If this function does return a pointer, the caller must eventually ** release the reference by calling unixUnfetch(). */ static int unixFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){ @@ -4843,11 +5719,16 @@ static int unixFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){ #if SQLITE_MAX_MMAP_SIZE>0 if( pFd->mmapSizeMax>0 ){ + /* Ensure that there is always at least a 256 byte buffer of addressable + ** memory following the returned page. If the database is corrupt, + ** SQLite may overread the page slightly (in practice only a few bytes, + ** but 256 is safe, round, number). */ + const int nEofBuffer = 256; if( pFd->pMapRegion==0 ){ int rc = unixMapfile(pFd, -1); if( rc!=SQLITE_OK ) return rc; } - if( pFd->mmapSize >= iOff+nAmt ){ + if( pFd->mmapSize >= (iOff+nAmt+nEofBuffer) ){ *pp = &((u8 *)pFd->pMapRegion)[iOff]; pFd->nFetchOut++; } @@ -4857,13 +5738,13 @@ static int unixFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){ } /* -** If the third argument is non-NULL, then this function releases a +** If the third argument is non-NULL, then this function releases a ** reference obtained by an earlier call to unixFetch(). The second ** argument passed to this function must be the same as the corresponding -** argument that was passed to the unixFetch() invocation. +** argument that was passed to the unixFetch() invocation. ** -** Or, if the third argument is NULL, then this function is being called -** to inform the VFS layer that, according to POSIX, any existing mapping +** Or, if the third argument is NULL, then this function is being called +** to inform the VFS layer that, according to POSIX, any existing mapping ** may now be invalid and should be unmapped. */ static int unixUnfetch(sqlite3_file *fd, i64 iOff, void *p){ @@ -4871,7 +5752,7 @@ static int unixUnfetch(sqlite3_file *fd, i64 iOff, void *p){ unixFile *pFd = (unixFile *)fd; /* The underlying database file */ UNUSED_PARAMETER(iOff); - /* If p==0 (unmap the entire file) then there must be no outstanding + /* If p==0 (unmap the entire file) then there must be no outstanding ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference), ** then there must be at least one outstanding. */ assert( (p==0)==(pFd->nFetchOut==0) ); @@ -4981,7 +5862,7 @@ IOMETHODS( IOMETHODS( nolockIoFinder, /* Finder function name */ nolockIoMethods, /* sqlite3_io_methods object name */ - 3, /* shared memory is disabled */ + 3, /* shared memory and mmap are enabled */ nolockClose, /* xClose method */ nolockLock, /* xLock method */ nolockUnlock, /* xUnlock method */ @@ -5079,8 +5960,8 @@ IOMETHODS( #endif #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE -/* -** This "finder" function attempts to determine the best locking strategy +/* +** This "finder" function attempts to determine the best locking strategy ** for the database file "filePath". It then returns the sqlite3_io_methods ** object that implements that strategy. ** @@ -5122,8 +6003,8 @@ static const sqlite3_io_methods *autolockIoFinderImpl( } /* Default case. Handles, amongst others, "nfs". - ** Test byte-range lock using fcntl(). If the call succeeds, - ** assume that the file-system supports POSIX style locks. + ** Test byte-range lock using fcntl(). If the call succeeds, + ** assume that the file-system supports POSIX style locks. */ lockInfo.l_len = 1; lockInfo.l_start = 0; @@ -5139,7 +6020,7 @@ static const sqlite3_io_methods *autolockIoFinderImpl( return &dotlockIoMethods; } } -static const sqlite3_io_methods +static const sqlite3_io_methods *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl; #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ @@ -5175,7 +6056,7 @@ static const sqlite3_io_methods *vxworksIoFinderImpl( return &semIoMethods; } } -static const sqlite3_io_methods +static const sqlite3_io_methods *(*const vxworksIoFinder)(const char*,unixFile*) = vxworksIoFinderImpl; #endif /* OS_VXWORKS */ @@ -5209,17 +6090,6 @@ static int fillInUnixFile( assert( pNew->pInode==NULL ); - /* Usually the path zFilename should not be a relative pathname. The - ** exception is when opening the proxy "conch" file in builds that - ** include the special Apple locking styles. - */ -#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE - assert( zFilename==0 || zFilename[0]=='/' - || pVfs->pAppData==(void*)&autolockIoFinder ); -#else - assert( zFilename==0 || zFilename[0]=='/' ); -#endif - /* No locking occurs in temporary files */ assert( zFilename!=0 || (ctrlFlags & UNIXFILE_NOLOCK)!=0 ); @@ -5243,7 +6113,7 @@ static int fillInUnixFile( pNew->pId = vxworksFindFileId(zFilename); if( pNew->pId==0 ){ ctrlFlags |= UNIXFILE_NOLOCK; - rc = SQLITE_NOMEM; + rc = SQLITE_NOMEM_BKPT; } #endif @@ -5299,7 +6169,7 @@ static int fillInUnixFile( afpLockingContext *pCtx; pNew->lockingContext = pCtx = sqlite3_malloc64( sizeof(*pCtx) ); if( pCtx==0 ){ - rc = SQLITE_NOMEM; + rc = SQLITE_NOMEM_BKPT; }else{ /* NB: zFilename exists and remains valid until the file is closed ** according to requirement F11141. So we do not need to make a @@ -5314,14 +6184,14 @@ static int fillInUnixFile( robust_close(pNew, h, __LINE__); h = -1; } - unixLeaveMutex(); + unixLeaveMutex(); } } #endif else if( pLockingStyle == &dotlockIoMethods ){ /* Dotfile locking uses the file path so it needs to be included in - ** the dotlockLockingContext + ** the dotlockLockingContext */ char *zLockFile; int nFilename; @@ -5329,7 +6199,7 @@ static int fillInUnixFile( nFilename = (int)strlen(zFilename) + 6; zLockFile = (char *)sqlite3_malloc64(nFilename); if( zLockFile==0 ){ - rc = SQLITE_NOMEM; + rc = SQLITE_NOMEM_BKPT; }else{ sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename); } @@ -5352,60 +6222,84 @@ static int fillInUnixFile( if( zSemName[n]=='/' ) zSemName[n] = '_'; pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1); if( pNew->pInode->pSem == SEM_FAILED ){ - rc = SQLITE_NOMEM; + rc = SQLITE_NOMEM_BKPT; pNew->pInode->aSemName[0] = '\0'; } } unixLeaveMutex(); } #endif - + storeLastErrno(pNew, 0); #if OS_VXWORKS if( rc!=SQLITE_OK ){ - if( h>=0 ) robust_close(pNew, h, __LINE__); - h = -1; - osUnlink(zFilename); - pNew->ctrlFlags |= UNIXFILE_DELETE; + if( h>=0 ){ + robust_close(pNew, h, __LINE__); + h = -1; + } + if( pNew->ctrlFlags & UNIXFILE_DELETE ){ + osUnlink(zFilename); + } + if( pNew->pId ){ + vxworksReleaseFileId(pNew->pId); + pNew->pId = 0; + } } #endif if( rc!=SQLITE_OK ){ if( h>=0 ) robust_close(pNew, h, __LINE__); }else{ - pNew->pMethod = pLockingStyle; + pId->pMethods = pLockingStyle; OpenCounter(+1); verifyDbFile(pNew); } return rc; } +/* +** Directories to consider for temp files. +*/ +static const char *azTempDirs[] = { + 0, + 0, + "/var/tmp", + "/usr/tmp", + "/tmp", + "." +}; + +/* +** Initialize first two members of azTempDirs[] array. +*/ +static void unixTempFileInit(void){ + azTempDirs[0] = getenv("SQLITE_TMPDIR"); + azTempDirs[1] = getenv("TMPDIR"); +} + /* ** Return the name of a directory in which to put temporary files. ** If no suitable temporary file directory can be found, return NULL. */ static const char *unixTempFileDir(void){ - static const char *azDirs[] = { - 0, - 0, - "/var/tmp", - "/usr/tmp", - "/tmp", - "." - }; - unsigned int i; + unsigned int i = 0; struct stat buf; const char *zDir = sqlite3_temp_directory; - if( !azDirs[0] ) azDirs[0] = getenv("SQLITE_TMPDIR"); - if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR"); - for(i=0; i=sizeof(azTempDirs)/sizeof(azTempDirs[0]) ) break; + zDir = azTempDirs[i++]; } - return zDir; + return 0; } /* @@ -5416,24 +6310,35 @@ static const char *unixTempFileDir(void){ static int unixGetTempname(int nBuf, char *zBuf){ const char *zDir; int iLimit = 0; + int rc = SQLITE_OK; /* It's odd to simulate an io-error here, but really this is just ** using the io-error infrastructure to test that SQLite handles this - ** function failing. + ** function failing. */ + zBuf[0] = 0; SimulateIOError( return SQLITE_IOERR ); + sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR)); zDir = unixTempFileDir(); - do{ - u64 r; - sqlite3_randomness(sizeof(r), &r); - assert( nBuf>2 ); - zBuf[nBuf-2] = 0; - sqlite3_snprintf(nBuf, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX"%llx%c", - zDir, r, 0); - if( zBuf[nBuf-2]!=0 || (iLimit++)>10 ) return SQLITE_ERROR; - }while( osAccess(zBuf,0)==0 ); - return SQLITE_OK; + if( zDir==0 ){ + rc = SQLITE_IOERR_GETTEMPPATH; + }else{ + do{ + u64 r; + sqlite3_randomness(sizeof(r), &r); + assert( nBuf>2 ); + zBuf[nBuf-2] = 0; + sqlite3_snprintf(nBuf, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX"%llx%c", + zDir, r, 0); + if( zBuf[nBuf-2]!=0 || (iLimit++)>10 ){ + rc = SQLITE_ERROR; + break; + } + }while( osAccess(zBuf,0)==0 ); + } + sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR)); + return rc; } #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) @@ -5446,8 +6351,8 @@ static int proxyTransformUnixFile(unixFile*, const char*); #endif /* -** Search for an unused file descriptor that was opened on the database -** file (not a journal or master-journal file) identified by pathname +** Search for an unused file descriptor that was opened on the database +** file (not a journal or super-journal file) identified by pathname ** zPath with SQLITE_OPEN_XXX flags matching those passed as the second ** argument to this function. ** @@ -5455,7 +6360,7 @@ static int proxyTransformUnixFile(unixFile*, const char*); ** but the associated file descriptor could not be closed because some ** other file descriptor open on the same file is holding a file-lock. ** Refer to comments in the unixClose() function and the lengthy comment -** describing "Posix Advisory Locking" at the start of this file for +** describing "Posix Advisory Locking" at the start of this file for ** further details. Also, ticket #4018. ** ** If a suitable file descriptor is found, then it is returned. If no @@ -5466,12 +6371,14 @@ static UnixUnusedFd *findReusableFd(const char *zPath, int flags){ /* Do not search for an unused file descriptor on vxworks. Not because ** vxworks would not benefit from the change (it might, we're not sure), - ** but because no way to test it is currently available. It is better - ** not to risk breaking vxworks support for the sake of such an obscure + ** but because no way to test it is currently available. It is better + ** not to risk breaking vxworks support for the sake of such an obscure ** feature. */ #if !OS_VXWORKS struct stat sStat; /* Results of stat() call */ + unixEnterMutex(); + /* A stat() call may fail for various reasons. If this happens, it is ** almost certain that an open() call on the same path will also fail. ** For this reason, if an error occurs in the stat() call here, it is @@ -5480,49 +6387,73 @@ static UnixUnusedFd *findReusableFd(const char *zPath, int flags){ ** ** Even if a subsequent open() call does succeed, the consequences of ** not searching for a reusable file descriptor are not dire. */ - if( 0==osStat(zPath, &sStat) ){ + if( inodeList!=0 && 0==osStat(zPath, &sStat) ){ unixInodeInfo *pInode; - unixEnterMutex(); pInode = inodeList; while( pInode && (pInode->fileId.dev!=sStat.st_dev - || pInode->fileId.ino!=sStat.st_ino) ){ + || pInode->fileId.ino!=(u64)sStat.st_ino) ){ pInode = pInode->pNext; } if( pInode ){ UnixUnusedFd **pp; + assert( sqlite3_mutex_notheld(pInode->pLockMutex) ); + sqlite3_mutex_enter(pInode->pLockMutex); + flags &= (SQLITE_OPEN_READONLY|SQLITE_OPEN_READWRITE); for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext)); pUnused = *pp; if( pUnused ){ *pp = pUnused->pNext; } + sqlite3_mutex_leave(pInode->pLockMutex); } - unixLeaveMutex(); } + unixLeaveMutex(); #endif /* if !OS_VXWORKS */ return pUnused; } +/* +** Find the mode, uid and gid of file zFile. +*/ +static int getFileMode( + const char *zFile, /* File name */ + mode_t *pMode, /* OUT: Permissions of zFile */ + uid_t *pUid, /* OUT: uid of zFile. */ + gid_t *pGid /* OUT: gid of zFile. */ +){ + struct stat sStat; /* Output of stat() on database file */ + int rc = SQLITE_OK; + if( 0==osStat(zFile, &sStat) ){ + *pMode = sStat.st_mode & 0777; + *pUid = sStat.st_uid; + *pGid = sStat.st_gid; + }else{ + rc = SQLITE_IOERR_FSTAT; + } + return rc; +} + /* ** This function is called by unixOpen() to determine the unix permissions ** to create new files with. If no error occurs, then SQLITE_OK is returned ** and a value suitable for passing as the third argument to open(2) is -** written to *pMode. If an IO error occurs, an SQLite error code is +** written to *pMode. If an IO error occurs, an SQLite error code is ** returned and the value of *pMode is not modified. ** ** In most cases, this routine sets *pMode to 0, which will become ** an indication to robust_open() to create the file using ** SQLITE_DEFAULT_FILE_PERMISSIONS adjusted by the umask. -** But if the file being opened is a WAL or regular journal file, then -** this function queries the file-system for the permissions on the -** corresponding database file and sets *pMode to this value. Whenever -** possible, WAL and journal files are created using the same permissions +** But if the file being opened is a WAL or regular journal file, then +** this function queries the file-system for the permissions on the +** corresponding database file and sets *pMode to this value. Whenever +** possible, WAL and journal files are created using the same permissions ** as the associated database file. ** ** If the SQLITE_ENABLE_8_3_NAMES option is enabled, then the ** original filename is unavailable. But 8_3_NAMES is only used for ** FAT filesystems and permissions do not matter there, so just use -** the default permissions. +** the default permissions. In 8_3_NAMES mode, leave *pMode set to zero. */ static int findCreateFileMode( const char *zPath, /* Path of file (possibly) being created */ @@ -5538,7 +6469,6 @@ static int findCreateFileMode( if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){ char zDb[MAX_PATHNAME+1]; /* Database file path */ int nDb; /* Number of valid bytes in zDb */ - struct stat sStat; /* Output of stat() on database file */ /* zPath is a path to a WAL or journal file. The following block derives ** the path to the associated database file from zPath. This block handles @@ -5549,42 +6479,43 @@ static int findCreateFileMode( ** "-journalNN" ** "-walNN" ** - ** where NN is a decimal number. The NN naming schemes are + ** where NN is a decimal number. The NN naming schemes are ** used by the test_multiplex.c module. + ** + ** In normal operation, the journal file name will always contain + ** a '-' character. However in 8+3 filename mode, or if a corrupt + ** rollback journal specifies a super-journal with a goofy name, then + ** the '-' might be missing or the '-' might be the first character in + ** the filename. In that case, just return SQLITE_OK with *pMode==0. */ - nDb = sqlite3Strlen30(zPath) - 1; - while( zPath[nDb]!='-' ){ -#ifndef SQLITE_ENABLE_8_3_NAMES - /* In the normal case (8+3 filenames disabled) the journal filename - ** is guaranteed to contain a '-' character. */ - assert( nDb>0 ); - assert( sqlite3Isalnum(zPath[nDb]) ); -#else - /* If 8+3 names are possible, then the journal file might not contain - ** a '-' character. So check for that case and return early. */ - if( nDb==0 || zPath[nDb]=='.' ) return SQLITE_OK; -#endif + nDb = sqlite3Strlen30(zPath) - 1; + while( nDb>0 && zPath[nDb]!='.' ){ + if( zPath[nDb]=='-' ){ + memcpy(zDb, zPath, nDb); + zDb[nDb] = '\0'; + rc = getFileMode(zDb, pMode, pUid, pGid); + break; + } nDb--; } - memcpy(zDb, zPath, nDb); - zDb[nDb] = '\0'; - - if( 0==osStat(zDb, &sStat) ){ - *pMode = sStat.st_mode & 0777; - *pUid = sStat.st_uid; - *pGid = sStat.st_gid; - }else{ - rc = SQLITE_IOERR_FSTAT; - } }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){ *pMode = 0600; + }else if( flags & SQLITE_OPEN_URI ){ + /* If this is a main database file and the file was opened using a URI + ** filename, check for the "modeof" parameter. If present, interpret + ** its value as a filename and try to copy the mode, uid and gid from + ** that file. */ + const char *z = sqlite3_uri_parameter(zPath, "modeof"); + if( z ){ + rc = getFileMode(z, pMode, pUid, pGid); + } } return rc; } /* ** Open the file zPath. -** +** ** Previously, the SQLite OS layer used three functions in place of this ** one: ** @@ -5595,13 +6526,13 @@ static int findCreateFileMode( ** These calls correspond to the following combinations of flags: ** ** ReadWrite() -> (READWRITE | CREATE) -** ReadOnly() -> (READONLY) +** ReadOnly() -> (READONLY) ** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE) ** ** The old OpenExclusive() accepted a boolean argument - "delFlag". If ** true, the file was configured to be automatically deleted when the -** file handle closed. To achieve the same effect using this new -** interface, add the DELETEONCLOSE flag to those specified above for +** file handle closed. To achieve the same effect using this new +** interface, add the DELETEONCLOSE flag to those specified above for ** OpenExclusive(). */ static int unixOpen( @@ -5614,7 +6545,7 @@ static int unixOpen( unixFile *p = (unixFile *)pFile; int fd = -1; /* File descriptor returned by open() */ int openFlags = 0; /* Flags to pass to open() */ - int eType = flags&0xFFFFFF00; /* Type of file to open */ + int eType = flags&0x0FFF00; /* Type of file to open */ int noLock; /* True to omit locking primitives */ int rc = SQLITE_OK; /* Function Return Code */ int ctrlFlags = 0; /* UNIXFILE_* flags */ @@ -5631,13 +6562,13 @@ static int unixOpen( struct statfs fsInfo; #endif - /* If creating a master or main-file journal, this function will open + /* If creating a super- or main-file journal, this function will open ** a file-descriptor on the directory too. The first time unixSync() ** is called the directory file descriptor will be fsync()ed and close()d. */ - int syncDir = (isCreate && ( - eType==SQLITE_OPEN_MASTER_JOURNAL - || eType==SQLITE_OPEN_MAIN_JOURNAL + int isNewJrnl = (isCreate && ( + eType==SQLITE_OPEN_SUPER_JOURNAL + || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_WAL )); @@ -5647,9 +6578,9 @@ static int unixOpen( char zTmpname[MAX_PATHNAME+2]; const char *zName = zPath; - /* Check the following statements are true: + /* Check the following statements are true: ** - ** (a) Exactly one of the READWRITE and READONLY flags must be set, and + ** (a) Exactly one of the READWRITE and READONLY flags must be set, and ** (b) if CREATE is set, then READWRITE must also be set, and ** (c) if EXCLUSIVE is set, then CREATE must also be set. ** (d) if DELETEONCLOSE is set, then CREATE must also be set. @@ -5659,20 +6590,26 @@ static int unixOpen( assert(isExclusive==0 || isCreate); assert(isDelete==0 || isCreate); - /* The main DB, main journal, WAL file and master journal are never + /* The main DB, main journal, WAL file and super-journal are never ** automatically deleted. Nor are they ever temporary files. */ assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB ); assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL ); - assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL ); + assert( (!isDelete && zName) || eType!=SQLITE_OPEN_SUPER_JOURNAL ); assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL ); /* Assert that the upper layer has set one of the "file-type" flags. */ - assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB - || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL - || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL + assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB + || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL + || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_SUPER_JOURNAL || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL ); +#if OS_VXWORKS + /* The file-ID mechanism used in Vxworks requires that all pathnames + ** provided to unixOpen must be absolute pathnames. */ + if( zPath!=0 && zPath[0]!='/' ){ return SQLITE_CANTOPEN; } +#endif + /* Detect a pid change and reset the PRNG. There is a race condition ** here such that two or more threads all trying to open databases at ** the same instant might all reset the PRNG. But multiple resets @@ -5682,9 +6619,13 @@ static int unixOpen( randomnessPid = osGetpid(0); sqlite3_randomness(0,0); } - memset(p, 0, sizeof(unixFile)); +#ifdef SQLITE_ASSERT_NO_FILES + /* Applications that never read or write a persistent disk files */ + assert( zName==0 ); +#endif + if( eType==SQLITE_OPEN_MAIN_DB ){ UnixUnusedFd *pUnused; pUnused = findReusableFd(zName, flags); @@ -5693,10 +6634,10 @@ static int unixOpen( }else{ pUnused = sqlite3_malloc64(sizeof(*pUnused)); if( !pUnused ){ - return SQLITE_NOMEM; + return SQLITE_NOMEM_BKPT; } } - p->pUnused = pUnused; + p->pPreallocatedUnused = pUnused; /* Database filenames are double-zero terminated if they are not ** URIs with parameters. Hence, they can always be passed into @@ -5705,7 +6646,7 @@ static int unixOpen( }else if( !zName ){ /* If zName is NULL, the upper layer is requesting a temp file. */ - assert(isDelete && !syncDir); + assert(isDelete && !isNewJrnl); rc = unixGetTempname(pVfs->mxPathname, zTmpname); if( rc!=SQLITE_OK ){ return rc; @@ -5719,13 +6660,13 @@ static int unixOpen( /* Determine the value of the flags parameter passed to POSIX function ** open(). These must be calculated even if open() is not called, as - ** they may be stored as part of the file handle and used by the + ** they may be stored as part of the file handle and used by the ** 'conch file' locking functions later on. */ if( isReadonly ) openFlags |= O_RDONLY; if( isReadWrite ) openFlags |= O_RDWR; if( isCreate ) openFlags |= O_CREAT; if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW); - openFlags |= (O_LARGEFILE|O_BINARY); + openFlags |= (O_LARGEFILE|O_BINARY|O_NOFOLLOW); if( fd<0 ){ mode_t openMode; /* Permissions to create file with */ @@ -5733,32 +6674,54 @@ static int unixOpen( gid_t gid; /* Groupid for the file */ rc = findCreateFileMode(zName, flags, &openMode, &uid, &gid); if( rc!=SQLITE_OK ){ - assert( !p->pUnused ); + assert( !p->pPreallocatedUnused ); assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL ); return rc; } fd = robust_open(zName, openFlags, openMode); OSTRACE(("OPENX %-3d %s 0%o\n", fd, zName, openFlags)); assert( !isExclusive || (openFlags & O_CREAT)!=0 ); - if( fd<0 && errno!=EISDIR && isReadWrite ){ - /* Failed to open the file for read/write access. Try read-only. */ - flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); - openFlags &= ~(O_RDWR|O_CREAT); - flags |= SQLITE_OPEN_READONLY; - openFlags |= O_RDONLY; - isReadonly = 1; - fd = robust_open(zName, openFlags, openMode); + if( fd<0 ){ + if( isNewJrnl && errno==EACCES && osAccess(zName, F_OK) ){ + /* If unable to create a journal because the directory is not + ** writable, change the error code to indicate that. */ + rc = SQLITE_READONLY_DIRECTORY; + }else if( errno!=EISDIR && isReadWrite ){ + /* Failed to open the file for read/write access. Try read-only. */ + UnixUnusedFd *pReadonly = 0; + flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); + openFlags &= ~(O_RDWR|O_CREAT); + flags |= SQLITE_OPEN_READONLY; + openFlags |= O_RDONLY; + isReadonly = 1; + pReadonly = findReusableFd(zName, flags); + if( pReadonly ){ + fd = pReadonly->fd; + sqlite3_free(pReadonly); + }else{ + fd = robust_open(zName, openFlags, openMode); + } + } } if( fd<0 ){ - rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName); + int rc2 = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName); + if( rc==SQLITE_OK ) rc = rc2; goto open_finished; } - /* If this process is running as root and if creating a new rollback - ** journal or WAL file, set the ownership of the journal or WAL to be - ** the same as the original database. + /* The owner of the rollback journal or WAL file should always be the + ** same as the owner of the database file. Try to ensure that this is + ** the case. The chown() system call will be a no-op if the current + ** process lacks root privileges, be we should at least try. Without + ** this step, if a root process opens a database file, it can leave + ** behinds a journal/WAL that is owned by root and hence make the + ** database inaccessible to unprivileged processes. + ** + ** If openMode==0, then that means uid and gid are not set correctly + ** (probably because SQLite is configured to use 8+3 filename mode) and + ** in that case we do not want to attempt the chown(). */ - if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){ + if( openMode && (flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL))!=0 ){ robustFchown(fd, uid, gid); } } @@ -5767,9 +6730,10 @@ static int unixOpen( *pOutFlags = flags; } - if( p->pUnused ){ - p->pUnused->fd = fd; - p->pUnused->flags = flags; + if( p->pPreallocatedUnused ){ + p->pPreallocatedUnused->fd = fd; + p->pPreallocatedUnused->flags = + flags & (SQLITE_OPEN_READONLY|SQLITE_OPEN_READWRITE); } if( isDelete ){ @@ -5779,7 +6743,7 @@ static int unixOpen( zPath = sqlite3_mprintf("%s", zName); if( zPath==0 ){ robust_close(p, fd, __LINE__); - return SQLITE_NOMEM; + return SQLITE_NOMEM_BKPT; } #else osUnlink(zName); @@ -5791,9 +6755,6 @@ static int unixOpen( } #endif - noLock = eType!=SQLITE_OPEN_MAIN_DB; - - #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE if( fstatfs(fd, &fsInfo) == -1 ){ storeLastErrno(p, errno); @@ -5811,8 +6772,9 @@ static int unixOpen( /* Set up appropriate ctrlFlags */ if( isDelete ) ctrlFlags |= UNIXFILE_DELETE; if( isReadonly ) ctrlFlags |= UNIXFILE_RDONLY; + noLock = eType!=SQLITE_OPEN_MAIN_DB; if( noLock ) ctrlFlags |= UNIXFILE_NOLOCK; - if( syncDir ) ctrlFlags |= UNIXFILE_DIRSYNC; + if( isNewJrnl ) ctrlFlags |= UNIXFILE_DIRSYNC; if( flags & SQLITE_OPEN_URI ) ctrlFlags |= UNIXFILE_URI; #if SQLITE_ENABLE_LOCKING_STYLE @@ -5823,7 +6785,7 @@ static int unixOpen( char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING"); int useProxy = 0; - /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means + /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means ** never use proxy, NULL means use proxy for non-local files only. */ if( envforce!=NULL ){ useProxy = atoi(envforce)>0; @@ -5835,9 +6797,9 @@ static int unixOpen( if( rc==SQLITE_OK ){ rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:"); if( rc!=SQLITE_OK ){ - /* Use unixClose to clean up the resources added in fillInUnixFile - ** and clear all the structure's references. Specifically, - ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op + /* Use unixClose to clean up the resources added in fillInUnixFile + ** and clear all the structure's references. Specifically, + ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op */ unixClose(pFile); return rc; @@ -5847,12 +6809,18 @@ static int unixOpen( } } #endif - + + assert( zPath==0 + || zPath[0]=='/' + || eType==SQLITE_OPEN_SUPER_JOURNAL + || eType==SQLITE_OPEN_MAIN_JOURNAL + || eType==SQLITE_OPEN_TEMP_JOURNAL + ); rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags); open_finished: if( rc!=SQLITE_OK ){ - sqlite3_free(p->pUnused); + sqlite3_free(p->pPreallocatedUnused); } return rc; } @@ -5926,7 +6894,8 @@ static int unixAccess( if( flags==SQLITE_ACCESS_EXISTS ){ struct stat buf; - *pResOut = (0==osStat(zPath, &buf) && buf.st_size>0); + *pResOut = 0==osStat(zPath, &buf) && + (!S_ISREG(buf.st_mode) || buf.st_size>0); }else{ *pResOut = osAccess(zPath, W_OK|R_OK)==0; } @@ -5934,38 +6903,105 @@ static int unixAccess( } /* -** +** A pathname under construction */ -static int mkFullPathname( - const char *zPath, /* Input path */ - char *zOut, /* Output buffer */ - int nOut /* Allocated size of buffer zOut */ +typedef struct DbPath DbPath; +struct DbPath { + int rc; /* Non-zero following any error */ + int nSymlink; /* Number of symlinks resolved */ + char *zOut; /* Write the pathname here */ + int nOut; /* Bytes of space available to zOut[] */ + int nUsed; /* Bytes of zOut[] currently being used */ +}; + +/* Forward reference */ +static void appendAllPathElements(DbPath*,const char*); + +/* +** Append a single path element to the DbPath under construction +*/ +static void appendOnePathElement( + DbPath *pPath, /* Path under construction, to which to append zName */ + const char *zName, /* Name to append to pPath. Not zero-terminated */ + int nName /* Number of significant bytes in zName */ ){ - int nPath = sqlite3Strlen30(zPath); - int iOff = 0; - if( zPath[0]!='/' ){ - if( osGetcwd(zOut, nOut-2)==0 ){ - return unixLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath); + assert( nName>0 ); + assert( zName!=0 ); + if( zName[0]=='.' ){ + if( nName==1 ) return; + if( zName[1]=='.' && nName==2 ){ + if( pPath->nUsed>1 ){ + assert( pPath->zOut[0]=='/' ); + while( pPath->zOut[--pPath->nUsed]!='/' ){} + } + return; } - iOff = sqlite3Strlen30(zOut); - zOut[iOff++] = '/'; } - if( (iOff+nPath+1)>nOut ){ - /* SQLite assumes that xFullPathname() nul-terminates the output buffer - ** even if it returns an error. */ - zOut[iOff] = '\0'; - return SQLITE_CANTOPEN_BKPT; + if( pPath->nUsed + nName + 2 >= pPath->nOut ){ + pPath->rc = SQLITE_ERROR; + return; } - sqlite3_snprintf(nOut-iOff, &zOut[iOff], "%s", zPath); - return SQLITE_OK; + pPath->zOut[pPath->nUsed++] = '/'; + memcpy(&pPath->zOut[pPath->nUsed], zName, nName); + pPath->nUsed += nName; +#if defined(HAVE_READLINK) && defined(HAVE_LSTAT) + if( pPath->rc==SQLITE_OK ){ + const char *zIn; + struct stat buf; + pPath->zOut[pPath->nUsed] = 0; + zIn = pPath->zOut; + if( osLstat(zIn, &buf)!=0 ){ + if( errno!=ENOENT ){ + pPath->rc = unixLogError(SQLITE_CANTOPEN_BKPT, "lstat", zIn); + } + }else if( S_ISLNK(buf.st_mode) ){ + ssize_t got; + char zLnk[SQLITE_MAX_PATHLEN+2]; + if( pPath->nSymlink++ > SQLITE_MAX_SYMLINK ){ + pPath->rc = SQLITE_CANTOPEN_BKPT; + return; + } + got = osReadlink(zIn, zLnk, sizeof(zLnk)-2); + if( got<=0 || got>=(ssize_t)sizeof(zLnk)-2 ){ + pPath->rc = unixLogError(SQLITE_CANTOPEN_BKPT, "readlink", zIn); + return; + } + zLnk[got] = 0; + if( zLnk[0]=='/' ){ + pPath->nUsed = 0; + }else{ + pPath->nUsed -= nName + 1; + } + appendAllPathElements(pPath, zLnk); + } + } +#endif +} + +/* +** Append all path elements in zPath to the DbPath under construction. +*/ +static void appendAllPathElements( + DbPath *pPath, /* Path under construction, to which to append zName */ + const char *zPath /* Path to append to pPath. Is zero-terminated */ +){ + int i = 0; + int j = 0; + do{ + while( zPath[i] && zPath[i]!='/' ){ i++; } + if( i>j ){ + appendOnePathElement(pPath, &zPath[j], i-j); + } + j = i+1; + }while( zPath[i++] ); } /* ** Turn a relative pathname into a full pathname. The relative path ** is stored as a nul-terminated string in the buffer pointed to by -** zPath. +** zPath. ** -** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes +** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes ** (in this case, MAX_PATHNAME bytes). The full-path is written to ** this buffer before returning. */ @@ -5975,84 +7011,27 @@ static int unixFullPathname( int nOut, /* Size of output buffer in bytes */ char *zOut /* Output buffer */ ){ -#if !defined(HAVE_READLINK) || !defined(HAVE_LSTAT) - return mkFullPathname(zPath, zOut, nOut); -#else - int rc = SQLITE_OK; - int nByte; - int nLink = 1; /* Number of symbolic links followed so far */ - const char *zIn = zPath; /* Input path for each iteration of loop */ - char *zDel = 0; - - assert( pVfs->mxPathname==MAX_PATHNAME ); + DbPath path; UNUSED_PARAMETER(pVfs); - - /* It's odd to simulate an io-error here, but really this is just - ** using the io-error infrastructure to test that SQLite handles this - ** function failing. This function could fail if, for example, the - ** current working directory has been unlinked. - */ - SimulateIOError( return SQLITE_ERROR ); - - do { - - /* Call stat() on path zIn. Set bLink to true if the path is a symbolic - ** link, or false otherwise. */ - int bLink = 0; - struct stat buf; - if( osLstat(zIn, &buf)!=0 ){ - if( errno!=ENOENT ){ - rc = unixLogError(SQLITE_CANTOPEN_BKPT, "lstat", zIn); - } - }else{ - bLink = S_ISLNK(buf.st_mode); - } - - if( bLink ){ - if( zDel==0 ){ - zDel = sqlite3_malloc(nOut); - if( zDel==0 ) rc = SQLITE_NOMEM; - }else if( ++nLink>SQLITE_MAX_SYMLINKS ){ - rc = SQLITE_CANTOPEN_BKPT; - } - - if( rc==SQLITE_OK ){ - nByte = osReadlink(zIn, zDel, nOut-1); - if( nByte<0 ){ - rc = unixLogError(SQLITE_CANTOPEN_BKPT, "readlink", zIn); - }else{ - if( zDel[0]!='/' ){ - int n; - for(n = sqlite3Strlen30(zIn); n>0 && zIn[n-1]!='/'; n--); - if( nByte+n+1>nOut ){ - rc = SQLITE_CANTOPEN_BKPT; - }else{ - memmove(&zDel[n], zDel, nByte+1); - memcpy(zDel, zIn, n); - nByte += n; - } - } - zDel[nByte] = '\0'; - } - } - - zIn = zDel; - } - - assert( rc!=SQLITE_OK || zIn!=zOut || zIn[0]=='/' ); - if( rc==SQLITE_OK && zIn!=zOut ){ - rc = mkFullPathname(zIn, zOut, nOut); + path.rc = 0; + path.nUsed = 0; + path.nSymlink = 0; + path.nOut = nOut; + path.zOut = zOut; + if( zPath[0]!='/' ){ + char zPwd[SQLITE_MAX_PATHLEN+2]; + if( osGetcwd(zPwd, sizeof(zPwd)-2)==0 ){ + return unixLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath); } - if( bLink==0 ) break; - zIn = zOut; - }while( rc==SQLITE_OK ); - - sqlite3_free(zDel); - return rc; -#endif /* HAVE_READLINK && HAVE_LSTAT */ + appendAllPathElements(&path, zPwd); + } + appendAllPathElements(&path, zPath); + zOut[path.nUsed] = 0; + if( path.rc || path.nUsed<2 ) return SQLITE_CANTOPEN_BKPT; + if( path.nSymlink ) return SQLITE_OK_SYMLINK; + return SQLITE_OK; } - #ifndef SQLITE_OMIT_LOAD_EXTENSION /* ** Interfaces for opening a shared library, finding entry points @@ -6082,7 +7061,7 @@ static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){ unixLeaveMutex(); } static void (*unixDlSym(sqlite3_vfs *NotUsed, void *p, const char*zSym))(void){ - /* + /* ** GCC with -pedantic-errors says that C90 does not allow a void* to be ** cast into a pointer to a function. And yet the library dlsym() routine ** returns a void* which is really a pointer to a function. So how do we @@ -6092,7 +7071,7 @@ static void (*unixDlSym(sqlite3_vfs *NotUsed, void *p, const char*zSym))(void){ ** parameters void* and const char* and returning a pointer to a function. ** We initialize x by assigning it a pointer to the dlsym() function. ** (That assignment requires a cast.) Then we call the function that - ** x points to. + ** x points to. ** ** This work-around is unlikely to work correctly on any system where ** you really cannot cast a function pointer into void*. But then, on the @@ -6135,7 +7114,7 @@ static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){ ** tests repeatable. */ memset(zBuf, 0, nBuf); - randomnessPid = osGetpid(0); + randomnessPid = osGetpid(0); #if !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS) { int fd, got; @@ -6166,16 +7145,22 @@ static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){ ** than the argument. */ static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){ -#if OS_VXWORKS +#if !defined(HAVE_NANOSLEEP) || HAVE_NANOSLEEP+0 struct timespec sp; - sp.tv_sec = microseconds / 1000000; sp.tv_nsec = (microseconds % 1000000) * 1000; + + /* Almost all modern unix systems support nanosleep(). But if you are + ** compiling for one of the rare exceptions, you can use + ** -DHAVE_NANOSLEEP=0 (perhaps in conjunction with -DHAVE_USLEEP if + ** usleep() is available) in order to bypass the use of nanosleep() */ nanosleep(&sp, NULL); + UNUSED_PARAMETER(NotUsed); return microseconds; #elif defined(HAVE_USLEEP) && HAVE_USLEEP - usleep(microseconds); + if( microseconds>=1000000 ) sleep(microseconds/1000000); + if( microseconds%1000000 ) usleep(microseconds%1000000); UNUSED_PARAMETER(NotUsed); return microseconds; #else @@ -6202,7 +7187,7 @@ int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */ ** epoch of noon in Greenwich on November 24, 4714 B.C according to the ** proleptic Gregorian calendar. ** -** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date +** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date ** cannot be found. */ static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){ @@ -6249,23 +7234,18 @@ static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){ # define unixCurrentTime 0 #endif -#ifndef SQLITE_OMIT_DEPRECATED /* -** We added the xGetLastError() method with the intention of providing -** better low-level error messages when operating-system problems come up -** during SQLite operation. But so far, none of that has been implemented -** in the core. So this routine is never called. For now, it is merely -** a place-holder. +** The xGetLastError() method is designed to return a better +** low-level error message when operating-system problems come up +** during SQLite operation. Only the integer return code is currently +** used. */ static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){ UNUSED_PARAMETER(NotUsed); UNUSED_PARAMETER(NotUsed2); UNUSED_PARAMETER(NotUsed3); - return 0; + return errno; } -#else -# define unixGetLastError 0 -#endif /* @@ -6314,7 +7294,7 @@ static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){ ** To address the performance and cache coherency issues, proxy file locking ** changes the way database access is controlled by limiting access to a ** single host at a time and moving file locks off of the database file -** and onto a proxy file on the local file system. +** and onto a proxy file on the local file system. ** ** ** Using proxy locks @@ -6340,19 +7320,19 @@ static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){ ** actual proxy file name is generated from the name and path of the ** database file. For example: ** -** For database path "/Users/me/foo.db" +** For database path "/Users/me/foo.db" ** The lock path will be "/sqliteplocks/_Users_me_foo.db:auto:") ** ** Once a lock proxy is configured for a database connection, it can not ** be removed, however it may be switched to a different proxy path via ** the above APIs (assuming the conch file is not being held by another -** connection or process). +** connection or process). ** ** ** How proxy locking works ** ----------------------- ** -** Proxy file locking relies primarily on two new supporting files: +** Proxy file locking relies primarily on two new supporting files: ** ** * conch file to limit access to the database file to a single host ** at a time @@ -6379,11 +7359,11 @@ static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){ ** host (the conch ensures that they all use the same local lock file). ** ** Requesting the lock proxy does not immediately take the conch, it is -** only taken when the first request to lock database file is made. +** only taken when the first request to lock database file is made. ** This matches the semantics of the traditional locking behavior, where ** opening a connection to a database file does not take a lock on it. -** The shared lock and an open file descriptor are maintained until -** the connection to the database is closed. +** The shared lock and an open file descriptor are maintained until +** the connection to the database is closed. ** ** The proxy file and the lock file are never deleted so they only need ** to be created the first time they are used. @@ -6397,7 +7377,7 @@ static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){ ** automatically configured for proxy locking, lock files are ** named automatically using the same logic as ** PRAGMA lock_proxy_file=":auto:" -** +** ** SQLITE_PROXY_DEBUG ** ** Enables the logging of error messages during host id file @@ -6412,8 +7392,8 @@ static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){ ** ** Permissions to use when creating a directory for storing the ** lock proxy files, only used when LOCKPROXYDIR is not set. -** -** +** +** ** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING, ** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will ** force proxy locking to be used for every database file opened, and 0 @@ -6423,12 +7403,12 @@ static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){ */ /* -** Proxy locking is only available on MacOSX +** Proxy locking is only available on MacOSX */ #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE /* -** The proxyLockingContext has the path and file structures for the remote +** The proxyLockingContext has the path and file structures for the remote ** and local proxy files in it */ typedef struct proxyLockingContext proxyLockingContext; @@ -6444,10 +7424,10 @@ struct proxyLockingContext { sqlite3_io_methods const *pOldMethod; /* Original I/O methods for close */ }; -/* -** The proxy lock file path for the database at dbPath is written into lPath, +/* +** The proxy lock file path for the database at dbPath is written into lPath, ** which must point to valid, writable memory large enough for a maxLen length -** file path. +** file path. */ static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){ int len; @@ -6464,7 +7444,7 @@ static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){ lPath, errno, osGetpid(0))); return SQLITE_IOERR_LOCK; } - len = strlcat(lPath, "sqliteplocks", maxLen); + len = strlcat(lPath, "sqliteplocks", maxLen); } # else len = strlcpy(lPath, "/tmp/", maxLen); @@ -6474,7 +7454,7 @@ static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){ if( lPath[len-1]!='/' ){ len = strlcat(lPath, "/", maxLen); } - + /* transform the db path to a unique cache name */ dbLen = (int)strlen(dbPath); for( i=0; i 0) ){ /* only mkdir if leaf dir != "." or "/" or ".." */ - if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/') + if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/') || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){ buf[i]='\0'; if( osMkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){ @@ -6538,7 +7518,7 @@ static int proxyCreateUnixFile( int fd = -1; unixFile *pNew; int rc = SQLITE_OK; - int openFlags = O_RDWR | O_CREAT; + int openFlags = O_RDWR | O_CREAT | O_NOFOLLOW; sqlite3_vfs dummyVfs; int terrno = 0; UnixUnusedFd *pUnused = NULL; @@ -6555,7 +7535,7 @@ static int proxyCreateUnixFile( }else{ pUnused = sqlite3_malloc64(sizeof(*pUnused)); if( !pUnused ){ - return SQLITE_NOMEM; + return SQLITE_NOMEM_BKPT; } } if( fd<0 ){ @@ -6568,7 +7548,7 @@ static int proxyCreateUnixFile( } } if( fd<0 ){ - openFlags = O_RDONLY; + openFlags = O_RDONLY | O_NOFOLLOW; fd = robust_open(path, openFlags, 0); terrno = errno; } @@ -6579,16 +7559,16 @@ static int proxyCreateUnixFile( switch (terrno) { case EACCES: return SQLITE_PERM; - case EIO: + case EIO: return SQLITE_IOERR_LOCK; /* even though it is the conch */ default: return SQLITE_CANTOPEN_BKPT; } } - + pNew = (unixFile *)sqlite3_malloc64(sizeof(*pNew)); if( pNew==NULL ){ - rc = SQLITE_NOMEM; + rc = SQLITE_NOMEM_BKPT; goto end_create_proxy; } memset(pNew, 0, sizeof(unixFile)); @@ -6598,14 +7578,14 @@ static int proxyCreateUnixFile( dummyVfs.zName = "dummy"; pUnused->fd = fd; pUnused->flags = openFlags; - pNew->pUnused = pUnused; - + pNew->pPreallocatedUnused = pUnused; + rc = fillInUnixFile(&dummyVfs, fd, (sqlite3_file*)pNew, path, 0); if( rc==SQLITE_OK ){ *ppFile = pNew; return SQLITE_OK; } -end_create_proxy: +end_create_proxy: robust_close(pNew, fd, __LINE__); sqlite3_free(pNew); sqlite3_free(pUnused); @@ -6619,18 +7599,18 @@ int sqlite3_hostid_num = 0; #define PROXY_HOSTIDLEN 16 /* conch file host id length */ -#ifdef HAVE_GETHOSTUUID +#if HAVE_GETHOSTUUID /* Not always defined in the headers as it ought to be */ extern int gethostuuid(uuid_t id, const struct timespec *wait); #endif -/* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN +/* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN ** bytes of writable memory. */ static int proxyGetHostID(unsigned char *pHostID, int *pError){ assert(PROXY_HOSTIDLEN == sizeof(uuid_t)); memset(pHostID, 0, PROXY_HOSTIDLEN); -#ifdef HAVE_GETHOSTUUID +#if HAVE_GETHOSTUUID { struct timespec timeout = {1, 0}; /* 1 sec timeout */ if( gethostuuid(pHostID, &timeout) ){ @@ -6650,7 +7630,7 @@ static int proxyGetHostID(unsigned char *pHostID, int *pError){ pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF)); } #endif - + return SQLITE_OK; } @@ -6661,14 +7641,14 @@ static int proxyGetHostID(unsigned char *pHostID, int *pError){ #define PROXY_PATHINDEX (PROXY_HEADERLEN+PROXY_HOSTIDLEN) #define PROXY_MAXCONCHLEN (PROXY_HEADERLEN+PROXY_HOSTIDLEN+MAXPATHLEN) -/* -** Takes an open conch file, copies the contents to a new path and then moves +/* +** Takes an open conch file, copies the contents to a new path and then moves ** it back. The newly created file's file descriptor is assigned to the -** conch file structure and finally the original conch file descriptor is +** conch file structure and finally the original conch file descriptor is ** closed. Returns zero if successful. */ static int proxyBreakConchLock(unixFile *pFile, uuid_t myHostID){ - proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; + proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; unixFile *conchFile = pCtx->conchFile; char tPath[MAXPATHLEN]; char buf[PROXY_MAXCONCHLEN]; @@ -6682,7 +7662,7 @@ static int proxyBreakConchLock(unixFile *pFile, uuid_t myHostID){ /* create a new path by replace the trailing '-conch' with '-break' */ pathLen = strlcpy(tPath, cPath, MAXPATHLEN); - if( pathLen>MAXPATHLEN || pathLen<6 || + if( pathLen>MAXPATHLEN || pathLen<6 || (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){ sqlite3_snprintf(sizeof(errmsg),errmsg,"path error (len %d)",(int)pathLen); goto end_breaklock; @@ -6694,7 +7674,7 @@ static int proxyBreakConchLock(unixFile *pFile, uuid_t myHostID){ goto end_breaklock; } /* write it out to the temporary break file */ - fd = robust_open(tPath, (O_RDWR|O_CREAT|O_EXCL), 0); + fd = robust_open(tPath, (O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW), 0); if( fd<0 ){ sqlite3_snprintf(sizeof(errmsg), errmsg, "create failed (%d)", errno); goto end_breaklock; @@ -6724,24 +7704,24 @@ static int proxyBreakConchLock(unixFile *pFile, uuid_t myHostID){ return rc; } -/* Take the requested lock on the conch file and break a stale lock if the +/* Take the requested lock on the conch file and break a stale lock if the ** host id matches. */ static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){ - proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; + proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; unixFile *conchFile = pCtx->conchFile; int rc = SQLITE_OK; int nTries = 0; struct timespec conchModTime; - + memset(&conchModTime, 0, sizeof(conchModTime)); do { rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType); nTries ++; if( rc==SQLITE_BUSY ){ /* If the lock failed (busy): - * 1st try: get the mod time of the conch, wait 0.5s and try again. - * 2nd try: fail if the mod time changed or host id is different, wait + * 1st try: get the mod time of the conch, wait 0.5s and try again. + * 2nd try: fail if the mod time changed or host id is different, wait * 10 sec and try again * 3rd try: break the lock unless the mod time has changed. */ @@ -6750,20 +7730,20 @@ static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){ storeLastErrno(pFile, errno); return SQLITE_IOERR_LOCK; } - + if( nTries==1 ){ conchModTime = buf.st_mtimespec; - usleep(500000); /* wait 0.5 sec and try the lock again*/ - continue; + unixSleep(0,500000); /* wait 0.5 sec and try the lock again*/ + continue; } assert( nTries>1 ); - if( conchModTime.tv_sec != buf.st_mtimespec.tv_sec || + if( conchModTime.tv_sec != buf.st_mtimespec.tv_sec || conchModTime.tv_nsec != buf.st_mtimespec.tv_nsec ){ return SQLITE_BUSY; } - - if( nTries==2 ){ + + if( nTries==2 ){ char tBuf[PROXY_MAXCONCHLEN]; int len = osPread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0); if( len<0 ){ @@ -6779,10 +7759,10 @@ static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){ /* don't break the lock on short read or a version mismatch */ return SQLITE_BUSY; } - usleep(10000000); /* wait 10 sec and try the lock again */ - continue; + unixSleep(0,10000000); /* wait 10 sec and try the lock again */ + continue; } - + assert( nTries==3 ); if( 0==proxyBreakConchLock(pFile, myHostID) ){ rc = SQLITE_OK; @@ -6795,19 +7775,19 @@ static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){ } } } while( rc==SQLITE_BUSY && nTries<3 ); - + return rc; } -/* Takes the conch by taking a shared lock and read the contents conch, if -** lockPath is non-NULL, the host ID and lock file path must match. A NULL -** lockPath means that the lockPath in the conch file will be used if the -** host IDs match, or a new lock path will be generated automatically +/* Takes the conch by taking a shared lock and read the contents conch, if +** lockPath is non-NULL, the host ID and lock file path must match. A NULL +** lockPath means that the lockPath in the conch file will be used if the +** host IDs match, or a new lock path will be generated automatically ** and written to the conch file. */ static int proxyTakeConch(unixFile *pFile){ - proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; - + proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; + if( pCtx->conchHeld!=0 ){ return SQLITE_OK; }else{ @@ -6823,7 +7803,7 @@ static int proxyTakeConch(unixFile *pFile){ int readLen = 0; int tryOldLockPath = 0; int forceNewLockPath = 0; - + OSTRACE(("TAKECONCH %d for %s pid=%d\n", conchFile->h, (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), osGetpid(0))); @@ -6844,21 +7824,21 @@ static int proxyTakeConch(unixFile *pFile){ storeLastErrno(pFile, conchFile->lastErrno); rc = SQLITE_IOERR_READ; goto end_takeconch; - }else if( readLen<=(PROXY_HEADERLEN+PROXY_HOSTIDLEN) || + }else if( readLen<=(PROXY_HEADERLEN+PROXY_HOSTIDLEN) || readBuf[0]!=(char)PROXY_CONCHVERSION ){ - /* a short read or version format mismatch means we need to create a new - ** conch file. + /* a short read or version format mismatch means we need to create a new + ** conch file. */ createConch = 1; } /* if the host id matches and the lock path already exists in the conch - ** we'll try to use the path there, if we can't open that path, we'll - ** retry with a new auto-generated path + ** we'll try to use the path there, if we can't open that path, we'll + ** retry with a new auto-generated path */ do { /* in case we need to try again for an :auto: named lock file */ if( !createConch && !forceNewLockPath ){ - hostIdMatch = !memcmp(&readBuf[PROXY_HEADERLEN], myHostID, + hostIdMatch = !memcmp(&readBuf[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN); /* if the conch has data compare the contents */ if( !pCtx->lockProxyPath ){ @@ -6867,7 +7847,7 @@ static int proxyTakeConch(unixFile *pFile){ */ if( hostIdMatch ){ size_t pathLen = (readLen - PROXY_PATHINDEX); - + if( pathLen>=MAXPATHLEN ){ pathLen=MAXPATHLEN-1; } @@ -6883,23 +7863,23 @@ static int proxyTakeConch(unixFile *pFile){ readLen-PROXY_PATHINDEX) ){ /* conch host and lock path match */ - goto end_takeconch; + goto end_takeconch; } } - + /* if the conch isn't writable and doesn't match, we can't take it */ if( (conchFile->openFlags&O_RDWR) == 0 ){ rc = SQLITE_BUSY; goto end_takeconch; } - + /* either the conch didn't match or we need to create a new one */ if( !pCtx->lockProxyPath ){ proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN); tempLockPath = lockPath; /* create a copy of the lock path _only_ if the conch is taken */ } - + /* update conch with host and path (this will fail if other process ** has a shared lock already), if the host id matches, use the big ** stick. @@ -6910,7 +7890,7 @@ static int proxyTakeConch(unixFile *pFile){ /* We are trying for an exclusive lock but another thread in this ** same process is still holding a shared lock. */ rc = SQLITE_BUSY; - } else { + } else { rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK); } }else{ @@ -6919,7 +7899,7 @@ static int proxyTakeConch(unixFile *pFile){ if( rc==SQLITE_OK ){ char writeBuffer[PROXY_MAXCONCHLEN]; int writeSize = 0; - + writeBuffer[0] = (char)PROXY_CONCHVERSION; memcpy(&writeBuffer[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN); if( pCtx->lockProxyPath!=NULL ){ @@ -6932,8 +7912,8 @@ static int proxyTakeConch(unixFile *pFile){ robust_ftruncate(conchFile->h, writeSize); rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0); full_fsync(conchFile->h,0,0); - /* If we created a new conch file (not just updated the contents of a - ** valid conch file), try to match the permissions of the database + /* If we created a new conch file (not just updated the contents of a + ** valid conch file), try to match the permissions of the database */ if( rc==SQLITE_OK && createConch ){ struct stat buf; @@ -6957,14 +7937,14 @@ static int proxyTakeConch(unixFile *pFile){ } }else{ int code = errno; - fprintf(stderr, "STAT FAILED[%d] with %d %s\n", + fprintf(stderr, "STAT FAILED[%d] with %d %s\n", err, code, strerror(code)); #endif } } } conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK); - + end_takeconch: OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h)); if( rc==SQLITE_OK && pFile->openFlags ){ @@ -6987,7 +7967,7 @@ static int proxyTakeConch(unixFile *pFile){ rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1); if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && tryOldLockPath ){ /* we couldn't create the proxy lock file with the old lock file path - ** so try again via auto-naming + ** so try again via auto-naming */ forceNewLockPath = 1; tryOldLockPath = 0; @@ -7001,13 +7981,13 @@ static int proxyTakeConch(unixFile *pFile){ if( tempLockPath ){ pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath); if( !pCtx->lockProxyPath ){ - rc = SQLITE_NOMEM; + rc = SQLITE_NOMEM_BKPT; } } } if( rc==SQLITE_OK ){ pCtx->conchHeld = 1; - + if( pCtx->lockProxy->pMethod == &afpIoMethods ){ afpLockingContext *afpCtx; afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext; @@ -7019,7 +7999,7 @@ static int proxyTakeConch(unixFile *pFile){ OSTRACE(("TAKECONCH %d %s\n", conchFile->h, rc==SQLITE_OK?"ok":"failed")); return rc; - } while (1); /* in case we need to retry the :auto: lock file - + } while (1); /* in case we need to retry the :auto: lock file - ** we should never get here except via the 'continue' call. */ } } @@ -7035,7 +8015,7 @@ static int proxyReleaseConch(unixFile *pFile){ pCtx = (proxyLockingContext *)pFile->lockingContext; conchFile = pCtx->conchFile; OSTRACE(("RELEASECONCH %d for %s pid=%d\n", conchFile->h, - (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), + (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), osGetpid(0))); if( pCtx->conchHeld>0 ){ rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK); @@ -7063,13 +8043,13 @@ static int proxyCreateConchPathname(char *dbPath, char **pConchPath){ char *conchPath; /* buffer in which to construct conch name */ /* Allocate space for the conch filename and initialize the name to - ** the name of the original database file. */ + ** the name of the original database file. */ *pConchPath = conchPath = (char *)sqlite3_malloc64(len + 8); if( conchPath==0 ){ - return SQLITE_NOMEM; + return SQLITE_NOMEM_BKPT; } memcpy(conchPath, dbPath, len+1); - + /* now insert a "." before the last / character */ for( i=(len-1); i>=0; i-- ){ if( conchPath[i]=='/' ){ @@ -7092,7 +8072,7 @@ static int proxyCreateConchPathname(char *dbPath, char **pConchPath){ /* Takes a fully configured proxy locking-style unix file and switches -** the local lock file path +** the local lock file path */ static int switchLockProxyPath(unixFile *pFile, const char *path) { proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext; @@ -7101,7 +8081,7 @@ static int switchLockProxyPath(unixFile *pFile, const char *path) { if( pFile->eFileLock!=NO_LOCK ){ return SQLITE_BUSY; - } + } /* nothing to do if the path is NULL, :auto: or matches the existing path */ if( !path || path[0]=='\0' || !strcmp(path, ":auto:") || @@ -7119,7 +8099,7 @@ static int switchLockProxyPath(unixFile *pFile, const char *path) { sqlite3_free(oldPath); pCtx->lockProxyPath = sqlite3DbStrDup(0, path); } - + return rc; } @@ -7133,7 +8113,7 @@ static int switchLockProxyPath(unixFile *pFile, const char *path) { static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){ #if defined(__APPLE__) if( pFile->pMethod == &afpIoMethods ){ - /* afp style keeps a reference to the db path in the filePath field + /* afp style keeps a reference to the db path in the filePath field ** of the struct */ assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN ); strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath, @@ -7154,9 +8134,9 @@ static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){ } /* -** Takes an already filled in unix file and alters it so all file locking +** Takes an already filled in unix file and alters it so all file locking ** will be performed on the local proxy lock file. The following fields -** are preserved in the locking context so that they can be restored and +** are preserved in the locking context so that they can be restored and ** the unix structure properly cleaned up at close time: ** ->lockingContext ** ->pMethod @@ -7166,7 +8146,7 @@ static int proxyTransformUnixFile(unixFile *pFile, const char *path) { char dbPath[MAXPATHLEN+1]; /* Name of the database file */ char *lockPath=NULL; int rc = SQLITE_OK; - + if( pFile->eFileLock!=NO_LOCK ){ return SQLITE_BUSY; } @@ -7176,13 +8156,13 @@ static int proxyTransformUnixFile(unixFile *pFile, const char *path) { }else{ lockPath=(char *)path; } - + OSTRACE(("TRANSPROXY %d for %s pid=%d\n", pFile->h, (lockPath ? lockPath : ":auto:"), osGetpid(0))); pCtx = sqlite3_malloc64( sizeof(*pCtx) ); if( pCtx==0 ){ - return SQLITE_NOMEM; + return SQLITE_NOMEM_BKPT; } memset(pCtx, 0, sizeof(*pCtx)); @@ -7210,7 +8190,7 @@ static int proxyTransformUnixFile(unixFile *pFile, const char *path) { rc = SQLITE_OK; } } - } + } if( rc==SQLITE_OK && lockPath ){ pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath); } @@ -7218,11 +8198,11 @@ static int proxyTransformUnixFile(unixFile *pFile, const char *path) { if( rc==SQLITE_OK ){ pCtx->dbPath = sqlite3DbStrDup(0, dbPath); if( pCtx->dbPath==NULL ){ - rc = SQLITE_NOMEM; + rc = SQLITE_NOMEM_BKPT; } } if( rc==SQLITE_OK ){ - /* all memory is allocated, proxys are created and assigned, + /* all memory is allocated, proxys are created and assigned, ** switch the locking context and pMethod then return. */ pCtx->oldLockingContext = pFile->lockingContext; @@ -7230,12 +8210,12 @@ static int proxyTransformUnixFile(unixFile *pFile, const char *path) { pCtx->pOldMethod = pFile->pMethod; pFile->pMethod = &proxyIoMethods; }else{ - if( pCtx->conchFile ){ + if( pCtx->conchFile ){ pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile); sqlite3_free(pCtx->conchFile); } sqlite3DbFree(0, pCtx->lockProxyPath); - sqlite3_free(pCtx->conchFilePath); + sqlite3_free(pCtx->conchFilePath); sqlite3_free(pCtx); } OSTRACE(("TRANSPROXY %d %s\n", pFile->h, @@ -7273,7 +8253,7 @@ static int proxyFileControl(sqlite3_file *id, int op, void *pArg){ if( isProxyStyle ){ /* turn off proxy locking - not supported. If support is added for ** switching proxy locking mode off then it will need to fail if - ** the journal mode is WAL mode. + ** the journal mode is WAL mode. */ rc = SQLITE_ERROR /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/; }else{ @@ -7283,9 +8263,9 @@ static int proxyFileControl(sqlite3_file *id, int op, void *pArg){ }else{ const char *proxyPath = (const char *)pArg; if( isProxyStyle ){ - proxyLockingContext *pCtx = + proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext; - if( !strcmp(pArg, ":auto:") + if( !strcmp(pArg, ":auto:") || (pCtx->lockProxyPath && !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN)) ){ @@ -7304,7 +8284,7 @@ static int proxyFileControl(sqlite3_file *id, int op, void *pArg){ assert( 0 ); /* The call assures that only valid opcodes are sent */ } } - /*NOTREACHED*/ + /*NOTREACHED*/ assert(0); return SQLITE_ERROR; } @@ -7410,7 +8390,7 @@ static int proxyClose(sqlite3_file *id) { unixFile *lockProxy = pCtx->lockProxy; unixFile *conchFile = pCtx->conchFile; int rc = SQLITE_OK; - + if( lockProxy ){ rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK); if( rc ) return rc; @@ -7447,7 +8427,7 @@ static int proxyClose(sqlite3_file *id) { ** The proxy locking style is intended for use with AFP filesystems. ** And since AFP is only supported on MacOSX, the proxy locking is also ** restricted to MacOSX. -** +** ** ******************* End of the proxy lock implementation ********************** ******************************************************************************/ @@ -7465,8 +8445,8 @@ static int proxyClose(sqlite3_file *id) { ** necessarily been initialized when this routine is called, and so they ** should not be used. */ -int sqlite3_os_init(void){ - /* +int sqlite3_os_init(void){ + /* ** The following macro defines an initializer for an sqlite3_vfs object. ** The name of the VFS is NAME. The pAppData is a pointer to a pointer ** to the "finder" function. (pAppData is a pointer to a pointer because @@ -7482,7 +8462,7 @@ int sqlite3_os_init(void){ ** ** Most finders simply return a pointer to a fixed sqlite3_io_methods ** object. But the "autolockIoFinder" available on MacOSX does a little - ** more than that; it looks at the filesystem type that hosts the + ** more than that; it looks at the filesystem type that hosts the ** database file and tries to choose an locking method appropriate for ** that filesystem time. */ @@ -7548,13 +8528,47 @@ int sqlite3_os_init(void){ /* Double-check that the aSyscall[] array has been constructed ** correctly. See ticket [bb3a86e890c8e96ab] */ - assert( ArraySize(aSyscall)==28 ); + assert( ArraySize(aSyscall)==29 ); /* Register all VFSes defined in the aVfs[] array */ for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){ +#ifdef SQLITE_DEFAULT_UNIX_VFS + sqlite3_vfs_register(&aVfs[i], + 0==strcmp(aVfs[i].zName,SQLITE_DEFAULT_UNIX_VFS)); +#else sqlite3_vfs_register(&aVfs[i], i==0); +#endif } - return SQLITE_OK; +#ifdef SQLITE_OS_KV_OPTIONAL + sqlite3KvvfsInit(); +#endif + unixBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1); +#if OS_VXWORKS + vxworksMutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS2); +#endif + +#ifndef SQLITE_OMIT_WAL + /* Validate lock assumptions */ + assert( SQLITE_SHM_NLOCK==8 ); /* Number of available locks */ + assert( UNIX_SHM_BASE==120 ); /* Start of locking area */ + /* Locks: + ** WRITE UNIX_SHM_BASE 120 + ** CKPT UNIX_SHM_BASE+1 121 + ** RECOVER UNIX_SHM_BASE+2 122 + ** READ-0 UNIX_SHM_BASE+3 123 + ** READ-1 UNIX_SHM_BASE+4 124 + ** READ-2 UNIX_SHM_BASE+5 125 + ** READ-3 UNIX_SHM_BASE+6 126 + ** READ-4 UNIX_SHM_BASE+7 127 + ** DMS UNIX_SHM_BASE+8 128 + */ + assert( UNIX_SHM_DMS==128 ); /* Byte offset of the deadman-switch */ +#endif + + /* Initialize temp file dir array. */ + unixTempFileInit(); + + return SQLITE_OK; } /* @@ -7564,8 +8578,12 @@ int sqlite3_os_init(void){ ** to release dynamically allocated objects. But not on unix. ** This routine is a no-op for unix. */ -int sqlite3_os_end(void){ - return SQLITE_OK; +int sqlite3_os_end(void){ + unixBigLock = 0; +#if OS_VXWORKS + vxworksMutex = 0; +#endif + return SQLITE_OK; } - + #endif /* SQLITE_OS_UNIX */ diff --git a/src/os_win.c b/src/os_win.c index eda6cf59fb..a6b25f2e86 100644 --- a/src/os_win.c +++ b/src/os_win.c @@ -284,10 +284,30 @@ struct winFile { int nFetchOut; /* Number of outstanding xFetch references */ HANDLE hMap; /* Handle for accessing memory mapping */ void *pMapRegion; /* Area memory mapped */ - sqlite3_int64 mmapSize; /* Usable size of mapped region */ - sqlite3_int64 mmapSizeActual; /* Actual size of mapped region */ + sqlite3_int64 mmapSize; /* Size of mapped region */ sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */ #endif +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT + DWORD iBusyTimeout; /* Wait this many millisec on locks */ + int bBlockOnConnect; +#endif +}; + +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT +# define winFileBusyTimeout(pDbFd) pDbFd->iBusyTimeout +#else +# define winFileBusyTimeout(pDbFd) 0 +#endif + +/* +** The winVfsAppData structure is used for the pAppData member for all of the +** Win32 VFS variants. +*/ +typedef struct winVfsAppData winVfsAppData; +struct winVfsAppData { + const sqlite3_io_methods *pMethod; /* The file I/O methods to use. */ + void *pAppData; /* The extra pAppData, if any. */ + BOOL bNoLock; /* Non-zero if locking is disabled. */ }; /* @@ -304,22 +324,6 @@ struct winFile { # define SQLITE_WIN32_DBG_BUF_SIZE ((int)(4096-sizeof(DWORD))) #endif -/* - * The value used with sqlite3_win32_set_directory() to specify that - * the data directory should be changed. - */ -#ifndef SQLITE_WIN32_DATA_DIRECTORY_TYPE -# define SQLITE_WIN32_DATA_DIRECTORY_TYPE (1) -#endif - -/* - * The value used with sqlite3_win32_set_directory() to specify that - * the temporary directory should be changed. - */ -#ifndef SQLITE_WIN32_TEMP_DIRECTORY_TYPE -# define SQLITE_WIN32_TEMP_DIRECTORY_TYPE (2) -#endif - /* * If compiled with SQLITE_WIN32_MALLOC on Windows, we will use the * various Win32 API heap functions instead of our own. @@ -341,22 +345,72 @@ struct winFile { ****************************************************************************** */ #ifndef SQLITE_WIN32_HEAP_CREATE -# define SQLITE_WIN32_HEAP_CREATE (TRUE) +# define SQLITE_WIN32_HEAP_CREATE (TRUE) +#endif + +/* + * This is the maximum possible initial size of the Win32-specific heap, in + * bytes. + */ +#ifndef SQLITE_WIN32_HEAP_MAX_INIT_SIZE +# define SQLITE_WIN32_HEAP_MAX_INIT_SIZE (4294967295U) +#endif + +/* + * This is the extra space for the initial size of the Win32-specific heap, + * in bytes. This value may be zero. + */ +#ifndef SQLITE_WIN32_HEAP_INIT_EXTRA +# define SQLITE_WIN32_HEAP_INIT_EXTRA (4194304) +#endif + +/* + * Calculate the maximum legal cache size, in pages, based on the maximum + * possible initial heap size and the default page size, setting aside the + * needed extra space. + */ +#ifndef SQLITE_WIN32_MAX_CACHE_SIZE +# define SQLITE_WIN32_MAX_CACHE_SIZE (((SQLITE_WIN32_HEAP_MAX_INIT_SIZE) - \ + (SQLITE_WIN32_HEAP_INIT_EXTRA)) / \ + (SQLITE_DEFAULT_PAGE_SIZE)) +#endif + +/* + * This is cache size used in the calculation of the initial size of the + * Win32-specific heap. It cannot be negative. + */ +#ifndef SQLITE_WIN32_CACHE_SIZE +# if SQLITE_DEFAULT_CACHE_SIZE>=0 +# define SQLITE_WIN32_CACHE_SIZE (SQLITE_DEFAULT_CACHE_SIZE) +# else +# define SQLITE_WIN32_CACHE_SIZE (-(SQLITE_DEFAULT_CACHE_SIZE)) +# endif +#endif + +/* + * Make sure that the calculated cache size, in pages, cannot cause the + * initial size of the Win32-specific heap to exceed the maximum amount + * of memory that can be specified in the call to HeapCreate. + */ +#if SQLITE_WIN32_CACHE_SIZE>SQLITE_WIN32_MAX_CACHE_SIZE +# undef SQLITE_WIN32_CACHE_SIZE +# define SQLITE_WIN32_CACHE_SIZE (2000) #endif /* * The initial size of the Win32-specific heap. This value may be zero. */ #ifndef SQLITE_WIN32_HEAP_INIT_SIZE -# define SQLITE_WIN32_HEAP_INIT_SIZE ((SQLITE_DEFAULT_CACHE_SIZE) * \ - (SQLITE_DEFAULT_PAGE_SIZE) + 4194304) +# define SQLITE_WIN32_HEAP_INIT_SIZE ((SQLITE_WIN32_CACHE_SIZE) * \ + (SQLITE_DEFAULT_PAGE_SIZE) + \ + (SQLITE_WIN32_HEAP_INIT_EXTRA)) #endif /* * The maximum size of the Win32-specific heap. This value may be zero. */ #ifndef SQLITE_WIN32_HEAP_MAX_SIZE -# define SQLITE_WIN32_HEAP_MAX_SIZE (0) +# define SQLITE_WIN32_HEAP_MAX_SIZE (0) #endif /* @@ -364,7 +418,7 @@ struct winFile { * zero for the default behavior. */ #ifndef SQLITE_WIN32_HEAP_FLAGS -# define SQLITE_WIN32_HEAP_FLAGS (0) +# define SQLITE_WIN32_HEAP_FLAGS (0) #endif @@ -563,7 +617,7 @@ static struct win_syscall { { "FileTimeToLocalFileTime", (SYSCALL)0, 0 }, #endif -#define osFileTimeToLocalFileTime ((BOOL(WINAPI*)(CONST FILETIME*, \ +#define osFileTimeToLocalFileTime ((BOOL(WINAPI*)(const FILETIME*, \ LPFILETIME))aSyscall[11].pCurrent) #if SQLITE_OS_WINCE @@ -572,7 +626,7 @@ static struct win_syscall { { "FileTimeToSystemTime", (SYSCALL)0, 0 }, #endif -#define osFileTimeToSystemTime ((BOOL(WINAPI*)(CONST FILETIME*, \ +#define osFileTimeToSystemTime ((BOOL(WINAPI*)(const FILETIME*, \ LPSYSTEMTIME))aSyscall[12].pCurrent) { "FlushFileBuffers", (SYSCALL)FlushFileBuffers, 0 }, @@ -678,6 +732,12 @@ static struct win_syscall { #define osGetFullPathNameW ((DWORD(WINAPI*)(LPCWSTR,DWORD,LPWSTR, \ LPWSTR*))aSyscall[25].pCurrent) +/* +** For GetLastError(), MSDN says: +** +** Minimum supported client: Windows XP [desktop apps | UWP apps] +** Minimum supported server: Windows Server 2003 [desktop apps | UWP apps] +*/ { "GetLastError", (SYSCALL)GetLastError, 0 }, #define osGetLastError ((DWORD(WINAPI*)(VOID))aSyscall[26].pCurrent) @@ -846,7 +906,7 @@ static struct win_syscall { { "LockFile", (SYSCALL)0, 0 }, #endif -#ifndef osLockFile +#if !defined(osLockFile) && defined(SQLITE_WIN32_HAS_ANSI) #define osLockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \ DWORD))aSyscall[47].pCurrent) #endif @@ -910,7 +970,7 @@ static struct win_syscall { { "SystemTimeToFileTime", (SYSCALL)SystemTimeToFileTime, 0 }, -#define osSystemTimeToFileTime ((BOOL(WINAPI*)(CONST SYSTEMTIME*, \ +#define osSystemTimeToFileTime ((BOOL(WINAPI*)(const SYSTEMTIME*, \ LPFILETIME))aSyscall[56].pCurrent) #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT @@ -919,7 +979,7 @@ static struct win_syscall { { "UnlockFile", (SYSCALL)0, 0 }, #endif -#ifndef osUnlockFile +#if !defined(osUnlockFile) && defined(SQLITE_WIN32_HAS_ANSI) #define osUnlockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \ DWORD))aSyscall[57].pCurrent) #endif @@ -960,11 +1020,13 @@ static struct win_syscall { #define osCreateEventExW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,LPCWSTR, \ DWORD,DWORD))aSyscall[62].pCurrent) -#if !SQLITE_OS_WINRT +/* +** For WaitForSingleObject(), MSDN says: +** +** Minimum supported client: Windows XP [desktop apps | UWP apps] +** Minimum supported server: Windows Server 2003 [desktop apps | UWP apps] +*/ { "WaitForSingleObject", (SYSCALL)WaitForSingleObject, 0 }, -#else - { "WaitForSingleObject", (SYSCALL)0, 0 }, -#endif #define osWaitForSingleObject ((DWORD(WINAPI*)(HANDLE, \ DWORD))aSyscall[63].pCurrent) @@ -1111,11 +1173,102 @@ static struct win_syscall { #define osFlushViewOfFile \ ((BOOL(WINAPI*)(LPCVOID,SIZE_T))aSyscall[79].pCurrent) +/* +** If SQLITE_ENABLE_SETLK_TIMEOUT is defined, we require CreateEvent() +** to implement blocking locks with timeouts. MSDN says: +** +** Minimum supported client: Windows XP [desktop apps | UWP apps] +** Minimum supported server: Windows Server 2003 [desktop apps | UWP apps] +*/ +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT + { "CreateEvent", (SYSCALL)CreateEvent, 0 }, +#else + { "CreateEvent", (SYSCALL)0, 0 }, +#endif + +#define osCreateEvent ( \ + (HANDLE(WINAPI*) (LPSECURITY_ATTRIBUTES,BOOL,BOOL,LPCSTR)) \ + aSyscall[80].pCurrent \ +) + +/* +** If SQLITE_ENABLE_SETLK_TIMEOUT is defined, we require CancelIo() +** for the case where a timeout expires and a lock request must be +** cancelled. +** +** Minimum supported client: Windows XP [desktop apps | UWP apps] +** Minimum supported server: Windows Server 2003 [desktop apps | UWP apps] +*/ +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT + { "CancelIo", (SYSCALL)CancelIo, 0 }, +#else + { "CancelIo", (SYSCALL)0, 0 }, +#endif + +#define osCancelIo ((BOOL(WINAPI*)(HANDLE))aSyscall[81].pCurrent) + +#if defined(SQLITE_WIN32_HAS_WIDE) && defined(_WIN32) + { "GetModuleHandleW", (SYSCALL)GetModuleHandleW, 0 }, +#else + { "GetModuleHandleW", (SYSCALL)0, 0 }, +#endif + +#define osGetModuleHandleW ((HMODULE(WINAPI*)(LPCWSTR))aSyscall[82].pCurrent) + +#ifndef _WIN32 + { "getenv", (SYSCALL)getenv, 0 }, +#else + { "getenv", (SYSCALL)0, 0 }, +#endif + +#define osGetenv ((const char *(*)(const char *))aSyscall[83].pCurrent) + +#ifndef _WIN32 + { "getcwd", (SYSCALL)getcwd, 0 }, +#else + { "getcwd", (SYSCALL)0, 0 }, +#endif + +#define osGetcwd ((char*(*)(char*,size_t))aSyscall[84].pCurrent) + +#ifndef _WIN32 + { "readlink", (SYSCALL)readlink, 0 }, +#else + { "readlink", (SYSCALL)0, 0 }, +#endif + +#define osReadlink ((ssize_t(*)(const char*,char*,size_t))aSyscall[85].pCurrent) + +#ifndef _WIN32 + { "lstat", (SYSCALL)lstat, 0 }, +#else + { "lstat", (SYSCALL)0, 0 }, +#endif + +#define osLstat ((int(*)(const char*,struct stat*))aSyscall[86].pCurrent) + +#ifndef _WIN32 + { "__errno", (SYSCALL)__errno, 0 }, +#else + { "__errno", (SYSCALL)0, 0 }, +#endif + +#define osErrno (*((int*(*)(void))aSyscall[87].pCurrent)()) + +#ifndef _WIN32 + { "cygwin_conv_path", (SYSCALL)cygwin_conv_path, 0 }, +#else + { "cygwin_conv_path", (SYSCALL)0, 0 }, +#endif + +#define osCygwin_conv_path ((size_t(*)(unsigned int, \ + const void *, void *, size_t))aSyscall[88].pCurrent) + }; /* End of the overrideable system calls */ /* ** This is the xSetSystemCall() method of sqlite3_vfs for all of the -** "win32" VFSes. Return SQLITE_OK opon successfully updating the +** "win32" VFSes. Return SQLITE_OK upon successfully updating the ** system call pointer, or SQLITE_NOTFOUND if there is no configurable ** system call named zName. */ @@ -1222,7 +1375,7 @@ int sqlite3_win32_compact_heap(LPUINT pnLargest){ if( lastErrno==NO_ERROR ){ sqlite3_log(SQLITE_NOMEM, "failed to HeapCompact (no space), heap=%p", (void*)hHeap); - rc = SQLITE_NOMEM; + rc = SQLITE_NOMEM_BKPT; }else{ sqlite3_log(SQLITE_ERROR, "failed to HeapCompact (%lu), heap=%p", osGetLastError(), (void*)hHeap); @@ -1246,17 +1399,17 @@ int sqlite3_win32_compact_heap(LPUINT pnLargest){ */ int sqlite3_win32_reset_heap(){ int rc; - MUTEX_LOGIC( sqlite3_mutex *pMaster; ) /* The main static mutex */ + MUTEX_LOGIC( sqlite3_mutex *pMainMtx; ) /* The main static mutex */ MUTEX_LOGIC( sqlite3_mutex *pMem; ) /* The memsys static mutex */ - MUTEX_LOGIC( pMaster = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER); ) - MUTEX_LOGIC( pMem = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM); ) - sqlite3_mutex_enter(pMaster); + MUTEX_LOGIC( pMainMtx = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); ) + MUTEX_LOGIC( pMem = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM); ) + sqlite3_mutex_enter(pMainMtx); sqlite3_mutex_enter(pMem); winMemAssertMagic(); if( winMemGetHeap()!=NULL && winMemGetOwned() && sqlite3_memory_used()==0 ){ /* ** At this point, there should be no outstanding memory allocations on - ** the heap. Also, since both the master and memsys locks are currently + ** the heap. Also, since both the main and memsys locks are currently ** being held by us, no other function (i.e. from another thread) should ** be able to even access the heap. Attempt to destroy and recreate our ** isolated Win32 native heap now. @@ -1279,11 +1432,12 @@ int sqlite3_win32_reset_heap(){ rc = SQLITE_BUSY; } sqlite3_mutex_leave(pMem); - sqlite3_mutex_leave(pMaster); + sqlite3_mutex_leave(pMainMtx); return rc; } #endif /* SQLITE_WIN32_MALLOC */ +#ifdef _WIN32 /* ** This function outputs the specified (ANSI) string to the Win32 debugger ** (if available). @@ -1294,6 +1448,12 @@ void sqlite3_win32_write_debug(const char *zBuf, int nBuf){ int nMin = MIN(nBuf, (SQLITE_WIN32_DBG_BUF_SIZE - 1)); /* may be negative. */ if( nMin<-1 ) nMin = -1; /* all negative values become -1. */ assert( nMin==-1 || nMin==0 || nMin0 ){ memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE); @@ -1320,6 +1480,7 @@ void sqlite3_win32_write_debug(const char *zBuf, int nBuf){ } #endif } +#endif /* _WIN32 */ /* ** The following routine suspends the current thread for at least ms @@ -1403,7 +1564,9 @@ int sqlite3_win32_is_nt(void){ } return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2; #elif SQLITE_TEST - return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2; + return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2 + || osInterlockedCompareExchange(&sqlite3_os_type, 0, 0)==0 + ; #else /* ** NOTE: All sub-platforms where the GetVersionEx[AW] functions are @@ -1542,7 +1705,7 @@ static int winMemInit(void *pAppData){ "failed to HeapCreate (%lu), flags=%u, initSize=%lu, maxSize=%lu", osGetLastError(), SQLITE_WIN32_HEAP_FLAGS, dwInitialSize, dwMaximumSize); - return SQLITE_NOMEM; + return SQLITE_NOMEM_BKPT; } pWinMemData->bOwned = TRUE; assert( pWinMemData->bOwned ); @@ -1552,7 +1715,7 @@ static int winMemInit(void *pAppData){ if( !pWinMemData->hHeap ){ sqlite3_log(SQLITE_NOMEM, "failed to GetProcessHeap (%lu)", osGetLastError()); - return SQLITE_NOMEM; + return SQLITE_NOMEM_BKPT; } pWinMemData->bOwned = FALSE; assert( !pWinMemData->bOwned ); @@ -1618,163 +1781,269 @@ void sqlite3MemSetDefault(void){ } #endif /* SQLITE_WIN32_MALLOC */ +#ifdef _WIN32 /* -** Convert a UTF-8 string to Microsoft Unicode (UTF-16?). +** Convert a UTF-8 string to Microsoft Unicode. ** -** Space to hold the returned string is obtained from malloc. +** Space to hold the returned string is obtained from sqlite3_malloc(). */ -static LPWSTR winUtf8ToUnicode(const char *zFilename){ +static LPWSTR winUtf8ToUnicode(const char *zText){ int nChar; - LPWSTR zWideFilename; + LPWSTR zWideText; - nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0); + nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, NULL, 0); if( nChar==0 ){ return 0; } - zWideFilename = sqlite3MallocZero( nChar*sizeof(zWideFilename[0]) ); - if( zWideFilename==0 ){ + zWideText = sqlite3MallocZero( nChar*sizeof(WCHAR) ); + if( zWideText==0 ){ return 0; } - nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename, + nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, zWideText, nChar); if( nChar==0 ){ - sqlite3_free(zWideFilename); - zWideFilename = 0; + sqlite3_free(zWideText); + zWideText = 0; } - return zWideFilename; + return zWideText; } +#endif /* _WIN32 */ /* -** Convert Microsoft Unicode to UTF-8. Space to hold the returned string is -** obtained from sqlite3_malloc(). +** Convert a Microsoft Unicode string to UTF-8. +** +** Space to hold the returned string is obtained from sqlite3_malloc(). */ -static char *winUnicodeToUtf8(LPCWSTR zWideFilename){ +static char *winUnicodeToUtf8(LPCWSTR zWideText){ int nByte; - char *zFilename; + char *zText; - nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, 0, 0, 0, 0); + nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, 0, 0, 0, 0); if( nByte == 0 ){ return 0; } - zFilename = sqlite3MallocZero( nByte ); - if( zFilename==0 ){ + zText = sqlite3MallocZero( nByte ); + if( zText==0 ){ return 0; } - nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, zFilename, nByte, + nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, zText, nByte, 0, 0); if( nByte == 0 ){ - sqlite3_free(zFilename); - zFilename = 0; + sqlite3_free(zText); + zText = 0; } - return zFilename; + return zText; } /* -** Convert an ANSI string to Microsoft Unicode, based on the -** current codepage settings for file apis. +** Convert an ANSI string to Microsoft Unicode, using the ANSI or OEM +** code page. ** -** Space to hold the returned string is obtained -** from sqlite3_malloc. +** Space to hold the returned string is obtained from sqlite3_malloc(). */ -static LPWSTR winMbcsToUnicode(const char *zFilename){ - int nByte; - LPWSTR zMbcsFilename; - int codepage = osAreFileApisANSI() ? CP_ACP : CP_OEMCP; +static LPWSTR winMbcsToUnicode(const char *zText, int useAnsi){ + int nWideChar; + LPWSTR zMbcsText; + int codepage = useAnsi ? CP_ACP : CP_OEMCP; - nByte = osMultiByteToWideChar(codepage, 0, zFilename, -1, NULL, - 0)*sizeof(WCHAR); - if( nByte==0 ){ + nWideChar = osMultiByteToWideChar(codepage, 0, zText, -1, NULL, + 0); + if( nWideChar==0 ){ return 0; } - zMbcsFilename = sqlite3MallocZero( nByte*sizeof(zMbcsFilename[0]) ); - if( zMbcsFilename==0 ){ + zMbcsText = sqlite3MallocZero( nWideChar*sizeof(WCHAR) ); + if( zMbcsText==0 ){ return 0; } - nByte = osMultiByteToWideChar(codepage, 0, zFilename, -1, zMbcsFilename, - nByte); - if( nByte==0 ){ - sqlite3_free(zMbcsFilename); - zMbcsFilename = 0; + nWideChar = osMultiByteToWideChar(codepage, 0, zText, -1, zMbcsText, + nWideChar); + if( nWideChar==0 ){ + sqlite3_free(zMbcsText); + zMbcsText = 0; } - return zMbcsFilename; + return zMbcsText; } +#ifdef _WIN32 /* -** Convert Microsoft Unicode to multi-byte character string, based on the -** user's ANSI codepage. +** Convert a Microsoft Unicode string to a multi-byte character string, +** using the ANSI or OEM code page. ** -** Space to hold the returned string is obtained from -** sqlite3_malloc(). +** Space to hold the returned string is obtained from sqlite3_malloc(). */ -static char *winUnicodeToMbcs(LPCWSTR zWideFilename){ +static char *winUnicodeToMbcs(LPCWSTR zWideText, int useAnsi){ int nByte; - char *zFilename; - int codepage = osAreFileApisANSI() ? CP_ACP : CP_OEMCP; + char *zText; + int codepage = useAnsi ? CP_ACP : CP_OEMCP; - nByte = osWideCharToMultiByte(codepage, 0, zWideFilename, -1, 0, 0, 0, 0); + nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, 0, 0, 0, 0); if( nByte == 0 ){ return 0; } - zFilename = sqlite3MallocZero( nByte ); - if( zFilename==0 ){ + zText = sqlite3MallocZero( nByte ); + if( zText==0 ){ return 0; } - nByte = osWideCharToMultiByte(codepage, 0, zWideFilename, -1, zFilename, + nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, zText, nByte, 0, 0); if( nByte == 0 ){ - sqlite3_free(zFilename); - zFilename = 0; + sqlite3_free(zText); + zText = 0; } - return zFilename; + return zText; } +#endif /* _WIN32 */ /* -** Convert multibyte character string to UTF-8. Space to hold the -** returned string is obtained from sqlite3_malloc(). +** Convert a multi-byte character string to UTF-8. +** +** Space to hold the returned string is obtained from sqlite3_malloc(). */ -char *sqlite3_win32_mbcs_to_utf8(const char *zFilename){ - char *zFilenameUtf8; +static char *winMbcsToUtf8(const char *zText, int useAnsi){ + char *zTextUtf8; LPWSTR zTmpWide; - zTmpWide = winMbcsToUnicode(zFilename); + zTmpWide = winMbcsToUnicode(zText, useAnsi); if( zTmpWide==0 ){ return 0; } - zFilenameUtf8 = winUnicodeToUtf8(zTmpWide); + zTextUtf8 = winUnicodeToUtf8(zTmpWide); sqlite3_free(zTmpWide); - return zFilenameUtf8; + return zTextUtf8; } +#ifdef _WIN32 /* -** Convert UTF-8 to multibyte character string. Space to hold the -** returned string is obtained from sqlite3_malloc(). +** Convert a UTF-8 string to a multi-byte character string. +** +** Space to hold the returned string is obtained from sqlite3_malloc(). */ -char *sqlite3_win32_utf8_to_mbcs(const char *zFilename){ - char *zFilenameMbcs; +static char *winUtf8ToMbcs(const char *zText, int useAnsi){ + char *zTextMbcs; LPWSTR zTmpWide; - zTmpWide = winUtf8ToUnicode(zFilename); + zTmpWide = winUtf8ToUnicode(zText); if( zTmpWide==0 ){ return 0; } - zFilenameMbcs = winUnicodeToMbcs(zTmpWide); + zTextMbcs = winUnicodeToMbcs(zTmpWide, useAnsi); sqlite3_free(zTmpWide); - return zFilenameMbcs; + return zTextMbcs; } /* -** This function sets the data directory or the temporary directory based on -** the provided arguments. The type argument must be 1 in order to set the -** data directory or 2 in order to set the temporary directory. The zValue -** argument is the name of the directory to use. The return value will be -** SQLITE_OK if successful. +** This is a public wrapper for the winUtf8ToUnicode() function. +*/ +LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText){ +#ifdef SQLITE_ENABLE_API_ARMOR + if( !zText ){ + (void)SQLITE_MISUSE_BKPT; + return 0; + } +#endif +#ifndef SQLITE_OMIT_AUTOINIT + if( sqlite3_initialize() ) return 0; +#endif + return winUtf8ToUnicode(zText); +} + +/* +** This is a public wrapper for the winUnicodeToUtf8() function. +*/ +char *sqlite3_win32_unicode_to_utf8(LPCWSTR zWideText){ +#ifdef SQLITE_ENABLE_API_ARMOR + if( !zWideText ){ + (void)SQLITE_MISUSE_BKPT; + return 0; + } +#endif +#ifndef SQLITE_OMIT_AUTOINIT + if( sqlite3_initialize() ) return 0; +#endif + return winUnicodeToUtf8(zWideText); +} +#endif /* _WIN32 */ + +/* +** This is a public wrapper for the winMbcsToUtf8() function. +*/ +char *sqlite3_win32_mbcs_to_utf8(const char *zText){ +#ifdef SQLITE_ENABLE_API_ARMOR + if( !zText ){ + (void)SQLITE_MISUSE_BKPT; + return 0; + } +#endif +#ifndef SQLITE_OMIT_AUTOINIT + if( sqlite3_initialize() ) return 0; +#endif + return winMbcsToUtf8(zText, osAreFileApisANSI()); +} + +#ifdef _WIN32 +/* +** This is a public wrapper for the winMbcsToUtf8() function. +*/ +char *sqlite3_win32_mbcs_to_utf8_v2(const char *zText, int useAnsi){ +#ifdef SQLITE_ENABLE_API_ARMOR + if( !zText ){ + (void)SQLITE_MISUSE_BKPT; + return 0; + } +#endif +#ifndef SQLITE_OMIT_AUTOINIT + if( sqlite3_initialize() ) return 0; +#endif + return winMbcsToUtf8(zText, useAnsi); +} + +/* +** This is a public wrapper for the winUtf8ToMbcs() function. +*/ +char *sqlite3_win32_utf8_to_mbcs(const char *zText){ +#ifdef SQLITE_ENABLE_API_ARMOR + if( !zText ){ + (void)SQLITE_MISUSE_BKPT; + return 0; + } +#endif +#ifndef SQLITE_OMIT_AUTOINIT + if( sqlite3_initialize() ) return 0; +#endif + return winUtf8ToMbcs(zText, osAreFileApisANSI()); +} + +/* +** This is a public wrapper for the winUtf8ToMbcs() function. +*/ +char *sqlite3_win32_utf8_to_mbcs_v2(const char *zText, int useAnsi){ +#ifdef SQLITE_ENABLE_API_ARMOR + if( !zText ){ + (void)SQLITE_MISUSE_BKPT; + return 0; + } +#endif +#ifndef SQLITE_OMIT_AUTOINIT + if( sqlite3_initialize() ) return 0; +#endif + return winUtf8ToMbcs(zText, useAnsi); +} + +/* +** This function is the same as sqlite3_win32_set_directory (below); however, +** it accepts a UTF-8 string. */ -int sqlite3_win32_set_directory(DWORD type, LPCWSTR zValue){ +int sqlite3_win32_set_directory8( + unsigned long type, /* Identifier for directory being set or reset */ + const char *zValue /* New value for directory being set or reset */ +){ char **ppDirectory = 0; + int rc; #ifndef SQLITE_OMIT_AUTOINIT - int rc = sqlite3_initialize(); + rc = sqlite3_initialize(); if( rc ) return rc; #endif + sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR)); if( type==SQLITE_WIN32_DATA_DIRECTORY_TYPE ){ ppDirectory = &sqlite3_data_directory; }else if( type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE ){ @@ -1785,19 +2054,58 @@ int sqlite3_win32_set_directory(DWORD type, LPCWSTR zValue){ ); assert( !ppDirectory || sqlite3MemdebugHasType(*ppDirectory, MEMTYPE_HEAP) ); if( ppDirectory ){ - char *zValueUtf8 = 0; + char *zCopy = 0; if( zValue && zValue[0] ){ - zValueUtf8 = winUnicodeToUtf8(zValue); - if ( zValueUtf8==0 ){ - return SQLITE_NOMEM; + zCopy = sqlite3_mprintf("%s", zValue); + if ( zCopy==0 ){ + rc = SQLITE_NOMEM_BKPT; + goto set_directory8_done; } } sqlite3_free(*ppDirectory); - *ppDirectory = zValueUtf8; - return SQLITE_OK; + *ppDirectory = zCopy; + rc = SQLITE_OK; + }else{ + rc = SQLITE_ERROR; + } +set_directory8_done: + sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR)); + return rc; +} + +/* +** This function is the same as sqlite3_win32_set_directory (below); however, +** it accepts a UTF-16 string. +*/ +int sqlite3_win32_set_directory16( + unsigned long type, /* Identifier for directory being set or reset */ + const void *zValue /* New value for directory being set or reset */ +){ + int rc; + char *zUtf8 = 0; + if( zValue ){ + zUtf8 = sqlite3_win32_unicode_to_utf8(zValue); + if( zUtf8==0 ) return SQLITE_NOMEM_BKPT; } - return SQLITE_ERROR; + rc = sqlite3_win32_set_directory8(type, zUtf8); + if( zUtf8 ) sqlite3_free(zUtf8); + return rc; +} + +/* +** This function sets the data directory or the temporary directory based on +** the provided arguments. The type argument must be 1 in order to set the +** data directory or 2 in order to set the temporary directory. The zValue +** argument is the name of the directory to use. The return value will be +** SQLITE_OK if successful. +*/ +int sqlite3_win32_set_directory( + unsigned long type, /* Identifier for directory being set or reset */ + void *zValue /* New value for directory being set or reset */ +){ + return sqlite3_win32_set_directory16(type, zValue); } +#endif /* _WIN32 */ /* ** The return value of winGetLastErrorMsg @@ -1861,7 +2169,7 @@ static int winGetLastErrorMsg(DWORD lastErrno, int nBuf, char *zBuf){ if( dwLen > 0 ){ /* allocate a buffer and convert to UTF8 */ sqlite3BeginBenignMalloc(); - zOut = sqlite3_win32_mbcs_to_utf8(zTemp); + zOut = winMbcsToUtf8(zTemp, osAreFileApisANSI()); sqlite3EndBenignMalloc(); /* free the system buffer allocated by FormatMessage */ osLocalFree(zTemp); @@ -2003,16 +2311,17 @@ static void winLogIoerr(int nRetry, int lineno){ } } -#if SQLITE_OS_WINCE -/************************************************************************* -** This section contains code for WinCE only. +/* +** This #if does not rely on the SQLITE_OS_WINCE define because the +** corresponding section in "date.c" cannot use it. */ -#if !defined(SQLITE_MSVC_LOCALTIME_API) || !SQLITE_MSVC_LOCALTIME_API +#if !defined(SQLITE_OMIT_LOCALTIME) && defined(_WIN32_WCE) && \ + (!defined(SQLITE_MSVC_LOCALTIME_API) || !SQLITE_MSVC_LOCALTIME_API) /* -** The MSVC CRT on Windows CE may not have a localtime() function. So -** create a substitute. +** The MSVC CRT on Windows CE may not have a localtime() function. +** So define a substitute. */ -#include +# include struct tm *__cdecl localtime(const time_t *t) { static struct tm y; @@ -2036,6 +2345,10 @@ struct tm *__cdecl localtime(const time_t *t) } #endif +#if SQLITE_OS_WINCE +/************************************************************************* +** This section contains code for WinCE only. +*/ #define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-(int)offsetof(winFile,h)] /* @@ -2066,7 +2379,7 @@ static int winceCreateLock(const char *zFilename, winFile *pFile){ zName = winUtf8ToUnicode(zFilename); if( zName==0 ){ /* out of memory */ - return SQLITE_IOERR_NOMEM; + return SQLITE_IOERR_NOMEM_BKPT; } /* Initialize the local lockdata */ @@ -2341,13 +2654,100 @@ static BOOL winLockFile( ovlp.Offset = offsetLow; ovlp.OffsetHigh = offsetHigh; return osLockFileEx(*phFile, flags, 0, numBytesLow, numBytesHigh, &ovlp); +#ifdef SQLITE_WIN32_HAS_ANSI }else{ return osLockFile(*phFile, offsetLow, offsetHigh, numBytesLow, numBytesHigh); +#endif } #endif } +#ifndef SQLITE_OMIT_WAL +/* +** Lock a region of nByte bytes starting at offset offset of file hFile. +** Take an EXCLUSIVE lock if parameter bExclusive is true, or a SHARED lock +** otherwise. If nMs is greater than zero and the lock cannot be obtained +** immediately, block for that many ms before giving up. +** +** This function returns SQLITE_OK if the lock is obtained successfully. If +** some other process holds the lock, SQLITE_BUSY is returned if nMs==0, or +** SQLITE_BUSY_TIMEOUT otherwise. Or, if an error occurs, SQLITE_IOERR. +*/ +static int winHandleLockTimeout( + HANDLE hFile, + DWORD offset, + DWORD nByte, + int bExcl, + DWORD nMs +){ + DWORD flags = LOCKFILE_FAIL_IMMEDIATELY | (bExcl?LOCKFILE_EXCLUSIVE_LOCK:0); + int rc = SQLITE_OK; + BOOL ret; + + if( !osIsNT() ){ + ret = winLockFile(&hFile, flags, offset, 0, nByte, 0); + }else{ + OVERLAPPED ovlp; + memset(&ovlp, 0, sizeof(OVERLAPPED)); + ovlp.Offset = offset; + +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT + if( nMs!=0 ){ + flags &= ~LOCKFILE_FAIL_IMMEDIATELY; + } + ovlp.hEvent = osCreateEvent(NULL, TRUE, FALSE, NULL); + if( ovlp.hEvent==NULL ){ + return SQLITE_IOERR_LOCK; + } +#endif + + ret = osLockFileEx(hFile, flags, 0, nByte, 0, &ovlp); + +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT + /* If SQLITE_ENABLE_SETLK_TIMEOUT is defined, then the file-handle was + ** opened with FILE_FLAG_OVERHEAD specified. In this case, the call to + ** LockFileEx() may fail because the request is still pending. This can + ** happen even if LOCKFILE_FAIL_IMMEDIATELY was specified. + ** + ** If nMs is 0, then LOCKFILE_FAIL_IMMEDIATELY was set in the flags + ** passed to LockFileEx(). In this case, if the operation is pending, + ** block indefinitely until it is finished. + ** + ** Otherwise, wait for up to nMs ms for the operation to finish. nMs + ** may be set to INFINITE. + */ + if( !ret && GetLastError()==ERROR_IO_PENDING ){ + DWORD nDelay = (nMs==0 ? INFINITE : nMs); + DWORD res = osWaitForSingleObject(ovlp.hEvent, nDelay); + if( res==WAIT_OBJECT_0 ){ + ret = TRUE; + }else if( res==WAIT_TIMEOUT ){ +#if SQLITE_ENABLE_SETLK_TIMEOUT==1 + rc = SQLITE_BUSY_TIMEOUT; +#else + rc = SQLITE_BUSY; +#endif + }else{ + /* Some other error has occurred */ + rc = SQLITE_IOERR_LOCK; + } + + /* If it is still pending, cancel the LockFileEx() call. */ + osCancelIo(hFile); + } + + osCloseHandle(ovlp.hEvent); +#endif + } + + if( rc==SQLITE_OK && !ret ){ + rc = SQLITE_BUSY; + } + return rc; +} +#endif /* #ifndef SQLITE_OMIT_WAL */ + /* ** Unlock a file region. */ @@ -2372,13 +2772,25 @@ static BOOL winUnlockFile( ovlp.Offset = offsetLow; ovlp.OffsetHigh = offsetHigh; return osUnlockFileEx(*phFile, 0, numBytesLow, numBytesHigh, &ovlp); +#ifdef SQLITE_WIN32_HAS_ANSI }else{ return osUnlockFile(*phFile, offsetLow, offsetHigh, numBytesLow, numBytesHigh); +#endif } #endif } +#ifndef SQLITE_OMIT_WAL +/* +** Remove an nByte lock starting at offset iOff from HANDLE h. +*/ +static int winHandleUnlock(HANDLE h, int iOff, int nByte){ + BOOL ret = winUnlockFile(&h, iOff, 0, nByte, 0); + return (ret ? SQLITE_OK : SQLITE_IOERR_UNLOCK); +} +#endif + /***************************************************************************** ** The next group of routines implement the I/O methods specified ** by the sqlite3_io_methods object. @@ -2392,66 +2804,70 @@ static BOOL winUnlockFile( #endif /* -** Move the current position of the file handle passed as the first -** argument to offset iOffset within the file. If successful, return 0. -** Otherwise, set pFile->lastErrno and return non-zero. +** Seek the file handle h to offset nByte of the file. +** +** If successful, return SQLITE_OK. Or, if an error occurs, return an SQLite +** error code. */ -static int winSeekFile(winFile *pFile, sqlite3_int64 iOffset){ +static int winHandleSeek(HANDLE h, sqlite3_int64 iOffset){ + int rc = SQLITE_OK; /* Return value */ + #if !SQLITE_OS_WINRT LONG upperBits; /* Most sig. 32 bits of new offset */ LONG lowerBits; /* Least sig. 32 bits of new offset */ DWORD dwRet; /* Value returned by SetFilePointer() */ - DWORD lastErrno; /* Value returned by GetLastError() */ - - OSTRACE(("SEEK file=%p, offset=%lld\n", pFile->h, iOffset)); upperBits = (LONG)((iOffset>>32) & 0x7fffffff); lowerBits = (LONG)(iOffset & 0xffffffff); + dwRet = osSetFilePointer(h, lowerBits, &upperBits, FILE_BEGIN); + /* API oddity: If successful, SetFilePointer() returns a dword ** containing the lower 32-bits of the new file-offset. Or, if it fails, ** it returns INVALID_SET_FILE_POINTER. However according to MSDN, ** INVALID_SET_FILE_POINTER may also be a valid new offset. So to determine ** whether an error has actually occurred, it is also necessary to call - ** GetLastError(). - */ - dwRet = osSetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN); - - if( (dwRet==INVALID_SET_FILE_POINTER - && ((lastErrno = osGetLastError())!=NO_ERROR)) ){ - pFile->lastErrno = lastErrno; - winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno, - "winSeekFile", pFile->zPath); - OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h)); - return 1; + ** GetLastError(). */ + if( dwRet==INVALID_SET_FILE_POINTER ){ + DWORD lastErrno = osGetLastError(); + if( lastErrno!=NO_ERROR ){ + rc = SQLITE_IOERR_SEEK; + } } - - OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h)); - return 0; #else - /* - ** Same as above, except that this implementation works for WinRT. - */ - + /* This implementation works for WinRT. */ LARGE_INTEGER x; /* The new offset */ BOOL bRet; /* Value returned by SetFilePointerEx() */ x.QuadPart = iOffset; - bRet = osSetFilePointerEx(pFile->h, x, 0, FILE_BEGIN); + bRet = osSetFilePointerEx(h, x, 0, FILE_BEGIN); if(!bRet){ - pFile->lastErrno = osGetLastError(); - winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno, - "winSeekFile", pFile->zPath); - OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h)); - return 1; + rc = SQLITE_IOERR_SEEK; } +#endif - OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h)); - return 0; -#endif + OSTRACE(("SEEK file=%p, offset=%lld rc=%s\n", h, iOffset, sqlite3ErrName(rc))); + return rc; } +/* +** Move the current position of the file handle passed as the first +** argument to offset iOffset within the file. If successful, return 0. +** Otherwise, set pFile->lastErrno and return non-zero. +*/ +static int winSeekFile(winFile *pFile, sqlite3_int64 iOffset){ + int rc; + + rc = winHandleSeek(pFile->h, iOffset); + if( rc!=SQLITE_OK ){ + pFile->lastErrno = osGetLastError(); + winLogError(rc, pFile->lastErrno, "winSeekFile", pFile->zPath); + } + return rc; +} + + #if SQLITE_MAX_MMAP_SIZE>0 /* Forward references to VFS helper methods used for memory mapped files */ static int winMapfile(winFile*, sqlite3_int64); @@ -2491,7 +2907,12 @@ static int winClose(sqlite3_file *id){ }while( rc==0 && ++cnt < MX_CLOSE_ATTEMPT && (sqlite3_win32_sleep(100), 1) ); #if SQLITE_OS_WINCE #define WINCE_DELETION_ATTEMPTS 3 - winceDestroyLock(pFile); + { + winVfsAppData *pAppData = (winVfsAppData*)pFile->pVfs->pAppData; + if( pAppData==NULL || !pAppData->bNoLock ){ + winceDestroyLock(pFile); + } + } if( pFile->zDeleteOnClose ){ int cnt = 0; while( @@ -2542,7 +2963,7 @@ static int winRead( pFile->h, pBuf, amt, offset, pFile->locktype)); #if SQLITE_MAX_MMAP_SIZE>0 - /* Deal with as much of this read request as possible by transfering + /* Deal with as much of this read request as possible by transferring ** data from the memory mapping using memcpy(). */ if( offsetmmapSize ){ if( offset+amt <= pFile->mmapSize ){ @@ -2620,7 +3041,7 @@ static int winWrite( pFile->h, pBuf, amt, offset, pFile->locktype)); #if defined(SQLITE_MMAP_READWRITE) && SQLITE_MAX_MMAP_SIZE>0 - /* Deal with as much of this write request as possible by transfering + /* Deal with as much of this write request as possible by transferring ** data from the memory mapping using memcpy(). */ if( offsetmmapSize ){ if( offset+amt <= pFile->mmapSize ){ @@ -2706,6 +3127,62 @@ static int winWrite( return SQLITE_OK; } +#ifndef SQLITE_OMIT_WAL +/* +** Truncate the file opened by handle h to nByte bytes in size. +*/ +static int winHandleTruncate(HANDLE h, sqlite3_int64 nByte){ + int rc = SQLITE_OK; /* Return code */ + rc = winHandleSeek(h, nByte); + if( rc==SQLITE_OK ){ + if( 0==osSetEndOfFile(h) ){ + rc = SQLITE_IOERR_TRUNCATE; + } + } + return rc; +} + +/* +** Determine the size in bytes of the file opened by the handle passed as +** the first argument. +*/ +static int winHandleSize(HANDLE h, sqlite3_int64 *pnByte){ + int rc = SQLITE_OK; + +#if SQLITE_OS_WINRT + FILE_STANDARD_INFO info; + BOOL b; + b = osGetFileInformationByHandleEx(h, FileStandardInfo, &info, sizeof(info)); + if( b ){ + *pnByte = info.EndOfFile.QuadPart; + }else{ + rc = SQLITE_IOERR_FSTAT; + } +#else + DWORD upperBits = 0; + DWORD lowerBits = 0; + + assert( pnByte ); + lowerBits = osGetFileSize(h, &upperBits); + *pnByte = (((sqlite3_int64)upperBits)<<32) + lowerBits; + if( lowerBits==INVALID_FILE_SIZE && osGetLastError()!=NO_ERROR ){ + rc = SQLITE_IOERR_FSTAT; + } +#endif + + return rc; +} + +/* +** Close the handle passed as the only argument. +*/ +static void winHandleClose(HANDLE h){ + if( h!=INVALID_HANDLE_VALUE ){ + osCloseHandle(h); + } +} +#endif /* #ifndef SQLITE_OMIT_WAL */ + /* ** Truncate an open file to a specified size */ @@ -2713,6 +3190,29 @@ static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){ winFile *pFile = (winFile*)id; /* File handle object */ int rc = SQLITE_OK; /* Return code for this function */ DWORD lastErrno; +#if SQLITE_MAX_MMAP_SIZE>0 + sqlite3_int64 oldMmapSize; + if( pFile->nFetchOut>0 ){ + /* File truncation is a no-op if there are outstanding memory mapped + ** pages. This is because truncating the file means temporarily unmapping + ** the file, and that might delete memory out from under existing cursors. + ** + ** This can result in incremental vacuum not truncating the file, + ** if there is an active read cursor when the incremental vacuum occurs. + ** No real harm comes of this - the database file is not corrupted, + ** though some folks might complain that the file is bigger than it + ** needs to be. + ** + ** The only feasible work-around is to defer the truncation until after + ** all references to memory-mapped content are closed. That is doable, + ** but involves adding a few branches in the common write code path which + ** could slow down normal operations slightly. Hence, we have decided for + ** now to simply make transactions a no-op if there are pending reads. We + ** can maybe revisit this decision in the future. + */ + return SQLITE_OK; + } +#endif assert( pFile ); SimulateIOError(return SQLITE_IOERR_TRUNCATE); @@ -2728,6 +3228,15 @@ static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){ nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk; } +#if SQLITE_MAX_MMAP_SIZE>0 + if( pFile->pMapRegion ){ + oldMmapSize = pFile->mmapSize; + }else{ + oldMmapSize = 0; + } + winUnmapfile(pFile); +#endif + /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */ if( winSeekFile(pFile, nByte) ){ rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno, @@ -2740,12 +3249,12 @@ static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){ } #if SQLITE_MAX_MMAP_SIZE>0 - /* If the file was truncated to a size smaller than the currently - ** mapped region, reduce the effective mapping size as well. SQLite will - ** use read() and write() to access data beyond this point from now on. - */ - if( pFile->pMapRegion && nBytemmapSize ){ - pFile->mmapSize = nByte; + if( rc==SQLITE_OK && oldMmapSize>0 ){ + if( oldMmapSize>nByte ){ + winMapfile(pFile, -1); + }else{ + winMapfile(pFile, oldMmapSize); + } } #endif @@ -2757,7 +3266,7 @@ static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){ #ifdef SQLITE_TEST /* ** Count the number of fullsyncs and normal syncs. This is used to test -** that syncs and fullsyncs are occuring at the right times. +** that syncs and fullsyncs are occurring at the right times. */ int sqlite3_sync_count = 0; int sqlite3_fullsync_count = 0; @@ -2929,8 +3438,9 @@ static int winFileSize(sqlite3_file *id, sqlite3_int64 *pSize){ ** Different API routines are called depending on whether or not this ** is Win9x or WinNT. */ -static int winGetReadLock(winFile *pFile){ +static int winGetReadLock(winFile *pFile, int bBlock){ int res; + DWORD mask = ~(bBlock ? LOCKFILE_FAIL_IMMEDIATELY : 0); OSTRACE(("READ-LOCK file=%p, lock=%d\n", pFile->h, pFile->locktype)); if( osIsNT() ){ #if SQLITE_OS_WINCE @@ -2940,7 +3450,7 @@ static int winGetReadLock(winFile *pFile){ */ res = winceLockFile(&pFile->h, SHARED_FIRST, 0, 1, 0); #else - res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS, SHARED_FIRST, 0, + res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS&mask, SHARED_FIRST, 0, SHARED_SIZE, 0); #endif } @@ -2949,7 +3459,7 @@ static int winGetReadLock(winFile *pFile){ int lk; sqlite3_randomness(sizeof(lk), &lk); pFile->sharedLockByte = (short)((lk & 0x7fffffff)%(SHARED_SIZE - 1)); - res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, + res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS&mask, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0); } #endif @@ -3044,47 +3554,62 @@ static int winLock(sqlite3_file *id, int locktype){ assert( locktype!=PENDING_LOCK ); assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK ); - /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or + /* Lock the PENDING_LOCK byte if we need to acquire an EXCLUSIVE lock or ** a SHARED lock. If we are acquiring a SHARED lock, the acquisition of ** the PENDING_LOCK byte is temporary. */ newLocktype = pFile->locktype; - if( (pFile->locktype==NO_LOCK) - || ( (locktype==EXCLUSIVE_LOCK) - && (pFile->locktype==RESERVED_LOCK)) + if( locktype==SHARED_LOCK + || (locktype==EXCLUSIVE_LOCK && pFile->locktype==RESERVED_LOCK) ){ int cnt = 3; - while( cnt-->0 && (res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, - PENDING_BYTE, 0, 1, 0))==0 ){ + + /* Flags for the LockFileEx() call. This should be an exclusive lock if + ** this call is to obtain EXCLUSIVE, or a shared lock if this call is to + ** obtain SHARED. */ + int flags = LOCKFILE_FAIL_IMMEDIATELY; + if( locktype==EXCLUSIVE_LOCK ){ + flags |= LOCKFILE_EXCLUSIVE_LOCK; + } + while( cnt>0 ){ /* Try 3 times to get the pending lock. This is needed to work ** around problems caused by indexing and/or anti-virus software on ** Windows systems. + ** ** If you are using this code as a model for alternative VFSes, do not - ** copy this retry logic. It is a hack intended for Windows only. - */ + ** copy this retry logic. It is a hack intended for Windows only. */ + res = winLockFile(&pFile->h, flags, PENDING_BYTE, 0, 1, 0); + if( res ) break; + lastErrno = osGetLastError(); - OSTRACE(("LOCK-PENDING-FAIL file=%p, count=%d, result=%d\n", - pFile->h, cnt, res)); + OSTRACE(("LOCK-PENDING-FAIL file=%p, count=%d, result=%d\n", + pFile->h, cnt, res + )); + if( lastErrno==ERROR_INVALID_HANDLE ){ pFile->lastErrno = lastErrno; rc = SQLITE_IOERR_LOCK; - OSTRACE(("LOCK-FAIL file=%p, count=%d, rc=%s\n", - pFile->h, cnt, sqlite3ErrName(rc))); + OSTRACE(("LOCK-FAIL file=%p, count=%d, rc=%s\n", + pFile->h, cnt, sqlite3ErrName(rc) + )); return rc; } - if( cnt ) sqlite3_win32_sleep(1); + + cnt--; + if( cnt>0 ) sqlite3_win32_sleep(1); } gotPendingLock = res; - if( !res ){ - lastErrno = osGetLastError(); - } } /* Acquire a shared lock */ if( locktype==SHARED_LOCK && res ){ assert( pFile->locktype==NO_LOCK ); - res = winGetReadLock(pFile); +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT + res = winGetReadLock(pFile, pFile->bBlockOnConnect); +#else + res = winGetReadLock(pFile, 0); +#endif if( res ){ newLocktype = SHARED_LOCK; }else{ @@ -3115,14 +3640,14 @@ static int winLock(sqlite3_file *id, int locktype){ */ if( locktype==EXCLUSIVE_LOCK && res ){ assert( pFile->locktype>=SHARED_LOCK ); - res = winUnlockReadLock(pFile); + (void)winUnlockReadLock(pFile); res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, SHARED_FIRST, 0, SHARED_SIZE, 0); if( res ){ newLocktype = EXCLUSIVE_LOCK; }else{ lastErrno = osGetLastError(); - winGetReadLock(pFile); + winGetReadLock(pFile, 0); } } @@ -3202,7 +3727,7 @@ static int winUnlock(sqlite3_file *id, int locktype){ type = pFile->locktype; if( type>=EXCLUSIVE_LOCK ){ winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0); - if( locktype==SHARED_LOCK && !winGetReadLock(pFile) ){ + if( locktype==SHARED_LOCK && !winGetReadLock(pFile, 0) ){ /* This should never happen. We should always be able to ** reacquire the read lock */ rc = winLogError(SQLITE_IOERR_UNLOCK, osGetLastError(), @@ -3224,6 +3749,44 @@ static int winUnlock(sqlite3_file *id, int locktype){ return rc; } +/****************************************************************************** +****************************** No-op Locking ********************************** +** +** Of the various locking implementations available, this is by far the +** simplest: locking is ignored. No attempt is made to lock the database +** file for reading or writing. +** +** This locking mode is appropriate for use on read-only databases +** (ex: databases that are burned into CD-ROM, for example.) It can +** also be used if the application employs some external mechanism to +** prevent simultaneous access of the same database by two or more +** database connections. But there is a serious risk of database +** corruption if this locking mode is used in situations where multiple +** database connections are accessing the same database file at the same +** time and one or more of those connections are writing. +*/ + +static int winNolockLock(sqlite3_file *id, int locktype){ + UNUSED_PARAMETER(id); + UNUSED_PARAMETER(locktype); + return SQLITE_OK; +} + +static int winNolockCheckReservedLock(sqlite3_file *id, int *pResOut){ + UNUSED_PARAMETER(id); + UNUSED_PARAMETER(pResOut); + return SQLITE_OK; +} + +static int winNolockUnlock(sqlite3_file *id, int locktype){ + UNUSED_PARAMETER(id); + UNUSED_PARAMETER(locktype); + return SQLITE_OK; +} + +/******************* End of the no-op lock implementation ********************* +******************************************************************************/ + /* ** If *pArg is initially negative then this is a query. Set *pArg to ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set. @@ -3243,6 +3806,7 @@ static void winModeBit(winFile *pFile, unsigned char mask, int *pArg){ /* Forward references to VFS helper methods used for temporary files */ static int winGetTempname(sqlite3_vfs *, char **); static int winIsDir(const void *); +static BOOL winIsLongPathPrefix(const char *); static BOOL winIsDriveLetterAndColon(const char *); /* @@ -3257,7 +3821,7 @@ static int winFileControl(sqlite3_file *id, int op, void *pArg){ OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h)); return SQLITE_OK; } - case SQLITE_LAST_ERRNO: { + case SQLITE_FCNTL_LAST_ERRNO: { *(int*)pArg = (int)pFile->lastErrno; OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h)); return SQLITE_OK; @@ -3315,6 +3879,12 @@ static int winFileControl(sqlite3_file *id, int op, void *pArg){ OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h)); return SQLITE_OK; } + case SQLITE_FCNTL_WIN32_GET_HANDLE: { + LPHANDLE phFile = (LPHANDLE)pArg; + *phFile = pFile->h; + OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h)); + return SQLITE_OK; + } #ifdef SQLITE_TEST case SQLITE_FCNTL_WIN32_SET_HANDLE: { LPHANDLE phFile = (LPHANDLE)pArg; @@ -3326,6 +3896,11 @@ static int winFileControl(sqlite3_file *id, int op, void *pArg){ return SQLITE_OK; } #endif + case SQLITE_FCNTL_NULL_IO: { + (void)osCloseHandle(pFile->h); + pFile->h = NULL; + return SQLITE_OK; + } case SQLITE_FCNTL_TEMPFILENAME: { char *zTFile = 0; int rc = winGetTempname(pFile->pVfs, &zTFile); @@ -3342,6 +3917,14 @@ static int winFileControl(sqlite3_file *id, int op, void *pArg){ if( newLimit>sqlite3GlobalConfig.mxMmap ){ newLimit = sqlite3GlobalConfig.mxMmap; } + + /* The value of newLimit may be eventually cast to (SIZE_T) and passed + ** to MapViewOfFile(). Restrict its value to 2GB if (SIZE_T) is not at + ** least a 64-bit type. */ + if( newLimit>0 && sizeof(SIZE_T)<8 ){ + newLimit = (newLimit & 0x7FFFFFFF); + } + *(i64*)pArg = pFile->mmapSizeMax; if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){ pFile->mmapSizeMax = newLimit; @@ -3354,6 +3937,50 @@ static int winFileControl(sqlite3_file *id, int op, void *pArg){ return rc; } #endif + +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT + case SQLITE_FCNTL_LOCK_TIMEOUT: { + int iOld = pFile->iBusyTimeout; + int iNew = *(int*)pArg; +#if SQLITE_ENABLE_SETLK_TIMEOUT==1 + pFile->iBusyTimeout = (iNew < 0) ? INFINITE : (DWORD)iNew; +#elif SQLITE_ENABLE_SETLK_TIMEOUT==2 + pFile->iBusyTimeout = (DWORD)(!!iNew); +#else +# error "SQLITE_ENABLE_SETLK_TIMEOUT must be set to 1 or 2" +#endif + *(int*)pArg = iOld; + return SQLITE_OK; + } + case SQLITE_FCNTL_BLOCK_ON_CONNECT: { + int iNew = *(int*)pArg; + pFile->bBlockOnConnect = iNew; + return SQLITE_OK; + } +#endif /* SQLITE_ENABLE_SETLK_TIMEOUT */ + +#if defined(SQLITE_DEBUG) || defined(SQLITE_ENABLE_FILESTAT) + case SQLITE_FCNTL_FILESTAT: { + sqlite3_str *pStr = (sqlite3_str*)pArg; + sqlite3_str_appendf(pStr, "{\"h\":%llu", (sqlite3_uint64)pFile->h); + sqlite3_str_appendf(pStr, ",\"vfs\":\"%s\"", pFile->pVfs->zName); + if( pFile->locktype ){ + static const char *azLock[] = { "SHARED", "RESERVED", + "PENDING", "EXCLUSIVE" }; + sqlite3_str_appendf(pStr, ",\"locktype\":\"%s\"", + azLock[pFile->locktype-1]); + } +#if SQLITE_MAX_MMAP_SIZE>0 + if( pFile->mmapSize ){ + sqlite3_str_appendf(pStr, ",\"mmapSize\":%lld", pFile->mmapSize); + sqlite3_str_appendf(pStr, ",\"nFetchOut\":%d", pFile->nFetchOut); + } +#endif + sqlite3_str_append(pStr, "}", 1); + return SQLITE_OK; + } +#endif /* SQLITE_DEBUG || SQLITE_ENABLE_FILESTAT */ + } OSTRACE(("FCNTL file=%p, rc=SQLITE_NOTFOUND\n", pFile->h)); return SQLITE_NOTFOUND; @@ -3379,7 +4006,7 @@ static int winSectorSize(sqlite3_file *id){ */ static int winDeviceCharacteristics(sqlite3_file *id){ winFile *p = (winFile*)id; - return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN | + return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN | SQLITE_IOCAP_SUBPAGE_READ | ((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE:0); } @@ -3391,6 +4018,103 @@ static int winDeviceCharacteristics(sqlite3_file *id){ */ static SYSTEM_INFO winSysInfo; +/* +** Convert a UTF-8 filename into whatever form the underlying +** operating system wants filenames in. Space to hold the result +** is obtained from malloc and must be freed by the calling +** function +** +** On Cygwin, 3 possible input forms are accepted: +** - If the filename starts with ":/" or ":\", +** it is converted to UTF-16 as-is. +** - If the filename contains '/', it is assumed to be a +** Cygwin absolute path, it is converted to a win32 +** absolute path in UTF-16. +** - Otherwise it must be a filename only, the win32 filename +** is returned in UTF-16. +** Note: If the function cygwin_conv_path() fails, only +** UTF-8 -> UTF-16 conversion will be done. This can only +** happen when the file path >32k, in which case winUtf8ToUnicode() +** will fail too. +*/ +static void *winConvertFromUtf8Filename(const char *zFilename){ + void *zConverted = 0; + if( osIsNT() ){ +#ifdef __CYGWIN__ + int nChar; + LPWSTR zWideFilename; + + if( osCygwin_conv_path && !(winIsDriveLetterAndColon(zFilename) + && winIsDirSep(zFilename[2])) ){ + i64 nByte; + int convertflag = CCP_POSIX_TO_WIN_W; + if( !strchr(zFilename, '/') ) convertflag |= CCP_RELATIVE; + nByte = (i64)osCygwin_conv_path(convertflag, + zFilename, 0, 0); + if( nByte>0 ){ + zConverted = sqlite3MallocZero(12+(u64)nByte); + if ( zConverted==0 ){ + return zConverted; + } + zWideFilename = zConverted; + /* Filenames should be prefixed, except when converted + * full path already starts with "\\?\". */ + if( osCygwin_conv_path(convertflag, zFilename, + zWideFilename+4, nByte)==0 ){ + if( (convertflag&CCP_RELATIVE) ){ + memmove(zWideFilename, zWideFilename+4, nByte); + }else if( memcmp(zWideFilename+4, L"\\\\", 4) ){ + memcpy(zWideFilename, L"\\\\?\\", 8); + }else if( zWideFilename[6]!='?' ){ + memmove(zWideFilename+6, zWideFilename+4, nByte); + memcpy(zWideFilename, L"\\\\?\\UNC", 14); + }else{ + memmove(zWideFilename, zWideFilename+4, nByte); + } + return zConverted; + } + sqlite3_free(zConverted); + } + } + nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0); + if( nChar==0 ){ + return 0; + } + zWideFilename = sqlite3MallocZero( nChar*sizeof(WCHAR)+12 ); + if( zWideFilename==0 ){ + return 0; + } + nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, + zWideFilename, nChar); + if( nChar==0 ){ + sqlite3_free(zWideFilename); + zWideFilename = 0; + }else if( nChar>MAX_PATH + && winIsDriveLetterAndColon(zFilename) + && winIsDirSep(zFilename[2]) ){ + memmove(zWideFilename+4, zWideFilename, nChar*sizeof(WCHAR)); + zWideFilename[2] = '\\'; + memcpy(zWideFilename, L"\\\\?\\", 8); + }else if( nChar>MAX_PATH + && winIsDirSep(zFilename[0]) && winIsDirSep(zFilename[1]) + && zFilename[2] != '?' ){ + memmove(zWideFilename+6, zWideFilename, nChar*sizeof(WCHAR)); + memcpy(zWideFilename, L"\\\\?\\UNC", 14); + } + zConverted = zWideFilename; +#else + zConverted = winUtf8ToUnicode(zFilename); +#endif /* __CYGWIN__ */ + } +#if defined(SQLITE_WIN32_HAS_ANSI) && defined(_WIN32) + else{ + zConverted = winUtf8ToMbcs(zFilename, osAreFileApisANSI()); + } +#endif + /* caller will handle out of memory */ + return zConverted; +} + #ifndef SQLITE_OMIT_WAL /* @@ -3406,15 +4130,16 @@ static SYSTEM_INFO winSysInfo; ** assert( winShmMutexHeld() ); ** winShmLeaveMutex() */ +static sqlite3_mutex *winBigLock = 0; static void winShmEnterMutex(void){ - sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1)); + sqlite3_mutex_enter(winBigLock); } static void winShmLeaveMutex(void){ - sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1)); + sqlite3_mutex_leave(winBigLock); } #ifndef NDEBUG static int winShmMutexHeld(void) { - return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1)); + return sqlite3_mutex_held(winBigLock); } #endif @@ -3426,36 +4151,49 @@ static int winShmMutexHeld(void) { ** log-summary is opened only once per process. ** ** winShmMutexHeld() must be true when creating or destroying -** this object or while reading or writing the following fields: +** this object, or while editing the global linked list that starts +** at winShmNodeList. ** -** nRef -** pNext +** When reading or writing the linked list starting at winShmNode.pWinShmList, +** pShmNode->mutex must be held. ** -** The following fields are read-only after the object is created: +** The following fields are constant after the object is created: ** -** fid ** zFilename +** hSharedShm +** mutex +** bUseSharedLockHandle ** -** Either winShmNode.mutex must be held or winShmNode.nRef==0 and +** Either winShmNode.mutex must be held or winShmNode.pWinShmList==0 and ** winShmMutexHeld() is true when reading or writing any other field ** in this structure. ** +** File-handle hSharedShm is always used to (a) take the DMS lock, (b) +** truncate the *-shm file if the DMS-locking protocol demands it, and +** (c) map regions of the *-shm file into memory using MapViewOfFile() +** or similar. If bUseSharedLockHandle is true, then other locks are also +** taken on hSharedShm. Or, if bUseSharedLockHandle is false, then other +** locks are taken using each connection's winShm.hShm handles. */ struct winShmNode { sqlite3_mutex *mutex; /* Mutex to access this object */ char *zFilename; /* Name of the file */ - winFile hFile; /* File handle from winOpen */ + HANDLE hSharedShm; /* File handle open on zFilename */ + int bUseSharedLockHandle; /* True to use hSharedShm for everything */ + int isUnlocked; /* DMS lock has not yet been obtained */ + int isReadonly; /* True if read-only */ int szRegion; /* Size of shared-memory regions */ int nRegion; /* Size of array apRegion */ + struct ShmRegion { HANDLE hMap; /* File handle from CreateFileMapping */ void *pMap; } *aRegion; DWORD lastErrno; /* The Windows errno from the last I/O error */ - int nRef; /* Number of winShm objects pointing to this */ - winShm *pFirst; /* All winShm objects pointing to this */ + winShm *pWinShmList; /* List of winShm objects with ptrs to this */ + winShmNode *pNext; /* Next in list of all winShmNode objects */ #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE) u8 nextShmId; /* Next available winShm.id value */ @@ -3471,26 +4209,19 @@ static winShmNode *winShmNodeList = 0; /* ** Structure used internally by this VFS to record the state of an -** open shared memory connection. -** -** The following fields are initialized when this object is created and -** are read-only thereafter: -** -** winShm.pShmNode -** winShm.id -** -** All other fields are read/write. The winShm.pShmNode->mutex must be held -** while accessing any read/write fields. +** open shared memory connection. There is one such structure for each +** winFile open on a wal mode database. */ struct winShm { winShmNode *pShmNode; /* The underlying winShmNode object */ - winShm *pNext; /* Next winShm with the same winShmNode */ - u8 hasMutex; /* True if holding the winShmNode mutex */ u16 sharedMask; /* Mask of shared locks held */ u16 exclMask; /* Mask of exclusive locks held */ + HANDLE hShm; /* File-handle on *-shm file. For locking. */ + int bReadonly; /* True if hShm is opened read-only */ #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE) u8 id; /* Id of this connection with its winShmNode */ #endif + winShm *pWinShmNext; /* Next winShm object on same winShmNode */ }; /* @@ -3499,56 +4230,12 @@ struct winShm { #define WIN_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */ #define WIN_SHM_DMS (WIN_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */ -/* -** Apply advisory locks for all n bytes beginning at ofst. -*/ -#define _SHM_UNLCK 1 -#define _SHM_RDLCK 2 -#define _SHM_WRLCK 3 -static int winShmSystemLock( - winShmNode *pFile, /* Apply locks to this open shared-memory segment */ - int lockType, /* _SHM_UNLCK, _SHM_RDLCK, or _SHM_WRLCK */ - int ofst, /* Offset to first byte to be locked/unlocked */ - int nByte /* Number of bytes to lock or unlock */ -){ - int rc = 0; /* Result code form Lock/UnlockFileEx() */ - - /* Access to the winShmNode object is serialized by the caller */ - assert( sqlite3_mutex_held(pFile->mutex) || pFile->nRef==0 ); - - OSTRACE(("SHM-LOCK file=%p, lock=%d, offset=%d, size=%d\n", - pFile->hFile.h, lockType, ofst, nByte)); - - /* Release/Acquire the system-level lock */ - if( lockType==_SHM_UNLCK ){ - rc = winUnlockFile(&pFile->hFile.h, ofst, 0, nByte, 0); - }else{ - /* Initialize the locking parameters */ - DWORD dwFlags = LOCKFILE_FAIL_IMMEDIATELY; - if( lockType == _SHM_WRLCK ) dwFlags |= LOCKFILE_EXCLUSIVE_LOCK; - rc = winLockFile(&pFile->hFile.h, dwFlags, ofst, 0, nByte, 0); - } - - if( rc!= 0 ){ - rc = SQLITE_OK; - }else{ - pFile->lastErrno = osGetLastError(); - rc = SQLITE_BUSY; - } - - OSTRACE(("SHM-LOCK file=%p, func=%s, errno=%lu, rc=%s\n", - pFile->hFile.h, (lockType == _SHM_UNLCK) ? "winUnlockFile" : - "winLockFile", pFile->lastErrno, sqlite3ErrName(rc))); - - return rc; -} - /* Forward references to VFS methods */ static int winOpen(sqlite3_vfs*,const char*,sqlite3_file*,int,int*); static int winDelete(sqlite3_vfs *,const char*,int); /* -** Purge the winShmNodeList list of all entries with winShmNode.nRef==0. +** Purge the winShmNodeList list of all entries with winShmNode.pWinShmList==0. ** ** This is not a VFS shared-memory method; it is a utility function called ** by VFS shared-memory methods. @@ -3561,7 +4248,7 @@ static void winShmPurge(sqlite3_vfs *pVfs, int deleteFlag){ osGetCurrentProcessId(), deleteFlag)); pp = &winShmNodeList; while( (p = *pp)!=0 ){ - if( p->nRef==0 ){ + if( p->pWinShmList==0 ){ int i; if( p->mutex ){ sqlite3_mutex_free(p->mutex); } for(i=0; inRegion; i++){ @@ -3574,11 +4261,7 @@ static void winShmPurge(sqlite3_vfs *pVfs, int deleteFlag){ osGetCurrentProcessId(), i, bRc ? "ok" : "failed")); UNUSED_VARIABLE_VALUE(bRc); } - if( p->hFile.h!=NULL && p->hFile.h!=INVALID_HANDLE_VALUE ){ - SimulateIOErrorBenign(1); - winClose((sqlite3_file *)&p->hFile); - SimulateIOErrorBenign(0); - } + winHandleClose(p->hSharedShm); if( deleteFlag ){ SimulateIOErrorBenign(1); sqlite3BeginBenignMalloc(); @@ -3596,116 +4279,294 @@ static void winShmPurge(sqlite3_vfs *pVfs, int deleteFlag){ } /* -** Open the shared-memory area associated with database file pDbFd. +** The DMS lock has not yet been taken on the shm file associated with +** pShmNode. Take the lock. Truncate the *-shm file if required. +** Return SQLITE_OK if successful, or an SQLite error code otherwise. +*/ +static int winLockSharedMemory(winShmNode *pShmNode, DWORD nMs){ + HANDLE h = pShmNode->hSharedShm; + int rc = SQLITE_OK; + + assert( sqlite3_mutex_held(pShmNode->mutex) ); + rc = winHandleLockTimeout(h, WIN_SHM_DMS, 1, 1, 0); + if( rc==SQLITE_OK ){ + /* We have an EXCLUSIVE lock on the DMS byte. This means that this + ** is the first process to open the file. Truncate it to zero bytes + ** in this case. */ + if( pShmNode->isReadonly ){ + rc = SQLITE_READONLY_CANTINIT; + }else{ + rc = winHandleTruncate(h, 0); + } + + /* Release the EXCLUSIVE lock acquired above. */ + winUnlockFile(&h, WIN_SHM_DMS, 0, 1, 0); + }else if( (rc & 0xFF)==SQLITE_BUSY ){ + rc = SQLITE_OK; + } + + if( rc==SQLITE_OK ){ + /* Take a SHARED lock on the DMS byte. */ + rc = winHandleLockTimeout(h, WIN_SHM_DMS, 1, 0, nMs); + if( rc==SQLITE_OK ){ + pShmNode->isUnlocked = 0; + } + } + + return rc; +} + + +/* +** This function is used to open a handle on a *-shm file. ** -** When opening a new shared-memory file, if no other instances of that -** file are currently open, in this process or in other processes, then -** the file must be truncated to zero length or have its header cleared. +** If SQLITE_ENABLE_SETLK_TIMEOUT is defined at build time, then the file +** is opened with FILE_FLAG_OVERLAPPED specified. If not, it is not. +*/ +static int winHandleOpen( + const char *zUtf8, /* File to open */ + int *pbReadonly, /* IN/OUT: True for readonly handle */ + HANDLE *ph /* OUT: New HANDLE for file */ +){ + int rc = SQLITE_OK; + void *zConverted = 0; + int bReadonly = *pbReadonly; + HANDLE h = INVALID_HANDLE_VALUE; + +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT + const DWORD flag_overlapped = FILE_FLAG_OVERLAPPED; +#else + const DWORD flag_overlapped = 0; +#endif + + /* Convert the filename to the system encoding. */ + zConverted = winConvertFromUtf8Filename(zUtf8); + if( zConverted==0 ){ + OSTRACE(("OPEN name=%s, rc=SQLITE_IOERR_NOMEM", zUtf8)); + rc = SQLITE_IOERR_NOMEM_BKPT; + goto winopenfile_out; + } + + /* Ensure the file we are trying to open is not actually a directory. */ + if( winIsDir(zConverted) ){ + OSTRACE(("OPEN name=%s, rc=SQLITE_CANTOPEN_ISDIR", zUtf8)); + rc = SQLITE_CANTOPEN_ISDIR; + goto winopenfile_out; + } + + /* TODO: platforms. + ** TODO: retry-on-ioerr. + */ + if( osIsNT() ){ +#if SQLITE_OS_WINRT + CREATEFILE2_EXTENDED_PARAMETERS extendedParameters; + memset(&extendedParameters, 0, sizeof(extendedParameters)); + extendedParameters.dwSize = sizeof(extendedParameters); + extendedParameters.dwFileAttributes = FILE_ATTRIBUTE_NORMAL; + extendedParameters.dwFileFlags = flag_overlapped; + extendedParameters.dwSecurityQosFlags = SECURITY_ANONYMOUS; + h = osCreateFile2((LPCWSTR)zConverted, + (GENERIC_READ | (bReadonly ? 0 : GENERIC_WRITE)),/* dwDesiredAccess */ + FILE_SHARE_READ | FILE_SHARE_WRITE, /* dwShareMode */ + OPEN_ALWAYS, /* dwCreationDisposition */ + &extendedParameters + ); +#else + h = osCreateFileW((LPCWSTR)zConverted, /* lpFileName */ + (GENERIC_READ | (bReadonly ? 0 : GENERIC_WRITE)), /* dwDesiredAccess */ + FILE_SHARE_READ | FILE_SHARE_WRITE, /* dwShareMode */ + NULL, /* lpSecurityAttributes */ + OPEN_ALWAYS, /* dwCreationDisposition */ + FILE_ATTRIBUTE_NORMAL|flag_overlapped, + NULL + ); +#endif + }else{ + /* Due to pre-processor directives earlier in this file, + ** SQLITE_WIN32_HAS_ANSI is always defined if osIsNT() is false. */ +#ifdef SQLITE_WIN32_HAS_ANSI + h = osCreateFileA((LPCSTR)zConverted, + (GENERIC_READ | (bReadonly ? 0 : GENERIC_WRITE)), /* dwDesiredAccess */ + FILE_SHARE_READ | FILE_SHARE_WRITE, /* dwShareMode */ + NULL, /* lpSecurityAttributes */ + OPEN_ALWAYS, /* dwCreationDisposition */ + FILE_ATTRIBUTE_NORMAL|flag_overlapped, + NULL + ); +#endif + } + + if( h==INVALID_HANDLE_VALUE ){ + if( bReadonly==0 ){ + bReadonly = 1; + rc = winHandleOpen(zUtf8, &bReadonly, &h); + }else{ + rc = SQLITE_CANTOPEN_BKPT; + } + } + + winopenfile_out: + sqlite3_free(zConverted); + *pbReadonly = bReadonly; + *ph = h; + return rc; +} + +/* +** Close pDbFd's connection to shared-memory. Delete the underlying +** *-shm file if deleteFlag is true. +*/ +static int winCloseSharedMemory(winFile *pDbFd, int deleteFlag){ + winShm *p; /* The connection to be closed */ + winShm **pp; /* Iterator for pShmNode->pWinShmList */ + winShmNode *pShmNode; /* The underlying shared-memory file */ + + p = pDbFd->pShm; + if( p==0 ) return SQLITE_OK; + if( p->hShm!=INVALID_HANDLE_VALUE ){ + osCloseHandle(p->hShm); + } + + winShmEnterMutex(); + pShmNode = p->pShmNode; + + /* Remove this connection from the winShmNode.pWinShmList list */ + sqlite3_mutex_enter(pShmNode->mutex); + for(pp=&pShmNode->pWinShmList; *pp!=p; pp=&(*pp)->pWinShmNext){} + *pp = p->pWinShmNext; + sqlite3_mutex_leave(pShmNode->mutex); + + winShmPurge(pDbFd->pVfs, deleteFlag); + winShmLeaveMutex(); + + /* Free the connection p */ + sqlite3_free(p); + pDbFd->pShm = 0; + return SQLITE_OK; +} + +/* +** testfixture builds may set this global variable to true via a +** Tcl interface. This forces the VFS to use the locking normally +** only used for UNC paths for all files. +*/ +#ifdef SQLITE_TEST +int sqlite3_win_test_unc_locking = 0; +#else +# define sqlite3_win_test_unc_locking 0 +#endif + +/* +** Return true if the string passed as the only argument is likely +** to be a UNC path. In other words, if it starts with "\\". +*/ +static int winIsUNCPath(const char *zFile){ + if( zFile[0]=='\\' && zFile[1]=='\\' ){ + return 1; + } + return sqlite3_win_test_unc_locking; +} + +/* +** Open the shared-memory area associated with database file pDbFd. */ static int winOpenSharedMemory(winFile *pDbFd){ struct winShm *p; /* The connection to be opened */ - struct winShmNode *pShmNode = 0; /* The underlying mmapped file */ - int rc; /* Result code */ - struct winShmNode *pNew; /* Newly allocated winShmNode */ + winShmNode *pShmNode = 0; /* The underlying mmapped file */ + int rc = SQLITE_OK; /* Result code */ + winShmNode *pNew; /* Newly allocated winShmNode */ int nName; /* Size of zName in bytes */ assert( pDbFd->pShm==0 ); /* Not previously opened */ /* Allocate space for the new sqlite3_shm object. Also speculatively - ** allocate space for a new winShmNode and filename. - */ + ** allocate space for a new winShmNode and filename. */ p = sqlite3MallocZero( sizeof(*p) ); - if( p==0 ) return SQLITE_IOERR_NOMEM; + if( p==0 ) return SQLITE_IOERR_NOMEM_BKPT; nName = sqlite3Strlen30(pDbFd->zPath); - pNew = sqlite3MallocZero( sizeof(*pShmNode) + nName + 17 ); + pNew = sqlite3MallocZero( sizeof(*pShmNode) + (i64)nName + 17 ); if( pNew==0 ){ sqlite3_free(p); - return SQLITE_IOERR_NOMEM; + return SQLITE_IOERR_NOMEM_BKPT; } pNew->zFilename = (char*)&pNew[1]; + pNew->hSharedShm = INVALID_HANDLE_VALUE; + pNew->isUnlocked = 1; + pNew->bUseSharedLockHandle = winIsUNCPath(pDbFd->zPath); sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath); sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename); /* Look to see if there is an existing winShmNode that can be used. - ** If no matching winShmNode currently exists, create a new one. - */ + ** If no matching winShmNode currently exists, then create a new one. */ winShmEnterMutex(); for(pShmNode = winShmNodeList; pShmNode; pShmNode=pShmNode->pNext){ /* TBD need to come up with better match here. Perhaps - ** use FILE_ID_BOTH_DIR_INFO Structure. - */ + ** use FILE_ID_BOTH_DIR_INFO Structure. */ if( sqlite3StrICmp(pShmNode->zFilename, pNew->zFilename)==0 ) break; } - if( pShmNode ){ - sqlite3_free(pNew); - }else{ + if( pShmNode==0 ){ pShmNode = pNew; - pNew = 0; - ((winFile*)(&pShmNode->hFile))->h = INVALID_HANDLE_VALUE; - pShmNode->pNext = winShmNodeList; - winShmNodeList = pShmNode; - - pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); - if( pShmNode->mutex==0 ){ - rc = SQLITE_IOERR_NOMEM; - goto shm_open_err; - } - rc = winOpen(pDbFd->pVfs, - pShmNode->zFilename, /* Name of the file (UTF-8) */ - (sqlite3_file*)&pShmNode->hFile, /* File handle here */ - SQLITE_OPEN_WAL | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, - 0); - if( SQLITE_OK!=rc ){ - goto shm_open_err; + /* Allocate a mutex for this winShmNode object, if one is required. */ + if( sqlite3GlobalConfig.bCoreMutex ){ + pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); + if( pShmNode->mutex==0 ) rc = SQLITE_IOERR_NOMEM_BKPT; } - /* Check to see if another process is holding the dead-man switch. - ** If not, truncate the file to zero length. - */ - if( winShmSystemLock(pShmNode, _SHM_WRLCK, WIN_SHM_DMS, 1)==SQLITE_OK ){ - rc = winTruncate((sqlite3_file *)&pShmNode->hFile, 0); - if( rc!=SQLITE_OK ){ - rc = winLogError(SQLITE_IOERR_SHMOPEN, osGetLastError(), - "winOpenShm", pDbFd->zPath); - } + /* Open a file-handle to use for mappings, and for the DMS lock. */ + if( rc==SQLITE_OK ){ + HANDLE h = INVALID_HANDLE_VALUE; + pShmNode->isReadonly = sqlite3_uri_boolean(pDbFd->zPath,"readonly_shm",0); + rc = winHandleOpen(pNew->zFilename, &pShmNode->isReadonly, &h); + pShmNode->hSharedShm = h; } + + /* If successful, link the new winShmNode into the global list. If an + ** error occurred, free the object. */ if( rc==SQLITE_OK ){ - winShmSystemLock(pShmNode, _SHM_UNLCK, WIN_SHM_DMS, 1); - rc = winShmSystemLock(pShmNode, _SHM_RDLCK, WIN_SHM_DMS, 1); + pShmNode->pNext = winShmNodeList; + winShmNodeList = pShmNode; + pNew = 0; + }else{ + sqlite3_mutex_free(pShmNode->mutex); + if( pShmNode->hSharedShm!=INVALID_HANDLE_VALUE ){ + osCloseHandle(pShmNode->hSharedShm); + } } - if( rc ) goto shm_open_err; } - /* Make the new connection a child of the winShmNode */ - p->pShmNode = pShmNode; + /* If no error has occurred, link the winShm object to the winShmNode and + ** the winShm to pDbFd. */ + if( rc==SQLITE_OK ){ + sqlite3_mutex_enter(pShmNode->mutex); + p->pShmNode = pShmNode; + p->pWinShmNext = pShmNode->pWinShmList; + pShmNode->pWinShmList = p; #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE) - p->id = pShmNode->nextShmId++; + p->id = pShmNode->nextShmId++; #endif - pShmNode->nRef++; - pDbFd->pShm = p; + pDbFd->pShm = p; + sqlite3_mutex_leave(pShmNode->mutex); + }else if( p ){ + sqlite3_free(p); + } + + assert( rc!=SQLITE_OK || pShmNode->isUnlocked==0 || pShmNode->nRegion==0 ); winShmLeaveMutex(); + sqlite3_free(pNew); - /* The reference count on pShmNode has already been incremented under - ** the cover of the winShmEnterMutex() mutex and the pointer from the - ** new (struct winShm) object to the pShmNode has been set. All that is - ** left to do is to link the new object into the linked list starting - ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex - ** mutex. - */ - sqlite3_mutex_enter(pShmNode->mutex); - p->pNext = pShmNode->pFirst; - pShmNode->pFirst = p; - sqlite3_mutex_leave(pShmNode->mutex); - return SQLITE_OK; + /* Open a file-handle on the *-shm file for this connection. This file-handle + ** is only used for locking. The mapping of the *-shm file is created using + ** the shared file handle in winShmNode.hSharedShm. */ + if( rc==SQLITE_OK && pShmNode->bUseSharedLockHandle==0 ){ + p->bReadonly = sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0); + rc = winHandleOpen(pShmNode->zFilename, &p->bReadonly, &p->hShm); + if( rc!=SQLITE_OK ){ + assert( p->hShm==INVALID_HANDLE_VALUE ); + winCloseSharedMemory(pDbFd, 0); + } + } - /* Jump here on any error */ -shm_open_err: - winShmSystemLock(pShmNode, _SHM_UNLCK, WIN_SHM_DMS, 1); - winShmPurge(pDbFd->pVfs, 0); /* This call frees pShmNode if required */ - sqlite3_free(p); - sqlite3_free(pNew); - winShmLeaveMutex(); return rc; } @@ -3717,38 +4578,7 @@ static int winShmUnmap( sqlite3_file *fd, /* Database holding shared memory */ int deleteFlag /* Delete after closing if true */ ){ - winFile *pDbFd; /* Database holding shared-memory */ - winShm *p; /* The connection to be closed */ - winShmNode *pShmNode; /* The underlying shared-memory file */ - winShm **pp; /* For looping over sibling connections */ - - pDbFd = (winFile*)fd; - p = pDbFd->pShm; - if( p==0 ) return SQLITE_OK; - pShmNode = p->pShmNode; - - /* Remove connection p from the set of connections associated - ** with pShmNode */ - sqlite3_mutex_enter(pShmNode->mutex); - for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){} - *pp = p->pNext; - - /* Free the connection p */ - sqlite3_free(p); - pDbFd->pShm = 0; - sqlite3_mutex_leave(pShmNode->mutex); - - /* If pShmNode->nRef has reached 0, then close the underlying - ** shared-memory file, too */ - winShmEnterMutex(); - assert( pShmNode->nRef>0 ); - pShmNode->nRef--; - if( pShmNode->nRef==0 ){ - winShmPurge(pDbFd->pVfs, deleteFlag); - } - winShmLeaveMutex(); - - return SQLITE_OK; + return winCloseSharedMemory((winFile*)fd, deleteFlag); } /* @@ -3762,10 +4592,13 @@ static int winShmLock( ){ winFile *pDbFd = (winFile*)fd; /* Connection holding shared memory */ winShm *p = pDbFd->pShm; /* The shared memory being locked */ - winShm *pX; /* For looping over all siblings */ - winShmNode *pShmNode = p->pShmNode; + winShmNode *pShmNode; int rc = SQLITE_OK; /* Result code */ - u16 mask; /* Mask of locks to take or release */ + u16 mask = (u16)((1U<<(ofst+n)) - (1U<pShmNode; + if( NEVER(pShmNode==0) ) return SQLITE_IOERR_SHMLOCK; assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK ); assert( n>=1 ); @@ -3775,85 +4608,127 @@ static int winShmLock( || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) ); assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 ); - mask = (u16)((1U<<(ofst+n)) - (1U<1 || mask==(1<mutex); - if( flags & SQLITE_SHM_UNLOCK ){ - u16 allMask = 0; /* Mask of locks held by siblings */ - - /* See if any siblings hold this same lock */ - for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ - if( pX==p ) continue; - assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 ); - allMask |= pX->sharedMask; - } - - /* Unlock the system-level locks */ - if( (mask & allMask)==0 ){ - rc = winShmSystemLock(pShmNode, _SHM_UNLCK, ofst+WIN_SHM_BASE, n); - }else{ - rc = SQLITE_OK; - } - - /* Undo the local locks */ - if( rc==SQLITE_OK ){ - p->exclMask &= ~mask; - p->sharedMask &= ~mask; - } - }else if( flags & SQLITE_SHM_SHARED ){ - u16 allShared = 0; /* Union of locks held by connections other than "p" */ + /* Check that, if this to be a blocking lock, no locks that occur later + ** in the following list than the lock being obtained are already held: + ** + ** 1. Recovery lock (ofst==2). + ** 2. Checkpointer lock (ofst==1). + ** 3. Write lock (ofst==0). + ** 4. Read locks (ofst>=3 && ofstexclMask|p->sharedMask); + assert( (flags & SQLITE_SHM_UNLOCK) || pDbFd->iBusyTimeout==0 || ( + (ofst!=2 || lockMask==0) + && (ofst!=1 || lockMask==0 || lockMask==2) + && (ofst!=0 || lockMask<3) + && (ofst<3 || lockMask<(1<pFirst; pX; pX=pX->pNext){ - if( (pX->exclMask & mask)!=0 ){ - rc = SQLITE_BUSY; - break; + /* Check if there is any work to do. There are three cases: + ** + ** a) An unlock operation where there are locks to unlock, + ** b) An shared lock where the requested lock is not already held + ** c) An exclusive lock where the requested lock is not already held + ** + ** The SQLite core never requests an exclusive lock that it already holds. + ** This is assert()ed immediately below. */ + assert( flags!=(SQLITE_SHM_EXCLUSIVE|SQLITE_SHM_LOCK) + || 0==(p->exclMask & mask) + ); + if( ((flags & SQLITE_SHM_UNLOCK) && ((p->exclMask|p->sharedMask) & mask)) + || (flags==(SQLITE_SHM_SHARED|SQLITE_SHM_LOCK) && 0==(p->sharedMask & mask)) + || (flags==(SQLITE_SHM_EXCLUSIVE|SQLITE_SHM_LOCK)) + ){ + HANDLE h = p->hShm; + + if( flags & SQLITE_SHM_UNLOCK ){ + /* Case (a) - unlock. */ + + assert( (p->exclMask & p->sharedMask)==0 ); + assert( !(flags & SQLITE_SHM_EXCLUSIVE) || (p->exclMask & mask)==mask ); + assert( !(flags & SQLITE_SHM_SHARED) || (p->sharedMask & mask)==mask ); + + assert( !(flags & SQLITE_SHM_SHARED) || n==1 ); + if( pShmNode->bUseSharedLockHandle ){ + h = pShmNode->hSharedShm; + if( flags & SQLITE_SHM_SHARED ){ + winShm *pShm; + sqlite3_mutex_enter(pShmNode->mutex); + for(pShm=pShmNode->pWinShmList; pShm; pShm=pShm->pWinShmNext){ + if( pShm!=p && (pShm->sharedMask & mask) ){ + /* Another connection within this process is also holding this + ** SHARED lock. So do not actually release the OS lock. */ + h = INVALID_HANDLE_VALUE; + break; + } + } + sqlite3_mutex_leave(pShmNode->mutex); + } } - allShared |= pX->sharedMask; - } - /* Get shared locks at the system level, if necessary */ - if( rc==SQLITE_OK ){ - if( (allShared & mask)==0 ){ - rc = winShmSystemLock(pShmNode, _SHM_RDLCK, ofst+WIN_SHM_BASE, n); - }else{ - rc = SQLITE_OK; + if( h!=INVALID_HANDLE_VALUE ){ + rc = winHandleUnlock(h, ofst+WIN_SHM_BASE, n); } - } - /* Get the local shared locks */ - if( rc==SQLITE_OK ){ - p->sharedMask |= mask; - } - }else{ - /* Make sure no sibling connections hold locks that will block this - ** lock. If any do, return SQLITE_BUSY right away. - */ - for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ - if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){ - rc = SQLITE_BUSY; - break; + /* If successful, also clear the bits in sharedMask/exclMask */ + if( rc==SQLITE_OK ){ + p->exclMask = (p->exclMask & ~mask); + p->sharedMask = (p->sharedMask & ~mask); + } + }else{ + int bExcl = ((flags & SQLITE_SHM_EXCLUSIVE) ? 1 : 0); + DWORD nMs = winFileBusyTimeout(pDbFd); + + if( pShmNode->bUseSharedLockHandle ){ + winShm *pShm; + h = pShmNode->hSharedShm; + sqlite3_mutex_enter(pShmNode->mutex); + for(pShm=pShmNode->pWinShmList; pShm; pShm=pShm->pWinShmNext){ + if( bExcl ){ + if( (pShm->sharedMask|pShm->exclMask) & mask ){ + rc = SQLITE_BUSY; + h = INVALID_HANDLE_VALUE; + } + }else{ + if( pShm->sharedMask & mask ){ + h = INVALID_HANDLE_VALUE; + }else if( pShm->exclMask & mask ){ + rc = SQLITE_BUSY; + h = INVALID_HANDLE_VALUE; + } + } + } + sqlite3_mutex_leave(pShmNode->mutex); } - } - /* Get the exclusive locks at the system level. Then if successful - ** also mark the local connection as being locked. - */ - if( rc==SQLITE_OK ){ - rc = winShmSystemLock(pShmNode, _SHM_WRLCK, ofst+WIN_SHM_BASE, n); + if( h!=INVALID_HANDLE_VALUE ){ + rc = winHandleLockTimeout(h, ofst+WIN_SHM_BASE, n, bExcl, nMs); + } if( rc==SQLITE_OK ){ - assert( (p->sharedMask & mask)==0 ); - p->exclMask |= mask; + if( bExcl ){ + p->exclMask = (p->exclMask | mask); + }else{ + p->sharedMask = (p->sharedMask | mask); + } } } } - sqlite3_mutex_leave(pShmNode->mutex); - OSTRACE(("SHM-LOCK pid=%lu, id=%d, sharedMask=%03x, exclMask=%03x, rc=%s\n", - osGetCurrentProcessId(), p->id, p->sharedMask, p->exclMask, - sqlite3ErrName(rc))); + + OSTRACE(( + "SHM-LOCK(%d,%d,%d) pid=%lu, id=%d, sharedMask=%03x, exclMask=%03x," + " rc=%s\n", + ofst, n, flags, + osGetCurrentProcessId(), p->id, p->sharedMask, p->exclMask, + sqlite3ErrName(rc)) + ); return rc; } @@ -3901,19 +4776,29 @@ static int winShmMap( winFile *pDbFd = (winFile*)fd; winShm *pShm = pDbFd->pShm; winShmNode *pShmNode; + DWORD protect = PAGE_READWRITE; + DWORD flags = FILE_MAP_WRITE | FILE_MAP_READ; int rc = SQLITE_OK; if( !pShm ){ rc = winOpenSharedMemory(pDbFd); if( rc!=SQLITE_OK ) return rc; pShm = pDbFd->pShm; + assert( pShm!=0 ); } pShmNode = pShm->pShmNode; sqlite3_mutex_enter(pShmNode->mutex); - assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 ); + if( pShmNode->isUnlocked ){ + /* Take the DMS lock. */ + assert( pShmNode->nRegion==0 ); + rc = winLockSharedMemory(pShmNode, winFileBusyTimeout(pDbFd)); + if( rc!=SQLITE_OK ) goto shmpage_out; + } + assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 ); if( pShmNode->nRegion<=iRegion ){ + HANDLE hShared = pShmNode->hSharedShm; struct ShmRegion *apNew; /* New aRegion[] array */ int nByte = (iRegion+1)*szRegion; /* Minimum required file size */ sqlite3_int64 sz; /* Current size of wal-index file */ @@ -3924,10 +4809,9 @@ static int winShmMap( ** Check to see if it has been allocated (i.e. if the wal-index file is ** large enough to contain the requested region). */ - rc = winFileSize((sqlite3_file *)&pShmNode->hFile, &sz); + rc = winHandleSize(hShared, &sz); if( rc!=SQLITE_OK ){ - rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(), - "winShmMap1", pDbFd->zPath); + rc = winLogError(rc, osGetLastError(), "winShmMap1", pDbFd->zPath); goto shmpage_out; } @@ -3936,44 +4820,42 @@ static int winShmMap( ** zero, exit early. *pp will be set to NULL and SQLITE_OK returned. ** ** Alternatively, if isWrite is non-zero, use ftruncate() to allocate - ** the requested memory region. - */ + ** the requested memory region. */ if( !isWrite ) goto shmpage_out; - rc = winTruncate((sqlite3_file *)&pShmNode->hFile, nByte); + rc = winHandleTruncate(hShared, nByte); if( rc!=SQLITE_OK ){ - rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(), - "winShmMap2", pDbFd->zPath); + rc = winLogError(rc, osGetLastError(), "winShmMap2", pDbFd->zPath); goto shmpage_out; } } /* Map the requested memory region into this processes address space. */ - apNew = (struct ShmRegion *)sqlite3_realloc64( + apNew = (struct ShmRegion*)sqlite3_realloc64( pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0]) ); if( !apNew ){ - rc = SQLITE_IOERR_NOMEM; + rc = SQLITE_IOERR_NOMEM_BKPT; goto shmpage_out; } pShmNode->aRegion = apNew; + if( pShmNode->isReadonly ){ + protect = PAGE_READONLY; + flags = FILE_MAP_READ; + } + while( pShmNode->nRegion<=iRegion ){ HANDLE hMap = NULL; /* file-mapping handle */ void *pMap = 0; /* Mapped memory region */ #if SQLITE_OS_WINRT - hMap = osCreateFileMappingFromApp(pShmNode->hFile.h, - NULL, PAGE_READWRITE, nByte, NULL - ); + hMap = osCreateFileMappingFromApp(hShared, NULL, protect, nByte, NULL); #elif defined(SQLITE_WIN32_HAS_WIDE) - hMap = osCreateFileMappingW(pShmNode->hFile.h, - NULL, PAGE_READWRITE, 0, nByte, NULL - ); + hMap = osCreateFileMappingW(hShared, NULL, protect, 0, nByte, NULL); #elif defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_CREATEFILEMAPPINGA - hMap = osCreateFileMappingA(pShmNode->hFile.h, - NULL, PAGE_READWRITE, 0, nByte, NULL - ); + hMap = osCreateFileMappingA(hShared, NULL, protect, 0, nByte, NULL); #endif + OSTRACE(("SHM-MAP-CREATE pid=%lu, region=%d, size=%d, rc=%s\n", osGetCurrentProcessId(), pShmNode->nRegion, nByte, hMap ? "ok" : "failed")); @@ -3981,11 +4863,11 @@ static int winShmMap( int iOffset = pShmNode->nRegion*szRegion; int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity; #if SQLITE_OS_WINRT - pMap = osMapViewOfFileFromApp(hMap, FILE_MAP_WRITE | FILE_MAP_READ, + pMap = osMapViewOfFileFromApp(hMap, flags, iOffset - iOffsetShift, szRegion + iOffsetShift ); #else - pMap = osMapViewOfFile(hMap, FILE_MAP_WRITE | FILE_MAP_READ, + pMap = osMapViewOfFile(hMap, flags, 0, iOffset - iOffsetShift, szRegion + iOffsetShift ); #endif @@ -4016,6 +4898,9 @@ static int winShmMap( }else{ *pp = 0; } + if( pShmNode->isReadonly && rc==SQLITE_OK ){ + rc = SQLITE_READONLY; + } sqlite3_mutex_leave(pShmNode->mutex); return rc; } @@ -4034,9 +4919,9 @@ static int winShmMap( static int winUnmapfile(winFile *pFile){ assert( pFile!=0 ); OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, pMapRegion=%p, " - "mmapSize=%lld, mmapSizeActual=%lld, mmapSizeMax=%lld\n", + "mmapSize=%lld, mmapSizeMax=%lld\n", osGetCurrentProcessId(), pFile, pFile->hMap, pFile->pMapRegion, - pFile->mmapSize, pFile->mmapSizeActual, pFile->mmapSizeMax)); + pFile->mmapSize, pFile->mmapSizeMax)); if( pFile->pMapRegion ){ if( !osUnmapViewOfFile(pFile->pMapRegion) ){ pFile->lastErrno = osGetLastError(); @@ -4048,7 +4933,6 @@ static int winUnmapfile(winFile *pFile){ } pFile->pMapRegion = 0; pFile->mmapSize = 0; - pFile->mmapSizeActual = 0; } if( pFile->hMap!=NULL ){ if( !osCloseHandle(pFile->hMap) ){ @@ -4159,7 +5043,6 @@ static int winMapfile(winFile *pFd, sqlite3_int64 nByte){ } pFd->pMapRegion = pNew; pFd->mmapSize = nMap; - pFd->mmapSizeActual = nMap; } OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n", @@ -4191,6 +5074,11 @@ static int winFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){ #if SQLITE_MAX_MMAP_SIZE>0 if( pFd->mmapSizeMax>0 ){ + /* Ensure that there is always at least a 256 byte buffer of addressable + ** memory following the returned page. If the database is corrupt, + ** SQLite may overread the page slightly (in practice only a few bytes, + ** but 256 is safe, round, number). */ + const int nEofBuffer = 256; if( pFd->pMapRegion==0 ){ int rc = winMapfile(pFd, -1); if( rc!=SQLITE_OK ){ @@ -4199,7 +5087,8 @@ static int winFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){ return rc; } } - if( pFd->mmapSize >= iOff+nAmt ){ + if( pFd->mmapSize >= (iOff+nAmt+nEofBuffer) ){ + assert( pFd->pMapRegion!=0 ); *pp = &((u8 *)pFd->pMapRegion)[iOff]; pFd->nFetchOut++; } @@ -4262,9 +5151,35 @@ static int winUnfetch(sqlite3_file *fd, i64 iOff, void *p){ /* ** This vector defines all the methods that can operate on an -** sqlite3_file for win32. +** sqlite3_file for win32. +*/ +static const sqlite3_io_methods winIoMethod = { + 3, /* iVersion */ + winClose, /* xClose */ + winRead, /* xRead */ + winWrite, /* xWrite */ + winTruncate, /* xTruncate */ + winSync, /* xSync */ + winFileSize, /* xFileSize */ + winLock, /* xLock */ + winUnlock, /* xUnlock */ + winCheckReservedLock, /* xCheckReservedLock */ + winFileControl, /* xFileControl */ + winSectorSize, /* xSectorSize */ + winDeviceCharacteristics, /* xDeviceCharacteristics */ + winShmMap, /* xShmMap */ + winShmLock, /* xShmLock */ + winShmBarrier, /* xShmBarrier */ + winShmUnmap, /* xShmUnmap */ + winFetch, /* xFetch */ + winUnfetch /* xUnfetch */ +}; + +/* +** This vector defines all the methods that can operate on an +** sqlite3_file for win32 without performing any locking. */ -static const sqlite3_io_methods winIoMethod = { +static const sqlite3_io_methods winIoNolockMethod = { 3, /* iVersion */ winClose, /* xClose */ winRead, /* xRead */ @@ -4272,9 +5187,9 @@ static const sqlite3_io_methods winIoMethod = { winTruncate, /* xTruncate */ winSync, /* xSync */ winFileSize, /* xFileSize */ - winLock, /* xLock */ - winUnlock, /* xUnlock */ - winCheckReservedLock, /* xCheckReservedLock */ + winNolockLock, /* xLock */ + winNolockUnlock, /* xUnlock */ + winNolockCheckReservedLock, /* xCheckReservedLock */ winFileControl, /* xFileControl */ winSectorSize, /* xSectorSize */ winDeviceCharacteristics, /* xDeviceCharacteristics */ @@ -4286,6 +5201,18 @@ static const sqlite3_io_methods winIoMethod = { winUnfetch /* xUnfetch */ }; +static winVfsAppData winAppData = { + &winIoMethod, /* pMethod */ + 0, /* pAppData */ + 0 /* bNoLock */ +}; + +static winVfsAppData winNolockAppData = { + &winIoNolockMethod, /* pMethod */ + 0, /* pAppData */ + 1 /* bNoLock */ +}; + /**************************************************************************** **************************** sqlite3_vfs methods **************************** ** @@ -4293,47 +5220,6 @@ static const sqlite3_io_methods winIoMethod = { ** sqlite3_vfs object. */ -#if defined(__CYGWIN__) -/* -** Convert a filename from whatever the underlying operating system -** supports for filenames into UTF-8. Space to hold the result is -** obtained from malloc and must be freed by the calling function. -*/ -static char *winConvertToUtf8Filename(const void *zFilename){ - char *zConverted = 0; - if( osIsNT() ){ - zConverted = winUnicodeToUtf8(zFilename); - } -#ifdef SQLITE_WIN32_HAS_ANSI - else{ - zConverted = sqlite3_win32_mbcs_to_utf8(zFilename); - } -#endif - /* caller will handle out of memory */ - return zConverted; -} -#endif - -/* -** Convert a UTF-8 filename into whatever form the underlying -** operating system wants filenames in. Space to hold the result -** is obtained from malloc and must be freed by the calling -** function. -*/ -static void *winConvertFromUtf8Filename(const char *zFilename){ - void *zConverted = 0; - if( osIsNT() ){ - zConverted = winUtf8ToUnicode(zFilename); - } -#ifdef SQLITE_WIN32_HAS_ANSI - else{ - zConverted = sqlite3_win32_utf8_to_mbcs(zFilename); - } -#endif - /* caller will handle out of memory */ - return zConverted; -} - /* ** This function returns non-zero if the specified UTF-8 string buffer ** ends with a directory separator character or one was successfully @@ -4346,7 +5232,14 @@ static int winMakeEndInDirSep(int nBuf, char *zBuf){ if( winIsDirSep(zBuf[nLen-1]) ){ return 1; }else if( nLen+1mxPathname; nBuf = nMax + 2; + nMax = pVfs->mxPathname; + nBuf = 2 + (i64)nMax; zBuf = sqlite3MallocZero( nBuf ); if( !zBuf ){ OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n")); - return SQLITE_IOERR_NOMEM; + return SQLITE_IOERR_NOMEM_BKPT; } /* Figure out the effective temporary directory. First, check if one @@ -4391,22 +5299,25 @@ static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){ */ nDir = nMax - (nPre + 15); assert( nDir>0 ); - if( sqlite3_temp_directory ){ + if( winTempDirDefined() ){ int nDirLen = sqlite3Strlen30(sqlite3_temp_directory); if( nDirLen>0 ){ if( !winIsDirSep(sqlite3_temp_directory[nDirLen-1]) ){ nDirLen++; } if( nDirLen>nDir ){ + sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR)); sqlite3_free(zBuf); OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n")); return winLogError(SQLITE_ERROR, 0, "winGetTempname1", 0); } sqlite3_snprintf(nMax, zBuf, "%s", sqlite3_temp_directory); } + sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR)); } + #if defined(__CYGWIN__) - else{ + else if( osGetenv!=NULL ){ static const char *azDirs[] = { 0, /* getenv("SQLITE_TMPDIR") */ 0, /* getenv("TMPDIR") */ @@ -4422,11 +5333,11 @@ static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){ unsigned int i; const char *zDir = 0; - if( !azDirs[0] ) azDirs[0] = getenv("SQLITE_TMPDIR"); - if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR"); - if( !azDirs[2] ) azDirs[2] = getenv("TMP"); - if( !azDirs[3] ) azDirs[3] = getenv("TEMP"); - if( !azDirs[4] ) azDirs[4] = getenv("USERPROFILE"); + if( !azDirs[0] ) azDirs[0] = osGetenv("SQLITE_TMPDIR"); + if( !azDirs[1] ) azDirs[1] = osGetenv("TMPDIR"); + if( !azDirs[2] ) azDirs[2] = osGetenv("TMP"); + if( !azDirs[3] ) azDirs[3] = osGetenv("TEMP"); + if( !azDirs[4] ) azDirs[4] = osGetenv("USERPROFILE"); for(i=0; i>= 8; zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ]; } zBuf[j] = 0; @@ -4606,7 +5488,7 @@ static int winIsDir(const void *zConverted){ return 0; /* Invalid name? */ } attr = sAttrData.dwFileAttributes; -#if SQLITE_OS_WINCE==0 +#if SQLITE_OS_WINCE==0 && defined(SQLITE_WIN32_HAS_ANSI) }else{ attr = osGetFileAttributesA((char*)zConverted); #endif @@ -4614,11 +5496,25 @@ static int winIsDir(const void *zConverted){ return (attr!=INVALID_FILE_ATTRIBUTES) && (attr&FILE_ATTRIBUTE_DIRECTORY); } +/* forward reference */ +static int winAccess( + sqlite3_vfs *pVfs, /* Not used on win32 */ + const char *zFilename, /* Name of file to check */ + int flags, /* Type of test to make on this file */ + int *pResOut /* OUT: Result */ +); + +/* +** The Windows version of xAccess() accepts an extra bit in the flags +** parameter that prevents an anti-virus retry loop. +*/ +#define NORETRY 0x4000 + /* ** Open a file. */ static int winOpen( - sqlite3_vfs *pVfs, /* Used to get maximum path name length */ + sqlite3_vfs *pVfs, /* Used to get maximum path length and AppData */ const char *zName, /* Name of the file (UTF-8) */ sqlite3_file *id, /* Write the SQLite file handle here */ int flags, /* Open mode flags */ @@ -4633,10 +5529,12 @@ static int winOpen( #if SQLITE_OS_WINCE int isTemp = 0; #endif + winVfsAppData *pAppData; winFile *pFile = (winFile*)id; void *zConverted; /* Filename in OS encoding */ const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */ int cnt = 0; + int isRO = 0; /* file is known to be accessible readonly */ /* If argument zPath is a NULL pointer, this function is required to open ** a temporary file. Use this buffer to store the file name in. @@ -4645,7 +5543,7 @@ static int winOpen( int rc = SQLITE_OK; /* Function Return Code */ #if !defined(NDEBUG) || SQLITE_OS_WINCE - int eType = flags&0xFFFFFF00; /* Type of file to open */ + int eType = flags&0x0FFF00; /* Type of file to open */ #endif int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE); @@ -4656,7 +5554,7 @@ static int winOpen( #ifndef NDEBUG int isOpenJournal = (isCreate && ( - eType==SQLITE_OPEN_MASTER_JOURNAL + eType==SQLITE_OPEN_SUPER_JOURNAL || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_WAL )); @@ -4677,17 +5575,17 @@ static int winOpen( assert(isExclusive==0 || isCreate); assert(isDelete==0 || isCreate); - /* The main DB, main journal, WAL file and master journal are never + /* The main DB, main journal, WAL file and super-journal are never ** automatically deleted. Nor are they ever temporary files. */ assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB ); assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL ); - assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL ); + assert( (!isDelete && zName) || eType!=SQLITE_OPEN_SUPER_JOURNAL ); assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL ); /* Assert that the upper layer has set one of the "file-type" flags. */ assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL - || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL + || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_SUPER_JOURNAL || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL ); @@ -4727,7 +5625,7 @@ static int winOpen( if( zConverted==0 ){ sqlite3_free(zTmpname); OSTRACE(("OPEN name=%s, rc=SQLITE_IOERR_NOMEM", zUtf8Name)); - return SQLITE_IOERR_NOMEM; + return SQLITE_IOERR_NOMEM_BKPT; } if( winIsDir(zConverted) ){ @@ -4759,7 +5657,11 @@ static int winOpen( dwCreationDisposition = OPEN_EXISTING; } - dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE; + if( 0==sqlite3_uri_boolean(zName, "exclusive", 0) ){ + dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE; + }else{ + dwShareMode = 0; + } if( isDelete ){ #if SQLITE_OS_WINCE @@ -4789,37 +5691,58 @@ static int winOpen( extendedParameters.dwSecurityQosFlags = SECURITY_ANONYMOUS; extendedParameters.lpSecurityAttributes = NULL; extendedParameters.hTemplateFile = NULL; - while( (h = osCreateFile2((LPCWSTR)zConverted, - dwDesiredAccess, - dwShareMode, - dwCreationDisposition, - &extendedParameters))==INVALID_HANDLE_VALUE && - winRetryIoerr(&cnt, &lastErrno) ){ - /* Noop */ - } + do{ + h = osCreateFile2((LPCWSTR)zConverted, + dwDesiredAccess, + dwShareMode, + dwCreationDisposition, + &extendedParameters); + if( h!=INVALID_HANDLE_VALUE ) break; + if( isReadWrite ){ + int rc2; + sqlite3BeginBenignMalloc(); + rc2 = winAccess(pVfs, zUtf8Name, SQLITE_ACCESS_READ|NORETRY, &isRO); + sqlite3EndBenignMalloc(); + if( rc2==SQLITE_OK && isRO ) break; + } + }while( winRetryIoerr(&cnt, &lastErrno) ); #else - while( (h = osCreateFileW((LPCWSTR)zConverted, - dwDesiredAccess, - dwShareMode, NULL, - dwCreationDisposition, - dwFlagsAndAttributes, - NULL))==INVALID_HANDLE_VALUE && - winRetryIoerr(&cnt, &lastErrno) ){ - /* Noop */ - } + do{ + h = osCreateFileW((LPCWSTR)zConverted, + dwDesiredAccess, + dwShareMode, NULL, + dwCreationDisposition, + dwFlagsAndAttributes, + NULL); + if( h!=INVALID_HANDLE_VALUE ) break; + if( isReadWrite ){ + int rc2; + sqlite3BeginBenignMalloc(); + rc2 = winAccess(pVfs, zUtf8Name, SQLITE_ACCESS_READ|NORETRY, &isRO); + sqlite3EndBenignMalloc(); + if( rc2==SQLITE_OK && isRO ) break; + } + }while( winRetryIoerr(&cnt, &lastErrno) ); #endif } #ifdef SQLITE_WIN32_HAS_ANSI else{ - while( (h = osCreateFileA((LPCSTR)zConverted, - dwDesiredAccess, - dwShareMode, NULL, - dwCreationDisposition, - dwFlagsAndAttributes, - NULL))==INVALID_HANDLE_VALUE && - winRetryIoerr(&cnt, &lastErrno) ){ - /* Noop */ - } + do{ + h = osCreateFileA((LPCSTR)zConverted, + dwDesiredAccess, + dwShareMode, NULL, + dwCreationDisposition, + dwFlagsAndAttributes, + NULL); + if( h!=INVALID_HANDLE_VALUE ) break; + if( isReadWrite ){ + int rc2; + sqlite3BeginBenignMalloc(); + rc2 = winAccess(pVfs, zUtf8Name, SQLITE_ACCESS_READ|NORETRY, &isRO); + sqlite3EndBenignMalloc(); + if( rc2==SQLITE_OK && isRO ) break; + } + }while( winRetryIoerr(&cnt, &lastErrno) ); } #endif winLogIoerr(cnt, __LINE__); @@ -4828,16 +5751,16 @@ static int winOpen( dwDesiredAccess, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok")); if( h==INVALID_HANDLE_VALUE ){ - pFile->lastErrno = lastErrno; - winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name); sqlite3_free(zConverted); sqlite3_free(zTmpname); - if( isReadWrite && !isExclusive ){ + if( isReadWrite && isRO && !isExclusive ){ return winOpen(pVfs, zName, id, ((flags|SQLITE_OPEN_READONLY) & ~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)), pOutFlags); }else{ + pFile->lastErrno = lastErrno; + winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name); return SQLITE_CANTOPEN_BKPT; } } @@ -4854,15 +5777,20 @@ static int winOpen( "rc=%s\n", h, zUtf8Name, dwDesiredAccess, pOutFlags, pOutFlags ? *pOutFlags : 0, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok")); + pAppData = (winVfsAppData*)pVfs->pAppData; + #if SQLITE_OS_WINCE - if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB - && (rc = winceCreateLock(zName, pFile))!=SQLITE_OK - ){ - osCloseHandle(h); - sqlite3_free(zConverted); - sqlite3_free(zTmpname); - OSTRACE(("OPEN-CE-LOCK name=%s, rc=%s\n", zName, sqlite3ErrName(rc))); - return rc; + { + if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB + && ((pAppData==NULL) || !pAppData->bNoLock) + && (rc = winceCreateLock(zName, pFile))!=SQLITE_OK + ){ + osCloseHandle(h); + sqlite3_free(zConverted); + sqlite3_free(zTmpname); + OSTRACE(("OPEN-CE-LOCK name=%s, rc=%s\n", zName, sqlite3ErrName(rc))); + return rc; + } } if( isTemp ){ pFile->zDeleteOnClose = zConverted; @@ -4873,13 +5801,15 @@ static int winOpen( } sqlite3_free(zTmpname); - pFile->pMethod = &winIoMethod; + id->pMethods = pAppData ? pAppData->pMethod : &winIoMethod; pFile->pVfs = pVfs; pFile->h = h; if( isReadonly ){ pFile->ctrlFlags |= WINFILE_RDONLY; } - if( sqlite3_uri_boolean(zName, "psow", SQLITE_POWERSAFE_OVERWRITE) ){ + if( (flags & SQLITE_OPEN_MAIN_DB) + && sqlite3_uri_boolean(zName, "psow", SQLITE_POWERSAFE_OVERWRITE) + ){ pFile->ctrlFlags |= WINFILE_PSOW; } pFile->lastErrno = NO_ERROR; @@ -4888,7 +5818,6 @@ static int winOpen( pFile->hMap = NULL; pFile->pMapRegion = 0; pFile->mmapSize = 0; - pFile->mmapSizeActual = 0; pFile->mmapSizeMax = sqlite3GlobalConfig.szMmap; #endif @@ -4927,7 +5856,7 @@ static int winDelete( zConverted = winConvertFromUtf8Filename(zFilename); if( zConverted==0 ){ OSTRACE(("DELETE name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename)); - return SQLITE_IOERR_NOMEM; + return SQLITE_IOERR_NOMEM_BKPT; } if( osIsNT() ){ do { @@ -5026,16 +5955,29 @@ static int winAccess( int rc = 0; DWORD lastErrno = 0; void *zConverted; + int noRetry = 0; /* Do not use winRetryIoerr() */ UNUSED_PARAMETER(pVfs); + if( (flags & NORETRY)!=0 ){ + noRetry = 1; + flags &= ~NORETRY; + } + SimulateIOError( return SQLITE_IOERR_ACCESS; ); OSTRACE(("ACCESS name=%s, flags=%x, pResOut=%p\n", zFilename, flags, pResOut)); + if( zFilename==0 ){ + *pResOut = 0; + OSTRACE(("ACCESS name=%s, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n", + zFilename, pResOut, *pResOut)); + return SQLITE_OK; + } + zConverted = winConvertFromUtf8Filename(zFilename); if( zConverted==0 ){ OSTRACE(("ACCESS name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename)); - return SQLITE_IOERR_NOMEM; + return SQLITE_IOERR_NOMEM_BKPT; } if( osIsNT() ){ int cnt = 0; @@ -5043,7 +5985,10 @@ static int winAccess( memset(&sAttrData, 0, sizeof(sAttrData)); while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted, GetFileExInfoStandard, - &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){} + &sAttrData)) + && !noRetry + && winRetryIoerr(&cnt, &lastErrno) + ){ /* Loop until true */} if( rc ){ /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file ** as if it does not exist. @@ -5090,6 +6035,17 @@ static int winAccess( return SQLITE_OK; } +/* +** Returns non-zero if the specified path name starts with the "long path" +** prefix. +*/ +static BOOL winIsLongPathPrefix( + const char *zPathname +){ + return ( zPathname[0]=='\\' && zPathname[1]=='\\' + && zPathname[2]=='?' && zPathname[3]=='\\' ); +} + /* ** Returns non-zero if the specified path name starts with a drive letter ** followed by a colon character. @@ -5100,6 +6056,7 @@ static BOOL winIsDriveLetterAndColon( return ( sqlite3Isalpha(zPathname[0]) && zPathname[1]==':' ); } +#ifdef _WIN32 /* ** Returns non-zero if the specified path name should be used verbatim. If ** non-zero is returned from this function, the calling function must simply @@ -5136,77 +6093,172 @@ static BOOL winIsVerbatimPathname( */ return FALSE; } +#endif /* _WIN32 */ + +#ifdef __CYGWIN__ +/* +** Simplify a filename into its canonical form +** by making the following changes: +** +** * convert any '/' to '\' (win32) or reverse (Cygwin) +** * removing any trailing and duplicate / (except for UNC paths) +** * convert /./ into just / +** +** Changes are made in-place. Return the new name length. +** +** The original filename is in z[0..]. If the path is shortened, +** no-longer used bytes will be written by '\0'. +*/ +static void winSimplifyName(char *z){ + int i, j; + for(i=j=0; z[i]; ++i){ + if( winIsDirSep(z[i]) ){ +#if !defined(SQLITE_TEST) + /* Some test-cases assume that "./foo" and "foo" are different */ + if( z[i+1]=='.' && winIsDirSep(z[i+2]) ){ + ++i; + continue; + } +#endif + if( !z[i+1] || (winIsDirSep(z[i+1]) && (i!=0)) ){ + continue; + } + z[j++] = osGetenv?'/':'\\'; + }else{ + z[j++] = z[i]; + } + } + while(jnOut ){ + /* SQLite assumes that xFullPathname() nul-terminates the output buffer + ** even if it returns an error. */ + zOut[iOff] = '\0'; + return SQLITE_CANTOPEN_BKPT; + } + sqlite3_snprintf(nOut-iOff, &zOut[iOff], "%s", zPath); + return SQLITE_OK; +} +#endif /* __CYGWIN__ */ /* ** Turn a relative pathname into a full pathname. Write the full ** pathname into zOut[]. zOut[] will be at least pVfs->mxPathname ** bytes in size. */ -static int winFullPathname( +static int winFullPathnameNoMutex( sqlite3_vfs *pVfs, /* Pointer to vfs object */ const char *zRelative, /* Possibly relative input path */ int nFull, /* Size of output buffer in bytes */ char *zFull /* Output buffer */ ){ +#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT + int nByte; + void *zConverted; + char *zOut; +#endif + + /* If this path name begins with "/X:" or "\\?\", where "X" is any + ** alphabetic character, discard the initial "/" from the pathname. + */ + if( zRelative[0]=='/' && (winIsDriveLetterAndColon(zRelative+1) + || winIsLongPathPrefix(zRelative+1)) ){ + zRelative++; + } -#if defined(__CYGWIN__) SimulateIOError( return SQLITE_ERROR ); - UNUSED_PARAMETER(nFull); - assert( nFull>=pVfs->mxPathname ); - if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){ - /* - ** NOTE: We are dealing with a relative path name and the data - ** directory has been set. Therefore, use it as the basis - ** for converting the relative path name to an absolute - ** one by prepending the data directory and a slash. - */ - char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 ); - if( !zOut ){ - return SQLITE_IOERR_NOMEM; - } - if( cygwin_conv_path( - (osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A) | - CCP_RELATIVE, zRelative, zOut, pVfs->mxPathname+1)<0 ){ - sqlite3_free(zOut); - return winLogError(SQLITE_CANTOPEN_CONVPATH, (DWORD)errno, - "winFullPathname1", zRelative); - }else{ - char *zUtf8 = winConvertToUtf8Filename(zOut); - if( !zUtf8 ){ - sqlite3_free(zOut); - return SQLITE_IOERR_NOMEM; - } - sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s", - sqlite3_data_directory, winGetDirSep(), zUtf8); - sqlite3_free(zUtf8); - sqlite3_free(zOut); - } - }else{ - char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 ); - if( !zOut ){ - return SQLITE_IOERR_NOMEM; - } - if( cygwin_conv_path( - (osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A), - zRelative, zOut, pVfs->mxPathname+1)<0 ){ - sqlite3_free(zOut); - return winLogError(SQLITE_CANTOPEN_CONVPATH, (DWORD)errno, - "winFullPathname2", zRelative); - }else{ - char *zUtf8 = winConvertToUtf8Filename(zOut); - if( !zUtf8 ){ - sqlite3_free(zOut); - return SQLITE_IOERR_NOMEM; - } - sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zUtf8); - sqlite3_free(zUtf8); - sqlite3_free(zOut); + +#ifdef __CYGWIN__ + if( osGetcwd ){ + zFull[nFull-1] = '\0'; + if( !winIsDriveLetterAndColon(zRelative) || !winIsDirSep(zRelative[2]) ){ + int rc = SQLITE_OK; + int nLink = 1; /* Number of symbolic links followed so far */ + const char *zIn = zRelative; /* Input path for each iteration of loop */ + char *zDel = 0; + struct stat buf; + + UNUSED_PARAMETER(pVfs); + + do { + /* Call lstat() on path zIn. Set bLink to true if the path is a symbolic + ** link, or false otherwise. */ + int bLink = 0; + if( osLstat && osReadlink ) { + if( osLstat(zIn, &buf)!=0 ){ + int myErrno = osErrno; + if( myErrno!=ENOENT ){ + rc = winLogError(SQLITE_CANTOPEN_BKPT, (DWORD)myErrno, "lstat", zIn); + } + }else{ + bLink = ((buf.st_mode & 0170000) == 0120000); + } + + if( bLink ){ + if( zDel==0 ){ + zDel = sqlite3MallocZero(nFull); + if( zDel==0 ) rc = SQLITE_NOMEM; + }else if( ++nLink>SQLITE_MAX_SYMLINKS ){ + rc = SQLITE_CANTOPEN_BKPT; + } + + if( rc==SQLITE_OK ){ + nByte = osReadlink(zIn, zDel, nFull-1); + if( nByte ==(DWORD)-1 ){ + rc = winLogError(SQLITE_CANTOPEN_BKPT, (DWORD)osErrno, "readlink", zIn); + }else{ + if( zDel[0]!='/' ){ + int n; + for(n = sqlite3Strlen30(zIn); n>0 && zIn[n-1]!='/'; n--); + if( nByte+n+1>nFull ){ + rc = SQLITE_CANTOPEN_BKPT; + }else{ + memmove(&zDel[n], zDel, nByte+1); + memcpy(zDel, zIn, n); + nByte += n; + } + } + zDel[nByte] = '\0'; + } + } + + zIn = zDel; + } + } + + assert( rc!=SQLITE_OK || zIn!=zFull || zIn[0]=='/' ); + if( rc==SQLITE_OK && zIn!=zFull ){ + rc = mkFullPathname(zIn, zFull, nFull); + } + if( bLink==0 ) break; + zIn = zFull; + }while( rc==SQLITE_OK ); + + sqlite3_free(zDel); + winSimplifyName(zFull); + return rc; } } - return SQLITE_OK; -#endif +#endif /* __CYGWIN__ */ -#if (SQLITE_OS_WINCE || SQLITE_OS_WINRT) && !defined(__CYGWIN__) +#if (SQLITE_OS_WINCE || SQLITE_OS_WINRT) && defined(_WIN32) SimulateIOError( return SQLITE_ERROR ); /* WinCE has no concept of a relative pathname, or so I am told. */ /* WinRT has no way to convert a relative path to an absolute one. */ @@ -5225,18 +6277,8 @@ static int winFullPathname( return SQLITE_OK; #endif -#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(__CYGWIN__) - DWORD nByte; - void *zConverted; - char *zOut; - - /* If this path name begins with "/X:", where "X" is any alphabetic - ** character, discard the initial "/" from the pathname. - */ - if( zRelative[0]=='/' && winIsDriveLetterAndColon(zRelative+1) ){ - zRelative++; - } - +#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT +#if defined(_WIN32) /* It's odd to simulate an io-error here, but really this is just ** using the io-error infrastructure to test that SQLite handles this ** function failing. This function could fail if, for example, the @@ -5254,9 +6296,10 @@ static int winFullPathname( sqlite3_data_directory, winGetDirSep(), zRelative); return SQLITE_OK; } +#endif zConverted = winConvertFromUtf8Filename(zRelative); if( zConverted==0 ){ - return SQLITE_IOERR_NOMEM; + return SQLITE_IOERR_NOMEM_BKPT; } if( osIsNT() ){ LPWSTR zTemp; @@ -5270,7 +6313,7 @@ static int winFullPathname( zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) ); if( zTemp==0 ){ sqlite3_free(zConverted); - return SQLITE_IOERR_NOMEM; + return SQLITE_IOERR_NOMEM_BKPT; } nByte = osGetFullPathNameW((LPCWSTR)zConverted, nByte, zTemp, 0); if( nByte==0 ){ @@ -5292,13 +6335,12 @@ static int winFullPathname( return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(), "winFullPathname3", zRelative); } - nByte += 3; - zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) ); + zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) + 3*sizeof(zTemp[0]) ); if( zTemp==0 ){ sqlite3_free(zConverted); - return SQLITE_IOERR_NOMEM; + return SQLITE_IOERR_NOMEM_BKPT; } - nByte = osGetFullPathNameA((char*)zConverted, nByte, zTemp, 0); + nByte = osGetFullPathNameA((char*)zConverted, nByte+3, zTemp, 0); if( nByte==0 ){ sqlite3_free(zConverted); sqlite3_free(zTemp); @@ -5306,19 +6348,52 @@ static int winFullPathname( "winFullPathname4", zRelative); } sqlite3_free(zConverted); - zOut = sqlite3_win32_mbcs_to_utf8(zTemp); + zOut = winMbcsToUtf8(zTemp, osAreFileApisANSI()); sqlite3_free(zTemp); } #endif if( zOut ){ +#ifdef __CYGWIN__ + if( memcmp(zOut, "\\\\?\\", 4) ){ + sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zOut); + }else if( memcmp(zOut+4, "UNC\\", 4) ){ + sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zOut+4); + }else{ + char *p = zOut+6; + *p = '\\'; + if( osGetcwd ){ + /* On Cygwin, UNC paths use forward slashes */ + while( *p ){ + if( *p=='\\' ) *p = '/'; + ++p; + } + } + sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zOut+6); + } +#else sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zOut); +#endif /* __CYGWIN__ */ sqlite3_free(zOut); return SQLITE_OK; }else{ - return SQLITE_IOERR_NOMEM; + return SQLITE_IOERR_NOMEM_BKPT; } #endif } +static int winFullPathname( + sqlite3_vfs *pVfs, /* Pointer to vfs object */ + const char *zRelative, /* Possibly relative input path */ + int nFull, /* Size of output buffer in bytes */ + char *zFull /* Output buffer */ +){ + int rc; + MUTEX_LOGIC( sqlite3_mutex *pMutex; ) + MUTEX_LOGIC( pMutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR); ) + sqlite3_mutex_enter(pMutex); + rc = winFullPathnameNoMutex(pVfs, zRelative, nFull, zFull); + sqlite3_mutex_leave(pMutex); + return rc; +} #ifndef SQLITE_OMIT_LOAD_EXTENSION /* @@ -5327,25 +6402,8 @@ static int winFullPathname( */ static void *winDlOpen(sqlite3_vfs *pVfs, const char *zFilename){ HANDLE h; -#if defined(__CYGWIN__) - int nFull = pVfs->mxPathname+1; - char *zFull = sqlite3MallocZero( nFull ); - void *zConverted = 0; - if( zFull==0 ){ - OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0)); - return 0; - } - if( winFullPathname(pVfs, zFilename, nFull, zFull)!=SQLITE_OK ){ - sqlite3_free(zFull); - OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0)); - return 0; - } - zConverted = winConvertFromUtf8Filename(zFull); - sqlite3_free(zFull); -#else void *zConverted = winConvertFromUtf8Filename(zFilename); UNUSED_PARAMETER(pVfs); -#endif if( zConverted==0 ){ OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0)); return 0; @@ -5390,65 +6448,82 @@ static void winDlClose(sqlite3_vfs *pVfs, void *pHandle){ #define winDlClose 0 #endif +/* State information for the randomness gatherer. */ +typedef struct EntropyGatherer EntropyGatherer; +struct EntropyGatherer { + unsigned char *a; /* Gather entropy into this buffer */ + int na; /* Size of a[] in bytes */ + int i; /* XOR next input into a[i] */ + int nXor; /* Number of XOR operations done */ +}; + +#if !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS) +/* Mix sz bytes of entropy into p. */ +static void xorMemory(EntropyGatherer *p, unsigned char *x, int sz){ + int j, k; + for(j=0, k=p->i; ja[k++] ^= x[j]; + if( k>=p->na ) k = 0; + } + p->i = k; + p->nXor += sz; +} +#endif /* !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS) */ /* ** Write up to nBuf bytes of randomness into zBuf. */ static int winRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){ - int n = 0; - UNUSED_PARAMETER(pVfs); #if defined(SQLITE_TEST) || defined(SQLITE_OMIT_RANDOMNESS) - n = nBuf; + UNUSED_PARAMETER(pVfs); memset(zBuf, 0, nBuf); + return nBuf; #else - if( sizeof(SYSTEMTIME)<=nBuf-n ){ + EntropyGatherer e; + UNUSED_PARAMETER(pVfs); + memset(zBuf, 0, nBuf); + e.a = (unsigned char*)zBuf; + e.na = nBuf; + e.nXor = 0; + e.i = 0; + { SYSTEMTIME x; osGetSystemTime(&x); - memcpy(&zBuf[n], &x, sizeof(x)); - n += sizeof(x); + xorMemory(&e, (unsigned char*)&x, sizeof(SYSTEMTIME)); } - if( sizeof(DWORD)<=nBuf-n ){ + { DWORD pid = osGetCurrentProcessId(); - memcpy(&zBuf[n], &pid, sizeof(pid)); - n += sizeof(pid); + xorMemory(&e, (unsigned char*)&pid, sizeof(DWORD)); } #if SQLITE_OS_WINRT - if( sizeof(ULONGLONG)<=nBuf-n ){ + { ULONGLONG cnt = osGetTickCount64(); - memcpy(&zBuf[n], &cnt, sizeof(cnt)); - n += sizeof(cnt); + xorMemory(&e, (unsigned char*)&cnt, sizeof(ULONGLONG)); } #else - if( sizeof(DWORD)<=nBuf-n ){ + { DWORD cnt = osGetTickCount(); - memcpy(&zBuf[n], &cnt, sizeof(cnt)); - n += sizeof(cnt); + xorMemory(&e, (unsigned char*)&cnt, sizeof(DWORD)); } -#endif - if( sizeof(LARGE_INTEGER)<=nBuf-n ){ +#endif /* SQLITE_OS_WINRT */ + { LARGE_INTEGER i; osQueryPerformanceCounter(&i); - memcpy(&zBuf[n], &i, sizeof(i)); - n += sizeof(i); + xorMemory(&e, (unsigned char*)&i, sizeof(LARGE_INTEGER)); } #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID - if( sizeof(UUID)<=nBuf-n ){ + { UUID id; memset(&id, 0, sizeof(UUID)); osUuidCreate(&id); - memcpy(&zBuf[n], &id, sizeof(UUID)); - n += sizeof(UUID); - } - if( sizeof(UUID)<=nBuf-n ){ - UUID id; + xorMemory(&e, (unsigned char*)&id, sizeof(UUID)); memset(&id, 0, sizeof(UUID)); osUuidCreateSequential(&id); - memcpy(&zBuf[n], &id, sizeof(UUID)); - n += sizeof(UUID); + xorMemory(&e, (unsigned char*)&id, sizeof(UUID)); } -#endif -#endif /* defined(SQLITE_TEST) || defined(SQLITE_ZERO_PRNG_SEED) */ - return n; +#endif /* !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID */ + return e.nXor>nBuf ? nBuf : e.nXor; +#endif /* defined(SQLITE_TEST) || defined(SQLITE_OMIT_RANDOMNESS) */ } @@ -5564,8 +6639,10 @@ static int winCurrentTime(sqlite3_vfs *pVfs, double *prNow){ ** sqlite3_errmsg(), possibly making IO errors easier to debug. */ static int winGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){ + DWORD e = osGetLastError(); UNUSED_PARAMETER(pVfs); - return winGetLastErrorMsg(osGetLastError(), nBuf, zBuf); + if( nBuf>0 ) winGetLastErrorMsg(e, nBuf, zBuf); + return e; } /* @@ -5573,59 +6650,109 @@ static int winGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){ */ int sqlite3_os_init(void){ static sqlite3_vfs winVfs = { - 3, /* iVersion */ - sizeof(winFile), /* szOsFile */ + 3, /* iVersion */ + sizeof(winFile), /* szOsFile */ SQLITE_WIN32_MAX_PATH_BYTES, /* mxPathname */ - 0, /* pNext */ - "win32", /* zName */ - 0, /* pAppData */ - winOpen, /* xOpen */ - winDelete, /* xDelete */ - winAccess, /* xAccess */ - winFullPathname, /* xFullPathname */ - winDlOpen, /* xDlOpen */ - winDlError, /* xDlError */ - winDlSym, /* xDlSym */ - winDlClose, /* xDlClose */ - winRandomness, /* xRandomness */ - winSleep, /* xSleep */ - winCurrentTime, /* xCurrentTime */ - winGetLastError, /* xGetLastError */ - winCurrentTimeInt64, /* xCurrentTimeInt64 */ - winSetSystemCall, /* xSetSystemCall */ - winGetSystemCall, /* xGetSystemCall */ - winNextSystemCall, /* xNextSystemCall */ + 0, /* pNext */ + "win32", /* zName */ + &winAppData, /* pAppData */ + winOpen, /* xOpen */ + winDelete, /* xDelete */ + winAccess, /* xAccess */ + winFullPathname, /* xFullPathname */ + winDlOpen, /* xDlOpen */ + winDlError, /* xDlError */ + winDlSym, /* xDlSym */ + winDlClose, /* xDlClose */ + winRandomness, /* xRandomness */ + winSleep, /* xSleep */ + winCurrentTime, /* xCurrentTime */ + winGetLastError, /* xGetLastError */ + winCurrentTimeInt64, /* xCurrentTimeInt64 */ + winSetSystemCall, /* xSetSystemCall */ + winGetSystemCall, /* xGetSystemCall */ + winNextSystemCall, /* xNextSystemCall */ }; #if defined(SQLITE_WIN32_HAS_WIDE) static sqlite3_vfs winLongPathVfs = { - 3, /* iVersion */ - sizeof(winFile), /* szOsFile */ + 3, /* iVersion */ + sizeof(winFile), /* szOsFile */ + SQLITE_WINNT_MAX_PATH_BYTES, /* mxPathname */ + 0, /* pNext */ + "win32-longpath", /* zName */ + &winAppData, /* pAppData */ + winOpen, /* xOpen */ + winDelete, /* xDelete */ + winAccess, /* xAccess */ + winFullPathname, /* xFullPathname */ + winDlOpen, /* xDlOpen */ + winDlError, /* xDlError */ + winDlSym, /* xDlSym */ + winDlClose, /* xDlClose */ + winRandomness, /* xRandomness */ + winSleep, /* xSleep */ + winCurrentTime, /* xCurrentTime */ + winGetLastError, /* xGetLastError */ + winCurrentTimeInt64, /* xCurrentTimeInt64 */ + winSetSystemCall, /* xSetSystemCall */ + winGetSystemCall, /* xGetSystemCall */ + winNextSystemCall, /* xNextSystemCall */ + }; +#endif + static sqlite3_vfs winNolockVfs = { + 3, /* iVersion */ + sizeof(winFile), /* szOsFile */ + SQLITE_WIN32_MAX_PATH_BYTES, /* mxPathname */ + 0, /* pNext */ + "win32-none", /* zName */ + &winNolockAppData, /* pAppData */ + winOpen, /* xOpen */ + winDelete, /* xDelete */ + winAccess, /* xAccess */ + winFullPathname, /* xFullPathname */ + winDlOpen, /* xDlOpen */ + winDlError, /* xDlError */ + winDlSym, /* xDlSym */ + winDlClose, /* xDlClose */ + winRandomness, /* xRandomness */ + winSleep, /* xSleep */ + winCurrentTime, /* xCurrentTime */ + winGetLastError, /* xGetLastError */ + winCurrentTimeInt64, /* xCurrentTimeInt64 */ + winSetSystemCall, /* xSetSystemCall */ + winGetSystemCall, /* xGetSystemCall */ + winNextSystemCall, /* xNextSystemCall */ + }; +#if defined(SQLITE_WIN32_HAS_WIDE) + static sqlite3_vfs winLongPathNolockVfs = { + 3, /* iVersion */ + sizeof(winFile), /* szOsFile */ SQLITE_WINNT_MAX_PATH_BYTES, /* mxPathname */ - 0, /* pNext */ - "win32-longpath", /* zName */ - 0, /* pAppData */ - winOpen, /* xOpen */ - winDelete, /* xDelete */ - winAccess, /* xAccess */ - winFullPathname, /* xFullPathname */ - winDlOpen, /* xDlOpen */ - winDlError, /* xDlError */ - winDlSym, /* xDlSym */ - winDlClose, /* xDlClose */ - winRandomness, /* xRandomness */ - winSleep, /* xSleep */ - winCurrentTime, /* xCurrentTime */ - winGetLastError, /* xGetLastError */ - winCurrentTimeInt64, /* xCurrentTimeInt64 */ - winSetSystemCall, /* xSetSystemCall */ - winGetSystemCall, /* xGetSystemCall */ - winNextSystemCall, /* xNextSystemCall */ + 0, /* pNext */ + "win32-longpath-none", /* zName */ + &winNolockAppData, /* pAppData */ + winOpen, /* xOpen */ + winDelete, /* xDelete */ + winAccess, /* xAccess */ + winFullPathname, /* xFullPathname */ + winDlOpen, /* xDlOpen */ + winDlError, /* xDlError */ + winDlSym, /* xDlSym */ + winDlClose, /* xDlClose */ + winRandomness, /* xRandomness */ + winSleep, /* xSleep */ + winCurrentTime, /* xCurrentTime */ + winGetLastError, /* xGetLastError */ + winCurrentTimeInt64, /* xCurrentTimeInt64 */ + winSetSystemCall, /* xSetSystemCall */ + winGetSystemCall, /* xGetSystemCall */ + winNextSystemCall, /* xNextSystemCall */ }; #endif /* Double-check that the aSyscall[] array has been constructed ** correctly. See ticket [bb3a86e890c8e96ab] */ - assert( ArraySize(aSyscall)==80 ); + assert( ArraySize(aSyscall)==89 ); /* get memory map allocation granularity */ memset(&winSysInfo, 0, sizeof(SYSTEM_INFO)); @@ -5643,6 +6770,16 @@ int sqlite3_os_init(void){ sqlite3_vfs_register(&winLongPathVfs, 0); #endif + sqlite3_vfs_register(&winNolockVfs, 0); + +#if defined(SQLITE_WIN32_HAS_WIDE) + sqlite3_vfs_register(&winLongPathNolockVfs, 0); +#endif + +#ifndef SQLITE_OMIT_WAL + winBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1); +#endif + return SQLITE_OK; } @@ -5653,6 +6790,11 @@ int sqlite3_os_end(void){ sleepObj = NULL; } #endif + +#ifndef SQLITE_OMIT_WAL + winBigLock = 0; +#endif + return SQLITE_OK; } diff --git a/src/os_win.h b/src/os_win.h index 17d6a2bef4..a0845f0038 100644 --- a/src/os_win.h +++ b/src/os_win.h @@ -12,8 +12,8 @@ ** ** This file contains code that is specific to Windows. */ -#ifndef _OS_WIN_H_ -#define _OS_WIN_H_ +#ifndef SQLITE_OS_WIN_H +#define SQLITE_OS_WIN_H /* ** Include the primary Windows SDK header file. @@ -22,6 +22,8 @@ #ifdef __CYGWIN__ # include +# include /* amalgamator: dontcache */ +# include /* amalgamator: dontcache */ # include /* amalgamator: dontcache */ #endif @@ -85,4 +87,4 @@ # define SQLITE_OS_WIN_THREADS 0 #endif -#endif /* _OS_WIN_H_ */ +#endif /* SQLITE_OS_WIN_H */ diff --git a/src/pager.c b/src/pager.c index 918f0206bd..49318fc712 100644 --- a/src/pager.c +++ b/src/pager.c @@ -10,7 +10,7 @@ ** ************************************************************************* ** This is the implementation of the page cache subsystem or "pager". -** +** ** The pager is used to access a database disk file. It implements ** atomic commit and rollback through the use of a journal file that ** is separate from the database file. The pager also implements file @@ -36,60 +36,60 @@ ** ** Definition: A page of the database file is said to be "overwriteable" if ** one or more of the following are true about the page: -** +** ** (a) The original content of the page as it was at the beginning of ** the transaction has been written into the rollback journal and ** synced. -** +** ** (b) The page was a freelist leaf page at the start of the transaction. -** +** ** (c) The page number is greater than the largest page that existed in ** the database file at the start of the transaction. -** +** ** (1) A page of the database file is never overwritten unless one of the ** following are true: -** +** ** (a) The page and all other pages on the same sector are overwriteable. -** +** ** (b) The atomic page write optimization is enabled, and the entire ** transaction other than the update of the transaction sequence ** number consists of a single page change. -** +** ** (2) The content of a page written into the rollback journal exactly matches ** both the content in the database when the rollback journal was written ** and the content in the database at the beginning of the current ** transaction. -** +** ** (3) Writes to the database file are an integer multiple of the page size ** in length and are aligned on a page boundary. -** +** ** (4) Reads from the database file are either aligned on a page boundary and ** an integer multiple of the page size in length or are taken from the ** first 100 bytes of the database file. -** +** ** (5) All writes to the database file are synced prior to the rollback journal ** being deleted, truncated, or zeroed. -** -** (6) If a master journal file is used, then all writes to the database file -** are synced prior to the master journal being deleted. -** +** +** (6) If a super-journal file is used, then all writes to the database file +** are synced prior to the super-journal being deleted. +** ** Definition: Two databases (or the same database at two points it time) ** are said to be "logically equivalent" if they give the same answer to ** all queries. Note in particular the content of freelist leaf ** pages can be changed arbitrarily without affecting the logical equivalence ** of the database. -** +** ** (7) At any time, if any subset, including the empty set and the total set, -** of the unsynced changes to a rollback journal are removed and the +** of the unsynced changes to a rollback journal are removed and the ** journal is rolled back, the resulting database file will be logically ** equivalent to the database file at the beginning of the transaction. -** +** ** (8) When a transaction is rolled back, the xTruncate method of the VFS ** is called to restore the database file to the same size it was at ** the beginning of the transaction. (In some VFSes, the xTruncate ** method is a no-op, but that does not change the fact the SQLite will ** invoke it.) -** +** ** (9) Whenever the database file is modified, at least one bit in the range ** of bytes from 24 through 39 inclusive will be changed prior to releasing ** the EXCLUSIVE lock, thus signaling other connections on the same @@ -122,14 +122,14 @@ int sqlite3PagerTrace=1; /* True to enable tracing */ /* ** The following two macros are used within the PAGERTRACE() macros above -** to print out file-descriptors. +** to print out file-descriptors. ** ** PAGERID() takes a pointer to a Pager struct as its argument. The ** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file ** struct as its argument. */ -#define PAGERID(p) ((int)(p->fd)) -#define FILEHANDLEID(fd) ((int)fd) +#define PAGERID(p) (SQLITE_PTR_TO_INT(p->fd)) +#define FILEHANDLEID(fd) (SQLITE_PTR_TO_INT(fd)) /* ** The Pager.eState variable stores the current 'state' of a pager. A @@ -143,7 +143,7 @@ int sqlite3PagerTrace=1; /* True to enable tracing */ ** | | | ** | V | ** |<-------WRITER_LOCKED------> ERROR -** | | ^ +** | | ^ ** | V | ** |<------WRITER_CACHEMOD-------->| ** | | | @@ -155,7 +155,7 @@ int sqlite3PagerTrace=1; /* True to enable tracing */ ** ** ** List of state transitions and the C [function] that performs each: -** +** ** OPEN -> READER [sqlite3PagerSharedLock] ** READER -> OPEN [pager_unlock] ** @@ -167,7 +167,7 @@ int sqlite3PagerTrace=1; /* True to enable tracing */ ** ** WRITER_*** -> ERROR [pager_error] ** ERROR -> OPEN [pager_unlock] -** +** ** ** OPEN: ** @@ -181,9 +181,9 @@ int sqlite3PagerTrace=1; /* True to enable tracing */ ** ** READER: ** -** In this state all the requirements for reading the database in +** In this state all the requirements for reading the database in ** rollback (non-WAL) mode are met. Unless the pager is (or recently -** was) in exclusive-locking mode, a user-level read transaction is +** was) in exclusive-locking mode, a user-level read transaction is ** open. The database size is known in this state. ** ** A connection running with locking_mode=normal enters this state when @@ -193,28 +193,28 @@ int sqlite3PagerTrace=1; /* True to enable tracing */ ** this state even after the read-transaction is closed. The only way ** a locking_mode=exclusive connection can transition from READER to OPEN ** is via the ERROR state (see below). -** +** ** * A read transaction may be active (but a write-transaction cannot). ** * A SHARED or greater lock is held on the database file. -** * The dbSize variable may be trusted (even if a user-level read +** * The dbSize variable may be trusted (even if a user-level read ** transaction is not active). The dbOrigSize and dbFileSize variables ** may not be trusted at this point. ** * If the database is a WAL database, then the WAL connection is open. -** * Even if a read-transaction is not open, it is guaranteed that +** * Even if a read-transaction is not open, it is guaranteed that ** there is no hot-journal in the file-system. ** ** WRITER_LOCKED: ** ** The pager moves to this state from READER when a write-transaction -** is first opened on the database. In WRITER_LOCKED state, all locks -** required to start a write-transaction are held, but no actual +** is first opened on the database. In WRITER_LOCKED state, all locks +** required to start a write-transaction are held, but no actual ** modifications to the cache or database have taken place. ** -** In rollback mode, a RESERVED or (if the transaction was opened with +** In rollback mode, a RESERVED or (if the transaction was opened with ** BEGIN EXCLUSIVE) EXCLUSIVE lock is obtained on the database file when -** moving to this state, but the journal file is not written to or opened -** to in this state. If the transaction is committed or rolled back while -** in WRITER_LOCKED state, all that is required is to unlock the database +** moving to this state, but the journal file is not written to or opened +** to in this state. If the transaction is committed or rolled back while +** in WRITER_LOCKED state, all that is required is to unlock the database ** file. ** ** IN WAL mode, WalBeginWriteTransaction() is called to lock the log file. @@ -222,7 +222,7 @@ int sqlite3PagerTrace=1; /* True to enable tracing */ ** is made to obtain an EXCLUSIVE lock on the database file. ** ** * A write transaction is active. -** * If the connection is open in rollback-mode, a RESERVED or greater +** * If the connection is open in rollback-mode, a RESERVED or greater ** lock is held on the database file. ** * If the connection is open in WAL-mode, a WAL write transaction ** is open (i.e. sqlite3WalBeginWriteTransaction() has been successfully @@ -241,7 +241,7 @@ int sqlite3PagerTrace=1; /* True to enable tracing */ ** ** * A write transaction is active. ** * A RESERVED or greater lock is held on the database file. -** * The journal file is open and the first header has been written +** * The journal file is open and the first header has been written ** to it, but the header has not been synced to disk. ** * The contents of the page cache have been modified. ** @@ -254,7 +254,7 @@ int sqlite3PagerTrace=1; /* True to enable tracing */ ** ** * A write transaction is active. ** * An EXCLUSIVE or greater lock is held on the database file. -** * The journal file is open and the first header has been written +** * The journal file is open and the first header has been written ** and synced to disk. ** * The contents of the page cache have been modified (and possibly ** written to disk). @@ -266,8 +266,8 @@ int sqlite3PagerTrace=1; /* True to enable tracing */ ** A rollback-mode pager changes to WRITER_FINISHED state from WRITER_DBMOD ** state after the entire transaction has been successfully written into the ** database file. In this state the transaction may be committed simply -** by finalizing the journal file. Once in WRITER_FINISHED state, it is -** not possible to modify the database further. At this point, the upper +** by finalizing the journal file. Once in WRITER_FINISHED state, it is +** not possible to modify the database further. At this point, the upper ** layer must either commit or rollback the transaction. ** ** * A write transaction is active. @@ -275,19 +275,19 @@ int sqlite3PagerTrace=1; /* True to enable tracing */ ** * All writing and syncing of journal and database data has finished. ** If no error occurred, all that remains is to finalize the journal to ** commit the transaction. If an error did occur, the caller will need -** to rollback the transaction. +** to rollback the transaction. ** ** ERROR: ** ** The ERROR state is entered when an IO or disk-full error (including -** SQLITE_IOERR_NOMEM) occurs at a point in the code that makes it -** difficult to be sure that the in-memory pager state (cache contents, +** SQLITE_IOERR_NOMEM) occurs at a point in the code that makes it +** difficult to be sure that the in-memory pager state (cache contents, ** db size etc.) are consistent with the contents of the file-system. ** ** Temporary pager files may enter the ERROR state, but in-memory pagers ** cannot. ** -** For example, if an IO error occurs while performing a rollback, +** For example, if an IO error occurs while performing a rollback, ** the contents of the page-cache may be left in an inconsistent state. ** At this point it would be dangerous to change back to READER state ** (as usually happens after a rollback). Any subsequent readers might @@ -297,13 +297,13 @@ int sqlite3PagerTrace=1; /* True to enable tracing */ ** instead of READER following such an error. ** ** Once it has entered the ERROR state, any attempt to use the pager -** to read or write data returns an error. Eventually, once all +** to read or write data returns an error. Eventually, once all ** outstanding transactions have been abandoned, the pager is able to -** transition back to OPEN state, discarding the contents of the +** transition back to OPEN state, discarding the contents of the ** page-cache and any other in-memory state at the same time. Everything -** is reloaded from disk (and, if necessary, hot-journal rollback peformed) +** is reloaded from disk (and, if necessary, hot-journal rollback performed) ** when a read-transaction is next opened on the pager (transitioning -** the pager into READER state). At that point the system has recovered +** the pager into READER state). At that point the system has recovered ** from the error. ** ** Specifically, the pager jumps into the ERROR state if: @@ -319,21 +319,21 @@ int sqlite3PagerTrace=1; /* True to enable tracing */ ** memory. ** ** In other cases, the error is returned to the b-tree layer. The b-tree -** layer then attempts a rollback operation. If the error condition +** layer then attempts a rollback operation. If the error condition ** persists, the pager enters the ERROR state via condition (1) above. ** ** Condition (3) is necessary because it can be triggered by a read-only ** statement executed within a transaction. In this case, if the error ** code were simply returned to the user, the b-tree layer would not ** automatically attempt a rollback, as it assumes that an error in a -** read-only statement cannot leave the pager in an internally inconsistent +** read-only statement cannot leave the pager in an internally inconsistent ** state. ** ** * The Pager.errCode variable is set to something other than SQLITE_OK. ** * There are one or more outstanding references to pages (after the ** last reference is dropped the pager should move back to OPEN state). ** * The pager is not an in-memory pager. -** +** ** ** Notes: ** @@ -343,7 +343,7 @@ int sqlite3PagerTrace=1; /* True to enable tracing */ ** ** * Normally, a connection open in exclusive mode is never in PAGER_OPEN ** state. There are two exceptions: immediately after exclusive-mode has -** been turned on (and before any read or write transactions are +** been turned on (and before any read or write transactions are ** executed), and when the pager is leaving the "error state". ** ** * See also: assert_pager_state(). @@ -357,7 +357,7 @@ int sqlite3PagerTrace=1; /* True to enable tracing */ #define PAGER_ERROR 6 /* -** The Pager.eLock variable is almost always set to one of the +** The Pager.eLock variable is almost always set to one of the ** following locking-states, according to the lock currently held on ** the database file: NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK. ** This variable is kept up to date as locks are taken and released by @@ -372,20 +372,20 @@ int sqlite3PagerTrace=1; /* True to enable tracing */ ** to a less exclusive (lower) value than the lock that is actually held ** at the system level, but it is never set to a more exclusive value. ** -** This is usually safe. If an xUnlock fails or appears to fail, there may +** This is usually safe. If an xUnlock fails or appears to fail, there may ** be a few redundant xLock() calls or a lock may be held for longer than ** required, but nothing really goes wrong. ** ** The exception is when the database file is unlocked as the pager moves -** from ERROR to OPEN state. At this point there may be a hot-journal file +** from ERROR to OPEN state. At this point there may be a hot-journal file ** in the file-system that needs to be rolled back (as part of an OPEN->SHARED ** transition, by the same pager or any other). If the call to xUnlock() ** fails at this point and the pager is left holding an EXCLUSIVE lock, this ** can confuse the call to xCheckReservedLock() call made later as part ** of hot-journal detection. ** -** xCheckReservedLock() is defined as returning true "if there is a RESERVED -** lock held by this process or any others". So xCheckReservedLock may +** xCheckReservedLock() is defined as returning true "if there is a RESERVED +** lock held by this process or any others". So xCheckReservedLock may ** return true because the caller itself is holding an EXCLUSIVE lock (but ** doesn't know it because of a previous error in xUnlock). If this happens ** a hot-journal may be mistaken for a journal being created by an active @@ -396,12 +396,12 @@ int sqlite3PagerTrace=1; /* True to enable tracing */ ** database in the ERROR state, Pager.eLock is set to UNKNOWN_LOCK. It ** is only changed back to a real locking state after a successful call ** to xLock(EXCLUSIVE). Also, the code to do the OPEN->SHARED state transition -** omits the check for a hot-journal if Pager.eLock is set to UNKNOWN_LOCK +** omits the check for a hot-journal if Pager.eLock is set to UNKNOWN_LOCK ** lock. Instead, it assumes a hot-journal exists and obtains an EXCLUSIVE ** lock on the database file before attempting to roll it back. See function ** PagerSharedLock() for more detail. ** -** Pager.eLock may only be set to UNKNOWN_LOCK when the pager is in +** Pager.eLock may only be set to UNKNOWN_LOCK when the pager is in ** PAGER_OPEN state. */ #define UNKNOWN_LOCK (EXCLUSIVE_LOCK+1) @@ -409,6 +409,7 @@ int sqlite3PagerTrace=1; /* True to enable tracing */ /* ** A macro used for invoking the codec if there is one */ +/* BEGIN SQLCIPHER */ #ifdef SQLITE_HAS_CODEC # define CODEC1(P,D,N,X,E) \ if( P->xCodec && P->xCodec(P->pCodec,D,N,X)==0 ){ E; } @@ -419,28 +420,16 @@ int sqlite3PagerTrace=1; /* True to enable tracing */ # define CODEC1(P,D,N,X,E) /* NO-OP */ # define CODEC2(P,D,N,X,E,O) O=(char*)D #endif +/* END SQLCIPHER */ /* -** The maximum allowed sector size. 64KiB. If the xSectorsize() method +** The maximum allowed sector size. 64KiB. If the xSectorsize() method ** returns a value larger than this, then MAX_SECTOR_SIZE is used instead. ** This could conceivably cause corruption following a power failure on ** such a system. This is currently an undocumented limit. */ #define MAX_SECTOR_SIZE 0x10000 -/* -** If the option SQLITE_EXTRA_DURABLE option is set at compile-time, then -** SQLite will do extra fsync() operations when synchronous==FULL to help -** ensure that transactions are durable across a power failure. Most -** applications are happy as long as transactions are consistent across -** a power failure, and are perfectly willing to lose the last transaction -** in exchange for the extra performance of avoiding directory syncs. -** And so the default SQLITE_EXTRA_DURABLE setting is off. -*/ -#ifndef SQLITE_EXTRA_DURABLE -# define SQLITE_EXTRA_DURABLE 0 -#endif - /* ** An instance of the following structure is allocated for each active @@ -450,7 +439,7 @@ int sqlite3PagerTrace=1; /* True to enable tracing */ ** ** When a savepoint is created, the PagerSavepoint.iHdrOffset field is ** set to 0. If a journal-header is written into the main journal while -** the savepoint is active, then iHdrOffset is set to the byte offset +** the savepoint is active, then iHdrOffset is set to the byte offset ** immediately following the last journal record written into the main ** journal before the journal-header. This is required during savepoint ** rollback (see pagerPlaybackSavepoint()). @@ -462,6 +451,7 @@ struct PagerSavepoint { Bitvec *pInSavepoint; /* Set of pages in this savepoint */ Pgno nOrig; /* Original number of pages in file */ Pgno iSubRec; /* Index of first record in sub-journal */ + int bTruncateOnRelease; /* If stmt journal may be truncated on RELEASE */ #ifndef SQLITE_OMIT_WAL u32 aWalData[WAL_SAVEPOINT_NDATA]; /* WAL savepoint context */ #endif @@ -500,44 +490,44 @@ struct PagerSavepoint { ** ** changeCountDone ** -** This boolean variable is used to make sure that the change-counter -** (the 4-byte header field at byte offset 24 of the database file) is -** not updated more often than necessary. +** This boolean variable is used to make sure that the change-counter +** (the 4-byte header field at byte offset 24 of the database file) is +** not updated more often than necessary. ** -** It is set to true when the change-counter field is updated, which +** It is set to true when the change-counter field is updated, which ** can only happen if an exclusive lock is held on the database file. -** It is cleared (set to false) whenever an exclusive lock is +** It is cleared (set to false) whenever an exclusive lock is ** relinquished on the database file. Each time a transaction is committed, ** The changeCountDone flag is inspected. If it is true, the work of ** updating the change-counter is omitted for the current transaction. ** -** This mechanism means that when running in exclusive mode, a connection +** This mechanism means that when running in exclusive mode, a connection ** need only update the change-counter once, for the first transaction ** committed. ** -** setMaster +** setSuper ** ** When PagerCommitPhaseOne() is called to commit a transaction, it may -** (or may not) specify a master-journal name to be written into the +** (or may not) specify a super-journal name to be written into the ** journal file before it is synced to disk. ** -** Whether or not a journal file contains a master-journal pointer affects -** the way in which the journal file is finalized after the transaction is +** Whether or not a journal file contains a super-journal pointer affects +** the way in which the journal file is finalized after the transaction is ** committed or rolled back when running in "journal_mode=PERSIST" mode. -** If a journal file does not contain a master-journal pointer, it is +** If a journal file does not contain a super-journal pointer, it is ** finalized by overwriting the first journal header with zeroes. If -** it does contain a master-journal pointer the journal file is finalized -** by truncating it to zero bytes, just as if the connection were +** it does contain a super-journal pointer the journal file is finalized +** by truncating it to zero bytes, just as if the connection were ** running in "journal_mode=truncate" mode. ** -** Journal files that contain master journal pointers cannot be finalized +** Journal files that contain super-journal pointers cannot be finalized ** simply by overwriting the first journal-header with zeroes, as the -** master journal pointer could interfere with hot-journal rollback of any +** super-journal pointer could interfere with hot-journal rollback of any ** subsequently interrupted transaction that reuses the journal file. ** ** The flag is cleared as soon as the journal file is finalized (either ** by PagerCommitPhaseTwo or PagerRollback). If an IO error prevents the -** journal file from being successfully finalized, the setMaster flag +** journal file from being successfully finalized, the setSuper flag ** is cleared anyway (and the pager will move to ERROR state). ** ** doNotSpill @@ -553,12 +543,12 @@ struct PagerSavepoint { ** to allocate a new page to prevent the journal file from being written ** while it is being traversed by code in pager_playback(). The SPILLFLAG_OFF ** case is a user preference. -** +** ** If the SPILLFLAG_NOSYNC bit is set, writing to the database from ** pagerStress() is permitted, but syncing the journal file is not. ** This flag is set by sqlite3PagerWrite() when the file-system sector-size ** is larger than the database page-size in order to prevent a journal sync -** from happening in between the journalling of two pages on the same sector. +** from happening in between the journalling of two pages on the same sector. ** ** subjInMemory ** @@ -566,16 +556,16 @@ struct PagerSavepoint { ** is opened as an in-memory journal file. If false, then in-memory ** sub-journals are only used for in-memory pager files. ** -** This variable is updated by the upper layer each time a new +** This variable is updated by the upper layer each time a new ** write-transaction is opened. ** ** dbSize, dbOrigSize, dbFileSize ** ** Variable dbSize is set to the number of pages in the database file. ** It is valid in PAGER_READER and higher states (all states except for -** OPEN and ERROR). +** OPEN and ERROR). ** -** dbSize is set based on the size of the database file, which may be +** dbSize is set based on the size of the database file, which may be ** larger than the size of the database (the value stored at offset ** 28 of the database header by the btree). If the size of the file ** is not an integer multiple of the page-size, the value stored in @@ -586,10 +576,10 @@ struct PagerSavepoint { ** ** During a write-transaction, if pages with page-numbers greater than ** dbSize are modified in the cache, dbSize is updated accordingly. -** Similarly, if the database is truncated using PagerTruncateImage(), +** Similarly, if the database is truncated using PagerTruncateImage(), ** dbSize is updated. ** -** Variables dbOrigSize and dbFileSize are valid in states +** Variables dbOrigSize and dbFileSize are valid in states ** PAGER_WRITER_LOCKED and higher. dbOrigSize is a copy of the dbSize ** variable at the start of the transaction. It is used during rollback, ** and to determine whether or not pages need to be journalled before @@ -598,12 +588,12 @@ struct PagerSavepoint { ** Throughout a write-transaction, dbFileSize contains the size of ** the file on disk in pages. It is set to a copy of dbSize when the ** write-transaction is first opened, and updated when VFS calls are made -** to write or truncate the database file on disk. +** to write or truncate the database file on disk. ** -** The only reason the dbFileSize variable is required is to suppress -** unnecessary calls to xTruncate() after committing a transaction. If, -** when a transaction is committed, the dbFileSize variable indicates -** that the database file is larger than the database image (Pager.dbSize), +** The only reason the dbFileSize variable is required is to suppress +** unnecessary calls to xTruncate() after committing a transaction. If, +** when a transaction is committed, the dbFileSize variable indicates +** that the database file is larger than the database image (Pager.dbSize), ** pager_truncate() is called. The pager_truncate() call uses xFilesize() ** to measure the database file on disk, and then truncates it if required. ** dbFileSize is not used when rolling back a transaction. In this case @@ -614,21 +604,33 @@ struct PagerSavepoint { ** dbHintSize ** ** The dbHintSize variable is used to limit the number of calls made to -** the VFS xFileControl(FCNTL_SIZE_HINT) method. +** the VFS xFileControl(FCNTL_SIZE_HINT) method. ** ** dbHintSize is set to a copy of the dbSize variable when a ** write-transaction is opened (at the same time as dbFileSize and ** dbOrigSize). If the xFileControl(FCNTL_SIZE_HINT) method is called, ** dbHintSize is increased to the number of pages that correspond to the -** size-hint passed to the method call. See pager_write_pagelist() for +** size-hint passed to the method call. See pager_write_pagelist() for ** details. ** ** errCode ** ** The Pager.errCode variable is only ever used in PAGER_ERROR state. It -** is set to zero in all other states. In PAGER_ERROR state, Pager.errCode -** is always set to SQLITE_FULL, SQLITE_IOERR or one of the SQLITE_IOERR_XXX +** is set to zero in all other states. In PAGER_ERROR state, Pager.errCode +** is always set to SQLITE_FULL, SQLITE_IOERR or one of the SQLITE_IOERR_XXX ** sub-codes. +** +** syncFlags, walSyncFlags +** +** syncFlags is either SQLITE_SYNC_NORMAL (0x02) or SQLITE_SYNC_FULL (0x03). +** syncFlags is used for rollback mode. walSyncFlags is used for WAL mode +** and contains the flags used to sync the checkpoint operations in the +** lower two bits, and sync flags used for transaction commits in the WAL +** file in bits 0x04 and 0x08. In other words, to get the correct sync flags +** for checkpoint operations, use (walSyncFlags&0x03) and to get the correct +** sync flags for transaction commit, use ((walSyncFlags>>2)&0x03). Note +** that with synchronous=NORMAL in WAL mode, transaction commit is not synced +** meaning that the 0x04 and 0x08 bits are both zero. */ struct Pager { sqlite3_vfs *pVfs; /* OS functions to use for IO */ @@ -638,13 +640,13 @@ struct Pager { u8 noSync; /* Do not sync the journal if true */ u8 fullSync; /* Do extra syncs of the journal for robustness */ u8 extraSync; /* sync directory after journal delete */ - u8 ckptSyncFlags; /* SYNC_NORMAL or SYNC_FULL for checkpoint */ - u8 walSyncFlags; /* SYNC_NORMAL or SYNC_FULL for wal writes */ u8 syncFlags; /* SYNC_NORMAL or SYNC_FULL otherwise */ + u8 walSyncFlags; /* See description above */ u8 tempFile; /* zFilename is a temporary or immutable file */ u8 noLock; /* Do not lock (except in WAL mode) */ u8 readOnly; /* True for a read-only database */ u8 memDb; /* True to inhibit all file I/O */ + u8 memVfs; /* VFS-implemented memory database */ /************************************************************************** ** The following block contains those class members that change during @@ -658,7 +660,7 @@ struct Pager { u8 eState; /* Pager state (OPEN, READER, WRITER_LOCKED..) */ u8 eLock; /* Current lock held on database file */ u8 changeCountDone; /* Set after incrementing the change-counter */ - u8 setMaster; /* True if a m-j name has been written to jrnl */ + u8 setSuper; /* Super-jrnl name is written into jrnl */ u8 doNotSpill; /* Do not spill the cache when non-zero */ u8 subjInMemory; /* True to use in-memory sub-journals */ u8 bUseFetch; /* True to use xFetch() */ @@ -694,40 +696,48 @@ struct Pager { i16 nReserve; /* Number of unused bytes at end of each page */ u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */ u32 sectorSize; /* Assumed sector size during rollback */ - int pageSize; /* Number of bytes in a page */ Pgno mxPgno; /* Maximum allowed size of the database */ + Pgno lckPgno; /* Page number for the locking page */ + i64 pageSize; /* Number of bytes in a page */ i64 journalSizeLimit; /* Size limit for persistent journal files */ char *zFilename; /* Name of the database file */ char *zJournal; /* Name of the journal file */ int (*xBusyHandler)(void*); /* Function to call when busy */ void *pBusyHandlerArg; /* Context argument for xBusyHandler */ - int aStat[3]; /* Total cache hits, misses and writes */ + u32 aStat[4]; /* Total cache hits, misses, writes, spills */ #ifdef SQLITE_TEST int nRead; /* Database pages read */ #endif void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */ + int (*xGet)(Pager*,Pgno,DbPage**,int); /* Routine to fetch a patch */ +/* BEGIN SQLCIPHER */ #ifdef SQLITE_HAS_CODEC void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */ void (*xCodecSizeChng)(void*,int,int); /* Notify of page size changes */ void (*xCodecFree)(void*); /* Destructor for the codec */ void *pCodec; /* First argument to xCodec... methods */ #endif +/* END SQLCIPHER */ char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */ PCache *pPCache; /* Pointer to page cache object */ #ifndef SQLITE_OMIT_WAL Wal *pWal; /* Write-ahead log used by "journal_mode=wal" */ char *zWal; /* File name for write-ahead log */ #endif +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT + sqlite3 *dbWal; +#endif }; /* ** Indexes for use with Pager.aStat[]. The Pager.aStat[] array contains -** the values accessed by passing SQLITE_DBSTATUS_CACHE_HIT, CACHE_MISS +** the values accessed by passing SQLITE_DBSTATUS_CACHE_HIT, CACHE_MISS ** or CACHE_WRITE to sqlite3_db_status(). */ #define PAGER_STAT_HIT 0 #define PAGER_STAT_MISS 1 #define PAGER_STAT_WRITE 2 +#define PAGER_STAT_SPILL 3 /* ** The following global variables hold counters used for @@ -779,7 +789,7 @@ static const unsigned char aJournalMagic[] = { #define JOURNAL_PG_SZ(pPager) ((pPager->pageSize) + 8) /* -** The journal header size for this pager. This is usually the same +** The journal header size for this pager. This is usually the same ** size as a single disk sector. See also setSectorSize(). */ #define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize) @@ -806,33 +816,44 @@ static const unsigned char aJournalMagic[] = { # define USEFETCH(x) 0 #endif +#ifdef SQLITE_DIRECT_OVERFLOW_READ /* -** The maximum legal page number is (2^31 - 1). -*/ -#define PAGER_MAX_PGNO 2147483647 - -/* -** The argument to this macro is a file descriptor (type sqlite3_file*). -** Return 0 if it is not open, or non-zero (but not 1) if it is. -** -** This is so that expressions can be written as: -** -** if( isOpen(pPager->jfd) ){ ... +** Return true if page pgno can be read directly from the database file +** by the b-tree layer. This is the case if: ** -** instead of -** -** if( pPager->jfd->pMethods ){ ... -*/ -#define isOpen(pFd) ((pFd)->pMethods!=0) - -/* -** Return true if this pager uses a write-ahead log instead of the usual -** rollback journal. Otherwise false. +** (1) the database file is open +** (2) the VFS for the database is able to do unaligned sub-page reads +** (3) there are no dirty pages in the cache, and +** (4) the desired page is not currently in the wal file. */ +int sqlite3PagerDirectReadOk(Pager *pPager, Pgno pgno){ + assert( pPager!=0 ); + assert( pPager->fd!=0 ); + if( pPager->fd->pMethods==0 ) return 0; /* Case (1) */ + if( sqlite3PCacheIsDirty(pPager->pPCache) ) return 0; /* Failed (3) */ +/* BEGIN SQLCIPHER */ +#ifdef SQLITE_HAS_CODEC + if( pPager->xCodec!=0 ) return 0; +#endif +/* END SQLCIPHER */ #ifndef SQLITE_OMIT_WAL -static int pagerUseWal(Pager *pPager){ - return (pPager->pWal!=0); + if( pPager->pWal ){ + u32 iRead = 0; + (void)sqlite3WalFindFrame(pPager->pWal, pgno, &iRead); + if( iRead ) return 0; /* Case (4) */ + } +#endif + assert( pPager->fd->pMethods->xDeviceCharacteristics!=0 ); + if( (pPager->fd->pMethods->xDeviceCharacteristics(pPager->fd) + & SQLITE_IOCAP_SUBPAGE_READ)==0 ){ + return 0; /* Case (2) */ + } + return 1; } +#endif + +#ifndef SQLITE_OMIT_WAL +# define pagerUseWal(x) ((x)->pWal!=0) #else # define pagerUseWal(x) 0 # define pagerRollbackWal(x) 0 @@ -841,7 +862,7 @@ static int pagerUseWal(Pager *pPager){ # define pagerBeginReadTransaction(z) SQLITE_OK #endif -#ifndef NDEBUG +#ifndef NDEBUG /* ** Usage: ** @@ -870,24 +891,25 @@ static int assert_pager_state(Pager *p){ assert( p->tempFile==0 || p->eLock==EXCLUSIVE_LOCK ); assert( p->tempFile==0 || pPager->changeCountDone ); - /* If the useJournal flag is clear, the journal-mode must be "OFF". + /* If the useJournal flag is clear, the journal-mode must be "OFF". ** And if the journal-mode is "OFF", the journal file must not be open. */ assert( p->journalMode==PAGER_JOURNALMODE_OFF || p->useJournal ); assert( p->journalMode!=PAGER_JOURNALMODE_OFF || !isOpen(p->jfd) ); - /* Check that MEMDB implies noSync. And an in-memory journal. Since - ** this means an in-memory pager performs no IO at all, it cannot encounter - ** either SQLITE_IOERR or SQLITE_FULL during rollback or while finalizing - ** a journal file. (although the in-memory journal implementation may - ** return SQLITE_IOERR_NOMEM while the journal file is being written). It - ** is therefore not possible for an in-memory pager to enter the ERROR + /* Check that MEMDB implies noSync. And an in-memory journal. Since + ** this means an in-memory pager performs no IO at all, it cannot encounter + ** either SQLITE_IOERR or SQLITE_FULL during rollback or while finalizing + ** a journal file. (although the in-memory journal implementation may + ** return SQLITE_IOERR_NOMEM while the journal file is being written). It + ** is therefore not possible for an in-memory pager to enter the ERROR ** state. */ if( MEMDB ){ + assert( !isOpen(p->fd) ); assert( p->noSync ); - assert( p->journalMode==PAGER_JOURNALMODE_OFF - || p->journalMode==PAGER_JOURNALMODE_MEMORY + assert( p->journalMode==PAGER_JOURNALMODE_OFF + || p->journalMode==PAGER_JOURNALMODE_MEMORY ); assert( p->eState!=PAGER_ERROR && p->eState!=PAGER_OPEN ); assert( pagerUseWal(p)==0 ); @@ -921,7 +943,7 @@ static int assert_pager_state(Pager *p){ assert( pPager->dbSize==pPager->dbOrigSize ); assert( pPager->dbOrigSize==pPager->dbFileSize ); assert( pPager->dbOrigSize==pPager->dbHintSize ); - assert( pPager->setMaster==0 ); + assert( pPager->setSuper==0 ); break; case PAGER_WRITER_CACHEMOD: @@ -934,9 +956,9 @@ static int assert_pager_state(Pager *p){ ** to journal_mode=wal. */ assert( p->eLock>=RESERVED_LOCK ); - assert( isOpen(p->jfd) - || p->journalMode==PAGER_JOURNALMODE_OFF - || p->journalMode==PAGER_JOURNALMODE_WAL + assert( isOpen(p->jfd) + || p->journalMode==PAGER_JOURNALMODE_OFF + || p->journalMode==PAGER_JOURNALMODE_WAL ); } assert( pPager->dbOrigSize==pPager->dbFileSize ); @@ -948,9 +970,10 @@ static int assert_pager_state(Pager *p){ assert( pPager->errCode==SQLITE_OK ); assert( !pagerUseWal(pPager) ); assert( p->eLock>=EXCLUSIVE_LOCK ); - assert( isOpen(p->jfd) - || p->journalMode==PAGER_JOURNALMODE_OFF - || p->journalMode==PAGER_JOURNALMODE_WAL + assert( isOpen(p->jfd) + || p->journalMode==PAGER_JOURNALMODE_OFF + || p->journalMode==PAGER_JOURNALMODE_WAL + || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC) ); assert( pPager->dbOrigSize<=pPager->dbHintSize ); break; @@ -959,9 +982,10 @@ static int assert_pager_state(Pager *p){ assert( p->eLock==EXCLUSIVE_LOCK ); assert( pPager->errCode==SQLITE_OK ); assert( !pagerUseWal(pPager) ); - assert( isOpen(p->jfd) - || p->journalMode==PAGER_JOURNALMODE_OFF - || p->journalMode==PAGER_JOURNALMODE_WAL + assert( isOpen(p->jfd) + || p->journalMode==PAGER_JOURNALMODE_OFF + || p->journalMode==PAGER_JOURNALMODE_WAL + || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC) ); break; @@ -971,7 +995,7 @@ static int assert_pager_state(Pager *p){ ** back to OPEN state. */ assert( pPager->errCode!=SQLITE_OK ); - assert( sqlite3PcacheRefCount(pPager->pPCache)>0 ); + assert( sqlite3PcacheRefCount(pPager->pPCache)>0 || pPager->tempFile ); break; } @@ -979,7 +1003,7 @@ static int assert_pager_state(Pager *p){ } #endif /* ifndef NDEBUG */ -#ifdef SQLITE_DEBUG +#ifdef SQLITE_DEBUG /* ** Return a pointer to a human readable string in a static buffer ** containing the state of the Pager object passed as an argument. This @@ -987,8 +1011,12 @@ static int assert_pager_state(Pager *p){ ** to "print *pPager" in gdb: ** ** (gdb) printf "%s", print_pager_state(pPager) +** +** This routine has external linkage in order to suppress compiler warnings +** about an unused function. It is enclosed within SQLITE_DEBUG and so does +** not appear in normal builds. */ -static char *print_pager_state(Pager *p){ +char *print_pager_state(Pager *p){ static char zRet[1024]; sqlite3_snprintf(1024, zRet, @@ -1030,6 +1058,35 @@ static char *print_pager_state(Pager *p){ } #endif +/* Forward references to the various page getters */ +static int getPageNormal(Pager*,Pgno,DbPage**,int); +static int getPageError(Pager*,Pgno,DbPage**,int); +#if SQLITE_MAX_MMAP_SIZE>0 +static int getPageMMap(Pager*,Pgno,DbPage**,int); +#endif + +/* +** Set the Pager.xGet method for the appropriate routine used to fetch +** content from the pager. +*/ +static void setGetterMethod(Pager *pPager){ + if( pPager->errCode ){ + pPager->xGet = getPageError; +#if SQLITE_MAX_MMAP_SIZE>0 + }else if( USEFETCH(pPager) +/* BEGIN SQLCIPHER */ +#ifdef SQLITE_HAS_CODEC + && pPager->xCodec==0 +#endif +/* END SQLCIPHER */ + ){ + pPager->xGet = getPageMMap; +#endif /* SQLITE_MAX_MMAP_SIZE>0 */ + }else{ + pPager->xGet = getPageNormal; + } +} + /* ** Return true if it is necessary to write page *pPg into the sub-journal. ** A page needs to be written into the sub-journal if there exists one @@ -1047,6 +1104,9 @@ static int subjRequiresPage(PgHdr *pPg){ for(i=0; inSavepoint; i++){ p = &pPager->aSavepoint[i]; if( p->nOrig>=pgno && 0==sqlite3BitvecTestNotNull(p->pInSavepoint, pgno) ){ + for(i=i+1; inSavepoint; i++){ + pPager->aSavepoint[i].bTruncateOnRelease = 0; + } return 1; } } @@ -1100,7 +1160,7 @@ static int write32bits(sqlite3_file *fd, i64 offset, u32 val){ ** succeeds, set the Pager.eLock variable to match the (attempted) new lock. ** ** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is -** called, do not modify it. See the comment above the #define of +** called, do not modify it. See the comment above the #define of ** UNKNOWN_LOCK for an explanation of this. */ static int pagerUnlockDb(Pager *pPager, int eLock){ @@ -1117,17 +1177,18 @@ static int pagerUnlockDb(Pager *pPager, int eLock){ } IOTRACE(("UNLOCK %p %d\n", pPager, eLock)) } + pPager->changeCountDone = pPager->tempFile; /* ticket fb3b3024ea238d5c */ return rc; } /* ** Lock the database file to level eLock, which must be either SHARED_LOCK, ** RESERVED_LOCK or EXCLUSIVE_LOCK. If the caller is successful, set the -** Pager.eLock variable to the new locking state. +** Pager.eLock variable to the new locking state. ** -** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is -** called, do not modify it unless the new locking state is EXCLUSIVE_LOCK. -** See the comment above the #define of UNKNOWN_LOCK for an explanation +** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is +** called, do not modify it unless the new locking state is EXCLUSIVE_LOCK. +** See the comment above the #define of UNKNOWN_LOCK for an explanation ** of this. */ static int pagerLockDb(Pager *pPager, int eLock){ @@ -1145,34 +1206,47 @@ static int pagerLockDb(Pager *pPager, int eLock){ } /* -** This function determines whether or not the atomic-write optimization -** can be used with this pager. The optimization can be used if: +** This function determines whether or not the atomic-write or +** atomic-batch-write optimizations can be used with this pager. The +** atomic-write optimization can be used if: ** ** (a) the value returned by OsDeviceCharacteristics() indicates that ** a database page may be written atomically, and ** (b) the value returned by OsSectorSize() is less than or equal ** to the page size. ** -** The optimization is also always enabled for temporary files. It is -** an error to call this function if pPager is opened on an in-memory -** database. +** If it can be used, then the value returned is the size of the journal +** file when it contains rollback data for exactly one page. +** +** The atomic-batch-write optimization can be used if OsDeviceCharacteristics() +** returns a value with the SQLITE_IOCAP_BATCH_ATOMIC bit set. -1 is +** returned in this case. ** -** If the optimization cannot be used, 0 is returned. If it can be used, -** then the value returned is the size of the journal file when it -** contains rollback data for exactly one page. +** If neither optimization can be used, 0 is returned. */ -#ifdef SQLITE_ENABLE_ATOMIC_WRITE static int jrnlBufferSize(Pager *pPager){ assert( !MEMDB ); - if( !pPager->tempFile ){ - int dc; /* Device characteristics */ - int nSector; /* Sector size */ - int szPage; /* Page size */ - assert( isOpen(pPager->fd) ); - dc = sqlite3OsDeviceCharacteristics(pPager->fd); - nSector = pPager->sectorSize; - szPage = pPager->pageSize; +#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \ + || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE) + int dc; /* Device characteristics */ + + assert( isOpen(pPager->fd) ); + dc = sqlite3OsDeviceCharacteristics(pPager->fd); +#else + UNUSED_PARAMETER(pPager); +#endif + +#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE + if( pPager->dbSize>0 && (dc&SQLITE_IOCAP_BATCH_ATOMIC) ){ + return -1; + } +#endif + +#ifdef SQLITE_ENABLE_ATOMIC_WRITE + { + int nSector = pPager->sectorSize; + int szPage = pPager->pageSize; assert(SQLITE_IOCAP_ATOMIC512==(512>>8)); assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8)); @@ -1182,9 +1256,11 @@ static int jrnlBufferSize(Pager *pPager){ } return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager); -} #endif + return 0; +} + /* ** If SQLITE_CHECK_PAGES is defined then we do some sanity checking ** on the cache using a hash function. This is used for testing @@ -1230,71 +1306,73 @@ static void checkPage(PgHdr *pPg){ /* ** When this is called the journal file for pager pPager must be open. -** This function attempts to read a master journal file name from the -** end of the file and, if successful, copies it into memory supplied -** by the caller. See comments above writeMasterJournal() for the format -** used to store a master journal file name at the end of a journal file. +** This function attempts to read a super-journal file name from the +** end of the file and, if successful, copies it into memory supplied +** by the caller. See comments above writeSuperJournal() for the format +** used to store a super-journal file name at the end of a journal file. ** -** zMaster must point to a buffer of at least nMaster bytes allocated by +** zSuper must point to a buffer of at least nSuper bytes allocated by ** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is -** enough space to write the master journal name). If the master journal -** name in the journal is longer than nMaster bytes (including a -** nul-terminator), then this is handled as if no master journal name +** enough space to write the super-journal name). If the super-journal +** name in the journal is longer than nSuper bytes (including a +** nul-terminator), then this is handled as if no super-journal name ** were present in the journal. ** -** If a master journal file name is present at the end of the journal -** file, then it is copied into the buffer pointed to by zMaster. A -** nul-terminator byte is appended to the buffer following the master -** journal file name. +** If a super-journal file name is present at the end of the journal +** file, then it is copied into the buffer pointed to by zSuper. A +** nul-terminator byte is appended to the buffer following the +** super-journal file name. ** -** If it is determined that no master journal file name is present -** zMaster[0] is set to 0 and SQLITE_OK returned. +** If it is determined that no super-journal file name is present +** zSuper[0] is set to 0 and SQLITE_OK returned. ** ** If an error occurs while reading from the journal file, an SQLite ** error code is returned. */ -static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, u32 nMaster){ +static int readSuperJournal(sqlite3_file *pJrnl, char *zSuper, u64 nSuper){ int rc; /* Return code */ - u32 len; /* Length in bytes of master journal name */ + u32 len; /* Length in bytes of super-journal name */ i64 szJ; /* Total size in bytes of journal file pJrnl */ u32 cksum; /* MJ checksum value read from journal */ u32 u; /* Unsigned loop counter */ unsigned char aMagic[8]; /* A buffer to hold the magic header */ - zMaster[0] = '\0'; + zSuper[0] = '\0'; if( SQLITE_OK!=(rc = sqlite3OsFileSize(pJrnl, &szJ)) || szJ<16 || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-16, &len)) - || len>=nMaster - || len==0 + || len>=nSuper + || len>szJ-16 + || len==0 || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-12, &cksum)) || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8)) || memcmp(aMagic, aJournalMagic, 8) - || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len)) + || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, zSuper, len, szJ-16-len)) ){ return rc; } - /* See if the checksum matches the master journal name */ + /* See if the checksum matches the super-journal name */ for(u=0; ujournalOff, assuming a sector +** Return the offset of the sector boundary at or immediately +** following the value in pPager->journalOff, assuming a sector ** size of pPager->sectorSize bytes. ** ** i.e for a sector size of 512: @@ -1305,7 +1383,7 @@ static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, u32 nMaster){ ** 512 512 ** 100 512 ** 2000 2048 -** +** */ static i64 journalHdrOffset(Pager *pPager){ i64 offset = 0; @@ -1327,12 +1405,12 @@ static i64 journalHdrOffset(Pager *pPager){ ** ** If doTruncate is non-zero or the Pager.journalSizeLimit variable is ** set to 0, then truncate the journal file to zero bytes in size. Otherwise, -** zero the 28-byte header at the start of the journal file. In either case, -** if the pager is not in no-sync mode, sync the journal file immediately +** zero the 28-byte header at the start of the journal file. In either case, +** if the pager is not in no-sync mode, sync the journal file immediately ** after writing or truncating it. ** ** If Pager.journalSizeLimit is set to a positive, non-zero value, and -** following the truncation or zeroing described above the size of the +** following the truncation or zeroing described above the size of the ** journal file in bytes is larger than this value, then truncate the ** journal file to Pager.journalSizeLimit bytes. The journal file does ** not need to be synced following this operation. @@ -1343,6 +1421,7 @@ static i64 journalHdrOffset(Pager *pPager){ static int zeroJournalHdr(Pager *pPager, int doTruncate){ int rc = SQLITE_OK; /* Return code */ assert( isOpen(pPager->jfd) ); + assert( !sqlite3JournalIsInMemory(pPager->jfd) ); if( pPager->journalOff ){ const i64 iLimit = pPager->journalSizeLimit; /* Local cache of jsl */ @@ -1357,8 +1436,8 @@ static int zeroJournalHdr(Pager *pPager, int doTruncate){ rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY|pPager->syncFlags); } - /* At this point the transaction is committed but the write lock - ** is still held on the file. If there is a size limit configured for + /* At this point the transaction is committed but the write lock + ** is still held on the file. If there is a size limit configured for ** the persistent journal and the journal file currently consumes more ** space than that limit allows for, truncate it now. There is no need ** to sync the file following this operation. @@ -1386,7 +1465,7 @@ static int zeroJournalHdr(Pager *pPager, int doTruncate){ ** - 4 bytes: Initial database page count. ** - 4 bytes: Sector size used by the process that wrote this journal. ** - 4 bytes: Database page size. -** +** ** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space. */ static int writeJournalHdr(Pager *pPager){ @@ -1402,8 +1481,8 @@ static int writeJournalHdr(Pager *pPager){ nHeader = JOURNAL_HDR_SZ(pPager); } - /* If there are active savepoints and any of them were created - ** since the most recent journal header was written, update the + /* If there are active savepoints and any of them were created + ** since the most recent journal header was written, update the ** PagerSavepoint.iHdrOffset fields now. */ for(ii=0; iinSavepoint; ii++){ @@ -1414,10 +1493,10 @@ static int writeJournalHdr(Pager *pPager){ pPager->journalHdr = pPager->journalOff = journalHdrOffset(pPager); - /* + /* ** Write the nRec Field - the number of page records that follow this ** journal header. Normally, zero is written to this value at this time. - ** After the records are added to the journal (and the journal synced, + ** After the records are added to the journal (and the journal synced, ** if in full-sync mode), the zero is overwritten with the true number ** of records (see syncJournal()). ** @@ -1436,7 +1515,7 @@ static int writeJournalHdr(Pager *pPager){ */ assert( isOpen(pPager->fd) || pPager->noSync ); if( pPager->noSync || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY) - || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND) + || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND) ){ memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic)); put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff); @@ -1444,9 +1523,32 @@ static int writeJournalHdr(Pager *pPager){ memset(zHeader, 0, sizeof(aJournalMagic)+4); } - /* The random check-hash initializer */ - sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit); + + + /* The random check-hash initializer */ + if( pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){ + sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit); + } +#ifdef SQLITE_DEBUG + else{ + /* The Pager.cksumInit variable is usually randomized above to protect + ** against there being existing records in the journal file. This is + ** dangerous, as following a crash they may be mistaken for records + ** written by the current transaction and rolled back into the database + ** file, causing corruption. The following assert statements verify + ** that this is not required in "journal_mode=memory" mode, as in that + ** case the journal file is always 0 bytes in size at this point. + ** It is advantageous to avoid the sqlite3_randomness() call if possible + ** as it takes the global PRNG mutex. */ + i64 sz = 0; + sqlite3OsFileSize(pPager->jfd, &sz); + assert( sz==0 ); + assert( pPager->journalOff==journalHdrOffset(pPager) ); + assert( sqlite3JournalIsInMemory(pPager->jfd) ); + } +#endif put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit); + /* The initial database size */ put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbOrigSize); /* The assumed sector size for this process */ @@ -1463,23 +1565,23 @@ static int writeJournalHdr(Pager *pPager){ memset(&zHeader[sizeof(aJournalMagic)+20], 0, nHeader-(sizeof(aJournalMagic)+20)); - /* In theory, it is only necessary to write the 28 bytes that the - ** journal header consumes to the journal file here. Then increment the - ** Pager.journalOff variable by JOURNAL_HDR_SZ so that the next + /* In theory, it is only necessary to write the 28 bytes that the + ** journal header consumes to the journal file here. Then increment the + ** Pager.journalOff variable by JOURNAL_HDR_SZ so that the next ** record is written to the following sector (leaving a gap in the file ** that will be implicitly filled in by the OS). ** - ** However it has been discovered that on some systems this pattern can + ** However it has been discovered that on some systems this pattern can ** be significantly slower than contiguously writing data to the file, - ** even if that means explicitly writing data to the block of + ** even if that means explicitly writing data to the block of ** (JOURNAL_HDR_SZ - 28) bytes that will not be used. So that is what - ** is done. + ** is done. ** - ** The loop is required here in case the sector-size is larger than the + ** The loop is required here in case the sector-size is larger than the ** database page size. Since the zHeader buffer is only Pager.pageSize ** bytes in size, more than one call to sqlite3OsWrite() may be required ** to populate the entire journal header sector. - */ + */ for(nWrite=0; rc==SQLITE_OK&&nWritejournalHdr, nHeader)) rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff); @@ -1577,29 +1679,29 @@ static int readJournalHdr( /* Check that the values read from the page-size and sector-size fields ** are within range. To be 'in range', both values need to be a power - ** of two greater than or equal to 512 or 32, and not greater than their + ** of two greater than or equal to 512 or 32, and not greater than their ** respective compile time maximum limits. */ if( iPageSize<512 || iSectorSize<32 || iPageSize>SQLITE_MAX_PAGE_SIZE || iSectorSize>MAX_SECTOR_SIZE - || ((iPageSize-1)&iPageSize)!=0 || ((iSectorSize-1)&iSectorSize)!=0 + || ((iPageSize-1)&iPageSize)!=0 || ((iSectorSize-1)&iSectorSize)!=0 ){ - /* If the either the page-size or sector-size in the journal-header is - ** invalid, then the process that wrote the journal-header must have - ** crashed before the header was synced. In this case stop reading + /* If the either the page-size or sector-size in the journal-header is + ** invalid, then the process that wrote the journal-header must have + ** crashed before the header was synced. In this case stop reading ** the journal file here. */ return SQLITE_DONE; } - /* Update the page-size to match the value read from the journal. - ** Use a testcase() macro to make sure that malloc failure within + /* Update the page-size to match the value read from the journal. + ** Use a testcase() macro to make sure that malloc failure within ** PagerSetPagesize() is tested. */ rc = sqlite3PagerSetPagesize(pPager, &iPageSize, -1); testcase( rc!=SQLITE_OK ); - /* Update the assumed sector-size to match the value used by + /* Update the assumed sector-size to match the value used by ** the process that created this journal. If this journal was ** created by a process other than this one, then this routine ** is being called from within pager_playback(). The local value @@ -1614,50 +1716,50 @@ static int readJournalHdr( /* -** Write the supplied master journal name into the journal file for pager -** pPager at the current location. The master journal name must be the last +** Write the supplied super-journal name into the journal file for pager +** pPager at the current location. The super-journal name must be the last ** thing written to a journal file. If the pager is in full-sync mode, the ** journal file descriptor is advanced to the next sector boundary before ** anything is written. The format is: ** -** + 4 bytes: PAGER_MJ_PGNO. -** + N bytes: Master journal filename in utf-8. -** + 4 bytes: N (length of master journal name in bytes, no nul-terminator). -** + 4 bytes: Master journal name checksum. +** + 4 bytes: PAGER_SJ_PGNO. +** + N bytes: super-journal filename in utf-8. +** + 4 bytes: N (length of super-journal name in bytes, no nul-terminator). +** + 4 bytes: super-journal name checksum. ** + 8 bytes: aJournalMagic[]. ** -** The master journal page checksum is the sum of the bytes in the master -** journal name, where each byte is interpreted as a signed 8-bit integer. +** The super-journal page checksum is the sum of the bytes in the super-journal +** name, where each byte is interpreted as a signed 8-bit integer. ** -** If zMaster is a NULL pointer (occurs for a single database transaction), +** If zSuper is a NULL pointer (occurs for a single database transaction), ** this call is a no-op. */ -static int writeMasterJournal(Pager *pPager, const char *zMaster){ +static int writeSuperJournal(Pager *pPager, const char *zSuper){ int rc; /* Return code */ - int nMaster; /* Length of string zMaster */ + int nSuper; /* Length of string zSuper */ i64 iHdrOff; /* Offset of header in journal file */ i64 jrnlSize; /* Size of journal file on disk */ - u32 cksum = 0; /* Checksum of string zMaster */ + u32 cksum = 0; /* Checksum of string zSuper */ - assert( pPager->setMaster==0 ); + assert( pPager->setSuper==0 ); assert( !pagerUseWal(pPager) ); - if( !zMaster - || pPager->journalMode==PAGER_JOURNALMODE_MEMORY + if( !zSuper + || pPager->journalMode==PAGER_JOURNALMODE_MEMORY || !isOpen(pPager->jfd) ){ return SQLITE_OK; } - pPager->setMaster = 1; + pPager->setSuper = 1; assert( pPager->journalHdr <= pPager->journalOff ); - /* Calculate the length in bytes and the checksum of zMaster */ - for(nMaster=0; zMaster[nMaster]; nMaster++){ - cksum += zMaster[nMaster]; + /* Calculate the length in bytes and the checksum of zSuper */ + for(nSuper=0; zSuper[nSuper]; nSuper++){ + cksum += zSuper[nSuper]; } /* If in full-sync mode, advance to the next disk sector before writing - ** the master journal name. This is in case the previous page written to + ** the super-journal name. This is in case the previous page written to ** the journal has already been synced. */ if( pPager->fullSync ){ @@ -1665,30 +1767,30 @@ static int writeMasterJournal(Pager *pPager, const char *zMaster){ } iHdrOff = pPager->journalOff; - /* Write the master journal data to the end of the journal file. If + /* Write the super-journal data to the end of the journal file. If ** an error occurs, return the error code to the caller. */ - if( (0 != (rc = write32bits(pPager->jfd, iHdrOff, PAGER_MJ_PGNO(pPager)))) - || (0 != (rc = sqlite3OsWrite(pPager->jfd, zMaster, nMaster, iHdrOff+4))) - || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster, nMaster))) - || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster+4, cksum))) + if( (0 != (rc = write32bits(pPager->jfd, iHdrOff, PAGER_SJ_PGNO(pPager)))) + || (0 != (rc = sqlite3OsWrite(pPager->jfd, zSuper, nSuper, iHdrOff+4))) + || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nSuper, nSuper))) + || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nSuper+4, cksum))) || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8, - iHdrOff+4+nMaster+8))) + iHdrOff+4+nSuper+8))) ){ return rc; } - pPager->journalOff += (nMaster+20); + pPager->journalOff += (nSuper+20); - /* If the pager is in peristent-journal mode, then the physical - ** journal-file may extend past the end of the master-journal name - ** and 8 bytes of magic data just written to the file. This is + /* If the pager is in persistent-journal mode, then the physical + ** journal-file may extend past the end of the super-journal name + ** and 8 bytes of magic data just written to the file. This is ** dangerous because the code to rollback a hot-journal file - ** will not be able to find the master-journal name to determine - ** whether or not the journal is hot. + ** will not be able to find the super-journal name to determine + ** whether or not the journal is hot. ** - ** Easiest thing to do in this scenario is to truncate the journal + ** Easiest thing to do in this scenario is to truncate the journal ** file to the required size. - */ + */ if( SQLITE_OK==(rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize)) && jrnlSize>pPager->journalOff ){ @@ -1710,7 +1812,6 @@ static void pager_reset(Pager *pPager){ ** Return the pPager->iDataVersion value */ u32 sqlite3PagerDataVersion(Pager *pPager){ - assert( pPager->eState>PAGER_OPEN ); return pPager->iDataVersion; } @@ -1724,7 +1825,7 @@ static void releaseAllSavepoints(Pager *pPager){ for(ii=0; iinSavepoint; ii++){ sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint); } - if( !pPager->exclusiveMode || sqlite3IsMemJournal(pPager->sjfd) ){ + if( !pPager->exclusiveMode || sqlite3JournalIsInMemory(pPager->sjfd) ){ sqlite3OsClose(pPager->sjfd); } sqlite3_free(pPager->aSavepoint); @@ -1734,7 +1835,7 @@ static void releaseAllSavepoints(Pager *pPager){ } /* -** Set the bit number pgno in the PagerSavepoint.pInSavepoint +** Set the bit number pgno in the PagerSavepoint.pInSavepoint ** bitvecs of all open savepoints. Return SQLITE_OK if successful ** or SQLITE_NOMEM if a malloc failure occurs. */ @@ -1763,8 +1864,8 @@ static int addToSavepointBitvecs(Pager *pPager, Pgno pgno){ ** not exhibit the UNDELETABLE_WHEN_OPEN property, the journal file is ** closed (if it is open). ** -** If the pager is in ERROR state when this function is called, the -** contents of the pager cache are discarded before switching back to +** If the pager is in ERROR state when this function is called, the +** contents of the pager cache are discarded before switching back to ** the OPEN state. Regardless of whether the pager is in exclusive-mode ** or not, any journal file left in the file-system will be treated ** as a hot-journal and rolled back the next time a read-transaction @@ -1772,9 +1873,9 @@ static int addToSavepointBitvecs(Pager *pPager, Pgno pgno){ */ static void pager_unlock(Pager *pPager){ - assert( pPager->eState==PAGER_READER - || pPager->eState==PAGER_OPEN - || pPager->eState==PAGER_ERROR + assert( pPager->eState==PAGER_READER + || pPager->eState==PAGER_OPEN + || pPager->eState==PAGER_ERROR ); sqlite3BitvecDestroy(pPager->pInJournal); @@ -1783,6 +1884,15 @@ static void pager_unlock(Pager *pPager){ if( pagerUseWal(pPager) ){ assert( !isOpen(pPager->jfd) ); + if( pPager->eState==PAGER_ERROR ){ + /* If an IO error occurs in wal.c while attempting to wrap the wal file, + ** then the Wal object may be holding a write-lock but no read-lock. + ** This call ensures that the write-lock is dropped as well. We cannot + ** have sqlite3WalEndReadTransaction() drop the write-lock, as it once + ** did, because this would break "BEGIN EXCLUSIVE" handling for + ** SQLITE_ENABLE_SETLK_TIMEOUT builds. */ + (void)sqlite3WalEndWriteTransaction(pPager->pWal); + } sqlite3WalEndReadTransaction(pPager->pWal); pPager->eState = PAGER_OPEN; }else if( !pPager->exclusiveMode ){ @@ -1821,7 +1931,6 @@ static void pager_unlock(Pager *pPager){ ** code is cleared and the cache reset in the block below. */ assert( pPager->errCode || pPager->eState!=PAGER_ERROR ); - pPager->changeCountDone = 0; pPager->eState = PAGER_OPEN; } @@ -1830,34 +1939,39 @@ static void pager_unlock(Pager *pPager){ ** it can safely move back to PAGER_OPEN state. This happens in both ** normal and exclusive-locking mode. */ + assert( pPager->errCode==SQLITE_OK || !MEMDB ); if( pPager->errCode ){ - assert( !MEMDB ); - pager_reset(pPager); - pPager->changeCountDone = pPager->tempFile; - pPager->eState = PAGER_OPEN; - pPager->errCode = SQLITE_OK; + if( pPager->tempFile==0 ){ + pager_reset(pPager); + pPager->changeCountDone = 0; + pPager->eState = PAGER_OPEN; + }else{ + pPager->eState = (isOpen(pPager->jfd) ? PAGER_OPEN : PAGER_READER); + } if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0); + pPager->errCode = SQLITE_OK; + setGetterMethod(pPager); } pPager->journalOff = 0; pPager->journalHdr = 0; - pPager->setMaster = 0; + pPager->setSuper = 0; } /* ** This function is called whenever an IOERR or FULL error that requires -** the pager to transition into the ERROR state may ahve occurred. -** The first argument is a pointer to the pager structure, the second -** the error-code about to be returned by a pager API function. The -** value returned is a copy of the second argument to this function. +** the pager to transition into the ERROR state may have occurred. +** The first argument is a pointer to the pager structure, the second +** the error-code about to be returned by a pager API function. The +** value returned is a copy of the second argument to this function. ** ** If the second argument is SQLITE_FULL, SQLITE_IOERR or one of the ** IOERR sub-codes, the pager enters the ERROR state and the error code ** is stored in Pager.errCode. While the pager remains in the ERROR state, ** all major API calls on the Pager will immediately return Pager.errCode. ** -** The ERROR state indicates that the contents of the pager-cache -** cannot be trusted. This state can be cleared by completely discarding +** The ERROR state indicates that the contents of the pager-cache +** cannot be trusted. This state can be cleared by completely discarding ** the contents of the pager-cache. If a transaction was active when ** the persistent error occurred, then the rollback journal may need ** to be replayed to restore the contents of the database file (as if @@ -1874,6 +1988,7 @@ static int pager_error(Pager *pPager, int rc){ if( rc2==SQLITE_FULL || rc2==SQLITE_IOERR ){ pPager->errCode = rc; pPager->eState = PAGER_ERROR; + setGetterMethod(pPager); } return rc; } @@ -1881,27 +1996,50 @@ static int pager_error(Pager *pPager, int rc){ static int pager_truncate(Pager *pPager, Pgno nPage); /* -** This routine ends a transaction. A transaction is usually ended by -** either a COMMIT or a ROLLBACK operation. This routine may be called +** The write transaction open on pPager is being committed (bCommit==1) +** or rolled back (bCommit==0). +** +** Return TRUE if and only if all dirty pages should be flushed to disk. +** +** Rules: +** +** * For non-TEMP databases, always sync to disk. This is necessary +** for transactions to be durable. +** +** * Sync TEMP database only on a COMMIT (not a ROLLBACK) when the backing +** file has been created already (via a spill on pagerStress()) and +** when the number of dirty pages in memory exceeds 25% of the total +** cache size. +*/ +static int pagerFlushOnCommit(Pager *pPager, int bCommit){ + if( pPager->tempFile==0 ) return 1; + if( !bCommit ) return 0; + if( !isOpen(pPager->fd) ) return 0; + return (sqlite3PCachePercentDirty(pPager->pPCache)>=25); +} + +/* +** This routine ends a transaction. A transaction is usually ended by +** either a COMMIT or a ROLLBACK operation. This routine may be called ** after rollback of a hot-journal, or if an error occurs while opening ** the journal file or writing the very first journal-header of a ** database transaction. -** +** ** This routine is never called in PAGER_ERROR state. If it is called ** in PAGER_NONE or PAGER_SHARED state and the lock held is less ** exclusive than a RESERVED lock, it is a no-op. ** ** Otherwise, any active savepoints are released. ** -** If the journal file is open, then it is "finalized". Once a journal -** file has been finalized it is not possible to use it to roll back a +** If the journal file is open, then it is "finalized". Once a journal +** file has been finalized it is not possible to use it to roll back a ** transaction. Nor will it be considered to be a hot-journal by this ** or any other database connection. Exactly how a journal is finalized ** depends on whether or not the pager is running in exclusive mode and ** the current journal-mode (Pager.journalMode value), as follows: ** ** journalMode==MEMORY -** Journal file descriptor is simply closed. This destroys an +** Journal file descriptor is simply closed. This destroys an ** in-memory journal. ** ** journalMode==TRUNCATE @@ -1921,19 +2059,19 @@ static int pager_truncate(Pager *pPager, Pgno nPage); ** journalMode==PERSIST is used instead. ** ** After the journal is finalized, the pager moves to PAGER_READER state. -** If running in non-exclusive rollback mode, the lock on the file is +** If running in non-exclusive rollback mode, the lock on the file is ** downgraded to a SHARED_LOCK. ** ** SQLITE_OK is returned if no error occurs. If an error occurs during ** any of the IO operations to finalize the journal file or unlock the -** database then the IO error code is returned to the user. If the +** database then the IO error code is returned to the user. If the ** operation to finalize the journal file fails, then the code still ** tries to unlock the database file if not in exclusive mode. If the ** unlock operation fails as well, then the first error code related ** to the first error encountered (the journal finalization one) is ** returned. */ -static int pager_end_transaction(Pager *pPager, int hasMaster, int bCommit){ +static int pager_end_transaction(Pager *pPager, int hasSuper, int bCommit){ int rc = SQLITE_OK; /* Error code from journal finalization operation */ int rc2 = SQLITE_OK; /* Error code from db file unlock operation */ @@ -1945,9 +2083,9 @@ static int pager_end_transaction(Pager *pPager, int hasMaster, int bCommit){ ** 1. After a successful hot-journal rollback, it is called with ** eState==PAGER_NONE and eLock==EXCLUSIVE_LOCK. ** - ** 2. If a connection with locking_mode=exclusive holding an EXCLUSIVE + ** 2. If a connection with locking_mode=exclusive holding an EXCLUSIVE ** lock switches back to locking_mode=normal and then executes a - ** read-transaction, this function is called with eState==PAGER_READER + ** read-transaction, this function is called with eState==PAGER_READER ** and eLock==EXCLUSIVE_LOCK when the read-transaction is closed. */ assert( assert_pager_state(pPager) ); @@ -1957,13 +2095,15 @@ static int pager_end_transaction(Pager *pPager, int hasMaster, int bCommit){ } releaseAllSavepoints(pPager); - assert( isOpen(pPager->jfd) || pPager->pInJournal==0 ); + assert( isOpen(pPager->jfd) || pPager->pInJournal==0 + || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_BATCH_ATOMIC) + ); if( isOpen(pPager->jfd) ){ assert( !pagerUseWal(pPager) ); /* Finalize the journal file. */ - if( sqlite3IsMemJournal(pPager->jfd) ){ - assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ); + if( sqlite3JournalIsInMemory(pPager->jfd) ){ + /* assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ); */ sqlite3OsClose(pPager->jfd); }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE ){ if( pPager->journalOff==0 ){ @@ -1981,20 +2121,21 @@ static int pager_end_transaction(Pager *pPager, int hasMaster, int bCommit){ } pPager->journalOff = 0; }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST - || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL) + || (pPager->exclusiveMode && pPager->journalModetempFile); pPager->journalOff = 0; }else{ /* This branch may be executed with Pager.journalMode==MEMORY if ** a hot-journal was just rolled back. In this case the journal ** file should be closed and deleted. If this connection writes to - ** the database file, it will do so using an in-memory journal. + ** the database file, it will do so using an in-memory journal. */ - int bDelete = (!pPager->tempFile && sqlite3JournalExists(pPager->jfd)); - assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE - || pPager->journalMode==PAGER_JOURNALMODE_MEMORY - || pPager->journalMode==PAGER_JOURNALMODE_WAL + int bDelete = !pPager->tempFile; + assert( sqlite3JournalIsInMemory(pPager->jfd)==0 ); + assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE + || pPager->journalMode==PAGER_JOURNALMODE_MEMORY + || pPager->journalMode==PAGER_JOURNALMODE_WAL ); sqlite3OsClose(pPager->jfd); if( bDelete ){ @@ -2017,12 +2158,18 @@ static int pager_end_transaction(Pager *pPager, int hasMaster, int bCommit){ sqlite3BitvecDestroy(pPager->pInJournal); pPager->pInJournal = 0; pPager->nRec = 0; - sqlite3PcacheCleanAll(pPager->pPCache); - sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize); + if( rc==SQLITE_OK ){ + if( MEMDB || pagerFlushOnCommit(pPager, bCommit) ){ + sqlite3PcacheCleanAll(pPager->pPCache); + }else{ + sqlite3PcacheClearWritable(pPager->pPCache); + } + sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize); + } if( pagerUseWal(pPager) ){ - /* Drop the WAL write-lock, if any. Also, if the connection was in - ** locking_mode=exclusive mode but is no longer, drop the EXCLUSIVE + /* Drop the WAL write-lock, if any. Also, if the connection was in + ** locking_mode=exclusive mode but is no longer, drop the EXCLUSIVE ** lock held on the database file. */ rc2 = sqlite3WalEndWriteTransaction(pPager->pWal); @@ -2030,7 +2177,7 @@ static int pager_end_transaction(Pager *pPager, int hasMaster, int bCommit){ }else if( rc==SQLITE_OK && bCommit && pPager->dbFileSize>pPager->dbSize ){ /* This branch is taken when committing a transaction in rollback-journal ** mode if the database file on disk is larger than the database image. - ** At this point the journal has been finalized and the transaction + ** At this point the journal has been finalized and the transaction ** successfully committed, but the EXCLUSIVE lock is still held on the ** file. So it is safe to truncate the database file to its minimum ** required size. */ @@ -2038,37 +2185,39 @@ static int pager_end_transaction(Pager *pPager, int hasMaster, int bCommit){ rc = pager_truncate(pPager, pPager->dbSize); } - if( rc==SQLITE_OK && bCommit && isOpen(pPager->fd) ){ + if( rc==SQLITE_OK && bCommit ){ rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_COMMIT_PHASETWO, 0); if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK; } - if( !pPager->exclusiveMode + if( !pPager->exclusiveMode && (!pagerUseWal(pPager) || sqlite3WalExclusiveMode(pPager->pWal, 0)) ){ rc2 = pagerUnlockDb(pPager, SHARED_LOCK); - pPager->changeCountDone = 0; } pPager->eState = PAGER_READER; - pPager->setMaster = 0; + pPager->setSuper = 0; return (rc==SQLITE_OK?rc2:rc); } +/* Forward reference */ +static int pager_playback(Pager *pPager, int isHot); + /* -** Execute a rollback if a transaction is active and unlock the -** database file. +** Execute a rollback if a transaction is active and unlock the +** database file. ** -** If the pager has already entered the ERROR state, do not attempt +** If the pager has already entered the ERROR state, do not attempt ** the rollback at this time. Instead, pager_unlock() is called. The ** call to pager_unlock() will discard all in-memory pages, unlock -** the database file and move the pager back to OPEN state. If this -** means that there is a hot-journal left in the file-system, the next -** connection to obtain a shared lock on the pager (which may be this one) +** the database file and move the pager back to OPEN state. If this +** means that there is a hot-journal left in the file-system, the next +** connection to obtain a shared lock on the pager (which may be this one) ** will roll it back. ** ** If the pager has not already entered the ERROR state, but an IO or -** malloc error occurs during a rollback, then this will itself cause +** malloc error occurs during a rollback, then this will itself cause ** the pager to enter the ERROR state. Which will be cleared by the ** call to pager_unlock(), as described above. */ @@ -2083,16 +2232,31 @@ static void pagerUnlockAndRollback(Pager *pPager){ assert( pPager->eState==PAGER_READER ); pager_end_transaction(pPager, 0, 0); } + }else if( pPager->eState==PAGER_ERROR + && pPager->journalMode==PAGER_JOURNALMODE_MEMORY + && isOpen(pPager->jfd) + ){ + /* Special case for a ROLLBACK due to I/O error with an in-memory + ** journal: We have to rollback immediately, before the journal is + ** closed, because once it is closed, all content is forgotten. */ + int errCode = pPager->errCode; + u8 eLock = pPager->eLock; + pPager->eState = PAGER_OPEN; + pPager->errCode = SQLITE_OK; + pPager->eLock = EXCLUSIVE_LOCK; + pager_playback(pPager, 1); + pPager->errCode = errCode; + pPager->eLock = eLock; } pager_unlock(pPager); } /* ** Parameter aData must point to a buffer of pPager->pageSize bytes -** of data. Compute and return a checksum based ont the contents of the +** of data. Compute and return a checksum based on the contents of the ** page of data and the current value of pPager->cksumInit. ** -** This is not a real checksum. It is really just the sum of the +** This is not a real checksum. It is really just the sum of the ** random initial value (pPager->cksumInit) and every 200th byte ** of the page data, starting with byte offset (pPager->pageSize%200). ** Each byte is interpreted as an 8-bit unsigned integer. @@ -2100,8 +2264,8 @@ static void pagerUnlockAndRollback(Pager *pPager){ ** Changing the formula used to compute this checksum results in an ** incompatible journal file format. ** -** If journal corruption occurs due to a power failure, the most likely -** scenario is that one end or the other of the record will be changed. +** If journal corruption occurs due to a power failure, the most likely +** scenario is that one end or the other of the record will be changed. ** It is much less likely that the two ends of the journal record will be ** correct and the middle be corrupt. Thus, this "checksum" scheme, ** though fast and simple, catches the mostly likely kind of corruption. @@ -2120,6 +2284,7 @@ static u32 pager_cksum(Pager *pPager, const u8 *aData){ ** Report the current page size and number of reserved bytes back ** to the codec. */ +/* BEGIN SQLCIPHER */ #ifdef SQLITE_HAS_CODEC static void pagerReportSize(Pager *pPager){ if( pPager->xCodecSizeChng ){ @@ -2130,7 +2295,9 @@ static void pagerReportSize(Pager *pPager){ #else # define pagerReportSize(X) /* No-op if we do not support a codec */ #endif +/* END SQLCIPHER */ +/* BEGIN SQLCIPHER */ #ifdef SQLITE_HAS_CODEC /* ** Make sure the number of reserved bits is the same in the destination @@ -2144,6 +2311,7 @@ void sqlite3PagerAlignReserve(Pager *pDest, Pager *pSrc){ } } #endif +/* END SQLCIPHER */ /* ** Read a single page from either the journal file (if isMainJrnl==1) or @@ -2151,7 +2319,7 @@ void sqlite3PagerAlignReserve(Pager *pDest, Pager *pSrc){ ** The page begins at offset *pOffset into the file. The *pOffset ** value is increased to the start of the next page in the journal. ** -** The main rollback journal uses checksums - the statement journal does +** The main rollback journal uses checksums - the statement journal does ** not. ** ** If the page number of the page record read from the (sub-)journal file @@ -2171,8 +2339,8 @@ void sqlite3PagerAlignReserve(Pager *pDest, Pager *pSrc){ ** is successfully read from the (sub-)journal file but appears to be ** corrupted, SQLITE_DONE is returned. Data is considered corrupted in ** two circumstances: -** -** * If the record page-number is illegal (0 or PAGER_MJ_PGNO), or +** +** * If the record page-number is illegal (0 or PAGER_SJ_PGNO), or ** * If the record is being rolled back from the main journal file ** and the checksum field does not match the record content. ** @@ -2196,6 +2364,13 @@ static int pager_playback_one_page( char *aData; /* Temporary storage for the page */ sqlite3_file *jfd; /* The file descriptor for the journal file */ int isSynced; /* True if journal page is synced */ +/* BEGIN SQLCIPHER */ +#ifdef SQLITE_HAS_CODEC + /* The jrnlEnc flag is true if Journal pages should be passed through + ** the codec. It is false for pure in-memory journals. */ + const int jrnlEnc = (isMainJrnl || pPager->subjInMemory==0); +#endif +/* END SQLCIPHER */ assert( (isMainJrnl&~1)==0 ); /* isMainJrnl is 0 or 1 */ assert( (isSavepnt&~1)==0 ); /* isSavepnt is 0 or 1 */ @@ -2206,7 +2381,7 @@ static int pager_playback_one_page( assert( aData ); /* Temp storage must have already been allocated */ assert( pagerUseWal(pPager)==0 || (!isMainJrnl && isSavepnt) ); - /* Either the state is greater than PAGER_WRITER_CACHEMOD (a transaction + /* Either the state is greater than PAGER_WRITER_CACHEMOD (a transaction ** or savepoint rollback done at the request of the caller) or this is ** a hot-journal rollback. If it is a hot-journal rollback, the pager ** is in state OPEN and holds an EXCLUSIVE lock. Hot-journal rollback @@ -2232,7 +2407,7 @@ static int pager_playback_one_page( ** it could cause invalid data to be written into the journal. We need to ** detect this invalid data (with high probability) and ignore it. */ - if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){ + if( pgno==0 || pgno==PAGER_SJ_PGNO(pPager) ){ assert( !isSavepnt ); return SQLITE_DONE; } @@ -2273,7 +2448,7 @@ static int pager_playback_one_page( ** assert()able. ** ** If in WRITER_DBMOD, WRITER_FINISHED or OPEN state, then we update the - ** pager cache if it exists and the main file. The page is then marked + ** pager cache if it exists and the main file. The page is then marked ** not dirty. Since this code is only executed in PAGER_OPEN state for ** a hot-journal rollback, it is guaranteed that the page-cache is empty ** if the pager is in OPEN state. @@ -2302,7 +2477,7 @@ static int pager_playback_one_page( pPg = sqlite3PagerLookup(pPager, pgno); } assert( pPg || !MEMDB ); - assert( pPager->eState!=PAGER_OPEN || pPg==0 ); + assert( pPager->eState!=PAGER_OPEN || pPg==0 || pPager->tempFile ); PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n", PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData), (isMainJrnl?"main-journal":"sub-journal") @@ -2319,30 +2494,54 @@ static int pager_playback_one_page( i64 ofst = (pgno-1)*(i64)pPager->pageSize; testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 ); assert( !pagerUseWal(pPager) ); + + /* Write the data read from the journal back into the database file. + ** This is usually safe even for an encrypted database - as the data + ** was encrypted before it was written to the journal file. The exception + ** is if the data was just read from an in-memory sub-journal. In that + ** case it must be encrypted here before it is copied into the database + ** file. */ +/* BEGIN SQLCIPHER */ +#ifdef SQLITE_HAS_CODEC + if( !jrnlEnc ){ + CODEC2(pPager, aData, pgno, 7, rc=SQLITE_NOMEM_BKPT, aData); + rc = sqlite3OsWrite(pPager->fd, (u8 *)aData, pPager->pageSize, ofst); + CODEC1(pPager, aData, pgno, 3, rc=SQLITE_NOMEM_BKPT); + }else +#endif +/* END SQLCIPHER */ rc = sqlite3OsWrite(pPager->fd, (u8 *)aData, pPager->pageSize, ofst); + if( pgno>pPager->dbFileSize ){ pPager->dbFileSize = pgno; } if( pPager->pBackup ){ - CODEC1(pPager, aData, pgno, 3, rc=SQLITE_NOMEM); +/* BEGIN SQLCIPHER */ +#ifdef SQLITE_HAS_CODEC + if( jrnlEnc ){ + CODEC1(pPager, aData, pgno, 3, rc=SQLITE_NOMEM_BKPT); + sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData); + CODEC2(pPager, aData, pgno, 7, rc=SQLITE_NOMEM_BKPT,aData); + }else +#endif +/* END SQLCIPHER */ sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData); - CODEC2(pPager, aData, pgno, 7, rc=SQLITE_NOMEM, aData); } }else if( !isMainJrnl && pPg==0 ){ /* If this is a rollback of a savepoint and data was not written to ** the database and the page is not in-memory, there is a potential - ** problem. When the page is next fetched by the b-tree layer, it - ** will be read from the database file, which may or may not be - ** current. + ** problem. When the page is next fetched by the b-tree layer, it + ** will be read from the database file, which may or may not be + ** current. ** ** There are a couple of different ways this can happen. All are quite - ** obscure. When running in synchronous mode, this can only happen + ** obscure. When running in synchronous mode, this can only happen ** if the page is on the free-list at the start of the transaction, then ** populated, then moved using sqlite3PagerMovepage(). ** ** The solution is to add an in-memory page to the cache containing - ** the data just read from the sub-journal. Mark the page as dirty - ** and if the pager requires a journal-sync, then mark the page as + ** the data just read from the sub-journal. Mark the page as dirty + ** and if the pager requires a journal-sync, then mark the page as ** requiring a journal-sync before it is written. */ assert( isSavepnt ); @@ -2352,7 +2551,6 @@ static int pager_playback_one_page( assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)!=0 ); pPager->doNotSpill &= ~SPILLFLAG_ROLLBACK; if( rc!=SQLITE_OK ) return rc; - pPg->flags &= ~PGHDR_NEED_READ; sqlite3PcacheMakeDirty(pPg); } if( pPg ){ @@ -2366,29 +2564,10 @@ static int pager_playback_one_page( pData = pPg->pData; memcpy(pData, (u8*)aData, pPager->pageSize); pPager->xReiniter(pPg); - if( isMainJrnl && (!isSavepnt || *pOffset<=pPager->journalHdr) ){ - /* If the contents of this page were just restored from the main - ** journal file, then its content must be as they were when the - ** transaction was first opened. In this case we can mark the page - ** as clean, since there will be no need to write it out to the - ** database. - ** - ** There is one exception to this rule. If the page is being rolled - ** back as part of a savepoint (or statement) rollback from an - ** unsynced portion of the main journal file, then it is not safe - ** to mark the page as clean. This is because marking the page as - ** clean will clear the PGHDR_NEED_SYNC flag. Since the page is - ** already in the journal file (recorded in Pager.pInJournal) and - ** the PGHDR_NEED_SYNC flag is cleared, if the page is written to - ** again within this transaction, it will be marked as dirty but - ** the PGHDR_NEED_SYNC flag will not be set. It could then potentially - ** be written out into the database file before its journal file - ** segment is synced. If a crash occurs during or following this, - ** database corruption may ensue. - */ - assert( !pagerUseWal(pPager) ); - sqlite3PcacheMakeClean(pPg); - } + /* It used to be that sqlite3PcacheMakeClean(pPg) was called here. But + ** that call was dangerous and had no detectable benefit since the cache + ** is normally cleaned by sqlite3PcacheCleanAll() after rollback and so + ** has been removed. */ pager_set_pagehash(pPg); /* If this was page 1, then restore the value of Pager.dbFileVers. @@ -2398,160 +2577,175 @@ static int pager_playback_one_page( } /* Decode the page just read from disk */ - CODEC1(pPager, pData, pPg->pgno, 3, rc=SQLITE_NOMEM); +/* BEGIN SQLCIPHER */ +#if SQLITE_HAS_CODEC + if( jrnlEnc ){ CODEC1(pPager, pData, pPg->pgno, 3, rc=SQLITE_NOMEM_BKPT); } +#endif +/* END SQLCIPHER */ sqlite3PcacheRelease(pPg); } return rc; } /* -** Parameter zMaster is the name of a master journal file. A single journal -** file that referred to the master journal file has just been rolled back. -** This routine checks if it is possible to delete the master journal file, +** Parameter zSuper is the name of a super-journal file. A single journal +** file that referred to the super-journal file has just been rolled back. +** This routine checks if it is possible to delete the super-journal file, ** and does so if it is. ** -** Argument zMaster may point to Pager.pTmpSpace. So that buffer is not +** Argument zSuper may point to Pager.pTmpSpace. So that buffer is not ** available for use within this function. ** -** When a master journal file is created, it is populated with the names -** of all of its child journals, one after another, formatted as utf-8 -** encoded text. The end of each child journal file is marked with a -** nul-terminator byte (0x00). i.e. the entire contents of a master journal +** When a super-journal file is created, it is populated with the names +** of all of its child journals, one after another, formatted as utf-8 +** encoded text. The end of each child journal file is marked with a +** nul-terminator byte (0x00). i.e. the entire contents of a super-journal ** file for a transaction involving two databases might be: ** ** "/home/bill/a.db-journal\x00/home/bill/b.db-journal\x00" ** -** A master journal file may only be deleted once all of its child +** A super-journal file may only be deleted once all of its child ** journals have been rolled back. ** -** This function reads the contents of the master-journal file into +** This function reads the contents of the super-journal file into ** memory and loops through each of the child journal names. For ** each child journal, it checks if: ** ** * if the child journal exists, and if so -** * if the child journal contains a reference to master journal -** file zMaster +** * if the child journal contains a reference to super-journal +** file zSuper ** ** If a child journal can be found that matches both of the criteria ** above, this function returns without doing anything. Otherwise, if -** no such child journal can be found, file zMaster is deleted from +** no such child journal can be found, file zSuper is deleted from ** the file-system using sqlite3OsDelete(). ** ** If an IO error within this function, an error code is returned. This ** function allocates memory by calling sqlite3Malloc(). If an allocation -** fails, SQLITE_NOMEM is returned. Otherwise, if no IO or malloc errors +** fails, SQLITE_NOMEM is returned. Otherwise, if no IO or malloc errors ** occur, SQLITE_OK is returned. ** ** TODO: This function allocates a single block of memory to load -** the entire contents of the master journal file. This could be -** a couple of kilobytes or so - potentially larger than the page +** the entire contents of the super-journal file. This could be +** a couple of kilobytes or so - potentially larger than the page ** size. */ -static int pager_delmaster(Pager *pPager, const char *zMaster){ +static int pager_delsuper(Pager *pPager, const char *zSuper){ sqlite3_vfs *pVfs = pPager->pVfs; int rc; /* Return code */ - sqlite3_file *pMaster; /* Malloc'd master-journal file descriptor */ + sqlite3_file *pSuper; /* Malloc'd super-journal file descriptor */ sqlite3_file *pJournal; /* Malloc'd child-journal file descriptor */ - char *zMasterJournal = 0; /* Contents of master journal file */ - i64 nMasterJournal; /* Size of master journal file */ + char *zSuperJournal = 0; /* Contents of super-journal file */ + i64 nSuperJournal; /* Size of super-journal file */ char *zJournal; /* Pointer to one journal within MJ file */ - char *zMasterPtr; /* Space to hold MJ filename from a journal file */ - int nMasterPtr; /* Amount of space allocated to zMasterPtr[] */ + char *zSuperPtr; /* Space to hold super-journal filename */ + char *zFree = 0; /* Free this buffer */ + i64 nSuperPtr; /* Amount of space allocated to zSuperPtr[] */ - /* Allocate space for both the pJournal and pMaster file descriptors. - ** If successful, open the master journal file for reading. + /* Allocate space for both the pJournal and pSuper file descriptors. + ** If successful, open the super-journal file for reading. */ - pMaster = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile * 2); - pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile); - if( !pMaster ){ - rc = SQLITE_NOMEM; + pSuper = (sqlite3_file *)sqlite3MallocZero(2 * (i64)pVfs->szOsFile); + if( !pSuper ){ + rc = SQLITE_NOMEM_BKPT; + pJournal = 0; }else{ - const int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL); - rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0); + const int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_SUPER_JOURNAL); + rc = sqlite3OsOpen(pVfs, zSuper, pSuper, flags, 0); + pJournal = (sqlite3_file *)(((u8 *)pSuper) + pVfs->szOsFile); } - if( rc!=SQLITE_OK ) goto delmaster_out; + if( rc!=SQLITE_OK ) goto delsuper_out; - /* Load the entire master journal file into space obtained from - ** sqlite3_malloc() and pointed to by zMasterJournal. Also obtain - ** sufficient space (in zMasterPtr) to hold the names of master - ** journal files extracted from regular rollback-journals. + /* Load the entire super-journal file into space obtained from + ** sqlite3_malloc() and pointed to by zSuperJournal. Also obtain + ** sufficient space (in zSuperPtr) to hold the names of super-journal + ** files extracted from regular rollback-journals. */ - rc = sqlite3OsFileSize(pMaster, &nMasterJournal); - if( rc!=SQLITE_OK ) goto delmaster_out; - nMasterPtr = pVfs->mxPathname+1; - zMasterJournal = sqlite3Malloc(nMasterJournal + nMasterPtr + 1); - if( !zMasterJournal ){ - rc = SQLITE_NOMEM; - goto delmaster_out; - } - zMasterPtr = &zMasterJournal[nMasterJournal+1]; - rc = sqlite3OsRead(pMaster, zMasterJournal, (int)nMasterJournal, 0); - if( rc!=SQLITE_OK ) goto delmaster_out; - zMasterJournal[nMasterJournal] = 0; - - zJournal = zMasterJournal; - while( (zJournal-zMasterJournal)mxPathname; + assert( nSuperJournal>=0 && nSuperPtr>0 ); + zFree = sqlite3Malloc(4 + nSuperJournal + nSuperPtr + 2); + if( !zFree ){ + rc = SQLITE_NOMEM_BKPT; + goto delsuper_out; + }else{ + assert( nSuperJournal<=0x7fffffff ); + } + zFree[0] = zFree[1] = zFree[2] = zFree[3] = 0; + zSuperJournal = &zFree[4]; + zSuperPtr = &zSuperJournal[nSuperJournal+2]; + rc = sqlite3OsRead(pSuper, zSuperJournal, (int)nSuperJournal, 0); + if( rc!=SQLITE_OK ) goto delsuper_out; + zSuperJournal[nSuperJournal] = 0; + zSuperJournal[nSuperJournal+1] = 0; + + zJournal = zSuperJournal; + while( (zJournal-zSuperJournal)pageSize bytes). +** DBMOD or OPEN state, this function is a no-op. Otherwise, the size +** of the file is changed to nPage pages (nPage*pPager->pageSize bytes). ** If the file on disk is currently larger than nPage pages, then use the VFS ** xTruncate() method to truncate it. ** -** Or, it might be the case that the file on disk is smaller than -** nPage pages. Some operating system implementations can get confused if -** you try to truncate a file to some size that is larger than it -** currently is, so detect this case and write a single zero byte to +** Or, it might be the case that the file on disk is smaller than +** nPage pages. Some operating system implementations can get confused if +** you try to truncate a file to some size that is larger than it +** currently is, so detect this case and write a single zero byte to ** the end of the new file instead. ** ** If successful, return SQLITE_OK. If an IO error occurs while modifying @@ -2561,9 +2755,11 @@ static int pager_truncate(Pager *pPager, Pgno nPage){ int rc = SQLITE_OK; assert( pPager->eState!=PAGER_ERROR ); assert( pPager->eState!=PAGER_READER ); - - if( isOpen(pPager->fd) - && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN) + PAGERTRACE(("Truncate %d npage %u\n", PAGERID(pPager), nPage)); + + + if( isOpen(pPager->fd) + && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN) ){ i64 currentSize, newSize; int szPage = pPager->pageSize; @@ -2579,6 +2775,7 @@ static int pager_truncate(Pager *pPager, Pgno nPage){ memset(pTmp, 0, szPage); testcase( (newSize-szPage) == currentSize ); testcase( (newSize-szPage) > currentSize ); + sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &newSize); rc = sqlite3OsWrite(pPager->fd, pTmp, szPage, newSize-szPage); } if( rc==SQLITE_OK ){ @@ -2607,9 +2804,9 @@ int sqlite3SectorSize(sqlite3_file *pFile){ /* ** Set the value of the Pager.sectorSize variable for the given ** pager based on the value returned by the xSectorSize method -** of the open database file. The sector size will be used -** to determine the size and alignment of journal header and -** master journal pointers within created journal files. +** of the open database file. The sector size will be used +** to determine the size and alignment of journal header and +** super-journal pointers within created journal files. ** ** For temporary files the effective sector size is always 512 bytes. ** @@ -2631,7 +2828,7 @@ static void setSectorSize(Pager *pPager){ assert( isOpen(pPager->fd) || pPager->tempFile ); if( pPager->tempFile - || (sqlite3OsDeviceCharacteristics(pPager->fd) & + || (sqlite3OsDeviceCharacteristics(pPager->fd) & SQLITE_IOCAP_POWERSAFE_OVERWRITE)!=0 ){ /* Sector size doesn't matter for temporary files. Also, the file @@ -2645,15 +2842,15 @@ static void setSectorSize(Pager *pPager){ /* ** Playback the journal and thus restore the database file to -** the state it was in before we started making changes. +** the state it was in before we started making changes. ** -** The journal file format is as follows: +** The journal file format is as follows: ** ** (1) 8 byte prefix. A copy of aJournalMagic[]. ** (2) 4 byte big-endian integer which is the number of valid page records ** in the journal. If this value is 0xffffffff, then compute the ** number of page records from the journal size. -** (3) 4 byte big-endian integer which is the initial value for the +** (3) 4 byte big-endian integer which is the initial value for the ** sanity checksum. ** (4) 4 byte integer which is the number of pages to truncate the ** database to during a rollback. @@ -2682,7 +2879,7 @@ static void setSectorSize(Pager *pPager){ ** from the file size. This value is used when the user selects the ** no-sync option for the journal. A power failure could lead to corruption ** in this case. But for things like temporary table (which will be -** deleted when the power is restored) we don't care. +** deleted when the power is restored) we don't care. ** ** If the file opened as the journal file is not a well-formed ** journal file then all pages up to the first corrupted page are rolled @@ -2694,7 +2891,7 @@ static void setSectorSize(Pager *pPager){ ** and an error code is returned. ** ** The isHot parameter indicates that we are trying to rollback a journal -** that might be a hot journal. Or, it could be that the journal is +** that might be a hot journal. Or, it could be that the journal is ** preserved because of JOURNALMODE_PERSIST or JOURNALMODE_TRUNCATE. ** If the journal really is hot, reset the pager cache prior rolling ** back any content. If the journal is merely persistent, no reset is @@ -2708,9 +2905,10 @@ static int pager_playback(Pager *pPager, int isHot){ Pgno mxPg = 0; /* Size of the original file in pages */ int rc; /* Result code of a subroutine */ int res = 1; /* Value returned by sqlite3OsAccess() */ - char *zMaster = 0; /* Name of master journal file if any */ + char *zSuper = 0; /* Name of super-journal file if any */ int needPagerReset; /* True to reset page prior to first page rollback */ int nPlayback = 0; /* Total number of pages restored from journal */ + u32 savedPageSize = pPager->pageSize; /* Figure out how many records are in the journal. Abort early if ** the journal is empty. @@ -2721,32 +2919,32 @@ static int pager_playback(Pager *pPager, int isHot){ goto end_playback; } - /* Read the master journal name from the journal, if it is present. - ** If a master journal file name is specified, but the file is not + /* Read the super-journal name from the journal, if it is present. + ** If a super-journal file name is specified, but the file is not ** present on disk, then the journal is not hot and does not need to be ** played back. ** ** TODO: Technically the following is an error because it assumes that ** buffer Pager.pTmpSpace is (mxPathname+1) bytes or larger. i.e. that ** (pPager->pageSize >= pPager->pVfs->mxPathname+1). Using os_unix.c, - ** mxPathname is 512, which is the same as the minimum allowable value + ** mxPathname is 512, which is the same as the minimum allowable value ** for pageSize. */ - zMaster = pPager->pTmpSpace; - rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1); - if( rc==SQLITE_OK && zMaster[0] ){ - rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res); + zSuper = pPager->pTmpSpace; + rc = readSuperJournal(pPager->jfd, zSuper, 1+(i64)pPager->pVfs->mxPathname); + if( rc==SQLITE_OK && zSuper[0] ){ + rc = sqlite3OsAccess(pVfs, zSuper, SQLITE_ACCESS_EXISTS, &res); } - zMaster = 0; + zSuper = 0; if( rc!=SQLITE_OK || !res ){ goto end_playback; } pPager->journalOff = 0; needPagerReset = isHot; - /* This loop terminates either when a readJournalHdr() or - ** pager_playback_one_page() call returns SQLITE_DONE or an IO error - ** occurs. + /* This loop terminates either when a readJournalHdr() or + ** pager_playback_one_page() call returns SQLITE_DONE or an IO error + ** occurs. */ while( 1 ){ /* Read the next journal header from the journal file. If there are @@ -2755,7 +2953,7 @@ static int pager_playback(Pager *pPager, int isHot){ ** This indicates nothing more needs to be rolled back. */ rc = readJournalHdr(pPager, isHot, szJ, &nRec, &mxPg); - if( rc!=SQLITE_OK ){ + if( rc!=SQLITE_OK ){ if( rc==SQLITE_DONE ){ rc = SQLITE_OK; } @@ -2783,7 +2981,7 @@ static int pager_playback(Pager *pPager, int isHot){ ** chunk of the journal contains zero pages to be rolled back. But ** when doing a ROLLBACK and the nRec==0 chunk is the last chunk in ** the journal, it means that the journal might contain additional - ** pages that need to be rolled back and that the number of pages + ** pages that need to be rolled back and that the number of pages ** should be computed based on the journal file size. */ if( nRec==0 && !isHot && @@ -2800,9 +2998,12 @@ static int pager_playback(Pager *pPager, int isHot){ goto end_playback; } pPager->dbSize = mxPg; + if( pPager->mxPgnomxPgno = mxPg; + } } - /* Copy original pages out of the journal and back into the + /* Copy original pages out of the journal and back into the ** database file and/or page cache. */ for(u=0; ufd->pMethods ){ - sqlite3OsFileControlHint(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0); - } + sqlite3OsFileControlHint(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0); #endif - /* If this playback is happening automatically as a result of an IO or - ** malloc error that occurred after the change-counter was updated but - ** before the transaction was committed, then the change-counter - ** modification may just have been reverted. If this happens in exclusive + /* If this playback is happening automatically as a result of an IO or + ** malloc error that occurred after the change-counter was updated but + ** before the transaction was committed, then the change-counter + ** modification may just have been reverted. If this happens in exclusive ** mode, then subsequent transactions performed by the connection will not ** update the change-counter at all. This may lead to cache inconsistency ** problems for other processes at some point in the future. So, just @@ -2863,8 +3065,12 @@ static int pager_playback(Pager *pPager, int isHot){ pPager->changeCountDone = pPager->tempFile; if( rc==SQLITE_OK ){ - zMaster = pPager->pTmpSpace; - rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1); + /* Leave 4 bytes of space before the super-journal filename in memory. + ** This is because it may end up being passed to sqlite3OsOpen(), in + ** which case it requires 4 0x00 bytes in memory immediately before + ** the filename. */ + zSuper = &pPager->pTmpSpace[4]; + rc = readSuperJournal(pPager->jfd, zSuper, 1+(i64)pPager->pVfs->mxPathname); testcase( rc!=SQLITE_OK ); } if( rc==SQLITE_OK @@ -2873,14 +3079,16 @@ static int pager_playback(Pager *pPager, int isHot){ rc = sqlite3PagerSync(pPager, 0); } if( rc==SQLITE_OK ){ - rc = pager_end_transaction(pPager, zMaster[0]!='\0', 0); + rc = pager_end_transaction(pPager, zSuper[0]!='\0', 0); testcase( rc!=SQLITE_OK ); } - if( rc==SQLITE_OK && zMaster[0] && res ){ - /* If there was a master journal and this routine will return success, - ** see if it is possible to delete the master journal. + if( rc==SQLITE_OK && zSuper[0] && res ){ + /* If there was a super-journal and this routine will return success, + ** see if it is possible to delete the super-journal. */ - rc = pager_delmaster(pPager, zMaster); + assert( zSuper==&pPager->pTmpSpace[4] ); + memset(pPager->pTmpSpace, 0, 4); + rc = pager_delsuper(pPager, zSuper); testcase( rc!=SQLITE_OK ); } if( isHot && nPlayback ){ @@ -2898,7 +3106,8 @@ static int pager_playback(Pager *pPager, int isHot){ /* -** Read the content for page pPg out of the database file and into +** Read the content for page pPg out of the database file (or out of +** the WAL if that is where the most recent copy if found) into ** pPg->pData. A shared lock or greater must be held on the database ** file before this function is called. ** @@ -2908,30 +3117,33 @@ static int pager_playback(Pager *pPager, int isHot){ ** If an IO error occurs, then the IO error is returned to the caller. ** Otherwise, SQLITE_OK is returned. */ -static int readDbPage(PgHdr *pPg, u32 iFrame){ +static int readDbPage(PgHdr *pPg){ Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */ - Pgno pgno = pPg->pgno; /* Page number to read */ int rc = SQLITE_OK; /* Return code */ - int pgsz = pPager->pageSize; /* Number of bytes to read */ + +#ifndef SQLITE_OMIT_WAL + u32 iFrame = 0; /* Frame of WAL containing pgno */ assert( pPager->eState>=PAGER_READER && !MEMDB ); assert( isOpen(pPager->fd) ); -#ifndef SQLITE_OMIT_WAL + if( pagerUseWal(pPager) ){ + rc = sqlite3WalFindFrame(pPager->pWal, pPg->pgno, &iFrame); + if( rc ) return rc; + } if( iFrame ){ - /* Try to pull the page from the write-ahead log. */ - rc = sqlite3WalReadFrame(pPager->pWal, iFrame, pgsz, pPg->pData); + rc = sqlite3WalReadFrame(pPager->pWal, iFrame,pPager->pageSize,pPg->pData); }else #endif { - i64 iOffset = (pgno-1)*(i64)pPager->pageSize; - rc = sqlite3OsRead(pPager->fd, pPg->pData, pgsz, iOffset); + i64 iOffset = (pPg->pgno-1)*(i64)pPager->pageSize; + rc = sqlite3OsRead(pPager->fd, pPg->pData, pPager->pageSize, iOffset); if( rc==SQLITE_IOERR_SHORT_READ ){ rc = SQLITE_OK; } } - if( pgno==1 ){ + if( pPg->pgno==1 ){ if( rc ){ /* If the read is unsuccessful, set the dbFileVers[] to something ** that will never be a valid file version. dbFileVers[] is a copy @@ -2951,13 +3163,13 @@ static int readDbPage(PgHdr *pPg, u32 iFrame){ memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers)); } } - CODEC1(pPager, pPg->pData, pgno, 3, rc = SQLITE_NOMEM); + CODEC1(pPager, pPg->pData, pPg->pgno, 3, rc = SQLITE_NOMEM_BKPT); PAGER_INCR(sqlite3_pager_readdb_count); PAGER_INCR(pPager->nRead); - IOTRACE(("PGIN %p %d\n", pPager, pgno)); + IOTRACE(("PGIN %p %d\n", pPager, pPg->pgno)); PAGERTRACE(("FETCH %d page %d hash(%08x)\n", - PAGERID(pPager), pgno, pager_pagehash(pPg))); + PAGERID(pPager), pPg->pgno, pager_pagehash(pPg))); return rc; } @@ -2972,6 +3184,7 @@ static int readDbPage(PgHdr *pPg, u32 iFrame){ */ static void pager_write_changecounter(PgHdr *pPg){ u32 change_counter; + if( NEVER(pPg==0) ) return; /* Increment the value just read and write it back to byte 24. */ change_counter = sqlite3Get4byte((u8*)pPg->pPager->dbFileVers)+1; @@ -2986,15 +3199,15 @@ static void pager_write_changecounter(PgHdr *pPg){ #ifndef SQLITE_OMIT_WAL /* -** This function is invoked once for each page that has already been +** This function is invoked once for each page that has already been ** written into the log file when a WAL transaction is rolled back. -** Parameter iPg is the page number of said page. The pCtx argument +** Parameter iPg is the page number of said page. The pCtx argument ** is actually a pointer to the Pager structure. ** ** If page iPg is present in the cache, and has no outstanding references, ** it is discarded. Otherwise, if there are one or more outstanding ** references, the page content is reloaded from the database. If the -** attempt to reload content from the database is required and fails, +** attempt to reload content from the database is required and fails, ** return an SQLite error code. Otherwise, SQLITE_OK. */ static int pagerUndoCallback(void *pCtx, Pgno iPg){ @@ -3008,11 +3221,7 @@ static int pagerUndoCallback(void *pCtx, Pgno iPg){ if( sqlite3PcachePageRefcount(pPg)==1 ){ sqlite3PcacheDrop(pPg); }else{ - u32 iFrame = 0; - rc = sqlite3WalFindFrame(pPager->pWal, pPg->pgno, &iFrame); - if( rc==SQLITE_OK ){ - rc = readDbPage(pPg, iFrame); - } + rc = readDbPage(pPg); if( rc==SQLITE_OK ){ pPager->xReiniter(pPg); } @@ -3024,7 +3233,7 @@ static int pagerUndoCallback(void *pCtx, Pgno iPg){ ** updated as data is copied out of the rollback journal and into the ** database. This is not generally possible with a WAL database, as ** rollback involves simply truncating the log file. Therefore, if one - ** or more frames have already been written to the log (and therefore + ** or more frames have already been written to the log (and therefore ** also copied into the backup databases) as part of this transaction, ** the backups must be restarted. */ @@ -3041,7 +3250,7 @@ static int pagerRollbackWal(Pager *pPager){ PgHdr *pList; /* List of dirty pages to revert */ /* For all pages in the cache that are currently dirty or have already - ** been written (but not committed) to the log file, do one of the + ** been written (but not committed) to the log file, do one of the ** following: ** ** + Discard the cached page (if refcount==0), or @@ -3063,11 +3272,11 @@ static int pagerRollbackWal(Pager *pPager){ ** This function is a wrapper around sqlite3WalFrames(). As well as logging ** the contents of the list of pages headed by pList (connected by pDirty), ** this function notifies any active backup processes that the pages have -** changed. +** changed. ** ** The list of pages passed into this routine is always sorted by page number. ** Hence, if page 1 appears anywhere on the list, it will be the first page. -*/ +*/ static int pagerWalFrames( Pager *pPager, /* Pager object */ PgHdr *pList, /* List of frames to log */ @@ -3081,7 +3290,7 @@ static int pagerWalFrames( assert( pPager->pWal ); assert( pList ); #ifdef SQLITE_DEBUG - /* Verify that the page list is in accending order */ + /* Verify that the page list is in ascending order */ for(p=pList; p && p->pDirty; p=p->pDirty){ assert( p->pgno < p->pDirty->pgno ); } @@ -3108,7 +3317,7 @@ static int pagerWalFrames( pPager->aStat[PAGER_STAT_WRITE] += nList; if( pList->pgno==1 ) pager_write_changecounter(pList); - rc = sqlite3WalFrames(pPager->pWal, + rc = sqlite3WalFrames(pPager->pWal, pPager->pageSize, pList, nTruncate, isCommit, pPager->walSyncFlags ); if( rc==SQLITE_OK && pPager->pBackup ){ @@ -3179,21 +3388,20 @@ static int pagerPagecount(Pager *pPager, Pgno *pnPage){ */ assert( pPager->eState==PAGER_OPEN ); assert( pPager->eLock>=SHARED_LOCK ); + assert( isOpen(pPager->fd) ); + assert( pPager->tempFile==0 ); nPage = sqlite3WalDbsize(pPager->pWal); /* If the number of pages in the database is not available from the - ** WAL sub-system, determine the page counte based on the size of + ** WAL sub-system, determine the page count based on the size of ** the database file. If the size of the database file is not an ** integer multiple of the page-size, round up the result. */ - if( nPage==0 ){ + if( nPage==0 && ALWAYS(isOpen(pPager->fd)) ){ i64 n = 0; /* Size of db file in bytes */ - assert( isOpen(pPager->fd) || pPager->tempFile ); - if( isOpen(pPager->fd) ){ - int rc = sqlite3OsFileSize(pPager->fd, &n); - if( rc!=SQLITE_OK ){ - return rc; - } + int rc = sqlite3OsFileSize(pPager->fd, &n); + if( rc!=SQLITE_OK ){ + return rc; } nPage = (Pgno)((n+pPager->pageSize-1) / pPager->pageSize); } @@ -3213,7 +3421,7 @@ static int pagerPagecount(Pager *pPager, Pgno *pnPage){ #ifndef SQLITE_OMIT_WAL /* ** Check if the *-wal file that corresponds to the database opened by pPager -** exists if the database is not empy, or verify that the *-wal file does +** exists if the database is not empty, or verify that the *-wal file does ** not exist (by deleting it) if the database file is empty. ** ** If the database is not empty and the *-wal file exists, open the pager @@ -3224,9 +3432,9 @@ static int pagerPagecount(Pager *pPager, Pgno *pnPage){ ** Return SQLITE_OK or an error code. ** ** The caller must hold a SHARED lock on the database file to call this -** function. Because an EXCLUSIVE lock on the db file is required to delete -** a WAL on a none-empty database, this ensures there is no race condition -** between the xAccess() below and an xDelete() being executed by some +** function. Because an EXCLUSIVE lock on the db file is required to delete +** a WAL on a none-empty database, this ensures there is no race condition +** between the xAccess() below and an xDelete() being executed by some ** other connection. */ static int pagerOpenWalIfPresent(Pager *pPager){ @@ -3236,23 +3444,21 @@ static int pagerOpenWalIfPresent(Pager *pPager){ if( !pPager->tempFile ){ int isWal; /* True if WAL file exists */ - Pgno nPage; /* Size of the database file */ - - rc = pagerPagecount(pPager, &nPage); - if( rc ) return rc; - if( nPage==0 ){ - rc = sqlite3OsDelete(pPager->pVfs, pPager->zWal, 0); - if( rc==SQLITE_IOERR_DELETE_NOENT ) rc = SQLITE_OK; - isWal = 0; - }else{ - rc = sqlite3OsAccess( - pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &isWal - ); - } + rc = sqlite3OsAccess( + pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &isWal + ); if( rc==SQLITE_OK ){ if( isWal ){ - testcase( sqlite3PcachePagecount(pPager->pPCache)==0 ); - rc = sqlite3PagerOpenWal(pPager, 0); + Pgno nPage; /* Size of the database file */ + + rc = pagerPagecount(pPager, &nPage); + if( rc ) return rc; + if( nPage==0 ){ + rc = sqlite3OsDelete(pPager->pVfs, pPager->zWal, 0); + }else{ + testcase( sqlite3PcachePagecount(pPager->pPCache)==0 ); + rc = sqlite3PagerOpenWal(pPager, 0); + } }else if( pPager->journalMode==PAGER_JOURNALMODE_WAL ){ pPager->journalMode = PAGER_JOURNALMODE_DELETE; } @@ -3264,21 +3470,21 @@ static int pagerOpenWalIfPresent(Pager *pPager){ /* ** Playback savepoint pSavepoint. Or, if pSavepoint==NULL, then playback -** the entire master journal file. The case pSavepoint==NULL occurs when -** a ROLLBACK TO command is invoked on a SAVEPOINT that is a transaction +** the entire super-journal file. The case pSavepoint==NULL occurs when +** a ROLLBACK TO command is invoked on a SAVEPOINT that is a transaction ** savepoint. ** -** When pSavepoint is not NULL (meaning a non-transaction savepoint is +** When pSavepoint is not NULL (meaning a non-transaction savepoint is ** being rolled back), then the rollback consists of up to three stages, ** performed in the order specified: ** ** * Pages are played back from the main journal starting at byte -** offset PagerSavepoint.iOffset and continuing to +** offset PagerSavepoint.iOffset and continuing to ** PagerSavepoint.iHdrOffset, or to the end of the main journal ** file if PagerSavepoint.iHdrOffset is zero. ** ** * If PagerSavepoint.iHdrOffset is not zero, then pages are played -** back starting from the journal header immediately following +** back starting from the journal header immediately following ** PagerSavepoint.iHdrOffset to the end of the main journal file. ** ** * Pages are then played back from the sub-journal file, starting @@ -3294,7 +3500,7 @@ static int pagerOpenWalIfPresent(Pager *pPager){ ** journal file. There is no need for a bitvec in this case. ** ** In either case, before playback commences the Pager.dbSize variable -** is reset to the value that it held at the start of the savepoint +** is reset to the value that it held at the start of the savepoint ** (or transaction). No page with a page-number greater than this value ** is played back. If one is encountered it is simply skipped. */ @@ -3311,11 +3517,11 @@ static int pagerPlaybackSavepoint(Pager *pPager, PagerSavepoint *pSavepoint){ if( pSavepoint ){ pDone = sqlite3BitvecCreate(pSavepoint->nOrig); if( !pDone ){ - return SQLITE_NOMEM; + return SQLITE_NOMEM_BKPT; } } - /* Set the database size back to the value it was before the savepoint + /* Set the database size back to the value it was before the savepoint ** being reverted was opened. */ pPager->dbSize = pSavepoint ? pSavepoint->nOrig : pPager->dbOrigSize; @@ -3368,7 +3574,7 @@ static int pagerPlaybackSavepoint(Pager *pPager, PagerSavepoint *pSavepoint){ ** test is related to ticket #2565. See the discussion in the ** pager_playback() function for additional information. */ - if( nJRec==0 + if( nJRec==0 && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){ nJRec = (u32)((szJ - pPager->journalOff)/JOURNAL_PG_SZ(pPager)); @@ -3432,6 +3638,7 @@ static void pagerFixMaplimit(Pager *pPager){ sqlite3_int64 sz; sz = pPager->szMmap; pPager->bUseFetch = (sz>0); + setGetterMethod(pPager); sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_MMAP_SIZE, &sz); } #endif @@ -3458,7 +3665,7 @@ void sqlite3PagerShrink(Pager *pPager){ ** The "level" in pgFlags & PAGER_SYNCHRONOUS_MASK sets the robustness ** of the database to damage due to OS crashes or power failures by ** changing the number of syncs()s when writing the journals. -** There are three levels: +** There are four levels: ** ** OFF sqlite3OsSync() is never called. This is the default ** for temporary and transient files. @@ -3478,6 +3685,10 @@ void sqlite3PagerShrink(Pager *pPager){ ** assurance that the journal will not be corrupted to the ** point of causing damage to the database during rollback. ** +** EXTRA This is like FULL except that is also syncs the directory +** that contains the rollback journal after the rollback +** journal is unlinked. +** ** The above is for a rollback-journal mode. For WAL mode, OFF continues ** to mean that no syncs ever occur. NORMAL means that the WAL is synced ** prior to the start of checkpoint and that the database file is synced @@ -3485,7 +3696,8 @@ void sqlite3PagerShrink(Pager *pPager){ ** was written back into the database. But no sync operations occur for ** an ordinary commit in NORMAL mode with WAL. FULL means that the WAL ** file is synced following each commit operation, in addition to the -** syncs associated with NORMAL. +** syncs associated with NORMAL. There is no difference between FULL +** and EXTRA for WAL mode. ** ** Do not confuse synchronous=FULL with SQLITE_SYNC_FULL. The ** SQLITE_SYNC_FULL macro means to use the MacOSX-style full-fsync @@ -3498,37 +3710,46 @@ void sqlite3PagerShrink(Pager *pPager){ ** Numeric values associated with these states are OFF==1, NORMAL=2, ** and FULL=3. */ -#ifndef SQLITE_OMIT_PAGER_PRAGMAS void sqlite3PagerSetFlags( Pager *pPager, /* The pager to set safety level for */ unsigned pgFlags /* Various flags */ ){ unsigned level = pgFlags & PAGER_SYNCHRONOUS_MASK; - if( pPager->tempFile ){ + if( pPager->tempFile || level==PAGER_SYNCHRONOUS_OFF ){ pPager->noSync = 1; pPager->fullSync = 0; pPager->extraSync = 0; }else{ - pPager->noSync = level==PAGER_SYNCHRONOUS_OFF ?1:0; + pPager->noSync = 0; pPager->fullSync = level>=PAGER_SYNCHRONOUS_FULL ?1:0; - pPager->extraSync = level==PAGER_SYNCHRONOUS_EXTRA ?1:0; + + /* Set Pager.extraSync if "PRAGMA synchronous=EXTRA" is requested, or + ** if the file-system supports F2FS style atomic writes. If this flag + ** is set, SQLite syncs the directory to disk immediately after deleting + ** a journal file in "PRAGMA journal_mode=DELETE" mode. */ + if( level==PAGER_SYNCHRONOUS_EXTRA +#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE + || (sqlite3OsDeviceCharacteristics(pPager->fd) & SQLITE_IOCAP_BATCH_ATOMIC) +#endif + ){ + pPager->extraSync = 1; + }else{ + pPager->extraSync = 0; + } } if( pPager->noSync ){ pPager->syncFlags = 0; - pPager->ckptSyncFlags = 0; }else if( pgFlags & PAGER_FULLFSYNC ){ pPager->syncFlags = SQLITE_SYNC_FULL; - pPager->ckptSyncFlags = SQLITE_SYNC_FULL; - }else if( pgFlags & PAGER_CKPT_FULLFSYNC ){ - pPager->syncFlags = SQLITE_SYNC_NORMAL; - pPager->ckptSyncFlags = SQLITE_SYNC_FULL; }else{ pPager->syncFlags = SQLITE_SYNC_NORMAL; - pPager->ckptSyncFlags = SQLITE_SYNC_NORMAL; } - pPager->walSyncFlags = pPager->syncFlags; + pPager->walSyncFlags = (pPager->syncFlags<<2); if( pPager->fullSync ){ - pPager->walSyncFlags |= WAL_SYNC_TRANSACTIONS; + pPager->walSyncFlags |= pPager->syncFlags; + } + if( (pgFlags & PAGER_CKPT_FULLFSYNC) && !pPager->noSync ){ + pPager->walSyncFlags |= (SQLITE_SYNC_FULL<<2); } if( pgFlags & PAGER_CACHESPILL ){ pPager->doNotSpill &= ~SPILLFLAG_OFF; @@ -3536,12 +3757,11 @@ void sqlite3PagerSetFlags( pPager->doNotSpill |= SPILLFLAG_OFF; } } -#endif /* ** The following global variable is incremented whenever the library ** attempts to open a temporary file. This information is used for -** testing and analysis only. +** testing and analysis only. */ #ifdef SQLITE_TEST int sqlite3_opentemp_count = 0; @@ -3550,8 +3770,8 @@ int sqlite3_opentemp_count = 0; /* ** Open a temporary file. ** -** Write the file descriptor into *pFile. Return SQLITE_OK on success -** or some other error code if we fail. The OS will automatically +** Write the file descriptor into *pFile. Return SQLITE_OK on success +** or some other error code if we fail. The OS will automatically ** delete the temporary file when it is closed. ** ** The flags passed to the VFS layer xOpen() call are those specified @@ -3583,9 +3803,9 @@ static int pagerOpentemp( /* ** Set the busy handler function. ** -** The pager invokes the busy-handler if sqlite3OsLock() returns +** The pager invokes the busy-handler if sqlite3OsLock() returns ** SQLITE_BUSY when trying to upgrade from no-lock to a SHARED lock, -** or when trying to upgrade from a RESERVED lock to an EXCLUSIVE +** or when trying to upgrade from a RESERVED lock to an EXCLUSIVE ** lock. It does *not* invoke the busy handler when upgrading from ** SHARED to RESERVED, or when upgrading from SHARED to EXCLUSIVE ** (which occurs during hot-journal rollback). Summary: @@ -3597,37 +3817,35 @@ static int pagerOpentemp( ** SHARED_LOCK -> EXCLUSIVE_LOCK | No ** RESERVED_LOCK -> EXCLUSIVE_LOCK | Yes ** -** If the busy-handler callback returns non-zero, the lock is +** If the busy-handler callback returns non-zero, the lock is ** retried. If it returns zero, then the SQLITE_BUSY error is ** returned to the caller of the pager API function. */ -void sqlite3PagerSetBusyhandler( +void sqlite3PagerSetBusyHandler( Pager *pPager, /* Pager object */ int (*xBusyHandler)(void *), /* Pointer to busy-handler function */ void *pBusyHandlerArg /* Argument to pass to xBusyHandler */ ){ + void **ap; pPager->xBusyHandler = xBusyHandler; pPager->pBusyHandlerArg = pBusyHandlerArg; - - if( isOpen(pPager->fd) ){ - void **ap = (void **)&pPager->xBusyHandler; - assert( ((int(*)(void *))(ap[0]))==xBusyHandler ); - assert( ap[1]==pBusyHandlerArg ); - sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_BUSYHANDLER, (void *)ap); - } + ap = (void **)&pPager->xBusyHandler; + assert( ((int(*)(void *))(ap[0]))==xBusyHandler ); + assert( ap[1]==pBusyHandlerArg ); + sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_BUSYHANDLER, (void *)ap); } /* -** Change the page size used by the Pager object. The new page size +** Change the page size used by the Pager object. The new page size ** is passed in *pPageSize. ** ** If the pager is in the error state when this function is called, it -** is a no-op. The value returned is the error state error code (i.e. +** is a no-op. The value returned is the error state error code (i.e. ** one of SQLITE_IOERR, an SQLITE_IOERR_xxx sub-code or SQLITE_FULL). ** ** Otherwise, if all of the following are true: ** -** * the new page size (value of *pPageSize) is valid (a power +** * the new page size (value of *pPageSize) is valid (a power ** of two between 512 and SQLITE_MAX_PAGE_SIZE, inclusive), and ** ** * there are no outstanding page references, and @@ -3637,14 +3855,14 @@ void sqlite3PagerSetBusyhandler( ** ** then the pager object page size is set to *pPageSize. ** -** If the page size is changed, then this function uses sqlite3PagerMalloc() -** to obtain a new Pager.pTmpSpace buffer. If this allocation attempt -** fails, SQLITE_NOMEM is returned and the page size remains unchanged. +** If the page size is changed, then this function uses sqlite3PagerMalloc() +** to obtain a new Pager.pTmpSpace buffer. If this allocation attempt +** fails, SQLITE_NOMEM is returned and the page size remains unchanged. ** In all other cases, SQLITE_OK is returned. ** ** If the page size is not changed, either because one of the enumerated ** conditions above is not true, the pager was in error state when this -** function was called, or because the memory allocation attempt failed, +** function was called, or because the memory allocation attempt failed, ** then *pPageSize is set to the old, retained page size before returning. */ int sqlite3PagerSetPagesize(Pager *pPager, u32 *pPageSize, int nReserve){ @@ -3654,7 +3872,7 @@ int sqlite3PagerSetPagesize(Pager *pPager, u32 *pPageSize, int nReserve){ ** function may be called from within PagerOpen(), before the state ** of the Pager object is internally consistent. ** - ** At one point this function returned an error if the pager was in + ** At one point this function returned an error if the pager was in ** PAGER_ERROR state. But since PAGER_ERROR state guarantees that ** there is at least one outstanding page reference, this function ** is a no-op for that case anyhow. @@ -3663,8 +3881,8 @@ int sqlite3PagerSetPagesize(Pager *pPager, u32 *pPageSize, int nReserve){ u32 pageSize = *pPageSize; assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) ); if( (pPager->memDb==0 || pPager->dbSize==0) - && sqlite3PcacheRefCount(pPager->pPCache)==0 - && pageSize && pageSize!=(u32)pPager->pageSize + && sqlite3PcacheRefCount(pPager->pPCache)==0 + && pageSize && pageSize!=(u32)pPager->pageSize ){ char *pNew = NULL; /* New temp space */ i64 nByte = 0; @@ -3673,8 +3891,14 @@ int sqlite3PagerSetPagesize(Pager *pPager, u32 *pPageSize, int nReserve){ rc = sqlite3OsFileSize(pPager->fd, &nByte); } if( rc==SQLITE_OK ){ - pNew = (char *)sqlite3PageMalloc(pageSize); - if( !pNew ) rc = SQLITE_NOMEM; + /* 8 bytes of zeroed overrun space is sufficient so that the b-tree + * cell header parser will never run off the end of the allocation */ + pNew = (char *)sqlite3PageMalloc(pageSize+8); + if( !pNew ){ + rc = SQLITE_NOMEM_BKPT; + }else{ + memset(pNew+pageSize, 0, 8); + } } if( rc==SQLITE_OK ){ @@ -3686,6 +3910,7 @@ int sqlite3PagerSetPagesize(Pager *pPager, u32 *pPageSize, int nReserve){ pPager->pTmpSpace = pNew; pPager->dbSize = (Pgno)((nByte+pageSize-1)/pageSize); pPager->pageSize = pageSize; + pPager->lckPgno = (Pgno)(PENDING_BYTE/pageSize) + 1; }else{ sqlite3PageFree(pNew); } @@ -3715,18 +3940,21 @@ void *sqlite3PagerTempSpace(Pager *pPager){ } /* -** Attempt to set the maximum database page count if mxPage is positive. +** Attempt to set the maximum database page count if mxPage is positive. ** Make no changes if mxPage is zero or negative. And never reduce the ** maximum page count below the current size of the database. ** ** Regardless of mxPage, return the current maximum page count. */ -int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){ +Pgno sqlite3PagerMaxPageCount(Pager *pPager, Pgno mxPage){ if( mxPage>0 ){ pPager->mxPgno = mxPage; } assert( pPager->eState!=PAGER_OPEN ); /* Called only by OP_MaxPgcnt */ - assert( pPager->mxPgno>=pPager->dbSize ); /* OP_MaxPgcnt enforces this */ + /* assert( pPager->mxPgno>=pPager->dbSize ); */ + /* OP_MaxPgcnt ensures that the parameter passed to this function is not + ** less than the total number of valid pages in the database. But this + ** may be less than Pager.dbSize, and so the assert() above is not valid */ return pPager->mxPgno; } @@ -3756,11 +3984,11 @@ void enable_simulated_io_errors(void){ /* ** Read the first N bytes from the beginning of the file into memory -** that pDest points to. +** that pDest points to. ** ** If the pager was opened on a transient file (zFilename==""), or ** opened on a file less than N bytes in size, the output buffer is -** zeroed and SQLITE_OK returned. The rationale for this is that this +** zeroed and SQLITE_OK returned. The rationale for this is that this ** function is used to read database headers, and a new transient or ** zero sized database has a header than consists entirely of zeroes. ** @@ -3793,7 +4021,7 @@ int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){ ** This function may only be called when a read-transaction is open on ** the pager. It returns the total number of pages in the database. ** -** However, if the file is between 1 and bytes in size, then +** However, if the file is between 1 and bytes in size, then ** this is considered a 1 page file. */ void sqlite3PagerPagecount(Pager *pPager, int *pnPage){ @@ -3808,19 +4036,19 @@ void sqlite3PagerPagecount(Pager *pPager, int *pnPage){ ** a similar or greater lock is already held, this function is a no-op ** (returning SQLITE_OK immediately). ** -** Otherwise, attempt to obtain the lock using sqlite3OsLock(). Invoke -** the busy callback if the lock is currently not available. Repeat -** until the busy callback returns false or until the attempt to +** Otherwise, attempt to obtain the lock using sqlite3OsLock(). Invoke +** the busy callback if the lock is currently not available. Repeat +** until the busy callback returns false or until the attempt to ** obtain the lock succeeds. ** ** Return SQLITE_OK on success and an error code if we cannot obtain -** the lock. If the lock is obtained successfully, set the Pager.state +** the lock. If the lock is obtained successfully, set the Pager.state ** variable to locktype before returning. */ static int pager_wait_on_lock(Pager *pPager, int locktype){ int rc; /* Return code */ - /* Check that this is either a no-op (because the requested lock is + /* Check that this is either a no-op (because the requested lock is ** already held), or one of the transitions that the busy-handler ** may be invoked during, according to the comment above ** sqlite3PagerSetBusyhandler(). @@ -3837,15 +4065,14 @@ static int pager_wait_on_lock(Pager *pPager, int locktype){ } /* -** Function assertTruncateConstraint(pPager) checks that one of the +** Function assertTruncateConstraint(pPager) checks that one of the ** following is true for all dirty pages currently in the page-cache: ** -** a) The page number is less than or equal to the size of the +** a) The page number is less than or equal to the size of the ** current database image, in pages, OR ** ** b) if the page content were written at this time, it would not -** be necessary to write the current content out to the sub-journal -** (as determined by function subjRequiresPage()). +** be necessary to write the current content out to the sub-journal. ** ** If the condition asserted by this function were not true, and the ** dirty page were to be discarded from the cache via the pagerStress() @@ -3853,15 +4080,23 @@ static int pager_wait_on_lock(Pager *pPager, int locktype){ ** the database file. If a savepoint transaction were rolled back after ** this happened, the correct behavior would be to restore the current ** content of the page. However, since this content is not present in either -** the database file or the portion of the rollback journal and +** the database file or the portion of the rollback journal and ** sub-journal rolled back the content could not be restored and the -** database image would become corrupt. It is therefore fortunate that +** database image would become corrupt. It is therefore fortunate that ** this circumstance cannot arise. */ #if defined(SQLITE_DEBUG) static void assertTruncateConstraintCb(PgHdr *pPg){ + Pager *pPager = pPg->pPager; assert( pPg->flags&PGHDR_DIRTY ); - assert( !subjRequiresPage(pPg) || pPg->pgno<=pPg->pPager->dbSize ); + if( pPg->pgno>pPager->dbSize ){ /* if (a) is false */ + Pgno pgno = pPg->pgno; + int i; + for(i=0; ipPager->nSavepoint; i++){ + PagerSavepoint *p = &pPager->aSavepoint[i]; + assert( p->nOrigpInSavepoint,pgno) ); + } + } } static void assertTruncateConstraint(Pager *pPager){ sqlite3PcacheIterateDirty(pPager->pPCache, assertTruncateConstraintCb); @@ -3871,9 +4106,9 @@ static void assertTruncateConstraint(Pager *pPager){ #endif /* -** Truncate the in-memory database file image to nPage pages. This -** function does not actually modify the database file on disk. It -** just sets the internal state of the pager object so that the +** Truncate the in-memory database file image to nPage pages. This +** function does not actually modify the database file on disk. It +** just sets the internal state of the pager object so that the ** truncation will be done when the current transaction is committed. ** ** This function is only called right before committing a transaction. @@ -3882,17 +4117,17 @@ static void assertTruncateConstraint(Pager *pPager){ ** then continue writing to the database. */ void sqlite3PagerTruncateImage(Pager *pPager, Pgno nPage){ - assert( pPager->dbSize>=nPage ); + assert( pPager->dbSize>=nPage || CORRUPT_DB ); assert( pPager->eState>=PAGER_WRITER_CACHEMOD ); pPager->dbSize = nPage; /* At one point the code here called assertTruncateConstraint() to ** ensure that all pages being truncated away by this operation are, - ** if one or more savepoints are open, present in the savepoint + ** if one or more savepoints are open, present in the savepoint ** journal so that they can be restored if the savepoint is rolled ** back. This is no longer necessary as this function is now only - ** called right before committing a transaction. So although the - ** Pager object may still have open savepoints (Pager.nSavepoint!=0), + ** called right before committing a transaction. So although the + ** Pager object may still have open savepoints (Pager.nSavepoint!=0), ** they cannot be rolled back. So the assertTruncateConstraint() call ** is no longer correct. */ } @@ -3904,12 +4139,12 @@ void sqlite3PagerTruncateImage(Pager *pPager, Pgno nPage){ ** size of the journal file so that the pager_playback() routine knows ** that the entire journal file has been synced. ** -** Syncing a hot-journal to disk before attempting to roll it back ensures +** Syncing a hot-journal to disk before attempting to roll it back ensures ** that if a power-failure occurs during the rollback, the process that ** attempts rollback following system recovery sees the same journal ** content as this process. ** -** If everything goes as planned, SQLITE_OK is returned. Otherwise, +** If everything goes as planned, SQLITE_OK is returned. Otherwise, ** an SQLite error code. */ static int pagerSyncHotJournal(Pager *pPager){ @@ -3923,8 +4158,9 @@ static int pagerSyncHotJournal(Pager *pPager){ return rc; } +#if SQLITE_MAX_MMAP_SIZE>0 /* -** Obtain a reference to a memory mapped page object for page number pgno. +** Obtain a reference to a memory mapped page object for page number pgno. ** The new object will use the pointer pData, obtained from xFetch(). ** If successful, set *ppPage to point to the new page reference ** and return SQLITE_OK. Otherwise, return an SQLite error code and set @@ -3940,19 +4176,21 @@ static int pagerAcquireMapPage( PgHdr **ppPage /* OUT: Acquired page object */ ){ PgHdr *p; /* Memory mapped page to return */ - + if( pPager->pMmapFreelist ){ *ppPage = p = pPager->pMmapFreelist; pPager->pMmapFreelist = p->pDirty; p->pDirty = 0; - memset(p->pExtra, 0, pPager->nExtra); + assert( pPager->nExtra>=8 ); + memset(p->pExtra, 0, 8); }else{ *ppPage = p = (PgHdr *)sqlite3MallocZero(sizeof(PgHdr) + pPager->nExtra); if( p==0 ){ sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1) * pPager->pageSize, pData); - return SQLITE_NOMEM; + return SQLITE_NOMEM_BKPT; } p->pExtra = (void *)&p[1]; + assert( EIGHT_BYTE_ALIGNMENT( p->pExtra ) ); p->flags = PGHDR_MMAP; p->nRef = 1; p->pPager = pPager; @@ -3970,9 +4208,10 @@ static int pagerAcquireMapPage( return SQLITE_OK; } +#endif /* -** Release a reference to page pPg. pPg must have been returned by an +** Release a reference to page pPg. pPg must have been returned by an ** earlier call to pagerAcquireMapPage(). */ static void pagerReleaseMapPage(PgHdr *pPg){ @@ -3997,6 +4236,30 @@ static void pagerFreeMapHdrs(Pager *pPager){ } } +/* Verify that the database file has not be deleted or renamed out from +** under the pager. Return SQLITE_OK if the database is still where it ought +** to be on disk. Return non-zero (SQLITE_READONLY_DBMOVED or some other error +** code from sqlite3OsAccess()) if the database has gone missing. +*/ +static int databaseIsUnmoved(Pager *pPager){ + int bHasMoved = 0; + int rc; + + if( pPager->tempFile ) return SQLITE_OK; + if( pPager->dbSize==0 ) return SQLITE_OK; + assert( pPager->zFilename && pPager->zFilename[0] ); + rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_HAS_MOVED, &bHasMoved); + if( rc==SQLITE_NOTFOUND ){ + /* If the HAS_MOVED file-control is unimplemented, assume that the file + ** has not been moved. That is the historical behavior of SQLite: prior to + ** version 3.8.3, it never checked */ + rc = SQLITE_OK; + }else if( rc==SQLITE_OK && bHasMoved ){ + rc = SQLITE_READONLY_DBMOVED; + } + return rc; +} + /* ** Shutdown the page cache. Free all memory and close all files. @@ -4008,13 +4271,13 @@ static void pagerFreeMapHdrs(Pager *pPager){ ** result in a coredump. ** ** This function always succeeds. If a transaction is active an attempt -** is made to roll it back. If an error occurs during the rollback +** is made to roll it back. If an error occurs during the rollback ** a hot journal may be left in the filesystem but no error is returned ** to the caller. */ -int sqlite3PagerClose(Pager *pPager){ - u8 *pTmp = (u8 *)pPager->pTmpSpace; - +int sqlite3PagerClose(Pager *pPager, sqlite3 *db){ + u8 *pTmp = (u8*)pPager->pTmpSpace; + assert( db || pagerUseWal(pPager)==0 ); assert( assert_pager_state(pPager) ); disable_simulated_io_errors(); sqlite3BeginBenignMalloc(); @@ -4022,16 +4285,25 @@ int sqlite3PagerClose(Pager *pPager){ /* pPager->errCode = 0; */ pPager->exclusiveMode = 0; #ifndef SQLITE_OMIT_WAL - sqlite3WalClose(pPager->pWal, pPager->ckptSyncFlags, pPager->pageSize, pTmp); - pPager->pWal = 0; + { + u8 *a = 0; + assert( db || pPager->pWal==0 ); + if( db && 0==(db->flags & SQLITE_NoCkptOnClose) + && SQLITE_OK==databaseIsUnmoved(pPager) + ){ + a = pTmp; + } + sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags, pPager->pageSize,a); + pPager->pWal = 0; + } #endif pager_reset(pPager); if( MEMDB ){ pager_unlock(pPager); }else{ /* If it is open, sync the journal file before calling UnlockAndRollback. - ** If this is not done, then an unsynced portion of the open journal - ** file may be played back into the database. If a power failure occurs + ** If this is not done, then an unsynced portion of the open journal + ** file may be played back into the database. If a power failure occurs ** while this is happening, the database could become corrupt. ** ** If an error occurs while trying to sync the journal, shift the pager @@ -4054,9 +4326,11 @@ int sqlite3PagerClose(Pager *pPager){ sqlite3PageFree(pTmp); sqlite3PcacheClose(pPager->pPCache); +/* BEGIN SQLCIPHER */ #ifdef SQLITE_HAS_CODEC if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec); #endif +/* END SQLCIPHER */ assert( !pPager->aSavepoint && !pPager->pInJournal ); assert( !isOpen(pPager->jfd) && !isOpen(pPager->sjfd) ); @@ -4087,7 +4361,7 @@ void sqlite3PagerRef(DbPage *pPg){ ** disk and can be restored in the event of a hot-journal rollback. ** ** If the Pager.noSync flag is set, then this function is a no-op. -** Otherwise, the actions required depend on the journal-mode and the +** Otherwise, the actions required depend on the journal-mode and the ** device characteristics of the file-system, as follows: ** ** * If the journal file is an in-memory journal file, no action need @@ -4099,7 +4373,7 @@ void sqlite3PagerRef(DbPage *pPg){ ** been written following it. If the pager is operating in full-sync ** mode, then the journal file is synced before this field is updated. ** -** * If the device does not support the SEQUENTIAL property, then +** * If the device does not support the SEQUENTIAL property, then ** journal file is synced. ** ** Or, in pseudo-code: @@ -4108,11 +4382,11 @@ void sqlite3PagerRef(DbPage *pPg){ ** if( NOT SAFE_APPEND ){ ** if( ) xSync(); ** -** } +** } ** if( NOT SEQUENTIAL ) xSync(); ** } ** -** If successful, this routine clears the PGHDR_NEED_SYNC flag of every +** If successful, this routine clears the PGHDR_NEED_SYNC flag of every ** page currently held in memory before returning SQLITE_OK. If an IO ** error is encountered, then the IO error code is returned to the caller. */ @@ -4140,10 +4414,10 @@ static int syncJournal(Pager *pPager, int newHdr){ ** mode, then the journal file may at this point actually be larger ** than Pager.journalOff bytes. If the next thing in the journal ** file happens to be a journal-header (written as part of the - ** previous connection's transaction), and a crash or power-failure - ** occurs after nRec is updated but before this connection writes - ** anything else to the journal file (or commits/rolls back its - ** transaction), then SQLite may become confused when doing the + ** previous connection's transaction), and a crash or power-failure + ** occurs after nRec is updated but before this connection writes + ** anything else to the journal file (or commits/rolls back its + ** transaction), then SQLite may become confused when doing the ** hot-journal rollback following recovery. It may roll back all ** of this connections data, then proceed to rolling back the old, ** out-of-date data that follows it. Database corruption. @@ -4153,7 +4427,7 @@ static int syncJournal(Pager *pPager, int newHdr){ ** byte to the start of it to prevent it from being recognized. ** ** Variable iNextHdrOffset is set to the offset at which this - ** problematic header will occur, if it exists. aMagic is used + ** problematic header will occur, if it exists. aMagic is used ** as a temporary buffer to inspect the first couple of bytes of ** the potential journal header. */ @@ -4180,7 +4454,7 @@ static int syncJournal(Pager *pPager, int newHdr){ ** it as a candidate for rollback. ** ** This is not required if the persistent media supports the - ** SAFE_APPEND property. Because in this case it is not possible + ** SAFE_APPEND property. Because in this case it is not possible ** for garbage data to be appended to the file, the nRec field ** is populated with 0xFFFFFFFF when the journal header is written ** and never needs to be updated. @@ -4200,7 +4474,7 @@ static int syncJournal(Pager *pPager, int newHdr){ if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){ PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager))); IOTRACE(("JSYNC %p\n", pPager)) - rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags| + rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags| (pPager->syncFlags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0) ); if( rc!=SQLITE_OK ) return rc; @@ -4217,8 +4491,8 @@ static int syncJournal(Pager *pPager, int newHdr){ } } - /* Unless the pager is in noSync mode, the journal file was just - ** successfully synced. Either way, clear the PGHDR_NEED_SYNC flag on + /* Unless the pager is in noSync mode, the journal file was just + ** successfully synced. Either way, clear the PGHDR_NEED_SYNC flag on ** all pages. */ sqlite3PcacheClearSyncFlags(pPager->pPCache); @@ -4238,9 +4512,9 @@ static int syncJournal(Pager *pPager, int newHdr){ ** is called. Before writing anything to the database file, this lock ** is upgraded to an EXCLUSIVE lock. If the lock cannot be obtained, ** SQLITE_BUSY is returned and no data is written to the database file. -** +** ** If the pager is a temp-file pager and the actual file-system file -** is not yet open, it is created and opened before any data is +** is not yet open, it is created and opened before any data is ** written out. ** ** Once the lock has been upgraded and, if necessary, the file opened, @@ -4255,7 +4529,7 @@ static int syncJournal(Pager *pPager, int newHdr){ ** in Pager.dbFileVers[] is updated to match the new value stored in ** the database file. ** -** If everything is successful, SQLITE_OK is returned. If an IO error +** If everything is successful, SQLITE_OK is returned. If an IO error ** occurs, an IO error code is returned. Or, if the EXCLUSIVE lock cannot ** be obtained, SQLITE_BUSY is returned. */ @@ -4264,8 +4538,9 @@ static int pager_write_pagelist(Pager *pPager, PgHdr *pList){ /* This function is only called for rollback pagers in WRITER_DBMOD state. */ assert( !pagerUseWal(pPager) ); - assert( pPager->eState==PAGER_WRITER_DBMOD ); + assert( pPager->tempFile || pPager->eState==PAGER_WRITER_DBMOD ); assert( pPager->eLock==EXCLUSIVE_LOCK ); + assert( isOpen(pPager->fd) || pList->pDirty==0 ); /* If the file is a temp-file has not yet been opened, open it now. It ** is not possible for rc to be other than SQLITE_OK if this branch @@ -4280,7 +4555,7 @@ static int pager_write_pagelist(Pager *pPager, PgHdr *pList){ ** file size will be. */ assert( rc!=SQLITE_OK || isOpen(pPager->fd) ); - if( rc==SQLITE_OK + if( rc==SQLITE_OK && pPager->dbHintSizedbSize && (pList->pDirty || pList->pgno>pPager->dbHintSize) ){ @@ -4302,20 +4577,20 @@ static int pager_write_pagelist(Pager *pPager, PgHdr *pList){ */ if( pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){ i64 offset = (pgno-1)*(i64)pPager->pageSize; /* Offset to write */ - char *pData; /* Data to write */ + char *pData; /* Data to write */ assert( (pList->flags&PGHDR_NEED_SYNC)==0 ); if( pList->pgno==1 ) pager_write_changecounter(pList); /* Encode the database */ - CODEC2(pPager, pList->pData, pgno, 6, return SQLITE_NOMEM, pData); + CODEC2(pPager, pList->pData, pgno, 6, return SQLITE_NOMEM_BKPT, pData); /* Write out the page data. */ rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset); /* If page 1 was just written, update Pager.dbFileVers to match - ** the value now stored in the database file. If writing this - ** page caused the database file to grow, update dbFileSize. + ** the value now stored in the database file. If writing this + ** page caused the database file to grow, update dbFileSize. */ if( pgno==1 ){ memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers)); @@ -4343,33 +4618,36 @@ static int pager_write_pagelist(Pager *pPager, PgHdr *pList){ } /* -** Ensure that the sub-journal file is open. If it is already open, this +** Ensure that the sub-journal file is open. If it is already open, this ** function is a no-op. ** -** SQLITE_OK is returned if everything goes according to plan. An -** SQLITE_IOERR_XXX error code is returned if a call to sqlite3OsOpen() +** SQLITE_OK is returned if everything goes according to plan. An +** SQLITE_IOERR_XXX error code is returned if a call to sqlite3OsOpen() ** fails. */ static int openSubJournal(Pager *pPager){ int rc = SQLITE_OK; if( !isOpen(pPager->sjfd) ){ + const int flags = SQLITE_OPEN_SUBJOURNAL | SQLITE_OPEN_READWRITE + | SQLITE_OPEN_CREATE | SQLITE_OPEN_EXCLUSIVE + | SQLITE_OPEN_DELETEONCLOSE; + int nStmtSpill = sqlite3Config.nStmtSpill; if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY || pPager->subjInMemory ){ - sqlite3MemJournalOpen(pPager->sjfd); - }else{ - rc = pagerOpentemp(pPager, pPager->sjfd, SQLITE_OPEN_SUBJOURNAL); + nStmtSpill = -1; } + rc = sqlite3JournalOpen(pPager->pVfs, 0, pPager->sjfd, flags, nStmtSpill); } return rc; } /* -** Append a record of the current state of page pPg to the sub-journal. +** Append a record of the current state of page pPg to the sub-journal. ** ** If successful, set the bit corresponding to pPg->pgno in the bitvecs ** for all open savepoints before returning. ** ** This function returns SQLITE_OK if everything is successful, an IO -** error code if the attempt to write to the sub-journal fails, or +** error code if the attempt to write to the sub-journal fails, or ** SQLITE_NOMEM if a malloc fails while setting a bit in a savepoint ** bitvec. */ @@ -4382,9 +4660,9 @@ static int subjournalPage(PgHdr *pPg){ assert( pPager->useJournal ); assert( isOpen(pPager->jfd) || pagerUseWal(pPager) ); assert( isOpen(pPager->sjfd) || pPager->nSubRec==0 ); - assert( pagerUseWal(pPager) - || pageInJournal(pPager, pPg) - || pPg->pgno>pPager->dbOrigSize + assert( pagerUseWal(pPager) + || pageInJournal(pPager, pPg) + || pPg->pgno>pPager->dbOrigSize ); rc = openSubJournal(pPager); @@ -4394,8 +4672,15 @@ static int subjournalPage(PgHdr *pPg){ void *pData = pPg->pData; i64 offset = (i64)pPager->nSubRec*(4+pPager->pageSize); char *pData2; - - CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2); + +/* BEGIN SQLCIPHER */ +#if SQLITE_HAS_CODEC + if( !pPager->subjInMemory ){ + CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM_BKPT, pData2); + }else +#endif +/* END SQLCIPHER */ + pData2 = pData; PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno)); rc = write32bits(pPager->sjfd, offset, pPg->pgno); if( rc==SQLITE_OK ){ @@ -4422,14 +4707,14 @@ static int subjournalPageIfRequired(PgHdr *pPg){ ** This function is called by the pcache layer when it has reached some ** soft memory limit. The first argument is a pointer to a Pager object ** (cast as a void*). The pager is always 'purgeable' (not an in-memory -** database). The second argument is a reference to a page that is +** database). The second argument is a reference to a page that is ** currently dirty but has no outstanding references. The page -** is always associated with the Pager object passed as the first +** is always associated with the Pager object passed as the first ** argument. ** ** The job of this function is to make pPg clean by writing its contents ** out to the database file, if possible. This may involve syncing the -** journal file. +** journal file. ** ** If successful, sqlite3PcacheMakeClean() is called on the page and ** SQLITE_OK returned. If an IO error occurs while trying to make the @@ -4454,7 +4739,7 @@ static int pagerStress(void *p, PgHdr *pPg){ ** a rollback or by user request, respectively. ** ** Spilling is also prohibited when in an error state since that could - ** lead to database corruption. In the current implementation it + ** lead to database corruption. In the current implementation it ** is impossible for sqlite3PcacheFetch() to be called with createFlag==3 ** while in the error state, hence it is impossible for this routine to ** be called in the error state. Nevertheless, we include a NEVER() @@ -4471,22 +4756,30 @@ static int pagerStress(void *p, PgHdr *pPg){ return SQLITE_OK; } + pPager->aStat[PAGER_STAT_SPILL]++; pPg->pDirty = 0; if( pagerUseWal(pPager) ){ /* Write a single frame for this page to the log. */ - rc = subjournalPageIfRequired(pPg); + rc = subjournalPageIfRequired(pPg); if( rc==SQLITE_OK ){ rc = pagerWalFrames(pPager, pPg, 0, 0); } }else{ - + +#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE + if( pPager->tempFile==0 ){ + rc = sqlite3JournalCreate(pPager->jfd); + if( rc!=SQLITE_OK ) return pager_error(pPager, rc); + } +#endif + /* Sync the journal file if required. */ - if( pPg->flags&PGHDR_NEED_SYNC + if( pPg->flags&PGHDR_NEED_SYNC || pPager->eState==PAGER_WRITER_CACHEMOD ){ rc = syncJournal(pPager, 1); } - + /* Write the contents of the page out to the database file. */ if( rc==SQLITE_OK ){ assert( (pPg->flags&PGHDR_NEED_SYNC)==0 ); @@ -4500,7 +4793,7 @@ static int pagerStress(void *p, PgHdr *pPg){ sqlite3PcacheMakeClean(pPg); } - return pager_error(pPager, rc); + return pager_error(pPager, rc); } /* @@ -4531,26 +4824,28 @@ int sqlite3PagerFlush(Pager *pPager){ ** The zFilename argument is the path to the database file to open. ** If zFilename is NULL then a randomly-named temporary file is created ** and used as the file to be cached. Temporary files are be deleted -** automatically when they are closed. If zFilename is ":memory:" then -** all information is held in cache. It is never written to disk. +** automatically when they are closed. If zFilename is ":memory:" then +** all information is held in cache. It is never written to disk. ** This can be used to implement an in-memory database. ** ** The nExtra parameter specifies the number of bytes of space allocated ** along with each page reference. This space is available to the user -** via the sqlite3PagerGetExtra() API. +** via the sqlite3PagerGetExtra() API. When a new page is allocated, the +** first 8 bytes of this space are zeroed but the remainder is uninitialized. +** (The extra space is used by btree as the MemPage object.) ** ** The flags argument is used to specify properties that affect the ** operation of the pager. It should be passed some bitwise combination ** of the PAGER_* flags. ** ** The vfsFlags parameter is a bitmask to pass to the flags parameter -** of the xOpen() method of the supplied VFS when opening files. +** of the xOpen() method of the supplied VFS when opening files. ** -** If the pager object is allocated and the specified file opened +** If the pager object is allocated and the specified file opened ** successfully, SQLITE_OK is returned and *ppPager set to point to ** the new pager object. If an error occurs, *ppPager is set to NULL ** and error code returned. This function may return SQLITE_NOMEM -** (sqlite3Malloc() is used to allocate memory), SQLITE_CANTOPEN or +** (sqlite3Malloc() is used to allocate memory), SQLITE_CANTOPEN or ** various SQLITE_IO_XXX errors. */ int sqlite3PagerOpen( @@ -4567,6 +4862,7 @@ int sqlite3PagerOpen( int rc = SQLITE_OK; /* Return code */ int tempFile = 0; /* True for temp files (incl. in-memory files) */ int memDb = 0; /* True if this is an in-memory file */ + int memJM = 0; /* Memory journal mode */ int readOnly = 0; /* True if this is a read-only file */ int journalFileSize; /* Bytes to allocate for each journal fd */ char *zPathname = 0; /* Full path to database file */ @@ -4575,21 +4871,12 @@ int sqlite3PagerOpen( int pcacheSize = sqlite3PcacheSize(); /* Bytes to allocate for PCache */ u32 szPageDflt = SQLITE_DEFAULT_PAGE_SIZE; /* Default page size */ const char *zUri = 0; /* URI args to copy */ - int nUri = 0; /* Number of bytes of URI args at *zUri */ + int nUriByte = 1; /* Number of bytes of URI args at *zUri */ + /* Figure out how much space is required for each journal file-handle - ** (there are two of them, the main journal and the sub-journal). This - ** is the maximum space required for an in-memory journal file handle - ** and a regular journal file-handle. Note that a "regular journal-handle" - ** may be a wrapper capable of caching the first portion of the journal - ** file in memory to implement the atomic-write optimization (see - ** source file journal.c). - */ - if( sqlite3JournalSize(pVfs)>sqlite3MemJournalSize() ){ - journalFileSize = ROUND8(sqlite3JournalSize(pVfs)); - }else{ - journalFileSize = ROUND8(sqlite3MemJournalSize()); - } + ** (there are two of them, the main journal and the sub-journal). */ + journalFileSize = ROUND8(sqlite3JournalSize(pVfs)); /* Set the output variable to NULL in case an error occurs. */ *ppPager = 0; @@ -4599,7 +4886,7 @@ int sqlite3PagerOpen( memDb = 1; if( zFilename && zFilename[0] ){ zPathname = sqlite3DbStrDup(0, zFilename); - if( zPathname==0 ) return SQLITE_NOMEM; + if( zPathname==0 ) return SQLITE_NOMEM_BKPT; nPathname = sqlite3Strlen30(zPathname); zFilename = 0; } @@ -4612,21 +4899,30 @@ int sqlite3PagerOpen( */ if( zFilename && zFilename[0] ){ const char *z; - nPathname = pVfs->mxPathname+1; - zPathname = sqlite3DbMallocRaw(0, nPathname*2); + nPathname = pVfs->mxPathname + 1; + zPathname = sqlite3DbMallocRaw(0, 2*(i64)nPathname); if( zPathname==0 ){ - return SQLITE_NOMEM; + return SQLITE_NOMEM_BKPT; } zPathname[0] = 0; /* Make sure initialized even if FullPathname() fails */ rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname); + if( rc!=SQLITE_OK ){ + if( rc==SQLITE_OK_SYMLINK ){ + if( vfsFlags & SQLITE_OPEN_NOFOLLOW ){ + rc = SQLITE_CANTOPEN_SYMLINK; + }else{ + rc = SQLITE_OK; + } + } + } nPathname = sqlite3Strlen30(zPathname); z = zUri = &zFilename[sqlite3Strlen30(zFilename)+1]; while( *z ){ - z += sqlite3Strlen30(z)+1; - z += sqlite3Strlen30(z)+1; + z += strlen(z)+1; + z += strlen(z)+1; } - nUri = (int)(&z[1] - zUri); - assert( nUri>=0 ); + nUriByte = (int)(&z[1] - zUri); + assert( nUriByte>=1 ); if( rc==SQLITE_OK && nPathname+8>pVfs->mxPathname ){ /* This branch is taken when the journal path required by ** the database being opened will be more than pVfs->mxPathname @@ -4643,7 +4939,7 @@ int sqlite3PagerOpen( } /* Allocate memory for the Pager structure, PCache object, the - ** three file descriptors, the database file name and the journal + ** three file descriptors, the database file name and the journal ** file name. The layout in memory is as follows: ** ** Pager object (sizeof(Pager) bytes) @@ -4651,50 +4947,113 @@ int sqlite3PagerOpen( ** Database file handle (pVfs->szOsFile bytes) ** Sub-journal file handle (journalFileSize bytes) ** Main journal file handle (journalFileSize bytes) + ** Ptr back to the Pager (sizeof(Pager*) bytes) + ** \0\0\0\0 database prefix (4 bytes) ** Database file name (nPathname+1 bytes) - ** Journal file name (nPathname+8+1 bytes) + ** URI query parameters (nUriByte bytes) + ** Journal filename (nPathname+8+1 bytes) + ** WAL filename (nPathname+4+1 bytes) + ** \0\0\0 terminator (3 bytes) + ** + ** Some 3rd-party software, over which we have no control, depends on + ** the specific order of the filenames and the \0 separators between them + ** so that it can (for example) find the database filename given the WAL + ** filename without using the sqlite3_filename_database() API. This is a + ** misuse of SQLite and a bug in the 3rd-party software, but the 3rd-party + ** software is in widespread use, so we try to avoid changing the filename + ** order and formatting if possible. In particular, the details of the + ** filename format expected by 3rd-party software should be as follows: + ** + ** - Main Database Path + ** - \0 + ** - Multiple URI components consisting of: + ** - Key + ** - \0 + ** - Value + ** - \0 + ** - \0 + ** - Journal Path + ** - \0 + ** - WAL Path (zWALName) + ** - \0 + ** + ** The sqlite3_create_filename() interface and the databaseFilename() utility + ** that is used by sqlite3_filename_database() and kin also depend on the + ** specific formatting and order of the various filenames, so if the format + ** changes here, be sure to change it there as well. */ + assert( SQLITE_PTRSIZE==sizeof(Pager*) ); pPtr = (u8 *)sqlite3MallocZero( - ROUND8(sizeof(*pPager)) + /* Pager structure */ - ROUND8(pcacheSize) + /* PCache object */ - ROUND8(pVfs->szOsFile) + /* The main db file */ - journalFileSize * 2 + /* The two journal files */ - nPathname + 1 + nUri + /* zFilename */ - nPathname + 8 + 2 /* zJournal */ + ROUND8(sizeof(*pPager)) + /* Pager structure */ + ROUND8(pcacheSize) + /* PCache object */ + ROUND8(pVfs->szOsFile) + /* The main db file */ + (u64)journalFileSize * 2 + /* The two journal files */ + SQLITE_PTRSIZE + /* Space to hold a pointer */ + 4 + /* Database prefix */ + (u64)nPathname + 1 + /* database filename */ + (u64)nUriByte + /* query parameters */ + (u64)nPathname + 8 + 1 + /* Journal filename */ #ifndef SQLITE_OMIT_WAL - + nPathname + 4 + 2 /* zWal */ + (u64)nPathname + 4 + 1 + /* WAL filename */ #endif + 3 /* Terminator */ ); assert( EIGHT_BYTE_ALIGNMENT(SQLITE_INT_TO_PTR(journalFileSize)) ); if( !pPtr ){ sqlite3DbFree(0, zPathname); - return SQLITE_NOMEM; - } - pPager = (Pager*)(pPtr); - pPager->pPCache = (PCache*)(pPtr += ROUND8(sizeof(*pPager))); - pPager->fd = (sqlite3_file*)(pPtr += ROUND8(pcacheSize)); - pPager->sjfd = (sqlite3_file*)(pPtr += ROUND8(pVfs->szOsFile)); - pPager->jfd = (sqlite3_file*)(pPtr += journalFileSize); - pPager->zFilename = (char*)(pPtr += journalFileSize); + return SQLITE_NOMEM_BKPT; + } + pPager = (Pager*)pPtr; pPtr += ROUND8(sizeof(*pPager)); + pPager->pPCache = (PCache*)pPtr; pPtr += ROUND8(pcacheSize); + pPager->fd = (sqlite3_file*)pPtr; pPtr += ROUND8(pVfs->szOsFile); + pPager->sjfd = (sqlite3_file*)pPtr; pPtr += journalFileSize; + pPager->jfd = (sqlite3_file*)pPtr; pPtr += journalFileSize; assert( EIGHT_BYTE_ALIGNMENT(pPager->jfd) ); + memcpy(pPtr, &pPager, SQLITE_PTRSIZE); pPtr += SQLITE_PTRSIZE; + + /* Fill in the Pager.zFilename and pPager.zQueryParam fields */ + pPtr += 4; /* Skip zero prefix */ + pPager->zFilename = (char*)pPtr; + if( nPathname>0 ){ + memcpy(pPtr, zPathname, nPathname); pPtr += nPathname + 1; + if( zUri ){ + memcpy(pPtr, zUri, nUriByte); pPtr += nUriByte; + }else{ + pPtr++; + } + } + + + /* Fill in Pager.zJournal */ + if( nPathname>0 ){ + pPager->zJournal = (char*)pPtr; + memcpy(pPtr, zPathname, nPathname); pPtr += nPathname; + memcpy(pPtr, "-journal",8); pPtr += 8 + 1; +#ifdef SQLITE_ENABLE_8_3_NAMES + sqlite3FileSuffix3(zFilename,pPager->zJournal); + pPtr = (u8*)(pPager->zJournal + sqlite3Strlen30(pPager->zJournal)+1); +#endif + }else{ + pPager->zJournal = 0; + } - /* Fill in the Pager.zFilename and Pager.zJournal buffers, if required. */ - if( zPathname ){ - assert( nPathname>0 ); - pPager->zJournal = (char*)(pPtr += nPathname + 1 + nUri); - memcpy(pPager->zFilename, zPathname, nPathname); - if( nUri ) memcpy(&pPager->zFilename[nPathname+1], zUri, nUri); - memcpy(pPager->zJournal, zPathname, nPathname); - memcpy(&pPager->zJournal[nPathname], "-journal\000", 8+2); - sqlite3FileSuffix3(pPager->zFilename, pPager->zJournal); #ifndef SQLITE_OMIT_WAL - pPager->zWal = &pPager->zJournal[nPathname+8+1]; - memcpy(pPager->zWal, zPathname, nPathname); - memcpy(&pPager->zWal[nPathname], "-wal\000", 4+1); - sqlite3FileSuffix3(pPager->zFilename, pPager->zWal); + /* Fill in Pager.zWal */ + if( nPathname>0 ){ + pPager->zWal = (char*)pPtr; + memcpy(pPtr, zPathname, nPathname); pPtr += nPathname; + memcpy(pPtr, "-wal", 4); pPtr += 4 + 1; +#ifdef SQLITE_ENABLE_8_3_NAMES + sqlite3FileSuffix3(zFilename, pPager->zWal); + pPtr = (u8*)(pPager->zWal + sqlite3Strlen30(pPager->zWal)+1); #endif - sqlite3DbFree(0, zPathname); + }else{ + pPager->zWal = 0; } +#endif + (void)pPtr; /* Suppress warning about unused pPtr value */ + + if( nPathname ) sqlite3DbFree(0, zPathname); pPager->pVfs = pVfs; pPager->vfsFlags = vfsFlags; @@ -4704,7 +5063,8 @@ int sqlite3PagerOpen( int fout = 0; /* VFS flags returned by xOpen() */ rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout); assert( !memDb ); - readOnly = (fout&SQLITE_OPEN_READONLY); + pPager->memVfs = memJM = (fout&SQLITE_OPEN_MEMORY)!=0; + readOnly = (fout&SQLITE_OPEN_READONLY)!=0; /* If the file was successfully opened for read/write access, ** choose a default page size in case we have to create the @@ -4740,9 +5100,9 @@ int sqlite3PagerOpen( } #endif } - pPager->noLock = sqlite3_uri_boolean(zFilename, "nolock", 0); + pPager->noLock = sqlite3_uri_boolean(pPager->zFilename, "nolock", 0); if( (iDc & SQLITE_IOCAP_IMMUTABLE)!=0 - || sqlite3_uri_boolean(zFilename, "immutable", 0) ){ + || sqlite3_uri_boolean(pPager->zFilename, "immutable", 0) ){ vfsFlags |= SQLITE_OPEN_READONLY; goto act_like_temp_file; } @@ -4757,7 +5117,7 @@ int sqlite3PagerOpen( ** disk and uses an in-memory rollback journal. ** ** This branch also runs for files marked as immutable. - */ + */ act_like_temp_file: tempFile = 1; pPager->eState = PAGER_READER; /* Pretend we already have a lock */ @@ -4766,7 +5126,7 @@ int sqlite3PagerOpen( readOnly = (vfsFlags&SQLITE_OPEN_READONLY); } - /* The following call to PagerSetPagesize() serves to set the value of + /* The following call to PagerSetPagesize() serves to set the value of ** Pager.pageSize and to allocate the Pager.pTmpSpace buffer. */ if( rc==SQLITE_OK ){ @@ -4777,8 +5137,8 @@ int sqlite3PagerOpen( /* Initialize the PCache object. */ if( rc==SQLITE_OK ){ - assert( nExtra<1000 ); nExtra = ROUND8(nExtra); + assert( nExtra>=8 && nExtra<1000 ); rc = sqlite3PcacheOpen(szPageDflt, nExtra, !memDb, !memDb?pagerStress:0, (void *)pPager, pPager->pPCache); } @@ -4806,32 +5166,15 @@ int sqlite3PagerOpen( /* pPager->state = PAGER_UNLOCK; */ /* pPager->errMask = 0; */ pPager->tempFile = (u8)tempFile; - assert( tempFile==PAGER_LOCKINGMODE_NORMAL + assert( tempFile==PAGER_LOCKINGMODE_NORMAL || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE ); assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 ); - pPager->exclusiveMode = (u8)tempFile; + pPager->exclusiveMode = (u8)tempFile; pPager->changeCountDone = pPager->tempFile; pPager->memDb = (u8)memDb; pPager->readOnly = (u8)readOnly; assert( useJournal || pPager->tempFile ); - pPager->noSync = pPager->tempFile; - if( pPager->noSync ){ - assert( pPager->fullSync==0 ); - assert( pPager->extraSync==0 ); - assert( pPager->syncFlags==0 ); - assert( pPager->walSyncFlags==0 ); - assert( pPager->ckptSyncFlags==0 ); - }else{ - pPager->fullSync = 1; -#if SQLITE_EXTRA_DURABLE - pPager->extraSync = 1; -#else - pPager->extraSync = 0; -#endif - pPager->syncFlags = SQLITE_SYNC_NORMAL; - pPager->walSyncFlags = SQLITE_SYNC_NORMAL | WAL_SYNC_TRANSACTIONS; - pPager->ckptSyncFlags = SQLITE_SYNC_NORMAL; - } + sqlite3PagerSetFlags(pPager, (SQLITE_DEFAULT_SYNCHRONOUS+1)|PAGER_CACHESPILL); /* pPager->pFirst = 0; */ /* pPager->pFirstSynced = 0; */ /* pPager->pLast = 0; */ @@ -4841,12 +5184,13 @@ int sqlite3PagerOpen( setSectorSize(pPager); if( !useJournal ){ pPager->journalMode = PAGER_JOURNALMODE_OFF; - }else if( memDb ){ + }else if( memDb || memJM ){ pPager->journalMode = PAGER_JOURNALMODE_MEMORY; } /* pPager->xBusyHandler = 0; */ /* pPager->pBusyHandlerArg = 0; */ pPager->xReiniter = xReinit; + setGetterMethod(pPager); /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */ /* pPager->szMmap = SQLITE_DEFAULT_MMAP_SIZE // will be set by btree.c */ @@ -4854,36 +5198,28 @@ int sqlite3PagerOpen( return SQLITE_OK; } - -/* Verify that the database file has not be deleted or renamed out from -** under the pager. Return SQLITE_OK if the database is still were it ought -** to be on disk. Return non-zero (SQLITE_READONLY_DBMOVED or some other error -** code from sqlite3OsAccess()) if the database has gone missing. +/* +** Return the sqlite3_file for the main database given the name +** of the corresponding WAL or Journal name as passed into +** xOpen. */ -static int databaseIsUnmoved(Pager *pPager){ - int bHasMoved = 0; - int rc; - - if( pPager->tempFile ) return SQLITE_OK; - if( pPager->dbSize==0 ) return SQLITE_OK; - assert( pPager->zFilename && pPager->zFilename[0] ); - rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_HAS_MOVED, &bHasMoved); - if( rc==SQLITE_NOTFOUND ){ - /* If the HAS_MOVED file-control is unimplemented, assume that the file - ** has not been moved. That is the historical behavior of SQLite: prior to - ** version 3.8.3, it never checked */ - rc = SQLITE_OK; - }else if( rc==SQLITE_OK && bHasMoved ){ - rc = SQLITE_READONLY_DBMOVED; +sqlite3_file *sqlite3_database_file_object(const char *zName){ + Pager *pPager; + const char *p; + while( zName[-1]!=0 || zName[-2]!=0 || zName[-3]!=0 || zName[-4]!=0 ){ + zName--; } - return rc; + p = zName - 4 - sizeof(Pager*); + assert( EIGHT_BYTE_ALIGNMENT(p) ); + pPager = *(Pager**)p; + return pPager->fd; } /* ** This function is called after transitioning from PAGER_UNLOCK to ** PAGER_SHARED state. It tests if there is a hot journal present in -** the file-system for the given pager. A hot journal is one that +** the file-system for the given pager. A hot journal is one that ** needs to be played back. According to this function, a hot-journal ** file exists if the following criteria are met: ** @@ -4898,14 +5234,14 @@ static int databaseIsUnmoved(Pager *pPager){ ** just deleted using OsDelete, *pExists is set to 0 and SQLITE_OK ** is returned. ** -** This routine does not check if there is a master journal filename -** at the end of the file. If there is, and that master journal file +** This routine does not check if there is a super-journal filename +** at the end of the file. If there is, and that super-journal file ** does not exist, then the journal file is not really hot. In this ** case this routine will return a false-positive. The pager_playback() -** routine will discover that the journal file is not really hot and -** will not roll it back. +** routine will discover that the journal file is not really hot and +** will not roll it back. ** -** If a hot-journal file is found to exist, *pExists is set to 1 and +** If a hot-journal file is found to exist, *pExists is set to 1 and ** SQLITE_OK returned. If no hot-journal file is present, *pExists is ** set to 0 and SQLITE_OK returned. If an IO error occurs while trying ** to determine whether or not a hot-journal file exists, the IO error @@ -4933,7 +5269,7 @@ static int hasHotJournal(Pager *pPager, int *pExists){ int locked = 0; /* True if some process holds a RESERVED lock */ /* Race condition here: Another process might have been holding the - ** the RESERVED lock and have a journal open at the sqlite3OsAccess() + ** the RESERVED lock and have a journal open at the sqlite3OsAccess() ** call above, but then delete the journal and drop the lock before ** we get to the following sqlite3OsCheckReservedLock() call. If that ** is the case, this routine might think there is a hot journal when @@ -4944,6 +5280,7 @@ static int hasHotJournal(Pager *pPager, int *pExists){ if( rc==SQLITE_OK && !locked ){ Pgno nPage; /* Number of pages in database file */ + assert( pPager->tempFile==0 ); rc = pagerPagecount(pPager, &nPage); if( rc==SQLITE_OK ){ /* If the database is zero pages in size, that means that either (1) the @@ -4965,7 +5302,7 @@ static int hasHotJournal(Pager *pPager, int *pExists){ /* The journal file exists and no other connection has a reserved ** or greater lock on the database file. Now check that there is ** at least one non-zero bytes at the start of the journal file. - ** If there is, then we consider this journal to be hot. If not, + ** If there is, then we consider this journal to be hot. If not, ** it can be ignored. */ if( !jrnlOpen ){ @@ -5015,7 +5352,7 @@ static int hasHotJournal(Pager *pPager, int *pExists){ ** on the database file), then an attempt is made to obtain a ** SHARED lock on the database file. Immediately after obtaining ** the SHARED lock, the file-system is checked for a hot-journal, -** which is played back if present. Following any hot-journal +** which is played back if present. Following any hot-journal ** rollback, the contents of the cache are validated by checking ** the 'change-counter' field of the database file header and ** discarded if they are found to be invalid. @@ -5026,8 +5363,8 @@ static int hasHotJournal(Pager *pPager, int *pExists){ ** the contents of the page cache and rolling back any open journal ** file. ** -** If everything is successful, SQLITE_OK is returned. If an IO error -** occurs while locking the database, checking for a hot-journal file or +** If everything is successful, SQLITE_OK is returned. If an IO error +** occurs while locking the database, checking for a hot-journal file or ** rolling back a journal file, the IO error code is returned. */ int sqlite3PagerSharedLock(Pager *pPager){ @@ -5035,18 +5372,18 @@ int sqlite3PagerSharedLock(Pager *pPager){ /* This routine is only called from b-tree and only when there are no ** outstanding pages. This implies that the pager state should either - ** be OPEN or READER. READER is only possible if the pager is or was in - ** exclusive access mode. - */ + ** be OPEN or READER. READER is only possible if the pager is or was in + ** exclusive access mode. */ assert( sqlite3PcacheRefCount(pPager->pPCache)==0 ); assert( assert_pager_state(pPager) ); assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER ); - if( NEVER(MEMDB && pPager->errCode) ){ return pPager->errCode; } + assert( pPager->errCode==SQLITE_OK ); if( !pagerUseWal(pPager) && pPager->eState==PAGER_OPEN ){ int bHotJournal = 1; /* True if there exists a hot journal-file */ assert( !MEMDB ); + assert( pPager->tempFile==0 || pPager->eLock==EXCLUSIVE_LOCK ); rc = pager_wait_on_lock(pPager, SHARED_LOCK); if( rc!=SQLITE_OK ){ @@ -5073,12 +5410,12 @@ int sqlite3PagerSharedLock(Pager *pPager){ ** important that a RESERVED lock is not obtained on the way to the ** EXCLUSIVE lock. If it were, another process might open the ** database file, detect the RESERVED lock, and conclude that the - ** database is safe to read while this process is still rolling the + ** database is safe to read while this process is still rolling the ** hot-journal back. - ** + ** ** Because the intermediate RESERVED lock is not requested, any - ** other process attempting to access the database file will get to - ** this point in the code and fail to obtain its own EXCLUSIVE lock + ** other process attempting to access the database file will get to + ** this point in the code and fail to obtain its own EXCLUSIVE lock ** on the database file. ** ** Unless the pager is in locking_mode=exclusive mode, the lock is @@ -5088,21 +5425,21 @@ int sqlite3PagerSharedLock(Pager *pPager){ if( rc!=SQLITE_OK ){ goto failed; } - - /* If it is not already open and the file exists on disk, open the - ** journal for read/write access. Write access is required because - ** in exclusive-access mode the file descriptor will be kept open - ** and possibly used for a transaction later on. Also, write-access - ** is usually required to finalize the journal in journal_mode=persist + + /* If it is not already open and the file exists on disk, open the + ** journal for read/write access. Write access is required because + ** in exclusive-access mode the file descriptor will be kept open + ** and possibly used for a transaction later on. Also, write-access + ** is usually required to finalize the journal in journal_mode=persist ** mode (and also for journal_mode=truncate on some systems). ** - ** If the journal does not exist, it usually means that some - ** other connection managed to get in and roll it back before - ** this connection obtained the exclusive lock above. Or, it + ** If the journal does not exist, it usually means that some + ** other connection managed to get in and roll it back before + ** this connection obtained the exclusive lock above. Or, it ** may mean that the pager was in the error-state when this ** function was called and the journal file does not exist. */ - if( !isOpen(pPager->jfd) ){ + if( !isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){ sqlite3_vfs * const pVfs = pPager->pVfs; int bExists; /* True if journal file exists */ rc = sqlite3OsAccess( @@ -5119,7 +5456,7 @@ int sqlite3PagerSharedLock(Pager *pPager){ } } } - + /* Playback and delete the journal. Drop the database write ** lock and reacquire the read lock. Purge the cache before ** playing back the hot-journal so that we don't end up with @@ -5132,7 +5469,7 @@ int sqlite3PagerSharedLock(Pager *pPager){ assert( rc==SQLITE_OK ); rc = pagerSyncHotJournal(pPager); if( rc==SQLITE_OK ){ - rc = pager_playback(pPager, 1); + rc = pager_playback(pPager, !pPager->tempFile); pPager->eState = PAGER_OPEN; } }else if( !pPager->exclusiveMode ){ @@ -5144,8 +5481,8 @@ int sqlite3PagerSharedLock(Pager *pPager){ ** or roll back a hot-journal while holding an EXCLUSIVE lock. The ** pager_unlock() routine will be called before returning to unlock ** the file. If the unlock attempt fails, then Pager.eLock must be - ** set to UNKNOWN_LOCK (see the comment above the #define for - ** UNKNOWN_LOCK above for an explanation). + ** set to UNKNOWN_LOCK (see the comment above the #define for + ** UNKNOWN_LOCK above for an explanation). ** ** In order to get pager_unlock() to do this, set Pager.eState to ** PAGER_ERROR now. This is not actually counted as a transition @@ -5153,7 +5490,7 @@ int sqlite3PagerSharedLock(Pager *pPager){ ** since we know that the same call to pager_unlock() will very ** shortly transition the pager object to the OPEN state. Calling ** assert_pager_state() would fail now, as it should not be possible - ** to be in ERROR state when there are zero outstanding page + ** to be in ERROR state when there are zero outstanding page ** references. */ pager_error(pPager, rc); @@ -5178,24 +5515,19 @@ int sqlite3PagerSharedLock(Pager *pPager){ ** a 32-bit counter that is incremented with each change. The ** other bytes change randomly with each file change when ** a codec is in use. - ** - ** There is a vanishingly small chance that a change will not be + ** + ** There is a vanishingly small chance that a change will not be ** detected. The chance of an undetected change is so small that ** it can be neglected. */ - Pgno nPage = 0; char dbFileVers[sizeof(pPager->dbFileVers)]; - rc = pagerPagecount(pPager, &nPage); - if( rc ) goto failed; - - if( nPage>0 ){ - IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers))); - rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24); - if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){ + IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers))); + rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24); + if( rc!=SQLITE_OK ){ + if( rc!=SQLITE_IOERR_SHORT_READ ){ goto failed; } - }else{ memset(dbFileVers, 0, sizeof(dbFileVers)); } @@ -5228,7 +5560,7 @@ int sqlite3PagerSharedLock(Pager *pPager){ rc = pagerBeginReadTransaction(pPager); } - if( pPager->eState==PAGER_OPEN && rc==SQLITE_OK ){ + if( pPager->tempFile==0 && pPager->eState==PAGER_OPEN && rc==SQLITE_OK ){ rc = pagerPagecount(pPager, &pPager->dbSize); } @@ -5251,37 +5583,45 @@ int sqlite3PagerSharedLock(Pager *pPager){ ** Except, in locking_mode=EXCLUSIVE when there is nothing to in ** the rollback journal, the unlock is not performed and there is ** nothing to rollback, so this routine is a no-op. -*/ +*/ static void pagerUnlockIfUnused(Pager *pPager){ - if( pPager->nMmapOut==0 && (sqlite3PcacheRefCount(pPager->pPCache)==0) ){ + if( sqlite3PcacheRefCount(pPager->pPCache)==0 ){ + assert( pPager->nMmapOut==0 ); /* because page1 is never memory mapped */ pagerUnlockAndRollback(pPager); } } /* -** Acquire a reference to page number pgno in pager pPager (a page -** reference has type DbPage*). If the requested reference is +** The page getter methods each try to acquire a reference to a +** page with page number pgno. If the requested reference is ** successfully obtained, it is copied to *ppPage and SQLITE_OK returned. ** -** If the requested page is already in the cache, it is returned. +** There are different implementations of the getter method depending +** on the current state of the pager. +** +** getPageNormal() -- The normal getter +** getPageError() -- Used if the pager is in an error state +** getPageMmap() -- Used if memory-mapped I/O is enabled +** +** If the requested page is already in the cache, it is returned. ** Otherwise, a new page object is allocated and populated with data ** read from the database file. In some cases, the pcache module may ** choose not to allocate a new page object and may reuse an existing ** object with no outstanding references. ** -** The extra data appended to a page is always initialized to zeros the -** first time a page is loaded into memory. If the page requested is +** The extra data appended to a page is always initialized to zeros the +** first time a page is loaded into memory. If the page requested is ** already in the cache when this function is called, then the extra ** data is left as it was when the page object was last used. ** -** If the database image is smaller than the requested page or if a -** non-zero value is passed as the noContent parameter and the -** requested page is not already stored in the cache, then no -** actual disk read occurs. In this case the memory image of the -** page is initialized to all zeros. +** If the database image is smaller than the requested page or if +** the flags parameter contains the PAGER_GET_NOCONTENT bit and the +** requested page is not already stored in the cache, then no +** actual disk read occurs. In this case the memory image of the +** page is initialized to all zeros. ** -** If noContent is true, it means that we do not care about the contents -** of the page. This occurs in two scenarios: +** If PAGER_GET_NOCONTENT is true, it means that we do not care about +** the contents of the page. This occurs in two scenarios: ** ** a) When reading a free-list leaf page from the database, and ** @@ -5289,8 +5629,8 @@ static void pagerUnlockIfUnused(Pager *pPager){ ** a new page into the cache to be filled with the data read ** from the savepoint journal. ** -** If noContent is true, then the data returned is zeroed instead of -** being read from the database. Additionally, the bits corresponding +** If PAGER_GET_NOCONTENT is true, then the data returned is zeroed instead +** of being read from the database. Additionally, the bits corresponding ** to pgno in Pager.pInJournal (bitvec of pages already written to the ** journal file) and the PagerSavepoint.pInSavepoint bitvecs of any open ** savepoints are set. This means if the page is made writable at any @@ -5308,136 +5648,75 @@ static void pagerUnlockIfUnused(Pager *pPager){ ** Since Lookup() never goes to disk, it never has to deal with locks ** or journal files. */ -int sqlite3PagerGet( +static int getPageNormal( Pager *pPager, /* The pager open on the database file */ Pgno pgno, /* Page number to fetch */ DbPage **ppPage, /* Write a pointer to the page here */ int flags /* PAGER_GET_XXX flags */ ){ int rc = SQLITE_OK; - PgHdr *pPg = 0; - u32 iFrame = 0; /* Frame to read from WAL file */ - const int noContent = (flags & PAGER_GET_NOCONTENT); - - /* It is acceptable to use a read-only (mmap) page for any page except - ** page 1 if there is no write-transaction open or the ACQUIRE_READONLY - ** flag was specified by the caller. And so long as the db is not a - ** temporary or in-memory database. */ - const int bMmapOk = (pgno>1 && USEFETCH(pPager) - && (pPager->eState==PAGER_READER || (flags & PAGER_GET_READONLY)) -#ifdef SQLITE_HAS_CODEC - && pPager->xCodec==0 -#endif - ); + PgHdr *pPg; + u8 noContent; /* True if PAGER_GET_NOCONTENT is set */ + sqlite3_pcache_page *pBase; - /* Optimization note: Adding the "pgno<=1" term before "pgno==0" here - ** allows the compiler optimizer to reuse the results of the "pgno>1" - ** test in the previous statement, and avoid testing pgno==0 in the - ** common case where pgno is large. */ - if( pgno<=1 && pgno==0 ){ - return SQLITE_CORRUPT_BKPT; - } + assert( pPager->errCode==SQLITE_OK ); assert( pPager->eState>=PAGER_READER ); assert( assert_pager_state(pPager) ); - assert( noContent==0 || bMmapOk==0 ); - assert( pPager->hasHeldSharedLock==1 ); - /* If the pager is in the error state, return an error immediately. - ** Otherwise, request the page from the PCache layer. */ - if( pPager->errCode!=SQLITE_OK ){ - rc = pPager->errCode; - }else{ - if( bMmapOk && pagerUseWal(pPager) ){ - rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame); - if( rc!=SQLITE_OK ) goto pager_acquire_err; - } - - if( bMmapOk && iFrame==0 ){ - void *pData = 0; - - rc = sqlite3OsFetch(pPager->fd, - (i64)(pgno-1) * pPager->pageSize, pPager->pageSize, &pData - ); - - if( rc==SQLITE_OK && pData ){ - if( pPager->eState>PAGER_READER ){ - pPg = sqlite3PagerLookup(pPager, pgno); - } - if( pPg==0 ){ - rc = pagerAcquireMapPage(pPager, pgno, pData, &pPg); - }else{ - sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1)*pPager->pageSize, pData); - } - if( pPg ){ - assert( rc==SQLITE_OK ); - *ppPage = pPg; - return SQLITE_OK; - } - } - if( rc!=SQLITE_OK ){ - goto pager_acquire_err; - } - } - - { - sqlite3_pcache_page *pBase; - pBase = sqlite3PcacheFetch(pPager->pPCache, pgno, 3); - if( pBase==0 ){ - rc = sqlite3PcacheFetchStress(pPager->pPCache, pgno, &pBase); - if( rc!=SQLITE_OK ) goto pager_acquire_err; - if( pBase==0 ){ - pPg = *ppPage = 0; - rc = SQLITE_NOMEM; - goto pager_acquire_err; - } - } - pPg = *ppPage = sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pBase); - assert( pPg!=0 ); - } - } - - if( rc!=SQLITE_OK ){ - /* Either the call to sqlite3PcacheFetch() returned an error or the - ** pager was already in the error-state when this function was called. - ** Set pPg to 0 and jump to the exception handler. */ + if( pgno==0 ) return SQLITE_CORRUPT_BKPT; + pBase = sqlite3PcacheFetch(pPager->pPCache, pgno, 3); + if( pBase==0 ){ pPg = 0; - goto pager_acquire_err; + rc = sqlite3PcacheFetchStress(pPager->pPCache, pgno, &pBase); + if( rc!=SQLITE_OK ) goto pager_acquire_err; + if( pBase==0 ){ + rc = SQLITE_NOMEM_BKPT; + goto pager_acquire_err; + } } + pPg = *ppPage = sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pBase); assert( pPg==(*ppPage) ); assert( pPg->pgno==pgno ); assert( pPg->pPager==pPager || pPg->pPager==0 ); + noContent = (flags & PAGER_GET_NOCONTENT)!=0; if( pPg->pPager && !noContent ){ /* In this case the pcache already contains an initialized copy of ** the page. Return without further ado. */ - assert( pgno<=PAGER_MAX_PGNO && pgno!=PAGER_MJ_PGNO(pPager) ); + assert( pgno!=PAGER_SJ_PGNO(pPager) ); pPager->aStat[PAGER_STAT_HIT]++; return SQLITE_OK; }else{ - /* The pager cache has created a new page. Its content needs to - ** be initialized. */ - - pPg->pPager = pPager; - - /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page - ** number greater than this, or the unused locking-page, is requested. */ - if( pgno>PAGER_MAX_PGNO || pgno==PAGER_MJ_PGNO(pPager) ){ + /* The pager cache has created a new page. Its content needs to + ** be initialized. But first some error checks: + ** + ** (*) obsolete. Was: maximum page number is 2^31 + ** (2) Never try to fetch the locking page + */ + if( pgno==PAGER_SJ_PGNO(pPager) ){ rc = SQLITE_CORRUPT_BKPT; goto pager_acquire_err; } - if( MEMDB || pPager->dbSizefd) ){ + pPg->pPager = pPager; + + assert( !isOpen(pPager->fd) || !MEMDB ); + if( !isOpen(pPager->fd) || pPager->dbSizepPager->mxPgno ){ rc = SQLITE_FULL; + if( pgno<=pPager->dbSize ){ + sqlite3PcacheRelease(pPg); + pPg = 0; + } goto pager_acquire_err; } if( noContent ){ /* Failure to set the bits in the InJournal bit-vectors is benign. - ** It merely means that we might do some extra work to journal a - ** page that does not need to be journaled. Nevertheless, be sure - ** to test the case where a malloc error occurs while trying to set + ** It merely means that we might do some extra work to journal a + ** page that does not need to be journaled. Nevertheless, be sure + ** to test the case where a malloc error occurs while trying to set ** a bit in a bit vector. */ sqlite3BeginBenignMalloc(); @@ -5452,20 +5731,15 @@ int sqlite3PagerGet( memset(pPg->pData, 0, pPager->pageSize); IOTRACE(("ZERO %p %d\n", pPager, pgno)); }else{ - if( pagerUseWal(pPager) && bMmapOk==0 ){ - rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame); - if( rc!=SQLITE_OK ) goto pager_acquire_err; - } assert( pPg->pPager==pPager ); pPager->aStat[PAGER_STAT_MISS]++; - rc = readDbPage(pPg, iFrame); + rc = readDbPage(pPg); if( rc!=SQLITE_OK ){ goto pager_acquire_err; } } pager_set_pagehash(pPg); } - return SQLITE_OK; pager_acquire_err: @@ -5474,20 +5748,133 @@ int sqlite3PagerGet( sqlite3PcacheDrop(pPg); } pagerUnlockIfUnused(pPager); + *ppPage = 0; + return rc; +} +#if SQLITE_MAX_MMAP_SIZE>0 +/* The page getter for when memory-mapped I/O is enabled */ +static int getPageMMap( + Pager *pPager, /* The pager open on the database file */ + Pgno pgno, /* Page number to fetch */ + DbPage **ppPage, /* Write a pointer to the page here */ + int flags /* PAGER_GET_XXX flags */ +){ + int rc = SQLITE_OK; + PgHdr *pPg = 0; + u32 iFrame = 0; /* Frame to read from WAL file */ + + /* It is acceptable to use a read-only (mmap) page for any page except + ** page 1 if there is no write-transaction open or the ACQUIRE_READONLY + ** flag was specified by the caller. And so long as the db is not a + ** temporary or in-memory database. */ + const int bMmapOk = (pgno>1 + && (pPager->eState==PAGER_READER || (flags & PAGER_GET_READONLY)) + ); + + assert( USEFETCH(pPager) ); +/* BEGIN SQLCIPHER */ +#ifdef SQLITE_HAS_CODEC + assert( pPager->xCodec==0 ); +#endif +/* END SQLCIPHER */ + + /* Optimization note: Adding the "pgno<=1" term before "pgno==0" here + ** allows the compiler optimizer to reuse the results of the "pgno>1" + ** test in the previous statement, and avoid testing pgno==0 in the + ** common case where pgno is large. */ + if( pgno<=1 && pgno==0 ){ + return SQLITE_CORRUPT_BKPT; + } + assert( pPager->eState>=PAGER_READER ); + assert( assert_pager_state(pPager) ); + assert( pPager->hasHeldSharedLock==1 ); + assert( pPager->errCode==SQLITE_OK ); + + if( bMmapOk && pagerUseWal(pPager) ){ + rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame); + if( rc!=SQLITE_OK ){ + *ppPage = 0; + return rc; + } + } + if( bMmapOk && iFrame==0 ){ + void *pData = 0; + rc = sqlite3OsFetch(pPager->fd, + (i64)(pgno-1) * pPager->pageSize, pPager->pageSize, &pData + ); + if( rc==SQLITE_OK && pData ){ + if( pPager->eState>PAGER_READER || pPager->tempFile ){ + pPg = sqlite3PagerLookup(pPager, pgno); + } + if( pPg==0 ){ + rc = pagerAcquireMapPage(pPager, pgno, pData, &pPg); + }else{ + sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1)*pPager->pageSize, pData); + } + if( pPg ){ + assert( rc==SQLITE_OK ); + *ppPage = pPg; + return SQLITE_OK; + } + } + if( rc!=SQLITE_OK ){ + *ppPage = 0; + return rc; + } + } + return getPageNormal(pPager, pgno, ppPage, flags); +} +#endif /* SQLITE_MAX_MMAP_SIZE>0 */ + +/* The page getter method for when the pager is an error state */ +static int getPageError( + Pager *pPager, /* The pager open on the database file */ + Pgno pgno, /* Page number to fetch */ + DbPage **ppPage, /* Write a pointer to the page here */ + int flags /* PAGER_GET_XXX flags */ +){ + UNUSED_PARAMETER(pgno); + UNUSED_PARAMETER(flags); + assert( pPager->errCode!=SQLITE_OK ); *ppPage = 0; + return pPager->errCode; +} + + +/* Dispatch all page fetch requests to the appropriate getter method. +*/ +int sqlite3PagerGet( + Pager *pPager, /* The pager open on the database file */ + Pgno pgno, /* Page number to fetch */ + DbPage **ppPage, /* Write a pointer to the page here */ + int flags /* PAGER_GET_XXX flags */ +){ +#if 0 /* Trace page fetch by setting to 1 */ + int rc; + printf("PAGE %u\n", pgno); + fflush(stdout); + rc = pPager->xGet(pPager, pgno, ppPage, flags); + if( rc ){ + printf("PAGE %u failed with 0x%02x\n", pgno, rc); + fflush(stdout); + } return rc; +#else + /* Normal, high-speed version of sqlite3PagerGet() */ + return pPager->xGet(pPager, pgno, ppPage, flags); +#endif } /* ** Acquire a page if it is already in the in-memory cache. Do ** not read the page from disk. Return a pointer to the page, -** or 0 if the page is not in cache. +** or 0 if the page is not in cache. ** ** See also sqlite3PagerGet(). The difference between this routine ** and sqlite3PagerGet() is that _get() will go to the disk and read ** in the page if the page is not already in cache. This routine -** returns NULL if the page is not in cache or if a disk I/O error +** returns NULL if the page is not in cache or if a disk I/O error ** has ever happened. */ DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){ @@ -5504,46 +5891,62 @@ DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){ /* ** Release a page reference. ** -** If the number of references to the page drop to zero, then the -** page is added to the LRU list. When all references to all pages -** are released, a rollback occurs and the lock on the database is -** removed. +** The sqlite3PagerUnref() and sqlite3PagerUnrefNotNull() may only be used +** if we know that the page being released is not the last reference to page1. +** The btree layer always holds page1 open until the end, so these first +** two routines can be used to release any page other than BtShared.pPage1. +** The assert() at tag-20230419-2 proves that this constraint is always +** honored. +** +** Use sqlite3PagerUnrefPageOne() to release page1. This latter routine +** checks the total number of outstanding pages and if the number of +** pages reaches zero it drops the database lock. */ void sqlite3PagerUnrefNotNull(DbPage *pPg){ - Pager *pPager; + TESTONLY( Pager *pPager = pPg->pPager; ) assert( pPg!=0 ); - pPager = pPg->pPager; if( pPg->flags & PGHDR_MMAP ){ + assert( pPg->pgno!=1 ); /* Page1 is never memory mapped */ pagerReleaseMapPage(pPg); }else{ sqlite3PcacheRelease(pPg); } - pagerUnlockIfUnused(pPager); + /* Do not use this routine to release the last reference to page1 */ + assert( sqlite3PcacheRefCount(pPager->pPCache)>0 ); /* tag-20230419-2 */ } void sqlite3PagerUnref(DbPage *pPg){ if( pPg ) sqlite3PagerUnrefNotNull(pPg); } +void sqlite3PagerUnrefPageOne(DbPage *pPg){ + Pager *pPager; + assert( pPg!=0 ); + assert( pPg->pgno==1 ); + assert( (pPg->flags & PGHDR_MMAP)==0 ); /* Page1 is never memory mapped */ + pPager = pPg->pPager; + sqlite3PcacheRelease(pPg); + pagerUnlockIfUnused(pPager); +} /* ** This function is called at the start of every write transaction. -** There must already be a RESERVED or EXCLUSIVE lock on the database +** There must already be a RESERVED or EXCLUSIVE lock on the database ** file when this routine is called. ** ** Open the journal file for pager pPager and write a journal header ** to the start of it. If there are active savepoints, open the sub-journal -** as well. This function is only used when the journal file is being -** opened to write a rollback log for a transaction. It is not used +** as well. This function is only used when the journal file is being +** opened to write a rollback log for a transaction. It is not used ** when opening a hot journal file to roll it back. ** ** If the journal file is already open (as it may be in exclusive mode), ** then this function just writes a journal header to the start of the -** already open file. +** already open file. ** ** Whether or not the journal file is opened by this function, the ** Pager.pInJournal bitvec structure is allocated. ** -** Return SQLITE_OK if everything is successful. Otherwise, return -** SQLITE_NOMEM if the attempt to allocate Pager.pInJournal fails, or +** Return SQLITE_OK if everything is successful. Otherwise, return +** SQLITE_NOMEM if the attempt to allocate Pager.pInJournal fails, or ** an IO error code if opening or writing the journal file fails. */ static int pager_open_journal(Pager *pPager){ @@ -5553,7 +5956,7 @@ static int pager_open_journal(Pager *pPager){ assert( pPager->eState==PAGER_WRITER_LOCKED ); assert( assert_pager_state(pPager) ); assert( pPager->pInJournal==0 ); - + /* If already in the error state, this function is a no-op. But on ** the other hand, this routine is never called if we are already in ** an error state. */ @@ -5562,46 +5965,47 @@ static int pager_open_journal(Pager *pPager){ if( !pagerUseWal(pPager) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){ pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize); if( pPager->pInJournal==0 ){ - return SQLITE_NOMEM; + return SQLITE_NOMEM_BKPT; } - + /* Open the journal file if it is not already open. */ if( !isOpen(pPager->jfd) ){ if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){ sqlite3MemJournalOpen(pPager->jfd); }else{ - const int flags = /* VFS flags to open journal file */ - SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE| - (pPager->tempFile ? - (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL): - (SQLITE_OPEN_MAIN_JOURNAL) - ); + int flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE; + int nSpill; + if( pPager->tempFile ){ + flags |= (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL); + flags |= SQLITE_OPEN_EXCLUSIVE; + nSpill = sqlite3Config.nStmtSpill; + }else{ + flags |= SQLITE_OPEN_MAIN_JOURNAL; + nSpill = jrnlBufferSize(pPager); + } + /* Verify that the database still has the same name as it did when ** it was originally opened. */ rc = databaseIsUnmoved(pPager); if( rc==SQLITE_OK ){ -#ifdef SQLITE_ENABLE_ATOMIC_WRITE - rc = sqlite3JournalOpen( - pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager) + rc = sqlite3JournalOpen ( + pVfs, pPager->zJournal, pPager->jfd, flags, nSpill ); -#else - rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0); -#endif } } assert( rc!=SQLITE_OK || isOpen(pPager->jfd) ); } - - - /* Write the first journal header to the journal file and open + + + /* Write the first journal header to the journal file and open ** the sub-journal if necessary. */ if( rc==SQLITE_OK ){ /* TODO: Check if all of these are really required. */ pPager->nRec = 0; pPager->journalOff = 0; - pPager->setMaster = 0; + pPager->setSuper = 0; pPager->journalHdr = 0; rc = writeJournalHdr(pPager); } @@ -5610,6 +6014,7 @@ static int pager_open_journal(Pager *pPager){ if( rc!=SQLITE_OK ){ sqlite3BitvecDestroy(pPager->pInJournal); pPager->pInJournal = 0; + pPager->journalOff = 0; }else{ assert( pPager->eState==PAGER_WRITER_LOCKED ); pPager->eState = PAGER_WRITER_CACHEMOD; @@ -5619,12 +6024,12 @@ static int pager_open_journal(Pager *pPager){ } /* -** Begin a write-transaction on the specified pager object. If a +** Begin a write-transaction on the specified pager object. If a ** write-transaction has already been opened, this function is a no-op. ** ** If the exFlag argument is false, then acquire at least a RESERVED ** lock on the database file. If exFlag is true, then acquire at least -** an EXCLUSIVE lock. If such a lock is already held, no locking +** an EXCLUSIVE lock. If such a lock is already held, no locking ** functions need be called. ** ** If the subjInMemory argument is non-zero, then any sub-journal opened @@ -5632,7 +6037,7 @@ static int pager_open_journal(Pager *pPager){ ** has no effect if the sub-journal is already opened (as it may be when ** running in exclusive mode) or if the transaction does not require a ** sub-journal. If the subjInMemory argument is zero, then any required -** sub-journal is implemented in-memory if pPager is an in-memory database, +** sub-journal is implemented in-memory if pPager is an in-memory database, ** or using a temporary file otherwise. */ int sqlite3PagerBegin(Pager *pPager, int exFlag, int subjInMemory){ @@ -5642,7 +6047,7 @@ int sqlite3PagerBegin(Pager *pPager, int exFlag, int subjInMemory){ assert( pPager->eState>=PAGER_READER && pPager->eStatesubjInMemory = (u8)subjInMemory; - if( ALWAYS(pPager->eState==PAGER_READER) ){ + if( pPager->eState==PAGER_READER ){ assert( pPager->pInJournal==0 ); if( pagerUseWal(pPager) ){ @@ -5680,9 +6085,9 @@ int sqlite3PagerBegin(Pager *pPager, int exFlag, int subjInMemory){ ** ** WAL mode sets Pager.eState to PAGER_WRITER_LOCKED or CACHEMOD ** when it has an open transaction, but never to DBMOD or FINISHED. - ** This is because in those states the code to roll back savepoint - ** transactions may copy data from the sub-journal into the database - ** file as well as into the page cache. Which would be incorrect in + ** This is because in those states the code to roll back savepoint + ** transactions may copy data from the sub-journal into the database + ** file as well as into the page cache. Which would be incorrect in ** WAL mode. */ pPager->eState = PAGER_WRITER_LOCKED; @@ -5714,10 +6119,10 @@ static SQLITE_NOINLINE int pagerAddPageToRollbackJournal(PgHdr *pPg){ /* We should never write to the journal file the page that ** contains the database locks. The following assert verifies ** that we do not. */ - assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) ); + assert( pPg->pgno!=PAGER_SJ_PGNO(pPager) ); assert( pPager->journalHdr<=pPager->journalOff ); - CODEC2(pPager, pPg->pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2); + CODEC2(pPager, pPg->pData, pPg->pgno, 7, return SQLITE_NOMEM_BKPT, pData2); cksum = pager_cksum(pPager, (u8*)pData2); /* Even if an IO or diskfull error occurs while journalling the @@ -5736,11 +6141,11 @@ static SQLITE_NOINLINE int pagerAddPageToRollbackJournal(PgHdr *pPg){ rc = write32bits(pPager->jfd, iOff+pPager->pageSize+4, cksum); if( rc!=SQLITE_OK ) return rc; - IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno, + IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno, pPager->journalOff, pPager->pageSize)); PAGER_INCR(sqlite3_pager_writej_count); PAGERTRACE(("JOURNAL %d page %d needSync=%d hash(%08x)\n", - PAGERID(pPager), pPg->pgno, + PAGERID(pPager), pPg->pgno, ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg))); pPager->journalOff += 8 + pPager->pageSize; @@ -5755,9 +6160,9 @@ static SQLITE_NOINLINE int pagerAddPageToRollbackJournal(PgHdr *pPg){ } /* -** Mark a single data page as writeable. The page is written into the +** Mark a single data page as writeable. The page is written into the ** main journal or sub-journal as required. If the page is written into -** one of the journals, the corresponding bit is set in the +** one of the journals, the corresponding bit is set in the ** Pager.pInJournal bitvec and the PagerSavepoint.pInSavepoint bitvecs ** of any open savepoints as appropriate. */ @@ -5765,7 +6170,7 @@ static int pager_write(PgHdr *pPg){ Pager *pPager = pPg->pPager; int rc = SQLITE_OK; - /* This routine is not called unless a write-transaction has already + /* This routine is not called unless a write-transaction has already ** been started. The journal file may or may not be open at this point. ** It is never called in the ERROR state. */ @@ -5782,7 +6187,7 @@ static int pager_write(PgHdr *pPg){ ** obtained the necessary locks to begin the write-transaction, but the ** rollback journal might not yet be open. Open it now if this is the case. ** - ** This is done before calling sqlite3PcacheMakeDirty() on the page. + ** This is done before calling sqlite3PcacheMakeDirty() on the page. ** Otherwise, if it were done after calling sqlite3PcacheMakeDirty(), then ** an error might occur and the pager would end up in WRITER_LOCKED state ** with pages marked as dirty in the cache. @@ -5827,7 +6232,7 @@ static int pager_write(PgHdr *pPg){ ** PGHDR_WRITEABLE bit that indicates that the page can be safely modified. */ pPg->flags |= PGHDR_WRITEABLE; - + /* If the statement journal is open and the page is not in it, ** then write the page into the statement journal. */ @@ -5893,7 +6298,7 @@ static SQLITE_NOINLINE int pagerWriteLargeSector(PgHdr *pPg){ Pgno pg = pg1+ii; PgHdr *pPage; if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){ - if( pg!=PAGER_MJ_PGNO(pPager) ){ + if( pg!=PAGER_SJ_PGNO(pPager) ){ rc = sqlite3PagerGet(pPager, pg, &pPage, 0); if( rc==SQLITE_OK ){ rc = pager_write(pPage); @@ -5911,7 +6316,7 @@ static SQLITE_NOINLINE int pagerWriteLargeSector(PgHdr *pPg){ } } - /* If the PGHDR_NEED_SYNC flag is set for any of the nPage pages + /* If the PGHDR_NEED_SYNC flag is set for any of the nPage pages ** starting at pg1, then it needs to be set for all of them. Because ** writing to any of these nPage pages may damage the others, the ** journal file must contain sync()ed copies of all of them @@ -5934,9 +6339,9 @@ static SQLITE_NOINLINE int pagerWriteLargeSector(PgHdr *pPg){ } /* -** Mark a data page as writeable. This routine must be called before -** making changes to a page. The caller must check the return value -** of this function and be careful not to change any page data unless +** Mark a data page as writeable. This routine must be called before +** making changes to a page. The caller must check the return value +** of this function and be careful not to change any page data unless ** this routine returns SQLITE_OK. ** ** The difference between this function and pager_write() is that this @@ -5952,12 +6357,13 @@ int sqlite3PagerWrite(PgHdr *pPg){ assert( (pPg->flags & PGHDR_MMAP)==0 ); assert( pPager->eState>=PAGER_WRITER_LOCKED ); assert( assert_pager_state(pPager) ); - if( pPager->errCode ){ - return pPager->errCode; - }else if( (pPg->flags & PGHDR_WRITEABLE)!=0 && pPager->dbSize>=pPg->pgno ){ + if( (pPg->flags & PGHDR_WRITEABLE)!=0 && pPager->dbSize>=pPg->pgno ){ if( pPager->nSavepoint ) return subjournalPageIfRequired(pPg); return SQLITE_OK; + }else if( pPager->errCode ){ + return pPager->errCode; }else if( pPager->sectorSize > (u32)pPager->pageSize ){ + assert( pPager->tempFile==0 ); return pagerWriteLargeSector(pPg); }else{ return pager_write(pPg); @@ -5986,32 +6392,39 @@ int sqlite3PagerIswriteable(DbPage *pPg){ ** on the given page is unused. The pager marks the page as clean so ** that it does not get written to disk. ** -** Tests show that this optimization can quadruple the speed of large +** Tests show that this optimization can quadruple the speed of large ** DELETE operations. +** +** This optimization cannot be used with a temp-file, as the page may +** have been dirty at the start of the transaction. In that case, if +** memory pressure forces page pPg out of the cache, the data does need +** to be written out to disk so that it may be read back in if the +** current transaction is rolled back. */ void sqlite3PagerDontWrite(PgHdr *pPg){ Pager *pPager = pPg->pPager; - if( (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){ + if( !pPager->tempFile && (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){ PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager))); IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno)) pPg->flags |= PGHDR_DONT_WRITE; pPg->flags &= ~PGHDR_WRITEABLE; + testcase( pPg->flags & PGHDR_NEED_SYNC ); pager_set_pagehash(pPg); } } /* -** This routine is called to increment the value of the database file -** change-counter, stored as a 4-byte big-endian integer starting at +** This routine is called to increment the value of the database file +** change-counter, stored as a 4-byte big-endian integer starting at ** byte offset 24 of the pager file. The secondary change counter at ** 92 is also updated, as is the SQLite version number at offset 96. ** ** But this only happens if the pPager->changeCountDone flag is false. ** To avoid excess churning of page 1, the update only happens once. -** See also the pager_write_changecounter() routine that does an +** See also the pager_write_changecounter() routine that does an ** unconditional update of the change counters. ** -** If the isDirectMode flag is zero, then this is done by calling +** If the isDirectMode flag is zero, then this is done by calling ** sqlite3PagerWrite() on page 1, then modifying the contents of the ** page data. In this case the file will be updated when the current ** transaction is committed. @@ -6019,7 +6432,7 @@ void sqlite3PagerDontWrite(PgHdr *pPg){ ** The isDirectMode flag may only be non-zero if the library was compiled ** with the SQLITE_ENABLE_ATOMIC_WRITE macro defined. In this case, ** if isDirect is non-zero, then the database file is updated directly -** by writing an updated version of page 1 using a call to the +** by writing an updated version of page 1 using a call to the ** sqlite3OsWrite() function. */ static int pager_incr_changecounter(Pager *pPager, int isDirectMode){ @@ -6048,7 +6461,7 @@ static int pager_incr_changecounter(Pager *pPager, int isDirectMode){ # define DIRECT_MODE isDirectMode #endif - if( !pPager->changeCountDone && ALWAYS(pPager->dbSize>0) ){ + if( !pPager->changeCountDone && pPager->dbSize>0 ){ PgHdr *pPgHdr; /* Reference to page 1 */ assert( !pPager->tempFile && isOpen(pPager->fd) ); @@ -6058,7 +6471,7 @@ static int pager_incr_changecounter(Pager *pPager, int isDirectMode){ assert( pPgHdr==0 || rc==SQLITE_OK ); /* If page one was fetched successfully, and this function is not - ** operating in direct-mode, make page 1 writable. When not in + ** operating in direct-mode, make page 1 writable. When not in ** direct mode, page 1 is always held in cache and hence the PagerGet() ** above is always successful - hence the ALWAYS on rc==SQLITE_OK. */ @@ -6074,7 +6487,7 @@ static int pager_incr_changecounter(Pager *pPager, int isDirectMode){ if( DIRECT_MODE ){ const void *zBuf; assert( pPager->dbFileSize>0 ); - CODEC2(pPager, pPgHdr->pData, 1, 6, rc=SQLITE_NOMEM, zBuf); + CODEC2(pPager, pPgHdr->pData, 1, 6, rc=SQLITE_NOMEM_BKPT, zBuf); if( rc==SQLITE_OK ){ rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0); pPager->aStat[PAGER_STAT_WRITE]++; @@ -6105,14 +6518,11 @@ static int pager_incr_changecounter(Pager *pPager, int isDirectMode){ ** If successful, or if called on a pager for which it is a no-op, this ** function returns SQLITE_OK. Otherwise, an IO error code is returned. */ -int sqlite3PagerSync(Pager *pPager, const char *zMaster){ +int sqlite3PagerSync(Pager *pPager, const char *zSuper){ int rc = SQLITE_OK; - - if( isOpen(pPager->fd) ){ - void *pArg = (void*)zMaster; - rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC, pArg); - if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK; - } + void *pArg = (void*)zSuper; + rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC, pArg); + if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK; if( rc==SQLITE_OK && !pPager->noSync ){ assert( !MEMDB ); rc = sqlite3OsSync(pPager->fd, pPager->syncFlags); @@ -6122,22 +6532,22 @@ int sqlite3PagerSync(Pager *pPager, const char *zMaster){ /* ** This function may only be called while a write-transaction is active in -** rollback. If the connection is in WAL mode, this call is a no-op. -** Otherwise, if the connection does not already have an EXCLUSIVE lock on +** rollback. If the connection is in WAL mode, this call is a no-op. +** Otherwise, if the connection does not already have an EXCLUSIVE lock on ** the database file, an attempt is made to obtain one. ** ** If the EXCLUSIVE lock is already held or the attempt to obtain it is ** successful, or the connection is in WAL mode, SQLITE_OK is returned. -** Otherwise, either SQLITE_BUSY or an SQLITE_IOERR_XXX error code is +** Otherwise, either SQLITE_BUSY or an SQLITE_IOERR_XXX error code is ** returned. */ int sqlite3PagerExclusiveLock(Pager *pPager){ int rc = pPager->errCode; assert( assert_pager_state(pPager) ); if( rc==SQLITE_OK ){ - assert( pPager->eState==PAGER_WRITER_CACHEMOD - || pPager->eState==PAGER_WRITER_DBMOD - || pPager->eState==PAGER_WRITER_LOCKED + assert( pPager->eState==PAGER_WRITER_CACHEMOD + || pPager->eState==PAGER_WRITER_DBMOD + || pPager->eState==PAGER_WRITER_LOCKED ); assert( assert_pager_state(pPager) ); if( 0==pagerUseWal(pPager) ){ @@ -6148,24 +6558,24 @@ int sqlite3PagerExclusiveLock(Pager *pPager){ } /* -** Sync the database file for the pager pPager. zMaster points to the name -** of a master journal file that should be written into the individual -** journal file. zMaster may be NULL, which is interpreted as no master -** journal (a single database transaction). +** Sync the database file for the pager pPager. zSuper points to the name +** of a super-journal file that should be written into the individual +** journal file. zSuper may be NULL, which is interpreted as no +** super-journal (a single database transaction). ** ** This routine ensures that: ** ** * The database file change-counter is updated, ** * the journal is synced (unless the atomic-write optimization is used), -** * all dirty pages are written to the database file, +** * all dirty pages are written to the database file, ** * the database file is truncated (if required), and -** * the database file synced. +** * the database file synced. ** -** The only thing that remains to commit the transaction is to finalize -** (delete, truncate or zero the first part of) the journal file (or -** delete the master journal file if specified). +** The only thing that remains to commit the transaction is to finalize +** (delete, truncate or zero the first part of) the journal file (or +** delete the super-journal file if specified). ** -** Note that if zMaster==NULL, this does not overwrite a previous value +** Note that if zSuper==NULL, this does not overwrite a previous value ** passed to an sqlite3PagerCommitPhaseOne() call. ** ** If the final parameter - noSync - is true, then the database file itself @@ -6175,7 +6585,7 @@ int sqlite3PagerExclusiveLock(Pager *pPager){ */ int sqlite3PagerCommitPhaseOne( Pager *pPager, /* Pager object */ - const char *zMaster, /* If not NULL, the master journal name */ + const char *zSuper, /* If not NULL, the super-journal name */ int noSync /* True to omit the xSync on the db file */ ){ int rc = SQLITE_OK; /* Return code */ @@ -6190,22 +6600,27 @@ int sqlite3PagerCommitPhaseOne( /* If a prior error occurred, report that error again. */ if( NEVER(pPager->errCode) ) return pPager->errCode; - PAGERTRACE(("DATABASE SYNC: File=%s zMaster=%s nSize=%d\n", - pPager->zFilename, zMaster, pPager->dbSize)); + /* Provide the ability to easily simulate an I/O error during testing */ + if( sqlite3FaultSim(400) ) return SQLITE_IOERR; + + PAGERTRACE(("DATABASE SYNC: File=%s zSuper=%s nSize=%d\n", + pPager->zFilename, zSuper, pPager->dbSize)); /* If no database changes have been made, return early. */ if( pPager->eStatetempFile ); + assert( isOpen(pPager->fd) || pPager->tempFile ); + if( 0==pagerFlushOnCommit(pPager, 1) ){ /* If this is an in-memory db, or no pages have been written to, or this ** function has already been called, it is mostly a no-op. However, any - ** backup in progress needs to be restarted. - */ + ** backup in progress needs to be restarted. */ sqlite3BackupRestart(pPager->pBackup); }else{ + PgHdr *pList; if( pagerUseWal(pPager) ){ - PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache); PgHdr *pPageOne = 0; + pList = sqlite3PcacheDirtyList(pPager->pPCache); if( pList==0 ){ /* Must have at least one page for the WAL commit flag. ** Ticket [2d1a5c67dfc2363e44f29d9bbd57f] 2011-05-18 */ @@ -6222,13 +6637,28 @@ int sqlite3PagerCommitPhaseOne( sqlite3PcacheCleanAll(pPager->pPCache); } }else{ + /* The bBatch boolean is true if the batch-atomic-write commit method + ** should be used. No rollback journal is created if batch-atomic-write + ** is enabled. + */ +#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE + sqlite3_file *fd = pPager->fd; + int bBatch = zSuper==0 /* An SQLITE_IOCAP_BATCH_ATOMIC commit */ + && (sqlite3OsDeviceCharacteristics(fd) & SQLITE_IOCAP_BATCH_ATOMIC) + && !pPager->noSync + && sqlite3JournalIsInMemory(pPager->jfd); +#else +# define bBatch 0 +#endif + +#ifdef SQLITE_ENABLE_ATOMIC_WRITE /* The following block updates the change-counter. Exactly how it ** does this depends on whether or not the atomic-update optimization - ** was enabled at compile time, and if this transaction meets the - ** runtime criteria to use the operation: + ** was enabled at compile time, and if this transaction meets the + ** runtime criteria to use the operation: ** ** * The file-system supports the atomic-write property for - ** blocks of size page-size, and + ** blocks of size page-size, and ** * This commit is not part of a multi-file transaction, and ** * Exactly one page has been modified and store in the journal file. ** @@ -6238,86 +6668,130 @@ int sqlite3PagerCommitPhaseOne( ** is not applicable to this transaction, call sqlite3JournalCreate() ** to make sure the journal file has actually been created, then call ** pager_incr_changecounter() to update the change-counter in indirect - ** mode. + ** mode. ** ** Otherwise, if the optimization is both enabled and applicable, ** then call pager_incr_changecounter() to update the change-counter ** in 'direct' mode. In this case the journal file will never be ** created for this transaction. */ - #ifdef SQLITE_ENABLE_ATOMIC_WRITE - PgHdr *pPg; - assert( isOpen(pPager->jfd) - || pPager->journalMode==PAGER_JOURNALMODE_OFF - || pPager->journalMode==PAGER_JOURNALMODE_WAL - ); - if( !zMaster && isOpen(pPager->jfd) - && pPager->journalOff==jrnlBufferSize(pPager) - && pPager->dbSize>=pPager->dbOrigSize - && (0==(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty) - ){ - /* Update the db file change counter via the direct-write method. The - ** following call will modify the in-memory representation of page 1 - ** to include the updated change counter and then write page 1 - ** directly to the database file. Because of the atomic-write - ** property of the host file-system, this is safe. - */ - rc = pager_incr_changecounter(pPager, 1); - }else{ - rc = sqlite3JournalCreate(pPager->jfd); - if( rc==SQLITE_OK ){ - rc = pager_incr_changecounter(pPager, 0); + if( bBatch==0 ){ + PgHdr *pPg; + assert( isOpen(pPager->jfd) + || pPager->journalMode==PAGER_JOURNALMODE_OFF + || pPager->journalMode==PAGER_JOURNALMODE_WAL + ); + if( !zSuper && isOpen(pPager->jfd) + && pPager->journalOff==jrnlBufferSize(pPager) + && pPager->dbSize>=pPager->dbOrigSize + && (!(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty) + ){ + /* Update the db file change counter via the direct-write method. The + ** following call will modify the in-memory representation of page 1 + ** to include the updated change counter and then write page 1 + ** directly to the database file. Because of the atomic-write + ** property of the host file-system, this is safe. + */ + rc = pager_incr_changecounter(pPager, 1); + }else{ + rc = sqlite3JournalCreate(pPager->jfd); + if( rc==SQLITE_OK ){ + rc = pager_incr_changecounter(pPager, 0); + } } } - #else +#else /* SQLITE_ENABLE_ATOMIC_WRITE */ +#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE + if( zSuper ){ + rc = sqlite3JournalCreate(pPager->jfd); + if( rc!=SQLITE_OK ) goto commit_phase_one_exit; + assert( bBatch==0 ); + } +#endif rc = pager_incr_changecounter(pPager, 0); - #endif +#endif /* !SQLITE_ENABLE_ATOMIC_WRITE */ if( rc!=SQLITE_OK ) goto commit_phase_one_exit; - - /* Write the master journal name into the journal file. If a master - ** journal file name has already been written to the journal file, - ** or if zMaster is NULL (no master journal), then this call is a no-op. + + /* Write the super-journal name into the journal file. If a + ** super-journal file name has already been written to the journal file, + ** or if zSuper is NULL (no super-journal), then this call is a no-op. */ - rc = writeMasterJournal(pPager, zMaster); + rc = writeSuperJournal(pPager, zSuper); if( rc!=SQLITE_OK ) goto commit_phase_one_exit; - + /* Sync the journal file and write all dirty pages to the database. - ** If the atomic-update optimization is being used, this sync will not + ** If the atomic-update optimization is being used, this sync will not ** create the journal file or perform any real IO. ** ** Because the change-counter page was just modified, unless the ** atomic-update optimization is used it is almost certain that the ** journal requires a sync here. However, in locking_mode=exclusive - ** on a system under memory pressure it is just possible that this is + ** on a system under memory pressure it is just possible that this is ** not the case. In this case it is likely enough that the redundant - ** xSync() call will be changed to a no-op by the OS anyhow. + ** xSync() call will be changed to a no-op by the OS anyhow. */ rc = syncJournal(pPager, 0); if( rc!=SQLITE_OK ) goto commit_phase_one_exit; - - rc = pager_write_pagelist(pPager,sqlite3PcacheDirtyList(pPager->pPCache)); + + pList = sqlite3PcacheDirtyList(pPager->pPCache); +#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE + if( bBatch ){ + rc = sqlite3OsFileControl(fd, SQLITE_FCNTL_BEGIN_ATOMIC_WRITE, 0); + if( rc==SQLITE_OK ){ + rc = pager_write_pagelist(pPager, pList); + if( rc==SQLITE_OK && pPager->dbSize>pPager->dbFileSize ){ + char *pTmp = pPager->pTmpSpace; + int szPage = (int)pPager->pageSize; + memset(pTmp, 0, szPage); + rc = sqlite3OsWrite(pPager->fd, pTmp, szPage, + ((i64)pPager->dbSize*pPager->pageSize)-szPage); + } + if( rc==SQLITE_OK ){ + rc = sqlite3OsFileControl(fd, SQLITE_FCNTL_COMMIT_ATOMIC_WRITE, 0); + } + if( rc!=SQLITE_OK ){ + sqlite3OsFileControlHint(fd, SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE, 0); + } + } + + if( (rc&0xFF)==SQLITE_IOERR && rc!=SQLITE_IOERR_NOMEM ){ + rc = sqlite3JournalCreate(pPager->jfd); + if( rc!=SQLITE_OK ){ + sqlite3OsClose(pPager->jfd); + goto commit_phase_one_exit; + } + bBatch = 0; + }else{ + sqlite3OsClose(pPager->jfd); + } + } +#endif /* SQLITE_ENABLE_BATCH_ATOMIC_WRITE */ + + if( bBatch==0 ){ + rc = pager_write_pagelist(pPager, pList); + } if( rc!=SQLITE_OK ){ assert( rc!=SQLITE_IOERR_BLOCKED ); goto commit_phase_one_exit; } sqlite3PcacheCleanAll(pPager->pPCache); - /* If the file on disk is smaller than the database image, use + /* If the file on disk is smaller than the database image, use ** pager_truncate to grow the file here. This can happen if the database ** image was extended as part of the current transaction and then the ** last page in the db image moved to the free-list. In this case the ** last page is never written out to disk, leaving the database file ** undersized. Fix this now if it is the case. */ if( pPager->dbSize>pPager->dbFileSize ){ - Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_MJ_PGNO(pPager)); + Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_SJ_PGNO(pPager)); assert( pPager->eState==PAGER_WRITER_DBMOD ); rc = pager_truncate(pPager, nNew); if( rc!=SQLITE_OK ) goto commit_phase_one_exit; } - + /* Finally, sync the database file. */ if( !noSync ){ - rc = sqlite3PagerSync(pPager, zMaster); + rc = sqlite3PagerSync(pPager, zSuper); } IOTRACE(("DBSYNC %p\n", pPager)) } @@ -6334,12 +6808,12 @@ int sqlite3PagerCommitPhaseOne( /* ** When this function is called, the database file has been completely ** updated to reflect the changes made by the current transaction and -** synced to disk. The journal file still exists in the file-system +** synced to disk. The journal file still exists in the file-system ** though, and if a failure occurs at this point it will eventually ** be used as a hot-journal and the current transaction rolled back. ** -** This function finalizes the journal file, either by deleting, -** truncating or partially zeroing it, so that it cannot be used +** This function finalizes the journal file, either by deleting, +** truncating or partially zeroing it, so that it cannot be used ** for hot-journal rollback. Once this is done the transaction is ** irrevocably committed. ** @@ -6353,6 +6827,7 @@ int sqlite3PagerCommitPhaseTwo(Pager *pPager){ ** But if (due to a coding error elsewhere in the system) it does get ** called, just return the same error code without doing anything. */ if( NEVER(pPager->errCode) ) return pPager->errCode; + pPager->iDataVersion++; assert( pPager->eState==PAGER_WRITER_LOCKED || pPager->eState==PAGER_WRITER_FINISHED @@ -6364,15 +6839,15 @@ int sqlite3PagerCommitPhaseTwo(Pager *pPager){ ** this transaction, the pager is running in exclusive-mode and is ** using persistent journals, then this function is a no-op. ** - ** The start of the journal file currently contains a single journal + ** The start of the journal file currently contains a single journal ** header with the nRec field set to 0. If such a journal is used as ** a hot-journal during hot-journal rollback, 0 changes will be made - ** to the database file. So there is no need to zero the journal + ** to the database file. So there is no need to zero the journal ** header. Since the pager is in exclusive mode, there is no need ** to drop any locks either. */ - if( pPager->eState==PAGER_WRITER_LOCKED - && pPager->exclusiveMode + if( pPager->eState==PAGER_WRITER_LOCKED + && pPager->exclusiveMode && pPager->journalMode==PAGER_JOURNALMODE_PERSIST ){ assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) || !pPager->journalOff ); @@ -6381,13 +6856,12 @@ int sqlite3PagerCommitPhaseTwo(Pager *pPager){ } PAGERTRACE(("COMMIT %d\n", PAGERID(pPager))); - pPager->iDataVersion++; - rc = pager_end_transaction(pPager, pPager->setMaster, 1); + rc = pager_end_transaction(pPager, pPager->setSuper, 1); return pager_error(pPager, rc); } /* -** If a write transaction is open, then all changes made within the +** If a write transaction is open, then all changes made within the ** transaction are reverted and the current write-transaction is closed. ** The pager falls back to PAGER_READER state if successful, or PAGER_ERROR ** state if an error occurs. @@ -6397,14 +6871,14 @@ int sqlite3PagerCommitPhaseTwo(Pager *pPager){ ** ** Otherwise, in rollback mode, this function performs two functions: ** -** 1) It rolls back the journal file, restoring all database file and +** 1) It rolls back the journal file, restoring all database file and ** in-memory cache pages to the state they were in when the transaction ** was opened, and ** ** 2) It finalizes the journal file, so that it is not used for hot ** rollback at any point in the future. ** -** Finalization of the journal file (task 2) is only performed if the +** Finalization of the journal file (task 2) is only performed if the ** rollback is successful. ** ** In WAL mode, all cache-entries containing data modified within the @@ -6417,7 +6891,7 @@ int sqlite3PagerRollback(Pager *pPager){ PAGERTRACE(("ROLLBACK %d\n", PAGERID(pPager))); /* PagerRollback() is a no-op if called in READER or OPEN state. If - ** the pager is already in the ERROR state, the rollback is not + ** the pager is already in the ERROR state, the rollback is not ** attempted here. Instead, the error code is returned to the caller. */ assert( assert_pager_state(pPager) ); @@ -6427,18 +6901,19 @@ int sqlite3PagerRollback(Pager *pPager){ if( pagerUseWal(pPager) ){ int rc2; rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK, -1); - rc2 = pager_end_transaction(pPager, pPager->setMaster, 0); + rc2 = pager_end_transaction(pPager, pPager->setSuper, 0); if( rc==SQLITE_OK ) rc = rc2; }else if( !isOpen(pPager->jfd) || pPager->eState==PAGER_WRITER_LOCKED ){ int eState = pPager->eState; rc = pager_end_transaction(pPager, 0, 0); if( !MEMDB && eState>PAGER_WRITER_LOCKED ){ - /* This can happen using journal_mode=off. Move the pager to the error + /* This can happen using journal_mode=off. Move the pager to the error ** state to indicate that the contents of the cache may not be trusted. ** Any active readers will get SQLITE_ABORT. */ pPager->errCode = SQLITE_ABORT; pPager->eState = PAGER_ERROR; + setGetterMethod(pPager); return rc; } }else{ @@ -6447,7 +6922,7 @@ int sqlite3PagerRollback(Pager *pPager){ assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK ); assert( rc==SQLITE_OK || rc==SQLITE_FULL || rc==SQLITE_CORRUPT - || rc==SQLITE_NOMEM || (rc&0xFF)==SQLITE_IOERR + || rc==SQLITE_NOMEM || (rc&0xFF)==SQLITE_IOERR || rc==SQLITE_CANTOPEN ); @@ -6479,8 +6954,8 @@ int sqlite3PagerRefcount(Pager *pPager){ ** used by the pager and its associated cache. */ int sqlite3PagerMemUsed(Pager *pPager){ - int perPageSize = pPager->pageSize + pPager->nExtra + sizeof(PgHdr) - + 5*sizeof(void*); + int perPageSize = pPager->pageSize + pPager->nExtra + + (int)(sizeof(PgHdr) + 5*sizeof(void*)); return perPageSize*sqlite3PcachePagecount(pPager->pPCache) + sqlite3MallocSize(pPager) + pPager->pageSize; @@ -6505,44 +6980,51 @@ int *sqlite3PagerStats(Pager *pPager){ a[3] = pPager->eState==PAGER_OPEN ? -1 : (int) pPager->dbSize; a[4] = pPager->eState; a[5] = pPager->errCode; - a[6] = pPager->aStat[PAGER_STAT_HIT]; - a[7] = pPager->aStat[PAGER_STAT_MISS]; + a[6] = (int)pPager->aStat[PAGER_STAT_HIT] & 0x7fffffff; + a[7] = (int)pPager->aStat[PAGER_STAT_MISS] & 0x7fffffff; a[8] = 0; /* Used to be pPager->nOvfl */ a[9] = pPager->nRead; - a[10] = pPager->aStat[PAGER_STAT_WRITE]; + a[10] = (int)pPager->aStat[PAGER_STAT_WRITE] & 0x7fffffff; return a; } #endif /* -** Parameter eStat must be either SQLITE_DBSTATUS_CACHE_HIT or -** SQLITE_DBSTATUS_CACHE_MISS. Before returning, *pnVal is incremented by the -** current cache hit or miss count, according to the value of eStat. If the -** reset parameter is non-zero, the cache hit or miss count is zeroed before +** Parameter eStat must be one of SQLITE_DBSTATUS_CACHE_HIT, _MISS, _WRITE, +** or _WRITE+1. The SQLITE_DBSTATUS_CACHE_WRITE+1 case is a translation +** of SQLITE_DBSTATUS_CACHE_SPILL. The _SPILL case is not contiguous because +** it was added later. +** +** Before returning, *pnVal is incremented by the +** current cache hit or miss count, according to the value of eStat. If the +** reset parameter is non-zero, the cache hit or miss count is zeroed before ** returning. */ -void sqlite3PagerCacheStat(Pager *pPager, int eStat, int reset, int *pnVal){ +void sqlite3PagerCacheStat(Pager *pPager, int eStat, int reset, u64 *pnVal){ assert( eStat==SQLITE_DBSTATUS_CACHE_HIT || eStat==SQLITE_DBSTATUS_CACHE_MISS || eStat==SQLITE_DBSTATUS_CACHE_WRITE + || eStat==SQLITE_DBSTATUS_CACHE_WRITE+1 ); assert( SQLITE_DBSTATUS_CACHE_HIT+1==SQLITE_DBSTATUS_CACHE_MISS ); assert( SQLITE_DBSTATUS_CACHE_HIT+2==SQLITE_DBSTATUS_CACHE_WRITE ); - assert( PAGER_STAT_HIT==0 && PAGER_STAT_MISS==1 && PAGER_STAT_WRITE==2 ); + assert( PAGER_STAT_HIT==0 && PAGER_STAT_MISS==1 + && PAGER_STAT_WRITE==2 && PAGER_STAT_SPILL==3 ); - *pnVal += pPager->aStat[eStat - SQLITE_DBSTATUS_CACHE_HIT]; + eStat -= SQLITE_DBSTATUS_CACHE_HIT; + *pnVal += pPager->aStat[eStat]; if( reset ){ - pPager->aStat[eStat - SQLITE_DBSTATUS_CACHE_HIT] = 0; + pPager->aStat[eStat] = 0; } } /* -** Return true if this is an in-memory pager. +** Return true if this is an in-memory or temp-file backed pager. */ int sqlite3PagerIsMemdb(Pager *pPager){ - return MEMDB; + return pPager->tempFile || pPager->memVfs; } /* @@ -6551,7 +7033,7 @@ int sqlite3PagerIsMemdb(Pager *pPager){ ** to make up the difference. If the number of savepoints is already ** equal to nSavepoint, then this function is a no-op. ** -** If a memory allocation fails, SQLITE_NOMEM is returned. If an error +** If a memory allocation fails, SQLITE_NOMEM is returned. If an error ** occurs while opening the sub-journal file, then an IO error code is ** returned. Otherwise, SQLITE_OK. */ @@ -6566,14 +7048,14 @@ static SQLITE_NOINLINE int pagerOpenSavepoint(Pager *pPager, int nSavepoint){ assert( nSavepoint>nCurrent && pPager->useJournal ); /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM - ** if the allocation fails. Otherwise, zero the new portion in case a + ** if the allocation fails. Otherwise, zero the new portion in case a ** malloc failure occurs while populating it in the for(...) loop below. */ aNew = (PagerSavepoint *)sqlite3Realloc( pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint ); if( !aNew ){ - return SQLITE_NOMEM; + return SQLITE_NOMEM_BKPT; } memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint)); pPager->aSavepoint = aNew; @@ -6588,8 +7070,9 @@ static SQLITE_NOINLINE int pagerOpenSavepoint(Pager *pPager, int nSavepoint){ } aNew[ii].iSubRec = pPager->nSubRec; aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize); + aNew[ii].bTruncateOnRelease = 1; if( !aNew[ii].pInSavepoint ){ - return SQLITE_NOMEM; + return SQLITE_NOMEM_BKPT; } if( pagerUseWal(pPager) ){ sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData); @@ -6614,7 +7097,7 @@ int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){ /* ** This function is called to rollback or release (commit) a savepoint. -** The savepoint to release or rollback need not be the most recently +** The savepoint to release or rollback need not be the most recently ** created savepoint. ** ** Parameter op is always either SAVEPOINT_ROLLBACK or SAVEPOINT_RELEASE. @@ -6622,28 +7105,32 @@ int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){ ** index iSavepoint. If it is SAVEPOINT_ROLLBACK, then rollback all changes ** that have occurred since the specified savepoint was created. ** -** The savepoint to rollback or release is identified by parameter +** The savepoint to rollback or release is identified by parameter ** iSavepoint. A value of 0 means to operate on the outermost savepoint ** (the first created). A value of (Pager.nSavepoint-1) means operate ** on the most recently created savepoint. If iSavepoint is greater than ** (Pager.nSavepoint-1), then this function is a no-op. ** ** If a negative value is passed to this function, then the current -** transaction is rolled back. This is different to calling +** transaction is rolled back. This is different to calling ** sqlite3PagerRollback() because this function does not terminate -** the transaction or unlock the database, it just restores the -** contents of the database to its original state. +** the transaction or unlock the database, it just restores the +** contents of the database to its original state. ** -** In any case, all savepoints with an index greater than iSavepoint +** In any case, all savepoints with an index greater than iSavepoint ** are destroyed. If this is a release operation (op==SAVEPOINT_RELEASE), ** then savepoint iSavepoint is also destroyed. ** ** This function may return SQLITE_NOMEM if a memory allocation fails, -** or an IO error code if an IO error occurs while rolling back a +** or an IO error code if an IO error occurs while rolling back a ** savepoint. If no errors occur, SQLITE_OK is returned. -*/ +*/ int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){ - int rc = pPager->errCode; /* Return code */ + int rc = pPager->errCode; + +#ifdef SQLITE_ENABLE_ZIPVFS + if( op==SAVEPOINT_RELEASE ) rc = SQLITE_OK; +#endif assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK ); assert( iSavepoint>=0 || op==SAVEPOINT_ROLLBACK ); @@ -6653,7 +7140,7 @@ int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){ int nNew; /* Number of remaining savepoints after this op. */ /* Figure out how many savepoints will still be active after this - ** operation. Store this value in nNew. Then free resources associated + ** operation. Store this value in nNew. Then free resources associated ** with any savepoints that are destroyed by this operation. */ nNew = iSavepoint + (( op==SAVEPOINT_RELEASE ) ? 0 : 1); @@ -6662,16 +7149,18 @@ int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){ } pPager->nSavepoint = nNew; - /* If this is a release of the outermost savepoint, truncate - ** the sub-journal to zero bytes in size. */ + /* Truncate the sub-journal so that it only includes the parts + ** that are still in use. */ if( op==SAVEPOINT_RELEASE ){ - if( nNew==0 && isOpen(pPager->sjfd) ){ + PagerSavepoint *pRel = &pPager->aSavepoint[nNew]; + if( pRel->bTruncateOnRelease && isOpen(pPager->sjfd) ){ /* Only truncate if it is an in-memory sub-journal. */ - if( sqlite3IsMemJournal(pPager->sjfd) ){ - rc = sqlite3OsTruncate(pPager->sjfd, 0); + if( sqlite3JournalIsInMemory(pPager->sjfd) ){ + i64 sz = (pPager->pageSize+4)*(i64)pRel->iSubRec; + rc = sqlite3OsTruncate(pPager->sjfd, sz); assert( rc==SQLITE_OK ); } - pPager->nSubRec = 0; + pPager->nSubRec = pRel->iSubRec; } } /* Else this is a rollback operation, playback the specified savepoint. @@ -6684,6 +7173,21 @@ int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){ rc = pagerPlaybackSavepoint(pPager, pSavepoint); assert(rc!=SQLITE_DONE); } + +#ifdef SQLITE_ENABLE_ZIPVFS + /* If the cache has been modified but the savepoint cannot be rolled + ** back journal_mode=off, put the pager in the error state. This way, + ** if the VFS used by this pager includes ZipVFS, the entire transaction + ** can be rolled back at the ZipVFS level. */ + else if( + pPager->journalMode==PAGER_JOURNALMODE_OFF + && pPager->eState>=PAGER_WRITER_CACHEMOD + ){ + pPager->errCode = SQLITE_ABORT; + pPager->eState = PAGER_ERROR; + setGetterMethod(pPager); + } +#endif } return rc; @@ -6698,9 +7202,17 @@ int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){ ** behavior. But when the Btree needs to know the filename for matching to ** shared cache, it uses nullIfMemDb==0 so that in-memory databases can ** participate in shared-cache. +** +** The return value to this routine is always safe to use with +** sqlite3_uri_parameter() and sqlite3_filename_database() and friends. */ -const char *sqlite3PagerFilename(Pager *pPager, int nullIfMemDb){ - return (nullIfMemDb && pPager->memDb) ? "" : pPager->zFilename; +const char *sqlite3PagerFilename(const Pager *pPager, int nullIfMemDb){ + static const char zFake[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; + if( nullIfMemDb && (pPager->memDb || sqlite3IsMemdb(pPager->pVfs)) ){ + return &zFake[4]; + }else{ + return pPager->zFilename; + } } /* @@ -6724,7 +7236,7 @@ sqlite3_file *sqlite3PagerFile(Pager *pPager){ ** This will be either the rollback journal or the WAL file. */ sqlite3_file *sqlite3PagerJrnlFile(Pager *pPager){ -#if SQLITE_OMIT_WAL +#ifdef SQLITE_OMIT_WAL return pPager->jfd; #else return pPager->pWal ? sqlite3WalFile(pPager->pWal) : pPager->jfd; @@ -6738,33 +7250,31 @@ const char *sqlite3PagerJournalname(Pager *pPager){ return pPager->zJournal; } -/* -** Return true if fsync() calls are disabled for this pager. Return FALSE -** if fsync()s are executed normally. -*/ -int sqlite3PagerNosync(Pager *pPager){ - return pPager->noSync; -} - +/* BEGIN SQLCIPHER */ #ifdef SQLITE_HAS_CODEC /* -** Set or retrieve the codec for this pager +** Set (or overwrite) the codec for this pager. If there is +** already a codec context (pCodec) attached, it WILL NOT be freed. +** The caller is responsible for freeing the old codec context prior +** setting a new one. */ -void sqlite3PagerSetCodec( +void sqlcipherPagerSetCodec( Pager *pPager, void *(*xCodec)(void*,void*,Pgno,int), void (*xCodecSizeChng)(void*,int,int), void (*xCodecFree)(void*), void *pCodec ){ - if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec); + pager_reset(pPager); pPager->xCodec = pPager->memDb ? 0 : xCodec; pPager->xCodecSizeChng = xCodecSizeChng; pPager->xCodecFree = xCodecFree; pPager->pCodec = pCodec; + setGetterMethod(pPager); pagerReportSize(pPager); } -void *sqlite3PagerGetCodec(Pager *pPager){ +/* Retrieve the codec for this pager */ +void *sqlcipherPagerGetCodec(Pager *pPager){ return pPager->pCodec; } @@ -6775,19 +7285,13 @@ void *sqlite3PagerGetCodec(Pager *pPager){ ** This function returns a pointer to a buffer containing the encrypted ** page content. If a malloc fails, this function may return NULL. */ -void *sqlite3PagerCodec(PgHdr *pPg){ +void *sqlcipherPagerCodec(PgHdr *pPg){ void *aData = 0; CODEC2(pPg->pPager, pPg->pData, pPg->pgno, 6, return 0, aData); return aData; } - -/* -** Return the current pager state -*/ -int sqlite3PagerState(Pager *pPager){ - return pPager->eState; -} #endif /* SQLITE_HAS_CODEC */ +/* END SQLCIPHER */ #ifndef SQLITE_OMIT_AUTOVACUUM /* @@ -6808,8 +7312,8 @@ int sqlite3PagerState(Pager *pPager){ ** transaction is active). ** ** If the fourth argument, isCommit, is non-zero, then this page is being -** moved as part of a database reorganization just before the transaction -** is being committed. In this case, it is guaranteed that the database page +** moved as part of a database reorganization just before the transaction +** is being committed. In this case, it is guaranteed that the database page ** pPg refers to will not be written to again within this transaction. ** ** This function may return SQLITE_NOMEM or an IO error code if an error @@ -6830,13 +7334,14 @@ int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, int isCommit){ /* In order to be able to rollback, an in-memory database must journal ** the page we are moving from. */ - if( MEMDB ){ + assert( pPager->tempFile || !MEMDB ); + if( pPager->tempFile ){ rc = sqlite3PagerWrite(pPg); if( rc ) return rc; } /* If the page being moved is dirty and has not been saved by the latest - ** savepoint, then save the current contents of the page into the + ** savepoint, then save the current contents of the page into the ** sub-journal now. This is required to handle the following scenario: ** ** BEGIN; @@ -6859,7 +7364,7 @@ int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, int isCommit){ return rc; } - PAGERTRACE(("MOVE %d page %d (needSync=%d) moves to %d\n", + PAGERTRACE(("MOVE %d page %d (needSync=%d) moves to %d\n", PAGERID(pPager), pPg->pgno, (pPg->flags&PGHDR_NEED_SYNC)?1:0, pgno)); IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno)) @@ -6867,7 +7372,7 @@ int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, int isCommit){ ** be written to, store pPg->pgno in local variable needSyncPgno. ** ** If the isCommit flag is set, there is no need to remember that - ** the journal needs to be sync()ed before database page pPg->pgno + ** the journal needs to be sync()ed before database page pPg->pgno ** can be written to. The caller has already promised not to write to it. */ if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){ @@ -6878,16 +7383,20 @@ int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, int isCommit){ } /* If the cache contains a page with page-number pgno, remove it - ** from its hash chain. Also, if the PGHDR_NEED_SYNC flag was set for - ** page pgno before the 'move' operation, it needs to be retained + ** from its hash chain. Also, if the PGHDR_NEED_SYNC flag was set for + ** page pgno before the 'move' operation, it needs to be retained ** for the page moved there. */ pPg->flags &= ~PGHDR_NEED_SYNC; pPgOld = sqlite3PagerLookup(pPager, pgno); - assert( !pPgOld || pPgOld->nRef==1 ); + assert( !pPgOld || pPgOld->nRef==1 || CORRUPT_DB ); if( pPgOld ){ + if( NEVER(pPgOld->nRef>1) ){ + sqlite3PagerUnrefNotNull(pPgOld); + return SQLITE_CORRUPT_BKPT; + } pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC); - if( MEMDB ){ + if( pPager->tempFile ){ /* Do not discard pages from an in-memory database since we might ** need to rollback later. Just move the page out of the way. */ sqlite3PcacheMove(pPgOld, pPager->dbSize+1); @@ -6904,16 +7413,15 @@ int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, int isCommit){ ** to exist, in case the transaction needs to roll back. Use pPgOld ** as the original page since it has already been allocated. */ - if( MEMDB ){ - assert( pPgOld ); + if( pPager->tempFile && pPgOld ){ sqlite3PcacheMove(pPgOld, origPgno); sqlite3PagerUnrefNotNull(pPgOld); } if( needSyncPgno ){ - /* If needSyncPgno is non-zero, then the journal file needs to be + /* If needSyncPgno is non-zero, then the journal file needs to be ** sync()ed before any data is written to database file page needSyncPgno. - ** Currently, no such page exists in the page-cache and the + ** Currently, no such page exists in the page-cache and the ** "is journaled" bitvec flag has been set. This needs to be remedied by ** loading the page into the pager-cache and setting the PGHDR_NEED_SYNC ** flag. @@ -6944,9 +7452,9 @@ int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, int isCommit){ #endif /* -** The page handle passed as the first argument refers to a dirty page -** with a page number other than iNew. This function changes the page's -** page number to iNew and sets the value of the PgHdr.flags field to +** The page handle passed as the first argument refers to a dirty page +** with a page number other than iNew. This function changes the page's +** page number to iNew and sets the value of the PgHdr.flags field to ** the value passed as the third parameter. */ void sqlite3PagerRekey(DbPage *pPg, Pgno iNew, u16 flags){ @@ -6964,7 +7472,7 @@ void *sqlite3PagerGetData(DbPage *pPg){ } /* -** Return a pointer to the Pager.nExtra bytes of "extra" space +** Return a pointer to the Pager.nExtra bytes of "extra" space ** allocated along with the specified page. */ void *sqlite3PagerGetExtra(DbPage *pPg){ @@ -6973,7 +7481,7 @@ void *sqlite3PagerGetExtra(DbPage *pPg){ /* ** Get/set the locking-mode for this pager. Parameter eMode must be one -** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or +** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or ** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then ** the locking-mode is set to the value specified. ** @@ -7017,20 +7525,13 @@ int sqlite3PagerLockingMode(Pager *pPager, int eMode){ int sqlite3PagerSetJournalMode(Pager *pPager, int eMode){ u8 eOld = pPager->journalMode; /* Prior journalmode */ -#ifdef SQLITE_DEBUG - /* The print_pager_state() routine is intended to be used by the debugger - ** only. We invoke it once here to suppress a compiler warning. */ - print_pager_state(pPager); -#endif - - /* The eMode parameter is always valid */ - assert( eMode==PAGER_JOURNALMODE_DELETE - || eMode==PAGER_JOURNALMODE_TRUNCATE - || eMode==PAGER_JOURNALMODE_PERSIST - || eMode==PAGER_JOURNALMODE_OFF - || eMode==PAGER_JOURNALMODE_WAL - || eMode==PAGER_JOURNALMODE_MEMORY ); + assert( eMode==PAGER_JOURNALMODE_DELETE /* 0 */ + || eMode==PAGER_JOURNALMODE_PERSIST /* 1 */ + || eMode==PAGER_JOURNALMODE_OFF /* 2 */ + || eMode==PAGER_JOURNALMODE_TRUNCATE /* 3 */ + || eMode==PAGER_JOURNALMODE_MEMORY /* 4 */ + || eMode==PAGER_JOURNALMODE_WAL /* 5 */ ); /* This routine is only called from the OP_JournalMode opcode, and ** the logic there will never allow a temporary file to be changed @@ -7054,7 +7555,7 @@ int sqlite3PagerSetJournalMode(Pager *pPager, int eMode){ assert( pPager->eState!=PAGER_ERROR ); pPager->journalMode = (u8)eMode; - /* When transistioning from TRUNCATE or PERSIST to any other journal + /* When transitioning from TRUNCATE or PERSIST to any other journal ** mode except WAL, unless the pager is in locking_mode=exclusive mode, ** delete the journal file. */ @@ -7067,7 +7568,6 @@ int sqlite3PagerSetJournalMode(Pager *pPager, int eMode){ assert( isOpen(pPager->fd) || pPager->exclusiveMode ); if( !pPager->exclusiveMode && (eOld & 5)==1 && (eMode & 1)==0 ){ - /* In this case we would like to delete the journal file. If it is ** not possible, then that is not a problem. Deleting the journal file ** here is an optimization only. @@ -7100,7 +7600,7 @@ int sqlite3PagerSetJournalMode(Pager *pPager, int eMode){ } assert( state==pPager->eState ); } - }else if( eMode==PAGER_JOURNALMODE_OFF ){ + }else if( eMode==PAGER_JOURNALMODE_OFF || eMode==PAGER_JOURNALMODE_MEMORY ){ sqlite3OsClose(pPager->jfd); } } @@ -7157,10 +7657,12 @@ sqlite3_backup **sqlite3PagerBackupPtr(Pager *pPager){ ** Unless this is an in-memory or temporary database, clear the pager cache. */ void sqlite3PagerClearCache(Pager *pPager){ - if( !MEMDB && pPager->tempFile==0 ) pager_reset(pPager); + assert( MEMDB==0 || pPager->tempFile ); + if( pPager->tempFile==0 ) pager_reset(pPager); } #endif + #ifndef SQLITE_OMIT_WAL /* ** This function is called when the user invokes "PRAGMA wal_checkpoint", @@ -7169,13 +7671,31 @@ void sqlite3PagerClearCache(Pager *pPager){ ** ** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART. */ -int sqlite3PagerCheckpoint(Pager *pPager, int eMode, int *pnLog, int *pnCkpt){ +int sqlite3PagerCheckpoint( + Pager *pPager, /* Checkpoint on this pager */ + sqlite3 *db, /* Db handle used to check for interrupts */ + int eMode, /* Type of checkpoint */ + int *pnLog, /* OUT: Final number of frames in log */ + int *pnCkpt /* OUT: Final number of checkpointed frames */ +){ int rc = SQLITE_OK; + if( pPager->pWal==0 && pPager->journalMode==PAGER_JOURNALMODE_WAL ){ + /* This only happens when a database file is zero bytes in size opened and + ** then "PRAGMA journal_mode=WAL" is run and then sqlite3_wal_checkpoint() + ** is invoked without any intervening transactions. We need to start + ** a transaction to initialize pWal. The PRAGMA table_list statement is + ** used for this since it starts transactions on every database file, + ** including all ATTACHed databases. This seems expensive for a single + ** sqlite3_wal_checkpoint() call, but it happens very rarely. + ** https://sqlite.org/forum/forumpost/fd0f19d229156939 + */ + sqlite3_exec(db, "PRAGMA table_list",0,0,0); + } if( pPager->pWal ){ - rc = sqlite3WalCheckpoint(pPager->pWal, eMode, - (eMode==SQLITE_CHECKPOINT_PASSIVE ? 0 : pPager->xBusyHandler), + rc = sqlite3WalCheckpoint(pPager->pWal, db, eMode, + (eMode<=SQLITE_CHECKPOINT_PASSIVE ? 0 : pPager->xBusyHandler), pPager->pBusyHandlerArg, - pPager->ckptSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace, + pPager->walSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace, pnLog, pnCkpt ); } @@ -7192,6 +7712,7 @@ int sqlite3PagerWalCallback(Pager *pPager){ */ int sqlite3PagerWalSupported(Pager *pPager){ const sqlite3_io_methods *pMethods = pPager->fd->pMethods; + if( pPager->noLock ) return 0; return pPager->exclusiveMode || (pMethods->iVersion>=2 && pMethods->xShmMap); } @@ -7201,20 +7722,22 @@ int sqlite3PagerWalSupported(Pager *pPager){ */ static int pagerExclusiveLock(Pager *pPager){ int rc; /* Return code */ + u8 eOrigLock; /* Original lock */ - assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK ); + assert( pPager->eLock>=SHARED_LOCK ); + eOrigLock = pPager->eLock; rc = pagerLockDb(pPager, EXCLUSIVE_LOCK); if( rc!=SQLITE_OK ){ - /* If the attempt to grab the exclusive lock failed, release the + /* If the attempt to grab the exclusive lock failed, release the ** pending lock that may have been obtained instead. */ - pagerUnlockDb(pPager, SHARED_LOCK); + pagerUnlockDb(pPager, eOrigLock); } return rc; } /* -** Call sqlite3WalOpen() to open the WAL handle. If the pager is in +** Call sqlite3WalOpen() to open the WAL handle. If the pager is in ** exclusive-locking mode when this function is called, take an EXCLUSIVE ** lock on the database file and use heap-memory to store the wal-index ** in. Otherwise, use the normal shared-memory. @@ -7225,8 +7748,8 @@ static int pagerOpenWal(Pager *pPager){ assert( pPager->pWal==0 && pPager->tempFile==0 ); assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK ); - /* If the pager is already in exclusive-mode, the WAL module will use - ** heap-memory for the wal-index instead of the VFS shared-memory + /* If the pager is already in exclusive-mode, the WAL module will use + ** heap-memory for the wal-index instead of the VFS shared-memory ** implementation. Take the exclusive lock now, before opening the WAL ** file, to make sure this is safe. */ @@ -7234,7 +7757,7 @@ static int pagerOpenWal(Pager *pPager){ rc = pagerExclusiveLock(pPager); } - /* Open the connection to the log file. If this operation fails, + /* Open the connection to the log file. If this operation fails, ** (e.g. due to malloc() failure), return an error code. */ if( rc==SQLITE_OK ){ @@ -7242,6 +7765,11 @@ static int pagerOpenWal(Pager *pPager){ pPager->fd, pPager->zWal, pPager->exclusiveMode, pPager->journalSizeLimit, &pPager->pWal ); +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT + if( rc==SQLITE_OK ){ + sqlite3WalDb(pPager->pWal, pPager->dbWal); + } +#endif } pagerFixMaplimit(pPager); @@ -7256,7 +7784,7 @@ static int pagerOpenWal(Pager *pPager){ ** If the pager passed as the first argument is open on a real database ** file (not a temp file or an in-memory database), and the WAL file ** is not already open, make an attempt to open it now. If successful, -** return SQLITE_OK. If an error occurs or the VFS used by the pager does +** return SQLITE_OK. If an error occurs or the VFS used by the pager does ** not support the xShmXXX() methods, return an error code. *pbOpen is ** not modified in either case. ** @@ -7298,12 +7826,12 @@ int sqlite3PagerOpenWal( ** This function is called to close the connection to the log file prior ** to switching from WAL to rollback mode. ** -** Before closing the log file, this function attempts to take an +** Before closing the log file, this function attempts to take an ** EXCLUSIVE lock on the database file. If this cannot be obtained, an ** error (SQLITE_BUSY) is returned and the log connection is not closed. ** If successful, the EXCLUSIVE lock is not released before returning. */ -int sqlite3PagerCloseWal(Pager *pPager){ +int sqlite3PagerCloseWal(Pager *pPager, sqlite3 *db){ int rc = SQLITE_OK; assert( pPager->journalMode==PAGER_JOURNALMODE_WAL ); @@ -7324,22 +7852,50 @@ int sqlite3PagerCloseWal(Pager *pPager){ rc = pagerOpenWal(pPager); } } - + /* Checkpoint and close the log. Because an EXCLUSIVE lock is held on ** the database file, the log and log-summary files will be deleted. */ if( rc==SQLITE_OK && pPager->pWal ){ rc = pagerExclusiveLock(pPager); if( rc==SQLITE_OK ){ - rc = sqlite3WalClose(pPager->pWal, pPager->ckptSyncFlags, + rc = sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags, pPager->pageSize, (u8*)pPager->pTmpSpace); pPager->pWal = 0; pagerFixMaplimit(pPager); + if( rc && !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK); } } return rc; } +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT +/* +** If pager pPager is a wal-mode database not in exclusive locking mode, +** invoke the sqlite3WalWriteLock() function on the associated Wal object +** with the same db and bLock parameters as were passed to this function. +** Return an SQLite error code if an error occurs, or SQLITE_OK otherwise. +*/ +int sqlite3PagerWalWriteLock(Pager *pPager, int bLock){ + int rc = SQLITE_OK; + if( pagerUseWal(pPager) && pPager->exclusiveMode==0 ){ + rc = sqlite3WalWriteLock(pPager->pWal, bLock); + } + return rc; +} + +/* +** Set the database handle used by the wal layer to determine if +** blocking locks are required. +*/ +void sqlite3PagerWalDb(Pager *pPager, sqlite3 *db){ + pPager->dbWal = db; + if( pagerUseWal(pPager) ){ + sqlite3WalDb(pPager->pWal, db); + } +} +#endif + #ifdef SQLITE_ENABLE_SNAPSHOT /* ** If this is a WAL database, obtain a snapshot handle for the snapshot @@ -7355,10 +7911,13 @@ int sqlite3PagerSnapshotGet(Pager *pPager, sqlite3_snapshot **ppSnapshot){ /* ** If this is a WAL database, store a pointer to pSnapshot. Next time a -** read transaction is opened, attempt to read from the snapshot it +** read transaction is opened, attempt to read from the snapshot it ** identifies. If this is not a WAL database, return an error. */ -int sqlite3PagerSnapshotOpen(Pager *pPager, sqlite3_snapshot *pSnapshot){ +int sqlite3PagerSnapshotOpen( + Pager *pPager, + sqlite3_snapshot *pSnapshot +){ int rc = SQLITE_OK; if( pPager->pWal ){ sqlite3WalSnapshotOpen(pPager->pWal, pSnapshot); @@ -7367,6 +7926,52 @@ int sqlite3PagerSnapshotOpen(Pager *pPager, sqlite3_snapshot *pSnapshot){ } return rc; } + +/* +** If this is a WAL database, call sqlite3WalSnapshotRecover(). If this +** is not a WAL database, return an error. +*/ +int sqlite3PagerSnapshotRecover(Pager *pPager){ + int rc; + if( pPager->pWal ){ + rc = sqlite3WalSnapshotRecover(pPager->pWal); + }else{ + rc = SQLITE_ERROR; + } + return rc; +} + +/* +** The caller currently has a read transaction open on the database. +** If this is not a WAL database, SQLITE_ERROR is returned. Otherwise, +** this function takes a SHARED lock on the CHECKPOINTER slot and then +** checks if the snapshot passed as the second argument is still +** available. If so, SQLITE_OK is returned. +** +** If the snapshot is not available, SQLITE_ERROR is returned. Or, if +** the CHECKPOINTER lock cannot be obtained, SQLITE_BUSY. If any error +** occurs (any value other than SQLITE_OK is returned), the CHECKPOINTER +** lock is released before returning. +*/ +int sqlite3PagerSnapshotCheck(Pager *pPager, sqlite3_snapshot *pSnapshot){ + int rc; + if( pPager->pWal ){ + rc = sqlite3WalSnapshotCheck(pPager->pWal, pSnapshot); + }else{ + rc = SQLITE_ERROR; + } + return rc; +} + +/* +** Release a lock obtained by an earlier successful call to +** sqlite3PagerSnapshotCheck(). +*/ +void sqlite3PagerSnapshotUnlock(Pager *pPager){ + assert( pPager->pWal ); + sqlite3WalSnapshotUnlock(pPager->pWal); +} + #endif /* SQLITE_ENABLE_SNAPSHOT */ #endif /* !SQLITE_OMIT_WAL */ @@ -7384,35 +7989,29 @@ int sqlite3PagerWalFramesize(Pager *pPager){ } #endif +#if defined(SQLITE_USE_SEH) && !defined(SQLITE_OMIT_WAL) +int sqlite3PagerWalSystemErrno(Pager *pPager){ + return sqlite3WalSystemErrno(pPager->pWal); +} +#endif #endif /* SQLITE_OMIT_DISKIO */ /* BEGIN SQLCIPHER */ #ifdef SQLITE_HAS_CODEC -void sqlite3pager_get_codec(Pager *pPager, void **ctx) { - *ctx = pPager->pCodec; -} - -int sqlite3pager_is_mj_pgno(Pager *pPager, Pgno pgno) { - return (PAGER_MJ_PGNO(pPager) == pgno) ? 1 : 0; -} -sqlite3_file *sqlite3Pager_get_fd(Pager *pPager) { - return (isOpen(pPager->fd)) ? pPager->fd : NULL; +int sqlite3pager_is_sj_pgno(Pager *pPager, Pgno pgno) { + return (PAGER_SJ_PGNO(pPager) == pgno) ? 1 : 0; } -void sqlite3pager_sqlite3PagerSetCodec( - Pager *pPager, - void *(*xCodec)(void*,void*,Pgno,int), - void (*xCodecSizeChng)(void*,int,int), - void (*xCodecFree)(void*), - void *pCodec -){ - sqlite3PagerSetCodec(pPager, xCodec, xCodecSizeChng, xCodecFree, pCodec); +void sqlite3pager_error(Pager *pPager, int error) { + pPager->errCode = error; + pPager->eState = PAGER_ERROR; + setGetterMethod(pPager); } -void sqlite3pager_sqlite3PagerSetError( Pager *pPager, int error) { - pPager->errCode = error; +void sqlite3pager_reset(Pager *pPager){ + pager_reset(pPager); } #endif diff --git a/src/pager.h b/src/pager.h index 8d9f08108d..b30775a15f 100644 --- a/src/pager.h +++ b/src/pager.h @@ -14,8 +14,8 @@ ** at a time and provides a journal for rollback. */ -#ifndef _PAGER_H_ -#define _PAGER_H_ +#ifndef SQLITE_PAGER_H +#define SQLITE_PAGER_H /* ** Default maximum size for persistent journal files. A negative @@ -43,14 +43,15 @@ typedef struct Pager Pager; typedef struct PgHdr DbPage; /* -** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is +** Page number PAGER_SJ_PGNO is never used in an SQLite database (it is ** reserved for working around a windows/posix incompatibility). It is ** used in the journal to signify that the remainder of the journal file -** is devoted to storing a master journal name - there are no more pages to -** roll back. See comments for function writeMasterJournal() in pager.c +** is devoted to storing a super-journal name - there are no more pages to +** roll back. See comments for function writeSuperJournal() in pager.c ** for details. */ -#define PAGER_MJ_PGNO(x) ((Pgno)((PENDING_BYTE/((x)->pageSize))+1)) +#define PAGER_SJ_PGNO_COMPUTED(x) ((Pgno)((PENDING_BYTE/((x)->pageSize))+1)) +#define PAGER_SJ_PGNO(x) ((x)->lckPgno) /* ** Allowed values for the flags parameter to sqlite3PagerOpen(). @@ -68,7 +69,11 @@ typedef struct PgHdr DbPage; #define PAGER_LOCKINGMODE_EXCLUSIVE 1 /* -** Numeric constants that encode the journalmode. +** Numeric constants that encode the journalmode. +** +** The numeric values encoded here (other than PAGER_JOURNALMODE_QUERY) +** are exposed in the API via the "PRAGMA journal_mode" command and +** therefore cannot be changed without a compatibility break. */ #define PAGER_JOURNALMODE_QUERY (-1) /* Query the value of journalmode */ #define PAGER_JOURNALMODE_DELETE 0 /* Commit by deleting journal file */ @@ -78,6 +83,22 @@ typedef struct PgHdr DbPage; #define PAGER_JOURNALMODE_MEMORY 4 /* In-memory journal file */ #define PAGER_JOURNALMODE_WAL 5 /* Use write-ahead logging */ +#define isWalMode(x) ((x)==PAGER_JOURNALMODE_WAL) + +/* +** The argument to this macro is a file descriptor (type sqlite3_file*). +** Return 0 if it is not open, or non-zero (but not 1) if it is. +** +** This is so that expressions can be written as: +** +** if( isOpen(pPager->jfd) ){ ... +** +** instead of +** +** if( pPager->jfd->pMethods ){ ... +*/ +#define isOpen(pFd) ((pFd)->pMethods!=0) + /* ** Flags that make up the mask passed to sqlite3PagerGet(). */ @@ -86,6 +107,11 @@ typedef struct PgHdr DbPage; /* ** Flags for sqlite3PagerSetFlags() +** +** Value constraints (enforced via assert()): +** PAGER_FULLFSYNC == SQLITE_FullFSync +** PAGER_CKPT_FULLFSYNC == SQLITE_CkptFullFSync +** PAGER_CACHE_SPILL == SQLITE_CacheSpill */ #define PAGER_SYNCHRONOUS_OFF 0x01 /* PRAGMA synchronous=OFF */ #define PAGER_SYNCHRONOUS_NORMAL 0x02 /* PRAGMA synchronous=NORMAL */ @@ -113,16 +139,18 @@ int sqlite3PagerOpen( int, void(*)(DbPage*) ); -int sqlite3PagerClose(Pager *pPager); +int sqlite3PagerClose(Pager *pPager, sqlite3*); int sqlite3PagerReadFileheader(Pager*, int, unsigned char*); /* Functions used to configure a Pager object. */ -void sqlite3PagerSetBusyhandler(Pager*, int(*)(void *), void *); +void sqlite3PagerSetBusyHandler(Pager*, int(*)(void *), void *); int sqlite3PagerSetPagesize(Pager*, u32*, int); +/* BEGIN SQLCIPHER */ #ifdef SQLITE_HAS_CODEC void sqlite3PagerAlignReserve(Pager*,Pager*); #endif -int sqlite3PagerMaxPageCount(Pager*, int); +/* END SQLCIPHER */ +Pgno sqlite3PagerMaxPageCount(Pager*, Pgno); void sqlite3PagerSetCachesize(Pager*, int); int sqlite3PagerSetSpillsize(Pager*, int); void sqlite3PagerSetMmapLimit(Pager *, sqlite3_int64); @@ -142,6 +170,7 @@ DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno); void sqlite3PagerRef(DbPage*); void sqlite3PagerUnref(DbPage*); void sqlite3PagerUnrefNotNull(DbPage*); +void sqlite3PagerUnrefPageOne(DbPage*); /* Operations on page references. */ int sqlite3PagerWrite(DbPage*); @@ -154,9 +183,9 @@ void *sqlite3PagerGetExtra(DbPage *); /* Functions used to manage pager transactions and savepoints. */ void sqlite3PagerPagecount(Pager*, int*); int sqlite3PagerBegin(Pager*, int exFlag, int); -int sqlite3PagerCommitPhaseOne(Pager*,const char *zMaster, int); +int sqlite3PagerCommitPhaseOne(Pager*,const char *zSuper, int); int sqlite3PagerExclusiveLock(Pager*); -int sqlite3PagerSync(Pager *pPager, const char *zMaster); +int sqlite3PagerSync(Pager *pPager, const char *zSuper); int sqlite3PagerCommitPhaseTwo(Pager*); int sqlite3PagerRollback(Pager*); int sqlite3PagerOpenSavepoint(Pager *pPager, int n); @@ -164,17 +193,32 @@ int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint); int sqlite3PagerSharedLock(Pager *pPager); #ifndef SQLITE_OMIT_WAL - int sqlite3PagerCheckpoint(Pager *pPager, int, int*, int*); + int sqlite3PagerCheckpoint(Pager *pPager, sqlite3*, int, int*, int*); int sqlite3PagerWalSupported(Pager *pPager); int sqlite3PagerWalCallback(Pager *pPager); int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen); - int sqlite3PagerCloseWal(Pager *pPager); + int sqlite3PagerCloseWal(Pager *pPager, sqlite3*); # ifdef SQLITE_ENABLE_SNAPSHOT - int sqlite3PagerSnapshotGet(Pager *pPager, sqlite3_snapshot **ppSnapshot); - int sqlite3PagerSnapshotOpen(Pager *pPager, sqlite3_snapshot *pSnapshot); + int sqlite3PagerSnapshotGet(Pager*, sqlite3_snapshot **ppSnapshot); + int sqlite3PagerSnapshotOpen(Pager*, sqlite3_snapshot *pSnapshot); + int sqlite3PagerSnapshotRecover(Pager *pPager); + int sqlite3PagerSnapshotCheck(Pager *pPager, sqlite3_snapshot *pSnapshot); + void sqlite3PagerSnapshotUnlock(Pager *pPager); # endif #endif +#if !defined(SQLITE_OMIT_WAL) && defined(SQLITE_ENABLE_SETLK_TIMEOUT) + int sqlite3PagerWalWriteLock(Pager*, int); + void sqlite3PagerWalDb(Pager*, sqlite3*); +#else +# define sqlite3PagerWalWriteLock(y,z) SQLITE_OK +# define sqlite3PagerWalDb(x,y) +#endif + +#ifdef SQLITE_DIRECT_OVERFLOW_READ + int sqlite3PagerDirectReadOk(Pager *pPager, Pgno pgno); +#endif + #ifdef SQLITE_ENABLE_ZIPVFS int sqlite3PagerWalFramesize(Pager *pPager); #endif @@ -186,16 +230,15 @@ u32 sqlite3PagerDataVersion(Pager*); int sqlite3PagerRefcount(Pager*); #endif int sqlite3PagerMemUsed(Pager*); -const char *sqlite3PagerFilename(Pager*, int); +const char *sqlite3PagerFilename(const Pager*, int); sqlite3_vfs *sqlite3PagerVfs(Pager*); sqlite3_file *sqlite3PagerFile(Pager*); sqlite3_file *sqlite3PagerJrnlFile(Pager*); const char *sqlite3PagerJournalname(Pager*); -int sqlite3PagerNosync(Pager*); void *sqlite3PagerTempSpace(Pager*); int sqlite3PagerIsMemdb(Pager*); -void sqlite3PagerCacheStat(Pager *, int, int, int *); -void sqlite3PagerClearCache(Pager *); +void sqlite3PagerCacheStat(Pager *, int, int, u64*); +void sqlite3PagerClearCache(Pager*); int sqlite3SectorSize(sqlite3_file *); /* Functions used to truncate the database file. */ @@ -203,9 +246,11 @@ void sqlite3PagerTruncateImage(Pager*,Pgno); void sqlite3PagerRekey(DbPage*, Pgno, u16); +/* BEGIN SQLCIPHER */ #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_WAL) -void *sqlite3PagerCodec(DbPage *); +void *sqlcipherPagerCodec(DbPage *); #endif +/* END SQLCIPHER */ /* Functions to support testing and debugging. */ #if !defined(NDEBUG) || defined(SQLITE_TEST) @@ -222,4 +267,8 @@ void *sqlite3PagerCodec(DbPage *); # define enable_simulated_io_errors() #endif -#endif /* _PAGER_H_ */ +#if defined(SQLITE_USE_SEH) && !defined(SQLITE_OMIT_WAL) +int sqlite3PagerWalSystemErrno(Pager*); +#endif + +#endif /* SQLITE_PAGER_H */ diff --git a/src/parse.y b/src/parse.y index 0bfe4e473a..617eb7303b 100644 --- a/src/parse.y +++ b/src/parse.y @@ -1,5 +1,6 @@ +%include { /* -** 2001 September 15 +** 2001-09-15 ** ** The author disclaims copyright to this source code. In place of ** a legal notice, here is a blessing: @@ -9,11 +10,20 @@ ** May you share freely, never taking more than you give. ** ************************************************************************* -** This file contains SQLite's grammar for SQL. Process this file -** using the lemon parser generator to generate C code that runs -** the parser. Lemon will also generate a header file containing -** numeric codes for all of the tokens. +** This file contains SQLite's SQL parser. +** +** The canonical source code to this file ("parse.y") is a Lemon grammar +** file that specifies the input grammar and actions to take while parsing. +** That input file is processed by Lemon to generate a C-language +** implementation of a parser for the given grammar. You might be reading +** this comment as part of the translated C-code. Edits should be made +** to the original parse.y sources. */ +} + +// Function used to enlarge the parser stack, if needed +%realloc parserStackRealloc +%free sqlite3_free // All token codes are small integers with #defines that begin with "TK_" %token_prefix TK_ @@ -24,19 +34,22 @@ %token_type {Token} %default_type {Token} -// The generated parser function takes a 4th argument as follows: -%extra_argument {Parse *pParse} +// An extra argument to the constructor for the parser, which is available +// to all actions. +%extra_context {Parse *pParse} // This code runs whenever there is a syntax error // %syntax_error { UNUSED_PARAMETER(yymajor); /* Silence some compiler warnings */ - assert( TOKEN.z[0] ); /* The tokenizer always gives us a token */ - sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN); + if( TOKEN.z[0] ){ + parserSyntaxError(pParse, &TOKEN); + }else{ + sqlite3ErrorMsg(pParse, "incomplete input"); + } } %stack_overflow { - UNUSED_PARAMETER(yypMinor); /* Silence some compiler warnings */ - sqlite3ErrorMsg(pParse, "parser stack overflow"); + sqlite3OomFault(pParse->db); } // The name of the generated procedure that implements the parser @@ -49,6 +62,11 @@ %include { #include "sqliteInt.h" +/* +** Verify that the pParse->isCreate field is set +*/ +#define ASSERT_IS_CREATE assert(pParse->isCreate) + /* ** Disable all error recovery processing in the parser push-down ** automaton. @@ -67,28 +85,23 @@ #define YYPARSEFREENEVERNULL 1 /* -** Alternative datatype for the argument to the malloc() routine passed -** into sqlite3ParserAlloc(). The default is size_t. -*/ -#define YYMALLOCARGTYPE u64 - -/* -** An instance of this structure holds information about the -** LIMIT clause of a SELECT statement. +** In the amalgamation, the parse.c file generated by lemon and the +** tokenize.c file are concatenated. In that case, sqlite3RunParser() +** has access to the the size of the yyParser object and so the parser +** engine can be allocated from stack. In that case, only the +** sqlite3ParserInit() and sqlite3ParserFinalize() routines are invoked +** and the sqlite3ParserAlloc() and sqlite3ParserFree() routines can be +** omitted. */ -struct LimitVal { - Expr *pLimit; /* The LIMIT expression. NULL if there is no limit */ - Expr *pOffset; /* The OFFSET expression. NULL if there is none */ -}; +#ifdef SQLITE_AMALGAMATION +# define sqlite3Parser_ENGINEALWAYSONSTACK 1 +#endif /* -** An instance of this structure is used to store the LIKE, -** GLOB, NOT LIKE, and NOT GLOB operators. +** Alternative datatype for the argument to the malloc() routine passed +** into sqlite3ParserAlloc(). The default is size_t. */ -struct LikeOp { - Token eOperator; /* "like" or "glob" or "regexp" */ - int bNot; /* True if the NOT keyword is present */ -}; +#define YYMALLOCARGTYPE u64 /* ** An instance of the following structure describes the event of a @@ -101,19 +114,49 @@ struct LikeOp { */ struct TrigEvent { int a; IdList * b; }; +struct FrameBound { int eType; Expr *pExpr; }; + /* -** An instance of this structure holds the ATTACH key and the key type. +** Generate a syntax error */ -struct AttachKey { int type; Token key; }; +static void parserSyntaxError(Parse *pParse, Token *p){ + sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", p); +} /* ** Disable lookaside memory allocation for objects that might be ** shared across database connections. */ static void disableLookaside(Parse *pParse){ + sqlite3 *db = pParse->db; pParse->disableLookaside++; - pParse->db->lookaside.bDisable++; +#ifdef SQLITE_DEBUG + pParse->isCreate = 1; +#endif + memset(&pParse->u1.cr, 0, sizeof(pParse->u1.cr)); + DisableLookaside; +} + +#if !defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) \ + && defined(SQLITE_UDL_CAPABLE_PARSER) +/* +** Issue an error message if an ORDER BY or LIMIT clause occurs on an +** UPDATE or DELETE statement. +*/ +static void updateDeleteLimitError( + Parse *pParse, + ExprList *pOrderBy, + Expr *pLimit +){ + if( pOrderBy ){ + sqlite3ErrorMsg(pParse, "syntax error near \"ORDER BY\""); + }else{ + sqlite3ErrorMsg(pParse, "syntax error near \"LIMIT\""); + } + sqlite3ExprListDelete(pParse->db, pOrderBy); + sqlite3ExprDelete(pParse->db, pLimit); } +#endif /* SQLITE_ENABLE_UPDATE_DELETE_LIMIT */ } // end %include @@ -122,11 +165,11 @@ input ::= cmdlist. cmdlist ::= cmdlist ecmd. cmdlist ::= ecmd. ecmd ::= SEMI. -ecmd ::= explain cmdx SEMI. -explain ::= . +ecmd ::= cmdx SEMI. %ifndef SQLITE_OMIT_EXPLAIN -explain ::= EXPLAIN. { pParse->explain = 1; } -explain ::= EXPLAIN QUERY PLAN. { pParse->explain = 2; } +ecmd ::= explain cmdx SEMI. {NEVER-REDUCE} +explain ::= EXPLAIN. { if( pParse->pReprepare==0 ) pParse->explain = 1; } +explain ::= EXPLAIN QUERY PLAN. { if( pParse->pReprepare==0 ) pParse->explain = 2; } %endif SQLITE_OMIT_EXPLAIN cmdx ::= cmd. { sqlite3FinishCoding(pParse); } @@ -139,12 +182,11 @@ trans_opt ::= TRANSACTION. trans_opt ::= TRANSACTION nm. %type transtype {int} transtype(A) ::= . {A = TK_DEFERRED;} -transtype(A) ::= DEFERRED(X). {A = @X;} -transtype(A) ::= IMMEDIATE(X). {A = @X;} -transtype(A) ::= EXCLUSIVE(X). {A = @X;} -cmd ::= COMMIT trans_opt. {sqlite3CommitTransaction(pParse);} -cmd ::= END trans_opt. {sqlite3CommitTransaction(pParse);} -cmd ::= ROLLBACK trans_opt. {sqlite3RollbackTransaction(pParse);} +transtype(A) ::= DEFERRED(X). {A = @X; /*A-overwrites-X*/} +transtype(A) ::= IMMEDIATE(X). {A = @X; /*A-overwrites-X*/} +transtype(A) ::= EXCLUSIVE(X). {A = @X; /*A-overwrites-X*/} +cmd ::= COMMIT|END(X) trans_opt. {sqlite3EndTransaction(pParse,@X);} +cmd ::= ROLLBACK(X) trans_opt. {sqlite3EndTransaction(pParse,@X);} savepoint_opt ::= SAVEPOINT. savepoint_opt ::= . @@ -164,28 +206,31 @@ cmd ::= create_table create_table_args. create_table ::= createkw temp(T) TABLE ifnotexists(E) nm(Y) dbnm(Z). { sqlite3StartTable(pParse,&Y,&Z,T,0,0,E); } -createkw(A) ::= CREATE(X). { +createkw(A) ::= CREATE(A). { disableLookaside(pParse); - A = X; } + %type ifnotexists {int} ifnotexists(A) ::= . {A = 0;} ifnotexists(A) ::= IF NOT EXISTS. {A = 1;} %type temp {int} %ifndef SQLITE_OMIT_TEMPDB -temp(A) ::= TEMP. {A = 1;} +temp(A) ::= TEMP. {A = pParse->db->init.busy==0;} %endif SQLITE_OMIT_TEMPDB temp(A) ::= . {A = 0;} -create_table_args ::= LP columnlist conslist_opt(X) RP(E) table_options(F). { +create_table_args ::= LP columnlist conslist_opt(X) RP(E) table_option_set(F). { sqlite3EndTable(pParse,&X,&E,F,0); } create_table_args ::= AS select(S). { sqlite3EndTable(pParse,0,0,0,S); sqlite3SelectDelete(pParse->db, S); } -%type table_options {int} -table_options(A) ::= . {A = 0;} -table_options(A) ::= WITHOUT nm(X). { +%type table_option_set {u32} +%type table_option {u32} +table_option_set(A) ::= . {A = 0;} +table_option_set(A) ::= table_option(A). +table_option_set(A) ::= table_option_set(X) COMMA table_option(Y). {A = X|Y;} +table_option(A) ::= WITHOUT nm(X). { if( X.n==5 && sqlite3_strnicmp(X.z,"rowid",5)==0 ){ A = TF_WithoutRowid | TF_NoVisibleRowid; }else{ @@ -193,29 +238,30 @@ table_options(A) ::= WITHOUT nm(X). { sqlite3ErrorMsg(pParse, "unknown table option: %.*s", X.n, X.z); } } -columnlist ::= columnlist COMMA column. -columnlist ::= column. - -// A "column" is a complete description of a single column in a -// CREATE TABLE statement. This includes the column name, its -// datatype, and other keywords such as PRIMARY KEY, UNIQUE, REFERENCES, -// NOT NULL and so forth. -// -column(A) ::= columnid(X) type carglist. { - A.z = X.z; - A.n = (int)(pParse->sLastToken.z-X.z) + pParse->sLastToken.n; -} -columnid(A) ::= nm(X). { - sqlite3AddColumn(pParse,&X); - A = X; - pParse->constraintName.n = 0; +table_option(A) ::= nm(X). { + if( X.n==6 && sqlite3_strnicmp(X.z,"strict",6)==0 ){ + A = TF_Strict; + }else{ + A = 0; + sqlite3ErrorMsg(pParse, "unknown table option: %.*s", X.n, X.z); + } } - - -// An IDENTIFIER can be a generic identifier, or one of several -// keywords. Any non-standard keyword can also be an identifier. +columnlist ::= columnlist COMMA columnname carglist. +columnlist ::= columnname carglist. +columnname(A) ::= nm(A) typetoken(Y). {sqlite3AddColumn(pParse,A,Y);} + +// Declare some tokens early in order to influence their values, to +// improve performance and reduce the executable size. The goal here is +// to get the "jump" operations in ISNULL through ESCAPE to have numeric +// values that are early enough so that all jump operations are clustered +// at the beginning. Also, operators like NE and EQ need to be adjacent, +// and all of the comparison operators need to be clustered together. +// Various assert() statements throughout the code enforce these restrictions. // -%token_class id ID|INDEXED. +%token ABORT ACTION AFTER ANALYZE ASC ATTACH BEFORE BEGIN BY CASCADE CAST. +%token CONFLICT DATABASE DEFERRED DESC DETACH EACH END EXCLUSIVE EXPLAIN FAIL. +%token OR AND NOT IS ISNOT MATCH LIKE_KW BETWEEN IN ISNULL NOTNULL NE EQ. +%token GT LE LT GE ESCAPE. // The following directive causes tokens ABORT, AFTER, ASC, etc. to // fallback to ID if they will not parse as their original value. @@ -223,19 +269,32 @@ columnid(A) ::= nm(X). { // %fallback ID ABORT ACTION AFTER ANALYZE ASC ATTACH BEFORE BEGIN BY CASCADE CAST COLUMNKW - CONFLICT DATABASE DEFERRED DESC DETACH EACH END EXCLUSIVE EXPLAIN FAIL FOR + CONFLICT DATABASE DEFERRED DESC DETACH DO + EACH END EXCLUSIVE EXPLAIN FAIL FOR IGNORE IMMEDIATE INITIALLY INSTEAD LIKE_KW MATCH NO PLAN - QUERY KEY OF OFFSET PRAGMA RAISE RECURSIVE RELEASE REPLACE RESTRICT ROW + QUERY KEY OF OFFSET PRAGMA RAISE RECURSIVE RELEASE REPLACE RESTRICT ROW ROWS ROLLBACK SAVEPOINT TEMP TRIGGER VACUUM VIEW VIRTUAL WITH WITHOUT + NULLS FIRST LAST %ifdef SQLITE_OMIT_COMPOUND_SELECT EXCEPT INTERSECT UNION %endif SQLITE_OMIT_COMPOUND_SELECT +%ifndef SQLITE_OMIT_WINDOWFUNC + CURRENT FOLLOWING PARTITION PRECEDING RANGE UNBOUNDED + EXCLUDE GROUPS OTHERS TIES +%endif SQLITE_OMIT_WINDOWFUNC +%ifdef SQLITE_ENABLE_ORDERED_SET_AGGREGATES + WITHIN +%endif SQLITE_ENABLE_ORDERED_SET_AGGREGATES +%ifndef SQLITE_OMIT_GENERATED_COLUMNS + GENERATED ALWAYS +%endif + MATERIALIZED REINDEX RENAME CTIME_KW IF . %wildcard ANY. // Define operator precedence early so that this is the first occurrence -// of the operator tokens in the grammer. Keeping the operators together +// of the operator tokens in the grammar. Keeping the operators together // causes them to be assigned integer values that are close together, // which keeps parser tables smaller. // @@ -254,63 +313,95 @@ columnid(A) ::= nm(X). { %left BITAND BITOR LSHIFT RSHIFT. %left PLUS MINUS. %left STAR SLASH REM. -%left CONCAT. +%left CONCAT PTR. %left COLLATE. %right BITNOT. +%nonassoc ON. + +// An IDENTIFIER can be a generic identifier, or one of several +// keywords. Any non-standard keyword can also be an identifier. +// +%token_class id ID|INDEXED. -// And "ids" is an identifer-or-string. +// And "ids" is an identifier-or-string. // %token_class ids ID|STRING. +// An identifier or a join-keyword +// +%token_class idj ID|INDEXED|JOIN_KW. + // The name of a column or table can be any of the following: // %type nm {Token} -nm(A) ::= id(X). {A = X;} -nm(A) ::= STRING(X). {A = X;} -nm(A) ::= JOIN_KW(X). {A = X;} +nm(A) ::= idj(A). +nm(A) ::= STRING(A). -// A typetoken is really one or more tokens that form a type name such +// A typetoken is really zero or more tokens that form a type name such // as can be found after the column name in a CREATE TABLE statement. // Multiple tokens are concatenated to form the value of the typetoken. // %type typetoken {Token} -type ::= . -type ::= typetoken(X). {sqlite3AddColumnType(pParse,&X);} -typetoken(A) ::= typename(X). {A = X;} -typetoken(A) ::= typename(X) LP signed RP(Y). { - A.z = X.z; - A.n = (int)(&Y.z[Y.n] - X.z); +typetoken(A) ::= . {A.n = 0; A.z = 0;} +typetoken(A) ::= typename(A). +typetoken(A) ::= typename(A) LP signed RP(Y). { + A.n = (int)(&Y.z[Y.n] - A.z); } -typetoken(A) ::= typename(X) LP signed COMMA signed RP(Y). { - A.z = X.z; - A.n = (int)(&Y.z[Y.n] - X.z); +typetoken(A) ::= typename(A) LP signed COMMA signed RP(Y). { + A.n = (int)(&Y.z[Y.n] - A.z); } %type typename {Token} -typename(A) ::= ids(X). {A = X;} -typename(A) ::= typename(X) ids(Y). {A.z=X.z; A.n=Y.n+(int)(Y.z-X.z);} +typename(A) ::= ids(A). +typename(A) ::= typename(A) ids(Y). {A.n=Y.n+(int)(Y.z-A.z);} signed ::= plus_num. signed ::= minus_num. +// The scanpt non-terminal takes a value which is a pointer to the +// input text just past the last token that has been shifted into +// the parser. By surrounding some phrase in the grammar with two +// scanpt non-terminals, we can capture the input text for that phrase. +// For example: +// +// something ::= .... scanpt(A) phrase scanpt(Z). +// +// The text that is parsed as "phrase" is a string starting at A +// and containing (int)(Z-A) characters. There might be some extra +// whitespace on either end of the text, but that can be removed in +// post-processing, if needed. +// +%type scanpt {const char*} +scanpt(A) ::= . { + assert( yyLookahead!=YYNOCODE ); + A = yyLookaheadToken.z; +} +scantok(A) ::= . { + assert( yyLookahead!=YYNOCODE ); + A = yyLookaheadToken; +} + // "carglist" is a list of additional constraints that come after the // column name and column type in a CREATE TABLE statement. // carglist ::= carglist ccons. carglist ::= . -ccons ::= CONSTRAINT nm(X). {pParse->constraintName = X;} -ccons ::= DEFAULT term(X). {sqlite3AddDefaultValue(pParse,&X);} -ccons ::= DEFAULT LP expr(X) RP. {sqlite3AddDefaultValue(pParse,&X);} -ccons ::= DEFAULT PLUS term(X). {sqlite3AddDefaultValue(pParse,&X);} -ccons ::= DEFAULT MINUS(A) term(X). { - ExprSpan v; - v.pExpr = sqlite3PExpr(pParse, TK_UMINUS, X.pExpr, 0, 0); - v.zStart = A.z; - v.zEnd = X.zEnd; - sqlite3AddDefaultValue(pParse,&v); -} -ccons ::= DEFAULT id(X). { - ExprSpan v; - spanExpr(&v, pParse, TK_STRING, &X); - sqlite3AddDefaultValue(pParse,&v); +ccons ::= CONSTRAINT nm(X). {ASSERT_IS_CREATE; pParse->u1.cr.constraintName = X;} +ccons ::= DEFAULT scantok(A) term(X). + {sqlite3AddDefaultValue(pParse,X,A.z,&A.z[A.n]);} +ccons ::= DEFAULT LP(A) expr(X) RP(Z). + {sqlite3AddDefaultValue(pParse,X,A.z+1,Z.z);} +ccons ::= DEFAULT PLUS(A) scantok(Z) term(X). + {sqlite3AddDefaultValue(pParse,X,A.z,&Z.z[Z.n]);} +ccons ::= DEFAULT MINUS(A) scantok(Z) term(X). { + Expr *p = sqlite3PExpr(pParse, TK_UMINUS, X, 0); + sqlite3AddDefaultValue(pParse,p,A.z,&Z.z[Z.n]); +} +ccons ::= DEFAULT scantok id(X). { + Expr *p = tokenExpr(pParse, TK_STRING, X); + if( p ){ + sqlite3ExprIdToTrueFalse(p); + testcase( p->op==TK_TRUEFALSE && sqlite3ExprTruthValue(p) ); + } + sqlite3AddDefaultValue(pParse,p,X.z,X.z+X.n); } // In addition to the type name, we also care about the primary key and @@ -320,12 +411,17 @@ ccons ::= NULL onconf. ccons ::= NOT NULL onconf(R). {sqlite3AddNotNull(pParse, R);} ccons ::= PRIMARY KEY sortorder(Z) onconf(R) autoinc(I). {sqlite3AddPrimaryKey(pParse,0,R,I,Z);} -ccons ::= UNIQUE onconf(R). {sqlite3CreateIndex(pParse,0,0,0,0,R,0,0,0,0);} -ccons ::= CHECK LP expr(X) RP. {sqlite3AddCheckConstraint(pParse,X.pExpr);} +ccons ::= UNIQUE onconf(R). {sqlite3CreateIndex(pParse,0,0,0,0,R,0,0,0,0, + SQLITE_IDXTYPE_UNIQUE);} +ccons ::= CHECK LP(A) expr(X) RP(B). {sqlite3AddCheckConstraint(pParse,X,A.z,B.z);} ccons ::= REFERENCES nm(T) eidlist_opt(TA) refargs(R). {sqlite3CreateForeignKey(pParse,0,&T,TA,R);} ccons ::= defer_subclause(D). {sqlite3DeferForeignKey(pParse,D);} ccons ::= COLLATE ids(C). {sqlite3AddCollateType(pParse, &C);} +ccons ::= GENERATED ALWAYS AS generated. +ccons ::= AS generated. +generated ::= LP expr(E) RP. {sqlite3AddGenerated(pParse,E,0);} +generated ::= LP expr(E) RP ID(TYPE). {sqlite3AddGenerated(pParse,E,&TYPE);} // The optional AUTOINCREMENT keyword %type autoinc {int} @@ -339,7 +435,7 @@ autoinc(X) ::= AUTOINCR. {X = 1;} // %type refargs {int} refargs(A) ::= . { A = OE_None*0x0101; /* EV: R-19803-45884 */} -refargs(A) ::= refargs(X) refarg(Y). { A = (X & ~Y.mask) | Y.value; } +refargs(A) ::= refargs(A) refarg(Y). { A = (A & ~Y.mask) | Y.value; } %type refarg {struct {int value; int mask;}} refarg(A) ::= MATCH nm. { A.value = 0; A.mask = 0x000000; } refarg(A) ::= ON INSERT refact. { A.value = 0; A.mask = 0x000000; } @@ -360,18 +456,19 @@ init_deferred_pred_opt(A) ::= INITIALLY DEFERRED. {A = 1;} init_deferred_pred_opt(A) ::= INITIALLY IMMEDIATE. {A = 0;} conslist_opt(A) ::= . {A.n = 0; A.z = 0;} -conslist_opt(A) ::= COMMA(X) conslist. {A = X;} +conslist_opt(A) ::= COMMA(A) conslist. conslist ::= conslist tconscomma tcons. conslist ::= tcons. -tconscomma ::= COMMA. {pParse->constraintName.n = 0;} +tconscomma ::= COMMA. {ASSERT_IS_CREATE; pParse->u1.cr.constraintName.n = 0;} tconscomma ::= . -tcons ::= CONSTRAINT nm(X). {pParse->constraintName = X;} +tcons ::= CONSTRAINT nm(X). {ASSERT_IS_CREATE; pParse->u1.cr.constraintName = X;} tcons ::= PRIMARY KEY LP sortlist(X) autoinc(I) RP onconf(R). {sqlite3AddPrimaryKey(pParse,X,R,I,0);} tcons ::= UNIQUE LP sortlist(X) RP onconf(R). - {sqlite3CreateIndex(pParse,0,0,0,X,R,0,0,0,0);} -tcons ::= CHECK LP expr(E) RP onconf. - {sqlite3AddCheckConstraint(pParse,E.pExpr);} + {sqlite3CreateIndex(pParse,0,0,0,X,R,0,0,0,0, + SQLITE_IDXTYPE_UNIQUE);} +tcons ::= CHECK LP(A) expr(E) RP(B) onconf. + {sqlite3AddCheckConstraint(pParse,E,A.z,B.z);} tcons ::= FOREIGN KEY LP eidlist(FA) RP REFERENCES nm(T) eidlist_opt(TA) refargs(R) defer_subclause_opt(D). { sqlite3CreateForeignKey(pParse, FA, &T, TA, R); @@ -379,7 +476,7 @@ tcons ::= FOREIGN KEY LP eidlist(FA) RP } %type defer_subclause_opt {int} defer_subclause_opt(A) ::= . {A = 0;} -defer_subclause_opt(A) ::= defer_subclause(X). {A = X;} +defer_subclause_opt(A) ::= defer_subclause(A). // The following is a non-standard extension that allows us to declare the // default behavior when there is a constraint conflict. @@ -391,7 +488,7 @@ onconf(A) ::= . {A = OE_Default;} onconf(A) ::= ON CONFLICT resolvetype(X). {A = X;} orconf(A) ::= . {A = OE_Default;} orconf(A) ::= OR resolvetype(X). {A = X;} -resolvetype(A) ::= raisetype(X). {A = X;} +resolvetype(A) ::= raisetype(A). resolvetype(A) ::= IGNORE. {A = OE_Ignore;} resolvetype(A) ::= REPLACE. {A = OE_Replace;} @@ -419,8 +516,12 @@ cmd ::= DROP VIEW ifexists(E) fullname(X). { //////////////////////// The SELECT statement ///////////////////////////////// // cmd ::= select(X). { - SelectDest dest = {SRT_Output, 0, 0, 0, 0, 0}; - sqlite3Select(pParse, X, &dest); + SelectDest dest = {SRT_Output, 0, 0, 0, 0, 0, 0}; + if( (pParse->db->mDbFlags & DBFLAG_EncodingFixed)!=0 + || sqlite3ReadSchema(pParse)==SQLITE_OK + ){ + sqlite3Select(pParse, X, &dest); + } sqlite3SelectDelete(pParse->db, X); } @@ -438,52 +539,86 @@ cmd ::= select(X). { ** SQLITE_LIMIT_COMPOUND_SELECT. */ static void parserDoubleLinkSelect(Parse *pParse, Select *p){ + assert( p!=0 ); if( p->pPrior ){ - Select *pNext = 0, *pLoop; - int mxSelect, cnt = 0; - for(pLoop=p; pLoop; pNext=pLoop, pLoop=pLoop->pPrior, cnt++){ + Select *pNext = 0, *pLoop = p; + int mxSelect, cnt = 1; + while(1){ pLoop->pNext = pNext; pLoop->selFlags |= SF_Compound; + pNext = pLoop; + pLoop = pLoop->pPrior; + if( pLoop==0 ) break; + cnt++; + if( pLoop->pOrderBy || pLoop->pLimit ){ + sqlite3ErrorMsg(pParse,"%s clause should come after %s not before", + pLoop->pOrderBy!=0 ? "ORDER BY" : "LIMIT", + sqlite3SelectOpName(pNext->op)); + break; + } } - if( (p->selFlags & SF_MultiValue)==0 && - (mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT])>0 && - cnt>mxSelect + if( (p->selFlags & (SF_MultiValue|SF_Values))==0 + && (mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT])>0 + && cnt>mxSelect ){ sqlite3ErrorMsg(pParse, "too many terms in compound SELECT"); } } } + + /* Attach a With object describing the WITH clause to a Select + ** object describing the query for which the WITH clause is a prefix. + */ + static Select *attachWithToSelect(Parse *pParse, Select *pSelect, With *pWith){ + if( pSelect ){ + pSelect->pWith = pWith; + parserDoubleLinkSelect(pParse, pSelect); + }else{ + sqlite3WithDelete(pParse->db, pWith); + } + return pSelect; + } + + /* Memory allocator for parser stack resizing. This is a thin wrapper around + ** sqlite3_realloc() that includes a call to sqlite3FaultSim() to facilitate + ** testing. + */ + static void *parserStackRealloc(void *pOld, sqlite3_uint64 newSize){ + return sqlite3FaultSim(700) ? 0 : sqlite3_realloc(pOld, newSize); + } } -select(A) ::= with(W) selectnowith(X). { - Select *p = X; +%ifndef SQLITE_OMIT_CTE +select(A) ::= WITH wqlist(W) selectnowith(X). {A = attachWithToSelect(pParse,X,W);} +select(A) ::= WITH RECURSIVE wqlist(W) selectnowith(X). + {A = attachWithToSelect(pParse,X,W);} + +%endif /* SQLITE_OMIT_CTE */ +select(A) ::= selectnowith(A). { + Select *p = A; if( p ){ - p->pWith = W; parserDoubleLinkSelect(pParse, p); - }else{ - sqlite3WithDelete(pParse->db, W); } - A = p; } -selectnowith(A) ::= oneselect(X). {A = X;} +selectnowith(A) ::= oneselect(A). %ifndef SQLITE_OMIT_COMPOUND_SELECT -selectnowith(A) ::= selectnowith(X) multiselect_op(Y) oneselect(Z). { +selectnowith(A) ::= selectnowith(A) multiselect_op(Y) oneselect(Z). { Select *pRhs = Z; - Select *pLhs = X; + Select *pLhs = A; if( pRhs && pRhs->pPrior ){ SrcList *pFrom; Token x; x.n = 0; parserDoubleLinkSelect(pParse, pRhs); - pFrom = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&x,pRhs,0,0); - pRhs = sqlite3SelectNew(pParse,0,pFrom,0,0,0,0,0,0,0); + pFrom = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&x,pRhs,0); + pRhs = sqlite3SelectNew(pParse,0,pFrom,0,0,0,0,0,0); } if( pRhs ){ pRhs->op = (u8)Y; pRhs->pPrior = pLhs; - if( ALWAYS(pLhs) ) pLhs->selFlags &= ~SF_MultiValue; - pRhs->selFlags &= ~SF_MultiValue; + if( ALWAYS(pLhs) ) pLhs->selFlags &= ~(u32)SF_MultiValue; + pRhs->selFlags &= ~(u32)SF_MultiValue; if( Y!=TK_ALL ) pParse->hasCompound = 1; }else{ sqlite3SelectDelete(pParse->db, pLhs); @@ -491,57 +626,51 @@ selectnowith(A) ::= selectnowith(X) multiselect_op(Y) oneselect(Z). { A = pRhs; } %type multiselect_op {int} -multiselect_op(A) ::= UNION(OP). {A = @OP;} +multiselect_op(A) ::= UNION(OP). {A = @OP; /*A-overwrites-OP*/} multiselect_op(A) ::= UNION ALL. {A = TK_ALL;} -multiselect_op(A) ::= EXCEPT|INTERSECT(OP). {A = @OP;} +multiselect_op(A) ::= EXCEPT|INTERSECT(OP). {A = @OP; /*A-overwrites-OP*/} %endif SQLITE_OMIT_COMPOUND_SELECT -oneselect(A) ::= SELECT(S) distinct(D) selcollist(W) from(X) where_opt(Y) - groupby_opt(P) having_opt(Q) orderby_opt(Z) limit_opt(L). { - A = sqlite3SelectNew(pParse,W,X,Y,P,Q,Z,D,L.pLimit,L.pOffset); -#if SELECTTRACE_ENABLED - /* Populate the Select.zSelName[] string that is used to help with - ** query planner debugging, to differentiate between multiple Select - ** objects in a complex query. - ** - ** If the SELECT keyword is immediately followed by a C-style comment - ** then extract the first few alphanumeric characters from within that - ** comment to be the zSelName value. Otherwise, the label is #N where - ** is an integer that is incremented with each SELECT statement seen. - */ - if( A!=0 ){ - const char *z = S.z+6; - int i; - sqlite3_snprintf(sizeof(A->zSelName), A->zSelName, "#%d", - ++pParse->nSelect); - while( z[0]==' ' ) z++; - if( z[0]=='/' && z[1]=='*' ){ - z += 2; - while( z[0]==' ' ) z++; - for(i=0; sqlite3Isalnum(z[i]); i++){} - sqlite3_snprintf(sizeof(A->zSelName), A->zSelName, "%.*s", i, z); - } + +oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y) + groupby_opt(P) having_opt(Q) + orderby_opt(Z) limit_opt(L). { + A = sqlite3SelectNew(pParse,W,X,Y,P,Q,Z,D,L); +} +%ifndef SQLITE_OMIT_WINDOWFUNC +oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y) + groupby_opt(P) having_opt(Q) window_clause(R) + orderby_opt(Z) limit_opt(L). { + A = sqlite3SelectNew(pParse,W,X,Y,P,Q,Z,D,L); + if( A ){ + A->pWinDefn = R; + }else{ + sqlite3WindowListDelete(pParse->db, R); } -#endif /* SELECTRACE_ENABLED */ } -oneselect(A) ::= values(X). {A = X;} +%endif + +// Single row VALUES clause. +// %type values {Select*} +oneselect(A) ::= values(A). %destructor values {sqlite3SelectDelete(pParse->db, $$);} values(A) ::= VALUES LP nexprlist(X) RP. { - A = sqlite3SelectNew(pParse,X,0,0,0,0,0,SF_Values,0,0); -} -values(A) ::= values(X) COMMA LP exprlist(Y) RP. { - Select *pRight, *pLeft = X; - pRight = sqlite3SelectNew(pParse,Y,0,0,0,0,0,SF_Values|SF_MultiValue,0,0); - if( ALWAYS(pLeft) ) pLeft->selFlags &= ~SF_MultiValue; - if( pRight ){ - pRight->op = TK_ALL; - pLeft = X; - pRight->pPrior = pLeft; - A = pRight; - }else{ - A = pLeft; - } + A = sqlite3SelectNew(pParse,X,0,0,0,0,0,SF_Values,0); +} + +// Multiple row VALUES clause. +// +%type mvalues {Select*} +oneselect(A) ::= mvalues(A). { + sqlite3MultiValuesEnd(pParse, A); +} +%destructor mvalues {sqlite3SelectDelete(pParse->db, $$);} +mvalues(A) ::= values(A) COMMA LP nexprlist(Y) RP. { + A = sqlite3MultiValues(pParse, A, Y); +} +mvalues(A) ::= mvalues(A) COMMA LP nexprlist(Y) RP. { + A = sqlite3MultiValues(pParse, A, Y); } // The "distinct" nonterminal is true (1) if the DISTINCT keyword is @@ -561,22 +690,25 @@ distinct(A) ::= . {A = 0;} %destructor selcollist {sqlite3ExprListDelete(pParse->db, $$);} %type sclp {ExprList*} %destructor sclp {sqlite3ExprListDelete(pParse->db, $$);} -sclp(A) ::= selcollist(X) COMMA. {A = X;} +sclp(A) ::= selcollist(A) COMMA. sclp(A) ::= . {A = 0;} -selcollist(A) ::= sclp(P) expr(X) as(Y). { - A = sqlite3ExprListAppend(pParse, P, X.pExpr); +selcollist(A) ::= sclp(A) scanpt(B) expr(X) scanpt(Z) as(Y). { + A = sqlite3ExprListAppend(pParse, A, X); if( Y.n>0 ) sqlite3ExprListSetName(pParse, A, &Y, 1); - sqlite3ExprListSetSpan(pParse,A,&X); + sqlite3ExprListSetSpan(pParse,A,B,Z); } -selcollist(A) ::= sclp(P) STAR. { +selcollist(A) ::= sclp(A) scanpt STAR(X). { Expr *p = sqlite3Expr(pParse->db, TK_ASTERISK, 0); - A = sqlite3ExprListAppend(pParse, P, p); + sqlite3ExprSetErrorOffset(p, (int)(X.z - pParse->zTail)); + A = sqlite3ExprListAppend(pParse, A, p); } -selcollist(A) ::= sclp(P) nm(X) DOT STAR(Y). { - Expr *pRight = sqlite3PExpr(pParse, TK_ASTERISK, 0, 0, &Y); - Expr *pLeft = sqlite3PExpr(pParse, TK_ID, 0, 0, &X); - Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0); - A = sqlite3ExprListAppend(pParse,P, pDot); +selcollist(A) ::= sclp(A) scanpt nm(X) DOT STAR(Y). { + Expr *pRight, *pLeft, *pDot; + pRight = sqlite3PExpr(pParse, TK_ASTERISK, 0, 0); + sqlite3ExprSetErrorOffset(pRight, (int)(Y.z - pParse->zTail)); + pLeft = tokenExpr(pParse, TK_ID, X); + pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight); + A = sqlite3ExprListAppend(pParse,A, pDot); } // An option "AS " phrase that can follow one of the expressions that @@ -584,8 +716,8 @@ selcollist(A) ::= sclp(P) nm(X) DOT STAR(Y). { // %type as {Token} as(X) ::= AS nm(Y). {X = Y;} -as(X) ::= ids(Y). {X = Y;} -as(X) ::= . {X.n = 0;} +as(X) ::= ids(X). +as(X) ::= . {X.n = 0; X.z = 0;} %type seltablist {SrcList*} @@ -597,56 +729,72 @@ as(X) ::= . {X.n = 0;} // A complete FROM clause. // -from(A) ::= . {A = sqlite3DbMallocZero(pParse->db, sizeof(*A));} +from(A) ::= . {A = 0;} from(A) ::= FROM seltablist(X). { A = X; - sqlite3SrcListShiftJoinType(A); + sqlite3SrcListShiftJoinType(pParse,A); } // "seltablist" is a "Select Table List" - the content of the FROM clause // in a SELECT statement. "stl_prefix" is a prefix of this list. // -stl_prefix(A) ::= seltablist(X) joinop(Y). { - A = X; +stl_prefix(A) ::= seltablist(A) joinop(Y). { if( ALWAYS(A && A->nSrc>0) ) A->a[A->nSrc-1].fg.jointype = (u8)Y; } stl_prefix(A) ::= . {A = 0;} -seltablist(A) ::= stl_prefix(X) nm(Y) dbnm(D) as(Z) indexed_opt(I) - on_opt(N) using_opt(U). { - A = sqlite3SrcListAppendFromTerm(pParse,X,&Y,&D,&Z,0,N,U); +seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) as(Z) on_using(N). { + A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,&N); +} +seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) as(Z) indexed_by(I) on_using(N). { + A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,&N); sqlite3SrcListIndexedBy(pParse, A, &I); } -seltablist(A) ::= stl_prefix(X) nm(Y) dbnm(D) LP exprlist(E) RP as(Z) - on_opt(N) using_opt(U). { - A = sqlite3SrcListAppendFromTerm(pParse,X,&Y,&D,&Z,0,N,U); +seltablist(A) ::= stl_prefix(A) nm(Y) dbnm(D) LP exprlist(E) RP as(Z) on_using(N). { + A = sqlite3SrcListAppendFromTerm(pParse,A,&Y,&D,&Z,0,&N); sqlite3SrcListFuncArgs(pParse, A, E); } %ifndef SQLITE_OMIT_SUBQUERY - seltablist(A) ::= stl_prefix(X) LP select(S) RP - as(Z) on_opt(N) using_opt(U). { - A = sqlite3SrcListAppendFromTerm(pParse,X,0,0,&Z,S,N,U); + seltablist(A) ::= stl_prefix(A) LP select(S) RP as(Z) on_using(N). { + A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,S,&N); } - seltablist(A) ::= stl_prefix(X) LP seltablist(F) RP - as(Z) on_opt(N) using_opt(U). { - if( X==0 && Z.n==0 && N==0 && U==0 ){ + seltablist(A) ::= stl_prefix(A) LP seltablist(F) RP as(Z) on_using(N). { + if( A==0 && Z.n==0 && N.pOn==0 && N.pUsing==0 ){ A = F; - }else if( F->nSrc==1 ){ - A = sqlite3SrcListAppendFromTerm(pParse,X,0,0,&Z,0,N,U); + }else if( ALWAYS(F!=0) && F->nSrc==1 ){ + A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,0,&N); if( A ){ - struct SrcList_item *pNew = &A->a[A->nSrc-1]; - struct SrcList_item *pOld = F->a; + SrcItem *pNew = &A->a[A->nSrc-1]; + SrcItem *pOld = F->a; + assert( pOld->fg.fixedSchema==0 ); pNew->zName = pOld->zName; - pNew->zDatabase = pOld->zDatabase; - pNew->pSelect = pOld->pSelect; - pOld->zName = pOld->zDatabase = 0; - pOld->pSelect = 0; + assert( pOld->fg.fixedSchema==0 ); + if( pOld->fg.isSubquery ){ + pNew->fg.isSubquery = 1; + pNew->u4.pSubq = pOld->u4.pSubq; + pOld->u4.pSubq = 0; + pOld->fg.isSubquery = 0; + assert( pNew->u4.pSubq!=0 && pNew->u4.pSubq->pSelect!=0 ); + if( (pNew->u4.pSubq->pSelect->selFlags & SF_NestedFrom)!=0 ){ + pNew->fg.isNestedFrom = 1; + } + }else{ + pNew->u4.zDatabase = pOld->u4.zDatabase; + pOld->u4.zDatabase = 0; + } + if( pOld->fg.isTabFunc ){ + pNew->u1.pFuncArg = pOld->u1.pFuncArg; + pOld->u1.pFuncArg = 0; + pOld->fg.isTabFunc = 0; + pNew->fg.isTabFunc = 1; + } + pOld->zName = 0; } sqlite3SrcListDelete(pParse->db, F); }else{ Select *pSubquery; - sqlite3SrcListShiftJoinType(F); - pSubquery = sqlite3SelectNew(pParse,0,F,0,0,0,0,SF_NestedFrom,0,0); - A = sqlite3SrcListAppendFromTerm(pParse,X,0,0,&Z,pSubquery,N,U); + sqlite3SrcListShiftJoinType(pParse,F); + pSubquery = sqlite3SelectNew(pParse,0,F,0,0,0,0,SF_NestedFrom,0); + A = sqlite3SrcListAppendFromTerm(pParse,A,0,0,&Z,pSubquery,&N); } } %endif SQLITE_OMIT_SUBQUERY @@ -657,19 +805,61 @@ dbnm(A) ::= DOT nm(X). {A = X;} %type fullname {SrcList*} %destructor fullname {sqlite3SrcListDelete(pParse->db, $$);} -fullname(A) ::= nm(X) dbnm(Y). {A = sqlite3SrcListAppend(pParse->db,0,&X,&Y);} +fullname(A) ::= nm(X). { + A = sqlite3SrcListAppend(pParse,0,&X,0); + if( IN_RENAME_OBJECT && A ) sqlite3RenameTokenMap(pParse, A->a[0].zName, &X); +} +fullname(A) ::= nm(X) DOT nm(Y). { + A = sqlite3SrcListAppend(pParse,0,&X,&Y); + if( IN_RENAME_OBJECT && A ) sqlite3RenameTokenMap(pParse, A->a[0].zName, &Y); +} + +%type xfullname {SrcList*} +%destructor xfullname {sqlite3SrcListDelete(pParse->db, $$);} +xfullname(A) ::= nm(X). + {A = sqlite3SrcListAppend(pParse,0,&X,0); /*A-overwrites-X*/} +xfullname(A) ::= nm(X) DOT nm(Y). + {A = sqlite3SrcListAppend(pParse,0,&X,&Y); /*A-overwrites-X*/} +xfullname(A) ::= nm(X) DOT nm(Y) AS nm(Z). { + A = sqlite3SrcListAppend(pParse,0,&X,&Y); /*A-overwrites-X*/ + if( A ) A->a[0].zAlias = sqlite3NameFromToken(pParse->db, &Z); +} +xfullname(A) ::= nm(X) AS nm(Z). { + A = sqlite3SrcListAppend(pParse,0,&X,0); /*A-overwrites-X*/ + if( A ) A->a[0].zAlias = sqlite3NameFromToken(pParse->db, &Z); +} %type joinop {int} joinop(X) ::= COMMA|JOIN. { X = JT_INNER; } -joinop(X) ::= JOIN_KW(A) JOIN. { X = sqlite3JoinType(pParse,&A,0,0); } -joinop(X) ::= JOIN_KW(A) nm(B) JOIN. { X = sqlite3JoinType(pParse,&A,&B,0); } +joinop(X) ::= JOIN_KW(A) JOIN. + {X = sqlite3JoinType(pParse,&A,0,0); /*X-overwrites-A*/} +joinop(X) ::= JOIN_KW(A) nm(B) JOIN. + {X = sqlite3JoinType(pParse,&A,&B,0); /*X-overwrites-A*/} joinop(X) ::= JOIN_KW(A) nm(B) nm(C) JOIN. - { X = sqlite3JoinType(pParse,&A,&B,&C); } + {X = sqlite3JoinType(pParse,&A,&B,&C);/*X-overwrites-A*/} -%type on_opt {Expr*} -%destructor on_opt {sqlite3ExprDelete(pParse->db, $$);} -on_opt(N) ::= ON expr(E). {N = E.pExpr;} -on_opt(N) ::= . {N = 0;} +// There is a parsing ambiguity in an upsert statement that uses a +// SELECT on the RHS of a the INSERT: +// +// INSERT INTO tab SELECT * FROM aaa JOIN bbb ON CONFLICT ... +// here ----^^ +// +// When the ON token is encountered, the parser does not know if it is +// the beginning of an ON CONFLICT clause, or the beginning of an ON +// clause associated with the JOIN. The conflict is resolved in favor +// of the JOIN. If an ON CONFLICT clause is intended, insert a dummy +// WHERE clause in between, like this: +// +// INSERT INTO tab SELECT * FROM aaa JOIN bbb WHERE true ON CONFLICT ... +// +// The [AND] and [OR] precedence marks in the rules for on_using cause the +// ON in this context to always be interpreted as belonging to the JOIN. +// +%type on_using {OnOrUsing} +//%destructor on_using {sqlite3ClearOnOrUsing(pParse->db, &$$);} +on_using(N) ::= ON expr(E). {N.pOn = E; N.pUsing = 0;} +on_using(N) ::= USING LP idlist(L) RP. {N.pOn = 0; N.pUsing = L;} +on_using(N) ::= . [OR] {N.pOn = 0; N.pUsing = 0;} // Note that this block abuses the Token type just a little. If there is // no "INDEXED BY" clause, the returned token is empty (z==0 && n==0). If @@ -682,15 +872,11 @@ on_opt(N) ::= . {N = 0;} // recognizes and interprets this as a special case. // %type indexed_opt {Token} +%type indexed_by {Token} indexed_opt(A) ::= . {A.z=0; A.n=0;} -indexed_opt(A) ::= INDEXED BY nm(X). {A = X;} -indexed_opt(A) ::= NOT INDEXED. {A.z=0; A.n=1;} - -%type using_opt {IdList*} -%destructor using_opt {sqlite3IdListDelete(pParse->db, $$);} -using_opt(U) ::= USING LP idlist(L) RP. {U = L;} -using_opt(U) ::= . {U = 0;} - +indexed_opt(A) ::= indexed_by(A). +indexed_by(A) ::= INDEXED BY nm(X). {A = X;} +indexed_by(A) ::= NOT INDEXED. {A.z=0; A.n=1;} %type orderby_opt {ExprList*} %destructor orderby_opt {sqlite3ExprListDelete(pParse->db, $$);} @@ -704,13 +890,13 @@ using_opt(U) ::= . {U = 0;} orderby_opt(A) ::= . {A = 0;} orderby_opt(A) ::= ORDER BY sortlist(X). {A = X;} -sortlist(A) ::= sortlist(X) COMMA expr(Y) sortorder(Z). { - A = sqlite3ExprListAppend(pParse,X,Y.pExpr); - sqlite3ExprListSetSortOrder(A,Z); +sortlist(A) ::= sortlist(A) COMMA expr(Y) sortorder(Z) nulls(X). { + A = sqlite3ExprListAppend(pParse,A,Y); + sqlite3ExprListSetSortOrder(A,Z,X); } -sortlist(A) ::= expr(Y) sortorder(Z). { - A = sqlite3ExprListAppend(pParse,0,Y.pExpr); - sqlite3ExprListSetSortOrder(A,Z); +sortlist(A) ::= expr(Y) sortorder(Z) nulls(X). { + A = sqlite3ExprListAppend(pParse,0,Y); /*A-overwrites-Y*/ + sqlite3ExprListSetSortOrder(A,Z,X); } %type sortorder {int} @@ -719,6 +905,11 @@ sortorder(A) ::= ASC. {A = SQLITE_SO_ASC;} sortorder(A) ::= DESC. {A = SQLITE_SO_DESC;} sortorder(A) ::= . {A = SQLITE_SO_UNDEFINED;} +%type nulls {int} +nulls(A) ::= NULLS FIRST. {A = SQLITE_SO_ASC;} +nulls(A) ::= NULLS LAST. {A = SQLITE_SO_DESC;} +nulls(A) ::= . {A = SQLITE_SO_UNDEFINED;} + %type groupby_opt {ExprList*} %destructor groupby_opt {sqlite3ExprListDelete(pParse->db, $$);} groupby_opt(A) ::= . {A = 0;} @@ -727,9 +918,9 @@ groupby_opt(A) ::= GROUP BY nexprlist(X). {A = X;} %type having_opt {Expr*} %destructor having_opt {sqlite3ExprDelete(pParse->db, $$);} having_opt(A) ::= . {A = 0;} -having_opt(A) ::= HAVING expr(X). {A = X.pExpr;} +having_opt(A) ::= HAVING expr(X). {A = X;} -%type limit_opt {struct LimitVal} +%type limit_opt {Expr*} // The destructor for limit_opt will never fire in the current grammar. // The limit_opt non-terminal only occurs at the end of a single production @@ -738,87 +929,152 @@ having_opt(A) ::= HAVING expr(X). {A = X.pExpr;} // reduce. So there is never a limit_opt non-terminal on the stack // except as a transient. So there is never anything to destroy. // -//%destructor limit_opt { -// sqlite3ExprDelete(pParse->db, $$.pLimit); -// sqlite3ExprDelete(pParse->db, $$.pOffset); -//} -limit_opt(A) ::= . {A.pLimit = 0; A.pOffset = 0;} -limit_opt(A) ::= LIMIT expr(X). {A.pLimit = X.pExpr; A.pOffset = 0;} +//%destructor limit_opt {sqlite3ExprDelete(pParse->db, $$);} +limit_opt(A) ::= . {A = 0;} +limit_opt(A) ::= LIMIT expr(X). + {A = sqlite3PExpr(pParse,TK_LIMIT,X,0);} limit_opt(A) ::= LIMIT expr(X) OFFSET expr(Y). - {A.pLimit = X.pExpr; A.pOffset = Y.pExpr;} + {A = sqlite3PExpr(pParse,TK_LIMIT,X,Y);} limit_opt(A) ::= LIMIT expr(X) COMMA expr(Y). - {A.pOffset = X.pExpr; A.pLimit = Y.pExpr;} + {A = sqlite3PExpr(pParse,TK_LIMIT,Y,X);} /////////////////////////// The DELETE statement ///////////////////////////// // -%ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT -cmd ::= with(C) DELETE FROM fullname(X) indexed_opt(I) where_opt(W) +%if SQLITE_ENABLE_UPDATE_DELETE_LIMIT || SQLITE_UDL_CAPABLE_PARSER +cmd ::= with DELETE FROM xfullname(X) indexed_opt(I) where_opt_ret(W) orderby_opt(O) limit_opt(L). { - sqlite3WithPush(pParse, C, 1); sqlite3SrcListIndexedBy(pParse, X, &I); - W = sqlite3LimitWhere(pParse, X, W, O, L.pLimit, L.pOffset, "DELETE"); - sqlite3DeleteFrom(pParse,X,W); +#ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT + if( O || L ){ + updateDeleteLimitError(pParse,O,L); + O = 0; + L = 0; + } +#endif + sqlite3DeleteFrom(pParse,X,W,O,L); } -%endif -%ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT -cmd ::= with(C) DELETE FROM fullname(X) indexed_opt(I) where_opt(W). { - sqlite3WithPush(pParse, C, 1); +%else +cmd ::= with DELETE FROM xfullname(X) indexed_opt(I) where_opt_ret(W). { sqlite3SrcListIndexedBy(pParse, X, &I); - sqlite3DeleteFrom(pParse,X,W); + sqlite3DeleteFrom(pParse,X,W,0,0); } %endif %type where_opt {Expr*} %destructor where_opt {sqlite3ExprDelete(pParse->db, $$);} +%type where_opt_ret {Expr*} +%destructor where_opt_ret {sqlite3ExprDelete(pParse->db, $$);} where_opt(A) ::= . {A = 0;} -where_opt(A) ::= WHERE expr(X). {A = X.pExpr;} +where_opt(A) ::= WHERE expr(X). {A = X;} +where_opt_ret(A) ::= . {A = 0;} +where_opt_ret(A) ::= WHERE expr(X). {A = X;} +where_opt_ret(A) ::= RETURNING selcollist(X). + {sqlite3AddReturning(pParse,X); A = 0;} +where_opt_ret(A) ::= WHERE expr(X) RETURNING selcollist(Y). + {sqlite3AddReturning(pParse,Y); A = X;} ////////////////////////// The UPDATE command //////////////////////////////// // -%ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT -cmd ::= with(C) UPDATE orconf(R) fullname(X) indexed_opt(I) SET setlist(Y) - where_opt(W) orderby_opt(O) limit_opt(L). { - sqlite3WithPush(pParse, C, 1); +%if SQLITE_ENABLE_UPDATE_DELETE_LIMIT || SQLITE_UDL_CAPABLE_PARSER +cmd ::= with UPDATE orconf(R) xfullname(X) indexed_opt(I) SET setlist(Y) from(F) + where_opt_ret(W) orderby_opt(O) limit_opt(L). { sqlite3SrcListIndexedBy(pParse, X, &I); + if( F ){ + SrcList *pFromClause = F; + if( pFromClause->nSrc>1 ){ + Select *pSubquery; + Token as; + pSubquery = sqlite3SelectNew(pParse,0,pFromClause,0,0,0,0,SF_NestedFrom,0); + as.n = 0; + as.z = 0; + pFromClause = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&as,pSubquery,0); + } + X = sqlite3SrcListAppendList(pParse, X, pFromClause); + } sqlite3ExprListCheckLength(pParse,Y,"set list"); - W = sqlite3LimitWhere(pParse, X, W, O, L.pLimit, L.pOffset, "UPDATE"); - sqlite3Update(pParse,X,Y,W,R); +#ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT + if( O || L ){ + updateDeleteLimitError(pParse,O,L); + O = 0; + L = 0; + } +#endif + sqlite3Update(pParse,X,Y,W,R,O,L,0); } -%endif -%ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT -cmd ::= with(C) UPDATE orconf(R) fullname(X) indexed_opt(I) SET setlist(Y) - where_opt(W). { - sqlite3WithPush(pParse, C, 1); +%else +cmd ::= with UPDATE orconf(R) xfullname(X) indexed_opt(I) SET setlist(Y) from(F) + where_opt_ret(W). { sqlite3SrcListIndexedBy(pParse, X, &I); sqlite3ExprListCheckLength(pParse,Y,"set list"); - sqlite3Update(pParse,X,Y,W,R); + if( F ){ + SrcList *pFromClause = F; + if( pFromClause->nSrc>1 ){ + Select *pSubquery; + Token as; + pSubquery = sqlite3SelectNew(pParse,0,pFromClause,0,0,0,0,SF_NestedFrom,0); + as.n = 0; + as.z = 0; + pFromClause = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&as,pSubquery,0); + } + X = sqlite3SrcListAppendList(pParse, X, pFromClause); + } + sqlite3Update(pParse,X,Y,W,R,0,0,0); } %endif + + %type setlist {ExprList*} %destructor setlist {sqlite3ExprListDelete(pParse->db, $$);} -setlist(A) ::= setlist(Z) COMMA nm(X) EQ expr(Y). { - A = sqlite3ExprListAppend(pParse, Z, Y.pExpr); +setlist(A) ::= setlist(A) COMMA nm(X) EQ expr(Y). { + A = sqlite3ExprListAppend(pParse, A, Y); sqlite3ExprListSetName(pParse, A, &X, 1); } +setlist(A) ::= setlist(A) COMMA LP idlist(X) RP EQ expr(Y). { + A = sqlite3ExprListAppendVector(pParse, A, X, Y); +} setlist(A) ::= nm(X) EQ expr(Y). { - A = sqlite3ExprListAppend(pParse, 0, Y.pExpr); + A = sqlite3ExprListAppend(pParse, 0, Y); sqlite3ExprListSetName(pParse, A, &X, 1); } +setlist(A) ::= LP idlist(X) RP EQ expr(Y). { + A = sqlite3ExprListAppendVector(pParse, 0, X, Y); +} ////////////////////////// The INSERT command ///////////////////////////////// // -cmd ::= with(W) insert_cmd(R) INTO fullname(X) idlist_opt(F) select(S). { - sqlite3WithPush(pParse, W, 1); - sqlite3Insert(pParse, X, S, F, R); +cmd ::= with insert_cmd(R) INTO xfullname(X) idlist_opt(F) select(S) + upsert(U). { + sqlite3Insert(pParse, X, S, F, R, U); } -cmd ::= with(W) insert_cmd(R) INTO fullname(X) idlist_opt(F) DEFAULT VALUES. +cmd ::= with insert_cmd(R) INTO xfullname(X) idlist_opt(F) DEFAULT VALUES returning. { - sqlite3WithPush(pParse, W, 1); - sqlite3Insert(pParse, X, 0, F, R); -} + sqlite3Insert(pParse, X, 0, F, R, 0); +} + +%type upsert {Upsert*} + +// Because upsert only occurs at the tip end of the INSERT rule for cmd, +// there is never a case where the value of the upsert pointer will not +// be destroyed by the cmd action. So comment-out the destructor to +// avoid unreachable code. +//%destructor upsert {sqlite3UpsertDelete(pParse->db,$$);} +upsert(A) ::= . { A = 0; } +upsert(A) ::= RETURNING selcollist(X). { A = 0; sqlite3AddReturning(pParse,X); } +upsert(A) ::= ON CONFLICT LP sortlist(T) RP where_opt(TW) + DO UPDATE SET setlist(Z) where_opt(W) upsert(N). + { A = sqlite3UpsertNew(pParse->db,T,TW,Z,W,N);} +upsert(A) ::= ON CONFLICT LP sortlist(T) RP where_opt(TW) DO NOTHING upsert(N). + { A = sqlite3UpsertNew(pParse->db,T,TW,0,0,N); } +upsert(A) ::= ON CONFLICT DO NOTHING returning. + { A = sqlite3UpsertNew(pParse->db,0,0,0,0,0); } +upsert(A) ::= ON CONFLICT DO UPDATE SET setlist(Z) where_opt(W) returning. + { A = sqlite3UpsertNew(pParse->db,0,0,Z,W,0);} + +returning ::= RETURNING selcollist(X). {sqlite3AddReturning(pParse,X);} +returning ::= . %type insert_cmd {int} insert_cmd(A) ::= INSERT orconf(R). {A = R;} @@ -831,195 +1087,269 @@ insert_cmd(A) ::= REPLACE. {A = OE_Replace;} idlist_opt(A) ::= . {A = 0;} idlist_opt(A) ::= LP idlist(X) RP. {A = X;} -idlist(A) ::= idlist(X) COMMA nm(Y). - {A = sqlite3IdListAppend(pParse->db,X,&Y);} +idlist(A) ::= idlist(A) COMMA nm(Y). + {A = sqlite3IdListAppend(pParse,A,&Y);} idlist(A) ::= nm(Y). - {A = sqlite3IdListAppend(pParse->db,0,&Y);} + {A = sqlite3IdListAppend(pParse,0,&Y); /*A-overwrites-Y*/} /////////////////////////// Expression Processing ///////////////////////////// // -%type expr {ExprSpan} -%destructor expr {sqlite3ExprDelete(pParse->db, $$.pExpr);} -%type term {ExprSpan} -%destructor term {sqlite3ExprDelete(pParse->db, $$.pExpr);} +%type expr {Expr*} +%destructor expr {sqlite3ExprDelete(pParse->db, $$);} +%type term {Expr*} +%destructor term {sqlite3ExprDelete(pParse->db, $$);} %include { - /* This is a utility routine used to set the ExprSpan.zStart and - ** ExprSpan.zEnd values of pOut so that the span covers the complete - ** range of text beginning with pStart and going to the end of pEnd. - */ - static void spanSet(ExprSpan *pOut, Token *pStart, Token *pEnd){ - pOut->zStart = pStart->z; - pOut->zEnd = &pEnd->z[pEnd->n]; - } - /* Construct a new Expr object from a single identifier. Use the - ** new Expr to populate pOut. Set the span of pOut to be the identifier - ** that created the expression. - */ - static void spanExpr(ExprSpan *pOut, Parse *pParse, int op, Token *pValue){ - pOut->pExpr = sqlite3PExpr(pParse, op, 0, 0, pValue); - pOut->zStart = pValue->z; - pOut->zEnd = &pValue->z[pValue->n]; + /* Construct a new Expr object from a single token */ + static Expr *tokenExpr(Parse *pParse, int op, Token t){ + Expr *p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)+t.n+1); + if( p ){ + /* memset(p, 0, sizeof(Expr)); */ + p->op = (u8)op; + p->affExpr = 0; + p->flags = EP_Leaf; + ExprClearVVAProperties(p); + /* p->iAgg = -1; // Not required */ + p->pLeft = p->pRight = 0; + p->pAggInfo = 0; + memset(&p->x, 0, sizeof(p->x)); + memset(&p->y, 0, sizeof(p->y)); + p->op2 = 0; + p->iTable = 0; + p->iColumn = 0; + p->u.zToken = (char*)&p[1]; + memcpy(p->u.zToken, t.z, t.n); + p->u.zToken[t.n] = 0; + p->w.iOfst = (int)(t.z - pParse->zTail); + if( sqlite3Isquote(p->u.zToken[0]) ){ + sqlite3DequoteExpr(p); + } +#if SQLITE_MAX_EXPR_DEPTH>0 + p->nHeight = 1; +#endif + if( IN_RENAME_OBJECT ){ + return (Expr*)sqlite3RenameTokenMap(pParse, (void*)p, &t); + } + } + return p; } + } -expr(A) ::= term(X). {A = X;} -expr(A) ::= LP(B) expr(X) RP(E). {A.pExpr = X.pExpr; spanSet(&A,&B,&E);} -term(A) ::= NULL(X). {spanExpr(&A, pParse, @X, &X);} -expr(A) ::= id(X). {spanExpr(&A, pParse, TK_ID, &X);} -expr(A) ::= JOIN_KW(X). {spanExpr(&A, pParse, TK_ID, &X);} +expr(A) ::= term(A). +expr(A) ::= LP expr(X) RP. {A = X;} +expr(A) ::= idj(X). {A=tokenExpr(pParse,TK_ID,X); /*A-overwrites-X*/} expr(A) ::= nm(X) DOT nm(Y). { - Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &X); - Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &Y); - A.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp2, 0); - spanSet(&A,&X,&Y); + Expr *temp1 = tokenExpr(pParse,TK_ID,X); + Expr *temp2 = tokenExpr(pParse,TK_ID,Y); + A = sqlite3PExpr(pParse, TK_DOT, temp1, temp2); } expr(A) ::= nm(X) DOT nm(Y) DOT nm(Z). { - Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &X); - Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &Y); - Expr *temp3 = sqlite3PExpr(pParse, TK_ID, 0, 0, &Z); - Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3, 0); - A.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp4, 0); - spanSet(&A,&X,&Z); -} -term(A) ::= INTEGER|FLOAT|BLOB(X). {spanExpr(&A, pParse, @X, &X);} -term(A) ::= STRING(X). {spanExpr(&A, pParse, @X, &X);} + Expr *temp1 = tokenExpr(pParse,TK_ID,X); + Expr *temp2 = tokenExpr(pParse,TK_ID,Y); + Expr *temp3 = tokenExpr(pParse,TK_ID,Z); + Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3); + if( IN_RENAME_OBJECT ){ + sqlite3RenameTokenRemap(pParse, 0, temp1); + } + A = sqlite3PExpr(pParse, TK_DOT, temp1, temp4); +} +term(A) ::= NULL|FLOAT|BLOB(X). {A=tokenExpr(pParse,@X,X); /*A-overwrites-X*/} +term(A) ::= STRING(X). {A=tokenExpr(pParse,@X,X); /*A-overwrites-X*/} +term(A) ::= INTEGER(X). { + A = sqlite3ExprAlloc(pParse->db, TK_INTEGER, &X, 1); + if( A ) A->w.iOfst = (int)(X.z - pParse->zTail); +} expr(A) ::= VARIABLE(X). { - if( X.n>=2 && X.z[0]=='#' && sqlite3Isdigit(X.z[1]) ){ + if( !(X.z[0]=='#' && sqlite3Isdigit(X.z[1])) ){ + u32 n = X.n; + A = tokenExpr(pParse, TK_VARIABLE, X); + sqlite3ExprAssignVarNumber(pParse, A, n); + }else{ /* When doing a nested parse, one can include terms in an expression ** that look like this: #1 #2 ... These terms refer to registers ** in the virtual machine. #N is the N-th register. */ + Token t = X; /*A-overwrites-X*/ + assert( t.n>=2 ); if( pParse->nested==0 ){ - sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &X); - A.pExpr = 0; + parserSyntaxError(pParse, &t); + A = 0; }else{ - A.pExpr = sqlite3PExpr(pParse, TK_REGISTER, 0, 0, &X); - if( A.pExpr ) sqlite3GetInt32(&X.z[1], &A.pExpr->iTable); + A = sqlite3PExpr(pParse, TK_REGISTER, 0, 0); + if( A ) sqlite3GetInt32(&t.z[1], &A->iTable); } - }else{ - spanExpr(&A, pParse, TK_VARIABLE, &X); - sqlite3ExprAssignVarNumber(pParse, A.pExpr); } - spanSet(&A, &X, &X); } -expr(A) ::= expr(E) COLLATE ids(C). { - A.pExpr = sqlite3ExprAddCollateToken(pParse, E.pExpr, &C, 1); - A.zStart = E.zStart; - A.zEnd = &C.z[C.n]; +expr(A) ::= expr(A) COLLATE ids(C). { + A = sqlite3ExprAddCollateToken(pParse, A, &C, 1); } %ifndef SQLITE_OMIT_CAST -expr(A) ::= CAST(X) LP expr(E) AS typetoken(T) RP(Y). { - A.pExpr = sqlite3PExpr(pParse, TK_CAST, E.pExpr, 0, &T); - spanSet(&A,&X,&Y); +expr(A) ::= CAST LP expr(E) AS typetoken(T) RP. { + A = sqlite3ExprAlloc(pParse->db, TK_CAST, &T, 1); + sqlite3ExprAttachSubtrees(pParse->db, A, E, 0); } %endif SQLITE_OMIT_CAST -expr(A) ::= id(X) LP distinct(D) exprlist(Y) RP(E). { - if( Y && Y->nExpr>pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG] ){ - sqlite3ErrorMsg(pParse, "too many arguments on function %T", &X); - } - A.pExpr = sqlite3ExprFunction(pParse, Y, &X); - spanSet(&A,&X,&E); - if( D==SF_Distinct && A.pExpr ){ - A.pExpr->flags |= EP_Distinct; - } + + +expr(A) ::= idj(X) LP distinct(D) exprlist(Y) RP. { + A = sqlite3ExprFunction(pParse, Y, &X, D); } -expr(A) ::= id(X) LP STAR RP(E). { - A.pExpr = sqlite3ExprFunction(pParse, 0, &X); - spanSet(&A,&X,&E); +expr(A) ::= idj(X) LP distinct(D) exprlist(Y) ORDER BY sortlist(O) RP. { + A = sqlite3ExprFunction(pParse, Y, &X, D); + sqlite3ExprAddFunctionOrderBy(pParse, A, O); } -term(A) ::= CTIME_KW(OP). { - A.pExpr = sqlite3ExprFunction(pParse, 0, &OP); - spanSet(&A, &OP, &OP); +expr(A) ::= idj(X) LP STAR RP. { + A = sqlite3ExprFunction(pParse, 0, &X, 0); } +%ifdef SQLITE_ENABLE_ORDERED_SET_AGGREGATES %include { - /* This routine constructs a binary expression node out of two ExprSpan - ** objects and uses the result to populate a new ExprSpan object. + /* Generate an expression node that represents an ordered-set aggregate function. + ** + ** SQLite does not do anything special to evaluate ordered-set aggregates. The + ** aggregate function itself is expected to do any required ordering on its own. + ** This is just syntactic sugar. + ** + ** This syntax: percentile(f) WITHIN GROUP ( ORDER BY y ) + ** + ** Is equivalent to: percentile(y,f) + ** + ** The purpose of this function is to generate an Expr node from the first syntax + ** into a TK_FUNCTION node that looks like it came from the second syntax. + ** + ** Only functions that have the SQLITE_SELFORDER1 property are allowed to do this + ** transformation. Because DISTINCT is not allowed in the ordered-set aggregate + ** syntax, an error is raised if DISTINCT is used. */ - static void spanBinaryExpr( - ExprSpan *pOut, /* Write the result here */ - Parse *pParse, /* The parsing context. Errors accumulate here */ - int op, /* The binary operation */ - ExprSpan *pLeft, /* The left operand */ - ExprSpan *pRight /* The right operand */ + static Expr *sqlite3ExprAddOrderedsetFunction( + Parse *pParse, /* Parsing context */ + Token *pFuncname, /* Name of the function */ + int isDistinct, /* DISTINCT or ALL qualifier */ + ExprList *pOrig, /* Arguments to the function */ + Expr *pOrderby /* Expression in the ORDER BY clause */ ){ - pOut->pExpr = sqlite3PExpr(pParse, op, pLeft->pExpr, pRight->pExpr, 0); - pOut->zStart = pLeft->zStart; - pOut->zEnd = pRight->zEnd; + ExprList *p; /* Modified argument list */ + Expr *pExpr; /* Final result */ + p = sqlite3ExprListAppend(pParse, 0, pOrderby); + if( pOrig ){ + int i; + for(i=0; inExpr; i++){ + p = sqlite3ExprListAppend(pParse, p, pOrig->a[i].pExpr); + pOrig->a[i].pExpr = 0; + } + sqlite3ExprListDelete(pParse->db, pOrig); + } + pExpr = sqlite3ExprFunction(pParse, p, pFuncname, 0); + if( pParse->nErr==0 ){ + FuncDef *pDef; + u8 enc = ENC(pParse->db); + assert( pExpr!=0 ); /* Because otherwise pParse->nErr would not be zero */ + assert( p!=0 ); /* Because otherwise pParse->nErr would not be zero */ + pDef = sqlite3FindFunction(pParse->db, pExpr->u.zToken, -2, enc, 0); + if( pDef==0 || (pDef->funcFlags & SQLITE_SELFORDER1)==0 ){ + sqlite3ErrorMsg(pParse, "%#T() is not an ordered-set aggregate", pExpr); + }else if( isDistinct==SF_Distinct ){ + sqlite3ErrorMsg(pParse, "DISTINCT not allowed on ordered-set aggregate %T()", + pFuncname); + } + } + return pExpr; } +} +expr(A) ::= idj(X) LP distinct(D) exprlist(Y) RP WITHIN GROUP LP ORDER BY expr(E) RP. { + A = sqlite3ExprAddOrderedsetFunction(pParse, &X, D, Y, E); +} +%endif SQLITE_ENABLE_ORDERED_SET_AGGREGATES - /* If doNot is true, then add a TK_NOT Expr-node wrapper around the - ** outside of *ppExpr. - */ - static void exprNot(Parse *pParse, int doNot, Expr **ppExpr){ - if( doNot ) *ppExpr = sqlite3PExpr(pParse, TK_NOT, *ppExpr, 0, 0); +%ifndef SQLITE_OMIT_WINDOWFUNC +expr(A) ::= idj(X) LP distinct(D) exprlist(Y) RP filter_over(Z). { + A = sqlite3ExprFunction(pParse, Y, &X, D); + sqlite3WindowAttach(pParse, A, Z); +} +expr(A) ::= idj(X) LP distinct(D) exprlist(Y) ORDER BY sortlist(O) RP filter_over(Z). { + A = sqlite3ExprFunction(pParse, Y, &X, D); + sqlite3WindowAttach(pParse, A, Z); + sqlite3ExprAddFunctionOrderBy(pParse, A, O); +} +expr(A) ::= idj(X) LP STAR RP filter_over(Z). { + A = sqlite3ExprFunction(pParse, 0, &X, 0); + sqlite3WindowAttach(pParse, A, Z); +} +%ifdef SQLITE_ENABLE_ORDERED_SET_AGGREGATES +expr(A) ::= idj(X) LP distinct(D) exprlist(Y) RP WITHIN GROUP LP ORDER BY expr(E) RP + filter_over(Z). { + A = sqlite3ExprAddOrderedsetFunction(pParse, &X, D, Y, E); + sqlite3WindowAttach(pParse, A, Z); +} +%endif SQLITE_ENABLE_ORDERED_SET_AGGREGATES + +%endif SQLITE_OMIT_WINDOWFUNC + +term(A) ::= CTIME_KW(OP). { + A = sqlite3ExprFunction(pParse, 0, &OP, 0); +} + +expr(A) ::= LP nexprlist(X) COMMA expr(Y) RP. { + ExprList *pList = sqlite3ExprListAppend(pParse, X, Y); + A = sqlite3PExpr(pParse, TK_VECTOR, 0, 0); + if( A ){ + A->x.pList = pList; + if( ALWAYS(pList->nExpr) ){ + A->flags |= pList->a[0].pExpr->flags & EP_Propagate; + } + }else{ + sqlite3ExprListDelete(pParse->db, pList); } } -expr(A) ::= expr(X) AND(OP) expr(Y). {spanBinaryExpr(&A,pParse,@OP,&X,&Y);} -expr(A) ::= expr(X) OR(OP) expr(Y). {spanBinaryExpr(&A,pParse,@OP,&X,&Y);} -expr(A) ::= expr(X) LT|GT|GE|LE(OP) expr(Y). - {spanBinaryExpr(&A,pParse,@OP,&X,&Y);} -expr(A) ::= expr(X) EQ|NE(OP) expr(Y). {spanBinaryExpr(&A,pParse,@OP,&X,&Y);} -expr(A) ::= expr(X) BITAND|BITOR|LSHIFT|RSHIFT(OP) expr(Y). - {spanBinaryExpr(&A,pParse,@OP,&X,&Y);} -expr(A) ::= expr(X) PLUS|MINUS(OP) expr(Y). - {spanBinaryExpr(&A,pParse,@OP,&X,&Y);} -expr(A) ::= expr(X) STAR|SLASH|REM(OP) expr(Y). - {spanBinaryExpr(&A,pParse,@OP,&X,&Y);} -expr(A) ::= expr(X) CONCAT(OP) expr(Y). {spanBinaryExpr(&A,pParse,@OP,&X,&Y);} -%type likeop {struct LikeOp} -likeop(A) ::= LIKE_KW|MATCH(X). {A.eOperator = X; A.bNot = 0;} -likeop(A) ::= NOT LIKE_KW|MATCH(X). {A.eOperator = X; A.bNot = 1;} -expr(A) ::= expr(X) likeop(OP) expr(Y). [LIKE_KW] { +expr(A) ::= expr(A) AND expr(Y). {A=sqlite3ExprAnd(pParse,A,Y);} +expr(A) ::= expr(A) OR(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);} +expr(A) ::= expr(A) LT|GT|GE|LE(OP) expr(Y). + {A=sqlite3PExpr(pParse,@OP,A,Y);} +expr(A) ::= expr(A) EQ|NE(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);} +expr(A) ::= expr(A) BITAND|BITOR|LSHIFT|RSHIFT(OP) expr(Y). + {A=sqlite3PExpr(pParse,@OP,A,Y);} +expr(A) ::= expr(A) PLUS|MINUS(OP) expr(Y). + {A=sqlite3PExpr(pParse,@OP,A,Y);} +expr(A) ::= expr(A) STAR|SLASH|REM(OP) expr(Y). + {A=sqlite3PExpr(pParse,@OP,A,Y);} +expr(A) ::= expr(A) CONCAT(OP) expr(Y). {A=sqlite3PExpr(pParse,@OP,A,Y);} +%type likeop {Token} +likeop(A) ::= LIKE_KW|MATCH(A). +likeop(A) ::= NOT LIKE_KW|MATCH(X). {A=X; A.n|=0x80000000; /*A-overwrite-X*/} +expr(A) ::= expr(A) likeop(OP) expr(Y). [LIKE_KW] { ExprList *pList; - pList = sqlite3ExprListAppend(pParse,0, Y.pExpr); - pList = sqlite3ExprListAppend(pParse,pList, X.pExpr); - A.pExpr = sqlite3ExprFunction(pParse, pList, &OP.eOperator); - exprNot(pParse, OP.bNot, &A.pExpr); - A.zStart = X.zStart; - A.zEnd = Y.zEnd; - if( A.pExpr ) A.pExpr->flags |= EP_InfixFunc; -} -expr(A) ::= expr(X) likeop(OP) expr(Y) ESCAPE expr(E). [LIKE_KW] { + int bNot = OP.n & 0x80000000; + OP.n &= 0x7fffffff; + pList = sqlite3ExprListAppend(pParse,0, Y); + pList = sqlite3ExprListAppend(pParse,pList, A); + A = sqlite3ExprFunction(pParse, pList, &OP, 0); + if( bNot ) A = sqlite3PExpr(pParse, TK_NOT, A, 0); + if( A ) A->flags |= EP_InfixFunc; +} +expr(A) ::= expr(A) likeop(OP) expr(Y) ESCAPE expr(E). [LIKE_KW] { ExprList *pList; - pList = sqlite3ExprListAppend(pParse,0, Y.pExpr); - pList = sqlite3ExprListAppend(pParse,pList, X.pExpr); - pList = sqlite3ExprListAppend(pParse,pList, E.pExpr); - A.pExpr = sqlite3ExprFunction(pParse, pList, &OP.eOperator); - exprNot(pParse, OP.bNot, &A.pExpr); - A.zStart = X.zStart; - A.zEnd = E.zEnd; - if( A.pExpr ) A.pExpr->flags |= EP_InfixFunc; -} - -%include { - /* Construct an expression node for a unary postfix operator - */ - static void spanUnaryPostfix( - ExprSpan *pOut, /* Write the new expression node here */ - Parse *pParse, /* Parsing context to record errors */ - int op, /* The operator */ - ExprSpan *pOperand, /* The operand */ - Token *pPostOp /* The operand token for setting the span */ - ){ - pOut->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0, 0); - pOut->zStart = pOperand->zStart; - pOut->zEnd = &pPostOp->z[pPostOp->n]; - } + int bNot = OP.n & 0x80000000; + OP.n &= 0x7fffffff; + pList = sqlite3ExprListAppend(pParse,0, Y); + pList = sqlite3ExprListAppend(pParse,pList, A); + pList = sqlite3ExprListAppend(pParse,pList, E); + A = sqlite3ExprFunction(pParse, pList, &OP, 0); + if( bNot ) A = sqlite3PExpr(pParse, TK_NOT, A, 0); + if( A ) A->flags |= EP_InfixFunc; } -expr(A) ::= expr(X) ISNULL|NOTNULL(E). {spanUnaryPostfix(&A,pParse,@E,&X,&E);} -expr(A) ::= expr(X) NOT NULL(E). {spanUnaryPostfix(&A,pParse,TK_NOTNULL,&X,&E);} +expr(A) ::= expr(A) ISNULL|NOTNULL(E). {A = sqlite3PExpr(pParse,@E,A,0);} +expr(A) ::= expr(A) NOT NULL. {A = sqlite3PExpr(pParse,TK_NOTNULL,A,0);} %include { /* A routine to convert a binary TK_IS or TK_ISNOT expression into a ** unary TK_ISNULL or TK_NOTNULL expression. */ static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){ sqlite3 *db = pParse->db; - if( pA && pY && pY->op==TK_NULL ){ + if( pA && pY && pY->op==TK_NULL && !IN_RENAME_OBJECT ){ pA->op = (u8)op; sqlite3ExprDelete(db, pA->pRight); pA->pRight = 0; @@ -1033,195 +1363,170 @@ expr(A) ::= expr(X) NOT NULL(E). {spanUnaryPostfix(&A,pParse,TK_NOTNULL,&X,&E);} // If expr2 is NULL then code as TK_ISNULL or TK_NOTNULL. If expr2 // is any other expression, code as TK_IS or TK_ISNOT. // -expr(A) ::= expr(X) IS expr(Y). { - spanBinaryExpr(&A,pParse,TK_IS,&X,&Y); - binaryToUnaryIfNull(pParse, Y.pExpr, A.pExpr, TK_ISNULL); -} -expr(A) ::= expr(X) IS NOT expr(Y). { - spanBinaryExpr(&A,pParse,TK_ISNOT,&X,&Y); - binaryToUnaryIfNull(pParse, Y.pExpr, A.pExpr, TK_NOTNULL); -} - -%include { - /* Construct an expression node for a unary prefix operator - */ - static void spanUnaryPrefix( - ExprSpan *pOut, /* Write the new expression node here */ - Parse *pParse, /* Parsing context to record errors */ - int op, /* The operator */ - ExprSpan *pOperand, /* The operand */ - Token *pPreOp /* The operand token for setting the span */ - ){ - pOut->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0, 0); - pOut->zStart = pPreOp->z; - pOut->zEnd = pOperand->zEnd; +expr(A) ::= expr(A) IS expr(Y). { + A = sqlite3PExpr(pParse,TK_IS,A,Y); + binaryToUnaryIfNull(pParse, Y, A, TK_ISNULL); +} +expr(A) ::= expr(A) IS NOT expr(Y). { + A = sqlite3PExpr(pParse,TK_ISNOT,A,Y); + binaryToUnaryIfNull(pParse, Y, A, TK_NOTNULL); +} +expr(A) ::= expr(A) IS NOT DISTINCT FROM expr(Y). { + A = sqlite3PExpr(pParse,TK_IS,A,Y); + binaryToUnaryIfNull(pParse, Y, A, TK_ISNULL); +} +expr(A) ::= expr(A) IS DISTINCT FROM expr(Y). { + A = sqlite3PExpr(pParse,TK_ISNOT,A,Y); + binaryToUnaryIfNull(pParse, Y, A, TK_NOTNULL); +} + +expr(A) ::= NOT(B) expr(X). + {A = sqlite3PExpr(pParse, @B, X, 0);/*A-overwrites-B*/} +expr(A) ::= BITNOT(B) expr(X). + {A = sqlite3PExpr(pParse, @B, X, 0);/*A-overwrites-B*/} +expr(A) ::= PLUS|MINUS(B) expr(X). [BITNOT] { + Expr *p = X; + u8 op = @B + (TK_UPLUS-TK_PLUS); + assert( TK_UPLUS>TK_PLUS ); + assert( TK_UMINUS == TK_MINUS + (TK_UPLUS - TK_PLUS) ); + if( p && p->op==TK_UPLUS ){ + p->op = op; + A = p; + }else{ + A = sqlite3PExpr(pParse, op, p, 0); + /*A-overwrites-B*/ } } - - -expr(A) ::= NOT(B) expr(X). {spanUnaryPrefix(&A,pParse,@B,&X,&B);} -expr(A) ::= BITNOT(B) expr(X). {spanUnaryPrefix(&A,pParse,@B,&X,&B);} -expr(A) ::= MINUS(B) expr(X). [BITNOT] - {spanUnaryPrefix(&A,pParse,TK_UMINUS,&X,&B);} -expr(A) ::= PLUS(B) expr(X). [BITNOT] - {spanUnaryPrefix(&A,pParse,TK_UPLUS,&X,&B);} +expr(A) ::= expr(B) PTR(C) expr(D). { + ExprList *pList = sqlite3ExprListAppend(pParse, 0, B); + pList = sqlite3ExprListAppend(pParse, pList, D); + A = sqlite3ExprFunction(pParse, pList, &C, 0); +} %type between_op {int} between_op(A) ::= BETWEEN. {A = 0;} between_op(A) ::= NOT BETWEEN. {A = 1;} -expr(A) ::= expr(W) between_op(N) expr(X) AND expr(Y). [BETWEEN] { - ExprList *pList = sqlite3ExprListAppend(pParse,0, X.pExpr); - pList = sqlite3ExprListAppend(pParse,pList, Y.pExpr); - A.pExpr = sqlite3PExpr(pParse, TK_BETWEEN, W.pExpr, 0, 0); - if( A.pExpr ){ - A.pExpr->x.pList = pList; +expr(A) ::= expr(A) between_op(N) expr(X) AND expr(Y). [BETWEEN] { + ExprList *pList = sqlite3ExprListAppend(pParse,0, X); + pList = sqlite3ExprListAppend(pParse,pList, Y); + A = sqlite3PExpr(pParse, TK_BETWEEN, A, 0); + if( A ){ + A->x.pList = pList; }else{ sqlite3ExprListDelete(pParse->db, pList); } - exprNot(pParse, N, &A.pExpr); - A.zStart = W.zStart; - A.zEnd = Y.zEnd; + if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0); } %ifndef SQLITE_OMIT_SUBQUERY %type in_op {int} in_op(A) ::= IN. {A = 0;} in_op(A) ::= NOT IN. {A = 1;} - expr(A) ::= expr(X) in_op(N) LP exprlist(Y) RP(E). [IN] { + expr(A) ::= expr(A) in_op(N) LP exprlist(Y) RP. [IN] { if( Y==0 ){ /* Expressions of the form ** ** expr1 IN () ** expr1 NOT IN () ** - ** simplify to constants 0 (false) and 1 (true), respectively, - ** regardless of the value of expr1. - */ - A.pExpr = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &sqlite3IntTokens[N]); - sqlite3ExprDelete(pParse->db, X.pExpr); - }else if( Y->nExpr==1 ){ - /* Expressions of the form: - ** - ** expr1 IN (?1) - ** expr1 NOT IN (?2) - ** - ** with exactly one value on the RHS can be simplified to something - ** like this: + ** simplify to constants 0 (false) and 1 (true), respectively. ** - ** expr1 == ?1 - ** expr1 <> ?2 - ** - ** But, the RHS of the == or <> is marked with the EP_Generic flag - ** so that it may not contribute to the computation of comparison - ** affinity or the collating sequence to use for comparison. Otherwise, - ** the semantics would be subtly different from IN or NOT IN. + ** Except, do not apply this optimization if expr1 contains a function + ** because that function might be an aggregate (we don't know yet whether + ** it is or not) and if it is an aggregate, that could change the meaning + ** of the whole query. */ - Expr *pRHS = Y->a[0].pExpr; - Y->a[0].pExpr = 0; - sqlite3ExprListDelete(pParse->db, Y); - /* pRHS cannot be NULL because a malloc error would have been detected - ** before now and control would have never reached this point */ - if( ALWAYS(pRHS) ){ - pRHS->flags &= ~EP_Collate; - pRHS->flags |= EP_Generic; + Expr *pB = sqlite3Expr(pParse->db, TK_STRING, N ? "true" : "false"); + if( pB ) sqlite3ExprIdToTrueFalse(pB); + if( !ExprHasProperty(A, EP_HasFunc) ){ + sqlite3ExprUnmapAndDelete(pParse, A); + A = pB; + }else{ + A = sqlite3PExpr(pParse, N ? TK_OR : TK_AND, pB, A); } - A.pExpr = sqlite3PExpr(pParse, N ? TK_NE : TK_EQ, X.pExpr, pRHS, 0); }else{ - A.pExpr = sqlite3PExpr(pParse, TK_IN, X.pExpr, 0, 0); - if( A.pExpr ){ - A.pExpr->x.pList = Y; - sqlite3ExprSetHeightAndFlags(pParse, A.pExpr); - }else{ + Expr *pRHS = Y->a[0].pExpr; + if( Y->nExpr==1 && sqlite3ExprIsConstant(pParse,pRHS) && A->op!=TK_VECTOR ){ + Y->a[0].pExpr = 0; + sqlite3ExprListDelete(pParse->db, Y); + pRHS = sqlite3PExpr(pParse, TK_UPLUS, pRHS, 0); + A = sqlite3PExpr(pParse, TK_EQ, A, pRHS); + }else if( Y->nExpr==1 && pRHS->op==TK_SELECT ){ + A = sqlite3PExpr(pParse, TK_IN, A, 0); + sqlite3PExprAddSelect(pParse, A, pRHS->x.pSelect); + pRHS->x.pSelect = 0; sqlite3ExprListDelete(pParse->db, Y); + }else{ + A = sqlite3PExpr(pParse, TK_IN, A, 0); + if( A==0 ){ + sqlite3ExprListDelete(pParse->db, Y); + }else if( A->pLeft->op==TK_VECTOR ){ + int nExpr = A->pLeft->x.pList->nExpr; + Select *pSelectRHS = sqlite3ExprListToValues(pParse, nExpr, Y); + if( pSelectRHS ){ + parserDoubleLinkSelect(pParse, pSelectRHS); + sqlite3PExprAddSelect(pParse, A, pSelectRHS); + } + }else{ + A->x.pList = Y; + sqlite3ExprSetHeightAndFlags(pParse, A); + } } - exprNot(pParse, N, &A.pExpr); + if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0); } - A.zStart = X.zStart; - A.zEnd = &E.z[E.n]; } - expr(A) ::= LP(B) select(X) RP(E). { - A.pExpr = sqlite3PExpr(pParse, TK_SELECT, 0, 0, 0); - if( A.pExpr ){ - A.pExpr->x.pSelect = X; - ExprSetProperty(A.pExpr, EP_xIsSelect|EP_Subquery); - sqlite3ExprSetHeightAndFlags(pParse, A.pExpr); - }else{ - sqlite3SelectDelete(pParse->db, X); - } - A.zStart = B.z; - A.zEnd = &E.z[E.n]; + expr(A) ::= LP select(X) RP. { + A = sqlite3PExpr(pParse, TK_SELECT, 0, 0); + sqlite3PExprAddSelect(pParse, A, X); } - expr(A) ::= expr(X) in_op(N) LP select(Y) RP(E). [IN] { - A.pExpr = sqlite3PExpr(pParse, TK_IN, X.pExpr, 0, 0); - if( A.pExpr ){ - A.pExpr->x.pSelect = Y; - ExprSetProperty(A.pExpr, EP_xIsSelect|EP_Subquery); - sqlite3ExprSetHeightAndFlags(pParse, A.pExpr); - }else{ - sqlite3SelectDelete(pParse->db, Y); - } - exprNot(pParse, N, &A.pExpr); - A.zStart = X.zStart; - A.zEnd = &E.z[E.n]; + expr(A) ::= expr(A) in_op(N) LP select(Y) RP. [IN] { + A = sqlite3PExpr(pParse, TK_IN, A, 0); + sqlite3PExprAddSelect(pParse, A, Y); + if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0); } - expr(A) ::= expr(X) in_op(N) nm(Y) dbnm(Z). [IN] { - SrcList *pSrc = sqlite3SrcListAppend(pParse->db, 0,&Y,&Z); - A.pExpr = sqlite3PExpr(pParse, TK_IN, X.pExpr, 0, 0); - if( A.pExpr ){ - A.pExpr->x.pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0,0); - ExprSetProperty(A.pExpr, EP_xIsSelect|EP_Subquery); - sqlite3ExprSetHeightAndFlags(pParse, A.pExpr); - }else{ - sqlite3SrcListDelete(pParse->db, pSrc); - } - exprNot(pParse, N, &A.pExpr); - A.zStart = X.zStart; - A.zEnd = Z.z ? &Z.z[Z.n] : &Y.z[Y.n]; + expr(A) ::= expr(A) in_op(N) nm(Y) dbnm(Z) paren_exprlist(E). [IN] { + SrcList *pSrc = sqlite3SrcListAppend(pParse, 0,&Y,&Z); + Select *pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0); + if( E ) sqlite3SrcListFuncArgs(pParse, pSelect ? pSrc : 0, E); + A = sqlite3PExpr(pParse, TK_IN, A, 0); + sqlite3PExprAddSelect(pParse, A, pSelect); + if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0); } - expr(A) ::= EXISTS(B) LP select(Y) RP(E). { - Expr *p = A.pExpr = sqlite3PExpr(pParse, TK_EXISTS, 0, 0, 0); - if( p ){ - p->x.pSelect = Y; - ExprSetProperty(p, EP_xIsSelect|EP_Subquery); - sqlite3ExprSetHeightAndFlags(pParse, p); - }else{ - sqlite3SelectDelete(pParse->db, Y); - } - A.zStart = B.z; - A.zEnd = &E.z[E.n]; + expr(A) ::= EXISTS LP select(Y) RP. { + Expr *p; + p = A = sqlite3PExpr(pParse, TK_EXISTS, 0, 0); + sqlite3PExprAddSelect(pParse, p, Y); } %endif SQLITE_OMIT_SUBQUERY /* CASE expressions */ -expr(A) ::= CASE(C) case_operand(X) case_exprlist(Y) case_else(Z) END(E). { - A.pExpr = sqlite3PExpr(pParse, TK_CASE, X, 0, 0); - if( A.pExpr ){ - A.pExpr->x.pList = Z ? sqlite3ExprListAppend(pParse,Y,Z) : Y; - sqlite3ExprSetHeightAndFlags(pParse, A.pExpr); +expr(A) ::= CASE case_operand(X) case_exprlist(Y) case_else(Z) END. { + A = sqlite3PExpr(pParse, TK_CASE, X, 0); + if( A ){ + A->x.pList = Z ? sqlite3ExprListAppend(pParse,Y,Z) : Y; + sqlite3ExprSetHeightAndFlags(pParse, A); }else{ sqlite3ExprListDelete(pParse->db, Y); sqlite3ExprDelete(pParse->db, Z); } - A.zStart = C.z; - A.zEnd = &E.z[E.n]; } %type case_exprlist {ExprList*} %destructor case_exprlist {sqlite3ExprListDelete(pParse->db, $$);} -case_exprlist(A) ::= case_exprlist(X) WHEN expr(Y) THEN expr(Z). { - A = sqlite3ExprListAppend(pParse,X, Y.pExpr); - A = sqlite3ExprListAppend(pParse,A, Z.pExpr); +case_exprlist(A) ::= case_exprlist(A) WHEN expr(Y) THEN expr(Z). { + A = sqlite3ExprListAppend(pParse,A, Y); + A = sqlite3ExprListAppend(pParse,A, Z); } case_exprlist(A) ::= WHEN expr(Y) THEN expr(Z). { - A = sqlite3ExprListAppend(pParse,0, Y.pExpr); - A = sqlite3ExprListAppend(pParse,A, Z.pExpr); + A = sqlite3ExprListAppend(pParse,0, Y); + A = sqlite3ExprListAppend(pParse,A, Z); } %type case_else {Expr*} %destructor case_else {sqlite3ExprDelete(pParse->db, $$);} -case_else(A) ::= ELSE expr(X). {A = X.pExpr;} +case_else(A) ::= ELSE expr(X). {A = X;} case_else(A) ::= . {A = 0;} %type case_operand {Expr*} %destructor case_operand {sqlite3ExprDelete(pParse->db, $$);} -case_operand(A) ::= expr(X). {A = X.pExpr;} +case_operand(A) ::= expr(A). case_operand(A) ::= . {A = 0;} %type exprlist {ExprList*} @@ -1229,12 +1534,21 @@ case_operand(A) ::= . {A = 0;} %type nexprlist {ExprList*} %destructor nexprlist {sqlite3ExprListDelete(pParse->db, $$);} -exprlist(A) ::= nexprlist(X). {A = X;} +exprlist(A) ::= nexprlist(A). exprlist(A) ::= . {A = 0;} -nexprlist(A) ::= nexprlist(X) COMMA expr(Y). - {A = sqlite3ExprListAppend(pParse,X,Y.pExpr);} +nexprlist(A) ::= nexprlist(A) COMMA expr(Y). + {A = sqlite3ExprListAppend(pParse,A,Y);} nexprlist(A) ::= expr(Y). - {A = sqlite3ExprListAppend(pParse,0,Y.pExpr);} + {A = sqlite3ExprListAppend(pParse,0,Y); /*A-overwrites-Y*/} + +%ifndef SQLITE_OMIT_SUBQUERY +/* A paren_exprlist is an optional expression list contained inside +** of parenthesis */ +%type paren_exprlist {ExprList*} +%destructor paren_exprlist {sqlite3ExprListDelete(pParse->db, $$);} +paren_exprlist(A) ::= . {A = 0;} +paren_exprlist(A) ::= LP exprlist(X) RP. {A = X;} +%endif SQLITE_OMIT_SUBQUERY ///////////////////////////// The CREATE INDEX command /////////////////////// @@ -1242,8 +1556,11 @@ nexprlist(A) ::= expr(Y). cmd ::= createkw(S) uniqueflag(U) INDEX ifnotexists(NE) nm(X) dbnm(D) ON nm(Y) LP sortlist(Z) RP where_opt(W). { sqlite3CreateIndex(pParse, &X, &D, - sqlite3SrcListAppend(pParse->db,0,&Y,0), Z, U, - &S, W, SQLITE_SO_ASC, NE); + sqlite3SrcListAppend(pParse,0,&Y,0), Z, U, + &S, W, SQLITE_SO_ASC, NE, SQLITE_IDXTYPE_APPDEF); + if( IN_RENAME_OBJECT && pParse->pNewIndex ){ + sqlite3RenameTokenMap(pParse, pParse->pNewIndex->zName, &Y); + } } %type uniqueflag {int} @@ -1261,7 +1578,7 @@ uniqueflag(A) ::= . {A = OE_None;} // // IMPORTANT COMPATIBILITY NOTE: Some prior versions of SQLite accepted // COLLATE clauses and ASC or DESC keywords on ID lists in inappropriate -// places - places that might have been stored in the sqlite_master schema. +// places - places that might have been stored in the sqlite_schema table. // Those extra features were ignored. But because they might be in some // (busted) old databases, we need to continue parsing them when loading // historical schemas. @@ -1298,11 +1615,11 @@ uniqueflag(A) ::= . {A = OE_None;} eidlist_opt(A) ::= . {A = 0;} eidlist_opt(A) ::= LP eidlist(X) RP. {A = X;} -eidlist(A) ::= eidlist(X) COMMA nm(Y) collate(C) sortorder(Z). { - A = parserAddExprIdListTerm(pParse, X, &Y, C, Z); +eidlist(A) ::= eidlist(A) COMMA nm(Y) collate(C) sortorder(Z). { + A = parserAddExprIdListTerm(pParse, A, &Y, C, Z); } eidlist(A) ::= nm(Y) collate(C) sortorder(Z). { - A = parserAddExprIdListTerm(pParse, 0, &Y, C, Z); + A = parserAddExprIdListTerm(pParse, 0, &Y, C, Z); /*A-overwrites-Y*/ } %type collate {int} @@ -1316,12 +1633,14 @@ cmd ::= DROP INDEX ifexists(E) fullname(X). {sqlite3DropIndex(pParse, X, E);} ///////////////////////////// The VACUUM command ///////////////////////////// // -%ifndef SQLITE_OMIT_VACUUM -%ifndef SQLITE_OMIT_ATTACH -cmd ::= VACUUM. {sqlite3Vacuum(pParse);} -cmd ::= VACUUM nm. {sqlite3Vacuum(pParse);} -%endif SQLITE_OMIT_ATTACH -%endif SQLITE_OMIT_VACUUM +%if !SQLITE_OMIT_VACUUM && !SQLITE_OMIT_ATTACH +%type vinto {Expr*} +%destructor vinto {sqlite3ExprDelete(pParse->db, $$);} +cmd ::= VACUUM vinto(Y). {sqlite3Vacuum(pParse,0,Y);} +cmd ::= VACUUM nm(X) vinto(Y). {sqlite3Vacuum(pParse,&X,Y);} +vinto(A) ::= INTO expr(X). {A = X;} +vinto(A) ::= . {A = 0;} +%endif ///////////////////////////// The PRAGMA command ///////////////////////////// // @@ -1334,15 +1653,15 @@ cmd ::= PRAGMA nm(X) dbnm(Z) EQ minus_num(Y). cmd ::= PRAGMA nm(X) dbnm(Z) LP minus_num(Y) RP. {sqlite3Pragma(pParse,&X,&Z,&Y,1);} -nmnum(A) ::= plus_num(X). {A = X;} -nmnum(A) ::= nm(X). {A = X;} -nmnum(A) ::= ON(X). {A = X;} -nmnum(A) ::= DELETE(X). {A = X;} -nmnum(A) ::= DEFAULT(X). {A = X;} +nmnum(A) ::= plus_num(A). +nmnum(A) ::= nm(A). +nmnum(A) ::= ON(A). +nmnum(A) ::= DELETE(A). +nmnum(A) ::= DEFAULT(A). %endif SQLITE_OMIT_PRAGMA %token_class number INTEGER|FLOAT. plus_num(A) ::= PLUS number(X). {A = X;} -plus_num(A) ::= number(X). {A = X;} +plus_num(A) ::= number(A). minus_num(A) ::= MINUS number(X). {A = X;} //////////////////////////// The CREATE TRIGGER command ///////////////////// @@ -1359,20 +1678,23 @@ trigger_decl(A) ::= temp(T) TRIGGER ifnotexists(NOERR) nm(B) dbnm(Z) trigger_time(C) trigger_event(D) ON fullname(E) foreach_clause when_clause(G). { sqlite3BeginTrigger(pParse, &B, &Z, C, D.a, D.b, E, G, T, NOERR); - A = (Z.n==0?B:Z); + A = (Z.n==0?B:Z); /*A-overwrites-T*/ +#ifdef SQLITE_DEBUG + assert( pParse->isCreate ); /* Set by createkw reduce action */ + pParse->isCreate = 0; /* But, should not be set for CREATE TRIGGER */ +#endif } %type trigger_time {int} -trigger_time(A) ::= BEFORE. { A = TK_BEFORE; } -trigger_time(A) ::= AFTER. { A = TK_AFTER; } +trigger_time(A) ::= BEFORE|AFTER(X). { A = @X; /*A-overwrites-X*/ } trigger_time(A) ::= INSTEAD OF. { A = TK_INSTEAD;} trigger_time(A) ::= . { A = TK_BEFORE; } %type trigger_event {struct TrigEvent} %destructor trigger_event {sqlite3IdListDelete(pParse->db, $$.b);} -trigger_event(A) ::= DELETE|INSERT(OP). {A.a = @OP; A.b = 0;} -trigger_event(A) ::= UPDATE(OP). {A.a = @OP; A.b = 0;} -trigger_event(A) ::= UPDATE OF idlist(X). {A.a = TK_UPDATE; A.b = X;} +trigger_event(A) ::= DELETE|INSERT(X). {A.a = @X; /*A-overwrites-X*/ A.b = 0;} +trigger_event(A) ::= UPDATE(X). {A.a = @X; /*A-overwrites-X*/ A.b = 0;} +trigger_event(A) ::= UPDATE OF idlist(X).{A.a = TK_UPDATE; A.b = X;} foreach_clause ::= . foreach_clause ::= FOR EACH ROW. @@ -1380,20 +1702,18 @@ foreach_clause ::= FOR EACH ROW. %type when_clause {Expr*} %destructor when_clause {sqlite3ExprDelete(pParse->db, $$);} when_clause(A) ::= . { A = 0; } -when_clause(A) ::= WHEN expr(X). { A = X.pExpr; } +when_clause(A) ::= WHEN expr(X). { A = X; } %type trigger_cmd_list {TriggerStep*} %destructor trigger_cmd_list {sqlite3DeleteTriggerStep(pParse->db, $$);} -trigger_cmd_list(A) ::= trigger_cmd_list(Y) trigger_cmd(X) SEMI. { - assert( Y!=0 ); - Y->pLast->pNext = X; - Y->pLast = X; - A = Y; +trigger_cmd_list(A) ::= trigger_cmd_list(A) trigger_cmd(X) SEMI. { + assert( A!=0 ); + A->pLast->pNext = X; + A->pLast = X; } -trigger_cmd_list(A) ::= trigger_cmd(X) SEMI. { - assert( X!=0 ); - X->pLast = X; - A = X; +trigger_cmd_list(A) ::= trigger_cmd(A) SEMI. { + assert( A!=0 ); + A->pLast = A; } // Disallow qualified table names on INSERT, UPDATE, and DELETE statements @@ -1401,7 +1721,7 @@ trigger_cmd_list(A) ::= trigger_cmd(X) SEMI. { // the same database as the table that the trigger fires on. // %type trnm {Token} -trnm(A) ::= nm(X). {A = X;} +trnm(A) ::= nm(A). trnm(A) ::= nm DOT nm(X). { A = X; sqlite3ErrorMsg(pParse, @@ -1431,36 +1751,34 @@ tridxby ::= NOT INDEXED. { %destructor trigger_cmd {sqlite3DeleteTriggerStep(pParse->db, $$);} // UPDATE trigger_cmd(A) ::= - UPDATE orconf(R) trnm(X) tridxby SET setlist(Y) where_opt(Z). - { A = sqlite3TriggerUpdateStep(pParse->db, &X, Y, Z, R); } + UPDATE(B) orconf(R) trnm(X) tridxby SET setlist(Y) from(F) where_opt(Z) scanpt(E). + {A = sqlite3TriggerUpdateStep(pParse, &X, F, Y, Z, R, B.z, E);} // INSERT -trigger_cmd(A) ::= insert_cmd(R) INTO trnm(X) idlist_opt(F) select(S). - {A = sqlite3TriggerInsertStep(pParse->db, &X, F, S, R);} - +trigger_cmd(A) ::= scanpt(B) insert_cmd(R) INTO + trnm(X) idlist_opt(F) select(S) upsert(U) scanpt(Z). { + A = sqlite3TriggerInsertStep(pParse,&X,F,S,R,U,B,Z);/*A-overwrites-R*/ +} // DELETE -trigger_cmd(A) ::= DELETE FROM trnm(X) tridxby where_opt(Y). - {A = sqlite3TriggerDeleteStep(pParse->db, &X, Y);} +trigger_cmd(A) ::= DELETE(B) FROM trnm(X) tridxby where_opt(Y) scanpt(E). + {A = sqlite3TriggerDeleteStep(pParse, &X, Y, B.z, E);} // SELECT -trigger_cmd(A) ::= select(X). {A = sqlite3TriggerSelectStep(pParse->db, X); } +trigger_cmd(A) ::= scanpt(B) select(X) scanpt(E). + {A = sqlite3TriggerSelectStep(pParse->db, X, B, E); /*A-overwrites-X*/} // The special RAISE expression that may occur in trigger programs -expr(A) ::= RAISE(X) LP IGNORE RP(Y). { - A.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0, 0); - if( A.pExpr ){ - A.pExpr->affinity = OE_Ignore; +expr(A) ::= RAISE LP IGNORE RP. { + A = sqlite3PExpr(pParse, TK_RAISE, 0, 0); + if( A ){ + A->affExpr = OE_Ignore; } - A.zStart = X.z; - A.zEnd = &Y.z[Y.n]; } -expr(A) ::= RAISE(X) LP raisetype(T) COMMA nm(Z) RP(Y). { - A.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0, &Z); - if( A.pExpr ) { - A.pExpr->affinity = (char)T; +expr(A) ::= RAISE LP raisetype(T) COMMA expr(Z) RP. { + A = sqlite3PExpr(pParse, TK_RAISE, Z, 0); + if( A ) { + A->affExpr = (char)T; } - A.zStart = X.z; - A.zEnd = &Y.z[Y.n]; } %endif !SQLITE_OMIT_TRIGGER @@ -1480,16 +1798,16 @@ cmd ::= DROP TRIGGER ifexists(NOERR) fullname(X). { //////////////////////// ATTACH DATABASE file AS name ///////////////////////// %ifndef SQLITE_OMIT_ATTACH cmd ::= ATTACH database_kw_opt expr(F) AS expr(D) key_opt(K). { - sqlite3Attach(pParse, F.pExpr, D.pExpr, K); + sqlite3Attach(pParse, F, D, K); } cmd ::= DETACH database_kw_opt expr(D). { - sqlite3Detach(pParse, D.pExpr); + sqlite3Detach(pParse, D); } %type key_opt {Expr*} %destructor key_opt {sqlite3ExprDelete(pParse->db, $$);} key_opt(A) ::= . { A = 0; } -key_opt(A) ::= KEY expr(X). { A = X.pExpr; } +key_opt(A) ::= KEY expr(X). { A = X; } database_kw_opt ::= DATABASE. database_kw_opt ::= . @@ -1508,20 +1826,33 @@ cmd ::= ANALYZE nm(X) dbnm(Y). {sqlite3Analyze(pParse, &X, &Y);} %endif //////////////////////// ALTER TABLE table ... //////////////////////////////// -%ifndef SQLITE_OMIT_ALTERTABLE +%ifndef SQLITE_OMIT_ALTERTABLE +%ifndef SQLITE_OMIT_VIRTUALTABLE cmd ::= ALTER TABLE fullname(X) RENAME TO nm(Z). { sqlite3AlterRenameTable(pParse,X,&Z); } -cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt column(Y). { +cmd ::= ALTER TABLE add_column_fullname + ADD kwcolumn_opt columnname(Y) carglist. { + Y.n = (int)(pParse->sLastToken.z-Y.z) + pParse->sLastToken.n; sqlite3AlterFinishAddColumn(pParse, &Y); } +cmd ::= ALTER TABLE fullname(X) DROP kwcolumn_opt nm(Y). { + sqlite3AlterDropColumn(pParse, X, &Y); +} + add_column_fullname ::= fullname(X). { disableLookaside(pParse); sqlite3AlterBeginAddColumn(pParse, X); } +cmd ::= ALTER TABLE fullname(X) RENAME kwcolumn_opt nm(Y) TO nm(Z). { + sqlite3AlterRenameColumn(pParse, X, &Y, &Z); +} + kwcolumn_opt ::= . kwcolumn_opt ::= COLUMNKW. -%endif SQLITE_OMIT_ALTERTABLE + +%endif SQLITE_OMIT_VIRTUALTABLE +%endif SQLITE_OMIT_ALTERTABLE //////////////////////// CREATE VIRTUAL TABLE ... ///////////////////////////// %ifndef SQLITE_OMIT_VIRTUALTABLE @@ -1545,20 +1876,217 @@ anylist ::= anylist ANY. //////////////////////// COMMON TABLE EXPRESSIONS //////////////////////////// -%type with {With*} %type wqlist {With*} -%destructor with {sqlite3WithDelete(pParse->db, $$);} %destructor wqlist {sqlite3WithDelete(pParse->db, $$);} +%type wqitem {Cte*} +// %destructor wqitem {sqlite3CteDelete(pParse->db, $$);} // not reachable -with(A) ::= . {A = 0;} +with ::= . %ifndef SQLITE_OMIT_CTE -with(A) ::= WITH wqlist(W). { A = W; } -with(A) ::= WITH RECURSIVE wqlist(W). { A = W; } +with ::= WITH wqlist(W). { sqlite3WithPush(pParse, W, 1); } +with ::= WITH RECURSIVE wqlist(W). { sqlite3WithPush(pParse, W, 1); } -wqlist(A) ::= nm(X) eidlist_opt(Y) AS LP select(Z) RP. { - A = sqlite3WithAdd(pParse, 0, &X, Y, Z); +%type wqas {u8} +wqas(A) ::= AS. {A = M10d_Any;} +wqas(A) ::= AS MATERIALIZED. {A = M10d_Yes;} +wqas(A) ::= AS NOT MATERIALIZED. {A = M10d_No;} +wqitem(A) ::= withnm(X) eidlist_opt(Y) wqas(M) LP select(Z) RP. { + A = sqlite3CteNew(pParse, &X, Y, Z, M); /*A-overwrites-X*/ +} +withnm(A) ::= nm(A). {pParse->bHasWith = 1;} +wqlist(A) ::= wqitem(X). { + A = sqlite3WithAdd(pParse, 0, X); /*A-overwrites-X*/ } -wqlist(A) ::= wqlist(W) COMMA nm(X) eidlist_opt(Y) AS LP select(Z) RP. { - A = sqlite3WithAdd(pParse, W, &X, Y, Z); +wqlist(A) ::= wqlist(A) COMMA wqitem(X). { + A = sqlite3WithAdd(pParse, A, X); } %endif SQLITE_OMIT_CTE + +//////////////////////// WINDOW FUNCTION EXPRESSIONS ///////////////////////// +// These must be at the end of this file. Specifically, the rules that +// introduce tokens WINDOW, OVER and FILTER must appear last. This causes +// the integer values assigned to these tokens to be larger than all other +// tokens that may be output by the tokenizer except TK_SPACE, TK_COMMENT, +// and TK_ILLEGAL. +// +%ifndef SQLITE_OMIT_WINDOWFUNC +%type windowdefn_list {Window*} +%destructor windowdefn_list {sqlite3WindowListDelete(pParse->db, $$);} +windowdefn_list(A) ::= windowdefn(A). +windowdefn_list(A) ::= windowdefn_list(Y) COMMA windowdefn(Z). { + assert( Z!=0 ); + sqlite3WindowChain(pParse, Z, Y); + Z->pNextWin = Y; + A = Z; +} + +%type windowdefn {Window*} +%destructor windowdefn {sqlite3WindowDelete(pParse->db, $$);} +windowdefn(A) ::= nm(X) AS LP window(Y) RP. { + if( ALWAYS(Y) ){ + Y->zName = sqlite3DbStrNDup(pParse->db, X.z, X.n); + } + A = Y; +} + +%type window {Window*} +%destructor window {sqlite3WindowDelete(pParse->db, $$);} + +%type frame_opt {Window*} +%destructor frame_opt {sqlite3WindowDelete(pParse->db, $$);} + +%type part_opt {ExprList*} +%destructor part_opt {sqlite3ExprListDelete(pParse->db, $$);} + +%type filter_clause {Expr*} +%destructor filter_clause {sqlite3ExprDelete(pParse->db, $$);} + +%type over_clause {Window*} +%destructor over_clause {sqlite3WindowDelete(pParse->db, $$);} + +%type filter_over {Window*} +%destructor filter_over {sqlite3WindowDelete(pParse->db, $$);} + +%type range_or_rows {int} + +%type frame_bound {struct FrameBound} +%destructor frame_bound {sqlite3ExprDelete(pParse->db, $$.pExpr);} +%type frame_bound_s {struct FrameBound} +%destructor frame_bound_s {sqlite3ExprDelete(pParse->db, $$.pExpr);} +%type frame_bound_e {struct FrameBound} +%destructor frame_bound_e {sqlite3ExprDelete(pParse->db, $$.pExpr);} + +window(A) ::= PARTITION BY nexprlist(X) orderby_opt(Y) frame_opt(Z). { + A = sqlite3WindowAssemble(pParse, Z, X, Y, 0); +} +window(A) ::= nm(W) PARTITION BY nexprlist(X) orderby_opt(Y) frame_opt(Z). { + A = sqlite3WindowAssemble(pParse, Z, X, Y, &W); +} +window(A) ::= ORDER BY sortlist(Y) frame_opt(Z). { + A = sqlite3WindowAssemble(pParse, Z, 0, Y, 0); +} +window(A) ::= nm(W) ORDER BY sortlist(Y) frame_opt(Z). { + A = sqlite3WindowAssemble(pParse, Z, 0, Y, &W); +} +window(A) ::= frame_opt(A). +window(A) ::= nm(W) frame_opt(Z). { + A = sqlite3WindowAssemble(pParse, Z, 0, 0, &W); +} + +frame_opt(A) ::= . { + A = sqlite3WindowAlloc(pParse, 0, TK_UNBOUNDED, 0, TK_CURRENT, 0, 0); +} +frame_opt(A) ::= range_or_rows(X) frame_bound_s(Y) frame_exclude_opt(Z). { + A = sqlite3WindowAlloc(pParse, X, Y.eType, Y.pExpr, TK_CURRENT, 0, Z); +} +frame_opt(A) ::= range_or_rows(X) BETWEEN frame_bound_s(Y) AND + frame_bound_e(Z) frame_exclude_opt(W). { + A = sqlite3WindowAlloc(pParse, X, Y.eType, Y.pExpr, Z.eType, Z.pExpr, W); +} + +range_or_rows(A) ::= RANGE|ROWS|GROUPS(X). {A = @X; /*A-overwrites-X*/} + +frame_bound_s(A) ::= frame_bound(X). {A = X;} +frame_bound_s(A) ::= UNBOUNDED(X) PRECEDING. {A.eType = @X; A.pExpr = 0;} +frame_bound_e(A) ::= frame_bound(X). {A = X;} +frame_bound_e(A) ::= UNBOUNDED(X) FOLLOWING. {A.eType = @X; A.pExpr = 0;} + +frame_bound(A) ::= expr(X) PRECEDING|FOLLOWING(Y). + {A.eType = @Y; A.pExpr = X;} +frame_bound(A) ::= CURRENT(X) ROW. {A.eType = @X; A.pExpr = 0;} + +%type frame_exclude_opt {u8} +frame_exclude_opt(A) ::= . {A = 0;} +frame_exclude_opt(A) ::= EXCLUDE frame_exclude(X). {A = X;} + +%type frame_exclude {u8} +frame_exclude(A) ::= NO(X) OTHERS. {A = @X; /*A-overwrites-X*/} +frame_exclude(A) ::= CURRENT(X) ROW. {A = @X; /*A-overwrites-X*/} +frame_exclude(A) ::= GROUP|TIES(X). {A = @X; /*A-overwrites-X*/} + + +%type window_clause {Window*} +%destructor window_clause {sqlite3WindowListDelete(pParse->db, $$);} +window_clause(A) ::= WINDOW windowdefn_list(B). { A = B; } + +filter_over(A) ::= filter_clause(F) over_clause(O). { + if( O ){ + O->pFilter = F; + }else{ + sqlite3ExprDelete(pParse->db, F); + } + A = O; +} +filter_over(A) ::= over_clause(O). { + A = O; +} +filter_over(A) ::= filter_clause(F). { + A = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window)); + if( A ){ + A->eFrmType = TK_FILTER; + A->pFilter = F; + }else{ + sqlite3ExprDelete(pParse->db, F); + } +} + +over_clause(A) ::= OVER LP window(Z) RP. { + A = Z; + assert( A!=0 ); +} +over_clause(A) ::= OVER nm(Z). { + A = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window)); + if( A ){ + A->zName = sqlite3DbStrNDup(pParse->db, Z.z, Z.n); + } +} + +filter_clause(A) ::= FILTER LP WHERE expr(X) RP. { A = X; } +%endif /* SQLITE_OMIT_WINDOWFUNC */ + +/* +** The code generator needs some extra TK_ token values for tokens that +** are synthesized and do not actually appear in the grammar: +*/ +%token + COLUMN /* Reference to a table column */ + AGG_FUNCTION /* An aggregate function */ + AGG_COLUMN /* An aggregated column */ + TRUEFALSE /* True or false keyword */ + ISNOT /* Combination of IS and NOT */ + FUNCTION /* A function invocation */ + UPLUS /* Unary plus */ + UMINUS /* Unary minus */ + TRUTH /* IS TRUE or IS FALSE or IS NOT TRUE or IS NOT FALSE */ + REGISTER /* Reference to a VDBE register */ + VECTOR /* Vector */ + SELECT_COLUMN /* Choose a single column from a multi-column SELECT */ + IF_NULL_ROW /* the if-null-row operator */ + ASTERISK /* The "*" in count(*) and similar */ + SPAN /* The span operator */ + ERROR /* An expression containing an error */ +. + +term(A) ::= QNUMBER(X). { + A=tokenExpr(pParse,@X,X); + sqlite3DequoteNumber(pParse, A); +} + +/* There must be no more than 255 tokens defined above. If this grammar +** is extended with new rules and tokens, they must either be so few in +** number that TK_SPAN is no more than 255, or else the new tokens must +** appear after this line. +*/ +%include { +#if TK_SPAN>255 +# error too many tokens in the grammar +#endif +} + +/* +** The TK_SPACE, TK_COMMENT, and TK_ILLEGAL tokens must be the last three +** tokens. The parser depends on this. Those tokens are not used in any +** grammar rule. They are only used by the tokenizer. Declare them last +** so that they are guaranteed to be the last three. +*/ +%token SPACE COMMENT ILLEGAL. diff --git a/src/pcache.c b/src/pcache.c index 5ac9d34a1e..3429284dc9 100644 --- a/src/pcache.c +++ b/src/pcache.c @@ -14,12 +14,34 @@ #include "sqliteInt.h" /* -** A complete page cache is an instance of this structure. +** A complete page cache is an instance of this structure. Every +** entry in the cache holds a single page of the database file. The +** btree layer only operates on the cached copy of the database pages. +** +** A page cache entry is "clean" if it exactly matches what is currently +** on disk. A page is "dirty" if it has been modified and needs to be +** persisted to disk. +** +** pDirty, pDirtyTail, pSynced: +** All dirty pages are linked into the doubly linked list using +** PgHdr.pDirtyNext and pDirtyPrev. The list is maintained in LRU order +** such that p was added to the list more recently than p->pDirtyNext. +** PCache.pDirty points to the first (newest) element in the list and +** pDirtyTail to the last (oldest). +** +** The PCache.pSynced variable is used to optimize searching for a dirty +** page to eject from the cache mid-transaction. It is better to eject +** a page that does not require a journal sync than one that does. +** Therefore, pSynced is maintained so that it *almost* always points +** to either the oldest page in the pDirty/pDirtyTail list that has a +** clear PGHDR_NEED_SYNC flag or to a page that is older than this one +** (so that the right page to eject can be found by following pDirtyPrev +** pointers). */ struct PCache { PgHdr *pDirty, *pDirtyTail; /* List of dirty pages in LRU order */ PgHdr *pSynced; /* Last synced page in dirty page list */ - int nRefSum; /* Sum of ref counts over all pages */ + i64 nRefSum; /* Sum of ref counts over all pages */ int szCache; /* Configured cache size */ int szSpill; /* Size before spilling occurs */ int szPage; /* Size of every page in this cache */ @@ -31,6 +53,132 @@ struct PCache { sqlite3_pcache *pCache; /* Pluggable cache module */ }; +/********************************** Test and Debug Logic **********************/ +/* +** Debug tracing macros. Enable by by changing the "0" to "1" and +** recompiling. +** +** When sqlite3PcacheTrace is 1, single line trace messages are issued. +** When sqlite3PcacheTrace is 2, a dump of the pcache showing all cache entries +** is displayed for many operations, resulting in a lot of output. +*/ +#if defined(SQLITE_DEBUG) && 0 + int sqlite3PcacheTrace = 2; /* 0: off 1: simple 2: cache dumps */ + int sqlite3PcacheMxDump = 9999; /* Max cache entries for pcacheDump() */ +# define pcacheTrace(X) if(sqlite3PcacheTrace){sqlite3DebugPrintf X;} + static void pcachePageTrace(int i, sqlite3_pcache_page *pLower){ + PgHdr *pPg; + unsigned char *a; + int j; + if( pLower==0 ){ + printf("%3d: NULL\n", i); + }else{ + pPg = (PgHdr*)pLower->pExtra; + printf("%3d: nRef %2lld flgs %02x data ", i, pPg->nRef, pPg->flags); + a = (unsigned char *)pLower->pBuf; + for(j=0; j<12; j++) printf("%02x", a[j]); + printf(" ptr %p\n", pPg); + } + } + static void pcacheDump(PCache *pCache){ + int N; + int i; + sqlite3_pcache_page *pLower; + + if( sqlite3PcacheTrace<2 ) return; + if( pCache->pCache==0 ) return; + N = sqlite3PcachePagecount(pCache); + if( N>sqlite3PcacheMxDump ) N = sqlite3PcacheMxDump; + for(i=1; i<=N; i++){ + pLower = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, i, 0); + pcachePageTrace(i, pLower); + if( pLower && ((PgHdr*)pLower)->pPage==0 ){ + sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, pLower, 0); + } + } + } +#else +# define pcacheTrace(X) +# define pcachePageTrace(PGNO, X) +# define pcacheDump(X) +#endif + +/* +** Return 1 if pPg is on the dirty list for pCache. Return 0 if not. +** This routine runs inside of assert() statements only. +*/ +#if defined(SQLITE_ENABLE_EXPENSIVE_ASSERT) +static int pageOnDirtyList(PCache *pCache, PgHdr *pPg){ + PgHdr *p; + for(p=pCache->pDirty; p; p=p->pDirtyNext){ + if( p==pPg ) return 1; + } + return 0; +} +static int pageNotOnDirtyList(PCache *pCache, PgHdr *pPg){ + PgHdr *p; + for(p=pCache->pDirty; p; p=p->pDirtyNext){ + if( p==pPg ) return 0; + } + return 1; +} +#else +# define pageOnDirtyList(A,B) 1 +# define pageNotOnDirtyList(A,B) 1 +#endif + +/* +** Check invariants on a PgHdr entry. Return true if everything is OK. +** Return false if any invariant is violated. +** +** This routine is for use inside of assert() statements only. For +** example: +** +** assert( sqlite3PcachePageSanity(pPg) ); +*/ +#ifdef SQLITE_DEBUG +int sqlite3PcachePageSanity(PgHdr *pPg){ + PCache *pCache; + assert( pPg!=0 ); + assert( pPg->pgno>0 || pPg->pPager==0 ); /* Page number is 1 or more */ + pCache = pPg->pCache; + assert( pCache!=0 ); /* Every page has an associated PCache */ + if( pPg->flags & PGHDR_CLEAN ){ + assert( (pPg->flags & PGHDR_DIRTY)==0 );/* Cannot be both CLEAN and DIRTY */ + assert( pageNotOnDirtyList(pCache, pPg) );/* CLEAN pages not on dirtylist */ + }else{ + assert( (pPg->flags & PGHDR_DIRTY)!=0 );/* If not CLEAN must be DIRTY */ + assert( pPg->pDirtyNext==0 || pPg->pDirtyNext->pDirtyPrev==pPg ); + assert( pPg->pDirtyPrev==0 || pPg->pDirtyPrev->pDirtyNext==pPg ); + assert( pPg->pDirtyPrev!=0 || pCache->pDirty==pPg ); + assert( pageOnDirtyList(pCache, pPg) ); + } + /* WRITEABLE pages must also be DIRTY */ + if( pPg->flags & PGHDR_WRITEABLE ){ + assert( pPg->flags & PGHDR_DIRTY ); /* WRITEABLE implies DIRTY */ + } + /* NEED_SYNC can be set independently of WRITEABLE. This can happen, + ** for example, when using the sqlite3PagerDontWrite() optimization: + ** (1) Page X is journalled, and gets WRITEABLE and NEED_SEEK. + ** (2) Page X moved to freelist, WRITEABLE is cleared + ** (3) Page X reused, WRITEABLE is set again + ** If NEED_SYNC had been cleared in step 2, then it would not be reset + ** in step 3, and page might be written into the database without first + ** syncing the rollback journal, which might cause corruption on a power + ** loss. + ** + ** Another example is when the database page size is smaller than the + ** disk sector size. When any page of a sector is journalled, all pages + ** in that sector are marked NEED_SYNC even if they are still CLEAN, just + ** in case they are later modified, since all pages in the same sector + ** must be journalled and synced before any of those pages can be safely + ** written. + */ + return 1; +} +#endif /* SQLITE_DEBUG */ + + /********************************** Linked List Management ********************/ /* Allowed values for second argument to pcacheManageDirtyList() */ @@ -47,17 +195,16 @@ struct PCache { static void pcacheManageDirtyList(PgHdr *pPage, u8 addRemove){ PCache *p = pPage->pCache; + pcacheTrace(("%p.DIRTYLIST.%s %d\n", p, + addRemove==1 ? "REMOVE" : addRemove==2 ? "ADD" : "FRONT", + pPage->pgno)); if( addRemove & PCACHE_DIRTYLIST_REMOVE ){ assert( pPage->pDirtyNext || pPage==p->pDirtyTail ); assert( pPage->pDirtyPrev || pPage==p->pDirty ); /* Update the PCache1.pSynced variable if necessary. */ if( p->pSynced==pPage ){ - PgHdr *pSynced = pPage->pDirtyPrev; - while( pSynced && (pSynced->flags&PGHDR_NEED_SYNC) ){ - pSynced = pSynced->pDirtyPrev; - } - p->pSynced = pSynced; + p->pSynced = pPage->pDirtyPrev; } if( pPage->pDirtyNext ){ @@ -69,19 +216,21 @@ static void pcacheManageDirtyList(PgHdr *pPage, u8 addRemove){ if( pPage->pDirtyPrev ){ pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext; }else{ + /* If there are now no dirty pages in the cache, set eCreate to 2. + ** This is an optimization that allows sqlite3PcacheFetch() to skip + ** searching for a dirty page to eject from the cache when it might + ** otherwise have to. */ assert( pPage==p->pDirty ); p->pDirty = pPage->pDirtyNext; - if( p->pDirty==0 && p->bPurgeable ){ - assert( p->eCreate==1 ); + assert( p->bPurgeable || p->eCreate==2 ); + if( p->pDirty==0 ){ /*OPTIMIZATION-IF-TRUE*/ + assert( p->bPurgeable==0 || p->eCreate==1 ); p->eCreate = 2; } } - pPage->pDirtyNext = 0; - pPage->pDirtyPrev = 0; } if( addRemove & PCACHE_DIRTYLIST_ADD ){ - assert( pPage->pDirtyNext==0 && pPage->pDirtyPrev==0 && p->pDirty!=pPage ); - + pPage->pDirtyPrev = 0; pPage->pDirtyNext = p->pDirty; if( pPage->pDirtyNext ){ assert( pPage->pDirtyNext->pDirtyPrev==0 ); @@ -94,10 +243,19 @@ static void pcacheManageDirtyList(PgHdr *pPage, u8 addRemove){ } } p->pDirty = pPage; - if( !p->pSynced && 0==(pPage->flags&PGHDR_NEED_SYNC) ){ + + /* If pSynced is NULL and this page has a clear NEED_SYNC flag, set + ** pSynced to point to it. Checking the NEED_SYNC flag is an + ** optimization, as if pSynced points to a page with the NEED_SYNC + ** flag set sqlite3PcacheFetchStress() searches through all newer + ** entries of the dirty-list for a page with NEED_SYNC clear anyway. */ + if( !p->pSynced + && 0==(pPage->flags&PGHDR_NEED_SYNC) /*OPTIMIZATION-IF-FALSE*/ + ){ p->pSynced = pPage; } } + pcacheDump(p); } /* @@ -106,7 +264,9 @@ static void pcacheManageDirtyList(PgHdr *pPage, u8 addRemove){ */ static void pcacheUnpin(PgHdr *p){ if( p->pCache->bPurgeable ){ + pcacheTrace(("%p.UNPIN %d\n", p->pCache, p->pgno)); sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0); + pcacheDump(p->pCache); } } @@ -120,10 +280,14 @@ static int numberOfCachePages(PCache *p){ ** suggested cache size is set to N. */ return p->szCache; }else{ - /* IMPLEMENTATION-OF: R-61436-13639 If the argument N is negative, then - ** the number of cache pages is adjusted to use approximately abs(N*1024) - ** bytes of memory. */ - return (int)((-1024*(i64)p->szCache)/(p->szPage+p->szExtra)); + i64 n; + /* IMPLEMENTATION-OF: R-59858-46238 If the argument N is negative, then the + ** number of cache pages is adjusted to be a number of pages that would + ** use approximately abs(N*1024) bytes of memory based on the current + ** page size. */ + n = ((-1024*(i64)p->szCache)/(p->szPage+p->szExtra)); + if( n>1000000000 ) n = 1000000000; + return (int)n; } } @@ -138,6 +302,7 @@ int sqlite3PcacheInitialize(void){ ** built-in default page cache is used instead of the application defined ** page cache. */ sqlite3PCacheSetDefault(); + assert( sqlite3GlobalConfig.pcache2.xInit!=0 ); } return sqlite3GlobalConfig.pcache2.xInit(sqlite3GlobalConfig.pcache2.pArg); } @@ -158,6 +323,12 @@ int sqlite3PcacheSize(void){ return sizeof(PCache); } ** has already been allocated and is passed in as the p pointer. ** The caller discovers how much space needs to be allocated by ** calling sqlite3PcacheSize(). +** +** szExtra is some extra space allocated for each page. The first +** 8 bytes of the extra space will be zeroed as the page is allocated, +** but remaining content will be uninitialized. Though it is opaque +** to this module, the extra space really ends up being the MemPage +** structure in the pager. */ int sqlite3PcacheOpen( int szPage, /* Size of every page */ @@ -170,12 +341,14 @@ int sqlite3PcacheOpen( memset(p, 0, sizeof(PCache)); p->szPage = 1; p->szExtra = szExtra; + assert( szExtra>=8 ); /* First 8 bytes will be zeroed */ p->bPurgeable = bPurgeable; p->eCreate = 2; p->xStress = xStress; p->pStress = pStress; p->szCache = 100; p->szSpill = 1; + pcacheTrace(("%p.OPEN szPage %d bPurgeable %d\n",p,szPage,bPurgeable)); return sqlite3PcacheSetPageSize(p, szPage); } @@ -191,13 +364,14 @@ int sqlite3PcacheSetPageSize(PCache *pCache, int szPage){ szPage, pCache->szExtra + ROUND8(sizeof(PgHdr)), pCache->bPurgeable ); - if( pNew==0 ) return SQLITE_NOMEM; + if( pNew==0 ) return SQLITE_NOMEM_BKPT; sqlite3GlobalConfig.pcache2.xCachesize(pNew, numberOfCachePages(pCache)); if( pCache->pCache ){ sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache); } pCache->pCache = pNew; pCache->szPage = szPage; + pcacheTrace(("%p.PAGESIZE %d\n",pCache,szPage)); } return SQLITE_OK; } @@ -232,11 +406,12 @@ sqlite3_pcache_page *sqlite3PcacheFetch( int createFlag /* If true, create page if it does not exist already */ ){ int eCreate; + sqlite3_pcache_page *pRes; assert( pCache!=0 ); assert( pCache->pCache!=0 ); assert( createFlag==3 || createFlag==0 ); - assert( pgno>0 ); + assert( pCache->eCreate==((pCache->bPurgeable && pCache->pDirty) ? 1 : 2) ); /* eCreate defines what to do if the page does not exist. ** 0 Do not allocate a new page. (createFlag==0) @@ -249,12 +424,16 @@ sqlite3_pcache_page *sqlite3PcacheFetch( assert( eCreate==0 || eCreate==1 || eCreate==2 ); assert( createFlag==0 || pCache->eCreate==eCreate ); assert( createFlag==0 || eCreate==1+(!pCache->bPurgeable||!pCache->pDirty) ); - return sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate); + pRes = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate); + pcacheTrace(("%p.FETCH %d%s (result: %p) ",pCache,pgno, + createFlag?" create":"",pRes)); + pcachePageTrace(pgno, pRes); + return pRes; } /* ** If the sqlite3PcacheFetch() routine is unable to allocate a new -** page because new clean pages are available for reuse and the cache +** page because no clean pages are available for reuse and the cache ** size limit has been reached, then this routine can be invoked to ** try harder to allocate a page. This routine might invoke the stress ** callback to spill dirty pages to the journal. It will then try to @@ -276,7 +455,11 @@ int sqlite3PcacheFetchStress( ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC ** cleared), but if that is not possible settle for any other ** unreferenced dirty page. - */ + ** + ** If the LRU page in the dirty list that has a clear PGHDR_NEED_SYNC + ** flag is currently referenced, then the following may leave pSynced + ** set incorrectly (pointing to other than the LRU page with NEED_SYNC + ** cleared). This is Ok, as pSynced is just an optimization. */ for(pPg=pCache->pSynced; pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC)); pPg=pPg->pDirtyPrev @@ -291,17 +474,19 @@ int sqlite3PcacheFetchStress( sqlite3_log(SQLITE_FULL, "spill page %d making room for %d - cache used: %d/%d", pPg->pgno, pgno, - sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache), + sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache), numberOfCachePages(pCache)); #endif + pcacheTrace(("%p.SPILL %d\n",pCache,pPg->pgno)); rc = pCache->xStress(pCache->pStress, pPg); + pcacheDump(pCache); if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){ return rc; } } } *ppPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2); - return *ppPage==0 ? SQLITE_NOMEM : SQLITE_OK; + return *ppPage==0 ? SQLITE_NOMEM_BKPT : SQLITE_OK; } /* @@ -322,11 +507,12 @@ static SQLITE_NOINLINE PgHdr *pcacheFetchFinishWithInit( assert( pPage!=0 ); pPgHdr = (PgHdr*)pPage->pExtra; assert( pPgHdr->pPage==0 ); - memset(pPgHdr, 0, sizeof(PgHdr)); + memset(&pPgHdr->pDirty, 0, sizeof(PgHdr) - offsetof(PgHdr,pDirty)); pPgHdr->pPage = pPage; pPgHdr->pData = pPage->pBuf; pPgHdr->pExtra = (void *)&pPgHdr[1]; - memset(pPgHdr->pExtra, 0, pCache->szExtra); + memset(pPgHdr->pExtra, 0, 8); + assert( EIGHT_BYTE_ALIGNMENT( pPgHdr->pExtra ) ); pPgHdr->pCache = pCache; pPgHdr->pgno = pgno; pPgHdr->flags = PGHDR_CLEAN; @@ -354,6 +540,7 @@ PgHdr *sqlite3PcacheFetchFinish( } pCache->nRefSum++; pPgHdr->nRef++; + assert( sqlite3PcachePageSanity(pPgHdr) ); return pPgHdr; } @@ -367,9 +554,9 @@ void SQLITE_NOINLINE sqlite3PcacheRelease(PgHdr *p){ if( (--p->nRef)==0 ){ if( p->flags&PGHDR_CLEAN ){ pcacheUnpin(p); - }else if( p->pDirtyPrev!=0 ){ - /* Move the page to the head of the dirty list. */ + }else{ pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT); + assert( sqlite3PcachePageSanity(p) ); } } } @@ -379,6 +566,7 @@ void SQLITE_NOINLINE sqlite3PcacheRelease(PgHdr *p){ */ void sqlite3PcacheRef(PgHdr *p){ assert(p->nRef>0); + assert( sqlite3PcachePageSanity(p) ); p->nRef++; p->pCache->nRefSum++; } @@ -390,6 +578,7 @@ void sqlite3PcacheRef(PgHdr *p){ */ void sqlite3PcacheDrop(PgHdr *p){ assert( p->nRef==1 ); + assert( sqlite3PcachePageSanity(p) ); if( p->flags&PGHDR_DIRTY ){ pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE); } @@ -403,13 +592,17 @@ void sqlite3PcacheDrop(PgHdr *p){ */ void sqlite3PcacheMakeDirty(PgHdr *p){ assert( p->nRef>0 ); - if( p->flags & (PGHDR_CLEAN|PGHDR_DONT_WRITE) ){ + assert( sqlite3PcachePageSanity(p) ); + if( p->flags & (PGHDR_CLEAN|PGHDR_DONT_WRITE) ){ /*OPTIMIZATION-IF-FALSE*/ p->flags &= ~PGHDR_DONT_WRITE; if( p->flags & PGHDR_CLEAN ){ p->flags ^= (PGHDR_DIRTY|PGHDR_CLEAN); + pcacheTrace(("%p.DIRTY %d\n",p->pCache,p->pgno)); assert( (p->flags & (PGHDR_DIRTY|PGHDR_CLEAN))==PGHDR_DIRTY ); pcacheManageDirtyList(p, PCACHE_DIRTYLIST_ADD); + assert( sqlite3PcachePageSanity(p) ); } + assert( sqlite3PcachePageSanity(p) ); } } @@ -418,14 +611,16 @@ void sqlite3PcacheMakeDirty(PgHdr *p){ ** make it so. */ void sqlite3PcacheMakeClean(PgHdr *p){ - if( (p->flags & PGHDR_DIRTY) ){ - assert( (p->flags & PGHDR_CLEAN)==0 ); - pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE); - p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC|PGHDR_WRITEABLE); - p->flags |= PGHDR_CLEAN; - if( p->nRef==0 ){ - pcacheUnpin(p); - } + assert( sqlite3PcachePageSanity(p) ); + assert( (p->flags & PGHDR_DIRTY)!=0 ); + assert( (p->flags & PGHDR_CLEAN)==0 ); + pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE); + p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC|PGHDR_WRITEABLE); + p->flags |= PGHDR_CLEAN; + pcacheTrace(("%p.CLEAN %d\n",p->pCache,p->pgno)); + assert( sqlite3PcachePageSanity(p) ); + if( p->nRef==0 ){ + pcacheUnpin(p); } } @@ -434,11 +629,24 @@ void sqlite3PcacheMakeClean(PgHdr *p){ */ void sqlite3PcacheCleanAll(PCache *pCache){ PgHdr *p; + pcacheTrace(("%p.CLEAN-ALL\n",pCache)); while( (p = pCache->pDirty)!=0 ){ sqlite3PcacheMakeClean(p); } } +/* +** Clear the PGHDR_NEED_SYNC and PGHDR_WRITEABLE flag from all dirty pages. +*/ +void sqlite3PcacheClearWritable(PCache *pCache){ + PgHdr *p; + pcacheTrace(("%p.CLEAR-WRITEABLE\n",pCache)); + for(p=pCache->pDirty; p; p=p->pDirtyNext){ + p->flags &= ~(PGHDR_NEED_SYNC|PGHDR_WRITEABLE); + } + pCache->pSynced = pCache->pDirtyTail; +} + /* ** Clear the PGHDR_NEED_SYNC flag from all dirty pages. */ @@ -455,12 +663,24 @@ void sqlite3PcacheClearSyncFlags(PCache *pCache){ */ void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){ PCache *pCache = p->pCache; + sqlite3_pcache_page *pOther; assert( p->nRef>0 ); assert( newPgno>0 ); + assert( sqlite3PcachePageSanity(p) ); + pcacheTrace(("%p.MOVE %d -> %d\n",pCache,p->pgno,newPgno)); + pOther = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, newPgno, 0); + if( pOther ){ + PgHdr *pXPage = (PgHdr*)pOther->pExtra; + assert( pXPage->nRef==0 ); + pXPage->nRef++; + pCache->nRefSum++; + sqlite3PcacheDrop(pXPage); + } sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno); p->pgno = newPgno; if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){ pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT); + assert( sqlite3PcachePageSanity(p) ); } } @@ -477,6 +697,7 @@ void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){ if( pCache->pCache ){ PgHdr *p; PgHdr *pNext; + pcacheTrace(("%p.TRUNCATE %d\n",pCache,pgno)); for(p=pCache->pDirty; p; p=pNext){ pNext = p->pDirtyNext; /* This routine never gets call with a positive pgno except right @@ -484,7 +705,7 @@ void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){ ** it must be that pgno==0. */ assert( p->pgno>0 ); - if( ALWAYS(p->pgno>pgno) ){ + if( p->pgno>pgno ){ assert( p->flags&PGHDR_DIRTY ); sqlite3PcacheMakeClean(p); } @@ -507,6 +728,7 @@ void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){ */ void sqlite3PcacheClose(PCache *pCache){ assert( pCache->pCache!=0 ); + pcacheTrace(("%p.CLOSE\n",pCache)); sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache); } @@ -519,34 +741,36 @@ void sqlite3PcacheClear(PCache *pCache){ /* ** Merge two lists of pages connected by pDirty and in pgno order. -** Do not both fixing the pDirtyPrev pointers. +** Do not bother fixing the pDirtyPrev pointers. */ static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){ PgHdr result, *pTail; pTail = &result; - while( pA && pB ){ + assert( pA!=0 && pB!=0 ); + for(;;){ if( pA->pgnopgno ){ pTail->pDirty = pA; pTail = pA; pA = pA->pDirty; + if( pA==0 ){ + pTail->pDirty = pB; + break; + } }else{ pTail->pDirty = pB; pTail = pB; pB = pB->pDirty; + if( pB==0 ){ + pTail->pDirty = pA; + break; + } } } - if( pA ){ - pTail->pDirty = pA; - }else if( pB ){ - pTail->pDirty = pB; - }else{ - pTail->pDirty = 0; - } return result.pDirty; } /* -** Sort the list of pages in accending order by pgno. Pages are +** Sort the list of pages in ascending order by pgno. Pages are ** connected by pDirty pointers. The pDirtyPrev pointers are ** corrupted by this sort. ** @@ -582,7 +806,8 @@ static PgHdr *pcacheSortDirtyList(PgHdr *pIn){ } p = a[0]; for(i=1; inRefSum; } /* ** Return the number of references to the page supplied as an argument. */ -int sqlite3PcachePageRefcount(PgHdr *p){ +i64 sqlite3PcachePageRefcount(PgHdr *p){ return p->nRef; } @@ -675,6 +900,26 @@ void sqlite3PcacheShrink(PCache *pCache){ */ int sqlite3HeaderSizePcache(void){ return ROUND8(sizeof(PgHdr)); } +/* +** Return the number of dirty pages currently in the cache, as a percentage +** of the configured cache size. +*/ +int sqlite3PCachePercentDirty(PCache *pCache){ + PgHdr *pDirty; + int nDirty = 0; + int nCache = numberOfCachePages(pCache); + for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext) nDirty++; + return nCache ? (int)(((i64)nDirty * 100) / nCache) : 0; +} + +#ifdef SQLITE_DIRECT_OVERFLOW_READ +/* +** Return true if there are one or more dirty pages in the cache. Else false. +*/ +int sqlite3PCacheIsDirty(PCache *pCache){ + return (pCache->pDirty!=0); +} +#endif #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG) /* diff --git a/src/pcache.h b/src/pcache.h index 475c04c061..f945dab1a4 100644 --- a/src/pcache.h +++ b/src/pcache.h @@ -26,7 +26,8 @@ struct PgHdr { sqlite3_pcache_page *pPage; /* Pcache object page handle */ void *pData; /* Page data */ void *pExtra; /* Extra content */ - PgHdr *pDirty; /* Transient list of dirty pages */ + PCache *pCache; /* PRIVATE: Cache that owns this page */ + PgHdr *pDirty; /* Transient list of dirty sorted by pgno */ Pager *pPager; /* The pager this page is part of */ Pgno pgno; /* Page number for this page */ #ifdef SQLITE_CHECK_PAGES @@ -35,14 +36,15 @@ struct PgHdr { u16 flags; /* PGHDR flags defined below */ /********************************************************************** - ** Elements above are public. All that follows is private to pcache.c - ** and should not be accessed by other modules. + ** Elements above, except pCache, are public. All that follow are + ** private to pcache.c and should not be accessed by other modules. + ** pCache is grouped with the public elements for efficiency. */ - i16 nRef; /* Number of users of this page */ - PCache *pCache; /* Cache that owns this page */ - + i64 nRef; /* Number of users of this page */ PgHdr *pDirtyNext; /* Next element in list of dirty pages */ PgHdr *pDirtyPrev; /* Previous element in list of dirty pages */ + /* NB: pDirtyNext and pDirtyPrev are undefined if the + ** PgHdr object is not dirty */ }; /* Bit values for PgHdr.flags */ @@ -51,11 +53,10 @@ struct PgHdr { #define PGHDR_WRITEABLE 0x004 /* Journaled and ready to modify */ #define PGHDR_NEED_SYNC 0x008 /* Fsync the rollback journal before ** writing this page to the database */ -#define PGHDR_NEED_READ 0x010 /* Content is unread */ -#define PGHDR_DONT_WRITE 0x020 /* Do not write content to disk */ -#define PGHDR_MMAP 0x040 /* This is an mmap page object */ +#define PGHDR_DONT_WRITE 0x010 /* Do not write content to disk */ +#define PGHDR_MMAP 0x020 /* This is an mmap page object */ -#define PGHDR_WAL_APPEND 0x080 /* Appended to wal file */ +#define PGHDR_WAL_APPEND 0x040 /* Appended to wal file */ /* Initialize and shutdown the page cache subsystem */ int sqlite3PcacheInitialize(void); @@ -99,6 +100,7 @@ void sqlite3PcacheDrop(PgHdr*); /* Remove page from cache */ void sqlite3PcacheMakeDirty(PgHdr*); /* Make sure page is marked dirty */ void sqlite3PcacheMakeClean(PgHdr*); /* Mark a single page as clean */ void sqlite3PcacheCleanAll(PCache*); /* Mark all dirty list pages as clean */ +void sqlite3PcacheClearWritable(PCache*); /* Change a page number. Used by incr-vacuum. */ void sqlite3PcacheMove(PgHdr*, Pgno); @@ -119,12 +121,12 @@ void sqlite3PcacheClearSyncFlags(PCache *); void sqlite3PcacheClear(PCache*); /* Return the total number of outstanding page references */ -int sqlite3PcacheRefCount(PCache*); +i64 sqlite3PcacheRefCount(PCache*); /* Increment the reference count of an existing page */ void sqlite3PcacheRef(PgHdr*); -int sqlite3PcachePageRefcount(PgHdr*); +i64 sqlite3PcachePageRefcount(PgHdr*); /* Return the total number of pages stored in the cache */ int sqlite3PcachePagecount(PCache*); @@ -137,6 +139,11 @@ int sqlite3PcachePagecount(PCache*); void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *)); #endif +#if defined(SQLITE_DEBUG) +/* Check invariants on a PgHdr object */ +int sqlite3PcachePageSanity(PgHdr*); +#endif + /* Set and get the suggested cache-size for the specified pager-cache. ** ** If no global maximum is configured, then the system attempts to limit @@ -173,4 +180,11 @@ void sqlite3PCacheSetDefault(void); int sqlite3HeaderSizePcache(void); int sqlite3HeaderSizePcache1(void); +/* Number of dirty pages as a percentage of the configured cache size */ +int sqlite3PCachePercentDirty(PCache*); + +#ifdef SQLITE_DIRECT_OVERFLOW_READ +int sqlite3PCacheIsDirty(PCache *pCache); +#endif + #endif /* _PCACHE_H_ */ diff --git a/src/pcache1.c b/src/pcache1.c index 7147f6a7a8..39607328f3 100644 --- a/src/pcache1.c +++ b/src/pcache1.c @@ -39,12 +39,13 @@ ** size can vary according to architecture, compile-time options, and ** SQLite library version number. ** -** If SQLITE_PCACHE_SEPARATE_HEADER is defined, then the extension is obtained -** using a separate memory allocation from the database page content. This -** seeks to overcome the "clownshoe" problem (also called "internal -** fragmentation" in academic literature) of allocating a few bytes more -** than a power of two with the memory allocator rounding up to the next -** power of two, and leaving the rounded-up space unused. +** Historical note: It used to be that if the SQLITE_PCACHE_SEPARATE_HEADER +** was defined, then the page content would be held in a separate memory +** allocation from the PgHdr1. This was intended to avoid clownshoe memory +** allocations. However, the btree layer needs a small (16-byte) overrun +** area after the page content buffer. The header serves as that overrun +** area. Therefore SQLITE_PCACHE_SEPARATE_HEADER was discontinued to avoid +** any possibility of a memory error. ** ** This module tracks pointers to PgHdr1 objects. Only pcache.c communicates ** with this module. Information is passed back and forth as PgHdr1 pointers. @@ -63,14 +64,14 @@ ** ** The third case is a chunk of heap memory (defaulting to 100 pages worth) ** that is allocated when the page cache is created. The size of the local -** bulk allocation can be adjusted using +** bulk allocation can be adjusted using ** ** sqlite3_config(SQLITE_CONFIG_PAGECACHE, (void*)0, 0, N). ** ** If N is positive, then N pages worth of memory are allocated using a single ** sqlite3Malloc() call and that memory is used for the first N pages allocated. ** Or if N is negative, then -1024*N bytes of memory are allocated and used -** for as many pages as can be accomodated. +** for as many pages as can be accommodated. ** ** Only one of (2) or (3) can be used. Once the memory available to (2) or ** (3) is exhausted, subsequent allocations fail over to the general-purpose @@ -88,24 +89,51 @@ typedef struct PgFreeslot PgFreeslot; typedef struct PGroup PGroup; /* -** Each cache entry is represented by an instance of the following -** structure. Unless SQLITE_PCACHE_SEPARATE_HEADER is defined, a buffer of -** PgHdr1.pCache->szPage bytes is allocated directly before this structure -** in memory. +** Each cache entry is represented by an instance of the following +** structure. A buffer of PgHdr1.pCache->szPage bytes is allocated +** directly before this structure and is used to cache the page content. +** +** When reading a corrupt database file, it is possible that SQLite might +** read a few bytes (no more than 16 bytes) past the end of the page buffer. +** It will only read past the end of the page buffer, never write. This +** object is positioned immediately after the page buffer to serve as an +** overrun area, so that overreads are harmless. +** +** Variables isBulkLocal and isAnchor were once type "u8". That works, +** but causes a 2-byte gap in the structure for most architectures (since +** pointers must be either 4 or 8-byte aligned). As this structure is located +** in memory directly after the associated page data, if the database is +** corrupt, code at the b-tree layer may overread the page buffer and +** read part of this structure before the corruption is detected. This +** can cause a valgrind error if the uninitialized gap is accessed. Using u16 +** ensures there is no such gap, and therefore no bytes of uninitialized +** memory in the structure. +** +** The pLruNext and pLruPrev pointers form a double-linked circular list +** of all pages that are unpinned. The PGroup.lru element (which should be +** the only element on the list with PgHdr1.isAnchor set to 1) forms the +** beginning and the end of the list. */ struct PgHdr1 { - sqlite3_pcache_page page; /* Base class. Must be first. pBuf & pExtra */ - unsigned int iKey; /* Key value (page number) */ - u8 isPinned; /* Page in use, not on the LRU list */ - u8 isBulkLocal; /* This page from bulk local storage */ - u8 isAnchor; /* This is the PGroup.lru element */ - PgHdr1 *pNext; /* Next in hash table chain */ - PCache1 *pCache; /* Cache that currently owns this page */ - PgHdr1 *pLruNext; /* Next in LRU list of unpinned pages */ - PgHdr1 *pLruPrev; /* Previous in LRU list of unpinned pages */ + sqlite3_pcache_page page; /* Base class. Must be first. pBuf & pExtra */ + unsigned int iKey; /* Key value (page number) */ + u16 isBulkLocal; /* This page from bulk local storage */ + u16 isAnchor; /* This is the PGroup.lru element */ + PgHdr1 *pNext; /* Next in hash table chain */ + PCache1 *pCache; /* Cache that currently owns this page */ + PgHdr1 *pLruNext; /* Next in circular LRU list of unpinned pages */ + PgHdr1 *pLruPrev; /* Previous in LRU list of unpinned pages */ + /* NB: pLruPrev is only valid if pLruNext!=0 */ }; -/* Each page cache (or PCache) belongs to a PGroup. A PGroup is a set +/* +** A page is pinned if it is not on the LRU list. To be "pinned" means +** that the page is in active use and must not be deallocated. +*/ +#define PAGE_IS_PINNED(p) ((p)->pLruNext==0) +#define PAGE_IS_UNPINNED(p) ((p)->pLruNext!=0) + +/* Each page cache (or PCache) belongs to a PGroup. A PGroup is a set ** of one or more PCaches that are able to recycle each other's unpinned ** pages when they are under memory pressure. A PGroup is an instance of ** the following object. @@ -132,7 +160,7 @@ struct PGroup { unsigned int nMaxPage; /* Sum of nMax for purgeable caches */ unsigned int nMinPage; /* Sum of nMin for purgeable caches */ unsigned int mxPinned; /* nMaxpage + 10 - nMinPage */ - unsigned int nCurrentPage; /* Number of purgeable pages allocated */ + unsigned int nPurgeable; /* Number of purgeable pages allocated */ PgHdr1 lru; /* The beginning and end of the LRU list */ }; @@ -141,16 +169,18 @@ struct PGroup { ** temporary or transient database) has a single page cache which ** is an instance of this object. ** -** Pointers to structures of this type are cast and returned as +** Pointers to structures of this type are cast and returned as ** opaque sqlite3_pcache* handles. */ struct PCache1 { /* Cache configuration parameters. Page size (szPage) and the purgeable - ** flag (bPurgeable) are set when the cache is created. nMax may be + ** flag (bPurgeable) and the pnPurgeable pointer are all set when the + ** cache is created and are never changed thereafter. nMax may be ** modified at any time by a call to the pcache1Cachesize() method. ** The PGroup mutex must be held when accessing nMax. */ PGroup *pGroup; /* PGroup this cache belongs to */ + unsigned int *pnPurgeable; /* Pointer to pGroup->nPurgeable */ int szPage; /* Size of database content section */ int szExtra; /* sizeof(MemPage)+sizeof(PgHdr) */ int szAlloc; /* Total size of one pcache line */ @@ -159,6 +189,7 @@ struct PCache1 { unsigned int nMax; /* Configured "cache_size" value */ unsigned int n90pct; /* nMax*9/10 */ unsigned int iMaxKey; /* Largest key seen since xTruncate() */ + unsigned int nPurgeableDummy; /* pnPurgeable points here when not used*/ /* Hash table of all pages. The following variables may only be accessed ** when the accessor is holding the PGroup mutex. @@ -192,7 +223,7 @@ static SQLITE_WSD struct PCacheGlobal { */ int isInit; /* True if initialized */ int separateCache; /* Use a new PGroup for each PCache */ - int nInitPage; /* Initial bulk allocation size */ + int nInitPage; /* Initial bulk allocation size */ int szSlot; /* Size of each free slot */ int nSlot; /* The number of pcache slots */ int nReserve; /* Try to keep nFreeSlot above this */ @@ -201,10 +232,6 @@ static SQLITE_WSD struct PCacheGlobal { sqlite3_mutex *mutex; /* Mutex for accessing the following: */ PgFreeslot *pFree; /* Free page blocks */ int nFreeSlot; /* Number of unused pcache slots */ - /* The following value requires a mutex to change. We skip the mutex on - ** reading because (1) most platforms read a 32-bit integer atomically and - ** (2) even if an incorrect value is read, no great harm is done since this - ** is really just an optimization. */ int bUnderPressure; /* True if low on PAGECACHE memory */ } pcache1_g; @@ -233,7 +260,7 @@ static SQLITE_WSD struct PCacheGlobal { /* -** This function is called during initialization if a static buffer is +** This function is called during initialization if a static buffer is ** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE ** verb to sqlite3_config(). Parameter pBuf points to an allocation large ** enough to contain 'n' buffers of 'sz' bytes each. @@ -245,13 +272,14 @@ void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){ if( pcache1.isInit ){ PgFreeslot *p; if( pBuf==0 ) sz = n = 0; + if( n==0 ) sz = 0; sz = ROUNDDOWN8(sz); pcache1.szSlot = sz; pcache1.nSlot = pcache1.nFreeSlot = n; pcache1.nReserve = n>90 ? 10 : (n/10 + 1); pcache1.pStart = pBuf; pcache1.pFree = 0; - pcache1.bUnderPressure = 0; + AtomicStore(&pcache1.bUnderPressure,0); while( n-- ){ p = (PgFreeslot*)pBuf; p->pNext = pcache1.pFree; @@ -279,31 +307,32 @@ static int pcache1InitBulk(PCache1 *pCache){ szBulk = -1024 * (i64)pcache1.nInitPage; } if( szBulk > pCache->szAlloc*(i64)pCache->nMax ){ - szBulk = pCache->szAlloc*pCache->nMax; + szBulk = pCache->szAlloc*(i64)pCache->nMax; } zBulk = pCache->pBulk = sqlite3Malloc( szBulk ); sqlite3EndBenignMalloc(); if( zBulk ){ int nBulk = sqlite3MallocSize(zBulk)/pCache->szAlloc; - int i; - for(i=0; iszPage]; pX->page.pBuf = zBulk; - pX->page.pExtra = &pX[1]; + pX->page.pExtra = (u8*)pX + ROUND8(sizeof(*pX)); + assert( EIGHT_BYTE_ALIGNMENT( pX->page.pExtra ) ); pX->isBulkLocal = 1; pX->isAnchor = 0; pX->pNext = pCache->pFree; + pX->pLruPrev = 0; /* Initializing this saves a valgrind error */ pCache->pFree = pX; zBulk += pCache->szAlloc; - } + }while( --nBulk ); } return pCache->pFree!=0; } /* ** Malloc function used within this file to allocate space from the buffer -** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no -** such buffer exists or there is no space left in it, this function falls +** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no +** such buffer exists or there is no space left in it, this function falls ** back to sqlite3Malloc(). ** ** Multiple threads can run this routine at the same time. Global variables @@ -318,7 +347,7 @@ static void *pcache1Alloc(int nByte){ if( p ){ pcache1.pFree = pcache1.pFree->pNext; pcache1.nFreeSlot--; - pcache1.bUnderPressure = pcache1.nFreeSlot=0 ); sqlite3StatusHighwater(SQLITE_STATUS_PAGECACHE_SIZE, nByte); sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_USED, 1); @@ -348,7 +377,6 @@ static void *pcache1Alloc(int nByte){ ** Free an allocated buffer obtained from pcache1Alloc(). */ static void pcache1Free(void *p){ - int nFreed = 0; if( p==0 ) return; if( SQLITE_WITHIN(p, pcache1.pStart, pcache1.pEnd) ){ PgFreeslot *pSlot; @@ -358,17 +386,20 @@ static void pcache1Free(void *p){ pSlot->pNext = pcache1.pFree; pcache1.pFree = pSlot; pcache1.nFreeSlot++; - pcache1.bUnderPressure = pcache1.nFreeSlotpGroup->mutex) ); if( pCache->pFree || (pCache->nPage==0 && pcache1InitBulk(pCache)) ){ + assert( pCache->pFree!=0 ); p = pCache->pFree; pCache->pFree = p->pNext; p->pNext = 0; }else{ #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT /* The group mutex must be released before pcache1Alloc() is called. This - ** is because it might call sqlite3_release_memory(), which assumes that + ** is because it might call sqlite3_release_memory(), which assumes that ** this mutex is not held. */ assert( pcache1.separateCache==0 ); assert( pCache->pGroup==&pcache1.grp ); pcache1LeaveMutex(pCache->pGroup); #endif if( benignMalloc ){ sqlite3BeginBenignMalloc(); } -#ifdef SQLITE_PCACHE_SEPARATE_HEADER - pPg = pcache1Alloc(pCache->szPage); - p = sqlite3Malloc(sizeof(PgHdr1) + pCache->szExtra); - if( !pPg || !p ){ - pcache1Free(pPg); - sqlite3_free(p); - pPg = 0; - } -#else pPg = pcache1Alloc(pCache->szAlloc); - p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage]; -#endif if( benignMalloc ){ sqlite3EndBenignMalloc(); } #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT pcache1EnterMutex(pCache->pGroup); #endif if( pPg==0 ) return 0; + p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage]; p->page.pBuf = pPg; - p->page.pExtra = &p[1]; + p->page.pExtra = (u8*)p + ROUND8(sizeof(*p)); + assert( EIGHT_BYTE_ALIGNMENT( p->page.pExtra ) ); p->isBulkLocal = 0; p->isAnchor = 0; + p->pLruPrev = 0; /* Initializing this saves a valgrind error */ } - if( pCache->bPurgeable ){ - pCache->pGroup->nCurrentPage++; - } + (*pCache->pnPurgeable)++; return p; } @@ -455,13 +477,8 @@ static void pcache1FreePage(PgHdr1 *p){ pCache->pFree = p; }else{ pcache1Free(p->page.pBuf); -#ifdef SQLITE_PCACHE_SEPARATE_HEADER - sqlite3_free(p); -#endif - } - if( pCache->bPurgeable ){ - pCache->pGroup->nCurrentPage--; } + (*pCache->pnPurgeable)--; } /* @@ -470,6 +487,7 @@ static void pcache1FreePage(PgHdr1 *p){ ** exists, this function falls back to sqlite3Malloc(). */ void *sqlite3PageMalloc(int sz){ + assert( sz<=65536+8 ); /* These allocations are never very large */ return pcache1Alloc(sz); } @@ -499,7 +517,7 @@ void sqlite3PageFree(void *p){ */ static int pcache1UnderMemoryPressure(PCache1 *pCache){ if( pcache1.nSlot && (pCache->szPage+pCache->szExtra)<=pcache1.szSlot ){ - return pcache1.bUnderPressure; + return AtomicLoad(&pcache1.bUnderPressure); }else{ return sqlite3HeapNearlyFull(); } @@ -516,12 +534,12 @@ static int pcache1UnderMemoryPressure(PCache1 *pCache){ */ static void pcache1ResizeHash(PCache1 *p){ PgHdr1 **apNew; - unsigned int nNew; - unsigned int i; + u64 nNew; + u32 i; assert( sqlite3_mutex_held(p->pGroup->mutex) ); - nNew = p->nHash*2; + nNew = 2*(u64)p->nHash; if( nNew<256 ){ nNew = 256; } @@ -549,35 +567,32 @@ static void pcache1ResizeHash(PCache1 *p){ } /* -** This function is used internally to remove the page pPage from the +** This function is used internally to remove the page pPage from the ** PGroup LRU list, if is part of it. If pPage is not part of the PGroup ** LRU list, then this function is a no-op. ** ** The PGroup mutex must be held when this function is called. */ static PgHdr1 *pcache1PinPage(PgHdr1 *pPage){ - PCache1 *pCache; - assert( pPage!=0 ); - assert( pPage->isPinned==0 ); - pCache = pPage->pCache; + assert( PAGE_IS_UNPINNED(pPage) ); assert( pPage->pLruNext ); assert( pPage->pLruPrev ); - assert( sqlite3_mutex_held(pCache->pGroup->mutex) ); + assert( sqlite3_mutex_held(pPage->pCache->pGroup->mutex) ); pPage->pLruPrev->pLruNext = pPage->pLruNext; pPage->pLruNext->pLruPrev = pPage->pLruPrev; pPage->pLruNext = 0; - pPage->pLruPrev = 0; - pPage->isPinned = 1; + /* pPage->pLruPrev = 0; + ** No need to clear pLruPrev as it is never accessed if pLruNext is 0 */ assert( pPage->isAnchor==0 ); - assert( pCache->pGroup->lru.isAnchor==1 ); - pCache->nRecyclable--; + assert( pPage->pCache->pGroup->lru.isAnchor==1 ); + pPage->pCache->nRecyclable--; return pPage; } /* -** Remove the page supplied as an argument from the hash table +** Remove the page supplied as an argument from the hash table ** (PCache1.apHash structure) that it is currently stored in. ** Also free the page if freePage is true. ** @@ -605,11 +620,11 @@ static void pcache1EnforceMaxPage(PCache1 *pCache){ PGroup *pGroup = pCache->pGroup; PgHdr1 *p; assert( sqlite3_mutex_held(pGroup->mutex) ); - while( pGroup->nCurrentPage>pGroup->nMaxPage + while( pGroup->nPurgeable>pGroup->nMaxPage && (p=pGroup->lru.pLruPrev)->isAnchor==0 ){ assert( p->pCache->pGroup==pGroup ); - assert( p->isPinned==0 ); + assert( PAGE_IS_UNPINNED(p) ); pcache1PinPage(p); pcache1RemoveFromHash(p, 1); } @@ -620,8 +635,8 @@ static void pcache1EnforceMaxPage(PCache1 *pCache){ } /* -** Discard all pages from cache pCache with a page number (key value) -** greater than or equal to iLimit. Any pinned pages that meet this +** Discard all pages from cache pCache with a page number (key value) +** greater than or equal to iLimit. Any pinned pages that meet this ** criteria are unpinned before they are discarded. ** ** The PCache mutex must be held when this function is called. @@ -630,25 +645,45 @@ static void pcache1TruncateUnsafe( PCache1 *pCache, /* The cache to truncate */ unsigned int iLimit /* Drop pages with this pgno or larger */ ){ - TESTONLY( unsigned int nPage = 0; ) /* To assert pCache->nPage is correct */ - unsigned int h; + TESTONLY( int nPage = 0; ) /* To assert pCache->nPage is correct */ + unsigned int h, iStop; assert( sqlite3_mutex_held(pCache->pGroup->mutex) ); - for(h=0; hnHash; h++){ - PgHdr1 **pp = &pCache->apHash[h]; + assert( pCache->iMaxKey >= iLimit ); + assert( pCache->nHash > 0 ); + if( pCache->iMaxKey - iLimit < pCache->nHash ){ + /* If we are just shaving the last few pages off the end of the + ** cache, then there is no point in scanning the entire hash table. + ** Only scan those hash slots that might contain pages that need to + ** be removed. */ + h = iLimit % pCache->nHash; + iStop = pCache->iMaxKey % pCache->nHash; + TESTONLY( nPage = -10; ) /* Disable the pCache->nPage validity check */ + }else{ + /* This is the general case where many pages are being removed. + ** It is necessary to scan the entire hash table */ + h = pCache->nHash/2; + iStop = h - 1; + } + for(;;){ + PgHdr1 **pp; PgHdr1 *pPage; + assert( hnHash ); + pp = &pCache->apHash[h]; while( (pPage = *pp)!=0 ){ if( pPage->iKey>=iLimit ){ pCache->nPage--; *pp = pPage->pNext; - if( !pPage->isPinned ) pcache1PinPage(pPage); + if( PAGE_IS_UNPINNED(pPage) ) pcache1PinPage(pPage); pcache1FreePage(pPage); }else{ pp = &pPage->pNext; - TESTONLY( nPage++; ) + TESTONLY( if( nPage>=0 ) nPage++; ) } } + if( h==iStop ) break; + h = (h+1) % pCache->nHash; } - assert( pCache->nPage==nPage ); + assert( nPage<0 || pCache->nPage==(unsigned)nPage ); } /******************************************************************************/ @@ -672,7 +707,7 @@ static int pcache1Init(void *NotUsed){ ** ** * Use a unified cache in single-threaded applications that have ** configured a start-time buffer for use as page-cache memory using - ** sqlite3_config(SQLITE_CONFIG_PAGECACHE, pBuf, sz, N) with non-NULL + ** sqlite3_config(SQLITE_CONFIG_PAGECACHE, pBuf, sz, N) with non-NULL ** pBuf argument. ** ** * Otherwise use separate caches (mode-1) @@ -688,8 +723,8 @@ static int pcache1Init(void *NotUsed){ #if SQLITE_THREADSAFE if( sqlite3GlobalConfig.bCoreMutex ){ - pcache1.grp.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU); - pcache1.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_PMEM); + pcache1.grp.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU); + pcache1.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PMEM); } #endif if( pcache1.separateCache @@ -707,7 +742,7 @@ static int pcache1Init(void *NotUsed){ /* ** Implementation of the sqlite3_pcache.xShutdown method. -** Note that the static mutex allocated in xInit does +** Note that the static mutex allocated in xInit does ** not need to be freed. */ static void pcache1Shutdown(void *NotUsed){ @@ -727,7 +762,7 @@ static void pcache1Destroy(sqlite3_pcache *p); static sqlite3_pcache *pcache1Create(int szPage, int szExtra, int bPurgeable){ PCache1 *pCache; /* The newly created page cache */ PGroup *pGroup; /* The group the new page cache will belong to */ - int sz; /* Bytes of memory required to allocate the new cache */ + i64 sz; /* Bytes of memory required to allocate the new cache */ assert( (szPage & (szPage-1))==0 && szPage>=512 && szPage<=65536 ); assert( szExtra < 300 ); @@ -741,6 +776,7 @@ static sqlite3_pcache *pcache1Create(int szPage, int szExtra, int bPurgeable){ }else{ pGroup = &pcache1.grp; } + pcache1EnterMutex(pGroup); if( pGroup->lru.isAnchor==0 ){ pGroup->lru.isAnchor = 1; pGroup->lru.pLruPrev = pGroup->lru.pLruNext = &pGroup->lru; @@ -750,12 +786,14 @@ static sqlite3_pcache *pcache1Create(int szPage, int szExtra, int bPurgeable){ pCache->szExtra = szExtra; pCache->szAlloc = szPage + szExtra + ROUND8(sizeof(PgHdr1)); pCache->bPurgeable = (bPurgeable ? 1 : 0); - pcache1EnterMutex(pGroup); pcache1ResizeHash(pCache); if( bPurgeable ){ pCache->nMin = 10; pGroup->nMinPage += pCache->nMin; pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage; + pCache->pnPurgeable = &pGroup->nPurgeable; + }else{ + pCache->pnPurgeable = &pCache->nPurgeableDummy; } pcache1LeaveMutex(pGroup); if( pCache->nHash==0 ){ @@ -767,18 +805,24 @@ static sqlite3_pcache *pcache1Create(int szPage, int szExtra, int bPurgeable){ } /* -** Implementation of the sqlite3_pcache.xCachesize method. +** Implementation of the sqlite3_pcache.xCachesize method. ** ** Configure the cache_size limit for a cache. */ static void pcache1Cachesize(sqlite3_pcache *p, int nMax){ PCache1 *pCache = (PCache1 *)p; + u32 n; + assert( nMax>=0 ); if( pCache->bPurgeable ){ PGroup *pGroup = pCache->pGroup; pcache1EnterMutex(pGroup); - pGroup->nMaxPage += (nMax - pCache->nMax); + n = (u32)nMax; + if( n > 0x7fff0000 - pGroup->nMaxPage + pCache->nMax ){ + n = 0x7fff0000 - pGroup->nMaxPage + pCache->nMax; + } + pGroup->nMaxPage += (n - pCache->nMax); pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage; - pCache->nMax = nMax; + pCache->nMax = n; pCache->n90pct = pCache->nMax*9/10; pcache1EnforceMaxPage(pCache); pcache1LeaveMutex(pGroup); @@ -786,7 +830,7 @@ static void pcache1Cachesize(sqlite3_pcache *p, int nMax){ } /* -** Implementation of the sqlite3_pcache.xShrink method. +** Implementation of the sqlite3_pcache.xShrink method. ** ** Free up as much memory as possible. */ @@ -794,7 +838,7 @@ static void pcache1Shrink(sqlite3_pcache *p){ PCache1 *pCache = (PCache1*)p; if( pCache->bPurgeable ){ PGroup *pGroup = pCache->pGroup; - int savedMaxPage; + unsigned int savedMaxPage; pcache1EnterMutex(pGroup); savedMaxPage = pGroup->nMaxPage; pGroup->nMaxPage = 0; @@ -805,7 +849,7 @@ static void pcache1Shrink(sqlite3_pcache *p){ } /* -** Implementation of the sqlite3_pcache.xPagecount method. +** Implementation of the sqlite3_pcache.xPagecount method. */ static int pcache1Pagecount(sqlite3_pcache *p){ int n; @@ -826,8 +870,8 @@ static int pcache1Pagecount(sqlite3_pcache *p){ ** for these steps, the main pcache1Fetch() procedure can run faster. */ static SQLITE_NOINLINE PgHdr1 *pcache1FetchStage2( - PCache1 *pCache, - unsigned int iKey, + PCache1 *pCache, + unsigned int iKey, int createFlag ){ unsigned int nPinned; @@ -857,7 +901,7 @@ static SQLITE_NOINLINE PgHdr1 *pcache1FetchStage2( ){ PCache1 *pOther; pPage = pGroup->lru.pLruPrev; - assert( pPage->isPinned==0 ); + assert( PAGE_IS_UNPINNED(pPage) ); pcache1RemoveFromHash(pPage, 0); pcache1PinPage(pPage); pOther = pPage->pCache; @@ -865,12 +909,12 @@ static SQLITE_NOINLINE PgHdr1 *pcache1FetchStage2( pcache1FreePage(pPage); pPage = 0; }else{ - pGroup->nCurrentPage -= (pOther->bPurgeable - pCache->bPurgeable); + pGroup->nPurgeable -= (pOther->bPurgeable - pCache->bPurgeable); } } - /* Step 5. If a usable page buffer has still not been found, - ** attempt to allocate a new one. + /* Step 5. If a usable page buffer has still not been found, + ** attempt to allocate a new one. */ if( !pPage ){ pPage = pcache1AllocPage(pCache, createFlag==1); @@ -882,9 +926,9 @@ static SQLITE_NOINLINE PgHdr1 *pcache1FetchStage2( pPage->iKey = iKey; pPage->pNext = pCache->apHash[h]; pPage->pCache = pCache; - pPage->pLruPrev = 0; pPage->pLruNext = 0; - pPage->isPinned = 1; + /* pPage->pLruPrev = 0; + ** No need to clear pLruPrev since it is not accessed when pLruNext==0 */ *(void **)pPage->page.pExtra = 0; pCache->apHash[h] = pPage; if( iKey>pCache->iMaxKey ){ @@ -895,13 +939,13 @@ static SQLITE_NOINLINE PgHdr1 *pcache1FetchStage2( } /* -** Implementation of the sqlite3_pcache.xFetch method. +** Implementation of the sqlite3_pcache.xFetch method. ** ** Fetch a page by key value. ** ** Whether or not a new page may be allocated by this function depends on ** the value of the createFlag argument. 0 means do not allocate a new -** page. 1 means allocate a new page if space is easily available. 2 +** page. 1 means allocate a new page if space is easily available. 2 ** means to try really hard to allocate a new page. ** ** For a non-purgeable cache (a cache used as the storage for an in-memory @@ -912,7 +956,7 @@ static SQLITE_NOINLINE PgHdr1 *pcache1FetchStage2( ** There are three different approaches to obtaining space for a page, ** depending on the value of parameter createFlag (which may be 0, 1 or 2). ** -** 1. Regardless of the value of createFlag, the cache is searched for a +** 1. Regardless of the value of createFlag, the cache is searched for a ** copy of the requested page. If one is found, it is returned. ** ** 2. If createFlag==0 and the page is not already in the cache, NULL is @@ -926,13 +970,13 @@ static SQLITE_NOINLINE PgHdr1 *pcache1FetchStage2( ** PCache1.nMax, or ** ** (b) the number of pages pinned by the cache is greater than -** the sum of nMax for all purgeable caches, less the sum of +** the sum of nMax for all purgeable caches, less the sum of ** nMin for all other purgeable caches, or ** ** 4. If none of the first three conditions apply and the cache is marked ** as purgeable, and if one of the following is true: ** -** (a) The number of pages allocated for the cache is already +** (a) The number of pages allocated for the cache is already ** PCache1.nMax, or ** ** (b) The number of pages allocated for all purgeable caches is @@ -944,7 +988,7 @@ static SQLITE_NOINLINE PgHdr1 *pcache1FetchStage2( ** ** then attempt to recycle a page from the LRU list. If it is the right ** size, return the recycled buffer. Otherwise, free the buffer and -** proceed to step 5. +** proceed to step 5. ** ** 5. Otherwise, allocate and return a new page buffer. ** @@ -954,8 +998,8 @@ static SQLITE_NOINLINE PgHdr1 *pcache1FetchStage2( ** invokes the appropriate routine. */ static PgHdr1 *pcache1FetchNoMutex( - sqlite3_pcache *p, - unsigned int iKey, + sqlite3_pcache *p, + unsigned int iKey, int createFlag ){ PCache1 *pCache = (PCache1 *)p; @@ -970,7 +1014,7 @@ static PgHdr1 *pcache1FetchNoMutex( ** Otherwise (page not in hash and createFlag!=0) continue with ** subsequent steps to try to create the page. */ if( pPage ){ - if( !pPage->isPinned ){ + if( PAGE_IS_UNPINNED(pPage) ){ return pcache1PinPage(pPage); }else{ return pPage; @@ -984,8 +1028,8 @@ static PgHdr1 *pcache1FetchNoMutex( } #if PCACHE1_MIGHT_USE_GROUP_MUTEX static PgHdr1 *pcache1FetchWithMutex( - sqlite3_pcache *p, - unsigned int iKey, + sqlite3_pcache *p, + unsigned int iKey, int createFlag ){ PCache1 *pCache = (PCache1 *)p; @@ -999,8 +1043,8 @@ static PgHdr1 *pcache1FetchWithMutex( } #endif static sqlite3_pcache_page *pcache1Fetch( - sqlite3_pcache *p, - unsigned int iKey, + sqlite3_pcache *p, + unsigned int iKey, int createFlag ){ #if PCACHE1_MIGHT_USE_GROUP_MUTEX || defined(SQLITE_DEBUG) @@ -1030,24 +1074,24 @@ static sqlite3_pcache_page *pcache1Fetch( ** Mark a page as unpinned (eligible for asynchronous recycling). */ static void pcache1Unpin( - sqlite3_pcache *p, - sqlite3_pcache_page *pPg, + sqlite3_pcache *p, + sqlite3_pcache_page *pPg, int reuseUnlikely ){ PCache1 *pCache = (PCache1 *)p; PgHdr1 *pPage = (PgHdr1 *)pPg; PGroup *pGroup = pCache->pGroup; - + assert( pPage->pCache==pCache ); pcache1EnterMutex(pGroup); - /* It is an error to call this function if the page is already + /* It is an error to call this function if the page is already ** part of the PGroup LRU list. */ - assert( pPage->pLruPrev==0 && pPage->pLruNext==0 ); - assert( pPage->isPinned==1 ); + assert( pPage->pLruNext==0 ); + assert( PAGE_IS_PINNED(pPage) ); - if( reuseUnlikely || pGroup->nCurrentPage>pGroup->nMaxPage ){ + if( reuseUnlikely || pGroup->nPurgeable>pGroup->nMaxPage ){ pcache1RemoveFromHash(pPage, 1); }else{ /* Add the page to the PGroup LRU list. */ @@ -1056,14 +1100,13 @@ static void pcache1Unpin( (pPage->pLruNext = *ppFirst)->pLruPrev = pPage; *ppFirst = pPage; pCache->nRecyclable++; - pPage->isPinned = 0; } pcache1LeaveMutex(pCache->pGroup); } /* -** Implementation of the sqlite3_pcache.xRekey method. +** Implementation of the sqlite3_pcache.xRekey method. */ static void pcache1Rekey( sqlite3_pcache *p, @@ -1074,23 +1117,26 @@ static void pcache1Rekey( PCache1 *pCache = (PCache1 *)p; PgHdr1 *pPage = (PgHdr1 *)pPg; PgHdr1 **pp; - unsigned int h; + unsigned int hOld, hNew; assert( pPage->iKey==iOld ); assert( pPage->pCache==pCache ); + assert( iOld!=iNew ); /* The page number really is changing */ pcache1EnterMutex(pCache->pGroup); - h = iOld%pCache->nHash; - pp = &pCache->apHash[h]; + assert( pcache1FetchNoMutex(p, iOld, 0)==pPage ); /* pPg really is iOld */ + hOld = iOld%pCache->nHash; + pp = &pCache->apHash[hOld]; while( (*pp)!=pPage ){ pp = &(*pp)->pNext; } *pp = pPage->pNext; - h = iNew%pCache->nHash; + assert( pcache1FetchNoMutex(p, iNew, 0)==0 ); /* iNew not in cache */ + hNew = iNew%pCache->nHash; pPage->iKey = iNew; - pPage->pNext = pCache->apHash[h]; - pCache->apHash[h] = pPage; + pPage->pNext = pCache->apHash[hNew]; + pCache->apHash[hNew] = pPage; if( iNew>pCache->iMaxKey ){ pCache->iMaxKey = iNew; } @@ -1099,7 +1145,7 @@ static void pcache1Rekey( } /* -** Implementation of the sqlite3_pcache.xTruncate method. +** Implementation of the sqlite3_pcache.xTruncate method. ** ** Discard all unpinned pages in the cache with a page number equal to ** or greater than parameter iLimit. Any pinned pages with a page number @@ -1116,7 +1162,7 @@ static void pcache1Truncate(sqlite3_pcache *p, unsigned int iLimit){ } /* -** Implementation of the sqlite3_pcache.xDestroy method. +** Implementation of the sqlite3_pcache.xDestroy method. ** ** Destroy a cache allocated using pcache1Create(). */ @@ -1125,7 +1171,7 @@ static void pcache1Destroy(sqlite3_pcache *p){ PGroup *pGroup = pCache->pGroup; assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) ); pcache1EnterMutex(pGroup); - pcache1TruncateUnsafe(pCache, 0); + if( pCache->nPage ) pcache1TruncateUnsafe(pCache, 0); assert( pGroup->nMaxPage >= pCache->nMax ); pGroup->nMaxPage -= pCache->nMax; assert( pGroup->nMinPage >= pCache->nMin ); @@ -1182,14 +1228,14 @@ sqlite3_mutex *sqlite3Pcache1Mutex(void){ ** by the current thread may be sqlite3_free()ed. ** ** nReq is the number of bytes of memory required. Once this much has -** been released, the function returns. The return value is the total number +** been released, the function returns. The return value is the total number ** of bytes of memory released. */ int sqlite3PcacheReleaseMemory(int nReq){ int nFree = 0; assert( sqlite3_mutex_notheld(pcache1.grp.mutex) ); assert( sqlite3_mutex_notheld(pcache1.mutex) ); - if( sqlite3GlobalConfig.nPage==0 ){ + if( sqlite3GlobalConfig.pPage==0 ){ PgHdr1 *p; pcache1EnterMutex(&pcache1.grp); while( (nReq<0 || nFreeisAnchor==0 ){ nFree += pcache1MemSize(p->page.pBuf); -#ifdef SQLITE_PCACHE_SEPARATE_HEADER - nFree += sqlite3MemSize(p); -#endif - assert( p->isPinned==0 ); + assert( PAGE_IS_UNPINNED(p) ); pcache1PinPage(p); pcache1RemoveFromHash(p, 1); } @@ -1224,10 +1267,10 @@ void sqlite3PcacheStats( PgHdr1 *p; int nRecyclable = 0; for(p=pcache1.grp.lru.pLruNext; p && !p->isAnchor; p=p->pLruNext){ - assert( p->isPinned==0 ); + assert( PAGE_IS_UNPINNED(p) ); nRecyclable++; } - *pnCurrent = pcache1.grp.nCurrentPage; + *pnCurrent = pcache1.grp.nPurgeable; *pnMax = (int)pcache1.grp.nMaxPage; *pnMin = (int)pcache1.grp.nMinPage; *pnRecyclable = nRecyclable; diff --git a/src/pragma.c b/src/pragma.c index 92658b5916..84b95fefb3 100644 --- a/src/pragma.c +++ b/src/pragma.c @@ -26,13 +26,41 @@ ** that includes the PragType_XXXX macro definitions and the aPragmaName[] ** object. This ensures that the aPragmaName[] table is arranged in ** lexicographical order to facility a binary search of the pragma name. -** Do not edit pragma.h directly. Edit and rerun the script in at +** Do not edit pragma.h directly. Edit and rerun the script in at ** ../tool/mkpragmatab.tcl. */ #include "pragma.h" +/* +** When the 0x10 bit of PRAGMA optimize is set, any ANALYZE commands +** will be run with an analysis_limit set to the lessor of the value of +** the following macro or to the actual analysis_limit if it is non-zero, +** in order to prevent PRAGMA optimize from running for too long. +** +** The value of 2000 is chosen empirically so that the worst-case run-time +** for PRAGMA optimize does not exceed 100 milliseconds against a variety +** of test databases on a RaspberryPI-4 compiled using -Os and without +** -DSQLITE_DEBUG. Of course, your mileage may vary. For the purpose of +** this paragraph, "worst-case" means that ANALYZE ends up being +** run on every table in the database. The worst case typically only +** happens if PRAGMA optimize is run on a database file for which ANALYZE +** has not been previously run and the 0x10000 flag is included so that +** all tables are analyzed. The usual case for PRAGMA optimize is that +** no ANALYZE commands will be run at all, or if any ANALYZE happens it +** will be against a single table, so that expected timing for PRAGMA +** optimize on a PI-4 is more like 1 millisecond or less with the 0x10000 +** flag or less than 100 microseconds without the 0x10000 flag. +** +** An analysis limit of 2000 is almost always sufficient for the query +** planner to fully characterize an index. The additional accuracy from +** a larger analysis is not usually helpful. +*/ +#ifndef SQLITE_DEFAULT_OPTIMIZE_LIMIT +# define SQLITE_DEFAULT_OPTIMIZE_LIMIT 2000 +#endif + /* ** Interpret the given string as a safety level. Return 0 for OFF, -** 1 for ON or NORMAL, 2 for FULL, and 3 for EXTRA. Return 1 for an empty or +** 1 for ON or NORMAL, 2 for FULL, and 3 for EXTRA. Return 1 for an empty or ** unrecognized string argument. The FULL and EXTRA option is disallowed ** if the omitFull parameter it 1. ** @@ -91,7 +119,7 @@ static int getLockingMode(const char *z){ /* ** Interpret the given string as an auto-vacuum mode value. ** -** The following strings, "none", "full" and "incremental" are +** The following strings, "none", "full" and "incremental" are ** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively. */ static int getAutoVacuum(const char *z){ @@ -131,7 +159,9 @@ static int getTempStore(const char *z){ static int invalidateTempStorage(Parse *pParse){ sqlite3 *db = pParse->db; if( db->aDb[1].pBt!=0 ){ - if( !db->autoCommit || sqlite3BtreeIsInReadTrans(db->aDb[1].pBt) ){ + if( !db->autoCommit + || sqlite3BtreeTxnState(db->aDb[1].pBt)!=SQLITE_TXN_NONE + ){ sqlite3ErrorMsg(pParse, "temporary storage cannot be changed " "from within a transaction"); return SQLITE_ERROR; @@ -163,29 +193,29 @@ static int changeTempStorage(Parse *pParse, const char *zStorageType){ #endif /* SQLITE_PAGER_PRAGMAS */ /* -** Set the names of the first N columns to the values in azCol[] +** Set result column names for a pragma. */ -static void setAllColumnNames( - Vdbe *v, /* The query under construction */ - int N, /* Number of columns */ - const char **azCol /* Names of columns */ +static void setPragmaResultColumnNames( + Vdbe *v, /* The query under construction */ + const PragmaName *pPragma /* The pragma */ ){ - int i; - sqlite3VdbeSetNumCols(v, N); - for(i=0; inPragCName; + sqlite3VdbeSetNumCols(v, n==0 ? 1 : n); + if( n==0 ){ + sqlite3VdbeSetColName(v, 0, COLNAME_NAME, pPragma->zName, SQLITE_STATIC); + }else{ + int i, j; + for(i=0, j=pPragma->iPragCName; iupr ? 0 : &aPragmaName[mid]; +} + +/* +** Create zero or more entries in the output for the SQL functions +** defined by FuncDef p. +*/ +static void pragmaFunclistLine( + Vdbe *v, /* The prepared statement being created */ + FuncDef *p, /* A particular function definition */ + int isBuiltin, /* True if this is a built-in function */ + int showInternFuncs /* True if showing internal functions */ +){ + u32 mask = + SQLITE_DETERMINISTIC | + SQLITE_DIRECTONLY | + SQLITE_SUBTYPE | + SQLITE_INNOCUOUS | + SQLITE_FUNC_INTERNAL + ; + if( showInternFuncs ) mask = 0xffffffff; + for(; p; p=p->pNext){ + const char *zType; + static const char *azEnc[] = { 0, "utf8", "utf16le", "utf16be" }; + + assert( SQLITE_FUNC_ENCMASK==0x3 ); + assert( strcmp(azEnc[SQLITE_UTF8],"utf8")==0 ); + assert( strcmp(azEnc[SQLITE_UTF16LE],"utf16le")==0 ); + assert( strcmp(azEnc[SQLITE_UTF16BE],"utf16be")==0 ); + + if( p->xSFunc==0 ) continue; + if( (p->funcFlags & SQLITE_FUNC_INTERNAL)!=0 + && showInternFuncs==0 + ){ + continue; + } + if( p->xValue!=0 ){ + zType = "w"; + }else if( p->xFinalize!=0 ){ + zType = "a"; + }else{ + zType = "s"; + } + sqlite3VdbeMultiLoad(v, 1, "sissii", + p->zName, isBuiltin, + zType, azEnc[p->funcFlags&SQLITE_FUNC_ENCMASK], + p->nArg, + (p->funcFlags & mask) ^ SQLITE_INNOCUOUS + ); + } +} + + +/* +** Helper subroutine for PRAGMA integrity_check: +** +** Generate code to output a single-column result row with a value of the +** string held in register 3. Decrement the result count in register 1 +** and halt if the maximum number of result rows have been issued. +*/ +static int integrityCheckResultRow(Vdbe *v){ + int addr; + sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1); + addr = sqlite3VdbeAddOp3(v, OP_IfPos, 1, sqlite3VdbeCurrentAddr(v)+2, 1); + VdbeCoverage(v); + sqlite3VdbeAddOp0(v, OP_Halt); + return addr; +} + +/* +** Should table pTab be skipped when doing an integrity_check? +** Return true or false. +** +** If pObjTab is not null, the return true if pTab matches pObjTab. +** +** If pObjTab is null, then return true only if pTab is an imposter table. +*/ +static int tableSkipIntegrityCheck(const Table *pTab, const Table *pObjTab){ + if( pObjTab ){ + return pTab!=pObjTab; + }else{ + return (pTab->tabFlags & TF_Imposter)!=0; + } +} + +/* +** Process a pragma statement. ** ** Pragmas are of this form: ** @@ -293,7 +423,7 @@ const char *sqlite3JournalModename(int eMode){ ** id and pId2 is any empty string. */ void sqlite3Pragma( - Parse *pParse, + Parse *pParse, Token *pId1, /* First part of [schema.]id field */ Token *pId2, /* Second part of [schema.]id field, or NULL */ Token *pValue, /* Token for , or NULL */ @@ -305,12 +435,11 @@ void sqlite3Pragma( Token *pId; /* Pointer to token */ char *aFcntl[4]; /* Argument to SQLITE_FCNTL_PRAGMA */ int iDb; /* Database index for */ - int lwr, upr, mid = 0; /* Binary search bounds */ int rc; /* return value form SQLITE_FCNTL_PRAGMA */ sqlite3 *db = pParse->db; /* The database connection */ Db *pDb; /* The specific database being pragmaed */ Vdbe *v = sqlite3GetVdbe(pParse); /* Prepared statement */ - const struct sPragmaNames *pPragma; + const PragmaName *pPragma; /* The pragma */ /* BEGIN SQLCIPHER */ #ifdef SQLITE_HAS_CODEC extern int sqlcipher_codec_pragma(sqlite3*, int, Parse *, const char *, const char *); @@ -327,8 +456,8 @@ void sqlite3Pragma( if( iDb<0 ) return; pDb = &db->aDb[iDb]; - /* If the temp database has been explicitly named as part of the - ** pragma, make sure it is open. + /* If the temp database has been explicitly named as part of the + ** pragma, make sure it is open. */ if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){ return; @@ -343,7 +472,7 @@ void sqlite3Pragma( } assert( pId2 ); - zDb = pId2->n>0 ? pDb->zName : 0; + zDb = pId2->n>0 ? pDb->zDbSName : 0; if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){ goto pragma_out; } @@ -370,7 +499,9 @@ void sqlite3Pragma( db->busyHandler.nBusy = 0; rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_PRAGMA, (void*)aFcntl); if( rc==SQLITE_OK ){ - returnSingleText(v, "result", aFcntl[0]); + sqlite3VdbeSetNumCols(v, 1); + sqlite3VdbeSetColName(v, 0, COLNAME_NAME, aFcntl[0], SQLITE_TRANSIENT); + returnSingleText(v, aFcntl[0]); sqlite3_free(aFcntl[0]); goto pragma_out; } @@ -395,29 +526,28 @@ void sqlite3Pragma( /* END SQLCIPHER */ /* Locate the pragma in the lookup table */ - lwr = 0; - upr = ArraySize(aPragmaNames)-1; - while( lwr<=upr ){ - mid = (lwr+upr)/2; - rc = sqlite3_stricmp(zLeft, aPragmaNames[mid].zName); - if( rc==0 ) break; - if( rc<0 ){ - upr = mid - 1; - }else{ - lwr = mid + 1; - } + pPragma = pragmaLocate(zLeft); + if( pPragma==0 ){ + /* IMP: R-43042-22504 No error messages are generated if an + ** unknown pragma is issued. */ + goto pragma_out; } - if( lwr>upr ) goto pragma_out; - pPragma = &aPragmaNames[mid]; /* Make sure the database schema is loaded if the pragma requires that */ - if( (pPragma->mPragFlag & PragFlag_NeedSchema)!=0 ){ + if( (pPragma->mPragFlg & PragFlg_NeedSchema)!=0 ){ if( sqlite3ReadSchema(pParse) ) goto pragma_out; } + /* Register the result column names for pragmas that return results */ + if( (pPragma->mPragFlg & PragFlg_NoColumns)==0 + && ((pPragma->mPragFlg & PragFlg_NoColumns1)==0 || zRight==0) + ){ + setPragmaResultColumnNames(v, pPragma); + } + /* Jump to the appropriate pragma handler */ switch( pPragma->ePragTyp ){ - + #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED) /* ** PRAGMA [schema.]default_cache_size @@ -451,7 +581,6 @@ void sqlite3Pragma( VdbeOp *aOp; sqlite3VdbeUsesBtree(v, iDb); if( !zRight ){ - setOneColumnName(v, "cache_size"); pParse->nMem += 2; sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(getCacheSize)); aOp = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize, iLn); @@ -486,13 +615,13 @@ void sqlite3Pragma( assert( pBt!=0 ); if( !zRight ){ int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0; - returnSingleInt(v, "page_size", size); + returnSingleInt(v, size); }else{ /* Malloc may fail when setting the page-size, as there is an internal ** buffer that the pager module resizes using sqlite3_realloc(). */ db->nextPagesize = sqlite3Atoi(zRight); - if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,-1,0) ){ + if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,0,0) ){ sqlite3OomFault(db); } } @@ -501,18 +630,22 @@ void sqlite3Pragma( /* ** PRAGMA [schema.]secure_delete - ** PRAGMA [schema.]secure_delete=ON/OFF + ** PRAGMA [schema.]secure_delete=ON/OFF/FAST ** ** The first form reports the current setting for the ** secure_delete flag. The second form changes the secure_delete - ** flag setting and reports thenew value. + ** flag setting and reports the new value. */ case PragTyp_SECURE_DELETE: { Btree *pBt = pDb->pBt; int b = -1; assert( pBt!=0 ); if( zRight ){ - b = sqlite3GetBoolean(zRight, 0); + if( sqlite3_stricmp(zRight, "fast")==0 ){ + b = 2; + }else{ + b = sqlite3GetBoolean(zRight, 0); + } } if( pId2->n==0 && b>=0 ){ int ii; @@ -521,7 +654,7 @@ void sqlite3Pragma( } } b = sqlite3BtreeSecureDelete(pBt, b); - returnSingleInt(v, "secure_delete", b); + returnSingleInt(v, b); break; } @@ -530,7 +663,7 @@ void sqlite3Pragma( ** PRAGMA [schema.]max_page_count=N ** ** The first form reports the current setting for the - ** maximum number of pages in the database file. The + ** maximum number of pages in the database file. The ** second form attempts to change this setting. Both ** forms return the current setting. ** @@ -544,17 +677,21 @@ void sqlite3Pragma( */ case PragTyp_PAGE_COUNT: { int iReg; + i64 x = 0; sqlite3CodeVerifySchema(pParse, iDb); iReg = ++pParse->nMem; if( sqlite3Tolower(zLeft[0])=='p' ){ sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg); }else{ - sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg, - sqlite3AbsInt32(sqlite3Atoi(zRight))); + if( zRight && sqlite3DecOrHexToI64(zRight,&x)==0 ){ + if( x<0 ) x = 0; + else if( x>0xfffffffe ) x = 0xfffffffe; + }else{ + x = 0; + } + sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg, (int)x); } sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1); - sqlite3VdbeSetNumCols(v, 1); - sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT); break; } @@ -600,7 +737,7 @@ void sqlite3Pragma( if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){ zRet = "exclusive"; } - returnSingleText(v, "locking_mode", zRet); + returnSingleText(v, zRet); break; } @@ -613,7 +750,6 @@ void sqlite3Pragma( int eMode; /* One of the PAGER_JOURNALMODE_XXX symbols */ int ii; /* Loop counter */ - setOneColumnName(v, "journal_mode"); if( zRight==0 ){ /* If there is no "=MODE" part of the pragma, do a query for the ** current mode */ @@ -629,6 +765,11 @@ void sqlite3Pragma( ** then do a query */ eMode = PAGER_JOURNALMODE_QUERY; } + if( eMode==PAGER_JOURNALMODE_OFF && (db->flags & SQLITE_Defensive)!=0 ){ + /* Do not allow journal-mode "OFF" in defensive since the database + ** can become corrupted using ordinary SQL when the journal is off */ + eMode = PAGER_JOURNALMODE_QUERY; + } } if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){ /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */ @@ -659,7 +800,7 @@ void sqlite3Pragma( if( iLimit<-1 ) iLimit = -1; } iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit); - returnSingleInt(v, "journal_size_limit", iLimit); + returnSingleInt(v, iLimit); break; } @@ -677,7 +818,7 @@ void sqlite3Pragma( Btree *pBt = pDb->pBt; assert( pBt!=0 ); if( !zRight ){ - returnSingleInt(v, "auto_vacuum", sqlite3BtreeGetAutoVacuum(pBt)); + returnSingleInt(v, sqlite3BtreeGetAutoVacuum(pBt)); }else{ int eAuto = getAutoVacuum(zRight); assert( eAuto>=0 && eAuto<=2 ); @@ -689,7 +830,7 @@ void sqlite3Pragma( */ rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto); if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){ - /* When setting the auto_vacuum mode to either "full" or + /* When setting the auto_vacuum mode to either "full" or ** "incremental", write the value of meta[6] in the database ** file. Before writing to meta[6], check that meta[3] indicates ** that this really is an auto-vacuum capable database. @@ -726,7 +867,7 @@ void sqlite3Pragma( */ #ifndef SQLITE_OMIT_AUTOVACUUM case PragTyp_INCREMENTAL_VACUUM: { - int iLimit, addr; + int iLimit = 0, addr; if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){ iLimit = 0x7fffffff; } @@ -756,7 +897,7 @@ void sqlite3Pragma( case PragTyp_CACHE_SIZE: { assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); if( !zRight ){ - returnSingleInt(v, "cache_size", pDb->pSchema->cache_size); + returnSingleInt(v, pDb->pSchema->cache_size); }else{ int size = sqlite3Atoi(zRight); pDb->pSchema->cache_size = size; @@ -772,7 +913,7 @@ void sqlite3Pragma( ** ** The first form reports the current local setting for the ** page cache spill size. The second form turns cache spill on - ** or off. When turnning cache spill on, the size is set to the + ** or off. When turning cache spill on, the size is set to the ** current cache_size. The third form sets a spill size that ** may be different form the cache size. ** If N is positive then that is the @@ -790,8 +931,8 @@ void sqlite3Pragma( case PragTyp_CACHE_SPILL: { assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); if( !zRight ){ - returnSingleInt(v, "cache_spill", - (db->flags & SQLITE_CacheSpill)==0 ? 0 : + returnSingleInt(v, + (db->flags & SQLITE_CacheSpill)==0 ? 0 : sqlite3BtreeSetSpillSize(pDb->pBt,0)); }else{ int size = 1; @@ -801,7 +942,7 @@ void sqlite3Pragma( if( sqlite3GetBoolean(zRight, size!=0) ){ db->flags |= SQLITE_CacheSpill; }else{ - db->flags &= ~SQLITE_CacheSpill; + db->flags &= ~(u64)SQLITE_CacheSpill; } setAllPagerFlags(db); } @@ -844,7 +985,7 @@ void sqlite3Pragma( rc = SQLITE_OK; #endif if( rc==SQLITE_OK ){ - returnSingleInt(v, "mmap_size", sz); + returnSingleInt(v, sz); }else if( rc!=SQLITE_NOTFOUND ){ pParse->nErr++; pParse->rc = rc; @@ -865,7 +1006,7 @@ void sqlite3Pragma( */ case PragTyp_TEMP_STORE: { if( !zRight ){ - returnSingleInt(v, "temp_store", db->temp_store); + returnSingleInt(v, db->temp_store); }else{ changeTempStorage(pParse, zRight); } @@ -883,8 +1024,9 @@ void sqlite3Pragma( ** */ case PragTyp_TEMP_STORE_DIRECTORY: { + sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR)); if( !zRight ){ - returnSingleText(v, "temp_store_directory", sqlite3_temp_directory); + returnSingleText(v, sqlite3_temp_directory); }else{ #ifndef SQLITE_OMIT_WSD if( zRight[0] ){ @@ -892,6 +1034,7 @@ void sqlite3Pragma( rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res); if( rc!=SQLITE_OK || res==0 ){ sqlite3ErrorMsg(pParse, "not a writable directory"); + sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR)); goto pragma_out; } } @@ -909,6 +1052,7 @@ void sqlite3Pragma( } #endif /* SQLITE_OMIT_WSD */ } + sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR)); break; } @@ -927,8 +1071,9 @@ void sqlite3Pragma( ** */ case PragTyp_DATA_STORE_DIRECTORY: { + sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR)); if( !zRight ){ - returnSingleText(v, "data_store_directory", sqlite3_data_directory); + returnSingleText(v, sqlite3_data_directory); }else{ #ifndef SQLITE_OMIT_WSD if( zRight[0] ){ @@ -936,6 +1081,7 @@ void sqlite3Pragma( rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res); if( rc!=SQLITE_OK || res==0 ){ sqlite3ErrorMsg(pParse, "not a writable directory"); + sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR)); goto pragma_out; } } @@ -947,6 +1093,7 @@ void sqlite3Pragma( } #endif /* SQLITE_OMIT_WSD */ } + sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR)); break; } #endif @@ -965,18 +1112,18 @@ void sqlite3Pragma( Pager *pPager = sqlite3BtreePager(pDb->pBt); char *proxy_file_path = NULL; sqlite3_file *pFile = sqlite3PagerFile(pPager); - sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE, + sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE, &proxy_file_path); - returnSingleText(v, "lock_proxy_file", proxy_file_path); + returnSingleText(v, proxy_file_path); }else{ Pager *pPager = sqlite3BtreePager(pDb->pBt); sqlite3_file *pFile = sqlite3PagerFile(pPager); int res; if( zRight[0] ){ - res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE, + res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE, zRight); } else { - res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE, + res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE, NULL); } if( res!=SQLITE_OK ){ @@ -986,8 +1133,8 @@ void sqlite3Pragma( } break; } -#endif /* SQLITE_ENABLE_LOCKING_STYLE */ - +#endif /* SQLITE_ENABLE_LOCKING_STYLE */ + /* ** PRAGMA [schema.]synchronous ** PRAGMA [schema.]synchronous=OFF|ON|NORMAL|FULL|EXTRA @@ -999,15 +1146,16 @@ void sqlite3Pragma( */ case PragTyp_SYNCHRONOUS: { if( !zRight ){ - returnSingleInt(v, "synchronous", pDb->safety_level-1); + returnSingleInt(v, pDb->safety_level-1); }else{ if( !db->autoCommit ){ - sqlite3ErrorMsg(pParse, + sqlite3ErrorMsg(pParse, "Safety level may not be changed inside a transaction"); - }else{ + }else if( iDb!=1 ){ int iLevel = (getSafetyLevel(zRight,0,1)+1) & PAGER_SYNCHRONOUS_MASK; if( iLevel==0 ) iLevel = 1; pDb->safety_level = iLevel; + pDb->bSyncSet = 1; setAllPagerFlags(db); } } @@ -1018,33 +1166,43 @@ void sqlite3Pragma( #ifndef SQLITE_OMIT_FLAG_PRAGMAS case PragTyp_FLAG: { if( zRight==0 ){ - returnSingleInt(v, pPragma->zName, (db->flags & pPragma->iArg)!=0 ); + setPragmaResultColumnNames(v, pPragma); + returnSingleInt(v, (db->flags & pPragma->iArg)!=0 ); }else{ - int mask = pPragma->iArg; /* Mask of bits to set or clear. */ + u64 mask = pPragma->iArg; /* Mask of bits to set or clear. */ if( db->autoCommit==0 ){ /* Foreign key support may not be enabled or disabled while not ** in auto-commit mode. */ mask &= ~(SQLITE_ForeignKeys); } -#if SQLITE_USER_AUTHENTICATION - if( db->auth.authLevel==UAUTH_User ){ - /* Do not allow non-admin users to modify the schema arbitrarily */ - mask &= ~(SQLITE_WriteSchema); - } -#endif if( sqlite3GetBoolean(zRight, 0) ){ - db->flags |= mask; + if( (mask & SQLITE_WriteSchema)==0 + || (db->flags & SQLITE_Defensive)==0 + ){ + db->flags |= mask; + } }else{ db->flags &= ~mask; - if( mask==SQLITE_DeferFKs ) db->nDeferredImmCons = 0; + if( mask==SQLITE_DeferFKs ){ + db->nDeferredImmCons = 0; + db->nDeferredCons = 0; + } + if( (mask & SQLITE_WriteSchema)!=0 + && sqlite3_stricmp(zRight, "reset")==0 + ){ + /* IMP: R-60817-01178 If the argument is "RESET" then schema + ** writing is disabled (as with "PRAGMA writable_schema=OFF") and, + ** in addition, the schema is reloaded. */ + sqlite3ResetAllSchemasOfConnection(db); + } } - /* Many of the flag-pragmas modify the code generated by the SQL + /* Many of the flag-pragmas modify the code generated by the SQL ** compiler (eg. count_changes). So add an opcode to expire all ** compiled SQL statements after modifying a pragma value. */ - sqlite3VdbeAddOp2(v, OP_Expire, 0, 0); + sqlite3VdbeAddOp0(v, OP_Expire); setAllPagerFlags(db); } break; @@ -1063,26 +1221,34 @@ void sqlite3Pragma( ** type: Column declaration type. ** notnull: True if 'NOT NULL' is part of column declaration ** dflt_value: The default value for the column, if any. + ** pk: Non-zero for PK fields. */ case PragTyp_TABLE_INFO: if( zRight ){ Table *pTab; - pTab = sqlite3FindTable(db, zRight, zDb); + sqlite3CodeVerifyNamedSchema(pParse, zDb); + pTab = sqlite3LocateTable(pParse, LOCATE_NOERR, zRight, zDb); if( pTab ){ - static const char *azCol[] = { - "cid", "name", "type", "notnull", "dflt_value", "pk" - }; int i, k; int nHidden = 0; Column *pCol; Index *pPk = sqlite3PrimaryKeyIndex(pTab); - pParse->nMem = 6; - sqlite3CodeVerifySchema(pParse, iDb); - setAllColumnNames(v, 6, azCol); assert( 6==ArraySize(azCol) ); + pParse->nMem = 7; sqlite3ViewGetColumnNames(pParse, pTab); for(i=0, pCol=pTab->aCol; inCol; i++, pCol++){ - if( IsHiddenColumn(pCol) ){ - nHidden++; - continue; + int isHidden = 0; + const Expr *pColExpr; + if( pCol->colFlags & COLFLAG_NOINSERT ){ + if( pPragma->iArg==0 ){ + nHidden++; + continue; + } + if( pCol->colFlags & COLFLAG_VIRTUAL ){ + isHidden = 2; /* GENERATED ALWAYS AS ... VIRTUAL */ + }else if( pCol->colFlags & COLFLAG_STORED ){ + isHidden = 3; /* GENERATED ALWAYS AS ... STORED */ + }else{ assert( pCol->colFlags & COLFLAG_HIDDEN ); + isHidden = 1; /* HIDDEN */ + } } if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){ k = 0; @@ -1091,54 +1257,145 @@ void sqlite3Pragma( }else{ for(k=1; k<=pTab->nCol && pPk->aiColumn[k-1]!=i; k++){} } - sqlite3VdbeMultiLoad(v, 1, "issisi", + pColExpr = sqlite3ColumnExpr(pTab,pCol); + assert( pColExpr==0 || pColExpr->op==TK_SPAN || isHidden>=2 ); + assert( pColExpr==0 || !ExprHasProperty(pColExpr, EP_IntValue) + || isHidden>=2 ); + sqlite3VdbeMultiLoad(v, 1, pPragma->iArg ? "issisii" : "issisi", i-nHidden, - pCol->zName, - pCol->zType ? pCol->zType : "", + pCol->zCnName, + sqlite3ColumnType(pCol,""), pCol->notNull ? 1 : 0, - pCol->zDflt, - k); - sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6); + (isHidden>=2 || pColExpr==0) ? 0 : pColExpr->u.zToken, + k, + isHidden); } } } break; + /* + ** PRAGMA table_list + ** + ** Return a single row for each table, virtual table, or view in the + ** entire schema. + ** + ** schema: Name of attached database hold this table + ** name: Name of the table itself + ** type: "table", "view", "virtual", "shadow" + ** ncol: Number of columns + ** wr: True for a WITHOUT ROWID table + ** strict: True for a STRICT table + */ + case PragTyp_TABLE_LIST: { + int ii; + pParse->nMem = 6; + sqlite3CodeVerifyNamedSchema(pParse, zDb); + for(ii=0; iinDb; ii++){ + HashElem *k; + Hash *pHash; + int initNCol; + if( zDb && sqlite3_stricmp(zDb, db->aDb[ii].zDbSName)!=0 ) continue; + + /* Ensure that the Table.nCol field is initialized for all views + ** and virtual tables. Each time we initialize a Table.nCol value + ** for a table, that can potentially disrupt the hash table, so restart + ** the initialization scan. + */ + pHash = &db->aDb[ii].pSchema->tblHash; + initNCol = sqliteHashCount(pHash); + while( initNCol-- ){ + for(k=sqliteHashFirst(pHash); 1; k=sqliteHashNext(k) ){ + Table *pTab; + if( k==0 ){ initNCol = 0; break; } + pTab = sqliteHashData(k); + if( pTab->nCol==0 ){ + char *zSql = sqlite3MPrintf(db, "SELECT*FROM\"%w\"", pTab->zName); + if( zSql ){ + sqlite3_stmt *pDummy = 0; + (void)sqlite3_prepare_v3(db, zSql, -1, SQLITE_PREPARE_DONT_LOG, + &pDummy, 0); + (void)sqlite3_finalize(pDummy); + sqlite3DbFree(db, zSql); + } + if( db->mallocFailed ){ + sqlite3ErrorMsg(db->pParse, "out of memory"); + db->pParse->rc = SQLITE_NOMEM_BKPT; + } + pHash = &db->aDb[ii].pSchema->tblHash; + break; + } + } + } + + for(k=sqliteHashFirst(pHash); k; k=sqliteHashNext(k) ){ + Table *pTab = sqliteHashData(k); + const char *zType; + if( zRight && sqlite3_stricmp(zRight, pTab->zName)!=0 ) continue; + if( IsView(pTab) ){ + zType = "view"; + }else if( IsVirtual(pTab) ){ + zType = "virtual"; + }else if( pTab->tabFlags & TF_Shadow ){ + zType = "shadow"; + }else{ + zType = "table"; + } + sqlite3VdbeMultiLoad(v, 1, "sssiii", + db->aDb[ii].zDbSName, + sqlite3PreferredTableName(pTab->zName), + zType, + pTab->nCol, + (pTab->tabFlags & TF_WithoutRowid)!=0, + (pTab->tabFlags & TF_Strict)!=0 + ); + } + } + } + break; + +#ifdef SQLITE_DEBUG case PragTyp_STATS: { - static const char *azCol[] = { "table", "index", "width", "height" }; Index *pIdx; HashElem *i; - v = sqlite3GetVdbe(pParse); - pParse->nMem = 4; + pParse->nMem = 5; sqlite3CodeVerifySchema(pParse, iDb); - setAllColumnNames(v, 4, azCol); assert( 4==ArraySize(azCol) ); for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){ Table *pTab = sqliteHashData(i); - sqlite3VdbeMultiLoad(v, 1, "ssii", - pTab->zName, + sqlite3VdbeMultiLoad(v, 1, "ssiii", + sqlite3PreferredTableName(pTab->zName), 0, - (int)sqlite3LogEstToInt(pTab->szTabRow), - (int)sqlite3LogEstToInt(pTab->nRowLogEst)); - sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4); + pTab->szTabRow, + pTab->nRowLogEst, + pTab->tabFlags); for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ - sqlite3VdbeMultiLoad(v, 2, "sii", + sqlite3VdbeMultiLoad(v, 2, "siiiX", pIdx->zName, - (int)sqlite3LogEstToInt(pIdx->szIdxRow), - (int)sqlite3LogEstToInt(pIdx->aiRowLogEst[0])); - sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4); + pIdx->szIdxRow, + pIdx->aiRowLogEst[0], + pIdx->hasStat1); + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 5); } } } break; +#endif case PragTyp_INDEX_INFO: if( zRight ){ Index *pIdx; Table *pTab; pIdx = sqlite3FindIndex(db, zRight, zDb); + if( pIdx==0 ){ + /* If there is no index named zRight, check to see if there is a + ** WITHOUT ROWID table named zRight, and if there is, show the + ** structure of the PRIMARY KEY index for that table. */ + pTab = sqlite3LocateTable(pParse, LOCATE_NOERR, zRight, zDb); + if( pTab && !HasRowid(pTab) ){ + pIdx = sqlite3PrimaryKeyIndex(pTab); + } + } if( pIdx ){ - static const char *azCol[] = { - "seqno", "cid", "name", "desc", "coll", "key" - }; + int iIdxDb = sqlite3SchemaToIndex(db, pIdx->pSchema); int i; int mx; if( pPragma->iArg ){ @@ -1151,15 +1408,14 @@ void sqlite3Pragma( pParse->nMem = 3; } pTab = pIdx->pTable; - sqlite3CodeVerifySchema(pParse, iDb); - assert( pParse->nMem<=ArraySize(azCol) ); - setAllColumnNames(v, pParse->nMem, azCol); + sqlite3CodeVerifySchema(pParse, iIdxDb); + assert( pParse->nMem<=pPragma->nPragCName ); for(i=0; iaiColumn[i]; - sqlite3VdbeMultiLoad(v, 1, "iis", i, cnum, - cnum<0 ? 0 : pTab->aCol[cnum].zName); + sqlite3VdbeMultiLoad(v, 1, "iisX", i, cnum, + cnum<0 ? 0 : pTab->aCol[cnum].zCnName); if( pPragma->iArg ){ - sqlite3VdbeMultiLoad(v, 4, "isi", + sqlite3VdbeMultiLoad(v, 4, "isiX", pIdx->aSortOrder[i], pIdx->azColl[i], inKeyCol); @@ -1176,13 +1432,9 @@ void sqlite3Pragma( int i; pTab = sqlite3FindTable(db, zRight, zDb); if( pTab ){ - static const char *azCol[] = { - "seq", "name", "unique", "origin", "partial" - }; - v = sqlite3GetVdbe(pParse); + int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema); pParse->nMem = 5; - sqlite3CodeVerifySchema(pParse, iDb); - setAllColumnNames(v, 5, azCol); assert( 5==ArraySize(azCol) ); + sqlite3CodeVerifySchema(pParse, iTabDb); for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){ const char *azOrigin[] = { "c", "u", "pk" }; sqlite3VdbeMultiLoad(v, 1, "isisi", @@ -1191,42 +1443,78 @@ void sqlite3Pragma( IsUniqueIndex(pIdx), azOrigin[pIdx->idxType], pIdx->pPartIdxWhere!=0); - sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 5); } } } break; case PragTyp_DATABASE_LIST: { - static const char *azCol[] = { "seq", "name", "file" }; int i; pParse->nMem = 3; - setAllColumnNames(v, 3, azCol); assert( 3==ArraySize(azCol) ); for(i=0; inDb; i++){ if( db->aDb[i].pBt==0 ) continue; - assert( db->aDb[i].zName!=0 ); + assert( db->aDb[i].zDbSName!=0 ); sqlite3VdbeMultiLoad(v, 1, "iss", i, - db->aDb[i].zName, + db->aDb[i].zDbSName, sqlite3BtreeGetFilename(db->aDb[i].pBt)); - sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3); } } break; case PragTyp_COLLATION_LIST: { - static const char *azCol[] = { "seq", "name" }; int i = 0; HashElem *p; pParse->nMem = 2; - setAllColumnNames(v, 2, azCol); assert( 2==ArraySize(azCol) ); for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){ CollSeq *pColl = (CollSeq *)sqliteHashData(p); sqlite3VdbeMultiLoad(v, 1, "is", i++, pColl->zName); - sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2); } } break; + +#ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS + case PragTyp_FUNCTION_LIST: { + int i; + HashElem *j; + FuncDef *p; + int showInternFunc = (db->mDbFlags & DBFLAG_InternalFunc)!=0; + pParse->nMem = 6; + for(i=0; iu.pHash ){ + assert( p->funcFlags & SQLITE_FUNC_BUILTIN ); + pragmaFunclistLine(v, p, 1, showInternFunc); + } + } + for(j=sqliteHashFirst(&db->aFunc); j; j=sqliteHashNext(j)){ + p = (FuncDef*)sqliteHashData(j); + assert( (p->funcFlags & SQLITE_FUNC_BUILTIN)==0 ); + pragmaFunclistLine(v, p, 0, showInternFunc); + } + } + break; + +#ifndef SQLITE_OMIT_VIRTUALTABLE + case PragTyp_MODULE_LIST: { + HashElem *j; + pParse->nMem = 1; + for(j=sqliteHashFirst(&db->aModule); j; j=sqliteHashNext(j)){ + Module *pMod = (Module*)sqliteHashData(j); + sqlite3VdbeMultiLoad(v, 1, "s", pMod->zName); + } + } + break; +#endif /* SQLITE_OMIT_VIRTUALTABLE */ + + case PragTyp_PRAGMA_LIST: { + int i; + for(i=0; ipFKey; + if( pTab && IsOrdinaryTable(pTab) ){ + pFK = pTab->u.tab.pFKey; if( pFK ){ - static const char *azCol[] = { - "id", "seq", "table", "from", "to", "on_update", "on_delete", - "match" - }; - int i = 0; + int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema); + int i = 0; pParse->nMem = 8; - sqlite3CodeVerifySchema(pParse, iDb); - setAllColumnNames(v, 8, azCol); assert( 8==ArraySize(azCol) ); + sqlite3CodeVerifySchema(pParse, iTabDb); while(pFK){ int j; for(j=0; jnCol; j++){ @@ -1253,12 +1536,11 @@ void sqlite3Pragma( i, j, pFK->zTo, - pTab->aCol[pFK->aCol[j].iFrom].zName, + pTab->aCol[pFK->aCol[j].iFrom].zCnName, pFK->aCol[j].zCol, actionName(pFK->aAction[1]), /* ON UPDATE */ actionName(pFK->aAction[0]), /* ON DELETE */ "NONE"); - sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 8); } ++i; pFK = pFK->pNextFrom; @@ -1281,20 +1563,14 @@ void sqlite3Pragma( HashElem *k; /* Loop counter: Next table in schema */ int x; /* result variable */ int regResult; /* 3 registers to hold a result row */ - int regKey; /* Register to hold key for checking the FK */ int regRow; /* Registers to hold a row from pTab */ int addrTop; /* Top of a loop checking foreign keys */ int addrOk; /* Jump here if the key is OK */ int *aiCols; /* child to parent column mapping */ - static const char *azCol[] = { "table", "rowid", "parent", "fkid" }; regResult = pParse->nMem+1; pParse->nMem += 4; - regKey = ++pParse->nMem; regRow = ++pParse->nMem; - v = sqlite3GetVdbe(pParse); - setAllColumnNames(v, 4, azCol); assert( 4==ArraySize(azCol) ); - sqlite3CodeVerifySchema(pParse, iDb); k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash); while( k ){ if( zRight ){ @@ -1304,12 +1580,16 @@ void sqlite3Pragma( pTab = (Table*)sqliteHashData(k); k = sqliteHashNext(k); } - if( pTab==0 || pTab->pFKey==0 ) continue; + if( pTab==0 || !IsOrdinaryTable(pTab) || pTab->u.tab.pFKey==0 ) continue; + iDb = sqlite3SchemaToIndex(db, pTab->pSchema); + zDb = db->aDb[iDb].zDbSName; + sqlite3CodeVerifySchema(pParse, iDb); sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); - if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow; + sqlite3TouchRegister(pParse, pTab->nCol+regRow); sqlite3OpenTable(pParse, 0, iDb, pTab, OP_OpenRead); sqlite3VdbeLoadString(v, regResult, pTab->zName); - for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){ + assert( IsOrdinaryTable(pTab) ); + for(i=1, pFK=pTab->u.tab.pFKey; pFK; i++, pFK=pFK->pNextFrom){ pParent = sqlite3FindTable(db, pFK->zTo, zDb); if( pParent==0 ) continue; pIdx = 0; @@ -1331,45 +1611,49 @@ void sqlite3Pragma( if( pFK ) break; if( pParse->nTabnTab = i; addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, 0); VdbeCoverage(v); - for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){ + assert( IsOrdinaryTable(pTab) ); + for(i=1, pFK=pTab->u.tab.pFKey; pFK; i++, pFK=pFK->pNextFrom){ pParent = sqlite3FindTable(db, pFK->zTo, zDb); pIdx = 0; aiCols = 0; if( pParent ){ x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, &aiCols); - assert( x==0 ); + assert( x==0 || db->mallocFailed ); } - addrOk = sqlite3VdbeMakeLabel(v); - if( pParent && pIdx==0 ){ - int iKey = pFK->aCol[0].iFrom; - assert( iKey>=0 && iKeynCol ); - if( iKey!=pTab->iPKey ){ - sqlite3VdbeAddOp3(v, OP_Column, 0, iKey, regRow); - sqlite3ColumnDefault(v, pTab, iKey, regRow); - sqlite3VdbeAddOp2(v, OP_IsNull, regRow, addrOk); VdbeCoverage(v); - sqlite3VdbeAddOp2(v, OP_MustBeInt, regRow, - sqlite3VdbeCurrentAddr(v)+3); VdbeCoverage(v); - }else{ - sqlite3VdbeAddOp2(v, OP_Rowid, 0, regRow); - } - sqlite3VdbeAddOp3(v, OP_NotExists, i, 0, regRow); VdbeCoverage(v); + addrOk = sqlite3VdbeMakeLabel(pParse); + + /* Generate code to read the child key values into registers + ** regRow..regRow+n. If any of the child key values are NULL, this + ** row cannot cause an FK violation. Jump directly to addrOk in + ** this case. */ + sqlite3TouchRegister(pParse, regRow + pFK->nCol); + for(j=0; jnCol; j++){ + int iCol = aiCols ? aiCols[j] : pFK->aCol[j].iFrom; + sqlite3ExprCodeGetColumnOfTable(v, pTab, 0, iCol, regRow+j); + sqlite3VdbeAddOp2(v, OP_IsNull, regRow+j, addrOk); VdbeCoverage(v); + } + + /* Generate code to query the parent index for a matching parent + ** key. If a match is found, jump to addrOk. */ + if( pIdx ){ + sqlite3VdbeAddOp4(v, OP_Affinity, regRow, pFK->nCol, 0, + sqlite3IndexAffinityStr(db,pIdx), pFK->nCol); + sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regRow, pFK->nCol); + VdbeCoverage(v); + }else if( pParent ){ + int jmp = sqlite3VdbeCurrentAddr(v)+2; + sqlite3VdbeAddOp3(v, OP_SeekRowid, i, jmp, regRow); VdbeCoverage(v); sqlite3VdbeGoto(v, addrOk); - sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2); + assert( pFK->nCol==1 || db->mallocFailed ); + } + + /* Generate code to report an FK violation to the caller. */ + if( HasRowid(pTab) ){ + sqlite3VdbeAddOp2(v, OP_Rowid, 0, regResult+1); }else{ - for(j=0; jnCol; j++){ - sqlite3ExprCodeGetColumnOfTable(v, pTab, 0, - aiCols ? aiCols[j] : pFK->aCol[j].iFrom, regRow+j); - sqlite3VdbeAddOp2(v, OP_IsNull, regRow+j, addrOk); VdbeCoverage(v); - } - if( pParent ){ - sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, pFK->nCol, regKey, - sqlite3IndexAffinityStr(db,pIdx), pFK->nCol); - sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regKey, 0); - VdbeCoverage(v); - } + sqlite3VdbeAddOp2(v, OP_Null, 0, regResult+1); } - sqlite3VdbeAddOp2(v, OP_Rowid, 0, regResult+1); - sqlite3VdbeMultiLoad(v, regResult+2, "si", pFK->zTo, i-1); + sqlite3VdbeMultiLoad(v, regResult+2, "siX", pFK->zTo, i-1); sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, 4); sqlite3VdbeResolveLabel(v, addrOk); sqlite3DbFree(db, aiCols); @@ -1382,19 +1666,7 @@ void sqlite3Pragma( #endif /* !defined(SQLITE_OMIT_TRIGGER) */ #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */ -#ifndef NDEBUG - case PragTyp_PARSER_TRACE: { - if( zRight ){ - if( sqlite3GetBoolean(zRight, 0) ){ - sqlite3ParserTrace(stdout, "parser: "); - }else{ - sqlite3ParserTrace(0, 0); - } - } - } - break; -#endif - +#ifndef SQLITE_OMIT_CASE_SENSITIVE_LIKE_PRAGMA /* Reinstall the LIKE and GLOB functions. The variant of LIKE ** used will be case sensitive or not depending on the RHS. */ @@ -1404,18 +1676,40 @@ void sqlite3Pragma( } } break; +#endif /* SQLITE_OMIT_CASE_SENSITIVE_LIKE_PRAGMA */ #ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX # define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100 #endif #ifndef SQLITE_OMIT_INTEGRITY_CHECK - /* Pragma "quick_check" is reduced version of + /* PRAGMA integrity_check + ** PRAGMA integrity_check(N) + ** PRAGMA quick_check + ** PRAGMA quick_check(N) + ** + ** Verify the integrity of the database. + ** + ** The "quick_check" is reduced version of ** integrity_check designed to detect most database corruption - ** without most of the overhead of a full integrity-check. + ** without the overhead of cross-checking indexes. Quick_check + ** is linear time whereas integrity_check is O(NlogN). + ** + ** The maximum number of errors is 100 by default. A different default + ** can be specified using a numeric parameter N. + ** + ** Or, the parameter N can be the name of a table. In that case, only + ** the one table named is verified. The freelist is only verified if + ** the named table is "sqlite_schema" (or one of its aliases). + ** + ** All schemas are checked by default. To check just a single + ** schema, use the form: + ** + ** PRAGMA schema.integrity_check; */ case PragTyp_INTEGRITY_CHECK: { int i, j, addr, mxErr; + Table *pObjTab = 0; /* Check only this one table, if not NULL */ int isQuick = (sqlite3Tolower(zLeft[0])=='q'); @@ -1434,201 +1728,495 @@ void sqlite3Pragma( /* Initialize the VDBE program */ pParse->nMem = 6; - setOneColumnName(v, "integrity_check"); /* Set the maximum error count */ mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX; if( zRight ){ - sqlite3GetInt32(zRight, &mxErr); - if( mxErr<=0 ){ - mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX; + if( sqlite3GetInt32(pValue->z, &mxErr) ){ + if( mxErr<=0 ){ + mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX; + } + }else{ + pObjTab = sqlite3LocateTable(pParse, 0, zRight, + iDb>=0 ? db->aDb[iDb].zDbSName : 0); } } - sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1); /* reg[1] holds errors left */ + sqlite3VdbeAddOp2(v, OP_Integer, mxErr-1, 1); /* reg[1] holds errors left */ /* Do an integrity check on each database file */ for(i=0; inDb; i++){ - HashElem *x; - Hash *pTbls; - int cnt = 0; + HashElem *x; /* For looping over tables in the schema */ + Hash *pTbls; /* Set of all tables in the schema */ + int *aRoot; /* Array of root page numbers of all btrees */ + int cnt = 0; /* Number of entries in aRoot[] */ if( OMIT_TEMPDB && i==1 ) continue; if( iDb>=0 && i!=iDb ) continue; sqlite3CodeVerifySchema(pParse, i); - addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */ - VdbeCoverage(v); - sqlite3VdbeAddOp2(v, OP_Halt, 0, 0); - sqlite3VdbeJumpHere(v, addr); + pParse->okConstFactor = 0; /* tag-20230327-1 */ /* Do an integrity check of the B-Tree ** - ** Begin by filling registers 2, 3, ... with the root pages numbers + ** Begin by finding the root pages numbers ** for all tables and indices in the database. */ assert( sqlite3SchemaMutexHeld(db, i, 0) ); pTbls = &db->aDb[i].pSchema->tblHash; + for(cnt=0, x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){ + Table *pTab = sqliteHashData(x); /* Current table */ + Index *pIdx; /* An index on pTab */ + int nIdx; /* Number of indexes on pTab */ + if( tableSkipIntegrityCheck(pTab,pObjTab) ) continue; + if( HasRowid(pTab) ) cnt++; + for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){ cnt++; } + } + if( cnt==0 ) continue; + if( pObjTab ) cnt++; + aRoot = sqlite3DbMallocRawNN(db, sizeof(int)*(cnt+1)); + if( aRoot==0 ) break; + cnt = 0; + if( pObjTab ) aRoot[++cnt] = 0; for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){ Table *pTab = sqliteHashData(x); Index *pIdx; - if( HasRowid(pTab) ){ - sqlite3VdbeAddOp2(v, OP_Integer, pTab->tnum, 2+cnt); - VdbeComment((v, "%s", pTab->zName)); - cnt++; - } + if( tableSkipIntegrityCheck(pTab,pObjTab) ) continue; + if( HasRowid(pTab) ) aRoot[++cnt] = pTab->tnum; for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ - sqlite3VdbeAddOp2(v, OP_Integer, pIdx->tnum, 2+cnt); - VdbeComment((v, "%s", pIdx->zName)); - cnt++; + aRoot[++cnt] = pIdx->tnum; } } + aRoot[0] = cnt; /* Make sure sufficient number of registers have been allocated */ - pParse->nMem = MAX( pParse->nMem, cnt+8 ); + sqlite3TouchRegister(pParse, 8+cnt); + sqlite3VdbeAddOp3(v, OP_Null, 0, 8, 8+cnt); + sqlite3ClearTempRegCache(pParse); /* Do the b-tree integrity checks */ - sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1); - sqlite3VdbeChangeP5(v, (u8)i); + sqlite3VdbeAddOp4(v, OP_IntegrityCk, 1, cnt, 8, (char*)aRoot,P4_INTARRAY); + sqlite3VdbeChangeP5(v, (u16)i); addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2); VdbeCoverage(v); sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, - sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName), + sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zDbSName), P4_DYNAMIC); - sqlite3VdbeAddOp3(v, OP_Move, 2, 4, 1); - sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2); - sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1); + sqlite3VdbeAddOp3(v, OP_Concat, 2, 3, 3); + integrityCheckResultRow(v); sqlite3VdbeJumpHere(v, addr); + /* Check that the indexes all have the right number of rows */ + cnt = pObjTab ? 1 : 0; + sqlite3VdbeLoadString(v, 2, "wrong # of entries in index "); + for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){ + int iTab = 0; + Table *pTab = sqliteHashData(x); + Index *pIdx; + if( tableSkipIntegrityCheck(pTab,pObjTab) ) continue; + if( HasRowid(pTab) ){ + iTab = cnt++; + }else{ + iTab = cnt; + for(pIdx=pTab->pIndex; ALWAYS(pIdx); pIdx=pIdx->pNext){ + if( IsPrimaryKeyIndex(pIdx) ) break; + iTab++; + } + } + for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ + if( pIdx->pPartIdxWhere==0 ){ + addr = sqlite3VdbeAddOp3(v, OP_Eq, 8+cnt, 0, 8+iTab); + VdbeCoverageNeverNull(v); + sqlite3VdbeLoadString(v, 4, pIdx->zName); + sqlite3VdbeAddOp3(v, OP_Concat, 4, 2, 3); + integrityCheckResultRow(v); + sqlite3VdbeJumpHere(v, addr); + } + cnt++; + } + } + /* Make sure all the indices are constructed correctly. */ - for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){ + for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){ Table *pTab = sqliteHashData(x); Index *pIdx, *pPk; - Index *pPrior = 0; + Index *pPrior = 0; /* Previous index */ int loopTop; int iDataCur, iIdxCur; int r1 = -1; - - if( pTab->pIndex==0 ) continue; - pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab); - addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Stop if out of errors */ - VdbeCoverage(v); - sqlite3VdbeAddOp2(v, OP_Halt, 0, 0); - sqlite3VdbeJumpHere(v, addr); - sqlite3ExprCacheClear(pParse); + int bStrict; /* True for a STRICT table */ + int r2; /* Previous key for WITHOUT ROWID tables */ + int mxCol; /* Maximum non-virtual column number */ + + if( tableSkipIntegrityCheck(pTab,pObjTab) ) continue; + if( !IsOrdinaryTable(pTab) ) continue; + if( isQuick || HasRowid(pTab) ){ + pPk = 0; + r2 = 0; + }else{ + pPk = sqlite3PrimaryKeyIndex(pTab); + r2 = sqlite3GetTempRange(pParse, pPk->nKeyCol); + sqlite3VdbeAddOp3(v, OP_Null, 1, r2, r2+pPk->nKeyCol-1); + } sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead, 0, 1, 0, &iDataCur, &iIdxCur); + /* reg[7] counts the number of entries in the table. + ** reg[8+i] counts the number of entries in the i-th index + */ sqlite3VdbeAddOp2(v, OP_Integer, 0, 7); for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */ } - pParse->nMem = MAX(pParse->nMem, 8+j); + assert( pParse->nMem>=8+j ); + assert( sqlite3NoTempsInRange(pParse,1,7+j) ); sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v); loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1); - /* Verify that all NOT NULL columns really are NOT NULL */ + + /* Fetch the right-most column from the table. This will cause + ** the entire record header to be parsed and sanity checked. It + ** will also prepopulate the cursor column cache that is used + ** by the OP_IsType code, so it is a required step. + */ + assert( !IsVirtual(pTab) ); + if( HasRowid(pTab) ){ + mxCol = -1; + for(j=0; jnCol; j++){ + if( (pTab->aCol[j].colFlags & COLFLAG_VIRTUAL)==0 ) mxCol++; + } + if( mxCol==pTab->iPKey ) mxCol--; + }else{ + /* COLFLAG_VIRTUAL columns are not included in the WITHOUT ROWID + ** PK index column-count, so there is no need to account for them + ** in this case. */ + mxCol = sqlite3PrimaryKeyIndex(pTab)->nColumn-1; + } + if( mxCol>=0 ){ + sqlite3VdbeAddOp3(v, OP_Column, iDataCur, mxCol, 3); + sqlite3VdbeTypeofColumn(v, 3); + } + + if( !isQuick ){ + if( pPk ){ + /* Verify WITHOUT ROWID keys are in ascending order */ + int a1; + char *zErr; + a1 = sqlite3VdbeAddOp4Int(v, OP_IdxGT, iDataCur, 0,r2,pPk->nKeyCol); + VdbeCoverage(v); + sqlite3VdbeAddOp1(v, OP_IsNull, r2); VdbeCoverage(v); + zErr = sqlite3MPrintf(db, + "row not in PRIMARY KEY order for %s", + pTab->zName); + sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC); + integrityCheckResultRow(v); + sqlite3VdbeJumpHere(v, a1); + sqlite3VdbeJumpHere(v, a1+1); + for(j=0; jnKeyCol; j++){ + sqlite3ExprCodeLoadIndexColumn(pParse, pPk, iDataCur, j, r2+j); + } + } + } + /* Verify datatypes for all columns: + ** + ** (1) NOT NULL columns may not contain a NULL + ** (2) Datatype must be exact for non-ANY columns in STRICT tables + ** (3) Datatype for TEXT columns in non-STRICT tables must be + ** NULL, TEXT, or BLOB. + ** (4) Datatype for numeric columns in non-STRICT tables must not + ** be a TEXT value that can be losslessly converted to numeric. + */ + bStrict = (pTab->tabFlags & TF_Strict)!=0; for(j=0; jnCol; j++){ char *zErr; - int jmp2, jmp3; + Column *pCol = pTab->aCol + j; /* The column to be checked */ + int labelError; /* Jump here to report an error */ + int labelOk; /* Jump here if all looks ok */ + int p1, p3, p4; /* Operands to the OP_IsType opcode */ + int doTypeCheck; /* Check datatypes (besides NOT NULL) */ + if( j==pTab->iPKey ) continue; - if( pTab->aCol[j].notNull==0 ) continue; - sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3); - sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG); - jmp2 = sqlite3VdbeAddOp1(v, OP_NotNull, 3); VdbeCoverage(v); - sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */ - zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName, - pTab->aCol[j].zName); - sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC); - sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1); - jmp3 = sqlite3VdbeAddOp1(v, OP_IfPos, 1); VdbeCoverage(v); - sqlite3VdbeAddOp0(v, OP_Halt); - sqlite3VdbeJumpHere(v, jmp2); - sqlite3VdbeJumpHere(v, jmp3); + if( bStrict ){ + doTypeCheck = pCol->eCType>COLTYPE_ANY; + }else{ + doTypeCheck = pCol->affinity>SQLITE_AFF_BLOB; + } + if( pCol->notNull==0 && !doTypeCheck ) continue; + + /* Compute the operands that will be needed for OP_IsType */ + p4 = SQLITE_NULL; + if( pCol->colFlags & COLFLAG_VIRTUAL ){ + sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3); + p1 = -1; + p3 = 3; + }else{ + if( pCol->iDflt ){ + sqlite3_value *pDfltValue = 0; + sqlite3ValueFromExpr(db, sqlite3ColumnExpr(pTab,pCol), ENC(db), + pCol->affinity, &pDfltValue); + if( pDfltValue ){ + p4 = sqlite3_value_type(pDfltValue); + sqlite3ValueFree(pDfltValue); + } + } + p1 = iDataCur; + if( !HasRowid(pTab) ){ + testcase( j!=sqlite3TableColumnToStorage(pTab, j) ); + p3 = sqlite3TableColumnToIndex(sqlite3PrimaryKeyIndex(pTab), j); + }else{ + p3 = sqlite3TableColumnToStorage(pTab,j); + testcase( p3!=j); + } + } + + labelError = sqlite3VdbeMakeLabel(pParse); + labelOk = sqlite3VdbeMakeLabel(pParse); + if( pCol->notNull ){ + /* (1) NOT NULL columns may not contain a NULL */ + int jmp3; + int jmp2 = sqlite3VdbeAddOp4Int(v, OP_IsType, p1, labelOk, p3, p4); + VdbeCoverage(v); + if( p1<0 ){ + sqlite3VdbeChangeP5(v, 0x0f); /* INT, REAL, TEXT, or BLOB */ + jmp3 = jmp2; + }else{ + sqlite3VdbeChangeP5(v, 0x0d); /* INT, TEXT, or BLOB */ + /* OP_IsType does not detect NaN values in the database file + ** which should be treated as a NULL. So if the header type + ** is REAL, we have to load the actual data using OP_Column + ** to reliably determine if the value is a NULL. */ + sqlite3VdbeAddOp3(v, OP_Column, p1, p3, 3); + sqlite3ColumnDefault(v, pTab, j, 3); + jmp3 = sqlite3VdbeAddOp2(v, OP_NotNull, 3, labelOk); + VdbeCoverage(v); + } + zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName, + pCol->zCnName); + sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC); + if( doTypeCheck ){ + sqlite3VdbeGoto(v, labelError); + sqlite3VdbeJumpHere(v, jmp2); + sqlite3VdbeJumpHere(v, jmp3); + }else{ + /* VDBE byte code will fall thru */ + } + } + if( bStrict && doTypeCheck ){ + /* (2) Datatype must be exact for non-ANY columns in STRICT tables*/ + static unsigned char aStdTypeMask[] = { + 0x1f, /* ANY */ + 0x18, /* BLOB */ + 0x11, /* INT */ + 0x11, /* INTEGER */ + 0x13, /* REAL */ + 0x14 /* TEXT */ + }; + sqlite3VdbeAddOp4Int(v, OP_IsType, p1, labelOk, p3, p4); + assert( pCol->eCType>=1 && pCol->eCType<=sizeof(aStdTypeMask) ); + sqlite3VdbeChangeP5(v, aStdTypeMask[pCol->eCType-1]); + VdbeCoverage(v); + zErr = sqlite3MPrintf(db, "non-%s value in %s.%s", + sqlite3StdType[pCol->eCType-1], + pTab->zName, pTab->aCol[j].zCnName); + sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC); + }else if( !bStrict && pCol->affinity==SQLITE_AFF_TEXT ){ + /* (3) Datatype for TEXT columns in non-STRICT tables must be + ** NULL, TEXT, or BLOB. */ + sqlite3VdbeAddOp4Int(v, OP_IsType, p1, labelOk, p3, p4); + sqlite3VdbeChangeP5(v, 0x1c); /* NULL, TEXT, or BLOB */ + VdbeCoverage(v); + zErr = sqlite3MPrintf(db, "NUMERIC value in %s.%s", + pTab->zName, pTab->aCol[j].zCnName); + sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC); + }else if( !bStrict && pCol->affinity>=SQLITE_AFF_NUMERIC ){ + /* (4) Datatype for numeric columns in non-STRICT tables must not + ** be a TEXT value that can be converted to numeric. */ + sqlite3VdbeAddOp4Int(v, OP_IsType, p1, labelOk, p3, p4); + sqlite3VdbeChangeP5(v, 0x1b); /* NULL, INT, FLOAT, or BLOB */ + VdbeCoverage(v); + if( p1>=0 ){ + sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3); + } + sqlite3VdbeAddOp4(v, OP_Affinity, 3, 1, 0, "C", P4_STATIC); + sqlite3VdbeAddOp4Int(v, OP_IsType, -1, labelOk, 3, p4); + sqlite3VdbeChangeP5(v, 0x1c); /* NULL, TEXT, or BLOB */ + VdbeCoverage(v); + zErr = sqlite3MPrintf(db, "TEXT value in %s.%s", + pTab->zName, pTab->aCol[j].zCnName); + sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC); + } + sqlite3VdbeResolveLabel(v, labelError); + integrityCheckResultRow(v); + sqlite3VdbeResolveLabel(v, labelOk); } - /* Validate index entries for the current row */ - for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ - int jmp2, jmp3, jmp4, jmp5; - int ckUniq = sqlite3VdbeMakeLabel(v); - if( pPk==pIdx ) continue; - r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3, - pPrior, r1); - pPrior = pIdx; - sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1); /* increment entry count */ - /* Verify that an index entry exists for the current table row */ - jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, ckUniq, r1, - pIdx->nColumn); VdbeCoverage(v); - sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */ - sqlite3VdbeLoadString(v, 3, "row "); - sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3); - sqlite3VdbeLoadString(v, 4, " missing from index "); - sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3); - jmp5 = sqlite3VdbeLoadString(v, 4, pIdx->zName); - sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3); - sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1); - jmp4 = sqlite3VdbeAddOp1(v, OP_IfPos, 1); VdbeCoverage(v); - sqlite3VdbeAddOp0(v, OP_Halt); - sqlite3VdbeJumpHere(v, jmp2); - /* For UNIQUE indexes, verify that only one entry exists with the - ** current key. The entry is unique if (1) any column is NULL - ** or (2) the next entry has a different key */ - if( IsUniqueIndex(pIdx) ){ - int uniqOk = sqlite3VdbeMakeLabel(v); - int jmp6; + /* Verify CHECK constraints */ + if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){ + ExprList *pCheck = sqlite3ExprListDup(db, pTab->pCheck, 0); + if( db->mallocFailed==0 ){ + int addrCkFault = sqlite3VdbeMakeLabel(pParse); + int addrCkOk = sqlite3VdbeMakeLabel(pParse); + char *zErr; + int k; + pParse->iSelfTab = iDataCur + 1; + for(k=pCheck->nExpr-1; k>0; k--){ + sqlite3ExprIfFalse(pParse, pCheck->a[k].pExpr, addrCkFault, 0); + } + sqlite3ExprIfTrue(pParse, pCheck->a[0].pExpr, addrCkOk, + SQLITE_JUMPIFNULL); + sqlite3VdbeResolveLabel(v, addrCkFault); + pParse->iSelfTab = 0; + zErr = sqlite3MPrintf(db, "CHECK constraint failed in %s", + pTab->zName); + sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC); + integrityCheckResultRow(v); + sqlite3VdbeResolveLabel(v, addrCkOk); + } + sqlite3ExprListDelete(db, pCheck); + } + if( !isQuick ){ /* Omit the remaining tests for quick_check */ + /* Validate index entries for the current row */ + for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ + int jmp2, jmp3, jmp4, jmp5, label6; int kk; + int ckUniq = sqlite3VdbeMakeLabel(pParse); + if( pPk==pIdx ) continue; + r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3, + pPrior, r1); + pPrior = pIdx; + sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1);/* increment entry count */ + /* Verify that an index entry exists for the current table row */ + jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, ckUniq, r1, + pIdx->nColumn); VdbeCoverage(v); + sqlite3VdbeLoadString(v, 3, "row "); + sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3); + sqlite3VdbeLoadString(v, 4, " missing from index "); + sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3); + jmp5 = sqlite3VdbeLoadString(v, 4, pIdx->zName); + sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3); + jmp4 = integrityCheckResultRow(v); + sqlite3VdbeJumpHere(v, jmp2); + + /* The OP_IdxRowid opcode is an optimized version of OP_Column + ** that extracts the rowid off the end of the index record. + ** But it only works correctly if index record does not have + ** any extra bytes at the end. Verify that this is the case. */ + if( HasRowid(pTab) ){ + int jmp7; + sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur+j, 3); + jmp7 = sqlite3VdbeAddOp3(v, OP_Eq, 3, 0, r1+pIdx->nColumn-1); + VdbeCoverageNeverNull(v); + sqlite3VdbeLoadString(v, 3, + "rowid not at end-of-record for row "); + sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3); + sqlite3VdbeLoadString(v, 4, " of index "); + sqlite3VdbeGoto(v, jmp5-1); + sqlite3VdbeJumpHere(v, jmp7); + } + + /* Any indexed columns with non-BINARY collations must still hold + ** the exact same text value as the table. */ + label6 = 0; for(kk=0; kknKeyCol; kk++){ - int iCol = pIdx->aiColumn[kk]; - assert( iCol!=XN_ROWID && iColnCol ); - if( iCol>=0 && pTab->aCol[iCol].notNull ) continue; - sqlite3VdbeAddOp2(v, OP_IsNull, r1+kk, uniqOk); - VdbeCoverage(v); + if( pIdx->azColl[kk]==sqlite3StrBINARY ) continue; + if( label6==0 ) label6 = sqlite3VdbeMakeLabel(pParse); + sqlite3VdbeAddOp3(v, OP_Column, iIdxCur+j, kk, 3); + sqlite3VdbeAddOp3(v, OP_Ne, 3, label6, r1+kk); VdbeCoverage(v); } - jmp6 = sqlite3VdbeAddOp1(v, OP_Next, iIdxCur+j); VdbeCoverage(v); - sqlite3VdbeGoto(v, uniqOk); - sqlite3VdbeJumpHere(v, jmp6); - sqlite3VdbeAddOp4Int(v, OP_IdxGT, iIdxCur+j, uniqOk, r1, - pIdx->nKeyCol); VdbeCoverage(v); - sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */ - sqlite3VdbeLoadString(v, 3, "non-unique entry in index "); - sqlite3VdbeGoto(v, jmp5); - sqlite3VdbeResolveLabel(v, uniqOk); + if( label6 ){ + int jmp6 = sqlite3VdbeAddOp0(v, OP_Goto); + sqlite3VdbeResolveLabel(v, label6); + sqlite3VdbeLoadString(v, 3, "row "); + sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3); + sqlite3VdbeLoadString(v, 4, " values differ from index "); + sqlite3VdbeGoto(v, jmp5-1); + sqlite3VdbeJumpHere(v, jmp6); + } + + /* For UNIQUE indexes, verify that only one entry exists with the + ** current key. The entry is unique if (1) any column is NULL + ** or (2) the next entry has a different key */ + if( IsUniqueIndex(pIdx) ){ + int uniqOk = sqlite3VdbeMakeLabel(pParse); + int jmp6; + for(kk=0; kknKeyCol; kk++){ + int iCol = pIdx->aiColumn[kk]; + assert( iCol!=XN_ROWID && iColnCol ); + if( iCol>=0 && pTab->aCol[iCol].notNull ) continue; + sqlite3VdbeAddOp2(v, OP_IsNull, r1+kk, uniqOk); + VdbeCoverage(v); + } + jmp6 = sqlite3VdbeAddOp1(v, OP_Next, iIdxCur+j); VdbeCoverage(v); + sqlite3VdbeGoto(v, uniqOk); + sqlite3VdbeJumpHere(v, jmp6); + sqlite3VdbeAddOp4Int(v, OP_IdxGT, iIdxCur+j, uniqOk, r1, + pIdx->nKeyCol); VdbeCoverage(v); + sqlite3VdbeLoadString(v, 3, "non-unique entry in index "); + sqlite3VdbeGoto(v, jmp5); + sqlite3VdbeResolveLabel(v, uniqOk); + } + sqlite3VdbeJumpHere(v, jmp4); + sqlite3ResolvePartIdxLabel(pParse, jmp3); } - sqlite3VdbeJumpHere(v, jmp4); - sqlite3ResolvePartIdxLabel(pParse, jmp3); } sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop); VdbeCoverage(v); sqlite3VdbeJumpHere(v, loopTop-1); -#ifndef SQLITE_OMIT_BTREECOUNT - sqlite3VdbeLoadString(v, 2, "wrong # of entries in index "); - for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ - if( pPk==pIdx ) continue; - addr = sqlite3VdbeCurrentAddr(v); - sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr+2); VdbeCoverage(v); - sqlite3VdbeAddOp2(v, OP_Halt, 0, 0); - sqlite3VdbeAddOp2(v, OP_Count, iIdxCur+j, 3); - sqlite3VdbeAddOp3(v, OP_Eq, 8+j, addr+8, 3); VdbeCoverage(v); - sqlite3VdbeChangeP5(v, SQLITE_NOTNULL); - sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); - sqlite3VdbeLoadString(v, 3, pIdx->zName); - sqlite3VdbeAddOp3(v, OP_Concat, 3, 2, 7); - sqlite3VdbeAddOp2(v, OP_ResultRow, 7, 1); + if( pPk ){ + assert( !isQuick ); + sqlite3ReleaseTempRange(pParse, r2, pPk->nKeyCol); } -#endif /* SQLITE_OMIT_BTREECOUNT */ - } + } + +#ifndef SQLITE_OMIT_VIRTUALTABLE + /* Second pass to invoke the xIntegrity method on all virtual + ** tables. + */ + for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){ + Table *pTab = sqliteHashData(x); + sqlite3_vtab *pVTab; + int a1; + if( tableSkipIntegrityCheck(pTab,pObjTab) ) continue; + if( IsOrdinaryTable(pTab) ) continue; + if( !IsVirtual(pTab) ) continue; + if( pTab->nCol<=0 ){ + const char *zMod = pTab->u.vtab.azArg[0]; + if( sqlite3HashFind(&db->aModule, zMod)==0 ) continue; + } + sqlite3ViewGetColumnNames(pParse, pTab); + if( pTab->u.vtab.p==0 ) continue; + pVTab = pTab->u.vtab.p->pVtab; + if( NEVER(pVTab==0) ) continue; + if( NEVER(pVTab->pModule==0) ) continue; + if( pVTab->pModule->iVersion<4 ) continue; + if( pVTab->pModule->xIntegrity==0 ) continue; + sqlite3VdbeAddOp3(v, OP_VCheck, i, 3, isQuick); + pTab->nTabRef++; + sqlite3VdbeAppendP4(v, pTab, P4_TABLEREF); + a1 = sqlite3VdbeAddOp1(v, OP_IsNull, 3); VdbeCoverage(v); + integrityCheckResultRow(v); + sqlite3VdbeJumpHere(v, a1); + continue; + } +#endif } { static const int iLn = VDBE_OFFSET_LINENO(2); static const VdbeOpList endCode[] = { { OP_AddImm, 1, 0, 0}, /* 0 */ - { OP_If, 1, 4, 0}, /* 1 */ + { OP_IfNotZero, 1, 4, 0}, /* 1 */ { OP_String8, 0, 3, 0}, /* 2 */ { OP_ResultRow, 3, 1, 0}, /* 3 */ + { OP_Halt, 0, 0, 0}, /* 4 */ + { OP_String8, 0, 3, 0}, /* 5 */ + { OP_Goto, 0, 3, 0}, /* 6 */ }; VdbeOp *aOp; aOp = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode, iLn); if( aOp ){ - aOp[0].p2 = -mxErr; + aOp[0].p2 = 1-mxErr; aOp[2].p4type = P4_STATIC; aOp[2].p4.z = "ok"; + aOp[5].p4type = P4_STATIC; + aOp[5].p4.z = (char*)sqlite3ErrStr(SQLITE_CORRUPT); } + sqlite3VdbeChangeP3(v, 0, sqlite3VdbeCurrentAddr(v)-2); } } break; @@ -1647,7 +2235,7 @@ void sqlite3Pragma( ** encoding that will be used for the main database file if a new file ** is created. If an existing main database file is opened, then the ** default text encoding for the existing database is used. - ** + ** ** In all cases new databases created using the ATTACH command are ** created to use the same default text encoding as the main database. If ** the main database has not been initialized and/or created when ATTACH @@ -1678,21 +2266,19 @@ void sqlite3Pragma( assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 ); assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE ); assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE ); - returnSingleText(v, "encoding", encnames[ENC(pParse->db)].zName); + returnSingleText(v, encnames[ENC(pParse->db)].zName); }else{ /* "PRAGMA encoding = XXX" */ /* Only change the value of sqlite.enc if the database handle is not ** initialized. If the main database exists, the new sqlite.enc value ** will be overwritten when the schema is next loaded. If it does not ** already exists, it will be created to use the new encoding value. */ - if( - !(DbHasProperty(db, 0, DB_SchemaLoaded)) || - DbHasProperty(db, 0, DB_Empty) - ){ + if( (db->mDbFlags & DBFLAG_EncodingFixed)==0 ){ for(pEnc=&encnames[0]; pEnc->zName; pEnc++){ if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){ - SCHEMA_ENC(db) = ENC(db) = - pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE; + u8 enc = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE; + SCHEMA_ENC(db) = enc; + sqlite3SetTextEncoding(db, enc); break; } } @@ -1713,7 +2299,9 @@ void sqlite3Pragma( ** PRAGMA [schema.]user_version ** PRAGMA [schema.]user_version = ** - ** PRAGMA [schema.]freelist_count = + ** PRAGMA [schema.]freelist_count + ** + ** PRAGMA [schema.]data_version ** ** PRAGMA [schema.]application_id ** PRAGMA [schema.]application_id = @@ -1739,7 +2327,7 @@ void sqlite3Pragma( case PragTyp_HEADER_VALUE: { int iCookie = pPragma->iArg; /* Which cookie to read or write */ sqlite3VdbeUsesBtree(v, iDb); - if( zRight && (pPragma->mPragFlag & PragFlag_ReadOnly)==0 ){ + if( zRight && (pPragma->mPragFlg & PragFlg_ReadOnly)==0 ){ /* Write the specified cookie value */ static const VdbeOpList setCookie[] = { { OP_Transaction, 0, 1, 0}, /* 0 */ @@ -1753,6 +2341,12 @@ void sqlite3Pragma( aOp[1].p1 = iDb; aOp[1].p2 = iCookie; aOp[1].p3 = sqlite3Atoi(zRight); + aOp[1].p5 = 1; + if( iCookie==BTREE_SCHEMA_VERSION && (db->flags & SQLITE_Defensive)!=0 ){ + /* Do not allow the use of PRAGMA schema_version=VALUE in defensive + ** mode. Change the OP_SetCookie opcode into a no-op. */ + aOp[1].opcode = OP_Noop; + } }else{ /* Read the specified cookie value */ static const VdbeOpList readCookie[] = { @@ -1767,8 +2361,7 @@ void sqlite3Pragma( aOp[0].p1 = iDb; aOp[1].p1 = iDb; aOp[1].p3 = iCookie; - sqlite3VdbeSetNumCols(v, 1); - sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT); + sqlite3VdbeReusable(v); } } break; @@ -1785,11 +2378,11 @@ void sqlite3Pragma( int i = 0; const char *zOpt; pParse->nMem = 1; - setOneColumnName(v, "compile_option"); while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){ sqlite3VdbeLoadString(v, 1, zOpt); sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); } + sqlite3VdbeReusable(v); } break; #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */ @@ -1801,8 +2394,7 @@ void sqlite3Pragma( ** Checkpoint the database. */ case PragTyp_WAL_CHECKPOINT: { - static const char *azCol[] = { "busy", "log", "checkpointed" }; - int iBt = (pId2->z?iDb:SQLITE_MAX_ATTACHED); + int iBt = (pId2->z?iDb:SQLITE_MAX_DB); int eMode = SQLITE_CHECKPOINT_PASSIVE; if( zRight ){ if( sqlite3StrICmp(zRight, "full")==0 ){ @@ -1811,9 +2403,10 @@ void sqlite3Pragma( eMode = SQLITE_CHECKPOINT_RESTART; }else if( sqlite3StrICmp(zRight, "truncate")==0 ){ eMode = SQLITE_CHECKPOINT_TRUNCATE; + }else if( sqlite3StrICmp(zRight, "noop")==0 ){ + eMode = SQLITE_CHECKPOINT_NOOP; } } - setAllColumnNames(v, 3, azCol); assert( 3==ArraySize(azCol) ); pParse->nMem = 3; sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1); sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3); @@ -1832,8 +2425,8 @@ void sqlite3Pragma( if( zRight ){ sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight)); } - returnSingleInt(v, "wal_autocheckpoint", - db->xWalCallback==sqlite3WalDefaultHook ? + returnSingleInt(v, + db->xWalCallback==sqlite3WalDefaultHook ? SQLITE_PTR_TO_INT(db->pWalArg) : 0); } break; @@ -1851,6 +2444,204 @@ void sqlite3Pragma( break; } + /* + ** PRAGMA optimize + ** PRAGMA optimize(MASK) + ** PRAGMA schema.optimize + ** PRAGMA schema.optimize(MASK) + ** + ** Attempt to optimize the database. All schemas are optimized in the first + ** two forms, and only the specified schema is optimized in the latter two. + ** + ** The details of optimizations performed by this pragma are expected + ** to change and improve over time. Applications should anticipate that + ** this pragma will perform new optimizations in future releases. + ** + ** The optional argument is a bitmask of optimizations to perform: + ** + ** 0x00001 Debugging mode. Do not actually perform any optimizations + ** but instead return one line of text for each optimization + ** that would have been done. Off by default. + ** + ** 0x00002 Run ANALYZE on tables that might benefit. On by default. + ** See below for additional information. + ** + ** 0x00010 Run all ANALYZE operations using an analysis_limit that + ** is the lessor of the current analysis_limit and the + ** SQLITE_DEFAULT_OPTIMIZE_LIMIT compile-time option. + ** The default value of SQLITE_DEFAULT_OPTIMIZE_LIMIT is + ** currently (2024-02-19) set to 2000, which is such that + ** the worst case run-time for PRAGMA optimize on a 100MB + ** database will usually be less than 100 milliseconds on + ** a RaspberryPI-4 class machine. On by default. + ** + ** 0x10000 Look at tables to see if they need to be reanalyzed + ** due to growth or shrinkage even if they have not been + ** queried during the current connection. Off by default. + ** + ** The default MASK is and always shall be 0x0fffe. In the current + ** implementation, the default mask only covers the 0x00002 optimization, + ** though additional optimizations that are covered by 0x0fffe might be + ** added in the future. Optimizations that are off by default and must + ** be explicitly requested have masks of 0x10000 or greater. + ** + ** DETERMINATION OF WHEN TO RUN ANALYZE + ** + ** In the current implementation, a table is analyzed if only if all of + ** the following are true: + ** + ** (1) MASK bit 0x00002 is set. + ** + ** (2) The table is an ordinary table, not a virtual table or view. + ** + ** (3) The table name does not begin with "sqlite_". + ** + ** (4) One or more of the following is true: + ** (4a) The 0x10000 MASK bit is set. + ** (4b) One or more indexes on the table lacks an entry + ** in the sqlite_stat1 table. + ** (4c) The query planner used sqlite_stat1-style statistics for one + ** or more indexes of the table at some point during the lifetime + ** of the current connection. + ** + ** (5) One or more of the following is true: + ** (5a) One or more indexes on the table lacks an entry + ** in the sqlite_stat1 table. (Same as 4a) + ** (5b) The number of rows in the table has increased or decreased by + ** 10-fold. In other words, the current size of the table is + ** 10 times larger than the size in sqlite_stat1 or else the + ** current size is less than 1/10th the size in sqlite_stat1. + ** + ** The rules for when tables are analyzed are likely to change in + ** future releases. Future versions of SQLite might accept a string + ** literal argument to this pragma that contains a mnemonic description + ** of the options rather than a bitmap. + */ + case PragTyp_OPTIMIZE: { + int iDbLast; /* Loop termination point for the schema loop */ + int iTabCur; /* Cursor for a table whose size needs checking */ + HashElem *k; /* Loop over tables of a schema */ + Schema *pSchema; /* The current schema */ + Table *pTab; /* A table in the schema */ + Index *pIdx; /* An index of the table */ + LogEst szThreshold; /* Size threshold above which reanalysis needed */ + char *zSubSql; /* SQL statement for the OP_SqlExec opcode */ + u32 opMask; /* Mask of operations to perform */ + int nLimit; /* Analysis limit to use */ + int nCheck = 0; /* Number of tables to be optimized */ + int nBtree = 0; /* Number of btrees to scan */ + int nIndex; /* Number of indexes on the current table */ + + if( zRight ){ + opMask = (u32)sqlite3Atoi(zRight); + if( (opMask & 0x02)==0 ) break; + }else{ + opMask = 0xfffe; + } + if( (opMask & 0x10)==0 ){ + nLimit = 0; + }else if( db->nAnalysisLimit>0 + && db->nAnalysisLimitnTab++; + for(iDbLast = zDb?iDb:db->nDb-1; iDb<=iDbLast; iDb++){ + if( iDb==1 ) continue; + sqlite3CodeVerifySchema(pParse, iDb); + pSchema = db->aDb[iDb].pSchema; + for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){ + pTab = (Table*)sqliteHashData(k); + + /* This only works for ordinary tables */ + if( !IsOrdinaryTable(pTab) ) continue; + + /* Do not scan system tables */ + if( 0==sqlite3StrNICmp(pTab->zName, "sqlite_", 7) ) continue; + + /* Find the size of the table as last recorded in sqlite_stat1. + ** If any index is unanalyzed, then the threshold is -1 to + ** indicate a new, unanalyzed index + */ + szThreshold = pTab->nRowLogEst; + nIndex = 0; + for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ + nIndex++; + if( !pIdx->hasStat1 ){ + szThreshold = -1; /* Always analyze if any index lacks statistics */ + } + } + + /* If table pTab has not been used in a way that would benefit from + ** having analysis statistics during the current session, then skip it, + ** unless the 0x10000 MASK bit is set. */ + if( (pTab->tabFlags & TF_MaybeReanalyze)!=0 ){ + /* Check for size change if stat1 has been used for a query */ + }else if( opMask & 0x10000 ){ + /* Check for size change if 0x10000 is set */ + }else if( pTab->pIndex!=0 && szThreshold<0 ){ + /* Do analysis if unanalyzed indexes exists */ + }else{ + /* Otherwise, we can skip this table */ + continue; + } + + nCheck++; + if( nCheck==2 ){ + /* If ANALYZE might be invoked two or more times, hold a write + ** transaction for efficiency */ + sqlite3BeginWriteOperation(pParse, 0, iDb); + } + nBtree += nIndex+1; + + /* Reanalyze if the table is 10 times larger or smaller than + ** the last analysis. Unconditional reanalysis if there are + ** unanalyzed indexes. */ + sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead); + if( szThreshold>=0 ){ + const LogEst iRange = 33; /* 10x size change */ + sqlite3VdbeAddOp4Int(v, OP_IfSizeBetween, iTabCur, + sqlite3VdbeCurrentAddr(v)+2+(opMask&1), + szThreshold>=iRange ? szThreshold-iRange : -1, + szThreshold+iRange); + VdbeCoverage(v); + }else{ + sqlite3VdbeAddOp2(v, OP_Rewind, iTabCur, + sqlite3VdbeCurrentAddr(v)+2+(opMask&1)); + VdbeCoverage(v); + } + zSubSql = sqlite3MPrintf(db, "ANALYZE \"%w\".\"%w\"", + db->aDb[iDb].zDbSName, pTab->zName); + if( opMask & 0x01 ){ + int r1 = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp4(v, OP_String8, 0, r1, 0, zSubSql, P4_DYNAMIC); + sqlite3VdbeAddOp2(v, OP_ResultRow, r1, 1); + }else{ + sqlite3VdbeAddOp4(v, OP_SqlExec, nLimit ? 0x02 : 00, nLimit, 0, + zSubSql, P4_DYNAMIC); + } + } + } + sqlite3VdbeAddOp0(v, OP_Expire); + + /* In a schema with a large number of tables and indexes, scale back + ** the analysis_limit to avoid excess run-time in the worst case. + */ + if( !db->mallocFailed && nLimit>0 && nBtree>100 ){ + int iAddr, iEnd; + VdbeOp *aOp; + nLimit = 100*nLimit/nBtree; + if( nLimit<100 ) nLimit = 100; + aOp = sqlite3VdbeGetOp(v, 0); + iEnd = sqlite3VdbeCurrentAddr(v); + for(iAddr=0; iAddrbusyTimeout); + returnSingleInt(v, db->busyTimeout); break; } @@ -1885,7 +2676,28 @@ void sqlite3Pragma( if( zRight && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK ){ sqlite3_soft_heap_limit64(N); } - returnSingleInt(v, "soft_heap_limit", sqlite3_soft_heap_limit64(-1)); + returnSingleInt(v, sqlite3_soft_heap_limit64(-1)); + break; + } + + /* + ** PRAGMA hard_heap_limit + ** PRAGMA hard_heap_limit = N + ** + ** Invoke sqlite3_hard_heap_limit64() to query or set the hard heap + ** limit. The hard heap limit can be activated or lowered by this + ** pragma, but not raised or deactivated. Only the + ** sqlite3_hard_heap_limit64() C-language API can raise or deactivate + ** the hard heap limit. This allows an application to set a heap limit + ** constraint that cannot be relaxed by an untrusted SQL script. + */ + case PragTyp_HARD_HEAP_LIMIT: { + sqlite3_int64 N; + if( zRight && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK ){ + sqlite3_int64 iPrior = sqlite3_hard_heap_limit64(-1); + if( N>0 && (iPrior==0 || iPrior>N) ) sqlite3_hard_heap_limit64(N); + } + returnSingleInt(v, sqlite3_hard_heap_limit64(-1)); break; } @@ -1904,8 +2716,26 @@ void sqlite3Pragma( ){ sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, (int)(N&0x7fffffff)); } - returnSingleInt(v, "threads", - sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, -1)); + returnSingleInt(v, sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, -1)); + break; + } + + /* + ** PRAGMA analysis_limit + ** PRAGMA analysis_limit = N + ** + ** Configure the maximum number of rows that ANALYZE will examine + ** in each index that it looks at. Return the new limit. + */ + case PragTyp_ANALYSIS_LIMIT: { + sqlite3_int64 N; + if( zRight + && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK /* IMP: R-40975-20399 */ + && N>=0 + ){ + db->nAnalysisLimit = (int)(N&0x7fffffff); + } + returnSingleInt(v, db->nAnalysisLimit); /* IMP: R-57594-65522 */ break; } @@ -1917,77 +2747,411 @@ void sqlite3Pragma( static const char *const azLockName[] = { "unlocked", "shared", "reserved", "pending", "exclusive" }; - static const char *azCol[] = { "database", "status" }; int i; - setAllColumnNames(v, 2, azCol); assert( 2==ArraySize(azCol) ); pParse->nMem = 2; for(i=0; inDb; i++){ Btree *pBt; const char *zState = "unknown"; int j; - if( db->aDb[i].zName==0 ) continue; + if( db->aDb[i].zDbSName==0 ) continue; pBt = db->aDb[i].pBt; if( pBt==0 || sqlite3BtreePager(pBt)==0 ){ zState = "closed"; - }else if( sqlite3_file_control(db, i ? db->aDb[i].zName : 0, + }else if( sqlite3_file_control(db, i ? db->aDb[i].zDbSName : 0, SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){ zState = azLockName[j]; } - sqlite3VdbeMultiLoad(v, 1, "ss", db->aDb[i].zName, zState); - sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2); + sqlite3VdbeMultiLoad(v, 1, "ss", db->aDb[i].zDbSName, zState); } break; } #endif +/* BEGIN SQLCIPHER */ #ifdef SQLITE_HAS_CODEC + /* Pragma iArg + ** ---------- ------ + ** key 0 + ** rekey 1 + ** hexkey 2 + ** hexrekey 3 + ** textkey 4 + ** textrekey 5 + */ case PragTyp_KEY: { - if( zRight ) sqlite3_key_v2(db, zDb, zRight, sqlite3Strlen30(zRight)); - break; - } - case PragTyp_REKEY: { - if( zRight ) sqlite3_rekey_v2(db, zDb, zRight, sqlite3Strlen30(zRight)); - break; - } - case PragTyp_HEXKEY: { if( zRight ){ - u8 iByte; - int i; - char zKey[40]; - for(i=0, iByte=0; iiArg==2 || pPragma->iArg==3 ){ + u8 iByte; + int i; + for(i=0, iByte=0; iiArg<4 ? sqlite3Strlen30(zRight) : -1; } - if( (zLeft[3] & 0xf)==0xb ){ - sqlite3_key_v2(db, zDb, zKey, i/2); + if( (pPragma->iArg & 1)==0 ){ + rc = sqlite3_key_v2(db, zDb, zKey, n); }else{ - sqlite3_rekey_v2(db, zDb, zKey, i/2); + rc = sqlite3_rekey_v2(db, zDb, zKey, n); + } + if( rc==SQLITE_OK && n!=0 ){ + sqlite3VdbeSetNumCols(v, 1); + sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "ok", SQLITE_STATIC); + returnSingleText(v, "ok"); + } else { + sqlite3ErrorMsg(pParse, "An error occurred with PRAGMA key or rekey. " + "PRAGMA key requires a key of one or more characters. " + "PRAGMA rekey can only be run on an existing encrypted database. " + "Use sqlcipher_export() and ATTACH to convert encrypted/plaintext databases."); + goto pragma_out; } } break; } #endif -#if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD) +/* END SQLCIPHER */ +#if defined(SQLITE_ENABLE_CEROD) case PragTyp_ACTIVATE_EXTENSIONS: if( zRight ){ -#ifdef SQLITE_HAS_CODEC - if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){ - sqlite3_activate_see(&zRight[4]); - } -#endif -#ifdef SQLITE_ENABLE_CEROD if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){ sqlite3_activate_cerod(&zRight[6]); } -#endif } break; #endif } /* End of the PRAGMA switch */ + /* The following block is a no-op unless SQLITE_DEBUG is defined. Its only + ** purpose is to execute assert() statements to verify that if the + ** PragFlg_NoColumns1 flag is set and the caller specified an argument + ** to the PRAGMA, the implementation has not added any OP_ResultRow + ** instructions to the VM. */ + if( (pPragma->mPragFlg & PragFlg_NoColumns1) && zRight ){ + sqlite3VdbeVerifyNoResultRow(v); + } + pragma_out: sqlite3DbFree(db, zLeft); sqlite3DbFree(db, zRight); } +#ifndef SQLITE_OMIT_VIRTUALTABLE +/***************************************************************************** +** Implementation of an eponymous virtual table that runs a pragma. +** +*/ +typedef struct PragmaVtab PragmaVtab; +typedef struct PragmaVtabCursor PragmaVtabCursor; +struct PragmaVtab { + sqlite3_vtab base; /* Base class. Must be first */ + sqlite3 *db; /* The database connection to which it belongs */ + const PragmaName *pName; /* Name of the pragma */ + u8 nHidden; /* Number of hidden columns */ + u8 iHidden; /* Index of the first hidden column */ +}; +struct PragmaVtabCursor { + sqlite3_vtab_cursor base; /* Base class. Must be first */ + sqlite3_stmt *pPragma; /* The pragma statement to run */ + sqlite_int64 iRowid; /* Current rowid */ + char *azArg[2]; /* Value of the argument and schema */ +}; + +/* +** Pragma virtual table module xConnect method. +*/ +static int pragmaVtabConnect( + sqlite3 *db, + void *pAux, + int argc, const char *const*argv, + sqlite3_vtab **ppVtab, + char **pzErr +){ + const PragmaName *pPragma = (const PragmaName*)pAux; + PragmaVtab *pTab = 0; + int rc; + int i, j; + char cSep = '('; + StrAccum acc; + char zBuf[200]; + + UNUSED_PARAMETER(argc); + UNUSED_PARAMETER(argv); + sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0); + sqlite3_str_appendall(&acc, "CREATE TABLE x"); + for(i=0, j=pPragma->iPragCName; inPragCName; i++, j++){ + sqlite3_str_appendf(&acc, "%c\"%s\"", cSep, pragCName[j]); + cSep = ','; + } + if( i==0 ){ + sqlite3_str_appendf(&acc, "(\"%s\"", pPragma->zName); + i++; + } + j = 0; + if( pPragma->mPragFlg & PragFlg_Result1 ){ + sqlite3_str_appendall(&acc, ",arg HIDDEN"); + j++; + } + if( pPragma->mPragFlg & (PragFlg_SchemaOpt|PragFlg_SchemaReq) ){ + sqlite3_str_appendall(&acc, ",schema HIDDEN"); + j++; + } + sqlite3_str_append(&acc, ")", 1); + sqlite3StrAccumFinish(&acc); + assert( strlen(zBuf) < sizeof(zBuf)-1 ); + rc = sqlite3_declare_vtab(db, zBuf); + if( rc==SQLITE_OK ){ + pTab = (PragmaVtab*)sqlite3_malloc(sizeof(PragmaVtab)); + if( pTab==0 ){ + rc = SQLITE_NOMEM; + }else{ + memset(pTab, 0, sizeof(PragmaVtab)); + pTab->pName = pPragma; + pTab->db = db; + pTab->iHidden = i; + pTab->nHidden = j; + } + }else{ + *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db)); + } + + *ppVtab = (sqlite3_vtab*)pTab; + return rc; +} + +/* +** Pragma virtual table module xDisconnect method. +*/ +static int pragmaVtabDisconnect(sqlite3_vtab *pVtab){ + PragmaVtab *pTab = (PragmaVtab*)pVtab; + sqlite3_free(pTab); + return SQLITE_OK; +} + +/* Figure out the best index to use to search a pragma virtual table. +** +** There are not really any index choices. But we want to encourage the +** query planner to give == constraints on as many hidden parameters as +** possible, and especially on the first hidden parameter. So return a +** high cost if hidden parameters are unconstrained. +*/ +static int pragmaVtabBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){ + PragmaVtab *pTab = (PragmaVtab*)tab; + const struct sqlite3_index_constraint *pConstraint; + int i, j; + int seen[2]; + + pIdxInfo->estimatedCost = (double)1; + if( pTab->nHidden==0 ){ return SQLITE_OK; } + pConstraint = pIdxInfo->aConstraint; + seen[0] = 0; + seen[1] = 0; + for(i=0; inConstraint; i++, pConstraint++){ + if( pConstraint->iColumn < pTab->iHidden ) continue; + if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue; + if( pConstraint->usable==0 ) return SQLITE_CONSTRAINT; + j = pConstraint->iColumn - pTab->iHidden; + assert( j < 2 ); + seen[j] = i+1; + } + if( seen[0]==0 ){ + pIdxInfo->estimatedCost = (double)2147483647; + pIdxInfo->estimatedRows = 2147483647; + return SQLITE_OK; + } + j = seen[0]-1; + pIdxInfo->aConstraintUsage[j].argvIndex = 1; + pIdxInfo->aConstraintUsage[j].omit = 1; + pIdxInfo->estimatedCost = (double)20; + pIdxInfo->estimatedRows = 20; + if( seen[1] ){ + j = seen[1]-1; + pIdxInfo->aConstraintUsage[j].argvIndex = 2; + pIdxInfo->aConstraintUsage[j].omit = 1; + } + return SQLITE_OK; +} + +/* Create a new cursor for the pragma virtual table */ +static int pragmaVtabOpen(sqlite3_vtab *pVtab, sqlite3_vtab_cursor **ppCursor){ + PragmaVtabCursor *pCsr; + pCsr = (PragmaVtabCursor*)sqlite3_malloc(sizeof(*pCsr)); + if( pCsr==0 ) return SQLITE_NOMEM; + memset(pCsr, 0, sizeof(PragmaVtabCursor)); + pCsr->base.pVtab = pVtab; + *ppCursor = &pCsr->base; + return SQLITE_OK; +} + +/* Clear all content from pragma virtual table cursor. */ +static void pragmaVtabCursorClear(PragmaVtabCursor *pCsr){ + int i; + sqlite3_finalize(pCsr->pPragma); + pCsr->pPragma = 0; + pCsr->iRowid = 0; + for(i=0; iazArg); i++){ + sqlite3_free(pCsr->azArg[i]); + pCsr->azArg[i] = 0; + } +} + +/* Close a pragma virtual table cursor */ +static int pragmaVtabClose(sqlite3_vtab_cursor *cur){ + PragmaVtabCursor *pCsr = (PragmaVtabCursor*)cur; + pragmaVtabCursorClear(pCsr); + sqlite3_free(pCsr); + return SQLITE_OK; +} + +/* Advance the pragma virtual table cursor to the next row */ +static int pragmaVtabNext(sqlite3_vtab_cursor *pVtabCursor){ + PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor; + int rc = SQLITE_OK; + + /* Increment the xRowid value */ + pCsr->iRowid++; + assert( pCsr->pPragma ); + if( SQLITE_ROW!=sqlite3_step(pCsr->pPragma) ){ + rc = sqlite3_finalize(pCsr->pPragma); + pCsr->pPragma = 0; + pragmaVtabCursorClear(pCsr); + } + return rc; +} + +/* +** Pragma virtual table module xFilter method. +*/ +static int pragmaVtabFilter( + sqlite3_vtab_cursor *pVtabCursor, + int idxNum, const char *idxStr, + int argc, sqlite3_value **argv +){ + PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor; + PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab); + int rc; + int i, j; + StrAccum acc; + char *zSql; + + UNUSED_PARAMETER(idxNum); + UNUSED_PARAMETER(idxStr); + pragmaVtabCursorClear(pCsr); + j = (pTab->pName->mPragFlg & PragFlg_Result1)!=0 ? 0 : 1; + for(i=0; iazArg) ); + assert( pCsr->azArg[j]==0 ); + if( zText ){ + pCsr->azArg[j] = sqlite3_mprintf("%s", zText); + if( pCsr->azArg[j]==0 ){ + return SQLITE_NOMEM; + } + } + } + sqlite3StrAccumInit(&acc, 0, 0, 0, pTab->db->aLimit[SQLITE_LIMIT_SQL_LENGTH]); + sqlite3_str_appendall(&acc, "PRAGMA "); + if( pCsr->azArg[1] ){ + sqlite3_str_appendf(&acc, "%Q.", pCsr->azArg[1]); + } + sqlite3_str_appendall(&acc, pTab->pName->zName); + if( pCsr->azArg[0] ){ + sqlite3_str_appendf(&acc, "=%Q", pCsr->azArg[0]); + } + zSql = sqlite3StrAccumFinish(&acc); + if( zSql==0 ) return SQLITE_NOMEM; + rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pPragma, 0); + sqlite3_free(zSql); + if( rc!=SQLITE_OK ){ + pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db)); + return rc; + } + return pragmaVtabNext(pVtabCursor); +} + +/* +** Pragma virtual table module xEof method. +*/ +static int pragmaVtabEof(sqlite3_vtab_cursor *pVtabCursor){ + PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor; + return (pCsr->pPragma==0); +} + +/* The xColumn method simply returns the corresponding column from +** the PRAGMA. +*/ +static int pragmaVtabColumn( + sqlite3_vtab_cursor *pVtabCursor, + sqlite3_context *ctx, + int i +){ + PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor; + PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab); + if( iiHidden ){ + sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pPragma, i)); + }else{ + sqlite3_result_text(ctx, pCsr->azArg[i-pTab->iHidden],-1,SQLITE_TRANSIENT); + } + return SQLITE_OK; +} + +/* +** Pragma virtual table module xRowid method. +*/ +static int pragmaVtabRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *p){ + PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor; + *p = pCsr->iRowid; + return SQLITE_OK; +} + +/* The pragma virtual table object */ +static const sqlite3_module pragmaVtabModule = { + 0, /* iVersion */ + 0, /* xCreate - create a table */ + pragmaVtabConnect, /* xConnect - connect to an existing table */ + pragmaVtabBestIndex, /* xBestIndex - Determine search strategy */ + pragmaVtabDisconnect, /* xDisconnect - Disconnect from a table */ + 0, /* xDestroy - Drop a table */ + pragmaVtabOpen, /* xOpen - open a cursor */ + pragmaVtabClose, /* xClose - close a cursor */ + pragmaVtabFilter, /* xFilter - configure scan constraints */ + pragmaVtabNext, /* xNext - advance a cursor */ + pragmaVtabEof, /* xEof */ + pragmaVtabColumn, /* xColumn - read data */ + pragmaVtabRowid, /* xRowid - read data */ + 0, /* xUpdate - write data */ + 0, /* xBegin - begin transaction */ + 0, /* xSync - sync transaction */ + 0, /* xCommit - commit transaction */ + 0, /* xRollback - rollback transaction */ + 0, /* xFindFunction - function overloading */ + 0, /* xRename - rename the table */ + 0, /* xSavepoint */ + 0, /* xRelease */ + 0, /* xRollbackTo */ + 0, /* xShadowName */ + 0 /* xIntegrity */ +}; + +/* +** Check to see if zTabName is really the name of a pragma. If it is, +** then register an eponymous virtual table for that pragma and return +** a pointer to the Module object for the new virtual table. +*/ +Module *sqlite3PragmaVtabRegister(sqlite3 *db, const char *zName){ + const PragmaName *pName; + assert( sqlite3_strnicmp(zName, "pragma_", 7)==0 ); + pName = pragmaLocate(zName+7); + if( pName==0 ) return 0; + if( (pName->mPragFlg & (PragFlg_Result0|PragFlg_Result1))==0 ) return 0; + assert( sqlite3HashFind(&db->aModule, zName)==0 ); + return sqlite3VtabCreateModule(db, zName, &pragmaVtabModule, (void*)pName, 0); +} + +#endif /* SQLITE_OMIT_VIRTUALTABLE */ #endif /* SQLITE_OMIT_PRAGMA */ diff --git a/src/pragma.h b/src/pragma.h deleted file mode 100644 index 81779e9d4a..0000000000 --- a/src/pragma.h +++ /dev/null @@ -1,464 +0,0 @@ -/* DO NOT EDIT! -** This file is automatically generated by the script at -** ../tool/mkpragmatab.tcl. To update the set of pragmas, edit -** that script and rerun it. -*/ -#define PragTyp_HEADER_VALUE 0 -#define PragTyp_AUTO_VACUUM 1 -#define PragTyp_FLAG 2 -#define PragTyp_BUSY_TIMEOUT 3 -#define PragTyp_CACHE_SIZE 4 -#define PragTyp_CACHE_SPILL 5 -#define PragTyp_CASE_SENSITIVE_LIKE 6 -#define PragTyp_COLLATION_LIST 7 -#define PragTyp_COMPILE_OPTIONS 8 -#define PragTyp_DATA_STORE_DIRECTORY 9 -#define PragTyp_DATABASE_LIST 10 -#define PragTyp_DEFAULT_CACHE_SIZE 11 -#define PragTyp_ENCODING 12 -#define PragTyp_FOREIGN_KEY_CHECK 13 -#define PragTyp_FOREIGN_KEY_LIST 14 -#define PragTyp_INCREMENTAL_VACUUM 15 -#define PragTyp_INDEX_INFO 16 -#define PragTyp_INDEX_LIST 17 -#define PragTyp_INTEGRITY_CHECK 18 -#define PragTyp_JOURNAL_MODE 19 -#define PragTyp_JOURNAL_SIZE_LIMIT 20 -#define PragTyp_LOCK_PROXY_FILE 21 -#define PragTyp_LOCKING_MODE 22 -#define PragTyp_PAGE_COUNT 23 -#define PragTyp_MMAP_SIZE 24 -#define PragTyp_PAGE_SIZE 25 -#define PragTyp_SECURE_DELETE 26 -#define PragTyp_SHRINK_MEMORY 27 -#define PragTyp_SOFT_HEAP_LIMIT 28 -#define PragTyp_STATS 29 -#define PragTyp_SYNCHRONOUS 30 -#define PragTyp_TABLE_INFO 31 -#define PragTyp_TEMP_STORE 32 -#define PragTyp_TEMP_STORE_DIRECTORY 33 -#define PragTyp_THREADS 34 -#define PragTyp_WAL_AUTOCHECKPOINT 35 -#define PragTyp_WAL_CHECKPOINT 36 -#define PragTyp_ACTIVATE_EXTENSIONS 37 -#define PragTyp_HEXKEY 38 -#define PragTyp_KEY 39 -#define PragTyp_REKEY 40 -#define PragTyp_LOCK_STATUS 41 -#define PragTyp_PARSER_TRACE 42 -#define PragFlag_NeedSchema 0x01 -#define PragFlag_ReadOnly 0x02 -static const struct sPragmaNames { - const char *const zName; /* Name of pragma */ - u8 ePragTyp; /* PragTyp_XXX value */ - u8 mPragFlag; /* Zero or more PragFlag_XXX values */ - u32 iArg; /* Extra argument */ -} aPragmaNames[] = { -#if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD) - { /* zName: */ "activate_extensions", - /* ePragTyp: */ PragTyp_ACTIVATE_EXTENSIONS, - /* ePragFlag: */ 0, - /* iArg: */ 0 }, -#endif -#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS) - { /* zName: */ "application_id", - /* ePragTyp: */ PragTyp_HEADER_VALUE, - /* ePragFlag: */ 0, - /* iArg: */ BTREE_APPLICATION_ID }, -#endif -#if !defined(SQLITE_OMIT_AUTOVACUUM) - { /* zName: */ "auto_vacuum", - /* ePragTyp: */ PragTyp_AUTO_VACUUM, - /* ePragFlag: */ PragFlag_NeedSchema, - /* iArg: */ 0 }, -#endif -#if !defined(SQLITE_OMIT_FLAG_PRAGMAS) -#if !defined(SQLITE_OMIT_AUTOMATIC_INDEX) - { /* zName: */ "automatic_index", - /* ePragTyp: */ PragTyp_FLAG, - /* ePragFlag: */ 0, - /* iArg: */ SQLITE_AutoIndex }, -#endif -#endif - { /* zName: */ "busy_timeout", - /* ePragTyp: */ PragTyp_BUSY_TIMEOUT, - /* ePragFlag: */ 0, - /* iArg: */ 0 }, -#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) - { /* zName: */ "cache_size", - /* ePragTyp: */ PragTyp_CACHE_SIZE, - /* ePragFlag: */ PragFlag_NeedSchema, - /* iArg: */ 0 }, -#endif -#if !defined(SQLITE_OMIT_FLAG_PRAGMAS) - { /* zName: */ "cache_spill", - /* ePragTyp: */ PragTyp_CACHE_SPILL, - /* ePragFlag: */ 0, - /* iArg: */ 0 }, -#endif - { /* zName: */ "case_sensitive_like", - /* ePragTyp: */ PragTyp_CASE_SENSITIVE_LIKE, - /* ePragFlag: */ 0, - /* iArg: */ 0 }, - { /* zName: */ "cell_size_check", - /* ePragTyp: */ PragTyp_FLAG, - /* ePragFlag: */ 0, - /* iArg: */ SQLITE_CellSizeCk }, -#if !defined(SQLITE_OMIT_FLAG_PRAGMAS) - { /* zName: */ "checkpoint_fullfsync", - /* ePragTyp: */ PragTyp_FLAG, - /* ePragFlag: */ 0, - /* iArg: */ SQLITE_CkptFullFSync }, -#endif -#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) - { /* zName: */ "collation_list", - /* ePragTyp: */ PragTyp_COLLATION_LIST, - /* ePragFlag: */ 0, - /* iArg: */ 0 }, -#endif -#if !defined(SQLITE_OMIT_COMPILEOPTION_DIAGS) - { /* zName: */ "compile_options", - /* ePragTyp: */ PragTyp_COMPILE_OPTIONS, - /* ePragFlag: */ 0, - /* iArg: */ 0 }, -#endif -#if !defined(SQLITE_OMIT_FLAG_PRAGMAS) - { /* zName: */ "count_changes", - /* ePragTyp: */ PragTyp_FLAG, - /* ePragFlag: */ 0, - /* iArg: */ SQLITE_CountRows }, -#endif -#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_OS_WIN - { /* zName: */ "data_store_directory", - /* ePragTyp: */ PragTyp_DATA_STORE_DIRECTORY, - /* ePragFlag: */ 0, - /* iArg: */ 0 }, -#endif -#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS) - { /* zName: */ "data_version", - /* ePragTyp: */ PragTyp_HEADER_VALUE, - /* ePragFlag: */ PragFlag_ReadOnly, - /* iArg: */ BTREE_DATA_VERSION }, -#endif -#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) - { /* zName: */ "database_list", - /* ePragTyp: */ PragTyp_DATABASE_LIST, - /* ePragFlag: */ PragFlag_NeedSchema, - /* iArg: */ 0 }, -#endif -#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED) - { /* zName: */ "default_cache_size", - /* ePragTyp: */ PragTyp_DEFAULT_CACHE_SIZE, - /* ePragFlag: */ PragFlag_NeedSchema, - /* iArg: */ 0 }, -#endif -#if !defined(SQLITE_OMIT_FLAG_PRAGMAS) -#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER) - { /* zName: */ "defer_foreign_keys", - /* ePragTyp: */ PragTyp_FLAG, - /* ePragFlag: */ 0, - /* iArg: */ SQLITE_DeferFKs }, -#endif -#endif -#if !defined(SQLITE_OMIT_FLAG_PRAGMAS) - { /* zName: */ "empty_result_callbacks", - /* ePragTyp: */ PragTyp_FLAG, - /* ePragFlag: */ 0, - /* iArg: */ SQLITE_NullCallback }, -#endif -#if !defined(SQLITE_OMIT_UTF16) - { /* zName: */ "encoding", - /* ePragTyp: */ PragTyp_ENCODING, - /* ePragFlag: */ 0, - /* iArg: */ 0 }, -#endif -#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER) - { /* zName: */ "foreign_key_check", - /* ePragTyp: */ PragTyp_FOREIGN_KEY_CHECK, - /* ePragFlag: */ PragFlag_NeedSchema, - /* iArg: */ 0 }, -#endif -#if !defined(SQLITE_OMIT_FOREIGN_KEY) - { /* zName: */ "foreign_key_list", - /* ePragTyp: */ PragTyp_FOREIGN_KEY_LIST, - /* ePragFlag: */ PragFlag_NeedSchema, - /* iArg: */ 0 }, -#endif -#if !defined(SQLITE_OMIT_FLAG_PRAGMAS) -#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER) - { /* zName: */ "foreign_keys", - /* ePragTyp: */ PragTyp_FLAG, - /* ePragFlag: */ 0, - /* iArg: */ SQLITE_ForeignKeys }, -#endif -#endif -#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS) - { /* zName: */ "freelist_count", - /* ePragTyp: */ PragTyp_HEADER_VALUE, - /* ePragFlag: */ PragFlag_ReadOnly, - /* iArg: */ BTREE_FREE_PAGE_COUNT }, -#endif -#if !defined(SQLITE_OMIT_FLAG_PRAGMAS) - { /* zName: */ "full_column_names", - /* ePragTyp: */ PragTyp_FLAG, - /* ePragFlag: */ 0, - /* iArg: */ SQLITE_FullColNames }, - { /* zName: */ "fullfsync", - /* ePragTyp: */ PragTyp_FLAG, - /* ePragFlag: */ 0, - /* iArg: */ SQLITE_FullFSync }, -#endif -#if defined(SQLITE_HAS_CODEC) - { /* zName: */ "hexkey", - /* ePragTyp: */ PragTyp_HEXKEY, - /* ePragFlag: */ 0, - /* iArg: */ 0 }, - { /* zName: */ "hexrekey", - /* ePragTyp: */ PragTyp_HEXKEY, - /* ePragFlag: */ 0, - /* iArg: */ 0 }, -#endif -#if !defined(SQLITE_OMIT_FLAG_PRAGMAS) -#if !defined(SQLITE_OMIT_CHECK) - { /* zName: */ "ignore_check_constraints", - /* ePragTyp: */ PragTyp_FLAG, - /* ePragFlag: */ 0, - /* iArg: */ SQLITE_IgnoreChecks }, -#endif -#endif -#if !defined(SQLITE_OMIT_AUTOVACUUM) - { /* zName: */ "incremental_vacuum", - /* ePragTyp: */ PragTyp_INCREMENTAL_VACUUM, - /* ePragFlag: */ PragFlag_NeedSchema, - /* iArg: */ 0 }, -#endif -#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) - { /* zName: */ "index_info", - /* ePragTyp: */ PragTyp_INDEX_INFO, - /* ePragFlag: */ PragFlag_NeedSchema, - /* iArg: */ 0 }, - { /* zName: */ "index_list", - /* ePragTyp: */ PragTyp_INDEX_LIST, - /* ePragFlag: */ PragFlag_NeedSchema, - /* iArg: */ 0 }, - { /* zName: */ "index_xinfo", - /* ePragTyp: */ PragTyp_INDEX_INFO, - /* ePragFlag: */ PragFlag_NeedSchema, - /* iArg: */ 1 }, -#endif -#if !defined(SQLITE_OMIT_INTEGRITY_CHECK) - { /* zName: */ "integrity_check", - /* ePragTyp: */ PragTyp_INTEGRITY_CHECK, - /* ePragFlag: */ PragFlag_NeedSchema, - /* iArg: */ 0 }, -#endif -#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) - { /* zName: */ "journal_mode", - /* ePragTyp: */ PragTyp_JOURNAL_MODE, - /* ePragFlag: */ PragFlag_NeedSchema, - /* iArg: */ 0 }, - { /* zName: */ "journal_size_limit", - /* ePragTyp: */ PragTyp_JOURNAL_SIZE_LIMIT, - /* ePragFlag: */ 0, - /* iArg: */ 0 }, -#endif -#if defined(SQLITE_HAS_CODEC) - { /* zName: */ "key", - /* ePragTyp: */ PragTyp_KEY, - /* ePragFlag: */ 0, - /* iArg: */ 0 }, -#endif -#if !defined(SQLITE_OMIT_FLAG_PRAGMAS) - { /* zName: */ "legacy_file_format", - /* ePragTyp: */ PragTyp_FLAG, - /* ePragFlag: */ 0, - /* iArg: */ SQLITE_LegacyFileFmt }, -#endif -#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_ENABLE_LOCKING_STYLE - { /* zName: */ "lock_proxy_file", - /* ePragTyp: */ PragTyp_LOCK_PROXY_FILE, - /* ePragFlag: */ 0, - /* iArg: */ 0 }, -#endif -#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST) - { /* zName: */ "lock_status", - /* ePragTyp: */ PragTyp_LOCK_STATUS, - /* ePragFlag: */ 0, - /* iArg: */ 0 }, -#endif -#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) - { /* zName: */ "locking_mode", - /* ePragTyp: */ PragTyp_LOCKING_MODE, - /* ePragFlag: */ 0, - /* iArg: */ 0 }, - { /* zName: */ "max_page_count", - /* ePragTyp: */ PragTyp_PAGE_COUNT, - /* ePragFlag: */ PragFlag_NeedSchema, - /* iArg: */ 0 }, - { /* zName: */ "mmap_size", - /* ePragTyp: */ PragTyp_MMAP_SIZE, - /* ePragFlag: */ 0, - /* iArg: */ 0 }, - { /* zName: */ "page_count", - /* ePragTyp: */ PragTyp_PAGE_COUNT, - /* ePragFlag: */ PragFlag_NeedSchema, - /* iArg: */ 0 }, - { /* zName: */ "page_size", - /* ePragTyp: */ PragTyp_PAGE_SIZE, - /* ePragFlag: */ 0, - /* iArg: */ 0 }, -#endif -#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_PARSER_TRACE) - { /* zName: */ "parser_trace", - /* ePragTyp: */ PragTyp_PARSER_TRACE, - /* ePragFlag: */ 0, - /* iArg: */ 0 }, -#endif -#if !defined(SQLITE_OMIT_FLAG_PRAGMAS) - { /* zName: */ "query_only", - /* ePragTyp: */ PragTyp_FLAG, - /* ePragFlag: */ 0, - /* iArg: */ SQLITE_QueryOnly }, -#endif -#if !defined(SQLITE_OMIT_INTEGRITY_CHECK) - { /* zName: */ "quick_check", - /* ePragTyp: */ PragTyp_INTEGRITY_CHECK, - /* ePragFlag: */ PragFlag_NeedSchema, - /* iArg: */ 0 }, -#endif -#if !defined(SQLITE_OMIT_FLAG_PRAGMAS) - { /* zName: */ "read_uncommitted", - /* ePragTyp: */ PragTyp_FLAG, - /* ePragFlag: */ 0, - /* iArg: */ SQLITE_ReadUncommitted }, - { /* zName: */ "recursive_triggers", - /* ePragTyp: */ PragTyp_FLAG, - /* ePragFlag: */ 0, - /* iArg: */ SQLITE_RecTriggers }, -#endif -#if defined(SQLITE_HAS_CODEC) - { /* zName: */ "rekey", - /* ePragTyp: */ PragTyp_REKEY, - /* ePragFlag: */ 0, - /* iArg: */ 0 }, -#endif -#if !defined(SQLITE_OMIT_FLAG_PRAGMAS) - { /* zName: */ "reverse_unordered_selects", - /* ePragTyp: */ PragTyp_FLAG, - /* ePragFlag: */ 0, - /* iArg: */ SQLITE_ReverseOrder }, -#endif -#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS) - { /* zName: */ "schema_version", - /* ePragTyp: */ PragTyp_HEADER_VALUE, - /* ePragFlag: */ 0, - /* iArg: */ BTREE_SCHEMA_VERSION }, -#endif -#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) - { /* zName: */ "secure_delete", - /* ePragTyp: */ PragTyp_SECURE_DELETE, - /* ePragFlag: */ 0, - /* iArg: */ 0 }, -#endif -#if !defined(SQLITE_OMIT_FLAG_PRAGMAS) - { /* zName: */ "short_column_names", - /* ePragTyp: */ PragTyp_FLAG, - /* ePragFlag: */ 0, - /* iArg: */ SQLITE_ShortColNames }, -#endif - { /* zName: */ "shrink_memory", - /* ePragTyp: */ PragTyp_SHRINK_MEMORY, - /* ePragFlag: */ 0, - /* iArg: */ 0 }, - { /* zName: */ "soft_heap_limit", - /* ePragTyp: */ PragTyp_SOFT_HEAP_LIMIT, - /* ePragFlag: */ 0, - /* iArg: */ 0 }, -#if !defined(SQLITE_OMIT_FLAG_PRAGMAS) -#if defined(SQLITE_DEBUG) - { /* zName: */ "sql_trace", - /* ePragTyp: */ PragTyp_FLAG, - /* ePragFlag: */ 0, - /* iArg: */ SQLITE_SqlTrace }, -#endif -#endif -#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) - { /* zName: */ "stats", - /* ePragTyp: */ PragTyp_STATS, - /* ePragFlag: */ PragFlag_NeedSchema, - /* iArg: */ 0 }, -#endif -#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) - { /* zName: */ "synchronous", - /* ePragTyp: */ PragTyp_SYNCHRONOUS, - /* ePragFlag: */ PragFlag_NeedSchema, - /* iArg: */ 0 }, -#endif -#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) - { /* zName: */ "table_info", - /* ePragTyp: */ PragTyp_TABLE_INFO, - /* ePragFlag: */ PragFlag_NeedSchema, - /* iArg: */ 0 }, -#endif -#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) - { /* zName: */ "temp_store", - /* ePragTyp: */ PragTyp_TEMP_STORE, - /* ePragFlag: */ 0, - /* iArg: */ 0 }, - { /* zName: */ "temp_store_directory", - /* ePragTyp: */ PragTyp_TEMP_STORE_DIRECTORY, - /* ePragFlag: */ 0, - /* iArg: */ 0 }, -#endif - { /* zName: */ "threads", - /* ePragTyp: */ PragTyp_THREADS, - /* ePragFlag: */ 0, - /* iArg: */ 0 }, -#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS) - { /* zName: */ "user_version", - /* ePragTyp: */ PragTyp_HEADER_VALUE, - /* ePragFlag: */ 0, - /* iArg: */ BTREE_USER_VERSION }, -#endif -#if !defined(SQLITE_OMIT_FLAG_PRAGMAS) -#if defined(SQLITE_DEBUG) - { /* zName: */ "vdbe_addoptrace", - /* ePragTyp: */ PragTyp_FLAG, - /* ePragFlag: */ 0, - /* iArg: */ SQLITE_VdbeAddopTrace }, - { /* zName: */ "vdbe_debug", - /* ePragTyp: */ PragTyp_FLAG, - /* ePragFlag: */ 0, - /* iArg: */ SQLITE_SqlTrace|SQLITE_VdbeListing|SQLITE_VdbeTrace }, - { /* zName: */ "vdbe_eqp", - /* ePragTyp: */ PragTyp_FLAG, - /* ePragFlag: */ 0, - /* iArg: */ SQLITE_VdbeEQP }, - { /* zName: */ "vdbe_listing", - /* ePragTyp: */ PragTyp_FLAG, - /* ePragFlag: */ 0, - /* iArg: */ SQLITE_VdbeListing }, - { /* zName: */ "vdbe_trace", - /* ePragTyp: */ PragTyp_FLAG, - /* ePragFlag: */ 0, - /* iArg: */ SQLITE_VdbeTrace }, -#endif -#endif -#if !defined(SQLITE_OMIT_WAL) - { /* zName: */ "wal_autocheckpoint", - /* ePragTyp: */ PragTyp_WAL_AUTOCHECKPOINT, - /* ePragFlag: */ 0, - /* iArg: */ 0 }, - { /* zName: */ "wal_checkpoint", - /* ePragTyp: */ PragTyp_WAL_CHECKPOINT, - /* ePragFlag: */ PragFlag_NeedSchema, - /* iArg: */ 0 }, -#endif -#if !defined(SQLITE_OMIT_FLAG_PRAGMAS) - { /* zName: */ "writable_schema", - /* ePragTyp: */ PragTyp_FLAG, - /* ePragFlag: */ 0, - /* iArg: */ SQLITE_WriteSchema|SQLITE_RecoveryMode }, -#endif -}; -/* Number of pragmas: 60 on by default, 73 total. */ diff --git a/src/prepare.c b/src/prepare.c index f74aa52e0b..539360b745 100644 --- a/src/prepare.c +++ b/src/prepare.c @@ -21,21 +21,63 @@ */ static void corruptSchema( InitData *pData, /* Initialization context */ - const char *zObj, /* Object being parsed at the point of error */ + char **azObj, /* Type and name of object being parsed */ const char *zExtra /* Error information */ ){ sqlite3 *db = pData->db; - if( !db->mallocFailed && (db->flags & SQLITE_RecoveryMode)==0 ){ + if( db->mallocFailed ){ + pData->rc = SQLITE_NOMEM_BKPT; + }else if( pData->pzErrMsg[0]!=0 ){ + /* A error message has already been generated. Do not overwrite it */ + }else if( pData->mInitFlags & (INITFLAG_AlterMask) ){ + static const char *azAlterType[] = { + "rename", + "drop column", + "add column" + }; + *pData->pzErrMsg = sqlite3MPrintf(db, + "error in %s %s after %s: %s", azObj[0], azObj[1], + azAlterType[(pData->mInitFlags&INITFLAG_AlterMask)-1], + zExtra + ); + pData->rc = SQLITE_ERROR; + }else if( db->flags & SQLITE_WriteSchema ){ + pData->rc = SQLITE_CORRUPT_BKPT; + }else{ char *z; - if( zObj==0 ) zObj = "?"; + const char *zObj = azObj[1] ? azObj[1] : "?"; z = sqlite3MPrintf(db, "malformed database schema (%s)", zObj); - if( zExtra ) z = sqlite3MPrintf(db, "%z - %s", z, zExtra); - sqlite3DbFree(db, *pData->pzErrMsg); + if( zExtra && zExtra[0] ) z = sqlite3MPrintf(db, "%z - %s", z, zExtra); *pData->pzErrMsg = z; + pData->rc = SQLITE_CORRUPT_BKPT; } - pData->rc = db->mallocFailed ? SQLITE_NOMEM : SQLITE_CORRUPT_BKPT; } +/* +** Check to see if any sibling index (another index on the same table) +** of pIndex has the same root page number, and if it does, return true. +** This would indicate a corrupt schema. +*/ +int sqlite3IndexHasDuplicateRootPage(Index *pIndex){ + Index *p; + for(p=pIndex->pTable->pIndex; p; p=p->pNext){ + if( p->tnum==pIndex->tnum && p!=pIndex ) return 1; + } + return 0; +} + +/* forward declaration */ +static int sqlite3Prepare( + sqlite3 *db, /* Database handle. */ + const char *zSql, /* UTF-8 encoded SQL statement. */ + int nBytes, /* Length of zSql in bytes. */ + u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */ + Vdbe *pReprepare, /* VM being reprepared */ + sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ + const char **pzTail /* OUT: End of parsed string */ +); + + /* ** This is the callback routine for the code that initializes the ** database. See sqlite3Init() below for additional information. @@ -43,9 +85,11 @@ static void corruptSchema( ** ** Each callback contains the following information: ** -** argv[0] = name of thing being created -** argv[1] = root page number for table or index. 0 for trigger or view. -** argv[2] = SQL text for the CREATE statement. +** argv[0] = type of object: "table", "index", "trigger", or "view". +** argv[1] = name of thing being created +** argv[2] = associated table if an index or trigger +** argv[3] = root page number for table or index. 0 for trigger or view. +** argv[4] = SQL text for the CREATE statement. ** */ int sqlite3InitCallback(void *pInit, int argc, char **argv, char **NotUsed){ @@ -53,52 +97,71 @@ int sqlite3InitCallback(void *pInit, int argc, char **argv, char **NotUsed){ sqlite3 *db = pData->db; int iDb = pData->iDb; - assert( argc==3 ); + assert( argc==5 ); UNUSED_PARAMETER2(NotUsed, argc); assert( sqlite3_mutex_held(db->mutex) ); - DbClearProperty(db, iDb, DB_Empty); + db->mDbFlags |= DBFLAG_EncodingFixed; + if( argv==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */ + pData->nInitRow++; if( db->mallocFailed ){ - corruptSchema(pData, argv[0], 0); + corruptSchema(pData, argv, 0); return 1; } assert( iDb>=0 && iDbnDb ); - if( argv==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */ - if( argv[1]==0 ){ - corruptSchema(pData, argv[0], 0); - }else if( sqlite3_strnicmp(argv[2],"create ",7)==0 ){ + if( argv[3]==0 ){ + corruptSchema(pData, argv, 0); + }else if( argv[4] + && 'c'==sqlite3UpperToLower[(unsigned char)argv[4][0]] + && 'r'==sqlite3UpperToLower[(unsigned char)argv[4][1]] ){ /* Call the parser to process a CREATE TABLE, INDEX or VIEW. ** But because db->init.busy is set to 1, no VDBE code is generated ** or executed. All the parser does is build the internal data ** structures that describe the table, index, or view. + ** + ** No other valid SQL statement, other than the variable CREATE statements, + ** can begin with the letters "C" and "R". Thus, it is not possible run + ** any other kind of statement while parsing the schema, even a corrupt + ** schema. */ int rc; + u8 saved_iDb = db->init.iDb; sqlite3_stmt *pStmt; TESTONLY(int rcp); /* Return code from sqlite3_prepare() */ assert( db->init.busy ); db->init.iDb = iDb; - db->init.newTnum = sqlite3Atoi(argv[1]); + if( sqlite3GetUInt32(argv[3], &db->init.newTnum)==0 + || (db->init.newTnum>pData->mxPage && pData->mxPage>0) + ){ + if( sqlite3Config.bExtraSchemaChecks ){ + corruptSchema(pData, argv, "invalid rootpage"); + } + } db->init.orphanTrigger = 0; - TESTONLY(rcp = ) sqlite3_prepare(db, argv[2], -1, &pStmt, 0); + db->init.azInit = (const char**)argv; + pStmt = 0; + TESTONLY(rcp = ) sqlite3Prepare(db, argv[4], -1, 0, 0, &pStmt, 0); rc = db->errCode; assert( (rc&0xFF)==(rcp&0xFF) ); - db->init.iDb = 0; + db->init.iDb = saved_iDb; + /* assert( saved_iDb==0 || (db->mDbFlags & DBFLAG_Vacuum)!=0 ); */ if( SQLITE_OK!=rc ){ if( db->init.orphanTrigger ){ assert( iDb==1 ); }else{ - pData->rc = rc; + if( rc > pData->rc ) pData->rc = rc; if( rc==SQLITE_NOMEM ){ sqlite3OomFault(db); }else if( rc!=SQLITE_INTERRUPT && (rc&0xFF)!=SQLITE_LOCKED ){ - corruptSchema(pData, argv[0], sqlite3_errmsg(db)); + corruptSchema(pData, argv, sqlite3_errmsg(db)); } } } + db->init.azInit = sqlite3StdType; /* Any array of string ptrs will do */ sqlite3_finalize(pStmt); - }else if( argv[0]==0 || (argv[2]!=0 && argv[2][0]!=0) ){ - corruptSchema(pData, argv[0], 0); + }else if( argv[1]==0 || (argv[4]!=0 && argv[4][0]!=0) ){ + corruptSchema(pData, argv, 0); }else{ /* If the SQL column is blank it means this is an index that ** was created to be the PRIMARY KEY or to fulfill a UNIQUE @@ -107,16 +170,18 @@ int sqlite3InitCallback(void *pInit, int argc, char **argv, char **NotUsed){ ** to do here is record the root page number for that index. */ Index *pIndex; - pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zName); + pIndex = sqlite3FindIndex(db, argv[1], db->aDb[iDb].zDbSName); if( pIndex==0 ){ - /* This can occur if there exists an index on a TEMP table which - ** has the same name as another index on a permanent index. Since - ** the permanent table is hidden by the TEMP table, we can also - ** safely ignore the index on the permanent table. - */ - /* Do Nothing */; - }else if( sqlite3GetInt32(argv[1], &pIndex->tnum)==0 ){ - corruptSchema(pData, argv[0], "invalid rootpage"); + corruptSchema(pData, argv, "orphan index"); + }else + if( sqlite3GetUInt32(argv[3],&pIndex->tnum)==0 + || pIndex->tnum<2 + || pIndex->tnum>pData->mxPage + || sqlite3IndexHasDuplicateRootPage(pIndex) + ){ + if( sqlite3Config.bExtraSchemaChecks ){ + corruptSchema(pData, argv, "invalid rootpage"); + } } } return 0; @@ -130,39 +195,49 @@ int sqlite3InitCallback(void *pInit, int argc, char **argv, char **NotUsed){ ** auxiliary databases. Return one of the SQLITE_ error codes to ** indicate success or failure. */ -static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){ +int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg, u32 mFlags){ int rc; int i; #ifndef SQLITE_OMIT_DEPRECATED int size; #endif Db *pDb; - char const *azArg[4]; + char const *azArg[6]; int meta[5]; InitData initData; - const char *zMasterName; + const char *zSchemaTabName; int openedTransaction = 0; + int mask = ((db->mDbFlags & DBFLAG_EncodingFixed) | ~DBFLAG_EncodingFixed); + assert( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0 ); assert( iDb>=0 && iDbnDb ); assert( db->aDb[iDb].pSchema ); assert( sqlite3_mutex_held(db->mutex) ); assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) ); - /* Construct the in-memory representation schema tables (sqlite_master or - ** sqlite_temp_master) by invoking the parser directly. The appropriate + db->init.busy = 1; + + /* Construct the in-memory representation schema tables (sqlite_schema or + ** sqlite_temp_schema) by invoking the parser directly. The appropriate ** table name will be inserted automatically by the parser so we can just ** use the abbreviation "x" here. The parser will also automatically tag ** the schema table as read-only. */ - azArg[0] = zMasterName = SCHEMA_TABLE(iDb); - azArg[1] = "1"; - azArg[2] = "CREATE TABLE x(type text,name text,tbl_name text," - "rootpage integer,sql text)"; - azArg[3] = 0; + azArg[0] = "table"; + azArg[1] = zSchemaTabName = SCHEMA_TABLE(iDb); + azArg[2] = azArg[1]; + azArg[3] = "1"; + azArg[4] = "CREATE TABLE x(type text,name text,tbl_name text," + "rootpage int,sql text)"; + azArg[5] = 0; initData.db = db; initData.iDb = iDb; initData.rc = SQLITE_OK; initData.pzErrMsg = pzErrMsg; - sqlite3InitCallback(&initData, 3, (char **)azArg, 0); + initData.mInitFlags = mFlags; + initData.nInitRow = 0; + initData.mxPage = 0; + sqlite3InitCallback(&initData, 5, (char **)azArg, 0); + db->mDbFlags &= mask; if( initData.rc ){ rc = initData.rc; goto error_out; @@ -172,18 +247,18 @@ static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){ */ pDb = &db->aDb[iDb]; if( pDb->pBt==0 ){ - if( !OMIT_TEMPDB && ALWAYS(iDb==1) ){ - DbSetProperty(db, 1, DB_SchemaLoaded); - } - return SQLITE_OK; + assert( iDb==1 ); + DbSetProperty(db, 1, DB_SchemaLoaded); + rc = SQLITE_OK; + goto error_out; } /* If there is not already a read-only (or read-write) transaction opened ** on the b-tree database, open one now. If a transaction is opened, it ** will be closed before this function returns. */ sqlite3BtreeEnter(pDb->pBt); - if( !sqlite3BtreeIsInReadTrans(pDb->pBt) ){ - rc = sqlite3BtreeBeginTrans(pDb->pBt, 0); + if( sqlite3BtreeTxnState(pDb->pBt)==SQLITE_TXN_NONE ){ + rc = sqlite3BtreeBeginTrans(pDb->pBt, 0, 0); if( rc!=SQLITE_OK ){ sqlite3SetString(pzErrMsg, db, sqlite3ErrStr(rc)); goto initone_error_out; @@ -211,6 +286,9 @@ static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){ for(i=0; ipBt, i+1, (u32 *)&meta[i]); } + if( (db->flags & SQLITE_ResetDatabase)!=0 ){ + memset(meta, 0, sizeof(meta)); + } pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1]; /* If opening a non-empty database, check the text encoding. For the @@ -219,27 +297,25 @@ static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){ ** as sqlite3.enc. */ if( meta[BTREE_TEXT_ENCODING-1] ){ /* text encoding */ - if( iDb==0 ){ -#ifndef SQLITE_OMIT_UTF16 + if( iDb==0 && (db->mDbFlags & DBFLAG_EncodingFixed)==0 ){ u8 encoding; +#ifndef SQLITE_OMIT_UTF16 /* If opening the main database, set ENC(db). */ encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3; if( encoding==0 ) encoding = SQLITE_UTF8; - ENC(db) = encoding; #else - ENC(db) = SQLITE_UTF8; + encoding = SQLITE_UTF8; #endif + sqlite3SetTextEncoding(db, encoding); }else{ /* If opening an attached database, the encoding much match ENC(db) */ - if( meta[BTREE_TEXT_ENCODING-1]!=ENC(db) ){ + if( (meta[BTREE_TEXT_ENCODING-1] & 3)!=ENC(db) ){ sqlite3SetString(pzErrMsg, db, "attached databases must use the same" " text encoding as main database"); rc = SQLITE_ERROR; goto initone_error_out; } } - }else{ - DbSetProperty(db, iDb, DB_Empty); } pDb->pSchema->enc = ENC(db); @@ -276,17 +352,18 @@ static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){ ** indices that the user might have created. */ if( iDb==0 && meta[BTREE_FILE_FORMAT-1]>=4 ){ - db->flags &= ~SQLITE_LegacyFileFmt; + db->flags &= ~(u64)SQLITE_LegacyFileFmt; } /* Read the schema information out of the schema tables */ assert( db->init.busy ); + initData.mxPage = sqlite3BtreeLastPage(pDb->pBt); { char *zSql; zSql = sqlite3MPrintf(db, - "SELECT name, rootpage, sql FROM \"%w\".%s ORDER BY rowid", - db->aDb[iDb].zName, zMasterName); + "SELECT*FROM\"%w\".%s ORDER BY rowid", + db->aDb[iDb].zDbSName, zSchemaTabName); #ifndef SQLITE_OMIT_AUTHORIZATION { sqlite3_xauth xAuth; @@ -306,18 +383,22 @@ static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){ } #endif } + assert( pDb == &(db->aDb[iDb]) ); if( db->mallocFailed ){ - rc = SQLITE_NOMEM; + rc = SQLITE_NOMEM_BKPT; sqlite3ResetAllSchemasOfConnection(db); - } - if( rc==SQLITE_OK || (db->flags&SQLITE_RecoveryMode)){ - /* Black magic: If the SQLITE_RecoveryMode flag is set, then consider - ** the schema loaded, even if errors occurred. In this situation the - ** current sqlite3_prepare() operation will fail, but the following one - ** will attempt to compile the supplied statement against whatever subset - ** of the schema was loaded before the error occurred. The primary - ** purpose of this is to allow access to the sqlite_master table - ** even when its contents have been corrupted. + pDb = &db->aDb[iDb]; + }else + if( rc==SQLITE_OK || ((db->flags&SQLITE_NoSchemaError) && rc!=SQLITE_NOMEM)){ + /* Hack: If the SQLITE_NoSchemaError flag is set, then consider + ** the schema loaded, even if errors (other than OOM) occurred. In + ** this situation the current sqlite3_prepare() operation will fail, + ** but the following one will attempt to compile the supplied statement + ** against whatever subset of the schema was loaded before the error + ** occurred. + ** + ** The primary purpose of this is to allow access to the sqlite_schema + ** table even when its contents have been corrupted. */ DbSetProperty(db, iDb, DB_SchemaLoaded); rc = SQLITE_OK; @@ -334,9 +415,13 @@ static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){ sqlite3BtreeLeave(pDb->pBt); error_out: - if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){ - sqlite3OomFault(db); + if( rc ){ + if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){ + sqlite3OomFault(db); + } + sqlite3ResetOneSchema(db, iDb); } + db->init.busy = 0; return rc; } @@ -347,47 +432,34 @@ static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){ ** error occurs, write an error message into *pzErrMsg. ** ** After a database is initialized, the DB_SchemaLoaded bit is set -** bit is set in the flags field of the Db structure. If the database -** file was of zero-length, then the DB_Empty flag is also set. +** bit is set in the flags field of the Db structure. */ int sqlite3Init(sqlite3 *db, char **pzErrMsg){ int i, rc; - int commit_internal = !(db->flags&SQLITE_InternChanges); + int commit_internal = !(db->mDbFlags&DBFLAG_SchemaChange); assert( sqlite3_mutex_held(db->mutex) ); assert( sqlite3BtreeHoldsMutex(db->aDb[0].pBt) ); assert( db->init.busy==0 ); - rc = SQLITE_OK; - db->init.busy = 1; ENC(db) = SCHEMA_ENC(db); - for(i=0; rc==SQLITE_OK && inDb; i++){ - if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue; - rc = sqlite3InitOne(db, i, pzErrMsg); - if( rc ){ - sqlite3ResetOneSchema(db, i); - } + assert( db->nDb>0 ); + /* Do the main schema first */ + if( !DbHasProperty(db, 0, DB_SchemaLoaded) ){ + rc = sqlite3InitOne(db, 0, pzErrMsg, 0); + if( rc ) return rc; } - - /* Once all the other databases have been initialized, load the schema - ** for the TEMP database. This is loaded last, as the TEMP database - ** schema may contain references to objects in other databases. - */ -#ifndef SQLITE_OMIT_TEMPDB - assert( db->nDb>1 ); - if( rc==SQLITE_OK && !DbHasProperty(db, 1, DB_SchemaLoaded) ){ - rc = sqlite3InitOne(db, 1, pzErrMsg); - if( rc ){ - sqlite3ResetOneSchema(db, 1); + /* All other schemas after the main schema. The "temp" schema must be last */ + for(i=db->nDb-1; i>0; i--){ + assert( i==1 || sqlite3BtreeHoldsMutex(db->aDb[i].pBt) ); + if( !DbHasProperty(db, i, DB_SchemaLoaded) ){ + rc = sqlite3InitOne(db, i, pzErrMsg, 0); + if( rc ) return rc; } } -#endif - - db->init.busy = 0; - if( rc==SQLITE_OK && commit_internal ){ + if( commit_internal ){ sqlite3CommitInternalChanges(db); } - - return rc; + return SQLITE_OK; } /* @@ -400,10 +472,12 @@ int sqlite3ReadSchema(Parse *pParse){ assert( sqlite3_mutex_held(db->mutex) ); if( !db->init.busy ){ rc = sqlite3Init(db, &pParse->zErrMsg); - } - if( rc!=SQLITE_OK ){ - pParse->rc = rc; - pParse->nErr++; + if( rc!=SQLITE_OK ){ + pParse->rc = rc; + pParse->nErr++; + }else if( db->noSharedCache ){ + db->mDbFlags |= DBFLAG_SchemaKnownOk; + } } return rc; } @@ -430,10 +504,11 @@ static void schemaIsValid(Parse *pParse){ /* If there is not already a read-only (or read-write) transaction opened ** on the b-tree database, open one now. If a transaction is opened, it ** will be closed immediately after reading the meta-value. */ - if( !sqlite3BtreeIsInReadTrans(pBt) ){ - rc = sqlite3BtreeBeginTrans(pBt, 0); + if( sqlite3BtreeTxnState(pBt)==SQLITE_TXN_NONE ){ + rc = sqlite3BtreeBeginTrans(pBt, 0, 0); if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){ sqlite3OomFault(db); + pParse->rc = SQLITE_NOMEM; } if( rc!=SQLITE_OK ) return; openedTransaction = 1; @@ -445,8 +520,8 @@ static void schemaIsValid(Parse *pParse){ sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&cookie); assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); if( cookie!=db->aDb[iDb].pSchema->schema_cookie ){ + if( DbHasProperty(db, iDb, DB_SchemaLoaded) ) pParse->rc = SQLITE_SCHEMA; sqlite3ResetOneSchema(db, iDb); - pParse->rc = SQLITE_SCHEMA; } /* Close the transaction, if one was opened. */ @@ -464,21 +539,23 @@ static void schemaIsValid(Parse *pParse){ ** attached database is returned. */ int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){ - int i = -1000000; + int i = -32768; - /* If pSchema is NULL, then return -1000000. This happens when code in + /* If pSchema is NULL, then return -32768. This happens when code in ** expr.c is trying to resolve a reference to a transient table (i.e. one ** created by a sub-select). In this case the return value of this ** function should never be used. ** - ** We return -1000000 instead of the more usual -1 simply because using - ** -1000000 as the incorrect index into db->aDb[] is much + ** We return -32768 instead of the more usual -1 simply because using + ** -32768 as the incorrect index into db->aDb[] is much ** more likely to cause a segfault than -1 (of course there are assert() - ** statements too, but it never hurts to play the odds). + ** statements too, but it never hurts to play the odds) and + ** -32768 will still fit into a 16-bit signed integer. */ assert( sqlite3_mutex_held(db->mutex) ); if( pSchema ){ - for(i=0; ALWAYS(inDb); i++){ + for(i=0; 1; i++){ + assert( inDb ); if( db->aDb[i].pSchema==pSchema ){ break; } @@ -491,19 +568,113 @@ int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){ /* ** Free all memory allocations in the pParse object */ -void sqlite3ParserReset(Parse *pParse){ - if( pParse ){ - sqlite3 *db = pParse->db; - sqlite3DbFree(db, pParse->aLabel); +void sqlite3ParseObjectReset(Parse *pParse){ + sqlite3 *db = pParse->db; + assert( db!=0 ); + assert( db->pParse==pParse ); + assert( pParse->nested==0 ); +#ifndef SQLITE_OMIT_SHARED_CACHE + if( pParse->aTableLock ) sqlite3DbNNFreeNN(db, pParse->aTableLock); +#endif + while( pParse->pCleanup ){ + ParseCleanup *pCleanup = pParse->pCleanup; + pParse->pCleanup = pCleanup->pNext; + pCleanup->xCleanup(db, pCleanup->pPtr); + sqlite3DbNNFreeNN(db, pCleanup); + } + if( pParse->aLabel ) sqlite3DbNNFreeNN(db, pParse->aLabel); + if( pParse->pConstExpr ){ sqlite3ExprListDelete(db, pParse->pConstExpr); - if( db ){ - assert( db->lookaside.bDisable >= pParse->disableLookaside ); - db->lookaside.bDisable -= pParse->disableLookaside; - } - pParse->disableLookaside = 0; } + assert( db->lookaside.bDisable >= pParse->disableLookaside ); + db->lookaside.bDisable -= pParse->disableLookaside; + db->lookaside.sz = db->lookaside.bDisable ? 0 : db->lookaside.szTrue; + assert( pParse->db->pParse==pParse ); + db->pParse = pParse->pOuterParse; +} + +/* +** Add a new cleanup operation to a Parser. The cleanup should happen when +** the parser object is destroyed. But, beware: the cleanup might happen +** immediately. +** +** Use this mechanism for uncommon cleanups. There is a higher setup +** cost for this mechanism (an extra malloc), so it should not be used +** for common cleanups that happen on most calls. But for less +** common cleanups, we save a single NULL-pointer comparison in +** sqlite3ParseObjectReset(), which reduces the total CPU cycle count. +** +** If a memory allocation error occurs, then the cleanup happens immediately. +** When either SQLITE_DEBUG or SQLITE_COVERAGE_TEST are defined, the +** pParse->earlyCleanup flag is set in that case. Calling code show verify +** that test cases exist for which this happens, to guard against possible +** use-after-free errors following an OOM. The preferred way to do this is +** to immediately follow the call to this routine with: +** +** testcase( pParse->earlyCleanup ); +** +** This routine returns a copy of its pPtr input (the third parameter) +** except if an early cleanup occurs, in which case it returns NULL. So +** another way to check for early cleanup is to check the return value. +** Or, stop using the pPtr parameter with this call and use only its +** return value thereafter. Something like this: +** +** pObj = sqlite3ParserAddCleanup(pParse, destructor, pObj); +*/ +void *sqlite3ParserAddCleanup( + Parse *pParse, /* Destroy when this Parser finishes */ + void (*xCleanup)(sqlite3*,void*), /* The cleanup routine */ + void *pPtr /* Pointer to object to be cleaned up */ +){ + ParseCleanup *pCleanup; + if( sqlite3FaultSim(300) ){ + pCleanup = 0; + sqlite3OomFault(pParse->db); + }else{ + pCleanup = sqlite3DbMallocRaw(pParse->db, sizeof(*pCleanup)); + } + if( pCleanup ){ + pCleanup->pNext = pParse->pCleanup; + pParse->pCleanup = pCleanup; + pCleanup->pPtr = pPtr; + pCleanup->xCleanup = xCleanup; + }else{ + xCleanup(pParse->db, pPtr); + pPtr = 0; +#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST) + pParse->earlyCleanup = 1; +#endif + } + return pPtr; } +/* +** Turn bulk memory into a valid Parse object and link that Parse object +** into database connection db. +** +** Call sqlite3ParseObjectReset() to undo this operation. +** +** Caution: Do not confuse this routine with sqlite3ParseObjectInit() which +** is generated by Lemon. +*/ +void sqlite3ParseObjectInit(Parse *pParse, sqlite3 *db){ + memset(PARSE_HDR(pParse), 0, PARSE_HDR_SZ); + memset(PARSE_TAIL(pParse), 0, PARSE_TAIL_SZ); + assert( db->pParse!=pParse ); + pParse->pOuterParse = db->pParse; + db->pParse = pParse; + pParse->db = db; + if( db->mallocFailed ) sqlite3ErrorMsg(pParse, "out of memory"); +} + +/* +** Maximum number of times that we will try again to prepare a statement +** that returns SQLITE_ERROR_RETRY. +*/ +#ifndef SQLITE_MAX_PREPARE_RETRY +# define SQLITE_MAX_PREPARE_RETRY 25 +#endif + /* ** Compile the UTF-8 encoded SQL statement zSql into a statement handle. */ @@ -511,27 +682,44 @@ static int sqlite3Prepare( sqlite3 *db, /* Database handle. */ const char *zSql, /* UTF-8 encoded SQL statement. */ int nBytes, /* Length of zSql in bytes. */ - int saveSqlFlag, /* True to copy SQL text into the sqlite3_stmt */ + u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */ Vdbe *pReprepare, /* VM being reprepared */ sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ const char **pzTail /* OUT: End of parsed string */ ){ - Parse *pParse; /* Parsing context */ - char *zErrMsg = 0; /* Error message */ int rc = SQLITE_OK; /* Result code */ int i; /* Loop counter */ + Parse sParse; /* Parsing context */ - /* Allocate the parsing context */ - pParse = sqlite3StackAllocZero(db, sizeof(*pParse)); - if( pParse==0 ){ - rc = SQLITE_NOMEM; - goto end_prepare; + /* sqlite3ParseObjectInit(&sParse, db); // inlined for performance */ + memset(PARSE_HDR(&sParse), 0, PARSE_HDR_SZ); + memset(PARSE_TAIL(&sParse), 0, PARSE_TAIL_SZ); + sParse.pOuterParse = db->pParse; + db->pParse = &sParse; + sParse.db = db; + if( pReprepare ){ + sParse.pReprepare = pReprepare; + sParse.explain = sqlite3_stmt_isexplain((sqlite3_stmt*)pReprepare); + }else{ + assert( sParse.pReprepare==0 ); } - pParse->pReprepare = pReprepare; assert( ppStmt && *ppStmt==0 ); - /* assert( !db->mallocFailed ); // not true with SQLITE_USE_ALLOCA */ + if( db->mallocFailed ){ + sqlite3ErrorMsg(&sParse, "out of memory"); + db->errCode = rc = SQLITE_NOMEM; + goto end_prepare; + } assert( sqlite3_mutex_held(db->mutex) ); + /* For a long-term use prepared statement avoid the use of + ** lookaside memory. + */ + if( prepFlags & SQLITE_PREPARE_PERSISTENT ){ + sParse.disableLookaside++; + DisableLookaside; + } + sParse.prepFlags = prepFlags & 0xff; + /* Check to verify that it is possible to get a read lock on all ** database schemas. The inability to get a read lock indicates that ** some other database connection is holding a write-lock, which in @@ -555,24 +743,26 @@ static int sqlite3Prepare( ** but it does *not* override schema lock detection, so this all still ** works even if READ_UNCOMMITTED is set. */ - for(i=0; inDb; i++) { - Btree *pBt = db->aDb[i].pBt; - if( pBt ){ - assert( sqlite3BtreeHoldsMutex(pBt) ); - rc = sqlite3BtreeSchemaLocked(pBt); - if( rc ){ - const char *zDb = db->aDb[i].zName; - sqlite3ErrorWithMsg(db, rc, "database schema is locked: %s", zDb); - testcase( db->flags & SQLITE_ReadUncommitted ); - goto end_prepare; + if( !db->noSharedCache ){ + for(i=0; inDb; i++) { + Btree *pBt = db->aDb[i].pBt; + if( pBt ){ + assert( sqlite3BtreeHoldsMutex(pBt) ); + rc = sqlite3BtreeSchemaLocked(pBt); + if( rc ){ + const char *zDb = db->aDb[i].zDbSName; + sqlite3ErrorWithMsg(db, rc, "database schema is locked: %s", zDb); + testcase( db->flags & SQLITE_ReadUncommit ); + goto end_prepare; + } } } } - sqlite3VtabUnlockList(db); +#ifndef SQLITE_OMIT_VIRTUALTABLE + if( db->pDisconnect ) sqlite3VtabUnlockList(db); +#endif - pParse->db = db; - pParse->nQueryLoop = 0; /* Logarithmic, so 0 really means 1 */ if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){ char *zSqlCopy; int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH]; @@ -585,95 +775,74 @@ static int sqlite3Prepare( } zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes); if( zSqlCopy ){ - sqlite3RunParser(pParse, zSqlCopy, &zErrMsg); - pParse->zTail = &zSql[pParse->zTail-zSqlCopy]; + sqlite3RunParser(&sParse, zSqlCopy); + sParse.zTail = &zSql[sParse.zTail-zSqlCopy]; sqlite3DbFree(db, zSqlCopy); }else{ - pParse->zTail = &zSql[nBytes]; + sParse.zTail = &zSql[nBytes]; } }else{ - sqlite3RunParser(pParse, zSql, &zErrMsg); + sqlite3RunParser(&sParse, zSql); } - assert( 0==pParse->nQueryLoop ); + assert( 0==sParse.nQueryLoop ); - if( pParse->rc==SQLITE_DONE ) pParse->rc = SQLITE_OK; - if( pParse->checkSchema ){ - schemaIsValid(pParse); - } - if( db->mallocFailed ){ - pParse->rc = SQLITE_NOMEM; - } if( pzTail ){ - *pzTail = pParse->zTail; - } - rc = pParse->rc; - -#ifndef SQLITE_OMIT_EXPLAIN - if( rc==SQLITE_OK && pParse->pVdbe && pParse->explain ){ - static const char * const azColName[] = { - "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment", - "selectid", "order", "from", "detail" - }; - int iFirst, mx; - if( pParse->explain==2 ){ - sqlite3VdbeSetNumCols(pParse->pVdbe, 4); - iFirst = 8; - mx = 12; - }else{ - sqlite3VdbeSetNumCols(pParse->pVdbe, 8); - iFirst = 0; - mx = 8; - } - for(i=iFirst; ipVdbe, i-iFirst, COLNAME_NAME, - azColName[i], SQLITE_STATIC); - } + *pzTail = sParse.zTail; } -#endif if( db->init.busy==0 ){ - Vdbe *pVdbe = pParse->pVdbe; - sqlite3VdbeSetSql(pVdbe, zSql, (int)(pParse->zTail-zSql), saveSqlFlag); + sqlite3VdbeSetSql(sParse.pVdbe, zSql, (int)(sParse.zTail-zSql), prepFlags); } - if( pParse->pVdbe && (rc!=SQLITE_OK || db->mallocFailed) ){ - sqlite3VdbeFinalize(pParse->pVdbe); - assert(!(*ppStmt)); - }else{ - *ppStmt = (sqlite3_stmt*)pParse->pVdbe; + if( db->mallocFailed ){ + sParse.rc = SQLITE_NOMEM_BKPT; + sParse.checkSchema = 0; } - - if( zErrMsg ){ - sqlite3ErrorWithMsg(db, rc, "%s", zErrMsg); - sqlite3DbFree(db, zErrMsg); + if( sParse.rc!=SQLITE_OK && sParse.rc!=SQLITE_DONE ){ + if( sParse.checkSchema && db->init.busy==0 ){ + schemaIsValid(&sParse); + } + if( sParse.pVdbe ){ + sqlite3VdbeFinalize(sParse.pVdbe); + } + assert( 0==(*ppStmt) ); + rc = sParse.rc; + if( sParse.zErrMsg ){ + sqlite3ErrorWithMsg(db, rc, "%s", sParse.zErrMsg); + sqlite3DbFree(db, sParse.zErrMsg); + }else{ + sqlite3Error(db, rc); + } }else{ - sqlite3Error(db, rc); + assert( sParse.zErrMsg==0 ); + *ppStmt = (sqlite3_stmt*)sParse.pVdbe; + rc = SQLITE_OK; + sqlite3ErrorClear(db); } + /* Delete any TriggerPrg structures allocated while parsing this statement. */ - while( pParse->pTriggerPrg ){ - TriggerPrg *pT = pParse->pTriggerPrg; - pParse->pTriggerPrg = pT->pNext; + while( sParse.pTriggerPrg ){ + TriggerPrg *pT = sParse.pTriggerPrg; + sParse.pTriggerPrg = pT->pNext; sqlite3DbFree(db, pT); } end_prepare: - sqlite3ParserReset(pParse); - sqlite3StackFree(db, pParse); - rc = sqlite3ApiExit(db, rc); - assert( (rc&db->errMask)==rc ); + sqlite3ParseObjectReset(&sParse); return rc; } static int sqlite3LockAndPrepare( sqlite3 *db, /* Database handle. */ const char *zSql, /* UTF-8 encoded SQL statement. */ int nBytes, /* Length of zSql in bytes. */ - int saveSqlFlag, /* True to copy SQL text into the sqlite3_stmt */ + u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */ Vdbe *pOld, /* VM being reprepared */ sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ const char **pzTail /* OUT: End of parsed string */ ){ int rc; + int cnt = 0; #ifdef SQLITE_ENABLE_API_ARMOR if( ppStmt==0 ) return SQLITE_MISUSE_BKPT; @@ -684,23 +853,33 @@ static int sqlite3LockAndPrepare( } sqlite3_mutex_enter(db->mutex); sqlite3BtreeEnterAll(db); - rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail); - if( rc==SQLITE_SCHEMA ){ - sqlite3_finalize(*ppStmt); - rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail); - } + do{ + /* Make multiple attempts to compile the SQL, until it either succeeds + ** or encounters a permanent error. A schema problem after one schema + ** reset is considered a permanent error. */ + rc = sqlite3Prepare(db, zSql, nBytes, prepFlags, pOld, ppStmt, pzTail); + assert( rc==SQLITE_OK || *ppStmt==0 ); + if( rc==SQLITE_OK || db->mallocFailed ) break; + cnt++; + }while( (rc==SQLITE_ERROR_RETRY && ALWAYS(cnt<=SQLITE_MAX_PREPARE_RETRY)) + || (rc==SQLITE_SCHEMA && (sqlite3ResetOneSchema(db,-1), cnt)==1) ); sqlite3BtreeLeaveAll(db); + assert( rc!=SQLITE_ERROR_RETRY ); + rc = sqlite3ApiExit(db, rc); + assert( (rc&db->errMask)==rc ); + db->busyHandler.nBusy = 0; sqlite3_mutex_leave(db->mutex); - assert( rc==SQLITE_OK || *ppStmt==0 ); + assert( rc==SQLITE_OK || (*ppStmt)==0 ); return rc; } + /* ** Rerun the compilation of a statement after a schema change. ** ** If the statement is successfully recompiled, return SQLITE_OK. Otherwise, ** if the statement cannot be recompiled because another connection has -** locked the sqlite3_master table, return SQLITE_LOCKED. If any other error +** locked the sqlite3_schema table, return SQLITE_LOCKED. If any other error ** occurs, return SQLITE_SCHEMA. */ int sqlite3Reprepare(Vdbe *p){ @@ -708,13 +887,15 @@ int sqlite3Reprepare(Vdbe *p){ sqlite3_stmt *pNew; const char *zSql; sqlite3 *db; + u8 prepFlags; assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) ); zSql = sqlite3_sql((sqlite3_stmt *)p); assert( zSql!=0 ); /* Reprepare only called for prepare_v2() statements */ db = sqlite3VdbeDb(p); assert( sqlite3_mutex_held(db->mutex) ); - rc = sqlite3LockAndPrepare(db, zSql, -1, 0, p, &pNew, 0); + prepFlags = sqlite3VdbePrepareFlags(p); + rc = sqlite3LockAndPrepare(db, zSql, -1, prepFlags, p, &pNew, 0); if( rc ){ if( rc==SQLITE_NOMEM ){ sqlite3OomFault(db); @@ -760,8 +941,36 @@ int sqlite3_prepare_v2( const char **pzTail /* OUT: End of parsed string */ ){ int rc; - rc = sqlite3LockAndPrepare(db,zSql,nBytes,1,0,ppStmt,pzTail); - assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */ + /* EVIDENCE-OF: R-37923-12173 The sqlite3_prepare_v2() interface works + ** exactly the same as sqlite3_prepare_v3() with a zero prepFlags + ** parameter. + ** + ** Proof in that the 5th parameter to sqlite3LockAndPrepare is 0 */ + rc = sqlite3LockAndPrepare(db,zSql,nBytes,SQLITE_PREPARE_SAVESQL,0, + ppStmt,pzTail); + assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); + return rc; +} +int sqlite3_prepare_v3( + sqlite3 *db, /* Database handle. */ + const char *zSql, /* UTF-8 encoded SQL statement. */ + int nBytes, /* Length of zSql in bytes. */ + unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_* flags */ + sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ + const char **pzTail /* OUT: End of parsed string */ +){ + int rc; + /* EVIDENCE-OF: R-56861-42673 sqlite3_prepare_v3() differs from + ** sqlite3_prepare_v2() only in having the extra prepFlags parameter, + ** which is a bit array consisting of zero or more of the + ** SQLITE_PREPARE_* flags. + ** + ** Proof by comparison to the implementation of sqlite3_prepare_v2() + ** directly above. */ + rc = sqlite3LockAndPrepare(db,zSql,nBytes, + SQLITE_PREPARE_SAVESQL|(prepFlags&SQLITE_PREPARE_MASK), + 0,ppStmt,pzTail); + assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); return rc; } @@ -774,7 +983,7 @@ static int sqlite3Prepare16( sqlite3 *db, /* Database handle. */ const void *zSql, /* UTF-16 encoded SQL statement. */ int nBytes, /* Length of zSql in bytes. */ - int saveSqlFlag, /* True to save SQL text into the sqlite3_stmt */ + u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */ sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ const void **pzTail /* OUT: End of parsed string */ ){ @@ -793,16 +1002,28 @@ static int sqlite3Prepare16( if( !sqlite3SafetyCheckOk(db)||zSql==0 ){ return SQLITE_MISUSE_BKPT; } + + /* Make sure nBytes is non-negative and correct. It should be the + ** number of bytes until the end of the input buffer or until the first + ** U+0000 character. If the input nBytes is odd, convert it into + ** an even number. If the input nBytes is negative, then the input + ** must be terminated by at least one U+0000 character */ if( nBytes>=0 ){ int sz; const char *z = (const char*)zSql; for(sz=0; szmutex); zSql8 = sqlite3Utf16to8(db, zSql, nBytes, SQLITE_UTF16NATIVE); if( zSql8 ){ - rc = sqlite3LockAndPrepare(db, zSql8, -1, saveSqlFlag, 0, ppStmt, &zTail8); + rc = sqlite3LockAndPrepare(db, zSql8, -1, prepFlags, 0, ppStmt, &zTail8); } if( zTail8 && pzTail ){ @@ -812,7 +1033,7 @@ static int sqlite3Prepare16( ** the same number of characters into the UTF-16 string. */ int chars_parsed = sqlite3Utf8CharLen(zSql8, (int)(zTail8-zSql8)); - *pzTail = (u8 *)zSql + sqlite3Utf16ByteLen(zSql, chars_parsed); + *pzTail = (u8 *)zSql + sqlite3Utf16ByteLen(zSql, nBytes, chars_parsed); } sqlite3DbFree(db, zSql8); rc = sqlite3ApiExit(db, rc); @@ -848,7 +1069,22 @@ int sqlite3_prepare16_v2( const void **pzTail /* OUT: End of parsed string */ ){ int rc; - rc = sqlite3Prepare16(db,zSql,nBytes,1,ppStmt,pzTail); + rc = sqlite3Prepare16(db,zSql,nBytes,SQLITE_PREPARE_SAVESQL,ppStmt,pzTail); + assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */ + return rc; +} +int sqlite3_prepare16_v3( + sqlite3 *db, /* Database handle. */ + const void *zSql, /* UTF-16 encoded SQL statement. */ + int nBytes, /* Length of zSql in bytes. */ + unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_* flags */ + sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ + const void **pzTail /* OUT: End of parsed string */ +){ + int rc; + rc = sqlite3Prepare16(db,zSql,nBytes, + SQLITE_PREPARE_SAVESQL|(prepFlags&SQLITE_PREPARE_MASK), + ppStmt,pzTail); assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */ return rc; } diff --git a/src/printf.c b/src/printf.c index 238997f37d..f75ed3b8a2 100644 --- a/src/printf.c +++ b/src/printf.c @@ -15,26 +15,27 @@ ** Conversion types fall into various categories as defined by the ** following enumeration. */ -#define etRADIX 1 /* Integer types. %d, %x, %o, and so forth */ -#define etFLOAT 2 /* Floating point. %f */ -#define etEXP 3 /* Exponentional notation. %e and %E */ -#define etGENERIC 4 /* Floating or exponential, depending on exponent. %g */ -#define etSIZE 5 /* Return number of characters processed so far. %n */ -#define etSTRING 6 /* Strings. %s */ -#define etDYNSTRING 7 /* Dynamically allocated strings. %z */ -#define etPERCENT 8 /* Percent symbol. %% */ -#define etCHARX 9 /* Characters. %c */ +#define etRADIX 0 /* non-decimal integer types. %x %o */ +#define etFLOAT 1 /* Floating point. %f */ +#define etEXP 2 /* Exponentional notation. %e and %E */ +#define etGENERIC 3 /* Floating or exponential, depending on exponent. %g */ +#define etSIZE 4 /* Return number of characters processed so far. %n */ +#define etSTRING 5 /* Strings. %s */ +#define etDYNSTRING 6 /* Dynamically allocated strings. %z */ +#define etPERCENT 7 /* Percent symbol. %% */ +#define etCHARX 8 /* Characters. %c */ /* The rest are extensions, not normally found in printf() */ -#define etSQLESCAPE 10 /* Strings with '\'' doubled. %q */ -#define etSQLESCAPE2 11 /* Strings with '\'' doubled and enclosed in '', - NULL pointers replaced by SQL NULL. %Q */ -#define etTOKEN 12 /* a pointer to a Token structure */ -#define etSRCLIST 13 /* a pointer to a SrcList */ -#define etPOINTER 14 /* The %p conversion */ -#define etSQLESCAPE3 15 /* %w -> Strings with '\"' doubled */ -#define etORDINAL 16 /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */ +#define etESCAPE_q 9 /* Strings with '\'' doubled. %q */ +#define etESCAPE_Q 10 /* Strings with '\'' doubled and enclosed in '', + NULL pointers replaced by SQL NULL. %Q */ +#define etTOKEN 11 /* a pointer to a Token structure */ +#define etSRCITEM 12 /* a pointer to a SrcItem */ +#define etPOINTER 13 /* The %p conversion */ +#define etESCAPE_w 14 /* %w -> Strings with '\"' doubled */ +#define etORDINAL 15 /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */ +#define etDECIMAL 16 /* %d or %u, but not %x, %o */ -#define etINVALID 0 /* Any unrecognized conversion type */ +#define etINVALID 17 /* Any unrecognized conversion type */ /* @@ -53,91 +54,83 @@ typedef struct et_info { /* Information about each format field */ etByte type; /* Conversion paradigm */ etByte charset; /* Offset into aDigits[] of the digits string */ etByte prefix; /* Offset into aPrefix[] of the prefix string */ + char iNxt; /* Next with same hash, or 0 for end of chain */ } et_info; /* ** Allowed values for et_info.flags */ -#define FLAG_SIGNED 1 /* True if the value to convert is signed */ -#define FLAG_INTERN 2 /* True if for internal use only */ -#define FLAG_STRING 4 /* Allow infinity precision */ - +#define FLAG_SIGNED 1 /* True if the value to convert is signed */ +#define FLAG_STRING 4 /* Allow infinite precision */ /* -** The following table is searched linearly, so it is good to put the -** most frequently used conversion types first. +** The table is searched by hash. In the case of %C where C is the character +** and that character has ASCII value j, then the hash is j%23. +** +** The order of the entries in fmtinfo[] and the hash chain was entered +** manually, but based on the output of the following TCL script: */ +#if 0 /***** Beginning of script ******/ +foreach c {d s g z q Q w c o u x X f e E G i n % p T S r} { + scan $c %c x + set n($c) $x +} +set mx [llength [array names n]] +puts "count: $mx" + +set mx 27 +puts "*********** mx=$mx ************" +for {set r 0} {$r<$mx} {incr r} { + puts -nonewline [format %2d: $r] + foreach c [array names n] { + if {($n($c))%$mx==$r} {puts -nonewline " $c"} + } + puts "" +} +#endif /***** End of script ********/ + static const char aDigits[] = "0123456789ABCDEF0123456789abcdef"; static const char aPrefix[] = "-x0\000X0"; -static const et_info fmtinfo[] = { - { 'd', 10, 1, etRADIX, 0, 0 }, - { 's', 0, 4, etSTRING, 0, 0 }, - { 'g', 0, 1, etGENERIC, 30, 0 }, - { 'z', 0, 4, etDYNSTRING, 0, 0 }, - { 'q', 0, 4, etSQLESCAPE, 0, 0 }, - { 'Q', 0, 4, etSQLESCAPE2, 0, 0 }, - { 'w', 0, 4, etSQLESCAPE3, 0, 0 }, - { 'c', 0, 0, etCHARX, 0, 0 }, - { 'o', 8, 0, etRADIX, 0, 2 }, - { 'u', 10, 0, etRADIX, 0, 0 }, - { 'x', 16, 0, etRADIX, 16, 1 }, - { 'X', 16, 0, etRADIX, 0, 4 }, -#ifndef SQLITE_OMIT_FLOATING_POINT - { 'f', 0, 1, etFLOAT, 0, 0 }, - { 'e', 0, 1, etEXP, 30, 0 }, - { 'E', 0, 1, etEXP, 14, 0 }, - { 'G', 0, 1, etGENERIC, 14, 0 }, -#endif - { 'i', 10, 1, etRADIX, 0, 0 }, - { 'n', 0, 0, etSIZE, 0, 0 }, - { '%', 0, 0, etPERCENT, 0, 0 }, - { 'p', 16, 0, etPOINTER, 0, 1 }, - -/* All the rest have the FLAG_INTERN bit set and are thus for internal -** use only */ - { 'T', 0, 2, etTOKEN, 0, 0 }, - { 'S', 0, 2, etSRCLIST, 0, 0 }, - { 'r', 10, 3, etORDINAL, 0, 0 }, +static const et_info fmtinfo[23] = { + /* 0 */ { 's', 0, 4, etSTRING, 0, 0, 1 }, + /* 1 */ { 'E', 0, 1, etEXP, 14, 0, 0 }, /* Hash: 0 */ + /* 2 */ { 'u', 10, 0, etDECIMAL, 0, 0, 3 }, + /* 3 */ { 'G', 0, 1, etGENERIC, 14, 0, 0 }, /* Hash: 2 */ + /* 4 */ { 'w', 0, 4, etESCAPE_w, 0, 0, 0 }, + /* 5 */ { 'x', 16, 0, etRADIX, 16, 1, 0 }, + /* 6 */ { 'c', 0, 0, etCHARX, 0, 0, 0 }, /* Hash: 7 */ + /* 7 */ { 'z', 0, 4, etDYNSTRING, 0, 0, 6 }, + /* 8 */ { 'd', 10, 1, etDECIMAL, 0, 0, 0 }, + /* 9 */ { 'e', 0, 1, etEXP, 30, 0, 0 }, + /* 10 */ { 'f', 0, 1, etFLOAT, 0, 0, 0 }, + /* 11 */ { 'g', 0, 1, etGENERIC, 30, 0, 0 }, + /* 12 */ { 'Q', 0, 4, etESCAPE_Q, 0, 0, 0 }, + /* 13 */ { 'i', 10, 1, etDECIMAL, 0, 0, 0 }, + /* 14 */ { '%', 0, 0, etPERCENT, 0, 0, 16 }, + /* 15 */ { 'T', 0, 0, etTOKEN, 0, 0, 0 }, + /* 16 */ { 'S', 0, 0, etSRCITEM, 0, 0, 0 }, /* Hash: 14 */ + /* 17 */ { 'X', 16, 0, etRADIX, 0, 4, 0 }, /* Hash: 19 */ + /* 18 */ { 'n', 0, 0, etSIZE, 0, 0, 0 }, + /* 19 */ { 'o', 8, 0, etRADIX, 0, 2, 17 }, + /* 20 */ { 'p', 16, 0, etPOINTER, 0, 1, 0 }, + /* 21 */ { 'q', 0, 4, etESCAPE_q, 0, 0, 0 }, + /* 22 */ { 'r', 10, 1, etORDINAL, 0, 0, 0 } }; -/* -** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point -** conversions will work. -*/ -#ifndef SQLITE_OMIT_FLOATING_POINT -/* -** "*val" is a double such that 0.1 <= *val < 10.0 -** Return the ascii code for the leading digit of *val, then -** multiply "*val" by 10.0 to renormalize. +/* Additional Notes: ** -** Example: -** input: *val = 3.14159 -** output: *val = 1.4159 function return = '3' -** -** The counter *cnt is incremented each time. After counter exceeds -** 16 (the number of significant digits in a 64-bit float) '0' is -** always returned. +** %S Takes a pointer to SrcItem. Shows name or database.name +** %!S Like %S but prefer the zName over the zAlias */ -static char et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){ - int digit; - LONGDOUBLE_TYPE d; - if( (*cnt)<=0 ) return '0'; - (*cnt)--; - digit = (int)*val; - d = digit; - digit += '0'; - *val = (*val - d)*10.0; - return (char)digit; -} -#endif /* SQLITE_OMIT_FLOATING_POINT */ /* ** Set the StrAccum object to an error mode. */ -static void setStrAccumError(StrAccum *p, u8 eError){ - assert( eError==STRACCUM_NOMEM || eError==STRACCUM_TOOBIG ); +void sqlite3StrAccumSetError(StrAccum *p, u8 eError){ + assert( eError==SQLITE_NOMEM || eError==SQLITE_TOOBIG ); p->accError = eError; - p->nAlloc = 0; + if( p->mxAlloc ) sqlite3_str_reset(p); + if( eError==SQLITE_TOOBIG ) sqlite3ErrorToParser(p->db, eError); } /* @@ -156,6 +149,28 @@ static char *getTextArg(PrintfArguments *p){ return (char*)sqlite3_value_text(p->apArg[p->nUsed++]); } +/* +** Allocate memory for a temporary buffer needed for printf rendering. +** +** If the requested size of the temp buffer is larger than the size +** of the output buffer in pAccum, then cause an SQLITE_TOOBIG error. +** Do the size check before the memory allocation to prevent rogue +** SQL from requesting large allocations using the precision or width +** field of the printf() function. +*/ +static char *printfTempBuf(sqlite3_str *pAccum, sqlite3_int64 n){ + char *z; + if( pAccum->accError ) return 0; + if( n>pAccum->nAlloc && n>pAccum->mxAlloc ){ + sqlite3StrAccumSetError(pAccum, SQLITE_TOOBIG); + return 0; + } + z = sqlite3DbMallocRaw(pAccum->db, n); + if( z==0 ){ + sqlite3StrAccumSetError(pAccum, SQLITE_NOMEM); + } + return z; +} /* ** On machines with a small stack size, you can redefine the @@ -166,11 +181,18 @@ static char *getTextArg(PrintfArguments *p){ #endif #define etBUFSIZE SQLITE_PRINT_BUF_SIZE /* Size of the output buffer */ +/* +** Hard limit on the precision of floating-point conversions. +*/ +#ifndef SQLITE_PRINTF_PRECISION_LIMIT +# define SQLITE_FP_PRECISION_LIMIT 100000000 +#endif + /* ** Render a string given by "fmt" into the StrAccum object. */ -void sqlite3VXPrintf( - StrAccum *pAccum, /* Accumulate results here */ +void sqlite3_str_vappendf( + sqlite3_str *pAccum, /* Accumulate results here */ const char *fmt, /* Format string */ va_list ap /* arguments */ ){ @@ -181,42 +203,40 @@ void sqlite3VXPrintf( int idx; /* A general purpose loop counter */ int width; /* Width of the current field */ etByte flag_leftjustify; /* True if "-" flag is present */ - etByte flag_plussign; /* True if "+" flag is present */ - etByte flag_blanksign; /* True if " " flag is present */ + etByte flag_prefix; /* '+' or ' ' or 0 for prefix */ etByte flag_alternateform; /* True if "#" flag is present */ etByte flag_altform2; /* True if "!" flag is present */ etByte flag_zeropad; /* True if field width constant starts with zero */ - etByte flag_long; /* True if "l" flag is present */ - etByte flag_longlong; /* True if the "ll" flag is present */ + etByte flag_long; /* 1 for the "l" flag, 2 for "ll", 0 by default */ etByte done; /* Loop termination flag */ - etByte xtype = 0; /* Conversion paradigm */ + etByte cThousand; /* Thousands separator for %d and %u */ + etByte xtype = etINVALID; /* Conversion paradigm */ u8 bArgList; /* True for SQLITE_PRINTF_SQLFUNC */ - u8 useIntern; /* Ok to use internal conversions (ex: %T) */ char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */ sqlite_uint64 longvalue; /* Value for integer types */ - LONGDOUBLE_TYPE realvalue; /* Value for real types */ + double realvalue; /* Value for real types */ const et_info *infop; /* Pointer to the appropriate info structure */ char *zOut; /* Rendering buffer */ int nOut; /* Size of the rendering buffer */ char *zExtra = 0; /* Malloced memory used by some conversion */ -#ifndef SQLITE_OMIT_FLOATING_POINT - int exp, e2; /* exponent of real numbers */ - int nsd; /* Number of significant digits returned */ - double rounder; /* Used for rounding floating point values */ + int exp, e2; /* exponent of real numbers */ etByte flag_dp; /* True if decimal point should be shown */ etByte flag_rtz; /* True if trailing zeros should be removed */ -#endif + PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */ char buf[etBUFSIZE]; /* Conversion buffer */ + /* pAccum never starts out with an empty buffer that was obtained from + ** malloc(). This precondition is required by the mprintf("%z...") + ** optimization. */ + assert( pAccum->nChar>0 || (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 ); + bufpt = 0; - if( pAccum->printfFlags ){ - if( (bArgList = (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC))!=0 ){ - pArgList = va_arg(ap, PrintfArguments*); - } - useIntern = pAccum->printfFlags & SQLITE_PRINTF_INTERNAL; + if( (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC)!=0 ){ + pArgList = va_arg(ap, PrintfArguments*); + bArgList = 1; }else{ - bArgList = useIntern = 0; + bArgList = 0; } for(; (c=(*fmt))!=0; ++fmt){ if( c!='%' ){ @@ -224,133 +244,160 @@ void sqlite3VXPrintf( #if HAVE_STRCHRNUL fmt = strchrnul(fmt, '%'); #else - do{ fmt++; }while( *fmt && *fmt != '%' ); + fmt = strchr(fmt, '%'); + if( fmt==0 ){ + fmt = bufpt + strlen(bufpt); + } #endif - sqlite3StrAccumAppend(pAccum, bufpt, (int)(fmt - bufpt)); + sqlite3_str_append(pAccum, bufpt, (int)(fmt - bufpt)); if( *fmt==0 ) break; } if( (c=(*++fmt))==0 ){ - sqlite3StrAccumAppend(pAccum, "%", 1); + sqlite3_str_append(pAccum, "%", 1); break; } /* Find out what flags are present */ - flag_leftjustify = flag_plussign = flag_blanksign = + flag_leftjustify = flag_prefix = cThousand = flag_alternateform = flag_altform2 = flag_zeropad = 0; done = 0; + width = 0; + flag_long = 0; + precision = -1; do{ switch( c ){ case '-': flag_leftjustify = 1; break; - case '+': flag_plussign = 1; break; - case ' ': flag_blanksign = 1; break; + case '+': flag_prefix = '+'; break; + case ' ': flag_prefix = ' '; break; case '#': flag_alternateform = 1; break; case '!': flag_altform2 = 1; break; case '0': flag_zeropad = 1; break; + case ',': cThousand = ','; break; default: done = 1; break; - } - }while( !done && (c=(*++fmt))!=0 ); - /* Get the field width */ - if( c=='*' ){ - if( bArgList ){ - width = (int)getIntArg(pArgList); - }else{ - width = va_arg(ap,int); - } - if( width<0 ){ - flag_leftjustify = 1; - width = width >= -2147483647 ? -width : 0; - } - c = *++fmt; - }else{ - unsigned wx = 0; - while( c>='0' && c<='9' ){ - wx = wx*10 + c - '0'; - c = *++fmt; - } - testcase( wx>0x7fffffff ); - width = wx & 0x7fffffff; - } - assert( width>=0 ); + case 'l': { + flag_long = 1; + c = *++fmt; + if( c=='l' ){ + c = *++fmt; + flag_long = 2; + } + done = 1; + break; + } + case '1': case '2': case '3': case '4': case '5': + case '6': case '7': case '8': case '9': { + unsigned wx = c - '0'; + while( (c = *++fmt)>='0' && c<='9' ){ + wx = wx*10 + c - '0'; + } + testcase( wx>0x7fffffff ); + width = wx & 0x7fffffff; #ifdef SQLITE_PRINTF_PRECISION_LIMIT - if( width>SQLITE_PRINTF_PRECISION_LIMIT ){ - width = SQLITE_PRINTF_PRECISION_LIMIT; - } + if( width>SQLITE_PRINTF_PRECISION_LIMIT ){ + width = SQLITE_PRINTF_PRECISION_LIMIT; + } #endif - - /* Get the precision */ - if( c=='.' ){ - c = *++fmt; - if( c=='*' ){ - if( bArgList ){ - precision = (int)getIntArg(pArgList); - }else{ - precision = va_arg(ap,int); + if( c!='.' && c!='l' ){ + done = 1; + }else{ + fmt--; + } + break; } - c = *++fmt; - if( precision<0 ){ - precision = precision >= -2147483647 ? -precision : -1; + case '*': { + if( bArgList ){ + width = (int)getIntArg(pArgList); + }else{ + width = va_arg(ap,int); + } + if( width<0 ){ + flag_leftjustify = 1; + width = width >= -2147483647 ? -width : 0; + } +#ifdef SQLITE_PRINTF_PRECISION_LIMIT + if( width>SQLITE_PRINTF_PRECISION_LIMIT ){ + width = SQLITE_PRINTF_PRECISION_LIMIT; + } +#endif + if( (c = fmt[1])!='.' && c!='l' ){ + c = *++fmt; + done = 1; + } + break; } - }else{ - unsigned px = 0; - while( c>='0' && c<='9' ){ - px = px*10 + c - '0'; + case '.': { c = *++fmt; - } - testcase( px>0x7fffffff ); - precision = px & 0x7fffffff; - } - }else{ - precision = -1; - } - assert( precision>=(-1) ); + if( c=='*' ){ + if( bArgList ){ + precision = (int)getIntArg(pArgList); + }else{ + precision = va_arg(ap,int); + } + if( precision<0 ){ + precision = precision >= -2147483647 ? -precision : -1; + } + c = *++fmt; + }else{ + unsigned px = 0; + while( c>='0' && c<='9' ){ + px = px*10 + c - '0'; + c = *++fmt; + } + testcase( px>0x7fffffff ); + precision = px & 0x7fffffff; + } #ifdef SQLITE_PRINTF_PRECISION_LIMIT - if( precision>SQLITE_PRINTF_PRECISION_LIMIT ){ - precision = SQLITE_PRINTF_PRECISION_LIMIT; - } + if( precision>SQLITE_PRINTF_PRECISION_LIMIT ){ + precision = SQLITE_PRINTF_PRECISION_LIMIT; + } #endif - - - /* Get the conversion type modifier */ - if( c=='l' ){ - flag_long = 1; - c = *++fmt; - if( c=='l' ){ - flag_longlong = 1; - c = *++fmt; - }else{ - flag_longlong = 0; + if( c=='l' ){ + --fmt; + }else{ + done = 1; + } + break; + } } - }else{ - flag_long = flag_longlong = 0; - } + }while( !done && (c=(*++fmt))!=0 ); + /* Fetch the info entry for the field */ +#ifdef SQLITE_EBCDIC + /* The hash table only works for ASCII. For EBCDIC, we need to do + ** a linear search of the table */ infop = &fmtinfo[0]; xtype = etINVALID; for(idx=0; idxflags & FLAG_INTERN)==0 ){ - xtype = infop->type; - }else{ - return; - } + xtype = infop->type; break; } } +#else + /* Fast hash-table lookup */ + assert( ArraySize(fmtinfo)==23 ); + idx = ((unsigned)c) % 23; + if( fmtinfo[idx].fmttype==c + || fmtinfo[idx = fmtinfo[idx].iNxt].fmttype==c + ){ + infop = &fmtinfo[idx]; + xtype = infop->type; + }else{ + infop = &fmtinfo[0]; + xtype = etINVALID; + } +#endif /* ** At this point, variables are initialized as follows: ** ** flag_alternateform TRUE if a '#' is present. ** flag_altform2 TRUE if a '!' is present. - ** flag_plussign TRUE if a '+' is present. + ** flag_prefix '+' or ' ' or zero ** flag_leftjustify TRUE if a '-' is present or if the ** field width was negative. ** flag_zeropad TRUE if the width began with 0. - ** flag_long TRUE if the letter 'l' (ell) prefixed - ** the conversion character. - ** flag_longlong TRUE if the letter 'll' (ell ell) prefixed - ** the conversion character. - ** flag_blanksign TRUE if a ' ' is present. + ** flag_long 1 for "l", 2 for "ll" ** width The specified field width. This is ** always non-negative. Zero is the default. ** precision The specified precision. The default @@ -358,63 +405,77 @@ void sqlite3VXPrintf( ** xtype The class of the conversion. ** infop Pointer to the appropriate info struct. */ + assert( width>=0 ); + assert( precision>=(-1) ); switch( xtype ){ case etPOINTER: - flag_longlong = sizeof(char*)==sizeof(i64); - flag_long = sizeof(char*)==sizeof(long int); - /* Fall through into the next case */ + flag_long = sizeof(char*)==sizeof(i64) ? 2 : + sizeof(char*)==sizeof(long int) ? 1 : 0; + /* no break */ deliberate_fall_through case etORDINAL: - case etRADIX: + case etRADIX: + cThousand = 0; + /* no break */ deliberate_fall_through + case etDECIMAL: if( infop->flags & FLAG_SIGNED ){ i64 v; if( bArgList ){ v = getIntArg(pArgList); - }else if( flag_longlong ){ - v = va_arg(ap,i64); }else if( flag_long ){ - v = va_arg(ap,long int); + if( flag_long==2 ){ + v = va_arg(ap,i64) ; + }else{ + v = va_arg(ap,long int); + } }else{ v = va_arg(ap,int); } if( v<0 ){ - if( v==SMALLEST_INT64 ){ - longvalue = ((u64)1)<<63; - }else{ - longvalue = -v; - } + testcase( v==SMALLEST_INT64 ); + testcase( v==(-1) ); + longvalue = ~v; + longvalue++; prefix = '-'; }else{ longvalue = v; - if( flag_plussign ) prefix = '+'; - else if( flag_blanksign ) prefix = ' '; - else prefix = 0; + prefix = flag_prefix; } }else{ if( bArgList ){ longvalue = (u64)getIntArg(pArgList); - }else if( flag_longlong ){ - longvalue = va_arg(ap,u64); }else if( flag_long ){ - longvalue = va_arg(ap,unsigned long int); + if( flag_long==2 ){ + longvalue = va_arg(ap,u64); + }else{ + longvalue = va_arg(ap,unsigned long int); + } }else{ longvalue = va_arg(ap,unsigned int); } prefix = 0; } + +#if WHERETRACE_ENABLED + if( xtype==etPOINTER && sqlite3WhereTrace & 0x100000 ) longvalue = 0; +#endif +#if TREETRACE_ENABLED + if( xtype==etPOINTER && sqlite3TreeTrace & 0x100000 ) longvalue = 0; +#endif + if( longvalue==0 ) flag_alternateform = 0; if( flag_zeropad && precision0 ); } length = (int)(&zOut[nOut-1]-bufpt); - for(idx=precision-length; idx>0; idx--){ + while( precision>length ){ *(--bufpt) = '0'; /* Zero pad */ + length++; + } + if( cThousand ){ + int nn = (length - 1)/3; /* Number of "," to insert */ + int ix = (length - 1)%3 + 1; + bufpt -= nn; + for(idx=0; nn>0; idx++){ + bufpt[idx] = bufpt[idx+nn]; + ix--; + if( ix==0 ){ + bufpt[++idx] = cThousand; + nn--; + ix = 3; + } + } } if( prefix ) *(--bufpt) = prefix; /* Add sign */ if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */ @@ -449,61 +525,83 @@ void sqlite3VXPrintf( break; case etFLOAT: case etEXP: - case etGENERIC: + case etGENERIC: { + FpDecode s; + int iRound; + int j; + if( bArgList ){ realvalue = getDoubleArg(pArgList); }else{ realvalue = va_arg(ap,double); } -#ifdef SQLITE_OMIT_FLOATING_POINT - length = 0; -#else if( precision<0 ) precision = 6; /* Set default precision */ - if( realvalue<0.0 ){ - realvalue = -realvalue; - prefix = '-'; - }else{ - if( flag_plussign ) prefix = '+'; - else if( flag_blanksign ) prefix = ' '; - else prefix = 0; +#ifdef SQLITE_FP_PRECISION_LIMIT + if( precision>SQLITE_FP_PRECISION_LIMIT ){ + precision = SQLITE_FP_PRECISION_LIMIT; } - if( xtype==etGENERIC && precision>0 ) precision--; - testcase( precision>0xfff ); - for(idx=precision&0xfff, rounder=0.5; idx>0; idx--, rounder*=0.1){} - if( xtype==etFLOAT ) realvalue += rounder; - /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */ - exp = 0; - if( sqlite3IsNaN((double)realvalue) ){ - bufpt = "NaN"; - length = 3; - break; +#endif + if( xtype==etFLOAT ){ + iRound = -precision; + }else if( xtype==etGENERIC ){ + if( precision==0 ) precision = 1; + iRound = precision; + }else{ + iRound = precision+1; } - if( realvalue>0.0 ){ - LONGDOUBLE_TYPE scale = 1.0; - while( realvalue>=1e100*scale && exp<=350 ){ scale *= 1e100;exp+=100;} - while( realvalue>=1e10*scale && exp<=350 ){ scale *= 1e10; exp+=10; } - while( realvalue>=10.0*scale && exp<=350 ){ scale *= 10.0; exp++; } - realvalue /= scale; - while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; } - while( realvalue<1.0 ){ realvalue *= 10.0; exp--; } - if( exp>350 ){ + sqlite3FpDecode(&s, realvalue, iRound, flag_altform2 ? 26 : 16); + if( s.isSpecial ){ + if( s.isSpecial==2 ){ + bufpt = flag_zeropad ? "null" : "NaN"; + length = sqlite3Strlen30(bufpt); + break; + }else if( flag_zeropad ){ + s.z[0] = '9'; + s.iDP = 1000; + s.n = 1; + }else{ + memcpy(buf, "-Inf", 5); bufpt = buf; - buf[0] = prefix; - memcpy(buf+(prefix!=0),"Inf",4); - length = 3+(prefix!=0); + if( s.sign=='-' ){ + /* no-op */ + }else if( flag_prefix ){ + buf[0] = flag_prefix; + }else{ + bufpt++; + } + length = sqlite3Strlen30(bufpt); break; } } - bufpt = buf; + if( s.sign=='-' ){ + if( flag_alternateform + && !flag_prefix + && xtype==etFLOAT + && s.iDP<=iRound + ){ + /* Suppress the minus sign if all of the following are true: + ** * The value displayed is zero + ** * The '#' flag is used + ** * The '+' flag is not used, and + ** * The format is %f + */ + prefix = 0; + }else{ + prefix = '-'; + } + }else{ + prefix = flag_prefix; + } + + exp = s.iDP-1; + /* ** If the field type is etGENERIC, then convert to either etEXP ** or etFLOAT, as appropriate. */ - if( xtype!=etFLOAT ){ - realvalue += rounder; - if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; } - } if( xtype==etGENERIC ){ + assert( precision>0 ); + precision--; flag_rtz = !flag_alternateform; if( exp<-4 || exp>precision ){ xtype = etEXP; @@ -517,29 +615,32 @@ void sqlite3VXPrintf( if( xtype==etEXP ){ e2 = 0; }else{ - e2 = exp; + e2 = s.iDP - 1; } - if( MAX(e2,0)+(i64)precision+(i64)width > etBUFSIZE - 15 ){ - bufpt = zExtra - = sqlite3Malloc( MAX(e2,0)+(i64)precision+(i64)width+15 ); - if( bufpt==0 ){ - setStrAccumError(pAccum, STRACCUM_NOMEM); - return; + bufpt = buf; + { + i64 szBufNeeded; /* Size of a temporary buffer needed */ + szBufNeeded = MAX(e2,0)+(i64)precision+(i64)width+15; + if( cThousand && e2>0 ) szBufNeeded += (e2+2)/3; + if( szBufNeeded > etBUFSIZE ){ + bufpt = zExtra = printfTempBuf(pAccum, szBufNeeded); + if( bufpt==0 ) return; } } zOut = bufpt; - nsd = 16 + flag_altform2*10; flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2; /* The sign in front of the number */ if( prefix ){ *(bufpt++) = prefix; } /* Digits prior to the decimal point */ + j = 0; if( e2<0 ){ *(bufpt++) = '0'; }else{ for(; e2>=0; e2--){ - *(bufpt++) = et_getdigit(&realvalue,&nsd); + *(bufpt++) = j1 ) *(bufpt++) = ','; } } /* The decimal point */ @@ -548,13 +649,12 @@ void sqlite3VXPrintf( } /* "0" digits after the decimal point but before the first ** significant digit of the number */ - for(e2++; e2<0; precision--, e2++){ - assert( precision>0 ); + for(e2++; e2<0 && precision>0; precision--, e2++){ *(bufpt++) = '0'; } /* Significant digits after the decimal point */ while( (precision--)>0 ){ - *(bufpt++) = et_getdigit(&realvalue,&nsd); + *(bufpt++) = jcharset]; if( exp<0 ){ *(bufpt++) = '-'; exp = -exp; @@ -603,8 +704,8 @@ void sqlite3VXPrintf( while( nPad-- ) bufpt[i++] = '0'; length = width; } -#endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */ break; + } case etSIZE: if( !bArgList ){ *(va_arg(ap,int*)) = pAccum->nChar; @@ -619,22 +720,47 @@ void sqlite3VXPrintf( case etCHARX: if( bArgList ){ bufpt = getTextArg(pArgList); - c = bufpt ? bufpt[0] : 0; + length = 1; + if( bufpt ){ + buf[0] = c = *(bufpt++); + if( (c&0xc0)==0xc0 ){ + while( length<4 && (bufpt[0]&0xc0)==0x80 ){ + buf[length++] = *(bufpt++); + } + } + }else{ + buf[0] = 0; + } }else{ - c = va_arg(ap,int); + unsigned int ch = va_arg(ap,unsigned int); + length = sqlite3AppendOneUtf8Character(buf, ch); } if( precision>1 ){ + i64 nPrior = 1; width -= precision-1; if( width>1 && !flag_leftjustify ){ - sqlite3AppendChar(pAccum, width-1, ' '); + sqlite3_str_appendchar(pAccum, width-1, ' '); width = 0; } - sqlite3AppendChar(pAccum, precision-1, c); + sqlite3_str_append(pAccum, buf, length); + precision--; + while( precision > 1 ){ + i64 nCopyBytes; + if( nPrior > precision-1 ) nPrior = precision - 1; + nCopyBytes = length*nPrior; + if( nCopyBytes + pAccum->nChar >= pAccum->nAlloc ){ + sqlite3StrAccumEnlarge(pAccum, nCopyBytes); + } + if( pAccum->accError ) break; + sqlite3_str_append(pAccum, + &pAccum->zText[pAccum->nChar-nCopyBytes], nCopyBytes); + precision -= nPrior; + nPrior *= 2; + } } - length = 1; - buf[0] = c; bufpt = buf; - break; + flag_altform2 = 1; + goto adjust_width_for_utf8; case etSTRING: case etDYNSTRING: if( bArgList ){ @@ -646,80 +772,207 @@ void sqlite3VXPrintf( if( bufpt==0 ){ bufpt = ""; }else if( xtype==etDYNSTRING ){ + if( pAccum->nChar==0 + && pAccum->mxAlloc + && width==0 + && precision<0 + && pAccum->accError==0 + ){ + /* Special optimization for sqlite3_mprintf("%z..."): + ** Extend an existing memory allocation rather than creating + ** a new one. */ + assert( (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 ); + pAccum->zText = bufpt; + pAccum->nAlloc = sqlite3DbMallocSize(pAccum->db, bufpt); + pAccum->nChar = 0x7fffffff & (int)strlen(bufpt); + pAccum->printfFlags |= SQLITE_PRINTF_MALLOCED; + length = 0; + break; + } zExtra = bufpt; } if( precision>=0 ){ - for(length=0; length 0 && z[0] ){ + SQLITE_SKIP_UTF8(z); + } + length = (int)(z - (unsigned char*)bufpt); + }else{ + for(length=0; length0 ){ + /* Adjust width to account for extra bytes in UTF-8 characters */ + int ii = length - 1; + while( ii>=0 ) if( (bufpt[ii--] & 0xc0)==0x80 ) width++; } break; - case etSQLESCAPE: /* Escape ' characters */ - case etSQLESCAPE2: /* Escape ' and enclose in '...' */ - case etSQLESCAPE3: { /* Escape " characters */ - int i, j, k, n, isnull; - int needQuote; + case etESCAPE_q: /* %q: Escape ' characters */ + case etESCAPE_Q: /* %Q: Escape ' and enclose in '...' */ + case etESCAPE_w: { /* %w: Escape " characters */ + i64 i, j, k, n; + int needQuote = 0; char ch; - char q = ((xtype==etSQLESCAPE3)?'"':'\''); /* Quote character */ char *escarg; + char q; if( bArgList ){ escarg = getTextArg(pArgList); }else{ escarg = va_arg(ap,char*); } - isnull = escarg==0; - if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)"); + if( escarg==0 ){ + escarg = (xtype==etESCAPE_Q ? "NULL" : "(NULL)"); + }else if( xtype==etESCAPE_Q ){ + needQuote = 1; + } + if( xtype==etESCAPE_w ){ + q = '"'; + flag_alternateform = 0; + }else{ + q = '\''; + } + /* For %q, %Q, and %w, the precision is the number of bytes (or + ** characters if the ! flags is present) to use from the input. + ** Because of the extra quoting characters inserted, the number + ** of output characters may be larger than the precision. + */ k = precision; for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){ if( ch==q ) n++; + if( flag_altform2 && (ch&0xc0)==0xc0 ){ + while( (escarg[i+1]&0xc0)==0x80 ){ i++; } + } + } + if( flag_alternateform ){ + /* For %#q, do unistr()-style backslash escapes for + ** all control characters, and for backslash itself. + ** For %#Q, do the same but only if there is at least + ** one control character. */ + u32 nBack = 0; + u32 nCtrl = 0; + for(k=0; ketBUFSIZE ){ - bufpt = zExtra = sqlite3Malloc( n ); - if( bufpt==0 ){ - setStrAccumError(pAccum, STRACCUM_NOMEM); - return; - } + bufpt = zExtra = printfTempBuf(pAccum, n); + if( bufpt==0 ) return; }else{ bufpt = buf; } j = 0; - if( needQuote ) bufpt[j++] = q; + if( needQuote ){ + if( needQuote==2 ){ + memcpy(&bufpt[j], "unistr('", 8); + j += 8; + }else{ + bufpt[j++] = '\''; + } + } k = i; - for(i=0; i=0x10 ? '1' : '0'; + bufpt[j++] = "0123456789abcdef"[ch&0xf]; + } + } + }else{ + for(i=0; i=0 && precisionn ){ - sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n); + if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return; + if( flag_alternateform ){ + /* %#T means an Expr pointer that uses Expr.u.zToken */ + Expr *pExpr = va_arg(ap,Expr*); + if( ALWAYS(pExpr) && ALWAYS(!ExprHasProperty(pExpr,EP_IntValue)) ){ + sqlite3_str_appendall(pAccum, (const char*)pExpr->u.zToken); + sqlite3RecordErrorOffsetOfExpr(pAccum->db, pExpr); + } + }else{ + /* %T means a Token pointer */ + Token *pToken = va_arg(ap, Token*); + assert( bArgList==0 ); + if( pToken && pToken->n ){ + sqlite3_str_append(pAccum, (const char*)pToken->z, pToken->n); + sqlite3RecordErrorByteOffset(pAccum->db, pToken->z); + } } length = width = 0; break; } - case etSRCLIST: { - SrcList *pSrc = va_arg(ap, SrcList*); - int k = va_arg(ap, int); - struct SrcList_item *pItem = &pSrc->a[k]; + case etSRCITEM: { + SrcItem *pItem; + if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return; + pItem = va_arg(ap, SrcItem*); assert( bArgList==0 ); - assert( k>=0 && knSrc ); - if( pItem->zDatabase ){ - sqlite3StrAccumAppendAll(pAccum, pItem->zDatabase); - sqlite3StrAccumAppend(pAccum, ".", 1); + if( pItem->zAlias && !flag_altform2 ){ + sqlite3_str_appendall(pAccum, pItem->zAlias); + }else if( pItem->zName ){ + if( pItem->fg.fixedSchema==0 + && pItem->fg.isSubquery==0 + && pItem->u4.zDatabase!=0 + ){ + sqlite3_str_appendall(pAccum, pItem->u4.zDatabase); + sqlite3_str_append(pAccum, ".", 1); + } + sqlite3_str_appendall(pAccum, pItem->zName); + }else if( pItem->zAlias ){ + sqlite3_str_appendall(pAccum, pItem->zAlias); + }else if( ALWAYS(pItem->fg.isSubquery) ){/* Because of tag-20240424-1 */ + Select *pSel = pItem->u4.pSubq->pSelect; + assert( pSel!=0 ); + if( pSel->selFlags & SF_NestedFrom ){ + sqlite3_str_appendf(pAccum, "(join-%u)", pSel->selId); + }else if( pSel->selFlags & SF_MultiValue ){ + assert( !pItem->fg.isTabFunc && !pItem->fg.isIndexedBy ); + sqlite3_str_appendf(pAccum, "%u-ROW VALUES CLAUSE", + pItem->u1.nRow); + }else{ + sqlite3_str_appendf(pAccum, "(subquery-%u)", pSel->selId); + } } - sqlite3StrAccumAppendAll(pAccum, pItem->zName); length = width = 0; break; } @@ -731,12 +984,19 @@ void sqlite3VXPrintf( /* ** The text of the conversion is pointed to by "bufpt" and is ** "length" characters long. The field width is "width". Do - ** the output. + ** the output. Both length and width are in bytes, not characters, + ** at this point. If the "!" flag was present on string conversions + ** indicating that width and precision should be expressed in characters, + ** then the values have been translated prior to reaching this point. */ width -= length; - if( width>0 && !flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' '); - sqlite3StrAccumAppend(pAccum, bufpt, length); - if( width>0 && flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' '); + if( width>0 ){ + if( !flag_leftjustify ) sqlite3_str_appendchar(pAccum, width, ' '); + sqlite3_str_append(pAccum, bufpt, length); + if( flag_leftjustify ) sqlite3_str_appendchar(pAccum, width, ' '); + }else{ + sqlite3_str_append(pAccum, bufpt, length); + } if( zExtra ){ sqlite3DbFree(pAccum->db, zExtra); @@ -745,6 +1005,45 @@ void sqlite3VXPrintf( }/* End for loop over the format string */ } /* End of function */ + +/* +** The z string points to the first character of a token that is +** associated with an error. If db does not already have an error +** byte offset recorded, try to compute the error byte offset for +** z and set the error byte offset in db. +*/ +void sqlite3RecordErrorByteOffset(sqlite3 *db, const char *z){ + const Parse *pParse; + const char *zText; + const char *zEnd; + assert( z!=0 ); + if( NEVER(db==0) ) return; + if( db->errByteOffset!=(-2) ) return; + pParse = db->pParse; + if( NEVER(pParse==0) ) return; + zText =pParse->zTail; + if( NEVER(zText==0) ) return; + zEnd = &zText[strlen(zText)]; + if( SQLITE_WITHIN(z,zText,zEnd) ){ + db->errByteOffset = (int)(z-zText); + } +} + +/* +** If pExpr has a byte offset for the start of a token, record that as +** as the error offset. +*/ +void sqlite3RecordErrorOffsetOfExpr(sqlite3 *db, const Expr *pExpr){ + while( pExpr + && (ExprHasProperty(pExpr,EP_OuterON|EP_InnerON) || pExpr->w.iOfst<=0) + ){ + pExpr = pExpr->pLeft; + } + if( pExpr==0 ) return; + if( ExprHasProperty(pExpr, EP_FromDDL) ) return; + db->errByteOffset = pExpr->w.iOfst; +} + /* ** Enlarge the memory allocation on a StrAccum object so that it is ** able to accept at least N more bytes of text. @@ -752,31 +1051,28 @@ void sqlite3VXPrintf( ** Return the number of bytes of text that StrAccum is able to accept ** after the attempted enlargement. The value returned might be zero. */ -static int sqlite3StrAccumEnlarge(StrAccum *p, int N){ +int sqlite3StrAccumEnlarge(StrAccum *p, i64 N){ char *zNew; - assert( p->nChar+(i64)N >= p->nAlloc ); /* Only called if really needed */ + assert( p->nChar+N >= p->nAlloc ); /* Only called if really needed */ if( p->accError ){ - testcase(p->accError==STRACCUM_TOOBIG); - testcase(p->accError==STRACCUM_NOMEM); + testcase(p->accError==SQLITE_TOOBIG); + testcase(p->accError==SQLITE_NOMEM); return 0; } if( p->mxAlloc==0 ){ - N = p->nAlloc - p->nChar - 1; - setStrAccumError(p, STRACCUM_TOOBIG); - return N; + sqlite3StrAccumSetError(p, SQLITE_TOOBIG); + return p->nAlloc - p->nChar - 1; }else{ char *zOld = isMalloced(p) ? p->zText : 0; - i64 szNew = p->nChar; - assert( (p->zText==0 || p->zText==p->zBase)==!isMalloced(p) ); - szNew += N + 1; + i64 szNew = p->nChar + N + 1; if( szNew+p->nChar<=p->mxAlloc ){ /* Force exponential buffer size growth as long as it does not overflow, ** to avoid having to call this routine too often */ szNew += p->nChar; } if( szNew > p->mxAlloc ){ - sqlite3StrAccumReset(p); - setStrAccumError(p, STRACCUM_TOOBIG); + sqlite3_str_reset(p); + sqlite3StrAccumSetError(p, SQLITE_TOOBIG); return 0; }else{ p->nAlloc = (int)szNew; @@ -784,7 +1080,7 @@ static int sqlite3StrAccumEnlarge(StrAccum *p, int N){ if( p->db ){ zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc); }else{ - zNew = sqlite3_realloc64(zOld, p->nAlloc); + zNew = sqlite3Realloc(zOld, p->nAlloc); } if( zNew ){ assert( p->zText!=0 || p->nChar==0 ); @@ -793,23 +1089,23 @@ static int sqlite3StrAccumEnlarge(StrAccum *p, int N){ p->nAlloc = sqlite3DbMallocSize(p->db, zNew); p->printfFlags |= SQLITE_PRINTF_MALLOCED; }else{ - sqlite3StrAccumReset(p); - setStrAccumError(p, STRACCUM_NOMEM); + sqlite3_str_reset(p); + sqlite3StrAccumSetError(p, SQLITE_NOMEM); return 0; } } - return N; + assert( N>=0 && N<=0x7fffffff ); + return (int)N; } /* ** Append N copies of character c to the given string buffer. */ -void sqlite3AppendChar(StrAccum *p, int N, char c){ +void sqlite3_str_appendchar(sqlite3_str *p, int N, char c){ testcase( p->nChar + (i64)N > 0x7fffffff ); if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){ return; } - assert( (p->zText==p->zBase)==!isMalloced(p) ); while( (N--)>0 ) p->zText[p->nChar++] = c; } @@ -817,9 +1113,9 @@ void sqlite3AppendChar(StrAccum *p, int N, char c){ ** The StrAccum "p" is not large enough to accept N new bytes of z[]. ** So enlarge if first, then do the append. ** -** This is a helper routine to sqlite3StrAccumAppend() that does special-case +** This is a helper routine to sqlite3_str_append() that does special-case ** work (enlarging the buffer) using tail recursion, so that the -** sqlite3StrAccumAppend() routine can use fast calling semantics. +** sqlite3_str_append() routine can use fast calling semantics. */ static void SQLITE_NOINLINE enlargeAndAppend(StrAccum *p, const char *z, int N){ N = sqlite3StrAccumEnlarge(p, N); @@ -827,21 +1123,20 @@ static void SQLITE_NOINLINE enlargeAndAppend(StrAccum *p, const char *z, int N){ memcpy(&p->zText[p->nChar], z, N); p->nChar += N; } - assert( (p->zText==0 || p->zText==p->zBase)==!isMalloced(p) ); } /* ** Append N bytes of text from z to the StrAccum object. Increase the ** size of the memory allocation for StrAccum if necessary. */ -void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){ +void sqlite3_str_append(sqlite3_str *p, const char *z, int N){ assert( z!=0 || N==0 ); assert( p->zText!=0 || p->nChar==0 || p->accError ); assert( N>=0 ); - assert( p->accError==0 || p->nAlloc==0 ); + assert( p->accError==0 || p->nAlloc==0 || p->mxAlloc==0 ); if( p->nChar+N >= p->nAlloc ){ enlargeAndAppend(p,z,N); - }else{ + }else if( N ){ assert( p->zText ); p->nChar += N; memcpy(&p->zText[p->nChar-N], z, N); @@ -851,8 +1146,8 @@ void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){ /* ** Append the complete text of zero-terminated string z[] to the p string. */ -void sqlite3StrAccumAppendAll(StrAccum *p, const char *z){ - sqlite3StrAccumAppend(p, z, sqlite3Strlen30(z)); +void sqlite3_str_appendall(sqlite3_str *p, const char *z){ + sqlite3_str_append(p, z, sqlite3Strlen30(z)); } @@ -861,32 +1156,95 @@ void sqlite3StrAccumAppendAll(StrAccum *p, const char *z){ ** Return a pointer to the resulting string. Return a NULL ** pointer if any kind of error was encountered. */ +static SQLITE_NOINLINE char *strAccumFinishRealloc(StrAccum *p){ + char *zText; + assert( p->mxAlloc>0 && !isMalloced(p) ); + zText = sqlite3DbMallocRaw(p->db, 1+(u64)p->nChar ); + if( zText ){ + memcpy(zText, p->zText, p->nChar+1); + p->printfFlags |= SQLITE_PRINTF_MALLOCED; + }else{ + sqlite3StrAccumSetError(p, SQLITE_NOMEM); + } + p->zText = zText; + return zText; +} char *sqlite3StrAccumFinish(StrAccum *p){ if( p->zText ){ - assert( (p->zText==p->zBase)==!isMalloced(p) ); p->zText[p->nChar] = 0; if( p->mxAlloc>0 && !isMalloced(p) ){ - p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 ); - if( p->zText ){ - memcpy(p->zText, p->zBase, p->nChar+1); - p->printfFlags |= SQLITE_PRINTF_MALLOCED; - }else{ - setStrAccumError(p, STRACCUM_NOMEM); - } + return strAccumFinishRealloc(p); } } return p->zText; } +/* +** Use the content of the StrAccum passed as the second argument +** as the result of an SQL function. +*/ +void sqlite3ResultStrAccum(sqlite3_context *pCtx, StrAccum *p){ + if( p->accError ){ + sqlite3_result_error_code(pCtx, p->accError); + sqlite3_str_reset(p); + }else if( isMalloced(p) ){ + sqlite3_result_text(pCtx, p->zText, p->nChar, SQLITE_DYNAMIC); + }else{ + sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC); + sqlite3_str_reset(p); + } +} + +/* +** This singleton is an sqlite3_str object that is returned if +** sqlite3_malloc() fails to provide space for a real one. This +** sqlite3_str object accepts no new text and always returns +** an SQLITE_NOMEM error. +*/ +static sqlite3_str sqlite3OomStr = { + 0, 0, 0, 0, 0, SQLITE_NOMEM, 0 +}; + +/* Finalize a string created using sqlite3_str_new(). +*/ +char *sqlite3_str_finish(sqlite3_str *p){ + char *z; + if( p!=0 && p!=&sqlite3OomStr ){ + z = sqlite3StrAccumFinish(p); + sqlite3_free(p); + }else{ + z = 0; + } + return z; +} + +/* Return any error code associated with p */ +int sqlite3_str_errcode(sqlite3_str *p){ + return p ? p->accError : SQLITE_NOMEM; +} + +/* Return the current length of p in bytes */ +int sqlite3_str_length(sqlite3_str *p){ + return p ? p->nChar : 0; +} + +/* Return the current value for p */ +char *sqlite3_str_value(sqlite3_str *p){ + if( p==0 || p->nChar==0 ) return 0; + p->zText[p->nChar] = 0; + return p->zText; +} + /* ** Reset an StrAccum string. Reclaim all malloced memory. */ -void sqlite3StrAccumReset(StrAccum *p){ - assert( (p->zText==0 || p->zText==p->zBase)==!isMalloced(p) ); +void sqlite3_str_reset(StrAccum *p){ if( isMalloced(p) ){ sqlite3DbFree(p->db, p->zText); p->printfFlags &= ~SQLITE_PRINTF_MALLOCED; } + p->nAlloc = 0; + p->nChar = 0; p->zText = 0; } @@ -905,15 +1263,27 @@ void sqlite3StrAccumReset(StrAccum *p){ ** allocations will ever occur. */ void sqlite3StrAccumInit(StrAccum *p, sqlite3 *db, char *zBase, int n, int mx){ - p->zText = p->zBase = zBase; + p->zText = zBase; p->db = db; - p->nChar = 0; p->nAlloc = n; p->mxAlloc = mx; + p->nChar = 0; p->accError = 0; p->printfFlags = 0; } +/* Allocate and initialize a new dynamic string object */ +sqlite3_str *sqlite3_str_new(sqlite3 *db){ + sqlite3_str *p = sqlite3_malloc64(sizeof(*p)); + if( p ){ + sqlite3StrAccumInit(p, 0, 0, 0, + db ? db->aLimit[SQLITE_LIMIT_LENGTH] : SQLITE_MAX_LENGTH); + }else{ + p = &sqlite3OomStr; + } + return p; +} + /* ** Print into memory obtained from sqliteMalloc(). Use the internal ** %-conversion extensions. @@ -926,9 +1296,9 @@ char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){ sqlite3StrAccumInit(&acc, db, zBase, sizeof(zBase), db->aLimit[SQLITE_LIMIT_LENGTH]); acc.printfFlags = SQLITE_PRINTF_INTERNAL; - sqlite3VXPrintf(&acc, zFormat, ap); + sqlite3_str_vappendf(&acc, zFormat, ap); z = sqlite3StrAccumFinish(&acc); - if( acc.accError==STRACCUM_NOMEM ){ + if( acc.accError==SQLITE_NOMEM ){ sqlite3OomFault(db); } return z; @@ -966,7 +1336,7 @@ char *sqlite3_vmprintf(const char *zFormat, va_list ap){ if( sqlite3_initialize() ) return 0; #endif sqlite3StrAccumInit(&acc, 0, zBase, sizeof(zBase), SQLITE_MAX_LENGTH); - sqlite3VXPrintf(&acc, zFormat, ap); + sqlite3_str_vappendf(&acc, zFormat, ap); z = sqlite3StrAccumFinish(&acc); return z; } @@ -1011,18 +1381,38 @@ char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){ } #endif sqlite3StrAccumInit(&acc, 0, zBuf, n, 0); - sqlite3VXPrintf(&acc, zFormat, ap); - return sqlite3StrAccumFinish(&acc); + sqlite3_str_vappendf(&acc, zFormat, ap); + zBuf[acc.nChar] = 0; + return zBuf; } char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){ - char *z; + StrAccum acc; va_list ap; + if( n<=0 ) return zBuf; +#ifdef SQLITE_ENABLE_API_ARMOR + if( zBuf==0 || zFormat==0 ) { + (void)SQLITE_MISUSE_BKPT; + if( zBuf ) zBuf[0] = 0; + return zBuf; + } +#endif + sqlite3StrAccumInit(&acc, 0, zBuf, n, 0); va_start(ap,zFormat); - z = sqlite3_vsnprintf(n, zBuf, zFormat, ap); + sqlite3_str_vappendf(&acc, zFormat, ap); va_end(ap); - return z; + zBuf[acc.nChar] = 0; + return zBuf; } +/* Maximum size of an sqlite3_log() message. */ +#if defined(SQLITE_MAX_LOG_MESSAGE) + /* Leave the definition as supplied */ +#elif SQLITE_PRINT_BUF_SIZE*10>10000 +# define SQLITE_MAX_LOG_MESSAGE 10000 +#else +# define SQLITE_MAX_LOG_MESSAGE (SQLITE_PRINT_BUF_SIZE*10) +#endif + /* ** This is the routine that actually formats the sqlite3_log() message. ** We house it in a separate routine from sqlite3_log() to avoid using @@ -1032,17 +1422,17 @@ char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){ ** allocate memory because it might be called while the memory allocator ** mutex is held. ** -** sqlite3VXPrintf() might ask for *temporary* memory allocations for +** sqlite3_str_vappendf() might ask for *temporary* memory allocations for ** certain format characters (%q) or for very large precisions or widths. ** Care must be taken that any sqlite3_log() calls that occur while the ** memory mutex is held do not use these mechanisms. */ static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){ StrAccum acc; /* String accumulator */ - char zMsg[SQLITE_PRINT_BUF_SIZE*3]; /* Complete log message */ + char zMsg[SQLITE_MAX_LOG_MESSAGE]; /* Complete log message */ sqlite3StrAccumInit(&acc, 0, zMsg, sizeof(zMsg), 0); - sqlite3VXPrintf(&acc, zFormat, ap); + sqlite3_str_vappendf(&acc, zFormat, ap); sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode, sqlite3StrAccumFinish(&acc)); } @@ -1068,25 +1458,101 @@ void sqlite3_log(int iErrCode, const char *zFormat, ...){ void sqlite3DebugPrintf(const char *zFormat, ...){ va_list ap; StrAccum acc; - char zBuf[500]; + char zBuf[SQLITE_PRINT_BUF_SIZE*10]; sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0); va_start(ap,zFormat); - sqlite3VXPrintf(&acc, zFormat, ap); + sqlite3_str_vappendf(&acc, zFormat, ap); va_end(ap); sqlite3StrAccumFinish(&acc); +#ifdef SQLITE_OS_TRACE_PROC + { + extern void SQLITE_OS_TRACE_PROC(const char *zBuf, int nBuf); + SQLITE_OS_TRACE_PROC(zBuf, sizeof(zBuf)); + } +#else fprintf(stdout,"%s", zBuf); fflush(stdout); +#endif } #endif /* -** variable-argument wrapper around sqlite3VXPrintf(). The bFlags argument +** variable-argument wrapper around sqlite3_str_vappendf(). The bFlags argument ** can contain the bit SQLITE_PRINTF_INTERNAL enable internal formats. */ -void sqlite3XPrintf(StrAccum *p, const char *zFormat, ...){ +void sqlite3_str_appendf(StrAccum *p, const char *zFormat, ...){ va_list ap; va_start(ap,zFormat); - sqlite3VXPrintf(p, zFormat, ap); + sqlite3_str_vappendf(p, zFormat, ap); va_end(ap); } + + +/***************************************************************************** +** Reference counted string/blob storage +*****************************************************************************/ + +/* +** Increase the reference count of the string by one. +** +** The input parameter is returned. +*/ +char *sqlite3RCStrRef(char *z){ + RCStr *p = (RCStr*)z; + assert( p!=0 ); + p--; + p->nRCRef++; + return z; +} + +/* +** Decrease the reference count by one. Free the string when the +** reference count reaches zero. +*/ +void sqlite3RCStrUnref(void *z){ + RCStr *p = (RCStr*)z; + assert( p!=0 ); + p--; + assert( p->nRCRef>0 ); + if( p->nRCRef>=2 ){ + p->nRCRef--; + }else{ + sqlite3_free(p); + } +} + +/* +** Create a new string that is capable of holding N bytes of text, not counting +** the zero byte at the end. The string is uninitialized. +** +** The reference count is initially 1. Call sqlite3RCStrUnref() to free the +** newly allocated string. +** +** This routine returns 0 on an OOM. +*/ +char *sqlite3RCStrNew(u64 N){ + RCStr *p = sqlite3_malloc64( N + sizeof(*p) + 1 ); + if( p==0 ) return 0; + p->nRCRef = 1; + return (char*)&p[1]; +} + +/* +** Change the size of the string so that it is able to hold N bytes. +** The string might be reallocated, so return the new allocation. +*/ +char *sqlite3RCStrResize(char *z, u64 N){ + RCStr *p = (RCStr*)z; + RCStr *pNew; + assert( p!=0 ); + p--; + assert( p->nRCRef==1 ); + pNew = sqlite3_realloc64(p, N+sizeof(RCStr)+1); + if( pNew==0 ){ + sqlite3_free(p); + return 0; + }else{ + return (char*)&pNew[1]; + } +} diff --git a/src/random.c b/src/random.c index 179d01bef2..ea8431ba94 100644 --- a/src/random.c +++ b/src/random.c @@ -22,16 +22,41 @@ ** This structure is the current state of the generator. */ static SQLITE_WSD struct sqlite3PrngType { - unsigned char isInit; /* True if initialized */ - unsigned char i, j; /* State variables */ - unsigned char s[256]; /* State variables */ + u32 s[16]; /* 64 bytes of chacha20 state */ + u8 out[64]; /* Output bytes */ + u8 n; /* Output bytes remaining */ } sqlite3Prng; + +/* The RFC-7539 ChaCha20 block function +*/ +#define ROTL(a,b) (((a) << (b)) | ((a) >> (32 - (b)))) +#define QR(a, b, c, d) ( \ + a += b, d ^= a, d = ROTL(d,16), \ + c += d, b ^= c, b = ROTL(b,12), \ + a += b, d ^= a, d = ROTL(d, 8), \ + c += d, b ^= c, b = ROTL(b, 7)) +static void chacha_block(u32 *out, const u32 *in){ + int i; + u32 x[16]; + memcpy(x, in, 64); + for(i=0; i<10; i++){ + QR(x[0], x[4], x[ 8], x[12]); + QR(x[1], x[5], x[ 9], x[13]); + QR(x[2], x[6], x[10], x[14]); + QR(x[3], x[7], x[11], x[15]); + QR(x[0], x[5], x[10], x[15]); + QR(x[1], x[6], x[11], x[12]); + QR(x[2], x[7], x[ 8], x[13]); + QR(x[3], x[4], x[ 9], x[14]); + } + for(i=0; i<16; i++) out[i] = x[i]+in[i]; +} + /* ** Return N random bytes. */ void sqlite3_randomness(int N, void *pBuf){ - unsigned char t; unsigned char *zBuf = pBuf; /* The "wsdPrng" macro will resolve to the pseudo-random number generator @@ -61,52 +86,50 @@ void sqlite3_randomness(int N, void *pBuf){ sqlite3_mutex_enter(mutex); if( N<=0 || pBuf==0 ){ - wsdPrng.isInit = 0; + wsdPrng.s[0] = 0; sqlite3_mutex_leave(mutex); return; } /* Initialize the state of the random number generator once, - ** the first time this routine is called. The seed value does - ** not need to contain a lot of randomness since we are not - ** trying to do secure encryption or anything like that... - ** - ** Nothing in this file or anywhere else in SQLite does any kind of - ** encryption. The RC4 algorithm is being used as a PRNG (pseudo-random - ** number generator) not as an encryption device. + ** the first time this routine is called. */ - if( !wsdPrng.isInit ){ - int i; - char k[256]; - wsdPrng.j = 0; - wsdPrng.i = 0; - sqlite3OsRandomness(sqlite3_vfs_find(0), 256, k); - for(i=0; i<256; i++){ - wsdPrng.s[i] = (u8)i; + if( wsdPrng.s[0]==0 ){ + sqlite3_vfs *pVfs = sqlite3_vfs_find(0); + static const u32 chacha20_init[] = { + 0x61707865, 0x3320646e, 0x79622d32, 0x6b206574 + }; + memcpy(&wsdPrng.s[0], chacha20_init, 16); + if( NEVER(pVfs==0) ){ + memset(&wsdPrng.s[4], 0, 44); + }else{ + sqlite3OsRandomness(pVfs, 44, (char*)&wsdPrng.s[4]); } - for(i=0; i<256; i++){ - wsdPrng.j += wsdPrng.s[i] + k[i]; - t = wsdPrng.s[wsdPrng.j]; - wsdPrng.s[wsdPrng.j] = wsdPrng.s[i]; - wsdPrng.s[i] = t; - } - wsdPrng.isInit = 1; + wsdPrng.s[15] = wsdPrng.s[12]; + wsdPrng.s[12] = 0; + wsdPrng.n = 0; } assert( N>0 ); - do{ - wsdPrng.i++; - t = wsdPrng.s[wsdPrng.i]; - wsdPrng.j += t; - wsdPrng.s[wsdPrng.i] = wsdPrng.s[wsdPrng.j]; - wsdPrng.s[wsdPrng.j] = t; - t += wsdPrng.s[wsdPrng.i]; - *(zBuf++) = wsdPrng.s[t]; - }while( --N ); + while( 1 /* exit by break */ ){ + if( N<=wsdPrng.n ){ + memcpy(zBuf, &wsdPrng.out[wsdPrng.n-N], N); + wsdPrng.n -= N; + break; + } + if( wsdPrng.n>0 ){ + memcpy(zBuf, wsdPrng.out, wsdPrng.n); + N -= wsdPrng.n; + zBuf += wsdPrng.n; + } + wsdPrng.s[12]++; + chacha_block((u32*)wsdPrng.out, wsdPrng.s); + wsdPrng.n = 64; + } sqlite3_mutex_leave(mutex); } -#ifndef SQLITE_OMIT_BUILTIN_TEST +#ifndef SQLITE_UNTESTABLE /* ** For testing purposes, we sometimes want to preserve the state of ** PRNG and restore the PRNG to its saved state at a later time, or @@ -131,4 +154,4 @@ void sqlite3PrngRestoreState(void){ sizeof(sqlite3Prng) ); } -#endif /* SQLITE_OMIT_BUILTIN_TEST */ +#endif /* SQLITE_UNTESTABLE */ diff --git a/src/resolve.c b/src/resolve.c index 81bb712a2e..16c193ca23 100644 --- a/src/resolve.c +++ b/src/resolve.c @@ -15,8 +15,11 @@ ** table and column. */ #include "sqliteInt.h" -#include -#include + +/* +** Magic table number to mean the EXCLUDED table in an UPSERT statement. +*/ +#define EXCLUDED_TABLE_NUMBER 2 /* ** Walk the expression tree pExpr and increase the aggregate function @@ -26,6 +29,8 @@ ** ** incrAggFunctionDepth(pExpr,n) is the main routine. incrAggDepth(..) ** is a helper function - a callback for the tree walker. +** +** See also the sqlite3WindowExtraAggFuncDepth() routine in window.c */ static int incrAggDepth(Walker *pWalker, Expr *pExpr){ if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.n; @@ -65,7 +70,6 @@ static void resolveAlias( ExprList *pEList, /* A result set */ int iCol, /* A column in the result set. 0..pEList->nExpr-1 */ Expr *pExpr, /* Transform this into an alias to the result set */ - const char *zType, /* "GROUP" or "ORDER" or "" */ int nSubquery /* Number of subqueries that the label is moving */ ){ Expr *pOrig; /* The iCol-th column of the result set */ @@ -75,64 +79,64 @@ static void resolveAlias( assert( iCol>=0 && iColnExpr ); pOrig = pEList->a[iCol].pExpr; assert( pOrig!=0 ); + assert( !ExprHasProperty(pExpr, EP_Reduced|EP_TokenOnly) ); + if( pExpr->pAggInfo ) return; db = pParse->db; pDup = sqlite3ExprDup(db, pOrig, 0); - if( pDup==0 ) return; - if( zType[0]!='G' ) incrAggFunctionDepth(pDup, nSubquery); - if( pExpr->op==TK_COLLATE ){ - pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken); - } - ExprSetProperty(pDup, EP_Alias); - - /* Before calling sqlite3ExprDelete(), set the EP_Static flag. This - ** prevents ExprDelete() from deleting the Expr structure itself, - ** allowing it to be repopulated by the memcpy() on the following line. - ** The pExpr->u.zToken might point into memory that will be freed by the - ** sqlite3DbFree(db, pDup) on the last line of this block, so be sure to - ** make a copy of the token before doing the sqlite3DbFree(). - */ - ExprSetProperty(pExpr, EP_Static); - sqlite3ExprDelete(db, pExpr); - memcpy(pExpr, pDup, sizeof(*pExpr)); - if( !ExprHasProperty(pExpr, EP_IntValue) && pExpr->u.zToken!=0 ){ - assert( (pExpr->flags & (EP_Reduced|EP_TokenOnly))==0 ); - pExpr->u.zToken = sqlite3DbStrDup(db, pExpr->u.zToken); - pExpr->flags |= EP_MemToken; - } - sqlite3DbFree(db, pDup); -} - - -/* -** Return TRUE if the name zCol occurs anywhere in the USING clause. -** -** Return FALSE if the USING clause is NULL or if it does not contain -** zCol. -*/ -static int nameInUsingClause(IdList *pUsing, const char *zCol){ - if( pUsing ){ - int k; - for(k=0; knId; k++){ - if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ) return 1; + if( db->mallocFailed ){ + sqlite3ExprDelete(db, pDup); + pDup = 0; + }else{ + Expr temp; + incrAggFunctionDepth(pDup, nSubquery); + if( pExpr->op==TK_COLLATE ){ + assert( !ExprHasProperty(pExpr, EP_IntValue) ); + pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken); + } + memcpy(&temp, pDup, sizeof(Expr)); + memcpy(pDup, pExpr, sizeof(Expr)); + memcpy(pExpr, &temp, sizeof(Expr)); + if( ExprHasProperty(pExpr, EP_WinFunc) ){ + if( ALWAYS(pExpr->y.pWin!=0) ){ + pExpr->y.pWin->pOwner = pExpr; + } } + sqlite3ExprDeferredDelete(pParse, pDup); } - return 0; } /* -** Subqueries stores the original database, table and column names for their -** result sets in ExprList.a[].zSpan, in the form "DATABASE.TABLE.COLUMN". -** Check to see if the zSpan given to this routine matches the zDb, zTab, -** and zCol. If any of zDb, zTab, and zCol are NULL then those fields will -** match anything. +** Subqueries store the original database, table and column names for their +** result sets in ExprList.a[].zSpan, in the form "DATABASE.TABLE.COLUMN", +** and mark the expression-list item by setting ExprList.a[].fg.eEName +** to ENAME_TAB. +** +** Check to see if the zSpan/eEName of the expression-list item passed to this +** routine matches the zDb, zTab, and zCol. If any of zDb, zTab, and zCol are +** NULL then those fields will match anything. Return true if there is a match, +** or false otherwise. +** +** SF_NestedFrom subqueries also store an entry for the implicit rowid (or +** _rowid_, or oid) column by setting ExprList.a[].fg.eEName to ENAME_ROWID, +** and setting zSpan to "DATABASE.TABLE.". This type of pItem +** argument matches if zCol is a rowid alias. If it is not NULL, (*pbRowid) +** is set to 1 if there is this kind of match. */ -int sqlite3MatchSpanName( - const char *zSpan, +int sqlite3MatchEName( + const struct ExprList_item *pItem, const char *zCol, const char *zTab, - const char *zDb + const char *zDb, + int *pbRowid ){ int n; + const char *zSpan; + int eEName = pItem->fg.eEName; + if( eEName!=ENAME_TAB && (eEName!=ENAME_ROWID || NEVER(pbRowid==0)) ){ + return 0; + } + assert( pbRowid==0 || *pbRowid==0 ); + zSpan = pItem->zEName; for(n=0; ALWAYS(zSpan[n]) && zSpan[n]!='.'; n++){} if( zDb && (sqlite3StrNICmp(zSpan, zDb, n)!=0 || zDb[n]!=0) ){ return 0; @@ -143,15 +147,110 @@ int sqlite3MatchSpanName( return 0; } zSpan += n+1; - if( zCol && sqlite3StrICmp(zSpan, zCol)!=0 ){ - return 0; + if( zCol ){ + if( eEName==ENAME_TAB && sqlite3StrICmp(zSpan, zCol)!=0 ) return 0; + if( eEName==ENAME_ROWID && sqlite3IsRowid(zCol)==0 ) return 0; } + if( eEName==ENAME_ROWID ) *pbRowid = 1; return 1; } +/* +** Return TRUE if the double-quoted string mis-feature should be supported. +*/ +static int areDoubleQuotedStringsEnabled(sqlite3 *db, NameContext *pTopNC){ + if( db->init.busy ) return 1; /* Always support for legacy schemas */ + if( pTopNC->ncFlags & NC_IsDDL ){ + /* Currently parsing a DDL statement */ + if( sqlite3WritableSchema(db) && (db->flags & SQLITE_DqsDML)!=0 ){ + return 1; + } + return (db->flags & SQLITE_DqsDDL)!=0; + }else{ + /* Currently parsing a DML statement */ + return (db->flags & SQLITE_DqsDML)!=0; + } +} + +/* +** The argument is guaranteed to be a non-NULL Expr node of type TK_COLUMN. +** return the appropriate colUsed mask. +*/ +Bitmask sqlite3ExprColUsed(Expr *pExpr){ + int n; + Table *pExTab; + + n = pExpr->iColumn; + assert( ExprUseYTab(pExpr) ); + pExTab = pExpr->y.pTab; + assert( pExTab!=0 ); + assert( n < pExTab->nCol ); + if( (pExTab->tabFlags & TF_HasGenerated)!=0 + && (pExTab->aCol[n].colFlags & COLFLAG_GENERATED)!=0 + ){ + testcase( pExTab->nCol==BMS-1 ); + testcase( pExTab->nCol==BMS ); + return pExTab->nCol>=BMS ? ALLBITS : MASKBIT(pExTab->nCol)-1; + }else{ + testcase( n==BMS-1 ); + testcase( n==BMS ); + if( n>=BMS ) n = BMS-1; + return ((Bitmask)1)<db, TK_COLUMN, 0, 0); + if( pNew ){ + pNew->iTable = pMatch->iCursor; + pNew->iColumn = iColumn; + pNew->y.pTab = pMatch->pSTab; + assert( (pMatch->fg.jointype & (JT_LEFT|JT_LTORJ))!=0 ); + ExprSetProperty(pNew, EP_CanBeNull); + *ppList = sqlite3ExprListAppend(pParse, *ppList, pNew); + } +} + +/* +** Return TRUE (non-zero) if zTab is a valid name for the schema table pTab. +*/ +static SQLITE_NOINLINE int isValidSchemaTableName( + const char *zTab, /* Name as it appears in the SQL */ + Table *pTab, /* The schema table we are trying to match */ + const char *zDb /* non-NULL if a database qualifier is present */ +){ + const char *zLegacy; + assert( pTab!=0 ); + assert( pTab->tnum==1 ); + if( sqlite3StrNICmp(zTab, "sqlite_", 7)!=0 ) return 0; + zLegacy = pTab->zName; + if( strcmp(zLegacy+7, &LEGACY_TEMP_SCHEMA_TABLE[7])==0 ){ + if( sqlite3StrICmp(zTab+7, &PREFERRED_TEMP_SCHEMA_TABLE[7])==0 ){ + return 1; + } + if( zDb==0 ) return 0; + if( sqlite3StrICmp(zTab+7, &LEGACY_SCHEMA_TABLE[7])==0 ) return 1; + if( sqlite3StrICmp(zTab+7, &PREFERRED_SCHEMA_TABLE[7])==0 ) return 1; + }else{ + if( sqlite3StrICmp(zTab+7, &PREFERRED_SCHEMA_TABLE[7])==0 ) return 1; + } + return 0; +} + /* ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up -** that name in the set of source tables in pSrcList and make the pExpr +** that name in the set of source tables in pSrcList and make the pExpr ** expression node refer back to that source column. The following changes ** are made to pExpr: ** @@ -159,7 +258,7 @@ int sqlite3MatchSpanName( ** (even if X is implied). ** pExpr->iTable Set to the cursor number for the table obtained ** from pSrcList. -** pExpr->pTab Points to the Table structure of X.Y (even if +** pExpr->y.pTab Points to the Table structure of X.Y (even if ** X and/or Y are implied.) ** pExpr->iColumn Set to the column number within the table. ** pExpr->op Set to TK_COLUMN. @@ -180,30 +279,31 @@ static int lookupName( Parse *pParse, /* The parsing context */ const char *zDb, /* Name of the database containing table, or NULL */ const char *zTab, /* Name of table containing column, or NULL */ - const char *zCol, /* Name of the column. */ + const Expr *pRight, /* Name of the column. */ NameContext *pNC, /* The name context used to resolve the name */ Expr *pExpr /* Make this EXPR node point to the selected column */ ){ int i, j; /* Loop counters */ int cnt = 0; /* Number of matching column names */ - int cntTab = 0; /* Number of matching table names */ + int cntTab = 0; /* Number of potential "rowid" matches */ int nSubquery = 0; /* How many levels of subquery */ sqlite3 *db = pParse->db; /* The database connection */ - struct SrcList_item *pItem; /* Use for looping over pSrcList items */ - struct SrcList_item *pMatch = 0; /* The matching pSrcList item */ + SrcItem *pItem; /* Use for looping over pSrcList items */ + SrcItem *pMatch = 0; /* The matching pSrcList item */ NameContext *pTopNC = pNC; /* First namecontext in the list */ Schema *pSchema = 0; /* Schema of the expression */ - int isTrigger = 0; /* True if resolved to a trigger column */ - Table *pTab = 0; /* Table hold the row */ - Column *pCol; /* A column of pTab */ + int eNewExprOp = TK_COLUMN; /* New value for pExpr->op on success */ + Table *pTab = 0; /* Table holding the row */ + ExprList *pFJMatch = 0; /* Matches for FULL JOIN .. USING */ + const char *zCol = pRight->u.zToken; assert( pNC ); /* the name context cannot be NULL. */ assert( zCol ); /* The Z in X.Y.Z cannot be NULL */ + assert( zDb==0 || zTab!=0 ); assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) ); /* Initialize the node to no-match */ pExpr->iTable = -1; - pExpr->pTab = 0; ExprSetVVAProperty(pExpr, EP_NoReduce); /* Translate the schema name in zDb into a pointer to the corresponding @@ -221,149 +321,320 @@ static int lookupName( zDb = 0; }else{ for(i=0; inDb; i++){ - assert( db->aDb[i].zName ); - if( sqlite3StrICmp(db->aDb[i].zName,zDb)==0 ){ + assert( db->aDb[i].zDbSName ); + if( sqlite3StrICmp(db->aDb[i].zDbSName,zDb)==0 ){ pSchema = db->aDb[i].pSchema; break; } } + if( i==db->nDb && sqlite3StrICmp("main", zDb)==0 ){ + /* This branch is taken when the main database has been renamed + ** using SQLITE_DBCONFIG_MAINDBNAME. */ + pSchema = db->aDb[0].pSchema; + zDb = db->aDb[0].zDbSName; + } } } /* Start at the inner-most context and move outward until a match is found */ - while( pNC && cnt==0 ){ + assert( pNC && cnt==0 ); + do{ ExprList *pEList; SrcList *pSrcList = pNC->pSrcList; if( pSrcList ){ for(i=0, pItem=pSrcList->a; inSrc; i++, pItem++){ - pTab = pItem->pTab; + pTab = pItem->pSTab; assert( pTab!=0 && pTab->zName!=0 ); - assert( pTab->nCol>0 ); - if( pItem->pSelect && (pItem->pSelect->selFlags & SF_NestedFrom)!=0 ){ + assert( pTab->nCol>0 || pParse->nErr ); + assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem)); + if( pItem->fg.isNestedFrom ){ + /* In this case, pItem is a subquery that has been formed from a + ** parenthesized subset of the FROM clause terms. Example: + ** .... FROM t1 LEFT JOIN (t2 RIGHT JOIN t3 USING(x)) USING(y) ... + ** \_________________________/ + ** This pItem -------------^ + */ int hit = 0; - pEList = pItem->pSelect->pEList; + Select *pSel; + assert( pItem->fg.isSubquery ); + assert( pItem->u4.pSubq!=0 ); + pSel = pItem->u4.pSubq->pSelect; + assert( pSel!=0 ); + pEList = pSel->pEList; + assert( pEList!=0 ); + assert( pEList->nExpr==pTab->nCol ); for(j=0; jnExpr; j++){ - if( sqlite3MatchSpanName(pEList->a[j].zSpan, zCol, zTab, zDb) ){ + int bRowid = 0; /* True if possible rowid match */ + if( !sqlite3MatchEName(&pEList->a[j], zCol, zTab, zDb, &bRowid) ){ + continue; + } + if( bRowid==0 ){ + if( cnt>0 ){ + if( pItem->fg.isUsing==0 + || sqlite3IdListIndex(pItem->u3.pUsing, zCol)<0 + || pMatch==pItem + ){ + /* Two or more tables have the same column name which is + ** not joined by USING. Or, a single table has two columns + ** that match a USING term (if pMatch==pItem). These are both + ** "ambiguous column name" errors. Signal as much by clearing + ** pFJMatch and letting cnt go above 1. */ + sqlite3ExprListDelete(db, pFJMatch); + pFJMatch = 0; + }else + if( (pItem->fg.jointype & JT_RIGHT)==0 ){ + /* An INNER or LEFT JOIN. Use the left-most table */ + continue; + }else + if( (pItem->fg.jointype & JT_LEFT)==0 ){ + /* A RIGHT JOIN. Use the right-most table */ + cnt = 0; + sqlite3ExprListDelete(db, pFJMatch); + pFJMatch = 0; + }else{ + /* For a FULL JOIN, we must construct a coalesce() func */ + extendFJMatch(pParse, &pFJMatch, pMatch, pExpr->iColumn); + } + } cnt++; - cntTab = 2; - pMatch = pItem; - pExpr->iColumn = j; hit = 1; + }else if( cnt>0 ){ + /* This is a potential rowid match, but there has already been + ** a real match found. So this can be ignored. */ + continue; } + cntTab++; + pMatch = pItem; + pExpr->iColumn = j; + pEList->a[j].fg.bUsed = 1; + + /* rowid cannot be part of a USING clause - assert() this. */ + assert( bRowid==0 || pEList->a[j].fg.bUsingTerm==0 ); + if( pEList->a[j].fg.bUsingTerm ) break; } if( hit || zTab==0 ) continue; } - if( zDb && pTab->pSchema!=pSchema ){ - continue; - } + assert( zDb==0 || zTab!=0 ); if( zTab ){ - const char *zTabName = pItem->zAlias ? pItem->zAlias : pTab->zName; - assert( zTabName!=0 ); - if( sqlite3StrICmp(zTabName, zTab)!=0 ){ - continue; + if( zDb ){ + if( pTab->pSchema!=pSchema ) continue; + if( pSchema==0 && strcmp(zDb,"*")!=0 ) continue; + } + if( pItem->zAlias!=0 ){ + if( sqlite3StrICmp(zTab, pItem->zAlias)!=0 ){ + continue; + } + }else if( sqlite3StrICmp(zTab, pTab->zName)!=0 ){ + if( pTab->tnum!=1 ) continue; + if( !isValidSchemaTableName(zTab, pTab, zDb) ) continue; + } + assert( ExprUseYTab(pExpr) ); + if( IN_RENAME_OBJECT && pItem->zAlias ){ + sqlite3RenameTokenRemap(pParse, 0, (void*)&pExpr->y.pTab); } } - if( 0==(cntTab++) ){ + j = sqlite3ColumnIndex(pTab, zCol); + if( j>=0 ){ + if( cnt>0 ){ + if( pItem->fg.isUsing==0 + || sqlite3IdListIndex(pItem->u3.pUsing, zCol)<0 + ){ + /* Two or more tables have the same column name which is + ** not joined by USING. This is an error. Signal as much + ** by clearing pFJMatch and letting cnt go above 1. */ + sqlite3ExprListDelete(db, pFJMatch); + pFJMatch = 0; + }else + if( (pItem->fg.jointype & JT_RIGHT)==0 ){ + /* An INNER or LEFT JOIN. Use the left-most table */ + continue; + }else + if( (pItem->fg.jointype & JT_LEFT)==0 ){ + /* A RIGHT JOIN. Use the right-most table */ + cnt = 0; + sqlite3ExprListDelete(db, pFJMatch); + pFJMatch = 0; + }else{ + /* For a FULL JOIN, we must construct a coalesce() func */ + extendFJMatch(pParse, &pFJMatch, pMatch, pExpr->iColumn); + } + } + cnt++; pMatch = pItem; + /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */ + pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j; + if( pItem->fg.isNestedFrom ){ + sqlite3SrcItemColumnUsed(pItem, j); + } } - for(j=0, pCol=pTab->aCol; jnCol; j++, pCol++){ - if( sqlite3StrICmp(pCol->zName, zCol)==0 ){ - /* If there has been exactly one prior match and this match - ** is for the right-hand table of a NATURAL JOIN or is in a - ** USING clause, then skip this match. - */ - if( cnt==1 ){ - if( pItem->fg.jointype & JT_NATURAL ) continue; - if( nameInUsingClause(pItem->pUsing, zCol) ) continue; - } - cnt++; + if( 0==cnt && VisibleRowid(pTab) ){ + /* pTab is a potential ROWID match. Keep track of it and match + ** the ROWID later if that seems appropriate. (Search for "cntTab" + ** to find related code.) Only allow a ROWID match if there is + ** a single ROWID match candidate. + */ +#ifdef SQLITE_ALLOW_ROWID_IN_VIEW + /* In SQLITE_ALLOW_ROWID_IN_VIEW mode, allow a ROWID match + ** if there is a single VIEW candidate or if there is a single + ** non-VIEW candidate plus multiple VIEW candidates. In other + ** words non-VIEW candidate terms take precedence over VIEWs. + */ + if( cntTab==0 + || (cntTab==1 + && pMatch!=0 + && ALWAYS(pMatch->pSTab!=0) + && (pMatch->pSTab->tabFlags & TF_Ephemeral)!=0 + && (pTab->tabFlags & TF_Ephemeral)==0) + ){ + cntTab = 1; pMatch = pItem; - /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */ - pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j; - break; + }else{ + cntTab++; } +#else + /* The (much more common) non-SQLITE_ALLOW_ROWID_IN_VIEW case is + ** simpler since we require exactly one candidate, which will + ** always be a non-VIEW + */ + cntTab++; + pMatch = pItem; +#endif } } if( pMatch ){ pExpr->iTable = pMatch->iCursor; - pExpr->pTab = pMatch->pTab; - /* RIGHT JOIN not (yet) supported */ - assert( (pMatch->fg.jointype & JT_RIGHT)==0 ); - if( (pMatch->fg.jointype & JT_LEFT)!=0 ){ + assert( ExprUseYTab(pExpr) ); + pExpr->y.pTab = pMatch->pSTab; + if( (pMatch->fg.jointype & (JT_LEFT|JT_LTORJ))!=0 ){ ExprSetProperty(pExpr, EP_CanBeNull); } - pSchema = pExpr->pTab->pSchema; + pSchema = pExpr->y.pTab->pSchema; } } /* if( pSrcList ) */ -#ifndef SQLITE_OMIT_TRIGGER - /* If we have not already resolved the name, then maybe - ** it is a new.* or old.* trigger argument reference +#if !defined(SQLITE_OMIT_TRIGGER) || !defined(SQLITE_OMIT_UPSERT) + /* If we have not already resolved the name, then maybe + ** it is a new.* or old.* trigger argument reference. Or + ** maybe it is an excluded.* from an upsert. Or maybe it is + ** a reference in the RETURNING clause to a table being modified. */ - if( zDb==0 && zTab!=0 && cntTab==0 && pParse->pTriggerTab!=0 ){ - int op = pParse->eTriggerOp; - assert( op==TK_DELETE || op==TK_UPDATE || op==TK_INSERT ); - if( op!=TK_DELETE && sqlite3StrICmp("new",zTab) == 0 ){ - pExpr->iTable = 1; - pTab = pParse->pTriggerTab; - }else if( op!=TK_INSERT && sqlite3StrICmp("old",zTab)==0 ){ - pExpr->iTable = 0; - pTab = pParse->pTriggerTab; - }else{ - pTab = 0; + if( cnt==0 && zDb==0 ){ + pTab = 0; +#ifndef SQLITE_OMIT_TRIGGER + if( pParse->pTriggerTab!=0 ){ + int op = pParse->eTriggerOp; + assert( op==TK_DELETE || op==TK_UPDATE || op==TK_INSERT ); + if( pParse->bReturning ){ + if( (pNC->ncFlags & NC_UBaseReg)!=0 + && ALWAYS(zTab==0 + || sqlite3StrICmp(zTab,pParse->pTriggerTab->zName)==0 + || isValidSchemaTableName(zTab, pParse->pTriggerTab, 0)) + ){ + pExpr->iTable = op!=TK_DELETE; + pTab = pParse->pTriggerTab; + } + }else if( op!=TK_DELETE && zTab && sqlite3StrICmp("new",zTab) == 0 ){ + pExpr->iTable = 1; + pTab = pParse->pTriggerTab; + }else if( op!=TK_INSERT && zTab && sqlite3StrICmp("old",zTab)==0 ){ + pExpr->iTable = 0; + pTab = pParse->pTriggerTab; + } } +#endif /* SQLITE_OMIT_TRIGGER */ +#ifndef SQLITE_OMIT_UPSERT + if( (pNC->ncFlags & NC_UUpsert)!=0 && zTab!=0 ){ + Upsert *pUpsert = pNC->uNC.pUpsert; + if( pUpsert && sqlite3StrICmp("excluded",zTab)==0 ){ + pTab = pUpsert->pUpsertSrc->a[0].pSTab; + pExpr->iTable = EXCLUDED_TABLE_NUMBER; + } + } +#endif /* SQLITE_OMIT_UPSERT */ - if( pTab ){ + if( pTab ){ int iCol; pSchema = pTab->pSchema; cntTab++; - for(iCol=0, pCol=pTab->aCol; iColnCol; iCol++, pCol++){ - if( sqlite3StrICmp(pCol->zName, zCol)==0 ){ - if( iCol==pTab->iPKey ){ - iCol = -1; - } - break; + iCol = sqlite3ColumnIndex(pTab, zCol); + if( iCol>=0 ){ + if( pTab->iPKey==iCol ) iCol = -1; + }else{ + if( sqlite3IsRowid(zCol) && VisibleRowid(pTab) ){ + iCol = -1; + }else{ + iCol = pTab->nCol; } } - if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) && VisibleRowid(pTab) ){ - /* IMP: R-51414-32910 */ - iCol = -1; - } if( iColnCol ){ cnt++; - if( iCol<0 ){ - pExpr->affinity = SQLITE_AFF_INTEGER; - }else if( pExpr->iTable==0 ){ - testcase( iCol==31 ); - testcase( iCol==32 ); - pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<iTable==EXCLUDED_TABLE_NUMBER ){ + testcase( iCol==(-1) ); + assert( ExprUseYTab(pExpr) ); + if( IN_RENAME_OBJECT ){ + pExpr->iColumn = iCol; + pExpr->y.pTab = pTab; + eNewExprOp = TK_COLUMN; + }else{ + pExpr->iTable = pNC->uNC.pUpsert->regData + + sqlite3TableColumnToStorage(pTab, iCol); + eNewExprOp = TK_REGISTER; + } + }else +#endif /* SQLITE_OMIT_UPSERT */ + { + assert( ExprUseYTab(pExpr) ); + pExpr->y.pTab = pTab; + if( pParse->bReturning ){ + eNewExprOp = TK_REGISTER; + pExpr->op2 = TK_COLUMN; + pExpr->iColumn = iCol; + pExpr->iTable = pNC->uNC.iBaseReg + (pTab->nCol+1)*pExpr->iTable + + sqlite3TableColumnToStorage(pTab, iCol) + 1; + }else{ + pExpr->iColumn = (i16)iCol; + eNewExprOp = TK_TRIGGER; +#ifndef SQLITE_OMIT_TRIGGER + if( iCol<0 ){ + pExpr->affExpr = SQLITE_AFF_INTEGER; + }else if( pExpr->iTable==0 ){ + testcase( iCol==31 ); + testcase( iCol==32 ); + pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<iColumn = (i16)iCol; - pExpr->pTab = pTab; - isTrigger = 1; } } } -#endif /* !defined(SQLITE_OMIT_TRIGGER) */ +#endif /* !defined(SQLITE_OMIT_TRIGGER) || !defined(SQLITE_OMIT_UPSERT) */ /* ** Perhaps the name is a reference to the ROWID */ if( cnt==0 - && cntTab==1 + && cntTab>=1 && pMatch - && (pNC->ncFlags & NC_IdxExpr)==0 + && (pNC->ncFlags & (NC_IdxExpr|NC_GenCol))==0 && sqlite3IsRowid(zCol) - && VisibleRowid(pMatch->pTab) + && ALWAYS(VisibleRowid(pMatch->pSTab) || pMatch->fg.isNestedFrom) ){ - cnt = 1; - pExpr->iColumn = -1; - pExpr->affinity = SQLITE_AFF_INTEGER; + cnt = cntTab; +#if SQLITE_ALLOW_ROWID_IN_VIEW+0==2 + if( pMatch->pSTab!=0 && IsView(pMatch->pSTab) ){ + eNewExprOp = TK_NULL; + } +#endif + if( pMatch->fg.isNestedFrom==0 ) pExpr->iColumn = -1; + pExpr->affExpr = SQLITE_AFF_INTEGER; } /* @@ -384,39 +655,56 @@ static int lookupName( ** is supported for backwards compatibility only. Hence, we issue a warning ** on sqlite3_log() whenever the capability is used. */ - if( (pEList = pNC->pEList)!=0 + if( cnt==0 + && (pNC->ncFlags & NC_UEList)!=0 && zTab==0 - && cnt==0 ){ + pEList = pNC->uNC.pEList; + assert( pEList!=0 ); for(j=0; jnExpr; j++){ - char *zAs = pEList->a[j].zName; - if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){ + char *zAs = pEList->a[j].zEName; + if( pEList->a[j].fg.eEName==ENAME_NAME + && sqlite3_stricmp(zAs, zCol)==0 + ){ Expr *pOrig; assert( pExpr->pLeft==0 && pExpr->pRight==0 ); - assert( pExpr->x.pList==0 ); - assert( pExpr->x.pSelect==0 ); + assert( ExprUseXList(pExpr)==0 || pExpr->x.pList==0 ); + assert( ExprUseXSelect(pExpr)==0 || pExpr->x.pSelect==0 ); pOrig = pEList->a[j].pExpr; if( (pNC->ncFlags&NC_AllowAgg)==0 && ExprHasProperty(pOrig, EP_Agg) ){ sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs); return WRC_Abort; } - resolveAlias(pParse, pEList, j, pExpr, "", nSubquery); + if( ExprHasProperty(pOrig, EP_Win) + && ((pNC->ncFlags&NC_AllowWin)==0 || pNC!=pTopNC ) + ){ + sqlite3ErrorMsg(pParse, "misuse of aliased window function %s",zAs); + return WRC_Abort; + } + if( sqlite3ExprVectorSize(pOrig)!=1 ){ + sqlite3ErrorMsg(pParse, "row value misused"); + return WRC_Abort; + } + resolveAlias(pParse, pEList, j, pExpr, nSubquery); cnt = 1; pMatch = 0; assert( zTab==0 && zDb==0 ); + if( IN_RENAME_OBJECT ){ + sqlite3RenameTokenRemap(pParse, 0, (void*)pExpr); + } goto lookupname_end; } - } + } } /* Advance to the next name context. The loop will exit when either ** we have a match (cnt>0) or when we run out of name contexts. */ - if( cnt==0 ){ - pNC = pNC->pNext; - nSubquery++; - } - } + if( cnt ) break; + pNC = pNC->pNext; + nSubquery++; + }while( pNC ); + /* ** If X and Y are NULL (in other words if only the column name Z is @@ -428,59 +716,132 @@ static int lookupName( ** Because no reference was made to outer contexts, the pNC->nRef ** fields are not changed in any context. */ - if( cnt==0 && zTab==0 && ExprHasProperty(pExpr,EP_DblQuoted) ){ - pExpr->op = TK_STRING; - pExpr->pTab = 0; - return WRC_Prune; + if( cnt==0 && zTab==0 ){ + assert( pExpr->op==TK_ID ); + if( ExprHasProperty(pExpr,EP_DblQuoted) + && areDoubleQuotedStringsEnabled(db, pTopNC) + ){ + /* If a double-quoted identifier does not match any known column name, + ** then treat it as a string. + ** + ** This hack was added in the early days of SQLite in a misguided attempt + ** to be compatible with MySQL 3.x, which used double-quotes for strings. + ** I now sorely regret putting in this hack. The effect of this hack is + ** that misspelled identifier names are silently converted into strings + ** rather than causing an error, to the frustration of countless + ** programmers. To all those frustrated programmers, my apologies. + ** + ** Someday, I hope to get rid of this hack. Unfortunately there is + ** a huge amount of legacy SQL that uses it. So for now, we just + ** issue a warning. + */ + sqlite3_log(SQLITE_WARNING, + "double-quoted string literal: \"%w\"", zCol); +#ifdef SQLITE_ENABLE_NORMALIZE + sqlite3VdbeAddDblquoteStr(db, pParse->pVdbe, zCol); +#endif + pExpr->op = TK_STRING; + memset(&pExpr->y, 0, sizeof(pExpr->y)); + return WRC_Prune; + } + if( sqlite3ExprIdToTrueFalse(pExpr) ){ + return WRC_Prune; + } } /* - ** cnt==0 means there was not match. cnt>1 means there were two or - ** more matches. Either way, we have an error. + ** cnt==0 means there was not match. + ** cnt>1 means there were two or more matches. + ** + ** cnt==0 is always an error. cnt>1 is often an error, but might + ** be multiple matches for a NATURAL LEFT JOIN or a LEFT JOIN USING. */ + assert( pFJMatch==0 || cnt>0 ); + assert( !ExprHasProperty(pExpr, EP_xIsSelect|EP_IntValue) ); if( cnt!=1 ){ const char *zErr; + if( pFJMatch ){ + if( pFJMatch->nExpr==cnt-1 ){ + if( ExprHasProperty(pExpr,EP_Leaf) ){ + ExprClearProperty(pExpr,EP_Leaf); + }else{ + sqlite3ExprDelete(db, pExpr->pLeft); + pExpr->pLeft = 0; + sqlite3ExprDelete(db, pExpr->pRight); + pExpr->pRight = 0; + } + extendFJMatch(pParse, &pFJMatch, pMatch, pExpr->iColumn); + pExpr->op = TK_FUNCTION; + pExpr->u.zToken = "coalesce"; + pExpr->x.pList = pFJMatch; + cnt = 1; + goto lookupname_end; + }else{ + sqlite3ExprListDelete(db, pFJMatch); + pFJMatch = 0; + } + } zErr = cnt==0 ? "no such column" : "ambiguous column name"; if( zDb ){ sqlite3ErrorMsg(pParse, "%s: %s.%s.%s", zErr, zDb, zTab, zCol); }else if( zTab ){ sqlite3ErrorMsg(pParse, "%s: %s.%s", zErr, zTab, zCol); + }else if( cnt==0 && ExprHasProperty(pRight,EP_DblQuoted) ){ + sqlite3ErrorMsg(pParse, "%s: \"%s\" - should this be a" + " string literal in single-quotes?", + zErr, zCol); }else{ sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol); } + sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr); pParse->checkSchema = 1; - pTopNC->nErr++; + pTopNC->nNcErr++; + eNewExprOp = TK_NULL; + } + assert( pFJMatch==0 ); + + /* Remove all substructure from pExpr */ + if( !ExprHasProperty(pExpr,(EP_TokenOnly|EP_Leaf)) ){ + sqlite3ExprDelete(db, pExpr->pLeft); + pExpr->pLeft = 0; + sqlite3ExprDelete(db, pExpr->pRight); + pExpr->pRight = 0; + ExprSetProperty(pExpr, EP_Leaf); } /* If a column from a table in pSrcList is referenced, then record ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes - ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the - ** column number is greater than the number of bits in the bitmask - ** then set the high-order bit of the bitmask. + ** bit 0 to be set. Column 1 sets bit 1. And so forth. Bit 63 is + ** set if the 63rd or any subsequent column is used. + ** + ** The colUsed mask is an optimization used to help determine if an + ** index is a covering index. The correct answer is still obtained + ** if the mask contains extra set bits. However, it is important to + ** avoid setting bits beyond the maximum column number of the table. + ** (See ticket [b92e5e8ec2cdbaa1]). + ** + ** If a generated column is referenced, set bits for every column + ** of the table. */ - if( pExpr->iColumn>=0 && pMatch!=0 ){ - int n = pExpr->iColumn; - testcase( n==BMS-1 ); - if( n>=BMS ){ - n = BMS-1; + if( pMatch ){ + if( pExpr->iColumn>=0 ){ + pMatch->colUsed |= sqlite3ExprColUsed(pExpr); + }else{ + pMatch->fg.rowidUsed = 1; } - assert( pMatch->iCursor==pExpr->iTable ); - pMatch->colUsed |= ((Bitmask)1)<pLeft); - pExpr->pLeft = 0; - sqlite3ExprDelete(db, pExpr->pRight); - pExpr->pRight = 0; - pExpr->op = (isTrigger ? TK_TRIGGER : TK_COLUMN); + pExpr->op = eNewExprOp; lookupname_end: if( cnt==1 ){ assert( pNC!=0 ); - if( !ExprHasProperty(pExpr, EP_Alias) ){ +#ifndef SQLITE_OMIT_AUTHORIZATION + if( pParse->db->xAuth + && (pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER) + ){ sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList); } +#endif /* Increment the nRef value on all name contexts from TopNC up to ** the point where the name matched. */ for(;;){ @@ -502,18 +863,27 @@ static int lookupName( Expr *sqlite3CreateColumnExpr(sqlite3 *db, SrcList *pSrc, int iSrc, int iCol){ Expr *p = sqlite3ExprAlloc(db, TK_COLUMN, 0, 0); if( p ){ - struct SrcList_item *pItem = &pSrc->a[iSrc]; - p->pTab = pItem->pTab; + SrcItem *pItem = &pSrc->a[iSrc]; + Table *pTab; + assert( ExprUseYTab(p) ); + pTab = p->y.pTab = pItem->pSTab; p->iTable = pItem->iCursor; - if( p->pTab->iPKey==iCol ){ + if( p->y.pTab->iPKey==iCol ){ p->iColumn = -1; }else{ p->iColumn = (ynVar)iCol; - testcase( iCol==BMS ); - testcase( iCol==BMS-1 ); - pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol); + if( (pTab->tabFlags & TF_HasGenerated)!=0 + && (pTab->aCol[iCol].colFlags & COLFLAG_GENERATED)!=0 + ){ + testcase( pTab->nCol==63 ); + testcase( pTab->nCol==64 ); + pItem->colUsed = pTab->nCol>=64 ? ALLBITS : MASKBIT(pTab->nCol)-1; + }else{ + testcase( iCol==BMS ); + testcase( iCol==BMS-1 ); + pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol); + } } - ExprSetProperty(p, EP_Resolved); } return p; } @@ -521,32 +891,51 @@ Expr *sqlite3CreateColumnExpr(sqlite3 *db, SrcList *pSrc, int iSrc, int iCol){ /* ** Report an error that an expression is not valid for some set of ** pNC->ncFlags values determined by validMask. +** +** static void notValid( +** Parse *pParse, // Leave error message here +** NameContext *pNC, // The name context +** const char *zMsg, // Type of error +** int validMask, // Set of contexts for which prohibited +** Expr *pExpr // Invalidate this expression on error +** ){...} +** +** As an optimization, since the conditional is almost always false +** (because errors are rare), the conditional is moved outside of the +** function call using a macro. */ -static void notValid( - Parse *pParse, /* Leave error message here */ - NameContext *pNC, /* The name context */ - const char *zMsg, /* Type of error */ - int validMask /* Set of contexts for which prohibited */ +static void notValidImpl( + Parse *pParse, /* Leave error message here */ + NameContext *pNC, /* The name context */ + const char *zMsg, /* Type of error */ + Expr *pExpr, /* Invalidate this expression on error */ + Expr *pError /* Associate error with this expression */ ){ - assert( (validMask&~(NC_IsCheck|NC_PartIdx|NC_IdxExpr))==0 ); - if( (pNC->ncFlags & validMask)!=0 ){ - const char *zIn = "partial index WHERE clauses"; - if( pNC->ncFlags & NC_IdxExpr ) zIn = "index expressions"; + const char *zIn = "partial index WHERE clauses"; + if( pNC->ncFlags & NC_IdxExpr ) zIn = "index expressions"; #ifndef SQLITE_OMIT_CHECK - else if( pNC->ncFlags & NC_IsCheck ) zIn = "CHECK constraints"; + else if( pNC->ncFlags & NC_IsCheck ) zIn = "CHECK constraints"; #endif - sqlite3ErrorMsg(pParse, "%s prohibited in %s", zMsg, zIn); - } +#ifndef SQLITE_OMIT_GENERATED_COLUMNS + else if( pNC->ncFlags & NC_GenCol ) zIn = "generated columns"; +#endif + sqlite3ErrorMsg(pParse, "%s prohibited in %s", zMsg, zIn); + if( pExpr ) pExpr->op = TK_NULL; + sqlite3RecordErrorOffsetOfExpr(pParse->db, pError); } +#define sqlite3ResolveNotValid(P,N,M,X,E,R) \ + assert( ((X)&~(NC_IsCheck|NC_PartIdx|NC_IdxExpr|NC_GenCol))==0 ); \ + if( ((N)->ncFlags & (X))!=0 ) notValidImpl(P,N,M,E,R); /* ** Expression p should encode a floating point value between 1.0 and 0.0. -** Return 1024 times this value. Or return -1 if p is not a floating point -** value between 1.0 and 0.0. +** Return 134,217,728 (2^27) times this value. Or return -1 if p is not +** a floating point value between 1.0 and 0.0. */ static int exprProbability(Expr *p){ double r = -1.0; if( p->op!=TK_FLOAT ) return -1; + assert( !ExprHasProperty(p, EP_IntValue) ); sqlite3AtoF(p->u.zToken, &r, sqlite3Strlen30(p->u.zToken), SQLITE_UTF8); assert( r>=0.0 ); if( r>1.0 ) return -1; @@ -573,8 +962,6 @@ static int resolveExprStep(Walker *pWalker, Expr *pExpr){ pParse = pNC->pParse; assert( pParse==pWalker->pParse ); - if( ExprHasProperty(pExpr, EP_Resolved) ) return WRC_Prune; - ExprSetProperty(pExpr, EP_Resolved); #ifndef NDEBUG if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){ SrcList *pSrcList = pNC->pSrcList; @@ -586,79 +973,163 @@ static int resolveExprStep(Walker *pWalker, Expr *pExpr){ #endif switch( pExpr->op ){ -#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY) /* The special operator TK_ROW means use the rowid for the first ** column in the FROM clause. This is used by the LIMIT and ORDER BY - ** clause processing on UPDATE and DELETE statements. + ** clause processing on UPDATE and DELETE statements, and by + ** UPDATE ... FROM statement processing. */ case TK_ROW: { SrcList *pSrcList = pNC->pSrcList; - struct SrcList_item *pItem; - assert( pSrcList && pSrcList->nSrc==1 ); - pItem = pSrcList->a; + SrcItem *pItem; + assert( pSrcList && pSrcList->nSrc>=1 ); + pItem = pSrcList->a; pExpr->op = TK_COLUMN; - pExpr->pTab = pItem->pTab; + assert( ExprUseYTab(pExpr) ); + pExpr->y.pTab = pItem->pSTab; pExpr->iTable = pItem->iCursor; - pExpr->iColumn = -1; - pExpr->affinity = SQLITE_AFF_INTEGER; + pExpr->iColumn--; + pExpr->affExpr = SQLITE_AFF_INTEGER; break; } -#endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) - && !defined(SQLITE_OMIT_SUBQUERY) */ - /* A lone identifier is the name of a column. + /* An optimization: Attempt to convert + ** + ** "expr IS NOT NULL" --> "TRUE" + ** "expr IS NULL" --> "FALSE" + ** + ** if we can prove that "expr" is never NULL. Call this the + ** "NOT NULL strength reduction optimization". + ** + ** If this optimization occurs, also restore the NameContext ref-counts + ** to the state they where in before the "column" LHS expression was + ** resolved. This prevents "column" from being counted as having been + ** referenced, which might prevent a SELECT from being erroneously + ** marked as correlated. + ** + ** 2024-03-28: Beware of aggregates. A bare column of aggregated table + ** can still evaluate to NULL even though it is marked as NOT NULL. + ** Example: + ** + ** CREATE TABLE t1(a INT NOT NULL); + ** SELECT a, a IS NULL, a IS NOT NULL, count(*) FROM t1; + ** + ** The "a IS NULL" and "a IS NOT NULL" expressions cannot be optimized + ** here because at the time this case is hit, we do not yet know whether + ** or not t1 is being aggregated. We have to assume the worst and omit + ** the optimization. The only time it is safe to apply this optimization + ** is within the WHERE clause. */ - case TK_ID: { - return lookupName(pParse, 0, 0, pExpr->u.zToken, pNC, pExpr); + case TK_NOTNULL: + case TK_ISNULL: { + int anRef[8]; + NameContext *p; + int i; + for(i=0, p=pNC; p && ipNext, i++){ + anRef[i] = p->nRef; + } + sqlite3WalkExpr(pWalker, pExpr->pLeft); + if( IN_RENAME_OBJECT ) return WRC_Prune; + if( sqlite3ExprCanBeNull(pExpr->pLeft) ){ + /* The expression can be NULL. So the optimization does not apply */ + return WRC_Prune; + } + + for(i=0, p=pNC; p; p=p->pNext, i++){ + if( (p->ncFlags & NC_Where)==0 ){ + return WRC_Prune; /* Not in a WHERE clause. Unsafe to optimize. */ + } + } + testcase( ExprHasProperty(pExpr, EP_OuterON) ); + assert( !ExprHasProperty(pExpr, EP_IntValue) ); +#if TREETRACE_ENABLED + if( sqlite3TreeTrace & 0x80000 ){ + sqlite3DebugPrintf( + "NOT NULL strength reduction converts the following to %d:\n", + pExpr->op==TK_NOTNULL + ); + sqlite3ShowExpr(pExpr); + } +#endif /* TREETRACE_ENABLED */ + pExpr->u.iValue = (pExpr->op==TK_NOTNULL); + pExpr->flags |= EP_IntValue; + pExpr->op = TK_INTEGER; + for(i=0, p=pNC; p && ipNext, i++){ + p->nRef = anRef[i]; + } + sqlite3ExprDelete(pParse->db, pExpr->pLeft); + pExpr->pLeft = 0; + return WRC_Prune; } - - /* A table name and column name: ID.ID + + /* A column name: ID + ** Or table name and column name: ID.ID ** Or a database, table and column: ID.ID.ID + ** + ** The TK_ID and TK_OUT cases are combined so that there will only + ** be one call to lookupName(). Then the compiler will in-line + ** lookupName() for a size reduction and performance increase. */ + case TK_ID: case TK_DOT: { - const char *zColumn; const char *zTable; const char *zDb; Expr *pRight; - /* if( pSrcList==0 ) break; */ - notValid(pParse, pNC, "the \".\" operator", NC_IdxExpr); - /*notValid(pParse, pNC, "the \".\" operator", NC_PartIdx|NC_IsCheck, 1);*/ - pRight = pExpr->pRight; - if( pRight->op==TK_ID ){ + if( pExpr->op==TK_ID ){ zDb = 0; - zTable = pExpr->pLeft->u.zToken; - zColumn = pRight->u.zToken; + zTable = 0; + assert( !ExprHasProperty(pExpr, EP_IntValue) ); + pRight = pExpr; }else{ - assert( pRight->op==TK_DOT ); - zDb = pExpr->pLeft->u.zToken; - zTable = pRight->pLeft->u.zToken; - zColumn = pRight->pRight->u.zToken; + Expr *pLeft = pExpr->pLeft; + testcase( pNC->ncFlags & NC_IdxExpr ); + testcase( pNC->ncFlags & NC_GenCol ); + sqlite3ResolveNotValid(pParse, pNC, "the \".\" operator", + NC_IdxExpr|NC_GenCol, 0, pExpr); + pRight = pExpr->pRight; + if( pRight->op==TK_ID ){ + zDb = 0; + }else{ + assert( pRight->op==TK_DOT ); + assert( !ExprHasProperty(pRight, EP_IntValue) ); + zDb = pLeft->u.zToken; + pLeft = pRight->pLeft; + pRight = pRight->pRight; + } + assert( ExprUseUToken(pLeft) && ExprUseUToken(pRight) ); + zTable = pLeft->u.zToken; + assert( ExprUseYTab(pExpr) ); + if( IN_RENAME_OBJECT ){ + sqlite3RenameTokenRemap(pParse, (void*)pExpr, (void*)pRight); + sqlite3RenameTokenRemap(pParse, (void*)&pExpr->y.pTab, (void*)pLeft); + } } - return lookupName(pParse, zDb, zTable, zColumn, pNC, pExpr); + return lookupName(pParse, zDb, zTable, pRight, pNC, pExpr); } /* Resolve function names */ case TK_FUNCTION: { - ExprList *pList = pExpr->x.pList; /* The argument list */ - int n = pList ? pList->nExpr : 0; /* Number of arguments */ + ExprList *pList; /* The argument list */ + int n; /* Number of arguments */ int no_such_func = 0; /* True if no such function exists */ int wrong_num_args = 0; /* True if wrong number of arguments */ int is_agg = 0; /* True if is an aggregate function */ - int auth; /* Authorization to use the function */ - int nId; /* Number of characters in function name */ const char *zId; /* The function name. */ FuncDef *pDef; /* Information about the function */ u8 enc = ENC(pParse->db); /* The database encoding */ - - assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); - notValid(pParse, pNC, "functions", NC_PartIdx); + int savedAllowFlags = (pNC->ncFlags & (NC_AllowAgg | NC_AllowWin)); +#ifndef SQLITE_OMIT_WINDOWFUNC + Window *pWin = (IsWindowFunc(pExpr) ? pExpr->y.pWin : 0); +#endif + assert( !ExprHasProperty(pExpr, EP_xIsSelect|EP_IntValue) ); + assert( pExpr->pLeft==0 || pExpr->pLeft->op==TK_ORDER ); + pList = pExpr->x.pList; + n = pList ? pList->nExpr : 0; zId = pExpr->u.zToken; - nId = sqlite3Strlen30(zId); - pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0); + pDef = sqlite3FindFunction(pParse->db, zId, n, enc, 0); if( pDef==0 ){ - pDef = sqlite3FindFunction(pParse->db, zId, nId, -2, enc, 0); + pDef = sqlite3FindFunction(pParse->db, zId, -2, enc, 0); if( pDef==0 ){ no_such_func = 1; }else{ @@ -667,14 +1138,14 @@ static int resolveExprStep(Walker *pWalker, Expr *pExpr){ }else{ is_agg = pDef->xFinalize!=0; if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){ - ExprSetProperty(pExpr, EP_Unlikely|EP_Skip); + ExprSetProperty(pExpr, EP_Unlikely); if( n==2 ){ pExpr->iTable = exprProbability(pList->a[1].pExpr); if( pExpr->iTable<0 ){ sqlite3ErrorMsg(pParse, - "second argument to likelihood() must be a " - "constant between 0.0 and 1.0"); - pNC->nErr++; + "second argument to %#T() must be a " + "constant between 0.0 and 1.0", pExpr); + pNC->nNcErr++; } }else{ /* EVIDENCE-OF: R-61304-29449 The unlikely(X) function is @@ -687,92 +1158,303 @@ static int resolveExprStep(Walker *pWalker, Expr *pExpr){ ** to likelihood(X,0.9375). */ /* TUNING: unlikely() probability is 0.0625. likely() is 0.9375 */ pExpr->iTable = pDef->zName[0]=='u' ? 8388608 : 125829120; - } + } } #ifndef SQLITE_OMIT_AUTHORIZATION - auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0); - if( auth!=SQLITE_OK ){ - if( auth==SQLITE_DENY ){ - sqlite3ErrorMsg(pParse, "not authorized to use function: %s", - pDef->zName); - pNC->nErr++; + { + int auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0,pDef->zName,0); + if( auth!=SQLITE_OK ){ + if( auth==SQLITE_DENY ){ + sqlite3ErrorMsg(pParse, "not authorized to use function: %#T", + pExpr); + pNC->nNcErr++; + } + pExpr->op = TK_NULL; + return WRC_Prune; } - pExpr->op = TK_NULL; - return WRC_Prune; } #endif + + /* If the function may call sqlite3_value_subtype(), then set the + ** EP_SubtArg flag on all of its argument expressions. This prevents + ** where.c from replacing the expression with a value read from an + ** index on the same expression, which will not have the correct + ** subtype. Also set the flag if the function expression itself is + ** an EP_SubtArg expression. In this case subtypes are required as + ** the function may return a value with a subtype back to its + ** caller using sqlite3_result_value(). */ + if( (pDef->funcFlags & SQLITE_SUBTYPE) + || ExprHasProperty(pExpr, EP_SubtArg) + ){ + int ii; + for(ii=0; iia[ii].pExpr, EP_SubtArg); + } + } + if( pDef->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG) ){ /* For the purposes of the EP_ConstFunc flag, date and time ** functions and other functions that change slowly are considered - ** constant because they are constant for the duration of one query */ + ** constant because they are constant for the duration of one query. + ** This allows them to be factored out of inner loops. */ ExprSetProperty(pExpr,EP_ConstFunc); } if( (pDef->funcFlags & SQLITE_FUNC_CONSTANT)==0 ){ - /* Date/time functions that use 'now', and other functions like + /* Clearly non-deterministic functions like random(), but also + ** date/time functions that use 'now', and other functions like ** sqlite_version() that might change over time cannot be used - ** in an index. */ - notValid(pParse, pNC, "non-deterministic functions", NC_IdxExpr); + ** in an index or generated column. Curiously, they can be used + ** in a CHECK constraint. SQLServer, MySQL, and PostgreSQL all + ** allow this. */ + sqlite3ResolveNotValid(pParse, pNC, "non-deterministic functions", + NC_IdxExpr|NC_PartIdx|NC_GenCol, 0, pExpr); + }else{ + assert( (NC_SelfRef & 0xff)==NC_SelfRef ); /* Must fit in 8 bits */ + pExpr->op2 = pNC->ncFlags & NC_SelfRef; + } + if( (pDef->funcFlags & SQLITE_FUNC_INTERNAL)!=0 + && pParse->nested==0 + && (pParse->db->mDbFlags & DBFLAG_InternalFunc)==0 + ){ + /* Internal-use-only functions are disallowed unless the + ** SQL is being compiled using sqlite3NestedParse() or + ** the SQLITE_TESTCTRL_INTERNAL_FUNCTIONS test-control has be + ** used to activate internal functions for testing purposes */ + no_such_func = 1; + pDef = 0; + }else + if( (pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE))!=0 + && !IN_RENAME_OBJECT + ){ + if( pNC->ncFlags & NC_FromDDL ) ExprSetProperty(pExpr, EP_FromDDL); + sqlite3ExprFunctionUsable(pParse, pExpr, pDef); } } - if( is_agg && (pNC->ncFlags & NC_AllowAgg)==0 ){ - sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId); - pNC->nErr++; - is_agg = 0; - }else if( no_such_func && pParse->db->init.busy==0 ){ - sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId); - pNC->nErr++; - }else if( wrong_num_args ){ - sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()", - nId, zId); - pNC->nErr++; + + if( 0==IN_RENAME_OBJECT ){ +#ifndef SQLITE_OMIT_WINDOWFUNC + assert( is_agg==0 || (pDef->funcFlags & SQLITE_FUNC_MINMAX) + || (pDef->xValue==0 && pDef->xInverse==0) + || (pDef->xValue && pDef->xInverse && pDef->xSFunc && pDef->xFinalize) + ); + if( pDef && pDef->xValue==0 && pWin ){ + sqlite3ErrorMsg(pParse, + "%#T() may not be used as a window function", pExpr + ); + pNC->nNcErr++; + }else if( + (is_agg && (pNC->ncFlags & NC_AllowAgg)==0) + || (is_agg && (pDef->funcFlags&SQLITE_FUNC_WINDOW) && !pWin) + || (is_agg && pWin && (pNC->ncFlags & NC_AllowWin)==0) + ){ + const char *zType; + if( (pDef->funcFlags & SQLITE_FUNC_WINDOW) || pWin ){ + zType = "window"; + }else{ + zType = "aggregate"; + } + sqlite3ErrorMsg(pParse, "misuse of %s function %#T()",zType,pExpr); + pNC->nNcErr++; + is_agg = 0; + } +#else + if( (is_agg && (pNC->ncFlags & NC_AllowAgg)==0) ){ + sqlite3ErrorMsg(pParse,"misuse of aggregate function %#T()",pExpr); + pNC->nNcErr++; + is_agg = 0; + } +#endif + else if( no_such_func && pParse->db->init.busy==0 +#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION + && pParse->explain==0 +#endif + ){ + sqlite3ErrorMsg(pParse, "no such function: %#T", pExpr); + pNC->nNcErr++; + }else if( wrong_num_args ){ + sqlite3ErrorMsg(pParse,"wrong number of arguments to function %#T()", + pExpr); + pNC->nNcErr++; + } +#ifndef SQLITE_OMIT_WINDOWFUNC + else if( is_agg==0 && ExprHasProperty(pExpr, EP_WinFunc) ){ + sqlite3ErrorMsg(pParse, + "FILTER may not be used with non-aggregate %#T()", + pExpr + ); + pNC->nNcErr++; + } +#endif + else if( is_agg==0 && pExpr->pLeft ){ + sqlite3ExprOrderByAggregateError(pParse, pExpr); + pNC->nNcErr++; + } + if( is_agg ){ + /* Window functions may not be arguments of aggregate functions. + ** Or arguments of other window functions. But aggregate functions + ** may be arguments for window functions. */ +#ifndef SQLITE_OMIT_WINDOWFUNC + pNC->ncFlags &= ~(NC_AllowWin | (!pWin ? NC_AllowAgg : 0)); +#else + pNC->ncFlags &= ~NC_AllowAgg; +#endif + } + } + else if( ExprHasProperty(pExpr, EP_WinFunc) || pExpr->pLeft ){ + is_agg = 1; } - if( is_agg ) pNC->ncFlags &= ~NC_AllowAgg; sqlite3WalkExprList(pWalker, pList); if( is_agg ){ - NameContext *pNC2 = pNC; - pExpr->op = TK_AGG_FUNCTION; - pExpr->op2 = 0; - while( pNC2 && !sqlite3FunctionUsesThisSrc(pExpr, pNC2->pSrcList) ){ - pExpr->op2++; - pNC2 = pNC2->pNext; + if( pExpr->pLeft ){ + assert( pExpr->pLeft->op==TK_ORDER ); + assert( ExprUseXList(pExpr->pLeft) ); + sqlite3WalkExprList(pWalker, pExpr->pLeft->x.pList); } - assert( pDef!=0 ); - if( pNC2 ){ - assert( SQLITE_FUNC_MINMAX==NC_MinMaxAgg ); - testcase( (pDef->funcFlags & SQLITE_FUNC_MINMAX)!=0 ); - pNC2->ncFlags |= NC_HasAgg | (pDef->funcFlags & SQLITE_FUNC_MINMAX); - +#ifndef SQLITE_OMIT_WINDOWFUNC + if( pWin && pParse->nErr==0 ){ + Select *pSel = pNC->pWinSelect; + assert( ExprUseYWin(pExpr) && pWin==pExpr->y.pWin ); + if( IN_RENAME_OBJECT==0 ){ + sqlite3WindowUpdate(pParse, pSel ? pSel->pWinDefn : 0, pWin, pDef); + if( pParse->db->mallocFailed ) break; + } + sqlite3WalkExprList(pWalker, pWin->pPartition); + sqlite3WalkExprList(pWalker, pWin->pOrderBy); + sqlite3WalkExpr(pWalker, pWin->pFilter); + sqlite3WindowLink(pSel, pWin); + pNC->ncFlags |= NC_HasWin; + }else +#endif /* SQLITE_OMIT_WINDOWFUNC */ + { + NameContext *pNC2; /* For looping up thru outer contexts */ + pExpr->op = TK_AGG_FUNCTION; + pExpr->op2 = 0; +#ifndef SQLITE_OMIT_WINDOWFUNC + if( ExprHasProperty(pExpr, EP_WinFunc) ){ + sqlite3WalkExpr(pWalker, pExpr->y.pWin->pFilter); + } +#endif + pNC2 = pNC; + while( pNC2 + && sqlite3ReferencesSrcList(pParse, pExpr, pNC2->pSrcList)==0 + ){ + pExpr->op2 += (1 + pNC2->nNestedSelect); + pNC2 = pNC2->pNext; + } + assert( pDef!=0 || IN_RENAME_OBJECT ); + if( pNC2 && pDef ){ + pExpr->op2 += pNC2->nNestedSelect; + assert( SQLITE_FUNC_MINMAX==NC_MinMaxAgg ); + assert( SQLITE_FUNC_ANYORDER==NC_OrderAgg ); + testcase( (pDef->funcFlags & SQLITE_FUNC_MINMAX)!=0 ); + testcase( (pDef->funcFlags & SQLITE_FUNC_ANYORDER)!=0 ); + pNC2->ncFlags |= NC_HasAgg + | ((pDef->funcFlags^SQLITE_FUNC_ANYORDER) + & (SQLITE_FUNC_MINMAX|SQLITE_FUNC_ANYORDER)); + } } - pNC->ncFlags |= NC_AllowAgg; + pNC->ncFlags |= savedAllowFlags; } /* FIX ME: Compute pExpr->affinity based on the expected return - ** type of the function + ** type of the function */ return WRC_Prune; } #ifndef SQLITE_OMIT_SUBQUERY + case TK_EXISTS: case TK_SELECT: - case TK_EXISTS: testcase( pExpr->op==TK_EXISTS ); #endif case TK_IN: { testcase( pExpr->op==TK_IN ); - if( ExprHasProperty(pExpr, EP_xIsSelect) ){ + testcase( pExpr->op==TK_EXISTS ); + testcase( pExpr->op==TK_SELECT ); + if( ExprUseXSelect(pExpr) ){ int nRef = pNC->nRef; - notValid(pParse, pNC, "subqueries", NC_IsCheck|NC_PartIdx|NC_IdxExpr); - sqlite3WalkSelect(pWalker, pExpr->x.pSelect); + testcase( pNC->ncFlags & NC_IsCheck ); + testcase( pNC->ncFlags & NC_PartIdx ); + testcase( pNC->ncFlags & NC_IdxExpr ); + testcase( pNC->ncFlags & NC_GenCol ); + assert( pExpr->x.pSelect ); + if( pExpr->op==TK_EXISTS ) pParse->bHasExists = 1; + if( pNC->ncFlags & NC_SelfRef ){ + notValidImpl(pParse, pNC, "subqueries", pExpr, pExpr); + }else{ + sqlite3WalkSelect(pWalker, pExpr->x.pSelect); + } assert( pNC->nRef>=nRef ); if( nRef!=pNC->nRef ){ ExprSetProperty(pExpr, EP_VarSelect); + pExpr->x.pSelect->selFlags |= SF_Correlated; } + pNC->ncFlags |= NC_Subquery; } break; } case TK_VARIABLE: { - notValid(pParse, pNC, "parameters", NC_IsCheck|NC_PartIdx|NC_IdxExpr); + testcase( pNC->ncFlags & NC_IsCheck ); + testcase( pNC->ncFlags & NC_PartIdx ); + testcase( pNC->ncFlags & NC_IdxExpr ); + testcase( pNC->ncFlags & NC_GenCol ); + sqlite3ResolveNotValid(pParse, pNC, "parameters", + NC_IsCheck|NC_PartIdx|NC_IdxExpr|NC_GenCol, pExpr, pExpr); + break; + } + case TK_IS: + case TK_ISNOT: { + Expr *pRight = sqlite3ExprSkipCollateAndLikely(pExpr->pRight); + assert( !ExprHasProperty(pExpr, EP_Reduced) ); + /* Handle special cases of "x IS TRUE", "x IS FALSE", "x IS NOT TRUE", + ** and "x IS NOT FALSE". */ + if( ALWAYS(pRight) && (pRight->op==TK_ID || pRight->op==TK_TRUEFALSE) ){ + int rc = resolveExprStep(pWalker, pRight); + if( rc==WRC_Abort ) return WRC_Abort; + if( pRight->op==TK_TRUEFALSE ){ + pExpr->op2 = pExpr->op; + pExpr->op = TK_TRUTH; + return WRC_Continue; + } + } + /* no break */ deliberate_fall_through + } + case TK_BETWEEN: + case TK_EQ: + case TK_NE: + case TK_LT: + case TK_LE: + case TK_GT: + case TK_GE: { + int nLeft, nRight; + if( pParse->db->mallocFailed ) break; + assert( pExpr->pLeft!=0 ); + nLeft = sqlite3ExprVectorSize(pExpr->pLeft); + if( pExpr->op==TK_BETWEEN ){ + assert( ExprUseXList(pExpr) ); + nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[0].pExpr); + if( nRight==nLeft ){ + nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[1].pExpr); + } + }else{ + assert( pExpr->pRight!=0 ); + nRight = sqlite3ExprVectorSize(pExpr->pRight); + } + if( nLeft!=nRight ){ + testcase( pExpr->op==TK_EQ ); + testcase( pExpr->op==TK_NE ); + testcase( pExpr->op==TK_LT ); + testcase( pExpr->op==TK_LE ); + testcase( pExpr->op==TK_GT ); + testcase( pExpr->op==TK_GE ); + testcase( pExpr->op==TK_IS ); + testcase( pExpr->op==TK_ISNOT ); + testcase( pExpr->op==TK_BETWEEN ); + sqlite3ErrorMsg(pParse, "row value misused"); + sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr); + } break; } } - return (pParse->nErr || pParse->db->mallocFailed) ? WRC_Abort : WRC_Continue; + assert( pParse->db->mallocFailed==0 || pParse->nErr!=0 ); + return pParse->nErr ? WRC_Abort : WRC_Continue; } /* @@ -797,10 +1479,13 @@ static int resolveAsName( UNUSED_PARAMETER(pParse); if( pE->op==TK_ID ){ - char *zCol = pE->u.zToken; + const char *zCol; + assert( !ExprHasProperty(pE, EP_IntValue) ); + zCol = pE->u.zToken; for(i=0; inExpr; i++){ - char *zAs = pEList->a[i].zName; - if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){ + if( pEList->a[i].fg.eEName==ENAME_NAME + && sqlite3_stricmp(pEList->a[i].zEName, zCol)==0 + ){ return i+1; } } @@ -838,7 +1523,7 @@ static int resolveOrderByTermToExprList( int rc; /* Return code from subprocedures */ u8 savedSuppErr; /* Saved value of db->suppressErr */ - assert( sqlite3ExprIsInteger(pE, &i)==0 ); + assert( sqlite3ExprIsInteger(pE, &i, 0)==0 ); pEList = pSelect->pEList; /* Resolve all names in the ORDER BY term expression @@ -846,9 +1531,9 @@ static int resolveOrderByTermToExprList( memset(&nc, 0, sizeof(nc)); nc.pParse = pParse; nc.pSrcList = pSelect->pSrc; - nc.pEList = pEList; - nc.ncFlags = NC_AllowAgg; - nc.nErr = 0; + nc.uNC.pEList = pEList; + nc.ncFlags = NC_AllowAgg|NC_UEList|NC_NoSelect; + nc.nNcErr = 0; db = pParse->db; savedSuppErr = db->suppressErr; db->suppressErr = 1; @@ -861,7 +1546,7 @@ static int resolveOrderByTermToExprList( ** result-set entry. */ for(i=0; inExpr; i++){ - if( sqlite3ExprCompare(pEList->a[i].pExpr, pE, -1)<2 ){ + if( sqlite3ExprCompare(0, pEList->a[i].pExpr, pE, -1)<2 ){ return i+1; } } @@ -877,11 +1562,13 @@ static void resolveOutOfRangeError( Parse *pParse, /* The error context into which to write the error */ const char *zType, /* "ORDER" or "GROUP" */ int i, /* The index (1-based) of the term out of range */ - int mx /* Largest permissible value of i */ + int mx, /* Largest permissible value of i */ + Expr *pError /* Associate the error with the expression */ ){ - sqlite3ErrorMsg(pParse, + sqlite3ErrorMsg(pParse, "%r %s BY term out of range - should be " "between 1 and %d", i, zType, mx); + sqlite3RecordErrorOffsetOfExpr(pParse->db, pError); } /* @@ -912,14 +1599,12 @@ static int resolveCompoundOrderBy( pOrderBy = pSelect->pOrderBy; if( pOrderBy==0 ) return 0; db = pParse->db; -#if SQLITE_MAX_COLUMN if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){ sqlite3ErrorMsg(pParse, "too many terms in ORDER BY clause"); return 1; } -#endif for(i=0; inExpr; i++){ - pOrderBy->a[i].done = 0; + pOrderBy->a[i].fg.done = 0; } pSelect->pNext = 0; while( pSelect->pPrior ){ @@ -934,43 +1619,60 @@ static int resolveCompoundOrderBy( for(i=0, pItem=pOrderBy->a; inExpr; i++, pItem++){ int iCol = -1; Expr *pE, *pDup; - if( pItem->done ) continue; - pE = sqlite3ExprSkipCollate(pItem->pExpr); - if( sqlite3ExprIsInteger(pE, &iCol) ){ + if( pItem->fg.done ) continue; + pE = sqlite3ExprSkipCollateAndLikely(pItem->pExpr); + if( NEVER(pE==0) ) continue; + if( sqlite3ExprIsInteger(pE, &iCol, 0) ){ if( iCol<=0 || iCol>pEList->nExpr ){ - resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr); + resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr, pE); return 1; } }else{ iCol = resolveAsName(pParse, pEList, pE); if( iCol==0 ){ + /* Now test if expression pE matches one of the values returned + ** by pSelect. In the usual case this is done by duplicating the + ** expression, resolving any symbols in it, and then comparing + ** it against each expression returned by the SELECT statement. + ** Once the comparisons are finished, the duplicate expression + ** is deleted. + ** + ** If this is running as part of an ALTER TABLE operation and + ** the symbols resolve successfully, also resolve the symbols in the + ** actual expression. This allows the code in alter.c to modify + ** column references within the ORDER BY expression as required. */ pDup = sqlite3ExprDup(db, pE, 0); if( !db->mallocFailed ){ assert(pDup); iCol = resolveOrderByTermToExprList(pParse, pSelect, pDup); + if( IN_RENAME_OBJECT && iCol>0 ){ + resolveOrderByTermToExprList(pParse, pSelect, pE); + } } sqlite3ExprDelete(db, pDup); } } if( iCol>0 ){ /* Convert the ORDER BY term into an integer column number iCol, - ** taking care to preserve the COLLATE clause if it exists */ - Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0); - if( pNew==0 ) return 1; - pNew->flags |= EP_IntValue; - pNew->u.iValue = iCol; - if( pItem->pExpr==pE ){ - pItem->pExpr = pNew; - }else{ - Expr *pParent = pItem->pExpr; - assert( pParent->op==TK_COLLATE ); - while( pParent->pLeft->op==TK_COLLATE ) pParent = pParent->pLeft; - assert( pParent->pLeft==pE ); - pParent->pLeft = pNew; + ** taking care to preserve the COLLATE clause if it exists. */ + if( !IN_RENAME_OBJECT ){ + Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0); + if( pNew==0 ) return 1; + pNew->flags |= EP_IntValue; + pNew->u.iValue = iCol; + if( pItem->pExpr==pE ){ + pItem->pExpr = pNew; + }else{ + Expr *pParent = pItem->pExpr; + assert( pParent->op==TK_COLLATE ); + while( pParent->pLeft->op==TK_COLLATE ) pParent = pParent->pLeft; + assert( pParent->pLeft==pE ); + pParent->pLeft = pNew; + } + sqlite3ExprDelete(db, pE); + pItem->u.x.iOrderByCol = (u16)iCol; } - sqlite3ExprDelete(db, pE); - pItem->u.x.iOrderByCol = (u16)iCol; - pItem->done = 1; + pItem->fg.done = 1; }else{ moreToDo = 1; } @@ -978,7 +1680,7 @@ static int resolveCompoundOrderBy( pSelect = pSelect->pNext; } for(i=0; inExpr; i++){ - if( pOrderBy->a[i].done==0 ){ + if( pOrderBy->a[i].fg.done==0 ){ sqlite3ErrorMsg(pParse, "%r ORDER BY term does not match any " "column in the result set", i+1); return 1; @@ -1008,28 +1710,55 @@ int sqlite3ResolveOrderGroupBy( ExprList *pEList; struct ExprList_item *pItem; - if( pOrderBy==0 || pParse->db->mallocFailed ) return 0; -#if SQLITE_MAX_COLUMN + if( pOrderBy==0 || pParse->db->mallocFailed || IN_RENAME_OBJECT ) return 0; if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){ sqlite3ErrorMsg(pParse, "too many terms in %s BY clause", zType); return 1; } -#endif pEList = pSelect->pEList; assert( pEList!=0 ); /* sqlite3SelectNew() guarantees this */ for(i=0, pItem=pOrderBy->a; inExpr; i++, pItem++){ if( pItem->u.x.iOrderByCol ){ if( pItem->u.x.iOrderByCol>pEList->nExpr ){ - resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr); + resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr, 0); return 1; } - resolveAlias(pParse, pEList, pItem->u.x.iOrderByCol-1, pItem->pExpr, - zType,0); + resolveAlias(pParse, pEList, pItem->u.x.iOrderByCol-1, pItem->pExpr,0); } } return 0; } +#ifndef SQLITE_OMIT_WINDOWFUNC +/* +** Walker callback for windowRemoveExprFromSelect(). +*/ +static int resolveRemoveWindowsCb(Walker *pWalker, Expr *pExpr){ + UNUSED_PARAMETER(pWalker); + if( ExprHasProperty(pExpr, EP_WinFunc) ){ + Window *pWin = pExpr->y.pWin; + sqlite3WindowUnlinkFromSelect(pWin); + } + return WRC_Continue; +} + +/* +** Remove any Window objects owned by the expression pExpr from the +** Select.pWin list of Select object pSelect. +*/ +static void windowRemoveExprFromSelect(Select *pSelect, Expr *pExpr){ + if( pSelect->pWin ){ + Walker sWalker; + memset(&sWalker, 0, sizeof(Walker)); + sWalker.xExprCallback = resolveRemoveWindowsCb; + sWalker.u.pSelect = pSelect; + sqlite3WalkExpr(&sWalker, pExpr); + } +} +#else +# define windowRemoveExprFromSelect(a, b) +#endif /* SQLITE_OMIT_WINDOWFUNC */ + /* ** pOrderBy is an ORDER BY or GROUP BY clause in SELECT statement pSelect. ** The Name context of the SELECT statement is pNC. zType is either @@ -1060,12 +1789,13 @@ static int resolveOrderGroupBy( Parse *pParse; /* Parsing context */ int nResult; /* Number of terms in the result set */ - if( pOrderBy==0 ) return 0; + assert( pOrderBy!=0 ); nResult = pSelect->pEList->nExpr; pParse = pNC->pParse; for(i=0, pItem=pOrderBy->a; inExpr; i++, pItem++){ Expr *pE = pItem->pExpr; - Expr *pE2 = sqlite3ExprSkipCollate(pE); + Expr *pE2 = sqlite3ExprSkipCollateAndLikely(pE); + if( NEVER(pE2==0) ) continue; if( zType[0]!='G' ){ iCol = resolveAsName(pParse, pSelect->pEList, pE2); if( iCol>0 ){ @@ -1077,12 +1807,12 @@ static int resolveOrderGroupBy( continue; } } - if( sqlite3ExprIsInteger(pE2, &iCol) ){ + if( sqlite3ExprIsInteger(pE2, &iCol, 0) ){ /* The ORDER BY term is an integer constant. Again, set the column ** number so that sqlite3ResolveOrderGroupBy() will convert the ** order-by term to a copy of the result-set expression */ if( iCol<1 || iCol>0xffff ){ - resolveOutOfRangeError(pParse, zType, i+1, nResult); + resolveOutOfRangeError(pParse, zType, i+1, nResult, pE2); return 1; } pItem->u.x.iOrderByCol = (u16)iCol; @@ -1095,7 +1825,11 @@ static int resolveOrderGroupBy( return 1; } for(j=0; jpEList->nExpr; j++){ - if( sqlite3ExprCompare(pE, pSelect->pEList->a[j].pExpr, -1)==0 ){ + if( sqlite3ExprCompare(0, pE, pSelect->pEList->a[j].pExpr, -1)==0 ){ + /* Since this expression is being changed into a reference + ** to an identical expression in the result set, remove all Window + ** objects belonging to the expression from the Select.pWin list. */ + windowRemoveExprFromSelect(pSelect, pE); pItem->u.x.iOrderByCol = j+1; } } @@ -1116,7 +1850,7 @@ static int resolveSelectStep(Walker *pWalker, Select *p){ ExprList *pGroupBy; /* The GROUP BY clause */ Select *pLeftmost; /* Left-most of SELECT of a compound */ sqlite3 *db; /* Database connection */ - + assert( p!=0 ); if( p->selFlags & SF_Resolved ){ @@ -1136,7 +1870,7 @@ static int resolveSelectStep(Walker *pWalker, Select *p){ */ if( (p->selFlags & SF_Expanded)==0 ){ sqlite3SelectPrep(pParse, p, pOuterNC); - return (pParse->nErr || db->mallocFailed) ? WRC_Abort : WRC_Prune; + return pParse->nErr ? WRC_Abort : WRC_Prune; } isCompound = p->pPrior!=0; @@ -1152,8 +1886,8 @@ static int resolveSelectStep(Walker *pWalker, Select *p){ */ memset(&sNC, 0, sizeof(sNC)); sNC.pParse = pParse; - if( sqlite3ResolveExprNames(&sNC, p->pLimit) || - sqlite3ResolveExprNames(&sNC, p->pOffset) ){ + sNC.pWinSelect = p; + if( sqlite3ResolveExprNames(&sNC, p->pLimit) ){ return WRC_Abort; } @@ -1164,69 +1898,76 @@ static int resolveSelectStep(Walker *pWalker, Select *p){ ** moves the pOrderBy down to the sub-query. It will be moved back ** after the names have been resolved. */ if( p->selFlags & SF_Converted ){ - Select *pSub = p->pSrc->a[0].pSelect; + Select *pSub; + assert( p->pSrc->a[0].fg.isSubquery ); + assert( p->pSrc->a[0].u4.pSubq!=0 ); + pSub = p->pSrc->a[0].u4.pSubq->pSelect; + assert( pSub!=0 ); assert( p->pSrc->nSrc==1 && p->pOrderBy ); assert( pSub->pPrior && pSub->pOrderBy==0 ); pSub->pOrderBy = p->pOrderBy; p->pOrderBy = 0; } - - /* Recursively resolve names in all subqueries + + /* Recursively resolve names in all subqueries in the FROM clause */ + if( pOuterNC ) pOuterNC->nNestedSelect++; for(i=0; ipSrc->nSrc; i++){ - struct SrcList_item *pItem = &p->pSrc->a[i]; - if( pItem->pSelect ){ - NameContext *pNC; /* Used to iterate name contexts */ - int nRef = 0; /* Refcount for pOuterNC and outer contexts */ + SrcItem *pItem = &p->pSrc->a[i]; + assert( pItem->zName!=0 + || pItem->fg.isSubquery ); /* Test of tag-20240424-1*/ + if( pItem->fg.isSubquery + && (pItem->u4.pSubq->pSelect->selFlags & SF_Resolved)==0 + ){ + int nRef = pOuterNC ? pOuterNC->nRef : 0; const char *zSavedContext = pParse->zAuthContext; - /* Count the total number of references to pOuterNC and all of its - ** parent contexts. After resolving references to expressions in - ** pItem->pSelect, check if this value has changed. If so, then - ** SELECT statement pItem->pSelect must be correlated. Set the - ** pItem->fg.isCorrelated flag if this is the case. */ - for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef += pNC->nRef; - if( pItem->zName ) pParse->zAuthContext = pItem->zName; - sqlite3ResolveSelectNames(pParse, pItem->pSelect, pOuterNC); + sqlite3ResolveSelectNames(pParse, pItem->u4.pSubq->pSelect, pOuterNC); pParse->zAuthContext = zSavedContext; - if( pParse->nErr || db->mallocFailed ) return WRC_Abort; - - for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef -= pNC->nRef; - assert( pItem->fg.isCorrelated==0 && nRef<=0 ); - pItem->fg.isCorrelated = (nRef!=0); + if( pParse->nErr ) return WRC_Abort; + assert( db->mallocFailed==0 ); + + /* If the number of references to the outer context changed when + ** expressions in the sub-select were resolved, the sub-select + ** is correlated. It is not required to check the refcount on any + ** but the innermost outer context object, as lookupName() increments + ** the refcount on all contexts between the current one and the + ** context containing the column when it resolves a name. */ + if( pOuterNC ){ + assert( pItem->fg.isCorrelated==0 && pOuterNC->nRef>=nRef ); + pItem->fg.isCorrelated = (pOuterNC->nRef>nRef); + } } } - + if( pOuterNC && ALWAYS(pOuterNC->nNestedSelect>0) ){ + pOuterNC->nNestedSelect--; + } + /* Set up the local name-context to pass to sqlite3ResolveExprNames() to ** resolve the result-set expression list. */ - sNC.ncFlags = NC_AllowAgg; + sNC.ncFlags = NC_AllowAgg|NC_AllowWin; sNC.pSrcList = p->pSrc; sNC.pNext = pOuterNC; - + /* Resolve names in the result set. */ if( sqlite3ResolveExprListNames(&sNC, p->pEList) ) return WRC_Abort; - - /* If there are no aggregate functions in the result-set, and no GROUP BY + sNC.ncFlags &= ~NC_AllowWin; + + /* If there are no aggregate functions in the result-set, and no GROUP BY ** expression, do not allow aggregates in any of the other expressions. */ assert( (p->selFlags & SF_Aggregate)==0 ); pGroupBy = p->pGroupBy; if( pGroupBy || (sNC.ncFlags & NC_HasAgg)!=0 ){ assert( NC_MinMaxAgg==SF_MinMaxAgg ); - p->selFlags |= SF_Aggregate | (sNC.ncFlags&NC_MinMaxAgg); + assert( NC_OrderAgg==SF_OrderByReqd ); + p->selFlags |= SF_Aggregate | (sNC.ncFlags&(NC_MinMaxAgg|NC_OrderAgg)); }else{ sNC.ncFlags &= ~NC_AllowAgg; } - - /* If a HAVING clause is present, then there must be a GROUP BY clause. - */ - if( p->pHaving && !pGroupBy ){ - sqlite3ErrorMsg(pParse, "a GROUP BY clause is required before HAVING"); - return WRC_Abort; - } - + /* Add the output column list to the name-context before parsing the ** other expressions in the SELECT statement. This is so that ** expressions in the WHERE clause (etc.) can refer to expressions by @@ -1235,33 +1976,59 @@ static int resolveSelectStep(Walker *pWalker, Select *p){ ** Minor point: If this is the case, then the expression will be ** re-evaluated for each reference to it. */ - sNC.pEList = p->pEList; - if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort; + assert( (sNC.ncFlags & (NC_UAggInfo|NC_UUpsert|NC_UBaseReg))==0 ); + sNC.uNC.pEList = p->pEList; + sNC.ncFlags |= NC_UEList; + if( p->pHaving ){ + if( (p->selFlags & SF_Aggregate)==0 ){ + sqlite3ErrorMsg(pParse, "HAVING clause on a non-aggregate query"); + return WRC_Abort; + } + if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort; + } + sNC.ncFlags |= NC_Where; if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) return WRC_Abort; + sNC.ncFlags &= ~NC_Where; /* Resolve names in table-valued-function arguments */ for(i=0; ipSrc->nSrc; i++){ - struct SrcList_item *pItem = &p->pSrc->a[i]; + SrcItem *pItem = &p->pSrc->a[i]; if( pItem->fg.isTabFunc - && sqlite3ResolveExprListNames(&sNC, pItem->u1.pFuncArg) + && sqlite3ResolveExprListNames(&sNC, pItem->u1.pFuncArg) ){ return WRC_Abort; } } +#ifndef SQLITE_OMIT_WINDOWFUNC + if( IN_RENAME_OBJECT ){ + Window *pWin; + for(pWin=p->pWinDefn; pWin; pWin=pWin->pNextWin){ + if( sqlite3ResolveExprListNames(&sNC, pWin->pOrderBy) + || sqlite3ResolveExprListNames(&sNC, pWin->pPartition) + ){ + return WRC_Abort; + } + } + } +#endif + /* The ORDER BY and GROUP BY clauses may not refer to terms in - ** outer queries + ** outer queries */ sNC.pNext = 0; - sNC.ncFlags |= NC_AllowAgg; + sNC.ncFlags |= NC_AllowAgg|NC_AllowWin; - /* If this is a converted compound query, move the ORDER BY clause from + /* If this is a converted compound query, move the ORDER BY clause from ** the sub-query back to the parent query. At this point each term ** within the ORDER BY clause has been transformed to an integer value. ** These integers will be replaced by copies of the corresponding result ** set expressions by the call to resolveOrderGroupBy() below. */ if( p->selFlags & SF_Converted ){ - Select *pSub = p->pSrc->a[0].pSelect; + Select *pSub; + assert( p->pSrc->a[0].fg.isSubquery ); + pSub = p->pSrc->a[0].u4.pSubq->pSelect; + assert( pSub!=0 ); p->pOrderBy = pSub->pOrderBy; pSub->pOrderBy = 0; } @@ -1276,7 +2043,8 @@ static int resolveSelectStep(Walker *pWalker, Select *p){ ** is not detected until much later, and so we need to go ahead and ** resolve those symbols on the incorrect ORDER BY for consistency. */ - if( isCompound<=nCompound /* Defer right-most ORDER BY of a compound */ + if( p->pOrderBy!=0 + && isCompound<=nCompound /* Defer right-most ORDER BY of a compound */ && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER") ){ return WRC_Abort; @@ -1284,13 +2052,14 @@ static int resolveSelectStep(Walker *pWalker, Select *p){ if( db->mallocFailed ){ return WRC_Abort; } - - /* Resolve the GROUP BY clause. At the same time, make sure + sNC.ncFlags &= ~NC_AllowWin; + + /* Resolve the GROUP BY clause. At the same time, make sure ** the GROUP BY clause does not contain aggregate functions. */ if( pGroupBy ){ struct ExprList_item *pItem; - + if( resolveOrderGroupBy(&sNC, p, pGroupBy, "GROUP") || db->mallocFailed ){ return WRC_Abort; } @@ -1332,7 +2101,7 @@ static int resolveSelectStep(Walker *pWalker, Select *p){ ** checking on function usage and set a flag if any aggregate functions ** are seen. ** -** To resolve table columns references we look for nodes (or subtrees) of the +** To resolve table columns references we look for nodes (or subtrees) of the ** form X.Y.Z or Y.Z or just Z where ** ** X: The name of a database. Ex: "main" or "temp" or @@ -1364,7 +2133,7 @@ static int resolveSelectStep(Walker *pWalker, Select *p){ ** ** SELECT a+b AS x, c+d AS y FROM t1 ORDER BY a+b; ** -** Function calls are checked to make sure that the function is +** Function calls are checked to make sure that the function is ** defined and that the correct number of arguments are specified. ** If the function is an aggregate function, then the NC_HasAgg flag is ** set and the opcode is changed from TK_FUNCTION to TK_AGG_FUNCTION. @@ -1374,67 +2143,96 @@ static int resolveSelectStep(Walker *pWalker, Select *p){ ** An error message is left in pParse if anything is amiss. The number ** if errors is returned. */ -int sqlite3ResolveExprNames( +int sqlite3ResolveExprNames( NameContext *pNC, /* Namespace to resolve expressions in. */ Expr *pExpr /* The expression to be analyzed. */ ){ - u16 savedHasAgg; + int savedHasAgg; Walker w; - if( pExpr==0 ) return 0; -#if SQLITE_MAX_EXPR_DEPTH>0 - { - Parse *pParse = pNC->pParse; - if( sqlite3ExprCheckHeight(pParse, pExpr->nHeight+pNC->pParse->nHeight) ){ - return 1; - } - pParse->nHeight += pExpr->nHeight; - } -#endif - savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg); - pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg); + if( pExpr==0 ) return SQLITE_OK; + savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg); + pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg); w.pParse = pNC->pParse; w.xExprCallback = resolveExprStep; - w.xSelectCallback = resolveSelectStep; + w.xSelectCallback = (pNC->ncFlags & NC_NoSelect) ? 0 : resolveSelectStep; w.xSelectCallback2 = 0; - w.walkerDepth = 0; - w.eCode = 0; w.u.pNC = pNC; - sqlite3WalkExpr(&w, pExpr); #if SQLITE_MAX_EXPR_DEPTH>0 - pNC->pParse->nHeight -= pExpr->nHeight; -#endif - if( pNC->nErr>0 || w.pParse->nErr>0 ){ - ExprSetProperty(pExpr, EP_Error); - } - if( pNC->ncFlags & NC_HasAgg ){ - ExprSetProperty(pExpr, EP_Agg); + w.pParse->nHeight += pExpr->nHeight; + if( sqlite3ExprCheckHeight(w.pParse, w.pParse->nHeight) ){ + return SQLITE_ERROR; } +#endif + assert( pExpr!=0 ); + sqlite3WalkExprNN(&w, pExpr); +#if SQLITE_MAX_EXPR_DEPTH>0 + w.pParse->nHeight -= pExpr->nHeight; +#endif + assert( EP_Agg==NC_HasAgg ); + assert( EP_Win==NC_HasWin ); + testcase( pNC->ncFlags & NC_HasAgg ); + testcase( pNC->ncFlags & NC_HasWin ); + ExprSetProperty(pExpr, pNC->ncFlags & (NC_HasAgg|NC_HasWin) ); pNC->ncFlags |= savedHasAgg; - return ExprHasProperty(pExpr, EP_Error); + return pNC->nNcErr>0 || w.pParse->nErr>0; } /* ** Resolve all names for all expression in an expression list. This is ** just like sqlite3ResolveExprNames() except that it works for an expression ** list rather than a single expression. +** +** The return value is SQLITE_OK (0) for success or SQLITE_ERROR (1) for a +** failure. */ -int sqlite3ResolveExprListNames( +int sqlite3ResolveExprListNames( NameContext *pNC, /* Namespace to resolve expressions in. */ ExprList *pList /* The expression list to be analyzed. */ ){ int i; - if( pList ){ - for(i=0; inExpr; i++){ - if( sqlite3ResolveExprNames(pNC, pList->a[i].pExpr) ) return WRC_Abort; + int savedHasAgg = 0; + Walker w; + if( pList==0 ) return SQLITE_OK; + w.pParse = pNC->pParse; + w.xExprCallback = resolveExprStep; + w.xSelectCallback = resolveSelectStep; + w.xSelectCallback2 = 0; + w.u.pNC = pNC; + savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg); + pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg); + for(i=0; inExpr; i++){ + Expr *pExpr = pList->a[i].pExpr; + if( pExpr==0 ) continue; +#if SQLITE_MAX_EXPR_DEPTH>0 + w.pParse->nHeight += pExpr->nHeight; + if( sqlite3ExprCheckHeight(w.pParse, w.pParse->nHeight) ){ + return SQLITE_ERROR; } +#endif + sqlite3WalkExprNN(&w, pExpr); +#if SQLITE_MAX_EXPR_DEPTH>0 + w.pParse->nHeight -= pExpr->nHeight; +#endif + assert( EP_Agg==NC_HasAgg ); + assert( EP_Win==NC_HasWin ); + testcase( pNC->ncFlags & NC_HasAgg ); + testcase( pNC->ncFlags & NC_HasWin ); + if( pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg) ){ + ExprSetProperty(pExpr, pNC->ncFlags & (NC_HasAgg|NC_HasWin) ); + savedHasAgg |= pNC->ncFlags & + (NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg); + pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg); + } + if( w.pParse->nErr>0 ) return SQLITE_ERROR; } - return WRC_Continue; + pNC->ncFlags |= savedHasAgg; + return SQLITE_OK; } /* ** Resolve all names in all expressions of a SELECT and in all -** decendents of the SELECT, including compounds off of p->pPrior, +** descendants of the SELECT, including compounds off of p->pPrior, ** subqueries in expressions, and subqueries used as FROM clause ** terms. ** @@ -1452,45 +2250,68 @@ void sqlite3ResolveSelectNames( Walker w; assert( p!=0 ); - memset(&w, 0, sizeof(w)); w.xExprCallback = resolveExprStep; w.xSelectCallback = resolveSelectStep; + w.xSelectCallback2 = 0; w.pParse = pParse; w.u.pNC = pOuterNC; sqlite3WalkSelect(&w, p); } /* -** Resolve names in expressions that can only reference a single table: +** Resolve names in expressions that can only reference a single table +** or which cannot reference any tables at all. Examples: ** -** * CHECK constraints -** * WHERE clauses on partial indices +** "type" flag +** ------------ +** (1) CHECK constraints NC_IsCheck +** (2) WHERE clauses on partial indices NC_PartIdx +** (3) Expressions in indexes on expressions NC_IdxExpr +** (4) Expression arguments to VACUUM INTO. 0 +** (5) GENERATED ALWAYS as expressions NC_GenCol ** -** The Expr.iTable value for Expr.op==TK_COLUMN nodes of the expression -** is set to -1 and the Expr.iColumn value is set to the column number. +** In all cases except (4), the Expr.iTable value for Expr.op==TK_COLUMN +** nodes of the expression is set to -1 and the Expr.iColumn value is +** set to the column number. In case (4), TK_COLUMN nodes cause an error. ** ** Any errors cause an error message to be set in pParse. */ -void sqlite3ResolveSelfReference( - Parse *pParse, /* Parsing context */ - Table *pTab, /* The table being referenced */ - int type, /* NC_IsCheck or NC_PartIdx or NC_IdxExpr */ - Expr *pExpr, /* Expression to resolve. May be NULL. */ - ExprList *pList /* Expression list to resolve. May be NUL. */ +int sqlite3ResolveSelfReference( + Parse *pParse, /* Parsing context */ + Table *pTab, /* The table being referenced, or NULL */ + int type, /* NC_IsCheck, NC_PartIdx, NC_IdxExpr, NC_GenCol, or 0 */ + Expr *pExpr, /* Expression to resolve. May be NULL. */ + ExprList *pList /* Expression list to resolve. May be NULL. */ ){ - SrcList sSrc; /* Fake SrcList for pParse->pNewTable */ + SrcList *pSrc; /* Fake SrcList for pParse->pNewTable */ NameContext sNC; /* Name context for pParse->pNewTable */ - - assert( type==NC_IsCheck || type==NC_PartIdx || type==NC_IdxExpr ); + int rc; + union { + SrcList sSrc; + u8 srcSpace[SZ_SRCLIST_1]; /* Memory space for the fake SrcList */ + } uSrc; + + assert( type==0 || pTab!=0 ); + assert( type==NC_IsCheck || type==NC_PartIdx || type==NC_IdxExpr + || type==NC_GenCol || pTab==0 ); memset(&sNC, 0, sizeof(sNC)); - memset(&sSrc, 0, sizeof(sSrc)); - sSrc.nSrc = 1; - sSrc.a[0].zName = pTab->zName; - sSrc.a[0].pTab = pTab; - sSrc.a[0].iCursor = -1; + memset(&uSrc, 0, sizeof(uSrc)); + pSrc = &uSrc.sSrc; + if( pTab ){ + pSrc->nSrc = 1; + pSrc->a[0].zName = pTab->zName; + pSrc->a[0].pSTab = pTab; + pSrc->a[0].iCursor = -1; + if( pTab->pSchema!=pParse->db->aDb[1].pSchema ){ + /* Cause EP_FromDDL to be set on TK_FUNCTION nodes of non-TEMP + ** schema elements */ + type |= NC_FromDDL; + } + } sNC.pParse = pParse; - sNC.pSrcList = &sSrc; - sNC.ncFlags = type; - if( sqlite3ResolveExprNames(&sNC, pExpr) ) return; - if( pList ) sqlite3ResolveExprListNames(&sNC, pList); + sNC.pSrcList = pSrc; + sNC.ncFlags = type | NC_IsDDL; + if( (rc = sqlite3ResolveExprNames(&sNC, pExpr))!=SQLITE_OK ) return rc; + if( pList ) rc = sqlite3ResolveExprListNames(&sNC, pList); + return rc; } diff --git a/src/rowset.c b/src/rowset.c index c2e73ed72e..5956cb2ad8 100644 --- a/src/rowset.c +++ b/src/rowset.c @@ -35,14 +35,14 @@ ** extracts the least value from the RowSet. ** ** The INSERT primitive might allocate additional memory. Memory is -** allocated in chunks so most INSERTs do no allocation. There is an +** allocated in chunks so most INSERTs do no allocation. There is an ** upper bound on the size of allocated memory. No memory is freed ** until DESTROY. ** ** The TEST primitive includes a "batch" number. The TEST primitive ** will only see elements that were inserted before the last change ** in the batch number. In other words, if an INSERT occurs between -** two TESTs where the TESTs have the same batch nubmer, then the +** two TESTs where the TESTs have the same batch number, then the ** value added by the INSERT will not be visible to the second TEST. ** The initial batch number is zero, so if the very first TEST contains ** a non-zero batch number, it will see all prior INSERTs. @@ -57,8 +57,9 @@ ** of the first SMALLEST is O(NlogN). Second and subsequent SMALLEST ** primitives are constant time. The cost of DESTROY is O(N). ** -** There is an added cost of O(N) when switching between TEST and -** SMALLEST primitives. +** TEST and SMALLEST may not be used by the same RowSet. This used to +** be possible, but the feature was not used, so it was removed in order +** to simplify the code. */ #include "sqliteInt.h" @@ -82,7 +83,7 @@ ** in the list, pLeft points to the tree, and v is unused. The ** RowSet.pForest value points to the head of this forest list. */ -struct RowSetEntry { +struct RowSetEntry { i64 v; /* ROWID value for this entry */ struct RowSetEntry *pRight; /* Right subtree (larger entries) or list */ struct RowSetEntry *pLeft; /* Left subtree (smaller entries) */ @@ -123,30 +124,23 @@ struct RowSet { #define ROWSET_NEXT 0x02 /* True if sqlite3RowSetNext() has been called */ /* -** Turn bulk memory into a RowSet object. N bytes of memory -** are available at pSpace. The db pointer is used as a memory context -** for any subsequent allocations that need to occur. -** Return a pointer to the new RowSet object. -** -** It must be the case that N is sufficient to make a Rowset. If not -** an assertion fault occurs. -** -** If N is larger than the minimum, use the surplus as an initial -** allocation of entries available to be filled. +** Allocate a RowSet object. Return NULL if a memory allocation +** error occurs. */ -RowSet *sqlite3RowSetInit(sqlite3 *db, void *pSpace, unsigned int N){ - RowSet *p; - assert( N >= ROUND8(sizeof(*p)) ); - p = pSpace; - p->pChunk = 0; - p->db = db; - p->pEntry = 0; - p->pLast = 0; - p->pForest = 0; - p->pFresh = (struct RowSetEntry*)(ROUND8(sizeof(*p)) + (char*)p); - p->nFresh = (u16)((N - ROUND8(sizeof(*p)))/sizeof(struct RowSetEntry)); - p->rsFlags = ROWSET_SORTED; - p->iBatch = 0; +RowSet *sqlite3RowSetInit(sqlite3 *db){ + RowSet *p = sqlite3DbMallocRawNN(db, sizeof(*p)); + if( p ){ + int N = sqlite3DbMallocSize(db, p); + p->pChunk = 0; + p->db = db; + p->pEntry = 0; + p->pLast = 0; + p->pForest = 0; + p->pFresh = (struct RowSetEntry*)(ROUND8(sizeof(*p)) + (char*)p); + p->nFresh = (u16)((N - ROUND8(sizeof(*p)))/sizeof(struct RowSetEntry)); + p->rsFlags = ROWSET_SORTED; + p->iBatch = 0; + } return p; } @@ -155,7 +149,8 @@ RowSet *sqlite3RowSetInit(sqlite3 *db, void *pSpace, unsigned int N){ ** the RowSet has allocated over its lifetime. This routine is ** the destructor for the RowSet. */ -void sqlite3RowSetClear(RowSet *p){ +void sqlite3RowSetClear(void *pArg){ + RowSet *p = (RowSet*)pArg; struct RowSetChunk *pChunk, *pNextChunk; for(pChunk=p->pChunk; pChunk; pChunk = pNextChunk){ pNextChunk = pChunk->pNextChunk; @@ -169,17 +164,29 @@ void sqlite3RowSetClear(RowSet *p){ p->rsFlags = ROWSET_SORTED; } +/* +** Deallocate all chunks from a RowSet. This frees all memory that +** the RowSet has allocated over its lifetime. This routine is +** the destructor for the RowSet. +*/ +void sqlite3RowSetDelete(void *pArg){ + sqlite3RowSetClear(pArg); + sqlite3DbFree(((RowSet*)pArg)->db, pArg); +} + /* ** Allocate a new RowSetEntry object that is associated with the ** given RowSet. Return a pointer to the new and completely uninitialized -** objected. +** object. ** ** In an OOM situation, the RowSet.db->mallocFailed flag is set and this ** routine returns NULL. */ static struct RowSetEntry *rowSetEntryAlloc(RowSet *p){ assert( p!=0 ); - if( p->nFresh==0 ){ + if( p->nFresh==0 ){ /*OPTIMIZATION-IF-FALSE*/ + /* We could allocate a fresh RowSetEntry each time one is needed, but it + ** is more efficient to pull a preallocated entry from the pool */ struct RowSetChunk *pNew; pNew = sqlite3DbMallocRawNN(p->db, sizeof(*pNew)); if( pNew==0 ){ @@ -213,7 +220,9 @@ void sqlite3RowSetInsert(RowSet *p, i64 rowid){ pEntry->pRight = 0; pLast = p->pLast; if( pLast ){ - if( (p->rsFlags & ROWSET_SORTED)!=0 && rowid<=pLast->v ){ + if( rowid<=pLast->v ){ /*OPTIMIZATION-IF-FALSE*/ + /* Avoid unnecessary sorts by preserving the ROWSET_SORTED flags + ** where possible */ p->rsFlags &= ~ROWSET_SORTED; } pLast->pRight = pEntry; @@ -226,7 +235,7 @@ void sqlite3RowSetInsert(RowSet *p, i64 rowid){ /* ** Merge two lists of RowSetEntry objects. Remove duplicates. ** -** The input lists are connected via pRight pointers and are +** The input lists are connected via pRight pointers and are ** assumed to each already be in sorted order. */ static struct RowSetEntry *rowSetEntryMerge( @@ -237,35 +246,33 @@ static struct RowSetEntry *rowSetEntryMerge( struct RowSetEntry *pTail; pTail = &head; - while( pA && pB ){ + assert( pA!=0 && pB!=0 ); + for(;;){ assert( pA->pRight==0 || pA->v<=pA->pRight->v ); assert( pB->pRight==0 || pB->v<=pB->pRight->v ); - if( pA->vv ){ - pTail->pRight = pA; + if( pA->v<=pB->v ){ + if( pA->vv ) pTail = pTail->pRight = pA; pA = pA->pRight; - pTail = pTail->pRight; - }else if( pB->vv ){ - pTail->pRight = pB; - pB = pB->pRight; - pTail = pTail->pRight; + if( pA==0 ){ + pTail->pRight = pB; + break; + } }else{ - pA = pA->pRight; + pTail = pTail->pRight = pB; + pB = pB->pRight; + if( pB==0 ){ + pTail->pRight = pA; + break; + } } } - if( pA ){ - assert( pA->pRight==0 || pA->v<=pA->pRight->v ); - pTail->pRight = pA; - }else{ - assert( pB==0 || pB->pRight==0 || pB->v<=pB->pRight->v ); - pTail->pRight = pB; - } return head.pRight; } /* ** Sort all elements on the list of RowSetEntry objects into order of ** increasing v. -*/ +*/ static struct RowSetEntry *rowSetEntrySort(struct RowSetEntry *pIn){ unsigned int i; struct RowSetEntry *pNext, *aBucket[40]; @@ -281,9 +288,10 @@ static struct RowSetEntry *rowSetEntrySort(struct RowSetEntry *pIn){ aBucket[i] = pIn; pIn = pNext; } - pIn = 0; - for(i=0; i1 ){ /*OPTIMIZATION-IF-TRUE*/ + /* This branch causes a *balanced* tree to be generated. A valid tree + ** is still generated without this branch, but the tree is wildly + ** unbalanced and inefficient. */ + pLeft = rowSetNDeepTree(ppList, iDepth-1); + p = *ppList; + if( p==0 ){ /*OPTIMIZATION-IF-FALSE*/ + /* It is safe to always return here, but the resulting tree + ** would be unbalanced */ + return pLeft; + } + p->pLeft = pLeft; + *ppList = p->pRight; + p->pRight = rowSetNDeepTree(ppList, iDepth-1); + }else{ p = *ppList; *ppList = p->pRight; p->pLeft = p->pRight = 0; - return p; } - pLeft = rowSetNDeepTree(ppList, iDepth-1); - p = *ppList; - if( p==0 ){ - return pLeft; - } - p->pLeft = pLeft; - *ppList = p->pRight; - p->pRight = rowSetNDeepTree(ppList, iDepth-1); return p; } @@ -378,59 +392,37 @@ static struct RowSetEntry *rowSetListToTree(struct RowSetEntry *pList){ return p; } -/* -** Take all the entries on p->pEntry and on the trees in p->pForest and -** sort them all together into one big ordered list on p->pEntry. -** -** This routine should only be called once in the life of a RowSet. -*/ -static void rowSetToList(RowSet *p){ - - /* This routine is called only once */ - assert( p!=0 && (p->rsFlags & ROWSET_NEXT)==0 ); - - if( (p->rsFlags & ROWSET_SORTED)==0 ){ - p->pEntry = rowSetEntrySort(p->pEntry); - } - - /* While this module could theoretically support it, sqlite3RowSetNext() - ** is never called after sqlite3RowSetText() for the same RowSet. So - ** there is never a forest to deal with. Should this change, simply - ** remove the assert() and the #if 0. */ - assert( p->pForest==0 ); -#if 0 - while( p->pForest ){ - struct RowSetEntry *pTree = p->pForest->pLeft; - if( pTree ){ - struct RowSetEntry *pHead, *pTail; - rowSetTreeToList(pTree, &pHead, &pTail); - p->pEntry = rowSetEntryMerge(p->pEntry, pHead); - } - p->pForest = p->pForest->pRight; - } -#endif - p->rsFlags |= ROWSET_NEXT; /* Verify this routine is never called again */ -} - /* ** Extract the smallest element from the RowSet. ** Write the element into *pRowid. Return 1 on success. Return ** 0 if the RowSet is already empty. ** ** After this routine has been called, the sqlite3RowSetInsert() -** routine may not be called again. +** routine may not be called again. +** +** This routine may not be called after sqlite3RowSetTest() has +** been used. Older versions of RowSet allowed that, but as the +** capability was not used by the code generator, it was removed +** for code economy. */ int sqlite3RowSetNext(RowSet *p, i64 *pRowid){ assert( p!=0 ); + assert( p->pForest==0 ); /* Cannot be used with sqlite3RowSetText() */ /* Merge the forest into a single sorted list on first call */ - if( (p->rsFlags & ROWSET_NEXT)==0 ) rowSetToList(p); + if( (p->rsFlags & ROWSET_NEXT)==0 ){ /*OPTIMIZATION-IF-FALSE*/ + if( (p->rsFlags & ROWSET_SORTED)==0 ){ /*OPTIMIZATION-IF-FALSE*/ + p->pEntry = rowSetEntrySort(p->pEntry); + } + p->rsFlags |= ROWSET_SORTED|ROWSET_NEXT; + } /* Return the next entry on the list */ if( p->pEntry ){ *pRowid = p->pEntry->v; p->pEntry = p->pEntry->pRight; - if( p->pEntry==0 ){ + if( p->pEntry==0 ){ /*OPTIMIZATION-IF-TRUE*/ + /* Free memory immediately, rather than waiting on sqlite3_finalize() */ sqlite3RowSetClear(p); } return 1; @@ -453,13 +445,15 @@ int sqlite3RowSetTest(RowSet *pRowSet, int iBatch, sqlite3_int64 iRowid){ /* This routine is never called after sqlite3RowSetNext() */ assert( pRowSet!=0 && (pRowSet->rsFlags & ROWSET_NEXT)==0 ); - /* Sort entries into the forest on the first test of a new batch + /* Sort entries into the forest on the first test of a new batch. + ** To save unnecessary work, only do this when the batch number changes. */ - if( iBatch!=pRowSet->iBatch ){ + if( iBatch!=pRowSet->iBatch ){ /*OPTIMIZATION-IF-FALSE*/ p = pRowSet->pEntry; if( p ){ struct RowSetEntry **ppPrevTree = &pRowSet->pForest; - if( (pRowSet->rsFlags & ROWSET_SORTED)==0 ){ + if( (pRowSet->rsFlags & ROWSET_SORTED)==0 ){ /*OPTIMIZATION-IF-FALSE*/ + /* Only sort the current set of entries if they need it */ p = rowSetEntrySort(p); } for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){ diff --git a/src/select.c b/src/select.c index c3132c2325..bc17ecf846 100644 --- a/src/select.c +++ b/src/select.c @@ -14,21 +14,6 @@ */ #include "sqliteInt.h" -/* -** Trace output macros -*/ -#if SELECTTRACE_ENABLED -/***/ int sqlite3SelectTrace = 0; -# define SELECTTRACE(K,P,S,X) \ - if(sqlite3SelectTrace&(K)) \ - sqlite3DebugPrintf("%*s%s.%p: ",(P)->nSelectIndent*2-2,"",\ - (S)->zSelName,(S)),\ - sqlite3DebugPrintf X -#else -# define SELECTTRACE(K,P,S,X) -#endif - - /* ** An instance of the following object is used to record information about ** how to process the DISTINCT keyword, to simplify passing that information @@ -36,7 +21,7 @@ */ typedef struct DistinctCtx DistinctCtx; struct DistinctCtx { - u8 isTnct; /* True if the DISTINCT keyword is present */ + u8 isTnct; /* 0: Not distinct. 1: DISTICT 2: DISTINCT and ORDER BY */ u8 eTnctType; /* One of the WHERE_DISTINCT_* operators */ int tabTnct; /* Ephemeral table used for DISTINCT processing */ int addrTnct; /* Address of OP_OpenEphemeral opcode for tabTnct */ @@ -45,6 +30,20 @@ struct DistinctCtx { /* ** An instance of the following object is used to record information about ** the ORDER BY (or GROUP BY) clause of query is being coded. +** +** The aDefer[] array is used by the sorter-references optimization. For +** example, assuming there is no index that can be used for the ORDER BY, +** for the query: +** +** SELECT a, bigblob FROM t1 ORDER BY a LIMIT 10; +** +** it may be more efficient to add just the "a" values to the sorter, and +** retrieve the associated "bigblob" values directly from table t1 as the +** 10 smallest "a" values are extracted from the sorter. +** +** When the sorter-reference optimization is used, there is one entry in the +** aDefer[] array for each database table that may be read as values are +** extracted from the sorter. */ typedef struct SortCtx SortCtx; struct SortCtx { @@ -55,15 +54,33 @@ struct SortCtx { int labelBkOut; /* Start label for the block-output subroutine */ int addrSortIndex; /* Address of the OP_SorterOpen or OP_OpenEphemeral */ int labelDone; /* Jump here when done, ex: LIMIT reached */ + int labelOBLopt; /* Jump here when sorter is full */ u8 sortFlags; /* Zero or more SORTFLAG_* bits */ +#ifdef SQLITE_ENABLE_SORTER_REFERENCES + u8 nDefer; /* Number of valid entries in aDefer[] */ + struct DeferredCsr { + Table *pTab; /* Table definition */ + int iCsr; /* Cursor number for table */ + int nKey; /* Number of PK columns for table pTab (>=1) */ + } aDefer[4]; +#endif + struct RowLoadInfo *pDeferredRowLoad; /* Deferred row loading info or NULL */ +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS + int addrPush; /* First instruction to push data into sorter */ + int addrPushEnd; /* Last instruction that pushes data into sorter */ +#endif }; #define SORTFLAG_UseSorter 0x01 /* Use SorterOpen instead of OpenEphemeral */ /* ** Delete all the content of a Select structure. Deallocate the structure -** itself only if bFree is true. +** itself depending on the value of bFree +** +** If bFree==1, call sqlite3DbFree() on the p object. +** If bFree==0, Leave the first Select object unfreed */ static void clearSelect(sqlite3 *db, Select *p, int bFree){ + assert( db!=0 ); while( p ){ Select *pPrior = p->pPrior; sqlite3ExprListDelete(db, p->pEList); @@ -73,9 +90,17 @@ static void clearSelect(sqlite3 *db, Select *p, int bFree){ sqlite3ExprDelete(db, p->pHaving); sqlite3ExprListDelete(db, p->pOrderBy); sqlite3ExprDelete(db, p->pLimit); - sqlite3ExprDelete(db, p->pOffset); - sqlite3WithDelete(db, p->pWith); - if( bFree ) sqlite3DbFree(db, p); + if( OK_IF_ALWAYS_TRUE(p->pWith) ) sqlite3WithDelete(db, p->pWith); +#ifndef SQLITE_OMIT_WINDOWFUNC + if( OK_IF_ALWAYS_TRUE(p->pWinDefn) ){ + sqlite3WindowListDelete(db, p->pWinDefn); + } + while( p->pWin ){ + assert( p->pWin->ppThis==&p->pWin ); + sqlite3WindowUnlinkFromSelect(p->pWin); + } +#endif + if( bFree ) sqlite3DbNNFreeNN(db, p); p = pPrior; bFree = 1; } @@ -87,7 +112,8 @@ static void clearSelect(sqlite3 *db, Select *p, int bFree){ void sqlite3SelectDestInit(SelectDest *pDest, int eDest, int iParm){ pDest->eDest = (u8)eDest; pDest->iSDParm = iParm; - pDest->affSdst = 0; + pDest->iSDParm2 = 0; + pDest->zAffSdst = 0; pDest->iSdst = 0; pDest->nSdst = 0; } @@ -105,33 +131,30 @@ Select *sqlite3SelectNew( ExprList *pGroupBy, /* the GROUP BY clause */ Expr *pHaving, /* the HAVING clause */ ExprList *pOrderBy, /* the ORDER BY clause */ - u16 selFlags, /* Flag parameters, such as SF_Distinct */ - Expr *pLimit, /* LIMIT value. NULL means not used */ - Expr *pOffset /* OFFSET value. NULL means no offset */ + u32 selFlags, /* Flag parameters, such as SF_Distinct */ + Expr *pLimit /* LIMIT value. NULL means not used */ ){ - Select *pNew; + Select *pNew, *pAllocated; Select standin; - sqlite3 *db = pParse->db; - pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew) ); + pAllocated = pNew = sqlite3DbMallocRawNN(pParse->db, sizeof(*pNew) ); if( pNew==0 ){ - assert( db->mallocFailed ); + assert( pParse->db->mallocFailed ); pNew = &standin; } if( pEList==0 ){ - pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db,TK_ASTERISK,0)); + pEList = sqlite3ExprListAppend(pParse, 0, + sqlite3Expr(pParse->db,TK_ASTERISK,0)); } pNew->pEList = pEList; pNew->op = TK_SELECT; pNew->selFlags = selFlags; pNew->iLimit = 0; pNew->iOffset = 0; -#if SELECTTRACE_ENABLED - pNew->zSelName[0] = 0; -#endif + pNew->selId = ++pParse->nSelect; pNew->addrOpenEphm[0] = -1; pNew->addrOpenEphm[1] = -1; pNew->nSelectRow = 0; - if( pSrc==0 ) pSrc = sqlite3DbMallocZero(db, sizeof(*pSrc)); + if( pSrc==0 ) pSrc = sqlite3DbMallocZero(pParse->db, SZ_SRCLIST_1); pNew->pSrc = pSrc; pNew->pWhere = pWhere; pNew->pGroupBy = pGroupBy; @@ -140,36 +163,29 @@ Select *sqlite3SelectNew( pNew->pPrior = 0; pNew->pNext = 0; pNew->pLimit = pLimit; - pNew->pOffset = pOffset; pNew->pWith = 0; - assert( pOffset==0 || pLimit!=0 || pParse->nErr>0 || db->mallocFailed!=0 ); - if( db->mallocFailed ) { - clearSelect(db, pNew, pNew!=&standin); - pNew = 0; +#ifndef SQLITE_OMIT_WINDOWFUNC + pNew->pWin = 0; + pNew->pWinDefn = 0; +#endif + if( pParse->db->mallocFailed ) { + clearSelect(pParse->db, pNew, pNew!=&standin); + pAllocated = 0; }else{ assert( pNew->pSrc!=0 || pParse->nErr>0 ); } - assert( pNew!=&standin ); - return pNew; -} - -#if SELECTTRACE_ENABLED -/* -** Set the name of a Select object -*/ -void sqlite3SelectSetName(Select *p, const char *zName){ - if( p && zName ){ - sqlite3_snprintf(sizeof(p->zSelName), p->zSelName, "%s", zName); - } + return pAllocated; } -#endif /* ** Delete the given Select structure and all of its substructures. */ void sqlite3SelectDelete(sqlite3 *db, Select *p){ - clearSelect(db, p, 1); + if( OK_IF_ALWAYS_TRUE(p) ) clearSelect(db, p, 1); +} +void sqlite3SelectDeleteGeneric(sqlite3 *db, void *p){ + if( ALWAYS(p) ) clearSelect(db, (Select*)p, 1); } /* @@ -196,6 +212,52 @@ static Select *findRightmost(Select *p){ ** ** If an illegal or unsupported join type is seen, then still return ** a join type, but put an error in the pParse structure. +** +** These are the valid join types: +** +** +** pA pB pC Return Value +** ------- ----- ----- ------------ +** CROSS - - JT_CROSS +** INNER - - JT_INNER +** LEFT - - JT_LEFT|JT_OUTER +** LEFT OUTER - JT_LEFT|JT_OUTER +** RIGHT - - JT_RIGHT|JT_OUTER +** RIGHT OUTER - JT_RIGHT|JT_OUTER +** FULL - - JT_LEFT|JT_RIGHT|JT_OUTER +** FULL OUTER - JT_LEFT|JT_RIGHT|JT_OUTER +** NATURAL INNER - JT_NATURAL|JT_INNER +** NATURAL LEFT - JT_NATURAL|JT_LEFT|JT_OUTER +** NATURAL LEFT OUTER JT_NATURAL|JT_LEFT|JT_OUTER +** NATURAL RIGHT - JT_NATURAL|JT_RIGHT|JT_OUTER +** NATURAL RIGHT OUTER JT_NATURAL|JT_RIGHT|JT_OUTER +** NATURAL FULL - JT_NATURAL|JT_LEFT|JT_RIGHT +** NATURAL FULL OUTER JT_NATRUAL|JT_LEFT|JT_RIGHT +** +** To preserve historical compatibly, SQLite also accepts a variety +** of other non-standard and in many cases nonsensical join types. +** This routine makes as much sense at it can from the nonsense join +** type and returns a result. Examples of accepted nonsense join types +** include but are not limited to: +** +** INNER CROSS JOIN -> same as JOIN +** NATURAL CROSS JOIN -> same as NATURAL JOIN +** OUTER LEFT JOIN -> same as LEFT JOIN +** LEFT NATURAL JOIN -> same as NATURAL LEFT JOIN +** LEFT RIGHT JOIN -> same as FULL JOIN +** RIGHT OUTER FULL JOIN -> same as FULL JOIN +** CROSS CROSS CROSS JOIN -> same as JOIN +** +** The only restrictions on the join type name are: +** +** * "INNER" cannot appear together with "OUTER", "LEFT", "RIGHT", +** or "FULL". +** +** * "CROSS" cannot appear together with "OUTER", "LEFT", "RIGHT, +** or "FULL". +** +** * If "OUTER" is present then there must also be one of +** "LEFT", "RIGHT", or "FULL" */ int sqlite3JoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){ int jointype = 0; @@ -208,13 +270,13 @@ int sqlite3JoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){ u8 nChar; /* Length of the keyword in characters */ u8 code; /* Join type mask */ } aKeyword[] = { - /* natural */ { 0, 7, JT_NATURAL }, - /* left */ { 6, 4, JT_LEFT|JT_OUTER }, - /* outer */ { 10, 5, JT_OUTER }, - /* right */ { 14, 5, JT_RIGHT|JT_OUTER }, - /* full */ { 19, 4, JT_LEFT|JT_RIGHT|JT_OUTER }, - /* inner */ { 23, 5, JT_INNER }, - /* cross */ { 28, 5, JT_INNER|JT_CROSS }, + /* (0) natural */ { 0, 7, JT_NATURAL }, + /* (1) left */ { 6, 4, JT_LEFT|JT_OUTER }, + /* (2) outer */ { 10, 5, JT_OUTER }, + /* (3) right */ { 14, 5, JT_RIGHT|JT_OUTER }, + /* (4) full */ { 19, 4, JT_LEFT|JT_RIGHT|JT_OUTER }, + /* (5) inner */ { 23, 5, JT_INNER }, + /* (6) cross */ { 28, 5, JT_INNER|JT_CROSS }, }; int i, j; apAll[0] = pA; @@ -223,7 +285,7 @@ int sqlite3JoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){ for(i=0; i<3 && apAll[i]; i++){ p = apAll[i]; for(j=0; jn==aKeyword[j].nChar + if( p->n==aKeyword[j].nChar && sqlite3StrNICmp((char*)p->z, &zKeyText[aKeyword[j].i], p->n)==0 ){ jointype |= aKeyword[j].code; break; @@ -237,18 +299,15 @@ int sqlite3JoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){ } if( (jointype & (JT_INNER|JT_OUTER))==(JT_INNER|JT_OUTER) || - (jointype & JT_ERROR)!=0 + (jointype & JT_ERROR)!=0 || + (jointype & (JT_OUTER|JT_LEFT|JT_RIGHT))==JT_OUTER ){ - const char *zSp = " "; - assert( pB!=0 ); - if( pC==0 ){ zSp++; } - sqlite3ErrorMsg(pParse, "unknown or unsupported join type: " - "%T %T%s%T", pA, pB, zSp, pC); - jointype = JT_INNER; - }else if( (jointype & JT_OUTER)!=0 - && (jointype & (JT_LEFT|JT_RIGHT))!=JT_LEFT ){ - sqlite3ErrorMsg(pParse, - "RIGHT and FULL OUTER JOINs are not currently supported"); + const char *zSp1 = " "; + const char *zSp2 = " "; + if( pB==0 ){ zSp1++; } + if( pC==0 ){ zSp2++; } + sqlite3ErrorMsg(pParse, "unknown join type: " + "%T%s%T%s%T", pA, zSp1, pB, zSp2, pC); jointype = JT_INNER; } return jointype; @@ -258,17 +317,61 @@ int sqlite3JoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){ ** Return the index of a column in a table. Return -1 if the column ** is not contained in the table. */ -static int columnIndex(Table *pTab, const char *zCol){ +int sqlite3ColumnIndex(Table *pTab, const char *zCol){ int i; - for(i=0; inCol; i++){ - if( sqlite3StrICmp(pTab->aCol[i].zName, zCol)==0 ) return i; + u8 h; + const Column *aCol; + int nCol; + + h = sqlite3StrIHash(zCol); + aCol = pTab->aCol; + nCol = pTab->nCol; + + /* See if the aHx gives us a lucky match */ + i = pTab->aHx[h % sizeof(pTab->aHx)]; + assert( i=nCol ) break; } return -1; } /* -** Search the first N tables in pSrc, from left to right, looking for a -** table that has a column named zCol. +** Mark a subquery result column as having been used. +*/ +void sqlite3SrcItemColumnUsed(SrcItem *pItem, int iCol){ + assert( pItem!=0 ); + assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem) ); + if( pItem->fg.isNestedFrom ){ + ExprList *pResults; + assert( pItem->fg.isSubquery ); + assert( pItem->u4.pSubq!=0 ); + assert( pItem->u4.pSubq->pSelect!=0 ); + pResults = pItem->u4.pSubq->pSelect->pEList; + assert( pResults!=0 ); + assert( iCol>=0 && iColnExpr ); + pResults->a[iCol].fg.bUsed = 1; + } +} + +/* +** Search the tables iStart..iEnd (inclusive) in pSrc, looking for a +** table that has a column named zCol. The search is left-to-right. +** The first match found is returned. ** ** When found, set *piTab and *piCol to the table index and column index ** of the matching column and return TRUE. @@ -277,19 +380,27 @@ static int columnIndex(Table *pTab, const char *zCol){ */ static int tableAndColumnIndex( SrcList *pSrc, /* Array of tables to search */ - int N, /* Number of tables in pSrc->a[] to search */ + int iStart, /* First member of pSrc->a[] to check */ + int iEnd, /* Last member of pSrc->a[] to check */ const char *zCol, /* Name of the column we are looking for */ int *piTab, /* Write index of pSrc->a[] here */ - int *piCol /* Write index of pSrc->a[*piTab].pTab->aCol[] here */ + int *piCol, /* Write index of pSrc->a[*piTab].pSTab->aCol[] here */ + int bIgnoreHidden /* Ignore hidden columns */ ){ int i; /* For looping over tables in pSrc */ int iCol; /* Index of column matching zCol */ + assert( iEndnSrc ); + assert( iStart>=0 ); assert( (piTab==0)==(piCol==0) ); /* Both or neither are NULL */ - for(i=0; ia[i].pTab, zCol); - if( iCol>=0 ){ + + for(i=iStart; i<=iEnd; i++){ + iCol = sqlite3ColumnIndex(pSrc->a[i].pSTab, zCol); + if( iCol>=0 + && (bIgnoreHidden==0 || IsHiddenColumn(&pSrc->a[i].pSTab->aCol[iCol])==0) + ){ if( piTab ){ + sqlite3SrcItemColumnUsed(&pSrc->a[i], iCol); *piTab = i; *piCol = iCol; } @@ -300,63 +411,19 @@ static int tableAndColumnIndex( } /* -** This function is used to add terms implied by JOIN syntax to the -** WHERE clause expression of a SELECT statement. The new term, which -** is ANDed with the existing WHERE clause, is of the form: -** -** (tab1.col1 = tab2.col2) -** -** where tab1 is the iSrc'th table in SrcList pSrc and tab2 is the -** (iSrc+1)'th. Column col1 is column iColLeft of tab1, and col2 is -** column iColRight of tab2. -*/ -static void addWhereTerm( - Parse *pParse, /* Parsing context */ - SrcList *pSrc, /* List of tables in FROM clause */ - int iLeft, /* Index of first table to join in pSrc */ - int iColLeft, /* Index of column in first table */ - int iRight, /* Index of second table in pSrc */ - int iColRight, /* Index of column in second table */ - int isOuterJoin, /* True if this is an OUTER join */ - Expr **ppWhere /* IN/OUT: The WHERE clause to add to */ -){ - sqlite3 *db = pParse->db; - Expr *pE1; - Expr *pE2; - Expr *pEq; - - assert( iLeftnSrc>iRight ); - assert( pSrc->a[iLeft].pTab ); - assert( pSrc->a[iRight].pTab ); - - pE1 = sqlite3CreateColumnExpr(db, pSrc, iLeft, iColLeft); - pE2 = sqlite3CreateColumnExpr(db, pSrc, iRight, iColRight); - - pEq = sqlite3PExpr(pParse, TK_EQ, pE1, pE2, 0); - if( pEq && isOuterJoin ){ - ExprSetProperty(pEq, EP_FromJoin); - assert( !ExprHasProperty(pEq, EP_TokenOnly|EP_Reduced) ); - ExprSetVVAProperty(pEq, EP_NoReduce); - pEq->iRightJoinTable = (i16)pE2->iTable; - } - *ppWhere = sqlite3ExprAnd(db, *ppWhere, pEq); -} - -/* -** Set the EP_FromJoin property on all terms of the given expression. -** And set the Expr.iRightJoinTable to iTable for every term in the +** Set the EP_OuterON property on all terms of the given expression. +** And set the Expr.w.iJoin to iTable for every term in the ** expression. ** -** The EP_FromJoin property is used on terms of an expression to tell -** the LEFT OUTER JOIN processing logic that this term is part of the +** The EP_OuterON property is used on terms of an expression to tell +** the OUTER JOIN processing logic that this term is part of the ** join restriction specified in the ON or USING clause and not a part ** of the more general WHERE clause. These terms are moved over to the ** WHERE clause during join processing but we need to remember that they ** originated in the ON or USING clause. ** -** The Expr.iRightJoinTable tells the WHERE clause processing that the -** expression depends on table iRightJoinTable even if that table is not +** The Expr.w.iJoin tells the WHERE clause processing that the +** expression depends on table w.iJoin even if that table is not ** explicitly mentioned in the expression. That information is needed ** for cases like this: ** @@ -369,132 +436,290 @@ static void addWhereTerm( ** after the t1 loop and rows with t1.x!=5 will never appear in ** the output, which is incorrect. */ -static void setJoinExpr(Expr *p, int iTable){ +void sqlite3SetJoinExpr(Expr *p, int iTable, u32 joinFlag){ + assert( joinFlag==EP_OuterON || joinFlag==EP_InnerON ); while( p ){ - ExprSetProperty(p, EP_FromJoin); + ExprSetProperty(p, joinFlag); assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) ); ExprSetVVAProperty(p, EP_NoReduce); - p->iRightJoinTable = (i16)iTable; - if( p->op==TK_FUNCTION && p->x.pList ){ - int i; - for(i=0; ix.pList->nExpr; i++){ - setJoinExpr(p->x.pList->a[i].pExpr, iTable); + p->w.iJoin = iTable; + if( ExprUseXList(p) ){ + if( p->x.pList ){ + int i; + for(i=0; ix.pList->nExpr; i++){ + sqlite3SetJoinExpr(p->x.pList->a[i].pExpr, iTable, joinFlag); + } + } + } + sqlite3SetJoinExpr(p->pLeft, iTable, joinFlag); + p = p->pRight; + } +} + +/* Undo the work of sqlite3SetJoinExpr(). This is used when a LEFT JOIN +** is simplified into an ordinary JOIN, and when an ON expression is +** "pushed down" into the WHERE clause of a subquery. +** +** Convert every term that is marked with EP_OuterON and w.iJoin==iTable into +** an ordinary term that omits the EP_OuterON mark. Or if iTable<0, then +** just clear every EP_OuterON and EP_InnerON mark from the expression tree. +** +** If nullable is true, that means that Expr p might evaluate to NULL even +** if it is a reference to a NOT NULL column. This can happen, for example, +** if the table that p references is on the left side of a RIGHT JOIN. +** If nullable is true, then take care to not remove the EP_CanBeNull bit. +** See forum thread https://sqlite.org/forum/forumpost/b40696f50145d21c +*/ +static void unsetJoinExpr(Expr *p, int iTable, int nullable){ + while( p ){ + if( iTable<0 || (ExprHasProperty(p, EP_OuterON) && p->w.iJoin==iTable) ){ + ExprClearProperty(p, EP_OuterON|EP_InnerON); + if( iTable>=0 ) ExprSetProperty(p, EP_InnerON); + } + if( p->op==TK_COLUMN && p->iTable==iTable && !nullable ){ + ExprClearProperty(p, EP_CanBeNull); + } + if( p->op==TK_FUNCTION ){ + assert( ExprUseXList(p) ); + assert( p->pLeft==0 ); + if( p->x.pList ){ + int i; + for(i=0; ix.pList->nExpr; i++){ + unsetJoinExpr(p->x.pList->a[i].pExpr, iTable, nullable); + } } } - setJoinExpr(p->pLeft, iTable); + unsetJoinExpr(p->pLeft, iTable, nullable); p = p->pRight; - } + } } /* ** This routine processes the join information for a SELECT statement. -** ON and USING clauses are converted into extra terms of the WHERE clause. -** NATURAL joins also create extra WHERE clause terms. +** +** * A NATURAL join is converted into a USING join. After that, we +** do not need to be concerned with NATURAL joins and we only have +** think about USING joins. +** +** * ON and USING clauses result in extra terms being added to the +** WHERE clause to enforce the specified constraints. The extra +** WHERE clause terms will be tagged with EP_OuterON or +** EP_InnerON so that we know that they originated in ON/USING. ** ** The terms of a FROM clause are contained in the Select.pSrc structure. ** The left most table is the first entry in Select.pSrc. The right-most ** table is the last entry. The join operator is held in the entry to -** the left. Thus entry 0 contains the join operator for the join between +** the right. Thus entry 1 contains the join operator for the join between ** entries 0 and 1. Any ON or USING clauses associated with the join are -** also attached to the left entry. +** also attached to the right entry. ** ** This routine returns the number of errors encountered. */ -static int sqliteProcessJoin(Parse *pParse, Select *p){ +static int sqlite3ProcessJoin(Parse *pParse, Select *p){ SrcList *pSrc; /* All tables in the FROM clause */ int i, j; /* Loop counters */ - struct SrcList_item *pLeft; /* Left table being joined */ - struct SrcList_item *pRight; /* Right table being joined */ + SrcItem *pLeft; /* Left table being joined */ + SrcItem *pRight; /* Right table being joined */ pSrc = p->pSrc; pLeft = &pSrc->a[0]; pRight = &pLeft[1]; for(i=0; inSrc-1; i++, pRight++, pLeft++){ - Table *pLeftTab = pLeft->pTab; - Table *pRightTab = pRight->pTab; - int isOuter; + Table *pRightTab = pRight->pSTab; + u32 joinType; - if( NEVER(pLeftTab==0 || pRightTab==0) ) continue; - isOuter = (pRight->fg.jointype & JT_OUTER)!=0; + if( NEVER(pLeft->pSTab==0 || pRightTab==0) ) continue; + joinType = (pRight->fg.jointype & JT_OUTER)!=0 ? EP_OuterON : EP_InnerON; - /* When the NATURAL keyword is present, add WHERE clause terms for - ** every column that the two tables have in common. + /* If this is a NATURAL join, synthesize an appropriate USING clause + ** to specify which columns should be joined. */ if( pRight->fg.jointype & JT_NATURAL ){ - if( pRight->pOn || pRight->pUsing ){ + IdList *pUsing = 0; + if( pRight->fg.isUsing || pRight->u3.pOn ){ sqlite3ErrorMsg(pParse, "a NATURAL join may not have " "an ON or USING clause", 0); return 1; } for(j=0; jnCol; j++){ char *zName; /* Name of column in the right table */ - int iLeft; /* Matching left table */ - int iLeftCol; /* Matching column in the left table */ - zName = pRightTab->aCol[j].zName; - if( tableAndColumnIndex(pSrc, i+1, zName, &iLeft, &iLeftCol) ){ - addWhereTerm(pParse, pSrc, iLeft, iLeftCol, i+1, j, - isOuter, &p->pWhere); + if( IsHiddenColumn(&pRightTab->aCol[j]) ) continue; + zName = pRightTab->aCol[j].zCnName; + if( tableAndColumnIndex(pSrc, 0, i, zName, 0, 0, 1) ){ + pUsing = sqlite3IdListAppend(pParse, pUsing, 0); + if( pUsing ){ + assert( pUsing->nId>0 ); + assert( pUsing->a[pUsing->nId-1].zName==0 ); + pUsing->a[pUsing->nId-1].zName = sqlite3DbStrDup(pParse->db, zName); + } } } - } - - /* Disallow both ON and USING clauses in the same join - */ - if( pRight->pOn && pRight->pUsing ){ - sqlite3ErrorMsg(pParse, "cannot have both ON and USING " - "clauses in the same join"); - return 1; - } - - /* Add the ON clause to the end of the WHERE clause, connected by - ** an AND operator. - */ - if( pRight->pOn ){ - if( isOuter ) setJoinExpr(pRight->pOn, pRight->iCursor); - p->pWhere = sqlite3ExprAnd(pParse->db, p->pWhere, pRight->pOn); - pRight->pOn = 0; + if( pUsing ){ + pRight->fg.isUsing = 1; + pRight->fg.isSynthUsing = 1; + pRight->u3.pUsing = pUsing; + } + if( pParse->nErr ) return 1; } /* Create extra terms on the WHERE clause for each column named - ** in the USING clause. Example: If the two tables to be joined are + ** in the USING clause. Example: If the two tables to be joined are ** A and B and the USING clause names X, Y, and Z, then add this ** to the WHERE clause: A.X=B.X AND A.Y=B.Y AND A.Z=B.Z ** Report an error if any column mentioned in the USING clause is ** not contained in both tables to be joined. */ - if( pRight->pUsing ){ - IdList *pList = pRight->pUsing; + if( pRight->fg.isUsing ){ + IdList *pList = pRight->u3.pUsing; + sqlite3 *db = pParse->db; + assert( pList!=0 ); for(j=0; jnId; j++){ char *zName; /* Name of the term in the USING clause */ int iLeft; /* Table on the left with matching column name */ int iLeftCol; /* Column number of matching column on the left */ int iRightCol; /* Column number of matching column on the right */ + Expr *pE1; /* Reference to the column on the LEFT of the join */ + Expr *pE2; /* Reference to the column on the RIGHT of the join */ + Expr *pEq; /* Equality constraint. pE1 == pE2 */ zName = pList->a[j].zName; - iRightCol = columnIndex(pRightTab, zName); + iRightCol = sqlite3ColumnIndex(pRightTab, zName); if( iRightCol<0 - || !tableAndColumnIndex(pSrc, i+1, zName, &iLeft, &iLeftCol) + || tableAndColumnIndex(pSrc, 0, i, zName, &iLeft, &iLeftCol, + pRight->fg.isSynthUsing)==0 ){ sqlite3ErrorMsg(pParse, "cannot join using column %s - column " "not present in both tables", zName); return 1; } - addWhereTerm(pParse, pSrc, iLeft, iLeftCol, i+1, iRightCol, - isOuter, &p->pWhere); + pE1 = sqlite3CreateColumnExpr(db, pSrc, iLeft, iLeftCol); + sqlite3SrcItemColumnUsed(&pSrc->a[iLeft], iLeftCol); + if( (pSrc->a[0].fg.jointype & JT_LTORJ)!=0 && pParse->nErr==0 ){ + /* This branch runs if the query contains one or more RIGHT or FULL + ** JOINs. If only a single table on the left side of this join + ** contains the zName column, then this branch is a no-op. + ** But if there are two or more tables on the left side + ** of the join, construct a coalesce() function that gathers all + ** such tables. Raise an error if more than one of those references + ** to zName is not also within a prior USING clause. + ** + ** We really ought to raise an error if there are two or more + ** non-USING references to zName on the left of an INNER or LEFT + ** JOIN. But older versions of SQLite do not do that, so we avoid + ** adding a new error so as to not break legacy applications. + */ + ExprList *pFuncArgs = 0; /* Arguments to the coalesce() */ + static const Token tkCoalesce = { "coalesce", 8 }; + assert( pE1!=0 ); + ExprSetProperty(pE1, EP_CanBeNull); + while( tableAndColumnIndex(pSrc, iLeft+1, i, zName, &iLeft, &iLeftCol, + pRight->fg.isSynthUsing)!=0 ){ + if( pSrc->a[iLeft].fg.isUsing==0 + || sqlite3IdListIndex(pSrc->a[iLeft].u3.pUsing, zName)<0 + ){ + sqlite3ErrorMsg(pParse, "ambiguous reference to %s in USING()", + zName); + break; + } + pFuncArgs = sqlite3ExprListAppend(pParse, pFuncArgs, pE1); + pE1 = sqlite3CreateColumnExpr(db, pSrc, iLeft, iLeftCol); + sqlite3SrcItemColumnUsed(&pSrc->a[iLeft], iLeftCol); + } + if( pFuncArgs ){ + pFuncArgs = sqlite3ExprListAppend(pParse, pFuncArgs, pE1); + pE1 = sqlite3ExprFunction(pParse, pFuncArgs, &tkCoalesce, 0); + if( pE1 ){ + pE1->affExpr = SQLITE_AFF_DEFER; + } + } + }else if( (pSrc->a[i+1].fg.jointype & JT_LEFT)!=0 && pParse->nErr==0 ){ + assert( pE1!=0 ); + ExprSetProperty(pE1, EP_CanBeNull); + } + pE2 = sqlite3CreateColumnExpr(db, pSrc, i+1, iRightCol); + sqlite3SrcItemColumnUsed(pRight, iRightCol); + pEq = sqlite3PExpr(pParse, TK_EQ, pE1, pE2); + assert( pE2!=0 || pEq==0 ); + if( pEq ){ + ExprSetProperty(pEq, joinType); + assert( !ExprHasProperty(pEq, EP_TokenOnly|EP_Reduced) ); + ExprSetVVAProperty(pEq, EP_NoReduce); + pEq->w.iJoin = pE2->iTable; + } + p->pWhere = sqlite3ExprAnd(pParse, p->pWhere, pEq); } } + + /* Add the ON clause to the end of the WHERE clause, connected by + ** an AND operator. + */ + else if( pRight->u3.pOn ){ + sqlite3SetJoinExpr(pRight->u3.pOn, pRight->iCursor, joinType); + p->pWhere = sqlite3ExprAnd(pParse, p->pWhere, pRight->u3.pOn); + pRight->u3.pOn = 0; + pRight->fg.isOn = 1; + p->selFlags |= SF_OnToWhere; + } } return 0; } -/* Forward reference */ -static KeyInfo *keyInfoFromExprList( - Parse *pParse, /* Parsing context */ - ExprList *pList, /* Form the KeyInfo object from this ExprList */ - int iStart, /* Begin with this column of pList */ - int nExtra /* Add this many extra columns to the end */ -); +/* +** An instance of this object holds information (beyond pParse and pSelect) +** needed to load the next result row that is to be added to the sorter. +*/ +typedef struct RowLoadInfo RowLoadInfo; +struct RowLoadInfo { + int regResult; /* Store results in array of registers here */ + u8 ecelFlags; /* Flag argument to ExprCodeExprList() */ +#ifdef SQLITE_ENABLE_SORTER_REFERENCES + ExprList *pExtra; /* Extra columns needed by sorter refs */ + int regExtraResult; /* Where to load the extra columns */ +#endif +}; + +/* +** This routine does the work of loading query data into an array of +** registers so that it can be added to the sorter. +*/ +static void innerLoopLoadRow( + Parse *pParse, /* Statement under construction */ + Select *pSelect, /* The query being coded */ + RowLoadInfo *pInfo /* Info needed to complete the row load */ +){ + sqlite3ExprCodeExprList(pParse, pSelect->pEList, pInfo->regResult, + 0, pInfo->ecelFlags); +#ifdef SQLITE_ENABLE_SORTER_REFERENCES + if( pInfo->pExtra ){ + sqlite3ExprCodeExprList(pParse, pInfo->pExtra, pInfo->regExtraResult, 0, 0); + sqlite3ExprListDelete(pParse->db, pInfo->pExtra); + } +#endif +} + +/* +** Code the OP_MakeRecord instruction that generates the entry to be +** added into the sorter. +** +** Return the register in which the result is stored. +*/ +static int makeSorterRecord( + Parse *pParse, + SortCtx *pSort, + Select *pSelect, + int regBase, + int nBase +){ + int nOBSat = pSort->nOBSat; + Vdbe *v = pParse->pVdbe; + int regOut = ++pParse->nMem; + if( pSort->pDeferredRowLoad ){ + innerLoopLoadRow(pParse, pSelect, pSort->pDeferredRowLoad); + } + sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase+nOBSat, nBase-nOBSat, regOut); + return regOut; +} /* ** Generate code that will push the record in registers regData @@ -506,7 +731,7 @@ static void pushOntoSorter( Select *pSelect, /* The whole SELECT statement */ int regData, /* First register holding data to be sorted */ int regOrigData, /* First register holding data before packing */ - int nData, /* Number of elements in the data array */ + int nData, /* Number of elements in the regData data array */ int nPrefixReg /* No. of reg prior to regData available for use */ ){ Vdbe *v = pParse->pVdbe; /* Stmt under construction */ @@ -514,32 +739,51 @@ static void pushOntoSorter( int nExpr = pSort->pOrderBy->nExpr; /* No. of ORDER BY terms */ int nBase = nExpr + bSeq + nData; /* Fields in sorter record */ int regBase; /* Regs for sorter record */ - int regRecord = ++pParse->nMem; /* Assembled sorter record */ + int regRecord = 0; /* Assembled sorter record */ int nOBSat = pSort->nOBSat; /* ORDER BY terms to skip */ int op; /* Opcode to add sorter record to sorter */ int iLimit; /* LIMIT counter */ + int iSkip = 0; /* End of the sorter insert loop */ assert( bSeq==0 || bSeq==1 ); - assert( nData==1 || regData==regOrigData ); + + /* Three cases: + ** (1) The data to be sorted has already been packed into a Record + ** by a prior OP_MakeRecord. In this case nData==1 and regData + ** will be completely unrelated to regOrigData. + ** (2) All output columns are included in the sort record. In that + ** case regData==regOrigData. + ** (3) Some output columns are omitted from the sort record due to + ** the SQLITE_ENABLE_SORTER_REFERENCES optimization, or due to the + ** SQLITE_ECEL_OMITREF optimization, or due to the + ** SortCtx.pDeferredRowLoad optimization. In any of these cases + ** regOrigData is 0 to prevent this routine from trying to copy + ** values that might not yet exist. + */ + assert( nData==1 || regData==regOrigData || regOrigData==0 ); + +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS + pSort->addrPush = sqlite3VdbeCurrentAddr(v); +#endif + if( nPrefixReg ){ assert( nPrefixReg==nExpr+bSeq ); - regBase = regData - nExpr - bSeq; + regBase = regData - nPrefixReg; }else{ regBase = pParse->nMem + 1; pParse->nMem += nBase; } assert( pSelect->iOffset==0 || pSelect->iLimit!=0 ); iLimit = pSelect->iOffset ? pSelect->iOffset+1 : pSelect->iLimit; - pSort->labelDone = sqlite3VdbeMakeLabel(v); + pSort->labelDone = sqlite3VdbeMakeLabel(pParse); sqlite3ExprCodeExprList(pParse, pSort->pOrderBy, regBase, regOrigData, - SQLITE_ECEL_DUP|SQLITE_ECEL_REF); + SQLITE_ECEL_DUP | (regOrigData? SQLITE_ECEL_REF : 0)); if( bSeq ){ sqlite3VdbeAddOp2(v, OP_Sequence, pSort->iECursor, regBase+nExpr); } - if( nPrefixReg==0 ){ + if( nPrefixReg==0 && nData>0 ){ sqlite3ExprCodeMove(pParse, regData, regBase+nExpr+bSeq, nData); } - sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase+nOBSat, nBase-nOBSat, regRecord); if( nOBSat>0 ){ int regPrevKey; /* The first nOBSat columns of the previous row */ int addrFirst; /* Address of the OP_IfNot opcode */ @@ -548,11 +792,12 @@ static void pushOntoSorter( int nKey; /* Number of sorting key columns, including OP_Sequence */ KeyInfo *pKI; /* Original KeyInfo on the sorter table */ + regRecord = makeSorterRecord(pParse, pSort, pSelect, regBase, nBase); regPrevKey = pParse->nMem+1; pParse->nMem += pSort->nOBSat; nKey = nExpr - pSort->nOBSat + bSeq; if( bSeq ){ - addrFirst = sqlite3VdbeAddOp1(v, OP_IfNot, regBase+nExpr); + addrFirst = sqlite3VdbeAddOp1(v, OP_IfNot, regBase+nExpr); }else{ addrFirst = sqlite3VdbeAddOp1(v, OP_SequenceTest, pSort->iECursor); } @@ -562,14 +807,15 @@ static void pushOntoSorter( if( pParse->db->mallocFailed ) return; pOp->p2 = nKey + nData; pKI = pOp->p4.pKeyInfo; - memset(pKI->aSortOrder, 0, pKI->nField); /* Makes OP_Jump below testable */ + memset(pKI->aSortFlags, 0, pKI->nKeyField); /* Makes OP_Jump testable */ sqlite3VdbeChangeP4(v, -1, (char*)pKI, P4_KEYINFO); - testcase( pKI->nXField>2 ); - pOp->p4.pKeyInfo = keyInfoFromExprList(pParse, pSort->pOrderBy, nOBSat, - pKI->nXField-1); + testcase( pKI->nAllField > pKI->nKeyField+2 ); + pOp->p4.pKeyInfo = sqlite3KeyInfoFromExprList(pParse,pSort->pOrderBy,nOBSat, + pKI->nAllField-pKI->nKeyField-1); + pOp = 0; /* Ensure pOp not used after sqlite3VdbeAddOp3() */ addrJmp = sqlite3VdbeCurrentAddr(v); sqlite3VdbeAddOp3(v, OP_Jump, addrJmp+1, 0, addrJmp+1); VdbeCoverage(v); - pSort->labelBkOut = sqlite3VdbeMakeLabel(v); + pSort->labelBkOut = sqlite3VdbeMakeLabel(pParse); pSort->regReturn = ++pParse->nMem; sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut); sqlite3VdbeAddOp1(v, OP_ResetSorter, pSort->iECursor); @@ -581,19 +827,48 @@ static void pushOntoSorter( sqlite3ExprCodeMove(pParse, regBase, regPrevKey, pSort->nOBSat); sqlite3VdbeJumpHere(v, addrJmp); } + if( iLimit ){ + /* At this point the values for the new sorter entry are stored + ** in an array of registers. They need to be composed into a record + ** and inserted into the sorter if either (a) there are currently + ** less than LIMIT+OFFSET items or (b) the new record is smaller than + ** the largest record currently in the sorter. If (b) is true and there + ** are already LIMIT+OFFSET items in the sorter, delete the largest + ** entry before inserting the new one. This way there are never more + ** than LIMIT+OFFSET items in the sorter. + ** + ** If the new record does not need to be inserted into the sorter, + ** jump to the next iteration of the loop. If the pSort->labelOBLopt + ** value is not zero, then it is a label of where to jump. Otherwise, + ** just bypass the row insert logic. See the header comment on the + ** sqlite3WhereOrderByLimitOptLabel() function for additional info. + */ + int iCsr = pSort->iECursor; + sqlite3VdbeAddOp2(v, OP_IfNotZero, iLimit, sqlite3VdbeCurrentAddr(v)+4); + VdbeCoverage(v); + sqlite3VdbeAddOp2(v, OP_Last, iCsr, 0); + iSkip = sqlite3VdbeAddOp4Int(v, OP_IdxLE, + iCsr, 0, regBase+nOBSat, nExpr-nOBSat); + VdbeCoverage(v); + sqlite3VdbeAddOp1(v, OP_Delete, iCsr); + } + if( regRecord==0 ){ + regRecord = makeSorterRecord(pParse, pSort, pSelect, regBase, nBase); + } if( pSort->sortFlags & SORTFLAG_UseSorter ){ op = OP_SorterInsert; }else{ op = OP_IdxInsert; } - sqlite3VdbeAddOp2(v, op, pSort->iECursor, regRecord); - if( iLimit ){ - int addr; - addr = sqlite3VdbeAddOp3(v, OP_IfNotZero, iLimit, 0, 1); VdbeCoverage(v); - sqlite3VdbeAddOp1(v, OP_Last, pSort->iECursor); - sqlite3VdbeAddOp1(v, OP_Delete, pSort->iECursor); - sqlite3VdbeJumpHere(v, addr); + sqlite3VdbeAddOp4Int(v, op, pSort->iECursor, regRecord, + regBase+nOBSat, nBase-nOBSat); + if( iSkip ){ + sqlite3VdbeChangeP2(v, iSkip, + pSort->labelOBLopt ? pSort->labelOBLopt : sqlite3VdbeCurrentAddr(v)); } +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS + pSort->addrPushEnd = sqlite3VdbeCurrentAddr(v)-1; +#endif } /* @@ -611,53 +886,242 @@ static void codeOffset( } /* -** Add code that will check to make sure the N registers starting at iMem -** form a distinct entry. iTab is a sorting index that holds previously -** seen combinations of the N values. A new entry is made in iTab -** if the current N values are new. -** -** A jump to addrRepeat is made and the N+1 values are popped from the -** stack if the top N elements are not distinct. +** Add code that will check to make sure the array of registers starting at +** iMem form a distinct entry. This is used by both "SELECT DISTINCT ..." and +** distinct aggregates ("SELECT count(DISTINCT ) ..."). Three strategies +** are available. Which is used depends on the value of parameter eTnctType, +** as follows: +** +** WHERE_DISTINCT_UNORDERED/WHERE_DISTINCT_NOOP: +** Build an ephemeral table that contains all entries seen before and +** skip entries which have been seen before. +** +** Parameter iTab is the cursor number of an ephemeral table that must +** be opened before the VM code generated by this routine is executed. +** The ephemeral cursor table is queried for a record identical to the +** record formed by the current array of registers. If one is found, +** jump to VM address addrRepeat. Otherwise, insert a new record into +** the ephemeral cursor and proceed. +** +** The returned value in this case is a copy of parameter iTab. +** +** WHERE_DISTINCT_ORDERED: +** In this case rows are being delivered sorted order. The ephemeral +** table is not required. Instead, the current set of values +** is compared against previous row. If they match, the new row +** is not distinct and control jumps to VM address addrRepeat. Otherwise, +** the VM program proceeds with processing the new row. +** +** The returned value in this case is the register number of the first +** in an array of registers used to store the previous result row so that +** it can be compared to the next. The caller must ensure that this +** register is initialized to NULL. (The fixDistinctOpenEph() routine +** will take care of this initialization.) +** +** WHERE_DISTINCT_UNIQUE: +** In this case it has already been determined that the rows are distinct. +** No special action is required. The return value is zero. +** +** Parameter pEList is the list of expressions used to generated the +** contents of each row. It is used by this routine to determine (a) +** how many elements there are in the array of registers and (b) the +** collation sequences that should be used for the comparisons if +** eTnctType is WHERE_DISTINCT_ORDERED. */ -static void codeDistinct( +static int codeDistinct( Parse *pParse, /* Parsing and code generating context */ + int eTnctType, /* WHERE_DISTINCT_* value */ int iTab, /* A sorting index used to test for distinctness */ int addrRepeat, /* Jump to here if not distinct */ - int N, /* Number of elements */ - int iMem /* First element */ + ExprList *pEList, /* Expression for each element */ + int regElem /* First element */ ){ - Vdbe *v; - int r1; + int iRet = 0; + int nResultCol = pEList->nExpr; + Vdbe *v = pParse->pVdbe; - v = pParse->pVdbe; - r1 = sqlite3GetTempReg(pParse); - sqlite3VdbeAddOp4Int(v, OP_Found, iTab, addrRepeat, iMem, N); VdbeCoverage(v); - sqlite3VdbeAddOp3(v, OP_MakeRecord, iMem, N, r1); - sqlite3VdbeAddOp2(v, OP_IdxInsert, iTab, r1); - sqlite3ReleaseTempReg(pParse, r1); + switch( eTnctType ){ + case WHERE_DISTINCT_ORDERED: { + int i; + int iJump; /* Jump destination */ + int regPrev; /* Previous row content */ + + /* Allocate space for the previous row */ + iRet = regPrev = pParse->nMem+1; + pParse->nMem += nResultCol; + + iJump = sqlite3VdbeCurrentAddr(v) + nResultCol; + for(i=0; ia[i].pExpr); + if( idb->mallocFailed ); + sqlite3VdbeAddOp3(v, OP_Copy, regElem, regPrev, nResultCol-1); + break; + } + + case WHERE_DISTINCT_UNIQUE: { + /* nothing to do */ + break; + } + + default: { + int r1 = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp4Int(v, OP_Found, iTab, addrRepeat, regElem, nResultCol); + VdbeCoverage(v); + sqlite3VdbeAddOp3(v, OP_MakeRecord, regElem, nResultCol, r1); + sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iTab, r1, regElem, nResultCol); + sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); + sqlite3ReleaseTempReg(pParse, r1); + iRet = iTab; + break; + } + } + + return iRet; } -#ifndef SQLITE_OMIT_SUBQUERY /* -** Generate an error message when a SELECT is used within a subexpression -** (example: "a IN (SELECT * FROM table)") but it has more than 1 result -** column. We do this in a subroutine because the error used to occur -** in multiple places. (The error only occurs in one place now, but we -** retain the subroutine to minimize code disruption.) +** This routine runs after codeDistinct(). It makes necessary +** adjustments to the OP_OpenEphemeral opcode that the codeDistinct() +** routine made use of. This processing must be done separately since +** sometimes codeDistinct is called before the OP_OpenEphemeral is actually +** laid down. +** +** WHERE_DISTINCT_NOOP: +** WHERE_DISTINCT_UNORDERED: +** +** No adjustments necessary. This function is a no-op. +** +** WHERE_DISTINCT_UNIQUE: +** +** The ephemeral table is not needed. So change the +** OP_OpenEphemeral opcode into an OP_Noop. +** +** WHERE_DISTINCT_ORDERED: +** +** The ephemeral table is not needed. But we do need register +** iVal to be initialized to NULL. So change the OP_OpenEphemeral +** into an OP_Null on the iVal register. */ -static int checkForMultiColumnSelectError( - Parse *pParse, /* Parse context. */ - SelectDest *pDest, /* Destination of SELECT results */ - int nExpr /* Number of result columns returned by SELECT */ +static void fixDistinctOpenEph( + Parse *pParse, /* Parsing and code generating context */ + int eTnctType, /* WHERE_DISTINCT_* value */ + int iVal, /* Value returned by codeDistinct() */ + int iOpenEphAddr /* Address of OP_OpenEphemeral instruction for iTab */ ){ - int eDest = pDest->eDest; - if( nExpr>1 && (eDest==SRT_Mem || eDest==SRT_Set) ){ - sqlite3ErrorMsg(pParse, "only a single result allowed for " - "a SELECT that is part of an expression"); - return 1; - }else{ - return 0; + if( pParse->nErr==0 + && (eTnctType==WHERE_DISTINCT_UNIQUE || eTnctType==WHERE_DISTINCT_ORDERED) + ){ + Vdbe *v = pParse->pVdbe; + sqlite3VdbeChangeToNoop(v, iOpenEphAddr); + if( sqlite3VdbeGetOp(v, iOpenEphAddr+1)->opcode==OP_Explain ){ + sqlite3VdbeChangeToNoop(v, iOpenEphAddr+1); + } + if( eTnctType==WHERE_DISTINCT_ORDERED ){ + /* Change the OP_OpenEphemeral to an OP_Null that sets the MEM_Cleared + ** bit on the first register of the previous value. This will cause the + ** OP_Ne added in codeDistinct() to always fail on the first iteration of + ** the loop even if the first row is all NULLs. */ + VdbeOp *pOp = sqlite3VdbeGetOp(v, iOpenEphAddr); + pOp->opcode = OP_Null; + pOp->p1 = 1; + pOp->p2 = iVal; + } + } +} + +#ifdef SQLITE_ENABLE_SORTER_REFERENCES +/* +** This function is called as part of inner-loop generation for a SELECT +** statement with an ORDER BY that is not optimized by an index. It +** determines the expressions, if any, that the sorter-reference +** optimization should be used for. The sorter-reference optimization +** is used for SELECT queries like: +** +** SELECT a, bigblob FROM t1 ORDER BY a LIMIT 10 +** +** If the optimization is used for expression "bigblob", then instead of +** storing values read from that column in the sorter records, the PK of +** the row from table t1 is stored instead. Then, as records are extracted from +** the sorter to return to the user, the required value of bigblob is +** retrieved directly from table t1. If the values are very large, this +** can be more efficient than storing them directly in the sorter records. +** +** The ExprList_item.fg.bSorterRef flag is set for each expression in pEList +** for which the sorter-reference optimization should be enabled. +** Additionally, the pSort->aDefer[] array is populated with entries +** for all cursors required to evaluate all selected expressions. Finally. +** output variable (*ppExtra) is set to an expression list containing +** expressions for all extra PK values that should be stored in the +** sorter records. +*/ +static void selectExprDefer( + Parse *pParse, /* Leave any error here */ + SortCtx *pSort, /* Sorter context */ + ExprList *pEList, /* Expressions destined for sorter */ + ExprList **ppExtra /* Expressions to append to sorter record */ +){ + int i; + int nDefer = 0; + ExprList *pExtra = 0; + for(i=0; inExpr; i++){ + struct ExprList_item *pItem = &pEList->a[i]; + if( pItem->u.x.iOrderByCol==0 ){ + Expr *pExpr = pItem->pExpr; + Table *pTab; + if( pExpr->op==TK_COLUMN + && pExpr->iColumn>=0 + && ALWAYS( ExprUseYTab(pExpr) ) + && (pTab = pExpr->y.pTab)!=0 + && IsOrdinaryTable(pTab) + && (pTab->aCol[pExpr->iColumn].colFlags & COLFLAG_SORTERREF)!=0 + ){ + int j; + for(j=0; jaDefer[j].iCsr==pExpr->iTable ) break; + } + if( j==nDefer ){ + if( nDefer==ArraySize(pSort->aDefer) ){ + continue; + }else{ + int nKey = 1; + int k; + Index *pPk = 0; + if( !HasRowid(pTab) ){ + pPk = sqlite3PrimaryKeyIndex(pTab); + nKey = pPk->nKeyCol; + } + for(k=0; kiTable = pExpr->iTable; + assert( ExprUseYTab(pNew) ); + pNew->y.pTab = pExpr->y.pTab; + pNew->iColumn = pPk ? pPk->aiColumn[k] : -1; + pExtra = sqlite3ExprListAppend(pParse, pExtra, pNew); + } + } + pSort->aDefer[nDefer].pTab = pExpr->y.pTab; + pSort->aDefer[nDefer].iCsr = pExpr->iTable; + pSort->aDefer[nDefer].nKey = nKey; + nDefer++; + } + } + pItem->fg.bSorterRef = 1; + } + } } + pSort->nDefer = (u8)nDefer; + *ppExtra = pExtra; } #endif @@ -665,16 +1129,15 @@ static int checkForMultiColumnSelectError( ** This routine generates the code for the inside of the inner loop ** of a SELECT. ** -** If srcTab is negative, then the pEList expressions +** If srcTab is negative, then the p->pEList expressions ** are evaluated in order to get the data for this row. If srcTab is -** zero or more, then data is pulled from srcTab and pEList is used only -** to get number columns and the datatype for each column. +** zero or more, then data is pulled from srcTab and p->pEList is used only +** to get the number of columns and the collation sequence for each column. */ static void selectInnerLoop( Parse *pParse, /* The parser context */ Select *p, /* The complete select statement being coded */ - ExprList *pEList, /* List of values being extracted */ - int srcTab, /* Pull data from this table */ + int srcTab, /* Pull data from this table if non-negative */ SortCtx *pSort, /* If not NULL, info on how to process ORDER BY */ DistinctCtx *pDistinct, /* If not NULL, info on how to process DISTINCT */ SelectDest *pDest, /* How to dispose of the results */ @@ -683,15 +1146,23 @@ static void selectInnerLoop( ){ Vdbe *v = pParse->pVdbe; int i; - int hasDistinct; /* True if the DISTINCT keyword is present */ - int regResult; /* Start of memory holding result set */ + int hasDistinct; /* True if the DISTINCT keyword is present */ int eDest = pDest->eDest; /* How to dispose of results */ int iParm = pDest->iSDParm; /* First argument to disposal method */ int nResultCol; /* Number of result columns */ int nPrefixReg = 0; /* Number of extra registers before regResult */ + RowLoadInfo sRowLoadInfo; /* Info for deferred row loading */ + + /* Usually, regResult is the first cell in an array of memory cells + ** containing the current result row. In this case regOrig is set to the + ** same value. However, if the results are being sent to the sorter, the + ** values for any expressions that are also part of the sort-key are omitted + ** from this array. In this case regOrig is set to zero. */ + int regResult; /* Start of memory holding current results */ + int regOrig; /* Start of memory holding full result (or 0) */ assert( v ); - assert( pEList!=0 ); + assert( p->pEList!=0 ); hasDistinct = pDistinct ? pDistinct->eTnctType : WHERE_DISTINCT_NOOP; if( pSort && pSort->pOrderBy==0 ) pSort = 0; if( pSort==0 && !hasDistinct ){ @@ -701,7 +1172,7 @@ static void selectInnerLoop( /* Pull the requested columns. */ - nResultCol = pEList->nExpr; + nResultCol = p->pEList->nExpr; if( pDest->iSdst==0 ){ if( pSort ){ @@ -720,23 +1191,97 @@ static void selectInnerLoop( pParse->nMem += nResultCol; } pDest->nSdst = nResultCol; - regResult = pDest->iSdst; + regOrig = regResult = pDest->iSdst; if( srcTab>=0 ){ for(i=0; ia[i].zName)); + VdbeComment((v, "%s", p->pEList->a[i].zEName)); } }else if( eDest!=SRT_Exists ){ +#ifdef SQLITE_ENABLE_SORTER_REFERENCES + ExprList *pExtra = 0; +#endif /* If the destination is an EXISTS(...) expression, the actual ** values returned by the SELECT are not required. */ - u8 ecelFlags; + u8 ecelFlags; /* "ecel" is an abbreviation of "ExprCodeExprList" */ + ExprList *pEList; if( eDest==SRT_Mem || eDest==SRT_Output || eDest==SRT_Coroutine ){ ecelFlags = SQLITE_ECEL_DUP; }else{ ecelFlags = 0; } - sqlite3ExprCodeExprList(pParse, pEList, regResult, 0, ecelFlags); + if( pSort && hasDistinct==0 && eDest!=SRT_EphemTab && eDest!=SRT_Table ){ + /* For each expression in p->pEList that is a copy of an expression in + ** the ORDER BY clause (pSort->pOrderBy), set the associated + ** iOrderByCol value to one more than the index of the ORDER BY + ** expression within the sort-key that pushOntoSorter() will generate. + ** This allows the p->pEList field to be omitted from the sorted record, + ** saving space and CPU cycles. */ + ecelFlags |= (SQLITE_ECEL_OMITREF|SQLITE_ECEL_REF); + + for(i=pSort->nOBSat; ipOrderBy->nExpr; i++){ + int j; + if( (j = pSort->pOrderBy->a[i].u.x.iOrderByCol)>0 ){ + p->pEList->a[j-1].u.x.iOrderByCol = i+1-pSort->nOBSat; + } + } +#ifdef SQLITE_ENABLE_SORTER_REFERENCES + selectExprDefer(pParse, pSort, p->pEList, &pExtra); + if( pExtra && pParse->db->mallocFailed==0 ){ + /* If there are any extra PK columns to add to the sorter records, + ** allocate extra memory cells and adjust the OpenEphemeral + ** instruction to account for the larger records. This is only + ** required if there are one or more WITHOUT ROWID tables with + ** composite primary keys in the SortCtx.aDefer[] array. */ + VdbeOp *pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex); + pOp->p2 += (pExtra->nExpr - pSort->nDefer); + pOp->p4.pKeyInfo->nAllField += (pExtra->nExpr - pSort->nDefer); + pParse->nMem += pExtra->nExpr; + } +#endif + + /* Adjust nResultCol to account for columns that are omitted + ** from the sorter by the optimizations in this branch */ + pEList = p->pEList; + for(i=0; inExpr; i++){ + if( pEList->a[i].u.x.iOrderByCol>0 +#ifdef SQLITE_ENABLE_SORTER_REFERENCES + || pEList->a[i].fg.bSorterRef +#endif + ){ + nResultCol--; + regOrig = 0; + } + } + + testcase( regOrig ); + testcase( eDest==SRT_Set ); + testcase( eDest==SRT_Mem ); + testcase( eDest==SRT_Coroutine ); + testcase( eDest==SRT_Output ); + assert( eDest==SRT_Set || eDest==SRT_Mem + || eDest==SRT_Coroutine || eDest==SRT_Output + || eDest==SRT_Upfrom ); + } + sRowLoadInfo.regResult = regResult; + sRowLoadInfo.ecelFlags = ecelFlags; +#ifdef SQLITE_ENABLE_SORTER_REFERENCES + sRowLoadInfo.pExtra = pExtra; + sRowLoadInfo.regExtraResult = regResult + nResultCol; + if( pExtra ) nResultCol += pExtra->nExpr; +#endif + if( p->iLimit + && (ecelFlags & SQLITE_ECEL_OMITREF)!=0 + && nPrefixReg>0 + ){ + assert( pSort!=0 ); + assert( hasDistinct==0 ); + pSort->pDeferredRowLoad = &sRowLoadInfo; + regOrig = 0; + }else{ + innerLoopLoadRow(pParse, p, &sRowLoadInfo); + } } /* If the DISTINCT keyword was present on the SELECT statement @@ -744,58 +1289,11 @@ static void selectInnerLoop( ** part of the result. */ if( hasDistinct ){ - switch( pDistinct->eTnctType ){ - case WHERE_DISTINCT_ORDERED: { - VdbeOp *pOp; /* No longer required OpenEphemeral instr. */ - int iJump; /* Jump destination */ - int regPrev; /* Previous row content */ - - /* Allocate space for the previous row */ - regPrev = pParse->nMem+1; - pParse->nMem += nResultCol; - - /* Change the OP_OpenEphemeral coded earlier to an OP_Null - ** sets the MEM_Cleared bit on the first register of the - ** previous value. This will cause the OP_Ne below to always - ** fail on the first iteration of the loop even if the first - ** row is all NULLs. - */ - sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct); - pOp = sqlite3VdbeGetOp(v, pDistinct->addrTnct); - pOp->opcode = OP_Null; - pOp->p1 = 1; - pOp->p2 = regPrev; - - iJump = sqlite3VdbeCurrentAddr(v) + nResultCol; - for(i=0; ia[i].pExpr); - if( idb->mallocFailed ); - sqlite3VdbeAddOp3(v, OP_Copy, regResult, regPrev, nResultCol-1); - break; - } - - case WHERE_DISTINCT_UNIQUE: { - sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct); - break; - } - - default: { - assert( pDistinct->eTnctType==WHERE_DISTINCT_UNORDERED ); - codeDistinct(pParse, pDistinct->tabTnct, iContinue, nResultCol, - regResult); - break; - } - } + int eType = pDistinct->eTnctType; + int iTab = pDistinct->tabTnct; + assert( nResultCol==p->pEList->nExpr ); + iTab = codeDistinct(pParse, eType, iTab, iContinue, p->pEList, regResult); + fixDistinctOpenEph(pParse, eType, iTab, pDistinct->addrTnct); if( pSort==0 ){ codeOffset(v, p->iOffset, iContinue); } @@ -810,7 +1308,7 @@ static void selectInnerLoop( int r1; r1 = sqlite3GetTempReg(pParse); sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1); - sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1); + sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, r1, regResult, nResultCol); sqlite3ReleaseTempReg(pParse, r1); break; } @@ -837,6 +1335,16 @@ static void selectInnerLoop( testcase( eDest==SRT_Fifo ); testcase( eDest==SRT_DistFifo ); sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1+nPrefixReg); +#if !defined(SQLITE_ENABLE_NULL_TRIM) && defined(SQLITE_DEBUG) + /* A destination of SRT_Table and a non-zero iSDParm2 parameter means + ** that this is an "UPDATE ... FROM" on a virtual table or view. In this + ** case set the p5 parameter of the OP_MakeRecord to OPFLAG_NOCHNG_MAGIC. + ** This does not affect operation in any way - it just allows MakeRecord + ** to process OPFLAG_NOCHANGE values without an assert() failing. */ + if( eDest==SRT_Table && pDest->iSDParm2 ){ + sqlite3VdbeChangeP5(v, OPFLAG_NOCHNG_MAGIC); + } +#endif #ifndef SQLITE_OMIT_CTE if( eDest==SRT_DistFifo ){ /* If the destination is DistFifo, then cursor (iParm+1) is open @@ -847,12 +1355,13 @@ static void selectInnerLoop( int addr = sqlite3VdbeCurrentAddr(v) + 4; sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, addr, r1, 0); VdbeCoverage(v); - sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm+1, r1); + sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm+1, r1,regResult,nResultCol); assert( pSort==0 ); } #endif if( pSort ){ - pushOntoSorter(pParse, pSort, p, r1+nPrefixReg,regResult,1,nPrefixReg); + assert( regResult==regOrig ); + pushOntoSorter(pParse, pSort, p, r1+nPrefixReg, regOrig, 1, nPrefixReg); }else{ int r2 = sqlite3GetTempReg(pParse); sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, r2); @@ -864,31 +1373,61 @@ static void selectInnerLoop( break; } + case SRT_Upfrom: { + if( pSort ){ + pushOntoSorter( + pParse, pSort, p, regResult, regOrig, nResultCol, nPrefixReg); + }else{ + int i2 = pDest->iSDParm2; + int r1 = sqlite3GetTempReg(pParse); + + /* If the UPDATE FROM join is an aggregate that matches no rows, it + ** might still be trying to return one row, because that is what + ** aggregates do. Don't record that empty row in the output table. */ + sqlite3VdbeAddOp2(v, OP_IsNull, regResult, iBreak); VdbeCoverage(v); + + sqlite3VdbeAddOp3(v, OP_MakeRecord, + regResult+(i2<0), nResultCol-(i2<0), r1); + if( i2<0 ){ + sqlite3VdbeAddOp3(v, OP_Insert, iParm, r1, regResult); + }else{ + sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, r1, regResult, i2); + } + } + break; + } + #ifndef SQLITE_OMIT_SUBQUERY /* If we are creating a set for an "expr IN (SELECT ...)" construct, ** then there should be a single item on the stack. Write this ** item into the set table with bogus data. */ case SRT_Set: { - assert( nResultCol==1 ); - pDest->affSdst = - sqlite3CompareAffinity(pEList->a[0].pExpr, pDest->affSdst); if( pSort ){ /* At first glance you would think we could optimize out the ** ORDER BY in this case since the order of entries in the set ** does not matter. But there might be a LIMIT clause, in which ** case the order does matter */ - pushOntoSorter(pParse, pSort, p, regResult, regResult, 1, nPrefixReg); + pushOntoSorter( + pParse, pSort, p, regResult, regOrig, nResultCol, nPrefixReg); + pDest->iSDParm2 = 0; /* Signal that any Bloom filter is unpopulated */ }else{ int r1 = sqlite3GetTempReg(pParse); - sqlite3VdbeAddOp4(v, OP_MakeRecord, regResult,1,r1, &pDest->affSdst, 1); - sqlite3ExprCacheAffinityChange(pParse, regResult, 1); - sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1); + assert( sqlite3Strlen30(pDest->zAffSdst)==nResultCol ); + sqlite3VdbeAddOp4(v, OP_MakeRecord, regResult, nResultCol, + r1, pDest->zAffSdst, nResultCol); + sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, r1, regResult, nResultCol); + if( pDest->iSDParm2 ){ + sqlite3VdbeAddOp4Int(v, OP_FilterAdd, pDest->iSDParm2, 0, + regResult, nResultCol); + ExplainQueryPlan((pParse, 0, "CREATE BLOOM FILTER")); + } sqlite3ReleaseTempReg(pParse, r1); } break; } + /* If any row exist in the result set, record that fact and abort. */ case SRT_Exists: { @@ -898,15 +1437,22 @@ static void selectInnerLoop( } /* If this is a scalar select that is part of an expression, then - ** store the results in the appropriate memory cell and break out - ** of the scan loop. + ** store the results in the appropriate memory cell or array of + ** memory cells and break out of the scan loop. */ case SRT_Mem: { - assert( nResultCol==1 ); if( pSort ){ - pushOntoSorter(pParse, pSort, p, regResult, regResult, 1, nPrefixReg); + assert( nResultCol<=pDest->nSdst ); + pushOntoSorter( + pParse, pSort, p, regResult, regOrig, nResultCol, nPrefixReg); + pDest->iSDParm = regResult; }else{ - assert( regResult==iParm ); + assert( nResultCol==pDest->nSdst ); + if( regResult!=iParm ){ + /* This occurs in cases where the SELECT had both a DISTINCT and + ** an OFFSET clause. */ + sqlite3VdbeAddOp3(v, OP_Copy, regResult, iParm, nResultCol-1); + } /* The LIMIT clause will jump out of the loop for us */ } break; @@ -918,13 +1464,12 @@ static void selectInnerLoop( testcase( eDest==SRT_Coroutine ); testcase( eDest==SRT_Output ); if( pSort ){ - pushOntoSorter(pParse, pSort, p, regResult, regResult, nResultCol, + pushOntoSorter(pParse, pSort, p, regResult, regOrig, nResultCol, nPrefixReg); }else if( eDest==SRT_Coroutine ){ sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm); }else{ sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, nResultCol); - sqlite3ExprCacheAffinityChange(pParse, regResult, nResultCol); } break; } @@ -952,7 +1497,7 @@ static void selectInnerLoop( /* If the destination is DistQueue, then cursor (iParm+1) is open ** on a second ephemeral index that holds all values every previously ** added to the queue. */ - addrTest = sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, 0, + addrTest = sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, 0, regResult, nResultCol); VdbeCoverage(v); } @@ -968,7 +1513,7 @@ static void selectInnerLoop( } sqlite3VdbeAddOp2(v, OP_Sequence, iParm, r2+nKey); sqlite3VdbeAddOp3(v, OP_MakeRecord, r2, nKey+2, r1); - sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1); + sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, r1, r2, nKey+2); if( addrTest ) sqlite3VdbeJumpHere(v, addrTest); sqlite3ReleaseTempReg(pParse, r1); sqlite3ReleaseTempRange(pParse, r2, nKey+2); @@ -1006,17 +1551,20 @@ static void selectInnerLoop( */ KeyInfo *sqlite3KeyInfoAlloc(sqlite3 *db, int N, int X){ int nExtra = (N+X)*(sizeof(CollSeq*)+1); - KeyInfo *p = sqlite3Malloc(sizeof(KeyInfo) + nExtra); + KeyInfo *p; + assert( X>=0 ); + if( NEVER(N+X>0xffff) ) return (KeyInfo*)sqlite3OomFault(db); + p = sqlite3DbMallocRawNN(db, SZ_KEYINFO(0) + nExtra); if( p ){ - p->aSortOrder = (u8*)&p->aColl[N+X]; - p->nField = (u16)N; - p->nXField = (u16)X; + p->aSortFlags = (u8*)&p->aColl[N+X]; + p->nKeyField = (u16)N; + p->nAllField = (u16)(N+X); p->enc = ENC(db); p->db = db; p->nRef = 1; - memset(&p[1], 0, nExtra); + memset(p->aColl, 0, nExtra); }else{ - sqlite3OomFault(db); + return (KeyInfo*)sqlite3OomFault(db); } return p; } @@ -1026,9 +1574,10 @@ KeyInfo *sqlite3KeyInfoAlloc(sqlite3 *db, int N, int X){ */ void sqlite3KeyInfoUnref(KeyInfo *p){ if( p ){ + assert( p->db!=0 ); assert( p->nRef>0 ); p->nRef--; - if( p->nRef==0 ) sqlite3DbFree(0, p); + if( p->nRef==0 ) sqlite3DbNNFreeNN(p->db, p); } } @@ -1067,7 +1616,7 @@ int sqlite3KeyInfoIsWriteable(KeyInfo *p){ return p->nRef==1; } ** function is responsible for seeing that this structure is eventually ** freed. */ -static KeyInfo *keyInfoFromExprList( +KeyInfo *sqlite3KeyInfoFromExprList( Parse *pParse, /* Parsing context */ ExprList *pList, /* Form the KeyInfo object from this ExprList */ int iStart, /* Begin with this column of pList */ @@ -1084,11 +1633,8 @@ static KeyInfo *keyInfoFromExprList( if( pInfo ){ assert( sqlite3KeyInfoIsWriteable(pInfo) ); for(i=iStart, pItem=pList->a+iStart; ipExpr); - if( !pColl ) pColl = db->pDfltColl; - pInfo->aColl[i-iStart] = pColl; - pInfo->aSortOrder[i-iStart] = pItem->sortOrder; + pInfo->aColl[i-iStart] = sqlite3ExprNNCollSeq(pParse, pItem->pExpr); + pInfo->aSortFlags[i-iStart] = pItem->fg.sortFlags; } } return pInfo; @@ -1097,7 +1643,7 @@ static KeyInfo *keyInfoFromExprList( /* ** Name of the connection operator, used for error messages. */ -static const char *selectOpName(int id){ +const char *sqlite3SelectOpName(int id){ char *z; switch( id ){ case TK_ALL: z = "UNION ALL"; break; @@ -1120,11 +1666,7 @@ static const char *selectOpName(int id){ ** is determined by the zUsage argument. */ static void explainTempTable(Parse *pParse, const char *zUsage){ - if( pParse->explain==2 ){ - Vdbe *v = pParse->pVdbe; - char *zMsg = sqlite3MPrintf(pParse->db, "USE TEMP B-TREE FOR %s", zUsage); - sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC); - } + ExplainQueryPlan((pParse, 0, "USE TEMP B-TREE FOR %s", zUsage)); } /* @@ -1142,42 +1684,6 @@ static void explainTempTable(Parse *pParse, const char *zUsage){ # define explainSetInteger(y,z) #endif -#if !defined(SQLITE_OMIT_EXPLAIN) && !defined(SQLITE_OMIT_COMPOUND_SELECT) -/* -** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function -** is a no-op. Otherwise, it adds a single row of output to the EQP result, -** where the caption is of one of the two forms: -** -** "COMPOSITE SUBQUERIES iSub1 and iSub2 (op)" -** "COMPOSITE SUBQUERIES iSub1 and iSub2 USING TEMP B-TREE (op)" -** -** where iSub1 and iSub2 are the integers passed as the corresponding -** function parameters, and op is the text representation of the parameter -** of the same name. The parameter "op" must be one of TK_UNION, TK_EXCEPT, -** TK_INTERSECT or TK_ALL. The first form is used if argument bUseTmp is -** false, or the second form if it is true. -*/ -static void explainComposite( - Parse *pParse, /* Parse context */ - int op, /* One of TK_UNION, TK_EXCEPT etc. */ - int iSub1, /* Subquery id 1 */ - int iSub2, /* Subquery id 2 */ - int bUseTmp /* True if a temp table was used */ -){ - assert( op==TK_UNION || op==TK_EXCEPT || op==TK_INTERSECT || op==TK_ALL ); - if( pParse->explain==2 ){ - Vdbe *v = pParse->pVdbe; - char *zMsg = sqlite3MPrintf( - pParse->db, "COMPOUND SUBQUERIES %d AND %d %s(%s)", iSub1, iSub2, - bUseTmp?"USING TEMP B-TREE ":"", selectOpName(op) - ); - sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC); - } -} -#else -/* No-op versions of the explainXXX() functions and macros. */ -# define explainComposite(v,w,x,y,z) -#endif /* ** If the inner loop was generated using a non-null pOrderBy argument, @@ -1194,8 +1700,8 @@ static void generateSortTail( ){ Vdbe *v = pParse->pVdbe; /* The prepared statement */ int addrBreak = pSort->labelDone; /* Jump here to exit loop */ - int addrContinue = sqlite3VdbeMakeLabel(v); /* Jump here for next cycle */ - int addr; + int addrContinue = sqlite3VdbeMakeLabel(pParse);/* Jump here for next cycle */ + int addr; /* Top of output loop. Jump for Next. */ int addrOnce = 0; int iTab; ExprList *pOrderBy = pSort->pOrderBy; @@ -1203,43 +1709,76 @@ static void generateSortTail( int iParm = pDest->iSDParm; int regRow; int regRowid; - int nKey; + int iCol; + int nKey; /* Number of key columns in sorter record */ int iSortTab; /* Sorter cursor to read from */ - int nSortData; /* Trailing values to read from sorter */ int i; int bSeq; /* True if sorter record includes seq. no. */ -#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS + int nRefKey = 0; struct ExprList_item *aOutEx = p->pEList->a; +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS + int addrExplain; /* Address of OP_Explain instruction */ #endif + nKey = pOrderBy->nExpr - pSort->nOBSat; + if( pSort->nOBSat==0 || nKey==1 ){ + ExplainQueryPlan2(addrExplain, (pParse, 0, + "USE TEMP B-TREE FOR %sORDER BY", pSort->nOBSat?"LAST TERM OF ":"" + )); + }else{ + ExplainQueryPlan2(addrExplain, (pParse, 0, + "USE TEMP B-TREE FOR LAST %d TERMS OF ORDER BY", nKey + )); + } + sqlite3VdbeScanStatusRange(v, addrExplain,pSort->addrPush,pSort->addrPushEnd); + sqlite3VdbeScanStatusCounters(v, addrExplain, addrExplain, pSort->addrPush); + + assert( addrBreak<0 ); if( pSort->labelBkOut ){ sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut); sqlite3VdbeGoto(v, addrBreak); sqlite3VdbeResolveLabel(v, pSort->labelBkOut); } + +#ifdef SQLITE_ENABLE_SORTER_REFERENCES + /* Open any cursors needed for sorter-reference expressions */ + for(i=0; inDefer; i++){ + Table *pTab = pSort->aDefer[i].pTab; + int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); + sqlite3OpenTable(pParse, pSort->aDefer[i].iCsr, iDb, pTab, OP_OpenRead); + nRefKey = MAX(nRefKey, pSort->aDefer[i].nKey); + } +#endif + iTab = pSort->iECursor; - if( eDest==SRT_Output || eDest==SRT_Coroutine ){ + if( eDest==SRT_Output || eDest==SRT_Coroutine || eDest==SRT_Mem ){ + if( eDest==SRT_Mem && p->iOffset ){ + sqlite3VdbeAddOp2(v, OP_Null, 0, pDest->iSdst); + } regRowid = 0; regRow = pDest->iSdst; - nSortData = nColumn; }else{ regRowid = sqlite3GetTempReg(pParse); - regRow = sqlite3GetTempReg(pParse); - nSortData = 1; + if( eDest==SRT_EphemTab || eDest==SRT_Table ){ + regRow = sqlite3GetTempReg(pParse); + nColumn = 0; + }else{ + regRow = sqlite3GetTempRange(pParse, nColumn); + } } - nKey = pOrderBy->nExpr - pSort->nOBSat; if( pSort->sortFlags & SORTFLAG_UseSorter ){ int regSortOut = ++pParse->nMem; iSortTab = pParse->nTab++; if( pSort->labelBkOut ){ - addrOnce = sqlite3CodeOnce(pParse); VdbeCoverage(v); + addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v); } - sqlite3VdbeAddOp3(v, OP_OpenPseudo, iSortTab, regSortOut, nKey+1+nSortData); + sqlite3VdbeAddOp3(v, OP_OpenPseudo, iSortTab, regSortOut, + nKey+1+nColumn+nRefKey); if( addrOnce ) sqlite3VdbeJumpHere(v, addrOnce); addr = 1 + sqlite3VdbeAddOp2(v, OP_SorterSort, iTab, addrBreak); VdbeCoverage(v); - codeOffset(v, p->iOffset, addrContinue); + assert( p->iLimit==0 && p->iOffset==0 ); sqlite3VdbeAddOp3(v, OP_SorterData, iTab, regSortOut, iSortTab); bSeq = 0; }else{ @@ -1247,13 +1786,69 @@ static void generateSortTail( codeOffset(v, p->iOffset, addrContinue); iSortTab = iTab; bSeq = 1; + if( p->iOffset>0 ){ + sqlite3VdbeAddOp2(v, OP_AddImm, p->iLimit, -1); + } + } + for(i=0, iCol=nKey+bSeq-1; inDefer ){ + int iKey = iCol+1; + int regKey = sqlite3GetTempRange(pParse, nRefKey); + + for(i=0; inDefer; i++){ + int iCsr = pSort->aDefer[i].iCsr; + Table *pTab = pSort->aDefer[i].pTab; + int nKey = pSort->aDefer[i].nKey; + + sqlite3VdbeAddOp1(v, OP_NullRow, iCsr); + if( HasRowid(pTab) ){ + sqlite3VdbeAddOp3(v, OP_Column, iSortTab, iKey++, regKey); + sqlite3VdbeAddOp3(v, OP_SeekRowid, iCsr, + sqlite3VdbeCurrentAddr(v)+1, regKey); + }else{ + int k; + int iJmp; + assert( sqlite3PrimaryKeyIndex(pTab)->nKeyCol==nKey ); + for(k=0; k=0; i--){ +#ifdef SQLITE_ENABLE_SORTER_REFERENCES + if( aOutEx[i].fg.bSorterRef ){ + sqlite3ExprCode(pParse, aOutEx[i].pExpr, regRow+i); + }else +#endif + { + int iRead; + if( aOutEx[i].u.x.iOrderByCol ){ + iRead = aOutEx[i].u.x.iOrderByCol-1; + }else{ + iRead = iCol--; + } + sqlite3VdbeAddOp3(v, OP_Column, iSortTab, iRead, regRow+i); + VdbeComment((v, "%s", aOutEx[i].zEName)); + } } + sqlite3VdbeScanStatusRange(v, addrExplain, addrExplain, -1); switch( eDest ){ + case SRT_Table: case SRT_EphemTab: { + sqlite3VdbeAddOp3(v, OP_Column, iSortTab, nKey+bSeq, regRow); sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, regRowid); sqlite3VdbeAddOp3(v, OP_Insert, iParm, regRow, regRowid); sqlite3VdbeChangeP5(v, OPFLAG_APPEND); @@ -1261,27 +1856,34 @@ static void generateSortTail( } #ifndef SQLITE_OMIT_SUBQUERY case SRT_Set: { - assert( nColumn==1 ); - sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, 1, regRowid, - &pDest->affSdst, 1); - sqlite3ExprCacheAffinityChange(pParse, regRow, 1); - sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, regRowid); + assert( nColumn==sqlite3Strlen30(pDest->zAffSdst) ); + sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, nColumn, regRowid, + pDest->zAffSdst, nColumn); + sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, regRowid, regRow, nColumn); break; } case SRT_Mem: { - assert( nColumn==1 ); - sqlite3ExprCodeMove(pParse, regRow, iParm, 1); /* The LIMIT clause will terminate the loop for us */ break; } #endif + case SRT_Upfrom: { + int i2 = pDest->iSDParm2; + int r1 = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp3(v, OP_MakeRecord,regRow+(i2<0),nColumn-(i2<0),r1); + if( i2<0 ){ + sqlite3VdbeAddOp3(v, OP_Insert, iParm, r1, regRow); + }else{ + sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, r1, regRow, i2); + } + break; + } default: { - assert( eDest==SRT_Output || eDest==SRT_Coroutine ); + assert( eDest==SRT_Output || eDest==SRT_Coroutine ); testcase( eDest==SRT_Output ); testcase( eDest==SRT_Coroutine ); if( eDest==SRT_Output ){ sqlite3VdbeAddOp2(v, OP_ResultRow, pDest->iSdst, nColumn); - sqlite3ExprCacheAffinityChange(pParse, pDest->iSdst, nColumn); }else{ sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm); } @@ -1289,7 +1891,11 @@ static void generateSortTail( } } if( regRowid ){ - sqlite3ReleaseTempReg(pParse, regRow); + if( eDest==SRT_Set ){ + sqlite3ReleaseTempRange(pParse, regRow, nColumn); + }else{ + sqlite3ReleaseTempReg(pParse, regRow); + } sqlite3ReleaseTempReg(pParse, regRowid); } /* The bottom of the loop @@ -1300,6 +1906,7 @@ static void generateSortTail( }else{ sqlite3VdbeAddOp2(v, OP_Next, iTab, addr); VdbeCoverage(v); } + sqlite3VdbeScanStatusRange(v, addrExplain, sqlite3VdbeCurrentAddr(v)-1, -1); if( pSort->regReturn ) sqlite3VdbeAddOp1(v, OP_Return, pSort->regReturn); sqlite3VdbeResolveLabel(v, addrBreak); } @@ -1308,44 +1915,41 @@ static void generateSortTail( ** Return a pointer to a string containing the 'declaration type' of the ** expression pExpr. The string may be treated as static by the caller. ** -** Also try to estimate the size of the returned value and return that -** result in *pEstWidth. -** ** The declaration type is the exact datatype definition extracted from the ** original CREATE TABLE statement if the expression is a column. The ** declaration type for a ROWID field is INTEGER. Exactly when an expression ** is considered a column can be complex in the presence of subqueries. The -** result-set expression in all of the following SELECT statements is +** result-set expression in all of the following SELECT statements is ** considered a column by this function. ** ** SELECT col FROM tbl; ** SELECT (SELECT col FROM tbl; ** SELECT (SELECT col FROM tbl); ** SELECT abc FROM (SELECT col AS abc FROM tbl); -** +** ** The declaration type for any expression other than a column is NULL. ** ** This routine has either 3 or 6 parameters depending on whether or not ** the SQLITE_ENABLE_COLUMN_METADATA compile-time option is used. */ #ifdef SQLITE_ENABLE_COLUMN_METADATA -# define columnType(A,B,C,D,E,F) columnTypeImpl(A,B,C,D,E,F) +# define columnType(A,B,C,D,E) columnTypeImpl(A,B,C,D,E) #else /* if !defined(SQLITE_ENABLE_COLUMN_METADATA) */ -# define columnType(A,B,C,D,E,F) columnTypeImpl(A,B,F) +# define columnType(A,B,C,D,E) columnTypeImpl(A,B) #endif static const char *columnTypeImpl( - NameContext *pNC, + NameContext *pNC, +#ifndef SQLITE_ENABLE_COLUMN_METADATA + Expr *pExpr +#else Expr *pExpr, -#ifdef SQLITE_ENABLE_COLUMN_METADATA const char **pzOrigDb, const char **pzOrigTab, - const char **pzOrigCol, + const char **pzOrigCol #endif - u8 *pEstWidth ){ char const *zType = 0; int j; - u8 estWidth = 1; #ifdef SQLITE_ENABLE_COLUMN_METADATA char const *zOrigDb = 0; char const *zOrigTab = 0; @@ -1355,7 +1959,6 @@ static const char *columnTypeImpl( assert( pExpr!=0 ); assert( pNC->pSrcList!=0 ); switch( pExpr->op ){ - case TK_AGG_COLUMN: case TK_COLUMN: { /* The expression is a column. Locate the table the column is being ** extracted from in NameContext.pSrcList. This table may be real @@ -1364,14 +1967,16 @@ static const char *columnTypeImpl( Table *pTab = 0; /* Table structure column is extracted from */ Select *pS = 0; /* Select the column is extracted from */ int iCol = pExpr->iColumn; /* Index of column in pTab */ - testcase( pExpr->op==TK_AGG_COLUMN ); - testcase( pExpr->op==TK_COLUMN ); while( pNC && !pTab ){ SrcList *pTabList = pNC->pSrcList; for(j=0;jnSrc && pTabList->a[j].iCursor!=pExpr->iTable;j++); if( jnSrc ){ - pTab = pTabList->a[j].pTab; - pS = pTabList->a[j].pSelect; + pTab = pTabList->a[j].pSTab; + if( pTabList->a[j].fg.isSubquery ){ + pS = pTabList->a[j].u4.pSubq->pSelect; + }else{ + pS = 0; + } }else{ pNC = pNC->pNext; } @@ -1380,70 +1985,68 @@ static const char *columnTypeImpl( if( pTab==0 ){ /* At one time, code such as "SELECT new.x" within a trigger would ** cause this condition to run. Since then, we have restructured how - ** trigger code is generated and so this condition is no longer + ** trigger code is generated and so this condition is no longer ** possible. However, it can still be true for statements like ** the following: ** ** CREATE TABLE t1(col INTEGER); ** SELECT (SELECT t1.col) FROM FROM t1; ** - ** when columnType() is called on the expression "t1.col" in the + ** when columnType() is called on the expression "t1.col" in the ** sub-select. In this case, set the column type to NULL, even ** though it should really be "INTEGER". ** ** This is not a problem, as the column type of "t1.col" is never - ** used. When columnType() is called on the expression + ** used. When columnType() is called on the expression ** "(SELECT t1.col)", the correct type is returned (see the TK_SELECT ** branch below. */ break; } - assert( pTab && pExpr->pTab==pTab ); + assert( pTab && ExprUseYTab(pExpr) && pExpr->y.pTab==pTab ); if( pS ){ /* The "table" is actually a sub-select or a view in the FROM clause ** of the SELECT statement. Return the declaration type and origin ** data for the result-set column of the sub-select. */ - if( iCol>=0 && ALWAYS(iColpEList->nExpr) ){ + if( iColpEList->nExpr + && (!ViewCanHaveRowid || iCol>=0) + ){ /* If iCol is less than zero, then the expression requests the - ** rowid of the sub-select or view. This expression is legal (see + ** rowid of the sub-select or view. This expression is legal (see ** test case misc2.2.2) - it always evaluates to NULL. - ** - ** The ALWAYS() is because iCol>=pS->pEList->nExpr will have been - ** caught already by name resolution. */ NameContext sNC; Expr *p = pS->pEList->a[iCol].pExpr; sNC.pSrcList = pS->pSrc; sNC.pNext = pNC; sNC.pParse = pNC->pParse; - zType = columnType(&sNC, p,&zOrigDb,&zOrigTab,&zOrigCol, &estWidth); + zType = columnType(&sNC, p,&zOrigDb,&zOrigTab,&zOrigCol); } - }else if( pTab->pSchema ){ - /* A real table */ + }else{ + /* A real table or a CTE table */ assert( !pS ); - if( iCol<0 ) iCol = pTab->iPKey; - assert( iCol==-1 || (iCol>=0 && iColnCol) ); #ifdef SQLITE_ENABLE_COLUMN_METADATA + if( iCol<0 ) iCol = pTab->iPKey; + assert( iCol==XN_ROWID || (iCol>=0 && iColnCol) ); if( iCol<0 ){ zType = "INTEGER"; zOrigCol = "rowid"; }else{ - zType = pTab->aCol[iCol].zType; - zOrigCol = pTab->aCol[iCol].zName; - estWidth = pTab->aCol[iCol].szEst; + zOrigCol = pTab->aCol[iCol].zCnName; + zType = sqlite3ColumnType(&pTab->aCol[iCol],0); } zOrigTab = pTab->zName; - if( pNC->pParse ){ + if( pNC->pParse && pTab->pSchema ){ int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema); - zOrigDb = pNC->pParse->db->aDb[iDb].zName; + zOrigDb = pNC->pParse->db->aDb[iDb].zDbSName; } #else + assert( iCol==XN_ROWID || (iCol>=0 && iColnCol) ); if( iCol<0 ){ zType = "INTEGER"; }else{ - zType = pTab->aCol[iCol].zType; - estWidth = pTab->aCol[iCol].szEst; + zType = sqlite3ColumnType(&pTab->aCol[iCol],0); } #endif } @@ -1456,19 +2059,21 @@ static const char *columnTypeImpl( ** statement. */ NameContext sNC; - Select *pS = pExpr->x.pSelect; - Expr *p = pS->pEList->a[0].pExpr; - assert( ExprHasProperty(pExpr, EP_xIsSelect) ); + Select *pS; + Expr *p; + assert( ExprUseXSelect(pExpr) ); + pS = pExpr->x.pSelect; + p = pS->pEList->a[0].pExpr; sNC.pSrcList = pS->pSrc; sNC.pNext = pNC; sNC.pParse = pNC->pParse; - zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol, &estWidth); + zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol); break; } #endif } -#ifdef SQLITE_ENABLE_COLUMN_METADATA +#ifdef SQLITE_ENABLE_COLUMN_METADATA if( pzOrigDb ){ assert( pzOrigTab && pzOrigCol ); *pzOrigDb = zOrigDb; @@ -1476,7 +2081,6 @@ static const char *columnTypeImpl( *pzOrigCol = zOrigCol; } #endif - if( pEstWidth ) *pEstWidth = estWidth; return zType; } @@ -1495,6 +2099,7 @@ static void generateColumnTypes( NameContext sNC; sNC.pSrcList = pTabList; sNC.pParse = pParse; + sNC.pNext = 0; for(i=0; inExpr; i++){ Expr *p = pEList->a[i].pExpr; const char *zType; @@ -1502,9 +2107,9 @@ static void generateColumnTypes( const char *zOrigDb = 0; const char *zOrigTab = 0; const char *zOrigCol = 0; - zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol, 0); + zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol); - /* The vdbe must make its own copy of the column-type and other + /* The vdbe must make its own copy of the column-type and other ** column specific strings, in case the schema is reset before this ** virtual machine is deleted. */ @@ -1512,69 +2117,97 @@ static void generateColumnTypes( sqlite3VdbeSetColName(v, i, COLNAME_TABLE, zOrigTab, SQLITE_TRANSIENT); sqlite3VdbeSetColName(v, i, COLNAME_COLUMN, zOrigCol, SQLITE_TRANSIENT); #else - zType = columnType(&sNC, p, 0, 0, 0, 0); + zType = columnType(&sNC, p, 0, 0, 0); #endif sqlite3VdbeSetColName(v, i, COLNAME_DECLTYPE, zType, SQLITE_TRANSIENT); } +#else + UNUSED_PARAMETER(pParse); + UNUSED_PARAMETER(pTabList); + UNUSED_PARAMETER(pEList); #endif /* !defined(SQLITE_OMIT_DECLTYPE) */ } + /* -** Generate code that will tell the VDBE the names of columns -** in the result set. This information is used to provide the -** azCol[] values in the callback. +** Compute the column names for a SELECT statement. +** +** The only guarantee that SQLite makes about column names is that if the +** column has an AS clause assigning it a name, that will be the name used. +** That is the only documented guarantee. However, countless applications +** developed over the years have made baseless assumptions about column names +** and will break if those assumptions changes. Hence, use extreme caution +** when modifying this routine to avoid breaking legacy. +** +** See Also: sqlite3ColumnsFromExprList() +** +** The PRAGMA short_column_names and PRAGMA full_column_names settings are +** deprecated. The default setting is short=ON, full=OFF. 99.9% of all +** applications should operate this way. Nevertheless, we need to support the +** other modes for legacy: +** +** short=OFF, full=OFF: Column name is the text of the expression has it +** originally appears in the SELECT statement. In +** other words, the zSpan of the result expression. +** +** short=ON, full=OFF: (This is the default setting). If the result +** refers directly to a table column, then the +** result column name is just the table column +** name: COLUMN. Otherwise use zSpan. +** +** full=ON, short=ANY: If the result refers directly to a table column, +** then the result column name with the table name +** prefix, ex: TABLE.COLUMN. Otherwise use zSpan. */ -static void generateColumnNames( +void sqlite3GenerateColumnNames( Parse *pParse, /* Parser context */ - SrcList *pTabList, /* List of tables */ - ExprList *pEList /* Expressions defining the result set */ + Select *pSelect /* Generate column names for this SELECT statement */ ){ Vdbe *v = pParse->pVdbe; - int i, j; + int i; + Table *pTab; + SrcList *pTabList; + ExprList *pEList; sqlite3 *db = pParse->db; - int fullNames, shortNames; - -#ifndef SQLITE_OMIT_EXPLAIN - /* If this is an EXPLAIN, skip this step */ - if( pParse->explain ){ - return; - } -#endif + int fullName; /* TABLE.COLUMN if no AS clause and is a direct table ref */ + int srcName; /* COLUMN or TABLE.COLUMN if no AS clause and is direct */ - if( pParse->colNamesSet || db->mallocFailed ) return; + if( pParse->colNamesSet ) return; + /* Column names are determined by the left-most term of a compound select */ + while( pSelect->pPrior ) pSelect = pSelect->pPrior; + TREETRACE(0x80,pParse,pSelect,("generating column names\n")); + pTabList = pSelect->pSrc; + pEList = pSelect->pEList; assert( v!=0 ); assert( pTabList!=0 ); pParse->colNamesSet = 1; - fullNames = (db->flags & SQLITE_FullColNames)!=0; - shortNames = (db->flags & SQLITE_ShortColNames)!=0; + fullName = (db->flags & SQLITE_FullColNames)!=0; + srcName = (db->flags & SQLITE_ShortColNames)!=0 || fullName; sqlite3VdbeSetNumCols(v, pEList->nExpr); for(i=0; inExpr; i++){ - Expr *p; - p = pEList->a[i].pExpr; - if( NEVER(p==0) ) continue; - if( pEList->a[i].zName ){ - char *zName = pEList->a[i].zName; + Expr *p = pEList->a[i].pExpr; + + assert( p!=0 ); + assert( p->op!=TK_AGG_COLUMN ); /* Agg processing has not run yet */ + assert( p->op!=TK_COLUMN + || (ExprUseYTab(p) && p->y.pTab!=0) ); /* Covering idx not yet coded */ + if( pEList->a[i].zEName && pEList->a[i].fg.eEName==ENAME_NAME ){ + /* An AS clause always takes first priority */ + char *zName = pEList->a[i].zEName; sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_TRANSIENT); - }else if( p->op==TK_COLUMN || p->op==TK_AGG_COLUMN ){ - Table *pTab; + }else if( srcName && p->op==TK_COLUMN ){ char *zCol; int iCol = p->iColumn; - for(j=0; ALWAYS(jnSrc); j++){ - if( pTabList->a[j].iCursor==p->iTable ) break; - } - assert( jnSrc ); - pTab = pTabList->a[j].pTab; + pTab = p->y.pTab; + assert( pTab!=0 ); if( iCol<0 ) iCol = pTab->iPKey; assert( iCol==-1 || (iCol>=0 && iColnCol) ); if( iCol<0 ){ zCol = "rowid"; }else{ - zCol = pTab->aCol[iCol].zName; + zCol = pTab->aCol[iCol].zCnName; } - if( !shortNames && !fullNames ){ - sqlite3VdbeSetColName(v, i, COLNAME_NAME, - sqlite3DbStrDup(db, pEList->a[i].zSpan), SQLITE_DYNAMIC); - }else if( fullNames ){ + if( fullName ){ char *zName = 0; zName = sqlite3MPrintf(db, "%s.%s", pTab->zName, zCol); sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_DYNAMIC); @@ -1582,7 +2215,7 @@ static void generateColumnNames( sqlite3VdbeSetColName(v, i, COLNAME_NAME, zCol, SQLITE_TRANSIENT); } }else{ - const char *z = pEList->a[i].zSpan; + const char *z = pEList->a[i].zEName; z = z==0 ? sqlite3MPrintf(db, "column%d", i+1) : sqlite3DbStrDup(db, z); sqlite3VdbeSetColName(v, i, COLNAME_NAME, z, SQLITE_DYNAMIC); } @@ -1602,6 +2235,15 @@ static void generateColumnNames( ** ** Return SQLITE_OK on success. If a memory allocation error occurs, ** store NULL in *paCol and 0 in *pnCol and return SQLITE_NOMEM. +** +** The only guarantee that SQLite makes about column names is that if the +** column has an AS clause assigning it a name, that will be the name used. +** That is the only documented guarantee. However, countless applications +** developed over the years have made baseless assumptions about column names +** and will break if those assumptions changes. Hence, use extreme caution +** when modifying this routine to avoid breaking legacy. +** +** See Also: sqlite3GenerateColumnNames() */ int sqlite3ColumnsFromExprList( Parse *pParse, /* Parsing context */ @@ -1614,16 +2256,17 @@ int sqlite3ColumnsFromExprList( u32 cnt; /* Index added to make the name unique */ Column *aCol, *pCol; /* For looping over result columns */ int nCol; /* Number of columns in the result set */ - Expr *p; /* Expression for a single result column */ char *zName; /* Column name */ int nName; /* Size of name in zName[] */ Hash ht; /* Hash table of column names */ + Table *pTab; sqlite3HashInit(&ht); if( pEList ){ nCol = pEList->nExpr; aCol = sqlite3DbMallocZero(db, sizeof(aCol[0])*nCol); testcase( aCol==0 ); + if( NEVER(nCol>32767) ) nCol = 32767; }else{ nCol = 0; aCol = 0; @@ -1632,144 +2275,209 @@ int sqlite3ColumnsFromExprList( *pnCol = nCol; *paCol = aCol; - for(i=0, pCol=aCol; imallocFailed; i++, pCol++){ + for(i=0, pCol=aCol; inErr; i++, pCol++){ + struct ExprList_item *pX = &pEList->a[i]; + struct ExprList_item *pCollide; /* Get an appropriate name for the column */ - p = sqlite3ExprSkipCollate(pEList->a[i].pExpr); - if( (zName = pEList->a[i].zName)!=0 ){ + if( (zName = pX->zEName)!=0 && pX->fg.eEName==ENAME_NAME ){ /* If the column contains an "AS " phrase, use as the name */ }else{ - Expr *pColExpr = p; /* The expression that is the result column name */ - Table *pTab; /* Table associated with this expression */ - while( pColExpr->op==TK_DOT ){ + Expr *pColExpr = sqlite3ExprSkipCollateAndLikely(pX->pExpr); + while( ALWAYS(pColExpr!=0) && pColExpr->op==TK_DOT ){ pColExpr = pColExpr->pRight; assert( pColExpr!=0 ); } - if( pColExpr->op==TK_COLUMN && ALWAYS(pColExpr->pTab!=0) ){ + if( pColExpr->op==TK_COLUMN + && ALWAYS( ExprUseYTab(pColExpr) ) + && ALWAYS( pColExpr->y.pTab!=0 ) + ){ /* For columns use the column name name */ int iCol = pColExpr->iColumn; - pTab = pColExpr->pTab; + pTab = pColExpr->y.pTab; if( iCol<0 ) iCol = pTab->iPKey; - zName = iCol>=0 ? pTab->aCol[iCol].zName : "rowid"; + zName = iCol>=0 ? pTab->aCol[iCol].zCnName : "rowid"; }else if( pColExpr->op==TK_ID ){ assert( !ExprHasProperty(pColExpr, EP_IntValue) ); zName = pColExpr->u.zToken; }else{ /* Use the original text of the column expression as its name */ - zName = pEList->a[i].zSpan; + assert( zName==pX->zEName ); /* pointer comparison intended */ } } - zName = sqlite3MPrintf(db, "%s", zName); + if( zName && !sqlite3IsTrueOrFalse(zName) ){ + zName = sqlite3DbStrDup(db, zName); + }else{ + zName = sqlite3MPrintf(db,"column%d",i+1); + } /* Make sure the column name is unique. If the name is not unique, ** append an integer to the name so that it becomes unique. */ cnt = 0; - while( zName && sqlite3HashFind(&ht, zName)!=0 ){ + while( zName && (pCollide = sqlite3HashFind(&ht, zName))!=0 ){ + if( pCollide->fg.bUsingTerm ){ + pCol->colFlags |= COLFLAG_NOEXPAND; + } nName = sqlite3Strlen30(zName); if( nName>0 ){ for(j=nName-1; j>0 && sqlite3Isdigit(zName[j]); j--){} if( zName[j]==':' ) nName = j; } zName = sqlite3MPrintf(db, "%.*z:%u", nName, zName, ++cnt); - if( cnt>3 ) sqlite3_randomness(sizeof(cnt), &cnt); + sqlite3ProgressCheck(pParse); + if( cnt>3 ){ + sqlite3_randomness(sizeof(cnt), &cnt); + } + } + pCol->zCnName = zName; + pCol->hName = sqlite3StrIHash(zName); + if( pX->fg.bNoExpand ){ + pCol->colFlags |= COLFLAG_NOEXPAND; } - pCol->zName = zName; sqlite3ColumnPropertiesFromName(0, pCol); - if( zName && sqlite3HashInsert(&ht, zName, pCol)==pCol ){ + if( zName && sqlite3HashInsert(&ht, zName, pX)==pX ){ sqlite3OomFault(db); } } sqlite3HashClear(&ht); - if( db->mallocFailed ){ + if( pParse->nErr ){ for(j=0; jrc; } return SQLITE_OK; } /* -** Add type and collation information to a column list based on -** a SELECT statement. -** -** The column list presumably came from selectColumnNamesFromExprList(). -** The column list has only names, not types or collations. This -** routine goes through and adds the types and collations. -** -** This routine requires that all identifiers in the SELECT -** statement be resolved. +** pTab is a transient Table object that represents a subquery of some +** kind (maybe a parenthesized subquery in the FROM clause of a larger +** query, or a VIEW, or a CTE). This routine computes type information +** for that Table object based on the Select object that implements the +** subquery. For the purposes of this routine, "type information" means: +** +** * The datatype name, as it might appear in a CREATE TABLE statement +** * Which collating sequence to use for the column +** * The affinity of the column */ -static void selectAddColumnTypeAndCollation( - Parse *pParse, /* Parsing contexts */ - Table *pTab, /* Add column type information to this table */ - Select *pSelect /* SELECT used to determine types and collations */ +void sqlite3SubqueryColumnTypes( + Parse *pParse, /* Parsing contexts */ + Table *pTab, /* Add column type information to this table */ + Select *pSelect, /* SELECT used to determine types and collations */ + char aff /* Default affinity. */ ){ sqlite3 *db = pParse->db; - NameContext sNC; Column *pCol; CollSeq *pColl; - int i; + int i,j; Expr *p; struct ExprList_item *a; - u64 szAll = 0; + NameContext sNC; assert( pSelect!=0 ); assert( (pSelect->selFlags & SF_Resolved)!=0 ); - assert( pTab->nCol==pSelect->pEList->nExpr || db->mallocFailed ); - if( db->mallocFailed ) return; + assert( pTab->nCol==pSelect->pEList->nExpr || pParse->nErr>0 ); + assert( aff==SQLITE_AFF_NONE || aff==SQLITE_AFF_BLOB ); + if( db->mallocFailed || IN_RENAME_OBJECT ) return; + while( pSelect->pPrior ) pSelect = pSelect->pPrior; + a = pSelect->pEList->a; memset(&sNC, 0, sizeof(sNC)); sNC.pSrcList = pSelect->pSrc; - a = pSelect->pEList->a; for(i=0, pCol=pTab->aCol; inCol; i++, pCol++){ + const char *zType; + i64 n; + int m = 0; + Select *pS2 = pSelect; + pTab->tabFlags |= (pCol->colFlags & COLFLAG_NOINSERT); p = a[i].pExpr; - if( pCol->zType==0 ){ - pCol->zType = sqlite3DbStrDup(db, - columnType(&sNC, p,0,0,0, &pCol->szEst)); - } - szAll += pCol->szEst; + /* pCol->szEst = ... // Column size est for SELECT tables never used */ pCol->affinity = sqlite3ExprAffinity(p); - if( pCol->affinity==0 ) pCol->affinity = SQLITE_AFF_BLOB; + while( pCol->affinity<=SQLITE_AFF_NONE && pS2->pNext!=0 ){ + m |= sqlite3ExprDataType(pS2->pEList->a[i].pExpr); + pS2 = pS2->pNext; + pCol->affinity = sqlite3ExprAffinity(pS2->pEList->a[i].pExpr); + } + if( pCol->affinity<=SQLITE_AFF_NONE ){ + pCol->affinity = aff; + } + if( pCol->affinity>=SQLITE_AFF_TEXT && (pS2->pNext || pS2!=pSelect) ){ + for(pS2=pS2->pNext; pS2; pS2=pS2->pNext){ + m |= sqlite3ExprDataType(pS2->pEList->a[i].pExpr); + } + if( pCol->affinity==SQLITE_AFF_TEXT && (m&0x01)!=0 ){ + pCol->affinity = SQLITE_AFF_BLOB; + }else + if( pCol->affinity>=SQLITE_AFF_NUMERIC && (m&0x02)!=0 ){ + pCol->affinity = SQLITE_AFF_BLOB; + } + if( pCol->affinity>=SQLITE_AFF_NUMERIC && p->op==TK_CAST ){ + pCol->affinity = SQLITE_AFF_FLEXNUM; + } + } + zType = columnType(&sNC, p, 0, 0, 0); + if( zType==0 || pCol->affinity!=sqlite3AffinityType(zType, 0) ){ + if( pCol->affinity==SQLITE_AFF_NUMERIC + || pCol->affinity==SQLITE_AFF_FLEXNUM + ){ + zType = "NUM"; + }else{ + zType = 0; + for(j=1; jaffinity ){ + zType = sqlite3StdType[j]; + break; + } + } + } + } + if( zType ){ + const i64 k = sqlite3Strlen30(zType); + n = sqlite3Strlen30(pCol->zCnName); + pCol->zCnName = sqlite3DbReallocOrFree(db, pCol->zCnName, n+k+2); + pCol->colFlags &= ~(COLFLAG_HASTYPE|COLFLAG_HASCOLL); + if( pCol->zCnName ){ + memcpy(&pCol->zCnName[n+1], zType, k+1); + pCol->colFlags |= COLFLAG_HASTYPE; + } + } pColl = sqlite3ExprCollSeq(pParse, p); - if( pColl && pCol->zColl==0 ){ - pCol->zColl = sqlite3DbStrDup(db, pColl->zName); + if( pColl ){ + assert( pTab->pIndex==0 ); + sqlite3ColumnSetColl(db, pCol, pColl->zName); } } - pTab->szTabRow = sqlite3LogEst(szAll*4); + pTab->szTabRow = 1; /* Any non-zero value works */ } /* ** Given a SELECT statement, generate a Table structure that describes ** the result set of that SELECT. */ -Table *sqlite3ResultSetOfSelect(Parse *pParse, Select *pSelect){ +Table *sqlite3ResultSetOfSelect(Parse *pParse, Select *pSelect, char aff){ Table *pTab; sqlite3 *db = pParse->db; - int savedFlags; + u64 savedFlags; savedFlags = db->flags; - db->flags &= ~SQLITE_FullColNames; + db->flags &= ~(u64)SQLITE_FullColNames; db->flags |= SQLITE_ShortColNames; sqlite3SelectPrep(pParse, pSelect, 0); + db->flags = savedFlags; if( pParse->nErr ) return 0; while( pSelect->pPrior ) pSelect = pSelect->pPrior; - db->flags = savedFlags; pTab = sqlite3DbMallocZero(db, sizeof(Table) ); if( pTab==0 ){ return 0; } - /* The sqlite3ResultSetOfSelect() is only used n contexts where lookaside - ** is disabled */ - assert( db->lookaside.bDisable ); - pTab->nRef = 1; + pTab->nTabRef = 1; pTab->zName = 0; pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) ); sqlite3ColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol); - selectAddColumnTypeAndCollation(pParse, pTab, pSelect); + sqlite3SubqueryColumnTypes(pParse, pTab, pSelect, aff); pTab->iPKey = -1; if( db->mallocFailed ){ sqlite3DeleteTable(db, pTab); @@ -1783,40 +2491,37 @@ Table *sqlite3ResultSetOfSelect(Parse *pParse, Select *pSelect){ ** If an error occurs, return NULL and leave a message in pParse. */ Vdbe *sqlite3GetVdbe(Parse *pParse){ - Vdbe *v = pParse->pVdbe; - if( v==0 ){ - v = pParse->pVdbe = sqlite3VdbeCreate(pParse); - if( v ) sqlite3VdbeAddOp0(v, OP_Init); - if( pParse->pToplevel==0 - && OptimizationEnabled(pParse->db,SQLITE_FactorOutConst) - ){ - pParse->okConstFactor = 1; - } - + if( pParse->pVdbe ){ + return pParse->pVdbe; + } + if( pParse->pToplevel==0 + && OptimizationEnabled(pParse->db,SQLITE_FactorOutConst) + ){ + pParse->okConstFactor = 1; } - return v; + return sqlite3VdbeCreate(pParse); } /* ** Compute the iLimit and iOffset fields of the SELECT based on the -** pLimit and pOffset expressions. pLimit and pOffset hold the expressions +** pLimit expressions. pLimit->pLeft and pLimit->pRight hold the expressions ** that appear in the original SQL statement after the LIMIT and OFFSET -** keywords. Or NULL if those keywords are omitted. iLimit and iOffset -** are the integer memory register numbers for counters used to compute -** the limit and offset. If there is no limit and/or offset, then +** keywords. Or NULL if those keywords are omitted. iLimit and iOffset +** are the integer memory register numbers for counters used to compute +** the limit and offset. If there is no limit and/or offset, then ** iLimit and iOffset are negative. ** ** This routine changes the values of iLimit and iOffset only if -** a limit or offset is defined by pLimit and pOffset. iLimit and -** iOffset should have been preset to appropriate default values (zero) +** a limit or offset is defined by pLimit->pLeft and pLimit->pRight. iLimit +** and iOffset should have been preset to appropriate default values (zero) ** prior to calling this routine. ** ** The iOffset register (if it exists) is initialized to the value ** of the OFFSET. The iLimit register is initialized to LIMIT. Register ** iOffset+1 is initialized to LIMIT+OFFSET. ** -** Only if pLimit!=0 or pOffset!=0 do the limit registers get +** Only if pLimit->pLeft!=0 do the limit registers get ** redefined. The UNION ALL operator uses this property to force ** the reuse of the same limit and offset registers across multiple ** SELECT statements. @@ -1826,38 +2531,41 @@ static void computeLimitRegisters(Parse *pParse, Select *p, int iBreak){ int iLimit = 0; int iOffset; int n; + Expr *pLimit = p->pLimit; + if( p->iLimit ) return; - /* + /* ** "LIMIT -1" always shows all rows. There is some ** controversy about what the correct behavior should be. ** The current implementation interprets "LIMIT 0" to mean ** no rows. */ - sqlite3ExprCacheClear(pParse); - assert( p->pOffset==0 || p->pLimit!=0 ); - if( p->pLimit ){ + if( pLimit ){ + assert( pLimit->op==TK_LIMIT ); + assert( pLimit->pLeft!=0 ); p->iLimit = iLimit = ++pParse->nMem; v = sqlite3GetVdbe(pParse); assert( v!=0 ); - if( sqlite3ExprIsInteger(p->pLimit, &n) ){ + if( sqlite3ExprIsInteger(pLimit->pLeft, &n, pParse) ){ sqlite3VdbeAddOp2(v, OP_Integer, n, iLimit); VdbeComment((v, "LIMIT counter")); if( n==0 ){ sqlite3VdbeGoto(v, iBreak); - }else if( n>=0 && p->nSelectRow>(u64)n ){ - p->nSelectRow = n; + }else if( n>=0 && p->nSelectRow>sqlite3LogEst((u64)n) ){ + p->nSelectRow = sqlite3LogEst((u64)n); + p->selFlags |= SF_FixedLimit; } }else{ - sqlite3ExprCode(pParse, p->pLimit, iLimit); + sqlite3ExprCode(pParse, pLimit->pLeft, iLimit); sqlite3VdbeAddOp1(v, OP_MustBeInt, iLimit); VdbeCoverage(v); VdbeComment((v, "LIMIT counter")); sqlite3VdbeAddOp2(v, OP_IfNot, iLimit, iBreak); VdbeCoverage(v); } - if( p->pOffset ){ + if( pLimit->pRight ){ p->iOffset = iOffset = ++pParse->nMem; pParse->nMem++; /* Allocate an extra register for limit+offset */ - sqlite3ExprCode(pParse, p->pOffset, iOffset); + sqlite3ExprCode(pParse, pLimit->pRight, iOffset); sqlite3VdbeAddOp1(v, OP_MustBeInt, iOffset); VdbeCoverage(v); VdbeComment((v, "OFFSET counter")); sqlite3VdbeAddOp3(v, OP_OffsetLimit, iLimit, iOffset+1, iOffset); @@ -1903,7 +2611,7 @@ static CollSeq *multiSelectCollSeq(Parse *pParse, Select *p, int iCol){ */ static KeyInfo *multiSelectOrderByKeyInfo(Parse *pParse, Select *p, int nExtra){ ExprList *pOrderBy = p->pOrderBy; - int nOrderBy = p->pOrderBy->nExpr; + int nOrderBy = ALWAYS(pOrderBy!=0) ? pOrderBy->nExpr : 0; sqlite3 *db = pParse->db; KeyInfo *pRet = sqlite3KeyInfoAlloc(db, nOrderBy+nExtra, 1); if( pRet ){ @@ -1923,7 +2631,7 @@ static KeyInfo *multiSelectOrderByKeyInfo(Parse *pParse, Select *p, int nExtra){ } assert( sqlite3KeyInfoIsWriteable(pRet) ); pRet->aColl[i] = pColl; - pRet->aSortOrder[i] = pOrderBy->a[i].sortOrder; + pRet->aSortFlags[i] = pOrderBy->a[i].fg.sortFlags; } } @@ -1955,7 +2663,7 @@ static KeyInfo *multiSelectOrderByKeyInfo(Parse *pParse, Select *p, int nExtra){ ** inserted into the Queue table. The iDistinct table keeps a copy of all rows ** that have ever been inserted into Queue and causes duplicates to be ** discarded. If the operator is UNION ALL, then duplicates are allowed. -** +** ** If the query has an ORDER BY, then entries in the Queue table are kept in ** ORDER BY order and the first entry is extracted for each cycle. Without ** an ORDER BY, the Queue table is just a FIFO. @@ -1975,7 +2683,8 @@ static void generateWithRecursiveQuery( SrcList *pSrc = p->pSrc; /* The FROM clause of the recursive query */ int nCol = p->pEList->nExpr; /* Number of columns in the recursive table */ Vdbe *v = pParse->pVdbe; /* The prepared statement under construction */ - Select *pSetup = p->pPrior; /* The setup query */ + Select *pSetup; /* The setup query */ + Select *pFirstRec; /* Left-most recursive term */ int addrTop; /* Top of the loop */ int addrCont, addrBreak; /* CONTINUE and BREAK addresses */ int iCurrent = 0; /* The Current table */ @@ -1983,24 +2692,31 @@ static void generateWithRecursiveQuery( int iQueue; /* The Queue table */ int iDistinct = 0; /* To ensure unique results if UNION */ int eDest = SRT_Fifo; /* How to write to Queue */ - SelectDest destQueue; /* SelectDest targetting the Queue table */ + SelectDest destQueue; /* SelectDest targeting the Queue table */ int i; /* Loop counter */ int rc; /* Result code */ ExprList *pOrderBy; /* The ORDER BY clause */ - Expr *pLimit, *pOffset; /* Saved LIMIT and OFFSET */ + Expr *pLimit; /* Saved LIMIT and OFFSET */ int regLimit, regOffset; /* Registers used by LIMIT and OFFSET */ +#ifndef SQLITE_OMIT_WINDOWFUNC + if( p->pWin ){ + sqlite3ErrorMsg(pParse, "cannot use window functions in recursive queries"); + return; + } +#endif + /* Obtain authorization to do a recursive query */ if( sqlite3AuthCheck(pParse, SQLITE_RECURSIVE, 0, 0, 0) ) return; /* Process the LIMIT and OFFSET clauses, if they exist */ - addrBreak = sqlite3VdbeMakeLabel(v); + addrBreak = sqlite3VdbeMakeLabel(pParse); + p->nSelectRow = 320; /* 4 billion rows */ computeLimitRegisters(pParse, p, addrBreak); pLimit = p->pLimit; - pOffset = p->pOffset; regLimit = p->iLimit; regOffset = p->iOffset; - p->pLimit = p->pOffset = 0; + p->pLimit = 0; p->iLimit = p->iOffset = 0; pOrderBy = p->pOrderBy; @@ -2044,8 +2760,26 @@ static void generateWithRecursiveQuery( /* Detach the ORDER BY clause from the compound SELECT */ p->pOrderBy = 0; + /* Figure out how many elements of the compound SELECT are part of the + ** recursive query. Make sure no recursive elements use aggregate + ** functions. Mark the recursive elements as UNION ALL even if they + ** are really UNION because the distinctness will be enforced by the + ** iDistinct table. pFirstRec is left pointing to the left-most + ** recursive term of the CTE. + */ + for(pFirstRec=p; ALWAYS(pFirstRec!=0); pFirstRec=pFirstRec->pPrior){ + if( pFirstRec->selFlags & SF_Aggregate ){ + sqlite3ErrorMsg(pParse, "recursive aggregate queries not supported"); + goto end_of_recursive_query; + } + pFirstRec->op = TK_ALL; + if( (pFirstRec->pPrior->selFlags & SF_Recursive)==0 ) break; + } + /* Store the results of the setup-query in Queue. */ + pSetup = pFirstRec->pPrior; pSetup->pNext = 0; + ExplainQueryPlan((pParse, 1, "SETUP")); rc = sqlite3Select(pParse, pSetup, &destQueue); pSetup->pNext = p; if( rc ) goto end_of_recursive_query; @@ -2063,9 +2797,9 @@ static void generateWithRecursiveQuery( sqlite3VdbeAddOp1(v, OP_Delete, iQueue); /* Output the single row in Current */ - addrCont = sqlite3VdbeMakeLabel(v); + addrCont = sqlite3VdbeMakeLabel(pParse); codeOffset(v, regOffset, addrCont); - selectInnerLoop(pParse, p, p->pEList, iCurrent, + selectInnerLoop(pParse, p, iCurrent, 0, 0, pDest, addrCont, addrBreak); if( regLimit ){ sqlite3VdbeAddOp2(v, OP_DecrJumpZero, regLimit, addrBreak); @@ -2076,14 +2810,11 @@ static void generateWithRecursiveQuery( /* Execute the recursive SELECT taking the single row in Current as ** the value for the recursive-table. Store the results in the Queue. */ - if( p->selFlags & SF_Aggregate ){ - sqlite3ErrorMsg(pParse, "recursive aggregate queries not supported"); - }else{ - p->pPrior = 0; - sqlite3Select(pParse, p, &destQueue); - assert( p->pPrior==0 ); - p->pPrior = pSetup; - } + pFirstRec->pPrior = 0; + ExplainQueryPlan((pParse, 1, "RECURSIVE STEP")); + sqlite3Select(pParse, p, &destQueue); + assert( pFirstRec->pPrior==0 ); + pFirstRec->pPrior = pSetup; /* Keep running the loop until the Queue is empty */ sqlite3VdbeGoto(v, addrTop); @@ -2093,7 +2824,6 @@ static void generateWithRecursiveQuery( sqlite3ExprListDelete(pParse->db, p->pOrderBy); p->pOrderBy = pOrderBy; p->pLimit = pLimit; - p->pOffset = pOffset; return; } #endif /* SQLITE_OMIT_CTE */ @@ -2112,42 +2842,57 @@ static int multiSelectOrderBy( ** on a VALUES clause. ** ** Because the Select object originates from a VALUES clause: -** (1) It has no LIMIT or OFFSET +** (1) There is no LIMIT or OFFSET or else there is a LIMIT of exactly 1 ** (2) All terms are UNION ALL ** (3) There is no ORDER BY clause +** +** The "LIMIT of exactly 1" case of condition (1) comes about when a VALUES +** clause occurs within scalar expression (ex: "SELECT (VALUES(1),(2),(3))"). +** The sqlite3CodeSubselect will have added the LIMIT 1 clause in tht case. +** Since the limit is exactly 1, we only need to evaluate the left-most VALUES. */ static int multiSelectValues( Parse *pParse, /* Parsing context */ Select *p, /* The right-most of SELECTs to be coded */ SelectDest *pDest /* What to do with query results */ ){ - Select *pPrior; int nRow = 1; int rc = 0; + int bShowAll = p->pLimit==0; assert( p->selFlags & SF_MultiValue ); do{ assert( p->selFlags & SF_Values ); assert( p->op==TK_ALL || (p->op==TK_SELECT && p->pPrior==0) ); - assert( p->pLimit==0 ); - assert( p->pOffset==0 ); assert( p->pNext==0 || p->pEList->nExpr==p->pNext->pEList->nExpr ); +#ifndef SQLITE_OMIT_WINDOWFUNC + if( p->pWin ) return -1; +#endif if( p->pPrior==0 ) break; assert( p->pPrior->pNext==p ); p = p->pPrior; - nRow++; + nRow += bShowAll; }while(1); + ExplainQueryPlan((pParse, 0, "SCAN %d CONSTANT ROW%s", nRow, + nRow==1 ? "" : "S")); while( p ){ - pPrior = p->pPrior; - p->pPrior = 0; - rc = sqlite3Select(pParse, p, pDest); - p->pPrior = pPrior; - if( rc ) break; + selectInnerLoop(pParse, p, -1, 0, 0, pDest, 1, 1); + if( !bShowAll ) break; p->nSelectRow = nRow; p = p->pNext; } return rc; } +/* +** Return true if the SELECT statement which is known to be the recursive +** part of a recursive CTE still has its anchor terms attached. If the +** anchor terms have already been removed, then return false. +*/ +static int hasAnchor(Select *p){ + while( p && (p->selFlags & SF_Recursive)!=0 ){ p = p->pPrior; } + return p!=0; +} + /* ** This routine is called to process a compound query form from ** two or more separate queries using UNION, UNION ALL, EXCEPT, or @@ -2155,7 +2900,7 @@ static int multiSelectValues( ** ** "p" points to the right-most of the two queries. the query on the ** left is p->pPrior. The left query could also be a compound query -** in which case this routine will be called recursively. +** in which case this routine will be called recursively. ** ** The results of the total query are to be written into a destination ** of type eDest with parameter iParm. @@ -2190,31 +2935,18 @@ static int multiSelect( SelectDest dest; /* Alternative data destination */ Select *pDelete = 0; /* Chain of simple selects to delete */ sqlite3 *db; /* Database connection */ -#ifndef SQLITE_OMIT_EXPLAIN - int iSub1 = 0; /* EQP id of left-hand query */ - int iSub2 = 0; /* EQP id of right-hand query */ -#endif /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs. Only ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT. */ assert( p && p->pPrior ); /* Calling function guarantees this much */ assert( (p->selFlags & SF_Recursive)==0 || p->op==TK_ALL || p->op==TK_UNION ); + assert( p->selFlags & SF_Compound ); db = pParse->db; pPrior = p->pPrior; dest = *pDest; - if( pPrior->pOrderBy ){ - sqlite3ErrorMsg(pParse,"ORDER BY clause should come after %s not before", - selectOpName(p->op)); - rc = 1; - goto multi_select_end; - } - if( pPrior->pLimit ){ - sqlite3ErrorMsg(pParse,"LIMIT clause should come after %s not before", - selectOpName(p->op)); - rc = 1; - goto multi_select_end; - } + assert( pPrior->pOrderBy==0 ); + assert( pPrior->pLimit==0 ); v = sqlite3GetVdbe(pParse); assert( v!=0 ); /* The VDBE already created by calling function */ @@ -2224,7 +2956,6 @@ static int multiSelect( if( dest.eDest==SRT_EphemTab ){ assert( p->pEList ); sqlite3VdbeAddOp2(v, OP_OpenEphemeral, dest.iSDParm, p->pEList->nExpr); - sqlite3VdbeChangeP5(v, BTREE_UNORDERED); dest.eDest = SRT_Table; } @@ -2232,7 +2963,8 @@ static int multiSelect( */ if( p->selFlags & SF_MultiValue ){ rc = multiSelectValues(pParse, p, &dest); - goto multi_select_end; + if( rc>=0 ) goto multi_select_end; + rc = SQLITE_OK; } /* Make sure all SELECTs in the statement have the same number of elements @@ -2242,7 +2974,7 @@ static int multiSelect( assert( p->pEList->nExpr==pPrior->pEList->nExpr ); #ifndef SQLITE_OMIT_CTE - if( p->selFlags & SF_Recursive ){ + if( (p->selFlags & SF_Recursive)!=0 && hasAnchor(p) ){ generateWithRecursiveQuery(pParse, p, &dest); }else #endif @@ -2251,235 +2983,262 @@ static int multiSelect( */ if( p->pOrderBy ){ return multiSelectOrderBy(pParse, p, pDest); - }else + }else{ - /* Generate code for the left and right SELECT statements. - */ - switch( p->op ){ - case TK_ALL: { - int addr = 0; - int nLimit; - assert( !pPrior->pLimit ); - pPrior->iLimit = p->iLimit; - pPrior->iOffset = p->iOffset; - pPrior->pLimit = p->pLimit; - pPrior->pOffset = p->pOffset; - explainSetInteger(iSub1, pParse->iNextSelectId); - rc = sqlite3Select(pParse, pPrior, &dest); - p->pLimit = 0; - p->pOffset = 0; - if( rc ){ - goto multi_select_end; - } - p->pPrior = 0; - p->iLimit = pPrior->iLimit; - p->iOffset = pPrior->iOffset; - if( p->iLimit ){ - addr = sqlite3VdbeAddOp1(v, OP_IfNot, p->iLimit); VdbeCoverage(v); - VdbeComment((v, "Jump ahead if LIMIT reached")); - if( p->iOffset ){ - sqlite3VdbeAddOp3(v, OP_OffsetLimit, - p->iLimit, p->iOffset+1, p->iOffset); +#ifndef SQLITE_OMIT_EXPLAIN + if( pPrior->pPrior==0 ){ + ExplainQueryPlan((pParse, 1, "COMPOUND QUERY")); + ExplainQueryPlan((pParse, 1, "LEFT-MOST SUBQUERY")); + } +#endif + + /* Generate code for the left and right SELECT statements. + */ + switch( p->op ){ + case TK_ALL: { + int addr = 0; + int nLimit = 0; /* Initialize to suppress harmless compiler warning */ + assert( !pPrior->pLimit ); + pPrior->iLimit = p->iLimit; + pPrior->iOffset = p->iOffset; + pPrior->pLimit = p->pLimit; + TREETRACE(0x200, pParse, p, ("multiSelect UNION ALL left...\n")); + rc = sqlite3Select(pParse, pPrior, &dest); + pPrior->pLimit = 0; + if( rc ){ + goto multi_select_end; } + p->pPrior = 0; + p->iLimit = pPrior->iLimit; + p->iOffset = pPrior->iOffset; + if( p->iLimit ){ + addr = sqlite3VdbeAddOp1(v, OP_IfNot, p->iLimit); VdbeCoverage(v); + VdbeComment((v, "Jump ahead if LIMIT reached")); + if( p->iOffset ){ + sqlite3VdbeAddOp3(v, OP_OffsetLimit, + p->iLimit, p->iOffset+1, p->iOffset); + } + } + ExplainQueryPlan((pParse, 1, "UNION ALL")); + TREETRACE(0x200, pParse, p, ("multiSelect UNION ALL right...\n")); + rc = sqlite3Select(pParse, p, &dest); + testcase( rc!=SQLITE_OK ); + pDelete = p->pPrior; + p->pPrior = pPrior; + p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow); + if( p->pLimit + && sqlite3ExprIsInteger(p->pLimit->pLeft, &nLimit, pParse) + && nLimit>0 && p->nSelectRow > sqlite3LogEst((u64)nLimit) + ){ + p->nSelectRow = sqlite3LogEst((u64)nLimit); + } + if( addr ){ + sqlite3VdbeJumpHere(v, addr); + } + break; } - explainSetInteger(iSub2, pParse->iNextSelectId); - rc = sqlite3Select(pParse, p, &dest); - testcase( rc!=SQLITE_OK ); - pDelete = p->pPrior; - p->pPrior = pPrior; - p->nSelectRow += pPrior->nSelectRow; - if( pPrior->pLimit - && sqlite3ExprIsInteger(pPrior->pLimit, &nLimit) - && nLimit>0 && p->nSelectRow > (u64)nLimit - ){ - p->nSelectRow = nLimit; - } - if( addr ){ - sqlite3VdbeJumpHere(v, addr); - } - break; - } - case TK_EXCEPT: - case TK_UNION: { - int unionTab; /* Cursor number of the temporary table holding result */ - u8 op = 0; /* One of the SRT_ operations to apply to self */ - int priorOp; /* The SRT_ operation to apply to prior selects */ - Expr *pLimit, *pOffset; /* Saved values of p->nLimit and p->nOffset */ - int addr; - SelectDest uniondest; - - testcase( p->op==TK_EXCEPT ); - testcase( p->op==TK_UNION ); - priorOp = SRT_Union; - if( dest.eDest==priorOp ){ - /* We can reuse a temporary table generated by a SELECT to our - ** right. + case TK_EXCEPT: + case TK_UNION: { + int unionTab; /* Cursor number of the temp table holding result */ + u8 op = 0; /* One of the SRT_ operations to apply to self */ + int priorOp; /* The SRT_ operation to apply to prior selects */ + Expr *pLimit; /* Saved values of p->nLimit */ + int addr; + int emptyBypass = 0; /* IfEmpty opcode to bypass RHS */ + SelectDest uniondest; + + + testcase( p->op==TK_EXCEPT ); + testcase( p->op==TK_UNION ); + priorOp = SRT_Union; + if( dest.eDest==priorOp ){ + /* We can reuse a temporary table generated by a SELECT to our + ** right. + */ + assert( p->pLimit==0 ); /* Not allowed on leftward elements */ + unionTab = dest.iSDParm; + }else{ + /* We will need to create our own temporary table to hold the + ** intermediate results. + */ + unionTab = pParse->nTab++; + assert( p->pOrderBy==0 ); + addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, unionTab, 0); + assert( p->addrOpenEphm[0] == -1 ); + p->addrOpenEphm[0] = addr; + findRightmost(p)->selFlags |= SF_UsesEphemeral; + assert( p->pEList ); + } + + + /* Code the SELECT statements to our left */ - assert( p->pLimit==0 ); /* Not allowed on leftward elements */ - assert( p->pOffset==0 ); /* Not allowed on leftward elements */ - unionTab = dest.iSDParm; - }else{ - /* We will need to create our own temporary table to hold the - ** intermediate results. + assert( !pPrior->pOrderBy ); + sqlite3SelectDestInit(&uniondest, priorOp, unionTab); + TREETRACE(0x200, pParse, p, ("multiSelect EXCEPT/UNION left...\n")); + rc = sqlite3Select(pParse, pPrior, &uniondest); + if( rc ){ + goto multi_select_end; + } + + /* Code the current SELECT statement + */ + if( p->op==TK_EXCEPT ){ + op = SRT_Except; + emptyBypass = sqlite3VdbeAddOp1(v, OP_IfEmpty, unionTab); + VdbeCoverage(v); + }else{ + assert( p->op==TK_UNION ); + op = SRT_Union; + } + p->pPrior = 0; + pLimit = p->pLimit; + p->pLimit = 0; + uniondest.eDest = op; + ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE", + sqlite3SelectOpName(p->op))); + TREETRACE(0x200, pParse, p, ("multiSelect EXCEPT/UNION right...\n")); + rc = sqlite3Select(pParse, p, &uniondest); + testcase( rc!=SQLITE_OK ); + assert( p->pOrderBy==0 ); + pDelete = p->pPrior; + p->pPrior = pPrior; + p->pOrderBy = 0; + if( p->op==TK_UNION ){ + p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow); + } + if( emptyBypass ) sqlite3VdbeJumpHere(v, emptyBypass); + sqlite3ExprDelete(db, p->pLimit); + p->pLimit = pLimit; + p->iLimit = 0; + p->iOffset = 0; + + /* Convert the data in the temporary table into whatever form + ** it is that we currently need. + */ + assert( unionTab==dest.iSDParm || dest.eDest!=priorOp ); + assert( p->pEList || db->mallocFailed ); + if( dest.eDest!=priorOp && db->mallocFailed==0 ){ + int iCont, iBreak, iStart; + iBreak = sqlite3VdbeMakeLabel(pParse); + iCont = sqlite3VdbeMakeLabel(pParse); + computeLimitRegisters(pParse, p, iBreak); + sqlite3VdbeAddOp2(v, OP_Rewind, unionTab, iBreak); VdbeCoverage(v); + iStart = sqlite3VdbeCurrentAddr(v); + selectInnerLoop(pParse, p, unionTab, + 0, 0, &dest, iCont, iBreak); + sqlite3VdbeResolveLabel(v, iCont); + sqlite3VdbeAddOp2(v, OP_Next, unionTab, iStart); VdbeCoverage(v); + sqlite3VdbeResolveLabel(v, iBreak); + sqlite3VdbeAddOp2(v, OP_Close, unionTab, 0); + } + break; + } + default: assert( p->op==TK_INTERSECT ); { + int tab1, tab2; + int iCont, iBreak, iStart; + Expr *pLimit; + int addr, iLimit, iOffset; + SelectDest intersectdest; + int r1; + int emptyBypass; + + /* INTERSECT is different from the others since it requires + ** two temporary tables. Hence it has its own case. Begin + ** by allocating the tables we will need. */ - unionTab = pParse->nTab++; + tab1 = pParse->nTab++; + tab2 = pParse->nTab++; assert( p->pOrderBy==0 ); - addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, unionTab, 0); + + addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab1, 0); assert( p->addrOpenEphm[0] == -1 ); p->addrOpenEphm[0] = addr; findRightmost(p)->selFlags |= SF_UsesEphemeral; assert( p->pEList ); - } + + /* Code the SELECTs to our left into temporary table "tab1". + */ + sqlite3SelectDestInit(&intersectdest, SRT_Union, tab1); + TREETRACE(0x400, pParse, p, ("multiSelect INTERSECT left...\n")); + rc = sqlite3Select(pParse, pPrior, &intersectdest); + if( rc ){ + goto multi_select_end; + } - /* Code the SELECT statements to our left - */ - assert( !pPrior->pOrderBy ); - sqlite3SelectDestInit(&uniondest, priorOp, unionTab); - explainSetInteger(iSub1, pParse->iNextSelectId); - rc = sqlite3Select(pParse, pPrior, &uniondest); - if( rc ){ - goto multi_select_end; - } + /* Initialize LIMIT counters before checking to see if the LHS + ** is empty, in case the jump is taken */ + iBreak = sqlite3VdbeMakeLabel(pParse); + computeLimitRegisters(pParse, p, iBreak); + emptyBypass = sqlite3VdbeAddOp1(v, OP_IfEmpty, tab1); VdbeCoverage(v); + + /* Code the current SELECT into temporary table "tab2" + */ + addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab2, 0); + assert( p->addrOpenEphm[1] == -1 ); + p->addrOpenEphm[1] = addr; + + /* Disable prior SELECTs and the LIMIT counters during the computation + ** of the RHS select */ + pLimit = p->pLimit; + iLimit = p->iLimit; + iOffset = p->iOffset; + p->pPrior = 0; + p->pLimit = 0; + p->iLimit = 0; + p->iOffset = 0; + + intersectdest.iSDParm = tab2; + ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE", + sqlite3SelectOpName(p->op))); + TREETRACE(0x400, pParse, p, ("multiSelect INTERSECT right...\n")); + rc = sqlite3Select(pParse, p, &intersectdest); + testcase( rc!=SQLITE_OK ); + pDelete = p->pPrior; + p->pPrior = pPrior; + if( p->nSelectRow>pPrior->nSelectRow ){ + p->nSelectRow = pPrior->nSelectRow; + } + sqlite3ExprDelete(db, p->pLimit); - /* Code the current SELECT statement - */ - if( p->op==TK_EXCEPT ){ - op = SRT_Except; - }else{ - assert( p->op==TK_UNION ); - op = SRT_Union; - } - p->pPrior = 0; - pLimit = p->pLimit; - p->pLimit = 0; - pOffset = p->pOffset; - p->pOffset = 0; - uniondest.eDest = op; - explainSetInteger(iSub2, pParse->iNextSelectId); - rc = sqlite3Select(pParse, p, &uniondest); - testcase( rc!=SQLITE_OK ); - /* Query flattening in sqlite3Select() might refill p->pOrderBy. - ** Be sure to delete p->pOrderBy, therefore, to avoid a memory leak. */ - sqlite3ExprListDelete(db, p->pOrderBy); - pDelete = p->pPrior; - p->pPrior = pPrior; - p->pOrderBy = 0; - if( p->op==TK_UNION ) p->nSelectRow += pPrior->nSelectRow; - sqlite3ExprDelete(db, p->pLimit); - p->pLimit = pLimit; - p->pOffset = pOffset; - p->iLimit = 0; - p->iOffset = 0; - - /* Convert the data in the temporary table into whatever form - ** it is that we currently need. - */ - assert( unionTab==dest.iSDParm || dest.eDest!=priorOp ); - if( dest.eDest!=priorOp ){ - int iCont, iBreak, iStart; + /* Reinstate the LIMIT counters prior to running the final intersect */ + p->pLimit = pLimit; + p->iLimit = iLimit; + p->iOffset = iOffset; + + /* Generate code to take the intersection of the two temporary + ** tables. + */ + if( rc ) break; assert( p->pEList ); - if( dest.eDest==SRT_Output ){ - Select *pFirst = p; - while( pFirst->pPrior ) pFirst = pFirst->pPrior; - generateColumnNames(pParse, pFirst->pSrc, pFirst->pEList); - } - iBreak = sqlite3VdbeMakeLabel(v); - iCont = sqlite3VdbeMakeLabel(v); - computeLimitRegisters(pParse, p, iBreak); - sqlite3VdbeAddOp2(v, OP_Rewind, unionTab, iBreak); VdbeCoverage(v); - iStart = sqlite3VdbeCurrentAddr(v); - selectInnerLoop(pParse, p, p->pEList, unionTab, + sqlite3VdbeAddOp1(v, OP_Rewind, tab1); + r1 = sqlite3GetTempReg(pParse); + iStart = sqlite3VdbeAddOp2(v, OP_RowData, tab1, r1); + iCont = sqlite3VdbeMakeLabel(pParse); + sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0); + VdbeCoverage(v); + sqlite3ReleaseTempReg(pParse, r1); + selectInnerLoop(pParse, p, tab1, 0, 0, &dest, iCont, iBreak); sqlite3VdbeResolveLabel(v, iCont); - sqlite3VdbeAddOp2(v, OP_Next, unionTab, iStart); VdbeCoverage(v); + sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart); VdbeCoverage(v); sqlite3VdbeResolveLabel(v, iBreak); - sqlite3VdbeAddOp2(v, OP_Close, unionTab, 0); + sqlite3VdbeAddOp2(v, OP_Close, tab2, 0); + sqlite3VdbeJumpHere(v, emptyBypass); + sqlite3VdbeAddOp2(v, OP_Close, tab1, 0); + break; } - break; } - default: assert( p->op==TK_INTERSECT ); { - int tab1, tab2; - int iCont, iBreak, iStart; - Expr *pLimit, *pOffset; - int addr; - SelectDest intersectdest; - int r1; - - /* INTERSECT is different from the others since it requires - ** two temporary tables. Hence it has its own case. Begin - ** by allocating the tables we will need. - */ - tab1 = pParse->nTab++; - tab2 = pParse->nTab++; - assert( p->pOrderBy==0 ); - - addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab1, 0); - assert( p->addrOpenEphm[0] == -1 ); - p->addrOpenEphm[0] = addr; - findRightmost(p)->selFlags |= SF_UsesEphemeral; - assert( p->pEList ); - - /* Code the SELECTs to our left into temporary table "tab1". - */ - sqlite3SelectDestInit(&intersectdest, SRT_Union, tab1); - explainSetInteger(iSub1, pParse->iNextSelectId); - rc = sqlite3Select(pParse, pPrior, &intersectdest); - if( rc ){ - goto multi_select_end; - } - - /* Code the current SELECT into temporary table "tab2" - */ - addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab2, 0); - assert( p->addrOpenEphm[1] == -1 ); - p->addrOpenEphm[1] = addr; - p->pPrior = 0; - pLimit = p->pLimit; - p->pLimit = 0; - pOffset = p->pOffset; - p->pOffset = 0; - intersectdest.iSDParm = tab2; - explainSetInteger(iSub2, pParse->iNextSelectId); - rc = sqlite3Select(pParse, p, &intersectdest); - testcase( rc!=SQLITE_OK ); - pDelete = p->pPrior; - p->pPrior = pPrior; - if( p->nSelectRow>pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow; - sqlite3ExprDelete(db, p->pLimit); - p->pLimit = pLimit; - p->pOffset = pOffset; - - /* Generate code to take the intersection of the two temporary - ** tables. - */ - assert( p->pEList ); - if( dest.eDest==SRT_Output ){ - Select *pFirst = p; - while( pFirst->pPrior ) pFirst = pFirst->pPrior; - generateColumnNames(pParse, pFirst->pSrc, pFirst->pEList); - } - iBreak = sqlite3VdbeMakeLabel(v); - iCont = sqlite3VdbeMakeLabel(v); - computeLimitRegisters(pParse, p, iBreak); - sqlite3VdbeAddOp2(v, OP_Rewind, tab1, iBreak); VdbeCoverage(v); - r1 = sqlite3GetTempReg(pParse); - iStart = sqlite3VdbeAddOp2(v, OP_RowKey, tab1, r1); - sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0); VdbeCoverage(v); - sqlite3ReleaseTempReg(pParse, r1); - selectInnerLoop(pParse, p, p->pEList, tab1, - 0, 0, &dest, iCont, iBreak); - sqlite3VdbeResolveLabel(v, iCont); - sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart); VdbeCoverage(v); - sqlite3VdbeResolveLabel(v, iBreak); - sqlite3VdbeAddOp2(v, OP_Close, tab2, 0); - sqlite3VdbeAddOp2(v, OP_Close, tab1, 0); - break; + + #ifndef SQLITE_OMIT_EXPLAIN + if( p->pNext==0 ){ + ExplainQueryPlanPop(pParse); } + #endif } - - explainComposite(pParse, p->op, iSub1, iSub2, p->op!=TK_ALL); - - /* Compute collating sequences used by + if( pParse->nErr ) goto multi_select_end; + + /* Compute collating sequences used by ** temporary tables needed to implement the compound select. ** Attach the KeyInfo structure to all temporary tables. ** @@ -2496,10 +3255,11 @@ static int multiSelect( int nCol; /* Number of columns in result set */ assert( p->pNext==0 ); + assert( p->pEList!=0 ); nCol = p->pEList->nExpr; pKeyInfo = sqlite3KeyInfoAlloc(db, nCol, 1); if( !pKeyInfo ){ - rc = SQLITE_NOMEM; + rc = SQLITE_NOMEM_BKPT; goto multi_select_end; } for(i=0, apColl=pKeyInfo->aColl; iiSdst = dest.iSdst; pDest->nSdst = dest.nSdst; - sqlite3SelectDelete(db, pDelete); + pDest->iSDParm2 = dest.iSDParm2; + if( pDelete ){ + sqlite3ParserAddCleanup(pParse, sqlite3SelectDeleteGeneric, pDelete); + } return rc; } #endif /* SQLITE_OMIT_COMPOUND_SELECT */ @@ -2544,13 +3307,14 @@ void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p){ sqlite3ErrorMsg(pParse, "all VALUES must have the same number of terms"); }else{ sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s" - " do not have the same number of result columns", selectOpName(p->op)); + " do not have the same number of result columns", + sqlite3SelectOpName(p->op)); } } /* ** Code an output subroutine for a coroutine implementation of a -** SELECT statment. +** SELECT statement. ** ** The data to be output is contained in pIn->iSdst. There are ** pIn->nSdst columns to be output. pDest is where the output should @@ -2583,9 +3347,9 @@ static int generateOutputSubroutine( int addr; addr = sqlite3VdbeCurrentAddr(v); - iContinue = sqlite3VdbeMakeLabel(v); + iContinue = sqlite3VdbeMakeLabel(pParse); - /* Suppress duplicates for UNION, EXCEPT, and INTERSECT + /* Suppress duplicates for UNION, EXCEPT, and INTERSECT */ if( regPrev ){ int addr1, addr2; @@ -2621,30 +3385,33 @@ static int generateOutputSubroutine( } #ifndef SQLITE_OMIT_SUBQUERY - /* If we are creating a set for an "expr IN (SELECT ...)" construct, - ** then there should be a single item on the stack. Write this - ** item into the set table with bogus data. + /* If we are creating a set for an "expr IN (SELECT ...)". */ case SRT_Set: { int r1; - assert( pIn->nSdst==1 || pParse->nErr>0 ); - pDest->affSdst = - sqlite3CompareAffinity(p->pEList->a[0].pExpr, pDest->affSdst); + testcase( pIn->nSdst>1 ); r1 = sqlite3GetTempReg(pParse); - sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, 1, r1, &pDest->affSdst,1); - sqlite3ExprCacheAffinityChange(pParse, pIn->iSdst, 1); - sqlite3VdbeAddOp2(v, OP_IdxInsert, pDest->iSDParm, r1); + sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst, + r1, pDest->zAffSdst, pIn->nSdst); + sqlite3VdbeAddOp4Int(v, OP_IdxInsert, pDest->iSDParm, r1, + pIn->iSdst, pIn->nSdst); + if( pDest->iSDParm2>0 ){ + sqlite3VdbeAddOp4Int(v, OP_FilterAdd, pDest->iSDParm2, 0, + pIn->iSdst, pIn->nSdst); + ExplainQueryPlan((pParse, 0, "CREATE BLOOM FILTER")); + } sqlite3ReleaseTempReg(pParse, r1); break; } /* If this is a scalar select that is part of an expression, then ** store the results in the appropriate memory cell and break out - ** of the scan loop. + ** of the scan loop. Note that the select might return multiple columns + ** if it is the RHS of a row-value IN operator. */ case SRT_Mem: { - assert( pIn->nSdst==1 || pParse->nErr>0 ); testcase( pIn->nSdst!=1 ); - sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSDParm, 1); + testcase( pIn->nSdst>1 ); + sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSDParm, pIn->nSdst); /* The LIMIT clause will jump out of the loop for us */ break; } @@ -2667,14 +3434,13 @@ static int generateOutputSubroutine( ** SRT_Output. This routine is never called with any other ** destination other than the ones handled above or SRT_Output. ** - ** For SRT_Output, results are stored in a sequence of registers. + ** For SRT_Output, results are stored in a sequence of registers. ** Then the OP_ResultRow opcode is used to cause sqlite3_step() to ** return the next row of result. */ default: { assert( pDest->eDest==SRT_Output ); sqlite3VdbeAddOp2(v, OP_ResultRow, pIn->iSdst, pIn->nSdst); - sqlite3ExprCacheAffinityChange(pParse, pIn->iSdst, pIn->nSdst); break; } } @@ -2725,7 +3491,7 @@ static int generateOutputSubroutine( ** ** EofB: Called when data is exhausted from selectB. ** -** The implementation of the latter five subroutines depend on which +** The implementation of the latter five subroutines depend on which ** is used: ** ** @@ -2775,7 +3541,7 @@ static int generateOutputSubroutine( ** ** We call AltB, AeqB, AgtB, EofA, and EofB "subroutines" but they are not ** actually called using Gosub and they do not Return. EofA and EofB loop -** until all data is exhausted then jump to the "end" labe. AltB, AeqB, +** until all data is exhausted then jump to the "end" label. AltB, AeqB, ** and AgtB jump to either L2 or to one of EofA or EofB. */ #ifndef SQLITE_OMIT_COMPOUND_SELECT @@ -2786,6 +3552,8 @@ static int multiSelectOrderBy( ){ int i, j; /* Loop counters */ Select *pPrior; /* Another SELECT immediately to our left */ + Select *pSplit; /* Left-most SELECT in the right-hand group */ + int nSelect; /* Number of SELECT statements in the compound */ Vdbe *v; /* Generate code to this VDBE */ SelectDest destA; /* Destination for coroutine A */ SelectDest destB; /* Destination for coroutine B */ @@ -2810,33 +3578,28 @@ static int multiSelectOrderBy( int savedOffset; /* Saved value of p->iOffset */ int labelCmpr; /* Label for the start of the merge algorithm */ int labelEnd; /* Label for the end of the overall SELECT stmt */ - int addr1; /* Jump instructions that get retargetted */ + int addr1; /* Jump instructions that get retargeted */ int op; /* One of TK_ALL, TK_UNION, TK_EXCEPT, TK_INTERSECT */ KeyInfo *pKeyDup = 0; /* Comparison information for duplicate removal */ KeyInfo *pKeyMerge; /* Comparison information for merging rows */ sqlite3 *db; /* Database connection */ ExprList *pOrderBy; /* The ORDER BY clause */ int nOrderBy; /* Number of terms in the ORDER BY clause */ - int *aPermute; /* Mapping from ORDER BY terms to result set columns */ -#ifndef SQLITE_OMIT_EXPLAIN - int iSub1; /* EQP id of left-hand query */ - int iSub2; /* EQP id of right-hand query */ -#endif + u32 *aPermute; /* Mapping from ORDER BY terms to result set columns */ assert( p->pOrderBy!=0 ); assert( pKeyDup==0 ); /* "Managed" code needs this. Ticket #3382. */ db = pParse->db; v = pParse->pVdbe; assert( v!=0 ); /* Already thrown the error if VDBE alloc failed */ - labelEnd = sqlite3VdbeMakeLabel(v); - labelCmpr = sqlite3VdbeMakeLabel(v); + labelEnd = sqlite3VdbeMakeLabel(pParse); + labelCmpr = sqlite3VdbeMakeLabel(pParse); /* Patch up the ORDER BY clause */ - op = p->op; - pPrior = p->pPrior; - assert( pPrior->pOrderBy==0 ); + op = p->op; + assert( p->pPrior->pOrderBy==0 ); pOrderBy = p->pOrderBy; assert( pOrderBy ); nOrderBy = pOrderBy->nExpr; @@ -2849,15 +3612,16 @@ static int multiSelectOrderBy( for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){ struct ExprList_item *pItem; for(j=0, pItem=pOrderBy->a; ju.x.iOrderByCol>0 ); if( pItem->u.x.iOrderByCol==i ) break; } if( j==nOrderBy ){ Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0); - if( pNew==0 ) return SQLITE_NOMEM; + if( pNew==0 ) return SQLITE_NOMEM_BKPT; pNew->flags |= EP_IntValue; pNew->u.iValue = i; - pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew); + p->pOrderBy = pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew); if( pOrderBy ) pOrderBy->a[nOrderBy++].u.x.iOrderByCol = (u16)i; } } @@ -2870,11 +3634,12 @@ static int multiSelectOrderBy( ** to the right and the left are evaluated, they use the correct ** collation. */ - aPermute = sqlite3DbMallocRawNN(db, sizeof(int)*(nOrderBy + 1)); + aPermute = sqlite3DbMallocRawNN(db, sizeof(u32)*(nOrderBy + 1)); if( aPermute ){ struct ExprList_item *pItem; aPermute[0] = nOrderBy; for(i=1, pItem=pOrderBy->a; i<=nOrderBy; i++, pItem++){ + assert( pItem!=0 ); assert( pItem->u.x.iOrderByCol>0 ); assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr ); aPermute[i] = pItem->u.x.iOrderByCol - 1; @@ -2884,11 +3649,6 @@ static int multiSelectOrderBy( pKeyMerge = 0; } - /* Reattach the ORDER BY clause to the query. - */ - p->pOrderBy = pOrderBy; - pPrior->pOrderBy = sqlite3ExprListDup(pParse->db, pOrderBy, 0); - /* Allocate a range of temporary registers and the KeyInfo needed ** for the logic that removes duplicate result rows when the ** operator is UNION, EXCEPT, or INTERSECT (but not UNION ALL). @@ -2906,19 +3666,37 @@ static int multiSelectOrderBy( assert( sqlite3KeyInfoIsWriteable(pKeyDup) ); for(i=0; iaColl[i] = multiSelectCollSeq(pParse, p, i); - pKeyDup->aSortOrder[i] = 0; + pKeyDup->aSortFlags[i] = 0; } } } - + /* Separate the left and the right query from one another */ - p->pPrior = 0; + nSelect = 1; + if( (op==TK_ALL || op==TK_UNION) + && OptimizationEnabled(db, SQLITE_BalancedMerge) + ){ + for(pSplit=p; pSplit->pPrior!=0 && pSplit->op==op; pSplit=pSplit->pPrior){ + nSelect++; + assert( pSplit->pPrior->pNext==pSplit ); + } + } + if( nSelect<=3 ){ + pSplit = p; + }else{ + pSplit = p; + for(i=2; ipPrior; } + } + pPrior = pSplit->pPrior; + assert( pPrior!=0 ); + pSplit->pPrior = 0; pPrior->pNext = 0; + assert( p->pOrderBy == pOrderBy ); + assert( pOrderBy!=0 || db->mallocFailed ); + pPrior->pOrderBy = sqlite3ExprListDup(pParse->db, pOrderBy, 0); sqlite3ResolveOrderGroupBy(pParse, p, p->pOrderBy, "ORDER"); - if( pPrior->pPrior==0 ){ - sqlite3ResolveOrderGroupBy(pParse, pPrior, pPrior->pOrderBy, "ORDER"); - } + sqlite3ResolveOrderGroupBy(pParse, pPrior, pPrior->pOrderBy, "ORDER"); /* Compute the limit registers */ computeLimitRegisters(pParse, p, labelEnd); @@ -2933,8 +3711,6 @@ static int multiSelectOrderBy( } sqlite3ExprDelete(db, p->pLimit); p->pLimit = 0; - sqlite3ExprDelete(db, p->pOffset); - p->pOffset = 0; regAddrA = ++pParse->nMem; regAddrB = ++pParse->nMem; @@ -2943,6 +3719,8 @@ static int multiSelectOrderBy( sqlite3SelectDestInit(&destA, SRT_Coroutine, regAddrA); sqlite3SelectDestInit(&destB, SRT_Coroutine, regAddrB); + ExplainQueryPlan((pParse, 1, "MERGE (%s)", sqlite3SelectOpName(p->op))); + /* Generate a coroutine to evaluate the SELECT statement to the ** left of the compound operator - the "A" select. */ @@ -2950,12 +3728,12 @@ static int multiSelectOrderBy( addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrA, 0, addrSelectA); VdbeComment((v, "left SELECT")); pPrior->iLimit = regLimitA; - explainSetInteger(iSub1, pParse->iNextSelectId); + ExplainQueryPlan((pParse, 1, "LEFT")); sqlite3Select(pParse, pPrior, &destA); sqlite3VdbeEndCoroutine(v, regAddrA); sqlite3VdbeJumpHere(v, addr1); - /* Generate a coroutine to evaluate the SELECT statement on + /* Generate a coroutine to evaluate the SELECT statement on ** the right - the "B" select */ addrSelectB = sqlite3VdbeCurrentAddr(v) + 1; @@ -2964,8 +3742,8 @@ static int multiSelectOrderBy( savedLimit = p->iLimit; savedOffset = p->iOffset; p->iLimit = regLimitB; - p->iOffset = 0; - explainSetInteger(iSub2, pParse->iNextSelectId); + p->iOffset = 0; + ExplainQueryPlan((pParse, 1, "RIGHT")); sqlite3Select(pParse, p, &destB); p->iLimit = savedLimit; p->iOffset = savedOffset; @@ -2978,7 +3756,7 @@ static int multiSelectOrderBy( addrOutA = generateOutputSubroutine(pParse, p, &destA, pDest, regOutA, regPrev, pKeyDup, labelEnd); - + /* Generate a subroutine that outputs the current row of the B ** select as the next output row of the compound select. */ @@ -2995,13 +3773,13 @@ static int multiSelectOrderBy( */ if( op==TK_EXCEPT || op==TK_INTERSECT ){ addrEofA_noB = addrEofA = labelEnd; - }else{ + }else{ VdbeNoopComment((v, "eof-A subroutine")); addrEofA = sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB); addrEofA_noB = sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, labelEnd); VdbeCoverage(v); sqlite3VdbeGoto(v, addrEofA); - p->nSelectRow += pPrior->nSelectRow; + p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow); } /* Generate a subroutine to run when the results from select B @@ -3010,7 +3788,7 @@ static int multiSelectOrderBy( if( op==TK_INTERSECT ){ addrEofB = addrEofA; if( p->nSelectRow > pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow; - }else{ + }else{ VdbeNoopComment((v, "eof-B subroutine")); addrEofB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA); sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, labelEnd); VdbeCoverage(v); @@ -3067,117 +3845,405 @@ static int multiSelectOrderBy( */ sqlite3VdbeResolveLabel(v, labelEnd); - /* Set the number of output columns - */ - if( pDest->eDest==SRT_Output ){ - Select *pFirst = pPrior; - while( pFirst->pPrior ) pFirst = pFirst->pPrior; - generateColumnNames(pParse, pFirst->pSrc, pFirst->pEList); - } - - /* Reassembly the compound query so that it will be freed correctly - ** by the calling function */ - if( p->pPrior ){ - sqlite3SelectDelete(db, p->pPrior); + /* Make arrangements to free the 2nd and subsequent arms of the compound + ** after the parse has finished */ + if( pSplit->pPrior ){ + sqlite3ParserAddCleanup(pParse, sqlite3SelectDeleteGeneric, pSplit->pPrior); } - p->pPrior = pPrior; - pPrior->pNext = p; + pSplit->pPrior = pPrior; + pPrior->pNext = pSplit; + sqlite3ExprListDelete(db, pPrior->pOrderBy); + pPrior->pOrderBy = 0; /*** TBD: Insert subroutine calls to close cursors on incomplete **** subqueries ****/ - explainComposite(pParse, p->op, iSub1, iSub2, 0); + ExplainQueryPlanPop(pParse); return pParse->nErr!=0; } #endif #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) -/* Forward Declarations */ -static void substExprList(sqlite3*, ExprList*, int, ExprList*); -static void substSelect(sqlite3*, Select *, int, ExprList*, int); + +/* An instance of the SubstContext object describes an substitution edit +** to be performed on a parse tree. +** +** All references to columns in table iTable are to be replaced by corresponding +** expressions in pEList. +** +** ## About "isOuterJoin": +** +** The isOuterJoin column indicates that the replacement will occur into a +** position in the parent that is NULL-able due to an OUTER JOIN. Either the +** target slot in the parent is the right operand of a LEFT JOIN, or one of +** the left operands of a RIGHT JOIN. In either case, we need to potentially +** bypass the substituted expression with OP_IfNullRow. +** +** Suppose the original expression is an integer constant. Even though the table +** has the nullRow flag set, because the expression is an integer constant, +** it will not be NULLed out. So instead, we insert an OP_IfNullRow opcode +** that checks to see if the nullRow flag is set on the table. If the nullRow +** flag is set, then the value in the register is set to NULL and the original +** expression is bypassed. If the nullRow flag is not set, then the original +** expression runs to populate the register. +** +** Example where this is needed: +** +** CREATE TABLE t1(a INTEGER PRIMARY KEY, b INT); +** CREATE TABLE t2(x INT UNIQUE); +** +** SELECT a,b,m,x FROM t1 LEFT JOIN (SELECT 59 AS m,x FROM t2) ON b=x; +** +** When the subquery on the right side of the LEFT JOIN is flattened, we +** have to add OP_IfNullRow in front of the OP_Integer that implements the +** "m" value of the subquery so that a NULL will be loaded instead of 59 +** when processing a non-matched row of the left. +*/ +typedef struct SubstContext { + Parse *pParse; /* The parsing context */ + int iTable; /* Replace references to this table */ + int iNewTable; /* New table number */ + int isOuterJoin; /* Add TK_IF_NULL_ROW opcodes on each replacement */ + int nSelDepth; /* Depth of sub-query recursion. Top==1 */ + ExprList *pEList; /* Replacement expressions */ + ExprList *pCList; /* Collation sequences for replacement expr */ +} SubstContext; + +/* Forward Declarations */ +static void substExprList(SubstContext*, ExprList*); +static void substSelect(SubstContext*, Select*, int); /* ** Scan through the expression pExpr. Replace every reference to ** a column in table number iTable with a copy of the iColumn-th -** entry in pEList. (But leave references to the ROWID column +** entry in pEList. (But leave references to the ROWID column ** unchanged.) ** ** This routine is part of the flattening procedure. A subquery ** whose result set is defined by pEList appears as entry in the ** FROM clause of a SELECT such that the VDBE cursor assigned to that -** FORM clause entry is iTable. This routine make the necessary +** FORM clause entry is iTable. This routine makes the necessary ** changes to pExpr so that it refers directly to the source table ** of the subquery rather the result set of the subquery. */ static Expr *substExpr( - sqlite3 *db, /* Report malloc errors to this connection */ - Expr *pExpr, /* Expr in which substitution occurs */ - int iTable, /* Table to be substituted */ - ExprList *pEList /* Substitute expressions */ + SubstContext *pSubst, /* Description of the substitution */ + Expr *pExpr /* Expr in which substitution occurs */ ){ if( pExpr==0 ) return 0; - if( pExpr->op==TK_COLUMN && pExpr->iTable==iTable ){ + if( ExprHasProperty(pExpr, EP_OuterON|EP_InnerON) + && pExpr->w.iJoin==pSubst->iTable + ){ + testcase( ExprHasProperty(pExpr, EP_InnerON) ); + pExpr->w.iJoin = pSubst->iNewTable; + } + if( pExpr->op==TK_COLUMN + && pExpr->iTable==pSubst->iTable + && !ExprHasProperty(pExpr, EP_FixedCol) + ){ +#ifdef SQLITE_ALLOW_ROWID_IN_VIEW if( pExpr->iColumn<0 ){ pExpr->op = TK_NULL; - }else{ + }else +#endif + { Expr *pNew; - assert( pEList!=0 && pExpr->iColumnnExpr ); - assert( pExpr->pLeft==0 && pExpr->pRight==0 ); - pNew = sqlite3ExprDup(db, pEList->a[pExpr->iColumn].pExpr, 0); - sqlite3ExprDelete(db, pExpr); - pExpr = pNew; + int iColumn; + Expr *pCopy; + Expr ifNullRow; + iColumn = pExpr->iColumn; + assert( iColumn>=0 ); + assert( pSubst->pEList!=0 && iColumnpEList->nExpr ); + assert( pExpr->pRight==0 ); + pCopy = pSubst->pEList->a[iColumn].pExpr; + if( sqlite3ExprIsVector(pCopy) ){ + sqlite3VectorErrorMsg(pSubst->pParse, pCopy); + }else{ + sqlite3 *db = pSubst->pParse->db; + if( pSubst->isOuterJoin + && (pCopy->op!=TK_COLUMN || pCopy->iTable!=pSubst->iNewTable) + ){ + memset(&ifNullRow, 0, sizeof(ifNullRow)); + ifNullRow.op = TK_IF_NULL_ROW; + ifNullRow.pLeft = pCopy; + ifNullRow.iTable = pSubst->iNewTable; + ifNullRow.iColumn = -99; + ifNullRow.flags = EP_IfNullRow; + pCopy = &ifNullRow; + } + testcase( ExprHasProperty(pCopy, EP_Subquery) ); + pNew = sqlite3ExprDup(db, pCopy, 0); + if( db->mallocFailed ){ + sqlite3ExprDelete(db, pNew); + return pExpr; + } + if( pSubst->isOuterJoin ){ + ExprSetProperty(pNew, EP_CanBeNull); + } + if( pNew->op==TK_TRUEFALSE ){ + pNew->u.iValue = sqlite3ExprTruthValue(pNew); + pNew->op = TK_INTEGER; + ExprSetProperty(pNew, EP_IntValue); + } + + /* Ensure that the expression now has an implicit collation sequence, + ** just as it did when it was a column of a view or sub-query. */ + { + CollSeq *pNat = sqlite3ExprCollSeq(pSubst->pParse, pNew); + CollSeq *pColl = sqlite3ExprCollSeq(pSubst->pParse, + pSubst->pCList->a[iColumn].pExpr + ); + if( pNat!=pColl || (pNew->op!=TK_COLUMN && pNew->op!=TK_COLLATE) ){ + pNew = sqlite3ExprAddCollateString(pSubst->pParse, pNew, + (pColl ? pColl->zName : "BINARY") + ); + } + } + ExprClearProperty(pNew, EP_Collate); + if( ExprHasProperty(pExpr,EP_OuterON|EP_InnerON) ){ + sqlite3SetJoinExpr(pNew, pExpr->w.iJoin, + pExpr->flags & (EP_OuterON|EP_InnerON)); + } + sqlite3ExprDelete(db, pExpr); + pExpr = pNew; + } } }else{ - pExpr->pLeft = substExpr(db, pExpr->pLeft, iTable, pEList); - pExpr->pRight = substExpr(db, pExpr->pRight, iTable, pEList); - if( ExprHasProperty(pExpr, EP_xIsSelect) ){ - substSelect(db, pExpr->x.pSelect, iTable, pEList, 1); + if( pExpr->op==TK_IF_NULL_ROW && pExpr->iTable==pSubst->iTable ){ + pExpr->iTable = pSubst->iNewTable; + } + if( pExpr->op==TK_AGG_FUNCTION && pExpr->op2>=pSubst->nSelDepth ){ + pExpr->op2--; + } + pExpr->pLeft = substExpr(pSubst, pExpr->pLeft); + pExpr->pRight = substExpr(pSubst, pExpr->pRight); + if( ExprUseXSelect(pExpr) ){ + substSelect(pSubst, pExpr->x.pSelect, 1); }else{ - substExprList(db, pExpr->x.pList, iTable, pEList); + substExprList(pSubst, pExpr->x.pList); } +#ifndef SQLITE_OMIT_WINDOWFUNC + if( ExprHasProperty(pExpr, EP_WinFunc) ){ + Window *pWin = pExpr->y.pWin; + pWin->pFilter = substExpr(pSubst, pWin->pFilter); + substExprList(pSubst, pWin->pPartition); + substExprList(pSubst, pWin->pOrderBy); + } +#endif } return pExpr; } static void substExprList( - sqlite3 *db, /* Report malloc errors here */ - ExprList *pList, /* List to scan and in which to make substitutes */ - int iTable, /* Table to be substituted */ - ExprList *pEList /* Substitute values */ + SubstContext *pSubst, /* Description of the substitution */ + ExprList *pList /* List to scan and in which to make substitutes */ ){ int i; if( pList==0 ) return; for(i=0; inExpr; i++){ - pList->a[i].pExpr = substExpr(db, pList->a[i].pExpr, iTable, pEList); + pList->a[i].pExpr = substExpr(pSubst, pList->a[i].pExpr); } } static void substSelect( - sqlite3 *db, /* Report malloc errors here */ - Select *p, /* SELECT statement in which to make substitutions */ - int iTable, /* Table to be replaced */ - ExprList *pEList, /* Substitute values */ - int doPrior /* Do substitutes on p->pPrior too */ + SubstContext *pSubst, /* Description of the substitution */ + Select *p, /* SELECT statement in which to make substitutions */ + int doPrior /* Do substitutes on p->pPrior too */ ){ SrcList *pSrc; - struct SrcList_item *pItem; + SrcItem *pItem; int i; if( !p ) return; + pSubst->nSelDepth++; do{ - substExprList(db, p->pEList, iTable, pEList); - substExprList(db, p->pGroupBy, iTable, pEList); - substExprList(db, p->pOrderBy, iTable, pEList); - p->pHaving = substExpr(db, p->pHaving, iTable, pEList); - p->pWhere = substExpr(db, p->pWhere, iTable, pEList); + substExprList(pSubst, p->pEList); + substExprList(pSubst, p->pGroupBy); + substExprList(pSubst, p->pOrderBy); + p->pHaving = substExpr(pSubst, p->pHaving); + p->pWhere = substExpr(pSubst, p->pWhere); pSrc = p->pSrc; assert( pSrc!=0 ); for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){ - substSelect(db, pItem->pSelect, iTable, pEList, 1); + if( pItem->fg.isSubquery ){ + substSelect(pSubst, pItem->u4.pSubq->pSelect, 1); + } if( pItem->fg.isTabFunc ){ - substExprList(db, pItem->u1.pFuncArg, iTable, pEList); + substExprList(pSubst, pItem->u1.pFuncArg); } } }while( doPrior && (p = p->pPrior)!=0 ); + pSubst->nSelDepth--; +} +#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */ + +#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) +/* +** pSelect is a SELECT statement and pSrcItem is one item in the FROM +** clause of that SELECT. +** +** This routine scans the entire SELECT statement and recomputes the +** pSrcItem->colUsed mask. +*/ +static int recomputeColumnsUsedExpr(Walker *pWalker, Expr *pExpr){ + SrcItem *pItem; + if( pExpr->op!=TK_COLUMN ) return WRC_Continue; + pItem = pWalker->u.pSrcItem; + if( pItem->iCursor!=pExpr->iTable ) return WRC_Continue; + if( pExpr->iColumn<0 ) return WRC_Continue; + pItem->colUsed |= sqlite3ExprColUsed(pExpr); + return WRC_Continue; +} +static void recomputeColumnsUsed( + Select *pSelect, /* The complete SELECT statement */ + SrcItem *pSrcItem /* Which FROM clause item to recompute */ +){ + Walker w; + if( NEVER(pSrcItem->pSTab==0) ) return; + memset(&w, 0, sizeof(w)); + w.xExprCallback = recomputeColumnsUsedExpr; + w.xSelectCallback = sqlite3SelectWalkNoop; + w.u.pSrcItem = pSrcItem; + pSrcItem->colUsed = 0; + sqlite3WalkSelect(&w, pSelect); +} +#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */ + +#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) +/* +** Assign new cursor numbers to each of the items in pSrc. For each +** new cursor number assigned, set an entry in the aCsrMap[] array +** to map the old cursor number to the new: +** +** aCsrMap[iOld+1] = iNew; +** +** The array is guaranteed by the caller to be large enough for all +** existing cursor numbers in pSrc. aCsrMap[0] is the array size. +** +** If pSrc contains any sub-selects, call this routine recursively +** on the FROM clause of each such sub-select, with iExcept set to -1. +*/ +static void srclistRenumberCursors( + Parse *pParse, /* Parse context */ + int *aCsrMap, /* Array to store cursor mappings in */ + SrcList *pSrc, /* FROM clause to renumber */ + int iExcept /* FROM clause item to skip */ +){ + int i; + SrcItem *pItem; + for(i=0, pItem=pSrc->a; inSrc; i++, pItem++){ + if( i!=iExcept ){ + Select *p; + assert( pItem->iCursor < aCsrMap[0] ); + if( !pItem->fg.isRecursive || aCsrMap[pItem->iCursor+1]==0 ){ + aCsrMap[pItem->iCursor+1] = pParse->nTab++; + } + pItem->iCursor = aCsrMap[pItem->iCursor+1]; + if( pItem->fg.isSubquery ){ + for(p=pItem->u4.pSubq->pSelect; p; p=p->pPrior){ + srclistRenumberCursors(pParse, aCsrMap, p->pSrc, -1); + } + } + } + } +} + +/* +** *piCursor is a cursor number. Change it if it needs to be mapped. +*/ +static void renumberCursorDoMapping(Walker *pWalker, int *piCursor){ + int *aCsrMap = pWalker->u.aiCol; + int iCsr = *piCursor; + if( iCsr < aCsrMap[0] && aCsrMap[iCsr+1]>0 ){ + *piCursor = aCsrMap[iCsr+1]; + } +} + +/* +** Expression walker callback used by renumberCursors() to update +** Expr objects to match newly assigned cursor numbers. +*/ +static int renumberCursorsCb(Walker *pWalker, Expr *pExpr){ + int op = pExpr->op; + if( op==TK_COLUMN || op==TK_IF_NULL_ROW ){ + renumberCursorDoMapping(pWalker, &pExpr->iTable); + } + if( ExprHasProperty(pExpr, EP_OuterON) ){ + renumberCursorDoMapping(pWalker, &pExpr->w.iJoin); + } + return WRC_Continue; +} + +/* +** Assign a new cursor number to each cursor in the FROM clause (Select.pSrc) +** of the SELECT statement passed as the second argument, and to each +** cursor in the FROM clause of any FROM clause sub-selects, recursively. +** Except, do not assign a new cursor number to the iExcept'th element in +** the FROM clause of (*p). Update all expressions and other references +** to refer to the new cursor numbers. +** +** Argument aCsrMap is an array that may be used for temporary working +** space. Two guarantees are made by the caller: +** +** * the array is larger than the largest cursor number used within the +** select statement passed as an argument, and +** +** * the array entries for all cursor numbers that do *not* appear in +** FROM clauses of the select statement as described above are +** initialized to zero. +*/ +static void renumberCursors( + Parse *pParse, /* Parse context */ + Select *p, /* Select to renumber cursors within */ + int iExcept, /* FROM clause item to skip */ + int *aCsrMap /* Working space */ +){ + Walker w; + srclistRenumberCursors(pParse, aCsrMap, p->pSrc, iExcept); + memset(&w, 0, sizeof(w)); + w.u.aiCol = aCsrMap; + w.xExprCallback = renumberCursorsCb; + w.xSelectCallback = sqlite3SelectWalkNoop; + sqlite3WalkSelect(&w, p); } #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */ +/* +** If pSel is not part of a compound SELECT, return a pointer to its +** expression list. Otherwise, return a pointer to the expression list +** of the leftmost SELECT in the compound. +*/ +static ExprList *findLeftmostExprlist(Select *pSel){ + while( pSel->pPrior ){ + pSel = pSel->pPrior; + } + return pSel->pEList; +} + +/* +** Return true if any of the result-set columns in the compound query +** have incompatible affinities on one or more arms of the compound. +*/ +static int compoundHasDifferentAffinities(Select *p){ + int ii; + ExprList *pList; + assert( p!=0 ); + assert( p->pEList!=0 ); + assert( p->pPrior!=0 ); + pList = p->pEList; + for(ii=0; iinExpr; ii++){ + char aff; + Select *pSub1; + assert( pList->a[ii].pExpr!=0 ); + aff = sqlite3ExprAffinity(pList->a[ii].pExpr); + for(pSub1=p->pPrior; pSub1; pSub1=pSub1->pPrior){ + assert( pSub1->pEList!=0 ); + assert( pSub1->pEList->nExpr>ii ); + assert( pSub1->pEList->a[ii].pExpr!=0 ); + if( sqlite3ExprAffinity(pSub1->pEList->a[ii].pExpr)!=aff ){ + return 1; + } + } + } + return 0; +} + #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) /* ** This routine attempts to flatten subqueries as a performance optimization. @@ -3201,70 +4267,87 @@ static void substSelect( ** SELECT x+y AS a FROM t1 WHERE z<100 AND a>5 ** ** The code generated for this simplification gives the same result -** but only has to scan the data once. And because indices might +** but only has to scan the data once. And because indices might ** exist on the table t1, a complete scan of the data might be ** avoided. ** -** Flattening is only attempted if all of the following are true: +** Flattening is subject to the following constraints: ** -** (1) The subquery and the outer query do not both use aggregates. +** (**) We no longer attempt to flatten aggregate subqueries. Was: +** The subquery and the outer query cannot both be aggregates. ** -** (2) The subquery is not an aggregate or (2a) the outer query is not a join -** and (2b) the outer query does not use subqueries other than the one -** FROM-clause subquery that is a candidate for flattening. (2b is -** due to ticket [2f7170d73bf9abf80] from 2015-02-09.) +** (**) We no longer attempt to flatten aggregate subqueries. Was: +** (2) If the subquery is an aggregate then +** (2a) the outer query must not be a join and +** (2b) the outer query must not use subqueries +** other than the one FROM-clause subquery that is a candidate +** for flattening. (This is due to ticket [2f7170d73bf9abf80] +** from 2015-02-09.) ** -** (3) The subquery is not the right operand of a left outer join -** (Originally ticket #306. Strengthened by ticket #3300) +** (3) If the subquery is the right operand of a LEFT JOIN then +** (3a) the subquery may not be a join +** (**) Was (3b): "the FROM clause of the subquery may not contain +** a virtual table" +** (**) Was: "The outer query may not have a GROUP BY." This case +** is now managed correctly +** (3d) the outer query may not be DISTINCT. +** See also (26) for restrictions on RIGHT JOIN. ** -** (4) The subquery is not DISTINCT. +** (4) The subquery can not be DISTINCT. ** ** (**) At one point restrictions (4) and (5) defined a subset of DISTINCT -** sub-queries that were excluded from this optimization. Restriction +** sub-queries that were excluded from this optimization. Restriction ** (4) has since been expanded to exclude all DISTINCT subqueries. ** -** (6) The subquery does not use aggregates or the outer query is not -** DISTINCT. +** (**) We no longer attempt to flatten aggregate subqueries. Was: +** If the subquery is aggregate, the outer query may not be DISTINCT. ** -** (7) The subquery has a FROM clause. TODO: For subqueries without -** A FROM clause, consider adding a FROM close with the special +** (7) The subquery must have a FROM clause. TODO: For subqueries without +** A FROM clause, consider adding a FROM clause with the special ** table sqlite_once that consists of a single row containing a ** single NULL. ** -** (8) The subquery does not use LIMIT or the outer query is not a join. +** (8) If the subquery uses LIMIT then the outer query may not be a join. ** -** (9) The subquery does not use LIMIT or the outer query does not use -** aggregates. +** (9) If the subquery uses LIMIT then the outer query may not be aggregate. ** ** (**) Restriction (10) was removed from the code on 2005-02-05 but we -** accidently carried the comment forward until 2014-09-15. Original -** text: "The subquery does not use aggregates or the outer query -** does not use LIMIT." +** accidentally carried the comment forward until 2014-09-15. Original +** constraint: "If the subquery is aggregate then the outer query +** may not use LIMIT." ** -** (11) The subquery and the outer query do not both have ORDER BY clauses. +** (11) The subquery and the outer query may not both have ORDER BY clauses. ** ** (**) Not implemented. Subsumed into restriction (3). Was previously ** a separate restriction deriving from ticket #350. ** -** (13) The subquery and outer query do not both use LIMIT. +** (13) The subquery and outer query may not both use LIMIT. ** -** (14) The subquery does not use OFFSET. +** (14) The subquery may not use OFFSET. ** -** (15) The outer query is not part of a compound select or the -** subquery does not have a LIMIT clause. +** (15) If the outer query is part of a compound select, then the +** subquery may not use LIMIT. ** (See ticket #2339 and ticket [02a8e81d44]). ** -** (16) The outer query is not an aggregate or the subquery does -** not contain ORDER BY. (Ticket #2942) This used to not matter -** until we introduced the group_concat() function. -** -** (17) The sub-query is not a compound select, or it is a UNION ALL -** compound clause made up entirely of non-aggregate queries, and -** the parent query: -** -** * is not itself part of a compound select, -** * is not an aggregate or DISTINCT query, and -** * is not a join +** (16) If the outer query is aggregate, then the subquery may not +** use ORDER BY. (Ticket #2942) This used to not matter +** until we introduced the group_concat() function. +** +** (17) If the subquery is a compound select, then +** (17a) all compound operators must be a UNION ALL, and +** (17b) no terms within the subquery compound may be aggregate +** or DISTINCT, and +** (17c) every term within the subquery compound must have a FROM clause +** (17d) the outer query may not be +** (17d1) aggregate, or +** (17d2) DISTINCT +** (17e) the subquery may not contain window functions, and +** (17f) the subquery must not be the RHS of a LEFT JOIN. +** (17g) either the subquery is the first element of the outer +** query or there are no RIGHT or FULL JOINs in any arm +** of the subquery. (This is a duplicate of condition (27b).) +** (17h) The corresponding result set expressions in all arms of the +** compound must have the same affinity. ** ** The parent and sub-query may contain WHERE clauses. Subject to ** rules (11), (13) and (14), they may also contain ORDER BY, @@ -3280,10 +4363,10 @@ static void substSelect( ** syntax error and return a detailed message. ** ** (18) If the sub-query is a compound select, then all terms of the -** ORDER by clause of the parent must be simple references to -** columns of the sub-query. +** ORDER BY clause of the parent must be copies of a term returned +** by the parent query. ** -** (19) The subquery does not use LIMIT or the outer query does not +** (19) If the subquery uses LIMIT then the outer query may not ** have a WHERE clause. ** ** (20) If the sub-query is a compound select, then it must not use @@ -3292,25 +4375,42 @@ static void substSelect( ** appear as unmodified result columns in the outer query. But we ** have other optimizations in mind to deal with that case. ** -** (21) The subquery does not use LIMIT or the outer query is not +** (21) If the subquery uses LIMIT then the outer query may not be ** DISTINCT. (See ticket [752e1646fc]). ** -** (22) The subquery is not a recursive CTE. +** (22) The subquery may not be a recursive CTE. ** -** (23) The parent is not a recursive CTE, or the sub-query is not a -** compound query. This restriction is because transforming the +** (23) If the outer query is a recursive CTE, then the sub-query may not be +** a compound query. This restriction is because transforming the ** parent to a compound query confuses the code that handles ** recursive queries in multiSelect(). ** -** (24) The subquery is not an aggregate that uses the built-in min() or +** (**) We no longer attempt to flatten aggregate subqueries. Was: +** The subquery may not be an aggregate that uses the built-in min() or ** or max() functions. (Without this restriction, a query like: ** "SELECT x FROM (SELECT max(y), x FROM t1)" would not necessarily ** return the value X for which Y was maximal.) ** +** (25) If either the subquery or the parent query contains a window +** function in the select list or ORDER BY clause, flattening +** is not attempted. +** +** (26) The subquery may not be the right operand of a RIGHT JOIN. +** See also (3) for restrictions on LEFT JOIN. +** +** (27) The subquery may not contain a FULL or RIGHT JOIN unless it +** is the first element of the parent query. Two subcases: +** (27a) the subquery is not a compound query. +** (27b) the subquery is a compound query and the RIGHT JOIN occurs +** in any arm of the compound query. (See also (17g).) +** +** (28) The subquery is not a MATERIALIZED CTE. (This is handled +** in the caller before ever reaching this routine.) +** ** ** In this routine, the "p" parameter is a pointer to the outer query. ** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query -** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates. +** uses aggregates. ** ** If flattening is not attempted, this routine is a no-op and returns 0. ** If flattening is attempted this routine returns 1. @@ -3322,8 +4422,7 @@ static int flattenSubquery( Parse *pParse, /* Parsing context */ Select *p, /* The parent or outer SELECT statement */ int iFrom, /* Index in p->pSrc->a[] of the inner subquery */ - int isAgg, /* True if outer SELECT uses aggregate functions */ - int subqueryIsAgg /* True if the subquery uses aggregate functions */ + int isAgg /* True if outer SELECT uses aggregate functions */ ){ const char *zSavedAuthContext = pParse->zAuthContext; Select *pParent; /* Current UNION ALL term of the other query */ @@ -3331,35 +4430,33 @@ static int flattenSubquery( Select *pSub1; /* Pointer to the rightmost select in sub-query */ SrcList *pSrc; /* The FROM clause of the outer query */ SrcList *pSubSrc; /* The FROM clause of the subquery */ - ExprList *pList; /* The result set of the outer query */ int iParent; /* VDBE cursor number of the pSub result set temp table */ + int iNewParent = -1;/* Replacement table for iParent */ + int isOuterJoin = 0; /* True if pSub is the right side of a LEFT JOIN */ int i; /* Loop counter */ Expr *pWhere; /* The WHERE clause */ - struct SrcList_item *pSubitem; /* The subquery */ + SrcItem *pSubitem; /* The subquery */ sqlite3 *db = pParse->db; + Walker w; /* Walker to persist agginfo data */ + int *aCsrMap = 0; /* Check to see if flattening is permitted. Return 0 if not. */ assert( p!=0 ); - assert( p->pPrior==0 ); /* Unable to flatten compound queries */ + assert( p->pPrior==0 ); if( OptimizationDisabled(db, SQLITE_QueryFlattener) ) return 0; pSrc = p->pSrc; assert( pSrc && iFrom>=0 && iFromnSrc ); pSubitem = &pSrc->a[iFrom]; iParent = pSubitem->iCursor; - pSub = pSubitem->pSelect; + assert( pSubitem->fg.isSubquery ); + pSub = pSubitem->u4.pSubq->pSelect; assert( pSub!=0 ); - if( subqueryIsAgg ){ - if( isAgg ) return 0; /* Restriction (1) */ - if( pSrc->nSrc>1 ) return 0; /* Restriction (2a) */ - if( (p->pWhere && ExprHasProperty(p->pWhere,EP_Subquery)) - || (sqlite3ExprListFlags(p->pEList) & EP_Subquery)!=0 - || (sqlite3ExprListFlags(p->pOrderBy) & EP_Subquery)!=0 - ){ - return 0; /* Restriction (2b) */ - } - } - + +#ifndef SQLITE_OMIT_WINDOWFUNC + if( p->pWin || pSub->pWin ) return 0; /* Restriction (25) */ +#endif + pSubSrc = pSub->pSrc; assert( pSubSrc ); /* Prior to version 3.1.2, when LIMIT and OFFSET had to be simple constants, @@ -3368,18 +4465,15 @@ static int flattenSubquery( ** became arbitrary expressions, we were forced to add restrictions (13) ** and (14). */ if( pSub->pLimit && p->pLimit ) return 0; /* Restriction (13) */ - if( pSub->pOffset ) return 0; /* Restriction (14) */ + if( pSub->pLimit && pSub->pLimit->pRight ) return 0; /* Restriction (14) */ if( (p->selFlags & SF_Compound)!=0 && pSub->pLimit ){ return 0; /* Restriction (15) */ } if( pSubSrc->nSrc==0 ) return 0; /* Restriction (7) */ - if( pSub->selFlags & SF_Distinct ) return 0; /* Restriction (5) */ + if( pSub->selFlags & SF_Distinct ) return 0; /* Restriction (4) */ if( pSub->pLimit && (pSrc->nSrc>1 || isAgg) ){ return 0; /* Restrictions (8)(9) */ } - if( (p->selFlags & SF_Distinct)!=0 && subqueryIsAgg ){ - return 0; /* Restriction (6) */ - } if( p->pOrderBy && pSub->pOrderBy ){ return 0; /* Restriction (11) */ } @@ -3388,19 +4482,14 @@ static int flattenSubquery( if( pSub->pLimit && (p->selFlags & SF_Distinct)!=0 ){ return 0; /* Restriction (21) */ } - testcase( pSub->selFlags & SF_Recursive ); - testcase( pSub->selFlags & SF_MinMaxAgg ); - if( pSub->selFlags & (SF_Recursive|SF_MinMaxAgg) ){ - return 0; /* Restrictions (22) and (24) */ - } - if( (p->selFlags & SF_Recursive) && pSub->pPrior ){ - return 0; /* Restriction (23) */ + if( pSub->selFlags & (SF_Recursive) ){ + return 0; /* Restrictions (22) */ } - /* OBSOLETE COMMENT 1: - ** Restriction 3: If the subquery is a join, make sure the subquery is - ** not used as the right operand of an outer join. Examples of why this - ** is not allowed: + /* + ** If the subquery is the right operand of a LEFT JOIN, then the + ** subquery may not be a join itself (3a). Example of why this is not + ** allowed: ** ** t1 LEFT OUTER JOIN (t2 JOIN t3) ** @@ -3410,67 +4499,88 @@ static int flattenSubquery( ** ** which is not at all the same thing. ** - ** OBSOLETE COMMENT 2: - ** Restriction 12: If the subquery is the right operand of a left outer - ** join, make sure the subquery has no WHERE clause. - ** An examples of why this is not allowed: - ** - ** t1 LEFT OUTER JOIN (SELECT * FROM t2 WHERE t2.x>0) - ** - ** If we flatten the above, we would get - ** - ** (t1 LEFT OUTER JOIN t2) WHERE t2.x>0 - ** - ** But the t2.x>0 test will always fail on a NULL row of t2, which - ** effectively converts the OUTER JOIN into an INNER JOIN. - ** - ** THIS OVERRIDES OBSOLETE COMMENTS 1 AND 2 ABOVE: - ** Ticket #3300 shows that flattening the right term of a LEFT JOIN - ** is fraught with danger. Best to avoid the whole thing. If the - ** subquery is the right term of a LEFT JOIN, then do not flatten. + ** See also tickets #306, #350, and #3300. */ - if( (pSubitem->fg.jointype & JT_OUTER)!=0 ){ - return 0; + if( (pSubitem->fg.jointype & (JT_OUTER|JT_LTORJ))!=0 ){ + if( pSubSrc->nSrc>1 /* (3a) */ + /**** || IsVirtual(pSubSrc->a[0].pSTab) (3b)-omitted */ + || (p->selFlags & SF_Distinct)!=0 /* (3d) */ + || (pSubitem->fg.jointype & JT_RIGHT)!=0 /* (26) */ + ){ + return 0; + } + isOuterJoin = 1; } - /* Restriction 17: If the sub-query is a compound SELECT, then it must + assert( pSubSrc->nSrc>0 ); /* True by restriction (7) */ + if( iFrom>0 && (pSubSrc->a[0].fg.jointype & JT_LTORJ)!=0 ){ + return 0; /* Restriction (27a) */ + } + + /* Condition (28) is blocked by the caller */ + assert( !pSubitem->fg.isCte || pSubitem->u2.pCteUse->eM10d!=M10d_Yes ); + + /* Restriction (17): If the sub-query is a compound SELECT, then it must ** use only the UNION ALL operator. And none of the simple select queries ** that make up the compound SELECT are allowed to be aggregate or distinct ** queries. */ if( pSub->pPrior ){ + int ii; if( pSub->pOrderBy ){ - return 0; /* Restriction 20 */ + return 0; /* Restriction (20) */ } - if( isAgg || (p->selFlags & SF_Distinct)!=0 || pSrc->nSrc!=1 ){ - return 0; + if( isAgg || (p->selFlags & SF_Distinct)!=0 || isOuterJoin>0 ){ + return 0; /* (17d1), (17d2), or (17f) */ } for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){ testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct ); testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate ); assert( pSub->pSrc!=0 ); + assert( (pSub->selFlags & SF_Recursive)==0 ); assert( pSub->pEList->nExpr==pSub1->pEList->nExpr ); - if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0 - || (pSub1->pPrior && pSub1->op!=TK_ALL) - || pSub1->pSrc->nSrc<1 + if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0 /* (17b) */ + || (pSub1->pPrior && pSub1->op!=TK_ALL) /* (17a) */ + || pSub1->pSrc->nSrc<1 /* (17c) */ +#ifndef SQLITE_OMIT_WINDOWFUNC + || pSub1->pWin /* (17e) */ +#endif ){ return 0; } + if( iFrom>0 && (pSub1->pSrc->a[0].fg.jointype & JT_LTORJ)!=0 ){ + /* Without this restriction, the JT_LTORJ flag would end up being + ** omitted on left-hand tables of the right join that is being + ** flattened. */ + return 0; /* Restrictions (17g), (27b) */ + } testcase( pSub1->pSrc->nSrc>1 ); } - /* Restriction 18. */ + /* Restriction (18). */ if( p->pOrderBy ){ - int ii; for(ii=0; iipOrderBy->nExpr; ii++){ if( p->pOrderBy->a[ii].u.x.iOrderByCol==0 ) return 0; } } + + /* Restriction (23) */ + if( (p->selFlags & SF_Recursive) ) return 0; + + /* Restriction (17h) */ + if( compoundHasDifferentAffinities(pSub) ) return 0; + + if( pSrc->nSrc>1 ){ + if( pParse->nSelect>500 ) return 0; + if( OptimizationDisabled(db, SQLITE_FlttnUnionAll) ) return 0; + aCsrMap = sqlite3DbMallocZero(db, ((i64)pParse->nTab+1)*sizeof(int)); + if( aCsrMap ) aCsrMap[0] = pParse->nTab; + } } /***** If we reach this point, flattening is permitted. *****/ - SELECTTRACE(1,pParse,p,("flatten %s.%p from term %d\n", - pSub->zSelName, pSub, iFrom)); + TREETRACE(0x4,pParse,p,("flatten %u.%p from term %d\n", + pSub->selId, pSub, iFrom)); /* Authorize the subquery */ pParse->zAuthContext = pSubitem->zName; @@ -3478,14 +4588,29 @@ static int flattenSubquery( testcase( i==SQLITE_DENY ); pParse->zAuthContext = zSavedAuthContext; + /* Delete the transient structures associated with the subquery */ + + if( ALWAYS(pSubitem->fg.isSubquery) ){ + pSub1 = sqlite3SubqueryDetach(db, pSubitem); + }else{ + pSub1 = 0; + } + assert( pSubitem->fg.isSubquery==0 ); + assert( pSubitem->fg.fixedSchema==0 ); + sqlite3DbFree(db, pSubitem->zName); + sqlite3DbFree(db, pSubitem->zAlias); + pSubitem->zName = 0; + pSubitem->zAlias = 0; + assert( pSubitem->fg.isUsing!=0 || pSubitem->u3.pOn==0 ); + /* If the sub-query is a compound SELECT statement, then (by restrictions - ** 17 and 18 above) it must be a UNION ALL and the parent query must + ** 17 and 18 above) it must be a UNION ALL and the parent query must ** be of the form: ** - ** SELECT FROM () + ** SELECT FROM () ** ** followed by any ORDER BY, LIMIT and/or OFFSET clauses. This block - ** creates N-1 copies of the parent query without any ORDER BY, LIMIT or + ** creates N-1 copies of the parent query without any ORDER BY, LIMIT or ** OFFSET clauses and joins them to the left-hand-side of the original ** using UNION ALL operators. In this case N is the number of simple ** select statements in the compound sub-query. @@ -3515,67 +4640,59 @@ static int flattenSubquery( Select *pNew; ExprList *pOrderBy = p->pOrderBy; Expr *pLimit = p->pLimit; - Expr *pOffset = p->pOffset; Select *pPrior = p->pPrior; + Table *pItemTab = pSubitem->pSTab; + pSubitem->pSTab = 0; p->pOrderBy = 0; - p->pSrc = 0; p->pPrior = 0; p->pLimit = 0; - p->pOffset = 0; pNew = sqlite3SelectDup(db, p, 0); - sqlite3SelectSetName(pNew, pSub->zSelName); - p->pOffset = pOffset; p->pLimit = pLimit; p->pOrderBy = pOrderBy; - p->pSrc = pSrc; p->op = TK_ALL; + pSubitem->pSTab = pItemTab; if( pNew==0 ){ p->pPrior = pPrior; }else{ + pNew->selId = ++pParse->nSelect; + if( aCsrMap && ALWAYS(db->mallocFailed==0) ){ + renumberCursors(pParse, pNew, iFrom, aCsrMap); + } pNew->pPrior = pPrior; if( pPrior ) pPrior->pNext = pNew; pNew->pNext = p; p->pPrior = pNew; - SELECTTRACE(2,pParse,p, - ("compound-subquery flattener creates %s.%p as peer\n", - pNew->zSelName, pNew)); + TREETRACE(0x4,pParse,p,("compound-subquery flattener" + " creates %u as peer\n",pNew->selId)); } - if( db->mallocFailed ) return 1; + assert( pSubitem->fg.isSubquery==0 ); + } + sqlite3DbFree(db, aCsrMap); + if( db->mallocFailed ){ + assert( pSubitem->fg.fixedSchema==0 ); + assert( pSubitem->fg.isSubquery==0 ); + assert( pSubitem->u4.zDatabase==0 ); + sqlite3SrcItemAttachSubquery(pParse, pSubitem, pSub1, 0); + return 1; } - - /* Begin flattening the iFrom-th entry of the FROM clause - ** in the outer query. - */ - pSub = pSub1 = pSubitem->pSelect; - - /* Delete the transient table structure associated with the - ** subquery - */ - sqlite3DbFree(db, pSubitem->zDatabase); - sqlite3DbFree(db, pSubitem->zName); - sqlite3DbFree(db, pSubitem->zAlias); - pSubitem->zDatabase = 0; - pSubitem->zName = 0; - pSubitem->zAlias = 0; - pSubitem->pSelect = 0; /* Defer deleting the Table object associated with the ** subquery until code generation is ** complete, since there may still exist Expr.pTab entries that ** refer to the subquery even after flattening. Ticket #3346. ** - ** pSubitem->pTab is always non-NULL by test restrictions and tests above. + ** pSubitem->pSTab is always non-NULL by test restrictions and tests above. */ - if( ALWAYS(pSubitem->pTab!=0) ){ - Table *pTabToDel = pSubitem->pTab; - if( pTabToDel->nRef==1 ){ + if( ALWAYS(pSubitem->pSTab!=0) ){ + Table *pTabToDel = pSubitem->pSTab; + if( pTabToDel->nTabRef==1 ){ Parse *pToplevel = sqlite3ParseToplevel(pParse); - pTabToDel->pNextZombie = pToplevel->pZombieTab; - pToplevel->pZombieTab = pTabToDel; + sqlite3ParserAddCleanup(pToplevel, sqlite3DeleteTableGeneric, pTabToDel); + testcase( pToplevel->earlyCleanup ); }else{ - pTabToDel->nRef--; + pTabToDel->nTabRef--; } - pSubitem->pTab = 0; + pSubitem->pSTab = 0; } /* The following loop runs once for each term in a compound-subquery @@ -3591,25 +4708,15 @@ static int flattenSubquery( ** those references with expressions that resolve to the subquery FROM ** elements we are now copying in. */ + pSub = pSub1; for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){ int nSubSrc; - u8 jointype = 0; + u8 jointype = pSubitem->fg.jointype; + assert( pSub!=0 ); pSubSrc = pSub->pSrc; /* FROM clause of subquery */ nSubSrc = pSubSrc->nSrc; /* Number of terms in subquery FROM clause */ pSrc = pParent->pSrc; /* FROM clause of the outer query */ - if( pSrc ){ - assert( pParent==p ); /* First time through the loop */ - jointype = pSubitem->fg.jointype; - }else{ - assert( pParent!=p ); /* 2nd and subsequent times through the loop */ - pSrc = pParent->pSrc = sqlite3SrcListAppend(db, 0, 0, 0); - if( pSrc==0 ){ - assert( db->mallocFailed ); - break; - } - } - /* The subquery uses a single slot of the FROM clause of the outer ** query. If the subquery has more than one element in its FROM clause, ** then expand the outer query to make space for it to hold all elements @@ -3626,26 +4733,32 @@ static int flattenSubquery( ** for the two elements in the FROM clause of the subquery. */ if( nSubSrc>1 ){ - pParent->pSrc = pSrc = sqlite3SrcListEnlarge(db, pSrc, nSubSrc-1,iFrom+1); - if( db->mallocFailed ){ - break; - } + pSrc = sqlite3SrcListEnlarge(pParse, pSrc, nSubSrc-1,iFrom+1); + if( pSrc==0 ) break; + pParent->pSrc = pSrc; + pSubitem = &pSrc->a[iFrom]; } /* Transfer the FROM clause terms from the subquery into the ** outer query. */ + iNewParent = pSubSrc->a[0].iCursor; for(i=0; ia[i+iFrom].pUsing); - assert( pSrc->a[i+iFrom].fg.isTabFunc==0 ); - pSrc->a[i+iFrom] = pSubSrc->a[i]; + SrcItem *pItem = &pSrc->a[i+iFrom]; + assert( pItem->fg.isTabFunc==0 ); + assert( pItem->fg.isSubquery + || pItem->fg.fixedSchema + || pItem->u4.zDatabase==0 ); + if( pItem->fg.isUsing ) sqlite3IdListDelete(db, pItem->u3.pUsing); + *pItem = pSubSrc->a[i]; + pItem->fg.jointype |= (jointype & JT_LTORJ); memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i])); } - pSrc->a[iFrom].fg.jointype = jointype; - - /* Now begin substituting subquery result set expressions for + pSubitem->fg.jointype |= jointype; + + /* Now begin substituting subquery result set expressions for ** references to the iParent in the outer query. - ** + ** ** Example: ** ** SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b; @@ -3655,20 +4768,12 @@ static int flattenSubquery( ** We look at every expression in the outer query and every place we see ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10". */ - pList = pParent->pEList; - for(i=0; inExpr; i++){ - if( pList->a[i].zName==0 ){ - char *zName = sqlite3DbStrDup(db, pList->a[i].zSpan); - sqlite3Dequote(zName); - pList->a[i].zName = zName; - } - } - if( pSub->pOrderBy ){ + if( pSub->pOrderBy && (pParent->selFlags & SF_NoopOrderBy)==0 ){ /* At this point, any non-zero iOrderByCol values indicate that the ** ORDER BY column expression is identical to the iOrderByCol'th ** expression returned by SELECT statement pSub. Since these values ** do not necessarily correspond to columns in SELECT statement pParent, - ** zero them before transfering the ORDER BY clause. + ** zero them before transferring the ORDER BY clause. ** ** Not doing this may cause an error if a subsequent call to this ** function attempts to flatten a compound sub-query into pParent @@ -3679,29 +4784,39 @@ static int flattenSubquery( pOrderBy->a[i].u.x.iOrderByCol = 0; } assert( pParent->pOrderBy==0 ); - assert( pSub->pPrior==0 ); pParent->pOrderBy = pOrderBy; pSub->pOrderBy = 0; } - pWhere = sqlite3ExprDup(db, pSub->pWhere, 0); - if( subqueryIsAgg ){ - assert( pParent->pHaving==0 ); - pParent->pHaving = pParent->pWhere; - pParent->pWhere = pWhere; - pParent->pHaving = sqlite3ExprAnd(db, pParent->pHaving, - sqlite3ExprDup(db, pSub->pHaving, 0)); - assert( pParent->pGroupBy==0 ); - pParent->pGroupBy = sqlite3ExprListDup(db, pSub->pGroupBy, 0); - }else{ - pParent->pWhere = sqlite3ExprAnd(db, pParent->pWhere, pWhere); + pWhere = pSub->pWhere; + pSub->pWhere = 0; + if( isOuterJoin>0 ){ + assert( pSubSrc->nSrc==1 ); + sqlite3SetJoinExpr(pWhere, iNewParent, EP_OuterON); } - substSelect(db, pParent, iParent, pSub->pEList, 0); - - /* The flattened query is distinct if either the inner or the - ** outer query is distinct. - */ - pParent->selFlags |= pSub->selFlags & SF_Distinct; - + if( pWhere ){ + if( pParent->pWhere ){ + pParent->pWhere = sqlite3PExpr(pParse, TK_AND, pWhere, pParent->pWhere); + }else{ + pParent->pWhere = pWhere; + } + } + if( db->mallocFailed==0 ){ + SubstContext x; + x.pParse = pParse; + x.iTable = iParent; + x.iNewTable = iNewParent; + x.isOuterJoin = isOuterJoin; + x.nSelDepth = 0; + x.pEList = pSub->pEList; + x.pCList = findLeftmostExprlist(pSub); + substSelect(&x, pParent, 0); + } + + /* The flattened query is a compound if either the inner or the + ** outer query is a compound. */ + pParent->selFlags |= pSub->selFlags & SF_Compound; + assert( (pSub->selFlags & SF_Distinct)==0 ); /* restriction (17b) */ + /* ** SELECT ... FROM (SELECT ... LIMIT a OFFSET b) LIMIT x OFFSET y; ** @@ -3712,16 +4827,23 @@ static int flattenSubquery( pParent->pLimit = pSub->pLimit; pSub->pLimit = 0; } + + /* Recompute the SrcItem.colUsed masks for the flattened + ** tables. */ + for(i=0; ia[i+iFrom]); + } } - /* Finially, delete what is left of the subquery and return - ** success. + /* Finally, delete what is left of the subquery and return success. */ + sqlite3AggInfoPersistWalkerInit(&w, pParse); + sqlite3WalkSelect(&w,pSub1); sqlite3SelectDelete(db, pSub1); -#if SELECTTRACE_ENABLED - if( sqlite3SelectTrace & 0x100 ){ - SELECTTRACE(0x100,pParse,p,("After flattening:\n")); +#if TREETRACE_ENABLED + if( sqlite3TreeTrace & 0x4 ){ + TREETRACE(0x4,pParse,p,("After flattening:\n")); sqlite3TreeViewSelect(0, p, 0); } #endif @@ -3730,148 +4852,741 @@ static int flattenSubquery( } #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */ +/* +** A structure to keep track of all of the column values that are fixed to +** a known value due to WHERE clause constraints of the form COLUMN=VALUE. +*/ +typedef struct WhereConst WhereConst; +struct WhereConst { + Parse *pParse; /* Parsing context */ + u8 *pOomFault; /* Pointer to pParse->db->mallocFailed */ + int nConst; /* Number for COLUMN=CONSTANT terms */ + int nChng; /* Number of times a constant is propagated */ + int bHasAffBlob; /* At least one column in apExpr[] as affinity BLOB */ + u32 mExcludeOn; /* Which ON expressions to exclude from considertion. + ** Either EP_OuterON or EP_InnerON|EP_OuterON */ + Expr **apExpr; /* [i*2] is COLUMN and [i*2+1] is VALUE */ +}; - -#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) /* -** Make copies of relevant WHERE clause terms of the outer query into -** the WHERE clause of subquery. Example: -** -** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1) WHERE x=5 AND y=10; -** -** Transformed into: +** Add a new entry to the pConst object. Except, do not add duplicate +** pColumn entries. Also, do not add if doing so would not be appropriate. ** -** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1 WHERE a=5 AND c-d=10) -** WHERE x=5 AND y=10; -** -** The hope is that the terms added to the inner query will make it more -** efficient. -** -** Do not attempt this optimization if: -** -** (1) The inner query is an aggregate. (In that case, we'd really want -** to copy the outer WHERE-clause terms onto the HAVING clause of the -** inner query. But they probably won't help there so do not bother.) -** -** (2) The inner query is the recursive part of a common table expression. -** -** (3) The inner query has a LIMIT clause (since the changes to the WHERE -** close would change the meaning of the LIMIT). -** -** (4) The inner query is the right operand of a LEFT JOIN. (The caller -** enforces this restriction since this routine does not have enough -** information to know.) -** -** (5) The WHERE clause expression originates in the ON or USING clause -** of a LEFT JOIN. -** -** Return 0 if no changes are made and non-zero if one or more WHERE clause -** terms are duplicated into the subquery. +** The caller guarantees the pColumn is a column and pValue is a constant. +** This routine has to do some additional checks before completing the +** insert. */ -static int pushDownWhereTerms( - sqlite3 *db, /* The database connection (for malloc()) */ - Select *pSubq, /* The subquery whose WHERE clause is to be augmented */ - Expr *pWhere, /* The WHERE clause of the outer query */ - int iCursor /* Cursor number of the subquery */ +static void constInsert( + WhereConst *pConst, /* The WhereConst into which we are inserting */ + Expr *pColumn, /* The COLUMN part of the constraint */ + Expr *pValue, /* The VALUE part of the constraint */ + Expr *pExpr /* Overall expression: COLUMN=VALUE or VALUE=COLUMN */ ){ - Expr *pNew; - int nChng = 0; - if( pWhere==0 ) return 0; - if( (pSubq->selFlags & (SF_Aggregate|SF_Recursive))!=0 ){ - return 0; /* restrictions (1) and (2) */ + int i; + assert( pColumn->op==TK_COLUMN ); + assert( sqlite3ExprIsConstant(pConst->pParse, pValue) ); + + if( ExprHasProperty(pColumn, EP_FixedCol) ) return; + if( sqlite3ExprAffinity(pValue)!=0 ) return; + if( !sqlite3IsBinary(sqlite3ExprCompareCollSeq(pConst->pParse,pExpr)) ){ + return; } - if( pSubq->pLimit!=0 ){ - return 0; /* restriction (3) */ + + /* 2018-10-25 ticket [cf5ed20f] + ** Make sure the same pColumn is not inserted more than once */ + for(i=0; inConst; i++){ + const Expr *pE2 = pConst->apExpr[i*2]; + assert( pE2->op==TK_COLUMN ); + if( pE2->iTable==pColumn->iTable + && pE2->iColumn==pColumn->iColumn + ){ + return; /* Already present. Return without doing anything. */ + } } - while( pWhere->op==TK_AND ){ - nChng += pushDownWhereTerms(db, pSubq, pWhere->pRight, iCursor); - pWhere = pWhere->pLeft; + assert( SQLITE_AFF_NONEbHasAffBlob = 1; } - if( ExprHasProperty(pWhere,EP_FromJoin) ) return 0; /* restriction 5 */ - if( sqlite3ExprIsTableConstant(pWhere, iCursor) ){ - nChng++; - while( pSubq ){ - pNew = sqlite3ExprDup(db, pWhere, 0); - pNew = substExpr(db, pNew, iCursor, pSubq->pEList); - pSubq->pWhere = sqlite3ExprAnd(db, pSubq->pWhere, pNew); - pSubq = pSubq->pPrior; - } + + pConst->nConst++; + pConst->apExpr = sqlite3DbReallocOrFree(pConst->pParse->db, pConst->apExpr, + pConst->nConst*2*sizeof(Expr*)); + if( pConst->apExpr==0 ){ + pConst->nConst = 0; + }else{ + pConst->apExpr[pConst->nConst*2-2] = pColumn; + pConst->apExpr[pConst->nConst*2-1] = pValue; } - return nChng; } -#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */ /* -** Based on the contents of the AggInfo structure indicated by the first -** argument, this function checks if the following are true: -** -** * the query contains just a single aggregate function, -** * the aggregate function is either min() or max(), and -** * the argument to the aggregate function is a column value. -** -** If all of the above are true, then WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX -** is returned as appropriate. Also, *ppMinMax is set to point to the -** list of arguments passed to the aggregate before returning. -** -** Or, if the conditions above are not met, *ppMinMax is set to 0 and -** WHERE_ORDERBY_NORMAL is returned. +** Find all terms of COLUMN=VALUE or VALUE=COLUMN in pExpr where VALUE +** is a constant expression and where the term must be true because it +** is part of the AND-connected terms of the expression. For each term +** found, add it to the pConst structure. */ -static u8 minMaxQuery(AggInfo *pAggInfo, ExprList **ppMinMax){ - int eRet = WHERE_ORDERBY_NORMAL; /* Return value */ - - *ppMinMax = 0; - if( pAggInfo->nFunc==1 ){ - Expr *pExpr = pAggInfo->aFunc[0].pExpr; /* Aggregate function */ - ExprList *pEList = pExpr->x.pList; /* Arguments to agg function */ - - assert( pExpr->op==TK_AGG_FUNCTION ); - if( pEList && pEList->nExpr==1 && pEList->a[0].pExpr->op==TK_AGG_COLUMN ){ - const char *zFunc = pExpr->u.zToken; - if( sqlite3StrICmp(zFunc, "min")==0 ){ - eRet = WHERE_ORDERBY_MIN; - *ppMinMax = pEList; - }else if( sqlite3StrICmp(zFunc, "max")==0 ){ - eRet = WHERE_ORDERBY_MAX; - *ppMinMax = pEList; - } - } +static void findConstInWhere(WhereConst *pConst, Expr *pExpr){ + Expr *pRight, *pLeft; + if( NEVER(pExpr==0) ) return; + if( ExprHasProperty(pExpr, pConst->mExcludeOn) ){ + testcase( ExprHasProperty(pExpr, EP_OuterON) ); + testcase( ExprHasProperty(pExpr, EP_InnerON) ); + return; + } + if( pExpr->op==TK_AND ){ + findConstInWhere(pConst, pExpr->pRight); + findConstInWhere(pConst, pExpr->pLeft); + return; + } + if( pExpr->op!=TK_EQ ) return; + pRight = pExpr->pRight; + pLeft = pExpr->pLeft; + assert( pRight!=0 ); + assert( pLeft!=0 ); + if( pRight->op==TK_COLUMN && sqlite3ExprIsConstant(pConst->pParse, pLeft) ){ + constInsert(pConst,pRight,pLeft,pExpr); + } + if( pLeft->op==TK_COLUMN && sqlite3ExprIsConstant(pConst->pParse, pRight) ){ + constInsert(pConst,pLeft,pRight,pExpr); } - - assert( *ppMinMax==0 || (*ppMinMax)->nExpr==1 ); - return eRet; } /* -** The select statement passed as the first argument is an aggregate query. -** The second argument is the associated aggregate-info object. This -** function tests if the SELECT is of the form: -** -** SELECT count(*) FROM +** This is a helper function for Walker callback propagateConstantExprRewrite(). ** -** where table is a database table, not a sub-select or view. If the query -** does match this pattern, then a pointer to the Table object representing -** is returned. Otherwise, 0 is returned. +** Argument pExpr is a candidate expression to be replaced by a value. If +** pExpr is equivalent to one of the columns named in pWalker->u.pConst, +** then overwrite it with the corresponding value. Except, do not do so +** if argument bIgnoreAffBlob is non-zero and the affinity of pExpr +** is SQLITE_AFF_BLOB. */ -static Table *isSimpleCount(Select *p, AggInfo *pAggInfo){ - Table *pTab; - Expr *pExpr; +static int propagateConstantExprRewriteOne( + WhereConst *pConst, + Expr *pExpr, + int bIgnoreAffBlob +){ + int i; + if( pConst->pOomFault[0] ) return WRC_Prune; + if( pExpr->op!=TK_COLUMN ) return WRC_Continue; + if( ExprHasProperty(pExpr, EP_FixedCol|pConst->mExcludeOn) ){ + testcase( ExprHasProperty(pExpr, EP_FixedCol) ); + testcase( ExprHasProperty(pExpr, EP_OuterON) ); + testcase( ExprHasProperty(pExpr, EP_InnerON) ); + return WRC_Continue; + } + for(i=0; inConst; i++){ + Expr *pColumn = pConst->apExpr[i*2]; + if( pColumn==pExpr ) continue; + if( pColumn->iTable!=pExpr->iTable ) continue; + if( pColumn->iColumn!=pExpr->iColumn ) continue; + assert( SQLITE_AFF_NONEnChng++; + ExprClearProperty(pExpr, EP_Leaf); + ExprSetProperty(pExpr, EP_FixedCol); + assert( pExpr->pLeft==0 ); + pExpr->pLeft = sqlite3ExprDup(pConst->pParse->db, pConst->apExpr[i*2+1], 0); + if( pConst->pParse->db->mallocFailed ) return WRC_Prune; + break; + } + return WRC_Prune; +} + +/* +** This is a Walker expression callback. pExpr is a node from the WHERE +** clause of a SELECT statement. This function examines pExpr to see if +** any substitutions based on the contents of pWalker->u.pConst should +** be made to pExpr or its immediate children. +** +** A substitution is made if: +** +** + pExpr is a column with an affinity other than BLOB that matches +** one of the columns in pWalker->u.pConst, or +** +** + pExpr is a binary comparison operator (=, <=, >=, <, >) that +** uses an affinity other than TEXT and one of its immediate +** children is a column that matches one of the columns in +** pWalker->u.pConst. +*/ +static int propagateConstantExprRewrite(Walker *pWalker, Expr *pExpr){ + WhereConst *pConst = pWalker->u.pConst; + assert( TK_GT==TK_EQ+1 ); + assert( TK_LE==TK_EQ+2 ); + assert( TK_LT==TK_EQ+3 ); + assert( TK_GE==TK_EQ+4 ); + if( pConst->bHasAffBlob ){ + if( (pExpr->op>=TK_EQ && pExpr->op<=TK_GE) + || pExpr->op==TK_IS + ){ + propagateConstantExprRewriteOne(pConst, pExpr->pLeft, 0); + if( pConst->pOomFault[0] ) return WRC_Prune; + if( sqlite3ExprAffinity(pExpr->pLeft)!=SQLITE_AFF_TEXT ){ + propagateConstantExprRewriteOne(pConst, pExpr->pRight, 0); + } + } + } + return propagateConstantExprRewriteOne(pConst, pExpr, pConst->bHasAffBlob); +} + +/* +** The WHERE-clause constant propagation optimization. +** +** If the WHERE clause contains terms of the form COLUMN=CONSTANT or +** CONSTANT=COLUMN that are top-level AND-connected terms that are not +** part of a ON clause from a LEFT JOIN, then throughout the query +** replace all other occurrences of COLUMN with CONSTANT. +** +** For example, the query: +** +** SELECT * FROM t1, t2, t3 WHERE t1.a=39 AND t2.b=t1.a AND t3.c=t2.b +** +** Is transformed into +** +** SELECT * FROM t1, t2, t3 WHERE t1.a=39 AND t2.b=39 AND t3.c=39 +** +** Return true if any transformations where made and false if not. +** +** Implementation note: Constant propagation is tricky due to affinity +** and collating sequence interactions. Consider this example: +** +** CREATE TABLE t1(a INT,b TEXT); +** INSERT INTO t1 VALUES(123,'0123'); +** SELECT * FROM t1 WHERE a=123 AND b=a; +** SELECT * FROM t1 WHERE a=123 AND b=123; +** +** The two SELECT statements above should return different answers. b=a +** is always true because the comparison uses numeric affinity, but b=123 +** is false because it uses text affinity and '0123' is not the same as '123'. +** To work around this, the expression tree is not actually changed from +** "b=a" to "b=123" but rather the "a" in "b=a" is tagged with EP_FixedCol +** and the "123" value is hung off of the pLeft pointer. Code generator +** routines know to generate the constant "123" instead of looking up the +** column value. Also, to avoid collation problems, this optimization is +** only attempted if the "a=123" term uses the default BINARY collation. +** +** 2021-05-25 forum post 6a06202608: Another troublesome case is... +** +** CREATE TABLE t1(x); +** INSERT INTO t1 VALUES(10.0); +** SELECT 1 FROM t1 WHERE x=10 AND x LIKE 10; +** +** The query should return no rows, because the t1.x value is '10.0' not '10' +** and '10.0' is not LIKE '10'. But if we are not careful, the first WHERE +** term "x=10" will cause the second WHERE term to become "10 LIKE 10", +** resulting in a false positive. To avoid this, constant propagation for +** columns with BLOB affinity is only allowed if the constant is used with +** operators ==, <=, <, >=, >, or IS in a way that will cause the correct +** type conversions to occur. See logic associated with the bHasAffBlob flag +** for details. +*/ +static int propagateConstants( + Parse *pParse, /* The parsing context */ + Select *p /* The query in which to propagate constants */ +){ + WhereConst x; + Walker w; + int nChng = 0; + x.pParse = pParse; + x.pOomFault = &pParse->db->mallocFailed; + do{ + x.nConst = 0; + x.nChng = 0; + x.apExpr = 0; + x.bHasAffBlob = 0; + if( ALWAYS(p->pSrc!=0) + && p->pSrc->nSrc>0 + && (p->pSrc->a[0].fg.jointype & JT_LTORJ)!=0 + ){ + /* Do not propagate constants on any ON clause if there is a + ** RIGHT JOIN anywhere in the query */ + x.mExcludeOn = EP_InnerON | EP_OuterON; + }else{ + /* Do not propagate constants through the ON clause of a LEFT JOIN */ + x.mExcludeOn = EP_OuterON; + } + findConstInWhere(&x, p->pWhere); + if( x.nConst ){ + memset(&w, 0, sizeof(w)); + w.pParse = pParse; + w.xExprCallback = propagateConstantExprRewrite; + w.xSelectCallback = sqlite3SelectWalkNoop; + w.xSelectCallback2 = 0; + w.walkerDepth = 0; + w.u.pConst = &x; + sqlite3WalkExpr(&w, p->pWhere); + sqlite3DbFree(x.pParse->db, x.apExpr); + nChng += x.nChng; + } + }while( x.nChng ); + return nChng; +} + +#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) +# if !defined(SQLITE_OMIT_WINDOWFUNC) +/* +** This function is called to determine whether or not it is safe to +** push WHERE clause expression pExpr down to FROM clause sub-query +** pSubq, which contains at least one window function. Return 1 +** if it is safe and the expression should be pushed down, or 0 +** otherwise. +** +** It is only safe to push the expression down if it consists only +** of constants and copies of expressions that appear in the PARTITION +** BY clause of all window function used by the sub-query. It is safe +** to filter out entire partitions, but not rows within partitions, as +** this may change the results of the window functions. +** +** At the time this function is called it is guaranteed that +** +** * the sub-query uses only one distinct window frame, and +** * that the window frame has a PARTITION BY clause. +*/ +static int pushDownWindowCheck(Parse *pParse, Select *pSubq, Expr *pExpr){ + assert( pSubq->pWin->pPartition ); + assert( (pSubq->selFlags & SF_MultiPart)==0 ); + assert( pSubq->pPrior==0 ); + return sqlite3ExprIsConstantOrGroupBy(pParse, pExpr, pSubq->pWin->pPartition); +} +# endif /* SQLITE_OMIT_WINDOWFUNC */ +#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */ + +#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) +/* +** Make copies of relevant WHERE clause terms of the outer query into +** the WHERE clause of subquery. Example: +** +** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1) WHERE x=5 AND y=10; +** +** Transformed into: +** +** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1 WHERE a=5 AND c-d=10) +** WHERE x=5 AND y=10; +** +** The hope is that the terms added to the inner query will make it more +** efficient. +** +** NAME AMBIGUITY +** +** This optimization is called the "WHERE-clause push-down optimization" +** or sometimes the "predicate push-down optimization". +** +** Do not confuse this optimization with another unrelated optimization +** with a similar name: The "MySQL push-down optimization" causes WHERE +** clause terms that can be evaluated using only the index and without +** reference to the table are run first, so that if they are false, +** unnecessary table seeks are avoided. +** +** RULES +** +** Do not attempt this optimization if: +** +** (1) (** This restriction was removed on 2017-09-29. We used to +** disallow this optimization for aggregate subqueries, but now +** it is allowed by putting the extra terms on the HAVING clause. +** The added HAVING clause is pointless if the subquery lacks +** a GROUP BY clause. But such a HAVING clause is also harmless +** so there does not appear to be any reason to add extra logic +** to suppress it. **) +** +** (2) The inner query is the recursive part of a common table expression. +** +** (3) The inner query has a LIMIT clause (since the changes to the WHERE +** clause would change the meaning of the LIMIT). +** +** (4) The inner query is the right operand of a LEFT JOIN and the +** expression to be pushed down does not come from the ON clause +** on that LEFT JOIN. +** +** (5) The WHERE clause expression originates in the ON or USING clause +** of a LEFT JOIN where iCursor is not the right-hand table of that +** left join. An example: +** +** SELECT * +** FROM (SELECT 1 AS a1 UNION ALL SELECT 2) AS aa +** JOIN (SELECT 1 AS b2 UNION ALL SELECT 2) AS bb ON (a1=b2) +** LEFT JOIN (SELECT 8 AS c3 UNION ALL SELECT 9) AS cc ON (b2=2); +** +** The correct answer is three rows: (1,1,NULL),(2,2,8),(2,2,9). +** But if the (b2=2) term were to be pushed down into the bb subquery, +** then the (1,1,NULL) row would be suppressed. +** +** (6) Window functions make things tricky as changes to the WHERE clause +** of the inner query could change the window over which window +** functions are calculated. Therefore, do not attempt the optimization +** if: +** +** (6a) The inner query uses multiple incompatible window partitions. +** +** (6b) The inner query is a compound and uses window-functions. +** +** (6c) The WHERE clause does not consist entirely of constants and +** copies of expressions found in the PARTITION BY clause of +** all window-functions used by the sub-query. It is safe to +** filter out entire partitions, as this does not change the +** window over which any window-function is calculated. +** +** (7) The inner query is a Common Table Expression (CTE) that should +** be materialized. (This restriction is implemented in the calling +** routine.) +** +** (8) If the subquery is a compound that uses UNION, INTERSECT, +** or EXCEPT, then all of the result set columns for all arms of +** the compound must use the BINARY collating sequence. +** +** (9) All three of the following are true: +** +** (9a) The WHERE clause expression originates in the ON or USING clause +** of a join (either an INNER or an OUTER join), and +** +** (9b) The subquery is to the right of the ON/USING clause +** +** (9c) There is a RIGHT JOIN (or FULL JOIN) in between the ON/USING +** clause and the subquery. +** +** Without this restriction, the WHERE-clause push-down optimization +** might move the ON/USING filter expression from the left side of a +** RIGHT JOIN over to the right side, which leads to incorrect answers. +** See also restriction (6) in sqlite3ExprIsSingleTableConstraint(). +** +** (10) The inner query is not the right-hand table of a RIGHT JOIN. +** +** (11) The subquery is not a VALUES clause +** +** (12) The WHERE clause is not "rowid ISNULL" or the equivalent. This +** case only comes up if SQLite is compiled using +** SQLITE_ALLOW_ROWID_IN_VIEW. +** +** Return 0 if no changes are made and non-zero if one or more WHERE clause +** terms are duplicated into the subquery. +*/ +static int pushDownWhereTerms( + Parse *pParse, /* Parse context (for malloc() and error reporting) */ + Select *pSubq, /* The subquery whose WHERE clause is to be augmented */ + Expr *pWhere, /* The WHERE clause of the outer query */ + SrcList *pSrcList, /* The complete from clause of the outer query */ + int iSrc /* Which FROM clause term to try to push into */ +){ + Expr *pNew; + SrcItem *pSrc; /* The subquery FROM term into which WHERE is pushed */ + int nChng = 0; + pSrc = &pSrcList->a[iSrc]; + if( pWhere==0 ) return 0; + if( pSubq->selFlags & (SF_Recursive|SF_MultiPart) ){ + return 0; /* restrictions (2) and (11) */ + } + if( pSrc->fg.jointype & (JT_LTORJ|JT_RIGHT) ){ + return 0; /* restrictions (10) */ + } + + if( pSubq->pPrior ){ + Select *pSel; + int notUnionAll = 0; + for(pSel=pSubq; pSel; pSel=pSel->pPrior){ + u8 op = pSel->op; + assert( op==TK_ALL || op==TK_SELECT + || op==TK_UNION || op==TK_INTERSECT || op==TK_EXCEPT ); + if( op!=TK_ALL && op!=TK_SELECT ){ + notUnionAll = 1; + } +#ifndef SQLITE_OMIT_WINDOWFUNC + if( pSel->pWin ) return 0; /* restriction (6b) */ +#endif + } + if( notUnionAll ){ + /* If any of the compound arms are connected using UNION, INTERSECT, + ** or EXCEPT, then we must ensure that none of the columns use a + ** non-BINARY collating sequence. */ + for(pSel=pSubq; pSel; pSel=pSel->pPrior){ + int ii; + const ExprList *pList = pSel->pEList; + assert( pList!=0 ); + for(ii=0; iinExpr; ii++){ + CollSeq *pColl = sqlite3ExprCollSeq(pParse, pList->a[ii].pExpr); + if( !sqlite3IsBinary(pColl) ){ + return 0; /* Restriction (8) */ + } + } + } + } + }else{ +#ifndef SQLITE_OMIT_WINDOWFUNC + if( pSubq->pWin && pSubq->pWin->pPartition==0 ) return 0; +#endif + } + +#ifdef SQLITE_DEBUG + /* Only the first term of a compound can have a WITH clause. But make + ** sure no other terms are marked SF_Recursive in case something changes + ** in the future. + */ + { + Select *pX; + for(pX=pSubq; pX; pX=pX->pPrior){ + assert( (pX->selFlags & (SF_Recursive))==0 ); + } + } +#endif + + if( pSubq->pLimit!=0 ){ + return 0; /* restriction (3) */ + } + while( pWhere->op==TK_AND ){ + nChng += pushDownWhereTerms(pParse, pSubq, pWhere->pRight, pSrcList, iSrc); + pWhere = pWhere->pLeft; + } + +#if 0 /* These checks now done by sqlite3ExprIsSingleTableConstraint() */ + if( ExprHasProperty(pWhere, EP_OuterON|EP_InnerON) /* (9a) */ + && (pSrcList->a[0].fg.jointype & JT_LTORJ)!=0 /* Fast pre-test of (9c) */ + ){ + int jj; + for(jj=0; jjw.iJoin==pSrcList->a[jj].iCursor ){ + /* If we reach this point, both (9a) and (9b) are satisfied. + ** The following loop checks (9c): + */ + for(jj++; jja[jj].fg.jointype & JT_RIGHT)!=0 ){ + return 0; /* restriction (9) */ + } + } + } + } + } + if( isLeftJoin + && (ExprHasProperty(pWhere,EP_OuterON)==0 + || pWhere->w.iJoin!=iCursor) + ){ + return 0; /* restriction (4) */ + } + if( ExprHasProperty(pWhere,EP_OuterON) + && pWhere->w.iJoin!=iCursor + ){ + return 0; /* restriction (5) */ + } +#endif + +#ifdef SQLITE_ALLOW_ROWID_IN_VIEW + if( ViewCanHaveRowid && (pWhere->op==TK_ISNULL || pWhere->op==TK_NOTNULL) ){ + Expr *pLeft = pWhere->pLeft; + if( ALWAYS(pLeft) + && pLeft->op==TK_COLUMN + && pLeft->iColumn < 0 + ){ + return 0; /* Restriction (12) */ + } + } +#endif + + if( sqlite3ExprIsSingleTableConstraint(pWhere, pSrcList, iSrc, 1) ){ + nChng++; + pSubq->selFlags |= SF_PushDown; + while( pSubq ){ + SubstContext x; + pNew = sqlite3ExprDup(pParse->db, pWhere, 0); + unsetJoinExpr(pNew, -1, 1); + x.pParse = pParse; + x.iTable = pSrc->iCursor; + x.iNewTable = pSrc->iCursor; + x.isOuterJoin = 0; + x.nSelDepth = 0; + x.pEList = pSubq->pEList; + x.pCList = findLeftmostExprlist(pSubq); + pNew = substExpr(&x, pNew); +#ifndef SQLITE_OMIT_WINDOWFUNC + if( pSubq->pWin && 0==pushDownWindowCheck(pParse, pSubq, pNew) ){ + /* Restriction 6c has prevented push-down in this case */ + sqlite3ExprDelete(pParse->db, pNew); + nChng--; + break; + } +#endif + if( pSubq->selFlags & SF_Aggregate ){ + pSubq->pHaving = sqlite3ExprAnd(pParse, pSubq->pHaving, pNew); + }else{ + pSubq->pWhere = sqlite3ExprAnd(pParse, pSubq->pWhere, pNew); + } + pSubq = pSubq->pPrior; + } + } + return nChng; +} +#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */ + +/* +** Check to see if a subquery contains result-set columns that are +** never used. If it does, change the value of those result-set columns +** to NULL so that they do not cause unnecessary work to compute. +** +** Return the number of column that were changed to NULL. +*/ +static int disableUnusedSubqueryResultColumns(SrcItem *pItem){ + int nCol; + Select *pSub; /* The subquery to be simplified */ + Select *pX; /* For looping over compound elements of pSub */ + Table *pTab; /* The table that describes the subquery */ + int j; /* Column number */ + int nChng = 0; /* Number of columns converted to NULL */ + Bitmask colUsed; /* Columns that may not be NULLed out */ + + assert( pItem!=0 ); + if( pItem->fg.isCorrelated || pItem->fg.isCte ){ + return 0; + } + assert( pItem->pSTab!=0 ); + pTab = pItem->pSTab; + assert( pItem->fg.isSubquery ); + pSub = pItem->u4.pSubq->pSelect; + assert( pSub->pEList->nExpr==pTab->nCol ); + for(pX=pSub; pX; pX=pX->pPrior){ + if( (pX->selFlags & (SF_Distinct|SF_Aggregate))!=0 ){ + testcase( pX->selFlags & SF_Distinct ); + testcase( pX->selFlags & SF_Aggregate ); + return 0; + } + if( pX->pPrior && pX->op!=TK_ALL ){ + /* This optimization does not work for compound subqueries that + ** use UNION, INTERSECT, or EXCEPT. Only UNION ALL is allowed. */ + return 0; + } +#ifndef SQLITE_OMIT_WINDOWFUNC + if( pX->pWin ){ + /* This optimization does not work for subqueries that use window + ** functions. */ + return 0; + } +#endif + } + colUsed = pItem->colUsed; + if( pSub->pOrderBy ){ + ExprList *pList = pSub->pOrderBy; + for(j=0; jnExpr; j++){ + u16 iCol = pList->a[j].u.x.iOrderByCol; + if( iCol>0 ){ + iCol--; + colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol); + } + } + } + nCol = pTab->nCol; + for(j=0; jpPrior) { + Expr *pY = pX->pEList->a[j].pExpr; + if( pY->op==TK_NULL ) continue; + pY->op = TK_NULL; + ExprClearProperty(pY, EP_Skip|EP_Unlikely); + pX->selFlags |= SF_PushDown; + nChng++; + } + } + return nChng; +} + + +/* +** The pFunc is the only aggregate function in the query. Check to see +** if the query is a candidate for the min/max optimization. +** +** If the query is a candidate for the min/max optimization, then set +** *ppMinMax to be an ORDER BY clause to be used for the optimization +** and return either WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX depending on +** whether pFunc is a min() or max() function. +** +** If the query is not a candidate for the min/max optimization, return +** WHERE_ORDERBY_NORMAL (which must be zero). +** +** This routine must be called after aggregate functions have been +** located but before their arguments have been subjected to aggregate +** analysis. +*/ +static u8 minMaxQuery(sqlite3 *db, Expr *pFunc, ExprList **ppMinMax){ + int eRet = WHERE_ORDERBY_NORMAL; /* Return value */ + ExprList *pEList; /* Arguments to agg function */ + const char *zFunc; /* Name of aggregate function pFunc */ + ExprList *pOrderBy; + u8 sortFlags = 0; + + assert( *ppMinMax==0 ); + assert( pFunc->op==TK_AGG_FUNCTION ); + assert( !IsWindowFunc(pFunc) ); + assert( ExprUseXList(pFunc) ); + pEList = pFunc->x.pList; + if( pEList==0 + || pEList->nExpr!=1 + || ExprHasProperty(pFunc, EP_WinFunc) + || OptimizationDisabled(db, SQLITE_MinMaxOpt) + ){ + return eRet; + } + assert( !ExprHasProperty(pFunc, EP_IntValue) ); + zFunc = pFunc->u.zToken; + if( sqlite3StrICmp(zFunc, "min")==0 ){ + eRet = WHERE_ORDERBY_MIN; + if( sqlite3ExprCanBeNull(pEList->a[0].pExpr) ){ + sortFlags = KEYINFO_ORDER_BIGNULL; + } + }else if( sqlite3StrICmp(zFunc, "max")==0 ){ + eRet = WHERE_ORDERBY_MAX; + sortFlags = KEYINFO_ORDER_DESC; + }else{ + return eRet; + } + *ppMinMax = pOrderBy = sqlite3ExprListDup(db, pEList, 0); + assert( pOrderBy!=0 || db->mallocFailed ); + if( pOrderBy ) pOrderBy->a[0].fg.sortFlags = sortFlags; + return eRet; +} + +/* +** The select statement passed as the first argument is an aggregate query. +** The second argument is the associated aggregate-info object. This +** function tests if the SELECT is of the form: +** +** SELECT count(*) FROM +** +** where table is a database table, not a sub-select or view. If the query +** does match this pattern, then a pointer to the Table object representing +** is returned. Otherwise, NULL is returned. +** +** This routine checks to see if it is safe to use the count optimization. +** A correct answer is still obtained (though perhaps more slowly) if +** this routine returns NULL when it could have returned a table pointer. +** But returning the pointer when NULL should have been returned can +** result in incorrect answers and/or crashes. So, when in doubt, return NULL. +*/ +static Table *isSimpleCount(Select *p, AggInfo *pAggInfo){ + Table *pTab; + Expr *pExpr; assert( !p->pGroupBy ); - if( p->pWhere || p->pEList->nExpr!=1 - || p->pSrc->nSrc!=1 || p->pSrc->a[0].pSelect + if( p->pWhere + || p->pEList->nExpr!=1 + || p->pSrc->nSrc!=1 + || p->pSrc->a[0].fg.isSubquery + || pAggInfo->nFunc!=1 + || p->pHaving ){ return 0; } - pTab = p->pSrc->a[0].pTab; + pTab = p->pSrc->a[0].pSTab; + assert( pTab!=0 ); + assert( !IsView(pTab) ); + if( !IsOrdinaryTable(pTab) ) return 0; pExpr = p->pEList->a[0].pExpr; - assert( pTab && !pTab->pSelect && pExpr ); - - if( IsVirtual(pTab) ) return 0; + assert( pExpr!=0 ); if( pExpr->op!=TK_AGG_FUNCTION ) return 0; - if( NEVER(pAggInfo->nFunc==0) ) return 0; + if( pExpr->pAggInfo!=pAggInfo ) return 0; if( (pAggInfo->aFunc[0].pFunc->funcFlags&SQLITE_FUNC_COUNT)==0 ) return 0; - if( pExpr->flags&EP_Distinct ) return 0; + assert( pAggInfo->aFunc[0].pFExpr==pExpr ); + testcase( ExprHasProperty(pExpr, EP_Distinct) ); + testcase( ExprHasProperty(pExpr, EP_WinFunc) ); + if( ExprHasProperty(pExpr, EP_Distinct|EP_WinFunc) ) return 0; return pTab; } @@ -3879,30 +5594,33 @@ static Table *isSimpleCount(Select *p, AggInfo *pAggInfo){ /* ** If the source-list item passed as an argument was augmented with an ** INDEXED BY clause, then try to locate the specified index. If there -** was such a clause and the named index cannot be found, return -** SQLITE_ERROR and leave an error in pParse. Otherwise, populate +** was such a clause and the named index cannot be found, return +** SQLITE_ERROR and leave an error in pParse. Otherwise, populate ** pFrom->pIndex and return SQLITE_OK. */ -int sqlite3IndexedByLookup(Parse *pParse, struct SrcList_item *pFrom){ - if( pFrom->pTab && pFrom->fg.isIndexedBy ){ - Table *pTab = pFrom->pTab; - char *zIndexedBy = pFrom->u1.zIndexedBy; - Index *pIdx; - for(pIdx=pTab->pIndex; - pIdx && sqlite3StrICmp(pIdx->zName, zIndexedBy); - pIdx=pIdx->pNext - ); - if( !pIdx ){ - sqlite3ErrorMsg(pParse, "no such index: %s", zIndexedBy, 0); - pParse->checkSchema = 1; - return SQLITE_ERROR; - } - pFrom->pIBIndex = pIdx; - } +int sqlite3IndexedByLookup(Parse *pParse, SrcItem *pFrom){ + Table *pTab = pFrom->pSTab; + char *zIndexedBy = pFrom->u1.zIndexedBy; + Index *pIdx; + assert( pTab!=0 ); + assert( pFrom->fg.isIndexedBy!=0 ); + + for(pIdx=pTab->pIndex; + pIdx && sqlite3StrICmp(pIdx->zName, zIndexedBy); + pIdx=pIdx->pNext + ); + if( !pIdx ){ + sqlite3ErrorMsg(pParse, "no such index: %s", zIndexedBy, 0); + pParse->checkSchema = 1; + return SQLITE_ERROR; + } + assert( pFrom->fg.isCte==0 ); + pFrom->u2.pIBIndex = pIdx; return SQLITE_OK; } + /* -** Detect compound SELECT statements that use an ORDER BY clause with +** Detect compound SELECT statements that use an ORDER BY clause with ** an alternative collating sequence. ** ** SELECT ... FROM t1 EXCEPT SELECT ... FROM t2 ORDER BY .. COLLATE ... @@ -3916,7 +5634,7 @@ int sqlite3IndexedByLookup(Parse *pParse, struct SrcList_item *pFrom){ ** above that generates the code for a compound SELECT with an ORDER BY clause ** uses a merge algorithm that requires the same collating sequence on the ** result columns as on the ORDER BY clause. See ticket -** http://www.sqlite.org/src/info/6709574d2a +** http://sqlite.org/src/info/6709574d2a ** ** This transformation is only needed for EXCEPT, INTERSECT, and UNION. ** The UNION ALL operator works fine with multiSelectOrderBy() even when @@ -3937,6 +5655,14 @@ static int convertCompoundSelectToSubquery(Walker *pWalker, Select *p){ for(pX=p; pX && (pX->op==TK_ALL || pX->op==TK_SELECT); pX=pX->pPrior){} if( pX==0 ) return WRC_Continue; a = p->pOrderBy->a; +#ifndef SQLITE_OMIT_WINDOWFUNC + /* If iOrderByCol is already non-zero, then it has already been matched + ** to a result column of the SELECT statement. This occurs when the + ** SELECT is rewritten for window-functions processing and then passed + ** to sqlite3SelectPrep() and similar a second time. The rewriting done + ** by this function is not required in this case. */ + if( a[0].u.x.iOrderByCol ) return WRC_Continue; +#endif for(i=p->pOrderBy->nExpr-1; i>=0; i--){ if( a[i].pExpr->flags & EP_Collate ) break; } @@ -3949,8 +5675,12 @@ static int convertCompoundSelectToSubquery(Walker *pWalker, Select *p){ pNew = sqlite3DbMallocZero(db, sizeof(*pNew) ); if( pNew==0 ) return WRC_Abort; memset(&dummy, 0, sizeof(dummy)); - pNewSrc = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&dummy,pNew,0,0); - if( pNewSrc==0 ) return WRC_Abort; + pNewSrc = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&dummy,pNew,0); + assert( pNewSrc!=0 || pParse->nErr ); + if( pParse->nErr ){ + sqlite3SrcListDelete(db, pNewSrc); + return WRC_Abort; + } *pNew = *p; p->pSrc = pNewSrc; p->pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ASTERISK, 0)); @@ -3962,13 +5692,15 @@ static int convertCompoundSelectToSubquery(Walker *pWalker, Select *p){ p->pPrior = 0; p->pNext = 0; p->pWith = 0; - p->selFlags &= ~SF_Compound; +#ifndef SQLITE_OMIT_WINDOWFUNC + p->pWinDefn = 0; +#endif + p->selFlags &= ~(u32)SF_Compound; assert( (p->selFlags & SF_Converted)==0 ); p->selFlags |= SF_Converted; assert( pNew->pPrior!=0 ); pNew->pPrior->pNext = pNew; pNew->pLimit = 0; - pNew->pOffset = 0; return WRC_Continue; } @@ -3977,7 +5709,7 @@ static int convertCompoundSelectToSubquery(Walker *pWalker, Select *p){ ** arguments. If it does, leave an error message in pParse and return ** non-zero, since pFrom is not allowed to be a table-valued function. */ -static int cannotBeFunction(Parse *pParse, struct SrcList_item *pFrom){ +static int cannotBeFunction(Parse *pParse, SrcItem *pFrom){ if( pFrom->fg.isTabFunc ){ sqlite3ErrorMsg(pParse, "'%s' is not a function", pFrom->zName); return 1; @@ -3987,9 +5719,9 @@ static int cannotBeFunction(Parse *pParse, struct SrcList_item *pFrom){ #ifndef SQLITE_OMIT_CTE /* -** Argument pWith (which may be NULL) points to a linked list of nested -** WITH contexts, from inner to outermost. If the table identified by -** FROM clause element pItem is really a common-table-expression (CTE) +** Argument pWith (which may be NULL) points to a linked list of nested +** WITH contexts, from inner to outermost. If the table identified by +** FROM clause element pItem is really a common-table-expression (CTE) ** then return a pointer to the CTE definition for that table. Otherwise ** return NULL. ** @@ -3998,21 +5730,22 @@ static int cannotBeFunction(Parse *pParse, struct SrcList_item *pFrom){ */ static struct Cte *searchWith( With *pWith, /* Current innermost WITH clause */ - struct SrcList_item *pItem, /* FROM clause element to resolve */ + SrcItem *pItem, /* FROM clause element to resolve */ With **ppContext /* OUT: WITH clause return value belongs to */ ){ - const char *zName; - if( pItem->zDatabase==0 && (zName = pItem->zName)!=0 ){ - With *p; - for(p=pWith; p; p=p->pOuter){ - int i; - for(i=0; inCte; i++){ - if( sqlite3StrICmp(zName, p->a[i].zName)==0 ){ - *ppContext = p; - return &p->a[i]; - } + const char *zName = pItem->zName; + With *p; + assert( pItem->fg.fixedSchema || pItem->u4.zDatabase==0 ); + assert( zName!=0 ); + for(p=pWith; p; p=p->pOuter){ + int i; + for(i=0; inCte; i++){ + if( sqlite3StrICmp(zName, p->a[i].zName)==0 ){ + *ppContext = p; + return &p->a[i]; } } + if( p->bView ) break; } return 0; } @@ -4022,55 +5755,92 @@ static struct Cte *searchWith( ** ** This routine pushes the WITH clause passed as the second argument ** onto the top of the stack. If argument bFree is true, then this -** WITH clause will never be popped from the stack. In this case it -** should be freed along with the Parse object. In other cases, when -** bFree==0, the With object will be freed along with the SELECT +** WITH clause will never be popped from the stack but should instead +** be freed along with the Parse object. In other cases, when +** bFree==0, the With object will be freed along with the SELECT ** statement with which it is associated. +** +** This routine returns a copy of pWith. Or, if bFree is true and +** the pWith object is destroyed immediately due to an OOM condition, +** then this routine return NULL. +** +** If bFree is true, do not continue to use the pWith pointer after +** calling this routine, Instead, use only the return value. */ -void sqlite3WithPush(Parse *pParse, With *pWith, u8 bFree){ - assert( bFree==0 || (pParse->pWith==0 && pParse->pWithToFree==0) ); +With *sqlite3WithPush(Parse *pParse, With *pWith, u8 bFree){ if( pWith ){ - assert( pParse->pWith!=pWith ); - pWith->pOuter = pParse->pWith; - pParse->pWith = pWith; - if( bFree ) pParse->pWithToFree = pWith; + if( bFree ){ + pWith = (With*)sqlite3ParserAddCleanup(pParse, sqlite3WithDeleteGeneric, + pWith); + if( pWith==0 ) return 0; + } + if( pParse->nErr==0 ){ + assert( pParse->pWith!=pWith ); + pWith->pOuter = pParse->pWith; + pParse->pWith = pWith; + } } + return pWith; } /* -** This function checks if argument pFrom refers to a CTE declared by -** a WITH clause on the stack currently maintained by the parser. And, -** if currently processing a CTE expression, if it is a recursive -** reference to the current CTE. -** -** If pFrom falls into either of the two categories above, pFrom->pTab -** and other fields are populated accordingly. The caller should check -** (pFrom->pTab!=0) to determine whether or not a successful match -** was found. -** -** Whether or not a match is found, SQLITE_OK is returned if no error -** occurs. If an error does occur, an error message is stored in the -** parser and some error code other than SQLITE_OK returned. +** This function checks if argument pFrom refers to a CTE declared by +** a WITH clause on the stack currently maintained by the parser (on the +** pParse->pWith linked list). And if currently processing a CTE +** CTE expression, through routine checks to see if the reference is +** a recursive reference to the CTE. +** +** If pFrom matches a CTE according to either of these two above, pFrom->pSTab +** and other fields are populated accordingly. +** +** Return 0 if no match is found. +** Return 1 if a match is found. +** Return 2 if an error condition is detected. */ -static int withExpand( - Walker *pWalker, - struct SrcList_item *pFrom +static int resolveFromTermToCte( + Parse *pParse, /* The parsing context */ + Walker *pWalker, /* Current tree walker */ + SrcItem *pFrom /* The FROM clause term to check */ ){ - Parse *pParse = pWalker->pParse; - sqlite3 *db = pParse->db; - struct Cte *pCte; /* Matched CTE (or NULL if no match) */ - With *pWith; /* WITH clause that pCte belongs to */ - - assert( pFrom->pTab==0 ); + Cte *pCte; /* Matched CTE (or NULL if no match) */ + With *pWith; /* The matching WITH */ + assert( pFrom->pSTab==0 ); + if( pParse->pWith==0 ){ + /* There are no WITH clauses in the stack. No match is possible */ + return 0; + } + if( pParse->nErr ){ + /* Prior errors might have left pParse->pWith in a goofy state, so + ** go no further. */ + return 0; + } + assert( pFrom->fg.hadSchema==0 || pFrom->fg.notCte!=0 ); + if( pFrom->fg.fixedSchema==0 && pFrom->u4.zDatabase!=0 ){ + /* The FROM term contains a schema qualifier (ex: main.t1) and so + ** it cannot possibly be a CTE reference. */ + return 0; + } + if( pFrom->fg.notCte ){ + /* The FROM term is specifically excluded from matching a CTE. + ** (1) It is part of a trigger that used to have zDatabase but had + ** zDatabase removed by sqlite3FixTriggerStep(). + ** (2) This is the first term in the FROM clause of an UPDATE. + */ + return 0; + } pCte = searchWith(pParse->pWith, pFrom, &pWith); if( pCte ){ + sqlite3 *db = pParse->db; Table *pTab; ExprList *pEList; Select *pSel; Select *pLeft; /* Left-most SELECT statement */ + Select *pRecTerm; /* Left-most recursive term */ int bMayRecursive; /* True if compound joined by UNION [ALL] */ With *pSavedWith; /* Initial value of pParse->pWith */ + int iRecTab = -1; /* Cursor for recursive table */ + CteUse *pCteUse; /* If pCte->zCteErr is non-NULL at this point, then this is an illegal ** recursive reference to CTE pCte. Leave an error in pParse and return @@ -4078,66 +5848,112 @@ static int withExpand( ** In this case, proceed. */ if( pCte->zCteErr ){ sqlite3ErrorMsg(pParse, pCte->zCteErr, pCte->zName); - return SQLITE_ERROR; + return 2; } - if( cannotBeFunction(pParse, pFrom) ) return SQLITE_ERROR; - - assert( pFrom->pTab==0 ); - pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table)); - if( pTab==0 ) return WRC_Abort; - pTab->nRef = 1; + if( cannotBeFunction(pParse, pFrom) ) return 2; + + assert( pFrom->pSTab==0 ); + pTab = sqlite3DbMallocZero(db, sizeof(Table)); + if( pTab==0 ) return 2; + pCteUse = pCte->pUse; + if( pCteUse==0 ){ + pCte->pUse = pCteUse = sqlite3DbMallocZero(db, sizeof(pCteUse[0])); + if( pCteUse==0 + || sqlite3ParserAddCleanup(pParse,sqlite3DbFree,pCteUse)==0 + ){ + sqlite3DbFree(db, pTab); + return 2; + } + pCteUse->eM10d = pCte->eM10d; + } + pFrom->pSTab = pTab; + pTab->nTabRef = 1; pTab->zName = sqlite3DbStrDup(db, pCte->zName); pTab->iPKey = -1; pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) ); pTab->tabFlags |= TF_Ephemeral | TF_NoVisibleRowid; - pFrom->pSelect = sqlite3SelectDup(db, pCte->pSelect, 0); - if( db->mallocFailed ) return SQLITE_NOMEM; - assert( pFrom->pSelect ); + sqlite3SrcItemAttachSubquery(pParse, pFrom, pCte->pSelect, 1); + if( db->mallocFailed ) return 2; + assert( pFrom->fg.isSubquery && pFrom->u4.pSubq ); + pSel = pFrom->u4.pSubq->pSelect; + assert( pSel!=0 ); + pSel->selFlags |= SF_CopyCte; + if( pFrom->fg.isIndexedBy ){ + sqlite3ErrorMsg(pParse, "no such index: \"%s\"", pFrom->u1.zIndexedBy); + return 2; + } + assert( !pFrom->fg.isIndexedBy ); + pFrom->fg.isCte = 1; + pFrom->u2.pCteUse = pCteUse; + pCteUse->nUse++; /* Check if this is a recursive CTE. */ - pSel = pFrom->pSelect; + pRecTerm = pSel; bMayRecursive = ( pSel->op==TK_ALL || pSel->op==TK_UNION ); - if( bMayRecursive ){ + while( bMayRecursive && pRecTerm->op==pSel->op ){ int i; - SrcList *pSrc = pFrom->pSelect->pSrc; + SrcList *pSrc = pRecTerm->pSrc; + assert( pRecTerm->pPrior!=0 ); for(i=0; inSrc; i++){ - struct SrcList_item *pItem = &pSrc->a[i]; - if( pItem->zDatabase==0 - && pItem->zName!=0 + SrcItem *pItem = &pSrc->a[i]; + if( pItem->zName!=0 + && !pItem->fg.hadSchema + && ALWAYS( !pItem->fg.isSubquery ) + && (pItem->fg.fixedSchema || pItem->u4.zDatabase==0) && 0==sqlite3StrICmp(pItem->zName, pCte->zName) - ){ - pItem->pTab = pTab; + ){ + pItem->pSTab = pTab; + pTab->nTabRef++; pItem->fg.isRecursive = 1; - pTab->nRef++; - pSel->selFlags |= SF_Recursive; + if( pRecTerm->selFlags & SF_Recursive ){ + sqlite3ErrorMsg(pParse, + "multiple references to recursive table: %s", pCte->zName + ); + return 2; + } + pRecTerm->selFlags |= SF_Recursive; + if( iRecTab<0 ) iRecTab = pParse->nTab++; + pItem->iCursor = iRecTab; } } + if( (pRecTerm->selFlags & SF_Recursive)==0 ) break; + pRecTerm = pRecTerm->pPrior; } - /* Only one recursive reference is permitted. */ - if( pTab->nRef>2 ){ - sqlite3ErrorMsg( - pParse, "multiple references to recursive table: %s", pCte->zName - ); - return SQLITE_ERROR; - } - assert( pTab->nRef==1 || ((pSel->selFlags&SF_Recursive) && pTab->nRef==2 )); - pCte->zCteErr = "circular reference: %s"; pSavedWith = pParse->pWith; pParse->pWith = pWith; - sqlite3WalkSelect(pWalker, bMayRecursive ? pSel->pPrior : pSel); - pParse->pWith = pWith; - - for(pLeft=pSel; pLeft->pPrior; pLeft=pLeft->pPrior); - pEList = pLeft->pEList; + if( pSel->selFlags & SF_Recursive ){ + int rc; + assert( pRecTerm!=0 ); + assert( (pRecTerm->selFlags & SF_Recursive)==0 ); + assert( pRecTerm->pNext!=0 ); + assert( (pRecTerm->pNext->selFlags & SF_Recursive)!=0 ); + assert( pRecTerm->pWith==0 ); + pRecTerm->pWith = pSel->pWith; + rc = sqlite3WalkSelect(pWalker, pRecTerm); + pRecTerm->pWith = 0; + if( rc ){ + pParse->pWith = pSavedWith; + return 2; + } + }else{ + if( sqlite3WalkSelect(pWalker, pSel) ){ + pParse->pWith = pSavedWith; + return 2; + } + } + pParse->pWith = pWith; + + for(pLeft=pSel; pLeft->pPrior; pLeft=pLeft->pPrior); + pEList = pLeft->pEList; if( pCte->pCols ){ if( pEList && pEList->nExpr!=pCte->pCols->nExpr ){ sqlite3ErrorMsg(pParse, "table %s has %d values for %d columns", pCte->zName, pEList->nExpr, pCte->pCols->nExpr ); pParse->pWith = pSavedWith; - return SQLITE_ERROR; + return 2; } pEList = pCte->pCols; } @@ -4153,32 +5969,96 @@ static int withExpand( } pCte->zCteErr = 0; pParse->pWith = pSavedWith; + return 1; /* Success */ } - - return SQLITE_OK; + return 0; /* No match */ } #endif #ifndef SQLITE_OMIT_CTE /* -** If the SELECT passed as the second argument has an associated WITH +** If the SELECT passed as the second argument has an associated WITH ** clause, pop it from the stack stored as part of the Parse object. ** ** This function is used as the xSelectCallback2() callback by ** sqlite3SelectExpand() when walking a SELECT tree to resolve table -** names and other FROM clause elements. +** names and other FROM clause elements. */ -static void selectPopWith(Walker *pWalker, Select *p){ +void sqlite3SelectPopWith(Walker *pWalker, Select *p){ Parse *pParse = pWalker->pParse; - With *pWith = findRightmost(p)->pWith; - if( pWith!=0 ){ - assert( pParse->pWith==pWith ); - pParse->pWith = pWith->pOuter; + if( OK_IF_ALWAYS_TRUE(pParse->pWith) && p->pPrior==0 ){ + With *pWith = findRightmost(p)->pWith; + if( pWith!=0 ){ + assert( pParse->pWith==pWith || pParse->nErr ); + pParse->pWith = pWith->pOuter; + } } } +#endif + +/* +** The SrcItem structure passed as the second argument represents a +** sub-query in the FROM clause of a SELECT statement. This function +** allocates and populates the SrcItem.pTab object. If successful, +** SQLITE_OK is returned. Otherwise, if an OOM error is encountered, +** SQLITE_NOMEM. +*/ +int sqlite3ExpandSubquery(Parse *pParse, SrcItem *pFrom){ + Select *pSel; + Table *pTab; + + assert( pFrom->fg.isSubquery ); + assert( pFrom->u4.pSubq!=0 ); + pSel = pFrom->u4.pSubq->pSelect; + assert( pSel ); + pFrom->pSTab = pTab = sqlite3DbMallocZero(pParse->db, sizeof(Table)); + if( pTab==0 ) return SQLITE_NOMEM; + pTab->nTabRef = 1; + if( pFrom->zAlias ){ + pTab->zName = sqlite3DbStrDup(pParse->db, pFrom->zAlias); + }else{ + pTab->zName = sqlite3MPrintf(pParse->db, "%!S", pFrom); + } + while( pSel->pPrior ){ pSel = pSel->pPrior; } + sqlite3ColumnsFromExprList(pParse, pSel->pEList,&pTab->nCol,&pTab->aCol); + pTab->iPKey = -1; + pTab->eTabType = TABTYP_VIEW; + pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) ); +#ifndef SQLITE_ALLOW_ROWID_IN_VIEW + /* The usual case - do not allow ROWID on a subquery */ + pTab->tabFlags |= TF_Ephemeral | TF_NoVisibleRowid; #else -#define selectPopWith 0 + /* Legacy compatibility mode */ + pTab->tabFlags |= TF_Ephemeral | sqlite3Config.mNoVisibleRowid; #endif + return pParse->nErr ? SQLITE_ERROR : SQLITE_OK; +} + + +/* +** Check the N SrcItem objects to the right of pBase. (N might be zero!) +** If any of those SrcItem objects have a USING clause containing zName +** then return true. +** +** If N is zero, or none of the N SrcItem objects to the right of pBase +** contains a USING clause, or if none of the USING clauses contain zName, +** then return false. +*/ +static int inAnyUsingClause( + const char *zName, /* Name we are looking for */ + SrcItem *pBase, /* The base SrcItem. Looking at pBase[1] and following */ + int N /* How many SrcItems to check */ +){ + while( N>0 ){ + N--; + pBase++; + if( pBase->fg.isUsing==0 ) continue; + if( NEVER(pBase->u3.pUsing==0) ) continue; + if( sqlite3IdListIndex(pBase->u3.pUsing, zName)>=0 ) return 1; + } + return 0; +} + /* ** This routine is a Walker callback for "expanding" a SELECT statement. @@ -4187,7 +6067,7 @@ static void selectPopWith(Walker *pWalker, Select *p){ ** (1) Make sure VDBE cursor numbers have been assigned to every ** element of the FROM clause. ** -** (2) Fill in the pTabList->a[].pTab fields in the SrcList that +** (2) Fill in the pTabList->a[].pTab fields in the SrcList that ** defines FROM clause. When views appear in the FROM clause, ** fill pTabList->a[].pSelect with a copy of the SELECT statement ** that implements the view. A copy is made of the view's SELECT @@ -4206,26 +6086,39 @@ static void selectPopWith(Walker *pWalker, Select *p){ */ static int selectExpander(Walker *pWalker, Select *p){ Parse *pParse = pWalker->pParse; - int i, j, k; + int i, j, k, rc; SrcList *pTabList; ExprList *pEList; - struct SrcList_item *pFrom; + SrcItem *pFrom; sqlite3 *db = pParse->db; Expr *pE, *pRight, *pExpr; u16 selFlags = p->selFlags; + u32 elistFlags = 0; p->selFlags |= SF_Expanded; if( db->mallocFailed ){ return WRC_Abort; } - if( NEVER(p->pSrc==0) || (selFlags & SF_Expanded)!=0 ){ + assert( p->pSrc!=0 ); + if( (selFlags & SF_Expanded)!=0 ){ return WRC_Prune; } + if( pWalker->eCode ){ + /* Renumber selId because it has been copied from a view */ + p->selId = ++pParse->nSelect; + } pTabList = p->pSrc; pEList = p->pEList; - if( pWalker->xSelectCallback2==selectPopWith ){ - sqlite3WithPush(pParse, findRightmost(p)->pWith, 0); + if( pParse->pWith && (p->selFlags & SF_View) ){ + if( p->pWith==0 ){ + p->pWith = (With*)sqlite3DbMallocZero(db, SZ_WITH(1) ); + if( p->pWith==0 ){ + return WRC_Abort; + } + } + p->pWith->bView = 1; } + sqlite3WithPush(pParse, p->pWith, 0); /* Make sure cursor numbers have been assigned to all entries in ** the FROM clause of the SELECT statement. @@ -4238,69 +6131,89 @@ static int selectExpander(Walker *pWalker, Select *p){ */ for(i=0, pFrom=pTabList->a; inSrc; i++, pFrom++){ Table *pTab; - assert( pFrom->fg.isRecursive==0 || pFrom->pTab!=0 ); - if( pFrom->fg.isRecursive ) continue; - assert( pFrom->pTab==0 ); -#ifndef SQLITE_OMIT_CTE - if( withExpand(pWalker, pFrom) ) return WRC_Abort; - if( pFrom->pTab ) {} else -#endif + assert( pFrom->fg.isRecursive==0 || pFrom->pSTab!=0 ); + if( pFrom->pSTab ) continue; + assert( pFrom->fg.isRecursive==0 ); if( pFrom->zName==0 ){ #ifndef SQLITE_OMIT_SUBQUERY - Select *pSel = pFrom->pSelect; + Select *pSel; + assert( pFrom->fg.isSubquery && pFrom->u4.pSubq!=0 ); + pSel = pFrom->u4.pSubq->pSelect; /* A sub-query in the FROM clause of a SELECT */ assert( pSel!=0 ); - assert( pFrom->pTab==0 ); + assert( pFrom->pSTab==0 ); if( sqlite3WalkSelect(pWalker, pSel) ) return WRC_Abort; - pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table)); - if( pTab==0 ) return WRC_Abort; - pTab->nRef = 1; - pTab->zName = sqlite3MPrintf(db, "sqlite_sq_%p", (void*)pTab); - while( pSel->pPrior ){ pSel = pSel->pPrior; } - sqlite3ColumnsFromExprList(pParse, pSel->pEList,&pTab->nCol,&pTab->aCol); - pTab->iPKey = -1; - pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) ); - pTab->tabFlags |= TF_Ephemeral; + if( sqlite3ExpandSubquery(pParse, pFrom) ) return WRC_Abort; +#endif +#ifndef SQLITE_OMIT_CTE + }else if( (rc = resolveFromTermToCte(pParse, pWalker, pFrom))!=0 ){ + if( rc>1 ) return WRC_Abort; + pTab = pFrom->pSTab; + assert( pTab!=0 ); #endif }else{ /* An ordinary table or view name in the FROM clause */ - assert( pFrom->pTab==0 ); - pFrom->pTab = pTab = sqlite3LocateTableItem(pParse, 0, pFrom); + assert( pFrom->pSTab==0 ); + pFrom->pSTab = pTab = sqlite3LocateTableItem(pParse, 0, pFrom); if( pTab==0 ) return WRC_Abort; - if( pTab->nRef==0xffff ){ + if( pTab->nTabRef>=0xffff ){ sqlite3ErrorMsg(pParse, "too many references to \"%s\": max 65535", pTab->zName); - pFrom->pTab = 0; + pFrom->pSTab = 0; return WRC_Abort; } - pTab->nRef++; + pTab->nTabRef++; if( !IsVirtual(pTab) && cannotBeFunction(pParse, pFrom) ){ return WRC_Abort; } -#if !defined(SQLITE_OMIT_VIEW) || !defined (SQLITE_OMIT_VIRTUALTABLE) - if( IsVirtual(pTab) || pTab->pSelect ){ +#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) + if( !IsOrdinaryTable(pTab) ){ i16 nCol; + u8 eCodeOrig = pWalker->eCode; if( sqlite3ViewGetColumnNames(pParse, pTab) ) return WRC_Abort; - assert( pFrom->pSelect==0 ); - pFrom->pSelect = sqlite3SelectDup(db, pTab->pSelect, 0); - sqlite3SelectSetName(pFrom->pSelect, pTab->zName); + assert( pFrom->fg.isSubquery==0 ); + if( IsView(pTab) ){ + if( (db->flags & SQLITE_EnableView)==0 + && pTab->pSchema!=db->aDb[1].pSchema + ){ + sqlite3ErrorMsg(pParse, "access to view \"%s\" prohibited", + pTab->zName); + } + sqlite3SrcItemAttachSubquery(pParse, pFrom, pTab->u.view.pSelect, 1); + } +#ifndef SQLITE_OMIT_VIRTUALTABLE + else if( ALWAYS(IsVirtual(pTab)) + && pFrom->fg.fromDDL + && ALWAYS(pTab->u.vtab.p!=0) + && pTab->u.vtab.p->eVtabRisk > ((db->flags & SQLITE_TrustedSchema)!=0) + ){ + sqlite3ErrorMsg(pParse, "unsafe use of virtual table \"%s\"", + pTab->zName); + } + assert( SQLITE_VTABRISK_Normal==1 && SQLITE_VTABRISK_High==2 ); +#endif nCol = pTab->nCol; pTab->nCol = -1; - sqlite3WalkSelect(pWalker, pFrom->pSelect); + pWalker->eCode = 1; /* Turn on Select.selId renumbering */ + if( pFrom->fg.isSubquery ){ + sqlite3WalkSelect(pWalker, pFrom->u4.pSubq->pSelect); + } + pWalker->eCode = eCodeOrig; pTab->nCol = nCol; } #endif } /* Locate the index named by the INDEXED BY clause, if any. */ - if( sqlite3IndexedByLookup(pParse, pFrom) ){ + if( pFrom->fg.isIndexedBy && sqlite3IndexedByLookup(pParse, pFrom) ){ return WRC_Abort; } } /* Process NATURAL keywords, and ON and USING clauses of joins. */ - if( db->mallocFailed || sqliteProcessJoin(pParse, p) ){ + assert( db->mallocFailed==0 || pParse->nErr!=0 ); + if( pParse->nErr || sqlite3ProcessJoin(pParse, p) ){ return WRC_Abort; } @@ -4321,6 +6234,7 @@ static int selectExpander(Walker *pWalker, Select *p){ assert( pE->op!=TK_DOT || pE->pRight!=0 ); assert( pE->op!=TK_DOT || (pE->pLeft!=0 && pE->pLeft->op==TK_ID) ); if( pE->op==TK_DOT && pE->pRight->op==TK_ASTERISK ) break; + elistFlags |= pE->flags; } if( knExpr ){ /* @@ -4336,6 +6250,7 @@ static int selectExpander(Walker *pWalker, Select *p){ for(k=0; knExpr; k++){ pE = a[k].pExpr; + elistFlags |= pE->flags; pRight = pE->pRight; assert( pE->op!=TK_DOT || pRight!=0 ); if( pE->op!=TK_ASTERISK @@ -4345,10 +6260,9 @@ static int selectExpander(Walker *pWalker, Select *p){ */ pNew = sqlite3ExprListAppend(pParse, pNew, a[k].pExpr); if( pNew ){ - pNew->a[pNew->nExpr-1].zName = a[k].zName; - pNew->a[pNew->nExpr-1].zSpan = a[k].zSpan; - a[k].zName = 0; - a[k].zSpan = 0; + pNew->a[pNew->nExpr-1].zEName = a[k].zEName; + pNew->a[pNew->nExpr-1].fg.eEName = a[k].fg.eEName; + a[k].zEName = 0; } a[k].pExpr = 0; }else{ @@ -4356,101 +6270,177 @@ static int selectExpander(Walker *pWalker, Select *p){ ** expanded. */ int tableSeen = 0; /* Set to 1 when TABLE matches */ char *zTName = 0; /* text of name of TABLE */ + int iErrOfst; if( pE->op==TK_DOT ){ + assert( (selFlags & SF_NestedFrom)==0 ); assert( pE->pLeft!=0 ); assert( !ExprHasProperty(pE->pLeft, EP_IntValue) ); zTName = pE->pLeft->u.zToken; + assert( ExprUseWOfst(pE->pLeft) ); + iErrOfst = pE->pRight->w.iOfst; + }else{ + assert( ExprUseWOfst(pE) ); + iErrOfst = pE->w.iOfst; } for(i=0, pFrom=pTabList->a; inSrc; i++, pFrom++){ - Table *pTab = pFrom->pTab; - Select *pSub = pFrom->pSelect; - char *zTabName = pFrom->zAlias; - const char *zSchemaName = 0; - int iDb; - if( zTabName==0 ){ + int nAdd; /* Number of cols including rowid */ + Table *pTab = pFrom->pSTab; /* Table for this data source */ + ExprList *pNestedFrom; /* Result-set of a nested FROM clause */ + char *zTabName; /* AS name for this data source */ + const char *zSchemaName = 0; /* Schema name for this data source */ + int iDb; /* Schema index for this data src */ + IdList *pUsing; /* USING clause for pFrom[1] */ + + if( (zTabName = pFrom->zAlias)==0 ){ zTabName = pTab->zName; } if( db->mallocFailed ) break; - if( pSub==0 || (pSub->selFlags & SF_NestedFrom)==0 ){ - pSub = 0; + assert( (int)pFrom->fg.isNestedFrom == IsNestedFrom(pFrom) ); + if( pFrom->fg.isNestedFrom ){ + assert( pFrom->fg.isSubquery && pFrom->u4.pSubq ); + assert( pFrom->u4.pSubq->pSelect!=0 ); + pNestedFrom = pFrom->u4.pSubq->pSelect->pEList; + assert( pNestedFrom!=0 ); + assert( pNestedFrom->nExpr==pTab->nCol ); + assert( VisibleRowid(pTab)==0 || ViewCanHaveRowid ); + }else{ if( zTName && sqlite3StrICmp(zTName, zTabName)!=0 ){ continue; } + pNestedFrom = 0; iDb = sqlite3SchemaToIndex(db, pTab->pSchema); - zSchemaName = iDb>=0 ? db->aDb[iDb].zName : "*"; + zSchemaName = iDb>=0 ? db->aDb[iDb].zDbSName : "*"; } - for(j=0; jnCol; j++){ - char *zName = pTab->aCol[j].zName; - char *zColname; /* The computed column name */ - char *zToFree; /* Malloced string that needs to be freed */ - Token sColname; /* Computed column name as a token */ - - assert( zName ); - if( zTName && pSub - && sqlite3MatchSpanName(pSub->pEList->a[j].zSpan, 0, zTName, 0)==0 - ){ - continue; + if( i+1nSrc + && pFrom[1].fg.isUsing + && (selFlags & SF_NestedFrom)!=0 + ){ + int ii; + pUsing = pFrom[1].u3.pUsing; + for(ii=0; iinId; ii++){ + const char *zUName = pUsing->a[ii].zName; + pRight = sqlite3Expr(db, TK_ID, zUName); + sqlite3ExprSetErrorOffset(pRight, iErrOfst); + pNew = sqlite3ExprListAppend(pParse, pNew, pRight); + if( pNew ){ + struct ExprList_item *pX = &pNew->a[pNew->nExpr-1]; + assert( pX->zEName==0 ); + pX->zEName = sqlite3MPrintf(db,"..%s", zUName); + pX->fg.eEName = ENAME_TAB; + pX->fg.bUsingTerm = 1; + } } + }else{ + pUsing = 0; + } - /* If a column is marked as 'hidden', omit it from the expanded - ** result-set list unless the SELECT has the SF_IncludeHidden - ** bit set. - */ - if( (p->selFlags & SF_IncludeHidden)==0 - && IsHiddenColumn(&pTab->aCol[j]) - ){ - continue; - } - tableSeen = 1; + nAdd = pTab->nCol; + if( VisibleRowid(pTab) && (selFlags & SF_NestedFrom)!=0 ) nAdd++; + for(j=0; jnCol ){ + zName = sqlite3RowidAlias(pTab); + if( zName==0 ) continue; + }else{ + zName = pTab->aCol[j].zCnName; - if( i>0 && zTName==0 ){ - if( (pFrom->fg.jointype & JT_NATURAL)!=0 - && tableAndColumnIndex(pTabList, i, zName, 0, 0) + /* If pTab is actually an SF_NestedFrom sub-select, do not + ** expand any ENAME_ROWID columns. */ + if( pNestedFrom && pNestedFrom->a[j].fg.eEName==ENAME_ROWID ){ + continue; + } + + if( zTName + && pNestedFrom + && sqlite3MatchEName(&pNestedFrom->a[j], 0, zTName, 0, 0)==0 + ){ + continue; + } + + /* If a column is marked as 'hidden', omit it from the expanded + ** result-set list unless the SELECT has the SF_IncludeHidden + ** bit set. + */ + if( (p->selFlags & SF_IncludeHidden)==0 + && IsHiddenColumn(&pTab->aCol[j]) ){ - /* In a NATURAL join, omit the join columns from the - ** table to the right of the join */ continue; } - if( sqlite3IdListIndex(pFrom->pUsing, zName)>=0 ){ + if( (pTab->aCol[j].colFlags & COLFLAG_NOEXPAND)!=0 + && zTName==0 + && (selFlags & (SF_NestedFrom))==0 + ){ + continue; + } + } + assert( zName ); + tableSeen = 1; + + if( i>0 && zTName==0 && (selFlags & SF_NestedFrom)==0 ){ + if( pFrom->fg.isUsing + && sqlite3IdListIndex(pFrom->u3.pUsing, zName)>=0 + ){ /* In a join with a USING clause, omit columns in the ** using clause from the table on the right. */ continue; } } pRight = sqlite3Expr(db, TK_ID, zName); - zColname = zName; - zToFree = 0; - if( longNames || pTabList->nSrc>1 ){ + if( (pTabList->nSrc>1 + && ( (pFrom->fg.jointype & JT_LTORJ)==0 + || (selFlags & SF_NestedFrom)!=0 + || !inAnyUsingClause(zName,pFrom,pTabList->nSrc-i-1) + ) + ) + || IN_RENAME_OBJECT + ){ Expr *pLeft; pLeft = sqlite3Expr(db, TK_ID, zTabName); - pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0); + pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight); + if( IN_RENAME_OBJECT && pE->pLeft ){ + sqlite3RenameTokenRemap(pParse, pLeft, pE->pLeft); + } if( zSchemaName ){ pLeft = sqlite3Expr(db, TK_ID, zSchemaName); - pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pExpr, 0); - } - if( longNames ){ - zColname = sqlite3MPrintf(db, "%s.%s", zTabName, zName); - zToFree = zColname; + pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pExpr); } }else{ pExpr = pRight; } + sqlite3ExprSetErrorOffset(pExpr, iErrOfst); pNew = sqlite3ExprListAppend(pParse, pNew, pExpr); - sqlite3TokenInit(&sColname, zColname); - sqlite3ExprListSetName(pParse, pNew, &sColname, 0); - if( pNew && (p->selFlags & SF_NestedFrom)!=0 ){ - struct ExprList_item *pX = &pNew->a[pNew->nExpr-1]; - if( pSub ){ - pX->zSpan = sqlite3DbStrDup(db, pSub->pEList->a[j].zSpan); - testcase( pX->zSpan==0 ); + if( pNew==0 ){ + break; /* OOM */ + } + pX = &pNew->a[pNew->nExpr-1]; + assert( pX->zEName==0 ); + if( (selFlags & SF_NestedFrom)!=0 && !IN_RENAME_OBJECT ){ + if( pNestedFrom && (!ViewCanHaveRowid || jnExpr) ){ + assert( jnExpr ); + pX->zEName = sqlite3DbStrDup(db, pNestedFrom->a[j].zEName); + testcase( pX->zEName==0 ); }else{ - pX->zSpan = sqlite3MPrintf(db, "%s.%s.%s", - zSchemaName, zTabName, zColname); - testcase( pX->zSpan==0 ); + pX->zEName = sqlite3MPrintf(db, "%s.%s.%s", + zSchemaName, zTabName, zName); + testcase( pX->zEName==0 ); } - pX->bSpanIsTab = 1; + pX->fg.eEName = (j==pTab->nCol ? ENAME_ROWID : ENAME_TAB); + if( (pFrom->fg.isUsing + && sqlite3IdListIndex(pFrom->u3.pUsing, zName)>=0) + || (pUsing && sqlite3IdListIndex(pUsing, zName)>=0) + || (jnCol && (pTab->aCol[j].colFlags & COLFLAG_NOEXPAND)) + ){ + pX->fg.bNoExpand = 1; + } + }else if( longNames ){ + pX->zEName = sqlite3MPrintf(db, "%s.%s", zTabName, zName); + pX->fg.eEName = ENAME_NAME; + }else{ + pX->zEName = sqlite3DbStrDup(db, zName); + pX->fg.eEName = ENAME_NAME; } - sqlite3DbFree(db, zToFree); } } if( !tableSeen ){ @@ -4465,29 +6455,34 @@ static int selectExpander(Walker *pWalker, Select *p){ sqlite3ExprListDelete(db, pEList); p->pEList = pNew; } -#if SQLITE_MAX_COLUMN - if( p->pEList && p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){ - sqlite3ErrorMsg(pParse, "too many columns in result set"); - return WRC_Abort; + if( p->pEList ){ + if( p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){ + sqlite3ErrorMsg(pParse, "too many columns in result set"); + return WRC_Abort; + } + if( (elistFlags & (EP_HasFunc|EP_Subquery))!=0 ){ + p->selFlags |= SF_ComplexResult; + } + } +#if TREETRACE_ENABLED + if( sqlite3TreeTrace & 0x8 ){ + TREETRACE(0x8,pParse,p,("After result-set wildcard expansion:\n")); + sqlite3TreeViewSelect(0, p, 0); } #endif return WRC_Continue; } +#if SQLITE_DEBUG /* -** No-op routine for the parse-tree walker. -** -** When this routine is the Walker.xExprCallback then expression trees -** are walked without any actions being taken at each node. Presumably, -** when this routine is used for Walker.xExprCallback then -** Walker.xSelectCallback is set to do something useful for every -** subquery in the parser tree. +** Always assert. This xSelectCallback2 implementation proves that the +** xSelectCallback2 is never invoked. */ -int sqlite3ExprWalkNoop(Walker *NotUsed, Expr *NotUsed2){ +void sqlite3SelectWalkAssert2(Walker *NotUsed, Select *NotUsed2){ UNUSED_PARAMETER2(NotUsed, NotUsed2); - return WRC_Continue; + assert( 0 ); } - +#endif /* ** This routine "expands" a SELECT statement and all of its subqueries. ** For additional information on what it means to "expand" a SELECT @@ -4503,17 +6498,16 @@ int sqlite3ExprWalkNoop(Walker *NotUsed, Expr *NotUsed2){ */ static void sqlite3SelectExpand(Parse *pParse, Select *pSelect){ Walker w; - memset(&w, 0, sizeof(w)); w.xExprCallback = sqlite3ExprWalkNoop; w.pParse = pParse; - if( pParse->hasCompound ){ + if( OK_IF_ALWAYS_TRUE(pParse->hasCompound) ){ w.xSelectCallback = convertCompoundSelectToSubquery; + w.xSelectCallback2 = 0; sqlite3WalkSelect(&w, pSelect); } w.xSelectCallback = selectExpander; - if( (pSelect->selFlags & SF_MultiValue)==0 ){ - w.xSelectCallback2 = selectPopWith; - } + w.xSelectCallback2 = sqlite3SelectPopWith; + w.eCode = 0; sqlite3WalkSelect(&w, pSelect); } @@ -4523,36 +6517,33 @@ static void sqlite3SelectExpand(Parse *pParse, Select *pSelect){ ** This is a Walker.xSelectCallback callback for the sqlite3SelectTypeInfo() ** interface. ** -** For each FROM-clause subquery, add Column.zType and Column.zColl -** information to the Table structure that represents the result set -** of that subquery. +** For each FROM-clause subquery, add Column.zType, Column.zColl, and +** Column.affinity information to the Table structure that represents +** the result set of that subquery. ** ** The Table structure that represents the result set was constructed -** by selectExpander() but the type and collation information was omitted -** at that point because identifiers had not yet been resolved. This -** routine is called after identifier resolution. +** by selectExpander() but the type and collation and affinity information +** was omitted at that point because identifiers had not yet been resolved. +** This routine is called after identifier resolution. */ static void selectAddSubqueryTypeInfo(Walker *pWalker, Select *p){ Parse *pParse; int i; SrcList *pTabList; - struct SrcList_item *pFrom; + SrcItem *pFrom; - assert( p->selFlags & SF_Resolved ); - assert( (p->selFlags & SF_HasTypeInfo)==0 ); + if( p->selFlags & SF_HasTypeInfo ) return; p->selFlags |= SF_HasTypeInfo; pParse = pWalker->pParse; + assert( (p->selFlags & SF_Resolved) ); pTabList = p->pSrc; for(i=0, pFrom=pTabList->a; inSrc; i++, pFrom++){ - Table *pTab = pFrom->pTab; + Table *pTab = pFrom->pSTab; assert( pTab!=0 ); - if( (pTab->tabFlags & TF_Ephemeral)!=0 ){ + if( (pTab->tabFlags & TF_Ephemeral)!=0 && pFrom->fg.isSubquery ){ /* A sub-query in the FROM clause of a SELECT */ - Select *pSel = pFrom->pSelect; - if( pSel ){ - while( pSel->pPrior ) pSel = pSel->pPrior; - selectAddColumnTypeAndCollation(pParse, pTab, pSel); - } + Select *pSel = pFrom->u4.pSubq->pSelect; + sqlite3SubqueryColumnTypes(pParse, pTab, pSel, SQLITE_AFF_NONE); } } } @@ -4569,7 +6560,7 @@ static void selectAddSubqueryTypeInfo(Walker *pWalker, Select *p){ static void sqlite3SelectAddTypeInfo(Parse *pParse, Select *pSelect){ #ifndef SQLITE_OMIT_SUBQUERY Walker w; - memset(&w, 0, sizeof(w)); + w.xSelectCallback = sqlite3SelectWalkNoop; w.xSelectCallback2 = selectAddSubqueryTypeInfo; w.xExprCallback = sqlite3ExprWalkNoop; w.pParse = pParse; @@ -4595,18 +6586,197 @@ void sqlite3SelectPrep( Select *p, /* The SELECT statement being coded. */ NameContext *pOuterNC /* Name context for container */ ){ - sqlite3 *db; - if( NEVER(p==0) ) return; - db = pParse->db; - if( db->mallocFailed ) return; + assert( p!=0 || pParse->db->mallocFailed ); + assert( pParse->db->pParse==pParse ); + if( pParse->db->mallocFailed ) return; if( p->selFlags & SF_HasTypeInfo ) return; sqlite3SelectExpand(pParse, p); - if( pParse->nErr || db->mallocFailed ) return; + if( pParse->nErr ) return; sqlite3ResolveSelectNames(pParse, p, pOuterNC); - if( pParse->nErr || db->mallocFailed ) return; + if( pParse->nErr ) return; sqlite3SelectAddTypeInfo(pParse, p); } +#if TREETRACE_ENABLED +/* +** Display all information about an AggInfo object +*/ +static void printAggInfo(AggInfo *pAggInfo){ + int ii; + sqlite3DebugPrintf("AggInfo %d/%p:\n", + pAggInfo->selId, pAggInfo); + for(ii=0; iinColumn; ii++){ + struct AggInfo_col *pCol = &pAggInfo->aCol[ii]; + sqlite3DebugPrintf( + "agg-column[%d] pTab=%s iTable=%d iColumn=%d iMem=%d" + " iSorterColumn=%d %s\n", + ii, pCol->pTab ? pCol->pTab->zName : "NULL", + pCol->iTable, pCol->iColumn, pAggInfo->iFirstReg+ii, + pCol->iSorterColumn, + ii>=pAggInfo->nAccumulator ? "" : " Accumulator"); + sqlite3TreeViewExpr(0, pAggInfo->aCol[ii].pCExpr, 0); + } + for(ii=0; iinFunc; ii++){ + sqlite3DebugPrintf("agg-func[%d]: iMem=%d\n", + ii, pAggInfo->iFirstReg+pAggInfo->nColumn+ii); + sqlite3TreeViewExpr(0, pAggInfo->aFunc[ii].pFExpr, 0); + } +} +#endif /* TREETRACE_ENABLED */ + +/* +** Analyze the arguments to aggregate functions. Create new pAggInfo->aCol[] +** entries for columns that are arguments to aggregate functions but which +** are not otherwise used. +** +** The aCol[] entries in AggInfo prior to nAccumulator are columns that +** are referenced outside of aggregate functions. These might be columns +** that are part of the GROUP by clause, for example. Other database engines +** would throw an error if there is a column reference that is not in the +** GROUP BY clause and that is not part of an aggregate function argument. +** But SQLite allows this. +** +** The aCol[] entries beginning with the aCol[nAccumulator] and following +** are column references that are used exclusively as arguments to +** aggregate functions. This routine is responsible for computing +** (or recomputing) those aCol[] entries. +*/ +static void analyzeAggFuncArgs( + AggInfo *pAggInfo, + NameContext *pNC +){ + int i; + assert( pAggInfo!=0 ); + assert( pAggInfo->iFirstReg==0 ); + pNC->ncFlags |= NC_InAggFunc; + for(i=0; inFunc; i++){ + Expr *pExpr = pAggInfo->aFunc[i].pFExpr; + assert( pExpr->op==TK_FUNCTION || pExpr->op==TK_AGG_FUNCTION ); + assert( ExprUseXList(pExpr) ); + sqlite3ExprAnalyzeAggList(pNC, pExpr->x.pList); + if( pExpr->pLeft ){ + assert( pExpr->pLeft->op==TK_ORDER ); + assert( ExprUseXList(pExpr->pLeft) ); + sqlite3ExprAnalyzeAggList(pNC, pExpr->pLeft->x.pList); + } +#ifndef SQLITE_OMIT_WINDOWFUNC + assert( !IsWindowFunc(pExpr) ); + if( ExprHasProperty(pExpr, EP_WinFunc) ){ + sqlite3ExprAnalyzeAggregates(pNC, pExpr->y.pWin->pFilter); + } +#endif + } + pNC->ncFlags &= ~NC_InAggFunc; +} + +/* +** An index on expressions is being used in the inner loop of an +** aggregate query with a GROUP BY clause. This routine attempts +** to adjust the AggInfo object to take advantage of index and to +** perhaps use the index as a covering index. +** +*/ +static void optimizeAggregateUseOfIndexedExpr( + Parse *pParse, /* Parsing context */ + Select *pSelect, /* The SELECT statement being processed */ + AggInfo *pAggInfo, /* The aggregate info */ + NameContext *pNC /* Name context used to resolve agg-func args */ +){ + assert( pAggInfo->iFirstReg==0 ); + assert( pSelect!=0 ); + assert( pSelect->pGroupBy!=0 ); + pAggInfo->nColumn = pAggInfo->nAccumulator; + if( ALWAYS(pAggInfo->nSortingColumn>0) ){ + int mx = pSelect->pGroupBy->nExpr - 1; + int j, k; + for(j=0; jnColumn; j++){ + k = pAggInfo->aCol[j].iSorterColumn; + if( k>mx ) mx = k; + } + pAggInfo->nSortingColumn = mx+1; + } + analyzeAggFuncArgs(pAggInfo, pNC); +#if TREETRACE_ENABLED + if( sqlite3TreeTrace & 0x20 ){ + IndexedExpr *pIEpr; + TREETRACE(0x20, pParse, pSelect, + ("AggInfo (possibly) adjusted for Indexed Exprs\n")); + sqlite3TreeViewSelect(0, pSelect, 0); + for(pIEpr=pParse->pIdxEpr; pIEpr; pIEpr=pIEpr->pIENext){ + printf("data-cursor=%d index={%d,%d}\n", + pIEpr->iDataCur, pIEpr->iIdxCur, pIEpr->iIdxCol); + sqlite3TreeViewExpr(0, pIEpr->pExpr, 0); + } + printAggInfo(pAggInfo); + } +#else + UNUSED_PARAMETER(pSelect); + UNUSED_PARAMETER(pParse); +#endif +} + +/* +** Walker callback for aggregateConvertIndexedExprRefToColumn(). +*/ +static int aggregateIdxEprRefToColCallback(Walker *pWalker, Expr *pExpr){ + AggInfo *pAggInfo; + struct AggInfo_col *pCol; + UNUSED_PARAMETER(pWalker); + if( pExpr->pAggInfo==0 ) return WRC_Continue; + if( pExpr->op==TK_AGG_COLUMN ) return WRC_Continue; + if( pExpr->op==TK_AGG_FUNCTION ) return WRC_Continue; + if( pExpr->op==TK_IF_NULL_ROW ) return WRC_Continue; + pAggInfo = pExpr->pAggInfo; + if( NEVER(pExpr->iAgg>=pAggInfo->nColumn) ) return WRC_Continue; + assert( pExpr->iAgg>=0 ); + pCol = &pAggInfo->aCol[pExpr->iAgg]; + pExpr->op = TK_AGG_COLUMN; + pExpr->iTable = pCol->iTable; + pExpr->iColumn = pCol->iColumn; + ExprClearProperty(pExpr, EP_Skip|EP_Collate|EP_Unlikely); + return WRC_Prune; +} + +/* +** Convert every pAggInfo->aFunc[].pExpr such that any node within +** those expressions that has pAppInfo set is changed into a TK_AGG_COLUMN +** opcode. +*/ +static void aggregateConvertIndexedExprRefToColumn(AggInfo *pAggInfo){ + int i; + Walker w; + memset(&w, 0, sizeof(w)); + w.xExprCallback = aggregateIdxEprRefToColCallback; + for(i=0; inFunc; i++){ + sqlite3WalkExpr(&w, pAggInfo->aFunc[i].pFExpr); + } +} + + +/* +** Allocate a block of registers so that there is one register for each +** pAggInfo->aCol[] and pAggInfo->aFunc[] entry in pAggInfo. The first +** register in this block is stored in pAggInfo->iFirstReg. +** +** This routine may only be called once for each AggInfo object. Prior +** to calling this routine: +** +** * The aCol[] and aFunc[] arrays may be modified +** * The AggInfoColumnReg() and AggInfoFuncReg() macros may not be used +** +** After calling this routine: +** +** * The aCol[] and aFunc[] arrays are fixed +** * The AggInfoColumnReg() and AggInfoFuncReg() macros may be used +** +*/ +static void assignAggregateRegisters(Parse *pParse, AggInfo *pAggInfo){ + assert( pAggInfo!=0 ); + assert( pAggInfo->iFirstReg==0 ); + pAggInfo->iFirstReg = pParse->nMem + 1; + pParse->nMem += pAggInfo->nColumn + pAggInfo->nFunc; +} + /* ** Reset the aggregate accumulator. ** @@ -4620,35 +6790,60 @@ static void resetAccumulator(Parse *pParse, AggInfo *pAggInfo){ int i; struct AggInfo_func *pFunc; int nReg = pAggInfo->nFunc + pAggInfo->nColumn; + assert( pAggInfo->iFirstReg>0 ); + assert( pParse->db->pParse==pParse ); + assert( pParse->db->mallocFailed==0 || pParse->nErr!=0 ); if( nReg==0 ) return; -#ifdef SQLITE_DEBUG - /* Verify that all AggInfo registers are within the range specified by - ** AggInfo.mnReg..AggInfo.mxReg */ - assert( nReg==pAggInfo->mxReg-pAggInfo->mnReg+1 ); - for(i=0; inColumn; i++){ - assert( pAggInfo->aCol[i].iMem>=pAggInfo->mnReg - && pAggInfo->aCol[i].iMem<=pAggInfo->mxReg ); - } - for(i=0; inFunc; i++){ - assert( pAggInfo->aFunc[i].iMem>=pAggInfo->mnReg - && pAggInfo->aFunc[i].iMem<=pAggInfo->mxReg ); - } -#endif - sqlite3VdbeAddOp3(v, OP_Null, 0, pAggInfo->mnReg, pAggInfo->mxReg); + if( pParse->nErr ) return; + sqlite3VdbeAddOp3(v, OP_Null, 0, pAggInfo->iFirstReg, + pAggInfo->iFirstReg+nReg-1); for(pFunc=pAggInfo->aFunc, i=0; inFunc; i++, pFunc++){ if( pFunc->iDistinct>=0 ){ - Expr *pE = pFunc->pExpr; - assert( !ExprHasProperty(pE, EP_xIsSelect) ); + Expr *pE = pFunc->pFExpr; + assert( ExprUseXList(pE) ); if( pE->x.pList==0 || pE->x.pList->nExpr!=1 ){ sqlite3ErrorMsg(pParse, "DISTINCT aggregates must have exactly one " "argument"); pFunc->iDistinct = -1; }else{ - KeyInfo *pKeyInfo = keyInfoFromExprList(pParse, pE->x.pList, 0, 0); - sqlite3VdbeAddOp4(v, OP_OpenEphemeral, pFunc->iDistinct, 0, 0, - (char*)pKeyInfo, P4_KEYINFO); + KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pE->x.pList,0,0); + pFunc->iDistAddr = sqlite3VdbeAddOp4(v, OP_OpenEphemeral, + pFunc->iDistinct, 0, 0, (char*)pKeyInfo, P4_KEYINFO); + ExplainQueryPlan((pParse, 0, "USE TEMP B-TREE FOR %s(DISTINCT)", + pFunc->pFunc->zName)); } } + if( pFunc->iOBTab>=0 ){ + ExprList *pOBList; + KeyInfo *pKeyInfo; + int nExtra = 0; + assert( pFunc->pFExpr->pLeft!=0 ); + assert( pFunc->pFExpr->pLeft->op==TK_ORDER ); + assert( ExprUseXList(pFunc->pFExpr->pLeft) ); + assert( pFunc->pFunc!=0 ); + pOBList = pFunc->pFExpr->pLeft->x.pList; + if( !pFunc->bOBUnique ){ + nExtra++; /* One extra column for the OP_Sequence */ + } + if( pFunc->bOBPayload ){ + /* extra columns for the function arguments */ + assert( ExprUseXList(pFunc->pFExpr) ); + assert( pFunc->pFExpr->x.pList!=0 ); + nExtra += pFunc->pFExpr->x.pList->nExpr; + } + if( pFunc->bUseSubtype ){ + nExtra += pFunc->pFExpr->x.pList->nExpr; + } + pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pOBList, 0, nExtra); + if( !pFunc->bOBUnique && pParse->nErr==0 ){ + pKeyInfo->nKeyField++; + } + sqlite3VdbeAddOp4(v, OP_OpenEphemeral, + pFunc->iOBTab, pOBList->nExpr+nExtra, 0, + (char*)pKeyInfo, P4_KEYINFO); + ExplainQueryPlan((pParse, 0, "USE TEMP B-TREE FOR %s(ORDER BY)", + pFunc->pFunc->zName)); + } } } @@ -4661,18 +6856,82 @@ static void finalizeAggFunctions(Parse *pParse, AggInfo *pAggInfo){ int i; struct AggInfo_func *pF; for(i=0, pF=pAggInfo->aFunc; inFunc; i++, pF++){ - ExprList *pList = pF->pExpr->x.pList; - assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) ); - sqlite3VdbeAddOp4(v, OP_AggFinal, pF->iMem, pList ? pList->nExpr : 0, 0, - (void*)pF->pFunc, P4_FUNCDEF); + ExprList *pList; + assert( ExprUseXList(pF->pFExpr) ); + if( pParse->nErr ) return; + pList = pF->pFExpr->x.pList; + if( pF->iOBTab>=0 ){ + /* For an ORDER BY aggregate, calls to OP_AggStep were deferred. Inputs + ** were stored in emphermal table pF->iOBTab. Here, we extract those + ** inputs (in ORDER BY order) and make all calls to OP_AggStep + ** before doing the OP_AggFinal call. */ + int iTop; /* Start of loop for extracting columns */ + int nArg; /* Number of columns to extract */ + int nKey; /* Key columns to be skipped */ + int regAgg; /* Extract into this array */ + int j; /* Loop counter */ + + assert( pF->pFunc!=0 ); + nArg = pList->nExpr; + regAgg = sqlite3GetTempRange(pParse, nArg); + + if( pF->bOBPayload==0 ){ + nKey = 0; + }else{ + assert( pF->pFExpr->pLeft!=0 ); + assert( ExprUseXList(pF->pFExpr->pLeft) ); + assert( pF->pFExpr->pLeft->x.pList!=0 ); + nKey = pF->pFExpr->pLeft->x.pList->nExpr; + if( ALWAYS(!pF->bOBUnique) ) nKey++; + } + iTop = sqlite3VdbeAddOp1(v, OP_Rewind, pF->iOBTab); VdbeCoverage(v); + for(j=nArg-1; j>=0; j--){ + sqlite3VdbeAddOp3(v, OP_Column, pF->iOBTab, nKey+j, regAgg+j); + } + if( pF->bUseSubtype ){ + int regSubtype = sqlite3GetTempReg(pParse); + int iBaseCol = nKey + nArg + (pF->bOBPayload==0 && pF->bOBUnique==0); + for(j=nArg-1; j>=0; j--){ + sqlite3VdbeAddOp3(v, OP_Column, pF->iOBTab, iBaseCol+j, regSubtype); + sqlite3VdbeAddOp2(v, OP_SetSubtype, regSubtype, regAgg+j); + } + sqlite3ReleaseTempReg(pParse, regSubtype); + } + sqlite3VdbeAddOp3(v, OP_AggStep, 0, regAgg, AggInfoFuncReg(pAggInfo,i)); + sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF); + sqlite3VdbeChangeP5(v, (u16)nArg); + sqlite3VdbeAddOp2(v, OP_Next, pF->iOBTab, iTop+1); VdbeCoverage(v); + sqlite3VdbeJumpHere(v, iTop); + sqlite3ReleaseTempRange(pParse, regAgg, nArg); + } + sqlite3VdbeAddOp2(v, OP_AggFinal, AggInfoFuncReg(pAggInfo,i), + pList ? pList->nExpr : 0); + sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF); } } /* -** Update the accumulator memory cells for an aggregate based on -** the current cursor position. +** Generate code that will update the accumulator memory cells for an +** aggregate based on the current cursor position. +** +** If regAcc is non-zero and there are no min() or max() aggregates +** in pAggInfo, then only populate the pAggInfo->nAccumulator accumulator +** registers if register regAcc contains 0. The caller will take care +** of setting and clearing regAcc. +** +** For an ORDER BY aggregate, the actual accumulator memory cell update +** is deferred until after all input rows have been received, so that they +** can be run in the requested order. In that case, instead of invoking +** OP_AggStep to update the accumulator, just add the arguments that would +** have been passed into OP_AggStep into the sorting ephemeral table +** (along with the appropriate sort key). */ -static void updateAccumulator(Parse *pParse, AggInfo *pAggInfo){ +static void updateAccumulator( + Parse *pParse, + int regAcc, + AggInfo *pAggInfo, + int eDistinctType +){ Vdbe *v = pParse->pVdbe; int i; int regHit = 0; @@ -4680,104 +6939,686 @@ static void updateAccumulator(Parse *pParse, AggInfo *pAggInfo){ struct AggInfo_func *pF; struct AggInfo_col *pC; + assert( pAggInfo->iFirstReg>0 ); + if( pParse->nErr ) return; pAggInfo->directMode = 1; for(i=0, pF=pAggInfo->aFunc; inFunc; i++, pF++){ int nArg; int addrNext = 0; int regAgg; - ExprList *pList = pF->pExpr->x.pList; - assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) ); - if( pList ){ + int regAggSz = 0; + int regDistinct = 0; + ExprList *pList; + assert( ExprUseXList(pF->pFExpr) ); + assert( !IsWindowFunc(pF->pFExpr) ); + assert( pF->pFunc!=0 ); + pList = pF->pFExpr->x.pList; + if( ExprHasProperty(pF->pFExpr, EP_WinFunc) ){ + Expr *pFilter = pF->pFExpr->y.pWin->pFilter; + if( pAggInfo->nAccumulator + && (pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL) + && regAcc + ){ + /* If regAcc==0, there there exists some min() or max() function + ** without a FILTER clause that will ensure the magnet registers + ** are populated. */ + if( regHit==0 ) regHit = ++pParse->nMem; + /* If this is the first row of the group (regAcc contains 0), clear the + ** "magnet" register regHit so that the accumulator registers + ** are populated if the FILTER clause jumps over the the + ** invocation of min() or max() altogether. Or, if this is not + ** the first row (regAcc contains 1), set the magnet register so that + ** the accumulators are not populated unless the min()/max() is invoked + ** and indicates that they should be. */ + sqlite3VdbeAddOp2(v, OP_Copy, regAcc, regHit); + } + addrNext = sqlite3VdbeMakeLabel(pParse); + sqlite3ExprIfFalse(pParse, pFilter, addrNext, SQLITE_JUMPIFNULL); + } + if( pF->iOBTab>=0 ){ + /* Instead of invoking AggStep, we must push the arguments that would + ** have been passed to AggStep onto the sorting table. */ + int jj; /* Registered used so far in building the record */ + ExprList *pOBList; /* The ORDER BY clause */ + assert( pList!=0 ); + nArg = pList->nExpr; + assert( nArg>0 ); + assert( pF->pFExpr->pLeft!=0 ); + assert( pF->pFExpr->pLeft->op==TK_ORDER ); + assert( ExprUseXList(pF->pFExpr->pLeft) ); + pOBList = pF->pFExpr->pLeft->x.pList; + assert( pOBList!=0 ); + assert( pOBList->nExpr>0 ); + regAggSz = pOBList->nExpr; + if( !pF->bOBUnique ){ + regAggSz++; /* One register for OP_Sequence */ + } + if( pF->bOBPayload ){ + regAggSz += nArg; + } + if( pF->bUseSubtype ){ + regAggSz += nArg; + } + regAggSz++; /* One extra register to hold result of MakeRecord */ + regAgg = sqlite3GetTempRange(pParse, regAggSz); + regDistinct = regAgg; + sqlite3ExprCodeExprList(pParse, pOBList, regAgg, 0, SQLITE_ECEL_DUP); + jj = pOBList->nExpr; + if( !pF->bOBUnique ){ + sqlite3VdbeAddOp2(v, OP_Sequence, pF->iOBTab, regAgg+jj); + jj++; + } + if( pF->bOBPayload ){ + regDistinct = regAgg+jj; + sqlite3ExprCodeExprList(pParse, pList, regDistinct, 0, SQLITE_ECEL_DUP); + jj += nArg; + } + if( pF->bUseSubtype ){ + int kk; + int regBase = pF->bOBPayload ? regDistinct : regAgg; + for(kk=0; kknExpr; regAgg = sqlite3GetTempRange(pParse, nArg); + regDistinct = regAgg; sqlite3ExprCodeExprList(pParse, pList, regAgg, 0, SQLITE_ECEL_DUP); }else{ nArg = 0; regAgg = 0; } - if( pF->iDistinct>=0 ){ - addrNext = sqlite3VdbeMakeLabel(v); - testcase( nArg==0 ); /* Error condition */ - testcase( nArg>1 ); /* Also an error */ - codeDistinct(pParse, pF->iDistinct, addrNext, 1, regAgg); + if( pF->iDistinct>=0 && pList ){ + if( addrNext==0 ){ + addrNext = sqlite3VdbeMakeLabel(pParse); + } + pF->iDistinct = codeDistinct(pParse, eDistinctType, + pF->iDistinct, addrNext, pList, regDistinct); + } + if( pF->iOBTab>=0 ){ + /* Insert a new record into the ORDER BY table */ + sqlite3VdbeAddOp3(v, OP_MakeRecord, regAgg, regAggSz-1, + regAgg+regAggSz-1); + sqlite3VdbeAddOp4Int(v, OP_IdxInsert, pF->iOBTab, regAgg+regAggSz-1, + regAgg, regAggSz-1); + sqlite3ReleaseTempRange(pParse, regAgg, regAggSz); + }else{ + /* Invoke the AggStep function */ + if( pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){ + CollSeq *pColl = 0; + struct ExprList_item *pItem; + int j; + assert( pList!=0 ); /* pList!=0 if pF->pFunc has NEEDCOLL */ + for(j=0, pItem=pList->a; !pColl && jpExpr); + } + if( !pColl ){ + pColl = pParse->db->pDfltColl; + } + if( regHit==0 && pAggInfo->nAccumulator ) regHit = ++pParse->nMem; + sqlite3VdbeAddOp4(v, OP_CollSeq, regHit, 0, 0, + (char *)pColl, P4_COLLSEQ); + } + sqlite3VdbeAddOp3(v, OP_AggStep, 0, regAgg, AggInfoFuncReg(pAggInfo,i)); + sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF); + sqlite3VdbeChangeP5(v, (u16)nArg); + sqlite3ReleaseTempRange(pParse, regAgg, nArg); } - if( pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){ - CollSeq *pColl = 0; - struct ExprList_item *pItem; - int j; - assert( pList!=0 ); /* pList!=0 if pF->pFunc has NEEDCOLL */ - for(j=0, pItem=pList->a; !pColl && jpExpr); - } - if( !pColl ){ - pColl = pParse->db->pDfltColl; - } - if( regHit==0 && pAggInfo->nAccumulator ) regHit = ++pParse->nMem; - sqlite3VdbeAddOp4(v, OP_CollSeq, regHit, 0, 0, (char *)pColl, P4_COLLSEQ); - } - sqlite3VdbeAddOp4(v, OP_AggStep0, 0, regAgg, pF->iMem, - (void*)pF->pFunc, P4_FUNCDEF); - sqlite3VdbeChangeP5(v, (u8)nArg); - sqlite3ExprCacheAffinityChange(pParse, regAgg, nArg); - sqlite3ReleaseTempRange(pParse, regAgg, nArg); if( addrNext ){ sqlite3VdbeResolveLabel(v, addrNext); - sqlite3ExprCacheClear(pParse); } + if( pParse->nErr ) return; + } + if( regHit==0 && pAggInfo->nAccumulator ){ + regHit = regAcc; } - - /* Before populating the accumulator registers, clear the column cache. - ** Otherwise, if any of the required column values are already present - ** in registers, sqlite3ExprCode() may use OP_SCopy to copy the value - ** to pC->iMem. But by the time the value is used, the original register - ** may have been used, invalidating the underlying buffer holding the - ** text or blob value. See ticket [883034dcb5]. - ** - ** Another solution would be to change the OP_SCopy used to copy cached - ** values to an OP_Copy. - */ if( regHit ){ addrHitTest = sqlite3VdbeAddOp1(v, OP_If, regHit); VdbeCoverage(v); } - sqlite3ExprCacheClear(pParse); for(i=0, pC=pAggInfo->aCol; inAccumulator; i++, pC++){ - sqlite3ExprCode(pParse, pC->pExpr, pC->iMem); + sqlite3ExprCode(pParse, pC->pCExpr, AggInfoColumnReg(pAggInfo,i)); + if( pParse->nErr ) return; } + pAggInfo->directMode = 0; - sqlite3ExprCacheClear(pParse); if( addrHitTest ){ - sqlite3VdbeJumpHere(v, addrHitTest); + sqlite3VdbeJumpHereOrPopInst(v, addrHitTest); + } +} + +/* +** Add a single OP_Explain instruction to the VDBE to explain a simple +** count(*) query ("SELECT count(*) FROM pTab"). +*/ +#ifndef SQLITE_OMIT_EXPLAIN +static void explainSimpleCount( + Parse *pParse, /* Parse context */ + Table *pTab, /* Table being queried */ + Index *pIdx /* Index used to optimize scan, or NULL */ +){ + if( pParse->explain==2 ){ + int bCover = (pIdx!=0 && (HasRowid(pTab) || !IsPrimaryKeyIndex(pIdx))); + sqlite3VdbeExplain(pParse, 0, "SCAN %s%s%s", + pTab->zName, + bCover ? " USING COVERING INDEX " : "", + bCover ? pIdx->zName : "" + ); + } +} +#else +# define explainSimpleCount(a,b,c) +#endif + +/* +** sqlite3WalkExpr() callback used by havingToWhere(). +** +** If the node passed to the callback is a TK_AND node, return +** WRC_Continue to tell sqlite3WalkExpr() to iterate through child nodes. +** +** Otherwise, return WRC_Prune. In this case, also check if the +** sub-expression matches the criteria for being moved to the WHERE +** clause. If so, add it to the WHERE clause and replace the sub-expression +** within the HAVING expression with a constant "1". +*/ +static int havingToWhereExprCb(Walker *pWalker, Expr *pExpr){ + if( pExpr->op!=TK_AND ){ + Select *pS = pWalker->u.pSelect; + /* This routine is called before the HAVING clause of the current + ** SELECT is analyzed for aggregates. So if pExpr->pAggInfo is set + ** here, it indicates that the expression is a correlated reference to a + ** column from an outer aggregate query, or an aggregate function that + ** belongs to an outer query. Do not move the expression to the WHERE + ** clause in this obscure case, as doing so may corrupt the outer Select + ** statements AggInfo structure. */ + if( sqlite3ExprIsConstantOrGroupBy(pWalker->pParse, pExpr, pS->pGroupBy) + && ExprAlwaysFalse(pExpr)==0 + && pExpr->pAggInfo==0 + ){ + sqlite3 *db = pWalker->pParse->db; + Expr *pNew = sqlite3Expr(db, TK_INTEGER, "1"); + if( pNew ){ + Expr *pWhere = pS->pWhere; + SWAP(Expr, *pNew, *pExpr); + pNew = sqlite3ExprAnd(pWalker->pParse, pWhere, pNew); + pS->pWhere = pNew; + pWalker->eCode = 1; + } + } + return WRC_Prune; + } + return WRC_Continue; +} + +/* +** Transfer eligible terms from the HAVING clause of a query, which is +** processed after grouping, to the WHERE clause, which is processed before +** grouping. For example, the query: +** +** SELECT * FROM WHERE a=? GROUP BY b HAVING b=? AND c=? +** +** can be rewritten as: +** +** SELECT * FROM WHERE a=? AND b=? GROUP BY b HAVING c=? +** +** A term of the HAVING expression is eligible for transfer if it consists +** entirely of constants and expressions that are also GROUP BY terms that +** use the "BINARY" collation sequence. +*/ +static void havingToWhere(Parse *pParse, Select *p){ + Walker sWalker; + memset(&sWalker, 0, sizeof(sWalker)); + sWalker.pParse = pParse; + sWalker.xExprCallback = havingToWhereExprCb; + sWalker.u.pSelect = p; + sqlite3WalkExpr(&sWalker, p->pHaving); +#if TREETRACE_ENABLED + if( sWalker.eCode && (sqlite3TreeTrace & 0x100)!=0 ){ + TREETRACE(0x100,pParse,p,("Move HAVING terms into WHERE:\n")); + sqlite3TreeViewSelect(0, p, 0); + } +#endif +} + +/* +** Check to see if the pThis entry of pTabList is a self-join of another view. +** Search FROM-clause entries in the range of iFirst..iEnd, including iFirst +** but stopping before iEnd. +** +** If pThis is a self-join, then return the SrcItem for the first other +** instance of that view found. If pThis is not a self-join then return 0. +*/ +static SrcItem *isSelfJoinView( + SrcList *pTabList, /* Search for self-joins in this FROM clause */ + SrcItem *pThis, /* Search for prior reference to this subquery */ + int iFirst, int iEnd /* Range of FROM-clause entries to search. */ +){ + SrcItem *pItem; + Select *pSel; + assert( pThis->fg.isSubquery ); + pSel = pThis->u4.pSubq->pSelect; + assert( pSel!=0 ); + if( pSel->selFlags & SF_PushDown ) return 0; + while( iFirsta[iFirst++]; + if( !pItem->fg.isSubquery ) continue; + if( pItem->fg.viaCoroutine ) continue; + if( pItem->zName==0 ) continue; + assert( pItem->pSTab!=0 ); + assert( pThis->pSTab!=0 ); + if( pItem->pSTab->pSchema!=pThis->pSTab->pSchema ) continue; + if( sqlite3_stricmp(pItem->zName, pThis->zName)!=0 ) continue; + pS1 = pItem->u4.pSubq->pSelect; + if( pItem->pSTab->pSchema==0 && pSel->selId!=pS1->selId ){ + /* The query flattener left two different CTE tables with identical + ** names in the same FROM clause. */ + continue; + } + if( pS1->selFlags & SF_PushDown ){ + /* The view was modified by some other optimization such as + ** pushDownWhereTerms() */ + continue; + } + return pItem; + } + return 0; +} + +/* +** Deallocate a single AggInfo object +*/ +static void agginfoFree(sqlite3 *db, void *pArg){ + AggInfo *p = (AggInfo*)pArg; + sqlite3DbFree(db, p->aCol); + sqlite3DbFree(db, p->aFunc); + sqlite3DbFreeNN(db, p); +} + +/* +** Attempt to transform a query of the form +** +** SELECT count(*) FROM (SELECT x FROM t1 UNION ALL SELECT y FROM t2) +** +** Into this: +** +** SELECT (SELECT count(*) FROM t1)+(SELECT count(*) FROM t2) +** +** The transformation only works if all of the following are true: +** +** * The subquery is a UNION ALL of two or more terms +** * The subquery does not have a LIMIT clause +** * There is no WHERE or GROUP BY or HAVING clauses on the subqueries +** * The outer query is a simple count(*) with no WHERE clause or other +** extraneous syntax. +** * None of the subqueries are DISTINCT (forumpost/a860f5fb2e 2025-03-10) +** +** Return TRUE if the optimization is undertaken. +*/ +static int countOfViewOptimization(Parse *pParse, Select *p){ + Select *pSub, *pPrior; + Expr *pExpr; + Expr *pCount; + sqlite3 *db; + SrcItem *pFrom; + if( (p->selFlags & SF_Aggregate)==0 ) return 0; /* This is an aggregate */ + if( p->pEList->nExpr!=1 ) return 0; /* Single result column */ + if( p->pWhere ) return 0; + if( p->pHaving ) return 0; + if( p->pGroupBy ) return 0; + if( p->pOrderBy ) return 0; + pExpr = p->pEList->a[0].pExpr; + if( pExpr->op!=TK_AGG_FUNCTION ) return 0; /* Result is an aggregate */ + assert( ExprUseUToken(pExpr) ); + if( sqlite3_stricmp(pExpr->u.zToken,"count") ) return 0; /* Is count() */ + assert( ExprUseXList(pExpr) ); + if( pExpr->x.pList!=0 ) return 0; /* Must be count(*) */ + if( p->pSrc->nSrc!=1 ) return 0; /* One table in FROM */ + if( ExprHasProperty(pExpr, EP_WinFunc) ) return 0;/* Not a window function */ + pFrom = p->pSrc->a; + if( pFrom->fg.isSubquery==0 ) return 0; /* FROM is a subquery */ + pSub = pFrom->u4.pSubq->pSelect; + if( pSub->pPrior==0 ) return 0; /* Must be a compound */ + if( pSub->selFlags & SF_CopyCte ) return 0; /* Not a CTE */ + do{ + if( pSub->op!=TK_ALL && pSub->pPrior ) return 0; /* Must be UNION ALL */ + if( pSub->pWhere ) return 0; /* No WHERE clause */ + if( pSub->pLimit ) return 0; /* No LIMIT clause */ + if( pSub->selFlags & (SF_Aggregate|SF_Distinct) ){ + testcase( pSub->selFlags & SF_Aggregate ); + testcase( pSub->selFlags & SF_Distinct ); + return 0; /* Not an aggregate nor DISTINCT */ + } + assert( pSub->pHaving==0 ); /* Due to the previous */ + pSub = pSub->pPrior; /* Repeat over compound */ + }while( pSub ); + + /* If we reach this point then it is OK to perform the transformation */ + + db = pParse->db; + pCount = pExpr; + pExpr = 0; + pSub = sqlite3SubqueryDetach(db, pFrom); + sqlite3SrcListDelete(db, p->pSrc); + p->pSrc = sqlite3DbMallocZero(pParse->db, SZ_SRCLIST_1); + while( pSub ){ + Expr *pTerm; + pPrior = pSub->pPrior; + pSub->pPrior = 0; + pSub->pNext = 0; + pSub->selFlags |= SF_Aggregate; + pSub->selFlags &= ~(u32)SF_Compound; + pSub->nSelectRow = 0; + sqlite3ParserAddCleanup(pParse, sqlite3ExprListDeleteGeneric, pSub->pEList); + pTerm = pPrior ? sqlite3ExprDup(db, pCount, 0) : pCount; + pSub->pEList = sqlite3ExprListAppend(pParse, 0, pTerm); + pTerm = sqlite3PExpr(pParse, TK_SELECT, 0, 0); + sqlite3PExprAddSelect(pParse, pTerm, pSub); + if( pExpr==0 ){ + pExpr = pTerm; + }else{ + pExpr = sqlite3PExpr(pParse, TK_PLUS, pTerm, pExpr); + } + pSub = pPrior; + } + p->pEList->a[0].pExpr = pExpr; + p->selFlags &= ~(u32)SF_Aggregate; + +#if TREETRACE_ENABLED + if( sqlite3TreeTrace & 0x200 ){ + TREETRACE(0x200,pParse,p,("After count-of-view optimization:\n")); + sqlite3TreeViewSelect(0, p, 0); + } +#endif + return 1; +} + +/* +** If any term of pSrc, or any SF_NestedFrom sub-query, is not the same +** as pSrcItem but has the same alias as p0, then return true. +** Otherwise return false. +*/ +static int sameSrcAlias(SrcItem *p0, SrcList *pSrc){ + int i; + for(i=0; inSrc; i++){ + SrcItem *p1 = &pSrc->a[i]; + if( p1==p0 ) continue; + if( p0->pSTab==p1->pSTab && 0==sqlite3_stricmp(p0->zAlias, p1->zAlias) ){ + return 1; + } + if( p1->fg.isSubquery + && (p1->u4.pSubq->pSelect->selFlags & SF_NestedFrom)!=0 + && sameSrcAlias(p0, p1->u4.pSubq->pSelect->pSrc) + ){ + return 1; + } + } + return 0; +} + +/* +** Return TRUE (non-zero) if the i-th entry in the pTabList SrcList can +** be implemented as a co-routine. The i-th entry is guaranteed to be +** a subquery. +** +** The subquery is implemented as a co-routine if all of the following are +** true: +** +** (1) The subquery will likely be implemented in the outer loop of +** the query. This will be the case if any one of the following +** conditions hold: +** (a) The subquery is the only term in the FROM clause +** (b) The subquery is the left-most term and a CROSS JOIN or similar +** requires it to be the outer loop +** (c) All of the following are true: +** (i) The subquery is the left-most subquery in the FROM clause +** (ii) There is nothing that would prevent the subquery from +** being used as the outer loop if the sqlite3WhereBegin() +** routine nominates it to that position. +** (iii) The query is not a UPDATE ... FROM +** (2) The subquery is not a CTE that should be materialized because +** (a) the AS MATERIALIZED keyword is used, or +** (b) the CTE is used multiple times and does not have the +** NOT MATERIALIZED keyword +** (3) The subquery is not part of a left operand for a RIGHT JOIN +** (4) The SQLITE_Coroutine optimization disable flag is not set +** (5) The subquery is not self-joined +*/ +static int fromClauseTermCanBeCoroutine( + Parse *pParse, /* Parsing context */ + SrcList *pTabList, /* FROM clause */ + int i, /* Which term of the FROM clause holds the subquery */ + int selFlags /* Flags on the SELECT statement */ +){ + SrcItem *pItem = &pTabList->a[i]; + if( pItem->fg.isCte ){ + const CteUse *pCteUse = pItem->u2.pCteUse; + if( pCteUse->eM10d==M10d_Yes ) return 0; /* (2a) */ + if( pCteUse->nUse>=2 && pCteUse->eM10d!=M10d_No ) return 0; /* (2b) */ + } + if( pTabList->a[0].fg.jointype & JT_LTORJ ) return 0; /* (3) */ + if( OptimizationDisabled(pParse->db, SQLITE_Coroutines) ) return 0; /* (4) */ + if( isSelfJoinView(pTabList, pItem, i+1, pTabList->nSrc)!=0 ){ + return 0; /* (5) */ + } + if( i==0 ){ + if( pTabList->nSrc==1 ) return 1; /* (1a) */ + if( pTabList->a[1].fg.jointype & JT_CROSS ) return 1; /* (1b) */ + if( selFlags & SF_UpdateFrom ) return 0; /* (1c-iii) */ + return 1; + } + if( selFlags & SF_UpdateFrom ) return 0; /* (1c-iii) */ + while( 1 /*exit-by-break*/ ){ + if( pItem->fg.jointype & (JT_OUTER|JT_CROSS) ) return 0; /* (1c-ii) */ + if( i==0 ) break; + i--; + pItem--; + if( pItem->fg.isSubquery ) return 0; /* (1c-i) */ + } + return 1; +} + +/* +** Argument pWhere is the WHERE clause belonging to SELECT statement p. This +** function attempts to transform expressions of the form: +** +** EXISTS (SELECT ...) +** +** into joins. For example, given +** +** CREATE TABLE sailors(sid INTEGER PRIMARY KEY, name TEXT); +** CREATE TABLE reserves(sid INT, day DATE, PRIMARY KEY(sid, day)); +** +** SELECT name FROM sailors AS S WHERE EXISTS ( +** SELECT * FROM reserves AS R WHERE S.sid = R.sid AND R.day = '2022-10-25' +** ); +** +** the SELECT statement may be transformed as follows: +** +** SELECT name FROM sailors AS S, reserves AS R +** WHERE S.sid = R.sid AND R.day = '2022-10-25'; +** +** **Approximately**. Really, we have to ensure that the FROM-clause term +** that was formerly inside the EXISTS is only executed once. This is handled +** by setting the SrcItem.fg.fromExists flag, which then causes code in +** the where.c file to exit the corresponding loop after the first successful +** match (if any). +*/ +static SQLITE_NOINLINE void existsToJoin( + Parse *pParse, /* Parsing context */ + Select *p, /* The SELECT statement being optimized */ + Expr *pWhere /* part of the WHERE clause currently being examined */ +){ + if( pParse->nErr==0 + && pWhere!=0 + && !ExprHasProperty(pWhere, EP_OuterON|EP_InnerON) + && ALWAYS(p->pSrc!=0) + && p->pSrc->nSrcop==TK_AND ){ + Expr *pRight = pWhere->pRight; + existsToJoin(pParse, p, pWhere->pLeft); + existsToJoin(pParse, p, pRight); + } + else if( pWhere->op==TK_EXISTS ){ + Select *pSub = pWhere->x.pSelect; + Expr *pSubWhere = pSub->pWhere; + if( pSub->pSrc->nSrc==1 + && (pSub->selFlags & SF_Aggregate)==0 + && !pSub->pSrc->a[0].fg.isSubquery + && pSub->pLimit==0 + && pSub->pPrior==0 + ){ + /* Before combining the sub-select with the parent, renumber the + ** cursor used by the subselect. This is because the EXISTS expression + ** might be a copy of another EXISTS expression from somewhere + ** else in the tree, and in this case it is important that it use + ** a unique cursor number. */ + sqlite3 *db = pParse->db; + int *aCsrMap = sqlite3DbMallocZero(db, (pParse->nTab+2)*sizeof(int)); + if( aCsrMap==0 ) return; + aCsrMap[0] = (pParse->nTab+1); + renumberCursors(pParse, pSub, -1, aCsrMap); + sqlite3DbFree(db, aCsrMap); + + memset(pWhere, 0, sizeof(*pWhere)); + pWhere->op = TK_INTEGER; + pWhere->u.iValue = 1; + ExprSetProperty(pWhere, EP_IntValue); + assert( p->pWhere!=0 ); + pSub->pSrc->a[0].fg.fromExists = 1; + pSub->pSrc->a[0].fg.jointype |= JT_CROSS; + p->pSrc = sqlite3SrcListAppendList(pParse, p->pSrc, pSub->pSrc); + if( pSubWhere ){ + p->pWhere = sqlite3PExpr(pParse, TK_AND, p->pWhere, pSubWhere); + pSub->pWhere = 0; + } + pSub->pSrc = 0; + sqlite3ParserAddCleanup(pParse, sqlite3SelectDeleteGeneric, pSub); +#if TREETRACE_ENABLED + if( sqlite3TreeTrace & 0x100000 ){ + TREETRACE(0x100000,pParse,p, + ("After EXISTS-to-JOIN optimization:\n")); + sqlite3TreeViewSelect(0, p, 0); + } +#endif + existsToJoin(pParse, p, pSubWhere); + } + } + } +} + +/* +** Type used for Walker callbacks by selectCheckOnClauses(). +*/ +typedef struct CheckOnCtx CheckOnCtx; +struct CheckOnCtx { + SrcList *pSrc; /* SrcList for this context */ + int iJoin; /* Cursor numbers must be =< than this */ + CheckOnCtx *pParent; /* Parent context */ +}; + +/* +** True if the SrcList passed as the only argument contains at least +** one RIGHT or FULL JOIN. False otherwise. +*/ +#define hasRightJoin(pSrc) (((pSrc)->a[0].fg.jointype & JT_LTORJ)!=0) + +/* +** The xExpr callback for the search of invalid ON clause terms. +*/ +static int selectCheckOnClausesExpr(Walker *pWalker, Expr *pExpr){ + CheckOnCtx *pCtx = pWalker->u.pCheckOnCtx; + + /* Check if pExpr is root or near-root of an ON clause constraint that needs + ** to be checked to ensure that it does not refer to tables in its FROM + ** clause to the right of itself. i.e. it is either: + ** + ** + an ON clause on an OUTER join, or + ** + an ON clause on an INNER join within a FROM that features at + ** least one RIGHT or FULL join. + */ + if( (ExprHasProperty(pExpr, EP_OuterON)) + || (ExprHasProperty(pExpr, EP_InnerON) && hasRightJoin(pCtx->pSrc)) + ){ + /* If CheckOnCtx.iJoin is already set, then fall through and process + ** this expression node as normal. Or, if CheckOnCtx.iJoin is still 0, + ** set it to the cursor number of the RHS of the join to which this + ** ON expression was attached and then iterate through the entire + ** expression. */ + assert( pCtx->iJoin==0 || pCtx->iJoin==pExpr->w.iJoin ); + if( pCtx->iJoin==0 ){ + pCtx->iJoin = pExpr->w.iJoin; + sqlite3WalkExprNN(pWalker, pExpr); + pCtx->iJoin = 0; + return WRC_Prune; + } + } + + if( pExpr->op==TK_COLUMN ){ + /* A column expression. Find the SrcList (if any) to which it refers. + ** Then, if CheckOnCtx.iJoin indicates that this expression is part of an + ** ON clause from that SrcList (i.e. if iJoin is non-zero), check that it + ** does not refer to a table to the right of CheckOnCtx.iJoin. */ + do { + SrcList *pSrc = pCtx->pSrc; + int iTab = pExpr->iTable; + if( iTab>=pSrc->a[0].iCursor && iTab<=pSrc->a[pSrc->nSrc-1].iCursor ){ + if( pCtx->iJoin && iTab>pCtx->iJoin ){ + sqlite3ErrorMsg(pWalker->pParse, + "ON clause references tables to its right"); + return WRC_Abort; + } + break; + } + pCtx = pCtx->pParent; + }while( pCtx ); + } + return WRC_Continue; +} + +/* +** The xSelect callback for the search of invalid ON clause terms. +*/ +static int selectCheckOnClausesSelect(Walker *pWalker, Select *pSelect){ + CheckOnCtx *pCtx = pWalker->u.pCheckOnCtx; + if( pSelect->pSrc==pCtx->pSrc || pSelect->pSrc->nSrc==0 ){ + return WRC_Continue; + }else{ + CheckOnCtx sCtx; + memset(&sCtx, 0, sizeof(sCtx)); + sCtx.pSrc = pSelect->pSrc; + sCtx.pParent = pCtx; + pWalker->u.pCheckOnCtx = &sCtx; + sqlite3WalkSelect(pWalker, pSelect); + pWalker->u.pCheckOnCtx = pCtx; + pSelect->selFlags &= ~SF_OnToWhere; + return WRC_Prune; } } /* -** Add a single OP_Explain instruction to the VDBE to explain a simple -** count(*) query ("SELECT count(*) FROM pTab"). +** Check all ON clauses in pSelect to verify that they do not reference +** columns to the right. */ -#ifndef SQLITE_OMIT_EXPLAIN -static void explainSimpleCount( - Parse *pParse, /* Parse context */ - Table *pTab, /* Table being queried */ - Index *pIdx /* Index used to optimize scan, or NULL */ -){ - if( pParse->explain==2 ){ - int bCover = (pIdx!=0 && (HasRowid(pTab) || !IsPrimaryKeyIndex(pIdx))); - char *zEqp = sqlite3MPrintf(pParse->db, "SCAN TABLE %s%s%s", - pTab->zName, - bCover ? " USING COVERING INDEX " : "", - bCover ? pIdx->zName : "" - ); - sqlite3VdbeAddOp4( - pParse->pVdbe, OP_Explain, pParse->iSelectId, 0, 0, zEqp, P4_DYNAMIC - ); - } +static void selectCheckOnClauses(Parse *pParse, Select *pSelect){ + Walker w; + CheckOnCtx sCtx; + assert( pSelect->selFlags & SF_OnToWhere ); + assert( pSelect->pSrc!=0 && pSelect->pSrc->nSrc>=2 ); + memset(&w, 0, sizeof(w)); + w.pParse = pParse; + w.xExprCallback = selectCheckOnClausesExpr; + w.xSelectCallback = selectCheckOnClausesSelect; + w.u.pCheckOnCtx = &sCtx; + memset(&sCtx, 0, sizeof(sCtx)); + sCtx.pSrc = pSelect->pSrc; + sqlite3WalkExprNN(&w, pSelect->pWhere); + pSelect->selFlags &= ~SF_OnToWhere; } -#else -# define explainSimpleCount(a,b,c) -#endif /* -** Generate code for the SELECT statement given in the p argument. +** Generate byte-code for the SELECT statement given in the p argument. ** ** The results are returned according to the SelectDest structure. ** See comments in sqliteInt.h for further information. @@ -4788,6 +7629,40 @@ static void explainSimpleCount( ** ** This routine does NOT free the Select structure passed in. The ** calling function needs to do that. +** +** This is a long function. The following is an outline of the processing +** steps, with tags referencing various milestones: +** +** * Resolve names and similar preparation tag-select-0100 +** * Scan of the FROM clause tag-select-0200 +** + OUTER JOIN strength reduction tag-select-0220 +** + Sub-query ORDER BY removal tag-select-0230 +** + Query flattening tag-select-0240 +** * Separate subroutine for compound-SELECT tag-select-0300 +** * WHERE-clause constant propagation tag-select-0330 +** * Count()-of-VIEW optimization tag-select-0350 +** * Scan of the FROM clause again tag-select-0400 +** + Authorize unreferenced tables tag-select-0410 +** + Predicate push-down optimization tag-select-0420 +** + Omit unused subquery columns optimization tag-select-0440 +** + Generate code to implement subqueries tag-select-0480 +** - Co-routines tag-select-0482 +** - Reuse previously computed CTE tag-select-0484 +** - REuse previously computed VIEW tag-select-0486 +** - Materialize a VIEW or CTE tag-select-0488 +** * DISTINCT ORDER BY -> GROUP BY optimization tag-select-0500 +** * Set up for ORDER BY tag-select-0600 +** * Create output table tag-select-0630 +** * Prepare registers for LIMIT tag-select-0650 +** * Setup for DISTINCT tag-select-0680 +** * Generate code for non-aggregate and non-GROUP BY tag-select-0700 +** * Generate code for aggregate and/or GROUP BY tag-select-0800 +** + GROUP BY queries tag-select-0810 +** + non-GROUP BY queries tag-select-0820 +** - Special case of count() w/o GROUP BY tag-select-0821 +** - General case of non-GROUP BY aggregates tag-select-0822 +** * Sort results, as needed tag-select-0900 +** * Internal self-checks tag-select-1000 */ int sqlite3Select( Parse *pParse, /* The parser context */ @@ -4803,81 +7678,199 @@ int sqlite3Select( Expr *pWhere; /* The WHERE clause. May be NULL */ ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */ Expr *pHaving; /* The HAVING clause. May be NULL */ + AggInfo *pAggInfo = 0; /* Aggregate information */ int rc = 1; /* Value to return from this function */ DistinctCtx sDistinct; /* Info on how to code the DISTINCT keyword */ SortCtx sSort; /* Info on how to code the ORDER BY clause */ - AggInfo sAggInfo; /* Information used by aggregate queries */ int iEnd; /* Address of the end of the query */ sqlite3 *db; /* The database connection */ - -#ifndef SQLITE_OMIT_EXPLAIN - int iRestoreSelectId = pParse->iSelectId; - pParse->iSelectId = pParse->iNextSelectId++; -#endif + ExprList *pMinMaxOrderBy = 0; /* Added ORDER BY for min/max queries */ + u8 minMaxFlag; /* Flag for min/max queries */ db = pParse->db; - if( p==0 || db->mallocFailed || pParse->nErr ){ + assert( pParse==db->pParse ); + v = sqlite3GetVdbe(pParse); + if( p==0 || pParse->nErr ){ return 1; } + assert( db->mallocFailed==0 ); if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1; - memset(&sAggInfo, 0, sizeof(sAggInfo)); -#if SELECTTRACE_ENABLED - pParse->nSelectIndent++; - SELECTTRACE(1,pParse,p, ("begin processing:\n")); - if( sqlite3SelectTrace & 0x100 ){ - sqlite3TreeViewSelect(0, p, 0); +#if TREETRACE_ENABLED + TREETRACE(0x1,pParse,p, ("begin processing:\n", pParse->addrExplain)); + if( sqlite3TreeTrace & 0x10000 ){ + if( (sqlite3TreeTrace & 0x10001)==0x10000 ){ + sqlite3TreeViewLine(0, "In sqlite3Select() at %s:%d", + __FILE__, __LINE__); + } + sqlite3ShowSelect(p); } #endif + /* tag-select-0100 */ assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistFifo ); assert( p->pOrderBy==0 || pDest->eDest!=SRT_Fifo ); assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistQueue ); assert( p->pOrderBy==0 || pDest->eDest!=SRT_Queue ); - if( IgnorableOrderby(pDest) ){ - assert(pDest->eDest==SRT_Exists || pDest->eDest==SRT_Union || - pDest->eDest==SRT_Except || pDest->eDest==SRT_Discard || - pDest->eDest==SRT_Queue || pDest->eDest==SRT_DistFifo || - pDest->eDest==SRT_DistQueue || pDest->eDest==SRT_Fifo); - /* If ORDER BY makes no difference in the output then neither does - ** DISTINCT so it can be removed too. */ - sqlite3ExprListDelete(db, p->pOrderBy); - p->pOrderBy = 0; - p->selFlags &= ~SF_Distinct; + if( IgnorableDistinct(pDest) ){ + assert(pDest->eDest==SRT_Exists || pDest->eDest==SRT_Union || + pDest->eDest==SRT_Except || pDest->eDest==SRT_Discard || + pDest->eDest==SRT_DistQueue || pDest->eDest==SRT_DistFifo ); + /* All of these destinations are also able to ignore the ORDER BY clause */ + if( p->pOrderBy ){ +#if TREETRACE_ENABLED + TREETRACE(0x800,pParse,p, ("dropping superfluous ORDER BY:\n")); + if( sqlite3TreeTrace & 0x800 ){ + sqlite3TreeViewExprList(0, p->pOrderBy, 0, "ORDERBY"); + } +#endif + sqlite3ParserAddCleanup(pParse, sqlite3ExprListDeleteGeneric, + p->pOrderBy); + testcase( pParse->earlyCleanup ); + p->pOrderBy = 0; + } + p->selFlags &= ~(u32)SF_Distinct; + p->selFlags |= SF_NoopOrderBy; } sqlite3SelectPrep(pParse, p, 0); - memset(&sSort, 0, sizeof(sSort)); - sSort.pOrderBy = p->pOrderBy; - pTabList = p->pSrc; - if( pParse->nErr || db->mallocFailed ){ + if( pParse->nErr ){ goto select_end; } + assert( db->mallocFailed==0 ); assert( p->pEList!=0 ); - isAgg = (p->selFlags & SF_Aggregate)!=0; -#if SELECTTRACE_ENABLED - if( sqlite3SelectTrace & 0x100 ){ - SELECTTRACE(0x100,pParse,p, ("after name resolution:\n")); +#if TREETRACE_ENABLED + if( sqlite3TreeTrace & 0x10 ){ + TREETRACE(0x10,pParse,p, ("after name resolution:\n")); sqlite3TreeViewSelect(0, p, 0); } #endif - - /* If writing to memory or generating a set - ** only a single column may be output. + /* If the SELECT statement contains ON clauses that were moved into + ** the WHERE clause, go through and verify that none of the terms + ** in the ON clauses reference tables to the right of the ON clause. + ** Do this now, after name resolution, but before query flattening */ -#ifndef SQLITE_OMIT_SUBQUERY - if( checkForMultiColumnSelectError(pParse, pDest, p->pEList->nExpr) ){ + if( p->selFlags & SF_OnToWhere ){ + selectCheckOnClauses(pParse, p); + if( pParse->nErr ){ + goto select_end; + } + } + + /* If the SF_UFSrcCheck flag is set, then this function is being called + ** as part of populating the temp table for an UPDATE...FROM statement. + ** In this case, it is an error if the target object (pSrc->a[0]) name + ** or alias is duplicated within FROM clause (pSrc->a[1..n]). + ** + ** Postgres disallows this case too. The reason is that some other + ** systems handle this case differently, and not all the same way, + ** which is just confusing. To avoid this, we follow PG's lead and + ** disallow it altogether. */ + if( p->selFlags & SF_UFSrcCheck ){ + SrcItem *p0 = &p->pSrc->a[0]; + if( sameSrcAlias(p0, p->pSrc) ){ + sqlite3ErrorMsg(pParse, + "target object/alias may not appear in FROM clause: %s", + p0->zAlias ? p0->zAlias : p0->pSTab->zName + ); + goto select_end; + } + + /* Clear the SF_UFSrcCheck flag. The check has already been performed, + ** and leaving this flag set can cause errors if a compound sub-query + ** in p->pSrc is flattened into this query and this function called + ** again as part of compound SELECT processing. */ + p->selFlags &= ~(u32)SF_UFSrcCheck; + } + + if( pDest->eDest==SRT_Output ){ + sqlite3GenerateColumnNames(pParse, p); + } + +#ifndef SQLITE_OMIT_WINDOWFUNC + if( sqlite3WindowRewrite(pParse, p) ){ + assert( pParse->nErr ); goto select_end; } +#if TREETRACE_ENABLED + if( p->pWin && (sqlite3TreeTrace & 0x40)!=0 ){ + TREETRACE(0x40,pParse,p, ("after window rewrite:\n")); + sqlite3TreeViewSelect(0, p, 0); + } #endif +#endif /* SQLITE_OMIT_WINDOWFUNC */ + pTabList = p->pSrc; + isAgg = (p->selFlags & SF_Aggregate)!=0; + memset(&sSort, 0, sizeof(sSort)); + sSort.pOrderBy = p->pOrderBy; - /* Try to flatten subqueries in the FROM clause up into the main query + /* Try to do various optimizations (flattening subqueries, and strength + ** reduction of join operators) in the FROM clause up into the main query + ** tag-select-0200 */ #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) for(i=0; !p->pPrior && inSrc; i++){ - struct SrcList_item *pItem = &pTabList->a[i]; - Select *pSub = pItem->pSelect; - int isAggSub; - Table *pTab = pItem->pTab; + SrcItem *pItem = &pTabList->a[i]; + Select *pSub = pItem->fg.isSubquery ? pItem->u4.pSubq->pSelect : 0; + Table *pTab = pItem->pSTab; + + /* The expander should have already created transient Table objects + ** even for FROM clause elements such as subqueries that do not correspond + ** to a real table */ + assert( pTab!=0 ); + + /* Try to simplify joins: + ** + ** LEFT JOIN -> JOIN + ** RIGHT JOIN -> JOIN + ** FULL JOIN -> RIGHT JOIN + ** + ** If terms of the i-th table are used in the WHERE clause in such a + ** way that the i-th table cannot be the NULL row of a join, then + ** perform the appropriate simplification. This is called + ** "OUTER JOIN strength reduction" in the SQLite documentation. + ** tag-select-0220 + */ + if( (pItem->fg.jointype & (JT_LEFT|JT_LTORJ))!=0 + && sqlite3ExprImpliesNonNullRow(p->pWhere, pItem->iCursor, + pItem->fg.jointype & JT_LTORJ) + && OptimizationEnabled(db, SQLITE_SimplifyJoin) + ){ + if( pItem->fg.jointype & JT_LEFT ){ + if( pItem->fg.jointype & JT_RIGHT ){ + TREETRACE(0x1000,pParse,p, + ("FULL-JOIN simplifies to RIGHT-JOIN on term %d\n",i)); + pItem->fg.jointype &= ~JT_LEFT; + }else{ + TREETRACE(0x1000,pParse,p, + ("LEFT-JOIN simplifies to JOIN on term %d\n",i)); + pItem->fg.jointype &= ~(JT_LEFT|JT_OUTER); + unsetJoinExpr(p->pWhere, pItem->iCursor, 0); + } + } + if( pItem->fg.jointype & JT_LTORJ ){ + for(j=i+1; jnSrc; j++){ + SrcItem *pI2 = &pTabList->a[j]; + if( pI2->fg.jointype & JT_RIGHT ){ + if( pI2->fg.jointype & JT_LEFT ){ + TREETRACE(0x1000,pParse,p, + ("FULL-JOIN simplifies to LEFT-JOIN on term %d\n",j)); + pI2->fg.jointype &= ~JT_RIGHT; + }else{ + TREETRACE(0x1000,pParse,p, + ("RIGHT-JOIN simplifies to JOIN on term %d\n",j)); + pI2->fg.jointype &= ~(JT_RIGHT|JT_OUTER); + unsetJoinExpr(p->pWhere, pI2->iCursor, 1); + } + } + } + for(j=pTabList->nSrc-1; j>=0; j--){ + pTabList->a[j].fg.jointype &= ~JT_LTORJ; + if( pTabList->a[j].fg.jointype & JT_RIGHT ) break; + } + } + } + + /* No further action if this term of the FROM clause is not a subquery */ if( pSub==0 ) continue; /* Catch mismatch in the declared columns of a view and the number of @@ -4888,13 +7881,95 @@ int sqlite3Select( goto select_end; } - isAggSub = (pSub->selFlags & SF_Aggregate)!=0; - if( flattenSubquery(pParse, p, i, isAgg, isAggSub) ){ + /* Do not attempt the usual optimizations (flattening and ORDER BY + ** elimination) on a MATERIALIZED common table expression because + ** a MATERIALIZED common table expression is an optimization fence. + */ + if( pItem->fg.isCte && pItem->u2.pCteUse->eM10d==M10d_Yes ){ + continue; + } + + /* Do not try to flatten an aggregate subquery. + ** + ** Flattening an aggregate subquery is only possible if the outer query + ** is not a join. But if the outer query is not a join, then the subquery + ** will be implemented as a co-routine and there is no advantage to + ** flattening in that case. + */ + if( (pSub->selFlags & SF_Aggregate)!=0 ) continue; + assert( pSub->pGroupBy==0 ); + + /* tag-select-0230: + ** If a FROM-clause subquery has an ORDER BY clause that is not + ** really doing anything, then delete it now so that it does not + ** interfere with query flattening. See the discussion at + ** https://sqlite.org/forum/forumpost/2d76f2bcf65d256a + ** + ** Beware of these cases where the ORDER BY clause may not be safely + ** omitted: + ** + ** (1) There is also a LIMIT clause + ** (2) The subquery was added to help with window-function + ** processing + ** (3) The subquery is in the FROM clause of an UPDATE + ** (4) The outer query uses an aggregate function other than + ** the built-in count(), min(), or max(). + ** (5) The ORDER BY isn't going to accomplish anything because + ** one of: + ** (a) The outer query has a different ORDER BY clause + ** (b) The subquery is part of a join + ** See forum post 062d576715d277c8 + ** (6) The subquery is not a recursive CTE. ORDER BY has a different + ** meaning for recursive CTEs and this optimization does not + ** apply. + ** + ** Also retain the ORDER BY if the OmitOrderBy optimization is disabled. + */ + if( pSub->pOrderBy!=0 + && (p->pOrderBy!=0 || pTabList->nSrc>1) /* Condition (5) */ + && pSub->pLimit==0 /* Condition (1) */ + && (pSub->selFlags & (SF_OrderByReqd|SF_Recursive))==0 /* (2) and (6) */ + && (p->selFlags & SF_OrderByReqd)==0 /* Condition (3) and (4) */ + && OptimizationEnabled(db, SQLITE_OmitOrderBy) + ){ + TREETRACE(0x800,pParse,p, + ("omit superfluous ORDER BY on %r FROM-clause subquery\n",i+1)); + sqlite3ParserAddCleanup(pParse, sqlite3ExprListDeleteGeneric, + pSub->pOrderBy); + pSub->pOrderBy = 0; + } + + /* If the outer query contains a "complex" result set (that is, + ** if the result set of the outer query uses functions or subqueries) + ** and if the subquery contains an ORDER BY clause and if + ** it will be implemented as a co-routine, then do not flatten. This + ** restriction allows SQL constructs like this: + ** + ** SELECT expensive_function(x) + ** FROM (SELECT x FROM tab ORDER BY y LIMIT 10); + ** + ** The expensive_function() is only computed on the 10 rows that + ** are output, rather than every row of the table. + ** + ** The requirement that the outer query have a complex result set + ** means that flattening does occur on simpler SQL constraints without + ** the expensive_function() like: + ** + ** SELECT x FROM (SELECT x FROM tab ORDER BY y LIMIT 10); + */ + if( pSub->pOrderBy!=0 + && i==0 + && (p->selFlags & SF_ComplexResult)!=0 + && (pTabList->nSrc==1 + || (pTabList->a[1].fg.jointype&(JT_OUTER|JT_CROSS))!=0) + ){ + continue; + } + + /* tag-select-0240 */ + if( flattenSubquery(pParse, p, i, isAgg) ){ + if( pParse->nErr ) goto select_end; /* This subquery can be absorbed into its parent. */ - if( isAggSub ){ - isAgg = 1; - p->selFlags |= SF_Aggregate; - } i = -1; } pTabList = p->pSrc; @@ -4905,48 +7980,117 @@ int sqlite3Select( } #endif - /* Get a pointer the VDBE under construction, allocating a new VDBE if one - ** does not already exist */ - v = sqlite3GetVdbe(pParse); - if( v==0 ) goto select_end; - #ifndef SQLITE_OMIT_COMPOUND_SELECT /* Handle compound SELECT statements using the separate multiSelect() - ** procedure. + ** procedure. tag-select-0300 */ if( p->pPrior ){ rc = multiSelect(pParse, p, pDest); - explainSetInteger(pParse->iSelectId, iRestoreSelectId); -#if SELECTTRACE_ENABLED - SELECTTRACE(1,pParse,p,("end compound-select processing\n")); - pParse->nSelectIndent--; +#if TREETRACE_ENABLED + TREETRACE(0x400,pParse,p,("end compound-select processing\n")); + if( (sqlite3TreeTrace & 0x400)!=0 && ExplainQueryPlanParent(pParse)==0 ){ + sqlite3TreeViewSelect(0, p, 0); + } #endif + if( p->pNext==0 ) ExplainQueryPlanPop(pParse); return rc; } #endif - /* Generate code for all sub-queries in the FROM clause + /* If there may be an "EXISTS (SELECT ...)" in the WHERE clause, attempt + ** to change it into a join. */ + if( pParse->bHasExists && OptimizationEnabled(db,SQLITE_ExistsToJoin) ){ + existsToJoin(pParse, p, p->pWhere); + pTabList = p->pSrc; + } + + /* Do the WHERE-clause constant propagation optimization if this is + ** a join. No need to spend time on this operation for non-join queries + ** as the equivalent optimization will be handled by query planner in + ** sqlite3WhereBegin(). tag-select-0330 + */ + if( p->pWhere!=0 + && p->pWhere->op==TK_AND + && OptimizationEnabled(db, SQLITE_PropagateConst) + && propagateConstants(pParse, p) + ){ +#if TREETRACE_ENABLED + if( sqlite3TreeTrace & 0x2000 ){ + TREETRACE(0x2000,pParse,p,("After constant propagation:\n")); + sqlite3TreeViewSelect(0, p, 0); + } +#endif + }else{ + TREETRACE(0x2000,pParse,p,("Constant propagation not helpful\n")); + } + + /* tag-select-0350 */ + if( OptimizationEnabled(db, SQLITE_QueryFlattener|SQLITE_CountOfView) + && countOfViewOptimization(pParse, p) + ){ + if( db->mallocFailed ) goto select_end; + pTabList = p->pSrc; + } + + /* Loop over all terms in the FROM clause and do two things for each term: + ** + ** (1) Authorize unreferenced tables + ** (2) Generate code for all sub-queries + ** + ** tag-select-0400 */ -#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) for(i=0; inSrc; i++){ - struct SrcList_item *pItem = &pTabList->a[i]; + SrcItem *pItem = &pTabList->a[i]; + SrcItem *pPrior; SelectDest dest; - Select *pSub = pItem->pSelect; - if( pSub==0 ) continue; + Subquery *pSubq; + Select *pSub; +#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) + const char *zSavedAuthContext; +#endif - /* Sometimes the code for a subquery will be generated more than - ** once, if the subquery is part of the WHERE clause in a LEFT JOIN, - ** for example. In that case, do not regenerate the code to manifest - ** a view or the co-routine to implement a view. The first instance - ** is sufficient, though the subroutine to manifest the view does need - ** to be invoked again. */ - if( pItem->addrFillSub ){ - if( pItem->fg.viaCoroutine==0 ){ - sqlite3VdbeAddOp2(v, OP_Gosub, pItem->regReturn, pItem->addrFillSub); + /* Authorized unreferenced tables. tag-select-0410 + ** + ** Issue SQLITE_READ authorizations with a fake column name for any + ** tables that are referenced but from which no values are extracted. + ** Examples of where these kinds of null SQLITE_READ authorizations + ** would occur: + ** + ** SELECT count(*) FROM t1; -- SQLITE_READ t1."" + ** SELECT t1.* FROM t1, t2; -- SQLITE_READ t2."" + ** + ** The fake column name is an empty string. It is possible for a table to + ** have a column named by the empty string, in which case there is no way to + ** distinguish between an unreferenced table and an actual reference to the + ** "" column. The original design was for the fake column name to be a NULL, + ** which would be unambiguous. But legacy authorization callbacks might + ** assume the column name is non-NULL and segfault. The use of an empty + ** string for the fake column name seems safer. + */ + if( pItem->colUsed==0 && pItem->zName!=0 ){ + const char *zDb; + if( pItem->fg.fixedSchema ){ + int iDb = sqlite3SchemaToIndex(pParse->db, pItem->u4.pSchema); + zDb = db->aDb[iDb].zDbSName; + }else if( pItem->fg.isSubquery ){ + zDb = 0; + }else{ + zDb = pItem->u4.zDatabase; } - continue; + sqlite3AuthCheck(pParse, SQLITE_READ, pItem->zName, "", zDb); } +#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) + /* Generate code for all sub-queries in the FROM clause + */ + if( pItem->fg.isSubquery==0 ) continue; + pSubq = pItem->u4.pSubq; + assert( pSubq!=0 ); + pSub = pSubq->pSelect; + + /* The code for a subquery should only be generated once. */ + if( pSubq->addrFillSub!=0 ) continue; + /* Increment Parse.nHeight by the height of the largest expression ** tree referred to by this, the parent select. The child select ** may contain expression trees of at most @@ -4958,77 +8102,139 @@ int sqlite3Select( /* Make copies of constant WHERE-clause terms in the outer query down ** inside the subquery. This can help the subquery to run more efficiently. + ** This is the "predicate push-down optimization". tag-select-0420 */ - if( (pItem->fg.jointype & JT_OUTER)==0 - && pushDownWhereTerms(db, pSub, p->pWhere, pItem->iCursor) + if( OptimizationEnabled(db, SQLITE_PushDown) + && (pItem->fg.isCte==0 + || (pItem->u2.pCteUse->eM10d!=M10d_Yes && pItem->u2.pCteUse->nUse<2)) + && pushDownWhereTerms(pParse, pSub, p->pWhere, pTabList, i) ){ -#if SELECTTRACE_ENABLED - if( sqlite3SelectTrace & 0x100 ){ - SELECTTRACE(0x100,pParse,p,("After WHERE-clause push-down:\n")); +#if TREETRACE_ENABLED + if( sqlite3TreeTrace & 0x4000 ){ + TREETRACE(0x4000,pParse,p, + ("After WHERE-clause push-down into subquery %d:\n", pSub->selId)); sqlite3TreeViewSelect(0, p, 0); } #endif + assert( pSubq->pSelect && (pSub->selFlags & SF_PushDown)!=0 ); + }else{ + TREETRACE(0x4000,pParse,p,("WHERE-clause push-down not possible\n")); } - /* Generate code to implement the subquery + /* Convert unused result columns of the subquery into simple NULL + ** expressions, to avoid unneeded searching and computation. + ** tag-select-0440 */ - if( pTabList->nSrc==1 - && (p->selFlags & SF_All)==0 - && OptimizationEnabled(db, SQLITE_SubqCoroutine) + if( OptimizationEnabled(db, SQLITE_NullUnusedCols) + && disableUnusedSubqueryResultColumns(pItem) ){ +#if TREETRACE_ENABLED + if( sqlite3TreeTrace & 0x4000 ){ + TREETRACE(0x4000,pParse,p, + ("Change unused result columns to NULL for subquery %d:\n", + pSub->selId)); + sqlite3TreeViewSelect(0, p, 0); + } +#endif + } + + zSavedAuthContext = pParse->zAuthContext; + pParse->zAuthContext = pItem->zName; + + /* Generate byte-code to implement the subquery tag-select-0480 + */ + if( fromClauseTermCanBeCoroutine(pParse, pTabList, i, p->selFlags) ){ /* Implement a co-routine that will return a single row of the result - ** set on each invocation. + ** set on each invocation. tag-select-0482 */ int addrTop = sqlite3VdbeCurrentAddr(v)+1; - pItem->regReturn = ++pParse->nMem; - sqlite3VdbeAddOp3(v, OP_InitCoroutine, pItem->regReturn, 0, addrTop); - VdbeComment((v, "%s", pItem->pTab->zName)); - pItem->addrFillSub = addrTop; - sqlite3SelectDestInit(&dest, SRT_Coroutine, pItem->regReturn); - explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId); + + pSubq->regReturn = ++pParse->nMem; + sqlite3VdbeAddOp3(v, OP_InitCoroutine, pSubq->regReturn, 0, addrTop); + VdbeComment((v, "%!S", pItem)); + pSubq->addrFillSub = addrTop; + sqlite3SelectDestInit(&dest, SRT_Coroutine, pSubq->regReturn); + ExplainQueryPlan((pParse, 1, "CO-ROUTINE %!S", pItem)); sqlite3Select(pParse, pSub, &dest); - pItem->pTab->nRowLogEst = sqlite3LogEst(pSub->nSelectRow); + pItem->pSTab->nRowLogEst = pSub->nSelectRow; pItem->fg.viaCoroutine = 1; - pItem->regResult = dest.iSdst; - sqlite3VdbeEndCoroutine(v, pItem->regReturn); + pSubq->regResult = dest.iSdst; + sqlite3VdbeEndCoroutine(v, pSubq->regReturn); + VdbeComment((v, "end %!S", pItem)); sqlite3VdbeJumpHere(v, addrTop-1); sqlite3ClearTempRegCache(pParse); + }else if( pItem->fg.isCte && pItem->u2.pCteUse->addrM9e>0 ){ + /* This is a CTE for which materialization code has already been + ** generated. Invoke the subroutine to compute the materialization, + ** then make the pItem->iCursor be a copy of the ephemeral table that + ** holds the result of the materialization. tag-select-0484 */ + CteUse *pCteUse = pItem->u2.pCteUse; + sqlite3VdbeAddOp2(v, OP_Gosub, pCteUse->regRtn, pCteUse->addrM9e); + if( pItem->iCursor!=pCteUse->iCur ){ + sqlite3VdbeAddOp2(v, OP_OpenDup, pItem->iCursor, pCteUse->iCur); + VdbeComment((v, "%!S", pItem)); + } + pSub->nSelectRow = pCteUse->nRowEst; + }else if( (pPrior = isSelfJoinView(pTabList, pItem, 0, i))!=0 ){ + /* This view has already been materialized by a prior entry in + ** this same FROM clause. Reuse it. tag-select-0486 */ + Subquery *pPriorSubq; + assert( pPrior->fg.isSubquery ); + pPriorSubq = pPrior->u4.pSubq; + assert( pPriorSubq!=0 ); + if( pPriorSubq->addrFillSub ){ + sqlite3VdbeAddOp2(v, OP_Gosub, pPriorSubq->regReturn, + pPriorSubq->addrFillSub); + } + sqlite3VdbeAddOp2(v, OP_OpenDup, pItem->iCursor, pPrior->iCursor); + pSub->nSelectRow = pPriorSubq->pSelect->nSelectRow; }else{ - /* Generate a subroutine that will fill an ephemeral table with - ** the content of this subquery. pItem->addrFillSub will point - ** to the address of the generated subroutine. pItem->regReturn - ** is a register allocated to hold the subroutine return address - */ + /* Materialize the view. If the view is not correlated, generate a + ** subroutine to do the materialization so that subsequent uses of + ** the same view can reuse the materialization. tag-select-0488 */ int topAddr; int onceAddr = 0; - int retAddr; - assert( pItem->addrFillSub==0 ); - pItem->regReturn = ++pParse->nMem; - topAddr = sqlite3VdbeAddOp2(v, OP_Integer, 0, pItem->regReturn); - pItem->addrFillSub = topAddr+1; +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS + int addrExplain; +#endif + + pSubq->regReturn = ++pParse->nMem; + topAddr = sqlite3VdbeAddOp0(v, OP_Goto); + pSubq->addrFillSub = topAddr+1; + pItem->fg.isMaterialized = 1; if( pItem->fg.isCorrelated==0 ){ /* If the subquery is not correlated and if we are not inside of ** a trigger, then we only need to compute the value of the subquery ** once. */ - onceAddr = sqlite3CodeOnce(pParse); VdbeCoverage(v); - VdbeComment((v, "materialize \"%s\"", pItem->pTab->zName)); + onceAddr = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v); + VdbeComment((v, "materialize %!S", pItem)); }else{ - VdbeNoopComment((v, "materialize \"%s\"", pItem->pTab->zName)); + VdbeNoopComment((v, "materialize %!S", pItem)); } sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor); - explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId); + + ExplainQueryPlan2(addrExplain, (pParse, 1, "MATERIALIZE %!S", pItem)); sqlite3Select(pParse, pSub, &dest); - pItem->pTab->nRowLogEst = sqlite3LogEst(pSub->nSelectRow); + pItem->pSTab->nRowLogEst = pSub->nSelectRow; if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr); - retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn); - VdbeComment((v, "end %s", pItem->pTab->zName)); - sqlite3VdbeChangeP1(v, topAddr, retAddr); + sqlite3VdbeAddOp2(v, OP_Return, pSubq->regReturn, topAddr+1); + VdbeComment((v, "end %!S", pItem)); + sqlite3VdbeScanStatusRange(v, addrExplain, addrExplain, -1); + sqlite3VdbeJumpHere(v, topAddr); sqlite3ClearTempRegCache(pParse); + if( pItem->fg.isCte && pItem->fg.isCorrelated==0 ){ + CteUse *pCteUse = pItem->u2.pCteUse; + pCteUse->addrM9e = pSubq->addrFillSub; + pCteUse->regRtn = pSubq->regReturn; + pCteUse->iCur = pItem->iCursor; + pCteUse->nRowEst = pSub->nSelectRow; + } } if( db->mallocFailed ) goto select_end; pParse->nHeight -= sqlite3SelectExprHeight(p); - } + pParse->zAuthContext = zSavedAuthContext; #endif + } /* Various elements of the SELECT copied into local variables for ** convenience */ @@ -5038,14 +8244,16 @@ int sqlite3Select( pHaving = p->pHaving; sDistinct.isTnct = (p->selFlags & SF_Distinct)!=0; -#if SELECTTRACE_ENABLED - if( sqlite3SelectTrace & 0x400 ){ - SELECTTRACE(0x400,pParse,p,("After all FROM-clause analysis:\n")); +#if TREETRACE_ENABLED + if( sqlite3TreeTrace & 0x8000 ){ + TREETRACE(0x8000,pParse,p,("After all FROM-clause analysis:\n")); sqlite3TreeViewSelect(0, p, 0); } #endif - /* If the query is DISTINCT with an ORDER BY but is not an aggregate, and + /* tag-select-0500 + ** + ** If the query is DISTINCT with an ORDER BY but is not an aggregate, and ** if the select-list is the same as the ORDER BY list, then this query ** can be rewritten as a GROUP BY. In other words, this: ** @@ -5055,20 +8263,38 @@ int sqlite3Select( ** ** SELECT xyz FROM ... GROUP BY xyz ORDER BY xyz ** - ** The second form is preferred as a single index (or temp-table) may be - ** used for both the ORDER BY and DISTINCT processing. As originally - ** written the query must use a temp-table for at least one of the ORDER + ** The second form is preferred as a single index (or temp-table) may be + ** used for both the ORDER BY and DISTINCT processing. As originally + ** written the query must use a temp-table for at least one of the ORDER ** BY and DISTINCT, and an index or separate temp-table for the other. */ - if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct + if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct && sqlite3ExprListCompare(sSort.pOrderBy, pEList, -1)==0 + && OptimizationEnabled(db, SQLITE_GroupByOrder) +#ifndef SQLITE_OMIT_WINDOWFUNC + && p->pWin==0 +#endif ){ - p->selFlags &= ~SF_Distinct; + p->selFlags &= ~(u32)SF_Distinct; pGroupBy = p->pGroupBy = sqlite3ExprListDup(db, pEList, 0); + if( pGroupBy ){ + for(i=0; inExpr; i++){ + pGroupBy->a[i].u.x.iOrderByCol = i+1; + } + } + p->selFlags |= SF_Aggregate; /* Notice that even thought SF_Distinct has been cleared from p->selFlags, ** the sDistinct.isTnct is still set. Hence, isTnct represents the ** original setting of the SF_Distinct flag, not the current setting */ assert( sDistinct.isTnct ); + sDistinct.isTnct = 2; + +#if TREETRACE_ENABLED + if( sqlite3TreeTrace & 0x20000 ){ + TREETRACE(0x20000,pParse,p,("Transform DISTINCT into GROUP BY:\n")); + sqlite3TreeViewSelect(0, p, 0); + } +#endif } /* If there is an ORDER BY clause, then create an ephemeral index to @@ -5077,11 +8303,12 @@ int sqlite3Select( ** If that is the case, then the OP_OpenEphemeral instruction will be ** changed to an OP_Noop once we figure out that the sorting index is ** not needed. The sSort.addrSortIndex variable is used to facilitate - ** that change. + ** that change. tag-select-0600 */ if( sSort.pOrderBy ){ KeyInfo *pKeyInfo; - pKeyInfo = keyInfoFromExprList(pParse, sSort.pOrderBy, 0, pEList->nExpr); + pKeyInfo = sqlite3KeyInfoFromExprList( + pParse, sSort.pOrderBy, 0, pEList->nExpr); sSort.iECursor = pParse->nTab++; sSort.addrSortIndex = sqlite3VdbeAddOp4(v, OP_OpenEphemeral, @@ -5093,29 +8320,44 @@ int sqlite3Select( } /* If the output is destined for a temporary table, open that table. + ** tag-select-0630 */ if( pDest->eDest==SRT_EphemTab ){ sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pDest->iSDParm, pEList->nExpr); + if( p->selFlags & SF_NestedFrom ){ + /* Delete or NULL-out result columns that will never be used */ + int ii; + for(ii=pEList->nExpr-1; ii>0 && pEList->a[ii].fg.bUsed==0; ii--){ + sqlite3ExprDelete(db, pEList->a[ii].pExpr); + sqlite3DbFree(db, pEList->a[ii].zEName); + pEList->nExpr--; + } + for(ii=0; iinExpr; ii++){ + if( pEList->a[ii].fg.bUsed==0 ) pEList->a[ii].pExpr->op = TK_NULL; + } + } } - /* Set the limiter. + /* Set the limiter. tag-select-0650 */ - iEnd = sqlite3VdbeMakeLabel(v); - p->nSelectRow = LARGEST_INT64; - computeLimitRegisters(pParse, p, iEnd); + iEnd = sqlite3VdbeMakeLabel(pParse); + if( (p->selFlags & SF_FixedLimit)==0 ){ + p->nSelectRow = 320; /* 4 billion rows */ + } + if( p->pLimit ) computeLimitRegisters(pParse, p, iEnd); if( p->iLimit==0 && sSort.addrSortIndex>=0 ){ sqlite3VdbeChangeOpcode(v, sSort.addrSortIndex, OP_SorterOpen); sSort.sortFlags |= SORTFLAG_UseSorter; } - /* Open an ephemeral index to use for the distinct set. + /* Open an ephemeral index to use for the distinct set. tag-select-0680 */ if( p->selFlags & SF_Distinct ){ sDistinct.tabTnct = pParse->nTab++; sDistinct.addrTnct = sqlite3VdbeAddOp4(v, OP_OpenEphemeral, - sDistinct.tabTnct, 0, 0, - (char*)keyInfoFromExprList(pParse, p->pEList,0,0), - P4_KEYINFO); + sDistinct.tabTnct, 0, 0, + (char*)sqlite3KeyInfoFromExprList(pParse, p->pEList,0,0), + P4_KEYINFO); sqlite3VdbeChangeP5(v, BTREE_UNORDERED); sDistinct.eTnctType = WHERE_DISTINCT_UNORDERED; }else{ @@ -5123,27 +8365,45 @@ int sqlite3Select( } if( !isAgg && pGroupBy==0 ){ - /* No aggregate functions and no GROUP BY clause */ - u16 wctrlFlags = (sDistinct.isTnct ? WHERE_WANT_DISTINCT : 0); + /* No aggregate functions and no GROUP BY clause. tag-select-0700 */ + u16 wctrlFlags = (sDistinct.isTnct ? WHERE_WANT_DISTINCT : 0) + | (p->selFlags & SF_FixedLimit); +#ifndef SQLITE_OMIT_WINDOWFUNC + Window *pWin = p->pWin; /* Main window object (or NULL) */ + if( pWin ){ + sqlite3WindowCodeInit(pParse, p); + } +#endif + assert( WHERE_USE_LIMIT==SF_FixedLimit ); + /* Begin the database scan. */ + TREETRACE(0x2,pParse,p,("WhereBegin\n")); pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, sSort.pOrderBy, - p->pEList, wctrlFlags, 0); + p->pEList, p, wctrlFlags, p->nSelectRow); if( pWInfo==0 ) goto select_end; if( sqlite3WhereOutputRowCount(pWInfo) < p->nSelectRow ){ p->nSelectRow = sqlite3WhereOutputRowCount(pWInfo); + if( pDest->eDest<=SRT_DistQueue && pDest->eDest>=SRT_DistFifo ){ + /* TUNING: For a UNION CTE, because UNION is implies DISTINCT, + ** reduce the estimated output row count by 8 (LogEst 30). + ** Search for tag-20250414a to see other cases */ + p->nSelectRow -= 30; + } } if( sDistinct.isTnct && sqlite3WhereIsDistinct(pWInfo) ){ sDistinct.eTnctType = sqlite3WhereIsDistinct(pWInfo); } if( sSort.pOrderBy ){ sSort.nOBSat = sqlite3WhereIsOrdered(pWInfo); + sSort.labelOBLopt = sqlite3WhereOrderByLimitOptLabel(pWInfo); if( sSort.nOBSat==sSort.pOrderBy->nExpr ){ sSort.pOrderBy = 0; } } + TREETRACE(0x2,pParse,p,("WhereBegin returns\n")); - /* If sorting index that was created by a prior OP_OpenEphemeral + /* If sorting index that was created by a prior OP_OpenEphemeral ** instruction ended up not being needed, then change the OP_OpenEphemeral ** into an OP_Noop. */ @@ -5151,17 +8411,41 @@ int sqlite3Select( sqlite3VdbeChangeToNoop(v, sSort.addrSortIndex); } - /* Use the standard inner loop. */ - selectInnerLoop(pParse, p, pEList, -1, &sSort, &sDistinct, pDest, - sqlite3WhereContinueLabel(pWInfo), - sqlite3WhereBreakLabel(pWInfo)); - - /* End the database scan loop. - */ - sqlite3WhereEnd(pWInfo); + assert( p->pEList==pEList ); +#ifndef SQLITE_OMIT_WINDOWFUNC + if( pWin ){ + int addrGosub = sqlite3VdbeMakeLabel(pParse); + int iCont = sqlite3VdbeMakeLabel(pParse); + int iBreak = sqlite3VdbeMakeLabel(pParse); + int regGosub = ++pParse->nMem; + + sqlite3WindowCodeStep(pParse, p, pWInfo, regGosub, addrGosub); + + sqlite3VdbeAddOp2(v, OP_Goto, 0, iBreak); + sqlite3VdbeResolveLabel(v, addrGosub); + VdbeNoopComment((v, "inner-loop subroutine")); + sSort.labelOBLopt = 0; + selectInnerLoop(pParse, p, -1, &sSort, &sDistinct, pDest, iCont, iBreak); + sqlite3VdbeResolveLabel(v, iCont); + sqlite3VdbeAddOp1(v, OP_Return, regGosub); + VdbeComment((v, "end inner-loop subroutine")); + sqlite3VdbeResolveLabel(v, iBreak); + }else +#endif /* SQLITE_OMIT_WINDOWFUNC */ + { + /* Use the standard inner loop. */ + selectInnerLoop(pParse, p, -1, &sSort, &sDistinct, pDest, + sqlite3WhereContinueLabel(pWInfo), + sqlite3WhereBreakLabel(pWInfo)); + + /* End the database scan loop. + */ + TREETRACE(0x2,pParse,p,("WhereEnd\n")); + sqlite3WhereEnd(pWInfo); + } }else{ - /* This case when there exist aggregate functions or a GROUP BY clause - ** or both */ + /* This case is for when there exist aggregate functions or a GROUP BY + ** clause or both. tag-select-0800 */ NameContext sNC; /* Name context for processing aggregate information */ int iAMem; /* First Mem address for storing current GROUP BY */ int iBMem; /* First Mem address for previous GROUP BY */ @@ -5188,58 +8472,103 @@ int sqlite3Select( for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){ pItem->u.x.iAlias = 0; } - if( p->nSelectRow>100 ) p->nSelectRow = 100; + assert( 66==sqlite3LogEst(100) ); + if( p->nSelectRow>66 ) p->nSelectRow = 66; + + /* If there is both a GROUP BY and an ORDER BY clause and they are + ** identical, then it may be possible to disable the ORDER BY clause + ** on the grounds that the GROUP BY will cause elements to come out + ** in the correct order. It also may not - the GROUP BY might use a + ** database index that causes rows to be grouped together as required + ** but not actually sorted. Either way, record the fact that the + ** ORDER BY and GROUP BY clauses are the same by setting the orderByGrp + ** variable. */ + if( sSort.pOrderBy && pGroupBy->nExpr==sSort.pOrderBy->nExpr ){ + int ii; + /* The GROUP BY processing doesn't care whether rows are delivered in + ** ASC or DESC order - only that each group is returned contiguously. + ** So set the ASC/DESC flags in the GROUP BY to match those in the + ** ORDER BY to maximize the chances of rows being delivered in an + ** order that makes the ORDER BY redundant. */ + for(ii=0; iinExpr; ii++){ + u8 sortFlags; + sortFlags = sSort.pOrderBy->a[ii].fg.sortFlags & KEYINFO_ORDER_DESC; + pGroupBy->a[ii].fg.sortFlags = sortFlags; + } + if( sqlite3ExprListCompare(pGroupBy, sSort.pOrderBy, -1)==0 ){ + orderByGrp = 1; + } + } }else{ - p->nSelectRow = 1; + assert( 0==sqlite3LogEst(1) ); + p->nSelectRow = 0; } - /* If there is both a GROUP BY and an ORDER BY clause and they are - ** identical, then it may be possible to disable the ORDER BY clause - ** on the grounds that the GROUP BY will cause elements to come out - ** in the correct order. It also may not - the GROUP BY might use a - ** database index that causes rows to be grouped together as required - ** but not actually sorted. Either way, record the fact that the - ** ORDER BY and GROUP BY clauses are the same by setting the orderByGrp - ** variable. */ - if( sqlite3ExprListCompare(pGroupBy, sSort.pOrderBy, -1)==0 ){ - orderByGrp = 1; - } - /* Create a label to jump to when we want to abort the query */ - addrEnd = sqlite3VdbeMakeLabel(v); + addrEnd = sqlite3VdbeMakeLabel(pParse); /* Convert TK_COLUMN nodes into TK_AGG_COLUMN and make entries in ** sAggInfo for all TK_AGG_FUNCTION nodes in expressions of the ** SELECT statement. */ + pAggInfo = sqlite3DbMallocZero(db, sizeof(*pAggInfo) ); + if( pAggInfo ){ + sqlite3ParserAddCleanup(pParse, agginfoFree, pAggInfo); + testcase( pParse->earlyCleanup ); + } + if( db->mallocFailed ){ + goto select_end; + } + pAggInfo->selId = p->selId; +#ifdef SQLITE_DEBUG + pAggInfo->pSelect = p; +#endif memset(&sNC, 0, sizeof(sNC)); sNC.pParse = pParse; sNC.pSrcList = pTabList; - sNC.pAggInfo = &sAggInfo; - sAggInfo.mnReg = pParse->nMem+1; - sAggInfo.nSortingColumn = pGroupBy ? pGroupBy->nExpr : 0; - sAggInfo.pGroupBy = pGroupBy; + sNC.uNC.pAggInfo = pAggInfo; + VVA_ONLY( sNC.ncFlags = NC_UAggInfo; ) + pAggInfo->nSortingColumn = pGroupBy ? pGroupBy->nExpr : 0; + pAggInfo->pGroupBy = pGroupBy; sqlite3ExprAnalyzeAggList(&sNC, pEList); sqlite3ExprAnalyzeAggList(&sNC, sSort.pOrderBy); if( pHaving ){ + if( pGroupBy ){ + assert( pWhere==p->pWhere ); + assert( pHaving==p->pHaving ); + assert( pGroupBy==p->pGroupBy ); + havingToWhere(pParse, p); + pWhere = p->pWhere; + } sqlite3ExprAnalyzeAggregates(&sNC, pHaving); } - sAggInfo.nAccumulator = sAggInfo.nColumn; - for(i=0; ix.pList); - sNC.ncFlags &= ~NC_InAggFunc; + pAggInfo->nAccumulator = pAggInfo->nColumn; + if( p->pGroupBy==0 && p->pHaving==0 && pAggInfo->nFunc==1 ){ + minMaxFlag = minMaxQuery(db, pAggInfo->aFunc[0].pFExpr, &pMinMaxOrderBy); + }else{ + minMaxFlag = WHERE_ORDERBY_NORMAL; } - sAggInfo.mxReg = pParse->nMem; + analyzeAggFuncArgs(pAggInfo, &sNC); if( db->mallocFailed ) goto select_end; +#if TREETRACE_ENABLED + if( sqlite3TreeTrace & 0x20 ){ + TREETRACE(0x20,pParse,p,("After aggregate analysis %p:\n", pAggInfo)); + sqlite3TreeViewSelect(0, p, 0); + if( minMaxFlag ){ + sqlite3DebugPrintf("MIN/MAX Optimization (0x%02x) adds:\n", minMaxFlag); + sqlite3TreeViewExprList(0, pMinMaxOrderBy, 0, "ORDERBY"); + } + printAggInfo(pAggInfo); + } +#endif + /* Processing for aggregates with GROUP BY is very different and - ** much more complex than aggregates without a GROUP BY. + ** much more complex than aggregates without a GROUP BY. tag-select-0810 */ if( pGroupBy ){ KeyInfo *pKeyInfo; /* Keying information for the group by clause */ - int addr1; /* A-vs-B comparision jump */ + int addr1; /* A-vs-B comparison jump */ int addrOutputRow; /* Start of subroutine that outputs a result row */ int regOutputRow; /* Return address register for output subroutine */ int addrSetAbort; /* Set the abort flag and return */ @@ -5247,16 +8576,33 @@ int sqlite3Select( int addrSortingIdx; /* The OP_OpenEphemeral for the sorting index */ int addrReset; /* Subroutine for resetting the accumulator */ int regReset; /* Return address register for reset subroutine */ + ExprList *pDistinct = 0; + u16 distFlag = 0; + int eDist = WHERE_DISTINCT_NOOP; + + if( pAggInfo->nFunc==1 + && pAggInfo->aFunc[0].iDistinct>=0 + && ALWAYS(pAggInfo->aFunc[0].pFExpr!=0) + && ALWAYS(ExprUseXList(pAggInfo->aFunc[0].pFExpr)) + && pAggInfo->aFunc[0].pFExpr->x.pList!=0 + ){ + Expr *pExpr = pAggInfo->aFunc[0].pFExpr->x.pList->a[0].pExpr; + pExpr = sqlite3ExprDup(db, pExpr, 0); + pDistinct = sqlite3ExprListDup(db, pGroupBy, 0); + pDistinct = sqlite3ExprListAppend(pParse, pDistinct, pExpr); + distFlag = pDistinct ? (WHERE_WANT_DISTINCT|WHERE_AGG_DISTINCT) : 0; + } /* If there is a GROUP BY clause we might need a sorting index to ** implement it. Allocate that sorting index now. If it turns out ** that we do not need it after all, the OP_SorterOpen instruction - ** will be converted into a Noop. + ** will be converted into a Noop. */ - sAggInfo.sortingIdx = pParse->nTab++; - pKeyInfo = keyInfoFromExprList(pParse, pGroupBy, 0, sAggInfo.nColumn); - addrSortingIdx = sqlite3VdbeAddOp4(v, OP_SorterOpen, - sAggInfo.sortingIdx, sAggInfo.nSortingColumn, + pAggInfo->sortingIdx = pParse->nTab++; + pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pGroupBy, + 0, pAggInfo->nColumn); + addrSortingIdx = sqlite3VdbeAddOp4(v, OP_SorterOpen, + pAggInfo->sortingIdx, pAggInfo->nSortingColumn, 0, (char*)pKeyInfo, P4_KEYINFO); /* Initialize memory locations used by GROUP BY aggregate processing @@ -5264,18 +8610,17 @@ int sqlite3Select( iUseFlag = ++pParse->nMem; iAbortFlag = ++pParse->nMem; regOutputRow = ++pParse->nMem; - addrOutputRow = sqlite3VdbeMakeLabel(v); + addrOutputRow = sqlite3VdbeMakeLabel(pParse); regReset = ++pParse->nMem; - addrReset = sqlite3VdbeMakeLabel(v); + addrReset = sqlite3VdbeMakeLabel(pParse); iAMem = pParse->nMem + 1; pParse->nMem += pGroupBy->nExpr; iBMem = pParse->nMem + 1; pParse->nMem += pGroupBy->nExpr; sqlite3VdbeAddOp2(v, OP_Integer, 0, iAbortFlag); VdbeComment((v, "clear abort flag")); - sqlite3VdbeAddOp2(v, OP_Integer, 0, iUseFlag); - VdbeComment((v, "indicate accumulator empty")); sqlite3VdbeAddOp3(v, OP_Null, 0, iAMem, iAMem+pGroupBy->nExpr-1); + sqlite3ExprNullRegisterRange(pParse, iAMem, pGroupBy->nExpr); /* Begin a loop that will extract all source rows in GROUP BY order. ** This might involve two separate loops with an OP_Sort in between, or @@ -5283,10 +8628,21 @@ int sqlite3Select( ** in the right order to begin with. */ sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset); - pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, 0, - WHERE_GROUPBY | (orderByGrp ? WHERE_SORTBYGROUP : 0), 0 + TREETRACE(0x2,pParse,p,("WhereBegin\n")); + pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, pDistinct, + p, (sDistinct.isTnct==2 ? WHERE_DISTINCTBY : WHERE_GROUPBY) + | (orderByGrp ? WHERE_SORTBYGROUP : 0) | distFlag, 0 ); - if( pWInfo==0 ) goto select_end; + if( pWInfo==0 ){ + sqlite3ExprListDelete(db, pDistinct); + goto select_end; + } + if( pParse->pIdxEpr ){ + optimizeAggregateUseOfIndexedExpr(pParse, p, pAggInfo, &sNC); + } + assignAggregateRegisters(pParse, pAggInfo); + eDist = sqlite3WhereIsDistinct(pWInfo); + TREETRACE(0x2,pParse,p,("WhereBegin returns\n")); if( sqlite3WhereIsOrdered(pWInfo)==pGroupBy->nExpr ){ /* The optimizer is able to deliver rows in group by order so ** we do not have to sort. The OP_OpenEphemeral table will be @@ -5304,47 +8660,71 @@ int sqlite3Select( int nCol; int nGroupBy; - explainTempTable(pParse, +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS + int addrExp; /* Address of OP_Explain instruction */ +#endif + ExplainQueryPlan2(addrExp, (pParse, 0, "USE TEMP B-TREE FOR %s", (sDistinct.isTnct && (p->selFlags&SF_Distinct)==0) ? - "DISTINCT" : "GROUP BY"); + "DISTINCT" : "GROUP BY" + )); groupBySort = 1; nGroupBy = pGroupBy->nExpr; nCol = nGroupBy; j = nGroupBy; - for(i=0; i=j ){ + for(i=0; inColumn; i++){ + if( pAggInfo->aCol[i].iSorterColumn>=j ){ nCol++; j++; } } regBase = sqlite3GetTempRange(pParse, nCol); - sqlite3ExprCacheClear(pParse); sqlite3ExprCodeExprList(pParse, pGroupBy, regBase, 0, 0); j = nGroupBy; - for(i=0; idirectMode = 1; + for(i=0; inColumn; i++){ + struct AggInfo_col *pCol = &pAggInfo->aCol[i]; if( pCol->iSorterColumn>=j ){ - int r1 = j + regBase; - sqlite3ExprCodeGetColumnToReg(pParse, - pCol->pTab, pCol->iColumn, pCol->iTable, r1); + sqlite3ExprCode(pParse, pCol->pCExpr, j + regBase); j++; } } + pAggInfo->directMode = 0; regRecord = sqlite3GetTempReg(pParse); + sqlite3VdbeScanStatusCounters(v, addrExp, 0, sqlite3VdbeCurrentAddr(v)); sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regRecord); - sqlite3VdbeAddOp2(v, OP_SorterInsert, sAggInfo.sortingIdx, regRecord); + sqlite3VdbeAddOp2(v, OP_SorterInsert, pAggInfo->sortingIdx, regRecord); + sqlite3VdbeScanStatusRange(v, addrExp, sqlite3VdbeCurrentAddr(v)-2, -1); sqlite3ReleaseTempReg(pParse, regRecord); sqlite3ReleaseTempRange(pParse, regBase, nCol); + TREETRACE(0x2,pParse,p,("WhereEnd\n")); sqlite3WhereEnd(pWInfo); - sAggInfo.sortingIdxPTab = sortPTab = pParse->nTab++; + pAggInfo->sortingIdxPTab = sortPTab = pParse->nTab++; sortOut = sqlite3GetTempReg(pParse); + sqlite3VdbeScanStatusCounters(v, addrExp, sqlite3VdbeCurrentAddr(v), 0); sqlite3VdbeAddOp3(v, OP_OpenPseudo, sortPTab, sortOut, nCol); - sqlite3VdbeAddOp2(v, OP_SorterSort, sAggInfo.sortingIdx, addrEnd); + sqlite3VdbeAddOp2(v, OP_SorterSort, pAggInfo->sortingIdx, addrEnd); VdbeComment((v, "GROUP BY sort")); VdbeCoverage(v); - sAggInfo.useSortingIdx = 1; - sqlite3ExprCacheClear(pParse); + pAggInfo->useSortingIdx = 1; + sqlite3VdbeScanStatusRange(v, addrExp, -1, sortPTab); + sqlite3VdbeScanStatusRange(v, addrExp, -1, pAggInfo->sortingIdx); + } + /* If there are entries in pAgggInfo->aFunc[] that contain subexpressions + ** that are indexed (and that were previously identified and tagged + ** in optimizeAggregateUseOfIndexedExpr()) then those subexpressions + ** must now be converted into a TK_AGG_COLUMN node so that the value + ** is correctly pulled from the index rather than being recomputed. */ + if( pParse->pIdxEpr ){ + aggregateConvertIndexedExprRefToColumn(pAggInfo); +#if TREETRACE_ENABLED + if( sqlite3TreeTrace & 0x20 ){ + TREETRACE(0x20, pParse, p, + ("AggInfo function expressions converted to reference index\n")); + sqlite3TreeViewSelect(0, p, 0); + printAggInfo(pAggInfo); + } +#endif } /* If the index or temporary table used by the GROUP BY sort @@ -5352,9 +8732,9 @@ int sqlite3Select( ** clause, cancel the ephemeral table open coded earlier. ** ** This is an optimization - the correct answer should result regardless. - ** Use the SQLITE_GroupByOrder flag with SQLITE_TESTCTRL_OPTIMIZER to + ** Use the SQLITE_GroupByOrder flag with SQLITE_TESTCTRL_OPTIMIZER to ** disable this optimization for testing purposes. */ - if( orderByGrp && OptimizationEnabled(db, SQLITE_GroupByOrder) + if( orderByGrp && OptimizationEnabled(db, SQLITE_GroupByOrder) && (groupBySort || sqlite3WhereIsSorted(pWInfo)) ){ sSort.pOrderBy = 0; @@ -5367,18 +8747,34 @@ int sqlite3Select( ** from the previous row currently stored in a0, a1, a2... */ addrTopOfLoop = sqlite3VdbeCurrentAddr(v); - sqlite3ExprCacheClear(pParse); if( groupBySort ){ - sqlite3VdbeAddOp3(v, OP_SorterData, sAggInfo.sortingIdx, + sqlite3VdbeAddOp3(v, OP_SorterData, pAggInfo->sortingIdx, sortOut, sortPTab); } for(j=0; jnExpr; j++){ + int iOrderByCol = pGroupBy->a[j].u.x.iOrderByCol; + if( groupBySort ){ sqlite3VdbeAddOp3(v, OP_Column, sortPTab, j, iBMem+j); }else{ - sAggInfo.directMode = 1; + pAggInfo->directMode = 1; sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j); } + + if( iOrderByCol ){ + Expr *pX = p->pEList->a[iOrderByCol-1].pExpr; + Expr *pBase = sqlite3ExprSkipCollateAndLikely(pX); + while( ALWAYS(pBase!=0) && pBase->op==TK_IF_NULL_ROW ){ + pX = pBase->pLeft; + pBase = sqlite3ExprSkipCollateAndLikely(pX); + } + if( ALWAYS(pBase!=0) + && pBase->op!=TK_AGG_COLUMN + && pBase->op!=TK_REGISTER + ){ + sqlite3ExprToRegister(pX, iAMem+j); + } + } } sqlite3VdbeAddOp4(v, OP_Compare, iAMem, iBMem, pGroupBy->nExpr, (char*)sqlite3KeyInfoRef(pKeyInfo), P4_KEYINFO); @@ -5394,36 +8790,38 @@ int sqlite3Select( ** and resets the aggregate accumulator registers in preparation ** for the next GROUP BY batch. */ - sqlite3ExprCodeMove(pParse, iBMem, iAMem, pGroupBy->nExpr); sqlite3VdbeAddOp2(v, OP_Gosub, regOutputRow, addrOutputRow); - VdbeComment((v, "output one row")); + VdbeComment((v, "output one row of %d", p->selId)); + sqlite3ExprCodeMove(pParse, iBMem, iAMem, pGroupBy->nExpr); sqlite3VdbeAddOp2(v, OP_IfPos, iAbortFlag, addrEnd); VdbeCoverage(v); VdbeComment((v, "check abort flag")); sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset); - VdbeComment((v, "reset accumulator")); + VdbeComment((v, "reset accumulator %d", p->selId)); /* Update the aggregate accumulators based on the content of ** the current row */ sqlite3VdbeJumpHere(v, addr1); - updateAccumulator(pParse, &sAggInfo); + updateAccumulator(pParse, iUseFlag, pAggInfo, eDist); sqlite3VdbeAddOp2(v, OP_Integer, 1, iUseFlag); - VdbeComment((v, "indicate data in accumulator")); + VdbeComment((v, "indicate data in accumulator %d", p->selId)); /* End of the loop */ if( groupBySort ){ - sqlite3VdbeAddOp2(v, OP_SorterNext, sAggInfo.sortingIdx, addrTopOfLoop); + sqlite3VdbeAddOp2(v, OP_SorterNext, pAggInfo->sortingIdx,addrTopOfLoop); VdbeCoverage(v); }else{ + TREETRACE(0x2,pParse,p,("WhereEnd\n")); sqlite3WhereEnd(pWInfo); sqlite3VdbeChangeToNoop(v, addrSortingIdx); } + sqlite3ExprListDelete(db, pDistinct); /* Output the final row of result */ sqlite3VdbeAddOp2(v, OP_Gosub, regOutputRow, addrOutputRow); - VdbeComment((v, "output final row")); + VdbeComment((v, "output final row of %d", p->selId)); /* Jump over the subroutines */ @@ -5444,29 +8842,36 @@ int sqlite3Select( addrOutputRow = sqlite3VdbeCurrentAddr(v); sqlite3VdbeAddOp2(v, OP_IfPos, iUseFlag, addrOutputRow+2); VdbeCoverage(v); - VdbeComment((v, "Groupby result generator entry point")); + VdbeComment((v, "Groupby result generator entry point %d", p->selId)); sqlite3VdbeAddOp1(v, OP_Return, regOutputRow); - finalizeAggFunctions(pParse, &sAggInfo); + finalizeAggFunctions(pParse, pAggInfo); sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, SQLITE_JUMPIFNULL); - selectInnerLoop(pParse, p, p->pEList, -1, &sSort, + selectInnerLoop(pParse, p, -1, &sSort, &sDistinct, pDest, addrOutputRow+1, addrSetAbort); sqlite3VdbeAddOp1(v, OP_Return, regOutputRow); - VdbeComment((v, "end groupby result generator")); + VdbeComment((v, "end groupby result generator %d", p->selId)); /* Generate a subroutine that will reset the group-by accumulator */ sqlite3VdbeResolveLabel(v, addrReset); - resetAccumulator(pParse, &sAggInfo); + resetAccumulator(pParse, pAggInfo); + sqlite3VdbeAddOp2(v, OP_Integer, 0, iUseFlag); + VdbeComment((v, "indicate accumulator %d empty", p->selId)); sqlite3VdbeAddOp1(v, OP_Return, regReset); - + + if( distFlag!=0 && eDist!=WHERE_DISTINCT_NOOP ){ + struct AggInfo_func *pF = &pAggInfo->aFunc[0]; + fixDistinctOpenEph(pParse, eDist, pF->iDistinct, pF->iDistAddr); + } } /* endif pGroupBy. Begin aggregate queries without GROUP BY: */ else { - ExprList *pDel = 0; -#ifndef SQLITE_OMIT_BTREECOUNT + /* Aggregate functions without GROUP BY. tag-select-0820 */ Table *pTab; - if( (pTab = isSimpleCount(p, &sAggInfo))!=0 ){ - /* If isSimpleCount() returns a pointer to a Table structure, then + if( (pTab = isSimpleCount(p, pAggInfo))!=0 ){ + /* tag-select-0821 + ** + ** If isSimpleCount() returns a pointer to a Table structure, then ** the SQL statement is of the form: ** ** SELECT count(*) FROM @@ -5484,7 +8889,7 @@ int sqlite3Select( Index *pIdx; /* Iterator variable */ KeyInfo *pKeyInfo = 0; /* Keyinfo for scanned index */ Index *pBest = 0; /* Best index found so far */ - int iRoot = pTab->tnum; /* Root page of scanned b-tree */ + Pgno iRoot = pTab->tnum; /* Root page of scanned b-tree */ sqlite3CodeVerifySchema(pParse, iDb); sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); @@ -5495,17 +8900,19 @@ int sqlite3Select( ** ** (2013-10-03) Do not count the entries in a partial index. ** - ** In practice the KeyInfo structure will not be used. It is only + ** In practice the KeyInfo structure will not be used. It is only ** passed to keep OP_OpenRead happy. */ if( !HasRowid(pTab) ) pBest = sqlite3PrimaryKeyIndex(pTab); - for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ - if( pIdx->bUnordered==0 - && pIdx->szIdxRowszTabRow - && pIdx->pPartIdxWhere==0 - && (!pBest || pIdx->szIdxRowszIdxRow) - ){ - pBest = pIdx; + if( !p->pSrc->a[0].fg.notIndexed ){ + for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ + if( pIdx->bUnordered==0 + && pIdx->szIdxRowszTabRow + && pIdx->pPartIdxWhere==0 + && (!pBest || pIdx->szIdxRowszIdxRow) + ){ + pBest = pIdx; + } } } if( pBest ){ @@ -5514,90 +8921,98 @@ int sqlite3Select( } /* Open a read-only cursor, execute the OP_Count, close the cursor. */ - sqlite3VdbeAddOp4Int(v, OP_OpenRead, iCsr, iRoot, iDb, 1); + sqlite3VdbeAddOp4Int(v, OP_OpenRead, iCsr, (int)iRoot, iDb, 1); if( pKeyInfo ){ sqlite3VdbeChangeP4(v, -1, (char *)pKeyInfo, P4_KEYINFO); } - sqlite3VdbeAddOp2(v, OP_Count, iCsr, sAggInfo.aFunc[0].iMem); + assignAggregateRegisters(pParse, pAggInfo); + sqlite3VdbeAddOp2(v, OP_Count, iCsr, AggInfoFuncReg(pAggInfo,0)); sqlite3VdbeAddOp1(v, OP_Close, iCsr); explainSimpleCount(pParse, pTab, pBest); - }else -#endif /* SQLITE_OMIT_BTREECOUNT */ - { - /* Check if the query is of one of the following forms: - ** - ** SELECT min(x) FROM ... - ** SELECT max(x) FROM ... - ** - ** If it is, then ask the code in where.c to attempt to sort results - ** as if there was an "ORDER ON x" or "ORDER ON x DESC" clause. - ** If where.c is able to produce results sorted in this order, then - ** add vdbe code to break out of the processing loop after the - ** first iteration (since the first iteration of the loop is - ** guaranteed to operate on the row with the minimum or maximum - ** value of x, the only row required). - ** - ** A special flag must be passed to sqlite3WhereBegin() to slightly - ** modify behavior as follows: - ** - ** + If the query is a "SELECT min(x)", then the loop coded by - ** where.c should not iterate over any values with a NULL value - ** for x. - ** - ** + The optimizer code in where.c (the thing that decides which - ** index or indices to use) should place a different priority on - ** satisfying the 'ORDER BY' clause than it does in other cases. - ** Refer to code and comments in where.c for details. - */ - ExprList *pMinMax = 0; - u8 flag = WHERE_ORDERBY_NORMAL; - - assert( p->pGroupBy==0 ); - assert( flag==0 ); - if( p->pHaving==0 ){ - flag = minMaxQuery(&sAggInfo, &pMinMax); - } - assert( flag==0 || (pMinMax!=0 && pMinMax->nExpr==1) ); - - if( flag ){ - pMinMax = sqlite3ExprListDup(db, pMinMax, 0); - pDel = pMinMax; - assert( db->mallocFailed || pMinMax!=0 ); - if( !db->mallocFailed ){ - pMinMax->a[0].sortOrder = flag!=WHERE_ORDERBY_MIN ?1:0; - pMinMax->a[0].pExpr->op = TK_COLUMN; + }else{ + /* The general case of an aggregate query without GROUP BY + ** tag-select-0822 */ + int regAcc = 0; /* "populate accumulators" flag */ + ExprList *pDistinct = 0; + u16 distFlag = 0; + int eDist; + + /* If there are accumulator registers but no min() or max() functions + ** without FILTER clauses, allocate register regAcc. Register regAcc + ** will contain 0 the first time the inner loop runs, and 1 thereafter. + ** The code generated by updateAccumulator() uses this to ensure + ** that the accumulator registers are (a) updated only once if + ** there are no min() or max functions or (b) always updated for the + ** first row visited by the aggregate, so that they are updated at + ** least once even if the FILTER clause means the min() or max() + ** function visits zero rows. */ + if( pAggInfo->nAccumulator ){ + for(i=0; inFunc; i++){ + if( ExprHasProperty(pAggInfo->aFunc[i].pFExpr, EP_WinFunc) ){ + continue; + } + if( pAggInfo->aFunc[i].pFunc->funcFlags&SQLITE_FUNC_NEEDCOLL ){ + break; + } } + if( i==pAggInfo->nFunc ){ + regAcc = ++pParse->nMem; + sqlite3VdbeAddOp2(v, OP_Integer, 0, regAcc); + } + }else if( pAggInfo->nFunc==1 && pAggInfo->aFunc[0].iDistinct>=0 ){ + assert( ExprUseXList(pAggInfo->aFunc[0].pFExpr) ); + pDistinct = pAggInfo->aFunc[0].pFExpr->x.pList; + distFlag = pDistinct ? (WHERE_WANT_DISTINCT|WHERE_AGG_DISTINCT) : 0; } - + assignAggregateRegisters(pParse, pAggInfo); + /* This case runs if the aggregate has no GROUP BY clause. The ** processing is much simpler since there is only a single row ** of output. */ - resetAccumulator(pParse, &sAggInfo); - pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMax,0,flag,0); + assert( p->pGroupBy==0 ); + resetAccumulator(pParse, pAggInfo); + + /* If this query is a candidate for the min/max optimization, then + ** minMaxFlag will have been previously set to either + ** WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX and pMinMaxOrderBy will + ** be an appropriate ORDER BY expression for the optimization. + */ + assert( minMaxFlag==WHERE_ORDERBY_NORMAL || pMinMaxOrderBy!=0 ); + assert( pMinMaxOrderBy==0 || pMinMaxOrderBy->nExpr==1 ); + + TREETRACE(0x2,pParse,p,("WhereBegin\n")); + pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMaxOrderBy, + pDistinct, p, minMaxFlag|distFlag, 0); if( pWInfo==0 ){ - sqlite3ExprListDelete(db, pDel); goto select_end; } - updateAccumulator(pParse, &sAggInfo); - assert( pMinMax==0 || pMinMax->nExpr==1 ); - if( sqlite3WhereIsOrdered(pWInfo)>0 ){ - sqlite3VdbeGoto(v, sqlite3WhereBreakLabel(pWInfo)); - VdbeComment((v, "%s() by index", - (flag==WHERE_ORDERBY_MIN?"min":"max"))); + TREETRACE(0x2,pParse,p,("WhereBegin returns\n")); + eDist = sqlite3WhereIsDistinct(pWInfo); + updateAccumulator(pParse, regAcc, pAggInfo, eDist); + if( eDist!=WHERE_DISTINCT_NOOP ){ + struct AggInfo_func *pF = pAggInfo->aFunc; + if( pF ){ + fixDistinctOpenEph(pParse, eDist, pF->iDistinct, pF->iDistAddr); + } + } + + if( regAcc ) sqlite3VdbeAddOp2(v, OP_Integer, 1, regAcc); + if( minMaxFlag ){ + sqlite3WhereMinMaxOptEarlyOut(v, pWInfo); } + TREETRACE(0x2,pParse,p,("WhereEnd\n")); sqlite3WhereEnd(pWInfo); - finalizeAggFunctions(pParse, &sAggInfo); + finalizeAggFunctions(pParse, pAggInfo); } sSort.pOrderBy = 0; sqlite3ExprIfFalse(pParse, pHaving, addrEnd, SQLITE_JUMPIFNULL); - selectInnerLoop(pParse, p, p->pEList, -1, 0, 0, + selectInnerLoop(pParse, p, -1, 0, 0, pDest, addrEnd, addrEnd); - sqlite3ExprListDelete(db, pDel); } sqlite3VdbeResolveLabel(v, addrEnd); - + } /* endif aggregate query */ if( sDistinct.eTnctType==WHERE_DISTINCT_UNORDERED ){ @@ -5605,11 +9020,10 @@ int sqlite3Select( } /* If there is an ORDER BY clause, then we need to sort the results - ** and send them to the callback one by one. + ** and send them to the callback one by one. tag-select-0900 */ if( sSort.pOrderBy ){ - explainTempTable(pParse, - sSort.nOBSat>0 ? "RIGHT PART OF ORDER BY":"ORDER BY"); + assert( p->pEList==pEList ); generateSortTail(pParse, p, &sSort, pEList->nExpr, pDest); } @@ -5625,19 +9039,39 @@ int sqlite3Select( ** successful coding of the SELECT. */ select_end: - explainSetInteger(pParse->iSelectId, iRestoreSelectId); - - /* Identify column names if results of the SELECT are to be output. - */ - if( rc==SQLITE_OK && pDest->eDest==SRT_Output ){ - generateColumnNames(pParse, pTabList, pEList); + assert( db->mallocFailed==0 || db->mallocFailed==1 ); + assert( db->mallocFailed==0 || pParse->nErr!=0 ); + sqlite3ExprListDelete(db, pMinMaxOrderBy); +#ifdef SQLITE_DEBUG + /* Internal self-checks. tag-select-1000 */ + if( pAggInfo && !db->mallocFailed ){ +#if TREETRACE_ENABLED + if( sqlite3TreeTrace & 0x20 ){ + TREETRACE(0x20,pParse,p,("Finished with AggInfo\n")); + printAggInfo(pAggInfo); + } +#endif + for(i=0; inColumn; i++){ + Expr *pExpr = pAggInfo->aCol[i].pCExpr; + if( pExpr==0 ) continue; + assert( pExpr->pAggInfo==pAggInfo ); + assert( pExpr->iAgg==i ); + } + for(i=0; inFunc; i++){ + Expr *pExpr = pAggInfo->aFunc[i].pFExpr; + assert( pExpr!=0 ); + assert( pExpr->pAggInfo==pAggInfo ); + assert( pExpr->iAgg==i ); + } } +#endif - sqlite3DbFree(db, sAggInfo.aCol); - sqlite3DbFree(db, sAggInfo.aFunc); -#if SELECTTRACE_ENABLED - SELECTTRACE(1,pParse,p,("end processing\n")); - pParse->nSelectIndent--; +#if TREETRACE_ENABLED + TREETRACE(0x1,pParse,p,("end processing\n")); + if( (sqlite3TreeTrace & 0x40000)!=0 && ExplainQueryPlanParent(pParse)==0 ){ + sqlite3TreeViewSelect(0, p, 0); + } #endif + ExplainQueryPlanPop(pParse); return rc; } diff --git a/src/shell.c b/src/shell.c deleted file mode 100644 index d9631498c8..0000000000 --- a/src/shell.c +++ /dev/null @@ -1,5020 +0,0 @@ -/* -** 2001 September 15 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -************************************************************************* -** This file contains code to implement the "sqlite" command line -** utility for accessing SQLite databases. -*/ -#if (defined(_WIN32) || defined(WIN32)) && !defined(_CRT_SECURE_NO_WARNINGS) -/* This needs to come before any includes for MSVC compiler */ -#define _CRT_SECURE_NO_WARNINGS -#endif - -/* -** If requested, include the SQLite compiler options file for MSVC. -*/ -#if defined(INCLUDE_MSVC_H) -#include "msvc.h" -#endif - -/* -** No support for loadable extensions in VxWorks. -*/ -#if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION -# define SQLITE_OMIT_LOAD_EXTENSION 1 -#endif - -/* -** Enable large-file support for fopen() and friends on unix. -*/ -#ifndef SQLITE_DISABLE_LFS -# define _LARGE_FILE 1 -# ifndef _FILE_OFFSET_BITS -# define _FILE_OFFSET_BITS 64 -# endif -# define _LARGEFILE_SOURCE 1 -#endif - -#include -#include -#include -#include -#include "sqlite3.h" -#if SQLITE_USER_AUTHENTICATION -# include "sqlite3userauth.h" -#endif -#include -#include - -#if !defined(_WIN32) && !defined(WIN32) -# include -# if !defined(__RTP__) && !defined(_WRS_KERNEL) -# include -# endif -# include -# include -#endif - -#if HAVE_READLINE -# include -# include -#endif - -#if HAVE_EDITLINE -# include -#endif - -#if HAVE_EDITLINE || HAVE_READLINE - -# define shell_add_history(X) add_history(X) -# define shell_read_history(X) read_history(X) -# define shell_write_history(X) write_history(X) -# define shell_stifle_history(X) stifle_history(X) -# define shell_readline(X) readline(X) - -#elif HAVE_LINENOISE - -# include "linenoise.h" -# define shell_add_history(X) linenoiseHistoryAdd(X) -# define shell_read_history(X) linenoiseHistoryLoad(X) -# define shell_write_history(X) linenoiseHistorySave(X) -# define shell_stifle_history(X) linenoiseHistorySetMaxLen(X) -# define shell_readline(X) linenoise(X) - -#else - -# define shell_read_history(X) -# define shell_write_history(X) -# define shell_stifle_history(X) - -# define SHELL_USE_LOCAL_GETLINE 1 -#endif - - -#if defined(_WIN32) || defined(WIN32) -# include -# include -# define isatty(h) _isatty(h) -# ifndef access -# define access(f,m) _access((f),(m)) -# endif -# undef popen -# define popen _popen -# undef pclose -# define pclose _pclose -#else - /* Make sure isatty() has a prototype. */ - extern int isatty(int); - -# if !defined(__RTP__) && !defined(_WRS_KERNEL) - /* popen and pclose are not C89 functions and so are - ** sometimes omitted from the header */ - extern FILE *popen(const char*,const char*); - extern int pclose(FILE*); -# else -# define SQLITE_OMIT_POPEN 1 -# endif -#endif - -#if defined(_WIN32_WCE) -/* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty() - * thus we always assume that we have a console. That can be - * overridden with the -batch command line option. - */ -#define isatty(x) 1 -#endif - -/* ctype macros that work with signed characters */ -#define IsSpace(X) isspace((unsigned char)X) -#define IsDigit(X) isdigit((unsigned char)X) -#define ToLower(X) (char)tolower((unsigned char)X) - -/* On Windows, we normally run with output mode of TEXT so that \n characters -** are automatically translated into \r\n. However, this behavior needs -** to be disabled in some cases (ex: when generating CSV output and when -** rendering quoted strings that contain \n characters). The following -** routines take care of that. -*/ -#if defined(_WIN32) || defined(WIN32) -static void setBinaryMode(FILE *out){ - fflush(out); - _setmode(_fileno(out), _O_BINARY); -} -static void setTextMode(FILE *out){ - fflush(out); - _setmode(_fileno(out), _O_TEXT); -} -#else -# define setBinaryMode(X) -# define setTextMode(X) -#endif - - -/* True if the timer is enabled */ -static int enableTimer = 0; - -/* Return the current wall-clock time */ -static sqlite3_int64 timeOfDay(void){ - static sqlite3_vfs *clockVfs = 0; - sqlite3_int64 t; - if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0); - if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){ - clockVfs->xCurrentTimeInt64(clockVfs, &t); - }else{ - double r; - clockVfs->xCurrentTime(clockVfs, &r); - t = (sqlite3_int64)(r*86400000.0); - } - return t; -} - -#if !defined(_WIN32) && !defined(WIN32) && !defined(__minux) -#include -#include - -/* VxWorks does not support getrusage() as far as we can determine */ -#if defined(_WRS_KERNEL) || defined(__RTP__) -struct rusage { - struct timeval ru_utime; /* user CPU time used */ - struct timeval ru_stime; /* system CPU time used */ -}; -#define getrusage(A,B) memset(B,0,sizeof(*B)) -#endif - -/* Saved resource information for the beginning of an operation */ -static struct rusage sBegin; /* CPU time at start */ -static sqlite3_int64 iBegin; /* Wall-clock time at start */ - -/* -** Begin timing an operation -*/ -static void beginTimer(void){ - if( enableTimer ){ - getrusage(RUSAGE_SELF, &sBegin); - iBegin = timeOfDay(); - } -} - -/* Return the difference of two time_structs in seconds */ -static double timeDiff(struct timeval *pStart, struct timeval *pEnd){ - return (pEnd->tv_usec - pStart->tv_usec)*0.000001 + - (double)(pEnd->tv_sec - pStart->tv_sec); -} - -/* -** Print the timing results. -*/ -static void endTimer(void){ - if( enableTimer ){ - sqlite3_int64 iEnd = timeOfDay(); - struct rusage sEnd; - getrusage(RUSAGE_SELF, &sEnd); - printf("Run Time: real %.3f user %f sys %f\n", - (iEnd - iBegin)*0.001, - timeDiff(&sBegin.ru_utime, &sEnd.ru_utime), - timeDiff(&sBegin.ru_stime, &sEnd.ru_stime)); - } -} - -#define BEGIN_TIMER beginTimer() -#define END_TIMER endTimer() -#define HAS_TIMER 1 - -#elif (defined(_WIN32) || defined(WIN32)) - -#include - -/* Saved resource information for the beginning of an operation */ -static HANDLE hProcess; -static FILETIME ftKernelBegin; -static FILETIME ftUserBegin; -static sqlite3_int64 ftWallBegin; -typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME, - LPFILETIME, LPFILETIME); -static GETPROCTIMES getProcessTimesAddr = NULL; - -/* -** Check to see if we have timer support. Return 1 if necessary -** support found (or found previously). -*/ -static int hasTimer(void){ - if( getProcessTimesAddr ){ - return 1; - } else { - /* GetProcessTimes() isn't supported in WIN95 and some other Windows - ** versions. See if the version we are running on has it, and if it - ** does, save off a pointer to it and the current process handle. - */ - hProcess = GetCurrentProcess(); - if( hProcess ){ - HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll")); - if( NULL != hinstLib ){ - getProcessTimesAddr = - (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes"); - if( NULL != getProcessTimesAddr ){ - return 1; - } - FreeLibrary(hinstLib); - } - } - } - return 0; -} - -/* -** Begin timing an operation -*/ -static void beginTimer(void){ - if( enableTimer && getProcessTimesAddr ){ - FILETIME ftCreation, ftExit; - getProcessTimesAddr(hProcess,&ftCreation,&ftExit, - &ftKernelBegin,&ftUserBegin); - ftWallBegin = timeOfDay(); - } -} - -/* Return the difference of two FILETIME structs in seconds */ -static double timeDiff(FILETIME *pStart, FILETIME *pEnd){ - sqlite_int64 i64Start = *((sqlite_int64 *) pStart); - sqlite_int64 i64End = *((sqlite_int64 *) pEnd); - return (double) ((i64End - i64Start) / 10000000.0); -} - -/* -** Print the timing results. -*/ -static void endTimer(void){ - if( enableTimer && getProcessTimesAddr){ - FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd; - sqlite3_int64 ftWallEnd = timeOfDay(); - getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd); - printf("Run Time: real %.3f user %f sys %f\n", - (ftWallEnd - ftWallBegin)*0.001, - timeDiff(&ftUserBegin, &ftUserEnd), - timeDiff(&ftKernelBegin, &ftKernelEnd)); - } -} - -#define BEGIN_TIMER beginTimer() -#define END_TIMER endTimer() -#define HAS_TIMER hasTimer() - -#else -#define BEGIN_TIMER -#define END_TIMER -#define HAS_TIMER 0 -#endif - -/* -** Used to prevent warnings about unused parameters -*/ -#define UNUSED_PARAMETER(x) (void)(x) - -/* -** If the following flag is set, then command execution stops -** at an error if we are not interactive. -*/ -static int bail_on_error = 0; - -/* -** Threat stdin as an interactive input if the following variable -** is true. Otherwise, assume stdin is connected to a file or pipe. -*/ -static int stdin_is_interactive = 1; - -/* -** On Windows systems we have to know if standard output is a console -** in order to translate UTF-8 into MBCS. The following variable is -** true if translation is required. -*/ -static int stdout_is_console = 1; - -/* -** The following is the open SQLite database. We make a pointer -** to this database a static variable so that it can be accessed -** by the SIGINT handler to interrupt database processing. -*/ -static sqlite3 *globalDb = 0; - -/* -** True if an interrupt (Control-C) has been received. -*/ -static volatile int seenInterrupt = 0; - -/* -** This is the name of our program. It is set in main(), used -** in a number of other places, mostly for error messages. -*/ -static char *Argv0; - -/* -** Prompt strings. Initialized in main. Settable with -** .prompt main continue -*/ -static char mainPrompt[20]; /* First line prompt. default: "sqlite> "*/ -static char continuePrompt[20]; /* Continuation prompt. default: " ...> " */ - -/* -** Write I/O traces to the following stream. -*/ -#ifdef SQLITE_ENABLE_IOTRACE -static FILE *iotrace = 0; -#endif - -/* -** This routine works like printf in that its first argument is a -** format string and subsequent arguments are values to be substituted -** in place of % fields. The result of formatting this string -** is written to iotrace. -*/ -#ifdef SQLITE_ENABLE_IOTRACE -static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){ - va_list ap; - char *z; - if( iotrace==0 ) return; - va_start(ap, zFormat); - z = sqlite3_vmprintf(zFormat, ap); - va_end(ap); - fprintf(iotrace, "%s", z); - sqlite3_free(z); -} -#endif - - -/* -** Determines if a string is a number of not. -*/ -static int isNumber(const char *z, int *realnum){ - if( *z=='-' || *z=='+' ) z++; - if( !IsDigit(*z) ){ - return 0; - } - z++; - if( realnum ) *realnum = 0; - while( IsDigit(*z) ){ z++; } - if( *z=='.' ){ - z++; - if( !IsDigit(*z) ) return 0; - while( IsDigit(*z) ){ z++; } - if( realnum ) *realnum = 1; - } - if( *z=='e' || *z=='E' ){ - z++; - if( *z=='+' || *z=='-' ) z++; - if( !IsDigit(*z) ) return 0; - while( IsDigit(*z) ){ z++; } - if( realnum ) *realnum = 1; - } - return *z==0; -} - -/* -** A global char* and an SQL function to access its current value -** from within an SQL statement. This program used to use the -** sqlite_exec_printf() API to substitue a string into an SQL statement. -** The correct way to do this with sqlite3 is to use the bind API, but -** since the shell is built around the callback paradigm it would be a lot -** of work. Instead just use this hack, which is quite harmless. -*/ -static const char *zShellStatic = 0; -static void shellstaticFunc( - sqlite3_context *context, - int argc, - sqlite3_value **argv -){ - assert( 0==argc ); - assert( zShellStatic ); - UNUSED_PARAMETER(argc); - UNUSED_PARAMETER(argv); - sqlite3_result_text(context, zShellStatic, -1, SQLITE_STATIC); -} - - -/* -** Compute a string length that is limited to what can be stored in -** lower 30 bits of a 32-bit signed integer. -*/ -static int strlen30(const char *z){ - const char *z2 = z; - while( *z2 ){ z2++; } - return 0x3fffffff & (int)(z2 - z); -} - -/* -** This routine reads a line of text from FILE in, stores -** the text in memory obtained from malloc() and returns a pointer -** to the text. NULL is returned at end of file, or if malloc() -** fails. -** -** If zLine is not NULL then it is a malloced buffer returned from -** a previous call to this routine that may be reused. -*/ -static char *local_getline(char *zLine, FILE *in){ - int nLine = zLine==0 ? 0 : 100; - int n = 0; - - while( 1 ){ - if( n+100>nLine ){ - nLine = nLine*2 + 100; - zLine = realloc(zLine, nLine); - if( zLine==0 ) return 0; - } - if( fgets(&zLine[n], nLine - n, in)==0 ){ - if( n==0 ){ - free(zLine); - return 0; - } - zLine[n] = 0; - break; - } - while( zLine[n] ) n++; - if( n>0 && zLine[n-1]=='\n' ){ - n--; - if( n>0 && zLine[n-1]=='\r' ) n--; - zLine[n] = 0; - break; - } - } -#if defined(_WIN32) || defined(WIN32) - /* For interactive input on Windows systems, translate the - ** multi-byte characterset characters into UTF-8. */ - if( stdin_is_interactive ){ - extern char *sqlite3_win32_mbcs_to_utf8(const char*); - char *zTrans = sqlite3_win32_mbcs_to_utf8(zLine); - if( zTrans ){ - int nTrans = strlen30(zTrans)+1; - if( nTrans>nLine ){ - zLine = realloc(zLine, nTrans); - if( zLine==0 ){ - sqlite3_free(zTrans); - return 0; - } - } - memcpy(zLine, zTrans, nTrans); - sqlite3_free(zTrans); - } - } -#endif /* defined(_WIN32) || defined(WIN32) */ - return zLine; -} - -/* -** Retrieve a single line of input text. -** -** If in==0 then read from standard input and prompt before each line. -** If isContinuation is true, then a continuation prompt is appropriate. -** If isContinuation is zero, then the main prompt should be used. -** -** If zPrior is not NULL then it is a buffer from a prior call to this -** routine that can be reused. -** -** The result is stored in space obtained from malloc() and must either -** be freed by the caller or else passed back into this routine via the -** zPrior argument for reuse. -*/ -static char *one_input_line(FILE *in, char *zPrior, int isContinuation){ - char *zPrompt; - char *zResult; - if( in!=0 ){ - zResult = local_getline(zPrior, in); - }else{ - zPrompt = isContinuation ? continuePrompt : mainPrompt; -#if SHELL_USE_LOCAL_GETLINE - printf("%s", zPrompt); - fflush(stdout); - zResult = local_getline(zPrior, stdin); -#else - free(zPrior); - zResult = shell_readline(zPrompt); - if( zResult && *zResult ) shell_add_history(zResult); -#endif - } - return zResult; -} - -/* -** Render output like fprintf(). Except, if the output is going to the -** console and if this is running on a Windows machine, translate the -** output from UTF-8 into MBCS. -*/ -#if defined(_WIN32) || defined(WIN32) -void utf8_printf(FILE *out, const char *zFormat, ...){ - va_list ap; - va_start(ap, zFormat); - if( stdout_is_console && (out==stdout || out==stderr) ){ - extern char *sqlite3_win32_utf8_to_mbcs(const char*); - char *z1 = sqlite3_vmprintf(zFormat, ap); - char *z2 = sqlite3_win32_utf8_to_mbcs(z1); - sqlite3_free(z1); - fputs(z2, out); - sqlite3_free(z2); - }else{ - vfprintf(out, zFormat, ap); - } - va_end(ap); -} -#elif !defined(utf8_printf) -# define utf8_printf fprintf -#endif - -/* -** Render output like fprintf(). This should not be used on anything that -** includes string formatting (e.g. "%s"). -*/ -#if !defined(raw_printf) -# define raw_printf fprintf -#endif - -/* -** Shell output mode information from before ".explain on", -** saved so that it can be restored by ".explain off" -*/ -typedef struct SavedModeInfo SavedModeInfo; -struct SavedModeInfo { - int valid; /* Is there legit data in here? */ - int mode; /* Mode prior to ".explain on" */ - int showHeader; /* The ".header" setting prior to ".explain on" */ - int colWidth[100]; /* Column widths prior to ".explain on" */ -}; - -/* -** State information about the database connection is contained in an -** instance of the following structure. -*/ -typedef struct ShellState ShellState; -struct ShellState { - sqlite3 *db; /* The database */ - int echoOn; /* True to echo input commands */ - int autoExplain; /* Automatically turn on .explain mode */ - int autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */ - int statsOn; /* True to display memory stats before each finalize */ - int scanstatsOn; /* True to display scan stats before each finalize */ - int countChanges; /* True to display change counts */ - int backslashOn; /* Resolve C-style \x escapes in SQL input text */ - int outCount; /* Revert to stdout when reaching zero */ - int cnt; /* Number of records displayed so far */ - FILE *out; /* Write results here */ - FILE *traceOut; /* Output for sqlite3_trace() */ - int nErr; /* Number of errors seen */ - int mode; /* An output mode setting */ - int cMode; /* temporary output mode for the current query */ - int normalMode; /* Output mode before ".explain on" */ - int writableSchema; /* True if PRAGMA writable_schema=ON */ - int showHeader; /* True to show column names in List or Column mode */ - unsigned shellFlgs; /* Various flags */ - char *zDestTable; /* Name of destination table when MODE_Insert */ - char colSeparator[20]; /* Column separator character for several modes */ - char rowSeparator[20]; /* Row separator character for MODE_Ascii */ - int colWidth[100]; /* Requested width of each column when in column mode*/ - int actualWidth[100]; /* Actual width of each column */ - char nullValue[20]; /* The text to print when a NULL comes back from - ** the database */ - char outfile[FILENAME_MAX]; /* Filename for *out */ - const char *zDbFilename; /* name of the database file */ - char *zFreeOnClose; /* Filename to free when closing */ - const char *zVfs; /* Name of VFS to use */ - sqlite3_stmt *pStmt; /* Current statement if any. */ - FILE *pLog; /* Write log output here */ - int *aiIndent; /* Array of indents used in MODE_Explain */ - int nIndent; /* Size of array aiIndent[] */ - int iIndent; /* Index of current op in aiIndent[] */ -}; - -/* -** These are the allowed shellFlgs values -*/ -#define SHFLG_Scratch 0x00001 /* The --scratch option is used */ -#define SHFLG_Pagecache 0x00002 /* The --pagecache option is used */ -#define SHFLG_Lookaside 0x00004 /* Lookaside memory is used */ - -/* -** These are the allowed modes. -*/ -#define MODE_Line 0 /* One column per line. Blank line between records */ -#define MODE_Column 1 /* One record per line in neat columns */ -#define MODE_List 2 /* One record per line with a separator */ -#define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */ -#define MODE_Html 4 /* Generate an XHTML table */ -#define MODE_Insert 5 /* Generate SQL "insert" statements */ -#define MODE_Tcl 6 /* Generate ANSI-C or TCL quoted elements */ -#define MODE_Csv 7 /* Quote strings, numbers are plain */ -#define MODE_Explain 8 /* Like MODE_Column, but do not truncate data */ -#define MODE_Ascii 9 /* Use ASCII unit and record separators (0x1F/0x1E) */ - -static const char *modeDescr[] = { - "line", - "column", - "list", - "semi", - "html", - "insert", - "tcl", - "csv", - "explain", - "ascii", -}; - -/* -** These are the column/row/line separators used by the various -** import/export modes. -*/ -#define SEP_Column "|" -#define SEP_Row "\n" -#define SEP_Tab "\t" -#define SEP_Space " " -#define SEP_Comma "," -#define SEP_CrLf "\r\n" -#define SEP_Unit "\x1F" -#define SEP_Record "\x1E" - -/* -** Number of elements in an array -*/ -#define ArraySize(X) (int)(sizeof(X)/sizeof(X[0])) - -/* -** A callback for the sqlite3_log() interface. -*/ -static void shellLog(void *pArg, int iErrCode, const char *zMsg){ - ShellState *p = (ShellState*)pArg; - if( p->pLog==0 ) return; - utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg); - fflush(p->pLog); -} - -/* -** Output the given string as a hex-encoded blob (eg. X'1234' ) -*/ -static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){ - int i; - char *zBlob = (char *)pBlob; - raw_printf(out,"X'"); - for(i=0; i0 ){ - utf8_printf(out,"%.*s",i,z); - } - if( z[i]=='<' ){ - raw_printf(out,"<"); - }else if( z[i]=='&' ){ - raw_printf(out,"&"); - }else if( z[i]=='>' ){ - raw_printf(out,">"); - }else if( z[i]=='\"' ){ - raw_printf(out,"""); - }else if( z[i]=='\'' ){ - raw_printf(out,"'"); - }else{ - break; - } - z += i + 1; - } -} - -/* -** If a field contains any character identified by a 1 in the following -** array, then the string must be quoted for CSV. -*/ -static const char needCsvQuote[] = { - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -}; - -/* -** Output a single term of CSV. Actually, p->colSeparator is used for -** the separator, which may or may not be a comma. p->nullValue is -** the null value. Strings are quoted if necessary. The separator -** is only issued if bSep is true. -*/ -static void output_csv(ShellState *p, const char *z, int bSep){ - FILE *out = p->out; - if( z==0 ){ - utf8_printf(out,"%s",p->nullValue); - }else{ - int i; - int nSep = strlen30(p->colSeparator); - for(i=0; z[i]; i++){ - if( needCsvQuote[((unsigned char*)z)[i]] - || (z[i]==p->colSeparator[0] && - (nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){ - i = 0; - break; - } - } - if( i==0 ){ - putc('"', out); - for(i=0; z[i]; i++){ - if( z[i]=='"' ) putc('"', out); - putc(z[i], out); - } - putc('"', out); - }else{ - utf8_printf(out, "%s", z); - } - } - if( bSep ){ - utf8_printf(p->out, "%s", p->colSeparator); - } -} - -#ifdef SIGINT -/* -** This routine runs when the user presses Ctrl-C -*/ -static void interrupt_handler(int NotUsed){ - UNUSED_PARAMETER(NotUsed); - seenInterrupt++; - if( seenInterrupt>2 ) exit(1); - if( globalDb ) sqlite3_interrupt(globalDb); -} -#endif - -/* -** This is the callback routine that the shell -** invokes for each row of a query result. -*/ -static int shell_callback( - void *pArg, - int nArg, /* Number of result columns */ - char **azArg, /* Text of each result column */ - char **azCol, /* Column names */ - int *aiType /* Column types */ -){ - int i; - ShellState *p = (ShellState*)pArg; - - switch( p->cMode ){ - case MODE_Line: { - int w = 5; - if( azArg==0 ) break; - for(i=0; iw ) w = len; - } - if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator); - for(i=0; iout,"%*s = %s%s", w, azCol[i], - azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator); - } - break; - } - case MODE_Explain: - case MODE_Column: { - static const int aExplainWidths[] = {4, 13, 4, 4, 4, 13, 2, 13}; - const int *colWidth; - int showHdr; - char *rowSep; - if( p->cMode==MODE_Column ){ - colWidth = p->colWidth; - showHdr = p->showHeader; - rowSep = p->rowSeparator; - }else{ - colWidth = aExplainWidths; - showHdr = 1; - rowSep = SEP_Row; - } - if( p->cnt++==0 ){ - for(i=0; icolWidth) ){ - w = colWidth[i]; - }else{ - w = 0; - } - if( w==0 ){ - w = strlen30(azCol[i] ? azCol[i] : ""); - if( w<10 ) w = 10; - n = strlen30(azArg && azArg[i] ? azArg[i] : p->nullValue); - if( wactualWidth) ){ - p->actualWidth[i] = w; - } - if( showHdr ){ - if( w<0 ){ - utf8_printf(p->out,"%*.*s%s",-w,-w,azCol[i], - i==nArg-1 ? rowSep : " "); - }else{ - utf8_printf(p->out,"%-*.*s%s",w,w,azCol[i], - i==nArg-1 ? rowSep : " "); - } - } - } - if( showHdr ){ - for(i=0; iactualWidth) ){ - w = p->actualWidth[i]; - if( w<0 ) w = -w; - }else{ - w = 10; - } - utf8_printf(p->out,"%-*.*s%s",w,w, - "----------------------------------------------------------" - "----------------------------------------------------------", - i==nArg-1 ? rowSep : " "); - } - } - } - if( azArg==0 ) break; - for(i=0; iactualWidth) ){ - w = p->actualWidth[i]; - }else{ - w = 10; - } - if( p->cMode==MODE_Explain && azArg[i] && strlen30(azArg[i])>w ){ - w = strlen30(azArg[i]); - } - if( i==1 && p->aiIndent && p->pStmt ){ - if( p->iIndentnIndent ){ - utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], ""); - } - p->iIndent++; - } - if( w<0 ){ - utf8_printf(p->out,"%*.*s%s",-w,-w, - azArg[i] ? azArg[i] : p->nullValue, - i==nArg-1 ? rowSep : " "); - }else{ - utf8_printf(p->out,"%-*.*s%s",w,w, - azArg[i] ? azArg[i] : p->nullValue, - i==nArg-1 ? rowSep : " "); - } - } - break; - } - case MODE_Semi: - case MODE_List: { - if( p->cnt++==0 && p->showHeader ){ - for(i=0; iout,"%s%s",azCol[i], - i==nArg-1 ? p->rowSeparator : p->colSeparator); - } - } - if( azArg==0 ) break; - for(i=0; inullValue; - utf8_printf(p->out, "%s", z); - if( iout, "%s", p->colSeparator); - }else if( p->cMode==MODE_Semi ){ - utf8_printf(p->out, ";%s", p->rowSeparator); - }else{ - utf8_printf(p->out, "%s", p->rowSeparator); - } - } - break; - } - case MODE_Html: { - if( p->cnt++==0 && p->showHeader ){ - raw_printf(p->out,"
    "); - for(i=0; iout,"\n"); - } - raw_printf(p->out,"\n"); - } - if( azArg==0 ) break; - raw_printf(p->out,""); - for(i=0; iout,"\n"); - } - raw_printf(p->out,"\n"); - break; - } - case MODE_Tcl: { - if( p->cnt++==0 && p->showHeader ){ - for(i=0; iout,azCol[i] ? azCol[i] : ""); - if(iout, "%s", p->colSeparator); - } - utf8_printf(p->out, "%s", p->rowSeparator); - } - if( azArg==0 ) break; - for(i=0; iout, azArg[i] ? azArg[i] : p->nullValue); - if(iout, "%s", p->colSeparator); - } - utf8_printf(p->out, "%s", p->rowSeparator); - break; - } - case MODE_Csv: { - setBinaryMode(p->out); - if( p->cnt++==0 && p->showHeader ){ - for(i=0; iout, "%s", p->rowSeparator); - } - if( nArg>0 ){ - for(i=0; iout, "%s", p->rowSeparator); - } - setTextMode(p->out); - break; - } - case MODE_Insert: { - p->cnt++; - if( azArg==0 ) break; - utf8_printf(p->out,"INSERT INTO %s",p->zDestTable); - if( p->showHeader ){ - raw_printf(p->out,"("); - for(i=0; i0 ? ",": ""; - utf8_printf(p->out, "%s%s", zSep, azCol[i]); - } - raw_printf(p->out,")"); - } - raw_printf(p->out," VALUES("); - for(i=0; i0 ? ",": ""; - if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){ - utf8_printf(p->out,"%sNULL",zSep); - }else if( aiType && aiType[i]==SQLITE_TEXT ){ - if( zSep[0] ) utf8_printf(p->out,"%s",zSep); - output_quoted_string(p->out, azArg[i]); - }else if( aiType && (aiType[i]==SQLITE_INTEGER - || aiType[i]==SQLITE_FLOAT) ){ - utf8_printf(p->out,"%s%s",zSep, azArg[i]); - }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ - const void *pBlob = sqlite3_column_blob(p->pStmt, i); - int nBlob = sqlite3_column_bytes(p->pStmt, i); - if( zSep[0] ) utf8_printf(p->out,"%s",zSep); - output_hex_blob(p->out, pBlob, nBlob); - }else if( isNumber(azArg[i], 0) ){ - utf8_printf(p->out,"%s%s",zSep, azArg[i]); - }else{ - if( zSep[0] ) utf8_printf(p->out,"%s",zSep); - output_quoted_string(p->out, azArg[i]); - } - } - raw_printf(p->out,");\n"); - break; - } - case MODE_Ascii: { - if( p->cnt++==0 && p->showHeader ){ - for(i=0; i0 ) utf8_printf(p->out, "%s", p->colSeparator); - utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : ""); - } - utf8_printf(p->out, "%s", p->rowSeparator); - } - if( azArg==0 ) break; - for(i=0; i0 ) utf8_printf(p->out, "%s", p->colSeparator); - utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue); - } - utf8_printf(p->out, "%s", p->rowSeparator); - break; - } - } - return 0; -} - -/* -** This is the callback routine that the SQLite library -** invokes for each row of a query result. -*/ -static int callback(void *pArg, int nArg, char **azArg, char **azCol){ - /* since we don't have type info, call the shell_callback with a NULL value */ - return shell_callback(pArg, nArg, azArg, azCol, NULL); -} - -/* -** Set the destination table field of the ShellState structure to -** the name of the table given. Escape any quote characters in the -** table name. -*/ -static void set_table_name(ShellState *p, const char *zName){ - int i, n; - int needQuote; - char *z; - - if( p->zDestTable ){ - free(p->zDestTable); - p->zDestTable = 0; - } - if( zName==0 ) return; - needQuote = !isalpha((unsigned char)*zName) && *zName!='_'; - for(i=n=0; zName[i]; i++, n++){ - if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ){ - needQuote = 1; - if( zName[i]=='\'' ) n++; - } - } - if( needQuote ) n += 2; - z = p->zDestTable = malloc( n+1 ); - if( z==0 ){ - raw_printf(stderr,"Error: out of memory\n"); - exit(1); - } - n = 0; - if( needQuote ) z[n++] = '\''; - for(i=0; zName[i]; i++){ - z[n++] = zName[i]; - if( zName[i]=='\'' ) z[n++] = '\''; - } - if( needQuote ) z[n++] = '\''; - z[n] = 0; -} - -/* zIn is either a pointer to a NULL-terminated string in memory obtained -** from malloc(), or a NULL pointer. The string pointed to by zAppend is -** added to zIn, and the result returned in memory obtained from malloc(). -** zIn, if it was not NULL, is freed. -** -** If the third argument, quote, is not '\0', then it is used as a -** quote character for zAppend. -*/ -static char *appendText(char *zIn, char const *zAppend, char quote){ - int len; - int i; - int nAppend = strlen30(zAppend); - int nIn = (zIn?strlen30(zIn):0); - - len = nAppend+nIn+1; - if( quote ){ - len += 2; - for(i=0; idb, zSelect, -1, &pSelect, 0); - if( rc!=SQLITE_OK || !pSelect ){ - utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, - sqlite3_errmsg(p->db)); - if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++; - return rc; - } - rc = sqlite3_step(pSelect); - nResult = sqlite3_column_count(pSelect); - while( rc==SQLITE_ROW ){ - if( zFirstRow ){ - utf8_printf(p->out, "%s", zFirstRow); - zFirstRow = 0; - } - z = (const char*)sqlite3_column_text(pSelect, 0); - utf8_printf(p->out, "%s", z); - for(i=1; iout, ",%s", sqlite3_column_text(pSelect, i)); - } - if( z==0 ) z = ""; - while( z[0] && (z[0]!='-' || z[1]!='-') ) z++; - if( z[0] ){ - raw_printf(p->out, "\n;\n"); - }else{ - raw_printf(p->out, ";\n"); - } - rc = sqlite3_step(pSelect); - } - rc = sqlite3_finalize(pSelect); - if( rc!=SQLITE_OK ){ - utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, - sqlite3_errmsg(p->db)); - if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++; - } - return rc; -} - -/* -** Allocate space and save off current error string. -*/ -static char *save_err_msg( - sqlite3 *db /* Database to query */ -){ - int nErrMsg = 1+strlen30(sqlite3_errmsg(db)); - char *zErrMsg = sqlite3_malloc64(nErrMsg); - if( zErrMsg ){ - memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg); - } - return zErrMsg; -} - -/* -** Display memory stats. -*/ -static int display_stats( - sqlite3 *db, /* Database to query */ - ShellState *pArg, /* Pointer to ShellState */ - int bReset /* True to reset the stats */ -){ - int iCur; - int iHiwtr; - - if( pArg && pArg->out ){ - - iHiwtr = iCur = -1; - sqlite3_status(SQLITE_STATUS_MEMORY_USED, &iCur, &iHiwtr, bReset); - raw_printf(pArg->out, - "Memory Used: %d (max %d) bytes\n", - iCur, iHiwtr); - iHiwtr = iCur = -1; - sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &iCur, &iHiwtr, bReset); - raw_printf(pArg->out, "Number of Outstanding Allocations: %d (max %d)\n", - iCur, iHiwtr); - if( pArg->shellFlgs & SHFLG_Pagecache ){ - iHiwtr = iCur = -1; - sqlite3_status(SQLITE_STATUS_PAGECACHE_USED, &iCur, &iHiwtr, bReset); - raw_printf(pArg->out, - "Number of Pcache Pages Used: %d (max %d) pages\n", - iCur, iHiwtr); - } - iHiwtr = iCur = -1; - sqlite3_status(SQLITE_STATUS_PAGECACHE_OVERFLOW, &iCur, &iHiwtr, bReset); - raw_printf(pArg->out, - "Number of Pcache Overflow Bytes: %d (max %d) bytes\n", - iCur, iHiwtr); - if( pArg->shellFlgs & SHFLG_Scratch ){ - iHiwtr = iCur = -1; - sqlite3_status(SQLITE_STATUS_SCRATCH_USED, &iCur, &iHiwtr, bReset); - raw_printf(pArg->out, - "Number of Scratch Allocations Used: %d (max %d)\n", - iCur, iHiwtr); - } - iHiwtr = iCur = -1; - sqlite3_status(SQLITE_STATUS_SCRATCH_OVERFLOW, &iCur, &iHiwtr, bReset); - raw_printf(pArg->out, - "Number of Scratch Overflow Bytes: %d (max %d) bytes\n", - iCur, iHiwtr); - iHiwtr = iCur = -1; - sqlite3_status(SQLITE_STATUS_MALLOC_SIZE, &iCur, &iHiwtr, bReset); - raw_printf(pArg->out, "Largest Allocation: %d bytes\n", - iHiwtr); - iHiwtr = iCur = -1; - sqlite3_status(SQLITE_STATUS_PAGECACHE_SIZE, &iCur, &iHiwtr, bReset); - raw_printf(pArg->out, "Largest Pcache Allocation: %d bytes\n", - iHiwtr); - iHiwtr = iCur = -1; - sqlite3_status(SQLITE_STATUS_SCRATCH_SIZE, &iCur, &iHiwtr, bReset); - raw_printf(pArg->out, "Largest Scratch Allocation: %d bytes\n", - iHiwtr); -#ifdef YYTRACKMAXSTACKDEPTH - iHiwtr = iCur = -1; - sqlite3_status(SQLITE_STATUS_PARSER_STACK, &iCur, &iHiwtr, bReset); - raw_printf(pArg->out, "Deepest Parser Stack: %d (max %d)\n", - iCur, iHiwtr); -#endif - } - - if( pArg && pArg->out && db ){ - if( pArg->shellFlgs & SHFLG_Lookaside ){ - iHiwtr = iCur = -1; - sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED, - &iCur, &iHiwtr, bReset); - raw_printf(pArg->out, - "Lookaside Slots Used: %d (max %d)\n", - iCur, iHiwtr); - sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT, - &iCur, &iHiwtr, bReset); - raw_printf(pArg->out, "Successful lookaside attempts: %d\n", - iHiwtr); - sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, - &iCur, &iHiwtr, bReset); - raw_printf(pArg->out, "Lookaside failures due to size: %d\n", - iHiwtr); - sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, - &iCur, &iHiwtr, bReset); - raw_printf(pArg->out, "Lookaside failures due to OOM: %d\n", - iHiwtr); - } - iHiwtr = iCur = -1; - sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset); - raw_printf(pArg->out, "Pager Heap Usage: %d bytes\n", - iCur); - iHiwtr = iCur = -1; - sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1); - raw_printf(pArg->out, "Page cache hits: %d\n", iCur); - iHiwtr = iCur = -1; - sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1); - raw_printf(pArg->out, "Page cache misses: %d\n", iCur); - iHiwtr = iCur = -1; - sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1); - raw_printf(pArg->out, "Page cache writes: %d\n", iCur); - iHiwtr = iCur = -1; - sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset); - raw_printf(pArg->out, "Schema Heap Usage: %d bytes\n", - iCur); - iHiwtr = iCur = -1; - sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset); - raw_printf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n", - iCur); - } - - if( pArg && pArg->out && db && pArg->pStmt ){ - iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP, - bReset); - raw_printf(pArg->out, "Fullscan Steps: %d\n", iCur); - iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset); - raw_printf(pArg->out, "Sort Operations: %d\n", iCur); - iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset); - raw_printf(pArg->out, "Autoindex Inserts: %d\n", iCur); - iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset); - raw_printf(pArg->out, "Virtual Machine Steps: %d\n", iCur); - } - - /* Do not remove this machine readable comment: extra-stats-output-here */ - - return 0; -} - -/* -** Display scan stats. -*/ -static void display_scanstats( - sqlite3 *db, /* Database to query */ - ShellState *pArg /* Pointer to ShellState */ -){ -#ifndef SQLITE_ENABLE_STMT_SCANSTATUS - UNUSED_PARAMETER(db); - UNUSED_PARAMETER(pArg); -#else - int i, k, n, mx; - raw_printf(pArg->out, "-------- scanstats --------\n"); - mx = 0; - for(k=0; k<=mx; k++){ - double rEstLoop = 1.0; - for(i=n=0; 1; i++){ - sqlite3_stmt *p = pArg->pStmt; - sqlite3_int64 nLoop, nVisit; - double rEst; - int iSid; - const char *zExplain; - if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){ - break; - } - sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid); - if( iSid>mx ) mx = iSid; - if( iSid!=k ) continue; - if( n==0 ){ - rEstLoop = (double)nLoop; - if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k); - } - n++; - sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit); - sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst); - sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain); - utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain); - rEstLoop *= rEst; - raw_printf(pArg->out, - " nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n", - nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst - ); - } - } - raw_printf(pArg->out, "---------------------------\n"); -#endif -} - -/* -** Parameter azArray points to a zero-terminated array of strings. zStr -** points to a single nul-terminated string. Return non-zero if zStr -** is equal, according to strcmp(), to any of the strings in the array. -** Otherwise, return zero. -*/ -static int str_in_array(const char *zStr, const char **azArray){ - int i; - for(i=0; azArray[i]; i++){ - if( 0==strcmp(zStr, azArray[i]) ) return 1; - } - return 0; -} - -/* -** If compiled statement pSql appears to be an EXPLAIN statement, allocate -** and populate the ShellState.aiIndent[] array with the number of -** spaces each opcode should be indented before it is output. -** -** The indenting rules are: -** -** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent -** all opcodes that occur between the p2 jump destination and the opcode -** itself by 2 spaces. -** -** * For each "Goto", if the jump destination is earlier in the program -** and ends on one of: -** Yield SeekGt SeekLt RowSetRead Rewind -** or if the P1 parameter is one instead of zero, -** then indent all opcodes between the earlier instruction -** and "Goto" by 2 spaces. -*/ -static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){ - const char *zSql; /* The text of the SQL statement */ - const char *z; /* Used to check if this is an EXPLAIN */ - int *abYield = 0; /* True if op is an OP_Yield */ - int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */ - int iOp; /* Index of operation in p->aiIndent[] */ - - const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", - "NextIfOpen", "PrevIfOpen", 0 }; - const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead", - "Rewind", 0 }; - const char *azGoto[] = { "Goto", 0 }; - - /* Try to figure out if this is really an EXPLAIN statement. If this - ** cannot be verified, return early. */ - if( sqlite3_column_count(pSql)!=8 ){ - p->cMode = p->mode; - return; - } - zSql = sqlite3_sql(pSql); - if( zSql==0 ) return; - for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++); - if( sqlite3_strnicmp(z, "explain", 7) ){ - p->cMode = p->mode; - return; - } - - for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){ - int i; - int iAddr = sqlite3_column_int(pSql, 0); - const char *zOp = (const char*)sqlite3_column_text(pSql, 1); - - /* Set p2 to the P2 field of the current opcode. Then, assuming that - ** p2 is an instruction address, set variable p2op to the index of that - ** instruction in the aiIndent[] array. p2 and p2op may be different if - ** the current instruction is part of a sub-program generated by an - ** SQL trigger or foreign key. */ - int p2 = sqlite3_column_int(pSql, 3); - int p2op = (p2 + (iOp-iAddr)); - - /* Grow the p->aiIndent array as required */ - if( iOp>=nAlloc ){ - if( iOp==0 ){ - /* Do further verfication that this is explain output. Abort if - ** it is not */ - static const char *explainCols[] = { - "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" }; - int jj; - for(jj=0; jjcMode = p->mode; - sqlite3_reset(pSql); - return; - } - } - } - nAlloc += 100; - p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int)); - abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int)); - } - abYield[iOp] = str_in_array(zOp, azYield); - p->aiIndent[iOp] = 0; - p->nIndent = iOp+1; - - if( str_in_array(zOp, azNext) ){ - for(i=p2op; iaiIndent[i] += 2; - } - if( str_in_array(zOp, azGoto) && p2opnIndent - && (abYield[p2op] || sqlite3_column_int(pSql, 2)) - ){ - for(i=p2op+1; iaiIndent[i] += 2; - } - } - - p->iIndent = 0; - sqlite3_free(abYield); - sqlite3_reset(pSql); -} - -/* -** Free the array allocated by explain_data_prepare(). -*/ -static void explain_data_delete(ShellState *p){ - sqlite3_free(p->aiIndent); - p->aiIndent = 0; - p->nIndent = 0; - p->iIndent = 0; -} - -/* -** Execute a statement or set of statements. Print -** any result rows/columns depending on the current mode -** set via the supplied callback. -** -** This is very similar to SQLite's built-in sqlite3_exec() -** function except it takes a slightly different callback -** and callback data argument. -*/ -static int shell_exec( - sqlite3 *db, /* An open database */ - const char *zSql, /* SQL to be evaluated */ - int (*xCallback)(void*,int,char**,char**,int*), /* Callback function */ - /* (not the same as sqlite3_exec) */ - ShellState *pArg, /* Pointer to ShellState */ - char **pzErrMsg /* Error msg written here */ -){ - sqlite3_stmt *pStmt = NULL; /* Statement to execute. */ - int rc = SQLITE_OK; /* Return Code */ - int rc2; - const char *zLeftover; /* Tail of unprocessed SQL */ - - if( pzErrMsg ){ - *pzErrMsg = NULL; - } - - while( zSql[0] && (SQLITE_OK == rc) ){ - rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover); - if( SQLITE_OK != rc ){ - if( pzErrMsg ){ - *pzErrMsg = save_err_msg(db); - } - }else{ - if( !pStmt ){ - /* this happens for a comment or white-space */ - zSql = zLeftover; - while( IsSpace(zSql[0]) ) zSql++; - continue; - } - - /* save off the prepared statment handle and reset row count */ - if( pArg ){ - pArg->pStmt = pStmt; - pArg->cnt = 0; - } - - /* echo the sql statement if echo on */ - if( pArg && pArg->echoOn ){ - const char *zStmtSql = sqlite3_sql(pStmt); - utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql); - } - - /* Show the EXPLAIN QUERY PLAN if .eqp is on */ - if( pArg && pArg->autoEQP ){ - sqlite3_stmt *pExplain; - char *zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", - sqlite3_sql(pStmt)); - rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); - if( rc==SQLITE_OK ){ - while( sqlite3_step(pExplain)==SQLITE_ROW ){ - raw_printf(pArg->out,"--EQP-- %d,",sqlite3_column_int(pExplain, 0)); - raw_printf(pArg->out,"%d,", sqlite3_column_int(pExplain, 1)); - raw_printf(pArg->out,"%d,", sqlite3_column_int(pExplain, 2)); - utf8_printf(pArg->out,"%s\n", sqlite3_column_text(pExplain, 3)); - } - } - sqlite3_finalize(pExplain); - sqlite3_free(zEQP); - } - - if( pArg ){ - pArg->cMode = pArg->mode; - if( pArg->autoExplain - && sqlite3_column_count(pStmt)==8 - && sqlite3_strlike("%EXPLAIN%", sqlite3_sql(pStmt),0)==0 - ){ - pArg->cMode = MODE_Explain; - } - - /* If the shell is currently in ".explain" mode, gather the extra - ** data required to add indents to the output.*/ - if( pArg->cMode==MODE_Explain ){ - explain_data_prepare(pArg, pStmt); - } - } - - /* perform the first step. this will tell us if we - ** have a result set or not and how wide it is. - */ - rc = sqlite3_step(pStmt); - /* if we have a result set... */ - if( SQLITE_ROW == rc ){ - /* if we have a callback... */ - if( xCallback ){ - /* allocate space for col name ptr, value ptr, and type */ - int nCol = sqlite3_column_count(pStmt); - void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1); - if( !pData ){ - rc = SQLITE_NOMEM; - }else{ - char **azCols = (char **)pData; /* Names of result columns */ - char **azVals = &azCols[nCol]; /* Results */ - int *aiTypes = (int *)&azVals[nCol]; /* Result types */ - int i, x; - assert(sizeof(int) <= sizeof(char *)); - /* save off ptrs to column names */ - for(i=0; icMode==MODE_Insert ){ - azVals[i] = ""; - }else{ - azVals[i] = (char*)sqlite3_column_text(pStmt, i); - } - if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){ - rc = SQLITE_NOMEM; - break; /* from for */ - } - } /* end for */ - - /* if data and types extracted successfully... */ - if( SQLITE_ROW == rc ){ - /* call the supplied callback with the result row data */ - if( xCallback(pArg, nCol, azVals, azCols, aiTypes) ){ - rc = SQLITE_ABORT; - }else{ - rc = sqlite3_step(pStmt); - } - } - } while( SQLITE_ROW == rc ); - sqlite3_free(pData); - } - }else{ - do{ - rc = sqlite3_step(pStmt); - } while( rc == SQLITE_ROW ); - } - } - - explain_data_delete(pArg); - - /* print usage stats if stats on */ - if( pArg && pArg->statsOn ){ - display_stats(db, pArg, 0); - } - - /* print loop-counters if required */ - if( pArg && pArg->scanstatsOn ){ - display_scanstats(db, pArg); - } - - /* Finalize the statement just executed. If this fails, save a - ** copy of the error message. Otherwise, set zSql to point to the - ** next statement to execute. */ - rc2 = sqlite3_finalize(pStmt); - if( rc!=SQLITE_NOMEM ) rc = rc2; - if( rc==SQLITE_OK ){ - zSql = zLeftover; - while( IsSpace(zSql[0]) ) zSql++; - }else if( pzErrMsg ){ - *pzErrMsg = save_err_msg(db); - } - - /* clear saved stmt handle */ - if( pArg ){ - pArg->pStmt = NULL; - } - } - } /* end while */ - - return rc; -} - - -/* -** This is a different callback routine used for dumping the database. -** Each row received by this callback consists of a table name, -** the table type ("index" or "table") and SQL to create the table. -** This routine should print text sufficient to recreate the table. -*/ -static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){ - int rc; - const char *zTable; - const char *zType; - const char *zSql; - const char *zPrepStmt = 0; - ShellState *p = (ShellState *)pArg; - - UNUSED_PARAMETER(azCol); - if( nArg!=3 ) return 1; - zTable = azArg[0]; - zType = azArg[1]; - zSql = azArg[2]; - - if( strcmp(zTable, "sqlite_sequence")==0 ){ - zPrepStmt = "DELETE FROM sqlite_sequence;\n"; - }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){ - raw_printf(p->out, "ANALYZE sqlite_master;\n"); - }else if( strncmp(zTable, "sqlite_", 7)==0 ){ - return 0; - }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){ - char *zIns; - if( !p->writableSchema ){ - raw_printf(p->out, "PRAGMA writable_schema=ON;\n"); - p->writableSchema = 1; - } - zIns = sqlite3_mprintf( - "INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)" - "VALUES('table','%q','%q',0,'%q');", - zTable, zTable, zSql); - utf8_printf(p->out, "%s\n", zIns); - sqlite3_free(zIns); - return 0; - }else{ - utf8_printf(p->out, "%s;\n", zSql); - } - - if( strcmp(zType, "table")==0 ){ - sqlite3_stmt *pTableInfo = 0; - char *zSelect = 0; - char *zTableInfo = 0; - char *zTmp = 0; - int nRow = 0; - - zTableInfo = appendText(zTableInfo, "PRAGMA table_info(", 0); - zTableInfo = appendText(zTableInfo, zTable, '"'); - zTableInfo = appendText(zTableInfo, ");", 0); - - rc = sqlite3_prepare_v2(p->db, zTableInfo, -1, &pTableInfo, 0); - free(zTableInfo); - if( rc!=SQLITE_OK || !pTableInfo ){ - return 1; - } - - zSelect = appendText(zSelect, "SELECT 'INSERT INTO ' || ", 0); - /* Always quote the table name, even if it appears to be pure ascii, - ** in case it is a keyword. Ex: INSERT INTO "table" ... */ - zTmp = appendText(zTmp, zTable, '"'); - if( zTmp ){ - zSelect = appendText(zSelect, zTmp, '\''); - free(zTmp); - } - zSelect = appendText(zSelect, " || ' VALUES(' || ", 0); - rc = sqlite3_step(pTableInfo); - while( rc==SQLITE_ROW ){ - const char *zText = (const char *)sqlite3_column_text(pTableInfo, 1); - zSelect = appendText(zSelect, "quote(", 0); - zSelect = appendText(zSelect, zText, '"'); - rc = sqlite3_step(pTableInfo); - if( rc==SQLITE_ROW ){ - zSelect = appendText(zSelect, "), ", 0); - }else{ - zSelect = appendText(zSelect, ") ", 0); - } - nRow++; - } - rc = sqlite3_finalize(pTableInfo); - if( rc!=SQLITE_OK || nRow==0 ){ - free(zSelect); - return 1; - } - zSelect = appendText(zSelect, "|| ')' FROM ", 0); - zSelect = appendText(zSelect, zTable, '"'); - - rc = run_table_dump_query(p, zSelect, zPrepStmt); - if( rc==SQLITE_CORRUPT ){ - zSelect = appendText(zSelect, " ORDER BY rowid DESC", 0); - run_table_dump_query(p, zSelect, 0); - } - free(zSelect); - } - return 0; -} - -/* -** Run zQuery. Use dump_callback() as the callback routine so that -** the contents of the query are output as SQL statements. -** -** If we get a SQLITE_CORRUPT error, rerun the query after appending -** "ORDER BY rowid DESC" to the end. -*/ -static int run_schema_dump_query( - ShellState *p, - const char *zQuery -){ - int rc; - char *zErr = 0; - rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr); - if( rc==SQLITE_CORRUPT ){ - char *zQ2; - int len = strlen30(zQuery); - raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n"); - if( zErr ){ - utf8_printf(p->out, "/****** %s ******/\n", zErr); - sqlite3_free(zErr); - zErr = 0; - } - zQ2 = malloc( len+100 ); - if( zQ2==0 ) return rc; - sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery); - rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr); - if( rc ){ - utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr); - }else{ - rc = SQLITE_CORRUPT; - } - sqlite3_free(zErr); - free(zQ2); - } - return rc; -} - -/* -** Text of a help message -*/ -static char zHelp[] = - ".backup ?DB? FILE Backup DB (default \"main\") to FILE\n" - ".bail on|off Stop after hitting an error. Default OFF\n" - ".binary on|off Turn binary output on or off. Default OFF\n" - ".changes on|off Show number of rows changed by SQL\n" - ".clone NEWDB Clone data into NEWDB from the existing database\n" - ".databases List names and files of attached databases\n" - ".dbinfo ?DB? Show status information about the database\n" - ".dump ?TABLE? ... Dump the database in an SQL text format\n" - " If TABLE specified, only dump tables matching\n" - " LIKE pattern TABLE.\n" - ".echo on|off Turn command echo on or off\n" - ".eqp on|off Enable or disable automatic EXPLAIN QUERY PLAN\n" - ".exit Exit this program\n" - ".explain ?on|off|auto? Turn EXPLAIN output mode on or off or to automatic\n" - ".fullschema Show schema and the content of sqlite_stat tables\n" - ".headers on|off Turn display of headers on or off\n" - ".help Show this message\n" - ".import FILE TABLE Import data from FILE into TABLE\n" - ".indexes ?TABLE? Show names of all indexes\n" - " If TABLE specified, only show indexes for tables\n" - " matching LIKE pattern TABLE.\n" -#ifdef SQLITE_ENABLE_IOTRACE - ".iotrace FILE Enable I/O diagnostic logging to FILE\n" -#endif - ".limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT\n" -#ifndef SQLITE_OMIT_LOAD_EXTENSION - ".load FILE ?ENTRY? Load an extension library\n" -#endif - ".log FILE|off Turn logging on or off. FILE can be stderr/stdout\n" - ".mode MODE ?TABLE? Set output mode where MODE is one of:\n" - " ascii Columns/rows delimited by 0x1F and 0x1E\n" - " csv Comma-separated values\n" - " column Left-aligned columns. (See .width)\n" - " html HTML
    "); - output_html_string(p->out, azCol[i]); - raw_printf(p->out,"
    "); - output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue); - raw_printf(p->out,"
    code\n" - " insert SQL insert statements for TABLE\n" - " line One value per line\n" - " list Values delimited by .separator strings\n" - " tabs Tab-separated values\n" - " tcl TCL list elements\n" - ".nullvalue STRING Use STRING in place of NULL values\n" - ".once FILENAME Output for the next SQL command only to FILENAME\n" - ".open ?FILENAME? Close existing database and reopen FILENAME\n" - ".output ?FILENAME? Send output to FILENAME or stdout\n" - ".print STRING... Print literal STRING\n" - ".prompt MAIN CONTINUE Replace the standard prompts\n" - ".quit Exit this program\n" - ".read FILENAME Execute SQL in FILENAME\n" - ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE\n" - ".save FILE Write in-memory database into FILE\n" - ".scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off\n" - ".schema ?TABLE? Show the CREATE statements\n" - " If TABLE specified, only show tables matching\n" - " LIKE pattern TABLE.\n" - ".separator COL ?ROW? Change the column separator and optionally the row\n" - " separator for both the output mode and .import\n" - ".shell CMD ARGS... Run CMD ARGS... in a system shell\n" - ".show Show the current values for various settings\n" - ".stats on|off Turn stats on or off\n" - ".system CMD ARGS... Run CMD ARGS... in a system shell\n" - ".tables ?TABLE? List names of tables\n" - " If TABLE specified, only list tables matching\n" - " LIKE pattern TABLE.\n" - ".timeout MS Try opening locked tables for MS milliseconds\n" - ".timer on|off Turn SQL timer on or off\n" - ".trace FILE|off Output each SQL statement as it is run\n" - ".vfsinfo ?AUX? Information about the top-level VFS\n" - ".vfslist List all available VFSes\n" - ".vfsname ?AUX? Print the name of the VFS stack\n" - ".width NUM1 NUM2 ... Set column widths for \"column\" mode\n" - " Negative values right-justify\n" -; - -/* Forward reference */ -static int process_input(ShellState *p, FILE *in); -/* -** Implementation of the "readfile(X)" SQL function. The entire content -** of the file named X is read and returned as a BLOB. NULL is returned -** if the file does not exist or is unreadable. -*/ -static void readfileFunc( - sqlite3_context *context, - int argc, - sqlite3_value **argv -){ - const char *zName; - FILE *in; - long nIn; - void *pBuf; - - UNUSED_PARAMETER(argc); - zName = (const char*)sqlite3_value_text(argv[0]); - if( zName==0 ) return; - in = fopen(zName, "rb"); - if( in==0 ) return; - fseek(in, 0, SEEK_END); - nIn = ftell(in); - rewind(in); - pBuf = sqlite3_malloc64( nIn ); - if( pBuf && 1==fread(pBuf, nIn, 1, in) ){ - sqlite3_result_blob(context, pBuf, nIn, sqlite3_free); - }else{ - sqlite3_free(pBuf); - } - fclose(in); -} - -/* -** Implementation of the "writefile(X,Y)" SQL function. The argument Y -** is written into file X. The number of bytes written is returned. Or -** NULL is returned if something goes wrong, such as being unable to open -** file X for writing. -*/ -static void writefileFunc( - sqlite3_context *context, - int argc, - sqlite3_value **argv -){ - FILE *out; - const char *z; - sqlite3_int64 rc; - const char *zFile; - - UNUSED_PARAMETER(argc); - zFile = (const char*)sqlite3_value_text(argv[0]); - if( zFile==0 ) return; - out = fopen(zFile, "wb"); - if( out==0 ) return; - z = (const char*)sqlite3_value_blob(argv[1]); - if( z==0 ){ - rc = 0; - }else{ - rc = fwrite(z, 1, sqlite3_value_bytes(argv[1]), out); - } - fclose(out); - sqlite3_result_int64(context, rc); -} - -/* -** Make sure the database is open. If it is not, then open it. If -** the database fails to open, print an error message and exit. -*/ -static void open_db(ShellState *p, int keepAlive){ - if( p->db==0 ){ - sqlite3_initialize(); - sqlite3_open(p->zDbFilename, &p->db); - globalDb = p->db; - if( p->db && sqlite3_errcode(p->db)==SQLITE_OK ){ - sqlite3_create_function(p->db, "shellstatic", 0, SQLITE_UTF8, 0, - shellstaticFunc, 0, 0); - } - if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){ - utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n", - p->zDbFilename, sqlite3_errmsg(p->db)); - if( keepAlive ) return; - exit(1); - } -#ifndef SQLITE_OMIT_LOAD_EXTENSION - sqlite3_enable_load_extension(p->db, 1); -#endif - sqlite3_create_function(p->db, "readfile", 1, SQLITE_UTF8, 0, - readfileFunc, 0, 0); - sqlite3_create_function(p->db, "writefile", 2, SQLITE_UTF8, 0, - writefileFunc, 0, 0); - } -} - -/* -** Do C-language style dequoting. -** -** \a -> alarm -** \b -> backspace -** \t -> tab -** \n -> newline -** \v -> vertical tab -** \f -> form feed -** \r -> carriage return -** \s -> space -** \" -> " -** \' -> ' -** \\ -> backslash -** \NNN -> ascii character NNN in octal -*/ -static void resolve_backslashes(char *z){ - int i, j; - char c; - while( *z && *z!='\\' ) z++; - for(i=j=0; (c = z[i])!=0; i++, j++){ - if( c=='\\' && z[i+1]!=0 ){ - c = z[++i]; - if( c=='a' ){ - c = '\a'; - }else if( c=='b' ){ - c = '\b'; - }else if( c=='t' ){ - c = '\t'; - }else if( c=='n' ){ - c = '\n'; - }else if( c=='v' ){ - c = '\v'; - }else if( c=='f' ){ - c = '\f'; - }else if( c=='r' ){ - c = '\r'; - }else if( c=='"' ){ - c = '"'; - }else if( c=='\'' ){ - c = '\''; - }else if( c=='\\' ){ - c = '\\'; - }else if( c>='0' && c<='7' ){ - c -= '0'; - if( z[i+1]>='0' && z[i+1]<='7' ){ - i++; - c = (c<<3) + z[i] - '0'; - if( z[i+1]>='0' && z[i+1]<='7' ){ - i++; - c = (c<<3) + z[i] - '0'; - } - } - } - } - z[j] = c; - } - if( j='0' && c<='9' ) return c - '0'; - if( c>='a' && c<='f' ) return c - 'a' + 10; - if( c>='A' && c<='F' ) return c - 'A' + 10; - return -1; -} - -/* -** Interpret zArg as an integer value, possibly with suffixes. -*/ -static sqlite3_int64 integerValue(const char *zArg){ - sqlite3_int64 v = 0; - static const struct { char *zSuffix; int iMult; } aMult[] = { - { "KiB", 1024 }, - { "MiB", 1024*1024 }, - { "GiB", 1024*1024*1024 }, - { "KB", 1000 }, - { "MB", 1000000 }, - { "GB", 1000000000 }, - { "K", 1000 }, - { "M", 1000000 }, - { "G", 1000000000 }, - }; - int i; - int isNeg = 0; - if( zArg[0]=='-' ){ - isNeg = 1; - zArg++; - }else if( zArg[0]=='+' ){ - zArg++; - } - if( zArg[0]=='0' && zArg[1]=='x' ){ - int x; - zArg += 2; - while( (x = hexDigitValue(zArg[0]))>=0 ){ - v = (v<<4) + x; - zArg++; - } - }else{ - while( IsDigit(zArg[0]) ){ - v = v*10 + zArg[0] - '0'; - zArg++; - } - } - for(i=0; i=0; i++){} - }else{ - for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){} - } - if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff); - if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){ - return 1; - } - if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){ - return 0; - } - utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n", - zArg); - return 0; -} - -/* -** Close an output file, assuming it is not stderr or stdout -*/ -static void output_file_close(FILE *f){ - if( f && f!=stdout && f!=stderr ) fclose(f); -} - -/* -** Try to open an output file. The names "stdout" and "stderr" are -** recognized and do the right thing. NULL is returned if the output -** filename is "off". -*/ -static FILE *output_file_open(const char *zFile){ - FILE *f; - if( strcmp(zFile,"stdout")==0 ){ - f = stdout; - }else if( strcmp(zFile, "stderr")==0 ){ - f = stderr; - }else if( strcmp(zFile, "off")==0 ){ - f = 0; - }else{ - f = fopen(zFile, "wb"); - if( f==0 ){ - utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile); - } - } - return f; -} - -/* -** A routine for handling output from sqlite3_trace(). -*/ -static void sql_trace_callback(void *pArg, const char *z){ - FILE *f = (FILE*)pArg; - if( f ){ - int i = (int)strlen(z); - while( i>0 && z[i-1]==';' ){ i--; } - utf8_printf(f, "%.*s;\n", i, z); - } -} - -/* -** A no-op routine that runs with the ".breakpoint" doc-command. This is -** a useful spot to set a debugger breakpoint. -*/ -static void test_breakpoint(void){ - static int nCall = 0; - nCall++; -} - -/* -** An object used to read a CSV and other files for import. -*/ -typedef struct ImportCtx ImportCtx; -struct ImportCtx { - const char *zFile; /* Name of the input file */ - FILE *in; /* Read the CSV text from this input stream */ - char *z; /* Accumulated text for a field */ - int n; /* Number of bytes in z */ - int nAlloc; /* Space allocated for z[] */ - int nLine; /* Current line number */ - int cTerm; /* Character that terminated the most recent field */ - int cColSep; /* The column separator character. (Usually ",") */ - int cRowSep; /* The row separator character. (Usually "\n") */ -}; - -/* Append a single byte to z[] */ -static void import_append_char(ImportCtx *p, int c){ - if( p->n+1>=p->nAlloc ){ - p->nAlloc += p->nAlloc + 100; - p->z = sqlite3_realloc64(p->z, p->nAlloc); - if( p->z==0 ){ - raw_printf(stderr, "out of memory\n"); - exit(1); - } - } - p->z[p->n++] = (char)c; -} - -/* Read a single field of CSV text. Compatible with rfc4180 and extended -** with the option of having a separator other than ",". -** -** + Input comes from p->in. -** + Store results in p->z of length p->n. Space to hold p->z comes -** from sqlite3_malloc64(). -** + Use p->cSep as the column separator. The default is ",". -** + Use p->rSep as the row separator. The default is "\n". -** + Keep track of the line number in p->nLine. -** + Store the character that terminates the field in p->cTerm. Store -** EOF on end-of-file. -** + Report syntax errors on stderr -*/ -static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){ - int c; - int cSep = p->cColSep; - int rSep = p->cRowSep; - p->n = 0; - c = fgetc(p->in); - if( c==EOF || seenInterrupt ){ - p->cTerm = EOF; - return 0; - } - if( c=='"' ){ - int pc, ppc; - int startLine = p->nLine; - int cQuote = c; - pc = ppc = 0; - while( 1 ){ - c = fgetc(p->in); - if( c==rSep ) p->nLine++; - if( c==cQuote ){ - if( pc==cQuote ){ - pc = 0; - continue; - } - } - if( (c==cSep && pc==cQuote) - || (c==rSep && pc==cQuote) - || (c==rSep && pc=='\r' && ppc==cQuote) - || (c==EOF && pc==cQuote) - ){ - do{ p->n--; }while( p->z[p->n]!=cQuote ); - p->cTerm = c; - break; - } - if( pc==cQuote && c!='\r' ){ - utf8_printf(stderr, "%s:%d: unescaped %c character\n", - p->zFile, p->nLine, cQuote); - } - if( c==EOF ){ - utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n", - p->zFile, startLine, cQuote); - p->cTerm = c; - break; - } - import_append_char(p, c); - ppc = pc; - pc = c; - } - }else{ - while( c!=EOF && c!=cSep && c!=rSep ){ - import_append_char(p, c); - c = fgetc(p->in); - } - if( c==rSep ){ - p->nLine++; - if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--; - } - p->cTerm = c; - } - if( p->z ) p->z[p->n] = 0; - return p->z; -} - -/* Read a single field of ASCII delimited text. -** -** + Input comes from p->in. -** + Store results in p->z of length p->n. Space to hold p->z comes -** from sqlite3_malloc64(). -** + Use p->cSep as the column separator. The default is "\x1F". -** + Use p->rSep as the row separator. The default is "\x1E". -** + Keep track of the row number in p->nLine. -** + Store the character that terminates the field in p->cTerm. Store -** EOF on end-of-file. -** + Report syntax errors on stderr -*/ -static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){ - int c; - int cSep = p->cColSep; - int rSep = p->cRowSep; - p->n = 0; - c = fgetc(p->in); - if( c==EOF || seenInterrupt ){ - p->cTerm = EOF; - return 0; - } - while( c!=EOF && c!=cSep && c!=rSep ){ - import_append_char(p, c); - c = fgetc(p->in); - } - if( c==rSep ){ - p->nLine++; - } - p->cTerm = c; - if( p->z ) p->z[p->n] = 0; - return p->z; -} - -/* -** Try to transfer data for table zTable. If an error is seen while -** moving forward, try to go backwards. The backwards movement won't -** work for WITHOUT ROWID tables. -*/ -static void tryToCloneData( - ShellState *p, - sqlite3 *newDb, - const char *zTable -){ - sqlite3_stmt *pQuery = 0; - sqlite3_stmt *pInsert = 0; - char *zQuery = 0; - char *zInsert = 0; - int rc; - int i, j, n; - int nTable = (int)strlen(zTable); - int k = 0; - int cnt = 0; - const int spinRate = 10000; - - zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable); - rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); - if( rc ){ - utf8_printf(stderr, "Error %d: %s on [%s]\n", - sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), - zQuery); - goto end_data_xfer; - } - n = sqlite3_column_count(pQuery); - zInsert = sqlite3_malloc64(200 + nTable + n*3); - if( zInsert==0 ){ - raw_printf(stderr, "out of memory\n"); - goto end_data_xfer; - } - sqlite3_snprintf(200+nTable,zInsert, - "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable); - i = (int)strlen(zInsert); - for(j=1; jdb, zQuery, -1, &pQuery, 0); - if( rc ){ - utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable); - break; - } - } /* End for(k=0...) */ - -end_data_xfer: - sqlite3_finalize(pQuery); - sqlite3_finalize(pInsert); - sqlite3_free(zQuery); - sqlite3_free(zInsert); -} - - -/* -** Try to transfer all rows of the schema that match zWhere. For -** each row, invoke xForEach() on the object defined by that row. -** If an error is encountered while moving forward through the -** sqlite_master table, try again moving backwards. -*/ -static void tryToCloneSchema( - ShellState *p, - sqlite3 *newDb, - const char *zWhere, - void (*xForEach)(ShellState*,sqlite3*,const char*) -){ - sqlite3_stmt *pQuery = 0; - char *zQuery = 0; - int rc; - const unsigned char *zName; - const unsigned char *zSql; - char *zErrMsg = 0; - - zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master" - " WHERE %s", zWhere); - rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); - if( rc ){ - utf8_printf(stderr, "Error: (%d) %s on [%s]\n", - sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), - zQuery); - goto end_schema_xfer; - } - while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ - zName = sqlite3_column_text(pQuery, 0); - zSql = sqlite3_column_text(pQuery, 1); - printf("%s... ", zName); fflush(stdout); - sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg); - if( zErrMsg ){ - utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql); - sqlite3_free(zErrMsg); - zErrMsg = 0; - } - if( xForEach ){ - xForEach(p, newDb, (const char*)zName); - } - printf("done\n"); - } - if( rc!=SQLITE_DONE ){ - sqlite3_finalize(pQuery); - sqlite3_free(zQuery); - zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master" - " WHERE %s ORDER BY rowid DESC", zWhere); - rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); - if( rc ){ - utf8_printf(stderr, "Error: (%d) %s on [%s]\n", - sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), - zQuery); - goto end_schema_xfer; - } - while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ - zName = sqlite3_column_text(pQuery, 0); - zSql = sqlite3_column_text(pQuery, 1); - printf("%s... ", zName); fflush(stdout); - sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg); - if( zErrMsg ){ - utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql); - sqlite3_free(zErrMsg); - zErrMsg = 0; - } - if( xForEach ){ - xForEach(p, newDb, (const char*)zName); - } - printf("done\n"); - } - } -end_schema_xfer: - sqlite3_finalize(pQuery); - sqlite3_free(zQuery); -} - -/* -** Open a new database file named "zNewDb". Try to recover as much information -** as possible out of the main database (which might be corrupt) and write it -** into zNewDb. -*/ -static void tryToClone(ShellState *p, const char *zNewDb){ - int rc; - sqlite3 *newDb = 0; - if( access(zNewDb,0)==0 ){ - utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb); - return; - } - rc = sqlite3_open(zNewDb, &newDb); - if( rc ){ - utf8_printf(stderr, "Cannot create output database: %s\n", - sqlite3_errmsg(newDb)); - }else{ - sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0); - sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0); - tryToCloneSchema(p, newDb, "type='table'", tryToCloneData); - tryToCloneSchema(p, newDb, "type!='table'", 0); - sqlite3_exec(newDb, "COMMIT;", 0, 0, 0); - sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0); - } - sqlite3_close(newDb); -} - -/* -** Change the output file back to stdout -*/ -static void output_reset(ShellState *p){ - if( p->outfile[0]=='|' ){ -#ifndef SQLITE_OMIT_POPEN - pclose(p->out); -#endif - }else{ - output_file_close(p->out); - } - p->outfile[0] = 0; - p->out = stdout; -} - -/* -** Run an SQL command and return the single integer result. -*/ -static int db_int(ShellState *p, const char *zSql){ - sqlite3_stmt *pStmt; - int res = 0; - sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); - if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){ - res = sqlite3_column_int(pStmt,0); - } - sqlite3_finalize(pStmt); - return res; -} - -/* -** Convert a 2-byte or 4-byte big-endian integer into a native integer -*/ -unsigned int get2byteInt(unsigned char *a){ - return (a[0]<<8) + a[1]; -} -unsigned int get4byteInt(unsigned char *a){ - return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3]; -} - -/* -** Implementation of the ".info" command. -** -** Return 1 on error, 2 to exit, and 0 otherwise. -*/ -static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){ - static const struct { const char *zName; int ofst; } aField[] = { - { "file change counter:", 24 }, - { "database page count:", 28 }, - { "freelist page count:", 36 }, - { "schema cookie:", 40 }, - { "schema format:", 44 }, - { "default cache size:", 48 }, - { "autovacuum top root:", 52 }, - { "incremental vacuum:", 64 }, - { "text encoding:", 56 }, - { "user version:", 60 }, - { "application id:", 68 }, - { "software version:", 96 }, - }; - static const struct { const char *zName; const char *zSql; } aQuery[] = { - { "number of tables:", - "SELECT count(*) FROM %s WHERE type='table'" }, - { "number of indexes:", - "SELECT count(*) FROM %s WHERE type='index'" }, - { "number of triggers:", - "SELECT count(*) FROM %s WHERE type='trigger'" }, - { "number of views:", - "SELECT count(*) FROM %s WHERE type='view'" }, - { "schema size:", - "SELECT total(length(sql)) FROM %s" }, - }; - sqlite3_file *pFile = 0; - int i; - char *zSchemaTab; - char *zDb = nArg>=2 ? azArg[1] : "main"; - unsigned char aHdr[100]; - open_db(p, 0); - if( p->db==0 ) return 1; - sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_FILE_POINTER, &pFile); - if( pFile==0 || pFile->pMethods==0 || pFile->pMethods->xRead==0 ){ - return 1; - } - i = pFile->pMethods->xRead(pFile, aHdr, 100, 0); - if( i!=SQLITE_OK ){ - raw_printf(stderr, "unable to read database header\n"); - return 1; - } - i = get2byteInt(aHdr+16); - if( i==1 ) i = 65536; - utf8_printf(p->out, "%-20s %d\n", "database page size:", i); - utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]); - utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]); - utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]); - for(i=0; iout, "%-20s %u", aField[i].zName, val); - switch( ofst ){ - case 56: { - if( val==1 ) raw_printf(p->out, " (utf8)"); - if( val==2 ) raw_printf(p->out, " (utf16le)"); - if( val==3 ) raw_printf(p->out, " (utf16be)"); - } - } - raw_printf(p->out, "\n"); - } - if( zDb==0 ){ - zSchemaTab = sqlite3_mprintf("main.sqlite_master"); - }else if( strcmp(zDb,"temp")==0 ){ - zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_master"); - }else{ - zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_master", zDb); - } - for(i=0; iout, "%-20s %d\n", aQuery[i].zName, val); - } - sqlite3_free(zSchemaTab); - return 0; -} - -/* -** Print the current sqlite3_errmsg() value to stderr and return 1. -*/ -static int shellDatabaseError(sqlite3 *db){ - const char *zErr = sqlite3_errmsg(db); - utf8_printf(stderr, "Error: %s\n", zErr); - return 1; -} - -/* -** Print an out-of-memory message to stderr and return 1. -*/ -static int shellNomemError(void){ - raw_printf(stderr, "Error: out of memory\n"); - return 1; -} - -/* -** If an input line begins with "." then invoke this routine to -** process that line. -** -** Return 1 on error, 2 to exit, and 0 otherwise. -*/ -static int do_meta_command(char *zLine, ShellState *p){ - int h = 1; - int nArg = 0; - int n, c; - int rc = 0; - char *azArg[50]; - - /* Parse the input line into tokens. - */ - while( zLine[h] && nArg=3 && strncmp(azArg[0], "backup", n)==0) - || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0) - ){ - const char *zDestFile = 0; - const char *zDb = 0; - sqlite3 *pDest; - sqlite3_backup *pBackup; - int j; - for(j=1; jdb, zDb); - if( pBackup==0 ){ - utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest)); - sqlite3_close(pDest); - return 1; - } - while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){} - sqlite3_backup_finish(pBackup); - if( rc==SQLITE_DONE ){ - rc = 0; - }else{ - utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest)); - rc = 1; - } - sqlite3_close(pDest); - }else - - if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){ - if( nArg==2 ){ - bail_on_error = booleanValue(azArg[1]); - }else{ - raw_printf(stderr, "Usage: .bail on|off\n"); - rc = 1; - } - }else - - if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){ - if( nArg==2 ){ - if( booleanValue(azArg[1]) ){ - setBinaryMode(p->out); - }else{ - setTextMode(p->out); - } - }else{ - raw_printf(stderr, "Usage: .binary on|off\n"); - rc = 1; - } - }else - - /* The undocumented ".breakpoint" command causes a call to the no-op - ** routine named test_breakpoint(). - */ - if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){ - test_breakpoint(); - }else - - if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){ - if( nArg==2 ){ - p->countChanges = booleanValue(azArg[1]); - }else{ - raw_printf(stderr, "Usage: .changes on|off\n"); - rc = 1; - } - }else - - if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){ - if( nArg==2 ){ - tryToClone(p, azArg[1]); - }else{ - raw_printf(stderr, "Usage: .clone FILENAME\n"); - rc = 1; - } - }else - - if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){ - ShellState data; - char *zErrMsg = 0; - open_db(p, 0); - memcpy(&data, p, sizeof(data)); - data.showHeader = 1; - data.cMode = data.mode = MODE_Column; - data.colWidth[0] = 3; - data.colWidth[1] = 15; - data.colWidth[2] = 58; - data.cnt = 0; - sqlite3_exec(p->db, "PRAGMA database_list; ", callback, &data, &zErrMsg); - if( zErrMsg ){ - utf8_printf(stderr,"Error: %s\n", zErrMsg); - sqlite3_free(zErrMsg); - rc = 1; - } - }else - - if( c=='d' && strncmp(azArg[0], "dbinfo", n)==0 ){ - rc = shell_dbinfo_command(p, nArg, azArg); - }else - - if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){ - open_db(p, 0); - /* When playing back a "dump", the content might appear in an order - ** which causes immediate foreign key constraints to be violated. - ** So disable foreign-key constraint enforcement to prevent problems. */ - if( nArg!=1 && nArg!=2 ){ - raw_printf(stderr, "Usage: .dump ?LIKE-PATTERN?\n"); - rc = 1; - goto meta_command_exit; - } - raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n"); - raw_printf(p->out, "BEGIN TRANSACTION;\n"); - p->writableSchema = 0; - sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0); - p->nErr = 0; - if( nArg==1 ){ - run_schema_dump_query(p, - "SELECT name, type, sql FROM sqlite_master " - "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'" - ); - run_schema_dump_query(p, - "SELECT name, type, sql FROM sqlite_master " - "WHERE name=='sqlite_sequence'" - ); - run_table_dump_query(p, - "SELECT sql FROM sqlite_master " - "WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0 - ); - }else{ - int i; - for(i=1; iwritableSchema ){ - raw_printf(p->out, "PRAGMA writable_schema=OFF;\n"); - p->writableSchema = 0; - } - sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0); - sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0); - raw_printf(p->out, p->nErr ? "ROLLBACK; -- due to errors\n" : "COMMIT;\n"); - }else - - if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){ - if( nArg==2 ){ - p->echoOn = booleanValue(azArg[1]); - }else{ - raw_printf(stderr, "Usage: .echo on|off\n"); - rc = 1; - } - }else - - if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){ - if( nArg==2 ){ - p->autoEQP = booleanValue(azArg[1]); - }else{ - raw_printf(stderr, "Usage: .eqp on|off\n"); - rc = 1; - } - }else - - if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){ - if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc); - rc = 2; - }else - - if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){ - int val = 1; - if( nArg>=2 ){ - if( strcmp(azArg[1],"auto")==0 ){ - val = 99; - }else{ - val = booleanValue(azArg[1]); - } - } - if( val==1 && p->mode!=MODE_Explain ){ - p->normalMode = p->mode; - p->mode = MODE_Explain; - p->autoExplain = 0; - }else if( val==0 ){ - if( p->mode==MODE_Explain ) p->mode = p->normalMode; - p->autoExplain = 0; - }else if( val==99 ){ - if( p->mode==MODE_Explain ) p->mode = p->normalMode; - p->autoExplain = 1; - } - }else - - if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){ - ShellState data; - char *zErrMsg = 0; - int doStats = 0; - if( nArg!=1 ){ - raw_printf(stderr, "Usage: .fullschema\n"); - rc = 1; - goto meta_command_exit; - } - open_db(p, 0); - memcpy(&data, p, sizeof(data)); - data.showHeader = 0; - data.cMode = data.mode = MODE_Semi; - rc = sqlite3_exec(p->db, - "SELECT sql FROM" - " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x" - " FROM sqlite_master UNION ALL" - " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) " - "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' " - "ORDER BY rowid", - callback, &data, &zErrMsg - ); - if( rc==SQLITE_OK ){ - sqlite3_stmt *pStmt; - rc = sqlite3_prepare_v2(p->db, - "SELECT rowid FROM sqlite_master" - " WHERE name GLOB 'sqlite_stat[134]'", - -1, &pStmt, 0); - doStats = sqlite3_step(pStmt)==SQLITE_ROW; - sqlite3_finalize(pStmt); - } - if( doStats==0 ){ - raw_printf(p->out, "/* No STAT tables available */\n"); - }else{ - raw_printf(p->out, "ANALYZE sqlite_master;\n"); - sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_master'", - callback, &data, &zErrMsg); - data.cMode = data.mode = MODE_Insert; - data.zDestTable = "sqlite_stat1"; - shell_exec(p->db, "SELECT * FROM sqlite_stat1", - shell_callback, &data,&zErrMsg); - data.zDestTable = "sqlite_stat3"; - shell_exec(p->db, "SELECT * FROM sqlite_stat3", - shell_callback, &data,&zErrMsg); - data.zDestTable = "sqlite_stat4"; - shell_exec(p->db, "SELECT * FROM sqlite_stat4", - shell_callback, &data, &zErrMsg); - raw_printf(p->out, "ANALYZE sqlite_master;\n"); - } - }else - - if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){ - if( nArg==2 ){ - p->showHeader = booleanValue(azArg[1]); - }else{ - raw_printf(stderr, "Usage: .headers on|off\n"); - rc = 1; - } - }else - - if( c=='h' && strncmp(azArg[0], "help", n)==0 ){ - utf8_printf(p->out, "%s", zHelp); - }else - - if( c=='i' && strncmp(azArg[0], "import", n)==0 ){ - char *zTable; /* Insert data into this table */ - char *zFile; /* Name of file to extra content from */ - sqlite3_stmt *pStmt = NULL; /* A statement */ - int nCol; /* Number of columns in the table */ - int nByte; /* Number of bytes in an SQL string */ - int i, j; /* Loop counters */ - int needCommit; /* True to COMMIT or ROLLBACK at end */ - int nSep; /* Number of bytes in p->colSeparator[] */ - char *zSql; /* An SQL statement */ - ImportCtx sCtx; /* Reader context */ - char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */ - int (SQLITE_CDECL *xCloser)(FILE*); /* Func to close file */ - - if( nArg!=3 ){ - raw_printf(stderr, "Usage: .import FILE TABLE\n"); - goto meta_command_exit; - } - zFile = azArg[1]; - zTable = azArg[2]; - seenInterrupt = 0; - memset(&sCtx, 0, sizeof(sCtx)); - open_db(p, 0); - nSep = strlen30(p->colSeparator); - if( nSep==0 ){ - raw_printf(stderr, - "Error: non-null column separator required for import\n"); - return 1; - } - if( nSep>1 ){ - raw_printf(stderr, "Error: multi-character column separators not allowed" - " for import\n"); - return 1; - } - nSep = strlen30(p->rowSeparator); - if( nSep==0 ){ - raw_printf(stderr, "Error: non-null row separator required for import\n"); - return 1; - } - if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator, SEP_CrLf)==0 ){ - /* When importing CSV (only), if the row separator is set to the - ** default output row separator, change it to the default input - ** row separator. This avoids having to maintain different input - ** and output row separators. */ - sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); - nSep = strlen30(p->rowSeparator); - } - if( nSep>1 ){ - raw_printf(stderr, "Error: multi-character row separators not allowed" - " for import\n"); - return 1; - } - sCtx.zFile = zFile; - sCtx.nLine = 1; - if( sCtx.zFile[0]=='|' ){ -#ifdef SQLITE_OMIT_POPEN - raw_printf(stderr, "Error: pipes are not supported in this OS\n"); - return 1; -#else - sCtx.in = popen(sCtx.zFile+1, "r"); - sCtx.zFile = ""; - xCloser = pclose; -#endif - }else{ - sCtx.in = fopen(sCtx.zFile, "rb"); - xCloser = fclose; - } - if( p->mode==MODE_Ascii ){ - xRead = ascii_read_one_field; - }else{ - xRead = csv_read_one_field; - } - if( sCtx.in==0 ){ - utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile); - return 1; - } - sCtx.cColSep = p->colSeparator[0]; - sCtx.cRowSep = p->rowSeparator[0]; - zSql = sqlite3_mprintf("SELECT * FROM %s", zTable); - if( zSql==0 ){ - raw_printf(stderr, "Error: out of memory\n"); - xCloser(sCtx.in); - return 1; - } - nByte = strlen30(zSql); - rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); - import_append_char(&sCtx, 0); /* To ensure sCtx.z is allocated */ - if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){ - char *zCreate = sqlite3_mprintf("CREATE TABLE %s", zTable); - char cSep = '('; - while( xRead(&sCtx) ){ - zCreate = sqlite3_mprintf("%z%c\n \"%s\" TEXT", zCreate, cSep, sCtx.z); - cSep = ','; - if( sCtx.cTerm!=sCtx.cColSep ) break; - } - if( cSep=='(' ){ - sqlite3_free(zCreate); - sqlite3_free(sCtx.z); - xCloser(sCtx.in); - utf8_printf(stderr,"%s: empty file\n", sCtx.zFile); - return 1; - } - zCreate = sqlite3_mprintf("%z\n)", zCreate); - rc = sqlite3_exec(p->db, zCreate, 0, 0, 0); - sqlite3_free(zCreate); - if( rc ){ - utf8_printf(stderr, "CREATE TABLE %s(...) failed: %s\n", zTable, - sqlite3_errmsg(p->db)); - sqlite3_free(sCtx.z); - xCloser(sCtx.in); - return 1; - } - rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); - } - sqlite3_free(zSql); - if( rc ){ - if (pStmt) sqlite3_finalize(pStmt); - utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db)); - xCloser(sCtx.in); - return 1; - } - nCol = sqlite3_column_count(pStmt); - sqlite3_finalize(pStmt); - pStmt = 0; - if( nCol==0 ) return 0; /* no columns, no error */ - zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 ); - if( zSql==0 ){ - raw_printf(stderr, "Error: out of memory\n"); - xCloser(sCtx.in); - return 1; - } - sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable); - j = strlen30(zSql); - for(i=1; idb, zSql, -1, &pStmt, 0); - sqlite3_free(zSql); - if( rc ){ - utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); - if (pStmt) sqlite3_finalize(pStmt); - xCloser(sCtx.in); - return 1; - } - needCommit = sqlite3_get_autocommit(p->db); - if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0); - do{ - int startLine = sCtx.nLine; - for(i=0; imode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break; - sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT); - if( i=nCol ){ - sqlite3_step(pStmt); - rc = sqlite3_reset(pStmt); - if( rc!=SQLITE_OK ){ - utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile, - startLine, sqlite3_errmsg(p->db)); - } - } - }while( sCtx.cTerm!=EOF ); - - xCloser(sCtx.in); - sqlite3_free(sCtx.z); - sqlite3_finalize(pStmt); - if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0); - }else - - if( c=='i' && (strncmp(azArg[0], "indices", n)==0 - || strncmp(azArg[0], "indexes", n)==0) ){ - ShellState data; - char *zErrMsg = 0; - open_db(p, 0); - memcpy(&data, p, sizeof(data)); - data.showHeader = 0; - data.cMode = data.mode = MODE_List; - if( nArg==1 ){ - rc = sqlite3_exec(p->db, - "SELECT name FROM sqlite_master " - "WHERE type='index' AND name NOT LIKE 'sqlite_%' " - "UNION ALL " - "SELECT name FROM sqlite_temp_master " - "WHERE type='index' " - "ORDER BY 1", - callback, &data, &zErrMsg - ); - }else if( nArg==2 ){ - zShellStatic = azArg[1]; - rc = sqlite3_exec(p->db, - "SELECT name FROM sqlite_master " - "WHERE type='index' AND tbl_name LIKE shellstatic() " - "UNION ALL " - "SELECT name FROM sqlite_temp_master " - "WHERE type='index' AND tbl_name LIKE shellstatic() " - "ORDER BY 1", - callback, &data, &zErrMsg - ); - zShellStatic = 0; - }else{ - raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n"); - rc = 1; - goto meta_command_exit; - } - if( zErrMsg ){ - utf8_printf(stderr,"Error: %s\n", zErrMsg); - sqlite3_free(zErrMsg); - rc = 1; - }else if( rc != SQLITE_OK ){ - raw_printf(stderr, - "Error: querying sqlite_master and sqlite_temp_master\n"); - rc = 1; - } - }else - -#ifdef SQLITE_ENABLE_IOTRACE - if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){ - SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...); - if( iotrace && iotrace!=stdout ) fclose(iotrace); - iotrace = 0; - if( nArg<2 ){ - sqlite3IoTrace = 0; - }else if( strcmp(azArg[1], "-")==0 ){ - sqlite3IoTrace = iotracePrintf; - iotrace = stdout; - }else{ - iotrace = fopen(azArg[1], "w"); - if( iotrace==0 ){ - utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]); - sqlite3IoTrace = 0; - rc = 1; - }else{ - sqlite3IoTrace = iotracePrintf; - } - } - }else -#endif - if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){ - static const struct { - const char *zLimitName; /* Name of a limit */ - int limitCode; /* Integer code for that limit */ - } aLimit[] = { - { "length", SQLITE_LIMIT_LENGTH }, - { "sql_length", SQLITE_LIMIT_SQL_LENGTH }, - { "column", SQLITE_LIMIT_COLUMN }, - { "expr_depth", SQLITE_LIMIT_EXPR_DEPTH }, - { "compound_select", SQLITE_LIMIT_COMPOUND_SELECT }, - { "vdbe_op", SQLITE_LIMIT_VDBE_OP }, - { "function_arg", SQLITE_LIMIT_FUNCTION_ARG }, - { "attached", SQLITE_LIMIT_ATTACHED }, - { "like_pattern_length", SQLITE_LIMIT_LIKE_PATTERN_LENGTH }, - { "variable_number", SQLITE_LIMIT_VARIABLE_NUMBER }, - { "trigger_depth", SQLITE_LIMIT_TRIGGER_DEPTH }, - { "worker_threads", SQLITE_LIMIT_WORKER_THREADS }, - }; - int i, n2; - open_db(p, 0); - if( nArg==1 ){ - for(i=0; idb, aLimit[i].limitCode, -1)); - } - }else if( nArg>3 ){ - raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n"); - rc = 1; - goto meta_command_exit; - }else{ - int iLimit = -1; - n2 = strlen30(azArg[1]); - for(i=0; idb, aLimit[iLimit].limitCode, - (int)integerValue(azArg[2])); - } - printf("%20s %d\n", aLimit[iLimit].zLimitName, - sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1)); - } - }else - -#ifndef SQLITE_OMIT_LOAD_EXTENSION - if( c=='l' && strncmp(azArg[0], "load", n)==0 ){ - const char *zFile, *zProc; - char *zErrMsg = 0; - if( nArg<2 ){ - raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n"); - rc = 1; - goto meta_command_exit; - } - zFile = azArg[1]; - zProc = nArg>=3 ? azArg[2] : 0; - open_db(p, 0); - rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg); - if( rc!=SQLITE_OK ){ - utf8_printf(stderr, "Error: %s\n", zErrMsg); - sqlite3_free(zErrMsg); - rc = 1; - } - }else -#endif - - if( c=='l' && strncmp(azArg[0], "log", n)==0 ){ - if( nArg!=2 ){ - raw_printf(stderr, "Usage: .log FILENAME\n"); - rc = 1; - }else{ - const char *zFile = azArg[1]; - output_file_close(p->pLog); - p->pLog = output_file_open(zFile); - } - }else - - if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){ - const char *zMode = nArg>=2 ? azArg[1] : ""; - int n2 = (int)strlen(zMode); - int c2 = zMode[0]; - if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){ - p->mode = MODE_Line; - }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){ - p->mode = MODE_Column; - }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){ - p->mode = MODE_List; - }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){ - p->mode = MODE_Html; - }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){ - p->mode = MODE_Tcl; - sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space); - }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){ - p->mode = MODE_Csv; - sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma); - sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf); - }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){ - p->mode = MODE_List; - sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab); - }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){ - p->mode = MODE_Insert; - set_table_name(p, nArg>=3 ? azArg[2] : "table"); - }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){ - p->mode = MODE_Ascii; - sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit); - sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record); - }else { - raw_printf(stderr, "Error: mode should be one of: " - "ascii column csv html insert line list tabs tcl\n"); - rc = 1; - } - p->cMode = p->mode; - }else - - if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){ - if( nArg==2 ){ - sqlite3_snprintf(sizeof(p->nullValue), p->nullValue, - "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]); - }else{ - raw_printf(stderr, "Usage: .nullvalue STRING\n"); - rc = 1; - } - }else - - if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){ - sqlite3 *savedDb = p->db; - const char *zSavedFilename = p->zDbFilename; - char *zNewFilename = 0; - p->db = 0; - if( nArg>=2 ) zNewFilename = sqlite3_mprintf("%s", azArg[1]); - p->zDbFilename = zNewFilename; - open_db(p, 1); - if( p->db!=0 ){ - sqlite3_close(savedDb); - sqlite3_free(p->zFreeOnClose); - p->zFreeOnClose = zNewFilename; - }else{ - sqlite3_free(zNewFilename); - p->db = savedDb; - p->zDbFilename = zSavedFilename; - } - }else - - if( c=='o' - && (strncmp(azArg[0], "output", n)==0 || strncmp(azArg[0], "once", n)==0) - ){ - const char *zFile = nArg>=2 ? azArg[1] : "stdout"; - if( nArg>2 ){ - utf8_printf(stderr, "Usage: .%s FILE\n", azArg[0]); - rc = 1; - goto meta_command_exit; - } - if( n>1 && strncmp(azArg[0], "once", n)==0 ){ - if( nArg<2 ){ - raw_printf(stderr, "Usage: .once FILE\n"); - rc = 1; - goto meta_command_exit; - } - p->outCount = 2; - }else{ - p->outCount = 0; - } - output_reset(p); - if( zFile[0]=='|' ){ -#ifdef SQLITE_OMIT_POPEN - raw_printf(stderr, "Error: pipes are not supported in this OS\n"); - rc = 1; - p->out = stdout; -#else - p->out = popen(zFile + 1, "w"); - if( p->out==0 ){ - utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1); - p->out = stdout; - rc = 1; - }else{ - sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile); - } -#endif - }else{ - p->out = output_file_open(zFile); - if( p->out==0 ){ - if( strcmp(zFile,"off")!=0 ){ - utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile); - } - p->out = stdout; - rc = 1; - } else { - sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile); - } - } - }else - - if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){ - int i; - for(i=1; i1 ) raw_printf(p->out, " "); - utf8_printf(p->out, "%s", azArg[i]); - } - raw_printf(p->out, "\n"); - }else - - if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){ - if( nArg >= 2) { - strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1); - } - if( nArg >= 3) { - strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1); - } - }else - - if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){ - rc = 2; - }else - - if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){ - FILE *alt; - if( nArg!=2 ){ - raw_printf(stderr, "Usage: .read FILE\n"); - rc = 1; - goto meta_command_exit; - } - alt = fopen(azArg[1], "rb"); - if( alt==0 ){ - utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]); - rc = 1; - }else{ - rc = process_input(p, alt); - fclose(alt); - } - }else - - if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){ - const char *zSrcFile; - const char *zDb; - sqlite3 *pSrc; - sqlite3_backup *pBackup; - int nTimeout = 0; - - if( nArg==2 ){ - zSrcFile = azArg[1]; - zDb = "main"; - }else if( nArg==3 ){ - zSrcFile = azArg[2]; - zDb = azArg[1]; - }else{ - raw_printf(stderr, "Usage: .restore ?DB? FILE\n"); - rc = 1; - goto meta_command_exit; - } - rc = sqlite3_open(zSrcFile, &pSrc); - if( rc!=SQLITE_OK ){ - utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile); - sqlite3_close(pSrc); - return 1; - } - open_db(p, 0); - pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main"); - if( pBackup==0 ){ - utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); - sqlite3_close(pSrc); - return 1; - } - while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK - || rc==SQLITE_BUSY ){ - if( rc==SQLITE_BUSY ){ - if( nTimeout++ >= 3 ) break; - sqlite3_sleep(100); - } - } - sqlite3_backup_finish(pBackup); - if( rc==SQLITE_DONE ){ - rc = 0; - }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){ - raw_printf(stderr, "Error: source database is busy\n"); - rc = 1; - }else{ - utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); - rc = 1; - } - sqlite3_close(pSrc); - }else - - - if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){ - if( nArg==2 ){ - p->scanstatsOn = booleanValue(azArg[1]); -#ifndef SQLITE_ENABLE_STMT_SCANSTATUS - raw_printf(stderr, "Warning: .scanstats not available in this build.\n"); -#endif - }else{ - raw_printf(stderr, "Usage: .scanstats on|off\n"); - rc = 1; - } - }else - - if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){ - ShellState data; - char *zErrMsg = 0; - open_db(p, 0); - memcpy(&data, p, sizeof(data)); - data.showHeader = 0; - data.cMode = data.mode = MODE_Semi; - if( nArg==2 ){ - int i; - for(i=0; azArg[1][i]; i++) azArg[1][i] = ToLower(azArg[1][i]); - if( strcmp(azArg[1],"sqlite_master")==0 ){ - char *new_argv[2], *new_colv[2]; - new_argv[0] = "CREATE TABLE sqlite_master (\n" - " type text,\n" - " name text,\n" - " tbl_name text,\n" - " rootpage integer,\n" - " sql text\n" - ")"; - new_argv[1] = 0; - new_colv[0] = "sql"; - new_colv[1] = 0; - callback(&data, 1, new_argv, new_colv); - rc = SQLITE_OK; - }else if( strcmp(azArg[1],"sqlite_temp_master")==0 ){ - char *new_argv[2], *new_colv[2]; - new_argv[0] = "CREATE TEMP TABLE sqlite_temp_master (\n" - " type text,\n" - " name text,\n" - " tbl_name text,\n" - " rootpage integer,\n" - " sql text\n" - ")"; - new_argv[1] = 0; - new_colv[0] = "sql"; - new_colv[1] = 0; - callback(&data, 1, new_argv, new_colv); - rc = SQLITE_OK; - }else{ - zShellStatic = azArg[1]; - rc = sqlite3_exec(p->db, - "SELECT sql FROM " - " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x" - " FROM sqlite_master UNION ALL" - " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) " - "WHERE lower(tbl_name) LIKE shellstatic()" - " AND type!='meta' AND sql NOTNULL " - "ORDER BY rowid", - callback, &data, &zErrMsg); - zShellStatic = 0; - } - }else if( nArg==1 ){ - rc = sqlite3_exec(p->db, - "SELECT sql FROM " - " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x" - " FROM sqlite_master UNION ALL" - " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) " - "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' " - "ORDER BY rowid", - callback, &data, &zErrMsg - ); - }else{ - raw_printf(stderr, "Usage: .schema ?LIKE-PATTERN?\n"); - rc = 1; - goto meta_command_exit; - } - if( zErrMsg ){ - utf8_printf(stderr,"Error: %s\n", zErrMsg); - sqlite3_free(zErrMsg); - rc = 1; - }else if( rc != SQLITE_OK ){ - raw_printf(stderr,"Error: querying schema information\n"); - rc = 1; - }else{ - rc = 0; - } - }else - - -#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) - if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){ - extern int sqlite3SelectTrace; - sqlite3SelectTrace = integerValue(azArg[1]); - }else -#endif - - -#ifdef SQLITE_DEBUG - /* Undocumented commands for internal testing. Subject to change - ** without notice. */ - if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){ - if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){ - int i, v; - for(i=1; iout, "%s: %d 0x%x\n", azArg[i], v, v); - } - } - if( strncmp(azArg[0]+9, "integer", n-9)==0 ){ - int i; sqlite3_int64 v; - for(i=1; iout, "%s", zBuf); - } - } - }else -#endif - - if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){ - if( nArg<2 || nArg>3 ){ - raw_printf(stderr, "Usage: .separator COL ?ROW?\n"); - rc = 1; - } - if( nArg>=2 ){ - sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, - "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]); - } - if( nArg>=3 ){ - sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, - "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]); - } - }else - - if( c=='s' - && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0) - ){ - char *zCmd; - int i, x; - if( nArg<2 ){ - raw_printf(stderr, "Usage: .system COMMAND\n"); - rc = 1; - goto meta_command_exit; - } - zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]); - for(i=2; iout, "%12.12s: %s\n","echo", p->echoOn ? "on" : "off"); - utf8_printf(p->out, "%12.12s: %s\n","eqp", p->autoEQP ? "on" : "off"); - utf8_printf(p->out, "%12.12s: %s\n","explain", - p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off"); - utf8_printf(p->out,"%12.12s: %s\n","headers", p->showHeader ? "on" : "off"); - utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]); - utf8_printf(p->out, "%12.12s: ", "nullvalue"); - output_c_string(p->out, p->nullValue); - raw_printf(p->out, "\n"); - utf8_printf(p->out,"%12.12s: %s\n","output", - strlen30(p->outfile) ? p->outfile : "stdout"); - utf8_printf(p->out,"%12.12s: ", "colseparator"); - output_c_string(p->out, p->colSeparator); - raw_printf(p->out, "\n"); - utf8_printf(p->out,"%12.12s: ", "rowseparator"); - output_c_string(p->out, p->rowSeparator); - raw_printf(p->out, "\n"); - utf8_printf(p->out, "%12.12s: %s\n","stats", p->statsOn ? "on" : "off"); - utf8_printf(p->out, "%12.12s: ", "width"); - for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) { - raw_printf(p->out, "%d ", p->colWidth[i]); - } - raw_printf(p->out, "\n"); - }else - - if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){ - if( nArg==2 ){ - p->statsOn = booleanValue(azArg[1]); - }else{ - raw_printf(stderr, "Usage: .stats on|off\n"); - rc = 1; - } - }else - - if( c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0 ){ - sqlite3_stmt *pStmt; - char **azResult; - int nRow, nAlloc; - char *zSql = 0; - int ii; - open_db(p, 0); - rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0); - if( rc ) return shellDatabaseError(p->db); - - /* Create an SQL statement to query for the list of tables in the - ** main and all attached databases where the table name matches the - ** LIKE pattern bound to variable "?1". */ - zSql = sqlite3_mprintf( - "SELECT name FROM sqlite_master" - " WHERE type IN ('table','view')" - " AND name NOT LIKE 'sqlite_%%'" - " AND name LIKE ?1"); - while( zSql && sqlite3_step(pStmt)==SQLITE_ROW ){ - const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1); - if( zDbName==0 || strcmp(zDbName,"main")==0 ) continue; - if( strcmp(zDbName,"temp")==0 ){ - zSql = sqlite3_mprintf( - "%z UNION ALL " - "SELECT 'temp.' || name FROM sqlite_temp_master" - " WHERE type IN ('table','view')" - " AND name NOT LIKE 'sqlite_%%'" - " AND name LIKE ?1", zSql); - }else{ - zSql = sqlite3_mprintf( - "%z UNION ALL " - "SELECT '%q.' || name FROM \"%w\".sqlite_master" - " WHERE type IN ('table','view')" - " AND name NOT LIKE 'sqlite_%%'" - " AND name LIKE ?1", zSql, zDbName, zDbName); - } - } - rc = sqlite3_finalize(pStmt); - if( zSql && rc==SQLITE_OK ){ - zSql = sqlite3_mprintf("%z ORDER BY 1", zSql); - if( zSql ) rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); - } - sqlite3_free(zSql); - if( !zSql ) return shellNomemError(); - if( rc ) return shellDatabaseError(p->db); - - /* Run the SQL statement prepared by the above block. Store the results - ** as an array of nul-terminated strings in azResult[]. */ - nRow = nAlloc = 0; - azResult = 0; - if( nArg>1 ){ - sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT); - }else{ - sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC); - } - while( sqlite3_step(pStmt)==SQLITE_ROW ){ - if( nRow>=nAlloc ){ - char **azNew; - int n2 = nAlloc*2 + 10; - azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2); - if( azNew==0 ){ - rc = shellNomemError(); - break; - } - nAlloc = n2; - azResult = azNew; - } - azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0)); - if( 0==azResult[nRow] ){ - rc = shellNomemError(); - break; - } - nRow++; - } - if( sqlite3_finalize(pStmt)!=SQLITE_OK ){ - rc = shellDatabaseError(p->db); - } - - /* Pretty-print the contents of array azResult[] to the output */ - if( rc==0 && nRow>0 ){ - int len, maxlen = 0; - int i, j; - int nPrintCol, nPrintRow; - for(i=0; imaxlen ) maxlen = len; - } - nPrintCol = 80/(maxlen+2); - if( nPrintCol<1 ) nPrintCol = 1; - nPrintRow = (nRow + nPrintCol - 1)/nPrintCol; - for(i=0; iout, "%s%-*s", zSp, maxlen, - azResult[j] ? azResult[j]:""); - } - raw_printf(p->out, "\n"); - } - } - - for(ii=0; ii=8 && strncmp(azArg[0], "testctrl", n)==0 && nArg>=2 ){ - static const struct { - const char *zCtrlName; /* Name of a test-control option */ - int ctrlCode; /* Integer code for that option */ - } aCtrl[] = { - { "prng_save", SQLITE_TESTCTRL_PRNG_SAVE }, - { "prng_restore", SQLITE_TESTCTRL_PRNG_RESTORE }, - { "prng_reset", SQLITE_TESTCTRL_PRNG_RESET }, - { "bitvec_test", SQLITE_TESTCTRL_BITVEC_TEST }, - { "fault_install", SQLITE_TESTCTRL_FAULT_INSTALL }, - { "benign_malloc_hooks", SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS }, - { "pending_byte", SQLITE_TESTCTRL_PENDING_BYTE }, - { "assert", SQLITE_TESTCTRL_ASSERT }, - { "always", SQLITE_TESTCTRL_ALWAYS }, - { "reserve", SQLITE_TESTCTRL_RESERVE }, - { "optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS }, - { "iskeyword", SQLITE_TESTCTRL_ISKEYWORD }, - { "scratchmalloc", SQLITE_TESTCTRL_SCRATCHMALLOC }, - { "byteorder", SQLITE_TESTCTRL_BYTEORDER }, - { "never_corrupt", SQLITE_TESTCTRL_NEVER_CORRUPT }, - { "imposter", SQLITE_TESTCTRL_IMPOSTER }, - }; - int testctrl = -1; - int rc2 = 0; - int i, n2; - open_db(p, 0); - - /* convert testctrl text option to value. allow any unique prefix - ** of the option name, or a numerical value. */ - n2 = strlen30(azArg[1]); - for(i=0; iSQLITE_TESTCTRL_LAST) ){ - utf8_printf(stderr,"Error: invalid testctrl option: %s\n", azArg[1]); - }else{ - switch(testctrl){ - - /* sqlite3_test_control(int, db, int) */ - case SQLITE_TESTCTRL_OPTIMIZATIONS: - case SQLITE_TESTCTRL_RESERVE: - if( nArg==3 ){ - int opt = (int)strtol(azArg[2], 0, 0); - rc2 = sqlite3_test_control(testctrl, p->db, opt); - raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); - } else { - utf8_printf(stderr,"Error: testctrl %s takes a single int option\n", - azArg[1]); - } - break; - - /* sqlite3_test_control(int) */ - case SQLITE_TESTCTRL_PRNG_SAVE: - case SQLITE_TESTCTRL_PRNG_RESTORE: - case SQLITE_TESTCTRL_PRNG_RESET: - case SQLITE_TESTCTRL_BYTEORDER: - if( nArg==2 ){ - rc2 = sqlite3_test_control(testctrl); - raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); - } else { - utf8_printf(stderr,"Error: testctrl %s takes no options\n", - azArg[1]); - } - break; - - /* sqlite3_test_control(int, uint) */ - case SQLITE_TESTCTRL_PENDING_BYTE: - if( nArg==3 ){ - unsigned int opt = (unsigned int)integerValue(azArg[2]); - rc2 = sqlite3_test_control(testctrl, opt); - raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); - } else { - utf8_printf(stderr,"Error: testctrl %s takes a single unsigned" - " int option\n", azArg[1]); - } - break; - - /* sqlite3_test_control(int, int) */ - case SQLITE_TESTCTRL_ASSERT: - case SQLITE_TESTCTRL_ALWAYS: - case SQLITE_TESTCTRL_NEVER_CORRUPT: - if( nArg==3 ){ - int opt = booleanValue(azArg[2]); - rc2 = sqlite3_test_control(testctrl, opt); - raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); - } else { - utf8_printf(stderr,"Error: testctrl %s takes a single int option\n", - azArg[1]); - } - break; - - /* sqlite3_test_control(int, char *) */ -#ifdef SQLITE_N_KEYWORD - case SQLITE_TESTCTRL_ISKEYWORD: - if( nArg==3 ){ - const char *opt = azArg[2]; - rc2 = sqlite3_test_control(testctrl, opt); - raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); - } else { - utf8_printf(stderr, - "Error: testctrl %s takes a single char * option\n", - azArg[1]); - } - break; -#endif - - case SQLITE_TESTCTRL_IMPOSTER: - if( nArg==5 ){ - rc2 = sqlite3_test_control(testctrl, p->db, - azArg[2], - integerValue(azArg[3]), - integerValue(azArg[4])); - raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); - }else{ - raw_printf(stderr,"Usage: .testctrl imposter dbName onoff tnum\n"); - } - break; - - case SQLITE_TESTCTRL_BITVEC_TEST: - case SQLITE_TESTCTRL_FAULT_INSTALL: - case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS: - case SQLITE_TESTCTRL_SCRATCHMALLOC: - default: - utf8_printf(stderr, - "Error: CLI support for testctrl %s not implemented\n", - azArg[1]); - break; - } - } - }else - - if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){ - open_db(p, 0); - sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0); - }else - - if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){ - if( nArg==2 ){ - enableTimer = booleanValue(azArg[1]); - if( enableTimer && !HAS_TIMER ){ - raw_printf(stderr, "Error: timer not available on this system.\n"); - enableTimer = 0; - } - }else{ - raw_printf(stderr, "Usage: .timer on|off\n"); - rc = 1; - } - }else - - if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){ - open_db(p, 0); - if( nArg!=2 ){ - raw_printf(stderr, "Usage: .trace FILE|off\n"); - rc = 1; - goto meta_command_exit; - } - output_file_close(p->traceOut); - p->traceOut = output_file_open(azArg[1]); -#if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) - if( p->traceOut==0 ){ - sqlite3_trace(p->db, 0, 0); - }else{ - sqlite3_trace(p->db, sql_trace_callback, p->traceOut); - } -#endif - }else - -#if SQLITE_USER_AUTHENTICATION - if( c=='u' && strncmp(azArg[0], "user", n)==0 ){ - if( nArg<2 ){ - raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n"); - rc = 1; - goto meta_command_exit; - } - open_db(p, 0); - if( strcmp(azArg[1],"login")==0 ){ - if( nArg!=4 ){ - raw_printf(stderr, "Usage: .user login USER PASSWORD\n"); - rc = 1; - goto meta_command_exit; - } - rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3], - (int)strlen(azArg[3])); - if( rc ){ - utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]); - rc = 1; - } - }else if( strcmp(azArg[1],"add")==0 ){ - if( nArg!=5 ){ - raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n"); - rc = 1; - goto meta_command_exit; - } - rc = sqlite3_user_add(p->db, azArg[2], - azArg[3], (int)strlen(azArg[3]), - booleanValue(azArg[4])); - if( rc ){ - raw_printf(stderr, "User-Add failed: %d\n", rc); - rc = 1; - } - }else if( strcmp(azArg[1],"edit")==0 ){ - if( nArg!=5 ){ - raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n"); - rc = 1; - goto meta_command_exit; - } - rc = sqlite3_user_change(p->db, azArg[2], - azArg[3], (int)strlen(azArg[3]), - booleanValue(azArg[4])); - if( rc ){ - raw_printf(stderr, "User-Edit failed: %d\n", rc); - rc = 1; - } - }else if( strcmp(azArg[1],"delete")==0 ){ - if( nArg!=3 ){ - raw_printf(stderr, "Usage: .user delete USER\n"); - rc = 1; - goto meta_command_exit; - } - rc = sqlite3_user_delete(p->db, azArg[2]); - if( rc ){ - raw_printf(stderr, "User-Delete failed: %d\n", rc); - rc = 1; - } - }else{ - raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n"); - rc = 1; - goto meta_command_exit; - } - }else -#endif /* SQLITE_USER_AUTHENTICATION */ - - if( c=='v' && strncmp(azArg[0], "version", n)==0 ){ - utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/, - sqlite3_libversion(), sqlite3_sourceid()); - }else - - if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){ - const char *zDbName = nArg==2 ? azArg[1] : "main"; - sqlite3_vfs *pVfs; - if( p->db ){ - sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs); - if( pVfs ){ - utf8_printf(p->out, "vfs.zName = \"%s\"\n", pVfs->zName); - raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion); - raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile); - raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname); - } - } - }else - - if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){ - sqlite3_vfs *pVfs; - sqlite3_vfs *pCurrent = 0; - if( p->db ){ - sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent); - } - for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){ - utf8_printf(p->out, "vfs.zName = \"%s\"%s\n", pVfs->zName, - pVfs==pCurrent ? " <--- CURRENT" : ""); - raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion); - raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile); - raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname); - if( pVfs->pNext ){ - raw_printf(p->out, "-----------------------------------\n"); - } - } - }else - - if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){ - const char *zDbName = nArg==2 ? azArg[1] : "main"; - char *zVfsName = 0; - if( p->db ){ - sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName); - if( zVfsName ){ - utf8_printf(p->out, "%s\n", zVfsName); - sqlite3_free(zVfsName); - } - } - }else - -#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) - if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){ - extern int sqlite3WhereTrace; - sqlite3WhereTrace = nArg>=2 ? booleanValue(azArg[1]) : 0xff; - }else -#endif - - if( c=='w' && strncmp(azArg[0], "width", n)==0 ){ - int j; - assert( nArg<=ArraySize(azArg) ); - for(j=1; jcolWidth); j++){ - p->colWidth[j-1] = (int)integerValue(azArg[j]); - } - }else - - { - utf8_printf(stderr, "Error: unknown command or invalid arguments: " - " \"%s\". Enter \".help\" for help\n", azArg[0]); - rc = 1; - } - -meta_command_exit: - if( p->outCount ){ - p->outCount--; - if( p->outCount==0 ) output_reset(p); - } - return rc; -} - -/* -** Return TRUE if a semicolon occurs anywhere in the first N characters -** of string z[]. -*/ -static int line_contains_semicolon(const char *z, int N){ - int i; - for(i=0; iout); - zLine = one_input_line(in, zLine, nSql>0); - if( zLine==0 ){ - /* End of input */ - if( stdin_is_interactive ) printf("\n"); - break; - } - if( seenInterrupt ){ - if( in!=0 ) break; - seenInterrupt = 0; - } - lineno++; - if( nSql==0 && _all_whitespace(zLine) ){ - if( p->echoOn ) printf("%s\n", zLine); - continue; - } - if( zLine && zLine[0]=='.' && nSql==0 ){ - if( p->echoOn ) printf("%s\n", zLine); - rc = do_meta_command(zLine, p); - if( rc==2 ){ /* exit requested */ - break; - }else if( rc ){ - errCnt++; - } - continue; - } - if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){ - memcpy(zLine,";",2); - } - nLine = strlen30(zLine); - if( nSql+nLine+2>=nAlloc ){ - nAlloc = nSql+nLine+100; - zSql = realloc(zSql, nAlloc); - if( zSql==0 ){ - raw_printf(stderr, "Error: out of memory\n"); - exit(1); - } - } - nSqlPrior = nSql; - if( nSql==0 ){ - int i; - for(i=0; zLine[i] && IsSpace(zLine[i]); i++){} - assert( nAlloc>0 && zSql!=0 ); - memcpy(zSql, zLine+i, nLine+1-i); - startline = lineno; - nSql = nLine-i; - }else{ - zSql[nSql++] = '\n'; - memcpy(zSql+nSql, zLine, nLine+1); - nSql += nLine; - } - if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior) - && sqlite3_complete(zSql) ){ - p->cnt = 0; - open_db(p, 0); - if( p->backslashOn ) resolve_backslashes(zSql); - BEGIN_TIMER; - rc = shell_exec(p->db, zSql, shell_callback, p, &zErrMsg); - END_TIMER; - if( rc || zErrMsg ){ - char zPrefix[100]; - if( in!=0 || !stdin_is_interactive ){ - sqlite3_snprintf(sizeof(zPrefix), zPrefix, - "Error: near line %d:", startline); - }else{ - sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:"); - } - if( zErrMsg!=0 ){ - utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg); - sqlite3_free(zErrMsg); - zErrMsg = 0; - }else{ - utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db)); - } - errCnt++; - }else if( p->countChanges ){ - raw_printf(p->out, "changes: %3d total_changes: %d\n", - sqlite3_changes(p->db), sqlite3_total_changes(p->db)); - } - nSql = 0; - if( p->outCount ){ - output_reset(p); - p->outCount = 0; - } - }else if( nSql && _all_whitespace(zSql) ){ - if( p->echoOn ) printf("%s\n", zSql); - nSql = 0; - } - } - if( nSql ){ - if( !_all_whitespace(zSql) ){ - utf8_printf(stderr, "Error: incomplete SQL: %s\n", zSql); - errCnt++; - } - } - free(zSql); - free(zLine); - return errCnt>0; -} - -/* -** Return a pathname which is the user's home directory. A -** 0 return indicates an error of some kind. -*/ -static char *find_home_dir(void){ - static char *home_dir = NULL; - if( home_dir ) return home_dir; - -#if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \ - && !defined(__RTP__) && !defined(_WRS_KERNEL) - { - struct passwd *pwent; - uid_t uid = getuid(); - if( (pwent=getpwuid(uid)) != NULL) { - home_dir = pwent->pw_dir; - } - } -#endif - -#if defined(_WIN32_WCE) - /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv() - */ - home_dir = "/"; -#else - -#if defined(_WIN32) || defined(WIN32) - if (!home_dir) { - home_dir = getenv("USERPROFILE"); - } -#endif - - if (!home_dir) { - home_dir = getenv("HOME"); - } - -#if defined(_WIN32) || defined(WIN32) - if (!home_dir) { - char *zDrive, *zPath; - int n; - zDrive = getenv("HOMEDRIVE"); - zPath = getenv("HOMEPATH"); - if( zDrive && zPath ){ - n = strlen30(zDrive) + strlen30(zPath) + 1; - home_dir = malloc( n ); - if( home_dir==0 ) return 0; - sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath); - return home_dir; - } - home_dir = "c:\\"; - } -#endif - -#endif /* !_WIN32_WCE */ - - if( home_dir ){ - int n = strlen30(home_dir) + 1; - char *z = malloc( n ); - if( z ) memcpy(z, home_dir, n); - home_dir = z; - } - - return home_dir; -} - -/* -** Read input from the file given by sqliterc_override. Or if that -** parameter is NULL, take input from ~/.sqliterc -** -** Returns the number of errors. -*/ -static void process_sqliterc( - ShellState *p, /* Configuration data */ - const char *sqliterc_override /* Name of config file. NULL to use default */ -){ - char *home_dir = NULL; - const char *sqliterc = sqliterc_override; - char *zBuf = 0; - FILE *in = NULL; - - if (sqliterc == NULL) { - home_dir = find_home_dir(); - if( home_dir==0 ){ - raw_printf(stderr, "-- warning: cannot find home directory;" - " cannot read ~/.sqliterc\n"); - return; - } - sqlite3_initialize(); - zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir); - sqliterc = zBuf; - } - in = fopen(sqliterc,"rb"); - if( in ){ - if( stdin_is_interactive ){ - utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc); - } - process_input(p,in); - fclose(in); - } - sqlite3_free(zBuf); -} - -/* -** Show available command line options -*/ -static const char zOptions[] = - " -ascii set output mode to 'ascii'\n" - " -bail stop after hitting an error\n" - " -batch force batch I/O\n" - " -column set output mode to 'column'\n" - " -cmd COMMAND run \"COMMAND\" before reading stdin\n" - " -csv set output mode to 'csv'\n" - " -echo print commands before execution\n" - " -init FILENAME read/process named file\n" - " -[no]header turn headers on or off\n" -#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) - " -heap SIZE Size of heap for memsys3 or memsys5\n" -#endif - " -help show this message\n" - " -html set output mode to HTML\n" - " -interactive force interactive I/O\n" - " -line set output mode to 'line'\n" - " -list set output mode to 'list'\n" - " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n" - " -mmap N default mmap size set to N\n" -#ifdef SQLITE_ENABLE_MULTIPLEX - " -multiplex enable the multiplexor VFS\n" -#endif - " -newline SEP set output row separator. Default: '\\n'\n" - " -nullvalue TEXT set text string for NULL values. Default ''\n" - " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n" - " -scratch SIZE N use N slots of SZ bytes each for scratch memory\n" - " -separator SEP set output column separator. Default: '|'\n" - " -stats print memory stats before each finalize\n" - " -version show SQLite version\n" - " -vfs NAME use NAME as the default VFS\n" -#ifdef SQLITE_ENABLE_VFSTRACE - " -vfstrace enable tracing of all VFS calls\n" -#endif -; -static void usage(int showDetail){ - utf8_printf(stderr, - "Usage: %s [OPTIONS] FILENAME [SQL]\n" - "FILENAME is the name of an SQLite database. A new database is created\n" - "if the file does not previously exist.\n", Argv0); - if( showDetail ){ - utf8_printf(stderr, "OPTIONS include:\n%s", zOptions); - }else{ - raw_printf(stderr, "Use the -help option for additional information\n"); - } - exit(1); -} - -/* -** Initialize the state information in data -*/ -static void main_init(ShellState *data) { - memset(data, 0, sizeof(*data)); - data->normalMode = data->cMode = data->mode = MODE_List; - data->autoExplain = 1; - memcpy(data->colSeparator,SEP_Column, 2); - memcpy(data->rowSeparator,SEP_Row, 2); - data->showHeader = 0; - data->shellFlgs = SHFLG_Lookaside; - sqlite3_config(SQLITE_CONFIG_URI, 1); - sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data); - sqlite3_config(SQLITE_CONFIG_MULTITHREAD); - sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> "); - sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> "); -} - -/* -** Output text to the console in a font that attracts extra attention. -*/ -#ifdef _WIN32 -static void printBold(const char *zText){ - HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE); - CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo; - GetConsoleScreenBufferInfo(out, &defaultScreenInfo); - SetConsoleTextAttribute(out, - FOREGROUND_RED|FOREGROUND_INTENSITY - ); - printf("%s", zText); - SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes); -} -#else -static void printBold(const char *zText){ - printf("\033[1m%s\033[0m", zText); -} -#endif - -/* -** Get the argument to an --option. Throw an error and die if no argument -** is available. -*/ -static char *cmdline_option_value(int argc, char **argv, int i){ - if( i==argc ){ - utf8_printf(stderr, "%s: Error: missing argument to %s\n", - argv[0], argv[argc-1]); - exit(1); - } - return argv[i]; -} - -int SQLITE_CDECL main(int argc, char **argv){ - char *zErrMsg = 0; - ShellState data; - const char *zInitFile = 0; - int i; - int rc = 0; - int warnInmemoryDb = 0; - int readStdin = 1; - int nCmd = 0; - char **azCmd = 0; - -#if USE_SYSTEM_SQLITE+0!=1 - if( strcmp(sqlite3_sourceid(),SQLITE_SOURCE_ID)!=0 ){ - utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n", - sqlite3_sourceid(), SQLITE_SOURCE_ID); - exit(1); - } -#endif - setBinaryMode(stdin); - setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */ - Argv0 = argv[0]; - main_init(&data); - stdin_is_interactive = isatty(0); - stdout_is_console = isatty(1); - - /* Make sure we have a valid signal handler early, before anything - ** else is done. - */ -#ifdef SIGINT - signal(SIGINT, interrupt_handler); -#endif - -#ifdef SQLITE_SHELL_DBNAME_PROC - { - /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name - ** of a C-function that will provide the name of the database file. Use - ** this compile-time option to embed this shell program in larger - ** applications. */ - extern void SQLITE_SHELL_DBNAME_PROC(const char**); - SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename); - warnInmemoryDb = 0; - } -#endif - - /* Do an initial pass through the command-line argument to locate - ** the name of the database file, the name of the initialization file, - ** the size of the alternative malloc heap, - ** and the first command to execute. - */ - for(i=1; i0x7fff0000 ) szHeap = 0x7fff0000; - sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64); -#endif - }else if( strcmp(z,"-scratch")==0 ){ - int n, sz; - sz = (int)integerValue(cmdline_option_value(argc,argv,++i)); - if( sz>400000 ) sz = 400000; - if( sz<2500 ) sz = 2500; - n = (int)integerValue(cmdline_option_value(argc,argv,++i)); - if( n>10 ) n = 10; - if( n<1 ) n = 1; - sqlite3_config(SQLITE_CONFIG_SCRATCH, malloc(n*sz+1), sz, n); - data.shellFlgs |= SHFLG_Scratch; - }else if( strcmp(z,"-pagecache")==0 ){ - int n, sz; - sz = (int)integerValue(cmdline_option_value(argc,argv,++i)); - if( sz>70000 ) sz = 70000; - if( sz<0 ) sz = 0; - n = (int)integerValue(cmdline_option_value(argc,argv,++i)); - sqlite3_config(SQLITE_CONFIG_PAGECACHE, - (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n); - data.shellFlgs |= SHFLG_Pagecache; - }else if( strcmp(z,"-lookaside")==0 ){ - int n, sz; - sz = (int)integerValue(cmdline_option_value(argc,argv,++i)); - if( sz<0 ) sz = 0; - n = (int)integerValue(cmdline_option_value(argc,argv,++i)); - if( n<0 ) n = 0; - sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n); - if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside; -#ifdef SQLITE_ENABLE_VFSTRACE - }else if( strcmp(z,"-vfstrace")==0 ){ - extern int vfstrace_register( - const char *zTraceName, - const char *zOldVfsName, - int (*xOut)(const char*,void*), - void *pOutArg, - int makeDefault - ); - vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1); -#endif -#ifdef SQLITE_ENABLE_MULTIPLEX - }else if( strcmp(z,"-multiplex")==0 ){ - extern int sqlite3_multiple_initialize(const char*,int); - sqlite3_multiplex_initialize(0, 1); -#endif - }else if( strcmp(z,"-mmap")==0 ){ - sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i)); - sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz); - }else if( strcmp(z,"-vfs")==0 ){ - sqlite3_vfs *pVfs = sqlite3_vfs_find(cmdline_option_value(argc,argv,++i)); - if( pVfs ){ - sqlite3_vfs_register(pVfs, 1); - }else{ - utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]); - exit(1); - } - } - } - if( data.zDbFilename==0 ){ -#ifndef SQLITE_OMIT_MEMORYDB - data.zDbFilename = ":memory:"; - warnInmemoryDb = argc==1; -#else - utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0); - return 1; -#endif - } - data.out = stdout; - - /* Go ahead and open the database file if it already exists. If the - ** file does not exist, delay opening it. This prevents empty database - ** files from being created if a user mistypes the database name argument - ** to the sqlite command-line tool. - */ - if( access(data.zDbFilename, 0)==0 ){ - open_db(&data, 0); - } - - /* Process the initialization file if there is one. If no -init option - ** is given on the command line, look for a file named ~/.sqliterc and - ** try to process it. - */ - process_sqliterc(&data,zInitFile); - - /* Make a second pass through the command-line argument and set - ** options. This second pass is delayed until after the initialization - ** file is processed so that the command-line arguments will override - ** settings in the initialization file. - */ - for(i=1; i +#include +#include +#include +#include +#include "sqlite3.h" +typedef sqlite3_int64 i64; +typedef sqlite3_uint64 u64; +typedef unsigned char u8; +#include +#include +#ifndef _WIN32 +# include +#endif + +#if !defined(_WIN32) && !defined(WIN32) +# include +# if !defined(__RTP__) && !defined(_WRS_KERNEL) && !defined(SQLITE_WASI) +# include +# endif +#endif +#if (!defined(_WIN32) && !defined(WIN32)) || defined(__MINGW32__) +# include +# include +# define GETPID getpid +# if defined(__MINGW32__) +# define DIRENT dirent +# ifndef S_ISLNK +# define S_ISLNK(mode) (0) +# endif +# endif +#else +# define GETPID (int)GetCurrentProcessId +#endif +#include +#include + +#if HAVE_READLINE +# include +# include +#endif + +#if HAVE_EDITLINE +# include +#endif + +#if HAVE_EDITLINE || HAVE_READLINE + +# define shell_add_history(X) add_history(X) +# define shell_read_history(X) read_history(X) +# define shell_write_history(X) write_history(X) +# define shell_stifle_history(X) stifle_history(X) +# define shell_readline(X) readline(X) + +#elif HAVE_LINENOISE + +# include "linenoise.h" +# define shell_add_history(X) linenoiseHistoryAdd(X) +# define shell_read_history(X) linenoiseHistoryLoad(X) +# define shell_write_history(X) linenoiseHistorySave(X) +# define shell_stifle_history(X) linenoiseHistorySetMaxLen(X) +# define shell_readline(X) linenoise(X) + +#else + +# define shell_read_history(X) +# define shell_write_history(X) +# define shell_stifle_history(X) + +# define SHELL_USE_LOCAL_GETLINE 1 +#endif + +#ifndef deliberate_fall_through +/* Quiet some compilers about some of our intentional code. */ +# if defined(GCC_VERSION) && GCC_VERSION>=7000000 +# define deliberate_fall_through __attribute__((fallthrough)); +# else +# define deliberate_fall_through +# endif +#endif + +#if defined(_WIN32) || defined(WIN32) +# if SQLITE_OS_WINRT +# define SQLITE_OMIT_POPEN 1 +# else +# include +# include +# define isatty(h) _isatty(h) +# ifndef access +# define access(f,m) _access((f),(m)) +# endif +# ifndef unlink +# define unlink _unlink +# endif +# ifndef strdup +# define strdup _strdup +# endif +# undef pclose +# define pclose _pclose +# endif +#else + /* Make sure isatty() has a prototype. */ + extern int isatty(int); + +# if !defined(__RTP__) && !defined(_WRS_KERNEL) && !defined(SQLITE_WASI) + /* popen and pclose are not C89 functions and so are + ** sometimes omitted from the header */ + extern FILE *popen(const char*,const char*); + extern int pclose(FILE*); +# else +# define SQLITE_OMIT_POPEN 1 +# endif +#endif + +#if defined(_WIN32_WCE) +/* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty() + * thus we always assume that we have a console. That can be + * overridden with the -batch command line option. + */ +#define isatty(x) 1 +#endif + +/* ctype macros that work with signed characters */ +#define IsSpace(X) isspace((unsigned char)X) +#define IsDigit(X) isdigit((unsigned char)X) +#define ToLower(X) (char)tolower((unsigned char)X) +#define IsAlnum(X) isalnum((unsigned char)X) +#define IsAlpha(X) isalpha((unsigned char)X) + +#if defined(_WIN32) || defined(WIN32) +#if SQLITE_OS_WINRT +#include +#endif +#undef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#include + +/* string conversion routines only needed on Win32 */ +extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR); +extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText); +#endif + +INCLUDE ../ext/misc/sqlite3_stdio.h +INCLUDE ../ext/misc/sqlite3_stdio.c + +/* Use console I/O package as a direct INCLUDE. */ +#define SQLITE_INTERNAL_LINKAGE static + +#ifdef SQLITE_SHELL_FIDDLE +/* Deselect most features from the console I/O package for Fiddle. */ +# define SQLITE_CIO_NO_REDIRECT +# define SQLITE_CIO_NO_CLASSIFY +# define SQLITE_CIO_NO_TRANSLATE +# define SQLITE_CIO_NO_SETMODE +# define SQLITE_CIO_NO_FLUSH +#endif + +#define eputz(z) sqlite3_fputs(z,stderr) +#define sputz(fp,z) sqlite3_fputs(z,fp) + +/* True if the timer is enabled */ +static int enableTimer = 0; + +/* A version of strcmp() that works with NULL values */ +static int cli_strcmp(const char *a, const char *b){ + if( a==0 ) a = ""; + if( b==0 ) b = ""; + return strcmp(a,b); +} +static int cli_strncmp(const char *a, const char *b, size_t n){ + if( a==0 ) a = ""; + if( b==0 ) b = ""; + return strncmp(a,b,n); +} + +/* Return the current wall-clock time in microseconds since the +** Unix epoch (1970-01-01T00:00:00Z) +*/ +static sqlite3_int64 timeOfDay(void){ +#if defined(_WIN64) + sqlite3_uint64 t; + FILETIME tm; + GetSystemTimePreciseAsFileTime(&tm); + t = ((u64)tm.dwHighDateTime<<32) | (u64)tm.dwLowDateTime; + t += 116444736000000000LL; + t /= 10; + return t; +#elif defined(_WIN32) + static sqlite3_vfs *clockVfs = 0; + sqlite3_int64 t; + if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0); + if( clockVfs==0 ) return 0; /* Never actually happens */ + if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){ + clockVfs->xCurrentTimeInt64(clockVfs, &t); + }else{ + double r; + clockVfs->xCurrentTime(clockVfs, &r); + t = (sqlite3_int64)(r*86400000.0); + } + return t*1000; +#else + struct timeval sNow; + (void)gettimeofday(&sNow,0); + return ((i64)sNow.tv_sec)*1000000 + sNow.tv_usec; +#endif +} + +#if !defined(_WIN32) && !defined(WIN32) && !defined(__minux) +#include +#include + +/* VxWorks does not support getrusage() as far as we can determine */ +#if defined(_WRS_KERNEL) || defined(__RTP__) +struct rusage { + struct timeval ru_utime; /* user CPU time used */ + struct timeval ru_stime; /* system CPU time used */ +}; +#define getrusage(A,B) memset(B,0,sizeof(*B)) +#endif + + +/* Saved resource information for the beginning of an operation */ +static struct rusage sBegin; /* CPU time at start */ +static sqlite3_int64 iBegin; /* Wall-clock time at start */ + +/* +** Begin timing an operation +*/ +static void beginTimer(void){ + if( enableTimer ){ + getrusage(RUSAGE_SELF, &sBegin); + iBegin = timeOfDay(); + } +} + +/* Return the difference of two time_structs in seconds */ +static double timeDiff(struct timeval *pStart, struct timeval *pEnd){ + return (pEnd->tv_usec - pStart->tv_usec)*0.000001 + + (double)(pEnd->tv_sec - pStart->tv_sec); +} + +/* +** Print the timing results. +*/ +static void endTimer(FILE *out){ + if( enableTimer ){ + sqlite3_int64 iEnd = timeOfDay(); + struct rusage sEnd; + getrusage(RUSAGE_SELF, &sEnd); + sqlite3_fprintf(out, "Run Time: real %.6f user %.6f sys %.6f\n", + (iEnd - iBegin)*0.000001, + timeDiff(&sBegin.ru_utime, &sEnd.ru_utime), + timeDiff(&sBegin.ru_stime, &sEnd.ru_stime)); + } +} + +#define BEGIN_TIMER beginTimer() +#define END_TIMER(X) endTimer(X) +#define HAS_TIMER 1 + +#elif (defined(_WIN32) || defined(WIN32)) + +/* Saved resource information for the beginning of an operation */ +static HANDLE hProcess; +static FILETIME ftKernelBegin; +static FILETIME ftUserBegin; +static sqlite3_int64 ftWallBegin; +typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME, + LPFILETIME, LPFILETIME); +static GETPROCTIMES getProcessTimesAddr = NULL; + +/* +** Check to see if we have timer support. Return 1 if necessary +** support found (or found previously). +*/ +static int hasTimer(void){ + if( getProcessTimesAddr ){ + return 1; + } else { +#if !SQLITE_OS_WINRT + /* GetProcessTimes() isn't supported in WIN95 and some other Windows + ** versions. See if the version we are running on has it, and if it + ** does, save off a pointer to it and the current process handle. + */ + hProcess = GetCurrentProcess(); + if( hProcess ){ + HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll")); + if( NULL != hinstLib ){ + getProcessTimesAddr = + (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes"); + if( NULL != getProcessTimesAddr ){ + return 1; + } + FreeLibrary(hinstLib); + } + } +#endif + } + return 0; +} + +/* +** Begin timing an operation +*/ +static void beginTimer(void){ + if( enableTimer && getProcessTimesAddr ){ + FILETIME ftCreation, ftExit; + getProcessTimesAddr(hProcess,&ftCreation,&ftExit, + &ftKernelBegin,&ftUserBegin); + ftWallBegin = timeOfDay(); + } +} + +/* Return the difference of two FILETIME structs in seconds */ +static double timeDiff(FILETIME *pStart, FILETIME *pEnd){ + sqlite_int64 i64Start = *((sqlite_int64 *) pStart); + sqlite_int64 i64End = *((sqlite_int64 *) pEnd); + return (double) ((i64End - i64Start) / 10000000.0); +} + +/* +** Print the timing results. +*/ +static void endTimer(FILE *out){ + if( enableTimer && getProcessTimesAddr){ + FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd; + sqlite3_int64 ftWallEnd = timeOfDay(); + getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd); +#ifdef _WIN64 + /* microsecond precision on 64-bit windows */ + sqlite3_fprintf(out, "Run Time: real %.6f user %f sys %f\n", + (ftWallEnd - ftWallBegin)*0.000001, + timeDiff(&ftUserBegin, &ftUserEnd), + timeDiff(&ftKernelBegin, &ftKernelEnd)); +#else + /* millisecond precisino on 32-bit windows */ + sqlite3_fprintf(out, "Run Time: real %.3f user %.3f sys %.3f\n", + (ftWallEnd - ftWallBegin)*0.000001, + timeDiff(&ftUserBegin, &ftUserEnd), + timeDiff(&ftKernelBegin, &ftKernelEnd)); +#endif + } +} + +#define BEGIN_TIMER beginTimer() +#define END_TIMER(X) endTimer(X) +#define HAS_TIMER hasTimer() + +#else +#define BEGIN_TIMER +#define END_TIMER(X) /*no-op*/ +#define HAS_TIMER 0 +#endif + +/* +** Used to prevent warnings about unused parameters +*/ +#define UNUSED_PARAMETER(x) (void)(x) + +/* +** Number of elements in an array +*/ +#define ArraySize(X) (int)(sizeof(X)/sizeof(X[0])) + +/* +** If the following flag is set, then command execution stops +** at an error if we are not interactive. +*/ +static int bail_on_error = 0; + +/* +** Treat stdin as an interactive input if the following variable +** is true. Otherwise, assume stdin is connected to a file or pipe. +*/ +static int stdin_is_interactive = 1; + +/* +** On Windows systems we need to know if standard output is a console +** in order to show that UTF-16 translation is done in the sign-on +** banner. The following variable is true if it is the console. +*/ +static int stdout_is_console = 1; + +/* +** The following is the open SQLite database. We make a pointer +** to this database a static variable so that it can be accessed +** by the SIGINT handler to interrupt database processing. +*/ +static sqlite3 *globalDb = 0; + +/* +** True if an interrupt (Control-C) has been received. +*/ +static volatile int seenInterrupt = 0; + +/* +** This is the name of our program. It is set in main(), used +** in a number of other places, mostly for error messages. +*/ +static char *Argv0; + +/* +** Prompt strings. Initialized in main. Settable with +** .prompt main continue +*/ +#define PROMPT_LEN_MAX 128 +/* First line prompt. default: "sqlite> " */ +static char mainPrompt[PROMPT_LEN_MAX]; +/* Continuation prompt. default: " ...> " */ +static char continuePrompt[PROMPT_LEN_MAX]; + +/* This is variant of the standard-library strncpy() routine with the +** one change that the destination string is always zero-terminated, even +** if there is no zero-terminator in the first n-1 characters of the source +** string. +*/ +static char *shell_strncpy(char *dest, const char *src, size_t n){ + size_t i; + for(i=0; iinParenLevel += ni; + if( ni==0 ) p->inParenLevel = 0; + p->zScannerAwaits = 0; +} + +/* Record that a lexeme is opened, or closed with args==0. */ +static void setLexemeOpen(struct DynaPrompt *p, char *s, char c){ + if( s!=0 || c==0 ){ + p->zScannerAwaits = s; + p->acAwait[0] = 0; + }else{ + p->acAwait[0] = c; + p->zScannerAwaits = p->acAwait; + } +} + +/* Upon demand, derive the continuation prompt to display. */ +static char *dynamicContinuePrompt(void){ + if( continuePrompt[0]==0 + || (dynPrompt.zScannerAwaits==0 && dynPrompt.inParenLevel == 0) ){ + return continuePrompt; + }else{ + if( dynPrompt.zScannerAwaits ){ + size_t ncp = strlen(continuePrompt); + size_t ndp = strlen(dynPrompt.zScannerAwaits); + if( ndp > ncp-3 ) return continuePrompt; + shell_strcpy(dynPrompt.dynamicPrompt, dynPrompt.zScannerAwaits); + while( ndp<3 ) dynPrompt.dynamicPrompt[ndp++] = ' '; + shell_strncpy(dynPrompt.dynamicPrompt+3, continuePrompt+3, + PROMPT_LEN_MAX-4); + }else{ + if( dynPrompt.inParenLevel>9 ){ + shell_strncpy(dynPrompt.dynamicPrompt, "(..", 4); + }else if( dynPrompt.inParenLevel<0 ){ + shell_strncpy(dynPrompt.dynamicPrompt, ")x!", 4); + }else{ + shell_strncpy(dynPrompt.dynamicPrompt, "(x.", 4); + dynPrompt.dynamicPrompt[2] = (char)('0'+dynPrompt.inParenLevel); + } + shell_strncpy(dynPrompt.dynamicPrompt+3, continuePrompt+3, + PROMPT_LEN_MAX-4); + } + } + return dynPrompt.dynamicPrompt; +} +#endif /* !defined(SQLITE_OMIT_DYNAPROMPT) */ + +/* Indicate out-of-memory and exit. */ +static void shell_out_of_memory(void){ + eputz("Error: out of memory\n"); + exit(1); +} + +/* Check a pointer to see if it is NULL. If it is NULL, exit with an +** out-of-memory error. +*/ +static void shell_check_oom(const void *p){ + if( p==0 ) shell_out_of_memory(); +} + +/* +** Write I/O traces to the following stream. +*/ +#ifdef SQLITE_ENABLE_IOTRACE +static FILE *iotrace = 0; +#endif + +/* +** This routine works like printf in that its first argument is a +** format string and subsequent arguments are values to be substituted +** in place of % fields. The result of formatting this string +** is written to iotrace. +*/ +#ifdef SQLITE_ENABLE_IOTRACE +static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){ + va_list ap; + char *z; + if( iotrace==0 ) return; + va_start(ap, zFormat); + z = sqlite3_vmprintf(zFormat, ap); + va_end(ap); + sqlite3_fprintf(iotrace, "%s", z); + sqlite3_free(z); +} +#endif + +/* Lookup table to estimate the number of columns consumed by a Unicode +** character. +*/ +static const struct { + unsigned char w; /* Width of the character in columns */ + int iFirst; /* First character in a span having this width */ +} aUWidth[] = { + /* {1, 0x00000}, */ + {0, 0x00300}, {1, 0x00370}, {0, 0x00483}, {1, 0x00487}, {0, 0x00488}, + {1, 0x0048a}, {0, 0x00591}, {1, 0x005be}, {0, 0x005bf}, {1, 0x005c0}, + {0, 0x005c1}, {1, 0x005c3}, {0, 0x005c4}, {1, 0x005c6}, {0, 0x005c7}, + {1, 0x005c8}, {0, 0x00600}, {1, 0x00604}, {0, 0x00610}, {1, 0x00616}, + {0, 0x0064b}, {1, 0x0065f}, {0, 0x00670}, {1, 0x00671}, {0, 0x006d6}, + {1, 0x006e5}, {0, 0x006e7}, {1, 0x006e9}, {0, 0x006ea}, {1, 0x006ee}, + {0, 0x0070f}, {1, 0x00710}, {0, 0x00711}, {1, 0x00712}, {0, 0x00730}, + {1, 0x0074b}, {0, 0x007a6}, {1, 0x007b1}, {0, 0x007eb}, {1, 0x007f4}, + {0, 0x00901}, {1, 0x00903}, {0, 0x0093c}, {1, 0x0093d}, {0, 0x00941}, + {1, 0x00949}, {0, 0x0094d}, {1, 0x0094e}, {0, 0x00951}, {1, 0x00955}, + {0, 0x00962}, {1, 0x00964}, {0, 0x00981}, {1, 0x00982}, {0, 0x009bc}, + {1, 0x009bd}, {0, 0x009c1}, {1, 0x009c5}, {0, 0x009cd}, {1, 0x009ce}, + {0, 0x009e2}, {1, 0x009e4}, {0, 0x00a01}, {1, 0x00a03}, {0, 0x00a3c}, + {1, 0x00a3d}, {0, 0x00a41}, {1, 0x00a43}, {0, 0x00a47}, {1, 0x00a49}, + {0, 0x00a4b}, {1, 0x00a4e}, {0, 0x00a70}, {1, 0x00a72}, {0, 0x00a81}, + {1, 0x00a83}, {0, 0x00abc}, {1, 0x00abd}, {0, 0x00ac1}, {1, 0x00ac6}, + {0, 0x00ac7}, {1, 0x00ac9}, {0, 0x00acd}, {1, 0x00ace}, {0, 0x00ae2}, + {1, 0x00ae4}, {0, 0x00b01}, {1, 0x00b02}, {0, 0x00b3c}, {1, 0x00b3d}, + {0, 0x00b3f}, {1, 0x00b40}, {0, 0x00b41}, {1, 0x00b44}, {0, 0x00b4d}, + {1, 0x00b4e}, {0, 0x00b56}, {1, 0x00b57}, {0, 0x00b82}, {1, 0x00b83}, + {0, 0x00bc0}, {1, 0x00bc1}, {0, 0x00bcd}, {1, 0x00bce}, {0, 0x00c3e}, + {1, 0x00c41}, {0, 0x00c46}, {1, 0x00c49}, {0, 0x00c4a}, {1, 0x00c4e}, + {0, 0x00c55}, {1, 0x00c57}, {0, 0x00cbc}, {1, 0x00cbd}, {0, 0x00cbf}, + {1, 0x00cc0}, {0, 0x00cc6}, {1, 0x00cc7}, {0, 0x00ccc}, {1, 0x00cce}, + {0, 0x00ce2}, {1, 0x00ce4}, {0, 0x00d41}, {1, 0x00d44}, {0, 0x00d4d}, + {1, 0x00d4e}, {0, 0x00dca}, {1, 0x00dcb}, {0, 0x00dd2}, {1, 0x00dd5}, + {0, 0x00dd6}, {1, 0x00dd7}, {0, 0x00e31}, {1, 0x00e32}, {0, 0x00e34}, + {1, 0x00e3b}, {0, 0x00e47}, {1, 0x00e4f}, {0, 0x00eb1}, {1, 0x00eb2}, + {0, 0x00eb4}, {1, 0x00eba}, {0, 0x00ebb}, {1, 0x00ebd}, {0, 0x00ec8}, + {1, 0x00ece}, {0, 0x00f18}, {1, 0x00f1a}, {0, 0x00f35}, {1, 0x00f36}, + {0, 0x00f37}, {1, 0x00f38}, {0, 0x00f39}, {1, 0x00f3a}, {0, 0x00f71}, + {1, 0x00f7f}, {0, 0x00f80}, {1, 0x00f85}, {0, 0x00f86}, {1, 0x00f88}, + {0, 0x00f90}, {1, 0x00f98}, {0, 0x00f99}, {1, 0x00fbd}, {0, 0x00fc6}, + {1, 0x00fc7}, {0, 0x0102d}, {1, 0x01031}, {0, 0x01032}, {1, 0x01033}, + {0, 0x01036}, {1, 0x01038}, {0, 0x01039}, {1, 0x0103a}, {0, 0x01058}, + {1, 0x0105a}, {2, 0x01100}, {0, 0x01160}, {1, 0x01200}, {0, 0x0135f}, + {1, 0x01360}, {0, 0x01712}, {1, 0x01715}, {0, 0x01732}, {1, 0x01735}, + {0, 0x01752}, {1, 0x01754}, {0, 0x01772}, {1, 0x01774}, {0, 0x017b4}, + {1, 0x017b6}, {0, 0x017b7}, {1, 0x017be}, {0, 0x017c6}, {1, 0x017c7}, + {0, 0x017c9}, {1, 0x017d4}, {0, 0x017dd}, {1, 0x017de}, {0, 0x0180b}, + {1, 0x0180e}, {0, 0x018a9}, {1, 0x018aa}, {0, 0x01920}, {1, 0x01923}, + {0, 0x01927}, {1, 0x01929}, {0, 0x01932}, {1, 0x01933}, {0, 0x01939}, + {1, 0x0193c}, {0, 0x01a17}, {1, 0x01a19}, {0, 0x01b00}, {1, 0x01b04}, + {0, 0x01b34}, {1, 0x01b35}, {0, 0x01b36}, {1, 0x01b3b}, {0, 0x01b3c}, + {1, 0x01b3d}, {0, 0x01b42}, {1, 0x01b43}, {0, 0x01b6b}, {1, 0x01b74}, + {0, 0x01dc0}, {1, 0x01dcb}, {0, 0x01dfe}, {1, 0x01e00}, {0, 0x0200b}, + {1, 0x02010}, {0, 0x0202a}, {1, 0x0202f}, {0, 0x02060}, {1, 0x02064}, + {0, 0x0206a}, {1, 0x02070}, {0, 0x020d0}, {1, 0x020f0}, {2, 0x02329}, + {1, 0x0232b}, {2, 0x02e80}, {0, 0x0302a}, {2, 0x03030}, {1, 0x0303f}, + {2, 0x03040}, {0, 0x03099}, {2, 0x0309b}, {1, 0x0a4d0}, {0, 0x0a806}, + {1, 0x0a807}, {0, 0x0a80b}, {1, 0x0a80c}, {0, 0x0a825}, {1, 0x0a827}, + {2, 0x0ac00}, {1, 0x0d7a4}, {2, 0x0f900}, {1, 0x0fb00}, {0, 0x0fb1e}, + {1, 0x0fb1f}, {0, 0x0fe00}, {2, 0x0fe10}, {1, 0x0fe1a}, {0, 0x0fe20}, + {1, 0x0fe24}, {2, 0x0fe30}, {1, 0x0fe70}, {0, 0x0feff}, {2, 0x0ff00}, + {1, 0x0ff61}, {2, 0x0ffe0}, {1, 0x0ffe7}, {0, 0x0fff9}, {1, 0x0fffc}, + {0, 0x10a01}, {1, 0x10a04}, {0, 0x10a05}, {1, 0x10a07}, {0, 0x10a0c}, + {1, 0x10a10}, {0, 0x10a38}, {1, 0x10a3b}, {0, 0x10a3f}, {1, 0x10a40}, + {0, 0x1d167}, {1, 0x1d16a}, {0, 0x1d173}, {1, 0x1d183}, {0, 0x1d185}, + {1, 0x1d18c}, {0, 0x1d1aa}, {1, 0x1d1ae}, {0, 0x1d242}, {1, 0x1d245}, + {2, 0x20000}, {1, 0x2fffe}, {2, 0x30000}, {1, 0x3fffe}, {0, 0xe0001}, + {1, 0xe0002}, {0, 0xe0020}, {1, 0xe0080}, {0, 0xe0100}, {1, 0xe01f0} +}; + +/* +** Return an estimate of the width, in columns, for the single Unicode +** character c. For normal characters, the answer is always 1. But the +** estimate might be 0 or 2 for zero-width and double-width characters. +** +** Different display devices display unicode using different widths. So +** it is impossible to know that true display width with 100% accuracy. +** Inaccuracies in the width estimates might cause columns to be misaligned. +** Unfortunately, there is nothing we can do about that. +*/ +int cli_wcwidth(int c){ + int iFirst, iLast; + + /* Fast path for common characters */ + if( c<=0x300 ) return 1; + + /* The general case */ + iFirst = 0; + iLast = sizeof(aUWidth)/sizeof(aUWidth[0]) - 1; + while( iFirst c ){ + iLast = iMid - 1; + }else{ + return aUWidth[iMid].w; + } + } + if( aUWidth[iLast].iFirst > c ) return aUWidth[iFirst].w; + return aUWidth[iLast].w; +} + +/* +** Compute the value and length of a multi-byte UTF-8 character that +** begins at z[0]. Return the length. Write the Unicode value into *pU. +** +** This routine only works for *multi-byte* UTF-8 characters. +*/ +static int decodeUtf8(const unsigned char *z, int *pU){ + if( (z[0] & 0xe0)==0xc0 && (z[1] & 0xc0)==0x80 ){ + *pU = ((z[0] & 0x1f)<<6) | (z[1] & 0x3f); + return 2; + } + if( (z[0] & 0xf0)==0xe0 && (z[1] & 0xc0)==0x80 && (z[2] & 0xc0)==0x80 ){ + *pU = ((z[0] & 0x0f)<<12) | ((z[1] & 0x3f)<<6) | (z[2] & 0x3f); + return 3; + } + if( (z[0] & 0xf8)==0xf0 && (z[1] & 0xc0)==0x80 && (z[2] & 0xc0)==0x80 + && (z[3] & 0xc0)==0x80 + ){ + *pU = ((z[0] & 0x0f)<<18) | ((z[1] & 0x3f)<<12) | ((z[2] & 0x3f))<<6 + | (z[3] & 0x3f); + return 4; + } + *pU = 0; + return 1; +} + + +#if 0 /* NOT USED */ +/* +** Return the width, in display columns, of a UTF-8 string. +** +** Each normal character counts as 1. Zero-width characters count +** as zero, and double-width characters count as 2. +*/ +int cli_wcswidth(const char *z){ + const unsigned char *a = (const unsigned char*)z; + int n = 0; + int i = 0; + unsigned char c; + while( (c = a[i])!=0 ){ + if( c>=0xc0 ){ + int u; + int len = decodeUtf8(&a[i], &u); + i += len; + n += cli_wcwidth(u); + }else if( c>=' ' ){ + n++; + i++; + }else{ + i++; + } + } + return n; +} +#endif + +/* +** Check to see if z[] is a valid VT100 escape. If it is, then +** return the number of bytes in the escape sequence. Return 0 if +** z[] is not a VT100 escape. +** +** This routine assumes that z[0] is \033 (ESC). +*/ +static int isVt100(const unsigned char *z){ + int i; + if( z[1]!='[' ) return 0; + i = 2; + while( z[i]>=0x30 && z[i]<=0x3f ){ i++; } + while( z[i]>=0x20 && z[i]<=0x2f ){ i++; } + if( z[i]<0x40 || z[i]>0x7e ) return 0; + return i+1; +} + +/* +** Output string zUtf to stdout as w characters. If w is negative, +** then right-justify the text. W is the width in UTF-8 characters, not +** in bytes. This is different from the %*.*s specification in printf +** since with %*.*s the width is measured in bytes, not characters. +** +** Take into account zero-width and double-width Unicode characters. +** In other words, a zero-width character does not count toward the +** the w limit. A double-width character counts as two. +** +** w should normally be a small number. A couple hundred at most. This +** routine caps w at 100 million to avoid integer overflow issues. +*/ +static void utf8_width_print(FILE *out, int w, const char *zUtf){ + const unsigned char *a = (const unsigned char*)zUtf; + static const int mxW = 10000000; + unsigned char c; + int i = 0; + int n = 0; + int k; + int aw; + if( w<-mxW ){ + w = -mxW; + }else if( w>mxW ){ + w= mxW; + } + aw = w<0 ? -w : w; + if( zUtf==0 ) zUtf = ""; + while( (c = a[i])!=0 ){ + if( (c&0xc0)==0xc0 ){ + int u; + int len = decodeUtf8(a+i, &u); + int x = cli_wcwidth(u); + if( x+n>aw ){ + break; + } + i += len; + n += x; + }else if( c==0x1b && (k = isVt100(&a[i]))>0 ){ + i += k; + }else if( n>=aw ){ + break; + }else{ + n++; + i++; + } + } + if( n>=aw ){ + sqlite3_fprintf(out, "%.*s", i, zUtf); + }else if( w<0 ){ + sqlite3_fprintf(out, "%*s%s", aw-n, "", zUtf); + }else{ + sqlite3_fprintf(out, "%s%*s", zUtf, aw-n, ""); + } +} + + +/* +** Determines if a string is a number of not. +*/ +static int isNumber(const char *z, int *realnum){ + if( *z=='-' || *z=='+' ) z++; + if( !IsDigit(*z) ){ + return 0; + } + z++; + if( realnum ) *realnum = 0; + while( IsDigit(*z) ){ z++; } + if( *z=='.' ){ + z++; + if( !IsDigit(*z) ) return 0; + while( IsDigit(*z) ){ z++; } + if( realnum ) *realnum = 1; + } + if( *z=='e' || *z=='E' ){ + z++; + if( *z=='+' || *z=='-' ) z++; + if( !IsDigit(*z) ) return 0; + while( IsDigit(*z) ){ z++; } + if( realnum ) *realnum = 1; + } + return *z==0; +} + +/* +** Compute a string length that is limited to what can be stored in +** lower 30 bits of a 32-bit signed integer. +*/ +static int strlen30(const char *z){ + const char *z2 = z; + while( *z2 ){ z2++; } + return 0x3fffffff & (int)(z2 - z); +} + +/* +** Return the length of a string in characters. Multibyte UTF8 characters +** count as a single character for single-width characters, or as two +** characters for double-width characters. +*/ +static int strlenChar(const char *z){ + int n = 0; + while( *z ){ + if( (0x80&z[0])==0 ){ + n++; + z++; + }else{ + int u = 0; + int len = decodeUtf8((const u8*)z, &u); + z += len; + n += cli_wcwidth(u); + } + } + return n; +} + +/* +** Return open FILE * if zFile exists, can be opened for read +** and is an ordinary file or a character stream source. +** Otherwise return 0. +*/ +static FILE * openChrSource(const char *zFile){ +#if defined(_WIN32) || defined(WIN32) + struct __stat64 x = {0}; +# define STAT_CHR_SRC(mode) ((mode & (_S_IFCHR|_S_IFIFO|_S_IFREG))!=0) + /* On Windows, open first, then check the stream nature. This order + ** is necessary because _stat() and sibs, when checking a named pipe, + ** effectively break the pipe as its supplier sees it. */ + FILE *rv = sqlite3_fopen(zFile, "rb"); + if( rv==0 ) return 0; + if( _fstat64(_fileno(rv), &x) != 0 + || !STAT_CHR_SRC(x.st_mode)){ + fclose(rv); + rv = 0; + } + return rv; +#else + struct stat x = {0}; + int rc = stat(zFile, &x); +# define STAT_CHR_SRC(mode) (S_ISREG(mode)||S_ISFIFO(mode)||S_ISCHR(mode)) + if( rc!=0 ) return 0; + if( STAT_CHR_SRC(x.st_mode) ){ + return sqlite3_fopen(zFile, "rb"); + }else{ + return 0; + } +#endif +#undef STAT_CHR_SRC +} + +/* +** This routine reads a line of text from FILE in, stores +** the text in memory obtained from malloc() and returns a pointer +** to the text. NULL is returned at end of file, or if malloc() +** fails, or if the length of the line is longer than about a gigabyte. +** +** If zLine is not NULL then it is a malloced buffer returned from +** a previous call to this routine that may be reused. +*/ +static char *local_getline(char *zLine, FILE *in){ + int nLine = zLine==0 ? 0 : 100; + int n = 0; + + while( 1 ){ + if( n+100>nLine ){ + if( nLine>=1073741773 ){ + free(zLine); + return 0; + } + nLine = nLine*2 + 100; + zLine = realloc(zLine, nLine); + shell_check_oom(zLine); + } + if( sqlite3_fgets(&zLine[n], nLine - n, in)==0 ){ + if( n==0 ){ + free(zLine); + return 0; + } + zLine[n] = 0; + break; + } + while( zLine[n] ) n++; + if( n>0 && zLine[n-1]=='\n' ){ + n--; + if( n>0 && zLine[n-1]=='\r' ) n--; + zLine[n] = 0; + break; + } + } + return zLine; +} + +/* +** Retrieve a single line of input text. +** +** If in==0 then read from standard input and prompt before each line. +** If isContinuation is true, then a continuation prompt is appropriate. +** If isContinuation is zero, then the main prompt should be used. +** +** If zPrior is not NULL then it is a buffer from a prior call to this +** routine that can be reused. +** +** The result is stored in space obtained from malloc() and must either +** be freed by the caller or else passed back into this routine via the +** zPrior argument for reuse. +*/ +#ifndef SQLITE_SHELL_FIDDLE +static char *one_input_line(FILE *in, char *zPrior, int isContinuation){ + char *zPrompt; + char *zResult; + if( in!=0 ){ + zResult = local_getline(zPrior, in); + }else{ + zPrompt = isContinuation ? CONTINUATION_PROMPT : mainPrompt; +#if SHELL_USE_LOCAL_GETLINE + sputz(stdout, zPrompt); + fflush(stdout); + do{ + zResult = local_getline(zPrior, stdin); + zPrior = 0; + /* ^C trap creates a false EOF, so let "interrupt" thread catch up. */ + if( zResult==0 ) sqlite3_sleep(50); + }while( zResult==0 && seenInterrupt>0 ); +#else + free(zPrior); + zResult = shell_readline(zPrompt); + while( zResult==0 ){ + /* ^C trap creates a false EOF, so let "interrupt" thread catch up. */ + sqlite3_sleep(50); + if( seenInterrupt==0 ) break; + zResult = shell_readline(""); + } +/* BEGIN SQLCIPHER */ +#ifdef SQLITE_HAS_CODEC + /* Simplistic filtering of input lines to prevent PRAGKA key and + PRAGMA rekey statements from being stored in readline history. + Note that this will only prevent single line statements, but that + will be sufficient for common cases. */ + if(zResult && *zResult && ( + sqlite3_strlike("%pragma%key%=%", zResult, 0)==0 || + sqlite3_strlike("%attach%database%as%key%", zResult, 0)==0 + ) + ) return zResult; +#endif +/* END SQLCIPHER */ + if( zResult && *zResult ) shell_add_history(zResult); +#endif + } + return zResult; +} +#endif /* !SQLITE_SHELL_FIDDLE */ + +/* +** Return the value of a hexadecimal digit. Return -1 if the input +** is not a hex digit. +*/ +static int hexDigitValue(char c){ + if( c>='0' && c<='9' ) return c - '0'; + if( c>='a' && c<='f' ) return c - 'a' + 10; + if( c>='A' && c<='F' ) return c - 'A' + 10; + return -1; +} + +/* +** Interpret zArg as an integer value, possibly with suffixes. +** +** If the value specified by zArg is outside the range of values that +** can be represented using a 64-bit twos-complement integer, then return +** the nearest representable value. +*/ +static sqlite3_int64 integerValue(const char *zArg){ + sqlite3_uint64 v = 0; + static const struct { char *zSuffix; unsigned int iMult; } aMult[] = { + { "KiB", 1024 }, + { "MiB", 1024*1024 }, + { "GiB", 1024*1024*1024 }, + { "KB", 1000 }, + { "MB", 1000000 }, + { "GB", 1000000000 }, + { "K", 1000 }, + { "M", 1000000 }, + { "G", 1000000000 }, + }; + int i; + int isNeg = 0; + if( zArg[0]=='-' ){ + isNeg = 1; + zArg++; + }else if( zArg[0]=='+' ){ + zArg++; + } + if( zArg[0]=='0' && zArg[1]=='x' ){ + int x; + zArg += 2; + while( (x = hexDigitValue(zArg[0]))>=0 ){ + if( v > 0x0fffffffffffffffULL ) goto integer_overflow; + v = (v<<4) + x; + zArg++; + } + }else{ + while( IsDigit(zArg[0]) ){ + if( v>=922337203685477580LL ){ + if( v>922337203685477580LL || zArg[0]>='8' ) goto integer_overflow; + } + v = v*10 + (zArg[0] - '0'); + zArg++; + } + } + for(i=0; i0x7fffffffffffffffULL ) goto integer_overflow; + return isNeg? -(sqlite3_int64)v : (sqlite3_int64)v; +integer_overflow: + return isNeg ? (i64)0x8000000000000000LL : 0x7fffffffffffffffLL; +} + +/* +** A variable length string to which one can append text. +*/ +typedef struct ShellText ShellText; +struct ShellText { + char *zTxt; /* The text */ + i64 n; /* Number of bytes of zTxt[] actually used */ + i64 nAlloc; /* Number of bytes allocated for zTxt[] */ +}; + +/* +** Initialize and destroy a ShellText object +*/ +static void initText(ShellText *p){ + memset(p, 0, sizeof(*p)); +} +static void freeText(ShellText *p){ + sqlite3_free(p->zTxt); + initText(p); +} + +/* zIn is either a pointer to a NULL-terminated string in memory obtained +** from malloc(), or a NULL pointer. The string pointed to by zAppend is +** added to zIn, and the result returned in memory obtained from malloc(). +** zIn, if it was not NULL, is freed. +** +** If the third argument, quote, is not '\0', then it is used as a +** quote character for zAppend. +*/ +static void appendText(ShellText *p, const char *zAppend, char quote){ + i64 len; + i64 i; + i64 nAppend = strlen30(zAppend); + + len = nAppend+p->n+1; + if( quote ){ + len += 2; + for(i=0; izTxt==0 || p->n+len>=p->nAlloc ){ + p->nAlloc = p->nAlloc*2 + len + 20; + p->zTxt = sqlite3_realloc64(p->zTxt, p->nAlloc); + shell_check_oom(p->zTxt); + } + + if( quote ){ + char *zCsr = p->zTxt+p->n; + *zCsr++ = quote; + for(i=0; in = (i64)(zCsr - p->zTxt); + *zCsr = '\0'; + }else{ + memcpy(p->zTxt+p->n, zAppend, nAppend); + p->n += nAppend; + p->zTxt[p->n] = '\0'; + } +} + +/* +** Attempt to determine if identifier zName needs to be quoted, either +** because it contains non-alphanumeric characters, or because it is an +** SQLite keyword. Be conservative in this estimate: When in doubt assume +** that quoting is required. +** +** Return '"' if quoting is required. Return 0 if no quoting is required. +*/ +static char quoteChar(const char *zName){ + int i; + if( zName==0 ) return '"'; + if( !IsAlpha(zName[0]) && zName[0]!='_' ) return '"'; + for(i=0; zName[i]; i++){ + if( !IsAlnum(zName[i]) && zName[i]!='_' ) return '"'; + } + return sqlite3_keyword_check(zName, i) ? '"' : 0; +} + +/* +** Construct a fake object name and column list to describe the structure +** of the view, virtual table, or table valued function zSchema.zName. +** +** The returned string comes from sqlite3_mprintf() and should be freed +** by the caller using sqlite3_free(). +*/ +static char *shellFakeSchema( + sqlite3 *db, /* The database connection containing the vtab */ + const char *zSchema, /* Schema of the database holding the vtab */ + const char *zName /* The name of the virtual table */ +){ + sqlite3_stmt *pStmt = 0; + char *zSql; + ShellText s; + char cQuote; + char *zDiv = "("; + int nRow = 0; + + zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;", + zSchema ? zSchema : "main", zName); + shell_check_oom(zSql); + sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); + sqlite3_free(zSql); + initText(&s); + if( zSchema ){ + cQuote = quoteChar(zSchema); + if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0; + appendText(&s, zSchema, cQuote); + appendText(&s, ".", 0); + } + cQuote = quoteChar(zName); + appendText(&s, zName, cQuote); + while( sqlite3_step(pStmt)==SQLITE_ROW ){ + const char *zCol = (const char*)sqlite3_column_text(pStmt, 1); + nRow++; + appendText(&s, zDiv, 0); + zDiv = ","; + if( zCol==0 ) zCol = ""; + cQuote = quoteChar(zCol); + appendText(&s, zCol, cQuote); + } + appendText(&s, ")", 0); + sqlite3_finalize(pStmt); + if( nRow==0 ){ + freeText(&s); + s.zTxt = 0; + } + return s.zTxt; +} + +/* +** SQL function: strtod(X) +** +** Use the C-library strtod() function to convert string X into a double. +** Used for comparing the accuracy of SQLite's internal text-to-float conversion +** routines against the C-library. +*/ +static void shellStrtod( + sqlite3_context *pCtx, + int nVal, + sqlite3_value **apVal +){ + char *z = (char*)sqlite3_value_text(apVal[0]); + UNUSED_PARAMETER(nVal); + if( z==0 ) return; + sqlite3_result_double(pCtx, strtod(z,0)); +} + +/* +** SQL function: dtostr(X) +** +** Use the C-library printf() function to convert real value X into a string. +** Used for comparing the accuracy of SQLite's internal float-to-text conversion +** routines against the C-library. +*/ +static void shellDtostr( + sqlite3_context *pCtx, + int nVal, + sqlite3_value **apVal +){ + double r = sqlite3_value_double(apVal[0]); + int n = nVal>=2 ? sqlite3_value_int(apVal[1]) : 26; + char z[400]; + if( n<1 ) n = 1; + if( n>350 ) n = 350; + sqlite3_snprintf(sizeof(z), z, "%#+.*e", n, r); + sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT); +} + +/* +** SQL function: shell_add_schema(S,X) +** +** Add the schema name X to the CREATE statement in S and return the result. +** Examples: +** +** CREATE TABLE t1(x) -> CREATE TABLE xyz.t1(x); +** +** Also works on +** +** CREATE INDEX +** CREATE UNIQUE INDEX +** CREATE VIEW +** CREATE TRIGGER +** CREATE VIRTUAL TABLE +** +** This UDF is used by the .schema command to insert the schema name of +** attached databases into the middle of the sqlite_schema.sql field. +*/ +static void shellAddSchemaName( + sqlite3_context *pCtx, + int nVal, + sqlite3_value **apVal +){ + static const char *aPrefix[] = { + "TABLE", + "INDEX", + "UNIQUE INDEX", + "VIEW", + "TRIGGER", + "VIRTUAL TABLE" + }; + int i = 0; + const char *zIn = (const char*)sqlite3_value_text(apVal[0]); + const char *zSchema = (const char*)sqlite3_value_text(apVal[1]); + const char *zName = (const char*)sqlite3_value_text(apVal[2]); + sqlite3 *db = sqlite3_context_db_handle(pCtx); + UNUSED_PARAMETER(nVal); + if( zIn!=0 && cli_strncmp(zIn, "CREATE ", 7)==0 ){ + for(i=0; ishellFlgs & (X))!=0) +#define ShellSetFlag(P,X) ((P)->shellFlgs|=(X)) +#define ShellClearFlag(P,X) ((P)->shellFlgs&=(~(X))) + +/* +** These are the allowed modes. +*/ +#define MODE_Line 0 /* One column per line. Blank line between records */ +#define MODE_Column 1 /* One record per line in neat columns */ +#define MODE_List 2 /* One record per line with a separator */ +#define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */ +#define MODE_Html 4 /* Generate an XHTML table */ +#define MODE_Insert 5 /* Generate SQL "insert" statements */ +#define MODE_Quote 6 /* Quote values as for SQL */ +#define MODE_Tcl 7 /* Generate ANSI-C or TCL quoted elements */ +#define MODE_Csv 8 /* Quote strings, numbers are plain */ +#define MODE_Explain 9 /* Like MODE_Column, but do not truncate data */ +#define MODE_Ascii 10 /* Use ASCII unit and record separators (0x1F/0x1E) */ +#define MODE_Pretty 11 /* Pretty-print schemas */ +#define MODE_EQP 12 /* Converts EXPLAIN QUERY PLAN output into a graph */ +#define MODE_Json 13 /* Output JSON */ +#define MODE_Markdown 14 /* Markdown formatting */ +#define MODE_Table 15 /* MySQL-style table formatting */ +#define MODE_Box 16 /* Unicode box-drawing characters */ +#define MODE_Count 17 /* Output only a count of the rows of output */ +#define MODE_Off 18 /* No query output shown */ +#define MODE_ScanExp 19 /* Like MODE_Explain, but for ".scanstats vm" */ +#define MODE_Www 20 /* Full web-page output */ + +static const char *modeDescr[] = { + "line", + "column", + "list", + "semi", + "html", + "insert", + "quote", + "tcl", + "csv", + "explain", + "ascii", + "prettyprint", + "eqp", + "json", + "markdown", + "table", + "box", + "count", + "off", + "scanexp", + "www", +}; + +/* +** These are the column/row/line separators used by the various +** import/export modes. +*/ +#define SEP_Column "|" +#define SEP_Row "\n" +#define SEP_Tab "\t" +#define SEP_Space " " +#define SEP_Comma "," +#define SEP_CrLf "\r\n" +#define SEP_Unit "\x1F" +#define SEP_Record "\x1E" + +/* +** Limit input nesting via .read or any other input redirect. +** It's not too expensive, so a generous allowance can be made. +*/ +#define MAX_INPUT_NESTING 25 + +/* +** A callback for the sqlite3_log() interface. +*/ +static void shellLog(void *pArg, int iErrCode, const char *zMsg){ + ShellState *p = (ShellState*)pArg; + if( p->pLog==0 ) return; + sqlite3_fprintf(p->pLog, "(%d) %s\n", iErrCode, zMsg); + fflush(p->pLog); +} + +/* +** SQL function: shell_putsnl(X) +** +** Write the text X to the screen (or whatever output is being directed) +** adding a newline at the end, and then return X. +*/ +static void shellPutsFunc( + sqlite3_context *pCtx, + int nVal, + sqlite3_value **apVal +){ + ShellState *p = (ShellState*)sqlite3_user_data(pCtx); + (void)nVal; + sqlite3_fprintf(p->out, "%s\n", sqlite3_value_text(apVal[0])); + sqlite3_result_value(pCtx, apVal[0]); +} + +/* +** If in safe mode, print an error message described by the arguments +** and exit immediately. +*/ +static void failIfSafeMode( + ShellState *p, + const char *zErrMsg, + ... +){ + if( p->bSafeMode ){ + va_list ap; + char *zMsg; + va_start(ap, zErrMsg); + zMsg = sqlite3_vmprintf(zErrMsg, ap); + va_end(ap); + sqlite3_fprintf(stderr, "line %lld: %s\n", p->lineno, zMsg); + exit(1); + } +} + +/* +** SQL function: edit(VALUE) +** edit(VALUE,EDITOR) +** +** These steps: +** +** (1) Write VALUE into a temporary file. +** (2) Run program EDITOR on that temporary file. +** (3) Read the temporary file back and return its content as the result. +** (4) Delete the temporary file +** +** If the EDITOR argument is omitted, use the value in the VISUAL +** environment variable. If still there is no EDITOR, through an error. +** +** Also throw an error if the EDITOR program returns a non-zero exit code. +*/ +#ifndef SQLITE_NOHAVE_SYSTEM +static void editFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + const char *zEditor; + char *zTempFile = 0; + sqlite3 *db; + char *zCmd = 0; + int bBin; + int rc; + int hasCRLF = 0; + FILE *f = 0; + sqlite3_int64 sz; + sqlite3_int64 x; + unsigned char *p = 0; + + if( argc==2 ){ + zEditor = (const char*)sqlite3_value_text(argv[1]); + }else{ + zEditor = getenv("VISUAL"); + } + if( zEditor==0 ){ + sqlite3_result_error(context, "no editor for edit()", -1); + return; + } + if( sqlite3_value_type(argv[0])==SQLITE_NULL ){ + sqlite3_result_error(context, "NULL input to edit()", -1); + return; + } + db = sqlite3_context_db_handle(context); + zTempFile = 0; + sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile); + if( zTempFile==0 ){ + sqlite3_uint64 r = 0; + sqlite3_randomness(sizeof(r), &r); + zTempFile = sqlite3_mprintf("temp%llx", r); + if( zTempFile==0 ){ + sqlite3_result_error_nomem(context); + return; + } + } + bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB; + /* When writing the file to be edited, do \n to \r\n conversions on systems + ** that want \r\n line endings */ + f = sqlite3_fopen(zTempFile, bBin ? "wb" : "w"); + if( f==0 ){ + sqlite3_result_error(context, "edit() cannot open temp file", -1); + goto edit_func_end; + } + sz = sqlite3_value_bytes(argv[0]); + if( bBin ){ + x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f); + }else{ + const char *z = (const char*)sqlite3_value_text(argv[0]); + /* Remember whether or not the value originally contained \r\n */ + if( z && strstr(z,"\r\n")!=0 ) hasCRLF = 1; + x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f); + } + fclose(f); + f = 0; + if( x!=sz ){ + sqlite3_result_error(context, "edit() could not write the whole file", -1); + goto edit_func_end; + } + zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile); + if( zCmd==0 ){ + sqlite3_result_error_nomem(context); + goto edit_func_end; + } + rc = system(zCmd); + sqlite3_free(zCmd); + if( rc ){ + sqlite3_result_error(context, "EDITOR returned non-zero", -1); + goto edit_func_end; + } + f = sqlite3_fopen(zTempFile, "rb"); + if( f==0 ){ + sqlite3_result_error(context, + "edit() cannot reopen temp file after edit", -1); + goto edit_func_end; + } + fseek(f, 0, SEEK_END); + sz = ftell(f); + rewind(f); + p = sqlite3_malloc64( sz+1 ); + if( p==0 ){ + sqlite3_result_error_nomem(context); + goto edit_func_end; + } + x = fread(p, 1, (size_t)sz, f); + fclose(f); + f = 0; + if( x!=sz ){ + sqlite3_result_error(context, "could not read back the whole file", -1); + goto edit_func_end; + } + if( bBin ){ + sqlite3_result_blob64(context, p, sz, sqlite3_free); + }else{ + sqlite3_int64 i, j; + if( hasCRLF ){ + /* If the original contains \r\n then do no conversions back to \n */ + }else{ + /* If the file did not originally contain \r\n then convert any new + ** \r\n back into \n */ + p[sz] = 0; + for(i=j=0; imodePrior = p->mode; + p->priorShFlgs = p->shellFlgs; + memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator)); + memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator)); +} +static void outputModePop(ShellState *p){ + p->mode = p->modePrior; + p->shellFlgs = p->priorShFlgs; + memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator)); + memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator)); +} + +/* +** Set output mode to text or binary for Windows. +*/ +static void setCrlfMode(ShellState *p){ +#ifdef _WIN32 + if( p->crlfMode ){ + sqlite3_fsetmode(p->out, _O_TEXT); + }else{ + sqlite3_fsetmode(p->out, _O_BINARY); + } +#else + UNUSED_PARAMETER(p); +#endif +} + +/* +** Output the given string as a hex-encoded blob (eg. X'1234' ) +*/ +static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){ + int i; + unsigned char *aBlob = (unsigned char*)pBlob; + + char *zStr = sqlite3_malloc64((i64)nBlob*2 + 1); + shell_check_oom(zStr); + + for(i=0; i> 4) ]; + zStr[i*2+1] = aHex[ (aBlob[i] & 0x0F) ]; + } + zStr[i*2] = '\0'; + + sqlite3_fprintf(out, "X'%s'", zStr); + sqlite3_free(zStr); +} + +/* +** Output the given string as a quoted string using SQL quoting conventions: +** +** (1) Single quotes (') within the string are doubled +** (2) The while string is enclosed in '...' +** (3) Control characters other than \n, \t, and \r\n are escaped +** using \u00XX notation and if such substitutions occur, +** the whole string is enclosed in unistr('...') instead of '...'. +** +** Step (3) is omitted if the control-character escape mode is OFF. +** +** See also: output_quoted_escaped_string() which does the same except +** that it does not make exceptions for \n, \t, and \r\n in step (3). +*/ +static void output_quoted_string(ShellState *p, const char *zInX){ + int i; + int needUnistr = 0; + int needDblQuote = 0; + const unsigned char *z = (const unsigned char*)zInX; + unsigned char c; + FILE *out = p->out; + sqlite3_fsetmode(out, _O_BINARY); + if( z==0 ) return; + for(i=0; (c = z[i])!=0; i++){ + if( c=='\'' ){ needDblQuote = 1; } + if( c>0x1f ) continue; + if( c=='\t' || c=='\n' ) continue; + if( c=='\r' && z[i+1]=='\n' ) continue; + needUnistr = 1; + break; + } + if( (needDblQuote==0 && needUnistr==0) + || (needDblQuote==0 && p->eEscMode==SHELL_ESC_OFF) + ){ + sqlite3_fprintf(out, "'%s'",z); + }else if( p->eEscMode==SHELL_ESC_OFF ){ + char *zEncoded = sqlite3_mprintf("%Q", z); + sqlite3_fputs(zEncoded, out); + sqlite3_free(zEncoded); + }else{ + if( needUnistr ){ + sqlite3_fputs("unistr('", out); + }else{ + sqlite3_fputs("'", out); + } + while( *z ){ + for(i=0; (c = z[i])!=0; i++){ + if( c=='\'' ) break; + if( c>0x1f ) continue; + if( c=='\t' || c=='\n' ) continue; + if( c=='\r' && z[i+1]=='\n' ) continue; + break; + } + if( i ){ + sqlite3_fprintf(out, "%.*s", i, z); + z += i; + } + if( c==0 ) break; + if( c=='\'' ){ + sqlite3_fputs("''", out); + }else{ + sqlite3_fprintf(out, "\\u%04x", c); + } + z++; + } + if( needUnistr ){ + sqlite3_fputs("')", out); + }else{ + sqlite3_fputs("'", out); + } + } + setCrlfMode(p); +} + +/* +** Output the given string as a quoted string using SQL quoting conventions. +** Additionallly , escape the "\n" and "\r" characters so that they do not +** get corrupted by end-of-line translation facilities in some operating +** systems. +** +** This is like output_quoted_string() but with the addition of the \r\n +** escape mechanism. +*/ +static void output_quoted_escaped_string(ShellState *p, const char *z){ + char *zEscaped; + sqlite3_fsetmode(p->out, _O_BINARY); + if( p->eEscMode==SHELL_ESC_OFF ){ + zEscaped = sqlite3_mprintf("%Q", z); + }else{ + zEscaped = sqlite3_mprintf("%#Q", z); + } + sqlite3_fputs(zEscaped, p->out); + sqlite3_free(zEscaped); + setCrlfMode(p); +} + +/* +** Find earliest of chars within s specified in zAny. +** With ns == ~0, is like strpbrk(s,zAny) and s must be 0-terminated. +*/ +static const char *anyOfInStr(const char *s, const char *zAny, size_t ns){ + const char *pcFirst = 0; + if( ns == ~(size_t)0 ) ns = strlen(s); + while(*zAny){ + const char *pc = (const char*)memchr(s, *zAny&0xff, ns); + if( pc ){ + pcFirst = pc; + ns = pcFirst - s; + } + ++zAny; + } + return pcFirst; +} + +/* Skip over as much z[] input char sequence as is valid UTF-8, +** limited per nAccept char's or whole characters and containing +** no char cn such that ((1<=0 => char count, nAccept<0 => character + */ +const char *zSkipValidUtf8(const char *z, int nAccept, long ccm){ + int ng = (nAccept<0)? -nAccept : 0; + const char *pcLimit = (nAccept>=0)? z+nAccept : 0; + assert(z!=0); + while( (pcLimit)? (z= pcLimit ) return z; + else{ + char ct = *zt++; + if( ct==0 || (zt-z)>4 || (ct & 0xC0)!=0x80 ){ + /* Trailing bytes are too few, too many, or invalid. */ + return z; + } + } + } while( ((c <<= 1) & 0x40) == 0x40 ); /* Eat lead byte's count. */ + z = zt; + } + } + return z; +} + + +/* +** Output the given string as a quoted according to C or TCL quoting rules. +*/ +static void output_c_string(FILE *out, const char *z){ + char c; + static const char *zq = "\""; + static long ctrlMask = ~0L; + static const char *zDQBSRO = "\"\\\x7f"; /* double-quote, backslash, rubout */ + char ace[3] = "\\?"; + char cbsSay; + sqlite3_fputs(zq, out); + while( *z!=0 ){ + const char *pcDQBSRO = anyOfInStr(z, zDQBSRO, ~(size_t)0); + const char *pcPast = zSkipValidUtf8(z, INT_MAX, ctrlMask); + const char *pcEnd = (pcDQBSRO && pcDQBSRO < pcPast)? pcDQBSRO : pcPast; + if( pcEnd > z ){ + sqlite3_fprintf(out, "%.*s", (int)(pcEnd-z), z); + } + if( (c = *pcEnd)==0 ) break; + ++pcEnd; + switch( c ){ + case '\\': case '"': + cbsSay = (char)c; + break; + case '\t': cbsSay = 't'; break; + case '\n': cbsSay = 'n'; break; + case '\r': cbsSay = 'r'; break; + case '\f': cbsSay = 'f'; break; + default: cbsSay = 0; break; + } + if( cbsSay ){ + ace[1] = cbsSay; + sqlite3_fputs(ace, out); + }else if( !isprint(c&0xff) ){ + sqlite3_fprintf(out, "\\%03o", c&0xff); + }else{ + ace[1] = (char)c; + sqlite3_fputs(ace+1, out); + } + z = pcEnd; + } + sqlite3_fputs(zq, out); +} + +/* +** Output the given string as quoted according to JSON quoting rules. +*/ +static void output_json_string(FILE *out, const char *z, i64 n){ + unsigned char c; + static const char *zq = "\""; + static long ctrlMask = ~0L; + static const char *zDQBS = "\"\\"; + const char *pcLimit; + char ace[3] = "\\?"; + char cbsSay; + + if( z==0 ) z = ""; + pcLimit = z + ((n<0)? strlen(z) : (size_t)n); + sqlite3_fputs(zq, out); + while( z < pcLimit ){ + const char *pcDQBS = anyOfInStr(z, zDQBS, pcLimit-z); + const char *pcPast = zSkipValidUtf8(z, (int)(pcLimit-z), ctrlMask); + const char *pcEnd = (pcDQBS && pcDQBS < pcPast)? pcDQBS : pcPast; + if( pcEnd > z ){ + sqlite3_fprintf(out, "%.*s", (int)(pcEnd-z), z); + z = pcEnd; + } + if( z >= pcLimit ) break; + c = (unsigned char)*(z++); + switch( c ){ + case '"': case '\\': + cbsSay = (char)c; + break; + case '\b': cbsSay = 'b'; break; + case '\f': cbsSay = 'f'; break; + case '\n': cbsSay = 'n'; break; + case '\r': cbsSay = 'r'; break; + case '\t': cbsSay = 't'; break; + default: cbsSay = 0; break; + } + if( cbsSay ){ + ace[1] = cbsSay; + sqlite3_fputs(ace, out); + }else if( c<=0x1f || c>=0x7f ){ + sqlite3_fprintf(out, "\\u%04x", c); + }else{ + ace[1] = (char)c; + sqlite3_fputs(ace+1, out); + } + } + sqlite3_fputs(zq, out); +} + +/* +** Escape the input string if it is needed and in accordance with +** eEscMode. +** +** Escaping is needed if the string contains any control characters +** other than \t, \n, and \r\n +** +** If no escaping is needed (the common case) then set *ppFree to NULL +** and return the original string. If escaping is needed, write the +** escaped string into memory obtained from sqlite3_malloc64() or the +** equivalent, and return the new string and set *ppFree to the new string +** as well. +** +** The caller is responsible for freeing *ppFree if it is non-NULL in order +** to reclaim memory. +*/ +static const char *escapeOutput( + ShellState *p, + const char *zInX, + char **ppFree +){ + i64 i, j; + i64 nCtrl = 0; + unsigned char *zIn; + unsigned char c; + unsigned char *zOut; + + + /* No escaping if disabled */ + if( p->eEscMode==SHELL_ESC_OFF ){ + *ppFree = 0; + return zInX; + } + + /* Count the number of control characters in the string. */ + zIn = (unsigned char*)zInX; + for(i=0; (c = zIn[i])!=0; i++){ + if( c<=0x1f + && c!='\t' + && c!='\n' + && (c!='\r' || zIn[i+1]!='\n') + ){ + nCtrl++; + } + } + if( nCtrl==0 ){ + *ppFree = 0; + return zInX; + } + if( p->eEscMode==SHELL_ESC_SYMBOL ) nCtrl *= 2; + zOut = sqlite3_malloc64( i + nCtrl + 1 ); + shell_check_oom(zOut); + for(i=j=0; (c = zIn[i])!=0; i++){ + if( c>0x1f + || c=='\t' + || c=='\n' + || (c=='\r' && zIn[i+1]=='\n') + ){ + continue; + } + if( i>0 ){ + memcpy(&zOut[j], zIn, i); + j += i; + } + zIn += i+1; + i = -1; + switch( p->eEscMode ){ + case SHELL_ESC_SYMBOL: + zOut[j++] = 0xe2; + zOut[j++] = 0x90; + zOut[j++] = 0x80+c; + break; + case SHELL_ESC_ASCII: + zOut[j++] = '^'; + zOut[j++] = 0x40+c; + break; + } + } + if( i>0 ){ + memcpy(&zOut[j], zIn, i); + j += i; + } + zOut[j] = 0; + *ppFree = (char*)zOut; + return (char*)zOut; +} + +/* +** Output the given string with characters that are special to +** HTML escaped. +*/ +static void output_html_string(FILE *out, const char *z){ + int i; + if( z==0 ) z = ""; + while( *z ){ + for(i=0; z[i] + && z[i]!='<' + && z[i]!='&' + && z[i]!='>' + && z[i]!='\"' + && z[i]!='\''; + i++){} + if( i>0 ){ + sqlite3_fprintf(out, "%.*s",i,z); + } + if( z[i]=='<' ){ + sqlite3_fputs("<", out); + }else if( z[i]=='&' ){ + sqlite3_fputs("&", out); + }else if( z[i]=='>' ){ + sqlite3_fputs(">", out); + }else if( z[i]=='\"' ){ + sqlite3_fputs(""", out); + }else if( z[i]=='\'' ){ + sqlite3_fputs("'", out); + }else{ + break; + } + z += i + 1; + } +} + +/* +** If a field contains any character identified by a 1 in the following +** array, then the string must be quoted for CSV. +*/ +static const char needCsvQuote[] = { + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, +}; + +/* +** Output a single term of CSV. Actually, p->colSeparator is used for +** the separator, which may or may not be a comma. p->nullValue is +** the null value. Strings are quoted if necessary. The separator +** is only issued if bSep is true. +*/ +static void output_csv(ShellState *p, const char *z, int bSep){ + if( z==0 ){ + sqlite3_fprintf(p->out, "%s",p->nullValue); + }else{ + unsigned i; + for(i=0; z[i]; i++){ + if( needCsvQuote[((unsigned char*)z)[i]] ){ + i = 0; + break; + } + } + if( i==0 || strstr(z, p->colSeparator)!=0 ){ + char *zQuoted = sqlite3_mprintf("\"%w\"", z); + shell_check_oom(zQuoted); + sqlite3_fputs(zQuoted, p->out); + sqlite3_free(zQuoted); + }else{ + sqlite3_fputs(z, p->out); + } + } + if( bSep ){ + sqlite3_fputs(p->colSeparator, p->out); + } +} + +/* +** This routine runs when the user presses Ctrl-C +*/ +static void interrupt_handler(int NotUsed){ + UNUSED_PARAMETER(NotUsed); + if( ++seenInterrupt>1 ) exit(1); + if( globalDb ) sqlite3_interrupt(globalDb); +} + +#if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE) +/* +** This routine runs for console events (e.g. Ctrl-C) on Win32 +*/ +static BOOL WINAPI ConsoleCtrlHandler( + DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */ +){ + if( dwCtrlType==CTRL_C_EVENT ){ + interrupt_handler(0); + return TRUE; + } + return FALSE; +} +#endif + +#ifndef SQLITE_OMIT_AUTHORIZATION +/* +** This authorizer runs in safe mode. +*/ +static int safeModeAuth( + void *pClientData, + int op, + const char *zA1, + const char *zA2, + const char *zA3, + const char *zA4 +){ + ShellState *p = (ShellState*)pClientData; + static const char *azProhibitedFunctions[] = { + "edit", + "fts3_tokenizer", + "load_extension", + "readfile", + "writefile", + "zipfile", + "zipfile_cds", + }; + UNUSED_PARAMETER(zA1); + UNUSED_PARAMETER(zA3); + UNUSED_PARAMETER(zA4); + switch( op ){ + case SQLITE_ATTACH: { +#ifndef SQLITE_SHELL_FIDDLE + /* In WASM builds the filesystem is a virtual sandbox, so + ** there's no harm in using ATTACH. */ + failIfSafeMode(p, "cannot run ATTACH in safe mode"); +#endif + break; + } + case SQLITE_FUNCTION: { + int i; + for(i=0; iout, "authorizer: %s", azAction[op]); + for(i=0; i<4; i++){ + sqlite3_fputs(" ", p->out); + if( az[i] ){ + output_c_string(p->out, az[i]); + }else{ + sqlite3_fputs("NULL", p->out); + } + } + sqlite3_fputs("\n", p->out); + if( p->bSafeMode ) (void)safeModeAuth(pClientData, op, zA1, zA2, zA3, zA4); + return SQLITE_OK; +} +#endif + +/* +** Print a schema statement. Part of MODE_Semi and MODE_Pretty output. +** +** This routine converts some CREATE TABLE statements for shadow tables +** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements. +** +** If the schema statement in z[] contains a start-of-comment and if +** sqlite3_complete() returns false, try to terminate the comment before +** printing the result. https://sqlite.org/forum/forumpost/d7be961c5c +*/ +static void printSchemaLine(FILE *out, const char *z, const char *zTail){ + char *zToFree = 0; + if( z==0 ) return; + if( zTail==0 ) return; + if( zTail[0]==';' && (strstr(z, "/*")!=0 || strstr(z,"--")!=0) ){ + const char *zOrig = z; + static const char *azTerm[] = { "", "*/", "\n" }; + int i; + for(i=0; iautoEQPtest ){ + sqlite3_fprintf(p->out, "%d,%d,%s\n", iEqpId, p2, zText); + } + pNew = sqlite3_malloc64( sizeof(*pNew) + nText ); + shell_check_oom(pNew); + pNew->iEqpId = iEqpId; + pNew->iParentId = p2; + memcpy(pNew->zText, zText, nText+1); + pNew->pNext = 0; + if( p->sGraph.pLast ){ + p->sGraph.pLast->pNext = pNew; + }else{ + p->sGraph.pRow = pNew; + } + p->sGraph.pLast = pNew; +} + +/* +** Free and reset the EXPLAIN QUERY PLAN data that has been collected +** in p->sGraph. +*/ +static void eqp_reset(ShellState *p){ + EQPGraphRow *pRow, *pNext; + for(pRow = p->sGraph.pRow; pRow; pRow = pNext){ + pNext = pRow->pNext; + sqlite3_free(pRow); + } + memset(&p->sGraph, 0, sizeof(p->sGraph)); +} + +/* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after +** pOld, or return the first such line if pOld is NULL +*/ +static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){ + EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow; + while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext; + return pRow; +} + +/* Render a single level of the graph that has iEqpId as its parent. Called +** recursively to render sublevels. +*/ +static void eqp_render_level(ShellState *p, int iEqpId){ + EQPGraphRow *pRow, *pNext; + i64 n = strlen(p->sGraph.zPrefix); + char *z; + for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){ + pNext = eqp_next_row(p, iEqpId, pRow); + z = pRow->zText; + sqlite3_fprintf(p->out, "%s%s%s\n", p->sGraph.zPrefix, + pNext ? "|--" : "`--", z); + if( n<(i64)sizeof(p->sGraph.zPrefix)-7 ){ + memcpy(&p->sGraph.zPrefix[n], pNext ? "| " : " ", 4); + eqp_render_level(p, pRow->iEqpId); + p->sGraph.zPrefix[n] = 0; + } + } +} + +/* +** Display and reset the EXPLAIN QUERY PLAN data +*/ +static void eqp_render(ShellState *p, i64 nCycle){ + EQPGraphRow *pRow = p->sGraph.pRow; + if( pRow ){ + if( pRow->zText[0]=='-' ){ + if( pRow->pNext==0 ){ + eqp_reset(p); + return; + } + sqlite3_fprintf(p->out, "%s\n", pRow->zText+3); + p->sGraph.pRow = pRow->pNext; + sqlite3_free(pRow); + }else if( nCycle>0 ){ + sqlite3_fprintf(p->out, "QUERY PLAN (cycles=%lld [100%%])\n", nCycle); + }else{ + sqlite3_fputs("QUERY PLAN\n", p->out); + } + p->sGraph.zPrefix[0] = 0; + eqp_render_level(p, 0); + eqp_reset(p); + } +} + +#ifndef SQLITE_OMIT_PROGRESS_CALLBACK +/* +** Progress handler callback. +*/ +static int progress_handler(void *pClientData) { + ShellState *p = (ShellState*)pClientData; + p->nProgress++; + if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){ + sqlite3_fprintf(p->out, "Progress limit reached (%u)\n", p->nProgress); + if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0; + if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0; + return 1; + } + if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){ + sqlite3_fprintf(p->out, "Progress %u\n", p->nProgress); + } + return 0; +} +#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */ + +/* +** Print N dashes +*/ +static void print_dashes(FILE *out, int N){ + const char zDash[] = "--------------------------------------------------"; + const int nDash = sizeof(zDash) - 1; + while( N>nDash ){ + sqlite3_fputs(zDash, out); + N -= nDash; + } + sqlite3_fprintf(out, "%.*s", N, zDash); +} + +/* +** Print a markdown or table-style row separator using ascii-art +*/ +static void print_row_separator( + ShellState *p, + int nArg, + const char *zSep +){ + int i; + if( nArg>0 ){ + sqlite3_fputs(zSep, p->out); + print_dashes(p->out, p->actualWidth[0]+2); + for(i=1; iout); + print_dashes(p->out, p->actualWidth[i]+2); + } + sqlite3_fputs(zSep, p->out); + } + sqlite3_fputs("\n", p->out); +} + +/* +** This is the callback routine that the shell +** invokes for each row of a query result. +*/ +static int shell_callback( + void *pArg, + int nArg, /* Number of result columns */ + char **azArg, /* Text of each result column */ + char **azCol, /* Column names */ + int *aiType /* Column types. Might be NULL */ +){ + int i; + ShellState *p = (ShellState*)pArg; + + if( azArg==0 ) return 0; + switch( p->cMode ){ + case MODE_Count: + case MODE_Off: { + break; + } + case MODE_Line: { + int w = 5; + if( azArg==0 ) break; + for(i=0; iw ) w = len; + } + if( p->cnt++>0 ) sqlite3_fputs(p->rowSeparator, p->out); + for(i=0; inullValue, &pFree); + sqlite3_fprintf(p->out, "%*s = %s%s", w, azCol[i], + pDisplay, p->rowSeparator); + if( pFree ) sqlite3_free(pFree); + } + break; + } + case MODE_ScanExp: + case MODE_Explain: { + static const int aExplainWidth[] = {4, 13, 4, 4, 4, 13, 2, 13}; + static const int aExplainMap[] = {0, 1, 2, 3, 4, 5, 6, 7 }; + static const int aScanExpWidth[] = {4, 15, 6, 13, 4, 4, 4, 13, 2, 13}; + static const int aScanExpMap[] = {0, 9, 8, 1, 2, 3, 4, 5, 6, 7 }; + + const int *aWidth = aExplainWidth; + const int *aMap = aExplainMap; + int nWidth = ArraySize(aExplainWidth); + int iIndent = 1; + + if( p->cMode==MODE_ScanExp ){ + aWidth = aScanExpWidth; + aMap = aScanExpMap; + nWidth = ArraySize(aScanExpWidth); + iIndent = 3; + } + if( nArg>nWidth ) nArg = nWidth; + + /* If this is the first row seen, print out the headers */ + if( p->cnt++==0 ){ + for(i=0; iout, aWidth[i], azCol[ aMap[i] ]); + sqlite3_fputs(i==nArg-1 ? "\n" : " ", p->out); + } + for(i=0; iout, aWidth[i]); + sqlite3_fputs(i==nArg-1 ? "\n" : " ", p->out); + } + } + + /* If there is no data, exit early. */ + if( azArg==0 ) break; + + for(i=0; iw ){ + w = strlenChar(zVal); + zSep = " "; + } + if( i==iIndent && p->aiIndent && p->pStmt ){ + if( p->iIndentnIndent ){ + sqlite3_fprintf(p->out, "%*.s", p->aiIndent[p->iIndent], ""); + } + p->iIndent++; + } + utf8_width_print(p->out, w, zVal ? zVal : p->nullValue); + sqlite3_fputs(i==nArg-1 ? "\n" : zSep, p->out); + } + break; + } + case MODE_Semi: { /* .schema and .fullschema output */ + printSchemaLine(p->out, azArg[0], ";\n"); + break; + } + case MODE_Pretty: { /* .schema and .fullschema with --indent */ + char *z; + int j; + int nParen = 0; + char cEnd = 0; + char c; + int nLine = 0; + int isIndex; + int isWhere = 0; + assert( nArg==1 ); + if( azArg[0]==0 ) break; + if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0 + || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0 + ){ + sqlite3_fprintf(p->out, "%s;\n", azArg[0]); + break; + } + isIndex = sqlite3_strlike("CREATE INDEX%", azArg[0], 0)==0 + || sqlite3_strlike("CREATE UNIQUE INDEX%", azArg[0], 0)==0; + z = sqlite3_mprintf("%s", azArg[0]); + shell_check_oom(z); + j = 0; + for(i=0; IsSpace(z[i]); i++){} + for(; (c = z[i])!=0; i++){ + if( IsSpace(c) ){ + if( z[j-1]=='\r' ) z[j-1] = '\n'; + if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue; + }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){ + j--; + } + z[j++] = c; + } + while( j>0 && IsSpace(z[j-1]) ){ j--; } + z[j] = 0; + if( strlen30(z)>=79 ){ + for(i=j=0; (c = z[i])!=0; i++){ /* Copy from z[i] back to z[j] */ + if( c==cEnd ){ + cEnd = 0; + }else if( c=='"' || c=='\'' || c=='`' ){ + cEnd = c; + }else if( c=='[' ){ + cEnd = ']'; + }else if( c=='-' && z[i+1]=='-' ){ + cEnd = '\n'; + }else if( c=='(' ){ + nParen++; + }else if( c==')' ){ + nParen--; + if( nLine>0 && nParen==0 && j>0 && !isWhere ){ + printSchemaLineN(p->out, z, j, "\n"); + j = 0; + } + }else if( (c=='w' || c=='W') + && nParen==0 && isIndex + && sqlite3_strnicmp("WHERE",&z[i],5)==0 + && !IsAlnum(z[i+5]) && z[i+5]!='_' ){ + isWhere = 1; + }else if( isWhere && (c=='A' || c=='a') + && nParen==0 + && sqlite3_strnicmp("AND",&z[i],3)==0 + && !IsAlnum(z[i+3]) && z[i+3]!='_' ){ + printSchemaLineN(p->out, z, j, "\n "); + j = 0; + } + z[j++] = c; + if( nParen==1 && cEnd==0 + && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1))) + && !isWhere + ){ + if( c=='\n' ) j--; + printSchemaLineN(p->out, z, j, "\n "); + j = 0; + nLine++; + while( IsSpace(z[i+1]) ){ i++; } + } + } + z[j] = 0; + } + printSchemaLine(p->out, z, ";\n"); + sqlite3_free(z); + break; + } + case MODE_List: { + if( p->cnt++==0 && p->showHeader ){ + for(i=0; iout, "%s%s", zOut, + i==nArg-1 ? p->rowSeparator : p->colSeparator); + if( pFree ) sqlite3_free(pFree); + } + } + if( azArg==0 ) break; + for(i=0; inullValue; + zOut = escapeOutput(p, z, &pFree); + sqlite3_fputs(zOut, p->out); + if( pFree ) sqlite3_free(pFree); + sqlite3_fputs((icolSeparator : p->rowSeparator, p->out); + } + break; + } + case MODE_Www: + case MODE_Html: { + if( p->cnt==0 && p->cMode==MODE_Www ){ + sqlite3_fputs( + "\n" + "
    \n" + ,p->out + ); + } + if( p->cnt==0 && (p->showHeader || p->cMode==MODE_Www) ){ + sqlite3_fputs("", p->out); + for(i=0; i", p->out); + output_html_string(p->out, azCol[i]); + sqlite3_fputs("\n", p->out); + } + sqlite3_fputs("\n", p->out); + } + p->cnt++; + if( azArg==0 ) break; + sqlite3_fputs("", p->out); + for(i=0; i", p->out); + output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue); + sqlite3_fputs("\n", p->out); + } + sqlite3_fputs("\n", p->out); + break; + } + case MODE_Tcl: { + if( p->cnt++==0 && p->showHeader ){ + for(i=0; iout, azCol[i] ? azCol[i] : ""); + if(icolSeparator, p->out); + } + sqlite3_fputs(p->rowSeparator, p->out); + } + if( azArg==0 ) break; + for(i=0; iout, azArg[i] ? azArg[i] : p->nullValue); + if(icolSeparator, p->out); + } + sqlite3_fputs(p->rowSeparator, p->out); + break; + } + case MODE_Csv: { + sqlite3_fsetmode(p->out, _O_BINARY); + if( p->cnt++==0 && p->showHeader ){ + for(i=0; irowSeparator, p->out); + } + if( nArg>0 ){ + for(i=0; irowSeparator, p->out); + } + setCrlfMode(p); + break; + } + case MODE_Insert: { + if( azArg==0 ) break; + sqlite3_fprintf(p->out, "INSERT INTO %s",p->zDestTable); + if( p->showHeader ){ + sqlite3_fputs("(", p->out); + for(i=0; i0 ) sqlite3_fputs(",", p->out); + if( quoteChar(azCol[i]) ){ + char *z = sqlite3_mprintf("\"%w\"", azCol[i]); + shell_check_oom(z); + sqlite3_fputs(z, p->out); + sqlite3_free(z); + }else{ + sqlite3_fprintf(p->out, "%s", azCol[i]); + } + } + sqlite3_fputs(")", p->out); + } + p->cnt++; + for(i=0; i0 ? "," : " VALUES(", p->out); + if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){ + sqlite3_fputs("NULL", p->out); + }else if( aiType && aiType[i]==SQLITE_TEXT ){ + if( ShellHasFlag(p, SHFLG_Newlines) ){ + output_quoted_string(p, azArg[i]); + }else{ + output_quoted_escaped_string(p, azArg[i]); + } + }else if( aiType && aiType[i]==SQLITE_INTEGER ){ + sqlite3_fputs(azArg[i], p->out); + }else if( aiType && aiType[i]==SQLITE_FLOAT ){ + char z[50]; + double r = sqlite3_column_double(p->pStmt, i); + sqlite3_uint64 ur; + memcpy(&ur,&r,sizeof(r)); + if( ur==0x7ff0000000000000LL ){ + sqlite3_fputs("9.0e+999", p->out); + }else if( ur==0xfff0000000000000LL ){ + sqlite3_fputs("-9.0e+999", p->out); + }else{ + sqlite3_int64 ir = (sqlite3_int64)r; + if( r==(double)ir ){ + sqlite3_snprintf(50,z,"%lld.0", ir); + }else{ + sqlite3_snprintf(50,z,"%!.20g", r); + } + sqlite3_fputs(z, p->out); + } + }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ + const void *pBlob = sqlite3_column_blob(p->pStmt, i); + int nBlob = sqlite3_column_bytes(p->pStmt, i); + output_hex_blob(p->out, pBlob, nBlob); + }else if( isNumber(azArg[i], 0) ){ + sqlite3_fputs(azArg[i], p->out); + }else if( ShellHasFlag(p, SHFLG_Newlines) ){ + output_quoted_string(p, azArg[i]); + }else{ + output_quoted_escaped_string(p, azArg[i]); + } + } + sqlite3_fputs(");\n", p->out); + break; + } + case MODE_Json: { + if( azArg==0 ) break; + if( p->cnt==0 ){ + sqlite3_fputs("[{", p->out); + }else{ + sqlite3_fputs(",\n{", p->out); + } + p->cnt++; + for(i=0; iout, azCol[i], -1); + sqlite3_fputs(":", p->out); + if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){ + sqlite3_fputs("null", p->out); + }else if( aiType && aiType[i]==SQLITE_FLOAT ){ + char z[50]; + double r = sqlite3_column_double(p->pStmt, i); + sqlite3_uint64 ur; + memcpy(&ur,&r,sizeof(r)); + if( ur==0x7ff0000000000000LL ){ + sqlite3_fputs("9.0e+999", p->out); + }else if( ur==0xfff0000000000000LL ){ + sqlite3_fputs("-9.0e+999", p->out); + }else{ + sqlite3_snprintf(50,z,"%!.20g", r); + sqlite3_fputs(z, p->out); + } + }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ + const void *pBlob = sqlite3_column_blob(p->pStmt, i); + int nBlob = sqlite3_column_bytes(p->pStmt, i); + output_json_string(p->out, pBlob, nBlob); + }else if( aiType && aiType[i]==SQLITE_TEXT ){ + output_json_string(p->out, azArg[i], -1); + }else{ + sqlite3_fputs(azArg[i], p->out); + } + if( iout); + } + } + sqlite3_fputs("}", p->out); + break; + } + case MODE_Quote: { + if( azArg==0 ) break; + if( p->cnt==0 && p->showHeader ){ + for(i=0; i0 ) sqlite3_fputs(p->colSeparator, p->out); + output_quoted_string(p, azCol[i]); + } + sqlite3_fputs(p->rowSeparator, p->out); + } + p->cnt++; + for(i=0; i0 ) sqlite3_fputs(p->colSeparator, p->out); + if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){ + sqlite3_fputs("NULL", p->out); + }else if( aiType && aiType[i]==SQLITE_TEXT ){ + output_quoted_string(p, azArg[i]); + }else if( aiType && aiType[i]==SQLITE_INTEGER ){ + sqlite3_fputs(azArg[i], p->out); + }else if( aiType && aiType[i]==SQLITE_FLOAT ){ + char z[50]; + double r = sqlite3_column_double(p->pStmt, i); + sqlite3_snprintf(50,z,"%!.20g", r); + sqlite3_fputs(z, p->out); + }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ + const void *pBlob = sqlite3_column_blob(p->pStmt, i); + int nBlob = sqlite3_column_bytes(p->pStmt, i); + output_hex_blob(p->out, pBlob, nBlob); + }else if( isNumber(azArg[i], 0) ){ + sqlite3_fputs(azArg[i], p->out); + }else{ + output_quoted_string(p, azArg[i]); + } + } + sqlite3_fputs(p->rowSeparator, p->out); + break; + } + case MODE_Ascii: { + if( p->cnt++==0 && p->showHeader ){ + for(i=0; i0 ) sqlite3_fputs(p->colSeparator, p->out); + sqlite3_fputs(azCol[i] ? azCol[i] : "", p->out); + } + sqlite3_fputs(p->rowSeparator, p->out); + } + if( azArg==0 ) break; + for(i=0; i0 ) sqlite3_fputs(p->colSeparator, p->out); + sqlite3_fputs(azArg[i] ? azArg[i] : p->nullValue, p->out); + } + sqlite3_fputs(p->rowSeparator, p->out); + break; + } + case MODE_EQP: { + eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]); + break; + } + } + return 0; +} + +/* +** This is the callback routine that the SQLite library +** invokes for each row of a query result. +*/ +static int callback(void *pArg, int nArg, char **azArg, char **azCol){ + /* since we don't have type info, call the shell_callback with a NULL value */ + return shell_callback(pArg, nArg, azArg, azCol, NULL); +} + +/* +** This is the callback routine from sqlite3_exec() that appends all +** output onto the end of a ShellText object. +*/ +static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){ + ShellText *p = (ShellText*)pArg; + int i; + UNUSED_PARAMETER(az); + if( azArg==0 ) return 0; + if( p->n ) appendText(p, "|", 0); + for(i=0; idb, + "SAVEPOINT selftest_init;\n" + "CREATE TABLE IF NOT EXISTS selftest(\n" + " tno INTEGER PRIMARY KEY,\n" /* Test number */ + " op TEXT,\n" /* Operator: memo run */ + " cmd TEXT,\n" /* Command text */ + " ans TEXT\n" /* Desired answer */ + ");" + "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n" + "INSERT INTO [_shell$self](rowid,op,cmd)\n" + " VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n" + " 'memo','Tests generated by --init');\n" + "INSERT INTO [_shell$self]\n" + " SELECT 'run',\n" + " 'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql " + "FROM sqlite_schema ORDER BY 2'',224))',\n" + " hex(sha3_query('SELECT type,name,tbl_name,sql " + "FROM sqlite_schema ORDER BY 2',224));\n" + "INSERT INTO [_shell$self]\n" + " SELECT 'run'," + " 'SELECT hex(sha3_query(''SELECT * FROM \"' ||" + " printf('%w',name) || '\" NOT INDEXED'',224))',\n" + " hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n" + " FROM (\n" + " SELECT name FROM sqlite_schema\n" + " WHERE type='table'\n" + " AND name<>'selftest'\n" + " AND coalesce(rootpage,0)>0\n" + " )\n" + " ORDER BY name;\n" + "INSERT INTO [_shell$self]\n" + " VALUES('run','PRAGMA integrity_check','ok');\n" + "INSERT INTO selftest(tno,op,cmd,ans)" + " SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n" + "DROP TABLE [_shell$self];" + ,0,0,&zErrMsg); + if( zErrMsg ){ + sqlite3_fprintf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg); + sqlite3_free(zErrMsg); + } + sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0); +} + + +/* +** Set the destination table field of the ShellState structure to +** the name of the table given. Escape any quote characters in the +** table name. +*/ +static void set_table_name(ShellState *p, const char *zName){ + if( p->zDestTable ){ + sqlite3_free(p->zDestTable); + p->zDestTable = 0; + } + if( zName==0 ) return; + if( quoteChar(zName) ){ + p->zDestTable = sqlite3_mprintf("\"%w\"", zName); + }else{ + p->zDestTable = sqlite3_mprintf("%s", zName); + } + shell_check_oom(p->zDestTable); +} + +/* +** Maybe construct two lines of text that point out the position of a +** syntax error. Return a pointer to the text, in memory obtained from +** sqlite3_malloc(). Or, if the most recent error does not involve a +** specific token that we can point to, return an empty string. +** +** In all cases, the memory returned is obtained from sqlite3_malloc64() +** and should be released by the caller invoking sqlite3_free(). +*/ +static char *shell_error_context(const char *zSql, sqlite3 *db){ + int iOffset; + size_t len; + char *zCode; + char *zMsg; + int i; + if( db==0 + || zSql==0 + || (iOffset = sqlite3_error_offset(db))<0 + || iOffset>=(int)strlen(zSql) + ){ + return sqlite3_mprintf(""); + } + while( iOffset>50 ){ + iOffset--; + zSql++; + while( (zSql[0]&0xc0)==0x80 ){ zSql++; iOffset--; } + } + len = strlen(zSql); + if( len>78 ){ + len = 78; + while( len>0 && (zSql[len]&0xc0)==0x80 ) len--; + } + zCode = sqlite3_mprintf("%.*s", len, zSql); + shell_check_oom(zCode); + for(i=0; zCode[i]; i++){ if( IsSpace(zSql[i]) ) zCode[i] = ' '; } + if( iOffset<25 ){ + zMsg = sqlite3_mprintf("\n %z\n %*s^--- error here", zCode,iOffset,""); + }else{ + zMsg = sqlite3_mprintf("\n %z\n %*serror here ---^", zCode,iOffset-14,""); + } + return zMsg; +} + + +/* +** Execute a query statement that will generate SQL output. Print +** the result columns, comma-separated, on a line and then add a +** semicolon terminator to the end of that line. +** +** If the number of columns is 1 and that column contains text "--" +** then write the semicolon on a separate line. That way, if a +** "--" comment occurs at the end of the statement, the comment +** won't consume the semicolon terminator. +*/ +static int run_table_dump_query( + ShellState *p, /* Query context */ + const char *zSelect /* SELECT statement to extract content */ +){ + sqlite3_stmt *pSelect; + int rc; + int nResult; + int i; + const char *z; + rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0); + if( rc!=SQLITE_OK || !pSelect ){ + char *zContext = shell_error_context(zSelect, p->db); + sqlite3_fprintf(p->out, "/**** ERROR: (%d) %s *****/\n%s", + rc, sqlite3_errmsg(p->db), zContext); + sqlite3_free(zContext); + if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++; + return rc; + } + rc = sqlite3_step(pSelect); + nResult = sqlite3_column_count(pSelect); + while( rc==SQLITE_ROW ){ + z = (const char*)sqlite3_column_text(pSelect, 0); + sqlite3_fprintf(p->out, "%s", z); + for(i=1; iout, ",%s", sqlite3_column_text(pSelect, i)); + } + if( z==0 ) z = ""; + while( z[0] && (z[0]!='-' || z[1]!='-') ) z++; + if( z[0] ){ + sqlite3_fputs("\n;\n", p->out); + }else{ + sqlite3_fputs(";\n", p->out); + } + rc = sqlite3_step(pSelect); + } + rc = sqlite3_finalize(pSelect); + if( rc!=SQLITE_OK ){ + sqlite3_fprintf(p->out, "/**** ERROR: (%d) %s *****/\n", + rc, sqlite3_errmsg(p->db)); + if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++; + } + return rc; +} + +/* +** Allocate space and save off string indicating current error. +*/ +static char *save_err_msg( + sqlite3 *db, /* Database to query */ + const char *zPhase, /* When the error occurs */ + int rc, /* Error code returned from API */ + const char *zSql /* SQL string, or NULL */ +){ + char *zErr; + char *zContext; + sqlite3_str *pStr = sqlite3_str_new(0); + sqlite3_str_appendf(pStr, "%s, %s", zPhase, sqlite3_errmsg(db)); + if( rc>1 ){ + sqlite3_str_appendf(pStr, " (%d)", rc); + } + zContext = shell_error_context(zSql, db); + if( zContext ){ + sqlite3_str_appendall(pStr, zContext); + sqlite3_free(zContext); + } + zErr = sqlite3_str_finish(pStr); + shell_check_oom(zErr); + return zErr; +} + +#ifdef __linux__ +/* +** Attempt to display I/O stats on Linux using /proc/PID/io +*/ +static void displayLinuxIoStats(FILE *out){ + FILE *in; + char z[200]; + sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid()); + in = sqlite3_fopen(z, "rb"); + if( in==0 ) return; + while( sqlite3_fgets(z, sizeof(z), in)!=0 ){ + static const struct { + const char *zPattern; + const char *zDesc; + } aTrans[] = { + { "rchar: ", "Bytes received by read():" }, + { "wchar: ", "Bytes sent to write():" }, + { "syscr: ", "Read() system calls:" }, + { "syscw: ", "Write() system calls:" }, + { "read_bytes: ", "Bytes read from storage:" }, + { "write_bytes: ", "Bytes written to storage:" }, + { "cancelled_write_bytes: ", "Cancelled write bytes:" }, + }; + int i; + for(i=0; i1 ){ + sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr); + }else{ + sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr); + } + sqlite3_fprintf(out, "%-36s %s\n", zLabel, zLine); +} + +/* +** Display memory stats. +*/ +static int display_stats( + sqlite3 *db, /* Database to query */ + ShellState *pArg, /* Pointer to ShellState */ + int bReset /* True to reset the stats */ +){ + int iCur, iHiwtr; + sqlite3_int64 iCur64, iHiwtr64; + FILE *out; + if( pArg==0 || pArg->out==0 ) return 0; + out = pArg->out; + + if( pArg->pStmt && pArg->statsOn==2 ){ + int nCol, i, x; + sqlite3_stmt *pStmt = pArg->pStmt; + char z[100]; + nCol = sqlite3_column_count(pStmt); + sqlite3_fprintf(out, "%-36s %d\n", "Number of output columns:", nCol); + for(i=0; istatsOn==3 ){ + if( pArg->pStmt ){ + iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP,bReset); + sqlite3_fprintf(out, "VM-steps: %d\n", iCur); + } + return 0; + } + + displayStatLine(out, "Memory Used:", + "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset); + displayStatLine(out, "Number of Outstanding Allocations:", + "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset); + if( pArg->shellFlgs & SHFLG_Pagecache ){ + displayStatLine(out, "Number of Pcache Pages Used:", + "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset); + } + displayStatLine(out, "Number of Pcache Overflow Bytes:", + "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset); + displayStatLine(out, "Largest Allocation:", + "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset); + displayStatLine(out, "Largest Pcache Allocation:", + "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset); +#ifdef YYTRACKMAXSTACKDEPTH + displayStatLine(out, "Deepest Parser Stack:", + "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset); +#endif + + if( db ){ + if( pArg->shellFlgs & SHFLG_Lookaside ){ + iHiwtr = iCur = -1; + sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED, + &iCur, &iHiwtr, bReset); + sqlite3_fprintf(out, + "Lookaside Slots Used: %d (max %d)\n", iCur, iHiwtr); + sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT, + &iCur, &iHiwtr, bReset); + sqlite3_fprintf(out, + "Successful lookaside attempts: %d\n", iHiwtr); + sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, + &iCur, &iHiwtr, bReset); + sqlite3_fprintf(out, + "Lookaside failures due to size: %d\n", iHiwtr); + sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, + &iCur, &iHiwtr, bReset); + sqlite3_fprintf(out, + "Lookaside failures due to OOM: %d\n", iHiwtr); + } + iHiwtr = iCur = -1; + sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset); + sqlite3_fprintf(out, + "Pager Heap Usage: %d bytes\n", iCur); + iHiwtr = iCur = -1; + sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1); + sqlite3_fprintf(out, + "Page cache hits: %d\n", iCur); + iHiwtr = iCur = -1; + sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1); + sqlite3_fprintf(out, + "Page cache misses: %d\n", iCur); + iHiwtr64 = iCur64 = -1; + sqlite3_db_status64(db, SQLITE_DBSTATUS_TEMPBUF_SPILL, &iCur64, &iHiwtr64, + 0); + iHiwtr = iCur = -1; + sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1); + sqlite3_fprintf(out, + "Page cache writes: %d\n", iCur); + iHiwtr = iCur = -1; + sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1); + sqlite3_fprintf(out, + "Page cache spills: %d\n", iCur); + sqlite3_fprintf(out, + "Temporary data spilled to disk: %lld\n", iCur64); + sqlite3_db_status64(db, SQLITE_DBSTATUS_TEMPBUF_SPILL, &iCur64, &iHiwtr64, + 1); + iHiwtr = iCur = -1; + sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset); + sqlite3_fprintf(out, + "Schema Heap Usage: %d bytes\n", iCur); + iHiwtr = iCur = -1; + sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset); + sqlite3_fprintf(out, + "Statement Heap/Lookaside Usage: %d bytes\n", iCur); + } + + if( pArg->pStmt ){ + int iHit, iMiss; + iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP, + bReset); + sqlite3_fprintf(out, + "Fullscan Steps: %d\n", iCur); + iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset); + sqlite3_fprintf(out, + "Sort Operations: %d\n", iCur); + iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset); + sqlite3_fprintf(out, + "Autoindex Inserts: %d\n", iCur); + iHit = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_HIT, + bReset); + iMiss = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_MISS, + bReset); + if( iHit || iMiss ){ + sqlite3_fprintf(out, + "Bloom filter bypass taken: %d/%d\n", iHit, iHit+iMiss); + } + iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset); + sqlite3_fprintf(out, + "Virtual Machine Steps: %d\n", iCur); + iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset); + sqlite3_fprintf(out, + "Reprepare operations: %d\n", iCur); + iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset); + sqlite3_fprintf(out, + "Number of times run: %d\n", iCur); + iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset); + sqlite3_fprintf(out, + "Memory used by prepared stmt: %d\n", iCur); + } + +#ifdef __linux__ + displayLinuxIoStats(pArg->out); +#endif + + /* Do not remove this machine readable comment: extra-stats-output-here */ + + return 0; +} + + +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS +static int scanStatsHeight(sqlite3_stmt *p, int iEntry){ + int iPid = 0; + int ret = 1; + sqlite3_stmt_scanstatus_v2(p, iEntry, + SQLITE_SCANSTAT_SELECTID, SQLITE_SCANSTAT_COMPLEX, (void*)&iPid + ); + while( iPid!=0 ){ + int ii; + for(ii=0; 1; ii++){ + int iId; + int res; + res = sqlite3_stmt_scanstatus_v2(p, ii, + SQLITE_SCANSTAT_SELECTID, SQLITE_SCANSTAT_COMPLEX, (void*)&iId + ); + if( res ) break; + if( iId==iPid ){ + sqlite3_stmt_scanstatus_v2(p, ii, + SQLITE_SCANSTAT_PARENTID, SQLITE_SCANSTAT_COMPLEX, (void*)&iPid + ); + } + } + ret++; + } + return ret; +} +#endif + +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS +static void display_explain_scanstats( + sqlite3 *db, /* Database to query */ + ShellState *pArg /* Pointer to ShellState */ +){ + static const int f = SQLITE_SCANSTAT_COMPLEX; + sqlite3_stmt *p = pArg->pStmt; + int ii = 0; + i64 nTotal = 0; + int nWidth = 0; + eqp_reset(pArg); + + for(ii=0; 1; ii++){ + const char *z = 0; + int n = 0; + if( sqlite3_stmt_scanstatus_v2(p,ii,SQLITE_SCANSTAT_EXPLAIN,f,(void*)&z) ){ + break; + } + n = (int)strlen(z) + scanStatsHeight(p, ii)*3; + if( n>nWidth ) nWidth = n; + } + nWidth += 4; + + sqlite3_stmt_scanstatus_v2(p, -1, SQLITE_SCANSTAT_NCYCLE, f, (void*)&nTotal); + for(ii=0; 1; ii++){ + i64 nLoop = 0; + i64 nRow = 0; + i64 nCycle = 0; + int iId = 0; + int iPid = 0; + const char *zo = 0; + const char *zName = 0; + char *zText = 0; + double rEst = 0.0; + + if( sqlite3_stmt_scanstatus_v2(p,ii,SQLITE_SCANSTAT_EXPLAIN,f,(void*)&zo) ){ + break; + } + sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_EST,f,(void*)&rEst); + sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_NLOOP,f,(void*)&nLoop); + sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_NVISIT,f,(void*)&nRow); + sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_NCYCLE,f,(void*)&nCycle); + sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_SELECTID,f,(void*)&iId); + sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_PARENTID,f,(void*)&iPid); + sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_NAME,f,(void*)&zName); + + zText = sqlite3_mprintf("%s", zo); + if( nCycle>=0 || nLoop>=0 || nRow>=0 ){ + char *z = 0; + if( nCycle>=0 && nTotal>0 ){ + z = sqlite3_mprintf("%zcycles=%lld [%d%%]", z, + nCycle, ((nCycle*100)+nTotal/2) / nTotal + ); + } + if( nLoop>=0 ){ + z = sqlite3_mprintf("%z%sloops=%lld", z, z ? " " : "", nLoop); + } + if( nRow>=0 ){ + z = sqlite3_mprintf("%z%srows=%lld", z, z ? " " : "", nRow); + } + + if( zName && pArg->scanstatsOn>1 ){ + double rpl = (double)nRow / (double)nLoop; + z = sqlite3_mprintf("%z rpl=%.1f est=%.1f", z, rpl, rEst); + } + + zText = sqlite3_mprintf( + "% *z (%z)", -1*(nWidth-scanStatsHeight(p, ii)*3), zText, z + ); + } + + eqp_append(pArg, iId, iPid, zText); + sqlite3_free(zText); + } + + eqp_render(pArg, nTotal); +} +#endif + + +/* +** Parameter azArray points to a zero-terminated array of strings. zStr +** points to a single nul-terminated string. Return non-zero if zStr +** is equal, according to strcmp(), to any of the strings in the array. +** Otherwise, return zero. +*/ +static int str_in_array(const char *zStr, const char **azArray){ + int i; + for(i=0; azArray[i]; i++){ + if( 0==cli_strcmp(zStr, azArray[i]) ) return 1; + } + return 0; +} + +/* +** If compiled statement pSql appears to be an EXPLAIN statement, allocate +** and populate the ShellState.aiIndent[] array with the number of +** spaces each opcode should be indented before it is output. +** +** The indenting rules are: +** +** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent +** all opcodes that occur between the p2 jump destination and the opcode +** itself by 2 spaces. +** +** * Do the previous for "Return" instructions for when P2 is positive. +** See tag-20220407a in wherecode.c and vdbe.c. +** +** * For each "Goto", if the jump destination is earlier in the program +** and ends on one of: +** Yield SeekGt SeekLt RowSetRead Rewind +** or if the P1 parameter is one instead of zero, +** then indent all opcodes between the earlier instruction +** and "Goto" by 2 spaces. +*/ +static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){ + int *abYield = 0; /* True if op is an OP_Yield */ + int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */ + int iOp; /* Index of operation in p->aiIndent[] */ + + const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", + "Return", 0 }; + const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead", + "Rewind", 0 }; + const char *azGoto[] = { "Goto", 0 }; + + /* The caller guarantees that the leftmost 4 columns of the statement + ** passed to this function are equivalent to the leftmost 4 columns + ** of EXPLAIN statement output. In practice the statement may be + ** an EXPLAIN, or it may be a query on the bytecode() virtual table. */ + assert( sqlite3_column_count(pSql)>=4 ); + assert( 0==sqlite3_stricmp( sqlite3_column_name(pSql, 0), "addr" ) ); + assert( 0==sqlite3_stricmp( sqlite3_column_name(pSql, 1), "opcode" ) ); + assert( 0==sqlite3_stricmp( sqlite3_column_name(pSql, 2), "p1" ) ); + assert( 0==sqlite3_stricmp( sqlite3_column_name(pSql, 3), "p2" ) ); + + for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){ + int i; + int iAddr = sqlite3_column_int(pSql, 0); + const char *zOp = (const char*)sqlite3_column_text(pSql, 1); + int p1 = sqlite3_column_int(pSql, 2); + int p2 = sqlite3_column_int(pSql, 3); + + /* Assuming that p2 is an instruction address, set variable p2op to the + ** index of that instruction in the aiIndent[] array. p2 and p2op may be + ** different if the current instruction is part of a sub-program generated + ** by an SQL trigger or foreign key. */ + int p2op = (p2 + (iOp-iAddr)); + + /* Grow the p->aiIndent array as required */ + if( iOp>=nAlloc ){ + nAlloc += 100; + p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int)); + shell_check_oom(p->aiIndent); + abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int)); + shell_check_oom(abYield); + } + + abYield[iOp] = str_in_array(zOp, azYield); + p->aiIndent[iOp] = 0; + p->nIndent = iOp+1; + if( str_in_array(zOp, azNext) && p2op>0 ){ + for(i=p2op; iaiIndent[i] += 2; + } + if( str_in_array(zOp, azGoto) && p2opaiIndent[i] += 2; + } + } + + p->iIndent = 0; + sqlite3_free(abYield); + sqlite3_reset(pSql); +} + +/* +** Free the array allocated by explain_data_prepare(). +*/ +static void explain_data_delete(ShellState *p){ + sqlite3_free(p->aiIndent); + p->aiIndent = 0; + p->nIndent = 0; + p->iIndent = 0; +} + +static void exec_prepared_stmt(ShellState*, sqlite3_stmt*); + +/* +** Display scan stats. +*/ +static void display_scanstats( + sqlite3 *db, /* Database to query */ + ShellState *pArg /* Pointer to ShellState */ +){ +#ifndef SQLITE_ENABLE_STMT_SCANSTATUS + UNUSED_PARAMETER(db); + UNUSED_PARAMETER(pArg); +#else + if( pArg->scanstatsOn==3 ){ + const char *zSql = + " SELECT addr, opcode, p1, p2, p3, p4, p5, comment, nexec," + " format('% 6s (%.2f%%)'," + " CASE WHEN ncycle<100_000 THEN ncycle || ' '" + " WHEN ncycle<100_000_000 THEN (ncycle/1_000) || 'K'" + " WHEN ncycle<100_000_000_000 THEN (ncycle/1_000_000) || 'M'" + " ELSE (ncycle/1000_000_000) || 'G' END," + " ncycle*100.0/(sum(ncycle) OVER ())" + " ) AS cycles" + " FROM bytecode(?)"; + + int rc = SQLITE_OK; + sqlite3_stmt *pStmt = 0; + rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); + if( rc==SQLITE_OK ){ + sqlite3_stmt *pSave = pArg->pStmt; + pArg->pStmt = pStmt; + sqlite3_bind_pointer(pStmt, 1, pSave, "stmt-pointer", 0); + + pArg->cnt = 0; + pArg->cMode = MODE_ScanExp; + explain_data_prepare(pArg, pStmt); + exec_prepared_stmt(pArg, pStmt); + explain_data_delete(pArg); + + sqlite3_finalize(pStmt); + pArg->pStmt = pSave; + } + }else{ + display_explain_scanstats(db, pArg); + } +#endif +} + +/* +** Disable and restore .wheretrace and .treetrace/.selecttrace settings. +*/ +static unsigned int savedSelectTrace; +static unsigned int savedWhereTrace; +static void disable_debug_trace_modes(void){ + unsigned int zero = 0; + sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 0, &savedSelectTrace); + sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &zero); + sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 2, &savedWhereTrace); + sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &zero); +} +static void restore_debug_trace_modes(void){ + sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &savedSelectTrace); + sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &savedWhereTrace); +} + +/* Create the TEMP table used to store parameter bindings */ +static void bind_table_init(ShellState *p){ + int wrSchema = 0; + int defensiveMode = 0; + sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &defensiveMode); + sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0); + sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema); + sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0); + sqlite3_exec(p->db, + "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n" + " key TEXT PRIMARY KEY,\n" + " value\n" + ") WITHOUT ROWID;", + 0, 0, 0); + sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0); + sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, defensiveMode, 0); +} + +/* +** Bind parameters on a prepared statement. +** +** Parameter bindings are taken from a TEMP table of the form: +** +** CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value) +** WITHOUT ROWID; +** +** No bindings occur if this table does not exist. The name of the table +** begins with "sqlite_" so that it will not collide with ordinary application +** tables. The table must be in the TEMP schema. +*/ +static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){ + int nVar; + int i; + int rc; + sqlite3_stmt *pQ = 0; + + nVar = sqlite3_bind_parameter_count(pStmt); + if( nVar==0 ) return; /* Nothing to do */ + if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters", + "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){ + rc = SQLITE_NOTFOUND; + pQ = 0; + }else{ + rc = sqlite3_prepare_v2(pArg->db, + "SELECT value FROM temp.sqlite_parameters" + " WHERE key=?1", -1, &pQ, 0); + } + for(i=1; i<=nVar; i++){ + char zNum[30]; + const char *zVar = sqlite3_bind_parameter_name(pStmt, i); + if( zVar==0 ){ + sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i); + zVar = zNum; + } + sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC); + if( rc==SQLITE_OK && pQ && sqlite3_step(pQ)==SQLITE_ROW ){ + sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0)); +#ifdef NAN + }else if( sqlite3_strlike("_NAN", zVar, 0)==0 ){ + sqlite3_bind_double(pStmt, i, NAN); +#endif +#ifdef INFINITY + }else if( sqlite3_strlike("_INF", zVar, 0)==0 ){ + sqlite3_bind_double(pStmt, i, INFINITY); +#endif + }else if( strncmp(zVar, "$int_", 5)==0 ){ + sqlite3_bind_int(pStmt, i, atoi(&zVar[5])); + }else if( strncmp(zVar, "$text_", 6)==0 ){ + size_t szVar = strlen(zVar); + char *zBuf = sqlite3_malloc64( szVar-5 ); + if( zBuf ){ + memcpy(zBuf, &zVar[6], szVar-5); + sqlite3_bind_text64(pStmt, i, zBuf, szVar-6, sqlite3_free, SQLITE_UTF8); + } +#ifdef SQLITE_ENABLE_CARRAY + }else if( strncmp(zVar, "$carray_", 8)==0 ){ + static char *azColorNames[] = { + "azure", "black", "blue", "brown", "cyan", "fuchsia", "gold", + "gray", "green", "indigo", "khaki", "lime", "magenta", "maroon", + "navy", "olive", "orange", "pink", "purple", "red", "silver", + "tan", "teal", "violet", "white", "yellow" + }; + static int aPrimes[] = { + 1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, + 53, 59, 61, 67, 71, 73, 79, 83, 89, 97 + }; + /* Special bindings: carray($carray_clr), carray($carray_primes) + ** with --unsafe-testing: carray($carray_clr_p,26,'char*'), + ** carray($carray_primes_p,26,'int32') + */ + if( strcmp(zVar+8,"clr")==0 ){ + sqlite3_carray_bind(pStmt,i,azColorNames,26,SQLITE_CARRAY_TEXT,0); + }else if( strcmp(zVar+8,"primes")==0 ){ + sqlite3_carray_bind(pStmt,i,aPrimes,26,SQLITE_CARRAY_INT32,0); + }else if( strcmp(zVar+8,"clr_p")==0 + && ShellHasFlag(pArg,SHFLG_TestingMode) ){ + sqlite3_bind_pointer(pStmt,i,azColorNames,"carray",0); + }else if( strcmp(zVar+8,"primes_p")==0 + && ShellHasFlag(pArg,SHFLG_TestingMode) ){ + sqlite3_bind_pointer(pStmt,i,aPrimes,"carray",0); + }else{ + sqlite3_bind_null(pStmt, i); + } +#endif + }else{ + sqlite3_bind_null(pStmt, i); + } + sqlite3_reset(pQ); + } + sqlite3_finalize(pQ); +} + +/* +** UTF8 box-drawing characters. Imagine box lines like this: +** +** 1 +** | +** 4 --+-- 2 +** | +** 3 +** +** Each box characters has between 2 and 4 of the lines leading from +** the center. The characters are here identified by the numbers of +** their corresponding lines. +*/ +#define BOX_24 "\342\224\200" /* U+2500 --- */ +#define BOX_13 "\342\224\202" /* U+2502 | */ +#define BOX_23 "\342\224\214" /* U+250c ,- */ +#define BOX_34 "\342\224\220" /* U+2510 -, */ +#define BOX_12 "\342\224\224" /* U+2514 '- */ +#define BOX_14 "\342\224\230" /* U+2518 -' */ +#define BOX_123 "\342\224\234" /* U+251c |- */ +#define BOX_134 "\342\224\244" /* U+2524 -| */ +#define BOX_234 "\342\224\254" /* U+252c -,- */ +#define BOX_124 "\342\224\264" /* U+2534 -'- */ +#define BOX_1234 "\342\224\274" /* U+253c -|- */ + +/* Draw horizontal line N characters long using unicode box +** characters +*/ +static void print_box_line(FILE *out, int N){ + const char zDash[] = + BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 + BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24; + const int nDash = sizeof(zDash) - 1; + N *= 3; + while( N>nDash ){ + sqlite3_fputs(zDash, out); + N -= nDash; + } + sqlite3_fprintf(out, "%.*s", N, zDash); +} + +/* +** Draw a horizontal separator for a MODE_Box table. +*/ +static void print_box_row_separator( + ShellState *p, + int nArg, + const char *zSep1, + const char *zSep2, + const char *zSep3 +){ + int i; + if( nArg>0 ){ + sqlite3_fputs(zSep1, p->out); + print_box_line(p->out, p->actualWidth[0]+2); + for(i=1; iout); + print_box_line(p->out, p->actualWidth[i]+2); + } + sqlite3_fputs(zSep3, p->out); + } + sqlite3_fputs("\n", p->out); +} + +/* +** z[] is a line of text that is to be displayed the .mode box or table or +** similar tabular formats. z[] might contain control characters such +** as \n, \t, \f, or \r. +** +** Compute characters to display on the first line of z[]. Stop at the +** first \r, \n, or \f. Expand \t into spaces. Return a copy (obtained +** from malloc()) of that first line, which caller should free sometime. +** Write anything to display on the next line into *pzTail. If this is +** the last line, write a NULL into *pzTail. (*pzTail is not allocated.) +*/ +static char *translateForDisplayAndDup( + ShellState *p, /* To access current settings */ + const unsigned char *z, /* Input text to be transformed */ + const unsigned char **pzTail, /* OUT: Tail of the input for next line */ + int mxWidth, /* Max width. 0 means no limit */ + u8 bWordWrap /* If true, avoid breaking mid-word */ +){ + int i; /* Input bytes consumed */ + int j; /* Output bytes generated */ + int k; /* Input bytes to be displayed */ + int n; /* Output column number */ + unsigned char *zOut; /* Output text */ + + if( z==0 ){ + *pzTail = 0; + return 0; + } + if( mxWidth<0 ) mxWidth = -mxWidth; + if( mxWidth==0 ) mxWidth = 1000000; + i = j = n = 0; + while( n=0xc0 ){ + int u; + int len = decodeUtf8(&z[i], &u); + i += len; + j += len; + n += cli_wcwidth(u); + continue; + } + if( c>=' ' ){ + n++; + i++; + j++; + continue; + } + if( c==0 || c=='\n' || (c=='\r' && z[i+1]=='\n') ) break; + if( c=='\t' ){ + do{ + n++; + j++; + }while( (n&7)!=0 && neEscMode==SHELL_ESC_OFF && (k = isVt100(&z[i]))>0 ){ + i += k; + j += k; + }else{ + n++; + j += 3; + i++; + } + } + if( n>=mxWidth && bWordWrap ){ + /* Perhaps try to back up to a better place to break the line */ + for(k=i; k>i/2; k--){ + if( IsSpace(z[k-1]) ) break; + } + if( k<=i/2 ){ + for(k=i; k>i/2; k--){ + if( IsAlnum(z[k-1])!=IsAlnum(z[k]) && (z[k]&0xc0)!=0x80 ) break; + } + } + if( k<=i/2 ){ + k = i; + }else{ + i = k; + while( z[i]==' ' ) i++; + } + }else{ + k = i; + } + if( n>=mxWidth && z[i]>=' ' ){ + *pzTail = &z[i]; + }else if( z[i]=='\r' && z[i+1]=='\n' ){ + *pzTail = z[i+2] ? &z[i+2] : 0; + }else if( z[i]==0 || z[i+1]==0 ){ + *pzTail = 0; + }else{ + *pzTail = &z[i+1]; + } + zOut = malloc( j+1 ); + shell_check_oom(zOut); + i = j = n = 0; + while( i=0xc0 ){ + int u; + int len = decodeUtf8(&z[i], &u); + do{ zOut[j++] = z[i++]; }while( (--len)>0 ); + n += cli_wcwidth(u); + continue; + } + if( c>=' ' ){ + n++; + zOut[j++] = z[i++]; + continue; + } + if( c==0 ) break; + if( z[i]=='\t' ){ + do{ + n++; + zOut[j++] = ' '; + }while( (n&7)!=0 && neEscMode ){ + case SHELL_ESC_SYMBOL: + zOut[j++] = 0xe2; + zOut[j++] = 0x90; + zOut[j++] = 0x80 + c; + break; + case SHELL_ESC_ASCII: + zOut[j++] = '^'; + zOut[j++] = 0x40 + c; + break; + case SHELL_ESC_OFF: { + int nn; + if( c==0x1b && (nn = isVt100(&z[i]))>0 ){ + memcpy(&zOut[j], &z[i], nn); + j += nn; + i += nn - 1; + }else{ + zOut[j++] = c; + } + break; + } + } + i++; + } + zOut[j] = 0; + return (char*)zOut; +} + +/* Return true if the text string z[] contains characters that need +** unistr() escaping. +*/ +static int needUnistr(const unsigned char *z){ + unsigned char c; + if( z==0 ) return 0; + while( (c = *z)>0x1f || c=='\t' || c=='\n' || (c=='\r' && z[1]=='\n') ){ z++; } + return c!=0; +} + +/* Extract the value of the i-th current column for pStmt as an SQL literal +** value. Memory is obtained from sqlite3_malloc64() and must be freed by +** the caller. +*/ +static char *quoted_column(sqlite3_stmt *pStmt, int i){ + switch( sqlite3_column_type(pStmt, i) ){ + case SQLITE_NULL: { + return sqlite3_mprintf("NULL"); + } + case SQLITE_INTEGER: + case SQLITE_FLOAT: { + return sqlite3_mprintf("%s",sqlite3_column_text(pStmt,i)); + } + case SQLITE_TEXT: { + const unsigned char *zText = sqlite3_column_text(pStmt,i); + return sqlite3_mprintf(needUnistr(zText)?"%#Q":"%Q",zText); + } + case SQLITE_BLOB: { + int j; + sqlite3_str *pStr = sqlite3_str_new(0); + const unsigned char *a = sqlite3_column_blob(pStmt,i); + int n = sqlite3_column_bytes(pStmt,i); + sqlite3_str_append(pStr, "x'", 2); + for(j=0; jcmOpts.bWordWrap; + const char *zEmpty = ""; + const char *zShowNull = p->nullValue; + + rc = sqlite3_step(pStmt); + if( rc!=SQLITE_ROW ) return; + nColumn = sqlite3_column_count(pStmt); + if( nColumn==0 ) goto columnar_end; + nAlloc = nColumn*4; + if( nAlloc<=0 ) nAlloc = 1; + azData = sqlite3_malloc64( nAlloc*sizeof(char*) ); + shell_check_oom(azData); + azNextLine = sqlite3_malloc64( nColumn*sizeof(char*) ); + shell_check_oom(azNextLine); + memset((void*)azNextLine, 0, nColumn*sizeof(char*) ); + if( p->cmOpts.bQuote ){ + azQuoted = sqlite3_malloc64( nColumn*sizeof(char*) ); + shell_check_oom(azQuoted); + memset(azQuoted, 0, nColumn*sizeof(char*) ); + } + abRowDiv = sqlite3_malloc64( nAlloc/nColumn ); + shell_check_oom(abRowDiv); + if( nColumn>p->nWidth ){ + p->colWidth = realloc(p->colWidth, (nColumn+1)*2*sizeof(int)); + shell_check_oom(p->colWidth); + for(i=p->nWidth; icolWidth[i] = 0; + p->nWidth = nColumn; + p->actualWidth = &p->colWidth[nColumn]; + } + memset(p->actualWidth, 0, nColumn*sizeof(int)); + for(i=0; icolWidth[i]; + if( w<0 ) w = -w; + p->actualWidth[i] = w; + } + for(i=0; icolWidth[i]; + if( wx==0 ){ + wx = p->cmOpts.iWrap; + } + if( wx<0 ) wx = -wx; + uz = (const unsigned char*)sqlite3_column_name(pStmt,i); + if( uz==0 ) uz = (u8*)""; + azData[i] = translateForDisplayAndDup(p, uz, &zNotUsed, wx, bw); + } + do{ + int useNextLine = bNextLine; + bNextLine = 0; + if( (nRow+2)*nColumn >= nAlloc ){ + nAlloc *= 2; + azData = sqlite3_realloc64(azData, nAlloc*sizeof(char*)); + shell_check_oom(azData); + abRowDiv = sqlite3_realloc64(abRowDiv, nAlloc/nColumn); + shell_check_oom(abRowDiv); + } + abRowDiv[nRow] = 1; + nRow++; + for(i=0; icolWidth[i]; + if( wx==0 ){ + wx = p->cmOpts.iWrap; + } + if( wx<0 ) wx = -wx; + if( useNextLine ){ + uz = azNextLine[i]; + if( uz==0 ) uz = (u8*)zEmpty; + }else if( p->cmOpts.bQuote ){ + assert( azQuoted!=0 ); + sqlite3_free(azQuoted[i]); + azQuoted[i] = quoted_column(pStmt,i); + uz = (const unsigned char*)azQuoted[i]; + }else{ + uz = (const unsigned char*)sqlite3_column_text(pStmt,i); + if( uz==0 ) uz = (u8*)zShowNull; + } + azData[nRow*nColumn + i] + = translateForDisplayAndDup(p, uz, &azNextLine[i], wx, bw); + if( azNextLine[i] ){ + bNextLine = 1; + abRowDiv[nRow-1] = 0; + bMultiLineRowExists = 1; + } + } + }while( bNextLine || sqlite3_step(pStmt)==SQLITE_ROW ); + nTotal = nColumn*(nRow+1); + for(i=0; ip->actualWidth[j] ) p->actualWidth[j] = n; + } + if( seenInterrupt ) goto columnar_end; + switch( p->cMode ){ + case MODE_Column: { + colSep = " "; + rowSep = "\n"; + if( p->showHeader ){ + for(i=0; iactualWidth[i]; + if( p->colWidth[i]<0 ) w = -w; + utf8_width_print(p->out, w, azData[i]); + sqlite3_fputs(i==nColumn-1?"\n":" ", p->out); + } + for(i=0; iout, p->actualWidth[i]); + sqlite3_fputs(i==nColumn-1?"\n":" ", p->out); + } + } + break; + } + case MODE_Table: { + colSep = " | "; + rowSep = " |\n"; + print_row_separator(p, nColumn, "+"); + sqlite3_fputs("| ", p->out); + for(i=0; iactualWidth[i]; + n = strlenChar(azData[i]); + sqlite3_fprintf(p->out, "%*s%s%*s", (w-n)/2, "", + azData[i], (w-n+1)/2, ""); + sqlite3_fputs(i==nColumn-1?" |\n":" | ", p->out); + } + print_row_separator(p, nColumn, "+"); + break; + } + case MODE_Markdown: { + colSep = " | "; + rowSep = " |\n"; + sqlite3_fputs("| ", p->out); + for(i=0; iactualWidth[i]; + n = strlenChar(azData[i]); + sqlite3_fprintf(p->out, "%*s%s%*s", (w-n)/2, "", + azData[i], (w-n+1)/2, ""); + sqlite3_fputs(i==nColumn-1?" |\n":" | ", p->out); + } + print_row_separator(p, nColumn, "|"); + break; + } + case MODE_Box: { + colSep = " " BOX_13 " "; + rowSep = " " BOX_13 "\n"; + print_box_row_separator(p, nColumn, BOX_23, BOX_234, BOX_34); + sqlite3_fputs(BOX_13 " ", p->out); + for(i=0; iactualWidth[i]; + n = strlenChar(azData[i]); + sqlite3_fprintf(p->out, "%*s%s%*s%s", + (w-n)/2, "", azData[i], (w-n+1)/2, "", + i==nColumn-1?" "BOX_13"\n":" "BOX_13" "); + } + print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134); + break; + } + } + for(i=nColumn, j=0; icMode!=MODE_Column ){ + sqlite3_fputs(p->cMode==MODE_Box?BOX_13" ":"| ", p->out); + } + z = azData[i]; + if( z==0 ) z = p->nullValue; + w = p->actualWidth[j]; + if( p->colWidth[j]<0 ) w = -w; + utf8_width_print(p->out, w, z); + if( j==nColumn-1 ){ + sqlite3_fputs(rowSep, p->out); + if( bMultiLineRowExists && abRowDiv[i/nColumn-1] && i+1cMode==MODE_Table ){ + print_row_separator(p, nColumn, "+"); + }else if( p->cMode==MODE_Box ){ + print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134); + }else if( p->cMode==MODE_Column ){ + sqlite3_fputs("\n", p->out); + } + } + j = -1; + if( seenInterrupt ) goto columnar_end; + }else{ + sqlite3_fputs(colSep, p->out); + } + } + if( p->cMode==MODE_Table ){ + print_row_separator(p, nColumn, "+"); + }else if( p->cMode==MODE_Box ){ + print_box_row_separator(p, nColumn, BOX_12, BOX_124, BOX_14); + } +columnar_end: + if( seenInterrupt ){ + sqlite3_fputs("Interrupt\n", p->out); + } + nData = (nRow+1)*nColumn; + for(i=0; icMode==MODE_Column + || pArg->cMode==MODE_Table + || pArg->cMode==MODE_Box + || pArg->cMode==MODE_Markdown + ){ + exec_prepared_stmt_columnar(pArg, pStmt); + return; + } + + /* perform the first step. this will tell us if we + ** have a result set or not and how wide it is. + */ + rc = sqlite3_step(pStmt); + /* if we have a result set... */ + if( SQLITE_ROW == rc ){ + /* allocate space for col name ptr, value ptr, and type */ + int nCol = sqlite3_column_count(pStmt); + void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1); + if( !pData ){ + shell_out_of_memory(); + }else{ + char **azCols = (char **)pData; /* Names of result columns */ + char **azVals = &azCols[nCol]; /* Results */ + int *aiTypes = (int *)&azVals[nCol]; /* Result types */ + int i, x; + assert(sizeof(int) <= sizeof(char *)); + /* save off ptrs to column names */ + for(i=0; icMode==MODE_Insert || pArg->cMode==MODE_Quote) + ){ + azVals[i] = ""; + }else{ + azVals[i] = (char*)sqlite3_column_text(pStmt, i); + } + if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){ + rc = SQLITE_NOMEM; + break; /* from for */ + } + } /* end for */ + + /* if data and types extracted successfully... */ + if( SQLITE_ROW == rc ){ + /* call the supplied callback with the result row data */ + if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){ + rc = SQLITE_ABORT; + }else{ + rc = sqlite3_step(pStmt); + } + } + } while( SQLITE_ROW == rc ); + sqlite3_free(pData); + if( pArg->cMode==MODE_Json ){ + sqlite3_fputs("]\n", pArg->out); + }else if( pArg->cMode==MODE_Www ){ + sqlite3_fputs("
    \n
    \n", pArg->out);
    +      }else if( pArg->cMode==MODE_Count ){
    +        char zBuf[200];
    +        sqlite3_snprintf(sizeof(zBuf), zBuf, "%llu row%s\n",
    +                         nRow, nRow!=1 ? "s" : "");
    +        printf("%s", zBuf);
    +      }
    +    }
    +  }
    +}
    +
    +#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_AUTHORIZATION)
    +/*
    +** This function is called to process SQL if the previous shell command
    +** was ".expert". It passes the SQL in the second argument directly to
    +** the sqlite3expert object.
    +**
    +** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
    +** code. In this case, (*pzErr) may be set to point to a buffer containing
    +** an English language error message. It is the responsibility of the
    +** caller to eventually free this buffer using sqlite3_free().
    +*/
    +static int expertHandleSQL(
    +  ShellState *pState,
    +  const char *zSql,
    +  char **pzErr
    +){
    +  assert( pState->expert.pExpert );
    +  assert( pzErr==0 || *pzErr==0 );
    +  return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
    +}
    +
    +/*
    +** This function is called either to silently clean up the object
    +** created by the ".expert" command (if bCancel==1), or to generate a
    +** report from it and then clean it up (if bCancel==0).
    +**
    +** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
    +** code. In this case, (*pzErr) may be set to point to a buffer containing
    +** an English language error message. It is the responsibility of the
    +** caller to eventually free this buffer using sqlite3_free().
    +*/
    +static int expertFinish(
    +  ShellState *pState,
    +  int bCancel,
    +  char **pzErr
    +){
    +  int rc = SQLITE_OK;
    +  sqlite3expert *p = pState->expert.pExpert;
    +  FILE *out = pState->out;
    +  assert( p );
    +  assert( bCancel || pzErr==0 || *pzErr==0 );
    +  if( bCancel==0 ){
    +    int bVerbose = pState->expert.bVerbose;
    +
    +    rc = sqlite3_expert_analyze(p, pzErr);
    +    if( rc==SQLITE_OK ){
    +      int nQuery = sqlite3_expert_count(p);
    +      int i;
    +
    +      if( bVerbose ){
    +        const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
    +        sqlite3_fputs("-- Candidates -----------------------------\n", out);
    +        sqlite3_fprintf(out, "%s\n", zCand);
    +      }
    +      for(i=0; iexpert.pExpert = 0;
    +  return rc;
    +}
    +
    +/*
    +** Implementation of ".expert" dot command.
    +*/
    +static int expertDotCommand(
    +  ShellState *pState,             /* Current shell tool state */
    +  char **azArg,                   /* Array of arguments passed to dot command */
    +  int nArg                        /* Number of entries in azArg[] */
    +){
    +  int rc = SQLITE_OK;
    +  char *zErr = 0;
    +  int i;
    +  int iSample = 0;
    +
    +  assert( pState->expert.pExpert==0 );
    +  memset(&pState->expert, 0, sizeof(ExpertInfo));
    +
    +  for(i=1; rc==SQLITE_OK && i=2 && 0==cli_strncmp(z, "-verbose", n) ){
    +      pState->expert.bVerbose = 1;
    +    }
    +    else if( n>=2 && 0==cli_strncmp(z, "-sample", n) ){
    +      if( i==(nArg-1) ){
    +        sqlite3_fprintf(stderr, "option requires an argument: %s\n", z);
    +        rc = SQLITE_ERROR;
    +      }else{
    +        iSample = (int)integerValue(azArg[++i]);
    +        if( iSample<0 || iSample>100 ){
    +          sqlite3_fprintf(stderr,"value out of range: %s\n", azArg[i]);
    +          rc = SQLITE_ERROR;
    +        }
    +      }
    +    }
    +    else{
    +      sqlite3_fprintf(stderr,"unknown option: %s\n", z);
    +      rc = SQLITE_ERROR;
    +    }
    +  }
    +
    +  if( rc==SQLITE_OK ){
    +    pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
    +    if( pState->expert.pExpert==0 ){
    +      sqlite3_fprintf(stderr,
    +          "sqlite3_expert_new: %s\n", zErr ? zErr : "out of memory");
    +      rc = SQLITE_ERROR;
    +    }else{
    +      sqlite3_expert_config(
    +          pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
    +      );
    +    }
    +  }
    +  sqlite3_free(zErr);
    +
    +  return rc;
    +}
    +#endif /* !SQLITE_OMIT_VIRTUALTABLE && !SQLITE_OMIT_AUTHORIZATION */
    +
    +/*
    +** Execute a statement or set of statements.  Print
    +** any result rows/columns depending on the current mode
    +** set via the supplied callback.
    +**
    +** This is very similar to SQLite's built-in sqlite3_exec()
    +** function except it takes a slightly different callback
    +** and callback data argument.
    +*/
    +static int shell_exec(
    +  ShellState *pArg,                         /* Pointer to ShellState */
    +  const char *zSql,                         /* SQL to be evaluated */
    +  char **pzErrMsg                           /* Error msg written here */
    +){
    +  sqlite3_stmt *pStmt = NULL;     /* Statement to execute. */
    +  int rc = SQLITE_OK;             /* Return Code */
    +  int rc2;
    +  const char *zLeftover;          /* Tail of unprocessed SQL */
    +  sqlite3 *db = pArg->db;
    +
    +  if( pzErrMsg ){
    +    *pzErrMsg = NULL;
    +  }
    +
    +#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_AUTHORIZATION)
    +  if( pArg->expert.pExpert ){
    +    rc = expertHandleSQL(pArg, zSql, pzErrMsg);
    +    return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
    +  }
    +#endif
    +
    +  while( zSql[0] && (SQLITE_OK == rc) ){
    +    static const char *zStmtSql;
    +    rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
    +    if( SQLITE_OK != rc ){
    +      if( pzErrMsg ){
    +        *pzErrMsg = save_err_msg(db, "in prepare", rc, zSql);
    +      }
    +    }else{
    +      if( !pStmt ){
    +        /* this happens for a comment or white-space */
    +        zSql = zLeftover;
    +        while( IsSpace(zSql[0]) ) zSql++;
    +        continue;
    +      }
    +      zStmtSql = sqlite3_sql(pStmt);
    +      if( zStmtSql==0 ) zStmtSql = "";
    +      while( IsSpace(zStmtSql[0]) ) zStmtSql++;
    +
    +      /* save off the prepared statement handle and reset row count */
    +      if( pArg ){
    +        pArg->pStmt = pStmt;
    +        pArg->cnt = 0;
    +      }
    +
    +      /* Show the EXPLAIN QUERY PLAN if .eqp is on */
    +      if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){
    +        sqlite3_stmt *pExplain;
    +        int triggerEQP = 0;
    +        disable_debug_trace_modes();
    +        sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
    +        if( pArg->autoEQP>=AUTOEQP_trigger ){
    +          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
    +        }
    +        pExplain = pStmt;
    +        sqlite3_reset(pExplain);
    +        rc = sqlite3_stmt_explain(pExplain, 2);
    +        if( rc==SQLITE_OK ){
    +          bind_prepared_stmt(pArg, pExplain);
    +          while( sqlite3_step(pExplain)==SQLITE_ROW ){
    +            const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
    +            int iEqpId = sqlite3_column_int(pExplain, 0);
    +            int iParentId = sqlite3_column_int(pExplain, 1);
    +            if( zEQPLine==0 ) zEQPLine = "";
    +            if( zEQPLine[0]=='-' ) eqp_render(pArg, 0);
    +            eqp_append(pArg, iEqpId, iParentId, zEQPLine);
    +          }
    +          eqp_render(pArg, 0);
    +        }
    +        if( pArg->autoEQP>=AUTOEQP_full ){
    +          /* Also do an EXPLAIN for ".eqp full" mode */
    +          sqlite3_reset(pExplain);
    +          rc = sqlite3_stmt_explain(pExplain, 1);
    +          if( rc==SQLITE_OK ){
    +            pArg->cMode = MODE_Explain;
    +            assert( sqlite3_stmt_isexplain(pExplain)==1 );
    +            bind_prepared_stmt(pArg, pExplain);
    +            explain_data_prepare(pArg, pExplain);
    +            exec_prepared_stmt(pArg, pExplain);
    +            explain_data_delete(pArg);
    +          }
    +        }
    +        if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
    +          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
    +        }
    +        sqlite3_reset(pStmt);
    +        sqlite3_stmt_explain(pStmt, 0);
    +        restore_debug_trace_modes();
    +      }
    +
    +      if( pArg ){
    +        int bIsExplain = (sqlite3_stmt_isexplain(pStmt)==1);
    +        pArg->cMode = pArg->mode;
    +        if( pArg->autoExplain ){
    +          if( bIsExplain ){
    +            pArg->cMode = MODE_Explain;
    +          }
    +          if( sqlite3_stmt_isexplain(pStmt)==2 ){
    +            pArg->cMode = MODE_EQP;
    +          }
    +        }
    +
    +        /* If the shell is currently in ".explain" mode, gather the extra
    +        ** data required to add indents to the output.*/
    +        if( pArg->cMode==MODE_Explain && bIsExplain ){
    +          explain_data_prepare(pArg, pStmt);
    +        }
    +      }
    +
    +      bind_prepared_stmt(pArg, pStmt);
    +      exec_prepared_stmt(pArg, pStmt);
    +      explain_data_delete(pArg);
    +      eqp_render(pArg, 0);
    +
    +      /* print usage stats if stats on */
    +      if( pArg && pArg->statsOn ){
    +        display_stats(db, pArg, 0);
    +      }
    +
    +      /* print loop-counters if required */
    +      if( pArg && pArg->scanstatsOn ){
    +        display_scanstats(db, pArg);
    +      }
    +
    +      /* Finalize the statement just executed. If this fails, save a
    +      ** copy of the error message. Otherwise, set zSql to point to the
    +      ** next statement to execute. */
    +      rc2 = sqlite3_finalize(pStmt);
    +      if( rc!=SQLITE_NOMEM ) rc = rc2;
    +      if( rc==SQLITE_OK ){
    +        zSql = zLeftover;
    +        while( IsSpace(zSql[0]) ) zSql++;
    +      }else if( pzErrMsg ){
    +        *pzErrMsg = save_err_msg(db, "stepping", rc, 0);
    +      }
    +
    +      /* clear saved stmt handle */
    +      if( pArg ){
    +        pArg->pStmt = NULL;
    +      }
    +    }
    +  } /* end while */
    +
    +  return rc;
    +}
    +
    +/*
    +** Release memory previously allocated by tableColumnList().
    +*/
    +static void freeColumnList(char **azCol){
    +  int i;
    +  for(i=1; azCol[i]; i++){
    +    sqlite3_free(azCol[i]);
    +  }
    +  /* azCol[0] is a static string */
    +  sqlite3_free(azCol);
    +}
    +
    +/*
    +** Return a list of pointers to strings which are the names of all
    +** columns in table zTab.   The memory to hold the names is dynamically
    +** allocated and must be released by the caller using a subsequent call
    +** to freeColumnList().
    +**
    +** The azCol[0] entry is usually NULL.  However, if zTab contains a rowid
    +** value that needs to be preserved, then azCol[0] is filled in with the
    +** name of the rowid column.
    +**
    +** The first regular column in the table is azCol[1].  The list is terminated
    +** by an entry with azCol[i]==0.
    +*/
    +static char **tableColumnList(ShellState *p, const char *zTab){
    +  char **azCol = 0;
    +  sqlite3_stmt *pStmt;
    +  char *zSql;
    +  int nCol = 0;
    +  i64 nAlloc = 0;
    +  int nPK = 0;       /* Number of PRIMARY KEY columns seen */
    +  int isIPK = 0;     /* True if one PRIMARY KEY column of type INTEGER */
    +  int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
    +  int rc;
    +
    +  zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
    +  shell_check_oom(zSql);
    +  rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
    +  sqlite3_free(zSql);
    +  if( rc ) return 0;
    +  while( sqlite3_step(pStmt)==SQLITE_ROW ){
    +    if( nCol>=nAlloc-2 ){
    +      nAlloc = nAlloc*2 + nCol + 10;
    +      azCol = sqlite3_realloc64(azCol, nAlloc*sizeof(azCol[0]));
    +      shell_check_oom(azCol);
    +    }
    +    azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
    +    shell_check_oom(azCol[nCol]);
    +    if( sqlite3_column_int(pStmt, 5) ){
    +      nPK++;
    +      if( nPK==1
    +       && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
    +                          "INTEGER")==0
    +      ){
    +        isIPK = 1;
    +      }else{
    +        isIPK = 0;
    +      }
    +    }
    +  }
    +  sqlite3_finalize(pStmt);
    +  if( azCol==0 ) return 0;
    +  azCol[0] = 0;
    +  azCol[nCol+1] = 0;
    +
    +  /* The decision of whether or not a rowid really needs to be preserved
    +  ** is tricky.  We never need to preserve a rowid for a WITHOUT ROWID table
    +  ** or a table with an INTEGER PRIMARY KEY.  We are unable to preserve
    +  ** rowids on tables where the rowid is inaccessible because there are other
    +  ** columns in the table named "rowid", "_rowid_", and "oid".
    +  */
    +  if( preserveRowid && isIPK ){
    +    /* If a single PRIMARY KEY column with type INTEGER was seen, then it
    +    ** might be an alias for the ROWID.  But it might also be a WITHOUT ROWID
    +    ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
    +    ** ROWID aliases.  To distinguish these cases, check to see if
    +    ** there is a "pk" entry in "PRAGMA index_list".  There will be
    +    ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
    +    */
    +    zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
    +                           " WHERE origin='pk'", zTab);
    +    shell_check_oom(zSql);
    +    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
    +    sqlite3_free(zSql);
    +    if( rc ){
    +      freeColumnList(azCol);
    +      return 0;
    +    }
    +    rc = sqlite3_step(pStmt);
    +    sqlite3_finalize(pStmt);
    +    preserveRowid = rc==SQLITE_ROW;
    +  }
    +  if( preserveRowid ){
    +    /* Only preserve the rowid if we can find a name to use for the
    +    ** rowid */
    +    static char *azRowid[] = { "rowid", "_rowid_", "oid" };
    +    int i, j;
    +    for(j=0; j<3; j++){
    +      for(i=1; i<=nCol; i++){
    +        if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
    +      }
    +      if( i>nCol ){
    +        /* At this point, we know that azRowid[j] is not the name of any
    +        ** ordinary column in the table.  Verify that azRowid[j] is a valid
    +        ** name for the rowid before adding it to azCol[0].  WITHOUT ROWID
    +        ** tables will fail this last check */
    +        rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
    +        if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
    +        break;
    +      }
    +    }
    +  }
    +  return azCol;
    +}
    +
    +/*
    +** Toggle the reverse_unordered_selects setting.
    +*/
    +static void toggleSelectOrder(sqlite3 *db){
    +  sqlite3_stmt *pStmt = 0;
    +  int iSetting = 0;
    +  char zStmt[100];
    +  sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
    +  if( sqlite3_step(pStmt)==SQLITE_ROW ){
    +    iSetting = sqlite3_column_int(pStmt, 0);
    +  }
    +  sqlite3_finalize(pStmt);
    +  sqlite3_snprintf(sizeof(zStmt), zStmt,
    +       "PRAGMA reverse_unordered_selects(%d)", !iSetting);
    +  sqlite3_exec(db, zStmt, 0, 0, 0);
    +}
    +
    +/* Forward reference */
    +static int db_int(sqlite3 *db, const char *zSql, ...);
    +
    +/*
    +** This is a different callback routine used for dumping the database.
    +** Each row received by this callback consists of a table name,
    +** the table type ("index" or "table") and SQL to create the table.
    +** This routine should print text sufficient to recreate the table.
    +*/
    +static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
    +  int rc;
    +  const char *zTable;
    +  const char *zType;
    +  const char *zSql;
    +  ShellState *p = (ShellState *)pArg;
    +  int dataOnly;
    +  int noSys;
    +
    +  UNUSED_PARAMETER(azNotUsed);
    +  if( nArg!=3 || azArg==0 ) return 0;
    +  zTable = azArg[0];
    +  zType = azArg[1];
    +  zSql = azArg[2];
    +  if( zTable==0 ) return 0;
    +  if( zType==0 ) return 0;
    +  dataOnly = (p->shellFlgs & SHFLG_DumpDataOnly)!=0;
    +  noSys    = (p->shellFlgs & SHFLG_DumpNoSys)!=0;
    +
    +  if( cli_strcmp(zTable, "sqlite_sequence")==0 && !noSys ){
    +    /* The sqlite_sequence table is repopulated last.  Delete content
    +    ** in the sqlite_sequence table added by prior repopulations prior to
    +    ** repopulating sqlite_sequence itself.  But only do this if the
    +    ** table is non-empty, because if it is empty the table might not
    +    ** have been recreated by prior repopulations. See forum posts:
    +    ** 2024-10-13T17:10:01z and 2025-10-29T19:38:43z
    +    */
    +    if( db_int(p->db, "SELECT count(*) FROM sqlite_sequence")>0 ){
    +      if( !p->writableSchema ){
    +        sqlite3_fputs("PRAGMA writable_schema=ON;\n", p->out);
    +        p->writableSchema = 1;
    +      }
    +      sqlite3_fputs("CREATE TABLE IF NOT EXISTS sqlite_sequence(name,seq);\n"
    +                    "DELETE FROM sqlite_sequence;\n", p->out);
    +    }
    +  }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 && !noSys ){
    +    if( !dataOnly ) sqlite3_fputs("ANALYZE sqlite_schema;\n", p->out);
    +  }else if( cli_strncmp(zTable, "sqlite_", 7)==0 ){
    +    return 0;
    +  }else if( dataOnly ){
    +    /* no-op */
    +  }else if( cli_strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
    +    char *zIns;
    +    if( !p->writableSchema ){
    +      sqlite3_fputs("PRAGMA writable_schema=ON;\n", p->out);
    +      p->writableSchema = 1;
    +    }
    +    zIns = sqlite3_mprintf(
    +       "INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)"
    +       "VALUES('table','%q','%q',0,'%q');",
    +       zTable, zTable, zSql);
    +    shell_check_oom(zIns);
    +    sqlite3_fprintf(p->out, "%s\n", zIns);
    +    sqlite3_free(zIns);
    +    return 0;
    +  }else{
    +    printSchemaLine(p->out, zSql, ";\n");
    +  }
    +
    +  if( cli_strcmp(zType, "table")==0 ){
    +    ShellText sSelect;
    +    ShellText sTable;
    +    char **azCol;
    +    int i;
    +    char *savedDestTable;
    +    int savedMode;
    +
    +    azCol = tableColumnList(p, zTable);
    +    if( azCol==0 ){
    +      p->nErr++;
    +      return 0;
    +    }
    +
    +    /* Always quote the table name, even if it appears to be pure ascii,
    +    ** in case it is a keyword. Ex:  INSERT INTO "table" ... */
    +    initText(&sTable);
    +    appendText(&sTable, zTable, quoteChar(zTable));
    +    /* If preserving the rowid, add a column list after the table name.
    +    ** In other words:  "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
    +    ** instead of the usual "INSERT INTO tab VALUES(...)".
    +    */
    +    if( azCol[0] ){
    +      appendText(&sTable, "(", 0);
    +      appendText(&sTable, azCol[0], 0);
    +      for(i=1; azCol[i]; i++){
    +        appendText(&sTable, ",", 0);
    +        appendText(&sTable, azCol[i], quoteChar(azCol[i]));
    +      }
    +      appendText(&sTable, ")", 0);
    +    }
    +
    +    /* Build an appropriate SELECT statement */
    +    initText(&sSelect);
    +    appendText(&sSelect, "SELECT ", 0);
    +    if( azCol[0] ){
    +      appendText(&sSelect, azCol[0], 0);
    +      appendText(&sSelect, ",", 0);
    +    }
    +    for(i=1; azCol[i]; i++){
    +      appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
    +      if( azCol[i+1] ){
    +        appendText(&sSelect, ",", 0);
    +      }
    +    }
    +    freeColumnList(azCol);
    +    appendText(&sSelect, " FROM ", 0);
    +    appendText(&sSelect, zTable, quoteChar(zTable));
    +
    +    savedDestTable = p->zDestTable;
    +    savedMode = p->mode;
    +    p->zDestTable = sTable.zTxt;
    +    p->mode = p->cMode = MODE_Insert;
    +    rc = shell_exec(p, sSelect.zTxt, 0);
    +    if( (rc&0xff)==SQLITE_CORRUPT ){
    +      sqlite3_fputs("/****** CORRUPTION ERROR *******/\n", p->out);
    +      toggleSelectOrder(p->db);
    +      shell_exec(p, sSelect.zTxt, 0);
    +      toggleSelectOrder(p->db);
    +    }
    +    p->zDestTable = savedDestTable;
    +    p->mode = savedMode;
    +    freeText(&sTable);
    +    freeText(&sSelect);
    +    if( rc ) p->nErr++;
    +  }
    +  return 0;
    +}
    +
    +/*
    +** Run zQuery.  Use dump_callback() as the callback routine so that
    +** the contents of the query are output as SQL statements.
    +**
    +** If we get a SQLITE_CORRUPT error, rerun the query after appending
    +** "ORDER BY rowid DESC" to the end.
    +*/
    +static int run_schema_dump_query(
    +  ShellState *p,
    +  const char *zQuery
    +){
    +  int rc;
    +  char *zErr = 0;
    +  rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
    +  if( rc==SQLITE_CORRUPT ){
    +    char *zQ2;
    +    int len = strlen30(zQuery);
    +    sqlite3_fputs("/****** CORRUPTION ERROR *******/\n", p->out);
    +    if( zErr ){
    +      sqlite3_fprintf(p->out, "/****** %s ******/\n", zErr);
    +      sqlite3_free(zErr);
    +      zErr = 0;
    +    }
    +    zQ2 = malloc( len+100 );
    +    if( zQ2==0 ) return rc;
    +    sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
    +    rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
    +    if( rc ){
    +      sqlite3_fprintf(p->out, "/****** ERROR: %s ******/\n", zErr);
    +    }else{
    +      rc = SQLITE_CORRUPT;
    +    }
    +    free(zQ2);
    +  }
    +  sqlite3_free(zErr);
    +  return rc;
    +}
    +
    +/*
    +** Text of help messages.
    +**
    +** The help text for each individual command begins with a line that starts
    +** with ".".  Subsequent lines are supplemental information.
    +**
    +** There must be two or more spaces between the end of the command and the
    +** start of the description of what that command does.
    +*/
    +static const char *(azHelp[]) = {
    +#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE) \
    +  && !defined(SQLITE_SHELL_FIDDLE)
    +  ".archive ...             Manage SQL archives",
    +  "   Each command must have exactly one of the following options:",
    +  "     -c, --create               Create a new archive",
    +  "     -u, --update               Add or update files with changed mtime",
    +  "     -i, --insert               Like -u but always add even if unchanged",
    +  "     -r, --remove               Remove files from archive",
    +  "     -t, --list                 List contents of archive",
    +  "     -x, --extract              Extract files from archive",
    +  "   Optional arguments:",
    +  "     -v, --verbose              Print each filename as it is processed",
    +  "     -f FILE, --file FILE       Use archive FILE (default is current db)",
    +  "     -a FILE, --append FILE     Open FILE using the apndvfs VFS",
    +  "     -C DIR, --directory DIR    Read/extract files from directory DIR",
    +  "     -g, --glob                 Use glob matching for names in archive",
    +  "     -n, --dryrun               Show the SQL that would have occurred",
    +  "   Examples:",
    +  "     .ar -cf ARCHIVE foo bar  # Create ARCHIVE from files foo and bar",
    +  "     .ar -tf ARCHIVE          # List members of ARCHIVE",
    +  "     .ar -xvf ARCHIVE         # Verbosely extract files from ARCHIVE",
    +  "   See also:",
    +  "      http://sqlite.org/cli.html#sqlite_archive_support",
    +#endif
    +#ifndef SQLITE_OMIT_AUTHORIZATION
    +  ".auth ON|OFF             Show authorizer callbacks",
    +#endif
    +#ifndef SQLITE_SHELL_FIDDLE
    +  ".backup ?DB? FILE        Backup DB (default \"main\") to FILE",
    +  "   Options:",
    +  "       --append            Use the appendvfs",
    +  "       --async             Write to FILE without journal and fsync()",
    +#endif
    +  ".bail on|off             Stop after hitting an error.  Default OFF",
    +#ifndef SQLITE_SHELL_FIDDLE
    +  ".cd DIRECTORY            Change the working directory to DIRECTORY",
    +#endif
    +  ".changes on|off          Show number of rows changed by SQL",
    +#ifndef SQLITE_SHELL_FIDDLE
    +  ".check GLOB              Fail if output since .testcase does not match",
    +  ".clone NEWDB             Clone data into NEWDB from the existing database",
    +#endif
    +  ".connection [close] [#]  Open or close an auxiliary database connection",
    +  ".crlf ?on|off?           Whether or not to use \\r\\n line endings",
    +  ".databases               List names and files of attached databases",
    +  ".dbconfig ?op? ?val?     List or change sqlite3_db_config() options",
    +#if SQLITE_SHELL_HAVE_RECOVER
    +  ".dbinfo ?DB?             Show status information about the database",
    +#endif
    +  ".dbtotxt                 Hex dump of the database file",
    +  ".dump ?OBJECTS?          Render database content as SQL",
    +  "   Options:",
    +  "     --data-only            Output only INSERT statements",
    +  "     --newlines             Allow unescaped newline characters in output",
    +  "     --nosys                Omit system tables (ex: \"sqlite_stat1\")",
    +  "     --preserve-rowids      Include ROWID values in the output",
    +  "   OBJECTS is a LIKE pattern for tables, indexes, triggers or views to dump",
    +  "   Additional LIKE patterns can be given in subsequent arguments",
    +  ".echo on|off             Turn command echo on or off",
    +  ".eqp on|off|full|...     Enable or disable automatic EXPLAIN QUERY PLAN",
    +  "   Other Modes:",
    +#ifdef SQLITE_DEBUG
    +  "      test                  Show raw EXPLAIN QUERY PLAN output",
    +  "      trace                 Like \"full\" but enable \"PRAGMA vdbe_trace\"",
    +#endif
    +  "      trigger               Like \"full\" but also show trigger bytecode",
    +#ifndef SQLITE_SHELL_FIDDLE
    +  ".excel                   Display the output of next command in spreadsheet",
    +  "   --bom                   Put a UTF8 byte-order mark on intermediate file",
    +#endif
    +#ifndef SQLITE_SHELL_FIDDLE
    +  ".exit ?CODE?             Exit this program with return-code CODE",
    +#endif
    +#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_AUTHORIZATION)
    +  ".expert                  EXPERIMENTAL. Suggest indexes for queries",
    +#endif
    +  ".explain ?on|off|auto?   Change the EXPLAIN formatting mode.  Default: auto",
    +  ".filectrl CMD ...        Run various sqlite3_file_control() operations",
    +  "   --schema SCHEMA         Use SCHEMA instead of \"main\"",
    +  "   --help                  Show CMD details",
    +  ".fullschema ?--indent?   Show schema and the content of sqlite_stat tables",
    +  ".headers on|off          Turn display of headers on or off",
    +  ".help ?-all? ?PATTERN?   Show help text for PATTERN",
    +#ifndef SQLITE_SHELL_FIDDLE
    +  ".import FILE TABLE       Import data from FILE into TABLE",
    +  "   Options:",
    +  "     --ascii               Use \\037 and \\036 as column and row separators",
    +  "     --csv                 Use , and \\n as column and row separators",
    +  "     --skip N              Skip the first N rows of input",
    +  "     --schema S            Target table to be S.TABLE",
    +  "     -v                    \"Verbose\" - increase auxiliary output",
    +  "   Notes:",
    +  "     *  If TABLE does not exist, it is created.  The first row of input",
    +  "        determines the column names.",
    +  "     *  If neither --csv or --ascii are used, the input mode is derived",
    +  "        from the \".mode\" output mode",
    +  "     *  If FILE begins with \"|\" then it is a command that generates the",
    +  "        input text.",
    +#endif
    +#ifndef SQLITE_OMIT_TEST_CONTROL
    +  ".imposter INDEX TABLE    Create imposter table TABLE on index INDEX",
    +#endif
    +  ".indexes ?TABLE?         Show names of indexes",
    +  "                           If TABLE is specified, only show indexes for",
    +  "                           tables matching TABLE using the LIKE operator.",
    +  ".intck ?STEPS_PER_UNLOCK?  Run an incremental integrity check on the db",
    +#ifdef SQLITE_ENABLE_IOTRACE
    +  ",iotrace FILE            Enable I/O diagnostic logging to FILE",
    +#endif
    +  ".limit ?LIMIT? ?VAL?     Display or change the value of an SQLITE_LIMIT",
    +  ".lint OPTIONS            Report potential schema issues.",
    +  "     Options:",
    +  "        fkey-indexes     Find missing foreign key indexes",
    +#if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_FIDDLE)
    +  ".load FILE ?ENTRY?       Load an extension library",
    +#endif
    +#if !defined(SQLITE_SHELL_FIDDLE)
    +  ".log FILE|on|off         Turn logging on or off.  FILE can be stderr/stdout",
    +#else
    +  ".log on|off              Turn logging on or off.",
    +#endif
    +  ".mode ?MODE? ?OPTIONS?   Set output mode",
    +  "   MODE is one of:",
    +  "     ascii       Columns/rows delimited by 0x1F and 0x1E",
    +  "     box         Tables using unicode box-drawing characters",
    +  "     csv         Comma-separated values",
    +  "     column      Output in columns.  (See .width)",
    +  "     html        HTML  code",
    +  "     insert      SQL insert statements for TABLE",
    +  "     json        Results in a JSON array",
    +  "     line        One value per line",
    +  "     list        Values delimited by \"|\"",
    +  "     markdown    Markdown table format",
    +  "     qbox        Shorthand for \"box --wrap 60 --quote\"",
    +  "     quote       Escape answers as for SQL",
    +  "     table       ASCII-art table",
    +  "     tabs        Tab-separated values",
    +  "     tcl         TCL list elements",
    +  "   OPTIONS: (for columnar modes or insert mode):",
    +  "     --escape T     ctrl-char escape; T is one of: symbol, ascii, off",
    +  "     --wrap N       Wrap output lines to no longer than N characters",
    +  "     --wordwrap B   Wrap or not at word boundaries per B (on/off)",
    +  "     --ww           Shorthand for \"--wordwrap 1\"",
    +  "     --quote        Quote output text as SQL literals",
    +  "     --noquote      Do not quote output text",
    +  "     TABLE          The name of SQL table used for \"insert\" mode",
    +#ifndef SQLITE_SHELL_FIDDLE
    +  ".nonce STRING            Suspend safe mode for one command if nonce matches",
    +#endif
    +  ".nullvalue STRING        Use STRING in place of NULL values",
    +#ifndef SQLITE_SHELL_FIDDLE
    +  ".once ?OPTIONS? ?FILE?   Output for the next SQL command only to FILE",
    +  "     If FILE begins with '|' then open as a pipe",
    +  "       --bom    Put a UTF8 byte-order mark at the beginning",
    +  "       -e       Send output to the system text editor",
    +  "       --plain  Use text/plain output instead of HTML for -w option",
    +  "       -w       Send output as HTML to a web browser (same as \".www\")",
    +  "       -x       Send output as CSV to a spreadsheet (same as \".excel\")",
    +  /* Note that .open is (partially) available in WASM builds but is
    +  ** currently only intended to be used by the fiddle tool, not
    +  ** end users, so is "undocumented." */
    +  ".open ?OPTIONS? ?FILE?   Close existing database and reopen FILE",
    +  "     Options:",
    +  "        --append        Use appendvfs to append database to the end of FILE",
    +#endif
    +#ifndef SQLITE_OMIT_DESERIALIZE
    +  "        --deserialize   Load into memory using sqlite3_deserialize()",
    +#endif
    +/*"        --exclusive     Set the SQLITE_OPEN_EXCLUSIVE flag", UNDOCUMENTED */
    +#ifndef SQLITE_OMIT_DESERIALIZE
    +  "        --hexdb         Load the output of \"dbtotxt\" as an in-memory db",
    +#endif
    +  "        --ifexist       Only open if FILE already exists",
    +#ifndef SQLITE_OMIT_DESERIALIZE
    +  "        --maxsize N     Maximum size for --hexdb or --deserialized database",
    +#endif
    +  "        --new           Initialize FILE to an empty database",
    +  "        --normal        FILE is an ordinary SQLite database",
    +  "        --nofollow      Do not follow symbolic links",
    +  "        --readonly      Open FILE readonly",
    +  "        --zip           FILE is a ZIP archive",
    +#ifndef SQLITE_SHELL_FIDDLE
    +  ".output ?FILE?           Send output to FILE or stdout if FILE is omitted",
    +  "   If FILE begins with '|' then open it as a pipe.",
    +  "   If FILE is 'off' then output is disabled.",
    +  "   Options:",
    +  "     --bom                 Prefix output with a UTF8 byte-order mark",
    +  "     -e                    Send output to the system text editor",
    +  "     --plain               Use text/plain for -w option",
    +  "     -w                    Send output to a web browser",
    +  "     -x                    Send output as CSV to a spreadsheet",
    +#endif
    +  ".parameter CMD ...       Manage SQL parameter bindings",
    +  "   clear                   Erase all bindings",
    +  "   init                    Initialize the TEMP table that holds bindings",
    +  "   list                    List the current parameter bindings",
    +  "   set PARAMETER VALUE     Given SQL parameter PARAMETER a value of VALUE",
    +  "                           PARAMETER should start with one of: $ : @ ?",
    +  "   unset PARAMETER         Remove PARAMETER from the binding table",
    +  ".print STRING...         Print literal STRING",
    +#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
    +  ".progress N              Invoke progress handler after every N opcodes",
    +  "   --limit N                 Interrupt after N progress callbacks",
    +  "   --once                    Do no more than one progress interrupt",
    +  "   --quiet|-q                No output except at interrupts",
    +  "   --reset                   Reset the count for each input and interrupt",
    +#endif
    +  ".prompt MAIN CONTINUE    Replace the standard prompts",
    +#ifndef SQLITE_SHELL_FIDDLE
    +  ".quit                    Stop interpreting input stream, exit if primary.",
    +  ".read FILE               Read input from FILE or command output",
    +  "    If FILE begins with \"|\", it is a command that generates the input.",
    +#endif
    +#if SQLITE_SHELL_HAVE_RECOVER
    +  ".recover                 Recover as much data as possible from corrupt db.",
    +  "   --ignore-freelist        Ignore pages that appear to be on db freelist",
    +  "   --lost-and-found TABLE   Alternative name for the lost-and-found table",
    +  "   --no-rowids              Do not attempt to recover rowid values",
    +  "                            that are not also INTEGER PRIMARY KEYs",
    +#endif
    +#ifndef SQLITE_SHELL_FIDDLE
    +  ".restore ?DB? FILE       Restore content of DB (default \"main\") from FILE",
    +  ".save ?OPTIONS? FILE     Write database to FILE (an alias for .backup ...)",
    +#endif
    +  ".scanstats on|off|est    Turn sqlite3_stmt_scanstatus() metrics on or off",
    +  ".schema ?PATTERN?        Show the CREATE statements matching PATTERN",
    +  "   Options:",
    +  "      --indent             Try to pretty-print the schema",
    +  "      --nosys              Omit objects whose names start with \"sqlite_\"",
    +  ",selftest ?OPTIONS?      Run tests defined in the SELFTEST table",
    +  "    Options:",
    +  "       --init               Create a new SELFTEST table",
    +  "       -v                   Verbose output",
    +  ".separator COL ?ROW?     Change the column and row separators",
    +#if defined(SQLITE_ENABLE_SESSION)
    +  ".session ?NAME? CMD ...  Create or control sessions",
    +  "   Subcommands:",
    +  "     attach TABLE             Attach TABLE",
    +  "     changeset FILE           Write a changeset into FILE",
    +  "     close                    Close one session",
    +  "     enable ?BOOLEAN?         Set or query the enable bit",
    +  "     filter GLOB...           Reject tables matching GLOBs",
    +  "     indirect ?BOOLEAN?       Mark or query the indirect status",
    +  "     isempty                  Query whether the session is empty",
    +  "     list                     List currently open session names",
    +  "     open DB NAME             Open a new session on DB",
    +  "     patchset FILE            Write a patchset into FILE",
    +  "   If ?NAME? is omitted, the first defined session is used.",
    +#endif
    +  ".sha3sum ...             Compute a SHA3 hash of database content",
    +  "    Options:",
    +  "      --schema              Also hash the sqlite_schema table",
    +  "      --sha3-224            Use the sha3-224 algorithm",
    +  "      --sha3-256            Use the sha3-256 algorithm (default)",
    +  "      --sha3-384            Use the sha3-384 algorithm",
    +  "      --sha3-512            Use the sha3-512 algorithm",
    +  "    Any other argument is a LIKE pattern for tables to hash",
    +#if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
    +  ".shell CMD ARGS...       Run CMD ARGS... in a system shell",
    +#endif
    +  ".show                    Show the current values for various settings",
    +  ".stats ?ARG?             Show stats or turn stats on or off",
    +  "   off                      Turn off automatic stat display",
    +  "   on                       Turn on automatic stat display",
    +  "   stmt                     Show statement stats",
    +  "   vmstep                   Show the virtual machine step count only",
    +#if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
    +  ".system CMD ARGS...      Run CMD ARGS... in a system shell",
    +#endif
    +  ".tables ?TABLE?          List names of tables matching LIKE pattern TABLE",
    +#ifndef SQLITE_SHELL_FIDDLE
    +  ",testcase NAME           Begin redirecting output to 'testcase-out.txt'",
    +#endif
    +  ",testctrl CMD ...        Run various sqlite3_test_control() operations",
    +  "                           Run \".testctrl\" with no arguments for details",
    +  ".timeout MS              Try opening locked tables for MS milliseconds",
    +  ".timer on|off            Turn SQL timer on or off",
    +#ifndef SQLITE_OMIT_TRACE
    +  ".trace ?OPTIONS?         Output each SQL statement as it is run",
    +  "    FILE                    Send output to FILE",
    +  "    stdout                  Send output to stdout",
    +  "    stderr                  Send output to stderr",
    +  "    off                     Disable tracing",
    +  "    --expanded              Expand query parameters",
    +#ifdef SQLITE_ENABLE_NORMALIZE
    +  "    --normalized            Normal the SQL statements",
    +#endif
    +  "    --plain                 Show SQL as it is input",
    +  "    --stmt                  Trace statement execution (SQLITE_TRACE_STMT)",
    +  "    --profile               Profile statements (SQLITE_TRACE_PROFILE)",
    +  "    --row                   Trace each row (SQLITE_TRACE_ROW)",
    +  "    --close                 Trace connection close (SQLITE_TRACE_CLOSE)",
    +#endif /* SQLITE_OMIT_TRACE */
    +#ifdef SQLITE_DEBUG
    +  ".unmodule NAME ...       Unregister virtual table modules",
    +  "    --allexcept             Unregister everything except those named",
    +#endif
    +  ".version                 Show source, library and compiler versions",
    +  ".vfsinfo ?AUX?           Information about the top-level VFS",
    +  ".vfslist                 List all available VFSes",
    +  ".vfsname ?AUX?           Print the name of the VFS stack",
    +  ".width NUM1 NUM2 ...     Set minimum column widths for columnar output",
    +  "     Negative values right-justify",
    +#ifndef SQLITE_SHELL_FIDDLE
    +  ".www                     Display output of the next command in web browser",
    +  "    --plain                 Show results as text/plain, not as HTML",
    +#endif
    +};
    +
    +/*
    +** Output help text for commands that match zPattern.
    +**
    +**    *   If zPattern is NULL, then show all documented commands, but
    +**        only give a one-line summary of each.
    +**
    +**    *   If zPattern is "-a" or "-all" or "--all" then show all help text
    +**        for all commands except undocumented commands.
    +**
    +**    *   If zPattern is "0" then show all help for undocumented commands.
    +**        Undocumented commands begin with "," instead of "." in the azHelp[]
    +**        array.
    +**
    +**    *   If zPattern is a prefix for one or more documented commands, then
    +**        show help for those commands.  If only a single command matches the
    +**        prefix, show the full text of the help.  If multiple commands match,
    +**        Only show just the first line of each.
    +**
    +**    *   Otherwise, show the complete text of any documented command for which
    +**        zPattern is a LIKE match for any text within that command help
    +**        text.
    +**
    +** Return the number commands that match zPattern.
    +*/
    +static int showHelp(FILE *out, const char *zPattern){
    +  int i = 0;
    +  int j = 0;
    +  int n = 0;
    +  char *zPat;
    +  if( zPattern==0 ){
    +    /* Show just the first line for all help topics */
    +    zPattern = "[a-z]";
    +  }else if( cli_strcmp(zPattern,"-a")==0
    +         || cli_strcmp(zPattern,"-all")==0
    +         || cli_strcmp(zPattern,"--all")==0
    +  ){
    +    /* Show everything except undocumented commands */
    +    zPattern = ".";
    +  }else if( cli_strcmp(zPattern,"0")==0 ){
    +    /* Show complete help text of undocumented commands */
    +    int show = 0;
    +    for(i=0; ip);
    +  sqlite3_free(pSession->zName);
    +  for(i=0; inFilter; i++){
    +    sqlite3_free(pSession->azFilter[i]);
    +  }
    +  sqlite3_free(pSession->azFilter);
    +  memset(pSession, 0, sizeof(OpenSession));
    +}
    +#endif
    +
    +/*
    +** Close all OpenSession objects and release all associated resources.
    +*/
    +#if defined(SQLITE_ENABLE_SESSION)
    +static void session_close_all(ShellState *p, int i){
    +  int j;
    +  struct AuxDb *pAuxDb = i<0 ? p->pAuxDb : &p->aAuxDb[i];
    +  for(j=0; jnSession; j++){
    +    session_close(&pAuxDb->aSession[j]);
    +  }
    +  pAuxDb->nSession = 0;
    +}
    +#else
    +# define session_close_all(X,Y)
    +#endif
    +
    +/*
    +** Implementation of the xFilter function for an open session.  Omit
    +** any tables named by ".session filter" but let all other table through.
    +*/
    +#if defined(SQLITE_ENABLE_SESSION)
    +static int session_filter(void *pCtx, const char *zTab){
    +  OpenSession *pSession = (OpenSession*)pCtx;
    +  int i;
    +  for(i=0; inFilter; i++){
    +    if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
    +  }
    +  return 1;
    +}
    +#endif
    +
    +/*
    +** Try to deduce the type of file for zName based on its content.  Return
    +** one of the SHELL_OPEN_* constants.
    +**
    +** If the file does not exist or is empty but its name looks like a ZIP
    +** archive and the dfltZip flag is true, then assume it is a ZIP archive.
    +** Otherwise, assume an ordinary database regardless of the filename if
    +** the type cannot be determined from content.
    +*/
    +int deduceDatabaseType(const char *zName, int dfltZip, int openFlags){
    +  FILE *f;
    +  size_t n;
    +  sqlite3 *db = 0;
    +  sqlite3_stmt *pStmt = 0;
    +  int rc = SHELL_OPEN_UNSPEC;
    +  char zBuf[100];
    +  if( access(zName,0)!=0 ) goto database_type_by_name;
    +  if( sqlite3_open_v2(zName, &db, openFlags, 0)==SQLITE_OK
    +   && sqlite3_prepare_v2(db,"SELECT count(*) FROM sqlite_schema",-1,&pStmt,0)
    +           ==SQLITE_OK
    +   && sqlite3_step(pStmt)==SQLITE_ROW
    +  ){
    +    rc = SHELL_OPEN_NORMAL;
    +  }
    +  sqlite3_finalize(pStmt);
    +  sqlite3_close(db);
    +  if( rc==SHELL_OPEN_NORMAL ) return SHELL_OPEN_NORMAL;
    +  f = sqlite3_fopen(zName, "rb");
    +  if( f==0 ) goto database_type_by_name;
    +  n = fread(zBuf, 16, 1, f);
    +  if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){
    +    fclose(f);
    +    return SHELL_OPEN_NORMAL;
    +  }
    +  fseek(f, -25, SEEK_END);
    +  n = fread(zBuf, 25, 1, f);
    +  if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
    +    rc = SHELL_OPEN_APPENDVFS;
    +  }else{
    +    fseek(f, -22, SEEK_END);
    +    n = fread(zBuf, 22, 1, f);
    +    if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
    +       && zBuf[3]==0x06 ){
    +      rc = SHELL_OPEN_ZIPFILE;
    +    }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
    +      rc = SHELL_OPEN_ZIPFILE;
    +    }
    +  }
    +  fclose(f);
    +  return rc;
    +
    +database_type_by_name:
    +  if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
    +    rc = SHELL_OPEN_ZIPFILE;
    +  }else{
    +    rc = SHELL_OPEN_NORMAL;
    +  }
    +  return rc;
    +}
    +
    +#ifndef SQLITE_OMIT_DESERIALIZE
    +/*
    +** Reconstruct an in-memory database using the output from the "dbtotxt"
    +** program.  Read content from the file in p->aAuxDb[].zDbFilename.
    +** If p->aAuxDb[].zDbFilename is 0, then read from standard input.
    +*/
    +static unsigned char *readHexDb(ShellState *p, int *pnData){
    +  unsigned char *a = 0;
    +  i64 nLine;
    +  int n = 0;                      /* Size of db per first line of hex dump */
    +  i64 sz = 0;                     /* n rounded up to nearest page boundary */
    +  int pgsz = 0;
    +  i64 iOffset = 0;
    +  int rc;
    +  FILE *in;
    +  const char *zDbFilename = p->pAuxDb->zDbFilename;
    +  unsigned int x[16];
    +  char zLine[1000];
    +  if( zDbFilename ){
    +    in = sqlite3_fopen(zDbFilename, "r");
    +    if( in==0 ){
    +      sqlite3_fprintf(stderr,"cannot open \"%s\" for reading\n", zDbFilename);
    +      return 0;
    +    }
    +    nLine = 0;
    +  }else{
    +    in = p->in;
    +    nLine = p->lineno;
    +    if( in==0 ) in = stdin;
    +  }
    +  *pnData = 0;
    +  nLine++;
    +  if( sqlite3_fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
    +  rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
    +  if( rc!=2 ) goto readHexDb_error;
    +  if( n<0 ) goto readHexDb_error;
    +  if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
    +    sqlite3_fputs("invalid pagesize\n", stderr);
    +    goto readHexDb_error;
    +  }
    +  sz = ((i64)n+pgsz-1)&~(pgsz-1); /* Round up to nearest multiple of pgsz */
    +  a = sqlite3_malloc64( sz ? sz : 1 );
    +  shell_check_oom(a);
    +  memset(a, 0, sz);
    +  for(nLine++; sqlite3_fgets(zLine, sizeof(zLine), in)!=0; nLine++){
    +    int j = 0;                    /* Page number from "| page" line */
    +    int k = 0;                    /* Offset from "| page" line */
    +    if( nLine>=2000000000 ){
    +      sqlite3_fprintf(stderr, "input too big\n");
    +      goto readHexDb_error;
    +    }
    +    rc = sscanf(zLine, "| page %d offset %d", &j, &k);
    +    if( rc==2 ){
    +      iOffset = k;
    +      continue;
    +    }
    +    if( cli_strncmp(zLine, "| end ", 6)==0 ){
    +      break;
    +    }
    +    rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
    +                &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
    +                &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
    +    if( rc==17 ){
    +      i64 iOff = iOffset+j;
    +      if( iOff+16<=sz && iOff>=0 ){
    +        int ii;
    +        for(ii=0; ii<16; ii++) a[iOff+ii] = x[ii]&0xff;
    +      }
    +    }
    +  }
    +  *pnData = sz;
    +  if( in!=p->in ){
    +    fclose(in);
    +  }else{
    +    p->lineno = nLine;
    +  }
    +  return a;
    +
    +readHexDb_error:
    +  if( in!=p->in ){
    +    fclose(in);
    +  }else{
    +    while( sqlite3_fgets(zLine, sizeof(zLine), p->in)!=0 ){
    +      nLine++;
    +      if(cli_strncmp(zLine, "| end ", 6)==0 ) break;
    +    }
    +    p->lineno = nLine;
    +  }
    +  sqlite3_free(a);
    +  sqlite3_fprintf(stderr,"Error on line %lld of --hexdb input\n", nLine);
    +  return 0;
    +}
    +#endif /* SQLITE_OMIT_DESERIALIZE */
    +
    +/*
    +** Scalar function "usleep(X)" invokes sqlite3_sleep(X) and returns X.
    +*/
    +static void shellUSleepFunc(
    +  sqlite3_context *context,
    +  int argcUnused,
    +  sqlite3_value **argv
    +){
    +  int sleep = sqlite3_value_int(argv[0]);
    +  (void)argcUnused;
    +  sqlite3_sleep(sleep/1000);
    +  sqlite3_result_int(context, sleep);
    +}
    +
    +/*
    +** SQL function:  shell_module_schema(X)
    +**
    +** Return a fake schema for the table-valued function or eponymous virtual
    +** table X.
    +*/
    +static void shellModuleSchema(
    +  sqlite3_context *pCtx,
    +  int nVal,
    +  sqlite3_value **apVal
    +){
    +  const char *zName;
    +  char *zFake;
    +  ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
    +  FILE *pSavedLog = p->pLog;
    +  UNUSED_PARAMETER(nVal);
    +  zName = (const char*)sqlite3_value_text(apVal[0]);
    +
    +  /* Temporarily disable the ".log" when calling shellFakeSchema() because
    +  ** shellFakeSchema() might generate failures for some ephemeral virtual
    +  ** tables due to missing arguments.  Example: fts4aux.
    +  ** https://sqlite.org/forum/forumpost/42fe6520b803be51 */
    +  p->pLog = 0;
    +  zFake = zName? shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName) : 0;
    +  p->pLog = pSavedLog;
    +
    +  if( zFake ){
    +    sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),
    +                        -1, sqlite3_free);
    +    sqlite3_free(zFake);
    +  }
    +}
    +
    +/* Flags for open_db().
    +**
    +** The default behavior of open_db() is to exit(1) if the database fails to
    +** open.  The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
    +** but still returns without calling exit.
    +**
    +** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
    +** ZIP archive if the file does not exist or is empty and its name matches
    +** the *.zip pattern.
    +*/
    +#define OPEN_DB_KEEPALIVE   0x001   /* Return after error if true */
    +#define OPEN_DB_ZIPFILE     0x002   /* Open as ZIP if name matches *.zip */
    +
    +/*
    +** Make sure the database is open.  If it is not, then open it.  If
    +** the database fails to open, print an error message and exit.
    +*/
    +static void open_db(ShellState *p, int openFlags){
    +  if( p->db==0 ){
    +    const char *zDbFilename = p->pAuxDb->zDbFilename;
    +    if( p->openMode==SHELL_OPEN_UNSPEC ){
    +      if( zDbFilename==0 || zDbFilename[0]==0 ){
    +        p->openMode = SHELL_OPEN_NORMAL;
    +      }else{
    +        p->openMode = (u8)deduceDatabaseType(zDbFilename,
    +                             (openFlags & OPEN_DB_ZIPFILE)!=0, p->openFlags);
    +      }
    +    }
    +    if( (p->openFlags & (SQLITE_OPEN_READONLY|SQLITE_OPEN_READWRITE))==0 ){
    +      if( p->openFlags==0 ) p->openFlags = SQLITE_OPEN_CREATE;
    +      p->openFlags |= SQLITE_OPEN_READWRITE;
    +    }
    +    switch( p->openMode ){
    +      case SHELL_OPEN_APPENDVFS: {
    +        sqlite3_open_v2(zDbFilename, &p->db, p->openFlags, "apndvfs");
    +        break;
    +      }
    +      case SHELL_OPEN_HEXDB:
    +      case SHELL_OPEN_DESERIALIZE: {
    +        sqlite3_open(0, &p->db);
    +        break;
    +      }
    +      case SHELL_OPEN_ZIPFILE: {
    +        sqlite3_open(":memory:", &p->db);
    +        break;
    +      }
    +      case SHELL_OPEN_UNSPEC:
    +      case SHELL_OPEN_NORMAL: {
    +        sqlite3_open_v2(zDbFilename, &p->db, p->openFlags, 0);
    +        break;
    +      }
    +    }
    +    if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
    +      sqlite3_fprintf(stderr,"Error: unable to open database \"%s\": %s\n",
    +            zDbFilename, sqlite3_errmsg(p->db));
    +      if( (openFlags & OPEN_DB_KEEPALIVE)==0 ){
    +        exit(1);
    +      }
    +      sqlite3_close(p->db);
    +      sqlite3_open(":memory:", &p->db);
    +      if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
    +        sqlite3_fputs("Also: unable to open substitute in-memory database.\n",
    +                      stderr);
    +        exit(1);
    +      }else{
    +        sqlite3_fprintf(stderr,
    +              "Notice: using substitute in-memory database instead of \"%s\"\n",
    +              zDbFilename);
    +      }
    +    }
    +    globalDb = p->db;
    +    sqlite3_db_config(p->db, SQLITE_DBCONFIG_STMT_SCANSTATUS, (int)0, (int*)0);
    +
    +    /* Reflect the use or absence of --unsafe-testing invocation. */
    +    {
    +      int testmode_on = ShellHasFlag(p,SHFLG_TestingMode);
    +      sqlite3_db_config(p->db, SQLITE_DBCONFIG_TRUSTED_SCHEMA, testmode_on,0);
    +      sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, !testmode_on,0);
    +    }
    +
    +#ifndef SQLITE_OMIT_LOAD_EXTENSION
    +    sqlite3_enable_load_extension(p->db, 1);
    +#endif
    +    sqlite3_sha_init(p->db, 0, 0);
    +    sqlite3_shathree_init(p->db, 0, 0);
    +    sqlite3_uint_init(p->db, 0, 0);
    +    sqlite3_stmtrand_init(p->db, 0, 0);
    +    sqlite3_decimal_init(p->db, 0, 0);
    +    sqlite3_base64_init(p->db, 0, 0);
    +    sqlite3_base85_init(p->db, 0, 0);
    +    sqlite3_regexp_init(p->db, 0, 0);
    +    sqlite3_ieee_init(p->db, 0, 0);
    +    sqlite3_series_init(p->db, 0, 0);
    +#ifndef SQLITE_SHELL_FIDDLE
    +    sqlite3_fileio_init(p->db, 0, 0);
    +    sqlite3_completion_init(p->db, 0, 0);
    +#endif
    +#ifdef SQLITE_HAVE_ZLIB
    +    if( !p->bSafeModePersist ){
    +      sqlite3_zipfile_init(p->db, 0, 0);
    +      sqlite3_sqlar_init(p->db, 0, 0);
    +    }
    +#endif
    +#ifdef SQLITE_SHELL_EXTFUNCS
    +    /* Create a preprocessing mechanism for extensions to make
    +     * their own provisions for being built into the shell.
    +     * This is a short-span macro. See further below for usage.
    +     */
    +#define SHELL_SUB_MACRO(base, variant) base ## _ ## variant
    +#define SHELL_SUBMACRO(base, variant) SHELL_SUB_MACRO(base, variant)
    +    /* Let custom-included extensions get their ..._init() called.
    +     * The WHATEVER_INIT( db, pzErrorMsg, pApi ) macro should cause
    +     * the extension's sqlite3_*_init( db, pzErrorMsg, pApi )
    +     * initialization routine to be called.
    +     */
    +    {
    +      int irc = SHELL_SUBMACRO(SQLITE_SHELL_EXTFUNCS, INIT)(p->db);
    +    /* Let custom-included extensions expose their functionality.
    +     * The WHATEVER_EXPOSE( db, pzErrorMsg ) macro should cause
    +     * the SQL functions, virtual tables, collating sequences or
    +     * VFS's implemented by the extension to be registered.
    +     */
    +      if( irc==SQLITE_OK
    +          || irc==SQLITE_OK_LOAD_PERMANENTLY ){
    +        SHELL_SUBMACRO(SQLITE_SHELL_EXTFUNCS, EXPOSE)(p->db, 0);
    +      }
    +#undef SHELL_SUB_MACRO
    +#undef SHELL_SUBMACRO
    +    }
    +#endif
    +
    +    sqlite3_create_function(p->db, "strtod", 1, SQLITE_UTF8, 0,
    +                            shellStrtod, 0, 0);
    +    sqlite3_create_function(p->db, "dtostr", 1, SQLITE_UTF8, 0,
    +                            shellDtostr, 0, 0);
    +    sqlite3_create_function(p->db, "dtostr", 2, SQLITE_UTF8, 0,
    +                            shellDtostr, 0, 0);
    +    sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
    +                            shellAddSchemaName, 0, 0);
    +    sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, p,
    +                            shellModuleSchema, 0, 0);
    +    sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
    +                            shellPutsFunc, 0, 0);
    +    sqlite3_create_function(p->db, "usleep",1,SQLITE_UTF8,0,
    +                            shellUSleepFunc, 0, 0);
    +#ifndef SQLITE_NOHAVE_SYSTEM
    +    sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
    +                            editFunc, 0, 0);
    +    sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
    +                            editFunc, 0, 0);
    +#endif
    +
    +    if( p->openMode==SHELL_OPEN_ZIPFILE ){
    +      char *zSql = sqlite3_mprintf(
    +         "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", zDbFilename);
    +      shell_check_oom(zSql);
    +      sqlite3_exec(p->db, zSql, 0, 0, 0);
    +      sqlite3_free(zSql);
    +    }
    +#ifndef SQLITE_OMIT_DESERIALIZE
    +    else
    +    if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){
    +      int rc;
    +      int nData = 0;
    +      unsigned char *aData;
    +      if( p->openMode==SHELL_OPEN_DESERIALIZE ){
    +        aData = (unsigned char*)readFile(zDbFilename, &nData);
    +      }else{
    +        aData = readHexDb(p, &nData);
    +      }
    +      if( aData==0 ){
    +        return;
    +      }
    +      rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
    +                   SQLITE_DESERIALIZE_RESIZEABLE |
    +                   SQLITE_DESERIALIZE_FREEONCLOSE);
    +      if( rc ){
    +        sqlite3_fprintf(stderr,"Error: sqlite3_deserialize() returns %d\n", rc);
    +      }
    +      if( p->szMax>0 ){
    +        sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
    +      }
    +    }
    +#endif
    +  }
    +  if( p->db!=0 ){
    +#ifndef SQLITE_OMIT_AUTHORIZATION
    +    if( p->bSafeModePersist ){
    +      sqlite3_set_authorizer(p->db, safeModeAuth, p);
    +    }
    +#endif
    +    sqlite3_db_config(
    +        p->db, SQLITE_DBCONFIG_STMT_SCANSTATUS, p->scanstatsOn, (int*)0
    +    );
    +  }
    +}
    +
    +/*
    +** Attempt to close the database connection.  Report errors.
    +*/
    +void close_db(sqlite3 *db){
    +  int rc = sqlite3_close(db);
    +  if( rc ){
    +    sqlite3_fprintf(stderr,
    +        "Error: sqlite3_close() returns %d: %s\n", rc, sqlite3_errmsg(db));
    +  }
    +}
    +
    +#if (HAVE_READLINE || HAVE_EDITLINE) \
    +  && !defined(SQLITE_OMIT_READLINE_COMPLETION)
    +/*
    +** Readline completion callbacks
    +*/
    +static char *readline_completion_generator(const char *text, int state){
    +  static sqlite3_stmt *pStmt = 0;
    +  char *zRet;
    +  if( state==0 ){
    +    char *zSql;
    +    sqlite3_finalize(pStmt);
    +    zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
    +                           "  FROM completion(%Q) ORDER BY 1", text);
    +    shell_check_oom(zSql);
    +    sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
    +    sqlite3_free(zSql);
    +  }
    +  if( sqlite3_step(pStmt)==SQLITE_ROW ){
    +    const char *z = (const char*)sqlite3_column_text(pStmt,0);
    +    zRet = z ? strdup(z) : 0;
    +  }else{
    +    sqlite3_finalize(pStmt);
    +    pStmt = 0;
    +    zRet = 0;
    +  }
    +  return zRet;
    +}
    +static char **readline_completion(const char *zText, int iStart, int iEnd){
    +  (void)iStart;
    +  (void)iEnd;
    +  rl_attempted_completion_over = 1;
    +  return rl_completion_matches(zText, readline_completion_generator);
    +}
    +
    +#elif HAVE_LINENOISE
    +/*
    +** Linenoise completion callback. Note that the 3rd argument is from
    +** the "msteveb" version of linenoise, not the "antirez" version.
    +*/
    +static void linenoise_completion(
    +  const char *zLine,
    +  linenoiseCompletions *lc
    +#if HAVE_LINENOISE==2
    +  ,void *pUserData
    +#endif
    +){
    +  i64 nLine = strlen(zLine);
    +  i64 i, iStart;
    +  sqlite3_stmt *pStmt = 0;
    +  char *zSql;
    +  char zBuf[1000];
    +
    +#if HAVE_LINENOISE==2
    +  UNUSED_PARAMETER(pUserData);
    +#endif
    +  if( nLine>(i64)sizeof(zBuf)-30 ) return;
    +  if( zLine[0]=='.' || zLine[0]=='#') return;
    +  for(i=nLine-1; i>=0 && (IsAlnum(zLine[i]) || zLine[i]=='_'); i--){}
    +  if( i==nLine-1 ) return;
    +  iStart = i+1;
    +  memcpy(zBuf, zLine, iStart);
    +  zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
    +                         "  FROM completion(%Q,%Q) ORDER BY 1",
    +                         &zLine[iStart], zLine);
    +  shell_check_oom(zSql);
    +  sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
    +  sqlite3_free(zSql);
    +  sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
    +  while( sqlite3_step(pStmt)==SQLITE_ROW ){
    +    const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
    +    int nCompletion = sqlite3_column_bytes(pStmt, 0);
    +    if( iStart+nCompletion < (i64)sizeof(zBuf)-1 && zCompletion ){
    +      memcpy(zBuf+iStart, zCompletion, nCompletion+1);
    +      linenoiseAddCompletion(lc, zBuf);
    +    }
    +  }
    +  sqlite3_finalize(pStmt);
    +}
    +#endif
    +
    +/*
    +** Do C-language style dequoting.
    +**
    +**    \a    -> alarm
    +**    \b    -> backspace
    +**    \t    -> tab
    +**    \n    -> newline
    +**    \v    -> vertical tab
    +**    \f    -> form feed
    +**    \r    -> carriage return
    +**    \s    -> space
    +**    \"    -> "
    +**    \'    -> '
    +**    \\    -> backslash
    +**    \NNN  -> ascii character NNN in octal
    +**    \xHH  -> ascii character HH in hexadecimal
    +*/
    +static void resolve_backslashes(char *z){
    +  int i, j;
    +  char c;
    +  while( *z && *z!='\\' ) z++;
    +  for(i=j=0; (c = z[i])!=0; i++, j++){
    +    if( c=='\\' && z[i+1]!=0 ){
    +      c = z[++i];
    +      if( c=='a' ){
    +        c = '\a';
    +      }else if( c=='b' ){
    +        c = '\b';
    +      }else if( c=='t' ){
    +        c = '\t';
    +      }else if( c=='n' ){
    +        c = '\n';
    +      }else if( c=='v' ){
    +        c = '\v';
    +      }else if( c=='f' ){
    +        c = '\f';
    +      }else if( c=='r' ){
    +        c = '\r';
    +      }else if( c=='"' ){
    +        c = '"';
    +      }else if( c=='\'' ){
    +        c = '\'';
    +      }else if( c=='\\' ){
    +        c = '\\';
    +      }else if( c=='x' ){
    +        int nhd = 0, hdv;
    +        u8 hv = 0;
    +        while( nhd<2 && (c=z[i+1+nhd])!=0 && (hdv=hexDigitValue(c))>=0 ){
    +          hv = (u8)((hv<<4)|hdv);
    +          ++nhd;
    +        }
    +        i += nhd;
    +        c = (u8)hv;
    +      }else if( c>='0' && c<='7' ){
    +        c -= '0';
    +        if( z[i+1]>='0' && z[i+1]<='7' ){
    +          i++;
    +          c = (c<<3) + z[i] - '0';
    +          if( z[i+1]>='0' && z[i+1]<='7' ){
    +            i++;
    +            c = (c<<3) + z[i] - '0';
    +          }
    +        }
    +      }
    +    }
    +    z[j] = c;
    +  }
    +  if( j=0; i++){}
    +  }else{
    +    for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
    +  }
    +  if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
    +  if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
    +    return 1;
    +  }
    +  if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
    +    return 0;
    +  }
    +  sqlite3_fprintf(stderr,
    +       "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n", zArg);
    +  return 0;
    +}
    +
    +/*
    +** Set or clear a shell flag according to a boolean value.
    +*/
    +static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
    +  if( booleanValue(zArg) ){
    +    ShellSetFlag(p, mFlag);
    +  }else{
    +    ShellClearFlag(p, mFlag);
    +  }
    +}
    +
    +/*
    +** Close an output file, assuming it is not stderr or stdout
    +*/
    +static void output_file_close(FILE *f){
    +  if( f && f!=stdout && f!=stderr ) fclose(f);
    +}
    +
    +/*
    +** Try to open an output file.   The names "stdout" and "stderr" are
    +** recognized and do the right thing.  NULL is returned if the output
    +** filename is "off".
    +*/
    +static FILE *output_file_open(const char *zFile){
    +  FILE *f;
    +  if( cli_strcmp(zFile,"stdout")==0 ){
    +    f = stdout;
    +  }else if( cli_strcmp(zFile, "stderr")==0 ){
    +    f = stderr;
    +  }else if( cli_strcmp(zFile, "off")==0 ){
    +    f = 0;
    +  }else{
    +    f = sqlite3_fopen(zFile, "w");
    +    if( f==0 ){
    +      sqlite3_fprintf(stderr,"Error: cannot open \"%s\"\n", zFile);
    +    }
    +  }
    +  return f;
    +}
    +
    +#ifndef SQLITE_OMIT_TRACE
    +/*
    +** A routine for handling output from sqlite3_trace().
    +*/
    +static int sql_trace_callback(
    +  unsigned mType,         /* The trace type */
    +  void *pArg,             /* The ShellState pointer */
    +  void *pP,               /* Usually a pointer to sqlite_stmt */
    +  void *pX                /* Auxiliary output */
    +){
    +  ShellState *p = (ShellState*)pArg;
    +  sqlite3_stmt *pStmt;
    +  const char *zSql;
    +  i64 nSql;
    +  if( p->traceOut==0 ) return 0;
    +  if( mType==SQLITE_TRACE_CLOSE ){
    +    sputz(p->traceOut, "-- closing database connection\n");
    +    return 0;
    +  }
    +  if( mType!=SQLITE_TRACE_ROW && pX!=0 && ((const char*)pX)[0]=='-' ){
    +    zSql = (const char*)pX;
    +  }else{
    +    pStmt = (sqlite3_stmt*)pP;
    +    switch( p->eTraceType ){
    +      case SHELL_TRACE_EXPANDED: {
    +        zSql = sqlite3_expanded_sql(pStmt);
    +        break;
    +      }
    +#ifdef SQLITE_ENABLE_NORMALIZE
    +      case SHELL_TRACE_NORMALIZED: {
    +        zSql = sqlite3_normalized_sql(pStmt);
    +        break;
    +      }
    +#endif
    +      default: {
    +        zSql = sqlite3_sql(pStmt);
    +        break;
    +      }
    +    }
    +  }
    +  if( zSql==0 ) return 0;
    +  nSql = strlen(zSql);
    +  if( nSql>1000000000 ) nSql = 1000000000;
    +  while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
    +  switch( mType ){
    +    case SQLITE_TRACE_ROW:
    +    case SQLITE_TRACE_STMT: {
    +      sqlite3_fprintf(p->traceOut, "%.*s;\n", (int)nSql, zSql);
    +      break;
    +    }
    +    case SQLITE_TRACE_PROFILE: {
    +      sqlite3_int64 nNanosec = pX ? *(sqlite3_int64*)pX : 0;
    +      sqlite3_fprintf(p->traceOut,
    +                      "%.*s; -- %lld ns\n", (int)nSql, zSql, nNanosec);
    +      break;
    +    }
    +  }
    +  return 0;
    +}
    +#endif
    +
    +/*
    +** A no-op routine that runs with the ".breakpoint" doc-command.  This is
    +** a useful spot to set a debugger breakpoint.
    +**
    +** This routine does not do anything practical.  The code are there simply
    +** to prevent the compiler from optimizing this routine out.
    +*/
    +static void test_breakpoint(void){
    +  static unsigned int nCall = 0;
    +  if( (nCall++)==0xffffffff ) printf("Many .breakpoints have run\n");
    +}
    +
    +/*
    +** An object used to read a CSV and other files for import.
    +*/
    +typedef struct ImportCtx ImportCtx;
    +struct ImportCtx {
    +  const char *zFile;  /* Name of the input file */
    +  FILE *in;           /* Read the CSV text from this input stream */
    +  int (SQLITE_CDECL *xCloser)(FILE*);      /* Func to close in */
    +  char *z;            /* Accumulated text for a field */
    +  i64 n;              /* Number of bytes in z */
    +  i64 nAlloc;         /* Space allocated for z[] */
    +  int nLine;          /* Current line number */
    +  int nRow;           /* Number of rows imported */
    +  int nErr;           /* Number of errors encountered */
    +  int bNotFirst;      /* True if one or more bytes already read */
    +  int cTerm;          /* Character that terminated the most recent field */
    +  int cColSep;        /* The column separator character.  (Usually ",") */
    +  int cRowSep;        /* The row separator character.  (Usually "\n") */
    +};
    +
    +/* Clean up resourced used by an ImportCtx */
    +static void import_cleanup(ImportCtx *p){
    +  if( p->in!=0 && p->xCloser!=0 ){
    +    p->xCloser(p->in);
    +    p->in = 0;
    +  }
    +  sqlite3_free(p->z);
    +  p->z = 0;
    +}
    +
    +/* Append a single byte to z[] */
    +static void import_append_char(ImportCtx *p, int c){
    +  if( p->n+1>=p->nAlloc ){
    +    p->nAlloc += p->nAlloc + 100;
    +    p->z = sqlite3_realloc64(p->z, p->nAlloc);
    +    shell_check_oom(p->z);
    +  }
    +  p->z[p->n++] = (char)c;
    +}
    +
    +/* Read a single field of CSV text.  Compatible with rfc4180 and extended
    +** with the option of having a separator other than ",".
    +**
    +**   +  Input comes from p->in.
    +**   +  Store results in p->z of length p->n.  Space to hold p->z comes
    +**      from sqlite3_malloc64().
    +**   +  Use p->cSep as the column separator.  The default is ",".
    +**   +  Use p->rSep as the row separator.  The default is "\n".
    +**   +  Keep track of the line number in p->nLine.
    +**   +  Store the character that terminates the field in p->cTerm.  Store
    +**      EOF on end-of-file.
    +**   +  Report syntax errors on stderr
    +*/
    +static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
    +  int c;
    +  int cSep = (u8)p->cColSep;
    +  int rSep = (u8)p->cRowSep;
    +  p->n = 0;
    +  c = fgetc(p->in);
    +  if( c==EOF || seenInterrupt ){
    +    p->cTerm = EOF;
    +    return 0;
    +  }
    +  if( c=='"' ){
    +    int pc, ppc;
    +    int startLine = p->nLine;
    +    int cQuote = c;
    +    pc = ppc = 0;
    +    while( 1 ){
    +      c = fgetc(p->in);
    +      if( c==rSep ) p->nLine++;
    +      if( c==cQuote ){
    +        if( pc==cQuote ){
    +          pc = 0;
    +          continue;
    +        }
    +      }
    +      if( (c==cSep && pc==cQuote)
    +       || (c==rSep && pc==cQuote)
    +       || (c==rSep && pc=='\r' && ppc==cQuote)
    +       || (c==EOF && pc==cQuote)
    +      ){
    +        do{ p->n--; }while( p->z[p->n]!=cQuote );
    +        p->cTerm = c;
    +        break;
    +      }
    +      if( pc==cQuote && c!='\r' ){
    +        sqlite3_fprintf(stderr,"%s:%d: unescaped %c character\n", 
    +                        p->zFile, p->nLine, cQuote);
    +      }
    +      if( c==EOF ){
    +        sqlite3_fprintf(stderr,"%s:%d: unterminated %c-quoted field\n",
    +              p->zFile, startLine, cQuote);
    +        p->cTerm = c;
    +        break;
    +      }
    +      import_append_char(p, c);
    +      ppc = pc;
    +      pc = c;
    +    }
    +  }else{
    +    /* If this is the first field being parsed and it begins with the
    +    ** UTF-8 BOM  (0xEF BB BF) then skip the BOM */
    +    if( (c&0xff)==0xef && p->bNotFirst==0 ){
    +      import_append_char(p, c);
    +      c = fgetc(p->in);
    +      if( (c&0xff)==0xbb ){
    +        import_append_char(p, c);
    +        c = fgetc(p->in);
    +        if( (c&0xff)==0xbf ){
    +          p->bNotFirst = 1;
    +          p->n = 0;
    +          return csv_read_one_field(p);
    +        }
    +      }
    +    }
    +    while( c!=EOF && c!=cSep && c!=rSep ){
    +      import_append_char(p, c);
    +      c = fgetc(p->in);
    +    }
    +    if( c==rSep ){
    +      p->nLine++;
    +      if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
    +    }
    +    p->cTerm = c;
    +  }
    +  if( p->z ) p->z[p->n] = 0;
    +  p->bNotFirst = 1;
    +  return p->z;
    +}
    +
    +/* Read a single field of ASCII delimited text.
    +**
    +**   +  Input comes from p->in.
    +**   +  Store results in p->z of length p->n.  Space to hold p->z comes
    +**      from sqlite3_malloc64().
    +**   +  Use p->cSep as the column separator.  The default is "\x1F".
    +**   +  Use p->rSep as the row separator.  The default is "\x1E".
    +**   +  Keep track of the row number in p->nLine.
    +**   +  Store the character that terminates the field in p->cTerm.  Store
    +**      EOF on end-of-file.
    +**   +  Report syntax errors on stderr
    +*/
    +static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
    +  int c;
    +  int cSep = (u8)p->cColSep;
    +  int rSep = (u8)p->cRowSep;
    +  p->n = 0;
    +  c = fgetc(p->in);
    +  if( c==EOF || seenInterrupt ){
    +    p->cTerm = EOF;
    +    return 0;
    +  }
    +  while( c!=EOF && c!=cSep && c!=rSep ){
    +    import_append_char(p, c);
    +    c = fgetc(p->in);
    +  }
    +  if( c==rSep ){
    +    p->nLine++;
    +  }
    +  p->cTerm = c;
    +  if( p->z ) p->z[p->n] = 0;
    +  return p->z;
    +}
    +
    +/*
    +** Try to transfer data for table zTable.  If an error is seen while
    +** moving forward, try to go backwards.  The backwards movement won't
    +** work for WITHOUT ROWID tables.
    +*/
    +static void tryToCloneData(
    +  ShellState *p,
    +  sqlite3 *newDb,
    +  const char *zTable
    +){
    +  sqlite3_stmt *pQuery = 0;
    +  sqlite3_stmt *pInsert = 0;
    +  char *zQuery = 0;
    +  char *zInsert = 0;
    +  int rc;
    +  int i, j, n;
    +  int nTable = strlen30(zTable);
    +  int k = 0;
    +  int cnt = 0;
    +  const int spinRate = 10000;
    +
    +  zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
    +  shell_check_oom(zQuery);
    +  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
    +  if( rc ){
    +    sqlite3_fprintf(stderr,"Error %d: %s on [%s]\n",
    +          sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), zQuery);
    +    goto end_data_xfer;
    +  }
    +  n = sqlite3_column_count(pQuery);
    +  zInsert = sqlite3_malloc64(200 + nTable + n*3);
    +  shell_check_oom(zInsert);
    +  sqlite3_snprintf(200+nTable,zInsert,
    +                   "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
    +  i = strlen30(zInsert);
    +  for(j=1; jdb, zQuery, -1, &pQuery, 0);
    +    if( rc ){
    +      sqlite3_fprintf(stderr,"Warning: cannot step \"%s\" backwards", zTable);
    +      break;
    +    }
    +  } /* End for(k=0...) */
    +
    +end_data_xfer:
    +  sqlite3_finalize(pQuery);
    +  sqlite3_finalize(pInsert);
    +  sqlite3_free(zQuery);
    +  sqlite3_free(zInsert);
    +}
    +
    +
    +/*
    +** Try to transfer all rows of the schema that match zWhere.  For
    +** each row, invoke xForEach() on the object defined by that row.
    +** If an error is encountered while moving forward through the
    +** sqlite_schema table, try again moving backwards.
    +*/
    +static void tryToCloneSchema(
    +  ShellState *p,
    +  sqlite3 *newDb,
    +  const char *zWhere,
    +  void (*xForEach)(ShellState*,sqlite3*,const char*)
    +){
    +  sqlite3_stmt *pQuery = 0;
    +  char *zQuery = 0;
    +  int rc;
    +  const unsigned char *zName;
    +  const unsigned char *zSql;
    +  char *zErrMsg = 0;
    +
    +  zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
    +                           " WHERE %s ORDER BY rowid ASC", zWhere);
    +  shell_check_oom(zQuery);
    +  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
    +  if( rc ){
    +    sqlite3_fprintf(stderr,
    +          "Error: (%d) %s on [%s]\n", sqlite3_extended_errcode(p->db),
    +          sqlite3_errmsg(p->db), zQuery);
    +    goto end_schema_xfer;
    +  }
    +  while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
    +    zName = sqlite3_column_text(pQuery, 0);
    +    zSql = sqlite3_column_text(pQuery, 1);
    +    if( zName==0 || zSql==0 ) continue;
    +    if( sqlite3_stricmp((char*)zName, "sqlite_sequence")!=0 ){
    +      sqlite3_fprintf(stdout, "%s... ", zName); fflush(stdout);
    +      sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
    +      if( zErrMsg ){
    +        sqlite3_fprintf(stderr,"Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
    +        sqlite3_free(zErrMsg);
    +        zErrMsg = 0;
    +      }
    +    }
    +    if( xForEach ){
    +      xForEach(p, newDb, (const char*)zName);
    +    }
    +    sputz(stdout, "done\n");
    +  }
    +  if( rc!=SQLITE_DONE ){
    +    sqlite3_finalize(pQuery);
    +    sqlite3_free(zQuery);
    +    zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
    +                             " WHERE %s ORDER BY rowid DESC", zWhere);
    +    shell_check_oom(zQuery);
    +    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
    +    if( rc ){
    +      sqlite3_fprintf(stderr,"Error: (%d) %s on [%s]\n",
    +            sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), zQuery);
    +      goto end_schema_xfer;
    +    }
    +    while( sqlite3_step(pQuery)==SQLITE_ROW ){
    +      zName = sqlite3_column_text(pQuery, 0);
    +      zSql = sqlite3_column_text(pQuery, 1);
    +      if( zName==0 || zSql==0 ) continue;
    +      if( sqlite3_stricmp((char*)zName, "sqlite_sequence")==0 ) continue;
    +      sqlite3_fprintf(stdout, "%s... ", zName); fflush(stdout);
    +      sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
    +      if( zErrMsg ){
    +        sqlite3_fprintf(stderr,"Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
    +        sqlite3_free(zErrMsg);
    +        zErrMsg = 0;
    +      }
    +      if( xForEach ){
    +        xForEach(p, newDb, (const char*)zName);
    +      }
    +      sputz(stdout, "done\n");
    +    }
    +  }
    +end_schema_xfer:
    +  sqlite3_finalize(pQuery);
    +  sqlite3_free(zQuery);
    +}
    +
    +/*
    +** Open a new database file named "zNewDb".  Try to recover as much information
    +** as possible out of the main database (which might be corrupt) and write it
    +** into zNewDb.
    +*/
    +static void tryToClone(ShellState *p, const char *zNewDb){
    +  int rc;
    +  sqlite3 *newDb = 0;
    +  if( access(zNewDb,0)==0 ){
    +    sqlite3_fprintf(stderr,"File \"%s\" already exists.\n", zNewDb);
    +    return;
    +  }
    +  rc = sqlite3_open(zNewDb, &newDb);
    +  if( rc ){
    +    sqlite3_fprintf(stderr,
    +        "Cannot create output database: %s\n", sqlite3_errmsg(newDb));
    +  }else{
    +    sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
    +    sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
    +    tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
    +    tryToCloneSchema(p, newDb, "type!='table'", 0);
    +    sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
    +    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
    +  }
    +  close_db(newDb);
    +}
    +
    +#ifndef SQLITE_SHELL_FIDDLE
    +/*
    +** Change the output stream (file or pipe or console) to something else.
    +*/
    +static void output_redir(ShellState *p, FILE *pfNew){
    +  if( p->out != stdout ){
    +    sqlite3_fputs("Output already redirected.\n", stderr);
    +  }else{
    +    p->out = pfNew;
    +    setCrlfMode(p);
    +    if( p->mode==MODE_Www ){
    +      sqlite3_fputs(
    +        "\n"
    +        "
    \n",
    +        p->out
    +      );
    +    }
    +  }
    +}
    +
    +/*
    +** Change the output file back to stdout.
    +**
    +** If the p->doXdgOpen flag is set, that means the output was being
    +** redirected to a temporary file named by p->zTempFile.  In that case,
    +** launch start/open/xdg-open on that temporary file.
    +*/
    +static void output_reset(ShellState *p){
    +  if( p->outfile[0]=='|' ){
    +#ifndef SQLITE_OMIT_POPEN
    +    pclose(p->out);
    +#endif
    +  }else{
    +    if( p->mode==MODE_Www ){
    +      sqlite3_fputs("
    \n", p->out); + } + output_file_close(p->out); +#ifndef SQLITE_NOHAVE_SYSTEM + if( p->doXdgOpen ){ + const char *zXdgOpenCmd = +#if defined(_WIN32) + "start"; +#elif defined(__APPLE__) + "open"; +#else + "xdg-open"; +#endif + char *zCmd; + zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile); + if( system(zCmd) ){ + sqlite3_fprintf(stderr,"Failed: [%s]\n", zCmd); + }else{ + /* Give the start/open/xdg-open command some time to get + ** going before we continue, and potential delete the + ** p->zTempFile data file out from under it */ + sqlite3_sleep(2000); + } + sqlite3_free(zCmd); + outputModePop(p); + p->doXdgOpen = 0; + } +#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */ + } + p->outfile[0] = 0; + p->out = stdout; + setCrlfMode(p); +} +#else +# define output_redir(SS,pfO) +# define output_reset(SS) +#endif + +/* +** Run an SQL command and return the single integer result. +*/ +static int db_int(sqlite3 *db, const char *zSql, ...){ + sqlite3_stmt *pStmt; + int res = 0; + char *z; + va_list ap; + va_start(ap, zSql); + z = sqlite3_vmprintf(zSql, ap); + va_end(ap); + sqlite3_prepare_v2(db, z, -1, &pStmt, 0); + if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){ + res = sqlite3_column_int(pStmt,0); + } + sqlite3_finalize(pStmt); + sqlite3_free(z); + return res; +} + +#if SQLITE_SHELL_HAVE_RECOVER +/* +** Convert a 2-byte or 4-byte big-endian integer into a native integer +*/ +static unsigned int get2byteInt(unsigned char *a){ + return ((unsigned int)a[0]<<8) + (unsigned int)a[1]; +} +static unsigned int get4byteInt(unsigned char *a){ + return ((unsigned int)a[0]<<24) + + ((unsigned int)a[1]<<16) + + ((unsigned int)a[2]<<8) + + (unsigned int)a[3]; +} + +/* +** Implementation of the ".dbinfo" command. +** +** Return 1 on error, 2 to exit, and 0 otherwise. +*/ +static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){ + static const struct { const char *zName; int ofst; } aField[] = { + { "file change counter:", 24 }, + { "database page count:", 28 }, + { "freelist page count:", 36 }, + { "schema cookie:", 40 }, + { "schema format:", 44 }, + { "default cache size:", 48 }, + { "autovacuum top root:", 52 }, + { "incremental vacuum:", 64 }, + { "text encoding:", 56 }, + { "user version:", 60 }, + { "application id:", 68 }, + { "software version:", 96 }, + }; + static const struct { const char *zName; const char *zSql; } aQuery[] = { + { "number of tables:", + "SELECT count(*) FROM %s WHERE type='table'" }, + { "number of indexes:", + "SELECT count(*) FROM %s WHERE type='index'" }, + { "number of triggers:", + "SELECT count(*) FROM %s WHERE type='trigger'" }, + { "number of views:", + "SELECT count(*) FROM %s WHERE type='view'" }, + { "schema size:", + "SELECT total(length(sql)) FROM %s" }, + }; + int i, rc; + unsigned iDataVersion; + char *zSchemaTab; + char *zDb = nArg>=2 ? azArg[1] : "main"; + sqlite3_stmt *pStmt = 0; + unsigned char aHdr[100]; + open_db(p, 0); + if( p->db==0 ) return 1; + rc = sqlite3_prepare_v2(p->db, + "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1", + -1, &pStmt, 0); + if( rc ){ + sqlite3_fprintf(stderr,"error: %s\n", sqlite3_errmsg(p->db)); + sqlite3_finalize(pStmt); + return 1; + } + sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC); + if( sqlite3_step(pStmt)==SQLITE_ROW + && sqlite3_column_bytes(pStmt,0)>100 + ){ + const u8 *pb = sqlite3_column_blob(pStmt,0); + shell_check_oom(pb); + memcpy(aHdr, pb, 100); + sqlite3_finalize(pStmt); + }else{ + sqlite3_fputs("unable to read database header\n", stderr); + sqlite3_finalize(pStmt); + return 1; + } + i = get2byteInt(aHdr+16); + if( i==1 ) i = 65536; + sqlite3_fprintf(p->out, "%-20s %d\n", "database page size:", i); + sqlite3_fprintf(p->out, "%-20s %d\n", "write format:", aHdr[18]); + sqlite3_fprintf(p->out, "%-20s %d\n", "read format:", aHdr[19]); + sqlite3_fprintf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]); + for(i=0; iout, "%-20s %u", aField[i].zName, val); + switch( ofst ){ + case 56: { + if( val==1 ) sqlite3_fputs(" (utf8)", p->out); + if( val==2 ) sqlite3_fputs(" (utf16le)", p->out); + if( val==3 ) sqlite3_fputs(" (utf16be)", p->out); + } + } + sqlite3_fputs("\n", p->out); + } + if( zDb==0 ){ + zSchemaTab = sqlite3_mprintf("main.sqlite_schema"); + }else if( cli_strcmp(zDb,"temp")==0 ){ + zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema"); + }else{ + zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_schema", zDb); + } + for(i=0; idb, aQuery[i].zSql, zSchemaTab); + sqlite3_fprintf(p->out, "%-20s %d\n", aQuery[i].zName, val); + } + sqlite3_free(zSchemaTab); + sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion); + sqlite3_fprintf(p->out, "%-20s %u\n", "data version", iDataVersion); + return 0; +} +#endif /* SQLITE_SHELL_HAVE_RECOVER */ + +/* +** Implementation of the ".dbtotxt" command. +** +** Return 1 on error, 2 to exit, and 0 otherwise. +*/ +static int shell_dbtotxt_command(ShellState *p, int nArg, char **azArg){ + sqlite3_stmt *pStmt = 0; + sqlite3_int64 nPage = 0; + int pgSz = 0; + const char *zTail; + char *zName = 0; + int rc, i, j; + unsigned char bShow[256]; /* Characters ok to display */ + + UNUSED_PARAMETER(nArg); + UNUSED_PARAMETER(azArg); + memset(bShow, '.', sizeof(bShow)); + for(i=' '; i<='~'; i++){ + if( i!='{' && i!='}' && i!='"' && i!='\\' ) bShow[i] = (unsigned char)i; + } + rc = sqlite3_prepare_v2(p->db, "PRAGMA page_size", -1, &pStmt, 0); + if( rc ) goto dbtotxt_error; + rc = 0; + if( sqlite3_step(pStmt)!=SQLITE_ROW ) goto dbtotxt_error; + pgSz = sqlite3_column_int(pStmt, 0); + sqlite3_finalize(pStmt); + pStmt = 0; + if( pgSz<512 || pgSz>65536 || (pgSz&(pgSz-1))!=0 ) goto dbtotxt_error; + rc = sqlite3_prepare_v2(p->db, "PRAGMA page_count", -1, &pStmt, 0); + if( rc ) goto dbtotxt_error; + rc = 0; + if( sqlite3_step(pStmt)!=SQLITE_ROW ) goto dbtotxt_error; + nPage = sqlite3_column_int64(pStmt, 0); + sqlite3_finalize(pStmt); + pStmt = 0; + if( nPage<1 ) goto dbtotxt_error; + rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0); + if( rc ) goto dbtotxt_error; + if( sqlite3_step(pStmt)!=SQLITE_ROW ){ + zTail = "unk.db"; + }else{ + const char *zFilename = (const char*)sqlite3_column_text(pStmt, 2); + if( zFilename==0 || zFilename[0]==0 ) zFilename = "unk.db"; + zTail = strrchr(zFilename, '/'); +#if defined(_WIN32) + if( zTail==0 ) zTail = strrchr(zFilename, '\\'); +#endif + if( zTail==0 ){ + zTail = zFilename; + }else if( zTail[1]!=0 ){ + zTail++; + } + } + zName = strdup(zTail); + shell_check_oom(zName); + sqlite3_fprintf(p->out, "| size %lld pagesize %d filename %s\n", + nPage*pgSz, pgSz, zName); + sqlite3_finalize(pStmt); + pStmt = 0; + rc = sqlite3_prepare_v2(p->db, + "SELECT pgno, data FROM sqlite_dbpage ORDER BY pgno", -1, &pStmt, 0); + if( rc ) goto dbtotxt_error; + while( sqlite3_step(pStmt)==SQLITE_ROW ){ + sqlite3_int64 pgno = sqlite3_column_int64(pStmt, 0); + const u8 *aData = sqlite3_column_blob(pStmt, 1); + int seenPageLabel = 0; + for(i=0; iout, "| page %lld offset %lld\n",pgno,(pgno-1)*pgSz); + seenPageLabel = 1; + } + sqlite3_fprintf(p->out, "| %5d:", i); + for(j=0; j<16; j++) sqlite3_fprintf(p->out, " %02x", aLine[j]); + sqlite3_fprintf(p->out, " "); + for(j=0; j<16; j++){ + unsigned char c = (unsigned char)aLine[j]; + sqlite3_fprintf(p->out, "%c", bShow[c]); + } + sqlite3_fprintf(p->out, "\n"); + } + } + sqlite3_finalize(pStmt); + sqlite3_fprintf(p->out, "| end %s\n", zName); + free(zName); + return 0; + +dbtotxt_error: + if( rc ){ + sqlite3_fprintf(stderr, "ERROR: %s\n", sqlite3_errmsg(p->db)); + } + sqlite3_finalize(pStmt); + free(zName); + return 1; +} + +/* +** Print the given string as an error message. +*/ +static void shellEmitError(const char *zErr){ + sqlite3_fprintf(stderr,"Error: %s\n", zErr); +} +/* +** Print the current sqlite3_errmsg() value to stderr and return 1. +*/ +static int shellDatabaseError(sqlite3 *db){ + shellEmitError(sqlite3_errmsg(db)); + return 1; +} + +/* +** Compare the pattern in zGlob[] against the text in z[]. Return TRUE +** if they match and FALSE (0) if they do not match. +** +** Globbing rules: +** +** '*' Matches any sequence of zero or more characters. +** +** '?' Matches exactly one character. +** +** [...] Matches one character from the enclosed list of +** characters. +** +** [^...] Matches one character not in the enclosed list. +** +** '#' Matches any sequence of one or more digits with an +** optional + or - sign in front +** +** ' ' Any span of whitespace matches any other span of +** whitespace. +** +** Extra whitespace at the end of z[] is ignored. +*/ +static int testcase_glob(const char *zGlob, const char *z){ + int c, c2; + int invert; + int seen; + + while( (c = (*(zGlob++)))!=0 ){ + if( IsSpace(c) ){ + if( !IsSpace(*z) ) return 0; + while( IsSpace(*zGlob) ) zGlob++; + while( IsSpace(*z) ) z++; + }else if( c=='*' ){ + while( (c=(*(zGlob++))) == '*' || c=='?' ){ + if( c=='?' && (*(z++))==0 ) return 0; + } + if( c==0 ){ + return 1; + }else if( c=='[' ){ + while( *z && testcase_glob(zGlob-1,z)==0 ){ + z++; + } + return (*z)!=0; + } + while( (c2 = (*(z++)))!=0 ){ + while( c2!=c ){ + c2 = *(z++); + if( c2==0 ) return 0; + } + if( testcase_glob(zGlob,z) ) return 1; + } + return 0; + }else if( c=='?' ){ + if( (*(z++))==0 ) return 0; + }else if( c=='[' ){ + int prior_c = 0; + seen = 0; + invert = 0; + c = *(z++); + if( c==0 ) return 0; + c2 = *(zGlob++); + if( c2=='^' ){ + invert = 1; + c2 = *(zGlob++); + } + if( c2==']' ){ + if( c==']' ) seen = 1; + c2 = *(zGlob++); + } + while( c2 && c2!=']' ){ + if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){ + c2 = *(zGlob++); + if( c>=prior_c && c<=c2 ) seen = 1; + prior_c = 0; + }else{ + if( c==c2 ){ + seen = 1; + } + prior_c = c2; + } + c2 = *(zGlob++); + } + if( c2==0 || (seen ^ invert)==0 ) return 0; + }else if( c=='#' ){ + if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++; + if( !IsDigit(z[0]) ) return 0; + z++; + while( IsDigit(z[0]) ){ z++; } + }else{ + if( c!=(*(z++)) ) return 0; + } + } + while( IsSpace(*z) ){ z++; } + return *z==0; +} + + +/* +** Compare the string as a command-line option with either one or two +** initial "-" characters. +*/ +static int optionMatch(const char *zStr, const char *zOpt){ + if( zStr[0]!='-' ) return 0; + zStr++; + if( zStr[0]=='-' ) zStr++; + return cli_strcmp(zStr, zOpt)==0; +} + +/* +** The input zFN is guaranteed to start with "file:" and is thus a URI +** filename. Extract the actual filename and return a pointer to that +** filename in spaced obtained from sqlite3_malloc(). +** +** The caller is responsible for freeing space using sqlite3_free() when +** it has finished with the filename. +*/ +static char *shellFilenameFromUri(const char *zFN){ + char *zOut; + int i, j, d1, d2; + + assert( cli_strncmp(zFN,"file:",5)==0 ); + zOut = sqlite3_mprintf("%s", zFN+5); + shell_check_oom(zOut); + for(i=j=0; zOut[i]!=0 && zOut[i]!='?'; i++){ + if( zOut[i]!='%' ){ + zOut[j++] = zOut[i]; + continue; + } + d1 = hexDigitValue(zOut[i+1]); + if( d1<0 ){ + zOut[j] = 0; + break; + } + d2 = hexDigitValue(zOut[i+2]); + if( d2<0 ){ + zOut[j] = 0; + break; + } + zOut[j++] = d1*16 + d2; + i += 2; + } + zOut[j] = 0; + return zOut; +} + +/* +** Delete a file. +*/ +int shellDeleteFile(const char *zFilename){ + int rc; +#ifdef _WIN32 + wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename); + rc = _wunlink(z); + sqlite3_free(z); +#else + rc = unlink(zFilename); +#endif + return rc; +} + +/* +** Try to delete the temporary file (if there is one) and free the +** memory used to hold the name of the temp file. +*/ +static void clearTempFile(ShellState *p){ + if( p->zTempFile==0 ) return; + if( p->doXdgOpen ) return; + if( shellDeleteFile(p->zTempFile) ) return; + sqlite3_free(p->zTempFile); + p->zTempFile = 0; +} + +/* +** Create a new temp file name with the given suffix. +*/ +static void newTempFile(ShellState *p, const char *zSuffix){ + clearTempFile(p); + sqlite3_free(p->zTempFile); + p->zTempFile = 0; + if( p->db ){ + sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile); + } + if( p->zTempFile==0 ){ + /* If p->db is an in-memory database then the TEMPFILENAME file-control + ** will not work and we will need to fallback to guessing */ + char *zTemp; + sqlite3_uint64 r; + sqlite3_randomness(sizeof(r), &r); + zTemp = getenv("TEMP"); + if( zTemp==0 ) zTemp = getenv("TMP"); + if( zTemp==0 ){ +#ifdef _WIN32 + zTemp = "\\tmp"; +#else + zTemp = "/tmp"; +#endif + } + p->zTempFile = sqlite3_mprintf("%s/temp%llx.%s", zTemp, r, zSuffix); + }else{ + p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix); + } + shell_check_oom(p->zTempFile); +} + + +/* +** The implementation of SQL scalar function fkey_collate_clause(), used +** by the ".lint fkey-indexes" command. This scalar function is always +** called with four arguments - the parent table name, the parent column name, +** the child table name and the child column name. +** +** fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col') +** +** If either of the named tables or columns do not exist, this function +** returns an empty string. An empty string is also returned if both tables +** and columns exist but have the same default collation sequence. Or, +** if both exist but the default collation sequences are different, this +** function returns the string " COLLATE ", where +** is the default collation sequence of the parent column. +*/ +static void shellFkeyCollateClause( + sqlite3_context *pCtx, + int nVal, + sqlite3_value **apVal +){ + sqlite3 *db = sqlite3_context_db_handle(pCtx); + const char *zParent; + const char *zParentCol; + const char *zParentSeq; + const char *zChild; + const char *zChildCol; + const char *zChildSeq = 0; /* Initialize to avoid false-positive warning */ + int rc; + + assert( nVal==4 ); + zParent = (const char*)sqlite3_value_text(apVal[0]); + zParentCol = (const char*)sqlite3_value_text(apVal[1]); + zChild = (const char*)sqlite3_value_text(apVal[2]); + zChildCol = (const char*)sqlite3_value_text(apVal[3]); + + sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC); + rc = sqlite3_table_column_metadata( + db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0 + ); + if( rc==SQLITE_OK ){ + rc = sqlite3_table_column_metadata( + db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0 + ); + } + + if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){ + char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq); + sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT); + sqlite3_free(z); + } +} + + +/* +** The implementation of dot-command ".lint fkey-indexes". +*/ +static int lintFkeyIndexes( + ShellState *pState, /* Current shell tool state */ + char **azArg, /* Array of arguments passed to dot command */ + int nArg /* Number of entries in azArg[] */ +){ + sqlite3 *db = pState->db; /* Database handle to query "main" db of */ + int bVerbose = 0; /* If -verbose is present */ + int bGroupByParent = 0; /* If -groupbyparent is present */ + int i; /* To iterate through azArg[] */ + const char *zIndent = ""; /* How much to indent CREATE INDEX by */ + int rc; /* Return code */ + sqlite3_stmt *pSql = 0; /* Compiled version of SQL statement below */ + FILE *out = pState->out; /* Send output here */ + + /* + ** This SELECT statement returns one row for each foreign key constraint + ** in the schema of the main database. The column values are: + ** + ** 0. The text of an SQL statement similar to: + ** + ** "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?" + ** + ** This SELECT is similar to the one that the foreign keys implementation + ** needs to run internally on child tables. If there is an index that can + ** be used to optimize this query, then it can also be used by the FK + ** implementation to optimize DELETE or UPDATE statements on the parent + ** table. + ** + ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by + ** the EXPLAIN QUERY PLAN command matches this pattern, then the schema + ** contains an index that can be used to optimize the query. + ** + ** 2. Human readable text that describes the child table and columns. e.g. + ** + ** "child_table(child_key1, child_key2)" + ** + ** 3. Human readable text that describes the parent table and columns. e.g. + ** + ** "parent_table(parent_key1, parent_key2)" + ** + ** 4. A full CREATE INDEX statement for an index that could be used to + ** optimize DELETE or UPDATE statements on the parent table. e.g. + ** + ** "CREATE INDEX child_table_child_key ON child_table(child_key)" + ** + ** 5. The name of the parent table. + ** + ** These six values are used by the C logic below to generate the report. + */ + const char *zSql = + "SELECT " + " 'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '" + " || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' " + " || fkey_collate_clause(" + " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')" + ", " + " 'SEARCH ' || s.name || ' USING COVERING INDEX*('" + " || group_concat('*=?', ' AND ') || ')'" + ", " + " s.name || '(' || group_concat(f.[from], ', ') || ')'" + ", " + " f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'" + ", " + " 'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))" + " || ' ON ' || quote(s.name) || '('" + " || group_concat(quote(f.[from]) ||" + " fkey_collate_clause(" + " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')" + " || ');'" + ", " + " f.[table] " + "FROM sqlite_schema AS s, pragma_foreign_key_list(s.name) AS f " + "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) " + "GROUP BY s.name, f.id " + "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)" + ; + const char *zGlobIPK = "SEARCH * USING INTEGER PRIMARY KEY (rowid=?)"; + + for(i=2; i1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){ + bVerbose = 1; + } + else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){ + bGroupByParent = 1; + zIndent = " "; + } + else{ + sqlite3_fprintf(stderr, + "Usage: %s %s ?-verbose? ?-groupbyparent?\n", azArg[0], azArg[1]); + return SQLITE_ERROR; + } + } + + /* Register the fkey_collate_clause() SQL function */ + rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8, + 0, shellFkeyCollateClause, 0, 0 + ); + + + if( rc==SQLITE_OK ){ + rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0); + } + if( rc==SQLITE_OK ){ + sqlite3_bind_int(pSql, 1, bGroupByParent); + } + + if( rc==SQLITE_OK ){ + int rc2; + char *zPrev = 0; + while( SQLITE_ROW==sqlite3_step(pSql) ){ + int res = -1; + sqlite3_stmt *pExplain = 0; + const char *zEQP = (const char*)sqlite3_column_text(pSql, 0); + const char *zGlob = (const char*)sqlite3_column_text(pSql, 1); + const char *zFrom = (const char*)sqlite3_column_text(pSql, 2); + const char *zTarget = (const char*)sqlite3_column_text(pSql, 3); + const char *zCI = (const char*)sqlite3_column_text(pSql, 4); + const char *zParent = (const char*)sqlite3_column_text(pSql, 5); + + if( zEQP==0 ) continue; + if( zGlob==0 ) continue; + rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); + if( rc!=SQLITE_OK ) break; + if( SQLITE_ROW==sqlite3_step(pExplain) ){ + const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3); + res = zPlan!=0 && ( 0==sqlite3_strglob(zGlob, zPlan) + || 0==sqlite3_strglob(zGlobIPK, zPlan)); + } + rc = sqlite3_finalize(pExplain); + if( rc!=SQLITE_OK ) break; + + if( res<0 ){ + sqlite3_fputs("Error: internal error", stderr); + break; + }else{ + if( bGroupByParent + && (bVerbose || res==0) + && (zPrev==0 || sqlite3_stricmp(zParent, zPrev)) + ){ + sqlite3_fprintf(out, "-- Parent table %s\n", zParent); + sqlite3_free(zPrev); + zPrev = sqlite3_mprintf("%s", zParent); + } + + if( res==0 ){ + sqlite3_fprintf(out, "%s%s --> %s\n", zIndent, zCI, zTarget); + }else if( bVerbose ){ + sqlite3_fprintf(out, + "%s/* no extra indexes required for %s -> %s */\n", + zIndent, zFrom, zTarget + ); + } + } + } + sqlite3_free(zPrev); + + if( rc!=SQLITE_OK ){ + sqlite3_fprintf(stderr,"%s\n", sqlite3_errmsg(db)); + } + + rc2 = sqlite3_finalize(pSql); + if( rc==SQLITE_OK && rc2!=SQLITE_OK ){ + rc = rc2; + sqlite3_fprintf(stderr,"%s\n", sqlite3_errmsg(db)); + } + }else{ + sqlite3_fprintf(stderr,"%s\n", sqlite3_errmsg(db)); + } + + return rc; +} + +/* +** Implementation of ".lint" dot command. +*/ +static int lintDotCommand( + ShellState *pState, /* Current shell tool state */ + char **azArg, /* Array of arguments passed to dot command */ + int nArg /* Number of entries in azArg[] */ +){ + int n; + n = (nArg>=2 ? strlen30(azArg[1]) : 0); + if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage; + return lintFkeyIndexes(pState, azArg, nArg); + + usage: + sqlite3_fprintf(stderr,"Usage %s sub-command ?switches...?\n", azArg[0]); + sqlite3_fprintf(stderr, "Where sub-commands are:\n"); + sqlite3_fprintf(stderr, " fkey-indexes\n"); + return SQLITE_ERROR; +} + +static void shellPrepare( + sqlite3 *db, + int *pRc, + const char *zSql, + sqlite3_stmt **ppStmt +){ + *ppStmt = 0; + if( *pRc==SQLITE_OK ){ + int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0); + if( rc!=SQLITE_OK ){ + sqlite3_fprintf(stderr, + "sql error: %s (%d)\n", sqlite3_errmsg(db), sqlite3_errcode(db)); + *pRc = rc; + } + } +} + +/* +** Create a prepared statement using printf-style arguments for the SQL. +*/ +static void shellPreparePrintf( + sqlite3 *db, + int *pRc, + sqlite3_stmt **ppStmt, + const char *zFmt, + ... +){ + *ppStmt = 0; + if( *pRc==SQLITE_OK ){ + va_list ap; + char *z; + va_start(ap, zFmt); + z = sqlite3_vmprintf(zFmt, ap); + va_end(ap); + if( z==0 ){ + *pRc = SQLITE_NOMEM; + }else{ + shellPrepare(db, pRc, z, ppStmt); + sqlite3_free(z); + } + } +} + +/* +** Finalize the prepared statement created using shellPreparePrintf(). +*/ +static void shellFinalize( + int *pRc, + sqlite3_stmt *pStmt +){ + if( pStmt ){ + sqlite3 *db = sqlite3_db_handle(pStmt); + int rc = sqlite3_finalize(pStmt); + if( *pRc==SQLITE_OK ){ + if( rc!=SQLITE_OK ){ + sqlite3_fprintf(stderr,"SQL error: %s\n", sqlite3_errmsg(db)); + } + *pRc = rc; + } + } +} + +#if !defined SQLITE_OMIT_VIRTUALTABLE +/* Reset the prepared statement created using shellPreparePrintf(). +** +** This routine is could be marked "static". But it is not always used, +** depending on compile-time options. By omitting the "static", we avoid +** nuisance compiler warnings about "defined but not used". +*/ +void shellReset( + int *pRc, + sqlite3_stmt *pStmt +){ + int rc = sqlite3_reset(pStmt); + if( *pRc==SQLITE_OK ){ + if( rc!=SQLITE_OK ){ + sqlite3 *db = sqlite3_db_handle(pStmt); + sqlite3_fprintf(stderr,"SQL error: %s\n", sqlite3_errmsg(db)); + } + *pRc = rc; + } +} +#endif /* !defined SQLITE_OMIT_VIRTUALTABLE */ + +#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) +/****************************************************************************** +** The ".archive" or ".ar" command. +*/ +/* +** Structure representing a single ".ar" command. +*/ +typedef struct ArCommand ArCommand; +struct ArCommand { + u8 eCmd; /* An AR_CMD_* value */ + u8 bVerbose; /* True if --verbose */ + u8 bZip; /* True if the archive is a ZIP */ + u8 bDryRun; /* True if --dry-run */ + u8 bAppend; /* True if --append */ + u8 bGlob; /* True if --glob */ + u8 fromCmdLine; /* Run from -A instead of .archive */ + int nArg; /* Number of command arguments */ + char *zSrcTable; /* "sqlar", "zipfile($file)" or "zip" */ + const char *zFile; /* --file argument, or NULL */ + const char *zDir; /* --directory argument, or NULL */ + char **azArg; /* Array of command arguments */ + ShellState *p; /* Shell state */ + FILE *out; /* Output to this stream */ + sqlite3 *db; /* Database containing the archive */ +}; + +/* +** Print a usage message for the .ar command to stderr and return SQLITE_ERROR. +*/ +static int arUsage(FILE *f){ + showHelp(f,"archive"); + return SQLITE_ERROR; +} + +/* +** Print an error message for the .ar command to stderr and return +** SQLITE_ERROR. +*/ +static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){ + va_list ap; + char *z; + va_start(ap, zFmt); + z = sqlite3_vmprintf(zFmt, ap); + va_end(ap); + shellEmitError(z); + if( pAr->fromCmdLine ){ + sqlite3_fputs("Use \"-A\" for more help\n", stderr); + }else{ + sqlite3_fputs("Use \".archive --help\" for more help\n", stderr); + } + sqlite3_free(z); + return SQLITE_ERROR; +} + +/* +** Values for ArCommand.eCmd. +*/ +#define AR_CMD_CREATE 1 +#define AR_CMD_UPDATE 2 +#define AR_CMD_INSERT 3 +#define AR_CMD_EXTRACT 4 +#define AR_CMD_LIST 5 +#define AR_CMD_HELP 6 +#define AR_CMD_REMOVE 7 + +/* +** Other (non-command) switches. +*/ +#define AR_SWITCH_VERBOSE 8 +#define AR_SWITCH_FILE 9 +#define AR_SWITCH_DIRECTORY 10 +#define AR_SWITCH_APPEND 11 +#define AR_SWITCH_DRYRUN 12 +#define AR_SWITCH_GLOB 13 + +static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){ + switch( eSwitch ){ + case AR_CMD_CREATE: + case AR_CMD_EXTRACT: + case AR_CMD_LIST: + case AR_CMD_REMOVE: + case AR_CMD_UPDATE: + case AR_CMD_INSERT: + case AR_CMD_HELP: + if( pAr->eCmd ){ + return arErrorMsg(pAr, "multiple command options"); + } + pAr->eCmd = eSwitch; + break; + + case AR_SWITCH_DRYRUN: + pAr->bDryRun = 1; + break; + case AR_SWITCH_GLOB: + pAr->bGlob = 1; + break; + case AR_SWITCH_VERBOSE: + pAr->bVerbose = 1; + break; + case AR_SWITCH_APPEND: + pAr->bAppend = 1; + deliberate_fall_through; /* FALLTHRU */ + case AR_SWITCH_FILE: + pAr->zFile = zArg; + break; + case AR_SWITCH_DIRECTORY: + pAr->zDir = zArg; + break; + } + + return SQLITE_OK; +} + +/* +** Parse the command line for an ".ar" command. The results are written into +** structure (*pAr). SQLITE_OK is returned if the command line is parsed +** successfully, otherwise an error message is written to stderr and +** SQLITE_ERROR returned. +*/ +static int arParseCommand( + char **azArg, /* Array of arguments passed to dot command */ + int nArg, /* Number of entries in azArg[] */ + ArCommand *pAr /* Populate this object */ +){ + struct ArSwitch { + const char *zLong; + char cShort; + u8 eSwitch; + u8 bArg; + } aSwitch[] = { + { "create", 'c', AR_CMD_CREATE, 0 }, + { "extract", 'x', AR_CMD_EXTRACT, 0 }, + { "insert", 'i', AR_CMD_INSERT, 0 }, + { "list", 't', AR_CMD_LIST, 0 }, + { "remove", 'r', AR_CMD_REMOVE, 0 }, + { "update", 'u', AR_CMD_UPDATE, 0 }, + { "help", 'h', AR_CMD_HELP, 0 }, + { "verbose", 'v', AR_SWITCH_VERBOSE, 0 }, + { "file", 'f', AR_SWITCH_FILE, 1 }, + { "append", 'a', AR_SWITCH_APPEND, 1 }, + { "directory", 'C', AR_SWITCH_DIRECTORY, 1 }, + { "dryrun", 'n', AR_SWITCH_DRYRUN, 0 }, + { "glob", 'g', AR_SWITCH_GLOB, 0 }, + }; + int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch); + struct ArSwitch *pEnd = &aSwitch[nSwitch]; + + if( nArg<=1 ){ + sqlite3_fprintf(stderr, "Wrong number of arguments. Usage:\n"); + return arUsage(stderr); + }else{ + char *z = azArg[1]; + if( z[0]!='-' ){ + /* Traditional style [tar] invocation */ + int i; + int iArg = 2; + for(i=0; z[i]; i++){ + const char *zArg = 0; + struct ArSwitch *pOpt; + for(pOpt=&aSwitch[0]; pOptcShort ) break; + } + if( pOpt==pEnd ){ + return arErrorMsg(pAr, "unrecognized option: %c", z[i]); + } + if( pOpt->bArg ){ + if( iArg>=nArg ){ + return arErrorMsg(pAr, "option requires an argument: %c",z[i]); + } + zArg = azArg[iArg++]; + } + if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR; + } + pAr->nArg = nArg-iArg; + if( pAr->nArg>0 ){ + pAr->azArg = &azArg[iArg]; + } + }else{ + /* Non-traditional invocation */ + int iArg; + for(iArg=1; iArgazArg = &azArg[iArg]; + pAr->nArg = nArg-iArg; + break; + } + n = strlen30(z); + + if( z[1]!='-' ){ + int i; + /* One or more short options */ + for(i=1; icShort ) break; + } + if( pOpt==pEnd ){ + return arErrorMsg(pAr, "unrecognized option: %c", z[i]); + } + if( pOpt->bArg ){ + if( i<(n-1) ){ + zArg = &z[i+1]; + i = n; + }else{ + if( iArg>=(nArg-1) ){ + return arErrorMsg(pAr, "option requires an argument: %c", + z[i]); + } + zArg = azArg[++iArg]; + } + } + if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR; + } + }else if( z[2]=='\0' ){ + /* A -- option, indicating that all remaining command line words + ** are command arguments. */ + pAr->azArg = &azArg[iArg+1]; + pAr->nArg = nArg-iArg-1; + break; + }else{ + /* A long option */ + const char *zArg = 0; /* Argument for option, if any */ + struct ArSwitch *pMatch = 0; /* Matching option */ + struct ArSwitch *pOpt; /* Iterator */ + for(pOpt=&aSwitch[0]; pOptzLong; + if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){ + if( pMatch ){ + return arErrorMsg(pAr, "ambiguous option: %s",z); + }else{ + pMatch = pOpt; + } + } + } + + if( pMatch==0 ){ + return arErrorMsg(pAr, "unrecognized option: %s", z); + } + if( pMatch->bArg ){ + if( iArg>=(nArg-1) ){ + return arErrorMsg(pAr, "option requires an argument: %s", z); + } + zArg = azArg[++iArg]; + } + if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR; + } + } + } + } + if( pAr->eCmd==0 ){ + sqlite3_fprintf(stderr, "Required argument missing. Usage:\n"); + return arUsage(stderr); + } + return SQLITE_OK; +} + +/* +** This function assumes that all arguments within the ArCommand.azArg[] +** array refer to archive members, as for the --extract, --list or --remove +** commands. It checks that each of them are "present". If any specified +** file is not present in the archive, an error is printed to stderr and an +** error code returned. Otherwise, if all specified arguments are present +** in the archive, SQLITE_OK is returned. Here, "present" means either an +** exact equality when pAr->bGlob is false or a "name GLOB pattern" match +** when pAr->bGlob is true. +** +** This function strips any trailing '/' characters from each argument. +** This is consistent with the way the [tar] command seems to work on +** Linux. +*/ +static int arCheckEntries(ArCommand *pAr){ + int rc = SQLITE_OK; + if( pAr->nArg ){ + int i, j; + sqlite3_stmt *pTest = 0; + const char *zSel = (pAr->bGlob) + ? "SELECT name FROM %s WHERE glob($name,name)" + : "SELECT name FROM %s WHERE name=$name"; + + shellPreparePrintf(pAr->db, &rc, &pTest, zSel, pAr->zSrcTable); + j = sqlite3_bind_parameter_index(pTest, "$name"); + for(i=0; inArg && rc==SQLITE_OK; i++){ + char *z = pAr->azArg[i]; + int n = strlen30(z); + int bOk = 0; + while( n>0 && z[n-1]=='/' ) n--; + z[n] = '\0'; + sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC); + if( SQLITE_ROW==sqlite3_step(pTest) ){ + bOk = 1; + } + shellReset(&rc, pTest); + if( rc==SQLITE_OK && bOk==0 ){ + sqlite3_fprintf(stderr,"not found in archive: %s\n", z); + rc = SQLITE_ERROR; + } + } + shellFinalize(&rc, pTest); + } + return rc; +} + +/* +** Format a WHERE clause that can be used against the "sqlar" table to +** identify all archive members that match the command arguments held +** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning. +** The caller is responsible for eventually calling sqlite3_free() on +** any non-NULL (*pzWhere) value. Here, "match" means strict equality +** when pAr->bGlob is false and GLOB match when pAr->bGlob is true. +*/ +static void arWhereClause( + int *pRc, + ArCommand *pAr, + char **pzWhere /* OUT: New WHERE clause */ +){ + char *zWhere = 0; + if( *pRc==SQLITE_OK ){ + if( pAr->nArg==0 ){ + zWhere = sqlite3_mprintf("1"); + }else{ + char *z1 = sqlite3_mprintf(pAr->bGlob ? "" : "name IN("); + char *z2 = sqlite3_mprintf(""); + const char *zSep1 = ""; + const char *zSep2 = ""; + + int i; + for(i=0; inArg && z1 && z2; i++){ + const char *z = pAr->azArg[i]; + int n = strlen30(z); + + if( pAr->bGlob ){ + z1 = sqlite3_mprintf("%z%sname GLOB '%q'", z1, zSep2, z); + z2 = sqlite3_mprintf( + "%z%ssubstr(name,1,%d) GLOB '%q/'", z2, zSep2, n+1,z + ); + }else{ + z1 = sqlite3_mprintf("%z%s'%q'", z1, zSep1, z); + z2 = sqlite3_mprintf("%z%ssubstr(name,1,%d) = '%q/'",z2,zSep2,n+1,z); + } + zSep1 = ", "; + zSep2 = " OR "; + } + if( z1==0 || z2==0 ){ + *pRc = SQLITE_NOMEM; + }else{ + zWhere = sqlite3_mprintf("(%s%s OR (name GLOB '*/*' AND (%s))) ", + z1, pAr->bGlob==0 ? ")" : "", z2 + ); + } + sqlite3_free(z1); + sqlite3_free(z2); + } + } + *pzWhere = zWhere; +} + +/* +** Implementation of .ar "lisT" command. +*/ +static int arListCommand(ArCommand *pAr){ + const char *zSql = "SELECT %s FROM %s WHERE %s"; + const char *azCols[] = { + "name", + "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name" + }; + + char *zWhere = 0; + sqlite3_stmt *pSql = 0; + int rc; + + rc = arCheckEntries(pAr); + arWhereClause(&rc, pAr, &zWhere); + + shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose], + pAr->zSrcTable, zWhere); + if( pAr->bDryRun ){ + sqlite3_fprintf(pAr->out, "%s\n", sqlite3_sql(pSql)); + }else{ + while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){ + if( pAr->bVerbose ){ + sqlite3_fprintf(pAr->out, "%s % 10d %s %s\n", + sqlite3_column_text(pSql, 0), sqlite3_column_int(pSql, 1), + sqlite3_column_text(pSql, 2),sqlite3_column_text(pSql, 3)); + }else{ + sqlite3_fprintf(pAr->out, "%s\n", sqlite3_column_text(pSql, 0)); + } + } + } + shellFinalize(&rc, pSql); + sqlite3_free(zWhere); + return rc; +} + +/* +** Implementation of .ar "Remove" command. +*/ +static int arRemoveCommand(ArCommand *pAr){ + int rc = 0; + char *zSql = 0; + char *zWhere = 0; + + if( pAr->nArg ){ + /* Verify that args actually exist within the archive before proceeding. + ** And formulate a WHERE clause to match them. */ + rc = arCheckEntries(pAr); + arWhereClause(&rc, pAr, &zWhere); + } + if( rc==SQLITE_OK ){ + zSql = sqlite3_mprintf("DELETE FROM %s WHERE %s;", + pAr->zSrcTable, zWhere); + if( pAr->bDryRun ){ + sqlite3_fprintf(pAr->out, "%s\n", zSql); + }else{ + char *zErr = 0; + rc = sqlite3_exec(pAr->db, "SAVEPOINT ar;", 0, 0, 0); + if( rc==SQLITE_OK ){ + rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr); + if( rc!=SQLITE_OK ){ + sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0); + }else{ + rc = sqlite3_exec(pAr->db, "RELEASE ar;", 0, 0, 0); + } + } + if( zErr ){ + sqlite3_fprintf(stdout, "ERROR: %s\n", zErr); /* stdout? */ + sqlite3_free(zErr); + } + } + } + sqlite3_free(zWhere); + sqlite3_free(zSql); + return rc; +} + +/* +** Implementation of .ar "eXtract" command. +*/ +static int arExtractCommand(ArCommand *pAr){ + const char *zSql1 = + "SELECT " + " ($dir || name)," + " writefile(($dir || name), %s, mode, mtime) " + "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)" + " AND name NOT GLOB '*..[/\\]*'"; + + const char *azExtraArg[] = { + "sqlar_uncompress(data, sz)", + "data" + }; + + sqlite3_stmt *pSql = 0; + int rc = SQLITE_OK; + char *zDir = 0; + char *zWhere = 0; + int i, j; + + /* If arguments are specified, check that they actually exist within + ** the archive before proceeding. And formulate a WHERE clause to + ** match them. */ + rc = arCheckEntries(pAr); + arWhereClause(&rc, pAr, &zWhere); + + if( rc==SQLITE_OK ){ + if( pAr->zDir ){ + zDir = sqlite3_mprintf("%s/", pAr->zDir); + }else{ + zDir = sqlite3_mprintf(""); + } + if( zDir==0 ) rc = SQLITE_NOMEM; + } + + shellPreparePrintf(pAr->db, &rc, &pSql, zSql1, + azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere + ); + + if( rc==SQLITE_OK ){ + j = sqlite3_bind_parameter_index(pSql, "$dir"); + sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC); + + /* Run the SELECT statement twice. The first time, writefile() is called + ** for all archive members that should be extracted. The second time, + ** only for the directories. This is because the timestamps for + ** extracted directories must be reset after they are populated (as + ** populating them changes the timestamp). */ + for(i=0; i<2; i++){ + j = sqlite3_bind_parameter_index(pSql, "$dirOnly"); + sqlite3_bind_int(pSql, j, i); + if( pAr->bDryRun ){ + sqlite3_fprintf(pAr->out, "%s\n", sqlite3_sql(pSql)); + }else{ + while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){ + if( i==0 && pAr->bVerbose ){ + sqlite3_fprintf(pAr->out, "%s\n", sqlite3_column_text(pSql, 0)); + } + } + } + shellReset(&rc, pSql); + } + shellFinalize(&rc, pSql); + } + + sqlite3_free(zDir); + sqlite3_free(zWhere); + return rc; +} + +/* +** Run the SQL statement in zSql. Or if doing a --dryrun, merely print it out. +*/ +static int arExecSql(ArCommand *pAr, const char *zSql){ + int rc; + if( pAr->bDryRun ){ + sqlite3_fprintf(pAr->out, "%s\n", zSql); + rc = SQLITE_OK; + }else{ + char *zErr = 0; + rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr); + if( zErr ){ + sqlite3_fprintf(stdout, "ERROR: %s\n", zErr); + sqlite3_free(zErr); + } + } + return rc; +} + + +/* +** Implementation of .ar "create", "insert", and "update" commands. +** +** create -> Create a new SQL archive +** insert -> Insert or reinsert all files listed +** update -> Insert files that have changed or that were not +** previously in the archive +** +** Create the "sqlar" table in the database if it does not already exist. +** Then add each file in the azFile[] array to the archive. Directories +** are added recursively. If argument bVerbose is non-zero, a message is +** printed on stdout for each file archived. +** +** The create command is the same as update, except that it drops +** any existing "sqlar" table before beginning. The "insert" command +** always overwrites every file named on the command-line, where as +** "update" only overwrites if the size or mtime or mode has changed. +*/ +static int arCreateOrUpdateCommand( + ArCommand *pAr, /* Command arguments and options */ + int bUpdate, /* true for a --create. */ + int bOnlyIfChanged /* Only update if file has changed */ +){ + const char *zCreate = + "CREATE TABLE IF NOT EXISTS sqlar(\n" + " name TEXT PRIMARY KEY, -- name of the file\n" + " mode INT, -- access permissions\n" + " mtime INT, -- last modification time\n" + " sz INT, -- original file size\n" + " data BLOB -- compressed content\n" + ")"; + const char *zDrop = "DROP TABLE IF EXISTS sqlar"; + const char *zInsertFmt[2] = { + "REPLACE INTO %s(name,mode,mtime,sz,data)\n" + " SELECT\n" + " %s,\n" + " mode,\n" + " mtime,\n" + " CASE substr(lsmode(mode),1,1)\n" + " WHEN '-' THEN length(data)\n" + " WHEN 'd' THEN 0\n" + " ELSE -1 END,\n" + " sqlar_compress(data)\n" + " FROM fsdir(%Q,%Q) AS disk\n" + " WHERE lsmode(mode) NOT LIKE '?%%'%s;" + , + "REPLACE INTO %s(name,mode,mtime,data)\n" + " SELECT\n" + " %s,\n" + " mode,\n" + " mtime,\n" + " data\n" + " FROM fsdir(%Q,%Q) AS disk\n" + " WHERE lsmode(mode) NOT LIKE '?%%'%s;" + }; + int i; /* For iterating through azFile[] */ + int rc; /* Return code */ + const char *zTab = 0; /* SQL table into which to insert */ + char *zSql; + char zTemp[50]; + char *zExists = 0; + + arExecSql(pAr, "PRAGMA page_size=512"); + rc = arExecSql(pAr, "SAVEPOINT ar;"); + if( rc!=SQLITE_OK ) return rc; + zTemp[0] = 0; + if( pAr->bZip ){ + /* Initialize the zipfile virtual table, if necessary */ + if( pAr->zFile ){ + sqlite3_uint64 r; + sqlite3_randomness(sizeof(r),&r); + sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r); + zTab = zTemp; + zSql = sqlite3_mprintf( + "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)", + zTab, pAr->zFile + ); + rc = arExecSql(pAr, zSql); + sqlite3_free(zSql); + }else{ + zTab = "zip"; + } + }else{ + /* Initialize the table for an SQLAR */ + zTab = "sqlar"; + if( bUpdate==0 ){ + rc = arExecSql(pAr, zDrop); + if( rc!=SQLITE_OK ) goto end_ar_transaction; + } + rc = arExecSql(pAr, zCreate); + } + if( bOnlyIfChanged ){ + zExists = sqlite3_mprintf( + " AND NOT EXISTS(" + "SELECT 1 FROM %s AS mem" + " WHERE mem.name=disk.name" + " AND mem.mtime=disk.mtime" + " AND mem.mode=disk.mode)", zTab); + }else{ + zExists = sqlite3_mprintf(""); + } + if( zExists==0 ) rc = SQLITE_NOMEM; + for(i=0; inArg && rc==SQLITE_OK; i++){ + char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab, + pAr->bVerbose ? "shell_putsnl(name)" : "name", + pAr->azArg[i], pAr->zDir, zExists); + rc = arExecSql(pAr, zSql2); + sqlite3_free(zSql2); + } +end_ar_transaction: + if( rc!=SQLITE_OK ){ + sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0); + }else{ + rc = arExecSql(pAr, "RELEASE ar;"); + if( pAr->bZip && pAr->zFile ){ + zSql = sqlite3_mprintf("DROP TABLE %s", zTemp); + arExecSql(pAr, zSql); + sqlite3_free(zSql); + } + } + sqlite3_free(zExists); + return rc; +} + +/* +** Implementation of ".ar" dot command. +*/ +static int arDotCommand( + ShellState *pState, /* Current shell tool state */ + int fromCmdLine, /* True if -A command-line option, not .ar cmd */ + char **azArg, /* Array of arguments passed to dot command */ + int nArg /* Number of entries in azArg[] */ +){ + ArCommand cmd; + int rc; + memset(&cmd, 0, sizeof(cmd)); + cmd.fromCmdLine = fromCmdLine; + rc = arParseCommand(azArg, nArg, &cmd); + if( rc==SQLITE_OK ){ + int eDbType = SHELL_OPEN_UNSPEC; + cmd.p = pState; + cmd.out = pState->out; + cmd.db = pState->db; + if( cmd.zFile ){ + eDbType = deduceDatabaseType(cmd.zFile, 1, 0); + }else{ + eDbType = pState->openMode; + } + if( eDbType==SHELL_OPEN_ZIPFILE ){ + if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){ + if( cmd.zFile==0 ){ + cmd.zSrcTable = sqlite3_mprintf("zip"); + }else{ + cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile); + } + } + cmd.bZip = 1; + }else if( cmd.zFile ){ + int flags; + if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS; + if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT + || cmd.eCmd==AR_CMD_REMOVE || cmd.eCmd==AR_CMD_UPDATE ){ + flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE; + }else{ + flags = SQLITE_OPEN_READONLY; + } + cmd.db = 0; + if( cmd.bDryRun ){ + sqlite3_fprintf(cmd.out, "-- open database '%s'%s\n", cmd.zFile, + eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : ""); + } + rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags, + eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0); + if( rc!=SQLITE_OK ){ + sqlite3_fprintf(stderr, "cannot open file: %s (%s)\n", + cmd.zFile, sqlite3_errmsg(cmd.db)); + goto end_ar_command; + } + sqlite3_fileio_init(cmd.db, 0, 0); + sqlite3_sqlar_init(cmd.db, 0, 0); + sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p, + shellPutsFunc, 0, 0); + + } + if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){ + if( cmd.eCmd!=AR_CMD_CREATE + && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0) + ){ + sqlite3_fprintf(stderr, "database does not contain an 'sqlar' table\n"); + rc = SQLITE_ERROR; + goto end_ar_command; + } + cmd.zSrcTable = sqlite3_mprintf("sqlar"); + } + + switch( cmd.eCmd ){ + case AR_CMD_CREATE: + rc = arCreateOrUpdateCommand(&cmd, 0, 0); + break; + + case AR_CMD_EXTRACT: + rc = arExtractCommand(&cmd); + break; + + case AR_CMD_LIST: + rc = arListCommand(&cmd); + break; + + case AR_CMD_HELP: + arUsage(pState->out); + break; + + case AR_CMD_INSERT: + rc = arCreateOrUpdateCommand(&cmd, 1, 0); + break; + + case AR_CMD_REMOVE: + rc = arRemoveCommand(&cmd); + break; + + default: + assert( cmd.eCmd==AR_CMD_UPDATE ); + rc = arCreateOrUpdateCommand(&cmd, 1, 1); + break; + } + } +end_ar_command: + if( cmd.db!=pState->db ){ + close_db(cmd.db); + } + sqlite3_free(cmd.zSrcTable); + + return rc; +} +/* End of the ".archive" or ".ar" command logic +*******************************************************************************/ +#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */ + +#if SQLITE_SHELL_HAVE_RECOVER + +/* +** This function is used as a callback by the recover extension. Simply +** print the supplied SQL statement to stdout. +*/ +static int recoverSqlCb(void *pCtx, const char *zSql){ + ShellState *pState = (ShellState*)pCtx; + sqlite3_fprintf(pState->out, "%s;\n", zSql); + return SQLITE_OK; +} + +/* +** This function is called to recover data from the database. A script +** to construct a new database containing all recovered data is output +** on stream pState->out. +*/ +static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){ + int rc = SQLITE_OK; + const char *zRecoveryDb = ""; /* Name of "recovery" database. Debug only */ + const char *zLAF = "lost_and_found"; + int bFreelist = 1; /* 0 if --ignore-freelist is specified */ + int bRowids = 1; /* 0 if --no-rowids */ + sqlite3_recover *p = 0; + int i = 0; + + for(i=1; iout, azArg[0]); + return 1; + } + } + + p = sqlite3_recover_init_sql( + pState->db, "main", recoverSqlCb, (void*)pState + ); + + sqlite3_recover_config(p, 789, (void*)zRecoveryDb); /* Debug use only */ + sqlite3_recover_config(p, SQLITE_RECOVER_LOST_AND_FOUND, (void*)zLAF); + sqlite3_recover_config(p, SQLITE_RECOVER_ROWIDS, (void*)&bRowids); + sqlite3_recover_config(p, SQLITE_RECOVER_FREELIST_CORRUPT,(void*)&bFreelist); + + sqlite3_fprintf(pState->out, ".dbconfig defensive off\n"); + sqlite3_recover_run(p); + if( sqlite3_recover_errcode(p)!=SQLITE_OK ){ + const char *zErr = sqlite3_recover_errmsg(p); + int errCode = sqlite3_recover_errcode(p); + sqlite3_fprintf(stderr,"sql error: %s (%d)\n", zErr, errCode); + } + rc = sqlite3_recover_finish(p); + return rc; +} +#endif /* SQLITE_SHELL_HAVE_RECOVER */ + +/* +** Implementation of ".intck STEPS_PER_UNLOCK" command. +*/ +static int intckDatabaseCmd(ShellState *pState, i64 nStepPerUnlock){ + sqlite3_intck *p = 0; + int rc = SQLITE_OK; + + rc = sqlite3_intck_open(pState->db, "main", &p); + if( rc==SQLITE_OK ){ + i64 nStep = 0; + i64 nError = 0; + const char *zErr = 0; + while( SQLITE_OK==sqlite3_intck_step(p) ){ + const char *zMsg = sqlite3_intck_message(p); + if( zMsg ){ + sqlite3_fprintf(pState->out, "%s\n", zMsg); + nError++; + } + nStep++; + if( nStepPerUnlock && (nStep % nStepPerUnlock)==0 ){ + sqlite3_intck_unlock(p); + } + } + rc = sqlite3_intck_error(p, &zErr); + if( zErr ){ + sqlite3_fprintf(stderr,"%s\n", zErr); + } + sqlite3_intck_close(p); + + sqlite3_fprintf(pState->out, "%lld steps, %lld errors\n", nStep, nError); + } + + return rc; +} + +/* + * zAutoColumn(zCol, &db, ?) => Maybe init db, add column zCol to it. + * zAutoColumn(0, &db, ?) => (db!=0) Form columns spec for CREATE TABLE, + * close db and set it to 0, and return the columns spec, to later + * be sqlite3_free()'ed by the caller. + * The return is 0 when either: + * (a) The db was not initialized and zCol==0 (There are no columns.) + * (b) zCol!=0 (Column was added, db initialized as needed.) + * The 3rd argument, pRenamed, references an out parameter. If the + * pointer is non-zero, its referent will be set to a summary of renames + * done if renaming was necessary, or set to 0 if none was done. The out + * string (if any) must be sqlite3_free()'ed by the caller. + */ +#ifdef SHELL_DEBUG +#define rc_err_oom_die(rc) \ + if( rc==SQLITE_NOMEM ) shell_check_oom(0); \ + else if(!(rc==SQLITE_OK||rc==SQLITE_DONE)) \ + sqlite3_fprintf(stderr,"E:%d\n",rc), assert(0) +#else +static void rc_err_oom_die(int rc){ + if( rc==SQLITE_NOMEM ) shell_check_oom(0); + assert(rc==SQLITE_OK||rc==SQLITE_DONE); +} +#endif + +#ifdef SHELL_COLFIX_DB /* If this is set, the DB can be in a file. */ +static char zCOL_DB[] = SHELL_STRINGIFY(SHELL_COLFIX_DB); +#else /* Otherwise, memory is faster/better for the transient DB. */ +static const char *zCOL_DB = ":memory:"; +#endif + +/* Define character (as C string) to separate generated column ordinal + * from protected part of incoming column names. This defaults to "_" + * so that incoming column identifiers that did not need not be quoted + * remain usable without being quoted. It must be one character. + */ +#ifndef SHELL_AUTOCOLUMN_SEP +# define AUTOCOLUMN_SEP "_" +#else +# define AUTOCOLUMN_SEP SHELL_STRINGIFY(SHELL_AUTOCOLUMN_SEP) +#endif + +static char *zAutoColumn(const char *zColNew, sqlite3 **pDb, char **pzRenamed){ + /* Queries and D{D,M}L used here */ + static const char * const zTabMake = "\ +CREATE TABLE ColNames(\ + cpos INTEGER PRIMARY KEY,\ + name TEXT, nlen INT, chop INT, reps INT, suff TEXT);\ +CREATE VIEW RepeatedNames AS \ +SELECT DISTINCT t.name FROM ColNames t \ +WHERE t.name COLLATE NOCASE IN (\ + SELECT o.name FROM ColNames o WHERE o.cpos<>t.cpos\ +);\ +"; + static const char * const zTabFill = "\ +INSERT INTO ColNames(name,nlen,chop,reps,suff)\ + VALUES(iif(length(?1)>0,?1,'?'),max(length(?1),1),0,0,'')\ +"; + static const char * const zHasDupes = "\ +SELECT count(DISTINCT (substring(name,1,nlen-chop)||suff) COLLATE NOCASE)\ + 1, printf('%c%0*d', '"AUTOCOLUMN_SEP"', $1, cpos), '')" +#else /* ...RENAME_MINIMAL_ONE_PASS */ +"WITH Lzn(nlz) AS (" /* Find minimum extraneous leading 0's for uniqueness */ +" SELECT 0 AS nlz" +" UNION" +" SELECT nlz+1 AS nlz FROM Lzn" +" WHERE EXISTS(" +" SELECT 1" +" FROM ColNames t, ColNames o" +" WHERE" +" iif(t.name IN (SELECT * FROM RepeatedNames)," +" printf('%s"AUTOCOLUMN_SEP"%s'," +" t.name, substring(printf('%.*c%0.*d',nlz+1,'0',$1,t.cpos),2))," +" t.name" +" )" +" =" +" iif(o.name IN (SELECT * FROM RepeatedNames)," +" printf('%s"AUTOCOLUMN_SEP"%s'," +" o.name, substring(printf('%.*c%0.*d',nlz+1,'0',$1,o.cpos),2))," +" o.name" +" )" +" COLLATE NOCASE" +" AND o.cpos<>t.cpos" +" GROUP BY t.cpos" +" )" +") UPDATE Colnames AS t SET" +" chop = 0," /* No chopping, never touch incoming names. */ +" suff = iif(name IN (SELECT * FROM RepeatedNames)," +" printf('"AUTOCOLUMN_SEP"%s', substring(" +" printf('%.*c%0.*d',(SELECT max(nlz) FROM Lzn)+1,'0',1,t.cpos),2))," +" ''" +" )" +#endif + ; + static const char * const zCollectVar = "\ +SELECT\ + '('||x'0a'\ + || group_concat(\ + cname||' TEXT',\ + ','||iif((cpos-1)%4>0, ' ', x'0a'||' '))\ + ||')' AS ColsSpec \ +FROM (\ + SELECT cpos, printf('\"%w\"',printf('%!.*s%s', nlen-chop,name,suff)) AS cname \ + FROM ColNames ORDER BY cpos\ +)"; + static const char * const zRenamesDone = + "SELECT group_concat(" + " printf('\"%w\" to \"%w\"',name,printf('%!.*s%s', nlen-chop, name, suff))," + " ','||x'0a')" + "FROM ColNames WHERE suff<>'' OR chop!=0" + ; + int rc; + sqlite3_stmt *pStmt = 0; + assert(pDb!=0); + if( zColNew ){ + /* Add initial or additional column. Init db if necessary. */ + if( *pDb==0 ){ + if( SQLITE_OK!=sqlite3_open(zCOL_DB, pDb) ) return 0; +#ifdef SHELL_COLFIX_DB + if(*zCOL_DB!=':') + sqlite3_exec(*pDb,"drop table if exists ColNames;" + "drop view if exists RepeatedNames;",0,0,0); +#endif +#undef SHELL_COLFIX_DB + rc = sqlite3_exec(*pDb, zTabMake, 0, 0, 0); + rc_err_oom_die(rc); + } + assert(*pDb!=0); + rc = sqlite3_prepare_v2(*pDb, zTabFill, -1, &pStmt, 0); + rc_err_oom_die(rc); + rc = sqlite3_bind_text(pStmt, 1, zColNew, -1, 0); + rc_err_oom_die(rc); + rc = sqlite3_step(pStmt); + rc_err_oom_die(rc); + sqlite3_finalize(pStmt); + return 0; + }else if( *pDb==0 ){ + return 0; + }else{ + /* Formulate the columns spec, close the DB, zero *pDb. */ + char *zColsSpec = 0; + int hasDupes = db_int(*pDb, "%s", zHasDupes); + int nDigits = (hasDupes)? db_int(*pDb, "%s", zColDigits) : 0; + if( hasDupes ){ +#ifdef SHELL_COLUMN_RENAME_CLEAN + rc = sqlite3_exec(*pDb, zDedoctor, 0, 0, 0); + rc_err_oom_die(rc); +#endif + rc = sqlite3_exec(*pDb, zSetReps, 0, 0, 0); + rc_err_oom_die(rc); + rc = sqlite3_prepare_v2(*pDb, zRenameRank, -1, &pStmt, 0); + rc_err_oom_die(rc); + sqlite3_bind_int(pStmt, 1, nDigits); + rc = sqlite3_step(pStmt); + sqlite3_finalize(pStmt); + if( rc!=SQLITE_DONE ) rc_err_oom_die(SQLITE_NOMEM); + } + assert(db_int(*pDb, "%s", zHasDupes)==0); /* Consider: remove this */ + rc = sqlite3_prepare_v2(*pDb, zCollectVar, -1, &pStmt, 0); + rc_err_oom_die(rc); + rc = sqlite3_step(pStmt); + if( rc==SQLITE_ROW ){ + zColsSpec = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0)); + }else{ + zColsSpec = 0; + } + if( pzRenamed!=0 ){ + if( !hasDupes ) *pzRenamed = 0; + else{ + sqlite3_finalize(pStmt); + if( SQLITE_OK==sqlite3_prepare_v2(*pDb, zRenamesDone, -1, &pStmt, 0) + && SQLITE_ROW==sqlite3_step(pStmt) ){ + *pzRenamed = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0)); + }else + *pzRenamed = 0; + } + } + sqlite3_finalize(pStmt); + sqlite3_close(*pDb); + *pDb = 0; + return zColsSpec; + } +} + +/* +** Check if the sqlite_schema table contains one or more virtual tables. If +** parameter zLike is not NULL, then it is an SQL expression that the +** sqlite_schema row must also match. If one or more such rows are found, +** print the following warning to the output: +** +** WARNING: Script requires that SQLITE_DBCONFIG_DEFENSIVE be disabled +*/ +static int outputDumpWarning(ShellState *p, const char *zLike){ + int rc = SQLITE_OK; + sqlite3_stmt *pStmt = 0; + shellPreparePrintf(p->db, &rc, &pStmt, + "SELECT 1 FROM sqlite_schema o WHERE " + "sql LIKE 'CREATE VIRTUAL TABLE%%' AND %s", zLike ? zLike : "true" + ); + if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){ + sqlite3_fputs("/* WARNING: " + "Script requires that SQLITE_DBCONFIG_DEFENSIVE be disabled */\n", + p->out + ); + } + shellFinalize(&rc, pStmt); + return rc; +} + +/* +** Fault-Simulator state and logic. +*/ +static struct { + int iId; /* ID that triggers a simulated fault. -1 means "any" */ + int iErr; /* The error code to return on a fault */ + int iCnt; /* Trigger the fault only if iCnt is already zero */ + int iInterval; /* Reset iCnt to this value after each fault */ + int eVerbose; /* When to print output */ + int nHit; /* Number of hits seen so far */ + int nRepeat; /* Turn off after this many hits. 0 for never */ + int nSkip; /* Skip this many before first fault */ +} faultsim_state = {-1, 0, 0, 0, 0, 0, 0, 0}; + +/* +** This is the fault-sim callback +*/ +static int faultsim_callback(int iArg){ + if( faultsim_state.iId>0 && faultsim_state.iId!=iArg ){ + return SQLITE_OK; + } + if( faultsim_state.iCnt ){ + if( faultsim_state.iCnt>0 ) faultsim_state.iCnt--; + if( faultsim_state.eVerbose>=2 ){ + sqlite3_fprintf(stdout, + "FAULT-SIM id=%d no-fault (cnt=%d)\n", iArg, faultsim_state.iCnt); + } + return SQLITE_OK; + } + if( faultsim_state.eVerbose>=1 ){ + sqlite3_fprintf(stdout, + "FAULT-SIM id=%d returns %d\n", iArg, faultsim_state.iErr); + } + faultsim_state.iCnt = faultsim_state.iInterval; + faultsim_state.nHit++; + if( faultsim_state.nRepeat>0 && faultsim_state.nRepeat<=faultsim_state.nHit ){ + faultsim_state.iCnt = -1; + } + return faultsim_state.iErr; +} + +/* +** If an input line begins with "." then invoke this routine to +** process that line. +** +** Return 1 on error, 2 to exit, and 0 otherwise. +*/ +static int do_meta_command(char *zLine, ShellState *p){ + int h = 1; + int nArg = 0; + int n, c; + int rc = 0; + char *azArg[52]; + +#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_AUTHORIZATION) + if( p->expert.pExpert ){ + expertFinish(p, 1, 0); + } +#endif + + /* Parse the input line into tokens. + */ + while( zLine[h] && nArgdb, shellAuth, p); + }else if( p->bSafeModePersist ){ + sqlite3_set_authorizer(p->db, safeModeAuth, p); + }else{ + sqlite3_set_authorizer(p->db, 0, 0); + } + }else +#endif + +#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) \ + && !defined(SQLITE_SHELL_FIDDLE) + if( c=='a' && cli_strncmp(azArg[0], "archive", n)==0 ){ + open_db(p, 0); + failIfSafeMode(p, "cannot run .archive in safe mode"); + rc = arDotCommand(p, 0, azArg, nArg); + }else +#endif + +#ifndef SQLITE_SHELL_FIDDLE + if( (c=='b' && n>=3 && cli_strncmp(azArg[0], "backup", n)==0) + || (c=='s' && n>=3 && cli_strncmp(azArg[0], "save", n)==0) + ){ + const char *zDestFile = 0; + const char *zDb = 0; + sqlite3 *pDest; + sqlite3_backup *pBackup; + int j; + int bAsync = 0; + const char *zVfs = 0; + failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]); + for(j=1; jdb, zDb); + if( pBackup==0 ){ + shellDatabaseError(pDest); + close_db(pDest); + return 1; + } + while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){} + sqlite3_backup_finish(pBackup); + if( rc==SQLITE_DONE ){ + rc = 0; + }else{ + shellDatabaseError(pDest); + rc = 1; + } + close_db(pDest); + }else +#endif /* !defined(SQLITE_SHELL_FIDDLE) */ + + if( c=='b' && n>=3 && cli_strncmp(azArg[0], "bail", n)==0 ){ + if( nArg==2 ){ + bail_on_error = booleanValue(azArg[1]); + }else{ + eputz("Usage: .bail on|off\n"); + rc = 1; + } + }else + + /* Undocumented. Legacy only. See "crlf" below */ + if( c=='b' && n>=3 && cli_strncmp(azArg[0], "binary", n)==0 ){ + eputz("The \".binary\" command is deprecated.\n"); + rc = 1; + }else + + /* The undocumented ".breakpoint" command causes a call to the no-op + ** routine named test_breakpoint(). + */ + if( c=='b' && n>=3 && cli_strncmp(azArg[0], "breakpoint", n)==0 ){ + test_breakpoint(); + }else + +#ifndef SQLITE_SHELL_FIDDLE + if( c=='c' && cli_strcmp(azArg[0],"cd")==0 ){ + failIfSafeMode(p, "cannot run .cd in safe mode"); + if( nArg==2 ){ +#if defined(_WIN32) || defined(WIN32) + wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]); + rc = !SetCurrentDirectoryW(z); + sqlite3_free(z); +#else + rc = chdir(azArg[1]); +#endif + if( rc ){ + sqlite3_fprintf(stderr,"Cannot change to directory \"%s\"\n", azArg[1]); + rc = 1; + } + }else{ + eputz("Usage: .cd DIRECTORY\n"); + rc = 1; + } + }else +#endif /* !defined(SQLITE_SHELL_FIDDLE) */ + + if( c=='c' && n>=3 && cli_strncmp(azArg[0], "changes", n)==0 ){ + if( nArg==2 ){ + setOrClearFlag(p, SHFLG_CountChanges, azArg[1]); + }else{ + eputz("Usage: .changes on|off\n"); + rc = 1; + } + }else + +#ifndef SQLITE_SHELL_FIDDLE + /* Cancel output redirection, if it is currently set (by .testcase) + ** Then read the content of the testcase-out.txt file and compare against + ** azArg[1]. If there are differences, report an error and exit. + */ + if( c=='c' && n>=3 && cli_strncmp(azArg[0], "check", n)==0 ){ + char *zRes = 0; + output_reset(p); + if( nArg!=2 ){ + eputz("Usage: .check GLOB-PATTERN\n"); + rc = 2; + }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){ + rc = 2; + }else if( testcase_glob(azArg[1],zRes)==0 ){ + sqlite3_fprintf(stderr, + "testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n", + p->zTestcase, azArg[1], zRes); + rc = 1; + }else{ + sqlite3_fprintf(p->out, "testcase-%s ok\n", p->zTestcase); + p->nCheck++; + } + sqlite3_free(zRes); + }else +#endif /* !defined(SQLITE_SHELL_FIDDLE) */ + +#ifndef SQLITE_SHELL_FIDDLE + if( c=='c' && cli_strncmp(azArg[0], "clone", n)==0 ){ + failIfSafeMode(p, "cannot run .clone in safe mode"); + if( nArg==2 ){ + tryToClone(p, azArg[1]); + }else{ + eputz("Usage: .clone FILENAME\n"); + rc = 1; + } + }else +#endif /* !defined(SQLITE_SHELL_FIDDLE) */ + + if( c=='c' && cli_strncmp(azArg[0], "connection", n)==0 ){ + if( nArg==1 ){ + /* List available connections */ + int i; + for(i=0; iaAuxDb); i++){ + const char *zFile = p->aAuxDb[i].zDbFilename; + if( p->aAuxDb[i].db==0 && p->pAuxDb!=&p->aAuxDb[i] ){ + zFile = "(not open)"; + }else if( zFile==0 ){ + zFile = "(memory)"; + }else if( zFile[0]==0 ){ + zFile = "(temporary-file)"; + } + if( p->pAuxDb == &p->aAuxDb[i] ){ + sqlite3_fprintf(stdout, "ACTIVE %d: %s\n", i, zFile); + }else if( p->aAuxDb[i].db!=0 ){ + sqlite3_fprintf(stdout, " %d: %s\n", i, zFile); + } + } + }else if( nArg==2 && IsDigit(azArg[1][0]) && azArg[1][1]==0 ){ + int i = azArg[1][0] - '0'; + if( p->pAuxDb != &p->aAuxDb[i] && i>=0 && iaAuxDb) ){ + p->pAuxDb->db = p->db; + p->pAuxDb = &p->aAuxDb[i]; + globalDb = p->db = p->pAuxDb->db; + p->pAuxDb->db = 0; + } + }else if( nArg==3 && cli_strcmp(azArg[1], "close")==0 + && IsDigit(azArg[2][0]) && azArg[2][1]==0 ){ + int i = azArg[2][0] - '0'; + if( i<0 || i>=ArraySize(p->aAuxDb) ){ + /* No-op */ + }else if( p->pAuxDb == &p->aAuxDb[i] ){ + eputz("cannot close the active database connection\n"); + rc = 1; + }else if( p->aAuxDb[i].db ){ + session_close_all(p, i); + close_db(p->aAuxDb[i].db); + p->aAuxDb[i].db = 0; + } + }else{ + eputz("Usage: .connection [close] [CONNECTION-NUMBER]\n"); + rc = 1; + } + }else + + if( c=='c' && n==4 + && (cli_strncmp(azArg[0], "crlf", n)==0 + || cli_strncmp(azArg[0], "crnl",n)==0) + ){ + if( nArg==2 ){ +#ifdef _WIN32 + p->crlfMode = booleanValue(azArg[1]); +#else + p->crlfMode = 0; +#endif + } + sqlite3_fprintf(stderr, "crlf is %s\n", p->crlfMode ? "ON" : "OFF"); + }else + + if( c=='d' && n>1 && cli_strncmp(azArg[0], "databases", n)==0 ){ + char **azName = 0; + int nName = 0; + sqlite3_stmt *pStmt; + int i; + open_db(p, 0); + rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0); + if( rc ){ + shellDatabaseError(p->db); + rc = 1; + }else{ + while( sqlite3_step(pStmt)==SQLITE_ROW ){ + const char *zSchema = (const char *)sqlite3_column_text(pStmt,1); + const char *zFile = (const char*)sqlite3_column_text(pStmt,2); + if( zSchema==0 || zFile==0 ) continue; + azName = sqlite3_realloc64(azName, (nName+1)*2*sizeof(char*)); + shell_check_oom(azName); + azName[nName*2] = strdup(zSchema); + azName[nName*2+1] = strdup(zFile); + nName++; + } + } + sqlite3_finalize(pStmt); + for(i=0; idb, azName[i*2]); + int bRdonly = sqlite3_db_readonly(p->db, azName[i*2]); + const char *z = azName[i*2+1]; + sqlite3_fprintf(p->out, "%s: %s %s%s\n", + azName[i*2], z && z[0] ? z : "\"\"", bRdonly ? "r/o" : "r/w", + eTxn==SQLITE_TXN_NONE ? "" : + eTxn==SQLITE_TXN_READ ? " read-txn" : " write-txn"); + free(azName[i*2]); + free(azName[i*2+1]); + } + sqlite3_free(azName); + }else + + if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbconfig", n)==0 ){ + static const struct DbConfigChoices { + const char *zName; + int op; + } aDbConfig[] = { + { "attach_create", SQLITE_DBCONFIG_ENABLE_ATTACH_CREATE }, + { "attach_write", SQLITE_DBCONFIG_ENABLE_ATTACH_WRITE }, + { "comments", SQLITE_DBCONFIG_ENABLE_COMMENTS }, + { "defensive", SQLITE_DBCONFIG_DEFENSIVE }, + { "dqs_ddl", SQLITE_DBCONFIG_DQS_DDL }, + { "dqs_dml", SQLITE_DBCONFIG_DQS_DML }, + { "enable_fkey", SQLITE_DBCONFIG_ENABLE_FKEY }, + { "enable_qpsg", SQLITE_DBCONFIG_ENABLE_QPSG }, + { "enable_trigger", SQLITE_DBCONFIG_ENABLE_TRIGGER }, + { "enable_view", SQLITE_DBCONFIG_ENABLE_VIEW }, + { "fts3_tokenizer", SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER }, + { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE }, + { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT }, + { "load_extension", SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION }, + { "no_ckpt_on_close", SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE }, + { "reset_database", SQLITE_DBCONFIG_RESET_DATABASE }, + { "reverse_scanorder", SQLITE_DBCONFIG_REVERSE_SCANORDER }, + { "stmt_scanstatus", SQLITE_DBCONFIG_STMT_SCANSTATUS }, + { "trigger_eqp", SQLITE_DBCONFIG_TRIGGER_EQP }, + { "trusted_schema", SQLITE_DBCONFIG_TRUSTED_SCHEMA }, + { "writable_schema", SQLITE_DBCONFIG_WRITABLE_SCHEMA }, + }; + int ii, v; + open_db(p, 0); + for(ii=0; ii1 && cli_strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue; + if( nArg>=3 ){ + sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0); + } + sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v); + sqlite3_fprintf(p->out, "%19s %s\n", + aDbConfig[ii].zName, v ? "on" : "off"); + if( nArg>1 ) break; + } + if( nArg>1 && ii==ArraySize(aDbConfig) ){ + sqlite3_fprintf(stderr,"Error: unknown dbconfig \"%s\"\n", azArg[1]); + eputz("Enter \".dbconfig\" with no arguments for a list\n"); + } + }else + +#if SQLITE_SHELL_HAVE_RECOVER + if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbinfo", n)==0 ){ + rc = shell_dbinfo_command(p, nArg, azArg); + }else + + if( c=='r' && cli_strncmp(azArg[0], "recover", n)==0 ){ + open_db(p, 0); + rc = recoverDatabaseCmd(p, nArg, azArg); + }else +#endif /* SQLITE_SHELL_HAVE_RECOVER */ + + if( c=='d' && cli_strncmp(azArg[0], "dump", n)==0 ){ + char *zLike = 0; + char *zSql; + int i; + int savedShowHeader = p->showHeader; + int savedShellFlags = p->shellFlgs; + ShellClearFlag(p, + SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo + |SHFLG_DumpDataOnly|SHFLG_DumpNoSys); + for(i=1; ishellFlgs & SHFLG_DumpDataOnly)==0 ){ + /* When playing back a "dump", the content might appear in an order + ** which causes immediate foreign key constraints to be violated. + ** So disable foreign-key constraint enforcement to prevent problems. */ + sqlite3_fputs("PRAGMA foreign_keys=OFF;\n", p->out); + sqlite3_fputs("BEGIN TRANSACTION;\n", p->out); + } + p->writableSchema = 0; + p->showHeader = 0; + /* Set writable_schema=ON since doing so forces SQLite to initialize + ** as much of the schema as it can even if the sqlite_schema table is + ** corrupt. */ + sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0); + p->nErr = 0; + if( zLike==0 ) zLike = sqlite3_mprintf("true"); + zSql = sqlite3_mprintf( + "SELECT name, type, sql FROM sqlite_schema AS o " + "WHERE (%s) AND type=='table'" + " AND sql NOT NULL" + " ORDER BY tbl_name='sqlite_sequence', rowid", + zLike + ); + run_schema_dump_query(p,zSql); + sqlite3_free(zSql); + if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){ + zSql = sqlite3_mprintf( + "SELECT sql FROM sqlite_schema AS o " + "WHERE (%s) AND sql NOT NULL" + " AND type IN ('index','trigger','view') " + "ORDER BY type COLLATE NOCASE DESC", + zLike + ); + run_table_dump_query(p, zSql); + sqlite3_free(zSql); + } + sqlite3_free(zLike); + if( p->writableSchema ){ + sqlite3_fputs("PRAGMA writable_schema=OFF;\n", p->out); + p->writableSchema = 0; + } + sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0); + sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0); + if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){ + sqlite3_fputs(p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n", p->out); + } + p->showHeader = savedShowHeader; + p->shellFlgs = savedShellFlags; + rc = p->nErr>0; + }else + + if( c=='e' && cli_strncmp(azArg[0], "echo", n)==0 ){ + if( nArg==2 ){ + setOrClearFlag(p, SHFLG_Echo, azArg[1]); + }else{ + eputz("Usage: .echo on|off\n"); + rc = 1; + } + }else + + if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbtotxt", n)==0 ){ + open_db(p, 0); + rc = shell_dbtotxt_command(p, nArg, azArg); + }else + + if( c=='e' && cli_strncmp(azArg[0], "eqp", n)==0 ){ + if( nArg==2 ){ + p->autoEQPtest = 0; + if( p->autoEQPtrace ){ + if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0); + p->autoEQPtrace = 0; + } + if( cli_strcmp(azArg[1],"full")==0 ){ + p->autoEQP = AUTOEQP_full; + }else if( cli_strcmp(azArg[1],"trigger")==0 ){ + p->autoEQP = AUTOEQP_trigger; +#ifdef SQLITE_DEBUG + }else if( cli_strcmp(azArg[1],"test")==0 ){ + p->autoEQP = AUTOEQP_on; + p->autoEQPtest = 1; + }else if( cli_strcmp(azArg[1],"trace")==0 ){ + p->autoEQP = AUTOEQP_full; + p->autoEQPtrace = 1; + open_db(p, 0); + sqlite3_exec(p->db, "SELECT name FROM sqlite_schema LIMIT 1", 0, 0, 0); + sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0); +#endif + }else{ + p->autoEQP = (u8)booleanValue(azArg[1]); + } + }else{ + eputz("Usage: .eqp off|on|trace|trigger|full\n"); + rc = 1; + } + }else + +#ifndef SQLITE_SHELL_FIDDLE + if( c=='e' && cli_strncmp(azArg[0], "exit", n)==0 ){ + if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc); + rc = 2; + }else +#endif + + /* The ".explain" command is automatic now. It is largely pointless. It + ** retained purely for backwards compatibility */ + if( c=='e' && cli_strncmp(azArg[0], "explain", n)==0 ){ + int val = 1; + if( nArg>=2 ){ + if( cli_strcmp(azArg[1],"auto")==0 ){ + val = 99; + }else{ + val = booleanValue(azArg[1]); + } + } + if( val==1 && p->mode!=MODE_Explain ){ + p->normalMode = p->mode; + p->mode = MODE_Explain; + p->autoExplain = 0; + }else if( val==0 ){ + if( p->mode==MODE_Explain ) p->mode = p->normalMode; + p->autoExplain = 0; + }else if( val==99 ){ + if( p->mode==MODE_Explain ) p->mode = p->normalMode; + p->autoExplain = 1; + } + }else + +#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_AUTHORIZATION) + if( c=='e' && cli_strncmp(azArg[0], "expert", n)==0 ){ + if( p->bSafeMode ){ + sqlite3_fprintf(stderr, + "Cannot run experimental commands such as \"%s\" in safe mode\n", + azArg[0]); + rc = 1; + }else{ + open_db(p, 0); + expertDotCommand(p, azArg, nArg); + } + }else +#endif + + if( c=='f' && cli_strncmp(azArg[0], "filectrl", n)==0 ){ + static const struct { + const char *zCtrlName; /* Name of a test-control option */ + int ctrlCode; /* Integer code for that option */ + const char *zUsage; /* Usage notes */ + } aCtrl[] = { + { "chunk_size", SQLITE_FCNTL_CHUNK_SIZE, "SIZE" }, + { "data_version", SQLITE_FCNTL_DATA_VERSION, "" }, + { "has_moved", SQLITE_FCNTL_HAS_MOVED, "" }, + { "lock_timeout", SQLITE_FCNTL_LOCK_TIMEOUT, "MILLISEC" }, + { "persist_wal", SQLITE_FCNTL_PERSIST_WAL, "[BOOLEAN]" }, + /* { "pragma", SQLITE_FCNTL_PRAGMA, "NAME ARG" },*/ + { "psow", SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]" }, + { "reserve_bytes", SQLITE_FCNTL_RESERVE_BYTES, "[N]" }, + { "size_limit", SQLITE_FCNTL_SIZE_LIMIT, "[LIMIT]" }, + { "tempfilename", SQLITE_FCNTL_TEMPFILENAME, "" }, + /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY, "COUNT DELAY" },*/ + }; + int filectrl = -1; + int iCtrl = -1; + sqlite3_int64 iRes = 0; /* Integer result to display if rc2==1 */ + int isOk = 0; /* 0: usage 1: %lld 2: no-result */ + int n2, i; + const char *zCmd = 0; + const char *zSchema = 0; + + open_db(p, 0); + zCmd = nArg>=2 ? azArg[1] : "help"; + + if( zCmd[0]=='-' + && (cli_strcmp(zCmd,"--schema")==0 || cli_strcmp(zCmd,"-schema")==0) + && nArg>=4 + ){ + zSchema = azArg[2]; + for(i=3; iout); + for(i=0; iout, + " .filectrl %s %s\n", aCtrl[i].zCtrlName, aCtrl[i].zUsage); + } + rc = 1; + goto meta_command_exit; + } + + /* convert filectrl text option to value. allow any unique prefix + ** of the option name, or a numerical value. */ + n2 = strlen30(zCmd); + for(i=0; idb, zSchema, SQLITE_FCNTL_SIZE_LIMIT, &iRes); + isOk = 1; + break; + } + case SQLITE_FCNTL_LOCK_TIMEOUT: + case SQLITE_FCNTL_CHUNK_SIZE: { + int x; + if( nArg!=3 ) break; + x = (int)integerValue(azArg[2]); + sqlite3_file_control(p->db, zSchema, filectrl, &x); + isOk = 2; + break; + } + case SQLITE_FCNTL_PERSIST_WAL: + case SQLITE_FCNTL_POWERSAFE_OVERWRITE: { + int x; + if( nArg!=2 && nArg!=3 ) break; + x = nArg==3 ? booleanValue(azArg[2]) : -1; + sqlite3_file_control(p->db, zSchema, filectrl, &x); + iRes = x; + isOk = 1; + break; + } + case SQLITE_FCNTL_DATA_VERSION: + case SQLITE_FCNTL_HAS_MOVED: { + int x; + if( nArg!=2 ) break; + sqlite3_file_control(p->db, zSchema, filectrl, &x); + iRes = x; + isOk = 1; + break; + } + case SQLITE_FCNTL_TEMPFILENAME: { + char *z = 0; + if( nArg!=2 ) break; + sqlite3_file_control(p->db, zSchema, filectrl, &z); + if( z ){ + sqlite3_fprintf(p->out, "%s\n", z); + sqlite3_free(z); + } + isOk = 2; + break; + } + case SQLITE_FCNTL_RESERVE_BYTES: { + int x; + if( nArg>=3 ){ + x = atoi(azArg[2]); + sqlite3_file_control(p->db, zSchema, filectrl, &x); + } + x = -1; + sqlite3_file_control(p->db, zSchema, filectrl, &x); + sqlite3_fprintf(p->out, "%d\n", x); + isOk = 2; + break; + } + } + } + if( isOk==0 && iCtrl>=0 ){ + sqlite3_fprintf(p->out, "Usage: .filectrl %s %s\n", + zCmd, aCtrl[iCtrl].zUsage); + rc = 1; + }else if( isOk==1 ){ + char zBuf[100]; + sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes); + sqlite3_fprintf(p->out, "%s\n", zBuf); + } + }else + + if( c=='f' && cli_strncmp(azArg[0], "fullschema", n)==0 ){ + ShellState data; + int doStats = 0; + memcpy(&data, p, sizeof(data)); + data.showHeader = 0; + data.cMode = data.mode = MODE_Semi; + if( nArg==2 && optionMatch(azArg[1], "indent") ){ + data.cMode = data.mode = MODE_Pretty; + nArg = 1; + } + if( nArg!=1 ){ + eputz("Usage: .fullschema ?--indent?\n"); + rc = 1; + goto meta_command_exit; + } + open_db(p, 0); + rc = sqlite3_exec(p->db, + "SELECT sql FROM" + " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x" + " FROM sqlite_schema UNION ALL" + " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_schema) " + "WHERE type!='meta' AND sql NOTNULL" + " AND name NOT LIKE 'sqlite__%' ESCAPE '_' " + "ORDER BY x", + callback, &data, 0 + ); + if( rc==SQLITE_OK ){ + sqlite3_stmt *pStmt; + rc = sqlite3_prepare_v2(p->db, + "SELECT rowid FROM sqlite_schema" + " WHERE name GLOB 'sqlite_stat[134]'", + -1, &pStmt, 0); + if( rc==SQLITE_OK ){ + doStats = sqlite3_step(pStmt)==SQLITE_ROW; + sqlite3_finalize(pStmt); + } + } + if( doStats==0 ){ + sqlite3_fputs("/* No STAT tables available */\n", p->out); + }else{ + sqlite3_fputs("ANALYZE sqlite_schema;\n", p->out); + data.cMode = data.mode = MODE_Insert; + data.zDestTable = "sqlite_stat1"; + shell_exec(&data, "SELECT * FROM sqlite_stat1", 0); + data.zDestTable = "sqlite_stat4"; + shell_exec(&data, "SELECT * FROM sqlite_stat4", 0); + sqlite3_fputs("ANALYZE sqlite_schema;\n", p->out); + } + }else + + if( c=='h' && cli_strncmp(azArg[0], "headers", n)==0 ){ + if( nArg==2 ){ + p->showHeader = booleanValue(azArg[1]); + p->shellFlgs |= SHFLG_HeaderSet; + }else{ + eputz("Usage: .headers on|off\n"); + rc = 1; + } + }else + + if( c=='h' && cli_strncmp(azArg[0], "help", n)==0 ){ + if( nArg>=2 ){ + n = showHelp(p->out, azArg[1]); + if( n==0 ){ + sqlite3_fprintf(p->out, "Nothing matches '%s'\n", azArg[1]); + } + }else{ + showHelp(p->out, 0); + } + }else + +#ifndef SQLITE_SHELL_FIDDLE + if( c=='i' && cli_strncmp(azArg[0], "import", n)==0 ){ + char *zTable = 0; /* Insert data into this table */ + char *zSchema = 0; /* Schema of zTable */ + char *zFile = 0; /* Name of file to extra content from */ + sqlite3_stmt *pStmt = NULL; /* A statement */ + int nCol; /* Number of columns in the table */ + i64 nByte; /* Number of bytes in an SQL string */ + int i, j; /* Loop counters */ + int needCommit; /* True to COMMIT or ROLLBACK at end */ + int nSep; /* Number of bytes in p->colSeparator[] */ + char *zSql = 0; /* An SQL statement */ + ImportCtx sCtx; /* Reader context */ + char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */ + int eVerbose = 0; /* Larger for more console output */ + i64 nSkip = 0; /* Initial lines to skip */ + int useOutputMode = 1; /* Use output mode to determine separators */ + char *zCreate = 0; /* CREATE TABLE statement text */ + + failIfSafeMode(p, "cannot run .import in safe mode"); + memset(&sCtx, 0, sizeof(sCtx)); + if( p->mode==MODE_Ascii ){ + xRead = ascii_read_one_field; + }else{ + xRead = csv_read_one_field; + } + rc = 1; + for(i=1; iout, "ERROR: extra argument: \"%s\". Usage:\n",z); + showHelp(p->out, "import"); + goto meta_command_exit; + } + }else if( cli_strcmp(z,"-v")==0 ){ + eVerbose++; + }else if( cli_strcmp(z,"-schema")==0 && iout, "ERROR: unknown option: \"%s\". Usage:\n", z); + showHelp(p->out, "import"); + goto meta_command_exit; + } + } + if( zTable==0 ){ + sqlite3_fprintf(p->out, "ERROR: missing %s argument. Usage:\n", + zFile==0 ? "FILE" : "TABLE"); + showHelp(p->out, "import"); + goto meta_command_exit; + } + seenInterrupt = 0; + open_db(p, 0); + if( useOutputMode ){ + /* If neither the --csv or --ascii options are specified, then set + ** the column and row separator characters from the output mode. */ + nSep = strlen30(p->colSeparator); + if( nSep==0 ){ + eputz("Error: non-null column separator required for import\n"); + goto meta_command_exit; + } + if( nSep>1 ){ + eputz("Error: multi-character column separators not allowed" + " for import\n"); + goto meta_command_exit; + } + nSep = strlen30(p->rowSeparator); + if( nSep==0 ){ + eputz("Error: non-null row separator required for import\n"); + goto meta_command_exit; + } + if( nSep==2 && p->mode==MODE_Csv + && cli_strcmp(p->rowSeparator,SEP_CrLf)==0 + ){ + /* When importing CSV (only), if the row separator is set to the + ** default output row separator, change it to the default input + ** row separator. This avoids having to maintain different input + ** and output row separators. */ + sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); + nSep = strlen30(p->rowSeparator); + } + if( nSep>1 ){ + eputz("Error: multi-character row separators not allowed" + " for import\n"); + goto meta_command_exit; + } + sCtx.cColSep = (u8)p->colSeparator[0]; + sCtx.cRowSep = (u8)p->rowSeparator[0]; + } + sCtx.zFile = zFile; + sCtx.nLine = 1; + if( sCtx.zFile[0]=='|' ){ +#ifdef SQLITE_OMIT_POPEN + eputz("Error: pipes are not supported in this OS\n"); + goto meta_command_exit; +#else + sCtx.in = sqlite3_popen(sCtx.zFile+1, "r"); + sCtx.zFile = ""; + sCtx.xCloser = pclose; +#endif + }else{ + sCtx.in = sqlite3_fopen(sCtx.zFile, "rb"); + sCtx.xCloser = fclose; + } + if( sCtx.in==0 ){ + sqlite3_fprintf(stderr,"Error: cannot open \"%s\"\n", zFile); + goto meta_command_exit; + } + if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){ + char zSep[2]; + zSep[1] = 0; + zSep[0] = sCtx.cColSep; + sqlite3_fputs("Column separator ", p->out); + output_c_string(p->out, zSep); + sqlite3_fputs(", row separator ", p->out); + zSep[0] = sCtx.cRowSep; + output_c_string(p->out, zSep); + sqlite3_fputs("\n", p->out); + } + sCtx.z = sqlite3_malloc64(120); + if( sCtx.z==0 ){ + import_cleanup(&sCtx); + shell_out_of_memory(); + } + /* Below, resources must be freed before exit. */ + while( nSkip>0 ){ + nSkip--; + while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){} + } + import_append_char(&sCtx, 0); /* To ensure sCtx.z is allocated */ + if( sqlite3_table_column_metadata(p->db, zSchema, zTable,0,0,0,0,0,0) + && 0==db_int(p->db, "SELECT count(*) FROM \"%w\".sqlite_schema" + " WHERE name=%Q AND type='view'", + zSchema ? zSchema : "main", zTable) + ){ + /* Table does not exist. Create it. */ + sqlite3 *dbCols = 0; + char *zRenames = 0; + char *zColDefs; + zCreate = sqlite3_mprintf("CREATE TABLE \"%w\".\"%w\"", + zSchema ? zSchema : "main", zTable); + while( xRead(&sCtx) ){ + zAutoColumn(sCtx.z, &dbCols, 0); + if( sCtx.cTerm!=sCtx.cColSep ) break; + } + zColDefs = zAutoColumn(0, &dbCols, &zRenames); + if( zRenames!=0 ){ + sqlite3_fprintf((stdin_is_interactive && p->in==stdin)? p->out : stderr, + "Columns renamed during .import %s due to duplicates:\n" + "%s\n", sCtx.zFile, zRenames); + sqlite3_free(zRenames); + } + assert(dbCols==0); + if( zColDefs==0 ){ + sqlite3_fprintf(stderr,"%s: empty file\n", sCtx.zFile); + import_cleanup(&sCtx); + rc = 1; + sqlite3_free(zCreate); + goto meta_command_exit; + } + zCreate = sqlite3_mprintf("%z%z\n", zCreate, zColDefs); + if( zCreate==0 ){ + import_cleanup(&sCtx); + shell_out_of_memory(); + } + if( eVerbose>=1 ){ + sqlite3_fprintf(p->out, "%s\n", zCreate); + } + rc = sqlite3_exec(p->db, zCreate, 0, 0, 0); + if( rc ){ + sqlite3_fprintf(stderr, + "%s failed:\n%s\n", zCreate, sqlite3_errmsg(p->db)); + } + sqlite3_free(zCreate); + zCreate = 0; + if( rc ){ + import_cleanup(&sCtx); + rc = 1; + goto meta_command_exit; + } + } + zSql = sqlite3_mprintf("SELECT count(*) FROM pragma_table_info(%Q,%Q);", + zTable, zSchema); + if( zSql==0 ){ + import_cleanup(&sCtx); + shell_out_of_memory(); + } + rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); + sqlite3_free(zSql); + zSql = 0; + if( rc ){ + if (pStmt) sqlite3_finalize(pStmt); + shellDatabaseError(p->db); + import_cleanup(&sCtx); + rc = 1; + goto meta_command_exit; + } + if( sqlite3_step(pStmt)==SQLITE_ROW ){ + nCol = sqlite3_column_int(pStmt, 0); + }else{ + nCol = 0; + } + sqlite3_finalize(pStmt); + pStmt = 0; + if( nCol==0 ) return 0; /* no columns, no error */ + + nByte = 64 /* space for "INSERT INTO", "VALUES(", ")\0" */ + + (zSchema ? strlen(zSchema)*2 + 2: 0) /* Quoted schema name */ + + strlen(zTable)*2 + 2 /* Quoted table name */ + + nCol*2; /* Space for ",?" for each column */ + zSql = sqlite3_malloc64( nByte ); + if( zSql==0 ){ + import_cleanup(&sCtx); + shell_out_of_memory(); + } + if( zSchema ){ + sqlite3_snprintf(nByte, zSql, "INSERT INTO \"%w\".\"%w\" VALUES(?", + zSchema, zTable); + }else{ + sqlite3_snprintf(nByte, zSql, "INSERT INTO \"%w\" VALUES(?", zTable); + } + j = strlen30(zSql); + for(i=1; i=2 ){ + sqlite3_fprintf(p->out, "Insert using: %s\n", zSql); + } + rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); + sqlite3_free(zSql); + zSql = 0; + if( rc ){ + shellDatabaseError(p->db); + if (pStmt) sqlite3_finalize(pStmt); + import_cleanup(&sCtx); + rc = 1; + goto meta_command_exit; + } + needCommit = sqlite3_get_autocommit(p->db); + if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0); + do{ + int startLine = sCtx.nLine; + for(i=0; imode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break; + /* + ** For CSV mode, per RFC 4180, accept EOF in lieu of final + ** record terminator but only for last field of multi-field row. + ** (If there are too few fields, it's not valid CSV anyway.) + */ + if( z==0 && (xRead==csv_read_one_field) && i==nCol-1 && i>0 ){ + z = ""; + } + sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT); + if( i=nCol ){ + sqlite3_step(pStmt); + rc = sqlite3_reset(pStmt); + if( rc!=SQLITE_OK ){ + sqlite3_fprintf(stderr,"%s:%d: INSERT failed: %s\n", + sCtx.zFile, startLine, sqlite3_errmsg(p->db)); + sCtx.nErr++; + }else{ + sCtx.nRow++; + } + } + }while( sCtx.cTerm!=EOF ); + + import_cleanup(&sCtx); + sqlite3_finalize(pStmt); + if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0); + if( eVerbose>0 ){ + sqlite3_fprintf(p->out, + "Added %d rows with %d errors using %d lines of input\n", + sCtx.nRow, sCtx.nErr, sCtx.nLine-1); + } + }else +#endif /* !defined(SQLITE_SHELL_FIDDLE) */ + +#ifndef SQLITE_UNTESTABLE + if( c=='i' && cli_strncmp(azArg[0], "imposter", n)==0 ){ + char *zSql; + char *zCollist = 0; + sqlite3_stmt *pStmt; + int tnum = 0; + int isWO = 0; /* True if making an imposter of a WITHOUT ROWID table */ + int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */ + int i; + if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){ + eputz("Usage: .imposter INDEX IMPOSTER\n" + " .imposter off\n"); + /* Also allowed, but not documented: + ** + ** .imposter TABLE IMPOSTER + ** + ** where TABLE is a WITHOUT ROWID table. In that case, the + ** imposter is another WITHOUT ROWID table with the columns in + ** storage order. */ + rc = 1; + goto meta_command_exit; + } + open_db(p, 0); + if( nArg==2 ){ + sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1); + goto meta_command_exit; + } + zSql = sqlite3_mprintf( + "SELECT rootpage, 0 FROM sqlite_schema" + " WHERE name='%q' AND type='index'" + "UNION ALL " + "SELECT rootpage, 1 FROM sqlite_schema" + " WHERE name='%q' AND type='table'" + " AND sql LIKE '%%without%%rowid%%'", + azArg[1], azArg[1] + ); + sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); + sqlite3_free(zSql); + if( sqlite3_step(pStmt)==SQLITE_ROW ){ + tnum = sqlite3_column_int(pStmt, 0); + isWO = sqlite3_column_int(pStmt, 1); + } + sqlite3_finalize(pStmt); + zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]); + rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); + sqlite3_free(zSql); + i = 0; + while( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){ + char zLabel[20]; + const char *zCol = (const char*)sqlite3_column_text(pStmt,2); + i++; + if( zCol==0 ){ + if( sqlite3_column_int(pStmt,1)==-1 ){ + zCol = "_ROWID_"; + }else{ + sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i); + zCol = zLabel; + } + } + if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){ + lenPK = (int)strlen(zCollist); + } + if( zCollist==0 ){ + zCollist = sqlite3_mprintf("\"%w\"", zCol); + }else{ + zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol); + } + } + sqlite3_finalize(pStmt); + if( i==0 || tnum==0 ){ + sqlite3_fprintf(stderr,"no such index: \"%s\"\n", azArg[1]); + rc = 1; + sqlite3_free(zCollist); + goto meta_command_exit; + } + if( lenPK==0 ) lenPK = 100000; + zSql = sqlite3_mprintf( + "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID", + azArg[2], zCollist, lenPK, zCollist); + sqlite3_free(zCollist); + rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 2, tnum); + if( rc==SQLITE_OK ){ + rc = sqlite3_exec(p->db, zSql, 0, 0, 0); + sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0); + if( rc ){ + sqlite3_fprintf(stderr, + "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db)); + }else{ + sqlite3_fprintf(stdout, "%s;\n", zSql); + } + }else{ + sqlite3_fprintf(stderr,"SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc); + rc = 1; + } + sqlite3_free(zSql); + }else +#endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */ + + if( c=='i' && cli_strncmp(azArg[0], "intck", n)==0 ){ + i64 iArg = 0; + if( nArg==2 ){ + iArg = integerValue(azArg[1]); + if( iArg==0 ) iArg = -1; + } + if( (nArg!=1 && nArg!=2) || iArg<0 ){ + sqlite3_fprintf(stderr,"%s","Usage: .intck STEPS_PER_UNLOCK\n"); + rc = 1; + goto meta_command_exit; + } + open_db(p, 0); + rc = intckDatabaseCmd(p, iArg); + }else + +#ifdef SQLITE_ENABLE_IOTRACE + if( c=='i' && cli_strncmp(azArg[0], "iotrace", n)==0 ){ + SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...); + if( iotrace && iotrace!=stdout ) fclose(iotrace); + iotrace = 0; + if( nArg<2 ){ + sqlite3IoTrace = 0; + }else if( cli_strcmp(azArg[1], "-")==0 ){ + sqlite3IoTrace = iotracePrintf; + iotrace = stdout; + }else{ + iotrace = sqlite3_fopen(azArg[1], "w"); + if( iotrace==0 ){ + sqlite3_fprintf(stderr,"Error: cannot open \"%s\"\n", azArg[1]); + sqlite3IoTrace = 0; + rc = 1; + }else{ + sqlite3IoTrace = iotracePrintf; + } + } + }else +#endif + + if( c=='l' && n>=5 && cli_strncmp(azArg[0], "limits", n)==0 ){ + static const struct { + const char *zLimitName; /* Name of a limit */ + int limitCode; /* Integer code for that limit */ + } aLimit[] = { + { "length", SQLITE_LIMIT_LENGTH }, + { "sql_length", SQLITE_LIMIT_SQL_LENGTH }, + { "column", SQLITE_LIMIT_COLUMN }, + { "expr_depth", SQLITE_LIMIT_EXPR_DEPTH }, + { "compound_select", SQLITE_LIMIT_COMPOUND_SELECT }, + { "vdbe_op", SQLITE_LIMIT_VDBE_OP }, + { "function_arg", SQLITE_LIMIT_FUNCTION_ARG }, + { "attached", SQLITE_LIMIT_ATTACHED }, + { "like_pattern_length", SQLITE_LIMIT_LIKE_PATTERN_LENGTH }, + { "variable_number", SQLITE_LIMIT_VARIABLE_NUMBER }, + { "trigger_depth", SQLITE_LIMIT_TRIGGER_DEPTH }, + { "worker_threads", SQLITE_LIMIT_WORKER_THREADS }, + }; + int i, n2; + open_db(p, 0); + if( nArg==1 ){ + for(i=0; idb, aLimit[i].limitCode, -1)); + } + }else if( nArg>3 ){ + eputz("Usage: .limit NAME ?NEW-VALUE?\n"); + rc = 1; + goto meta_command_exit; + }else{ + int iLimit = -1; + n2 = strlen30(azArg[1]); + for(i=0; idb, aLimit[iLimit].limitCode, + (int)integerValue(azArg[2])); + } + sqlite3_fprintf(stdout, "%20s %d\n", aLimit[iLimit].zLimitName, + sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1)); + } + }else + + if( c=='l' && n>2 && cli_strncmp(azArg[0], "lint", n)==0 ){ + open_db(p, 0); + lintDotCommand(p, azArg, nArg); + }else + +#if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_FIDDLE) + if( c=='l' && cli_strncmp(azArg[0], "load", n)==0 ){ + const char *zFile, *zProc; + char *zErrMsg = 0; + failIfSafeMode(p, "cannot run .load in safe mode"); + if( nArg<2 || azArg[1][0]==0 ){ + /* Must have a non-empty FILE. (Will not load self.) */ + eputz("Usage: .load FILE ?ENTRYPOINT?\n"); + rc = 1; + goto meta_command_exit; + } + zFile = azArg[1]; + zProc = nArg>=3 ? azArg[2] : 0; + open_db(p, 0); + rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg); + if( rc!=SQLITE_OK ){ + shellEmitError(zErrMsg); + sqlite3_free(zErrMsg); + rc = 1; + } + }else +#endif + + if( c=='l' && cli_strncmp(azArg[0], "log", n)==0 ){ + if( nArg!=2 ){ + eputz("Usage: .log FILENAME\n"); + rc = 1; + }else{ + const char *zFile = azArg[1]; + if( p->bSafeMode + && cli_strcmp(zFile,"on")!=0 + && cli_strcmp(zFile,"off")!=0 + ){ + sputz(stdout, "cannot set .log to anything other" + " than \"on\" or \"off\"\n"); + zFile = "off"; + } + output_file_close(p->pLog); + if( cli_strcmp(zFile,"on")==0 ) zFile = "stdout"; + p->pLog = output_file_open(zFile); + } + }else + + if( c=='m' && cli_strncmp(azArg[0], "mode", n)==0 ){ + const char *zMode = 0; + const char *zTabname = 0; + int i, n2; + int chng = 0; /* 0x01: change to cmopts. 0x02: Any other change */ + ColModeOpts cmOpts = ColModeOpts_default; + for(i=1; ieEscMode = k; + chng |= 2; + break; + } + } + if( k>=ArraySize(shell_EscModeNames) ){ + sqlite3_fprintf(stderr, "unknown control character escape mode \"%s\"" + " - choices:", zEsc); + for(k=0; kmode==MODE_Column + || (p->mode>=MODE_Markdown && p->mode<=MODE_Box) + ){ + sqlite3_fprintf(p->out, + "current output mode: %s --wrap %d --wordwrap %s " + "--%squote --escape %s\n", + modeDescr[p->mode], p->cmOpts.iWrap, + p->cmOpts.bWordWrap ? "on" : "off", + p->cmOpts.bQuote ? "" : "no", + shell_EscModeNames[p->eEscMode] + ); + }else{ + sqlite3_fprintf(p->out, + "current output mode: %s --escape %s\n", + modeDescr[p->mode], + shell_EscModeNames[p->eEscMode] + ); + } + } + if( zMode==0 ){ + zMode = modeDescr[p->mode]; + if( (chng&1)==0 ) cmOpts = p->cmOpts; + } + n2 = strlen30(zMode); + if( cli_strncmp(zMode,"lines",n2)==0 ){ + p->mode = MODE_Line; + sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); + }else if( cli_strncmp(zMode,"columns",n2)==0 ){ + p->mode = MODE_Column; + if( (p->shellFlgs & SHFLG_HeaderSet)==0 ){ + p->showHeader = 1; + } + sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); + p->cmOpts = cmOpts; + }else if( cli_strncmp(zMode,"list",n2)==0 ){ + p->mode = MODE_List; + sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column); + sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); + }else if( cli_strncmp(zMode,"html",n2)==0 ){ + p->mode = MODE_Html; + }else if( cli_strncmp(zMode,"tcl",n2)==0 ){ + p->mode = MODE_Tcl; + sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space); + sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); + }else if( cli_strncmp(zMode,"csv",n2)==0 ){ + p->mode = MODE_Csv; + sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma); + sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf); + }else if( cli_strncmp(zMode,"tabs",n2)==0 ){ + p->mode = MODE_List; + sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab); + }else if( cli_strncmp(zMode,"insert",n2)==0 ){ + p->mode = MODE_Insert; + set_table_name(p, zTabname ? zTabname : "table"); + if( p->eEscMode==SHELL_ESC_OFF ){ + ShellSetFlag(p, SHFLG_Newlines); + }else{ + ShellClearFlag(p, SHFLG_Newlines); + } + }else if( cli_strncmp(zMode,"quote",n2)==0 ){ + p->mode = MODE_Quote; + sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma); + sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); + }else if( cli_strncmp(zMode,"ascii",n2)==0 ){ + p->mode = MODE_Ascii; + sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit); + sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record); + }else if( cli_strncmp(zMode,"markdown",n2)==0 ){ + p->mode = MODE_Markdown; + p->cmOpts = cmOpts; + }else if( cli_strncmp(zMode,"table",n2)==0 ){ + p->mode = MODE_Table; + p->cmOpts = cmOpts; + }else if( cli_strncmp(zMode,"box",n2)==0 ){ + p->mode = MODE_Box; + p->cmOpts = cmOpts; + }else if( cli_strncmp(zMode,"count",n2)==0 ){ + p->mode = MODE_Count; + }else if( cli_strncmp(zMode,"off",n2)==0 ){ + p->mode = MODE_Off; + }else if( cli_strncmp(zMode,"json",n2)==0 ){ + p->mode = MODE_Json; + }else{ + eputz("Error: mode should be one of: " + "ascii box column csv html insert json line list markdown " + "qbox quote table tabs tcl\n"); + rc = 1; + } + p->cMode = p->mode; + }else + +#ifndef SQLITE_SHELL_FIDDLE + if( c=='n' && cli_strcmp(azArg[0], "nonce")==0 ){ + if( nArg!=2 ){ + eputz("Usage: .nonce NONCE\n"); + rc = 1; + }else if( p->zNonce==0 || cli_strcmp(azArg[1],p->zNonce)!=0 ){ + sqlite3_fprintf(stderr,"line %lld: incorrect nonce: \"%s\"\n", + p->lineno, azArg[1]); + exit(1); + }else{ + p->bSafeMode = 0; + return 0; /* Return immediately to bypass the safe mode reset + ** at the end of this procedure */ + } + }else +#endif /* !defined(SQLITE_SHELL_FIDDLE) */ + + if( c=='n' && cli_strncmp(azArg[0], "nullvalue", n)==0 ){ + if( nArg==2 ){ + sqlite3_snprintf(sizeof(p->nullValue), p->nullValue, + "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]); + }else{ + eputz("Usage: .nullvalue STRING\n"); + rc = 1; + } + }else + + if( c=='o' && cli_strncmp(azArg[0], "open", n)==0 && n>=2 ){ + const char *zFN = 0; /* Pointer to constant filename */ + char *zNewFilename = 0; /* Name of the database file to open */ + int iName = 1; /* Index in azArg[] of the filename */ + int newFlag = 0; /* True to delete file before opening */ + int openMode = SHELL_OPEN_UNSPEC; + int openFlags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE; + + /* Check for command-line arguments */ + for(iName=1; iNameszMax = integerValue(azArg[++iName]); +#endif /* SQLITE_OMIT_DESERIALIZE */ + }else +#endif /* !SQLITE_SHELL_FIDDLE */ + if( z[0]=='-' ){ + sqlite3_fprintf(stderr,"unknown option: %s\n", z); + rc = 1; + goto meta_command_exit; + }else if( zFN ){ + sqlite3_fprintf(stderr,"extra argument: \"%s\"\n", z); + rc = 1; + goto meta_command_exit; + }else{ + zFN = z; + } + } + + /* Close the existing database */ + session_close_all(p, -1); + close_db(p->db); + p->db = 0; + p->pAuxDb->zDbFilename = 0; + sqlite3_free(p->pAuxDb->zFreeOnClose); + p->pAuxDb->zFreeOnClose = 0; + p->openMode = openMode; + p->openFlags = openFlags; + p->szMax = 0; + + /* If a filename is specified, try to open it first */ + if( zFN || p->openMode==SHELL_OPEN_HEXDB ){ + if( newFlag && zFN && !p->bSafeMode ){ + if( cli_strncmp(zFN,"file:",5)==0 ){ + char *zDel = shellFilenameFromUri(zFN); + shell_check_oom(zDel); + shellDeleteFile(zDel); + sqlite3_free(zDel); + }else{ + shellDeleteFile(zFN); + } + } +#ifndef SQLITE_SHELL_FIDDLE + if( p->bSafeMode + && p->openMode!=SHELL_OPEN_HEXDB + && zFN + && cli_strcmp(zFN,":memory:")!=0 + ){ + failIfSafeMode(p, "cannot open disk-based database files in safe mode"); + } +#else + /* WASM mode has its own sandboxed pseudo-filesystem. */ +#endif + if( zFN ){ + zNewFilename = sqlite3_mprintf("%s", zFN); + shell_check_oom(zNewFilename); + }else{ + zNewFilename = 0; + } + p->pAuxDb->zDbFilename = zNewFilename; + open_db(p, OPEN_DB_KEEPALIVE); + if( p->db==0 ){ + sqlite3_fprintf(stderr,"Error: cannot open '%s'\n", zNewFilename); + sqlite3_free(zNewFilename); + }else{ + p->pAuxDb->zFreeOnClose = zNewFilename; + } + } + if( p->db==0 ){ + /* As a fall-back open a TEMP database */ + p->pAuxDb->zDbFilename = 0; + open_db(p, 0); + } + }else + +#ifndef SQLITE_SHELL_FIDDLE + if( (c=='o' + && (cli_strncmp(azArg[0], "output", n)==0 + || cli_strncmp(azArg[0], "once", n)==0)) + || (c=='e' && n==5 && cli_strcmp(azArg[0],"excel")==0) + || (c=='w' && n==3 && cli_strcmp(azArg[0],"www")==0) + ){ + char *zFile = 0; + int i; + int eMode = 0; /* 0: .outout/.once, 'x'=.excel, 'w'=.www */ + int bOnce = 0; /* 0: .output, 1: .once, 2: .excel/.www */ + int bPlain = 0; /* --plain option */ + static const char *zBomUtf8 = "\357\273\277"; + const char *zBom = 0; + + failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]); + if( c=='e' ){ + eMode = 'x'; + bOnce = 2; + }else if( c=='w' ){ + eMode = 'w'; + bOnce = 2; + }else if( cli_strncmp(azArg[0],"once",n)==0 ){ + bOnce = 1; + } + for(i=1; iout, + "ERROR: unknown option: \"%s\". Usage:\n", azArg[i]); + showHelp(p->out, azArg[0]); + rc = 1; + sqlite3_free(zFile); + goto meta_command_exit; + } + }else if( zFile==0 && eMode==0 ){ + if( cli_strcmp(z, "off")==0 ){ +#ifdef _WIN32 + zFile = sqlite3_mprintf("nul"); +#else + zFile = sqlite3_mprintf("/dev/null"); +#endif + }else{ + zFile = sqlite3_mprintf("%s", z); + } + if( zFile && zFile[0]=='|' ){ + while( i+1out, + "ERROR: extra parameter: \"%s\". Usage:\n", azArg[i]); + showHelp(p->out, azArg[0]); + rc = 1; + sqlite3_free(zFile); + goto meta_command_exit; + } + } + if( zFile==0 ){ + zFile = sqlite3_mprintf("stdout"); + } + shell_check_oom(zFile); + if( bOnce ){ + p->outCount = 2; + }else{ + p->outCount = 0; + } + output_reset(p); +#ifndef SQLITE_NOHAVE_SYSTEM + if( eMode=='e' || eMode=='x' || eMode=='w' ){ + p->doXdgOpen = 1; + outputModePush(p); + if( eMode=='x' ){ + /* spreadsheet mode. Output as CSV. */ + newTempFile(p, "csv"); + ShellClearFlag(p, SHFLG_Echo); + p->mode = MODE_Csv; + sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma); + sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf); +#ifdef _WIN32 + zBom = zBomUtf8; /* Always include the BOM on Windows, as Excel does + ** not work without it. */ +#endif + }else if( eMode=='w' ){ + /* web-browser mode. */ + newTempFile(p, "html"); + if( !bPlain ) p->mode = MODE_Www; + }else{ + /* text editor mode */ + newTempFile(p, "txt"); + } + sqlite3_free(zFile); + zFile = sqlite3_mprintf("%s", p->zTempFile); + } +#endif /* SQLITE_NOHAVE_SYSTEM */ + shell_check_oom(zFile); + if( zFile[0]=='|' ){ +#ifdef SQLITE_OMIT_POPEN + eputz("Error: pipes are not supported in this OS\n"); + rc = 1; + output_redir(p, stdout); +#else + FILE *pfPipe = sqlite3_popen(zFile + 1, "w"); + if( pfPipe==0 ){ + assert( stderr!=NULL ); + sqlite3_fprintf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1); + rc = 1; + }else{ + output_redir(p, pfPipe); + if( zBom ) sqlite3_fputs(zBom, pfPipe); + sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile); + } +#endif + }else{ + FILE *pfFile = output_file_open(zFile); + if( pfFile==0 ){ + if( cli_strcmp(zFile,"off")!=0 ){ + assert( stderr!=NULL ); + sqlite3_fprintf(stderr,"Error: cannot write to \"%s\"\n", zFile); + } + rc = 1; + } else { + output_redir(p, pfFile); + if( zBom ) sqlite3_fputs(zBom, pfFile); + if( bPlain && eMode=='w' ){ + sqlite3_fputs( + "\n\n\n", + pfFile + ); + } + sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile); + } + } + sqlite3_free(zFile); + }else +#endif /* !defined(SQLITE_SHELL_FIDDLE) */ + + if( c=='p' && n>=3 && cli_strncmp(azArg[0], "parameter", n)==0 ){ + open_db(p,0); + if( nArg<=1 ) goto parameter_syntax_error; + + /* .parameter clear + ** Clear all bind parameters by dropping the TEMP table that holds them. + */ + if( nArg==2 && cli_strcmp(azArg[1],"clear")==0 ){ + sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;", + 0, 0, 0); + }else + + /* .parameter list + ** List all bind parameters. + */ + if( nArg==2 && cli_strcmp(azArg[1],"list")==0 ){ + sqlite3_stmt *pStmt = 0; + int rx; + int len = 0; + rx = sqlite3_prepare_v2(p->db, + "SELECT max(length(key)) " + "FROM temp.sqlite_parameters;", -1, &pStmt, 0); + if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){ + len = sqlite3_column_int(pStmt, 0); + if( len>40 ) len = 40; + } + sqlite3_finalize(pStmt); + pStmt = 0; + if( len ){ + rx = sqlite3_prepare_v2(p->db, + "SELECT key, quote(value) " + "FROM temp.sqlite_parameters;", -1, &pStmt, 0); + while( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){ + sqlite3_fprintf(p->out, + "%-*s %s\n", len, sqlite3_column_text(pStmt,0), + sqlite3_column_text(pStmt,1)); + } + sqlite3_finalize(pStmt); + } + }else + + /* .parameter init + ** Make sure the TEMP table used to hold bind parameters exists. + ** Create it if necessary. + */ + if( nArg==2 && cli_strcmp(azArg[1],"init")==0 ){ + bind_table_init(p); + }else + + /* .parameter set NAME VALUE + ** Set or reset a bind parameter. NAME should be the full parameter + ** name exactly as it appears in the query. (ex: $abc, @def). The + ** VALUE can be in either SQL literal notation, or if not it will be + ** understood to be a text string. + */ + if( nArg==4 && cli_strcmp(azArg[1],"set")==0 ){ + int rx; + char *zSql; + sqlite3_stmt *pStmt; + const char *zKey = azArg[2]; + const char *zValue = azArg[3]; + bind_table_init(p); + zSql = sqlite3_mprintf( + "REPLACE INTO temp.sqlite_parameters(key,value)" + "VALUES(%Q,%s);", zKey, zValue); + shell_check_oom(zSql); + pStmt = 0; + rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); + sqlite3_free(zSql); + if( rx!=SQLITE_OK ){ + sqlite3_finalize(pStmt); + pStmt = 0; + zSql = sqlite3_mprintf( + "REPLACE INTO temp.sqlite_parameters(key,value)" + "VALUES(%Q,%Q);", zKey, zValue); + shell_check_oom(zSql); + rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); + sqlite3_free(zSql); + if( rx!=SQLITE_OK ){ + sqlite3_fprintf(p->out, "Error: %s\n", sqlite3_errmsg(p->db)); + sqlite3_finalize(pStmt); + pStmt = 0; + rc = 1; + } + } + bind_prepared_stmt(p, pStmt); + sqlite3_step(pStmt); + sqlite3_finalize(pStmt); + }else + + /* .parameter unset NAME + ** Remove the NAME binding from the parameter binding table, if it + ** exists. + */ + if( nArg==3 && cli_strcmp(azArg[1],"unset")==0 ){ + char *zSql = sqlite3_mprintf( + "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]); + shell_check_oom(zSql); + sqlite3_exec(p->db, zSql, 0, 0, 0); + sqlite3_free(zSql); + }else + /* If no command name matches, show a syntax error */ + parameter_syntax_error: + showHelp(p->out, "parameter"); + }else + + if( c=='p' && n>=3 && cli_strncmp(azArg[0], "print", n)==0 ){ + int i; + for(i=1; i<nArg; i++){ + if( i>1 ) sqlite3_fputs(" ", p->out); + sqlite3_fputs(azArg[i], p->out); + } + sqlite3_fputs("\n", p->out); + }else + +#ifndef SQLITE_OMIT_PROGRESS_CALLBACK + if( c=='p' && n>=3 && cli_strncmp(azArg[0], "progress", n)==0 ){ + int i; + int nn = 0; + p->flgProgress = 0; + p->mxProgress = 0; + p->nProgress = 0; + for(i=1; i<nArg; i++){ + const char *z = azArg[i]; + if( z[0]=='-' ){ + z++; + if( z[0]=='-' ) z++; + if( cli_strcmp(z,"quiet")==0 || cli_strcmp(z,"q")==0 ){ + p->flgProgress |= SHELL_PROGRESS_QUIET; + continue; + } + if( cli_strcmp(z,"reset")==0 ){ + p->flgProgress |= SHELL_PROGRESS_RESET; + continue; + } + if( cli_strcmp(z,"once")==0 ){ + p->flgProgress |= SHELL_PROGRESS_ONCE; + continue; + } + if( cli_strcmp(z,"limit")==0 ){ + if( i+1>=nArg ){ + eputz("Error: missing argument on --limit\n"); + rc = 1; + goto meta_command_exit; + }else{ + p->mxProgress = (int)integerValue(azArg[++i]); + } + continue; + } + sqlite3_fprintf(stderr,"Error: unknown option: \"%s\"\n", azArg[i]); + rc = 1; + goto meta_command_exit; + }else{ + nn = (int)integerValue(z); + } + } + open_db(p, 0); + sqlite3_progress_handler(p->db, nn, progress_handler, p); + }else +#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */ + + if( c=='p' && cli_strncmp(azArg[0], "prompt", n)==0 ){ + if( nArg >= 2) { + shell_strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1); + } + if( nArg >= 3) { + shell_strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1); + } + }else + +#ifndef SQLITE_SHELL_FIDDLE + if( c=='q' && cli_strncmp(azArg[0], "quit", n)==0 ){ + rc = 2; + }else +#endif + +#ifndef SQLITE_SHELL_FIDDLE + if( c=='r' && n>=3 && cli_strncmp(azArg[0], "read", n)==0 ){ + FILE *inSaved = p->in; + i64 savedLineno = p->lineno; + failIfSafeMode(p, "cannot run .read in safe mode"); + if( nArg!=2 ){ + eputz("Usage: .read FILE\n"); + rc = 1; + goto meta_command_exit; + } + if( azArg[1][0]=='|' ){ +#ifdef SQLITE_OMIT_POPEN + eputz("Error: pipes are not supported in this OS\n"); + rc = 1; +#else + p->in = sqlite3_popen(azArg[1]+1, "r"); + if( p->in==0 ){ + sqlite3_fprintf(stderr,"Error: cannot open \"%s\"\n", azArg[1]); + rc = 1; + }else{ + rc = process_input(p, "<pipe>"); + pclose(p->in); + } +#endif + }else if( (p->in = openChrSource(azArg[1]))==0 ){ + sqlite3_fprintf(stderr,"Error: cannot open \"%s\"\n", azArg[1]); + rc = 1; + }else{ + rc = process_input(p, azArg[1]); + fclose(p->in); + } + p->in = inSaved; + p->lineno = savedLineno; + }else +#endif /* !defined(SQLITE_SHELL_FIDDLE) */ + +#ifndef SQLITE_SHELL_FIDDLE + if( c=='r' && n>=3 && cli_strncmp(azArg[0], "restore", n)==0 ){ + const char *zSrcFile; + const char *zDb; + sqlite3 *pSrc; + sqlite3_backup *pBackup; + int nTimeout = 0; + + failIfSafeMode(p, "cannot run .restore in safe mode"); + if( nArg==2 ){ + zSrcFile = azArg[1]; + zDb = "main"; + }else if( nArg==3 ){ + zSrcFile = azArg[2]; + zDb = azArg[1]; + }else{ + eputz("Usage: .restore ?DB? FILE\n"); + rc = 1; + goto meta_command_exit; + } + rc = sqlite3_open(zSrcFile, &pSrc); + if( rc!=SQLITE_OK ){ + sqlite3_fprintf(stderr,"Error: cannot open \"%s\"\n", zSrcFile); + close_db(pSrc); + return 1; + } + open_db(p, 0); + pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main"); + if( pBackup==0 ){ + shellDatabaseError(p->db); + close_db(pSrc); + return 1; + } + while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK + || rc==SQLITE_BUSY ){ + if( rc==SQLITE_BUSY ){ + if( nTimeout++ >= 3 ) break; + sqlite3_sleep(100); + } + } + sqlite3_backup_finish(pBackup); + if( rc==SQLITE_DONE ){ + rc = 0; + }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){ + eputz("Error: source database is busy\n"); + rc = 1; + }else{ + shellDatabaseError(p->db); + rc = 1; + } + close_db(pSrc); + }else +#endif /* !defined(SQLITE_SHELL_FIDDLE) */ + + if( c=='s' && + (cli_strncmp(azArg[0], "scanstats", n)==0 || + cli_strncmp(azArg[0], "scanstatus", n)==0) + ){ + if( nArg==2 ){ + if( cli_strcmp(azArg[1], "vm")==0 ){ + p->scanstatsOn = 3; + }else + if( cli_strcmp(azArg[1], "est")==0 ){ + p->scanstatsOn = 2; + }else{ + p->scanstatsOn = (u8)booleanValue(azArg[1]); + } + open_db(p, 0); + sqlite3_db_config( + p->db, SQLITE_DBCONFIG_STMT_SCANSTATUS, p->scanstatsOn, (int*)0 + ); +#if !defined(SQLITE_ENABLE_STMT_SCANSTATUS) + eputz("Warning: .scanstats not available in this build.\n"); +#elif !defined(SQLITE_ENABLE_BYTECODE_VTAB) + if( p->scanstatsOn==3 ){ + eputz("Warning: \".scanstats vm\" not available in this build.\n"); + } +#endif + }else{ + eputz("Usage: .scanstats on|off|est\n"); + rc = 1; + } + }else + + if( c=='s' && cli_strncmp(azArg[0], "schema", n)==0 ){ + ShellText sSelect; + ShellState data; + char *zErrMsg = 0; + const char *zDiv = "("; + const char *zName = 0; + int iSchema = 0; + int bDebug = 0; + int bNoSystemTabs = 0; + int ii; + + open_db(p, 0); + memcpy(&data, p, sizeof(data)); + data.showHeader = 0; + data.cMode = data.mode = MODE_Semi; + initText(&sSelect); + for(ii=1; ii<nArg; ii++){ + if( optionMatch(azArg[ii],"indent") ){ + data.cMode = data.mode = MODE_Pretty; + }else if( optionMatch(azArg[ii],"debug") ){ + bDebug = 1; + }else if( optionMatch(azArg[ii],"nosys") ){ + bNoSystemTabs = 1; + }else if( azArg[ii][0]=='-' ){ + sqlite3_fprintf(stderr,"Unknown option: \"%s\"\n", azArg[ii]); + rc = 1; + goto meta_command_exit; + }else if( zName==0 ){ + zName = azArg[ii]; + }else{ + eputz("Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?\n"); + rc = 1; + goto meta_command_exit; + } + } + if( zName!=0 ){ + int isSchema = sqlite3_strlike(zName, "sqlite_master", '\\')==0 + || sqlite3_strlike(zName, "sqlite_schema", '\\')==0 + || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0 + || sqlite3_strlike(zName,"sqlite_temp_schema", '\\')==0; + if( isSchema ){ + char *new_argv[2], *new_colv[2]; + new_argv[0] = sqlite3_mprintf( + "CREATE TABLE %s (\n" + " type text,\n" + " name text,\n" + " tbl_name text,\n" + " rootpage integer,\n" + " sql text\n" + ")", zName); + shell_check_oom(new_argv[0]); + new_argv[1] = 0; + new_colv[0] = "sql"; + new_colv[1] = 0; + callback(&data, 1, new_argv, new_colv); + sqlite3_free(new_argv[0]); + } + } + if( zDiv ){ + sqlite3_stmt *pStmt = 0; + rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list", + -1, &pStmt, 0); + if( rc ){ + shellDatabaseError(p->db); + sqlite3_finalize(pStmt); + rc = 1; + goto meta_command_exit; + } + appendText(&sSelect, "SELECT sql FROM", 0); + iSchema = 0; + while( sqlite3_step(pStmt)==SQLITE_ROW ){ + const char *zDb = (const char*)sqlite3_column_text(pStmt, 0); + char zScNum[30]; + sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema); + appendText(&sSelect, zDiv, 0); + zDiv = " UNION ALL "; + appendText(&sSelect, "SELECT shell_add_schema(sql,", 0); + if( sqlite3_stricmp(zDb, "main")!=0 ){ + appendText(&sSelect, zDb, '\''); + }else{ + appendText(&sSelect, "NULL", 0); + } + appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0); + appendText(&sSelect, zScNum, 0); + appendText(&sSelect, " AS snum, ", 0); + appendText(&sSelect, zDb, '\''); + appendText(&sSelect, " AS sname FROM ", 0); + appendText(&sSelect, zDb, quoteChar(zDb)); + appendText(&sSelect, ".sqlite_schema", 0); + } + sqlite3_finalize(pStmt); +#ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS + if( zName ){ + appendText(&sSelect, + " UNION ALL SELECT shell_module_schema(name)," + " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list", + 0); + } +#endif + appendText(&sSelect, ") WHERE ", 0); + if( zName ){ + char *zQarg = sqlite3_mprintf("%Q", zName); + int bGlob; + shell_check_oom(zQarg); + bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 || + strchr(zName, '[') != 0; + if( strchr(zName, '.') ){ + appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0); + }else{ + appendText(&sSelect, "lower(tbl_name)", 0); + } + appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0); + appendText(&sSelect, zQarg, 0); + if( !bGlob ){ + appendText(&sSelect, " ESCAPE '\\' ", 0); + } + appendText(&sSelect, " AND ", 0); + sqlite3_free(zQarg); + } + if( bNoSystemTabs ){ + appendText(&sSelect, "name NOT LIKE 'sqlite__%%' ESCAPE '_' AND ", 0); + } + appendText(&sSelect, "sql IS NOT NULL" + " ORDER BY snum, rowid", 0); + if( bDebug ){ + sqlite3_fprintf(p->out, "SQL: %s;\n", sSelect.zTxt); + }else{ + rc = sqlite3_exec(p->db, sSelect.zTxt, callback, &data, &zErrMsg); + } + freeText(&sSelect); + } + if( zErrMsg ){ + shellEmitError(zErrMsg); + sqlite3_free(zErrMsg); + rc = 1; + }else if( rc != SQLITE_OK ){ + eputz("Error: querying schema information\n"); + rc = 1; + }else{ + rc = 0; + } + }else + + if( (c=='s' && n==11 && cli_strncmp(azArg[0], "selecttrace", n)==0) + || (c=='t' && n==9 && cli_strncmp(azArg[0], "treetrace", n)==0) + ){ + unsigned int x = nArg>=2? (unsigned int)integerValue(azArg[1]) : 0xffffffff; + sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &x); + }else + +#if defined(SQLITE_ENABLE_SESSION) + if( c=='s' && cli_strncmp(azArg[0],"session",n)==0 && n>=3 ){ + struct AuxDb *pAuxDb = p->pAuxDb; + OpenSession *pSession = &pAuxDb->aSession[0]; + char **azCmd = &azArg[1]; + int iSes = 0; + int nCmd = nArg - 1; + int i; + if( nArg<=1 ) goto session_syntax_error; + open_db(p, 0); + if( nArg>=3 ){ + for(iSes=0; iSes<pAuxDb->nSession; iSes++){ + if( cli_strcmp(pAuxDb->aSession[iSes].zName, azArg[1])==0 ) break; + } + if( iSes<pAuxDb->nSession ){ + pSession = &pAuxDb->aSession[iSes]; + azCmd++; + nCmd--; + }else{ + pSession = &pAuxDb->aSession[0]; + iSes = 0; + } + } + + /* .session attach TABLE + ** Invoke the sqlite3session_attach() interface to attach a particular + ** table so that it is never filtered. + */ + if( cli_strcmp(azCmd[0],"attach")==0 ){ + if( nCmd!=2 ) goto session_syntax_error; + if( pSession->p==0 ){ + session_not_open: + eputz("ERROR: No sessions are open\n"); + }else{ + rc = sqlite3session_attach(pSession->p, azCmd[1]); + if( rc ){ + sqlite3_fprintf(stderr, + "ERROR: sqlite3session_attach() returns %d\n",rc); + rc = 0; + } + } + }else + + /* .session changeset FILE + ** .session patchset FILE + ** Write a changeset or patchset into a file. The file is overwritten. + */ + if( cli_strcmp(azCmd[0],"changeset")==0 + || cli_strcmp(azCmd[0],"patchset")==0 + ){ + FILE *out = 0; + failIfSafeMode(p, "cannot run \".session %s\" in safe mode", azCmd[0]); + if( nCmd!=2 ) goto session_syntax_error; + if( pSession->p==0 ) goto session_not_open; + out = sqlite3_fopen(azCmd[1], "wb"); + if( out==0 ){ + sqlite3_fprintf(stderr,"ERROR: cannot open \"%s\" for writing\n", + azCmd[1]); + }else{ + int szChng; + void *pChng; + if( azCmd[0][0]=='c' ){ + rc = sqlite3session_changeset(pSession->p, &szChng, &pChng); + }else{ + rc = sqlite3session_patchset(pSession->p, &szChng, &pChng); + } + if( rc ){ + sqlite3_fprintf(stdout, "Error: error code %d\n", rc); + rc = 0; + } + if( pChng + && fwrite(pChng, szChng, 1, out)!=1 ){ + sqlite3_fprintf(stderr, + "ERROR: Failed to write entire %d-byte output\n", szChng); + } + sqlite3_free(pChng); + fclose(out); + } + }else + + /* .session close + ** Close the identified session + */ + if( cli_strcmp(azCmd[0], "close")==0 ){ + if( nCmd!=1 ) goto session_syntax_error; + if( pAuxDb->nSession ){ + session_close(pSession); + pAuxDb->aSession[iSes] = pAuxDb->aSession[--pAuxDb->nSession]; + } + }else + + /* .session enable ?BOOLEAN? + ** Query or set the enable flag + */ + if( cli_strcmp(azCmd[0], "enable")==0 ){ + int ii; + if( nCmd>2 ) goto session_syntax_error; + ii = nCmd==1 ? -1 : booleanValue(azCmd[1]); + if( pAuxDb->nSession ){ + ii = sqlite3session_enable(pSession->p, ii); + sqlite3_fprintf(p->out, + "session %s enable flag = %d\n", pSession->zName, ii); + } + }else + + /* .session filter GLOB .... + ** Set a list of GLOB patterns of table names to be excluded. + */ + if( cli_strcmp(azCmd[0], "filter")==0 ){ + int ii; + i64 nByte; + if( nCmd<2 ) goto session_syntax_error; + if( pAuxDb->nSession ){ + for(ii=0; ii<pSession->nFilter; ii++){ + sqlite3_free(pSession->azFilter[ii]); + } + sqlite3_free(pSession->azFilter); + nByte = sizeof(pSession->azFilter[0])*(nCmd-1); + pSession->azFilter = sqlite3_malloc64( nByte ); + shell_check_oom( pSession->azFilter ); + for(ii=1; ii<nCmd; ii++){ + char *x = pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]); + shell_check_oom(x); + } + pSession->nFilter = ii-1; + } + }else + + /* .session indirect ?BOOLEAN? + ** Query or set the indirect flag + */ + if( cli_strcmp(azCmd[0], "indirect")==0 ){ + int ii; + if( nCmd>2 ) goto session_syntax_error; + ii = nCmd==1 ? -1 : booleanValue(azCmd[1]); + if( pAuxDb->nSession ){ + ii = sqlite3session_indirect(pSession->p, ii); + sqlite3_fprintf(p->out, + "session %s indirect flag = %d\n", pSession->zName, ii); + } + }else + + /* .session isempty + ** Determine if the session is empty + */ + if( cli_strcmp(azCmd[0], "isempty")==0 ){ + int ii; + if( nCmd!=1 ) goto session_syntax_error; + if( pAuxDb->nSession ){ + ii = sqlite3session_isempty(pSession->p); + sqlite3_fprintf(p->out, + "session %s isempty flag = %d\n", pSession->zName, ii); + } + }else + + /* .session list + ** List all currently open sessions + */ + if( cli_strcmp(azCmd[0],"list")==0 ){ + for(i=0; i<pAuxDb->nSession; i++){ + sqlite3_fprintf(p->out, "%d %s\n", i, pAuxDb->aSession[i].zName); + } + }else + + /* .session open DB NAME + ** Open a new session called NAME on the attached database DB. + ** DB is normally "main". + */ + if( cli_strcmp(azCmd[0],"open")==0 ){ + char *zName; + if( nCmd!=3 ) goto session_syntax_error; + zName = azCmd[2]; + if( zName[0]==0 ) goto session_syntax_error; + for(i=0; i<pAuxDb->nSession; i++){ + if( cli_strcmp(pAuxDb->aSession[i].zName,zName)==0 ){ + sqlite3_fprintf(stderr,"Session \"%s\" already exists\n", zName); + goto meta_command_exit; + } + } + if( pAuxDb->nSession>=ArraySize(pAuxDb->aSession) ){ + sqlite3_fprintf(stderr, + "Maximum of %d sessions\n", ArraySize(pAuxDb->aSession)); + goto meta_command_exit; + } + pSession = &pAuxDb->aSession[pAuxDb->nSession]; + rc = sqlite3session_create(p->db, azCmd[1], &pSession->p); + if( rc ){ + sqlite3_fprintf(stderr,"Cannot open session: error code=%d\n", rc); + rc = 0; + goto meta_command_exit; + } + pSession->nFilter = 0; + sqlite3session_table_filter(pSession->p, session_filter, pSession); + pAuxDb->nSession++; + pSession->zName = sqlite3_mprintf("%s", zName); + shell_check_oom(pSession->zName); + }else + /* If no command name matches, show a syntax error */ + session_syntax_error: + showHelp(p->out, "session"); + }else +#endif + +#ifdef SQLITE_DEBUG + /* Undocumented commands for internal testing. Subject to change + ** without notice. */ + if( c=='s' && n>=10 && cli_strncmp(azArg[0], "selftest-", 9)==0 ){ + if( cli_strncmp(azArg[0]+9, "boolean", n-9)==0 ){ + int i, v; + for(i=1; i<nArg; i++){ + v = booleanValue(azArg[i]); + sqlite3_fprintf(p->out, "%s: %d 0x%x\n", azArg[i], v, v); + } + } + if( cli_strncmp(azArg[0]+9, "integer", n-9)==0 ){ + int i; sqlite3_int64 v; + for(i=1; i<nArg; i++){ + char zBuf[200]; + v = integerValue(azArg[i]); + sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v); + sqlite3_fputs(zBuf, p->out); + } + } + }else +#endif + + if( c=='s' && n>=4 && cli_strncmp(azArg[0],"selftest",n)==0 ){ + int bIsInit = 0; /* True to initialize the SELFTEST table */ + int bVerbose = 0; /* Verbose output */ + int bSelftestExists; /* True if SELFTEST already exists */ + int i, k; /* Loop counters */ + int nTest = 0; /* Number of tests runs */ + int nErr = 0; /* Number of errors seen */ + ShellText str; /* Answer for a query */ + sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */ + + open_db(p,0); + for(i=1; i<nArg; i++){ + const char *z = azArg[i]; + if( z[0]=='-' && z[1]=='-' ) z++; + if( cli_strcmp(z,"-init")==0 ){ + bIsInit = 1; + }else + if( cli_strcmp(z,"-v")==0 ){ + bVerbose++; + }else + { + sqlite3_fprintf(stderr, + "Unknown option \"%s\" on \"%s\"\n", azArg[i], azArg[0]); + sqlite3_fputs("Should be one of: --init -v\n", stderr); + rc = 1; + goto meta_command_exit; + } + } + if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0) + != SQLITE_OK ){ + bSelftestExists = 0; + }else{ + bSelftestExists = 1; + } + if( bIsInit ){ + createSelftestTable(p); + bSelftestExists = 1; + } + initText(&str); + appendText(&str, "x", 0); + for(k=bSelftestExists; k>=0; k--){ + if( k==1 ){ + rc = sqlite3_prepare_v2(p->db, + "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno", + -1, &pStmt, 0); + }else{ + rc = sqlite3_prepare_v2(p->db, + "VALUES(0,'memo','Missing SELFTEST table - default checks only','')," + " (1,'run','PRAGMA integrity_check','ok')", + -1, &pStmt, 0); + } + if( rc ){ + eputz("Error querying the selftest table\n"); + rc = 1; + sqlite3_finalize(pStmt); + goto meta_command_exit; + } + for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){ + int tno = sqlite3_column_int(pStmt, 0); + const char *zOp = (const char*)sqlite3_column_text(pStmt, 1); + const char *zSql = (const char*)sqlite3_column_text(pStmt, 2); + const char *zAns = (const char*)sqlite3_column_text(pStmt, 3); + + if( zOp==0 ) continue; + if( zSql==0 ) continue; + if( zAns==0 ) continue; + k = 0; + if( bVerbose>0 ){ + sqlite3_fprintf(stdout, "%d: %s %s\n", tno, zOp, zSql); + } + if( cli_strcmp(zOp,"memo")==0 ){ + sqlite3_fprintf(p->out, "%s\n", zSql); + }else + if( cli_strcmp(zOp,"run")==0 ){ + char *zErrMsg = 0; + str.n = 0; + str.zTxt[0] = 0; + rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg); + nTest++; + if( bVerbose ){ + sqlite3_fprintf(p->out, "Result: %s\n", str.zTxt); + } + if( rc || zErrMsg ){ + nErr++; + rc = 1; + sqlite3_fprintf(p->out, "%d: error-code-%d: %s\n", tno, rc,zErrMsg); + sqlite3_free(zErrMsg); + }else if( cli_strcmp(zAns,str.zTxt)!=0 ){ + nErr++; + rc = 1; + sqlite3_fprintf(p->out, "%d: Expected: [%s]\n", tno, zAns); + sqlite3_fprintf(p->out, "%d: Got: [%s]\n", tno, str.zTxt); + } + } + else{ + sqlite3_fprintf(stderr, + "Unknown operation \"%s\" on selftest line %d\n", zOp, tno); + rc = 1; + break; + } + } /* End loop over rows of content from SELFTEST */ + sqlite3_finalize(pStmt); + } /* End loop over k */ + freeText(&str); + sqlite3_fprintf(p->out, "%d errors out of %d tests\n", nErr, nTest); + }else + + if( c=='s' && cli_strncmp(azArg[0], "separator", n)==0 ){ + if( nArg<2 || nArg>3 ){ + eputz("Usage: .separator COL ?ROW?\n"); + rc = 1; + } + if( nArg>=2 ){ + sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, + "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]); + } + if( nArg>=3 ){ + sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, + "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]); + } + }else + + if( c=='s' && n>=4 && cli_strncmp(azArg[0],"sha3sum",n)==0 ){ + const char *zLike = 0; /* Which table to checksum. 0 means everything */ + int i; /* Loop counter */ + int bSchema = 0; /* Also hash the schema */ + int bSeparate = 0; /* Hash each table separately */ + int iSize = 224; /* Hash algorithm to use */ + int bDebug = 0; /* Only show the query that would have run */ + sqlite3_stmt *pStmt; /* For querying tables names */ + char *zSql; /* SQL to be run */ + char *zSep; /* Separator */ + ShellText sSql; /* Complete SQL for the query to run the hash */ + ShellText sQuery; /* Set of queries used to read all content */ + open_db(p, 0); + for(i=1; i<nArg; i++){ + const char *z = azArg[i]; + if( z[0]=='-' ){ + z++; + if( z[0]=='-' ) z++; + if( cli_strcmp(z,"schema")==0 ){ + bSchema = 1; + }else + if( cli_strcmp(z,"sha3-224")==0 || cli_strcmp(z,"sha3-256")==0 + || cli_strcmp(z,"sha3-384")==0 || cli_strcmp(z,"sha3-512")==0 + ){ + iSize = atoi(&z[5]); + }else + if( cli_strcmp(z,"debug")==0 ){ + bDebug = 1; + }else + { + sqlite3_fprintf(stderr, + "Unknown option \"%s\" on \"%s\"\n", azArg[i], azArg[0]); + showHelp(p->out, azArg[0]); + rc = 1; + goto meta_command_exit; + } + }else if( zLike ){ + eputz("Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n"); + rc = 1; + goto meta_command_exit; + }else{ + zLike = z; + bSeparate = 1; + if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1; + } + } + if( bSchema ){ + zSql = "SELECT lower(name) as tname FROM sqlite_schema" + " WHERE type='table' AND coalesce(rootpage,0)>1" + " UNION ALL SELECT 'sqlite_schema'" + " ORDER BY 1 collate nocase"; + }else{ + zSql = "SELECT lower(name) as tname FROM sqlite_schema" + " WHERE type='table' AND coalesce(rootpage,0)>1" + " AND name NOT LIKE 'sqlite__%' ESCAPE '_'" + " ORDER BY 1 collate nocase"; + } + sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); + initText(&sQuery); + initText(&sSql); + appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0); + zSep = "VALUES("; + while( SQLITE_ROW==sqlite3_step(pStmt) ){ + const char *zTab = (const char*)sqlite3_column_text(pStmt,0); + if( zTab==0 ) continue; + if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue; + if( cli_strncmp(zTab, "sqlite_",7)!=0 ){ + appendText(&sQuery,"SELECT * FROM ", 0); + appendText(&sQuery,zTab,'"'); + appendText(&sQuery," NOT INDEXED;", 0); + }else if( cli_strcmp(zTab, "sqlite_schema")==0 ){ + appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_schema" + " ORDER BY name;", 0); + }else if( cli_strcmp(zTab, "sqlite_sequence")==0 ){ + appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence" + " ORDER BY name;", 0); + }else if( cli_strcmp(zTab, "sqlite_stat1")==0 ){ + appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1" + " ORDER BY tbl,idx;", 0); + }else if( cli_strcmp(zTab, "sqlite_stat4")==0 ){ + appendText(&sQuery, "SELECT * FROM ", 0); + appendText(&sQuery, zTab, 0); + appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0); + } + appendText(&sSql, zSep, 0); + appendText(&sSql, sQuery.zTxt, '\''); + sQuery.n = 0; + appendText(&sSql, ",", 0); + appendText(&sSql, zTab, '\''); + zSep = "),("; + } + sqlite3_finalize(pStmt); + if( bSeparate ){ + zSql = sqlite3_mprintf( + "%s))" + " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label" + " FROM [sha3sum$query]", + sSql.zTxt, iSize); + }else{ + zSql = sqlite3_mprintf( + "%s))" + " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash" + " FROM [sha3sum$query]", + sSql.zTxt, iSize); + } + shell_check_oom(zSql); + freeText(&sQuery); + freeText(&sSql); + if( bDebug ){ + sqlite3_fprintf(p->out, "%s\n", zSql); + }else{ + shell_exec(p, zSql, 0); + } +#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) && !defined(SQLITE_OMIT_VIRTUALTABLE) + { + int lrc; + char *zRevText = /* Query for reversible to-blob-to-text check */ + "SELECT lower(name) as tname FROM sqlite_schema\n" + "WHERE type='table' AND coalesce(rootpage,0)>1\n" + "AND name NOT LIKE 'sqlite__%%' ESCAPE '_'%s\n" + "ORDER BY 1 collate nocase"; + zRevText = sqlite3_mprintf(zRevText, zLike? " AND name LIKE $tspec" : ""); + zRevText = sqlite3_mprintf( + /* lower-case query is first run, producing upper-case query. */ + "with tabcols as materialized(\n" + "select tname, cname\n" + "from (" + " select printf('\"%%w\"',ss.tname) as tname," + " printf('\"%%w\"',ti.name) as cname\n" + " from (%z) ss\n inner join pragma_table_info(tname) ti))\n" + "select 'SELECT total(bad_text_count) AS bad_text_count\n" + "FROM ('||group_concat(query, ' UNION ALL ')||')' as btc_query\n" + " from (select 'SELECT COUNT(*) AS bad_text_count\n" + "FROM '||tname||' WHERE '\n" + "||group_concat('CAST(CAST('||cname||' AS BLOB) AS TEXT)<>'||cname\n" + "|| ' AND typeof('||cname||')=''text'' ',\n" + "' OR ') as query, tname from tabcols group by tname)" + , zRevText); + shell_check_oom(zRevText); + if( bDebug ) sqlite3_fprintf(p->out, "%s\n", zRevText); + lrc = sqlite3_prepare_v2(p->db, zRevText, -1, &pStmt, 0); + if( lrc!=SQLITE_OK ){ + /* assert(lrc==SQLITE_NOMEM); // might also be SQLITE_ERROR if the + ** user does cruel and unnatural things like ".limit expr_depth 0". */ + rc = 1; + }else{ + if( zLike ) sqlite3_bind_text(pStmt,1,zLike,-1,SQLITE_STATIC); + lrc = SQLITE_ROW==sqlite3_step(pStmt); + if( lrc ){ + const char *zGenQuery = (char*)sqlite3_column_text(pStmt,0); + sqlite3_stmt *pCheckStmt; + lrc = sqlite3_prepare_v2(p->db, zGenQuery, -1, &pCheckStmt, 0); + if( bDebug ) sqlite3_fprintf(p->out, "%s\n", zGenQuery); + if( lrc!=SQLITE_OK ){ + rc = 1; + }else{ + if( SQLITE_ROW==sqlite3_step(pCheckStmt) ){ + double countIrreversible = sqlite3_column_double(pCheckStmt, 0); + if( countIrreversible>0 ){ + int sz = (int)(countIrreversible + 0.5); + sqlite3_fprintf(stderr, + "Digest includes %d invalidly encoded text field%s.\n", + sz, (sz>1)? "s": ""); + } + } + sqlite3_finalize(pCheckStmt); + } + sqlite3_finalize(pStmt); + } + } + if( rc ) eputz(".sha3sum failed.\n"); + sqlite3_free(zRevText); + } +#endif /* !defined(*_OMIT_SCHEMA_PRAGMAS) && !defined(*_OMIT_VIRTUALTABLE) */ + sqlite3_free(zSql); + }else + +#if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE) + if( c=='s' + && (cli_strncmp(azArg[0], "shell", n)==0 + || cli_strncmp(azArg[0],"system",n)==0) + ){ + char *zCmd; + int i, x; + failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]); + if( nArg<2 ){ + eputz("Usage: .system COMMAND\n"); + rc = 1; + goto meta_command_exit; + } + zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]); + for(i=2; i<nArg && zCmd!=0; i++){ + zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"", + zCmd, azArg[i]); + } + /*consoleRestore();*/ + x = zCmd!=0 ? system(zCmd) : 1; + /*consoleRenewSetup();*/ + sqlite3_free(zCmd); + if( x ) sqlite3_fprintf(stderr,"System command returns %d\n", x); + }else +#endif /* !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE) */ + + if( c=='s' && cli_strncmp(azArg[0], "show", n)==0 ){ + static const char *azBool[] = { "off", "on", "trigger", "full"}; + const char *zOut; + int i; + if( nArg!=1 ){ + eputz("Usage: .show\n"); + rc = 1; + goto meta_command_exit; + } + sqlite3_fprintf(p->out, "%12.12s: %s\n","echo", + azBool[ShellHasFlag(p, SHFLG_Echo)]); + sqlite3_fprintf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]); + sqlite3_fprintf(p->out, "%12.12s: %s\n","explain", + p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off"); + sqlite3_fprintf(p->out, "%12.12s: %s\n","headers", + azBool[p->showHeader!=0]); + if( p->mode==MODE_Column + || (p->mode>=MODE_Markdown && p->mode<=MODE_Box) + ){ + sqlite3_fprintf(p->out, + "%12.12s: %s --wrap %d --wordwrap %s --%squote\n", "mode", + modeDescr[p->mode], p->cmOpts.iWrap, + p->cmOpts.bWordWrap ? "on" : "off", + p->cmOpts.bQuote ? "" : "no"); + }else{ + sqlite3_fprintf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]); + } + sqlite3_fprintf(p->out, "%12.12s: ", "nullvalue"); + output_c_string(p->out, p->nullValue); + sqlite3_fputs("\n", p->out); + sqlite3_fprintf(p->out, "%12.12s: %s\n","output", + strlen30(p->outfile) ? p->outfile : "stdout"); + sqlite3_fprintf(p->out, "%12.12s: ", "colseparator"); + output_c_string(p->out, p->colSeparator); + sqlite3_fputs("\n", p->out); + sqlite3_fprintf(p->out, "%12.12s: ", "rowseparator"); + output_c_string(p->out, p->rowSeparator); + sqlite3_fputs("\n", p->out); + switch( p->statsOn ){ + case 0: zOut = "off"; break; + default: zOut = "on"; break; + case 2: zOut = "stmt"; break; + case 3: zOut = "vmstep"; break; + } + sqlite3_fprintf(p->out, "%12.12s: %s\n","stats", zOut); + sqlite3_fprintf(p->out, "%12.12s: ", "width"); + for (i=0;i<p->nWidth;i++) { + sqlite3_fprintf(p->out, "%d ", p->colWidth[i]); + } + sqlite3_fputs("\n", p->out); + sqlite3_fprintf(p->out, "%12.12s: %s\n", "filename", + p->pAuxDb->zDbFilename ? p->pAuxDb->zDbFilename : ""); + }else + + if( c=='s' && cli_strncmp(azArg[0], "stats", n)==0 ){ + if( nArg==2 ){ + if( cli_strcmp(azArg[1],"stmt")==0 ){ + p->statsOn = 2; + }else if( cli_strcmp(azArg[1],"vmstep")==0 ){ + p->statsOn = 3; + }else{ + p->statsOn = (u8)booleanValue(azArg[1]); + } + }else if( nArg==1 ){ + display_stats(p->db, p, 0); + }else{ + eputz("Usage: .stats ?on|off|stmt|vmstep?\n"); + rc = 1; + } + }else + + if( (c=='t' && n>1 && cli_strncmp(azArg[0], "tables", n)==0) + || (c=='i' && (cli_strncmp(azArg[0], "indices", n)==0 + || cli_strncmp(azArg[0], "indexes", n)==0) ) + ){ + sqlite3_stmt *pStmt; + char **azResult; + int nRow, nAlloc; + int ii; + ShellText s; + initText(&s); + open_db(p, 0); + rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0); + if( rc ){ + sqlite3_finalize(pStmt); + return shellDatabaseError(p->db); + } + + if( nArg>2 && c=='i' ){ + /* It is an historical accident that the .indexes command shows an error + ** when called with the wrong number of arguments whereas the .tables + ** command does not. */ + eputz("Usage: .indexes ?LIKE-PATTERN?\n"); + rc = 1; + sqlite3_finalize(pStmt); + goto meta_command_exit; + } + for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){ + const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1); + if( zDbName==0 ) continue; + if( s.zTxt && s.zTxt[0] ) appendText(&s, " UNION ALL ", 0); + if( sqlite3_stricmp(zDbName, "main")==0 ){ + appendText(&s, "SELECT name FROM ", 0); + }else{ + appendText(&s, "SELECT ", 0); + appendText(&s, zDbName, '\''); + appendText(&s, "||'.'||name FROM ", 0); + } + appendText(&s, zDbName, '"'); + appendText(&s, ".sqlite_schema ", 0); + if( c=='t' ){ + appendText(&s," WHERE type IN ('table','view')" + " AND name NOT LIKE 'sqlite__%' ESCAPE '_'" + " AND name LIKE ?1", 0); + }else{ + appendText(&s," WHERE type='index'" + " AND tbl_name LIKE ?1", 0); + } + } + rc = sqlite3_finalize(pStmt); + if( rc==SQLITE_OK ){ + appendText(&s, " ORDER BY 1", 0); + rc = sqlite3_prepare_v2(p->db, s.zTxt, -1, &pStmt, 0); + } + freeText(&s); + if( rc ) return shellDatabaseError(p->db); + + /* Run the SQL statement prepared by the above block. Store the results + ** as an array of nul-terminated strings in azResult[]. */ + nRow = nAlloc = 0; + azResult = 0; + if( nArg>1 ){ + sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT); + }else{ + sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC); + } + while( sqlite3_step(pStmt)==SQLITE_ROW ){ + if( nRow>=nAlloc ){ + char **azNew; + sqlite3_int64 n2 = 2*(sqlite3_int64)nAlloc + 10; + azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2); + shell_check_oom(azNew); + nAlloc = (int)n2; + azResult = azNew; + } + azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0)); + shell_check_oom(azResult[nRow]); + nRow++; + } + if( sqlite3_finalize(pStmt)!=SQLITE_OK ){ + rc = shellDatabaseError(p->db); + } + + /* Pretty-print the contents of array azResult[] to the output */ + if( rc==0 && nRow>0 ){ + int len, maxlen = 0; + int i, j; + int nPrintCol, nPrintRow; + for(i=0; i<nRow; i++){ + len = strlen30(azResult[i]); + if( len>maxlen ) maxlen = len; + } + nPrintCol = 80/(maxlen+2); + if( nPrintCol<1 ) nPrintCol = 1; + nPrintRow = (nRow + nPrintCol - 1)/nPrintCol; + for(i=0; i<nPrintRow; i++){ + for(j=i; j<nRow; j+=nPrintRow){ + char *zSp = j<nPrintRow ? "" : " "; + sqlite3_fprintf(p->out, + "%s%-*s", zSp, maxlen, azResult[j] ? azResult[j]:""); + } + sqlite3_fputs("\n", p->out); + } + } + + for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]); + sqlite3_free(azResult); + }else + +#ifndef SQLITE_SHELL_FIDDLE + /* Begin redirecting output to the file "testcase-out.txt" */ + if( c=='t' && cli_strcmp(azArg[0],"testcase")==0 ){ + output_reset(p); + p->out = output_file_open("testcase-out.txt"); + if( p->out==0 ){ + eputz("Error: cannot open 'testcase-out.txt'\n"); + } + if( nArg>=2 ){ + sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]); + }else{ + sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?"); + } + }else +#endif /* !defined(SQLITE_SHELL_FIDDLE) */ + +#ifndef SQLITE_UNTESTABLE + if( c=='t' && n>=8 && cli_strncmp(azArg[0], "testctrl", n)==0 ){ + static const struct { + const char *zCtrlName; /* Name of a test-control option */ + int ctrlCode; /* Integer code for that option */ + int unSafe; /* Not valid unless --unsafe-testing */ + const char *zUsage; /* Usage notes */ + } aCtrl[] = { + {"always", SQLITE_TESTCTRL_ALWAYS, 1, "BOOLEAN" }, + {"assert", SQLITE_TESTCTRL_ASSERT, 1, "BOOLEAN" }, + /*{"benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS,1, "" },*/ + {"bitvec_test", SQLITE_TESTCTRL_BITVEC_TEST, 1, "SIZE INT-ARRAY"}, + {"byteorder", SQLITE_TESTCTRL_BYTEORDER, 0, "" }, + {"extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,0,"BOOLEAN" }, + {"fault_install", SQLITE_TESTCTRL_FAULT_INSTALL, 1,"args..." }, + {"fk_no_action", SQLITE_TESTCTRL_FK_NO_ACTION, 0, "BOOLEAN" }, + {"imposter", SQLITE_TESTCTRL_IMPOSTER,1,"SCHEMA ON/OFF ROOTPAGE"}, + {"internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS,0,"" }, + {"json_selfcheck", SQLITE_TESTCTRL_JSON_SELFCHECK ,0,"BOOLEAN" }, + {"localtime_fault", SQLITE_TESTCTRL_LOCALTIME_FAULT,0,"BOOLEAN" }, + {"never_corrupt", SQLITE_TESTCTRL_NEVER_CORRUPT,1, "BOOLEAN" }, + {"optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS,0,"DISABLE-MASK ..."}, +#ifdef YYCOVERAGE + {"parser_coverage", SQLITE_TESTCTRL_PARSER_COVERAGE,0,"" }, +#endif + {"pending_byte", SQLITE_TESTCTRL_PENDING_BYTE,1, "OFFSET " }, + {"prng_restore", SQLITE_TESTCTRL_PRNG_RESTORE,0, "" }, + {"prng_save", SQLITE_TESTCTRL_PRNG_SAVE, 0, "" }, + {"prng_seed", SQLITE_TESTCTRL_PRNG_SEED, 0, "SEED ?db?" }, + {"seek_count", SQLITE_TESTCTRL_SEEK_COUNT, 0, "" }, + {"sorter_mmap", SQLITE_TESTCTRL_SORTER_MMAP, 0, "NMAX" }, + {"tune", SQLITE_TESTCTRL_TUNE, 1, "ID VALUE" }, + }; + int testctrl = -1; + int iCtrl = -1; + int rc2 = 0; /* 0: usage. 1: %d 2: %x 3: no-output */ + int isOk = 0; + int i, n2; + const char *zCmd = 0; + + open_db(p, 0); + zCmd = nArg>=2 ? azArg[1] : "help"; + + /* The argument can optionally begin with "-" or "--" */ + if( zCmd[0]=='-' && zCmd[1] ){ + zCmd++; + if( zCmd[0]=='-' && zCmd[1] ) zCmd++; + } + + /* --help lists all test-controls */ + if( cli_strcmp(zCmd,"help")==0 ){ + sqlite3_fputs("Available test-controls:\n", p->out); + for(i=0; i<ArraySize(aCtrl); i++){ + if( aCtrl[i].unSafe && !ShellHasFlag(p,SHFLG_TestingMode) ) continue; + sqlite3_fprintf(p->out, " .testctrl %s %s\n", + aCtrl[i].zCtrlName, aCtrl[i].zUsage); + } + rc = 1; + goto meta_command_exit; + } + + /* convert testctrl text option to value. allow any unique prefix + ** of the option name, or a numerical value. */ + n2 = strlen30(zCmd); + for(i=0; i<ArraySize(aCtrl); i++){ + if( aCtrl[i].unSafe && !ShellHasFlag(p,SHFLG_TestingMode) ) continue; + if( cli_strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){ + if( testctrl<0 ){ + testctrl = aCtrl[i].ctrlCode; + iCtrl = i; + }else{ + sqlite3_fprintf(stderr,"Error: ambiguous test-control: \"%s\"\n" + "Use \".testctrl --help\" for help\n", zCmd); + rc = 1; + goto meta_command_exit; + } + } + } + if( testctrl<0 ){ + sqlite3_fprintf(stderr,"Error: unknown test-control: %s\n" + "Use \".testctrl --help\" for help\n", zCmd); + }else{ + switch(testctrl){ + + /* Special processing for .testctrl opt MASK ... + ** Each MASK argument can be one of: + ** + ** +LABEL Enable the named optimization + ** + ** -LABEL Disable the named optimization + ** + ** INTEGER Mask of optimizations to disable + */ + case SQLITE_TESTCTRL_OPTIMIZATIONS: { + static const struct { + unsigned int mask; /* Mask for this optimization */ + unsigned int bDsply; /* Display this on output */ + const char *zLabel; /* Name of optimization */ + } aLabel[] = { + { 0x00000001, 1, "QueryFlattener" }, + { 0x00000001, 0, "Flatten" }, + { 0x00000002, 1, "WindowFunc" }, + { 0x00000004, 1, "GroupByOrder" }, + { 0x00000008, 1, "FactorOutConst" }, + { 0x00000010, 1, "DistinctOpt" }, + { 0x00000020, 1, "CoverIdxScan" }, + { 0x00000040, 1, "OrderByIdxJoin" }, + { 0x00000080, 1, "Transitive" }, + { 0x00000100, 1, "OmitNoopJoin" }, + { 0x00000200, 1, "CountOfView" }, + { 0x00000400, 1, "CursorHints" }, + { 0x00000800, 1, "Stat4" }, + { 0x00001000, 1, "PushDown" }, + { 0x00002000, 1, "SimplifyJoin" }, + { 0x00004000, 1, "SkipScan" }, + { 0x00008000, 1, "PropagateConst" }, + { 0x00010000, 1, "MinMaxOpt" }, + { 0x00020000, 1, "SeekScan" }, + { 0x00040000, 1, "OmitOrderBy" }, + { 0x00080000, 1, "BloomFilter" }, + { 0x00100000, 1, "BloomPulldown" }, + { 0x00200000, 1, "BalancedMerge" }, + { 0x00400000, 1, "ReleaseReg" }, + { 0x00800000, 1, "FlttnUnionAll" }, + { 0x01000000, 1, "IndexedEXpr" }, + { 0x02000000, 1, "Coroutines" }, + { 0x04000000, 1, "NullUnusedCols" }, + { 0x08000000, 1, "OnePass" }, + { 0x10000000, 1, "OrderBySubq" }, + { 0x20000000, 1, "StarQuery" }, + { 0x40000000, 1, "ExistsToJoin" }, + { 0xffffffff, 0, "All" }, + }; + unsigned int curOpt; + unsigned int newOpt; + unsigned int m; + int ii; + int nOff; + sqlite3_test_control(SQLITE_TESTCTRL_GETOPT, p->db, &curOpt); + newOpt = curOpt; + for(ii=2; ii<nArg; ii++){ + const char *z = azArg[ii]; + int useLabel = 0; + const char *zLabel = 0; + if( (z[0]=='+'|| z[0]=='-') && !IsDigit(z[1]) ){ + useLabel = z[0]; + zLabel = &z[1]; + }else if( !IsDigit(z[0]) && z[0]!=0 && !IsDigit(z[1]) ){ + useLabel = '+'; + zLabel = z; + }else{ + newOpt = (unsigned int)strtol(z,0,0); + } + if( useLabel ){ + int jj; + for(jj=0; jj<ArraySize(aLabel); jj++){ + if( sqlite3_stricmp(zLabel, aLabel[jj].zLabel)==0 ) break; + } + if( jj>=ArraySize(aLabel) ){ + sqlite3_fprintf(stderr, + "Error: no such optimization: \"%s\"\n", zLabel); + sqlite3_fputs("Should be one of:", stderr); + for(jj=0; jj<ArraySize(aLabel); jj++){ + sqlite3_fprintf(stderr," %s", aLabel[jj].zLabel); + } + sqlite3_fputs("\n", stderr); + rc = 1; + goto meta_command_exit; + } + if( useLabel=='+' ){ + newOpt &= ~aLabel[jj].mask; + }else{ + newOpt |= aLabel[jj].mask; + } + } + } + if( curOpt!=newOpt ){ + sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,p->db,newOpt); + } + for(ii=nOff=0, m=1; ii<32; ii++, m <<= 1){ + if( m & newOpt ) nOff++; + } + if( nOff<12 ){ + sqlite3_fputs("+All", p->out); + for(ii=0; ii<ArraySize(aLabel); ii++){ + if( !aLabel[ii].bDsply ) continue; + if( (newOpt & aLabel[ii].mask)!=0 ){ + sqlite3_fprintf(p->out, " -%s", aLabel[ii].zLabel); + } + } + }else{ + sqlite3_fputs("-All", p->out); + for(ii=0; ii<ArraySize(aLabel); ii++){ + if( !aLabel[ii].bDsply ) continue; + if( (newOpt & aLabel[ii].mask)==0 ){ + sqlite3_fprintf(p->out, " +%s", aLabel[ii].zLabel); + } + } + } + sqlite3_fputs("\n", p->out); + rc2 = isOk = 3; + break; + } + + /* sqlite3_test_control(int, db, int) */ + case SQLITE_TESTCTRL_FK_NO_ACTION: + if( nArg==3 ){ + unsigned int opt = (unsigned int)strtol(azArg[2], 0, 0); + rc2 = sqlite3_test_control(testctrl, p->db, opt); + isOk = 3; + } + break; + + /* sqlite3_test_control(int) */ + case SQLITE_TESTCTRL_PRNG_SAVE: + case SQLITE_TESTCTRL_PRNG_RESTORE: + case SQLITE_TESTCTRL_BYTEORDER: + if( nArg==2 ){ + rc2 = sqlite3_test_control(testctrl); + isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3; + } + break; + + /* sqlite3_test_control(int, uint) */ + case SQLITE_TESTCTRL_PENDING_BYTE: + if( nArg==3 ){ + unsigned int opt = (unsigned int)integerValue(azArg[2]); + rc2 = sqlite3_test_control(testctrl, opt); + isOk = 3; + } + break; + + /* sqlite3_test_control(int, int, sqlite3*) */ + case SQLITE_TESTCTRL_PRNG_SEED: + if( nArg==3 || nArg==4 ){ + int ii = (int)integerValue(azArg[2]); + sqlite3 *db; + if( ii==0 && cli_strcmp(azArg[2],"random")==0 ){ + sqlite3_randomness(sizeof(ii),&ii); + sqlite3_fprintf(stdout, "-- random seed: %d\n", ii); + } + if( nArg==3 ){ + db = 0; + }else{ + db = p->db; + /* Make sure the schema has been loaded */ + sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0); + } + rc2 = sqlite3_test_control(testctrl, ii, db); + isOk = 3; + } + break; + + /* sqlite3_test_control(int, int) */ + case SQLITE_TESTCTRL_ASSERT: + case SQLITE_TESTCTRL_ALWAYS: + if( nArg==3 ){ + int opt = booleanValue(azArg[2]); + rc2 = sqlite3_test_control(testctrl, opt); + isOk = 1; + } + break; + + /* sqlite3_test_control(int, int) */ + case SQLITE_TESTCTRL_LOCALTIME_FAULT: + case SQLITE_TESTCTRL_NEVER_CORRUPT: + if( nArg==3 ){ + int opt = booleanValue(azArg[2]); + rc2 = sqlite3_test_control(testctrl, opt); + isOk = 3; + } + break; + + /* sqlite3_test_control(sqlite3*) */ + case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS: + rc2 = sqlite3_test_control(testctrl, p->db); + isOk = 3; + break; + + case SQLITE_TESTCTRL_IMPOSTER: + if( nArg==5 ){ + rc2 = sqlite3_test_control(testctrl, p->db, + azArg[2], + integerValue(azArg[3]), + integerValue(azArg[4])); + isOk = 3; + } + break; + + case SQLITE_TESTCTRL_SEEK_COUNT: { + u64 x = 0; + rc2 = sqlite3_test_control(testctrl, p->db, &x); + sqlite3_fprintf(p->out, "%llu\n", x); + isOk = 3; + break; + } + +#ifdef YYCOVERAGE + case SQLITE_TESTCTRL_PARSER_COVERAGE: { + if( nArg==2 ){ + sqlite3_test_control(testctrl, p->out); + isOk = 3; + } + break; + } +#endif +#ifdef SQLITE_DEBUG + case SQLITE_TESTCTRL_TUNE: { + if( nArg==4 ){ + int id = (int)integerValue(azArg[2]); + int val = (int)integerValue(azArg[3]); + sqlite3_test_control(testctrl, id, &val); + isOk = 3; + }else if( nArg==3 ){ + int id = (int)integerValue(azArg[2]); + sqlite3_test_control(testctrl, -id, &rc2); + isOk = 1; + }else if( nArg==2 ){ + int id = 1; + while(1){ + int val = 0; + rc2 = sqlite3_test_control(testctrl, -id, &val); + if( rc2!=SQLITE_OK ) break; + if( id>1 ) sqlite3_fputs(" ", p->out); + sqlite3_fprintf(p->out, "%d: %d", id, val); + id++; + } + if( id>1 ) sqlite3_fputs("\n", p->out); + isOk = 3; + } + break; + } +#endif + case SQLITE_TESTCTRL_SORTER_MMAP: + if( nArg==3 ){ + int opt = (unsigned int)integerValue(azArg[2]); + rc2 = sqlite3_test_control(testctrl, p->db, opt); + isOk = 3; + } + break; + case SQLITE_TESTCTRL_JSON_SELFCHECK: + if( nArg==2 ){ + rc2 = -1; + isOk = 1; + }else{ + rc2 = booleanValue(azArg[2]); + isOk = 3; + } + sqlite3_test_control(testctrl, &rc2); + break; + case SQLITE_TESTCTRL_BITVEC_TEST: { + /* Examples: + ** .testctrl bitvec_test 100 6,1 -- Show BITVEC constants + ** .testctrl bitvec_test 1000 1,12,7,3 -- Simple test + ** ---- -------- + ** size of Bitvec -----^ ^--- aOp array. 0 added at end. + ** + ** See comments on sqlite3BitvecBuiltinTest() for more information + ** about the aOp[] array. + */ + int iSize; + const char *zTestArg; + int nOp; + int ii, jj, x; + int *aOp; + if( nArg!=4 ){ + sqlite3_fprintf(stderr, + "ERROR - should be: \".testctrl bitvec_test SIZE INT-ARRAY\"\n" + ); + rc = 1; + goto meta_command_exit; + } + isOk = 3; + iSize = (int)integerValue(azArg[2]); + zTestArg = azArg[3]; + nOp = (int)strlen(zTestArg)+1; + aOp = malloc( sizeof(int)*(nOp+1) ); + shell_check_oom(aOp); + memset(aOp, 0, sizeof(int)*(nOp+1) ); + for(ii = jj = x = 0; zTestArg[ii]!=0; ii++){ + if( IsDigit(zTestArg[ii]) ){ + x = x*10 + zTestArg[ii] - '0'; + }else{ + aOp[jj++] = x; + x = 0; + } + } + aOp[jj] = x; + x = sqlite3_test_control(testctrl, iSize, aOp); + sqlite3_fprintf(p->out, "result: %d\n", x); + free(aOp); + break; + } + case SQLITE_TESTCTRL_FAULT_INSTALL: { + int kk; + int bShowHelp = nArg<=2; + isOk = 3; + for(kk=2; kk<nArg; kk++){ + const char *z = azArg[kk]; + if( z[0]=='-' && z[1]=='-' ) z++; + if( cli_strcmp(z,"off")==0 ){ + sqlite3_test_control(testctrl, 0); + }else if( cli_strcmp(z,"on")==0 ){ + faultsim_state.iCnt = faultsim_state.nSkip; + if( faultsim_state.iErr==0 ) faultsim_state.iErr = 1; + faultsim_state.nHit = 0; + sqlite3_test_control(testctrl, faultsim_callback); + }else if( cli_strcmp(z,"reset")==0 ){ + faultsim_state.iCnt = faultsim_state.nSkip; + faultsim_state.nHit = 0; + sqlite3_test_control(testctrl, faultsim_callback); + }else if( cli_strcmp(z,"status")==0 ){ + sqlite3_fprintf(p->out, "faultsim.iId: %d\n", + faultsim_state.iId); + sqlite3_fprintf(p->out, "faultsim.iErr: %d\n", + faultsim_state.iErr); + sqlite3_fprintf(p->out, "faultsim.iCnt: %d\n", + faultsim_state.iCnt); + sqlite3_fprintf(p->out, "faultsim.nHit: %d\n", + faultsim_state.nHit); + sqlite3_fprintf(p->out, "faultsim.iInterval: %d\n", + faultsim_state.iInterval); + sqlite3_fprintf(p->out, "faultsim.eVerbose: %d\n", + faultsim_state.eVerbose); + sqlite3_fprintf(p->out, "faultsim.nRepeat: %d\n", + faultsim_state.nRepeat); + sqlite3_fprintf(p->out, "faultsim.nSkip: %d\n", + faultsim_state.nSkip); + }else if( cli_strcmp(z,"-v")==0 ){ + if( faultsim_state.eVerbose<2 ) faultsim_state.eVerbose++; + }else if( cli_strcmp(z,"-q")==0 ){ + if( faultsim_state.eVerbose>0 ) faultsim_state.eVerbose--; + }else if( cli_strcmp(z,"-id")==0 && kk+1<nArg ){ + faultsim_state.iId = atoi(azArg[++kk]); + }else if( cli_strcmp(z,"-errcode")==0 && kk+1<nArg ){ + faultsim_state.iErr = atoi(azArg[++kk]); + }else if( cli_strcmp(z,"-interval")==0 && kk+1<nArg ){ + faultsim_state.iInterval = atoi(azArg[++kk]); + }else if( cli_strcmp(z,"-repeat")==0 && kk+1<nArg ){ + faultsim_state.nRepeat = atoi(azArg[++kk]); + }else if( cli_strcmp(z,"-skip")==0 && kk+1<nArg ){ + faultsim_state.nSkip = atoi(azArg[++kk]); + }else if( cli_strcmp(z,"-?")==0 || sqlite3_strglob("*help*",z)==0){ + bShowHelp = 1; + }else{ + sqlite3_fprintf(stderr, + "Unrecognized fault_install argument: \"%s\"\n", + azArg[kk]); + rc = 1; + bShowHelp = 1; + break; + } + } + if( bShowHelp ){ + sqlite3_fputs( + "Usage: .testctrl fault_install ARGS\n" + "Possible arguments:\n" + " off Disable faultsim\n" + " on Activate faultsim\n" + " reset Reset the trigger counter\n" + " status Show current status\n" + " -v Increase verbosity\n" + " -q Decrease verbosity\n" + " --errcode N When triggered, return N as error code\n" + " --id ID Trigger only for the ID specified\n" + " --interval N Trigger only after every N-th call\n" + " --repeat N Turn off after N hits. 0 means never\n" + " --skip N Skip the first N encounters\n" + ,p->out + ); + } + break; + } + } + } + if( isOk==0 && iCtrl>=0 ){ + sqlite3_fprintf(p->out, + "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage); + rc = 1; + }else if( isOk==1 ){ + sqlite3_fprintf(p->out, "%d\n", rc2); + }else if( isOk==2 ){ + sqlite3_fprintf(p->out, "0x%08x\n", rc2); + } + }else +#endif /* !defined(SQLITE_UNTESTABLE) */ + + if( c=='t' && n>4 && cli_strncmp(azArg[0], "timeout", n)==0 ){ + open_db(p, 0); + sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0); + }else + + if( c=='t' && n>=5 && cli_strncmp(azArg[0], "timer", n)==0 ){ + if( nArg==2 ){ + enableTimer = booleanValue(azArg[1]); + if( enableTimer && !HAS_TIMER ){ + eputz("Error: timer not available on this system.\n"); + enableTimer = 0; + } + }else{ + eputz("Usage: .timer on|off\n"); + rc = 1; + } + }else + +#ifndef SQLITE_OMIT_TRACE + if( c=='t' && cli_strncmp(azArg[0], "trace", n)==0 ){ + int mType = 0; + int jj; + open_db(p, 0); + for(jj=1; jj<nArg; jj++){ + const char *z = azArg[jj]; + if( z[0]=='-' ){ + if( optionMatch(z, "expanded") ){ + p->eTraceType = SHELL_TRACE_EXPANDED; + } +#ifdef SQLITE_ENABLE_NORMALIZE + else if( optionMatch(z, "normalized") ){ + p->eTraceType = SHELL_TRACE_NORMALIZED; + } +#endif + else if( optionMatch(z, "plain") ){ + p->eTraceType = SHELL_TRACE_PLAIN; + } + else if( optionMatch(z, "profile") ){ + mType |= SQLITE_TRACE_PROFILE; + } + else if( optionMatch(z, "row") ){ + mType |= SQLITE_TRACE_ROW; + } + else if( optionMatch(z, "stmt") ){ + mType |= SQLITE_TRACE_STMT; + } + else if( optionMatch(z, "close") ){ + mType |= SQLITE_TRACE_CLOSE; + } + else { + sqlite3_fprintf(stderr,"Unknown option \"%s\" on \".trace\"\n", z); + rc = 1; + goto meta_command_exit; + } + }else{ + output_file_close(p->traceOut); + p->traceOut = output_file_open(z); + } + } + if( p->traceOut==0 ){ + sqlite3_trace_v2(p->db, 0, 0, 0); + }else{ + if( mType==0 ) mType = SQLITE_TRACE_STMT; + sqlite3_trace_v2(p->db, mType, sql_trace_callback, p); + } + }else +#endif /* !defined(SQLITE_OMIT_TRACE) */ + +#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE) + if( c=='u' && cli_strncmp(azArg[0], "unmodule", n)==0 ){ + int ii; + int lenOpt; + char *zOpt; + if( nArg<2 ){ + eputz("Usage: .unmodule [--allexcept] NAME ...\n"); + rc = 1; + goto meta_command_exit; + } + open_db(p, 0); + zOpt = azArg[1]; + if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++; + lenOpt = (int)strlen(zOpt); + if( lenOpt>=3 && cli_strncmp(zOpt, "-allexcept",lenOpt)==0 ){ + assert( azArg[nArg]==0 ); + sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0); + }else{ + for(ii=1; ii<nArg; ii++){ + sqlite3_create_module(p->db, azArg[ii], 0, 0); + } + } + }else +#endif + + if( c=='v' && cli_strncmp(azArg[0], "version", n)==0 ){ + char *zPtrSz = sizeof(void*)==8 ? "64-bit" : "32-bit"; + sqlite3_fprintf(p->out, "SQLite %s %s\n" /*extra-version-info*/, + sqlite3_libversion(), sqlite3_sourceid()); +/* BEGIN SQLCIPHER */ +#ifdef SQLITE_HAS_CODEC + { + extern char* sqlcipher_version(); + char *sqlcipher_ver = sqlcipher_version(); + sqlite3_fprintf(p->out, "SQLCipher %s\n", sqlcipher_ver); + sqlite3_free(sqlcipher_ver); + } +#endif +/* END SQLCIPHER */ +#if SQLITE_HAVE_ZLIB + sqlite3_fprintf(p->out, "zlib version %s\n", zlibVersion()); +#endif +#define CTIMEOPT_VAL_(opt) #opt +#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt) +#if defined(__clang__) && defined(__clang_major__) + sqlite3_fprintf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "." + CTIMEOPT_VAL(__clang_minor__) "." + CTIMEOPT_VAL(__clang_patchlevel__) " (%s)\n", zPtrSz); +#elif defined(_MSC_VER) + sqlite3_fprintf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) " (%s)\n", zPtrSz); +#elif defined(__GNUC__) && defined(__VERSION__) + sqlite3_fprintf(p->out, "gcc-" __VERSION__ " (%s)\n", zPtrSz); +#endif + }else + + if( c=='v' && cli_strncmp(azArg[0], "vfsinfo", n)==0 ){ + const char *zDbName = nArg==2 ? azArg[1] : "main"; + sqlite3_vfs *pVfs = 0; + if( p->db ){ + sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs); + if( pVfs ){ + sqlite3_fprintf(p->out, "vfs.zName = \"%s\"\n", pVfs->zName); + sqlite3_fprintf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion); + sqlite3_fprintf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile); + sqlite3_fprintf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname); + } + } + }else + + if( c=='v' && cli_strncmp(azArg[0], "vfslist", n)==0 ){ + sqlite3_vfs *pVfs; + sqlite3_vfs *pCurrent = 0; + if( p->db ){ + sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent); + } + for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){ + sqlite3_fprintf(p->out, "vfs.zName = \"%s\"%s\n", pVfs->zName, + pVfs==pCurrent ? " <--- CURRENT" : ""); + sqlite3_fprintf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion); + sqlite3_fprintf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile); + sqlite3_fprintf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname); + if( pVfs->pNext ){ + sqlite3_fputs("-----------------------------------\n", p->out); + } + } + }else + + if( c=='v' && cli_strncmp(azArg[0], "vfsname", n)==0 ){ + const char *zDbName = nArg==2 ? azArg[1] : "main"; + char *zVfsName = 0; + if( p->db ){ + sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName); + if( zVfsName ){ + sqlite3_fprintf(p->out, "%s\n", zVfsName); + sqlite3_free(zVfsName); + } + } + }else + + if( c=='w' && cli_strncmp(azArg[0], "wheretrace", n)==0 ){ + unsigned int x = nArg>=2? (unsigned int)integerValue(azArg[1]) : 0xffffffff; + sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &x); + }else + + if( c=='w' && cli_strncmp(azArg[0], "width", n)==0 ){ + int j; + assert( nArg<=ArraySize(azArg) ); + p->nWidth = nArg-1; + p->colWidth = realloc(p->colWidth, (p->nWidth+1)*sizeof(int)*2); + if( p->colWidth==0 && p->nWidth>0 ) shell_out_of_memory(); + if( p->nWidth ) p->actualWidth = &p->colWidth[p->nWidth]; + for(j=1; j<nArg; j++){ + i64 w = integerValue(azArg[j]); + if( w < -30000 ) w = -30000; + if( w > +30000 ) w = +30000; + p->colWidth[j-1] = (int)w; + } + }else + + { + sqlite3_fprintf(stderr,"Error: unknown command or invalid arguments: " + " \"%s\". Enter \".help\" for help\n", azArg[0]); + rc = 1; + } + +meta_command_exit: + if( p->outCount ){ + p->outCount--; + if( p->outCount==0 ) output_reset(p); + } + p->bSafeMode = p->bSafeModePersist; + return rc; +} + +/* Line scan result and intermediate states (supporting scan resumption) +*/ +#ifndef CHAR_BIT +# define CHAR_BIT 8 +#endif +typedef enum { + QSS_HasDark = 1<<CHAR_BIT, QSS_EndingSemi = 2<<CHAR_BIT, + QSS_CharMask = (1<<CHAR_BIT)-1, QSS_ScanMask = 3<<CHAR_BIT, + QSS_Start = 0 +} QuickScanState; +#define QSS_SETV(qss, newst) ((newst) | ((qss) & QSS_ScanMask)) +#define QSS_INPLAIN(qss) (((qss)&QSS_CharMask)==QSS_Start) +#define QSS_PLAINWHITE(qss) (((qss)&~QSS_EndingSemi)==QSS_Start) +#define QSS_PLAINDARK(qss) (((qss)&~QSS_EndingSemi)==QSS_HasDark) +#define QSS_SEMITERM(qss) (((qss)&~QSS_HasDark)==QSS_EndingSemi) + +/* +** Scan line for classification to guide shell's handling. +** The scan is resumable for subsequent lines when prior +** return values are passed as the 2nd argument. +*/ +static QuickScanState quickscan(char *zLine, QuickScanState qss, + SCAN_TRACKER_REFTYPE pst){ + char cin; + char cWait = (char)qss; /* intentional narrowing loss */ + if( cWait==0 ){ + PlainScan: + while( (cin = *zLine++)!=0 ){ + if( IsSpace(cin) ) + continue; + switch (cin){ + case '-': + if( *zLine!='-' ) + break; + while((cin = *++zLine)!=0 ) + if( cin=='\n') + goto PlainScan; + return qss; + case ';': + qss |= QSS_EndingSemi; + continue; + case '/': + if( *zLine=='*' ){ + ++zLine; + cWait = '*'; + CONTINUE_PROMPT_AWAITS(pst, "/*"); + qss = QSS_SETV(qss, cWait); + goto TermScan; + } + break; + case '[': + cin = ']'; + deliberate_fall_through; /* FALLTHRU */ + case '`': case '\'': case '"': + cWait = cin; + qss = QSS_HasDark | cWait; + CONTINUE_PROMPT_AWAITC(pst, cin); + goto TermScan; + case '(': + CONTINUE_PAREN_INCR(pst, 1); + break; + case ')': + CONTINUE_PAREN_INCR(pst, -1); + break; + default: + break; + } + qss = (qss & ~QSS_EndingSemi) | QSS_HasDark; + } + }else{ + TermScan: + while( (cin = *zLine++)!=0 ){ + if( cin==cWait ){ + switch( cWait ){ + case '*': + if( *zLine != '/' ) + continue; + ++zLine; + CONTINUE_PROMPT_AWAITC(pst, 0); + qss = QSS_SETV(qss, 0); + goto PlainScan; + case '`': case '\'': case '"': + if(*zLine==cWait){ + /* Swallow doubled end-delimiter.*/ + ++zLine; + continue; + } + deliberate_fall_through; /* FALLTHRU */ + case ']': + CONTINUE_PROMPT_AWAITC(pst, 0); + qss = QSS_SETV(qss, 0); + goto PlainScan; + default: assert(0); + } + } + } + } + return qss; +} + +/* +** Return TRUE if the line typed in is an SQL command terminator other +** than a semi-colon. The SQL Server style "go" command is understood +** as is the Oracle "/". +*/ +static int line_is_command_terminator(char *zLine){ + while( IsSpace(zLine[0]) ){ zLine++; }; + if( zLine[0]=='/' ) + zLine += 1; /* Oracle */ + else if ( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o' ) + zLine += 2; /* SQL Server */ + else + return 0; + return quickscan(zLine, QSS_Start, 0)==QSS_Start; +} + +/* +** The CLI needs a working sqlite3_complete() to work properly. So error +** out of the build if compiling with SQLITE_OMIT_COMPLETE. +*/ +#ifdef SQLITE_OMIT_COMPLETE +# error the CLI application is incompatible with SQLITE_OMIT_COMPLETE. +#endif + +/* +** Return true if zSql is a complete SQL statement. Return false if it +** ends in the middle of a string literal or C-style comment. +*/ +static int line_is_complete(char *zSql, int nSql){ + int rc; + if( zSql==0 ) return 1; + zSql[nSql] = ';'; + zSql[nSql+1] = 0; + rc = sqlite3_complete(zSql); + zSql[nSql] = 0; + return rc; +} + +/* +** This function is called after processing each line of SQL in the +** runOneSqlLine() function. Its purpose is to detect scenarios where +** defensive mode should be automatically turned off. Specifically, when +** +** 1. The first line of input is "PRAGMA foreign_keys=OFF;", +** 2. The second line of input is "BEGIN TRANSACTION;", +** 3. The database is empty, and +** 4. The shell is not running in --safe mode. +** +** The implementation uses the ShellState.eRestoreState to maintain state: +** +** 0: Have not seen any SQL. +** 1: Have seen "PRAGMA foreign_keys=OFF;". +** 2-6: Currently running .dump transaction. If the "2" bit is set, +** disable DEFENSIVE when done. If "4" is set, disable DQS_DDL. +** 7: Nothing left to do. This function becomes a no-op. +*/ +static int doAutoDetectRestore(ShellState *p, const char *zSql){ + int rc = SQLITE_OK; + + if( p->eRestoreState<7 ){ + switch( p->eRestoreState ){ + case 0: { + const char *zExpect = "PRAGMA foreign_keys=OFF;"; + assert( strlen(zExpect)==24 ); + if( p->bSafeMode==0 + && strlen(zSql)>=24 + && memcmp(zSql, zExpect, 25)==0 + ){ + p->eRestoreState = 1; + }else{ + p->eRestoreState = 7; + } + break; + }; + + case 1: { + int bIsDump = 0; + const char *zExpect = "BEGIN TRANSACTION;"; + assert( strlen(zExpect)==18 ); + if( memcmp(zSql, zExpect, 19)==0 ){ + /* Now check if the database is empty. */ + const char *zQuery = "SELECT 1 FROM sqlite_schema LIMIT 1"; + sqlite3_stmt *pStmt = 0; + + bIsDump = 1; + shellPrepare(p->db, &rc, zQuery, &pStmt); + if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){ + bIsDump = 0; + } + shellFinalize(&rc, pStmt); + } + if( bIsDump && rc==SQLITE_OK ){ + int bDefense = 0; + int bDqsDdl = 0; + sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &bDefense); + sqlite3_db_config(p->db, SQLITE_DBCONFIG_DQS_DDL, -1, &bDqsDdl); + sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0); + sqlite3_db_config(p->db, SQLITE_DBCONFIG_DQS_DDL, 1, 0); + p->eRestoreState = (bDefense ? 2 : 0) + (bDqsDdl ? 4 : 0); + }else{ + p->eRestoreState = 7; + } + break; + } + + default: { + if( sqlite3_get_autocommit(p->db) ){ + if( (p->eRestoreState & 2) ){ + sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 1, 0); + } + if( (p->eRestoreState & 4) ){ + sqlite3_db_config(p->db, SQLITE_DBCONFIG_DQS_DDL, 0, 0); + } + p->eRestoreState = 7; + } + break; + } + } + } + + return rc; +} + +/* +** Run a single line of SQL. Return the number of errors. +*/ +static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){ + int rc; + char *zErrMsg = 0; + + open_db(p, 0); + if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql); + if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0; + BEGIN_TIMER; + rc = shell_exec(p, zSql, &zErrMsg); + END_TIMER(p->out); + if( rc || zErrMsg ){ + char zPrefix[100]; + const char *zErrorTail; + const char *zErrorType; + if( zErrMsg==0 ){ + zErrorType = "Error"; + zErrorTail = sqlite3_errmsg(p->db); + }else if( cli_strncmp(zErrMsg, "in prepare, ",12)==0 ){ + zErrorType = "Parse error"; + zErrorTail = &zErrMsg[12]; + }else if( cli_strncmp(zErrMsg, "stepping, ", 10)==0 ){ + zErrorType = "Runtime error"; + zErrorTail = &zErrMsg[10]; + }else{ + zErrorType = "Error"; + zErrorTail = zErrMsg; + } + if( in!=0 || !stdin_is_interactive ){ + sqlite3_snprintf(sizeof(zPrefix), zPrefix, + "%s near line %d:", zErrorType, startline); + }else{ + sqlite3_snprintf(sizeof(zPrefix), zPrefix, "%s:", zErrorType); + } + sqlite3_fprintf(stderr,"%s %s\n", zPrefix, zErrorTail); + sqlite3_free(zErrMsg); + zErrMsg = 0; + return 1; + }else if( ShellHasFlag(p, SHFLG_CountChanges) ){ + char zLineBuf[2000]; + sqlite3_snprintf(sizeof(zLineBuf), zLineBuf, + "changes: %lld total_changes: %lld", + sqlite3_changes64(p->db), sqlite3_total_changes64(p->db)); + sqlite3_fprintf(p->out, "%s\n", zLineBuf); + } + + if( doAutoDetectRestore(p, zSql) ) return 1; + return 0; +} + +static void echo_group_input(ShellState *p, const char *zDo){ + if( ShellHasFlag(p, SHFLG_Echo) ){ + sqlite3_fprintf(p->out, "%s\n", zDo); + fflush(p->out); + } +} + +#ifdef SQLITE_SHELL_FIDDLE +/* +** Alternate one_input_line() impl for wasm mode. This is not in the primary +** impl because we need the global shellState and cannot access it from that +** function without moving lots of code around (creating a larger/messier diff). +*/ +static char *one_input_line(FILE *in, char *zPrior, int isContinuation){ + /* Parse the next line from shellState.wasm.zInput. */ + const char *zBegin = shellState.wasm.zPos; + const char *z = zBegin; + char *zLine = 0; + i64 nZ = 0; + + UNUSED_PARAMETER(in); + UNUSED_PARAMETER(isContinuation); + if(!z || !*z){ + return 0; + } + while(*z && IsSpace(*z)) ++z; + zBegin = z; + for(; *z && '\n'!=*z; ++nZ, ++z){} + if(nZ>0 && '\r'==zBegin[nZ-1]){ + --nZ; + } + shellState.wasm.zPos = z; + zLine = realloc(zPrior, nZ+1); + shell_check_oom(zLine); + memcpy(zLine, zBegin, nZ); + zLine[nZ] = 0; + return zLine; +} +#endif /* SQLITE_SHELL_FIDDLE */ + +/* +** Read input from *in and process it. If *in==0 then input +** is interactive - the user is typing it it. Otherwise, input +** is coming from a file or device. A prompt is issued and history +** is saved only if input is interactive. An interrupt signal will +** cause this routine to exit immediately, unless input is interactive. +** +** Return the number of errors. +*/ +static int process_input(ShellState *p, const char *zSrc){ + char *zLine = 0; /* A single input line */ + char *zSql = 0; /* Accumulated SQL text */ + i64 nLine; /* Length of current line */ + i64 nSql = 0; /* Bytes of zSql[] used */ + i64 nAlloc = 0; /* Allocated zSql[] space */ + int rc; /* Error code */ + int errCnt = 0; /* Number of errors seen */ + i64 startline = 0; /* Line number for start of current input */ + QuickScanState qss = QSS_Start; /* Accumulated line status (so far) */ + + if( p->inputNesting==MAX_INPUT_NESTING ){ + /* This will be more informative in a later version. */ + sqlite3_fprintf(stderr,"%s: Input nesting limit (%d) reached at line %lld." + " Check recursion.\n", zSrc, MAX_INPUT_NESTING, p->lineno); + return 1; + } + ++p->inputNesting; + p->lineno = 0; + CONTINUE_PROMPT_RESET; + while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){ + fflush(p->out); + zLine = one_input_line(p->in, zLine, nSql>0); + if( zLine==0 ){ + /* End of input */ + if( p->in==0 && stdin_is_interactive ) sqlite3_fputs("\n", p->out); + break; + } + if( seenInterrupt ){ + if( p->in!=0 ) break; + seenInterrupt = 0; + } + p->lineno++; + if( QSS_INPLAIN(qss) + && line_is_command_terminator(zLine) + && line_is_complete(zSql, nSql) ){ + memcpy(zLine,";",2); + } + qss = quickscan(zLine, qss, CONTINUE_PROMPT_PSTATE); + if( QSS_PLAINWHITE(qss) && nSql==0 ){ + /* Just swallow single-line whitespace */ + echo_group_input(p, zLine); + qss = QSS_Start; + continue; + } + if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){ + CONTINUE_PROMPT_RESET; + echo_group_input(p, zLine); + if( zLine[0]=='.' ){ + rc = do_meta_command(zLine, p); + if( rc==2 ){ /* exit requested */ + break; + }else if( rc ){ + errCnt++; + } + } + qss = QSS_Start; + continue; + } + /* No single-line dispositions remain; accumulate line(s). */ + nLine = strlen(zLine); + if( nSql+nLine+2>=nAlloc ){ + /* Grow buffer by half-again increments when big. */ + nAlloc = nSql+(nSql>>1)+nLine+100; + zSql = realloc(zSql, nAlloc); + shell_check_oom(zSql); + } + if( nSql==0 ){ + i64 i; + for(i=0; zLine[i] && IsSpace(zLine[i]); i++){} + assert( nAlloc>0 && zSql!=0 ); + memcpy(zSql, zLine+i, nLine+1-i); + startline = p->lineno; + nSql = nLine-i; + }else{ + zSql[nSql++] = '\n'; + memcpy(zSql+nSql, zLine, nLine+1); + nSql += nLine; + } + if( nSql>0x7fff0000 ){ + char zSize[100]; + sqlite3_snprintf(sizeof(zSize),zSize,"%,lld",nSql); + sqlite3_fprintf(stderr, "%s:%lld: Input SQL is too big: %s bytes\n", + zSrc, startline, zSize); + nSql = 0; + errCnt++; + break; + }else if( nSql && QSS_SEMITERM(qss) && sqlite3_complete(zSql) ){ + echo_group_input(p, zSql); + errCnt += runOneSqlLine(p, zSql, p->in, startline); + CONTINUE_PROMPT_RESET; + nSql = 0; + if( p->outCount ){ + output_reset(p); + p->outCount = 0; + }else{ + clearTempFile(p); + } + p->bSafeMode = p->bSafeModePersist; + qss = QSS_Start; + }else if( nSql && QSS_PLAINWHITE(qss) ){ + echo_group_input(p, zSql); + nSql = 0; + qss = QSS_Start; + } + } + if( nSql ){ + /* This may be incomplete. Let the SQL parser deal with that. */ + echo_group_input(p, zSql); + errCnt += runOneSqlLine(p, zSql, p->in, startline); + CONTINUE_PROMPT_RESET; + } + free(zSql); + free(zLine); + --p->inputNesting; + return errCnt>0; +} + +/* +** Return a pathname which is the user's home directory. A +** 0 return indicates an error of some kind. +*/ +static char *find_home_dir(int clearFlag){ + static char *home_dir = NULL; + if( clearFlag ){ + free(home_dir); + home_dir = 0; + return 0; + } + if( home_dir ) return home_dir; + +#if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \ + && !defined(__RTP__) && !defined(_WRS_KERNEL) && !defined(SQLITE_WASI) + { + struct passwd *pwent; + uid_t uid = getuid(); + if( (pwent=getpwuid(uid)) != NULL) { + home_dir = pwent->pw_dir; + } + } +#endif + +#if defined(_WIN32_WCE) + /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv() + */ + home_dir = "/"; +#else + +#if defined(_WIN32) || defined(WIN32) + if (!home_dir) { + home_dir = getenv("USERPROFILE"); + } +#endif + + if (!home_dir) { + home_dir = getenv("HOME"); + } + +#if defined(_WIN32) || defined(WIN32) + if (!home_dir) { + char *zDrive, *zPath; + int n; + zDrive = getenv("HOMEDRIVE"); + zPath = getenv("HOMEPATH"); + if( zDrive && zPath ){ + n = strlen30(zDrive) + strlen30(zPath) + 1; + home_dir = malloc( n ); + if( home_dir==0 ) return 0; + sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath); + return home_dir; + } + home_dir = "c:\\"; + } +#endif + +#endif /* !_WIN32_WCE */ + + if( home_dir ){ + i64 n = strlen(home_dir) + 1; + char *z = malloc( n ); + if( z ) memcpy(z, home_dir, n); + home_dir = z; + } + + return home_dir; +} + +/* +** On non-Windows platforms, look for: +** +** - ${zEnvVar}/${zBaseName} +** - ${HOME}/${zSubdir}/${zBaseName} +** +** $zEnvVar is intended to be the name of an XDG_... environment +** variable, e.g. XDG_CONFIG_HOME or XDG_STATE_HOME. If zEnvVar is +** NULL or getenv(zEnvVar) is NULL then fall back to the second +** option. If the selected option is not found in the filesystem, +** return 0. +** +** zSubdir may be NULL or empty, in which case ${HOME}/${zBaseName} +** becomes the fallback. +** +** Both zSubdir and zBaseName may contain subdirectory parts. zSubdir +** will conventionally be ".config" or ".local/state", which, not +** coincidentally, is the typical subdir of the corresponding XDG_... +** var with the XDG var's $HOME prefix. +** +** The returned string is obtained from sqlite3_malloc() and should be +** sqlite3_free()'d by the caller. +*/ +static char *find_xdg_file(const char *zEnvVar, const char *zSubdir, + const char *zBaseName){ +#if defined(_WIN32) || defined(WIN32) || defined(_WIN32_WCE) \ + || defined(__RTP__) || defined(_WRS_KERNEL) + return 0; +#else + char *zConfigFile = 0; + const char *zXdgDir; + + zXdgDir = zEnvVar ? getenv(zEnvVar) : 0; + if( zXdgDir ){ + zConfigFile = sqlite3_mprintf("%s/%s", zXdgDir, zBaseName); + }else{ + const char * zHome = find_home_dir(0); + if( zHome==0 ) return 0; + zConfigFile = (zSubdir && *zSubdir) + ? sqlite3_mprintf("%s/%s/%s", zHome, zSubdir, zBaseName) + : sqlite3_mprintf("%s/%s", zHome, zBaseName); + } + shell_check_oom(zConfigFile); + if( access(zConfigFile,0)!=0 ){ + sqlite3_free(zConfigFile); + zConfigFile = 0; + } + return zConfigFile; +#endif +} + +/* +** Read input from the file sqliterc_override. If that parameter is +** NULL, take it from find_xdg_file(), if found, or fall back to +** ~/.sqliterc. +** +** Failure to read the config is only considered a failure if +** sqliterc_override is not NULL, in which case this function may emit +** a warning or, if ::bail_on_error is true, fail fatally if the file +** named by sqliterc_override is not found. +*/ +static void process_sqliterc( + ShellState *p, /* Configuration data */ + const char *sqliterc_override /* Name of config file. NULL to use default */ +){ + char *home_dir = NULL; + char *sqliterc = (char*)sqliterc_override; + FILE *inSaved = p->in; + i64 savedLineno = p->lineno; + + if( sqliterc == NULL ){ + sqliterc = find_xdg_file("XDG_CONFIG_HOME", + ".config", + "sqlite3/sqliterc"); + } + if( sqliterc == NULL ){ + home_dir = find_home_dir(0); + if( home_dir==0 ){ + eputz("-- warning: cannot find home directory;" + " cannot read ~/.sqliterc\n"); + return; + } + sqliterc = sqlite3_mprintf("%s/.sqliterc",home_dir); + shell_check_oom(sqliterc); + } + p->in = sqliterc ? sqlite3_fopen(sqliterc,"rb") : 0; + if( p->in ){ + if( stdin_is_interactive ){ + sqlite3_fprintf(stderr,"-- Loading resources from %s\n", sqliterc); + } + if( process_input(p, sqliterc) && bail_on_error ) exit(1); + fclose(p->in); + }else if( sqliterc_override!=0 ){ + sqlite3_fprintf(stderr,"cannot open: \"%s\"\n", sqliterc); + if( bail_on_error ) exit(1); + } + p->in = inSaved; + p->lineno = savedLineno; + if( sqliterc != sqliterc_override ){ + sqlite3_free(sqliterc); + } +} + +/* +** Show available command line options +*/ +static const char zOptions[] = + " -- treat no subsequent arguments as options\n" +#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE) + " -A ARGS... run \".archive ARGS\" and exit\n" +#endif + " -append append the database to the end of the file\n" + " -ascii set output mode to 'ascii'\n" + " -bail stop after hitting an error\n" + " -batch force batch I/O\n" + " -box set output mode to 'box'\n" + " -column set output mode to 'column'\n" + " -cmd COMMAND run \"COMMAND\" before reading stdin\n" + " -csv set output mode to 'csv'\n" +#if !defined(SQLITE_OMIT_DESERIALIZE) + " -deserialize open the database using sqlite3_deserialize()\n" +#endif + " -echo print inputs before execution\n" + " -escape T ctrl-char escape; T is one of: symbol, ascii, off\n" + " -init FILENAME read/process named file\n" + " -[no]header turn headers on or off\n" +#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) + " -heap SIZE Size of heap for memsys3 or memsys5\n" +#endif + " -help show this message\n" + " -html set output mode to HTML\n" + " -ifexists only open if database already exists\n" + " -interactive force interactive I/O\n" + " -json set output mode to 'json'\n" + " -line set output mode to 'line'\n" + " -list set output mode to 'list'\n" + " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n" + " -markdown set output mode to 'markdown'\n" +#if !defined(SQLITE_OMIT_DESERIALIZE) + " -maxsize N maximum size for a --deserialize database\n" +#endif + " -memtrace trace all memory allocations and deallocations\n" + " -mmap N default mmap size set to N\n" +#ifdef SQLITE_ENABLE_MULTIPLEX + " -multiplex enable the multiplexor VFS\n" +#endif + " -newline SEP set output row separator. Default: '\\n'\n" + " -nofollow refuse to open symbolic links to database files\n" + " -nonce STRING set the safe-mode escape nonce\n" + " -no-rowid-in-view Disable rowid-in-view using sqlite3_config()\n" + " -nullvalue TEXT set text string for NULL values. Default ''\n" + " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n" + " -pcachetrace trace all page cache operations\n" + " -quote set output mode to 'quote'\n" + " -readonly open the database read-only\n" + " -safe enable safe-mode\n" + " -separator SEP set output column separator. Default: '|'\n" +#ifdef SQLITE_ENABLE_SORTER_REFERENCES + " -sorterref SIZE sorter references threshold size\n" +#endif + " -stats print memory stats before each finalize\n" + " -table set output mode to 'table'\n" + " -tabs set output mode to 'tabs'\n" + " -unsafe-testing allow unsafe commands and modes for testing\n" + " -version show SQLite version\n" + " -vfs NAME use NAME as the default VFS\n" + " -vfstrace enable tracing of all VFS calls\n" +#ifdef SQLITE_HAVE_ZLIB + " -zip open the file as a ZIP Archive\n" +#endif +; +static void usage(int showDetail){ + sqlite3_fprintf(stderr,"Usage: %s [OPTIONS] [FILENAME [SQL...]]\n" + "FILENAME is the name of an SQLite database. A new database is created\n" + "if the file does not previously exist. Defaults to :memory:.\n", Argv0); + if( showDetail ){ + sqlite3_fprintf(stderr,"OPTIONS include:\n%s", zOptions); + }else{ + eputz("Use the -help option for additional information\n"); + } + exit(0); +} + +/* +** Internal check: Verify that the SQLite is uninitialized. Print a +** error message if it is initialized. +*/ +static void verify_uninitialized(void){ + if( sqlite3_config(-1)==SQLITE_MISUSE ){ + sputz(stdout, "WARNING: attempt to configure SQLite after" + " initialization.\n"); + } +} + +/* +** Initialize the state information in data +*/ +static void main_init(ShellState *data) { + memset(data, 0, sizeof(*data)); + data->normalMode = data->cMode = data->mode = MODE_List; + data->autoExplain = 1; +#ifdef _WIN32 + data->crlfMode = 1; +#endif + data->pAuxDb = &data->aAuxDb[0]; + memcpy(data->colSeparator,SEP_Column, 2); + memcpy(data->rowSeparator,SEP_Row, 2); + data->showHeader = 0; + data->shellFlgs = SHFLG_Lookaside; + sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data); +#if !defined(SQLITE_SHELL_FIDDLE) + verify_uninitialized(); +#endif + sqlite3_config(SQLITE_CONFIG_URI, 1); + sqlite3_config(SQLITE_CONFIG_MULTITHREAD); + sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> "); + sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> "); +} + +/* +** Output text to the console in a font that attracts extra attention. +*/ +#if defined(_WIN32) || defined(WIN32) +static void printBold(const char *zText){ +#if !SQLITE_OS_WINRT + HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE); + CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo; + GetConsoleScreenBufferInfo(out, &defaultScreenInfo); + SetConsoleTextAttribute(out, + FOREGROUND_RED|FOREGROUND_INTENSITY + ); +#endif + sputz(stdout, zText); +#if !SQLITE_OS_WINRT + SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes); +#endif +} +#else +static void printBold(const char *zText){ + sqlite3_fprintf(stdout, "\033[1m%s\033[0m", zText); +} +#endif + +/* +** Get the argument to an --option. Throw an error and die if no argument +** is available. +*/ +static char *cmdline_option_value(int argc, char **argv, int i){ + if( i==argc ){ + sqlite3_fprintf(stderr, + "%s: Error: missing argument to %s\n", argv[0], argv[argc-1]); + exit(1); + } + return argv[i]; +} + +static void sayAbnormalExit(void){ + if( seenInterrupt ) eputz("Program interrupted.\n"); +} + +/* Routine to output from vfstrace +*/ +static int vfstraceOut(const char *z, void *pArg){ + ShellState *p = (ShellState*)pArg; + sqlite3_fputs(z, p->out); + fflush(p->out); + return 1; +} + +#ifndef SQLITE_SHELL_IS_UTF8 +# if (defined(_WIN32) || defined(WIN32)) \ + && (defined(_MSC_VER) || (defined(UNICODE) && defined(__GNUC__))) +# define SQLITE_SHELL_IS_UTF8 (0) +# else +# define SQLITE_SHELL_IS_UTF8 (1) +# endif +#endif + +#ifdef SQLITE_SHELL_FIDDLE +# define main fiddle_main +#endif + +#if SQLITE_SHELL_IS_UTF8 +int SQLITE_CDECL main(int argc, char **argv){ +#else +int SQLITE_CDECL wmain(int argc, wchar_t **wargv){ + char **argv; +#endif +#ifdef SQLITE_DEBUG + sqlite3_int64 mem_main_enter = 0; +#endif + char *zErrMsg = 0; +#ifdef SQLITE_SHELL_FIDDLE +# define data shellState +#else + ShellState data; +#endif + const char *zInitFile = 0; + int i; + int rc = 0; + int warnInmemoryDb = 0; + int readStdin = 1; + int nCmd = 0; + int nOptsEnd = argc; + int bEnableVfstrace = 0; + char **azCmd = 0; + const char *zVfs = 0; /* Value of -vfs command-line option */ +#if !SQLITE_SHELL_IS_UTF8 + char **argvToFree = 0; + int argcToFree = 0; +#endif + setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */ + +#ifdef SQLITE_SHELL_FIDDLE + stdin_is_interactive = 0; + stdout_is_console = 1; + data.wasm.zDefaultDbName = "/fiddle.sqlite3"; +#else + stdin_is_interactive = isatty(0); + stdout_is_console = isatty(1); +#endif + atexit(sayAbnormalExit); +#ifdef SQLITE_DEBUG + mem_main_enter = sqlite3_memory_used(); +#endif +#if !defined(_WIN32_WCE) + if( getenv("SQLITE_DEBUG_BREAK") ){ + if( isatty(0) && isatty(2) ){ + char zLine[100]; + sqlite3_fprintf(stderr, + "attach debugger to process %d and press ENTER to continue...", + GETPID()); + if( sqlite3_fgets(zLine, sizeof(zLine), stdin)!=0 + && cli_strcmp(zLine,"stop")==0 + ){ + exit(1); + } + }else{ +#if defined(_WIN32) || defined(WIN32) +#if SQLITE_OS_WINRT + __debugbreak(); +#else + DebugBreak(); +#endif +#elif defined(SIGTRAP) + raise(SIGTRAP); +#endif + } + } +#endif + /* Register a valid signal handler early, before much else is done. */ +#ifdef SIGINT + signal(SIGINT, interrupt_handler); +#elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE) + if( !SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE) ){ + eputz("No ^C handler.\n"); + } +#endif + +#if USE_SYSTEM_SQLITE+0!=1 + if( cli_strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){ + sqlite3_fprintf(stderr, + "SQLite header and source version mismatch\n%s\n%s\n", + sqlite3_sourceid(), SQLITE_SOURCE_ID); + exit(1); + } +#endif + main_init(&data); + + /* On Windows, we must translate command-line arguments into UTF-8. + ** The SQLite memory allocator subsystem has to be enabled in order to + ** do this. But we want to run an sqlite3_shutdown() afterwards so that + ** subsequent sqlite3_config() calls will work. So copy all results into + ** memory that does not come from the SQLite memory allocator. + */ +#if !SQLITE_SHELL_IS_UTF8 + sqlite3_initialize(); + argvToFree = malloc(sizeof(argv[0])*argc*2); + shell_check_oom(argvToFree); + argcToFree = argc; + argv = argvToFree + argc; + for(i=0; i<argc; i++){ + char *z = sqlite3_win32_unicode_to_utf8(wargv[i]); + i64 n; + shell_check_oom(z); + n = strlen(z); + argv[i] = malloc( n+1 ); + shell_check_oom(argv[i]); + memcpy(argv[i], z, n+1); + argvToFree[i] = argv[i]; + sqlite3_free(z); + } + sqlite3_shutdown(); +#endif + + assert( argc>=1 && argv && argv[0] ); + Argv0 = argv[0]; + +#ifdef SQLITE_SHELL_DBNAME_PROC + { + /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name + ** of a C-function that will provide the name of the database file. Use + ** this compile-time option to embed this shell program in larger + ** applications. */ + extern void SQLITE_SHELL_DBNAME_PROC(const char**); + SQLITE_SHELL_DBNAME_PROC(&data.pAuxDb->zDbFilename); + warnInmemoryDb = 0; + } +#endif + + /* Do an initial pass through the command-line argument to locate + ** the name of the database file, the name of the initialization file, + ** the size of the alternative malloc heap, options affecting commands + ** or SQL run from the command line, and the first command to execute. + */ +#ifndef SQLITE_SHELL_FIDDLE + verify_uninitialized(); +#endif + for(i=1; i<argc; i++){ + char *z; + z = argv[i]; + if( z[0]!='-' || i>nOptsEnd ){ + if( data.aAuxDb->zDbFilename==0 ){ + data.aAuxDb->zDbFilename = z; + }else{ + /* Excess arguments are interpreted as SQL (or dot-commands) and + ** mean that nothing is read from stdin */ + readStdin = 0; + nCmd++; + azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd); + shell_check_oom(azCmd); + azCmd[nCmd-1] = z; + } + continue; + } + if( z[1]=='-' ) z++; + if( cli_strcmp(z, "-")==0 ){ + nOptsEnd = i; + continue; + }else if( cli_strcmp(z,"-separator")==0 + || cli_strcmp(z,"-nullvalue")==0 + || cli_strcmp(z,"-newline")==0 + || cli_strcmp(z,"-cmd")==0 + ){ + (void)cmdline_option_value(argc, argv, ++i); + }else if( cli_strcmp(z,"-init")==0 ){ + zInitFile = cmdline_option_value(argc, argv, ++i); + }else if( cli_strcmp(z,"-interactive")==0 ){ + }else if( cli_strcmp(z,"-batch")==0 ){ + /* Need to check for batch mode here to so we can avoid printing + ** informational messages (like from process_sqliterc) before + ** we do the actual processing of arguments later in a second pass. + */ + stdin_is_interactive = 0; + }else if( cli_strcmp(z,"-utf8")==0 ){ + }else if( cli_strcmp(z,"-no-utf8")==0 ){ + }else if( cli_strcmp(z,"-no-rowid-in-view")==0 ){ + int val = 0; + sqlite3_config(SQLITE_CONFIG_ROWID_IN_VIEW, &val); + assert( val==0 ); + }else if( cli_strcmp(z,"-heap")==0 ){ +#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) + const char *zSize; + sqlite3_int64 szHeap; + + zSize = cmdline_option_value(argc, argv, ++i); + szHeap = integerValue(zSize); + if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000; + verify_uninitialized(); + sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64); +#else + (void)cmdline_option_value(argc, argv, ++i); +#endif + }else if( cli_strcmp(z,"-pagecache")==0 ){ + sqlite3_int64 n, sz; + sz = integerValue(cmdline_option_value(argc,argv,++i)); + if( sz>65536 ) sz = 65536; + if( sz<0 ) sz = 0; + n = integerValue(cmdline_option_value(argc,argv,++i)); + if( sz>0 && n>0 && 0xffffffffffffLL/sz<n ){ + n = 0xffffffffffffLL/sz; + } + if( sz>0 && (sz & (sz-1))==0 ){ + /* If SIZE is a power of two, round it up by the PCACHE_HDRSZ */ + int szHdr = 0; + sqlite3_config(SQLITE_CONFIG_PCACHE_HDRSZ, &szHdr); + sz += szHdr; + sqlite3_fprintf(stdout, "Page cache size increased to %d to accommodate" + " the %d-byte headers\n", (int)sz, szHdr); + } + verify_uninitialized(); + sqlite3_config(SQLITE_CONFIG_PAGECACHE, + (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n); + data.shellFlgs |= SHFLG_Pagecache; + }else if( cli_strcmp(z,"-lookaside")==0 ){ + int n, sz; + sz = (int)integerValue(cmdline_option_value(argc,argv,++i)); + if( sz<0 ) sz = 0; + n = (int)integerValue(cmdline_option_value(argc,argv,++i)); + if( n<0 ) n = 0; + verify_uninitialized(); + sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n); + if( (i64)sz*(i64)n==0 ) data.shellFlgs &= ~SHFLG_Lookaside; + }else if( cli_strcmp(z,"-threadsafe")==0 ){ + int n; + n = (int)integerValue(cmdline_option_value(argc,argv,++i)); + verify_uninitialized(); + switch( n ){ + case 0: sqlite3_config(SQLITE_CONFIG_SINGLETHREAD); break; + case 2: sqlite3_config(SQLITE_CONFIG_MULTITHREAD); break; + default: sqlite3_config(SQLITE_CONFIG_SERIALIZED); break; + } + }else if( cli_strcmp(z,"-vfstrace")==0 ){ + bEnableVfstrace = 1; +#ifdef SQLITE_ENABLE_MULTIPLEX + }else if( cli_strcmp(z,"-multiplex")==0 ){ + extern int sqlite3_multiplex_initialize(const char*,int); + sqlite3_multiplex_initialize(0, 1); +#endif + }else if( cli_strcmp(z,"-mmap")==0 ){ + sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i)); + verify_uninitialized(); + sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz); +#if defined(SQLITE_ENABLE_SORTER_REFERENCES) + }else if( cli_strcmp(z,"-sorterref")==0 ){ + sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i)); + verify_uninitialized(); + sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz); +#endif + }else if( cli_strcmp(z,"-vfs")==0 ){ + zVfs = cmdline_option_value(argc, argv, ++i); +#ifdef SQLITE_HAVE_ZLIB + }else if( cli_strcmp(z,"-zip")==0 ){ + data.openMode = SHELL_OPEN_ZIPFILE; +#endif + }else if( cli_strcmp(z,"-append")==0 ){ + data.openMode = SHELL_OPEN_APPENDVFS; +#ifndef SQLITE_OMIT_DESERIALIZE + }else if( cli_strcmp(z,"-deserialize")==0 ){ + data.openMode = SHELL_OPEN_DESERIALIZE; + }else if( cli_strcmp(z,"-maxsize")==0 && i+1<argc ){ + data.szMax = integerValue(argv[++i]); +#endif + }else if( cli_strcmp(z,"-readonly")==0 ){ + data.openFlags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); + data.openFlags |= SQLITE_OPEN_READONLY; + }else if( cli_strcmp(z,"-nofollow")==0 ){ + data.openFlags |= SQLITE_OPEN_NOFOLLOW; + }else if( cli_strcmp(z,"-exclusive")==0 ){ /* UNDOCUMENTED */ + data.openFlags |= SQLITE_OPEN_EXCLUSIVE; + }else if( cli_strcmp(z,"-ifexists")==0 ){ + data.openFlags &= ~(SQLITE_OPEN_CREATE); + if( data.openFlags==0 ) data.openFlags = SQLITE_OPEN_READWRITE; +#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) + }else if( cli_strncmp(z, "-A",2)==0 ){ + /* All remaining command-line arguments are passed to the ".archive" + ** command, so ignore them */ + break; +#endif + }else if( cli_strcmp(z, "-memtrace")==0 ){ + sqlite3MemTraceActivate(stderr); + }else if( cli_strcmp(z, "-pcachetrace")==0 ){ + sqlite3PcacheTraceActivate(stderr); + }else if( cli_strcmp(z,"-bail")==0 ){ + bail_on_error = 1; + }else if( cli_strcmp(z,"-nonce")==0 ){ + free(data.zNonce); + data.zNonce = strdup(cmdline_option_value(argc, argv, ++i)); + }else if( cli_strcmp(z,"-unsafe-testing")==0 ){ + ShellSetFlag(&data,SHFLG_TestingMode); + }else if( cli_strcmp(z,"-safe")==0 ){ + /* no-op - catch this on the second pass */ + }else if( cli_strcmp(z,"-escape")==0 && i+1<argc ){ + /* skip over the argument */ + i++; + } + } +#ifndef SQLITE_SHELL_FIDDLE + if( !bEnableVfstrace ) verify_uninitialized(); +#endif + + +#ifdef SQLITE_SHELL_INIT_PROC + { + /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name + ** of a C-function that will perform initialization actions on SQLite that + ** occur just before or after sqlite3_initialize(). Use this compile-time + ** option to embed this shell program in larger applications. */ + extern void SQLITE_SHELL_INIT_PROC(void); + SQLITE_SHELL_INIT_PROC(); + } +#else + /* All the sqlite3_config() calls have now been made. So it is safe + ** to call sqlite3_initialize() and process any command line -vfs option. */ + sqlite3_initialize(); +#endif + + if( zVfs ){ + sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs); + if( pVfs ){ + sqlite3_vfs_register(pVfs, 1); + }else{ + sqlite3_fprintf(stderr,"no such VFS: \"%s\"\n", zVfs); + exit(1); + } + } + + if( data.pAuxDb->zDbFilename==0 ){ +#ifndef SQLITE_OMIT_MEMORYDB + data.pAuxDb->zDbFilename = ":memory:"; + warnInmemoryDb = argc==1; +#else + sqlite3_fprintf(stderr, + "%s: Error: no database filename specified\n", Argv0); + return 1; +#endif + } + data.out = stdout; + if( bEnableVfstrace ){ + vfstrace_register("trace",0,vfstraceOut, &data, 1); + } +#ifndef SQLITE_SHELL_FIDDLE + sqlite3_appendvfs_init(0,0,0); +#endif + + /* Go ahead and open the database file if it already exists. If the + ** file does not exist, delay opening it. This prevents empty database + ** files from being created if a user mistypes the database name argument + ** to the sqlite command-line tool. + */ + if( access(data.pAuxDb->zDbFilename, 0)==0 ){ + open_db(&data, 0); + } + + /* Process the initialization file if there is one. If no -init option + ** is given on the command line, look for a file named ~/.sqliterc and + ** try to process it. + */ + process_sqliterc(&data,zInitFile); + + /* Make a second pass through the command-line argument and set + ** options. This second pass is delayed until after the initialization + ** file is processed so that the command-line arguments will override + ** settings in the initialization file. + */ + for(i=1; i<argc; i++){ + char *z = argv[i]; + if( z[0]!='-' || i>=nOptsEnd ) continue; + if( z[1]=='-' ){ z++; } + if( cli_strcmp(z,"-init")==0 ){ + i++; + }else if( cli_strcmp(z,"-html")==0 ){ + data.mode = MODE_Html; + }else if( cli_strcmp(z,"-list")==0 ){ + data.mode = MODE_List; + }else if( cli_strcmp(z,"-quote")==0 ){ + data.mode = MODE_Quote; + sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Comma); + sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row); + }else if( cli_strcmp(z,"-line")==0 ){ + data.mode = MODE_Line; + }else if( cli_strcmp(z,"-column")==0 ){ + data.mode = MODE_Column; + }else if( cli_strcmp(z,"-json")==0 ){ + data.mode = MODE_Json; + }else if( cli_strcmp(z,"-markdown")==0 ){ + data.mode = MODE_Markdown; + }else if( cli_strcmp(z,"-table")==0 ){ + data.mode = MODE_Table; + }else if( cli_strcmp(z,"-box")==0 ){ + data.mode = MODE_Box; + }else if( cli_strcmp(z,"-csv")==0 ){ + data.mode = MODE_Csv; + memcpy(data.colSeparator,",",2); + }else if( cli_strcmp(z,"-escape")==0 && i+1<argc ){ + /* See similar code at tag-20250224-1 */ + const char *zEsc = argv[++i]; + int k; + for(k=0; k<ArraySize(shell_EscModeNames); k++){ + if( sqlite3_stricmp(zEsc,shell_EscModeNames[k])==0 ){ + data.eEscMode = k; + break; + } + } + if( k>=ArraySize(shell_EscModeNames) ){ + sqlite3_fprintf(stderr, "unknown control character escape mode \"%s\"" + " - choices:", zEsc); + for(k=0; k<ArraySize(shell_EscModeNames); k++){ + sqlite3_fprintf(stderr, " %s", shell_EscModeNames[k]); + } + sqlite3_fprintf(stderr, "\n"); + exit(1); + } +#ifdef SQLITE_HAVE_ZLIB + }else if( cli_strcmp(z,"-zip")==0 ){ + data.openMode = SHELL_OPEN_ZIPFILE; +#endif + }else if( cli_strcmp(z,"-append")==0 ){ + data.openMode = SHELL_OPEN_APPENDVFS; +#ifndef SQLITE_OMIT_DESERIALIZE + }else if( cli_strcmp(z,"-deserialize")==0 ){ + data.openMode = SHELL_OPEN_DESERIALIZE; + }else if( cli_strcmp(z,"-maxsize")==0 && i+1<argc ){ + data.szMax = integerValue(argv[++i]); +#endif + }else if( cli_strcmp(z,"-readonly")==0 ){ + data.openFlags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); + data.openFlags |= SQLITE_OPEN_READONLY; + }else if( cli_strcmp(z,"-nofollow")==0 ){ + data.openFlags |= SQLITE_OPEN_NOFOLLOW; + }else if( cli_strcmp(z,"-exclusive")==0 ){ /* UNDOCUMENTED */ + data.openFlags |= SQLITE_OPEN_EXCLUSIVE; + }else if( cli_strcmp(z,"-ifexists")==0 ){ + data.openFlags &= ~(SQLITE_OPEN_CREATE); + if( data.openFlags==0 ) data.openFlags = SQLITE_OPEN_READWRITE; + }else if( cli_strcmp(z,"-ascii")==0 ){ + data.mode = MODE_Ascii; + sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,SEP_Unit); + sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,SEP_Record); + }else if( cli_strcmp(z,"-tabs")==0 ){ + data.mode = MODE_List; + sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,SEP_Tab); + sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,SEP_Row); + }else if( cli_strcmp(z,"-separator")==0 ){ + sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, + "%s",cmdline_option_value(argc,argv,++i)); + }else if( cli_strcmp(z,"-newline")==0 ){ + sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, + "%s",cmdline_option_value(argc,argv,++i)); + }else if( cli_strcmp(z,"-nullvalue")==0 ){ + sqlite3_snprintf(sizeof(data.nullValue), data.nullValue, + "%s",cmdline_option_value(argc,argv,++i)); + }else if( cli_strcmp(z,"-header")==0 ){ + data.showHeader = 1; + ShellSetFlag(&data, SHFLG_HeaderSet); + }else if( cli_strcmp(z,"-noheader")==0 ){ + data.showHeader = 0; + ShellSetFlag(&data, SHFLG_HeaderSet); + }else if( cli_strcmp(z,"-echo")==0 ){ + ShellSetFlag(&data, SHFLG_Echo); + }else if( cli_strcmp(z,"-eqp")==0 ){ + data.autoEQP = AUTOEQP_on; + }else if( cli_strcmp(z,"-eqpfull")==0 ){ + data.autoEQP = AUTOEQP_full; + }else if( cli_strcmp(z,"-stats")==0 ){ + data.statsOn = 1; + }else if( cli_strcmp(z,"-scanstats")==0 ){ + data.scanstatsOn = 1; + }else if( cli_strcmp(z,"-backslash")==0 ){ + /* Undocumented command-line option: -backslash + ** Causes C-style backslash escapes to be evaluated in SQL statements + ** prior to sending the SQL into SQLite. Useful for injecting + ** crazy bytes in the middle of SQL statements for testing and debugging. + */ + ShellSetFlag(&data, SHFLG_Backslash); + }else if( cli_strcmp(z,"-bail")==0 ){ + /* No-op. The bail_on_error flag should already be set. */ + }else if( cli_strcmp(z,"-version")==0 ){ +/* BEGIN SQLCIPHER */ +#ifdef SQLITE_HAS_CODEC + extern char* sqlcipher_version(); + char *sqlcipher_ver = sqlcipher_version(); + sqlite3_fprintf(stdout, "%s %s (%d-bit)", + sqlite3_libversion(), sqlite3_sourceid(), 8*(int)sizeof(char*)); + sqlite3_fprintf(stdout, " (SQLCipher %s)\n", sqlcipher_ver); + sqlite3_free(sqlcipher_ver); +#else + sqlite3_fprintf(stdout, "%s %s (%d-bit)\n", + sqlite3_libversion(), sqlite3_sourceid(), 8*(int)sizeof(char*)); +#endif +/* END SQLCIPHER */ + return 0; + }else if( cli_strcmp(z,"-interactive")==0 ){ + /* Need to check for interactive override here to so that it can + ** affect console setup (for Windows only) and testing thereof. + */ + stdin_is_interactive = 1; + }else if( cli_strcmp(z,"-batch")==0 ){ + /* already handled */ + }else if( cli_strcmp(z,"-utf8")==0 ){ + /* already handled */ + }else if( cli_strcmp(z,"-no-utf8")==0 ){ + /* already handled */ + }else if( cli_strcmp(z,"-no-rowid-in-view")==0 ){ + /* already handled */ + }else if( cli_strcmp(z,"-heap")==0 ){ + i++; + }else if( cli_strcmp(z,"-pagecache")==0 ){ + i+=2; + }else if( cli_strcmp(z,"-lookaside")==0 ){ + i+=2; + }else if( cli_strcmp(z,"-threadsafe")==0 ){ + i+=2; + }else if( cli_strcmp(z,"-nonce")==0 ){ + i += 2; + }else if( cli_strcmp(z,"-mmap")==0 ){ + i++; + }else if( cli_strcmp(z,"-memtrace")==0 ){ + i++; + }else if( cli_strcmp(z,"-pcachetrace")==0 ){ + i++; +#ifdef SQLITE_ENABLE_SORTER_REFERENCES + }else if( cli_strcmp(z,"-sorterref")==0 ){ + i++; +#endif + }else if( cli_strcmp(z,"-vfs")==0 ){ + i++; + }else if( cli_strcmp(z,"-vfstrace")==0 ){ + i++; +#ifdef SQLITE_ENABLE_MULTIPLEX + }else if( cli_strcmp(z,"-multiplex")==0 ){ + i++; +#endif + }else if( cli_strcmp(z,"-help")==0 ){ + usage(1); + }else if( cli_strcmp(z,"-cmd")==0 ){ + /* Run commands that follow -cmd first and separately from commands + ** that simply appear on the command-line. This seems goofy. It would + ** be better if all commands ran in the order that they appear. But + ** we retain the goofy behavior for historical compatibility. */ + if( i==argc-1 ) break; + z = cmdline_option_value(argc,argv,++i); + if( z[0]=='.' ){ + rc = do_meta_command(z, &data); + if( rc && bail_on_error ) return rc==2 ? 0 : rc; + }else{ + open_db(&data, 0); + rc = shell_exec(&data, z, &zErrMsg); + if( zErrMsg!=0 ){ + shellEmitError(zErrMsg); + sqlite3_free(zErrMsg); + if( bail_on_error ) return rc!=0 ? rc : 1; + }else if( rc!=0 ){ + sqlite3_fprintf(stderr,"Error: unable to process SQL \"%s\"\n", z); + if( bail_on_error ) return rc; + } + } +#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) + }else if( cli_strncmp(z, "-A", 2)==0 ){ + if( nCmd>0 ){ + sqlite3_fprintf(stderr,"Error: cannot mix regular SQL or dot-commands" + " with \"%s\"\n", z); + rc = 1; + goto shell_main_exit; + } + open_db(&data, OPEN_DB_ZIPFILE); + if( z[2] ){ + argv[i] = &z[2]; + arDotCommand(&data, 1, argv+(i-1), argc-(i-1)); + }else{ + arDotCommand(&data, 1, argv+i, argc-i); + } + readStdin = 0; + break; +#endif + }else if( cli_strcmp(z,"-safe")==0 ){ + data.bSafeMode = data.bSafeModePersist = 1; + }else if( cli_strcmp(z,"-unsafe-testing")==0 ){ + /* Acted upon in first pass. */ + }else{ + sqlite3_fprintf(stderr,"%s: Error: unknown option: %s\n", Argv0, z); + eputz("Use -help for a list of options.\n"); + return 1; + } + data.cMode = data.mode; + } + + if( !readStdin ){ + /* Run all arguments that do not begin with '-' as if they were separate + ** command-line inputs, except for the argToSkip argument which contains + ** the database filename. + */ + for(i=0; i<nCmd; i++){ + echo_group_input(&data, azCmd[i]); + if( azCmd[i][0]=='.' ){ + rc = do_meta_command(azCmd[i], &data); + if( rc ){ + if( rc==2 ) rc = 0; + goto shell_main_exit; + } + }else{ + open_db(&data, 0); + rc = shell_exec(&data, azCmd[i], &zErrMsg); + if( zErrMsg || rc ){ + if( zErrMsg!=0 ){ + shellEmitError(zErrMsg); + }else{ + sqlite3_fprintf(stderr, + "Error: unable to process SQL: %s\n", azCmd[i]); + } + sqlite3_free(zErrMsg); + if( rc==0 ) rc = 1; + goto shell_main_exit; + } + } + } + }else{ + /* Run commands received from standard input + */ + if( stdin_is_interactive ){ + char *zHome; + char *zHistory; +/* BEGIN SQLCIPHER */ +#ifdef SQLITE_HAS_CODEC + { + extern char* sqlcipher_version(); + char *sqlcipher_ver = sqlcipher_version(); + sqlite3_fprintf(stdout, + "SQLite version %s %.19s" /*extra-version-info*/ + " (SQLCipher %s)\n" /*sqlcipher version info*/ + "Enter \".help\" for usage hints.\n", + sqlite3_libversion(), sqlite3_sourceid(), sqlcipher_ver); + sqlite3_free(sqlcipher_ver); + } +#else + sqlite3_fprintf(stdout, + "SQLite version %s %.19s\n" /*extra-version-info*/ + "Enter \".help\" for usage hints.\n", + sqlite3_libversion(), sqlite3_sourceid()); +#endif +/* END SQLCIPHER */ + if( warnInmemoryDb ){ + sputz(stdout, "Connected to a "); + printBold("transient in-memory database"); + sputz(stdout, ".\nUse \".open FILENAME\" to reopen on a" + " persistent database.\n"); + } + zHistory = getenv("SQLITE_HISTORY"); + if( zHistory ){ + zHistory = sqlite3_mprintf("%s", zHistory); + shell_check_oom(zHistory); + }else{ + zHistory = find_xdg_file("XDG_STATE_HOME", + ".local/state", + "sqlite_history"); + if( 0==zHistory && (zHome = find_home_dir(0))!=0 ){ + zHistory = sqlite3_mprintf("%s/.sqlite_history", zHome); + shell_check_oom(zHistory); + } + } + if( zHistory ){ shell_read_history(zHistory); } +#if (HAVE_READLINE || HAVE_EDITLINE) && !defined(SQLITE_OMIT_READLINE_COMPLETION) + rl_attempted_completion_function = readline_completion; +#elif HAVE_LINENOISE==1 + linenoiseSetCompletionCallback(linenoise_completion); +#elif HAVE_LINENOISE==2 + linenoiseSetCompletionCallback(linenoise_completion, NULL); +#endif + data.in = 0; + rc = process_input(&data, "<stdin>"); + if( zHistory ){ + shell_stifle_history(2000); + shell_write_history(zHistory); + sqlite3_free(zHistory); + } + }else{ + data.in = stdin; + rc = process_input(&data, "<stdin>"); + } + } +#ifndef SQLITE_SHELL_FIDDLE + /* In WASM mode we have to leave the db state in place so that + ** client code can "push" SQL into it after this call returns. */ +#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_AUTHORIZATION) + if( data.expert.pExpert ){ + expertFinish(&data, 1, 0); + } +#endif + shell_main_exit: + free(azCmd); + set_table_name(&data, 0); + if( data.db ){ + session_close_all(&data, -1); + close_db(data.db); + } + for(i=0; i<ArraySize(data.aAuxDb); i++){ + sqlite3_free(data.aAuxDb[i].zFreeOnClose); + if( data.aAuxDb[i].db ){ + session_close_all(&data, i); + close_db(data.aAuxDb[i].db); + } + } + find_home_dir(1); + output_reset(&data); + data.doXdgOpen = 0; + clearTempFile(&data); +#if !SQLITE_SHELL_IS_UTF8 + for(i=0; i<argcToFree; i++) free(argvToFree[i]); + free(argvToFree); +#endif + free(data.colWidth); + free(data.zNonce); + /* Clear the global data structure so that valgrind will detect memory + ** leaks */ + memset(&data, 0, sizeof(data)); + if( bEnableVfstrace ){ + vfstrace_unregister("trace"); + } +#ifdef SQLITE_DEBUG + if( sqlite3_memory_used()>mem_main_enter ){ + sqlite3_fprintf(stderr,"Memory leaked: %u bytes\n", + (unsigned int)(sqlite3_memory_used()-mem_main_enter)); + } +#endif +#else /* SQLITE_SHELL_FIDDLE... */ + shell_main_exit: +#endif + return rc; +} + + +#ifdef SQLITE_SHELL_FIDDLE +/* Only for emcc experimentation purposes. */ +int fiddle_experiment(int a,int b){ + return a + b; +} + +/* +** Returns a pointer to the current DB handle. +*/ +sqlite3 * fiddle_db_handle(){ + return globalDb; +} + +/* +** Returns a pointer to the given DB name's VFS. If zDbName is 0 then +** "main" is assumed. Returns 0 if no db with the given name is +** open. +*/ +sqlite3_vfs * fiddle_db_vfs(const char *zDbName){ + sqlite3_vfs * pVfs = 0; + if(globalDb){ + sqlite3_file_control(globalDb, zDbName ? zDbName : "main", + SQLITE_FCNTL_VFS_POINTER, &pVfs); + } + return pVfs; +} + +/* Only for emcc experimentation purposes. */ +sqlite3 * fiddle_db_arg(sqlite3 *arg){ + sqlite3_fprintf(stdout, "fiddle_db_arg(%p)\n", (const void*)arg); + return arg; +} + +/* +** Intended to be called via a SharedWorker() while a separate +** SharedWorker() (which manages the wasm module) is performing work +** which should be interrupted. Unfortunately, SharedWorker is not +** portable enough to make real use of. +*/ +void fiddle_interrupt(void){ + if( globalDb ) sqlite3_interrupt(globalDb); +} + +/* +** Returns the filename of the given db name, assuming "main" if +** zDbName is NULL. Returns NULL if globalDb is not opened. +*/ +const char * fiddle_db_filename(const char * zDbName){ + return globalDb + ? sqlite3_db_filename(globalDb, zDbName ? zDbName : "main") + : NULL; +} + +/* +** Completely wipes out the contents of the currently-opened database +** but leaves its storage intact for reuse. If any transactions are +** active, they are forcibly rolled back. +*/ +void fiddle_reset_db(void){ + if( globalDb ){ + int rc; + while( sqlite3_txn_state(globalDb,0)>0 ){ + /* + ** Resolve problem reported in + ** https://sqlite.org/forum/forumpost/0b41a25d65 + */ + sqlite3_fputs("Rolling back in-progress transaction.\n", stdout); + sqlite3_exec(globalDb,"ROLLBACK", 0, 0, 0); + } + rc = sqlite3_db_config(globalDb, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0); + if( 0==rc ) sqlite3_exec(globalDb, "VACUUM", 0, 0, 0); + sqlite3_db_config(globalDb, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0); + } +} + +/* +** Uses the current database's VFS xRead to stream the db file's +** contents out to the given callback. The callback gets a single +** chunk of size n (its 2nd argument) on each call and must return 0 +** on success, non-0 on error. This function returns 0 on success, +** SQLITE_NOTFOUND if no db is open, or propagates any other non-0 +** code from the callback. Note that this is not thread-friendly: it +** expects that it will be the only thread reading the db file and +** takes no measures to ensure that is the case. +*/ +int fiddle_export_db( int (*xCallback)(unsigned const char *zOut, int n) ){ + sqlite3_int64 nSize = 0; + sqlite3_int64 nPos = 0; + sqlite3_file * pFile = 0; + unsigned char buf[1024 * 8]; + int nBuf = (int)sizeof(buf); + int rc = shellState.db + ? sqlite3_file_control(shellState.db, "main", + SQLITE_FCNTL_FILE_POINTER, &pFile) + : SQLITE_NOTFOUND; + if( rc ) return rc; + rc = pFile->pMethods->xFileSize(pFile, &nSize); + if( rc ) return rc; + if(nSize % nBuf){ + /* DB size is not an even multiple of the buffer size. Reduce + ** buffer size so that we do not unduly inflate the db size when + ** exporting. */ + if(0 == nSize % 4096) nBuf = 4096; + else if(0 == nSize % 2048) nBuf = 2048; + else if(0 == nSize % 1024) nBuf = 1024; + else nBuf = 512; + } + for( ; 0==rc && nPos<nSize; nPos += nBuf ){ + rc = pFile->pMethods->xRead(pFile, buf, nBuf, nPos); + if(SQLITE_IOERR_SHORT_READ == rc){ + rc = (nPos + nBuf) < nSize ? rc : 0/*assume EOF*/; + } + if( 0==rc ) rc = xCallback(buf, nBuf); + } + return rc; +} + +/* +** Trivial exportable function for emscripten. It processes zSql as if +** it were input to the sqlite3 shell and redirects all output to the +** wasm binding. fiddle_main() must have been called before this +** is called, or results are undefined. +*/ +void fiddle_exec(const char * zSql){ + if(zSql && *zSql){ + if('.'==*zSql) puts(zSql); + shellState.wasm.zInput = zSql; + shellState.wasm.zPos = zSql; + process_input(&shellState, "<stdin>"); + shellState.wasm.zInput = shellState.wasm.zPos = 0; + } +} +#endif /* SQLITE_SHELL_FIDDLE */ diff --git a/src/sqlcipher.c b/src/sqlcipher.c new file mode 100644 index 0000000000..889e0b7259 --- /dev/null +++ b/src/sqlcipher.c @@ -0,0 +1,3889 @@ +/* +** SQLCipher +** http://zetetic.net +** +** Copyright (c) 2008-2024, ZETETIC LLC +** All rights reserved. +** +** Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in the +** documentation and/or other materials provided with the distribution. +** * Neither the name of the ZETETIC LLC nor the +** names of its contributors may be used to endorse or promote products +** derived from this software without specific prior written permission. +** +** THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY +** EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +** DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY +** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +** +*/ +/* BEGIN SQLCIPHER */ +#ifdef SQLITE_HAS_CODEC + +#if !defined(SQLCIPHER_OMIT_LOG_DEVICE) +#if defined(__ANDROID__) +#include <android/log.h> +#elif defined(__APPLE__) +#include <TargetConditionals.h> +#include <os/log.h> +#endif +#endif + +#include <time.h> + +#if defined(_WIN32) || defined(SQLITE_OS_WINRT) +#include <windows.h> /* amalgamator: dontcache */ +#else +#include <sys/time.h> /* amalgamator: dontcache */ +#endif + +#ifndef OMIT_MEMLOCK +#if defined(__unix__) || defined(__APPLE__) || defined(_AIX) +#include <errno.h> /* amalgamator: dontcache */ +#include <unistd.h> /* amalgamator: dontcache */ +#include <sys/resource.h> /* amalgamator: dontcache */ +#include <sys/mman.h> /* amalgamator: dontcache */ +#endif +#endif + +#include <assert.h> +#include "sqlcipher.h" +#include "btreeInt.h" +#include "pager.h" +#include "vdbeInt.h" + +#if !defined(SQLITE_EXTRA_INIT) || !defined(SQLITE_EXTRA_SHUTDOWN) +#error "SQLCipher must be compiled with -DSQLITE_EXTRA_INIT=sqlcipher_extra_init -DSQLITE_EXTRA_SHUTDOWN=sqlcipher_extra_shutdown" +#endif + +#if !defined(SQLITE_THREADSAFE) || !(SQLITE_THREADSAFE == 1 || SQLITE_THREADSAFE == 2) +#error "SQLCipher must be compiled with -DSQLITE_THREADSAFE=<1 or 2>" +#endif + +#if !defined(SQLITE_TEMP_STORE) || SQLITE_TEMP_STORE == 0 || SQLITE_TEMP_STORE == 1 +#error "SQLCipher must be compiled with -DSQLITE_TEMP_STORE=<2 or 3>" +#endif + +/* extensions defined in pager.c */ +void *sqlcipherPagerGetCodec(Pager*); +void sqlcipherPagerSetCodec(Pager*, void *(*)(void*,void*,Pgno,int), void (*)(void*,int,int), void (*)(void*), void *); +int sqlite3pager_is_sj_pgno(Pager*, Pgno); +void sqlite3pager_error(Pager*, int); +void sqlite3pager_reset(Pager *pPager); +/* end extensions defined in pager.c */ + +#if !defined (SQLCIPHER_CRYPTO_CC) \ + && !defined (SQLCIPHER_CRYPTO_OPENSSL) \ + && !defined (SQLCIPHER_CRYPTO_CUSTOM) +#define SQLCIPHER_CRYPTO_OPENSSL +#endif + +#define FILE_HEADER_SZ 16 + +#define CIPHER_XSTR(s) CIPHER_STR(s) +#define CIPHER_STR(s) #s + +#ifndef CIPHER_VERSION_NUMBER +#define CIPHER_VERSION_NUMBER 4.13.0 +#endif + +#ifndef CIPHER_VERSION_BUILD +#define CIPHER_VERSION_BUILD community +#endif + +#define CIPHER_READ_CTX 0 +#define CIPHER_WRITE_CTX 1 +#define CIPHER_READWRITE_CTX 2 + +#ifndef PBKDF2_ITER +#define PBKDF2_ITER 256000 +#endif + +#define SQLCIPHER_FLAG_GET(FLAG,BIT) ((FLAG & BIT) != 0) +#define SQLCIPHER_FLAG_SET(FLAG,BIT) FLAG |= BIT +#define SQLCIPHER_FLAG_UNSET(FLAG,BIT) FLAG &= ~BIT + +/* possible flags for codec_ctx->flags */ +#define CIPHER_FLAG_HMAC (1 << 0) +#define CIPHER_FLAG_LE_PGNO (1 << 1) +#define CIPHER_FLAG_BE_PGNO (1 << 2) +#define CIPHER_FLAG_KEY_USED (1 << 3) +#define CIPHER_FLAG_HAS_KDF_SALT (1 << 4) + + +#ifndef DEFAULT_CIPHER_FLAGS +#define DEFAULT_CIPHER_FLAGS CIPHER_FLAG_HMAC | CIPHER_FLAG_LE_PGNO +#endif + + +/* by default, sqlcipher will use a reduced number of iterations to generate + the HMAC key / or transform a raw cipher key + */ +#ifndef FAST_PBKDF2_ITER +#define FAST_PBKDF2_ITER 2 +#endif + +/* this if a fixed random array that will be xor'd with the database salt to ensure that the + salt passed to the HMAC key derivation function is not the same as that used to derive + the encryption key. This can be overridden at compile time but it will make the resulting + binary incompatible with the default builds when using HMAC. A future version of SQLcipher + will likely allow this to be defined at runtime via pragma */ +#ifndef HMAC_SALT_MASK +#define HMAC_SALT_MASK 0x3a +#endif + +#ifndef CIPHER_MAX_IV_SZ +#define CIPHER_MAX_IV_SZ 16 +#endif + +#ifndef CIPHER_MAX_KEY_SZ +#define CIPHER_MAX_KEY_SZ 64 +#endif + + +/* the default implementation of SQLCipher uses a cipher_ctx + to keep track of read / write state separately. The following + struct and associated functions are defined here */ +typedef struct { + int derive_key; + int pass_sz; + unsigned char *key; + unsigned char *hmac_key; + unsigned char *pass; +} cipher_ctx; + + +typedef struct { + int store_pass; + int kdf_iter; + int fast_kdf_iter; + int kdf_salt_sz; + int key_sz; + int iv_sz; + int block_sz; + int page_sz; + int reserve_sz; + int hmac_sz; + int plaintext_header_sz; + int hmac_algorithm; + int kdf_algorithm; + int error; + unsigned int flags; + unsigned char *kdf_salt; + unsigned char *hmac_kdf_salt; + unsigned char *buffer; + Btree *pBt; + cipher_ctx *read_ctx; + cipher_ctx *write_ctx; + sqlcipher_provider *provider; + void *provider_ctx; +} codec_ctx ; + +typedef struct private_block private_block; +struct private_block { + private_block *next; + u32 size; + u32 is_used; +}; + +/* implementation of simple, fast PSRNG function using xoshiro256++ (XOR/shift/rotate) + * https://prng.di.unimi.it/ under the public domain via https://prng.di.unimi.it/xoshiro256plusplus.c + * xoshiro is NEVER used for any cryptographic functions as CSPRNG. It is solely used for + * generating random data for testing, debugging, and forensic purposes (overwriting memory segments) */ +static volatile uint64_t xoshiro_s[4]; + +static inline uint64_t xoshiro_rotl(const uint64_t x, int k) { + return (x << k) | (x >> (64 - k)); +} + +uint64_t xoshiro_next(void) { + volatile uint64_t result = xoshiro_rotl(xoshiro_s[0] + xoshiro_s[3], 23) + xoshiro_s[0]; + volatile uint64_t t = xoshiro_s[1] << 17; + + xoshiro_s[2] ^= xoshiro_s[0]; + xoshiro_s[3] ^= xoshiro_s[1]; + xoshiro_s[1] ^= xoshiro_s[2]; + xoshiro_s[0] ^= xoshiro_s[3]; + + xoshiro_s[2] ^= t; + + xoshiro_s[3] = xoshiro_rotl(xoshiro_s[3], 45); + + return result; +} + +static void xoshiro_randomness(unsigned char *ptr, int sz) { + volatile uint64_t val; + volatile int to_copy; + while (sz > 0) { + val = xoshiro_next(); + to_copy = (sz >= sizeof(val)) ? sizeof(val) : sz; + memcpy(ptr, (void *) &val, to_copy); + ptr += to_copy; + sz -= to_copy; + } +} + +#ifdef SQLCIPHER_TEST +/* possible flags for simulating specific test conditions */ +#define TEST_FAIL_ENCRYPT 0x01 +#define TEST_FAIL_DECRYPT 0x02 +#define TEST_FAIL_MIGRATE 0x04 + +static volatile unsigned int cipher_test_flags = 0; +static volatile int cipher_test_rand = 0; + +static int sqlcipher_get_test_fail() { + int x; + + /* if cipher_test_rand is not set to a non-zero value always fail (return true) */ + if (cipher_test_rand == 0) return 1; + + xoshiro_randomness((unsigned char *) &x, sizeof(x)); + return ((x % cipher_test_rand) == 0); +} +#endif + +static volatile unsigned int default_flags = DEFAULT_CIPHER_FLAGS; +static volatile unsigned char hmac_salt_mask = HMAC_SALT_MASK; +static volatile int default_kdf_iter = PBKDF2_ITER; +static volatile int default_page_size = 4096; +static volatile int default_plaintext_header_size = 0; +static volatile int default_hmac_algorithm = SQLCIPHER_HMAC_SHA512; +static volatile int default_kdf_algorithm = SQLCIPHER_PBKDF2_HMAC_SHA512; +static volatile int sqlcipher_mem_security_on = 0; +static volatile int sqlcipher_mem_executed = 0; +static volatile int sqlcipher_mem_initialized = 0; +static volatile sqlite3_mem_methods default_mem_methods; +static sqlcipher_provider *default_provider = NULL; + +static sqlite3_mutex* sqlcipher_static_mutex[SQLCIPHER_MUTEX_COUNT]; + +#ifndef SQLCIPHER_LOG_LEVEL_DEFAULT +#define SQLCIPHER_LOG_LEVEL_DEFAULT SQLCIPHER_LOG_WARN +#endif +static FILE* sqlcipher_log_file = NULL; +static volatile int sqlcipher_log_device = 0; +static volatile unsigned int sqlcipher_log_level = SQLCIPHER_LOG_NONE; +static volatile unsigned int sqlcipher_log_source = SQLCIPHER_LOG_ANY; +static volatile int sqlcipher_log_set = 0; + +static size_t sqlcipher_shield_mask_sz = 32; +static u8* sqlcipher_shield_mask = NULL; + +/* Establish the default size of the private heap. This can be overriden + * at compile time by setting -DSQLCIPHER_PRIVATE_HEAP_SIZE_DEFAULT=X */ +#ifndef SQLCIPHER_PRIVATE_HEAP_SIZE_DEFAULT +/* On android, the maximim amount of memory that can be memlocked in 64k. This also + * seems to be a popular ulimit on linux distributions, containsers, etc. Therefore + * the default heap size is chosen as 48K, which is either 4 (with 4k page size) + * or 1 (with 16k page size) page less than the max. We choose to allocate slightly + * less than the max just in case the app has locked some other page(s). This + * initial allocation should be enough to support at least 10 concurrent + * sqlcipher-enabled database connections at the same time without requiring any + * overflow allocations */ +#define SQLCIPHER_PRIVATE_HEAP_SIZE_DEFAULT 49152 +#endif +/* if default allocation fails, we'll reduce the size by this amount + * and try again. This is also the minimium of the private heap. The minimum + * size will be 4 4K pages or 1 16K page (possible with latest android)*/ +#define SQLCIPHER_PRIVATE_HEAP_SIZE_STEP 16384 + +static volatile size_t private_heap_sz = SQLCIPHER_PRIVATE_HEAP_SIZE_DEFAULT; +static u8* private_heap = NULL; +static volatile size_t private_heap_used = 0; /* bytes currently used on private heap */ +static volatile size_t private_heap_hwm = 0; /* larged number of bytes used on the private heap at one time */ +static volatile size_t private_heap_alloc = 0; /* total bytes allocated on private heap over time */ +static volatile u32 private_heap_allocs = 0; /* total number of allocations on private heap over time */ +static volatile size_t private_heap_overflow = 0; /* total bytes overflowing private heap over time */ +static volatile u32 private_heap_overflows = 0; /* number of overlow allocations over time */ + +/* to prevent excessive fragmentation blocks will + * only be split if there are at least this many + * bytes available after the split. This should allow for at + * least two addtional small allocations */ +#define SQLCIPHER_PRIVATE_HEAP_MIN_SPLIT_SIZE 32 + +/* requested sizes will be rounded up to the nearest 8 bytes for alignment */ +#define SQLCIPHER_PRIVATE_HEAP_ALIGNMENT 8 +#define SQLCIPHER_PRIVATE_HEAP_ROUNDUP(x) ((x % SQLCIPHER_PRIVATE_HEAP_ALIGNMENT) ? \ + ((x / SQLCIPHER_PRIVATE_HEAP_ALIGNMENT) + 1) * SQLCIPHER_PRIVATE_HEAP_ALIGNMENT : x) + +static volatile int sqlcipher_init = 0; +static volatile int sqlcipher_shutdown = 0; +static volatile int sqlcipher_cleanup = 0; +static int sqlcipher_init_error = SQLITE_ERROR; + +static void sqlcipher_internal_free(void *, sqlite_uint64); +static void *sqlcipher_internal_malloc(sqlite_uint64); + + +/* +** Simple shared routines for converting hex char strings to binary data + */ +static int cipher_hex2int(char c) { + return (c>='0' && c<='9') ? (c)-'0' : + (c>='A' && c<='F') ? (c)-'A'+10 : + (c>='a' && c<='f') ? (c)-'a'+10 : 0; +} + +static void cipher_hex2bin(const unsigned char *hex, int sz, unsigned char *out){ + int i; + for(i = 0; i < sz; i += 2){ + out[i/2] = (cipher_hex2int(hex[i])<<4) | cipher_hex2int(hex[i+1]); + } +} + +static void cipher_bin2hex(const unsigned char* in, int sz, char *out) { + int i; + for(i=0; i < sz; i++) { + sqlite3_snprintf(3, out + (i*2), "%02x ", in[i]); + } +} + +static int cipher_isHex(const unsigned char *hex, int sz){ + int i; + for(i = 0; i < sz; i++) { + unsigned char c = hex[i]; + if ((c < '0' || c > '9') && + (c < 'A' || c > 'F') && + (c < 'a' || c > 'f')) { + return 0; + } + } + return 1; +} + +sqlite3_mutex* sqlcipher_mutex(int mutex) { + if(mutex < 0 || mutex >= SQLCIPHER_MUTEX_COUNT) return NULL; + return sqlcipher_static_mutex[mutex]; +} + +static void sqlcipher_atexit(void) { + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "%s: calling sqlcipher_extra_shutdown()", __func__); + sqlcipher_extra_shutdown(); +} + +static void sqlcipher_fini(void) { + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "%s: calling sqlcipher_extra_shutdown()", __func__); + sqlcipher_extra_shutdown(); +} + +#if defined(_WIN32) + #ifndef SQLCIPHER_OMIT_DLLMAIN + BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) { + switch (fdwReason) { + case DLL_PROCESS_DETACH: + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "%s: calling sqlcipher_extra_shutdown()", __func__); + sqlcipher_extra_shutdown(); + break; + default: + break; + } + return TRUE; + } + #endif +#elif defined(__APPLE__) + #if defined(__has_feature) + #if __has_feature(address_sanitizer) + static void sqlcipher_cleanup_destructor(void) __attribute__((destructor)); + static void sqlcipher_cleanup_destructor(void) { sqlcipher_fini(); } + #else + static void (*const sqlcipher_fini_func)(void) __attribute__((used, section("__DATA,__mod_term_func"))) = sqlcipher_fini; + #endif + #else + static void (*const sqlcipher_fini_func)(void) __attribute__((used, section("__DATA,__mod_term_func"))) = sqlcipher_fini; + #endif +#else +static void (*const sqlcipher_fini_func)(void) __attribute__((used, section(".fini_array"))) = sqlcipher_fini; +#endif + +static void sqlcipher_exportFunc(sqlite3_context*, int, sqlite3_value**); + +static int sqlcipher_export_init(sqlite3* db, const char** errmsg, const struct sqlite3_api_routines* api) { + sqlite3_create_function_v2(db, "sqlcipher_export", -1, SQLITE_UTF8, 0, sqlcipher_exportFunc, 0, 0, 0); + return SQLITE_OK; +} + +/* The extra_init function is called by sqlite3_init automaticay by virtue of + * being defined with SQLITE_EXTRA_INIT. This function sets up + * static mutexes used internally by SQLCipher and initializes + * the internal private heap */ +int sqlcipher_extra_init(const char* arg) { + int rc = SQLITE_OK, i=0; + void* provider_ctx = NULL; + + sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER)); + + if(sqlcipher_init) { + /* if this init routine already completed successfully return immediately */ + sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER)); + return SQLITE_OK; + } + + /* only register cleanup handlers once per process */ + if(!sqlcipher_cleanup) { + atexit(sqlcipher_atexit); + sqlcipher_cleanup = 1; + } + +#ifndef SQLCIPHER_OMIT_DEFAULT_LOGGING + /* when sqlcipher is first activated, set a default log target and level of WARN if the + logging settings have not yet been initialized. Use the "device log" for + android (logcat) or apple (console). Use stderr on all other platforms. */ + if(!sqlcipher_log_set) { + + /* set log level if it is different than the uninitalized default value of NONE */ + if(sqlcipher_log_level == SQLCIPHER_LOG_NONE) { + sqlcipher_log_level = SQLCIPHER_LOG_LEVEL_DEFAULT; + } + + /* set the default file or device if neither is already set */ + if(sqlcipher_log_device == 0 && sqlcipher_log_file == NULL) { +#if defined(__ANDROID__) || defined(__APPLE__) + sqlcipher_log_device = 1; +#else + sqlcipher_log_file = stderr; +#endif + } + sqlcipher_log_set = 1; + } +#endif + + /* allocate static mutexe, and return error if any fail to allocate */ + for(i = 0; i < SQLCIPHER_MUTEX_COUNT; i++) { + if(sqlcipher_static_mutex[i] == NULL) { + if((sqlcipher_static_mutex[i] = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST)) == NULL) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_MEMORY, "%s: failed to allocate static mutex %d", __func__, i); + rc = SQLITE_NOMEM; + goto error; + } + } + } + + /* initialize the private heap for use in internal SQLCipher memory allocations */ + if(private_heap == NULL) { + while(private_heap_sz >= SQLCIPHER_PRIVATE_HEAP_SIZE_STEP) { + /* attempt to allocate the private heap. If allocation fails, reduce the size and try again */ + if((private_heap = sqlcipher_internal_malloc(private_heap_sz))) { + xoshiro_randomness(private_heap, (int) private_heap_sz); + /* initialize the head block of the linked list at the start of the heap */ + private_block *head = (private_block *) private_heap; + head->is_used = 0; + head->size = (u32) private_heap_sz - sizeof(private_block); + head->next = NULL; + break; + } + + /* allocation failed, reduce the requested size of the heap */ + private_heap_sz -= SQLCIPHER_PRIVATE_HEAP_SIZE_STEP; + } + } + if(!private_heap) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_MEMORY, "%s: failed to allocate private heap", __func__); + rc = SQLITE_NOMEM; + goto error; + } + + /* check to see if there is a provider registered at this point + if there no provider registered at this point, register the + default provider */ + if(sqlcipher_get_provider() == NULL) { + sqlcipher_provider *p = sqlcipher_malloc(sizeof(sqlcipher_provider)); +#if defined (SQLCIPHER_CRYPTO_CC) + extern int sqlcipher_cc_setup(sqlcipher_provider *p); + sqlcipher_cc_setup(p); +#elif defined (SQLCIPHER_CRYPTO_OPENSSL) + extern int sqlcipher_openssl_setup(sqlcipher_provider *p); + sqlcipher_openssl_setup(p); +#elif defined (SQLCIPHER_CRYPTO_OSSL3) + extern int sqlcipher_ossl3_setup(sqlcipher_provider *p); + sqlcipher_ossl3_setup(p); +#elif defined (SQLCIPHER_CRYPTO_CUSTOM) + extern int SQLCIPHER_CRYPTO_CUSTOM(sqlcipher_provider *p); + SQLCIPHER_CRYPTO_CUSTOM(p); +#else +#error "NO DEFAULT SQLCIPHER CRYPTO PROVIDER DEFINED" +#endif + if((rc = sqlcipher_register_provider(p)) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER, "%s: failed to register provider %p %d", __func__, p, rc); + goto error; + } + } + + /* required random data */ + if((rc = default_provider->ctx_init(&provider_ctx)) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_MEMORY, "%s: failed to initilize provider context %d", __func__, rc); + goto error; + } + + if((rc = default_provider->random(provider_ctx, (void *)xoshiro_s, sizeof(xoshiro_s))) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_MEMORY, "%s: failed to generate xoshiro seed %d", __func__, rc); + goto error; + } + + if(!sqlcipher_shield_mask) { + if(!(sqlcipher_shield_mask = sqlcipher_internal_malloc(sqlcipher_shield_mask_sz))) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_MEMORY, "%s: failed to allocate shield mask", __func__); + goto error; + } + if((rc = default_provider->random(provider_ctx, sqlcipher_shield_mask, (int) sqlcipher_shield_mask_sz)) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_MEMORY, "%s: failed to generate requisite random mask data %d", __func__, rc); + goto error; + } + } + + default_provider->ctx_free(&provider_ctx); + + sqlcipher_init = 1; + sqlcipher_shutdown = 0; + + /* leave the master mutex so we can proceed with auto extension registration */ + sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER)); + + /* finally, extension registration occurs outside of the mutex because it is + * uses SQLITE_MUTEX_STATIC_MASTER itself */ + sqlite3_auto_extension((void (*)(void))sqlcipher_export_init); + + return SQLITE_OK; + +error: + /* if an error occurs during initialization, tear down everything that was setup */ + if(private_heap) { + sqlcipher_internal_free(private_heap, private_heap_sz); + private_heap = NULL; + } + if(sqlcipher_shield_mask) { + sqlcipher_internal_free(sqlcipher_shield_mask, sqlcipher_shield_mask_sz); + sqlcipher_shield_mask = NULL; + } + for(i = 0; i < SQLCIPHER_MUTEX_COUNT; i++) { + if(sqlcipher_static_mutex[i]) { + sqlite3_mutex_free(sqlcipher_static_mutex[i]); + sqlcipher_static_mutex[i] = NULL; + } + } + + /* post cleanup return the error code back up to sqlite3_init() */ + sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER)); + sqlcipher_init_error = rc; + return rc; +} + +/* The extra_shutdown function is called by sqlite3_shutdown() + * because it is defined with SQLITE_EXTRA_SHUTDOWN. In addition it will + * be called via atexit(), finalizer, and DllMain. The function will + * cleanup resources allocated by SQLCipher including mutexes, + * the private heap, and default provider. */ +void sqlcipher_extra_shutdown(void) { + int i = 0; + sqlcipher_provider *provider = NULL; + + /* if sqlcipher hasn't been initialized or the shutdown already completed exit early */ + if(!sqlcipher_init || sqlcipher_shutdown) { + goto cleanup; + } + + if(sqlcipher_shield_mask) { + sqlcipher_internal_free(sqlcipher_shield_mask, sqlcipher_shield_mask_sz); + sqlcipher_shield_mask = NULL; + } + + /* free the provider list. start at the default provider and move through the list + * freeing each one. If a provider has a shutdown function, call it before freeing. + * finally NULL out the default_provider */ + provider = default_provider; + while(provider) { + sqlcipher_provider *next = provider->next; + if(provider->shutdown) { + provider->shutdown(); + } + sqlcipher_free(provider, sizeof(sqlcipher_provider)); + provider = next; + } + default_provider = NULL; + + /* free private heap. If SQLCipher is compiled in test mode, it will deliberately + not free the heap (leaking it) if the heap is not empty. This will allow tooling + to detect memory issues like unfreed private heap memory */ + if(private_heap) { +#ifdef SQLCIPHER_TEST + size_t used = 0; + private_block *block = NULL; + block = (private_block *) private_heap; + while (block != NULL) { + if(block->is_used) { + used+= block->size; + i++; + } + block = block->next; + } + if(used > 0) { + /* don't free the heap so that sqlite treats this as unfreed memory */ + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_MEMORY, + "%s: SQLCipher private heap unfreed memory %u bytes in %d allocations", __func__, used, i); + } else { + sqlcipher_internal_free(private_heap, private_heap_sz); + private_heap = NULL; + } +#else + sqlcipher_internal_free(private_heap, private_heap_sz); + private_heap = NULL; +#endif + sqlcipher_log(SQLCIPHER_LOG_INFO, SQLCIPHER_LOG_MEMORY, + "%s: SQLCipher private heap stats: size=%u, hwm=%u, alloc=%u, allocs=%u, overflow=%u, overflows=%u", __func__, + private_heap_sz, private_heap_hwm, private_heap_alloc, private_heap_allocs, private_heap_overflow, private_heap_overflows + ); + } + + /* free all of sqlcipher's static mutexes */ + for(i = 0; i < SQLCIPHER_MUTEX_COUNT; i++) { + if(sqlcipher_static_mutex[i]) { + sqlite3_mutex_free(sqlcipher_static_mutex[i]); + sqlcipher_static_mutex[i] = NULL; + } + } + +cleanup: + sqlcipher_init = 0; + sqlcipher_init_error = SQLITE_ERROR; + sqlcipher_shutdown = 1; +} + +static void sqlcipher_shield(unsigned char *in, int sz) { + int i = 0; + for(i = 0; i < sz; i++) { + in[i] ^= sqlcipher_shield_mask[i % sqlcipher_shield_mask_sz]; + } +} + +/* constant time memset using volitile to avoid having the memset + optimized out by the compiler. + Note: As suggested by Joachim Schipper (joachim.schipper@fox-it.com) +*/ +void* sqlcipher_memset(void *v, unsigned char value, sqlite_uint64 len) { + volatile sqlite_uint64 i = 0; + volatile unsigned char *a = v; + + if (v == NULL) return v; + + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MEMORY, "sqlcipher_memset: setting %p[0-%u]=%d)", a, len, value); + for(i = 0; i < len; i++) { + a[i] = value; + } + + return v; +} + +/* constant time memory check tests every position of a memory segement + matches a single value (i.e. the memory is all zeros) + returns 0 if match, 1 of no match */ +int sqlcipher_ismemset(const void *v, unsigned char value, sqlite_uint64 len) { + const volatile unsigned char *a = v; + volatile sqlite_uint64 i = 0, result = 0; + + for(i = 0; i < len; i++) { + result |= a[i] ^ value; + } + + return (result != 0); +} + +/* constant time memory comparison routine. + returns 0 if match, 1 if no match */ +int sqlcipher_memcmp(const void *v0, const void *v1, int len) { + const volatile unsigned char *a0 = v0, *a1 = v1; + volatile int i = 0, result = 0; + + for(i = 0; i < len; i++) { + result |= a0[i] ^ a1[i]; + } + + return (result != 0); +} + +static void sqlcipher_mlock(void *ptr, sqlite_uint64 sz) { +#ifndef OMIT_MEMLOCK +#if defined(__unix__) || defined(__APPLE__) + int rc; + unsigned long pagesize = sysconf(_SC_PAGESIZE); + unsigned long offset = (unsigned long) ptr % pagesize; + + if(ptr == NULL || sz == 0) return; + + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MEMORY, "sqlcipher_mlock: calling mlock(%p,%lu); _SC_PAGESIZE=%lu", ptr - offset, sz + offset, pagesize); + rc = mlock(ptr - offset, sz + offset); + if(rc!=0) { + sqlcipher_log(SQLCIPHER_LOG_WARN, SQLCIPHER_LOG_MEMORY, "sqlcipher_mlock: mlock() returned %d errno=%d", rc, errno); + sqlcipher_log(SQLCIPHER_LOG_INFO, SQLCIPHER_LOG_MEMORY, "sqlcipher_mlock: mlock(%p,%lu) returned %d errno=%d", ptr - offset, sz + offset, rc, errno); + } +#elif defined(_WIN32) +#if !(defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP || WINAPI_FAMILY == WINAPI_FAMILY_PC_APP)) + int rc; + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MEMORY, "sqlcipher_mlock: calling VirtualLock(%p,%d)", ptr, sz); + rc = VirtualLock(ptr, sz); + if(rc==0) { + sqlcipher_log(SQLCIPHER_LOG_WARN, SQLCIPHER_LOG_MEMORY, "sqlcipher_mlock: VirtualLock() returned %d LastError=%d", rc, GetLastError()); + sqlcipher_log(SQLCIPHER_LOG_INFO, SQLCIPHER_LOG_MEMORY, "sqlcipher_mlock: VirtualLock(%p,%d) returned %d LastError=%d", ptr, sz, rc, GetLastError()); + } +#endif +#endif +#endif +} + +static void sqlcipher_munlock(void *ptr, sqlite_uint64 sz) { +#ifndef OMIT_MEMLOCK +#if defined(__unix__) || defined(__APPLE__) + int rc; + unsigned long pagesize = sysconf(_SC_PAGESIZE); + unsigned long offset = (unsigned long) ptr % pagesize; + + if(ptr == NULL || sz == 0) return; + + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MEMORY, "sqlcipher_munlock: calling munlock(%p,%lu)", ptr - offset, sz + offset); + rc = munlock(ptr - offset, sz + offset); + if(rc!=0) { + sqlcipher_log(SQLCIPHER_LOG_INFO, SQLCIPHER_LOG_MEMORY, "sqlcipher_munlock: munlock(%p,%lu) returned %d errno=%d", ptr - offset, sz + offset, rc, errno); + } +#elif defined(_WIN32) +#if !(defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP || WINAPI_FAMILY == WINAPI_FAMILY_PC_APP)) + int rc; + + if(ptr == NULL || sz == 0) return; + + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MEMORY, "sqlcipher_munlock: calling VirtualUnlock(%p,%d)", ptr, sz); + rc = VirtualUnlock(ptr, sz); + + /* because memory allocations may be made from the same individual page, it is possible for VirtualUnlock to be called + * multiple times for the same page. Subsequent calls will return an error, but this can be safely ignored (i.e. because + * the previous call for that page unlocked the memory already). Log an info level event only in that case. */ + if(!rc) { + sqlcipher_log(SQLCIPHER_LOG_INFO, SQLCIPHER_LOG_MEMORY, "sqlcipher_munlock: VirtualUnlock(%p,%d) returned %d LastError=%d", ptr, sz, rc, GetLastError()); + } +#endif +#endif +#endif +} + +/** sqlcipher wraps the default memory subsystem so it can optionally provide the + * memory security feature which will lock and sanitize ALL memory used by + * the sqlite library internally. Memory security feature is disabled by default + * but but the wrapper is used regardless, it just forwards to the default + * memory management implementation when disabled + */ +static int sqlcipher_mem_init(void *pAppData) { + return default_mem_methods.xInit(pAppData); +} +static void sqlcipher_mem_shutdown(void *pAppData) { + default_mem_methods.xShutdown(pAppData); +} +static void *sqlcipher_mem_malloc(int n) { + void *ptr = default_mem_methods.xMalloc(n); + if(!sqlcipher_mem_executed) sqlcipher_mem_executed = 1; + if(sqlcipher_mem_security_on) { + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MEMORY, "sqlcipher_mem_malloc: calling sqlcipher_mlock(%p,%d)", ptr, n); + sqlcipher_mlock(ptr, n); + } + return ptr; +} +static int sqlcipher_mem_size(void *p) { + return default_mem_methods.xSize(p); +} +static void sqlcipher_mem_free(void *p) { + int sz; + if(!sqlcipher_mem_executed) sqlcipher_mem_executed = 1; + if(sqlcipher_mem_security_on) { + sz = sqlcipher_mem_size(p); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MEMORY, "%s: calling xoshiro_randomness(%p,%d) and sqlcipher_munlock(%p, %d)", __func__, p, sz, p, sz); + xoshiro_randomness(p, sz); + sqlcipher_munlock(p, sz); + } + default_mem_methods.xFree(p); +} +static void *sqlcipher_mem_realloc(void *p, int n) { + void *new = NULL; + int orig_sz = 0; + if(sqlcipher_mem_security_on) { + orig_sz = sqlcipher_mem_size(p); + if (n==0) { + sqlcipher_mem_free(p); + return NULL; + } else if (!p) { + return sqlcipher_mem_malloc(n); + } else if(n <= orig_sz) { + return p; + } else { + new = sqlcipher_mem_malloc(n); + if(new) { + memcpy(new, p, orig_sz); + sqlcipher_mem_free(p); + } + return new; + } + } else { + return default_mem_methods.xRealloc(p, n); + } +} + +static int sqlcipher_mem_roundup(int n) { + return default_mem_methods.xRoundup(n); +} + +static sqlite3_mem_methods sqlcipher_mem_methods = { + sqlcipher_mem_malloc, + sqlcipher_mem_free, + sqlcipher_mem_realloc, + sqlcipher_mem_size, + sqlcipher_mem_roundup, + sqlcipher_mem_init, + sqlcipher_mem_shutdown, + 0 +}; + +void sqlcipher_init_memmethods() { + if(sqlcipher_mem_initialized) return; + if(sqlite3_config(SQLITE_CONFIG_GETMALLOC, &default_mem_methods) != SQLITE_OK || + sqlite3_config(SQLITE_CONFIG_MALLOC, &sqlcipher_mem_methods) != SQLITE_OK) { + sqlcipher_mem_security_on = sqlcipher_mem_executed = sqlcipher_mem_initialized = 0; + } else { + sqlcipher_mem_initialized = 1; + } +} + +/** + * Free and wipe memory. Uses SQLites internal sqlite3_free so that memory + * can be countend and memory leak detection works in the test suite. + * If ptr is not null memory will be freed. + * If sz is greater than zero, the memory will be overwritten with zero before it is freed + * If sz is > 0, and not compiled with OMIT_MEMLOCK, system will attempt to unlock the + * memory segment so it can be paged + */ +static void sqlcipher_internal_free(void *ptr, sqlite_uint64 sz) { + xoshiro_randomness(ptr, sz); + sqlcipher_munlock(ptr, sz); + sqlite3_free(ptr); +} + +/** + * allocate memory. Uses sqlite's internall malloc wrapper so memory can be + * reference counted and leak detection works. Unless compiled with OMIT_MEMLOCK + * attempts to lock the memory pages so sensitive information won't be swapped + */ +static void* sqlcipher_internal_malloc(sqlite_uint64 sz) { + void *ptr; + ptr = sqlite3_malloc(sz); + sqlcipher_memset(ptr, 0, sz); + sqlcipher_mlock(ptr, sz); + return ptr; +} + +void *sqlcipher_malloc(sqlite3_uint64 size) { + void *alloc = NULL; + private_block *block = NULL, *split = NULL; + + if(size < 1) return NULL; + + size = SQLCIPHER_PRIVATE_HEAP_ROUNDUP(size); + + block = (private_block *) private_heap; + + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: entering SQLCIPHER_MUTEX_MEM", __func__); + sqlite3_mutex_enter(sqlcipher_mutex(SQLCIPHER_MUTEX_MEM)); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: entered SQLCIPHER_MUTEX_MEM", __func__); + + /* iterate through the blocks in the heap to find one which is big enough to hold + the requested allocation. Stop when one is found. */ + while(block != NULL && alloc == NULL) { + if(!block->is_used && block->size >= size) { + /* mark the block as in use and set the return pointer to the start + of the block free space */ + block->is_used = 1; + alloc = ((u8*)block) + sizeof(private_block); + sqlcipher_memset(alloc, 0, size); + + /* if there is at least the minimim amount of required space left after allocation, + split off a new free block and insert it after the in-use block */ + if(block->size >= size + sizeof(private_block) + SQLCIPHER_PRIVATE_HEAP_MIN_SPLIT_SIZE) { + split = (private_block*) (((u8*) block) + size + sizeof(private_block)); + split->is_used = 0; + split->size = block->size - size - sizeof(private_block); + + /* insert inbetween current block and next */ + split->next = block->next; + block->next = split; + + /* only set the size of the current block to the requested amount + if the block was split. otherwise, size will be the full amount + of the block, which will actually be larger than the requested amount */ + block->size = size; + } + } + block = block->next; + } + + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: leaving SQLCIPHER_MUTEX_MEM", __func__); + sqlite3_mutex_leave(sqlcipher_mutex(SQLCIPHER_MUTEX_MEM)); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: left SQLCIPHER_MUTEX_MEM", __func__); + + /* If we were unable to locate a free block large enough to service the request, the fallback + behavior will simply attempt to allocate additional memory using malloc. */ + if(alloc == NULL) { + private_heap_overflow += size; + private_heap_overflows++; + alloc = sqlcipher_internal_malloc(size); + sqlcipher_log(SQLCIPHER_LOG_INFO, SQLCIPHER_LOG_MEMORY, "%s: unable to allocate %u bytes on private heap, allocated %p using sqlcipher_internal_malloc fallback", __func__, size, alloc); + } else { + private_heap_used += size; + if(private_heap_used > private_heap_hwm) { + /* if the current bytes allocated on the private heap are greater than the high water mark, set the HWM to the new amount */ + private_heap_hwm = private_heap_used; + } + private_heap_alloc += size; + private_heap_allocs++; + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MEMORY, "%s allocated %u bytes on private heap at %p", __func__, size, alloc); + } + + return alloc; +} + +void sqlcipher_free(void *mem, sqlite3_uint64 sz) { + private_block *block = NULL, *prev = NULL; + void *alloc = NULL; + u32 block_size = 0; + block = (private_block *) private_heap; + + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: entering SQLCIPHER_MUTEX_MEM", __func__); + sqlite3_mutex_enter(sqlcipher_mutex(SQLCIPHER_MUTEX_MEM)); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: entered SQLCIPHER_MUTEX_MEM", __func__); + + /* search the heap for the block that contains this address */ + while(block != NULL) { + alloc = ((u8*)block)+sizeof(private_block); + /* if the memory address to be freed corresponds to this block's + allocation, mark it as unused. If they don't match, move + on to the next block */ + if(mem == alloc) { + block->is_used = 0; + block_size = block->size; /* retain the acual size of the block in use for stats adjustment */ + xoshiro_randomness(alloc, block->size); + + /* check whether the previous block is free, if so merge*/ + if(prev && !prev->is_used) { + prev->size = prev->size + sizeof(private_block) + block->size; + prev->next = block->next; + block = prev; + } + + /* check to see whether the next block is free, if so merge */ + if(block->next && !block->next->is_used) { + block->size = block->size + sizeof(private_block) + block->next->size; + block->next = block->next->next; + } + + /* once the block has been identified, marked free, and optionally + consolidated with it's neighbors, exit the loop, but leave + the block pointer intact so we know we found it in the heap */ + break; + } + + prev = block; + block = block->next; + } + + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: leaving SQLCIPHER_MUTEX_MEM", __func__); + sqlite3_mutex_leave(sqlcipher_mutex(SQLCIPHER_MUTEX_MEM)); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: left SQLCIPHER_MUTEX_MEM", __func__); + + /* If the memory address couldn't be found in the private heap + then it was allocated by the fallback mechanism and should + be deallocated with free() */ + if(!block) { + sqlcipher_log(SQLCIPHER_LOG_INFO, SQLCIPHER_LOG_MEMORY, "%s: unable to find %p with %u bytes on private heap, calling sqlcipher_internal_free fallback", __func__, mem, sz); + sqlcipher_internal_free(mem, sz); + } else { + private_heap_used -= block_size; + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MEMORY, "%s freed %u bytes (%u total) on private heap at %p", __func__, sz, block_size, mem); + } +} + +int sqlcipher_register_provider(sqlcipher_provider *p) { + int preexisting = 0, rc = SQLITE_OK; + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: entering SQLCIPHER_MUTEX_PROVIDER", __func__); + sqlite3_mutex_enter(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER)); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: entered SQLCIPHER_MUTEX_PROVIDER", __func__); + + if(!p || default_provider == p) { + goto cleanup; + } + + if(!default_provider) { + /* initial provider registration, NULL out previous pointer*/ + p->next = NULL; + } else { + /* one or more previous provider has already been registered, search through + * the list to see if the new provider has already been registered and handle + * appropriately */ + sqlcipher_provider *previous = default_provider; + sqlcipher_provider *current = default_provider->next; + while(current) { + if(current == p) { + /* this is a duplicate provider registration, and the provider in question + * already exists on the list. In that case, pop it out so it can be moved up to default. + * note that if we found an existing match we should avoid re-initializing the provider */ + previous->next = current->next; + preexisting = 1; + break; + } + previous = current; + current = current->next; + } + /* the current default_provider gets tacked on the list */ + p->next = default_provider; + } + + /* the new provider is elevated to default. if the provider was not preexisting and it has an initializer, call it */ + if(!preexisting && p->init) { + rc = p->init(); + } + + if(rc == SQLITE_OK) { + default_provider = p; + } +cleanup: + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: leaving SQLCIPHER_MUTEX_PROVIDER", __func__); + sqlite3_mutex_leave(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER)); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: left SQLCIPHER_MUTEX_PROVIDER", __func__); + + return rc; +} + +/* return a pointer to the currently registered provider. This will + allow an application to fetch the current registered provider and + make minor changes to it */ +sqlcipher_provider* sqlcipher_get_provider() { + return default_provider; +} + +char* sqlcipher_version(void) { +#ifdef CIPHER_VERSION_QUALIFIER + char *version = sqlite3_mprintf("%s %s %s", CIPHER_XSTR(CIPHER_VERSION_NUMBER), CIPHER_XSTR(CIPHER_VERSION_QUALIFIER), CIPHER_XSTR(CIPHER_VERSION_BUILD)); +#else + char *version = sqlite3_mprintf("%s %s", CIPHER_XSTR(CIPHER_VERSION_NUMBER), CIPHER_XSTR(CIPHER_VERSION_BUILD)); +#endif + return version; +} + +/** + * Initialize new cipher_ctx struct. This function will allocate memory + * for the cipher context and for the key + * + * returns SQLITE_OK if initialization was successful + * returns SQLITE_NOMEM if an error occured allocating memory + */ +static int sqlcipher_cipher_ctx_init(codec_ctx *ctx, cipher_ctx **iCtx) { + cipher_ctx *c_ctx; + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_MEMORY, "sqlcipher_cipher_ctx_init: allocating context"); + *iCtx = (cipher_ctx *) sqlcipher_malloc(sizeof(cipher_ctx)); + c_ctx = *iCtx; + if(c_ctx == NULL) return SQLITE_NOMEM; + + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_MEMORY, "sqlcipher_cipher_ctx_init: allocating key"); + c_ctx->key = (unsigned char *) sqlcipher_malloc(ctx->key_sz); + + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_MEMORY, "sqlcipher_cipher_ctx_init: allocating hmac_key"); + c_ctx->hmac_key = (unsigned char *) sqlcipher_malloc(ctx->key_sz); + + if(!c_ctx->key || !c_ctx->hmac_key) return SQLITE_NOMEM; + + return SQLITE_OK; +} + +/** + * Free and wipe memory associated with a cipher_ctx + */ +static void sqlcipher_cipher_ctx_free(codec_ctx* ctx, cipher_ctx **iCtx) { + cipher_ctx *c_ctx = *iCtx; + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_MEMORY, "cipher_ctx_free: iCtx=%p", iCtx); + if(c_ctx->key) sqlcipher_free(c_ctx->key, ctx->key_sz); + if(c_ctx->hmac_key) sqlcipher_free(c_ctx->hmac_key, ctx->key_sz); + if(c_ctx->pass) sqlcipher_free(c_ctx->pass, c_ctx->pass_sz); + sqlcipher_free(c_ctx, sizeof(cipher_ctx)); +} + +static int sqlcipher_codec_ctx_reserve_setup(codec_ctx *ctx) { + int base_reserve = ctx->iv_sz; /* base reserve size will be IV only */ + int reserve = base_reserve; + + ctx->hmac_sz = ctx->provider->get_hmac_sz(ctx->provider_ctx, ctx->hmac_algorithm); + + if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_HMAC)) + reserve += ctx->hmac_sz; /* if reserve will include hmac, update that size */ + + /* calculate the amount of reserve needed in even increments of the cipher block size */ + if(ctx->block_sz > 0) { + reserve = ((reserve % ctx->block_sz) == 0) ? reserve : + ((reserve / ctx->block_sz) + 1) * ctx->block_sz; + } + + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_reserve_setup: base_reserve=%d block_sz=%d md_size=%d reserve=%d", + base_reserve, ctx->block_sz, ctx->hmac_sz, reserve); + + ctx->reserve_sz = reserve; + + return SQLITE_OK; +} + +/** + * Compare one cipher_ctx to another. + * + * returns 0 if all the parameters (except the derived key data) are the same + * returns 1 otherwise + */ +static int sqlcipher_cipher_ctx_cmp(cipher_ctx *c1, cipher_ctx *c2) { + int are_equal = ( + c1->pass_sz == c2->pass_sz + && ( + c1->pass == c2->pass + || !sqlcipher_memcmp((const unsigned char*)c1->pass, + (const unsigned char*)c2->pass, + c1->pass_sz) + )); + + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_cipher_ctx_cmp: c1=%p c2=%p sqlcipher_memcmp(c1->pass, c2_pass)=%d are_equal=%d", + c1, c2, + (c1->pass == NULL || c2->pass == NULL) ? + -1 : + sqlcipher_memcmp( + (const unsigned char*)c1->pass, + (const unsigned char*)c2->pass, + c1->pass_sz + ), + are_equal + ); + + return !are_equal; /* return 0 if they are the same, 1 otherwise */ +} + +/** + * Copy one cipher_ctx to another. For instance, assuming that read_ctx is a + * fully initialized context, you could copy it to write_ctx and all yet data + * and pass information across + * + * returns SQLITE_OK if initialization was successful + * returns SQLITE_NOMEM if an error occured allocating memory + */ +static int sqlcipher_cipher_ctx_copy(codec_ctx *ctx, cipher_ctx *target, cipher_ctx *source) { + void *key = target->key; + void *hmac_key = target->hmac_key; + + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_cipher_ctx_copy: target=%p, source=%p", target, source); + if(target->pass) sqlcipher_free(target->pass, target->pass_sz); + memcpy(target, source, sizeof(cipher_ctx)); + + target->key = key; /* restore pointer to previously allocated key data */ + memcpy(target->key, source->key, ctx->key_sz); + + target->hmac_key = hmac_key; /* restore pointer to previously allocated hmac key data */ + memcpy(target->hmac_key, source->hmac_key, ctx->key_sz); + + if(source->pass && source->pass_sz) { + target->pass = sqlcipher_malloc(source->pass_sz); + if(target->pass == NULL) return SQLITE_NOMEM; + memcpy(target->pass, source->pass, source->pass_sz); + } + return SQLITE_OK; +} + +/** + * Get the keyspec for the cipher_ctx + * + * returns SQLITE_OK if assignment was successfull + * returns SQLITE_NOMEM if an error occured allocating memory + */ +static int sqlcipher_cipher_ctx_get_keyspec(codec_ctx *ctx, cipher_ctx *c_ctx, char **keyspec_ptr, int *keyspec_sz) { + int sz = 0; + char *keyspec = NULL, *out = NULL; + + if(keyspec_ptr == NULL) return SQLITE_NOMEM; + + /* establish the size for a hex-formated key specification, containing the + * raw encryption key, optional hmac key, and the salt used to generate it. + * The format will be either: + * x'hex(key)...hex(hmac_key)...hex(salt)' + * or + * x'hex(key)...hex(salt)' + *. The contents are SQLite BLOB formatted, so oversize by 3 bytes for the leading + * x' and trailing ' characters required by the spec*/ + if(ctx->flags & CIPHER_FLAG_HMAC) { /* if HMAC is enabled, encode key, hmac key, and salt */ + sz = ((ctx->key_sz + ctx->key_sz + ctx->kdf_salt_sz) * 2) + 3; + } else { /* otherwise encode key and salt */ + sz = ((ctx->key_sz + ctx->kdf_salt_sz) * 2) + 3; + } + + *keyspec_ptr = sqlcipher_malloc(sz); + keyspec = *keyspec_ptr; + + if(keyspec == NULL) return SQLITE_NOMEM; + + keyspec[0] = 'x'; + keyspec[1] = '\''; + + out = keyspec + 2; + + /* start with the encryption key */ + sqlcipher_shield(c_ctx->key, ctx->key_sz); + cipher_bin2hex(c_ctx->key, ctx->key_sz, out); + sqlcipher_shield(c_ctx->key, ctx->key_sz); + out += ctx->key_sz * 2; + + if(ctx->flags & CIPHER_FLAG_HMAC) { + /* add the hmac key after the encryption key if HMAC is in use*/ + sqlcipher_shield(c_ctx->hmac_key, ctx->key_sz); + cipher_bin2hex(c_ctx->hmac_key, ctx->key_sz, out); + sqlcipher_shield(c_ctx->hmac_key, ctx->key_sz); + out += ctx->key_sz * 2; + } + + /* finally encode the salt last */ + cipher_bin2hex(ctx->kdf_salt, ctx->kdf_salt_sz, out); + + keyspec[sz - 1] = '\''; + *keyspec_sz = sz; + + return SQLITE_OK; +} + +static void sqlcipher_set_derive_key(codec_ctx *ctx, int derive) { + if(ctx->read_ctx != NULL) ctx->read_ctx->derive_key = derive; + if(ctx->write_ctx != NULL) ctx->write_ctx->derive_key = derive; +} + +/** + * Set the passphrase for the cipher_ctx + * + * returns SQLITE_OK if assignment was successfull + * returns SQLITE_NOMEM if an error occured allocating memory + */ +static int sqlcipher_cipher_ctx_set_pass(cipher_ctx *ctx, const void *zKey, int nKey) { + /* free, zero existing pointers and size */ + if(ctx->pass) sqlcipher_free(ctx->pass, ctx->pass_sz); + ctx->pass = NULL; + ctx->pass_sz = 0; + + if(zKey && nKey) { /* if new password is provided, copy it */ + ctx->pass_sz = nKey; + ctx->pass = sqlcipher_malloc(nKey); + if(ctx->pass == NULL) return SQLITE_NOMEM; + memcpy(ctx->pass, zKey, nKey); + } + return SQLITE_OK; +} + +static int sqlcipher_codec_ctx_set_pass(codec_ctx *ctx, const void *zKey, int nKey, int for_ctx) { + cipher_ctx *c_ctx = for_ctx ? ctx->write_ctx : ctx->read_ctx; + int rc; + + if((rc = sqlcipher_cipher_ctx_set_pass(c_ctx, zKey, nKey)) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_set_pass: error %d from sqlcipher_cipher_ctx_set_pass", rc); + return rc; + } + + c_ctx->derive_key = 1; + + if(for_ctx == 2) { + if((rc = sqlcipher_cipher_ctx_copy(ctx, for_ctx ? ctx->read_ctx : ctx->write_ctx, c_ctx)) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_set_pass: error %d from sqlcipher_cipher_ctx_copy", rc); + return rc; + } + } + + return SQLITE_OK; +} + +static int sqlcipher_codec_ctx_set_kdf_iter(codec_ctx *ctx, int kdf_iter) { + if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_KEY_USED)) return SQLITE_OK; + ctx->kdf_iter = kdf_iter; + sqlcipher_set_derive_key(ctx, 1); + return SQLITE_OK; +} + +static int sqlcipher_codec_ctx_set_fast_kdf_iter(codec_ctx *ctx, int fast_kdf_iter) { + if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_KEY_USED)) return SQLITE_OK; + + ctx->fast_kdf_iter = fast_kdf_iter; + sqlcipher_set_derive_key(ctx, 1); + return SQLITE_OK; +} + +/* set the global default flag for HMAC */ +static void sqlcipher_set_default_use_hmac(int use) { + if(use) SQLCIPHER_FLAG_SET(default_flags, CIPHER_FLAG_HMAC); + else SQLCIPHER_FLAG_UNSET(default_flags,CIPHER_FLAG_HMAC); +} + +/* set the codec flag for whether this individual database should be using hmac */ +static int sqlcipher_codec_ctx_set_use_hmac(codec_ctx *ctx, int use) { + if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_KEY_USED)) return SQLITE_OK; + + if(use) { + SQLCIPHER_FLAG_SET(ctx->flags, CIPHER_FLAG_HMAC); + } else { + SQLCIPHER_FLAG_UNSET(ctx->flags, CIPHER_FLAG_HMAC); + } + + return sqlcipher_codec_ctx_reserve_setup(ctx); +} + +/* the length of plaintext header size must be: + * 1. greater than or equal to zero + * 2. a multiple of the cipher block size + * 3. less than or equal to the non-reserve size of the first database page + * + * Note: it is possible to leave the entire first page in plaintext. This is discouraged since it will + * likely leak some small amount of schema data, but it's required to support use of the recovery VFS. + * see comment in sqlcipher_page_cipher for more details. + */ +static int sqlcipher_codec_ctx_set_plaintext_header_size(codec_ctx *ctx, int size) { + if(size >= 0 && ctx->block_sz > 0 && (size % ctx->block_sz) == 0 && size <= (ctx->page_sz - ctx->reserve_sz)) { + ctx->plaintext_header_sz = size; + return SQLITE_OK; + } + ctx->plaintext_header_sz = -1; + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "%s: attempt to set invalid plantext_header_size %d", __func__, size); + return SQLITE_ERROR; +} + +static int sqlcipher_codec_ctx_set_hmac_algorithm(codec_ctx *ctx, int algorithm) { + if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_KEY_USED)) return SQLITE_OK; + + ctx->hmac_algorithm = algorithm; + return sqlcipher_codec_ctx_reserve_setup(ctx); +} + +static int sqlcipher_codec_ctx_set_kdf_algorithm(codec_ctx *ctx, int algorithm) { + if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_KEY_USED)) return SQLITE_OK; + + ctx->kdf_algorithm = algorithm; + return SQLITE_OK; +} + +static void sqlcipher_codec_ctx_set_error(codec_ctx *ctx, int error) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_set_error %d", error); + sqlite3pager_error(sqlite3BtreePager(ctx->pBt), error); + ctx->pBt->pBt->db->errCode = error; + ctx->error = error; +} + +static int sqlcipher_codec_ctx_init_kdf_salt(codec_ctx *ctx) { + sqlite3_file *fd = sqlite3PagerFile(sqlite3BtreePager(ctx->pBt)); + + if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_HAS_KDF_SALT)) { + return SQLITE_OK; /* don't reload salt when not needed */ + } + + /* read salt from header, if present, otherwise generate a new random salt */ + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_init_kdf_salt: obtaining salt"); + if(fd == NULL || fd->pMethods == 0 || sqlite3OsRead(fd, ctx->kdf_salt, ctx->kdf_salt_sz, 0) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_init_kdf_salt: unable to read salt from file header, generating random"); + if(ctx->provider->random(ctx->provider_ctx, ctx->kdf_salt, ctx->kdf_salt_sz) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_init_kdf_salt: error retrieving random bytes from provider"); + return SQLITE_ERROR; + } + } + SQLCIPHER_FLAG_SET(ctx->flags, CIPHER_FLAG_HAS_KDF_SALT); + return SQLITE_OK; +} + +static int sqlcipher_codec_ctx_set_kdf_salt(codec_ctx *ctx, unsigned char *salt, int size) { + if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_KEY_USED)) return SQLITE_OK; + + if(size >= ctx->kdf_salt_sz) { + memcpy(ctx->kdf_salt, salt, ctx->kdf_salt_sz); + SQLCIPHER_FLAG_SET(ctx->flags, CIPHER_FLAG_HAS_KDF_SALT); + return SQLITE_OK; + } + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_set_kdf_salt: attempt to set salt of incorrect size %d", size); + return SQLITE_ERROR; +} + +static int sqlcipher_codec_ctx_get_kdf_salt(codec_ctx *ctx, void** salt) { + int rc = SQLITE_OK; + if(!SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_HAS_KDF_SALT)) { + if((rc = sqlcipher_codec_ctx_init_kdf_salt(ctx)) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_get_kdf_salt: error %d from sqlcipher_codec_ctx_init_kdf_salt", rc); + } + } + *salt = ctx->kdf_salt; + + return rc; +} + +static int sqlcipher_codec_ctx_set_pagesize(codec_ctx *ctx, int size) { + if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_KEY_USED)) return SQLITE_OK; + + if(!((size != 0) && ((size & (size - 1)) == 0)) || size < 512 || size > 65536) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "cipher_page_size not a power of 2 and between 512 and 65536 inclusive"); + return SQLITE_ERROR; + } + /* attempt to free the existing page buffer */ + if(ctx->buffer) sqlcipher_free(ctx->buffer,ctx->page_sz); + ctx->page_sz = size; + + /* pre-allocate a page buffer of PageSize bytes. This will + be used as a persistent buffer for encryption and decryption + operations to avoid overhead of multiple memory allocations*/ + ctx->buffer = sqlcipher_malloc(size); + if(ctx->buffer == NULL) return SQLITE_NOMEM; + + return SQLITE_OK; +} + +static int sqlcipher_codec_ctx_init(codec_ctx **iCtx, Db *pDb, Pager *pPager, const void *zKey, int nKey) { + int rc; + codec_ctx *ctx; + + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_MEMORY, "sqlcipher_codec_ctx_init: allocating context"); + + *iCtx = sqlcipher_malloc(sizeof(codec_ctx)); + ctx = *iCtx; + + if(ctx == NULL) return SQLITE_NOMEM; + + ctx->pBt = pDb->pBt; /* assign pointer to database btree structure */ + + /* allocate space for salt data. Then read the first 16 bytes + directly off the database file. This is the salt for the + key derivation function. If we get a short read allocate + a new random salt value */ + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_MEMORY, "sqlcipher_codec_ctx_init: allocating kdf_salt"); + ctx->kdf_salt_sz = FILE_HEADER_SZ; + ctx->kdf_salt = sqlcipher_malloc(ctx->kdf_salt_sz); + if(ctx->kdf_salt == NULL) return SQLITE_NOMEM; + + /* allocate space for separate hmac salt data. We want the + HMAC derivation salt to be different than the encryption + key derivation salt */ + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_MEMORY, "sqlcipher_codec_ctx_init: allocating hmac_kdf_salt"); + ctx->hmac_kdf_salt = sqlcipher_malloc(ctx->kdf_salt_sz); + if(ctx->hmac_kdf_salt == NULL) return SQLITE_NOMEM; + + /* setup default flags */ + ctx->flags = default_flags; + + /* the context will use the current default crypto provider */ + ctx->provider = default_provider; + + if((rc = ctx->provider->ctx_init(&ctx->provider_ctx)) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_init: error %d returned from ctx_init", rc); + return rc; + } + + ctx->key_sz = ctx->provider->get_key_sz(ctx->provider_ctx); + ctx->iv_sz = ctx->provider->get_iv_sz(ctx->provider_ctx); + ctx->block_sz = ctx->provider->get_block_sz(ctx->provider_ctx); + + /* + Always overwrite page size and set to the default because the first page of the database + in encrypted and thus sqlite can't effectively determine the pagesize. this causes an issue in + cases where bytes 16 & 17 of the page header are a power of 2 as reported by John Lehman + */ + if((rc = sqlcipher_codec_ctx_set_pagesize(ctx, default_page_size)) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_init: error %d returned from sqlcipher_codec_ctx_set_pagesize with %d", rc, default_page_size); + return rc; + } + + /* establish settings for the KDF iterations and fast (HMAC) KDF iterations */ + if((rc = sqlcipher_codec_ctx_set_kdf_iter(ctx, default_kdf_iter)) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_init: error %d setting default_kdf_iter %d", rc, default_kdf_iter); + return rc; + } + + if((rc = sqlcipher_codec_ctx_set_fast_kdf_iter(ctx, FAST_PBKDF2_ITER)) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_init: error %d setting fast_kdf_iter to %d", rc, FAST_PBKDF2_ITER); + return rc; + } + + /* set the default HMAC and KDF algorithms which will determine the reserve size */ + if((rc = sqlcipher_codec_ctx_set_hmac_algorithm(ctx, default_hmac_algorithm)) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_init: error %d setting sqlcipher_codec_ctx_set_hmac_algorithm with %d", rc, default_hmac_algorithm); + return rc; + } + + /* Note that use_hmac is a special case that requires recalculation of page size + so we call set_use_hmac to perform setup */ + if((rc = sqlcipher_codec_ctx_set_use_hmac(ctx, SQLCIPHER_FLAG_GET(default_flags, CIPHER_FLAG_HMAC))) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_init: error %d setting use_hmac %d", rc, SQLCIPHER_FLAG_GET(default_flags, CIPHER_FLAG_HMAC)); + return rc; + } + + if((rc = sqlcipher_codec_ctx_set_kdf_algorithm(ctx, default_kdf_algorithm)) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_init: error %d setting sqlcipher_codec_ctx_set_kdf_algorithm with %d", rc, default_kdf_algorithm); + return rc; + } + + /* setup the default plaintext header size */ + if((rc = sqlcipher_codec_ctx_set_plaintext_header_size(ctx, default_plaintext_header_size)) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_init: error %d setting sqlcipher_codec_ctx_set_plaintext_header_size with %d", rc, default_plaintext_header_size); + return rc; + } + + /* initialize the read and write sub-contexts. this must happen after key_sz is established */ + if((rc = sqlcipher_cipher_ctx_init(ctx, &ctx->read_ctx)) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_init: error %d initializing read_ctx", rc); + return rc; + } + + if((rc = sqlcipher_cipher_ctx_init(ctx, &ctx->write_ctx)) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_init: error %d initializing write_ctx", rc); + return rc; + } + + /* set the key material on one of the sub cipher contexts and sync them up */ + if((rc = sqlcipher_codec_ctx_set_pass(ctx, zKey, nKey, 0)) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_init: error %d setting pass key", rc); + return rc; + } + + if((rc = sqlcipher_cipher_ctx_copy(ctx, ctx->write_ctx, ctx->read_ctx)) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_init: error %d copying write_ctx to read_ctx", rc); + return rc; + } + + return SQLITE_OK; +} + +/** + * Free and wipe memory associated with a cipher_ctx, including the allocated + * read_ctx and write_ctx. + */ +static void sqlcipher_codec_ctx_free(codec_ctx **iCtx) { + codec_ctx *ctx = *iCtx; + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_MEMORY, "codec_ctx_free: iCtx=%p", iCtx); + if(ctx->kdf_salt) sqlcipher_free(ctx->kdf_salt, ctx->kdf_salt_sz); + if(ctx->hmac_kdf_salt) sqlcipher_free(ctx->hmac_kdf_salt, ctx->kdf_salt_sz); + if(ctx->buffer) sqlcipher_free(ctx->buffer, ctx->page_sz); + if(ctx->provider) ctx->provider->ctx_free(&ctx->provider_ctx); + + sqlcipher_cipher_ctx_free(ctx, &ctx->read_ctx); + sqlcipher_cipher_ctx_free(ctx, &ctx->write_ctx); + sqlcipher_free(ctx, sizeof(codec_ctx)); +} + +/** convert a 32bit unsigned integer to little endian byte ordering */ +static void sqlcipher_put4byte_le(unsigned char *p, u32 v) { + p[0] = (u8)v; + p[1] = (u8)(v>>8); + p[2] = (u8)(v>>16); + p[3] = (u8)(v>>24); +} + +static int sqlcipher_page_hmac(codec_ctx *ctx, cipher_ctx *c_ctx, Pgno pgno, unsigned char *in, int in_sz, unsigned char *out) { + unsigned char pgno_raw[sizeof(pgno)]; + int rc; + /* we may convert page number to consistent representation before calculating MAC for + compatibility across big-endian and little-endian platforms. + + Note: The public release of sqlcipher 2.0.0 to 2.0.6 had a bug where the bytes of pgno + were used directly in the MAC. SQLCipher convert's to little endian by default to preserve + backwards compatibility on the most popular platforms, but can optionally be configured + to use either big endian or native byte ordering via pragma. */ + + if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_LE_PGNO)) { /* compute hmac using little endian pgno*/ + sqlcipher_put4byte_le(pgno_raw, pgno); + } else if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_BE_PGNO)) { /* compute hmac using big endian pgno */ + sqlite3Put4byte(pgno_raw, pgno); /* sqlite3Put4byte converts 32bit uint to big endian */ + } else { /* use native byte ordering */ + memcpy(pgno_raw, &pgno, sizeof(pgno)); + } + + /* include the encrypted page data, initialization vector, and page number in HMAC. This will + prevent both tampering with the ciphertext, manipulation of the IV, or resequencing otherwise + valid pages out of order in a database */ + sqlcipher_shield(c_ctx->hmac_key, ctx->key_sz); + rc = ctx->provider->hmac( + ctx->provider_ctx, ctx->hmac_algorithm, c_ctx->hmac_key, + ctx->key_sz, in, + in_sz, (unsigned char*) &pgno_raw, + sizeof(pgno), out); + sqlcipher_shield(c_ctx->hmac_key, ctx->key_sz); + + return rc; +} + +/* + * ctx - codec context + * pgno - page number in database + * size - size in bytes of input and output buffers + * mode - 1 to encrypt, 0 to decrypt + * in - pointer to input bytes + * out - pouter to output bytes + */ +static int sqlcipher_page_cipher(codec_ctx *ctx, int for_ctx, Pgno pgno, int mode, int page_sz, unsigned char *in, unsigned char *out) { + cipher_ctx *c_ctx = for_ctx ? ctx->write_ctx : ctx->read_ctx; + unsigned char *iv_in, *iv_out, *hmac_in, *hmac_out, *out_start; + int size, rc; + + /* calculate some required positions into various buffers */ + size = page_sz - ctx->reserve_sz; /* adjust size to useable size and memset reserve at end of page */ + iv_out = out + size; + iv_in = in + size; + + /* if the full amount of the first page (excluding reserve size), e.g. 4016 bytes for a 4096 byte page size with HMAC_SHA512, + * is used as a plaintext header, then the entire first page will be completely plaintext, and this function should just return early. + * This should almost never occur during normal usage, so we will log at WARN level, but it is required in the special case that + * a user wants to attempt recovery on an encrypted database. In that case, the database header must be completely plaintext so that + * the recovery VFS can be used with it's special 1st page logic */ + if(pgno == 1 && size == 0) { + sqlcipher_log(SQLCIPHER_LOG_WARN, SQLCIPHER_LOG_CORE, "%s: skipping encryption/decryption for fully plaintext header", __func__); + return SQLITE_OK; + } + + /* hmac will be written immediately after the initialization vector. the remainder of the page reserve will contain + random bytes. note, these pointers are only valid when using hmac */ + hmac_in = in + size + ctx->iv_sz; + hmac_out = out + size + ctx->iv_sz; + out_start = out; /* note the original position of the output buffer pointer, as out will be rewritten during encryption */ + + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "%s: pgno=%d, mode=%d, size=%d", __func__, pgno, mode, size); + CODEC_HEXDUMP("sqlcipher_page_cipher: input page data", in, page_sz); + + /* the key size should never be zero. If it is, error out. */ + if(ctx->key_sz == 0) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "%s: error possible context corruption, key_sz is zero for pgno=%d", __func__, pgno); + goto error; + } + + if(mode == SQLCIPHER_ENCRYPT) { + /* start at front of the reserve block, write random data to the end */ + if(ctx->provider->random(ctx->provider_ctx, iv_out, ctx->reserve_sz) != SQLITE_OK) goto error; + } else { /* SQLCIPHER_DECRYPT */ + memcpy(iv_out, iv_in, ctx->iv_sz); /* copy the iv from the input to output buffer */ + } + + if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_HMAC) && (mode == SQLCIPHER_DECRYPT)) { + if(sqlcipher_page_hmac(ctx, c_ctx, pgno, in, size + ctx->iv_sz, hmac_out) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "%s: hmac operation on decrypt failed for pgno=%d", __func__, pgno); + goto error; + } + + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "%s: comparing hmac on in=%p out=%p hmac_sz=%d", __func__, hmac_in, hmac_out, ctx->hmac_sz); + if(sqlcipher_memcmp(hmac_in, hmac_out, ctx->hmac_sz) != 0) { /* the hmac check failed */ + if(sqlite3BtreeGetAutoVacuum(ctx->pBt) != BTREE_AUTOVACUUM_NONE && sqlcipher_ismemset(in, 0, page_sz) == 0) { + /* first check if the entire contents of the page is zeros. If so, this page + resulted from a short read (i.e. sqlite attempted to pull a page after the end of the file. these + short read failures must be ignored for autovaccum mode to work so wipe the output buffer + and return SQLITE_OK to skip the decryption step. */ + sqlcipher_log(SQLCIPHER_LOG_INFO, SQLCIPHER_LOG_CORE, "%s: zeroed page (short read) for pgno %d with autovacuum enabled", __func__, pgno); + sqlcipher_memset(out, 0, page_sz); + return SQLITE_OK; + } else { + /* if the page memory is not all zeros, it means the there was data and a hmac on the page. + since the check failed, the page was either tampered with or corrupted. wipe the output buffer, + and return SQLITE_ERROR to the caller */ + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "%s: hmac check failed for pgno=%d", __func__, pgno); + goto error; + } + } + } + + sqlcipher_shield(c_ctx->key, ctx->key_sz); + rc = ctx->provider->cipher(ctx->provider_ctx, mode, c_ctx->key, ctx->key_sz, iv_out, in, size, out); + sqlcipher_shield(c_ctx->key, ctx->key_sz); + + if(rc != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "%s: cipher operation mode=%d failed for pgno=%d", __func__, mode, pgno); + goto error; + }; + + if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_HMAC) && (mode == SQLCIPHER_ENCRYPT)) { + if(sqlcipher_page_hmac(ctx, c_ctx, pgno, out_start, size + ctx->iv_sz, hmac_out) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "%s: hmac operation on encrypt failed for pgno=%d", __func__, pgno); + goto error; + }; + } + + CODEC_HEXDUMP("sqlcipher_page_cipher: output page data", out_start, page_sz); + + return SQLITE_OK; +error: + sqlcipher_memset(out, 0, page_sz); + return SQLITE_ERROR; +} + +/** + * Derive an encryption key for a cipher contex key based on the raw password. + * + * If the raw key data is formated as x'hex' and there are exactly enough hex chars to fill + * the key (i.e 64 hex chars for a 256 bit key) then the key data will be used directly. + + * Else, if the raw key data is formated as x'hex' and there are exactly enough hex chars to fill + * the key and the salt (i.e 92 hex chars for a 256 bit key and 16 byte salt) then it will be unpacked + * as the key followed by the salt. + * + * Otherwise, a key data will be derived using PBKDF2 + * + * returns SQLITE_OK if initialization was successful + * returns SQLITE_ERROR if the key could't be derived (for instance if pass is NULL or pass_sz is 0) + */ +static int sqlcipher_cipher_ctx_key_derive(codec_ctx *ctx, cipher_ctx *c_ctx) { + int rc, raw_key_sz = 0, raw_salt_sz = 0, blob_format = 0, derive_hmac_key = 1; + + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "%s: ctx->kdf_salt_sz=%d ctx->kdf_iter=%d ctx->fast_kdf_iter=%d ctx->key_sz=%d", + __func__, ctx->kdf_salt_sz, ctx->kdf_iter, ctx->fast_kdf_iter, ctx->key_sz); + + /* if key material is present on the context for derivation */ + if(!c_ctx->pass || !c_ctx->pass_sz) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_cipher_ctx_key_derive: key material is not present on the context for key derivation"); + return SQLITE_ERROR; + } + + /* if necessary, initialize the salt from the header or random source */ + if(!SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_HAS_KDF_SALT)) { + if((rc = sqlcipher_codec_ctx_init_kdf_salt(ctx)) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "%s: error %d from sqlcipher_codec_ctx_init_kdf_salt", __func__, rc); + goto error; + } + } + + /* raw hey hex encoded is 2x long */ + raw_key_sz = ctx->key_sz * 2; + raw_salt_sz = ctx->kdf_salt_sz *2; + + /* raw key must be BLOB formatted: + * 1. greater than or equal to 5 characters long + * 2. starting with x' + * 3. ending with ' + * 4. length of contents between the x' and ' must be a power of 2 + * 5. contents must be hex */ + blob_format = + c_ctx->pass_sz >= 5 + && sqlite3StrNICmp((const char *)c_ctx->pass ,"x'", 2) == 0 + && c_ctx->pass[c_ctx->pass_sz - 1] == '\'' + && (c_ctx->pass_sz - 3) % 2 == 0 + && cipher_isHex(c_ctx->pass + 2, c_ctx->pass_sz - 3); + + if(blob_format && c_ctx->pass_sz == raw_key_sz + 3) { + /* option 1 - raw key consisting of only the encryption key */ + const unsigned char *z = c_ctx->pass + 2; /* adjust lead offset of x' */ + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "%s: using raw key only", __func__); + cipher_hex2bin(z, raw_key_sz, c_ctx->key); + } else if(blob_format && c_ctx->pass_sz == raw_key_sz + raw_salt_sz + 3) { + /* option 2 - raw key consisting of the encryption key and salt */ + const unsigned char *z = c_ctx->pass + 2; + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "%s: using raw key and salt", __func__); + cipher_hex2bin(z, raw_key_sz, c_ctx->key); + cipher_hex2bin(z + raw_key_sz, raw_salt_sz, ctx->kdf_salt); + } else if(blob_format && c_ctx->pass_sz == raw_key_sz + raw_key_sz + raw_salt_sz + 3) { + /* option 3 - raw key consisting of the encryption key, then hmac key, then salt */ + const unsigned char *z = c_ctx->pass + 2; + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "%s: using raw key, hmac key, and salt", __func__); + cipher_hex2bin(z, raw_key_sz, c_ctx->key); + cipher_hex2bin(z + raw_key_sz, raw_key_sz, c_ctx->hmac_key); + cipher_hex2bin(z + raw_key_sz + raw_key_sz, raw_salt_sz, ctx->kdf_salt); + derive_hmac_key = 0; + } else { + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "%s: deriving key using PBKDF2 with %d iterations", __func__, ctx->kdf_iter); + if((rc = ctx->provider->kdf(ctx->provider_ctx, ctx->kdf_algorithm, c_ctx->pass, c_ctx->pass_sz, + ctx->kdf_salt, ctx->kdf_salt_sz, ctx->kdf_iter, + ctx->key_sz, c_ctx->key)) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "%s: error %d occurred from provider kdf generating encryption key", __func__, rc); + goto error; + } + } + + /* if this context is setup to use hmac checks, and we didn't already get an hmac + * key inbound via keyspec / raw key, then generate a seperate + * key for HMAC. In this case, we use the output of the previous KDF as the input to + * this KDF run. This ensures a distinct but predictable HMAC key. */ + if(ctx->flags & CIPHER_FLAG_HMAC && derive_hmac_key) { + int i; + + /* start by copying the kdf key into the hmac salt slot + then XOR it with the fixed hmac salt defined at compile time + this ensures that the salt passed in to derive the hmac key, while + easy to derive and publically known, is not the same as the salt used + to generate the encryption key */ + memcpy(ctx->hmac_kdf_salt, ctx->kdf_salt, ctx->kdf_salt_sz); + for(i = 0; i < ctx->kdf_salt_sz; i++) { + ctx->hmac_kdf_salt[i] ^= hmac_salt_mask; + } + + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "%s: deriving hmac key from encryption key using PBKDF2 with %d iterations", + __func__, ctx->fast_kdf_iter); + + if((rc = ctx->provider->kdf(ctx->provider_ctx, ctx->kdf_algorithm, c_ctx->key, ctx->key_sz, + ctx->hmac_kdf_salt, ctx->kdf_salt_sz, ctx->fast_kdf_iter, + ctx->key_sz, c_ctx->hmac_key)) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "%s: error occurred from provider kdf generating HMAC key", __func__, rc); + goto error; + } + } + + sqlcipher_shield(c_ctx->key, ctx->key_sz); + sqlcipher_shield(c_ctx->hmac_key, ctx->key_sz); + c_ctx->derive_key = 0; + return SQLITE_OK; + +error: + /* if an error occurred, overwrite any derived key material */ + xoshiro_randomness(c_ctx->key, ctx->key_sz); + xoshiro_randomness(c_ctx->hmac_key, ctx->key_sz); + return SQLITE_ERROR; +} + +static int sqlcipher_codec_key_derive(codec_ctx *ctx) { + /* derive key on first use if necessary */ + if(ctx->read_ctx->derive_key) { + if(sqlcipher_cipher_ctx_key_derive(ctx, ctx->read_ctx) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_key_derive: error occurred deriving read_ctx key"); + return SQLITE_ERROR; + } + } + + if(ctx->write_ctx->derive_key) { + if(sqlcipher_cipher_ctx_cmp(ctx->write_ctx, ctx->read_ctx) == 0) { + /* the relevant parameters are the same, just copy read key */ + if(sqlcipher_cipher_ctx_copy(ctx, ctx->write_ctx, ctx->read_ctx) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_key_derive: error occurred copying read_ctx to write_ctx"); + return SQLITE_ERROR; + } + } else { + if(sqlcipher_cipher_ctx_key_derive(ctx, ctx->write_ctx) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_key_derive: error occurred deriving write_ctx key"); + return SQLITE_ERROR; + } + } + } + + /* wipe and free passphrase after key derivation */ + if(ctx->store_pass != 1) { + sqlcipher_cipher_ctx_set_pass(ctx->read_ctx, NULL, 0); + sqlcipher_cipher_ctx_set_pass(ctx->write_ctx, NULL, 0); + } + + return SQLITE_OK; +} + +static int sqlcipher_codec_key_copy(codec_ctx *ctx, int source) { + if(source == CIPHER_READ_CTX) { + return sqlcipher_cipher_ctx_copy(ctx, ctx->write_ctx, ctx->read_ctx); + } else { + return sqlcipher_cipher_ctx_copy(ctx, ctx->read_ctx, ctx->write_ctx); + } +} + +static int sqlcipher_check_connection(const char *filename, char *key, int key_sz, char *sql, int *user_version, char** journal_mode) { + int rc; + sqlite3 *db = NULL; + sqlite3_stmt *statement = NULL; + char *query_journal_mode = "PRAGMA journal_mode;"; + char *query_user_version = "PRAGMA user_version;"; + + rc = sqlite3_open(filename, &db); + if(rc != SQLITE_OK) goto cleanup; + + rc = sqlite3_key(db, key, key_sz); + if(rc != SQLITE_OK) goto cleanup; + + rc = sqlite3_exec(db, sql, NULL, NULL, NULL); + if(rc != SQLITE_OK) goto cleanup; + + /* start by querying the user version. + this will fail if the key is incorrect */ + rc = sqlite3_prepare(db, query_user_version, -1, &statement, NULL); + if(rc != SQLITE_OK) goto cleanup; + + rc = sqlite3_step(statement); + if(rc == SQLITE_ROW) { + *user_version = sqlite3_column_int(statement, 0); + } else { + goto cleanup; + } + sqlite3_finalize(statement); + + rc = sqlite3_prepare(db, query_journal_mode, -1, &statement, NULL); + if(rc != SQLITE_OK) goto cleanup; + + rc = sqlite3_step(statement); + if(rc == SQLITE_ROW) { + *journal_mode = sqlite3_mprintf("%s", sqlite3_column_text(statement, 0)); + } else { + goto cleanup; + } + rc = SQLITE_OK; + /* cleanup will finalize open statement */ + +cleanup: + if(statement) sqlite3_finalize(statement); + if(db) sqlite3_close(db); + return rc; +} + +static int sqlcipher_codec_ctx_integrity_check(codec_ctx *ctx, Parse *pParse, char *column) { + Pgno page = 1; + int rc = 0; + char *result; + unsigned char *hmac_out = NULL; + sqlite3_file *fd = sqlite3PagerFile(sqlite3BtreePager(ctx->pBt)); + i64 file_sz; + + Vdbe *v = sqlite3GetVdbe(pParse); + sqlite3VdbeSetNumCols(v, 1); + sqlite3VdbeSetColName(v, 0, COLNAME_NAME, column, SQLITE_STATIC); + + if(fd == NULL || fd->pMethods == 0) { + sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, "database file is undefined", P4_TRANSIENT); + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); + goto cleanup; + } + + if(!(ctx->flags & CIPHER_FLAG_HMAC)) { + sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, "HMAC is not enabled, unable to integrity check", P4_TRANSIENT); + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); + goto cleanup; + } + + if((rc = sqlcipher_codec_key_derive(ctx)) != SQLITE_OK) { + sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, "unable to derive keys", P4_TRANSIENT); + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); + goto cleanup; + } + + sqlite3OsFileSize(fd, &file_sz); + hmac_out = sqlcipher_malloc(ctx->hmac_sz); + + for(page = 1; page <= file_sz / ctx->page_sz; page++) { + i64 offset = (page - 1) * ctx->page_sz; + int payload_sz = ctx->page_sz - ctx->reserve_sz + ctx->iv_sz; + int read_sz = ctx->page_sz; + + /* skip integrity check on PAGER_SJ_PGNO since it will have no valid content */ + if(sqlite3pager_is_sj_pgno(sqlite3BtreePager(ctx->pBt), page)) continue; + + if(page==1) { + int page1_offset = ctx->plaintext_header_sz ? ctx->plaintext_header_sz : FILE_HEADER_SZ; + read_sz = read_sz - page1_offset; + payload_sz = payload_sz - page1_offset; + offset += page1_offset; + } + + sqlcipher_memset(ctx->buffer, 0, ctx->page_sz); + sqlcipher_memset(hmac_out, 0, ctx->hmac_sz); + if(sqlite3OsRead(fd, ctx->buffer, read_sz, offset) != SQLITE_OK) { + result = sqlite3_mprintf("error reading %d bytes from file page %d at offset %d", read_sz, page, offset); + sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, result, P4_DYNAMIC); + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); + } else if(sqlcipher_page_hmac(ctx, ctx->read_ctx, page, ctx->buffer, payload_sz, hmac_out) != SQLITE_OK) { + result = sqlite3_mprintf("HMAC operation failed for page %d", page); + sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, result, P4_DYNAMIC); + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); + } else if(sqlcipher_memcmp(ctx->buffer + payload_sz, hmac_out, ctx->hmac_sz) != 0) { + result = sqlite3_mprintf("HMAC verification failed for page %d", page); + sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, result, P4_DYNAMIC); + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); + } + } + + if(file_sz % ctx->page_sz != 0) { + result = sqlite3_mprintf("page %d has an invalid size of %lld bytes (expected %d bytes)", page, file_sz - ((file_sz / ctx->page_sz) * ctx->page_sz), ctx->page_sz); + sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, result, P4_DYNAMIC); + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); + } + +cleanup: + if(hmac_out != NULL) sqlcipher_free(hmac_out, ctx->hmac_sz); + return SQLITE_OK; +} + +static int sqlcipher_codec_ctx_migrate(codec_ctx *ctx) { + int i, pass_sz, keyspec_sz, nRes, user_version, rc, rc_cleanup, oflags; + Db *pDb = 0; + sqlite3 *db = ctx->pBt->db; + const char *db_filename = sqlite3_db_filename(db, "main"); + char *set_user_version = NULL, *pass = NULL, *attach_command = NULL, *migrated_db_filename = NULL, *keyspec = NULL, *temp = NULL, *journal_mode = NULL, *set_journal_mode = NULL, *pragma_compat = NULL; + Btree *pDest = NULL, *pSrc = NULL; + sqlite3_file *srcfile, *destfile; +#if defined(_WIN32) || defined(SQLITE_OS_WINRT) + LPWSTR w_db_filename = NULL, w_migrated_db_filename = NULL; + int w_db_filename_sz = 0, w_migrated_db_filename_sz = 0; +#endif + pass_sz = keyspec_sz = rc = user_version = 0; + + if(!db_filename || sqlite3Strlen30(db_filename) < 1) + goto cleanup; /* exit immediately if this is an in memory database */ + + /* pull the provided password / key material off the current codec context */ + pass_sz = ctx->read_ctx->pass_sz; + pass = sqlcipher_malloc(pass_sz+1); + memset(pass, 0, pass_sz+1); + memcpy(pass, ctx->read_ctx->pass, pass_sz); + + /* Version 4 - current, no upgrade required, so exit immediately */ + rc = sqlcipher_check_connection(db_filename, pass, pass_sz, "", &user_version, &journal_mode); + if(rc == SQLITE_OK){ + sqlcipher_log(SQLCIPHER_LOG_INFO, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: no upgrade required - exiting"); + goto cleanup; + } + + for(i = 3; i > 0; i--) { + pragma_compat = sqlite3_mprintf("PRAGMA cipher_compatibility = %d;", i); + rc = sqlcipher_check_connection(db_filename, pass, pass_sz, pragma_compat, &user_version, &journal_mode); + if(rc == SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: version %d format found", i); + goto migrate; + } + if(pragma_compat) sqlcipher_free(pragma_compat, sqlite3Strlen30(pragma_compat)); + pragma_compat = NULL; + } + + /* if we exit the loop normally we failed to determine the version, this is an error */ + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: unable to determine format version for upgrade: this may indicate custom settings were used "); + goto handle_error; + +migrate: + + temp = sqlite3_mprintf("%s-migrated", db_filename); + /* overallocate migrated_db_filename, because sqlite3OsOpen will read past the null terminator + * to determine whether the filename was URI formatted */ + migrated_db_filename = sqlcipher_malloc(sqlite3Strlen30(temp)+2); + memcpy(migrated_db_filename, temp, sqlite3Strlen30(temp)); + sqlcipher_free(temp, sqlite3Strlen30(temp)); + + attach_command = sqlite3_mprintf("ATTACH DATABASE '%s' as migrate;", migrated_db_filename, pass); + set_user_version = sqlite3_mprintf("PRAGMA migrate.user_version = %d;", user_version); + + rc = sqlite3_exec(db, pragma_compat, NULL, NULL, NULL); + if(rc != SQLITE_OK){ + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: set compatibility mode failed, error code %d", rc); + goto handle_error; + } + + /* force journal mode to DELETE, we will set it back later if different */ + rc = sqlite3_exec(db, "PRAGMA journal_mode = delete;", NULL, NULL, NULL); + if(rc != SQLITE_OK){ + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: force journal mode DELETE failed, error code %d", rc); + goto handle_error; + } + + rc = sqlite3_exec(db, attach_command, NULL, NULL, NULL); + if(rc != SQLITE_OK){ + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: attach failed, error code %d", rc); + goto handle_error; + } + + rc = sqlite3_key_v2(db, "migrate", pass, pass_sz); + if(rc != SQLITE_OK){ + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: keying attached database failed, error code %d", rc); + goto handle_error; + } + + rc = sqlite3_exec(db, "SELECT sqlcipher_export('migrate');", NULL, NULL, NULL); + if(rc != SQLITE_OK){ + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: sqlcipher_export failed, error code %d", rc); + goto handle_error; + } + +#ifdef SQLCIPHER_TEST + if((cipher_test_flags & TEST_FAIL_MIGRATE) > 0) { + rc = SQLITE_ERROR; + sqlcipher_log(SQLCIPHER_LOG_WARN, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: simulated migrate failure, error code %d", rc); + goto handle_error; + } +#endif + + rc = sqlite3_exec(db, set_user_version, NULL, NULL, NULL); + if(rc != SQLITE_OK){ + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: set user version failed, error code %d", rc); + goto handle_error; + } + + if( !db->autoCommit ){ + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: cannot migrate from within a transaction"); + goto handle_error; + } + if( db->nVdbeActive>1 ){ + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: cannot migrate - SQL statements in progress"); + goto handle_error; + } + + pDest = db->aDb[0].pBt; + pDb = &(db->aDb[db->nDb-1]); + pSrc = pDb->pBt; + + nRes = sqlite3BtreeGetRequestedReserve(pSrc); + /* unset the BTS_PAGESIZE_FIXED flag to avoid SQLITE_READONLY */ + pDest->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED; + rc = sqlite3BtreeSetPageSize(pDest, default_page_size, nRes, 0); + if(rc != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: failed to set btree page size to %d res %d rc %d", default_page_size, nRes, rc); + goto handle_error; + } + + sqlcipherCodecGetKey(db, db->nDb - 1, (void**)&keyspec, &keyspec_sz); + SQLCIPHER_FLAG_UNSET(ctx->flags, CIPHER_FLAG_KEY_USED); + sqlcipherCodecAttach(db, 0, keyspec, keyspec_sz); + + srcfile = sqlite3PagerFile(sqlite3BtreePager(pSrc)); + destfile = sqlite3PagerFile(sqlite3BtreePager(pDest)); + + sqlite3OsClose(srcfile); + sqlite3OsClose(destfile); + +#if defined(_WIN32) || defined(SQLITE_OS_WINRT) + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: performing windows MoveFileExA"); + + w_db_filename_sz = MultiByteToWideChar(CP_UTF8, 0, (LPCCH) db_filename, -1, NULL, 0); + w_db_filename = sqlcipher_malloc(w_db_filename_sz * sizeof(wchar_t)); + w_db_filename_sz = MultiByteToWideChar(CP_UTF8, 0, (LPCCH) db_filename, -1, (const LPWSTR) w_db_filename, w_db_filename_sz); + + w_migrated_db_filename_sz = MultiByteToWideChar(CP_UTF8, 0, (LPCCH) migrated_db_filename, -1, NULL, 0); + w_migrated_db_filename = sqlcipher_malloc(w_migrated_db_filename_sz * sizeof(wchar_t)); + w_migrated_db_filename_sz = MultiByteToWideChar(CP_UTF8, 0, (LPCCH) migrated_db_filename, -1, (const LPWSTR) w_migrated_db_filename, w_migrated_db_filename_sz); + + if(!MoveFileExW(w_migrated_db_filename, w_db_filename, MOVEFILE_REPLACE_EXISTING)) { + rc = SQLITE_ERROR; + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: error occurred while renaming migration files %d", rc); + goto handle_error; + } +#else + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: performing POSIX rename"); + if ((rc = rename(migrated_db_filename, db_filename)) != 0) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: error occurred while renaming migration files %s to %s: %d", migrated_db_filename, db_filename, rc); + goto handle_error; + } +#endif + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: renamed migration database %s to main database %s: %d", migrated_db_filename, db_filename, rc); + + rc = sqlite3OsOpen(db->pVfs, migrated_db_filename, srcfile, SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_MAIN_DB, &oflags); + if(rc != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: failed to reopen migration database %s: %d", migrated_db_filename, rc); + goto handle_error; + } + + rc = sqlite3OsOpen(db->pVfs, db_filename, destfile, SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_MAIN_DB, &oflags); + if(rc != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: failed to reopen main database %s: %d", db_filename, rc); + goto handle_error; + } + + sqlite3pager_reset(sqlite3BtreePager(pDest)); + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: reset pager"); + +handle_error: + rc_cleanup = sqlite3_exec(db, "DETACH DATABASE migrate;", NULL, NULL, NULL); + if(rc_cleanup != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_WARN, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: DETACH DATABASE migrate failed: %d", rc_cleanup); + /* only overwrite the rc in the cleanup stage if it is currently not an error. This will prevent overwriting a previous error that occured earlier in migration */ + if(rc == SQLITE_OK) { + rc = rc_cleanup; + } + } + + sqlite3ResetAllSchemasOfConnection(db); + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: reset all schemas"); + + if(journal_mode) { + set_journal_mode = sqlite3_mprintf("PRAGMA journal_mode = %s;", journal_mode); + rc_cleanup = sqlite3_exec(db, set_journal_mode, NULL, NULL, NULL); + if(rc_cleanup != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: failed to re-set journal mode via %s: %d", set_journal_mode, rc_cleanup); + if(rc == SQLITE_OK) { + rc = rc_cleanup; + } + } + } + + if(migrated_db_filename) { + int del_rc = sqlite3OsDelete(db->pVfs, migrated_db_filename, 0); + if(del_rc != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: failed to delete migration database %s: %d", migrated_db_filename, del_rc); + } + } + + if(rc != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: an error occurred attempting to migrate the database - last error %d", rc); + sqlite3pager_reset(sqlite3BtreePager(ctx->pBt)); + ctx->error = rc; /* set flag for deferred error */ + } + +cleanup: + if(pass) sqlcipher_free(pass, pass_sz); + if(keyspec) sqlcipher_free(keyspec, keyspec_sz); + if(attach_command) sqlcipher_free(attach_command, sqlite3Strlen30(attach_command)); + if(migrated_db_filename) sqlcipher_free(migrated_db_filename, sqlite3Strlen30(migrated_db_filename)); + if(set_user_version) sqlcipher_free(set_user_version, sqlite3Strlen30(set_user_version)); + if(set_journal_mode) sqlcipher_free(set_journal_mode, sqlite3Strlen30(set_journal_mode)); + if(journal_mode) sqlcipher_free(journal_mode, sqlite3Strlen30(journal_mode)); + if(pragma_compat) sqlcipher_free(pragma_compat, sqlite3Strlen30(pragma_compat)); +#if defined(_WIN32) || defined(SQLITE_OS_WINRT) + if(w_db_filename) sqlcipher_free(w_db_filename, w_db_filename_sz); + if(w_migrated_db_filename) sqlcipher_free(w_migrated_db_filename, w_migrated_db_filename_sz); +#endif + + return rc; +} + +static int sqlcipher_codec_add_random(codec_ctx *ctx, const char *zRight, int random_sz){ + const char *suffix = &zRight[random_sz-1]; + int n = random_sz - 3; /* adjust for leading x' and tailing ' */ + if (n > 0 && + sqlite3StrNICmp((const char *)zRight ,"x'", 2) == 0 && + sqlite3StrNICmp(suffix, "'", 1) == 0 && + n % 2 == 0) { + int rc = 0; + int buffer_sz = n / 2; + unsigned char *random; + const unsigned char *z = (const unsigned char *)zRight + 2; /* adjust lead offset of x' */ + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_codec_add_random: using raw random blob from hex"); + random = sqlcipher_malloc(buffer_sz); + memset(random, 0, buffer_sz); + cipher_hex2bin(z, n, random); + rc = ctx->provider->add_random(ctx->provider_ctx, random, buffer_sz); + sqlcipher_free(random, buffer_sz); + return rc; + } + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_add_random: attemt to add random with invalid format"); + return SQLITE_ERROR; +} + +#if defined(_WIN32) +/* On windows convert to utf-16 when writing to stderr or stdout to avoid + * a potential exception when writing mixed context to those streams + * when using the shell. */ +static int sqlcipher_fprintf(FILE* stream, const char* format, ...) { + int sz; + va_list ap; + + if (stream == stderr || stream == stdout) { + char* buffer = NULL; + wchar_t* wbuffer = NULL; + + va_start(ap, format); + buffer = sqlite3_vmprintf(format, ap); + va_end(ap); + sz = (int)strlen(buffer); + + wbuffer = sqlite3_malloc((sz + 1) * sizeof(wchar_t)); + if (wbuffer == NULL) return -1; + + sz = MultiByteToWideChar(CP_UTF8, 0, buffer, sz, wbuffer, sz); + wbuffer[sz] = (wchar_t) 0; + fputws(wbuffer, stream); + + sqlite3_free(wbuffer); + sqlite3_free(buffer); + } else { + va_start(ap, format); + sz = vfprintf(stream, format, ap); + va_end(ap); + } + return sz; +} +#else +#define sqlcipher_fprintf fprintf +#endif + +#if !defined(SQLITE_OMIT_TRACE) + +#define SQLCIPHER_PROFILE_FMT "Elapsed time:%.3f ms - %s\n" +#define SQLCIPHER_PROFILE_FMT_OSLOG "Elapsed time:%{public}.3f ms - %{public}s\n" + +static int sqlcipher_profile_callback(unsigned int trace, void *file, void *stmt, void *run_time){ + FILE *f = (FILE*) file; + double elapsed = (*((sqlite3_uint64*)run_time))/1000000.0; + if(f == NULL) { +#if !defined(SQLCIPHER_OMIT_LOG_DEVICE) +#if defined(__ANDROID__) + __android_log_print(ANDROID_LOG_DEBUG, "sqlcipher", SQLCIPHER_PROFILE_FMT, elapsed, sqlite3_sql((sqlite3_stmt*)stmt)); +#elif defined(__APPLE__) + os_log(OS_LOG_DEFAULT, SQLCIPHER_PROFILE_FMT_OSLOG, elapsed, sqlite3_sql((sqlite3_stmt*)stmt)); +#endif +#endif + } else { + sqlcipher_fprintf(f, SQLCIPHER_PROFILE_FMT, elapsed, sqlite3_sql((sqlite3_stmt*)stmt)); + } + return SQLITE_OK; +} +#endif + +static int sqlcipher_cipher_profile(sqlite3 *db, const char *destination){ +#if defined(SQLITE_OMIT_TRACE) + return SQLITE_ERROR; +#else + FILE *f = NULL; + if(sqlite3_stricmp(destination, "off") == 0){ + sqlite3_trace_v2(db, 0, NULL, NULL); /* disable tracing */ + } else { + if(sqlite3_stricmp(destination, "stdout") == 0){ + f = stdout; + }else if(sqlite3_stricmp(destination, "stderr") == 0){ + f = stderr; + }else if(sqlite3_stricmp(destination, "logcat") == 0 || sqlite3_stricmp(destination, "device") == 0){ + f = NULL; /* file pointer will be NULL indicating the device target (i.e. logcat or oslog). We will accept logcat for backwards compatibility */ + }else{ +#if !defined(SQLCIPHER_PROFILE_USE_FOPEN) && (defined(_WIN32) && (__STDC_VERSION__ > 199901L) || defined(SQLITE_OS_WINRT)) + if(fopen_s(&f, destination, "a") != 0) return SQLITE_ERROR; +#else + if((f = fopen(destination, "a")) == 0) return SQLITE_ERROR; +#endif + } + sqlite3_trace_v2(db, SQLITE_TRACE_PROFILE, sqlcipher_profile_callback, f); + } + return SQLITE_OK; +#endif +} + +static char *sqlcipher_get_log_level_str(unsigned int level) { + switch(level) { + case SQLCIPHER_LOG_ERROR: + return "ERROR"; + case SQLCIPHER_LOG_WARN: + return "WARN"; + case SQLCIPHER_LOG_INFO: + return "INFO"; + case SQLCIPHER_LOG_DEBUG: + return "DEBUG"; + case SQLCIPHER_LOG_TRACE: + return "TRACE"; + case SQLCIPHER_LOG_ANY: + return "ANY"; + } + return "NONE"; +} + +static char *sqlcipher_get_log_source_str(unsigned int source) { + switch(source) { + case SQLCIPHER_LOG_NONE: + return "NONE"; + case SQLCIPHER_LOG_CORE: + return "CORE"; + case SQLCIPHER_LOG_MEMORY: + return "MEMORY"; + case SQLCIPHER_LOG_MUTEX: + return "MUTEX"; + case SQLCIPHER_LOG_PROVIDER: + return "PROVIDER"; + } + return "ANY"; +} + +static char *sqlcipher_get_log_sources_str(unsigned int source) { + if(source == SQLCIPHER_LOG_NONE) { + return sqlite3_mprintf("%s", "NONE"); + } else if (source == SQLCIPHER_LOG_ANY) { + return sqlite3_mprintf("%s", "ANY"); + } else { + char *sources = NULL; + unsigned int flag; + for(flag = SQLCIPHER_LOG_CORE; flag != 0; flag = flag << 1) { + if(SQLCIPHER_FLAG_GET(source, flag)) { + char *src = sqlcipher_get_log_source_str(flag); + if(sources) { + char *tmp = sqlite3_mprintf("%s %s", sources, src); + sqlite3_free(sources); + sources = tmp; + } else { + sources = sqlite3_mprintf("%s", src); + } + } + } + return sources; + } +} + +#ifndef SQLCIPHER_OMIT_LOG +/* constants from https://github.com/Alexpux/mingw-w64/blob/master/mingw-w64-crt/misc/gettimeofday.c */ +#define FILETIME_1970 116444736000000000ull /* seconds between 1/1/1601 and 1/1/1970 */ +#define HECTONANOSEC_PER_SEC 10000000ull +#define MAX_LOG_LEN 8192 +void sqlcipher_log(unsigned int level, unsigned int source, const char *message, ...) { + va_list params; + va_start(params, message); + char formatted[MAX_LOG_LEN]; + size_t len = 0; + +#ifdef CODEC_DEBUG +#if defined(SQLCIPHER_OMIT_LOG_DEVICE) || (!defined(__ANDROID__) && !defined(__APPLE__)) + sqlite3_vsnprintf(MAX_LOG_LEN, formatted, message, params); + sqlcipher_fprintf(stderr, formatted); + sqlcipher_fprintf(stderr, "\n"); + goto end; +#else +#if defined(__ANDROID__) + __android_log_vprint(ANDROID_LOG_DEBUG, "sqlcipher", message, params); + goto end; +#elif defined(__APPLE__) + sqlite3_vsnprintf(MAX_LOG_LEN, formatted, message, params); + os_log(OS_LOG_DEFAULT, "%{public}s", formatted); + goto end; +#endif +#endif +#endif + if( + level > sqlcipher_log_level /* log level is higher, e.g. level filter is at ERROR but this message is DEBUG */ + || !SQLCIPHER_FLAG_GET(sqlcipher_log_source, source) /* source filter doesn't match this message source */ + || (sqlcipher_log_device == 0 && sqlcipher_log_file == NULL) /* no configured log target */ + ) { + /* skip logging this message */ + goto end; + } + + sqlite3_snprintf(MAX_LOG_LEN, formatted, "%s %s ", sqlcipher_get_log_level_str(level), sqlcipher_get_log_source_str(source)); + len = strlen(formatted); + sqlite3_vsnprintf(MAX_LOG_LEN - (int) len, formatted + (int) len, message, params); + +#if !defined(SQLCIPHER_OMIT_LOG_DEVICE) + if(sqlcipher_log_device) { +#if defined(__ANDROID__) + __android_log_write(ANDROID_LOG_DEBUG, "sqlcipher", formatted); + goto end; +#elif defined(__APPLE__) + os_log(OS_LOG_DEFAULT, "%{public}s", formatted); + goto end; +#endif + } +#endif + + if(sqlcipher_log_file != NULL){ + char buffer[24]; + struct tm tt; + int ms; + time_t sec; +#ifdef _WIN32 + SYSTEMTIME st; + FILETIME ft; + GetSystemTime(&st); + SystemTimeToFileTime(&st, &ft); + sec = (time_t) ((*((sqlite_int64*)&ft) - FILETIME_1970) / HECTONANOSEC_PER_SEC); + ms = st.wMilliseconds; + localtime_s(&tt, &sec); +#else + struct timeval tv; + gettimeofday(&tv, NULL); + sec = tv.tv_sec; + ms = tv.tv_usec/1000.0; + localtime_r(&sec, &tt); +#endif + if(strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", &tt)) { + sqlcipher_fprintf((FILE*)sqlcipher_log_file, "%s.%03d: %s\n", buffer, ms, formatted); + goto end; + } + } + +end: + va_end(params); +} +#endif + +static int sqlcipher_set_log(const char *destination){ +#ifdef SQLCIPHER_OMIT_LOG + return SQLITE_ERROR; +#else + /* close open trace file if it is not stdout or stderr, then + reset trace settings */ + if(sqlcipher_log_file != NULL && sqlcipher_log_file != stdout && sqlcipher_log_file != stderr) { + fclose((FILE*)sqlcipher_log_file); + } + sqlcipher_log_file = NULL; + sqlcipher_log_device = 0; + + if(sqlite3_stricmp(destination, "logcat") == 0 || sqlite3_stricmp(destination, "device") == 0){ + /* use the appropriate device log. accept logcat for backwards compatibility */ + sqlcipher_log_device = 1; + } else if(sqlite3_stricmp(destination, "stdout") == 0){ + sqlcipher_log_file = stdout; + }else if(sqlite3_stricmp(destination, "stderr") == 0){ + sqlcipher_log_file = stderr; + }else if(sqlite3_stricmp(destination, "off") != 0){ +#if !defined(SQLCIPHER_PROFILE_USE_FOPEN) && (defined(_WIN32) && (__STDC_VERSION__ > 199901L) || defined(SQLITE_OS_WINRT)) + if(fopen_s(&sqlcipher_log_file, destination, "a") != 0) return SQLITE_ERROR; +#else + if((sqlcipher_log_file = fopen(destination, "a")) == 0) return SQLITE_ERROR; +#endif + } + sqlcipher_log(SQLCIPHER_LOG_INFO, SQLCIPHER_LOG_CORE, "sqlcipher_set_log: set log to %s", destination); + return SQLITE_OK; +#endif +} + +static void sqlcipher_vdbe_return_string(Parse *pParse, const char *zLabel, const char *value, int value_type){ + Vdbe *v = sqlite3GetVdbe(pParse); + sqlite3VdbeSetNumCols(v, 1); + sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLabel, SQLITE_STATIC); + sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, value, value_type); + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); +} + +static int codec_set_btree_to_codec_pagesize(sqlite3 *db, Db *pDb, codec_ctx *ctx) { + int rc; + + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "codec_set_btree_to_codec_pagesize: sqlite3BtreeSetPageSize() size=%d reserve=%d", ctx->page_sz, ctx->reserve_sz); + + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "codec_set_btree_to_codec_pagesize: entering database mutex %p", db->mutex); + sqlite3_mutex_enter(db->mutex); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "codec_set_btree_to_codec_pagesize: entered database mutex %p", db->mutex); + db->nextPagesize = ctx->page_sz; + + /* before forcing the page size we need to unset the BTS_PAGESIZE_FIXED flag, else + sqliteBtreeSetPageSize will block the change */ + pDb->pBt->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED; + rc = sqlite3BtreeSetPageSize(pDb->pBt, ctx->page_sz, ctx->reserve_sz, 0); + + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "codec_set_btree_to_codec_pagesize: sqlite3BtreeSetPageSize returned %d", rc); + + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "codec_set_btree_to_codec_pagesize: leaving database mutex %p", db->mutex); + sqlite3_mutex_leave(db->mutex); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "codec_set_btree_to_codec_pagesize: left database mutex %p", db->mutex); + + return rc; +} + +static int codec_set_pass_key(sqlite3* db, int nDb, const void *zKey, int nKey, int for_ctx) { + struct Db *pDb = &db->aDb[nDb]; + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "codec_set_pass_key: db=%p nDb=%d for_ctx=%d", db, nDb, for_ctx); + if(pDb->pBt) { + codec_ctx *ctx = (codec_ctx*) sqlcipherPagerGetCodec(sqlite3BtreePager(pDb->pBt)); + + if(ctx) { + return sqlcipher_codec_ctx_set_pass(ctx, zKey, nKey, for_ctx); + } else { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "codec_set_pass_key: error ocurred fetching codec from pager on db %d", nDb); + return SQLITE_ERROR; + } + } + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "codec_set_pass_key: no btree present on db %d", nDb); + return SQLITE_ERROR; +} + +int sqlcipher_codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLeft, const char *zRight) { + struct Db *pDb = &db->aDb[iDb]; + codec_ctx *ctx = NULL; + int rc; + + if(pDb->pBt) { + ctx = (codec_ctx*) sqlcipherPagerGetCodec(sqlite3BtreePager(pDb->pBt)); + } + + if(sqlite3_stricmp(zLeft, "key") !=0 && sqlite3_stricmp(zLeft, "rekey") != 0) { + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_codec_pragma: db=%p iDb=%d pParse=%p zLeft=%s zRight=%s ctx=%p", db, iDb, pParse, zLeft, zRight, ctx); + } + +#ifdef SQLCIPHER_TEST + if( sqlite3_stricmp(zLeft,"cipher_test_on")==0 ){ + if( zRight ) { + if(sqlite3_stricmp(zRight, "fail_encrypt")==0) { + SQLCIPHER_FLAG_SET(cipher_test_flags,TEST_FAIL_ENCRYPT); + } else + if(sqlite3_stricmp(zRight, "fail_decrypt")==0) { + SQLCIPHER_FLAG_SET(cipher_test_flags,TEST_FAIL_DECRYPT); + } else + if(sqlite3_stricmp(zRight, "fail_migrate")==0) { + SQLCIPHER_FLAG_SET(cipher_test_flags,TEST_FAIL_MIGRATE); + } + } + } else + if( sqlite3_stricmp(zLeft,"cipher_test_off")==0 ){ + if( zRight ) { + if(sqlite3_stricmp(zRight, "fail_encrypt")==0) { + SQLCIPHER_FLAG_UNSET(cipher_test_flags,TEST_FAIL_ENCRYPT); + } else + if(sqlite3_stricmp(zRight, "fail_decrypt")==0) { + SQLCIPHER_FLAG_UNSET(cipher_test_flags,TEST_FAIL_DECRYPT); + } else + if(sqlite3_stricmp(zRight, "fail_migrate")==0) { + SQLCIPHER_FLAG_UNSET(cipher_test_flags,TEST_FAIL_MIGRATE); + } + } + } else + if( sqlite3_stricmp(zLeft,"cipher_test")==0 ){ + char *flags = sqlite3_mprintf("%u", cipher_test_flags); + sqlcipher_vdbe_return_string(pParse, "cipher_test", flags, P4_DYNAMIC); + }else + if( sqlite3_stricmp(zLeft,"cipher_test_rand")==0 ){ + if( zRight ) { + int rand = atoi(zRight); + cipher_test_rand = rand; + } else { + char *rand = sqlite3_mprintf("%d", cipher_test_rand); + sqlcipher_vdbe_return_string(pParse, "cipher_test_rand", rand, P4_DYNAMIC); + } + } else +#endif + if( sqlite3_stricmp(zLeft, "cipher_fips_status")== 0 && !zRight ){ + if(ctx) { + char *fips_mode_status = sqlite3_mprintf("%d", ctx->provider->fips_status(ctx->provider_ctx)); + sqlcipher_vdbe_return_string(pParse, "cipher_fips_status", fips_mode_status, P4_DYNAMIC); + } + } else + if( sqlite3_stricmp(zLeft, "cipher_status")== 0 && !zRight ){ + if(ctx && ctx->error == SQLITE_OK) { + sqlcipher_vdbe_return_string(pParse, "cipher_status", "1", P4_TRANSIENT); + } else { + sqlcipher_vdbe_return_string(pParse, "cipher_status", "0", P4_TRANSIENT); + } + } else + if( sqlite3_stricmp(zLeft, "cipher_store_pass")==0 && zRight ) { + if(ctx) { + char *deprecation = "PRAGMA cipher_store_pass is deprecated, please remove from use"; + ctx->store_pass = sqlite3GetBoolean(zRight, 1); + sqlcipher_vdbe_return_string(pParse, "cipher_store_pass", deprecation, P4_TRANSIENT); + sqlite3_log(SQLITE_WARNING, deprecation); + } + } else + if( sqlite3_stricmp(zLeft, "cipher_store_pass")==0 && !zRight ) { + if(ctx){ + char *store_pass_value = sqlite3_mprintf("%d", ctx->store_pass); + sqlcipher_vdbe_return_string(pParse, "cipher_store_pass", store_pass_value, P4_DYNAMIC); + } + } else + if( sqlite3_stricmp(zLeft, "cipher_profile")== 0 && zRight ){ + char *profile_status = sqlite3_mprintf("%d", sqlcipher_cipher_profile(db, zRight)); + sqlcipher_vdbe_return_string(pParse, "cipher_profile", profile_status, P4_DYNAMIC); + } else + if( sqlite3_stricmp(zLeft, "cipher_add_random")==0 && zRight ){ + if(ctx) { + char *add_random_status = sqlite3_mprintf("%d", sqlcipher_codec_add_random(ctx, zRight, sqlite3Strlen30(zRight))); + sqlcipher_vdbe_return_string(pParse, "cipher_add_random", add_random_status, P4_DYNAMIC); + } + } else + if( sqlite3_stricmp(zLeft, "cipher_migrate")==0 && !zRight ){ + if(ctx){ + int status = sqlcipher_codec_ctx_migrate(ctx); + char *migrate_status = sqlite3_mprintf("%d", status); + sqlcipher_vdbe_return_string(pParse, "cipher_migrate", migrate_status, P4_DYNAMIC); + if(status != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_pragma: error occurred during cipher_migrate: %d", status); + } + } + } else + if( sqlite3_stricmp(zLeft, "cipher_provider")==0 && !zRight ){ + if(ctx) { + sqlcipher_vdbe_return_string(pParse, "cipher_provider", + ctx->provider->get_provider_name(ctx->provider_ctx), P4_TRANSIENT); + } + } else + if( sqlite3_stricmp(zLeft, "cipher_provider_version")==0 && !zRight){ + if(ctx) { + sqlcipher_vdbe_return_string(pParse, "cipher_provider_version", + ctx->provider->get_provider_version(ctx->provider_ctx), P4_TRANSIENT); + } + } else + if( sqlite3_stricmp(zLeft, "cipher_version")==0 && !zRight ){ + sqlcipher_vdbe_return_string(pParse, "cipher_version", sqlcipher_version(), P4_DYNAMIC); + }else + if( sqlite3_stricmp(zLeft, "cipher")==0 ){ + if(ctx) { + if( zRight ) { + const char* message = "PRAGMA cipher is no longer supported."; + sqlcipher_vdbe_return_string(pParse, "cipher", message, P4_TRANSIENT); + sqlite3_log(SQLITE_WARNING, message); + }else { + sqlcipher_vdbe_return_string(pParse, "cipher", + ctx->provider->get_cipher(ctx->provider_ctx), P4_TRANSIENT); + } + } + }else + if( sqlite3_stricmp(zLeft, "rekey_cipher")==0 && zRight ){ + const char* message = "PRAGMA rekey_cipher is no longer supported."; + sqlcipher_vdbe_return_string(pParse, "rekey_cipher", message, P4_TRANSIENT); + sqlite3_log(SQLITE_WARNING, message); + }else + if( sqlite3_stricmp(zLeft,"cipher_default_kdf_iter")==0 ){ + if( zRight ) { + default_kdf_iter = atoi(zRight); /* change default KDF iterations */ + } else { + char *kdf_iter = sqlite3_mprintf("%d", default_kdf_iter); + sqlcipher_vdbe_return_string(pParse, "cipher_default_kdf_iter", kdf_iter, P4_DYNAMIC); + } + }else + if( sqlite3_stricmp(zLeft, "kdf_iter")==0 ){ + if(ctx) { + if( zRight ) { + sqlcipher_codec_ctx_set_kdf_iter(ctx, atoi(zRight)); /* change of RW PBKDF2 iteration */ + } else { + char *kdf_iter = sqlite3_mprintf("%d", ctx->kdf_iter); + sqlcipher_vdbe_return_string(pParse, "kdf_iter", kdf_iter, P4_DYNAMIC); + } + } + }else + if( sqlite3_stricmp(zLeft, "fast_kdf_iter")==0){ + if(ctx) { + if( zRight ) { + char *deprecation = "PRAGMA fast_kdf_iter is deprecated, please remove from use"; + sqlcipher_codec_ctx_set_fast_kdf_iter(ctx, atoi(zRight)); /* change of RW PBKDF2 iteration */ + sqlcipher_vdbe_return_string(pParse, "fast_kdf_iter", deprecation, P4_TRANSIENT); + sqlite3_log(SQLITE_WARNING, deprecation); + } else { + char *fast_kdf_iter = sqlite3_mprintf("%d", ctx->fast_kdf_iter); + sqlcipher_vdbe_return_string(pParse, "fast_kdf_iter", fast_kdf_iter, P4_DYNAMIC); + } + } + }else + if( sqlite3_stricmp(zLeft, "rekey_kdf_iter")==0 && zRight ){ + const char* message = "PRAGMA rekey_kdf_iter is no longer supported."; + sqlcipher_vdbe_return_string(pParse, "rekey_kdf_iter", message, P4_TRANSIENT); + sqlite3_log(SQLITE_WARNING, message); + }else + if( sqlite3_stricmp(zLeft,"page_size")==0 || sqlite3_stricmp(zLeft,"cipher_page_size")==0 ){ + /* PRAGMA cipher_page_size will alter the size of the database pages while ensuring that the + required reserve space is allocated at the end of each page. This will also override the + standard SQLite PRAGMA page_size behavior if a codec context is attached to the database handle. + If PRAGMA page_size is invoked but a codec context is not attached (i.e. dealing with a standard + unencrypted database) then return early and allow the standard PRAGMA page_size logic to apply. */ + if(ctx) { + if( zRight ) { + int size = atoi(zRight); + rc = sqlcipher_codec_ctx_set_pagesize(ctx, size); + if(rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, rc); + rc = codec_set_btree_to_codec_pagesize(db, pDb, ctx); + if(rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, rc); + } else { + char * page_size = sqlite3_mprintf("%d", ctx->page_sz); + sqlcipher_vdbe_return_string(pParse, "cipher_page_size", page_size, P4_DYNAMIC); + } + } else { + return 0; /* return early so that the PragTyp_PAGE_SIZE case logic in pragma.c will take effect */ + } + }else + if( sqlite3_stricmp(zLeft,"cipher_default_page_size")==0 ){ + if( zRight ) { + default_page_size = atoi(zRight); + } else { + char *page_size = sqlite3_mprintf("%d", default_page_size); + sqlcipher_vdbe_return_string(pParse, "cipher_default_page_size", page_size, P4_DYNAMIC); + } + }else + if( sqlite3_stricmp(zLeft,"cipher_default_use_hmac")==0 ){ + if( zRight ) { + sqlcipher_set_default_use_hmac(sqlite3GetBoolean(zRight,1)); + } else { + char *default_use_hmac = sqlite3_mprintf("%d", SQLCIPHER_FLAG_GET(default_flags, CIPHER_FLAG_HMAC)); + sqlcipher_vdbe_return_string(pParse, "cipher_default_use_hmac", default_use_hmac, P4_DYNAMIC); + } + }else + if( sqlite3_stricmp(zLeft,"cipher_use_hmac")==0 ){ + if(ctx) { + if( zRight ) { + rc = sqlcipher_codec_ctx_set_use_hmac(ctx, sqlite3GetBoolean(zRight,1)); + if(rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, rc); + /* since the use of hmac has changed, the page size may also change */ + rc = codec_set_btree_to_codec_pagesize(db, pDb, ctx); + if(rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, rc); + } else { + char *hmac_flag = sqlite3_mprintf("%d", SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_HMAC)); + sqlcipher_vdbe_return_string(pParse, "cipher_use_hmac", hmac_flag, P4_DYNAMIC); + } + } + }else + if( sqlite3_stricmp(zLeft,"cipher_hmac_pgno")==0 ){ + if(ctx) { + if(zRight) { + char *deprecation = "PRAGMA cipher_hmac_pgno is deprecated, please remove from use"; + /* clear both pgno endian flags */ + if(sqlite3_stricmp(zRight, "le") == 0) { + SQLCIPHER_FLAG_UNSET(ctx->flags, CIPHER_FLAG_BE_PGNO); + SQLCIPHER_FLAG_SET(ctx->flags, CIPHER_FLAG_LE_PGNO); + } else if(sqlite3_stricmp(zRight, "be") == 0) { + SQLCIPHER_FLAG_UNSET(ctx->flags, CIPHER_FLAG_LE_PGNO); + SQLCIPHER_FLAG_SET(ctx->flags, CIPHER_FLAG_BE_PGNO); + } else if(sqlite3_stricmp(zRight, "native") == 0) { + SQLCIPHER_FLAG_UNSET(ctx->flags, CIPHER_FLAG_LE_PGNO); + SQLCIPHER_FLAG_UNSET(ctx->flags, CIPHER_FLAG_BE_PGNO); + } + sqlcipher_vdbe_return_string(pParse, "cipher_hmac_pgno", deprecation, P4_TRANSIENT); + sqlite3_log(SQLITE_WARNING, deprecation); + + } else { + if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_LE_PGNO)) { + sqlcipher_vdbe_return_string(pParse, "cipher_hmac_pgno", "le", P4_TRANSIENT); + } else if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_BE_PGNO)) { + sqlcipher_vdbe_return_string(pParse, "cipher_hmac_pgno", "be", P4_TRANSIENT); + } else { + sqlcipher_vdbe_return_string(pParse, "cipher_hmac_pgno", "native", P4_TRANSIENT); + } + } + } + }else + if( sqlite3_stricmp(zLeft,"cipher_hmac_salt_mask")==0 ){ + if(ctx) { + if(zRight) { + char *deprecation = "PRAGMA cipher_hmac_salt_mask is deprecated, please remove from use"; + if (sqlite3StrNICmp(zRight ,"x'", 2) == 0 && sqlite3Strlen30(zRight) == 5) { + unsigned char mask = 0; + const unsigned char *hex = (const unsigned char *)zRight+2; + cipher_hex2bin(hex,2,&mask); + hmac_salt_mask = mask; + } + sqlcipher_vdbe_return_string(pParse, "cipher_hmac_salt_mask", deprecation, P4_TRANSIENT); + sqlite3_log(SQLITE_WARNING, deprecation); + } else { + char *mask = sqlite3_mprintf("%02x", hmac_salt_mask); + sqlcipher_vdbe_return_string(pParse, "cipher_hmac_salt_mask", mask, P4_DYNAMIC); + } + } + }else + if( sqlite3_stricmp(zLeft,"cipher_plaintext_header_size")==0 ){ + if(ctx) { + if( zRight ) { + int size = atoi(zRight); + /* deliberately ignore result code, if size is invalid it will be set to -1 + and trip the error later in the codec */ + sqlcipher_codec_ctx_set_plaintext_header_size(ctx, size); + } else { + char *size = sqlite3_mprintf("%d", ctx->plaintext_header_sz); + sqlcipher_vdbe_return_string(pParse, "cipher_plaintext_header_size", size, P4_DYNAMIC); + } + } + }else + if( sqlite3_stricmp(zLeft,"cipher_default_plaintext_header_size")==0 ){ + if( zRight ) { + default_plaintext_header_size = atoi(zRight); + } else { + char *size = sqlite3_mprintf("%d", default_plaintext_header_size); + sqlcipher_vdbe_return_string(pParse, "cipher_default_plaintext_header_size", size, P4_DYNAMIC); + } + }else + if( sqlite3_stricmp(zLeft,"cipher_salt")==0 ){ + if(ctx) { + if(zRight) { + if (sqlite3StrNICmp(zRight ,"x'", 2) == 0 && sqlite3Strlen30(zRight) == (FILE_HEADER_SZ*2)+3) { + unsigned char *salt = (unsigned char*) sqlite3_malloc(FILE_HEADER_SZ); + const unsigned char *hex = (const unsigned char *)zRight+2; + cipher_hex2bin(hex,FILE_HEADER_SZ*2,salt); + sqlcipher_codec_ctx_set_kdf_salt(ctx, salt, FILE_HEADER_SZ); + sqlite3_free(salt); + } + } else { + void *salt; + char *hexsalt = (char*) sqlite3_malloc((FILE_HEADER_SZ*2)+1); + if((rc = sqlcipher_codec_ctx_get_kdf_salt(ctx, &salt)) == SQLITE_OK) { + cipher_bin2hex(salt, FILE_HEADER_SZ, hexsalt); + sqlcipher_vdbe_return_string(pParse, "cipher_salt", hexsalt, P4_DYNAMIC); + } else { + sqlite3_free(hexsalt); + sqlcipher_codec_ctx_set_error(ctx, rc); + } + } + } + }else + if( sqlite3_stricmp(zLeft,"cipher_hmac_algorithm")==0 ){ + if(ctx) { + if(zRight) { + rc = SQLITE_ERROR; + if(sqlite3_stricmp(zRight, SQLCIPHER_HMAC_SHA1_LABEL) == 0) { + rc = sqlcipher_codec_ctx_set_hmac_algorithm(ctx, SQLCIPHER_HMAC_SHA1); + } else if(sqlite3_stricmp(zRight, SQLCIPHER_HMAC_SHA256_LABEL) == 0) { + rc = sqlcipher_codec_ctx_set_hmac_algorithm(ctx, SQLCIPHER_HMAC_SHA256); + } else if(sqlite3_stricmp(zRight, SQLCIPHER_HMAC_SHA512_LABEL) == 0) { + rc = sqlcipher_codec_ctx_set_hmac_algorithm(ctx, SQLCIPHER_HMAC_SHA512); + } + if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); + rc = codec_set_btree_to_codec_pagesize(db, pDb, ctx); + if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); + } else { + int algorithm = ctx->hmac_algorithm; + if(ctx->hmac_algorithm == SQLCIPHER_HMAC_SHA1) { + sqlcipher_vdbe_return_string(pParse, "cipher_hmac_algorithm", SQLCIPHER_HMAC_SHA1_LABEL, P4_TRANSIENT); + } else if(algorithm == SQLCIPHER_HMAC_SHA256) { + sqlcipher_vdbe_return_string(pParse, "cipher_hmac_algorithm", SQLCIPHER_HMAC_SHA256_LABEL, P4_TRANSIENT); + } else if(algorithm == SQLCIPHER_HMAC_SHA512) { + sqlcipher_vdbe_return_string(pParse, "cipher_hmac_algorithm", SQLCIPHER_HMAC_SHA512_LABEL, P4_TRANSIENT); + } + } + } + }else + if( sqlite3_stricmp(zLeft,"cipher_default_hmac_algorithm")==0 ){ + if(zRight) { + rc = SQLITE_OK; + if(sqlite3_stricmp(zRight, SQLCIPHER_HMAC_SHA1_LABEL) == 0) { + default_hmac_algorithm = SQLCIPHER_HMAC_SHA1; + } else if(sqlite3_stricmp(zRight, SQLCIPHER_HMAC_SHA256_LABEL) == 0) { + default_hmac_algorithm = SQLCIPHER_HMAC_SHA256; + } else if(sqlite3_stricmp(zRight, SQLCIPHER_HMAC_SHA512_LABEL) == 0) { + default_hmac_algorithm = SQLCIPHER_HMAC_SHA512; + } + } else { + if(default_hmac_algorithm == SQLCIPHER_HMAC_SHA1) { + sqlcipher_vdbe_return_string(pParse, "cipher_default_hmac_algorithm", SQLCIPHER_HMAC_SHA1_LABEL, P4_TRANSIENT); + } else if(default_hmac_algorithm == SQLCIPHER_HMAC_SHA256) { + sqlcipher_vdbe_return_string(pParse, "cipher_default_hmac_algorithm", SQLCIPHER_HMAC_SHA256_LABEL, P4_TRANSIENT); + } else if(default_hmac_algorithm == SQLCIPHER_HMAC_SHA512) { + sqlcipher_vdbe_return_string(pParse, "cipher_default_hmac_algorithm", SQLCIPHER_HMAC_SHA512_LABEL, P4_TRANSIENT); + } + } + }else + if( sqlite3_stricmp(zLeft,"cipher_kdf_algorithm")==0 ){ + if(ctx) { + if(zRight) { + rc = SQLITE_ERROR; + if(sqlite3_stricmp(zRight, SQLCIPHER_PBKDF2_HMAC_SHA1_LABEL) == 0) { + rc = sqlcipher_codec_ctx_set_kdf_algorithm(ctx, SQLCIPHER_PBKDF2_HMAC_SHA1); + } else if(sqlite3_stricmp(zRight, SQLCIPHER_PBKDF2_HMAC_SHA256_LABEL) == 0) { + rc = sqlcipher_codec_ctx_set_kdf_algorithm(ctx, SQLCIPHER_PBKDF2_HMAC_SHA256); + } else if(sqlite3_stricmp(zRight, SQLCIPHER_PBKDF2_HMAC_SHA512_LABEL) == 0) { + rc = sqlcipher_codec_ctx_set_kdf_algorithm(ctx, SQLCIPHER_PBKDF2_HMAC_SHA512); + } + if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); + } else { + if(ctx->kdf_algorithm == SQLCIPHER_PBKDF2_HMAC_SHA1) { + sqlcipher_vdbe_return_string(pParse, "cipher_kdf_algorithm", SQLCIPHER_PBKDF2_HMAC_SHA1_LABEL, P4_TRANSIENT); + } else if(ctx->kdf_algorithm == SQLCIPHER_PBKDF2_HMAC_SHA256) { + sqlcipher_vdbe_return_string(pParse, "cipher_kdf_algorithm", SQLCIPHER_PBKDF2_HMAC_SHA256_LABEL, P4_TRANSIENT); + } else if(ctx->kdf_algorithm == SQLCIPHER_PBKDF2_HMAC_SHA512) { + sqlcipher_vdbe_return_string(pParse, "cipher_kdf_algorithm", SQLCIPHER_PBKDF2_HMAC_SHA512_LABEL, P4_TRANSIENT); + } + } + } + }else + if( sqlite3_stricmp(zLeft,"cipher_default_kdf_algorithm")==0 ){ + if(zRight) { + rc = SQLITE_OK; + if(sqlite3_stricmp(zRight, SQLCIPHER_PBKDF2_HMAC_SHA1_LABEL) == 0) { + default_kdf_algorithm = SQLCIPHER_PBKDF2_HMAC_SHA1; + } else if(sqlite3_stricmp(zRight, SQLCIPHER_PBKDF2_HMAC_SHA256_LABEL) == 0) { + default_kdf_algorithm = SQLCIPHER_PBKDF2_HMAC_SHA256; + } else if(sqlite3_stricmp(zRight, SQLCIPHER_PBKDF2_HMAC_SHA512_LABEL) == 0) { + default_kdf_algorithm = SQLCIPHER_PBKDF2_HMAC_SHA512; + } + } else { + if(default_kdf_algorithm == SQLCIPHER_PBKDF2_HMAC_SHA1) { + sqlcipher_vdbe_return_string(pParse, "cipher_default_kdf_algorithm", SQLCIPHER_PBKDF2_HMAC_SHA1_LABEL, P4_TRANSIENT); + } else if(default_kdf_algorithm == SQLCIPHER_PBKDF2_HMAC_SHA256) { + sqlcipher_vdbe_return_string(pParse, "cipher_default_kdf_algorithm", SQLCIPHER_PBKDF2_HMAC_SHA256_LABEL, P4_TRANSIENT); + } else if(default_kdf_algorithm == SQLCIPHER_PBKDF2_HMAC_SHA512) { + sqlcipher_vdbe_return_string(pParse, "cipher_default_kdf_algorithm", SQLCIPHER_PBKDF2_HMAC_SHA512_LABEL, P4_TRANSIENT); + } + } + }else + if( sqlite3_stricmp(zLeft,"cipher_compatibility")==0 ){ + if(ctx) { + if(zRight) { + int version = atoi(zRight); + + switch(version) { + case 1: + rc = sqlcipher_codec_ctx_set_pagesize(ctx, 1024); + if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); + rc = sqlcipher_codec_ctx_set_hmac_algorithm(ctx, SQLCIPHER_HMAC_SHA1); + if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); + rc = sqlcipher_codec_ctx_set_kdf_algorithm(ctx, SQLCIPHER_PBKDF2_HMAC_SHA1); + if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); + rc = sqlcipher_codec_ctx_set_kdf_iter(ctx, 4000); + if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); + rc = sqlcipher_codec_ctx_set_use_hmac(ctx, 0); + if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); + break; + + case 2: + rc = sqlcipher_codec_ctx_set_pagesize(ctx, 1024); + if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); + rc = sqlcipher_codec_ctx_set_hmac_algorithm(ctx, SQLCIPHER_HMAC_SHA1); + if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); + rc = sqlcipher_codec_ctx_set_kdf_algorithm(ctx, SQLCIPHER_PBKDF2_HMAC_SHA1); + if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); + rc = sqlcipher_codec_ctx_set_kdf_iter(ctx, 4000); + if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); + rc = sqlcipher_codec_ctx_set_use_hmac(ctx, 1); + if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); + break; + + case 3: + rc = sqlcipher_codec_ctx_set_pagesize(ctx, 1024); + if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); + rc = sqlcipher_codec_ctx_set_hmac_algorithm(ctx, SQLCIPHER_HMAC_SHA1); + if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); + rc = sqlcipher_codec_ctx_set_kdf_algorithm(ctx, SQLCIPHER_PBKDF2_HMAC_SHA1); + if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); + rc = sqlcipher_codec_ctx_set_kdf_iter(ctx, 64000); + if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); + rc = sqlcipher_codec_ctx_set_use_hmac(ctx, 1); + if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); + break; + + default: + rc = sqlcipher_codec_ctx_set_pagesize(ctx, 4096); + if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); + rc = sqlcipher_codec_ctx_set_hmac_algorithm(ctx, SQLCIPHER_HMAC_SHA512); + if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); + rc = sqlcipher_codec_ctx_set_kdf_algorithm(ctx, SQLCIPHER_PBKDF2_HMAC_SHA512); + if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); + rc = sqlcipher_codec_ctx_set_kdf_iter(ctx, 256000); + if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); + rc = sqlcipher_codec_ctx_set_use_hmac(ctx, 1); + if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); + break; + } + + rc = codec_set_btree_to_codec_pagesize(db, pDb, ctx); + if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); + } + } + }else + if( sqlite3_stricmp(zLeft,"cipher_default_compatibility")==0 ){ + if(zRight) { + int version = atoi(zRight); + switch(version) { + case 1: + default_page_size = 1024; + default_hmac_algorithm = SQLCIPHER_HMAC_SHA1; + default_kdf_algorithm = SQLCIPHER_PBKDF2_HMAC_SHA1; + default_kdf_iter = 4000; + sqlcipher_set_default_use_hmac(0); + break; + + case 2: + default_page_size = 1024; + default_hmac_algorithm = SQLCIPHER_HMAC_SHA1; + default_kdf_algorithm = SQLCIPHER_PBKDF2_HMAC_SHA1; + default_kdf_iter = 4000; + sqlcipher_set_default_use_hmac(1); + break; + + case 3: + default_page_size = 1024; + default_hmac_algorithm = SQLCIPHER_HMAC_SHA1; + default_kdf_algorithm = SQLCIPHER_PBKDF2_HMAC_SHA1; + default_kdf_iter = 64000; + sqlcipher_set_default_use_hmac(1); + break; + + default: + default_page_size = 4096; + default_hmac_algorithm = SQLCIPHER_HMAC_SHA512; + default_kdf_algorithm = SQLCIPHER_PBKDF2_HMAC_SHA512; + default_kdf_iter = 256000; + sqlcipher_set_default_use_hmac(1); + break; + } + } + }else + if( sqlite3_stricmp(zLeft,"cipher_memory_security")==0 ){ + if( zRight ) { + if(sqlite3GetBoolean(zRight,1)) { + /* memory security can only be enabled, not disabled */ + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_set_mem_security: on"); + sqlcipher_mem_security_on = 1; + } + } else { + /* only report that memory security is enabled if pragma cipher_memory_security is ON and + SQLCipher's allocator/deallocator was run at least one time */ + int state = sqlcipher_mem_security_on && sqlcipher_mem_executed; + char *on = sqlite3_mprintf("%d", state); + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, + "sqlcipher_get_mem_security: sqlcipher_mem_security_on = %d, sqlcipher_mem_executed = %d", + sqlcipher_mem_security_on, sqlcipher_mem_executed); + sqlcipher_vdbe_return_string(pParse, "cipher_memory_security", on, P4_DYNAMIC); + } + }else + if( sqlite3_stricmp(zLeft,"cipher_settings")==0 ){ + if(ctx) { + int algorithm; + char *pragma; + + pragma = sqlite3_mprintf("PRAGMA kdf_iter = %d;", ctx->kdf_iter); + sqlcipher_vdbe_return_string(pParse, "pragma", pragma, P4_DYNAMIC); + + pragma = sqlite3_mprintf("PRAGMA cipher_page_size = %d;", ctx->page_sz); + sqlcipher_vdbe_return_string(pParse, "pragma", pragma, P4_DYNAMIC); + + pragma = sqlite3_mprintf("PRAGMA cipher_use_hmac = %d;", SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_HMAC)); + sqlcipher_vdbe_return_string(pParse, "pragma", pragma, P4_DYNAMIC); + + pragma = sqlite3_mprintf("PRAGMA cipher_plaintext_header_size = %d;", ctx->plaintext_header_sz); + sqlcipher_vdbe_return_string(pParse, "pragma", pragma, P4_DYNAMIC); + + algorithm = ctx->hmac_algorithm; + pragma = NULL; + if(algorithm == SQLCIPHER_HMAC_SHA1) { + pragma = sqlite3_mprintf("PRAGMA cipher_hmac_algorithm = %s;", SQLCIPHER_HMAC_SHA1_LABEL); + } else if(algorithm == SQLCIPHER_HMAC_SHA256) { + pragma = sqlite3_mprintf("PRAGMA cipher_hmac_algorithm = %s;", SQLCIPHER_HMAC_SHA256_LABEL); + } else if(algorithm == SQLCIPHER_HMAC_SHA512) { + pragma = sqlite3_mprintf("PRAGMA cipher_hmac_algorithm = %s;", SQLCIPHER_HMAC_SHA512_LABEL); + } + sqlcipher_vdbe_return_string(pParse, "pragma", pragma, P4_DYNAMIC); + + algorithm = ctx->kdf_algorithm; + pragma = NULL; + if(algorithm == SQLCIPHER_PBKDF2_HMAC_SHA1) { + pragma = sqlite3_mprintf("PRAGMA cipher_kdf_algorithm = %s;", SQLCIPHER_PBKDF2_HMAC_SHA1_LABEL); + } else if(algorithm == SQLCIPHER_PBKDF2_HMAC_SHA256) { + pragma = sqlite3_mprintf("PRAGMA cipher_kdf_algorithm = %s;", SQLCIPHER_PBKDF2_HMAC_SHA256_LABEL); + } else if(algorithm == SQLCIPHER_PBKDF2_HMAC_SHA512) { + pragma = sqlite3_mprintf("PRAGMA cipher_kdf_algorithm = %s;", SQLCIPHER_PBKDF2_HMAC_SHA512_LABEL); + } + sqlcipher_vdbe_return_string(pParse, "pragma", pragma, P4_DYNAMIC); + + } + }else + if( sqlite3_stricmp(zLeft,"cipher_default_settings")==0 ){ + char *pragma; + + pragma = sqlite3_mprintf("PRAGMA cipher_default_kdf_iter = %d;", default_kdf_iter); + sqlcipher_vdbe_return_string(pParse, "pragma", pragma, P4_DYNAMIC); + + pragma = sqlite3_mprintf("PRAGMA cipher_default_page_size = %d;", default_page_size); + sqlcipher_vdbe_return_string(pParse, "pragma", pragma, P4_DYNAMIC); + + pragma = sqlite3_mprintf("PRAGMA cipher_default_use_hmac = %d;", SQLCIPHER_FLAG_GET(default_flags, CIPHER_FLAG_HMAC)); + sqlcipher_vdbe_return_string(pParse, "pragma", pragma, P4_DYNAMIC); + + pragma = sqlite3_mprintf("PRAGMA cipher_default_plaintext_header_size = %d;", default_plaintext_header_size); + sqlcipher_vdbe_return_string(pParse, "pragma", pragma, P4_DYNAMIC); + + pragma = NULL; + if(default_hmac_algorithm == SQLCIPHER_HMAC_SHA1) { + pragma = sqlite3_mprintf("PRAGMA cipher_default_hmac_algorithm = %s;", SQLCIPHER_HMAC_SHA1_LABEL); + } else if(default_hmac_algorithm == SQLCIPHER_HMAC_SHA256) { + pragma = sqlite3_mprintf("PRAGMA cipher_default_hmac_algorithm = %s;", SQLCIPHER_HMAC_SHA256_LABEL); + } else if(default_hmac_algorithm == SQLCIPHER_HMAC_SHA512) { + pragma = sqlite3_mprintf("PRAGMA cipher_default_hmac_algorithm = %s;", SQLCIPHER_HMAC_SHA512_LABEL); + } + sqlcipher_vdbe_return_string(pParse, "pragma", pragma, P4_DYNAMIC); + + pragma = NULL; + if(default_kdf_algorithm == SQLCIPHER_PBKDF2_HMAC_SHA1) { + pragma = sqlite3_mprintf("PRAGMA cipher_default_kdf_algorithm = %s;", SQLCIPHER_PBKDF2_HMAC_SHA1_LABEL); + } else if(default_kdf_algorithm == SQLCIPHER_PBKDF2_HMAC_SHA256) { + pragma = sqlite3_mprintf("PRAGMA cipher_default_kdf_algorithm = %s;", SQLCIPHER_PBKDF2_HMAC_SHA256_LABEL); + } else if(default_kdf_algorithm == SQLCIPHER_PBKDF2_HMAC_SHA512) { + pragma = sqlite3_mprintf("PRAGMA cipher_default_kdf_algorithm = %s;", SQLCIPHER_PBKDF2_HMAC_SHA512_LABEL); + } + sqlcipher_vdbe_return_string(pParse, "pragma", pragma, P4_DYNAMIC); + }else + if( sqlite3_stricmp(zLeft,"cipher_integrity_check")==0 ){ + if(ctx) { + sqlcipher_codec_ctx_integrity_check(ctx, pParse, "cipher_integrity_check"); + } + } else + if( sqlite3_stricmp(zLeft, "cipher_log_level")==0 ){ + if(zRight) { + sqlcipher_log_level = SQLCIPHER_LOG_NONE; + if(sqlite3_stricmp(zRight, "ERROR")==0) sqlcipher_log_level = SQLCIPHER_LOG_ERROR; + else if(sqlite3_stricmp(zRight, "WARN" )==0) sqlcipher_log_level = SQLCIPHER_LOG_WARN; + else if(sqlite3_stricmp(zRight, "INFO" )==0) sqlcipher_log_level = SQLCIPHER_LOG_INFO; + else if(sqlite3_stricmp(zRight, "DEBUG")==0) sqlcipher_log_level = SQLCIPHER_LOG_DEBUG; + else if(sqlite3_stricmp(zRight, "TRACE")==0) sqlcipher_log_level = SQLCIPHER_LOG_TRACE; + } + sqlcipher_vdbe_return_string(pParse, "cipher_log_level", sqlcipher_get_log_level_str(sqlcipher_log_level), P4_TRANSIENT); + } else + if( sqlite3_stricmp(zLeft, "cipher_log_source")==0 ){ + if(zRight) { + if(sqlite3_stricmp(zRight, "NONE" )==0) sqlcipher_log_source = SQLCIPHER_LOG_NONE; + else if(sqlite3_stricmp(zRight, "ANY" )==0) sqlcipher_log_source = SQLCIPHER_LOG_ANY; + else { + if(sqlite3_stricmp(zRight, "CORE" )==0) SQLCIPHER_FLAG_SET(sqlcipher_log_source, SQLCIPHER_LOG_CORE); + else if(sqlite3_stricmp(zRight, "MEMORY" )==0) SQLCIPHER_FLAG_SET(sqlcipher_log_source, SQLCIPHER_LOG_MEMORY); + else if(sqlite3_stricmp(zRight, "MUTEX" )==0) SQLCIPHER_FLAG_SET(sqlcipher_log_source, SQLCIPHER_LOG_MUTEX); + else if(sqlite3_stricmp(zRight, "PROVIDER")==0) SQLCIPHER_FLAG_SET(sqlcipher_log_source, SQLCIPHER_LOG_PROVIDER); + } + } + sqlcipher_vdbe_return_string(pParse, "cipher_log_source", sqlcipher_get_log_sources_str(sqlcipher_log_source), P4_DYNAMIC); + } else + if( sqlite3_stricmp(zLeft, "cipher_log")== 0 && zRight ){ + char *status = sqlite3_mprintf("%d", sqlcipher_set_log(zRight)); + sqlcipher_vdbe_return_string(pParse, "cipher_log", status, P4_DYNAMIC); + }else { + return 0; + } + return 1; +} + +/* these constants are used internally within SQLite's pager.c to differentiate between + operations on the main database or journal pages. This is important in the context + of a rekey operations, where the journal must be written using the original key + material (to allow a transactional rollback), while the new database pages are being + written with the new key material*/ +#define CODEC_READ_OP 3 +#define CODEC_WRITE_OP 6 +#define CODEC_JOURNAL_OP 7 + +/* + * sqlite3Codec can be called in multiple modes. + * encrypt mode - expected to return a pointer to the + * encrypted data without altering pData. + * decrypt mode - expected to return a pointer to pData, with + * the data decrypted in the input buffer + */ +static void* sqlite3Codec(void *iCtx, void *data, Pgno pgno, int mode) { + codec_ctx *ctx = (codec_ctx *) iCtx; + int offset = 0, rc = 0; + unsigned char *pData = (unsigned char *) data; + int cctx = CIPHER_READ_CTX; + void *out = NULL; + sqlite3_mutex *mutex = ctx->pBt->sharable ? sqlcipher_mutex(SQLCIPHER_MUTEX_SHAREDCACHE) : NULL; + + /* in shared cache mode, this needs to be mutexed to prevent a separate database handle from + * nuking the context on the shared Btree */ + if(mutex) { + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: entering mutex %p", __func__, mutex); + sqlite3_mutex_enter(mutex); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: entered mutex %p", __func__, mutex); + } + + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlite3Codec: pgno=%d, mode=%d, ctx->page_sz=%d", pgno, mode, ctx->page_sz); + + if(ctx->error != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "%s: identified deferred error condition: %d", __func__, rc); + sqlcipher_codec_ctx_set_error(ctx, ctx->error); + goto cleanup; + } + + /* call to derive keys if not present yet */ + if((rc = sqlcipher_codec_key_derive(ctx)) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlite3Codec: error occurred during key derivation: %d", rc); + sqlcipher_codec_ctx_set_error(ctx, rc); + goto cleanup; + } + + /* if the plaintext_header_size is negative that means an invalid size was set via + PRAGMA. We can't set the error state on the pager at that point because the pager + may not be open yet. However, this is a fatal error state, so abort the codec */ + if(ctx->plaintext_header_sz < 0) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlite3Codec: error invalid ctx->plaintext_header_sz: %d", ctx->plaintext_header_sz); + sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); + goto cleanup; + } + + if(pgno == 1) /* adjust starting pointers in data page for header offset on first page*/ + offset = ctx->plaintext_header_sz ? ctx->plaintext_header_sz : FILE_HEADER_SZ; + + + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlite3Codec: switch mode=%d offset=%d", mode, offset); + switch(mode) { + case CODEC_READ_OP: /* decrypt */ + if(pgno == 1) /* copy initial part of file header or SQLite magic to buffer */ + memcpy(ctx->buffer, ctx->plaintext_header_sz ? pData : (void *) SQLITE_FILE_HEADER, offset); + + rc = sqlcipher_page_cipher(ctx, cctx, pgno, SQLCIPHER_DECRYPT, ctx->page_sz - offset, pData + offset, (unsigned char*)ctx->buffer + offset); +#ifdef SQLCIPHER_TEST + if((cipher_test_flags & TEST_FAIL_DECRYPT) > 0 && sqlcipher_get_test_fail()) { + rc = SQLITE_ERROR; + sqlcipher_log(SQLCIPHER_LOG_WARN, SQLCIPHER_LOG_CORE, "sqlite3Codec: simulating decryption failure for pgno=%d, mode=%d, ctx->page_sz=%d", pgno, mode, ctx->page_sz); + } +#endif + if(rc != SQLITE_OK) { + /* failure to decrypt a page is considered a permanent error and will render the pager unusable + * in order to prevent inconsistent data being loaded into page cache. The only exception here is when a database is being "recovered", + * which we consider to be the case if the plaintext header size is set to the full non-reserved size of a page. If that is the case we consider + * this to be operating in recovery mode, and will log the error but not permanently put the codec into an error state */ + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlite3Codec: error decrypting page %d data: %d", pgno, rc); + sqlcipher_memset((unsigned char*) ctx->buffer+offset, 0, ctx->page_sz-offset); + if(ctx->plaintext_header_sz == ctx->page_sz - ctx->reserve_sz) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "%s: plaintext header size of %d indicates recovery mode, suppressing permanent error", __func__, ctx->plaintext_header_sz); + } else { + sqlcipher_codec_ctx_set_error(ctx, rc); + } + } else { + SQLCIPHER_FLAG_SET(ctx->flags, CIPHER_FLAG_KEY_USED); + } + memcpy(pData, ctx->buffer, ctx->page_sz); /* copy buffer data back to pData and return */ + out = pData; + goto cleanup; + break; + + case CODEC_WRITE_OP: /* encrypt database page, operate on write context and fall through to case 7, so the write context is used*/ + cctx = CIPHER_WRITE_CTX; + + case CODEC_JOURNAL_OP: /* encrypt journal page, operate on read context use to get the original page data from the database */ + if(pgno == 1) { /* copy initial part of file header or salt to buffer */ + void *kdf_salt = NULL; + /* retrieve the kdf salt */ + if((rc = sqlcipher_codec_ctx_get_kdf_salt(ctx, &kdf_salt)) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlite3Codec: error retrieving salt: %d", rc); + sqlcipher_codec_ctx_set_error(ctx, rc); + goto cleanup; + } + memcpy(ctx->buffer, ctx->plaintext_header_sz ? pData : kdf_salt, offset); + } + rc = sqlcipher_page_cipher(ctx, cctx, pgno, SQLCIPHER_ENCRYPT, ctx->page_sz - offset, pData + offset, (unsigned char*)ctx->buffer + offset); +#ifdef SQLCIPHER_TEST + if((cipher_test_flags & TEST_FAIL_ENCRYPT) > 0 && sqlcipher_get_test_fail()) { + rc = SQLITE_ERROR; + sqlcipher_log(SQLCIPHER_LOG_WARN, SQLCIPHER_LOG_CORE, "sqlite3Codec: simulating encryption failure for pgno=%d, mode=%d, ctx->page_sz=%d", pgno, mode, ctx->page_sz); + } +#endif + if(rc != SQLITE_OK) { + /* failure to encrypt a page is considered a permanent error and will render the pager unusable + in order to prevent corrupted pages from being written to the main databased when using WAL */ + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlite3Codec: error encrypting page %d data: %d", pgno, rc); + sqlcipher_memset((unsigned char*)ctx->buffer+offset, 0, ctx->page_sz-offset); + sqlcipher_codec_ctx_set_error(ctx, rc); + goto cleanup; + } + SQLCIPHER_FLAG_SET(ctx->flags, CIPHER_FLAG_KEY_USED); + out = ctx->buffer; /* return persistent buffer data, pData remains intact */ + goto cleanup; + break; + + default: + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlite3Codec: error unsupported codec mode %d", mode); + sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); /* unsupported mode, set error */ + out = pData; + goto cleanup; + break; + } + +cleanup: + if(mutex) { + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: leaving mutex %p", __func__, mutex); + sqlite3_mutex_leave(mutex); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: left mutex %p", __func__, mutex); + } + return out; +} + +/* This callback will be invoked when a database connection is closed. It is basically a light wrapper + * ariund sqlciher_codec_ctx_free that locks the shared cache mutex if necessary */ +static void sqlite3FreeCodecArg(void *pCodecArg) { + codec_ctx *ctx = (codec_ctx *) pCodecArg; + sqlite3_mutex *mutex = ctx->pBt->sharable ? sqlcipher_mutex(SQLCIPHER_MUTEX_SHAREDCACHE) : NULL; + + if(pCodecArg == NULL) return; + + /* in shared cache mode, this needs to be mutexed to prevent a codec context from being deallocated when + * it is in use by the codec due to cross-database handle access to the shared Btree */ + if(mutex) { + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: entering mutex %p", __func__, mutex); + sqlite3_mutex_enter(mutex); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: entered mutex %p", __func__, mutex); + } + + sqlcipher_codec_ctx_free(&ctx); /* wipe and free allocated memory for the context */ + + if(mutex) { + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: leaving mutex %p", __func__, mutex); + sqlite3_mutex_leave(mutex); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: left mutex %p", __func__, mutex); + } +} + +int sqlcipherCodecAttach(sqlite3* db, int nDb, const void *zKey, int nKey) { + struct Db *pDb = NULL; + sqlite3_file *fd = NULL; + codec_ctx *ctx = NULL; + Pager *pPager = NULL; + int rc = SQLITE_OK; + sqlite3_mutex *extra_mutex = NULL; + + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "%s: db=%p, nDb=%d", __func__, db, nDb); + + if(!sqlcipher_init) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "%s: sqlcipher not initialized %d", __func__, sqlcipher_init_error); + return sqlcipher_init_error; + } + + /* error pKey is not null and nKey is > 0 */ + if(!(nKey && zKey)) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "%s: no key", __func__); + return SQLITE_MISUSE; + } + + if(!(db && nDb >= 0 && nDb < db->nDb && (pDb = &db->aDb[nDb]))) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "%s: invalid database %p %d", __func__, db, nDb); + return SQLITE_MISUSE; + } + + /* After this point, early returns for API misuse are complete, lock on a mutex and ensure it is cleaned + * up later. If shared cache is enabled then enter a specially defined "global" recursive mutex specifically + * for isolating shared cache connections, otherwise use the built-in databse mutex */ + extra_mutex = pDb->pBt->sharable ? sqlcipher_mutex(SQLCIPHER_MUTEX_SHAREDCACHE) : NULL; + + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: entering database mutex %p", __func__, db->mutex); + sqlite3_mutex_enter(db->mutex); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: entered database mutex %p", __func__, db->mutex); + + if(extra_mutex) { + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: entering mutex %p", __func__, extra_mutex); + sqlite3_mutex_enter(extra_mutex); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: entered mutex %p", __func__, extra_mutex); + } + + pPager = sqlite3BtreePager(pDb->pBt); + ctx = (codec_ctx*) sqlcipherPagerGetCodec(pPager); + + if(ctx != NULL) { + /* There is already a codec attached to this database */ + if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_KEY_USED)) { + /* The key was derived and used successfully, so return early */ + sqlcipher_log(SQLCIPHER_LOG_INFO, SQLCIPHER_LOG_CORE, "%s: disregarding attempt to set key on an previously keyed database connection handle", __func__); + goto cleanup; +#ifndef SQLITE_DEBUG + } else if (pDb->pBt->sharable) { + /* This Btree is participating in shared cache. It would be usafe to reset and reattach a new codec, so return early. + * + * When compiled with SQLITE_DEBUG, all database connections have shared cached enabled. This behavior of disallowing reset + * of the codec on a shared cache connection will break several tests that depend on the the ability to reset the codec, + * like migration tests, repeat-keying tests, etc. Asa result we will disable shared cache handling when compiled with + * SQLIE_DEBUG enabled.*/ + sqlcipher_log(SQLCIPHER_LOG_INFO, SQLCIPHER_LOG_CORE, "%s: disregarding attempt to set key on an shared cache handle", __func__); + goto cleanup; +#endif + } else { + /* To preseve legacy functionality where an incorrect key could be replaced by a correct key without closing the database, + * if the key has not been used, and shared cache is not enabled, reset the codec on this pager entirely. + * This will call sqlcipher_codec_ctx_free directly instead of through sqlite3FreeCodecArg because this function already + * holds the shared cache mutex if it is necessary, and that avoids requiring a more expensive recursive mutex */ + sqlcipher_log(SQLCIPHER_LOG_INFO, SQLCIPHER_LOG_CORE, "%s: resetting existing codec on pager", __func__); + sqlcipher_codec_ctx_free(&ctx); + sqlcipherPagerSetCodec(pPager, NULL, NULL, NULL, NULL); + ctx = NULL; + } + } + + /* check if the sqlite3_file is open, and if not force handle to NULL */ + if((fd = sqlite3PagerFile(pPager))->pMethods == 0) fd = NULL; + + /* point the internal codec argument against the contet to be prepared */ + rc = sqlcipher_codec_ctx_init(&ctx, pDb, pPager, zKey, nKey); + + if(rc != SQLITE_OK) { + /* initialization failed, do not attach potentially corrupted context */ + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "%s: context initialization failed, forcing error state with rc=%d", __func__, rc); + /* force an error at the pager level, such that even the upstream caller ignores the return code + the pager will be in an error state and will process no further operations */ + sqlite3pager_error(pPager, rc); + pDb->pBt->pBt->db->errCode = rc; + goto cleanup; + } + + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "%s: calling sqlcipherPagerSetCodec()", __func__); + sqlcipherPagerSetCodec(pPager, sqlite3Codec, NULL, sqlite3FreeCodecArg, (void *) ctx); + + codec_set_btree_to_codec_pagesize(db, pDb, ctx); + + /* force secure delete. This has the benefit of wiping internal data when deleted + and also ensures that all pages are written to disk (i.e. not skipped by + sqlite3PagerDontWrite optimizations) */ + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "%s: calling sqlite3BtreeSecureDelete()", __func__); + sqlite3BtreeSecureDelete(pDb->pBt, 1); + + /* if fd is null, then this is an in-memory database and + we dont' want to overwrite the AutoVacuum settings + if not null, then set to the default */ + if(fd != NULL) { + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "%s: calling sqlite3BtreeSetAutoVacuum()", __func__); + sqlite3BtreeSetAutoVacuum(pDb->pBt, SQLITE_DEFAULT_AUTOVACUUM); + } + +cleanup: + + if(extra_mutex) { + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: leaving mutex %p", __func__, extra_mutex); + sqlite3_mutex_leave(extra_mutex); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: left mutex %p", __func__, extra_mutex); + } + + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: leaving database mutex %p", __func__, db->mutex); + sqlite3_mutex_leave(db->mutex); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: left database mutex %p", __func__, db->mutex); + + return rc; +} + +/* search for the index of the named database by comparing db names. main is + * always 0, temp 1, and other attached databses follow. If the name is + * NULL or empty the main database will be used consistent with sqlite defaults. If + * sqlite3 handle is NULL or the database can't be found by name, return -1 indicating + * an invalid database */ +int sqlcipher_find_db_index(sqlite3 *db, const char *zDb) { + int db_index; + + if(!db) return -1; + if(!zDb || sqlite3_stricmp(zDb,"")==0) return 0; + + for(db_index = 0; db_index < db->nDb; db_index++) { + struct Db *pDb = &db->aDb[db_index]; + if(sqlite3_stricmp(pDb->zDbSName, zDb) == 0) { + return db_index; + } + } + return -1; +} + +void sqlite3_activate_see(const char* in) { + /* do nothing, security enhancements are always active */ +} + +int sqlite3_key(sqlite3 *db, const void *pKey, int nKey) { + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "%s: db=%p", __func__, db); + return sqlite3_key_v2(db, "main", pKey, nKey); +} + +int sqlite3_key_v2(sqlite3 *db, const char *zDb, const void *pKey, int nKey) { + int db_index = sqlcipher_find_db_index(db, zDb); + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "%s: db=%p zDb=%s db_index=%d", __func__, db, zDb, db_index); + return sqlcipherCodecAttach(db, db_index, pKey, nKey); +} + +int sqlite3_rekey(sqlite3 *db, const void *pKey, int nKey) { + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlite3_rekey: db=%p", db); + return sqlite3_rekey_v2(db, "main", pKey, nKey); +} + +/* sqlite3_rekey_v2 +** Given a database, this will reencrypt the database using a new key. +** There is only one possible modes of operation - to encrypt a database +** that is already encrpyted. If the database is not already encrypted +** this should do nothing +** The proposed logic for this function follows: +** 1. Determine if the database is already encryptped +** 2. If there is NOT already a key present do nothing +** 3. If there is a key present, re-encrypt the database with the new key +*/ +int sqlite3_rekey_v2(sqlite3 *db, const char *zDb, const void *pKey, int nKey) { + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlite3_rekey_v2: db=%p zDb=%s", db, zDb); + + if(!sqlcipher_init) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "%s: sqlcipher not initialized %d",__func__, sqlcipher_init_error); + return sqlcipher_init_error; + } + + if(db && pKey && nKey) { + int db_index = sqlcipher_find_db_index(db, zDb); + struct Db *pDb = NULL; + + if(!(db_index >= 0 && db_index < db->nDb)) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "%s: invalid database zDb=%p", __func__, zDb); + return SQLITE_MISUSE; + } + + pDb = &db->aDb[db_index]; + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlite3_rekey_v2: database zDb=%p db_index:%d", zDb, db_index); + + if(pDb->pBt) { + codec_ctx *ctx; + int rc, page_count; + Pgno pgno; + PgHdr *page; + Pager *pPager = sqlite3BtreePager(pDb->pBt); + + ctx = (codec_ctx*) sqlcipherPagerGetCodec(pPager); + + if(ctx == NULL) { + /* there was no codec attached to this database, so this should do nothing! */ + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlite3_rekey_v2: no codec attached to db %s: rekey can't be used on an unencrypted database", zDb); + return SQLITE_MISUSE; + } + + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlite3_rekey_v2: entering database mutex %p", db->mutex); + sqlite3_mutex_enter(db->mutex); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlite3_rekey_v2: entered database mutex %p", db->mutex); + + codec_set_pass_key(db, db_index, pKey, nKey, CIPHER_WRITE_CTX); + + /* do stuff here to rewrite the database + ** 1. Create a transaction on the database + ** 2. Iterate through each page, reading it and then writing it. + ** 3. If that goes ok then commit and put ctx->rekey into ctx->key + ** note: don't deallocate rekey since it may be used in a subsequent iteration + */ + rc = sqlite3BtreeBeginTrans(pDb->pBt, 1, 0); /* begin write transaction */ + sqlite3PagerPagecount(pPager, &page_count); + for(pgno = 1; rc == SQLITE_OK && pgno <= (unsigned int)page_count; pgno++) { /* pgno's start at 1 see pager.c:pagerAcquire */ + if(!sqlite3pager_is_sj_pgno(pPager, pgno)) { /* skip this page (see pager.c:pagerAcquire for reasoning) */ + rc = sqlite3PagerGet(pPager, pgno, &page, 0); + if(rc == SQLITE_OK) { /* write page see pager_incr_changecounter for example */ + rc = sqlite3PagerWrite(page); + if(rc == SQLITE_OK) { + sqlite3PagerUnref(page); + } else { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlite3_rekey_v2: error %d occurred writing page %d", rc, pgno); + } + } else { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlite3_rekey_v2: error %d occurred reading page %d", rc, pgno); + } + } + } + + /* if commit was successful commit and copy the rekey data to current key, else rollback to release locks */ + if(rc == SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlite3_rekey_v2: committing"); + rc = sqlite3BtreeCommit(pDb->pBt); + sqlcipher_codec_key_copy(ctx, CIPHER_WRITE_CTX); + } else { + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlite3_rekey_v2: rollback"); + sqlite3BtreeRollback(pDb->pBt, SQLITE_ABORT_ROLLBACK, 0); + } + + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlite3_rekey_v2: leaving database mutex %p", db->mutex); + sqlite3_mutex_leave(db->mutex); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlite3_rekey_v2: left database mutex %p", db->mutex); + } + return SQLITE_OK; + } + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlite3_rekey_v2: no key provided for db %s: rekey can't be used to decrypt an encrypted database", zDb); + return SQLITE_MISUSE; +} + +/* + * Retrieves the current key attached to the database if there is a codec attached to it. + * The key will be passed back using internally allocated memory and must be freed using + * sqlcipher_free to avoid memory leaks. If no key is present, zKey will be set to NULL + * and nKey to 0. + * + * If the encryption key has not yet been derived or the key material is stored, it will + * be passed back directly. Otherwise, a "keyspec" consisting of the raw key and salt + * will be used instead. */ +void sqlcipherCodecGetKey(sqlite3* db, int nDb, void **zKey, int *nKey) { + struct Db *pDb = NULL; + + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipherCodecGetKey:db=%p, nDb=%d", db, nDb); + + *zKey = NULL; + *nKey = 0; + + if(!(db && nDb >= 0 && nDb < db->nDb)) return; /* invalid database */ + + pDb = &db->aDb[nDb]; + + if( pDb->pBt ) { + codec_ctx *ctx = (codec_ctx*) sqlcipherPagerGetCodec(sqlite3BtreePager(pDb->pBt)); + + if(ctx) { + /* if the key has not been derived yet, or the key is stored (vi PRAGMA cipher_store_pass) + * then return the key material. Other wise pass back the keyspec */ + if(ctx->read_ctx->derive_key || ctx->store_pass == 1) { + *zKey = sqlcipher_malloc(ctx->read_ctx->pass_sz); + *nKey = ctx->read_ctx->pass_sz; + memcpy(*zKey, ctx->read_ctx->pass, ctx->read_ctx->pass_sz); + } else { + sqlcipher_cipher_ctx_get_keyspec(ctx, ctx->read_ctx, (char**) zKey, nKey); + } + } + } +} + +/* + * Implementation of an "export" function that allows a caller + * to duplicate the main database to an attached database. This is intended + * as a conveneince for users who need to: + * + * 1. migrate from an non-encrypted database to an encrypted database + * 2. move from an encrypted database to a non-encrypted database + * 3. convert beween the various flavors of encrypted databases. + * + * This implementation is based heavily on the procedure and code used + * in vacuum.c, but is exposed as a function that allows export to any + * named attached database. + */ + +/* +** Finalize a prepared statement. If there was an error, store the +** text of the error message in *pzErrMsg. Return the result code. +** +** Based on vacuumFinalize from vacuum.c +*/ +static int sqlcipher_finalize(sqlite3 *db, sqlite3_stmt *pStmt, char **pzErrMsg){ + int rc; + rc = sqlite3VdbeFinalize((Vdbe*)pStmt); + if( rc ){ + sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db)); + } + return rc; +} + +/* +** Execute zSql on database db. Return an error code. +** +** Based on execSql from vacuum.c +*/ +static int sqlcipher_execSql(sqlite3 *db, char **pzErrMsg, const char *zSql){ + sqlite3_stmt *pStmt; + VVA_ONLY( int rc; ) + if( !zSql ){ + return SQLITE_NOMEM; + } + if( SQLITE_OK!=sqlite3_prepare(db, zSql, -1, &pStmt, 0) ){ + sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db)); + return sqlite3_errcode(db); + } + VVA_ONLY( rc = ) sqlite3_step(pStmt); + assert( rc!=SQLITE_ROW ); + return sqlcipher_finalize(db, pStmt, pzErrMsg); +} + +/* +** Execute zSql on database db. The statement returns exactly +** one column. Execute this as SQL on the same database. +** +** Based on execExecSql from vacuum.c +*/ +static int sqlcipher_execExecSql(sqlite3 *db, char **pzErrMsg, const char *zSql){ + sqlite3_stmt *pStmt; + int rc; + + rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0); + if( rc!=SQLITE_OK ) return rc; + + while( SQLITE_ROW==sqlite3_step(pStmt) ){ + rc = sqlcipher_execSql(db, pzErrMsg, (char*)sqlite3_column_text(pStmt, 0)); + if( rc!=SQLITE_OK ){ + sqlcipher_finalize(db, pStmt, pzErrMsg); + return rc; + } + } + + return sqlcipher_finalize(db, pStmt, pzErrMsg); +} + +/* + * copy database and schema from the main database to an attached database + * + * Based on sqlite3RunVacuum from vacuum.c +*/ +static void sqlcipher_exportFunc(sqlite3_context *context, int argc, sqlite3_value **argv) { + sqlite3 *db = sqlite3_context_db_handle(context); + const char* targetDb, *sourceDb; + int targetDb_idx = 0; + u64 saved_flags = db->flags; /* Saved value of the db->flags */ + u32 saved_mDbFlags = db->mDbFlags; /* Saved value of the db->mDbFlags */ + int saved_nChange = db->nChange; /* Saved value of db->nChange */ + int saved_nTotalChange = db->nTotalChange; /* Saved value of db->nTotalChange */ + u8 saved_mTrace = db->mTrace; /* Saved value of db->mTrace */ + int rc = SQLITE_OK; /* Return code from service routines */ + char *zSql = NULL; /* SQL statements */ + char *pzErrMsg = NULL; + + if(argc != 1 && argc != 2) { + rc = SQLITE_ERROR; + pzErrMsg = sqlite3_mprintf("invalid number of arguments (%d) passed to sqlcipher_export", argc); + goto end_of_export; + } + + if(sqlite3_value_type(argv[0]) == SQLITE_NULL) { + rc = SQLITE_ERROR; + pzErrMsg = sqlite3_mprintf("target database can't be NULL"); + goto end_of_export; + } + + targetDb = (const char*) sqlite3_value_text(argv[0]); + sourceDb = "main"; + + if(argc == 2) { + if(sqlite3_value_type(argv[1]) == SQLITE_NULL) { + rc = SQLITE_ERROR; + pzErrMsg = sqlite3_mprintf("target database can't be NULL"); + goto end_of_export; + } + sourceDb = (char *) sqlite3_value_text(argv[1]); + } + + + /* if the target database is not valid, do not proceed. */ + targetDb_idx = sqlcipher_find_db_index(db, targetDb); + if(targetDb_idx < 0) { + rc = SQLITE_ERROR; + pzErrMsg = sqlite3_mprintf("unknown database %s", targetDb); + goto end_of_export; + } + db->init.iDb = targetDb_idx; + + db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks; + db->mDbFlags |= DBFLAG_PreferBuiltin | DBFLAG_Vacuum; + db->flags &= ~(u64)(SQLITE_ForeignKeys | SQLITE_ReverseOrder | SQLITE_Defensive | SQLITE_CountRows); + db->mTrace = 0; + + /* Query the schema of the main database. Create a mirror schema + ** in the temporary database. + */ + zSql = sqlite3_mprintf( + "SELECT sql " + " FROM %s.sqlite_schema WHERE type='table' AND name!='sqlite_sequence'" + " AND rootpage>0" + , sourceDb); + rc = (zSql == NULL) ? SQLITE_NOMEM : sqlcipher_execExecSql(db, &pzErrMsg, zSql); + if( rc!=SQLITE_OK ) goto end_of_export; + sqlite3_free(zSql); + + zSql = sqlite3_mprintf( + "SELECT sql " + " FROM %s.sqlite_schema WHERE sql LIKE 'CREATE INDEX %%' " + , sourceDb); + rc = (zSql == NULL) ? SQLITE_NOMEM : sqlcipher_execExecSql(db, &pzErrMsg, zSql); + if( rc!=SQLITE_OK ) goto end_of_export; + sqlite3_free(zSql); + + zSql = sqlite3_mprintf( + "SELECT sql " + " FROM %s.sqlite_schema WHERE sql LIKE 'CREATE UNIQUE INDEX %%'" + , sourceDb); + rc = (zSql == NULL) ? SQLITE_NOMEM : sqlcipher_execExecSql(db, &pzErrMsg, zSql); + if( rc!=SQLITE_OK ) goto end_of_export; + sqlite3_free(zSql); + + /* Loop through the tables in the main database. For each, do + ** an "INSERT INTO rekey_db.xxx SELECT * FROM main.xxx;" to copy + ** the contents to the temporary database. + */ + zSql = sqlite3_mprintf( + "SELECT 'INSERT INTO %s.' || quote(name) " + "|| ' SELECT * FROM %s.' || quote(name) || ';'" + "FROM %s.sqlite_schema " + "WHERE type = 'table' AND name!='sqlite_sequence' " + " AND rootpage>0" + , targetDb, sourceDb, sourceDb); + rc = (zSql == NULL) ? SQLITE_NOMEM : sqlcipher_execExecSql(db, &pzErrMsg, zSql); + if( rc!=SQLITE_OK ) goto end_of_export; + sqlite3_free(zSql); + + /* Copy over the contents of the sequence table + */ + zSql = sqlite3_mprintf( + "SELECT 'INSERT INTO %s.' || quote(name) " + "|| ' SELECT * FROM %s.' || quote(name) || ';' " + "FROM %s.sqlite_schema WHERE name=='sqlite_sequence';" + , targetDb, sourceDb, targetDb); + rc = (zSql == NULL) ? SQLITE_NOMEM : sqlcipher_execExecSql(db, &pzErrMsg, zSql); + if( rc!=SQLITE_OK ) goto end_of_export; + sqlite3_free(zSql); + + /* Copy the triggers, views, and virtual tables from the main database + ** over to the temporary database. None of these objects has any + ** associated storage, so all we have to do is copy their entries + ** from the SQLITE_MASTER table. + */ + zSql = sqlite3_mprintf( + "INSERT INTO %s.sqlite_schema " + " SELECT type, name, tbl_name, rootpage, sql" + " FROM %s.sqlite_schema" + " WHERE type='view' OR type='trigger'" + " OR (type='table' AND rootpage=0)" + , targetDb, sourceDb); + rc = (zSql == NULL) ? SQLITE_NOMEM : sqlcipher_execSql(db, &pzErrMsg, zSql); + if( rc!=SQLITE_OK ) goto end_of_export; + sqlite3_free(zSql); + + zSql = NULL; +end_of_export: + db->init.iDb = 0; + db->flags = saved_flags; + db->mDbFlags = saved_mDbFlags; + db->nChange = saved_nChange; + db->nTotalChange = saved_nTotalChange; + db->mTrace = saved_mTrace; + + if(zSql) sqlite3_free(zSql); + + if(rc) { + if(pzErrMsg != NULL) { + sqlite3_result_error(context, pzErrMsg, -1); + sqlite3DbFree(db, pzErrMsg); + } else { + sqlite3_result_error(context, sqlite3ErrStr(rc), -1); + } + } +} +#endif +/* END SQLCIPHER */ diff --git a/src/sqlcipher.h b/src/sqlcipher.h index 6da19447f7..de25d0b1fe 100644 --- a/src/sqlcipher.h +++ b/src/sqlcipher.h @@ -35,41 +35,129 @@ #ifndef SQLCIPHER_H #define SQLCIPHER_H +#include "sqlite3.h" +#include "sqliteInt.h" -typedef struct { - int (*activate)(void *ctx); - int (*deactivate)(void *ctx); +#define SQLCIPHER_DECRYPT 0 +#define SQLCIPHER_ENCRYPT 1 + +#define SQLCIPHER_HMAC_SHA1 0 +#define SQLCIPHER_HMAC_SHA1_LABEL "HMAC_SHA1" +#define SQLCIPHER_HMAC_SHA256 1 +#define SQLCIPHER_HMAC_SHA256_LABEL "HMAC_SHA256" +#define SQLCIPHER_HMAC_SHA512 2 +#define SQLCIPHER_HMAC_SHA512_LABEL "HMAC_SHA512" + + +#define SQLCIPHER_PBKDF2_HMAC_SHA1 0 +#define SQLCIPHER_PBKDF2_HMAC_SHA1_LABEL "PBKDF2_HMAC_SHA1" +#define SQLCIPHER_PBKDF2_HMAC_SHA256 1 +#define SQLCIPHER_PBKDF2_HMAC_SHA256_LABEL "PBKDF2_HMAC_SHA256" +#define SQLCIPHER_PBKDF2_HMAC_SHA512 2 +#define SQLCIPHER_PBKDF2_HMAC_SHA512_LABEL "PBKDF2_HMAC_SHA512" + +typedef struct sqlcipher_provider sqlcipher_provider; +struct sqlcipher_provider { + int (*init)(void); + void (*shutdown)(void); const char* (*get_provider_name)(void *ctx); - int (*add_random)(void *ctx, void *buffer, int length); + int (*add_random)(void *ctx, const void *buffer, int length); int (*random)(void *ctx, void *buffer, int length); - int (*hmac)(void *ctx, unsigned char *hmac_key, int key_sz, unsigned char *in, int in_sz, unsigned char *in2, int in2_sz, unsigned char *out); - int (*kdf)(void *ctx, const unsigned char *pass, int pass_sz, unsigned char* salt, int salt_sz, int workfactor, int key_sz, unsigned char *key); - int (*cipher)(void *ctx, int mode, unsigned char *key, int key_sz, unsigned char *iv, unsigned char *in, int in_sz, unsigned char *out); - int (*set_cipher)(void *ctx, const char *cipher_name); + int (*hmac)(void *ctx, int algorithm, + const unsigned char *hmac_key, int key_sz, + const unsigned char *in, int in_sz, + const unsigned char *in2, int in2_sz, + unsigned char *out); + int (*kdf)(void *ctx, int algorithm, + const unsigned char *pass, int pass_sz, + const unsigned char* salt, int salt_sz, + int workfactor, + int key_sz, unsigned char *key); + int (*cipher)(void *ctx, int mode, + const unsigned char *key, int key_sz, + const unsigned char *iv, + const unsigned char *in, int in_sz, + unsigned char *out); const char* (*get_cipher)(void *ctx); int (*get_key_sz)(void *ctx); int (*get_iv_sz)(void *ctx); int (*get_block_sz)(void *ctx); - int (*get_hmac_sz)(void *ctx); - int (*ctx_copy)(void *target_ctx, void *source_ctx); - int (*ctx_cmp)(void *c1, void *c2); + int (*get_hmac_sz)(void *ctx, int algorithm); int (*ctx_init)(void **ctx); int (*ctx_free)(void **ctx); int (*fips_status)(void *ctx); const char* (*get_provider_version)(void *ctx); -} sqlcipher_provider; + sqlcipher_provider *next; +}; + +/* public interfaces called externally */ +int sqlcipher_extra_init(const char*); +void sqlcipher_extra_shutdown(void); +void sqlcipher_init_memmethods(void); +int sqlcipher_codec_pragma(sqlite3*, int, Parse*, const char *, const char*); +int sqlcipherCodecAttach(sqlite3*, int, const void *, int); +void sqlcipherCodecGetKey(sqlite3*, int, void**, int*); +int sqlcipher_find_db_index(sqlite3 *, const char *); /* utility functions */ -void sqlcipher_free(void *ptr, int sz); -void* sqlcipher_malloc(int sz); -void* sqlcipher_memset(void *v, unsigned char value, int len); -int sqlcipher_ismemset(const void *v, unsigned char value, int len); -int sqlcipher_memcmp(const void *v0, const void *v1, int len); -void sqlcipher_free(void *, int); +void* sqlcipher_memset(void *, unsigned char, sqlite_uint64); +int sqlcipher_ismemset(const void *, unsigned char, sqlite_uint64); +int sqlcipher_memcmp(const void *, const void *, int); +void* sqlcipher_malloc(sqlite_uint64); +void sqlcipher_free(void *, sqlite_uint64); +char* sqlcipher_version(void); /* provider interfaces */ -int sqlcipher_register_provider(sqlcipher_provider *p); -sqlcipher_provider* sqlcipher_get_provider(); +int sqlcipher_register_provider(sqlcipher_provider *); +sqlcipher_provider* sqlcipher_get_provider(void); + +#define SQLCIPHER_MUTEX_PROVIDER 0 +#define SQLCIPHER_MUTEX_PROVIDER_ACTIVATE 1 +#define SQLCIPHER_MUTEX_PROVIDER_RAND 2 +#define SQLCIPHER_MUTEX_RESERVED1 3 +#define SQLCIPHER_MUTEX_RESERVED2 4 +#define SQLCIPHER_MUTEX_RESERVED3 5 +#define SQLCIPHER_MUTEX_MEM 6 +#define SQLCIPHER_MUTEX_SHAREDCACHE 7 +#define SQLCIPHER_MUTEX_COUNT 8 + +sqlite3_mutex* sqlcipher_mutex(int); + +#define SQLCIPHER_LOG_NONE 0 +#define SQLCIPHER_LOG_ANY 0xffffffff + +#define SQLCIPHER_LOG_ERROR (1<<0) +#define SQLCIPHER_LOG_WARN (1<<1) +#define SQLCIPHER_LOG_INFO (1<<2) +#define SQLCIPHER_LOG_DEBUG (1<<3) +#define SQLCIPHER_LOG_TRACE (1<<4) + +#define SQLCIPHER_LOG_CORE (1<<0) +#define SQLCIPHER_LOG_MEMORY (1<<1) +#define SQLCIPHER_LOG_MUTEX (1<<2) +#define SQLCIPHER_LOG_PROVIDER (1<<3) + +#ifdef SQLCIPHER_OMIT_LOG +#define sqlcipher_log(level, source, message, ...) +#else +void sqlcipher_log(unsigned int level, unsigned int source, const char *message, ...); +#endif + +#ifdef CODEC_DEBUG_PAGEDATA +#define CODEC_HEXDUMP(DESC,BUFFER,LEN) \ + { \ + int __pctr; \ + printf(DESC); \ + for(__pctr=0; __pctr < LEN; __pctr++) { \ + if(__pctr % 16 == 0) printf("\n%05x: ",__pctr); \ + printf("%02x ",((unsigned char*) BUFFER)[__pctr]); \ + } \ + printf("\n"); \ + fflush(stdout); \ + } +#else +#define CODEC_HEXDUMP(DESC,BUFFER,LEN) +#endif #endif #endif diff --git a/src/sqlite.h.in b/src/sqlite.h.in index 9e7222bd50..7f82dd0e78 100644 --- a/src/sqlite.h.in +++ b/src/sqlite.h.in @@ -1,5 +1,5 @@ /* -** 2001 September 15 +** 2001-09-15 ** ** The author disclaims copyright to this source code. In place of ** a legal notice, here is a blessing: @@ -30,8 +30,8 @@ ** the version number) and changes its name to "sqlite3.h" as ** part of the build process. */ -#ifndef _SQLITE3_H_ -#define _SQLITE3_H_ +#ifndef SQLITE3_H +#define SQLITE3_H #include <stdarg.h> /* Needed for the definition of va_list */ /* @@ -43,7 +43,30 @@ extern "C" { /* -** Provide the ability to override linkage features of the interface. +** Facilitate override of interface linkage and calling conventions. +** Be aware that these macros may not be used within this particular +** translation of the amalgamation and its associated header file. +** +** The SQLITE_EXTERN and SQLITE_API macros are used to instruct the +** compiler that the target identifier should have external linkage. +** +** The SQLITE_CDECL macro is used to set the calling convention for +** public functions that accept a variable number of arguments. +** +** The SQLITE_APICALL macro is used to set the calling convention for +** public functions that accept a fixed number of arguments. +** +** The SQLITE_STDCALL macro is no longer used and is now deprecated. +** +** The SQLITE_CALLBACK macro is used to set the calling convention for +** function pointers. +** +** The SQLITE_SYSAPI macro is used to set the calling convention for +** functions provided by the operating system. +** +** Currently, the SQLITE_CDECL, SQLITE_APICALL, SQLITE_CALLBACK, and +** SQLITE_SYSAPI macros are used only when building for environments +** that require non-default calling conventions. */ #ifndef SQLITE_EXTERN # define SQLITE_EXTERN extern @@ -54,8 +77,17 @@ extern "C" { #ifndef SQLITE_CDECL # define SQLITE_CDECL #endif +#ifndef SQLITE_APICALL +# define SQLITE_APICALL +#endif #ifndef SQLITE_STDCALL -# define SQLITE_STDCALL +# define SQLITE_STDCALL SQLITE_APICALL +#endif +#ifndef SQLITE_CALLBACK +# define SQLITE_CALLBACK +#endif +#ifndef SQLITE_SYSAPI +# define SQLITE_SYSAPI #endif /* @@ -99,13 +131,16 @@ extern "C" { ** be held constant and Z will be incremented or else Y will be incremented ** and Z will be reset to zero. ** -** Since version 3.6.18, SQLite source code has been stored in the -** <a href="http://www.fossil-scm.org/">Fossil configuration management +** Since [version 3.6.18] ([dateof:3.6.18]), +** SQLite source code has been stored in the +** <a href="http://fossil-scm.org/">Fossil configuration management ** system</a>. ^The SQLITE_SOURCE_ID macro evaluates to ** a string which identifies a particular check-in of SQLite ** within its configuration management system. ^The SQLITE_SOURCE_ID -** string contains the date and time of the check-in (UTC) and an SHA1 -** hash of the entire source tree. +** string contains the date and time of the check-in (UTC) and a SHA1 +** or SHA3-256 hash of the entire source tree. If the source code has +** been edited in any way since it was last checked in, then the last +** four hexadecimal digits of the hash may be modified. ** ** See also: [sqlite3_libversion()], ** [sqlite3_libversion_number()], [sqlite3_sourceid()], @@ -114,10 +149,13 @@ extern "C" { #define SQLITE_VERSION "--VERS--" #define SQLITE_VERSION_NUMBER --VERSION-NUMBER-- #define SQLITE_SOURCE_ID "--SOURCE-ID--" +#define SQLITE_SCM_BRANCH "--SCM-BRANCH--" +#define SQLITE_SCM_TAGS "--SCM-TAGS--" +#define SQLITE_SCM_DATETIME "--SCM-DATETIME--" /* ** CAPI3REF: Run-Time Library Version Numbers -** KEYWORDS: sqlite3_version, sqlite3_sourceid +** KEYWORDS: sqlite3_version sqlite3_sourceid ** ** These interfaces provide the same information as the [SQLITE_VERSION], ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros @@ -129,19 +167,21 @@ extern "C" { ** ** <blockquote><pre> ** assert( sqlite3_libversion_number()==SQLITE_VERSION_NUMBER ); -** assert( strcmp(sqlite3_sourceid(),SQLITE_SOURCE_ID)==0 ); +** assert( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,80)==0 ); ** assert( strcmp(sqlite3_libversion(),SQLITE_VERSION)==0 ); ** </pre></blockquote>)^ ** -** ^The sqlite3_version[] string constant contains the text of [SQLITE_VERSION] -** macro. ^The sqlite3_libversion() function returns a pointer to the -** to the sqlite3_version[] string constant. The sqlite3_libversion() +** ^The sqlite3_version[] string constant contains the text of the +** [SQLITE_VERSION] macro. ^The sqlite3_libversion() function returns a +** pointer to the sqlite3_version[] string constant. The sqlite3_libversion() ** function is provided for use in DLLs since DLL users usually do not have ** direct access to string constants within the DLL. ^The ** sqlite3_libversion_number() function returns an integer equal to -** [SQLITE_VERSION_NUMBER]. ^The sqlite3_sourceid() function returns -** a pointer to a string constant whose value is the same as the -** [SQLITE_SOURCE_ID] C preprocessor macro. +** [SQLITE_VERSION_NUMBER]. ^(The sqlite3_sourceid() function returns +** a pointer to a string constant whose value is the same as the +** [SQLITE_SOURCE_ID] C preprocessor macro. Except if SQLite is built +** using an edited copy of [the amalgamation], then the last four characters +** of the hash might be different from [SQLITE_SOURCE_ID].)^ ** ** See also: [sqlite_version()] and [sqlite_source_id()]. */ @@ -153,20 +193,20 @@ int sqlite3_libversion_number(void); /* ** CAPI3REF: Run-Time Library Compilation Options Diagnostics ** -** ^The sqlite3_compileoption_used() function returns 0 or 1 -** indicating whether the specified option was defined at -** compile time. ^The SQLITE_ prefix may be omitted from the -** option name passed to sqlite3_compileoption_used(). +** ^The sqlite3_compileoption_used() function returns 0 or 1 +** indicating whether the specified option was defined at +** compile time. ^The SQLITE_ prefix may be omitted from the +** option name passed to sqlite3_compileoption_used(). ** ** ^The sqlite3_compileoption_get() function allows iterating ** over the list of options that were defined at compile time by ** returning the N-th compile time option string. ^If N is out of range, -** sqlite3_compileoption_get() returns a NULL pointer. ^The SQLITE_ -** prefix is omitted from any strings returned by +** sqlite3_compileoption_get() returns a NULL pointer. ^The SQLITE_ +** prefix is omitted from any strings returned by ** sqlite3_compileoption_get(). ** ** ^Support for the diagnostic functions sqlite3_compileoption_used() -** and sqlite3_compileoption_get() may be omitted by specifying the +** and sqlite3_compileoption_get() may be omitted by specifying the ** [SQLITE_OMIT_COMPILEOPTION_DIAGS] option at compile time. ** ** See also: SQL functions [sqlite_compileoption_used()] and @@ -175,6 +215,9 @@ int sqlite3_libversion_number(void); #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS int sqlite3_compileoption_used(const char *zOptName); const char *sqlite3_compileoption_get(int N); +#else +# define sqlite3_compileoption_used(X) 0 +# define sqlite3_compileoption_get(X) ((void*)0) #endif /* @@ -187,7 +230,7 @@ const char *sqlite3_compileoption_get(int N); ** SQLite can be compiled with or without mutexes. When ** the [SQLITE_THREADSAFE] C preprocessor macro is 1 or 2, mutexes ** are enabled and SQLite is threadsafe. When the -** [SQLITE_THREADSAFE] macro is 0, +** [SQLITE_THREADSAFE] macro is 0, ** the mutexes are omitted. Without the mutexes, it is not safe ** to use SQLite concurrently from more than one thread. ** @@ -244,12 +287,16 @@ typedef struct sqlite3 sqlite3; ** ** ^The sqlite3_int64 and sqlite_int64 types can store integer values ** between -9223372036854775808 and +9223372036854775807 inclusive. ^The -** sqlite3_uint64 and sqlite_uint64 types can store integer values +** sqlite3_uint64 and sqlite_uint64 types can store integer values ** between 0 and +18446744073709551615 inclusive. */ #ifdef SQLITE_INT64_TYPE typedef SQLITE_INT64_TYPE sqlite_int64; - typedef unsigned SQLITE_INT64_TYPE sqlite_uint64; +# ifdef SQLITE_UINT64_TYPE + typedef SQLITE_UINT64_TYPE sqlite_uint64; +# else + typedef unsigned SQLITE_INT64_TYPE sqlite_uint64; +# endif #elif defined(_MSC_VER) || defined(__BORLANDC__) typedef __int64 sqlite_int64; typedef unsigned __int64 sqlite_uint64; @@ -278,26 +325,22 @@ typedef sqlite_uint64 sqlite3_uint64; ** the [sqlite3] object is successfully destroyed and all associated ** resources are deallocated. ** -** ^If the database connection is associated with unfinalized prepared -** statements or unfinished sqlite3_backup objects then sqlite3_close() -** will leave the database connection open and return [SQLITE_BUSY]. -** ^If sqlite3_close_v2() is called with unfinalized prepared statements -** and/or unfinished sqlite3_backups, then the database connection becomes -** an unusable "zombie" which will automatically be deallocated when the -** last prepared statement is finalized or the last sqlite3_backup is -** finished. The sqlite3_close_v2() interface is intended for use with -** host languages that are garbage collected, and where the order in which -** destructors are called is arbitrary. -** -** Applications should [sqlite3_finalize | finalize] all [prepared statements], -** [sqlite3_blob_close | close] all [BLOB handles], and +** Ideally, applications should [sqlite3_finalize | finalize] all +** [prepared statements], [sqlite3_blob_close | close] all [BLOB handles], and ** [sqlite3_backup_finish | finish] all [sqlite3_backup] objects associated -** with the [sqlite3] object prior to attempting to close the object. ^If -** sqlite3_close_v2() is called on a [database connection] that still has -** outstanding [prepared statements], [BLOB handles], and/or -** [sqlite3_backup] objects then it returns [SQLITE_OK] and the deallocation -** of resources is deferred until all [prepared statements], [BLOB handles], -** and [sqlite3_backup] objects are also destroyed. +** with the [sqlite3] object prior to attempting to close the object. +** ^If the database connection is associated with unfinalized prepared +** statements, BLOB handlers, and/or unfinished sqlite3_backup objects then +** sqlite3_close() will leave the database connection open and return +** [SQLITE_BUSY]. ^If sqlite3_close_v2() is called with unfinalized prepared +** statements, unclosed BLOB handlers, and/or unfinished sqlite3_backups, +** it returns [SQLITE_OK] regardless, but instead of deallocating the database +** connection immediately, it marks the database connection as an unusable +** "zombie" and makes arrangements to automatically deallocate the database +** connection after all prepared statements are finalized, all BLOB handles +** are closed, and all backups have finished. The sqlite3_close_v2() interface +** is intended for use with host languages that are garbage collected, and +** where the order in which destructors are called is arbitrary. ** ** ^If an [sqlite3] object is destroyed while a transaction is open, ** the transaction is automatically rolled back. @@ -327,10 +370,10 @@ typedef int (*sqlite3_callback)(void*,int,char**, char**); ** The sqlite3_exec() interface is a convenience wrapper around ** [sqlite3_prepare_v2()], [sqlite3_step()], and [sqlite3_finalize()], ** that allows an application to run multiple statements of SQL -** without having to use a lot of C code. +** without having to use a lot of C code. ** ** ^The sqlite3_exec() interface runs zero or more UTF-8 encoded, -** semicolon-separate SQL statements passed into its 2nd argument, +** semicolon-separated SQL statements passed into its 2nd argument, ** in the context of the [database connection] passed in as its 1st ** argument. ^If the callback function of the 3rd argument to ** sqlite3_exec() is not NULL, then it is invoked for each result row @@ -363,11 +406,11 @@ typedef int (*sqlite3_callback)(void*,int,char**, char**); ** result row is NULL then the corresponding string pointer for the ** sqlite3_exec() callback is a NULL pointer. ^The 4th argument to the ** sqlite3_exec() callback is an array of pointers to strings where each -** entry represents the name of corresponding result column as obtained +** entry represents the name of a corresponding result column as obtained ** from [sqlite3_column_name()]. ** ** ^If the 2nd parameter to sqlite3_exec() is a NULL pointer, a pointer -** to an empty string, or a pointer that contains only whitespace and/or +** to an empty string, or a pointer that contains only whitespace and/or ** SQL comments, then no SQL statements are evaluated and the database ** is not changed. ** @@ -380,6 +423,8 @@ typedef int (*sqlite3_callback)(void*,int,char**, char**); ** the 1st parameter to sqlite3_exec() while sqlite3_exec() is running. ** <li> The application must not modify the SQL statement text passed into ** the 2nd parameter of sqlite3_exec() while sqlite3_exec() is running. +** <li> The application must not dereference the arrays or string pointers +** passed as the 3rd and 4th callback parameters after it returns. ** </ul> */ int sqlite3_exec( @@ -403,7 +448,7 @@ int sqlite3_exec( */ #define SQLITE_OK 0 /* Successful result */ /* beginning-of-error-codes */ -#define SQLITE_ERROR 1 /* SQL error or missing database */ +#define SQLITE_ERROR 1 /* Generic error */ #define SQLITE_INTERNAL 2 /* Internal logic error in SQLite */ #define SQLITE_PERM 3 /* Access permission denied */ #define SQLITE_ABORT 4 /* Callback routine requested an abort */ @@ -418,7 +463,7 @@ int sqlite3_exec( #define SQLITE_FULL 13 /* Insertion failed because database is full */ #define SQLITE_CANTOPEN 14 /* Unable to open the database file */ #define SQLITE_PROTOCOL 15 /* Database lock protocol error */ -#define SQLITE_EMPTY 16 /* Database is empty */ +#define SQLITE_EMPTY 16 /* Internal use only */ #define SQLITE_SCHEMA 17 /* The database schema changed */ #define SQLITE_TOOBIG 18 /* String or BLOB exceeds size limit */ #define SQLITE_CONSTRAINT 19 /* Abort due to constraint violation */ @@ -426,7 +471,7 @@ int sqlite3_exec( #define SQLITE_MISUSE 21 /* Library used incorrectly */ #define SQLITE_NOLFS 22 /* Uses OS features not supported on host */ #define SQLITE_AUTH 23 /* Authorization denied */ -#define SQLITE_FORMAT 24 /* Auxiliary database format error */ +#define SQLITE_FORMAT 24 /* Not used */ #define SQLITE_RANGE 25 /* 2nd parameter to sqlite3_bind out of range */ #define SQLITE_NOTADB 26 /* File opened that is not a database file */ #define SQLITE_NOTICE 27 /* Notifications from sqlite3_log() */ @@ -443,7 +488,8 @@ int sqlite3_exec( ** [result codes]. However, experience has shown that many of ** these result codes are too coarse-grained. They do not provide as ** much information about problems as programmers might like. In an effort to -** address this, newer versions of SQLite (version 3.3.8 and later) include +** address this, newer versions of SQLite (version 3.3.8 [dateof:3.3.8] +** and later) include ** support for additional result codes that provide more detailed information ** about errors. These [extended result codes] are enabled or disabled ** on a per database connection basis using the @@ -451,6 +497,12 @@ int sqlite3_exec( ** the most recent error can be obtained using ** [sqlite3_extended_errcode()]. */ +#define SQLITE_ERROR_MISSING_COLLSEQ (SQLITE_ERROR | (1<<8)) +#define SQLITE_ERROR_RETRY (SQLITE_ERROR | (2<<8)) +#define SQLITE_ERROR_SNAPSHOT (SQLITE_ERROR | (3<<8)) +#define SQLITE_ERROR_RESERVESIZE (SQLITE_ERROR | (4<<8)) +#define SQLITE_ERROR_KEY (SQLITE_ERROR | (5<<8)) +#define SQLITE_ERROR_UNABLE (SQLITE_ERROR | (6<<8)) #define SQLITE_IOERR_READ (SQLITE_IOERR | (1<<8)) #define SQLITE_IOERR_SHORT_READ (SQLITE_IOERR | (2<<8)) #define SQLITE_IOERR_WRITE (SQLITE_IOERR | (3<<8)) @@ -479,18 +531,34 @@ int sqlite3_exec( #define SQLITE_IOERR_CONVPATH (SQLITE_IOERR | (26<<8)) #define SQLITE_IOERR_VNODE (SQLITE_IOERR | (27<<8)) #define SQLITE_IOERR_AUTH (SQLITE_IOERR | (28<<8)) +#define SQLITE_IOERR_BEGIN_ATOMIC (SQLITE_IOERR | (29<<8)) +#define SQLITE_IOERR_COMMIT_ATOMIC (SQLITE_IOERR | (30<<8)) +#define SQLITE_IOERR_ROLLBACK_ATOMIC (SQLITE_IOERR | (31<<8)) +#define SQLITE_IOERR_DATA (SQLITE_IOERR | (32<<8)) +#define SQLITE_IOERR_CORRUPTFS (SQLITE_IOERR | (33<<8)) +#define SQLITE_IOERR_IN_PAGE (SQLITE_IOERR | (34<<8)) +#define SQLITE_IOERR_BADKEY (SQLITE_IOERR | (35<<8)) +#define SQLITE_IOERR_CODEC (SQLITE_IOERR | (36<<8)) #define SQLITE_LOCKED_SHAREDCACHE (SQLITE_LOCKED | (1<<8)) +#define SQLITE_LOCKED_VTAB (SQLITE_LOCKED | (2<<8)) #define SQLITE_BUSY_RECOVERY (SQLITE_BUSY | (1<<8)) #define SQLITE_BUSY_SNAPSHOT (SQLITE_BUSY | (2<<8)) +#define SQLITE_BUSY_TIMEOUT (SQLITE_BUSY | (3<<8)) #define SQLITE_CANTOPEN_NOTEMPDIR (SQLITE_CANTOPEN | (1<<8)) #define SQLITE_CANTOPEN_ISDIR (SQLITE_CANTOPEN | (2<<8)) #define SQLITE_CANTOPEN_FULLPATH (SQLITE_CANTOPEN | (3<<8)) #define SQLITE_CANTOPEN_CONVPATH (SQLITE_CANTOPEN | (4<<8)) +#define SQLITE_CANTOPEN_DIRTYWAL (SQLITE_CANTOPEN | (5<<8)) /* Not Used */ +#define SQLITE_CANTOPEN_SYMLINK (SQLITE_CANTOPEN | (6<<8)) #define SQLITE_CORRUPT_VTAB (SQLITE_CORRUPT | (1<<8)) +#define SQLITE_CORRUPT_SEQUENCE (SQLITE_CORRUPT | (2<<8)) +#define SQLITE_CORRUPT_INDEX (SQLITE_CORRUPT | (3<<8)) #define SQLITE_READONLY_RECOVERY (SQLITE_READONLY | (1<<8)) #define SQLITE_READONLY_CANTLOCK (SQLITE_READONLY | (2<<8)) #define SQLITE_READONLY_ROLLBACK (SQLITE_READONLY | (3<<8)) #define SQLITE_READONLY_DBMOVED (SQLITE_READONLY | (4<<8)) +#define SQLITE_READONLY_CANTINIT (SQLITE_READONLY | (5<<8)) +#define SQLITE_READONLY_DIRECTORY (SQLITE_READONLY | (6<<8)) #define SQLITE_ABORT_ROLLBACK (SQLITE_ABORT | (2<<8)) #define SQLITE_CONSTRAINT_CHECK (SQLITE_CONSTRAINT | (1<<8)) #define SQLITE_CONSTRAINT_COMMITHOOK (SQLITE_CONSTRAINT | (2<<8)) @@ -502,10 +570,15 @@ int sqlite3_exec( #define SQLITE_CONSTRAINT_UNIQUE (SQLITE_CONSTRAINT | (8<<8)) #define SQLITE_CONSTRAINT_VTAB (SQLITE_CONSTRAINT | (9<<8)) #define SQLITE_CONSTRAINT_ROWID (SQLITE_CONSTRAINT |(10<<8)) +#define SQLITE_CONSTRAINT_PINNED (SQLITE_CONSTRAINT |(11<<8)) +#define SQLITE_CONSTRAINT_DATATYPE (SQLITE_CONSTRAINT |(12<<8)) #define SQLITE_NOTICE_RECOVER_WAL (SQLITE_NOTICE | (1<<8)) #define SQLITE_NOTICE_RECOVER_ROLLBACK (SQLITE_NOTICE | (2<<8)) +#define SQLITE_NOTICE_RBU (SQLITE_NOTICE | (3<<8)) #define SQLITE_WARNING_AUTOINDEX (SQLITE_WARNING | (1<<8)) #define SQLITE_AUTH_USER (SQLITE_AUTH | (1<<8)) +#define SQLITE_OK_LOAD_PERMANENTLY (SQLITE_OK | (1<<8)) +#define SQLITE_OK_SYMLINK (SQLITE_OK | (2<<8)) /* internal use only */ /* ** CAPI3REF: Flags For File Open Operations @@ -513,6 +586,19 @@ int sqlite3_exec( ** These bit values are intended for use in the ** 3rd parameter to the [sqlite3_open_v2()] interface and ** in the 4th parameter to the [sqlite3_vfs.xOpen] method. +** +** Only those flags marked as "Ok for sqlite3_open_v2()" may be +** used as the third argument to the [sqlite3_open_v2()] interface. +** The other flags have historically been ignored by sqlite3_open_v2(), +** though future versions of SQLite might change so that an error is +** raised if any of the disallowed bits are passed into sqlite3_open_v2(). +** Applications should not depend on the historical behavior. +** +** Note in particular that passing the SQLITE_OPEN_EXCLUSIVE flag into +** [sqlite3_open_v2()] does *not* cause the underlying database file +** to be opened using O_EXCL. Passing SQLITE_OPEN_EXCLUSIVE into +** [sqlite3_open_v2()] has historically been a no-op and might become an +** error in future versions of SQLite. */ #define SQLITE_OPEN_READONLY 0x00000001 /* Ok for sqlite3_open_v2() */ #define SQLITE_OPEN_READWRITE 0x00000002 /* Ok for sqlite3_open_v2() */ @@ -528,14 +614,19 @@ int sqlite3_exec( #define SQLITE_OPEN_MAIN_JOURNAL 0x00000800 /* VFS only */ #define SQLITE_OPEN_TEMP_JOURNAL 0x00001000 /* VFS only */ #define SQLITE_OPEN_SUBJOURNAL 0x00002000 /* VFS only */ -#define SQLITE_OPEN_MASTER_JOURNAL 0x00004000 /* VFS only */ +#define SQLITE_OPEN_SUPER_JOURNAL 0x00004000 /* VFS only */ #define SQLITE_OPEN_NOMUTEX 0x00008000 /* Ok for sqlite3_open_v2() */ #define SQLITE_OPEN_FULLMUTEX 0x00010000 /* Ok for sqlite3_open_v2() */ #define SQLITE_OPEN_SHAREDCACHE 0x00020000 /* Ok for sqlite3_open_v2() */ #define SQLITE_OPEN_PRIVATECACHE 0x00040000 /* Ok for sqlite3_open_v2() */ #define SQLITE_OPEN_WAL 0x00080000 /* VFS only */ +#define SQLITE_OPEN_NOFOLLOW 0x01000000 /* Ok for sqlite3_open_v2() */ +#define SQLITE_OPEN_EXRESCODE 0x02000000 /* Extended result codes */ /* Reserved: 0x00F00000 */ +/* Legacy compatibility: */ +#define SQLITE_OPEN_MASTER_JOURNAL 0x00004000 /* VFS only */ + /* ** CAPI3REF: Device Characteristics @@ -560,10 +651,22 @@ int sqlite3_exec( ** file that were written at the application level might have changed ** and that adjacent bytes, even bytes within the same sector are ** guaranteed to be unchanged. The SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN -** flag indicate that a file cannot be deleted when open. The +** flag indicates that a file cannot be deleted when open. The ** SQLITE_IOCAP_IMMUTABLE flag indicates that the file is on ** read-only media and cannot be changed even by processes with ** elevated privileges. +** +** The SQLITE_IOCAP_BATCH_ATOMIC property means that the underlying +** filesystem supports doing multiple write operations atomically when those +** write operations are bracketed by [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] and +** [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE]. +** +** The SQLITE_IOCAP_SUBPAGE_READ property means that it is ok to read +** from the database file in amounts that are not a multiple of the +** page size and that do not begin at a page boundary. Without this +** property, SQLite is careful to only do full-page reads and write +** on aligned pages, with the one exception that it will do a sub-page +** read of the first page to access the database header. */ #define SQLITE_IOCAP_ATOMIC 0x00000001 #define SQLITE_IOCAP_ATOMIC512 0x00000002 @@ -579,19 +682,25 @@ int sqlite3_exec( #define SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN 0x00000800 #define SQLITE_IOCAP_POWERSAFE_OVERWRITE 0x00001000 #define SQLITE_IOCAP_IMMUTABLE 0x00002000 +#define SQLITE_IOCAP_BATCH_ATOMIC 0x00004000 +#define SQLITE_IOCAP_SUBPAGE_READ 0x00008000 /* ** CAPI3REF: File Locking Levels ** ** SQLite uses one of these integer values as the second ** argument to calls it makes to the xLock() and xUnlock() methods -** of an [sqlite3_io_methods] object. +** of an [sqlite3_io_methods] object. These values are ordered from +** least restrictive to most restrictive. +** +** The argument to xLock() is always SHARED or higher. The argument to +** xUnlock is either SHARED or NONE. */ -#define SQLITE_LOCK_NONE 0 -#define SQLITE_LOCK_SHARED 1 -#define SQLITE_LOCK_RESERVED 2 -#define SQLITE_LOCK_PENDING 3 -#define SQLITE_LOCK_EXCLUSIVE 4 +#define SQLITE_LOCK_NONE 0 /* xUnlock() only */ +#define SQLITE_LOCK_SHARED 1 /* xLock() or xUnlock() */ +#define SQLITE_LOCK_RESERVED 2 /* xLock() only */ +#define SQLITE_LOCK_PENDING 3 /* xLock() only */ +#define SQLITE_LOCK_EXCLUSIVE 4 /* xLock() only */ /* ** CAPI3REF: Synchronization Type Flags @@ -626,7 +735,7 @@ int sqlite3_exec( /* ** CAPI3REF: OS Interface Open File Handle ** -** An [sqlite3_file] object represents an open file in the +** An [sqlite3_file] object represents an open file in the ** [sqlite3_vfs | OS interface layer]. Individual OS interface ** implementations will ** want to subclass this object by appending additional fields @@ -648,7 +757,7 @@ struct sqlite3_file { ** This object defines the methods used to perform various operations ** against the open file represented by the [sqlite3_file] object. ** -** If the [sqlite3_vfs.xOpen] method sets the sqlite3_file.pMethods element +** If the [sqlite3_vfs.xOpen] method sets the sqlite3_file.pMethods element ** to a non-NULL pointer, then the sqlite3_io_methods.xClose method ** may be invoked even if the [sqlite3_vfs.xOpen] reported that it failed. The ** only way to prevent a call to xClose following a failed [sqlite3_vfs.xOpen] @@ -669,11 +778,18 @@ struct sqlite3_file { ** <li> [SQLITE_LOCK_PENDING], or ** <li> [SQLITE_LOCK_EXCLUSIVE]. ** </ul> -** xLock() increases the lock. xUnlock() decreases the lock. +** xLock() upgrades the database file lock. In other words, xLock() moves the +** database file lock in the direction NONE toward EXCLUSIVE. The argument to +** xLock() is always one of SHARED, RESERVED, PENDING, or EXCLUSIVE, never +** SQLITE_LOCK_NONE. If the database file lock is already at or above the +** requested lock, then the call to xLock() is a no-op. +** xUnlock() downgrades the database file lock to either SHARED or NONE. +** If the lock is already at or below the requested lock state, then the call +** to xUnlock() is a no-op. ** The xCheckReservedLock() method checks whether any database connection, ** either in this process or in some other process, is holding a RESERVED, -** PENDING, or EXCLUSIVE lock on the file. It returns true -** if such a lock exists and false otherwise. +** PENDING, or EXCLUSIVE lock on the file. It returns, via its output +** pointer parameter, true if such a lock exists and false otherwise. ** ** The xFileControl() method is a generic interface that allows custom ** VFS implementations to directly control an open file using the @@ -710,6 +826,11 @@ struct sqlite3_file { ** <li> [SQLITE_IOCAP_ATOMIC64K] ** <li> [SQLITE_IOCAP_SAFE_APPEND] ** <li> [SQLITE_IOCAP_SEQUENTIAL] +** <li> [SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN] +** <li> [SQLITE_IOCAP_POWERSAFE_OVERWRITE] +** <li> [SQLITE_IOCAP_IMMUTABLE] +** <li> [SQLITE_IOCAP_BATCH_ATOMIC] +** <li> [SQLITE_IOCAP_SUBPAGE_READ] ** </ul> ** ** The SQLITE_IOCAP_ATOMIC property means that all writes of @@ -770,9 +891,8 @@ struct sqlite3_io_methods { ** opcode causes the xFileControl method to write the current state of ** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED], ** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE]) -** into an integer that the pArg argument points to. This capability -** is used during testing and is only available when the SQLITE_TEST -** compile-time option is used. +** into an integer that the pArg argument points to. +** This capability is only available if SQLite is compiled with [SQLITE_DEBUG]. ** ** <li>[[SQLITE_FCNTL_SIZE_HINT]] ** The [SQLITE_FCNTL_SIZE_HINT] opcode is used by SQLite to give the VFS @@ -782,10 +902,19 @@ struct sqlite3_io_methods { ** file space based on this hint in order to help writes to the database ** file run faster. ** +** <li>[[SQLITE_FCNTL_SIZE_LIMIT]] +** The [SQLITE_FCNTL_SIZE_LIMIT] opcode is used by in-memory VFS that +** implements [sqlite3_deserialize()] to set an upper bound on the size +** of the in-memory database. The argument is a pointer to a [sqlite3_int64]. +** If the integer pointed to is negative, then it is filled in with the +** current limit. Otherwise the limit is set to the larger of the value +** of the integer pointed to and the current database size. The integer +** pointed to is set to the new limit. +** ** <li>[[SQLITE_FCNTL_CHUNK_SIZE]] ** The [SQLITE_FCNTL_CHUNK_SIZE] opcode is used to request that the VFS ** extends and truncates the database file in chunks of a size specified -** by the user. The fourth argument to [sqlite3_file_control()] should +** by the user. The fourth argument to [sqlite3_file_control()] should ** point to an integer (type int) containing the new chunk-size to use ** for the nominated database. Allocating database file space in large ** chunks (say 1MB at a time), may reduce file-system fragmentation and @@ -803,29 +932,29 @@ struct sqlite3_io_methods { ** connection. See also [SQLITE_FCNTL_FILE_POINTER]. ** ** <li>[[SQLITE_FCNTL_SYNC_OMITTED]] -** No longer in use. +** The SQLITE_FCNTL_SYNC_OMITTED file-control is no longer used. ** ** <li>[[SQLITE_FCNTL_SYNC]] ** The [SQLITE_FCNTL_SYNC] opcode is generated internally by SQLite and ** sent to the VFS immediately before the xSync method is invoked on a -** database file descriptor. Or, if the xSync method is not invoked -** because the user has configured SQLite with -** [PRAGMA synchronous | PRAGMA synchronous=OFF] it is invoked in place +** database file descriptor. Or, if the xSync method is not invoked +** because the user has configured SQLite with +** [PRAGMA synchronous | PRAGMA synchronous=OFF] it is invoked in place ** of the xSync method. In most cases, the pointer argument passed with ** this file-control is NULL. However, if the database file is being synced ** as part of a multi-database commit, the argument points to a nul-terminated -** string containing the transactions master-journal file name. VFSes that -** do not need this signal should silently ignore this opcode. Applications -** should not call [sqlite3_file_control()] with this opcode as doing so may -** disrupt the operation of the specialized VFSes that do require it. +** string containing the transactions super-journal file name. VFSes that +** do not need this signal should silently ignore this opcode. Applications +** should not call [sqlite3_file_control()] with this opcode as doing so may +** disrupt the operation of the specialized VFSes that do require it. ** ** <li>[[SQLITE_FCNTL_COMMIT_PHASETWO]] ** The [SQLITE_FCNTL_COMMIT_PHASETWO] opcode is generated internally by SQLite ** and sent to the VFS after a transaction has been committed immediately ** but before the database is unlocked. VFSes that do not need this signal ** should silently ignore this opcode. Applications should not call -** [sqlite3_file_control()] with this opcode as doing so may disrupt the -** operation of the specialized VFSes that do require it. +** [sqlite3_file_control()] with this opcode as doing so may disrupt the +** operation of the specialized VFSes that do require it. ** ** <li>[[SQLITE_FCNTL_WIN32_AV_RETRY]] ** ^The [SQLITE_FCNTL_WIN32_AV_RETRY] opcode is used to configure automatic @@ -838,7 +967,7 @@ struct sqlite3_io_methods { ** opcode allows these two values (10 retries and 25 milliseconds of delay) ** to be adjusted. The values are changed for all database connections ** within the same process. The argument is a pointer to an array of two -** integers where the first integer i the new retry count and the second +** integers where the first integer is the new retry count and the second ** integer is the delay. If either integer is negative, then the setting ** is not changed but instead the prior value of that setting is written ** into the array entry, allowing the current retry settings to be @@ -847,7 +976,8 @@ struct sqlite3_io_methods { ** <li>[[SQLITE_FCNTL_PERSIST_WAL]] ** ^The [SQLITE_FCNTL_PERSIST_WAL] opcode is used to set or query the ** persistent [WAL | Write Ahead Log] setting. By default, the auxiliary -** write ahead log and shared memory files used for transaction control +** write ahead log ([WAL file]) and shared memory +** files used for transaction control ** are automatically deleted when the latest connection to the database ** closes. Setting persistent WAL mode causes those files to persist after ** close. Persisting the files is useful when other processes that do not @@ -872,13 +1002,13 @@ struct sqlite3_io_methods { ** <li>[[SQLITE_FCNTL_OVERWRITE]] ** ^The [SQLITE_FCNTL_OVERWRITE] opcode is invoked by SQLite after opening ** a write transaction to indicate that, unless it is rolled back for some -** reason, the entire database file will be overwritten by the current +** reason, the entire database file will be overwritten by the current ** transaction. This is used by VACUUM operations. ** ** <li>[[SQLITE_FCNTL_VFSNAME]] ** ^The [SQLITE_FCNTL_VFSNAME] opcode can be used to obtain the names of -** all [VFSes] in the VFS stack. The names are of all VFS shims and the -** final bottom-level VFS are written into memory obtained from +** all [VFSes] in the VFS stack. The names of all VFS shims and the +** final bottom-level VFS are written into memory obtained from ** [sqlite3_malloc()] and the result is stored in the char* variable ** that the fourth parameter of [sqlite3_file_control()] points to. ** The caller is responsible for freeing the memory when done. As with @@ -891,13 +1021,13 @@ struct sqlite3_io_methods { ** ^The [SQLITE_FCNTL_VFS_POINTER] opcode finds a pointer to the top-level ** [VFSes] currently in use. ^(The argument X in ** sqlite3_file_control(db,SQLITE_FCNTL_VFS_POINTER,X) must be -** of type "[sqlite3_vfs] **". This opcodes will set *X +** of type "[sqlite3_vfs] **". This opcode will set *X ** to a pointer to the top-level VFS.)^ ** ^When there are multiple VFS shims in the stack, this opcode finds the ** upper-most shim only. ** ** <li>[[SQLITE_FCNTL_PRAGMA]] -** ^Whenever a [PRAGMA] statement is parsed, an [SQLITE_FCNTL_PRAGMA] +** ^Whenever a [PRAGMA] statement is parsed, an [SQLITE_FCNTL_PRAGMA] ** file control is sent to the open [sqlite3_file] object corresponding ** to the database file to which the pragma statement refers. ^The argument ** to the [SQLITE_FCNTL_PRAGMA] file control is an array of @@ -908,7 +1038,7 @@ struct sqlite3_io_methods { ** of the char** argument point to a string obtained from [sqlite3_mprintf()] ** or the equivalent and that string will become the result of the pragma or ** the error message if the pragma fails. ^If the -** [SQLITE_FCNTL_PRAGMA] file control returns [SQLITE_NOTFOUND], then normal +** [SQLITE_FCNTL_PRAGMA] file control returns [SQLITE_NOTFOUND], then normal ** [PRAGMA] processing continues. ^If the [SQLITE_FCNTL_PRAGMA] ** file control returns [SQLITE_OK], then the parser assumes that the ** VFS has handled the PRAGMA itself and the parser generates a no-op @@ -925,16 +1055,16 @@ struct sqlite3_io_methods { ** ^The [SQLITE_FCNTL_BUSYHANDLER] ** file-control may be invoked by SQLite on the database file handle ** shortly after it is opened in order to provide a custom VFS with access -** to the connections busy-handler callback. The argument is of type (void **) +** to the connection's busy-handler callback. The argument is of type (void**) ** - an array of two (void *) values. The first (void *) actually points -** to a function of type (int (*)(void *)). In order to invoke the connections +** to a function of type (int (*)(void *)). In order to invoke the connection's ** busy-handler, this function should be invoked with the second (void *) in ** the array as the only argument. If it returns non-zero, then the operation ** should be retried. If it returns zero, the custom VFS should abandon the ** current operation. ** ** <li>[[SQLITE_FCNTL_TEMPFILENAME]] -** ^Application can invoke the [SQLITE_FCNTL_TEMPFILENAME] file-control +** ^Applications can invoke the [SQLITE_FCNTL_TEMPFILENAME] file-control ** to have SQLite generate a ** temporary filename using the same algorithm that is followed to generate ** temporary filenames for TEMP tables and other internal uses. The @@ -948,7 +1078,7 @@ struct sqlite3_io_methods { ** The argument is a pointer to a value of type sqlite3_int64 that ** is an advisory maximum number of bytes in the file to memory map. The ** pointer is overwritten with the old value. The limit is not changed if -** the value originally pointed to is negative, and so the current limit +** the value originally pointed to is negative, and so the current limit ** can be queried by passing in a pointer to a negative number. This ** file-control is used internally to implement [PRAGMA mmap_size]. ** @@ -966,12 +1096,23 @@ struct sqlite3_io_methods { ** on whether or not the file has been renamed, moved, or deleted since it ** was first opened. ** +** <li>[[SQLITE_FCNTL_WIN32_GET_HANDLE]] +** The [SQLITE_FCNTL_WIN32_GET_HANDLE] opcode can be used to obtain the +** underlying native file handle associated with a file handle. This file +** control interprets its argument as a pointer to a native file handle and +** writes the resulting value there. +** ** <li>[[SQLITE_FCNTL_WIN32_SET_HANDLE]] ** The [SQLITE_FCNTL_WIN32_SET_HANDLE] opcode is used for debugging. This ** opcode causes the xFileControl method to swap the file handle with the one ** pointed to by the pArg argument. This capability is used during testing ** and only needs to be supported when SQLITE_TEST is defined. ** +** <li>[[SQLITE_FCNTL_NULL_IO]] +** The [SQLITE_FCNTL_NULL_IO] opcode sets the low-level file descriptor +** or file handle for the [sqlite3_file] object such that it will no longer +** read or write to the database file. +** ** <li>[[SQLITE_FCNTL_WAL_BLOCK]] ** The [SQLITE_FCNTL_WAL_BLOCK] is a signal to the VFS layer that it might ** be advantageous to block on the next WAL lock if the lock is not immediately @@ -986,7 +1127,117 @@ struct sqlite3_io_methods { ** <li>[[SQLITE_FCNTL_RBU]] ** The [SQLITE_FCNTL_RBU] opcode is implemented by the special VFS used by ** the RBU extension only. All other VFS should return SQLITE_NOTFOUND for -** this opcode. +** this opcode. +** +** <li>[[SQLITE_FCNTL_BEGIN_ATOMIC_WRITE]] +** If the [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] opcode returns SQLITE_OK, then +** the file descriptor is placed in "batch write mode", which +** means all subsequent write operations will be deferred and done +** atomically at the next [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE]. Systems +** that do not support batch atomic writes will return SQLITE_NOTFOUND. +** ^Following a successful SQLITE_FCNTL_BEGIN_ATOMIC_WRITE and prior to +** the closing [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE] or +** [SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE], SQLite will make +** no VFS interface calls on the same [sqlite3_file] file descriptor +** except for calls to the xWrite method and the xFileControl method +** with [SQLITE_FCNTL_SIZE_HINT]. +** +** <li>[[SQLITE_FCNTL_COMMIT_ATOMIC_WRITE]] +** The [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE] opcode causes all write +** operations since the previous successful call to +** [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] to be performed atomically. +** This file control returns [SQLITE_OK] if and only if the writes were +** all performed successfully and have been committed to persistent storage. +** ^Regardless of whether or not it is successful, this file control takes +** the file descriptor out of batch write mode so that all subsequent +** write operations are independent. +** ^SQLite will never invoke SQLITE_FCNTL_COMMIT_ATOMIC_WRITE without +** a prior successful call to [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE]. +** +** <li>[[SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE]] +** The [SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE] opcode causes all write +** operations since the previous successful call to +** [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] to be rolled back. +** ^This file control takes the file descriptor out of batch write mode +** so that all subsequent write operations are independent. +** ^SQLite will never invoke SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE without +** a prior successful call to [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE]. +** +** <li>[[SQLITE_FCNTL_LOCK_TIMEOUT]] +** The [SQLITE_FCNTL_LOCK_TIMEOUT] opcode is used to configure a VFS +** to block for up to M milliseconds before failing when attempting to +** obtain a file lock using the xLock or xShmLock methods of the VFS. +** The parameter is a pointer to a 32-bit signed integer that contains +** the value that M is to be set to. Before returning, the 32-bit signed +** integer is overwritten with the previous value of M. +** +** <li>[[SQLITE_FCNTL_BLOCK_ON_CONNECT]] +** The [SQLITE_FCNTL_BLOCK_ON_CONNECT] opcode is used to configure the +** VFS to block when taking a SHARED lock to connect to a wal mode database. +** This is used to implement the functionality associated with +** SQLITE_SETLK_BLOCK_ON_CONNECT. +** +** <li>[[SQLITE_FCNTL_DATA_VERSION]] +** The [SQLITE_FCNTL_DATA_VERSION] opcode is used to detect changes to +** a database file. The argument is a pointer to a 32-bit unsigned integer. +** The "data version" for the pager is written into the pointer. The +** "data version" changes whenever any change occurs to the corresponding +** database file, either through SQL statements on the same database +** connection or through transactions committed by separate database +** connections possibly in other processes. The [sqlite3_total_changes()] +** interface can be used to find if any database on the connection has changed, +** but that interface responds to changes on TEMP as well as MAIN and does +** not provide a mechanism to detect changes to MAIN only. Also, the +** [sqlite3_total_changes()] interface responds to internal changes only and +** omits changes made by other database connections. The +** [PRAGMA data_version] command provides a mechanism to detect changes to +** a single attached database that occur due to other database connections, +** but omits changes implemented by the database connection on which it is +** called. This file control is the only mechanism to detect changes that +** happen either internally or externally and that are associated with +** a particular attached database. +** +** <li>[[SQLITE_FCNTL_CKPT_START]] +** The [SQLITE_FCNTL_CKPT_START] opcode is invoked from within a checkpoint +** in wal mode before the client starts to copy pages from the wal +** file to the database file. +** +** <li>[[SQLITE_FCNTL_CKPT_DONE]] +** The [SQLITE_FCNTL_CKPT_DONE] opcode is invoked from within a checkpoint +** in wal mode after the client has finished copying pages from the wal +** file to the database file, but before the *-shm file is updated to +** record the fact that the pages have been checkpointed. +** +** <li>[[SQLITE_FCNTL_EXTERNAL_READER]] +** The EXPERIMENTAL [SQLITE_FCNTL_EXTERNAL_READER] opcode is used to detect +** whether or not there is a database client in another process with a wal-mode +** transaction open on the database or not. It is only available on unix. The +** (void*) argument passed with this file-control should be a pointer to a +** value of type (int). The integer value is set to 1 if the database is a wal +** mode database and there exists at least one client in another process that +** currently has an SQL transaction open on the database. It is set to 0 if +** the database is not a wal-mode db, or if there is no such connection in any +** other process. This opcode cannot be used to detect transactions opened +** by clients within the current process, only within other processes. +** +** <li>[[SQLITE_FCNTL_CKSM_FILE]] +** The [SQLITE_FCNTL_CKSM_FILE] opcode is for use internally by the +** [checksum VFS shim] only. +** +** <li>[[SQLITE_FCNTL_RESET_CACHE]] +** If there is currently no transaction open on the database, and the +** database is not a temp db, then the [SQLITE_FCNTL_RESET_CACHE] file-control +** purges the contents of the in-memory page cache. If there is an open +** transaction, or if the db is a temp-db, this opcode is a no-op, not an error. +** +** <li>[[SQLITE_FCNTL_FILESTAT]] +** The [SQLITE_FCNTL_FILESTAT] opcode returns low-level diagnostic information +** about the [sqlite3_file] objects used access the database and journal files +** for the given schema. The fourth parameter to [sqlite3_file_control()] +** should be an initialized [sqlite3_str] pointer. JSON text describing +** various aspects of the sqlite3_file object is appended to the sqlite3_str. +** The SQLITE_FCNTL_FILESTAT opcode is usually a no-op, unless compile-time +** options are used to enable it. ** </ul> */ #define SQLITE_FCNTL_LOCKSTATE 1 @@ -1016,6 +1267,23 @@ struct sqlite3_io_methods { #define SQLITE_FCNTL_RBU 26 #define SQLITE_FCNTL_VFS_POINTER 27 #define SQLITE_FCNTL_JOURNAL_POINTER 28 +#define SQLITE_FCNTL_WIN32_GET_HANDLE 29 +#define SQLITE_FCNTL_PDB 30 +#define SQLITE_FCNTL_BEGIN_ATOMIC_WRITE 31 +#define SQLITE_FCNTL_COMMIT_ATOMIC_WRITE 32 +#define SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE 33 +#define SQLITE_FCNTL_LOCK_TIMEOUT 34 +#define SQLITE_FCNTL_DATA_VERSION 35 +#define SQLITE_FCNTL_SIZE_LIMIT 36 +#define SQLITE_FCNTL_CKPT_DONE 37 +#define SQLITE_FCNTL_RESERVE_BYTES 38 +#define SQLITE_FCNTL_CKPT_START 39 +#define SQLITE_FCNTL_EXTERNAL_READER 40 +#define SQLITE_FCNTL_CKSM_FILE 41 +#define SQLITE_FCNTL_RESET_CACHE 42 +#define SQLITE_FCNTL_NULL_IO 43 +#define SQLITE_FCNTL_BLOCK_ON_CONNECT 44 +#define SQLITE_FCNTL_FILESTAT 45 /* deprecated names */ #define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE @@ -1035,6 +1303,36 @@ struct sqlite3_io_methods { */ typedef struct sqlite3_mutex sqlite3_mutex; +/* +** CAPI3REF: Loadable Extension Thunk +** +** A pointer to the opaque sqlite3_api_routines structure is passed as +** the third parameter to entry points of [loadable extensions]. This +** structure must be typedefed in order to work around compiler warnings +** on some platforms. +*/ +typedef struct sqlite3_api_routines sqlite3_api_routines; + +/* +** CAPI3REF: File Name +** +** Type [sqlite3_filename] is used by SQLite to pass filenames to the +** xOpen method of a [VFS]. It may be cast to (const char*) and treated +** as a normal, nul-terminated, UTF-8 buffer containing the filename, but +** may also be passed to special APIs such as: +** +** <ul> +** <li> sqlite3_filename_database() +** <li> sqlite3_filename_journal() +** <li> sqlite3_filename_wal() +** <li> sqlite3_uri_parameter() +** <li> sqlite3_uri_boolean() +** <li> sqlite3_uri_int64() +** <li> sqlite3_uri_key() +** </ul> +*/ +typedef const char *sqlite3_filename; + /* ** CAPI3REF: OS Interface Object ** @@ -1043,12 +1341,18 @@ typedef struct sqlite3_mutex sqlite3_mutex; ** in the name of the object stands for "virtual file system". See ** the [VFS | VFS documentation] for further information. ** -** The value of the iVersion field is initially 1 but may be larger in -** future versions of SQLite. Additional fields may be appended to this -** object when the iVersion value is increased. Note that the structure -** of the sqlite3_vfs object changes in the transaction between -** SQLite version 3.5.9 and 3.6.0 and yet the iVersion field was not -** modified. +** The VFS interface is sometimes extended by adding new methods onto +** the end. Each time such an extension occurs, the iVersion field +** is incremented. The iVersion value started out as 1 in +** SQLite [version 3.5.0] on [dateof:3.5.0], then increased to 2 +** with SQLite [version 3.7.0] on [dateof:3.7.0], and then increased +** to 3 with SQLite [version 3.7.6] on [dateof:3.7.6]. Additional fields +** may be appended to the sqlite3_vfs object and the iVersion value +** may increase again in future versions of SQLite. +** Note that due to an oversight, the structure +** of the sqlite3_vfs object changed in the transition from +** SQLite [version 3.5.9] to [version 3.6.0] on [dateof:3.6.0] +** and yet the iVersion field was not increased. ** ** The szOsFile field is the size of the subclassed [sqlite3_file] ** structure used by this VFS. mxPathname is the maximum length of @@ -1083,14 +1387,14 @@ typedef struct sqlite3_mutex sqlite3_mutex; ** the [sqlite3_file] can safely store a pointer to the ** filename if it needs to remember the filename for some reason. ** If the zFilename parameter to xOpen is a NULL pointer then xOpen -** must invent its own temporary name for the file. ^Whenever the +** must invent its own temporary name for the file. ^Whenever the ** xFilename parameter is NULL it will also be the case that the ** flags parameter will include [SQLITE_OPEN_DELETEONCLOSE]. ** ** The flags argument to xOpen() includes all bits set in ** the flags argument to [sqlite3_open_v2()]. Or if [sqlite3_open()] ** or [sqlite3_open16()] is used, then flags includes at least -** [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]. +** [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]. ** If xOpen() opens a file read-only then it sets *pOutFlags to ** include [SQLITE_OPEN_READONLY]. Other bits in *pOutFlags may be set. ** @@ -1104,7 +1408,7 @@ typedef struct sqlite3_mutex sqlite3_mutex; ** <li> [SQLITE_OPEN_TEMP_JOURNAL] ** <li> [SQLITE_OPEN_TRANSIENT_DB] ** <li> [SQLITE_OPEN_SUBJOURNAL] -** <li> [SQLITE_OPEN_MASTER_JOURNAL] +** <li> [SQLITE_OPEN_SUPER_JOURNAL] ** <li> [SQLITE_OPEN_WAL] ** </ul>)^ ** @@ -1132,14 +1436,14 @@ typedef struct sqlite3_mutex sqlite3_mutex; ** ^The [SQLITE_OPEN_EXCLUSIVE] flag is always used in conjunction ** with the [SQLITE_OPEN_CREATE] flag, which are both directly ** analogous to the O_EXCL and O_CREAT flags of the POSIX open() -** API. The SQLITE_OPEN_EXCLUSIVE flag, when paired with the +** API. The SQLITE_OPEN_EXCLUSIVE flag, when paired with the ** SQLITE_OPEN_CREATE, is used to indicate that file should always ** be created, and that it is an error if it already exists. -** It is <i>not</i> used to indicate the file should be opened +** It is <i>not</i> used to indicate the file should be opened ** for exclusive access. ** ** ^At least szOsFile bytes of memory are allocated by SQLite -** to hold the [sqlite3_file] structure passed as the third +** to hold the [sqlite3_file] structure passed as the third ** argument to xOpen. The xOpen method does not have to ** allocate the structure; it should just fill it in. Note that ** the xOpen method must set the sqlite3_file.pMethods to either @@ -1152,8 +1456,14 @@ typedef struct sqlite3_mutex sqlite3_mutex; ** ^The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS] ** to test for the existence of a file, or [SQLITE_ACCESS_READWRITE] to ** test whether a file is readable and writable, or [SQLITE_ACCESS_READ] -** to test whether a file is at least readable. The file can be a -** directory. +** to test whether a file is at least readable. The SQLITE_ACCESS_READ +** flag is never actually used and is not implemented in the built-in +** VFSes of SQLite. The file is named by the second argument and can be a +** directory. The xAccess method returns [SQLITE_OK] on success or some +** non-zero error code if there is an I/O error or if the name of +** the file given in the second argument is illegal. If SQLITE_OK +** is returned, then non-zero or zero is written into *pResOut to indicate +** whether or not the file is accessible. ** ** ^SQLite will always allocate at least mxPathname+1 bytes for the ** output buffer xFullPathname. The exact size of the output buffer @@ -1173,16 +1483,16 @@ typedef struct sqlite3_mutex sqlite3_mutex; ** method returns a Julian Day Number for the current date and time as ** a floating point value. ** ^The xCurrentTimeInt64() method returns, as an integer, the Julian -** Day Number multiplied by 86400000 (the number of milliseconds in -** a 24-hour day). +** Day Number multiplied by 86400000 (the number of milliseconds in +** a 24-hour day). ** ^SQLite will use the xCurrentTimeInt64() method to get the current -** date and time if that method is available (if iVersion is 2 or +** date and time if that method is available (if iVersion is 2 or ** greater and the function pointer is not NULL) and will fall back ** to xCurrentTime() if xCurrentTimeInt64() is unavailable. ** ** ^The xSetSystemCall(), xGetSystemCall(), and xNestSystemCall() interfaces ** are not used by the SQLite core. These optional interfaces are provided -** by some VFSes to facilitate testing of the VFS code. By overriding +** by some VFSes to facilitate testing of the VFS code. By overriding ** system calls with functions under its control, a test program can ** simulate faults and error conditions that would otherwise be difficult ** or impossible to induce. The set of system calls that can be overridden @@ -1201,7 +1511,7 @@ struct sqlite3_vfs { sqlite3_vfs *pNext; /* Next registered VFS */ const char *zName; /* Name of this virtual file system */ void *pAppData; /* Pointer to application-specific data */ - int (*xOpen)(sqlite3_vfs*, const char *zName, sqlite3_file*, + int (*xOpen)(sqlite3_vfs*, sqlite3_filename zName, sqlite3_file*, int flags, int *pOutFlags); int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir); int (*xAccess)(sqlite3_vfs*, const char *zName, int flags, int *pResOut); @@ -1228,8 +1538,8 @@ struct sqlite3_vfs { const char *(*xNextSystemCall)(sqlite3_vfs*, const char *zName); /* ** The methods above are in versions 1 through 3 of the sqlite_vfs object. - ** New fields may be appended in figure versions. The iVersion - ** value will increment whenever this happens. + ** New fields may be appended in future versions. The iVersion + ** value will increment whenever this happens. */ }; @@ -1273,7 +1583,7 @@ struct sqlite3_vfs { ** </ul> ** ** When unlocking, the same SHARED or EXCLUSIVE flag must be supplied as -** was given on the corresponding lock. +** was given on the corresponding lock. ** ** The xShmLock method can transition between unlocked and SHARED or ** between unlocked and EXCLUSIVE. It cannot transition between SHARED @@ -1336,7 +1646,7 @@ struct sqlite3_vfs { ** SQLite interfaces so that an application usually does not need to ** invoke sqlite3_initialize() directly. For example, [sqlite3_open()] ** calls sqlite3_initialize() so the SQLite library will be automatically -** initialized when [sqlite3_open()] is called if it has not be initialized +** initialized when [sqlite3_open()] is called if it has not been initialized ** already. ^However, if SQLite is compiled with the [SQLITE_OMIT_AUTOINIT] ** compile-time option, then the automatic calls to sqlite3_initialize() ** are omitted and the application must call sqlite3_initialize() directly @@ -1388,20 +1698,23 @@ int sqlite3_os_end(void); ** must ensure that no other SQLite interfaces are invoked by other ** threads while sqlite3_config() is running.</b> ** -** The sqlite3_config() interface -** may only be invoked prior to library initialization using -** [sqlite3_initialize()] or after shutdown by [sqlite3_shutdown()]. -** ^If sqlite3_config() is called after [sqlite3_initialize()] and before -** [sqlite3_shutdown()] then it will return SQLITE_MISUSE. -** Note, however, that ^sqlite3_config() can be called as part of the -** implementation of an application-defined [sqlite3_os_init()]. -** ** The first argument to sqlite3_config() is an integer ** [configuration option] that determines ** what property of SQLite is to be configured. Subsequent arguments ** vary depending on the [configuration option] ** in the first argument. ** +** For most configuration options, the sqlite3_config() interface +** may only be invoked prior to library initialization using +** [sqlite3_initialize()] or after shutdown by [sqlite3_shutdown()]. +** The exceptional configuration options that may be invoked at any time +** are called "anytime configuration options". +** ^If sqlite3_config() is called after [sqlite3_initialize()] and before +** [sqlite3_shutdown()] with a first argument that is not an anytime +** configuration option, then the sqlite3_config() call will return SQLITE_MISUSE. +** Note, however, that ^sqlite3_config() can be called as part of the +** implementation of an application-defined [sqlite3_os_init()]. +** ** ^When a configuration option is set, sqlite3_config() returns [SQLITE_OK]. ** ^If the option is unknown or SQLite is unable to set the option ** then this routine returns a non-zero [error code]. @@ -1418,7 +1731,7 @@ int sqlite3_config(int, ...); ** [database connection] (specified in the first argument). ** ** The second argument to sqlite3_db_config(D,V,...) is the -** [SQLITE_DBCONFIG_LOOKASIDE | configuration verb] - an integer code +** [SQLITE_DBCONFIG_LOOKASIDE | configuration verb] - an integer code ** that indicates what aspect of the [database connection] is being configured. ** Subsequent arguments vary depending on the configuration verb. ** @@ -1436,7 +1749,7 @@ int sqlite3_db_config(sqlite3*, int op, ...); ** This object is used in only one place in the SQLite interface. ** A pointer to an instance of this object is the argument to ** [sqlite3_config()] when the configuration option is -** [SQLITE_CONFIG_MALLOC] or [SQLITE_CONFIG_GETMALLOC]. +** [SQLITE_CONFIG_MALLOC] or [SQLITE_CONFIG_GETMALLOC]. ** By creating an instance of this object ** and passing it to [sqlite3_config]([SQLITE_CONFIG_MALLOC]) ** during configuration, an application can specify an alternative @@ -1466,17 +1779,17 @@ int sqlite3_db_config(sqlite3*, int op, ...); ** allocators round up memory allocations at least to the next multiple ** of 8. Some allocators round up to a larger multiple or to a power of 2. ** Every memory allocation request coming in through [sqlite3_malloc()] -** or [sqlite3_realloc()] first calls xRoundup. If xRoundup returns 0, +** or [sqlite3_realloc()] first calls xRoundup. If xRoundup returns 0, ** that causes the corresponding memory allocation to fail. ** ** The xInit method initializes the memory allocator. For example, -** it might allocate any require mutexes or initialize internal data +** it might allocate any required mutexes or initialize internal data ** structures. The xShutdown method is invoked (indirectly) by ** [sqlite3_shutdown()] and should deallocate any resources acquired ** by xInit. The pAppData pointer is used as the only parameter to ** xInit and xShutdown. ** -** SQLite holds the [SQLITE_MUTEX_STATIC_MASTER] mutex when it invokes +** SQLite holds the [SQLITE_MUTEX_STATIC_MAIN] mutex when it invokes ** the xInit method, so the xInit method need not be threadsafe. The ** xShutdown method is only called from [sqlite3_shutdown()] so it does ** not need to be threadsafe either. For all other methods, SQLite @@ -1509,6 +1822,23 @@ struct sqlite3_mem_methods { ** These constants are the available integer configuration options that ** can be passed as the first argument to the [sqlite3_config()] interface. ** +** Most of the configuration options for sqlite3_config() +** will only work if invoked prior to [sqlite3_initialize()] or after +** [sqlite3_shutdown()]. The few exceptions to this rule are called +** "anytime configuration options". +** ^Calling [sqlite3_config()] with a first argument that is not an +** anytime configuration option in between calls to [sqlite3_initialize()] and +** [sqlite3_shutdown()] is a no-op that returns SQLITE_MISUSE. +** +** The set of anytime configuration options can change (by insertions +** and/or deletions) from one release of SQLite to the next. +** As of SQLite version 3.42.0, the complete set of anytime configuration +** options is: +** <ul> +** <li> SQLITE_CONFIG_LOG +** <li> SQLITE_CONFIG_PCACHE_HDRSZ +** </ul> +** ** New configuration options may be added in future releases of SQLite. ** Existing configuration options might be discontinued. Applications ** should check the return code from [sqlite3_config()] to make sure that @@ -1524,7 +1854,7 @@ struct sqlite3_mem_methods { ** by a single thread. ^If SQLite is compiled with ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then ** it is not possible to change the [threading mode] from its default -** value of Single-thread and so [sqlite3_config()] will return +** value of Single-thread and so [sqlite3_config()] will return ** [SQLITE_ERROR] if called with the SQLITE_CONFIG_SINGLETHREAD ** configuration option.</dd> ** @@ -1559,7 +1889,7 @@ struct sqlite3_mem_methods { ** SQLITE_CONFIG_SERIALIZED configuration option.</dd> ** ** [[SQLITE_CONFIG_MALLOC]] <dt>SQLITE_CONFIG_MALLOC</dt> -** <dd> ^(The SQLITE_CONFIG_MALLOC option takes a single argument which is +** <dd> ^(The SQLITE_CONFIG_MALLOC option takes a single argument which is ** a pointer to an instance of the [sqlite3_mem_methods] structure. ** The argument specifies ** alternative low-level memory allocation routines to be used in place of @@ -1573,15 +1903,26 @@ struct sqlite3_mem_methods { ** The [sqlite3_mem_methods] ** structure is filled with the currently defined memory allocation routines.)^ ** This option can be used to overload the default memory allocation -** routines with a wrapper that simulations memory allocation failure or +** routines with a wrapper that simulates memory allocation failure or ** tracks memory usage, for example. </dd> ** +** [[SQLITE_CONFIG_SMALL_MALLOC]] <dt>SQLITE_CONFIG_SMALL_MALLOC</dt> +** <dd> ^The SQLITE_CONFIG_SMALL_MALLOC option takes a single argument of +** type int, interpreted as a boolean, which if true provides a hint to +** SQLite that it should avoid large memory allocations if possible. +** SQLite will run faster if it is free to make large memory allocations, +** but some applications might prefer to run slower in exchange for +** guarantees about memory fragmentation that are possible if large +** allocations are avoided. This hint is normally off. +** </dd> +** ** [[SQLITE_CONFIG_MEMSTATUS]] <dt>SQLITE_CONFIG_MEMSTATUS</dt> -** <dd> ^The SQLITE_CONFIG_MEMSTATUS option takes single argument of type int, +** <dd> ^The SQLITE_CONFIG_MEMSTATUS option takes a single argument of type int, ** interpreted as a boolean, which enables or disables the collection of ** memory allocation statistics. ^(When memory allocation statistics are ** disabled, the following SQLite interfaces become non-operational: ** <ul> +** <li> [sqlite3_hard_heap_limit64()] ** <li> [sqlite3_memory_used()] ** <li> [sqlite3_memory_highwater()] ** <li> [sqlite3_soft_heap_limit64()] @@ -1593,32 +1934,14 @@ struct sqlite3_mem_methods { ** </dd> ** ** [[SQLITE_CONFIG_SCRATCH]] <dt>SQLITE_CONFIG_SCRATCH</dt> -** <dd> ^The SQLITE_CONFIG_SCRATCH option specifies a static memory buffer -** that SQLite can use for scratch memory. ^(There are three arguments -** to SQLITE_CONFIG_SCRATCH: A pointer an 8-byte -** aligned memory buffer from which the scratch allocations will be -** drawn, the size of each scratch allocation (sz), -** and the maximum number of scratch allocations (N).)^ -** The first argument must be a pointer to an 8-byte aligned buffer -** of at least sz*N bytes of memory. -** ^SQLite will not use more than one scratch buffers per thread. -** ^SQLite will never request a scratch buffer that is more than 6 -** times the database page size. -** ^If SQLite needs needs additional -** scratch memory beyond what is provided by this configuration option, then -** [sqlite3_malloc()] will be used to obtain the memory needed.<p> -** ^When the application provides any amount of scratch memory using -** SQLITE_CONFIG_SCRATCH, SQLite avoids unnecessary large -** [sqlite3_malloc|heap allocations]. -** This can help [Robson proof|prevent memory allocation failures] due to heap -** fragmentation in low-memory embedded systems. +** <dd> The SQLITE_CONFIG_SCRATCH option is no longer used. ** </dd> ** ** [[SQLITE_CONFIG_PAGECACHE]] <dt>SQLITE_CONFIG_PAGECACHE</dt> ** <dd> ^The SQLITE_CONFIG_PAGECACHE option specifies a memory pool ** that SQLite can use for the database page cache with the default page -** cache implementation. -** This configuration option is a no-op if an application-define page +** cache implementation. +** This configuration option is a no-op if an application-defined page ** cache implementation is loaded using the [SQLITE_CONFIG_PCACHE2]. ** ^There are three arguments to SQLITE_CONFIG_PAGECACHE: A pointer to ** 8-byte aligned memory (pMem), the size of each page cache line (sz), @@ -1639,16 +1962,15 @@ struct sqlite3_mem_methods { ** ^If pMem is NULL and N is non-zero, then each database connection ** does an initial bulk allocation for page cache memory ** from [sqlite3_malloc()] sufficient for N cache lines if N is positive or -** of -1024*N bytes if N is negative, . ^If additional +** of -1024*N bytes if N is negative. ^If additional ** page cache memory is needed beyond what is provided by the initial ** allocation, then SQLite goes to [sqlite3_malloc()] separately for each ** additional cache line. </dd> ** ** [[SQLITE_CONFIG_HEAP]] <dt>SQLITE_CONFIG_HEAP</dt> -** <dd> ^The SQLITE_CONFIG_HEAP option specifies a static memory buffer +** <dd> ^The SQLITE_CONFIG_HEAP option specifies a static memory buffer ** that SQLite will use for all of its dynamic memory allocation needs -** beyond those provided for by [SQLITE_CONFIG_SCRATCH] and -** [SQLITE_CONFIG_PAGECACHE]. +** beyond those provided for by [SQLITE_CONFIG_PAGECACHE]. ** ^The SQLITE_CONFIG_HEAP option is only available if SQLite is compiled ** with either [SQLITE_ENABLE_MEMSYS3] or [SQLITE_ENABLE_MEMSYS5] and returns ** [SQLITE_ERROR] if invoked otherwise. @@ -1669,7 +1991,7 @@ struct sqlite3_mem_methods { ** <dd> ^(The SQLITE_CONFIG_MUTEX option takes a single argument which is a ** pointer to an instance of the [sqlite3_mutex_methods] structure. ** The argument specifies alternative low-level mutex routines to be used -** in place the mutex routines built into SQLite.)^ ^SQLite makes a copy of +** in place of the mutex routines built into SQLite.)^ ^SQLite makes a copy of ** the content of the [sqlite3_mutex_methods] structure before the call to ** [sqlite3_config()] returns. ^If SQLite is compiled with ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then @@ -1692,30 +2014,33 @@ struct sqlite3_mem_methods { ** ** [[SQLITE_CONFIG_LOOKASIDE]] <dt>SQLITE_CONFIG_LOOKASIDE</dt> ** <dd> ^(The SQLITE_CONFIG_LOOKASIDE option takes two arguments that determine -** the default size of lookaside memory on each [database connection]. +** the default size of [lookaside memory] on each [database connection]. ** The first argument is the -** size of each lookaside buffer slot and the second is the number of -** slots allocated to each database connection.)^ ^(SQLITE_CONFIG_LOOKASIDE -** sets the <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE] -** option to [sqlite3_db_config()] can be used to change the lookaside -** configuration on individual connections.)^ </dd> +** size of each lookaside buffer slot ("sz") and the second is the number of +** slots allocated to each database connection ("cnt").)^ +** ^(SQLITE_CONFIG_LOOKASIDE sets the <i>default</i> lookaside size. +** The [SQLITE_DBCONFIG_LOOKASIDE] option to [sqlite3_db_config()] can +** be used to change the lookaside configuration on individual connections.)^ +** The [-DSQLITE_DEFAULT_LOOKASIDE] option can be used to change the +** default lookaside configuration at compile-time. +** </dd> ** ** [[SQLITE_CONFIG_PCACHE2]] <dt>SQLITE_CONFIG_PCACHE2</dt> -** <dd> ^(The SQLITE_CONFIG_PCACHE2 option takes a single argument which is +** <dd> ^(The SQLITE_CONFIG_PCACHE2 option takes a single argument which is ** a pointer to an [sqlite3_pcache_methods2] object. This object specifies ** the interface to a custom page cache implementation.)^ ** ^SQLite makes a copy of the [sqlite3_pcache_methods2] object.</dd> ** ** [[SQLITE_CONFIG_GETPCACHE2]] <dt>SQLITE_CONFIG_GETPCACHE2</dt> ** <dd> ^(The SQLITE_CONFIG_GETPCACHE2 option takes a single argument which -** is a pointer to an [sqlite3_pcache_methods2] object. SQLite copies of +** is a pointer to an [sqlite3_pcache_methods2] object. SQLite copies off ** the current page cache implementation into that object.)^ </dd> ** ** [[SQLITE_CONFIG_LOG]] <dt>SQLITE_CONFIG_LOG</dt> ** <dd> The SQLITE_CONFIG_LOG option is used to configure the SQLite ** global [error log]. ** (^The SQLITE_CONFIG_LOG option takes two arguments: a pointer to a -** function with a call signature of void(*)(void*,int,const char*), +** function with a call signature of void(*)(void*,int,const char*), ** and a pointer to void. ^If the function pointer is not NULL, it is ** invoked by [sqlite3_log()] to process each logging event. ^If the ** function pointer is NULL, the [sqlite3_log()] interface becomes a no-op. @@ -1725,7 +2050,7 @@ struct sqlite3_mem_methods { ** the logger function is a copy of the first parameter to the corresponding ** [sqlite3_log()] call and is intended to be a [result code] or an ** [extended result code]. ^The third parameter passed to the logger is -** log message after formatting via [sqlite3_snprintf()]. +** a log message after formatting via [sqlite3_snprintf()]. ** The SQLite logging interface is not reentrant; the logger function ** supplied by the application must not invoke any SQLite interface. ** In a multi-threaded application, the application-defined logger @@ -1820,39 +2145,109 @@ struct sqlite3_mem_methods { ** is enabled (using the [PRAGMA threads] command) and the amount of content ** to be sorted exceeds the page size times the minimum of the ** [PRAGMA cache_size] setting and this value. +** +** [[SQLITE_CONFIG_STMTJRNL_SPILL]] +** <dt>SQLITE_CONFIG_STMTJRNL_SPILL +** <dd>^The SQLITE_CONFIG_STMTJRNL_SPILL option takes a single parameter which +** becomes the [statement journal] spill-to-disk threshold. +** [Statement journals] are held in memory until their size (in bytes) +** exceeds this threshold, at which point they are written to disk. +** Or if the threshold is -1, statement journals are always held +** exclusively in memory. +** Since many statement journals never become large, setting the spill +** threshold to a value such as 64KiB can greatly reduce the amount of +** I/O required to support statement rollback. +** The default value for this setting is controlled by the +** [SQLITE_STMTJRNL_SPILL] compile-time option. +** +** [[SQLITE_CONFIG_SORTERREF_SIZE]] +** <dt>SQLITE_CONFIG_SORTERREF_SIZE +** <dd>The SQLITE_CONFIG_SORTERREF_SIZE option accepts a single parameter +** of type (int) - the new value of the sorter-reference size threshold. +** Usually, when SQLite uses an external sort to order records according +** to an ORDER BY clause, all fields required by the caller are present in the +** sorted records. However, if SQLite determines based on the declared type +** of a table column that its values are likely to be very large - larger +** than the configured sorter-reference size threshold - then a reference +** is stored in each sorted record and the required column values loaded +** from the database as records are returned in sorted order. The default +** value for this option is to never use this optimization. Specifying a +** negative value for this option restores the default behavior. +** This option is only available if SQLite is compiled with the +** [SQLITE_ENABLE_SORTER_REFERENCES] compile-time option. +** +** [[SQLITE_CONFIG_MEMDB_MAXSIZE]] +** <dt>SQLITE_CONFIG_MEMDB_MAXSIZE +** <dd>The SQLITE_CONFIG_MEMDB_MAXSIZE option accepts a single parameter +** [sqlite3_int64] parameter which is the default maximum size for an in-memory +** database created using [sqlite3_deserialize()]. This default maximum +** size can be adjusted up or down for individual databases using the +** [SQLITE_FCNTL_SIZE_LIMIT] [sqlite3_file_control|file-control]. If this +** configuration setting is never used, then the default maximum is determined +** by the [SQLITE_MEMDB_DEFAULT_MAXSIZE] compile-time option. If that +** compile-time option is not set, then the default maximum is 1073741824. +** +** [[SQLITE_CONFIG_ROWID_IN_VIEW]] +** <dt>SQLITE_CONFIG_ROWID_IN_VIEW +** <dd>The SQLITE_CONFIG_ROWID_IN_VIEW option enables or disables the ability +** for VIEWs to have a ROWID. The capability can only be enabled if SQLite is +** compiled with -DSQLITE_ALLOW_ROWID_IN_VIEW, in which case the capability +** defaults to on. This configuration option queries the current setting or +** changes the setting to off or on. The argument is a pointer to an integer. +** If that integer initially holds a value of 1, then the ability for VIEWs to +** have ROWIDs is activated. If the integer initially holds zero, then the +** ability is deactivated. Any other initial value for the integer leaves the +** setting unchanged. After changes, if any, the integer is written with +** a 1 or 0, if the ability for VIEWs to have ROWIDs is on or off. If SQLite +** is compiled without -DSQLITE_ALLOW_ROWID_IN_VIEW (which is the usual and +** recommended case) then the integer is always filled with zero, regardless +** if its initial value. ** </dl> */ -#define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */ -#define SQLITE_CONFIG_MULTITHREAD 2 /* nil */ -#define SQLITE_CONFIG_SERIALIZED 3 /* nil */ -#define SQLITE_CONFIG_MALLOC 4 /* sqlite3_mem_methods* */ -#define SQLITE_CONFIG_GETMALLOC 5 /* sqlite3_mem_methods* */ -#define SQLITE_CONFIG_SCRATCH 6 /* void*, int sz, int N */ -#define SQLITE_CONFIG_PAGECACHE 7 /* void*, int sz, int N */ -#define SQLITE_CONFIG_HEAP 8 /* void*, int nByte, int min */ -#define SQLITE_CONFIG_MEMSTATUS 9 /* boolean */ -#define SQLITE_CONFIG_MUTEX 10 /* sqlite3_mutex_methods* */ -#define SQLITE_CONFIG_GETMUTEX 11 /* sqlite3_mutex_methods* */ -/* previously SQLITE_CONFIG_CHUNKALLOC 12 which is now unused. */ -#define SQLITE_CONFIG_LOOKASIDE 13 /* int int */ -#define SQLITE_CONFIG_PCACHE 14 /* no-op */ -#define SQLITE_CONFIG_GETPCACHE 15 /* no-op */ -#define SQLITE_CONFIG_LOG 16 /* xFunc, void* */ -#define SQLITE_CONFIG_URI 17 /* int */ -#define SQLITE_CONFIG_PCACHE2 18 /* sqlite3_pcache_methods2* */ -#define SQLITE_CONFIG_GETPCACHE2 19 /* sqlite3_pcache_methods2* */ +#define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */ +#define SQLITE_CONFIG_MULTITHREAD 2 /* nil */ +#define SQLITE_CONFIG_SERIALIZED 3 /* nil */ +#define SQLITE_CONFIG_MALLOC 4 /* sqlite3_mem_methods* */ +#define SQLITE_CONFIG_GETMALLOC 5 /* sqlite3_mem_methods* */ +#define SQLITE_CONFIG_SCRATCH 6 /* No longer used */ +#define SQLITE_CONFIG_PAGECACHE 7 /* void*, int sz, int N */ +#define SQLITE_CONFIG_HEAP 8 /* void*, int nByte, int min */ +#define SQLITE_CONFIG_MEMSTATUS 9 /* boolean */ +#define SQLITE_CONFIG_MUTEX 10 /* sqlite3_mutex_methods* */ +#define SQLITE_CONFIG_GETMUTEX 11 /* sqlite3_mutex_methods* */ +/* previously SQLITE_CONFIG_CHUNKALLOC 12 which is now unused. */ +#define SQLITE_CONFIG_LOOKASIDE 13 /* int int */ +#define SQLITE_CONFIG_PCACHE 14 /* no-op */ +#define SQLITE_CONFIG_GETPCACHE 15 /* no-op */ +#define SQLITE_CONFIG_LOG 16 /* xFunc, void* */ +#define SQLITE_CONFIG_URI 17 /* int */ +#define SQLITE_CONFIG_PCACHE2 18 /* sqlite3_pcache_methods2* */ +#define SQLITE_CONFIG_GETPCACHE2 19 /* sqlite3_pcache_methods2* */ #define SQLITE_CONFIG_COVERING_INDEX_SCAN 20 /* int */ -#define SQLITE_CONFIG_SQLLOG 21 /* xSqllog, void* */ -#define SQLITE_CONFIG_MMAP_SIZE 22 /* sqlite3_int64, sqlite3_int64 */ +#define SQLITE_CONFIG_SQLLOG 21 /* xSqllog, void* */ +#define SQLITE_CONFIG_MMAP_SIZE 22 /* sqlite3_int64, sqlite3_int64 */ #define SQLITE_CONFIG_WIN32_HEAPSIZE 23 /* int nByte */ #define SQLITE_CONFIG_PCACHE_HDRSZ 24 /* int *psz */ #define SQLITE_CONFIG_PMASZ 25 /* unsigned int szPma */ +#define SQLITE_CONFIG_STMTJRNL_SPILL 26 /* int nByte */ +#define SQLITE_CONFIG_SMALL_MALLOC 27 /* boolean */ +#define SQLITE_CONFIG_SORTERREF_SIZE 28 /* int nByte */ +#define SQLITE_CONFIG_MEMDB_MAXSIZE 29 /* sqlite3_int64 */ +#define SQLITE_CONFIG_ROWID_IN_VIEW 30 /* int* */ /* ** CAPI3REF: Database Connection Configuration Options ** ** These constants are the available integer configuration options that -** can be passed as the second argument to the [sqlite3_db_config()] interface. +** can be passed as the second parameter to the [sqlite3_db_config()] interface. +** +** The [sqlite3_db_config()] interface is a var-args function. It takes a +** variable number of parameters, though always at least two. The number of +** parameters passed into sqlite3_db_config() depends on which of these +** constants is given as the second parameter. This documentation page +** refers to parameters beyond the second as "arguments". Thus, when this +** page says "the N-th argument" it means "the N-th parameter past the +** configuration option" or "the (N+2)-th parameter to sqlite3_db_config()". ** ** New configuration options may be added in future releases of SQLite. ** Existing configuration options might be discontinued. Applications @@ -1862,31 +2257,59 @@ struct sqlite3_mem_methods { ** is invoked. ** ** <dl> +** [[SQLITE_DBCONFIG_LOOKASIDE]] ** <dt>SQLITE_DBCONFIG_LOOKASIDE</dt> -** <dd> ^This option takes three additional arguments that determine the -** [lookaside memory allocator] configuration for the [database connection]. -** ^The first argument (the third parameter to [sqlite3_db_config()] is a +** <dd> The SQLITE_DBCONFIG_LOOKASIDE option is used to adjust the +** configuration of the [lookaside memory allocator] within a database +** connection. +** The arguments to the SQLITE_DBCONFIG_LOOKASIDE option are <i>not</i> +** in the [DBCONFIG arguments|usual format]. +** The SQLITE_DBCONFIG_LOOKASIDE option takes three arguments, not two, +** so that a call to [sqlite3_db_config()] that uses SQLITE_DBCONFIG_LOOKASIDE +** should have a total of five parameters. +** <ol> +** <li><p>The first argument ("buf") is a ** pointer to a memory buffer to use for lookaside memory. -** ^The first argument after the SQLITE_DBCONFIG_LOOKASIDE verb -** may be NULL in which case SQLite will allocate the -** lookaside buffer itself using [sqlite3_malloc()]. ^The second argument is the -** size of each lookaside buffer slot. ^The third argument is the number of -** slots. The size of the buffer in the first argument must be greater than -** or equal to the product of the second and third arguments. The buffer -** must be aligned to an 8-byte boundary. ^If the second argument to -** SQLITE_DBCONFIG_LOOKASIDE is not a multiple of 8, it is internally -** rounded down to the next smaller multiple of 8. ^(The lookaside memory +** The first argument may be NULL in which case SQLite will allocate the +** lookaside buffer itself using [sqlite3_malloc()]. +** <li><P>The second argument ("sz") is the +** size of each lookaside buffer slot. Lookaside is disabled if "sz" +** is less than 8. The "sz" argument should be a multiple of 8 less than +** 65536. If "sz" does not meet this constraint, it is reduced in size until +** it does. +** <li><p>The third argument ("cnt") is the number of slots. Lookaside is disabled +** if "cnt"is less than 1. The "cnt" value will be reduced, if necessary, so +** that the product of "sz" and "cnt" does not exceed 2,147,418,112. The "cnt" +** parameter is usually chosen so that the product of "sz" and "cnt" is less +** than 1,000,000. +** </ol> +** <p>If the "buf" argument is not NULL, then it must +** point to a memory buffer with a size that is greater than +** or equal to the product of "sz" and "cnt". +** The buffer must be aligned to an 8-byte boundary. +** The lookaside memory ** configuration for a database connection can only be changed when that ** connection is not currently using lookaside memory, or in other words -** when the "current value" returned by -** [sqlite3_db_status](D,[SQLITE_CONFIG_LOOKASIDE],...) is zero. +** when the value returned by [SQLITE_DBSTATUS_LOOKASIDE_USED] is zero. ** Any attempt to change the lookaside memory configuration when lookaside -** memory is in use leaves the configuration unchanged and returns -** [SQLITE_BUSY].)^</dd> +** memory is in use leaves the configuration unchanged and returns +** [SQLITE_BUSY]. +** If the "buf" argument is NULL and an attempt +** to allocate memory based on "sz" and "cnt" fails, then +** lookaside is silently disabled. +** <p> +** The [SQLITE_CONFIG_LOOKASIDE] configuration option can be used to set the +** default lookaside configuration at initialization. The +** [-DSQLITE_DEFAULT_LOOKASIDE] option can be used to set the default lookaside +** configuration at compile-time. Typical values for lookaside are 1200 for +** "sz" and 40 to 100 for "cnt". +** </dd> ** +** [[SQLITE_DBCONFIG_ENABLE_FKEY]] ** <dt>SQLITE_DBCONFIG_ENABLE_FKEY</dt> ** <dd> ^This option is used to enable or disable the enforcement of -** [foreign key constraints]. There should be two additional arguments. +** [foreign key constraints]. This is the same setting that is +** enabled or disabled by the [PRAGMA foreign_keys] statement. ** The first argument is an integer which is 0 to disable FK enforcement, ** positive to enable FK enforcement or negative to leave FK enforcement ** unchanged. The second parameter is a pointer to an integer into which @@ -1894,6 +2317,7 @@ struct sqlite3_mem_methods { ** following this call. The second parameter may be a NULL pointer, in ** which case the FK enforcement setting is not reported back. </dd> ** +** [[SQLITE_DBCONFIG_ENABLE_TRIGGER]] ** <dt>SQLITE_DBCONFIG_ENABLE_TRIGGER</dt> ** <dd> ^This option is used to enable or disable [CREATE TRIGGER | triggers]. ** There should be two additional arguments. @@ -1902,14 +2326,370 @@ struct sqlite3_mem_methods { ** The second parameter is a pointer to an integer into which ** is written 0 or 1 to indicate whether triggers are disabled or enabled ** following this call. The second parameter may be a NULL pointer, in -** which case the trigger setting is not reported back. </dd> +** which case the trigger setting is not reported back. +** +** <p>Originally this option disabled all triggers. ^(However, since +** SQLite version 3.35.0, TEMP triggers are still allowed even if +** this option is off. So, in other words, this option now only disables +** triggers in the main database schema or in the schemas of [ATTACH]-ed +** databases.)^ </dd> +** +** [[SQLITE_DBCONFIG_ENABLE_VIEW]] +** <dt>SQLITE_DBCONFIG_ENABLE_VIEW</dt> +** <dd> ^This option is used to enable or disable [CREATE VIEW | views]. +** There must be two additional arguments. +** The first argument is an integer which is 0 to disable views, +** positive to enable views or negative to leave the setting unchanged. +** The second parameter is a pointer to an integer into which +** is written 0 or 1 to indicate whether views are disabled or enabled +** following this call. The second parameter may be a NULL pointer, in +** which case the view setting is not reported back. +** +** <p>Originally this option disabled all views. ^(However, since +** SQLite version 3.35.0, TEMP views are still allowed even if +** this option is off. So, in other words, this option now only disables +** views in the main database schema or in the schemas of ATTACH-ed +** databases.)^ </dd> +** +** [[SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER]] +** <dt>SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER</dt> +** <dd> ^This option is used to enable or disable using the +** [fts3_tokenizer()] function - part of the [FTS3] full-text search engine +** extension - without using bound parameters as the parameters. Doing so +** is disabled by default. There must be two additional arguments. The first +** argument is an integer. If it is passed 0, then using fts3_tokenizer() +** without bound parameters is disabled. If it is passed a positive value, +** then calling fts3_tokenizer without bound parameters is enabled. If it +** is passed a negative value, this setting is not modified - this can be +** used to query for the current setting. The second parameter is a pointer +** to an integer into which is written 0 or 1 to indicate the current value +** of this setting (after it is modified, if applicable). The second +** parameter may be a NULL pointer, in which case the value of the setting +** is not reported back. Refer to [FTS3] documentation for further details. +** </dd> +** +** [[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION]] +** <dt>SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION</dt> +** <dd> ^This option is used to enable or disable the [sqlite3_load_extension()] +** interface independently of the [load_extension()] SQL function. +** The [sqlite3_enable_load_extension()] API enables or disables both the +** C-API [sqlite3_load_extension()] and the SQL function [load_extension()]. +** There must be two additional arguments. +** When the first argument to this interface is 1, then only the C-API is +** enabled and the SQL function remains disabled. If the first argument to +** this interface is 0, then both the C-API and the SQL function are disabled. +** If the first argument is -1, then no changes are made to the state of either +** the C-API or the SQL function. +** The second parameter is a pointer to an integer into which +** is written 0 or 1 to indicate whether [sqlite3_load_extension()] interface +** is disabled or enabled following this call. The second parameter may +** be a NULL pointer, in which case the new setting is not reported back. +** </dd> +** +** [[SQLITE_DBCONFIG_MAINDBNAME]] <dt>SQLITE_DBCONFIG_MAINDBNAME</dt> +** <dd> ^This option is used to change the name of the "main" database +** schema. This option does not follow the +** [DBCONFIG arguments|usual SQLITE_DBCONFIG argument format]. +** This option takes exactly one additional argument so that the +** [sqlite3_db_config()] call has a total of three parameters. The +** extra argument must be a pointer to a constant UTF8 string which +** will become the new schema name in place of "main". ^SQLite does +** not make a copy of the new main schema name string, so the application +** must ensure that the argument passed into SQLITE_DBCONFIG MAINDBNAME +** is unchanged until after the database connection closes. +** </dd> +** +** [[SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE]] +** <dt>SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE</dt> +** <dd> Usually, when a database in [WAL mode] is closed or detached from a +** database handle, SQLite checks if if there are other connections to the +** same database, and if there are no other database connection (if the +** connection being closed is the last open connection to the database), +** then SQLite performs a [checkpoint] before closing the connection and +** deletes the WAL file. The SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE option can +** be used to override that behavior. The first argument passed to this +** operation (the third parameter to [sqlite3_db_config()]) is an integer +** which is positive to disable checkpoints-on-close, or zero (the default) +** to enable them, and negative to leave the setting unchanged. +** The second argument (the fourth parameter) is a pointer to an integer +** into which is written 0 or 1 to indicate whether checkpoints-on-close +** have been disabled - 0 if they are not disabled, 1 if they are. +** </dd> +** +** [[SQLITE_DBCONFIG_ENABLE_QPSG]] <dt>SQLITE_DBCONFIG_ENABLE_QPSG</dt> +** <dd>^(The SQLITE_DBCONFIG_ENABLE_QPSG option activates or deactivates +** the [query planner stability guarantee] (QPSG). When the QPSG is active, +** a single SQL query statement will always use the same algorithm regardless +** of values of [bound parameters].)^ The QPSG disables some query optimizations +** that look at the values of bound parameters, which can make some queries +** slower. But the QPSG has the advantage of more predictable behavior. With +** the QPSG active, SQLite will always use the same query plan in the field as +** was used during testing in the lab. +** The first argument to this setting is an integer which is 0 to disable +** the QPSG, positive to enable QPSG, or negative to leave the setting +** unchanged. The second parameter is a pointer to an integer into which +** is written 0 or 1 to indicate whether the QPSG is disabled or enabled +** following this call. +** </dd> +** +** [[SQLITE_DBCONFIG_TRIGGER_EQP]] <dt>SQLITE_DBCONFIG_TRIGGER_EQP</dt> +** <dd> By default, the output of EXPLAIN QUERY PLAN commands does not +** include output for any operations performed by trigger programs. This +** option is used to set or clear (the default) a flag that governs this +** behavior. The first parameter passed to this operation is an integer - +** positive to enable output for trigger programs, or zero to disable it, +** or negative to leave the setting unchanged. +** The second parameter is a pointer to an integer into which is written +** 0 or 1 to indicate whether output-for-triggers has been disabled - 0 if +** it is not disabled, 1 if it is. +** </dd> +** +** [[SQLITE_DBCONFIG_RESET_DATABASE]] <dt>SQLITE_DBCONFIG_RESET_DATABASE</dt> +** <dd> Set the SQLITE_DBCONFIG_RESET_DATABASE flag and then run +** [VACUUM] in order to reset a database back to an empty database +** with no schema and no content. The following process works even for +** a badly corrupted database file: +** <ol> +** <li> If the database connection is newly opened, make sure it has read the +** database schema by preparing then discarding some query against the +** database, or calling sqlite3_table_column_metadata(), ignoring any +** errors. This step is only necessary if the application desires to keep +** the database in WAL mode after the reset if it was in WAL mode before +** the reset. +** <li> sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0); +** <li> [sqlite3_exec](db, "[VACUUM]", 0, 0, 0); +** <li> sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0); +** </ol> +** Because resetting a database is destructive and irreversible, the +** process requires the use of this obscure API and multiple steps to +** help ensure that it does not happen by accident. Because this +** feature must be capable of resetting corrupt databases, and +** shutting down virtual tables may require access to that corrupt +** storage, the library must abandon any installed virtual tables +** without calling their xDestroy() methods. +** +** [[SQLITE_DBCONFIG_DEFENSIVE]] <dt>SQLITE_DBCONFIG_DEFENSIVE</dt> +** <dd>The SQLITE_DBCONFIG_DEFENSIVE option activates or deactivates the +** "defensive" flag for a database connection. When the defensive +** flag is enabled, language features that allow ordinary SQL to +** deliberately corrupt the database file are disabled. The disabled +** features include but are not limited to the following: +** <ul> +** <li> The [PRAGMA writable_schema=ON] statement. +** <li> The [PRAGMA journal_mode=OFF] statement. +** <li> The [PRAGMA schema_version=N] statement. +** <li> Writes to the [sqlite_dbpage] virtual table. +** <li> Direct writes to [shadow tables]. +** </ul> +** </dd> +** +** [[SQLITE_DBCONFIG_WRITABLE_SCHEMA]] <dt>SQLITE_DBCONFIG_WRITABLE_SCHEMA</dt> +** <dd>The SQLITE_DBCONFIG_WRITABLE_SCHEMA option activates or deactivates the +** "writable_schema" flag. This has the same effect and is logically equivalent +** to setting [PRAGMA writable_schema=ON] or [PRAGMA writable_schema=OFF]. +** The first argument to this setting is an integer which is 0 to disable +** the writable_schema, positive to enable writable_schema, or negative to +** leave the setting unchanged. The second parameter is a pointer to an +** integer into which is written 0 or 1 to indicate whether the writable_schema +** is enabled or disabled following this call. +** </dd> +** +** [[SQLITE_DBCONFIG_LEGACY_ALTER_TABLE]] +** <dt>SQLITE_DBCONFIG_LEGACY_ALTER_TABLE</dt> +** <dd>The SQLITE_DBCONFIG_LEGACY_ALTER_TABLE option activates or deactivates +** the legacy behavior of the [ALTER TABLE RENAME] command such that it +** behaves as it did prior to [version 3.24.0] (2018-06-04). See the +** "Compatibility Notice" on the [ALTER TABLE RENAME documentation] for +** additional information. This feature can also be turned on and off +** using the [PRAGMA legacy_alter_table] statement. +** </dd> +** +** [[SQLITE_DBCONFIG_DQS_DML]] +** <dt>SQLITE_DBCONFIG_DQS_DML</dt> +** <dd>The SQLITE_DBCONFIG_DQS_DML option activates or deactivates +** the legacy [double-quoted string literal] misfeature for DML statements +** only, that is DELETE, INSERT, SELECT, and UPDATE statements. The +** default value of this setting is determined by the [-DSQLITE_DQS] +** compile-time option. +** </dd> +** +** [[SQLITE_DBCONFIG_DQS_DDL]] +** <dt>SQLITE_DBCONFIG_DQS_DDL</dt> +** <dd>The SQLITE_DBCONFIG_DQS option activates or deactivates +** the legacy [double-quoted string literal] misfeature for DDL statements, +** such as CREATE TABLE and CREATE INDEX. The +** default value of this setting is determined by the [-DSQLITE_DQS] +** compile-time option. +** </dd> +** +** [[SQLITE_DBCONFIG_TRUSTED_SCHEMA]] +** <dt>SQLITE_DBCONFIG_TRUSTED_SCHEMA</dt> +** <dd>The SQLITE_DBCONFIG_TRUSTED_SCHEMA option tells SQLite to +** assume that database schemas are untainted by malicious content. +** When the SQLITE_DBCONFIG_TRUSTED_SCHEMA option is disabled, SQLite +** takes additional defensive steps to protect the application from harm +** including: +** <ul> +** <li> Prohibit the use of SQL functions inside triggers, views, +** CHECK constraints, DEFAULT clauses, expression indexes, +** partial indexes, or generated columns +** unless those functions are tagged with [SQLITE_INNOCUOUS]. +** <li> Prohibit the use of virtual tables inside of triggers or views +** unless those virtual tables are tagged with [SQLITE_VTAB_INNOCUOUS]. +** </ul> +** This setting defaults to "on" for legacy compatibility, however +** all applications are advised to turn it off if possible. This setting +** can also be controlled using the [PRAGMA trusted_schema] statement. +** </dd> +** +** [[SQLITE_DBCONFIG_LEGACY_FILE_FORMAT]] +** <dt>SQLITE_DBCONFIG_LEGACY_FILE_FORMAT</dt> +** <dd>The SQLITE_DBCONFIG_LEGACY_FILE_FORMAT option activates or deactivates +** the legacy file format flag. When activated, this flag causes all newly +** created database files to have a schema format version number (the 4-byte +** integer found at offset 44 into the database header) of 1. This in turn +** means that the resulting database file will be readable and writable by +** any SQLite version back to 3.0.0 ([dateof:3.0.0]). Without this setting, +** newly created databases are generally not understandable by SQLite versions +** prior to 3.3.0 ([dateof:3.3.0]). As these words are written, there +** is now scarcely any need to generate database files that are compatible +** all the way back to version 3.0.0, and so this setting is of little +** practical use, but is provided so that SQLite can continue to claim the +** ability to generate new database files that are compatible with version +** 3.0.0. +** <p>Note that when the SQLITE_DBCONFIG_LEGACY_FILE_FORMAT setting is on, +** the [VACUUM] command will fail with an obscure error when attempting to +** process a table with generated columns and a descending index. This is +** not considered a bug since SQLite versions 3.3.0 and earlier do not support +** either generated columns or descending indexes. +** </dd> +** +** [[SQLITE_DBCONFIG_STMT_SCANSTATUS]] +** <dt>SQLITE_DBCONFIG_STMT_SCANSTATUS</dt> +** <dd>The SQLITE_DBCONFIG_STMT_SCANSTATUS option is only useful in +** SQLITE_ENABLE_STMT_SCANSTATUS builds. In this case, it sets or clears +** a flag that enables collection of the sqlite3_stmt_scanstatus_v2() +** statistics. For statistics to be collected, the flag must be set on +** the database handle both when the SQL statement is prepared and when it +** is stepped. The flag is set (collection of statistics is enabled) +** by default. <p>This option takes two arguments: an integer and a pointer to +** an integer. The first argument is 1, 0, or -1 to enable, disable, or +** leave unchanged the statement scanstatus option. If the second argument +** is not NULL, then the value of the statement scanstatus setting after +** processing the first argument is written into the integer that the second +** argument points to. +** </dd> +** +** [[SQLITE_DBCONFIG_REVERSE_SCANORDER]] +** <dt>SQLITE_DBCONFIG_REVERSE_SCANORDER</dt> +** <dd>The SQLITE_DBCONFIG_REVERSE_SCANORDER option changes the default order +** in which tables and indexes are scanned so that the scans start at the end +** and work toward the beginning rather than starting at the beginning and +** working toward the end. Setting SQLITE_DBCONFIG_REVERSE_SCANORDER is the +** same as setting [PRAGMA reverse_unordered_selects]. <p>This option takes +** two arguments which are an integer and a pointer to an integer. The first +** argument is 1, 0, or -1 to enable, disable, or leave unchanged the +** reverse scan order flag, respectively. If the second argument is not NULL, +** then 0 or 1 is written into the integer that the second argument points to +** depending on if the reverse scan order flag is set after processing the +** first argument. +** </dd> +** +** [[SQLITE_DBCONFIG_ENABLE_ATTACH_CREATE]] +** <dt>SQLITE_DBCONFIG_ENABLE_ATTACH_CREATE</dt> +** <dd>The SQLITE_DBCONFIG_ENABLE_ATTACH_CREATE option enables or disables +** the ability of the [ATTACH DATABASE] SQL command to create a new database +** file if the database filed named in the ATTACH command does not already +** exist. This ability of ATTACH to create a new database is enabled by +** default. Applications can disable or reenable the ability for ATTACH to +** create new database files using this DBCONFIG option.<p> +** This option takes two arguments which are an integer and a pointer +** to an integer. The first argument is 1, 0, or -1 to enable, disable, or +** leave unchanged the attach-create flag, respectively. If the second +** argument is not NULL, then 0 or 1 is written into the integer that the +** second argument points to depending on if the attach-create flag is set +** after processing the first argument. +** </dd> +** +** [[SQLITE_DBCONFIG_ENABLE_ATTACH_WRITE]] +** <dt>SQLITE_DBCONFIG_ENABLE_ATTACH_WRITE</dt> +** <dd>The SQLITE_DBCONFIG_ENABLE_ATTACH_WRITE option enables or disables the +** ability of the [ATTACH DATABASE] SQL command to open a database for writing. +** This capability is enabled by default. Applications can disable or +** reenable this capability using the current DBCONFIG option. If +** this capability is disabled, the [ATTACH] command will still work, +** but the database will be opened read-only. If this option is disabled, +** then the ability to create a new database using [ATTACH] is also disabled, +** regardless of the value of the [SQLITE_DBCONFIG_ENABLE_ATTACH_CREATE] +** option.<p> +** This option takes two arguments which are an integer and a pointer +** to an integer. The first argument is 1, 0, or -1 to enable, disable, or +** leave unchanged the ability to ATTACH another database for writing, +** respectively. If the second argument is not NULL, then 0 or 1 is written +** into the integer to which the second argument points, depending on whether +** the ability to ATTACH a read/write database is enabled or disabled +** after processing the first argument. +** </dd> +** +** [[SQLITE_DBCONFIG_ENABLE_COMMENTS]] +** <dt>SQLITE_DBCONFIG_ENABLE_COMMENTS</dt> +** <dd>The SQLITE_DBCONFIG_ENABLE_COMMENTS option enables or disables the +** ability to include comments in SQL text. Comments are enabled by default. +** An application can disable or reenable comments in SQL text using this +** DBCONFIG option.<p> +** This option takes two arguments which are an integer and a pointer +** to an integer. The first argument is 1, 0, or -1 to enable, disable, or +** leave unchanged the ability to use comments in SQL text, +** respectively. If the second argument is not NULL, then 0 or 1 is written +** into the integer that the second argument points to depending on if +** comments are allowed in SQL text after processing the first argument. +** </dd> ** ** </dl> -*/ -#define SQLITE_DBCONFIG_LOOKASIDE 1001 /* void* int int */ -#define SQLITE_DBCONFIG_ENABLE_FKEY 1002 /* int int* */ -#define SQLITE_DBCONFIG_ENABLE_TRIGGER 1003 /* int int* */ - +** +** [[DBCONFIG arguments]] <h3>Arguments To SQLITE_DBCONFIG Options</h3> +** +** <p>Most of the SQLITE_DBCONFIG options take two arguments, so that the +** overall call to [sqlite3_db_config()] has a total of four parameters. +** The first argument (the third parameter to sqlite3_db_config()) is an integer. +** The second argument is a pointer to an integer. If the first argument is 1, +** then the option becomes enabled. If the first integer argument is 0, then the +** option is disabled. If the first argument is -1, then the option setting +** is unchanged. The second argument, the pointer to an integer, may be NULL. +** If the second argument is not NULL, then a value of 0 or 1 is written into +** the integer to which the second argument points, depending on whether the +** setting is disabled or enabled after applying any changes specified by +** the first argument. +** +** <p>While most SQLITE_DBCONFIG options use the argument format +** described in the previous paragraph, the [SQLITE_DBCONFIG_MAINDBNAME] +** and [SQLITE_DBCONFIG_LOOKASIDE] options are different. See the +** documentation of those exceptional options for details. +*/ +#define SQLITE_DBCONFIG_MAINDBNAME 1000 /* const char* */ +#define SQLITE_DBCONFIG_LOOKASIDE 1001 /* void* int int */ +#define SQLITE_DBCONFIG_ENABLE_FKEY 1002 /* int int* */ +#define SQLITE_DBCONFIG_ENABLE_TRIGGER 1003 /* int int* */ +#define SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER 1004 /* int int* */ +#define SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION 1005 /* int int* */ +#define SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE 1006 /* int int* */ +#define SQLITE_DBCONFIG_ENABLE_QPSG 1007 /* int int* */ +#define SQLITE_DBCONFIG_TRIGGER_EQP 1008 /* int int* */ +#define SQLITE_DBCONFIG_RESET_DATABASE 1009 /* int int* */ +#define SQLITE_DBCONFIG_DEFENSIVE 1010 /* int int* */ +#define SQLITE_DBCONFIG_WRITABLE_SCHEMA 1011 /* int int* */ +#define SQLITE_DBCONFIG_LEGACY_ALTER_TABLE 1012 /* int int* */ +#define SQLITE_DBCONFIG_DQS_DML 1013 /* int int* */ +#define SQLITE_DBCONFIG_DQS_DDL 1014 /* int int* */ +#define SQLITE_DBCONFIG_ENABLE_VIEW 1015 /* int int* */ +#define SQLITE_DBCONFIG_LEGACY_FILE_FORMAT 1016 /* int int* */ +#define SQLITE_DBCONFIG_TRUSTED_SCHEMA 1017 /* int int* */ +#define SQLITE_DBCONFIG_STMT_SCANSTATUS 1018 /* int int* */ +#define SQLITE_DBCONFIG_REVERSE_SCANORDER 1019 /* int int* */ +#define SQLITE_DBCONFIG_ENABLE_ATTACH_CREATE 1020 /* int int* */ +#define SQLITE_DBCONFIG_ENABLE_ATTACH_WRITE 1021 /* int int* */ +#define SQLITE_DBCONFIG_ENABLE_COMMENTS 1022 /* int int* */ +#define SQLITE_DBCONFIG_MAX 1022 /* Largest DBCONFIG */ /* ** CAPI3REF: Enable Or Disable Extended Result Codes @@ -1933,20 +2713,30 @@ int sqlite3_extended_result_codes(sqlite3*, int onoff); ** the table has a column of type [INTEGER PRIMARY KEY] then that column ** is another alias for the rowid. ** -** ^The sqlite3_last_insert_rowid(D) interface returns the [rowid] of the -** most recent successful [INSERT] into a rowid table or [virtual table] -** on database connection D. -** ^Inserts into [WITHOUT ROWID] tables are not recorded. -** ^If no successful [INSERT]s into rowid tables -** have ever occurred on the database connection D, -** then sqlite3_last_insert_rowid(D) returns zero. -** -** ^(If an [INSERT] occurs within a trigger or within a [virtual table] -** method, then this routine will return the [rowid] of the inserted -** row as long as the trigger or virtual table method is running. -** But once the trigger or virtual table method ends, the value returned -** by this routine reverts to what it was before the trigger or virtual -** table method began.)^ +** ^The sqlite3_last_insert_rowid(D) interface usually returns the [rowid] of +** the most recent successful [INSERT] into a rowid table or [virtual table] +** on database connection D. ^Inserts into [WITHOUT ROWID] tables are not +** recorded. ^If no successful [INSERT]s into rowid tables have ever occurred +** on the database connection D, then sqlite3_last_insert_rowid(D) returns +** zero. +** +** As well as being set automatically as rows are inserted into database +** tables, the value returned by this function may be set explicitly by +** [sqlite3_set_last_insert_rowid()] +** +** Some virtual table implementations may INSERT rows into rowid tables as +** part of committing a transaction (e.g. to flush data accumulated in memory +** to disk). In this case subsequent calls to this function return the rowid +** associated with these internal INSERT operations, which leads to +** unintuitive results. Virtual table implementations that do write to rowid +** tables in this way can avoid this problem by restoring the original +** rowid value using [sqlite3_set_last_insert_rowid()] before returning +** control to the user. +** +** ^(If an [INSERT] occurs within a trigger then this routine will +** return the [rowid] of the inserted row as long as the trigger is +** running. Once the trigger program ends, the value returned +** by this routine reverts to what it was before the trigger was fired.)^ ** ** ^An [INSERT] that fails due to a constraint violation is not a ** successful [INSERT] and does not change the value returned by this @@ -1973,82 +2763,123 @@ int sqlite3_extended_result_codes(sqlite3*, int onoff); */ sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*); +/* +** CAPI3REF: Set the Last Insert Rowid value. +** METHOD: sqlite3 +** +** The sqlite3_set_last_insert_rowid(D, R) method allows the application to +** set the value returned by calling sqlite3_last_insert_rowid(D) to R +** without inserting a row into the database. +*/ +void sqlite3_set_last_insert_rowid(sqlite3*,sqlite3_int64); + /* ** CAPI3REF: Count The Number Of Rows Modified ** METHOD: sqlite3 ** -** ^This function returns the number of rows modified, inserted or +** ^These functions return the number of rows modified, inserted or ** deleted by the most recently completed INSERT, UPDATE or DELETE ** statement on the database connection specified by the only parameter. -** ^Executing any other type of SQL statement does not modify the value -** returned by this function. +** The two functions are identical except for the type of the return value +** and that if the number of rows modified by the most recent INSERT, UPDATE, +** or DELETE is greater than the maximum value supported by type "int", then +** the return value of sqlite3_changes() is undefined. ^Executing any other +** type of SQL statement does not modify the value returned by these functions. +** For the purposes of this interface, a CREATE TABLE AS SELECT statement +** does not count as an INSERT, UPDATE or DELETE statement and hence the rows +** added to the new table by the CREATE TABLE AS SELECT statement are not +** counted. ** ** ^Only changes made directly by the INSERT, UPDATE or DELETE statement are -** considered - auxiliary changes caused by [CREATE TRIGGER | triggers], +** considered - auxiliary changes caused by [CREATE TRIGGER | triggers], ** [foreign key actions] or [REPLACE] constraint resolution are not counted. -** -** Changes to a view that are intercepted by -** [INSTEAD OF trigger | INSTEAD OF triggers] are not counted. ^The value -** returned by sqlite3_changes() immediately after an INSERT, UPDATE or -** DELETE statement run on a view is always zero. Only changes made to real +** +** Changes to a view that are intercepted by +** [INSTEAD OF trigger | INSTEAD OF triggers] are not counted. ^The value +** returned by sqlite3_changes() immediately after an INSERT, UPDATE or +** DELETE statement run on a view is always zero. Only changes made to real ** tables are counted. ** ** Things are more complicated if the sqlite3_changes() function is ** executed while a trigger program is running. This may happen if the ** program uses the [changes() SQL function], or if some other callback ** function invokes sqlite3_changes() directly. Essentially: -** +** ** <ul> ** <li> ^(Before entering a trigger program the value returned by -** sqlite3_changes() function is saved. After the trigger program +** sqlite3_changes() function is saved. After the trigger program ** has finished, the original value is restored.)^ -** -** <li> ^(Within a trigger program each INSERT, UPDATE and DELETE -** statement sets the value returned by sqlite3_changes() -** upon completion as normal. Of course, this value will not include -** any changes performed by sub-triggers, as the sqlite3_changes() +** +** <li> ^(Within a trigger program each INSERT, UPDATE and DELETE +** statement sets the value returned by sqlite3_changes() +** upon completion as normal. Of course, this value will not include +** any changes performed by sub-triggers, as the sqlite3_changes() ** value will be saved and restored after each sub-trigger has run.)^ ** </ul> -** +** ** ^This means that if the changes() SQL function (or similar) is used -** by the first INSERT, UPDATE or DELETE statement within a trigger, it +** by the first INSERT, UPDATE or DELETE statement within a trigger, it ** returns the value as set when the calling statement began executing. -** ^If it is used by the second or subsequent such statement within a trigger -** program, the value returned reflects the number of rows modified by the +** ^If it is used by the second or subsequent such statement within a trigger +** program, the value returned reflects the number of rows modified by the ** previous INSERT, UPDATE or DELETE statement within the same trigger. ** -** See also the [sqlite3_total_changes()] interface, the -** [count_changes pragma], and the [changes() SQL function]. -** ** If a separate thread makes changes on the same database connection ** while [sqlite3_changes()] is running then the value returned ** is unpredictable and not meaningful. +** +** See also: +** <ul> +** <li> the [sqlite3_total_changes()] interface +** <li> the [count_changes pragma] +** <li> the [changes() SQL function] +** <li> the [data_version pragma] +** </ul> */ int sqlite3_changes(sqlite3*); +sqlite3_int64 sqlite3_changes64(sqlite3*); /* ** CAPI3REF: Total Number Of Rows Modified ** METHOD: sqlite3 ** -** ^This function returns the total number of rows inserted, modified or +** ^These functions return the total number of rows inserted, modified or ** deleted by all [INSERT], [UPDATE] or [DELETE] statements completed ** since the database connection was opened, including those executed as -** part of trigger programs. ^Executing any other type of SQL statement -** does not affect the value returned by sqlite3_total_changes(). -** +** part of trigger programs. The two functions are identical except for the +** type of the return value and that if the number of rows modified by the +** connection exceeds the maximum value supported by type "int", then +** the return value of sqlite3_total_changes() is undefined. ^Executing +** any other type of SQL statement does not affect the value returned by +** sqlite3_total_changes(). +** ** ^Changes made as part of [foreign key actions] are included in the ** count, but those made as part of REPLACE constraint resolution are -** not. ^Changes to a view that are intercepted by INSTEAD OF triggers +** not. ^Changes to a view that are intercepted by INSTEAD OF triggers ** are not counted. -** -** See also the [sqlite3_changes()] interface, the -** [count_changes pragma], and the [total_changes() SQL function]. +** +** The [sqlite3_total_changes(D)] interface only reports the number +** of rows that changed due to SQL statement run against database +** connection D. Any changes by other database connections are ignored. +** To detect changes against a database file from other database +** connections use the [PRAGMA data_version] command or the +** [SQLITE_FCNTL_DATA_VERSION] [file control]. ** ** If a separate thread makes changes on the same database connection ** while [sqlite3_total_changes()] is running then the value ** returned is unpredictable and not meaningful. +** +** See also: +** <ul> +** <li> the [sqlite3_changes()] interface +** <li> the [count_changes pragma] +** <li> the [changes() SQL function] +** <li> the [data_version pragma] +** <li> the [SQLITE_FCNTL_DATA_VERSION] [file control] +** </ul> */ int sqlite3_total_changes(sqlite3*); +sqlite3_int64 sqlite3_total_changes64(sqlite3*); /* ** CAPI3REF: Interrupt A Long-Running Query @@ -2076,8 +2907,8 @@ int sqlite3_total_changes(sqlite3*); ** ** ^The sqlite3_interrupt(D) call is in effect until all currently running ** SQL statements on [database connection] D complete. ^Any new SQL statements -** that are started after the sqlite3_interrupt() call and before the -** running statements reaches zero are interrupted as if they had been +** that are started after the sqlite3_interrupt() call and before the +** running statement count reaches zero are interrupted as if they had been ** running prior to the sqlite3_interrupt() call. ^New SQL statements ** that are started after the running statement count reaches zero are ** not effected by the sqlite3_interrupt(). @@ -2085,10 +2916,12 @@ int sqlite3_total_changes(sqlite3*); ** SQL statements is a no-op and has no effect on SQL statements ** that are started after the sqlite3_interrupt() call returns. ** -** If the database connection closes while [sqlite3_interrupt()] -** is running then bad things will likely happen. +** ^The [sqlite3_is_interrupted(D)] interface can be used to determine whether +** or not an interrupt is currently in effect for [database connection] D. +** It returns 1 if an interrupt is currently in effect, or 0 otherwise. */ void sqlite3_interrupt(sqlite3*); +int sqlite3_is_interrupted(sqlite3*); /* ** CAPI3REF: Determine If An SQL Statement Is Complete @@ -2108,10 +2941,10 @@ void sqlite3_interrupt(sqlite3*); ** ^These routines return 0 if the statement is incomplete. ^If a ** memory allocation fails, then SQLITE_NOMEM is returned. ** -** ^These routines do not parse the SQL statements thus +** ^These routines do not parse the SQL statements and thus ** will not detect syntactically incorrect SQL. ** -** ^(If SQLite has not been initialized using [sqlite3_initialize()] prior +** ^(If SQLite has not been initialized using [sqlite3_initialize()] prior ** to invoking sqlite3_complete16() then sqlite3_initialize() is invoked ** automatically by sqlite3_complete16(). If that initialization fails, ** then the return value from sqlite3_complete16() will be non-zero @@ -2156,7 +2989,7 @@ int sqlite3_complete16(const void *sql); ** The presence of a busy handler does not guarantee that it will be invoked ** when there is lock contention. ^If SQLite determines that invoking the busy ** handler could result in a deadlock, it will go ahead and return [SQLITE_BUSY] -** to the application instead of invoking the +** to the application instead of invoking the ** busy handler. ** Consider a scenario where one process is holding a read lock that ** it is trying to promote to a reserved lock and @@ -2181,11 +3014,11 @@ int sqlite3_complete16(const void *sql); ** database connection that invoked the busy handler. In other words, ** the busy handler is not reentrant. Any such actions ** result in undefined behavior. -** +** ** A busy handler must not close the database connection ** or [prepared statement] that invoked the busy handler. */ -int sqlite3_busy_handler(sqlite3*, int(*)(void*,int), void*); +int sqlite3_busy_handler(sqlite3*,int(*)(void*,int),void*); /* ** CAPI3REF: Set A Busy Timeout @@ -2210,6 +3043,44 @@ int sqlite3_busy_handler(sqlite3*, int(*)(void*,int), void*); */ int sqlite3_busy_timeout(sqlite3*, int ms); +/* +** CAPI3REF: Set the Setlk Timeout +** METHOD: sqlite3 +** +** This routine is only useful in SQLITE_ENABLE_SETLK_TIMEOUT builds. If +** the VFS supports blocking locks, it sets the timeout in ms used by +** eligible locks taken on wal mode databases by the specified database +** handle. In non-SQLITE_ENABLE_SETLK_TIMEOUT builds, or if the VFS does +** not support blocking locks, this function is a no-op. +** +** Passing 0 to this function disables blocking locks altogether. Passing +** -1 to this function requests that the VFS blocks for a long time - +** indefinitely if possible. The results of passing any other negative value +** are undefined. +** +** Internally, each SQLite database handle stores two timeout values - the +** busy-timeout (used for rollback mode databases, or if the VFS does not +** support blocking locks) and the setlk-timeout (used for blocking locks +** on wal-mode databases). The sqlite3_busy_timeout() method sets both +** values, this function sets only the setlk-timeout value. Therefore, +** to configure separate busy-timeout and setlk-timeout values for a single +** database handle, call sqlite3_busy_timeout() followed by this function. +** +** Whenever the number of connections to a wal mode database falls from +** 1 to 0, the last connection takes an exclusive lock on the database, +** then checkpoints and deletes the wal file. While it is doing this, any +** new connection that tries to read from the database fails with an +** SQLITE_BUSY error. Or, if the SQLITE_SETLK_BLOCK_ON_CONNECT flag is +** passed to this API, the new connection blocks until the exclusive lock +** has been released. +*/ +int sqlite3_setlk_timeout(sqlite3*, int ms, int flags); + +/* +** CAPI3REF: Flags for sqlite3_setlk_timeout() +*/ +#define SQLITE_SETLK_BLOCK_ON_CONNECT 0x01 + /* ** CAPI3REF: Convenience Routines For Running Queries ** METHOD: sqlite3 @@ -2217,7 +3088,7 @@ int sqlite3_busy_timeout(sqlite3*, int ms); ** This is a legacy interface that is preserved for backwards compatibility. ** Use of this interface is not recommended. ** -** Definition: A <b>result table</b> is memory data structure created by the +** Definition: A <b>result table</b> is a memory data structure created by the ** [sqlite3_get_table()] interface. A result table records the ** complete query results from one or more queries. ** @@ -2248,9 +3119,9 @@ int sqlite3_busy_timeout(sqlite3*, int ms); ** Cindy | 21 ** </pre></blockquote> ** -** There are two column (M==2) and three rows (N==3). Thus the +** There are two columns (M==2) and three rows (N==3). Thus the ** result table has 8 entries. Suppose the result table is stored -** in an array names azResult. Then azResult holds this content: +** in an array named azResult. Then azResult holds this content: ** ** <blockquote><pre> ** azResult&#91;0] = "Name"; @@ -2298,16 +3169,16 @@ void sqlite3_free_table(char **result); ** ** These routines are work-alikes of the "printf()" family of functions ** from the standard C library. -** These routines understand most of the common K&R formatting options, -** plus some additional non-standard formats, detailed below. -** Note that some of the more obscure formatting options from recent -** C-library standards are omitted from this implementation. +** These routines understand most of the common formatting options from +** the standard library printf() +** plus some additional non-standard formats ([%q], [%Q], [%w], and [%z]). +** See the [built-in printf()] documentation for details. ** ** ^The sqlite3_mprintf() and sqlite3_vmprintf() routines write their -** results into memory obtained from [sqlite3_malloc()]. +** results into memory obtained from [sqlite3_malloc64()]. ** The strings returned by these two routines should be ** released by [sqlite3_free()]. ^Both routines return a -** NULL pointer if [sqlite3_malloc()] is unable to allocate enough +** NULL pointer if [sqlite3_malloc64()] is unable to allocate enough ** memory to hold the resulting string. ** ** ^(The sqlite3_snprintf() routine is similar to "snprintf()" from @@ -2331,71 +3202,7 @@ void sqlite3_free_table(char **result); ** ** ^The sqlite3_vsnprintf() routine is a varargs version of sqlite3_snprintf(). ** -** These routines all implement some additional formatting -** options that are useful for constructing SQL statements. -** All of the usual printf() formatting options apply. In addition, there -** is are "%q", "%Q", "%w" and "%z" options. -** -** ^(The %q option works like %s in that it substitutes a nul-terminated -** string from the argument list. But %q also doubles every '\'' character. -** %q is designed for use inside a string literal.)^ By doubling each '\'' -** character it escapes that character and allows it to be inserted into -** the string. -** -** For example, assume the string variable zText contains text as follows: -** -** <blockquote><pre> -** char *zText = "It's a happy day!"; -** </pre></blockquote> -** -** One can use this text in an SQL statement as follows: -** -** <blockquote><pre> -** char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES('%q')", zText); -** sqlite3_exec(db, zSQL, 0, 0, 0); -** sqlite3_free(zSQL); -** </pre></blockquote> -** -** Because the %q format string is used, the '\'' character in zText -** is escaped and the SQL generated is as follows: -** -** <blockquote><pre> -** INSERT INTO table1 VALUES('It''s a happy day!') -** </pre></blockquote> -** -** This is correct. Had we used %s instead of %q, the generated SQL -** would have looked like this: -** -** <blockquote><pre> -** INSERT INTO table1 VALUES('It's a happy day!'); -** </pre></blockquote> -** -** This second example is an SQL syntax error. As a general rule you should -** always use %q instead of %s when inserting text into a string literal. -** -** ^(The %Q option works like %q except it also adds single quotes around -** the outside of the total string. Additionally, if the parameter in the -** argument list is a NULL pointer, %Q substitutes the text "NULL" (without -** single quotes).)^ So, for example, one could say: -** -** <blockquote><pre> -** char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES(%Q)", zText); -** sqlite3_exec(db, zSQL, 0, 0, 0); -** sqlite3_free(zSQL); -** </pre></blockquote> -** -** The code above will render a correct SQL statement in the zSQL -** variable even if the zText variable is a NULL pointer. -** -** ^(The "%w" formatting option is like "%q" except that it expects to -** be contained within double-quotes instead of single quotes, and it -** escapes the double-quote character instead of the single-quote -** character.)^ The "%w" formatting option is intended for safely inserting -** table and column names into a constructed SQL statement. -** -** ^(The "%z" formatting option works like "%s" but with the -** addition that after the string has been read and copied into -** the result, [sqlite3_free()] is called on the input string.)^ +** See also: [built-in printf()], [printf() SQL function] */ char *sqlite3_mprintf(const char*,...); char *sqlite3_vmprintf(const char*, va_list); @@ -2407,7 +3214,7 @@ char *sqlite3_vsnprintf(int,char*,const char*, va_list); ** ** The SQLite core uses these three routines for all of its own ** internal memory allocation needs. "Core" in the previous sentence -** does not include operating-system specific VFS implementation. The +** does not include operating-system specific [VFS] implementation. The ** Windows VFS uses native malloc() and free() for some operations. ** ** ^The sqlite3_malloc() routine returns a pointer to a block @@ -2424,7 +3231,7 @@ char *sqlite3_vsnprintf(int,char*,const char*, va_list); ** ^Calling sqlite3_free() with a pointer previously returned ** by sqlite3_malloc() or sqlite3_realloc() releases that memory so ** that it might be reused. ^The sqlite3_free() routine is -** a no-op if is called with a NULL pointer. Passing a NULL pointer +** a no-op if it is called with a NULL pointer. Passing a NULL pointer ** to sqlite3_free() is harmless. After being freed, memory ** should neither be read nor written. Even reading previously freed ** memory might result in a segmentation fault or other severe error. @@ -2442,13 +3249,13 @@ char *sqlite3_vsnprintf(int,char*,const char*, va_list); ** sqlite3_free(X). ** ^sqlite3_realloc(X,N) returns a pointer to a memory allocation ** of at least N bytes in size or NULL if insufficient memory is available. -** ^If M is the size of the prior allocation, then min(N,M) bytes -** of the prior allocation are copied into the beginning of buffer returned +** ^If M is the size of the prior allocation, then min(N,M) bytes of the +** prior allocation are copied into the beginning of the buffer returned ** by sqlite3_realloc(X,N) and the prior allocation is freed. ** ^If sqlite3_realloc(X,N) returns NULL and N is positive, then the ** prior allocation is not freed. ** -** ^The sqlite3_realloc64(X,N) interfaces works the same as +** ^The sqlite3_realloc64(X,N) interface works the same as ** sqlite3_realloc(X,N) except that N is a 64-bit unsigned integer instead ** of a 32-bit signed integer. ** @@ -2468,19 +3275,6 @@ char *sqlite3_vsnprintf(int,char*,const char*, va_list); ** 4 byte boundary if the [SQLITE_4_BYTE_ALIGNED_MALLOC] compile-time ** option is used. ** -** In SQLite version 3.5.0 and 3.5.1, it was possible to define -** the SQLITE_OMIT_MEMORY_ALLOCATION which would cause the built-in -** implementation of these routines to be omitted. That capability -** is no longer provided. Only built-in memory allocators can be used. -** -** Prior to SQLite version 3.7.10, the Windows OS interface layer called -** the system malloc() and free() directly when converting -** filenames between the UTF-8 encoding used by SQLite -** and whatever filename encoding is used by the particular Windows -** installation. Memory allocation errors were detected, but -** they were reported back as [SQLITE_CANTOPEN] or -** [SQLITE_IOERR] rather than [SQLITE_NOMEM]. -** ** The pointer arguments to [sqlite3_free()] and [sqlite3_realloc()] ** must be either NULL or else pointers obtained from a prior ** invocation of [sqlite3_malloc()] or [sqlite3_realloc()] that have @@ -2511,7 +3305,7 @@ sqlite3_uint64 sqlite3_msize(void*); ** was last reset. ^The values returned by [sqlite3_memory_used()] and ** [sqlite3_memory_highwater()] include any overhead ** added by SQLite in its implementation of [sqlite3_malloc()], -** but not overhead added by the any underlying system library +** but not overhead added by any underlying system library ** routines that [sqlite3_malloc()] may call. ** ** ^The memory high-water mark is reset to the current value of @@ -2529,7 +3323,7 @@ sqlite3_int64 sqlite3_memory_highwater(int resetFlag); ** SQLite contains a high-quality pseudo-random number generator (PRNG) used to ** select random [ROWID | ROWIDs] when inserting new records into a table that ** already uses the largest possible [ROWID]. The PRNG is also used for -** the build-in random() and randomblob() SQL functions. This interface allows +** the built-in random() and randomblob() SQL functions. This interface allows ** applications to access the same PRNG for other purposes. ** ** ^A call to this routine stores N bytes of randomness into buffer P. @@ -2549,12 +3343,14 @@ void sqlite3_randomness(int N, void *P); /* ** CAPI3REF: Compile-Time Authorization Callbacks ** METHOD: sqlite3 +** KEYWORDS: {authorizer callback} ** ** ^This routine registers an authorizer callback with a particular ** [database connection], supplied in the first argument. ** ^The authorizer callback is invoked as SQL statements are being compiled ** by [sqlite3_prepare()] or its variants [sqlite3_prepare_v2()], -** [sqlite3_prepare16()] and [sqlite3_prepare16_v2()]. ^At various +** [sqlite3_prepare_v3()], [sqlite3_prepare16()], [sqlite3_prepare16_v2()], +** and [sqlite3_prepare16_v3()]. ^At various ** points during the compilation process, as logic is being created ** to perform various actions, the authorizer callback is invoked to ** see if those actions are allowed. ^The authorizer callback should @@ -2570,14 +3366,16 @@ void sqlite3_randomness(int N, void *P); ** requested is ok. ^When the callback returns [SQLITE_DENY], the ** [sqlite3_prepare_v2()] or equivalent call that triggered the ** authorizer will fail with an error message explaining that -** access is denied. +** access is denied. ** ** ^The first parameter to the authorizer callback is a copy of the third ** parameter to the sqlite3_set_authorizer() interface. ^The second parameter ** to the callback is an integer [SQLITE_COPY | action code] that specifies ** the particular action to be authorized. ^The third through sixth parameters -** to the callback are zero-terminated strings that contain additional -** details about the action to be authorized. +** to the callback are either NULL pointers or zero-terminated strings +** that contain additional details about the action to be authorized. +** Applications must always be prepared to encounter a NULL pointer in any +** of the third through the sixth parameters of the authorization callback. ** ** ^If the action code is [SQLITE_READ] ** and the callback returns [SQLITE_IGNORE] then the @@ -2586,6 +3384,10 @@ void sqlite3_randomness(int N, void *P); ** been read if [SQLITE_OK] had been returned. The [SQLITE_IGNORE] ** return can be used to deny an untrusted user access to individual ** columns of a table. +** ^When a table is referenced by a [SELECT] but no column values are +** extracted from that table (for example in a query like +** "SELECT count(*) FROM tab") then the [SQLITE_READ] authorizer callback +** is invoked once for that table with a column name that is an empty string. ** ^If the action code is [SQLITE_DELETE] and the callback returns ** [SQLITE_IGNORE] then the [DELETE] operation proceeds but the ** [truncate optimization] is disabled and all rows are deleted individually. @@ -2617,7 +3419,7 @@ void sqlite3_randomness(int N, void *P); ** database connections for the meaning of "modify" in this paragraph. ** ** ^When [sqlite3_prepare_v2()] is used to prepare a statement, the -** statement might be re-prepared during [sqlite3_step()] due to a +** statement might be re-prepared during [sqlite3_step()] due to a ** schema change. Hence, the application should ensure that the ** correct authorizer callback remains in place during the [sqlite3_step()]. ** @@ -2704,8 +3506,11 @@ int sqlite3_set_authorizer( #define SQLITE_RECURSIVE 33 /* NULL NULL */ /* -** CAPI3REF: Tracing And Profiling Functions -** METHOD: sqlite3 +** CAPI3REF: Deprecated Tracing And Profiling Functions +** DEPRECATED +** +** These routines are deprecated. Use the [sqlite3_trace_v2()] interface +** instead of the routines described here. ** ** These routines register callback functions that can be used for ** tracing and profiling the execution of SQL statements. @@ -2728,26 +3533,122 @@ int sqlite3_set_authorizer( ** time is in units of nanoseconds, however the current implementation ** is only capable of millisecond resolution so the six least significant ** digits in the time are meaningless. Future versions of SQLite -** might provide greater resolution on the profiler callback. The -** sqlite3_profile() function is considered experimental and is -** subject to change in future versions of SQLite. +** might provide greater resolution on the profiler callback. Invoking +** either [sqlite3_trace()] or [sqlite3_trace_v2()] will cancel the +** profile callback. */ -void *sqlite3_trace(sqlite3*, void(*xTrace)(void*,const char*), void*); -SQLITE_EXPERIMENTAL void *sqlite3_profile(sqlite3*, +SQLITE_DEPRECATED void *sqlite3_trace(sqlite3*, + void(*xTrace)(void*,const char*), void*); +SQLITE_DEPRECATED void *sqlite3_profile(sqlite3*, void(*xProfile)(void*,const char*,sqlite3_uint64), void*); +/* +** CAPI3REF: SQL Trace Event Codes +** KEYWORDS: SQLITE_TRACE +** +** These constants identify classes of events that can be monitored +** using the [sqlite3_trace_v2()] tracing logic. The M argument +** to [sqlite3_trace_v2(D,M,X,P)] is an OR-ed combination of one or more of +** the following constants. ^The first argument to the trace callback +** is one of the following constants. +** +** New tracing constants may be added in future releases. +** +** ^A trace callback has four arguments: xCallback(T,C,P,X). +** ^The T argument is one of the integer type codes above. +** ^The C argument is a copy of the context pointer passed in as the +** fourth argument to [sqlite3_trace_v2()]. +** The P and X arguments are pointers whose meanings depend on T. +** +** <dl> +** [[SQLITE_TRACE_STMT]] <dt>SQLITE_TRACE_STMT</dt> +** <dd>^An SQLITE_TRACE_STMT callback is invoked when a prepared statement +** first begins running and possibly at other times during the +** execution of the prepared statement, such as at the start of each +** trigger subprogram. ^The P argument is a pointer to the +** [prepared statement]. ^The X argument is a pointer to a string which +** is the unexpanded SQL text of the prepared statement or an SQL comment +** that indicates the invocation of a trigger. ^The callback can compute +** the same text that would have been returned by the legacy [sqlite3_trace()] +** interface by using the X argument when X begins with "--" and invoking +** [sqlite3_expanded_sql(P)] otherwise. +** +** [[SQLITE_TRACE_PROFILE]] <dt>SQLITE_TRACE_PROFILE</dt> +** <dd>^An SQLITE_TRACE_PROFILE callback provides approximately the same +** information as is provided by the [sqlite3_profile()] callback. +** ^The P argument is a pointer to the [prepared statement] and the +** X argument points to a 64-bit integer which is approximately +** the number of nanoseconds that the prepared statement took to run. +** ^The SQLITE_TRACE_PROFILE callback is invoked when the statement finishes. +** +** [[SQLITE_TRACE_ROW]] <dt>SQLITE_TRACE_ROW</dt> +** <dd>^An SQLITE_TRACE_ROW callback is invoked whenever a prepared +** statement generates a single row of result. +** ^The P argument is a pointer to the [prepared statement] and the +** X argument is unused. +** +** [[SQLITE_TRACE_CLOSE]] <dt>SQLITE_TRACE_CLOSE</dt> +** <dd>^An SQLITE_TRACE_CLOSE callback is invoked when a database +** connection closes. +** ^The P argument is a pointer to the [database connection] object +** and the X argument is unused. +** </dl> +*/ +#define SQLITE_TRACE_STMT 0x01 +#define SQLITE_TRACE_PROFILE 0x02 +#define SQLITE_TRACE_ROW 0x04 +#define SQLITE_TRACE_CLOSE 0x08 + +/* +** CAPI3REF: SQL Trace Hook +** METHOD: sqlite3 +** +** ^The sqlite3_trace_v2(D,M,X,P) interface registers a trace callback +** function X against [database connection] D, using property mask M +** and context pointer P. ^If the X callback is +** NULL or if the M mask is zero, then tracing is disabled. The +** M argument should be the bitwise OR-ed combination of +** zero or more [SQLITE_TRACE] constants. +** +** ^Each call to either sqlite3_trace(D,X,P) or sqlite3_trace_v2(D,M,X,P) +** overrides (cancels) all prior calls to sqlite3_trace(D,X,P) or +** sqlite3_trace_v2(D,M,X,P) for the [database connection] D. Each +** database connection may have at most one trace callback. +** +** ^The X callback is invoked whenever any of the events identified by +** mask M occur. ^The integer return value from the callback is currently +** ignored, though this may change in future releases. Callback +** implementations should return zero to ensure future compatibility. +** +** ^A trace callback is invoked with four arguments: callback(T,C,P,X). +** ^The T argument is one of the [SQLITE_TRACE] +** constants to indicate why the callback was invoked. +** ^The C argument is a copy of the context pointer. +** The P and X arguments are pointers whose meanings depend on T. +** +** The sqlite3_trace_v2() interface is intended to replace the legacy +** interfaces [sqlite3_trace()] and [sqlite3_profile()], both of which +** are deprecated. +*/ +int sqlite3_trace_v2( + sqlite3*, + unsigned uMask, + int(*xCallback)(unsigned,void*,void*,void*), + void *pCtx +); + /* ** CAPI3REF: Query Progress Callbacks ** METHOD: sqlite3 ** ** ^The sqlite3_progress_handler(D,N,X,P) interface causes the callback ** function X to be invoked periodically during long running calls to -** [sqlite3_exec()], [sqlite3_step()] and [sqlite3_get_table()] for +** [sqlite3_step()] and [sqlite3_prepare()] and similar for ** database connection D. An example use for this ** interface is to keep a GUI updated during a large query. ** -** ^The parameter P is passed through as the only parameter to the -** callback function X. ^The parameter N is the approximate number of +** ^The parameter P is passed through as the only parameter to the +** callback function X. ^The parameter N is the approximate number of ** [virtual machine instructions] that are evaluated between successive ** invocations of the callback X. ^If N is less than one then the progress ** handler is disabled. @@ -2767,6 +3668,13 @@ SQLITE_EXPERIMENTAL void *sqlite3_profile(sqlite3*, ** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their ** database connections for the meaning of "modify" in this paragraph. ** +** The progress handler callback would originally only be invoked from the +** bytecode engine. It still might be invoked during [sqlite3_prepare()] +** and similar because those routines might force a reparse of the schema +** which involves running the bytecode engine. However, beginning with +** SQLite version 3.41.0, the progress handler callback might also be +** invoked directly from [sqlite3_prepare()] while analyzing and generating +** code for complex queries. */ void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*); @@ -2774,7 +3682,7 @@ void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*); ** CAPI3REF: Opening A New Database Connection ** CONSTRUCTOR: sqlite3 ** -** ^These routines open an SQLite database file as specified by the +** ^These routines open an SQLite database file as specified by the ** filename argument. ^The filename argument is interpreted as UTF-8 for ** sqlite3_open() and sqlite3_open_v2() and as UTF-16 in the native byte ** order for sqlite3_open16(). ^(A [database connection] handle is usually @@ -2798,20 +3706,23 @@ void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*); ** The sqlite3_open_v2() interface works like sqlite3_open() ** except that it accepts two additional parameters for additional control ** over the new database connection. ^(The flags parameter to -** sqlite3_open_v2() can take one of -** the following three values, optionally combined with the -** [SQLITE_OPEN_NOMUTEX], [SQLITE_OPEN_FULLMUTEX], [SQLITE_OPEN_SHAREDCACHE], -** [SQLITE_OPEN_PRIVATECACHE], and/or [SQLITE_OPEN_URI] flags:)^ +** sqlite3_open_v2() must include, at a minimum, one of the following +** three flag combinations:)^ ** ** <dl> ** ^(<dt>[SQLITE_OPEN_READONLY]</dt> -** <dd>The database is opened in read-only mode. If the database does not -** already exist, an error is returned.</dd>)^ +** <dd>The database is opened in read-only mode. If the database does +** not already exist, an error is returned.</dd>)^ ** ** ^(<dt>[SQLITE_OPEN_READWRITE]</dt> -** <dd>The database is opened for reading and writing if possible, or reading -** only if the file is write protected by the operating system. In either -** case the database must already exist, otherwise an error is returned.</dd>)^ +** <dd>The database is opened for reading and writing if possible, or +** reading only if the file is write protected by the operating +** system. In either case the database must already exist, otherwise +** an error is returned. For historical reasons, if opening in +** read-write mode fails due to OS-level permissions, an attempt is +** made to open it in read-only mode. [sqlite3_db_readonly()] can be +** used to determine whether the database is actually +** read-write.</dd>)^ ** ** ^(<dt>[SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]</dt> ** <dd>The database is opened for reading and writing, and is created if @@ -2819,22 +3730,69 @@ void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*); ** sqlite3_open() and sqlite3_open16().</dd>)^ ** </dl> ** +** In addition to the required flags, the following optional flags are +** also supported: +** +** <dl> +** ^(<dt>[SQLITE_OPEN_URI]</dt> +** <dd>The filename can be interpreted as a URI if this flag is set.</dd>)^ +** +** ^(<dt>[SQLITE_OPEN_MEMORY]</dt> +** <dd>The database will be opened as an in-memory database. The database +** is named by the "filename" argument for the purposes of cache-sharing, +** if shared cache mode is enabled, but the "filename" is otherwise ignored. +** </dd>)^ +** +** ^(<dt>[SQLITE_OPEN_NOMUTEX]</dt> +** <dd>The new database connection will use the "multi-thread" +** [threading mode].)^ This means that separate threads are allowed +** to use SQLite at the same time, as long as each thread is using +** a different [database connection]. +** +** ^(<dt>[SQLITE_OPEN_FULLMUTEX]</dt> +** <dd>The new database connection will use the "serialized" +** [threading mode].)^ This means the multiple threads can safely +** attempt to use the same database connection at the same time. +** (Mutexes will block any actual concurrency, but in this mode +** there is no harm in trying.) +** +** ^(<dt>[SQLITE_OPEN_SHAREDCACHE]</dt> +** <dd>The database is opened with [shared cache] enabled, overriding +** the default shared cache setting provided by +** [sqlite3_enable_shared_cache()].)^ +** The [use of shared cache mode is discouraged] and hence shared cache +** capabilities may be omitted from many builds of SQLite. In such cases, +** this option is a no-op. +** +** ^(<dt>[SQLITE_OPEN_PRIVATECACHE]</dt> +** <dd>The database is opened with [shared cache] disabled, overriding +** the default shared cache setting provided by +** [sqlite3_enable_shared_cache()].)^ +** +** [[OPEN_EXRESCODE]] ^(<dt>[SQLITE_OPEN_EXRESCODE]</dt> +** <dd>The database connection comes up in "extended result code mode". +** In other words, the database behaves as if +** [sqlite3_extended_result_codes(db,1)] were called on the database +** connection as soon as the connection is created. In addition to setting +** the extended result code mode, this flag also causes [sqlite3_open_v2()] +** to return an extended result code.</dd> +** +** [[OPEN_NOFOLLOW]] ^(<dt>[SQLITE_OPEN_NOFOLLOW]</dt> +** <dd>The database filename is not allowed to contain a symbolic link</dd> +** </dl>)^ +** ** If the 3rd parameter to sqlite3_open_v2() is not one of the -** combinations shown above optionally combined with other +** required combinations shown above optionally combined with other ** [SQLITE_OPEN_READONLY | SQLITE_OPEN_* bits] -** then the behavior is undefined. -** -** ^If the [SQLITE_OPEN_NOMUTEX] flag is set, then the database connection -** opens in the multi-thread [threading mode] as long as the single-thread -** mode has not been set at compile-time or start-time. ^If the -** [SQLITE_OPEN_FULLMUTEX] flag is set then the database connection opens -** in the serialized [threading mode] unless single-thread was -** previously selected at compile-time or start-time. -** ^The [SQLITE_OPEN_SHAREDCACHE] flag causes the database connection to be -** eligible to use [shared cache mode], regardless of whether or not shared -** cache is enabled using [sqlite3_enable_shared_cache()]. ^The -** [SQLITE_OPEN_PRIVATECACHE] flag causes the database connection to not -** participate in [shared cache mode] even if it is enabled. +** then the behavior is undefined. Historic versions of SQLite +** have silently ignored surplus bits in the flags parameter to +** sqlite3_open_v2(), however that behavior might not be carried through +** into future versions of SQLite and so applications should not rely +** upon it. Note in particular that the SQLITE_OPEN_EXCLUSIVE flag is a no-op +** for sqlite3_open_v2(). The SQLITE_OPEN_EXCLUSIVE does *not* cause +** the open to fail if the database already exists. The SQLITE_OPEN_EXCLUSIVE +** flag is intended for use by the [sqlite3_vfs|VFS interface] only, and not +** by sqlite3_open_v2(). ** ** ^The fourth parameter to sqlite3_open_v2() is the name of the ** [sqlite3_vfs] object that defines the operating system interface that @@ -2858,26 +3816,26 @@ void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*); ** ^If [URI filename] interpretation is enabled, and the filename argument ** begins with "file:", then the filename is interpreted as a URI. ^URI ** filename interpretation is enabled if the [SQLITE_OPEN_URI] flag is -** set in the fourth argument to sqlite3_open_v2(), or if it has +** set in the third argument to sqlite3_open_v2(), or if it has ** been enabled globally using the [SQLITE_CONFIG_URI] option with the ** [sqlite3_config()] method or by the [SQLITE_USE_URI] compile-time option. -** As of SQLite version 3.7.7, URI filename interpretation is turned off +** URI filename interpretation is turned off ** by default, but future releases of SQLite might enable URI filename ** interpretation by default. See "[URI filenames]" for additional ** information. ** ** URI filenames are parsed according to RFC 3986. ^If the URI contains an -** authority, then it must be either an empty string or the string -** "localhost". ^If the authority is not an empty string or "localhost", an -** error is returned to the caller. ^The fragment component of a URI, if +** authority, then it must be either an empty string or the string +** "localhost". ^If the authority is not an empty string or "localhost", an +** error is returned to the caller. ^The fragment component of a URI, if ** present, is ignored. ** ** ^SQLite uses the path component of the URI as the name of the disk file -** which contains the database. ^If the path begins with a '/' character, -** then it is interpreted as an absolute path. ^If the path does not begin +** which contains the database. ^If the path begins with a '/' character, +** then it is interpreted as an absolute path. ^If the path does not begin ** with a '/' (meaning that the authority section is omitted from the URI) -** then the path is interpreted as a relative path. -** ^(On windows, the first component of an absolute path +** then the path is interpreted as a relative path. +** ^(On windows, the first component of an absolute path ** is a drive specification (e.g. "C:").)^ ** ** [[core URI query parameters]] @@ -2897,13 +3855,13 @@ void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*); ** ** <li> <b>mode</b>: ^(The mode parameter may be set to either "ro", "rw", ** "rwc", or "memory". Attempting to set it to any other value is -** an error)^. -** ^If "ro" is specified, then the database is opened for read-only -** access, just as if the [SQLITE_OPEN_READONLY] flag had been set in the -** third argument to sqlite3_open_v2(). ^If the mode option is set to -** "rw", then the database is opened for read-write (but not create) -** access, as if SQLITE_OPEN_READWRITE (but not SQLITE_OPEN_CREATE) had -** been set. ^Value "rwc" is equivalent to setting both +** an error)^. +** ^If "ro" is specified, then the database is opened for read-only +** access, just as if the [SQLITE_OPEN_READONLY] flag had been set in the +** third argument to sqlite3_open_v2(). ^If the mode option is set to +** "rw", then the database is opened for read-write (but not create) +** access, as if SQLITE_OPEN_READWRITE (but not SQLITE_OPEN_CREATE) had +** been set. ^Value "rwc" is equivalent to setting both ** SQLITE_OPEN_READWRITE and SQLITE_OPEN_CREATE. ^If the mode option is ** set to "memory" then a pure [in-memory database] that never reads ** or writes from disk is used. ^It is an error to specify a value for @@ -2913,7 +3871,7 @@ void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*); ** <li> <b>cache</b>: ^The cache parameter may be set to either "shared" or ** "private". ^Setting it to "shared" is equivalent to setting the ** SQLITE_OPEN_SHAREDCACHE bit in the flags argument passed to -** sqlite3_open_v2(). ^Setting the cache parameter to "private" is +** sqlite3_open_v2(). ^Setting the cache parameter to "private" is ** equivalent to setting the SQLITE_OPEN_PRIVATECACHE bit. ** ^If sqlite3_open_v2() is used and the "cache" parameter is present in ** a URI filename, its value overrides any behavior requested by setting @@ -2939,7 +3897,7 @@ void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*); ** property on a database file that does in fact change can result ** in incorrect query results and/or [SQLITE_CORRUPT] errors. ** See also: [SQLITE_IOCAP_IMMUTABLE]. -** +** ** </ul> ** ** ^Specifying an unknown parameter in the query component of a URI is not an @@ -2951,36 +3909,37 @@ void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*); ** ** <table border="1" align=center cellpadding=5> ** <tr><th> URI filenames <th> Results -** <tr><td> file:data.db <td> +** <tr><td> file:data.db <td> ** Open the file "data.db" in the current directory. ** <tr><td> file:/home/fred/data.db<br> -** file:///home/fred/data.db <br> -** file://localhost/home/fred/data.db <br> <td> +** file:///home/fred/data.db <br> +** file://localhost/home/fred/data.db <br> <td> ** Open the database file "/home/fred/data.db". -** <tr><td> file://darkstar/home/fred/data.db <td> +** <tr><td> file://darkstar/home/fred/data.db <td> ** An error. "darkstar" is not a recognized authority. -** <tr><td style="white-space:nowrap"> +** <tr><td style="white-space:nowrap"> ** file:///C:/Documents%20and%20Settings/fred/Desktop/data.db ** <td> Windows only: Open the file "data.db" on fred's desktop on drive -** C:. Note that the %20 escaping in this example is not strictly +** C:. Note that the %20 escaping in this example is not strictly ** necessary - space characters can be used literally ** in URI filenames. -** <tr><td> file:data.db?mode=ro&cache=private <td> +** <tr><td> file:data.db?mode=ro&cache=private <td> ** Open file "data.db" in the current directory for read-only access. ** Regardless of whether or not shared-cache mode is enabled by ** default, use a private cache. ** <tr><td> file:/home/fred/data.db?vfs=unix-dotfile <td> ** Open file "/home/fred/data.db". Use the special VFS "unix-dotfile" ** that uses dot-files in place of posix advisory locking. -** <tr><td> file:data.db?mode=readonly <td> +** <tr><td> file:data.db?mode=readonly <td> ** An error. "readonly" is not a valid option for the "mode" parameter. +** Use "ro" instead: "file:data.db?mode=ro". ** </table> ** ** ^URI hexadecimal escape sequences (%HH) are supported within the path and ** query components of a URI. A hexadecimal escape sequence consists of a -** percent sign - "%" - followed by exactly two hexadecimal digits +** percent sign - "%" - followed by exactly two hexadecimal digits ** specifying an octet value. ^Before the path or query components of a -** URI filename are interpreted, they are encoded using UTF-8 and all +** URI filename are interpreted, they are encoded using UTF-8 and all ** hexadecimal escape sequences replaced by a single byte containing the ** corresponding octet. If this process generates an invalid UTF-8 encoding, ** the results are undefined. @@ -3015,17 +3974,27 @@ int sqlite3_open_v2( /* ** CAPI3REF: Obtain Values For URI Parameters ** -** These are utility routines, useful to VFS implementations, that check -** to see if a database file was a URI that contained a specific query +** These are utility routines, useful to [VFS|custom VFS implementations], +** that check if a database file was a URI that contained a specific query ** parameter, and if so obtains the value of that query parameter. ** -** If F is the database filename pointer passed into the xOpen() method of -** a VFS implementation when the flags parameter to xOpen() has one or -** more of the [SQLITE_OPEN_URI] or [SQLITE_OPEN_MAIN_DB] bits set and -** P is the name of the query parameter, then +** The first parameter to these interfaces (hereafter referred to +** as F) must be one of: +** <ul> +** <li> A database filename pointer created by the SQLite core and +** passed into the xOpen() method of a VFS implementation, or +** <li> A filename obtained from [sqlite3_db_filename()], or +** <li> A new filename constructed using [sqlite3_create_filename()]. +** </ul> +** If the F parameter is not one of the above, then the behavior is +** undefined and probably undesirable. Older versions of SQLite were +** more tolerant of invalid F parameters than newer versions. +** +** If F is a suitable filename (as described in the previous paragraph) +** and if P is the name of the query parameter, then ** sqlite3_uri_parameter(F,P) returns the value of the P -** parameter if it exists or a NULL pointer if P does not appear as a -** query parameter on F. If P is a query parameter of F +** parameter if it exists or a NULL pointer if P does not appear as a +** query parameter on F. If P is a query parameter of F and it ** has no explicit value, then sqlite3_uri_parameter(F,P) returns ** a pointer to an empty string. ** @@ -3033,56 +4002,199 @@ int sqlite3_open_v2( ** parameter and returns true (1) or false (0) according to the value ** of P. The sqlite3_uri_boolean(F,P,B) routine returns true (1) if the ** value of query parameter P is one of "yes", "true", or "on" in any -** case or if the value begins with a non-zero number. The +** case or if the value begins with a non-zero number. The ** sqlite3_uri_boolean(F,P,B) routines returns false (0) if the value of ** query parameter P is one of "no", "false", or "off" in any case or ** if the value begins with a numeric zero. If P is not a query -** parameter on F or if the value of P is does not match any of the +** parameter on F or if the value of P does not match any of the ** above, then sqlite3_uri_boolean(F,P,B) returns (B!=0). ** ** The sqlite3_uri_int64(F,P,D) routine converts the value of P into a ** 64-bit signed integer and returns that integer, or D if P does not ** exist. If the value of P is something other than an integer, then ** zero is returned. -** +** +** The sqlite3_uri_key(F,N) returns a pointer to the name (not +** the value) of the N-th query parameter for filename F, or a NULL +** pointer if N is less than zero or greater than the number of query +** parameters minus 1. The N value is zero-based so N should be 0 to obtain +** the name of the first query parameter, 1 for the second parameter, and +** so forth. +** ** If F is a NULL pointer, then sqlite3_uri_parameter(F,P) returns NULL and ** sqlite3_uri_boolean(F,P,B) returns B. If F is not a NULL pointer and -** is not a database file pathname pointer that SQLite passed into the xOpen -** VFS method, then the behavior of this routine is undefined and probably -** undesirable. +** is not a database file pathname pointer that the SQLite core passed +** into the xOpen VFS method, then the behavior of this routine is undefined +** and probably undesirable. +** +** Beginning with SQLite [version 3.31.0] ([dateof:3.31.0]) the input F +** parameter can also be the name of a rollback journal file or WAL file +** in addition to the main database file. Prior to version 3.31.0, these +** routines would only work if F was the name of the main database file. +** When the F parameter is the name of the rollback journal or WAL file, +** it has access to all the same query parameters as were found on the +** main database file. +** +** See the [URI filename] documentation for additional information. +*/ +const char *sqlite3_uri_parameter(sqlite3_filename z, const char *zParam); +int sqlite3_uri_boolean(sqlite3_filename z, const char *zParam, int bDefault); +sqlite3_int64 sqlite3_uri_int64(sqlite3_filename, const char*, sqlite3_int64); +const char *sqlite3_uri_key(sqlite3_filename z, int N); + +/* +** CAPI3REF: Translate filenames +** +** These routines are available to [VFS|custom VFS implementations] for +** translating filenames between the main database file, the journal file, +** and the WAL file. +** +** If F is the name of an sqlite database file, journal file, or WAL file +** passed by the SQLite core into the VFS, then sqlite3_filename_database(F) +** returns the name of the corresponding database file. +** +** If F is the name of an sqlite database file, journal file, or WAL file +** passed by the SQLite core into the VFS, or if F is a database filename +** obtained from [sqlite3_db_filename()], then sqlite3_filename_journal(F) +** returns the name of the corresponding rollback journal file. +** +** If F is the name of an sqlite database file, journal file, or WAL file +** that was passed by the SQLite core into the VFS, or if F is a database +** filename obtained from [sqlite3_db_filename()], then +** sqlite3_filename_wal(F) returns the name of the corresponding +** WAL file. +** +** In all of the above, if F is not the name of a database, journal or WAL +** filename passed into the VFS from the SQLite core and F is not the +** return value from [sqlite3_db_filename()], then the result is +** undefined and is likely a memory access violation. +*/ +const char *sqlite3_filename_database(sqlite3_filename); +const char *sqlite3_filename_journal(sqlite3_filename); +const char *sqlite3_filename_wal(sqlite3_filename); + +/* +** CAPI3REF: Database File Corresponding To A Journal +** +** ^If X is the name of a rollback or WAL-mode journal file that is +** passed into the xOpen method of [sqlite3_vfs], then +** sqlite3_database_file_object(X) returns a pointer to the [sqlite3_file] +** object that represents the main database file. +** +** This routine is intended for use in custom [VFS] implementations +** only. It is not a general-purpose interface. +** The argument sqlite3_file_object(X) must be a filename pointer that +** has been passed into [sqlite3_vfs].xOpen method where the +** flags parameter to xOpen contains one of the bits +** [SQLITE_OPEN_MAIN_JOURNAL] or [SQLITE_OPEN_WAL]. Any other use +** of this routine results in undefined and probably undesirable +** behavior. */ -const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam); -int sqlite3_uri_boolean(const char *zFile, const char *zParam, int bDefault); -sqlite3_int64 sqlite3_uri_int64(const char*, const char*, sqlite3_int64); +sqlite3_file *sqlite3_database_file_object(const char*); +/* +** CAPI3REF: Create and Destroy VFS Filenames +** +** These interfaces are provided for use by [VFS shim] implementations and +** are not useful outside of that context. +** +** The sqlite3_create_filename(D,J,W,N,P) allocates memory to hold a version of +** database filename D with corresponding journal file J and WAL file W and +** an array P of N URI Key/Value pairs. The result from +** sqlite3_create_filename(D,J,W,N,P) is a pointer to a database filename that +** is safe to pass to routines like: +** <ul> +** <li> [sqlite3_uri_parameter()], +** <li> [sqlite3_uri_boolean()], +** <li> [sqlite3_uri_int64()], +** <li> [sqlite3_uri_key()], +** <li> [sqlite3_filename_database()], +** <li> [sqlite3_filename_journal()], or +** <li> [sqlite3_filename_wal()]. +** </ul> +** If a memory allocation error occurs, sqlite3_create_filename() might +** return a NULL pointer. The memory obtained from sqlite3_create_filename(X) +** must be released by a corresponding call to sqlite3_free_filename(Y). +** +** The P parameter in sqlite3_create_filename(D,J,W,N,P) should be an array +** of 2*N pointers to strings. Each pair of pointers in this array corresponds +** to a key and value for a query parameter. The P parameter may be a NULL +** pointer if N is zero. None of the 2*N pointers in the P array may be +** NULL pointers and key pointers should not be empty strings. +** None of the D, J, or W parameters to sqlite3_create_filename(D,J,W,N,P) may +** be NULL pointers, though they can be empty strings. +** +** The sqlite3_free_filename(Y) routine releases a memory allocation +** previously obtained from sqlite3_create_filename(). Invoking +** sqlite3_free_filename(Y) where Y is a NULL pointer is a harmless no-op. +** +** If the Y parameter to sqlite3_free_filename(Y) is anything other +** than a NULL pointer or a pointer previously acquired from +** sqlite3_create_filename(), then bad things such as heap +** corruption or segfaults may occur. The value Y should not be +** used again after sqlite3_free_filename(Y) has been called. This means +** that if the [sqlite3_vfs.xOpen()] method of a VFS has been called using Y, +** then the corresponding [sqlite3_module.xClose() method should also be +** invoked prior to calling sqlite3_free_filename(Y). +*/ +sqlite3_filename sqlite3_create_filename( + const char *zDatabase, + const char *zJournal, + const char *zWal, + int nParam, + const char **azParam +); +void sqlite3_free_filename(sqlite3_filename); /* ** CAPI3REF: Error Codes And Messages ** METHOD: sqlite3 ** -** ^If the most recent sqlite3_* API call associated with +** ^If the most recent sqlite3_* API call associated with ** [database connection] D failed, then the sqlite3_errcode(D) interface ** returns the numeric [result code] or [extended result code] for that ** API call. -** If the most recent API call was successful, -** then the return value from sqlite3_errcode() is undefined. ** ^The sqlite3_extended_errcode() -** interface is the same except that it always returns the +** interface is the same except that it always returns the ** [extended result code] even when extended result codes are ** disabled. ** +** The values returned by sqlite3_errcode() and/or +** sqlite3_extended_errcode() might change with each API call. +** Except, there are some interfaces that are guaranteed to never +** change the value of the error code. The error-code preserving +** interfaces include the following: +** +** <ul> +** <li> sqlite3_errcode() +** <li> sqlite3_extended_errcode() +** <li> sqlite3_errmsg() +** <li> sqlite3_errmsg16() +** <li> sqlite3_error_offset() +** </ul> +** ** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language -** text that describes the error, as either UTF-8 or UTF-16 respectively. +** text that describes the error, as either UTF-8 or UTF-16 respectively, +** or NULL if no error message is available. +** (See how SQLite handles [invalid UTF] for exceptions to this rule.) ** ^(Memory to hold the error message string is managed internally. ** The application does not need to worry about freeing the result. ** However, the error string might be overwritten or deallocated by ** subsequent calls to other SQLite interface functions.)^ ** -** ^The sqlite3_errstr() interface returns the English-language text -** that describes the [result code], as UTF-8. +** ^The sqlite3_errstr(E) interface returns the English-language text +** that describes the [result code] E, as UTF-8, or NULL if E is not a +** result code for which a text error message is available. ** ^(Memory to hold the error message string is managed internally ** and must not be freed by the application)^. ** +** ^If the most recent error references a specific token in the input +** SQL, the sqlite3_error_offset() interface returns the byte offset +** of the start of that token. ^The byte offset returned by +** sqlite3_error_offset() assumes that the input SQL is UTF-8. +** ^If the most recent error does not reference a specific token in the input +** SQL, then the sqlite3_error_offset() function returns -1. +** ** When the serialized [threading mode] is in use, it might be the ** case that a second error occurs on a separate thread in between ** the time of the first error and the call to these interfaces. @@ -3102,6 +4214,35 @@ int sqlite3_extended_errcode(sqlite3 *db); const char *sqlite3_errmsg(sqlite3*); const void *sqlite3_errmsg16(sqlite3*); const char *sqlite3_errstr(int); +int sqlite3_error_offset(sqlite3 *db); + +/* +** CAPI3REF: Set Error Codes And Message +** METHOD: sqlite3 +** +** Set the error code of the database handle passed as the first argument +** to errcode, and the error message to a copy of nul-terminated string +** zErrMsg. If zErrMsg is passed NULL, then the error message is set to +** the default message associated with the supplied error code. Subsequent +** calls to [sqlite3_errcode()] and [sqlite3_errmsg()] and similar will +** return the values set by this routine in place of what was previously +** set by SQLite itself. +** +** This function returns SQLITE_OK if the error code and error message are +** successfully set, SQLITE_NOMEM if an OOM occurs, and SQLITE_MISUSE if +** the database handle is NULL or invalid. +** +** The error code and message set by this routine remains in effect until +** they are changed, either by another call to this routine or until they are +** changed to by SQLite itself to reflect the result of some subsquent +** API call. +** +** This function is intended for use by SQLite extensions or wrappers. The +** idea is that an extension or wrapper can use this routine to set error +** messages and error codes and thus behave more like a core SQLite +** feature from the point of view of an application. +*/ +int sqlite3_set_errmsg(sqlite3 *db, int errcode, const char *zErrMsg); /* ** CAPI3REF: Prepared Statement Object @@ -3111,7 +4252,7 @@ const char *sqlite3_errstr(int); ** has been compiled into binary form and is ready to be evaluated. ** ** Think of each SQL statement as a separate computer program. The -** original SQL text is source code. A prepared statement object +** original SQL text is source code. A prepared statement object ** is the compiled object code. All SQL must be converted into a ** prepared statement before it can be run. ** @@ -3141,7 +4282,7 @@ typedef struct sqlite3_stmt sqlite3_stmt; ** new limit for that construct.)^ ** ** ^If the new limit is a negative number, the limit is unchanged. -** ^(For each limit category SQLITE_LIMIT_<i>NAME</i> there is a +** ^(For each limit category SQLITE_LIMIT_<i>NAME</i> there is a ** [limits | hard upper bound] ** set at compile-time by a C preprocessor macro called ** [limits | SQLITE_MAX_<i>NAME</i>]. @@ -3149,7 +4290,7 @@ typedef struct sqlite3_stmt sqlite3_stmt; ** ^Attempts to increase a limit above its hard upper bound are ** silently truncated to the hard upper bound. ** -** ^Regardless of whether or not the limit was changed, the +** ^Regardless of whether or not the limit was changed, the ** [sqlite3_limit()] interface returns the prior value of the limit. ** ^Hence, to find the current value of a limit without changing it, ** simply invoke this interface with the third parameter set to -1. @@ -3177,8 +4318,8 @@ int sqlite3_limit(sqlite3*, int id, int newVal); ** ** These constants define various performance limits ** that can be lowered at run-time using [sqlite3_limit()]. -** The synopsis of the meanings of the various limits is shown below. -** Additional information is available at [limits | Limits in SQLite]. +** A concise description of these limits follows, and additional information +** is available at [limits | Limits in SQLite]. ** ** <dl> ** [[SQLITE_LIMIT_LENGTH]] ^(<dt>SQLITE_LIMIT_LENGTH</dt> @@ -3200,9 +4341,9 @@ int sqlite3_limit(sqlite3*, int id, int newVal); ** ** [[SQLITE_LIMIT_VDBE_OP]] ^(<dt>SQLITE_LIMIT_VDBE_OP</dt> ** <dd>The maximum number of instructions in a virtual machine program -** used to implement an SQL statement. This limit is not currently -** enforced, though that might be added in some future release of -** SQLite.</dd>)^ +** used to implement an SQL statement. If [sqlite3_prepare_v2()] or +** the equivalent tries to allocate space for more than this many opcodes +** in a single prepared statement, an SQLITE_NOMEM error is returned.</dd>)^ ** ** [[SQLITE_LIMIT_FUNCTION_ARG]] ^(<dt>SQLITE_LIMIT_FUNCTION_ARG</dt> ** <dd>The maximum number of arguments on a function.</dd>)^ @@ -3240,32 +4381,98 @@ int sqlite3_limit(sqlite3*, int id, int newVal); #define SQLITE_LIMIT_TRIGGER_DEPTH 10 #define SQLITE_LIMIT_WORKER_THREADS 11 +/* +** CAPI3REF: Prepare Flags +** +** These constants define various flags that can be passed into the +** "prepFlags" parameter of the [sqlite3_prepare_v3()] and +** [sqlite3_prepare16_v3()] interfaces. +** +** New flags may be added in future releases of SQLite. +** +** <dl> +** [[SQLITE_PREPARE_PERSISTENT]] ^(<dt>SQLITE_PREPARE_PERSISTENT</dt> +** <dd>The SQLITE_PREPARE_PERSISTENT flag is a hint to the query planner +** that the prepared statement will be retained for a long time and +** probably reused many times.)^ ^Without this flag, [sqlite3_prepare_v3()] +** and [sqlite3_prepare16_v3()] assume that the prepared statement will +** be used just once or at most a few times and then destroyed using +** [sqlite3_finalize()] relatively soon. The current implementation acts +** on this hint by avoiding the use of [lookaside memory] so as not to +** deplete the limited store of lookaside memory. Future versions of +** SQLite may act on this hint differently. +** +** [[SQLITE_PREPARE_NORMALIZE]] <dt>SQLITE_PREPARE_NORMALIZE</dt> +** <dd>The SQLITE_PREPARE_NORMALIZE flag is a no-op. This flag used +** to be required for any prepared statement that wanted to use the +** [sqlite3_normalized_sql()] interface. However, the +** [sqlite3_normalized_sql()] interface is now available to all +** prepared statements, regardless of whether or not they use this +** flag. +** +** [[SQLITE_PREPARE_NO_VTAB]] <dt>SQLITE_PREPARE_NO_VTAB</dt> +** <dd>The SQLITE_PREPARE_NO_VTAB flag causes the SQL compiler +** to return an error (error code SQLITE_ERROR) if the statement uses +** any virtual tables. +** +** [[SQLITE_PREPARE_DONT_LOG]] <dt>SQLITE_PREPARE_DONT_LOG</dt> +** <dd>The SQLITE_PREPARE_DONT_LOG flag prevents SQL compiler +** errors from being sent to the error log defined by +** [SQLITE_CONFIG_LOG]. This can be used, for example, to do test +** compiles to see if some SQL syntax is well-formed, without generating +** messages on the global error log when it is not. If the test compile +** fails, the sqlite3_prepare_v3() call returns the same error indications +** with or without this flag; it just omits the call to [sqlite3_log()] that +** logs the error. +** </dl> +*/ +#define SQLITE_PREPARE_PERSISTENT 0x01 +#define SQLITE_PREPARE_NORMALIZE 0x02 +#define SQLITE_PREPARE_NO_VTAB 0x04 +#define SQLITE_PREPARE_DONT_LOG 0x10 + /* ** CAPI3REF: Compiling An SQL Statement ** KEYWORDS: {SQL statement compiler} ** METHOD: sqlite3 ** CONSTRUCTOR: sqlite3_stmt ** -** To execute an SQL query, it must first be compiled into a byte-code -** program using one of these routines. +** To execute an SQL statement, it must first be compiled into a byte-code +** program using one of these routines. Or, in other words, these routines +** are constructors for the [prepared statement] object. +** +** The preferred routine to use is [sqlite3_prepare_v2()]. The +** [sqlite3_prepare()] interface is legacy and should be avoided. +** [sqlite3_prepare_v3()] has an extra "prepFlags" option that is used +** for special purposes. +** +** The use of the UTF-8 interfaces is preferred, as SQLite currently +** does all parsing using UTF-8. The UTF-16 interfaces are provided +** as a convenience. The UTF-16 interfaces work by converting the +** input text into UTF-8, then invoking the corresponding UTF-8 interface. ** ** The first argument, "db", is a [database connection] obtained from a ** prior successful call to [sqlite3_open()], [sqlite3_open_v2()] or ** [sqlite3_open16()]. The database connection must not have been closed. ** ** The second argument, "zSql", is the statement to be compiled, encoded -** as either UTF-8 or UTF-16. The sqlite3_prepare() and sqlite3_prepare_v2() -** interfaces use UTF-8, and sqlite3_prepare16() and sqlite3_prepare16_v2() -** use UTF-16. +** as either UTF-8 or UTF-16. The sqlite3_prepare(), sqlite3_prepare_v2(), +** and sqlite3_prepare_v3() +** interfaces use UTF-8, and sqlite3_prepare16(), sqlite3_prepare16_v2(), +** and sqlite3_prepare16_v3() use UTF-16. ** ** ^If the nByte argument is negative, then zSql is read up to the -** first zero terminator. ^If nByte is positive, then it is the -** number of bytes read from zSql. ^If nByte is zero, then no prepared +** first zero terminator. ^If nByte is positive, then it is the maximum +** number of bytes read from zSql. When nByte is positive, zSql is read +** up to the first zero terminator or until the nByte bytes have been read, +** whichever comes first. ^If nByte is zero, then no prepared ** statement is generated. ** If the caller knows that the supplied string is nul-terminated, then ** there is a small performance advantage to passing an nByte parameter that ** is the number of bytes in the input string <i>including</i> ** the nul-terminator. +** Note that nByte measures the length of the input in bytes, not +** characters, even for the UTF-16 interfaces. ** ** ^If pzTail is not NULL then *pzTail is made to point to the first byte ** past the end of the first SQL statement in zSql. These routines only @@ -3283,10 +4490,11 @@ int sqlite3_limit(sqlite3*, int id, int newVal); ** ^On success, the sqlite3_prepare() family of routines return [SQLITE_OK]; ** otherwise an [error code] is returned. ** -** The sqlite3_prepare_v2() and sqlite3_prepare16_v2() interfaces are -** recommended for all new programs. The two older interfaces are retained -** for backwards compatibility, but their use is discouraged. -** ^In the "v2" interfaces, the prepared statement +** The sqlite3_prepare_v2(), sqlite3_prepare_v3(), sqlite3_prepare16_v2(), +** and sqlite3_prepare16_v3() interfaces are recommended for all new programs. +** The older interfaces (sqlite3_prepare() and sqlite3_prepare16()) +** are retained for backwards compatibility, but their use is discouraged. +** ^In the "vX" interfaces, the prepared statement ** that is returned (the [sqlite3_stmt] object) contains a copy of the ** original SQL text. This causes the [sqlite3_step()] interface to ** behave differently in three ways: @@ -3309,17 +4517,23 @@ int sqlite3_limit(sqlite3*, int id, int newVal); ** </li> ** ** <li> -** ^If the specific value bound to [parameter | host parameter] in the +** ^If the specific value bound to a [parameter | host parameter] in the ** WHERE clause might influence the choice of query plan for a statement, -** then the statement will be automatically recompiled, as if there had been -** a schema change, on the first [sqlite3_step()] call following any change -** to the [sqlite3_bind_text | bindings] of that [parameter]. -** ^The specific value of WHERE-clause [parameter] might influence the +** then the statement will be automatically recompiled, as if there had been +** a schema change, on the first [sqlite3_step()] call following any change +** to the [sqlite3_bind_text | bindings] of that [parameter]. +** ^The specific value of a WHERE-clause [parameter] might influence the ** choice of query plan if the parameter is the left-hand side of a [LIKE] ** or [GLOB] operator or if the parameter is compared to an indexed column -** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled. +** and the [SQLITE_ENABLE_STAT4] compile-time option is enabled. ** </li> ** </ol> +** +** <p>^sqlite3_prepare_v3() differs from sqlite3_prepare_v2() only in having +** the extra prepFlags parameter, which is a bit array consisting of zero or +** more of the [SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_*] flags. ^The +** sqlite3_prepare_v2() interface works exactly the same as +** sqlite3_prepare_v3() with a zero prepFlags parameter. */ int sqlite3_prepare( sqlite3 *db, /* Database handle */ @@ -3335,6 +4549,14 @@ int sqlite3_prepare_v2( sqlite3_stmt **ppStmt, /* OUT: Statement handle */ const char **pzTail /* OUT: Pointer to unused portion of zSql */ ); +int sqlite3_prepare_v3( + sqlite3 *db, /* Database handle */ + const char *zSql, /* SQL statement, UTF-8 encoded */ + int nByte, /* Maximum length of zSql in bytes. */ + unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_ flags */ + sqlite3_stmt **ppStmt, /* OUT: Statement handle */ + const char **pzTail /* OUT: Pointer to unused portion of zSql */ +); int sqlite3_prepare16( sqlite3 *db, /* Database handle */ const void *zSql, /* SQL statement, UTF-16 encoded */ @@ -3349,16 +4571,61 @@ int sqlite3_prepare16_v2( sqlite3_stmt **ppStmt, /* OUT: Statement handle */ const void **pzTail /* OUT: Pointer to unused portion of zSql */ ); +int sqlite3_prepare16_v3( + sqlite3 *db, /* Database handle */ + const void *zSql, /* SQL statement, UTF-16 encoded */ + int nByte, /* Maximum length of zSql in bytes. */ + unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_ flags */ + sqlite3_stmt **ppStmt, /* OUT: Statement handle */ + const void **pzTail /* OUT: Pointer to unused portion of zSql */ +); /* ** CAPI3REF: Retrieving Statement SQL ** METHOD: sqlite3_stmt ** -** ^This interface can be used to retrieve a saved copy of the original -** SQL text used to create a [prepared statement] if that statement was -** compiled using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()]. +** ^The sqlite3_sql(P) interface returns a pointer to a copy of the UTF-8 +** SQL text used to create [prepared statement] P if P was +** created by [sqlite3_prepare_v2()], [sqlite3_prepare_v3()], +** [sqlite3_prepare16_v2()], or [sqlite3_prepare16_v3()]. +** ^The sqlite3_expanded_sql(P) interface returns a pointer to a UTF-8 +** string containing the SQL text of prepared statement P with +** [bound parameters] expanded. +** ^The sqlite3_normalized_sql(P) interface returns a pointer to a UTF-8 +** string containing the normalized SQL text of prepared statement P. The +** semantics used to normalize a SQL statement are unspecified and subject +** to change. At a minimum, literal values will be replaced with suitable +** placeholders. +** +** ^(For example, if a prepared statement is created using the SQL +** text "SELECT $abc,:xyz" and if parameter $abc is bound to integer 2345 +** and parameter :xyz is unbound, then sqlite3_sql() will return +** the original string, "SELECT $abc,:xyz" but sqlite3_expanded_sql() +** will return "SELECT 2345,NULL".)^ +** +** ^The sqlite3_expanded_sql() interface returns NULL if insufficient memory +** is available to hold the result, or if the result would exceed the +** maximum string length determined by the [SQLITE_LIMIT_LENGTH]. +** +** ^The [SQLITE_TRACE_SIZE_LIMIT] compile-time option limits the size of +** bound parameter expansions. ^The [SQLITE_OMIT_TRACE] compile-time +** option causes sqlite3_expanded_sql() to always return NULL. +** +** ^The strings returned by sqlite3_sql(P) and sqlite3_normalized_sql(P) +** are managed by SQLite and are automatically freed when the prepared +** statement is finalized. +** ^The string returned by sqlite3_expanded_sql(P), on the other hand, +** is obtained from [sqlite3_malloc()] and must be freed by the application +** by passing it to [sqlite3_free()]. +** +** ^The sqlite3_normalized_sql() interface is only available if +** the [SQLITE_ENABLE_NORMALIZE] compile-time option is defined. */ const char *sqlite3_sql(sqlite3_stmt *pStmt); +char *sqlite3_expanded_sql(sqlite3_stmt *pStmt); +#ifdef SQLITE_ENABLE_NORMALIZE +const char *sqlite3_normalized_sql(sqlite3_stmt *pStmt); +#endif /* ** CAPI3REF: Determine If An SQL Statement Writes The Database @@ -3369,8 +4636,8 @@ const char *sqlite3_sql(sqlite3_stmt *pStmt); ** the content of the database file. ** ** Note that [application-defined SQL functions] or -** [virtual tables] might change the database indirectly as a side effect. -** ^(For example, if an application defines a function "eval()" that +** [virtual tables] might change the database indirectly as a side effect. +** ^(For example, if an application defines a function "eval()" that ** calls [sqlite3_exec()], then the following SQL statement would ** change the database file through side-effects: ** @@ -3384,31 +4651,95 @@ const char *sqlite3_sql(sqlite3_stmt *pStmt); ** ^Transaction control statements such as [BEGIN], [COMMIT], [ROLLBACK], ** [SAVEPOINT], and [RELEASE] cause sqlite3_stmt_readonly() to return true, ** since the statements themselves do not actually modify the database but -** rather they control the timing of when other statements modify the +** rather they control the timing of when other statements modify the ** database. ^The [ATTACH] and [DETACH] statements also cause ** sqlite3_stmt_readonly() to return true since, while those statements -** change the configuration of a database connection, they do not make +** change the configuration of a database connection, they do not make ** changes to the content of the database files on disk. +** ^The sqlite3_stmt_readonly() interface returns true for [BEGIN] since +** [BEGIN] merely sets internal flags, but the [BEGIN|BEGIN IMMEDIATE] and +** [BEGIN|BEGIN EXCLUSIVE] commands do touch the database and so +** sqlite3_stmt_readonly() returns false for those commands. +** +** ^This routine returns false if there is any possibility that the +** statement might change the database file. ^A false return does +** not guarantee that the statement will change the database file. +** ^For example, an UPDATE statement might have a WHERE clause that +** makes it a no-op, but the sqlite3_stmt_readonly() result would still +** be false. ^Similarly, a CREATE TABLE IF NOT EXISTS statement is a +** read-only no-op if the table already exists, but +** sqlite3_stmt_readonly() still returns false for such a statement. +** +** ^If prepared statement X is an [EXPLAIN] or [EXPLAIN QUERY PLAN] +** statement, then sqlite3_stmt_readonly(X) returns the same value as +** if the EXPLAIN or EXPLAIN QUERY PLAN prefix were omitted. */ int sqlite3_stmt_readonly(sqlite3_stmt *pStmt); /* -** CAPI3REF: Determine If A Prepared Statement Has Been Reset +** CAPI3REF: Query The EXPLAIN Setting For A Prepared Statement ** METHOD: sqlite3_stmt ** -** ^The sqlite3_stmt_busy(S) interface returns true (non-zero) if the -** [prepared statement] S has been stepped at least once using -** [sqlite3_step(S)] but has neither run to completion (returned -** [SQLITE_DONE] from [sqlite3_step(S)]) nor +** ^The sqlite3_stmt_isexplain(S) interface returns 1 if the +** prepared statement S is an EXPLAIN statement, or 2 if the +** statement S is an EXPLAIN QUERY PLAN. +** ^The sqlite3_stmt_isexplain(S) interface returns 0 if S is +** an ordinary statement or a NULL pointer. +*/ +int sqlite3_stmt_isexplain(sqlite3_stmt *pStmt); + +/* +** CAPI3REF: Change The EXPLAIN Setting For A Prepared Statement +** METHOD: sqlite3_stmt +** +** The sqlite3_stmt_explain(S,E) interface changes the EXPLAIN +** setting for [prepared statement] S. If E is zero, then S becomes +** a normal prepared statement. If E is 1, then S behaves as if +** its SQL text began with "[EXPLAIN]". If E is 2, then S behaves as if +** its SQL text began with "[EXPLAIN QUERY PLAN]". +** +** Calling sqlite3_stmt_explain(S,E) might cause S to be reprepared. +** SQLite tries to avoid a reprepare, but a reprepare might be necessary +** on the first transition into EXPLAIN or EXPLAIN QUERY PLAN mode. +** +** Because of the potential need to reprepare, a call to +** sqlite3_stmt_explain(S,E) will fail with SQLITE_ERROR if S cannot be +** reprepared because it was created using [sqlite3_prepare()] instead of +** the newer [sqlite3_prepare_v2()] or [sqlite3_prepare_v3()] interfaces and +** hence has no saved SQL text with which to reprepare. +** +** Changing the explain setting for a prepared statement does not change +** the original SQL text for the statement. Hence, if the SQL text originally +** began with EXPLAIN or EXPLAIN QUERY PLAN, but sqlite3_stmt_explain(S,0) +** is called to convert the statement into an ordinary statement, the EXPLAIN +** or EXPLAIN QUERY PLAN keywords will still appear in the sqlite3_sql(S) +** output, even though the statement now acts like a normal SQL statement. +** +** This routine returns SQLITE_OK if the explain mode is successfully +** changed, or an error code if the explain mode could not be changed. +** The explain mode cannot be changed while a statement is active. +** Hence, it is good practice to call [sqlite3_reset(S)] +** immediately prior to calling sqlite3_stmt_explain(S,E). +*/ +int sqlite3_stmt_explain(sqlite3_stmt *pStmt, int eMode); + +/* +** CAPI3REF: Determine If A Prepared Statement Has Been Reset +** METHOD: sqlite3_stmt +** +** ^The sqlite3_stmt_busy(S) interface returns true (non-zero) if the +** [prepared statement] S has been stepped at least once using +** [sqlite3_step(S)] but has neither run to completion (returned +** [SQLITE_DONE] from [sqlite3_step(S)]) nor ** been reset using [sqlite3_reset(S)]. ^The sqlite3_stmt_busy(S) -** interface returns false if S is a NULL pointer. If S is not a +** interface returns false if S is a NULL pointer. If S is not a ** NULL pointer and is not a pointer to a valid [prepared statement] ** object, then the behavior is undefined and probably undesirable. ** ** This interface can be used in combination [sqlite3_next_stmt()] -** to locate all prepared statements associated with a database +** to locate all prepared statements associated with a database ** connection that are in need of being reset. This can be used, -** for example, in diagnostic routines to search for prepared +** for example, in diagnostic routines to search for prepared ** statements that are holding a transaction open. */ int sqlite3_stmt_busy(sqlite3_stmt*); @@ -3427,7 +4758,7 @@ int sqlite3_stmt_busy(sqlite3_stmt*); ** will accept either a protected or an unprotected sqlite3_value. ** Every interface that accepts sqlite3_value arguments specifies ** whether or not it requires a protected sqlite3_value. The -** [sqlite3_value_dup()] interface can be used to construct a new +** [sqlite3_value_dup()] interface can be used to construct a new ** protected sqlite3_value from an unprotected sqlite3_value. ** ** The terms "protected" and "unprotected" refer to whether or not @@ -3435,7 +4766,7 @@ int sqlite3_stmt_busy(sqlite3_stmt*); ** sqlite3_value object but no mutex is held for an unprotected ** sqlite3_value object. If SQLite is compiled to be single-threaded ** (with [SQLITE_THREADSAFE=0] and with [sqlite3_threadsafe()] returning 0) -** or if SQLite is run in one of reduced mutex modes +** or if SQLite is run in one of reduced mutex modes ** [SQLITE_CONFIG_SINGLETHREAD] or [SQLITE_CONFIG_MULTITHREAD] ** then there is no distinction between protected and unprotected ** sqlite3_value objects and they can be used interchangeably. However, @@ -3445,21 +4776,24 @@ int sqlite3_stmt_busy(sqlite3_stmt*); ** ** ^The sqlite3_value objects that are passed as parameters into the ** implementation of [application-defined SQL functions] are protected. +** ^The sqlite3_value objects returned by [sqlite3_vtab_rhs_value()] +** are protected. ** ^The sqlite3_value object returned by ** [sqlite3_column_value()] is unprotected. -** Unprotected sqlite3_value objects may only be used with -** [sqlite3_result_value()] and [sqlite3_bind_value()]. +** Unprotected sqlite3_value objects may only be used as arguments +** to [sqlite3_result_value()], [sqlite3_bind_value()], and +** [sqlite3_value_dup()]. ** The [sqlite3_value_blob | sqlite3_value_type()] family of ** interfaces require protected sqlite3_value objects. */ -typedef struct Mem sqlite3_value; +typedef struct sqlite3_value sqlite3_value; /* ** CAPI3REF: SQL Function Context Object ** ** The context in which an SQL function executes is stored in an ** sqlite3_context object. ^A pointer to an sqlite3_context object -** is always first parameter to [application-defined SQL functions]. +** is always the first parameter to [application-defined SQL functions]. ** The application-defined SQL function implementation will pass this ** pointer through into calls to [sqlite3_result_int | sqlite3_result()], ** [sqlite3_aggregate_context()], [sqlite3_user_data()], @@ -3475,7 +4809,7 @@ typedef struct sqlite3_context sqlite3_context; ** METHOD: sqlite3_stmt ** ** ^(In the SQL statement text input to [sqlite3_prepare_v2()] and its variants, -** literals may be replaced by a [parameter] that matches one of following +** literals may be replaced by a [parameter] that matches one of the following ** templates: ** ** <ul> @@ -3503,12 +4837,30 @@ typedef struct sqlite3_context sqlite3_context; ** [sqlite3_bind_parameter_index()] API if desired. ^The index ** for "?NNN" parameters is the value of NNN. ** ^The NNN value must be between 1 and the [sqlite3_limit()] -** parameter [SQLITE_LIMIT_VARIABLE_NUMBER] (default value: 999). +** parameter [SQLITE_LIMIT_VARIABLE_NUMBER] (default value: 32766). ** ** ^The third argument is the value to bind to the parameter. ** ^If the third parameter to sqlite3_bind_text() or sqlite3_bind_text16() ** or sqlite3_bind_blob() is a NULL pointer then the fourth parameter ** is ignored and the end result is the same as sqlite3_bind_null(). +** ^If the third parameter to sqlite3_bind_text() is not NULL, then +** it should be a pointer to well-formed UTF8 text. +** ^If the third parameter to sqlite3_bind_text16() is not NULL, then +** it should be a pointer to well-formed UTF16 text. +** ^If the third parameter to sqlite3_bind_text64() is not NULL, then +** it should be a pointer to a well-formed unicode string that is +** either UTF8 if the sixth parameter is SQLITE_UTF8, or UTF16 +** otherwise. +** +** [[byte-order determination rules]] ^The byte-order of +** UTF16 input text is determined by the byte-order mark (BOM, U+FEFF) +** found in the first character, which is removed, or in the absence of a BOM +** the byte order is the native byte order of the host +** machine for sqlite3_bind_text16() or the byte order specified in +** the 6th parameter for sqlite3_bind_text64().)^ +** ^If UTF16 input text contains invalid unicode +** characters, then SQLite might change those invalid characters +** into the unicode replacement character: U+FFFD. ** ** ^(In those routines that have a fourth argument, its value is the ** number of bytes in the parameter. To be clear: the value is the @@ -3522,21 +4874,27 @@ typedef struct sqlite3_context sqlite3_context; ** or sqlite3_bind_text16() or sqlite3_bind_text64() then ** that parameter must be the byte offset ** where the NUL terminator would occur assuming the string were NUL -** terminated. If any NUL characters occur at byte offsets less than +** terminated. If any NUL characters occur at byte offsets less than ** the value of the fourth parameter then the resulting string value will ** contain embedded NULs. The result of expressions involving strings ** with embedded NULs is undefined. ** -** ^The fifth argument to the BLOB and string binding interfaces -** is a destructor used to dispose of the BLOB or -** string after SQLite has finished with it. ^The destructor is called -** to dispose of the BLOB or string even if the call to bind API fails. -** ^If the fifth argument is -** the special value [SQLITE_STATIC], then SQLite assumes that the -** information is in static, unmanaged space and does not need to be freed. -** ^If the fifth argument has the value [SQLITE_TRANSIENT], then -** SQLite makes its own private copy of the data immediately, before -** the sqlite3_bind_*() routine returns. +** ^The fifth argument to the BLOB and string binding interfaces controls +** or indicates the lifetime of the object referenced by the third parameter. +** These three options exist: +** ^ (1) A destructor to dispose of the BLOB or string after SQLite has finished +** with it may be passed. ^It is called to dispose of the BLOB or string even +** if the call to the bind API fails, except the destructor is not called if +** the third parameter is a NULL pointer or the fourth parameter is negative. +** ^ (2) The special constant, [SQLITE_STATIC], may be passed to indicate that +** the application remains responsible for disposing of the object. ^In this +** case, the object and the provided pointer to it must remain valid until +** either the prepared statement is finalized or the same SQL parameter is +** bound to something else, whichever occurs sooner. +** ^ (3) The constant, [SQLITE_TRANSIENT], may be passed to indicate that the +** object is to be copied prior to the return from sqlite3_bind_*(). ^The +** object and pointer to it must remain valid until then. ^SQLite will then +** manage the lifetime of its private copy. ** ** ^The sixth argument to sqlite3_bind_text64() must be one of ** [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE] @@ -3554,6 +4912,17 @@ typedef struct sqlite3_context sqlite3_context; ** [sqlite3_blob_open | incremental BLOB I/O] routines. ** ^A negative value for the zeroblob results in a zero-length BLOB. ** +** ^The sqlite3_bind_pointer(S,I,P,T,D) routine causes the I-th parameter in +** [prepared statement] S to have an SQL value of NULL, but to also be +** associated with the pointer P of type T. ^D is either a NULL pointer or +** a pointer to a destructor function for P. ^SQLite will invoke the +** destructor D with a single argument of P when it is finished using +** P, even if the call to sqlite3_bind_pointer() fails. Due to a +** historical design quirk, results are undefined if D is +** SQLITE_TRANSIENT. The T parameter should be a static string, +** preferably a string literal. The sqlite3_bind_pointer() routine is +** part of the [pointer passing interface] added for SQLite 3.20.0. +** ** ^If any of the sqlite3_bind_*() routines are called with a NULL pointer ** for the [prepared statement] or with a prepared statement for which ** [sqlite3_step()] has been called more recently than [sqlite3_reset()], @@ -3587,6 +4956,7 @@ int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*)); int sqlite3_bind_text64(sqlite3_stmt*, int, const char*, sqlite3_uint64, void(*)(void*), unsigned char encoding); int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*); +int sqlite3_bind_pointer(sqlite3_stmt*, int, void*, const char*,void(*)(void*)); int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n); int sqlite3_bind_zeroblob64(sqlite3_stmt*, int, sqlite3_uint64); @@ -3630,8 +5000,8 @@ int sqlite3_bind_parameter_count(sqlite3_stmt*); ** ^If the value N is out of range or if the N-th parameter is ** nameless, then NULL is returned. ^The returned string is ** always in UTF-8 encoding even if the named parameter was -** originally specified as UTF-16 in [sqlite3_prepare16()] or -** [sqlite3_prepare16_v2()]. +** originally specified as UTF-16 in [sqlite3_prepare16()], +** [sqlite3_prepare16_v2()], or [sqlite3_prepare16_v3()]. ** ** See also: [sqlite3_bind_blob|sqlite3_bind()], ** [sqlite3_bind_parameter_count()], and @@ -3648,7 +5018,8 @@ const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int); ** parameter to [sqlite3_bind_blob|sqlite3_bind()]. ^A zero ** is returned if no matching parameter is found. ^The parameter ** name must be given in UTF-8 even if the original statement -** was prepared from UTF-16 text using [sqlite3_prepare16_v2()]. +** was prepared from UTF-16 text using [sqlite3_prepare16_v2()] or +** [sqlite3_prepare16_v3()]. ** ** See also: [sqlite3_bind_blob|sqlite3_bind()], ** [sqlite3_bind_parameter_count()], and @@ -3671,8 +5042,12 @@ int sqlite3_clear_bindings(sqlite3_stmt*); ** METHOD: sqlite3_stmt ** ** ^Return the number of columns in the result set returned by the -** [prepared statement]. ^This routine returns 0 if pStmt is an SQL -** statement that does not return data (for example an [UPDATE]). +** [prepared statement]. ^If this routine returns 0, that means the +** [prepared statement] returns no data (for example an [UPDATE]). +** ^However, just because this routine returns a positive number does not +** mean that one or more rows of data will be returned. ^A SELECT statement +** will always have a positive sqlite3_column_count() but depending on the +** WHERE clause constraints and the table content, it might return no rows. ** ** See also: [sqlite3_data_count()] */ @@ -3713,7 +5088,7 @@ const void *sqlite3_column_name16(sqlite3_stmt*, int N); ** METHOD: sqlite3_stmt ** ** ^These routines provide a means to determine the database, table, and -** table column that is the origin of a particular result column in +** table column that is the origin of a particular result column in a ** [SELECT] statement. ** ^The name of the database or table or column can be returned as ** either a UTF-8 or UTF-16 string. ^The _database_ routines return @@ -3735,7 +5110,7 @@ const void *sqlite3_column_name16(sqlite3_stmt*, int N); ** ** ^If the Nth column returned by the statement is an expression or ** subquery and is not a column value, then all of these functions return -** NULL. ^These routine might also return NULL if a memory allocation error +** NULL. ^These routines might also return NULL if a memory allocation error ** occurs. ^Otherwise, they return the name of the attached database, table, ** or column that query result column was extracted from. ** @@ -3745,10 +5120,6 @@ const void *sqlite3_column_name16(sqlite3_stmt*, int N); ** ^These APIs are only available if the library was compiled with the ** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol. ** -** If two or more threads call one or more of these routines against the same -** prepared statement and column at the same time then the results are -** undefined. -** ** If two or more threads call one or more ** [sqlite3_column_database_name | column metadata interfaces] ** for the same [prepared statement] and result column @@ -3798,16 +5169,18 @@ const void *sqlite3_column_decltype16(sqlite3_stmt*,int); ** CAPI3REF: Evaluate An SQL Statement ** METHOD: sqlite3_stmt ** -** After a [prepared statement] has been prepared using either -** [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] or one of the legacy +** After a [prepared statement] has been prepared using any of +** [sqlite3_prepare_v2()], [sqlite3_prepare_v3()], [sqlite3_prepare16_v2()], +** or [sqlite3_prepare16_v3()] or one of the legacy ** interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], this function ** must be called one or more times to evaluate the statement. ** ** The details of the behavior of the sqlite3_step() interface depend -** on whether the statement was prepared using the newer "v2" interface -** [sqlite3_prepare_v2()] and [sqlite3_prepare16_v2()] or the older legacy -** interface [sqlite3_prepare()] and [sqlite3_prepare16()]. The use of the -** new "v2" interface is recommended for new applications but the legacy +** on whether the statement was prepared using the newer "vX" interfaces +** [sqlite3_prepare_v3()], [sqlite3_prepare_v2()], [sqlite3_prepare16_v3()], +** [sqlite3_prepare16_v2()] or the older legacy +** interfaces [sqlite3_prepare()] and [sqlite3_prepare16()]. The use of the +** new "vX" interface is recommended for new applications but the legacy ** interface will continue to be supported. ** ** ^In the legacy interface, the return value will be either [SQLITE_BUSY], @@ -3851,9 +5224,10 @@ const void *sqlite3_column_decltype16(sqlite3_stmt*,int); ** For all versions of SQLite up to and including 3.6.23.1, a call to ** [sqlite3_reset()] was required after sqlite3_step() returned anything ** other than [SQLITE_ROW] before any subsequent invocation of -** sqlite3_step(). Failure to reset the prepared statement using +** sqlite3_step(). Failure to reset the prepared statement using ** [sqlite3_reset()] would result in an [SQLITE_MISUSE] return from -** sqlite3_step(). But after version 3.6.23.1, sqlite3_step() began +** sqlite3_step(). But after [version 3.6.23.1] ([dateof:3.6.23.1]), +** sqlite3_step() began ** calling [sqlite3_reset()] automatically in this circumstance rather ** than returning [SQLITE_MISUSE]. This is not considered a compatibility ** break because any application that ever receives an SQLITE_MISUSE error @@ -3867,10 +5241,11 @@ const void *sqlite3_column_decltype16(sqlite3_stmt*,int); ** specific [error codes] that better describes the error. ** We admit that this is a goofy design. The problem has been fixed ** with the "v2" interface. If you prepare all of your SQL statements -** using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] instead +** using [sqlite3_prepare_v3()] or [sqlite3_prepare_v2()] +** or [sqlite3_prepare16_v2()] or [sqlite3_prepare16_v3()] instead ** of the legacy [sqlite3_prepare()] and [sqlite3_prepare16()] interfaces, ** then the more specific [error codes] are returned directly -** by sqlite3_step(). The use of the "v2" interface is recommended. +** by sqlite3_step(). The use of the "vX" interfaces is recommended. */ int sqlite3_step(sqlite3_stmt*); @@ -3881,7 +5256,7 @@ int sqlite3_step(sqlite3_stmt*); ** ^The sqlite3_data_count(P) interface returns the number of columns in the ** current row of the result set of [prepared statement] P. ** ^If prepared statement P does not have results ready to return -** (via calls to the [sqlite3_column_int | sqlite3_column_*()] of +** (via calls to the [sqlite3_column_int | sqlite3_column()] family of ** interfaces) then sqlite3_data_count(P) returns 0. ** ^The sqlite3_data_count(P) routine also returns 0 if P is a NULL pointer. ** ^The sqlite3_data_count(P) routine returns 0 if the previous call to @@ -3932,6 +5307,28 @@ int sqlite3_data_count(sqlite3_stmt *pStmt); ** KEYWORDS: {column access functions} ** METHOD: sqlite3_stmt ** +** <b>Summary:</b> +** <blockquote><table border=0 cellpadding=0 cellspacing=0> +** <tr><td><b>sqlite3_column_blob</b><td>&rarr;<td>BLOB result +** <tr><td><b>sqlite3_column_double</b><td>&rarr;<td>REAL result +** <tr><td><b>sqlite3_column_int</b><td>&rarr;<td>32-bit INTEGER result +** <tr><td><b>sqlite3_column_int64</b><td>&rarr;<td>64-bit INTEGER result +** <tr><td><b>sqlite3_column_text</b><td>&rarr;<td>UTF-8 TEXT result +** <tr><td><b>sqlite3_column_text16</b><td>&rarr;<td>UTF-16 TEXT result +** <tr><td><b>sqlite3_column_value</b><td>&rarr;<td>The result as an +** [sqlite3_value|unprotected sqlite3_value] object. +** <tr><td>&nbsp;<td>&nbsp;<td>&nbsp; +** <tr><td><b>sqlite3_column_bytes</b><td>&rarr;<td>Size of a BLOB +** or a UTF-8 TEXT result in bytes +** <tr><td><b>sqlite3_column_bytes16&nbsp;&nbsp;</b> +** <td>&rarr;&nbsp;&nbsp;<td>Size of UTF-16 +** TEXT in bytes +** <tr><td><b>sqlite3_column_type</b><td>&rarr;<td>Default +** datatype of the result +** </table></blockquote> +** +** <b>Details:</b> +** ** ^These routines return information about a single column of the current ** result row of a query. ^In every case the first argument is a pointer ** to the [prepared statement] that is being evaluated (the [sqlite3_stmt*] @@ -3953,16 +5350,29 @@ int sqlite3_data_count(sqlite3_stmt *pStmt); ** are called from a different thread while any of these routines ** are pending, then the results are undefined. ** +** The first six interfaces (_blob, _double, _int, _int64, _text, and _text16) +** each return the value of a result column in a specific data format. If +** the result column is not initially in the requested format (for example, +** if the query returns an integer but the sqlite3_column_text() interface +** is used to extract the value) then an automatic type conversion is performed. +** ** ^The sqlite3_column_type() routine returns the ** [SQLITE_INTEGER | datatype code] for the initial data type ** of the result column. ^The returned value is one of [SQLITE_INTEGER], -** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL]. The value -** returned by sqlite3_column_type() is only meaningful if no type -** conversions have occurred as described below. After a type conversion, -** the value returned by sqlite3_column_type() is undefined. Future +** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL]. +** The return value of sqlite3_column_type() can be used to decide which +** of the first six interface should be used to extract the column value. +** The value returned by sqlite3_column_type() is only meaningful if no +** automatic type conversions have occurred for the value in question. +** After a type conversion, the result of calling sqlite3_column_type() +** is undefined, though harmless. Future ** versions of SQLite may change the behavior of sqlite3_column_type() ** following a type conversion. ** +** If the result is a BLOB or a TEXT string, then the sqlite3_column_bytes() +** or sqlite3_column_bytes16() interfaces can be used to determine the size +** of that BLOB or string. +** ** ^If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes() ** routine returns the number of bytes in that BLOB or string. ** ^If the result is a UTF-16 string, then sqlite3_column_bytes() converts @@ -3981,7 +5391,7 @@ int sqlite3_data_count(sqlite3_stmt *pStmt); ** the number of bytes in that string. ** ^If the result is NULL, then sqlite3_column_bytes16() returns zero. ** -** ^The values returned by [sqlite3_column_bytes()] and +** ^The values returned by [sqlite3_column_bytes()] and ** [sqlite3_column_bytes16()] do not include the zero terminators at the end ** of the string. ^For clarity: the values returned by ** [sqlite3_column_bytes()] and [sqlite3_column_bytes16()] are the number of @@ -3991,6 +5401,10 @@ int sqlite3_data_count(sqlite3_stmt *pStmt); ** even empty strings, are always zero-terminated. ^The return ** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer. ** +** ^Strings returned by sqlite3_column_text16() always have the endianness +** which is native to the platform, regardless of the text encoding set +** for the database. +** ** <b>Warning:</b> ^The object returned by [sqlite3_column_value()] is an ** [unprotected sqlite3_value] object. In a multithreaded environment, ** an unprotected sqlite3_value object may only be used safely with @@ -3999,9 +5413,13 @@ int sqlite3_data_count(sqlite3_stmt *pStmt); ** [sqlite3_column_value()] is used in any other way, including calls ** to routines like [sqlite3_value_int()], [sqlite3_value_text()], ** or [sqlite3_value_bytes()], the behavior is not threadsafe. +** Hence, the sqlite3_column_value() interface +** is normally only useful within the implementation of +** [application-defined SQL functions] or [virtual tables], not within +** top-level application code. ** -** These routines attempt to convert the value where appropriate. ^For -** example, if the internal representation is FLOAT and a text result +** These routines may attempt to convert the datatype of the result. +** ^For example, if the internal representation is FLOAT and a text result ** is requested, [sqlite3_snprintf()] is used internally to perform the ** conversion automatically. ^(The following table details the conversions ** that are applied: @@ -4025,7 +5443,7 @@ int sqlite3_data_count(sqlite3_stmt *pStmt); ** <tr><td> TEXT <td> BLOB <td> No change ** <tr><td> BLOB <td> INTEGER <td> [CAST] to INTEGER ** <tr><td> BLOB <td> FLOAT <td> [CAST] to REAL -** <tr><td> BLOB <td> TEXT <td> Add a zero terminator if needed +** <tr><td> BLOB <td> TEXT <td> [CAST] to TEXT, ensure zero terminator ** </table> ** </blockquote>)^ ** @@ -4073,26 +5491,40 @@ int sqlite3_data_count(sqlite3_stmt *pStmt); ** ^The pointers returned are valid until a type conversion occurs as ** described above, or until [sqlite3_step()] or [sqlite3_reset()] or ** [sqlite3_finalize()] is called. ^The memory space used to hold strings -** and BLOBs is freed automatically. Do <em>not</em> pass the pointers returned +** and BLOBs is freed automatically. Do not pass the pointers returned ** from [sqlite3_column_blob()], [sqlite3_column_text()], etc. into ** [sqlite3_free()]. ** -** ^(If a memory allocation error occurs during the evaluation of any -** of these routines, a default value is returned. The default value -** is either the integer 0, the floating point number 0.0, or a NULL -** pointer. Subsequent calls to [sqlite3_errcode()] will return -** [SQLITE_NOMEM].)^ +** As long as the input parameters are correct, these routines will only +** fail if an out-of-memory error occurs during a format conversion. +** Only the following subset of interfaces are subject to out-of-memory +** errors: +** +** <ul> +** <li> sqlite3_column_blob() +** <li> sqlite3_column_text() +** <li> sqlite3_column_text16() +** <li> sqlite3_column_bytes() +** <li> sqlite3_column_bytes16() +** </ul> +** +** If an out-of-memory error occurs, then the return value from these +** routines is the same as if the column had contained an SQL NULL value. +** Valid SQL NULL returns can be distinguished from out-of-memory errors +** by invoking the [sqlite3_errcode()] immediately after the suspect +** return value is obtained and before any +** other SQLite interface is called on the same [database connection]. */ const void *sqlite3_column_blob(sqlite3_stmt*, int iCol); -int sqlite3_column_bytes(sqlite3_stmt*, int iCol); -int sqlite3_column_bytes16(sqlite3_stmt*, int iCol); double sqlite3_column_double(sqlite3_stmt*, int iCol); int sqlite3_column_int(sqlite3_stmt*, int iCol); sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol); const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol); const void *sqlite3_column_text16(sqlite3_stmt*, int iCol); -int sqlite3_column_type(sqlite3_stmt*, int iCol); sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol); +int sqlite3_column_bytes(sqlite3_stmt*, int iCol); +int sqlite3_column_bytes16(sqlite3_stmt*, int iCol); +int sqlite3_column_type(sqlite3_stmt*, int iCol); /* ** CAPI3REF: Destroy A Prepared Statement Object @@ -4100,7 +5532,7 @@ sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol); ** ** ^The sqlite3_finalize() function is called to delete a [prepared statement]. ** ^If the most recent evaluation of the statement encountered no errors -** or if the statement is never been evaluated, then sqlite3_finalize() returns +** or if the statement has never been evaluated, then sqlite3_finalize() returns ** SQLITE_OK. ^If the most recent evaluation of statement S failed, then ** sqlite3_finalize(S) returns the appropriate [error code] or ** [extended error code]. @@ -4135,34 +5567,47 @@ int sqlite3_finalize(sqlite3_stmt *pStmt); ** ^The [sqlite3_reset(S)] interface resets the [prepared statement] S ** back to the beginning of its program. ** -** ^If the most recent call to [sqlite3_step(S)] for the -** [prepared statement] S returned [SQLITE_ROW] or [SQLITE_DONE], -** or if [sqlite3_step(S)] has never before been called on S, -** then [sqlite3_reset(S)] returns [SQLITE_OK]. +** ^The return code from [sqlite3_reset(S)] indicates whether or not +** the previous evaluation of prepared statement S completed successfully. +** ^If [sqlite3_step(S)] has never before been called on S or if +** [sqlite3_step(S)] has not been called since the previous call +** to [sqlite3_reset(S)], then [sqlite3_reset(S)] will return +** [SQLITE_OK]. ** ** ^If the most recent call to [sqlite3_step(S)] for the ** [prepared statement] S indicated an error, then ** [sqlite3_reset(S)] returns an appropriate [error code]. +** ^The [sqlite3_reset(S)] interface might also return an [error code] +** if there were no prior errors but the process of resetting +** the prepared statement caused a new error. ^For example, if an +** [INSERT] statement with a [RETURNING] clause is only stepped one time, +** that one call to [sqlite3_step(S)] might return SQLITE_ROW but +** the overall statement might still fail and the [sqlite3_reset(S)] call +** might return SQLITE_BUSY if locking constraints prevent the +** database change from committing. Therefore, it is important that +** applications check the return code from [sqlite3_reset(S)] even if +** no prior call to [sqlite3_step(S)] indicated a problem. ** ** ^The [sqlite3_reset(S)] interface does not change the values ** of any [sqlite3_bind_blob|bindings] on the [prepared statement] S. */ int sqlite3_reset(sqlite3_stmt *pStmt); + /* ** CAPI3REF: Create Or Redefine SQL Functions ** KEYWORDS: {function creation routines} -** KEYWORDS: {application-defined SQL function} -** KEYWORDS: {application-defined SQL functions} ** METHOD: sqlite3 ** ** ^These functions (collectively known as "function creation routines") ** are used to add SQL functions or aggregates or to redefine the behavior -** of existing SQL functions or aggregates. The only differences between -** these routines are the text encoding expected for -** the second parameter (the name of the function being created) -** and the presence or absence of a destructor callback for -** the application data pointer. +** of existing SQL functions or aggregates. The only differences between +** the three "sqlite3_create_function*" routines are the text encoding +** expected for the second parameter (the name of the function being +** created) and the presence or absence of a destructor callback for +** the application data pointer. Function sqlite3_create_window_function() +** is similar, but allows the user to supply the extra callback functions +** needed by [aggregate window functions]. ** ** ^The first parameter is the [database connection] to which the SQL ** function is to be added. ^If an application uses more than one database @@ -4172,7 +5617,7 @@ int sqlite3_reset(sqlite3_stmt *pStmt); ** ^The second parameter is the name of the SQL function to be created or ** redefined. ^The length of the name is limited to 255 bytes in a UTF-8 ** representation, exclusive of the zero-terminator. ^Note that the name -** length limit is in UTF-8 bytes, not characters nor UTF-16 bytes. +** length limit is in UTF-8 bytes, not characters nor UTF-16 bytes. ** ^Any attempt to create a function with a longer name ** will result in [SQLITE_MISUSE] being returned. ** @@ -4187,7 +5632,7 @@ int sqlite3_reset(sqlite3_stmt *pStmt); ** ^The fourth parameter, eTextRep, specifies what ** [SQLITE_UTF8 | text encoding] this SQL function prefers for ** its parameters. The application should set this parameter to -** [SQLITE_UTF16LE] if the function implementation invokes +** [SQLITE_UTF16LE] if the function implementation invokes ** [sqlite3_value_text16le()] on an input, or [SQLITE_UTF16BE] if the ** implementation invokes [sqlite3_value_text16be()] on an input, or ** [SQLITE_UTF16] if [sqlite3_value_text16()] is used, or [SQLITE_UTF8] @@ -4205,10 +5650,26 @@ int sqlite3_reset(sqlite3_stmt *pStmt); ** perform additional optimizations on deterministic functions, so use ** of the [SQLITE_DETERMINISTIC] flag is recommended where possible. ** +** ^The fourth parameter may also optionally include the [SQLITE_DIRECTONLY] +** flag, which if present prevents the function from being invoked from +** within VIEWs, TRIGGERs, CHECK constraints, generated column expressions, +** index expressions, or the WHERE clause of partial indexes. +** +** For best security, the [SQLITE_DIRECTONLY] flag is recommended for +** all application-defined SQL functions that do not need to be +** used inside of triggers, views, CHECK constraints, or other elements of +** the database schema. This flag is especially recommended for SQL +** functions that have side effects or reveal internal application state. +** Without this flag, an attacker might be able to modify the schema of +** a database file to include invocations of the function with parameters +** chosen by the attacker, which the application will then execute when +** the database file is opened and read. +** ** ^(The fifth parameter is an arbitrary pointer. The implementation of the ** function can gain access to this pointer using [sqlite3_user_data()].)^ ** -** ^The sixth, seventh and eighth parameters, xFunc, xStep and xFinal, are +** ^The sixth, seventh and eighth parameters passed to the three +** "sqlite3_create_function*" functions, xFunc, xStep and xFinal, are ** pointers to C-language functions that implement the SQL function or ** aggregate. ^A scalar SQL function requires an implementation of the xFunc ** callback only; NULL pointers must be passed as the xStep and xFinal @@ -4217,15 +5678,24 @@ int sqlite3_reset(sqlite3_stmt *pStmt); ** SQL function or aggregate, pass NULL pointers for all three function ** callbacks. ** -** ^(If the ninth parameter to sqlite3_create_function_v2() is not NULL, -** then it is destructor for the application data pointer. -** The destructor is invoked when the function is deleted, either by being -** overloaded or when the database connection closes.)^ -** ^The destructor is also invoked if the call to -** sqlite3_create_function_v2() fails. -** ^When the destructor callback of the tenth parameter is invoked, it -** is passed a single argument which is a copy of the application data -** pointer which was the fifth parameter to sqlite3_create_function_v2(). +** ^The sixth, seventh, eighth and ninth parameters (xStep, xFinal, xValue +** and xInverse) passed to sqlite3_create_window_function are pointers to +** C-language callbacks that implement the new function. xStep and xFinal +** must both be non-NULL. xValue and xInverse may either both be NULL, in +** which case a regular aggregate function is created, or must both be +** non-NULL, in which case the new function may be used as either an aggregate +** or aggregate window function. More details regarding the implementation +** of aggregate window functions are +** [user-defined window functions|available here]. +** +** ^(If the final parameter to sqlite3_create_function_v2() or +** sqlite3_create_window_function() is not NULL, then it is the destructor for +** the application data pointer. The destructor is invoked when the function +** is deleted, either by being overloaded or when the database connection +** closes.)^ ^The destructor is also invoked if the call to +** sqlite3_create_function_v2() fails. ^When the destructor callback is +** invoked, it is passed a single argument which is a copy of the application +** data pointer which was the fifth parameter to sqlite3_create_function_v2(). ** ** ^It is permitted to register multiple implementations of the same ** functions with the same name but with either differing numbers of @@ -4235,7 +5705,7 @@ int sqlite3_reset(sqlite3_stmt *pStmt); ** nArg parameter is a better match than a function implementation with ** a negative nArg. ^A function where the preferred text encoding ** matches the database encoding is a better -** match than a function where the encoding is different. +** match than a function where the encoding is different. ** ^A function where the encoding difference is between UTF16le and UTF16be ** is a closer match than a function where the encoding difference is ** between UTF8 and UTF16. @@ -4278,11 +5748,23 @@ int sqlite3_create_function_v2( void (*xFinal)(sqlite3_context*), void(*xDestroy)(void*) ); +int sqlite3_create_window_function( + sqlite3 *db, + const char *zFunctionName, + int nArg, + int eTextRep, + void *pApp, + void (*xStep)(sqlite3_context*,int,sqlite3_value**), + void (*xFinal)(sqlite3_context*), + void (*xValue)(sqlite3_context*), + void (*xInverse)(sqlite3_context*,int,sqlite3_value**), + void(*xDestroy)(void*) +); /* ** CAPI3REF: Text Encodings ** -** These constant define integer codes that represent the various +** These constants define integer codes that represent the various ** text encodings supported by SQLite. */ #define SQLITE_UTF8 1 /* IMP: R-37514-35566 */ @@ -4295,19 +5777,115 @@ int sqlite3_create_function_v2( /* ** CAPI3REF: Function Flags ** -** These constants may be ORed together with the +** These constants may be ORed together with the ** [SQLITE_UTF8 | preferred text encoding] as the fourth argument ** to [sqlite3_create_function()], [sqlite3_create_function16()], or ** [sqlite3_create_function_v2()]. +** +** <dl> +** [[SQLITE_DETERMINISTIC]] <dt>SQLITE_DETERMINISTIC</dt><dd> +** The SQLITE_DETERMINISTIC flag means that the new function always gives +** the same output when the input parameters are the same. +** The [abs|abs() function] is deterministic, for example, but +** [randomblob|randomblob()] is not. Functions must +** be deterministic in order to be used in certain contexts such as +** with the WHERE clause of [partial indexes] or in [generated columns]. +** SQLite might also optimize deterministic functions by factoring them +** out of inner loops. +** </dd> +** +** [[SQLITE_DIRECTONLY]] <dt>SQLITE_DIRECTONLY</dt><dd> +** The SQLITE_DIRECTONLY flag means that the function may only be invoked +** from top-level SQL, and cannot be used in VIEWs or TRIGGERs nor in +** schema structures such as [CHECK constraints], [DEFAULT clauses], +** [expression indexes], [partial indexes], or [generated columns]. +** <p> +** The SQLITE_DIRECTONLY flag is recommended for any +** [application-defined SQL function] +** that has side-effects or that could potentially leak sensitive information. +** This will prevent attacks in which an application is tricked +** into using a database file that has had its schema surreptitiously +** modified to invoke the application-defined function in ways that are +** harmful. +** <p> +** Some people say it is good practice to set SQLITE_DIRECTONLY on all +** [application-defined SQL functions], regardless of whether or not they +** are security sensitive, as doing so prevents those functions from being used +** inside of the database schema, and thus ensures that the database +** can be inspected and modified using generic tools (such as the [CLI]) +** that do not have access to the application-defined functions. +** </dd> +** +** [[SQLITE_INNOCUOUS]] <dt>SQLITE_INNOCUOUS</dt><dd> +** The SQLITE_INNOCUOUS flag means that the function is unlikely +** to cause problems even if misused. An innocuous function should have +** no side effects and should not depend on any values other than its +** input parameters. The [abs|abs() function] is an example of an +** innocuous function. +** The [load_extension() SQL function] is not innocuous because of its +** side effects. +** <p> SQLITE_INNOCUOUS is similar to SQLITE_DETERMINISTIC, but is not +** exactly the same. The [random|random() function] is an example of a +** function that is innocuous but not deterministic. +** <p>Some heightened security settings +** ([SQLITE_DBCONFIG_TRUSTED_SCHEMA] and [PRAGMA trusted_schema=OFF]) +** disable the use of SQL functions inside views and triggers and in +** schema structures such as [CHECK constraints], [DEFAULT clauses], +** [expression indexes], [partial indexes], and [generated columns] unless +** the function is tagged with SQLITE_INNOCUOUS. Most built-in functions +** are innocuous. Developers are advised to avoid using the +** SQLITE_INNOCUOUS flag for application-defined functions unless the +** function has been carefully audited and found to be free of potentially +** security-adverse side-effects and information-leaks. +** </dd> +** +** [[SQLITE_SUBTYPE]] <dt>SQLITE_SUBTYPE</dt><dd> +** The SQLITE_SUBTYPE flag indicates to SQLite that a function might call +** [sqlite3_value_subtype()] to inspect the sub-types of its arguments. +** This flag instructs SQLite to omit some corner-case optimizations that +** might disrupt the operation of the [sqlite3_value_subtype()] function, +** causing it to return zero rather than the correct subtype(). +** All SQL functions that invoke [sqlite3_value_subtype()] should have this +** property. If the SQLITE_SUBTYPE property is omitted, then the return +** value from [sqlite3_value_subtype()] might sometimes be zero even though +** a non-zero subtype was specified by the function argument expression. +** +** [[SQLITE_RESULT_SUBTYPE]] <dt>SQLITE_RESULT_SUBTYPE</dt><dd> +** The SQLITE_RESULT_SUBTYPE flag indicates to SQLite that a function might call +** [sqlite3_result_subtype()] to cause a sub-type to be associated with its +** result. +** Every function that invokes [sqlite3_result_subtype()] should have this +** property. If it does not, then the call to [sqlite3_result_subtype()] +** might become a no-op if the function is used as a term in an +** [expression index]. On the other hand, SQL functions that never invoke +** [sqlite3_result_subtype()] should avoid setting this property, as the +** purpose of this property is to disable certain optimizations that are +** incompatible with subtypes. +** +** [[SQLITE_SELFORDER1]] <dt>SQLITE_SELFORDER1</dt><dd> +** The SQLITE_SELFORDER1 flag indicates that the function is an aggregate +** that internally orders the values provided to the first argument. The +** ordered-set aggregate SQL notation with a single ORDER BY term can be +** used to invoke this function. If the ordered-set aggregate notation is +** used on a function that lacks this flag, then an error is raised. Note +** that the ordered-set aggregate syntax is only available if SQLite is +** built using the -DSQLITE_ENABLE_ORDERED_SET_AGGREGATES compile-time option. +** </dd> +** </dl> */ -#define SQLITE_DETERMINISTIC 0x800 +#define SQLITE_DETERMINISTIC 0x000000800 +#define SQLITE_DIRECTONLY 0x000080000 +#define SQLITE_SUBTYPE 0x000100000 +#define SQLITE_INNOCUOUS 0x000200000 +#define SQLITE_RESULT_SUBTYPE 0x001000000 +#define SQLITE_SELFORDER1 0x002000000 /* ** CAPI3REF: Deprecated Functions ** DEPRECATED ** ** These functions are [deprecated]. In order to maintain -** backwards compatibility with older code, these functions continue +** backwards compatibility with older code, these functions continue ** to be supported. However, new applications should avoid ** the use of these functions. To encourage programmers to avoid ** these functions, we will not explain what they do. @@ -4326,21 +5904,45 @@ SQLITE_DEPRECATED int sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int), ** CAPI3REF: Obtaining SQL Values ** METHOD: sqlite3_value ** -** The C-language implementation of SQL functions and aggregates uses -** this set of interface routines to access the parameter values on -** the function or aggregate. -** -** The xFunc (for scalar functions) or xStep (for aggregates) parameters -** to [sqlite3_create_function()] and [sqlite3_create_function16()] -** define callbacks that implement the SQL functions and aggregates. -** The 3rd parameter to these callbacks is an array of pointers to -** [protected sqlite3_value] objects. There is one [sqlite3_value] object for -** each parameter to the SQL function. These routines are used to -** extract values from the [sqlite3_value] objects. +** <b>Summary:</b> +** <blockquote><table border=0 cellpadding=0 cellspacing=0> +** <tr><td><b>sqlite3_value_blob</b><td>&rarr;<td>BLOB value +** <tr><td><b>sqlite3_value_double</b><td>&rarr;<td>REAL value +** <tr><td><b>sqlite3_value_int</b><td>&rarr;<td>32-bit INTEGER value +** <tr><td><b>sqlite3_value_int64</b><td>&rarr;<td>64-bit INTEGER value +** <tr><td><b>sqlite3_value_pointer</b><td>&rarr;<td>Pointer value +** <tr><td><b>sqlite3_value_text</b><td>&rarr;<td>UTF-8 TEXT value +** <tr><td><b>sqlite3_value_text16</b><td>&rarr;<td>UTF-16 TEXT value in +** the native byteorder +** <tr><td><b>sqlite3_value_text16be</b><td>&rarr;<td>UTF-16be TEXT value +** <tr><td><b>sqlite3_value_text16le</b><td>&rarr;<td>UTF-16le TEXT value +** <tr><td>&nbsp;<td>&nbsp;<td>&nbsp; +** <tr><td><b>sqlite3_value_bytes</b><td>&rarr;<td>Size of a BLOB +** or a UTF-8 TEXT in bytes +** <tr><td><b>sqlite3_value_bytes16&nbsp;&nbsp;</b> +** <td>&rarr;&nbsp;&nbsp;<td>Size of UTF-16 +** TEXT in bytes +** <tr><td><b>sqlite3_value_type</b><td>&rarr;<td>Default +** datatype of the value +** <tr><td><b>sqlite3_value_numeric_type&nbsp;&nbsp;</b> +** <td>&rarr;&nbsp;&nbsp;<td>Best numeric datatype of the value +** <tr><td><b>sqlite3_value_nochange&nbsp;&nbsp;</b> +** <td>&rarr;&nbsp;&nbsp;<td>True if the column is unchanged in an UPDATE +** against a virtual table. +** <tr><td><b>sqlite3_value_frombind&nbsp;&nbsp;</b> +** <td>&rarr;&nbsp;&nbsp;<td>True if value originated from a [bound parameter] +** </table></blockquote> +** +** <b>Details:</b> +** +** These routines extract type, size, and content information from +** [protected sqlite3_value] objects. Protected sqlite3_value objects +** are used to pass parameter information into the functions that +** implement [application-defined SQL functions] and [virtual tables]. ** ** These routines work only with [protected sqlite3_value] objects. ** Any attempt to use these routines on an [unprotected sqlite3_value] -** object results in undefined behavior. +** is not threadsafe. ** ** ^These routines work just like the corresponding [column access functions] ** except that these routines take a single [protected sqlite3_value] object @@ -4351,6 +5953,24 @@ SQLITE_DEPRECATED int sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int), ** sqlite3_value_text16be() and sqlite3_value_text16le() interfaces ** extract UTF-16 strings as big-endian and little-endian respectively. ** +** ^If [sqlite3_value] object V was initialized +** using [sqlite3_bind_pointer(S,I,P,X,D)] or [sqlite3_result_pointer(C,P,X,D)] +** and if X and Y are strings that compare equal according to strcmp(X,Y), +** then sqlite3_value_pointer(V,Y) will return the pointer P. ^Otherwise, +** sqlite3_value_pointer(V,Y) returns a NULL. The sqlite3_bind_pointer() +** routine is part of the [pointer passing interface] added for SQLite 3.20.0. +** +** ^(The sqlite3_value_type(V) interface returns the +** [SQLITE_INTEGER | datatype code] for the initial datatype of the +** [sqlite3_value] object V. The returned value is one of [SQLITE_INTEGER], +** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL].)^ +** Other interfaces might change the datatype for an sqlite3_value object. +** For example, if the datatype is initially SQLITE_INTEGER and +** sqlite3_value_text(V) is called to extract a text value for that +** integer, then subsequent calls to sqlite3_value_type(V) might return +** SQLITE_TEXT. Whether or not a persistent internal datatype conversion +** occurs is undefined and may change from one release of SQLite to the next. +** ** ^(The sqlite3_value_numeric_type() interface attempts to apply ** numeric affinity to the value. This means that an attempt is ** made to convert the value to an integer or floating point. If @@ -4359,6 +5979,24 @@ SQLITE_DEPRECATED int sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int), ** then the conversion is performed. Otherwise no conversion occurs. ** The [SQLITE_INTEGER | datatype] after conversion is returned.)^ ** +** ^Within the [xUpdate] method of a [virtual table], the +** sqlite3_value_nochange(X) interface returns true if and only if +** the column corresponding to X is unchanged by the UPDATE operation +** that the xUpdate method call was invoked to implement and if +** the prior [xColumn] method call that was invoked to extract +** the value for that column returned without setting a result (probably +** because it queried [sqlite3_vtab_nochange()] and found that the column +** was unchanging). ^Within an [xUpdate] method, any value for which +** sqlite3_value_nochange(X) is true will in all other respects appear +** to be a NULL value. If sqlite3_value_nochange(X) is invoked anywhere other +** than within an [xUpdate] method call for an UPDATE statement, then +** the return value is arbitrary and meaningless. +** +** ^The sqlite3_value_frombind(X) interface returns non-zero if the +** value X originated from one of the [sqlite3_bind_int|sqlite3_bind()] +** interfaces. ^If X comes from an SQL literal value, or a table column, +** or an expression, then sqlite3_value_frombind(X) returns zero. +** ** Please pay particular attention to the fact that the pointer returned ** from [sqlite3_value_blob()], [sqlite3_value_text()], or ** [sqlite3_value_text16()] can be invalidated by a subsequent call to @@ -4367,19 +6005,66 @@ SQLITE_DEPRECATED int sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int), ** ** These routines must be called from the same thread as ** the SQL function that supplied the [sqlite3_value*] parameters. +** +** As long as the input parameter is correct, these routines can only +** fail if an out-of-memory error occurs during a format conversion. +** Only the following subset of interfaces are subject to out-of-memory +** errors: +** +** <ul> +** <li> sqlite3_value_blob() +** <li> sqlite3_value_text() +** <li> sqlite3_value_text16() +** <li> sqlite3_value_text16le() +** <li> sqlite3_value_text16be() +** <li> sqlite3_value_bytes() +** <li> sqlite3_value_bytes16() +** </ul> +** +** If an out-of-memory error occurs, then the return value from these +** routines is the same as if the column had contained an SQL NULL value. +** Valid SQL NULL returns can be distinguished from out-of-memory errors +** by invoking the [sqlite3_errcode()] immediately after the suspect +** return value is obtained and before any +** other SQLite interface is called on the same [database connection]. */ const void *sqlite3_value_blob(sqlite3_value*); -int sqlite3_value_bytes(sqlite3_value*); -int sqlite3_value_bytes16(sqlite3_value*); double sqlite3_value_double(sqlite3_value*); int sqlite3_value_int(sqlite3_value*); sqlite3_int64 sqlite3_value_int64(sqlite3_value*); +void *sqlite3_value_pointer(sqlite3_value*, const char*); const unsigned char *sqlite3_value_text(sqlite3_value*); const void *sqlite3_value_text16(sqlite3_value*); const void *sqlite3_value_text16le(sqlite3_value*); const void *sqlite3_value_text16be(sqlite3_value*); +int sqlite3_value_bytes(sqlite3_value*); +int sqlite3_value_bytes16(sqlite3_value*); int sqlite3_value_type(sqlite3_value*); int sqlite3_value_numeric_type(sqlite3_value*); +int sqlite3_value_nochange(sqlite3_value*); +int sqlite3_value_frombind(sqlite3_value*); + +/* +** CAPI3REF: Report the internal text encoding state of an sqlite3_value object +** METHOD: sqlite3_value +** +** ^(The sqlite3_value_encoding(X) interface returns one of [SQLITE_UTF8], +** [SQLITE_UTF16BE], or [SQLITE_UTF16LE] according to the current text encoding +** of the value X, assuming that X has type TEXT.)^ If sqlite3_value_type(X) +** returns something other than SQLITE_TEXT, then the return value from +** sqlite3_value_encoding(X) is meaningless. ^Calls to +** [sqlite3_value_text(X)], [sqlite3_value_text16(X)], [sqlite3_value_text16be(X)], +** [sqlite3_value_text16le(X)], [sqlite3_value_bytes(X)], or +** [sqlite3_value_bytes16(X)] might change the encoding of the value X and +** thus change the return from subsequent calls to sqlite3_value_encoding(X). +** +** This routine is intended for used by applications that test and validate +** the SQLite implementation. This routine is inquiring about the opaque +** internal state of an [sqlite3_value] object. Ordinary applications should +** not need to know what the internal state of an sqlite3_value object is and +** hence should not need to use this interface. +*/ +int sqlite3_value_encoding(sqlite3_value*); /* ** CAPI3REF: Finding The Subtype Of SQL Values @@ -4391,9 +6076,11 @@ int sqlite3_value_numeric_type(sqlite3_value*); ** one SQL function to another. Use the [sqlite3_result_subtype()] ** routine to set the subtype for the return value of an SQL function. ** -** SQLite makes no use of subtype itself. It merely passes the subtype -** from the result of one [application-defined SQL function] into the -** input of another. +** Every [application-defined SQL function] that invokes this interface +** should include the [SQLITE_SUBTYPE] property in the text +** encoding argument when the function is [sqlite3_create_function|registered]. +** If the [SQLITE_SUBTYPE] property is omitted, then sqlite3_value_subtype() +** might return zero instead of the upstream subtype in some corner cases. */ unsigned int sqlite3_value_subtype(sqlite3_value*); @@ -4402,10 +6089,11 @@ unsigned int sqlite3_value_subtype(sqlite3_value*); ** METHOD: sqlite3_value ** ** ^The sqlite3_value_dup(V) interface makes a copy of the [sqlite3_value] -** object D and returns a pointer to that copy. ^The [sqlite3_value] returned +** object V and returns a pointer to that copy. ^The [sqlite3_value] returned ** is a [protected sqlite3_value] object even if the input is not. ** ^The sqlite3_value_dup(V) interface returns NULL if V is NULL or if a -** memory allocation fails. +** memory allocation fails. ^If V is a [pointer value], then the result +** of sqlite3_value_dup(V) is a NULL value. ** ** ^The sqlite3_value_free(V) interface frees an [sqlite3_value] object ** previously obtained from [sqlite3_value_dup()]. ^If V is a NULL pointer @@ -4421,9 +6109,9 @@ void sqlite3_value_free(sqlite3_value*); ** Implementations of aggregate SQL functions use this ** routine to allocate memory for storing their state. ** -** ^The first time the sqlite3_aggregate_context(C,N) routine is called -** for a particular aggregate function, SQLite -** allocates N of memory, zeroes out that memory, and returns a pointer +** ^The first time the sqlite3_aggregate_context(C,N) routine is called +** for a particular aggregate function, SQLite allocates +** N bytes of memory, zeroes out that memory, and returns a pointer ** to the new memory. ^On second and subsequent calls to ** sqlite3_aggregate_context() for the same aggregate function instance, ** the same buffer is returned. Sqlite3_aggregate_context() is normally @@ -4434,19 +6122,19 @@ void sqlite3_value_free(sqlite3_value*); ** In those cases, sqlite3_aggregate_context() might be called for the ** first time from within xFinal().)^ ** -** ^The sqlite3_aggregate_context(C,N) routine returns a NULL pointer +** ^The sqlite3_aggregate_context(C,N) routine returns a NULL pointer ** when first called if N is less than or equal to zero or if a memory -** allocate error occurs. +** allocation error occurs. ** ** ^(The amount of space allocated by sqlite3_aggregate_context(C,N) is -** determined by the N parameter on first successful call. Changing the -** value of N in subsequent call to sqlite3_aggregate_context() within +** determined by the N parameter on the first successful call. Changing the +** value of N in any subsequent call to sqlite3_aggregate_context() within ** the same aggregate function instance will not resize the memory ** allocation.)^ Within the xFinal callback, it is customary to set -** N=0 in calls to sqlite3_aggregate_context(C,N) so that no +** N=0 in calls to sqlite3_aggregate_context(C,N) so that no ** pointless memory allocations occur. ** -** ^SQLite automatically frees the memory allocated by +** ^SQLite automatically frees the memory allocated by ** sqlite3_aggregate_context() when the aggregate query concludes. ** ** The first parameter must be a copy of the @@ -4491,55 +6179,127 @@ sqlite3 *sqlite3_context_db_handle(sqlite3_context*); ** METHOD: sqlite3_context ** ** These functions may be used by (non-aggregate) SQL functions to -** associate metadata with argument values. If the same value is passed to -** multiple invocations of the same SQL function during query execution, under -** some circumstances the associated metadata may be preserved. An example -** of where this might be useful is in a regular-expression matching -** function. The compiled version of the regular expression can be stored as -** metadata associated with the pattern string. +** associate auxiliary data with argument values. If the same argument +** value is passed to multiple invocations of the same SQL function during +** query execution, under some circumstances the associated auxiliary data +** might be preserved. An example of where this might be useful is in a +** regular-expression matching function. The compiled version of the regular +** expression can be stored as auxiliary data associated with the pattern string. ** Then as long as the pattern string remains the same, ** the compiled regular expression can be reused on multiple ** invocations of the same function. ** -** ^The sqlite3_get_auxdata() interface returns a pointer to the metadata -** associated by the sqlite3_set_auxdata() function with the Nth argument -** value to the application-defined function. ^If there is no metadata -** associated with the function argument, this sqlite3_get_auxdata() interface +** ^The sqlite3_get_auxdata(C,N) interface returns a pointer to the auxiliary data +** associated by the sqlite3_set_auxdata(C,N,P,X) function with the Nth argument +** value to the application-defined function. ^N is zero for the left-most +** function argument. ^If there is no auxiliary data +** associated with the function argument, the sqlite3_get_auxdata(C,N) interface ** returns a NULL pointer. ** -** ^The sqlite3_set_auxdata(C,N,P,X) interface saves P as metadata for the N-th -** argument of the application-defined function. ^Subsequent +** ^The sqlite3_set_auxdata(C,N,P,X) interface saves P as auxiliary data for the +** N-th argument of the application-defined function. ^Subsequent ** calls to sqlite3_get_auxdata(C,N) return P from the most recent -** sqlite3_set_auxdata(C,N,P,X) call if the metadata is still valid or -** NULL if the metadata has been discarded. +** sqlite3_set_auxdata(C,N,P,X) call if the auxiliary data is still valid or +** NULL if the auxiliary data has been discarded. ** ^After each call to sqlite3_set_auxdata(C,N,P,X) where X is not NULL, ** SQLite will invoke the destructor function X with parameter P exactly -** once, when the metadata is discarded. -** SQLite is free to discard the metadata at any time, including: <ul> -** <li> when the corresponding function parameter changes, or -** <li> when [sqlite3_reset()] or [sqlite3_finalize()] is called for the -** SQL statement, or -** <li> when sqlite3_set_auxdata() is invoked again on the same parameter, or -** <li> during the original sqlite3_set_auxdata() call when a memory -** allocation error occurs. </ul>)^ -** -** Note the last bullet in particular. The destructor X in +** once, when the auxiliary data is discarded. +** SQLite is free to discard the auxiliary data at any time, including: <ul> +** <li> ^(when the corresponding function parameter changes)^, or +** <li> ^(when [sqlite3_reset()] or [sqlite3_finalize()] is called for the +** SQL statement)^, or +** <li> ^(when sqlite3_set_auxdata() is invoked again on the same +** parameter)^, or +** <li> ^(during the original sqlite3_set_auxdata() call when a memory +** allocation error occurs.)^ +** <li> ^(during the original sqlite3_set_auxdata() call if the function +** is evaluated during query planning instead of during query execution, +** as sometimes happens with [SQLITE_ENABLE_STAT4].)^ </ul> +** +** Note the last two bullets in particular. The destructor X in ** sqlite3_set_auxdata(C,N,P,X) might be called immediately, before the ** sqlite3_set_auxdata() interface even returns. Hence sqlite3_set_auxdata() ** should be called near the end of the function implementation and the ** function implementation should not make any use of P after -** sqlite3_set_auxdata() has been called. -** -** ^(In practice, metadata is preserved between function calls for +** sqlite3_set_auxdata() has been called. Furthermore, a call to +** sqlite3_get_auxdata() that occurs immediately after a corresponding call +** to sqlite3_set_auxdata() might still return NULL if an out-of-memory +** condition occurred during the sqlite3_set_auxdata() call or if the +** function is being evaluated during query planning rather than during +** query execution. +** +** ^(In practice, auxiliary data is preserved between function calls for ** function parameters that are compile-time constants, including literal ** values and [parameters] and expressions composed from the same.)^ ** +** The value of the N parameter to these interfaces should be non-negative. +** Future enhancements may make use of negative N values to define new +** kinds of function caching behavior. +** ** These routines must be called from the same thread in which ** the SQL function is running. +** +** See also: [sqlite3_get_clientdata()] and [sqlite3_set_clientdata()]. */ void *sqlite3_get_auxdata(sqlite3_context*, int N); void sqlite3_set_auxdata(sqlite3_context*, int N, void*, void (*)(void*)); +/* +** CAPI3REF: Database Connection Client Data +** METHOD: sqlite3 +** +** These functions are used to associate one or more named pointers +** with a [database connection]. +** A call to sqlite3_set_clientdata(D,N,P,X) causes the pointer P +** to be attached to [database connection] D using name N. Subsequent +** calls to sqlite3_get_clientdata(D,N) will return a copy of pointer P +** or a NULL pointer if there were no prior calls to +** sqlite3_set_clientdata() with the same values of D and N. +** Names are compared using strcmp() and are thus case sensitive. +** It returns 0 on success and SQLITE_NOMEM on allocation failure. +** +** If P and X are both non-NULL, then the destructor X is invoked with +** argument P on the first of the following occurrences: +** <ul> +** <li> An out-of-memory error occurs during the call to +** sqlite3_set_clientdata() which attempts to register pointer P. +** <li> A subsequent call to sqlite3_set_clientdata(D,N,P,X) is made +** with the same D and N parameters. +** <li> The database connection closes. SQLite does not make any guarantees +** about the order in which destructors are called, only that all +** destructors will be called exactly once at some point during the +** database connection closing process. +** </ul> +** +** SQLite does not do anything with client data other than invoke +** destructors on the client data at the appropriate time. The intended +** use for client data is to provide a mechanism for wrapper libraries +** to store additional information about an SQLite database connection. +** +** There is no limit (other than available memory) on the number of different +** client data pointers (with different names) that can be attached to a +** single database connection. However, the implementation is optimized +** for the case of having only one or two different client data names. +** Applications and wrapper libraries are discouraged from using more than +** one client data name each. +** +** There is no way to enumerate the client data pointers +** associated with a database connection. The N parameter can be thought +** of as a secret key such that only code that knows the secret key is able +** to access the associated data. +** +** Security Warning: These interfaces should not be exposed in scripting +** languages or in other circumstances where it might be possible for an +** attacker to invoke them. Any agent that can invoke these interfaces +** can probably also take control of the process. +** +** Database connection client data is only available for SQLite +** version 3.44.0 ([dateof:3.44.0]) and later. +** +** See also: [sqlite3_set_auxdata()] and [sqlite3_get_auxdata()]. +*/ +void *sqlite3_get_clientdata(sqlite3*,const char*); +int sqlite3_set_clientdata(sqlite3*, const char*, void*, void(*)(void*)); /* ** CAPI3REF: Constants Defining Special Destructor Behavior @@ -4591,8 +6351,9 @@ typedef void (*sqlite3_destructor_type)(void*); ** 2nd parameter of sqlite3_result_error() or sqlite3_result_error16() ** as the text of an error message. ^SQLite interprets the error ** message string from sqlite3_result_error() as UTF-8. ^SQLite -** interprets the string from sqlite3_result_error16() as UTF-16 in native -** byte order. ^If the third parameter to sqlite3_result_error() +** interprets the string from sqlite3_result_error16() as UTF-16 using +** the same [byte-order determination rules] as [sqlite3_bind_text16()]. +** ^If the third parameter to sqlite3_result_error() ** or sqlite3_result_error16() is negative then SQLite takes as the error ** message all text up through the first zero character. ** ^If the third parameter to sqlite3_result_error() or @@ -4634,15 +6395,16 @@ typedef void (*sqlite3_destructor_type)(void*); ** of [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE]. ** ^SQLite takes the text result from the application from ** the 2nd parameter of the sqlite3_result_text* interfaces. -** ^If the 3rd parameter to the sqlite3_result_text* interfaces -** is negative, then SQLite takes result text from the 2nd parameter -** through the first zero character. +** ^If the 3rd parameter to any of the sqlite3_result_text* interfaces +** other than sqlite3_result_text64() is negative, then SQLite computes +** the string length itself by searching the 2nd parameter for the first +** zero character. ** ^If the 3rd parameter to the sqlite3_result_text* interfaces ** is non-negative, then as many bytes (not characters) of the text ** pointed to by the 2nd parameter are taken as the application-defined ** function result. If the 3rd parameter is non-negative, then it ** must be the byte offset into the string where the NUL terminator would -** appear if the string where NUL terminated. If any NUL characters occur +** appear if the string were NUL terminated. If any NUL characters occur ** in the string at a byte offset that is less than the value of the 3rd ** parameter, then the resulting string will contain embedded NULs and the ** result of expressions operating on strings with embedded NULs is undefined. @@ -4657,9 +6419,28 @@ typedef void (*sqlite3_destructor_type)(void*); ** when it has finished using that result. ** ^If the 4th parameter to the sqlite3_result_text* interfaces ** or sqlite3_result_blob is the special constant SQLITE_TRANSIENT -** then SQLite makes a copy of the result into space obtained from +** then SQLite makes a copy of the result into space obtained ** from [sqlite3_malloc()] before it returns. ** +** ^For the sqlite3_result_text16(), sqlite3_result_text16le(), and +** sqlite3_result_text16be() routines, and for sqlite3_result_text64() +** when the encoding is not UTF8, if the input UTF16 begins with a +** byte-order mark (BOM, U+FEFF) then the BOM is removed from the +** string and the rest of the string is interpreted according to the +** byte-order specified by the BOM. ^The byte-order specified by +** the BOM at the beginning of the text overrides the byte-order +** specified by the interface procedure. ^So, for example, if +** sqlite3_result_text16le() is invoked with text that begins +** with bytes 0xfe, 0xff (a big-endian byte-order mark) then the +** first two bytes of input are skipped and the remaining input +** is interpreted as UTF16BE text. +** +** ^For UTF16 input text to the sqlite3_result_text16(), +** sqlite3_result_text16be(), sqlite3_result_text16le(), and +** sqlite3_result_text64() routines, if the text contains invalid +** UTF16 characters, the invalid characters might be converted +** into the unicode replacement character, U+FFFD. +** ** ^The sqlite3_result_value() interface sets the result of ** the application-defined function to be a copy of the ** [unprotected sqlite3_value] object specified by the 2nd parameter. ^The @@ -4670,7 +6451,18 @@ typedef void (*sqlite3_destructor_type)(void*); ** [unprotected sqlite3_value] object is required, so either ** kind of [sqlite3_value] object can be used with this interface. ** -** If these routines are called from within the different thread +** ^The sqlite3_result_pointer(C,P,T,D) interface sets the result to an +** SQL NULL value, just like [sqlite3_result_null(C)], except that it +** also associates the host-language pointer P or type T with that +** NULL value such that the pointer can be retrieved within an +** [application-defined SQL function] using [sqlite3_value_pointer()]. +** ^If the D parameter is not NULL, then it is a pointer to a destructor +** for the P parameter. ^SQLite invokes D with P as its only argument +** when SQLite is finished with P. The T parameter should be a static +** string and preferably a string literal. The sqlite3_result_pointer() +** routine is part of the [pointer passing interface] added for SQLite 3.20.0. +** +** If these routines are called from within a different thread ** than the one containing the application-defined function that received ** the [sqlite3_context] pointer, the results are undefined. */ @@ -4693,6 +6485,7 @@ void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*)); void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*)); void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*)); void sqlite3_result_value(sqlite3_context*, sqlite3_value*); +void sqlite3_result_pointer(sqlite3_context*, void*,const char*,void(*)(void*)); void sqlite3_result_zeroblob(sqlite3_context*, int n); int sqlite3_result_zeroblob64(sqlite3_context*, sqlite3_uint64 n); @@ -4702,12 +6495,26 @@ int sqlite3_result_zeroblob64(sqlite3_context*, sqlite3_uint64 n); ** METHOD: sqlite3_context ** ** The sqlite3_result_subtype(C,T) function causes the subtype of -** the result from the [application-defined SQL function] with -** [sqlite3_context] C to be the value T. Only the lower 8 bits +** the result from the [application-defined SQL function] with +** [sqlite3_context] C to be the value T. Only the lower 8 bits ** of the subtype T are preserved in current versions of SQLite; ** higher order bits are discarded. ** The number of subtype bytes preserved by SQLite might increase ** in future releases of SQLite. +** +** Every [application-defined SQL function] that invokes this interface +** should include the [SQLITE_RESULT_SUBTYPE] property in its +** text encoding argument when the SQL function is +** [sqlite3_create_function|registered]. If the [SQLITE_RESULT_SUBTYPE] +** property is omitted from the function that invokes sqlite3_result_subtype(), +** then in some cases the sqlite3_result_subtype() might fail to set +** the result subtype. +** +** If SQLite is compiled with -DSQLITE_STRICT_SUBTYPE=1, then any +** SQL function that invokes the sqlite3_result_subtype() interface +** and that does not have the SQLITE_RESULT_SUBTYPE property will raise +** an error. Future versions of SQLite might enable -DSQLITE_STRICT_SUBTYPE=1 +** by default. */ void sqlite3_result_subtype(sqlite3_context*,unsigned int); @@ -4733,7 +6540,7 @@ void sqlite3_result_subtype(sqlite3_context*,unsigned int); ** <li> [SQLITE_UTF16_ALIGNED]. ** </ul>)^ ** ^The eTextRep argument determines the encoding of strings passed -** to the collating function callback, xCallback. +** to the collating function callback, xCompare. ** ^The [SQLITE_UTF16] and [SQLITE_UTF16_ALIGNED] values for eTextRep ** force strings to be UTF16 with native byte order. ** ^The [SQLITE_UTF16_ALIGNED] value for eTextRep forces strings to begin @@ -4742,18 +6549,19 @@ void sqlite3_result_subtype(sqlite3_context*,unsigned int); ** ^The fourth argument, pArg, is an application data pointer that is passed ** through as the first argument to the collating function callback. ** -** ^The fifth argument, xCallback, is a pointer to the collating function. +** ^The fifth argument, xCompare, is a pointer to the collating function. ** ^Multiple collating functions can be registered using the same name but ** with different eTextRep parameters and SQLite will use whichever ** function requires the least amount of data transformation. -** ^If the xCallback argument is NULL then the collating function is +** ^If the xCompare argument is NULL then the collating function is ** deleted. ^When all collating functions having the same name are deleted, ** that collation is no longer usable. ** -** ^The collating function callback is invoked with a copy of the pArg +** ^The collating function callback is invoked with a copy of the pArg ** application data pointer and with two strings in the encoding specified -** by the eTextRep argument. The collating function must return an -** integer that is negative, zero, or positive +** by the eTextRep argument. The two integer parameters to the collating +** function callback are the length of the two strings, in bytes. The collating +** function must return an integer that is negative, zero, or positive ** if the first string is less than, equal to, or greater than the second, ** respectively. A collating function must always return the same answer ** given the same inputs. If two or more collating functions are registered @@ -4770,7 +6578,7 @@ void sqlite3_result_subtype(sqlite3_context*,unsigned int); ** </ol> ** ** If a collating function fails any of the above constraints and that -** collating function is registered and used, then the behavior of SQLite +** collating function is registered and used, then the behavior of SQLite ** is undefined. ** ** ^The sqlite3_create_collation_v2() works like sqlite3_create_collation() @@ -4780,36 +6588,36 @@ void sqlite3_result_subtype(sqlite3_context*,unsigned int); ** calls to the collation creation functions or when the ** [database connection] is closed using [sqlite3_close()]. ** -** ^The xDestroy callback is <u>not</u> called if the +** ^The xDestroy callback is <u>not</u> called if the ** sqlite3_create_collation_v2() function fails. Applications that invoke -** sqlite3_create_collation_v2() with a non-NULL xDestroy argument should +** sqlite3_create_collation_v2() with a non-NULL xDestroy argument should ** check the return code and dispose of the application data pointer ** themselves rather than expecting SQLite to deal with it for them. -** This is different from every other SQLite interface. The inconsistency -** is unfortunate but cannot be changed without breaking backwards +** This is different from every other SQLite interface. The inconsistency +** is unfortunate but cannot be changed without breaking backwards ** compatibility. ** ** See also: [sqlite3_collation_needed()] and [sqlite3_collation_needed16()]. */ int sqlite3_create_collation( - sqlite3*, - const char *zName, - int eTextRep, + sqlite3*, + const char *zName, + int eTextRep, void *pArg, int(*xCompare)(void*,int,const void*,int,const void*) ); int sqlite3_create_collation_v2( - sqlite3*, - const char *zName, - int eTextRep, + sqlite3*, + const char *zName, + int eTextRep, void *pArg, int(*xCompare)(void*,int,const void*,int,const void*), void(*xDestroy)(void*) ); int sqlite3_create_collation16( - sqlite3*, + sqlite3*, const void *zName, - int eTextRep, + int eTextRep, void *pArg, int(*xCompare)(void*,int,const void*,int,const void*) ); @@ -4842,16 +6650,17 @@ int sqlite3_create_collation16( ** [sqlite3_create_collation_v2()]. */ int sqlite3_collation_needed( - sqlite3*, - void*, + sqlite3*, + void*, void(*)(void*,sqlite3*,int eTextRep,const char*) ); int sqlite3_collation_needed16( - sqlite3*, + sqlite3*, void*, void(*)(void*,sqlite3*,int eTextRep,const void*) ); +/* BEGIN SQLCIPHER */ #ifdef SQLITE_HAS_CODEC /* ** Specify the key for an encrypted database. This routine should be @@ -4878,6 +6687,19 @@ int sqlite3_key_v2( ** The code to implement this API is not available in the public release ** of SQLite. */ +/* SQLCipher usage note: + + If the current database is plaintext SQLCipher will NOT encrypt it. + If the current database is encrypted and pNew==0 or nNew==0, SQLCipher + will NOT decrypt it. + + This routine will ONLY work on an already encrypted database in order + to change the key. + + Conversion from plaintext-to-encrypted or encrypted-to-plaintext should + use an ATTACHed database and the sqlcipher_export() convenience function + as per the SQLCipher Documentation. +*/ int sqlite3_rekey( sqlite3 *db, /* Database to be rekeyed */ const void *pKey, int nKey /* The new key */ @@ -4896,10 +6718,11 @@ void sqlite3_activate_see( const char *zPassPhrase /* Activation phrase */ ); #endif +/* END SQLCIPHER */ #ifdef SQLITE_ENABLE_CEROD /* -** Specify the activation key for a CEROD database. Unless +** Specify the activation key for a CEROD database. Unless ** activated, none of the CEROD routines will work. */ void sqlite3_activate_cerod( @@ -4923,6 +6746,13 @@ void sqlite3_activate_cerod( ** of the default VFS is not implemented correctly, or not implemented at ** all, then the behavior of sqlite3_sleep() may deviate from the description ** in the previous paragraphs. +** +** If a negative argument is passed to sqlite3_sleep() the results vary by +** VFS and operating system. Some system treat a negative argument as an +** instruction to sleep forever. Others understand it to mean do not sleep +** at all. ^In SQLite version 3.42.0 and later, a negative +** argument passed into sqlite3_sleep() is changed to zero before it is relayed +** down into the xSleep method of the VFS. */ int sqlite3_sleep(int); @@ -4955,7 +6785,7 @@ int sqlite3_sleep(int); ** ^The [temp_store_directory pragma] may modify this variable and cause ** it to point to memory obtained from [sqlite3_malloc]. ^Furthermore, ** the [temp_store_directory pragma] always assumes that any string -** that this variable points to is held in memory obtained from +** that this variable points to is held in memory obtained from ** [sqlite3_malloc] and the pragma may attempt to free that memory ** using [sqlite3_free]. ** Hence, if this variable is modified directly, either it should be @@ -5012,7 +6842,7 @@ SQLITE_EXTERN char *sqlite3_temp_directory; ** ^The [data_store_directory pragma] may modify this variable and cause ** it to point to memory obtained from [sqlite3_malloc]. ^Furthermore, ** the [data_store_directory pragma] always assumes that any string -** that this variable points to is held in memory obtained from +** that this variable points to is held in memory obtained from ** [sqlite3_malloc] and the pragma may attempt to free that memory ** using [sqlite3_free]. ** Hence, if this variable is modified directly, either it should be @@ -5021,6 +6851,41 @@ SQLITE_EXTERN char *sqlite3_temp_directory; */ SQLITE_EXTERN char *sqlite3_data_directory; +/* +** CAPI3REF: Win32 Specific Interface +** +** These interfaces are available only on Windows. The +** [sqlite3_win32_set_directory] interface is used to set the value associated +** with the [sqlite3_temp_directory] or [sqlite3_data_directory] variable, to +** zValue, depending on the value of the type parameter. The zValue parameter +** should be NULL to cause the previous value to be freed via [sqlite3_free]; +** a non-NULL value will be copied into memory obtained from [sqlite3_malloc] +** prior to being used. The [sqlite3_win32_set_directory] interface returns +** [SQLITE_OK] to indicate success, [SQLITE_ERROR] if the type is unsupported, +** or [SQLITE_NOMEM] if memory could not be allocated. The value of the +** [sqlite3_data_directory] variable is intended to act as a replacement for +** the current directory on the sub-platforms of Win32 where that concept is +** not present, e.g. WinRT and UWP. The [sqlite3_win32_set_directory8] and +** [sqlite3_win32_set_directory16] interfaces behave exactly the same as the +** sqlite3_win32_set_directory interface except the string parameter must be +** UTF-8 or UTF-16, respectively. +*/ +int sqlite3_win32_set_directory( + unsigned long type, /* Identifier for directory being set or reset */ + void *zValue /* New value for directory being set or reset */ +); +int sqlite3_win32_set_directory8(unsigned long type, const char *zValue); +int sqlite3_win32_set_directory16(unsigned long type, const void *zValue); + +/* +** CAPI3REF: Win32 Directory Types +** +** These macros are only available on Windows. They define the allowed values +** for the type argument to the [sqlite3_win32_set_directory] interface. +*/ +#define SQLITE_WIN32_DATA_DIRECTORY_TYPE 1 +#define SQLITE_WIN32_TEMP_DIRECTORY_TYPE 2 + /* ** CAPI3REF: Test For Auto-Commit Mode ** KEYWORDS: {autocommit mode} @@ -5058,22 +6923,59 @@ int sqlite3_get_autocommit(sqlite3*); */ sqlite3 *sqlite3_db_handle(sqlite3_stmt*); +/* +** CAPI3REF: Return The Schema Name For A Database Connection +** METHOD: sqlite3 +** +** ^The sqlite3_db_name(D,N) interface returns a pointer to the schema name +** for the N-th database on database connection D, or a NULL pointer if N is +** out of range. An N value of 0 means the main database file. An N of 1 is +** the "temp" schema. Larger values of N correspond to various ATTACH-ed +** databases. +** +** Space to hold the string that is returned by sqlite3_db_name() is managed +** by SQLite itself. The string might be deallocated by any operation that +** changes the schema, including [ATTACH] or [DETACH] or calls to +** [sqlite3_serialize()] or [sqlite3_deserialize()], even operations that +** occur on a different thread. Applications that need to +** remember the string long-term should make their own copy. Applications that +** are accessing the same database connection simultaneously on multiple +** threads should mutex-protect calls to this API and should make their own +** private copy of the result prior to releasing the mutex. +*/ +const char *sqlite3_db_name(sqlite3 *db, int N); + /* ** CAPI3REF: Return The Filename For A Database Connection ** METHOD: sqlite3 ** -** ^The sqlite3_db_filename(D,N) interface returns a pointer to a filename -** associated with database N of connection D. ^The main database file -** has the name "main". If there is no attached database N on the database +** ^The sqlite3_db_filename(D,N) interface returns a pointer to the filename +** associated with database N of connection D. +** ^If there is no attached database N on the database ** connection D, or if database N is a temporary or in-memory database, then -** a NULL pointer is returned. +** this function will return either a NULL pointer or an empty string. +** +** ^The string value returned by this routine is owned and managed by +** the database connection. ^The value will be valid until the database N +** is [DETACH]-ed or until the database connection closes. ** ** ^The filename returned by this function is the output of the ** xFullPathname method of the [VFS]. ^In other words, the filename ** will be an absolute pathname, even if the filename used ** to open the database originally was a URI or relative pathname. +** +** If the filename pointer returned by this routine is not NULL, then it +** can be used as the filename input parameter to these routines: +** <ul> +** <li> [sqlite3_uri_parameter()] +** <li> [sqlite3_uri_boolean()] +** <li> [sqlite3_uri_int64()] +** <li> [sqlite3_filename_database()] +** <li> [sqlite3_filename_journal()] +** <li> [sqlite3_filename_wal()] +** </ul> */ -const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName); +sqlite3_filename sqlite3_db_filename(sqlite3 *db, const char *zDbName); /* ** CAPI3REF: Determine if a database is read-only @@ -5085,6 +6987,57 @@ const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName); */ int sqlite3_db_readonly(sqlite3 *db, const char *zDbName); +/* +** CAPI3REF: Determine the transaction state of a database +** METHOD: sqlite3 +** +** ^The sqlite3_txn_state(D,S) interface returns the current +** [transaction state] of schema S in database connection D. ^If S is NULL, +** then the highest transaction state of any schema on database connection D +** is returned. Transaction states are (in order of lowest to highest): +** <ol> +** <li value="0"> SQLITE_TXN_NONE +** <li value="1"> SQLITE_TXN_READ +** <li value="2"> SQLITE_TXN_WRITE +** </ol> +** ^If the S argument to sqlite3_txn_state(D,S) is not the name of +** a valid schema, then -1 is returned. +*/ +int sqlite3_txn_state(sqlite3*,const char *zSchema); + +/* +** CAPI3REF: Allowed return values from sqlite3_txn_state() +** KEYWORDS: {transaction state} +** +** These constants define the current transaction state of a database file. +** ^The [sqlite3_txn_state(D,S)] interface returns one of these +** constants in order to describe the transaction state of schema S +** in [database connection] D. +** +** <dl> +** [[SQLITE_TXN_NONE]] <dt>SQLITE_TXN_NONE</dt> +** <dd>The SQLITE_TXN_NONE state means that no transaction is currently +** pending.</dd> +** +** [[SQLITE_TXN_READ]] <dt>SQLITE_TXN_READ</dt> +** <dd>The SQLITE_TXN_READ state means that the database is currently +** in a read transaction. Content has been read from the database file +** but nothing in the database file has changed. The transaction state +** will be advanced to SQLITE_TXN_WRITE if any changes occur and there are +** no other conflicting concurrent write transactions. The transaction +** state will revert to SQLITE_TXN_NONE following a [ROLLBACK] or +** [COMMIT].</dd> +** +** [[SQLITE_TXN_WRITE]] <dt>SQLITE_TXN_WRITE</dt> +** <dd>The SQLITE_TXN_WRITE state means that the database is currently +** in a write transaction. Content has been written to the database file +** but has not yet committed. The transaction state will change to +** SQLITE_TXN_NONE at the next [ROLLBACK] or [COMMIT].</dd> +*/ +#define SQLITE_TXN_NONE 0 +#define SQLITE_TXN_READ 1 +#define SQLITE_TXN_WRITE 2 + /* ** CAPI3REF: Find the next prepared statement ** METHOD: sqlite3 @@ -5151,6 +7104,72 @@ sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt); void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*); void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*); +/* +** CAPI3REF: Autovacuum Compaction Amount Callback +** METHOD: sqlite3 +** +** ^The sqlite3_autovacuum_pages(D,C,P,X) interface registers a callback +** function C that is invoked prior to each autovacuum of the database +** file. ^The callback is passed a copy of the generic data pointer (P), +** the schema-name of the attached database that is being autovacuumed, +** the size of the database file in pages, the number of free pages, +** and the number of bytes per page, respectively. The callback should +** return the number of free pages that should be removed by the +** autovacuum. ^If the callback returns zero, then no autovacuum happens. +** ^If the value returned is greater than or equal to the number of +** free pages, then a complete autovacuum happens. +** +** <p>^If there are multiple ATTACH-ed database files that are being +** modified as part of a transaction commit, then the autovacuum pages +** callback is invoked separately for each file. +** +** <p><b>The callback is not reentrant.</b> The callback function should +** not attempt to invoke any other SQLite interface. If it does, bad +** things may happen, including segmentation faults and corrupt database +** files. The callback function should be a simple function that +** does some arithmetic on its input parameters and returns a result. +** +** ^The X parameter to sqlite3_autovacuum_pages(D,C,P,X) is an optional +** destructor for the P parameter. ^If X is not NULL, then X(P) is +** invoked whenever the database connection closes or when the callback +** is overwritten by another invocation of sqlite3_autovacuum_pages(). +** +** <p>^There is only one autovacuum pages callback per database connection. +** ^Each call to the sqlite3_autovacuum_pages() interface overrides all +** previous invocations for that database connection. ^If the callback +** argument (C) to sqlite3_autovacuum_pages(D,C,P,X) is a NULL pointer, +** then the autovacuum steps callback is canceled. The return value +** from sqlite3_autovacuum_pages() is normally SQLITE_OK, but might +** be some other error code if something goes wrong. The current +** implementation will only return SQLITE_OK or SQLITE_MISUSE, but other +** return codes might be added in future releases. +** +** <p>If no autovacuum pages callback is specified (the usual case) or +** a NULL pointer is provided for the callback, +** then the default behavior is to vacuum all free pages. So, in other +** words, the default behavior is the same as if the callback function +** were something like this: +** +** <blockquote><pre> +** &nbsp; unsigned int demonstration_autovac_pages_callback( +** &nbsp; void *pClientData, +** &nbsp; const char *zSchema, +** &nbsp; unsigned int nDbPage, +** &nbsp; unsigned int nFreePage, +** &nbsp; unsigned int nBytePerPage +** &nbsp; ){ +** &nbsp; return nFreePage; +** &nbsp; } +** </pre></blockquote> +*/ +int sqlite3_autovacuum_pages( + sqlite3 *db, + unsigned int(*)(void*,const char*,unsigned int,unsigned int,unsigned int), + void*, + void(*)(void*) +); + + /* ** CAPI3REF: Data Change Notification Callbacks ** METHOD: sqlite3 @@ -5158,12 +7177,14 @@ void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*); ** ^The sqlite3_update_hook() interface registers a callback function ** with the [database connection] identified by the first argument ** to be invoked whenever a row is updated, inserted or deleted in -** a rowid table. +** a [rowid table]. ** ^Any callback set by a previous call to this function ** for the same database connection is overridden. ** ** ^The second argument is a pointer to the function to invoke when a ** row is updated, inserted or deleted in a rowid table. +** ^The update hook is disabled by invoking sqlite3_update_hook() +** with a NULL pointer as the second parameter. ** ^The first argument to the callback is a copy of the third argument ** to sqlite3_update_hook(). ** ^The second callback argument is one of [SQLITE_INSERT], [SQLITE_DELETE], @@ -5175,16 +7196,22 @@ void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*); ** ^In the case of an update, this is the [rowid] after the update takes place. ** ** ^(The update hook is not invoked when internal system tables are -** modified (i.e. sqlite_master and sqlite_sequence).)^ +** modified (i.e. sqlite_sequence).)^ ** ^The update hook is not invoked when [WITHOUT ROWID] tables are modified. ** ** ^In the current implementation, the update hook -** is not invoked when duplication rows are deleted because of an +** is not invoked when conflicting rows are deleted because of an ** [ON CONFLICT | ON CONFLICT REPLACE] clause. ^Nor is the update hook ** invoked when rows are deleted using the [truncate optimization]. ** The exceptions defined in this paragraph might change in a future ** release of SQLite. ** +** Whether the update hook is invoked before or after the +** corresponding change is currently unspecified and may differ +** depending on the type of change. Do not rely on the order of the +** hook call with regards to the final result of the operation which +** triggers the hook. +** ** The update hook implementation must not do anything that will modify ** the database connection that invoked the update hook. Any actions ** to modify the database connection must be deferred until after the @@ -5197,11 +7224,11 @@ void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*); ** on the same [database connection] D, or NULL for ** the first call on D. ** -** See also the [sqlite3_commit_hook()] and [sqlite3_rollback_hook()] -** interfaces. +** See also the [sqlite3_commit_hook()], [sqlite3_rollback_hook()], +** and [sqlite3_preupdate_hook()] interfaces. */ void *sqlite3_update_hook( - sqlite3*, + sqlite3*, void(*)(void *,int ,char const *,char const *,sqlite3_int64), void* ); @@ -5214,25 +7241,35 @@ void *sqlite3_update_hook( ** to the same database. Sharing is enabled if the argument is true ** and disabled if the argument is false.)^ ** +** This interface is omitted if SQLite is compiled with +** [-DSQLITE_OMIT_SHARED_CACHE]. The [-DSQLITE_OMIT_SHARED_CACHE] +** compile-time option is recommended because the +** [use of shared cache mode is discouraged]. +** ** ^Cache sharing is enabled and disabled for an entire process. -** This is a change as of SQLite version 3.5.0. In prior versions of SQLite, +** This is a change as of SQLite [version 3.5.0] ([dateof:3.5.0]). +** In prior versions of SQLite, ** sharing was enabled or disabled for each thread separately. ** ** ^(The cache sharing mode set by this interface effects all subsequent ** calls to [sqlite3_open()], [sqlite3_open_v2()], and [sqlite3_open16()]. -** Existing database connections continue use the sharing mode +** Existing database connections continue to use the sharing mode ** that was in effect at the time they were opened.)^ ** ** ^(This routine returns [SQLITE_OK] if shared cache was enabled or disabled ** successfully. An [error code] is returned otherwise.)^ ** -** ^Shared cache is disabled by default. But this might change in -** future releases of SQLite. Applications that care about shared -** cache setting should set it explicitly. +** ^Shared cache is disabled by default. It is recommended that it stay +** that way. In other words, do not use this routine. This interface +** continues to be provided for historical compatibility, but its use is +** discouraged. Any use of shared cache is discouraged. If shared cache +** must be used, it is recommended that shared cache only be enabled for +** individual database connections using the [sqlite3_open_v2()] interface +** with the [SQLITE_OPEN_SHAREDCACHE] flag. ** ** Note: This method is disabled on MacOS X 10.7 and iOS version 5.0 -** and will always return SQLITE_MISUSE. On those systems, -** shared cache mode should be enabled per-database connection via +** and will always return SQLITE_MISUSE. On those systems, +** shared cache mode should be enabled per-database connection via ** [sqlite3_open_v2()] with [SQLITE_OPEN_SHAREDCACHE]. ** ** This interface is threadsafe on processors where writing a @@ -5275,6 +7312,9 @@ int sqlite3_db_release_memory(sqlite3*); /* ** CAPI3REF: Impose A Limit On Heap Size ** +** These interfaces impose limits on the amount of heap memory that will be +** used by all database connections within a single process. +** ** ^The sqlite3_soft_heap_limit64() interface sets and/or queries the ** soft limit on the amount of heap memory that may be allocated by SQLite. ** ^SQLite strives to keep heap memory utilization below the soft heap @@ -5282,23 +7322,44 @@ int sqlite3_db_release_memory(sqlite3*); ** as heap memory usages approaches the limit. ** ^The soft heap limit is "soft" because even though SQLite strives to stay ** below the limit, it will exceed the limit rather than generate -** an [SQLITE_NOMEM] error. In other words, the soft heap limit +** an [SQLITE_NOMEM] error. In other words, the soft heap limit ** is advisory only. ** -** ^The return value from sqlite3_soft_heap_limit64() is the size of -** the soft heap limit prior to the call, or negative in the case of an -** error. ^If the argument N is negative -** then no change is made to the soft heap limit. Hence, the current -** size of the soft heap limit can be determined by invoking -** sqlite3_soft_heap_limit64() with a negative argument. -** -** ^If the argument N is zero then the soft heap limit is disabled. +** ^The sqlite3_hard_heap_limit64(N) interface sets a hard upper bound of +** N bytes on the amount of memory that will be allocated. ^The +** sqlite3_hard_heap_limit64(N) interface is similar to +** sqlite3_soft_heap_limit64(N) except that memory allocations will fail +** when the hard heap limit is reached. ** -** ^(The soft heap limit is not enforced in the current implementation +** ^The return value from both sqlite3_soft_heap_limit64() and +** sqlite3_hard_heap_limit64() is the size of +** the heap limit prior to the call, or negative in the case of an +** error. ^If the argument N is negative +** then no change is made to the heap limit. Hence, the current +** size of heap limits can be determined by invoking +** sqlite3_soft_heap_limit64(-1) or sqlite3_hard_heap_limit(-1). +** +** ^Setting the heap limits to zero disables the heap limiter mechanism. +** +** ^The soft heap limit may not be greater than the hard heap limit. +** ^If the hard heap limit is enabled and if sqlite3_soft_heap_limit(N) +** is invoked with a value of N that is greater than the hard heap limit, +** the soft heap limit is set to the value of the hard heap limit. +** ^The soft heap limit is automatically enabled whenever the hard heap +** limit is enabled. ^When sqlite3_hard_heap_limit64(N) is invoked and +** the soft heap limit is outside the range of 1..N, then the soft heap +** limit is set to N. ^Invoking sqlite3_soft_heap_limit64(0) when the +** hard heap limit is enabled makes the soft heap limit equal to the +** hard heap limit. +** +** The memory allocation limits can also be adjusted using +** [PRAGMA soft_heap_limit] and [PRAGMA hard_heap_limit]. +** +** ^(The heap limits are not enforced in the current implementation ** if one or more of following conditions are true: ** ** <ul> -** <li> The soft heap limit is set to zero. +** <li> The limit value is set to zero. ** <li> Memory accounting is disabled using a combination of the ** [sqlite3_config]([SQLITE_CONFIG_MEMSTATUS],...) start-time option and ** the [SQLITE_DEFAULT_MEMSTATUS] compile-time option. @@ -5309,20 +7370,11 @@ int sqlite3_db_release_memory(sqlite3*); ** from the heap. ** </ul>)^ ** -** Beginning with SQLite version 3.7.3, the soft heap limit is enforced -** regardless of whether or not the [SQLITE_ENABLE_MEMORY_MANAGEMENT] -** compile-time option is invoked. With [SQLITE_ENABLE_MEMORY_MANAGEMENT], -** the soft heap limit is enforced on every memory allocation. Without -** [SQLITE_ENABLE_MEMORY_MANAGEMENT], the soft heap limit is only enforced -** when memory is allocated by the page cache. Testing suggests that because -** the page cache is the predominate memory user in SQLite, most -** applications will achieve adequate soft heap limit enforcement without -** the use of [SQLITE_ENABLE_MEMORY_MANAGEMENT]. -** -** The circumstances under which SQLite will enforce the soft heap limit may -** changes in future releases of SQLite. +** The circumstances under which SQLite will enforce the heap limits may +** change in future releases of SQLite. */ sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 N); +sqlite3_int64 sqlite3_hard_heap_limit64(sqlite3_int64 N); /* ** CAPI3REF: Deprecated Soft Heap Limit Interface @@ -5346,11 +7398,13 @@ SQLITE_DEPRECATED void sqlite3_soft_heap_limit(int N); ** interface returns SQLITE_OK and fills in the non-NULL pointers in ** the final five arguments with appropriate values if the specified ** column exists. ^The sqlite3_table_column_metadata() interface returns -** SQLITE_ERROR and if the specified column does not exist. +** SQLITE_ERROR if the specified column does not exist. ** ^If the column-name parameter to sqlite3_table_column_metadata() is a -** NULL pointer, then this routine simply checks for the existance of the +** NULL pointer, then this routine simply checks for the existence of the ** table and returns SQLITE_OK if the table exists and SQLITE_ERROR if it -** does not. +** does not. If the table name parameter T in a call to +** sqlite3_table_column_metadata(X,D,T,C,...) is NULL then the result is +** undefined behavior. ** ** ^The column is identified by the second, third and fourth parameters to ** this function. ^(The second parameter is either the name of the database @@ -5384,7 +7438,7 @@ SQLITE_DEPRECATED void sqlite3_soft_heap_limit(int N); ** ** ^If the specified table is actually a view, an [error code] is returned. ** -** ^If the specified column is "rowid", "oid" or "_rowid_" and the table +** ^If the specified column is "rowid", "oid" or "_rowid_" and the table ** is not a [WITHOUT ROWID] table and an ** [INTEGER PRIMARY KEY] column has been explicitly declared, then the output ** parameters are set for the explicitly declared column. ^(If there is no @@ -5432,8 +7486,8 @@ int sqlite3_table_column_metadata( ** ^The entry point is zProc. ** ^(zProc may be 0, in which case SQLite will try to come up with an ** entry point name on its own. It first tries "sqlite3_extension_init". -** If that does not work, it constructs a name "sqlite3_X_init" where the -** X is consists of the lower-case equivalent of all ASCII alphabetic +** If that does not work, it constructs a name "sqlite3_X_init" where +** X consists of the lower-case equivalent of all ASCII alphabetic ** characters in the filename from the last "/" to the first following ** "." and omitting any initial "lib".)^ ** ^The sqlite3_load_extension() interface returns @@ -5445,9 +7499,18 @@ int sqlite3_table_column_metadata( ** should free this memory by calling [sqlite3_free()]. ** ** ^Extension loading must be enabled using -** [sqlite3_enable_load_extension()] prior to calling this API, +** [sqlite3_enable_load_extension()] or +** [sqlite3_db_config](db,[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION],1,NULL) +** prior to calling this API, ** otherwise an error will be returned. ** +** <b>Security warning:</b> It is recommended that the +** [SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION] method be used to enable only this +** interface. The use of the [sqlite3_enable_load_extension()] interface +** should be avoided. This will keep the SQL function [load_extension()] +** disabled and prevent SQL injections from giving attackers +** access to extension loading capabilities. +** ** See also the [load_extension() SQL function]. */ int sqlite3_load_extension( @@ -5470,6 +7533,17 @@ int sqlite3_load_extension( ** ^Call the sqlite3_enable_load_extension() routine with onoff==1 ** to turn extension loading on and call it with onoff==0 to turn ** it back off again. +** +** ^This interface enables or disables both the C-API +** [sqlite3_load_extension()] and the SQL function [load_extension()]. +** ^(Use [sqlite3_db_config](db,[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION],..) +** to enable or disable only the C-API.)^ +** +** <b>Security warning:</b> It is recommended that extension loading +** be enabled using the [SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION] method +** rather than this interface, so the [load_extension()] SQL function +** remains disabled. This will prevent SQL injections from giving attackers +** access to extension loading capabilities. */ int sqlite3_enable_load_extension(sqlite3 *db, int onoff); @@ -5483,8 +7557,8 @@ int sqlite3_enable_load_extension(sqlite3 *db, int onoff); ** ** ^(Even though the function prototype shows that xEntryPoint() takes ** no arguments and returns void, SQLite invokes xEntryPoint() with three -** arguments and expects and integer result as if the signature of the -** entry point where as follows: +** arguments and expects an integer result as if the signature of the +** entry point were as follows: ** ** <blockquote><pre> ** &nbsp; int xEntryPoint( @@ -5509,7 +7583,7 @@ int sqlite3_enable_load_extension(sqlite3 *db, int onoff); ** See also: [sqlite3_reset_auto_extension()] ** and [sqlite3_cancel_auto_extension()] */ -int sqlite3_auto_extension(void (*xEntryPoint)(void)); +int sqlite3_auto_extension(void(*xEntryPoint)(void)); /* ** CAPI3REF: Cancel Automatic Extension Loading @@ -5517,11 +7591,11 @@ int sqlite3_auto_extension(void (*xEntryPoint)(void)); ** ^The [sqlite3_cancel_auto_extension(X)] interface unregisters the ** initialization routine X that was registered using a prior call to ** [sqlite3_auto_extension(X)]. ^The [sqlite3_cancel_auto_extension(X)] -** routine returns 1 if initialization routine X was successfully +** routine returns 1 if initialization routine X was successfully ** unregistered and it returns 0 if X was not on the list of initialization ** routines. */ -int sqlite3_cancel_auto_extension(void (*xEntryPoint)(void)); +int sqlite3_cancel_auto_extension(void(*xEntryPoint)(void)); /* ** CAPI3REF: Reset Automatic Extension Loading @@ -5531,15 +7605,6 @@ int sqlite3_cancel_auto_extension(void (*xEntryPoint)(void)); */ void sqlite3_reset_auto_extension(void); -/* -** The interface to the virtual-table mechanism is currently considered -** to be experimental. The interface might change in incompatible ways. -** If this is a problem for you, do not use the interface at this time. -** -** When the virtual-table mechanism stabilizes, we will declare the -** interface fixed, support it indefinitely, and remove this comment. -*/ - /* ** Structures used by the virtual table interface */ @@ -5552,8 +7617,8 @@ typedef struct sqlite3_module sqlite3_module; ** CAPI3REF: Virtual Table Object ** KEYWORDS: sqlite3_module {virtual table module} ** -** This structure, sometimes called a "virtual table module", -** defines the implementation of a [virtual tables]. +** This structure, sometimes called a "virtual table module", +** defines the implementation of a [virtual table]. ** This structure consists mostly of methods for the module. ** ** ^A virtual table module is created by filling in a persistent @@ -5592,11 +7657,18 @@ struct sqlite3_module { void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), void **ppArg); int (*xRename)(sqlite3_vtab *pVtab, const char *zNew); - /* The methods above are in version 1 of the sqlite_module object. Those + /* The methods above are in version 1 of the sqlite_module object. Those ** below are for version 2 and greater. */ int (*xSavepoint)(sqlite3_vtab *pVTab, int); int (*xRelease)(sqlite3_vtab *pVTab, int); int (*xRollbackTo)(sqlite3_vtab *pVTab, int); + /* The methods above are in versions 1 and 2 of the sqlite_module object. + ** Those below are for version 3 and greater. */ + int (*xShadowName)(const char*); + /* The methods above are in versions 1 through 3 of the sqlite_module object. + ** Those below are for version 4 and greater. */ + int (*xIntegrity)(sqlite3_vtab *pVTab, const char *zSchema, + const char *zTabName, int mFlags, char **pzErr); }; /* @@ -5639,7 +7711,7 @@ struct sqlite3_module { ** required by SQLite. If the table has at least 64 columns and any column ** to the right of the first 63 is required, then bit 63 of colUsed is also ** set. In other words, column iCol may be required if the expression -** (colUsed & ((sqlite3_uint64)1 << (iCol>=63 ? 63 : iCol))) evaluates to +** (colUsed & ((sqlite3_uint64)1 << (iCol>=63 ? 63 : iCol))) evaluates to ** non-zero. ** ** The [xBestIndex] method must fill aConstraintUsage[] with information @@ -5647,12 +7719,18 @@ struct sqlite3_module { ** the right-hand side of the corresponding aConstraint[] is evaluated ** and becomes the argvIndex-th entry in argv. ^(If aConstraintUsage[].omit ** is true, then the constraint is assumed to be fully handled by the -** virtual table and is not checked again by SQLite.)^ -** -** ^The idxNum and idxPtr values are recorded and passed into the +** virtual table and might not be checked again by the byte code.)^ ^(The +** aConstraintUsage[].omit flag is an optimization hint. When the omit flag +** is left in its default setting of false, the constraint will always be +** checked separately in byte code. If the omit flag is changed to true, then +** the constraint may or may not be checked in byte code. In other words, +** when the omit flag is true there is no guarantee that the constraint will +** not be checked again using byte code.)^ +** +** ^The idxNum and idxStr values are recorded and passed into the ** [xFilter] method. -** ^[sqlite3_free()] is used to free idxPtr if and only if -** needToFreeIdxPtr is true. +** ^[sqlite3_free()] is used to free idxStr if and only if +** needToFreeIdxStr is true. ** ** ^The orderByConsumed means that output from [xFilter]/[xNext] will occur in ** the correct order to satisfy the ORDER BY clause so that no separate @@ -5660,17 +7738,19 @@ struct sqlite3_module { ** ** ^The estimatedCost value is an estimate of the cost of a particular ** strategy. A cost of N indicates that the cost of the strategy is similar -** to a linear scan of an SQLite table with N rows. A cost of log(N) +** to a linear scan of an SQLite table with N rows. A cost of log(N) ** indicates that the expense of the operation is similar to that of a ** binary search on a unique indexed field of an SQLite table with N rows. ** ** ^The estimatedRows value is an estimate of the number of rows that ** will be returned by the strategy. ** -** The xBestIndex method may optionally populate the idxFlags field with a -** mask of SQLITE_INDEX_SCAN_* flags. Currently there is only one such flag - -** SQLITE_INDEX_SCAN_UNIQUE. If the xBestIndex method sets this flag, SQLite -** assumes that the strategy may visit at most one row. +** The xBestIndex method may optionally populate the idxFlags field with a +** mask of SQLITE_INDEX_SCAN_* flags. One such flag is +** [SQLITE_INDEX_SCAN_HEX], which if set causes the [EXPLAIN QUERY PLAN] +** output to show the idxNum as hex instead of as decimal. Another flag is +** SQLITE_INDEX_SCAN_UNIQUE, which if set indicates that the query plan will +** return at most one row. ** ** Additionally, if xBestIndex sets the SQLITE_INDEX_SCAN_UNIQUE flag, then ** SQLite also assumes that if a call to the xUpdate() method is made as @@ -5683,13 +7763,15 @@ struct sqlite3_module { ** the xUpdate method are automatically rolled back by SQLite. ** ** IMPORTANT: The estimatedRows field was added to the sqlite3_index_info -** structure for SQLite version 3.8.2. If a virtual table extension is -** used with an SQLite version earlier than 3.8.2, the results of attempting -** to read or write the estimatedRows field are undefined (but are likely -** to included crashing the application). The estimatedRows field should +** structure for SQLite [version 3.8.2] ([dateof:3.8.2]). +** If a virtual table extension is +** used with an SQLite version earlier than 3.8.2, the results of attempting +** to read or write the estimatedRows field are undefined (but are likely +** to include crashing the application). The estimatedRows field should ** therefore only be used if [sqlite3_libversion_number()] returns a ** value greater than or equal to 3008002. Similarly, the idxFlags field -** was added for version 3.9.0. It may therefore only be used if +** was added for [version 3.9.0] ([dateof:3.9.0]). +** It may therefore only be used if ** sqlite3_libversion_number() returns a value greater than or equal to ** 3009000. */ @@ -5727,26 +7809,70 @@ struct sqlite3_index_info { /* ** CAPI3REF: Virtual Table Scan Flags +** +** Virtual table implementations are allowed to set the +** [sqlite3_index_info].idxFlags field to some combination of +** these bits. */ -#define SQLITE_INDEX_SCAN_UNIQUE 1 /* Scan visits at most 1 row */ +#define SQLITE_INDEX_SCAN_UNIQUE 0x00000001 /* Scan visits at most 1 row */ +#define SQLITE_INDEX_SCAN_HEX 0x00000002 /* Display idxNum as hex */ + /* in EXPLAIN QUERY PLAN */ /* ** CAPI3REF: Virtual Table Constraint Operator Codes ** -** These macros defined the allowed values for the +** These macros define the allowed values for the ** [sqlite3_index_info].aConstraint[].op field. Each value represents -** an operator that is part of a constraint term in the wHERE clause of +** an operator that is part of a constraint term in the WHERE clause of ** a query that uses a [virtual table]. -*/ -#define SQLITE_INDEX_CONSTRAINT_EQ 2 -#define SQLITE_INDEX_CONSTRAINT_GT 4 -#define SQLITE_INDEX_CONSTRAINT_LE 8 -#define SQLITE_INDEX_CONSTRAINT_LT 16 -#define SQLITE_INDEX_CONSTRAINT_GE 32 -#define SQLITE_INDEX_CONSTRAINT_MATCH 64 -#define SQLITE_INDEX_CONSTRAINT_LIKE 65 -#define SQLITE_INDEX_CONSTRAINT_GLOB 66 -#define SQLITE_INDEX_CONSTRAINT_REGEXP 67 +** +** ^The left-hand operand of the operator is given by the corresponding +** aConstraint[].iColumn field. ^An iColumn of -1 indicates the left-hand +** operand is the rowid. +** The SQLITE_INDEX_CONSTRAINT_LIMIT and SQLITE_INDEX_CONSTRAINT_OFFSET +** operators have no left-hand operand, and so for those operators the +** corresponding aConstraint[].iColumn is meaningless and should not be +** used. +** +** All operator values from SQLITE_INDEX_CONSTRAINT_FUNCTION through +** value 255 are reserved to represent functions that are overloaded +** by the [xFindFunction|xFindFunction method] of the virtual table +** implementation. +** +** The right-hand operands for each constraint might be accessible using +** the [sqlite3_vtab_rhs_value()] interface. Usually the right-hand +** operand is only available if it appears as a single constant literal +** in the input SQL. If the right-hand operand is another column or an +** expression (even a constant expression) or a parameter, then the +** sqlite3_vtab_rhs_value() probably will not be able to extract it. +** ^The SQLITE_INDEX_CONSTRAINT_ISNULL and +** SQLITE_INDEX_CONSTRAINT_ISNOTNULL operators have no right-hand operand +** and hence calls to sqlite3_vtab_rhs_value() for those operators will +** always return SQLITE_NOTFOUND. +** +** The collating sequence to be used for comparison can be found using +** the [sqlite3_vtab_collation()] interface. For most real-world virtual +** tables, the collating sequence of constraints does not matter (for example +** because the constraints are numeric) and so the sqlite3_vtab_collation() +** interface is not commonly needed. +*/ +#define SQLITE_INDEX_CONSTRAINT_EQ 2 +#define SQLITE_INDEX_CONSTRAINT_GT 4 +#define SQLITE_INDEX_CONSTRAINT_LE 8 +#define SQLITE_INDEX_CONSTRAINT_LT 16 +#define SQLITE_INDEX_CONSTRAINT_GE 32 +#define SQLITE_INDEX_CONSTRAINT_MATCH 64 +#define SQLITE_INDEX_CONSTRAINT_LIKE 65 +#define SQLITE_INDEX_CONSTRAINT_GLOB 66 +#define SQLITE_INDEX_CONSTRAINT_REGEXP 67 +#define SQLITE_INDEX_CONSTRAINT_NE 68 +#define SQLITE_INDEX_CONSTRAINT_ISNOT 69 +#define SQLITE_INDEX_CONSTRAINT_ISNOTNULL 70 +#define SQLITE_INDEX_CONSTRAINT_ISNULL 71 +#define SQLITE_INDEX_CONSTRAINT_IS 72 +#define SQLITE_INDEX_CONSTRAINT_LIMIT 73 +#define SQLITE_INDEX_CONSTRAINT_OFFSET 74 +#define SQLITE_INDEX_CONSTRAINT_FUNCTION 150 /* ** CAPI3REF: Register A Virtual Table Implementation @@ -5758,12 +7884,12 @@ struct sqlite3_index_info { ** preexisting [virtual table] for the module. ** ** ^The module name is registered on the [database connection] specified -** by the first parameter. ^The name of the module is given by the +** by the first parameter. ^The name of the module is given by the ** second parameter. ^The third parameter is a pointer to ** the implementation of the [virtual table module]. ^The fourth ** parameter is an arbitrary client data pointer that is passed through ** into the [xCreate] and [xConnect] methods of the virtual table module -** when a new virtual table is be being created or reinitialized. +** when a new virtual table is being created or reinitialized. ** ** ^The sqlite3_create_module_v2() interface has a fifth parameter which ** is a pointer to a destructor for the pClientData. ^SQLite will @@ -5773,6 +7899,12 @@ struct sqlite3_index_info { ** ^The sqlite3_create_module() ** interface is equivalent to sqlite3_create_module_v2() with a NULL ** destructor. +** +** ^If the third parameter (the pointer to the sqlite3_module object) is +** NULL then no new module is created and any existing modules with the +** same name are dropped. +** +** See also: [sqlite3_drop_modules()] */ int sqlite3_create_module( sqlite3 *db, /* SQLite connection to register module with */ @@ -5788,6 +7920,23 @@ int sqlite3_create_module_v2( void(*xDestroy)(void*) /* Module destructor function */ ); +/* +** CAPI3REF: Remove Unnecessary Virtual Table Implementations +** METHOD: sqlite3 +** +** ^The sqlite3_drop_modules(D,L) interface removes all virtual +** table modules from database connection D except those named on list L. +** The L parameter must be either NULL or a pointer to an array of pointers +** to strings where the array is terminated by a single NULL pointer. +** ^If the L parameter is NULL, then all virtual table modules are removed. +** +** See also: [sqlite3_create_module()] +*/ +int sqlite3_drop_modules( + sqlite3 *db, /* Remove modules from this connection */ + const char **azKeep /* Except, do not remove the ones named here */ +); + /* ** CAPI3REF: Virtual Table Instance Object ** KEYWORDS: sqlite3_vtab @@ -5850,7 +7999,7 @@ int sqlite3_declare_vtab(sqlite3*, const char *zSQL); ** METHOD: sqlite3 ** ** ^(Virtual tables can provide alternative implementations of functions -** using the [xFindFunction] method of the [virtual table module]. +** using the [xFindFunction] method of the [virtual table module]. ** But global versions of those functions ** must exist in order to be overloaded.)^ ** @@ -5864,16 +8013,6 @@ int sqlite3_declare_vtab(sqlite3*, const char *zSQL); */ int sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg); -/* -** The interface to the virtual-table mechanism defined above (back up -** to a comment remarkably similar to this one) is currently considered -** to be experimental. The interface might change in incompatible ways. -** If this is a problem for you, do not use the interface at this time. -** -** When the virtual-table mechanism stabilizes, we will declare the -** interface fixed, support it indefinitely, and remove this comment. -*/ - /* ** CAPI3REF: A Handle To An Open BLOB ** KEYWORDS: {BLOB handle} {BLOB handles} @@ -5901,7 +8040,7 @@ typedef struct sqlite3_blob sqlite3_blob; ** SELECT zColumn FROM zDb.zTable WHERE [rowid] = iRow; ** </pre>)^ ** -** ^(Parameter zDb is not the filename that contains the database, but +** ^(Parameter zDb is not the filename that contains the database, but ** rather the symbolic name of the database. For attached databases, this is ** the name that appears after the AS keyword in the [ATTACH] statement. ** For the main database file, the database name is "main". For TEMP @@ -5914,29 +8053,35 @@ typedef struct sqlite3_blob sqlite3_blob; ** ^(On success, [SQLITE_OK] is returned and the new [BLOB handle] is stored ** in *ppBlob. Otherwise an [error code] is returned and, unless the error ** code is SQLITE_MISUSE, *ppBlob is set to NULL.)^ ^This means that, provided -** the API is not misused, it is always safe to call [sqlite3_blob_close()] -** on *ppBlob after this function it returns. +** the API is not misused, it is always safe to call [sqlite3_blob_close()] +** on *ppBlob after this function returns. ** ** This function fails with SQLITE_ERROR if any of the following are true: ** <ul> -** <li> ^(Database zDb does not exist)^, -** <li> ^(Table zTable does not exist within database zDb)^, -** <li> ^(Table zTable is a WITHOUT ROWID table)^, +** <li> ^(Database zDb does not exist)^, +** <li> ^(Table zTable does not exist within database zDb)^, +** <li> ^(Table zTable is a WITHOUT ROWID table)^, ** <li> ^(Column zColumn does not exist)^, ** <li> ^(Row iRow is not present in the table)^, ** <li> ^(The specified column of row iRow contains a value that is not ** a TEXT or BLOB value)^, -** <li> ^(Column zColumn is part of an index, PRIMARY KEY or UNIQUE +** <li> ^(Column zColumn is part of an index, PRIMARY KEY or UNIQUE ** constraint and the blob is being opened for read/write access)^, -** <li> ^([foreign key constraints | Foreign key constraints] are enabled, +** <li> ^([foreign key constraints | Foreign key constraints] are enabled, ** column zColumn is part of a [child key] definition and the blob is ** being opened for read/write access)^. ** </ul> ** -** ^Unless it returns SQLITE_MISUSE, this function sets the -** [database connection] error code and message accessible via -** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions. +** ^Unless it returns SQLITE_MISUSE, this function sets the +** [database connection] error code and message accessible via +** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions. ** +** A BLOB referenced by sqlite3_blob_open() may be read using the +** [sqlite3_blob_read()] interface and modified by using +** [sqlite3_blob_write()]. The [BLOB handle] can be moved to a +** different row of the same table using the [sqlite3_blob_reopen()] +** interface. However, the column, table, or database of a [BLOB handle] +** cannot be changed after the [BLOB handle] is opened. ** ** ^(If the row that a BLOB handle points to is modified by an ** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects @@ -5955,11 +8100,15 @@ typedef struct sqlite3_blob sqlite3_blob; ** blob. ** ** ^The [sqlite3_bind_zeroblob()] and [sqlite3_result_zeroblob()] interfaces -** and the built-in [zeroblob] SQL function may be used to create a +** and the built-in [zeroblob] SQL function may be used to create a ** zero-filled blob to read or write using the incremental-blob interface. ** ** To avoid a resource leak, every open [BLOB handle] should eventually ** be released by a call to [sqlite3_blob_close()]. +** +** See also: [sqlite3_blob_close()], +** [sqlite3_blob_reopen()], [sqlite3_blob_read()], +** [sqlite3_blob_bytes()], [sqlite3_blob_write()]. */ int sqlite3_blob_open( sqlite3*, @@ -5975,11 +8124,11 @@ int sqlite3_blob_open( ** CAPI3REF: Move a BLOB Handle to a New Row ** METHOD: sqlite3_blob ** -** ^This function is used to move an existing blob handle so that it points +** ^This function is used to move an existing [BLOB handle] so that it points ** to a different row of the same database table. ^The new row is identified ** by the rowid value passed as the second argument. Only the row can be ** changed. ^The database, table and column on which the blob handle is open -** remain the same. Moving an existing blob handle to a new row can be +** remain the same. Moving an existing [BLOB handle] to a new row is ** faster than closing the existing handle and opening a new one. ** ** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] - @@ -6001,7 +8150,7 @@ int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64); ** DESTRUCTOR: sqlite3_blob ** ** ^This function closes an open [BLOB handle]. ^(The BLOB handle is closed -** unconditionally. Even if this routine returns an error code, the +** unconditionally. Even if this routine returns an error code, the ** handle is still closed.)^ ** ** ^If the blob handle being closed was opened for read-write access, and if @@ -6011,10 +8160,10 @@ int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64); ** code is returned and the transaction rolled back. ** ** Calling this function with an argument that is not a NULL pointer or an -** open blob handle results in undefined behaviour. ^Calling this routine -** with a null pointer (such as would be returned by a failed call to +** open blob handle results in undefined behavior. ^Calling this routine +** with a null pointer (such as would be returned by a failed call to ** [sqlite3_blob_open()]) is a harmless no-op. ^Otherwise, if this function -** is passed a valid open blob handle, the values returned by the +** is passed a valid open blob handle, the values returned by the ** sqlite3_errcode() and sqlite3_errmsg() functions are set before returning. */ int sqlite3_blob_close(sqlite3_blob *); @@ -6023,9 +8172,9 @@ int sqlite3_blob_close(sqlite3_blob *); ** CAPI3REF: Return The Size Of An Open BLOB ** METHOD: sqlite3_blob ** -** ^Returns the size in bytes of the BLOB accessible via the +** ^Returns the size in bytes of the BLOB accessible via the ** successfully opened [BLOB handle] in its only argument. ^The -** incremental blob I/O routines can only read or overwriting existing +** incremental blob I/O routines can only read or overwrite existing ** blob content; they cannot change the size of a blob. ** ** This routine only works on a [BLOB handle] which has been created @@ -6074,9 +8223,9 @@ int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset); ** ** ^(On success, sqlite3_blob_write() returns SQLITE_OK. ** Otherwise, an [error code] or an [extended error code] is returned.)^ -** ^Unless SQLITE_MISUSE is returned, this function sets the -** [database connection] error code and message accessible via -** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions. +** ^Unless SQLITE_MISUSE is returned, this function sets the +** [database connection] error code and message accessible via +** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions. ** ** ^If the [BLOB handle] passed as the first argument was not opened for ** writing (the flags parameter to [sqlite3_blob_open()] was zero), @@ -6085,9 +8234,9 @@ int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset); ** This function may only modify the contents of the BLOB; it is ** not possible to increase the size of a BLOB using this API. ** ^If offset iOffset is less than N bytes from the end of the BLOB, -** [SQLITE_ERROR] is returned and no data is written. The size of the -** BLOB (and hence the maximum value of N+iOffset) can be determined -** using the [sqlite3_blob_bytes()] interface. ^If N or iOffset are less +** [SQLITE_ERROR] is returned and no data is written. The size of the +** BLOB (and hence the maximum value of N+iOffset) can be determined +** using the [sqlite3_blob_bytes()] interface. ^If N or iOffset are less ** than zero [SQLITE_ERROR] is returned and no data is written. ** ** ^An attempt to write to an expired [BLOB handle] fails with an @@ -6175,13 +8324,13 @@ int sqlite3_vfs_unregister(sqlite3_vfs*); ** ^The sqlite3_mutex_alloc() routine allocates a new ** mutex and returns a pointer to it. ^The sqlite3_mutex_alloc() ** routine returns NULL if it is unable to allocate the requested -** mutex. The argument to sqlite3_mutex_alloc() must one of these +** mutex. The argument to sqlite3_mutex_alloc() must be one of these ** integer constants: ** ** <ul> ** <li> SQLITE_MUTEX_FAST ** <li> SQLITE_MUTEX_RECURSIVE -** <li> SQLITE_MUTEX_STATIC_MASTER +** <li> SQLITE_MUTEX_STATIC_MAIN ** <li> SQLITE_MUTEX_STATIC_MEM ** <li> SQLITE_MUTEX_STATIC_OPEN ** <li> SQLITE_MUTEX_STATIC_PRNG @@ -6238,18 +8387,20 @@ int sqlite3_vfs_unregister(sqlite3_vfs*); ** ** ^(Some systems (for example, Windows 95) do not support the operation ** implemented by sqlite3_mutex_try(). On those systems, sqlite3_mutex_try() -** will always return SQLITE_BUSY. The SQLite core only ever uses -** sqlite3_mutex_try() as an optimization so this is acceptable -** behavior.)^ +** will always return SQLITE_BUSY. In most cases the SQLite core only uses +** sqlite3_mutex_try() as an optimization, so this is acceptable +** behavior. The exceptions are unix builds that set the +** SQLITE_ENABLE_SETLK_TIMEOUT build option. In that case a working +** sqlite3_mutex_try() is required.)^ ** ** ^The sqlite3_mutex_leave() routine exits a mutex that was ** previously entered by the same thread. The behavior ** is undefined if the mutex is not currently entered by the ** calling thread or is not currently allocated. ** -** ^If the argument to sqlite3_mutex_enter(), sqlite3_mutex_try(), or -** sqlite3_mutex_leave() is a NULL pointer, then all three routines -** behave as no-ops. +** ^If the argument to sqlite3_mutex_enter(), sqlite3_mutex_try(), +** sqlite3_mutex_leave(), or sqlite3_mutex_free() is a NULL pointer, +** then any of the four routines behaves as a no-op. ** ** See also: [sqlite3_mutex_held()] and [sqlite3_mutex_notheld()]. */ @@ -6304,7 +8455,7 @@ void sqlite3_mutex_leave(sqlite3_mutex*); ** The only difference is that the public sqlite3_XXX functions enumerated ** above silently ignore any invocations that pass a NULL pointer instead ** of a valid mutex handle. The implementations of the methods defined -** by this structure are not required to handle this case, the results +** by this structure are not required to handle this case. The results ** of passing a NULL pointer instead of a valid mutex handle are undefined ** (i.e. it is acceptable to provide an implementation that segfaults if ** it is passed a NULL pointer). @@ -6383,11 +8534,11 @@ int sqlite3_mutex_notheld(sqlite3_mutex*); */ #define SQLITE_MUTEX_FAST 0 #define SQLITE_MUTEX_RECURSIVE 1 -#define SQLITE_MUTEX_STATIC_MASTER 2 +#define SQLITE_MUTEX_STATIC_MAIN 2 #define SQLITE_MUTEX_STATIC_MEM 3 /* sqlite3_malloc() */ #define SQLITE_MUTEX_STATIC_MEM2 4 /* NOT USED */ #define SQLITE_MUTEX_STATIC_OPEN 4 /* sqlite3BtreeOpen() */ -#define SQLITE_MUTEX_STATIC_PRNG 5 /* sqlite3_random() */ +#define SQLITE_MUTEX_STATIC_PRNG 5 /* sqlite3_randomness() */ #define SQLITE_MUTEX_STATIC_LRU 6 /* lru page list */ #define SQLITE_MUTEX_STATIC_LRU2 7 /* NOT USED */ #define SQLITE_MUTEX_STATIC_PMEM 7 /* sqlite3PageMalloc() */ @@ -6398,11 +8549,15 @@ int sqlite3_mutex_notheld(sqlite3_mutex*); #define SQLITE_MUTEX_STATIC_VFS2 12 /* For use by extension VFS */ #define SQLITE_MUTEX_STATIC_VFS3 13 /* For use by application VFS */ +/* Legacy compatibility: */ +#define SQLITE_MUTEX_STATIC_MASTER 2 + + /* ** CAPI3REF: Retrieve the mutex for a database connection ** METHOD: sqlite3 ** -** ^This interface returns a pointer the [sqlite3_mutex] object that +** ^This interface returns a pointer to the [sqlite3_mutex] object that ** serializes access to the [database connection] given in the argument ** when the [threading mode] is Serialized. ** ^If the [threading mode] is Single-thread or Multi-thread then this @@ -6413,6 +8568,7 @@ sqlite3_mutex *sqlite3_db_mutex(sqlite3*); /* ** CAPI3REF: Low-Level Control Of Database Files ** METHOD: sqlite3 +** KEYWORDS: {file control} ** ** ^The [sqlite3_file_control()] interface makes a direct call to the ** xFileControl method for the [sqlite3_io_methods] object associated @@ -6427,11 +8583,18 @@ sqlite3_mutex *sqlite3_db_mutex(sqlite3*); ** the xFileControl method. ^The return value of the xFileControl ** method becomes the return value of this routine. ** -** ^The SQLITE_FCNTL_FILE_POINTER value for the op parameter causes +** A few opcodes for [sqlite3_file_control()] are handled directly +** by the SQLite core and never invoke the +** sqlite3_io_methods.xFileControl method. +** ^The [SQLITE_FCNTL_FILE_POINTER] value for the op parameter causes ** a pointer to the underlying [sqlite3_file] object to be written into -** the space pointed to by the 4th parameter. ^The SQLITE_FCNTL_FILE_POINTER -** case is a short-circuit path which does not actually invoke the -** underlying sqlite3_io_methods.xFileControl method. +** the space pointed to by the 4th parameter. The +** [SQLITE_FCNTL_JOURNAL_POINTER] works similarly except that it returns +** the [sqlite3_file] object associated with the journal file instead of +** the main database. The [SQLITE_FCNTL_VFS_POINTER] opcode returns +** a pointer to the underlying [sqlite3_vfs] object for the file. +** The [SQLITE_FCNTL_DATA_VERSION] returns the data version counter +** from the pager. ** ** ^If the second parameter (zDbName) does not match the name of any ** open database file, then SQLITE_ERROR is returned. ^This error @@ -6441,7 +8604,7 @@ sqlite3_mutex *sqlite3_db_mutex(sqlite3*); ** an incorrect zDbName and an SQLITE_ERROR return from the underlying ** xFileControl method. ** -** See also: [SQLITE_FCNTL_LOCKSTATE] +** See also: [file control opcodes] */ int sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*); @@ -6478,26 +8641,223 @@ int sqlite3_test_control(int op, ...); #define SQLITE_TESTCTRL_FIRST 5 #define SQLITE_TESTCTRL_PRNG_SAVE 5 #define SQLITE_TESTCTRL_PRNG_RESTORE 6 -#define SQLITE_TESTCTRL_PRNG_RESET 7 +#define SQLITE_TESTCTRL_PRNG_RESET 7 /* NOT USED */ +#define SQLITE_TESTCTRL_FK_NO_ACTION 7 #define SQLITE_TESTCTRL_BITVEC_TEST 8 #define SQLITE_TESTCTRL_FAULT_INSTALL 9 #define SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS 10 #define SQLITE_TESTCTRL_PENDING_BYTE 11 #define SQLITE_TESTCTRL_ASSERT 12 #define SQLITE_TESTCTRL_ALWAYS 13 -#define SQLITE_TESTCTRL_RESERVE 14 +#define SQLITE_TESTCTRL_RESERVE 14 /* NOT USED */ +#define SQLITE_TESTCTRL_JSON_SELFCHECK 14 #define SQLITE_TESTCTRL_OPTIMIZATIONS 15 -#define SQLITE_TESTCTRL_ISKEYWORD 16 -#define SQLITE_TESTCTRL_SCRATCHMALLOC 17 +#define SQLITE_TESTCTRL_ISKEYWORD 16 /* NOT USED */ +#define SQLITE_TESTCTRL_GETOPT 16 +#define SQLITE_TESTCTRL_SCRATCHMALLOC 17 /* NOT USED */ +#define SQLITE_TESTCTRL_INTERNAL_FUNCTIONS 17 #define SQLITE_TESTCTRL_LOCALTIME_FAULT 18 #define SQLITE_TESTCTRL_EXPLAIN_STMT 19 /* NOT USED */ +#define SQLITE_TESTCTRL_ONCE_RESET_THRESHOLD 19 #define SQLITE_TESTCTRL_NEVER_CORRUPT 20 #define SQLITE_TESTCTRL_VDBE_COVERAGE 21 #define SQLITE_TESTCTRL_BYTEORDER 22 #define SQLITE_TESTCTRL_ISINIT 23 #define SQLITE_TESTCTRL_SORTER_MMAP 24 #define SQLITE_TESTCTRL_IMPOSTER 25 -#define SQLITE_TESTCTRL_LAST 25 +#define SQLITE_TESTCTRL_PARSER_COVERAGE 26 +#define SQLITE_TESTCTRL_RESULT_INTREAL 27 +#define SQLITE_TESTCTRL_PRNG_SEED 28 +#define SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS 29 +#define SQLITE_TESTCTRL_SEEK_COUNT 30 +#define SQLITE_TESTCTRL_TRACEFLAGS 31 +#define SQLITE_TESTCTRL_TUNE 32 +#define SQLITE_TESTCTRL_LOGEST 33 +#define SQLITE_TESTCTRL_USELONGDOUBLE 34 /* NOT USED */ +#define SQLITE_TESTCTRL_LAST 34 /* Largest TESTCTRL */ + +/* +** CAPI3REF: SQL Keyword Checking +** +** These routines provide access to the set of SQL language keywords +** recognized by SQLite. Applications can use these routines to determine +** whether or not a specific identifier needs to be escaped (for example, +** by enclosing in double-quotes) so as not to confuse the parser. +** +** The sqlite3_keyword_count() interface returns the number of distinct +** keywords understood by SQLite. +** +** The sqlite3_keyword_name(N,Z,L) interface finds the 0-based N-th keyword and +** makes *Z point to that keyword expressed as UTF8 and writes the number +** of bytes in the keyword into *L. The string that *Z points to is not +** zero-terminated. The sqlite3_keyword_name(N,Z,L) routine returns +** SQLITE_OK if N is within bounds and SQLITE_ERROR if not. If either Z +** or L are NULL or invalid pointers then calls to +** sqlite3_keyword_name(N,Z,L) result in undefined behavior. +** +** The sqlite3_keyword_check(Z,L) interface checks to see whether or not +** the L-byte UTF8 identifier that Z points to is a keyword, returning non-zero +** if it is and zero if not. +** +** The parser used by SQLite is forgiving. It is often possible to use +** a keyword as an identifier as long as such use does not result in a +** parsing ambiguity. For example, the statement +** "CREATE TABLE BEGIN(REPLACE,PRAGMA,END);" is accepted by SQLite, and +** creates a new table named "BEGIN" with three columns named +** "REPLACE", "PRAGMA", and "END". Nevertheless, best practice is to avoid +** using keywords as identifiers. Common techniques used to avoid keyword +** name collisions include: +** <ul> +** <li> Put all identifier names inside double-quotes. This is the official +** SQL way to escape identifier names. +** <li> Put identifier names inside &#91;...&#93;. This is not standard SQL, +** but it is what SQL Server does and so lots of programmers use this +** technique. +** <li> Begin every identifier with the letter "Z" as no SQL keywords start +** with "Z". +** <li> Include a digit somewhere in every identifier name. +** </ul> +** +** Note that the number of keywords understood by SQLite can depend on +** compile-time options. For example, "VACUUM" is not a keyword if +** SQLite is compiled with the [-DSQLITE_OMIT_VACUUM] option. Also, +** new keywords may be added to future releases of SQLite. +*/ +int sqlite3_keyword_count(void); +int sqlite3_keyword_name(int,const char**,int*); +int sqlite3_keyword_check(const char*,int); + +/* +** CAPI3REF: Dynamic String Object +** KEYWORDS: {dynamic string} +** +** An instance of the sqlite3_str object contains a dynamically-sized +** string under construction. +** +** The lifecycle of an sqlite3_str object is as follows: +** <ol> +** <li> ^The sqlite3_str object is created using [sqlite3_str_new()]. +** <li> ^Text is appended to the sqlite3_str object using various +** methods, such as [sqlite3_str_appendf()]. +** <li> ^The sqlite3_str object is destroyed and the string it created +** is returned using the [sqlite3_str_finish()] interface. +** </ol> +*/ +typedef struct sqlite3_str sqlite3_str; + +/* +** CAPI3REF: Create A New Dynamic String Object +** CONSTRUCTOR: sqlite3_str +** +** ^The [sqlite3_str_new(D)] interface allocates and initializes +** a new [sqlite3_str] object. To avoid memory leaks, the object returned by +** [sqlite3_str_new()] must be freed by a subsequent call to +** [sqlite3_str_finish(X)]. +** +** ^The [sqlite3_str_new(D)] interface always returns a pointer to a +** valid [sqlite3_str] object, though in the event of an out-of-memory +** error the returned object might be a special singleton that will +** silently reject new text, always return SQLITE_NOMEM from +** [sqlite3_str_errcode()], always return 0 for +** [sqlite3_str_length()], and always return NULL from +** [sqlite3_str_finish(X)]. It is always safe to use the value +** returned by [sqlite3_str_new(D)] as the sqlite3_str parameter +** to any of the other [sqlite3_str] methods. +** +** The D parameter to [sqlite3_str_new(D)] may be NULL. If the +** D parameter in [sqlite3_str_new(D)] is not NULL, then the maximum +** length of the string contained in the [sqlite3_str] object will be +** the value set for [sqlite3_limit](D,[SQLITE_LIMIT_LENGTH]) instead +** of [SQLITE_MAX_LENGTH]. +*/ +sqlite3_str *sqlite3_str_new(sqlite3*); + +/* +** CAPI3REF: Finalize A Dynamic String +** DESTRUCTOR: sqlite3_str +** +** ^The [sqlite3_str_finish(X)] interface destroys the sqlite3_str object X +** and returns a pointer to a memory buffer obtained from [sqlite3_malloc64()] +** that contains the constructed string. The calling application should +** pass the returned value to [sqlite3_free()] to avoid a memory leak. +** ^The [sqlite3_str_finish(X)] interface may return a NULL pointer if any +** errors were encountered during construction of the string. ^The +** [sqlite3_str_finish(X)] interface will also return a NULL pointer if the +** string in [sqlite3_str] object X is zero bytes long. +*/ +char *sqlite3_str_finish(sqlite3_str*); + +/* +** CAPI3REF: Add Content To A Dynamic String +** METHOD: sqlite3_str +** +** These interfaces add content to an sqlite3_str object previously obtained +** from [sqlite3_str_new()]. +** +** ^The [sqlite3_str_appendf(X,F,...)] and +** [sqlite3_str_vappendf(X,F,V)] interfaces uses the [built-in printf] +** functionality of SQLite to append formatted text onto the end of +** [sqlite3_str] object X. +** +** ^The [sqlite3_str_append(X,S,N)] method appends exactly N bytes from string S +** onto the end of the [sqlite3_str] object X. N must be non-negative. +** S must contain at least N non-zero bytes of content. To append a +** zero-terminated string in its entirety, use the [sqlite3_str_appendall()] +** method instead. +** +** ^The [sqlite3_str_appendall(X,S)] method appends the complete content of +** zero-terminated string S onto the end of [sqlite3_str] object X. +** +** ^The [sqlite3_str_appendchar(X,N,C)] method appends N copies of the +** single-byte character C onto the end of [sqlite3_str] object X. +** ^This method can be used, for example, to add whitespace indentation. +** +** ^The [sqlite3_str_reset(X)] method resets the string under construction +** inside [sqlite3_str] object X back to zero bytes in length. +** +** These methods do not return a result code. ^If an error occurs, that fact +** is recorded in the [sqlite3_str] object and can be recovered by a +** subsequent call to [sqlite3_str_errcode(X)]. +*/ +void sqlite3_str_appendf(sqlite3_str*, const char *zFormat, ...); +void sqlite3_str_vappendf(sqlite3_str*, const char *zFormat, va_list); +void sqlite3_str_append(sqlite3_str*, const char *zIn, int N); +void sqlite3_str_appendall(sqlite3_str*, const char *zIn); +void sqlite3_str_appendchar(sqlite3_str*, int N, char C); +void sqlite3_str_reset(sqlite3_str*); + +/* +** CAPI3REF: Status Of A Dynamic String +** METHOD: sqlite3_str +** +** These interfaces return the current status of an [sqlite3_str] object. +** +** ^If any prior errors have occurred while constructing the dynamic string +** in sqlite3_str X, then the [sqlite3_str_errcode(X)] method will return +** an appropriate error code. ^The [sqlite3_str_errcode(X)] method returns +** [SQLITE_NOMEM] following any out-of-memory error, or +** [SQLITE_TOOBIG] if the size of the dynamic string exceeds +** [SQLITE_MAX_LENGTH], or [SQLITE_OK] if there have been no errors. +** +** ^The [sqlite3_str_length(X)] method returns the current length, in bytes, +** of the dynamic string under construction in [sqlite3_str] object X. +** ^The length returned by [sqlite3_str_length(X)] does not include the +** zero-termination byte. +** +** ^The [sqlite3_str_value(X)] method returns a pointer to the current +** content of the dynamic string under construction in X. The value +** returned by [sqlite3_str_value(X)] is managed by the sqlite3_str object X +** and might be freed or altered by any subsequent method on the same +** [sqlite3_str] object. Applications must not use the pointer returned by +** [sqlite3_str_value(X)] after any subsequent method call on the same +** object. ^Applications may change the content of the string returned +** by [sqlite3_str_value(X)] as long as they do not write into any bytes +** outside the range of 0 to [sqlite3_str_length(X)] and do not read or +** write any byte after any subsequent sqlite3_str method call. +*/ +int sqlite3_str_errcode(sqlite3_str*); +int sqlite3_str_length(sqlite3_str*); +char *sqlite3_str_value(sqlite3_str*); /* ** CAPI3REF: SQLite Runtime Status @@ -6546,8 +8906,7 @@ int sqlite3_status64( ** <dd>This parameter is the current amount of memory checked out ** using [sqlite3_malloc()], either directly or indirectly. The ** figure includes calls made to [sqlite3_malloc()] by the application -** and internal memory usage by the SQLite library. Scratch memory -** controlled by [SQLITE_CONFIG_SCRATCH] and auxiliary page-cache +** and internal memory usage by the SQLite library. Auxiliary page-cache ** memory controlled by [SQLITE_CONFIG_PAGECACHE] is not included in ** this parameter. The amount returned is the sum of the allocation ** sizes as reported by the xSize method in [sqlite3_mem_methods].</dd>)^ @@ -6556,7 +8915,7 @@ int sqlite3_status64( ** <dd>This parameter records the largest memory allocation request ** handed to [sqlite3_malloc()] or [sqlite3_realloc()] (or their ** internal equivalents). Only the value returned in the -** *pHighwater parameter to [sqlite3_status()] is of interest. +** *pHighwater parameter to [sqlite3_status()] is of interest. ** The value written into the *pCurrent parameter is undefined.</dd>)^ ** ** [[SQLITE_STATUS_MALLOC_COUNT]] ^(<dt>SQLITE_STATUS_MALLOC_COUNT</dt> @@ -6565,52 +8924,37 @@ int sqlite3_status64( ** ** [[SQLITE_STATUS_PAGECACHE_USED]] ^(<dt>SQLITE_STATUS_PAGECACHE_USED</dt> ** <dd>This parameter returns the number of pages used out of the -** [pagecache memory allocator] that was configured using +** [pagecache memory allocator] that was configured using ** [SQLITE_CONFIG_PAGECACHE]. The ** value returned is in pages, not in bytes.</dd>)^ ** -** [[SQLITE_STATUS_PAGECACHE_OVERFLOW]] +** [[SQLITE_STATUS_PAGECACHE_OVERFLOW]] ** ^(<dt>SQLITE_STATUS_PAGECACHE_OVERFLOW</dt> ** <dd>This parameter returns the number of bytes of page cache ** allocation which could not be satisfied by the [SQLITE_CONFIG_PAGECACHE] ** buffer and where forced to overflow to [sqlite3_malloc()]. The ** returned value includes allocations that overflowed because they -** where too large (they were larger than the "sz" parameter to +** were too large (they were larger than the "sz" parameter to ** [SQLITE_CONFIG_PAGECACHE]) and allocations that overflowed because ** no space was left in the page cache.</dd>)^ ** ** [[SQLITE_STATUS_PAGECACHE_SIZE]] ^(<dt>SQLITE_STATUS_PAGECACHE_SIZE</dt> ** <dd>This parameter records the largest memory allocation request -** handed to [pagecache memory allocator]. Only the value returned in the -** *pHighwater parameter to [sqlite3_status()] is of interest. +** handed to the [pagecache memory allocator]. Only the value returned in the +** *pHighwater parameter to [sqlite3_status()] is of interest. ** The value written into the *pCurrent parameter is undefined.</dd>)^ ** -** [[SQLITE_STATUS_SCRATCH_USED]] ^(<dt>SQLITE_STATUS_SCRATCH_USED</dt> -** <dd>This parameter returns the number of allocations used out of the -** [scratch memory allocator] configured using -** [SQLITE_CONFIG_SCRATCH]. The value returned is in allocations, not -** in bytes. Since a single thread may only have one scratch allocation -** outstanding at time, this parameter also reports the number of threads -** using scratch memory at the same time.</dd>)^ +** [[SQLITE_STATUS_SCRATCH_USED]] <dt>SQLITE_STATUS_SCRATCH_USED</dt> +** <dd>No longer used.</dd> ** ** [[SQLITE_STATUS_SCRATCH_OVERFLOW]] ^(<dt>SQLITE_STATUS_SCRATCH_OVERFLOW</dt> -** <dd>This parameter returns the number of bytes of scratch memory -** allocation which could not be satisfied by the [SQLITE_CONFIG_SCRATCH] -** buffer and where forced to overflow to [sqlite3_malloc()]. The values -** returned include overflows because the requested allocation was too -** larger (that is, because the requested allocation was larger than the -** "sz" parameter to [SQLITE_CONFIG_SCRATCH]) and because no scratch buffer -** slots were available. -** </dd>)^ +** <dd>No longer used.</dd> ** -** [[SQLITE_STATUS_SCRATCH_SIZE]] ^(<dt>SQLITE_STATUS_SCRATCH_SIZE</dt> -** <dd>This parameter records the largest memory allocation request -** handed to [scratch memory allocator]. Only the value returned in the -** *pHighwater parameter to [sqlite3_status()] is of interest. -** The value written into the *pCurrent parameter is undefined.</dd>)^ +** [[SQLITE_STATUS_SCRATCH_SIZE]] <dt>SQLITE_STATUS_SCRATCH_SIZE</dt> +** <dd>No longer used.</dd> ** ** [[SQLITE_STATUS_PARSER_STACK]] ^(<dt>SQLITE_STATUS_PARSER_STACK</dt> -** <dd>The *pHighwater parameter records the deepest parser stack. +** <dd>The *pHighwater parameter records the deepest parser stack. ** The *pCurrent value is undefined. The *pHighwater value is only ** meaningful if SQLite is compiled with [YYTRACKMAXSTACKDEPTH].</dd>)^ ** </dl> @@ -6620,24 +8964,24 @@ int sqlite3_status64( #define SQLITE_STATUS_MEMORY_USED 0 #define SQLITE_STATUS_PAGECACHE_USED 1 #define SQLITE_STATUS_PAGECACHE_OVERFLOW 2 -#define SQLITE_STATUS_SCRATCH_USED 3 -#define SQLITE_STATUS_SCRATCH_OVERFLOW 4 +#define SQLITE_STATUS_SCRATCH_USED 3 /* NOT USED */ +#define SQLITE_STATUS_SCRATCH_OVERFLOW 4 /* NOT USED */ #define SQLITE_STATUS_MALLOC_SIZE 5 #define SQLITE_STATUS_PARSER_STACK 6 #define SQLITE_STATUS_PAGECACHE_SIZE 7 -#define SQLITE_STATUS_SCRATCH_SIZE 8 +#define SQLITE_STATUS_SCRATCH_SIZE 8 /* NOT USED */ #define SQLITE_STATUS_MALLOC_COUNT 9 /* ** CAPI3REF: Database Connection Status ** METHOD: sqlite3 ** -** ^This interface is used to retrieve runtime status information +** ^This interface is used to retrieve runtime status information ** about a single [database connection]. ^The first argument is the ** database connection object to be interrogated. ^The second argument ** is an integer constant, taken from the set of ** [SQLITE_DBSTATUS options], that -** determines the parameter to interrogate. The set of +** determines the parameter to interrogate. The set of ** [SQLITE_DBSTATUS options] is likely ** to grow in future releases of SQLite. ** @@ -6649,9 +8993,18 @@ int sqlite3_status64( ** ^The sqlite3_db_status() routine returns SQLITE_OK on success and a ** non-zero [error code] on failure. ** +** ^The sqlite3_db_status64(D,O,C,H,R) routine works exactly the same +** way as sqlite3_db_status(D,O,C,H,R) routine except that the C and H +** parameters are pointer to 64-bit integers (type: sqlite3_int64) instead +** of pointers to 32-bit integers, which allows larger status values +** to be returned. If a status value exceeds 2,147,483,647 then +** sqlite3_db_status() will truncate the value whereas sqlite3_db_status64() +** will return the full value. +** ** See also: [sqlite3_status()] and [sqlite3_stmt_status()]. */ int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg); +int sqlite3_db_status64(sqlite3*,int,sqlite3_int64*,sqlite3_int64*,int); /* ** CAPI3REF: Status Parameters for database connections @@ -6672,39 +9025,53 @@ int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg); ** checked out.</dd>)^ ** ** [[SQLITE_DBSTATUS_LOOKASIDE_HIT]] ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_HIT</dt> -** <dd>This parameter returns the number malloc attempts that were +** <dd>This parameter returns the number of malloc attempts that were ** satisfied using lookaside memory. Only the high-water value is meaningful; -** the current value is always zero.)^ +** the current value is always zero.</dd>)^ ** ** [[SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE]] ** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE</dt> -** <dd>This parameter returns the number malloc attempts that might have +** <dd>This parameter returns the number of malloc attempts that might have ** been satisfied using lookaside memory but failed due to the amount of ** memory requested being larger than the lookaside slot size. ** Only the high-water value is meaningful; -** the current value is always zero.)^ +** the current value is always zero.</dd>)^ ** ** [[SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL]] ** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL</dt> -** <dd>This parameter returns the number malloc attempts that might have +** <dd>This parameter returns the number of malloc attempts that might have ** been satisfied using lookaside memory but failed due to all lookaside ** memory already being in use. ** Only the high-water value is meaningful; -** the current value is always zero.)^ +** the current value is always zero.</dd>)^ ** ** [[SQLITE_DBSTATUS_CACHE_USED]] ^(<dt>SQLITE_DBSTATUS_CACHE_USED</dt> ** <dd>This parameter returns the approximate number of bytes of heap ** memory used by all pager caches associated with the database connection.)^ ** ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_USED is always 0. +** </dd> +** +** [[SQLITE_DBSTATUS_CACHE_USED_SHARED]] +** ^(<dt>SQLITE_DBSTATUS_CACHE_USED_SHARED</dt> +** <dd>This parameter is similar to DBSTATUS_CACHE_USED, except that if a +** pager cache is shared between two or more connections the bytes of heap +** memory used by that pager cache is divided evenly between the attached +** connections.)^ In other words, if none of the pager caches associated +** with the database connection are shared, this request returns the same +** value as DBSTATUS_CACHE_USED. Or, if one or more of the pager caches are +** shared, the value returned by this call will be smaller than that returned +** by DBSTATUS_CACHE_USED. ^The highwater mark associated with +** SQLITE_DBSTATUS_CACHE_USED_SHARED is always 0.</dd> ** ** [[SQLITE_DBSTATUS_SCHEMA_USED]] ^(<dt>SQLITE_DBSTATUS_SCHEMA_USED</dt> ** <dd>This parameter returns the approximate number of bytes of heap ** memory used to store the schema for all databases associated -** with the connection - main, temp, and any [ATTACH]-ed databases.)^ +** with the connection - main, temp, and any [ATTACH]-ed databases.)^ ** ^The full amount of memory used by the schemas is reported, even if the ** schema memory is shared with other database connections due to ** [shared cache mode] being enabled. ** ^The highwater mark associated with SQLITE_DBSTATUS_SCHEMA_USED is always 0. +** </dd> ** ** [[SQLITE_DBSTATUS_STMT_USED]] ^(<dt>SQLITE_DBSTATUS_STMT_USED</dt> ** <dd>This parameter returns the approximate number of bytes of heap @@ -6715,13 +9082,13 @@ int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg); ** ** [[SQLITE_DBSTATUS_CACHE_HIT]] ^(<dt>SQLITE_DBSTATUS_CACHE_HIT</dt> ** <dd>This parameter returns the number of pager cache hits that have -** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_HIT +** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_HIT ** is always 0. ** </dd> ** ** [[SQLITE_DBSTATUS_CACHE_MISS]] ^(<dt>SQLITE_DBSTATUS_CACHE_MISS</dt> ** <dd>This parameter returns the number of pager cache misses that have -** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_MISS +** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_MISS ** is always 0. ** </dd> ** @@ -6734,12 +9101,37 @@ int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg); ** If an IO or other error occurs while writing a page to disk, the effect ** on subsequent SQLITE_DBSTATUS_CACHE_WRITE requests is undefined.)^ ^The ** highwater mark associated with SQLITE_DBSTATUS_CACHE_WRITE is always 0. +** <p> +** ^(There is overlap between the quantities measured by this parameter +** (SQLITE_DBSTATUS_CACHE_WRITE) and SQLITE_DBSTATUS_TEMPBUF_SPILL. +** Resetting one will reduce the other.)^ +** </dd> +** +** [[SQLITE_DBSTATUS_CACHE_SPILL]] ^(<dt>SQLITE_DBSTATUS_CACHE_SPILL</dt> +** <dd>This parameter returns the number of dirty cache entries that have +** been written to disk in the middle of a transaction due to the page +** cache overflowing. Transactions are more efficient if they are written +** to disk all at once. When pages spill mid-transaction, that introduces +** additional overhead. This parameter can be used to help identify +** inefficiencies that can be resolved by increasing the cache size. ** </dd> ** ** [[SQLITE_DBSTATUS_DEFERRED_FKS]] ^(<dt>SQLITE_DBSTATUS_DEFERRED_FKS</dt> ** <dd>This parameter returns zero for the current value if and only if ** all foreign key constraints (deferred or immediate) have been ** resolved.)^ ^The highwater mark is always 0. +** +** [[SQLITE_DBSTATUS_TEMPBUF_SPILL] ^(<dt>SQLITE_DBSTATUS_TEMPBUF_SPILL</dt> +** <dd>^(This parameter returns the number of bytes written to temporary +** files on disk that could have been kept in memory had sufficient memory +** been available. This value includes writes to intermediate tables that +** are part of complex queries, external sorts that spill to disk, and +** writes to TEMP tables.)^ +** ^The highwater mark is always 0. +** <p> +** ^(There is overlap between the quantities measured by this parameter +** (SQLITE_DBSTATUS_TEMPBUF_SPILL) and SQLITE_DBSTATUS_CACHE_WRITE. +** Resetting one will reduce the other.)^ ** </dd> ** </dl> */ @@ -6754,7 +9146,10 @@ int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg); #define SQLITE_DBSTATUS_CACHE_MISS 8 #define SQLITE_DBSTATUS_CACHE_WRITE 9 #define SQLITE_DBSTATUS_DEFERRED_FKS 10 -#define SQLITE_DBSTATUS_MAX 10 /* Largest defined DBSTATUS */ +#define SQLITE_DBSTATUS_CACHE_USED_SHARED 11 +#define SQLITE_DBSTATUS_CACHE_SPILL 12 +#define SQLITE_DBSTATUS_TEMPBUF_SPILL 13 +#define SQLITE_DBSTATUS_MAX 13 /* Largest defined DBSTATUS */ /* @@ -6768,7 +9163,7 @@ int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg); ** statements. For example, if the number of table steps greatly exceeds ** the number of table searches or result rows, that would tend to indicate ** that the prepared statement is using a full table scan rather than -** an index. +** an index. ** ** ^(This interface is used to retrieve and reset counter values from ** a [prepared statement]. The first argument is the prepared statement @@ -6795,28 +9190,56 @@ int sqlite3_stmt_status(sqlite3_stmt*, int op,int resetFlg); ** [[SQLITE_STMTSTATUS_FULLSCAN_STEP]] <dt>SQLITE_STMTSTATUS_FULLSCAN_STEP</dt> ** <dd>^This is the number of times that SQLite has stepped forward in ** a table as part of a full table scan. Large numbers for this counter -** may indicate opportunities for performance improvement through +** may indicate opportunities for performance improvement through ** careful use of indices.</dd> ** ** [[SQLITE_STMTSTATUS_SORT]] <dt>SQLITE_STMTSTATUS_SORT</dt> ** <dd>^This is the number of sort operations that have occurred. ** A non-zero value in this counter may indicate an opportunity to -** improvement performance through careful use of indices.</dd> +** improve performance through careful use of indices.</dd> ** ** [[SQLITE_STMTSTATUS_AUTOINDEX]] <dt>SQLITE_STMTSTATUS_AUTOINDEX</dt> ** <dd>^This is the number of rows inserted into transient indices that ** were created automatically in order to help joins run faster. ** A non-zero value in this counter may indicate an opportunity to -** improvement performance by adding permanent indices that do not +** improve performance by adding permanent indices that do not ** need to be reinitialized each time the statement is run.</dd> ** ** [[SQLITE_STMTSTATUS_VM_STEP]] <dt>SQLITE_STMTSTATUS_VM_STEP</dt> ** <dd>^This is the number of virtual machine operations executed ** by the prepared statement if that number is less than or equal -** to 2147483647. The number of virtual machine operations can be +** to 2147483647. The number of virtual machine operations can be ** used as a proxy for the total work done by the prepared statement. ** If the number of virtual machine operations exceeds 2147483647 -** then the value returned by this statement status code is undefined. +** then the value returned by this statement status code is undefined.</dd> +** +** [[SQLITE_STMTSTATUS_REPREPARE]] <dt>SQLITE_STMTSTATUS_REPREPARE</dt> +** <dd>^This is the number of times that the prepare statement has been +** automatically regenerated due to schema changes or changes to +** [bound parameters] that might affect the query plan.</dd> +** +** [[SQLITE_STMTSTATUS_RUN]] <dt>SQLITE_STMTSTATUS_RUN</dt> +** <dd>^This is the number of times that the prepared statement has +** been run. A single "run" for the purposes of this counter is one +** or more calls to [sqlite3_step()] followed by a call to [sqlite3_reset()]. +** The counter is incremented on the first [sqlite3_step()] call of each +** cycle.</dd> +** +** [[SQLITE_STMTSTATUS_FILTER_MISS]] +** [[SQLITE_STMTSTATUS_FILTER HIT]] +** <dt>SQLITE_STMTSTATUS_FILTER_HIT<br> +** SQLITE_STMTSTATUS_FILTER_MISS</dt> +** <dd>^SQLITE_STMTSTATUS_FILTER_HIT is the number of times that a join +** step was bypassed because a Bloom filter returned not-found. The +** corresponding SQLITE_STMTSTATUS_FILTER_MISS value is the number of +** times that the Bloom filter returned a find, and thus the join step +** had to be processed as normal.</dd> +** +** [[SQLITE_STMTSTATUS_MEMUSED]] <dt>SQLITE_STMTSTATUS_MEMUSED</dt> +** <dd>^This is the approximate number of bytes of heap memory +** used to store the prepared statement. ^This value is not actually +** a counter, and so the resetFlg parameter to sqlite3_stmt_status() +** is ignored when the opcode is SQLITE_STMTSTATUS_MEMUSED. ** </dd> ** </dl> */ @@ -6824,6 +9247,11 @@ int sqlite3_stmt_status(sqlite3_stmt*, int op,int resetFlg); #define SQLITE_STMTSTATUS_SORT 2 #define SQLITE_STMTSTATUS_AUTOINDEX 3 #define SQLITE_STMTSTATUS_VM_STEP 4 +#define SQLITE_STMTSTATUS_REPREPARE 5 +#define SQLITE_STMTSTATUS_RUN 6 +#define SQLITE_STMTSTATUS_FILTER_MISS 7 +#define SQLITE_STMTSTATUS_FILTER_HIT 8 +#define SQLITE_STMTSTATUS_MEMUSED 99 /* ** CAPI3REF: Custom Page Cache Object @@ -6859,15 +9287,15 @@ struct sqlite3_pcache_page { ** KEYWORDS: {page cache} ** ** ^(The [sqlite3_config]([SQLITE_CONFIG_PCACHE2], ...) interface can -** register an alternative page cache implementation by passing in an +** register an alternative page cache implementation by passing in an ** instance of the sqlite3_pcache_methods2 structure.)^ -** In many applications, most of the heap memory allocated by +** In many applications, most of the heap memory allocated by ** SQLite is used for the page cache. -** By implementing a +** By implementing a ** custom page cache using this API, an application can better control -** the amount of memory consumed by SQLite, the way in which -** that memory is allocated and released, and the policies used to -** determine exactly which parts of a database file are cached and for +** the amount of memory consumed by SQLite, the way in which +** that memory is allocated and released, and the policies used to +** determine exactly which parts of a database file are cached and for ** how long. ** ** The alternative page cache mechanism is an @@ -6880,19 +9308,19 @@ struct sqlite3_pcache_page { ** [sqlite3_config()] returns.)^ ** ** [[the xInit() page cache method]] -** ^(The xInit() method is called once for each effective +** ^(The xInit() method is called once for each effective ** call to [sqlite3_initialize()])^ ** (usually only once during the lifetime of the process). ^(The xInit() ** method is passed a copy of the sqlite3_pcache_methods2.pArg value.)^ -** The intent of the xInit() method is to set up global data structures -** required by the custom page cache implementation. -** ^(If the xInit() method is NULL, then the +** The intent of the xInit() method is to set up global data structures +** required by the custom page cache implementation. +** ^(If the xInit() method is NULL, then the ** built-in default page cache is used instead of the application defined ** page cache.)^ ** ** [[the xShutdown() page cache method]] ** ^The xShutdown() method is called by [sqlite3_shutdown()]. -** It can be used to clean up +** It can be used to clean up ** any outstanding resources before process shutdown, if required. ** ^The xShutdown() method may be NULL. ** @@ -6910,9 +9338,9 @@ struct sqlite3_pcache_page { ** SQLite will typically create one cache instance for each open database file, ** though this is not guaranteed. ^The ** first parameter, szPage, is the size in bytes of the pages that must -** be allocated by the cache. ^szPage will always a power of two. ^The -** second parameter szExtra is a number of bytes of extra storage -** associated with each page cache entry. ^The szExtra parameter will +** be allocated by the cache. ^szPage will always be a power of two. ^The +** second parameter szExtra is a number of bytes of extra storage +** associated with each page cache entry. ^The szExtra parameter will be ** a number less than 250. SQLite will use the ** extra szExtra bytes on each page to store metadata about the underlying ** database page on disk. The value passed into szExtra depends @@ -6920,17 +9348,17 @@ struct sqlite3_pcache_page { ** ^The third argument to xCreate(), bPurgeable, is true if the cache being ** created will be used to cache database pages of a file stored on disk, or ** false if it is used for an in-memory database. The cache implementation -** does not have to do anything special based with the value of bPurgeable; +** does not have to do anything special based upon the value of bPurgeable; ** it is purely advisory. ^On a cache where bPurgeable is false, SQLite will ** never invoke xUnpin() except to deliberately delete a page. ** ^In other words, calls to xUnpin() on a cache with bPurgeable set to -** false will always have the "discard" flag set to true. -** ^Hence, a cache created with bPurgeable false will +** false will always have the "discard" flag set to true. +** ^Hence, a cache created with bPurgeable set to false will ** never contain any unpinned pages. ** ** [[the xCachesize() page cache method]] ** ^(The xCachesize() method may be called at any time by SQLite to set the -** suggested maximum cache-size (number of pages stored by) the cache +** suggested maximum cache-size (number of pages stored) for the cache ** instance passed as the first argument. This is the value configured using ** the SQLite "[PRAGMA cache_size]" command.)^ As with the bPurgeable ** parameter, the implementation is not required to do anything with this @@ -6939,12 +9367,12 @@ struct sqlite3_pcache_page { ** [[the xPagecount() page cache methods]] ** The xPagecount() method must return the number of pages currently ** stored in the cache, both pinned and unpinned. -** +** ** [[the xFetch() page cache methods]] -** The xFetch() method locates a page in the cache and returns a pointer to +** The xFetch() method locates a page in the cache and returns a pointer to ** an sqlite3_pcache_page object associated with that page, or a NULL pointer. ** The pBuf element of the returned sqlite3_pcache_page object will be a -** pointer to a buffer of szPage bytes used to store the content of a +** pointer to a buffer of szPage bytes used to store the content of a ** single database page. The pExtra element of sqlite3_pcache_page will be ** a pointer to the szExtra bytes of extra storage that SQLite has requested ** for each entry in the page cache. @@ -6957,12 +9385,12 @@ struct sqlite3_pcache_page { ** implementation must return a pointer to the page buffer with its content ** intact. If the requested page is not already in the cache, then the ** cache implementation should use the value of the createFlag -** parameter to help it determined what action to take: +** parameter to help it determine what action to take: ** ** <table border=1 width=85% align=center> ** <tr><th> createFlag <th> Behavior when page is not already in cache ** <tr><td> 0 <td> Do not allocate a new page. Return NULL. -** <tr><td> 1 <td> Allocate a new page if it easy and convenient to do so. +** <tr><td> 1 <td> Allocate a new page if it is easy and convenient to do so. ** Otherwise return NULL. ** <tr><td> 2 <td> Make every effort to allocate a new page. Only return ** NULL if allocating a new page is effectively impossible. @@ -6970,7 +9398,7 @@ struct sqlite3_pcache_page { ** ** ^(SQLite will normally invoke xFetch() with a createFlag of 0 or 1. SQLite ** will only use a createFlag of 2 after a prior call with a createFlag of 1 -** failed.)^ In between the to xFetch() calls, SQLite may +** failed.)^ In between the xFetch() calls, SQLite may ** attempt to unpin one or more cache pages by spilling the content of ** pinned pages to disk and synching the operating system disk cache. ** @@ -6979,12 +9407,12 @@ struct sqlite3_pcache_page { ** as its second argument. If the third parameter, discard, is non-zero, ** then the page must be evicted from the cache. ** ^If the discard parameter is -** zero, then the page may be discarded or retained at the discretion of +** zero, then the page may be discarded or retained at the discretion of the ** page cache implementation. ^The page cache implementation ** may choose to evict unpinned pages at any time. ** -** The cache must not perform any reference counting. A single -** call to xUnpin() unpins the page regardless of the number of prior calls +** The cache must not perform any reference counting. A single +** call to xUnpin() unpins the page regardless of the number of prior calls ** to xFetch(). ** ** [[the xRekey() page cache methods]] @@ -6997,7 +9425,7 @@ struct sqlite3_pcache_page { ** When SQLite calls the xTruncate() method, the cache must discard all ** existing cache entries with page numbers (keys) greater than or equal ** to the value of the iLimit parameter passed to xTruncate(). If any -** of these pages are pinned, they are implicitly unpinned, meaning that +** of these pages are pinned, they become implicitly unpinned, meaning that ** they can be safely discarded. ** ** [[the xDestroy() page cache method]] @@ -7024,7 +9452,7 @@ struct sqlite3_pcache_methods2 { int (*xPagecount)(sqlite3_pcache*); sqlite3_pcache_page *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag); void (*xUnpin)(sqlite3_pcache*, sqlite3_pcache_page*, int discard); - void (*xRekey)(sqlite3_pcache*, sqlite3_pcache_page*, + void (*xRekey)(sqlite3_pcache*, sqlite3_pcache_page*, unsigned oldKey, unsigned newKey); void (*xTruncate)(sqlite3_pcache*, unsigned iLimit); void (*xDestroy)(sqlite3_pcache*); @@ -7069,7 +9497,7 @@ typedef struct sqlite3_backup sqlite3_backup; ** ** The backup API copies the content of one database into another. ** It is useful either for creating backups of databases or -** for copying in-memory databases to or from persistent files. +** for copying in-memory databases to or from persistent files. ** ** See Also: [Using the SQLite Online Backup API] ** @@ -7080,36 +9508,36 @@ typedef struct sqlite3_backup sqlite3_backup; ** ^Thus, the backup may be performed on a live source database without ** preventing other database connections from ** reading or writing to the source database while the backup is underway. -** -** ^(To perform a backup operation: +** +** ^(To perform a backup operation: ** <ol> ** <li><b>sqlite3_backup_init()</b> is called once to initialize the -** backup, -** <li><b>sqlite3_backup_step()</b> is called one or more times to transfer +** backup, +** <li><b>sqlite3_backup_step()</b> is called one or more times to transfer ** the data between the two databases, and finally -** <li><b>sqlite3_backup_finish()</b> is called to release all resources -** associated with the backup operation. +** <li><b>sqlite3_backup_finish()</b> is called to release all resources +** associated with the backup operation. ** </ol>)^ ** There should be exactly one call to sqlite3_backup_finish() for each ** successful call to sqlite3_backup_init(). ** ** [[sqlite3_backup_init()]] <b>sqlite3_backup_init()</b> ** -** ^The D and N arguments to sqlite3_backup_init(D,N,S,M) are the -** [database connection] associated with the destination database +** ^The D and N arguments to sqlite3_backup_init(D,N,S,M) are the +** [database connection] associated with the destination database ** and the database name, respectively. ** ^The database name is "main" for the main database, "temp" for the ** temporary database, or the name specified after the AS keyword in ** an [ATTACH] statement for an attached database. -** ^The S and M arguments passed to +** ^The S and M arguments passed to ** sqlite3_backup_init(D,N,S,M) identify the [database connection] ** and database name of the source database, respectively. ** ^The source and destination [database connections] (parameters S and D) ** must be different or else sqlite3_backup_init(D,N,S,M) will fail with ** an error. ** -** ^A call to sqlite3_backup_init() will fail, returning SQLITE_ERROR, if -** there is already a read or read-write transaction open on the +** ^A call to sqlite3_backup_init() will fail, returning NULL, if +** there is already a read or read-write transaction open on the ** destination database. ** ** ^If an error occurs within sqlite3_backup_init(D,N,S,M), then NULL is @@ -7121,14 +9549,14 @@ typedef struct sqlite3_backup sqlite3_backup; ** ^A successful call to sqlite3_backup_init() returns a pointer to an ** [sqlite3_backup] object. ** ^The [sqlite3_backup] object may be used with the sqlite3_backup_step() and -** sqlite3_backup_finish() functions to perform the specified backup +** sqlite3_backup_finish() functions to perform the specified backup ** operation. ** ** [[sqlite3_backup_step()]] <b>sqlite3_backup_step()</b> ** -** ^Function sqlite3_backup_step(B,N) will copy up to N pages between +** ^Function sqlite3_backup_step(B,N) will copy up to N pages between ** the source and destination databases specified by [sqlite3_backup] object B. -** ^If N is negative, all remaining source pages are copied. +** ^If N is negative, all remaining source pages are copied. ** ^If sqlite3_backup_step(B,N) successfully copies N pages and there ** are still more pages to be copied, then the function returns [SQLITE_OK]. ** ^If sqlite3_backup_step(B,N) successfully finishes copying all pages @@ -7150,8 +9578,8 @@ typedef struct sqlite3_backup sqlite3_backup; ** ** ^If sqlite3_backup_step() cannot obtain a required file-system lock, then ** the [sqlite3_busy_handler | busy-handler function] -** is invoked (if one is specified). ^If the -** busy-handler returns non-zero before the lock is available, then +** is invoked (if one is specified). ^If the +** busy-handler returns non-zero before the lock is available, then ** [SQLITE_BUSY] is returned to the caller. ^In this case the call to ** sqlite3_backup_step() can be retried later. ^If the source ** [database connection] @@ -7159,15 +9587,15 @@ typedef struct sqlite3_backup sqlite3_backup; ** is called, then [SQLITE_LOCKED] is returned immediately. ^Again, in this ** case the call to sqlite3_backup_step() can be retried later on. ^(If ** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX], [SQLITE_NOMEM], or -** [SQLITE_READONLY] is returned, then -** there is no point in retrying the call to sqlite3_backup_step(). These -** errors are considered fatal.)^ The application must accept -** that the backup operation has failed and pass the backup operation handle +** [SQLITE_READONLY] is returned, then +** there is no point in retrying the call to sqlite3_backup_step(). These +** errors are considered fatal.)^ The application must accept +** that the backup operation has failed and pass the backup operation handle ** to the sqlite3_backup_finish() to release associated resources. ** ** ^The first call to sqlite3_backup_step() obtains an exclusive lock -** on the destination file. ^The exclusive lock is not released until either -** sqlite3_backup_finish() is called or the backup operation is complete +** on the destination file. ^The exclusive lock is not released until either +** sqlite3_backup_finish() is called or the backup operation is complete ** and sqlite3_backup_step() returns [SQLITE_DONE]. ^Every call to ** sqlite3_backup_step() obtains a [shared lock] on the source database that ** lasts for the duration of the sqlite3_backup_step() call. @@ -7176,25 +9604,25 @@ typedef struct sqlite3_backup sqlite3_backup; ** through the backup process. ^If the source database is modified by an ** external process or via a database connection other than the one being ** used by the backup operation, then the backup will be automatically -** restarted by the next call to sqlite3_backup_step(). ^If the source -** database is modified by the using the same database connection as is used +** restarted by the next call to sqlite3_backup_step(). ^If the source +** database is modified by using the same database connection as is used ** by the backup operation, then the backup database is automatically ** updated at the same time. ** ** [[sqlite3_backup_finish()]] <b>sqlite3_backup_finish()</b> ** -** When sqlite3_backup_step() has returned [SQLITE_DONE], or when the +** When sqlite3_backup_step() has returned [SQLITE_DONE], or when the ** application wishes to abandon the backup operation, the application ** should destroy the [sqlite3_backup] by passing it to sqlite3_backup_finish(). ** ^The sqlite3_backup_finish() interfaces releases all -** resources associated with the [sqlite3_backup] object. +** resources associated with the [sqlite3_backup] object. ** ^If sqlite3_backup_step() has not yet returned [SQLITE_DONE], then any ** active write-transaction on the destination database is rolled back. ** The [sqlite3_backup] object is invalid ** and may not be used following a call to sqlite3_backup_finish(). ** ** ^The value returned by sqlite3_backup_finish is [SQLITE_OK] if no -** sqlite3_backup_step() errors occurred, regardless or whether or not +** sqlite3_backup_step() errors occurred, regardless of whether or not ** sqlite3_backup_step() completed. ** ^If an out-of-memory condition or IO error occurred during any prior ** sqlite3_backup_step() call on the same [sqlite3_backup] object, then @@ -7227,28 +9655,38 @@ typedef struct sqlite3_backup sqlite3_backup; ** connections, then the source database connection may be used concurrently ** from within other threads. ** -** However, the application must guarantee that the destination -** [database connection] is not passed to any other API (by any thread) after +** However, the application must guarantee that the destination +** [database connection] is not passed to any other API (by any thread) after ** sqlite3_backup_init() is called and before the corresponding call to ** sqlite3_backup_finish(). SQLite does not currently check to see ** if the application incorrectly accesses the destination [database connection] ** and so no error code is reported, but the operations may malfunction ** nevertheless. Use of the destination database connection while a -** backup is in progress might also also cause a mutex deadlock. +** backup is in progress might also cause a mutex deadlock. ** ** If running in [shared cache mode], the application must ** guarantee that the shared cache used by the destination database ** is not accessed while the backup is running. In practice this means -** that the application must guarantee that the disk file being +** that the application must guarantee that the disk file being ** backed up to is not accessed by any connection within the process, ** not just the specific connection that was passed to sqlite3_backup_init(). ** -** The [sqlite3_backup] object itself is partially threadsafe. Multiple +** The [sqlite3_backup] object itself is partially threadsafe. Multiple ** threads may safely make multiple concurrent calls to sqlite3_backup_step(). ** However, the sqlite3_backup_remaining() and sqlite3_backup_pagecount() ** APIs are not strictly speaking threadsafe. If they are invoked at the ** same time as another thread is invoking sqlite3_backup_step() it is ** possible that they return invalid values. +** +** <b>Alternatives To Using The Backup API</b> +** +** Other techniques for safely creating a consistent backup of an SQLite +** database include: +** +** <ul> +** <li> The [VACUUM INTO] command. +** <li> The [sqlite3_rsync] utility program. +** </ul> */ sqlite3_backup *sqlite3_backup_init( sqlite3 *pDest, /* Destination database handle */ @@ -7268,8 +9706,8 @@ int sqlite3_backup_pagecount(sqlite3_backup *p); ** ^When running in shared-cache mode, a database operation may fail with ** an [SQLITE_LOCKED] error if the required locks on the shared-cache or ** individual tables within the shared-cache cannot be obtained. See -** [SQLite Shared-Cache Mode] for a description of shared-cache locking. -** ^This API may be used to register a callback that SQLite will invoke +** [SQLite Shared-Cache Mode] for a description of shared-cache locking. +** ^This API may be used to register a callback that SQLite will invoke ** when the connection currently holding the required lock relinquishes it. ** ^This API is only available if the library was compiled with the ** [SQLITE_ENABLE_UNLOCK_NOTIFY] C-preprocessor symbol defined. @@ -7277,18 +9715,18 @@ int sqlite3_backup_pagecount(sqlite3_backup *p); ** See Also: [Using the SQLite Unlock Notification Feature]. ** ** ^Shared-cache locks are released when a database connection concludes -** its current transaction, either by committing it or rolling it back. +** its current transaction, either by committing it or rolling it back. ** ** ^When a connection (known as the blocked connection) fails to obtain a ** shared-cache lock and SQLITE_LOCKED is returned to the caller, the ** identity of the database connection (the blocking connection) that -** has locked the required resource is stored internally. ^After an +** has locked the required resource is stored internally. ^After an ** application receives an SQLITE_LOCKED error, it may call the -** sqlite3_unlock_notify() method with the blocked connection handle as +** sqlite3_unlock_notify() method with the blocked connection handle as ** the first argument to register for a callback that will be invoked -** when the blocking connections current transaction is concluded. ^The +** when the blocking connection's current transaction is concluded. ^The ** callback is invoked from within the [sqlite3_step] or [sqlite3_close] -** call that concludes the blocking connections transaction. +** call that concludes the blocking connection's transaction. ** ** ^(If sqlite3_unlock_notify() is called in a multi-threaded application, ** there is a chance that the blocking connection will have already @@ -7298,15 +9736,15 @@ int sqlite3_backup_pagecount(sqlite3_backup *p); ** ** ^If the blocked connection is attempting to obtain a write-lock on a ** shared-cache table, and more than one other connection currently holds -** a read-lock on the same table, then SQLite arbitrarily selects one of +** a read-lock on the same table, then SQLite arbitrarily selects one of ** the other connections to use as the blocking connection. ** -** ^(There may be at most one unlock-notify callback registered by a +** ^(There may be at most one unlock-notify callback registered by a ** blocked connection. If sqlite3_unlock_notify() is called when the ** blocked connection already has a registered unlock-notify callback, ** then the new callback replaces the old.)^ ^If sqlite3_unlock_notify() is ** called with a NULL pointer as its second argument, then any existing -** unlock-notify callback is canceled. ^The blocked connections +** unlock-notify callback is canceled. ^The blocked connection's ** unlock-notify callback may also be canceled by closing the blocked ** connection using [sqlite3_close()]. ** @@ -7319,25 +9757,25 @@ int sqlite3_backup_pagecount(sqlite3_backup *p); ** ** <b>Callback Invocation Details</b> ** -** When an unlock-notify callback is registered, the application provides a +** When an unlock-notify callback is registered, the application provides a ** single void* pointer that is passed to the callback when it is invoked. ** However, the signature of the callback function allows SQLite to pass ** it an array of void* context pointers. The first argument passed to ** an unlock-notify callback is a pointer to an array of void* pointers, ** and the second is the number of entries in the array. ** -** When a blocking connections transaction is concluded, there may be +** When a blocking connection's transaction is concluded, there may be ** more than one blocked connection that has registered for an unlock-notify ** callback. ^If two or more such blocked connections have specified the ** same callback function, then instead of invoking the callback function ** multiple times, it is invoked once with the set of void* context pointers ** specified by the blocked connections bundled together into an array. -** This gives the application an opportunity to prioritize any actions +** This gives the application an opportunity to prioritize any actions ** related to the set of unblocked database connections. ** ** <b>Deadlock Detection</b> ** -** Assuming that after registering for an unlock-notify callback a +** Assuming that after registering for an unlock-notify callback a ** database waits for the callback to be issued before taking any further ** action (a reasonable assumption), then using this API may cause the ** application to deadlock. For example, if connection X is waiting for @@ -7360,7 +9798,7 @@ int sqlite3_backup_pagecount(sqlite3_backup *p); ** ** <b>The "DROP TABLE" Exception</b> ** -** When a call to [sqlite3_step()] returns SQLITE_LOCKED, it is almost +** When a call to [sqlite3_step()] returns SQLITE_LOCKED, it is almost ** always appropriate to call sqlite3_unlock_notify(). There is however, ** one exception. When executing a "DROP TABLE" or "DROP INDEX" statement, ** SQLite checks if there are any currently executing SELECT statements @@ -7373,7 +9811,7 @@ int sqlite3_backup_pagecount(sqlite3_backup *p); ** One way around this problem is to check the extended error code returned ** by an sqlite3_step() call. ^(If there is a blocking connection, then the ** extended error code is set to SQLITE_LOCKED_SHAREDCACHE. Otherwise, in -** the special "DROP TABLE/INDEX" case, the extended error code is just +** the special "DROP TABLE/INDEX" case, the extended error code is just ** SQLITE_LOCKED.)^ */ int sqlite3_unlock_notify( @@ -7464,8 +9902,8 @@ void sqlite3_log(int iErrCode, const char *zFormat, ...); ** ^The [sqlite3_wal_hook()] function is used to register a callback that ** is invoked each time data is committed to a database in wal mode. ** -** ^(The callback is invoked by SQLite after the commit has taken place and -** the associated write-lock on the database released)^, so the implementation +** ^(The callback is invoked by SQLite after the commit has taken place and +** the associated write-lock on the database released)^, so the implementation ** may read, write or [checkpoint] the database as required. ** ** ^The first parameter passed to the callback function when it is invoked @@ -7476,7 +9914,7 @@ void sqlite3_log(int iErrCode, const char *zFormat, ...); ** is the number of pages currently in the write-ahead log file, ** including those that were just committed. ** -** The callback function should normally return [SQLITE_OK]. ^If an error +** ^The callback function should normally return [SQLITE_OK]. ^If an error ** code is returned, that error will propagate back up through the ** SQLite code base to cause the statement that provoked the callback ** to report an error, though the commit will have still occurred. If the @@ -7484,15 +9922,29 @@ void sqlite3_log(int iErrCode, const char *zFormat, ...); ** that does not correspond to any valid SQLite error code, the results ** are undefined. ** -** A single database handle may have at most a single write-ahead log callback -** registered at one time. ^Calling [sqlite3_wal_hook()] replaces any -** previously registered write-ahead log callback. ^Note that the -** [sqlite3_wal_autocheckpoint()] interface and the -** [wal_autocheckpoint pragma] both invoke [sqlite3_wal_hook()] and will -** those overwrite any prior [sqlite3_wal_hook()] settings. +** ^A single database handle may have at most a single write-ahead log +** callback registered at one time. ^Calling [sqlite3_wal_hook()] +** replaces the default behavior or previously registered write-ahead +** log callback. +** +** ^The return value is a copy of the third parameter from the +** previous call, if any, or 0. +** +** ^The [sqlite3_wal_autocheckpoint()] interface and the +** [wal_autocheckpoint pragma] both invoke [sqlite3_wal_hook()] and +** will overwrite any prior [sqlite3_wal_hook()] settings. +** +** ^If a write-ahead log callback is set using this function then +** [sqlite3_wal_checkpoint_v2()] or [PRAGMA wal_checkpoint] +** should be invoked periodically to keep the write-ahead log file +** from growing without bound. +** +** ^Passing a NULL pointer for the callback disables automatic +** checkpointing entirely. To re-enable the default behavior, call +** sqlite3_wal_autocheckpoint(db,1000) or use [PRAGMA wal_checkpoint]. */ void *sqlite3_wal_hook( - sqlite3*, + sqlite3*, int(*)(void *,sqlite3*,const char*,int), void* ); @@ -7505,8 +9957,8 @@ void *sqlite3_wal_hook( ** [sqlite3_wal_hook()] that causes any database on [database connection] D ** to automatically [checkpoint] ** after committing a transaction if there are N or -** more frames in the [write-ahead log] file. ^Passing zero or -** a negative value as the nFrame parameter disables automatic +** more frames in the [write-ahead log] file. ^Passing zero or +** a negative value as the N parameter disables automatic ** checkpoints entirely. ** ** ^The callback registered by this function replaces any existing callback @@ -7522,9 +9974,10 @@ void *sqlite3_wal_hook( ** ** ^Every new [database connection] defaults to having the auto-checkpoint ** enabled with a threshold of 1000 or [SQLITE_DEFAULT_WAL_AUTOCHECKPOINT] -** pages. The use of this interface -** is only necessary if the default setting is found to be suboptimal -** for a particular application. +** pages. +** +** ^The use of this interface is only necessary if the default setting +** is found to be suboptimal for a particular application. */ int sqlite3_wal_autocheckpoint(sqlite3 *db, int N); @@ -7535,7 +9988,7 @@ int sqlite3_wal_autocheckpoint(sqlite3 *db, int N); ** ^(The sqlite3_wal_checkpoint(D,X) is equivalent to ** [sqlite3_wal_checkpoint_v2](D,X,[SQLITE_CHECKPOINT_PASSIVE],0,0).)^ ** -** In brief, sqlite3_wal_checkpoint(D,X) causes the content in the +** In brief, sqlite3_wal_checkpoint(D,X) causes the content in the ** [write-ahead log] for database X on [database connection] D to be ** transferred into the database file and for the write-ahead log to ** be reset. See the [checkpointing] documentation for addition @@ -7561,10 +10014,10 @@ int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb); ** ** <dl> ** <dt>SQLITE_CHECKPOINT_PASSIVE<dd> -** ^Checkpoint as many frames as possible without waiting for any database -** readers or writers to finish, then sync the database file if all frames +** ^Checkpoint as many frames as possible without waiting for any database +** readers or writers to finish, then sync the database file if all frames ** in the log were checkpointed. ^The [busy-handler callback] -** is never invoked in the SQLITE_CHECKPOINT_PASSIVE mode. +** is never invoked in the SQLITE_CHECKPOINT_PASSIVE mode. ** ^On the other hand, passive mode might leave the checkpoint unfinished ** if there are concurrent readers or writers. ** @@ -7578,9 +10031,9 @@ int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb); ** ** <dt>SQLITE_CHECKPOINT_RESTART<dd> ** ^This mode works the same way as SQLITE_CHECKPOINT_FULL with the addition -** that after checkpointing the log file it blocks (calls the +** that after checkpointing the log file it blocks (calls the ** [busy-handler callback]) -** until all readers are reading from the database file only. ^This ensures +** until all readers are reading from the database file only. ^This ensures ** that the next writer will restart the log file from the beginning. ** ^Like SQLITE_CHECKPOINT_FULL, this mode blocks new ** database writer attempts while it is pending, but does not impede readers. @@ -7589,6 +10042,11 @@ int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb); ** ^This mode works the same way as SQLITE_CHECKPOINT_RESTART with the ** addition that it also truncates the log file to zero bytes just prior ** to a successful return. +** +** <dt>SQLITE_CHECKPOINT_NOOP<dd> +** ^This mode always checkpoints zero frames. The only reason to invoke +** a NOOP checkpoint is to access the values returned by +** sqlite3_wal_checkpoint_v2() via output parameters *pnLog and *pnCkpt. ** </dl> ** ** ^If pnLog is not NULL, then *pnLog is set to the total number of frames in @@ -7602,31 +10060,31 @@ int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb); ** truncated to zero bytes and so both *pnLog and *pnCkpt will be set to zero. ** ** ^All calls obtain an exclusive "checkpoint" lock on the database file. ^If -** any other process is running a checkpoint operation at the same time, the -** lock cannot be obtained and SQLITE_BUSY is returned. ^Even if there is a +** any other process is running a checkpoint operation at the same time, the +** lock cannot be obtained and SQLITE_BUSY is returned. ^Even if there is a ** busy-handler configured, it will not be invoked in this case. ** -** ^The SQLITE_CHECKPOINT_FULL, RESTART and TRUNCATE modes also obtain the +** ^The SQLITE_CHECKPOINT_FULL, RESTART and TRUNCATE modes also obtain the ** exclusive "writer" lock on the database file. ^If the writer lock cannot be ** obtained immediately, and a busy-handler is configured, it is invoked and ** the writer lock retried until either the busy-handler returns 0 or the lock ** is successfully obtained. ^The busy-handler is also invoked while waiting for ** database readers as described above. ^If the busy-handler returns 0 before ** the writer lock is obtained or while waiting for database readers, the -** checkpoint operation proceeds from that point in the same way as -** SQLITE_CHECKPOINT_PASSIVE - checkpointing as many frames as possible +** checkpoint operation proceeds from that point in the same way as +** SQLITE_CHECKPOINT_PASSIVE - checkpointing as many frames as possible ** without blocking any further. ^SQLITE_BUSY is returned in this case. ** ** ^If parameter zDb is NULL or points to a zero length string, then the -** specified operation is attempted on all WAL databases [attached] to +** specified operation is attempted on all WAL databases [attached] to ** [database connection] db. In this case the -** values written to output parameters *pnLog and *pnCkpt are undefined. ^If -** an SQLITE_BUSY error is encountered when processing one or more of the -** attached WAL databases, the operation is still attempted on any remaining -** attached databases and SQLITE_BUSY is returned at the end. ^If any other -** error occurs while processing an attached database, processing is abandoned -** and the error code is returned to the caller immediately. ^If no error -** (SQLITE_BUSY or otherwise) is encountered while processing the attached +** values written to output parameters *pnLog and *pnCkpt are undefined. ^If +** an SQLITE_BUSY error is encountered when processing one or more of the +** attached WAL databases, the operation is still attempted on any remaining +** attached databases and SQLITE_BUSY is returned at the end. ^If any other +** error occurs while processing an attached database, processing is abandoned +** and the error code is returned to the caller immediately. ^If no error +** (SQLITE_BUSY or otherwise) is encountered while processing the attached ** databases, SQLITE_OK is returned. ** ** ^If database zDb is the name of an attached database that is not in WAL @@ -7659,9 +10117,10 @@ int sqlite3_wal_checkpoint_v2( ** See the [sqlite3_wal_checkpoint_v2()] documentation for details on the ** meaning of each of these checkpoint modes. */ +#define SQLITE_CHECKPOINT_NOOP -1 /* Do no work at all */ #define SQLITE_CHECKPOINT_PASSIVE 0 /* Do as much as possible w/o blocking */ #define SQLITE_CHECKPOINT_FULL 1 /* Wait for writers, then checkpoint */ -#define SQLITE_CHECKPOINT_RESTART 2 /* Like FULL but wait for for readers */ +#define SQLITE_CHECKPOINT_RESTART 2 /* Like FULL but wait for readers */ #define SQLITE_CHECKPOINT_TRUNCATE 3 /* Like RESTART but also truncate WAL */ /* @@ -7674,21 +10133,28 @@ int sqlite3_wal_checkpoint_v2( ** If this interface is invoked outside the context of an xConnect or ** xCreate virtual table method then the behavior is undefined. ** -** At present, there is only one option that may be configured using -** this function. (See [SQLITE_VTAB_CONSTRAINT_SUPPORT].) Further options -** may be added in the future. +** In the call sqlite3_vtab_config(D,C,...) the D parameter is the +** [database connection] in which the virtual table is being created and +** which is passed in as the first argument to the [xConnect] or [xCreate] +** method that is invoking sqlite3_vtab_config(). The C parameter is one +** of the [virtual table configuration options]. The presence and meaning +** of parameters after C depend on which [virtual table configuration option] +** is used. */ int sqlite3_vtab_config(sqlite3*, int op, ...); /* ** CAPI3REF: Virtual Table Configuration Options +** KEYWORDS: {virtual table configuration options} +** KEYWORDS: {virtual table configuration option} ** ** These macros define the various options to the ** [sqlite3_vtab_config()] interface that [virtual table] implementations ** can use to customize and optimize their behavior. ** ** <dl> -** <dt>SQLITE_VTAB_CONSTRAINT_SUPPORT +** [[SQLITE_VTAB_CONSTRAINT_SUPPORT]] +** <dt>SQLITE_VTAB_CONSTRAINT_SUPPORT</dt> ** <dd>Calls of the form ** [sqlite3_vtab_config](db,SQLITE_VTAB_CONSTRAINT_SUPPORT,X) are supported, ** where X is an integer. If X is zero, then the [virtual table] whose @@ -7696,30 +10162,62 @@ int sqlite3_vtab_config(sqlite3*, int op, ...); ** support constraints. In this configuration (which is the default) if ** a call to the [xUpdate] method returns [SQLITE_CONSTRAINT], then the entire ** statement is rolled back as if [ON CONFLICT | OR ABORT] had been -** specified as part of the users SQL statement, regardless of the actual +** specified as part of the user's SQL statement, regardless of the actual ** ON CONFLICT mode specified. ** ** If X is non-zero, then the virtual table implementation guarantees ** that if [xUpdate] returns [SQLITE_CONSTRAINT], it will do so before ** any modifications to internal or persistent data structures have been made. -** If the [ON CONFLICT] mode is ABORT, FAIL, IGNORE or ROLLBACK, SQLite +** If the [ON CONFLICT] mode is ABORT, FAIL, IGNORE or ROLLBACK, SQLite ** is able to roll back a statement or database transaction, and abandon -** or continue processing the current SQL statement as appropriate. +** or continue processing the current SQL statement as appropriate. ** If the ON CONFLICT mode is REPLACE and the [xUpdate] method returns ** [SQLITE_CONSTRAINT], SQLite handles this as if the ON CONFLICT mode ** had been ABORT. ** ** Virtual table implementations that are required to handle OR REPLACE -** must do so within the [xUpdate] method. If a call to the -** [sqlite3_vtab_on_conflict()] function indicates that the current ON -** CONFLICT policy is REPLACE, the virtual table implementation should +** must do so within the [xUpdate] method. If a call to the +** [sqlite3_vtab_on_conflict()] function indicates that the current ON +** CONFLICT policy is REPLACE, the virtual table implementation should ** silently replace the appropriate rows within the xUpdate callback and ** return SQLITE_OK. Or, if this is not possible, it may return -** SQLITE_CONSTRAINT, in which case SQLite falls back to OR ABORT +** SQLITE_CONSTRAINT, in which case SQLite falls back to OR ABORT ** constraint handling. +** </dd> +** +** [[SQLITE_VTAB_DIRECTONLY]]<dt>SQLITE_VTAB_DIRECTONLY</dt> +** <dd>Calls of the form +** [sqlite3_vtab_config](db,SQLITE_VTAB_DIRECTONLY) from within the +** the [xConnect] or [xCreate] methods of a [virtual table] implementation +** prohibits that virtual table from being used from within triggers and +** views. +** </dd> +** +** [[SQLITE_VTAB_INNOCUOUS]]<dt>SQLITE_VTAB_INNOCUOUS</dt> +** <dd>Calls of the form +** [sqlite3_vtab_config](db,SQLITE_VTAB_INNOCUOUS) from within the +** [xConnect] or [xCreate] methods of a [virtual table] implementation +** identify that virtual table as being safe to use from within triggers +** and views. Conceptually, the SQLITE_VTAB_INNOCUOUS tag means that the +** virtual table can do no serious harm even if it is controlled by a +** malicious hacker. Developers should avoid setting the SQLITE_VTAB_INNOCUOUS +** flag unless absolutely necessary. +** </dd> +** +** [[SQLITE_VTAB_USES_ALL_SCHEMAS]]<dt>SQLITE_VTAB_USES_ALL_SCHEMAS</dt> +** <dd>Calls of the form +** [sqlite3_vtab_config](db,SQLITE_VTAB_USES_ALL_SCHEMA) from within the +** the [xConnect] or [xCreate] methods of a [virtual table] implementation +** instruct the query planner to begin at least a read transaction on +** all schemas ("main", "temp", and any ATTACH-ed databases) whenever the +** virtual table is used. +** </dd> ** </dl> */ #define SQLITE_VTAB_CONSTRAINT_SUPPORT 1 +#define SQLITE_VTAB_INNOCUOUS 2 +#define SQLITE_VTAB_DIRECTONLY 3 +#define SQLITE_VTAB_USES_ALL_SCHEMAS 4 /* ** CAPI3REF: Determine The Virtual Table Conflict Policy @@ -7733,13 +10231,331 @@ int sqlite3_vtab_config(sqlite3*, int op, ...); */ int sqlite3_vtab_on_conflict(sqlite3 *); +/* +** CAPI3REF: Determine If Virtual Table Column Access Is For UPDATE +** +** If the sqlite3_vtab_nochange(X) routine is called within the [xColumn] +** method of a [virtual table], then it might return true if the +** column is being fetched as part of an UPDATE operation during which the +** column value will not change. The virtual table implementation can use +** this hint as permission to substitute a return value that is less +** expensive to compute and that the corresponding +** [xUpdate] method understands as a "no-change" value. +** +** If the [xColumn] method calls sqlite3_vtab_nochange() and finds that +** the column is not changed by the UPDATE statement, then the xColumn +** method can optionally return without setting a result, without calling +** any of the [sqlite3_result_int|sqlite3_result_xxxxx() interfaces]. +** In that case, [sqlite3_value_nochange(X)] will return true for the +** same column in the [xUpdate] method. +** +** The sqlite3_vtab_nochange() routine is an optimization. Virtual table +** implementations should continue to give a correct answer even if the +** sqlite3_vtab_nochange() interface were to always return false. In the +** current implementation, the sqlite3_vtab_nochange() interface does always +** returns false for the enhanced [UPDATE FROM] statement. +*/ +int sqlite3_vtab_nochange(sqlite3_context*); + +/* +** CAPI3REF: Determine The Collation For a Virtual Table Constraint +** METHOD: sqlite3_index_info +** +** This function may only be called from within a call to the [xBestIndex] +** method of a [virtual table]. This function returns a pointer to a string +** that is the name of the appropriate collation sequence to use for text +** comparisons on the constraint identified by its arguments. +** +** The first argument must be the pointer to the [sqlite3_index_info] object +** that is the first parameter to the xBestIndex() method. The second argument +** must be an index into the aConstraint[] array belonging to the +** sqlite3_index_info structure passed to xBestIndex. +** +** Important: +** The first parameter must be the same pointer that is passed into the +** xBestMethod() method. The first parameter may not be a pointer to a +** different [sqlite3_index_info] object, even an exact copy. +** +** The return value is computed as follows: +** +** <ol> +** <li><p> If the constraint comes from a WHERE clause expression that contains +** a [COLLATE operator], then the name of the collation specified by +** that COLLATE operator is returned. +** <li><p> If there is no COLLATE operator, but the column that is the subject +** of the constraint specifies an alternative collating sequence via +** a [COLLATE clause] on the column definition within the CREATE TABLE +** statement that was passed into [sqlite3_declare_vtab()], then the +** name of that alternative collating sequence is returned. +** <li><p> Otherwise, "BINARY" is returned. +** </ol> +*/ +const char *sqlite3_vtab_collation(sqlite3_index_info*,int); + +/* +** CAPI3REF: Determine if a virtual table query is DISTINCT +** METHOD: sqlite3_index_info +** +** This API may only be used from within an [xBestIndex|xBestIndex method] +** of a [virtual table] implementation. The result of calling this +** interface from outside of xBestIndex() is undefined and probably harmful. +** +** ^The sqlite3_vtab_distinct() interface returns an integer between 0 and +** 3. The integer returned by sqlite3_vtab_distinct() +** gives the virtual table additional information about how the query +** planner wants the output to be ordered. As long as the virtual table +** can meet the ordering requirements of the query planner, it may set +** the "orderByConsumed" flag. +** +** <ol><li value="0"><p> +** ^If the sqlite3_vtab_distinct() interface returns 0, that means +** that the query planner needs the virtual table to return all rows in the +** sort order defined by the "nOrderBy" and "aOrderBy" fields of the +** [sqlite3_index_info] object. This is the default expectation. If the +** virtual table outputs all rows in sorted order, then it is always safe for +** the xBestIndex method to set the "orderByConsumed" flag, regardless of +** the return value from sqlite3_vtab_distinct(). +** <li value="1"><p> +** ^(If the sqlite3_vtab_distinct() interface returns 1, that means +** that the query planner does not need the rows to be returned in sorted order +** as long as all rows with the same values in all columns identified by the +** "aOrderBy" field are adjacent.)^ This mode is used when the query planner +** is doing a GROUP BY. +** <li value="2"><p> +** ^(If the sqlite3_vtab_distinct() interface returns 2, that means +** that the query planner does not need the rows returned in any particular +** order, as long as rows with the same values in all columns identified +** by "aOrderBy" are adjacent.)^ ^(Furthermore, when two or more rows +** contain the same values for all columns identified by "colUsed", all but +** one such row may optionally be omitted from the result.)^ +** The virtual table is not required to omit rows that are duplicates +** over the "colUsed" columns, but if the virtual table can do that without +** too much extra effort, it could potentially help the query to run faster. +** This mode is used for a DISTINCT query. +** <li value="3"><p> +** ^(If the sqlite3_vtab_distinct() interface returns 3, that means the +** virtual table must return rows in the order defined by "aOrderBy" as +** if the sqlite3_vtab_distinct() interface had returned 0. However if +** two or more rows in the result have the same values for all columns +** identified by "colUsed", then all but one such row may optionally be +** omitted.)^ Like when the return value is 2, the virtual table +** is not required to omit rows that are duplicates over the "colUsed" +** columns, but if the virtual table can do that without +** too much extra effort, it could potentially help the query to run faster. +** This mode is used for queries +** that have both DISTINCT and ORDER BY clauses. +** </ol> +** +** <p>The following table summarizes the conditions under which the +** virtual table is allowed to set the "orderByConsumed" flag based on +** the value returned by sqlite3_vtab_distinct(). This table is a +** restatement of the previous four paragraphs: +** +** <table border=1 cellspacing=0 cellpadding=10 width="90%"> +** <tr> +** <td valign="top">sqlite3_vtab_distinct() return value +** <td valign="top">Rows are returned in aOrderBy order +** <td valign="top">Rows with the same value in all aOrderBy columns are adjacent +** <td valign="top">Duplicates over all colUsed columns may be omitted +** <tr><td>0<td>yes<td>yes<td>no +** <tr><td>1<td>no<td>yes<td>no +** <tr><td>2<td>no<td>yes<td>yes +** <tr><td>3<td>yes<td>yes<td>yes +** </table> +** +** ^For the purposes of comparing virtual table output values to see if the +** values are the same value for sorting purposes, two NULL values are considered +** to be the same. In other words, the comparison operator is "IS" +** (or "IS NOT DISTINCT FROM") and not "==". +** +** If a virtual table implementation is unable to meet the requirements +** specified above, then it must not set the "orderByConsumed" flag in the +** [sqlite3_index_info] object or an incorrect answer may result. +** +** ^A virtual table implementation is always free to return rows in any order +** it wants, as long as the "orderByConsumed" flag is not set. ^When the +** "orderByConsumed" flag is unset, the query planner will add extra +** [bytecode] to ensure that the final results returned by the SQL query are +** ordered correctly. The use of the "orderByConsumed" flag and the +** sqlite3_vtab_distinct() interface is merely an optimization. ^Careful +** use of the sqlite3_vtab_distinct() interface and the "orderByConsumed" +** flag might help queries against a virtual table to run faster. Being +** overly aggressive and setting the "orderByConsumed" flag when it is not +** valid to do so, on the other hand, might cause SQLite to return incorrect +** results. +*/ +int sqlite3_vtab_distinct(sqlite3_index_info*); + +/* +** CAPI3REF: Identify and handle IN constraints in xBestIndex +** +** This interface may only be used from within an +** [xBestIndex|xBestIndex() method] of a [virtual table] implementation. +** The result of invoking this interface from any other context is +** undefined and probably harmful. +** +** ^(A constraint on a virtual table of the form +** "[IN operator|column IN (...)]" is +** communicated to the xBestIndex method as a +** [SQLITE_INDEX_CONSTRAINT_EQ] constraint.)^ If xBestIndex wants to use +** this constraint, it must set the corresponding +** aConstraintUsage[].argvIndex to a positive integer. ^(Then, under +** the usual mode of handling IN operators, SQLite generates [bytecode] +** that invokes the [xFilter|xFilter() method] once for each value +** on the right-hand side of the IN operator.)^ Thus the virtual table +** only sees a single value from the right-hand side of the IN operator +** at a time. +** +** In some cases, however, it would be advantageous for the virtual +** table to see all values on the right-hand of the IN operator all at +** once. The sqlite3_vtab_in() interfaces facilitates this in two ways: +** +** <ol> +** <li><p> +** ^A call to sqlite3_vtab_in(P,N,-1) will return true (non-zero) +** if and only if the [sqlite3_index_info|P->aConstraint][N] constraint +** is an [IN operator] that can be processed all at once. ^In other words, +** sqlite3_vtab_in() with -1 in the third argument is a mechanism +** by which the virtual table can ask SQLite if all-at-once processing +** of the IN operator is even possible. +** +** <li><p> +** ^A call to sqlite3_vtab_in(P,N,F) with F==1 or F==0 indicates +** to SQLite that the virtual table does or does not want to process +** the IN operator all-at-once, respectively. ^Thus when the third +** parameter (F) is non-negative, this interface is the mechanism by +** which the virtual table tells SQLite how it wants to process the +** IN operator. +** </ol> +** +** ^The sqlite3_vtab_in(P,N,F) interface can be invoked multiple times +** within the same xBestIndex method call. ^For any given P,N pair, +** the return value from sqlite3_vtab_in(P,N,F) will always be the same +** within the same xBestIndex call. ^If the interface returns true +** (non-zero), that means that the constraint is an IN operator +** that can be processed all-at-once. ^If the constraint is not an IN +** operator or cannot be processed all-at-once, then the interface returns +** false. +** +** ^(All-at-once processing of the IN operator is selected if both of the +** following conditions are met: +** +** <ol> +** <li><p> The P->aConstraintUsage[N].argvIndex value is set to a positive +** integer. This is how the virtual table tells SQLite that it wants to +** use the N-th constraint. +** +** <li><p> The last call to sqlite3_vtab_in(P,N,F) for which F was +** non-negative had F>=1. +** </ol>)^ +** +** ^If either or both of the conditions above are false, then SQLite uses +** the traditional one-at-a-time processing strategy for the IN constraint. +** ^If both conditions are true, then the argvIndex-th parameter to the +** xFilter method will be an [sqlite3_value] that appears to be NULL, +** but which can be passed to [sqlite3_vtab_in_first()] and +** [sqlite3_vtab_in_next()] to find all values on the right-hand side +** of the IN constraint. +*/ +int sqlite3_vtab_in(sqlite3_index_info*, int iCons, int bHandle); + +/* +** CAPI3REF: Find all elements on the right-hand side of an IN constraint. +** +** These interfaces are only useful from within the +** [xFilter|xFilter() method] of a [virtual table] implementation. +** The result of invoking these interfaces from any other context +** is undefined and probably harmful. +** +** The X parameter in a call to sqlite3_vtab_in_first(X,P) or +** sqlite3_vtab_in_next(X,P) should be one of the parameters to the +** xFilter method which invokes these routines, and specifically +** a parameter that was previously selected for all-at-once IN constraint +** processing using the [sqlite3_vtab_in()] interface in the +** [xBestIndex|xBestIndex method]. ^(If the X parameter is not +** an xFilter argument that was selected for all-at-once IN constraint +** processing, then these routines return [SQLITE_ERROR].)^ +** +** ^(Use these routines to access all values on the right-hand side +** of the IN constraint using code like the following: +** +** <blockquote><pre> +** &nbsp; for(rc=sqlite3_vtab_in_first(pList, &pVal); +** &nbsp; rc==SQLITE_OK && pVal; +** &nbsp; rc=sqlite3_vtab_in_next(pList, &pVal) +** &nbsp; ){ +** &nbsp; // do something with pVal +** &nbsp; } +** &nbsp; if( rc!=SQLITE_DONE ){ +** &nbsp; // an error has occurred +** &nbsp; } +** </pre></blockquote>)^ +** +** ^On success, the sqlite3_vtab_in_first(X,P) and sqlite3_vtab_in_next(X,P) +** routines return SQLITE_OK and set *P to point to the first or next value +** on the RHS of the IN constraint. ^If there are no more values on the +** right hand side of the IN constraint, then *P is set to NULL and these +** routines return [SQLITE_DONE]. ^The return value might be +** some other value, such as SQLITE_NOMEM, in the event of a malfunction. +** +** The *ppOut values returned by these routines are only valid until the +** next call to either of these routines or until the end of the xFilter +** method from which these routines were called. If the virtual table +** implementation needs to retain the *ppOut values for longer, it must make +** copies. The *ppOut values are [protected sqlite3_value|protected]. +*/ +int sqlite3_vtab_in_first(sqlite3_value *pVal, sqlite3_value **ppOut); +int sqlite3_vtab_in_next(sqlite3_value *pVal, sqlite3_value **ppOut); + +/* +** CAPI3REF: Constraint values in xBestIndex() +** METHOD: sqlite3_index_info +** +** This API may only be used from within the [xBestIndex|xBestIndex method] +** of a [virtual table] implementation. The result of calling this interface +** from outside of an xBestIndex method are undefined and probably harmful. +** +** ^When the sqlite3_vtab_rhs_value(P,J,V) interface is invoked from within +** the [xBestIndex] method of a [virtual table] implementation, with P being +** a copy of the [sqlite3_index_info] object pointer passed into xBestIndex and +** J being a 0-based index into P->aConstraint[], then this routine +** attempts to set *V to the value of the right-hand operand of +** that constraint if the right-hand operand is known. ^If the +** right-hand operand is not known, then *V is set to a NULL pointer. +** ^The sqlite3_vtab_rhs_value(P,J,V) interface returns SQLITE_OK if +** and only if *V is set to a value. ^The sqlite3_vtab_rhs_value(P,J,V) +** inteface returns SQLITE_NOTFOUND if the right-hand side of the J-th +** constraint is not available. ^The sqlite3_vtab_rhs_value() interface +** can return a result code other than SQLITE_OK or SQLITE_NOTFOUND if +** something goes wrong. +** +** The sqlite3_vtab_rhs_value() interface is usually only successful if +** the right-hand operand of a constraint is a literal value in the original +** SQL statement. If the right-hand operand is an expression or a reference +** to some other column or a [host parameter], then sqlite3_vtab_rhs_value() +** will probably return [SQLITE_NOTFOUND]. +** +** ^(Some constraints, such as [SQLITE_INDEX_CONSTRAINT_ISNULL] and +** [SQLITE_INDEX_CONSTRAINT_ISNOTNULL], have no right-hand operand. For such +** constraints, sqlite3_vtab_rhs_value() always returns SQLITE_NOTFOUND.)^ +** +** ^The [sqlite3_value] object returned in *V is a protected sqlite3_value +** and remains valid for the duration of the xBestIndex method call. +** ^When xBestIndex returns, the sqlite3_value object returned by +** sqlite3_vtab_rhs_value() is automatically deallocated. +** +** The "_rhs_" in the name of this routine is an abbreviation for +** "Right-Hand Side". +*/ +int sqlite3_vtab_rhs_value(sqlite3_index_info*, int, sqlite3_value **ppVal); + /* ** CAPI3REF: Conflict resolution modes ** KEYWORDS: {conflict resolution mode} ** ** These constants are returned by [sqlite3_vtab_on_conflict()] to -** inform a [virtual table] implementation what the [ON CONFLICT] mode -** is for the SQL statement being evaluated. +** inform a [virtual table] implementation of the [ON CONFLICT] mode +** for the SQL statement being evaluated. ** ** Note that the [SQLITE_IGNORE] constant is also used as a potential ** return value from the [sqlite3_set_authorizer()] callback and that @@ -7763,39 +10579,55 @@ int sqlite3_vtab_on_conflict(sqlite3 *); ** managed by the prepared statement S and will be automatically freed when ** S is finalized. ** +** Not all values are available for all query elements. When a value is +** not available, the output variable is set to -1 if the value is numeric, +** or to NULL if it is a string (SQLITE_SCANSTAT_NAME). +** ** <dl> ** [[SQLITE_SCANSTAT_NLOOP]] <dt>SQLITE_SCANSTAT_NLOOP</dt> -** <dd>^The [sqlite3_int64] variable pointed to by the T parameter will be +** <dd>^The [sqlite3_int64] variable pointed to by the V parameter will be ** set to the total number of times that the X-th loop has run.</dd> ** ** [[SQLITE_SCANSTAT_NVISIT]] <dt>SQLITE_SCANSTAT_NVISIT</dt> -** <dd>^The [sqlite3_int64] variable pointed to by the T parameter will be set +** <dd>^The [sqlite3_int64] variable pointed to by the V parameter will be set ** to the total number of rows examined by all iterations of the X-th loop.</dd> ** ** [[SQLITE_SCANSTAT_EST]] <dt>SQLITE_SCANSTAT_EST</dt> -** <dd>^The "double" variable pointed to by the T parameter will be set to the +** <dd>^The "double" variable pointed to by the V parameter will be set to the ** query planner's estimate for the average number of rows output from each -** iteration of the X-th loop. If the query planner's estimates was accurate, +** iteration of the X-th loop. If the query planner's estimate was accurate, ** then this value will approximate the quotient NVISIT/NLOOP and the ** product of this value for all prior loops with the same SELECTID will -** be the NLOOP value for the current loop. +** be the NLOOP value for the current loop.</dd> ** ** [[SQLITE_SCANSTAT_NAME]] <dt>SQLITE_SCANSTAT_NAME</dt> -** <dd>^The "const char *" variable pointed to by the T parameter will be set +** <dd>^The "const char *" variable pointed to by the V parameter will be set ** to a zero-terminated UTF-8 string containing the name of the index or table -** used for the X-th loop. +** used for the X-th loop.</dd> ** ** [[SQLITE_SCANSTAT_EXPLAIN]] <dt>SQLITE_SCANSTAT_EXPLAIN</dt> -** <dd>^The "const char *" variable pointed to by the T parameter will be set +** <dd>^The "const char *" variable pointed to by the V parameter will be set ** to a zero-terminated UTF-8 string containing the [EXPLAIN QUERY PLAN] -** description for the X-th loop. -** -** [[SQLITE_SCANSTAT_SELECTID]] <dt>SQLITE_SCANSTAT_SELECT</dt> -** <dd>^The "int" variable pointed to by the T parameter will be set to the -** "select-id" for the X-th loop. The select-id identifies which query or -** subquery the loop is part of. The main query has a select-id of zero. -** The select-id is the same value as is output in the first column -** of an [EXPLAIN QUERY PLAN] query. +** description for the X-th loop.</dd> +** +** [[SQLITE_SCANSTAT_SELECTID]] <dt>SQLITE_SCANSTAT_SELECTID</dt> +** <dd>^The "int" variable pointed to by the V parameter will be set to the +** id for the X-th query plan element. The id value is unique within the +** statement. The select-id is the same value as is output in the first +** column of an [EXPLAIN QUERY PLAN] query.</dd> +** +** [[SQLITE_SCANSTAT_PARENTID]] <dt>SQLITE_SCANSTAT_PARENTID</dt> +** <dd>The "int" variable pointed to by the V parameter will be set to the +** id of the parent of the current query element, if applicable, or +** to zero if the query element has no parent. This is the same value as +** returned in the second column of an [EXPLAIN QUERY PLAN] query.</dd> +** +** [[SQLITE_SCANSTAT_NCYCLE]] <dt>SQLITE_SCANSTAT_NCYCLE</dt> +** <dd>The sqlite3_int64 output value is set to the number of cycles, +** according to the processor time-stamp counter, that elapsed while the +** query element was being processed. This value is not available for +** all query elements - if it is unavailable the output variable is +** set to -1.</dd> ** </dl> */ #define SQLITE_SCANSTAT_NLOOP 0 @@ -7804,12 +10636,14 @@ int sqlite3_vtab_on_conflict(sqlite3 *); #define SQLITE_SCANSTAT_NAME 3 #define SQLITE_SCANSTAT_EXPLAIN 4 #define SQLITE_SCANSTAT_SELECTID 5 +#define SQLITE_SCANSTAT_PARENTID 6 +#define SQLITE_SCANSTAT_NCYCLE 7 /* ** CAPI3REF: Prepared Statement Scan Status ** METHOD: sqlite3_stmt ** -** This interface returns information about the predicted and measured +** These interfaces return information about the predicted and measured ** performance for pStmt. Advanced applications can use this ** interface to compare the predicted and the measured performance and ** issue warnings and/or rerun [ANALYZE] if discrepancies are found. @@ -7820,19 +10654,25 @@ int sqlite3_vtab_on_conflict(sqlite3 *); ** ** The "iScanStatusOp" parameter determines which status information to return. ** The "iScanStatusOp" must be one of the [scanstatus options] or the behavior -** of this interface is undefined. -** ^The requested measurement is written into a variable pointed to by -** the "pOut" parameter. -** Parameter "idx" identifies the specific loop to retrieve statistics for. -** Loops are numbered starting from zero. ^If idx is out of range - less than -** zero or greater than or equal to the total number of loops used to implement -** the statement - a non-zero value is returned and the variable that pOut -** points to is unchanged. -** -** ^Statistics might not be available for all loops in all statements. ^In cases -** where there exist loops with no available statistics, this function behaves -** as if the loop did not exist - it returns non-zero and leave the variable -** that pOut points to unchanged. +** of this interface is undefined. ^The requested measurement is written into +** a variable pointed to by the "pOut" parameter. +** +** The "flags" parameter must be passed a mask of flags. At present only +** one flag is defined - SQLITE_SCANSTAT_COMPLEX. If SQLITE_SCANSTAT_COMPLEX +** is specified, then status information is available for all elements +** of a query plan that are reported by "EXPLAIN QUERY PLAN" output. If +** SQLITE_SCANSTAT_COMPLEX is not specified, then only query plan elements +** that correspond to query loops (the "SCAN..." and "SEARCH..." elements of +** the EXPLAIN QUERY PLAN output) are available. Invoking API +** sqlite3_stmt_scanstatus() is equivalent to calling +** sqlite3_stmt_scanstatus_v2() with a zeroed flags parameter. +** +** Parameter "idx" identifies the specific query element to retrieve statistics +** for. Query elements are numbered starting from zero. A value of -1 may +** retrieve statistics for the entire query. ^If idx is out of range +** - less than -1 or greater than or equal to the total number of query +** elements used to implement the statement - a non-zero value is returned and +** the variable that pOut points to is unchanged. ** ** See also: [sqlite3_stmt_scanstatus_reset()] */ @@ -7841,7 +10681,20 @@ int sqlite3_stmt_scanstatus( int idx, /* Index of loop to report on */ int iScanStatusOp, /* Information desired. SQLITE_SCANSTAT_* */ void *pOut /* Result written here */ -); +); +int sqlite3_stmt_scanstatus_v2( + sqlite3_stmt *pStmt, /* Prepared statement for which info desired */ + int idx, /* Index of loop to report on */ + int iScanStatusOp, /* Information desired. SQLITE_SCANSTAT_* */ + int flags, /* Mask of flags defined below */ + void *pOut /* Result written here */ +); + +/* +** CAPI3REF: Prepared Statement Scan Status +** KEYWORDS: {scan status flags} +*/ +#define SQLITE_SCANSTAT_COMPLEX 0x0001 /* ** CAPI3REF: Zero Scan-Status Counters @@ -7856,18 +10709,19 @@ void sqlite3_stmt_scanstatus_reset(sqlite3_stmt*); /* ** CAPI3REF: Flush caches to disk mid-transaction +** METHOD: sqlite3 ** ** ^If a write-transaction is open on [database connection] D when the -** [sqlite3_db_cacheflush(D)] interface invoked, any dirty -** pages in the pager-cache that are not currently in use are written out +** [sqlite3_db_cacheflush(D)] interface is invoked, any dirty +** pages in the pager-cache that are not currently in use are written out ** to disk. A dirty page may be in use if a database cursor created by an ** active SQL statement is reading from it, or if it is page 1 of a database ** file (page 1 is always "in use"). ^The [sqlite3_db_cacheflush(D)] ** interface flushes caches for all schemas - "main", "temp", and ** any [attached] databases. ** -** ^If this function needs to obtain extra database locks before dirty pages -** can be flushed to disk, it does so. ^If those locks cannot be obtained +** ^If this function needs to obtain extra database locks before dirty pages +** can be flushed to disk, it does so. ^If those locks cannot be obtained ** immediately and there is a busy-handler callback configured, it is invoked ** in the usual manner. ^If the required lock still cannot be obtained, then ** the database is skipped and an attempt made to flush any dirty pages @@ -7886,10 +10740,139 @@ void sqlite3_stmt_scanstatus_reset(sqlite3_stmt*); */ int sqlite3_db_cacheflush(sqlite3*); +/* +** CAPI3REF: The pre-update hook. +** METHOD: sqlite3 +** +** ^These interfaces are only available if SQLite is compiled using the +** [SQLITE_ENABLE_PREUPDATE_HOOK] compile-time option. +** +** ^The [sqlite3_preupdate_hook()] interface registers a callback function +** that is invoked prior to each [INSERT], [UPDATE], and [DELETE] operation +** on a database table. +** ^At most one preupdate hook may be registered at a time on a single +** [database connection]; each call to [sqlite3_preupdate_hook()] overrides +** the previous setting. +** ^The preupdate hook is disabled by invoking [sqlite3_preupdate_hook()] +** with a NULL pointer as the second parameter. +** ^The third parameter to [sqlite3_preupdate_hook()] is passed through as +** the first parameter to callbacks. +** +** ^The preupdate hook only fires for changes to real database tables; the +** preupdate hook is not invoked for changes to [virtual tables] or to +** system tables like sqlite_sequence or sqlite_stat1. +** +** ^The second parameter to the preupdate callback is a pointer to +** the [database connection] that registered the preupdate hook. +** ^The third parameter to the preupdate callback is one of the constants +** [SQLITE_INSERT], [SQLITE_DELETE], or [SQLITE_UPDATE] to identify the +** kind of update operation that is about to occur. +** ^(The fourth parameter to the preupdate callback is the name of the +** database within the database connection that is being modified. This +** will be "main" for the main database or "temp" for TEMP tables or +** the name given after the AS keyword in the [ATTACH] statement for attached +** databases.)^ +** ^The fifth parameter to the preupdate callback is the name of the +** table that is being modified. +** +** For an UPDATE or DELETE operation on a [rowid table], the sixth +** parameter passed to the preupdate callback is the initial [rowid] of the +** row being modified or deleted. For an INSERT operation on a rowid table, +** or any operation on a WITHOUT ROWID table, the value of the sixth +** parameter is undefined. For an INSERT or UPDATE on a rowid table the +** seventh parameter is the final rowid value of the row being inserted +** or updated. The value of the seventh parameter passed to the callback +** function is not defined for operations on WITHOUT ROWID tables, or for +** DELETE operations on rowid tables. +** +** ^The sqlite3_preupdate_hook(D,C,P) function returns the P argument from +** the previous call on the same [database connection] D, or NULL for +** the first call on D. +** +** The [sqlite3_preupdate_old()], [sqlite3_preupdate_new()], +** [sqlite3_preupdate_count()], and [sqlite3_preupdate_depth()] interfaces +** provide additional information about a preupdate event. These routines +** may only be called from within a preupdate callback. Invoking any of +** these routines from outside of a preupdate callback or with a +** [database connection] pointer that is different from the one supplied +** to the preupdate callback results in undefined and probably undesirable +** behavior. +** +** ^The [sqlite3_preupdate_count(D)] interface returns the number of columns +** in the row that is being inserted, updated, or deleted. +** +** ^The [sqlite3_preupdate_old(D,N,P)] interface writes into P a pointer to +** a [protected sqlite3_value] that contains the value of the Nth column of +** the table row before it is updated. The N parameter must be between 0 +** and one less than the number of columns or the behavior will be +** undefined. This must only be used within SQLITE_UPDATE and SQLITE_DELETE +** preupdate callbacks; if it is used by an SQLITE_INSERT callback then the +** behavior is undefined. The [sqlite3_value] that P points to +** will be destroyed when the preupdate callback returns. +** +** ^The [sqlite3_preupdate_new(D,N,P)] interface writes into P a pointer to +** a [protected sqlite3_value] that contains the value of the Nth column of +** the table row after it is updated. The N parameter must be between 0 +** and one less than the number of columns or the behavior will be +** undefined. This must only be used within SQLITE_INSERT and SQLITE_UPDATE +** preupdate callbacks; if it is used by an SQLITE_DELETE callback then the +** behavior is undefined. The [sqlite3_value] that P points to +** will be destroyed when the preupdate callback returns. +** +** ^The [sqlite3_preupdate_depth(D)] interface returns 0 if the preupdate +** callback was invoked as a result of a direct insert, update, or delete +** operation; or 1 for inserts, updates, or deletes invoked by top-level +** triggers; or 2 for changes resulting from triggers called by top-level +** triggers; and so forth. +** +** When the [sqlite3_blob_write()] API is used to update a blob column, +** the pre-update hook is invoked with SQLITE_DELETE, because +** the new values are not yet available. In this case, when a +** callback made with op==SQLITE_DELETE is actually a write using the +** sqlite3_blob_write() API, the [sqlite3_preupdate_blobwrite()] returns +** the index of the column being written. In other cases, where the +** pre-update hook is being invoked for some other reason, including a +** regular DELETE, sqlite3_preupdate_blobwrite() returns -1. +** +** See also: [sqlite3_update_hook()] +*/ +#if defined(SQLITE_ENABLE_PREUPDATE_HOOK) +void *sqlite3_preupdate_hook( + sqlite3 *db, + void(*xPreUpdate)( + void *pCtx, /* Copy of third arg to preupdate_hook() */ + sqlite3 *db, /* Database handle */ + int op, /* SQLITE_UPDATE, DELETE or INSERT */ + char const *zDb, /* Database name */ + char const *zName, /* Table name */ + sqlite3_int64 iKey1, /* Rowid of row about to be deleted/updated */ + sqlite3_int64 iKey2 /* New rowid value (for a rowid UPDATE) */ + ), + void* +); +int sqlite3_preupdate_old(sqlite3 *, int, sqlite3_value **); +int sqlite3_preupdate_count(sqlite3 *); +int sqlite3_preupdate_depth(sqlite3 *); +int sqlite3_preupdate_new(sqlite3 *, int, sqlite3_value **); +int sqlite3_preupdate_blobwrite(sqlite3 *); +#endif + +/* +** CAPI3REF: Low-level system error code +** METHOD: sqlite3 +** +** ^Attempt to return the underlying operating system error code or error +** number that caused the most recent I/O error or failure to open a file. +** The return value is OS-dependent. For example, on unix systems, after +** [sqlite3_open_v2()] returns [SQLITE_CANTOPEN], this interface could be +** called to get back the underlying "errno" that caused the problem, such +** as ENOSPC, EAUTH, EISDIR, and so forth. +*/ +int sqlite3_system_errno(sqlite3*); + /* ** CAPI3REF: Database Snapshot -** KEYWORDS: {snapshot} -** EXPERIMENTAL +** KEYWORDS: {snapshot} {sqlite3_snapshot} ** ** An instance of the snapshot object records the state of a [WAL mode] ** database for some specific point in history. @@ -7906,35 +10889,63 @@ int sqlite3_db_cacheflush(sqlite3*); ** version of the database file so that it is possible to later open a new read ** transaction that sees that historical version of the database rather than ** the most recent version. -** -** The constructor for this object is [sqlite3_snapshot_get()]. The -** [sqlite3_snapshot_open()] method causes a fresh read transaction to refer -** to an historical snapshot (if possible). The destructor for -** sqlite3_snapshot objects is [sqlite3_snapshot_free()]. */ -typedef struct sqlite3_snapshot sqlite3_snapshot; +typedef struct sqlite3_snapshot { + unsigned char hidden[48]; +} sqlite3_snapshot; /* ** CAPI3REF: Record A Database Snapshot -** EXPERIMENTAL +** CONSTRUCTOR: sqlite3_snapshot ** ** ^The [sqlite3_snapshot_get(D,S,P)] interface attempts to make a ** new [sqlite3_snapshot] object that records the current state of ** schema S in database connection D. ^On success, the ** [sqlite3_snapshot_get(D,S,P)] interface writes a pointer to the newly ** created [sqlite3_snapshot] object into *P and returns SQLITE_OK. -** ^If schema S of [database connection] D is not a [WAL mode] database -** that is in a read transaction, then [sqlite3_snapshot_get(D,S,P)] -** leaves the *P value unchanged and returns an appropriate [error code]. +** If there is not already a read-transaction open on schema S when +** this function is called, one is opened automatically. +** +** If a read-transaction is opened by this function, then it is guaranteed +** that the returned snapshot object may not be invalidated by a database +** writer or checkpointer until after the read-transaction is closed. This +** is not guaranteed if a read-transaction is already open when this +** function is called. In that case, any subsequent write or checkpoint +** operation on the database may invalidate the returned snapshot handle, +** even while the read-transaction remains open. +** +** The following must be true for this function to succeed. If any of +** the following statements are false when sqlite3_snapshot_get() is +** called, SQLITE_ERROR is returned. The final value of *P is undefined +** in this case. +** +** <ul> +** <li> The database handle must not be in [autocommit mode]. +** +** <li> Schema S of [database connection] D must be a [WAL mode] database. +** +** <li> There must not be a write transaction open on schema S of database +** connection D. +** +** <li> One or more transactions must have been written to the current wal +** file since it was created on disk (by any connection). This means +** that a snapshot cannot be taken on a wal mode database with no wal +** file immediately after it is first opened. At least one transaction +** must be written to it first. +** </ul> +** +** This function may also return SQLITE_NOMEM. If it is called with the +** database handle in autocommit mode but fails for some other reason, +** whether or not a read transaction is opened on schema S is undefined. ** ** The [sqlite3_snapshot] object returned from a successful call to ** [sqlite3_snapshot_get()] must be freed using [sqlite3_snapshot_free()] ** to avoid a memory leak. ** ** The [sqlite3_snapshot_get()] interface is only available when the -** SQLITE_ENABLE_SNAPSHOT compile-time option is used. +** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used. */ -SQLITE_EXPERIMENTAL int sqlite3_snapshot_get( +int sqlite3_snapshot_get( sqlite3 *db, const char *zSchema, sqlite3_snapshot **ppSnapshot @@ -7942,24 +10953,48 @@ SQLITE_EXPERIMENTAL int sqlite3_snapshot_get( /* ** CAPI3REF: Start a read transaction on an historical snapshot -** EXPERIMENTAL -** -** ^The [sqlite3_snapshot_open(D,S,P)] interface attempts to move the -** read transaction that is currently open on schema S of -** [database connection] D so that it refers to historical [snapshot] P. -** ^The [sqlite3_snapshot_open()] interface returns SQLITE_OK on success -** or an appropriate [error code] if it fails. -** -** ^In order to succeed, a call to [sqlite3_snapshot_open(D,S,P)] must be -** the first operation, apart from other sqlite3_snapshot_open() calls, -** following the [BEGIN] that starts a new read transaction. -** ^A [snapshot] will fail to open if it has been overwritten by a -** [checkpoint]. +** METHOD: sqlite3_snapshot +** +** ^The [sqlite3_snapshot_open(D,S,P)] interface either starts a new read +** transaction or upgrades an existing one for schema S of +** [database connection] D such that the read transaction refers to +** historical [snapshot] P, rather than the most recent change to the +** database. ^The [sqlite3_snapshot_open()] interface returns SQLITE_OK +** on success or an appropriate [error code] if it fails. +** +** ^In order to succeed, the database connection must not be in +** [autocommit mode] when [sqlite3_snapshot_open(D,S,P)] is called. If there +** is already a read transaction open on schema S, then the database handle +** must have no active statements (SELECT statements that have been passed +** to sqlite3_step() but not sqlite3_reset() or sqlite3_finalize()). +** SQLITE_ERROR is returned if either of these conditions is violated, or +** if schema S does not exist, or if the snapshot object is invalid. +** +** ^A call to sqlite3_snapshot_open() will fail to open if the specified +** snapshot has been overwritten by a [checkpoint]. In this case +** SQLITE_ERROR_SNAPSHOT is returned. +** +** If there is already a read transaction open when this function is +** invoked, then the same read transaction remains open (on the same +** database snapshot) if SQLITE_ERROR, SQLITE_BUSY or SQLITE_ERROR_SNAPSHOT +** is returned. If another error code - for example SQLITE_PROTOCOL or an +** SQLITE_IOERR error code - is returned, then the final state of the +** read transaction is undefined. If SQLITE_OK is returned, then the +** read transaction is now open on database snapshot P. +** +** ^(A call to [sqlite3_snapshot_open(D,S,P)] will fail if the +** database connection D does not know that the database file for +** schema S is in [WAL mode]. A database connection might not know +** that the database file is in [WAL mode] if there has been no prior +** I/O on that database connection, or if the database entered [WAL mode] +** after the most recent I/O on the database connection.)^ +** (Hint: Run "[PRAGMA application_id]" against a newly opened +** database connection in order to make it ready to use snapshots.) ** ** The [sqlite3_snapshot_open()] interface is only available when the -** SQLITE_ENABLE_SNAPSHOT compile-time option is used. +** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used. */ -SQLITE_EXPERIMENTAL int sqlite3_snapshot_open( +int sqlite3_snapshot_open( sqlite3 *db, const char *zSchema, sqlite3_snapshot *pSnapshot @@ -7967,16 +11002,264 @@ SQLITE_EXPERIMENTAL int sqlite3_snapshot_open( /* ** CAPI3REF: Destroy a snapshot -** EXPERIMENTAL +** DESTRUCTOR: sqlite3_snapshot ** ** ^The [sqlite3_snapshot_free(P)] interface destroys [sqlite3_snapshot] P. ** The application must eventually free every [sqlite3_snapshot] object ** using this routine to avoid a memory leak. ** ** The [sqlite3_snapshot_free()] interface is only available when the -** SQLITE_ENABLE_SNAPSHOT compile-time option is used. +** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used. +*/ +void sqlite3_snapshot_free(sqlite3_snapshot*); + +/* +** CAPI3REF: Compare the ages of two snapshot handles. +** METHOD: sqlite3_snapshot +** +** The sqlite3_snapshot_cmp(P1, P2) interface is used to compare the ages +** of two valid snapshot handles. +** +** If the two snapshot handles are not associated with the same database +** file, the result of the comparison is undefined. +** +** Additionally, the result of the comparison is only valid if both of the +** snapshot handles were obtained by calling sqlite3_snapshot_get() since the +** last time the wal file was deleted. The wal file is deleted when the +** database is changed back to rollback mode or when the number of database +** clients drops to zero. If either snapshot handle was obtained before the +** wal file was last deleted, the value returned by this function +** is undefined. +** +** Otherwise, this API returns a negative value if P1 refers to an older +** snapshot than P2, zero if the two handles refer to the same database +** snapshot, and a positive value if P1 is a newer snapshot than P2. +** +** This interface is only available if SQLite is compiled with the +** [SQLITE_ENABLE_SNAPSHOT] option. +*/ +int sqlite3_snapshot_cmp( + sqlite3_snapshot *p1, + sqlite3_snapshot *p2 +); + +/* +** CAPI3REF: Recover snapshots from a wal file +** METHOD: sqlite3_snapshot +** +** If a [WAL file] remains on disk after all database connections close +** (either through the use of the [SQLITE_FCNTL_PERSIST_WAL] [file control] +** or because the last process to have the database opened exited without +** calling [sqlite3_close()]) and a new connection is subsequently opened +** on that database and [WAL file], the [sqlite3_snapshot_open()] interface +** will only be able to open the last transaction added to the WAL file +** even though the WAL file contains other valid transactions. +** +** This function attempts to scan the WAL file associated with database zDb +** of database handle db and make all valid snapshots available to +** sqlite3_snapshot_open(). It is an error if there is already a read +** transaction open on the database, or if the database is not a WAL mode +** database. +** +** SQLITE_OK is returned if successful, or an SQLite error code otherwise. +** +** This interface is only available if SQLite is compiled with the +** [SQLITE_ENABLE_SNAPSHOT] option. +*/ +int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb); + +/* +** CAPI3REF: Serialize a database +** +** The sqlite3_serialize(D,S,P,F) interface returns a pointer to +** memory that is a serialization of the S database on +** [database connection] D. If S is a NULL pointer, the main database is used. +** If P is not a NULL pointer, then the size of the database in bytes +** is written into *P. +** +** For an ordinary on-disk database file, the serialization is just a +** copy of the disk file. For an in-memory database or a "TEMP" database, +** the serialization is the same sequence of bytes which would be written +** to disk if that database were backed up to disk. +** +** The usual case is that sqlite3_serialize() copies the serialization of +** the database into memory obtained from [sqlite3_malloc64()] and returns +** a pointer to that memory. The caller is responsible for freeing the +** returned value to avoid a memory leak. However, if the F argument +** contains the SQLITE_SERIALIZE_NOCOPY bit, then no memory allocations +** are made, and the sqlite3_serialize() function will return a pointer +** to the contiguous memory representation of the database that SQLite +** is currently using for that database, or NULL if no such contiguous +** memory representation of the database exists. A contiguous memory +** representation of the database will usually only exist if there has +** been a prior call to [sqlite3_deserialize(D,S,...)] with the same +** values of D and S. +** The size of the database is written into *P even if the +** SQLITE_SERIALIZE_NOCOPY bit is set but no contiguous copy +** of the database exists. +** +** After the call, if the SQLITE_SERIALIZE_NOCOPY bit had been set, +** the returned buffer content will remain accessible and unchanged +** until either the next write operation on the connection or when +** the connection is closed, and applications must not modify the +** buffer. If the bit had been clear, the returned buffer will not +** be accessed by SQLite after the call. +** +** A call to sqlite3_serialize(D,S,P,F) might return NULL even if the +** SQLITE_SERIALIZE_NOCOPY bit is omitted from argument F if a memory +** allocation error occurs. +** +** This interface is omitted if SQLite is compiled with the +** [SQLITE_OMIT_DESERIALIZE] option. +*/ +unsigned char *sqlite3_serialize( + sqlite3 *db, /* The database connection */ + const char *zSchema, /* Which DB to serialize. ex: "main", "temp", ... */ + sqlite3_int64 *piSize, /* Write size of the DB here, if not NULL */ + unsigned int mFlags /* Zero or more SQLITE_SERIALIZE_* flags */ +); + +/* +** CAPI3REF: Flags for sqlite3_serialize +** +** Zero or more of the following constants can be OR-ed together for +** the F argument to [sqlite3_serialize(D,S,P,F)]. +** +** SQLITE_SERIALIZE_NOCOPY means that [sqlite3_serialize()] will return +** a pointer to contiguous in-memory database that it is currently using, +** without making a copy of the database. If SQLite is not currently using +** a contiguous in-memory database, then this option causes +** [sqlite3_serialize()] to return a NULL pointer. SQLite will only be +** using a contiguous in-memory database if it has been initialized by a +** prior call to [sqlite3_deserialize()]. */ -SQLITE_EXPERIMENTAL void sqlite3_snapshot_free(sqlite3_snapshot*); +#define SQLITE_SERIALIZE_NOCOPY 0x001 /* Do no memory allocations */ + +/* +** CAPI3REF: Deserialize a database +** +** The sqlite3_deserialize(D,S,P,N,M,F) interface causes the +** [database connection] D to disconnect from database S and then +** reopen S as an in-memory database based on the serialization +** contained in P. If S is a NULL pointer, the main database is +** used. The serialized database P is N bytes in size. M is the size +** of the buffer P, which might be larger than N. If M is larger than +** N, and the SQLITE_DESERIALIZE_READONLY bit is not set in F, then +** SQLite is permitted to add content to the in-memory database as +** long as the total size does not exceed M bytes. +** +** If the SQLITE_DESERIALIZE_FREEONCLOSE bit is set in F, then SQLite will +** invoke sqlite3_free() on the serialization buffer when the database +** connection closes. If the SQLITE_DESERIALIZE_RESIZEABLE bit is set, then +** SQLite will try to increase the buffer size using sqlite3_realloc64() +** if writes on the database cause it to grow larger than M bytes. +** +** Applications must not modify the buffer P or invalidate it before +** the database connection D is closed. +** +** The sqlite3_deserialize() interface will fail with SQLITE_BUSY if the +** database is currently in a read transaction or is involved in a backup +** operation. +** +** It is not possible to deserialize into the TEMP database. If the +** S argument to sqlite3_deserialize(D,S,P,N,M,F) is "temp" then the +** function returns SQLITE_ERROR. +** +** The deserialized database should not be in [WAL mode]. If the database +** is in WAL mode, then any attempt to use the database file will result +** in an [SQLITE_CANTOPEN] error. The application can set the +** [file format version numbers] (bytes 18 and 19) of the input database P +** to 0x01 prior to invoking sqlite3_deserialize(D,S,P,N,M,F) to force the +** database file into rollback mode and work around this limitation. +** +** If sqlite3_deserialize(D,S,P,N,M,F) fails for any reason and if the +** SQLITE_DESERIALIZE_FREEONCLOSE bit is set in argument F, then +** [sqlite3_free()] is invoked on argument P prior to returning. +** +** This interface is omitted if SQLite is compiled with the +** [SQLITE_OMIT_DESERIALIZE] option. +*/ +int sqlite3_deserialize( + sqlite3 *db, /* The database connection */ + const char *zSchema, /* Which DB to reopen with the deserialization */ + unsigned char *pData, /* The serialized database content */ + sqlite3_int64 szDb, /* Number of bytes in the deserialization */ + sqlite3_int64 szBuf, /* Total size of buffer pData[] */ + unsigned mFlags /* Zero or more SQLITE_DESERIALIZE_* flags */ +); + +/* +** CAPI3REF: Flags for sqlite3_deserialize() +** +** The following are allowed values for the 6th argument (the F argument) to +** the [sqlite3_deserialize(D,S,P,N,M,F)] interface. +** +** The SQLITE_DESERIALIZE_FREEONCLOSE means that the database serialization +** in the P argument is held in memory obtained from [sqlite3_malloc64()] +** and that SQLite should take ownership of this memory and automatically +** free it when it has finished using it. Without this flag, the caller +** is responsible for freeing any dynamically allocated memory. +** +** The SQLITE_DESERIALIZE_RESIZEABLE flag means that SQLite is allowed to +** grow the size of the database using calls to [sqlite3_realloc64()]. This +** flag should only be used if SQLITE_DESERIALIZE_FREEONCLOSE is also used. +** Without this flag, the deserialized database cannot increase in size beyond +** the number of bytes specified by the M parameter. +** +** The SQLITE_DESERIALIZE_READONLY flag means that the deserialized database +** should be treated as read-only. +*/ +#define SQLITE_DESERIALIZE_FREEONCLOSE 1 /* Call sqlite3_free() on close */ +#define SQLITE_DESERIALIZE_RESIZEABLE 2 /* Resize using sqlite3_realloc64() */ +#define SQLITE_DESERIALIZE_READONLY 4 /* Database is read-only */ + +/* +** CAPI3REF: Bind array values to the CARRAY table-valued function +** +** The sqlite3_carray_bind(S,I,P,N,F,X) interface binds an array value to +** one of the first argument of the [carray() table-valued function]. The +** S parameter is a pointer to the [prepared statement] that uses the carray() +** functions. I is the parameter index to be bound. P is a pointer to the +** array to be bound, and N is the number of eements in the array. The +** F argument is one of constants [SQLITE_CARRAY_INT32], [SQLITE_CARRAY_INT64], +** [SQLITE_CARRAY_DOUBLE], [SQLITE_CARRAY_TEXT], or [SQLITE_CARRAY_BLOB] to +** indicate the datatype of the array being bound. The X argument is not a +** NULL pointer, then SQLite will invoke the function X on the P parameter +** after it has finished using P, even if the call to +** sqlite3_carray_bind() fails. The special-case finalizer +** SQLITE_TRANSIENT has no effect here. +*/ +int sqlite3_carray_bind( + sqlite3_stmt *pStmt, /* Statement to be bound */ + int i, /* Parameter index */ + void *aData, /* Pointer to array data */ + int nData, /* Number of data elements */ + int mFlags, /* CARRAY flags */ + void (*xDel)(void*) /* Destructor for aData */ +); + +/* +** CAPI3REF: Datatypes for the CARRAY table-valued function +** +** The fifth argument to the [sqlite3_carray_bind()] interface musts be +** one of the following constants, to specify the datatype of the array +** that is being bound into the [carray table-valued function]. +*/ +#define SQLITE_CARRAY_INT32 0 /* Data is 32-bit signed integers */ +#define SQLITE_CARRAY_INT64 1 /* Data is 64-bit signed integers */ +#define SQLITE_CARRAY_DOUBLE 2 /* Data is doubles */ +#define SQLITE_CARRAY_TEXT 3 /* Data is char* */ +#define SQLITE_CARRAY_BLOB 4 /* Data is struct iovec */ + +/* +** Versions of the above #defines that omit the initial SQLITE_, for +** legacy compatibility. +*/ +#define CARRAY_INT32 0 /* Data is 32-bit signed integers */ +#define CARRAY_INT64 1 /* Data is 64-bit signed integers */ +#define CARRAY_DOUBLE 2 /* Data is doubles */ +#define CARRAY_TEXT 3 /* Data is char* */ +#define CARRAY_BLOB 4 /* Data is struct iovec */ /* ** Undo the hack that converts floating point types to integer for @@ -7986,7 +11269,18 @@ SQLITE_EXPERIMENTAL void sqlite3_snapshot_free(sqlite3_snapshot*); # undef double #endif +#if defined(__wasi__) +# undef SQLITE_WASI +# define SQLITE_WASI 1 +# ifndef SQLITE_OMIT_LOAD_EXTENSION +# define SQLITE_OMIT_LOAD_EXTENSION +# endif +# ifndef SQLITE_THREADSAFE +# define SQLITE_THREADSAFE 0 +# endif +#endif + #ifdef __cplusplus } /* End of the 'extern "C"' block */ #endif -#endif /* _SQLITE3_H_ */ +/* #endif for SQLITE3_H will be added by mksqlite3.tcl */ diff --git a/src/sqlite3.rc b/src/sqlite3.rc index 5a856490d6..aad468d349 100644 --- a/src/sqlite3.rc +++ b/src/sqlite3.rc @@ -70,7 +70,7 @@ BEGIN VALUE "FileDescription", "SQLite is a software library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine." VALUE "FileVersion", SQLITE_VERSION VALUE "InternalName", "sqlite3" - VALUE "LegalCopyright", "http://www.sqlite.org/copyright.html" + VALUE "LegalCopyright", "http://sqlite.org/copyright.html" VALUE "ProductName", "SQLite" VALUE "ProductVersion", SQLITE_VERSION VALUE "SourceId", SQLITE_SOURCE_ID diff --git a/src/sqlite3ext.h b/src/sqlite3ext.h index 2e1c764a52..5258faaed3 100644 --- a/src/sqlite3ext.h +++ b/src/sqlite3ext.h @@ -15,12 +15,10 @@ ** as extensions by SQLite should #include this file instead of ** sqlite3.h. */ -#ifndef _SQLITE3EXT_H_ -#define _SQLITE3EXT_H_ +#ifndef SQLITE3EXT_H +#define SQLITE3EXT_H #include "sqlite3.h" -typedef struct sqlite3_api_routines sqlite3_api_routines; - /* ** The following structure holds pointers to all of the SQLite API ** routines. @@ -136,7 +134,7 @@ struct sqlite3_api_routines { int (*set_authorizer)(sqlite3*,int(*)(void*,int,const char*,const char*, const char*,const char*),void*); void (*set_auxdata)(sqlite3_context*,int,void*,void (*)(void*)); - char * (*snprintf)(int,char*,const char*,...); + char * (*xsnprintf)(int,char*,const char*,...); int (*step)(sqlite3_stmt*); int (*table_column_metadata)(sqlite3*,const char*,const char*,const char*, char const**,char const**,int*,int*,int*); @@ -248,7 +246,7 @@ struct sqlite3_api_routines { int (*uri_boolean)(const char*,const char*,int); sqlite3_int64 (*uri_int64)(const char*,const char*,sqlite3_int64); const char *(*uri_parameter)(const char*,const char*); - char *(*vsnprintf)(int,char*,const char*,va_list); + char *(*xvsnprintf)(int,char*,const char*,va_list); int (*wal_checkpoint_v2)(sqlite3*,const char*,int,int*,int*); /* Version 3.8.7 and later */ int (*auto_extension)(void(*)(void)); @@ -279,8 +277,113 @@ struct sqlite3_api_routines { int (*status64)(int,sqlite3_int64*,sqlite3_int64*,int); int (*strlike)(const char*,const char*,unsigned int); int (*db_cacheflush)(sqlite3*); + /* Version 3.12.0 and later */ + int (*system_errno)(sqlite3*); + /* Version 3.14.0 and later */ + int (*trace_v2)(sqlite3*,unsigned,int(*)(unsigned,void*,void*,void*),void*); + char *(*expanded_sql)(sqlite3_stmt*); + /* Version 3.18.0 and later */ + void (*set_last_insert_rowid)(sqlite3*,sqlite3_int64); + /* Version 3.20.0 and later */ + int (*prepare_v3)(sqlite3*,const char*,int,unsigned int, + sqlite3_stmt**,const char**); + int (*prepare16_v3)(sqlite3*,const void*,int,unsigned int, + sqlite3_stmt**,const void**); + int (*bind_pointer)(sqlite3_stmt*,int,void*,const char*,void(*)(void*)); + void (*result_pointer)(sqlite3_context*,void*,const char*,void(*)(void*)); + void *(*value_pointer)(sqlite3_value*,const char*); + int (*vtab_nochange)(sqlite3_context*); + int (*value_nochange)(sqlite3_value*); + const char *(*vtab_collation)(sqlite3_index_info*,int); + /* Version 3.24.0 and later */ + int (*keyword_count)(void); + int (*keyword_name)(int,const char**,int*); + int (*keyword_check)(const char*,int); + sqlite3_str *(*str_new)(sqlite3*); + char *(*str_finish)(sqlite3_str*); + void (*str_appendf)(sqlite3_str*, const char *zFormat, ...); + void (*str_vappendf)(sqlite3_str*, const char *zFormat, va_list); + void (*str_append)(sqlite3_str*, const char *zIn, int N); + void (*str_appendall)(sqlite3_str*, const char *zIn); + void (*str_appendchar)(sqlite3_str*, int N, char C); + void (*str_reset)(sqlite3_str*); + int (*str_errcode)(sqlite3_str*); + int (*str_length)(sqlite3_str*); + char *(*str_value)(sqlite3_str*); + /* Version 3.25.0 and later */ + int (*create_window_function)(sqlite3*,const char*,int,int,void*, + void (*xStep)(sqlite3_context*,int,sqlite3_value**), + void (*xFinal)(sqlite3_context*), + void (*xValue)(sqlite3_context*), + void (*xInv)(sqlite3_context*,int,sqlite3_value**), + void(*xDestroy)(void*)); + /* Version 3.26.0 and later */ + const char *(*normalized_sql)(sqlite3_stmt*); + /* Version 3.28.0 and later */ + int (*stmt_isexplain)(sqlite3_stmt*); + int (*value_frombind)(sqlite3_value*); + /* Version 3.30.0 and later */ + int (*drop_modules)(sqlite3*,const char**); + /* Version 3.31.0 and later */ + sqlite3_int64 (*hard_heap_limit64)(sqlite3_int64); + const char *(*uri_key)(const char*,int); + const char *(*filename_database)(const char*); + const char *(*filename_journal)(const char*); + const char *(*filename_wal)(const char*); + /* Version 3.32.0 and later */ + const char *(*create_filename)(const char*,const char*,const char*, + int,const char**); + void (*free_filename)(const char*); + sqlite3_file *(*database_file_object)(const char*); + /* Version 3.34.0 and later */ + int (*txn_state)(sqlite3*,const char*); + /* Version 3.36.1 and later */ + sqlite3_int64 (*changes64)(sqlite3*); + sqlite3_int64 (*total_changes64)(sqlite3*); + /* Version 3.37.0 and later */ + int (*autovacuum_pages)(sqlite3*, + unsigned int(*)(void*,const char*,unsigned int,unsigned int,unsigned int), + void*, void(*)(void*)); + /* Version 3.38.0 and later */ + int (*error_offset)(sqlite3*); + int (*vtab_rhs_value)(sqlite3_index_info*,int,sqlite3_value**); + int (*vtab_distinct)(sqlite3_index_info*); + int (*vtab_in)(sqlite3_index_info*,int,int); + int (*vtab_in_first)(sqlite3_value*,sqlite3_value**); + int (*vtab_in_next)(sqlite3_value*,sqlite3_value**); + /* Version 3.39.0 and later */ + int (*deserialize)(sqlite3*,const char*,unsigned char*, + sqlite3_int64,sqlite3_int64,unsigned); + unsigned char *(*serialize)(sqlite3*,const char *,sqlite3_int64*, + unsigned int); + const char *(*db_name)(sqlite3*,int); + /* Version 3.40.0 and later */ + int (*value_encoding)(sqlite3_value*); + /* Version 3.41.0 and later */ + int (*is_interrupted)(sqlite3*); + /* Version 3.43.0 and later */ + int (*stmt_explain)(sqlite3_stmt*,int); + /* Version 3.44.0 and later */ + void *(*get_clientdata)(sqlite3*,const char*); + int (*set_clientdata)(sqlite3*, const char*, void*, void(*)(void*)); + /* Version 3.50.0 and later */ + int (*setlk_timeout)(sqlite3*,int,int); + /* Version 3.51.0 and later */ + int (*set_errmsg)(sqlite3*,int,const char*); + int (*db_status64)(sqlite3*,int,sqlite3_int64*,sqlite3_int64*,int); + }; +/* +** This is the function signature used for all extension entry points. It +** is also defined in the file "loadext.c". +*/ +typedef int (*sqlite3_loadext_entry)( + sqlite3 *db, /* Handle to the database. */ + char **pzErrMsg, /* Used to set error string on failure. */ + const sqlite3_api_routines *pThunk /* Extension API function pointers. */ +); + /* ** The following macros redefine the API routines so that they are ** redirected through the global sqlite3_api structure. @@ -395,7 +498,7 @@ struct sqlite3_api_routines { #define sqlite3_rollback_hook sqlite3_api->rollback_hook #define sqlite3_set_authorizer sqlite3_api->set_authorizer #define sqlite3_set_auxdata sqlite3_api->set_auxdata -#define sqlite3_snprintf sqlite3_api->snprintf +#define sqlite3_snprintf sqlite3_api->xsnprintf #define sqlite3_step sqlite3_api->step #define sqlite3_table_column_metadata sqlite3_api->table_column_metadata #define sqlite3_thread_cleanup sqlite3_api->thread_cleanup @@ -419,7 +522,7 @@ struct sqlite3_api_routines { #define sqlite3_value_text16le sqlite3_api->value_text16le #define sqlite3_value_type sqlite3_api->value_type #define sqlite3_vmprintf sqlite3_api->vmprintf -#define sqlite3_vsnprintf sqlite3_api->vsnprintf +#define sqlite3_vsnprintf sqlite3_api->xvsnprintf #define sqlite3_overload_function sqlite3_api->overload_function #define sqlite3_prepare_v2 sqlite3_api->prepare_v2 #define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2 @@ -495,7 +598,7 @@ struct sqlite3_api_routines { #define sqlite3_uri_boolean sqlite3_api->uri_boolean #define sqlite3_uri_int64 sqlite3_api->uri_int64 #define sqlite3_uri_parameter sqlite3_api->uri_parameter -#define sqlite3_uri_vsnprintf sqlite3_api->vsnprintf +#define sqlite3_uri_vsnprintf sqlite3_api->xvsnprintf #define sqlite3_wal_checkpoint_v2 sqlite3_api->wal_checkpoint_v2 /* Version 3.8.7 and later */ #define sqlite3_auto_extension sqlite3_api->auto_extension @@ -522,6 +625,91 @@ struct sqlite3_api_routines { #define sqlite3_status64 sqlite3_api->status64 #define sqlite3_strlike sqlite3_api->strlike #define sqlite3_db_cacheflush sqlite3_api->db_cacheflush +/* Version 3.12.0 and later */ +#define sqlite3_system_errno sqlite3_api->system_errno +/* Version 3.14.0 and later */ +#define sqlite3_trace_v2 sqlite3_api->trace_v2 +#define sqlite3_expanded_sql sqlite3_api->expanded_sql +/* Version 3.18.0 and later */ +#define sqlite3_set_last_insert_rowid sqlite3_api->set_last_insert_rowid +/* Version 3.20.0 and later */ +#define sqlite3_prepare_v3 sqlite3_api->prepare_v3 +#define sqlite3_prepare16_v3 sqlite3_api->prepare16_v3 +#define sqlite3_bind_pointer sqlite3_api->bind_pointer +#define sqlite3_result_pointer sqlite3_api->result_pointer +#define sqlite3_value_pointer sqlite3_api->value_pointer +/* Version 3.22.0 and later */ +#define sqlite3_vtab_nochange sqlite3_api->vtab_nochange +#define sqlite3_value_nochange sqlite3_api->value_nochange +#define sqlite3_vtab_collation sqlite3_api->vtab_collation +/* Version 3.24.0 and later */ +#define sqlite3_keyword_count sqlite3_api->keyword_count +#define sqlite3_keyword_name sqlite3_api->keyword_name +#define sqlite3_keyword_check sqlite3_api->keyword_check +#define sqlite3_str_new sqlite3_api->str_new +#define sqlite3_str_finish sqlite3_api->str_finish +#define sqlite3_str_appendf sqlite3_api->str_appendf +#define sqlite3_str_vappendf sqlite3_api->str_vappendf +#define sqlite3_str_append sqlite3_api->str_append +#define sqlite3_str_appendall sqlite3_api->str_appendall +#define sqlite3_str_appendchar sqlite3_api->str_appendchar +#define sqlite3_str_reset sqlite3_api->str_reset +#define sqlite3_str_errcode sqlite3_api->str_errcode +#define sqlite3_str_length sqlite3_api->str_length +#define sqlite3_str_value sqlite3_api->str_value +/* Version 3.25.0 and later */ +#define sqlite3_create_window_function sqlite3_api->create_window_function +/* Version 3.26.0 and later */ +#define sqlite3_normalized_sql sqlite3_api->normalized_sql +/* Version 3.28.0 and later */ +#define sqlite3_stmt_isexplain sqlite3_api->stmt_isexplain +#define sqlite3_value_frombind sqlite3_api->value_frombind +/* Version 3.30.0 and later */ +#define sqlite3_drop_modules sqlite3_api->drop_modules +/* Version 3.31.0 and later */ +#define sqlite3_hard_heap_limit64 sqlite3_api->hard_heap_limit64 +#define sqlite3_uri_key sqlite3_api->uri_key +#define sqlite3_filename_database sqlite3_api->filename_database +#define sqlite3_filename_journal sqlite3_api->filename_journal +#define sqlite3_filename_wal sqlite3_api->filename_wal +/* Version 3.32.0 and later */ +#define sqlite3_create_filename sqlite3_api->create_filename +#define sqlite3_free_filename sqlite3_api->free_filename +#define sqlite3_database_file_object sqlite3_api->database_file_object +/* Version 3.34.0 and later */ +#define sqlite3_txn_state sqlite3_api->txn_state +/* Version 3.36.1 and later */ +#define sqlite3_changes64 sqlite3_api->changes64 +#define sqlite3_total_changes64 sqlite3_api->total_changes64 +/* Version 3.37.0 and later */ +#define sqlite3_autovacuum_pages sqlite3_api->autovacuum_pages +/* Version 3.38.0 and later */ +#define sqlite3_error_offset sqlite3_api->error_offset +#define sqlite3_vtab_rhs_value sqlite3_api->vtab_rhs_value +#define sqlite3_vtab_distinct sqlite3_api->vtab_distinct +#define sqlite3_vtab_in sqlite3_api->vtab_in +#define sqlite3_vtab_in_first sqlite3_api->vtab_in_first +#define sqlite3_vtab_in_next sqlite3_api->vtab_in_next +/* Version 3.39.0 and later */ +#ifndef SQLITE_OMIT_DESERIALIZE +#define sqlite3_deserialize sqlite3_api->deserialize +#define sqlite3_serialize sqlite3_api->serialize +#endif +#define sqlite3_db_name sqlite3_api->db_name +/* Version 3.40.0 and later */ +#define sqlite3_value_encoding sqlite3_api->value_encoding +/* Version 3.41.0 and later */ +#define sqlite3_is_interrupted sqlite3_api->is_interrupted +/* Version 3.43.0 and later */ +#define sqlite3_stmt_explain sqlite3_api->stmt_explain +/* Version 3.44.0 and later */ +#define sqlite3_get_clientdata sqlite3_api->get_clientdata +#define sqlite3_set_clientdata sqlite3_api->set_clientdata +/* Version 3.50.0 and later */ +#define sqlite3_setlk_timeout sqlite3_api->setlk_timeout +/* Version 3.51.0 and later */ +#define sqlite3_set_errmsg sqlite3_api->set_errmsg +#define sqlite3_db_status64 sqlite3_api->db_status64 #endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */ #if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) @@ -539,4 +727,4 @@ struct sqlite3_api_routines { # define SQLITE_EXTENSION_INIT3 /*no-op*/ #endif -#endif /* _SQLITE3EXT_H_ */ +#endif /* SQLITE3EXT_H */ diff --git a/src/sqliteInt.h b/src/sqliteInt.h index f04aebe79a..9bcc903e23 100644 --- a/src/sqliteInt.h +++ b/src/sqliteInt.h @@ -12,8 +12,43 @@ ** Internal interface definitions for SQLite. ** */ -#ifndef _SQLITEINT_H_ -#define _SQLITEINT_H_ +#ifndef SQLITEINT_H +#define SQLITEINT_H + +/* Special Comments: +** +** Some comments have special meaning to the tools that measure test +** coverage: +** +** NO_TEST - The branches on this line are not +** measured by branch coverage. This is +** used on lines of code that actually +** implement parts of coverage testing. +** +** OPTIMIZATION-IF-TRUE - This branch is allowed to always be false +** and the correct answer is still obtained, +** though perhaps more slowly. +** +** OPTIMIZATION-IF-FALSE - This branch is allowed to always be true +** and the correct answer is still obtained, +** though perhaps more slowly. +** +** PREVENTS-HARMLESS-OVERREAD - This branch prevents a buffer overread +** that would be harmless and undetectable +** if it did occur. +** +** In all cases, the special comment must be enclosed in the usual +** slash-asterisk...asterisk-slash comment marks, with no spaces between the +** asterisks and the comment text. +*/ + +/* +** Make sure the Tcl calling convention macro is defined. This macro is +** only used by test code and Tcl integration code. +*/ +#ifndef SQLITE_TCLAPI +# define SQLITE_TCLAPI +#endif /* ** Include the header file used to customize the compiler options for MSVC. @@ -60,12 +95,41 @@ # define _LARGEFILE_SOURCE 1 #endif -/* What version of GCC is being used. 0 means GCC is not being used */ -#ifdef __GNUC__ +/* The GCC_VERSION and MSVC_VERSION macros are used to +** conditionally include optimizations for each of these compilers. A +** value of 0 means that compiler is not being used. The +** SQLITE_DISABLE_INTRINSIC macro means do not use any compiler-specific +** optimizations, and hence set all compiler macros to 0 +** +** There was once also a CLANG_VERSION macro. However, we learn that the +** version numbers in clang are for "marketing" only and are inconsistent +** and unreliable. Fortunately, all versions of clang also recognize the +** gcc version numbers and have reasonable settings for gcc version numbers, +** so the GCC_VERSION macro will be set to a correct non-zero value even +** when compiling with clang. +*/ +#if defined(__GNUC__) && !defined(SQLITE_DISABLE_INTRINSIC) # define GCC_VERSION (__GNUC__*1000000+__GNUC_MINOR__*1000+__GNUC_PATCHLEVEL__) #else # define GCC_VERSION 0 #endif +#if defined(_MSC_VER) && !defined(SQLITE_DISABLE_INTRINSIC) +# define MSVC_VERSION _MSC_VER +#else +# define MSVC_VERSION 0 +#endif + +/* +** Some C99 functions in "math.h" are only present for MSVC when its version +** is associated with Visual Studio 2013 or higher. +*/ +#ifndef SQLITE_HAVE_C99_MATH_FUNCS +# if MSVC_VERSION==0 || MSVC_VERSION>=1800 +# define SQLITE_HAVE_C99_MATH_FUNCS (1) +# else +# define SQLITE_HAVE_C99_MATH_FUNCS (0) +# endif +#endif /* Needed for various definitions... */ #if defined(__GNUC__) && !defined(_GNU_SOURCE) @@ -76,6 +140,18 @@ # define _BSD_SOURCE #endif +/* +** Macro to disable warnings about missing "break" at the end of a "case". +*/ +#if defined(__has_attribute) +# if __has_attribute(fallthrough) +# define deliberate_fall_through __attribute__((fallthrough)); +# endif +#endif +#if !defined(deliberate_fall_through) +# define deliberate_fall_through +#endif + /* ** For MinGW, check to see if we can include the header file containing its ** version information, among other things. Normally, this internal MinGW @@ -108,18 +184,35 @@ # define _USE_32BIT_TIME_T #endif +/* Optionally #include a user-defined header, whereby compilation options +** may be set prior to where they take effect, but after platform setup. +** If SQLITE_CUSTOM_INCLUDE=? is defined, its value names the #include +** file. +*/ +#ifdef SQLITE_CUSTOM_INCLUDE +# define INC_STRINGIFY_(f) #f +# define INC_STRINGIFY(f) INC_STRINGIFY_(f) +# include INC_STRINGIFY(SQLITE_CUSTOM_INCLUDE) +#endif + /* The public SQLite interface. The _FILE_OFFSET_BITS macro must appear ** first in QNX. Also, the _USE_32BIT_TIME_T macro must appear first for ** MinGW. */ #include "sqlite3.h" +/* +** Reuse the STATIC_LRU for mutex access to sqlite3_temp_directory. +*/ +#define SQLITE_MUTEX_STATIC_TEMPDIR SQLITE_MUTEX_STATIC_VFS1 + /* ** Include the configuration header output by 'configure' if we're using the ** autoconf-based build */ -#ifdef _HAVE_SQLITE_CONFIG_H -#include "config.h" +#if defined(_HAVE_SQLITE_CONFIG_H) && !defined(SQLITECONFIG_H) +#include "sqlite_cfg.h" +#define SQLITECONFIG_H 1 #endif #include "sqliteLimit.h" @@ -133,6 +226,23 @@ #pragma warn -spa /* Suspicious pointer arithmetic */ #endif +/* +** A few places in the code require atomic load/store of aligned +** integer values. +*/ +#ifndef __has_extension +# define __has_extension(x) 0 /* compatibility with non-clang compilers */ +#endif +#if GCC_VERSION>=4007000 || __has_extension(c_atomic) +# define SQLITE_ATOMIC_INTRINSICS 1 +# define AtomicLoad(PTR) __atomic_load_n((PTR),__ATOMIC_RELAXED) +# define AtomicStore(PTR,VAL) __atomic_store_n((PTR),(VAL),__ATOMIC_RELAXED) +#else +# define SQLITE_ATOMIC_INTRINSICS 0 +# define AtomicLoad(PTR) (*(PTR)) +# define AtomicStore(PTR,VAL) (*(PTR) = (VAL)) +#endif + /* ** Include standard header files as necessary */ @@ -149,7 +259,7 @@ ** to the next, so we have developed the following set of #if statements ** to generate appropriate macros for a wide range of compilers. ** -** The correct "ANSI" way to do this is to use the intptr_t type. +** The correct "ANSI" way to do this is to use the intptr_t type. ** Unfortunately, that typedef is not available on all compilers, or ** if it is available, it requires an #include of specific headers ** that vary from one machine to the next. @@ -159,45 +269,37 @@ ** So we have to define the macros in different ways depending on the ** compiler. */ -#if defined(__PTRDIFF_TYPE__) /* This case should work for GCC */ +#if defined(HAVE_STDINT_H) /* Use this case if we have ANSI headers */ +# define SQLITE_INT_TO_PTR(X) ((void*)(intptr_t)(X)) +# define SQLITE_PTR_TO_INT(X) ((int)(intptr_t)(X)) +#elif defined(__PTRDIFF_TYPE__) /* This case should work for GCC */ # define SQLITE_INT_TO_PTR(X) ((void*)(__PTRDIFF_TYPE__)(X)) # define SQLITE_PTR_TO_INT(X) ((int)(__PTRDIFF_TYPE__)(X)) #elif !defined(__GNUC__) /* Works for compilers other than LLVM */ # define SQLITE_INT_TO_PTR(X) ((void*)&((char*)0)[X]) # define SQLITE_PTR_TO_INT(X) ((int)(((char*)X)-(char*)0)) -#elif defined(HAVE_STDINT_H) /* Use this case if we have ANSI headers */ -# define SQLITE_INT_TO_PTR(X) ((void*)(intptr_t)(X)) -# define SQLITE_PTR_TO_INT(X) ((int)(intptr_t)(X)) #else /* Generates a warning - but it always works */ # define SQLITE_INT_TO_PTR(X) ((void*)(X)) # define SQLITE_PTR_TO_INT(X) ((int)(X)) #endif /* -** The SQLITE_WITHIN(P,S,E) macro checks to see if pointer P points to -** something between S (inclusive) and E (exclusive). -** -** In other words, S is a buffer and E is a pointer to the first byte after -** the end of buffer S. This macro returns true if P points to something -** contained within the buffer S. -*/ -#if defined(HAVE_STDINT_H) -# define SQLITE_WITHIN(P,S,E) \ - ((uintptr_t)(P)>=(uintptr_t)(S) && (uintptr_t)(P)<(uintptr_t)(E)) -#else -# define SQLITE_WITHIN(P,S,E) ((P)>=(S) && (P)<(E)) -#endif - -/* -** A macro to hint to the compiler that a function should not be +** Macros to hint to the compiler that a function should or should not be ** inlined. */ #if defined(__GNUC__) # define SQLITE_NOINLINE __attribute__((noinline)) +# define SQLITE_INLINE __attribute__((always_inline)) inline #elif defined(_MSC_VER) && _MSC_VER>=1310 # define SQLITE_NOINLINE __declspec(noinline) +# define SQLITE_INLINE __forceinline #else # define SQLITE_NOINLINE +# define SQLITE_INLINE +#endif +#if defined(SQLITE_COVERAGE_TEST) || defined(__STRICT_ANSI__) +# undef SQLITE_INLINE +# define SQLITE_INLINE #endif /* @@ -206,11 +308,12 @@ ** the SQLITE_DISABLE_INTRINSIC define. */ #if !defined(SQLITE_DISABLE_INTRINSIC) -# if defined(_MSC_VER) && _MSC_VER>=1300 +# if defined(_MSC_VER) && _MSC_VER>=1400 # if !defined(_WIN32_WCE) # include <intrin.h> # pragma intrinsic(_byteswap_ushort) # pragma intrinsic(_byteswap_ulong) +# pragma intrinsic(_byteswap_uint64) # pragma intrinsic(_ReadWriteBarrier) # else # include <cmnintrin.h> @@ -218,6 +321,29 @@ # endif #endif +/* +** Enable SQLITE_USE_SEH by default on MSVC builds. Only omit +** SEH support if the -DSQLITE_OMIT_SEH option is given. +*/ +#if defined(_MSC_VER) && !defined(SQLITE_OMIT_SEH) +# define SQLITE_USE_SEH 1 +#else +# undef SQLITE_USE_SEH +#endif + +/* +** Enable SQLITE_DIRECT_OVERFLOW_READ, unless the build explicitly +** disables it using -DSQLITE_DIRECT_OVERFLOW_READ=0 +*/ +#if defined(SQLITE_DIRECT_OVERFLOW_READ) && SQLITE_DIRECT_OVERFLOW_READ+1==1 + /* Disable if -DSQLITE_DIRECT_OVERFLOW_READ=0 */ +# undef SQLITE_DIRECT_OVERFLOW_READ +#else + /* In all other cases, enable */ +# define SQLITE_DIRECT_OVERFLOW_READ 1 +#endif + + /* ** The SQLITE_THREADSAFE macro must be defined as 0, 1, or 2. ** 0 means mutexes are permanently disable and the library is never @@ -228,6 +354,11 @@ ** ** Older versions of SQLite used an optional THREADSAFE macro. ** We support that for legacy. +** +** To ensure that the correct value of "THREADSAFE" is reported when querying +** for compile-time options at runtime (e.g. "PRAGMA compile_options"), this +** logic is partially replicated in ctime.c. If it is updated here, it should +** also be updated there. */ #if !defined(SQLITE_THREADSAFE) # if defined(THREADSAFE) @@ -316,7 +447,7 @@ ** is set. Thus NDEBUG becomes an opt-in rather than an opt-out ** feature. */ -#if !defined(NDEBUG) && !defined(SQLITE_DEBUG) +#if !defined(NDEBUG) && !defined(SQLITE_DEBUG) # define NDEBUG 1 #endif #if defined(NDEBUG) && defined(SQLITE_DEBUG) @@ -331,7 +462,7 @@ #endif /* -** The testcase() macro is used to aid in coverage testing. When +** The testcase() macro is used to aid in coverage testing. When ** doing coverage testing, the condition inside the argument to ** testcase() must be evaluated both true and false in order to ** get full branch coverage. The testcase() macro is inserted @@ -342,11 +473,12 @@ ** is significant and used at least once. On switch statements ** where multiple cases go to the same block of code, testcase() ** can insure that all cases are evaluated. -** */ -#ifdef SQLITE_COVERAGE_TEST - void sqlite3Coverage(int); -# define testcase(X) if( X ){ sqlite3Coverage(__LINE__); } +#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_DEBUG) +# ifndef SQLITE_AMALGAMATION + extern unsigned int sqlite3CoverageCounter; +# endif +# define testcase(X) if( X ){ sqlite3CoverageCounter += (unsigned)__LINE__; } #else # define testcase(X) #endif @@ -377,7 +509,15 @@ #endif /* -** The ALWAYS and NEVER macros surround boolean expressions which +** Disable ALWAYS() and NEVER() (make them pass-throughs) for coverage +** and mutation testing +*/ +#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST) +# define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1 +#endif + +/* +** The ALWAYS and NEVER macros surround boolean expressions which ** are intended to always be true or false, respectively. Such ** expressions could be omitted from the code completely. But they ** are included in a few cases in order to enhance the resilience @@ -391,7 +531,7 @@ ** be true and false so that the unreachable code they specify will ** not be counted as untested code. */ -#if defined(SQLITE_COVERAGE_TEST) +#if defined(SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS) # define ALWAYS(X) (1) # define NEVER(X) (0) #elif !defined(NDEBUG) @@ -402,6 +542,21 @@ # define NEVER(X) (X) #endif +/* +** Some conditionals are optimizations only. In other words, if the +** conditionals are replaced with a constant 1 (true) or 0 (false) then +** the correct answer is still obtained, though perhaps not as quickly. +** +** The following macros mark these optimizations conditionals. +*/ +#if defined(SQLITE_MUTATION_TEST) +# define OK_IF_ALWAYS_TRUE(X) (1) +# define OK_IF_ALWAYS_FALSE(X) (0) +#else +# define OK_IF_ALWAYS_TRUE(X) (X) +# define OK_IF_ALWAYS_FALSE(X) (X) +#endif + /* ** Some malloc failures are only possible if SQLITE_TEST_REALLOC_STRESS is ** defined. We need to defend against those failures when testing with @@ -443,6 +598,22 @@ # undef SQLITE_NEED_ERR_NAME #endif +/* +** SQLITE_ENABLE_EXPLAIN_COMMENTS is incompatible with SQLITE_OMIT_EXPLAIN +*/ +#ifdef SQLITE_OMIT_EXPLAIN +# undef SQLITE_ENABLE_EXPLAIN_COMMENTS +#endif + +/* +** SQLITE_OMIT_VIRTUALTABLE implies SQLITE_OMIT_ALTERTABLE +*/ +#if defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_ALTERTABLE) +# define SQLITE_OMIT_ALTERTABLE +#endif + +#define SQLITE_DIGIT_SEPARATOR '_' + /* ** Return true (non-zero) if the input is an integer that is too large ** to fit in 32-bits. This macro is used inside of various testcase() @@ -467,6 +638,19 @@ #include <string.h> #include <assert.h> #include <stddef.h> +#include <ctype.h> + +/* +** Use a macro to replace memcpy() if compiled with SQLITE_INLINE_MEMCPY. +** This allows better measurements of where memcpy() is used when running +** cachegrind. But this macro version of memcpy() is very slow so it +** should not be used in production. This is a performance measurement +** hack only. +*/ +#ifdef SQLITE_INLINE_MEMCPY +# define memcpy(D,S,N) {char*xxd=(char*)(D);const char*xxs=(const char*)(S);\ + int xxn=(N);while(xxn-->0)*(xxd++)=*(xxs++);} +#endif /* ** If compiling for a processor that lacks floating point support, @@ -475,7 +659,8 @@ #ifdef SQLITE_OMIT_FLOATING_POINT # define double sqlite_int64 # define float sqlite_int64 -# define LONGDOUBLE_TYPE sqlite_int64 +# define fabs(X) ((X)<0?-(X):(X)) +# define sqlite3IsOverflow(X) 0 # ifndef SQLITE_BIG_DBL # define SQLITE_BIG_DBL (((sqlite3_int64)1)<<50) # endif @@ -490,7 +675,7 @@ /* ** OMIT_TEMPDB is set to 1 if SQLITE_OMIT_TEMPDB is defined, or 0 -** afterward. Having this macro allows us to cause the C compiler +** afterward. Having this macro allows us to cause the C compiler ** to omit code used by TEMP tables without messy #ifndef statements. */ #ifdef SQLITE_OMIT_TEMPDB @@ -524,12 +709,11 @@ */ #ifndef SQLITE_TEMP_STORE # define SQLITE_TEMP_STORE 1 -# define SQLITE_TEMP_STORE_xc 1 /* Exclude from ctime.c */ #endif /* ** If no value has been provided for SQLITE_MAX_WORKER_THREADS, or if -** SQLITE_TEMP_STORE is set to 3 (never use temporary files), set it +** SQLITE_TEMP_STORE is set to 3 (never use temporary files), set it ** to zero. */ #if SQLITE_TEMP_STORE==3 || SQLITE_THREADSAFE==0 @@ -552,9 +736,28 @@ ** pagecaches for each database connection. A positive number is the ** number of pages. A negative number N translations means that a buffer ** of -1024*N bytes is allocated and used for as many pages as it will hold. +** +** The default value of "20" was chosen to minimize the run-time of the +** speedtest1 test program with options: --shrink-memory --reprepare */ #ifndef SQLITE_DEFAULT_PCACHE_INITSZ -# define SQLITE_DEFAULT_PCACHE_INITSZ 100 +# define SQLITE_DEFAULT_PCACHE_INITSZ 20 +#endif + +/* +** Default value for the SQLITE_CONFIG_SORTERREF_SIZE option. +*/ +#ifndef SQLITE_DEFAULT_SORTERREF_SIZE +# define SQLITE_DEFAULT_SORTERREF_SIZE 0x7fffffff +#endif + +/* +** The compile-time options SQLITE_MMAP_READWRITE and +** SQLITE_ENABLE_BATCH_ATOMIC_WRITE are not compatible with one another. +** You must choose one or the other (or neither) but not both. +*/ +#if defined(SQLITE_MMAP_READWRITE) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE) +#error Cannot use both SQLITE_MMAP_READWRITE and SQLITE_ENABLE_BATCH_ATOMIC_WRITE #endif /* @@ -562,14 +765,28 @@ ** ourselves. */ #ifndef offsetof -#define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD)) +# define offsetof(ST,M) ((size_t)((char*)&((ST*)0)->M - (char*)0)) +#endif + +/* +** Work around C99 "flex-array" syntax for pre-C99 compilers, so as +** to avoid complaints from -fsanitize=strict-bounds. +*/ +#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) +# define FLEXARRAY +#else +# define FLEXARRAY 1 #endif /* ** Macros to compute minimum and maximum of two numbers. */ -#define MIN(A,B) ((A)<(B)?(A):(B)) -#define MAX(A,B) ((A)>(B)?(A):(B)) +#ifndef MIN +# define MIN(A,B) ((A)<(B)?(A):(B)) +#endif +#ifndef MAX +# define MAX(A,B) ((A)>(B)?(A):(B)) +#endif /* ** Swap two objects of type TYPE. @@ -628,9 +845,6 @@ # define INT8_TYPE signed char # endif #endif -#ifndef LONGDOUBLE_TYPE -# define LONGDOUBLE_TYPE long double -#endif typedef sqlite_int64 i64; /* 8-byte signed integer */ typedef sqlite_uint64 u64; /* 8-byte unsigned integer */ typedef UINT32_TYPE u32; /* 4-byte unsigned integer */ @@ -639,6 +853,11 @@ typedef INT16_TYPE i16; /* 2-byte signed integer */ typedef UINT8_TYPE u8; /* 1-byte unsigned integer */ typedef INT8_TYPE i8; /* 1-byte signed integer */ +/* A bitfield type for use inside of structures. Always follow with :N where +** N is the number of bits. +*/ +typedef unsigned bft; /* Bit Field Type */ + /* ** SQLITE_MAX_U32 is a u64 constant that is the maximum u64 value ** that can be stored in a u32 without loss of data. The value @@ -649,15 +868,9 @@ typedef INT8_TYPE i8; /* 1-byte signed integer */ /* ** The datatype used to store estimates of the number of rows in a -** table or index. This is an unsigned integer type. For 99.9% of -** the world, a 32-bit integer is sufficient. But a 64-bit integer -** can be used at compile-time if desired. +** table or index. */ -#ifdef SQLITE_64BIT_STATS - typedef u64 tRowcnt; /* 64-bit only if requested at compile-time */ -#else - typedef u32 tRowcnt; /* 32-bit is the default */ -#endif +typedef u64 tRowcnt; /* ** Estimated quantities used for query planning are stored as 16-bit @@ -677,12 +890,14 @@ typedef INT8_TYPE i8; /* 1-byte signed integer */ ** 4 -> 20 1000 -> 99 1048576 -> 200 ** 10 -> 33 1024 -> 100 4294967296 -> 320 ** -** The LogEst can be negative to indicate fractional values. +** The LogEst can be negative to indicate fractional values. ** Examples: ** ** 0.5 -> -10 0.1 -> -33 0.0625 -> -40 */ typedef INT16_TYPE LogEst; +#define LOGEST_MIN (-32768) +#define LOGEST_MAX (32767) /* ** Set the SQLITE_PTRSIZE macro to the number of bytes in a pointer @@ -691,45 +906,112 @@ typedef INT16_TYPE LogEst; # if defined(__SIZEOF_POINTER__) # define SQLITE_PTRSIZE __SIZEOF_POINTER__ # elif defined(i386) || defined(__i386__) || defined(_M_IX86) || \ - defined(_M_ARM) || defined(__arm__) || defined(__x86) + defined(_M_ARM) || defined(__arm__) || defined(__x86) || \ + (defined(__APPLE__) && defined(__ppc__)) || \ + (defined(__TOS_AIX__) && !defined(__64BIT__)) # define SQLITE_PTRSIZE 4 # else # define SQLITE_PTRSIZE 8 # endif #endif +/* The uptr type is an unsigned integer large enough to hold a pointer +*/ +#if defined(HAVE_STDINT_H) + typedef uintptr_t uptr; +#elif SQLITE_PTRSIZE==4 + typedef u32 uptr; +#else + typedef u64 uptr; +#endif + +/* +** The SQLITE_WITHIN(P,S,E) macro checks to see if pointer P points to +** something between S (inclusive) and E (exclusive). +** +** In other words, S is a buffer and E is a pointer to the first byte after +** the end of buffer S. This macro returns true if P points to something +** contained within the buffer S. +*/ +#define SQLITE_WITHIN(P,S,E) (((uptr)(P)>=(uptr)(S))&&((uptr)(P)<(uptr)(E))) + +/* +** P is one byte past the end of a large buffer. Return true if a span of bytes +** between S..E crosses the end of that buffer. In other words, return true +** if the sub-buffer S..E-1 overflows the buffer whose last byte is P-1. +** +** S is the start of the span. E is one byte past the end of end of span. +** +** P +** |-----------------| FALSE +** |-------| +** S E +** +** P +** |-----------------| +** |-------| TRUE +** S E +** +** P +** |-----------------| +** |-------| FALSE +** S E +*/ +#define SQLITE_OVERFLOW(P,S,E) (((uptr)(S)<(uptr)(P))&&((uptr)(E)>(uptr)(P))) + /* ** Macros to determine whether the machine is big or little endian, ** and whether or not that determination is run-time or compile-time. ** ** For best performance, an attempt is made to guess at the byte-order ** using C-preprocessor macros. If that is unsuccessful, or if -** -DSQLITE_RUNTIME_BYTEORDER=1 is set, then byte-order is determined +** -DSQLITE_BYTEORDER=0 is set, then byte-order is determined ** at run-time. +** +** If you are building SQLite on some obscure platform for which the +** following ifdef magic does not work, you can always include either: +** +** -DSQLITE_BYTEORDER=1234 +** +** or +** +** -DSQLITE_BYTEORDER=4321 +** +** to cause the build to work for little-endian or big-endian processors, +** respectively. */ -#if (defined(i386) || defined(__i386__) || defined(_M_IX86) || \ - defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \ - defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \ - defined(__arm__)) && !defined(SQLITE_RUNTIME_BYTEORDER) -# define SQLITE_BYTEORDER 1234 -# define SQLITE_BIGENDIAN 0 -# define SQLITE_LITTLEENDIAN 1 -# define SQLITE_UTF16NATIVE SQLITE_UTF16LE +#ifndef SQLITE_BYTEORDER /* Replicate changes at tag-20230904a */ +# if defined(__BYTE_ORDER__) && __BYTE_ORDER__==__ORDER_BIG_ENDIAN__ +# define SQLITE_BYTEORDER 4321 +# elif defined(__BYTE_ORDER__) && __BYTE_ORDER__==__ORDER_LITTLE_ENDIAN__ +# define SQLITE_BYTEORDER 1234 +# elif defined(__BIG_ENDIAN__) && __BIG_ENDIAN__==1 +# define SQLITE_BYTEORDER 4321 +# elif defined(i386) || defined(__i386__) || defined(_M_IX86) || \ + defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \ + defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \ + defined(__ARMEL__) || defined(__AARCH64EL__) || defined(_M_ARM64) +# define SQLITE_BYTEORDER 1234 +# elif defined(sparc) || defined(__ARMEB__) || defined(__AARCH64EB__) +# define SQLITE_BYTEORDER 4321 +# else +# define SQLITE_BYTEORDER 0 +# endif #endif -#if (defined(sparc) || defined(__ppc__)) \ - && !defined(SQLITE_RUNTIME_BYTEORDER) -# define SQLITE_BYTEORDER 4321 +#if SQLITE_BYTEORDER==4321 # define SQLITE_BIGENDIAN 1 # define SQLITE_LITTLEENDIAN 0 # define SQLITE_UTF16NATIVE SQLITE_UTF16BE -#endif -#if !defined(SQLITE_BYTEORDER) +#elif SQLITE_BYTEORDER==1234 +# define SQLITE_BIGENDIAN 0 +# define SQLITE_LITTLEENDIAN 1 +# define SQLITE_UTF16NATIVE SQLITE_UTF16LE +#else # ifdef SQLITE_AMALGAMATION const int sqlite3one = 1; # else extern const int sqlite3one; # endif -# define SQLITE_BYTEORDER 0 /* 0 means "unknown at compile-time" */ # define SQLITE_BIGENDIAN (*(char *)(&sqlite3one)==0) # define SQLITE_LITTLEENDIAN (*(char *)(&sqlite3one)==1) # define SQLITE_UTF16NATIVE (SQLITE_BIGENDIAN?SQLITE_UTF16BE:SQLITE_UTF16LE) @@ -741,13 +1023,33 @@ typedef INT16_TYPE LogEst; ** compilers. */ #define LARGEST_INT64 (0xffffffff|(((i64)0x7fffffff)<<32)) +#define LARGEST_UINT64 (0xffffffff|(((u64)0xffffffff)<<32)) #define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64) -/* +/* +** Macro SMXV(n) return the maximum value that can be held in variable n, +** assuming n is a signed integer type. UMXV(n) is similar for unsigned +** integer types. +*/ +#define SMXV(n) ((((i64)1)<<(sizeof(n)*8-1))-1) +#define UMXV(n) ((((i64)1)<<(sizeof(n)*8))-1) + +/* ** Round up a number to the next larger multiple of 8. This is used ** to force 8-byte alignment on 64-bit architectures. +** +** ROUND8() always does the rounding, for any argument. +** +** ROUND8P() assumes that the argument is already an integer number of +** pointers in size, and so it is a no-op on systems where the pointer +** size is 8. */ #define ROUND8(x) (((x)+7)&~7) +#if SQLITE_PTRSIZE==8 +# define ROUND8P(x) (x) +#else +# define ROUND8P(x) (((x)+7)&~7) +#endif /* ** Round down to the nearest multiple of 8 @@ -764,9 +1066,9 @@ typedef INT16_TYPE LogEst; ** pointers. In that case, only verify 4-byte alignment. */ #ifdef SQLITE_4_BYTE_ALIGNED_MALLOC -# define EIGHT_BYTE_ALIGNMENT(X) ((((char*)(X) - (char*)0)&3)==0) +# define EIGHT_BYTE_ALIGNMENT(X) ((((uptr)(X) - (uptr)0)&3)==0) #else -# define EIGHT_BYTE_ALIGNMENT(X) ((((char*)(X) - (char*)0)&7)==0) +# define EIGHT_BYTE_ALIGNMENT(X) ((((uptr)(X) - (uptr)0)&7)==0) #endif /* @@ -794,7 +1096,6 @@ typedef INT16_TYPE LogEst; # else # define SQLITE_MAX_MMAP_SIZE 0 # endif -# define SQLITE_MAX_MMAP_SIZE_xc 1 /* exclude from ctime.c */ #endif /* @@ -804,7 +1105,6 @@ typedef INT16_TYPE LogEst; */ #ifndef SQLITE_DEFAULT_MMAP_SIZE # define SQLITE_DEFAULT_MMAP_SIZE 0 -# define SQLITE_DEFAULT_MMAP_SIZE_xc 1 /* Exclude from ctime.c */ #endif #if SQLITE_DEFAULT_MMAP_SIZE>SQLITE_MAX_MMAP_SIZE # undef SQLITE_DEFAULT_MMAP_SIZE @@ -812,32 +1112,100 @@ typedef INT16_TYPE LogEst; #endif /* -** Only one of SQLITE_ENABLE_STAT3 or SQLITE_ENABLE_STAT4 can be defined. -** Priority is given to SQLITE_ENABLE_STAT4. If either are defined, also -** define SQLITE_ENABLE_STAT3_OR_STAT4 +** TREETRACE_ENABLED will be either 1 or 0 depending on whether or not +** the Abstract Syntax Tree tracing logic is turned on. */ -#ifdef SQLITE_ENABLE_STAT4 -# undef SQLITE_ENABLE_STAT3 -# define SQLITE_ENABLE_STAT3_OR_STAT4 1 -#elif SQLITE_ENABLE_STAT3 -# define SQLITE_ENABLE_STAT3_OR_STAT4 1 -#elif SQLITE_ENABLE_STAT3_OR_STAT4 -# undef SQLITE_ENABLE_STAT3_OR_STAT4 +#if !defined(SQLITE_AMALGAMATION) +extern u32 sqlite3TreeTrace; +#endif +#if defined(SQLITE_DEBUG) \ + && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_SELECTTRACE) \ + || defined(SQLITE_ENABLE_TREETRACE)) +# define TREETRACE_ENABLED 1 +# define TREETRACE(K,P,S,X) \ + if(sqlite3TreeTrace&(K)) \ + sqlite3DebugPrintf("%u/%d/%p: ",(S)->selId,(P)->addrExplain,(S)),\ + sqlite3DebugPrintf X +#else +# define TREETRACE(K,P,S,X) +# define TREETRACE_ENABLED 0 +#endif + +/* TREETRACE flag meanings: +** +** 0x00000001 Beginning and end of SELECT processing +** 0x00000002 WHERE clause processing +** 0x00000004 Query flattener +** 0x00000008 Result-set wildcard expansion +** 0x00000010 Query name resolution +** 0x00000020 Aggregate analysis +** 0x00000040 Window functions +** 0x00000080 Generated column names +** 0x00000100 Move HAVING terms into WHERE +** 0x00000200 Count-of-view optimization +** 0x00000400 Compound SELECT processing +** 0x00000800 Drop superfluous ORDER BY +** 0x00001000 LEFT JOIN simplifies to JOIN +** 0x00002000 Constant propagation +** 0x00004000 Push-down optimization +** 0x00008000 After all FROM-clause analysis +** 0x00010000 Beginning of DELETE/INSERT/UPDATE processing +** 0x00020000 Transform DISTINCT into GROUP BY +** 0x00040000 SELECT tree dump after all code has been generated +** 0x00080000 NOT NULL strength reduction +** 0x00100000 Pointers are all shown as zero +** 0x00200000 EXISTS-to-JOIN optimization +*/ + +/* +** Macros for "wheretrace" +*/ +extern u32 sqlite3WhereTrace; +#if defined(SQLITE_DEBUG) \ + && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_WHERETRACE)) +# define WHERETRACE(K,X) if(sqlite3WhereTrace&(K)) sqlite3DebugPrintf X +# define WHERETRACE_ENABLED 1 +#else +# define WHERETRACE(K,X) #endif /* -** SELECTTRACE_ENABLED will be either 1 or 0 depending on whether or not -** the Select query generator tracing logic is turned on. +** Bits for the sqlite3WhereTrace mask: +** +** (---any--) Top-level block structure +** 0x-------F High-level debug messages +** 0x----FFF- More detail +** 0xFFFF---- Low-level debug messages +** +** 0x00000001 Code generation +** 0x00000002 Solver (Use 0x40000 for less detail) +** 0x00000004 Solver costs +** 0x00000008 WhereLoop inserts +** +** 0x00000010 Display sqlite3_index_info xBestIndex calls +** 0x00000020 Range an equality scan metrics +** 0x00000040 IN operator decisions +** 0x00000080 WhereLoop cost adjustments +** 0x00000100 +** 0x00000200 Covering index decisions +** 0x00000400 OR optimization +** 0x00000800 Index scanner +** 0x00001000 More details associated with code generation +** 0x00002000 +** 0x00004000 Show all WHERE terms at key points +** 0x00008000 Show the full SELECT statement at key places +** +** 0x00010000 Show more detail when printing WHERE terms +** 0x00020000 Show WHERE terms returned from whereScanNext() +** 0x00040000 Solver overview messages +** 0x00080000 Star-query heuristic +** 0x00100000 Pointers are all shown as zero */ -#if defined(SQLITE_DEBUG) || defined(SQLITE_ENABLE_SELECTTRACE) -# define SELECTTRACE_ENABLED 1 -#else -# define SELECTTRACE_ENABLED 0 -#endif + /* ** An instance of the following structure is used to store the busy-handler -** callback for a given sqlite handle. +** callback for a given sqlite handle. ** ** The sqlite.busyHandler member of the sqlite struct contains the busy ** callback for the database handle. Each pager opened via the sqlite @@ -846,28 +1214,44 @@ typedef INT16_TYPE LogEst; */ typedef struct BusyHandler BusyHandler; struct BusyHandler { - int (*xFunc)(void *,int); /* The busy callback */ - void *pArg; /* First arg to busy callback */ - int nBusy; /* Incremented with each busy call */ + int (*xBusyHandler)(void *,int); /* The busy callback */ + void *pBusyArg; /* First arg to busy callback */ + int nBusy; /* Incremented with each busy call */ }; /* -** Name of the master database table. The master database table -** is a special table that holds the names and attributes of all -** user tables and indices. +** Name of table that holds the database schema. +** +** The PREFERRED names are used wherever possible. But LEGACY is also +** used for backwards compatibility. +** +** 1. Queries can use either the PREFERRED or the LEGACY names +** 2. The sqlite3_set_authorizer() callback uses the LEGACY name +** 3. The PRAGMA table_list statement uses the PREFERRED name +** +** The LEGACY names are stored in the internal symbol hash table +** in support of (2). Names are translated using sqlite3PreferredTableName() +** for (3). The sqlite3FindTable() function takes care of translating +** names for (1). +** +** Note that "sqlite_temp_schema" can also be called "temp.sqlite_schema". */ -#define MASTER_NAME "sqlite_master" -#define TEMP_MASTER_NAME "sqlite_temp_master" +#define LEGACY_SCHEMA_TABLE "sqlite_master" +#define LEGACY_TEMP_SCHEMA_TABLE "sqlite_temp_master" +#define PREFERRED_SCHEMA_TABLE "sqlite_schema" +#define PREFERRED_TEMP_SCHEMA_TABLE "sqlite_temp_schema" + /* -** The root-page of the master database table. +** The root-page of the schema table. */ -#define MASTER_ROOT 1 +#define SCHEMA_ROOT 1 /* -** The name of the schema table. +** The name of the schema table. The name is different for TEMP. */ -#define SCHEMA_TABLE(x) ((!OMIT_TEMPDB)&&(x==1)?TEMP_MASTER_NAME:MASTER_NAME) +#define SCHEMA_TABLE(x) \ + ((!OMIT_TEMPDB)&&(x==1)?LEGACY_TEMP_SCHEMA_TABLE:LEGACY_SCHEMA_TABLE) /* ** A convenience macro that returns the number of elements in @@ -882,13 +1266,13 @@ struct BusyHandler { /* ** The following value as a destructor means to use sqlite3DbFree(). -** The sqlite3DbFree() routine requires two parameters instead of the -** one parameter that destructors normally want. So we have to introduce -** this magic value that the code knows to handle differently. Any +** The sqlite3DbFree() routine requires two parameters instead of the +** one parameter that destructors normally want. So we have to introduce +** this magic value that the code knows to handle differently. Any ** pointer will work here as long as it is distinct from SQLITE_STATIC ** and SQLITE_TRANSIENT. */ -#define SQLITE_DYNAMIC ((sqlite3_destructor_type)sqlite3MallocSize) +#define SQLITE_DYNAMIC ((sqlite3_destructor_type)sqlite3RowSetClear) /* ** When SQLITE_OMIT_WSD is defined, it means that the target platform does @@ -911,16 +1295,16 @@ struct BusyHandler { int sqlite3_wsd_init(int N, int J); void *sqlite3_wsd_find(void *K, int L); #else - #define SQLITE_WSD + #define SQLITE_WSD #define GLOBAL(t,v) v #define sqlite3GlobalConfig sqlite3Config #endif /* ** The following macros are used to suppress compiler warnings and to -** make it clear to human readers when a function parameter is deliberately +** make it clear to human readers when a function parameter is deliberately ** left unused within the body of a function. This usually happens when -** a function is called via a function pointer. For example the +** a function is called via a function pointer. For example the ** implementation of an SQL aggregate step callback may not use the ** parameter indicating the number of arguments passed to the aggregate, ** if it knows that this is enforced elsewhere. @@ -944,17 +1328,22 @@ typedef struct AutoincInfo AutoincInfo; typedef struct Bitvec Bitvec; typedef struct CollSeq CollSeq; typedef struct Column Column; +typedef struct Cte Cte; +typedef struct CteUse CteUse; typedef struct Db Db; +typedef struct DbClientData DbClientData; +typedef struct DbFixer DbFixer; typedef struct Schema Schema; typedef struct Expr Expr; typedef struct ExprList ExprList; -typedef struct ExprSpan ExprSpan; typedef struct FKey FKey; +typedef struct FpDecode FpDecode; typedef struct FuncDestructor FuncDestructor; typedef struct FuncDef FuncDef; typedef struct FuncDefHash FuncDefHash; typedef struct IdList IdList; typedef struct Index Index; +typedef struct IndexedExpr IndexedExpr; typedef struct IndexSample IndexSample; typedef struct KeyClass KeyClass; typedef struct KeyInfo KeyInfo; @@ -962,15 +1351,23 @@ typedef struct Lookaside Lookaside; typedef struct LookasideSlot LookasideSlot; typedef struct Module Module; typedef struct NameContext NameContext; +typedef struct OnOrUsing OnOrUsing; typedef struct Parse Parse; +typedef struct ParseCleanup ParseCleanup; +typedef struct PreUpdate PreUpdate; typedef struct PrintfArguments PrintfArguments; +typedef struct RCStr RCStr; +typedef struct RenameToken RenameToken; +typedef struct Returning Returning; typedef struct RowSet RowSet; typedef struct Savepoint Savepoint; typedef struct Select Select; typedef struct SQLiteThread SQLiteThread; typedef struct SelectDest SelectDest; +typedef struct Subquery Subquery; +typedef struct SrcItem SrcItem; typedef struct SrcList SrcList; -typedef struct StrAccum StrAccum; +typedef struct sqlite3_str StrAccum; /* Internal alias for sqlite3_str */ typedef struct Table Table; typedef struct TableLock TableLock; typedef struct Token Token; @@ -979,25 +1376,93 @@ typedef struct Trigger Trigger; typedef struct TriggerPrg TriggerPrg; typedef struct TriggerStep TriggerStep; typedef struct UnpackedRecord UnpackedRecord; +typedef struct Upsert Upsert; typedef struct VTable VTable; typedef struct VtabCtx VtabCtx; typedef struct Walker Walker; typedef struct WhereInfo WhereInfo; +typedef struct Window Window; typedef struct With With; + /* -** Defer sourcing vdbe.h and btree.h until after the "u8" and +** The bitmask datatype defined below is used for various optimizations. +** +** Changing this from a 64-bit to a 32-bit type limits the number of +** tables in a join to 32 instead of 64. But it also reduces the size +** of the library by 738 bytes on ix86. +*/ +#ifdef SQLITE_BITMASK_TYPE + typedef SQLITE_BITMASK_TYPE Bitmask; +#else + typedef u64 Bitmask; +#endif + +/* +** The number of bits in a Bitmask. "BMS" means "BitMask Size". +*/ +#define BMS ((int)(sizeof(Bitmask)*8)) + +/* +** A bit in a Bitmask +*/ +#define MASKBIT(n) (((Bitmask)1)<<(n)) +#define MASKBIT64(n) (((u64)1)<<(n)) +#define MASKBIT32(n) (((unsigned int)1)<<(n)) +#define SMASKBIT32(n) ((n)<=31?((unsigned int)1)<<(n):0) +#define ALLBITS ((Bitmask)-1) +#define TOPBIT (((Bitmask)1)<<(BMS-1)) + +/* A VList object records a mapping between parameters/variables/wildcards +** in the SQL statement (such as $abc, @pqr, or :xyz) and the integer +** variable number associated with that parameter. See the format description +** on the sqlite3VListAdd() routine for more information. A VList is really +** just an array of integers. +*/ +typedef int VList; + +/* +** Defer sourcing vdbe.h and btree.h until after the "u8" and ** "BusyHandler" typedefs. vdbe.h also requires a few of the opaque ** pointer types (i.e. FuncDef) defined above. */ +#include "os.h" +#include "pager.h" #include "btree.h" #include "vdbe.h" -#include "pager.h" #include "pcache.h" - -#include "os.h" #include "mutex.h" +/* The SQLITE_EXTRA_DURABLE compile-time option used to set the default +** synchronous setting to EXTRA. It is no longer supported. +*/ +#ifdef SQLITE_EXTRA_DURABLE +# warning Use SQLITE_DEFAULT_SYNCHRONOUS=3 instead of SQLITE_EXTRA_DURABLE +# define SQLITE_DEFAULT_SYNCHRONOUS 3 +#endif + +/* +** Default synchronous levels. +** +** Note that (for historical reasons) the PAGER_SYNCHRONOUS_* macros differ +** from the SQLITE_DEFAULT_SYNCHRONOUS value by 1. +** +** PAGER_SYNCHRONOUS DEFAULT_SYNCHRONOUS +** OFF 1 0 +** NORMAL 2 1 +** FULL 3 2 +** EXTRA 4 3 +** +** The "PRAGMA synchronous" statement also uses the zero-based numbers. +** In other words, the zero-based numbers are used for all external interfaces +** and the one-based values are used internally. +*/ +#ifndef SQLITE_DEFAULT_SYNCHRONOUS +# define SQLITE_DEFAULT_SYNCHRONOUS 2 +#endif +#ifndef SQLITE_DEFAULT_WAL_SYNCHRONOUS +# define SQLITE_DEFAULT_WAL_SYNCHRONOUS SQLITE_DEFAULT_SYNCHRONOUS +#endif /* ** Each database file to be accessed by the system is an instance @@ -1007,9 +1472,10 @@ typedef struct With With; ** databases may be attached. */ struct Db { - char *zName; /* Name of this database */ + char *zDbSName; /* Name of this database. (schema name, not filename) */ Btree *pBt; /* The B*Tree structure for this database file */ u8 safety_level; /* How aggressive at syncing data to disk */ + u8 bSyncSet; /* True if "PRAGMA synchronous=N" has been run */ Schema *pSchema; /* Pointer to database schema (possibly shared) */ }; @@ -1017,10 +1483,10 @@ struct Db { ** An instance of the following structure stores a database schema. ** ** Most Schema objects are associated with a Btree. The exception is -** the Schema for the TEMP databaes (sqlite3.aDb[1]) which is free-standing. +** the Schema for the TEMP database (sqlite3.aDb[1]) which is free-standing. ** In shared cache mode, a single Schema object can be shared by multiple ** Btrees that refer to the same underlying BtShared object. -** +** ** Schema objects are automatically deallocated when the last Btree that ** references them is destroyed. The TEMP Schema is manually freed by ** sqlite3_close(). @@ -1045,7 +1511,7 @@ struct Schema { }; /* -** These macros can be used to test, set, or clear bits in the +** These macros can be used to test, set, or clear bits in the ** Db.pSchema->flags field. */ #define DbHasProperty(D,I,P) (((D)->aDb[I].pSchema->schemaFlags&(P))==(P)) @@ -1065,7 +1531,7 @@ struct Schema { */ #define DB_SchemaLoaded 0x0001 /* The schema has been loaded */ #define DB_UnresetViews 0x0002 /* Some views have defined column names */ -#define DB_Empty 0x0004 /* The file is empty (length 0 bytes) */ +#define DB_ResetWanted 0x0008 /* Reset the schema when nSchemaLock==0 */ /* ** The number of different kinds of things that can be limited @@ -1092,70 +1558,103 @@ struct Schema { ** is shared by multiple database connections. Therefore, while parsing ** schema information, the Lookaside.bEnabled flag is cleared so that ** lookaside allocations are not used to construct the schema objects. +** +** New lookaside allocations are only allowed if bDisable==0. When +** bDisable is greater than zero, sz is set to zero which effectively +** disables lookaside without adding a new test for the bDisable flag +** in a performance-critical path. sz should be set by to szTrue whenever +** bDisable changes back to zero. +** +** Lookaside buffers are initially held on the pInit list. As they are +** used and freed, they are added back to the pFree list. New allocations +** come off of pFree first, then pInit as a fallback. This dual-list +** allows use to compute a high-water mark - the maximum number of allocations +** outstanding at any point in the past - by subtracting the number of +** allocations on the pInit list from the total number of allocations. +** +** Enhancement on 2019-12-12: Two-size-lookaside +** The default lookaside configuration is 100 slots of 1200 bytes each. +** The larger slot sizes are important for performance, but they waste +** a lot of space, as most lookaside allocations are less than 128 bytes. +** The two-size-lookaside enhancement breaks up the lookaside allocation +** into two pools: One of 128-byte slots and the other of the default size +** (1200-byte) slots. Allocations are filled from the small-pool first, +** failing over to the full-size pool if that does not work. Thus more +** lookaside slots are available while also using less memory. +** This enhancement can be omitted by compiling with +** SQLITE_OMIT_TWOSIZE_LOOKASIDE. */ struct Lookaside { u32 bDisable; /* Only operate the lookaside when zero */ u16 sz; /* Size of each buffer in bytes */ + u16 szTrue; /* True value of sz, even if disabled */ u8 bMalloced; /* True if pStart obtained from sqlite3_malloc() */ - int nOut; /* Number of buffers currently checked out */ - int mxOut; /* Highwater mark for nOut */ - int anStat[3]; /* 0: hits. 1: size misses. 2: full misses */ + u32 nSlot; /* Number of lookaside slots allocated */ + u32 anStat[3]; /* 0: hits. 1: size misses. 2: full misses */ + LookasideSlot *pInit; /* List of buffers not previously used */ LookasideSlot *pFree; /* List of available buffers */ +#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE + LookasideSlot *pSmallInit; /* List of small buffers not previously used */ + LookasideSlot *pSmallFree; /* List of available small buffers */ + void *pMiddle; /* First byte past end of full-size buffers and + ** the first byte of LOOKASIDE_SMALL buffers */ +#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */ void *pStart; /* First byte of available memory space */ void *pEnd; /* First byte past end of available space */ + void *pTrueEnd; /* True value of pEnd, when db->pnBytesFreed!=0 */ }; struct LookasideSlot { LookasideSlot *pNext; /* Next buffer in the list of free buffers */ }; +#define DisableLookaside db->lookaside.bDisable++;db->lookaside.sz=0 +#define EnableLookaside db->lookaside.bDisable--;\ + db->lookaside.sz=db->lookaside.bDisable?0:db->lookaside.szTrue + +/* Size of the smaller allocations in two-size lookaside */ +#ifdef SQLITE_OMIT_TWOSIZE_LOOKASIDE +# define LOOKASIDE_SMALL 0 +#else +# define LOOKASIDE_SMALL 128 +#endif + /* -** A hash table for function definitions. +** A hash table for built-in function definitions. (Application-defined +** functions use a regular table table from hash.h.) ** ** Hash each FuncDef structure into one of the FuncDefHash.a[] slots. -** Collisions are on the FuncDef.pHash chain. +** Collisions are on the FuncDef.u.pHash chain. Use the SQLITE_FUNC_HASH() +** macro to compute a hash on the function name. */ +#define SQLITE_FUNC_HASH_SZ 23 struct FuncDefHash { - FuncDef *a[23]; /* Hash table for functions */ + FuncDef *a[SQLITE_FUNC_HASH_SZ]; /* Hash table for functions */ }; +#define SQLITE_FUNC_HASH(C,L) (((C)+(L))%SQLITE_FUNC_HASH_SZ) -#ifdef SQLITE_USER_AUTHENTICATION /* -** Information held in the "sqlite3" database connection object and used -** to manage user authentication. +** typedef for the authorization callback function. */ -typedef struct sqlite3_userauth sqlite3_userauth; -struct sqlite3_userauth { - u8 authLevel; /* Current authentication level */ - int nAuthPW; /* Size of the zAuthPW in bytes */ - char *zAuthPW; /* Password used to authenticate */ - char *zAuthUser; /* User name used to authenticate */ -}; +typedef int (*sqlite3_xauth)(void*,int,const char*,const char*,const char*, + const char*); -/* Allowed values for sqlite3_userauth.authLevel */ -#define UAUTH_Unknown 0 /* Authentication not yet checked */ -#define UAUTH_Fail 1 /* User authentication failed */ -#define UAUTH_User 2 /* Authenticated as a normal user */ -#define UAUTH_Admin 3 /* Authenticated as an administrator */ - -/* Functions used only by user authorization logic */ -int sqlite3UserAuthTable(const char*); -int sqlite3UserAuthCheckLogin(sqlite3*,const char*,u8*); -void sqlite3UserAuthInit(sqlite3*); -void sqlite3CryptFunc(sqlite3_context*,int,sqlite3_value**); - -#endif /* SQLITE_USER_AUTHENTICATION */ - -/* -** typedef for the authorization callback function. +#ifndef SQLITE_OMIT_DEPRECATED +/* This is an extra SQLITE_TRACE macro that indicates "legacy" tracing +** in the style of sqlite3_trace() */ -#ifdef SQLITE_USER_AUTHENTICATION - typedef int (*sqlite3_xauth)(void*,int,const char*,const char*,const char*, - const char*, const char*); +#define SQLITE_TRACE_LEGACY 0x40 /* Use the legacy xTrace */ +#define SQLITE_TRACE_XPROFILE 0x80 /* Use the legacy xProfile */ #else - typedef int (*sqlite3_xauth)(void*,int,const char*,const char*,const char*, - const char*); -#endif +#define SQLITE_TRACE_LEGACY 0 +#define SQLITE_TRACE_XPROFILE 0 +#endif /* SQLITE_OMIT_DEPRECATED */ +#define SQLITE_TRACE_NONLEGACY_MASK 0x0f /* Normal flags */ +/* +** Maximum number of sqlite3.aDb[] entries. This is the number of attached +** databases plus 2 for "main" and "temp". +*/ +#define SQLITE_MAX_DB (SQLITE_MAX_ATTACHED+2) /* ** Each database connection is an instance of the following structure. @@ -1163,17 +1662,21 @@ void sqlite3CryptFunc(sqlite3_context*,int,sqlite3_value**); struct sqlite3 { sqlite3_vfs *pVfs; /* OS Interface */ struct Vdbe *pVdbe; /* List of active virtual machines */ - CollSeq *pDfltColl; /* The default collating sequence (BINARY) */ + CollSeq *pDfltColl; /* BINARY collseq for the database encoding */ sqlite3_mutex *mutex; /* Connection mutex */ Db *aDb; /* All backends */ int nDb; /* Number of backends currently in use */ - int flags; /* Miscellaneous flags. See below */ + u32 mDbFlags; /* flags recording internal state */ + u64 flags; /* flags settable by pragmas. See below */ i64 lastRowid; /* ROWID of most recent insert (see above) */ i64 szMmap; /* Default mmap_size setting */ + u32 nSchemaLock; /* Do not reset the schema when non-zero */ unsigned int openFlags; /* Flags passed to sqlite3_vfs.xOpen() */ int errCode; /* Most recent error code (SQLITE_*) */ + int errByteOffset; /* Byte offset of error in SQL statement */ int errMask; /* & result codes with this before returning */ - u16 dbOptFlags; /* Flags to enable/disable optimizations */ + int iSysErrno; /* Errno value from last system error */ + u32 dbOptFlags; /* Flags to enable/disable optimizations */ u8 enc; /* Text encoding */ u8 autoCommit; /* The auto-commit flag. */ u8 temp_store; /* 1: file 2: memory 0: default */ @@ -1184,18 +1687,23 @@ struct sqlite3 { u8 suppressErr; /* Do not issue error messages if true */ u8 vtabOnConflict; /* Value to return for s3_vtab_on_conflict() */ u8 isTransactionSavepoint; /* True if the outermost savepoint is a TS */ + u8 mTrace; /* zero or more SQLITE_TRACE flags */ + u8 noSharedCache; /* True if no shared-cache backends */ + u8 nSqlExec; /* Number of pending OP_SqlExec opcodes */ + u8 eOpenState; /* Current condition of the connection */ int nextPagesize; /* Pagesize after VACUUM if >0 */ - u32 magic; /* Magic number for detect library misuse */ - int nChange; /* Value returned by sqlite3_changes() */ - int nTotalChange; /* Value returned by sqlite3_total_changes() */ + i64 nChange; /* Value returned by sqlite3_changes() */ + i64 nTotalChange; /* Value returned by sqlite3_total_changes() */ int aLimit[SQLITE_N_LIMIT]; /* Limits */ int nMaxSorterMmap; /* Maximum size of regions mapped by sorter */ struct sqlite3InitInfo { /* Information used during initialization */ - int newTnum; /* Rootpage of table being initialized */ + Pgno newTnum; /* Rootpage of table being initialized */ u8 iDb; /* Which db file is being initialized */ u8 busy; /* TRUE if currently initializing */ - u8 orphanTrigger; /* Last statement is orphaned TEMP trigger */ - u8 imposterTable; /* Building an imposter table */ + unsigned orphanTrigger : 1; /* Last statement is orphaned TEMP trigger */ + unsigned imposterTable : 2; /* Building an imposter table */ + unsigned reopenMemdb : 1; /* ATTACH is really a reopen using MemDB */ + const char **azInit; /* "type", "name", and "tbl_name" columns */ } init; int nVdbeActive; /* Number of VDBEs currently running */ int nVdbeRead; /* Number of active VDBEs that read or write */ @@ -1204,16 +1712,32 @@ struct sqlite3 { int nVDestroy; /* Number of active OP_VDestroy operations */ int nExtension; /* Number of loaded extensions */ void **aExtension; /* Array of shared library handles */ - void (*xTrace)(void*,const char*); /* Trace function */ - void *pTraceArg; /* Argument to the trace function */ + union { + void (*xLegacy)(void*,const char*); /* mTrace==SQLITE_TRACE_LEGACY */ + int (*xV2)(u32,void*,void*,void*); /* All other mTrace values */ + } trace; + void *pTraceArg; /* Argument to the trace function */ +#ifndef SQLITE_OMIT_DEPRECATED void (*xProfile)(void*,const char*,u64); /* Profiling function */ void *pProfileArg; /* Argument to profile function */ - void *pCommitArg; /* Argument to xCommitCallback() */ +#endif + void *pCommitArg; /* Argument to xCommitCallback() */ int (*xCommitCallback)(void*); /* Invoked at every commit. */ - void *pRollbackArg; /* Argument to xRollbackCallback() */ + void *pRollbackArg; /* Argument to xRollbackCallback() */ void (*xRollbackCallback)(void*); /* Invoked at every commit. */ void *pUpdateArg; void (*xUpdateCallback)(void*,int, const char*,const char*,sqlite_int64); + void *pAutovacPagesArg; /* Client argument to autovac_pages */ + void (*xAutovacDestr)(void*); /* Destructor for pAutovacPAgesArg */ + unsigned int (*xAutovacPages)(void*,const char*,u32,u32,u32); + Parse *pParse; /* Current parse */ +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK + void *pPreUpdateArg; /* First argument to xPreUpdateCallback */ + void (*xPreUpdateCallback)( /* Registered using sqlite3_preupdate_hook() */ + void*,sqlite3*,int,char const*,char const*,sqlite3_int64,sqlite3_int64 + ); + PreUpdate *pPreUpdate; /* Context for active pre-update callback */ +#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ #ifndef SQLITE_OMIT_WAL int (*xWalCallback)(void *, sqlite3 *, const char *, int); void *pWalArg; @@ -1241,22 +1765,29 @@ struct sqlite3 { Hash aModule; /* populated by sqlite3_create_module() */ VtabCtx *pVtabCtx; /* Context for active vtab connect/create */ VTable **aVTrans; /* Virtual tables with open transactions */ - VTable *pDisconnect; /* Disconnect these in next sqlite3_prepare() */ + VTable *pDisconnect; /* Disconnect these in next sqlite3_prepare() */ #endif - FuncDefHash aFunc; /* Hash table of connection functions */ + Hash aFunc; /* Hash table of connection functions */ Hash aCollSeq; /* All collating sequences */ BusyHandler busyHandler; /* Busy callback */ Db aDbStatic[2]; /* Static space for the 2 default backends */ Savepoint *pSavepoint; /* List of active savepoints */ + int nAnalysisLimit; /* Number of index rows to ANALYZE */ int busyTimeout; /* Busy handler timeout, in msec */ +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT + int setlkTimeout; /* Blocking lock timeout, in msec. -1 -> inf. */ + int setlkFlags; /* Flags passed to setlk_timeout() */ +#endif int nSavepoint; /* Number of non-transaction savepoints */ int nStatement; /* Number of nested statement-transactions */ i64 nDeferredCons; /* Net deferred constraints this transaction. */ i64 nDeferredImmCons; /* Net deferred immediate constraints */ int *pnBytesFreed; /* If not NULL, increment this in DbFree() */ + DbClientData *pDbData; /* sqlite3_set_clientdata() content */ + u64 nSpill; /* TEMP content spilled to disk */ #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY - /* The following variables are all protected by the STATIC_MASTER - ** mutex, not by sqlite3.mutex. They are used by code in notify.c. + /* The following variables are all protected by the STATIC_MAIN + ** mutex, not by sqlite3.mutex. They are used by code in notify.c. ** ** When X.pUnlockConnection==Y, that means that X is waiting for Y to ** unlock so that it can proceed. @@ -1271,9 +1802,6 @@ struct sqlite3 { void (*xUnlockNotify)(void **, int); /* Unlock notify callback */ sqlite3 *pNextBlocked; /* Next in list of all blocked connections */ #endif -#ifdef SQLITE_USER_AUTHENTICATION - sqlite3_userauth auth; /* User authentication information */ -#endif }; /* @@ -1282,73 +1810,132 @@ struct sqlite3 { #define SCHEMA_ENC(db) ((db)->aDb[0].pSchema->enc) #define ENC(db) ((db)->enc) +/* +** A u64 constant where the lower 32 bits are all zeros. Only the +** upper 32 bits are included in the argument. Necessary because some +** C-compilers still do not accept LL integer literals. +*/ +#define HI(X) ((u64)(X)<<32) + /* ** Possible values for the sqlite3.flags. +** +** Value constraints (enforced via assert()): +** SQLITE_FullFSync == PAGER_FULLFSYNC +** SQLITE_CkptFullFSync == PAGER_CKPT_FULLFSYNC +** SQLITE_CacheSpill == PAGER_CACHE_SPILL */ -#define SQLITE_VdbeTrace 0x00000001 /* True to trace VDBE execution */ -#define SQLITE_InternChanges 0x00000002 /* Uncommitted Hash table changes */ +#define SQLITE_WriteSchema 0x00000001 /* OK to update SQLITE_SCHEMA */ +#define SQLITE_LegacyFileFmt 0x00000002 /* Create new databases in format 1 */ #define SQLITE_FullColNames 0x00000004 /* Show full column names on SELECT */ #define SQLITE_FullFSync 0x00000008 /* Use full fsync on the backend */ #define SQLITE_CkptFullFSync 0x00000010 /* Use full fsync for checkpoint */ #define SQLITE_CacheSpill 0x00000020 /* OK to spill pager cache */ #define SQLITE_ShortColNames 0x00000040 /* Show short columns names */ -#define SQLITE_CountRows 0x00000080 /* Count rows changed by INSERT, */ - /* DELETE, or UPDATE and return */ - /* the count using a callback. */ +#define SQLITE_TrustedSchema 0x00000080 /* Allow unsafe functions and + ** vtabs in the schema definition */ #define SQLITE_NullCallback 0x00000100 /* Invoke the callback once if the */ /* result set is empty */ -#define SQLITE_SqlTrace 0x00000200 /* Debug print SQL as it executes */ -#define SQLITE_VdbeListing 0x00000400 /* Debug listings of VDBE programs */ -#define SQLITE_WriteSchema 0x00000800 /* OK to update SQLITE_MASTER */ -#define SQLITE_VdbeAddopTrace 0x00001000 /* Trace sqlite3VdbeAddOp() calls */ -#define SQLITE_IgnoreChecks 0x00002000 /* Do not enforce check constraints */ -#define SQLITE_ReadUncommitted 0x0004000 /* For shared-cache mode */ -#define SQLITE_LegacyFileFmt 0x00008000 /* Create new databases in format 1 */ -#define SQLITE_RecoveryMode 0x00010000 /* Ignore schema errors */ -#define SQLITE_ReverseOrder 0x00020000 /* Reverse unordered SELECTs */ -#define SQLITE_RecTriggers 0x00040000 /* Enable recursive triggers */ -#define SQLITE_ForeignKeys 0x00080000 /* Enforce foreign key constraints */ -#define SQLITE_AutoIndex 0x00100000 /* Enable automatic indexes */ -#define SQLITE_PreferBuiltin 0x00200000 /* Preference to built-in funcs */ -#define SQLITE_LoadExtension 0x00400000 /* Enable load_extension */ -#define SQLITE_EnableTrigger 0x00800000 /* True to enable triggers */ -#define SQLITE_DeferFKs 0x01000000 /* Defer all FK constraints */ -#define SQLITE_QueryOnly 0x02000000 /* Disable database changes */ -#define SQLITE_VdbeEQP 0x04000000 /* Debug EXPLAIN QUERY PLAN */ -#define SQLITE_Vacuum 0x08000000 /* Currently in a VACUUM */ -#define SQLITE_CellSizeCk 0x10000000 /* Check btree cell sizes on load */ +#define SQLITE_IgnoreChecks 0x00000200 /* Do not enforce check constraints */ +#define SQLITE_StmtScanStatus 0x00000400 /* Enable stmt_scanstats() counters */ +#define SQLITE_NoCkptOnClose 0x00000800 /* No checkpoint on close()/DETACH */ +#define SQLITE_ReverseOrder 0x00001000 /* Reverse unordered SELECTs */ +#define SQLITE_RecTriggers 0x00002000 /* Enable recursive triggers */ +#define SQLITE_ForeignKeys 0x00004000 /* Enforce foreign key constraints */ +#define SQLITE_AutoIndex 0x00008000 /* Enable automatic indexes */ +#define SQLITE_LoadExtension 0x00010000 /* Enable load_extension */ +#define SQLITE_LoadExtFunc 0x00020000 /* Enable load_extension() SQL func */ +#define SQLITE_EnableTrigger 0x00040000 /* True to enable triggers */ +#define SQLITE_DeferFKs 0x00080000 /* Defer all FK constraints */ +#define SQLITE_QueryOnly 0x00100000 /* Disable database changes */ +#define SQLITE_CellSizeCk 0x00200000 /* Check btree cell sizes on load */ +#define SQLITE_Fts3Tokenizer 0x00400000 /* Enable fts3_tokenizer(2) */ +#define SQLITE_EnableQPSG 0x00800000 /* Query Planner Stability Guarantee*/ +#define SQLITE_TriggerEQP 0x01000000 /* Show trigger EXPLAIN QUERY PLAN */ +#define SQLITE_ResetDatabase 0x02000000 /* Reset the database */ +#define SQLITE_LegacyAlter 0x04000000 /* Legacy ALTER TABLE behaviour */ +#define SQLITE_NoSchemaError 0x08000000 /* Do not report schema parse errors*/ +#define SQLITE_Defensive 0x10000000 /* Input SQL is likely hostile */ +#define SQLITE_DqsDDL 0x20000000 /* dbl-quoted strings allowed in DDL*/ +#define SQLITE_DqsDML 0x40000000 /* dbl-quoted strings allowed in DML*/ +#define SQLITE_EnableView 0x80000000 /* Enable the use of views */ +#define SQLITE_CountRows HI(0x00001) /* Count rows changed by INSERT, */ + /* DELETE, or UPDATE and return */ + /* the count using a callback. */ +#define SQLITE_CorruptRdOnly HI(0x00002) /* Prohibit writes due to error */ +#define SQLITE_ReadUncommit HI(0x00004) /* READ UNCOMMITTED in shared-cache */ +#define SQLITE_FkNoAction HI(0x00008) /* Treat all FK as NO ACTION */ +#define SQLITE_AttachCreate HI(0x00010) /* ATTACH allowed to create new dbs */ +#define SQLITE_AttachWrite HI(0x00020) /* ATTACH allowed to open for write */ +#define SQLITE_Comments HI(0x00040) /* Enable SQL comments */ + +/* Flags used only if debugging */ +#ifdef SQLITE_DEBUG +#define SQLITE_SqlTrace HI(0x0100000) /* Debug print SQL as it executes */ +#define SQLITE_VdbeListing HI(0x0200000) /* Debug listings of VDBE progs */ +#define SQLITE_VdbeTrace HI(0x0400000) /* True to trace VDBE execution */ +#define SQLITE_VdbeAddopTrace HI(0x0800000) /* Trace sqlite3VdbeAddOp() calls */ +#define SQLITE_VdbeEQP HI(0x1000000) /* Debug EXPLAIN QUERY PLAN */ +#define SQLITE_ParserTrace HI(0x2000000) /* PRAGMA parser_trace=ON */ +#endif +/* +** Allowed values for sqlite3.mDbFlags +*/ +#define DBFLAG_SchemaChange 0x0001 /* Uncommitted Hash table changes */ +#define DBFLAG_PreferBuiltin 0x0002 /* Preference to built-in funcs */ +#define DBFLAG_Vacuum 0x0004 /* Currently in a VACUUM */ +#define DBFLAG_VacuumInto 0x0008 /* Currently running VACUUM INTO */ +#define DBFLAG_SchemaKnownOk 0x0010 /* Schema is known to be valid */ +#define DBFLAG_InternalFunc 0x0020 /* Allow use of internal functions */ +#define DBFLAG_EncodingFixed 0x0040 /* No longer possible to change enc. */ /* ** Bits of the sqlite3.dbOptFlags field that are used by the ** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to ** selectively disable various optimizations. */ -#define SQLITE_QueryFlattener 0x0001 /* Query flattening */ -#define SQLITE_ColumnCache 0x0002 /* Column cache */ -#define SQLITE_GroupByOrder 0x0004 /* GROUPBY cover of ORDERBY */ -#define SQLITE_FactorOutConst 0x0008 /* Constant factoring */ -/* not used 0x0010 // Was: SQLITE_IdxRealAsInt */ -#define SQLITE_DistinctOpt 0x0020 /* DISTINCT using indexes */ -#define SQLITE_CoverIdxScan 0x0040 /* Covering index scans */ -#define SQLITE_OrderByIdxJoin 0x0080 /* ORDER BY of joins via index */ -#define SQLITE_SubqCoroutine 0x0100 /* Evaluate subqueries as coroutines */ -#define SQLITE_Transitive 0x0200 /* Transitive constraints */ -#define SQLITE_OmitNoopJoin 0x0400 /* Omit unused tables in joins */ -#define SQLITE_Stat34 0x0800 /* Use STAT3 or STAT4 data */ -#define SQLITE_CursorHints 0x2000 /* Add OP_CursorHint opcodes */ -#define SQLITE_AllOpts 0xffff /* All optimizations */ +#define SQLITE_QueryFlattener 0x00000001 /* Query flattening */ +#define SQLITE_WindowFunc 0x00000002 /* Use xInverse for window functions */ +#define SQLITE_GroupByOrder 0x00000004 /* GROUPBY cover of ORDERBY */ +#define SQLITE_FactorOutConst 0x00000008 /* Constant factoring */ +#define SQLITE_DistinctOpt 0x00000010 /* DISTINCT using indexes */ +#define SQLITE_CoverIdxScan 0x00000020 /* Covering index scans */ +#define SQLITE_OrderByIdxJoin 0x00000040 /* ORDER BY of joins via index */ +#define SQLITE_Transitive 0x00000080 /* Transitive constraints */ +#define SQLITE_OmitNoopJoin 0x00000100 /* Omit unused tables in joins */ +#define SQLITE_CountOfView 0x00000200 /* The count-of-view optimization */ +#define SQLITE_CursorHints 0x00000400 /* Add OP_CursorHint opcodes */ +#define SQLITE_Stat4 0x00000800 /* Use STAT4 data */ + /* TH3 expects this value ^^^^^^^^^^ to be 0x0000800. Don't change it */ +#define SQLITE_PushDown 0x00001000 /* WHERE-clause push-down opt */ +#define SQLITE_SimplifyJoin 0x00002000 /* Convert LEFT JOIN to JOIN */ +#define SQLITE_SkipScan 0x00004000 /* Skip-scans */ +#define SQLITE_PropagateConst 0x00008000 /* The constant propagation opt */ +#define SQLITE_MinMaxOpt 0x00010000 /* The min/max optimization */ +#define SQLITE_SeekScan 0x00020000 /* The OP_SeekScan optimization */ +#define SQLITE_OmitOrderBy 0x00040000 /* Omit pointless ORDER BY */ + /* TH3 expects this value ^^^^^^^^^^ to be 0x40000. Coordinate any change */ +#define SQLITE_BloomFilter 0x00080000 /* Use a Bloom filter on searches */ +#define SQLITE_BloomPulldown 0x00100000 /* Run Bloom filters early */ +#define SQLITE_BalancedMerge 0x00200000 /* Balance multi-way merges */ +#define SQLITE_ReleaseReg 0x00400000 /* Use OP_ReleaseReg for testing */ +#define SQLITE_FlttnUnionAll 0x00800000 /* Disable the UNION ALL flattener */ + /* TH3 expects this value ^^^^^^^^^^ See flatten04.test */ +#define SQLITE_IndexedExpr 0x01000000 /* Pull exprs from index when able */ +#define SQLITE_Coroutines 0x02000000 /* Co-routines for subqueries */ +#define SQLITE_NullUnusedCols 0x04000000 /* NULL unused columns in subqueries */ +#define SQLITE_OnePass 0x08000000 /* Single-pass DELETE and UPDATE */ +#define SQLITE_OrderBySubq 0x10000000 /* ORDER BY in subquery helps outer */ +#define SQLITE_StarQuery 0x20000000 /* Heurists for star queries */ +#define SQLITE_ExistsToJoin 0x40000000 /* The EXISTS-to-JOIN optimization */ +#define SQLITE_AllOpts 0xffffffff /* All optimizations */ /* ** Macros for testing whether or not optimizations are enabled or disabled. */ -#ifndef SQLITE_OMIT_BUILTIN_TEST #define OptimizationDisabled(db, mask) (((db)->dbOptFlags&(mask))!=0) #define OptimizationEnabled(db, mask) (((db)->dbOptFlags&(mask))==0) -#else -#define OptimizationDisabled(db, mask) 0 -#define OptimizationEnabled(db, mask) 1 -#endif /* ** Return true if it OK to factor constant expressions into the initialization @@ -1356,41 +1943,48 @@ struct sqlite3 { */ #define ConstFactorOk(P) ((P)->okConstFactor) -/* -** Possible values for the sqlite.magic field. -** The numbers are obtained at random and have no special meaning, other -** than being distinct from one another. +/* Possible values for the sqlite3.eOpenState field. +** The numbers are randomly selected such that a minimum of three bits must +** change to convert any number to another or to zero */ -#define SQLITE_MAGIC_OPEN 0xa029a697 /* Database is open */ -#define SQLITE_MAGIC_CLOSED 0x9f3c2d33 /* Database is closed */ -#define SQLITE_MAGIC_SICK 0x4b771290 /* Error and awaiting close */ -#define SQLITE_MAGIC_BUSY 0xf03b7906 /* Database currently in use */ -#define SQLITE_MAGIC_ERROR 0xb5357930 /* An SQLITE_MISUSE error occurred */ -#define SQLITE_MAGIC_ZOMBIE 0x64cffc7f /* Close with last statement close */ +#define SQLITE_STATE_OPEN 0x76 /* Database is open */ +#define SQLITE_STATE_CLOSED 0xce /* Database is closed */ +#define SQLITE_STATE_SICK 0xba /* Error and awaiting close */ +#define SQLITE_STATE_BUSY 0x6d /* Database currently in use */ +#define SQLITE_STATE_ERROR 0xd5 /* An SQLITE_MISUSE error occurred */ +#define SQLITE_STATE_ZOMBIE 0xa7 /* Close with last statement close */ /* ** Each SQL function is defined by an instance of the following -** structure. A pointer to this structure is stored in the sqlite.aFunc -** hash table. When multiple functions have the same name, the hash table -** points to a linked list of these structures. +** structure. For global built-in functions (ex: substr(), max(), count()) +** a pointer to this structure is held in the sqlite3BuiltinFunctions object. +** For per-connection application-defined functions, a pointer to this +** structure is held in the db->aHash hash table. +** +** The u.pHash field is used by the global built-ins. The u.pDestructor +** field is used by per-connection app-def functions. */ struct FuncDef { i16 nArg; /* Number of arguments. -1 means unlimited */ - u16 funcFlags; /* Some combination of SQLITE_FUNC_* */ + u32 funcFlags; /* Some combination of SQLITE_FUNC_* */ void *pUserData; /* User data parameter */ FuncDef *pNext; /* Next function with same name */ void (*xSFunc)(sqlite3_context*,int,sqlite3_value**); /* func or agg-step */ void (*xFinalize)(sqlite3_context*); /* Agg finalizer */ - char *zName; /* SQL name of the function. */ - FuncDef *pHash; /* Next with a different name but the same hash */ - FuncDestructor *pDestructor; /* Reference counted destructor function */ + void (*xValue)(sqlite3_context*); /* Current agg value */ + void (*xInverse)(sqlite3_context*,int,sqlite3_value**); /* inverse agg-step */ + const char *zName; /* SQL name of the function. */ + union { + FuncDef *pHash; /* Next with a different name but the same hash */ + FuncDestructor *pDestructor; /* Reference counted destructor function */ + } u; /* pHash if SQLITE_FUNC_BUILTIN, pDestructor otherwise */ }; /* ** This structure encapsulates a user-function destructor callback (as ** configured using create_function_v2()) and a reference counter. When ** create_function_v2() is called to create a function with a destructor, -** a single object of this type is allocated. FuncDestructor.nRef is set to +** a single object of this type is allocated. FuncDestructor.nRef is set to ** the number of FuncDef objects created (either 1 or 3, depending on whether ** or not the specified encoding is SQLITE_ANY). The FuncDef.pDestructor ** member of each of the new FuncDef objects is set to point to the allocated @@ -1411,6 +2005,23 @@ struct FuncDestructor { ** values must correspond to OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG. And ** SQLITE_FUNC_CONSTANT must be the same as SQLITE_DETERMINISTIC. There ** are assert() statements in the code to verify this. +** +** Value constraints (enforced via assert()): +** SQLITE_FUNC_MINMAX == NC_MinMaxAgg == SF_MinMaxAgg +** SQLITE_FUNC_ANYORDER == NC_OrderAgg == SF_OrderByReqd +** SQLITE_FUNC_LENGTH == OPFLAG_LENGTHARG +** SQLITE_FUNC_TYPEOF == OPFLAG_TYPEOFARG +** SQLITE_FUNC_BYTELEN == OPFLAG_BYTELENARG +** SQLITE_FUNC_CONSTANT == SQLITE_DETERMINISTIC from the API +** SQLITE_FUNC_DIRECT == SQLITE_DIRECTONLY from the API +** SQLITE_FUNC_UNSAFE == SQLITE_INNOCUOUS -- opposite meanings!!! +** SQLITE_FUNC_ENCMASK depends on SQLITE_UTF* macros in the API +** +** Note that even though SQLITE_FUNC_UNSAFE and SQLITE_INNOCUOUS have the +** same bit value, their meanings are inverted. SQLITE_FUNC_UNSAFE is +** used internally and if set means that the function has side effects. +** SQLITE_INNOCUOUS is used by application code and means "not unsafe". +** See multiple instances of tag-20230109-1. */ #define SQLITE_FUNC_ENCMASK 0x0003 /* SQLITE_UTF8, SQLITE_UTF16BE or UTF16LE */ #define SQLITE_FUNC_LIKE 0x0004 /* Candidate for the LIKE optimization */ @@ -1419,33 +2030,81 @@ struct FuncDestructor { #define SQLITE_FUNC_NEEDCOLL 0x0020 /* sqlite3GetFuncCollSeq() might be called*/ #define SQLITE_FUNC_LENGTH 0x0040 /* Built-in length() function */ #define SQLITE_FUNC_TYPEOF 0x0080 /* Built-in typeof() function */ +#define SQLITE_FUNC_BYTELEN 0x00c0 /* Built-in octet_length() function */ #define SQLITE_FUNC_COUNT 0x0100 /* Built-in count(*) aggregate */ -#define SQLITE_FUNC_COALESCE 0x0200 /* Built-in coalesce() or ifnull() */ +/* 0x0200 -- available for reuse */ #define SQLITE_FUNC_UNLIKELY 0x0400 /* Built-in unlikely() function */ #define SQLITE_FUNC_CONSTANT 0x0800 /* Constant inputs give a constant output */ #define SQLITE_FUNC_MINMAX 0x1000 /* True for min() and max() aggregates */ #define SQLITE_FUNC_SLOCHNG 0x2000 /* "Slow Change". Value constant during a ** single query - might change over time */ +#define SQLITE_FUNC_TEST 0x4000 /* Built-in testing functions */ +#define SQLITE_FUNC_RUNONLY 0x8000 /* Cannot be used by valueFromFunction */ +#define SQLITE_FUNC_WINDOW 0x00010000 /* Built-in window-only function */ +#define SQLITE_FUNC_INTERNAL 0x00040000 /* For use by NestedParse() only */ +#define SQLITE_FUNC_DIRECT 0x00080000 /* Not for use in TRIGGERs or VIEWs */ +/* SQLITE_SUBTYPE 0x00100000 // Consumer of subtypes */ +#define SQLITE_FUNC_UNSAFE 0x00200000 /* Function has side effects */ +#define SQLITE_FUNC_INLINE 0x00400000 /* Functions implemented in-line */ +#define SQLITE_FUNC_BUILTIN 0x00800000 /* This is a built-in function */ +/* SQLITE_RESULT_SUBTYPE 0x01000000 // Generator of subtypes */ +#define SQLITE_FUNC_ANYORDER 0x08000000 /* count/min/max aggregate */ + +/* Identifier numbers for each in-line function */ +#define INLINEFUNC_coalesce 0 +#define INLINEFUNC_implies_nonnull_row 1 +#define INLINEFUNC_expr_implies_expr 2 +#define INLINEFUNC_expr_compare 3 +#define INLINEFUNC_affinity 4 +#define INLINEFUNC_iif 5 +#define INLINEFUNC_sqlite_offset 6 +#define INLINEFUNC_unlikely 99 /* Default case */ /* ** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are ** used to create the initializers for the FuncDef structures. ** ** FUNCTION(zName, nArg, iArg, bNC, xFunc) -** Used to create a scalar function definition of a function zName +** Used to create a scalar function definition of a function zName ** implemented by C function xFunc that accepts nArg arguments. The ** value passed as iArg is cast to a (void*) and made available -** as the user-data (sqlite3_user_data()) for the function. If +** as the user-data (sqlite3_user_data()) for the function. If ** argument bNC is true, then the SQLITE_FUNC_NEEDCOLL flag is set. ** ** VFUNCTION(zName, nArg, iArg, bNC, xFunc) ** Like FUNCTION except it omits the SQLITE_FUNC_CONSTANT flag. ** +** SFUNCTION(zName, nArg, iArg, bNC, xFunc) +** Like FUNCTION except it omits the SQLITE_FUNC_CONSTANT flag and +** adds the SQLITE_DIRECTONLY flag. +** +** INLINE_FUNC(zName, nArg, iFuncId, mFlags) +** zName is the name of a function that is implemented by in-line +** byte code rather than by the usual callbacks. The iFuncId +** parameter determines the function id. The mFlags parameter is +** optional SQLITE_FUNC_ flags for this function. +** +** TEST_FUNC(zName, nArg, iFuncId, mFlags) +** zName is the name of a test-only function implemented by in-line +** byte code rather than by the usual callbacks. The iFuncId +** parameter determines the function id. The mFlags parameter is +** optional SQLITE_FUNC_ flags for this function. +** ** DFUNCTION(zName, nArg, iArg, bNC, xFunc) ** Like FUNCTION except it omits the SQLITE_FUNC_CONSTANT flag and ** adds the SQLITE_FUNC_SLOCHNG flag. Used for date & time functions ** and functions like sqlite_version() that can change, but not during -** a single query. +** a single query. The iArg is ignored. The user-data is always set +** to a NULL pointer. The bNC parameter is not used. +** +** MFUNCTION(zName, nArg, xPtr, xFunc) +** For math-library functions. xPtr is an arbitrary pointer. +** +** PURE_DATE(zName, nArg, iArg, bNC, xFunc) +** Used for "pure" date/time functions, this macro is like DFUNCTION +** except that it does set the SQLITE_FUNC_CONSTANT flags. iArg is +** ignored and the user-data for these functions is set to an +** arbitrary non-NULL pointer. The bNC parameter is not used. ** ** AGGREGATE(zName, nArg, iArg, bNC, xStep, xFinal) ** Used to create an aggregate function definition implemented by @@ -1453,38 +2112,73 @@ struct FuncDestructor { ** are interpreted in the same way as the first 4 parameters to ** FUNCTION(). ** +** WAGGREGATE(zName, nArg, iArg, xStep, xFinal, xValue, xInverse) +** Used to create an aggregate function definition implemented by +** the C functions xStep and xFinal. The first four parameters +** are interpreted in the same way as the first 4 parameters to +** FUNCTION(). +** ** LIKEFUNC(zName, nArg, pArg, flags) -** Used to create a scalar function definition of a function zName -** that accepts nArg arguments and is implemented by a call to C +** Used to create a scalar function definition of a function zName +** that accepts nArg arguments and is implemented by a call to C ** function likeFunc. Argument pArg is cast to a (void *) and made ** available as the function user-data (sqlite3_user_data()). The ** FuncDef.flags variable is set to the value passed as the flags ** parameter. */ #define FUNCTION(zName, nArg, iArg, bNC, xFunc) \ - {nArg, SQLITE_FUNC_CONSTANT|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \ - SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, #zName, 0, 0} + {nArg, SQLITE_FUNC_BUILTIN|\ + SQLITE_FUNC_CONSTANT|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \ + SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} } #define VFUNCTION(zName, nArg, iArg, bNC, xFunc) \ - {nArg, SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \ - SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, #zName, 0, 0} + {nArg, SQLITE_FUNC_BUILTIN|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \ + SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} } +#define SFUNCTION(zName, nArg, iArg, bNC, xFunc) \ + {nArg, SQLITE_FUNC_BUILTIN|SQLITE_UTF8|SQLITE_DIRECTONLY|SQLITE_FUNC_UNSAFE, \ + SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} } +#define MFUNCTION(zName, nArg, xPtr, xFunc) \ + {nArg, SQLITE_FUNC_BUILTIN|SQLITE_FUNC_CONSTANT|SQLITE_UTF8, \ + xPtr, 0, xFunc, 0, 0, 0, #zName, {0} } +#define JFUNCTION(zName, nArg, bUseCache, bWS, bRS, bJsonB, iArg, xFunc) \ + {nArg, SQLITE_FUNC_BUILTIN|SQLITE_DETERMINISTIC|SQLITE_FUNC_CONSTANT|\ + SQLITE_UTF8|((bUseCache)*SQLITE_FUNC_RUNONLY)|\ + ((bRS)*SQLITE_SUBTYPE)|((bWS)*SQLITE_RESULT_SUBTYPE), \ + SQLITE_INT_TO_PTR(iArg|((bJsonB)*JSON_BLOB)),0,xFunc,0, 0, 0, #zName, {0} } +#define INLINE_FUNC(zName, nArg, iArg, mFlags) \ + {nArg, SQLITE_FUNC_BUILTIN|\ + SQLITE_UTF8|SQLITE_FUNC_INLINE|SQLITE_FUNC_CONSTANT|(mFlags), \ + SQLITE_INT_TO_PTR(iArg), 0, noopFunc, 0, 0, 0, #zName, {0} } +#define TEST_FUNC(zName, nArg, iArg, mFlags) \ + {nArg, SQLITE_FUNC_BUILTIN|\ + SQLITE_UTF8|SQLITE_FUNC_INTERNAL|SQLITE_FUNC_TEST| \ + SQLITE_FUNC_INLINE|SQLITE_FUNC_CONSTANT|(mFlags), \ + SQLITE_INT_TO_PTR(iArg), 0, noopFunc, 0, 0, 0, #zName, {0} } #define DFUNCTION(zName, nArg, iArg, bNC, xFunc) \ - {nArg, SQLITE_FUNC_SLOCHNG|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \ - SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, #zName, 0, 0} + {nArg, SQLITE_FUNC_BUILTIN|SQLITE_FUNC_SLOCHNG|SQLITE_UTF8, \ + 0, 0, xFunc, 0, 0, 0, #zName, {0} } +#define PURE_DATE(zName, nArg, iArg, bNC, xFunc) \ + {nArg, SQLITE_FUNC_BUILTIN|\ + SQLITE_FUNC_SLOCHNG|SQLITE_UTF8|SQLITE_FUNC_CONSTANT, \ + (void*)&sqlite3Config, 0, xFunc, 0, 0, 0, #zName, {0} } #define FUNCTION2(zName, nArg, iArg, bNC, xFunc, extraFlags) \ - {nArg,SQLITE_FUNC_CONSTANT|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL)|extraFlags,\ - SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, #zName, 0, 0} + {nArg, SQLITE_FUNC_BUILTIN|\ + SQLITE_FUNC_CONSTANT|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL)|extraFlags,\ + SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} } #define STR_FUNCTION(zName, nArg, pArg, bNC, xFunc) \ - {nArg, SQLITE_FUNC_SLOCHNG|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \ - pArg, 0, xFunc, 0, #zName, 0, 0} + {nArg, SQLITE_FUNC_BUILTIN|\ + SQLITE_FUNC_SLOCHNG|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \ + pArg, 0, xFunc, 0, 0, 0, #zName, {0} } #define LIKEFUNC(zName, nArg, arg, flags) \ - {nArg, SQLITE_FUNC_CONSTANT|SQLITE_UTF8|flags, \ - (void *)arg, 0, likeFunc, 0, #zName, 0, 0} -#define AGGREGATE(zName, nArg, arg, nc, xStep, xFinal) \ - {nArg, SQLITE_UTF8|(nc*SQLITE_FUNC_NEEDCOLL), \ - SQLITE_INT_TO_PTR(arg), 0, xStep,xFinal,#zName,0,0} -#define AGGREGATE2(zName, nArg, arg, nc, xStep, xFinal, extraFlags) \ - {nArg, SQLITE_UTF8|(nc*SQLITE_FUNC_NEEDCOLL)|extraFlags, \ - SQLITE_INT_TO_PTR(arg), 0, xStep,xFinal,#zName,0,0} + {nArg, SQLITE_FUNC_BUILTIN|SQLITE_FUNC_CONSTANT|SQLITE_UTF8|flags, \ + (void *)arg, 0, likeFunc, 0, 0, 0, #zName, {0} } +#define WAGGREGATE(zName, nArg, arg, nc, xStep, xFinal, xValue, xInverse, f) \ + {nArg, SQLITE_FUNC_BUILTIN|SQLITE_UTF8|(nc*SQLITE_FUNC_NEEDCOLL)|f, \ + SQLITE_INT_TO_PTR(arg), 0, xStep,xFinal,xValue,xInverse,#zName, {0}} +#define INTERNAL_FUNCTION(zName, nArg, xFunc) \ + {nArg, SQLITE_FUNC_BUILTIN|\ + SQLITE_FUNC_INTERNAL|SQLITE_UTF8|SQLITE_FUNC_CONSTANT, \ + 0, 0, xFunc, 0, 0, 0, #zName, {0} } + /* ** All current savepoints are stored in a linked list starting at @@ -1516,31 +2210,84 @@ struct Savepoint { struct Module { const sqlite3_module *pModule; /* Callback pointers */ const char *zName; /* Name passed to create_module() */ + int nRefModule; /* Number of pointers to this object */ void *pAux; /* pAux passed to create_module() */ void (*xDestroy)(void *); /* Module destructor function */ Table *pEpoTab; /* Eponymous table for this module */ }; /* -** information about each column of an SQL table is held in an instance -** of this structure. +** Information about each column of an SQL table is held in an instance +** of the Column structure, in the Table.aCol[] array. +** +** Definitions: +** +** "table column index" This is the index of the column in the +** Table.aCol[] array, and also the index of +** the column in the original CREATE TABLE stmt. +** +** "storage column index" This is the index of the column in the +** record BLOB generated by the OP_MakeRecord +** opcode. The storage column index is less than +** or equal to the table column index. It is +** equal if and only if there are no VIRTUAL +** columns to the left. +** +** Notes on zCnName: +** The zCnName field stores the name of the column, the datatype of the +** column, and the collating sequence for the column, in that order, all in +** a single allocation. Each string is 0x00 terminated. The datatype +** is only included if the COLFLAG_HASTYPE bit of colFlags is set and the +** collating sequence name is only included if the COLFLAG_HASCOLL bit is +** set. */ struct Column { - char *zName; /* Name of this column */ - Expr *pDflt; /* Default value of this column */ - char *zDflt; /* Original text of the default value */ - char *zType; /* Data type for this column */ - char *zColl; /* Collating sequence. If NULL, use the default */ - u8 notNull; /* An OE_ code for handling a NOT NULL constraint */ - char affinity; /* One of the SQLITE_AFF_... values */ - u8 szEst; /* Estimated size of value in this column. sizeof(INT)==1 */ - u8 colFlags; /* Boolean properties. See COLFLAG_ defines below */ + char *zCnName; /* Name of this column */ + unsigned notNull :4; /* An OE_ code for handling a NOT NULL constraint */ + unsigned eCType :4; /* One of the standard types */ + char affinity; /* One of the SQLITE_AFF_... values */ + u8 szEst; /* Est size of value in this column. sizeof(INT)==1 */ + u8 hName; /* Column name hash for faster lookup */ + u16 iDflt; /* 1-based index of DEFAULT. 0 means "none" */ + u16 colFlags; /* Boolean properties. See COLFLAG_ defines below */ }; -/* Allowed values for Column.colFlags: -*/ -#define COLFLAG_PRIMKEY 0x0001 /* Column is part of the primary key */ -#define COLFLAG_HIDDEN 0x0002 /* A hidden column in a virtual table */ +/* Allowed values for Column.eCType. +** +** Values must match entries in the global constant arrays +** sqlite3StdTypeLen[] and sqlite3StdType[]. Each value is one more +** than the offset into these arrays for the corresponding name. +** Adjust the SQLITE_N_STDTYPE value if adding or removing entries. +*/ +#define COLTYPE_CUSTOM 0 /* Type appended to zName */ +#define COLTYPE_ANY 1 +#define COLTYPE_BLOB 2 +#define COLTYPE_INT 3 +#define COLTYPE_INTEGER 4 +#define COLTYPE_REAL 5 +#define COLTYPE_TEXT 6 +#define SQLITE_N_STDTYPE 6 /* Number of standard types */ + +/* Allowed values for Column.colFlags. +** +** Constraints: +** TF_HasVirtual == COLFLAG_VIRTUAL +** TF_HasStored == COLFLAG_STORED +** TF_HasHidden == COLFLAG_HIDDEN +*/ +#define COLFLAG_PRIMKEY 0x0001 /* Column is part of the primary key */ +#define COLFLAG_HIDDEN 0x0002 /* A hidden column in a virtual table */ +#define COLFLAG_HASTYPE 0x0004 /* Type name follows column name */ +#define COLFLAG_UNIQUE 0x0008 /* Column def contains "UNIQUE" or "PK" */ +#define COLFLAG_SORTERREF 0x0010 /* Use sorter-refs with this column */ +#define COLFLAG_VIRTUAL 0x0020 /* GENERATED ALWAYS AS ... VIRTUAL */ +#define COLFLAG_STORED 0x0040 /* GENERATED ALWAYS AS ... STORED */ +#define COLFLAG_NOTAVAIL 0x0080 /* STORED column not yet calculated */ +#define COLFLAG_BUSY 0x0100 /* Blocks recursion on GENERATED columns */ +#define COLFLAG_HASCOLL 0x0200 /* Has collating sequence name in zCnName */ +#define COLFLAG_NOEXPAND 0x0400 /* Omit this column when expanding "*" */ +#define COLFLAG_GENERATED 0x0060 /* Combo: _STORED, _VIRTUAL */ +#define COLFLAG_NOINSERT 0x0062 /* Combo: _HIDDEN, _STORED, _VIRTUAL */ /* ** A "Collating Sequence" is defined by an instance of the following @@ -1571,7 +2318,7 @@ struct CollSeq { ** ** These used to have mnemonic name like 'i' for SQLITE_AFF_INTEGER and ** 't' for SQLITE_AFF_TEXT. But we can save a little space and improve -** the speed a little by numbering the values consecutively. +** the speed a little by numbering the values consecutively. ** ** But rather than start with 0 or 1, we begin with 'A'. That way, ** when multiple affinity types are concatenated into a string and @@ -1580,17 +2327,20 @@ struct CollSeq { ** Note also that the numeric types are grouped together so that testing ** for a numeric type is a single comparison. And the BLOB type is first. */ -#define SQLITE_AFF_BLOB 'A' -#define SQLITE_AFF_TEXT 'B' -#define SQLITE_AFF_NUMERIC 'C' -#define SQLITE_AFF_INTEGER 'D' -#define SQLITE_AFF_REAL 'E' +#define SQLITE_AFF_NONE 0x40 /* '@' */ +#define SQLITE_AFF_BLOB 0x41 /* 'A' */ +#define SQLITE_AFF_TEXT 0x42 /* 'B' */ +#define SQLITE_AFF_NUMERIC 0x43 /* 'C' */ +#define SQLITE_AFF_INTEGER 0x44 /* 'D' */ +#define SQLITE_AFF_REAL 0x45 /* 'E' */ +#define SQLITE_AFF_FLEXNUM 0x46 /* 'F' */ +#define SQLITE_AFF_DEFER 0x58 /* 'X' - defer computation until later */ #define sqlite3IsNumericAffinity(X) ((X)>=SQLITE_AFF_NUMERIC) /* ** The SQLITE_AFF_MASK values masks off the significant bits of an -** affinity value. +** affinity value. */ #define SQLITE_AFF_MASK 0x47 @@ -1604,26 +2354,25 @@ struct CollSeq { ** prove that the operands are always NOT NULL. */ #define SQLITE_JUMPIFNULL 0x10 /* jumps if either operand is NULL */ -#define SQLITE_STOREP2 0x20 /* Store result in reg[P2] rather than jump */ #define SQLITE_NULLEQ 0x80 /* NULL=NULL */ #define SQLITE_NOTNULL 0x90 /* Assert that operands are never NULL */ /* ** An object of this type is created for each virtual table present in -** the database schema. +** the database schema. ** ** If the database schema is shared, then there is one instance of this ** structure for each database connection (sqlite3*) that uses the shared ** schema. This is because each database connection requires its own unique -** instance of the sqlite3_vtab* handle used to access the virtual table -** implementation. sqlite3_vtab* handles can not be shared between -** database connections, even when the rest of the in-memory database +** instance of the sqlite3_vtab* handle used to access the virtual table +** implementation. sqlite3_vtab* handles can not be shared between +** database connections, even when the rest of the in-memory database ** schema is shared, as the implementation often stores the database ** connection handle passed to it via the xConnect() or xCreate() method ** during initialization internally. This database connection handle may -** then be used by the virtual table implementation to access real tables -** within the database. So that they appear as part of the callers -** transaction, these accesses need to be made via the same database +** then be used by the virtual table implementation to access real tables +** within the database. So that they appear as part of the callers +** transaction, these accesses need to be made via the same database ** connection as that used to execute SQL operations on the virtual table. ** ** All VTable objects that correspond to a single table in a shared @@ -1635,19 +2384,19 @@ struct CollSeq { ** sqlite3_vtab* handle in the compiled query. ** ** When an in-memory Table object is deleted (for example when the -** schema is being reloaded for some reason), the VTable objects are not -** deleted and the sqlite3_vtab* handles are not xDisconnect()ed +** schema is being reloaded for some reason), the VTable objects are not +** deleted and the sqlite3_vtab* handles are not xDisconnect()ed ** immediately. Instead, they are moved from the Table.pVTable list to ** another linked list headed by the sqlite3.pDisconnect member of the -** corresponding sqlite3 structure. They are then deleted/xDisconnected +** corresponding sqlite3 structure. They are then deleted/xDisconnected ** next time a statement is prepared using said sqlite3*. This is done ** to avoid deadlock issues involving multiple sqlite3.mutex mutexes. ** Refer to comments above function sqlite3VtabUnlockList() for an ** explanation as to why it is safe to add an entry to an sqlite3.pDisconnect ** list without holding the corresponding sqlite3.mutex mutex. ** -** The memory for objects of this type is always allocated by -** sqlite3DbMalloc(), using the connection handle stored in VTable.db as +** The memory for objects of this type is always allocated by +** sqlite3DbMalloc(), using the connection handle stored in VTable.db as ** the first argument. */ struct VTable { @@ -1656,45 +2405,61 @@ struct VTable { sqlite3_vtab *pVtab; /* Pointer to vtab instance */ int nRef; /* Number of pointers to this structure */ u8 bConstraint; /* True if constraints are supported */ + u8 bAllSchemas; /* True if might use any attached schema */ + u8 eVtabRisk; /* Riskiness of allowing hacker access */ int iSavepoint; /* Depth of the SAVEPOINT stack */ VTable *pNext; /* Next in linked list (see above) */ }; +/* Allowed values for VTable.eVtabRisk +*/ +#define SQLITE_VTABRISK_Low 0 +#define SQLITE_VTABRISK_Normal 1 +#define SQLITE_VTABRISK_High 2 + /* -** The schema for each SQL table and view is represented in memory -** by an instance of the following structure. +** The schema for each SQL table, virtual table, and view is represented +** in memory by an instance of the following structure. */ struct Table { char *zName; /* Name of the table or view */ Column *aCol; /* Information about each column */ Index *pIndex; /* List of SQL indexes on this table. */ - Select *pSelect; /* NULL for tables. Points to definition if a view. */ - FKey *pFKey; /* Linked list of all foreign keys in this table */ char *zColAff; /* String defining the affinity of each column */ ExprList *pCheck; /* All CHECK constraints */ /* ... also used as column name list in a VIEW */ - int tnum; /* Root BTree page for this table */ + Pgno tnum; /* Root BTree page for this table */ + u32 nTabRef; /* Number of pointers to this Table */ + u32 tabFlags; /* Mask of TF_* values */ i16 iPKey; /* If not negative, use aCol[iPKey] as the rowid */ i16 nCol; /* Number of columns in this table */ - u16 nRef; /* Number of pointers to this Table */ + i16 nNVCol; /* Number of columns that are not VIRTUAL */ LogEst nRowLogEst; /* Estimated rows in table - from sqlite_stat1 table */ LogEst szTabRow; /* Estimated size of each table row in bytes */ #ifdef SQLITE_ENABLE_COSTMULT LogEst costMult; /* Cost multiplier for using this table */ #endif - u8 tabFlags; /* Mask of TF_* values */ u8 keyConf; /* What to do in case of uniqueness conflict on iPKey */ -#ifndef SQLITE_OMIT_ALTERTABLE - int addColOffset; /* Offset in CREATE TABLE stmt to add a new column */ -#endif -#ifndef SQLITE_OMIT_VIRTUALTABLE - int nModuleArg; /* Number of arguments to the module */ - char **azModuleArg; /* 0: module 1: schema 2: vtab name 3...: args */ - VTable *pVTable; /* List of VTable objects. */ -#endif - Trigger *pTrigger; /* List of triggers stored in pSchema */ + u8 eTabType; /* 0: normal, 1: virtual, 2: view */ + union { + struct { /* Used by ordinary tables: */ + int addColOffset; /* Offset in CREATE TABLE stmt to add a new column */ + FKey *pFKey; /* Linked list of all foreign keys in this table */ + ExprList *pDfltList; /* DEFAULT clauses on various columns. + ** Or the AS clause for generated columns. */ + } tab; + struct { /* Used by views: */ + Select *pSelect; /* View definition */ + } view; + struct { /* Used by virtual tables only: */ + int nArg; /* Number of arguments to the module */ + char **azArg; /* 0: module 1: schema 2: vtab name 3...: args */ + VTable *p; /* List of VTable objects. */ + } vtab; + } u; + Trigger *pTrigger; /* List of triggers on this object */ Schema *pSchema; /* Schema that contains this table */ - Table *pNextZombie; /* Next on the Parse.pZombieTab list */ + u8 aHx[16]; /* Column aHt[K%sizeof(aHt)] might have hash K */ }; /* @@ -1704,17 +2469,43 @@ struct Table { ** followed by non-hidden columns. Example: "CREATE VIRTUAL TABLE x USING ** vtab1(a HIDDEN, b);". Since "b" is a non-hidden column but "a" is hidden, ** the TF_OOOHidden attribute would apply in this case. Such tables require -** special handling during INSERT processing. +** special handling during INSERT processing. The "OOO" means "Out Of Order". +** +** Constraints: +** +** TF_HasVirtual == COLFLAG_VIRTUAL +** TF_HasStored == COLFLAG_STORED +** TF_HasHidden == COLFLAG_HIDDEN +*/ +#define TF_Readonly 0x00000001 /* Read-only system table */ +#define TF_HasHidden 0x00000002 /* Has one or more hidden columns */ +#define TF_HasPrimaryKey 0x00000004 /* Table has a primary key */ +#define TF_Autoincrement 0x00000008 /* Integer primary key is autoincrement */ +#define TF_HasStat1 0x00000010 /* nRowLogEst set from sqlite_stat1 */ +#define TF_HasVirtual 0x00000020 /* Has one or more VIRTUAL columns */ +#define TF_HasStored 0x00000040 /* Has one or more STORED columns */ +#define TF_HasGenerated 0x00000060 /* Combo: HasVirtual + HasStored */ +#define TF_WithoutRowid 0x00000080 /* No rowid. PRIMARY KEY is the key */ +#define TF_MaybeReanalyze 0x00000100 /* Maybe run ANALYZE on this table */ +#define TF_NoVisibleRowid 0x00000200 /* No user-visible "rowid" column */ +#define TF_OOOHidden 0x00000400 /* Out-of-Order hidden columns */ +#define TF_HasNotNull 0x00000800 /* Contains NOT NULL constraints */ +#define TF_Shadow 0x00001000 /* True for a shadow table */ +#define TF_HasStat4 0x00002000 /* STAT4 info available for this table */ +#define TF_Ephemeral 0x00004000 /* An ephemeral table */ +#define TF_Eponymous 0x00008000 /* An eponymous virtual table */ +#define TF_Strict 0x00010000 /* STRICT mode */ +#define TF_Imposter 0x00020000 /* An imposter table */ + +/* +** Allowed values for Table.eTabType */ -#define TF_Readonly 0x01 /* Read-only system table */ -#define TF_Ephemeral 0x02 /* An ephemeral table */ -#define TF_HasPrimaryKey 0x04 /* Table has a primary key */ -#define TF_Autoincrement 0x08 /* Integer primary key is autoincrement */ -#define TF_Virtual 0x10 /* Is a virtual table */ -#define TF_WithoutRowid 0x20 /* No rowid. PRIMARY KEY is the key */ -#define TF_NoVisibleRowid 0x40 /* No user-visible "rowid" column */ -#define TF_OOOHidden 0x80 /* Out-of-Order hidden columns */ +#define TABTYP_NORM 0 /* Ordinary table */ +#define TABTYP_VTAB 1 /* Virtual table */ +#define TABTYP_VIEW 2 /* A view */ +#define IsView(X) ((X)->eTabType==TABTYP_VIEW) +#define IsOrdinaryTable(X) ((X)->eTabType==TABTYP_NORM) /* ** Test to see whether or not a table is a virtual table. This is @@ -1722,9 +2513,12 @@ struct Table { ** table support is omitted from the build. */ #ifndef SQLITE_OMIT_VIRTUALTABLE -# define IsVirtual(X) (((X)->tabFlags & TF_Virtual)!=0) +# define IsVirtual(X) ((X)->eTabType==TABTYP_VTAB) +# define ExprIsVtab(X) \ + ((X)->op==TK_COLUMN && (X)->y.pTab->eTabType==TABTYP_VTAB) #else # define IsVirtual(X) 0 +# define ExprIsVtab(X) 0 #endif /* @@ -1749,6 +2543,15 @@ struct Table { #define HasRowid(X) (((X)->tabFlags & TF_WithoutRowid)==0) #define VisibleRowid(X) (((X)->tabFlags & TF_NoVisibleRowid)==0) +/* Macro is true if the SQLITE_ALLOW_ROWID_IN_VIEW (mis-)feature is +** available. By default, this macro is false +*/ +#ifndef SQLITE_ALLOW_ROWID_IN_VIEW +# define ViewCanHaveRowid 0 +#else +# define ViewCanHaveRowid (sqlite3Config.mNoVisibleRowid==0) +#endif + /* ** Each foreign key constraint is an instance of the following structure. ** @@ -1791,9 +2594,13 @@ struct FKey { struct sColMap { /* Mapping of columns in pFrom to columns in zTo */ int iFrom; /* Index of column in pFrom */ char *zCol; /* Name of column in zTo. If NULL use PRIMARY KEY */ - } aCol[1]; /* One entry for each of nCol columns */ + } aCol[FLEXARRAY]; /* One entry for each of nCol columns */ }; +/* The size (in bytes) of an FKey object holding N columns. The answer +** does NOT include space to hold the zTo name. */ +#define SZ_FKEY(N) (offsetof(FKey,aCol)+(N)*sizeof(struct sColMap)) + /* ** SQLite supports many different ways to resolve a constraint ** error. ROLLBACK processing means that a constraint violation @@ -1808,16 +2615,22 @@ struct FKey { ** is returned. REPLACE means that preexisting database rows that caused ** a UNIQUE constraint violation are removed so that the new insert or ** update can proceed. Processing continues and no error is reported. +** UPDATE applies to insert operations only and means that the insert +** is omitted and the DO UPDATE clause of an upsert is run instead. ** -** RESTRICT, SETNULL, and CASCADE actions apply only to foreign keys. +** RESTRICT, SETNULL, SETDFLT, and CASCADE actions apply only to foreign keys. ** RESTRICT is the same as ABORT for IMMEDIATE foreign keys and the ** same as ROLLBACK for DEFERRED keys. SETNULL means that the foreign -** key is set to NULL. CASCADE means that a DELETE or UPDATE of the +** key is set to NULL. SETDFLT means that the foreign key is set +** to its default value. CASCADE means that a DELETE or UPDATE of the ** referenced table row is propagated into the row that holds the ** foreign key. -** +** +** The OE_Default value is a place holder that means to use whatever +** conflict resolution algorithm is required from context. +** ** The following symbolic values are used to record which type -** of action to take. +** of conflict resolution action to take. */ #define OE_None 0 /* There is no constraint to check */ #define OE_Rollback 1 /* Fail the operation and rollback the transaction */ @@ -1825,34 +2638,57 @@ struct FKey { #define OE_Fail 3 /* Stop the operation but leave all prior changes */ #define OE_Ignore 4 /* Ignore the error. Do not do the INSERT or UPDATE */ #define OE_Replace 5 /* Delete existing record, then do INSERT or UPDATE */ - -#define OE_Restrict 6 /* OE_Abort for IMMEDIATE, OE_Rollback for DEFERRED */ -#define OE_SetNull 7 /* Set the foreign key value to NULL */ -#define OE_SetDflt 8 /* Set the foreign key value to its default */ -#define OE_Cascade 9 /* Cascade the changes */ - -#define OE_Default 10 /* Do whatever the default action is */ +#define OE_Update 6 /* Process as a DO UPDATE in an upsert */ +#define OE_Restrict 7 /* OE_Abort for IMMEDIATE, OE_Rollback for DEFERRED */ +#define OE_SetNull 8 /* Set the foreign key value to NULL */ +#define OE_SetDflt 9 /* Set the foreign key value to its default */ +#define OE_Cascade 10 /* Cascade the changes */ +#define OE_Default 11 /* Do whatever the default action is */ /* ** An instance of the following structure is passed as the first -** argument to sqlite3VdbeKeyCompare and is used to control the +** argument to sqlite3VdbeKeyCompare and is used to control the ** comparison of the two index keys. ** -** Note that aSortOrder[] and aColl[] have nField+1 slots. There -** are nField slots for the columns of an index then one extra slot -** for the rowid at the end. +** The aSortOrder[] and aColl[] arrays have nAllField slots each. There +** are nKeyField slots for the columns of an index then extra slots +** for the rowid or key at the end. The aSortOrder array is located after +** the aColl[] array. +** +** If SQLITE_ENABLE_PREUPDATE_HOOK is defined, then aSortFlags might be NULL +** to indicate that this object is for use by a preupdate hook. When aSortFlags +** is NULL, then nAllField is uninitialized and no space is allocated for +** aColl[], so those fields may not be used. */ struct KeyInfo { u32 nRef; /* Number of references to this KeyInfo object */ u8 enc; /* Text encoding - one of the SQLITE_UTF* values */ - u16 nField; /* Number of key columns in the index */ - u16 nXField; /* Number of columns beyond the key columns */ + u16 nKeyField; /* Number of key columns in the index */ + u16 nAllField; /* Total columns, including key plus others */ sqlite3 *db; /* The database connection */ - u8 *aSortOrder; /* Sort order for each column. */ - CollSeq *aColl[1]; /* Collating sequence for each term of the key */ + u8 *aSortFlags; /* Sort order for each column. */ + CollSeq *aColl[FLEXARRAY]; /* Collating sequence for each term of the key */ }; +/* The size (in bytes) of a KeyInfo object with up to N fields. This includes +** the main body of the KeyInfo object and the aColl[] array of N elements, +** but does not count the memory used to hold aSortFlags[]. */ +#define SZ_KEYINFO(N) (offsetof(KeyInfo,aColl) + (N)*sizeof(CollSeq*)) + +/* The size of a bare KeyInfo with no aColl[] entries */ +#if FLEXARRAY+1 > 1 +# define SZ_KEYINFO_0 offsetof(KeyInfo,aColl) +#else +# define SZ_KEYINFO_0 sizeof(KeyInfo) +#endif + +/* +** Allowed bit values for entries in the KeyInfo.aSortFlags[] array. +*/ +#define KEYINFO_ORDER_DESC 0x01 /* DESC sort order */ +#define KEYINFO_ORDER_BIGNULL 0x02 /* NULL is larger than any other value */ + /* ** This object holds a record which has been parsed out into individual ** fields, for the purposes of doing a comparison. @@ -1865,9 +2701,8 @@ struct KeyInfo { ** ** An instance of this object serves as a "key" for doing a search on ** an index b+tree. The goal of the search is to find the entry that -** is closed to the key described by this object. This object might hold -** just a prefix of the key. The number of fields is given by -** pKeyInfo->nField. +** is closest to the key described by this object. This object might hold +** just a prefix of the key. The number of fields is given by nField. ** ** The r1 and r2 fields are the values to return if this key is less than ** or greater than a key in the btree, respectively. These are normally @@ -1877,7 +2712,7 @@ struct KeyInfo { ** The key comparison functions actually return default_rc when they find ** an equals comparison. default_rc can be -1, 0, or +1. If there are ** multiple entries in the b-tree with the same key (when only looking -** at the first pKeyInfo->nFields,) then default_rc can be set to -1 to +** at the first nField elements) then default_rc can be set to -1 to ** cause the search to find the last match, or +1 to cause the search to ** find the first match. ** @@ -1889,13 +2724,18 @@ struct KeyInfo { ** b-tree. */ struct UnpackedRecord { - KeyInfo *pKeyInfo; /* Collation and sort-order information */ - Mem *aMem; /* Values */ + KeyInfo *pKeyInfo; /* Comparison info for the index that is unpacked */ + Mem *aMem; /* Values for columns of the index */ + union { + char *z; /* Cache of aMem[0].z for vdbeRecordCompareString() */ + i64 i; /* Cache of aMem[0].u.i for vdbeRecordCompareInt() */ + } u; + int n; /* Cache of aMem[0].n used by vdbeRecordCompareString() */ u16 nField; /* Number of entries in apMem[] */ i8 default_rc; /* Comparison result if keys are equal */ u8 errCode; /* Error detected by xRecordCompare (CORRUPT or NOMEM) */ - i8 r1; /* Value to return if (lhs > rhs) */ - i8 r2; /* Value to return if (rhs < lhs) */ + i8 r1; /* Value to return if (lhs < rhs) */ + i8 r2; /* Value to return if (lhs > rhs) */ u8 eqSeen; /* True if an equality comparison has been seen */ }; @@ -1914,7 +2754,7 @@ struct UnpackedRecord { ** In the Table structure describing Ex1, nCol==3 because there are ** three columns in the table. In the Index structure describing ** Ex2, nColumn==2 since 2 of the 3 columns of Ex1 are indexed. -** The value of aiColumn is {2, 0}. aiColumn[0]==2 because the +** The value of aiColumn is {2, 0}. aiColumn[0]==2 because the ** first column to be indexed (c3) has an index of 2 in Ex1.aCol[]. ** The second column to be indexed (c1) has an index of 0 in ** Ex1.aCol[], hence Ex2.aiColumn[1]==0. @@ -1922,12 +2762,24 @@ struct UnpackedRecord { ** The Index.onError field determines whether or not the indexed columns ** must be unique and what to do if they are not. When Index.onError=OE_None, ** it means this is not a unique index. Otherwise it is a unique index -** and the value of Index.onError indicate the which conflict resolution -** algorithm to employ whenever an attempt is made to insert a non-unique +** and the value of Index.onError indicates which conflict resolution +** algorithm to employ when an attempt is made to insert a non-unique ** element. ** +** The colNotIdxed bitmask is used in combination with SrcItem.colUsed +** for a fast test to see if an index can serve as a covering index. +** colNotIdxed has a 1 bit for every column of the original table that +** is *not* available in the index. Thus the expression +** "colUsed & colNotIdxed" will be non-zero if the index is not a +** covering index. The most significant bit of of colNotIdxed will always +** be true (note-20221022-a). If a column beyond the 63rd column of the +** table is used, the "colUsed & colNotIdxed" test will always be non-zero +** and we have to assume either that the index is not covering, or use +** an alternative (slower) algorithm to determine whether or not +** the index is covering. +** ** While parsing a CREATE TABLE or CREATE INDEX statement in order to -** generate VDBE code (as opposed to parsing one read from an sqlite_master +** generate VDBE code (as opposed to parsing one read from an sqlite_schema ** table as part of parsing an existing database schema), transient instances ** of this structure may be created. In this case the Index.tnum variable is ** used to store the address of a VDBE instruction, not a database page @@ -1946,25 +2798,33 @@ struct Index { const char **azColl; /* Array of collation sequence names for index */ Expr *pPartIdxWhere; /* WHERE clause for partial indices */ ExprList *aColExpr; /* Column expressions */ - int tnum; /* DB Page containing root of this index */ + Pgno tnum; /* DB Page containing root of this index */ LogEst szIdxRow; /* Estimated average row size in bytes */ u16 nKeyCol; /* Number of columns forming the key */ - u16 nColumn; /* Number of columns stored in the index */ + u16 nColumn; /* Nr columns in btree. Can be 2*Table.nCol */ u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */ - unsigned idxType:2; /* 1==UNIQUE, 2==PRIMARY KEY, 0==CREATE INDEX */ + unsigned idxType:2; /* 0:Normal 1:UNIQUE, 2:PRIMARY KEY, 3:IPK */ unsigned bUnordered:1; /* Use this index for == or IN queries only */ unsigned uniqNotNull:1; /* True if UNIQUE and NOT NULL for all columns */ unsigned isResized:1; /* True if resizeIndexObject() has been called */ unsigned isCovering:1; /* True if this is a covering index */ unsigned noSkipScan:1; /* Do not try to use skip-scan if true */ -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 + unsigned hasStat1:1; /* aiRowLogEst values come from sqlite_stat1 */ + unsigned bNoQuery:1; /* Do not use this index to optimize queries */ + unsigned bAscKeyBug:1; /* True if the bba7b69f9849b5bf bug applies */ + unsigned bHasVCol:1; /* Index references one or more VIRTUAL columns */ + unsigned bHasExpr:1; /* Index contains an expression, either a literal + ** expression, or a reference to a VIRTUAL column */ +#ifdef SQLITE_ENABLE_STAT4 int nSample; /* Number of elements in aSample[] */ + int mxSample; /* Number of slots allocated to aSample[] */ int nSampleCol; /* Size of IndexSample.anEq[] and so on */ tRowcnt *aAvgEq; /* Average nEq values for keys not in aSample */ IndexSample *aSample; /* Samples of the left-most key */ tRowcnt *aiRowEst; /* Non-logarithmic stat1 data for this index */ tRowcnt nRowEst0; /* Non-logarithmic number of rows in the index */ #endif + Bitmask colNotIdxed; /* Unindexed columns in pTab */ }; /* @@ -1973,6 +2833,7 @@ struct Index { #define SQLITE_IDXTYPE_APPDEF 0 /* Created using CREATE INDEX */ #define SQLITE_IDXTYPE_UNIQUE 1 /* Implements a UNIQUE constraint */ #define SQLITE_IDXTYPE_PRIMARYKEY 2 /* Is the PRIMARY KEY for the table */ +#define SQLITE_IDXTYPE_IPK 3 /* INTEGER PRIMARY KEY index */ /* Return true if index X is a PRIMARY KEY index */ #define IsPrimaryKeyIndex(X) ((X)->idxType==SQLITE_IDXTYPE_PRIMARYKEY) @@ -1987,7 +2848,7 @@ struct Index { #define XN_EXPR (-2) /* Indexed column is an expression */ /* -** Each sample stored in the sqlite_stat3 table is represented in memory +** Each sample stored in the sqlite_stat4 table is represented in memory ** using a structure of this type. See documentation at the top of the ** analyze.c source file for additional information. */ @@ -1999,13 +2860,21 @@ struct IndexSample { tRowcnt *anDLt; /* Est. number of distinct keys less than this sample */ }; +/* +** Possible values to use within the flags argument to sqlite3GetToken(). +*/ +#define SQLITE_TOKEN_QUOTED 0x1 /* Token is a quoted identifier. */ +#define SQLITE_TOKEN_KEYWORD 0x2 /* Token is a keyword. */ + /* ** Each token coming out of the lexer is an instance of ** this structure. Tokens are also used as part of an expression. ** -** Note if Token.z==0 then Token.dyn and Token.n are undefined and -** may contain random values. Do not make any assumptions about Token.dyn -** and Token.n when Token.z==0. +** The memory that "z" points to is owned by other objects. Take care +** that the owner of the "z" string does not deallocate the string before +** the Token goes out of scope! Very often, the "z" points to some place +** in the middle of the Parse.zSql text. But it might also point to a +** static string. */ struct Token { const char *z; /* Text of the token. Not NULL-terminated! */ @@ -2017,7 +2886,7 @@ struct Token { ** code for a SELECT that contains aggregate functions. ** ** If Expr.op==TK_AGG_COLUMN or TK_AGG_FUNCTION then Expr.pAggInfo is a -** pointer to this structure. The Expr.iColumn field is the index in +** pointer to this structure. The Expr.iAgg field is the index in ** AggInfo.aCol[] or AggInfo.aFunc[] of information needed to generate ** code for that node. ** @@ -2030,32 +2899,56 @@ struct AggInfo { ** from source tables rather than from accumulators */ u8 useSortingIdx; /* In direct mode, reference the sorting index rather ** than the source table */ + u32 nSortingColumn; /* Number of columns in the sorting index */ int sortingIdx; /* Cursor number of the sorting index */ int sortingIdxPTab; /* Cursor number of pseudo-table */ - int nSortingColumn; /* Number of columns in the sorting index */ - int mnReg, mxReg; /* Range of registers allocated for aCol and aFunc */ + int iFirstReg; /* First register in range for aCol[] and aFunc[] */ ExprList *pGroupBy; /* The group by clause */ struct AggInfo_col { /* For each column used in source tables */ Table *pTab; /* Source table */ + Expr *pCExpr; /* The original expression */ int iTable; /* Cursor number of the source table */ int iColumn; /* Column number within the source table */ int iSorterColumn; /* Column number in the sorting index */ - int iMem; /* Memory location that acts as accumulator */ - Expr *pExpr; /* The original expression */ } *aCol; int nColumn; /* Number of used entries in aCol[] */ int nAccumulator; /* Number of columns that show through to the output. ** Additional columns are used only as parameters to ** aggregate functions */ struct AggInfo_func { /* For each aggregate function */ - Expr *pExpr; /* Expression encoding the function */ + Expr *pFExpr; /* Expression encoding the function */ FuncDef *pFunc; /* The aggregate function implementation */ - int iMem; /* Memory location that acts as accumulator */ int iDistinct; /* Ephemeral table used to enforce DISTINCT */ + int iDistAddr; /* Address of OP_OpenEphemeral */ + int iOBTab; /* Ephemeral table to implement ORDER BY */ + u8 bOBPayload; /* iOBTab has payload columns separate from key */ + u8 bOBUnique; /* Enforce uniqueness on iOBTab keys */ + u8 bUseSubtype; /* Transfer subtype info through sorter */ } *aFunc; int nFunc; /* Number of entries in aFunc[] */ + u32 selId; /* Select to which this AggInfo belongs */ +#ifdef SQLITE_DEBUG + Select *pSelect; /* SELECT statement that this AggInfo supports */ +#endif }; +/* +** Macros to compute aCol[] and aFunc[] register numbers. +** +** These macros should not be used prior to the call to +** assignAggregateRegisters() that computes the value of pAggInfo->iFirstReg. +** The assert()s that are part of this macro verify that constraint. +*/ +#ifndef NDEBUG +#define AggInfoColumnReg(A,I) (assert((A)->iFirstReg),(A)->iFirstReg+(I)) +#define AggInfoFuncReg(A,I) \ + (assert((A)->iFirstReg),(A)->iFirstReg+(A)->nColumn+(I)) +#else +#define AggInfoColumnReg(A,I) ((A)->iFirstReg+(I)) +#define AggInfoFuncReg(A,I) \ + ((A)->iFirstReg+(A)->nColumn+(I)) +#endif + /* ** The datatype ynVar is a signed integer, either 16-bit or 32-bit. ** Usually it is 16-bits. But if SQLITE_MAX_VARIABLE_NUMBER is greater @@ -2063,10 +2956,10 @@ struct AggInfo { ** it uses less memory in the Expr object, which is a big memory user ** in systems with lots of prepared statements. And few applications ** need more than about 10 or 20 variables. But some extreme users want -** to have prepared statements with over 32767 variables, and for them +** to have prepared statements with over 32766 variables, and for them ** the option is available (at compile-time). */ -#if SQLITE_MAX_VARIABLE_NUMBER<=32767 +#if SQLITE_MAX_VARIABLE_NUMBER<32767 typedef i16 ynVar; #else typedef int ynVar; @@ -2082,11 +2975,11 @@ typedef int ynVar; ** to represent the greater-than-or-equal-to operator in the expression ** tree. ** -** If the expression is an SQL literal (TK_INTEGER, TK_FLOAT, TK_BLOB, -** or TK_STRING), then Expr.token contains the text of the SQL literal. If -** the expression is a variable (TK_VARIABLE), then Expr.token contains the +** If the expression is an SQL literal (TK_INTEGER, TK_FLOAT, TK_BLOB, +** or TK_STRING), then Expr.u.zToken contains the text of the SQL literal. If +** the expression is a variable (TK_VARIABLE), then Expr.u.zToken contains the ** variable name. Finally, if the expression is an SQL function (TK_FUNCTION), -** then Expr.token contains the name of the function. +** then Expr.u.zToken contains the name of the function. ** ** Expr.pRight and Expr.pLeft are the left and right subexpressions of a ** binary operator. Either or both may be NULL. @@ -2095,7 +2988,7 @@ typedef int ynVar; ** a CASE expression or an IN expression of the form "<lhs> IN (<y>, <z>...)". ** Expr.x.pSelect is used if the expression is a sub-select or an expression of ** the form "<lhs> IN (SELECT ...)". If the EP_xIsSelect bit is set in the -** Expr.flags mask, then Expr.x.pSelect is valid. Otherwise, Expr.x.pList is +** Expr.flags mask, then Expr.x.pSelect is valid. Otherwise, Expr.x.pList is ** valid. ** ** An expression of the form ID or ID.ID refers to a column in a table. @@ -2106,8 +2999,8 @@ typedef int ynVar; ** value is also stored in the Expr.iAgg column in the aggregate so that ** it can be accessed after all aggregates are computed. ** -** If the expression is an unbound variable marker (a question mark -** character '?' in the original SQL) then the Expr.iTable holds the index +** If the expression is an unbound variable marker (a question mark +** character '?' in the original SQL) then the Expr.iTable holds the index ** number for that variable. ** ** If the expression is a subquery then Expr.iColumn holds an integer @@ -2126,7 +3019,7 @@ typedef int ynVar; ** help reduce memory requirements, sometimes an Expr object will be ** truncated. And to reduce the number of memory allocations, sometimes ** two or more Expr objects will be stored in a single memory allocation, -** together with Expr.zToken strings. +** together with Expr.u.zToken strings. ** ** If the EP_Reduced and EP_TokenOnly flags are set when ** an Expr object is truncated. When EP_Reduced is set, then all @@ -2137,7 +3030,14 @@ typedef int ynVar; */ struct Expr { u8 op; /* Operation performed by this node */ - char affinity; /* The affinity of the column or 0 if not a column */ + char affExpr; /* affinity, or RAISE type */ + u8 op2; /* TK_REGISTER/TK_TRUTH: original value of Expr.op + ** TK_COLUMN: the value of p5 for OP_Column + ** TK_AGG_FUNCTION: nesting depth + ** TK_FUNCTION: NC_SelfRef flag if needs OP_PureFunc */ +#ifdef SQLITE_DEBUG + u8 vvaFlags; /* Verification flags. */ +#endif u32 flags; /* Various flags. EP_* See below */ union { char *zToken; /* Token value. Zero terminated and dequoted */ @@ -2146,7 +3046,7 @@ struct Expr { /* If the EP_TokenOnly flag is set in the Expr.flags mask, then no ** space is allocated for the fields below this point. An attempt to - ** access them will result in a segfault or malfunction. + ** access them will result in a segfault or malfunction. *********************************************************************/ Expr *pLeft; /* Left subnode */ @@ -2167,72 +3067,121 @@ struct Expr { int iTable; /* TK_COLUMN: cursor number of table holding column ** TK_REGISTER: register number ** TK_TRIGGER: 1 -> new, 0 -> old - ** EP_Unlikely: 134217728 times likelihood */ + ** EP_Unlikely: 134217728 times likelihood + ** TK_IN: ephemeral table holding RHS + ** TK_SELECT_COLUMN: Number of columns on the LHS + ** TK_SELECT: 1st register of result vector */ ynVar iColumn; /* TK_COLUMN: column index. -1 for rowid. - ** TK_VARIABLE: variable number (always >= 1). */ + ** TK_VARIABLE: variable number (always >= 1). + ** TK_SELECT_COLUMN: column of the result vector */ i16 iAgg; /* Which entry in pAggInfo->aCol[] or ->aFunc[] */ - i16 iRightJoinTable; /* If EP_FromJoin, the right table of the join */ - u8 op2; /* TK_REGISTER: original value of Expr.op - ** TK_COLUMN: the value of p5 for OP_Column - ** TK_AGG_FUNCTION: nesting depth */ + union { + int iJoin; /* If EP_OuterON or EP_InnerON, the right table */ + int iOfst; /* else: start of token from start of statement */ + } w; AggInfo *pAggInfo; /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */ - Table *pTab; /* Table for TK_COLUMN expressions. */ + union { + Table *pTab; /* TK_COLUMN: Table containing column. Can be NULL + ** for a column of an index on an expression */ + Window *pWin; /* EP_WinFunc: Window/Filter defn for a function */ + int nReg; /* TK_NULLS: Number of registers to NULL out */ + struct { /* TK_IN, TK_SELECT, and TK_EXISTS */ + int iAddr; /* Subroutine entry address */ + int regReturn; /* Register used to hold return address */ + } sub; + } y; }; -/* -** The following are the meanings of bits in the Expr.flags field. +/* The following are the meanings of bits in the Expr.flags field. +** Value restrictions: +** +** EP_Agg == NC_HasAgg == SF_HasAgg +** EP_Win == NC_HasWin +*/ +#define EP_OuterON 0x000001 /* Originates in ON/USING clause of outer join */ +#define EP_InnerON 0x000002 /* Originates in ON/USING of an inner join */ +#define EP_Distinct 0x000004 /* Aggregate function with DISTINCT keyword */ +#define EP_HasFunc 0x000008 /* Contains one or more functions of any kind */ +#define EP_Agg 0x000010 /* Contains one or more aggregate functions */ +#define EP_FixedCol 0x000020 /* TK_Column with a known fixed value */ +#define EP_VarSelect 0x000040 /* pSelect is correlated, not constant */ +#define EP_DblQuoted 0x000080 /* token.z was originally in "..." */ +#define EP_InfixFunc 0x000100 /* True for an infix function: LIKE, GLOB, etc */ +#define EP_Collate 0x000200 /* Tree contains a TK_COLLATE operator */ +#define EP_Commuted 0x000400 /* Comparison operator has been commuted */ +#define EP_IntValue 0x000800 /* Integer value contained in u.iValue */ +#define EP_xIsSelect 0x001000 /* x.pSelect is valid (otherwise x.pList is) */ +#define EP_Skip 0x002000 /* Operator does not contribute to affinity */ +#define EP_Reduced 0x004000 /* Expr struct EXPR_REDUCEDSIZE bytes only */ +#define EP_Win 0x008000 /* Contains window functions */ +#define EP_TokenOnly 0x010000 /* Expr struct EXPR_TOKENONLYSIZE bytes only */ +#define EP_FullSize 0x020000 /* Expr structure must remain full sized */ +#define EP_IfNullRow 0x040000 /* The TK_IF_NULL_ROW opcode */ +#define EP_Unlikely 0x080000 /* unlikely() or likelihood() function */ +#define EP_ConstFunc 0x100000 /* A SQLITE_FUNC_CONSTANT or _SLOCHNG function */ +#define EP_CanBeNull 0x200000 /* Can be null despite NOT NULL constraint */ +#define EP_Subquery 0x400000 /* Tree contains a TK_SELECT operator */ +#define EP_Leaf 0x800000 /* Expr.pLeft, .pRight, .u.pSelect all NULL */ +#define EP_WinFunc 0x1000000 /* TK_FUNCTION with Expr.y.pWin set */ +#define EP_Subrtn 0x2000000 /* Uses Expr.y.sub. TK_IN, _SELECT, or _EXISTS */ +#define EP_Quoted 0x4000000 /* TK_ID was originally quoted */ +#define EP_Static 0x8000000 /* Held in memory not obtained from malloc() */ +#define EP_IsTrue 0x10000000 /* Always has boolean value of TRUE */ +#define EP_IsFalse 0x20000000 /* Always has boolean value of FALSE */ +#define EP_FromDDL 0x40000000 /* Originates from sqlite_schema */ +#define EP_SubtArg 0x80000000 /* Is argument to SQLITE_SUBTYPE function */ + +/* The EP_Propagate mask is a set of properties that automatically propagate +** upwards into parent nodes. +*/ +#define EP_Propagate (EP_Collate|EP_Subquery|EP_HasFunc) + +/* Macros can be used to test, set, or clear bits in the +** Expr.flags field. */ -#define EP_FromJoin 0x000001 /* Originates in ON/USING clause of outer join */ -#define EP_Agg 0x000002 /* Contains one or more aggregate functions */ -#define EP_Resolved 0x000004 /* IDs have been resolved to COLUMNs */ -#define EP_Error 0x000008 /* Expression contains one or more errors */ -#define EP_Distinct 0x000010 /* Aggregate function with DISTINCT keyword */ -#define EP_VarSelect 0x000020 /* pSelect is correlated, not constant */ -#define EP_DblQuoted 0x000040 /* token.z was originally in "..." */ -#define EP_InfixFunc 0x000080 /* True for an infix function: LIKE, GLOB, etc */ -#define EP_Collate 0x000100 /* Tree contains a TK_COLLATE operator */ -#define EP_Generic 0x000200 /* Ignore COLLATE or affinity on this tree */ -#define EP_IntValue 0x000400 /* Integer value contained in u.iValue */ -#define EP_xIsSelect 0x000800 /* x.pSelect is valid (otherwise x.pList is) */ -#define EP_Skip 0x001000 /* COLLATE, AS, or UNLIKELY */ -#define EP_Reduced 0x002000 /* Expr struct EXPR_REDUCEDSIZE bytes only */ -#define EP_TokenOnly 0x004000 /* Expr struct EXPR_TOKENONLYSIZE bytes only */ -#define EP_Static 0x008000 /* Held in memory not obtained from malloc() */ -#define EP_MemToken 0x010000 /* Need to sqlite3DbFree() Expr.zToken */ -#define EP_NoReduce 0x020000 /* Cannot EXPRDUP_REDUCE this Expr */ -#define EP_Unlikely 0x040000 /* unlikely() or likelihood() function */ -#define EP_ConstFunc 0x080000 /* A SQLITE_FUNC_CONSTANT or _SLOCHNG function */ -#define EP_CanBeNull 0x100000 /* Can be null despite NOT NULL constraint */ -#define EP_Subquery 0x200000 /* Tree contains a TK_SELECT operator */ -#define EP_Alias 0x400000 /* Is an alias for a result set column */ +#define ExprHasProperty(E,P) (((E)->flags&(u32)(P))!=0) +#define ExprHasAllProperty(E,P) (((E)->flags&(u32)(P))==(u32)(P)) +#define ExprSetProperty(E,P) (E)->flags|=(u32)(P) +#define ExprClearProperty(E,P) (E)->flags&=~(u32)(P) +#define ExprAlwaysTrue(E) (((E)->flags&(EP_OuterON|EP_IsTrue))==EP_IsTrue) +#define ExprAlwaysFalse(E) (((E)->flags&(EP_OuterON|EP_IsFalse))==EP_IsFalse) +#define ExprIsFullSize(E) (((E)->flags&(EP_Reduced|EP_TokenOnly))==0) -/* -** Combinations of two or more EP_* flags +/* Macros used to ensure that the correct members of unions are accessed +** in Expr. */ -#define EP_Propagate (EP_Collate|EP_Subquery) /* Propagate these bits up tree */ +#define ExprUseUToken(E) (((E)->flags&EP_IntValue)==0) +#define ExprUseUValue(E) (((E)->flags&EP_IntValue)!=0) +#define ExprUseWOfst(E) (((E)->flags&(EP_InnerON|EP_OuterON))==0) +#define ExprUseWJoin(E) (((E)->flags&(EP_InnerON|EP_OuterON))!=0) +#define ExprUseXList(E) (((E)->flags&EP_xIsSelect)==0) +#define ExprUseXSelect(E) (((E)->flags&EP_xIsSelect)!=0) +#define ExprUseYTab(E) (((E)->flags&(EP_WinFunc|EP_Subrtn))==0) +#define ExprUseYWin(E) (((E)->flags&EP_WinFunc)!=0) +#define ExprUseYSub(E) (((E)->flags&EP_Subrtn)!=0) -/* -** These macros can be used to test, set, or clear bits in the -** Expr.flags field. +/* Flags for use with Expr.vvaFlags */ -#define ExprHasProperty(E,P) (((E)->flags&(P))!=0) -#define ExprHasAllProperty(E,P) (((E)->flags&(P))==(P)) -#define ExprSetProperty(E,P) (E)->flags|=(P) -#define ExprClearProperty(E,P) (E)->flags&=~(P) +#define EP_NoReduce 0x01 /* Cannot EXPRDUP_REDUCE this Expr */ +#define EP_Immutable 0x02 /* Do not change this Expr node */ /* The ExprSetVVAProperty() macro is used for Verification, Validation, ** and Accreditation only. It works like ExprSetProperty() during VVA ** processes but is a no-op for delivery. */ #ifdef SQLITE_DEBUG -# define ExprSetVVAProperty(E,P) (E)->flags|=(P) +# define ExprSetVVAProperty(E,P) (E)->vvaFlags|=(P) +# define ExprHasVVAProperty(E,P) (((E)->vvaFlags&(P))!=0) +# define ExprClearVVAProperties(E) (E)->vvaFlags = 0 #else # define ExprSetVVAProperty(E,P) +# define ExprHasVVAProperty(E,P) 0 +# define ExprClearVVAProperties(E) #endif /* -** Macros to determine the number of bytes required by a normal Expr -** struct, an Expr struct with the EP_Reduced flag set in Expr.flags +** Macros to determine the number of bytes required by a normal Expr +** struct, an Expr struct with the EP_Reduced flag set in Expr.flags ** and an Expr struct with the EP_TokenOnly flag set. */ #define EXPR_FULLSIZE sizeof(Expr) /* Full size */ @@ -2240,11 +3189,23 @@ struct Expr { #define EXPR_TOKENONLYSIZE offsetof(Expr,pLeft) /* Fewer features */ /* -** Flags passed to the sqlite3ExprDup() function. See the header comment +** Flags passed to the sqlite3ExprDup() function. See the header comment ** above sqlite3ExprDup() for details. */ #define EXPRDUP_REDUCE 0x0001 /* Used reduced-size Expr nodes */ +/* +** True if the expression passed as an argument was a function with +** an OVER() clause (a window function). +*/ +#ifdef SQLITE_OMIT_WINDOWFUNC +# define IsWindowFunc(p) 0 +#else +# define IsWindowFunc(p) ( \ + ExprHasProperty((p), EP_WinFunc) && p->y.pWin->eFrmType!=TK_FILTER \ + ) +#endif + /* ** A list of expressions. Each expression may optionally have a ** name. An expr/name combination can be used in several ways, such @@ -2253,44 +3214,61 @@ struct Expr { ** also be used as the argument to a function, in which case the a.zName ** field is not used. ** -** By default the Expr.zSpan field holds a human-readable description of -** the expression that is used in the generation of error messages and -** column labels. In this case, Expr.zSpan is typically the text of a -** column expression as it exists in a SELECT statement. However, if -** the bSpanIsTab flag is set, then zSpan is overloaded to mean the name -** of the result column in the form: DATABASE.TABLE.COLUMN. This later -** form is used for name resolution with nested FROM clauses. +** In order to try to keep memory usage down, the Expr.a.zEName field +** is used for multiple purposes: +** +** eEName Usage +** ---------- ------------------------- +** ENAME_NAME (1) the AS of result set column +** (2) COLUMN= of an UPDATE +** +** ENAME_TAB DB.TABLE.NAME used to resolve names +** of subqueries +** +** ENAME_SPAN Text of the original result set +** expression. */ struct ExprList { int nExpr; /* Number of expressions on the list */ + int nAlloc; /* Number of a[] slots allocated */ struct ExprList_item { /* For each expression in the list */ - Expr *pExpr; /* The list of expressions */ - char *zName; /* Token associated with this expression */ - char *zSpan; /* Original text of the expression */ - u8 sortOrder; /* 1 for DESC or 0 for ASC */ - unsigned done :1; /* A flag to indicate when processing is finished */ - unsigned bSpanIsTab :1; /* zSpan holds DB.TABLE.COLUMN */ - unsigned reusable :1; /* Constant expression is reusable */ + Expr *pExpr; /* The parse tree for this expression */ + char *zEName; /* Token associated with this expression */ + struct { + u8 sortFlags; /* Mask of KEYINFO_ORDER_* flags */ + unsigned eEName :2; /* Meaning of zEName */ + unsigned done :1; /* Indicates when processing is finished */ + unsigned reusable :1; /* Constant expression is reusable */ + unsigned bSorterRef :1; /* Defer evaluation until after sorting */ + unsigned bNulls :1; /* True if explicit "NULLS FIRST/LAST" */ + unsigned bUsed :1; /* This column used in a SF_NestedFrom subquery */ + unsigned bUsingTerm:1; /* Term from the USING clause of a NestedFrom */ + unsigned bNoExpand: 1; /* Term is an auxiliary in NestedFrom and should + ** not be expanded by "*" in parent queries */ + } fg; union { - struct { + struct { /* Used by any ExprList other than Parse.pConsExpr */ u16 iOrderByCol; /* For ORDER BY, column number in result set */ u16 iAlias; /* Index into Parse.aAlias[] for zName */ } x; - int iConstExprReg; /* Register in which Expr value is cached */ + int iConstExprReg; /* Register in which Expr value is cached. Used only + ** by Parse.pConstExpr */ } u; - } *a; /* Alloc a power of two greater or equal to nExpr */ + } a[FLEXARRAY]; /* One slot for each expression in the list */ }; +/* The size (in bytes) of an ExprList object that is big enough to hold +** as many as N expressions. */ +#define SZ_EXPRLIST(N) \ + (offsetof(ExprList,a) + (N)*sizeof(struct ExprList_item)) + /* -** An instance of this structure is used by the parser to record both -** the parse tree for an expression and the span of input text for an -** expression. +** Allowed values for Expr.a.eEName */ -struct ExprSpan { - Expr *pExpr; /* The expression parse tree */ - const char *zStart; /* First character of input text */ - const char *zEnd; /* One character past the end of input text */ -}; +#define ENAME_NAME 0 /* The AS clause of a result set */ +#define ENAME_SPAN 1 /* Complete text of the result set expression */ +#define ENAME_TAB 2 /* "DB.TABLE.NAME" for the result set */ +#define ENAME_ROWID 3 /* "DB.TABLE._rowid_" for * expansion of rowid */ /* ** An instance of this structure can hold a simple list of identifiers, @@ -2308,120 +3286,186 @@ struct ExprSpan { ** If "a" is the k-th column of table "t", then IdList.a[0].idx==k. */ struct IdList { + int nId; /* Number of identifiers on the list */ struct IdList_item { char *zName; /* Name of the identifier */ - int idx; /* Index in some Table.aCol[] of a column named zName */ - } *a; - int nId; /* Number of identifiers on the list */ + } a[FLEXARRAY]; }; +/* The size (in bytes) of an IdList object that can hold up to N IDs. */ +#define SZ_IDLIST(N) (offsetof(IdList,a)+(N)*sizeof(struct IdList_item)) + /* -** The bitmask datatype defined below is used for various optimizations. -** -** Changing this from a 64-bit to a 32-bit type limits the number of -** tables in a join to 32 instead of 64. But it also reduces the size -** of the library by 738 bytes on ix86. +** Allowed values for IdList.eType, which determines which value of the a.u4 +** is valid. */ -typedef u64 Bitmask; +#define EU4_NONE 0 /* Does not use IdList.a.u4 */ +#define EU4_IDX 1 /* Uses IdList.a.u4.idx */ +#define EU4_EXPR 2 /* Uses IdList.a.u4.pExpr -- NOT CURRENTLY USED */ /* -** The number of bits in a Bitmask. "BMS" means "BitMask Size". +** Details of the implementation of a subquery. */ -#define BMS ((int)(sizeof(Bitmask)*8)) +struct Subquery { + Select *pSelect; /* A SELECT statement used in place of a table name */ + int addrFillSub; /* Address of subroutine to initialize a subquery */ + int regReturn; /* Register holding return address of addrFillSub */ + int regResult; /* Registers holding results of a co-routine */ +}; + +/* +** The SrcItem object represents a single term in the FROM clause of a query. +** The SrcList object is mostly an array of SrcItems. +** +** The jointype starts out showing the join type between the current table +** and the next table on the list. The parser builds the list this way. +** But sqlite3SrcListShiftJoinType() later shifts the jointypes so that each +** jointype expresses the join between the table and the previous table. +** +** In the colUsed field, the high-order bit (bit 63) is set if the table +** contains more than 63 columns and the 64-th or later column is used. +** +** Aggressive use of "union" helps keep the size of the object small. This +** has been shown to boost performance, in addition to saving memory. +** Access to union elements is gated by the following rules which should +** always be checked, either by an if-statement or by an assert(). +** +** Field Only access if this is true +** --------------- ----------------------------------- +** u1.zIndexedBy fg.isIndexedBy +** u1.pFuncArg fg.isTabFunc +** u1.nRow !fg.isTabFunc && !fg.isIndexedBy +** +** u2.pIBIndex fg.isIndexedBy +** u2.pCteUse fg.isCte +** +** u3.pOn !fg.isUsing +** u3.pUsing fg.isUsing +** +** u4.zDatabase !fg.fixedSchema && !fg.isSubquery +** u4.pSchema fg.fixedSchema +** u4.pSubq fg.isSubquery +** +** See also the sqlite3SrcListDelete() routine for assert() statements that +** check invariants on the fields of this object, especially the flags +** inside the fg struct. +*/ +struct SrcItem { + char *zName; /* Name of the table */ + char *zAlias; /* The "B" part of a "A AS B" phrase. zName is the "A" */ + Table *pSTab; /* Table object for zName. Mnemonic: Srcitem-TABle */ + struct { + u8 jointype; /* Type of join between this table and the previous */ + unsigned notIndexed :1; /* True if there is a NOT INDEXED clause */ + unsigned isIndexedBy :1; /* True if there is an INDEXED BY clause */ + unsigned isSubquery :1; /* True if this term is a subquery */ + unsigned isTabFunc :1; /* True if table-valued-function syntax */ + unsigned isCorrelated :1; /* True if sub-query is correlated */ + unsigned isMaterialized:1; /* This is a materialized view */ + unsigned viaCoroutine :1; /* Implemented as a co-routine */ + unsigned isRecursive :1; /* True for recursive reference in WITH */ + unsigned fromDDL :1; /* Comes from sqlite_schema */ + unsigned isCte :1; /* This is a CTE */ + unsigned notCte :1; /* This item may not match a CTE */ + unsigned isUsing :1; /* u3.pUsing is valid */ + unsigned isOn :1; /* u3.pOn was once valid and non-NULL */ + unsigned isSynthUsing :1; /* u3.pUsing is synthesized from NATURAL */ + unsigned isNestedFrom :1; /* pSelect is a SF_NestedFrom subquery */ + unsigned rowidUsed :1; /* The ROWID of this table is referenced */ + unsigned fixedSchema :1; /* Uses u4.pSchema, not u4.zDatabase */ + unsigned hadSchema :1; /* Had u4.zDatabase before u4.pSchema */ + unsigned fromExists :1; /* Comes from WHERE EXISTS(...) */ + } fg; + int iCursor; /* The VDBE cursor number used to access this table */ + Bitmask colUsed; /* Bit N set if column N used. Details above for N>62 */ + union { + char *zIndexedBy; /* Identifier from "INDEXED BY <zIndex>" clause */ + ExprList *pFuncArg; /* Arguments to table-valued-function */ + u32 nRow; /* Number of rows in a VALUES clause */ + } u1; + union { + Index *pIBIndex; /* Index structure corresponding to u1.zIndexedBy */ + CteUse *pCteUse; /* CTE Usage info when fg.isCte is true */ + } u2; + union { + Expr *pOn; /* fg.isUsing==0 => The ON clause of a join */ + IdList *pUsing; /* fg.isUsing==1 => The USING clause of a join */ + } u3; + union { + Schema *pSchema; /* Schema to which this item is fixed */ + char *zDatabase; /* Name of database holding this table */ + Subquery *pSubq; /* Description of a subquery */ + } u4; +}; /* -** A bit in a Bitmask +** The OnOrUsing object represents either an ON clause or a USING clause. +** It can never be both at the same time, but it can be neither. */ -#define MASKBIT(n) (((Bitmask)1)<<(n)) -#define MASKBIT32(n) (((unsigned int)1)<<(n)) +struct OnOrUsing { + Expr *pOn; /* The ON clause of a join */ + IdList *pUsing; /* The USING clause of a join */ +}; /* -** The following structure describes the FROM clause of a SELECT statement. -** Each table or subquery in the FROM clause is a separate element of -** the SrcList.a[] array. -** -** With the addition of multiple database support, the following structure -** can also be used to describe a particular table such as the table that -** is modified by an INSERT, DELETE, or UPDATE statement. In standard SQL, -** such a table must be a simple name: ID. But in SQLite, the table can -** now be identified by a database name, a dot, then the table name: ID.ID. -** -** The jointype starts out showing the join type between the current table -** and the next table on the list. The parser builds the list this way. -** But sqlite3SrcListShiftJoinType() later shifts the jointypes so that each -** jointype expresses the join between the table and the previous table. +** This object represents one or more tables that are the source of +** content for an SQL statement. For example, a single SrcList object +** is used to hold the FROM clause of a SELECT statement. SrcList also +** represents the target tables for DELETE, INSERT, and UPDATE statements. ** -** In the colUsed field, the high-order bit (bit 63) is set if the table -** contains more than 63 columns and the 64-th or later column is used. */ struct SrcList { - int nSrc; /* Number of tables or subqueries in the FROM clause */ - u32 nAlloc; /* Number of entries allocated in a[] below */ - struct SrcList_item { - Schema *pSchema; /* Schema to which this item is fixed */ - char *zDatabase; /* Name of database holding this table */ - char *zName; /* Name of the table */ - char *zAlias; /* The "B" part of a "A AS B" phrase. zName is the "A" */ - Table *pTab; /* An SQL table corresponding to zName */ - Select *pSelect; /* A SELECT statement used in place of a table name */ - int addrFillSub; /* Address of subroutine to manifest a subquery */ - int regReturn; /* Register holding return address of addrFillSub */ - int regResult; /* Registers holding results of a co-routine */ - struct { - u8 jointype; /* Type of join between this able and the previous */ - unsigned notIndexed :1; /* True if there is a NOT INDEXED clause */ - unsigned isIndexedBy :1; /* True if there is an INDEXED BY clause */ - unsigned isTabFunc :1; /* True if table-valued-function syntax */ - unsigned isCorrelated :1; /* True if sub-query is correlated */ - unsigned viaCoroutine :1; /* Implemented as a co-routine */ - unsigned isRecursive :1; /* True for recursive reference in WITH */ - } fg; -#ifndef SQLITE_OMIT_EXPLAIN - u8 iSelectId; /* If pSelect!=0, the id of the sub-select in EQP */ -#endif - int iCursor; /* The VDBE cursor number used to access this table */ - Expr *pOn; /* The ON clause of a join */ - IdList *pUsing; /* The USING clause of a join */ - Bitmask colUsed; /* Bit N (1<<N) set if column N of pTab is used */ - union { - char *zIndexedBy; /* Identifier from "INDEXED BY <zIndex>" clause */ - ExprList *pFuncArg; /* Arguments to table-valued-function */ - } u1; - Index *pIBIndex; /* Index structure corresponding to u1.zIndexedBy */ - } a[1]; /* One entry for each identifier on the list */ + int nSrc; /* Number of tables or subqueries in the FROM clause */ + u32 nAlloc; /* Number of entries allocated in a[] below */ + SrcItem a[FLEXARRAY]; /* One entry for each identifier on the list */ }; +/* Size (in bytes) of a SrcList object that can hold as many as N +** SrcItem objects. */ +#define SZ_SRCLIST(N) (offsetof(SrcList,a)+(N)*sizeof(SrcItem)) + +/* Size (in bytes( of a SrcList object that holds 1 SrcItem. This is a +** special case of SZ_SRCITEM(1) that comes up often. */ +#define SZ_SRCLIST_1 (offsetof(SrcList,a)+sizeof(SrcItem)) + /* ** Permitted values of the SrcList.a.jointype field */ -#define JT_INNER 0x0001 /* Any kind of inner or cross join */ -#define JT_CROSS 0x0002 /* Explicit use of the CROSS keyword */ -#define JT_NATURAL 0x0004 /* True for a "natural" join */ -#define JT_LEFT 0x0008 /* Left outer join */ -#define JT_RIGHT 0x0010 /* Right outer join */ -#define JT_OUTER 0x0020 /* The "OUTER" keyword is present */ -#define JT_ERROR 0x0040 /* unknown or unsupported join type */ - +#define JT_INNER 0x01 /* Any kind of inner or cross join */ +#define JT_CROSS 0x02 /* Explicit use of the CROSS keyword */ +#define JT_NATURAL 0x04 /* True for a "natural" join */ +#define JT_LEFT 0x08 /* Left outer join */ +#define JT_RIGHT 0x10 /* Right outer join */ +#define JT_OUTER 0x20 /* The "OUTER" keyword is present */ +#define JT_LTORJ 0x40 /* One of the LEFT operands of a RIGHT JOIN + ** Mnemonic: Left Table Of Right Join */ +#define JT_ERROR 0x80 /* unknown or unsupported join type */ /* ** Flags appropriate for the wctrlFlags parameter of sqlite3WhereBegin() ** and the WhereInfo.wctrlFlags member. +** +** Value constraints (enforced via assert()): +** WHERE_USE_LIMIT == SF_FixedLimit */ #define WHERE_ORDERBY_NORMAL 0x0000 /* No-op */ #define WHERE_ORDERBY_MIN 0x0001 /* ORDER BY processing for min() func */ #define WHERE_ORDERBY_MAX 0x0002 /* ORDER BY processing for max() func */ #define WHERE_ONEPASS_DESIRED 0x0004 /* Want to do one-pass UPDATE/DELETE */ -#define WHERE_DUPLICATES_OK 0x0008 /* Ok to return a row more than once */ -#define WHERE_OMIT_OPEN_CLOSE 0x0010 /* Table cursors are already open */ -#define WHERE_FORCE_TABLE 0x0020 /* Do not use an index-only search */ -#define WHERE_ONETABLE_ONLY 0x0040 /* Only code the 1st table in pTabList */ -#define WHERE_NO_AUTOINDEX 0x0080 /* Disallow automatic indexes */ -#define WHERE_GROUPBY 0x0100 /* pOrderBy is really a GROUP BY */ -#define WHERE_DISTINCTBY 0x0200 /* pOrderby is really a DISTINCT clause */ -#define WHERE_WANT_DISTINCT 0x0400 /* All output needs to be distinct */ -#define WHERE_SORTBYGROUP 0x0800 /* Support sqlite3WhereIsSorted() */ -#define WHERE_REOPEN_IDX 0x1000 /* Try to use OP_ReopenIdx */ -#define WHERE_ONEPASS_MULTIROW 0x2000 /* ONEPASS is ok with multiple rows */ +#define WHERE_ONEPASS_MULTIROW 0x0008 /* ONEPASS is ok with multiple rows */ +#define WHERE_DUPLICATES_OK 0x0010 /* Ok to return a row more than once */ +#define WHERE_OR_SUBCLAUSE 0x0020 /* Processing a sub-WHERE as part of + ** the OR optimization */ +#define WHERE_GROUPBY 0x0040 /* pOrderBy is really a GROUP BY */ +#define WHERE_DISTINCTBY 0x0080 /* pOrderby is really a DISTINCT clause */ +#define WHERE_WANT_DISTINCT 0x0100 /* All output needs to be distinct */ +#define WHERE_SORTBYGROUP 0x0200 /* Support sqlite3WhereIsSorted() */ +#define WHERE_AGG_DISTINCT 0x0400 /* Query is "SELECT agg(DISTINCT ...)" */ +#define WHERE_ORDERBY_LIMIT 0x0800 /* ORDERBY+LIMIT on the inner loop */ +#define WHERE_RIGHT_JOIN 0x1000 /* Processing a RIGHT JOIN */ +#define WHERE_KEEP_ALL_JOINS 0x2000 /* Do not do the omit-noop-join opt */ +#define WHERE_USE_LIMIT 0x4000 /* Use the LIMIT in cost estimates */ + /* 0x8000 not currently used */ /* Allowed return values from sqlite3WhereIsDistinct() */ @@ -2439,12 +3483,12 @@ struct SrcList { ** pEList corresponds to the result set of a SELECT and is NULL for ** other statements. ** -** NameContexts can be nested. When resolving names, the inner-most +** NameContexts can be nested. When resolving names, the inner-most ** context is searched first. If no match is found, the next outer ** context is checked. If there is still no match, the next context ** is checked. This process continues until either a match is found ** or all contexts are check. When a match is found, the nRef member of -** the context containing the match is incremented. +** the context containing the match is incremented. ** ** Each subquery gets a new NameContext. The pNext field points to the ** NameContext in the parent query. Thus the process of scanning the @@ -2454,38 +3498,96 @@ struct SrcList { struct NameContext { Parse *pParse; /* The parser */ SrcList *pSrcList; /* One or more tables used to resolve names */ - ExprList *pEList; /* Optional list of result-set columns */ - AggInfo *pAggInfo; /* Information about aggregates at this level */ + union { + ExprList *pEList; /* Optional list of result-set columns */ + AggInfo *pAggInfo; /* Information about aggregates at this level */ + Upsert *pUpsert; /* ON CONFLICT clause information from an upsert */ + int iBaseReg; /* For TK_REGISTER when parsing RETURNING */ + } uNC; NameContext *pNext; /* Next outer name context. NULL for outermost */ int nRef; /* Number of names resolved by this context */ - int nErr; /* Number of errors encountered while resolving names */ - u16 ncFlags; /* Zero or more NC_* flags defined below */ + int nNcErr; /* Number of errors encountered while resolving names */ + int ncFlags; /* Zero or more NC_* flags defined below */ + u32 nNestedSelect; /* Number of nested selects using this NC */ + Select *pWinSelect; /* SELECT statement for any window functions */ }; /* ** Allowed values for the NameContext, ncFlags field. ** -** Note: NC_MinMaxAgg must have the same value as SF_MinMaxAgg and -** SQLITE_FUNC_MINMAX. -** -*/ -#define NC_AllowAgg 0x0001 /* Aggregate functions are allowed here */ -#define NC_HasAgg 0x0002 /* One or more aggregate functions seen */ -#define NC_IsCheck 0x0004 /* True if resolving names in a CHECK constraint */ -#define NC_InAggFunc 0x0008 /* True if analyzing arguments to an agg func */ -#define NC_PartIdx 0x0010 /* True if resolving a partial index WHERE */ -#define NC_IdxExpr 0x0020 /* True if resolving columns of CREATE INDEX */ -#define NC_MinMaxAgg 0x1000 /* min/max aggregates seen. See note above */ +** Value constraints (all checked via assert()): +** NC_HasAgg == SF_HasAgg == EP_Agg +** NC_MinMaxAgg == SF_MinMaxAgg == SQLITE_FUNC_MINMAX +** NC_OrderAgg == SF_OrderByReqd == SQLITE_FUNC_ANYORDER +** NC_HasWin == EP_Win +** +*/ +#define NC_AllowAgg 0x000001 /* Aggregate functions are allowed here */ +#define NC_PartIdx 0x000002 /* True if resolving a partial index WHERE */ +#define NC_IsCheck 0x000004 /* True if resolving a CHECK constraint */ +#define NC_GenCol 0x000008 /* True for a GENERATED ALWAYS AS clause */ +#define NC_HasAgg 0x000010 /* One or more aggregate functions seen */ +#define NC_IdxExpr 0x000020 /* True if resolving columns of CREATE INDEX */ +#define NC_SelfRef 0x00002e /* Combo: PartIdx, isCheck, GenCol, and IdxExpr */ +#define NC_Subquery 0x000040 /* A subquery has been seen */ +#define NC_UEList 0x000080 /* True if uNC.pEList is used */ +#define NC_UAggInfo 0x000100 /* True if uNC.pAggInfo is used */ +#define NC_UUpsert 0x000200 /* True if uNC.pUpsert is used */ +#define NC_UBaseReg 0x000400 /* True if uNC.iBaseReg is used */ +#define NC_MinMaxAgg 0x001000 /* min/max aggregates seen. See note above */ +/* 0x002000 // available for reuse */ +#define NC_AllowWin 0x004000 /* Window functions are allowed here */ +#define NC_HasWin 0x008000 /* One or more window functions seen */ +#define NC_IsDDL 0x010000 /* Resolving names in a CREATE statement */ +#define NC_InAggFunc 0x020000 /* True if analyzing arguments to an agg func */ +#define NC_FromDDL 0x040000 /* SQL text comes from sqlite_schema */ +#define NC_NoSelect 0x080000 /* Do not descend into sub-selects */ +#define NC_Where 0x100000 /* Processing WHERE clause of a SELECT */ +#define NC_OrderAgg 0x8000000 /* Has an aggregate other than count/min/max */ + +/* +** An instance of the following object describes a single ON CONFLICT +** clause in an upsert. +** +** The pUpsertTarget field is only set if the ON CONFLICT clause includes +** conflict-target clause. (In "ON CONFLICT(a,b)" the "(a,b)" is the +** conflict-target clause.) The pUpsertTargetWhere is the optional +** WHERE clause used to identify partial unique indexes. +** +** pUpsertSet is the list of column=expr terms of the UPDATE statement. +** The pUpsertSet field is NULL for a ON CONFLICT DO NOTHING. The +** pUpsertWhere is the WHERE clause for the UPDATE and is NULL if the +** WHERE clause is omitted. +*/ +struct Upsert { + ExprList *pUpsertTarget; /* Optional description of conflict target */ + Expr *pUpsertTargetWhere; /* WHERE clause for partial index targets */ + ExprList *pUpsertSet; /* The SET clause from an ON CONFLICT UPDATE */ + Expr *pUpsertWhere; /* WHERE clause for the ON CONFLICT UPDATE */ + Upsert *pNextUpsert; /* Next ON CONFLICT clause in the list */ + u8 isDoUpdate; /* True for DO UPDATE. False for DO NOTHING */ + u8 isDup; /* True if 2nd or later with same pUpsertIdx */ + /* Above this point is the parse tree for the ON CONFLICT clauses. + ** The next group of fields stores intermediate data. */ + void *pToFree; /* Free memory when deleting the Upsert object */ + /* All fields above are owned by the Upsert object and must be freed + ** when the Upsert is destroyed. The fields below are used to transfer + ** information from the INSERT processing down into the UPDATE processing + ** while generating code. The fields below are owned by the INSERT + ** statement and will be freed by INSERT processing. */ + Index *pUpsertIdx; /* UNIQUE constraint specified by pUpsertTarget */ + SrcList *pUpsertSrc; /* Table to be updated */ + int regData; /* First register holding array of VALUES */ + int iDataCur; /* Index of the data cursor */ + int iIdxCur; /* Index of the first index cursor */ +}; /* ** An instance of the following structure contains all information ** needed to generate code for a single SELECT statement. ** -** nLimit is set to -1 if there is no LIMIT clause. nOffset is set to 0. -** If there is a LIMIT clause, the parser sets nLimit to the value of the -** limit and nOffset to the value of the offset (or 0 if there is not -** offset). But later on, nLimit and nOffset become the memory locations -** in the VDBE that record the limit and offset counters. +** See the header comment on the computeLimitRegisters() routine for a +** detailed description of the meaning of the iLimit and iOffset fields. ** ** addrOpenEphm[] entries contain the address of OP_OpenEphemeral opcodes. ** These addresses must be stored so that we can go back and fill in @@ -2498,15 +3600,13 @@ struct NameContext { ** sequences for the ORDER BY clause. */ struct Select { - ExprList *pEList; /* The fields of the result */ u8 op; /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */ - u16 selFlags; /* Various SF_* values */ + LogEst nSelectRow; /* Estimated number of result rows */ + u32 selFlags; /* Various SF_* values */ int iLimit, iOffset; /* Memory registers holding LIMIT & OFFSET counters */ -#if SELECTTRACE_ENABLED - char zSelName[12]; /* Symbolic name of this SELECT use for debugging */ -#endif + u32 selId; /* Unique identifier number for this SELECT */ int addrOpenEphm[2]; /* OP_OpenEphem opcodes related to this select */ - u64 nSelectRow; /* Estimated number of result rows */ + ExprList *pEList; /* The fields of the result */ SrcList *pSrc; /* The FROM clause */ Expr *pWhere; /* The WHERE clause */ ExprList *pGroupBy; /* The GROUP BY clause */ @@ -2515,38 +3615,66 @@ struct Select { Select *pPrior; /* Prior select in a compound select statement */ Select *pNext; /* Next select to the left in a compound */ Expr *pLimit; /* LIMIT expression. NULL means not used. */ - Expr *pOffset; /* OFFSET expression. NULL means not used. */ With *pWith; /* WITH clause attached to this select. Or NULL. */ +#ifndef SQLITE_OMIT_WINDOWFUNC + Window *pWin; /* List of window functions */ + Window *pWinDefn; /* List of named window definitions */ +#endif }; /* ** Allowed values for Select.selFlags. The "SF" prefix stands for ** "Select Flag". -*/ -#define SF_Distinct 0x0001 /* Output should be DISTINCT */ -#define SF_All 0x0002 /* Includes the ALL keyword */ -#define SF_Resolved 0x0004 /* Identifiers have been resolved */ -#define SF_Aggregate 0x0008 /* Contains aggregate functions */ -#define SF_UsesEphemeral 0x0010 /* Uses the OpenEphemeral opcode */ -#define SF_Expanded 0x0020 /* sqlite3SelectExpand() called on this */ -#define SF_HasTypeInfo 0x0040 /* FROM subqueries have Table metadata */ -#define SF_Compound 0x0080 /* Part of a compound query */ -#define SF_Values 0x0100 /* Synthesized from VALUES clause */ -#define SF_MultiValue 0x0200 /* Single VALUES term with multiple rows */ -#define SF_NestedFrom 0x0400 /* Part of a parenthesized FROM clause */ -#define SF_MaybeConvert 0x0800 /* Need convertCompoundSelectToSubquery() */ -#define SF_MinMaxAgg 0x1000 /* Aggregate containing min() or max() */ -#define SF_Recursive 0x2000 /* The recursive part of a recursive CTE */ -#define SF_Converted 0x4000 /* By convertCompoundSelectToSubquery() */ -#define SF_IncludeHidden 0x8000 /* Include hidden columns in output */ - +** +** Value constraints (all checked via assert()) +** SF_HasAgg == NC_HasAgg +** SF_MinMaxAgg == NC_MinMaxAgg == SQLITE_FUNC_MINMAX +** SF_OrderByReqd == NC_OrderAgg == SQLITE_FUNC_ANYORDER +** SF_FixedLimit == WHERE_USE_LIMIT +*/ +#define SF_Distinct 0x0000001 /* Output should be DISTINCT */ +#define SF_All 0x0000002 /* Includes the ALL keyword */ +#define SF_Resolved 0x0000004 /* Identifiers have been resolved */ +#define SF_Aggregate 0x0000008 /* Contains agg functions or a GROUP BY */ +#define SF_HasAgg 0x0000010 /* Contains aggregate functions */ +#define SF_UsesEphemeral 0x0000020 /* Uses the OpenEphemeral opcode */ +#define SF_Expanded 0x0000040 /* sqlite3SelectExpand() called on this */ +#define SF_HasTypeInfo 0x0000080 /* FROM subqueries have Table metadata */ +#define SF_Compound 0x0000100 /* Part of a compound query */ +#define SF_Values 0x0000200 /* Synthesized from VALUES clause */ +#define SF_MultiValue 0x0000400 /* Single VALUES term with multiple rows */ +#define SF_NestedFrom 0x0000800 /* Part of a parenthesized FROM clause */ +#define SF_MinMaxAgg 0x0001000 /* Aggregate containing min() or max() */ +#define SF_Recursive 0x0002000 /* The recursive part of a recursive CTE */ +#define SF_FixedLimit 0x0004000 /* nSelectRow set by a constant LIMIT */ +#define SF_MaybeConvert 0x0008000 /* Need convertCompoundSelectToSubquery() */ +#define SF_Converted 0x0010000 /* By convertCompoundSelectToSubquery() */ +#define SF_IncludeHidden 0x0020000 /* Include hidden columns in output */ +#define SF_ComplexResult 0x0040000 /* Result contains subquery or function */ +#define SF_WhereBegin 0x0080000 /* Really a WhereBegin() call. Debug Only */ +#define SF_WinRewrite 0x0100000 /* Window function rewrite accomplished */ +#define SF_View 0x0200000 /* SELECT statement is a view */ +#define SF_NoopOrderBy 0x0400000 /* ORDER BY is ignored for this query */ +#define SF_UFSrcCheck 0x0800000 /* Check pSrc as required by UPDATE...FROM */ +#define SF_PushDown 0x1000000 /* Modified by WHERE-clause push-down opt */ +#define SF_MultiPart 0x2000000 /* Has multiple incompatible PARTITIONs */ +#define SF_CopyCte 0x4000000 /* SELECT statement is a copy of a CTE */ +#define SF_OrderByReqd 0x8000000 /* The ORDER BY clause may not be omitted */ +#define SF_UpdateFrom 0x10000000 /* Query originates with UPDATE FROM */ +#define SF_Correlated 0x20000000 /* True if references the outer context */ +#define SF_OnToWhere 0x40000000 /* One or more ON clauses moved to WHERE */ + +/* True if SrcItem X is a subquery that has SF_NestedFrom */ +#define IsNestedFrom(X) \ + ((X)->fg.isSubquery && \ + ((X)->u4.pSubq->pSelect->selFlags&SF_NestedFrom)!=0) /* ** The results of a SELECT can be distributed in several ways, as defined ** by one of the following macros. The "SRT" prefix means "SELECT Result ** Type". ** -** SRT_Union Store results as a key in a temporary index +** SRT_Union Store results as a key in a temporary index ** identified by pDest->iSDParm. ** ** SRT_Except Remove results from the temporary index pDest->iSDParm. @@ -2558,9 +3686,6 @@ struct Select { ** statements within triggers whose only purpose is ** the side-effects of functions. ** -** All of the above are free to ignore their ORDER BY clause. Those that -** follow must honor the ORDER BY clause. -** ** SRT_Output Generate a row of output (using the OP_ResultRow ** opcode) for each row in the result set. ** @@ -2570,9 +3695,13 @@ struct Select { ** of the query. This destination implies "LIMIT 1". ** ** SRT_Set The result must be a single column. Store each -** row of result as the key in table pDest->iSDParm. +** row of result as the key in table pDest->iSDParm. ** Apply the affinity pDest->affSdst before storing -** results. Used to implement "IN (SELECT ...)". +** results. if pDest->iSDParm2 is positive, then it is +** a register holding a Bloom filter for the IN operator +** that should be populated in addition to the +** pDest->iSDParm table. This SRT is used to +** implement "IN (SELECT ...)". ** ** SRT_EphemTab Create an temporary table pDest->iSDParm and store ** the result there. The cursor is left open after @@ -2604,18 +3733,31 @@ struct Select { ** SRT_DistQueue Store results in priority queue pDest->iSDParm only if ** the same record has never been stored before. The ** index at pDest->iSDParm+1 hold all prior stores. +** +** SRT_Upfrom Store results in the temporary table already opened by +** pDest->iSDParm. If (pDest->iSDParm<0), then the temp +** table is an intkey table - in this case the first +** column returned by the SELECT is used as the integer +** key. If (pDest->iSDParm>0), then the table is an index +** table. (pDest->iSDParm) is the number of key columns in +** each index record in this case. */ #define SRT_Union 1 /* Store result as keys in an index */ #define SRT_Except 2 /* Remove result from a UNION index */ #define SRT_Exists 3 /* Store 1 if the result is not empty */ #define SRT_Discard 4 /* Do not save the results anywhere */ -#define SRT_Fifo 5 /* Store result as data with an automatic rowid */ -#define SRT_DistFifo 6 /* Like SRT_Fifo, but unique results only */ +#define SRT_DistFifo 5 /* Like SRT_Fifo, but unique results only */ +#define SRT_DistQueue 6 /* Like SRT_Queue, but unique results only */ + +/* The DISTINCT clause is ignored for all of the above. Not that +** IgnorableDistinct() implies IgnorableOrderby() */ +#define IgnorableDistinct(X) ((X->eDest)<=SRT_DistQueue) + #define SRT_Queue 7 /* Store result in an queue */ -#define SRT_DistQueue 8 /* Like SRT_Queue, but unique results only */ +#define SRT_Fifo 8 /* Store result as data with an automatic rowid */ /* The ORDER BY clause is ignored for all of the above */ -#define IgnorableOrderby(X) ((X->eDest)<=SRT_DistQueue) +#define IgnorableOrderby(X) ((X->eDest)<=SRT_Fifo) #define SRT_Output 9 /* Output each row of result */ #define SRT_Mem 10 /* Store result in a memory cell */ @@ -2623,22 +3765,24 @@ struct Select { #define SRT_EphemTab 12 /* Create transient tab and store like SRT_Table */ #define SRT_Coroutine 13 /* Generate a single row of result */ #define SRT_Table 14 /* Store result as data with an automatic rowid */ +#define SRT_Upfrom 15 /* Store result as data with rowid */ /* ** An instance of this object describes where to put of the results of ** a SELECT statement. */ struct SelectDest { - u8 eDest; /* How to dispose of the results. On of SRT_* above. */ - char affSdst; /* Affinity used when eDest==SRT_Set */ + u8 eDest; /* How to dispose of the results. One of SRT_* above. */ int iSDParm; /* A parameter used by the eDest disposal method */ + int iSDParm2; /* A second parameter for the eDest disposal method */ int iSdst; /* Base register where results are written */ int nSdst; /* Number of registers allocated */ + char *zAffSdst; /* Affinity used for SRT_Set */ ExprList *pOrderBy; /* Key columns for SRT_Queue and SRT_DistQueue */ }; /* -** During code generation of statements that do inserts into AUTOINCREMENT +** During code generation of statements that do inserts into AUTOINCREMENT ** tables, the following information is attached to the Table.u.autoInc.p ** pointer of each autoincrement table to record some side information that ** the code generator needs. We have to keep per-table autoincrement @@ -2654,14 +3798,7 @@ struct AutoincInfo { }; /* -** Size of the column cache -*/ -#ifndef SQLITE_N_COLCACHE -# define SQLITE_N_COLCACHE 10 -#endif - -/* -** At least one instance of the following structure is created for each +** At least one instance of the following structure is created for each ** trigger that may be fired while parsing an INSERT, UPDATE or DELETE ** statement. All such objects are stored in the linked list headed at ** Parse.pTriggerPrg and deleted once statement compilation has been @@ -2674,7 +3811,7 @@ struct AutoincInfo { ** values for both pTrigger and orconf. ** ** The TriggerPrg.aColmask[0] variable is set to a mask of old.* columns -** accessed (or set to 0 for triggers fired as a result of INSERT +** accessed (or set to 0 for triggers fired as a result of INSERT ** statements). Similarly, the TriggerPrg.aColmask[1] variable is set to ** a mask of new.* columns used by the program. */ @@ -2699,11 +3836,45 @@ struct TriggerPrg { #else typedef unsigned int yDbMask; # define DbMaskTest(M,I) (((M)&(((yDbMask)1)<<(I)))!=0) -# define DbMaskZero(M) (M)=0 -# define DbMaskSet(M,I) (M)|=(((yDbMask)1)<<(I)) -# define DbMaskAllZero(M) (M)==0 -# define DbMaskNonZero(M) (M)!=0 +# define DbMaskZero(M) ((M)=0) +# define DbMaskSet(M,I) ((M)|=(((yDbMask)1)<<(I))) +# define DbMaskAllZero(M) ((M)==0) +# define DbMaskNonZero(M) ((M)!=0) +#endif + +/* +** For each index X that has as one of its arguments either an expression +** or the name of a virtual generated column, and if X is in scope such that +** the value of the expression can simply be read from the index, then +** there is an instance of this object on the Parse.pIdxExpr list. +** +** During code generation, while generating code to evaluate expressions, +** this list is consulted and if a matching expression is found, the value +** is read from the index rather than being recomputed. +*/ +struct IndexedExpr { + Expr *pExpr; /* The expression contained in the index */ + int iDataCur; /* The data cursor associated with the index */ + int iIdxCur; /* The index cursor */ + int iIdxCol; /* The index column that contains value of pExpr */ + u8 bMaybeNullRow; /* True if we need an OP_IfNullRow check */ + u8 aff; /* Affinity of the pExpr expression */ + IndexedExpr *pIENext; /* Next in a list of all indexed expressions */ +#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS + const char *zIdxName; /* Name of index, used only for bytecode comments */ #endif +}; + +/* +** An instance of the ParseCleanup object specifies an operation that +** should be performed after parsing to deallocation resources obtained +** during the parse and which are no longer needed. +*/ +struct ParseCleanup { + ParseCleanup *pNext; /* Next cleanup task */ + void *pPtr; /* Pointer to object to deallocate */ + void (*xCleanup)(sqlite3*,void*); /* Deallocation routine */ +}; /* ** An SQL parser context. A copy of this structure is passed through @@ -2715,7 +3886,7 @@ struct TriggerPrg { ** is constant but the second part is reset at the beginning and end of ** each recursion. ** -** The nTableLock and aTableLock variables are only used if the shared-cache +** The nTableLock and aTableLock variables are only used if the shared-cache ** feature is enabled (if sqlite3Tsd()->useSharedData is true). They are ** used to store the set of table-locks required by the statement being ** compiled. Function sqlite3TableLock() is used to add entries to the @@ -2726,115 +3897,159 @@ struct Parse { char *zErrMsg; /* An error message */ Vdbe *pVdbe; /* An engine for executing database bytecode */ int rc; /* Return code from execution */ - u8 colNamesSet; /* TRUE after OP_ColumnName has been issued to pVdbe */ - u8 checkSchema; /* Causes schema cookie check after an error */ + LogEst nQueryLoop; /* Est number of iterations of a query (10*log2(N)) */ u8 nested; /* Number of nested calls to the parser/code generator */ u8 nTempReg; /* Number of temporary registers in aTempReg[] */ u8 isMultiWrite; /* True if statement may modify/insert multiple rows */ u8 mayAbort; /* True if statement may throw an ABORT exception */ u8 hasCompound; /* Need to invoke convertCompoundSelectToSubquery() */ - u8 okConstFactor; /* OK to factor out constants */ u8 disableLookaside; /* Number of times lookaside has been disabled */ - int aTempReg[8]; /* Holding area for temporary registers */ + u8 prepFlags; /* SQLITE_PREPARE_* flags */ + u8 withinRJSubrtn; /* Nesting level for RIGHT JOIN body subroutines */ + u8 bHasExists; /* Has a correlated "EXISTS (SELECT ....)" expression */ + u8 mSubrtnSig; /* mini Bloom filter on available SubrtnSig.selId */ + u8 eTriggerOp; /* TK_UPDATE, TK_INSERT or TK_DELETE */ + u8 bReturning; /* Coding a RETURNING trigger */ + u8 eOrconf; /* Default ON CONFLICT policy for trigger steps */ + u8 disableTriggers; /* True to disable triggers */ +#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST) + u8 earlyCleanup; /* OOM inside sqlite3ParserAddCleanup() */ +#endif +#ifdef SQLITE_DEBUG + u8 ifNotExists; /* Might be true if IF NOT EXISTS. Assert()s only */ + u8 isCreate; /* CREATE TABLE, INDEX, or VIEW (but not TRIGGER) + ** and ALTER TABLE ADD COLUMN. */ +#endif + bft colNamesSet :1; /* TRUE after OP_ColumnName has been issued to pVdbe */ + bft bHasWith :1; /* True if statement contains WITH */ + bft okConstFactor :1; /* OK to factor out constants */ + bft checkSchema :1; /* Causes schema cookie check after an error */ int nRangeReg; /* Size of the temporary register block */ int iRangeReg; /* First register in temporary register block */ int nErr; /* Number of errors seen */ int nTab; /* Number of previously allocated VDBE cursors */ int nMem; /* Number of memory cells used so far */ - int nSet; /* Number of sets used so far */ - int nOnce; /* Number of OP_Once instructions so far */ - int nOpAlloc; /* Number of slots allocated for Vdbe.aOp[] */ int szOpAlloc; /* Bytes of memory space allocated for Vdbe.aOp[] */ - int iFixedOp; /* Never back out opcodes iFixedOp-1 or earlier */ - int ckBase; /* Base register of data during check constraints */ - int iSelfTab; /* Table of an index whose exprs are being coded */ - int iCacheLevel; /* ColCache valid when aColCache[].iLevel<=iCacheLevel */ - int iCacheCnt; /* Counter used to generate aColCache[].lru values */ - int nLabel; /* Number of labels used */ + int iSelfTab; /* Table associated with an index on expr, or negative + ** of the base register during check-constraint eval */ + int nLabel; /* The *negative* of the number of labels used */ + int nLabelAlloc; /* Number of slots in aLabel */ int *aLabel; /* Space to hold the labels */ - struct yColCache { - int iTable; /* Table cursor number */ - i16 iColumn; /* Table column number */ - u8 tempReg; /* iReg is a temp register that needs to be freed */ - int iLevel; /* Nesting level */ - int iReg; /* Reg with value of this column. 0 means none. */ - int lru; /* Least recently used entry has the smallest value */ - } aColCache[SQLITE_N_COLCACHE]; /* One for each column cache entry */ ExprList *pConstExpr;/* Constant expressions */ - Token constraintName;/* Name of the constraint currently being parsed */ + IndexedExpr *pIdxEpr;/* List of expressions used by active indexes */ + IndexedExpr *pIdxPartExpr; /* Exprs constrained by index WHERE clauses */ yDbMask writeMask; /* Start a write transaction on these databases */ yDbMask cookieMask; /* Bitmask of schema verified databases */ - int cookieValue[SQLITE_MAX_ATTACHED+2]; /* Values of cookies to verify */ - int regRowid; /* Register holding rowid of CREATE TABLE entry */ - int regRoot; /* Register holding root page number for new objects */ - int nMaxArg; /* Max args passed to user function by sub-program */ -#if SELECTTRACE_ENABLED - int nSelect; /* Number of SELECT statements seen */ - int nSelectIndent; /* How far to indent SELECTTRACE() output */ + int nMaxArg; /* Max args to xUpdate and xFilter vtab methods */ + int nSelect; /* Number of SELECT stmts. Counter for Select.selId */ +#ifndef SQLITE_OMIT_PROGRESS_CALLBACK + u32 nProgressSteps; /* xProgress steps taken during sqlite3_prepare() */ #endif #ifndef SQLITE_OMIT_SHARED_CACHE int nTableLock; /* Number of locks in aTableLock */ TableLock *aTableLock; /* Required table locks for shared-cache mode */ #endif AutoincInfo *pAinc; /* Information about AUTOINCREMENT counters */ - - /* Information used while coding trigger programs. */ Parse *pToplevel; /* Parse structure for main program (or NULL) */ Table *pTriggerTab; /* Table triggers are being coded for */ - int addrCrTab; /* Address of OP_CreateTable opcode on CREATE TABLE */ - u32 nQueryLoop; /* Est number of iterations of a query (10*log2(N)) */ - u32 oldmask; /* Mask of old.* columns referenced */ - u32 newmask; /* Mask of new.* columns referenced */ - u8 eTriggerOp; /* TK_UPDATE, TK_INSERT or TK_DELETE */ - u8 eOrconf; /* Default ON CONFLICT policy for trigger steps */ - u8 disableTriggers; /* True to disable triggers */ + TriggerPrg *pTriggerPrg; /* Linked list of coded triggers */ + ParseCleanup *pCleanup; /* List of cleanup operations to run after parse */ + + /************************************************************************** + ** Fields above must be initialized to zero. The fields that follow, + ** down to the beginning of the recursive section, do not need to be + ** initialized as they will be set before being used. The boundary is + ** determined by offsetof(Parse,aTempReg). + **************************************************************************/ + + int aTempReg[8]; /* Holding area for temporary registers */ + Parse *pOuterParse; /* Outer Parse object when nested */ + Token sNameToken; /* Token with unqualified schema object name */ + u32 oldmask; /* Mask of old.* columns referenced */ + u32 newmask; /* Mask of new.* columns referenced */ + union { + struct { /* These fields available when isCreate is true */ + int addrCrTab; /* Address of OP_CreateBtree on CREATE TABLE */ + int regRowid; /* Register holding rowid of CREATE TABLE entry */ + int regRoot; /* Register holding root page for new objects */ + Token constraintName; /* Name of the constraint currently being parsed */ + } cr; + struct { /* These fields available to all other statements */ + Returning *pReturning; /* The RETURNING clause */ + } d; + } u1; /************************************************************************ ** Above is constant between recursions. Below is reset before and after ** each recursion. The boundary between these two regions is determined - ** using offsetof(Parse,nVar) so the nVar field must be the first field - ** in the recursive region. + ** using offsetof(Parse,sLastToken) so the sLastToken field must be the + ** first field in the recursive region. ************************************************************************/ + Token sLastToken; /* The last token parsed */ ynVar nVar; /* Number of '?' variables seen in the SQL so far */ - int nzVar; /* Number of available slots in azVar[] */ u8 iPkSortOrder; /* ASC or DESC for INTEGER PRIMARY KEY */ u8 explain; /* True if the EXPLAIN flag is found on the query */ + u8 eParseMode; /* PARSE_MODE_XXX constant */ #ifndef SQLITE_OMIT_VIRTUALTABLE - u8 declareVtab; /* True if inside sqlite3_declare_vtab() */ int nVtabLock; /* Number of virtual tables to lock */ #endif - int nAlias; /* Number of aliased result set columns */ int nHeight; /* Expression tree height of current sub-select */ -#ifndef SQLITE_OMIT_EXPLAIN - int iSelectId; /* ID of current select for EXPLAIN output */ - int iNextSelectId; /* Next available select ID for EXPLAIN output */ -#endif - char **azVar; /* Pointers to names of parameters */ + int addrExplain; /* Address of current OP_Explain opcode */ + VList *pVList; /* Mapping between variable names and numbers */ Vdbe *pReprepare; /* VM being reprepared (sqlite3Reprepare()) */ const char *zTail; /* All SQL text past the last semicolon parsed */ Table *pNewTable; /* A table being constructed by CREATE TABLE */ + Index *pNewIndex; /* An index being constructed by CREATE INDEX. + ** Also used to hold redundant UNIQUE constraints + ** during a RENAME COLUMN */ Trigger *pNewTrigger; /* Trigger under construct by a CREATE TRIGGER */ const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */ - Token sNameToken; /* Token with unqualified schema object name */ - Token sLastToken; /* The last token parsed */ #ifndef SQLITE_OMIT_VIRTUALTABLE Token sArg; /* Complete text of a module argument */ Table **apVtabLock; /* Pointer to virtual tables needing locking */ #endif - Table *pZombieTab; /* List of Table objects to delete after code gen */ - TriggerPrg *pTriggerPrg; /* Linked list of coded triggers */ With *pWith; /* Current WITH clause, or NULL */ - With *pWithToFree; /* Free this WITH object at the end of the parse */ +#ifndef SQLITE_OMIT_ALTERTABLE + RenameToken *pRename; /* Tokens subject to renaming by ALTER TABLE */ +#endif }; +/* Allowed values for Parse.eParseMode +*/ +#define PARSE_MODE_NORMAL 0 +#define PARSE_MODE_DECLARE_VTAB 1 +#define PARSE_MODE_RENAME 2 +#define PARSE_MODE_UNMAP 3 + +/* +** Sizes and pointers of various parts of the Parse object. +*/ +#define PARSE_HDR(X) (((char*)(X))+offsetof(Parse,zErrMsg)) +#define PARSE_HDR_SZ (offsetof(Parse,aTempReg)-offsetof(Parse,zErrMsg)) /* Recursive part w/o aColCache*/ +#define PARSE_RECURSE_SZ offsetof(Parse,sLastToken) /* Recursive part */ +#define PARSE_TAIL_SZ (sizeof(Parse)-PARSE_RECURSE_SZ) /* Non-recursive part */ +#define PARSE_TAIL(X) (((char*)(X))+PARSE_RECURSE_SZ) /* Pointer to tail */ + /* ** Return true if currently inside an sqlite3_declare_vtab() call. */ #ifdef SQLITE_OMIT_VIRTUALTABLE #define IN_DECLARE_VTAB 0 #else - #define IN_DECLARE_VTAB (pParse->declareVtab) + #define IN_DECLARE_VTAB (pParse->eParseMode==PARSE_MODE_DECLARE_VTAB) +#endif + +#if defined(SQLITE_OMIT_ALTERTABLE) + #define IN_RENAME_OBJECT 0 +#else + #define IN_RENAME_OBJECT (pParse->eParseMode>=PARSE_MODE_RENAME) +#endif + +#if defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_OMIT_ALTERTABLE) + #define IN_SPECIAL_PARSE 0 +#else + #define IN_SPECIAL_PARSE (pParse->eParseMode!=PARSE_MODE_NORMAL) #endif /* @@ -2848,44 +4063,59 @@ struct AuthContext { /* ** Bitfield flags for P5 value in various opcodes. +** +** Value constraints (enforced via assert()): +** OPFLAG_LENGTHARG == SQLITE_FUNC_LENGTH +** OPFLAG_TYPEOFARG == SQLITE_FUNC_TYPEOF +** OPFLAG_BULKCSR == BTREE_BULKLOAD +** OPFLAG_SEEKEQ == BTREE_SEEK_EQ +** OPFLAG_FORDELETE == BTREE_FORDELETE +** OPFLAG_SAVEPOSITION == BTREE_SAVEPOSITION +** OPFLAG_AUXDELETE == BTREE_AUXDELETE */ #define OPFLAG_NCHANGE 0x01 /* OP_Insert: Set to update db->nChange */ /* Also used in P2 (not P5) of OP_Delete */ +#define OPFLAG_NOCHNG 0x01 /* OP_VColumn nochange for UPDATE */ #define OPFLAG_EPHEM 0x01 /* OP_Column: Ephemeral output is ok */ -#define OPFLAG_LASTROWID 0x02 /* Set to update db->lastRowid */ +#define OPFLAG_LASTROWID 0x20 /* Set to update db->lastRowid */ #define OPFLAG_ISUPDATE 0x04 /* This OP_Insert is an sql UPDATE */ #define OPFLAG_APPEND 0x08 /* This is likely to be an append */ #define OPFLAG_USESEEKRESULT 0x10 /* Try to avoid a seek in BtreeInsert() */ +#define OPFLAG_ISNOOP 0x40 /* OP_Delete does pre-update-hook only */ #define OPFLAG_LENGTHARG 0x40 /* OP_Column only used for length() */ #define OPFLAG_TYPEOFARG 0x80 /* OP_Column only used for typeof() */ +#define OPFLAG_BYTELENARG 0xc0 /* OP_Column only for octet_length() */ #define OPFLAG_BULKCSR 0x01 /* OP_Open** used to open bulk cursor */ #define OPFLAG_SEEKEQ 0x02 /* OP_Open** cursor uses EQ seek only */ #define OPFLAG_FORDELETE 0x08 /* OP_Open should use BTREE_FORDELETE */ #define OPFLAG_P2ISREG 0x10 /* P2 to OP_Open** is a register number */ #define OPFLAG_PERMUTE 0x01 /* OP_Compare: use the permutation */ -#define OPFLAG_SAVEPOSITION 0x02 /* OP_Delete: keep cursor position */ +#define OPFLAG_SAVEPOSITION 0x02 /* OP_Delete/Insert: save cursor pos */ #define OPFLAG_AUXDELETE 0x04 /* OP_Delete: index in a DELETE op */ +#define OPFLAG_NOCHNG_MAGIC 0x6d /* OP_MakeRecord: serialtype 10 is ok */ +#define OPFLAG_PREFORMAT 0x80 /* OP_Insert uses preformatted cell */ /* - * Each trigger present in the database schema is stored as an instance of - * struct Trigger. - * - * Pointers to instances of struct Trigger are stored in two ways. - * 1. In the "trigHash" hash table (part of the sqlite3* that represents the - * database). This allows Trigger structures to be retrieved by name. - * 2. All triggers associated with a single table form a linked list, using the - * pNext member of struct Trigger. A pointer to the first element of the - * linked list is stored as the "pTrigger" member of the associated - * struct Table. - * - * The "step_list" member points to the first element of a linked list - * containing the SQL statements specified as the trigger program. - */ +** Each trigger present in the database schema is stored as an instance of +** struct Trigger. +** +** Pointers to instances of struct Trigger are stored in two ways. +** 1. In the "trigHash" hash table (part of the sqlite3* that represents the +** database). This allows Trigger structures to be retrieved by name. +** 2. All triggers associated with a single table form a linked list, using the +** pNext member of struct Trigger. A pointer to the first element of the +** linked list is stored as the "pTrigger" member of the associated +** struct Table. +** +** The "step_list" member points to the first element of a linked list +** containing the SQL statements specified as the trigger program. +*/ struct Trigger { char *zName; /* The name of the trigger */ char *table; /* The table or view to which the trigger applies */ u8 op; /* One of TK_DELETE, TK_UPDATE, TK_INSERT */ u8 tr_tm; /* One of TRIGGER_BEFORE, TRIGGER_AFTER */ + u8 bReturning; /* This trigger implements a RETURNING clause */ Expr *pWhen; /* The WHEN clause of the expression (may be NULL) */ IdList *pColumns; /* If this is an UPDATE OF <column-list> trigger, the <column-list> is stored here */ @@ -2897,7 +4127,7 @@ struct Trigger { /* ** A trigger is either a BEFORE or an AFTER trigger. The following constants -** determine which. +** determine which. ** ** If there are multiple triggers, you might of some BEFORE and some AFTER. ** In that cases, the constants below can be ORed together. @@ -2906,93 +4136,120 @@ struct Trigger { #define TRIGGER_AFTER 2 /* - * An instance of struct TriggerStep is used to store a single SQL statement - * that is a part of a trigger-program. - * - * Instances of struct TriggerStep are stored in a singly linked list (linked - * using the "pNext" member) referenced by the "step_list" member of the - * associated struct Trigger instance. The first element of the linked list is - * the first step of the trigger-program. - * - * The "op" member indicates whether this is a "DELETE", "INSERT", "UPDATE" or - * "SELECT" statement. The meanings of the other members is determined by the - * value of "op" as follows: - * - * (op == TK_INSERT) - * orconf -> stores the ON CONFLICT algorithm - * pSelect -> If this is an INSERT INTO ... SELECT ... statement, then - * this stores a pointer to the SELECT statement. Otherwise NULL. - * zTarget -> Dequoted name of the table to insert into. - * pExprList -> If this is an INSERT INTO ... VALUES ... statement, then - * this stores values to be inserted. Otherwise NULL. - * pIdList -> If this is an INSERT INTO ... (<column-names>) VALUES ... - * statement, then this stores the column-names to be - * inserted into. - * - * (op == TK_DELETE) - * zTarget -> Dequoted name of the table to delete from. - * pWhere -> The WHERE clause of the DELETE statement if one is specified. - * Otherwise NULL. - * - * (op == TK_UPDATE) - * zTarget -> Dequoted name of the table to update. - * pWhere -> The WHERE clause of the UPDATE statement if one is specified. - * Otherwise NULL. - * pExprList -> A list of the columns to update and the expressions to update - * them to. See sqlite3Update() documentation of "pChanges" - * argument. - * - */ +** An instance of struct TriggerStep is used to store a single SQL statement +** that is a part of a trigger-program. +** +** Instances of struct TriggerStep are stored in a singly linked list (linked +** using the "pNext" member) referenced by the "step_list" member of the +** associated struct Trigger instance. The first element of the linked list is +** the first step of the trigger-program. +** +** The "op" member indicates whether this is a "DELETE", "INSERT", "UPDATE" or +** "SELECT" statement. The meanings of the other members is determined by the +** value of "op" as follows: +** +** (op == TK_INSERT) +** orconf -> stores the ON CONFLICT algorithm +** pSelect -> The content to be inserted - either a SELECT statement or +** a VALUES clause. +** zTarget -> Dequoted name of the table to insert into. +** pIdList -> If this is an INSERT INTO ... (<column-names>) VALUES ... +** statement, then this stores the column-names to be +** inserted into. +** pUpsert -> The ON CONFLICT clauses for an Upsert +** +** (op == TK_DELETE) +** zTarget -> Dequoted name of the table to delete from. +** pWhere -> The WHERE clause of the DELETE statement if one is specified. +** Otherwise NULL. +** +** (op == TK_UPDATE) +** zTarget -> Dequoted name of the table to update. +** pWhere -> The WHERE clause of the UPDATE statement if one is specified. +** Otherwise NULL. +** pExprList -> A list of the columns to update and the expressions to update +** them to. See sqlite3Update() documentation of "pChanges" +** argument. +** +** (op == TK_SELECT) +** pSelect -> The SELECT statement +** +** (op == TK_RETURNING) +** pExprList -> The list of expressions that follow the RETURNING keyword. +** +*/ struct TriggerStep { - u8 op; /* One of TK_DELETE, TK_UPDATE, TK_INSERT, TK_SELECT */ + u8 op; /* One of TK_DELETE, TK_UPDATE, TK_INSERT, TK_SELECT, + ** or TK_RETURNING */ u8 orconf; /* OE_Rollback etc. */ Trigger *pTrig; /* The trigger that this step is a part of */ Select *pSelect; /* SELECT statement or RHS of INSERT INTO SELECT ... */ char *zTarget; /* Target table for DELETE, UPDATE, INSERT */ + SrcList *pFrom; /* FROM clause for UPDATE statement (if any) */ Expr *pWhere; /* The WHERE clause for DELETE or UPDATE steps */ - ExprList *pExprList; /* SET clause for UPDATE. */ + ExprList *pExprList; /* SET clause for UPDATE, or RETURNING clause */ IdList *pIdList; /* Column names for INSERT */ + Upsert *pUpsert; /* Upsert clauses on an INSERT */ + char *zSpan; /* Original SQL text of this command */ TriggerStep *pNext; /* Next in the link-list */ TriggerStep *pLast; /* Last element in link-list. Valid for 1st elem only */ }; /* -** The following structure contains information used by the sqliteFix... -** routines as they walk the parse tree to make database references -** explicit. +** Information about a RETURNING clause */ -typedef struct DbFixer DbFixer; -struct DbFixer { - Parse *pParse; /* The parsing context. Error messages written here */ - Schema *pSchema; /* Fix items to this schema */ - int bVarOnly; /* Check for variable references only */ - const char *zDb; /* Make sure all objects are contained in this database */ - const char *zType; /* Type of the container - used for error messages */ - const Token *pName; /* Name of the container - used for error messages */ +struct Returning { + Parse *pParse; /* The parse that includes the RETURNING clause */ + ExprList *pReturnEL; /* List of expressions to return */ + Trigger retTrig; /* The transient trigger that implements RETURNING */ + TriggerStep retTStep; /* The trigger step */ + int iRetCur; /* Transient table holding RETURNING results */ + int nRetCol; /* Number of in pReturnEL after expansion */ + int iRetReg; /* Register array for holding a row of RETURNING */ + char zName[40]; /* Name of trigger: "sqlite_returning_%p" */ }; /* -** An objected used to accumulate the text of a string where we +** An object used to accumulate the text of a string where we ** do not necessarily know how big the string will be in the end. */ -struct StrAccum { +struct sqlite3_str { sqlite3 *db; /* Optional database for lookaside. Can be NULL */ - char *zBase; /* A base allocation. Not from malloc. */ char *zText; /* The string collected so far */ - u32 nChar; /* Length of the string so far */ u32 nAlloc; /* Amount of space allocated in zText */ u32 mxAlloc; /* Maximum allowed allocation. 0 for no malloc usage */ - u8 accError; /* STRACCUM_NOMEM or STRACCUM_TOOBIG */ + u32 nChar; /* Length of the string so far */ + u8 accError; /* SQLITE_NOMEM or SQLITE_TOOBIG */ u8 printfFlags; /* SQLITE_PRINTF flags below */ }; -#define STRACCUM_NOMEM 1 -#define STRACCUM_TOOBIG 2 #define SQLITE_PRINTF_INTERNAL 0x01 /* Internal-use-only converters allowed */ #define SQLITE_PRINTF_SQLFUNC 0x02 /* SQL function arguments to VXPrintf */ -#define SQLITE_PRINTF_MALLOCED 0x04 /* True if xText is allocated space */ +#define SQLITE_PRINTF_MALLOCED 0x04 /* True if zText is allocated space */ #define isMalloced(X) (((X)->printfFlags & SQLITE_PRINTF_MALLOCED)!=0) +/* +** The following object is the header for an "RCStr" or "reference-counted +** string". An RCStr is passed around and used like any other char* +** that has been dynamically allocated. The important interface +** differences: +** +** 1. RCStr strings are reference counted. They are deallocated +** when the reference count reaches zero. +** +** 2. Use sqlite3RCStrUnref() to free an RCStr string rather than +** sqlite3_free() +** +** 3. Make a (read-only) copy of a read-only RCStr string using +** sqlite3RCStrRef(). +** +** "String" is in the name, but an RCStr object can also be used to hold +** binary data. +*/ +struct RCStr { + u64 nRCRef; /* Number of references */ + /* Total structure size should be a multiple of 8 bytes for alignment */ +}; /* ** A pointer to this structure is used to communicate information @@ -3003,8 +4260,35 @@ typedef struct { char **pzErrMsg; /* Error message stored here */ int iDb; /* 0 for main database. 1 for TEMP, 2.. for ATTACHed */ int rc; /* Result code stored here */ + u32 mInitFlags; /* Flags controlling error messages */ + u32 nInitRow; /* Number of rows processed */ + Pgno mxPage; /* Maximum page number. 0 for no limit. */ } InitData; +/* +** Allowed values for mInitFlags +*/ +#define INITFLAG_AlterMask 0x0003 /* Types of ALTER */ +#define INITFLAG_AlterRename 0x0001 /* Reparse after a RENAME */ +#define INITFLAG_AlterDrop 0x0002 /* Reparse after a DROP COLUMN */ +#define INITFLAG_AlterAdd 0x0003 /* Reparse after an ADD COLUMN */ + +/* Tuning parameters are set using SQLITE_TESTCTRL_TUNE and are controlled +** on debug-builds of the CLI using ".testctrl tune ID VALUE". Tuning +** parameters are for temporary use during development, to help find +** optimal values for parameters in the query planner. The should not +** be used on trunk check-ins. They are a temporary mechanism available +** for transient development builds only. +** +** Tuning parameters are numbered starting with 1. +*/ +#define SQLITE_NTUNE 6 /* Should be zero for all trunk check-ins */ +#ifdef SQLITE_DEBUG +# define Tuning(X) (sqlite3Config.aTune[(X)-1]) +#else +# define Tuning(X) 0 +#endif + /* ** Structure containing global configuration data for the SQLite library. ** @@ -3012,14 +4296,20 @@ typedef struct { */ struct Sqlite3Config { int bMemstat; /* True to enable memory status */ - int bCoreMutex; /* True to enable core mutexing */ - int bFullMutex; /* True to enable full mutexing */ - int bOpenUri; /* True to interpret filenames as URIs */ - int bUseCis; /* Use covering indices for full-scans */ + u8 bCoreMutex; /* True to enable core mutexing */ + u8 bFullMutex; /* True to enable full mutexing */ + u8 bOpenUri; /* True to interpret filenames as URIs */ + u8 bUseCis; /* Use covering indices for full-scans */ + u8 bSmallMalloc; /* Avoid large memory allocations if true */ + u8 bExtraSchemaChecks; /* Verify type,name,tbl_name in schema */ +#ifdef SQLITE_DEBUG + u8 bJsonSelfcheck; /* Double-check JSON parsing */ +#endif int mxStrlen; /* Maximum string length */ int neverCorrupt; /* Database is always well-formed */ int szLookaside; /* Default lookaside buffer size */ int nLookaside; /* Default lookaside buffer count */ + int nStmtSpill; /* Stmt-journal spill-to-disk threshold */ sqlite3_mem_methods m; /* Low-level memory allocation interface */ sqlite3_mutex_methods mutex; /* Low-level mutex interface */ sqlite3_pcache_methods2 pcache2; /* Low-level page-cache interface */ @@ -3028,9 +4318,6 @@ struct Sqlite3Config { int mnReq, mxReq; /* Min and max heap requests sizes */ sqlite3_int64 szMmap; /* mmap() space per open file */ sqlite3_int64 mxMmap; /* Maximum value for szMmap */ - void *pScratch; /* Scratch memory */ - int szScratch; /* Size of each scratch buffer */ - int nScratch; /* Number of scratch buffers */ void *pPage; /* Page cache memory */ int szPage; /* Size of each page in pPage[] */ int nPage; /* Number of pages in pPage[] */ @@ -3056,13 +4343,29 @@ struct Sqlite3Config { /* The following callback (if not NULL) is invoked on every VDBE branch ** operation. Set the callback using SQLITE_TESTCTRL_VDBE_COVERAGE. */ - void (*xVdbeBranch)(void*,int iSrcLine,u8 eThis,u8 eMx); /* Callback */ + void (*xVdbeBranch)(void*,unsigned iSrcLine,u8 eThis,u8 eMx); /* Callback */ void *pVdbeBranchArg; /* 1st argument */ #endif -#ifndef SQLITE_OMIT_BUILTIN_TEST +#ifndef SQLITE_OMIT_DESERIALIZE + sqlite3_int64 mxMemdbSize; /* Default max memdb size */ +#endif +#ifndef SQLITE_UNTESTABLE int (*xTestCallback)(int); /* Invoked by sqlite3FaultSim() */ +#endif +#ifdef SQLITE_ALLOW_ROWID_IN_VIEW + u32 mNoVisibleRowid; /* TF_NoVisibleRowid if the ROWID_IN_VIEW + ** feature is disabled. 0 if rowids can + ** occur in views. */ #endif int bLocaltimeFault; /* True to fail localtime() calls */ + int (*xAltLocaltime)(const void*,void*); /* Alternative localtime() routine */ + int iOnceResetThreshold; /* When to reset OP_Once counters */ + u32 szSorterRef; /* Min size in bytes to use sorter-refs */ + unsigned int iPrngSeed; /* Alternative fixed seed for the PRNG */ + /* vvvv--- must be last ---vvv */ +#ifdef SQLITE_DEBUG + sqlite3_int64 aTune[SQLITE_NTUNE]; /* Tuning parameters */ +#endif }; /* @@ -3092,25 +4395,69 @@ struct Walker { int (*xSelectCallback)(Walker*,Select*); /* Callback for SELECTs */ void (*xSelectCallback2)(Walker*,Select*);/* Second callback for SELECTs */ int walkerDepth; /* Number of subqueries */ - u8 eCode; /* A small processing code */ + u16 eCode; /* A small processing code */ + u16 mWFlags; /* Use-dependent flags */ union { /* Extra data for callback */ - NameContext *pNC; /* Naming context */ - int n; /* A counter */ - int iCur; /* A cursor number */ - SrcList *pSrcList; /* FROM clause */ - struct SrcCount *pSrcCount; /* Counting column references */ - struct CCurHint *pCCurHint; /* Used by codeCursorHint() */ - int *aiCol; /* array of column indexes */ + NameContext *pNC; /* Naming context */ + int n; /* A counter */ + int iCur; /* A cursor number */ + SrcList *pSrcList; /* FROM clause */ + struct CCurHint *pCCurHint; /* Used by codeCursorHint() */ + struct RefSrcList *pRefSrcList; /* sqlite3ReferencesSrcList() */ + int *aiCol; /* array of column indexes */ + struct IdxCover *pIdxCover; /* Check for index coverage */ + ExprList *pGroupBy; /* GROUP BY clause */ + Select *pSelect; /* HAVING to WHERE clause ctx */ + struct WindowRewrite *pRewrite; /* Window rewrite context */ + struct WhereConst *pConst; /* WHERE clause constants */ + struct RenameCtx *pRename; /* RENAME COLUMN context */ + struct Table *pTab; /* Table of generated column */ + struct CoveringIndexCheck *pCovIdxCk; /* Check for covering index */ + SrcItem *pSrcItem; /* A single FROM clause item */ + DbFixer *pFix; /* See sqlite3FixSelect() */ + Mem *aMem; /* See sqlite3BtreeCursorHint() */ + struct CheckOnCtx *pCheckOnCtx; /* See selectCheckOnClauses() */ } u; }; +/* +** The following structure contains information used by the sqliteFix... +** routines as they walk the parse tree to make database references +** explicit. +*/ +struct DbFixer { + Parse *pParse; /* The parsing context. Error messages written here */ + Walker w; /* Walker object */ + Schema *pSchema; /* Fix items to this schema */ + u8 bTemp; /* True for TEMP schema entries */ + const char *zDb; /* Make sure all objects are contained in this database */ + const char *zType; /* Type of the container - used for error messages */ + const Token *pName; /* Name of the container - used for error messages */ +}; + /* Forward declarations */ int sqlite3WalkExpr(Walker*, Expr*); +int sqlite3WalkExprNN(Walker*, Expr*); int sqlite3WalkExprList(Walker*, ExprList*); int sqlite3WalkSelect(Walker*, Select*); int sqlite3WalkSelectExpr(Walker*, Select*); int sqlite3WalkSelectFrom(Walker*, Select*); int sqlite3ExprWalkNoop(Walker*, Expr*); +int sqlite3SelectWalkNoop(Walker*, Select*); +int sqlite3SelectWalkFail(Walker*, Select*); +int sqlite3WalkerDepthIncrease(Walker*,Select*); +void sqlite3WalkerDepthDecrease(Walker*,Select*); +void sqlite3WalkWinDefnDummyCallback(Walker*,Select*); + +#ifdef SQLITE_DEBUG +void sqlite3SelectWalkAssert2(Walker*, Select*); +#endif + +#ifndef SQLITE_OMIT_CTE +void sqlite3SelectPopWith(Walker*, Select*); +#else +# define sqlite3SelectPopWith 0 +#endif /* ** Return code from the parse-tree walking primitives and their @@ -3121,20 +4468,74 @@ int sqlite3ExprWalkNoop(Walker*, Expr*); #define WRC_Abort 2 /* Abandon the tree walk */ /* -** An instance of this structure represents a set of one or more CTEs -** (common table expressions) created by a single WITH clause. +** A single common table expression +*/ +struct Cte { + char *zName; /* Name of this CTE */ + ExprList *pCols; /* List of explicit column names, or NULL */ + Select *pSelect; /* The definition of this CTE */ + const char *zCteErr; /* Error message for circular references */ + CteUse *pUse; /* Usage information for this CTE */ + u8 eM10d; /* The MATERIALIZED flag */ +}; + +/* +** Allowed values for the materialized flag (eM10d): +*/ +#define M10d_Yes 0 /* AS MATERIALIZED */ +#define M10d_Any 1 /* Not specified. Query planner's choice */ +#define M10d_No 2 /* AS NOT MATERIALIZED */ + +/* +** An instance of the With object represents a WITH clause containing +** one or more CTEs (common table expressions). */ struct With { - int nCte; /* Number of CTEs in the WITH clause */ - With *pOuter; /* Containing WITH clause, or NULL */ - struct Cte { /* For each CTE in the WITH clause.... */ - char *zName; /* Name of this CTE */ - ExprList *pCols; /* List of explicit column names, or NULL */ - Select *pSelect; /* The definition of this CTE */ - const char *zCteErr; /* Error message for circular references */ - } a[1]; + int nCte; /* Number of CTEs in the WITH clause */ + int bView; /* Belongs to the outermost Select of a view */ + With *pOuter; /* Containing WITH clause, or NULL */ + Cte a[FLEXARRAY]; /* For each CTE in the WITH clause.... */ +}; + +/* The size (in bytes) of a With object that can hold as many +** as N different CTEs. */ +#define SZ_WITH(N) (offsetof(With,a) + (N)*sizeof(Cte)) + +/* +** The Cte object is not guaranteed to persist for the entire duration +** of code generation. (The query flattener or other parser tree +** edits might delete it.) The following object records information +** about each Common Table Expression that must be preserved for the +** duration of the parse. +** +** The CteUse objects are freed using sqlite3ParserAddCleanup() rather +** than sqlite3SelectDelete(), which is what enables them to persist +** until the end of code generation. +*/ +struct CteUse { + int nUse; /* Number of users of this CTE */ + int addrM9e; /* Start of subroutine to compute materialization */ + int regRtn; /* Return address register for addrM9e subroutine */ + int iCur; /* Ephemeral table holding the materialization */ + LogEst nRowEst; /* Estimated number of rows in the table */ + u8 eM10d; /* The MATERIALIZED flag */ +}; + + +/* Client data associated with sqlite3_set_clientdata() and +** sqlite3_get_clientdata(). +*/ +struct DbClientData { + DbClientData *pNext; /* Next in a linked list */ + void *pData; /* The data */ + void (*xDestructor)(void*); /* Destructor. Might be NULL */ + char zName[FLEXARRAY]; /* Name of this client data. MUST BE LAST */ }; +/* The size (in bytes) of a DbClientData object that can has a name +** that is N bytes long, including the zero-terminator. */ +#define SZ_DBCLIENTDATA(N) (offsetof(DbClientData,zName)+(N)) + #ifdef SQLITE_DEBUG /* ** An instance of the TreeView object is used for printing the content of @@ -3146,6 +4547,87 @@ struct TreeView { }; #endif /* SQLITE_DEBUG */ +/* +** This object is used in various ways, most (but not all) related to window +** functions. +** +** (1) A single instance of this structure is attached to the +** the Expr.y.pWin field for each window function in an expression tree. +** This object holds the information contained in the OVER clause, +** plus additional fields used during code generation. +** +** (2) All window functions in a single SELECT form a linked-list +** attached to Select.pWin. The Window.pFunc and Window.pExpr +** fields point back to the expression that is the window function. +** +** (3) The terms of the WINDOW clause of a SELECT are instances of this +** object on a linked list attached to Select.pWinDefn. +** +** (4) For an aggregate function with a FILTER clause, an instance +** of this object is stored in Expr.y.pWin with eFrmType set to +** TK_FILTER. In this case the only field used is Window.pFilter. +** +** The uses (1) and (2) are really the same Window object that just happens +** to be accessible in two different ways. Use case (3) are separate objects. +*/ +struct Window { + char *zName; /* Name of window (may be NULL) */ + char *zBase; /* Name of base window for chaining (may be NULL) */ + ExprList *pPartition; /* PARTITION BY clause */ + ExprList *pOrderBy; /* ORDER BY clause */ + u8 eFrmType; /* TK_RANGE, TK_GROUPS, TK_ROWS, or 0 */ + u8 eStart; /* UNBOUNDED, CURRENT, PRECEDING or FOLLOWING */ + u8 eEnd; /* UNBOUNDED, CURRENT, PRECEDING or FOLLOWING */ + u8 bImplicitFrame; /* True if frame was implicitly specified */ + u8 eExclude; /* TK_NO, TK_CURRENT, TK_TIES, TK_GROUP, or 0 */ + Expr *pStart; /* Expression for "<expr> PRECEDING" */ + Expr *pEnd; /* Expression for "<expr> FOLLOWING" */ + Window **ppThis; /* Pointer to this object in Select.pWin list */ + Window *pNextWin; /* Next window function belonging to this SELECT */ + Expr *pFilter; /* The FILTER expression */ + FuncDef *pWFunc; /* The function */ + int iEphCsr; /* Partition buffer or Peer buffer */ + int regAccum; /* Accumulator */ + int regResult; /* Interim result */ + int csrApp; /* Function cursor (used by min/max) */ + int regApp; /* Function register (also used by min/max) */ + int regPart; /* Array of registers for PARTITION BY values */ + Expr *pOwner; /* Expression object this window is attached to */ + int nBufferCol; /* Number of columns in buffer table */ + int iArgCol; /* Offset of first argument for this function */ + int regOne; /* Register containing constant value 1 */ + int regStartRowid; + int regEndRowid; + u8 bExprArgs; /* Defer evaluation of window function arguments + ** due to the SQLITE_SUBTYPE flag */ +}; + +Select *sqlite3MultiValues(Parse *pParse, Select *pLeft, ExprList *pRow); +void sqlite3MultiValuesEnd(Parse *pParse, Select *pVal); + +#ifndef SQLITE_OMIT_WINDOWFUNC +void sqlite3WindowDelete(sqlite3*, Window*); +void sqlite3WindowUnlinkFromSelect(Window*); +void sqlite3WindowListDelete(sqlite3 *db, Window *p); +Window *sqlite3WindowAlloc(Parse*, int, int, Expr*, int , Expr*, u8); +void sqlite3WindowAttach(Parse*, Expr*, Window*); +void sqlite3WindowLink(Select *pSel, Window *pWin); +int sqlite3WindowCompare(const Parse*, const Window*, const Window*, int); +void sqlite3WindowCodeInit(Parse*, Select*); +void sqlite3WindowCodeStep(Parse*, Select*, WhereInfo*, int, int); +int sqlite3WindowRewrite(Parse*, Select*); +void sqlite3WindowUpdate(Parse*, Window*, Window*, FuncDef*); +Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p); +Window *sqlite3WindowListDup(sqlite3 *db, Window *p); +void sqlite3WindowFunctions(void); +void sqlite3WindowChain(Parse*, Window*, Window*); +Window *sqlite3WindowAssemble(Parse*, Window*, ExprList*, ExprList*, Token*); +#else +# define sqlite3WindowDelete(a,b) +# define sqlite3WindowFunctions() +# define sqlite3WindowAttach(a,b,c) +#endif + /* ** Assuming zIn points to the first byte of a UTF-8 character, ** advance zIn to point to the first byte of the next UTF-8 character. @@ -3163,12 +4645,28 @@ struct TreeView { ** using sqlite3_log(). The routines also provide a convenient place ** to set a debugger breakpoint. */ +int sqlite3ReportError(int iErr, int lineno, const char *zType); int sqlite3CorruptError(int); int sqlite3MisuseError(int); int sqlite3CantopenError(int); #define SQLITE_CORRUPT_BKPT sqlite3CorruptError(__LINE__) #define SQLITE_MISUSE_BKPT sqlite3MisuseError(__LINE__) #define SQLITE_CANTOPEN_BKPT sqlite3CantopenError(__LINE__) +#ifdef SQLITE_DEBUG + int sqlite3NomemError(int); + int sqlite3IoerrnomemError(int); +# define SQLITE_NOMEM_BKPT sqlite3NomemError(__LINE__) +# define SQLITE_IOERR_NOMEM_BKPT sqlite3IoerrnomemError(__LINE__) +#else +# define SQLITE_NOMEM_BKPT SQLITE_NOMEM +# define SQLITE_IOERR_NOMEM_BKPT SQLITE_IOERR_NOMEM +#endif +#if defined(SQLITE_DEBUG) || defined(SQLITE_ENABLE_CORRUPT_PGNO) + int sqlite3CorruptPgnoError(int,Pgno); +# define SQLITE_CORRUPT_PGNO(P) sqlite3CorruptPgnoError(__LINE__,(P)) +#else +# define SQLITE_CORRUPT_PGNO(P) sqlite3CorruptError(__LINE__) +#endif /* ** FTS3 and FTS4 both require virtual table support @@ -3187,15 +4685,6 @@ int sqlite3CantopenError(int); # define SQLITE_ENABLE_FTS3 1 #endif -/* -** The ctype.h header is needed for non-ASCII systems. It is also -** needed by FTS3 when FTS3 is included in the amalgamation. -*/ -#if !defined(SQLITE_ASCII) || \ - (defined(SQLITE_ENABLE_FTS3) && defined(SQLITE_AMALGAMATION)) -# include <ctype.h> -#endif - /* ** The following macros mimic the standard library functions toupper(), ** isspace(), isalnum(), isdigit() and isxdigit(), respectively. The @@ -3209,6 +4698,9 @@ int sqlite3CantopenError(int); # define sqlite3Isdigit(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x04) # define sqlite3Isxdigit(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x08) # define sqlite3Tolower(x) (sqlite3UpperToLower[(unsigned char)(x)]) +# define sqlite3Isquote(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x80) +# define sqlite3JsonId1(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x42) +# define sqlite3JsonId2(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x46) #else # define sqlite3Toupper(x) toupper((unsigned char)(x)) # define sqlite3Isspace(x) isspace((unsigned char)(x)) @@ -3217,16 +4709,19 @@ int sqlite3CantopenError(int); # define sqlite3Isdigit(x) isdigit((unsigned char)(x)) # define sqlite3Isxdigit(x) isxdigit((unsigned char)(x)) # define sqlite3Tolower(x) tolower((unsigned char)(x)) +# define sqlite3Isquote(x) ((x)=='"'||(x)=='\''||(x)=='['||(x)=='`') +# define sqlite3JsonId1(x) (sqlite3IsIdChar(x)&&(x)<'0') +# define sqlite3JsonId2(x) sqlite3IsIdChar(x) #endif -#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS int sqlite3IsIdChar(u8); -#endif /* ** Internal function prototypes */ -#define sqlite3StrICmp sqlite3_stricmp +int sqlite3StrICmp(const char*,const char*); int sqlite3Strlen30(const char*); +#define sqlite3Strlen30NN(C) (strlen(C)&0x3fffffff) +char *sqlite3ColumnType(Column*,char*); #define sqlite3StrNICmp sqlite3_strnicmp int sqlite3MallocInit(void); @@ -3238,18 +4733,19 @@ void *sqlite3DbMallocRaw(sqlite3*, u64); void *sqlite3DbMallocRawNN(sqlite3*, u64); char *sqlite3DbStrDup(sqlite3*,const char*); char *sqlite3DbStrNDup(sqlite3*,const char*, u64); +char *sqlite3DbSpanDup(sqlite3*,const char*,const char*); void *sqlite3Realloc(void*, u64); void *sqlite3DbReallocOrFree(sqlite3 *, void *, u64); void *sqlite3DbRealloc(sqlite3 *, void *, u64); void sqlite3DbFree(sqlite3*, void*); -int sqlite3MallocSize(void*); -int sqlite3DbMallocSize(sqlite3*, void*); -void *sqlite3ScratchMalloc(int); -void sqlite3ScratchFree(void*); +void sqlite3DbFreeNN(sqlite3*, void*); +void sqlite3DbNNFreeNN(sqlite3*, void*); +int sqlite3MallocSize(const void*); +int sqlite3DbMallocSize(sqlite3*, const void*); void *sqlite3PageMalloc(int); void sqlite3PageFree(void*); void sqlite3MemSetDefault(void); -#ifndef SQLITE_OMIT_BUILTIN_TEST +#ifndef SQLITE_UNTESTABLE void sqlite3BenignMallocHooks(void (*)(void), void (*)(void)); #endif int sqlite3HeapNearlyFull(void); @@ -3264,19 +4760,25 @@ int sqlite3HeapNearlyFull(void); */ #ifdef SQLITE_USE_ALLOCA # define sqlite3StackAllocRaw(D,N) alloca(N) -# define sqlite3StackAllocZero(D,N) memset(alloca(N), 0, N) -# define sqlite3StackFree(D,P) +# define sqlite3StackAllocRawNN(D,N) alloca(N) +# define sqlite3StackFree(D,P) +# define sqlite3StackFreeNN(D,P) #else # define sqlite3StackAllocRaw(D,N) sqlite3DbMallocRaw(D,N) -# define sqlite3StackAllocZero(D,N) sqlite3DbMallocZero(D,N) +# define sqlite3StackAllocRawNN(D,N) sqlite3DbMallocRawNN(D,N) # define sqlite3StackFree(D,P) sqlite3DbFree(D,P) +# define sqlite3StackFreeNN(D,P) sqlite3DbFreeNN(D,P) #endif -#ifdef SQLITE_ENABLE_MEMSYS3 -const sqlite3_mem_methods *sqlite3MemGetMemsys3(void); -#endif +/* Do not allow both MEMSYS5 and MEMSYS3 to be defined together. If they +** are, disable MEMSYS3 +*/ #ifdef SQLITE_ENABLE_MEMSYS5 const sqlite3_mem_methods *sqlite3MemGetMemsys5(void); +#undef SQLITE_ENABLE_MEMSYS3 +#endif +#ifdef SQLITE_ENABLE_MEMSYS3 +const sqlite3_mem_methods *sqlite3MemGetMemsys3(void); #endif @@ -3297,15 +4799,29 @@ sqlite3_int64 sqlite3StatusValue(int); void sqlite3StatusUp(int, int); void sqlite3StatusDown(int, int); void sqlite3StatusHighwater(int, int); +int sqlite3LookasideUsed(sqlite3*,int*); /* Access to mutexes used by sqlite3_status() */ sqlite3_mutex *sqlite3Pcache1Mutex(void); sqlite3_mutex *sqlite3MallocMutex(void); +#if defined(SQLITE_ENABLE_MULTITHREADED_CHECKS) && !defined(SQLITE_MUTEX_OMIT) +void sqlite3MutexWarnOnContention(sqlite3_mutex*); +#else +# define sqlite3MutexWarnOnContention(x) +#endif + #ifndef SQLITE_OMIT_FLOATING_POINT +# define EXP754 (((u64)0x7ff)<<52) +# define MAN754 ((((u64)1)<<52)-1) +# define IsNaN(X) (((X)&EXP754)==EXP754 && ((X)&MAN754)!=0) +# define IsOvfl(X) (((X)&EXP754)==EXP754) int sqlite3IsNaN(double); + int sqlite3IsOverflow(double); #else -# define sqlite3IsNaN(X) 0 +# define IsNaN(X) 0 +# define sqlite3IsNaN(X) 0 +# define sqlite3IsOVerflow(X) 0 #endif /* @@ -3318,8 +4834,20 @@ struct PrintfArguments { sqlite3_value **apArg; /* The argument values */ }; -void sqlite3VXPrintf(StrAccum*, const char*, va_list); -void sqlite3XPrintf(StrAccum*, const char*, ...); +/* +** An instance of this object receives the decoding of a floating point +** value into an approximate decimal representation. +*/ +struct FpDecode { + char sign; /* '+' or '-' */ + char isSpecial; /* 1: Infinity 2: NaN */ + int n; /* Significant digits in the decode */ + int iDP; /* Location of the decimal point */ + char *z; /* Start of significant digits */ + char zBuf[24]; /* Storage for significant digits */ +}; + +void sqlite3FpDecode(FpDecode*,double,int,int); char *sqlite3MPrintf(sqlite3*,const char*, ...); char *sqlite3VMPrintf(sqlite3*,const char*, va_list); #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE) @@ -3330,72 +4858,162 @@ char *sqlite3VMPrintf(sqlite3*,const char*, va_list); #endif #if defined(SQLITE_DEBUG) + void sqlite3TreeViewLine(TreeView*, const char *zFormat, ...); void sqlite3TreeViewExpr(TreeView*, const Expr*, u8); + void sqlite3TreeViewBareExprList(TreeView*, const ExprList*, const char*); void sqlite3TreeViewExprList(TreeView*, const ExprList*, u8, const char*); + void sqlite3TreeViewBareIdList(TreeView*, const IdList*, const char*); + void sqlite3TreeViewIdList(TreeView*, const IdList*, u8, const char*); + void sqlite3TreeViewColumnList(TreeView*, const Column*, int, u8); + void sqlite3TreeViewSrcList(TreeView*, const SrcList*); void sqlite3TreeViewSelect(TreeView*, const Select*, u8); void sqlite3TreeViewWith(TreeView*, const With*, u8); + void sqlite3TreeViewUpsert(TreeView*, const Upsert*, u8); +#if TREETRACE_ENABLED + void sqlite3TreeViewDelete(const With*, const SrcList*, const Expr*, + const ExprList*,const Expr*, const Trigger*); + void sqlite3TreeViewInsert(const With*, const SrcList*, + const IdList*, const Select*, const ExprList*, + int, const Upsert*, const Trigger*); + void sqlite3TreeViewUpdate(const With*, const SrcList*, const ExprList*, + const Expr*, int, const ExprList*, const Expr*, + const Upsert*, const Trigger*); +#endif +#ifndef SQLITE_OMIT_TRIGGER + void sqlite3TreeViewTriggerStep(TreeView*, const TriggerStep*, u8, u8); + void sqlite3TreeViewTrigger(TreeView*, const Trigger*, u8, u8); +#endif +#ifndef SQLITE_OMIT_WINDOWFUNC + void sqlite3TreeViewWindow(TreeView*, const Window*, u8); + void sqlite3TreeViewWinFunc(TreeView*, const Window*, u8); +#endif + void sqlite3ShowExpr(const Expr*); + void sqlite3ShowExprList(const ExprList*); + void sqlite3ShowIdList(const IdList*); + void sqlite3ShowSrcList(const SrcList*); + void sqlite3ShowSelect(const Select*); + void sqlite3ShowWith(const With*); + void sqlite3ShowUpsert(const Upsert*); +#ifndef SQLITE_OMIT_TRIGGER + void sqlite3ShowTriggerStep(const TriggerStep*); + void sqlite3ShowTriggerStepList(const TriggerStep*); + void sqlite3ShowTrigger(const Trigger*); + void sqlite3ShowTriggerList(const Trigger*); +#endif +#ifndef SQLITE_OMIT_WINDOWFUNC + void sqlite3ShowWindow(const Window*); + void sqlite3ShowWinFunc(const Window*); +#endif + void sqlite3ShowBitvec(Bitvec*); #endif - void sqlite3SetString(char **, sqlite3*, const char*); +void sqlite3ProgressCheck(Parse*); void sqlite3ErrorMsg(Parse*, const char*, ...); -int sqlite3Dequote(char*); +int sqlite3ErrorToParser(sqlite3*,int); +void sqlite3Dequote(char*); +void sqlite3DequoteExpr(Expr*); +void sqlite3DequoteToken(Token*); +void sqlite3DequoteNumber(Parse*, Expr*); void sqlite3TokenInit(Token*,char*); int sqlite3KeywordCode(const unsigned char*, int); -int sqlite3RunParser(Parse*, const char*, char **); +int sqlite3RunParser(Parse*, const char*); void sqlite3FinishCoding(Parse*); int sqlite3GetTempReg(Parse*); void sqlite3ReleaseTempReg(Parse*,int); int sqlite3GetTempRange(Parse*,int); void sqlite3ReleaseTempRange(Parse*,int,int); void sqlite3ClearTempRegCache(Parse*); +void sqlite3TouchRegister(Parse*,int); +#if defined(SQLITE_ENABLE_STAT4) || defined(SQLITE_DEBUG) +int sqlite3FirstAvailableRegister(Parse*,int); +#endif +#ifdef SQLITE_DEBUG +int sqlite3NoTempsInRange(Parse*,int,int); +#endif Expr *sqlite3ExprAlloc(sqlite3*,int,const Token*,int); Expr *sqlite3Expr(sqlite3*,int,const char*); void sqlite3ExprAttachSubtrees(sqlite3*,Expr*,Expr*,Expr*); -Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*, const Token*); -Expr *sqlite3ExprAnd(sqlite3*,Expr*, Expr*); -Expr *sqlite3ExprFunction(Parse*,ExprList*, Token*); -void sqlite3ExprAssignVarNumber(Parse*, Expr*); +Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*); +void sqlite3PExprAddSelect(Parse*, Expr*, Select*); +Expr *sqlite3ExprAnd(Parse*,Expr*, Expr*); +Expr *sqlite3ExprSimplifiedAndOr(Expr*); +Expr *sqlite3ExprFunction(Parse*,ExprList*, const Token*, int); +void sqlite3ExprAddFunctionOrderBy(Parse*,Expr*,ExprList*); +void sqlite3ExprOrderByAggregateError(Parse*,Expr*); +void sqlite3ExprFunctionUsable(Parse*,const Expr*,const FuncDef*); +void sqlite3ExprAssignVarNumber(Parse*, Expr*, u32); void sqlite3ExprDelete(sqlite3*, Expr*); +void sqlite3ExprDeleteGeneric(sqlite3*,void*); +int sqlite3ExprDeferredDelete(Parse*, Expr*); +void sqlite3ExprUnmapAndDelete(Parse*, Expr*); ExprList *sqlite3ExprListAppend(Parse*,ExprList*,Expr*); -void sqlite3ExprListSetSortOrder(ExprList*,int); -void sqlite3ExprListSetName(Parse*,ExprList*,Token*,int); -void sqlite3ExprListSetSpan(Parse*,ExprList*,ExprSpan*); +ExprList *sqlite3ExprListAppendVector(Parse*,ExprList*,IdList*,Expr*); +Select *sqlite3ExprListToValues(Parse*, int, ExprList*); +void sqlite3ExprListSetSortOrder(ExprList*,int,int); +void sqlite3ExprListSetName(Parse*,ExprList*,const Token*,int); +void sqlite3ExprListSetSpan(Parse*,ExprList*,const char*,const char*); void sqlite3ExprListDelete(sqlite3*, ExprList*); +void sqlite3ExprListDeleteGeneric(sqlite3*,void*); u32 sqlite3ExprListFlags(const ExprList*); +int sqlite3IndexHasDuplicateRootPage(Index*); int sqlite3Init(sqlite3*, char**); int sqlite3InitCallback(void*, int, char**, char**); +int sqlite3InitOne(sqlite3*, int, char**, u32); void sqlite3Pragma(Parse*,Token*,Token*,Token*,int); +#ifndef SQLITE_OMIT_VIRTUALTABLE +Module *sqlite3PragmaVtabRegister(sqlite3*,const char *zName); +#endif void sqlite3ResetAllSchemasOfConnection(sqlite3*); void sqlite3ResetOneSchema(sqlite3*,int); void sqlite3CollapseDatabaseArray(sqlite3*); void sqlite3CommitInternalChanges(sqlite3*); +void sqlite3ColumnSetExpr(Parse*,Table*,Column*,Expr*); +Expr *sqlite3ColumnExpr(Table*,Column*); +void sqlite3ColumnSetColl(sqlite3*,Column*,const char*zColl); +const char *sqlite3ColumnColl(Column*); void sqlite3DeleteColumnNames(sqlite3*,Table*); +void sqlite3GenerateColumnNames(Parse *pParse, Select *pSelect); int sqlite3ColumnsFromExprList(Parse*,ExprList*,i16*,Column**); -Table *sqlite3ResultSetOfSelect(Parse*,Select*); -void sqlite3OpenMasterTable(Parse *, int); +void sqlite3SubqueryColumnTypes(Parse*,Table*,Select*,char); +Table *sqlite3ResultSetOfSelect(Parse*,Select*,char); +void sqlite3OpenSchemaTable(Parse *, int); Index *sqlite3PrimaryKeyIndex(Table*); -i16 sqlite3ColumnOfIndex(Index*, i16); +int sqlite3TableColumnToIndex(Index*, int); +#ifdef SQLITE_OMIT_GENERATED_COLUMNS +# define sqlite3TableColumnToStorage(T,X) (X) /* No-op pass-through */ +# define sqlite3StorageColumnToTable(T,X) (X) /* No-op pass-through */ +#else + i16 sqlite3TableColumnToStorage(Table*, i16); + i16 sqlite3StorageColumnToTable(Table*, i16); +#endif void sqlite3StartTable(Parse*,Token*,Token*,int,int,int,int); #if SQLITE_ENABLE_HIDDEN_COLUMNS void sqlite3ColumnPropertiesFromName(Table*, Column*); #else # define sqlite3ColumnPropertiesFromName(T,C) /* no-op */ #endif -void sqlite3AddColumn(Parse*,Token*); +void sqlite3AddColumn(Parse*,Token,Token); void sqlite3AddNotNull(Parse*, int); void sqlite3AddPrimaryKey(Parse*, ExprList*, int, int, int); -void sqlite3AddCheckConstraint(Parse*, Expr*); -void sqlite3AddColumnType(Parse*,Token*); -void sqlite3AddDefaultValue(Parse*,ExprSpan*); +void sqlite3AddCheckConstraint(Parse*, Expr*, const char*, const char*); +void sqlite3AddDefaultValue(Parse*,Expr*,const char*,const char*); void sqlite3AddCollateType(Parse*, Token*); -void sqlite3EndTable(Parse*,Token*,Token*,u8,Select*); +void sqlite3AddGenerated(Parse*,Expr*,Token*); +void sqlite3EndTable(Parse*,Token*,Token*,u32,Select*); +void sqlite3AddReturning(Parse*,ExprList*); int sqlite3ParseUri(const char*,const char*,unsigned int*, sqlite3_vfs**,char**,char **); +/* BEGIN SQLCIPHER */ +#ifdef SQLITE_HAS_CODEC + int sqlite3CodecQueryParameters(sqlite3*,const char*,const char*); +#else +# define sqlite3CodecQueryParameters(A,B,C) 0 +#endif +/* END SQLCIPHER */ Btree *sqlite3DbNameToBtree(sqlite3*,const char*); -int sqlite3CodeOnce(Parse *); -#ifdef SQLITE_OMIT_BUILTIN_TEST +#ifdef SQLITE_UNTESTABLE # define sqlite3FaultSim(X) SQLITE_OK #else int sqlite3FaultSim(int); @@ -3408,12 +5026,13 @@ int sqlite3BitvecSet(Bitvec*, u32); void sqlite3BitvecClear(Bitvec*, u32, void*); void sqlite3BitvecDestroy(Bitvec*); u32 sqlite3BitvecSize(Bitvec*); -#ifndef SQLITE_OMIT_BUILTIN_TEST +#ifndef SQLITE_UNTESTABLE int sqlite3BitvecBuiltinTest(int,int*); #endif -RowSet *sqlite3RowSetInit(sqlite3*, void*, unsigned int); -void sqlite3RowSetClear(RowSet*); +RowSet *sqlite3RowSetInit(sqlite3*); +void sqlite3RowSetDelete(void*); +void sqlite3RowSetClear(void*); void sqlite3RowSetInsert(RowSet*, i64); int sqlite3RowSetTest(RowSet*, int iBatch, i64); int sqlite3RowSetNext(RowSet*, i64*); @@ -3432,6 +5051,8 @@ void sqlite3CreateView(Parse*,Token*,Token*,Token*,ExprList*,Select*,int,int); void sqlite3DropTable(Parse*, SrcList*, int, int); void sqlite3CodeDropTable(Parse*, Table*, int, int); void sqlite3DeleteTable(sqlite3*, Table*); +void sqlite3DeleteTableGeneric(sqlite3*, void*); +void sqlite3FreeIndex(sqlite3*, Index*); #ifndef SQLITE_OMIT_AUTOINCREMENT void sqlite3AutoincrementBegin(Parse *pParse); void sqlite3AutoincrementEnd(Parse *pParse); @@ -3439,42 +5060,56 @@ void sqlite3DeleteTable(sqlite3*, Table*); # define sqlite3AutoincrementBegin(X) # define sqlite3AutoincrementEnd(X) #endif -void sqlite3Insert(Parse*, SrcList*, Select*, IdList*, int); +void sqlite3Insert(Parse*, SrcList*, Select*, IdList*, int, Upsert*); +#ifndef SQLITE_OMIT_GENERATED_COLUMNS + void sqlite3ComputeGeneratedColumns(Parse*, int, Table*); +#endif void *sqlite3ArrayAllocate(sqlite3*,void*,int,int*,int*); -IdList *sqlite3IdListAppend(sqlite3*, IdList*, Token*); +IdList *sqlite3IdListAppend(Parse*, IdList*, Token*); int sqlite3IdListIndex(IdList*,const char*); -SrcList *sqlite3SrcListEnlarge(sqlite3*, SrcList*, int, int); -SrcList *sqlite3SrcListAppend(sqlite3*, SrcList*, Token*, Token*); +SrcList *sqlite3SrcListEnlarge(Parse*, SrcList*, int, int); +SrcList *sqlite3SrcListAppendList(Parse *pParse, SrcList *p1, SrcList *p2); +SrcList *sqlite3SrcListAppend(Parse*, SrcList*, Token*, Token*); +void sqlite3SubqueryDelete(sqlite3*,Subquery*); +Select *sqlite3SubqueryDetach(sqlite3*,SrcItem*); +int sqlite3SrcItemAttachSubquery(Parse*, SrcItem*, Select*, int); SrcList *sqlite3SrcListAppendFromTerm(Parse*, SrcList*, Token*, Token*, - Token*, Select*, Expr*, IdList*); + Token*, Select*, OnOrUsing*); void sqlite3SrcListIndexedBy(Parse *, SrcList *, Token *); void sqlite3SrcListFuncArgs(Parse*, SrcList*, ExprList*); -int sqlite3IndexedByLookup(Parse *, struct SrcList_item *); -void sqlite3SrcListShiftJoinType(SrcList*); +int sqlite3IndexedByLookup(Parse *, SrcItem *); +void sqlite3SrcListShiftJoinType(Parse*,SrcList*); void sqlite3SrcListAssignCursors(Parse*, SrcList*); void sqlite3IdListDelete(sqlite3*, IdList*); +void sqlite3ClearOnOrUsing(sqlite3*, OnOrUsing*); void sqlite3SrcListDelete(sqlite3*, SrcList*); -Index *sqlite3AllocateIndexObject(sqlite3*,i16,int,char**); -Index *sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*, - Expr*, int, int); +Index *sqlite3AllocateIndexObject(sqlite3*,int,int,char**); +void sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*, + Expr*, int, int, u8); void sqlite3DropIndex(Parse*, SrcList*, int); int sqlite3Select(Parse*, Select*, SelectDest*); Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*, - Expr*,ExprList*,u16,Expr*,Expr*); + Expr*,ExprList*,u32,Expr*); void sqlite3SelectDelete(sqlite3*, Select*); +void sqlite3SelectDeleteGeneric(sqlite3*,void*); Table *sqlite3SrcListLookup(Parse*, SrcList*); -int sqlite3IsReadOnly(Parse*, Table*, int); +int sqlite3IsReadOnly(Parse*, Table*, Trigger*); void sqlite3OpenTable(Parse*, int iCur, int iDb, Table*, int); #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY) -Expr *sqlite3LimitWhere(Parse*,SrcList*,Expr*,ExprList*,Expr*,Expr*,char*); -#endif -void sqlite3DeleteFrom(Parse*, SrcList*, Expr*); -void sqlite3Update(Parse*, SrcList*, ExprList*, Expr*, int); -WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,ExprList*,u16,int); +Expr *sqlite3LimitWhere(Parse*,SrcList*,Expr*,ExprList*,Expr*,char*); +#endif +void sqlite3CodeChangeCount(Vdbe*,int,const char*); +void sqlite3DeleteFrom(Parse*, SrcList*, Expr*, ExprList*, Expr*); +void sqlite3Update(Parse*, SrcList*, ExprList*,Expr*,int,ExprList*,Expr*, + Upsert*); +WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*, + ExprList*,Select*,u16,int); void sqlite3WhereEnd(WhereInfo*); -u64 sqlite3WhereOutputRowCount(WhereInfo*); +LogEst sqlite3WhereOutputRowCount(WhereInfo*); int sqlite3WhereIsDistinct(WhereInfo*); int sqlite3WhereIsOrdered(WhereInfo*); +int sqlite3WhereOrderByLimitOptLabel(WhereInfo*); +void sqlite3WhereMinMaxOptEarlyOut(Vdbe*,WhereInfo*); int sqlite3WhereIsSorted(WhereInfo*); int sqlite3WhereContinueLabel(WhereInfo*); int sqlite3WhereBreakLabel(WhereInfo*); @@ -3482,48 +5117,54 @@ int sqlite3WhereOkOnePass(WhereInfo*, int*); #define ONEPASS_OFF 0 /* Use of ONEPASS not allowed */ #define ONEPASS_SINGLE 1 /* ONEPASS valid for a single row update */ #define ONEPASS_MULTI 2 /* ONEPASS is valid for multiple rows */ +int sqlite3WhereUsesDeferredSeek(WhereInfo*); void sqlite3ExprCodeLoadIndexColumn(Parse*, Index*, int, int, int); int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int, u8); -void sqlite3ExprCodeGetColumnToReg(Parse*, Table*, int, int, int); void sqlite3ExprCodeGetColumnOfTable(Vdbe*, Table*, int, int, int); void sqlite3ExprCodeMove(Parse*, int, int, int); -void sqlite3ExprCacheStore(Parse*, int, int, int); -void sqlite3ExprCachePush(Parse*); -void sqlite3ExprCachePop(Parse*); -void sqlite3ExprCacheRemove(Parse*, int, int); -void sqlite3ExprCacheClear(Parse*); -void sqlite3ExprCacheAffinityChange(Parse*, int, int); +void sqlite3ExprToRegister(Expr *pExpr, int iReg); void sqlite3ExprCode(Parse*, Expr*, int); +#ifndef SQLITE_OMIT_GENERATED_COLUMNS +void sqlite3ExprCodeGeneratedColumn(Parse*, Table*, Column*, int); +#endif void sqlite3ExprCodeCopy(Parse*, Expr*, int); void sqlite3ExprCodeFactorable(Parse*, Expr*, int); -void sqlite3ExprCodeAtInit(Parse*, Expr*, int, u8); +int sqlite3ExprCodeRunJustOnce(Parse*, Expr*, int); +void sqlite3ExprNullRegisterRange(Parse*, int, int); int sqlite3ExprCodeTemp(Parse*, Expr*, int*); int sqlite3ExprCodeTarget(Parse*, Expr*, int); -void sqlite3ExprCodeAndCache(Parse*, Expr*, int); int sqlite3ExprCodeExprList(Parse*, ExprList*, int, int, u8); #define SQLITE_ECEL_DUP 0x01 /* Deep, not shallow copies */ #define SQLITE_ECEL_FACTOR 0x02 /* Factor out constant terms */ #define SQLITE_ECEL_REF 0x04 /* Use ExprList.u.x.iOrderByCol */ +#define SQLITE_ECEL_OMITREF 0x08 /* Omit if ExprList.u.x.iOrderByCol */ void sqlite3ExprIfTrue(Parse*, Expr*, int, int); void sqlite3ExprIfFalse(Parse*, Expr*, int, int); void sqlite3ExprIfFalseDup(Parse*, Expr*, int, int); Table *sqlite3FindTable(sqlite3*,const char*, const char*); -Table *sqlite3LocateTable(Parse*,int isView,const char*, const char*); -Table *sqlite3LocateTableItem(Parse*,int isView,struct SrcList_item *); +#define LOCATE_VIEW 0x01 +#define LOCATE_NOERR 0x02 +Table *sqlite3LocateTable(Parse*,u32 flags,const char*, const char*); +const char *sqlite3PreferredTableName(const char*); +Table *sqlite3LocateTableItem(Parse*,u32 flags,SrcItem *); Index *sqlite3FindIndex(sqlite3*,const char*, const char*); void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*); void sqlite3UnlinkAndDeleteIndex(sqlite3*,int,const char*); -void sqlite3Vacuum(Parse*); -int sqlite3RunVacuum(char**, sqlite3*); -char *sqlite3NameFromToken(sqlite3*, Token*); -int sqlite3ExprCompare(Expr*, Expr*, int); -int sqlite3ExprListCompare(ExprList*, ExprList*, int); -int sqlite3ExprImpliesExpr(Expr*, Expr*, int); +void sqlite3Vacuum(Parse*,Token*,Expr*); +int sqlite3RunVacuum(char**, sqlite3*, int, sqlite3_value*); +char *sqlite3NameFromToken(sqlite3*, const Token*); +int sqlite3ExprCompare(const Parse*,const Expr*,const Expr*, int); +int sqlite3ExprCompareSkip(Expr*,Expr*,int); +int sqlite3ExprListCompare(const ExprList*,const ExprList*, int); +int sqlite3ExprImpliesExpr(const Parse*,const Expr*,const Expr*, int); +int sqlite3ExprImpliesNonNullRow(Expr*,int,int); +void sqlite3AggInfoPersistWalkerInit(Walker*,Parse*); void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*); void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*); -int sqlite3FunctionUsesThisSrc(Expr*, SrcList*); +int sqlite3ExprCoveredByIndex(Expr*, int iCur, Index *pIdx); +int sqlite3ReferencesSrcList(Parse*, Expr*, SrcList*); Vdbe *sqlite3GetVdbe(Parse*); -#ifndef SQLITE_OMIT_BUILTIN_TEST +#ifndef SQLITE_UNTESTABLE void sqlite3PrngSaveState(void); void sqlite3PrngRestoreState(void); #endif @@ -3531,29 +5172,38 @@ void sqlite3RollbackAll(sqlite3*,int); void sqlite3CodeVerifySchema(Parse*, int); void sqlite3CodeVerifyNamedSchema(Parse*, const char *zDb); void sqlite3BeginTransaction(Parse*, int); -void sqlite3CommitTransaction(Parse*); -void sqlite3RollbackTransaction(Parse*); +void sqlite3EndTransaction(Parse*,int); void sqlite3Savepoint(Parse*, int, Token*); void sqlite3CloseSavepoints(sqlite3 *); void sqlite3LeaveMutexAndCloseZombie(sqlite3*); -int sqlite3ExprIsConstant(Expr*); -int sqlite3ExprIsConstantNotJoin(Expr*); +u32 sqlite3IsTrueOrFalse(const char*); +int sqlite3ExprIdToTrueFalse(Expr*); +int sqlite3ExprTruthValue(const Expr*); +int sqlite3ExprIsConstant(Parse*,Expr*); int sqlite3ExprIsConstantOrFunction(Expr*, u8); -int sqlite3ExprIsTableConstant(Expr*,int); +int sqlite3ExprIsConstantOrGroupBy(Parse*, Expr*, ExprList*); +int sqlite3ExprIsSingleTableConstraint(Expr*,const SrcList*,int,int); #ifdef SQLITE_ENABLE_CURSOR_HINTS int sqlite3ExprContainsSubquery(Expr*); #endif -int sqlite3ExprIsInteger(Expr*, int*); +int sqlite3ExprIsInteger(const Expr*, int*, Parse*); int sqlite3ExprCanBeNull(const Expr*); int sqlite3ExprNeedsNoAffinityChange(const Expr*, char); int sqlite3IsRowid(const char*); +const char *sqlite3RowidAlias(Table *pTab); void sqlite3GenerateRowDelete( Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8,int); void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*, int); int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*,Index*,int); void sqlite3ResolvePartIdxLabel(Parse*,int); +int sqlite3ExprReferencesUpdatedColumn(Expr*,int*,int); void sqlite3GenerateConstraintChecks(Parse*,Table*,int*,int,int,int,int, - u8,u8,int,int*,int*); + u8,u8,int,int*,int*,Upsert*); +#ifdef SQLITE_ENABLE_NULL_TRIM + void sqlite3SetMakeRecordP5(Vdbe*,Table*); +#else +# define sqlite3SetMakeRecordP5(A,B) +#endif void sqlite3CompleteInsertion(Parse*,Table*,int,int,int,int*,int,int,int); int sqlite3OpenTableAndIndices(Parse*, Table*, int, u8, int, u8*, int*, int*); void sqlite3BeginWriteOperation(Parse*, int, int); @@ -3562,27 +5212,34 @@ void sqlite3MayAbort(Parse*); void sqlite3HaltConstraint(Parse*, int, int, char*, i8, u8); void sqlite3UniqueConstraint(Parse*, int, Index*); void sqlite3RowidConstraint(Parse*, int, Table*); -Expr *sqlite3ExprDup(sqlite3*,Expr*,int); -ExprList *sqlite3ExprListDup(sqlite3*,ExprList*,int); -SrcList *sqlite3SrcListDup(sqlite3*,SrcList*,int); -IdList *sqlite3IdListDup(sqlite3*,IdList*); -Select *sqlite3SelectDup(sqlite3*,Select*,int); -#if SELECTTRACE_ENABLED -void sqlite3SelectSetName(Select*,const char*); -#else -# define sqlite3SelectSetName(A,B) -#endif -void sqlite3FuncDefInsert(FuncDefHash*, FuncDef*); -FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,int,u8,u8); -void sqlite3RegisterBuiltinFunctions(sqlite3*); +Expr *sqlite3ExprDup(sqlite3*,const Expr*,int); +ExprList *sqlite3ExprListDup(sqlite3*,const ExprList*,int); +SrcList *sqlite3SrcListDup(sqlite3*,const SrcList*,int); +IdList *sqlite3IdListDup(sqlite3*,const IdList*); +Select *sqlite3SelectDup(sqlite3*,const Select*,int); +FuncDef *sqlite3FunctionSearch(int,const char*); +void sqlite3InsertBuiltinFuncs(FuncDef*,int); +FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,u8,u8); +void sqlite3QuoteValue(StrAccum*,sqlite3_value*,int); +int sqlite3AppendOneUtf8Character(char*, u32); +void sqlite3RegisterBuiltinFunctions(void); void sqlite3RegisterDateTimeFunctions(void); -void sqlite3RegisterGlobalFunctions(void); +void sqlite3RegisterJsonFunctions(void); +void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3*); +#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_JSON) + Module *sqlite3JsonVtabRegister(sqlite3*,const char*); +#endif int sqlite3SafetyCheckOk(sqlite3*); int sqlite3SafetyCheckSickOrOk(sqlite3*); void sqlite3ChangeCookie(Parse*, int); +With *sqlite3WithDup(sqlite3 *db, With *p); + +#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_CARRAY) + Module *sqlite3CarrayRegister(sqlite3*); +#endif #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) -void sqlite3MaterializeView(Parse*, Table*, Expr*, int); +void sqlite3MaterializeView(Parse*, Table*, Expr*, ExprList*,Expr*,int); #endif #ifndef SQLITE_OMIT_TRIGGER @@ -3598,14 +5255,19 @@ void sqlite3MaterializeView(Parse*, Table*, Expr*, int); void sqlite3CodeRowTriggerDirect(Parse *, Trigger *, Table *, int, int, int); void sqliteViewTriggers(Parse*, Table*, Expr*, int, ExprList*); void sqlite3DeleteTriggerStep(sqlite3*, TriggerStep*); - TriggerStep *sqlite3TriggerSelectStep(sqlite3*,Select*); - TriggerStep *sqlite3TriggerInsertStep(sqlite3*,Token*, IdList*, - Select*,u8); - TriggerStep *sqlite3TriggerUpdateStep(sqlite3*,Token*,ExprList*, Expr*, u8); - TriggerStep *sqlite3TriggerDeleteStep(sqlite3*,Token*, Expr*); + TriggerStep *sqlite3TriggerSelectStep(sqlite3*,Select*, + const char*,const char*); + TriggerStep *sqlite3TriggerInsertStep(Parse*,Token*, IdList*, + Select*,u8,Upsert*, + const char*,const char*); + TriggerStep *sqlite3TriggerUpdateStep(Parse*,Token*,SrcList*,ExprList*, + Expr*, u8, const char*,const char*); + TriggerStep *sqlite3TriggerDeleteStep(Parse*,Token*, Expr*, + const char*,const char*); void sqlite3DeleteTrigger(sqlite3*, Trigger*); void sqlite3UnlinkAndDeleteTrigger(sqlite3*,int,const char*); u32 sqlite3TriggerColmask(Parse*,Trigger*,ExprList*,int,int,Table*,int); + SrcList *sqlite3TriggerStepSrc(Parse*, TriggerStep*); # define sqlite3ParseToplevel(p) ((p)->pToplevel ? (p)->pToplevel : (p)) # define sqlite3IsToplevel(p) ((p)->pToplevel==0) #else @@ -3619,9 +5281,13 @@ void sqlite3MaterializeView(Parse*, Table*, Expr*, int); # define sqlite3ParseToplevel(p) p # define sqlite3IsToplevel(p) 1 # define sqlite3TriggerColmask(A,B,C,D,E,F,G) 0 +# define sqlite3TriggerStepSrc(A,B) 0 #endif int sqlite3JoinType(Parse*, Token*, Token*, Token*); +int sqlite3ColumnIndex(Table *pTab, const char *zCol); +void sqlite3SrcItemColumnUsed(SrcItem*,int); +void sqlite3SetJoinExpr(Expr*,int,u32); void sqlite3CreateForeignKey(Parse*, ExprList*, Token*, ExprList*, int); void sqlite3DeferForeignKey(Parse*, int); #ifndef SQLITE_OMIT_AUTHORIZATION @@ -3636,26 +5302,35 @@ void sqlite3DeferForeignKey(Parse*, int); # define sqlite3AuthContextPush(a,b,c) # define sqlite3AuthContextPop(a) ((void)(a)) #endif +int sqlite3DbIsNamed(sqlite3 *db, int iDb, const char *zName); void sqlite3Attach(Parse*, Expr*, Expr*, Expr*); void sqlite3Detach(Parse*, Expr*); void sqlite3FixInit(DbFixer*, Parse*, int, const char*, const Token*); int sqlite3FixSrcList(DbFixer*, SrcList*); int sqlite3FixSelect(DbFixer*, Select*); int sqlite3FixExpr(DbFixer*, Expr*); -int sqlite3FixExprList(DbFixer*, ExprList*); int sqlite3FixTriggerStep(DbFixer*, TriggerStep*); + +int sqlite3RealSameAsInt(double,sqlite3_int64); +i64 sqlite3RealToI64(double); +int sqlite3Int64ToText(i64,char*); int sqlite3AtoF(const char *z, double*, int, u8); int sqlite3GetInt32(const char *, int*); +int sqlite3GetUInt32(const char*, u32*); int sqlite3Atoi(const char*); -int sqlite3Utf16ByteLen(const void *pData, int nChar); +#ifndef SQLITE_OMIT_UTF16 +int sqlite3Utf16ByteLen(const void *pData, int nByte, int nChar); +#endif int sqlite3Utf8CharLen(const char *pData, int nByte); u32 sqlite3Utf8Read(const u8**); +int sqlite3Utf8ReadLimited(const u8*, int, u32*); LogEst sqlite3LogEst(u64); LogEst sqlite3LogEstAdd(LogEst,LogEst); -#ifndef SQLITE_OMIT_VIRTUALTABLE LogEst sqlite3LogEstFromDouble(double); -#endif u64 sqlite3LogEstToInt(LogEst); +VList *sqlite3VListAdd(sqlite3*,VList*,const char*,int,int); +const char *sqlite3VListNumToName(VList*,int); +int sqlite3VListNameToNum(VList*,const char*,int); /* ** Routines to read and write variable-length integers. These used to @@ -3674,6 +5349,8 @@ int sqlite3VarintLen(u64 v); */ #define getVarint32(A,B) \ (u8)((*(A)<(u8)0x80)?((B)=(u32)*(A)),1:sqlite3GetVarint32((A),(u32 *)&(B))) +#define getVarint32NR(A,B) \ + B=(u32)*(A);if(B>=0x80)sqlite3GetVarint32((A),(u32*)&(B)) #define putVarint32(A,B) \ (u8)(((u32)(B)<(u32)0x80)?(*(A)=(unsigned char)(B)),1:\ sqlite3PutVarint((A),(B))) @@ -3682,15 +5359,22 @@ int sqlite3VarintLen(u64 v); const char *sqlite3IndexAffinityStr(sqlite3*, Index*); +char *sqlite3TableAffinityStr(sqlite3*,const Table*); void sqlite3TableAffinity(Vdbe*, Table*, int); -char sqlite3CompareAffinity(Expr *pExpr, char aff2); -int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity); -char sqlite3ExprAffinity(Expr *pExpr); +char sqlite3CompareAffinity(const Expr *pExpr, char aff2); +int sqlite3IndexAffinityOk(const Expr *pExpr, char idx_affinity); +char sqlite3TableColumnAffinity(const Table*,int); +char sqlite3ExprAffinity(const Expr *pExpr); +int sqlite3ExprDataType(const Expr *pExpr); int sqlite3Atoi64(const char*, i64*, int, u8); int sqlite3DecOrHexToI64(const char*, i64*); void sqlite3ErrorWithMsg(sqlite3*, int, const char*,...); void sqlite3Error(sqlite3*,int); +void sqlite3ErrorClear(sqlite3*); +void sqlite3SystemError(sqlite3*,int); +#if !defined(SQLITE_OMIT_BLOB_LITERAL) void *sqlite3HexToBlob(sqlite3*, const char *z, int n); +#endif u8 sqlite3HexToInt(int h); int sqlite3TwoPartName(Parse *, Token *, Token *, Token **); @@ -3698,17 +5382,30 @@ int sqlite3TwoPartName(Parse *, Token *, Token *, Token **); const char *sqlite3ErrName(int); #endif +#ifndef SQLITE_OMIT_DESERIALIZE +int sqlite3MemdbInit(void); +int sqlite3IsMemdb(const sqlite3_vfs*); +#else +# define sqlite3IsMemdb(X) 0 +#endif + const char *sqlite3ErrStr(int); int sqlite3ReadSchema(Parse *pParse); CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int); +int sqlite3IsBinary(const CollSeq*); CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName); -CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr); -Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr*, const Token*, int); -Expr *sqlite3ExprAddCollateString(Parse*,Expr*,const char*); +void sqlite3SetTextEncoding(sqlite3 *db, u8); +CollSeq *sqlite3ExprCollSeq(Parse *pParse, const Expr *pExpr); +CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, const Expr *pExpr); +int sqlite3ExprCollSeqMatch(Parse*,const Expr*,const Expr*); +Expr *sqlite3ExprAddCollateToken(const Parse *pParse, Expr*, const Token*, int); +Expr *sqlite3ExprAddCollateString(const Parse*,Expr*,const char*); Expr *sqlite3ExprSkipCollate(Expr*); +Expr *sqlite3ExprSkipCollateAndLikely(Expr*); int sqlite3CheckCollSeq(Parse *, CollSeq *); -int sqlite3CheckObjectName(Parse *, const char *); -void sqlite3VdbeSetChanges(sqlite3 *, int); +int sqlite3WritableSchema(sqlite3*); +int sqlite3CheckObjectName(Parse*, const char*,const char*,const char*); +void sqlite3VdbeSetChanges(sqlite3 *, i64); int sqlite3AddInt64(i64*,i64); int sqlite3SubInt64(i64*,i64); int sqlite3MulInt64(i64*,i64); @@ -3721,48 +5418,78 @@ void sqlite3FileSuffix3(const char*, char*); u8 sqlite3GetBoolean(const char *z,u8); const void *sqlite3ValueText(sqlite3_value*, u8); +int sqlite3ValueIsOfClass(const sqlite3_value*, void(*)(void*)); int sqlite3ValueBytes(sqlite3_value*, u8); -void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8, +void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8, void(*)(void*)); void sqlite3ValueSetNull(sqlite3_value*); void sqlite3ValueFree(sqlite3_value*); +#ifndef SQLITE_UNTESTABLE +void sqlite3ResultIntReal(sqlite3_context*); +#endif sqlite3_value *sqlite3ValueNew(sqlite3 *); +#ifndef SQLITE_OMIT_UTF16 char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8); -int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **); +#endif +int sqlite3ValueFromExpr(sqlite3 *, const Expr *, u8, u8, sqlite3_value **); void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8); #ifndef SQLITE_AMALGAMATION extern const unsigned char sqlite3OpcodeProperty[]; extern const char sqlite3StrBINARY[]; +extern const unsigned char sqlite3StdTypeLen[]; +extern const char sqlite3StdTypeAffinity[]; +extern const char *sqlite3StdType[]; extern const unsigned char sqlite3UpperToLower[]; +extern const unsigned char *sqlite3aLTb; +extern const unsigned char *sqlite3aEQb; +extern const unsigned char *sqlite3aGTb; extern const unsigned char sqlite3CtypeMap[]; -extern const Token sqlite3IntTokens[]; extern SQLITE_WSD struct Sqlite3Config sqlite3Config; -extern SQLITE_WSD FuncDefHash sqlite3GlobalFunctions; +extern FuncDefHash sqlite3BuiltinFunctions; #ifndef SQLITE_OMIT_WSD extern int sqlite3PendingByte; #endif +#endif /* SQLITE_AMALGAMATION */ +#ifdef VDBE_PROFILE +extern sqlite3_uint64 sqlite3NProfileCnt; #endif -void sqlite3RootPageMoved(sqlite3*, int, int, int); +void sqlite3RootPageMoved(sqlite3*, int, Pgno, Pgno); void sqlite3Reindex(Parse*, Token*, Token*); void sqlite3AlterFunctions(void); void sqlite3AlterRenameTable(Parse*, SrcList*, Token*); -int sqlite3GetToken(const unsigned char *, int *); +void sqlite3AlterRenameColumn(Parse*, SrcList*, Token*, Token*); +i64 sqlite3GetToken(const unsigned char *, int *); void sqlite3NestedParse(Parse*, const char*, ...); -void sqlite3ExpirePreparedStatements(sqlite3*); -int sqlite3CodeSubselect(Parse *, Expr *, int, int); +void sqlite3ExpirePreparedStatements(sqlite3*, int); +void sqlite3CodeRhsOfIN(Parse*, Expr*, int); +int sqlite3CodeSubselect(Parse*, Expr*); void sqlite3SelectPrep(Parse*, Select*, NameContext*); +int sqlite3ExpandSubquery(Parse*, SrcItem*); void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p); -int sqlite3MatchSpanName(const char*, const char*, const char*, const char*); +int sqlite3MatchEName( + const struct ExprList_item*, + const char*, + const char*, + const char*, + int* +); +Bitmask sqlite3ExprColUsed(Expr*); +u8 sqlite3StrIHash(const char*); int sqlite3ResolveExprNames(NameContext*, Expr*); int sqlite3ResolveExprListNames(NameContext*, ExprList*); void sqlite3ResolveSelectNames(Parse*, Select*, NameContext*); -void sqlite3ResolveSelfReference(Parse*,Table*,int,Expr*,ExprList*); +int sqlite3ResolveSelfReference(Parse*,Table*,int,Expr*,ExprList*); int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*); void sqlite3ColumnDefault(Vdbe *, Table *, int, int); void sqlite3AlterFinishAddColumn(Parse *, Token *); void sqlite3AlterBeginAddColumn(Parse *, SrcList *); +void sqlite3AlterDropColumn(Parse*, SrcList*, const Token*); +const void *sqlite3RenameTokenMap(Parse*, const void*, const Token*); +void sqlite3RenameTokenRemap(Parse*, const void *pTo, const void *pFrom); +void sqlite3RenameExprUnmap(Parse*, Expr*); +void sqlite3RenameExprlistUnmap(Parse*, ExprList*); CollSeq *sqlite3GetCollSeq(Parse*, u8, CollSeq *, const char*); -char sqlite3AffinityType(const char*, u8*); +char sqlite3AffinityType(const char*, Column*); void sqlite3Analyze(Parse*, Token*, Token*); int sqlite3InvokeBusyHandler(BusyHandler*); int sqlite3FindDb(sqlite3*, Token*); @@ -3779,45 +5506,69 @@ KeyInfo *sqlite3KeyInfoAlloc(sqlite3*,int,int); void sqlite3KeyInfoUnref(KeyInfo*); KeyInfo *sqlite3KeyInfoRef(KeyInfo*); KeyInfo *sqlite3KeyInfoOfIndex(Parse*, Index*); +KeyInfo *sqlite3KeyInfoFromExprList(Parse*, ExprList*, int, int); +const char *sqlite3SelectOpName(int); +int sqlite3HasExplicitNulls(Parse*, ExprList*); + #ifdef SQLITE_DEBUG int sqlite3KeyInfoIsWriteable(KeyInfo*); #endif -int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *, +int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *, + void (*)(sqlite3_context*,int,sqlite3_value **), + void (*)(sqlite3_context*,int,sqlite3_value **), + void (*)(sqlite3_context*), + void (*)(sqlite3_context*), void (*)(sqlite3_context*,int,sqlite3_value **), - void (*)(sqlite3_context*,int,sqlite3_value **), void (*)(sqlite3_context*), FuncDestructor *pDestructor ); -void sqlite3OomFault(sqlite3*); +void sqlite3NoopDestructor(void*); +void *sqlite3OomFault(sqlite3*); void sqlite3OomClear(sqlite3*); int sqlite3ApiExit(sqlite3 *db, int); int sqlite3OpenTempDatabase(Parse *); +char *sqlite3RCStrRef(char*); +void sqlite3RCStrUnref(void*); +char *sqlite3RCStrNew(u64); +char *sqlite3RCStrResize(char*,u64); + void sqlite3StrAccumInit(StrAccum*, sqlite3*, char*, int, int); -void sqlite3StrAccumAppend(StrAccum*,const char*,int); -void sqlite3StrAccumAppendAll(StrAccum*,const char*); -void sqlite3AppendChar(StrAccum*,int,char); +int sqlite3StrAccumEnlarge(StrAccum*, i64); char *sqlite3StrAccumFinish(StrAccum*); -void sqlite3StrAccumReset(StrAccum*); +void sqlite3StrAccumSetError(StrAccum*, u8); +void sqlite3ResultStrAccum(sqlite3_context*,StrAccum*); void sqlite3SelectDestInit(SelectDest*,int,int); Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int); +void sqlite3RecordErrorByteOffset(sqlite3*,const char*); +void sqlite3RecordErrorOffsetOfExpr(sqlite3*,const Expr*); void sqlite3BackupRestart(sqlite3_backup *); void sqlite3BackupUpdate(sqlite3_backup *, Pgno, const u8 *); -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 -void sqlite3AnalyzeFunctions(void); -int sqlite3Stat4ProbeSetValue(Parse*,Index*,UnpackedRecord**,Expr*,u8,int,int*); +#ifndef SQLITE_OMIT_SUBQUERY +int sqlite3ExprCheckIN(Parse*, Expr*); +#else +# define sqlite3ExprCheckIN(x,y) SQLITE_OK +#endif + +#ifdef SQLITE_ENABLE_STAT4 +int sqlite3Stat4ProbeSetValue( + Parse*,Index*,UnpackedRecord**,Expr*,int,int,int*); int sqlite3Stat4ValueFromExpr(Parse*, Expr*, u8, sqlite3_value**); void sqlite3Stat4ProbeFree(UnpackedRecord*); int sqlite3Stat4Column(sqlite3*, const void*, int, int, sqlite3_value**); +char sqlite3IndexColumnAffinity(sqlite3*, Index*, int); #endif /* ** The interface to the LEMON-generated parser */ -void *sqlite3ParserAlloc(void*(*)(u64)); -void sqlite3ParserFree(void*, void(*)(void*)); -void sqlite3Parser(void*, int, Token, Parse*); +#ifndef SQLITE_AMALGAMATION + void *sqlite3ParserAlloc(void*(*)(u64), Parse*); + void sqlite3ParserFree(void*, void(*)(void*)); +#endif +void sqlite3Parser(void*, int, Token); +int sqlite3ParserFallback(int); #ifdef YYTRACKMAXSTACKDEPTH int sqlite3ParserStackPeak(void*); #endif @@ -3830,7 +5581,7 @@ void sqlite3AutoLoadExtensions(sqlite3*); #endif #ifndef SQLITE_OMIT_SHARED_CACHE - void sqlite3TableLock(Parse *, int, int, u8, const char *); + void sqlite3TableLock(Parse *, int, Pgno, u8, const char *); #else #define sqlite3TableLock(v,w,x,y,z) #endif @@ -3840,13 +5591,14 @@ void sqlite3AutoLoadExtensions(sqlite3*); #endif #ifdef SQLITE_OMIT_VIRTUALTABLE -# define sqlite3VtabClear(Y) +# define sqlite3VtabClear(D,T) # define sqlite3VtabSync(X,Y) SQLITE_OK # define sqlite3VtabRollback(X) # define sqlite3VtabCommit(X) # define sqlite3VtabInSync(db) 0 -# define sqlite3VtabLock(X) +# define sqlite3VtabLock(X) # define sqlite3VtabUnlock(X) +# define sqlite3VtabModuleUnref(D,X) # define sqlite3VtabUnlockList(X) # define sqlite3VtabSavepoint(X, Y, Z) SQLITE_OK # define sqlite3GetVTable(X,Y) ((VTable*)0) @@ -3858,12 +5610,30 @@ void sqlite3AutoLoadExtensions(sqlite3*); int sqlite3VtabCommit(sqlite3 *db); void sqlite3VtabLock(VTable *); void sqlite3VtabUnlock(VTable *); + void sqlite3VtabModuleUnref(sqlite3*,Module*); void sqlite3VtabUnlockList(sqlite3*); int sqlite3VtabSavepoint(sqlite3 *, int, int); void sqlite3VtabImportErrmsg(Vdbe*, sqlite3_vtab*); VTable *sqlite3GetVTable(sqlite3*, Table*); + Module *sqlite3VtabCreateModule( + sqlite3*, + const char*, + const sqlite3_module*, + void*, + void(*)(void*) + ); # define sqlite3VtabInSync(db) ((db)->nVTrans>0 && (db)->aVTrans==0) #endif +int sqlite3ReadOnlyShadowTables(sqlite3 *db); +#ifndef SQLITE_OMIT_VIRTUALTABLE + int sqlite3ShadowTableName(sqlite3 *db, const char *zName); + int sqlite3IsShadowTableOf(sqlite3*,Table*,const char*); + void sqlite3MarkAllShadowTablesOf(sqlite3*, Table*); +#else +# define sqlite3ShadowTableName(A,B) 0 +# define sqlite3IsShadowTableOf(A,B,C) 0 +# define sqlite3MarkAllShadowTablesOf(A,B) +#endif int sqlite3VtabEponymousTableInit(Parse*,Module*); void sqlite3VtabEponymousTableClear(sqlite3*,Module*); void sqlite3VtabMakeWritable(Parse*,Table*); @@ -3875,15 +5645,22 @@ int sqlite3VtabCallCreate(sqlite3*, int, const char *, char **); int sqlite3VtabCallConnect(Parse*, Table*); int sqlite3VtabCallDestroy(sqlite3*, int, const char *); int sqlite3VtabBegin(sqlite3 *, VTable *); + FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*); -void sqlite3InvalidFunction(sqlite3_context*,int,sqlite3_value**); +void sqlite3VtabUsesAllSchemas(Parse*); sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context*); int sqlite3VdbeParameterIndex(Vdbe*, const char*, int); int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *); -void sqlite3ParserReset(Parse*); +void sqlite3ParseObjectInit(Parse*,sqlite3*); +void sqlite3ParseObjectReset(Parse*); +void *sqlite3ParserAddCleanup(Parse*,void(*)(sqlite3*,void*),void*); +#ifdef SQLITE_ENABLE_NORMALIZE +char *sqlite3Normalize(Vdbe*, const char*); +#endif int sqlite3Reprepare(Vdbe*); void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*); -CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *); +CollSeq *sqlite3ExprCompareCollSeq(Parse*,const Expr*); +CollSeq *sqlite3BinaryCompareCollSeq(Parse *, const Expr*, const Expr*); int sqlite3TempInMemory(const sqlite3*); const char *sqlite3JournalModename(int); #ifndef SQLITE_OMIT_WAL @@ -3891,19 +5668,41 @@ const char *sqlite3JournalModename(int); int sqlite3WalDefaultHook(void*,sqlite3*,const char*,int); #endif #ifndef SQLITE_OMIT_CTE - With *sqlite3WithAdd(Parse*,With*,Token*,ExprList*,Select*); + Cte *sqlite3CteNew(Parse*,Token*,ExprList*,Select*,u8); + void sqlite3CteDelete(sqlite3*,Cte*); + With *sqlite3WithAdd(Parse*,With*,Cte*); void sqlite3WithDelete(sqlite3*,With*); - void sqlite3WithPush(Parse*, With*, u8); + void sqlite3WithDeleteGeneric(sqlite3*,void*); + With *sqlite3WithPush(Parse*, With*, u8); #else -#define sqlite3WithPush(x,y,z) -#define sqlite3WithDelete(x,y) +# define sqlite3CteNew(P,T,E,S) ((void*)0) +# define sqlite3CteDelete(D,C) +# define sqlite3CteWithAdd(P,W,C) ((void*)0) +# define sqlite3WithDelete(x,y) +# define sqlite3WithPush(x,y,z) ((void*)0) +#endif +#ifndef SQLITE_OMIT_UPSERT + Upsert *sqlite3UpsertNew(sqlite3*,ExprList*,Expr*,ExprList*,Expr*,Upsert*); + void sqlite3UpsertDelete(sqlite3*,Upsert*); + Upsert *sqlite3UpsertDup(sqlite3*,Upsert*); + int sqlite3UpsertAnalyzeTarget(Parse*,SrcList*,Upsert*,Upsert*); + void sqlite3UpsertDoUpdate(Parse*,Upsert*,Table*,Index*,int); + Upsert *sqlite3UpsertOfIndex(Upsert*,Index*); + int sqlite3UpsertNextIsIPK(Upsert*); +#else +#define sqlite3UpsertNew(u,v,w,x,y,z) ((Upsert*)0) +#define sqlite3UpsertDelete(x,y) +#define sqlite3UpsertDup(x,y) ((Upsert*)0) +#define sqlite3UpsertOfIndex(x,y) ((Upsert*)0) +#define sqlite3UpsertNextIsIPK(x) 0 #endif + /* Declarations for functions in fkey.c. All of these are replaced by ** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign ** key functionality is available. If OMIT_TRIGGER is defined but ** OMIT_FOREIGN_KEY is not, only some of the functions are no-oped. In -** this case foreign keys are parsed, but no other functionality is +** this case foreign keys are parsed, but no other functionality is ** provided (enforcement of FK constraints requires the triggers sub-system). */ #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER) @@ -3913,12 +5712,15 @@ const char *sqlite3JournalModename(int); int sqlite3FkRequired(Parse*, Table*, int*, int); u32 sqlite3FkOldmask(Parse*, Table*); FKey *sqlite3FkReferences(Table *); + void sqlite3FkClearTriggerCache(sqlite3*,int); #else #define sqlite3FkActions(a,b,c,d,e,f) #define sqlite3FkCheck(a,b,c,d,e,f) #define sqlite3FkDropTable(a,b,c) #define sqlite3FkOldmask(a,b) 0 #define sqlite3FkRequired(a,b,c,d) 0 + #define sqlite3FkReferences(a) 0 + #define sqlite3FkClearTriggerCache(a,b) #endif #ifndef SQLITE_OMIT_FOREIGN_KEY void sqlite3FkDelete(sqlite3 *, Table*); @@ -3937,10 +5739,10 @@ const char *sqlite3JournalModename(int); /* ** The interface to the code in fault.c used for identifying "benign" -** malloc failures. This is only present if SQLITE_OMIT_BUILTIN_TEST +** malloc failures. This is only present if SQLITE_UNTESTABLE ** is not defined. */ -#ifndef SQLITE_OMIT_BUILTIN_TEST +#ifndef SQLITE_UNTESTABLE void sqlite3BeginBenignMalloc(void); void sqlite3EndBenignMalloc(void); #else @@ -3962,30 +5764,27 @@ const char *sqlite3JournalModename(int); #define IN_INDEX_NOOP_OK 0x0001 /* OK to return IN_INDEX_NOOP */ #define IN_INDEX_MEMBERSHIP 0x0002 /* IN operator used for membership test */ #define IN_INDEX_LOOP 0x0004 /* IN operator used as a loop */ -int sqlite3FindInIndex(Parse *, Expr *, u32, int*); +int sqlite3FindInIndex(Parse *, Expr *, u32, int*, int*, int*); -#ifdef SQLITE_ENABLE_ATOMIC_WRITE - int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int); - int sqlite3JournalSize(sqlite3_vfs *); +int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int); +int sqlite3JournalSize(sqlite3_vfs *); +#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \ + || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE) int sqlite3JournalCreate(sqlite3_file *); - int sqlite3JournalExists(sqlite3_file *p); -#else - #define sqlite3JournalSize(pVfs) ((pVfs)->szOsFile) - #define sqlite3JournalExists(p) 1 #endif +int sqlite3JournalIsInMemory(sqlite3_file *p); void sqlite3MemJournalOpen(sqlite3_file *); -int sqlite3MemJournalSize(void); -int sqlite3IsMemJournal(sqlite3_file *); void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p); #if SQLITE_MAX_EXPR_DEPTH>0 - int sqlite3SelectExprHeight(Select *); + int sqlite3SelectExprHeight(const Select *); int sqlite3ExprCheckHeight(Parse*, int); #else #define sqlite3SelectExprHeight(x) 0 #define sqlite3ExprCheckHeight(x,y) #endif +void sqlite3ExprSetErrorOffset(Expr*,int); u32 sqlite3Get4byte(const u8*); void sqlite3Put4byte(u8*, u32); @@ -4003,11 +5802,14 @@ void sqlite3Put4byte(u8*, u32); #ifdef SQLITE_DEBUG void sqlite3ParserTrace(FILE*, char *); #endif +#if defined(YYCOVERAGE) + int sqlite3ParserCoverage(FILE*); +#endif /* ** If the SQLITE_ENABLE IOTRACE exists then the global variable ** sqlite3IoTrace is a pointer to a printf-like routine used to -** print I/O tracing messages. +** print I/O tracing messages. */ #ifdef SQLITE_ENABLE_IOTRACE # define IOTRACE(A) if( sqlite3IoTrace ){ sqlite3IoTrace A; } @@ -4041,15 +5843,15 @@ SQLITE_API SQLITE_EXTERN void (SQLITE_CDECL *sqlite3IoTrace)(const char*,...); ** that allocations that might have been satisfied by lookaside are not ** passed back to non-lookaside free() routines. Asserts such as the ** example above are placed on the non-lookaside free() routines to verify -** this constraint. +** this constraint. ** ** All of this is no-op for a production build. It only comes into ** play when the SQLITE_MEMDEBUG compile-time option is used. */ #ifdef SQLITE_MEMDEBUG void sqlite3MemdebugSetType(void*,u8); - int sqlite3MemdebugHasType(void*,u8); - int sqlite3MemdebugNoType(void*,u8); + int sqlite3MemdebugHasType(const void*,u8); + int sqlite3MemdebugNoType(const void*,u8); #else # define sqlite3MemdebugSetType(X,Y) /* no-op */ # define sqlite3MemdebugHasType(X,Y) 1 @@ -4057,8 +5859,7 @@ SQLITE_API SQLITE_EXTERN void (SQLITE_CDECL *sqlite3IoTrace)(const char*,...); #endif #define MEMTYPE_HEAP 0x01 /* General heap allocations */ #define MEMTYPE_LOOKASIDE 0x02 /* Heap that might have been lookaside */ -#define MEMTYPE_SCRATCH 0x04 /* Scratch allocations */ -#define MEMTYPE_PCACHE 0x08 /* Page cache allocations */ +#define MEMTYPE_PCACHE 0x04 /* Page cache allocations */ /* ** Threading interface @@ -4068,8 +5869,37 @@ int sqlite3ThreadCreate(SQLiteThread**,void*(*)(void*),void*); int sqlite3ThreadJoin(SQLiteThread*, void**); #endif +#if defined(SQLITE_ENABLE_DBPAGE_VTAB) || defined(SQLITE_TEST) +int sqlite3DbpageRegister(sqlite3*); +#endif #if defined(SQLITE_ENABLE_DBSTAT_VTAB) || defined(SQLITE_TEST) int sqlite3DbstatRegister(sqlite3*); #endif -#endif /* _SQLITEINT_H_ */ +int sqlite3ExprVectorSize(const Expr *pExpr); +int sqlite3ExprIsVector(const Expr *pExpr); +Expr *sqlite3VectorFieldSubexpr(Expr*, int); +Expr *sqlite3ExprForVectorField(Parse*,Expr*,int,int); +void sqlite3VectorErrorMsg(Parse*, Expr*); + +#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS +const char **sqlite3CompileOptions(int *pnOpt); +#endif + +#if SQLITE_OS_UNIX && defined(SQLITE_OS_KV_OPTIONAL) +int sqlite3KvvfsInit(void); +#endif + +#if defined(VDBE_PROFILE) \ + || defined(SQLITE_PERFORMANCE_TRACE) \ + || defined(SQLITE_ENABLE_STMT_SCANSTATUS) +sqlite3_uint64 sqlite3Hwtime(void); +#endif + +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS +# define IS_STMT_SCANSTATUS(db) (db->flags & SQLITE_StmtScanStatus) +#else +# define IS_STMT_SCANSTATUS(db) 0 +#endif + +#endif /* SQLITEINT_H */ diff --git a/src/sqliteLimit.h b/src/sqliteLimit.h index 75cad1274b..6b6bb7167a 100644 --- a/src/sqliteLimit.h +++ b/src/sqliteLimit.h @@ -9,7 +9,7 @@ ** May you share freely, never taking more than you give. ** ************************************************************************* -** +** ** This file defines various limits of what SQLite can process. */ @@ -23,6 +23,7 @@ #ifndef SQLITE_MAX_LENGTH # define SQLITE_MAX_LENGTH 1000000000 #endif +#define SQLITE_MIN_LENGTH 30 /* Minimum value for the length limit */ /* ** This is the maximum number of @@ -35,14 +36,22 @@ ** * Terms in the GROUP BY or ORDER BY clauses of a SELECT statement. ** * Terms in the VALUES clause of an INSERT statement ** -** The hard upper limit here is 32676. Most database people will +** The hard upper limit here is 32767. Most database people will ** tell you that in a well-normalized database, you usually should ** not have more than a dozen or so columns in any table. And if ** that is the case, there is no point in having more than a few ** dozen values in any of the other situations described above. +** +** An index can only have SQLITE_MAX_COLUMN columns from the user +** point of view, but the underlying b-tree that implements the index +** might have up to twice as many columns in a WITHOUT ROWID table, +** since must also store the primary key at the end. Hence the +** column count for Index is u16 instead of i16. */ -#ifndef SQLITE_MAX_COLUMN +#if !defined(SQLITE_MAX_COLUMN) # define SQLITE_MAX_COLUMN 2000 +#elif SQLITE_MAX_COLUMN>32767 +# error SQLITE_MAX_COLUMN may not exceed 32767 #endif /* @@ -57,14 +66,10 @@ #endif /* -** The maximum depth of an expression tree. This is limited to -** some extent by SQLITE_MAX_SQL_LENGTH. But sometime you might -** want to place more severe limits on the complexity of an -** expression. -** -** A value of 0 used to mean that the limit was not enforced. -** But that is no longer true. The limit is now strictly enforced -** at all times. +** The maximum depth of an expression tree. This is limited to +** some extent by SQLITE_MAX_SQL_LENGTH. But sometime you might +** want to place more severe limits on the complexity of an +** expression. A value of 0 means that there is no limit. */ #ifndef SQLITE_MAX_EXPR_DEPTH # define SQLITE_MAX_EXPR_DEPTH 1000 @@ -76,7 +81,7 @@ ** level of recursion for each term. A stack overflow can result ** if the number of terms is too large. In practice, most SQL ** never has more than 3 or 4 terms. Use a value of 0 to disable -** any limit on the number of terms in a compount SELECT. +** any limit on the number of terms in a compound SELECT. */ #ifndef SQLITE_MAX_COMPOUND_SELECT # define SQLITE_MAX_COMPOUND_SELECT 500 @@ -87,27 +92,31 @@ ** Not currently enforced. */ #ifndef SQLITE_MAX_VDBE_OP -# define SQLITE_MAX_VDBE_OP 25000 +# define SQLITE_MAX_VDBE_OP 250000000 #endif /* ** The maximum number of arguments to an SQL function. +** +** This value has a hard upper limit of 32767 due to storage +** constraints (it needs to fit inside a i16). We keep it +** lower than that to prevent abuse. */ #ifndef SQLITE_MAX_FUNCTION_ARG -# define SQLITE_MAX_FUNCTION_ARG 127 +# define SQLITE_MAX_FUNCTION_ARG 1000 #endif /* ** The suggested maximum number of in-memory pages to use for ** the main database table and for temporary tables. ** -** IMPLEMENTATION-OF: R-31093-59126 The default suggested cache size -** is 2000 pages. +** IMPLEMENTATION-OF: R-30185-15359 The default suggested cache size is -2000, +** which means the cache size is limited to 2048000 bytes of memory. ** IMPLEMENTATION-OF: R-48205-43578 The default suggested cache size can be ** altered using the SQLITE_DEFAULT_CACHE_SIZE compile-time options. */ #ifndef SQLITE_DEFAULT_CACHE_SIZE -# define SQLITE_DEFAULT_CACHE_SIZE 2000 +# define SQLITE_DEFAULT_CACHE_SIZE -2000 #endif /* @@ -120,8 +129,9 @@ /* ** The maximum number of attached databases. This must be between 0 -** and 62. The upper bound on 62 is because a 64-bit integer bitmap -** is used internally to track attached databases. +** and 125. The upper bound of 125 is because the attached databases are +** counted using a signed 8-bit integer which has a maximum value of 127 +** and we have to allow 2 extra counts for the "main" and "temp" databases. */ #ifndef SQLITE_MAX_ATTACHED # define SQLITE_MAX_ATTACHED 10 @@ -130,9 +140,12 @@ /* ** The maximum value of a ?nnn wildcard that the parser will accept. +** If the value exceeds 32767 then extra space is required for the Expr +** structure. But otherwise, we believe that the number can be as large +** as a signed 32-bit integer can hold. */ #ifndef SQLITE_MAX_VARIABLE_NUMBER -# define SQLITE_MAX_VARIABLE_NUMBER 999 +# define SQLITE_MAX_VARIABLE_NUMBER 32766 #endif /* Maximum page size. The upper bound on this value is 65536. This a limit @@ -140,10 +153,10 @@ ** ** Earlier versions of SQLite allowed the user to change this value at ** compile time. This is no longer permitted, on the grounds that it creates -** a library that is technically incompatible with an SQLite library -** compiled with a different limit. If a process operating on a database -** with a page-size of 65536 bytes crashes, then an instance of SQLite -** compiled with the default page-size limit will not be able to rollback +** a library that is technically incompatible with an SQLite library +** compiled with a different limit. If a process operating on a database +** with a page-size of 65536 bytes crashes, then an instance of SQLite +** compiled with the default page-size limit will not be able to rollback ** the aborted transaction. This could lead to database corruption. */ #ifdef SQLITE_MAX_PAGE_SIZE @@ -156,7 +169,7 @@ ** The default size of a database page. */ #ifndef SQLITE_DEFAULT_PAGE_SIZE -# define SQLITE_DEFAULT_PAGE_SIZE 1024 +# define SQLITE_DEFAULT_PAGE_SIZE 4096 #endif #if SQLITE_DEFAULT_PAGE_SIZE>SQLITE_MAX_PAGE_SIZE # undef SQLITE_DEFAULT_PAGE_SIZE @@ -183,11 +196,11 @@ ** Maximum number of pages in one database file. ** ** This is really just the default value for the max_page_count pragma. -** This value can be lowered (or raised) at run-time using that the +** This value can be lowered (or raised) at run-time using the ** max_page_count macro. */ #ifndef SQLITE_MAX_PAGE_COUNT -# define SQLITE_MAX_PAGE_COUNT 1073741823 +# define SQLITE_MAX_PAGE_COUNT 0xfffffffe /* 4294967294 */ #endif /* @@ -202,7 +215,7 @@ ** Maximum depth of recursion for triggers. ** ** A value of 1 means that a trigger program will not be able to itself -** fire any triggers. A value of 0 means that no trigger programs at all +** fire any triggers. A value of 0 means that no trigger programs at all ** may be executed. */ #ifndef SQLITE_MAX_TRIGGER_DEPTH diff --git a/src/status.c b/src/status.c index 69f92ff7c6..5db67b87b5 100644 --- a/src/status.c +++ b/src/status.c @@ -122,7 +122,6 @@ void sqlite3StatusHighwater(int op, int X){ : sqlite3MallocMutex()) ); assert( op==SQLITE_STATUS_MALLOC_SIZE || op==SQLITE_STATUS_PAGECACHE_SIZE - || op==SQLITE_STATUS_SCRATCH_SIZE || op==SQLITE_STATUS_PARSER_STACK ); if( newValue>wsdStat.mxValue[op] ){ wsdStat.mxValue[op] = newValue; @@ -158,7 +157,7 @@ int sqlite3_status64( return SQLITE_OK; } int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){ - sqlite3_int64 iCur, iHwtr; + sqlite3_int64 iCur = 0, iHwtr = 0; int rc; #ifdef SQLITE_ENABLE_API_ARMOR if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPT; @@ -171,29 +170,72 @@ int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){ return rc; } +/* +** Return the number of LookasideSlot elements on the linked list +*/ +static u32 countLookasideSlots(LookasideSlot *p){ + u32 cnt = 0; + while( p ){ + p = p->pNext; + cnt++; + } + return cnt; +} + +/* +** Count the number of slots of lookaside memory that are outstanding +*/ +int sqlite3LookasideUsed(sqlite3 *db, int *pHighwater){ + u32 nInit = countLookasideSlots(db->lookaside.pInit); + u32 nFree = countLookasideSlots(db->lookaside.pFree); +#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE + nInit += countLookasideSlots(db->lookaside.pSmallInit); + nFree += countLookasideSlots(db->lookaside.pSmallFree); +#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */ + assert( db->lookaside.nSlot >= nInit+nFree ); + if( pHighwater ) *pHighwater = (int)(db->lookaside.nSlot - nInit); + return (int)(db->lookaside.nSlot - (nInit+nFree)); +} + /* ** Query status information for a single database connection */ -int sqlite3_db_status( - sqlite3 *db, /* The database connection whose status is desired */ - int op, /* Status verb */ - int *pCurrent, /* Write current value here */ - int *pHighwater, /* Write high-water mark here */ - int resetFlag /* Reset high-water mark if true */ +int sqlite3_db_status64( + sqlite3 *db, /* The database connection whose status is desired */ + int op, /* Status verb */ + sqlite3_int64 *pCurrent, /* Write current value here */ + sqlite3_int64 *pHighwtr, /* Write high-water mark here */ + int resetFlag /* Reset high-water mark if true */ ){ int rc = SQLITE_OK; /* Return code */ #ifdef SQLITE_ENABLE_API_ARMOR - if( !sqlite3SafetyCheckOk(db) || pCurrent==0|| pHighwater==0 ){ + if( !sqlite3SafetyCheckOk(db) || pCurrent==0|| pHighwtr==0 ){ return SQLITE_MISUSE_BKPT; } #endif sqlite3_mutex_enter(db->mutex); switch( op ){ case SQLITE_DBSTATUS_LOOKASIDE_USED: { - *pCurrent = db->lookaside.nOut; - *pHighwater = db->lookaside.mxOut; + int H = 0; + *pCurrent = sqlite3LookasideUsed(db, &H); + *pHighwtr = H; if( resetFlag ){ - db->lookaside.mxOut = db->lookaside.nOut; + LookasideSlot *p = db->lookaside.pFree; + if( p ){ + while( p->pNext ) p = p->pNext; + p->pNext = db->lookaside.pInit; + db->lookaside.pInit = db->lookaside.pFree; + db->lookaside.pFree = 0; + } +#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE + p = db->lookaside.pSmallFree; + if( p ){ + while( p->pNext ) p = p->pNext; + p->pNext = db->lookaside.pSmallInit; + db->lookaside.pSmallInit = db->lookaside.pSmallFree; + db->lookaside.pSmallFree = 0; + } +#endif } break; } @@ -207,7 +249,7 @@ int sqlite3_db_status( assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)>=0 ); assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)<3 ); *pCurrent = 0; - *pHighwater = db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT]; + *pHighwtr = db->lookaside.anStat[op-SQLITE_DBSTATUS_LOOKASIDE_HIT]; if( resetFlag ){ db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT] = 0; } @@ -219,34 +261,41 @@ int sqlite3_db_status( ** by all pagers associated with the given database connection. The ** highwater mark is meaningless and is returned as zero. */ + case SQLITE_DBSTATUS_CACHE_USED_SHARED: case SQLITE_DBSTATUS_CACHE_USED: { - int totalUsed = 0; + sqlite3_int64 totalUsed = 0; int i; sqlite3BtreeEnterAll(db); for(i=0; i<db->nDb; i++){ Btree *pBt = db->aDb[i].pBt; if( pBt ){ Pager *pPager = sqlite3BtreePager(pBt); - totalUsed += sqlite3PagerMemUsed(pPager); + int nByte = sqlite3PagerMemUsed(pPager); + if( op==SQLITE_DBSTATUS_CACHE_USED_SHARED ){ + nByte = nByte / sqlite3BtreeConnectionCount(pBt); + } + totalUsed += nByte; } } sqlite3BtreeLeaveAll(db); *pCurrent = totalUsed; - *pHighwater = 0; + *pHighwtr = 0; break; } /* ** *pCurrent gets an accurate estimate of the amount of memory used ** to store the schema for all databases (main, temp, and any ATTACHed - ** databases. *pHighwater is set to zero. + ** databases. *pHighwtr is set to zero. */ case SQLITE_DBSTATUS_SCHEMA_USED: { - int i; /* Used to iterate through schemas */ - int nByte = 0; /* Used to accumulate return value */ + int i; /* Used to iterate through schemas */ + int nByte = 0; /* Used to accumulate return value */ sqlite3BtreeEnterAll(db); db->pnBytesFreed = &nByte; + assert( db->lookaside.pEnd==db->lookaside.pTrueEnd ); + db->lookaside.pEnd = db->lookaside.pStart; for(i=0; i<db->nDb; i++){ Schema *pSchema = db->aDb[i].pSchema; if( ALWAYS(pSchema!=0) ){ @@ -272,9 +321,10 @@ int sqlite3_db_status( } } db->pnBytesFreed = 0; + db->lookaside.pEnd = db->lookaside.pTrueEnd; sqlite3BtreeLeaveAll(db); - *pHighwater = 0; + *pHighwtr = 0; *pCurrent = nByte; break; } @@ -282,20 +332,22 @@ int sqlite3_db_status( /* ** *pCurrent gets an accurate estimate of the amount of memory used ** to store all prepared statements. - ** *pHighwater is set to zero. + ** *pHighwtr is set to zero. */ case SQLITE_DBSTATUS_STMT_USED: { struct Vdbe *pVdbe; /* Used to iterate through VMs */ int nByte = 0; /* Used to accumulate return value */ db->pnBytesFreed = &nByte; - for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pNext){ - sqlite3VdbeClearObject(db, pVdbe); - sqlite3DbFree(db, pVdbe); + assert( db->lookaside.pEnd==db->lookaside.pTrueEnd ); + db->lookaside.pEnd = db->lookaside.pStart; + for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pVNext){ + sqlite3VdbeDelete(pVdbe); } + db->lookaside.pEnd = db->lookaside.pTrueEnd; db->pnBytesFreed = 0; - *pHighwater = 0; /* IMP: R-64479-57858 */ + *pHighwtr = 0; /* IMP: R-64479-57858 */ *pCurrent = nByte; break; @@ -303,14 +355,17 @@ int sqlite3_db_status( /* ** Set *pCurrent to the total cache hits or misses encountered by all - ** pagers the database handle is connected to. *pHighwater is always set + ** pagers the database handle is connected to. *pHighwtr is always set ** to zero. */ + case SQLITE_DBSTATUS_CACHE_SPILL: + op = SQLITE_DBSTATUS_CACHE_WRITE+1; + /* no break */ deliberate_fall_through case SQLITE_DBSTATUS_CACHE_HIT: case SQLITE_DBSTATUS_CACHE_MISS: case SQLITE_DBSTATUS_CACHE_WRITE:{ int i; - int nRet = 0; + u64 nRet = 0; assert( SQLITE_DBSTATUS_CACHE_MISS==SQLITE_DBSTATUS_CACHE_HIT+1 ); assert( SQLITE_DBSTATUS_CACHE_WRITE==SQLITE_DBSTATUS_CACHE_HIT+2 ); @@ -320,19 +375,39 @@ int sqlite3_db_status( sqlite3PagerCacheStat(pPager, op, resetFlag, &nRet); } } - *pHighwater = 0; /* IMP: R-42420-56072 */ + *pHighwtr = 0; /* IMP: R-42420-56072 */ /* IMP: R-54100-20147 */ /* IMP: R-29431-39229 */ *pCurrent = nRet; break; } + /* Set *pCurrent to the number of bytes that the db database connection + ** has spilled to the filesystem in temporary files that could have been + ** stored in memory, had sufficient memory been available. + ** The *pHighwater is always set to zero. + */ + case SQLITE_DBSTATUS_TEMPBUF_SPILL: { + u64 nRet = 0; + if( db->aDb[1].pBt ){ + Pager *pPager = sqlite3BtreePager(db->aDb[1].pBt); + sqlite3PagerCacheStat(pPager, SQLITE_DBSTATUS_CACHE_WRITE, + resetFlag, &nRet); + nRet *= sqlite3BtreeGetPageSize(db->aDb[1].pBt); + } + nRet += db->nSpill; + if( resetFlag ) db->nSpill = 0; + *pHighwtr = 0; + *pCurrent = nRet; + break; + } + /* Set *pCurrent to non-zero if there are unresolved deferred foreign ** key constraints. Set *pCurrent to zero if all foreign key constraints - ** have been satisfied. The *pHighwater is always set to zero. + ** have been satisfied. The *pHighwtr is always set to zero. */ case SQLITE_DBSTATUS_DEFERRED_FKS: { - *pHighwater = 0; /* IMP: R-11967-56545 */ + *pHighwtr = 0; /* IMP: R-11967-56545 */ *pCurrent = db->nDeferredImmCons>0 || db->nDeferredCons>0; break; } @@ -344,3 +419,28 @@ int sqlite3_db_status( sqlite3_mutex_leave(db->mutex); return rc; } + +/* +** 32-bit variant of sqlite3_db_status64() +*/ +int sqlite3_db_status( + sqlite3 *db, /* The database connection whose status is desired */ + int op, /* Status verb */ + int *pCurrent, /* Write current value here */ + int *pHighwtr, /* Write high-water mark here */ + int resetFlag /* Reset high-water mark if true */ +){ + sqlite3_int64 C = 0, H = 0; + int rc; +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) || pCurrent==0|| pHighwtr==0 ){ + return SQLITE_MISUSE_BKPT; + } +#endif + rc = sqlite3_db_status64(db, op, &C, &H, resetFlag); + if( rc==0 ){ + *pCurrent = C & 0x7fffffff; + *pHighwtr = H & 0x7fffffff; + } + return rc; +} diff --git a/src/table.c b/src/table.c index 153bfb319f..db60a827a0 100644 --- a/src/table.c +++ b/src/table.c @@ -17,8 +17,6 @@ ** if they are not used. */ #include "sqliteInt.h" -#include <stdlib.h> -#include <string.h> #ifndef SQLITE_OMIT_GET_TABLE @@ -58,7 +56,7 @@ static int sqlite3_get_table_cb(void *pArg, int nCol, char **argv, char **colv){ if( p->nData + need > p->nAlloc ){ char **azNew; p->nAlloc = p->nAlloc*2 + need; - azNew = sqlite3_realloc64( p->azResult, sizeof(char*)*p->nAlloc ); + azNew = sqlite3Realloc( p->azResult, sizeof(char*)*p->nAlloc ); if( azNew==0 ) goto malloc_failed; p->azResult = azNew; } @@ -101,7 +99,7 @@ static int sqlite3_get_table_cb(void *pArg, int nCol, char **argv, char **colv){ return 0; malloc_failed: - p->rc = SQLITE_NOMEM; + p->rc = SQLITE_NOMEM_BKPT; return 1; } @@ -142,7 +140,7 @@ int sqlite3_get_table( res.azResult = sqlite3_malloc64(sizeof(char*)*res.nAlloc ); if( res.azResult==0 ){ db->errCode = SQLITE_NOMEM; - return SQLITE_NOMEM; + return SQLITE_NOMEM_BKPT; } res.azResult[0] = 0; rc = sqlite3_exec(db, zSql, sqlite3_get_table_cb, &res, pzErrMsg); @@ -167,11 +165,11 @@ int sqlite3_get_table( } if( res.nAlloc>res.nData ){ char **azNew; - azNew = sqlite3_realloc64( res.azResult, sizeof(char*)*res.nData ); + azNew = sqlite3Realloc( res.azResult, sizeof(char*)*res.nData ); if( azNew==0 ){ sqlite3_free_table(&res.azResult[1]); db->errCode = SQLITE_NOMEM; - return SQLITE_NOMEM; + return SQLITE_NOMEM_BKPT; } res.azResult = azNew; } diff --git a/src/tclsqlite.c b/src/tclsqlite.c index 77da6eef52..32d6d6a120 100644 --- a/src/tclsqlite.c +++ b/src/tclsqlite.c @@ -14,26 +14,48 @@ ** ** Compile-time options: ** -** -DTCLSH=1 Add a "main()" routine that works as a tclsh. +** -DTCLSH Add a "main()" routine that works as a tclsh. ** -** -DSQLITE_TCLMD5 When used in conjuction with -DTCLSH=1, add -** four new commands to the TCL interpreter for -** generating MD5 checksums: md5, md5file, -** md5-10x8, and md5file-10x8. +** -DTCLSH_INIT_PROC=name ** -** -DSQLITE_TEST When used in conjuction with -DTCLSH=1, add -** hundreds of new commands used for testing -** SQLite. This option implies -DSQLITE_TCLMD5. +** Invoke name(interp) to initialize the Tcl interpreter. +** If name(interp) returns a non-NULL string, then run +** that string as a Tcl script to launch the application. +** If name(interp) returns NULL, then run the regular +** tclsh-emulator code. */ +#ifdef TCLSH_INIT_PROC +# define TCLSH 1 +#endif /* ** If requested, include the SQLite compiler options file for MSVC. */ #if defined(INCLUDE_MSVC_H) -#include "msvc.h" +# include "msvc.h" +#endif + +/****** Copy of tclsqlite.h ******/ +#if defined(INCLUDE_SQLITE_TCL_H) +# include "sqlite_tcl.h" /* Special case for Windows using STDCALL */ +#else +# include <tcl.h> /* All normal cases */ +# ifndef SQLITE_TCLAPI +# define SQLITE_TCLAPI +# endif +#endif +/* Compatability between Tcl8.6 and Tcl9.0 */ +#if TCL_MAJOR_VERSION==9 +# define CONST const +#elif !defined(Tcl_Size) + typedef int Tcl_Size; +# ifndef Tcl_BounceRefCount +# define Tcl_BounceRefCount(X) Tcl_IncrRefCount(X); Tcl_DecrRefCount(X) + /* https://www.tcl-lang.org/man/tcl9.0/TclLib/Object.html */ +# endif #endif +/**** End copy of tclsqlite.h ****/ -#include "tcl.h" #include <errno.h> /* @@ -46,18 +68,44 @@ # include <string.h> # include <assert.h> typedef unsigned char u8; +# ifndef SQLITE_PTRSIZE +# if defined(__SIZEOF_POINTER__) +# define SQLITE_PTRSIZE __SIZEOF_POINTER__ +# elif defined(i386) || defined(__i386__) || defined(_M_IX86) || \ + defined(_M_ARM) || defined(__arm__) || defined(__x86) || \ + (defined(__APPLE__) && defined(__POWERPC__)) || \ + (defined(__TOS_AIX__) && !defined(__64BIT__)) +# define SQLITE_PTRSIZE 4 +# else +# define SQLITE_PTRSIZE 8 +# endif +# endif /* SQLITE_PTRSIZE */ +# if defined(HAVE_STDINT_H) || (defined(__STDC_VERSION__) && \ + (__STDC_VERSION__ >= 199901L)) +# include <stdint.h> + typedef uintptr_t uptr; +# elif SQLITE_PTRSIZE==4 + typedef unsigned int uptr; +# else + typedef sqlite3_uint64 uptr; +# endif #endif #include <ctype.h> /* Used to get the current process ID */ #if !defined(_WIN32) +# include <signal.h> # include <unistd.h> # define GETPID getpid #elif !defined(_WIN32_WCE) # ifndef SQLITE_AMALGAMATION -# define WIN32_LEAN_AND_MEAN +# ifndef WIN32_LEAN_AND_MEAN +# define WIN32_LEAN_AND_MEAN +# endif # include <windows.h> # endif +# include <io.h> +# define isatty(h) _isatty(h) # define GETPID (int)GetCurrentProcessId #endif @@ -79,6 +127,14 @@ typedef struct SqliteDb SqliteDb; /* ** New SQL functions can be created as TCL scripts. Each such function ** is described by an instance of the following structure. +** +** Variable eType may be set to SQLITE_INTEGER, SQLITE_FLOAT, SQLITE_TEXT, +** SQLITE_BLOB or SQLITE_NULL. If it is SQLITE_NULL, then the implementation +** attempts to determine the type of the result based on the Tcl object. +** If it is SQLITE_TEXT or SQLITE_BLOB, then a text (sqlite3_result_text()) +** or blob (sqlite3_result_blob()) is returned. If it is SQLITE_INTEGER +** or SQLITE_FLOAT, then an attempt is made to return an integer or float +** value, falling back to float and then text if this is not possible. */ typedef struct SqlFunc SqlFunc; struct SqlFunc { @@ -86,6 +142,7 @@ struct SqlFunc { Tcl_Obj *pScript; /* The Tcl_Obj representation of the script */ SqliteDb *pDb; /* Database connection that owns this function */ int useEvalObjv; /* True if it is safe to use Tcl_EvalObjv */ + int eType; /* Type of value to return */ char *zName; /* Name of this function */ SqlFunc *pNext; /* Next function on the list of them all */ }; @@ -133,13 +190,16 @@ struct SqliteDb { char *zBusy; /* The busy callback routine */ char *zCommit; /* The commit hook callback routine */ char *zTrace; /* The trace callback routine */ + char *zTraceV2; /* The trace_v2 callback routine */ char *zProfile; /* The profile callback routine */ char *zProgress; /* The progress callback routine */ + char *zBindFallback; /* Callback to invoke on a binding miss */ char *zAuth; /* The authorization callback routine */ int disableAuth; /* Disable the authorizer if it exists */ char *zNull; /* Text to substitute for an SQL NULL value */ SqlFunc *pFunc; /* List of SQL functions */ Tcl_Obj *pUpdateHook; /* Update hook script (if any) */ + Tcl_Obj *pPreUpdateHook; /* Pre-update hook script (if any) */ Tcl_Obj *pRollbackHook; /* Rollback hook script (if any) */ Tcl_Obj *pWalHook; /* WAL hook script (if any) */ Tcl_Obj *pUnlockNotify; /* Unlock notify script (if any) */ @@ -152,8 +212,10 @@ struct SqliteDb { int nStmt; /* Number of statements in stmtList */ IncrblobChannel *pIncrblob;/* Linked list of open incrblob channels */ int nStep, nSort, nIndex; /* Statistics for most recent operation */ + int nVMStep; /* Another statistic for most recent operation */ int nTransaction; /* Number of nested [transaction] methods */ int openFlags; /* Flags used to open. (SQLITE_OPEN_URI) */ + int nRef; /* Delete object when this reaches 0 */ #ifdef SQLITE_TEST int bLegacyPrepare; /* True to use sqlite3_prepare() */ #endif @@ -162,7 +224,8 @@ struct SqliteDb { struct IncrblobChannel { sqlite3_blob *pBlob; /* sqlite3 blob handle */ SqliteDb *pDb; /* Associated database connection */ - int iSeek; /* Current seek offset */ + sqlite3_int64 iSeek; /* Current seek offset */ + unsigned int isClosed; /* TCL_CLOSE_READ or TCL_CLOSE_WRITE */ Tcl_Channel channel; /* Channel identifier */ IncrblobChannel *pNext; /* Linked list of all open incrblob channels */ IncrblobChannel *pPrev; /* Linked list of all open incrblob channels */ @@ -191,7 +254,7 @@ static void closeIncrblobChannels(SqliteDb *pDb){ for(p=pDb->pIncrblob; p; p=pNext){ pNext = p->pNext; - /* Note: Calling unregister here call Tcl_Close on the incrblob channel, + /* Note: Calling unregister here call Tcl_Close on the incrblob channel, ** which deletes the IncrblobChannel structure at *p. So do not ** call Tcl_Free() here. */ @@ -202,11 +265,23 @@ static void closeIncrblobChannels(SqliteDb *pDb){ /* ** Close an incremental blob channel. */ -static int incrblobClose(ClientData instanceData, Tcl_Interp *interp){ +static int SQLITE_TCLAPI incrblobClose2( + ClientData instanceData, + Tcl_Interp *interp, + int flags +){ IncrblobChannel *p = (IncrblobChannel *)instanceData; - int rc = sqlite3_blob_close(p->pBlob); + int rc; sqlite3 *db = p->pDb->db; + if( flags ){ + p->isClosed |= flags; + return TCL_OK; + } + + /* If we reach this point, then we really do need to close the channel */ + rc = sqlite3_blob_close(p->pBlob); + /* Remove the channel from the SqliteDb.pIncrblob list. */ if( p->pNext ){ p->pNext->pPrev = p->pPrev; @@ -227,20 +302,27 @@ static int incrblobClose(ClientData instanceData, Tcl_Interp *interp){ } return TCL_OK; } +static int SQLITE_TCLAPI incrblobClose( + ClientData instanceData, + Tcl_Interp *interp +){ + return incrblobClose2(instanceData, interp, 0); +} + /* ** Read data from an incremental blob channel. */ -static int incrblobInput( - ClientData instanceData, - char *buf, +static int SQLITE_TCLAPI incrblobInput( + ClientData instanceData, + char *buf, int bufSize, int *errorCodePtr ){ IncrblobChannel *p = (IncrblobChannel *)instanceData; - int nRead = bufSize; /* Number of bytes to read */ - int nBlob; /* Total size of the blob */ - int rc; /* sqlite error code */ + sqlite3_int64 nRead = bufSize; /* Number of bytes to read */ + sqlite3_int64 nBlob; /* Total size of the blob */ + int rc; /* sqlite error code */ nBlob = sqlite3_blob_bytes(p->pBlob); if( (p->iSeek+nRead)>nBlob ){ @@ -250,7 +332,7 @@ static int incrblobInput( return 0; } - rc = sqlite3_blob_read(p->pBlob, (void *)buf, nRead, p->iSeek); + rc = sqlite3_blob_read(p->pBlob, (void *)buf, (int)nRead, (int)p->iSeek); if( rc!=SQLITE_OK ){ *errorCodePtr = rc; return -1; @@ -263,16 +345,16 @@ static int incrblobInput( /* ** Write data to an incremental blob channel. */ -static int incrblobOutput( - ClientData instanceData, - CONST char *buf, +static int SQLITE_TCLAPI incrblobOutput( + ClientData instanceData, + const char *buf, int toWrite, int *errorCodePtr ){ IncrblobChannel *p = (IncrblobChannel *)instanceData; - int nWrite = toWrite; /* Number of bytes to write */ - int nBlob; /* Total size of the blob */ - int rc; /* sqlite error code */ + sqlite3_int64 nWrite = toWrite; /* Number of bytes to write */ + sqlite3_int64 nBlob; /* Total size of the blob */ + int rc; /* sqlite error code */ nBlob = sqlite3_blob_bytes(p->pBlob); if( (p->iSeek+nWrite)>nBlob ){ @@ -283,7 +365,7 @@ static int incrblobOutput( return 0; } - rc = sqlite3_blob_write(p->pBlob, (void *)buf, nWrite, p->iSeek); + rc = sqlite3_blob_write(p->pBlob, (void*)buf,(int)nWrite, (int)p->iSeek); if( rc!=SQLITE_OK ){ *errorCodePtr = EIO; return -1; @@ -293,12 +375,19 @@ static int incrblobOutput( return nWrite; } +/* The datatype of Tcl_DriverWideSeekProc changes between tcl8.6 and tcl9.0 */ +#if TCL_MAJOR_VERSION==9 +# define WideSeekProcType long long +#else +# define WideSeekProcType Tcl_WideInt +#endif + /* ** Seek an incremental blob channel. */ -static int incrblobSeek( - ClientData instanceData, - long offset, +static WideSeekProcType SQLITE_TCLAPI incrblobWideSeek( + ClientData instanceData, + WideSeekProcType offset, int seekMode, int *errorCodePtr ){ @@ -320,18 +409,33 @@ static int incrblobSeek( return p->iSeek; } +static int SQLITE_TCLAPI incrblobSeek( + ClientData instanceData, + long offset, + int seekMode, + int *errorCodePtr +){ + return incrblobWideSeek(instanceData,offset,seekMode,errorCodePtr); +} -static void incrblobWatch(ClientData instanceData, int mode){ - /* NO-OP */ +static void SQLITE_TCLAPI incrblobWatch( + ClientData instanceData, + int mode +){ + /* NO-OP */ } -static int incrblobHandle(ClientData instanceData, int dir, ClientData *hPtr){ +static int SQLITE_TCLAPI incrblobHandle( + ClientData instanceData, + int dir, + ClientData *hPtr +){ return TCL_ERROR; } static Tcl_ChannelType IncrblobChannelType = { "incrblob", /* typeName */ - TCL_CHANNEL_VERSION_2, /* version */ + TCL_CHANNEL_VERSION_5, /* version */ incrblobClose, /* closeProc */ incrblobInput, /* inputProc */ incrblobOutput, /* outputProc */ @@ -340,22 +444,22 @@ static Tcl_ChannelType IncrblobChannelType = { 0, /* getOptionProc */ incrblobWatch, /* watchProc (this is a no-op) */ incrblobHandle, /* getHandleProc (always returns error) */ - 0, /* close2Proc */ + incrblobClose2, /* close2Proc */ 0, /* blockModeProc */ 0, /* flushProc */ 0, /* handlerProc */ - 0, /* wideSeekProc */ + incrblobWideSeek, /* wideSeekProc */ }; /* ** Create a new incrblob channel. */ static int createIncrblobChannel( - Tcl_Interp *interp, - SqliteDb *pDb, + Tcl_Interp *interp, + SqliteDb *pDb, const char *zDb, - const char *zTable, - const char *zColumn, + const char *zTable, + const char *zColumn, sqlite_int64 iRow, int isReadonly ){ @@ -376,8 +480,9 @@ static int createIncrblobChannel( } p = (IncrblobChannel *)Tcl_Alloc(sizeof(IncrblobChannel)); - p->iSeek = 0; + memset(p, 0, sizeof(*p)); p->pBlob = pBlob; + if( (flags & TCL_WRITABLE)==0 ) p->isClosed |= TCL_CLOSE_WRITE; sqlite3_snprintf(sizeof(zChannel), zChannel, "incrblob_%d", ++count); p->channel = Tcl_CreateChannel(&IncrblobChannelType, zChannel, p, flags); @@ -411,13 +516,13 @@ static int createIncrblobChannel( ** or {...} or ; to be seen anywhere. Most callback scripts consist ** of just a single procedure name and they meet this requirement. */ -static int safeToUseEvalObjv(Tcl_Interp *interp, Tcl_Obj *pCmd){ +static int safeToUseEvalObjv(Tcl_Obj *pCmd){ /* We could try to do something with Tcl_Parse(). But we will instead ** just do a search for forbidden characters. If any of the forbidden ** characters appear in pCmd, we will report the string as unsafe. */ const char *z; - int n; + Tcl_Size n; z = Tcl_GetStringFromObj(pCmd, &n); while( n-- > 0 ){ int c = *(z++); @@ -437,7 +542,7 @@ static SqlFunc *findSqlFunc(SqliteDb *pDb, const char *zName){ pNew = (SqlFunc*)Tcl_Alloc( sizeof(*pNew) + nName + 1 ); pNew->zName = (char*)&pNew[1]; memcpy(pNew->zName, zName, nName+1); - for(p=pDb->pFunc; p; p=p->pNext){ + for(p=pDb->pFunc; p; p=p->pNext){ if( sqlite3_stricmp(p->zName, pNew->zName)==0 ){ Tcl_Free((char*)pNew); return p; @@ -480,55 +585,84 @@ static void flushStmtCache(SqliteDb *pDb){ pDb->stmtList = 0; } +/* +** Increment the reference counter on the SqliteDb object. The reference +** should be released by calling delDatabaseRef(). +*/ +static void addDatabaseRef(SqliteDb *pDb){ + pDb->nRef++; +} + +/* +** Decrement the reference counter associated with the SqliteDb object. +** If it reaches zero, delete the object. +*/ +static void delDatabaseRef(SqliteDb *pDb){ + assert( pDb->nRef>0 ); + pDb->nRef--; + if( pDb->nRef==0 ){ + flushStmtCache(pDb); + closeIncrblobChannels(pDb); + sqlite3_close(pDb->db); + while( pDb->pFunc ){ + SqlFunc *pFunc = pDb->pFunc; + pDb->pFunc = pFunc->pNext; + assert( pFunc->pDb==pDb ); + Tcl_DecrRefCount(pFunc->pScript); + Tcl_Free((char*)pFunc); + } + while( pDb->pCollate ){ + SqlCollate *pCollate = pDb->pCollate; + pDb->pCollate = pCollate->pNext; + Tcl_Free((char*)pCollate); + } + if( pDb->zBusy ){ + Tcl_Free(pDb->zBusy); + } + if( pDb->zTrace ){ + Tcl_Free(pDb->zTrace); + } + if( pDb->zTraceV2 ){ + Tcl_Free(pDb->zTraceV2); + } + if( pDb->zProfile ){ + Tcl_Free(pDb->zProfile); + } + if( pDb->zBindFallback ){ + Tcl_Free(pDb->zBindFallback); + } + if( pDb->zAuth ){ + Tcl_Free(pDb->zAuth); + } + if( pDb->zNull ){ + Tcl_Free(pDb->zNull); + } + if( pDb->pUpdateHook ){ + Tcl_DecrRefCount(pDb->pUpdateHook); + } + if( pDb->pPreUpdateHook ){ + Tcl_DecrRefCount(pDb->pPreUpdateHook); + } + if( pDb->pRollbackHook ){ + Tcl_DecrRefCount(pDb->pRollbackHook); + } + if( pDb->pWalHook ){ + Tcl_DecrRefCount(pDb->pWalHook); + } + if( pDb->pCollateNeeded ){ + Tcl_DecrRefCount(pDb->pCollateNeeded); + } + Tcl_Free((char*)pDb); + } +} + /* ** TCL calls this procedure when an sqlite3 database command is ** deleted. */ -static void DbDeleteCmd(void *db){ +static void SQLITE_TCLAPI DbDeleteCmd(void *db){ SqliteDb *pDb = (SqliteDb*)db; - flushStmtCache(pDb); - closeIncrblobChannels(pDb); - sqlite3_close(pDb->db); - while( pDb->pFunc ){ - SqlFunc *pFunc = pDb->pFunc; - pDb->pFunc = pFunc->pNext; - assert( pFunc->pDb==pDb ); - Tcl_DecrRefCount(pFunc->pScript); - Tcl_Free((char*)pFunc); - } - while( pDb->pCollate ){ - SqlCollate *pCollate = pDb->pCollate; - pDb->pCollate = pCollate->pNext; - Tcl_Free((char*)pCollate); - } - if( pDb->zBusy ){ - Tcl_Free(pDb->zBusy); - } - if( pDb->zTrace ){ - Tcl_Free(pDb->zTrace); - } - if( pDb->zProfile ){ - Tcl_Free(pDb->zProfile); - } - if( pDb->zAuth ){ - Tcl_Free(pDb->zAuth); - } - if( pDb->zNull ){ - Tcl_Free(pDb->zNull); - } - if( pDb->pUpdateHook ){ - Tcl_DecrRefCount(pDb->pUpdateHook); - } - if( pDb->pRollbackHook ){ - Tcl_DecrRefCount(pDb->pRollbackHook); - } - if( pDb->pWalHook ){ - Tcl_DecrRefCount(pDb->pWalHook); - } - if( pDb->pCollateNeeded ){ - Tcl_DecrRefCount(pDb->pCollateNeeded); - } - Tcl_Free((char*)pDb); + delDatabaseRef(pDb); } /* @@ -565,7 +699,8 @@ static int DbProgressHandler(void *cd){ } #endif -#ifndef SQLITE_OMIT_TRACE +#if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) && \ + !defined(SQLITE_OMIT_DEPRECATED) /* ** This routine is called by the SQLite trace handler whenever a new ** block of SQL is executed. The TCL script in pDb->zTrace is executed. @@ -585,6 +720,83 @@ static void DbTraceHandler(void *cd, const char *zSql){ #ifndef SQLITE_OMIT_TRACE /* +** This routine is called by the SQLite trace_v2 handler whenever a new +** supported event is generated. Unsupported event types are ignored. +** The TCL script in pDb->zTraceV2 is executed, with the arguments for +** the event appended to it (as list elements). +*/ +static int DbTraceV2Handler( + unsigned type, /* One of the SQLITE_TRACE_* event types. */ + void *cd, /* The original context data pointer. */ + void *pd, /* Primary event data, depends on event type. */ + void *xd /* Extra event data, depends on event type. */ +){ + SqliteDb *pDb = (SqliteDb*)cd; + Tcl_Obj *pCmd; + + switch( type ){ + case SQLITE_TRACE_STMT: { + sqlite3_stmt *pStmt = (sqlite3_stmt *)pd; + char *zSql = (char *)xd; + + pCmd = Tcl_NewStringObj(pDb->zTraceV2, -1); + Tcl_IncrRefCount(pCmd); + Tcl_ListObjAppendElement(pDb->interp, pCmd, + Tcl_NewWideIntObj((Tcl_WideInt)(uptr)pStmt)); + Tcl_ListObjAppendElement(pDb->interp, pCmd, + Tcl_NewStringObj(zSql, -1)); + Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT); + Tcl_DecrRefCount(pCmd); + Tcl_ResetResult(pDb->interp); + break; + } + case SQLITE_TRACE_PROFILE: { + sqlite3_stmt *pStmt = (sqlite3_stmt *)pd; + sqlite3_int64 ns = *(sqlite3_int64*)xd; + + pCmd = Tcl_NewStringObj(pDb->zTraceV2, -1); + Tcl_IncrRefCount(pCmd); + Tcl_ListObjAppendElement(pDb->interp, pCmd, + Tcl_NewWideIntObj((Tcl_WideInt)(uptr)pStmt)); + Tcl_ListObjAppendElement(pDb->interp, pCmd, + Tcl_NewWideIntObj((Tcl_WideInt)ns)); + Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT); + Tcl_DecrRefCount(pCmd); + Tcl_ResetResult(pDb->interp); + break; + } + case SQLITE_TRACE_ROW: { + sqlite3_stmt *pStmt = (sqlite3_stmt *)pd; + + pCmd = Tcl_NewStringObj(pDb->zTraceV2, -1); + Tcl_IncrRefCount(pCmd); + Tcl_ListObjAppendElement(pDb->interp, pCmd, + Tcl_NewWideIntObj((Tcl_WideInt)(uptr)pStmt)); + Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT); + Tcl_DecrRefCount(pCmd); + Tcl_ResetResult(pDb->interp); + break; + } + case SQLITE_TRACE_CLOSE: { + sqlite3 *db = (sqlite3 *)pd; + + pCmd = Tcl_NewStringObj(pDb->zTraceV2, -1); + Tcl_IncrRefCount(pCmd); + Tcl_ListObjAppendElement(pDb->interp, pCmd, + Tcl_NewWideIntObj((Tcl_WideInt)(uptr)db)); + Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT); + Tcl_DecrRefCount(pCmd); + Tcl_ResetResult(pDb->interp); + break; + } + } + return SQLITE_OK; +} +#endif + +#if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) && \ + !defined(SQLITE_OMIT_DEPRECATED) +/* ** This routine is called by the SQLite profile handler after a statement ** SQL has executed. The TCL script in pDb->zProfile is evaluated. */ @@ -633,9 +845,9 @@ static void DbRollbackHandler(void *clientData){ ** This procedure handles wal_hook callbacks. */ static int DbWalHandler( - void *clientData, - sqlite3 *db, - const char *zDb, + void *clientData, + sqlite3 *db, + const char *zDb, int nEntry ){ int ret = SQLITE_OK; @@ -649,7 +861,7 @@ static int DbWalHandler( Tcl_IncrRefCount(p); Tcl_ListObjAppendElement(interp, p, Tcl_NewStringObj(zDb, -1)); Tcl_ListObjAppendElement(interp, p, Tcl_NewIntObj(nEntry)); - if( TCL_OK!=Tcl_EvalObjEx(interp, p, 0) + if( TCL_OK!=Tcl_EvalObjEx(interp, p, 0) || TCL_OK!=Tcl_GetIntFromObj(interp, Tcl_GetObjResult(interp), &ret) ){ Tcl_BackgroundError(interp); @@ -686,23 +898,63 @@ static void DbUnlockNotify(void **apArg, int nArg){ } #endif +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK +/* +** Pre-update hook callback. +*/ +static void DbPreUpdateHandler( + void *p, + sqlite3 *db, + int op, + const char *zDb, + const char *zTbl, + sqlite_int64 iKey1, + sqlite_int64 iKey2 +){ + SqliteDb *pDb = (SqliteDb *)p; + Tcl_Obj *pCmd; + static const char *azStr[] = {"DELETE", "INSERT", "UPDATE"}; + + assert( (SQLITE_DELETE-1)/9 == 0 ); + assert( (SQLITE_INSERT-1)/9 == 1 ); + assert( (SQLITE_UPDATE-1)/9 == 2 ); + assert( pDb->pPreUpdateHook ); + assert( db==pDb->db ); + assert( op==SQLITE_INSERT || op==SQLITE_UPDATE || op==SQLITE_DELETE ); + + pCmd = Tcl_DuplicateObj(pDb->pPreUpdateHook); + Tcl_IncrRefCount(pCmd); + Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(azStr[(op-1)/9], -1)); + Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zDb, -1)); + Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zTbl, -1)); + Tcl_ListObjAppendElement(0, pCmd, Tcl_NewWideIntObj(iKey1)); + Tcl_ListObjAppendElement(0, pCmd, Tcl_NewWideIntObj(iKey2)); + Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT); + Tcl_DecrRefCount(pCmd); +} +#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ + static void DbUpdateHandler( - void *p, + void *p, int op, - const char *zDb, - const char *zTbl, + const char *zDb, + const char *zTbl, sqlite_int64 rowid ){ SqliteDb *pDb = (SqliteDb *)p; Tcl_Obj *pCmd; + static const char *azStr[] = {"DELETE", "INSERT", "UPDATE"}; + + assert( (SQLITE_DELETE-1)/9 == 0 ); + assert( (SQLITE_INSERT-1)/9 == 1 ); + assert( (SQLITE_UPDATE-1)/9 == 2 ); assert( pDb->pUpdateHook ); assert( op==SQLITE_INSERT || op==SQLITE_UPDATE || op==SQLITE_DELETE ); pCmd = Tcl_DuplicateObj(pDb->pUpdateHook); Tcl_IncrRefCount(pCmd); - Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj( - ( (op==SQLITE_INSERT)?"INSERT":(op==SQLITE_UPDATE)?"UPDATE":"DELETE"), -1)); + Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(azStr[(op-1)/9], -1)); Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zDb, -1)); Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zTbl, -1)); Tcl_ListObjAppendElement(0, pCmd, Tcl_NewWideIntObj(rowid)); @@ -771,23 +1023,23 @@ static void tclSqlFunc(sqlite3_context *context, int argc, sqlite3_value**argv){ ** script object, lappend the arguments, then evaluate the copy. ** ** By "shallow" copy, we mean only the outer list Tcl_Obj is duplicated. - ** The new Tcl_Obj contains pointers to the original list elements. + ** The new Tcl_Obj contains pointers to the original list elements. ** That way, when Tcl_EvalObjv() is run and shimmers the first element ** of the list to tclCmdNameType, that alternate representation will ** be preserved and reused on the next invocation. */ Tcl_Obj **aArg; - int nArg; + Tcl_Size nArg; if( Tcl_ListObjGetElements(p->interp, p->pScript, &nArg, &aArg) ){ - sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1); + sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1); return; - } + } pCmd = Tcl_NewListObj(nArg, aArg); Tcl_IncrRefCount(pCmd); for(i=0; i<argc; i++){ sqlite3_value *pIn = argv[i]; Tcl_Obj *pVal; - + /* Set pVal to contain the i'th column of this row. */ switch( sqlite3_value_type(pIn) ){ case SQLITE_BLOB: { @@ -822,7 +1074,7 @@ static void tclSqlFunc(sqlite3_context *context, int argc, sqlite3_value**argv){ rc = Tcl_ListObjAppendElement(p->interp, pCmd, pVal); if( rc ){ Tcl_DecrRefCount(pCmd); - sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1); + sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1); return; } } @@ -836,35 +1088,66 @@ static void tclSqlFunc(sqlite3_context *context, int argc, sqlite3_value**argv){ Tcl_DecrRefCount(pCmd); } - if( rc && rc!=TCL_RETURN ){ - sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1); + if( TCL_BREAK==rc ){ + sqlite3_result_null(context); + }else if( rc && rc!=TCL_RETURN ){ + sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1); }else{ Tcl_Obj *pVar = Tcl_GetObjResult(p->interp); - int n; + Tcl_Size n; u8 *data; const char *zType = (pVar->typePtr ? pVar->typePtr->name : ""); char c = zType[0]; - if( c=='b' && strcmp(zType,"bytearray")==0 && pVar->bytes==0 ){ - /* Only return a BLOB type if the Tcl variable is a bytearray and - ** has no string representation. */ - data = Tcl_GetByteArrayFromObj(pVar, &n); - sqlite3_result_blob(context, data, n, SQLITE_TRANSIENT); - }else if( c=='b' && strcmp(zType,"boolean")==0 ){ - Tcl_GetIntFromObj(0, pVar, &n); - sqlite3_result_int(context, n); - }else if( c=='d' && strcmp(zType,"double")==0 ){ - double r; - Tcl_GetDoubleFromObj(0, pVar, &r); - sqlite3_result_double(context, r); - }else if( (c=='w' && strcmp(zType,"wideInt")==0) || - (c=='i' && strcmp(zType,"int")==0) ){ - Tcl_WideInt v; - Tcl_GetWideIntFromObj(0, pVar, &v); - sqlite3_result_int64(context, v); - }else{ - data = (unsigned char *)Tcl_GetStringFromObj(pVar, &n); - sqlite3_result_text(context, (char *)data, n, SQLITE_TRANSIENT); + int eType = p->eType; + + if( eType==SQLITE_NULL ){ + if( c=='b' && strcmp(zType,"bytearray")==0 && pVar->bytes==0 ){ + /* Only return a BLOB type if the Tcl variable is a bytearray and + ** has no string representation. */ + eType = SQLITE_BLOB; + }else if( (c=='b' && pVar->bytes==0 && strcmp(zType,"boolean")==0 ) + || (c=='b' && pVar->bytes==0 && strcmp(zType,"booleanString")==0 ) + || (c=='w' && strcmp(zType,"wideInt")==0) + || (c=='i' && strcmp(zType,"int")==0) + ){ + eType = SQLITE_INTEGER; + }else if( c=='d' && strcmp(zType,"double")==0 ){ + eType = SQLITE_FLOAT; + }else{ + eType = SQLITE_TEXT; + } + } + + switch( eType ){ + case SQLITE_BLOB: { + data = Tcl_GetByteArrayFromObj(pVar, &n); + sqlite3_result_blob(context, data, n, SQLITE_TRANSIENT); + break; + } + case SQLITE_INTEGER: { + Tcl_WideInt v; + if( TCL_OK==Tcl_GetWideIntFromObj(0, pVar, &v) ){ + sqlite3_result_int64(context, v); + break; + } + /* fall-through */ + } + case SQLITE_FLOAT: { + double r; + if( TCL_OK==Tcl_GetDoubleFromObj(0, pVar, &r) ){ + sqlite3_result_double(context, r); + break; + } + /* fall-through */ + } + default: { + data = (unsigned char *)Tcl_GetStringFromObj(pVar, &n); + sqlite3_result_text64(context, (char *)data, n, SQLITE_TRANSIENT, + SQLITE_UTF8); + break; + } } + } } @@ -882,17 +1165,21 @@ static int auth_callback( const char *zArg2, const char *zArg3, const char *zArg4 -#ifdef SQLITE_USER_AUTHENTICATION - ,const char *zArg5 -#endif ){ const char *zCode; Tcl_DString str; int rc; const char *zReply; + /* EVIDENCE-OF: R-38590-62769 The first parameter to the authorizer + ** callback is a copy of the third parameter to the + ** sqlite3_set_authorizer() interface. + */ SqliteDb *pDb = (SqliteDb*)pArg; if( pDb->disableAuth ) return SQLITE_OK; + /* EVIDENCE-OF: R-56518-44310 The second parameter to the callback is an + ** integer action code that specifies the particular action to be + ** authorized. */ switch( code ){ case SQLITE_COPY : zCode="SQLITE_COPY"; break; case SQLITE_CREATE_INDEX : zCode="SQLITE_CREATE_INDEX"; break; @@ -937,9 +1224,6 @@ static int auth_callback( Tcl_DStringAppendElement(&str, zArg2 ? zArg2 : ""); Tcl_DStringAppendElement(&str, zArg3 ? zArg3 : ""); Tcl_DStringAppendElement(&str, zArg4 ? zArg4 : ""); -#ifdef SQLITE_USER_AUTHENTICATION - Tcl_DStringAppendElement(&str, zArg5 ? zArg5 : ""); -#endif rc = Tcl_GlobalEval(pDb->interp, Tcl_DStringValue(&str)); Tcl_DStringFree(&str); zReply = rc==TCL_OK ? Tcl_GetStringResult(pDb->interp) : "SQLITE_DENY"; @@ -956,6 +1240,7 @@ static int auth_callback( } #endif /* SQLITE_OMIT_AUTHORIZATION */ +#if 0 /* ** This routine reads a line of text from FILE in, stores ** the text in memory obtained from malloc() and returns a pointer @@ -1000,6 +1285,7 @@ static char *local_getline(char *zPrompt, FILE *in){ zLine = realloc( zLine, n+1 ); return zLine; } +#endif /* @@ -1010,7 +1296,7 @@ static char *local_getline(char *zPrompt, FILE *in){ ** It is invoked after evaluating the script SCRIPT to commit or rollback ** the transaction or savepoint opened by the [transaction] command. */ -static int DbTransPostCmd( +static int SQLITE_TCLAPI DbTransPostCmd( ClientData data[], /* data[0] is the Sqlite3Db* for $db */ Tcl_Interp *interp, /* Tcl interpreter */ int result /* Result of evaluating SCRIPT */ @@ -1031,12 +1317,12 @@ static int DbTransPostCmd( pDb->disableAuth++; if( sqlite3_exec(pDb->db, zEnd, 0, 0, 0) ){ /* This is a tricky scenario to handle. The most likely cause of an - ** error is that the exec() above was an attempt to commit the + ** error is that the exec() above was an attempt to commit the ** top-level transaction that returned SQLITE_BUSY. Or, less likely, ** that an IO-error has occurred. In either case, throw a Tcl exception ** and try to rollback the transaction. ** - ** But it could also be that the user executed one or more BEGIN, + ** But it could also be that the user executed one or more BEGIN, ** COMMIT, SAVEPOINT, RELEASE or ROLLBACK commands that are confusing ** this method's logic. Not clear how this would be best handled. */ @@ -1048,6 +1334,7 @@ static int DbTransPostCmd( } pDb->disableAuth--; + delDatabaseRef(pDb); return rc; } @@ -1055,7 +1342,7 @@ static int DbTransPostCmd( ** Unless SQLITE_TEST is defined, this function is a simple wrapper around ** sqlite3_prepare_v2(). If SQLITE_TEST is defined, then it uses either ** sqlite3_prepare_v2() or legacy interface sqlite3_prepare(), depending -** on whether or not the [db_use_legacy_prepare] command has been used to +** on whether or not the [db_use_legacy_prepare] command has been used to ** configure the connection. */ static int dbPrepare( @@ -1064,12 +1351,18 @@ static int dbPrepare( sqlite3_stmt **ppStmt, /* OUT: Prepared statement */ const char **pzOut /* OUT: Pointer to next SQL statement */ ){ + unsigned int prepFlags = 0; #ifdef SQLITE_TEST if( pDb->bLegacyPrepare ){ return sqlite3_prepare(pDb->db, zSql, -1, ppStmt, pzOut); } #endif - return sqlite3_prepare_v2(pDb->db, zSql, -1, ppStmt, pzOut); + /* If the statement cache is large, use the SQLITE_PREPARE_PERSISTENT + ** flags, which uses less lookaside memory. But if the cache is small, + ** omit that flag to make full use of lookaside */ + if( pDb->maxStmt>5 ) prepFlags = SQLITE_PREPARE_PERSISTENT; + + return sqlite3_prepare_v3(pDb->db, zSql, -1, prepFlags, ppStmt, pzOut); } /* @@ -1101,6 +1394,8 @@ static int dbPrepareAndBind( int iParm = 0; /* Next free entry in apParm */ char c; int i; + int needResultReset = 0; /* Need to invoke Tcl_ResetResult() */ + int rc = SQLITE_OK; /* Value to return */ Tcl_Interp *interp = pDb->interp; *ppPreStmt = 0; @@ -1111,7 +1406,7 @@ static int dbPrepareAndBind( for(pPreStmt = pDb->stmtList; pPreStmt; pPreStmt=pPreStmt->pNext){ int n = pPreStmt->nSql; - if( nSql>=n + if( nSql>=n && memcmp(pPreStmt->zSql, zSql, n)==0 && (zSql[n]==0 || zSql[n-1]==';') ){ @@ -1137,7 +1432,7 @@ static int dbPrepareAndBind( break; } } - + /* If no prepared statement was found. Compile the SQL text. Also allocate ** a new SqlPreparedStmt structure. */ if( pPreStmt==0 ){ @@ -1183,13 +1478,32 @@ static int dbPrepareAndBind( assert( strlen30(pPreStmt->zSql)==pPreStmt->nSql ); assert( 0==memcmp(pPreStmt->zSql, zSql, pPreStmt->nSql) ); - /* Bind values to parameters that begin with $ or : */ + /* Bind values to parameters that begin with $ or : */ for(i=1; i<=nVar; i++){ const char *zVar = sqlite3_bind_parameter_name(pStmt, i); if( zVar!=0 && (zVar[0]=='$' || zVar[0]==':' || zVar[0]=='@') ){ Tcl_Obj *pVar = Tcl_GetVar2Ex(interp, &zVar[1], 0, 0); + if( pVar==0 && pDb->zBindFallback!=0 ){ + Tcl_Obj *pCmd; + int rx; + pCmd = Tcl_NewStringObj(pDb->zBindFallback, -1); + Tcl_IncrRefCount(pCmd); + Tcl_ListObjAppendElement(interp, pCmd, Tcl_NewStringObj(zVar,-1)); + if( needResultReset ) Tcl_ResetResult(interp); + needResultReset = 1; + rx = Tcl_EvalObjEx(interp, pCmd, TCL_EVAL_DIRECT); + Tcl_DecrRefCount(pCmd); + if( rx==TCL_OK ){ + pVar = Tcl_GetObjResult(interp); + }else if( rx==TCL_ERROR ){ + rc = TCL_ERROR; + break; + }else{ + pVar = 0; + } + } if( pVar ){ - int n; + Tcl_Size n; u8 *data; const char *zType = (pVar->typePtr ? pVar->typePtr->name : ""); c = zType[0]; @@ -1202,9 +1516,13 @@ static int dbPrepareAndBind( sqlite3_bind_blob(pStmt, i, data, n, SQLITE_STATIC); Tcl_IncrRefCount(pVar); pPreStmt->apParm[iParm++] = pVar; - }else if( c=='b' && strcmp(zType,"boolean")==0 ){ - Tcl_GetIntFromObj(interp, pVar, &n); - sqlite3_bind_int(pStmt, i, n); + }else if( c=='b' && pVar->bytes==0 + && (strcmp(zType,"booleanString")==0 + || strcmp(zType,"boolean")==0) + ){ + int nn; + Tcl_GetBooleanFromObj(interp, pVar, &nn); + sqlite3_bind_int(pStmt, i, nn); }else if( c=='d' && strcmp(zType,"double")==0 ){ double r; Tcl_GetDoubleFromObj(interp, pVar, &r); @@ -1216,19 +1534,22 @@ static int dbPrepareAndBind( sqlite3_bind_int64(pStmt, i, v); }else{ data = (unsigned char *)Tcl_GetStringFromObj(pVar, &n); - sqlite3_bind_text(pStmt, i, (char *)data, n, SQLITE_STATIC); + sqlite3_bind_text64(pStmt, i, (char *)data, n, SQLITE_STATIC, + SQLITE_UTF8); Tcl_IncrRefCount(pVar); pPreStmt->apParm[iParm++] = pVar; } }else{ sqlite3_bind_null(pStmt, i); } + if( needResultReset ) Tcl_ResetResult(pDb->interp); } } pPreStmt->nParm = iParm; *ppPreStmt = pPreStmt; + if( needResultReset && rc==TCL_OK ) Tcl_ResetResult(pDb->interp); - return TCL_OK; + return rc; } /* @@ -1271,8 +1592,8 @@ static void dbReleaseStmt( assert( pDb->nStmt>0 ); } pDb->nStmt++; - - /* If we have too many statement in cache, remove the surplus from + + /* If we have too many statement in cache, remove the surplus from ** the end of the cache list. */ while( pDb->nStmt>pDb->maxStmt ){ SqlPreparedStmt *pLast = pDb->stmtLast; @@ -1300,10 +1621,14 @@ struct DbEvalContext { const char *zSql; /* Remaining SQL to execute */ SqlPreparedStmt *pPreStmt; /* Current statement */ int nCol; /* Number of columns returned by pStmt */ - Tcl_Obj *pArray; /* Name of array variable */ + int evalFlags; /* Flags used */ + Tcl_Obj *pVarName; /* Name of target array/dict variable */ Tcl_Obj **apColName; /* Array of column names */ }; +#define SQLITE_EVAL_WITHOUTNULLS 0x00001 /* Unset array(*) for NULL */ +#define SQLITE_EVAL_ASDICT 0x00002 /* Use dict instead of array */ + /* ** Release any cache of column names currently held as part of ** the DbEvalContext structure passed as the first argument. @@ -1323,30 +1648,33 @@ static void dbReleaseColumnNames(DbEvalContext *p){ /* ** Initialize a DbEvalContext structure. ** -** If pArray is not NULL, then it contains the name of a Tcl array +** If pVarName is not NULL, then it contains the name of a Tcl array ** variable. The "*" member of this array is set to a list containing ** the names of the columns returned by the statement as part of each -** call to dbEvalStep(), in order from left to right. e.g. if the names -** of the returned columns are a, b and c, it does the equivalent of the +** call to dbEvalStep(), in order from left to right. e.g. if the names +** of the returned columns are a, b and c, it does the equivalent of the ** tcl command: ** -** set ${pArray}(*) {a b c} +** set ${pVarName}(*) {a b c} */ static void dbEvalInit( DbEvalContext *p, /* Pointer to structure to initialize */ SqliteDb *pDb, /* Database handle */ Tcl_Obj *pSql, /* Object containing SQL script */ - Tcl_Obj *pArray /* Name of Tcl array to set (*) element of */ + Tcl_Obj *pVarName, /* Name of Tcl array to set (*) element of */ + int evalFlags /* Flags controlling evaluation */ ){ memset(p, 0, sizeof(DbEvalContext)); p->pDb = pDb; p->zSql = Tcl_GetString(pSql); p->pSql = pSql; Tcl_IncrRefCount(pSql); - if( pArray ){ - p->pArray = pArray; - Tcl_IncrRefCount(pArray); + if( pVarName ){ + p->pVarName = pVarName; + Tcl_IncrRefCount(pVarName); } + p->evalFlags = evalFlags; + addDatabaseRef(p->pDb); } /* @@ -1366,7 +1694,7 @@ static void dbEvalRowInfo( Tcl_Obj **apColName = 0; /* Array of column names */ p->nCol = nCol = sqlite3_column_count(pStmt); - if( nCol>0 && (papColName || p->pArray) ){ + if( nCol>0 && (papColName || p->pVarName) ){ apColName = (Tcl_Obj**)Tcl_Alloc( sizeof(Tcl_Obj*)*nCol ); for(i=0; i<nCol; i++){ apColName[i] = Tcl_NewStringObj(sqlite3_column_name(pStmt,i), -1); @@ -1375,20 +1703,35 @@ static void dbEvalRowInfo( p->apColName = apColName; } - /* If results are being stored in an array variable, then create - ** the array(*) entry for that array + /* If results are being stored in a variable then create the + ** array(*) or dict(*) entry for that variable. */ - if( p->pArray ){ + if( p->pVarName ){ Tcl_Interp *interp = p->pDb->interp; Tcl_Obj *pColList = Tcl_NewObj(); Tcl_Obj *pStar = Tcl_NewStringObj("*", -1); + Tcl_IncrRefCount(pColList); + Tcl_IncrRefCount(pStar); for(i=0; i<nCol; i++){ Tcl_ListObjAppendElement(interp, pColList, apColName[i]); } - Tcl_IncrRefCount(pStar); - Tcl_ObjSetVar2(interp, p->pArray, pStar, pColList, 0); + if( 0==(SQLITE_EVAL_ASDICT & p->evalFlags) ){ + Tcl_ObjSetVar2(interp, p->pVarName, pStar, pColList, 0); + }else{ + Tcl_Obj * pDict = Tcl_ObjGetVar2(interp, p->pVarName, NULL, 0); + if( !pDict ){ + pDict = Tcl_NewDictObj(); + }else if( Tcl_IsShared(pDict) ){ + pDict = Tcl_DuplicateObj(pDict); + } + if( Tcl_DictObjPut(interp, pDict, pStar, pColList)==TCL_OK ){ + Tcl_ObjSetVar2(interp, p->pVarName, NULL, pDict, 0); + } + Tcl_BounceRefCount(pDict); + } Tcl_DecrRefCount(pStar); + Tcl_DecrRefCount(pColList); } } @@ -1430,7 +1773,7 @@ static int dbEvalStep(DbEvalContext *p){ if( rcs==SQLITE_ROW ){ return TCL_OK; } - if( p->pArray ){ + if( p->pVarName ){ dbEvalRowInfo(p, 0, 0); } rcs = sqlite3_reset(pStmt); @@ -1438,6 +1781,7 @@ static int dbEvalStep(DbEvalContext *p){ pDb->nStep = sqlite3_stmt_status(pStmt,SQLITE_STMTSTATUS_FULLSCAN_STEP,1); pDb->nSort = sqlite3_stmt_status(pStmt,SQLITE_STMTSTATUS_SORT,1); pDb->nIndex = sqlite3_stmt_status(pStmt,SQLITE_STMTSTATUS_AUTOINDEX,1); + pDb->nVMStep = sqlite3_stmt_status(pStmt,SQLITE_STMTSTATUS_VM_STEP,1); dbReleaseColumnNames(p); p->pPreStmt = 0; @@ -1448,7 +1792,7 @@ static int dbEvalStep(DbEvalContext *p){ #if SQLITE_TEST if( p->pDb->bLegacyPrepare && rcs==SQLITE_SCHEMA && zPrevSql ){ /* If the runtime error was an SQLITE_SCHEMA, and the database - ** handle is configured to use the legacy sqlite3_prepare() + ** handle is configured to use the legacy sqlite3_prepare() ** interface, retry prepare()/step() on the same SQL statement. ** This only happens once. If there is a second SQLITE_SCHEMA ** error, the error will be returned to the caller. */ @@ -1480,12 +1824,13 @@ static void dbEvalFinalize(DbEvalContext *p){ dbReleaseStmt(p->pDb, p->pPreStmt, 0); p->pPreStmt = 0; } - if( p->pArray ){ - Tcl_DecrRefCount(p->pArray); - p->pArray = 0; + if( p->pVarName ){ + Tcl_DecrRefCount(p->pVarName); + p->pVarName = 0; } Tcl_DecrRefCount(p->pSql); dbReleaseColumnNames(p); + delDatabaseRef(p->pDb); } /* @@ -1523,12 +1868,13 @@ static Tcl_Obj *dbEvalColumnValue(DbEvalContext *p, int iCol){ /* ** If using Tcl version 8.6 or greater, use the NR functions to avoid -** recursive evalution of scripts by the [db eval] and [db trans] +** recursive evaluation of scripts by the [db eval] and [db trans] ** commands. Even if the headers used while compiling the extension ** are 8.6 or newer, the code still tests the Tcl version at runtime. ** This allows stubs-enabled builds to be used with older Tcl libraries. */ -#if TCL_MAJOR_VERSION>8 || (TCL_MAJOR_VERSION==8 && TCL_MINOR_VERSION>=6) +#if TCL_MAJOR_VERSION>8 || !defined(TCL_MINOR_VERSION) \ + || TCL_MINOR_VERSION>=6 # define SQLITE_TCL_NRE 1 static int DbUseNre(void){ int major, minor; @@ -1536,11 +1882,11 @@ static int DbUseNre(void){ return( (major==8 && minor>=6) || major>8 ); } #else -/* +/* ** Compiling using headers earlier than 8.6. In this case NR cannot be ** used, so DbUseNre() to always return zero. Add #defines for the other ** Tcl_NRxxx() functions to prevent them from causing compilation errors, -** even though the only invocations of them are within conditional blocks +** even though the only invocations of them are within conditional blocks ** of the form: ** ** if( DbUseNre() ) { ... } @@ -1555,9 +1901,9 @@ static int DbUseNre(void){ /* ** This function is part of the implementation of the command: ** -** $db eval SQL ?ARRAYNAME? SCRIPT +** $db eval SQL ?TGT-NAME? SCRIPT */ -static int DbEvalNextCmd( +static int SQLITE_TCLAPI DbEvalNextCmd( ClientData data[], /* data[0] is the (DbEvalContext*) */ Tcl_Interp *interp, /* Tcl interpreter */ int result /* Result so far */ @@ -1569,8 +1915,8 @@ static int DbEvalNextCmd( ** is a pointer to a Tcl_Obj containing the script to run for each row ** returned by the queries encapsulated in data[0]. */ DbEvalContext *p = (DbEvalContext *)data[0]; - Tcl_Obj *pScript = (Tcl_Obj *)data[1]; - Tcl_Obj *pArray = p->pArray; + Tcl_Obj * const pScript = (Tcl_Obj *)data[1]; + Tcl_Obj * const pVarName = p->pVarName; while( (rc==TCL_OK || rc==TCL_CONTINUE) && TCL_OK==(rc = dbEvalStep(p)) ){ int i; @@ -1578,19 +1924,54 @@ static int DbEvalNextCmd( Tcl_Obj **apColName; dbEvalRowInfo(p, &nCol, &apColName); for(i=0; i<nCol; i++){ - Tcl_Obj *pVal = dbEvalColumnValue(p, i); - if( pArray==0 ){ - Tcl_ObjSetVar2(interp, apColName[i], 0, pVal, 0); + if( pVarName==0 ){ + Tcl_ObjSetVar2(interp, apColName[i], 0, dbEvalColumnValue(p,i), 0); + }else if( (p->evalFlags & SQLITE_EVAL_WITHOUTNULLS)!=0 + && sqlite3_column_type(p->pPreStmt->pStmt, i)==SQLITE_NULL + ){ + /* Remove NULL-containing column from the target container... */ + if( 0==(SQLITE_EVAL_ASDICT & p->evalFlags) ){ + /* Target is an array */ + Tcl_UnsetVar2(interp, Tcl_GetString(pVarName), + Tcl_GetString(apColName[i]), 0); + }else{ + /* Target is a dict */ + Tcl_Obj *pDict = Tcl_ObjGetVar2(interp, pVarName, NULL, 0); + if( pDict ){ + if( Tcl_IsShared(pDict) ){ + pDict = Tcl_DuplicateObj(pDict); + } + if( Tcl_DictObjRemove(interp, pDict, apColName[i])==TCL_OK ){ + Tcl_ObjSetVar2(interp, pVarName, NULL, pDict, 0); + } + Tcl_BounceRefCount(pDict); + } + } + }else if( 0==(SQLITE_EVAL_ASDICT & p->evalFlags) ){ + /* Target is an array: set target(colName) = colValue */ + Tcl_ObjSetVar2(interp, pVarName, apColName[i], + dbEvalColumnValue(p,i), 0); }else{ - Tcl_ObjSetVar2(interp, pArray, apColName[i], pVal, 0); + /* Target is a dict: set target(colName) = colValue */ + Tcl_Obj *pDict = Tcl_ObjGetVar2(interp, pVarName, NULL, 0); + if( !pDict ){ + pDict = Tcl_NewDictObj(); + }else if( Tcl_IsShared(pDict) ){ + pDict = Tcl_DuplicateObj(pDict); + } + if( Tcl_DictObjPut(interp, pDict, apColName[i], + dbEvalColumnValue(p,i))==TCL_OK ){ + Tcl_ObjSetVar2(interp, pVarName, NULL, pDict, 0); + } + Tcl_BounceRefCount(pDict); } } - /* The required interpreter variables are now populated with the data + /* The required interpreter variables are now populated with the data ** from the current row. If using NRE, schedule callbacks to evaluate ** script pScript, then to invoke this function again to fetch the next ** row (or clean up if there is no next row or the script throws an - ** exception). After scheduling the callbacks, return control to the + ** exception). After scheduling the callbacks, return control to the ** caller. ** ** If not using NRE, evaluate pScript directly and continue with the @@ -1614,6 +1995,46 @@ static int DbEvalNextCmd( return rc; } +/* +** This function is used by the implementations of the following database +** handle sub-commands: +** +** $db update_hook ?SCRIPT? +** $db wal_hook ?SCRIPT? +** $db commit_hook ?SCRIPT? +** $db preupdate hook ?SCRIPT? +*/ +static void DbHookCmd( + Tcl_Interp *interp, /* Tcl interpreter */ + SqliteDb *pDb, /* Database handle */ + Tcl_Obj *pArg, /* SCRIPT argument (or NULL) */ + Tcl_Obj **ppHook /* Pointer to member of SqliteDb */ +){ + sqlite3 *db = pDb->db; + + if( *ppHook ){ + Tcl_SetObjResult(interp, *ppHook); + if( pArg ){ + Tcl_DecrRefCount(*ppHook); + *ppHook = 0; + } + } + if( pArg ){ + assert( !(*ppHook) ); + if( Tcl_GetString(pArg)[0] ){ + *ppHook = pArg; + Tcl_IncrRefCount(*ppHook); + } + } + +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK + sqlite3_preupdate_hook(db, (pDb->pPreUpdateHook?DbPreUpdateHandler:0), pDb); +#endif + sqlite3_update_hook(db, (pDb->pUpdateHook?DbUpdateHandler:0), pDb); + sqlite3_rollback_hook(db, (pDb->pRollbackHook?DbRollbackHandler:0), pDb); + sqlite3_wal_hook(db, (pDb->pWalHook?DbWalHandler:0), pDb); +} + /* ** The "sqlite" command below creates a new Tcl command for each ** connection it opens to an SQLite database. This routine is invoked @@ -1627,37 +2048,47 @@ static int DbEvalNextCmd( ** and calls that connection "db1". The second command causes this ** subroutine to be invoked. */ -static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ +static int SQLITE_TCLAPI DbObjCmd( + void *cd, + Tcl_Interp *interp, + int objc, + Tcl_Obj *const*objv +){ SqliteDb *pDb = (SqliteDb*)cd; int choice; int rc = TCL_OK; static const char *DB_strs[] = { - "authorizer", "backup", "busy", - "cache", "changes", "close", - "collate", "collation_needed", "commit_hook", - "complete", "copy", "enable_load_extension", - "errorcode", "eval", "exists", - "function", "incrblob", "interrupt", - "last_insert_rowid", "nullvalue", "onecolumn", - "profile", "progress", "rekey", - "restore", "rollback_hook", "status", - "timeout", "total_changes", "trace", - "transaction", "unlock_notify", "update_hook", - "version", "wal_hook", 0 + "authorizer", "backup", "bind_fallback", + "busy", "cache", "changes", + "close", "collate", "collation_needed", + "commit_hook", "complete", "config", + "copy", "deserialize", "enable_load_extension", + "errorcode", "erroroffset", "eval", + "exists", "function", "incrblob", + "interrupt", "last_insert_rowid", "nullvalue", + "onecolumn", "preupdate", "profile", + "progress", "rekey", "restore", + "rollback_hook", "serialize", "status", + "timeout", "total_changes", "trace", + "trace_v2", "transaction", "unlock_notify", + "update_hook", "version", "wal_hook", + 0 }; enum DB_enum { - DB_AUTHORIZER, DB_BACKUP, DB_BUSY, - DB_CACHE, DB_CHANGES, DB_CLOSE, - DB_COLLATE, DB_COLLATION_NEEDED, DB_COMMIT_HOOK, - DB_COMPLETE, DB_COPY, DB_ENABLE_LOAD_EXTENSION, - DB_ERRORCODE, DB_EVAL, DB_EXISTS, - DB_FUNCTION, DB_INCRBLOB, DB_INTERRUPT, - DB_LAST_INSERT_ROWID, DB_NULLVALUE, DB_ONECOLUMN, - DB_PROFILE, DB_PROGRESS, DB_REKEY, - DB_RESTORE, DB_ROLLBACK_HOOK, DB_STATUS, - DB_TIMEOUT, DB_TOTAL_CHANGES, DB_TRACE, - DB_TRANSACTION, DB_UNLOCK_NOTIFY, DB_UPDATE_HOOK, - DB_VERSION, DB_WAL_HOOK + DB_AUTHORIZER, DB_BACKUP, DB_BIND_FALLBACK, + DB_BUSY, DB_CACHE, DB_CHANGES, + DB_CLOSE, DB_COLLATE, DB_COLLATION_NEEDED, + DB_COMMIT_HOOK, DB_COMPLETE, DB_CONFIG, + DB_COPY, DB_DESERIALIZE, DB_ENABLE_LOAD_EXTENSION, + DB_ERRORCODE, DB_ERROROFFSET, DB_EVAL, + DB_EXISTS, DB_FUNCTION, DB_INCRBLOB, + DB_INTERRUPT, DB_LAST_INSERT_ROWID, DB_NULLVALUE, + DB_ONECOLUMN, DB_PREUPDATE, DB_PROFILE, + DB_PROGRESS, DB_REKEY, DB_RESTORE, + DB_ROLLBACK_HOOK, DB_SERIALIZE, DB_STATUS, + DB_TIMEOUT, DB_TOTAL_CHANGES, DB_TRACE, + DB_TRACE_V2, DB_TRANSACTION, DB_UNLOCK_NOTIFY, + DB_UPDATE_HOOK, DB_VERSION, DB_WAL_HOOK, }; /* don't leave trailing commas on DB_enum, it confuses the AIX xlc compiler */ @@ -1683,7 +2114,7 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ ** (4) Name of the database (ex: "main", "temp") ** (5) Name of trigger that is doing the access ** - ** The callback should return on of the following strings: SQLITE_OK, + ** The callback should return one of the following strings: SQLITE_OK, ** SQLITE_IGNORE, or SQLITE_DENY. Any other return value is an error. ** ** If this method is invoked with no arguments, the current authorization @@ -1704,7 +2135,7 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ } }else{ char *zAuth; - int len; + Tcl_Size len; if( pDb->zAuth ){ Tcl_Free(pDb->zAuth); } @@ -1779,6 +2210,49 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ break; } + /* $db bind_fallback ?CALLBACK? + ** + ** When resolving bind parameters in an SQL statement, if the parameter + ** cannot be associated with a TCL variable then invoke CALLBACK with a + ** single argument that is the name of the parameter and use the return + ** value of the CALLBACK as the binding. If CALLBACK returns something + ** other than TCL_OK or TCL_ERROR then bind a NULL. + ** + ** If CALLBACK is an empty string, then revert to the default behavior + ** which is to set the binding to NULL. + ** + ** If CALLBACK returns an error, that causes the statement execution to + ** abort. Hence, to configure a connection so that it throws an error + ** on an attempt to bind an unknown variable, do something like this: + ** + ** proc bind_error {name} {error "no such variable: $name"} + ** db bind_fallback bind_error + */ + case DB_BIND_FALLBACK: { + if( objc>3 ){ + Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?"); + return TCL_ERROR; + }else if( objc==2 ){ + if( pDb->zBindFallback ){ + Tcl_AppendResult(interp, pDb->zBindFallback, (char*)0); + } + }else{ + char *zCallback; + Tcl_Size len; + if( pDb->zBindFallback ){ + Tcl_Free(pDb->zBindFallback); + } + zCallback = Tcl_GetStringFromObj(objv[2], &len); + if( zCallback && len>0 ){ + pDb->zBindFallback = Tcl_Alloc( len + 1 ); + memcpy(pDb->zBindFallback, zCallback, len+1); + }else{ + pDb->zBindFallback = 0; + } + } + break; + } + /* $db busy ?CALLBACK? ** ** Invoke the given callback if an SQL statement attempts to open @@ -1794,7 +2268,7 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ } }else{ char *zBusy; - int len; + Tcl_Size len; if( pDb->zBusy ){ Tcl_Free(pDb->zBusy); } @@ -1843,7 +2317,7 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ return TCL_ERROR; }else{ if( TCL_ERROR==Tcl_GetIntFromObj(interp, objv[3], &n) ){ - Tcl_AppendResult( interp, "cannot convert \"", + Tcl_AppendResult( interp, "cannot convert \"", Tcl_GetStringFromObj(objv[3],0), "\" to integer", (char*)0); return TCL_ERROR; }else{ @@ -1857,7 +2331,7 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ } } }else{ - Tcl_AppendResult( interp, "bad option \"", + Tcl_AppendResult( interp, "bad option \"", Tcl_GetStringFromObj(objv[2],0), "\": must be flush or size", (char*)0); return TCL_ERROR; @@ -1868,7 +2342,7 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ /* $db changes ** ** Return the number of rows that were modified, inserted, or deleted by - ** the most recent INSERT, UPDATE or DELETE statement, not including + ** the most recent INSERT, UPDATE or DELETE statement, not including ** any changes made by trigger programs. */ case DB_CHANGES: { @@ -1878,7 +2352,7 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ return TCL_ERROR; } pResult = Tcl_GetObjResult(interp); - Tcl_SetIntObj(pResult, sqlite3_changes(pDb->db)); + Tcl_SetWideIntObj(pResult, sqlite3_changes64(pDb->db)); break; } @@ -1901,7 +2375,7 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ SqlCollate *pCollate; char *zName; char *zScript; - int nScript; + Tcl_Size nScript; if( objc!=4 ){ Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT"); return TCL_ERROR; @@ -1915,7 +2389,7 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ pCollate->zScript = (char*)&pCollate[1]; pDb->pCollate = pCollate; memcpy(pCollate->zScript, zScript, nScript+1); - if( sqlite3_create_collation(pDb->db, zName, SQLITE_UTF8, + if( sqlite3_create_collation(pDb->db, zName, SQLITE_UTF8, pCollate, tclSqlCollate) ){ Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE); return TCL_ERROR; @@ -1960,7 +2434,7 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ } }else{ const char *zCommit; - int len; + Tcl_Size len; if( pDb->zCommit ){ Tcl_Free(pDb->zCommit); } @@ -2002,6 +2476,76 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ break; } + /* $db config ?OPTION? ?BOOLEAN? + ** + ** Configure the database connection using the sqlite3_db_config() + ** interface. + */ + case DB_CONFIG: { + static const struct DbConfigChoices { + const char *zName; + int op; + } aDbConfig[] = { + { "defensive", SQLITE_DBCONFIG_DEFENSIVE }, + { "dqs_ddl", SQLITE_DBCONFIG_DQS_DDL }, + { "dqs_dml", SQLITE_DBCONFIG_DQS_DML }, + { "enable_fkey", SQLITE_DBCONFIG_ENABLE_FKEY }, + { "enable_qpsg", SQLITE_DBCONFIG_ENABLE_QPSG }, + { "enable_trigger", SQLITE_DBCONFIG_ENABLE_TRIGGER }, + { "enable_view", SQLITE_DBCONFIG_ENABLE_VIEW }, + { "fts3_tokenizer", SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER }, + { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE }, + { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT }, + { "load_extension", SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION }, + { "no_ckpt_on_close", SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE }, + { "reset_database", SQLITE_DBCONFIG_RESET_DATABASE }, + { "trigger_eqp", SQLITE_DBCONFIG_TRIGGER_EQP }, + { "trusted_schema", SQLITE_DBCONFIG_TRUSTED_SCHEMA }, + { "writable_schema", SQLITE_DBCONFIG_WRITABLE_SCHEMA }, + }; + Tcl_Obj *pResult; + int ii; + if( objc>4 ){ + Tcl_WrongNumArgs(interp, 2, objv, "?OPTION? ?BOOLEAN?"); + return TCL_ERROR; + } + if( objc==2 ){ + /* With no arguments, list all configuration options and with the + ** current value */ + pResult = Tcl_NewListObj(0,0); + for(ii=0; ii<sizeof(aDbConfig)/sizeof(aDbConfig[0]); ii++){ + int v = 0; + sqlite3_db_config(pDb->db, aDbConfig[ii].op, -1, &v); + Tcl_ListObjAppendElement(interp, pResult, + Tcl_NewStringObj(aDbConfig[ii].zName,-1)); + Tcl_ListObjAppendElement(interp, pResult, + Tcl_NewIntObj(v)); + } + }else{ + const char *zOpt = Tcl_GetString(objv[2]); + int onoff = -1; + int v = 0; + if( zOpt[0]=='-' ) zOpt++; + for(ii=0; ii<sizeof(aDbConfig)/sizeof(aDbConfig[0]); ii++){ + if( strcmp(aDbConfig[ii].zName, zOpt)==0 ) break; + } + if( ii>=sizeof(aDbConfig)/sizeof(aDbConfig[0]) ){ + Tcl_AppendResult(interp, "unknown config option: \"", zOpt, + "\"", (void*)0); + return TCL_ERROR; + } + if( objc==4 ){ + if( Tcl_GetBooleanFromObj(interp, objv[3], &onoff) ){ + return TCL_ERROR; + } + } + sqlite3_db_config(pDb->db, aDbConfig[ii].op, onoff, &v); + pResult = Tcl_NewIntObj(v); + } + Tcl_SetObjResult(interp, pResult); + break; + } + /* $db copy conflict-algorithm table filename ?SEPARATOR? ?NULLINDICATOR? ** ** Copy data into table from filename, optionally using SEPARATOR @@ -2017,7 +2561,7 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ ** ** This command usage is equivalent to the sqlite2.x COPY statement, ** which imports file data into a table using the PostgreSQL COPY file format: - ** $db copy $conflit_algo $table_name $filename \t \\N + ** $db copy $conflict_algorithm $table_name $filename \t \\N */ case DB_COPY: { char *zTable; /* Insert data into this table */ @@ -2033,15 +2577,16 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ char *zLine; /* A single line of input from the file */ char **azCol; /* zLine[] broken up into columns */ const char *zCommit; /* How to commit changes */ - FILE *in; /* The input file */ + Tcl_Channel in; /* The input file */ int lineno = 0; /* Line number of input file */ char zLineNum[80]; /* Line number print buffer */ + Tcl_Obj *str; Tcl_Obj *pResult; /* interp result */ const char *zSep; const char *zNull; if( objc<5 || objc>7 ){ - Tcl_WrongNumArgs(interp, 2, objv, + Tcl_WrongNumArgs(interp, 2, objv, "CONFLICT-ALGORITHM TABLE FILENAME ?SEPARATOR? ?NULLINDICATOR?"); return TCL_ERROR; } @@ -2070,7 +2615,7 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ strcmp(zConflict, "fail" ) != 0 && strcmp(zConflict, "ignore" ) != 0 && strcmp(zConflict, "replace" ) != 0 ) { - Tcl_AppendResult(interp, "Error: \"", zConflict, + Tcl_AppendResult(interp, "Error: \"", zConflict, "\", conflict-algorithm must be one of: rollback, " "abort, fail, ignore, or replace", (char*)0); return TCL_ERROR; @@ -2114,23 +2659,27 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ sqlite3_finalize(pStmt); return TCL_ERROR; } - in = fopen(zFile, "rb"); + in = Tcl_OpenFileChannel(interp, zFile, "rb", 0666); if( in==0 ){ - Tcl_AppendResult(interp, "Error: cannot open file: ", zFile, NULL); sqlite3_finalize(pStmt); return TCL_ERROR; } + Tcl_SetChannelOption(NULL, in, "-translation", "auto"); azCol = malloc( sizeof(azCol[0])*(nCol+1) ); if( azCol==0 ) { Tcl_AppendResult(interp, "Error: can't malloc()", (char*)0); - fclose(in); + Tcl_Close(interp, in); return TCL_ERROR; } + str = Tcl_NewObj(); + Tcl_IncrRefCount(str); (void)sqlite3_exec(pDb->db, "BEGIN", 0, 0, 0); zCommit = "COMMIT"; - while( (zLine = local_getline(0, in))!=0 ){ + while( Tcl_GetsObj(in, str)>=0 ) { char *z; + Tcl_Size byteLen; lineno++; + zLine = (char *)Tcl_GetByteArrayFromObj(str, &byteLen); azCol[0] = zLine; for(i=0, z=zLine; *z; z++){ if( *z==zSep[0] && strncmp(z, zSep, nSep)==0 ){ @@ -2159,7 +2708,7 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ for(i=0; i<nCol; i++){ /* check for null data, if so, bind as null */ if( (nNull>0 && strcmp(azCol[i], zNull)==0) - || strlen30(azCol[i])==0 + || strlen30(azCol[i])==0 ){ sqlite3_bind_null(pStmt, i+1); }else{ @@ -2168,15 +2717,16 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ } sqlite3_step(pStmt); rc = sqlite3_reset(pStmt); - free(zLine); + Tcl_SetObjLength(str, 0); if( rc!=SQLITE_OK ){ Tcl_AppendResult(interp,"Error: ", sqlite3_errmsg(pDb->db), (char*)0); zCommit = "ROLLBACK"; break; } } + Tcl_DecrRefCount(str); free(azCol); - fclose(in); + Tcl_Close(interp, in); sqlite3_finalize(pStmt); (void)sqlite3_exec(pDb->db, zCommit, 0, 0, 0); @@ -2195,6 +2745,83 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ break; } + /* + ** $db deserialize ?-maxsize N? ?-readonly BOOL? ?DATABASE? VALUE + ** + ** Reopen DATABASE (default "main") using the content in $VALUE + */ + case DB_DESERIALIZE: { +#ifdef SQLITE_OMIT_DESERIALIZE + Tcl_AppendResult(interp, "MEMDB not available in this build", + (char*)0); + rc = TCL_ERROR; +#else + const char *zSchema = 0; + Tcl_Obj *pValue = 0; + unsigned char *pBA; + unsigned char *pData; + Tcl_Size len; + int xrc; + sqlite3_int64 mxSize = 0; + int i; + int isReadonly = 0; + + + if( objc<3 ){ + Tcl_WrongNumArgs(interp, 2, objv, "?DATABASE? VALUE"); + rc = TCL_ERROR; + break; + } + for(i=2; i<objc-1; i++){ + const char *z = Tcl_GetString(objv[i]); + if( strcmp(z,"-maxsize")==0 && i<objc-2 ){ + Tcl_WideInt x; + rc = Tcl_GetWideIntFromObj(interp, objv[++i], &x); + if( rc ) goto deserialize_error; + mxSize = x; + continue; + } + if( strcmp(z,"-readonly")==0 && i<objc-2 ){ + rc = Tcl_GetBooleanFromObj(interp, objv[++i], &isReadonly); + if( rc ) goto deserialize_error; + continue; + } + if( zSchema==0 && i==objc-2 && z[0]!='-' ){ + zSchema = z; + continue; + } + Tcl_AppendResult(interp, "unknown option: ", z, (char*)0); + rc = TCL_ERROR; + goto deserialize_error; + } + pValue = objv[objc-1]; + pBA = Tcl_GetByteArrayFromObj(pValue, &len); + pData = sqlite3_malloc64( len ); + if( pData==0 && len>0 ){ + Tcl_AppendResult(interp, "out of memory", (char*)0); + rc = TCL_ERROR; + }else{ + int flags; + if( len>0 ) memcpy(pData, pBA, len); + if( isReadonly ){ + flags = SQLITE_DESERIALIZE_FREEONCLOSE | SQLITE_DESERIALIZE_READONLY; + }else{ + flags = SQLITE_DESERIALIZE_FREEONCLOSE | SQLITE_DESERIALIZE_RESIZEABLE; + } + xrc = sqlite3_deserialize(pDb->db, zSchema, pData, len, len, flags); + if( xrc ){ + Tcl_AppendResult(interp, "unable to set MEMDB content", (char*)0); + rc = TCL_ERROR; + } + if( mxSize>0 ){ + sqlite3_file_control(pDb->db, zSchema,SQLITE_FCNTL_SIZE_LIMIT,&mxSize); + } + } +deserialize_error: +#endif + break; + } + /* ** $db enable_load_extension BOOLEAN ** @@ -2231,6 +2858,17 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ break; } + /* + ** $db erroroffset + ** + ** Return the numeric error code that was returned by the most recent + ** call to sqlite3_exec(). + */ + case DB_ERROROFFSET: { + Tcl_SetObjResult(interp, Tcl_NewIntObj(sqlite3_error_offset(pDb->db))); + break; + } + /* ** $db exists $sql ** $db onecolumn $sql @@ -2238,45 +2876,64 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ ** The onecolumn method is the equivalent of: ** lindex [$db eval $sql] 0 */ - case DB_EXISTS: + case DB_EXISTS: case DB_ONECOLUMN: { + Tcl_Obj *pResult = 0; DbEvalContext sEval; if( objc!=3 ){ Tcl_WrongNumArgs(interp, 2, objv, "SQL"); return TCL_ERROR; } - dbEvalInit(&sEval, pDb, objv[2], 0); + dbEvalInit(&sEval, pDb, objv[2], 0, 0); rc = dbEvalStep(&sEval); if( choice==DB_ONECOLUMN ){ if( rc==TCL_OK ){ - Tcl_SetObjResult(interp, dbEvalColumnValue(&sEval, 0)); + pResult = dbEvalColumnValue(&sEval, 0); }else if( rc==TCL_BREAK ){ Tcl_ResetResult(interp); } }else if( rc==TCL_BREAK || rc==TCL_OK ){ - Tcl_SetObjResult(interp, Tcl_NewBooleanObj(rc==TCL_OK)); + pResult = Tcl_NewBooleanObj(rc==TCL_OK); } dbEvalFinalize(&sEval); + if( pResult ) Tcl_SetObjResult(interp, pResult); if( rc==TCL_BREAK ){ rc = TCL_OK; } break; } - + /* - ** $db eval $sql ?array? ?{ ...code... }? + ** $db eval ?options? $sql ?varName? ?{ ...code... }? ** - ** The SQL statement in $sql is evaluated. For each row, the values are - ** placed in elements of the array named "array" and ...code... is executed. - ** If "array" and "code" are omitted, then no callback is every invoked. - ** If "array" is an empty string, then the values are placed in variables - ** that have the same name as the fields extracted by the query. + ** The SQL statement in $sql is evaluated. For each row, the values + ** are placed in elements of the array or dict named $varName and + ** ...code... is executed. If $varName and $code are omitted, then + ** no callback is ever invoked. If $varName is an empty string, + ** then the values are placed in variables that have the same name + ** as the fields extracted by the query, and those variables are + ** accessible during the eval of $code. */ case DB_EVAL: { + int evalFlags = 0; + const char *zOpt; + while( objc>3 && (zOpt = Tcl_GetString(objv[2]))!=0 && zOpt[0]=='-' ){ + if( strcmp(zOpt, "-withoutnulls")==0 ){ + evalFlags |= SQLITE_EVAL_WITHOUTNULLS; + }else if( strcmp(zOpt, "-asdict")==0 ){ + evalFlags |= SQLITE_EVAL_ASDICT; + }else{ + Tcl_AppendResult(interp, "unknown option: \"", zOpt, "\"", (void*)0); + return TCL_ERROR; + } + objc--; + objv++; + } if( objc<3 || objc>5 ){ - Tcl_WrongNumArgs(interp, 2, objv, "SQL ?ARRAY-NAME? ?SCRIPT?"); + Tcl_WrongNumArgs(interp, 2, objv, + "?OPTIONS? SQL ?VAR-NAME? ?SCRIPT?"); return TCL_ERROR; } @@ -2284,7 +2941,7 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ DbEvalContext sEval; Tcl_Obj *pRet = Tcl_NewObj(); Tcl_IncrRefCount(pRet); - dbEvalInit(&sEval, pDb, objv[2], 0); + dbEvalInit(&sEval, pDb, objv[2], 0, 0); while( TCL_OK==(rc = dbEvalStep(&sEval)) ){ int i; int nCol; @@ -2302,17 +2959,17 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ }else{ ClientData cd2[2]; DbEvalContext *p; - Tcl_Obj *pArray = 0; + Tcl_Obj *pVarName = 0; Tcl_Obj *pScript; - if( objc==5 && *(char *)Tcl_GetString(objv[3]) ){ - pArray = objv[3]; + if( objc>=5 && *(char *)Tcl_GetString(objv[3]) ){ + pVarName = objv[3]; } pScript = objv[objc-1]; Tcl_IncrRefCount(pScript); - + p = (DbEvalContext *)Tcl_Alloc(sizeof(DbEvalContext)); - dbEvalInit(p, pDb, objv[2], pArray); + dbEvalInit(p, pDb, objv[2], pVarName, evalFlags); cd2[0] = (void *)p; cd2[1] = (void *)pScript; @@ -2322,10 +2979,17 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ } /* - ** $db function NAME [-argcount N] [-deterministic] SCRIPT + ** $db function NAME [OPTIONS] SCRIPT ** ** Create a new SQL function called NAME. Whenever that function is ** called, invoke SCRIPT to evaluate the function. + ** + ** Options: + ** --argcount N Function has exactly N arguments + ** --deterministic The function is pure + ** --directonly Prohibit use inside triggers and views + ** --innocuous Has no side effects or information leaks + ** --returntype TYPE Specify the return type of the function */ case DB_FUNCTION: { int flags = SQLITE_UTF8; @@ -2334,6 +2998,7 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ char *zName; int nArg = -1; int i; + int eType = SQLITE_NULL; if( objc<4 ){ Tcl_WrongNumArgs(interp, 2, objv, "NAME ?SWITCHES? SCRIPT"); return TCL_ERROR; @@ -2341,9 +3006,9 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ for(i=3; i<(objc-1); i++){ const char *z = Tcl_GetString(objv[i]); int n = strlen30(z); - if( n>2 && strncmp(z, "-argcount",n)==0 ){ + if( n>1 && strncmp(z, "-argcount",n)==0 ){ if( i==(objc-2) ){ - Tcl_AppendResult(interp, "option requires an argument: ", z, 0); + Tcl_AppendResult(interp, "option requires an argument: ", z,(char*)0); return TCL_ERROR; } if( Tcl_GetIntFromObj(interp, objv[i+1], &nArg) ) return TCL_ERROR; @@ -2354,11 +3019,32 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ } i++; }else - if( n>2 && strncmp(z, "-deterministic",n)==0 ){ + if( n>1 && strncmp(z, "-deterministic",n)==0 ){ flags |= SQLITE_DETERMINISTIC; + }else + if( n>1 && strncmp(z, "-directonly",n)==0 ){ + flags |= SQLITE_DIRECTONLY; + }else + if( n>1 && strncmp(z, "-innocuous",n)==0 ){ + flags |= SQLITE_INNOCUOUS; + }else + if( n>1 && strncmp(z, "-returntype", n)==0 ){ + const char *azType[] = {"integer", "real", "text", "blob", "any", 0}; + assert( SQLITE_INTEGER==1 && SQLITE_FLOAT==2 && SQLITE_TEXT==3 ); + assert( SQLITE_BLOB==4 && SQLITE_NULL==5 ); + if( i==(objc-2) ){ + Tcl_AppendResult(interp, "option requires an argument: ", z,(char*)0); + return TCL_ERROR; + } + i++; + if( Tcl_GetIndexFromObj(interp, objv[i], azType, "type", 0, &eType) ){ + return TCL_ERROR; + } + eType++; }else{ - Tcl_AppendResult(interp, "bad option \"", z, - "\": must be -argcount or -deterministic", 0 + Tcl_AppendResult(interp, "bad option \"", z, + "\": must be -argcount, -deterministic, -directonly," + " -innocuous, or -returntype", (char*)0 ); return TCL_ERROR; } @@ -2373,7 +3059,8 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ } pFunc->pScript = pScript; Tcl_IncrRefCount(pScript); - pFunc->useEvalObjv = safeToUseEvalObjv(interp, pScript); + pFunc->useEvalObjv = safeToUseEvalObjv(pScript); + pFunc->eType = eType; rc = sqlite3_create_function(pDb->db, zName, nArg, flags, pFunc, tclSqlFunc, 0, 0); if( rc!=SQLITE_OK ){ @@ -2408,7 +3095,7 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ } if( objc==(6+isReadonly) ){ - zDb = Tcl_GetString(objv[2]); + zDb = Tcl_GetString(objv[2+isReadonly]); } zTable = Tcl_GetString(objv[objc-3]); zColumn = Tcl_GetString(objv[objc-2]); @@ -2448,7 +3135,7 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ return TCL_ERROR; } if( objc==3 ){ - int len; + Tcl_Size len; char *zNull = Tcl_GetStringFromObj(objv[2], &len); if( pDb->zNull ){ Tcl_Free(pDb->zNull); @@ -2466,7 +3153,7 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ } /* - ** $db last_insert_rowid + ** $db last_insert_rowid ** ** Return an integer which is the ROWID for the most recent insert. */ @@ -2488,7 +3175,7 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ */ /* $db progress ?N CALLBACK? - ** + ** ** Invoke the given callback every N virtual machine opcodes while executing ** queries. */ @@ -2497,9 +3184,12 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ if( pDb->zProgress ){ Tcl_AppendResult(interp, pDb->zProgress, (char*)0); } +#ifndef SQLITE_OMIT_PROGRESS_CALLBACK + sqlite3_progress_handler(pDb->db, 0, 0, 0); +#endif }else if( objc==4 ){ char *zProgress; - int len; + Tcl_Size len; int N; if( TCL_OK!=Tcl_GetIntFromObj(interp, objv[2], &N) ){ return TCL_ERROR; @@ -2545,7 +3235,7 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ } }else{ char *zProfile; - int len; + Tcl_Size len; if( pDb->zProfile ){ Tcl_Free(pDb->zProfile); } @@ -2556,7 +3246,8 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ }else{ pDb->zProfile = 0; } -#if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) +#if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) && \ + !defined(SQLITE_OMIT_DEPRECATED) if( pDb->zProfile ){ pDb->interp = interp; sqlite3_profile(pDb->db, DbProfileHandler, pDb); @@ -2574,15 +3265,18 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ ** Change the encryption key on the currently open database. */ case DB_REKEY: { -#ifdef SQLITE_HAS_CODEC +/* BEGIN SQLCIPHER */ +#if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL) int nKey; void *pKey; #endif +/* END SQLCIPHER */ if( objc!=3 ){ Tcl_WrongNumArgs(interp, 2, objv, "KEY"); return TCL_ERROR; } -#ifdef SQLITE_HAS_CODEC +/* BEGIN SQLCIPHER */ +#if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL) pKey = Tcl_GetByteArrayFromObj(objv[2], &nKey); rc = sqlite3_rekey(pDb->db, pKey, nKey); if( rc ){ @@ -2590,12 +3284,13 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ rc = TCL_ERROR; } #endif +/* END SQLCIPHER */ break; } /* $db restore ?DATABASE? FILENAME ** - ** Open a database file named FILENAME. Transfer the content + ** Open a database file named FILENAME. Transfer the content ** of FILENAME into the local database DATABASE (default: "main"). */ case DB_RESTORE: { @@ -2654,9 +3349,42 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ } /* - ** $db status (step|sort|autoindex) + ** $db serialize ?DATABASE? ** - ** Display SQLITE_STMTSTATUS_FULLSCAN_STEP or + ** Return a serialization of a database. + */ + case DB_SERIALIZE: { +#ifdef SQLITE_OMIT_DESERIALIZE + Tcl_AppendResult(interp, "MEMDB not available in this build", + (char*)0); + rc = TCL_ERROR; +#else + const char *zSchema = objc>=3 ? Tcl_GetString(objv[2]) : "main"; + sqlite3_int64 sz = 0; + unsigned char *pData; + if( objc!=2 && objc!=3 ){ + Tcl_WrongNumArgs(interp, 2, objv, "?DATABASE?"); + rc = TCL_ERROR; + }else{ + int needFree; + pData = sqlite3_serialize(pDb->db, zSchema, &sz, SQLITE_SERIALIZE_NOCOPY); + if( pData ){ + needFree = 0; + }else{ + pData = sqlite3_serialize(pDb->db, zSchema, &sz, 0); + needFree = 1; + } + Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(pData,sz)); + if( needFree ) sqlite3_free(pData); + } +#endif + break; + } + + /* + ** $db status (step|sort|autoindex|vmstep) + ** + ** Display SQLITE_STMTSTATUS_FULLSCAN_STEP or ** SQLITE_STMTSTATUS_SORT for the most recent eval. */ case DB_STATUS: { @@ -2673,16 +3401,18 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ v = pDb->nSort; }else if( strcmp(zOp, "autoindex")==0 ){ v = pDb->nIndex; + }else if( strcmp(zOp, "vmstep")==0 ){ + v = pDb->nVMStep; }else{ - Tcl_AppendResult(interp, - "bad argument: should be autoindex, step, or sort", + Tcl_AppendResult(interp, + "bad argument: should be autoindex, step, sort or vmstep", (char*)0); return TCL_ERROR; } Tcl_SetObjResult(interp, Tcl_NewIntObj(v)); break; } - + /* ** $db timeout MILLESECONDS ** @@ -2698,11 +3428,11 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ sqlite3_busy_timeout(pDb->db, ms); break; } - + /* ** $db total_changes ** - ** Return the number of rows that were modified, inserted, or deleted + ** Return the number of rows that were modified, inserted, or deleted ** since the database handle was created. */ case DB_TOTAL_CHANGES: { @@ -2712,7 +3442,7 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ return TCL_ERROR; } pResult = Tcl_GetObjResult(interp); - Tcl_SetIntObj(pResult, sqlite3_total_changes(pDb->db)); + Tcl_SetWideIntObj(pResult, sqlite3_total_changes64(pDb->db)); break; } @@ -2732,7 +3462,7 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ } }else{ char *zTrace; - int len; + Tcl_Size len; if( pDb->zTrace ){ Tcl_Free(pDb->zTrace); } @@ -2743,7 +3473,8 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ }else{ pDb->zTrace = 0; } -#if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) +#if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) && \ + !defined(SQLITE_OMIT_DEPRECATED) if( pDb->zTrace ){ pDb->interp = interp; sqlite3_trace(pDb->db, DbTraceHandler, pDb); @@ -2755,12 +3486,94 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ break; } + /* $db trace_v2 ?CALLBACK? ?MASK? + ** + ** Make arrangements to invoke the CALLBACK routine for each trace event + ** matching the mask that is generated. The parameters are appended to + ** CALLBACK before it is executed. + */ + case DB_TRACE_V2: { + if( objc>4 ){ + Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK? ?MASK?"); + return TCL_ERROR; + }else if( objc==2 ){ + if( pDb->zTraceV2 ){ + Tcl_AppendResult(interp, pDb->zTraceV2, (char*)0); + } + }else{ + char *zTraceV2; + Tcl_Size len; + Tcl_WideInt wMask = 0; + if( objc==4 ){ + static const char *TTYPE_strs[] = { + "statement", "profile", "row", "close", 0 + }; + enum TTYPE_enum { + TTYPE_STMT, TTYPE_PROFILE, TTYPE_ROW, TTYPE_CLOSE + }; + Tcl_Size i; + if( TCL_OK!=Tcl_ListObjLength(interp, objv[3], &len) ){ + return TCL_ERROR; + } + for(i=0; i<len; i++){ + Tcl_Obj *pObj; + int ttype; + if( TCL_OK!=Tcl_ListObjIndex(interp, objv[3], i, &pObj) ){ + return TCL_ERROR; + } + if( Tcl_GetIndexFromObj(interp, pObj, TTYPE_strs, "trace type", + 0, &ttype)!=TCL_OK ){ + Tcl_WideInt wType; + Tcl_Obj *pError = Tcl_DuplicateObj(Tcl_GetObjResult(interp)); + Tcl_IncrRefCount(pError); + if( TCL_OK==Tcl_GetWideIntFromObj(interp, pObj, &wType) ){ + Tcl_DecrRefCount(pError); + wMask |= wType; + }else{ + Tcl_SetObjResult(interp, pError); + Tcl_DecrRefCount(pError); + return TCL_ERROR; + } + }else{ + switch( (enum TTYPE_enum)ttype ){ + case TTYPE_STMT: wMask |= SQLITE_TRACE_STMT; break; + case TTYPE_PROFILE: wMask |= SQLITE_TRACE_PROFILE; break; + case TTYPE_ROW: wMask |= SQLITE_TRACE_ROW; break; + case TTYPE_CLOSE: wMask |= SQLITE_TRACE_CLOSE; break; + } + } + } + }else{ + wMask = SQLITE_TRACE_STMT; /* use the "legacy" default */ + } + if( pDb->zTraceV2 ){ + Tcl_Free(pDb->zTraceV2); + } + zTraceV2 = Tcl_GetStringFromObj(objv[2], &len); + if( zTraceV2 && len>0 ){ + pDb->zTraceV2 = Tcl_Alloc( len + 1 ); + memcpy(pDb->zTraceV2, zTraceV2, len+1); + }else{ + pDb->zTraceV2 = 0; + } +#if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) + if( pDb->zTraceV2 ){ + pDb->interp = interp; + sqlite3_trace_v2(pDb->db, (unsigned)wMask, DbTraceV2Handler, pDb); + }else{ + sqlite3_trace_v2(pDb->db, 0, 0, 0); + } +#endif + } + break; + } + /* $db transaction [-deferred|-immediate|-exclusive] SCRIPT ** ** Start a new transaction (if we are not already in the midst of a ** transaction) and execute the TCL script SCRIPT. After SCRIPT ** completes, either commit the transaction or roll it back if SCRIPT - ** throws an exception. Or if no new transation was started, do nothing. + ** throws an exception. Or if no new transaction was started, do nothing. ** pass the exception on up the stack. ** ** This command was inspired by Dave Thomas's talk on Ruby at the @@ -2807,8 +3620,9 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ /* If using NRE, schedule a callback to invoke the script pScript, then ** a second callback to commit (or rollback) the transaction or savepoint ** opened above. If not using NRE, evaluate the script directly, then - ** call function DbTransPostCmd() to commit (or rollback) the transaction + ** call function DbTransPostCmd() to commit (or rollback) the transaction ** or savepoint. */ + addDatabaseRef(pDb); /* DbTransPostCmd() calls delDatabaseRef() */ if( DbUseNre() ){ Tcl_NRAddCallback(interp, DbTransPostCmd, cd, 0, 0, 0); (void)Tcl_NREvalObj(interp, pScript, 0); @@ -2838,14 +3652,14 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ Tcl_DecrRefCount(pDb->pUnlockNotify); pDb->pUnlockNotify = 0; } - + if( objc==3 ){ xNotify = DbUnlockNotify; pNotifyArg = (void *)pDb; pDb->pUnlockNotify = objv[2]; Tcl_IncrRefCount(pDb->pUnlockNotify); } - + if( sqlite3_unlock_notify(pDb->db, xNotify, pNotifyArg) ){ Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), (char*)0); rc = TCL_ERROR; @@ -2855,50 +3669,112 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ break; } + /* + ** $db preupdate_hook count + ** $db preupdate_hook hook ?SCRIPT? + ** $db preupdate_hook new INDEX + ** $db preupdate_hook old INDEX + */ + case DB_PREUPDATE: { +#ifndef SQLITE_ENABLE_PREUPDATE_HOOK + Tcl_AppendResult(interp, "preupdate_hook was omitted at compile-time", + (char*)0); + rc = TCL_ERROR; +#else + static const char *azSub[] = {"count", "depth", "hook", "new", "old", 0}; + enum DbPreupdateSubCmd { + PRE_COUNT, PRE_DEPTH, PRE_HOOK, PRE_NEW, PRE_OLD + }; + int iSub; + + if( objc<3 ){ + Tcl_WrongNumArgs(interp, 2, objv, "SUB-COMMAND ?ARGS?"); + } + if( Tcl_GetIndexFromObj(interp, objv[2], azSub, "sub-command", 0, &iSub) ){ + return TCL_ERROR; + } + + switch( (enum DbPreupdateSubCmd)iSub ){ + case PRE_COUNT: { + int nCol = sqlite3_preupdate_count(pDb->db); + Tcl_SetObjResult(interp, Tcl_NewIntObj(nCol)); + break; + } + + case PRE_HOOK: { + if( objc>4 ){ + Tcl_WrongNumArgs(interp, 2, objv, "hook ?SCRIPT?"); + return TCL_ERROR; + } + DbHookCmd(interp, pDb, (objc==4 ? objv[3] : 0), &pDb->pPreUpdateHook); + break; + } + + case PRE_DEPTH: { + Tcl_Obj *pRet; + if( objc!=3 ){ + Tcl_WrongNumArgs(interp, 3, objv, ""); + return TCL_ERROR; + } + pRet = Tcl_NewIntObj(sqlite3_preupdate_depth(pDb->db)); + Tcl_SetObjResult(interp, pRet); + break; + } + + case PRE_NEW: + case PRE_OLD: { + int iIdx; + sqlite3_value *pValue; + if( objc!=4 ){ + Tcl_WrongNumArgs(interp, 3, objv, "INDEX"); + return TCL_ERROR; + } + if( Tcl_GetIntFromObj(interp, objv[3], &iIdx) ){ + return TCL_ERROR; + } + + if( iSub==PRE_OLD ){ + rc = sqlite3_preupdate_old(pDb->db, iIdx, &pValue); + }else{ + assert( iSub==PRE_NEW ); + rc = sqlite3_preupdate_new(pDb->db, iIdx, &pValue); + } + + if( rc==SQLITE_OK ){ + Tcl_Obj *pObj; + pObj = Tcl_NewStringObj((char*)sqlite3_value_text(pValue), -1); + Tcl_SetObjResult(interp, pObj); + }else{ + Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), (char*)0); + return TCL_ERROR; + } + } + } +#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ + break; + } + /* ** $db wal_hook ?script? ** $db update_hook ?script? ** $db rollback_hook ?script? */ - case DB_WAL_HOOK: - case DB_UPDATE_HOOK: + case DB_WAL_HOOK: + case DB_UPDATE_HOOK: case DB_ROLLBACK_HOOK: { - - /* set ppHook to point at pUpdateHook or pRollbackHook, depending on + /* set ppHook to point at pUpdateHook or pRollbackHook, depending on ** whether [$db update_hook] or [$db rollback_hook] was invoked. */ - Tcl_Obj **ppHook; - if( choice==DB_UPDATE_HOOK ){ - ppHook = &pDb->pUpdateHook; - }else if( choice==DB_WAL_HOOK ){ - ppHook = &pDb->pWalHook; - }else{ - ppHook = &pDb->pRollbackHook; - } - - if( objc!=2 && objc!=3 ){ + Tcl_Obj **ppHook = 0; + if( choice==DB_WAL_HOOK ) ppHook = &pDb->pWalHook; + if( choice==DB_UPDATE_HOOK ) ppHook = &pDb->pUpdateHook; + if( choice==DB_ROLLBACK_HOOK ) ppHook = &pDb->pRollbackHook; + if( objc>3 ){ Tcl_WrongNumArgs(interp, 2, objv, "?SCRIPT?"); return TCL_ERROR; } - if( *ppHook ){ - Tcl_SetObjResult(interp, *ppHook); - if( objc==3 ){ - Tcl_DecrRefCount(*ppHook); - *ppHook = 0; - } - } - if( objc==3 ){ - assert( !(*ppHook) ); - if( Tcl_GetCharLength(objv[2])>0 ){ - *ppHook = objv[2]; - Tcl_IncrRefCount(*ppHook); - } - } - - sqlite3_update_hook(pDb->db, (pDb->pUpdateHook?DbUpdateHandler:0), pDb); - sqlite3_rollback_hook(pDb->db,(pDb->pRollbackHook?DbRollbackHandler:0),pDb); - sqlite3_wal_hook(pDb->db,(pDb->pWalHook?DbWalHandler:0),pDb); + DbHookCmd(interp, pDb, (objc==3 ? objv[2] : 0), ppHook); break; } @@ -2907,7 +3783,42 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ ** Return the version string for this database. */ case DB_VERSION: { - Tcl_SetResult(interp, (char *)sqlite3_libversion(), TCL_STATIC); + int i; + for(i=2; i<objc; i++){ + const char *zArg = Tcl_GetString(objv[i]); + /* Optional arguments to $db version are used for testing purpose */ +#ifdef SQLITE_TEST + /* $db version -use-legacy-prepare BOOLEAN + ** + ** Turn the use of legacy sqlite3_prepare() on or off. + */ + if( strcmp(zArg, "-use-legacy-prepare")==0 && i+1<objc ){ + i++; + if( Tcl_GetBooleanFromObj(interp, objv[i], &pDb->bLegacyPrepare) ){ + return TCL_ERROR; + } + }else + + /* $db version -last-stmt-ptr + ** + ** Return a string which is a hex encoding of the pointer to the + ** most recent sqlite3_stmt in the statement cache. + */ + if( strcmp(zArg, "-last-stmt-ptr")==0 ){ + char zBuf[100]; + sqlite3_snprintf(sizeof(zBuf), zBuf, "%p", + pDb->stmtList ? pDb->stmtList->pStmt: 0); + Tcl_SetResult(interp, zBuf, TCL_VOLATILE); + }else +#endif /* SQLITE_TEST */ + { + Tcl_AppendResult(interp, "unknown argument: ", zArg, (char*)0); + return TCL_ERROR; + } + } + if( i==2 ){ + Tcl_SetResult(interp, (char *)sqlite3_libversion(), TCL_STATIC); + } break; } @@ -2921,7 +3832,7 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ ** Adaptor that provides an objCmd interface to the NRE-enabled ** interface implementation. */ -static int DbObjCmdAdaptor( +static int SQLITE_TCLAPI DbObjCmdAdaptor( void *cd, Tcl_Interp *interp, int objc, @@ -2931,9 +3842,31 @@ static int DbObjCmdAdaptor( } #endif /* SQLITE_TCL_NRE */ +/* +** Issue the usage message when the "sqlite3" command arguments are +** incorrect. +*/ +static int sqliteCmdUsage( + Tcl_Interp *interp, + Tcl_Obj *const*objv +){ + Tcl_WrongNumArgs(interp, 1, objv, + "HANDLE ?FILENAME? ?-vfs VFSNAME? ?-readonly BOOLEAN? ?-create BOOLEAN?" + " ?-nofollow BOOLEAN?" + " ?-nomutex BOOLEAN? ?-fullmutex BOOLEAN? ?-uri BOOLEAN?" +/* BEGIN SQLCIPHER */ +#if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL) + " ?-key CODECKEY?" +#endif +/* END SQLCIPHER */ + ); + return TCL_ERROR; +} + /* ** sqlite3 DBNAME FILENAME ?-vfs VFSNAME? ?-key KEY? ?-readonly BOOLEAN? ** ?-create BOOLEAN? ?-nomutex BOOLEAN? +** ?-nofollow BOOLEAN? ** ** This is the main Tcl command. When the "sqlite" Tcl command is ** invoked, this routine runs to process that command. @@ -2946,23 +3879,31 @@ static int DbObjCmdAdaptor( ** The second argument is the name of the database file. ** */ -static int DbMain(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ +static int SQLITE_TCLAPI DbMain( + void *cd, + Tcl_Interp *interp, + int objc, + Tcl_Obj *const*objv +){ SqliteDb *p; const char *zArg; char *zErrMsg; int i; - const char *zFile; + const char *zFile = 0; const char *zVfs = 0; int flags; + int bTranslateFileName = 1; Tcl_DString translatedFilename; -#ifdef SQLITE_HAS_CODEC +/* BEGIN SQLCIPHER */ +#if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL) void *pKey = 0; int nKey = 0; #endif +/* END SQLCIPHER */ int rc; /* In normal use, each TCL interpreter runs in a single thread. So - ** by default, we can turn of mutexing on SQLite database connections. + ** by default, we can turn off mutexing on SQLite database connections. ** However, for testing purposes it is useful to have mutexes turned ** on. So, by default, mutexes default off. But if compiled with ** SQLITE_TCL_DEFAULT_FULLMUTEX then mutexes default on. @@ -2973,6 +3914,7 @@ static int DbMain(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_NOMUTEX; #endif + if( objc==1 ) return sqliteCmdUsage(interp, objv); if( objc==2 ){ zArg = Tcl_GetStringFromObj(objv[1], 0); if( strcmp(zArg,"-version")==0 ){ @@ -2984,25 +3926,37 @@ static int DbMain(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ return TCL_OK; } if( strcmp(zArg,"-has-codec")==0 ){ -#ifdef SQLITE_HAS_CODEC +/* BEGIN SQLCIPHER */ +#if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL) Tcl_AppendResult(interp,"1",(char*)0); #else Tcl_AppendResult(interp,"0",(char*)0); #endif +/* END SQLCIPHER */ return TCL_OK; } + if( zArg[0]=='-' ) return sqliteCmdUsage(interp, objv); } - for(i=3; i+1<objc; i+=2){ + for(i=2; i<objc; i++){ zArg = Tcl_GetString(objv[i]); + if( zArg[0]!='-' ){ + if( zFile!=0 ) return sqliteCmdUsage(interp, objv); + zFile = zArg; + continue; + } + if( i==objc-1 ) return sqliteCmdUsage(interp, objv); + i++; if( strcmp(zArg,"-key")==0 ){ -#ifdef SQLITE_HAS_CODEC - pKey = Tcl_GetByteArrayFromObj(objv[i+1], &nKey); +/* BEGIN SQLCIPHER */ +#if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL) + pKey = Tcl_GetByteArrayFromObj(objv[i], &nKey); #endif +/* END SQLCIPHER */ }else if( strcmp(zArg, "-vfs")==0 ){ - zVfs = Tcl_GetString(objv[i+1]); + zVfs = Tcl_GetString(objv[i]); }else if( strcmp(zArg, "-readonly")==0 ){ int b; - if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR; + if( Tcl_GetBooleanFromObj(interp, objv[i], &b) ) return TCL_ERROR; if( b ){ flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); flags |= SQLITE_OPEN_READONLY; @@ -3012,15 +3966,23 @@ static int DbMain(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ } }else if( strcmp(zArg, "-create")==0 ){ int b; - if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR; + if( Tcl_GetBooleanFromObj(interp, objv[i], &b) ) return TCL_ERROR; if( b && (flags & SQLITE_OPEN_READONLY)==0 ){ flags |= SQLITE_OPEN_CREATE; }else{ flags &= ~SQLITE_OPEN_CREATE; } + }else if( strcmp(zArg, "-nofollow")==0 ){ + int b; + if( Tcl_GetBooleanFromObj(interp, objv[i], &b) ) return TCL_ERROR; + if( b ){ + flags |= SQLITE_OPEN_NOFOLLOW; + }else{ + flags &= ~SQLITE_OPEN_NOFOLLOW; + } }else if( strcmp(zArg, "-nomutex")==0 ){ int b; - if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR; + if( Tcl_GetBooleanFromObj(interp, objv[i], &b) ) return TCL_ERROR; if( b ){ flags |= SQLITE_OPEN_NOMUTEX; flags &= ~SQLITE_OPEN_FULLMUTEX; @@ -3029,7 +3991,7 @@ static int DbMain(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ } }else if( strcmp(zArg, "-fullmutex")==0 ){ int b; - if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR; + if( Tcl_GetBooleanFromObj(interp, objv[i], &b) ) return TCL_ERROR; if( b ){ flags |= SQLITE_OPEN_FULLMUTEX; flags &= ~SQLITE_OPEN_NOMUTEX; @@ -3038,38 +4000,32 @@ static int DbMain(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ } }else if( strcmp(zArg, "-uri")==0 ){ int b; - if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR; + if( Tcl_GetBooleanFromObj(interp, objv[i], &b) ) return TCL_ERROR; if( b ){ flags |= SQLITE_OPEN_URI; }else{ flags &= ~SQLITE_OPEN_URI; } + }else if( strcmp(zArg, "-translatefilename")==0 ){ + if( Tcl_GetBooleanFromObj(interp, objv[i], &bTranslateFileName) ){ + return TCL_ERROR; + } }else{ Tcl_AppendResult(interp, "unknown option: ", zArg, (char*)0); return TCL_ERROR; } } - if( objc<3 || (objc&1)!=1 ){ - Tcl_WrongNumArgs(interp, 1, objv, - "HANDLE FILENAME ?-vfs VFSNAME? ?-readonly BOOLEAN? ?-create BOOLEAN?" - " ?-nomutex BOOLEAN? ?-fullmutex BOOLEAN? ?-uri BOOLEAN?" -#ifdef SQLITE_HAS_CODEC - " ?-key CODECKEY?" -#endif - ); - return TCL_ERROR; - } zErrMsg = 0; p = (SqliteDb*)Tcl_Alloc( sizeof(*p) ); - if( p==0 ){ - Tcl_SetResult(interp, (char *)"malloc failed", TCL_STATIC); - return TCL_ERROR; - } memset(p, 0, sizeof(*p)); - zFile = Tcl_GetStringFromObj(objv[2], 0); - zFile = Tcl_TranslateFileName(interp, zFile, &translatedFilename); + if( zFile==0 ) zFile = ""; + if( bTranslateFileName ){ + zFile = Tcl_TranslateFileName(interp, zFile, &translatedFilename); + } rc = sqlite3_open_v2(zFile, &p->db, flags, zVfs); - Tcl_DStringFree(&translatedFilename); + if( bTranslateFileName ){ + Tcl_DStringFree(&translatedFilename); + } if( p->db ){ if( SQLITE_OK!=sqlite3_errcode(p->db) ){ zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(p->db)); @@ -3079,11 +4035,13 @@ static int DbMain(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ }else{ zErrMsg = sqlite3_mprintf("%s", sqlite3_errstr(rc)); } -#ifdef SQLITE_HAS_CODEC +/* BEGIN SQLCIPHER */ +#if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL) if( p->db ){ sqlite3_key(p->db, pKey, nKey); } #endif +/* END SQLCIPHER */ if( p->db==0 ){ Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE); Tcl_Free((char*)p); @@ -3100,6 +4058,7 @@ static int DbMain(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ }else{ Tcl_CreateObjCommand(interp, zArg, DbObjCmd, (char*)p, DbDeleteCmd); } + p->nRef = 1; return TCL_OK; } @@ -3133,7 +4092,7 @@ static int DbMain(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){ ** The EXTERN macros are required by TCL in order to work on windows. */ EXTERN int Sqlite3_Init(Tcl_Interp *interp){ - int rc = Tcl_InitStubs(interp, "8.4", 0) ? TCL_OK : TCL_ERROR; + int rc = Tcl_InitStubs(interp, "8.5-", 0) ? TCL_OK : TCL_ERROR; if( rc==TCL_OK ){ Tcl_CreateObjCommand(interp, "sqlite3", (Tcl_ObjCmdProc*)DbMain, 0, 0); #ifndef SQLITE_3_SUFFIX_ONLY @@ -3157,717 +4116,114 @@ EXTERN int Tclsqlite3_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } EXTERN int Sqlite3_SafeInit(Tcl_Interp *interp){ return TCL_ERROR; } EXTERN int Sqlite3_SafeUnload(Tcl_Interp *interp, int flags){return TCL_ERROR;} - - -#ifndef SQLITE_3_SUFFIX_ONLY -int Sqlite_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); } -int Tclsqlite_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); } -int Sqlite_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } -int Tclsqlite_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } -#endif - -#ifdef TCLSH -/***************************************************************************** -** All of the code that follows is used to build standalone TCL interpreters -** that are statically linked with SQLite. Enable these by compiling -** with -DTCLSH=n where n can be 1 or 2. An n of 1 generates a standard -** tclsh but with SQLite built in. An n of 2 generates the SQLite space -** analysis program. -*/ - -#if defined(SQLITE_TEST) || defined(SQLITE_TCLMD5) /* - * This code implements the MD5 message-digest algorithm. - * The algorithm is due to Ron Rivest. This code was - * written by Colin Plumb in 1993, no copyright is claimed. - * This code is in the public domain; do with it what you wish. - * - * Equivalent code is available from RSA Data Security, Inc. - * This code has been tested against that, and is equivalent, - * except that you don't need to include two pages of legalese - * with every copy. - * - * To compute the message digest of a chunk of bytes, declare an - * MD5Context structure, pass it to MD5Init, call MD5Update as - * needed on buffers full of bytes, and then call MD5Final, which - * will fill a supplied 16-byte array with the digest. - */ - -/* - * If compiled on a machine that doesn't have a 32-bit integer, - * you just set "uint32" to the appropriate datatype for an - * unsigned 32-bit integer. For example: - * - * cc -Duint32='unsigned long' md5.c - * - */ -#ifndef uint32 -# define uint32 unsigned int -#endif - -struct MD5Context { - int isInit; - uint32 buf[4]; - uint32 bits[2]; - unsigned char in[64]; -}; -typedef struct MD5Context MD5Context; - -/* - * Note: this code is harmless on little-endian machines. - */ -static void byteReverse (unsigned char *buf, unsigned longs){ - uint32 t; - do { - t = (uint32)((unsigned)buf[3]<<8 | buf[2]) << 16 | - ((unsigned)buf[1]<<8 | buf[0]); - *(uint32 *)buf = t; - buf += 4; - } while (--longs); -} -/* The four core functions - F1 is optimized somewhat */ - -/* #define F1(x, y, z) (x & y | ~x & z) */ -#define F1(x, y, z) (z ^ (x & (y ^ z))) -#define F2(x, y, z) F1(z, x, y) -#define F3(x, y, z) (x ^ y ^ z) -#define F4(x, y, z) (y ^ (x | ~z)) - -/* This is the central step in the MD5 algorithm. */ -#define MD5STEP(f, w, x, y, z, data, s) \ - ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x ) - -/* - * The core of the MD5 algorithm, this alters an existing MD5 hash to - * reflect the addition of 16 longwords of new data. MD5Update blocks - * the data and converts bytes into longwords for this routine. - */ -static void MD5Transform(uint32 buf[4], const uint32 in[16]){ - register uint32 a, b, c, d; - - a = buf[0]; - b = buf[1]; - c = buf[2]; - d = buf[3]; - - MD5STEP(F1, a, b, c, d, in[ 0]+0xd76aa478, 7); - MD5STEP(F1, d, a, b, c, in[ 1]+0xe8c7b756, 12); - MD5STEP(F1, c, d, a, b, in[ 2]+0x242070db, 17); - MD5STEP(F1, b, c, d, a, in[ 3]+0xc1bdceee, 22); - MD5STEP(F1, a, b, c, d, in[ 4]+0xf57c0faf, 7); - MD5STEP(F1, d, a, b, c, in[ 5]+0x4787c62a, 12); - MD5STEP(F1, c, d, a, b, in[ 6]+0xa8304613, 17); - MD5STEP(F1, b, c, d, a, in[ 7]+0xfd469501, 22); - MD5STEP(F1, a, b, c, d, in[ 8]+0x698098d8, 7); - MD5STEP(F1, d, a, b, c, in[ 9]+0x8b44f7af, 12); - MD5STEP(F1, c, d, a, b, in[10]+0xffff5bb1, 17); - MD5STEP(F1, b, c, d, a, in[11]+0x895cd7be, 22); - MD5STEP(F1, a, b, c, d, in[12]+0x6b901122, 7); - MD5STEP(F1, d, a, b, c, in[13]+0xfd987193, 12); - MD5STEP(F1, c, d, a, b, in[14]+0xa679438e, 17); - MD5STEP(F1, b, c, d, a, in[15]+0x49b40821, 22); - - MD5STEP(F2, a, b, c, d, in[ 1]+0xf61e2562, 5); - MD5STEP(F2, d, a, b, c, in[ 6]+0xc040b340, 9); - MD5STEP(F2, c, d, a, b, in[11]+0x265e5a51, 14); - MD5STEP(F2, b, c, d, a, in[ 0]+0xe9b6c7aa, 20); - MD5STEP(F2, a, b, c, d, in[ 5]+0xd62f105d, 5); - MD5STEP(F2, d, a, b, c, in[10]+0x02441453, 9); - MD5STEP(F2, c, d, a, b, in[15]+0xd8a1e681, 14); - MD5STEP(F2, b, c, d, a, in[ 4]+0xe7d3fbc8, 20); - MD5STEP(F2, a, b, c, d, in[ 9]+0x21e1cde6, 5); - MD5STEP(F2, d, a, b, c, in[14]+0xc33707d6, 9); - MD5STEP(F2, c, d, a, b, in[ 3]+0xf4d50d87, 14); - MD5STEP(F2, b, c, d, a, in[ 8]+0x455a14ed, 20); - MD5STEP(F2, a, b, c, d, in[13]+0xa9e3e905, 5); - MD5STEP(F2, d, a, b, c, in[ 2]+0xfcefa3f8, 9); - MD5STEP(F2, c, d, a, b, in[ 7]+0x676f02d9, 14); - MD5STEP(F2, b, c, d, a, in[12]+0x8d2a4c8a, 20); - - MD5STEP(F3, a, b, c, d, in[ 5]+0xfffa3942, 4); - MD5STEP(F3, d, a, b, c, in[ 8]+0x8771f681, 11); - MD5STEP(F3, c, d, a, b, in[11]+0x6d9d6122, 16); - MD5STEP(F3, b, c, d, a, in[14]+0xfde5380c, 23); - MD5STEP(F3, a, b, c, d, in[ 1]+0xa4beea44, 4); - MD5STEP(F3, d, a, b, c, in[ 4]+0x4bdecfa9, 11); - MD5STEP(F3, c, d, a, b, in[ 7]+0xf6bb4b60, 16); - MD5STEP(F3, b, c, d, a, in[10]+0xbebfbc70, 23); - MD5STEP(F3, a, b, c, d, in[13]+0x289b7ec6, 4); - MD5STEP(F3, d, a, b, c, in[ 0]+0xeaa127fa, 11); - MD5STEP(F3, c, d, a, b, in[ 3]+0xd4ef3085, 16); - MD5STEP(F3, b, c, d, a, in[ 6]+0x04881d05, 23); - MD5STEP(F3, a, b, c, d, in[ 9]+0xd9d4d039, 4); - MD5STEP(F3, d, a, b, c, in[12]+0xe6db99e5, 11); - MD5STEP(F3, c, d, a, b, in[15]+0x1fa27cf8, 16); - MD5STEP(F3, b, c, d, a, in[ 2]+0xc4ac5665, 23); - - MD5STEP(F4, a, b, c, d, in[ 0]+0xf4292244, 6); - MD5STEP(F4, d, a, b, c, in[ 7]+0x432aff97, 10); - MD5STEP(F4, c, d, a, b, in[14]+0xab9423a7, 15); - MD5STEP(F4, b, c, d, a, in[ 5]+0xfc93a039, 21); - MD5STEP(F4, a, b, c, d, in[12]+0x655b59c3, 6); - MD5STEP(F4, d, a, b, c, in[ 3]+0x8f0ccc92, 10); - MD5STEP(F4, c, d, a, b, in[10]+0xffeff47d, 15); - MD5STEP(F4, b, c, d, a, in[ 1]+0x85845dd1, 21); - MD5STEP(F4, a, b, c, d, in[ 8]+0x6fa87e4f, 6); - MD5STEP(F4, d, a, b, c, in[15]+0xfe2ce6e0, 10); - MD5STEP(F4, c, d, a, b, in[ 6]+0xa3014314, 15); - MD5STEP(F4, b, c, d, a, in[13]+0x4e0811a1, 21); - MD5STEP(F4, a, b, c, d, in[ 4]+0xf7537e82, 6); - MD5STEP(F4, d, a, b, c, in[11]+0xbd3af235, 10); - MD5STEP(F4, c, d, a, b, in[ 2]+0x2ad7d2bb, 15); - MD5STEP(F4, b, c, d, a, in[ 9]+0xeb86d391, 21); - - buf[0] += a; - buf[1] += b; - buf[2] += c; - buf[3] += d; -} - -/* - * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious - * initialization constants. - */ -static void MD5Init(MD5Context *ctx){ - ctx->isInit = 1; - ctx->buf[0] = 0x67452301; - ctx->buf[1] = 0xefcdab89; - ctx->buf[2] = 0x98badcfe; - ctx->buf[3] = 0x10325476; - ctx->bits[0] = 0; - ctx->bits[1] = 0; -} - -/* - * Update context to reflect the concatenation of another buffer full - * of bytes. - */ -static -void MD5Update(MD5Context *ctx, const unsigned char *buf, unsigned int len){ - uint32 t; - - /* Update bitcount */ - - t = ctx->bits[0]; - if ((ctx->bits[0] = t + ((uint32)len << 3)) < t) - ctx->bits[1]++; /* Carry from low to high */ - ctx->bits[1] += len >> 29; - - t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */ - - /* Handle any leading odd-sized chunks */ - - if ( t ) { - unsigned char *p = (unsigned char *)ctx->in + t; - - t = 64-t; - if (len < t) { - memcpy(p, buf, len); - return; - } - memcpy(p, buf, t); - byteReverse(ctx->in, 16); - MD5Transform(ctx->buf, (uint32 *)ctx->in); - buf += t; - len -= t; - } - - /* Process data in 64-byte chunks */ - - while (len >= 64) { - memcpy(ctx->in, buf, 64); - byteReverse(ctx->in, 16); - MD5Transform(ctx->buf, (uint32 *)ctx->in); - buf += 64; - len -= 64; - } - - /* Handle any remaining bytes of data. */ - - memcpy(ctx->in, buf, len); -} - -/* - * Final wrapup - pad to 64-byte boundary with the bit pattern - * 1 0* (64-bit count of bits processed, MSB-first) - */ -static void MD5Final(unsigned char digest[16], MD5Context *ctx){ - unsigned count; - unsigned char *p; - - /* Compute number of bytes mod 64 */ - count = (ctx->bits[0] >> 3) & 0x3F; - - /* Set the first char of padding to 0x80. This is safe since there is - always at least one byte free */ - p = ctx->in + count; - *p++ = 0x80; - - /* Bytes of padding needed to make 64 bytes */ - count = 64 - 1 - count; - - /* Pad out to 56 mod 64 */ - if (count < 8) { - /* Two lots of padding: Pad the first block to 64 bytes */ - memset(p, 0, count); - byteReverse(ctx->in, 16); - MD5Transform(ctx->buf, (uint32 *)ctx->in); - - /* Now fill the next block with 56 bytes */ - memset(ctx->in, 0, 56); - } else { - /* Pad block to 56 bytes */ - memset(p, 0, count-8); - } - byteReverse(ctx->in, 14); - - /* Append length in bits and transform */ - memcpy(ctx->in + 14*4, ctx->bits, 8); - - MD5Transform(ctx->buf, (uint32 *)ctx->in); - byteReverse((unsigned char *)ctx->buf, 4); - memcpy(digest, ctx->buf, 16); -} - -/* -** Convert a 128-bit MD5 digest into a 32-digit base-16 number. -*/ -static void MD5DigestToBase16(unsigned char *digest, char *zBuf){ - static char const zEncode[] = "0123456789abcdef"; - int i, j; - - for(j=i=0; i<16; i++){ - int a = digest[i]; - zBuf[j++] = zEncode[(a>>4)&0xf]; - zBuf[j++] = zEncode[a & 0xf]; - } - zBuf[j] = 0; -} - - -/* -** Convert a 128-bit MD5 digest into sequency of eight 5-digit integers -** each representing 16 bits of the digest and separated from each -** other by a "-" character. +** Versions of all of the above entry points that omit the "3" at the end +** of the name. Years ago (circa 2004) the "3" was necessary to distinguish +** SQLite version 3 from Sqlite version 2. But two decades have elapsed. +** SQLite2 is not longer a conflict. So it is ok to omit the "3". +** +** Omitting the "3" helps TCL find the entry point. */ -static void MD5DigestToBase10x8(unsigned char digest[16], char zDigest[50]){ - int i, j; - unsigned int x; - for(i=j=0; i<16; i+=2){ - x = digest[i]*256 + digest[i+1]; - if( i>0 ) zDigest[j++] = '-'; - sqlite3_snprintf(50-j, &zDigest[j], "%05u", x); - j += 5; - } - zDigest[j] = 0; -} +EXTERN int Sqlite_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp);} +EXTERN int Tclsqlite_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); } +EXTERN int Sqlite_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } +EXTERN int Tclsqlite_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; } +EXTERN int Sqlite_SafeInit(Tcl_Interp *interp){ return TCL_ERROR; } +EXTERN int Sqlite_SafeUnload(Tcl_Interp *interp, int flags){return TCL_ERROR;} -/* -** A TCL command for md5. The argument is the text to be hashed. The -** Result is the hash in base64. -*/ -static int md5_cmd(void*cd, Tcl_Interp *interp, int argc, const char **argv){ - MD5Context ctx; - unsigned char digest[16]; - char zBuf[50]; - void (*converter)(unsigned char*, char*); - - if( argc!=2 ){ - Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0], - " TEXT\"", (char*)0); - return TCL_ERROR; - } - MD5Init(&ctx); - MD5Update(&ctx, (unsigned char*)argv[1], (unsigned)strlen(argv[1])); - MD5Final(digest, &ctx); - converter = (void(*)(unsigned char*,char*))cd; - converter(digest, zBuf); - Tcl_AppendResult(interp, zBuf, (char*)0); - return TCL_OK; -} +/* Also variants with a lowercase "s". I'm told that these are +** deprecated in Tcl9, but they continue to be included for backwards +** compatibility. */ +EXTERN int sqlite3_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp);} +EXTERN int sqlite_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp);} -/* -** A TCL command to take the md5 hash of a file. The argument is the -** name of the file. -*/ -static int md5file_cmd(void*cd, Tcl_Interp*interp, int argc, const char **argv){ - FILE *in; - MD5Context ctx; - void (*converter)(unsigned char*, char*); - unsigned char digest[16]; - char zBuf[10240]; - - if( argc!=2 ){ - Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0], - " FILENAME\"", (char*)0); - return TCL_ERROR; - } - in = fopen(argv[1],"rb"); - if( in==0 ){ - Tcl_AppendResult(interp,"unable to open file \"", argv[1], - "\" for reading", (char*)0); - return TCL_ERROR; - } - MD5Init(&ctx); - for(;;){ - int n; - n = (int)fread(zBuf, 1, sizeof(zBuf), in); - if( n<=0 ) break; - MD5Update(&ctx, (unsigned char*)zBuf, (unsigned)n); - } - fclose(in); - MD5Final(digest, &ctx); - converter = (void(*)(unsigned char*,char*))cd; - converter(digest, zBuf); - Tcl_AppendResult(interp, zBuf, (char*)0); - return TCL_OK; -} - -/* -** Register the four new TCL commands for generating MD5 checksums -** with the TCL interpreter. -*/ -int Md5_Init(Tcl_Interp *interp){ - Tcl_CreateCommand(interp, "md5", (Tcl_CmdProc*)md5_cmd, - MD5DigestToBase16, 0); - Tcl_CreateCommand(interp, "md5-10x8", (Tcl_CmdProc*)md5_cmd, - MD5DigestToBase10x8, 0); - Tcl_CreateCommand(interp, "md5file", (Tcl_CmdProc*)md5file_cmd, - MD5DigestToBase16, 0); - Tcl_CreateCommand(interp, "md5file-10x8", (Tcl_CmdProc*)md5file_cmd, - MD5DigestToBase10x8, 0); - return TCL_OK; -} -#endif /* defined(SQLITE_TEST) || defined(SQLITE_TCLMD5) */ -#if defined(SQLITE_TEST) /* -** During testing, the special md5sum() aggregate function is available. -** inside SQLite. The following routines implement that function. +** If the TCLSH macro is defined, add code to make a stand-alone program. */ -static void md5step(sqlite3_context *context, int argc, sqlite3_value **argv){ - MD5Context *p; - int i; - if( argc<1 ) return; - p = sqlite3_aggregate_context(context, sizeof(*p)); - if( p==0 ) return; - if( !p->isInit ){ - MD5Init(p); - } - for(i=0; i<argc; i++){ - const char *zData = (char*)sqlite3_value_text(argv[i]); - if( zData ){ - MD5Update(p, (unsigned char*)zData, (int)strlen(zData)); - } - } -} -static void md5finalize(sqlite3_context *context){ - MD5Context *p; - unsigned char digest[16]; - char zBuf[33]; - p = sqlite3_aggregate_context(context, sizeof(*p)); - MD5Final(digest,p); - MD5DigestToBase16(digest, zBuf); - sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); -} -int Md5_Register(sqlite3 *db){ - int rc = sqlite3_create_function(db, "md5sum", -1, SQLITE_UTF8, 0, 0, - md5step, md5finalize); - sqlite3_overload_function(db, "md5sum", -1); /* To exercise this API */ - return rc; -} -#endif /* defined(SQLITE_TEST) */ - +#if defined(TCLSH) -/* -** If the macro TCLSH is one, then put in code this for the -** "main" routine that will initialize Tcl and take input from -** standard input, or if a file is named on the command line -** the TCL interpreter reads and evaluates that file. +/* This is the main routine for an ordinary TCL shell. If there are +** arguments, run the first argument as a script. Otherwise, read TCL +** commands from standard input */ -#if TCLSH==1 static const char *tclsh_main_loop(void){ static const char zMainloop[] = - "set line {}\n" - "while {![eof stdin]} {\n" - "if {$line!=\"\"} {\n" - "puts -nonewline \"> \"\n" - "} else {\n" - "puts -nonewline \"% \"\n" + "if {[llength $argv]>=1} {\n" +#ifdef WIN32 + "set new [list]\n" + "foreach arg $argv {\n" + "if {[string match -* $arg] || [file exists $arg]} {\n" + "lappend new $arg\n" + "} else {\n" + "set once 0\n" + "foreach match [lsort [glob -nocomplain $arg]] {\n" + "lappend new $match\n" + "set once 1\n" + "}\n" + "if {!$once} {lappend new $arg}\n" + "}\n" "}\n" - "flush stdout\n" - "append line [gets stdin]\n" - "if {[info complete $line]} {\n" - "if {[catch {uplevel #0 $line} result]} {\n" - "puts stderr \"Error: $result\"\n" - "} elseif {$result!=\"\"} {\n" - "puts $result\n" + "set argv $new\n" + "unset new\n" +#endif + "set argv0 [lindex $argv 0]\n" + "set argv [lrange $argv 1 end]\n" + "source $argv0\n" + "} else {\n" + "set line {}\n" + "while {![eof stdin]} {\n" + "if {$line!=\"\"} {\n" + "puts -nonewline \"> \"\n" + "} else {\n" + "puts -nonewline \"% \"\n" + "}\n" + "flush stdout\n" + "append line [gets stdin]\n" + "if {[info complete $line]} {\n" + "if {[catch {uplevel #0 $line} result]} {\n" + "puts stderr \"Error: $result\"\n" + "} elseif {$result!=\"\"} {\n" + "puts $result\n" + "}\n" + "set line {}\n" + "} else {\n" + "append line \\n\n" "}\n" - "set line {}\n" - "} else {\n" - "append line \\n\n" "}\n" "}\n" ; return zMainloop; } -#endif -#if TCLSH==2 -static const char *tclsh_main_loop(void); -#endif - -#ifdef SQLITE_TEST -static void init_all(Tcl_Interp *); -static int init_all_cmd( - ClientData cd, - Tcl_Interp *interp, - int objc, - Tcl_Obj *CONST objv[] -){ - - Tcl_Interp *slave; - if( objc!=2 ){ - Tcl_WrongNumArgs(interp, 1, objv, "SLAVE"); - return TCL_ERROR; - } - - slave = Tcl_GetSlave(interp, Tcl_GetString(objv[1])); - if( !slave ){ - return TCL_ERROR; - } - - init_all(slave); - return TCL_OK; -} - -/* -** Tclcmd: db_use_legacy_prepare DB BOOLEAN -** -** The first argument to this command must be a database command created by -** [sqlite3]. If the second argument is true, then the handle is configured -** to use the sqlite3_prepare_v2() function to prepare statements. If it -** is false, sqlite3_prepare(). -*/ -static int db_use_legacy_prepare_cmd( - ClientData cd, - Tcl_Interp *interp, - int objc, - Tcl_Obj *CONST objv[] -){ - Tcl_CmdInfo cmdInfo; - SqliteDb *pDb; - int bPrepare; - - if( objc!=3 ){ - Tcl_WrongNumArgs(interp, 1, objv, "DB BOOLEAN"); - return TCL_ERROR; - } - - if( !Tcl_GetCommandInfo(interp, Tcl_GetString(objv[1]), &cmdInfo) ){ - Tcl_AppendResult(interp, "no such db: ", Tcl_GetString(objv[1]), (char*)0); - return TCL_ERROR; - } - pDb = (SqliteDb*)cmdInfo.objClientData; - if( Tcl_GetBooleanFromObj(interp, objv[2], &bPrepare) ){ - return TCL_ERROR; - } - - pDb->bLegacyPrepare = bPrepare; - - Tcl_ResetResult(interp); - return TCL_OK; -} - -/* -** Tclcmd: db_last_stmt_ptr DB -** -** If the statement cache associated with database DB is not empty, -** return the text representation of the most recently used statement -** handle. -*/ -static int db_last_stmt_ptr( - ClientData cd, - Tcl_Interp *interp, - int objc, - Tcl_Obj *CONST objv[] -){ - extern int sqlite3TestMakePointerStr(Tcl_Interp*, char*, void*); - Tcl_CmdInfo cmdInfo; - SqliteDb *pDb; - sqlite3_stmt *pStmt = 0; - char zBuf[100]; - - if( objc!=2 ){ - Tcl_WrongNumArgs(interp, 1, objv, "DB"); - return TCL_ERROR; - } - - if( !Tcl_GetCommandInfo(interp, Tcl_GetString(objv[1]), &cmdInfo) ){ - Tcl_AppendResult(interp, "no such db: ", Tcl_GetString(objv[1]), (char*)0); - return TCL_ERROR; - } - pDb = (SqliteDb*)cmdInfo.objClientData; - - if( pDb->stmtList ) pStmt = pDb->stmtList->pStmt; - if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ){ - return TCL_ERROR; - } - Tcl_SetResult(interp, zBuf, TCL_VOLATILE); - - return TCL_OK; -} -#endif /* SQLITE_TEST */ - -/* -** Configure the interpreter passed as the first argument to have access -** to the commands and linked variables that make up: -** -** * the [sqlite3] extension itself, -** -** * If SQLITE_TCLMD5 or SQLITE_TEST is defined, the Md5 commands, and -** -** * If SQLITE_TEST is set, the various test interfaces used by the Tcl -** test suite. -*/ -static void init_all(Tcl_Interp *interp){ - Sqlite3_Init(interp); - -#if defined(SQLITE_TEST) || defined(SQLITE_TCLMD5) - Md5_Init(interp); -#endif - -#ifdef SQLITE_TEST - { - extern int Sqliteconfig_Init(Tcl_Interp*); - extern int Sqlitetest1_Init(Tcl_Interp*); - extern int Sqlitetest2_Init(Tcl_Interp*); - extern int Sqlitetest3_Init(Tcl_Interp*); - extern int Sqlitetest4_Init(Tcl_Interp*); - extern int Sqlitetest5_Init(Tcl_Interp*); - extern int Sqlitetest6_Init(Tcl_Interp*); - extern int Sqlitetest7_Init(Tcl_Interp*); - extern int Sqlitetest8_Init(Tcl_Interp*); - extern int Sqlitetest9_Init(Tcl_Interp*); - extern int Sqlitetestasync_Init(Tcl_Interp*); - extern int Sqlitetest_autoext_Init(Tcl_Interp*); - extern int Sqlitetest_blob_Init(Tcl_Interp*); - extern int Sqlitetest_demovfs_Init(Tcl_Interp *); - extern int Sqlitetest_func_Init(Tcl_Interp*); - extern int Sqlitetest_hexio_Init(Tcl_Interp*); - extern int Sqlitetest_init_Init(Tcl_Interp*); - extern int Sqlitetest_malloc_Init(Tcl_Interp*); - extern int Sqlitetest_mutex_Init(Tcl_Interp*); - extern int Sqlitetestschema_Init(Tcl_Interp*); - extern int Sqlitetestsse_Init(Tcl_Interp*); - extern int Sqlitetesttclvar_Init(Tcl_Interp*); - extern int Sqlitetestfs_Init(Tcl_Interp*); - extern int SqlitetestThread_Init(Tcl_Interp*); - extern int SqlitetestOnefile_Init(); - extern int SqlitetestOsinst_Init(Tcl_Interp*); - extern int Sqlitetestbackup_Init(Tcl_Interp*); - extern int Sqlitetestintarray_Init(Tcl_Interp*); - extern int Sqlitetestvfs_Init(Tcl_Interp *); - extern int Sqlitetestrtree_Init(Tcl_Interp*); - extern int Sqlitequota_Init(Tcl_Interp*); - extern int Sqlitemultiplex_Init(Tcl_Interp*); - extern int SqliteSuperlock_Init(Tcl_Interp*); - extern int SqlitetestSyscall_Init(Tcl_Interp*); - extern int Fts5tcl_Init(Tcl_Interp *); - extern int SqliteRbu_Init(Tcl_Interp*); -#if defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4) - extern int Sqlitetestfts3_Init(Tcl_Interp *interp); -#endif - -#ifdef SQLITE_ENABLE_ZIPVFS - extern int Zipvfs_Init(Tcl_Interp*); - Zipvfs_Init(interp); -#endif - - Sqliteconfig_Init(interp); - Sqlitetest1_Init(interp); - Sqlitetest2_Init(interp); - Sqlitetest3_Init(interp); - Sqlitetest4_Init(interp); - Sqlitetest5_Init(interp); - Sqlitetest6_Init(interp); - Sqlitetest7_Init(interp); - Sqlitetest8_Init(interp); - Sqlitetest9_Init(interp); - Sqlitetestasync_Init(interp); - Sqlitetest_autoext_Init(interp); - Sqlitetest_blob_Init(interp); - Sqlitetest_demovfs_Init(interp); - Sqlitetest_func_Init(interp); - Sqlitetest_hexio_Init(interp); - Sqlitetest_init_Init(interp); - Sqlitetest_malloc_Init(interp); - Sqlitetest_mutex_Init(interp); - Sqlitetestschema_Init(interp); - Sqlitetesttclvar_Init(interp); - Sqlitetestfs_Init(interp); - SqlitetestThread_Init(interp); - SqlitetestOnefile_Init(interp); - SqlitetestOsinst_Init(interp); - Sqlitetestbackup_Init(interp); - Sqlitetestintarray_Init(interp); - Sqlitetestvfs_Init(interp); - Sqlitetestrtree_Init(interp); - Sqlitequota_Init(interp); - Sqlitemultiplex_Init(interp); - SqliteSuperlock_Init(interp); - SqlitetestSyscall_Init(interp); - Fts5tcl_Init(interp); - SqliteRbu_Init(interp); - -#if defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4) - Sqlitetestfts3_Init(interp); -#endif - - Tcl_CreateObjCommand( - interp, "load_testfixture_extensions", init_all_cmd, 0, 0 - ); - Tcl_CreateObjCommand( - interp, "db_use_legacy_prepare", db_use_legacy_prepare_cmd, 0, 0 - ); - Tcl_CreateObjCommand( - interp, "db_last_stmt_ptr", db_last_stmt_ptr, 0, 0 - ); - -#ifdef SQLITE_SSE - Sqlitetestsse_Init(interp); -#endif - } -#endif -} -/* Needed for the setrlimit() system call on unix */ -#if defined(unix) -#include <sys/resource.h> +#ifndef TCLSH_MAIN +# define TCLSH_MAIN main #endif - -#define TCLSH_MAIN main /* Needed to fake out mktclapp */ -int TCLSH_MAIN(int argc, char **argv){ +int SQLITE_CDECL TCLSH_MAIN(int argc, char **argv){ Tcl_Interp *interp; + int i; + const char *zScript = 0; + char zArgc[32]; +#if defined(TCLSH_INIT_PROC) + extern const char *TCLSH_INIT_PROC(Tcl_Interp*); +#endif #if !defined(_WIN32_WCE) - if( getenv("BREAK") ){ - fprintf(stderr, - "attach debugger to process %d and press any key to continue.\n", - GETPID()); - fgetc(stdin); - } + if( getenv("SQLITE_DEBUG_BREAK") ){ + if( isatty(0) && isatty(2) ){ + fprintf(stderr, + "attach debugger to process %d and press any key to continue.\n", + GETPID()); + fgetc(stdin); + }else{ +#if defined(_WIN32) || defined(WIN32) + DebugBreak(); +#elif defined(SIGTRAP) + raise(SIGTRAP); #endif - - /* Since the primary use case for this binary is testing of SQLite, - ** be sure to generate core files if we crash */ -#if defined(SQLITE_TEST) && defined(unix) - { struct rlimit x; - getrlimit(RLIMIT_CORE, &x); - x.rlim_cur = x.rlim_max; - setrlimit(RLIMIT_CORE, &x); + } } -#endif /* SQLITE_TEST && unix */ - +#endif /* Call sqlite3_shutdown() once before doing anything else. This is to ** test that sqlite3_shutdown() can be safely called by a process before @@ -3877,32 +4233,27 @@ int TCLSH_MAIN(int argc, char **argv){ Tcl_FindExecutable(argv[0]); Tcl_SetSystemEncoding(NULL, "utf-8"); interp = Tcl_CreateInterp(); + Sqlite3_Init(interp); -#if TCLSH==2 - sqlite3_config(SQLITE_CONFIG_SINGLETHREAD); + sqlite3_snprintf(sizeof(zArgc), zArgc, "%d", argc-1); + Tcl_SetVar(interp,"argc", zArgc, TCL_GLOBAL_ONLY); + Tcl_SetVar(interp,"argv0",argv[0],TCL_GLOBAL_ONLY); + Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY); + for(i=1; i<argc; i++){ + Tcl_SetVar(interp, "argv", argv[i], + TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE); + } +#if defined(TCLSH_INIT_PROC) + zScript = TCLSH_INIT_PROC(interp); #endif - - init_all(interp); - if( argc>=2 ){ - int i; - char zArgc[32]; - sqlite3_snprintf(sizeof(zArgc), zArgc, "%d", argc-(3-TCLSH)); - Tcl_SetVar(interp,"argc", zArgc, TCL_GLOBAL_ONLY); - Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY); - Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY); - for(i=3-TCLSH; i<argc; i++){ - Tcl_SetVar(interp, "argv", argv[i], - TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE); - } - if( TCLSH==1 && Tcl_EvalFile(interp, argv[1])!=TCL_OK ){ - const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY); - if( zInfo==0 ) zInfo = Tcl_GetStringResult(interp); - fprintf(stderr,"%s: %s\n", *argv, zInfo); - return 1; - } - } - if( TCLSH==2 || argc<=1 ){ - Tcl_GlobalEval(interp, tclsh_main_loop()); + if( zScript==0 ){ + zScript = tclsh_main_loop(); + } + if( Tcl_GlobalEval(interp, zScript)!=TCL_OK ){ + const char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY); + if( zInfo==0 ) zInfo = Tcl_GetStringResult(interp); + fprintf(stderr,"%s: %s\n", *argv, zInfo); + return 1; } return 0; } diff --git a/src/tclsqlite.h b/src/tclsqlite.h new file mode 100644 index 0000000000..f71ec9a7c2 --- /dev/null +++ b/src/tclsqlite.h @@ -0,0 +1,42 @@ +/* +** 2024-07-30 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This header file defines the interface to TCL as used by SQLite. +** SQLite subcomponents that use TCL (the libsqlite3.c interface library +** and various test*.c pieces) should #include this file rather than +** including tcl.h directly. +*/ +/****** Any edits to this file must mirrored in tclsqlite.c ***********/ + +/* When compiling for Windows using STDCALL instead of CDECL calling +** conventions, the MSVC makefile has to build a customized version of +** the "tcl.h" header that specifies the calling conventions for each +** interface. That customized "tcl.h" is named "sqlite_tcl.h". +*/ +#if defined(INCLUDE_SQLITE_TCL_H) +# include "sqlite_tcl.h" /* Special case for Windows using STDCALL */ +#else +# include <tcl.h> /* All normal cases */ +# ifndef SQLITE_TCLAPI +# define SQLITE_TCLAPI +# endif +#endif + +/****** Any edits to this file must mirrored in tclsqlite.c ***********/ + +/* Compatibility between Tcl8.6 and Tcl9.0 */ +#if TCL_MAJOR_VERSION==9 +# define CONST const +#elif !defined(Tcl_Size) +# define Tcl_Size int +#endif + +/****** Any edits to this file must mirrored in tclsqlite.c ***********/ diff --git a/src/test1.c b/src/test1.c index c5e71582bc..41b3324050 100644 --- a/src/test1.c +++ b/src/test1.c @@ -16,10 +16,17 @@ #include "sqliteInt.h" #if SQLITE_OS_WIN # include "os_win.h" +# include <windows.h> +#else +# include <unistd.h> +# if defined(__APPLE__) +# include <sys/param.h> +# include <sys/sysctl.h> +# endif #endif #include "vdbeInt.h" -#include "tcl.h" +#include "tclsqlite.h" #include <stdlib.h> #include <string.h> @@ -75,7 +82,7 @@ void *sqlite3TestTextToPtr(const char *z){ ** for an sqlite connection instance. Bad things happen if the ** input is not an sqlite connection. */ -static int get_sqlite_pointer( +static int SQLITE_TCLAPI get_sqlite_pointer( void * clientData, Tcl_Interp *interp, int objc, @@ -95,7 +102,7 @@ static int get_sqlite_pointer( } p = (struct SqliteDb*)cmdInfo.objClientData; sqlite3_snprintf(sizeof(zBuf), zBuf, "%p", p->db); - Tcl_AppendResult(interp, zBuf, 0); + Tcl_AppendResult(interp, zBuf, NULL); return TCL_OK; } @@ -146,7 +153,7 @@ int sqlite3TestErrCode(Tcl_Interp *interp, sqlite3 *db, int rc){ "error code %s (%d) does not match sqlite3_errcode %s (%d)", t1ErrorName(rc), rc, t1ErrorName(r2), r2); Tcl_ResetResult(interp); - Tcl_AppendResult(interp, zBuf, 0); + Tcl_AppendResult(interp, zBuf, NULL); return 1; } return 0; @@ -221,7 +228,7 @@ static void io_trace_callback(const char *zFormat, ...){ ** I/O tracing begins going into FILENAME. If FILENAME is an empty ** string, I/O tracing is turned off. */ -static int test_io_trace( +static int SQLITE_TCLAPI test_io_trace( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -262,7 +269,7 @@ static int test_io_trace( ** ** Also return true if the OMIT_MISUSE environment variable exists. */ -static int clang_sanitize_address( +static int SQLITE_TCLAPI clang_sanitize_address( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -289,7 +296,7 @@ static int clang_sanitize_address( ** DB. The SQL is the string FORMAT. The format string should contain ** one %s or %q. STRING is the value inserted into %s or %q. */ -static int test_exec_printf( +static int SQLITE_TCLAPI test_exec_printf( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -327,7 +334,7 @@ static int test_exec_printf( ** HEX into ASCII. Most characters are translated as is. %HH becomes ** a hex character. */ -static int test_exec_hex( +static int SQLITE_TCLAPI test_exec_hex( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -373,7 +380,7 @@ static int test_exec_hex( ** ** Enter or leave the mutex on a database connection. */ -static int db_enter( +static int SQLITE_TCLAPI db_enter( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -389,7 +396,7 @@ static int db_enter( sqlite3_mutex_enter(db->mutex); return TCL_OK; } -static int db_leave( +static int SQLITE_TCLAPI db_leave( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -411,7 +418,7 @@ static int db_leave( ** ** Invoke the sqlite3_exec interface using the open database DB */ -static int test_exec( +static int SQLITE_TCLAPI test_exec( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -458,7 +465,7 @@ static int test_exec( ** Invoke the sqlite3_exec interface using the open database DB. Discard ** all results */ -static int test_exec_nr( +static int SQLITE_TCLAPI test_exec_nr( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -485,7 +492,7 @@ static int test_exec_nr( ** concatenate arg0 through argn using separator as the separator. ** Return the result. */ -static int test_mprintf_z( +static int SQLITE_TCLAPI test_mprintf_z( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -497,7 +504,7 @@ static int test_mprintf_z( for(i=2; i<argc && (i==2 || zResult); i++){ zResult = sqlite3_mprintf("%z%s%s", zResult, argv[1], argv[i]); } - Tcl_AppendResult(interp, zResult, 0); + Tcl_AppendResult(interp, zResult, NULL); sqlite3_free(zResult); return TCL_OK; } @@ -508,7 +515,7 @@ static int test_mprintf_z( ** Test the %n format of sqlite_mprintf(). Return the length of the ** input string. */ -static int test_mprintf_n( +static int SQLITE_TCLAPI test_mprintf_n( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -532,7 +539,7 @@ static int test_mprintf_n( ** You pass in a format string that requires more than one argument, ** bad things will happen. */ -static int test_snprintf_int( +static int SQLITE_TCLAPI test_snprintf_int( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -545,7 +552,7 @@ static int test_snprintf_int( if( n>sizeof(zStr) ) n = sizeof(zStr); sqlite3_snprintf(sizeof(zStr), zStr, "abcdefghijklmnopqrstuvwxyz"); sqlite3_snprintf(n, zStr, zFormat, a1); - Tcl_AppendResult(interp, zStr, 0); + Tcl_AppendResult(interp, zStr, NULL); return TCL_OK; } @@ -558,7 +565,7 @@ static int test_snprintf_int( ** DB. The SQL is the string FORMAT. The format string should contain ** one %s or %q. STRING is the value inserted into %s or %q. */ -static int test_get_table_printf( +static int SQLITE_TCLAPI test_get_table_printf( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -593,6 +600,7 @@ static int test_get_table_printf( } sqlite3_free(zSql); sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", rc); + Tcl_ResetResult(interp); Tcl_AppendElement(interp, zBuf); if( rc==SQLITE_OK ){ if( argc==4 ){ @@ -621,7 +629,7 @@ static int test_get_table_printf( ** ** Returns the integer ROWID of the most recent insert. */ -static int test_last_rowid( +static int SQLITE_TCLAPI test_last_rowid( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -631,12 +639,12 @@ static int test_last_rowid( char zBuf[30]; if( argc!=2 ){ - Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB\"", 0); + Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB\"", NULL); return TCL_ERROR; } if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", sqlite3_last_insert_rowid(db)); - Tcl_AppendResult(interp, zBuf, 0); + Tcl_AppendResult(interp, zBuf, NULL); return SQLITE_OK; } @@ -645,13 +653,13 @@ static int test_last_rowid( ** ** Set the codec key. */ -static int test_key( +static int SQLITE_TCLAPI test_key( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ char **argv /* Text of each argument */ ){ -#ifdef SQLITE_HAS_CODEC +#if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL) sqlite3 *db; const char *zKey; int nKey; @@ -673,7 +681,7 @@ static int test_key( ** ** Change the codec key. */ -static int test_rekey( +static int SQLITE_TCLAPI test_rekey( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -701,7 +709,7 @@ static int test_rekey( ** ** Closes the database opened by sqlite3_open. */ -static int sqlite_test_close( +static int SQLITE_TCLAPI sqlite_test_close( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -725,7 +733,7 @@ static int sqlite_test_close( ** ** Closes the database opened by sqlite3_open. */ -static int sqlite_test_close_v2( +static int SQLITE_TCLAPI sqlite_test_close_v2( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -994,6 +1002,117 @@ static void nondeterministicFunction( sqlite3_result_int(context, cnt++); } +/* +** This SQL function returns the integer value of its argument as a MEM_IntReal +** value. +*/ +static void intrealFunction( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + sqlite3_int64 v = sqlite3_value_int64(argv[0]); + sqlite3_result_int64(context, v); + sqlite3_test_control(SQLITE_TESTCTRL_RESULT_INTREAL, context); +} + +/* +** These SQL functions attempt to return a value (their first argument) +** that has been modified to have multiple datatypes. For example both +** TEXT and INTEGER. +*/ +static void addTextTypeFunction( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + (void)sqlite3_value_text(argv[0]); + (void)argc; + sqlite3_result_value(context, argv[0]); +} +static void addIntTypeFunction( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + (void)sqlite3_value_int64(argv[0]); + (void)argc; + sqlite3_result_value(context, argv[0]); +} +static void addRealTypeFunction( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + (void)sqlite3_value_double(argv[0]); + (void)argc; + sqlite3_result_value(context, argv[0]); +} + +/* +** SQL function: strtod(X) +** +** Use the C-library strtod() function to convert string X into a double. +** Used for comparing the accuracy of SQLite's internal text-to-float conversion +** routines against the C-library. +*/ +static void shellStrtod( + sqlite3_context *pCtx, + int nVal, + sqlite3_value **apVal +){ + char *z = (char*)sqlite3_value_text(apVal[0]); + UNUSED_PARAMETER(nVal); + if( z==0 ) return; + sqlite3_result_double(pCtx, strtod(z,0)); +} + +/* +** SQL function: dtostr(X) +** +** Use the C-library printf() function to convert real value X into a string. +** Used for comparing the accuracy of SQLite's internal float-to-text conversion +** routines against the C-library. +*/ +static void shellDtostr( + sqlite3_context *pCtx, + int nVal, + sqlite3_value **apVal +){ + double r = sqlite3_value_double(apVal[0]); + int n = nVal>=2 ? sqlite3_value_int(apVal[1]) : 26; + char z[400]; + if( n<1 ) n = 1; + if( n>350 ) n = 350; + sprintf(z, "%#+.*e", n, r); + sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT); +} + +/* +** We need a method for setting the pointer values created by the +** intarray_addr, int64array_addr, doublearray_addr, and textarray_addr +** routines below. The inttoptr(X) SQL function accomplishes +** this. Tcl scripts will bind an array address as an integer X and +** the inttoptr() SQL function will use sqlite3_result_pointer() to +** convert that integer into a pointer usable by carray(). +*/ +static void inttoptrFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + void *p; + sqlite3_int64 i64; + i64 = sqlite3_value_int64(argv[0]); + if( sizeof(i64)==sizeof(p) ){ + memcpy(&p, &i64, sizeof(p)); + }else{ + int i32 = i64 & 0xffffffff; + memcpy(&p, &i32, sizeof(p)); + } + sqlite3_result_pointer(context, p, "carray", 0); +} + /* ** Usage: sqlite3_create_function DB ** @@ -1007,9 +1126,10 @@ static void nondeterministicFunction( ** ** The original motivation for this routine was to be able to call the ** sqlite3_create_function function while a query is in progress in order -** to test the SQLITE_MISUSE detection logic. +** to test the SQLITE_MISUSE detection logic. It is now also used to register +** a bunch of SQL functions that are useful for testing. */ -static int test_create_function( +static int SQLITE_TCLAPI test_create_function( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -1058,6 +1178,55 @@ static int test_create_function( 0, nondeterministicFunction, 0, 0); } + /* The intreal() function converts its argument to an integer and returns + ** it as a MEM_IntReal. + */ + if( rc==SQLITE_OK ){ + rc = sqlite3_create_function(db, "intreal", 1, SQLITE_UTF8, + 0, intrealFunction, 0, 0); + } + + /* The add_text_type(), add_int_type(), and add_real_type() functions + ** attempt to return a value that has multiple datatypes. + */ + if( rc==SQLITE_OK ){ + rc = sqlite3_create_function(db, "add_text_type", 1, SQLITE_UTF8, + 0, addTextTypeFunction, 0, 0); + } + if( rc==SQLITE_OK ){ + rc = sqlite3_create_function(db, "add_int_type", 1, SQLITE_UTF8, + 0, addIntTypeFunction, 0, 0); + } + if( rc==SQLITE_OK ){ + rc = sqlite3_create_function(db, "add_real_type", 1, SQLITE_UTF8, + 0, addRealTypeFunction, 0, 0); + } + + /* Functions strtod() and dtostr() work as in the shell. These routines + ** use the standard C library to convert between floating point and + ** text. This is used to compare SQLite's internal conversion routines + ** against the standard library conversion routines. + ** + ** Both routines copy/pasted from the shell.c.in implementation + ** on 2023-07-03. + */ + if( rc==SQLITE_OK ){ + rc = sqlite3_create_function(db, "strtod", 1, SQLITE_UTF8, 0, + shellStrtod, 0, 0); + } + if( rc==SQLITE_OK ){ + rc = sqlite3_create_function(db, "dtostr", 1, SQLITE_UTF8, 0, + shellDtostr, 0, 0); + } + if( rc==SQLITE_OK ){ + rc = sqlite3_create_function(db, "dtostr", 2, SQLITE_UTF8, 0, + shellDtostr, 0, 0); + } + if( rc==SQLITE_OK ){ + rc = sqlite3_create_function(db, "inttoptr", 1, SQLITE_UTF8, 0, + inttoptrFunc, 0, 0); + } + #ifndef SQLITE_OMIT_UTF16 /* Use the sqlite3_create_function16() API here. Mainly for fun, but also ** because it is not tested anywhere else. */ @@ -1084,6 +1253,33 @@ static int test_create_function( return TCL_OK; } +/* +** Usage: sqlite3_drop_modules DB ?NAME ...? +** +** Invoke the sqlite3_drop_modules(D,L) interface on database +** connection DB, in order to drop all modules except those named in +** the argument. +*/ +static int SQLITE_TCLAPI test_drop_modules( + void *NotUsed, + Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ + int argc, /* Number of arguments */ + char **argv /* Text of each argument */ +){ + sqlite3 *db; + + if( argc<2 ){ + Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], + " DB\"", 0); + return TCL_ERROR; + } + if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; +#ifndef SQLITE_OMIT_VIRTUALTABLE + sqlite3_drop_modules(db, argc>2 ? (const char**)(argv+2) : 0); +#endif + return TCL_OK; +} + /* ** Routines to implement the x_count() aggregate function. ** @@ -1166,7 +1362,7 @@ static void legacyCountFinalize(sqlite3_context *context){ ** "legacy_count()" with the supplied database handle. This is used ** to test the deprecated sqlite3_aggregate_count() API. */ -static int test_create_aggregate( +static int SQLITE_TCLAPI test_create_aggregate( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -1207,7 +1403,7 @@ static int test_create_aggregate( ** Puts uses a separate buffer and debugging statements will be out of ** sequence if it is used. */ -static int test_printf( +static int SQLITE_TCLAPI test_printf( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -1229,7 +1425,7 @@ static int test_printf( ** ** Call mprintf with three integer arguments */ -static int sqlite3_mprintf_int( +static int SQLITE_TCLAPI sqlite3_mprintf_int( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -1246,7 +1442,7 @@ static int sqlite3_mprintf_int( if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR; } z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]); - Tcl_AppendResult(interp, z, 0); + Tcl_AppendResult(interp, z, NULL); sqlite3_free(z); return TCL_OK; } @@ -1256,7 +1452,7 @@ static int sqlite3_mprintf_int( ** ** Call mprintf with three 64-bit integer arguments */ -static int sqlite3_mprintf_int64( +static int SQLITE_TCLAPI sqlite3_mprintf_int64( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -1271,13 +1467,13 @@ static int sqlite3_mprintf_int64( return TCL_ERROR; } for(i=2; i<5; i++){ - if( sqlite3Atoi64(argv[i], &a[i-2], 1000000, SQLITE_UTF8) ){ - Tcl_AppendResult(interp, "argument is not a valid 64-bit integer", 0); + if( sqlite3Atoi64(argv[i], &a[i-2], sqlite3Strlen30(argv[i]), SQLITE_UTF8) ){ + Tcl_AppendResult(interp, "argument is not a valid 64-bit integer", NULL); return TCL_ERROR; } } z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]); - Tcl_AppendResult(interp, z, 0); + Tcl_AppendResult(interp, z, NULL); sqlite3_free(z); return TCL_OK; } @@ -1289,7 +1485,7 @@ static int sqlite3_mprintf_int64( ** same as sqlite3_mprintf_int or sqlite3_mprintf_int64, depending on ** platform. */ -static int sqlite3_mprintf_long( +static int SQLITE_TCLAPI sqlite3_mprintf_long( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -1310,7 +1506,7 @@ static int sqlite3_mprintf_long( a[i-2] &= (((u64)1)<<(sizeof(int)*8))-1; } z = sqlite3_mprintf(argv[1], a[0], a[1], a[2]); - Tcl_AppendResult(interp, z, 0); + Tcl_AppendResult(interp, z, NULL); sqlite3_free(z); return TCL_OK; } @@ -1320,7 +1516,7 @@ static int sqlite3_mprintf_long( ** ** Call mprintf with two integer arguments and one string argument */ -static int sqlite3_mprintf_str( +static int SQLITE_TCLAPI sqlite3_mprintf_str( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -1337,7 +1533,7 @@ static int sqlite3_mprintf_str( if( Tcl_GetInt(interp, argv[i], &a[i-2]) ) return TCL_ERROR; } z = sqlite3_mprintf(argv[1], a[0], a[1], argc>4 ? argv[4] : NULL); - Tcl_AppendResult(interp, z, 0); + Tcl_AppendResult(interp, z, NULL); sqlite3_free(z); return TCL_OK; } @@ -1347,7 +1543,7 @@ static int sqlite3_mprintf_str( ** ** Call mprintf with two integer arguments and one string argument */ -static int sqlite3_snprintf_str( +static int SQLITE_TCLAPI sqlite3_snprintf_str( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -1363,7 +1559,7 @@ static int sqlite3_snprintf_str( } if( Tcl_GetInt(interp, argv[1], &n) ) return TCL_ERROR; if( n<0 ){ - Tcl_AppendResult(interp, "N must be non-negative", 0); + Tcl_AppendResult(interp, "N must be non-negative", NULL); return TCL_ERROR; } for(i=3; i<5; i++){ @@ -1371,7 +1567,7 @@ static int sqlite3_snprintf_str( } z = sqlite3_malloc( n+1 ); sqlite3_snprintf(n, z, argv[2], a[0], a[1], argc>4 ? argv[5] : NULL); - Tcl_AppendResult(interp, z, 0); + Tcl_AppendResult(interp, z, NULL); sqlite3_free(z); return TCL_OK; } @@ -1381,7 +1577,7 @@ static int sqlite3_snprintf_str( ** ** Call mprintf with two integer arguments and one double argument */ -static int sqlite3_mprintf_double( +static int SQLITE_TCLAPI sqlite3_mprintf_double( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -1400,7 +1596,7 @@ static int sqlite3_mprintf_double( } if( Tcl_GetDouble(interp, argv[4], &r) ) return TCL_ERROR; z = sqlite3_mprintf(argv[1], a[0], a[1], r); - Tcl_AppendResult(interp, z, 0); + Tcl_AppendResult(interp, z, NULL); sqlite3_free(z); return TCL_OK; } @@ -1412,7 +1608,7 @@ static int sqlite3_mprintf_double( ** two arguments given above. This is used to generate overflow and underflow ** doubles to test that they are converted properly. */ -static int sqlite3_mprintf_scaled( +static int SQLITE_TCLAPI sqlite3_mprintf_scaled( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -1430,7 +1626,7 @@ static int sqlite3_mprintf_scaled( if( Tcl_GetDouble(interp, argv[i], &r[i-2]) ) return TCL_ERROR; } z = sqlite3_mprintf(argv[1], r[0]*r[1]); - Tcl_AppendResult(interp, z, 0); + Tcl_AppendResult(interp, z, NULL); sqlite3_free(z); return TCL_OK; } @@ -1442,7 +1638,7 @@ static int sqlite3_mprintf_scaled( ** two arguments given above. This is used to generate overflow and underflow ** doubles to test that they are converted properly. */ -static int sqlite3_mprintf_stronly( +static int SQLITE_TCLAPI sqlite3_mprintf_stronly( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -1455,7 +1651,7 @@ static int sqlite3_mprintf_stronly( return TCL_ERROR; } z = sqlite3_mprintf(argv[1], argv[2]); - Tcl_AppendResult(interp, z, 0); + Tcl_AppendResult(interp, z, NULL); sqlite3_free(z); return TCL_OK; } @@ -1466,7 +1662,7 @@ static int sqlite3_mprintf_stronly( ** Call mprintf with a single double argument which is derived from the ** hexadecimal encoding of an IEEE double. */ -static int sqlite3_mprintf_hexdouble( +static int SQLITE_TCLAPI sqlite3_mprintf_hexdouble( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -1482,14 +1678,14 @@ static int sqlite3_mprintf_hexdouble( return TCL_ERROR; } if( sscanf(argv[2], "%08x%08x", &x2, &x1)!=2 ){ - Tcl_AppendResult(interp, "2nd argument should be 16-characters of hex", 0); + Tcl_AppendResult(interp, "2nd argument should be 16-characters of hex", NULL); return TCL_ERROR; } d = x2; d = (d<<32) + x1; memcpy(&r, &d, sizeof(r)); z = sqlite3_mprintf(argv[1], r); - Tcl_AppendResult(interp, z, 0); + Tcl_AppendResult(interp, z, NULL); sqlite3_free(z); return TCL_OK; } @@ -1499,7 +1695,7 @@ static int sqlite3_mprintf_hexdouble( ** */ #if !defined(SQLITE_OMIT_SHARED_CACHE) -static int test_enable_shared( +static int SQLITE_TCLAPI test_enable_shared( ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ @@ -1536,7 +1732,7 @@ static int test_enable_shared( ** Usage: sqlite3_extended_result_codes DB BOOLEAN ** */ -static int test_extended_result_codes( +static int SQLITE_TCLAPI test_extended_result_codes( ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ @@ -1559,7 +1755,7 @@ static int test_extended_result_codes( ** Usage: sqlite3_libversion_number ** */ -static int test_libversion_number( +static int SQLITE_TCLAPI test_libversion_number( ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ @@ -1573,7 +1769,7 @@ static int test_libversion_number( ** Usage: sqlite3_table_column_metadata DB dbname tblname colname ** */ -static int test_table_column_metadata( +static int SQLITE_TCLAPI test_table_column_metadata( ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ @@ -1607,7 +1803,7 @@ static int test_table_column_metadata( &zDatatype, &zCollseq, &notnull, &primarykey, &autoincrement); if( rc!=SQLITE_OK ){ - Tcl_AppendResult(interp, sqlite3_errmsg(db), 0); + Tcl_AppendResult(interp, sqlite3_errmsg(db), NULL); return TCL_ERROR; } @@ -1624,13 +1820,13 @@ static int test_table_column_metadata( #ifndef SQLITE_OMIT_INCRBLOB -static int blobHandleFromObj( +static int SQLITE_TCLAPI blobHandleFromObj( Tcl_Interp *interp, Tcl_Obj *pObj, sqlite3_blob **ppBlob ){ char *z; - int n; + Tcl_Size n; z = Tcl_GetStringFromObj(pObj, &n); if( n==0 ){ @@ -1653,7 +1849,7 @@ static int blobHandleFromObj( return TCL_OK; } -static int test_blob_reopen( +static int SQLITE_TCLAPI test_blob_reopen( ClientData clientData, /* Not used */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ @@ -1729,7 +1925,7 @@ static int testCreateCollationCmp( return iRes; } -static int test_create_collation_v2( +static int SQLITE_TCLAPI test_create_collation_v2( ClientData clientData, /* Not used */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ @@ -1804,7 +2000,7 @@ static void cf2Destroy(void *pUser){ if( p->pDestroy ) Tcl_DecrRefCount(p->pDestroy); sqlite3_free(p); } -static int test_create_function_v2( +static int SQLITE_TCLAPI test_create_function_v2( ClientData clientData, /* Not used */ Tcl_Interp *interp, /* The invoking TCL interpreter */ int objc, /* Number of arguments */ @@ -1827,7 +2023,8 @@ static int test_create_function_v2( {"utf16le", SQLITE_UTF16LE }, {"utf16be", SQLITE_UTF16BE }, {"any", SQLITE_ANY }, - {"0", 0 } + {"0", 0 }, + {0, 0 } }; if( objc<5 || (objc%2)==0 ){ @@ -1883,7 +2080,7 @@ static int test_create_function_v2( ); if( rc!=SQLITE_OK ){ Tcl_ResetResult(interp); - Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); + Tcl_AppendResult(interp, sqlite3ErrName(rc), NULL); return TCL_ERROR; } return TCL_OK; @@ -1892,7 +2089,7 @@ static int test_create_function_v2( /* ** Usage: sqlite3_load_extension DB-HANDLE FILE ?PROC? */ -static int test_load_extension( +static int SQLITE_TCLAPI test_load_extension( ClientData clientData, /* Not used */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ @@ -1931,6 +2128,8 @@ static int test_load_extension( #ifdef SQLITE_OMIT_LOAD_EXTENSION rc = SQLITE_ERROR; zErr = sqlite3_mprintf("this build omits sqlite3_load_extension()"); + (void)zProc; + (void)zFile; #else rc = sqlite3_load_extension(db, zFile, zProc, &zErr); #endif @@ -1948,7 +2147,7 @@ static int test_load_extension( /* ** Usage: sqlite3_enable_load_extension DB-HANDLE ONOFF */ -static int test_enable_load( +static int SQLITE_TCLAPI test_enable_load( ClientData clientData, /* Not used */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ @@ -1994,7 +2193,7 @@ static int test_enable_load( ** This command is used to test the recoverability of a database in ** the event of a program crash. */ -static int sqlite_abort( +static int SQLITE_TCLAPI sqlite_abort( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -2053,7 +2252,7 @@ static void testFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ ** ** Register the test SQL function on the database DB under the name NAME. */ -static int test_register_func( +static int SQLITE_TCLAPI test_register_func( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -2070,7 +2269,7 @@ static int test_register_func( rc = sqlite3_create_function(db, argv[2], -1, SQLITE_UTF8, 0, testFunc, 0, 0); if( rc!=0 ){ - Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0); + Tcl_AppendResult(interp, sqlite3ErrStr(rc), NULL); return TCL_ERROR; } if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; @@ -2082,7 +2281,7 @@ static int test_register_func( ** ** Finalize a statement handle. */ -static int test_finalize( +static int SQLITE_TCLAPI test_finalize( void * clientData, Tcl_Interp *interp, int objc, @@ -2114,7 +2313,7 @@ static int test_finalize( ** ** Get the value of a status counter from a statement. */ -static int test_stmt_status( +static int SQLITE_TCLAPI test_stmt_status( void * clientData, Tcl_Interp *interp, int objc, @@ -2133,6 +2332,9 @@ static int test_stmt_status( { "SQLITE_STMTSTATUS_SORT", SQLITE_STMTSTATUS_SORT }, { "SQLITE_STMTSTATUS_AUTOINDEX", SQLITE_STMTSTATUS_AUTOINDEX }, { "SQLITE_STMTSTATUS_VM_STEP", SQLITE_STMTSTATUS_VM_STEP }, + { "SQLITE_STMTSTATUS_REPREPARE", SQLITE_STMTSTATUS_REPREPARE }, + { "SQLITE_STMTSTATUS_RUN", SQLITE_STMTSTATUS_RUN }, + { "SQLITE_STMTSTATUS_MEMUSED", SQLITE_STMTSTATUS_MEMUSED }, }; if( objc!=4 ){ Tcl_WrongNumArgs(interp, 1, objv, "STMT PARAMETER RESETFLAG"); @@ -2157,9 +2359,9 @@ static int test_stmt_status( #ifdef SQLITE_ENABLE_STMT_SCANSTATUS /* -** Usage: sqlite3_stmt_scanstatus STMT IDX +** Usage: sqlite3_stmt_scanstatus ?-flags FLAGS? STMT IDX */ -static int test_stmt_scanstatus( +static int SQLITE_TCLAPI test_stmt_scanstatus( void * clientData, Tcl_Interp *interp, int objc, @@ -2172,36 +2374,142 @@ static int test_stmt_scanstatus( const char *zExplain; sqlite3_int64 nLoop; sqlite3_int64 nVisit; + sqlite3_int64 nCycle; double rEst; int res; + int flags = 0; + int iSelectId = 0; + int iParentId = 0; + int bDebug = 0; - if( objc!=3 ){ - Tcl_WrongNumArgs(interp, 1, objv, "STMT IDX"); + if( objc==5 ){ + struct Flag { + const char *zFlag; + int flag; + } aTbl[] = { + {"complex", SQLITE_SCANSTAT_COMPLEX}, + {"debug", -1}, + {0, 0} + }; + + Tcl_Obj **aFlag = 0; + Tcl_Size nFlag = 0; + int ii; + + if( Tcl_ListObjGetElements(interp, objv[2], &nFlag, &aFlag) ){ + return TCL_ERROR; + } + for(ii=0; ii<(int)nFlag; ii++){ + int iVal = 0; + res = Tcl_GetIndexFromObjStruct( + interp, aFlag[ii], aTbl, sizeof(aTbl[0]), "flag", 0, &iVal + ); + if( res ) return TCL_ERROR; + if( aTbl[iVal].flag==-1 ){ + bDebug = 1; + }else{ + flags |= aTbl[iVal].flag; + } + } + } + + if( objc!=3 && objc!=5 ){ + Tcl_WrongNumArgs(interp, 1, objv, "-flags FLAGS STMT IDX"); + return TCL_ERROR; + } + if( getStmtPointer(interp, Tcl_GetString(objv[objc-2]), &pStmt) + || Tcl_GetIntFromObj(interp, objv[objc-1], &idx) + ){ + return TCL_ERROR; + } + + if( bDebug && 0==(flags & SQLITE_SCANSTAT_COMPLEX) ){ + Tcl_SetObjResult(interp, + Tcl_NewStringObj("may not specify debug without complex", -1) + ); return TCL_ERROR; } - if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; - if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; - res = sqlite3_stmt_scanstatus(pStmt, idx, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop); - if( res==0 ){ + if( idx<0 ){ Tcl_Obj *pRet = Tcl_NewObj(); - Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("nLoop", -1)); - Tcl_ListObjAppendElement(0, pRet, Tcl_NewWideIntObj(nLoop)); - sqlite3_stmt_scanstatus(pStmt, idx, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit); - Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("nVisit", -1)); - Tcl_ListObjAppendElement(0, pRet, Tcl_NewWideIntObj(nVisit)); - sqlite3_stmt_scanstatus(pStmt, idx, SQLITE_SCANSTAT_EST, (void*)&rEst); - Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("nEst", -1)); - Tcl_ListObjAppendElement(0, pRet, Tcl_NewDoubleObj(rEst)); - sqlite3_stmt_scanstatus(pStmt, idx, SQLITE_SCANSTAT_NAME, (void*)&zName); - Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("zName", -1)); - Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zName, -1)); - sqlite3_stmt_scanstatus(pStmt, idx, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain); - Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("zExplain", -1)); - Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zExplain, -1)); + res = sqlite3_stmt_scanstatus_v2( + pStmt, -1, SQLITE_SCANSTAT_NCYCLE, flags, (void*)&nCycle + ); + sqlite3_stmt_scanstatus_v2( + pStmt, idx, SQLITE_SCANSTAT_NCYCLE, flags, (void*)&nCycle); + Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("nCycle", -1)); + Tcl_ListObjAppendElement(0, pRet, Tcl_NewWideIntObj(nCycle)); Tcl_SetObjResult(interp, pRet); }else{ - Tcl_ResetResult(interp); + res = sqlite3_stmt_scanstatus_v2( + pStmt, idx, SQLITE_SCANSTAT_NLOOP, flags, (void*)&nLoop + ); + if( res==0 ){ + Tcl_Obj *pRet = Tcl_NewObj(); + Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("nLoop", -1)); + Tcl_ListObjAppendElement(0, pRet, Tcl_NewWideIntObj(nLoop)); + sqlite3_stmt_scanstatus_v2( + pStmt, idx, SQLITE_SCANSTAT_NVISIT, flags, (void*)&nVisit); + Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("nVisit", -1)); + Tcl_ListObjAppendElement(0, pRet, Tcl_NewWideIntObj(nVisit)); + sqlite3_stmt_scanstatus_v2( + pStmt, idx, SQLITE_SCANSTAT_EST, flags, (void*)&rEst); + Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("nEst", -1)); + Tcl_ListObjAppendElement(0, pRet, Tcl_NewDoubleObj(rEst)); + sqlite3_stmt_scanstatus_v2( + pStmt, idx, SQLITE_SCANSTAT_NAME, flags, (void*)&zName); + Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("zName", -1)); + Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zName, -1)); + sqlite3_stmt_scanstatus_v2( + pStmt, idx, SQLITE_SCANSTAT_EXPLAIN, flags, (void*)&zExplain); + Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("zExplain", -1)); + Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zExplain, -1)); + sqlite3_stmt_scanstatus_v2( + pStmt, idx, SQLITE_SCANSTAT_SELECTID, flags, (void*)&iSelectId); + Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("iSelectId", -1)); + Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(iSelectId)); + sqlite3_stmt_scanstatus_v2( + pStmt, idx, SQLITE_SCANSTAT_PARENTID, flags, (void*)&iParentId); + Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("iParentId", -1)); + Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(iParentId)); + sqlite3_stmt_scanstatus_v2( + pStmt, idx, SQLITE_SCANSTAT_NCYCLE, flags, (void*)&nCycle); + Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("nCycle", -1)); + Tcl_ListObjAppendElement(0, pRet, Tcl_NewWideIntObj(nCycle)); + + if( bDebug ){ + int ii; + ScanStatus *pScan = &((Vdbe*)pStmt)->aScan[idx]; + Tcl_Obj *pRange = Tcl_NewObj(); + Tcl_Obj *pCsr = Tcl_NewObj(); + + Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("debug_loop", -1)); + Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(pScan->addrLoop)); + Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("debug_visit", -1)); + Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(pScan->addrVisit)); + Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("debug_explain",-1)); + Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(pScan->addrExplain)); + for(ii=0; ii<ArraySize(pScan->aAddrRange)/2; ii++){ + int iStart = pScan->aAddrRange[ii*2]; + int iEnd = pScan->aAddrRange[ii*2+1]; + if( iStart>0 ){ + Tcl_ListObjAppendElement(0, pRange, Tcl_NewIntObj(iStart)); + Tcl_ListObjAppendElement(0, pRange, Tcl_NewIntObj(iEnd)); + }else if( iStart<0 ){ + Tcl_ListObjAppendElement(0, pCsr, Tcl_NewIntObj(iEnd)); + } + } + + Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("debug_range", -1)); + Tcl_ListObjAppendElement(0, pRet, pRange); + Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj("debug_csr", -1)); + Tcl_ListObjAppendElement(0, pRet, pCsr); + } + + Tcl_SetObjResult(interp, pRet); + }else{ + Tcl_ResetResult(interp); + } } return TCL_OK; } @@ -2209,7 +2517,7 @@ static int test_stmt_scanstatus( /* ** Usage: sqlite3_stmt_scanstatus_reset STMT */ -static int test_stmt_scanstatus_reset( +static int SQLITE_TCLAPI test_stmt_scanstatus_reset( void * clientData, Tcl_Interp *interp, int objc, @@ -2232,7 +2540,7 @@ static int test_stmt_scanstatus_reset( ** ** Zero the SQLITE_CONFIG_SQLLOG configuration */ -static int test_config_sqllog( +static int SQLITE_TCLAPI test_config_sqllog( void * clientData, Tcl_Interp *interp, int objc, @@ -2247,12 +2555,33 @@ static int test_config_sqllog( } #endif +/* +** Usage: sqlite3_config_sorterref +** +** Set the SQLITE_CONFIG_SORTERREF_SIZE configuration option +*/ +static int SQLITE_TCLAPI test_config_sorterref( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + int iVal; + if( objc!=2 ){ + Tcl_WrongNumArgs(interp, 1, objv, "NBYTE"); + return TCL_ERROR; + } + if( Tcl_GetIntFromObj(interp, objv[1], &iVal) ) return TCL_ERROR; + sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, iVal); + return TCL_OK; +} + /* ** Usage: vfs_current_time_int64 ** ** Return the value returned by the default VFS's xCurrentTimeInt64 method. */ -static int vfsCurrentTimeInt64( +static int SQLITE_TCLAPI vfsCurrentTimeInt64( void * clientData, Tcl_Interp *interp, int objc, @@ -2269,11 +2598,36 @@ static int vfsCurrentTimeInt64( return TCL_OK; } +#ifndef SQLITE_OMIT_VIRTUALTABLE +/* +** Usage: create_null_module DB NAME +*/ +static int SQLITE_TCLAPI test_create_null_module( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + sqlite3 *db; + char *zName; + + if( objc!=3 ){ + Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME"); + return TCL_ERROR; + } + if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; + zName = Tcl_GetString(objv[2]); + + sqlite3_create_module(db, zName, 0, 0); + return TCL_OK; +} +#endif /* SQLITE_OMIT_VIRTUALTABLE */ + #ifdef SQLITE_ENABLE_SNAPSHOT /* ** Usage: sqlite3_snapshot_get DB DBNAME */ -static int test_snapshot_get( +static int SQLITE_TCLAPI test_snapshot_get( void * clientData, Tcl_Interp *interp, int objc, @@ -2304,11 +2658,43 @@ static int test_snapshot_get( } #endif /* SQLITE_ENABLE_SNAPSHOT */ +#ifdef SQLITE_ENABLE_SNAPSHOT +/* +** Usage: sqlite3_snapshot_recover DB DBNAME +*/ +static int SQLITE_TCLAPI test_snapshot_recover( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + int rc; + sqlite3 *db; + char *zName; + + if( objc!=3 ){ + Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME"); + return TCL_ERROR; + } + if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; + zName = Tcl_GetString(objv[2]); + + rc = sqlite3_snapshot_recover(db, zName); + if( rc!=SQLITE_OK ){ + Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1)); + return TCL_ERROR; + }else{ + Tcl_ResetResult(interp); + } + return TCL_OK; +} +#endif /* SQLITE_ENABLE_SNAPSHOT */ + #ifdef SQLITE_ENABLE_SNAPSHOT /* ** Usage: sqlite3_snapshot_open DB DBNAME SNAPSHOT */ -static int test_snapshot_open( +static int SQLITE_TCLAPI test_snapshot_open( void * clientData, Tcl_Interp *interp, int objc, @@ -2331,6 +2717,8 @@ static int test_snapshot_open( if( rc!=SQLITE_OK ){ Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1)); return TCL_ERROR; + }else{ + Tcl_ResetResult(interp); } return TCL_OK; } @@ -2340,7 +2728,7 @@ static int test_snapshot_open( /* ** Usage: sqlite3_snapshot_free SNAPSHOT */ -static int test_snapshot_free( +static int SQLITE_TCLAPI test_snapshot_free( void * clientData, Tcl_Interp *interp, int objc, @@ -2357,87 +2745,337 @@ static int test_snapshot_free( } #endif /* SQLITE_ENABLE_SNAPSHOT */ +#ifdef SQLITE_ENABLE_SNAPSHOT /* -** Usage: sqlite3_next_stmt DB STMT -** -** Return the next statment in sequence after STMT. +** Usage: sqlite3_snapshot_cmp SNAPSHOT1 SNAPSHOT2 */ -static int test_next_stmt( +static int SQLITE_TCLAPI test_snapshot_cmp( void * clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[] ){ - sqlite3_stmt *pStmt; - sqlite3 *db = 0; - char zBuf[50]; - + int res; + sqlite3_snapshot *p1; + sqlite3_snapshot *p2; if( objc!=3 ){ - Tcl_AppendResult(interp, "wrong # args: should be \"", - Tcl_GetStringFromObj(objv[0], 0), " DB STMT", 0); + Tcl_WrongNumArgs(interp, 1, objv, "SNAPSHOT1 SNAPSHOT2"); return TCL_ERROR; } - - if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; - if( getStmtPointer(interp, Tcl_GetString(objv[2]), &pStmt) ) return TCL_ERROR; - pStmt = sqlite3_next_stmt(db, pStmt); - if( pStmt ){ - if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; - Tcl_AppendResult(interp, zBuf, 0); - } + p1 = (sqlite3_snapshot*)sqlite3TestTextToPtr(Tcl_GetString(objv[1])); + p2 = (sqlite3_snapshot*)sqlite3TestTextToPtr(Tcl_GetString(objv[2])); + res = sqlite3_snapshot_cmp(p1, p2); + Tcl_SetObjResult(interp, Tcl_NewIntObj(res)); return TCL_OK; } +#endif /* SQLITE_ENABLE_SNAPSHOT */ +#ifdef SQLITE_ENABLE_SNAPSHOT /* -** Usage: sqlite3_stmt_readonly STMT -** -** Return true if STMT is a NULL pointer or a pointer to a statement -** that is guaranteed to leave the database unmodified. +** Usage: sqlite3_snapshot_get_blob DB DBNAME */ -static int test_stmt_readonly( +static int SQLITE_TCLAPI test_snapshot_get_blob( void * clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[] ){ - sqlite3_stmt *pStmt; int rc; + sqlite3 *db; + char *zName; + sqlite3_snapshot *pSnapshot = 0; - if( objc!=2 ){ - Tcl_AppendResult(interp, "wrong # args: should be \"", - Tcl_GetStringFromObj(objv[0], 0), " STMT", 0); + if( objc!=3 ){ + Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME"); return TCL_ERROR; } + if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; + zName = Tcl_GetString(objv[2]); - if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; - rc = sqlite3_stmt_readonly(pStmt); - Tcl_SetObjResult(interp, Tcl_NewBooleanObj(rc)); + rc = sqlite3_snapshot_get(db, zName, &pSnapshot); + if( rc!=SQLITE_OK ){ + Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1)); + return TCL_ERROR; + }else{ + Tcl_SetObjResult(interp, + Tcl_NewByteArrayObj((unsigned char*)pSnapshot, sizeof(sqlite3_snapshot)) + ); + sqlite3_snapshot_free(pSnapshot); + } return TCL_OK; } +#endif /* SQLITE_ENABLE_SNAPSHOT */ -/* -** Usage: sqlite3_stmt_busy STMT -** -** Return true if STMT is a non-NULL pointer to a statement -** that has been stepped but not to completion. +#ifdef SQLITE_ENABLE_SNAPSHOT + /* + ** Usage: sqlite3_snapshot_open_blob DB DBNAME SNAPSHOT */ -static int test_stmt_busy( +static int SQLITE_TCLAPI test_snapshot_open_blob( void * clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[] ){ - sqlite3_stmt *pStmt; int rc; + sqlite3 *db; + char *zName; + unsigned char *pBlob; + Tcl_Size nBlob; - if( objc!=2 ){ - Tcl_AppendResult(interp, "wrong # args: should be \"", - Tcl_GetStringFromObj(objv[0], 0), " STMT", 0); + if( objc!=4 ){ + Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME SNAPSHOT"); return TCL_ERROR; } - - if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; - rc = sqlite3_stmt_busy(pStmt); + if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; + zName = Tcl_GetString(objv[2]); + pBlob = Tcl_GetByteArrayFromObj(objv[3], &nBlob); + if( nBlob!=sizeof(sqlite3_snapshot) ){ + Tcl_AppendResult(interp, "bad SNAPSHOT", NULL); + return TCL_ERROR; + } + rc = sqlite3_snapshot_open(db, zName, (sqlite3_snapshot*)pBlob); + if( rc!=SQLITE_OK ){ + Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1)); + return TCL_ERROR; + } + return TCL_OK; +} +#endif /* SQLITE_ENABLE_SNAPSHOT */ + +#ifdef SQLITE_ENABLE_SNAPSHOT +/* +** Usage: sqlite3_snapshot_cmp_blob SNAPSHOT1 SNAPSHOT2 +*/ +static int SQLITE_TCLAPI test_snapshot_cmp_blob( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + int res; + unsigned char *p1; + unsigned char *p2; + Tcl_Size n1; + Tcl_Size n2; + + if( objc!=3 ){ + Tcl_WrongNumArgs(interp, 1, objv, "SNAPSHOT1 SNAPSHOT2"); + return TCL_ERROR; + } + + p1 = Tcl_GetByteArrayFromObj(objv[1], &n1); + p2 = Tcl_GetByteArrayFromObj(objv[2], &n2); + + if( n1!=sizeof(sqlite3_snapshot) || n1!=n2 ){ + Tcl_AppendResult(interp, "bad SNAPSHOT", NULL); + return TCL_ERROR; + } + + res = sqlite3_snapshot_cmp((sqlite3_snapshot*)p1, (sqlite3_snapshot*)p2); + Tcl_SetObjResult(interp, Tcl_NewIntObj(res)); + return TCL_OK; +} +#endif /* SQLITE_ENABLE_SNAPSHOT */ + +/* +** Usage: sqlite3_delete_database FILENAME +*/ +int sqlite3_delete_database(const char*); /* in test_delete.c */ +static int SQLITE_TCLAPI test_delete_database( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + int rc; + const char *zFile; + if( objc!=2 ){ + Tcl_WrongNumArgs(interp, 1, objv, "FILE"); + return TCL_ERROR; + } + zFile = (const char*)Tcl_GetString(objv[1]); + rc = sqlite3_delete_database(zFile); + + Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1)); + return TCL_OK; +} + +/* +** Usage: atomic_batch_write PATH +*/ +static int SQLITE_TCLAPI test_atomic_batch_write( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + char *zFile = 0; /* Path to file to test */ + sqlite3 *db = 0; /* Database handle */ + sqlite3_file *pFd = 0; /* SQLite fd open on zFile */ + int bRes = 0; /* Integer result of this command */ + int dc = 0; /* Device-characteristics mask */ + int rc; /* sqlite3_open() return code */ + + if( objc!=2 ){ + Tcl_WrongNumArgs(interp, 1, objv, "PATH"); + return TCL_ERROR; + } + zFile = Tcl_GetString(objv[1]); + + rc = sqlite3_open(zFile, &db); + if( rc!=SQLITE_OK ){ + Tcl_AppendResult(interp, sqlite3_errmsg(db), NULL); + sqlite3_close(db); + return TCL_ERROR; + } + + rc = sqlite3_file_control(db, "main", SQLITE_FCNTL_FILE_POINTER, (void*)&pFd); + dc = pFd->pMethods->xDeviceCharacteristics(pFd); + if( dc & SQLITE_IOCAP_BATCH_ATOMIC ){ + bRes = 1; + } + + Tcl_SetObjResult(interp, Tcl_NewIntObj(bRes)); + sqlite3_close(db); + return TCL_OK; +} + +/* +** Usage: sqlite3_next_stmt DB STMT +** +** Return the next statement in sequence after STMT. +*/ +static int SQLITE_TCLAPI test_next_stmt( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + sqlite3_stmt *pStmt; + sqlite3 *db = 0; + char zBuf[50]; + + if( objc!=3 ){ + Tcl_AppendResult(interp, "wrong # args: should be \"", + Tcl_GetStringFromObj(objv[0], 0), " DB STMT", 0); + return TCL_ERROR; + } + + if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; + if( getStmtPointer(interp, Tcl_GetString(objv[2]), &pStmt) ) return TCL_ERROR; + pStmt = sqlite3_next_stmt(db, pStmt); + if( pStmt ){ + if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; + Tcl_AppendResult(interp, zBuf, NULL); + } + return TCL_OK; +} + +/* +** Usage: sqlite3_stmt_readonly STMT +** +** Return true if STMT is a NULL pointer or a pointer to a statement +** that is guaranteed to leave the database unmodified. +*/ +static int SQLITE_TCLAPI test_stmt_readonly( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + sqlite3_stmt *pStmt; + int rc; + + if( objc!=2 ){ + Tcl_AppendResult(interp, "wrong # args: should be \"", + Tcl_GetStringFromObj(objv[0], 0), " STMT", 0); + return TCL_ERROR; + } + + if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; + rc = sqlite3_stmt_readonly(pStmt); + Tcl_SetObjResult(interp, Tcl_NewBooleanObj(rc)); + return TCL_OK; +} + +/* +** Usage: sqlite3_stmt_isexplain STMT +** +** Return 1, 2, or 0 respectively if STMT is an EXPLAIN statement, an +** EXPLAIN QUERY PLAN statement or an ordinary statement or NULL pointer. +*/ +static int SQLITE_TCLAPI test_stmt_isexplain( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + sqlite3_stmt *pStmt; + int rc; + + if( objc!=2 ){ + Tcl_AppendResult(interp, "wrong # args: should be \"", + Tcl_GetStringFromObj(objv[0], 0), " STMT", 0); + return TCL_ERROR; + } + + if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; + rc = sqlite3_stmt_isexplain(pStmt); + Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); + return TCL_OK; +} + +/* +** Usage: sqlite3_stmt_explain STMT INT +** +** Set the explain to normal (0), EXPLAIN (1) or EXPLAIN QUERY PLAN (2). +*/ +static int SQLITE_TCLAPI test_stmt_explain( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + sqlite3_stmt *pStmt; + int eMode = 0; + int rc; + + if( objc!=3 ){ + Tcl_AppendResult(interp, "wrong # args: should be \"", + Tcl_GetStringFromObj(objv[0], 0), " STMT INT", 0); + return TCL_ERROR; + } + + if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; + if( Tcl_GetIntFromObj(interp, objv[2], &eMode) ) return TCL_ERROR; + rc = sqlite3_stmt_explain(pStmt, eMode); + Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); + return TCL_OK; +} + +/* +** Usage: sqlite3_stmt_busy STMT +** +** Return true if STMT is a non-NULL pointer to a statement +** that has been stepped but not to completion. +*/ +static int SQLITE_TCLAPI test_stmt_busy( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + sqlite3_stmt *pStmt; + int rc; + + if( objc!=2 ){ + Tcl_AppendResult(interp, "wrong # args: should be \"", + Tcl_GetStringFromObj(objv[0], 0), " STMT", 0); + return TCL_ERROR; + } + + if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; + rc = sqlite3_stmt_busy(pStmt); Tcl_SetObjResult(interp, Tcl_NewBooleanObj(rc)); return TCL_OK; } @@ -2447,7 +3085,7 @@ static int test_stmt_busy( ** ** Return true if STMT uses a statement journal. */ -static int uses_stmt_journal( +static int SQLITE_TCLAPI uses_stmt_journal( void * clientData, Tcl_Interp *interp, int objc, @@ -2473,7 +3111,7 @@ static int uses_stmt_journal( ** ** Reset a statement handle. */ -static int test_reset( +static int SQLITE_TCLAPI test_reset( void * clientData, Tcl_Interp *interp, int objc, @@ -2508,7 +3146,7 @@ static int test_reset( ** ** Return TRUE if a recompilation of the statement is recommended. */ -static int test_expired( +static int SQLITE_TCLAPI test_expired( void * clientData, Tcl_Interp *interp, int objc, @@ -2532,7 +3170,7 @@ static int test_expired( ** ** Transfer all bindings from FROMSTMT over to TOSTMT */ -static int test_transfer_bind( +static int SQLITE_TCLAPI test_transfer_bind( void * clientData, Tcl_Interp *interp, int objc, @@ -2559,7 +3197,7 @@ static int test_transfer_bind( ** Return the number of changes made to the database by the last SQL ** execution. */ -static int test_changes( +static int SQLITE_TCLAPI test_changes( void * clientData, Tcl_Interp *interp, int objc, @@ -2594,7 +3232,7 @@ static int sqlite_static_bind_nbyte = 0; ** of the VALUE is made. If FLAGS=="blob10" then a VALUE is ignored ** an a 10-byte blob "abc\000xyz\000pq" is inserted. */ -static int test_bind( +static int SQLITE_TCLAPI test_bind( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -2630,7 +3268,7 @@ static int test_bind( if( rc ){ char zBuf[50]; sqlite3_snprintf(sizeof(zBuf), zBuf, "(%d) ", rc); - Tcl_AppendResult(interp, zBuf, sqlite3ErrStr(rc), 0); + Tcl_AppendResult(interp, zBuf, sqlite3ErrStr(rc), NULL); return TCL_ERROR; } return TCL_OK; @@ -2716,7 +3354,7 @@ static int test_collate_func( Tcl_GetIntFromObj(i, Tcl_GetObjResult(i), &res); return res; } -static int test_collate( +static int SQLITE_TCLAPI test_collate( void * clientData, Tcl_Interp *interp, int objc, @@ -2762,14 +3400,14 @@ static int test_collate( if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; if( rc!=SQLITE_OK ){ - Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); + Tcl_AppendResult(interp, sqlite3ErrName(rc), NULL); return TCL_ERROR; } return TCL_OK; bad_args: Tcl_AppendResult(interp, "wrong # args: should be \"", - Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0); + Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", NULL); return TCL_ERROR; } @@ -2790,7 +3428,7 @@ static int test_utf16bin_collate_func( if( res==0 ) res = nA - nB; return res; } -static int test_utf16bin_collate( +static int SQLITE_TCLAPI test_utf16bin_collate( void * clientData, Tcl_Interp *interp, int objc, @@ -2847,7 +3485,7 @@ static void test_collate_needed_cb( /* ** Usage: add_test_collate_needed DB */ -static int test_collate_needed( +static int SQLITE_TCLAPI test_collate_needed( void * clientData, Tcl_Interp *interp, int objc, @@ -2898,7 +3536,7 @@ static int alignmentCollFunc( } return rc; } -static int add_alignment_test_collations( +static int SQLITE_TCLAPI add_alignment_test_collations( void * clientData, Tcl_Interp *interp, int objc, @@ -3016,7 +3654,7 @@ static void test_function_utf16be( sqlite3ValueFree(pVal); } #endif /* SQLITE_OMIT_UTF16 */ -static int test_function( +static int SQLITE_TCLAPI test_function( void * clientData, Tcl_Interp *interp, int objc, @@ -3048,7 +3686,7 @@ static int test_function( return TCL_OK; bad_args: Tcl_AppendResult(interp, "wrong # args: should be \"", - Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", 0); + Tcl_GetStringFromObj(objv[0], 0), " <DB> <utf8> <utf16le> <utf16be>", NULL); #endif /* SQLITE_OMIT_UTF16 */ return TCL_ERROR; } @@ -3056,12 +3694,12 @@ static int test_function( /* ** Usage: sqlite3_test_errstr <err code> ** -** Test that the english language string equivalents for sqlite error codes +** Test that the English language string equivalents for sqlite error codes ** are sane. The parameter is an integer representing an sqlite error code. ** The result is a list of two elements, the string representation of the -** error code and the english language explanation. +** error code and the English language explanation. */ -static int test_errstr( +static int SQLITE_TCLAPI test_errstr( void * clientData, Tcl_Interp *interp, int objc, @@ -3094,7 +3732,7 @@ static int test_errstr( ** Then run testfixture in the debugger and wait for the breakpoint to ** fire. Then additional breakpoints can be set to trace down the bug. */ -static int test_breakpoint( +static int SQLITE_TCLAPI test_breakpoint( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -3110,7 +3748,7 @@ static int test_breakpoint( ** IDX is the index of a wildcard in the prepared statement. This command ** binds a N-byte zero-filled BLOB to the wildcard. */ -static int test_bind_zeroblob( +static int SQLITE_TCLAPI test_bind_zeroblob( void * clientData, Tcl_Interp *interp, int objc, @@ -3146,7 +3784,7 @@ static int test_bind_zeroblob( ** IDX is the index of a wildcard in the prepared statement. This command ** binds a N-byte zero-filled BLOB to the wildcard. */ -static int test_bind_zeroblob64( +static int SQLITE_TCLAPI test_bind_zeroblob64( void * clientData, Tcl_Interp *interp, int objc, @@ -3154,7 +3792,7 @@ static int test_bind_zeroblob64( ){ sqlite3_stmt *pStmt; int idx; - i64 n; + Tcl_WideInt n; int rc; if( objc!=4 ){ @@ -3169,7 +3807,7 @@ static int test_bind_zeroblob64( rc = sqlite3_bind_zeroblob64(pStmt, idx, n); if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; if( rc!=SQLITE_OK ){ - Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); + Tcl_AppendResult(interp, sqlite3ErrName(rc), NULL); return TCL_ERROR; } @@ -3183,7 +3821,7 @@ static int test_bind_zeroblob64( ** N is the index of a wildcard in the prepared statement. This command ** binds a 32-bit integer VALUE to that wildcard. */ -static int test_bind_int( +static int SQLITE_TCLAPI test_bind_int( void * clientData, Tcl_Interp *interp, int objc, @@ -3196,7 +3834,7 @@ static int test_bind_int( if( objc!=4 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", - Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0); + Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", NULL); return TCL_ERROR; } @@ -3215,87 +3853,225 @@ static int test_bind_int( /* -** Usage: sqlite3_bind_int64 STMT N VALUE +** Usage: intarray_addr INT ... ** -** Test the sqlite3_bind_int64 interface. STMT is a prepared statement. -** N is the index of a wildcard in the prepared statement. This command -** binds a 64-bit integer VALUE to that wildcard. +** Return the address of a C-language array of 32-bit integers. +** +** Space to hold the array is obtained from malloc(). Call this procedure once +** with no arguments in order to release memory. Each call to this procedure +** overwrites the previous array. */ -static int test_bind_int64( +static int SQLITE_TCLAPI test_intarray_addr( void * clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[] ){ - sqlite3_stmt *pStmt; - int idx; - Tcl_WideInt value; - int rc; - - if( objc!=4 ){ - Tcl_AppendResult(interp, "wrong # args: should be \"", - Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0); - return TCL_ERROR; - } - - if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; - if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; - if( Tcl_GetWideIntFromObj(interp, objv[3], &value) ) return TCL_ERROR; - - rc = sqlite3_bind_int64(pStmt, idx, value); - if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; - if( rc!=SQLITE_OK ){ - return TCL_ERROR; - } + int i; + static int *p = 0; + sqlite3_free(p); + p = 0; + if( objc>1 ){ + p = sqlite3_malloc( sizeof(p[0])*(objc-1) ); + if( p==0 ) return TCL_ERROR; + for(i=0; i<objc-1; i++){ + if( Tcl_GetIntFromObj(interp, objv[1+i], &p[i]) ){ + sqlite3_free(p); + p = 0; + return TCL_ERROR; + } + } + } + Tcl_SetObjResult(interp, Tcl_NewWideIntObj((uptr)p)); return TCL_OK; } - - /* -** Usage: sqlite3_bind_double STMT N VALUE +** Usage: int64array_addr INT ... ** -** Test the sqlite3_bind_double interface. STMT is a prepared statement. -** N is the index of a wildcard in the prepared statement. This command -** binds a 64-bit integer VALUE to that wildcard. +** Return the address of a C-language array of 32-bit integers. +** +** Space to hold the array is obtained from malloc(). Call this procedure once +** with no arguments in order to release memory. Each call to this procedure +** overwrites the previous array. */ -static int test_bind_double( +static int SQLITE_TCLAPI test_int64array_addr( void * clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[] ){ - sqlite3_stmt *pStmt; - int idx; - double value = 0; - int rc; - const char *zVal; int i; - static const struct { - const char *zName; /* Name of the special floating point value */ - unsigned int iUpper; /* Upper 32 bits */ - unsigned int iLower; /* Lower 32 bits */ - } aSpecialFp[] = { - { "NaN", 0x7fffffff, 0xffffffff }, - { "SNaN", 0x7ff7ffff, 0xffffffff }, - { "-NaN", 0xffffffff, 0xffffffff }, - { "-SNaN", 0xfff7ffff, 0xffffffff }, - { "+Inf", 0x7ff00000, 0x00000000 }, - { "-Inf", 0xfff00000, 0x00000000 }, - { "Epsilon", 0x00000000, 0x00000001 }, - { "-Epsilon", 0x80000000, 0x00000001 }, - { "NaN0", 0x7ff80000, 0x00000000 }, - { "-NaN0", 0xfff80000, 0x00000000 }, - }; - - if( objc!=4 ){ - Tcl_AppendResult(interp, "wrong # args: should be \"", - Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", 0); - return TCL_ERROR; - } + static sqlite3_int64 *p = 0; - if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; - if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; + sqlite3_free(p); + p = 0; + if( objc>1 ){ + p = sqlite3_malloc( sizeof(p[0])*(objc-1) ); + if( p==0 ) return TCL_ERROR; + for(i=0; i<objc-1; i++){ + Tcl_WideInt v; + if( Tcl_GetWideIntFromObj(interp, objv[1+i], &v) ){ + sqlite3_free(p); + p = 0; + return TCL_ERROR; + } + p[i] = v; + } + } + Tcl_SetObjResult(interp, Tcl_NewWideIntObj((uptr)p)); + return TCL_OK; +} +/* +** Usage: doublearray_addr INT ... +** +** Return the address of a C-language array of doubles. +** +** Space to hold the array is obtained from malloc(). Call this procedure once +** with no arguments in order to release memory. Each call to this procedure +** overwrites the previous array. +*/ +static int SQLITE_TCLAPI test_doublearray_addr( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + int i; + static double *p = 0; + + sqlite3_free(p); + p = 0; + if( objc>1 ){ + p = sqlite3_malloc( sizeof(p[0])*(objc-1) ); + if( p==0 ) return TCL_ERROR; + for(i=0; i<objc-1; i++){ + if( Tcl_GetDoubleFromObj(interp, objv[1+i], &p[i]) ){ + sqlite3_free(p); + p = 0; + return TCL_ERROR; + } + } + } + Tcl_SetObjResult(interp, Tcl_NewWideIntObj((uptr)p)); + return TCL_OK; +} +/* +** Usage: textarray_addr TEXT ... +** +** Return the address of a C-language array of strings. +** +** Space to hold the array is obtained from malloc(). Call this procedure once +** with no arguments in order to release memory. Each call to this procedure +** overwrites the previous array. +*/ +static int SQLITE_TCLAPI test_textarray_addr( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + int i; + static int n = 0; + static char **p = 0; + + for(i=0; i<n; i++) sqlite3_free(p[i]); + sqlite3_free(p); + p = 0; + if( objc>1 ){ + p = sqlite3_malloc( sizeof(p[0])*(objc-1) ); + if( p==0 ) return TCL_ERROR; + for(i=0; i<objc-1; i++){ + p[i] = sqlite3_mprintf("%s", Tcl_GetString(objv[1+i])); + } + } + n = objc-1; + Tcl_SetObjResult(interp, Tcl_NewWideIntObj((uptr)p)); + return TCL_OK; +} + +/* +** Usage: sqlite3_bind_int64 STMT N VALUE +** +** Test the sqlite3_bind_int64 interface. STMT is a prepared statement. +** N is the index of a wildcard in the prepared statement. This command +** binds a 64-bit integer VALUE to that wildcard. +*/ +static int SQLITE_TCLAPI test_bind_int64( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + sqlite3_stmt *pStmt; + int idx; + Tcl_WideInt value; + int rc; + + if( objc!=4 ){ + Tcl_AppendResult(interp, "wrong # args: should be \"", + Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", NULL); + return TCL_ERROR; + } + + if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; + if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; + if( Tcl_GetWideIntFromObj(interp, objv[3], &value) ) return TCL_ERROR; + + rc = sqlite3_bind_int64(pStmt, idx, value); + if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; + if( rc!=SQLITE_OK ){ + return TCL_ERROR; + } + + return TCL_OK; +} + + +/* +** Usage: sqlite3_bind_double STMT N VALUE +** +** Test the sqlite3_bind_double interface. STMT is a prepared statement. +** N is the index of a wildcard in the prepared statement. This command +** binds a 64-bit integer VALUE to that wildcard. +*/ +static int SQLITE_TCLAPI test_bind_double( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + sqlite3_stmt *pStmt; + int idx; + double value = 0; + int rc; + const char *zVal; + int i; + static const struct { + const char *zName; /* Name of the special floating point value */ + unsigned int iUpper; /* Upper 32 bits */ + unsigned int iLower; /* Lower 32 bits */ + } aSpecialFp[] = { + { "NaN", 0x7fffffff, 0xffffffff }, + { "SNaN", 0x7ff7ffff, 0xffffffff }, + { "-NaN", 0xffffffff, 0xffffffff }, + { "-SNaN", 0xfff7ffff, 0xffffffff }, + { "+Inf", 0x7ff00000, 0x00000000 }, + { "-Inf", 0xfff00000, 0x00000000 }, + { "Epsilon", 0x00000000, 0x00000001 }, + { "-Epsilon", 0x80000000, 0x00000001 }, + { "NaN0", 0x7ff80000, 0x00000000 }, + { "-NaN0", 0xfff80000, 0x00000000 }, + }; + + if( objc!=4 ){ + Tcl_AppendResult(interp, "wrong # args: should be \"", + Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE", NULL); + return TCL_ERROR; + } + + if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; + if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; /* Intercept the string "NaN" and generate a NaN value for it. ** All other strings are passed through to Tcl_GetDoubleFromObj(). @@ -3335,7 +4111,7 @@ static int test_bind_double( ** N is the index of a wildcard in the prepared statement. This command ** binds a NULL to the wildcard. */ -static int test_bind_null( +static int SQLITE_TCLAPI test_bind_null( void * clientData, Tcl_Interp *interp, int objc, @@ -3347,7 +4123,7 @@ static int test_bind_null( if( objc!=3 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", - Tcl_GetStringFromObj(objv[0], 0), " STMT N", 0); + Tcl_GetStringFromObj(objv[0], 0), " STMT N", NULL); return TCL_ERROR; } @@ -3371,7 +4147,7 @@ static int test_bind_null( ** binds a UTF-8 string STRING to the wildcard. The string is BYTES bytes ** long. */ -static int test_bind_text( +static int SQLITE_TCLAPI test_bind_text( void * clientData, Tcl_Interp *interp, int objc, @@ -3379,25 +4155,37 @@ static int test_bind_text( ){ sqlite3_stmt *pStmt; int idx; + Tcl_Size trueLength = 0; int bytes; char *value; int rc; + char *toFree = 0; if( objc!=5 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", - Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", 0); + Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", NULL); return TCL_ERROR; } if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; - value = (char*)Tcl_GetByteArrayFromObj(objv[3], &bytes); + value = (char*)Tcl_GetByteArrayFromObj(objv[3], &trueLength); if( Tcl_GetIntFromObj(interp, objv[4], &bytes) ) return TCL_ERROR; - + if( bytes<0 ){ + toFree = malloc( trueLength + 1 ); + if( toFree==0 ){ + Tcl_AppendResult(interp, "out of memory", (void*)0); + return TCL_ERROR; + } + memcpy(toFree, value, trueLength); + toFree[trueLength] = 0; + value = toFree; + } rc = sqlite3_bind_text(pStmt, idx, value, bytes, SQLITE_TRANSIENT); + free(toFree); if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; if( rc!=SQLITE_OK ){ - Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); + Tcl_AppendResult(interp, sqlite3ErrName(rc), (void*)0); return TCL_ERROR; } @@ -3412,7 +4200,7 @@ static int test_bind_text( ** binds a UTF-16 string STRING to the wildcard. The string is BYTES bytes ** long. */ -static int test_bind_text16( +static int SQLITE_TCLAPI test_bind_text16( void * clientData, Tcl_Interp *interp, int objc, @@ -3423,7 +4211,9 @@ static int test_bind_text16( int idx; int bytes; char *value; + char *toFree = 0; int rc; + Tcl_Size trueLength = 0; void (*xDel)(void*) = (objc==6?SQLITE_STATIC:SQLITE_TRANSIENT); Tcl_Obj *oStmt = objv[objc-4]; @@ -3433,19 +4223,29 @@ static int test_bind_text16( if( objc!=5 && objc!=6){ Tcl_AppendResult(interp, "wrong # args: should be \"", - Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", 0); + Tcl_GetStringFromObj(objv[0], 0), " STMT N VALUE BYTES", NULL); return TCL_ERROR; } if( getStmtPointer(interp, Tcl_GetString(oStmt), &pStmt) ) return TCL_ERROR; if( Tcl_GetIntFromObj(interp, oN, &idx) ) return TCL_ERROR; - value = (char*)Tcl_GetByteArrayFromObj(oString, 0); + value = (char*)Tcl_GetByteArrayFromObj(oString, &trueLength); if( Tcl_GetIntFromObj(interp, oBytes, &bytes) ) return TCL_ERROR; - + if( bytes<0 && xDel==SQLITE_TRANSIENT ){ + toFree = malloc( trueLength + 3 ); + if( toFree==0 ){ + Tcl_AppendResult(interp, "out of memory", (void*)0); + return TCL_ERROR; + } + memcpy(toFree, value, trueLength); + memset(toFree+trueLength, 0, 3); + value = toFree; + } rc = sqlite3_bind_text16(pStmt, idx, (void *)value, bytes, xDel); + free(toFree); if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; if( rc!=SQLITE_OK ){ - Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); + Tcl_AppendResult(interp, sqlite3ErrName(rc), NULL); return TCL_ERROR; } @@ -3460,13 +4260,14 @@ static int test_bind_text16( ** N is the index of a wildcard in the prepared statement. This command ** binds a BLOB to the wildcard. The BLOB is BYTES bytes in size. */ -static int test_bind_blob( +static int SQLITE_TCLAPI test_bind_blob( void * clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[] ){ sqlite3_stmt *pStmt; + Tcl_Size len; int idx; int bytes; char *value; @@ -3475,7 +4276,7 @@ static int test_bind_blob( if( objc!=5 && objc!=6 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", - Tcl_GetStringFromObj(objv[0], 0), " STMT N DATA BYTES", 0); + Tcl_GetStringFromObj(objv[0], 0), " STMT N DATA BYTES", NULL); return TCL_ERROR; } @@ -3486,9 +4287,18 @@ static int test_bind_blob( if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; - value = Tcl_GetString(objv[3]); + + value = (char*)Tcl_GetByteArrayFromObj(objv[3], &len); if( Tcl_GetIntFromObj(interp, objv[4], &bytes) ) return TCL_ERROR; + if( bytes>len ){ + char zBuf[200]; + sqlite3_snprintf(sizeof(zBuf), zBuf, + "cannot use %d blob bytes, have %d", bytes, (int)len); + Tcl_AppendResult(interp, zBuf, (char*)0); + return TCL_ERROR; + } + rc = sqlite3_bind_blob(pStmt, idx, value, bytes, xDestructor); if( sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ) return TCL_ERROR; if( rc!=SQLITE_OK ){ @@ -3498,12 +4308,439 @@ static int test_bind_blob( return TCL_OK; } +/* +** Usage: sqlite3_bind_value_from_preupdate STMT N NEW|OLD IDX +** +** Test the sqlite3_bind_value interface using sqlite3_value objects +** obtained from either sqlite3_preupdate_new() (if arg[3]=="new") or +** sqlite3_preupdate_old() if (arg[3]=="old"). IDX is the index to +** pass to the sqlite3_preupdate_xxx() function. +*/ +static int SQLITE_TCLAPI test_bind_value_from_preupdate( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + sqlite3_stmt *pStmt; + int idx; + int bidx; +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK + const char *z3 = 0; + sqlite3 *db = 0; + sqlite3_value *pVal = 0; +#endif + + if( objc!=5 ){ + Tcl_WrongNumArgs(interp, 1, objv, "STMT N NEW|OLD IDX"); + return TCL_ERROR; + } + + if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; + if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; + if( Tcl_GetIntFromObj(interp, objv[4], &bidx) ) return TCL_ERROR; + +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK + z3 = Tcl_GetString(objv[3]); + db = sqlite3_db_handle(pStmt); + if( z3[0]=='n' ){ + sqlite3_preupdate_new(db, bidx, &pVal); + }else if( z3[0]=='o' ){ + sqlite3_preupdate_old(db, bidx, &pVal); + }else{ + Tcl_AppendResult(interp, "expected new or old, got: ", z3, (char*)0); + return TCL_ERROR; + } + sqlite3_bind_value(pStmt, idx, pVal); +#endif + + return TCL_OK; +} + +/* +** Usage: sqlite3_bind_value_from_select STMT N SELECT +** +** Test the sqlite3_bind_value interface. STMT is a prepared statement. +** N is the index of a wildcard in the prepared statement. +*/ +static int SQLITE_TCLAPI test_bind_value_from_select( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + sqlite3_stmt *pStmt; + sqlite3_stmt *pStmt2; + int idx; + const char *zSql = 0; + sqlite3 *db = 0; + int rc = SQLITE_OK; + + if( objc!=4 ){ + Tcl_WrongNumArgs(interp, 1, objv, "STMT N SELECT"); + return TCL_ERROR; + } + + if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; + if( Tcl_GetIntFromObj(interp, objv[2], &idx) ) return TCL_ERROR; + zSql = Tcl_GetString(objv[3]); + db = sqlite3_db_handle(pStmt); + + rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt2, 0); + if( rc!=SQLITE_OK ){ + Tcl_AppendResult(interp, "error in SQL: ", sqlite3_errmsg(db), (char*)0); + return TCL_ERROR; + } + if( sqlite3_step(pStmt2)==SQLITE_ROW ){ + sqlite3_value *pVal = sqlite3_column_value(pStmt2, 0); + sqlite3_bind_value(pStmt, idx, pVal); + } + rc = sqlite3_finalize(pStmt2); + if( rc!=SQLITE_OK ){ + Tcl_AppendResult(interp, + "error runnning SQL: ", sqlite3_errmsg(db), (char*)0 + ); + return TCL_ERROR; + } + + return TCL_OK; +} + +#ifdef _WIN32 + struct iovec { + void *iov_base; + size_t iov_len; + }; +#else +# include <sys/uio.h> +#endif + +#ifndef SQLITE_OMIT_VIRTUALTABLE + +/* +** These two are used by the -malloc option to sqlite3_carray_bind() +*/ +static void *testCarrayAlloc(int n){ + u8 *pRet = (u8*)sqlite3_malloc(n+16); + if( pRet ){ + pRet = &pRet[16]; + } + return (void*)pRet; +} +static void testCarrayFree(void *p){ + if( p ){ + u8 *p2 = (u8*)p; + sqlite3_free(&p2[-16]); + } +} + +static void delIntptr(void *p){ + ckfree(p); +} + +/* +** bind_carray_intptr STMT IPARAM INT0 INT1 INT2... +*/ +static int SQLITE_TCLAPI bind_carray_intptr( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + sqlite3_stmt *pStmt = 0; + int iVar = 0; + int *aInt = 0; + int nInt = 0; + int ii = 0; + int rc = SQLITE_OK; + + if( objc<3 ){ + Tcl_WrongNumArgs(interp, 1, objv, "STMT"); + return TCL_ERROR; + } + if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; + if( Tcl_GetIntFromObj(interp, objv[2], &iVar) ) return TCL_ERROR; + nInt = objc - 3; + + aInt = ckalloc((nInt+1) * sizeof(int)); + for(ii=0; ii<nInt; ii++){ + if( Tcl_GetIntFromObj(interp, objv[3+ii], &aInt[ii]) ){ + ckfree(aInt); + return TCL_ERROR; + } + } + + rc = sqlite3_bind_pointer(pStmt, iVar, (void*)aInt, "carray", delIntptr); + Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0); + + return TCL_OK; +} + +/* +** sqlite3_carray_bind [options...] STMT NAME VALUE ... +** +** Options: +** -malloc +** -transient +** -static +** -int32 +** -int64 +** -double +** -text +** -blob +** +** Each call clears static data. Called with no options does nothing +** but clear static data. +*/ +static int SQLITE_TCLAPI test_carray_bind( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + sqlite3_stmt *pStmt; + int eType = 0; /* CARRAY_INT32 */ + int mFlagsOverride = 0; + int nData = 0; + void *aData = 0; + int isTransient = 0; + int isStatic = 0; + int isMalloc = 0; /* True to use custom xDel function */ + int idx; + int i, j; + int rc = SQLITE_OK; + void (*xDel)(void*) = sqlite3_free; + static void *aStaticData = 0; + static int nStaticData = 0; + static int eStaticType = 0; + + if( aStaticData ){ + /* Always clear preexisting static data on every call */ + if( eStaticType==3 ){ + for(i=0; i<nStaticData; i++){ + sqlite3_free(((char**)aStaticData)[i]); + } + } + if( eStaticType==4 ){ + for(i=0; i<nStaticData; i++){ + sqlite3_free(((struct iovec*)aStaticData)[i].iov_base); + } + } + sqlite3_free(aStaticData); + aStaticData = 0; + nStaticData = 0; + eStaticType = 0; + } + if( objc==1 ) return TCL_OK; + + for(i=1; i<objc && Tcl_GetString(objv[i])[0]=='-'; i++){ + const char *z = Tcl_GetString(objv[i]); + if( strcmp(z, "-transient")==0 ){ + isTransient = 1; + xDel = SQLITE_TRANSIENT; + }else + if( strcmp(z, "-static")==0 ){ + isStatic = 1; + xDel = SQLITE_STATIC; + }else + if( strcmp(z, "-malloc")==0 ){ + isMalloc = 1; + xDel = testCarrayFree; + }else + if( strcmp(z, "-int32")==0 ){ + eType = 0; /* CARRAY_INT32 */ + }else + if( strcmp(z, "-int64")==0 ){ + eType = 1; /* CARRAY_INT64 */ + }else + if( strcmp(z, "-double")==0 ){ + eType = 2; /* CARRAY_DOUBLE */ + }else + if( strcmp(z, "-text")==0 ){ + eType = 3; /* CARRAY_TEXT */ + }else + if( strcmp(z, "-blob")==0 ){ + eType = 4; /* CARRAY_BLOB */ + }else + if( i<(objc-1) && strcmp(z, "-flags")==0 ){ + i++; + if( Tcl_GetIntFromObj(interp, objv[i], &mFlagsOverride) ){ + return TCL_ERROR; + } + }else + if( strcmp(z, "--")==0 ){ + break; + }else + { + Tcl_AppendResult(interp, "unknown option: ", z, (char*)0); + return TCL_ERROR; + } + } + if( eType==3 && !isStatic && !isTransient ){ + Tcl_AppendResult(interp, "text data must be either -static or -transient", + (char*)0); + return TCL_ERROR; + } + if( eType==4 && !isStatic && !isTransient ){ + Tcl_AppendResult(interp, "blob data must be either -static or -transient", + (char*)0); + return TCL_ERROR; + } + if( isStatic && isTransient ){ + Tcl_AppendResult(interp, "cannot be both -static and -transient", + (char*)0); + return TCL_ERROR; + } + if( objc-i < 2 ){ + Tcl_WrongNumArgs(interp, 1, objv, "[OPTIONS] STMT IDX VALUE ..."); + return TCL_ERROR; + } + if( getStmtPointer(interp, Tcl_GetString(objv[i]), &pStmt) ) return TCL_ERROR; + i++; + if( Tcl_GetIntFromObj(interp, objv[i], &idx) ) return TCL_ERROR; + i++; + nData = objc - i; + switch( eType + 5*(nData<=0) ){ + case 0: { /* INT32 */ + int *a = sqlite3_malloc( sizeof(int)*nData ); + if( a==0 ){ rc = SQLITE_NOMEM; goto carray_bind_done; } + for(j=0; j<nData; j++){ + int v; + if( Tcl_GetIntFromObj(interp, objv[i+j], &v) ){ + sqlite3_free(a); + return TCL_ERROR; + } + a[j] = v; + } + aData = a; + break; + } + case 1: { /* INT64 */ + sqlite3_int64 *a = sqlite3_malloc( sizeof(sqlite3_int64)*nData ); + if( a==0 ){ rc = SQLITE_NOMEM; goto carray_bind_done; } + for(j=0; j<nData; j++){ + Tcl_WideInt v; + if( Tcl_GetWideIntFromObj(interp, objv[i+j], &v) ){ + sqlite3_free(a); + return TCL_ERROR; + } + a[j] = v; + } + aData = a; + break; + } + case 2: { /* DOUBLE */ + double *a = sqlite3_malloc( sizeof(double)*nData ); + if( a==0 ){ rc = SQLITE_NOMEM; goto carray_bind_done; } + for(j=0; j<nData; j++){ + double v; + if( Tcl_GetDoubleFromObj(interp, objv[i+j], &v) ){ + sqlite3_free(a); + return TCL_ERROR; + } + a[j] = v; + } + aData = a; + break; + } + case 3: { /* TEXT */ + char **a = sqlite3_malloc( sizeof(char*)*nData ); + if( a==0 ){ + rc = SQLITE_NOMEM; + }else{ + memset(a, 0, sizeof(char*)*nData); + } + for(j=0; rc==SQLITE_OK && j<nData; j++){ + const char *v = Tcl_GetString(objv[i+j]); + if( v && strcmp(v, "NULL") ){ + a[j] = sqlite3_mprintf("%s", v); + if( a[j]==0 ) rc = SQLITE_NOMEM; + } + } + aData = a; + break; + } + case 4: { /* BLOB */ + struct iovec *a = sqlite3_malloc( sizeof(struct iovec)*nData ); + if( a==0 ){ + rc = SQLITE_NOMEM; + }else{ + memset(a, 0, sizeof(struct iovec)*nData); + } + for(j=0; rc==SQLITE_OK && j<nData; j++){ + Tcl_Size n = 0; + unsigned char *v = Tcl_GetByteArrayFromObj(objv[i+i], &n); + a[j].iov_len = (size_t)n; + a[j].iov_base = sqlite3_malloc64( n ); + if( a[j].iov_base==0 ){ + a[j].iov_len = 0; + rc = SQLITE_NOMEM; + }else{ + memcpy(a[j].iov_base, v, n); + } + } + aData = a; + break; + } + case 5: { /* nData==0 */ + aData = ""; + xDel = SQLITE_STATIC; + isTransient = 0; + isStatic = 0; + break; + } + } + + if( rc==SQLITE_OK ){ + if( isStatic ){ + aStaticData = aData; + nStaticData = nData; + eStaticType = eType; + } + else if( isMalloc ){ + int nByte = ((eType==0) ? sizeof(int) : sizeof(i64)) * nData; + void *aByte = testCarrayAlloc(nByte); + if( aByte==0 ){ + sqlite3_free(aData); + rc = SQLITE_NOMEM; + }else{ + memcpy(aByte, aData, nByte); + sqlite3_free(aData); + aData = aByte; + xDel = testCarrayFree; + } + assert( eType==0 || eType==1 || eType==2 ); + } + } + + if( rc==SQLITE_OK ){ + if( mFlagsOverride==0 ) mFlagsOverride = eType; + rc = sqlite3_carray_bind(pStmt, idx, aData, nData, mFlagsOverride, xDel); + } + if( isTransient ){ + if( eType==3 && aData ){ + for(i=0; i<nData; i++) sqlite3_free(((char**)aData)[i]); + } + if( eType==4 && aData ){ + for(i=0; i<nData; i++) sqlite3_free(((struct iovec*)aData)[i].iov_base); + } + sqlite3_free(aData); + } +carray_bind_done: + if( rc ){ + Tcl_AppendResult(interp, sqlite3_errstr(rc), (char*)0); + return TCL_ERROR; + } + return TCL_OK; +} +#endif /* SQLITE_OMIT_VIRTUALTABLE */ + /* ** Usage: sqlite3_bind_parameter_count STMT ** ** Return the number of wildcards in the given statement. */ -static int test_bind_parameter_count( +static int SQLITE_TCLAPI test_bind_parameter_count( void * clientData, Tcl_Interp *interp, int objc, @@ -3527,7 +4764,7 @@ static int test_bind_parameter_count( ** An empty string is returned if N is out of range or if the wildcard ** is nameless. */ -static int test_bind_parameter_name( +static int SQLITE_TCLAPI test_bind_parameter_name( void * clientData, Tcl_Interp *interp, int objc, @@ -3554,7 +4791,7 @@ static int test_bind_parameter_name( ** Return the index of the wildcard called NAME. Return 0 if there is ** no such wildcard. */ -static int test_bind_parameter_index( +static int SQLITE_TCLAPI test_bind_parameter_index( void * clientData, Tcl_Interp *interp, int objc, @@ -3579,7 +4816,7 @@ static int test_bind_parameter_index( ** Usage: sqlite3_clear_bindings STMT ** */ -static int test_clear_bindings( +static int SQLITE_TCLAPI test_clear_bindings( void * clientData, Tcl_Interp *interp, int objc, @@ -3599,7 +4836,7 @@ static int test_clear_bindings( /* ** Usage: sqlite3_sleep MILLISECONDS */ -static int test_sleep( +static int SQLITE_TCLAPI test_sleep( void * clientData, Tcl_Interp *interp, int objc, @@ -3624,7 +4861,7 @@ static int test_sleep( ** Return the string representation of the most recent sqlite3_* API ** error code. e.g. "SQLITE_ERROR". */ -static int test_ex_errcode( +static int SQLITE_TCLAPI test_ex_errcode( void * clientData, Tcl_Interp *interp, int objc, @@ -3635,12 +4872,12 @@ static int test_ex_errcode( if( objc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", - Tcl_GetString(objv[0]), " DB", 0); + Tcl_GetString(objv[0]), " DB", NULL); return TCL_ERROR; } if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; rc = sqlite3_extended_errcode(db); - Tcl_AppendResult(interp, (char *)t1ErrorName(rc), 0); + Tcl_AppendResult(interp, (char *)t1ErrorName(rc), NULL); return TCL_OK; } @@ -3651,7 +4888,7 @@ static int test_ex_errcode( ** Return the string representation of the most recent sqlite3_* API ** error code. e.g. "SQLITE_ERROR". */ -static int test_errcode( +static int SQLITE_TCLAPI test_errcode( void * clientData, Tcl_Interp *interp, int objc, @@ -3662,12 +4899,12 @@ static int test_errcode( if( objc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", - Tcl_GetString(objv[0]), " DB", 0); + Tcl_GetString(objv[0]), " DB", NULL); return TCL_ERROR; } if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; rc = sqlite3_errcode(db); - Tcl_AppendResult(interp, (char *)t1ErrorName(rc), 0); + Tcl_AppendResult(interp, (char *)t1ErrorName(rc), NULL); return TCL_OK; } @@ -3677,7 +4914,7 @@ static int test_errcode( ** Returns the UTF-8 representation of the error message string for the ** most recent sqlite3_* API call. */ -static int test_errmsg( +static int SQLITE_TCLAPI test_errmsg( void * clientData, Tcl_Interp *interp, int objc, @@ -3688,7 +4925,7 @@ static int test_errmsg( if( objc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", - Tcl_GetString(objv[0]), " DB", 0); + Tcl_GetString(objv[0]), " DB", NULL); return TCL_ERROR; } if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; @@ -3698,6 +4935,34 @@ static int test_errmsg( return TCL_OK; } + +/* +** Usage: sqlite3_error_offset DB +** +** Return the byte offset into the input UTF8 SQL for the most recent +** error, or -1 of the error does not refer to a specific token. +*/ +static int SQLITE_TCLAPI test_error_offset( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + sqlite3 *db; + int iByteOffset; + + if( objc!=2 ){ + Tcl_AppendResult(interp, "wrong # args: should be \"", + Tcl_GetString(objv[0]), " DB", NULL); + return TCL_ERROR; + } + if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; + + iByteOffset = sqlite3_error_offset(db); + Tcl_SetObjResult(interp, Tcl_NewIntObj(iByteOffset)); + return TCL_OK; +} + /* ** Usage: test_errmsg16 DB ** @@ -3706,7 +4971,7 @@ static int test_errmsg( ** level, and it includes the 0x00 0x00 terminator bytes at the end of the ** UTF-16 string. */ -static int test_errmsg16( +static int SQLITE_TCLAPI test_errmsg16( void * clientData, Tcl_Interp *interp, int objc, @@ -3720,30 +4985,118 @@ static int test_errmsg16( if( objc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", - Tcl_GetString(objv[0]), " DB", 0); + Tcl_GetString(objv[0]), " DB", NULL); + return TCL_ERROR; + } + if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; + + zErr = sqlite3_errmsg16(db); + if( zErr ){ + z = zErr; + for(bytes=0; z[bytes] || z[bytes+1]; bytes+=2){} + } + Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(zErr, bytes)); +#endif /* SQLITE_OMIT_UTF16 */ + return TCL_OK; +} + +/* +** Usage: sqlite3_set_errmsg DB ERRCODE ERRMSG +*/ +static int SQLITE_TCLAPI test_set_errmsg( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + const char *zDb = 0; + const char *zErr = 0; + int iErr = 0; + sqlite3 *db = 0; + int rc; + + if( objc!=4 ){ + Tcl_WrongNumArgs(interp, 1, objv, "DB ERRCODE ERRMSG"); + return TCL_ERROR; + } + zDb = Tcl_GetString(objv[1]); + if( zDb[0] ){ + if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; + } + if( Tcl_GetIntFromObj(interp, objv[2], &iErr) ) return TCL_ERROR; + zErr = Tcl_GetString(objv[3]); + + rc = sqlite3_set_errmsg(db, iErr, (zErr[0] ? zErr : 0)); + Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0); + return TCL_OK; +} + +/* +** Usage: sqlite3_prepare DB sql bytes ?tailvar? +** +** Compile up to <bytes> bytes of the supplied SQL string <sql> using +** database handle <DB>. The parameter <tailval> is the name of a global +** variable that is set to the unused portion of <sql> (if any). A +** STMT handle is returned. +*/ +static int SQLITE_TCLAPI test_prepare( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + sqlite3 *db; + const char *zSql; + int bytes; + const char *zTail = 0; + sqlite3_stmt *pStmt = 0; + char zBuf[50]; + int rc; + + if( objc!=5 && objc!=4 ){ + Tcl_AppendResult(interp, "wrong # args: should be \"", + Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", NULL); + return TCL_ERROR; + } + if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; + zSql = Tcl_GetString(objv[2]); + if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR; + + rc = sqlite3_prepare(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0); + Tcl_ResetResult(interp); + if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; + if( zTail && objc>=5 ){ + if( bytes>=0 ){ + bytes = bytes - (int)(zTail-zSql); + } + if( (int)strlen(zTail)<bytes ){ + bytes = (int)strlen(zTail); + } + Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0); + } + if( rc!=SQLITE_OK ){ + assert( pStmt==0 ); + sqlite3_snprintf(sizeof(zBuf), zBuf, "(%d) ", rc); + Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), NULL); return TCL_ERROR; } - if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; - zErr = sqlite3_errmsg16(db); - if( zErr ){ - z = zErr; - for(bytes=0; z[bytes] || z[bytes+1]; bytes+=2){} + if( pStmt ){ + if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; + Tcl_AppendResult(interp, zBuf, NULL); } - Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(zErr, bytes)); -#endif /* SQLITE_OMIT_UTF16 */ return TCL_OK; } /* -** Usage: sqlite3_prepare DB sql bytes ?tailvar? +** Usage: sqlite3_prepare_v2 DB sql bytes ?tailvar? ** ** Compile up to <bytes> bytes of the supplied SQL string <sql> using ** database handle <DB>. The parameter <tailval> is the name of a global ** variable that is set to the unused portion of <sql> (if any). A ** STMT handle is returned. */ -static int test_prepare( +static int SQLITE_TCLAPI test_prepare_v2( void * clientData, Tcl_Interp *interp, int objc, @@ -3751,56 +5104,73 @@ static int test_prepare( ){ sqlite3 *db; const char *zSql; + char *zCopy = 0; /* malloc() copy of zSql */ int bytes; const char *zTail = 0; + const char **pzTail; sqlite3_stmt *pStmt = 0; char zBuf[50]; int rc; if( objc!=5 && objc!=4 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", - Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", 0); + Tcl_GetString(objv[0]), " DB sql bytes tailvar", NULL); return TCL_ERROR; } if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; zSql = Tcl_GetString(objv[2]); if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR; - rc = sqlite3_prepare(db, zSql, bytes, &pStmt, objc>=5 ? &zTail : 0); + /* Instead of using zSql directly, make a copy into a buffer obtained + ** directly from malloc(). The idea is to make it easier for valgrind + ** to spot buffer overreads. */ + if( bytes>=0 ){ + zCopy = malloc(bytes); + memcpy(zCopy, zSql, bytes); + }else{ + int n = (int)strlen(zSql) + 1; + zCopy = malloc(n); + memcpy(zCopy, zSql, n); + } + pzTail = objc>=5 ? &zTail : 0; + rc = sqlite3_prepare_v2(db, zCopy, bytes, &pStmt, pzTail); + if( objc>=5 ){ + zTail = &zSql[(zTail - zCopy)]; + } + free(zCopy); + + assert(rc==SQLITE_OK || pStmt==0); Tcl_ResetResult(interp); if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; - if( zTail && objc>=5 ){ + if( rc==SQLITE_OK && objc>=5 && zTail ){ if( bytes>=0 ){ bytes = bytes - (int)(zTail-zSql); } - if( (int)strlen(zTail)<bytes ){ - bytes = (int)strlen(zTail); - } Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0); } if( rc!=SQLITE_OK ){ assert( pStmt==0 ); sqlite3_snprintf(sizeof(zBuf), zBuf, "(%d) ", rc); - Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0); + Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), NULL); return TCL_ERROR; } if( pStmt ){ if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; - Tcl_AppendResult(interp, zBuf, 0); + Tcl_AppendResult(interp, zBuf, NULL); } return TCL_OK; } /* -** Usage: sqlite3_prepare_v2 DB sql bytes ?tailvar? +** Usage: sqlite3_prepare_v3 DB sql bytes flags ?tailvar? ** ** Compile up to <bytes> bytes of the supplied SQL string <sql> using -** database handle <DB>. The parameter <tailval> is the name of a global -** variable that is set to the unused portion of <sql> (if any). A -** STMT handle is returned. +** database handle <DB> and flags <flags>. The parameter <tailval> is +** the name of a global variable that is set to the unused portion of +** <sql> (if any). A STMT handle is returned. */ -static int test_prepare_v2( +static int SQLITE_TCLAPI test_prepare_v3( void * clientData, Tcl_Interp *interp, int objc, @@ -3809,20 +5179,22 @@ static int test_prepare_v2( sqlite3 *db; const char *zSql; char *zCopy = 0; /* malloc() copy of zSql */ - int bytes; + int bytes, flags; const char *zTail = 0; + const char **pzTail; sqlite3_stmt *pStmt = 0; char zBuf[50]; int rc; - if( objc!=5 && objc!=4 ){ + if( objc!=6 && objc!=5 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", - Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0); + Tcl_GetString(objv[0]), " DB sql bytes flags tailvar", NULL); return TCL_ERROR; } if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; zSql = Tcl_GetString(objv[2]); if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR; + if( Tcl_GetIntFromObj(interp, objv[4], &flags) ) return TCL_ERROR; /* Instead of using zSql directly, make a copy into a buffer obtained ** directly from malloc(). The idea is to make it easier for valgrind @@ -3835,29 +5207,30 @@ static int test_prepare_v2( zCopy = malloc(n); memcpy(zCopy, zSql, n); } - rc = sqlite3_prepare_v2(db, zCopy, bytes, &pStmt, objc>=5 ? &zTail : 0); - free(zCopy); + pzTail = objc>=6 ? &zTail : 0; + rc = sqlite3_prepare_v3(db, zCopy, bytes, (unsigned int)flags,&pStmt,pzTail); zTail = &zSql[(zTail - zCopy)]; + free(zCopy); assert(rc==SQLITE_OK || pStmt==0); Tcl_ResetResult(interp); if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR; - if( rc==SQLITE_OK && zTail && objc>=5 ){ + if( rc==SQLITE_OK && zTail && objc>=6 ){ if( bytes>=0 ){ bytes = bytes - (int)(zTail-zSql); } - Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0); + Tcl_ObjSetVar2(interp, objv[5], 0, Tcl_NewStringObj(zTail, bytes), 0); } if( rc!=SQLITE_OK ){ assert( pStmt==0 ); sqlite3_snprintf(sizeof(zBuf), zBuf, "(%d) ", rc); - Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0); + Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), NULL); return TCL_ERROR; } if( pStmt ){ if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; - Tcl_AppendResult(interp, zBuf, 0); + Tcl_AppendResult(interp, zBuf, NULL); } return TCL_OK; } @@ -3868,7 +5241,7 @@ static int test_prepare_v2( ** Generate a prepared statement for a zero-byte string as a test ** for ticket #3134. The string should be preceded by a zero byte. */ -static int test_prepare_tkt3134( +static int SQLITE_TCLAPI test_prepare_tkt3134( void * clientData, Tcl_Interp *interp, int objc, @@ -3882,7 +5255,7 @@ static int test_prepare_tkt3134( if( objc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", - Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0); + Tcl_GetString(objv[0]), " DB sql bytes tailvar", NULL); return TCL_ERROR; } if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; @@ -3892,13 +5265,13 @@ static int test_prepare_tkt3134( if( rc!=SQLITE_OK ){ assert( pStmt==0 ); sqlite3_snprintf(sizeof(zBuf), zBuf, "(%d) ", rc); - Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0); + Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), NULL); return TCL_ERROR; } if( pStmt ){ if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; - Tcl_AppendResult(interp, zBuf, 0); + Tcl_AppendResult(interp, zBuf, NULL); } return TCL_OK; } @@ -3911,7 +5284,7 @@ static int test_prepare_tkt3134( ** variable that is set to the unused portion of <sql> (if any). A ** STMT handle is returned. */ -static int test_prepare16( +static int SQLITE_TCLAPI test_prepare16( void * clientData, Tcl_Interp *interp, int objc, @@ -3926,11 +5299,11 @@ static int test_prepare16( char zBuf[50]; int rc; int bytes; /* The integer specified as arg 3 */ - int objlen; /* The byte-array length of arg 2 */ + Tcl_Size objlen; /* The byte-array length of arg 2 */ if( objc!=5 && objc!=4 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", - Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", 0); + Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", NULL); return TCL_ERROR; } if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; @@ -3958,7 +5331,7 @@ static int test_prepare16( if( pStmt ){ if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; } - Tcl_AppendResult(interp, zBuf, 0); + Tcl_AppendResult(interp, zBuf, NULL); #endif /* SQLITE_OMIT_UTF16 */ return TCL_OK; } @@ -3971,7 +5344,7 @@ static int test_prepare16( ** variable that is set to the unused portion of <sql> (if any). A ** STMT handle is returned. */ -static int test_prepare16_v2( +static int SQLITE_TCLAPI test_prepare16_v2( void * clientData, Tcl_Interp *interp, int objc, @@ -3986,11 +5359,11 @@ static int test_prepare16_v2( char zBuf[50]; int rc; int bytes; /* The integer specified as arg 3 */ - int objlen; /* The byte-array length of arg 2 */ + Tcl_Size objlen; /* The byte-array length of arg 2 */ if( objc!=5 && objc!=4 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", - Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", 0); + Tcl_GetString(objv[0]), " DB sql bytes ?tailvar?", NULL); return TCL_ERROR; } if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; @@ -4018,7 +5391,7 @@ static int test_prepare16_v2( if( pStmt ){ if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; } - Tcl_AppendResult(interp, zBuf, 0); + Tcl_AppendResult(interp, zBuf, NULL); #endif /* SQLITE_OMIT_UTF16 */ return TCL_OK; } @@ -4026,7 +5399,7 @@ static int test_prepare16_v2( /* ** Usage: sqlite3_open filename ?options-list? */ -static int test_open( +static int SQLITE_TCLAPI test_open( void * clientData, Tcl_Interp *interp, int objc, @@ -4038,7 +5411,7 @@ static int test_open( if( objc!=3 && objc!=2 && objc!=1 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", - Tcl_GetString(objv[0]), " filename options-list", 0); + Tcl_GetString(objv[0]), " filename options-list", NULL); return TCL_ERROR; } @@ -4046,14 +5419,14 @@ static int test_open( sqlite3_open(zFilename, &db); if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR; - Tcl_AppendResult(interp, zBuf, 0); + Tcl_AppendResult(interp, zBuf, NULL); return TCL_OK; } /* ** Usage: sqlite3_open_v2 FILENAME FLAGS VFS */ -static int test_open_v2( +static int SQLITE_TCLAPI test_open_v2( void * clientData, Tcl_Interp *interp, int objc, @@ -4066,9 +5439,9 @@ static int test_open_v2( int rc; char zBuf[100]; - int nFlag; + Tcl_Size nFlag; Tcl_Obj **apFlag; - int i; + Tcl_Size i; if( objc!=4 ){ Tcl_WrongNumArgs(interp, 1, objv, "FILENAME FLAGS VFS"); @@ -4098,13 +5471,14 @@ static int test_open_v2( { "SQLITE_OPEN_MAIN_JOURNAL", SQLITE_OPEN_MAIN_JOURNAL }, { "SQLITE_OPEN_TEMP_JOURNAL", SQLITE_OPEN_TEMP_JOURNAL }, { "SQLITE_OPEN_SUBJOURNAL", SQLITE_OPEN_SUBJOURNAL }, - { "SQLITE_OPEN_MASTER_JOURNAL", SQLITE_OPEN_MASTER_JOURNAL }, + { "SQLITE_OPEN_SUPER_JOURNAL", SQLITE_OPEN_SUPER_JOURNAL }, { "SQLITE_OPEN_NOMUTEX", SQLITE_OPEN_NOMUTEX }, { "SQLITE_OPEN_FULLMUTEX", SQLITE_OPEN_FULLMUTEX }, { "SQLITE_OPEN_SHAREDCACHE", SQLITE_OPEN_SHAREDCACHE }, { "SQLITE_OPEN_PRIVATECACHE", SQLITE_OPEN_PRIVATECACHE }, { "SQLITE_OPEN_WAL", SQLITE_OPEN_WAL }, { "SQLITE_OPEN_URI", SQLITE_OPEN_URI }, + { "SQLITE_OPEN_EXRESCODE", SQLITE_OPEN_EXRESCODE }, { 0, 0 } }; rc = Tcl_GetIndexFromObjStruct(interp, apFlag[i], aFlag, sizeof(aFlag[0]), @@ -4116,14 +5490,14 @@ static int test_open_v2( rc = sqlite3_open_v2(zFilename, &db, flags, zVfs); if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR; - Tcl_AppendResult(interp, zBuf, 0); + Tcl_AppendResult(interp, zBuf, NULL); return TCL_OK; } /* ** Usage: sqlite3_open16 filename options */ -static int test_open16( +static int SQLITE_TCLAPI test_open16( void * clientData, Tcl_Interp *interp, int objc, @@ -4136,7 +5510,7 @@ static int test_open16( if( objc!=3 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", - Tcl_GetString(objv[0]), " filename options-list", 0); + Tcl_GetString(objv[0]), " filename options-list", NULL); return TCL_ERROR; } @@ -4144,7 +5518,7 @@ static int test_open16( sqlite3_open16(zFilename, &db); if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR; - Tcl_AppendResult(interp, zBuf, 0); + Tcl_AppendResult(interp, zBuf, NULL); #endif /* SQLITE_OMIT_UTF16 */ return TCL_OK; } @@ -4155,7 +5529,7 @@ static int test_open16( ** Return 1 if the supplied argument is a complete SQL statement, or zero ** otherwise. */ -static int test_complete16( +static int SQLITE_TCLAPI test_complete16( void * clientData, Tcl_Interp *interp, int objc, @@ -4175,12 +5549,41 @@ static int test_complete16( return TCL_OK; } +/* +** Usage: sqlite3_normalize SQL +** +** Return the normalized value for an SQL statement. +*/ +static int SQLITE_TCLAPI test_normalize( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + char *zSql; + char *zNorm; + extern char *sqlite3_normalize(const char*); + + if( objc!=2 ){ + Tcl_WrongNumArgs(interp, 1, objv, "SQL"); + return TCL_ERROR; + } + + zSql = (char*)Tcl_GetString(objv[1]); + zNorm = sqlite3_normalize(zSql); + if( zNorm ){ + Tcl_SetObjResult(interp, Tcl_NewStringObj(zNorm, -1)); + sqlite3_free(zNorm); + } + return TCL_OK; +} + /* ** Usage: sqlite3_step STMT ** ** Advance the statement to the next row. */ -static int test_step( +static int SQLITE_TCLAPI test_step( void * clientData, Tcl_Interp *interp, int objc, @@ -4191,7 +5594,7 @@ static int test_step( if( objc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", - Tcl_GetString(objv[0]), " STMT", 0); + Tcl_GetString(objv[0]), " STMT", NULL); return TCL_ERROR; } @@ -4203,7 +5606,7 @@ static int test_step( return TCL_OK; } -static int test_sql( +static int SQLITE_TCLAPI test_sql( void * clientData, Tcl_Interp *interp, int objc, @@ -4220,13 +5623,52 @@ static int test_sql( Tcl_SetResult(interp, (char *)sqlite3_sql(pStmt), TCL_VOLATILE); return TCL_OK; } +static int SQLITE_TCLAPI test_ex_sql( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + sqlite3_stmt *pStmt; + char *z; + + if( objc!=2 ){ + Tcl_WrongNumArgs(interp, 1, objv, "STMT"); + return TCL_ERROR; + } + + if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; + z = sqlite3_expanded_sql(pStmt); + Tcl_SetResult(interp, z, TCL_VOLATILE); + sqlite3_free(z); + return TCL_OK; +} +#ifdef SQLITE_ENABLE_NORMALIZE +static int SQLITE_TCLAPI test_norm_sql( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + sqlite3_stmt *pStmt; + + if( objc!=2 ){ + Tcl_WrongNumArgs(interp, 1, objv, "STMT"); + return TCL_ERROR; + } + + if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; + Tcl_SetResult(interp, (char *)sqlite3_normalized_sql(pStmt), TCL_VOLATILE); + return TCL_OK; +} +#endif /* SQLITE_ENABLE_NORMALIZE */ /* ** Usage: sqlite3_column_count STMT ** ** Return the number of columns returned by the sql statement STMT. */ -static int test_column_count( +static int SQLITE_TCLAPI test_column_count( void * clientData, Tcl_Interp *interp, int objc, @@ -4236,7 +5678,7 @@ static int test_column_count( if( objc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", - Tcl_GetString(objv[0]), " STMT column", 0); + Tcl_GetString(objv[0]), " STMT column", NULL); return TCL_ERROR; } @@ -4251,7 +5693,7 @@ static int test_column_count( ** ** Return the type of the data in column 'column' of the current row. */ -static int test_column_type( +static int SQLITE_TCLAPI test_column_type( void * clientData, Tcl_Interp *interp, int objc, @@ -4263,7 +5705,7 @@ static int test_column_type( if( objc!=3 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", - Tcl_GetString(objv[0]), " STMT column", 0); + Tcl_GetString(objv[0]), " STMT column", NULL); return TCL_ERROR; } @@ -4300,7 +5742,7 @@ static int test_column_type( ** Return the data in column 'column' of the current row cast as an ** wide (64-bit) integer. */ -static int test_column_int64( +static int SQLITE_TCLAPI test_column_int64( void * clientData, Tcl_Interp *interp, int objc, @@ -4312,7 +5754,7 @@ static int test_column_int64( if( objc!=3 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", - Tcl_GetString(objv[0]), " STMT column", 0); + Tcl_GetString(objv[0]), " STMT column", NULL); return TCL_ERROR; } @@ -4327,7 +5769,7 @@ static int test_column_int64( /* ** Usage: sqlite3_column_blob STMT column */ -static int test_column_blob( +static int SQLITE_TCLAPI test_column_blob( void * clientData, Tcl_Interp *interp, int objc, @@ -4341,7 +5783,7 @@ static int test_column_blob( if( objc!=3 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", - Tcl_GetString(objv[0]), " STMT column", 0); + Tcl_GetString(objv[0]), " STMT column", NULL); return TCL_ERROR; } @@ -4359,7 +5801,7 @@ static int test_column_blob( ** ** Return the data in column 'column' of the current row cast as a double. */ -static int test_column_double( +static int SQLITE_TCLAPI test_column_double( void * clientData, Tcl_Interp *interp, int objc, @@ -4371,7 +5813,7 @@ static int test_column_double( if( objc!=3 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", - Tcl_GetString(objv[0]), " STMT column", 0); + Tcl_GetString(objv[0]), " STMT column", NULL); return TCL_ERROR; } @@ -4388,7 +5830,7 @@ static int test_column_double( ** ** Return the number of columns returned by the sql statement STMT. */ -static int test_data_count( +static int SQLITE_TCLAPI test_data_count( void * clientData, Tcl_Interp *interp, int objc, @@ -4398,7 +5840,7 @@ static int test_data_count( if( objc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", - Tcl_GetString(objv[0]), " STMT column", 0); + Tcl_GetString(objv[0]), " STMT column", NULL); return TCL_ERROR; } @@ -4415,7 +5857,7 @@ static int test_data_count( ** ** Usage: sqlite3_column_name STMT column */ -static int test_stmt_utf8( +static int SQLITE_TCLAPI test_stmt_utf8( void * clientData, /* Pointer to SQLite API function to be invoke */ Tcl_Interp *interp, int objc, @@ -4424,25 +5866,31 @@ static int test_stmt_utf8( sqlite3_stmt *pStmt; int col; const char *(*xFunc)(sqlite3_stmt*, int); + const unsigned char *(*xFuncU)(sqlite3_stmt*, int); const char *zRet; xFunc = (const char *(*)(sqlite3_stmt*, int))clientData; + xFuncU = (const unsigned char*(*)(sqlite3_stmt*,int))xFunc; if( objc!=3 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", - Tcl_GetString(objv[0]), " STMT column", 0); + Tcl_GetString(objv[0]), " STMT column", NULL); return TCL_ERROR; } if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR; if( Tcl_GetIntFromObj(interp, objv[2], &col) ) return TCL_ERROR; - zRet = xFunc(pStmt, col); + if( xFunc==sqlite3_column_name || xFunc==sqlite3_column_decltype ){ + zRet = xFunc(pStmt, col); + }else{ + zRet = (const char*)xFuncU(pStmt, col); + } if( zRet ){ Tcl_SetResult(interp, (char *)zRet, 0); } return TCL_OK; } -static int test_global_recover( +static int SQLITE_TCLAPI test_global_recover( void * clientData, Tcl_Interp *interp, int objc, @@ -4467,7 +5915,7 @@ static int test_global_recover( ** ** Usage: sqlite3_column_name STMT column */ -static int test_stmt_utf16( +static int SQLITE_TCLAPI test_stmt_utf16( void * clientData, /* Pointer to SQLite API function to be invoked */ Tcl_Interp *interp, int objc, @@ -4483,7 +5931,7 @@ static int test_stmt_utf16( xFunc = (const void *(*)(sqlite3_stmt*, int))clientData; if( objc!=3 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", - Tcl_GetString(objv[0]), " STMT column", 0); + Tcl_GetString(objv[0]), " STMT column", NULL); return TCL_ERROR; } @@ -4511,7 +5959,7 @@ static int test_stmt_utf16( ** Usage: sqlite3_column_bytes16 STMT column ** */ -static int test_stmt_int( +static int SQLITE_TCLAPI test_stmt_int( void * clientData, /* Pointer to SQLite API function to be invoked */ Tcl_Interp *interp, int objc, @@ -4524,7 +5972,7 @@ static int test_stmt_int( xFunc = (int (*)(sqlite3_stmt*, int))clientData; if( objc!=3 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", - Tcl_GetString(objv[0]), " STMT column", 0); + Tcl_GetString(objv[0]), " STMT column", NULL); return TCL_ERROR; } @@ -4535,44 +5983,12 @@ static int test_stmt_int( return TCL_OK; } -/* -** Usage: sqlite_set_magic DB MAGIC-NUMBER -** -** Set the db->magic value. This is used to test error recovery logic. -*/ -static int sqlite_set_magic( - void * clientData, - Tcl_Interp *interp, - int argc, - char **argv -){ - sqlite3 *db; - if( argc!=3 ){ - Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " DB MAGIC", 0); - return TCL_ERROR; - } - if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; - if( strcmp(argv[2], "SQLITE_MAGIC_OPEN")==0 ){ - db->magic = SQLITE_MAGIC_OPEN; - }else if( strcmp(argv[2], "SQLITE_MAGIC_CLOSED")==0 ){ - db->magic = SQLITE_MAGIC_CLOSED; - }else if( strcmp(argv[2], "SQLITE_MAGIC_BUSY")==0 ){ - db->magic = SQLITE_MAGIC_BUSY; - }else if( strcmp(argv[2], "SQLITE_MAGIC_ERROR")==0 ){ - db->magic = SQLITE_MAGIC_ERROR; - }else if( Tcl_GetInt(interp, argv[2], (int*)&db->magic) ){ - return TCL_ERROR; - } - return TCL_OK; -} - /* ** Usage: sqlite3_interrupt DB ** ** Trigger an interrupt on DB */ -static int test_interrupt( +static int SQLITE_TCLAPI test_interrupt( void * clientData, Tcl_Interp *interp, int argc, @@ -4580,7 +5996,7 @@ static int test_interrupt( ){ sqlite3 *db; if( argc!=2 ){ - Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB", 0); + Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB", NULL); return TCL_ERROR; } if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; @@ -4588,49 +6004,26 @@ static int test_interrupt( return TCL_OK; } -static u8 *sqlite3_stack_baseline = 0; - -/* -** Fill the stack with a known bitpattern. -*/ -static void prepStack(void){ - int i; - u32 bigBuf[65536]; - for(i=0; i<sizeof(bigBuf)/sizeof(bigBuf[0]); i++) bigBuf[i] = 0xdeadbeef; - sqlite3_stack_baseline = (u8*)&bigBuf[65536]; -} - -/* -** Get the current stack depth. Used for debugging only. -*/ -u64 sqlite3StackDepth(void){ - u8 x; - return (u64)(sqlite3_stack_baseline - &x); -} - /* -** Usage: sqlite3_stack_used DB SQL +** Usage: sqlite3_is_interrupted DB ** -** Try to measure the amount of stack space used by a call to sqlite3_exec +** return true if an interrupt is current in effect on DB */ -static int test_stack_used( +static int SQLITE_TCLAPI test_is_interrupted( void * clientData, Tcl_Interp *interp, int argc, char **argv ){ sqlite3 *db; - int i; - if( argc!=3 ){ - Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " DB SQL", 0); + int rc; + if( argc!=2 ){ + Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " DB", NULL); return TCL_ERROR; } if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; - prepStack(); - (void)sqlite3_exec(db, argv[2], 0, 0, 0); - for(i=65535; i>=0 && ((u32*)sqlite3_stack_baseline)[-i]==0xdeadbeef; i--){} - Tcl_SetObjResult(interp, Tcl_NewIntObj(i*4)); + rc = sqlite3_is_interrupted(db); + Tcl_AppendResult(interp, rc ? "1" : "0", (void*)0); return TCL_OK; } @@ -4641,7 +6034,7 @@ static int test_stack_used( ** is assumed that the user function was created as UTF8, any number of ** arguments (the way the TCL interface does it). */ -static int delete_function( +static int SQLITE_TCLAPI delete_function( void * clientData, Tcl_Interp *interp, int argc, @@ -4667,7 +6060,7 @@ static int delete_function( ** DB. It is assumed that the collation sequence was created as UTF8 (the ** way the TCL interface does it). */ -static int delete_collation( +static int SQLITE_TCLAPI delete_collation( void * clientData, Tcl_Interp *interp, int argc, @@ -4692,7 +6085,7 @@ static int delete_collation( ** Return true if the database DB is currently in auto-commit mode. ** Return false if not. */ -static int get_autocommit( +static int SQLITE_TCLAPI get_autocommit( void * clientData, Tcl_Interp *interp, int argc, @@ -4707,7 +6100,7 @@ static int get_autocommit( } if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", sqlite3_get_autocommit(db)); - Tcl_AppendResult(interp, zBuf, 0); + Tcl_AppendResult(interp, zBuf, NULL); return TCL_OK; } @@ -4718,7 +6111,7 @@ static int get_autocommit( ** method of the TCL interface. But we need a way to test the case ** where it returns SQLITE_MISUSE. */ -static int test_busy_timeout( +static int SQLITE_TCLAPI test_busy_timeout( void * clientData, Tcl_Interp *interp, int argc, @@ -4728,13 +6121,49 @@ static int test_busy_timeout( sqlite3 *db; if( argc!=3 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " DB", 0); + " DB", NULL); return TCL_ERROR; } if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; if( Tcl_GetInt(interp, argv[2], &ms) ) return TCL_ERROR; rc = sqlite3_busy_timeout(db, ms); - Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); + Tcl_AppendResult(interp, sqlite3ErrName(rc), NULL); + return TCL_OK; +} + +/* +** Usage: sqlite3_setlk_timeout ?-blockonconnect? DB MS +** +** Set the setlk timeout. +*/ +static int SQLITE_TCLAPI test_setlk_timeout( + void * clientData, + Tcl_Interp *interp, + int argc, + char **argv +){ + int rc, ms; + sqlite3 *db; + int bBlockOnConnect = 0; + + if( argc==4 ){ + const char *zArg = argv[1]; + const size_t nArg = strlen(zArg); + if( nArg>=2 && nArg<=15 && memcmp(zArg, "-blockonconnect", nArg)==0 ){ + bBlockOnConnect = 1; + } + } + if( argc!=(3+bBlockOnConnect) ){ + Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], + " ?-blockonconnect? DB MS", NULL); + return TCL_ERROR; + } + if( getDbPointer(interp, argv[argc-2], &db) ) return TCL_ERROR; + if( Tcl_GetInt(interp, argv[argc-1], &ms) ) return TCL_ERROR; + rc = sqlite3_setlk_timeout( + db, ms, (bBlockOnConnect ? SQLITE_SETLK_BLOCK_ON_CONNECT : 0) + ); + Tcl_AppendResult(interp, sqlite3ErrName(rc), NULL); return TCL_OK; } @@ -4744,7 +6173,7 @@ static int test_busy_timeout( ** Return the name of the internal representation for the ** value of the given variable. */ -static int tcl_variable_type( +static int SQLITE_TCLAPI tcl_variable_type( void * clientData, Tcl_Interp *interp, int objc, @@ -4763,6 +6192,145 @@ static int tcl_variable_type( return TCL_OK; } +#include <ctype.h> + +/* +** Usage: fpnum_compare STRING1 STRING2 +** +** Compare two strings. Return true if the strings are the same and +** false if they differ. +** +** For this comparison, the strings are analyzed as a sequenced of +** whitespace separated tokens. The whitespace is ignored. Only the +** tokens are compared. Comparison rules: +** +** A. Tokens that are not floating-point numbers must match exactly. +** +** B. Floating point number must have exactly the same digits before +** the decimal point. +** +** C. Digits must match after the decimal point up to 15 digits, +** taking rounding into consideration. +** +** D. An exponent on a floating point of the form "e+NN" will +** match "e+N" if NN==N. Likewise for the negative exponent. +** +** This routine is used for comparing results that might involve floating +** point values. Tcl9.0 and Tcl8.6 differ in the number of significant +** digits that they show, so there is no way to write a portable test result +** without this routine. +** +** This routine is only called after [string compare] fails, which is seldom, +** so performance is not a pressing concern. Better to get the correct answer +** slowly. +*/ +static int SQLITE_TCLAPI fpnum_compare( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + const unsigned char *zA; + const unsigned char *zB; + int i, j; + int nDigit; + + if( objc!=3 ){ + Tcl_WrongNumArgs(interp, 1, objv, "STRING1 STRING2"); + return TCL_ERROR; + } + zA = (const unsigned char*)Tcl_GetString(objv[1]); + zB = (const unsigned char*)Tcl_GetString(objv[2]); + i = j = 0; + while( 1 ){ + /* Skip whitespace before and after tokens */ + while( isspace(zA[i]) ){ i++; } + while( isspace(zB[j]) ){ j++; } + + if( zA[i]!=zB[j] ) break; /* First character must match */ + if( zA[i]=='-' && isdigit(zA[i+1]) ){ i++; j++; } /* Skip initial '-' */ + if( !isdigit(zA[i]) ){ + /* Not a number. Must match exactly */ + while( !isspace(zA[i]) && zA[i] && zA[i]==zB[j] ){ i++; j++; } + if( zA[i]!=zB[j] ) break; + if( isspace(zA[i]) ) continue; + break; + } + + /* At this point we know we are dealing with a number zA[i] and zB[j] + ** are both digits (leading "-" have been skipped). See if they are + ** the same number. Start by matching digits before the decimal + ** point, which must all be the same. */ + nDigit = 0; + while( zA[i]==zB[j] && isdigit(zA[i]) ){ i++; j++; nDigit++; } + if( zA[i]!=zB[j] ) break; + if( zA[i]==0 ) break; + if( zA[i]=='.' && zB[j]=='.' ){ + /* Count more matching digits after the decimal point */ + i++; + j++; + while( zA[i]==zB[j] && isdigit(zA[i]) ){ i++; j++; nDigit++; } + if( zA[i]==0 ){ + while( zB[j]=='0' || (isdigit(zB[j]) && nDigit>=15) ){ j++; nDigit++; } + break; + } + if( zB[j]==0 ){ + while( zA[i]=='0' || (isdigit(zA[i]) && nDigit>=15) ){ i++; nDigit++; } + break; + } + if( isspace(zA[i]) && isspace(zB[j]) ) continue; + + if( isdigit(zA[i]) && isdigit(zB[j]) ){ + /* A and B are both digits, but different digits */ + if( zA[i]==zB[j]+1 && !isdigit(zA[i+1]) && isdigit(zB[j+1]) ){ + /* Is A a rounded up version of B? */ + j++; + while( zB[j]=='9' ){ j++; nDigit++; } + if( nDigit<14 && (!isdigit(zB[j]) || zB[j]<5) ) break; + while( isdigit(zB[j]) ){ j++; } + i++; + }else if( zB[j]==zA[i]+1 && !isdigit(zB[j+1]) && isdigit(zA[i+1]) ){ + /* Is B a rounded up version of A? */ + i++; + while( zA[i]=='9' ){ i++; nDigit++; } + if( nDigit<14 && (!isdigit(zA[i]) || zA[i]<5) ) break; + while( isdigit(zA[i]) ){ i++; } + j++; + }else{ + break; + } + }else if( !isdigit(zA[i]) && isdigit(zB[j]) ){ + while( zB[j]=='0' ){ j++; nDigit++; } + if( nDigit<15 ) break; + while( isdigit(zB[j]) ){ j++; } + }else if( !isdigit(zB[j]) && isdigit(zA[i]) ){ + while( zA[i]=='0' ){ i++; nDigit++; } + if( nDigit<15 ) break; + while( isdigit(zA[i]) ){ i++; } + }else{ + break; + } + } + if( zA[i]=='e' && zB[j]=='e' ){ + i++; + j++; + if( (zA[i]=='+' || zA[i]=='-') && zB[j]==zA[i] ){ i++; j++; } + if( zA[i]!=zB[j] ){ + if( zA[i]=='0' && zA[i+1]==zB[j] ){ i++; } + if( zB[j]=='0' && zB[j+1]==zA[i] ){ j++; } + } + while( zA[i]==zB[j] && isdigit(zA[i]) ){ i++; j++; } + if( zA[i]!=zB[j] ) break; + if( zA[i]==0 ) break; + continue; + } + } + while( isspace(zA[i]) ){ i++; } + while( isspace(zB[j]) ){ j++; } + Tcl_SetObjResult(interp, Tcl_NewIntObj(zA[i]==0 && zB[j]==0)); + return TCL_OK; +} + /* ** Usage: sqlite3_release_memory ?N? ** @@ -4770,7 +6338,7 @@ static int tcl_variable_type( ** The integer N is the number of bytes we are trying to release. The ** return value is the amount of memory actually released. */ -static int test_release_memory( +static int SQLITE_TCLAPI test_release_memory( void * clientData, Tcl_Interp *interp, int objc, @@ -4801,7 +6369,7 @@ static int test_release_memory( ** Attempt to release memory currently held by database DB. Return the ** result code (which in the current implementation is always zero). */ -static int test_db_release_memory( +static int SQLITE_TCLAPI test_db_release_memory( void * clientData, Tcl_Interp *interp, int objc, @@ -4824,7 +6392,7 @@ static int test_db_release_memory( ** ** Attempt to flush any dirty pages to disk. */ -static int test_db_cacheflush( +static int SQLITE_TCLAPI test_db_cacheflush( void * clientData, Tcl_Interp *interp, int objc, @@ -4847,12 +6415,35 @@ static int test_db_cacheflush( return TCL_OK; } +/* +** Usage: sqlite3_system_errno DB +** +** Return the low-level system errno value. +*/ +static int SQLITE_TCLAPI test_system_errno( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + sqlite3 *db; + int iErrno; + if( objc!=2 ){ + Tcl_WrongNumArgs(interp, 1, objv, "DB"); + return TCL_ERROR; + } + if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; + iErrno = sqlite3_system_errno(db); + Tcl_SetObjResult(interp, Tcl_NewIntObj(iErrno)); + return TCL_OK; +} + /* ** Usage: sqlite3_db_filename DB DBNAME ** ** Return the name of a file associated with a database. */ -static int test_db_filename( +static int SQLITE_TCLAPI test_db_filename( void * clientData, Tcl_Interp *interp, int objc, @@ -4876,7 +6467,7 @@ static int test_db_filename( ** Return 1 or 0 if DBNAME is readonly or not. Return -1 if DBNAME does ** not exist. */ -static int test_db_readonly( +static int SQLITE_TCLAPI test_db_readonly( void * clientData, Tcl_Interp *interp, int objc, @@ -4901,7 +6492,7 @@ static int test_db_readonly( ** limit is only changed if the N is present. The previous limit ** is returned. */ -static int test_soft_heap_limit( +static int SQLITE_TCLAPI test_soft_heap_limit( void * clientData, Tcl_Interp *interp, int objc, @@ -4921,12 +6512,39 @@ static int test_soft_heap_limit( return TCL_OK; } +/* +** Usage: sqlite3_hard_heap_limit ?N? +** +** Query or set the hard heap limit for the current thread. The +** limit is only changed if the N is present. The previous limit +** is returned. +*/ +static int SQLITE_TCLAPI test_hard_heap_limit( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + sqlite3_int64 amt; + Tcl_WideInt N = -1; + if( objc!=1 && objc!=2 ){ + Tcl_WrongNumArgs(interp, 1, objv, "?N?"); + return TCL_ERROR; + } + if( objc==2 ){ + if( Tcl_GetWideIntFromObj(interp, objv[1], &N) ) return TCL_ERROR; + } + amt = sqlite3_hard_heap_limit64(N); + Tcl_SetObjResult(interp, Tcl_NewWideIntObj(amt)); + return TCL_OK; +} + /* ** Usage: sqlite3_thread_cleanup ** ** Call the sqlite3_thread_cleanup API. */ -static int test_thread_cleanup( +static int SQLITE_TCLAPI test_thread_cleanup( void * clientData, Tcl_Interp *interp, int objc, @@ -4944,7 +6562,7 @@ static int test_thread_cleanup( ** Return a list of numbers which are the PagerRefcount for all ** pagers on each database connection. */ -static int test_pager_refcounts( +static int SQLITE_TCLAPI test_pager_refcounts( void * clientData, Tcl_Interp *interp, int objc, @@ -4957,7 +6575,7 @@ static int test_pager_refcounts( if( objc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", - Tcl_GetStringFromObj(objv[0], 0), " DB", 0); + Tcl_GetStringFromObj(objv[0], 0), " DB", NULL); return TCL_ERROR; } if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; @@ -4990,7 +6608,7 @@ static int test_pager_refcounts( ** and that the errors they are seeing in the test scripts might be ** a result of their defective TCL rather than problems in SQLite. */ -static int working_64bit_int( +static int SQLITE_TCLAPI working_64bit_int( ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ @@ -5015,7 +6633,7 @@ static int working_64bit_int( ** VFS when none are previously registered, and the ability to ** unregister the only available VFS. Ticket #2738 */ -static int vfs_unlink_test( +static int SQLITE_TCLAPI vfs_unlink_test( ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ @@ -5044,7 +6662,7 @@ static int vfs_unlink_test( assert( sqlite3_vfs_find("__two")==&two ); /* Calling sqlite_vfs_register with non-zero second parameter changes the - ** default VFS, even if the 1st parameter is an existig VFS that is + ** default VFS, even if the 1st parameter is an existing VFS that is ** previously registered as the non-default. */ sqlite3_vfs_register(&one, 1); @@ -5117,7 +6735,7 @@ static int vfs_unlink_test( ** This TCL command attempts to vfs_find and vfs_register when the ** sqlite3_initialize() interface is failing. All calls should fail. */ -static int vfs_initfail_test( +static int SQLITE_TCLAPI vfs_initfail_test( ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ @@ -5145,7 +6763,7 @@ static int nVfs = 0; ** ** Unregister all VFSes. */ -static int vfs_unregister_all( +static int SQLITE_TCLAPI vfs_unregister_all( ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ @@ -5163,17 +6781,19 @@ static int vfs_unregister_all( /* ** tclcmd: vfs_reregister_all ** -** Restore all VFSes that were removed using vfs_unregister_all +** Restore all VFSes that were removed using vfs_unregister_all. Taking +** care to put the linked list back together in the same order as it was +** in before vfs_unregister_all was invoked. */ -static int vfs_reregister_all( +static int SQLITE_TCLAPI vfs_reregister_all( ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ Tcl_Obj *CONST objv[] /* Command arguments */ ){ int i; - for(i=0; i<nVfs; i++){ - sqlite3_vfs_register(apVfs[i], i==0); + for(i=nVfs-1; i>=0; i--){ + sqlite3_vfs_register(apVfs[i], 1); } return TCL_OK; } @@ -5185,7 +6805,7 @@ static int vfs_reregister_all( ** This TCL command runs the sqlite3_file_control interface and ** verifies correct operation of the same. */ -static int file_control_test( +static int SQLITE_TCLAPI file_control_test( ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ @@ -5197,7 +6817,7 @@ static int file_control_test( if( objc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", - Tcl_GetStringFromObj(objv[0], 0), " DB", 0); + Tcl_GetStringFromObj(objv[0], 0), " DB", NULL); return TCL_ERROR; } if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; @@ -5220,7 +6840,7 @@ static int file_control_test( ** This TCL command runs the sqlite3_file_control interface and ** verifies correct operation of the SQLITE_LAST_ERRNO verb. */ -static int file_control_lasterrno_test( +static int SQLITE_TCLAPI file_control_lasterrno_test( ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ @@ -5232,7 +6852,7 @@ static int file_control_lasterrno_test( if( objc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", - Tcl_GetStringFromObj(objv[0], 0), " DB", 0); + Tcl_GetStringFromObj(objv[0], 0), " DB", NULL); return TCL_ERROR; } if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ @@ -5245,10 +6865,48 @@ static int file_control_lasterrno_test( } if( iArg!=0 ) { Tcl_AppendResult(interp, "Unexpected non-zero errno: ", - Tcl_GetStringFromObj(Tcl_NewIntObj(iArg), 0), " ", 0); + Tcl_GetStringFromObj(Tcl_NewIntObj(iArg), 0), " ", NULL); + return TCL_ERROR; + } + return TCL_OK; +} + +/* +** tclcmd: file_control_data_version DB DBNAME +** +** This TCL command runs the sqlite3_file_control with the +** SQLITE_FCNTL_DATA_VERSION opcode, returning the result. +*/ +static int SQLITE_TCLAPI file_control_data_version( + ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ + Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ + int objc, /* Number of arguments */ + Tcl_Obj *CONST objv[] /* Command arguments */ +){ + unsigned int iVers; /* data version */ + char *zDb; /* Db name ("main", "temp" etc.) */ + sqlite3 *db; /* Database handle */ + int rc; /* file_control() return code */ + char zBuf[100]; + + if( objc!=3 && objc!=2 ){ + Tcl_WrongNumArgs(interp, 1, objv, "DB [DBNAME]"); + return TCL_ERROR; + } + if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ + return TCL_ERROR; + } + zDb = objc==3 ? Tcl_GetString(objv[2]) : NULL; + + rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_DATA_VERSION, (void *)&iVers); + if( rc ){ + Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC); return TCL_ERROR; + }else{ + sqlite3_snprintf(sizeof(zBuf),zBuf,"%u",iVers); + Tcl_SetResult(interp, (char *)zBuf, TCL_VOLATILE); + return TCL_OK; } - return TCL_OK; } /* @@ -5258,7 +6916,7 @@ static int file_control_lasterrno_test( ** verifies correct operation of the SQLITE_GET_LOCKPROXYFILE and ** SQLITE_SET_LOCKPROXYFILE verbs. */ -static int file_control_chunksize_test( +static int SQLITE_TCLAPI file_control_chunksize_test( ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ @@ -5295,7 +6953,7 @@ static int file_control_chunksize_test( ** This TCL command runs the sqlite3_file_control interface ** with SQLITE_FCNTL_SIZE_HINT */ -static int file_control_sizehint_test( +static int SQLITE_TCLAPI file_control_sizehint_test( ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ @@ -5333,7 +6991,7 @@ static int file_control_sizehint_test( ** verifies correct operation of the SQLITE_GET_LOCKPROXYFILE and ** SQLITE_SET_LOCKPROXYFILE verbs. */ -static int file_control_lockproxy_test( +static int SQLITE_TCLAPI file_control_lockproxy_test( ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ @@ -5343,7 +7001,7 @@ static int file_control_lockproxy_test( if( objc!=3 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", - Tcl_GetStringFromObj(objv[0], 0), " DB PWD", 0); + Tcl_GetStringFromObj(objv[0], 0), " DB PWD", NULL); return TCL_ERROR; } if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ @@ -5361,7 +7019,7 @@ static int file_control_lockproxy_test( { char *testPath; int rc; - int nPwd; + Tcl_Size nPwd; const char *zPwd; char proxyPath[400]; @@ -5379,7 +7037,7 @@ static int file_control_lockproxy_test( rc = sqlite3_file_control(db, NULL, SQLITE_GET_LOCKPROXYFILE, &testPath); if( strncmp(proxyPath,testPath,11) ){ Tcl_AppendResult(interp, "Lock proxy file did not match the " - "previously assigned value", 0); + "previously assigned value", NULL); return TCL_ERROR; } if( rc ){ @@ -5403,7 +7061,7 @@ static int file_control_lockproxy_test( ** This TCL command runs the sqlite3_file_control interface with ** the SQLITE_FCNTL_WIN32_AV_RETRY opcode. */ -static int file_control_win32_av_retry( +static int SQLITE_TCLAPI file_control_win32_av_retry( ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ @@ -5416,7 +7074,7 @@ static int file_control_win32_av_retry( if( objc!=4 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", - Tcl_GetStringFromObj(objv[0], 0), " DB NRETRY DELAY", 0); + Tcl_GetStringFromObj(objv[0], 0), " DB NRETRY DELAY", NULL); return TCL_ERROR; } if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ @@ -5430,13 +7088,45 @@ static int file_control_win32_av_retry( return TCL_OK; } +/* +** tclcmd: file_control_win32_get_handle DB +** +** This TCL command runs the sqlite3_file_control interface with +** the SQLITE_FCNTL_WIN32_GET_HANDLE opcode. +*/ +static int file_control_win32_get_handle( + ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ + Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ + int objc, /* Number of arguments */ + Tcl_Obj *CONST objv[] /* Command arguments */ +){ + sqlite3 *db; + int rc; + HANDLE hFile = NULL; + char z[100]; + + if( objc!=2 ){ + Tcl_AppendResult(interp, "wrong # args: should be \"", + Tcl_GetStringFromObj(objv[0], 0), " DB", NULL); + return TCL_ERROR; + } + if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ + return TCL_ERROR; + } + rc = sqlite3_file_control(db, NULL, SQLITE_FCNTL_WIN32_GET_HANDLE, + (void*)&hFile); + sqlite3_snprintf(sizeof(z), z, "%d %p", rc, (void*)hFile); + Tcl_AppendResult(interp, z, (char*)0); + return TCL_OK; +} + /* ** tclcmd: file_control_win32_set_handle DB HANDLE ** ** This TCL command runs the sqlite3_file_control interface with ** the SQLITE_FCNTL_WIN32_SET_HANDLE opcode. */ -static int file_control_win32_set_handle( +static int SQLITE_TCLAPI file_control_win32_set_handle( ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ @@ -5449,7 +7139,7 @@ static int file_control_win32_set_handle( if( objc!=3 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", - Tcl_GetStringFromObj(objv[0], 0), " DB HANDLE", 0); + Tcl_GetStringFromObj(objv[0], 0), " DB HANDLE", NULL); return TCL_ERROR; } if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ @@ -5472,7 +7162,7 @@ static int file_control_win32_set_handle( ** This TCL command runs the sqlite3_file_control interface with ** the SQLITE_FCNTL_PERSIST_WAL opcode. */ -static int file_control_persist_wal( +static int SQLITE_TCLAPI file_control_persist_wal( ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ @@ -5485,7 +7175,7 @@ static int file_control_persist_wal( if( objc!=3 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", - Tcl_GetStringFromObj(objv[0], 0), " DB FLAG", 0); + Tcl_GetStringFromObj(objv[0], 0), " DB FLAG", NULL); return TCL_ERROR; } if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ @@ -5504,7 +7194,7 @@ static int file_control_persist_wal( ** This TCL command runs the sqlite3_file_control interface with ** the SQLITE_FCNTL_POWERSAFE_OVERWRITE opcode. */ -static int file_control_powersafe_overwrite( +static int SQLITE_TCLAPI file_control_powersafe_overwrite( ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ @@ -5517,7 +7207,7 @@ static int file_control_powersafe_overwrite( if( objc!=3 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", - Tcl_GetStringFromObj(objv[0], 0), " DB FLAG", 0); + Tcl_GetStringFromObj(objv[0], 0), " DB FLAG", NULL); return TCL_ERROR; } if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ @@ -5536,7 +7226,7 @@ static int file_control_powersafe_overwrite( ** ** Return a string that describes the stack of VFSes. */ -static int file_control_vfsname( +static int SQLITE_TCLAPI file_control_vfsname( ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ @@ -5548,7 +7238,7 @@ static int file_control_vfsname( if( objc!=2 && objc!=3 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", - Tcl_GetStringFromObj(objv[0], 0), " DB ?AUXDB?", 0); + Tcl_GetStringFromObj(objv[0], 0), " DB ?AUXDB?", NULL); return TCL_ERROR; } if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ @@ -5563,12 +7253,42 @@ static int file_control_vfsname( return TCL_OK; } +/* +** tclcmd: file_control_reservebytes DB N +*/ +static int SQLITE_TCLAPI file_control_reservebytes( + ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ + Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ + int objc, /* Number of arguments */ + Tcl_Obj *CONST objv[] /* Command arguments */ +){ + sqlite3 *db; + const char *zDbName = "main"; + int n = 0; + int rc; + + if( objc!=3 ){ + Tcl_WrongNumArgs(interp, 1, objv, "DB N"); + return TCL_ERROR; + } + if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) + || Tcl_GetIntFromObj(interp, objv[2], &n) + ){ + return TCL_ERROR; + } + + rc = sqlite3_file_control(db, zDbName, SQLITE_FCNTL_RESERVE_BYTES, (void*)&n); + Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1)); + return TCL_OK; +} + + /* ** tclcmd: file_control_tempfilename DB ?AUXDB? ** ** Return a string that is a temporary filename */ -static int file_control_tempfilename( +static int SQLITE_TCLAPI file_control_tempfilename( ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ @@ -5580,7 +7300,7 @@ static int file_control_tempfilename( if( objc!=2 && objc!=3 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", - Tcl_GetStringFromObj(objv[0], 0), " DB ?AUXDB?", 0); + Tcl_GetStringFromObj(objv[0], 0), " DB ?AUXDB?", NULL); return TCL_ERROR; } if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ @@ -5595,13 +7315,49 @@ static int file_control_tempfilename( return TCL_OK; } +/* +** tclcmd: file_control_external_reader DB ?AUXDB? +** +** Return a string that is a temporary filename +*/ +static int SQLITE_TCLAPI file_control_external_reader( + ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ + Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ + int objc, /* Number of arguments */ + Tcl_Obj *CONST objv[] /* Command arguments */ +){ + sqlite3 *db; + const char *zName = "main"; + int iRes = 0; + int rc = SQLITE_OK; + + if( objc!=2 && objc!=3 ){ + Tcl_AppendResult(interp, "wrong # args: should be \"", + Tcl_GetStringFromObj(objv[0], 0), " DB ?AUXDB?", NULL); + return TCL_ERROR; + } + if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ + return TCL_ERROR; + } + if( objc==3 ){ + zName = Tcl_GetString(objv[2]); + } + rc = sqlite3_file_control(db, zName, SQLITE_FCNTL_EXTERNAL_READER, &iRes); + if( rc!=SQLITE_OK ){ + Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); + return TCL_ERROR; + } + Tcl_SetObjResult(interp, Tcl_NewIntObj(iRes)); + return TCL_OK; +} + /* ** tclcmd: sqlite3_vfs_list ** ** Return a tcl list containing the names of all registered vfs's. */ -static int vfs_list( +static int SQLITE_TCLAPI vfs_list( ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ @@ -5626,7 +7382,7 @@ static int vfs_list( ** This TCL command runs the sqlite3_limit interface and ** verifies correct operation of the same. */ -static int test_limit( +static int SQLITE_TCLAPI test_limit( ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ @@ -5661,7 +7417,7 @@ static int test_limit( if( objc!=4 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", - Tcl_GetStringFromObj(objv[0], 0), " DB ID VALUE", 0); + Tcl_GetStringFromObj(objv[0], 0), " DB ID VALUE", NULL); return TCL_ERROR; } if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; @@ -5689,7 +7445,7 @@ static int test_limit( ** At the same time, verify that sqlite3_test_control works even when ** called with an out-of-range opcode. */ -static int save_prng_state( +static int SQLITE_TCLAPI save_prng_state( ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ @@ -5705,7 +7461,7 @@ static int save_prng_state( /* ** tclcmd: restore_prng_state */ -static int restore_prng_state( +static int SQLITE_TCLAPI restore_prng_state( ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ @@ -5717,23 +7473,81 @@ static int restore_prng_state( /* ** tclcmd: reset_prng_state */ -static int reset_prng_state( +static int SQLITE_TCLAPI reset_prng_state( + ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ + Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ + int objc, /* Number of arguments */ + Tcl_Obj *CONST objv[] /* Command arguments */ +){ + sqlite3_randomness(0,0); + return TCL_OK; +} +/* +** tclcmd: prng_seed INT ?DB? +** +** Set up the SQLITE_TESTCTRL_PRNG_SEED pragma with parameter INT and DB. +** INT is an integer. DB is a database connection, or a NULL pointer if +** omitted. +** +** When INT!=0 and DB!=0, set the PRNG seed to the value of the schema +** cookie for DB, or to INT if the schema cookie happens to be zero. +** +** When INT!=0 and DB==0, set the PRNG seed to just INT. +** +** If INT==0 and DB==0 then use the default procedure of calling the +** xRandomness method on the default VFS to get the PRNG seed. +*/ +static int SQLITE_TCLAPI prng_seed( + ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ + Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ + int objc, /* Number of arguments */ + Tcl_Obj *CONST objv[] /* Command arguments */ +){ + int i = 0; + sqlite3 *db = 0; + if( objc!=2 && objc!=3 ){ + Tcl_WrongNumArgs(interp, 1, objv, "SEED ?DB?"); + return TCL_ERROR; + } + if( Tcl_GetIntFromObj(interp,objv[1],&i) ) return TCL_ERROR; + if( objc==3 && getDbPointer(interp, Tcl_GetString(objv[2]), &db) ){ + return TCL_ERROR; + } + sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SEED, i, db); + return TCL_OK; +} + +/* +** tclcmd: extra_schema_checks BOOLEAN +** +** Enable or disable schema checks when parsing the sqlite_schema file. +** This is always enabled in production, but it is sometimes useful to +** disable the checks in order to make some internal error states reachable +** for testing. +*/ +static int SQLITE_TCLAPI extra_schema_checks( ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ Tcl_Obj *CONST objv[] /* Command arguments */ ){ - sqlite3_test_control(SQLITE_TESTCTRL_PRNG_RESET); + int i = 0; + if( objc!=2 ){ + Tcl_WrongNumArgs(interp, 1, objv, "BOOLEAN"); + return TCL_ERROR; + } + if( Tcl_GetBooleanFromObj(interp,objv[1],&i) ) return TCL_ERROR; + sqlite3_test_control(SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS, i); return TCL_OK; } /* ** tclcmd: database_may_be_corrupt ** -** Indicate that database files might be corrupt. In other words, set the normal +** Indicate that database files might be corrupt. In other words, set the normal ** state of operation. */ -static int database_may_be_corrupt( +static int SQLITE_TCLAPI database_may_be_corrupt( ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ @@ -5745,10 +7559,11 @@ static int database_may_be_corrupt( /* ** tclcmd: database_never_corrupt ** -** Indicate that database files are always well-formed. This enables extra assert() -** statements that test conditions that are always true for well-formed databases. +** Indicate that database files are always well-formed. This enables +** extra assert() statements that test conditions that are always true +** for well-formed databases. */ -static int database_never_corrupt( +static int SQLITE_TCLAPI database_never_corrupt( ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ @@ -5761,7 +7576,7 @@ static int database_never_corrupt( /* ** tclcmd: pcache_stats */ -static int test_pcache_stats( +static int SQLITE_TCLAPI test_pcache_stats( ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ @@ -5803,7 +7618,7 @@ static void test_unlock_notify_cb(void **aArg, int nArg){ ** tclcmd: sqlite3_unlock_notify db */ #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY -static int test_unlock_notify( +static int SQLITE_TCLAPI test_unlock_notify( ClientData clientData, /* Unused */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ @@ -5829,7 +7644,7 @@ static int test_unlock_notify( /* ** tclcmd: sqlite3_wal_checkpoint db ?NAME? */ -static int test_wal_checkpoint( +static int SQLITE_TCLAPI test_wal_checkpoint( ClientData clientData, /* Unused */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ @@ -5873,7 +7688,7 @@ static int test_wal_checkpoint( ** the number of frames in the log and the number of frames in the log ** that have been checkpointed. */ -static int test_wal_checkpoint_v2( +static int SQLITE_TCLAPI test_wal_checkpoint_v2( ClientData clientData, /* Unused */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ @@ -5913,7 +7728,7 @@ static int test_wal_checkpoint_v2( if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){ const char *zErrCode = sqlite3ErrName(rc); Tcl_ResetResult(interp); - Tcl_AppendResult(interp, zErrCode, " - ", (char *)sqlite3_errmsg(db), 0); + Tcl_AppendResult(interp, zErrCode, " - ", (char *)sqlite3_errmsg(db), NULL); return TCL_ERROR; } @@ -5929,7 +7744,7 @@ static int test_wal_checkpoint_v2( /* ** tclcmd: sqlite3_wal_autocheckpoint db VALUE */ -static int test_wal_autocheckpoint( +static int SQLITE_TCLAPI test_wal_autocheckpoint( ClientData clientData, /* Unused */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ @@ -5965,12 +7780,16 @@ static int test_wal_autocheckpoint( /* ** tclcmd: test_sqlite3_log ?SCRIPT? +** +** Caution: If you register a log callback, you must deregister it (by +** invoking test_sqlite3_log with no arguments) prior to closing the +** Tcl interpreter or else a memory error will occur. */ static struct LogCallback { Tcl_Interp *pInterp; Tcl_Obj *pObj; } logcallback = {0, 0}; -static void xLogcallback(void *unused, int err, char *zMsg){ +static void xLogcallback(void *unused, int err, const char *zMsg){ Tcl_Obj *pNew = Tcl_DuplicateObj(logcallback.pObj); Tcl_IncrRefCount(pNew); Tcl_ListObjAppendElement( @@ -5980,7 +7799,7 @@ static void xLogcallback(void *unused, int err, char *zMsg){ Tcl_EvalObjEx(logcallback.pInterp, pNew, TCL_EVAL_GLOBAL|TCL_EVAL_DIRECT); Tcl_DecrRefCount(pNew); } -static int test_sqlite3_log( +static int SQLITE_TCLAPI test_sqlite3_log( ClientData clientData, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ @@ -5996,7 +7815,7 @@ static int test_sqlite3_log( logcallback.pInterp = 0; sqlite3_config(SQLITE_CONFIG_LOG, (void*)0, (void*)0); } - if( objc>1 ){ + if( objc>1 && Tcl_GetString(objv[1])[0]!=0 ){ logcallback.pObj = objv[1]; Tcl_IncrRefCount(logcallback.pObj); logcallback.pInterp = interp; @@ -6011,7 +7830,7 @@ static int test_sqlite3_log( ** Run a TCL command using its objProc interface. Throw an error if ** the command has no objProc interface. */ -static int runAsObjProc( +static int SQLITE_TCLAPI runAsObjProc( void * clientData, Tcl_Interp *interp, int objc, @@ -6074,7 +7893,7 @@ int printExplainQueryPlan(sqlite3_stmt *pStmt){ return sqlite3_finalize(pExplain); } -static int test_print_eqp( +static int SQLITE_TCLAPI test_print_eqp( void * clientData, Tcl_Interp *interp, int objc, @@ -6099,10 +7918,114 @@ static int test_print_eqp( } #endif /* SQLITE_OMIT_EXPLAIN */ +#include <time.h> +/* +** This is an alternative localtime_r() implementation used for testing +** the 'localtime' and 'utc' modifiers of date-time functions. Because +** the OS-supplied localtime_r() is locale-dependent, this alternative is +** provided as a stable test platform. +** +** Operation: +** +** (1) Localtime is 30 minutes earlier than (west of) UTC on +** even days (counting from 1970-01-01) +** +** (2) Localtime is 30 minutes later than (east of) UTC on odd days. +** +** (3) The function fails for the specific date/time value +** of 2000-05-29 14:16:00 in order to test the ability of +** SQLite to deal with localtime_r() failures. +*/ +static int testLocaltime(const void *aliasT, void *aliasTM){ + const time_t t = *(const time_t*)aliasT; + struct tm *pTm = (struct tm *)aliasTM; + time_t altT; + sqlite3_int64 iJD; + int Z, A, B, C, D, E, X1, S; + + if( (t/86400) & 1 ){ + altT = t + 1800; /* 30 minutes later on odd days */ + }else{ + altT = t - 1800; /* 30 minutes earlier on even days */ + } + iJD = (sqlite3_int64)(altT + 210866760000); + Z = (int)((iJD + 43200)/86400); + A = (int)((Z - 1867216.25)/36524.25); + A = Z + 1 + A - (A/4); + B = A + 1524; + C = (int)((B - 122.1)/365.25); + D = (36525*(C&32767))/100; + E = (int)((B-D)/30.6001); + X1 = (int)(30.6001*E); + pTm->tm_mday = B - D - X1; + pTm->tm_mon = E<14 ? E-2 : E-14; + pTm->tm_year = (pTm->tm_mon>1 ? C - 4716 : C - 4715) - 1900; + S = (int)((iJD + 43200)%86400); + pTm->tm_hour = S/3600; + pTm->tm_min = (S/60)%60; + pTm->tm_sec = S % 60; + return t==959609760; /* Special case: 2000-05-29 14:16:00 fails */ +} + +/* +** TCLCMD: strftime FORMAT UNIXTIMESTAMP +** +** Access to the C-library strftime() routine, so that its results +** can be compared against SQLite's internal strftime() SQL function +** implementation. +*/ +static int SQLITE_TCLAPI strftime_cmd( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + Tcl_WideInt ts; + time_t t; + struct tm *pTm; + const char *zFmt; + size_t n; + char zBuf[1000]; + if( objc!=3 ){ + Tcl_WrongNumArgs(interp, 1, objv, "FORMAT UNIXTIMESTAMP"); + return TCL_ERROR; + } + if( Tcl_GetWideIntFromObj(interp, objv[2], &ts) ) return TCL_ERROR; + zFmt = Tcl_GetString(objv[1]); + t = (time_t)ts; + pTm = gmtime(&t); + n = strftime(zBuf, sizeof(zBuf)-1, zFmt, pTm); + if( n>=0 && n<sizeof(zBuf) ){ + zBuf[n] = 0; + Tcl_SetResult(interp, zBuf, TCL_VOLATILE); + } + return TCL_OK; +} + +/* +** .treetrace N +*/ +static int SQLITE_TCLAPI test_treetrace( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + unsigned int v = 0; + if( objc>=2 ){ + if( Tcl_GetIntFromObj(interp, objv[1], (int*)&v)==TCL_OK ){ + sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &v); + } + } + sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 0, &v); + Tcl_SetObjResult(interp, Tcl_NewIntObj((int)v)); + return TCL_OK; +} + /* ** sqlite3_test_control VERB ARGS... */ -static int test_test_control( +static int SQLITE_TCLAPI test_test_control( void * clientData, Tcl_Interp *interp, int objc, @@ -6112,9 +8035,12 @@ static int test_test_control( const char *zName; int i; } aVerb[] = { - { "SQLITE_TESTCTRL_LOCALTIME_FAULT", SQLITE_TESTCTRL_LOCALTIME_FAULT }, - { "SQLITE_TESTCTRL_SORTER_MMAP", SQLITE_TESTCTRL_SORTER_MMAP }, - { "SQLITE_TESTCTRL_IMPOSTER", SQLITE_TESTCTRL_IMPOSTER }, + { "SQLITE_TESTCTRL_LOCALTIME_FAULT", SQLITE_TESTCTRL_LOCALTIME_FAULT }, + { "SQLITE_TESTCTRL_SORTER_MMAP", SQLITE_TESTCTRL_SORTER_MMAP }, + { "SQLITE_TESTCTRL_IMPOSTER", SQLITE_TESTCTRL_IMPOSTER }, + { "SQLITE_TESTCTRL_INTERNAL_FUNCTIONS", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS}, + { "SQLITE_TESTCTRL_FK_NO_ACTION", SQLITE_TESTCTRL_FK_NO_ACTION}, + { 0, 0 } }; int iVerb; int iFlag; @@ -6132,14 +8058,38 @@ static int test_test_control( iFlag = aVerb[iVerb].i; switch( iFlag ){ + case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS: { + sqlite3 *db = 0; + if( objc!=3 ){ + Tcl_WrongNumArgs(interp, 2, objv, "DB"); + return TCL_ERROR; + } + if( getDbPointer(interp, Tcl_GetString(objv[2]), &db) ) return TCL_ERROR; + sqlite3_test_control(SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, db); + break; + } case SQLITE_TESTCTRL_LOCALTIME_FAULT: { int val; if( objc!=3 ){ - Tcl_WrongNumArgs(interp, 2, objv, "ONOFF"); + Tcl_WrongNumArgs(interp, 2, objv, "0|1|2"); + return TCL_ERROR; + } + if( Tcl_GetIntFromObj(interp, objv[2], &val) ) return TCL_ERROR; + sqlite3_test_control(iFlag, val, testLocaltime); + break; + } + + case SQLITE_TESTCTRL_FK_NO_ACTION: { + int val = 0; + sqlite3 *db = 0; + if( objc!=4 ){ + Tcl_WrongNumArgs(interp, 2, objv, "DB BOOLEAN"); return TCL_ERROR; } - if( Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR; - sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, val); + if( getDbPointer(interp, Tcl_GetString(objv[2]), &db) ) return TCL_ERROR; + if( Tcl_GetBooleanFromObj(interp, objv[3], &val) ) return TCL_ERROR; + + sqlite3_test_control(SQLITE_TESTCTRL_FK_NO_ACTION, db, val); break; } @@ -6181,7 +8131,7 @@ static int test_test_control( #include <sys/time.h> #include <sys/resource.h> -static int test_getrusage( +static int SQLITE_TCLAPI test_getrusage( void * clientData, Tcl_Interp *interp, int objc, @@ -6210,7 +8160,7 @@ static int test_getrusage( */ struct win32FileLocker { char *evName; /* Name of event to signal thread startup */ - HANDLE h; /* Handle of the file to be locked */ + sqlite3_file *pFd; /* Handle of the file to be locked */ int delay1; /* Delay before locking */ int delay2; /* Delay before unlocking */ int ok; /* Finished ok */ @@ -6219,13 +8169,15 @@ struct win32FileLocker { #endif -#if SQLITE_OS_WIN +#ifdef _WIN32 #include <process.h> /* ** The background thread that does file locking. */ -static void win32_file_locker(void *pAppData){ +static void SQLITE_CDECL win32_file_locker(void *pAppData){ struct win32FileLocker *p = (struct win32FileLocker*)pAppData; + sqlite3_file *pFd = p->pFd; + HANDLE h = INVALID_HANDLE_VALUE; if( p->evName ){ HANDLE ev = OpenEvent(EVENT_MODIFY_STATE, FALSE, p->evName); if ( ev ){ @@ -6234,65 +8186,86 @@ static void win32_file_locker(void *pAppData){ } } if( p->delay1 ) Sleep(p->delay1); - if( LockFile(p->h, 0, 0, 100000000, 0) ){ + pFd->pMethods->xFileControl(pFd, SQLITE_FCNTL_WIN32_GET_HANDLE, (void*)&h); + if( LockFile(h, 0, 0, 100000000, 0) ){ Sleep(p->delay2); - UnlockFile(p->h, 0, 0, 100000000, 0); + UnlockFile(h, 0, 0, 100000000, 0); p->ok = 1; }else{ p->err = 1; } - CloseHandle(p->h); - p->h = 0; + pFd->pMethods->xClose(pFd); + sqlite3_free(pFd); + p->pFd = 0; p->delay1 = 0; p->delay2 = 0; } #endif -#if SQLITE_OS_WIN +#ifdef _WIN32 /* ** lock_win32_file FILENAME DELAY1 DELAY2 ** ** Get an exclusive manditory lock on file for DELAY2 milliseconds. ** Wait DELAY1 milliseconds before acquiring the lock. */ -static int win32_file_lock( +static int SQLITE_TCLAPI win32_file_lock( void * clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[] ){ static struct win32FileLocker x = { "win32_file_lock", 0, 0, 0, 0, 0 }; - const char *zFilename; + const char *zFilename = 0; + Tcl_Size nFilename = 0; + char *zTerm = 0; char zBuf[200]; int retry = 0; HANDLE ev; DWORD wResult; + sqlite3_vfs *pVfs = 0; + int flags = SQLITE_OPEN_MAIN_DB | SQLITE_OPEN_READWRITE; + int rc = SQLITE_OK; if( objc!=4 && objc!=1 ){ Tcl_WrongNumArgs(interp, 1, objv, "FILENAME DELAY1 DELAY2"); return TCL_ERROR; } if( objc==1 ){ + HANDLE h = INVALID_HANDLE_VALUE; + if( x.pFd ){ + x.pFd->pMethods->xFileControl( + x.pFd, SQLITE_FCNTL_WIN32_GET_HANDLE, (void*)&h + ); + } sqlite3_snprintf(sizeof(zBuf), zBuf, "%d %d %d %d %d", - x.ok, x.err, x.delay1, x.delay2, x.h); + x.ok, x.err, x.delay1, x.delay2, h); Tcl_AppendResult(interp, zBuf, (char*)0); return TCL_OK; } - while( x.h && retry<30 ){ + while( x.pFd && retry<30 ){ retry++; Sleep(100); } - if( x.h ){ + if( x.pFd ){ Tcl_AppendResult(interp, "busy", (char*)0); return TCL_ERROR; } if( Tcl_GetIntFromObj(interp, objv[2], &x.delay1) ) return TCL_ERROR; if( Tcl_GetIntFromObj(interp, objv[3], &x.delay2) ) return TCL_ERROR; - zFilename = Tcl_GetString(objv[1]); - x.h = CreateFile(zFilename, GENERIC_READ|GENERIC_WRITE, - FILE_SHARE_READ|FILE_SHARE_WRITE, 0, OPEN_ALWAYS, - FILE_ATTRIBUTE_NORMAL, 0); - if( !x.h ){ + pVfs = sqlite3_vfs_find(0); + x.pFd = (sqlite3_file*)sqlite3_malloc(pVfs->szOsFile); + + /* xOpen() must be passed a dual-nul-terminated string preceded in memory + ** by 4 0x00 bytes. */ + zFilename = Tcl_GetStringFromObj(objv[1], &nFilename); + zTerm = (char*)sqlite3_malloc(nFilename+6); + memset(zTerm, 0, nFilename+6); + memcpy(&zTerm[4], zFilename, nFilename); + rc = pVfs->xOpen(pVfs, &zTerm[4], x.pFd, flags, &flags); + sqlite3_free(zTerm); + + if( rc!=SQLITE_OK ){ Tcl_AppendResult(interp, "cannot open file: ", zFilename, (char*)0); return TCL_ERROR; } @@ -6312,145 +8285,6 @@ static int win32_file_lock( CloseHandle(ev); return TCL_OK; } - -/* -** exists_win32_path PATH -** -** Returns non-zero if the specified path exists, whose fully qualified name -** may exceed 260 characters if it is prefixed with "\\?\". -*/ -static int win32_exists_path( - void *clientData, - Tcl_Interp *interp, - int objc, - Tcl_Obj *CONST objv[] -){ - if( objc!=2 ){ - Tcl_WrongNumArgs(interp, 1, objv, "PATH"); - return TCL_ERROR; - } - Tcl_SetObjResult(interp, Tcl_NewBooleanObj( - GetFileAttributesW( Tcl_GetUnicode(objv[1]))!=INVALID_FILE_ATTRIBUTES )); - return TCL_OK; -} - -/* -** find_win32_file PATTERN -** -** Returns a list of entries in a directory that match the specified pattern, -** whose fully qualified name may exceed 248 characters if it is prefixed with -** "\\?\". -*/ -static int win32_find_file( - void *clientData, - Tcl_Interp *interp, - int objc, - Tcl_Obj *CONST objv[] -){ - HANDLE hFindFile = INVALID_HANDLE_VALUE; - WIN32_FIND_DATAW findData; - Tcl_Obj *listObj; - DWORD lastErrno; - if( objc!=2 ){ - Tcl_WrongNumArgs(interp, 1, objv, "PATTERN"); - return TCL_ERROR; - } - hFindFile = FindFirstFileW(Tcl_GetUnicode(objv[1]), &findData); - if( hFindFile==INVALID_HANDLE_VALUE ){ - Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError())); - return TCL_ERROR; - } - listObj = Tcl_NewObj(); - Tcl_IncrRefCount(listObj); - do { - Tcl_ListObjAppendElement(interp, listObj, Tcl_NewUnicodeObj( - findData.cFileName, -1)); - Tcl_ListObjAppendElement(interp, listObj, Tcl_NewWideIntObj( - findData.dwFileAttributes)); - } while( FindNextFileW(hFindFile, &findData) ); - lastErrno = GetLastError(); - if( lastErrno!=NO_ERROR && lastErrno!=ERROR_NO_MORE_FILES ){ - FindClose(hFindFile); - Tcl_DecrRefCount(listObj); - Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError())); - return TCL_ERROR; - } - FindClose(hFindFile); - Tcl_SetObjResult(interp, listObj); - return TCL_OK; -} - -/* -** delete_win32_file FILENAME -** -** Deletes the specified file, whose fully qualified name may exceed 260 -** characters if it is prefixed with "\\?\". -*/ -static int win32_delete_file( - void *clientData, - Tcl_Interp *interp, - int objc, - Tcl_Obj *CONST objv[] -){ - if( objc!=2 ){ - Tcl_WrongNumArgs(interp, 1, objv, "FILENAME"); - return TCL_ERROR; - } - if( !DeleteFileW(Tcl_GetUnicode(objv[1])) ){ - Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError())); - return TCL_ERROR; - } - Tcl_ResetResult(interp); - return TCL_OK; -} - -/* -** make_win32_dir DIRECTORY -** -** Creates the specified directory, whose fully qualified name may exceed 248 -** characters if it is prefixed with "\\?\". -*/ -static int win32_mkdir( - void *clientData, - Tcl_Interp *interp, - int objc, - Tcl_Obj *CONST objv[] -){ - if( objc!=2 ){ - Tcl_WrongNumArgs(interp, 1, objv, "DIRECTORY"); - return TCL_ERROR; - } - if( !CreateDirectoryW(Tcl_GetUnicode(objv[1]), NULL) ){ - Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError())); - return TCL_ERROR; - } - Tcl_ResetResult(interp); - return TCL_OK; -} - -/* -** remove_win32_dir DIRECTORY -** -** Removes the specified directory, whose fully qualified name may exceed 248 -** characters if it is prefixed with "\\?\". -*/ -static int win32_rmdir( - void *clientData, - Tcl_Interp *interp, - int objc, - Tcl_Obj *CONST objv[] -){ - if( objc!=2 ){ - Tcl_WrongNumArgs(interp, 1, objv, "DIRECTORY"); - return TCL_ERROR; - } - if( !RemoveDirectoryW(Tcl_GetUnicode(objv[1])) ){ - Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError())); - return TCL_ERROR; - } - Tcl_ResetResult(interp); - return TCL_OK; -} #endif @@ -6459,9 +8293,14 @@ static int win32_rmdir( ** ** Enable or disable query optimizations using the sqlite3_test_control() ** interface. Disable if BOOLEAN is false and enable if BOOLEAN is true. -** OPT is the name of the optimization to be disabled. +** OPT is the name of the optimization to be disabled. OPT can also be a +** list or optimizations names, in which case all optimizations named are +** enabled or disabled. +** +** Each invocation of this control overrides all prior invocations. The +** changes are not cumulative. */ -static int optimization_control( +static int SQLITE_TCLAPI optimization_control( void * clientData, Tcl_Interp *interp, int objc, @@ -6472,6 +8311,7 @@ static int optimization_control( const char *zOpt; int onoff; int mask = 0; + int cnt = 0; static const struct { const char *zOptName; int mask; @@ -6479,17 +8319,21 @@ static int optimization_control( { "all", SQLITE_AllOpts }, { "none", 0 }, { "query-flattener", SQLITE_QueryFlattener }, - { "column-cache", SQLITE_ColumnCache }, { "groupby-order", SQLITE_GroupByOrder }, { "factor-constants", SQLITE_FactorOutConst }, { "distinct-opt", SQLITE_DistinctOpt }, { "cover-idx-scan", SQLITE_CoverIdxScan }, { "order-by-idx-join", SQLITE_OrderByIdxJoin }, + { "order-by-subquery", SQLITE_OrderBySubq }, { "transitive", SQLITE_Transitive }, - { "subquery-coroutine", SQLITE_SubqCoroutine }, { "omit-noop-join", SQLITE_OmitNoopJoin }, - { "stat3", SQLITE_Stat34 }, - { "stat4", SQLITE_Stat34 }, + { "stat4", SQLITE_Stat4 }, + { "skip-scan", SQLITE_SkipScan }, + { "push-down", SQLITE_PushDown }, + { "balanced-merge", SQLITE_BalancedMerge }, + { "propagate-const", SQLITE_PropagateConst }, + { "one-pass", SQLITE_OnePass }, + { "exists-to-join", SQLITE_ExistsToJoin }, }; if( objc!=4 ){ @@ -6500,13 +8344,13 @@ static int optimization_control( if( Tcl_GetBooleanFromObj(interp, objv[3], &onoff) ) return TCL_ERROR; zOpt = Tcl_GetString(objv[2]); for(i=0; i<sizeof(aOpt)/sizeof(aOpt[0]); i++){ - if( strcmp(zOpt, aOpt[i].zOptName)==0 ){ - mask = aOpt[i].mask; - break; + if( strstr(zOpt, aOpt[i].zOptName)!=0 ){ + mask |= aOpt[i].mask; + cnt++; } } if( onoff ) mask = ~mask; - if( i>=sizeof(aOpt)/sizeof(aOpt[0]) ){ + if( cnt==0 ){ Tcl_AppendResult(interp, "unknown optimization - should be one of:", (char*)0); for(i=0; i<sizeof(aOpt)/sizeof(aOpt[0]); i++){ @@ -6515,51 +8359,81 @@ static int optimization_control( return TCL_ERROR; } sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, db, mask); + Tcl_SetObjResult(interp, Tcl_NewIntObj(mask)); return TCL_OK; } -typedef struct sqlite3_api_routines sqlite3_api_routines; /* ** load_static_extension DB NAME ... ** ** Load one or more statically linked extensions. */ -static int tclLoadStaticExtensionCmd( +static int SQLITE_TCLAPI tclLoadStaticExtensionCmd( void * clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[] ){ extern int sqlite3_amatch_init(sqlite3*,char**,const sqlite3_api_routines*); + extern int sqlite3_appendvfs_init(sqlite3*,char**,const sqlite3_api_routines*); + extern int sqlite3_basexx_init(sqlite3*,char**,const sqlite3_api_routines*); extern int sqlite3_closure_init(sqlite3*,char**,const sqlite3_api_routines*); + extern int sqlite3_csv_init(sqlite3*,char**,const sqlite3_api_routines*); extern int sqlite3_eval_init(sqlite3*,char**,const sqlite3_api_routines*); + extern int sqlite3_explain_init(sqlite3*,char**,const sqlite3_api_routines*); extern int sqlite3_fileio_init(sqlite3*,char**,const sqlite3_api_routines*); + extern int sqlite3_decimal_init(sqlite3*,char**,const sqlite3_api_routines*); extern int sqlite3_fuzzer_init(sqlite3*,char**,const sqlite3_api_routines*); extern int sqlite3_ieee_init(sqlite3*,char**,const sqlite3_api_routines*); extern int sqlite3_nextchar_init(sqlite3*,char**,const sqlite3_api_routines*); - extern int sqlite3_percentile_init(sqlite3*,char**,const sqlite3_api_routines*); +#ifndef SQLITE_OMIT_VIRTUALTABLE + extern int sqlite3_prefixes_init(sqlite3*,char**,const sqlite3_api_routines*); +#endif + extern int sqlite3_qpvtab_init(sqlite3*,char**,const sqlite3_api_routines*); + extern int sqlite3_randomjson_init(sqlite3*,char**,const sqlite3_api_routines*); extern int sqlite3_regexp_init(sqlite3*,char**,const sqlite3_api_routines*); + extern int sqlite3_remember_init(sqlite3*,char**,const sqlite3_api_routines*); extern int sqlite3_series_init(sqlite3*,char**,const sqlite3_api_routines*); extern int sqlite3_spellfix_init(sqlite3*,char**,const sqlite3_api_routines*); + extern int sqlite3_stmtrand_init(sqlite3*,char**,const sqlite3_api_routines*); extern int sqlite3_totype_init(sqlite3*,char**,const sqlite3_api_routines*); extern int sqlite3_wholenumber_init(sqlite3*,char**,const sqlite3_api_routines*); + extern int sqlite3_unionvtab_init(sqlite3*,char**,const sqlite3_api_routines*); +#ifdef SQLITE_HAVE_ZLIB + extern int sqlite3_zipfile_init(sqlite3*,char**,const sqlite3_api_routines*); +#endif static const struct { const char *zExtName; int (*pInit)(sqlite3*,char**,const sqlite3_api_routines*); } aExtension[] = { { "amatch", sqlite3_amatch_init }, + { "appendvfs", sqlite3_appendvfs_init }, + { "basexx", sqlite3_basexx_init }, { "closure", sqlite3_closure_init }, + { "csv", sqlite3_csv_init }, + { "decimal", sqlite3_decimal_init }, { "eval", sqlite3_eval_init }, + { "explain", sqlite3_explain_init }, { "fileio", sqlite3_fileio_init }, { "fuzzer", sqlite3_fuzzer_init }, { "ieee754", sqlite3_ieee_init }, { "nextchar", sqlite3_nextchar_init }, - { "percentile", sqlite3_percentile_init }, +#ifndef SQLITE_OMIT_VIRTUALTABLE + { "prefixes", sqlite3_prefixes_init }, +#endif + { "qpvtab", sqlite3_qpvtab_init }, + { "randomjson", sqlite3_randomjson_init }, { "regexp", sqlite3_regexp_init }, + { "remember", sqlite3_remember_init }, { "series", sqlite3_series_init }, { "spellfix", sqlite3_spellfix_init }, + { "stmtrand", sqlite3_stmtrand_init }, { "totype", sqlite3_totype_init }, + { "unionvtab", sqlite3_unionvtab_init }, { "wholenumber", sqlite3_wholenumber_init }, +#ifdef SQLITE_HAVE_ZLIB + { "zipfile", sqlite3_zipfile_init }, +#endif }; sqlite3 *db; const char *zName; @@ -6584,7 +8458,7 @@ static int tclLoadStaticExtensionCmd( }else{ rc = SQLITE_OK; } - if( rc!=SQLITE_OK || zErrMsg ){ + if( (rc!=SQLITE_OK && rc!=SQLITE_OK_LOAD_PERMANENTLY) || zErrMsg ){ Tcl_AppendResult(interp, "initialization of ", zName, " failed: ", zErrMsg, (char*)0); sqlite3_free(zErrMsg); @@ -6598,7 +8472,7 @@ static int tclLoadStaticExtensionCmd( ** sorter_test_fakeheap BOOL ** */ -static int sorter_test_fakeheap( +static int SQLITE_TCLAPI sorter_test_fakeheap( void * clientData, Tcl_Interp *interp, int objc, @@ -6638,7 +8512,7 @@ static int sorter_test_fakeheap( ** Then execute statement $SQL2. Check that the statement returns the same ** set of integers in the same order as in the previous step (using $SQL1). */ -static int sorter_test_sort4_helper( +static int SQLITE_TCLAPI sorter_test_sort4_helper( void * clientData, Tcl_Interp *interp, int objc, @@ -6648,8 +8522,8 @@ static int sorter_test_sort4_helper( const char *zSql2; int nStep; int iStep; - int iCksum1 = 0; - int iCksum2 = 0; + unsigned int iCksum1 = 0; + unsigned int iCksum2 = 0; int rc; int iB; sqlite3 *db; @@ -6672,11 +8546,11 @@ static int sorter_test_sort4_helper( for(iStep=0; iStep<nStep && SQLITE_ROW==sqlite3_step(pStmt); iStep++){ int a = sqlite3_column_int(pStmt, 0); if( a!=sqlite3_column_int(pStmt, iB) ){ - Tcl_AppendResult(interp, "data error: (a!=b)", 0); + Tcl_AppendResult(interp, "data error: (a!=b)", (void*)0); return TCL_ERROR; } - iCksum1 += (iCksum1 << 3) + a; + iCksum1 += (iCksum1 << 3) + (unsigned int)a; } rc = sqlite3_finalize(pStmt); if( rc!=SQLITE_OK ) goto sql_error; @@ -6685,241 +8559,482 @@ static int sorter_test_sort4_helper( if( rc!=SQLITE_OK ) goto sql_error; for(iStep=0; SQLITE_ROW==sqlite3_step(pStmt); iStep++){ int a = sqlite3_column_int(pStmt, 0); - iCksum2 += (iCksum2 << 3) + a; + iCksum2 += (iCksum2 << 3) + (unsigned int)a; } rc = sqlite3_finalize(pStmt); if( rc!=SQLITE_OK ) goto sql_error; if( iCksum1!=iCksum2 ){ - Tcl_AppendResult(interp, "checksum mismatch", 0); + Tcl_AppendResult(interp, "checksum mismatch", (void*)0); return TCL_ERROR; } return TCL_OK; sql_error: - Tcl_AppendResult(interp, "sql error: ", sqlite3_errmsg(db), 0); + Tcl_AppendResult(interp, "sql error: ", sqlite3_errmsg(db), (void*)0); return TCL_ERROR; } -#ifdef SQLITE_USER_AUTHENTICATION -#include "sqlite3userauth.h" + /* -** tclcmd: sqlite3_user_authenticate DB USERNAME PASSWORD +** tclcmd: register_dbstat_vtab DB +** +** Cause the dbstat virtual table to be available on the connection DB */ -static int test_user_authenticate( - ClientData clientData, /* Unused */ - Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ - int objc, /* Number of arguments */ - Tcl_Obj *CONST objv[] /* Command arguments */ +static int SQLITE_TCLAPI test_register_dbstat_vtab( + void *clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] ){ - char *zUser = 0; - char *zPasswd = 0; - int nPasswd = 0; - sqlite3 *db; - int rc; +#ifdef SQLITE_OMIT_VIRTUALTABLE + Tcl_AppendResult(interp, "dbstat not available because of " + "SQLITE_OMIT_VIRTUALTABLE", (void*)0); + return TCL_ERROR; +#else + struct SqliteDb { sqlite3 *db; }; + char *zDb; + Tcl_CmdInfo cmdInfo; - if( objc!=4 ){ - Tcl_WrongNumArgs(interp, 1, objv, "DB USERNAME PASSWORD"); + if( objc!=2 ){ + Tcl_WrongNumArgs(interp, 1, objv, "DB"); return TCL_ERROR; } - if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ - return TCL_ERROR; + + zDb = Tcl_GetString(objv[1]); + if( Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){ + sqlite3* db = ((struct SqliteDb*)cmdInfo.objClientData)->db; + sqlite3DbstatRegister(db); } - zUser = Tcl_GetString(objv[2]); - zPasswd = Tcl_GetStringFromObj(objv[3], &nPasswd); - rc = sqlite3_user_authenticate(db, zUser, zPasswd, nPasswd); - Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); return TCL_OK; +#endif /* SQLITE_OMIT_VIRTUALTABLE */ } -#endif /* SQLITE_USER_AUTHENTICATION */ -#ifdef SQLITE_USER_AUTHENTICATION /* -** tclcmd: sqlite3_user_add DB USERNAME PASSWORD ISADMIN +** tclcmd: sqlite3_db_config DB SETTING VALUE +** +** Invoke sqlite3_db_config() for one of the setting values. */ -static int test_user_add( - ClientData clientData, /* Unused */ - Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ - int objc, /* Number of arguments */ - Tcl_Obj *CONST objv[] /* Command arguments */ +static int SQLITE_TCLAPI test_sqlite3_db_config( + void *clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] ){ - char *zUser = 0; - char *zPasswd = 0; - int nPasswd = 0; - int isAdmin = 0; + static const struct { + const char *zName; + int eVal; + } aSetting[] = { + { "FKEY", SQLITE_DBCONFIG_ENABLE_FKEY }, + { "TRIGGER", SQLITE_DBCONFIG_ENABLE_TRIGGER }, + { "FTS3_TOKENIZER", SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER }, + { "LOAD_EXTENSION", SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION }, + { "NO_CKPT_ON_CLOSE", SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE }, + { "QPSG", SQLITE_DBCONFIG_ENABLE_QPSG }, + { "TRIGGER_EQP", SQLITE_DBCONFIG_TRIGGER_EQP }, + { "RESET_DB", SQLITE_DBCONFIG_RESET_DATABASE }, + { "DEFENSIVE", SQLITE_DBCONFIG_DEFENSIVE }, + { "WRITABLE_SCHEMA", SQLITE_DBCONFIG_WRITABLE_SCHEMA }, + { "LEGACY_ALTER_TABLE", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE }, + { "DQS_DML", SQLITE_DBCONFIG_DQS_DML }, + { "DQS_DDL", SQLITE_DBCONFIG_DQS_DDL }, + { "LEGACY_FILE_FORMAT", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT }, + { "TRUSTED_SCHEMA", SQLITE_DBCONFIG_TRUSTED_SCHEMA }, + { "STMT_SCANSTATUS", SQLITE_DBCONFIG_STMT_SCANSTATUS }, + { "REVERSE_SCANORDER", SQLITE_DBCONFIG_REVERSE_SCANORDER }, + { "ATTACH_CREATE", SQLITE_DBCONFIG_ENABLE_ATTACH_CREATE }, + { "ATTACH_WRITE", SQLITE_DBCONFIG_ENABLE_ATTACH_WRITE }, + { "COMMENTS", SQLITE_DBCONFIG_ENABLE_COMMENTS }, + }; + int i; + int v = 0; + const char *zSetting; sqlite3 *db; - int rc; - if( objc!=5 ){ - Tcl_WrongNumArgs(interp, 1, objv, "DB USERNAME PASSWORD ISADMIN"); + if( objc!=4 && objc!=3 ){ + Tcl_WrongNumArgs(interp, 1, objv, "DB SETTING [VALUE]"); return TCL_ERROR; } - if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ + if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; + zSetting = Tcl_GetString(objv[2]); + if( sqlite3_strglob("SQLITE_*", zSetting)==0 ) zSetting += 7; + if( sqlite3_strglob("DBCONFIG_*", zSetting)==0 ) zSetting += 9; + if( sqlite3_strglob("ENABLE_*", zSetting)==0 ) zSetting += 7; + for(i=0; i<ArraySize(aSetting); i++){ + if( strcmp(zSetting, aSetting[i].zName)==0 ) break; + } + if( i>=ArraySize(aSetting) ){ + Tcl_SetObjResult(interp, + Tcl_NewStringObj("unknown sqlite3_db_config setting", -1)); return TCL_ERROR; } - zUser = Tcl_GetString(objv[2]); - zPasswd = Tcl_GetStringFromObj(objv[3], &nPasswd); - Tcl_GetBooleanFromObj(interp, objv[4], &isAdmin); - rc = sqlite3_user_add(db, zUser, zPasswd, nPasswd, isAdmin); - Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); + if( objc==4 ){ + if( Tcl_GetIntFromObj(interp, objv[3], &v) ) return TCL_ERROR; + }else{ + v = -1; + } + sqlite3_db_config(db, aSetting[i].eVal, v, &v); + Tcl_SetObjResult(interp, Tcl_NewIntObj(v)); return TCL_OK; } -#endif /* SQLITE_USER_AUTHENTICATION */ - -#ifdef SQLITE_USER_AUTHENTICATION /* -** tclcmd: sqlite3_user_change DB USERNAME PASSWORD ISADMIN +** tclcmd: sqlite3_txn_state DB ?SCHEMA? +** +** Invoke sqlite3_txn_state(DB,SCHEMA) and return the +** numeric value that results. Use NULL for SCHEMA if the 3 argument +** is omitted. */ -static int test_user_change( - ClientData clientData, /* Unused */ - Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ - int objc, /* Number of arguments */ - Tcl_Obj *CONST objv[] /* Command arguments */ +static int SQLITE_TCLAPI test_sqlite3_txn_state( + void *clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] ){ - char *zUser = 0; - char *zPasswd = 0; - int nPasswd = 0; - int isAdmin = 0; sqlite3 *db; - int rc; + const char *zSchema; + int iTxn; - if( objc!=5 ){ - Tcl_WrongNumArgs(interp, 1, objv, "DB USERNAME PASSWORD ISADMIN"); - return TCL_ERROR; - } - if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ + if( objc!=2 && objc!=3 ){ + Tcl_WrongNumArgs(interp, 1, objv, "DB ?SCHEMA?"); return TCL_ERROR; } - zUser = Tcl_GetString(objv[2]); - zPasswd = Tcl_GetStringFromObj(objv[3], &nPasswd); - Tcl_GetBooleanFromObj(interp, objv[4], &isAdmin); - rc = sqlite3_user_change(db, zUser, zPasswd, nPasswd, isAdmin); - Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); + if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; + zSchema = objc==3 ? Tcl_GetString(objv[2]) : 0; + iTxn = sqlite3_txn_state(db, zSchema); + Tcl_SetObjResult(interp, Tcl_NewIntObj(iTxn)); return TCL_OK; } -#endif /* SQLITE_USER_AUTHENTICATION */ -#ifdef SQLITE_USER_AUTHENTICATION /* -** tclcmd: sqlite3_user_delete DB USERNAME +** Change the name of the main database schema from "main" to "icecube". */ -static int test_user_delete( - ClientData clientData, /* Unused */ - Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ - int objc, /* Number of arguments */ - Tcl_Obj *CONST objv[] /* Command arguments */ +static int SQLITE_TCLAPI test_dbconfig_maindbname_icecube( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] ){ - char *zUser = 0; - sqlite3 *db; int rc; + sqlite3 *db; + extern int getDbPointer(Tcl_Interp*, const char*, sqlite3**); + if( objc!=2 ){ + Tcl_WrongNumArgs(interp, 1, objv, "DB"); + return TCL_ERROR; + }else{ + if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; + rc = sqlite3_db_config(db, SQLITE_DBCONFIG_MAINDBNAME, "icecube"); + Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); + return TCL_OK; + } +} - if( objc!=3 ){ - Tcl_WrongNumArgs(interp, 1, objv, "DB USERNAME"); +/* +** Usage: sqlite3_mmap_warm DB DBNAME +*/ +static int SQLITE_TCLAPI test_mmap_warm( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + extern int getDbPointer(Tcl_Interp*, const char*, sqlite3**); + extern int sqlite3_mmap_warm(sqlite3 *db, const char *); + + if( objc!=2 && objc!=3 ){ + Tcl_WrongNumArgs(interp, 1, objv, "DB ?DBNAME?"); return TCL_ERROR; + }else{ + int rc; + sqlite3 *db; + const char *zDb = 0; + if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; + if( objc==3 ){ + zDb = Tcl_GetString(objv[2]); + } + rc = sqlite3_mmap_warm(db, zDb); + Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1)); + return TCL_OK; } - if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ){ +} + +/* +** Usage: test_write_db DB OFFSET DATA +** +** Obtain the sqlite3_file* object for the database file for the "main" db +** of handle DB. Then invoke its xWrite method to write data DATA to offset +** OFFSET. +*/ +static int SQLITE_TCLAPI test_write_db( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + sqlite3 *db = 0; + Tcl_WideInt iOff = 0; + const unsigned char *aData = 0; + Tcl_Size nData = 0; + sqlite3_file *pFile = 0; + int rc; + + if( objc!=4 ){ + Tcl_WrongNumArgs(interp, 1, objv, "DB OFFSET DATA"); return TCL_ERROR; } - zUser = Tcl_GetString(objv[2]); - rc = sqlite3_user_delete(db, zUser); - Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC); + if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; + if( Tcl_GetWideIntFromObj(interp, objv[2], &iOff) ) return TCL_ERROR; + aData = Tcl_GetByteArrayFromObj(objv[3], &nData); + + sqlite3_file_control(db, "main", SQLITE_FCNTL_FILE_POINTER, (void*)&pFile); + rc = pFile->pMethods->xWrite(pFile, aData, (int)(nData&0x7fffffff), iOff); + + Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE); return TCL_OK; } -#endif /* SQLITE_USER_AUTHENTICATION */ /* -** tclcmd: bad_behavior TYPE +** Usage: sqlite3_register_cksumvfs ** -** Do some things that should trigger a valgrind or -fsanitize=undefined -** warning. This is used to verify that errors and warnings output by those -** tools are detected by the test scripts. +*/ +static int SQLITE_TCLAPI test_register_cksumvfs( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + if( objc!=1 ){ + Tcl_WrongNumArgs(interp, 1, objv, ""); + return TCL_ERROR; + }else{ + extern int sqlite3_register_cksumvfs(const char*); + int rc = sqlite3_register_cksumvfs(0); + Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE); + } + return TCL_OK; +} + +/* +** Usage: sqlite3_unregister_cksumvfs ** -** TYPE BEHAVIOR -** 1 Overflow a signed integer -** 2 Jump based on an uninitialized variable -** 3 Read after free -** 4 Panic */ -static int test_bad_behavior( - ClientData clientData, /* Pointer to an integer containing zero */ - Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ - int objc, /* Number of arguments */ - Tcl_Obj *CONST objv[] /* Command arguments */ +static int SQLITE_TCLAPI test_unregister_cksumvfs( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] ){ - int iType; - int xyz; - int i = *(int*)clientData; - int j; - int w[10]; - int *a; - if( objc!=2 ){ - Tcl_WrongNumArgs(interp, 1, objv, "TYPE"); + if( objc!=1 ){ + Tcl_WrongNumArgs(interp, 1, objv, ""); return TCL_ERROR; + }else{ + extern int sqlite3_unregister_cksumvfs(void); + int rc = sqlite3_unregister_cksumvfs(); + Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE); } - if( Tcl_GetIntFromObj(interp, objv[1], &iType) ) return TCL_ERROR; - switch( iType ){ - case 1: { - xyz = 0x7fffff00 - i; - xyz += 0x100; - Tcl_SetObjResult(interp, Tcl_NewIntObj(xyz)); - break; - } - case 2: { - w[1] = 5; - if( w[i]>0 ) w[1]++; - Tcl_SetObjResult(interp, Tcl_NewIntObj(w[1])); - break; + return TCL_OK; +} + +/* +** Usage: decode_hexdb TEXT +** +** Example: db deserialize [decode_hexdb $output_of_dbtotxt] +** +** This routine returns a byte-array for an SQLite database file that +** is constructed from a text input which is the output of the "dbtotxt" +** utility. +*/ +static int SQLITE_TCLAPI test_decode_hexdb( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + const char *zIn = 0; + unsigned char *a = 0; + int n = 0; + int i, iNext; + int iOffset = 0; + int j, k; + int rc; + unsigned int x[16]; + if( objc!=2 ){ + Tcl_WrongNumArgs(interp, 1, objv, "HEXDB"); + return TCL_ERROR; + } + zIn = Tcl_GetString(objv[1]); + for(i=0; zIn[i]; i=iNext){ + for(iNext=i; zIn[iNext] && zIn[iNext]!='\n'; iNext++){} + if( zIn[iNext]=='\n' ) iNext++; + while( zIn[i]==' ' || zIn[i]=='\t' ){ i++; } + if( a==0 ){ + int pgsz; + rc = sscanf(zIn+i, "| size %d pagesize %d", &n, &pgsz); + if( rc!=2 ) continue; + if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ){ + Tcl_AppendResult(interp, "bad 'pagesize' field", (void*)0); + return TCL_ERROR; + } + n = (n+pgsz-1)&~(pgsz-1); /* Round n up to the next multiple of pgsz */ + if( n<512 ){ + Tcl_AppendResult(interp, "bad 'size' field", (void*)0); + return TCL_ERROR; + } + a = malloc( n ); + if( a==0 ){ + Tcl_AppendResult(interp, "out of memory", (void*)0); + return TCL_ERROR; + } + memset(a, 0, n); + continue; } - case 3: { - a = malloc( sizeof(int)*10 ); - for(j=0; j<10; j++) a[j] = j; - free(a); - Tcl_SetObjResult(interp, Tcl_NewIntObj(a[i])); - break; + rc = sscanf(zIn+i, "| page %d offset %d", &j, &k); + if( rc==2 ){ + iOffset = k; + continue; } - case 4: { - Tcl_Panic("Deliberate panic"); - break; + rc = sscanf(zIn+i,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x", + &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7], + &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]); + if( rc==17 ){ + k = iOffset+j; + if( k+16<=n ){ + int ii; + for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff; + } + continue; } } + Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(a, n)); + free(a); return TCL_OK; -} +} /* -** tclcmd: register_dbstat_vtab DB +** Client data for the autovacuum_pages callback. +*/ +struct AutovacPageData { + Tcl_Interp *interp; + char *zScript; +}; +typedef struct AutovacPageData AutovacPageData; + +/* +** Callback functions for sqlite3_autovacuum_pages +*/ +static unsigned int test_autovacuum_pages_callback( + void *pClientData, + const char *zSchema, + unsigned int nFilePages, + unsigned int nFreePages, + unsigned int nBytePerPage +){ + AutovacPageData *pData = (AutovacPageData*)pClientData; + Tcl_DString str; + unsigned int x; + char zBuf[100]; + Tcl_DStringInit(&str); + Tcl_DStringAppend(&str, pData->zScript, -1); + Tcl_DStringAppendElement(&str, zSchema); + sqlite3_snprintf(sizeof(zBuf), zBuf, "%u", nFilePages); + Tcl_DStringAppendElement(&str, zBuf); + sqlite3_snprintf(sizeof(zBuf), zBuf, "%u", nFreePages); + Tcl_DStringAppendElement(&str, zBuf); + sqlite3_snprintf(sizeof(zBuf), zBuf, "%u", nBytePerPage); + Tcl_DStringAppendElement(&str, zBuf); + Tcl_ResetResult(pData->interp); + Tcl_Eval(pData->interp, Tcl_DStringValue(&str)); + Tcl_DStringFree(&str); + x = nFreePages; + (void)Tcl_GetIntFromObj(0, Tcl_GetObjResult(pData->interp), (int*)&x); + return x; +} + +/* +** Usage: sqlite3_autovacuum_pages DB SCRIPT ** -** Cause the dbstat virtual table to be available on the connection DB +** Add an autovacuum-pages callback to database connection DB. The callback +** will invoke SCRIPT, after appending parameters. +** +** If SCRIPT is an empty string or is omitted, then the callback is +** cancelled. */ -static int test_register_dbstat_vtab( - void *clientData, +static int SQLITE_TCLAPI test_autovacuum_pages( + void * clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[] ){ -#ifdef SQLITE_OMIT_VIRTUALTABLE - Tcl_AppendResult(interp, "dbstat not available because of " - "SQLITE_OMIT_VIRTUALTABLE", (void*)0); - return TCL_ERROR; -#else - struct SqliteDb { sqlite3 *db; }; - char *zDb; - Tcl_CmdInfo cmdInfo; - - if( objc!=2 ){ - Tcl_WrongNumArgs(interp, 1, objv, "DB"); + AutovacPageData *pData; + sqlite3 *db; + int rc; + const char *zScript; + if( objc!=2 && objc!=3 ){ + Tcl_WrongNumArgs(interp, 1, objv, "DB ?SCRIPT?"); return TCL_ERROR; } - - zDb = Tcl_GetString(objv[1]); - if( Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){ - sqlite3* db = ((struct SqliteDb*)cmdInfo.objClientData)->db; - sqlite3DbstatRegister(db); + if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; + zScript = objc==3 ? Tcl_GetString(objv[2]) : 0; + if( zScript ){ + size_t nScript = strlen(zScript); + pData = sqlite3_malloc64( sizeof(*pData) + nScript + 1 ); + if( pData==0 ){ + Tcl_AppendResult(interp, "out of memory", (void*)0); + return TCL_ERROR; + } + pData->interp = interp; + pData->zScript = (char*)&pData[1]; + memcpy(pData->zScript, zScript, nScript+1); + rc = sqlite3_autovacuum_pages(db,test_autovacuum_pages_callback, + pData, sqlite3_free); + }else{ + rc = sqlite3_autovacuum_pages(db, 0, 0, 0); + } + if( rc ){ + char zBuf[1000]; + sqlite3_snprintf(sizeof(zBuf), zBuf, + "sqlite3_autovacuum_pages() returns %d", rc); + Tcl_AppendResult(interp, zBuf, (void*)0); + return TCL_ERROR; } return TCL_OK; -#endif /* SQLITE_OMIT_VIRTUALTABLE */ } +/* +** Usage: number_of_cores +** +** Return a guess at the number of available cores available on the +** processor on which this process is running. +*/ +static int SQLITE_TCLAPI guess_number_of_cores( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + unsigned int nCore = 1; +#ifdef _WIN32 + SYSTEM_INFO sysinfo; + GetSystemInfo(&sysinfo); + nCore = (unsigned int)sysinfo.dwNumberOfProcessors; +#elif defined(__APPLE__) + int nm[2]; + size_t len = 4; + nm[0] = CTL_HW; nm[1] = HW_AVAILCPU; + sysctl(nm, 2, &nCore, &len, NULL, 0); + if( nCore<1 ){ + nm[1] = HW_NCPU; + sysctl(nm, 2, &nCore, &len, NULL, 0); + } +#else + nCore = sysconf(_SC_NPROCESSORS_ONLN); +#endif + if( nCore<=0 ) nCore = 1; + Tcl_SetObjResult(interp, Tcl_NewIntObj((int)nCore)); + return SQLITE_OK; +} + + /* ** Register commands with the TCL interpreter. */ @@ -6934,9 +9049,8 @@ int Sqlitetest1_Init(Tcl_Interp *interp){ extern int sqlite3_hostid_num; #endif extern int sqlite3_max_blobsize; - extern int sqlite3BtreeSharedCacheReport(void*, + extern int SQLITE_TCLAPI sqlite3BtreeSharedCacheReport(void*, Tcl_Interp*,int,Tcl_Obj*CONST*); - static int iZero = 0; static struct { char *zName; Tcl_CmdProc *xProc; @@ -6967,19 +9081,20 @@ int Sqlitetest1_Init(Tcl_Interp *interp){ { "sqlite3_close_v2", (Tcl_CmdProc*)sqlite_test_close_v2 }, { "sqlite3_create_function", (Tcl_CmdProc*)test_create_function }, { "sqlite3_create_aggregate", (Tcl_CmdProc*)test_create_aggregate }, + { "sqlite3_drop_modules", (Tcl_CmdProc*)test_drop_modules }, { "sqlite_register_test_function", (Tcl_CmdProc*)test_register_func }, { "sqlite_abort", (Tcl_CmdProc*)sqlite_abort }, { "sqlite_bind", (Tcl_CmdProc*)test_bind }, { "breakpoint", (Tcl_CmdProc*)test_breakpoint }, { "sqlite3_key", (Tcl_CmdProc*)test_key }, { "sqlite3_rekey", (Tcl_CmdProc*)test_rekey }, - { "sqlite_set_magic", (Tcl_CmdProc*)sqlite_set_magic }, { "sqlite3_interrupt", (Tcl_CmdProc*)test_interrupt }, + { "sqlite3_is_interrupted", (Tcl_CmdProc*)test_is_interrupted }, { "sqlite_delete_function", (Tcl_CmdProc*)delete_function }, { "sqlite_delete_collation", (Tcl_CmdProc*)delete_collation }, { "sqlite3_get_autocommit", (Tcl_CmdProc*)get_autocommit }, - { "sqlite3_stack_used", (Tcl_CmdProc*)test_stack_used }, { "sqlite3_busy_timeout", (Tcl_CmdProc*)test_busy_timeout }, + { "sqlite3_setlk_timeout", (Tcl_CmdProc*)test_setlk_timeout }, { "printf", (Tcl_CmdProc*)test_printf }, { "sqlite3IoTrace", (Tcl_CmdProc*)test_io_trace }, { "clang_sanitize_address", (Tcl_CmdProc*)clang_sanitize_address }, @@ -6989,9 +9104,14 @@ int Sqlitetest1_Init(Tcl_Interp *interp){ Tcl_ObjCmdProc *xProc; void *clientData; } aObjCmd[] = { - { "bad_behavior", test_bad_behavior, (void*)&iZero }, + { "sqlite3_db_config", test_sqlite3_db_config, 0 }, + { "sqlite3_txn_state", test_sqlite3_txn_state, 0 }, { "register_dbstat_vtab", test_register_dbstat_vtab }, { "sqlite3_connection_pointer", get_sqlite_pointer, 0 }, + { "intarray_addr", test_intarray_addr, 0 }, + { "int64array_addr", test_int64array_addr, 0 }, + { "doublearray_addr", test_doublearray_addr, 0 }, + { "textarray_addr", test_textarray_addr, 0 }, { "sqlite3_bind_int", test_bind_int, 0 }, { "sqlite3_bind_zeroblob", test_bind_zeroblob, 0 }, { "sqlite3_bind_zeroblob64", test_bind_zeroblob64, 0 }, @@ -7001,6 +9121,12 @@ int Sqlitetest1_Init(Tcl_Interp *interp){ { "sqlite3_bind_text", test_bind_text ,0 }, { "sqlite3_bind_text16", test_bind_text16 ,0 }, { "sqlite3_bind_blob", test_bind_blob ,0 }, + { "sqlite3_bind_value_from_select",test_bind_value_from_select ,0 }, + { "sqlite3_bind_value_from_preupdate",test_bind_value_from_preupdate ,0 }, +#ifndef SQLITE_OMIT_VIRTUALTABLE + { "sqlite3_carray_bind", test_carray_bind ,0 }, + { "bind_carray_intptr", bind_carray_intptr ,0 }, +#endif { "sqlite3_bind_parameter_count", test_bind_parameter_count, 0}, { "sqlite3_bind_parameter_name", test_bind_parameter_name, 0}, { "sqlite3_bind_parameter_index", test_bind_parameter_index, 0}, @@ -7009,15 +9135,19 @@ int Sqlitetest1_Init(Tcl_Interp *interp){ { "sqlite3_errcode", test_errcode ,0 }, { "sqlite3_extended_errcode", test_ex_errcode ,0 }, { "sqlite3_errmsg", test_errmsg ,0 }, + { "sqlite3_error_offset", test_error_offset ,0 }, { "sqlite3_errmsg16", test_errmsg16 ,0 }, + { "sqlite3_set_errmsg", test_set_errmsg ,0 }, { "sqlite3_open", test_open ,0 }, { "sqlite3_open16", test_open16 ,0 }, { "sqlite3_open_v2", test_open_v2 ,0 }, { "sqlite3_complete16", test_complete16 ,0 }, + { "sqlite3_normalize", test_normalize ,0 }, { "sqlite3_prepare", test_prepare ,0 }, { "sqlite3_prepare16", test_prepare16 ,0 }, { "sqlite3_prepare_v2", test_prepare_v2 ,0 }, + { "sqlite3_prepare_v3", test_prepare_v3 ,0 }, { "sqlite3_prepare_tkt3134", test_prepare_tkt3134, 0}, { "sqlite3_prepare16_v2", test_prepare16_v2 ,0 }, { "sqlite3_finalize", test_finalize ,0 }, @@ -7028,17 +9158,26 @@ int Sqlitetest1_Init(Tcl_Interp *interp){ { "sqlite3_changes", test_changes ,0 }, { "sqlite3_step", test_step ,0 }, { "sqlite3_sql", test_sql ,0 }, + { "sqlite3_expanded_sql", test_ex_sql ,0 }, +#ifdef SQLITE_ENABLE_NORMALIZE + { "sqlite3_normalized_sql", test_norm_sql ,0 }, +#endif { "sqlite3_next_stmt", test_next_stmt ,0 }, { "sqlite3_stmt_readonly", test_stmt_readonly ,0 }, + { "sqlite3_stmt_isexplain", test_stmt_isexplain,0 }, + { "sqlite3_stmt_explain", test_stmt_explain ,0 }, { "sqlite3_stmt_busy", test_stmt_busy ,0 }, { "uses_stmt_journal", uses_stmt_journal ,0 }, { "sqlite3_release_memory", test_release_memory, 0}, { "sqlite3_db_release_memory", test_db_release_memory, 0}, { "sqlite3_db_cacheflush", test_db_cacheflush, 0}, + { "sqlite3_system_errno", test_system_errno, 0}, { "sqlite3_db_filename", test_db_filename, 0}, { "sqlite3_db_readonly", test_db_readonly, 0}, { "sqlite3_soft_heap_limit", test_soft_heap_limit, 0}, + { "sqlite3_soft_heap_limit64", test_soft_heap_limit, 0}, + { "sqlite3_hard_heap_limit64", test_hard_heap_limit, 0}, { "sqlite3_thread_cleanup", test_thread_cleanup, 0}, { "sqlite3_pager_refcounts", test_pager_refcounts, 0}, @@ -7046,20 +9185,18 @@ int Sqlitetest1_Init(Tcl_Interp *interp){ { "sqlite3_enable_load_extension", test_enable_load, 0}, { "sqlite3_extended_result_codes", test_extended_result_codes, 0}, { "sqlite3_limit", test_limit, 0}, + { "dbconfig_maindbname_icecube", test_dbconfig_maindbname_icecube }, { "save_prng_state", save_prng_state, 0 }, { "restore_prng_state", restore_prng_state, 0 }, { "reset_prng_state", reset_prng_state, 0 }, + { "prng_seed", prng_seed, 0 }, + { "extra_schema_checks", extra_schema_checks, 0}, { "database_never_corrupt", database_never_corrupt, 0}, { "database_may_be_corrupt", database_may_be_corrupt, 0}, { "optimization_control", optimization_control,0}, -#if SQLITE_OS_WIN +#ifdef _WIN32 { "lock_win32_file", win32_file_lock, 0 }, - { "exists_win32_path", win32_exists_path, 0 }, - { "find_win32_file", win32_find_file, 0 }, - { "delete_win32_file", win32_delete_file, 0 }, - { "make_win32_dir", win32_mkdir, 0 }, - { "remove_win32_dir", win32_rmdir, 0 }, #endif { "tcl_objproc", runAsObjProc, 0 }, @@ -7110,14 +9247,18 @@ int Sqlitetest1_Init(Tcl_Interp *interp){ { "file_control_lockproxy_test", file_control_lockproxy_test, 0 }, { "file_control_chunksize_test", file_control_chunksize_test, 0 }, { "file_control_sizehint_test", file_control_sizehint_test, 0 }, + { "file_control_data_version", file_control_data_version, 0 }, #if SQLITE_OS_WIN { "file_control_win32_av_retry", file_control_win32_av_retry, 0 }, + { "file_control_win32_get_handle", file_control_win32_get_handle, 0 }, { "file_control_win32_set_handle", file_control_win32_set_handle, 0 }, #endif { "file_control_persist_wal", file_control_persist_wal, 0 }, { "file_control_powersafe_overwrite",file_control_powersafe_overwrite,0}, { "file_control_vfsname", file_control_vfsname, 0 }, + { "file_control_reservebytes", file_control_reservebytes, 0 }, { "file_control_tempfilename", file_control_tempfilename, 0 }, + { "file_control_external_reader", file_control_external_reader, 0 }, { "sqlite3_vfs_list", vfs_list, 0 }, { "sqlite3_create_function_v2", test_create_function_v2, 0 }, @@ -7130,6 +9271,7 @@ int Sqlitetest1_Init(Tcl_Interp *interp){ #endif { "sqlite3_test_errstr", test_errstr, 0 }, { "tcl_variable_type", tcl_variable_type, 0 }, + { "fpnum_compare", fpnum_compare, 0 }, #ifndef SQLITE_OMIT_SHARED_CACHE { "sqlite3_enable_shared_cache", test_enable_shared, 0 }, { "sqlite3_shared_cache_report", sqlite3BtreeSharedCacheReport, 0}, @@ -7150,19 +9292,15 @@ int Sqlitetest1_Init(Tcl_Interp *interp){ #ifndef SQLITE_OMIT_EXPLAIN { "print_explain_query_plan", test_print_eqp, 0 }, #endif + { "strftime", strftime_cmd }, { "sqlite3_test_control", test_test_control }, + { ".treetrace", test_treetrace }, #if SQLITE_OS_UNIX { "getrusage", test_getrusage }, #endif { "load_static_extension", tclLoadStaticExtensionCmd }, { "sorter_test_fakeheap", sorter_test_fakeheap }, { "sorter_test_sort4_helper", sorter_test_sort4_helper }, -#ifdef SQLITE_USER_AUTHENTICATION - { "sqlite3_user_authenticate", test_user_authenticate, 0 }, - { "sqlite3_user_add", test_user_add, 0 }, - { "sqlite3_user_change", test_user_change, 0 }, - { "sqlite3_user_delete", test_user_delete, 0 }, -#endif #ifdef SQLITE_ENABLE_STMT_SCANSTATUS { "sqlite3_stmt_scanstatus", test_stmt_scanstatus, 0 }, { "sqlite3_stmt_scanstatus_reset", test_stmt_scanstatus_reset, 0 }, @@ -7175,10 +9313,27 @@ int Sqlitetest1_Init(Tcl_Interp *interp){ { "sqlite3_snapshot_get", test_snapshot_get, 0 }, { "sqlite3_snapshot_open", test_snapshot_open, 0 }, { "sqlite3_snapshot_free", test_snapshot_free, 0 }, + { "sqlite3_snapshot_cmp", test_snapshot_cmp, 0 }, + { "sqlite3_snapshot_recover", test_snapshot_recover, 0 }, + { "sqlite3_snapshot_get_blob", test_snapshot_get_blob, 0 }, + { "sqlite3_snapshot_open_blob", test_snapshot_open_blob, 0 }, + { "sqlite3_snapshot_cmp_blob", test_snapshot_cmp_blob, 0 }, +#endif + { "sqlite3_delete_database", test_delete_database, 0 }, + { "atomic_batch_write", test_atomic_batch_write, 0 }, + { "sqlite3_mmap_warm", test_mmap_warm, 0 }, + { "sqlite3_config_sorterref", test_config_sorterref, 0 }, + { "sqlite3_autovacuum_pages", test_autovacuum_pages, 0 }, + { "decode_hexdb", test_decode_hexdb, 0 }, + { "test_write_db", test_write_db, 0 }, + { "sqlite3_register_cksumvfs", test_register_cksumvfs, 0 }, + { "sqlite3_unregister_cksumvfs", test_unregister_cksumvfs, 0 }, + { "number_of_cores", guess_number_of_cores, 0 }, +#ifndef SQLITE_OMIT_VIRTUALTABLE + { "create_null_module", test_create_null_module, 0 }, #endif }; static int bitmask_size = sizeof(Bitmask)*8; - static int longdouble_size = sizeof(LONGDOUBLE_TYPE); int i; extern int sqlite3_sync_count, sqlite3_fullsync_count; extern int sqlite3_opentemp_count; @@ -7188,10 +9343,11 @@ int Sqlitetest1_Init(Tcl_Interp *interp){ extern int sqlite3_pager_writedb_count; extern int sqlite3_pager_writej_count; #if SQLITE_OS_WIN + extern int sqlite3_win_test_unc_locking; extern LONG volatile sqlite3_os_type; #endif #ifdef SQLITE_DEBUG - extern int sqlite3WhereTrace; + extern u32 sqlite3WhereTrace; extern int sqlite3OSTrace; extern int sqlite3WalTrace; #endif @@ -7247,6 +9403,8 @@ int Sqlitetest1_Init(Tcl_Interp *interp){ #if SQLITE_OS_WIN Tcl_LinkVar(interp, "sqlite_os_type", (char*)&sqlite3_os_type, TCL_LINK_LONG); + Tcl_LinkVar(interp, "sqlite3_win_test_unc_locking", + (char*)&sqlite3_win_test_unc_locking, TCL_LINK_INT); #endif #ifdef SQLITE_TEST { @@ -7279,12 +9437,14 @@ int Sqlitetest1_Init(Tcl_Interp *interp){ (char*)&sqlite3_data_directory, TCL_LINK_STRING); Tcl_LinkVar(interp, "bitmask_size", (char*)&bitmask_size, TCL_LINK_INT|TCL_LINK_READ_ONLY); - Tcl_LinkVar(interp, "longdouble_size", - (char*)&longdouble_size, TCL_LINK_INT|TCL_LINK_READ_ONLY); Tcl_LinkVar(interp, "sqlite_sync_count", (char*)&sqlite3_sync_count, TCL_LINK_INT); Tcl_LinkVar(interp, "sqlite_fullsync_count", (char*)&sqlite3_fullsync_count, TCL_LINK_INT); +#if defined(SQLITE_ENABLE_TREETRACE) + Tcl_LinkVar(interp, "sqlite3_unsupported_treetrace", + (char*)&sqlite3TreeTrace, TCL_LINK_INT); +#endif #if defined(SQLITE_ENABLE_FTS3) && defined(SQLITE_TEST) Tcl_LinkVar(interp, "sqlite_fts3_enable_parentheses", (char*)&sqlite3_fts3_enable_parentheses, TCL_LINK_INT); diff --git a/src/test2.c b/src/test2.c index d16ba5016a..899728ead9 100644 --- a/src/test2.c +++ b/src/test2.c @@ -14,7 +14,7 @@ ** testing of the SQLite library. */ #include "sqliteInt.h" -#include "tcl.h" +#include "tclsqlite.h" #include <stdlib.h> #include <string.h> #include <ctype.h> @@ -38,7 +38,7 @@ static void pager_test_reiniter(DbPage *pNotUsed){ ** ** Open a new pager */ -static int pager_open( +static int SQLITE_TCLAPI pager_open( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -51,7 +51,7 @@ static int pager_open( char zBuf[100]; if( argc!=3 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " FILENAME N-PAGE\"", 0); + " FILENAME N-PAGE\"", NULL); return TCL_ERROR; } if( Tcl_GetInt(interp, argv[2], &nPage) ) return TCL_ERROR; @@ -59,14 +59,14 @@ static int pager_open( SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_MAIN_DB, pager_test_reiniter); if( rc!=SQLITE_OK ){ - Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); + Tcl_AppendResult(interp, sqlite3ErrName(rc), NULL); return TCL_ERROR; } sqlite3PagerSetCachesize(pPager, nPage); pageSize = test_pagesize; sqlite3PagerSetPagesize(pPager, &pageSize, -1); sqlite3_snprintf(sizeof(zBuf),zBuf,"%p",pPager); - Tcl_AppendResult(interp, zBuf, 0); + Tcl_AppendResult(interp, zBuf, NULL); return TCL_OK; } @@ -75,7 +75,7 @@ static int pager_open( ** ** Close the given pager. */ -static int pager_close( +static int SQLITE_TCLAPI pager_close( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -85,13 +85,13 @@ static int pager_close( int rc; if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " ID\"", 0); + " ID\"", NULL); return TCL_ERROR; } pPager = sqlite3TestTextToPtr(argv[1]); - rc = sqlite3PagerClose(pPager); + rc = sqlite3PagerClose(pPager, 0); if( rc!=SQLITE_OK ){ - Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); + Tcl_AppendResult(interp, sqlite3ErrName(rc), NULL); return TCL_ERROR; } return TCL_OK; @@ -102,7 +102,7 @@ static int pager_close( ** ** Rollback changes */ -static int pager_rollback( +static int SQLITE_TCLAPI pager_rollback( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -112,13 +112,13 @@ static int pager_rollback( int rc; if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " ID\"", 0); + " ID\"", NULL); return TCL_ERROR; } pPager = sqlite3TestTextToPtr(argv[1]); rc = sqlite3PagerRollback(pPager); if( rc!=SQLITE_OK ){ - Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); + Tcl_AppendResult(interp, sqlite3ErrName(rc), NULL); return TCL_ERROR; } return TCL_OK; @@ -129,7 +129,7 @@ static int pager_rollback( ** ** Commit all changes */ -static int pager_commit( +static int SQLITE_TCLAPI pager_commit( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -139,18 +139,18 @@ static int pager_commit( int rc; if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " ID\"", 0); + " ID\"", NULL); return TCL_ERROR; } pPager = sqlite3TestTextToPtr(argv[1]); rc = sqlite3PagerCommitPhaseOne(pPager, 0, 0); if( rc!=SQLITE_OK ){ - Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); + Tcl_AppendResult(interp, sqlite3ErrName(rc), NULL); return TCL_ERROR; } rc = sqlite3PagerCommitPhaseTwo(pPager); if( rc!=SQLITE_OK ){ - Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); + Tcl_AppendResult(interp, sqlite3ErrName(rc), NULL); return TCL_ERROR; } return TCL_OK; @@ -161,7 +161,7 @@ static int pager_commit( ** ** Start a new checkpoint. */ -static int pager_stmt_begin( +static int SQLITE_TCLAPI pager_stmt_begin( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -171,13 +171,13 @@ static int pager_stmt_begin( int rc; if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " ID\"", 0); + " ID\"", NULL); return TCL_ERROR; } pPager = sqlite3TestTextToPtr(argv[1]); rc = sqlite3PagerOpenSavepoint(pPager, 1); if( rc!=SQLITE_OK ){ - Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); + Tcl_AppendResult(interp, sqlite3ErrName(rc), NULL); return TCL_ERROR; } return TCL_OK; @@ -188,7 +188,7 @@ static int pager_stmt_begin( ** ** Rollback changes to a checkpoint */ -static int pager_stmt_rollback( +static int SQLITE_TCLAPI pager_stmt_rollback( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -198,14 +198,14 @@ static int pager_stmt_rollback( int rc; if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " ID\"", 0); + " ID\"", NULL); return TCL_ERROR; } pPager = sqlite3TestTextToPtr(argv[1]); rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK, 0); sqlite3PagerSavepoint(pPager, SAVEPOINT_RELEASE, 0); if( rc!=SQLITE_OK ){ - Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); + Tcl_AppendResult(interp, sqlite3ErrName(rc), NULL); return TCL_ERROR; } return TCL_OK; @@ -216,7 +216,7 @@ static int pager_stmt_rollback( ** ** Commit changes to a checkpoint */ -static int pager_stmt_commit( +static int SQLITE_TCLAPI pager_stmt_commit( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -226,13 +226,13 @@ static int pager_stmt_commit( int rc; if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " ID\"", 0); + " ID\"", NULL); return TCL_ERROR; } pPager = sqlite3TestTextToPtr(argv[1]); rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_RELEASE, 0); if( rc!=SQLITE_OK ){ - Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); + Tcl_AppendResult(interp, sqlite3ErrName(rc), NULL); return TCL_ERROR; } return TCL_OK; @@ -243,7 +243,7 @@ static int pager_stmt_commit( ** ** Return pager statistics. */ -static int pager_stats( +static int SQLITE_TCLAPI pager_stats( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -253,7 +253,7 @@ static int pager_stats( int i, *a; if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " ID\"", 0); + " ID\"", NULL); return TCL_ERROR; } pPager = sqlite3TestTextToPtr(argv[1]); @@ -276,7 +276,7 @@ static int pager_stats( ** ** Return the size of the database file. */ -static int pager_pagecount( +static int SQLITE_TCLAPI pager_pagecount( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -287,13 +287,13 @@ static int pager_pagecount( int nPage; if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " ID\"", 0); + " ID\"", NULL); return TCL_ERROR; } pPager = sqlite3TestTextToPtr(argv[1]); sqlite3PagerPagecount(pPager, &nPage); sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", nPage); - Tcl_AppendResult(interp, zBuf, 0); + Tcl_AppendResult(interp, zBuf, NULL); return TCL_OK; } @@ -302,7 +302,7 @@ static int pager_pagecount( ** ** Return a pointer to a page from the database. */ -static int page_get( +static int SQLITE_TCLAPI page_get( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -315,7 +315,7 @@ static int page_get( int rc; if( argc!=3 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " ID PGNO\"", 0); + " ID PGNO\"", NULL); return TCL_ERROR; } pPager = sqlite3TestTextToPtr(argv[1]); @@ -325,11 +325,11 @@ static int page_get( rc = sqlite3PagerGet(pPager, pgno, &pPage, 0); } if( rc!=SQLITE_OK ){ - Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); + Tcl_AppendResult(interp, sqlite3ErrName(rc), NULL); return TCL_ERROR; } sqlite3_snprintf(sizeof(zBuf),zBuf,"%p",pPage); - Tcl_AppendResult(interp, zBuf, 0); + Tcl_AppendResult(interp, zBuf, NULL); return TCL_OK; } @@ -339,7 +339,7 @@ static int page_get( ** Return a pointer to a page if the page is already in cache. ** If not in cache, return an empty string. */ -static int page_lookup( +static int SQLITE_TCLAPI page_lookup( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -351,7 +351,7 @@ static int page_lookup( int pgno; if( argc!=3 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " ID PGNO\"", 0); + " ID PGNO\"", NULL); return TCL_ERROR; } pPager = sqlite3TestTextToPtr(argv[1]); @@ -359,7 +359,7 @@ static int page_lookup( pPage = sqlite3PagerLookup(pPager, pgno); if( pPage ){ sqlite3_snprintf(sizeof(zBuf),zBuf,"%p",pPage); - Tcl_AppendResult(interp, zBuf, 0); + Tcl_AppendResult(interp, zBuf, NULL); } return TCL_OK; } @@ -367,7 +367,7 @@ static int page_lookup( /* ** Usage: pager_truncate ID PGNO */ -static int pager_truncate( +static int SQLITE_TCLAPI pager_truncate( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -377,7 +377,7 @@ static int pager_truncate( int pgno; if( argc!=3 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " ID PGNO\"", 0); + " ID PGNO\"", NULL); return TCL_ERROR; } pPager = sqlite3TestTextToPtr(argv[1]); @@ -392,7 +392,7 @@ static int pager_truncate( ** ** Drop a pointer to a page. */ -static int page_unref( +static int SQLITE_TCLAPI page_unref( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -401,7 +401,7 @@ static int page_unref( DbPage *pPage; if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " PAGE\"", 0); + " PAGE\"", NULL); return TCL_ERROR; } pPage = (DbPage *)sqlite3TestTextToPtr(argv[1]); @@ -414,7 +414,7 @@ static int page_unref( ** ** Return the content of a page */ -static int page_read( +static int SQLITE_TCLAPI page_read( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -424,12 +424,12 @@ static int page_read( DbPage *pPage; if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " PAGE\"", 0); + " PAGE\"", NULL); return TCL_ERROR; } pPage = sqlite3TestTextToPtr(argv[1]); memcpy(zBuf, sqlite3PagerGetData(pPage), sizeof(zBuf)); - Tcl_AppendResult(interp, zBuf, 0); + Tcl_AppendResult(interp, zBuf, NULL); return TCL_OK; } @@ -438,7 +438,7 @@ static int page_read( ** ** Return the page number for a page. */ -static int page_number( +static int SQLITE_TCLAPI page_number( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -448,12 +448,12 @@ static int page_number( DbPage *pPage; if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " PAGE\"", 0); + " PAGE\"", NULL); return TCL_ERROR; } pPage = (DbPage *)sqlite3TestTextToPtr(argv[1]); sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", sqlite3PagerPagenumber(pPage)); - Tcl_AppendResult(interp, zBuf, 0); + Tcl_AppendResult(interp, zBuf, NULL); return TCL_OK; } @@ -462,7 +462,7 @@ static int page_number( ** ** Write something into a page. */ -static int page_write( +static int SQLITE_TCLAPI page_write( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -473,13 +473,13 @@ static int page_write( int rc; if( argc!=3 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " PAGE DATA\"", 0); + " PAGE DATA\"", NULL); return TCL_ERROR; } pPage = (DbPage *)sqlite3TestTextToPtr(argv[1]); rc = sqlite3PagerWrite(pPage); if( rc!=SQLITE_OK ){ - Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); + Tcl_AppendResult(interp, sqlite3ErrName(rc), NULL); return TCL_ERROR; } pData = sqlite3PagerGetData(pPage); @@ -498,7 +498,7 @@ static int page_write( ** new pages after N. If N is 2096 or bigger, this will test the ** ability of SQLite to write to large files. */ -static int fake_big_file( +static int SQLITE_TCLAPI fake_big_file( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -513,10 +513,18 @@ static int fake_big_file( int nFile; if( argc!=3 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " N-MEGABYTES FILE\"", 0); + " N-MEGABYTES FILE\"", NULL); return TCL_ERROR; } if( Tcl_GetInt(interp, argv[1], &n) ) return TCL_ERROR; +#if defined(_WIN32) + if( n>2 ){ + Tcl_AppendResult(interp, "cannot create ", argv[1], + "MB file because Windows " + "does not support sparse files", (void*)0); + return TCL_ERROR; + } +#endif pVfs = sqlite3_vfs_find(0); nFile = (int)strlen(argv[2]); @@ -528,7 +536,7 @@ static int fake_big_file( (SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_DB), 0 ); if( rc ){ - Tcl_AppendResult(interp, "open failed: ", sqlite3ErrName(rc), 0); + Tcl_AppendResult(interp, "open failed: ", sqlite3ErrName(rc), NULL); sqlite3_free(zFile); return TCL_ERROR; } @@ -538,7 +546,7 @@ static int fake_big_file( sqlite3OsCloseFree(fd); sqlite3_free(zFile); if( rc ){ - Tcl_AppendResult(interp, "write failed: ", sqlite3ErrName(rc), 0); + Tcl_AppendResult(interp, "write failed: ", sqlite3ErrName(rc), NULL); return TCL_ERROR; } return TCL_OK; @@ -551,7 +559,7 @@ static int fake_big_file( ** ** Set the PENDING_BYTE using the sqlite3_test_control() interface. */ -static int testPendingByte( +static int SQLITE_TCLAPI testPendingByte( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -596,7 +604,7 @@ static int faultSimCallback(int x){ zInt[i] = (x%10) + '0'; } if( isNeg ) zInt[i--] = '-'; - memcpy(faultSimScript+faultSimScriptSize, zInt+i+1, sizeof(zInt)-i); + memcpy(faultSimScript+faultSimScriptSize, zInt+i+1, sizeof(zInt)-i-1); } rc = Tcl_Eval(faultSimInterp, faultSimScript); if( rc ){ @@ -616,7 +624,7 @@ static int faultSimCallback(int x){ ** appended, whenever sqlite3FaultSim() is called. Or, if SCRIPT is the ** empty string, cancel the sqlite3FaultSim() callback. */ -static int faultInstallCmd( +static int SQLITE_TCLAPI faultInstallCmd( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -659,7 +667,7 @@ static int faultInstallCmd( ** Invoke the SQLITE_TESTCTRL_BITVEC_TEST operator on test_control. ** See comments on sqlite3BitvecBuiltinTest() for additional information. */ -static int testBitvecBuiltinTest( +static int SQLITE_TCLAPI testBitvecBuiltinTest( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ diff --git a/src/test3.c b/src/test3.c index 2a41068e5f..8fbb96a80d 100644 --- a/src/test3.c +++ b/src/test3.c @@ -15,7 +15,7 @@ */ #include "sqliteInt.h" #include "btreeInt.h" -#include "tcl.h" +#include "tclsqlite.h" #include <stdlib.h> #include <string.h> @@ -33,7 +33,7 @@ static int nRefSqlite3 = 0; ** ** Open a new database */ -static int btree_open( +static int SQLITE_TCLAPI btree_open( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -46,9 +46,10 @@ static int btree_open( char *zFilename; if( argc!=3 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " FILENAME NCACHE FLAGS\"", 0); + " FILENAME NCACHE FLAGS\"", NULL); return TCL_ERROR; } + if( Tcl_GetInt(interp, argv[2], &nCache) ) return TCL_ERROR; nRefSqlite3++; if( nRefSqlite3==1 ){ @@ -65,12 +66,12 @@ static int btree_open( SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_MAIN_DB); sqlite3_free(zFilename); if( rc!=SQLITE_OK ){ - Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); + Tcl_AppendResult(interp, sqlite3ErrName(rc), NULL); return TCL_ERROR; } sqlite3BtreeSetCacheSize(pBt, nCache); sqlite3_snprintf(sizeof(zBuf), zBuf,"%p", pBt); - Tcl_AppendResult(interp, zBuf, 0); + Tcl_AppendResult(interp, zBuf, NULL); return TCL_OK; } @@ -79,7 +80,7 @@ static int btree_open( ** ** Close the given database. */ -static int btree_close( +static int SQLITE_TCLAPI btree_close( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -89,13 +90,13 @@ static int btree_close( int rc; if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " ID\"", 0); + " ID\"", NULL); return TCL_ERROR; } pBt = sqlite3TestTextToPtr(argv[1]); rc = sqlite3BtreeClose(pBt); if( rc!=SQLITE_OK ){ - Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); + Tcl_AppendResult(interp, sqlite3ErrName(rc), NULL); return TCL_ERROR; } nRefSqlite3--; @@ -114,7 +115,7 @@ static int btree_close( ** ** Start a new transaction */ -static int btree_begin_transaction( +static int SQLITE_TCLAPI btree_begin_transaction( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -124,15 +125,15 @@ static int btree_begin_transaction( int rc; if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " ID\"", 0); + " ID\"", NULL); return TCL_ERROR; } pBt = sqlite3TestTextToPtr(argv[1]); sqlite3BtreeEnter(pBt); - rc = sqlite3BtreeBeginTrans(pBt, 1); + rc = sqlite3BtreeBeginTrans(pBt, 1, 0); sqlite3BtreeLeave(pBt); if( rc!=SQLITE_OK ){ - Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); + Tcl_AppendResult(interp, sqlite3ErrName(rc), NULL); return TCL_ERROR; } return TCL_OK; @@ -143,7 +144,7 @@ static int btree_begin_transaction( ** ** Returns pager statistics */ -static int btree_pager_stats( +static int SQLITE_TCLAPI btree_pager_stats( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -155,7 +156,7 @@ static int btree_pager_stats( if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " ID\"", 0); + " ID\"", NULL); return TCL_ERROR; } pBt = sqlite3TestTextToPtr(argv[1]); @@ -193,7 +194,7 @@ static int btree_pager_stats( ** ** Create a new cursor. Return the ID for the cursor. */ -static int btree_cursor( +static int SQLITE_TCLAPI btree_cursor( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -208,7 +209,7 @@ static int btree_cursor( if( argc!=4 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " ID TABLENUM WRITEABLE\"", 0); + " ID TABLENUM WRITEABLE\"", NULL); return TCL_ERROR; } pBt = sqlite3TestTextToPtr(argv[1]); @@ -229,11 +230,11 @@ static int btree_cursor( sqlite3_mutex_leave(pBt->db->mutex); if( rc ){ ckfree((char *)pCur); - Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); + Tcl_AppendResult(interp, sqlite3ErrName(rc), NULL); return TCL_ERROR; } sqlite3_snprintf(sizeof(zBuf), zBuf,"%p", pCur); - Tcl_AppendResult(interp, zBuf, 0); + Tcl_AppendResult(interp, zBuf, NULL); return SQLITE_OK; } @@ -242,31 +243,36 @@ static int btree_cursor( ** ** Close a cursor opened using btree_cursor. */ -static int btree_close_cursor( +static int SQLITE_TCLAPI btree_close_cursor( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ const char **argv /* Text of each argument */ ){ BtCursor *pCur; - Btree *pBt; int rc; if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " ID\"", 0); + " ID\"", NULL); return TCL_ERROR; } pCur = sqlite3TestTextToPtr(argv[1]); - pBt = pCur->pBtree; - sqlite3_mutex_enter(pBt->db->mutex); - sqlite3BtreeEnter(pBt); +#if SQLITE_THREADSAFE>0 + { + Btree *pBt = pCur->pBtree; + sqlite3_mutex_enter(pBt->db->mutex); + sqlite3BtreeEnter(pBt); + rc = sqlite3BtreeCloseCursor(pCur); + sqlite3BtreeLeave(pBt); + sqlite3_mutex_leave(pBt->db->mutex); + } +#else rc = sqlite3BtreeCloseCursor(pCur); - sqlite3BtreeLeave(pBt); - sqlite3_mutex_leave(pBt->db->mutex); +#endif ckfree((char *)pCur); if( rc ){ - Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); + Tcl_AppendResult(interp, sqlite3ErrName(rc), NULL); return TCL_ERROR; } return SQLITE_OK; @@ -279,7 +285,7 @@ static int btree_close_cursor( ** or 1 if the cursor was already on the last entry in the table or if ** the table is empty. */ -static int btree_next( +static int SQLITE_TCLAPI btree_next( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -292,19 +298,23 @@ static int btree_next( if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " ID\"", 0); + " ID\"", NULL); return TCL_ERROR; } pCur = sqlite3TestTextToPtr(argv[1]); sqlite3BtreeEnter(pCur->pBtree); - rc = sqlite3BtreeNext(pCur, &res); + rc = sqlite3BtreeNext(pCur, 0); + if( rc==SQLITE_DONE ){ + res = 1; + rc = SQLITE_OK; + } sqlite3BtreeLeave(pCur->pBtree); if( rc ){ - Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); + Tcl_AppendResult(interp, sqlite3ErrName(rc), NULL); return TCL_ERROR; } sqlite3_snprintf(sizeof(zBuf),zBuf,"%d",res); - Tcl_AppendResult(interp, zBuf, 0); + Tcl_AppendResult(interp, zBuf, NULL); return SQLITE_OK; } @@ -314,7 +324,7 @@ static int btree_next( ** Move the cursor to the first entry in the table. Return 0 if the ** cursor was left point to something and 1 if the table is empty. */ -static int btree_first( +static int SQLITE_TCLAPI btree_first( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -327,7 +337,7 @@ static int btree_first( if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " ID\"", 0); + " ID\"", NULL); return TCL_ERROR; } pCur = sqlite3TestTextToPtr(argv[1]); @@ -335,11 +345,11 @@ static int btree_first( rc = sqlite3BtreeFirst(pCur, &res); sqlite3BtreeLeave(pCur->pBtree); if( rc ){ - Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); + Tcl_AppendResult(interp, sqlite3ErrName(rc), NULL); return TCL_ERROR; } sqlite3_snprintf(sizeof(zBuf),zBuf,"%d",res); - Tcl_AppendResult(interp, zBuf, 0); + Tcl_AppendResult(interp, zBuf, NULL); return SQLITE_OK; } @@ -349,7 +359,7 @@ static int btree_first( ** Return TRUE if the given cursor is not pointing at a valid entry. ** Return FALSE if the cursor does point to a valid entry. */ -static int btree_eof( +static int SQLITE_TCLAPI btree_eof( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -361,7 +371,7 @@ static int btree_eof( if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " ID\"", 0); + " ID\"", NULL); return TCL_ERROR; } pCur = sqlite3TestTextToPtr(argv[1]); @@ -369,7 +379,7 @@ static int btree_eof( rc = sqlite3BtreeEof(pCur); sqlite3BtreeLeave(pCur->pBtree); sqlite3_snprintf(sizeof(zBuf),zBuf, "%d", rc); - Tcl_AppendResult(interp, zBuf, 0); + Tcl_AppendResult(interp, zBuf, NULL); return SQLITE_OK; } @@ -378,36 +388,27 @@ static int btree_eof( ** ** Return the number of bytes of payload */ -static int btree_payload_size( +static int SQLITE_TCLAPI btree_payload_size( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ const char **argv /* Text of each argument */ ){ BtCursor *pCur; - int n2; - u64 n1; + u32 n; char zBuf[50]; if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " ID\"", 0); + " ID\"", NULL); return TCL_ERROR; } pCur = sqlite3TestTextToPtr(argv[1]); sqlite3BtreeEnter(pCur->pBtree); - - /* The cursor may be in "require-seek" state. If this is the case, the - ** call to BtreeDataSize() will fix it. */ - sqlite3BtreeDataSize(pCur, (u32*)&n2); - if( pCur->apPage[pCur->iPage]->intKey ){ - n1 = 0; - }else{ - sqlite3BtreeKeySize(pCur, (i64*)&n1); - } + n = sqlite3BtreePayloadSize(pCur); sqlite3BtreeLeave(pCur->pBtree); - sqlite3_snprintf(sizeof(zBuf),zBuf, "%d", (int)(n1+n2)); - Tcl_AppendResult(interp, zBuf, 0); + sqlite3_snprintf(sizeof(zBuf),zBuf, "%u", n); + Tcl_AppendResult(interp, zBuf, NULL); return SQLITE_OK; } @@ -418,14 +419,14 @@ static int btree_payload_size( ** routines, both for accuracy and for speed. ** ** An integer is written using putVarint() and read back with -** getVarint() and varified to be unchanged. This repeats COUNT +** getVarint() and verified to be unchanged. This repeats COUNT ** times. The first integer is START*MULTIPLIER. Each iteration ** increases the integer by INCREMENT. ** ** This command returns nothing if it works. It returns an error message ** if something goes wrong. */ -static int btree_varint_test( +static int SQLITE_TCLAPI btree_varint_test( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -437,7 +438,7 @@ static int btree_varint_test( unsigned char zBuf[100]; if( argc!=5 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " START MULTIPLIER COUNT INCREMENT\"", 0); + " START MULTIPLIER COUNT INCREMENT\"", NULL); return TCL_ERROR; } if( Tcl_GetInt(interp, argv[1], (int*)&start) ) return TCL_ERROR; @@ -452,20 +453,20 @@ static int btree_varint_test( if( n1>9 || n1<1 ){ sqlite3_snprintf(sizeof(zErr), zErr, "putVarint returned %d - should be between 1 and 9", n1); - Tcl_AppendResult(interp, zErr, 0); + Tcl_AppendResult(interp, zErr, NULL); return TCL_ERROR; } n2 = getVarint(zBuf, &out); if( n1!=n2 ){ sqlite3_snprintf(sizeof(zErr), zErr, "putVarint returned %d and getVarint returned %d", n1, n2); - Tcl_AppendResult(interp, zErr, 0); + Tcl_AppendResult(interp, zErr, NULL); return TCL_ERROR; } if( in!=out ){ sqlite3_snprintf(sizeof(zErr), zErr, "Wrote 0x%016llx and got back 0x%016llx", in, out); - Tcl_AppendResult(interp, zErr, 0); + Tcl_AppendResult(interp, zErr, NULL); return TCL_ERROR; } if( (in & 0xffffffff)==in ){ @@ -476,14 +477,14 @@ static int btree_varint_test( sqlite3_snprintf(sizeof(zErr), zErr, "putVarint returned %d and GetVarint32 returned %d", n1, n2); - Tcl_AppendResult(interp, zErr, 0); + Tcl_AppendResult(interp, zErr, NULL); return TCL_ERROR; } if( in!=out ){ sqlite3_snprintf(sizeof(zErr), zErr, "Wrote 0x%016llx and got back 0x%016llx from GetVarint32", in, out); - Tcl_AppendResult(interp, zErr, 0); + Tcl_AppendResult(interp, zErr, NULL); return TCL_ERROR; } } @@ -509,7 +510,7 @@ static int btree_varint_test( ** sqlite3 db test.db ** set bt [btree_from_db db] */ -static int btree_from_db( +static int SQLITE_TCLAPI btree_from_db( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -523,12 +524,12 @@ static int btree_from_db( if( argc!=2 && argc!=3 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " DB-HANDLE ?N?\"", 0); + " DB-HANDLE ?N?\"", NULL); return TCL_ERROR; } if( 1!=Tcl_GetCommandInfo(interp, argv[1], &info) ){ - Tcl_AppendResult(interp, "No such db-handle: \"", argv[1], "\"", 0); + Tcl_AppendResult(interp, "No such db-handle: \"", argv[1], "\"", NULL); return TCL_ERROR; } if( argc==3 ){ @@ -547,9 +548,9 @@ static int btree_from_db( /* ** Usage: btree_ismemdb ID ** -** Return true if the B-Tree is in-memory. +** Return true if the B-Tree is currently stored entirely in memory. */ -static int btree_ismemdb( +static int SQLITE_TCLAPI btree_ismemdb( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -557,16 +558,18 @@ static int btree_ismemdb( ){ Btree *pBt; int res; + sqlite3_file *pFile; if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " ID\"", 0); + " ID\"", NULL); return TCL_ERROR; } pBt = sqlite3TestTextToPtr(argv[1]); sqlite3_mutex_enter(pBt->db->mutex); sqlite3BtreeEnter(pBt); - res = sqlite3PagerIsMemdb(sqlite3BtreePager(pBt)); + pFile = sqlite3PagerFile(sqlite3BtreePager(pBt)); + res = (pFile->pMethods==0); sqlite3BtreeLeave(pBt); sqlite3_mutex_leave(pBt->db->mutex); Tcl_SetObjResult(interp, Tcl_NewBooleanObj(res)); @@ -578,7 +581,7 @@ static int btree_ismemdb( ** ** Set the size of the cache used by btree $ID. */ -static int btree_set_cache_size( +static int SQLITE_TCLAPI btree_set_cache_size( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -589,7 +592,7 @@ static int btree_set_cache_size( if( argc!=3 ){ Tcl_AppendResult( - interp, "wrong # args: should be \"", argv[0], " BT NCACHE\"", 0); + interp, "wrong # args: should be \"", argv[0], " BT NCACHE\"", NULL); return TCL_ERROR; } pBt = sqlite3TestTextToPtr(argv[1]); @@ -608,7 +611,7 @@ static int btree_set_cache_size( ** ** Set the size of the cache used by btree $ID. */ -static int btree_insert( +static int SQLITE_TCLAPI btree_insert( ClientData clientData, Tcl_Interp *interp, int objc, @@ -616,33 +619,35 @@ static int btree_insert( ){ BtCursor *pCur; int rc; - void *pKey = 0; - int nKey = 0; - void *pData = 0; - int nData = 0; + BtreePayload x; + Tcl_Size n; if( objc!=4 && objc!=3 ){ Tcl_WrongNumArgs(interp, 1, objv, "?-intkey? CSR KEY VALUE"); return TCL_ERROR; } + memset(&x, 0, sizeof(x)); if( objc==4 ){ - if( Tcl_GetIntFromObj(interp, objv[2], &nKey) ) return TCL_ERROR; - pData = (void*)Tcl_GetByteArrayFromObj(objv[3], &nData); + if( Tcl_GetIntFromObj(interp, objv[2], &rc) ) return TCL_ERROR; + x.nKey = rc; + x.pData = (void*)Tcl_GetByteArrayFromObj(objv[3], &n); + x.nData = (int)n; }else{ - pKey = (void*)Tcl_GetByteArrayFromObj(objv[2], &nKey); + x.pKey = (void*)Tcl_GetByteArrayFromObj(objv[2], &n); + x.nKey = (int)n; } pCur = (BtCursor*)sqlite3TestTextToPtr(Tcl_GetString(objv[1])); sqlite3_mutex_enter(pCur->pBtree->db->mutex); sqlite3BtreeEnter(pCur->pBtree); - rc = sqlite3BtreeInsert(pCur, pKey, nKey, pData, nData, 0, 0, 0); + rc = sqlite3BtreeInsert(pCur, &x, 0, 0); sqlite3BtreeLeave(pCur->pBtree); sqlite3_mutex_leave(pCur->pBtree->db->mutex); Tcl_ResetResult(interp); if( rc ){ - Tcl_AppendResult(interp, sqlite3ErrName(rc), 0); + Tcl_AppendResult(interp, sqlite3ErrName(rc), NULL); return TCL_ERROR; } return TCL_OK; diff --git a/src/test4.c b/src/test4.c index d689030303..07236b3e5d 100644 --- a/src/test4.c +++ b/src/test4.c @@ -12,7 +12,7 @@ ** Code for testing the SQLite library in a multithreaded environment. */ #include "sqliteInt.h" -#include "tcl.h" +#include "tclsqlite.h" #if SQLITE_OS_UNIX && SQLITE_THREADSAFE #include <stdlib.h> #include <string.h> @@ -28,7 +28,7 @@ extern const char *sqlite3ErrName(int); */ typedef struct Thread Thread; struct Thread { - /* The first group of fields are writable by the master and read-only + /* The first group of fields are writable by the leader and read-only ** to the thread. */ char *zFilename; /* Name of database file */ void (*xOp)(Thread*); /* next operation to do */ @@ -37,7 +37,7 @@ struct Thread { int busy; /* True if this thread is in use */ /* The next group of fields are writable by the thread but read-only to the - ** master. */ + ** leader. */ int completed; /* Number of operations completed */ sqlite3 *db; /* Open database */ sqlite3_stmt *pStmt; /* Pending operation */ @@ -56,11 +56,16 @@ struct Thread { #define N_THREAD 26 static Thread threadset[N_THREAD]; +static void test_barrier(){ + sqlite3_mutex *pMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_APP1); + sqlite3_mutex_enter(pMutex); + sqlite3_mutex_leave(pMutex); +} /* ** The main loop for a thread. Threads use busy waiting. */ -static void *thread_main(void *pArg){ +static void *test_thread_main(void *pArg){ Thread *p = (Thread*)pArg; if( p->db ){ sqlite3_close(p->db); @@ -72,16 +77,20 @@ static void *thread_main(void *pArg){ p->db = 0; } p->pStmt = 0; + test_barrier(); p->completed = 1; while( p->opnum<=p->completed ) sched_yield(); + test_barrier(); while( p->xOp ){ if( p->zErr && p->zErr!=p->zStaticErr ){ sqlite3_free(p->zErr); p->zErr = 0; } (*p->xOp)(p); + test_barrier(); p->completed++; while( p->opnum<=p->completed ) sched_yield(); + test_barrier(); } if( p->pStmt ){ sqlite3_finalize(p->pStmt); @@ -95,6 +104,7 @@ static void *thread_main(void *pArg){ sqlite3_free(p->zErr); p->zErr = 0; } + test_barrier(); p->completed++; #ifndef SQLITE_OMIT_DEPRECATED sqlite3_thread_cleanup(); @@ -109,7 +119,7 @@ static void *thread_main(void *pArg){ */ static int parse_thread_id(Tcl_Interp *interp, const char *zArg){ if( zArg==0 || zArg[0]==0 || zArg[1]!=0 || !isupper((unsigned char)zArg[0]) ){ - Tcl_AppendResult(interp, "thread ID must be an upper case letter", 0); + Tcl_AppendResult(interp, "thread ID must be an upper case letter", NULL); return -1; } return zArg[0] - 'A'; @@ -121,7 +131,7 @@ static int parse_thread_id(Tcl_Interp *interp, const char *zArg){ ** NAME should be an upper case letter. Start the thread running with ** an open connection to the given database. */ -static int tcl_thread_create( +static int SQLITE_TCLAPI tcl_thread_create( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -133,13 +143,13 @@ static int tcl_thread_create( if( argc!=3 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " ID FILENAME", 0); + " ID FILENAME", NULL); return TCL_ERROR; } i = parse_thread_id(interp, argv[1]); if( i<0 ) return TCL_ERROR; if( threadset[i].busy ){ - Tcl_AppendResult(interp, "thread ", argv[1], " is already running", 0); + Tcl_AppendResult(interp, "thread ", argv[1], " is already running", NULL); return TCL_ERROR; } threadset[i].busy = 1; @@ -147,9 +157,9 @@ static int tcl_thread_create( threadset[i].zFilename = sqlite3_mprintf("%s", argv[2]); threadset[i].opnum = 1; threadset[i].completed = 0; - rc = pthread_create(&x, 0, thread_main, &threadset[i]); + rc = pthread_create(&x, 0, test_thread_main, &threadset[i]); if( rc ){ - Tcl_AppendResult(interp, "failed to create the thread", 0); + Tcl_AppendResult(interp, "failed to create the thread", NULL); sqlite3_free(threadset[i].zFilename); threadset[i].busy = 0; return TCL_ERROR; @@ -161,8 +171,10 @@ static int tcl_thread_create( /* ** Wait for a thread to reach its idle state. */ -static void thread_wait(Thread *p){ +static void test_thread_wait(Thread *p){ + test_barrier(); while( p->opnum>p->completed ) sched_yield(); + test_barrier(); } /* @@ -170,7 +182,7 @@ static void thread_wait(Thread *p){ ** ** Wait on thread ID to reach its idle state. */ -static int tcl_thread_wait( +static int SQLITE_TCLAPI tcl_thread_wait( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -180,27 +192,27 @@ static int tcl_thread_wait( if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " ID", 0); + " ID", NULL); return TCL_ERROR; } i = parse_thread_id(interp, argv[1]); if( i<0 ) return TCL_ERROR; if( !threadset[i].busy ){ - Tcl_AppendResult(interp, "no such thread", 0); + Tcl_AppendResult(interp, "no such thread", NULL); return TCL_ERROR; } - thread_wait(&threadset[i]); + test_thread_wait(&threadset[i]); return TCL_OK; } /* ** Stop a thread. */ -static void stop_thread(Thread *p){ - thread_wait(p); +static void test_stop_thread(Thread *p){ + test_thread_wait(p); p->xOp = 0; p->opnum++; - thread_wait(p); + test_thread_wait(p); sqlite3_free(p->zArg); p->zArg = 0; sqlite3_free(p->zFilename); @@ -214,7 +226,7 @@ static void stop_thread(Thread *p){ ** Cause a thread to shut itself down. Wait for the shutdown to be ** completed. If ID is "*" then stop all threads. */ -static int tcl_thread_halt( +static int SQLITE_TCLAPI tcl_thread_halt( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -224,21 +236,21 @@ static int tcl_thread_halt( if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " ID", 0); + " ID", NULL); return TCL_ERROR; } if( argv[1][0]=='*' && argv[1][1]==0 ){ for(i=0; i<N_THREAD; i++){ - if( threadset[i].busy ) stop_thread(&threadset[i]); + if( threadset[i].busy ) test_stop_thread(&threadset[i]); } }else{ i = parse_thread_id(interp, argv[1]); if( i<0 ) return TCL_ERROR; if( !threadset[i].busy ){ - Tcl_AppendResult(interp, "no such thread", 0); + Tcl_AppendResult(interp, "no such thread", NULL); return TCL_ERROR; } - stop_thread(&threadset[i]); + test_stop_thread(&threadset[i]); } return TCL_OK; } @@ -249,7 +261,7 @@ static int tcl_thread_halt( ** Wait on the most recent thread_step to complete, then return the ** number of columns in the result set. */ -static int tcl_thread_argc( +static int SQLITE_TCLAPI tcl_thread_argc( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -260,18 +272,18 @@ static int tcl_thread_argc( if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " ID", 0); + " ID", NULL); return TCL_ERROR; } i = parse_thread_id(interp, argv[1]); if( i<0 ) return TCL_ERROR; if( !threadset[i].busy ){ - Tcl_AppendResult(interp, "no such thread", 0); + Tcl_AppendResult(interp, "no such thread", NULL); return TCL_ERROR; } - thread_wait(&threadset[i]); + test_thread_wait(&threadset[i]); sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", threadset[i].argc); - Tcl_AppendResult(interp, zBuf, 0); + Tcl_AppendResult(interp, zBuf, NULL); return TCL_OK; } @@ -281,7 +293,7 @@ static int tcl_thread_argc( ** Wait on the most recent thread_step to complete, then return the ** value of the N-th columns in the result set. */ -static int tcl_thread_argv( +static int SQLITE_TCLAPI tcl_thread_argv( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -292,22 +304,22 @@ static int tcl_thread_argv( if( argc!=3 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " ID N", 0); + " ID N", NULL); return TCL_ERROR; } i = parse_thread_id(interp, argv[1]); if( i<0 ) return TCL_ERROR; if( !threadset[i].busy ){ - Tcl_AppendResult(interp, "no such thread", 0); + Tcl_AppendResult(interp, "no such thread", NULL); return TCL_ERROR; } if( Tcl_GetInt(interp, argv[2], &n) ) return TCL_ERROR; - thread_wait(&threadset[i]); + test_thread_wait(&threadset[i]); if( n<0 || n>=threadset[i].argc ){ - Tcl_AppendResult(interp, "column number out of range", 0); + Tcl_AppendResult(interp, "column number out of range", NULL); return TCL_ERROR; } - Tcl_AppendResult(interp, threadset[i].argv[n], 0); + Tcl_AppendResult(interp, threadset[i].argv[n], NULL); return TCL_OK; } @@ -317,7 +329,7 @@ static int tcl_thread_argv( ** Wait on the most recent thread_step to complete, then return the ** name of the N-th columns in the result set. */ -static int tcl_thread_colname( +static int SQLITE_TCLAPI tcl_thread_colname( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -328,22 +340,22 @@ static int tcl_thread_colname( if( argc!=3 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " ID N", 0); + " ID N", NULL); return TCL_ERROR; } i = parse_thread_id(interp, argv[1]); if( i<0 ) return TCL_ERROR; if( !threadset[i].busy ){ - Tcl_AppendResult(interp, "no such thread", 0); + Tcl_AppendResult(interp, "no such thread", NULL); return TCL_ERROR; } if( Tcl_GetInt(interp, argv[2], &n) ) return TCL_ERROR; - thread_wait(&threadset[i]); + test_thread_wait(&threadset[i]); if( n<0 || n>=threadset[i].argc ){ - Tcl_AppendResult(interp, "column number out of range", 0); + Tcl_AppendResult(interp, "column number out of range", NULL); return TCL_ERROR; } - Tcl_AppendResult(interp, threadset[i].colv[n], 0); + Tcl_AppendResult(interp, threadset[i].colv[n], NULL); return TCL_OK; } @@ -353,7 +365,7 @@ static int tcl_thread_colname( ** Wait on the most recent operation to complete, then return the ** result code from that operation. */ -static int tcl_thread_result( +static int SQLITE_TCLAPI tcl_thread_result( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -364,18 +376,18 @@ static int tcl_thread_result( if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " ID", 0); + " ID", NULL); return TCL_ERROR; } i = parse_thread_id(interp, argv[1]); if( i<0 ) return TCL_ERROR; if( !threadset[i].busy ){ - Tcl_AppendResult(interp, "no such thread", 0); + Tcl_AppendResult(interp, "no such thread", NULL); return TCL_ERROR; } - thread_wait(&threadset[i]); + test_thread_wait(&threadset[i]); zName = sqlite3ErrName(threadset[i].rc); - Tcl_AppendResult(interp, zName, 0); + Tcl_AppendResult(interp, zName, NULL); return TCL_OK; } @@ -385,7 +397,7 @@ static int tcl_thread_result( ** Wait on the most recent operation to complete, then return the ** error string. */ -static int tcl_thread_error( +static int SQLITE_TCLAPI tcl_thread_error( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -395,17 +407,17 @@ static int tcl_thread_error( if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " ID", 0); + " ID", NULL); return TCL_ERROR; } i = parse_thread_id(interp, argv[1]); if( i<0 ) return TCL_ERROR; if( !threadset[i].busy ){ - Tcl_AppendResult(interp, "no such thread", 0); + Tcl_AppendResult(interp, "no such thread", NULL); return TCL_ERROR; } - thread_wait(&threadset[i]); - Tcl_AppendResult(interp, threadset[i].zErr, 0); + test_thread_wait(&threadset[i]); + Tcl_AppendResult(interp, threadset[i].zErr, NULL); return TCL_OK; } @@ -430,7 +442,7 @@ static void do_compile(Thread *p){ ** ** Compile a new virtual machine. */ -static int tcl_thread_compile( +static int SQLITE_TCLAPI tcl_thread_compile( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -439,19 +451,20 @@ static int tcl_thread_compile( int i; if( argc!=3 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " ID SQL", 0); + " ID SQL", NULL); return TCL_ERROR; } i = parse_thread_id(interp, argv[1]); if( i<0 ) return TCL_ERROR; if( !threadset[i].busy ){ - Tcl_AppendResult(interp, "no such thread", 0); + Tcl_AppendResult(interp, "no such thread", NULL); return TCL_ERROR; } - thread_wait(&threadset[i]); + test_thread_wait(&threadset[i]); threadset[i].xOp = do_compile; sqlite3_free(threadset[i].zArg); threadset[i].zArg = sqlite3_mprintf("%s", argv[2]); + test_barrier(); threadset[i].opnum++; return TCL_OK; } @@ -483,7 +496,7 @@ static void do_step(Thread *p){ ** ** Advance the virtual machine by one step */ -static int tcl_thread_step( +static int SQLITE_TCLAPI tcl_thread_step( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -492,17 +505,18 @@ static int tcl_thread_step( int i; if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " IDL", 0); + " IDL", NULL); return TCL_ERROR; } i = parse_thread_id(interp, argv[1]); if( i<0 ) return TCL_ERROR; if( !threadset[i].busy ){ - Tcl_AppendResult(interp, "no such thread", 0); + Tcl_AppendResult(interp, "no such thread", NULL); return TCL_ERROR; } - thread_wait(&threadset[i]); + test_thread_wait(&threadset[i]); threadset[i].xOp = do_step; + test_barrier(); threadset[i].opnum++; return TCL_OK; } @@ -525,7 +539,7 @@ static void do_finalize(Thread *p){ ** ** Finalize the virtual machine. */ -static int tcl_thread_finalize( +static int SQLITE_TCLAPI tcl_thread_finalize( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -534,19 +548,20 @@ static int tcl_thread_finalize( int i; if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " IDL", 0); + " IDL", NULL); return TCL_ERROR; } i = parse_thread_id(interp, argv[1]); if( i<0 ) return TCL_ERROR; if( !threadset[i].busy ){ - Tcl_AppendResult(interp, "no such thread", 0); + Tcl_AppendResult(interp, "no such thread", NULL); return TCL_ERROR; } - thread_wait(&threadset[i]); + test_thread_wait(&threadset[i]); threadset[i].xOp = do_finalize; sqlite3_free(threadset[i].zArg); threadset[i].zArg = 0; + test_barrier(); threadset[i].opnum++; return TCL_OK; } @@ -556,7 +571,7 @@ static int tcl_thread_finalize( ** ** Interchange the sqlite* pointer between two threads. */ -static int tcl_thread_swap( +static int SQLITE_TCLAPI tcl_thread_swap( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -566,23 +581,23 @@ static int tcl_thread_swap( sqlite3 *temp; if( argc!=3 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " ID1 ID2", 0); + " ID1 ID2", NULL); return TCL_ERROR; } i = parse_thread_id(interp, argv[1]); if( i<0 ) return TCL_ERROR; if( !threadset[i].busy ){ - Tcl_AppendResult(interp, "no such thread", 0); + Tcl_AppendResult(interp, "no such thread", NULL); return TCL_ERROR; } - thread_wait(&threadset[i]); + test_thread_wait(&threadset[i]); j = parse_thread_id(interp, argv[2]); if( j<0 ) return TCL_ERROR; if( !threadset[j].busy ){ - Tcl_AppendResult(interp, "no such thread", 0); + Tcl_AppendResult(interp, "no such thread", NULL); return TCL_ERROR; } - thread_wait(&threadset[j]); + test_thread_wait(&threadset[j]); temp = threadset[i].db; threadset[i].db = threadset[j].db; threadset[j].db = temp; @@ -596,7 +611,7 @@ static int tcl_thread_swap( ** remove the pointer from the thread itself. Afterwards, the thread ** can be stopped and the connection can be used by the main thread. */ -static int tcl_thread_db_get( +static int SQLITE_TCLAPI tcl_thread_db_get( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -607,16 +622,16 @@ static int tcl_thread_db_get( extern int sqlite3TestMakePointerStr(Tcl_Interp*, char*, void*); if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " ID", 0); + " ID", NULL); return TCL_ERROR; } i = parse_thread_id(interp, argv[1]); if( i<0 ) return TCL_ERROR; if( !threadset[i].busy ){ - Tcl_AppendResult(interp, "no such thread", 0); + Tcl_AppendResult(interp, "no such thread", NULL); return TCL_ERROR; } - thread_wait(&threadset[i]); + test_thread_wait(&threadset[i]); sqlite3TestMakePointerStr(interp, zBuf, threadset[i].db); threadset[i].db = 0; Tcl_AppendResult(interp, zBuf, (char*)0); @@ -627,7 +642,7 @@ static int tcl_thread_db_get( ** Usage: thread_db_put ID DB ** */ -static int tcl_thread_db_put( +static int SQLITE_TCLAPI tcl_thread_db_put( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -638,16 +653,16 @@ static int tcl_thread_db_put( extern void *sqlite3TestTextToPtr(const char *); if( argc!=3 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " ID DB", 0); + " ID DB", NULL); return TCL_ERROR; } i = parse_thread_id(interp, argv[1]); if( i<0 ) return TCL_ERROR; if( !threadset[i].busy ){ - Tcl_AppendResult(interp, "no such thread", 0); + Tcl_AppendResult(interp, "no such thread", NULL); return TCL_ERROR; } - thread_wait(&threadset[i]); + test_thread_wait(&threadset[i]); assert( !threadset[i].db ); threadset[i].db = (sqlite3*)sqlite3TestTextToPtr(argv[2]); return TCL_OK; @@ -659,7 +674,7 @@ static int tcl_thread_db_put( ** Return the database stmt pointer for the given thread. Then ** remove the pointer from the thread itself. */ -static int tcl_thread_stmt_get( +static int SQLITE_TCLAPI tcl_thread_stmt_get( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ @@ -670,16 +685,16 @@ static int tcl_thread_stmt_get( extern int sqlite3TestMakePointerStr(Tcl_Interp*, char*, void*); if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " ID", 0); + " ID", NULL); return TCL_ERROR; } i = parse_thread_id(interp, argv[1]); if( i<0 ) return TCL_ERROR; if( !threadset[i].busy ){ - Tcl_AppendResult(interp, "no such thread", 0); + Tcl_AppendResult(interp, "no such thread", NULL); return TCL_ERROR; } - thread_wait(&threadset[i]); + test_thread_wait(&threadset[i]); sqlite3TestMakePointerStr(interp, zBuf, threadset[i].pStmt); threadset[i].pStmt = 0; Tcl_AppendResult(interp, zBuf, (char*)0); diff --git a/src/test5.c b/src/test5.c index 952e3325e5..06d2de911d 100644 --- a/src/test5.c +++ b/src/test5.c @@ -17,7 +17,7 @@ */ #include "sqliteInt.h" #include "vdbeInt.h" -#include "tcl.h" +#include "tclsqlite.h" #include <stdlib.h> #include <string.h> @@ -26,13 +26,13 @@ ** object with the encoded representation of the string, including ** the NULL terminator. */ -static int binarize( +static int SQLITE_TCLAPI binarize( void * clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[] ){ - int len; + Tcl_Size len; char *bytes; Tcl_Obj *pRet; assert(objc==2); @@ -54,7 +54,7 @@ static int binarize( ** If <do-calls> is 0, then the calls to sqlite3_value_text() are not ** actually made. */ -static int test_value_overhead( +static int SQLITE_TCLAPI test_value_overhead( void * clientData, Tcl_Interp *interp, int objc, @@ -67,7 +67,7 @@ static int test_value_overhead( if( objc!=3 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", - Tcl_GetStringFromObj(objv[0], 0), " <repeat-count> <do-calls>", 0); + Tcl_GetStringFromObj(objv[0], 0), " <repeat-count> <do-calls>", NULL); return TCL_ERROR; } @@ -106,7 +106,7 @@ static u8 name_to_enc(Tcl_Interp *interp, Tcl_Obj *pObj){ } } if( !pEnc->enc ){ - Tcl_AppendResult(interp, "No such encoding: ", z, 0); + Tcl_AppendResult(interp, "No such encoding: ", z, NULL); } if( pEnc->enc==SQLITE_UTF16 ){ return SQLITE_UTF16NATIVE; @@ -118,7 +118,7 @@ static u8 name_to_enc(Tcl_Interp *interp, Tcl_Obj *pObj){ ** Usage: test_translate <string/blob> <from enc> <to enc> ?<transient>? ** */ -static int test_translate( +static int SQLITE_TCLAPI test_translate( void * clientData, Tcl_Interp *interp, int objc, @@ -129,13 +129,13 @@ static int test_translate( sqlite3_value *pVal; char *z; - int len; + Tcl_Size len; void (*xDel)(void *p) = SQLITE_STATIC; if( objc!=4 && objc!=5 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", Tcl_GetStringFromObj(objv[0], 0), - " <string/blob> <from enc> <to enc>", 0 + " <string/blob> <from enc> <to enc>", NULL ); return TCL_ERROR; } @@ -160,7 +160,7 @@ static int test_translate( z = (char*)Tcl_GetByteArrayFromObj(objv[1], &len); if( objc==5 ){ char *zTmp = z; - z = sqlite3_malloc(len); + z = sqlite3_malloc64(len); memcpy(z, zTmp, len); } sqlite3ValueSetStr(pVal, -1, z, enc_from, xDel); @@ -182,7 +182,7 @@ static int test_translate( ** translation. If there is a problem an assert() will fail. **/ void sqlite3UtfSelfTest(void); -static int test_translate_selftest( +static int SQLITE_TCLAPI test_translate_selftest( void * clientData, Tcl_Interp *interp, int objc, diff --git a/src/test6.c b/src/test6.c index 306482dcd3..aee7bf12aa 100644 --- a/src/test6.c +++ b/src/test6.c @@ -16,7 +16,7 @@ */ #if SQLITE_TEST /* This file is used for testing only */ #include "sqliteInt.h" -#include "tcl.h" +#include "tclsqlite.h" #ifndef SQLITE_OMIT_DISKIO /* This file is a no-op if disk I/O is disabled */ @@ -29,25 +29,25 @@ typedef struct WriteBuffer WriteBuffer; /* ** Method: ** -** This layer is implemented as a wrapper around the "real" -** sqlite3_file object for the host system. Each time data is +** This layer is implemented as a wrapper around the "real" +** sqlite3_file object for the host system. Each time data is ** written to the file object, instead of being written to the -** underlying file, the write operation is stored in an in-memory +** underlying file, the write operation is stored in an in-memory ** structure (type WriteBuffer). This structure is placed at the ** end of a global ordered list (the write-list). ** ** When data is read from a file object, the requested region is -** first retrieved from the real file. The write-list is then -** traversed and data copied from any overlapping WriteBuffer +** first retrieved from the real file. The write-list is then +** traversed and data copied from any overlapping WriteBuffer ** structures to the output buffer. i.e. a read() operation following ** one or more write() operations works as expected, even if no ** data has actually been written out to the real file. ** -** When a fsync() operation is performed, an operating system crash -** may be simulated, in which case exit(-1) is called (the call to +** When a fsync() operation is performed, an operating system crash +** may be simulated, in which case exit(-1) is called (the call to ** xSync() never returns). Whether or not a crash is simulated, -** the data associated with a subset of the WriteBuffer structures -** stored in the write-list is written to the real underlying files +** the data associated with a subset of the WriteBuffer structures +** stored in the write-list is written to the real underlying files ** and the entries removed from the write-list. If a crash is simulated, ** a subset of the buffers may be corrupted before the data is written. ** @@ -59,13 +59,13 @@ typedef struct WriteBuffer WriteBuffer; ** Normal mode is used when the simulated device has none of the ** SQLITE_IOCAP_XXX flags set. ** -** In normal mode, if the fsync() is not a simulated crash, the +** In normal mode, if the fsync() is not a simulated crash, the ** write-list is traversed from beginning to end. Each WriteBuffer ** structure associated with the file handle used to call xSync() ** is written to the real file and removed from the write-list. ** -** If a crash is simulated, one of the following takes place for -** each WriteBuffer in the write-list, regardless of which +** If a crash is simulated, one of the following takes place for +** each WriteBuffer in the write-list, regardless of which ** file-handle it is associated with: ** ** 1. The buffer is correctly written to the file, just as if @@ -73,14 +73,14 @@ typedef struct WriteBuffer WriteBuffer; ** ** 2. Nothing is done. ** -** 3. Garbage data is written to all sectors of the file that +** 3. Garbage data is written to all sectors of the file that ** overlap the region specified by the WriteBuffer. Or garbage -** data is written to some contiguous section within the +** data is written to some contiguous section within the ** overlapped sectors. ** ** Device Characteristic flag handling: ** -** If the IOCAP_ATOMIC flag is set, then option (3) above is +** If the IOCAP_ATOMIC flag is set, then option (3) above is ** never selected. ** ** If the IOCAP_ATOMIC512 flag is set, and the WriteBuffer represents @@ -92,11 +92,11 @@ typedef struct WriteBuffer WriteBuffer; ** ** If either the IOCAP_SAFEAPPEND or IOCAP_SEQUENTIAL flags are set ** and a crash is being simulated, then an entry of the write-list is -** selected at random. Everything in the list after the selected entry +** selected at random. Everything in the list after the selected entry ** is discarded before processing begins. ** -** If IOCAP_SEQUENTIAL is set and a crash is being simulated, option -** (1) is selected for all write-list entries except the last. If a +** If IOCAP_SEQUENTIAL is set and a crash is being simulated, option +** (1) is selected for all write-list entries except the last. If a ** crash is not being simulated, then all entries in the write-list ** that occur before at least one write() on the file-handle specified ** as part of the xSync() are written to their associated real files. @@ -110,7 +110,7 @@ typedef struct WriteBuffer WriteBuffer; ** Each write operation in the write-list is represented by an instance ** of the following structure. ** -** If zBuf is 0, then this structure represents a call to xTruncate(), +** If zBuf is 0, then this structure represents a call to xTruncate(), ** not xWrite(). In that case, iOffset is the size that the file is ** truncated to. */ @@ -129,7 +129,7 @@ struct CrashFile { char *zName; int flags; /* Flags the file was opened with */ - /* Cache of the entire file. This is used to speed up OsRead() and + /* Cache of the entire file. This is used to speed up OsRead() and ** OsFileSize() calls. Although both could be done by traversing the ** write-list, in practice this is impractically slow. */ @@ -146,7 +146,7 @@ struct CrashGlobal { int iDeviceCharacteristics; /* Value of simulated device characteristics */ int iCrash; /* Crash on the iCrash'th call to xSync() */ - char zCrashFile[500]; /* Crash during an xSync() on this file */ + char zCrashFile[500]; /* Crash during an xSync() on this file */ }; static CrashGlobal g = {0, 0, SQLITE_DEFAULT_SECTOR_SIZE, 0, 0}; @@ -157,18 +157,18 @@ static CrashGlobal g = {0, 0, SQLITE_DEFAULT_SECTOR_SIZE, 0, 0}; static int sqlite3CrashTestEnable = 0; static void *crash_malloc(int nByte){ - return (void *)Tcl_Alloc((size_t)nByte); + return (void *)Tcl_AttemptAlloc((size_t)nByte); } static void crash_free(void *p){ Tcl_Free(p); } static void *crash_realloc(void *p, int n){ - return (void *)Tcl_Realloc(p, (size_t)n); + return (void *)Tcl_AttemptRealloc(p, (size_t)n); } /* ** Wrapper around the sqlite3OsWrite() function that avoids writing to the -** 512 byte block begining at offset PENDING_BYTE. +** 512 byte block beginning at offset PENDING_BYTE. */ static int writeDbFile(CrashFile *p, u8 *z, i64 iAmt, i64 iOff){ int rc = SQLITE_OK; @@ -190,7 +190,7 @@ static int writeListSync(CrashFile *pFile, int isCrash){ WriteBuffer *pWrite; WriteBuffer **ppPtr; - /* If this is not a crash simulation, set pFinal to point to the + /* If this is not a crash simulation, set pFinal to point to the ** last element of the write-list that is associated with file handle ** pFile. ** @@ -215,7 +215,9 @@ static int writeListSync(CrashFile *pFile, int isCrash){ } #ifdef TRACE_CRASHTEST - printf("Sync %s (is %s crash)\n", pFile->zName, (isCrash?"a":"not a")); + if( pFile ){ + printf("Sync %s (is %s crash)\n", pFile->zName, (isCrash?"a":"not a")); + } #endif ppPtr = &g.pWriteList; @@ -236,7 +238,7 @@ static int writeListSync(CrashFile *pFile, int isCrash){ char random; sqlite3_randomness(1, &random); - /* Do not select option 3 (sector trashing) if the IOCAP_ATOMIC flag + /* Do not select option 3 (sector trashing) if the IOCAP_ATOMIC flag ** is set or this is an OsTruncate(), not an Oswrite(). */ if( (iDc&SQLITE_IOCAP_ATOMIC) || (pWrite->zBuf==0) ){ @@ -282,7 +284,7 @@ static int writeListSync(CrashFile *pFile, int isCrash){ *ppPtr = pWrite->pNext; #ifdef TRACE_CRASHTEST if( isCrash ){ - printf("Writing %d bytes @ %d (%s)\n", + printf("Writing %d bytes @ %d (%s)\n", pWrite->nBuf, (int)pWrite->iOffset, pWrite->pFile->zName ); } @@ -294,7 +296,7 @@ static int writeListSync(CrashFile *pFile, int isCrash){ ppPtr = &pWrite->pNext; #ifdef TRACE_CRASHTEST if( isCrash ){ - printf("Omiting %d bytes @ %d (%s)\n", + printf("Omiting %d bytes @ %d (%s)\n", pWrite->nBuf, (int)pWrite->iOffset, pWrite->pFile->zName ); } @@ -309,8 +311,9 @@ static int writeListSync(CrashFile *pFile, int isCrash){ assert(pWrite->zBuf); #ifdef TRACE_CRASHTEST - printf("Trashing %d sectors @ %lld (sector %d) (%s)\n", - 1+iLast-iFirst, pWrite->iOffset, iFirst, pWrite->pFile->zName + printf("Trashing %d sectors (%d bytes) @ %lld (sector %d) (%s)\n", + 1+iLast-iFirst, (1+iLast-iFirst)*g.iSectorSize, + pWrite->iOffset, iFirst, pWrite->pFile->zName ); #endif @@ -318,7 +321,7 @@ static int writeListSync(CrashFile *pFile, int isCrash){ if( zGarbage ){ sqlite3_int64 i; for(i=iFirst; rc==SQLITE_OK && i<=iLast; i++){ - sqlite3_randomness(g.iSectorSize, zGarbage); + sqlite3_randomness(g.iSectorSize, zGarbage); rc = writeDbFile( pWrite->pFile, zGarbage, g.iSectorSize, i*g.iSectorSize ); @@ -382,7 +385,7 @@ static int writeListAppend( g.pWriteList = pNew; } g.pWriteListEnd = pNew; - + return SQLITE_OK; } @@ -400,9 +403,9 @@ static int cfClose(sqlite3_file *pFile){ ** Read data from a crash-file. */ static int cfRead( - sqlite3_file *pFile, - void *zBuf, - int iAmt, + sqlite3_file *pFile, + void *zBuf, + int iAmt, sqlite_int64 iOfst ){ CrashFile *pCrash = (CrashFile *)pFile; @@ -424,9 +427,9 @@ static int cfRead( ** Write data to a crash-file. */ static int cfWrite( - sqlite3_file *pFile, - const void *zBuf, - int iAmt, + sqlite3_file *pFile, + const void *zBuf, + int iAmt, sqlite_int64 iOfst ){ CrashFile *pCrash = (CrashFile *)pFile; @@ -543,13 +546,16 @@ static int cfDeviceCharacteristics(sqlite3_file *pFile){ ** Pass-throughs for WAL support. */ static int cfShmLock(sqlite3_file *pFile, int ofst, int n, int flags){ - return sqlite3OsShmLock(((CrashFile*)pFile)->pRealFile, ofst, n, flags); + sqlite3_file *pReal = ((CrashFile*)pFile)->pRealFile; + return pReal->pMethods->xShmLock(pReal, ofst, n, flags); } static void cfShmBarrier(sqlite3_file *pFile){ - sqlite3OsShmBarrier(((CrashFile*)pFile)->pRealFile); + sqlite3_file *pReal = ((CrashFile*)pFile)->pRealFile; + pReal->pMethods->xShmBarrier(pReal); } static int cfShmUnmap(sqlite3_file *pFile, int delFlag){ - return sqlite3OsShmUnmap(((CrashFile*)pFile)->pRealFile, delFlag); + sqlite3_file *pReal = ((CrashFile*)pFile)->pRealFile; + return pReal->pMethods->xShmUnmap(pReal, delFlag); } static int cfShmMap( sqlite3_file *pFile, /* Handle open on database file */ @@ -558,7 +564,8 @@ static int cfShmMap( int w, /* True to extend file if necessary */ void volatile **pp /* OUT: Mapped memory */ ){ - return sqlite3OsShmMap(((CrashFile*)pFile)->pRealFile, iRegion, sz, w, pp); + sqlite3_file *pReal = ((CrashFile*)pFile)->pRealFile; + return pReal->pMethods->xShmMap(pReal, iRegion, sz, w, pp); } static const sqlite3_io_methods CrashFileVtab = { @@ -593,7 +600,7 @@ struct crashAppData { ** ** The caller will have allocated pVfs->szOsFile bytes of space ** at pFile. This file uses this space for the CrashFile structure -** and allocates space for the "real" file structure using +** and allocates space for the "real" file structure using ** sqlite3_malloc(). The assumption here is (pVfs->szOsFile) is ** equal or greater than sizeof(CrashFile). */ @@ -656,17 +663,17 @@ static int cfDelete(sqlite3_vfs *pCfVfs, const char *zPath, int dirSync){ return pVfs->xDelete(pVfs, zPath, dirSync); } static int cfAccess( - sqlite3_vfs *pCfVfs, - const char *zPath, - int flags, + sqlite3_vfs *pCfVfs, + const char *zPath, + int flags, int *pResOut ){ sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; return pVfs->xAccess(pVfs, zPath, flags, pResOut); } static int cfFullPathname( - sqlite3_vfs *pCfVfs, - const char *zPath, + sqlite3_vfs *pCfVfs, + const char *zPath, int nPathOut, char *zPathOut ){ @@ -701,6 +708,10 @@ static int cfCurrentTime(sqlite3_vfs *pCfVfs, double *pTimeOut){ sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; return pVfs->xCurrentTime(pVfs, pTimeOut); } +static int cfGetLastError(sqlite3_vfs *pCfVfs, int n, char *z){ + sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; + return pVfs->xGetLastError(pVfs, n, z); +} static int processDevSymArgs( Tcl_Interp *interp, @@ -725,6 +736,7 @@ static int processDevSymArgs( { "sequential", SQLITE_IOCAP_SEQUENTIAL }, { "safe_append", SQLITE_IOCAP_SAFE_APPEND }, { "powersafe_overwrite", SQLITE_IOCAP_POWERSAFE_OVERWRITE }, + { "batch-atomic", SQLITE_IOCAP_BATCH_ATOMIC }, { 0, 0 } }; @@ -735,20 +747,20 @@ static int processDevSymArgs( int setDeviceChar = 0; for(i=0; i<objc; i+=2){ - int nOpt; + Tcl_Size nOpt; char *zOpt = Tcl_GetStringFromObj(objv[i], &nOpt); - if( (nOpt>11 || nOpt<2 || strncmp("-sectorsize", zOpt, nOpt)) + if( (nOpt>11 || nOpt<2 || strncmp("-sectorsize", zOpt, nOpt)) && (nOpt>16 || nOpt<2 || strncmp("-characteristics", zOpt, nOpt)) ){ - Tcl_AppendResult(interp, - "Bad option: \"", zOpt, - "\" - must be \"-characteristics\" or \"-sectorsize\"", 0 + Tcl_AppendResult(interp, + "Bad option: \"", zOpt, + "\" - must be \"-characteristics\" or \"-sectorsize\"", NULL ); return TCL_ERROR; } if( i==objc-1 ){ - Tcl_AppendResult(interp, "Option requires an argument: \"", zOpt, "\"",0); + Tcl_AppendResult(interp, "Option requires an argument: \"", zOpt, "\"", NULL); return TCL_ERROR; } @@ -760,17 +772,17 @@ static int processDevSymArgs( }else{ int j; Tcl_Obj **apObj; - int nObj; + Tcl_Size nObj; if( Tcl_ListObjGetElements(interp, objv[i+1], &nObj, &apObj) ){ return TCL_ERROR; } - for(j=0; j<nObj; j++){ + for(j=0; j<(int)nObj; j++){ int rc; int iChoice; Tcl_Obj *pFlag = Tcl_DuplicateObj(apObj[j]); Tcl_IncrRefCount(pFlag); Tcl_UtfToLower(Tcl_GetString(pFlag)); - + rc = Tcl_GetIndexFromObjStruct( interp, pFlag, aFlag, sizeof(aFlag[0]), "no such flag", 0, &iChoice ); @@ -796,18 +808,40 @@ static int processDevSymArgs( } /* -** tclcmd: sqlite_crash_enable ENABLE +** tclcmd: sqlite3_crash_now +** +** Simulate a crash immediately. This function does not return +** (writeListSync() calls exit(-1)). +*/ +static int SQLITE_TCLAPI crashNowCmd( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + if( objc!=1 ){ + Tcl_WrongNumArgs(interp, 1, objv, ""); + return TCL_ERROR; + } + writeListSync(0, 1); + assert( 0 ); + return TCL_OK; +} + +/* +** tclcmd: sqlite_crash_enable ENABLE ?DEFAULT? ** ** Parameter ENABLE must be a boolean value. If true, then the "crash" ** vfs is added to the system. If false, it is removed. */ -static int crashEnableCmd( +static int SQLITE_TCLAPI crashEnableCmd( void * clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[] ){ int isEnable; + int isDefault = 0; static sqlite3_vfs crashVfs = { 2, /* iVersion */ 0, /* szOsFile */ @@ -815,7 +849,7 @@ static int crashEnableCmd( 0, /* pNext */ "crash", /* zName */ 0, /* pAppData */ - + cfOpen, /* xOpen */ cfDelete, /* xDelete */ cfAccess, /* xAccess */ @@ -827,18 +861,21 @@ static int crashEnableCmd( cfRandomness, /* xRandomness */ cfSleep, /* xSleep */ cfCurrentTime, /* xCurrentTime */ - 0, /* xGetlastError */ + cfGetLastError, /* xGetLastError */ 0, /* xCurrentTimeInt64 */ }; - if( objc!=2 ){ - Tcl_WrongNumArgs(interp, 1, objv, "ENABLE"); + if( objc!=2 && objc!=3 ){ + Tcl_WrongNumArgs(interp, 1, objv, "ENABLE ?DEFAULT?"); return TCL_ERROR; } if( Tcl_GetBooleanFromObj(interp, objv[1], &isEnable) ){ return TCL_ERROR; } + if( objc==3 && Tcl_GetBooleanFromObj(interp, objv[2], &isDefault) ){ + return TCL_ERROR; + } if( (isEnable && crashVfs.pAppData) || (!isEnable && !crashVfs.pAppData) ){ return TCL_OK; @@ -849,7 +886,7 @@ static int crashEnableCmd( crashVfs.mxPathname = pOriginalVfs->mxPathname; crashVfs.pAppData = (void *)pOriginalVfs; crashVfs.szOsFile = sizeof(CrashFile) + pOriginalVfs->szOsFile; - sqlite3_vfs_register(&crashVfs, 0); + sqlite3_vfs_register(&crashVfs, isDefault); }else{ crashVfs.pAppData = 0; sqlite3_vfs_unregister(&crashVfs); @@ -868,7 +905,7 @@ static int crashEnableCmd( ** an argument. For -sectorsize, this is the simulated sector size in ** bytes. For -characteristics, the argument must be a list of io-capability ** flags to simulate. Valid flags are "atomic", "atomic512", "atomic1K", -** "atomic2K", "atomic4K", "atomic8K", "atomic16K", "atomic32K", +** "atomic2K", "atomic4K", "atomic8K", "atomic16K", "atomic32K", ** "atomic64K", "sequential" and "safe_append". ** ** Example: @@ -876,7 +913,7 @@ static int crashEnableCmd( ** sqlite_crashparams -sect 1024 -char {atomic sequential} ./test.db 1 ** */ -static int crashParamsObjCmd( +static int SQLITE_TCLAPI crashParamsObjCmd( void * clientData, Tcl_Interp *interp, int objc, @@ -884,7 +921,8 @@ static int crashParamsObjCmd( ){ int iDelay; const char *zCrashFile; - int nCrashFile, iDc, iSectorSize; + Tcl_Size nCrashFile; + int iDc, iSectorSize; iDc = -1; iSectorSize = -1; @@ -896,7 +934,7 @@ static int crashParamsObjCmd( zCrashFile = Tcl_GetStringFromObj(objv[objc-1], &nCrashFile); if( nCrashFile>=sizeof(g.zCrashFile) ){ - Tcl_AppendResult(interp, "Filename is too long: \"", zCrashFile, "\"", 0); + Tcl_AppendResult(interp, "Filename is too long: \"", zCrashFile, "\"", NULL); goto error; } if( Tcl_GetIntFromObj(interp, objv[objc-2], &iDelay) ){ @@ -923,7 +961,7 @@ static int crashParamsObjCmd( return TCL_ERROR; } -static int devSymObjCmd( +static int SQLITE_TCLAPI devSymObjCmd( void * clientData, Tcl_Interp *interp, int objc, @@ -942,10 +980,54 @@ static int devSymObjCmd( return TCL_OK; } +/* +** tclcmd: sqlite3_crash_on_write N +*/ +static int SQLITE_TCLAPI writeCrashObjCmd( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + void devsym_crash_on_write(int); + int nWrite = 0; + + if( objc!=2 ){ + Tcl_WrongNumArgs(interp, 1, objv, "NWRITE"); + return TCL_ERROR; + } + if( Tcl_GetIntFromObj(interp, objv[1], &nWrite) ){ + return TCL_ERROR; + } + + devsym_crash_on_write(nWrite); + return TCL_OK; +} + +/* +** tclcmd: unregister_devsim +*/ +static int SQLITE_TCLAPI dsUnregisterObjCmd( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + void devsym_unregister(void); + + if( objc!=1 ){ + Tcl_WrongNumArgs(interp, 1, objv, ""); + return TCL_ERROR; + } + + devsym_unregister(); + return TCL_OK; +} + /* ** tclcmd: register_jt_vfs ?-default? PARENT-VFS */ -static int jtObjCmd( +static int SQLITE_TCLAPI jtObjCmd( void * clientData, Tcl_Interp *interp, int objc, @@ -961,8 +1043,8 @@ static int jtObjCmd( zParent = Tcl_GetString(objv[1]); if( objc==3 ){ if( strcmp(zParent, "-default") ){ - Tcl_AppendResult(interp, - "bad option \"", zParent, "\": must be -default", 0 + Tcl_AppendResult(interp, + "bad option \"", zParent, "\": must be -default", NULL ); return TCL_ERROR; } @@ -973,7 +1055,7 @@ static int jtObjCmd( zParent = 0; } if( jt_register(zParent, objc==3) ){ - Tcl_AppendResult(interp, "Error in jt_register", 0); + Tcl_AppendResult(interp, "Error in jt_register", NULL); return TCL_ERROR; } @@ -983,7 +1065,7 @@ static int jtObjCmd( /* ** tclcmd: unregister_jt_vfs */ -static int jtUnregisterObjCmd( +static int SQLITE_TCLAPI jtUnregisterObjCmd( void * clientData, Tcl_Interp *interp, int objc, @@ -1009,7 +1091,10 @@ int Sqlitetest6_Init(Tcl_Interp *interp){ #ifndef SQLITE_OMIT_DISKIO Tcl_CreateObjCommand(interp, "sqlite3_crash_enable", crashEnableCmd, 0, 0); Tcl_CreateObjCommand(interp, "sqlite3_crashparams", crashParamsObjCmd, 0, 0); + Tcl_CreateObjCommand(interp, "sqlite3_crash_now", crashNowCmd, 0, 0); Tcl_CreateObjCommand(interp, "sqlite3_simulate_device", devSymObjCmd, 0, 0); + Tcl_CreateObjCommand(interp, "sqlite3_crash_on_write", writeCrashObjCmd,0,0); + Tcl_CreateObjCommand(interp, "unregister_devsim", dsUnregisterObjCmd, 0, 0); Tcl_CreateObjCommand(interp, "register_jt_vfs", jtObjCmd, 0, 0); Tcl_CreateObjCommand(interp, "unregister_jt_vfs", jtUnregisterObjCmd, 0, 0); #endif diff --git a/src/test7.c b/src/test7.c deleted file mode 100644 index 6ba3631b41..0000000000 --- a/src/test7.c +++ /dev/null @@ -1,714 +0,0 @@ -/* -** 2006 January 09 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -************************************************************************* -** Code for testing the client/server version of the SQLite library. -** Derived from test4.c. -*/ -#include "sqliteInt.h" -#include "tcl.h" - -/* -** This test only works on UNIX with a SQLITE_THREADSAFE build that includes -** the SQLITE_SERVER option. -*/ -#if defined(SQLITE_SERVER) && !defined(SQLITE_OMIT_SHARED_CACHE) && \ - SQLITE_OS_UNIX && SQLITE_THREADSAFE - -#include <stdlib.h> -#include <string.h> -#include <pthread.h> -#include <sched.h> -#include <ctype.h> - -/* -** Interfaces defined in server.c -*/ -int sqlite3_client_open(const char*, sqlite3**); -int sqlite3_client_prepare(sqlite3*,const char*,int, - sqlite3_stmt**,const char**); -int sqlite3_client_step(sqlite3_stmt*); -int sqlite3_client_reset(sqlite3_stmt*); -int sqlite3_client_finalize(sqlite3_stmt*); -int sqlite3_client_close(sqlite3*); -int sqlite3_server_start(void); -int sqlite3_server_stop(void); -void sqlite3_server_start2(int *pnDecr); - -/* -** Each thread is controlled by an instance of the following -** structure. -*/ -typedef struct Thread Thread; -struct Thread { - /* The first group of fields are writable by the supervisor thread - ** and read-only to the client threads - */ - char *zFilename; /* Name of database file */ - void (*xOp)(Thread*); /* next operation to do */ - char *zArg; /* argument usable by xOp */ - volatile int opnum; /* Operation number */ - volatile int busy; /* True if this thread is in use */ - - /* The next group of fields are writable by the client threads - ** but read-only to the superviser thread. - */ - volatile int completed; /* Number of operations completed */ - sqlite3 *db; /* Open database */ - sqlite3_stmt *pStmt; /* Pending operation */ - char *zErr; /* operation error */ - char *zStaticErr; /* Static error message */ - int rc; /* operation return code */ - int argc; /* number of columns in result */ - const char *argv[100]; /* result columns */ - const char *colv[100]; /* result column names */ - - /* Initialized to 1 by the supervisor thread when the client is - ** created, and then deemed read-only to the supervisor thread. - ** Is set to 0 by the server thread belonging to this client - ** just before it exits. - */ - int nServer; /* Number of server threads running */ -}; - -/* -** There can be as many as 26 threads running at once. Each is named -** by a capital letter: A, B, C, ..., Y, Z. -*/ -#define N_THREAD 26 -static Thread threadset[N_THREAD]; - -/* -** The main loop for a thread. Threads use busy waiting. -*/ -static void *client_main(void *pArg){ - Thread *p = (Thread*)pArg; - if( p->db ){ - sqlite3_client_close(p->db); - } - sqlite3_client_open(p->zFilename, &p->db); - if( SQLITE_OK!=sqlite3_errcode(p->db) ){ - p->zErr = strdup(sqlite3_errmsg(p->db)); - sqlite3_client_close(p->db); - p->db = 0; - } - p->pStmt = 0; - p->completed = 1; - while( p->opnum<=p->completed ) sched_yield(); - while( p->xOp ){ - if( p->zErr && p->zErr!=p->zStaticErr ){ - sqlite3_free(p->zErr); - p->zErr = 0; - } - (*p->xOp)(p); - p->completed++; - while( p->opnum<=p->completed ) sched_yield(); - } - if( p->pStmt ){ - sqlite3_client_finalize(p->pStmt); - p->pStmt = 0; - } - if( p->db ){ - sqlite3_client_close(p->db); - p->db = 0; - } - if( p->zErr && p->zErr!=p->zStaticErr ){ - sqlite3_free(p->zErr); - p->zErr = 0; - } - p->completed++; -#ifndef SQLITE_OMIT_DEPRECATED - sqlite3_thread_cleanup(); -#endif - return 0; -} - -/* -** Get a thread ID which is an upper case letter. Return the index. -** If the argument is not a valid thread ID put an error message in -** the interpreter and return -1. -*/ -static int parse_client_id(Tcl_Interp *interp, const char *zArg){ - if( zArg==0 || zArg[0]==0 || zArg[1]!=0 || !isupper((unsigned char)zArg[0]) ){ - Tcl_AppendResult(interp, "thread ID must be an upper case letter", 0); - return -1; - } - return zArg[0] - 'A'; -} - -/* -** Usage: client_create NAME FILENAME -** -** NAME should be an upper case letter. Start the thread running with -** an open connection to the given database. -*/ -static int tcl_client_create( - void *NotUsed, - Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ - int argc, /* Number of arguments */ - const char **argv /* Text of each argument */ -){ - int i; - pthread_t x; - int rc; - - if( argc!=3 ){ - Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " ID FILENAME", 0); - return TCL_ERROR; - } - i = parse_client_id(interp, argv[1]); - if( i<0 ) return TCL_ERROR; - if( threadset[i].busy ){ - Tcl_AppendResult(interp, "thread ", argv[1], " is already running", 0); - return TCL_ERROR; - } - threadset[i].busy = 1; - sqlite3_free(threadset[i].zFilename); - threadset[i].zFilename = sqlite3_mprintf("%s", argv[2]); - threadset[i].opnum = 1; - threadset[i].completed = 0; - rc = pthread_create(&x, 0, client_main, &threadset[i]); - if( rc ){ - Tcl_AppendResult(interp, "failed to create the thread", 0); - sqlite3_free(threadset[i].zFilename); - threadset[i].busy = 0; - return TCL_ERROR; - } - pthread_detach(x); - if( threadset[i].nServer==0 ){ - threadset[i].nServer = 1; - sqlite3_server_start2(&threadset[i].nServer); - } - return TCL_OK; -} - -/* -** Wait for a thread to reach its idle state. -*/ -static void client_wait(Thread *p){ - while( p->opnum>p->completed ) sched_yield(); -} - -/* -** Usage: client_wait ID -** -** Wait on thread ID to reach its idle state. -*/ -static int tcl_client_wait( - void *NotUsed, - Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ - int argc, /* Number of arguments */ - const char **argv /* Text of each argument */ -){ - int i; - - if( argc!=2 ){ - Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " ID", 0); - return TCL_ERROR; - } - i = parse_client_id(interp, argv[1]); - if( i<0 ) return TCL_ERROR; - if( !threadset[i].busy ){ - Tcl_AppendResult(interp, "no such thread", 0); - return TCL_ERROR; - } - client_wait(&threadset[i]); - return TCL_OK; -} - -/* -** Stop a thread. -*/ -static void stop_thread(Thread *p){ - client_wait(p); - p->xOp = 0; - p->opnum++; - client_wait(p); - sqlite3_free(p->zArg); - p->zArg = 0; - sqlite3_free(p->zFilename); - p->zFilename = 0; - p->busy = 0; -} - -/* -** Usage: client_halt ID -** -** Cause a client thread to shut itself down. Wait for the shutdown to be -** completed. If ID is "*" then stop all client threads. -*/ -static int tcl_client_halt( - void *NotUsed, - Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ - int argc, /* Number of arguments */ - const char **argv /* Text of each argument */ -){ - int i; - - if( argc!=2 ){ - Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " ID", 0); - return TCL_ERROR; - } - if( argv[1][0]=='*' && argv[1][1]==0 ){ - for(i=0; i<N_THREAD; i++){ - if( threadset[i].busy ){ - stop_thread(&threadset[i]); - } - } - }else{ - i = parse_client_id(interp, argv[1]); - if( i<0 ) return TCL_ERROR; - if( !threadset[i].busy ){ - Tcl_AppendResult(interp, "no such thread", 0); - return TCL_ERROR; - } - stop_thread(&threadset[i]); - } - - /* If no client threads are still running, also stop the server */ - for(i=0; i<N_THREAD && threadset[i].busy==0; i++){} - if( i>=N_THREAD ){ - sqlite3_server_stop(); - while( 1 ){ - for(i=0; i<N_THREAD && threadset[i].nServer==0; i++); - if( i==N_THREAD ) break; - sched_yield(); - } - } - return TCL_OK; -} - -/* -** Usage: client_argc ID -** -** Wait on the most recent client_step to complete, then return the -** number of columns in the result set. -*/ -static int tcl_client_argc( - void *NotUsed, - Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ - int argc, /* Number of arguments */ - const char **argv /* Text of each argument */ -){ - int i; - char zBuf[100]; - - if( argc!=2 ){ - Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " ID", 0); - return TCL_ERROR; - } - i = parse_client_id(interp, argv[1]); - if( i<0 ) return TCL_ERROR; - if( !threadset[i].busy ){ - Tcl_AppendResult(interp, "no such thread", 0); - return TCL_ERROR; - } - client_wait(&threadset[i]); - sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", threadset[i].argc); - Tcl_AppendResult(interp, zBuf, 0); - return TCL_OK; -} - -/* -** Usage: client_argv ID N -** -** Wait on the most recent client_step to complete, then return the -** value of the N-th columns in the result set. -*/ -static int tcl_client_argv( - void *NotUsed, - Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ - int argc, /* Number of arguments */ - const char **argv /* Text of each argument */ -){ - int i; - int n; - - if( argc!=3 ){ - Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " ID N", 0); - return TCL_ERROR; - } - i = parse_client_id(interp, argv[1]); - if( i<0 ) return TCL_ERROR; - if( !threadset[i].busy ){ - Tcl_AppendResult(interp, "no such thread", 0); - return TCL_ERROR; - } - if( Tcl_GetInt(interp, argv[2], &n) ) return TCL_ERROR; - client_wait(&threadset[i]); - if( n<0 || n>=threadset[i].argc ){ - Tcl_AppendResult(interp, "column number out of range", 0); - return TCL_ERROR; - } - Tcl_AppendResult(interp, threadset[i].argv[n], 0); - return TCL_OK; -} - -/* -** Usage: client_colname ID N -** -** Wait on the most recent client_step to complete, then return the -** name of the N-th columns in the result set. -*/ -static int tcl_client_colname( - void *NotUsed, - Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ - int argc, /* Number of arguments */ - const char **argv /* Text of each argument */ -){ - int i; - int n; - - if( argc!=3 ){ - Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " ID N", 0); - return TCL_ERROR; - } - i = parse_client_id(interp, argv[1]); - if( i<0 ) return TCL_ERROR; - if( !threadset[i].busy ){ - Tcl_AppendResult(interp, "no such thread", 0); - return TCL_ERROR; - } - if( Tcl_GetInt(interp, argv[2], &n) ) return TCL_ERROR; - client_wait(&threadset[i]); - if( n<0 || n>=threadset[i].argc ){ - Tcl_AppendResult(interp, "column number out of range", 0); - return TCL_ERROR; - } - Tcl_AppendResult(interp, threadset[i].colv[n], 0); - return TCL_OK; -} - -extern const char *sqlite3ErrName(int); - -/* -** Usage: client_result ID -** -** Wait on the most recent operation to complete, then return the -** result code from that operation. -*/ -static int tcl_client_result( - void *NotUsed, - Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ - int argc, /* Number of arguments */ - const char **argv /* Text of each argument */ -){ - int i; - const char *zName; - - if( argc!=2 ){ - Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " ID", 0); - return TCL_ERROR; - } - i = parse_client_id(interp, argv[1]); - if( i<0 ) return TCL_ERROR; - if( !threadset[i].busy ){ - Tcl_AppendResult(interp, "no such thread", 0); - return TCL_ERROR; - } - client_wait(&threadset[i]); - zName = sqlite3ErrName(threadset[i].rc); - Tcl_AppendResult(interp, zName, 0); - return TCL_OK; -} - -/* -** Usage: client_error ID -** -** Wait on the most recent operation to complete, then return the -** error string. -*/ -static int tcl_client_error( - void *NotUsed, - Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ - int argc, /* Number of arguments */ - const char **argv /* Text of each argument */ -){ - int i; - - if( argc!=2 ){ - Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " ID", 0); - return TCL_ERROR; - } - i = parse_client_id(interp, argv[1]); - if( i<0 ) return TCL_ERROR; - if( !threadset[i].busy ){ - Tcl_AppendResult(interp, "no such thread", 0); - return TCL_ERROR; - } - client_wait(&threadset[i]); - Tcl_AppendResult(interp, threadset[i].zErr, 0); - return TCL_OK; -} - -/* -** This procedure runs in the thread to compile an SQL statement. -*/ -static void do_compile(Thread *p){ - if( p->db==0 ){ - p->zErr = p->zStaticErr = "no database is open"; - p->rc = SQLITE_ERROR; - return; - } - if( p->pStmt ){ - sqlite3_client_finalize(p->pStmt); - p->pStmt = 0; - } - p->rc = sqlite3_client_prepare(p->db, p->zArg, -1, &p->pStmt, 0); -} - -/* -** Usage: client_compile ID SQL -** -** Compile a new virtual machine. -*/ -static int tcl_client_compile( - void *NotUsed, - Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ - int argc, /* Number of arguments */ - const char **argv /* Text of each argument */ -){ - int i; - if( argc!=3 ){ - Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " ID SQL", 0); - return TCL_ERROR; - } - i = parse_client_id(interp, argv[1]); - if( i<0 ) return TCL_ERROR; - if( !threadset[i].busy ){ - Tcl_AppendResult(interp, "no such thread", 0); - return TCL_ERROR; - } - client_wait(&threadset[i]); - threadset[i].xOp = do_compile; - sqlite3_free(threadset[i].zArg); - threadset[i].zArg = sqlite3_mprintf("%s", argv[2]); - threadset[i].opnum++; - return TCL_OK; -} - -/* -** This procedure runs in the thread to step the virtual machine. -*/ -static void do_step(Thread *p){ - int i; - if( p->pStmt==0 ){ - p->zErr = p->zStaticErr = "no virtual machine available"; - p->rc = SQLITE_ERROR; - return; - } - p->rc = sqlite3_client_step(p->pStmt); - if( p->rc==SQLITE_ROW ){ - p->argc = sqlite3_column_count(p->pStmt); - for(i=0; i<sqlite3_data_count(p->pStmt); i++){ - p->argv[i] = (char*)sqlite3_column_text(p->pStmt, i); - } - for(i=0; i<p->argc; i++){ - p->colv[i] = sqlite3_column_name(p->pStmt, i); - } - } -} - -/* -** Usage: client_step ID -** -** Advance the virtual machine by one step -*/ -static int tcl_client_step( - void *NotUsed, - Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ - int argc, /* Number of arguments */ - const char **argv /* Text of each argument */ -){ - int i; - if( argc!=2 ){ - Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " IDL", 0); - return TCL_ERROR; - } - i = parse_client_id(interp, argv[1]); - if( i<0 ) return TCL_ERROR; - if( !threadset[i].busy ){ - Tcl_AppendResult(interp, "no such thread", 0); - return TCL_ERROR; - } - client_wait(&threadset[i]); - threadset[i].xOp = do_step; - threadset[i].opnum++; - return TCL_OK; -} - -/* -** This procedure runs in the thread to finalize a virtual machine. -*/ -static void do_finalize(Thread *p){ - if( p->pStmt==0 ){ - p->zErr = p->zStaticErr = "no virtual machine available"; - p->rc = SQLITE_ERROR; - return; - } - p->rc = sqlite3_client_finalize(p->pStmt); - p->pStmt = 0; -} - -/* -** Usage: client_finalize ID -** -** Finalize the virtual machine. -*/ -static int tcl_client_finalize( - void *NotUsed, - Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ - int argc, /* Number of arguments */ - const char **argv /* Text of each argument */ -){ - int i; - if( argc!=2 ){ - Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " IDL", 0); - return TCL_ERROR; - } - i = parse_client_id(interp, argv[1]); - if( i<0 ) return TCL_ERROR; - if( !threadset[i].busy ){ - Tcl_AppendResult(interp, "no such thread", 0); - return TCL_ERROR; - } - client_wait(&threadset[i]); - threadset[i].xOp = do_finalize; - sqlite3_free(threadset[i].zArg); - threadset[i].zArg = 0; - threadset[i].opnum++; - return TCL_OK; -} - -/* -** This procedure runs in the thread to reset a virtual machine. -*/ -static void do_reset(Thread *p){ - if( p->pStmt==0 ){ - p->zErr = p->zStaticErr = "no virtual machine available"; - p->rc = SQLITE_ERROR; - return; - } - p->rc = sqlite3_client_reset(p->pStmt); - p->pStmt = 0; -} - -/* -** Usage: client_reset ID -** -** Finalize the virtual machine. -*/ -static int tcl_client_reset( - void *NotUsed, - Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ - int argc, /* Number of arguments */ - const char **argv /* Text of each argument */ -){ - int i; - if( argc!=2 ){ - Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " IDL", 0); - return TCL_ERROR; - } - i = parse_client_id(interp, argv[1]); - if( i<0 ) return TCL_ERROR; - if( !threadset[i].busy ){ - Tcl_AppendResult(interp, "no such thread", 0); - return TCL_ERROR; - } - client_wait(&threadset[i]); - threadset[i].xOp = do_reset; - sqlite3_free(threadset[i].zArg); - threadset[i].zArg = 0; - threadset[i].opnum++; - return TCL_OK; -} - -/* -** Usage: client_swap ID ID -** -** Interchange the sqlite* pointer between two threads. -*/ -static int tcl_client_swap( - void *NotUsed, - Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ - int argc, /* Number of arguments */ - const char **argv /* Text of each argument */ -){ - int i, j; - sqlite3 *temp; - if( argc!=3 ){ - Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], - " ID1 ID2", 0); - return TCL_ERROR; - } - i = parse_client_id(interp, argv[1]); - if( i<0 ) return TCL_ERROR; - if( !threadset[i].busy ){ - Tcl_AppendResult(interp, "no such thread", 0); - return TCL_ERROR; - } - client_wait(&threadset[i]); - j = parse_client_id(interp, argv[2]); - if( j<0 ) return TCL_ERROR; - if( !threadset[j].busy ){ - Tcl_AppendResult(interp, "no such thread", 0); - return TCL_ERROR; - } - client_wait(&threadset[j]); - temp = threadset[i].db; - threadset[i].db = threadset[j].db; - threadset[j].db = temp; - return TCL_OK; -} - -/* -** Register commands with the TCL interpreter. -*/ -int Sqlitetest7_Init(Tcl_Interp *interp){ - static struct { - char *zName; - Tcl_CmdProc *xProc; - } aCmd[] = { - { "client_create", (Tcl_CmdProc*)tcl_client_create }, - { "client_wait", (Tcl_CmdProc*)tcl_client_wait }, - { "client_halt", (Tcl_CmdProc*)tcl_client_halt }, - { "client_argc", (Tcl_CmdProc*)tcl_client_argc }, - { "client_argv", (Tcl_CmdProc*)tcl_client_argv }, - { "client_colname", (Tcl_CmdProc*)tcl_client_colname }, - { "client_result", (Tcl_CmdProc*)tcl_client_result }, - { "client_error", (Tcl_CmdProc*)tcl_client_error }, - { "client_compile", (Tcl_CmdProc*)tcl_client_compile }, - { "client_step", (Tcl_CmdProc*)tcl_client_step }, - { "client_reset", (Tcl_CmdProc*)tcl_client_reset }, - { "client_finalize", (Tcl_CmdProc*)tcl_client_finalize }, - { "client_swap", (Tcl_CmdProc*)tcl_client_swap }, - }; - int i; - - for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){ - Tcl_CreateCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0); - } - return TCL_OK; -} -#else -int Sqlitetest7_Init(Tcl_Interp *interp){ return TCL_OK; } -#endif /* SQLITE_OS_UNIX */ diff --git a/src/test8.c b/src/test8.c index fb781ac8fd..8a13f5d556 100644 --- a/src/test8.c +++ b/src/test8.c @@ -14,7 +14,7 @@ ** testing of the SQLite library. */ #include "sqliteInt.h" -#include "tcl.h" +#include "tclsqlite.h" #include <stdlib.h> #include <string.h> @@ -25,7 +25,7 @@ typedef struct echo_cursor echo_cursor; /* ** The test module defined in this file uses four global Tcl variables to -** commicate with test-scripts: +** communicate with test-scripts: ** ** $::echo_module ** $::echo_module_sync_fail @@ -337,7 +337,7 @@ static int echoDeclareVtab( if( pVtab->zTableName ){ sqlite3_stmt *pStmt = 0; rc = sqlite3_prepare(db, - "SELECT sql FROM sqlite_master WHERE type = 'table' AND name = ?", + "SELECT sql FROM sqlite_schema WHERE type = 'table' AND name = ?", -1, &pStmt, 0); if( rc==SQLITE_OK ){ sqlite3_bind_text(pStmt, 1, pVtab->zTableName, -1, 0); @@ -385,6 +385,7 @@ static int echoDestructor(sqlite3_vtab *pVtab){ typedef struct EchoModule EchoModule; struct EchoModule { Tcl_Interp *interp; + sqlite3 *db; }; /* @@ -893,17 +894,18 @@ static int echoBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){ case SQLITE_INDEX_CONSTRAINT_REGEXP: zOp = "regexp"; break; } - if( zOp[0]=='L' ){ - zNew = sqlite3_mprintf(" %s %s LIKE (SELECT '%%'||?||'%%')", - zSep, zNewCol); - } else { - zNew = sqlite3_mprintf(" %s %s %s ?", zSep, zNewCol, zOp); + if( zOp ){ + if( zOp[0]=='L' ){ + zNew = sqlite3_mprintf(" %s %s LIKE (SELECT '%%'||?||'%%')", + zSep, zNewCol); + } else { + zNew = sqlite3_mprintf(" %s %s %s ?", zSep, zNewCol, zOp); + } + string_concat(&zQuery, zNew, 1, &rc); + zSep = "AND"; + pUsage->argvIndex = ++nArg; + pUsage->omit = 1; } - string_concat(&zQuery, zNew, 1, &rc); - - zSep = "AND"; - pUsage->argvIndex = ++nArg; - pUsage->omit = 1; } } @@ -1311,7 +1313,12 @@ static sqlite3_module echoModule = { echoCommit, /* xCommit - commit transaction */ echoRollback, /* xRollback - rollback transaction */ echoFindFunction, /* xFindFunction - function overloading */ - echoRename /* xRename - rename the table */ + echoRename, /* xRename - rename the table */ + 0, /* xSavepoint */ + 0, /* xRelease */ + 0, /* xRollbackTo */ + 0, /* xShadowName */ + 0 /* xIntegrity */ }; static sqlite3_module echoModuleV2 = { @@ -1337,7 +1344,9 @@ static sqlite3_module echoModuleV2 = { echoRename, /* xRename - rename the table */ echoSavepoint, echoRelease, - echoRollbackTo + echoRollbackTo, + 0, /* xShadowName */ + 0 /* xIntegrity */ }; /* @@ -1347,13 +1356,16 @@ extern int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb); extern const char *sqlite3ErrName(int); static void moduleDestroy(void *p){ + EchoModule *pMod = (EchoModule*)p; + sqlite3_create_function(pMod->db, "function_that_does_not_exist_0982ma98", + SQLITE_ANY, 1, 0, 0, 0, 0); sqlite3_free(p); } /* ** Register the echo virtual table module. */ -static int register_echo_module( +static int SQLITE_TCLAPI register_echo_module( ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ @@ -1371,6 +1383,7 @@ static int register_echo_module( /* Virtual table module "echo" */ pMod = sqlite3_malloc(sizeof(EchoModule)); pMod->interp = interp; + pMod->db = db; rc = sqlite3_create_module_v2( db, "echo", &echoModule, (void*)pMod, moduleDestroy ); @@ -1379,6 +1392,7 @@ static int register_echo_module( if( rc==SQLITE_OK ){ pMod = sqlite3_malloc(sizeof(EchoModule)); pMod->interp = interp; + pMod->db = db; rc = sqlite3_create_module_v2(db, "echo_v2", &echoModuleV2, (void*)pMod, moduleDestroy ); @@ -1393,7 +1407,7 @@ static int register_echo_module( ** ** sqlite3_declare_vtab DB SQL */ -static int declare_vtab( +static int SQLITE_TCLAPI declare_vtab( ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ diff --git a/src/test9.c b/src/test9.c index e5993e8ff7..62b16795e3 100644 --- a/src/test9.c +++ b/src/test9.c @@ -15,14 +15,14 @@ ** as there is not much point in binding to Tcl. */ #include "sqliteInt.h" -#include "tcl.h" +#include "tclsqlite.h" #include <stdlib.h> #include <string.h> /* ** c_collation_test */ -static int c_collation_test( +static int SQLITE_TCLAPI c_collation_test( ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ @@ -56,14 +56,14 @@ static int c_collation_test( error_out: Tcl_ResetResult(interp); - Tcl_AppendResult(interp, "Error testing function: ", zErrFunction, 0); + Tcl_AppendResult(interp, "Error testing function: ", zErrFunction, NULL); return TCL_ERROR; } /* ** c_realloc_test */ -static int c_realloc_test( +static int SQLITE_TCLAPI c_realloc_test( ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ @@ -96,7 +96,7 @@ static int c_realloc_test( error_out: Tcl_ResetResult(interp); - Tcl_AppendResult(interp, "Error testing function: ", zErrFunction, 0); + Tcl_AppendResult(interp, "Error testing function: ", zErrFunction, NULL); return TCL_ERROR; } @@ -104,7 +104,7 @@ static int c_realloc_test( /* ** c_misuse_test */ -static int c_misuse_test( +static int SQLITE_TCLAPI c_misuse_test( ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ @@ -174,7 +174,7 @@ static int c_misuse_test( error_out: Tcl_ResetResult(interp); - Tcl_AppendResult(interp, "Error testing function: ", zErrFunction, 0); + Tcl_AppendResult(interp, "Error testing function: ", zErrFunction, NULL); return TCL_ERROR; } diff --git a/src/test_async.c b/src/test_async.c deleted file mode 100644 index b0b943185b..0000000000 --- a/src/test_async.c +++ /dev/null @@ -1,241 +0,0 @@ -/* -** 2005 December 14 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -************************************************************************* -** -** This file contains a binding of the asynchronous IO extension interface -** (defined in ext/async/sqlite3async.h) to Tcl. -*/ - -#define TCL_THREADS -#include <tcl.h> - -#ifdef SQLITE_ENABLE_ASYNCIO - -#include "sqlite3async.h" -#include "sqlite3.h" -#include <assert.h> - -/* From main.c */ -extern const char *sqlite3ErrName(int); - - -struct TestAsyncGlobal { - int isInstalled; /* True when async VFS is installed */ -} testasync_g = { 0 }; - -TCL_DECLARE_MUTEX(testasync_g_writerMutex); - -/* -** sqlite3async_initialize PARENT-VFS ISDEFAULT -*/ -static int testAsyncInit( - void * clientData, - Tcl_Interp *interp, - int objc, - Tcl_Obj *CONST objv[] -){ - const char *zParent; - int isDefault; - int rc; - - if( objc!=3 ){ - Tcl_WrongNumArgs(interp, 1, objv, "PARENT-VFS ISDEFAULT"); - return TCL_ERROR; - } - zParent = Tcl_GetString(objv[1]); - if( !*zParent ) { - zParent = 0; - } - if( Tcl_GetBooleanFromObj(interp, objv[2], &isDefault) ){ - return TCL_ERROR; - } - - rc = sqlite3async_initialize(zParent, isDefault); - if( rc!=SQLITE_OK ){ - Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1)); - return TCL_ERROR; - } - return TCL_OK; -} - -/* -** sqlite3async_shutdown -*/ -static int testAsyncShutdown( - void * clientData, - Tcl_Interp *interp, - int objc, - Tcl_Obj *CONST objv[] -){ - sqlite3async_shutdown(); - return TCL_OK; -} - -static Tcl_ThreadCreateType tclWriterThread(ClientData pIsStarted){ - Tcl_MutexLock(&testasync_g_writerMutex); - *((int *)pIsStarted) = 1; - sqlite3async_run(); - Tcl_MutexUnlock(&testasync_g_writerMutex); - Tcl_ExitThread(0); - TCL_THREAD_CREATE_RETURN; -} - -/* -** sqlite3async_start -** -** Start a new writer thread. -*/ -static int testAsyncStart( - void * clientData, - Tcl_Interp *interp, - int objc, - Tcl_Obj *CONST objv[] -){ - volatile int isStarted = 0; - ClientData threadData = (ClientData)&isStarted; - - Tcl_ThreadId x; - const int nStack = TCL_THREAD_STACK_DEFAULT; - const int flags = TCL_THREAD_NOFLAGS; - int rc; - - rc = Tcl_CreateThread(&x, tclWriterThread, threadData, nStack, flags); - if( rc!=TCL_OK ){ - Tcl_AppendResult(interp, "Tcl_CreateThread() failed", 0); - return TCL_ERROR; - } - - while( isStarted==0 ) { /* Busy loop */ } - return TCL_OK; -} - -/* -** sqlite3async_wait -** -** Wait for the current writer thread to terminate. -** -** If the current writer thread is set to run forever then this -** command would block forever. To prevent that, an error is returned. -*/ -static int testAsyncWait( - void * clientData, - Tcl_Interp *interp, - int objc, - Tcl_Obj *CONST objv[] -){ - int eCond; - if( objc!=1 ){ - Tcl_WrongNumArgs(interp, 1, objv, ""); - return TCL_ERROR; - } - - sqlite3async_control(SQLITEASYNC_GET_HALT, &eCond); - if( eCond==SQLITEASYNC_HALT_NEVER ){ - Tcl_AppendResult(interp, "would block forever", (char*)0); - return TCL_ERROR; - } - - Tcl_MutexLock(&testasync_g_writerMutex); - Tcl_MutexUnlock(&testasync_g_writerMutex); - return TCL_OK; -} - -/* -** sqlite3async_control OPTION ?VALUE? -*/ -static int testAsyncControl( - void * clientData, - Tcl_Interp *interp, - int objc, - Tcl_Obj *CONST objv[] -){ - int rc = SQLITE_OK; - int aeOpt[] = { SQLITEASYNC_HALT, SQLITEASYNC_DELAY, SQLITEASYNC_LOCKFILES }; - const char *azOpt[] = { "halt", "delay", "lockfiles", 0 }; - const char *az[] = { "never", "now", "idle", 0 }; - int iVal; - int eOpt; - - if( objc!=2 && objc!=3 ){ - Tcl_WrongNumArgs(interp, 1, objv, "OPTION ?VALUE?"); - return TCL_ERROR; - } - if( Tcl_GetIndexFromObj(interp, objv[1], azOpt, "option", 0, &eOpt) ){ - return TCL_ERROR; - } - eOpt = aeOpt[eOpt]; - - if( objc==3 ){ - switch( eOpt ){ - case SQLITEASYNC_HALT: { - assert( SQLITEASYNC_HALT_NEVER==0 ); - assert( SQLITEASYNC_HALT_NOW==1 ); - assert( SQLITEASYNC_HALT_IDLE==2 ); - if( Tcl_GetIndexFromObj(interp, objv[2], az, "value", 0, &iVal) ){ - return TCL_ERROR; - } - break; - } - case SQLITEASYNC_DELAY: - if( Tcl_GetIntFromObj(interp, objv[2], &iVal) ){ - return TCL_ERROR; - } - break; - - case SQLITEASYNC_LOCKFILES: - if( Tcl_GetBooleanFromObj(interp, objv[2], &iVal) ){ - return TCL_ERROR; - } - break; - } - - rc = sqlite3async_control(eOpt, iVal); - } - - if( rc==SQLITE_OK ){ - rc = sqlite3async_control( - eOpt==SQLITEASYNC_HALT ? SQLITEASYNC_GET_HALT : - eOpt==SQLITEASYNC_DELAY ? SQLITEASYNC_GET_DELAY : - SQLITEASYNC_GET_LOCKFILES, &iVal); - } - - if( rc!=SQLITE_OK ){ - Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1)); - return TCL_ERROR; - } - - if( eOpt==SQLITEASYNC_HALT ){ - Tcl_SetObjResult(interp, Tcl_NewStringObj(az[iVal], -1)); - }else{ - Tcl_SetObjResult(interp, Tcl_NewIntObj(iVal)); - } - - return TCL_OK; -} - -#endif /* SQLITE_ENABLE_ASYNCIO */ - -/* -** This routine registers the custom TCL commands defined in this -** module. This should be the only procedure visible from outside -** of this module. -*/ -int Sqlitetestasync_Init(Tcl_Interp *interp){ -#ifdef SQLITE_ENABLE_ASYNCIO - Tcl_CreateObjCommand(interp,"sqlite3async_start",testAsyncStart,0,0); - Tcl_CreateObjCommand(interp,"sqlite3async_wait",testAsyncWait,0,0); - - Tcl_CreateObjCommand(interp,"sqlite3async_control",testAsyncControl,0,0); - Tcl_CreateObjCommand(interp,"sqlite3async_initialize",testAsyncInit,0,0); - Tcl_CreateObjCommand(interp,"sqlite3async_shutdown",testAsyncShutdown,0,0); -#endif /* SQLITE_ENABLE_ASYNCIO */ - return TCL_OK; -} diff --git a/src/test_autoext.c b/src/test_autoext.c index a5236d2390..74ca55879f 100644 --- a/src/test_autoext.c +++ b/src/test_autoext.c @@ -11,7 +11,7 @@ ************************************************************************* ** Test extension for testing the sqlite3_auto_extension() function. */ -#include "tcl.h" +#include "tclsqlite.h" #include "sqlite3ext.h" #ifndef SQLITE_OMIT_LOAD_EXTENSION @@ -87,13 +87,13 @@ static int broken_init( ** ** Register the "sqr" extension to be loaded automatically. */ -static int autoExtSqrObjCmd( +static int SQLITE_TCLAPI autoExtSqrObjCmd( void * clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[] ){ - int rc = sqlite3_auto_extension((void*)sqr_init); + int rc = sqlite3_auto_extension((void(*)(void))sqr_init); Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); return SQLITE_OK; } @@ -103,13 +103,13 @@ static int autoExtSqrObjCmd( ** ** Unregister the "sqr" extension. */ -static int cancelAutoExtSqrObjCmd( +static int SQLITE_TCLAPI cancelAutoExtSqrObjCmd( void * clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[] ){ - int rc = sqlite3_cancel_auto_extension((void*)sqr_init); + int rc = sqlite3_cancel_auto_extension((void(*)(void))sqr_init); Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); return SQLITE_OK; } @@ -119,13 +119,13 @@ static int cancelAutoExtSqrObjCmd( ** ** Register the "cube" extension to be loaded automatically. */ -static int autoExtCubeObjCmd( +static int SQLITE_TCLAPI autoExtCubeObjCmd( void * clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[] ){ - int rc = sqlite3_auto_extension((void*)cube_init); + int rc = sqlite3_auto_extension((void(*)(void))cube_init); Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); return SQLITE_OK; } @@ -135,13 +135,13 @@ static int autoExtCubeObjCmd( ** ** Unregister the "cube" extension. */ -static int cancelAutoExtCubeObjCmd( +static int SQLITE_TCLAPI cancelAutoExtCubeObjCmd( void * clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[] ){ - int rc = sqlite3_cancel_auto_extension((void*)cube_init); + int rc = sqlite3_cancel_auto_extension((void(*)(void))cube_init); Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); return SQLITE_OK; } @@ -151,13 +151,13 @@ static int cancelAutoExtCubeObjCmd( ** ** Register the broken extension to be loaded automatically. */ -static int autoExtBrokenObjCmd( +static int SQLITE_TCLAPI autoExtBrokenObjCmd( void * clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[] ){ - int rc = sqlite3_auto_extension((void*)broken_init); + int rc = sqlite3_auto_extension((void(*)(void))broken_init); Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); return SQLITE_OK; } @@ -167,13 +167,13 @@ static int autoExtBrokenObjCmd( ** ** Unregister the broken extension. */ -static int cancelAutoExtBrokenObjCmd( +static int SQLITE_TCLAPI cancelAutoExtBrokenObjCmd( void * clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[] ){ - int rc = sqlite3_cancel_auto_extension((void*)broken_init); + int rc = sqlite3_cancel_auto_extension((void(*)(void))broken_init); Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); return SQLITE_OK; } @@ -186,7 +186,7 @@ static int cancelAutoExtBrokenObjCmd( ** ** Reset all auto-extensions */ -static int resetAutoExtObjCmd( +static int SQLITE_TCLAPI resetAutoExtObjCmd( void * clientData, Tcl_Interp *interp, int objc, diff --git a/src/test_backup.c b/src/test_backup.c index 6b4d6b9b1c..ae2348ebc2 100644 --- a/src/test_backup.c +++ b/src/test_backup.c @@ -13,7 +13,7 @@ ** */ -#include "tcl.h" +#include "tclsqlite.h" #include "sqlite3.h" #include <assert.h> @@ -23,7 +23,7 @@ extern const char *sqlite3ErrName(int); /* These functions are implemented in test1.c. */ extern int getDbPointer(Tcl_Interp *, const char *, sqlite3 **); -static int backupTestCmd( +static int SQLITE_TCLAPI backupTestCmd( ClientData clientData, Tcl_Interp *interp, int objc, @@ -98,7 +98,7 @@ static int backupTestCmd( return TCL_OK; } -static void backupTestFinish(ClientData clientData){ +static void SQLITE_TCLAPI backupTestFinish(ClientData clientData){ sqlite3_backup *pBackup = (sqlite3_backup *)clientData; sqlite3_backup_finish(pBackup); } @@ -107,7 +107,7 @@ static void backupTestFinish(ClientData clientData){ ** sqlite3_backup CMDNAME DESTHANDLE DESTNAME SRCHANDLE SRCNAME ** */ -static int backupTestInit( +static int SQLITE_TCLAPI backupTestInit( ClientData clientData, Tcl_Interp *interp, int objc, @@ -135,7 +135,7 @@ static int backupTestInit( pBackup = sqlite3_backup_init(pDestDb, zDestName, pSrcDb, zSrcName); if( !pBackup ){ - Tcl_AppendResult(interp, "sqlite3_backup_init() failed", 0); + Tcl_AppendResult(interp, "sqlite3_backup_init() failed", NULL); return TCL_ERROR; } diff --git a/src/test_bestindex.c b/src/test_bestindex.c new file mode 100644 index 0000000000..f6b5db0fbe --- /dev/null +++ b/src/test_bestindex.c @@ -0,0 +1,982 @@ +/* +** 2016-03-01 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** Code for testing the virtual table xBestIndex method and the query +** planner. +*/ + + +/* +** INSTRUCTIONS +** +** This module exports a single tcl command - [register_tcl_module]. When +** invoked, it registers a special virtual table module with a database +** connection. +** +** The virtual table is currently read-only. And always returns zero rows. +** It is created with a single argument - the name of a Tcl command - as +** follows: +** +** CREATE VIRTUAL TABLE x1 USING tcl(tcl_command); +** +** The command [tcl_command] is invoked when the table is first created (or +** connected), when the xBestIndex() method is invoked and when the xFilter() +** method is called. When it is created (or connected), it is invoked as +** follows: +** +** tcl_command xConnect +** +** In this case the return value of the script is passed to the +** sqlite3_declare_vtab() function to create the virtual table schema. +** +** When the xBestIndex() method is called by SQLite, the Tcl command is +** invoked as: +** +** tcl_command xBestIndex CONSTRAINTS ORDERBY MASK +** +** where CONSTRAINTS is a tcl representation of the aConstraints[] array, +** ORDERBY is a representation of the contents of the aOrderBy[] array and +** MASK is a copy of sqlite3_index_info.colUsed. For example if the virtual +** table is declared as: +** +** CREATE TABLE x1(a, b, c) +** +** and the query is: +** +** SELECT * FROM x1 WHERE a=? AND c<? ORDER BY b, c; +** +** then the Tcl command is: +** +** tcl_command xBestIndex \ +** {{op eq column 0 usable 1} {op lt column 2 usable 1}} \ +** {{column 1 desc 0} {column 2 desc 0}} \ +** 7 +** +** The return value of the script is a list of key-value pairs used to +** populate the output fields of the sqlite3_index_info structure. Possible +** keys and the usage of the accompanying values are: +** +** "orderby" (value of orderByConsumed flag) +** "cost" (value of estimatedCost field) +** "rows" (value of estimatedRows field) +** "use" (index of used constraint in aConstraint[]) +** "omit" (like "use", but also sets omit flag) +** "idxnum" (value of idxNum field) +** "idxstr" (value of idxStr field) +** +** Refer to code below for further details. +** +** When SQLite calls the xFilter() method, this module invokes the following +** Tcl script: +** +** tcl_command xFilter IDXNUM IDXSTR ARGLIST +** +** IDXNUM and IDXSTR are the values of the idxNum and idxStr parameters +** passed to xFilter. ARGLIST is a Tcl list containing each of the arguments +** passed to xFilter in text form. +** +** As with xBestIndex(), the return value of the script is interpreted as a +** list of key-value pairs. There is currently only one key defined - "sql". +** The value must be the full text of an SQL statement that returns the data +** for the current scan. The leftmost column returned by the SELECT is assumed +** to contain the rowid. Other columns must follow, in order from left to +** right. +*/ + + +#include "sqliteInt.h" +#include "tclsqlite.h" + +#ifndef SQLITE_OMIT_VIRTUALTABLE + + +typedef struct tcl_vtab tcl_vtab; +typedef struct tcl_cursor tcl_cursor; +typedef struct TestFindFunction TestFindFunction; +typedef struct TestVtabContext TestVtabContext; + +/* +** A fs virtual-table object +*/ +struct tcl_vtab { + sqlite3_vtab base; + Tcl_Interp *interp; + Tcl_Obj *pCmd; + TestFindFunction *pFindFunctionList; + sqlite3 *db; +}; + +/* A tcl cursor object */ +struct tcl_cursor { + sqlite3_vtab_cursor base; + sqlite3_stmt *pStmt; /* Read data from here */ +}; + +struct TestFindFunction { + tcl_vtab *pTab; + const char *zName; + TestFindFunction *pNext; +}; + +struct TestVtabContext { + Tcl_Interp *interp; + Tcl_Obj *pDefault; +}; + +/* +** Dequote string z in place. +*/ +static void tclDequote(char *z){ + char q = z[0]; + + /* Set stack variable q to the close-quote character */ + if( q=='[' || q=='\'' || q=='"' || q=='`' ){ + int iIn = 1; + int iOut = 0; + if( q=='[' ) q = ']'; + + while( ALWAYS(z[iIn]) ){ + if( z[iIn]==q ){ + if( z[iIn+1]!=q ){ + /* Character iIn was the close quote. */ + iIn++; + break; + }else{ + /* Character iIn and iIn+1 form an escaped quote character. Skip + ** the input cursor past both and copy a single quote character + ** to the output buffer. */ + iIn += 2; + z[iOut++] = q; + } + }else{ + z[iOut++] = z[iIn++]; + } + } + + z[iOut] = '\0'; + } +} + +/* +** This function is the implementation of both the xConnect and xCreate +** methods of the fs virtual table. +** +** The argv[] array contains the following: +** +** argv[0] -> module name ("fs") +** argv[1] -> database name +** argv[2] -> table name +** argv[...] -> other module argument fields. +*/ +static int tclConnect( + sqlite3 *db, + void *pAux, + int argc, const char *const*argv, + sqlite3_vtab **ppVtab, + char **pzErr +){ + TestVtabContext *pCtx = (TestVtabContext*)pAux; + Tcl_Interp *interp = pCtx->interp; + tcl_vtab *pTab = 0; + char *zCmd = 0; + Tcl_Obj *pScript = 0; + int rc = SQLITE_OK; + + if( argc!=4 && (argc!=3 || pCtx->pDefault==0) ){ + *pzErr = sqlite3_mprintf("wrong number of arguments"); + return SQLITE_ERROR; + } + + if( argc==4 ){ + zCmd = sqlite3_malloc64(strlen(argv[3])+1); + } + pTab = (tcl_vtab*)sqlite3_malloc64(sizeof(tcl_vtab)); + if( (zCmd || argc==3) && pTab ){ + memset(pTab, 0, sizeof(tcl_vtab)); + + if( zCmd ){ + memcpy(zCmd, argv[3], strlen(argv[3])+1); + tclDequote(zCmd); + pTab->pCmd = Tcl_NewStringObj(zCmd, -1); + }else{ + pTab->pCmd = Tcl_DuplicateObj(pCtx->pDefault); + } + + pTab->interp = interp; + pTab->db = db; + Tcl_IncrRefCount(pTab->pCmd); + + pScript = Tcl_DuplicateObj(pTab->pCmd); + Tcl_IncrRefCount(pScript); + Tcl_ListObjAppendElement(interp, pScript, Tcl_NewStringObj("xConnect", -1)); + + rc = Tcl_EvalObjEx(interp, pScript, TCL_EVAL_GLOBAL); + if( rc!=TCL_OK ){ + *pzErr = sqlite3_mprintf("%s", Tcl_GetStringResult(interp)); + if( sqlite3_stricmp(*pzErr, "database schema has changed")==0 ){ + rc = SQLITE_SCHEMA; + }else{ + rc = SQLITE_ERROR; + } + }else{ + rc = sqlite3_declare_vtab(db, Tcl_GetStringResult(interp)); + if( rc!=SQLITE_OK ){ + *pzErr = sqlite3_mprintf("declare_vtab: %s", sqlite3_errmsg(db)); + } + } + + if( rc!=SQLITE_OK ){ + sqlite3_free(pTab); + pTab = 0; + } + }else{ + rc = SQLITE_NOMEM; + } + + sqlite3_free(zCmd); + *ppVtab = pTab ? &pTab->base : 0; + return rc; +} + +/* The xDisconnect and xDestroy methods are also the same */ +static int tclDisconnect(sqlite3_vtab *pVtab){ + tcl_vtab *pTab = (tcl_vtab*)pVtab; + while( pTab->pFindFunctionList ){ + TestFindFunction *p = pTab->pFindFunctionList; + pTab->pFindFunctionList = p->pNext; + sqlite3_free(p); + } + Tcl_DecrRefCount(pTab->pCmd); + sqlite3_free(pTab); + return SQLITE_OK; +} + +/* +** Open a new tcl cursor. +*/ +static int tclOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){ + tcl_cursor *pCur; + pCur = sqlite3_malloc(sizeof(tcl_cursor)); + if( pCur==0 ) return SQLITE_NOMEM; + memset(pCur, 0, sizeof(tcl_cursor)); + *ppCursor = &pCur->base; + return SQLITE_OK; +} + +/* +** Close a tcl cursor. +*/ +static int tclClose(sqlite3_vtab_cursor *cur){ + tcl_cursor *pCur = (tcl_cursor *)cur; + if( pCur ){ + sqlite3_finalize(pCur->pStmt); + sqlite3_free(pCur); + } + return SQLITE_OK; +} + +static int tclNext(sqlite3_vtab_cursor *pVtabCursor){ + tcl_cursor *pCsr = (tcl_cursor*)pVtabCursor; + if( pCsr->pStmt ){ + tcl_vtab *pTab = (tcl_vtab*)(pVtabCursor->pVtab); + int rc = sqlite3_step(pCsr->pStmt); + if( rc!=SQLITE_ROW ){ + const char *zErr; + rc = sqlite3_finalize(pCsr->pStmt); + pCsr->pStmt = 0; + if( rc!=SQLITE_OK ){ + zErr = sqlite3_errmsg(pTab->db); + pTab->base.zErrMsg = sqlite3_mprintf("%s", zErr); + } + } + } + return SQLITE_OK; +} + +static int tclFilter( + sqlite3_vtab_cursor *pVtabCursor, + int idxNum, const char *idxStr, + int argc, sqlite3_value **argv +){ + tcl_cursor *pCsr = (tcl_cursor*)pVtabCursor; + tcl_vtab *pTab = (tcl_vtab*)(pVtabCursor->pVtab); + Tcl_Interp *interp = pTab->interp; + Tcl_Obj *pScript; + Tcl_Obj *pArg; + int ii; + int rc; + + pScript = Tcl_DuplicateObj(pTab->pCmd); + Tcl_IncrRefCount(pScript); + Tcl_ListObjAppendElement(interp, pScript, Tcl_NewStringObj("xFilter", -1)); + Tcl_ListObjAppendElement(interp, pScript, Tcl_NewIntObj(idxNum)); + Tcl_ListObjAppendElement( + interp, pScript, Tcl_NewStringObj(idxStr ? idxStr : "", -1) + ); + + pArg = Tcl_NewObj(); + Tcl_IncrRefCount(pArg); + for(ii=0; ii<argc; ii++){ + const char *zVal = (const char*)sqlite3_value_text(argv[ii]); + Tcl_Obj *pVal; + if( zVal==0 ){ + sqlite3_value *pMem; + pVal = Tcl_NewObj(); + for(rc=sqlite3_vtab_in_first(argv[ii], &pMem); + rc==SQLITE_OK && pMem; + rc=sqlite3_vtab_in_next(argv[ii], &pMem) + ){ + Tcl_Obj *pVal2 = 0; + zVal = (const char*)sqlite3_value_text(pMem); + if( zVal ){ + pVal2 = Tcl_NewStringObj(zVal, -1); + }else{ + pVal2 = Tcl_NewObj(); + } + Tcl_ListObjAppendElement(interp, pVal, pVal2); + } + }else{ + pVal = Tcl_NewStringObj(zVal, -1); + } + Tcl_ListObjAppendElement(interp, pArg, pVal); + } + Tcl_ListObjAppendElement(interp, pScript, pArg); + Tcl_DecrRefCount(pArg); + + rc = Tcl_EvalObjEx(interp, pScript, TCL_EVAL_GLOBAL); + if( rc!=TCL_OK ){ + const char *zErr = Tcl_GetStringResult(interp); + rc = SQLITE_ERROR; + pTab->base.zErrMsg = sqlite3_mprintf("%s", zErr); + }else{ + /* Analyze the scripts return value. The return value should be a tcl + ** list object with an even number of elements. The first element of each + ** pair must be one of: + ** + ** "sql" (SQL statement to return data) + */ + Tcl_Obj *pRes = Tcl_GetObjResult(interp); + Tcl_Obj **apElem = 0; + Tcl_Size nElem; + rc = Tcl_ListObjGetElements(interp, pRes, &nElem, &apElem); + if( rc!=TCL_OK ){ + const char *zErr = Tcl_GetStringResult(interp); + rc = SQLITE_ERROR; + pTab->base.zErrMsg = sqlite3_mprintf("%s", zErr); + }else{ + for(ii=0; rc==SQLITE_OK && ii<(int)nElem; ii+=2){ + const char *zCmd = Tcl_GetString(apElem[ii]); + Tcl_Obj *p = apElem[ii+1]; + if( sqlite3_stricmp("sql", zCmd)==0 ){ + const char *zSql = Tcl_GetString(p); + rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pStmt, 0); + if( rc!=SQLITE_OK ){ + const char *zErr = sqlite3_errmsg(pTab->db); + pTab->base.zErrMsg = sqlite3_mprintf("unexpected: %s", zErr); + } + }else{ + rc = SQLITE_ERROR; + pTab->base.zErrMsg = sqlite3_mprintf("unexpected: %s", zCmd); + } + } + } + } + + if( rc==SQLITE_OK ){ + rc = tclNext(pVtabCursor); + } + return rc; +} + +static int tclColumn( + sqlite3_vtab_cursor *pVtabCursor, + sqlite3_context *ctx, + int i +){ + tcl_cursor *pCsr = (tcl_cursor*)pVtabCursor; + sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pStmt, i+1)); + return SQLITE_OK; +} + +static int tclRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *pRowid){ + tcl_cursor *pCsr = (tcl_cursor*)pVtabCursor; + *pRowid = sqlite3_column_int64(pCsr->pStmt, 0); + return SQLITE_OK; +} + +static int tclEof(sqlite3_vtab_cursor *pVtabCursor){ + tcl_cursor *pCsr = (tcl_cursor*)pVtabCursor; + return (pCsr->pStmt==0); +} + +static void testBestIndexObjConstraints( + Tcl_Interp *interp, + sqlite3_index_info *pIdxInfo +){ + int ii; + Tcl_Obj *pRes = Tcl_NewObj(); + Tcl_IncrRefCount(pRes); + for(ii=0; ii<pIdxInfo->nConstraint; ii++){ + struct sqlite3_index_constraint const *pCons = &pIdxInfo->aConstraint[ii]; + Tcl_Obj *pElem = Tcl_NewObj(); + const char *zOp = 0; + + Tcl_IncrRefCount(pElem); + + switch( pCons->op ){ + case SQLITE_INDEX_CONSTRAINT_EQ: + zOp = "eq"; break; + case SQLITE_INDEX_CONSTRAINT_GT: + zOp = "gt"; break; + case SQLITE_INDEX_CONSTRAINT_LE: + zOp = "le"; break; + case SQLITE_INDEX_CONSTRAINT_LT: + zOp = "lt"; break; + case SQLITE_INDEX_CONSTRAINT_GE: + zOp = "ge"; break; + case SQLITE_INDEX_CONSTRAINT_MATCH: + zOp = "match"; break; + case SQLITE_INDEX_CONSTRAINT_LIKE: + zOp = "like"; break; + case SQLITE_INDEX_CONSTRAINT_GLOB: + zOp = "glob"; break; + case SQLITE_INDEX_CONSTRAINT_REGEXP: + zOp = "regexp"; break; + case SQLITE_INDEX_CONSTRAINT_NE: + zOp = "ne"; break; + case SQLITE_INDEX_CONSTRAINT_ISNOT: + zOp = "isnot"; break; + case SQLITE_INDEX_CONSTRAINT_ISNOTNULL: + zOp = "isnotnull"; break; + case SQLITE_INDEX_CONSTRAINT_ISNULL: + zOp = "isnull"; break; + case SQLITE_INDEX_CONSTRAINT_IS: + zOp = "is"; break; + case SQLITE_INDEX_CONSTRAINT_LIMIT: + zOp = "limit"; break; + case SQLITE_INDEX_CONSTRAINT_OFFSET: + zOp = "offset"; break; + } + + Tcl_ListObjAppendElement(0, pElem, Tcl_NewStringObj("op", -1)); + if( zOp ){ + Tcl_ListObjAppendElement(0, pElem, Tcl_NewStringObj(zOp, -1)); + }else{ + Tcl_ListObjAppendElement(0, pElem, Tcl_NewIntObj(pCons->op)); + } + Tcl_ListObjAppendElement(0, pElem, Tcl_NewStringObj("column", -1)); + Tcl_ListObjAppendElement(0, pElem, Tcl_NewIntObj(pCons->iColumn)); + Tcl_ListObjAppendElement(0, pElem, Tcl_NewStringObj("usable", -1)); + Tcl_ListObjAppendElement(0, pElem, Tcl_NewIntObj(pCons->usable)); + + Tcl_ListObjAppendElement(0, pRes, pElem); + Tcl_DecrRefCount(pElem); + } + + Tcl_SetObjResult(interp, pRes); + Tcl_DecrRefCount(pRes); +} + +static void testBestIndexObjOrderby( + Tcl_Interp *interp, + sqlite3_index_info *pIdxInfo +){ + int ii; + Tcl_Obj *pRes = Tcl_NewObj(); + Tcl_IncrRefCount(pRes); + for(ii=0; ii<pIdxInfo->nOrderBy; ii++){ + struct sqlite3_index_orderby const *pOrder = &pIdxInfo->aOrderBy[ii]; + Tcl_Obj *pElem = Tcl_NewObj(); + Tcl_IncrRefCount(pElem); + + Tcl_ListObjAppendElement(0, pElem, Tcl_NewStringObj("column", -1)); + Tcl_ListObjAppendElement(0, pElem, Tcl_NewIntObj(pOrder->iColumn)); + Tcl_ListObjAppendElement(0, pElem, Tcl_NewStringObj("desc", -1)); + Tcl_ListObjAppendElement(0, pElem, Tcl_NewIntObj(pOrder->desc)); + + Tcl_ListObjAppendElement(0, pRes, pElem); + Tcl_DecrRefCount(pElem); + } + + Tcl_SetObjResult(interp, pRes); + Tcl_DecrRefCount(pRes); +} + +/* +** Implementation of the handle passed to each xBestIndex callback. This +** object features the following sub-commands: +** +** $hdl constraints +** $hdl orderby +** $hdl mask +** +** $hdl distinct +** Return the result (an integer) of calling sqlite3_vtab_distinct() +** on the index-info structure. +** +** $hdl in IDX BOOLEAN +** Wrapper around sqlite3_vtab_in(). Returns an integer. +** +** $hdl rhs_value IDX ?DEFAULT? +** Wrapper around sqlite3_vtab_rhs_value(). +*/ +static int SQLITE_TCLAPI testBestIndexObj( + ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ + Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ + int objc, /* Number of arguments */ + Tcl_Obj *CONST objv[] /* Command arguments */ +){ + const char *azSub[] = { + "constraints", /* 0 */ + "orderby", /* 1 */ + "mask", /* 2 */ + "distinct", /* 3 */ + "in", /* 4 */ + "rhs_value", /* 5 */ + "collation", /* 6 */ + 0 + }; + int ii; + sqlite3_index_info *pIdxInfo = (sqlite3_index_info*)clientData; + + if( objc<2 ){ + Tcl_WrongNumArgs(interp, 1, objv, "SUB-COMMAND"); + return TCL_ERROR; + } + if( Tcl_GetIndexFromObj(interp, objv[1], azSub, "sub-command", 0, &ii) ){ + return TCL_ERROR; + } + + if( ii<4 && objc!=2 ){ + Tcl_WrongNumArgs(interp, 2, objv, ""); + return TCL_ERROR; + } + if( ii==4 && objc!=4 ){ + Tcl_WrongNumArgs(interp, 2, objv, "INDEX BOOLEAN"); + return TCL_ERROR; + } + if( ii==5 && objc!=3 && objc!=4 ){ + Tcl_WrongNumArgs(interp, 2, objv, "INDEX ?DEFAULT?"); + return TCL_ERROR; + } + + switch( ii ){ + case 0: assert( sqlite3_stricmp(azSub[ii], "constraints")==0 ); + testBestIndexObjConstraints(interp, pIdxInfo); + break; + + case 1: assert( sqlite3_stricmp(azSub[ii], "orderby")==0 ); + testBestIndexObjOrderby(interp, pIdxInfo); + break; + + case 2: assert( sqlite3_stricmp(azSub[ii], "mask")==0 ); + Tcl_SetObjResult(interp, Tcl_NewWideIntObj(pIdxInfo->colUsed)); + break; + + case 3: assert( sqlite3_stricmp(azSub[ii], "distinct")==0 ); { + int bDistinct = sqlite3_vtab_distinct(pIdxInfo); + Tcl_SetObjResult(interp, Tcl_NewIntObj(bDistinct)); + break; + } + + case 4: assert( sqlite3_stricmp(azSub[ii], "in")==0 ); { + int iCons; + int bHandle; + if( Tcl_GetIntFromObj(interp, objv[2], &iCons) + || Tcl_GetBooleanFromObj(interp, objv[3], &bHandle) + ){ + return TCL_ERROR; + } + Tcl_SetObjResult(interp, + Tcl_NewIntObj(sqlite3_vtab_in(pIdxInfo, iCons, bHandle)) + ); + break; + } + + case 5: assert( sqlite3_stricmp(azSub[ii], "rhs_value")==0 ); { + int iCons = 0; + int rc; + sqlite3_value *pVal = 0; + const char *zVal = ""; + if( Tcl_GetIntFromObj(interp, objv[2], &iCons) ){ + return TCL_ERROR; + } + rc = sqlite3_vtab_rhs_value(pIdxInfo, iCons, &pVal); + if( rc!=SQLITE_OK && rc!=SQLITE_NOTFOUND ){ + Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE); + return TCL_ERROR; + } + if( pVal ){ + zVal = (const char*)sqlite3_value_text(pVal); + }else if( objc==4 ){ + zVal = Tcl_GetString(objv[3]); + } + Tcl_SetObjResult(interp, Tcl_NewStringObj(zVal, -1)); + break; + } + + case 6: assert( sqlite3_stricmp(azSub[ii], "collation")==0 ); { + int iCons = 0; + const char *zColl = ""; + if( Tcl_GetIntFromObj(interp, objv[2], &iCons) ){ + return TCL_ERROR; + } + zColl = sqlite3_vtab_collation(pIdxInfo, iCons); + Tcl_SetObjResult(interp, Tcl_NewStringObj(zColl, -1)); + break; + } + } + + return TCL_OK; +} + +static int tclBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){ + tcl_vtab *pTab = (tcl_vtab*)tab; + Tcl_Interp *interp = pTab->interp; + int rc = SQLITE_OK; + + static int iNext = 43; + char zHdl[24]; + Tcl_Obj *pScript; + + pScript = Tcl_DuplicateObj(pTab->pCmd); + Tcl_IncrRefCount(pScript); + Tcl_ListObjAppendElement(interp, pScript, Tcl_NewStringObj("xBestIndex", -1)); + + sqlite3_snprintf(sizeof(zHdl), zHdl, "bestindex%d", iNext++); + Tcl_CreateObjCommand(interp, zHdl, testBestIndexObj, pIdxInfo, 0); + Tcl_ListObjAppendElement(interp, pScript, Tcl_NewStringObj(zHdl, -1)); + rc = Tcl_EvalObjEx(interp, pScript, TCL_EVAL_GLOBAL); + Tcl_DeleteCommand(interp, zHdl); + Tcl_DecrRefCount(pScript); + + if( rc!=TCL_OK ){ + const char *zErr = Tcl_GetStringResult(interp); + rc = SQLITE_ERROR; + pTab->base.zErrMsg = sqlite3_mprintf("%s", zErr); + }else{ + /* Analyze the scripts return value. The return value should be a tcl + ** list object with an even number of elements. The first element of each + ** pair must be one of: + ** + ** "orderby" (value of orderByConsumed flag) + ** "cost" (value of estimatedCost field) + ** "rows" (value of estimatedRows field) + ** "use" (index of used constraint in aConstraint[]) + ** "idxnum" (value of idxNum field) + ** "idxstr" (value of idxStr field) + ** "omit" (index of omitted constraint in aConstraint[]) + */ + Tcl_Obj *pRes = Tcl_GetObjResult(interp); + Tcl_Obj **apElem = 0; + Tcl_Size nElem; + rc = Tcl_ListObjGetElements(interp, pRes, &nElem, &apElem); + if( rc!=TCL_OK ){ + const char *zErr = Tcl_GetStringResult(interp); + rc = SQLITE_ERROR; + pTab->base.zErrMsg = sqlite3_mprintf("%s", zErr); + }else{ + int ii; + int iArgv = 1; + for(ii=0; rc==SQLITE_OK && ii<(int)nElem; ii+=2){ + const char *zCmd = Tcl_GetString(apElem[ii]); + Tcl_Obj *p = apElem[ii+1]; + if( sqlite3_stricmp("cost", zCmd)==0 ){ + rc = Tcl_GetDoubleFromObj(interp, p, &pIdxInfo->estimatedCost); + }else + if( sqlite3_stricmp("orderby", zCmd)==0 ){ + rc = Tcl_GetIntFromObj(interp, p, &pIdxInfo->orderByConsumed); + }else + if( sqlite3_stricmp("idxnum", zCmd)==0 ){ + rc = Tcl_GetIntFromObj(interp, p, &pIdxInfo->idxNum); + }else + if( sqlite3_stricmp("idxstr", zCmd)==0 ){ + sqlite3_free(pIdxInfo->idxStr); + pIdxInfo->idxStr = sqlite3_mprintf("%s", Tcl_GetString(p)); + pIdxInfo->needToFreeIdxStr = 1; + }else + if( sqlite3_stricmp("rows", zCmd)==0 ){ + Tcl_WideInt x = 0; + rc = Tcl_GetWideIntFromObj(interp, p, &x); + pIdxInfo->estimatedRows = (tRowcnt)x; + }else + if( sqlite3_stricmp("use", zCmd)==0 + || sqlite3_stricmp("omit", zCmd)==0 + ){ + int iCons; + rc = Tcl_GetIntFromObj(interp, p, &iCons); + if( rc==SQLITE_OK ){ + if( iCons<0 || iCons>=pIdxInfo->nConstraint ){ + rc = SQLITE_ERROR; + pTab->base.zErrMsg = sqlite3_mprintf("unexpected: %d", iCons); + }else{ + int bOmit = (zCmd[0]=='o' || zCmd[0]=='O'); + pIdxInfo->aConstraintUsage[iCons].argvIndex = iArgv++; + pIdxInfo->aConstraintUsage[iCons].omit = bOmit; + } + } + }else + if( sqlite3_stricmp("constraint", zCmd)==0 ){ + rc = SQLITE_CONSTRAINT; + pTab->base.zErrMsg = sqlite3_mprintf("%s", Tcl_GetString(p)); + }else{ + rc = SQLITE_ERROR; + pTab->base.zErrMsg = sqlite3_mprintf("unexpected: %s", zCmd); + } + if( rc!=SQLITE_OK && pTab->base.zErrMsg==0 ){ + const char *zErr = Tcl_GetStringResult(interp); + pTab->base.zErrMsg = sqlite3_mprintf("%s", zErr); + } + } + } + } + + return rc; +} + +static void tclFunction(sqlite3_context *pCtx, int nArg, sqlite3_value **apArg){ + TestFindFunction *p = (TestFindFunction*)sqlite3_user_data(pCtx); + Tcl_Interp *interp = p->pTab->interp; + Tcl_Obj *pScript = 0; + Tcl_Obj *pRet = 0; + int ii; + + pScript = Tcl_DuplicateObj(p->pTab->pCmd); + Tcl_IncrRefCount(pScript); + Tcl_ListObjAppendElement(interp, pScript, Tcl_NewStringObj("function", -1)); + Tcl_ListObjAppendElement(interp, pScript, Tcl_NewStringObj(p->zName, -1)); + + for(ii=0; ii<nArg; ii++){ + const char *zArg = (const char*)sqlite3_value_text(apArg[ii]); + Tcl_ListObjAppendElement(interp, pScript, + (zArg ? Tcl_NewStringObj(zArg, -1) : Tcl_NewObj()) + ); + } + Tcl_EvalObjEx(interp, pScript, TCL_EVAL_GLOBAL); + Tcl_DecrRefCount(pScript); + + pRet = Tcl_GetObjResult(interp); + sqlite3_result_text(pCtx, Tcl_GetString(pRet), -1, SQLITE_TRANSIENT); +} + +static int tclFindFunction( + sqlite3_vtab *tab, + int nArg, + const char *zName, + void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT */ + void **ppArg /* OUT */ +){ + int iRet = 0; + tcl_vtab *pTab = (tcl_vtab*)tab; + Tcl_Interp *interp = pTab->interp; + Tcl_Obj *pScript = 0; + int rc = SQLITE_OK; + + pScript = Tcl_DuplicateObj(pTab->pCmd); + Tcl_IncrRefCount(pScript); + Tcl_ListObjAppendElement( + interp, pScript, Tcl_NewStringObj("xFindFunction", -1) + ); + Tcl_ListObjAppendElement(interp, pScript, Tcl_NewIntObj(nArg)); + Tcl_ListObjAppendElement(interp, pScript, Tcl_NewStringObj(zName, -1)); + rc = Tcl_EvalObjEx(interp, pScript, TCL_EVAL_GLOBAL); + Tcl_DecrRefCount(pScript); + + if( rc==SQLITE_OK ){ + Tcl_Obj *pObj = Tcl_GetObjResult(interp); + + if( Tcl_GetIntFromObj(interp, pObj, &iRet) ){ + rc = SQLITE_ERROR; + }else if( iRet>0 ){ + sqlite3_int64 nName = strlen(zName); + sqlite3_int64 nByte = nName + 1 + sizeof(TestFindFunction); + TestFindFunction *pNew = 0; + + pNew = (TestFindFunction*)sqlite3_malloc64(nByte); + if( pNew==0 ){ + iRet = 0; + }else{ + memset(pNew, 0, nByte); + pNew->zName = (const char*)&pNew[1]; + memcpy((char*)pNew->zName, zName, nName); + pNew->pTab = pTab; + pNew->pNext = pTab->pFindFunctionList; + pTab->pFindFunctionList = pNew; + *ppArg = (void*)pNew; + *pxFunc = tclFunction; + } + } + } + + return iRet; +} + +static int tclUpdate( + sqlite3_vtab *tab, + int nArg, + sqlite3_value **apVal, + sqlite3_int64 *piRowid +){ + tcl_vtab *pTab = (tcl_vtab*)tab; + Tcl_Interp *interp = pTab->interp; + Tcl_Obj *pEval = Tcl_DuplicateObj(pTab->pCmd); + Tcl_Obj *pRes = 0; + int rc = TCL_OK; + + Tcl_IncrRefCount(pEval); + Tcl_ListObjAppendElement(interp, pEval, Tcl_NewStringObj("xUpdate",-1)); + + rc = Tcl_EvalObjEx(interp, pEval, TCL_EVAL_GLOBAL); + Tcl_DecrRefCount(pEval); + + if( rc==TCL_OK ){ + Tcl_Obj *pRes = Tcl_GetObjResult(interp); + Tcl_WideInt v; + rc = Tcl_GetWideIntFromObj(interp, pRes, &v); + *piRowid = (sqlite3_int64)v; + } + + if( rc!=TCL_OK ){ + tab->zErrMsg = sqlite3_mprintf("%s", Tcl_GetStringResult(pTab->interp)); + return rc; + } + + return SQLITE_OK; +} + +/* +** A virtual table module that provides read-only access to a +** Tcl global variable namespace. +*/ +static sqlite3_module tclModule = { + 0, /* iVersion */ + tclConnect, + tclConnect, + tclBestIndex, + tclDisconnect, + tclDisconnect, + tclOpen, /* xOpen - open a cursor */ + tclClose, /* xClose - close a cursor */ + tclFilter, /* xFilter - configure scan constraints */ + tclNext, /* xNext - advance a cursor */ + tclEof, /* xEof - check for end of scan */ + tclColumn, /* xColumn - read data */ + tclRowid, /* xRowid - read data */ + 0, /* xUpdate */ + 0, /* xBegin */ + 0, /* xSync */ + 0, /* xCommit */ + 0, /* xRollback */ + tclFindFunction, /* xFindFunction */ + 0, /* xRename */ + 0, /* xSavepoint */ + 0, /* xRelease */ + 0, /* xRollbackTo */ + 0, /* xShadowName */ + 0 /* xIntegrity */ +}; +static sqlite3_module tclModuleUpdate = { + 0, /* iVersion */ + tclConnect, + tclConnect, + tclBestIndex, + tclDisconnect, + tclDisconnect, + tclOpen, /* xOpen - open a cursor */ + tclClose, /* xClose - close a cursor */ + tclFilter, /* xFilter - configure scan constraints */ + tclNext, /* xNext - advance a cursor */ + tclEof, /* xEof - check for end of scan */ + tclColumn, /* xColumn - read data */ + tclRowid, /* xRowid - read data */ + tclUpdate, /* xUpdate */ + 0, /* xBegin */ + 0, /* xSync */ + 0, /* xCommit */ + 0, /* xRollback */ + tclFindFunction, /* xFindFunction */ + 0, /* xRename */ + 0, /* xSavepoint */ + 0, /* xRelease */ + 0, /* xRollbackTo */ + 0, /* xShadowName */ + 0 /* xIntegrity */ +}; + +/* +** Decode a pointer to an sqlite3 object. +*/ +extern int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb); + +static void delTestVtabCtx(void *p){ + TestVtabContext *pCtx = (TestVtabContext*)p; + if( pCtx->pDefault ){ + Tcl_DecrRefCount(pCtx->pDefault); + } + ckfree(pCtx); +} + +/* +** Register the echo virtual table module. +*/ +static int SQLITE_TCLAPI register_tcl_module( + ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ + Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ + int objc, /* Number of arguments */ + Tcl_Obj *CONST objv[] /* Command arguments */ +){ + sqlite3 *db; + if( objc!=2 && objc!=3 ){ + Tcl_WrongNumArgs(interp, 1, objv, "DB ?DEFAULT-CMD?"); + return TCL_ERROR; + } + if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; +#ifndef SQLITE_OMIT_VIRTUALTABLE + { + sqlite3_module *pMod = &tclModule; + TestVtabContext *pCtx = (TestVtabContext*)ckalloc(sizeof(TestVtabContext)); + pCtx->interp = interp; + pCtx->pDefault = 0; + if( objc==3 ){ + pCtx->pDefault = objv[2]; + Tcl_IncrRefCount(pCtx->pDefault); + } + + if( objc==3 ){ pMod = &tclModuleUpdate; } + sqlite3_create_module_v2(db, "tcl", pMod, (void*)pCtx, delTestVtabCtx); + } +#endif + return TCL_OK; +} + +#endif + + +/* +** Register commands with the TCL interpreter. +*/ +int Sqlitetesttcl_Init(Tcl_Interp *interp){ +#ifndef SQLITE_OMIT_VIRTUALTABLE + static struct { + char *zName; + Tcl_ObjCmdProc *xProc; + void *clientData; + } aObjCmd[] = { + { "register_tcl_module", register_tcl_module, 0 }, + }; + int i; + for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){ + Tcl_CreateObjCommand(interp, aObjCmd[i].zName, + aObjCmd[i].xProc, aObjCmd[i].clientData, 0); + } +#endif + return TCL_OK; +} diff --git a/src/test_blob.c b/src/test_blob.c index 4a7075a28a..ae5a734179 100644 --- a/src/test_blob.c +++ b/src/test_blob.c @@ -12,7 +12,7 @@ ** */ #include "sqliteInt.h" -#include "tcl.h" +#include "tclsqlite.h" #include <stdlib.h> #include <string.h> #include <assert.h> @@ -54,7 +54,7 @@ static int blobHandleFromObj( sqlite3_blob **ppBlob ){ char *z; - int n; + Tcl_Size n; z = Tcl_GetStringFromObj(pObj, &n); if( n==0 ){ @@ -84,7 +84,7 @@ static int blobHandleFromObj( ** NULL Pointer is returned. */ static char *blobStringFromObj(Tcl_Obj *pObj){ - int n; + Tcl_Size n; char *z; z = Tcl_GetStringFromObj(pObj, &n); return (n ? z : 0); @@ -95,7 +95,7 @@ static char *blobStringFromObj(Tcl_Obj *pObj){ ** ** Tcl test harness for the sqlite3_blob_open() function. */ -static int test_blob_open( +static int SQLITE_TCLAPI test_blob_open( ClientData clientData, /* Not used */ Tcl_Interp *interp, /* Calling TCL interpreter */ int objc, /* Number of arguments */ @@ -105,12 +105,12 @@ static int test_blob_open( const char *zDb; const char *zTable; const char *zColumn; - sqlite_int64 iRowid; + Tcl_WideInt iRowid; int flags; const char *zVarname; - int nVarname; + Tcl_Size nVarname; - sqlite3_blob *pBlob = (sqlite3_blob*)0xFFFFFFFF; + sqlite3_blob *pBlob = (sqlite3_blob*)&flags; /* Non-zero initialization */ int rc; if( objc!=8 ){ @@ -146,7 +146,7 @@ static int test_blob_open( /* ** sqlite3_blob_close HANDLE */ -static int test_blob_close( +static int SQLITE_TCLAPI test_blob_close( ClientData clientData, /* Not used */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ @@ -174,7 +174,7 @@ static int test_blob_close( /* ** sqlite3_blob_bytes HANDLE */ -static int test_blob_bytes( +static int SQLITE_TCLAPI test_blob_bytes( ClientData clientData, /* Not used */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ @@ -210,7 +210,7 @@ static int test_blob_bytes( ** text representation of the returned error code (i.e. "SQLITE_NOMEM") ** and a Tcl exception is thrown. */ -static int test_blob_read( +static int SQLITE_TCLAPI test_blob_read( ClientData clientData, /* Not used */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ @@ -235,7 +235,11 @@ static int test_blob_read( } if( nByte>0 ){ - zBuf = (unsigned char *)Tcl_Alloc(nByte); + zBuf = (unsigned char *)Tcl_AttemptAlloc(nByte); + if( zBuf==0 ){ + Tcl_AppendResult(interp, "out of memory in " __FILE__, NULL); + return TCL_ERROR; + } } rc = sqlite3_blob_read(pBlob, zBuf, nByte, iOffset); if( rc==SQLITE_OK ){ @@ -262,7 +266,7 @@ static int test_blob_read( ** result is set to the text representation of the returned error code ** (i.e. "SQLITE_NOMEM") and a Tcl exception is thrown. */ -static int test_blob_write( +static int SQLITE_TCLAPI test_blob_write( ClientData clientData, /* Not used */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ @@ -273,7 +277,8 @@ static int test_blob_write( int rc; unsigned char *zBuf; - int nBuf; + Tcl_Size nBuf; + int n; if( objc!=4 && objc!=5 ){ Tcl_WrongNumArgs(interp, 1, objv, "HANDLE OFFSET DATA ?NDATA?"); @@ -286,10 +291,11 @@ static int test_blob_write( } zBuf = Tcl_GetByteArrayFromObj(objv[3], &nBuf); - if( objc==5 && Tcl_GetIntFromObj(interp, objv[4], &nBuf) ){ + n = (int)(nBuf & 0x7fffffff); + if( objc==5 && Tcl_GetIntFromObj(interp, objv[4], &n) ){ return TCL_ERROR; } - rc = sqlite3_blob_write(pBlob, zBuf, nBuf, iOffset); + rc = sqlite3_blob_write(pBlob, zBuf, n, iOffset); if( rc!=SQLITE_OK ){ Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE); } diff --git a/src/test_btree.c b/src/test_btree.c index dfe77051b6..168a10f1f3 100644 --- a/src/test_btree.c +++ b/src/test_btree.c @@ -14,7 +14,7 @@ ** testing of the SQLite library. */ #include "btreeInt.h" -#include <tcl.h> +#include "tclsqlite.h" /* ** Usage: sqlite3_shared_cache_report @@ -22,7 +22,7 @@ ** Return a list of file that are shared and the number of ** references to each file. */ -int sqlite3BtreeSharedCacheReport( +int SQLITE_TCLAPI sqlite3BtreeSharedCacheReport( void * clientData, Tcl_Interp *interp, int objc, diff --git a/src/test_config.c b/src/test_config.c index 5db7117555..e66d15009c 100644 --- a/src/test_config.c +++ b/src/test_config.c @@ -24,7 +24,7 @@ # include "os_win.h" #endif -#include "tcl.h" +#include "tclsqlite.h" #include <stdlib.h> #include <string.h> @@ -55,12 +55,33 @@ static void set_options(Tcl_Interp *interp){ Tcl_SetVar2(interp, "sqlite_options", "rowid32", "0", TCL_GLOBAL_ONLY); #endif +#ifdef SQLITE_ALLOW_ROWID_IN_VIEW + Tcl_SetVar2( + interp, "sqlite_options", "allow_rowid_in_view", "1", TCL_GLOBAL_ONLY); +#else + Tcl_SetVar2( + interp, "sqlite_options", "allow_rowid_in_view", "0", TCL_GLOBAL_ONLY); +#endif + +#if defined(SQLITE_ENABLE_CARRAY) + Tcl_SetVar2(interp, "sqlite_options","carray","1",TCL_GLOBAL_ONLY); +#else + Tcl_SetVar2(interp, "sqlite_options","carray","0",TCL_GLOBAL_ONLY); +#endif + #ifdef SQLITE_CASE_SENSITIVE_LIKE Tcl_SetVar2(interp, "sqlite_options","casesensitivelike","1",TCL_GLOBAL_ONLY); #else Tcl_SetVar2(interp, "sqlite_options","casesensitivelike","0",TCL_GLOBAL_ONLY); #endif +#ifdef CONFIG_SLOWDOWN_FACTOR + Tcl_SetVar2(interp, "sqlite_options","configslower", + STRINGVALUE(CONFIG_SLOWDOWN_FACTOR),TCL_GLOBAL_ONLY); +#else + Tcl_SetVar2(interp, "sqlite_options","configslower","1.0",TCL_GLOBAL_ONLY); +#endif + #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT Tcl_SetVar2(interp, "sqlite_options", "curdir", "1", TCL_GLOBAL_ONLY); #else @@ -73,12 +94,25 @@ static void set_options(Tcl_Interp *interp){ Tcl_SetVar2(interp, "sqlite_options", "win32malloc", "0", TCL_GLOBAL_ONLY); #endif +#if defined(SQLITE_OS_WINRT) && SQLITE_OS_WINRT + Tcl_SetVar2(interp, "sqlite_options", "winrt", "1", TCL_GLOBAL_ONLY); +#else + Tcl_SetVar2(interp, "sqlite_options", "winrt", "0", TCL_GLOBAL_ONLY); +#endif + #ifdef SQLITE_DEBUG Tcl_SetVar2(interp, "sqlite_options", "debug", "1", TCL_GLOBAL_ONLY); #else Tcl_SetVar2(interp, "sqlite_options", "debug", "0", TCL_GLOBAL_ONLY); #endif +#ifdef SQLITE_DEFAULT_CKPTFULLFSYNC + Tcl_SetVar2(interp, "sqlite_options", "default_ckptfullfsync", + SQLITE_DEFAULT_CKPTFULLFSYNC ? "1" : "0", TCL_GLOBAL_ONLY); +#else + Tcl_SetVar2(interp, "sqlite_options", "default_ckptfullfsync", "0", TCL_GLOBAL_ONLY); +#endif + #ifdef SQLITE_DIRECT_OVERFLOW_READ Tcl_SetVar2(interp, "sqlite_options", "direct_read", "1", TCL_GLOBAL_ONLY); #else @@ -97,6 +131,12 @@ static void set_options(Tcl_Interp *interp){ Tcl_SetVar2(interp, "sqlite_options", "lfs", "1", TCL_GLOBAL_ONLY); #endif +#ifdef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS + Tcl_SetVar2(interp, "sqlite_options", "pagecache_overflow_stats","0",TCL_GLOBAL_ONLY); +#else + Tcl_SetVar2(interp, "sqlite_options", "pagecache_overflow_stats","1",TCL_GLOBAL_ONLY); +#endif + #if SQLITE_MAX_MMAP_SIZE>0 Tcl_SetVar2(interp, "sqlite_options", "mmap", "1", TCL_GLOBAL_ONLY); #else @@ -107,7 +147,7 @@ static void set_options(Tcl_Interp *interp){ STRINGVALUE(SQLITE_MAX_WORKER_THREADS), TCL_GLOBAL_ONLY ); -#if 1 /* def SQLITE_MEMDEBUG */ +#ifdef SQLITE_MEMDEBUG Tcl_SetVar2(interp, "sqlite_options", "memdebug", "1", TCL_GLOBAL_ONLY); #else Tcl_SetVar2(interp, "sqlite_options", "memdebug", "0", TCL_GLOBAL_ONLY); @@ -131,6 +171,18 @@ static void set_options(Tcl_Interp *interp){ Tcl_SetVar2(interp, "sqlite_options", "hiddencolumns", "0", TCL_GLOBAL_ONLY); #endif +#ifndef SQLITE_OMIT_DESERIALIZE + Tcl_SetVar2(interp, "sqlite_options", "deserialize", "1", TCL_GLOBAL_ONLY); +#else + Tcl_SetVar2(interp, "sqlite_options", "deserialize", "0", TCL_GLOBAL_ONLY); +#endif + +#ifdef SQLITE_ENABLE_MATH_FUNCTIONS + Tcl_SetVar2(interp, "sqlite_options", "mathlib", "1", TCL_GLOBAL_ONLY); +#else + Tcl_SetVar2(interp, "sqlite_options", "mathlib", "0", TCL_GLOBAL_ONLY); +#endif + #ifdef SQLITE_ENABLE_MEMSYS3 Tcl_SetVar2(interp, "sqlite_options", "mem3", "1", TCL_GLOBAL_ONLY); #else @@ -143,6 +195,26 @@ static void set_options(Tcl_Interp *interp){ Tcl_SetVar2(interp, "sqlite_options", "mem5", "0", TCL_GLOBAL_ONLY); #endif +#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC + Tcl_SetVar2(interp, "sqlite_options", "offset_sql_func","1",TCL_GLOBAL_ONLY); +#else + Tcl_SetVar2(interp, "sqlite_options", "offset_sql_func","0",TCL_GLOBAL_ONLY); +#endif + +#ifdef SQLITE_ENABLE_ORDERED_SET_AGGREGATES + Tcl_SetVar2(interp, "sqlite_options", + "ordered_set_aggregates","1",TCL_GLOBAL_ONLY); +#else + Tcl_SetVar2(interp, "sqlite_options", + "ordered_set_aggregates","0",TCL_GLOBAL_ONLY); +#endif + +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK + Tcl_SetVar2(interp, "sqlite_options", "preupdate", "1", TCL_GLOBAL_ONLY); +#else + Tcl_SetVar2(interp, "sqlite_options", "preupdate", "0", TCL_GLOBAL_ONLY); +#endif + #ifdef SQLITE_ENABLE_SNAPSHOT Tcl_SetVar2(interp, "sqlite_options", "snapshot", "1", TCL_GLOBAL_ONLY); #else @@ -185,12 +257,26 @@ static void set_options(Tcl_Interp *interp){ Tcl_SetVar2(interp, "sqlite_options", "atomicwrite", "0", TCL_GLOBAL_ONLY); #endif -#ifdef SQLITE_ENABLE_JSON1 +#ifdef SQLITE_ENABLE_GEOPOLY + Tcl_SetVar2(interp, "sqlite_options", "geopoly", "1", TCL_GLOBAL_ONLY); +#else + Tcl_SetVar2(interp, "sqlite_options", "geopoly", "0", TCL_GLOBAL_ONLY); +#endif + +#ifndef SQLITE_OMIT_JSON Tcl_SetVar2(interp, "sqlite_options", "json1", "1", TCL_GLOBAL_ONLY); #else Tcl_SetVar2(interp, "sqlite_options", "json1", "0", TCL_GLOBAL_ONLY); #endif +/* BEGIN SQLCIPHER */ +#ifdef SQLITE_HAS_CODEC + Tcl_SetVar2(interp, "sqlite_options", "has_codec", "1", TCL_GLOBAL_ONLY); +#else + Tcl_SetVar2(interp, "sqlite_options", "has_codec", "0", TCL_GLOBAL_ONLY); +#endif +/* END SQLCIPHER */ + #ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS Tcl_SetVar2(interp, "sqlite_options", "like_match_blobs", "0", TCL_GLOBAL_ONLY); #else @@ -245,7 +331,7 @@ static void set_options(Tcl_Interp *interp){ Tcl_SetVar2(interp, "sqlite_options", "between_opt", "1", TCL_GLOBAL_ONLY); #endif -#ifdef SQLITE_OMIT_BUILTIN_TEST +#ifdef SQLITE_UNTESTABLE Tcl_SetVar2(interp, "sqlite_options", "builtin_test", "0", TCL_GLOBAL_ONLY); #else Tcl_SetVar2(interp, "sqlite_options", "builtin_test", "1", TCL_GLOBAL_ONLY); @@ -281,6 +367,14 @@ static void set_options(Tcl_Interp *interp){ Tcl_SetVar2(interp, "sqlite_options", "columnmetadata", "0", TCL_GLOBAL_ONLY); #endif +#ifdef SQLITE_ENABLE_ORDEREDSETFUNC + Tcl_SetVar2(interp, "sqlite_options", "ordered_set_funcs", "1", + TCL_GLOBAL_ONLY); +#else + Tcl_SetVar2(interp, "sqlite_options", "ordered_set_funcs", "0", + TCL_GLOBAL_ONLY); +#endif + #ifdef SQLITE_ENABLE_OVERSIZE_CELL_CHECK Tcl_SetVar2(interp, "sqlite_options", "oversize_cell_check", "1", TCL_GLOBAL_ONLY); @@ -352,30 +446,12 @@ static void set_options(Tcl_Interp *interp){ Tcl_SetVar2(interp, "sqlite_options", "foreignkey", "1", TCL_GLOBAL_ONLY); #endif -#ifdef SQLITE_ENABLE_FTS1 - Tcl_SetVar2(interp, "sqlite_options", "fts1", "1", TCL_GLOBAL_ONLY); -#else - Tcl_SetVar2(interp, "sqlite_options", "fts1", "0", TCL_GLOBAL_ONLY); -#endif - -#ifdef SQLITE_ENABLE_FTS2 - Tcl_SetVar2(interp, "sqlite_options", "fts2", "1", TCL_GLOBAL_ONLY); -#else - Tcl_SetVar2(interp, "sqlite_options", "fts2", "0", TCL_GLOBAL_ONLY); -#endif - #ifdef SQLITE_ENABLE_FTS3 Tcl_SetVar2(interp, "sqlite_options", "fts3", "1", TCL_GLOBAL_ONLY); #else Tcl_SetVar2(interp, "sqlite_options", "fts3", "0", TCL_GLOBAL_ONLY); #endif -#ifdef SQLITE_ENABLE_FTS3_TOKENIZER - Tcl_SetVar2(interp, "sqlite_options", "fts3_tokenizer", "1", TCL_GLOBAL_ONLY); -#else - Tcl_SetVar2(interp, "sqlite_options", "fts3_tokenizer", "0", TCL_GLOBAL_ONLY); -#endif - #ifdef SQLITE_ENABLE_FTS5 Tcl_SetVar2(interp, "sqlite_options", "fts5", "1", TCL_GLOBAL_ONLY); #else @@ -406,6 +482,12 @@ static void set_options(Tcl_Interp *interp){ Tcl_SetVar2(interp, "sqlite_options", "icu", "0", TCL_GLOBAL_ONLY); #endif +#ifdef SQLITE_ENABLE_ICU_COLLATIONS + Tcl_SetVar2(interp, "sqlite_options", "icu_collations", "1", TCL_GLOBAL_ONLY); +#else + Tcl_SetVar2(interp, "sqlite_options", "icu_collations", "0", TCL_GLOBAL_ONLY); +#endif + #ifdef SQLITE_OMIT_INCRBLOB Tcl_SetVar2(interp, "sqlite_options", "incrblob", "0", TCL_GLOBAL_ONLY); #else @@ -448,10 +530,6 @@ static void set_options(Tcl_Interp *interp){ Tcl_SetVar2(interp, "sqlite_options", "lookaside", "1", TCL_GLOBAL_ONLY); #endif -Tcl_SetVar2(interp, "sqlite_options", "long_double", - sizeof(LONGDOUBLE_TYPE)>sizeof(double) ? "1" : "0", - TCL_GLOBAL_ONLY); - #ifdef SQLITE_OMIT_MEMORYDB Tcl_SetVar2(interp, "sqlite_options", "memorydb", "0", TCL_GLOBAL_ONLY); #else @@ -466,6 +544,12 @@ Tcl_SetVar2(interp, "sqlite_options", "long_double", Tcl_SetVar2(interp, "sqlite_options", "mergesort", "1", TCL_GLOBAL_ONLY); +#ifdef SQLITE_ENABLE_NULL_TRIM + Tcl_SetVar2(interp, "sqlite_options", "null_trim", "1", TCL_GLOBAL_ONLY); +#else + Tcl_SetVar2(interp, "sqlite_options", "null_trim", "0", TCL_GLOBAL_ONLY); +#endif + #ifdef SQLITE_OMIT_OR_OPTIMIZATION Tcl_SetVar2(interp, "sqlite_options", "or_opt", "0", TCL_GLOBAL_ONLY); #else @@ -527,15 +611,21 @@ Tcl_SetVar2(interp, "sqlite_options", "mergesort", "1", TCL_GLOBAL_ONLY); Tcl_SetVar2(interp, "sqlite_options", "schema_version", "1", TCL_GLOBAL_ONLY); #endif +#if defined(SQLITE_ENABLE_SESSION) && defined(SQLITE_ENABLE_PREUPDATE_HOOK) + Tcl_SetVar2(interp, "sqlite_options", "session", "1", TCL_GLOBAL_ONLY); +#else + Tcl_SetVar2(interp, "sqlite_options", "session", "0", TCL_GLOBAL_ONLY); +#endif + #ifdef SQLITE_ENABLE_STAT4 Tcl_SetVar2(interp, "sqlite_options", "stat4", "1", TCL_GLOBAL_ONLY); #else Tcl_SetVar2(interp, "sqlite_options", "stat4", "0", TCL_GLOBAL_ONLY); #endif -#if defined(SQLITE_ENABLE_STAT3) && !defined(SQLITE_ENABLE_STAT4) - Tcl_SetVar2(interp, "sqlite_options", "stat3", "1", TCL_GLOBAL_ONLY); +#if defined(SQLITE_ENABLE_STMTVTAB) && !defined(SQLITE_OMIT_VIRTUALTABLE) + Tcl_SetVar2(interp, "sqlite_options", "stmtvtab", "1", TCL_GLOBAL_ONLY); #else - Tcl_SetVar2(interp, "sqlite_options", "stat3", "0", TCL_GLOBAL_ONLY); + Tcl_SetVar2(interp, "sqlite_options", "stmtvtab", "0", TCL_GLOBAL_ONLY); #endif #ifdef SQLITE_ENABLE_STMT_SCANSTATUS @@ -582,7 +672,11 @@ Tcl_SetVar2(interp, "sqlite_options", "mergesort", "1", TCL_GLOBAL_ONLY); #endif Tcl_SetVar2(interp, "sqlite_options", "threadsafe", - STRINGVALUE(SQLITE_THREADSAFE), TCL_GLOBAL_ONLY); + SQLITE_THREADSAFE ? "1" : "0", TCL_GLOBAL_ONLY); + Tcl_SetVar2(interp, "sqlite_options", "threadsafe1", + SQLITE_THREADSAFE==1 ? "1" : "0", TCL_GLOBAL_ONLY); + Tcl_SetVar2(interp, "sqlite_options", "threadsafe2", + SQLITE_THREADSAFE==2 ? "1" : "0", TCL_GLOBAL_ONLY); assert( sqlite3_threadsafe()==SQLITE_THREADSAFE ); #ifdef SQLITE_OMIT_TEMPDB @@ -657,16 +751,16 @@ Tcl_SetVar2(interp, "sqlite_options", "mergesort", "1", TCL_GLOBAL_ONLY); Tcl_SetVar2(interp, "sqlite_options", "unlock_notify", "0", TCL_GLOBAL_ONLY); #endif -#ifdef SQLITE_SECURE_DELETE - Tcl_SetVar2(interp, "sqlite_options", "secure_delete", "1", TCL_GLOBAL_ONLY); +#ifdef SQLITE_FAST_SECURE_DELETE + Tcl_SetVar2(interp, "sqlite_options", "fast_secure_delete", "1", TCL_GLOBAL_ONLY); #else - Tcl_SetVar2(interp, "sqlite_options", "secure_delete", "0", TCL_GLOBAL_ONLY); + Tcl_SetVar2(interp, "sqlite_options", "fast_secure_delete", "0", TCL_GLOBAL_ONLY); #endif -#ifdef SQLITE_USER_AUTHENTICATION - Tcl_SetVar2(interp, "sqlite_options", "userauth", "1", TCL_GLOBAL_ONLY); +#ifdef SQLITE_SECURE_DELETE + Tcl_SetVar2(interp, "sqlite_options", "secure_delete", "1", TCL_GLOBAL_ONLY); #else - Tcl_SetVar2(interp, "sqlite_options", "userauth", "0", TCL_GLOBAL_ONLY); + Tcl_SetVar2(interp, "sqlite_options", "secure_delete", "0", TCL_GLOBAL_ONLY); #endif #ifdef SQLITE_MULTIPLEX_EXT_OVWR @@ -687,6 +781,31 @@ Tcl_SetVar2(interp, "sqlite_options", "mergesort", "1", TCL_GLOBAL_ONLY); Tcl_SetVar2(interp, "sqlite_options", "sqllog", "0", TCL_GLOBAL_ONLY); #endif +#ifdef SQLITE_ENABLE_URI_00_ERROR + Tcl_SetVar2(interp, "sqlite_options", "uri_00_error", "1", TCL_GLOBAL_ONLY); +#else + Tcl_SetVar2(interp, "sqlite_options", "uri_00_error", "0", TCL_GLOBAL_ONLY); +#endif + +#if defined(SQLITE_ENABLE_NORMALIZE) + Tcl_SetVar2(interp, "sqlite_options", "normalize", "1", TCL_GLOBAL_ONLY); +#else + Tcl_SetVar2(interp, "sqlite_options", "normalize", "0", TCL_GLOBAL_ONLY); +#endif + +#ifdef SQLITE_OMIT_WINDOWFUNC + Tcl_SetVar2(interp, "sqlite_options", "windowfunc", "0", TCL_GLOBAL_ONLY); +#else + Tcl_SetVar2(interp, "sqlite_options", "windowfunc", "1", TCL_GLOBAL_ONLY); +#endif + +#if !defined(SQLITE_ENABLE_SETLK_TIMEOUT) + Tcl_SetVar2(interp, "sqlite_options", "setlk_timeout", "0", TCL_GLOBAL_ONLY); +#else + Tcl_SetVar2(interp, "sqlite_options", "setlk_timeout", + STRINGVALUE(SQLITE_ENABLE_SETLK_TIMEOUT), TCL_GLOBAL_ONLY); +#endif + #define LINKVAR(x) { \ static const int cv_ ## x = SQLITE_ ## x; \ Tcl_LinkVar(interp, "SQLITE_" #x, (char *)&(cv_ ## x), \ @@ -707,6 +826,8 @@ Tcl_SetVar2(interp, "sqlite_options", "mergesort", "1", TCL_GLOBAL_ONLY); LINKVAR( DEFAULT_CACHE_SIZE ); LINKVAR( DEFAULT_PAGE_SIZE ); LINKVAR( DEFAULT_FILE_FORMAT ); + LINKVAR( DEFAULT_SYNCHRONOUS ); + LINKVAR( DEFAULT_WAL_SYNCHRONOUS ); LINKVAR( MAX_ATTACHED ); LINKVAR( MAX_DEFAULT_PAGE_SIZE ); LINKVAR( MAX_WORKER_THREADS ); diff --git a/src/test_delete.c b/src/test_delete.c new file mode 100644 index 0000000000..3f34a72c46 --- /dev/null +++ b/src/test_delete.c @@ -0,0 +1,156 @@ +/* +** 2016 September 10 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains test code to delete an SQLite database and all +** of its associated files. Associated files include: +** +** * The journal file. +** * The wal file. +** * The SQLITE_ENABLE_8_3_NAMES version of the db, journal or wal files. +** * Files created by the test_multiplex.c module to extend any of the +** above. +*/ + +#ifndef SQLITE_OS_WIN +# include <unistd.h> +# include <errno.h> +#endif +#include <string.h> +#include <assert.h> +#include "sqlite3.h" + +/* The following #defines are copied from test_multiplex.c */ +#ifndef MX_CHUNK_NUMBER +# define MX_CHUNK_NUMBER 299 +#endif +#ifndef SQLITE_MULTIPLEX_JOURNAL_8_3_OFFSET +# define SQLITE_MULTIPLEX_JOURNAL_8_3_OFFSET 400 +#endif +#ifndef SQLITE_MULTIPLEX_WAL_8_3_OFFSET +# define SQLITE_MULTIPLEX_WAL_8_3_OFFSET 700 +#endif + +/* +** This routine is a copy of (most of) the code from SQLite function +** sqlite3FileSuffix3(). It modifies the filename in buffer z in the +** same way as SQLite does when in 8.3 filenames mode. +*/ +static void sqlite3Delete83Name(char *z){ + int i, sz; + sz = (int)strlen(z); + for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){} + if( z[i]=='.' && (sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4); +} + +/* +** zFile is a filename. Assuming no error occurs, if this file exists, +** set *pbExists to true and unlink it. Or, if the file does not exist, +** set *pbExists to false before returning. +** +** If an error occurs, non-zero is returned. Or, if no error occurs, zero. +*/ +static int sqlite3DeleteUnlinkIfExists( + sqlite3_vfs *pVfs, + const char *zFile, + int *pbExists +){ + int rc = SQLITE_ERROR; +#ifdef _WIN32 + if( pVfs ){ + if( pbExists ) *pbExists = 1; + rc = pVfs->xDelete(pVfs, zFile, 0); + if( rc==SQLITE_IOERR_DELETE_NOENT ){ + if( pbExists ) *pbExists = 0; + rc = SQLITE_OK; + } + } +#else + assert( pVfs==0 ); + rc = access(zFile, F_OK); + if( rc ){ + if( errno==ENOENT ){ + if( pbExists ) *pbExists = 0; + rc = SQLITE_OK; + } + }else{ + if( pbExists ) *pbExists = 1; + rc = unlink(zFile); + } +#endif + return rc; +} + +/* +** Delete the database file identified by the string argument passed to this +** function. The string must contain a filename, not an SQLite URI. +*/ +SQLITE_API int sqlite3_delete_database( + const char *zFile /* File to delete */ +){ + char *zBuf; /* Buffer to sprintf() filenames to */ + int nBuf; /* Size of buffer in bytes */ + int rc = 0; /* System error code */ + int i; /* Iterate through azFmt[] and aMFile[] */ + + const char *azFmt[] = { "%s", "%s-journal", "%s-wal", "%s-shm" }; + + struct MFile { + const char *zFmt; + int iOffset; + int b83; + } aMFile[] = { + { "%s%03d", 0, 0 }, + { "%s-journal%03d", 0, 0 }, + { "%s-wal%03d", 0, 0 }, + { "%s%03d", 0, 1 }, + { "%s-journal%03d", SQLITE_MULTIPLEX_JOURNAL_8_3_OFFSET, 1 }, + { "%s-wal%03d", SQLITE_MULTIPLEX_WAL_8_3_OFFSET, 1 }, + }; + +#ifdef _WIN32 + sqlite3_vfs *pVfs = sqlite3_vfs_find("win32"); +#else + sqlite3_vfs *pVfs = 0; +#endif + + /* Allocate a buffer large enough for any of the files that need to be + ** deleted. */ + nBuf = (int)strlen(zFile) + 100; + zBuf = (char*)sqlite3_malloc(nBuf); + if( zBuf==0 ) return SQLITE_NOMEM; + + /* Delete both the regular and 8.3 filenames versions of the database, + ** journal, wal and shm files. */ + for(i=0; rc==0 && i<sizeof(azFmt)/sizeof(azFmt[0]); i++){ + sqlite3_snprintf(nBuf, zBuf, azFmt[i], zFile); + rc = sqlite3DeleteUnlinkIfExists(pVfs, zBuf, 0); + if( rc==0 && i!=0 ){ + sqlite3Delete83Name(zBuf); + rc = sqlite3DeleteUnlinkIfExists(pVfs, zBuf, 0); + } + } + + /* Delete any multiplexor files */ + for(i=0; rc==0 && i<sizeof(aMFile)/sizeof(aMFile[0]); i++){ + struct MFile *p = &aMFile[i]; + int iChunk; + for(iChunk=1; iChunk<=MX_CHUNK_NUMBER; iChunk++){ + int bExists; + sqlite3_snprintf(nBuf, zBuf, p->zFmt, zFile, iChunk+p->iOffset); + if( p->b83 ) sqlite3Delete83Name(zBuf); + rc = sqlite3DeleteUnlinkIfExists(pVfs, zBuf, &bExists); + if( bExists==0 || rc!=0 ) break; + } + } + + sqlite3_free(zBuf); + return (rc ? SQLITE_ERROR : SQLITE_OK); +} diff --git a/src/test_demovfs.c b/src/test_demovfs.c index 9410a309a6..e92fd56138 100644 --- a/src/test_demovfs.c +++ b/src/test_demovfs.c @@ -240,6 +240,9 @@ static int demoRead( if( nRead==iAmt ){ return SQLITE_OK; }else if( nRead>=0 ){ + if( nRead<iAmt ){ + memset(&((char*)zBuf)[nRead], 0, iAmt-nRead); + } return SQLITE_IOERR_SHORT_READ; } @@ -369,7 +372,7 @@ static int demoCheckReservedLock(sqlite3_file *pFile, int *pResOut){ ** No xFileControl() verbs are implemented by this VFS. */ static int demoFileControl(sqlite3_file *pFile, int op, void *pArg){ - return SQLITE_OK; + return SQLITE_NOTFOUND; } /* @@ -458,22 +461,23 @@ static int demoDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){ if( rc==0 && dirSync ){ int dfd; /* File descriptor open on directory */ - int i; /* Iterator variable */ + char *zSlash; char zDir[MAXPATHNAME+1]; /* Name of directory containing file zPath */ /* Figure out the directory name from the path of the file deleted. */ sqlite3_snprintf(MAXPATHNAME, zDir, "%s", zPath); zDir[MAXPATHNAME] = '\0'; - for(i=strlen(zDir); i>1 && zDir[i]!='/'; i++); - zDir[i] = '\0'; - - /* Open a file-descriptor on the directory. Sync. Close. */ - dfd = open(zDir, O_RDONLY, 0); - if( dfd<0 ){ - rc = -1; - }else{ - rc = fsync(dfd); - close(dfd); + zSlash = strrchr(zDir,'/'); + if( zSlash ){ + /* Open a file-descriptor on the directory. Sync. Close. */ + zSlash[0] = 0; + dfd = open(zDir, O_RDONLY, 0); + if( dfd<0 ){ + rc = -1; + }else{ + rc = fsync(dfd); + close(dfd); + } } } return (rc==0 ? SQLITE_OK : SQLITE_IOERR_DELETE); @@ -641,10 +645,10 @@ sqlite3_vfs *sqlite3_demovfs(void){ #ifdef SQLITE_TEST -#include <tcl.h> +#include "tclsqlite.h" #if SQLITE_OS_UNIX -static int register_demovfs( +static int SQLITE_TCLAPI register_demovfs( ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ @@ -653,7 +657,7 @@ static int register_demovfs( sqlite3_vfs_register(sqlite3_demovfs(), 1); return TCL_OK; } -static int unregister_demovfs( +static int SQLITE_TCLAPI unregister_demovfs( ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ diff --git a/src/test_devsym.c b/src/test_devsym.c index 21f0f684d8..86b78f15a1 100644 --- a/src/test_devsym.c +++ b/src/test_devsym.c @@ -28,6 +28,7 @@ ** Name used to identify this VFS. */ #define DEVSYM_VFS_NAME "devsym" +#define WRITECRASH_NAME "writecrash" typedef struct devsym_file devsym_file; struct devsym_file { @@ -72,68 +73,21 @@ static int devsymRandomness(sqlite3_vfs*, int nByte, char *zOut); static int devsymSleep(sqlite3_vfs*, int microseconds); static int devsymCurrentTime(sqlite3_vfs*, double*); -static sqlite3_vfs devsym_vfs = { - 2, /* iVersion */ - sizeof(devsym_file), /* szOsFile */ - DEVSYM_MAX_PATHNAME, /* mxPathname */ - 0, /* pNext */ - DEVSYM_VFS_NAME, /* zName */ - 0, /* pAppData */ - devsymOpen, /* xOpen */ - devsymDelete, /* xDelete */ - devsymAccess, /* xAccess */ - devsymFullPathname, /* xFullPathname */ -#ifndef SQLITE_OMIT_LOAD_EXTENSION - devsymDlOpen, /* xDlOpen */ - devsymDlError, /* xDlError */ - devsymDlSym, /* xDlSym */ - devsymDlClose, /* xDlClose */ -#else - 0, /* xDlOpen */ - 0, /* xDlError */ - 0, /* xDlSym */ - 0, /* xDlClose */ -#endif /* SQLITE_OMIT_LOAD_EXTENSION */ - devsymRandomness, /* xRandomness */ - devsymSleep, /* xSleep */ - devsymCurrentTime, /* xCurrentTime */ - 0, /* xGetLastError */ - 0 /* xCurrentTimeInt64 */ -}; - -static sqlite3_io_methods devsym_io_methods = { - 2, /* iVersion */ - devsymClose, /* xClose */ - devsymRead, /* xRead */ - devsymWrite, /* xWrite */ - devsymTruncate, /* xTruncate */ - devsymSync, /* xSync */ - devsymFileSize, /* xFileSize */ - devsymLock, /* xLock */ - devsymUnlock, /* xUnlock */ - devsymCheckReservedLock, /* xCheckReservedLock */ - devsymFileControl, /* xFileControl */ - devsymSectorSize, /* xSectorSize */ - devsymDeviceCharacteristics, /* xDeviceCharacteristics */ - devsymShmMap, /* xShmMap */ - devsymShmLock, /* xShmLock */ - devsymShmBarrier, /* xShmBarrier */ - devsymShmUnmap /* xShmUnmap */ -}; - struct DevsymGlobal { sqlite3_vfs *pVfs; int iDeviceChar; int iSectorSize; + int nWriteCrash; }; -struct DevsymGlobal g = {0, 0, 512}; +struct DevsymGlobal g = {0, 0, 512, 0}; /* ** Close an devsym-file. */ static int devsymClose(sqlite3_file *pFile){ devsym_file *p = (devsym_file *)pFile; - return sqlite3OsClose(p->pReal); + sqlite3OsClose(p->pReal); + return SQLITE_OK; } /* @@ -233,11 +187,11 @@ static int devsymDeviceCharacteristics(sqlite3_file *pFile){ } /* -** Shared-memory methods are all pass-thrus. +** Shared-memory methods are all pass-throughs. */ static int devsymShmLock(sqlite3_file *pFile, int ofst, int n, int flags){ devsym_file *p = (devsym_file *)pFile; - return sqlite3OsShmLock(p->pReal, ofst, n, flags); + return p->pReal->pMethods->xShmLock(p->pReal, ofst, n, flags); } static int devsymShmMap( sqlite3_file *pFile, @@ -247,15 +201,15 @@ static int devsymShmMap( void volatile **pp ){ devsym_file *p = (devsym_file *)pFile; - return sqlite3OsShmMap(p->pReal, iRegion, szRegion, isWrite, pp); + return p->pReal->pMethods->xShmMap(p->pReal, iRegion, szRegion, isWrite, pp); } static void devsymShmBarrier(sqlite3_file *pFile){ devsym_file *p = (devsym_file *)pFile; - sqlite3OsShmBarrier(p->pReal); + p->pReal->pMethods->xShmBarrier(p->pReal); } static int devsymShmUnmap(sqlite3_file *pFile, int delFlag){ devsym_file *p = (devsym_file *)pFile; - return sqlite3OsShmUnmap(p->pReal, delFlag); + return p->pReal->pMethods->xShmUnmap(p->pReal, delFlag); } @@ -270,6 +224,26 @@ static int devsymOpen( int flags, int *pOutFlags ){ +static sqlite3_io_methods devsym_io_methods = { + 2, /* iVersion */ + devsymClose, /* xClose */ + devsymRead, /* xRead */ + devsymWrite, /* xWrite */ + devsymTruncate, /* xTruncate */ + devsymSync, /* xSync */ + devsymFileSize, /* xFileSize */ + devsymLock, /* xLock */ + devsymUnlock, /* xUnlock */ + devsymCheckReservedLock, /* xCheckReservedLock */ + devsymFileControl, /* xFileControl */ + devsymSectorSize, /* xSectorSize */ + devsymDeviceCharacteristics, /* xDeviceCharacteristics */ + devsymShmMap, /* xShmMap */ + devsymShmLock, /* xShmLock */ + devsymShmBarrier, /* xShmBarrier */ + devsymShmUnmap /* xShmUnmap */ +}; + int rc; devsym_file *p = (devsym_file *)pFile; p->pReal = (sqlite3_file *)&p[1]; @@ -371,6 +345,137 @@ static int devsymCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){ return g.pVfs->xCurrentTime(g.pVfs, pTimeOut); } +/* +** Return the sector-size in bytes for an writecrash-file. +*/ +static int writecrashSectorSize(sqlite3_file *pFile){ + devsym_file *p = (devsym_file *)pFile; + return sqlite3OsSectorSize(p->pReal); +} + +/* +** Return the device characteristic flags supported by an writecrash-file. +*/ +static int writecrashDeviceCharacteristics(sqlite3_file *pFile){ + devsym_file *p = (devsym_file *)pFile; + return sqlite3OsDeviceCharacteristics(p->pReal); +} + +/* +** Write data to an writecrash-file. +*/ +static int writecrashWrite( + sqlite3_file *pFile, + const void *zBuf, + int iAmt, + sqlite_int64 iOfst +){ + devsym_file *p = (devsym_file *)pFile; + if( g.nWriteCrash>0 ){ + g.nWriteCrash--; + if( g.nWriteCrash==0 ) abort(); + } + return sqlite3OsWrite(p->pReal, zBuf, iAmt, iOfst); +} + +/* +** Open an writecrash file handle. +*/ +static int writecrashOpen( + sqlite3_vfs *pVfs, + const char *zName, + sqlite3_file *pFile, + int flags, + int *pOutFlags +){ +static sqlite3_io_methods writecrash_io_methods = { + 2, /* iVersion */ + devsymClose, /* xClose */ + devsymRead, /* xRead */ + writecrashWrite, /* xWrite */ + devsymTruncate, /* xTruncate */ + devsymSync, /* xSync */ + devsymFileSize, /* xFileSize */ + devsymLock, /* xLock */ + devsymUnlock, /* xUnlock */ + devsymCheckReservedLock, /* xCheckReservedLock */ + devsymFileControl, /* xFileControl */ + writecrashSectorSize, /* xSectorSize */ + writecrashDeviceCharacteristics, /* xDeviceCharacteristics */ + devsymShmMap, /* xShmMap */ + devsymShmLock, /* xShmLock */ + devsymShmBarrier, /* xShmBarrier */ + devsymShmUnmap /* xShmUnmap */ +}; + + int rc; + devsym_file *p = (devsym_file *)pFile; + p->pReal = (sqlite3_file *)&p[1]; + rc = sqlite3OsOpen(g.pVfs, zName, p->pReal, flags, pOutFlags); + if( p->pReal->pMethods ){ + pFile->pMethods = &writecrash_io_methods; + } + return rc; +} + +static sqlite3_vfs devsym_vfs = { + 2, /* iVersion */ + sizeof(devsym_file), /* szOsFile */ + DEVSYM_MAX_PATHNAME, /* mxPathname */ + 0, /* pNext */ + DEVSYM_VFS_NAME, /* zName */ + 0, /* pAppData */ + devsymOpen, /* xOpen */ + devsymDelete, /* xDelete */ + devsymAccess, /* xAccess */ + devsymFullPathname, /* xFullPathname */ +#ifndef SQLITE_OMIT_LOAD_EXTENSION + devsymDlOpen, /* xDlOpen */ + devsymDlError, /* xDlError */ + devsymDlSym, /* xDlSym */ + devsymDlClose, /* xDlClose */ +#else + 0, /* xDlOpen */ + 0, /* xDlError */ + 0, /* xDlSym */ + 0, /* xDlClose */ +#endif /* SQLITE_OMIT_LOAD_EXTENSION */ + devsymRandomness, /* xRandomness */ + devsymSleep, /* xSleep */ + devsymCurrentTime, /* xCurrentTime */ + 0, /* xGetLastError */ + 0 /* xCurrentTimeInt64 */ +}; + +static sqlite3_vfs writecrash_vfs = { + 2, /* iVersion */ + sizeof(devsym_file), /* szOsFile */ + DEVSYM_MAX_PATHNAME, /* mxPathname */ + 0, /* pNext */ + WRITECRASH_NAME, /* zName */ + 0, /* pAppData */ + writecrashOpen, /* xOpen */ + devsymDelete, /* xDelete */ + devsymAccess, /* xAccess */ + devsymFullPathname, /* xFullPathname */ +#ifndef SQLITE_OMIT_LOAD_EXTENSION + devsymDlOpen, /* xDlOpen */ + devsymDlError, /* xDlError */ + devsymDlSym, /* xDlSym */ + devsymDlClose, /* xDlClose */ +#else + 0, /* xDlOpen */ + 0, /* xDlError */ + 0, /* xDlSym */ + 0, /* xDlClose */ +#endif /* SQLITE_OMIT_LOAD_EXTENSION */ + devsymRandomness, /* xRandomness */ + devsymSleep, /* xSleep */ + devsymCurrentTime, /* xCurrentTime */ + 0, /* xGetLastError */ + 0 /* xCurrentTimeInt64 */ +}; + /* ** This procedure registers the devsym vfs with SQLite. If the argument is @@ -378,10 +483,13 @@ static int devsymCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){ ** available function in this file. */ void devsym_register(int iDeviceChar, int iSectorSize){ + if( g.pVfs==0 ){ g.pVfs = sqlite3_vfs_find(0); devsym_vfs.szOsFile += g.pVfs->szOsFile; + writecrash_vfs.szOsFile += g.pVfs->szOsFile; sqlite3_vfs_register(&devsym_vfs, 0); + sqlite3_vfs_register(&writecrash_vfs, 0); } if( iDeviceChar>=0 ){ g.iDeviceChar = iDeviceChar; @@ -395,4 +503,23 @@ void devsym_register(int iDeviceChar, int iSectorSize){ } } +void devsym_unregister(){ + sqlite3_vfs_unregister(&devsym_vfs); + sqlite3_vfs_unregister(&writecrash_vfs); + g.pVfs = 0; + g.iDeviceChar = 0; + g.iSectorSize = 0; +} + +void devsym_crash_on_write(int nWrite){ + if( g.pVfs==0 ){ + g.pVfs = sqlite3_vfs_find(0); + devsym_vfs.szOsFile += g.pVfs->szOsFile; + writecrash_vfs.szOsFile += g.pVfs->szOsFile; + sqlite3_vfs_register(&devsym_vfs, 0); + sqlite3_vfs_register(&writecrash_vfs, 0); + } + g.nWriteCrash = nWrite; +} + #endif diff --git a/src/test_fs.c b/src/test_fs.c index 45db0b53b8..6879f82734 100644 --- a/src/test_fs.c +++ b/src/test_fs.c @@ -62,26 +62,18 @@ ** SELECT * FROM fstree WHERE path LIKE '/home/dan/sqlite/%' */ #include "sqliteInt.h" -#include "tcl.h" - +#include "tclsqlite.h" #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> -#if SQLITE_OS_UNIX || defined(__MINGW_H) +#if !defined(_WIN32) || defined(__MSVCRT__) # include <unistd.h> # include <dirent.h> -# ifndef DIRENT -# define DIRENT dirent -# endif -#endif -#if SQLITE_OS_WIN -# include <io.h> -# if !defined(__MINGW_H) -# include "test_windirent.h" -# endif +#else +# include "windirent.h" # ifndef S_ISREG # define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG) # endif @@ -125,7 +117,7 @@ struct FsdirCsr { char *zDir; /* Buffer containing directory scanned */ DIR *pDir; /* Open directory */ sqlite3_int64 iRowid; - struct DIRENT entry; /* Current entry */ + struct dirent *pEntry; }; /* @@ -232,16 +224,8 @@ static int fsdirNext(sqlite3_vtab_cursor *cur){ FsdirCsr *pCsr = (FsdirCsr*)cur; if( pCsr->pDir ){ - struct DIRENT *pRes = 0; -#if defined(__MINGW_H) - pRes = readdir(pCsr->pDir); - if( pRes!=0 ){ - memcpy(&pCsr->entry, pRes, sizeof(struct DIRENT)); - } -#else - readdir_r(pCsr->pDir, &pCsr->entry, &pRes); -#endif - if( pRes==0 ){ + pCsr->pEntry = readdir(pCsr->pDir); + if( pCsr->pEntry==0 ){ closedir(pCsr->pDir); pCsr->pDir = 0; } @@ -304,7 +288,7 @@ static int fsdirColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){ break; case 1: /* name */ - sqlite3_result_text(ctx, pCsr->entry.d_name, -1, SQLITE_TRANSIENT); + sqlite3_result_text(ctx, pCsr->pEntry->d_name, -1, SQLITE_TRANSIENT); break; default: @@ -494,11 +478,15 @@ static int fstreeFilter( int nDir; char aWild[2] = { '\0', '\0' }; -#if SQLITE_OS_WIN - zRoot = sqlite3_mprintf("%s%c", getenv("SystemDrive"), '/'); - nRoot = strlen(zRoot); - zPrefix = sqlite3_mprintf("%s", getenv("SystemDrive")); - nPrefix = strlen(zPrefix); +#ifdef _WIN32 + const char *zDrive = getenv("fstreeDrive"); + if( zDrive==0 ){ + zDrive = getenv("SystemDrive"); + } + zRoot = sqlite3_mprintf("%s%c", zDrive, '/'); + nRoot = sqlite3Strlen30(zRoot); + zPrefix = sqlite3_mprintf("%s", zDrive); + nPrefix = sqlite3Strlen30(zPrefix); #else zRoot = "/"; nRoot = 1; @@ -537,12 +525,13 @@ static int fstreeFilter( zDir = zQuery; } } + if( nDir==0 ) nDir = 1; sqlite3_bind_text(pCsr->pStmt, 1, zDir, nDir, SQLITE_TRANSIENT); sqlite3_bind_text(pCsr->pStmt, 2, zRoot, nRoot, SQLITE_TRANSIENT); sqlite3_bind_text(pCsr->pStmt, 3, zPrefix, nPrefix, SQLITE_TRANSIENT); -#if SQLITE_OS_WIN +#ifdef _WIN32 sqlite3_free(zPrefix); sqlite3_free(zRoot); #endif @@ -735,7 +724,7 @@ static int fsColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){ fstat(fd, &sbuf); if( sbuf.st_size>=pCur->nAlloc ){ - int nNew = sbuf.st_size*2; + sqlite3_int64 nNew = sbuf.st_size*2; char *zNew; if( nNew<1024 ) nNew = 1024; @@ -815,6 +804,11 @@ static sqlite3_module fsModule = { 0, /* xRollback */ 0, /* xFindMethod */ 0, /* xRename */ + 0, /* xSavepoint */ + 0, /* xRelease */ + 0, /* xRollbackTo */ + 0, /* xShadowName */ + 0 /* xIntegrity */ }; static sqlite3_module fsdirModule = { @@ -838,6 +832,11 @@ static sqlite3_module fsdirModule = { 0, /* xRollback */ 0, /* xFindMethod */ 0, /* xRename */ + 0, /* xSavepoint */ + 0, /* xRelease */ + 0, /* xRollbackTo */ + 0, /* xShadowName */ + 0 /* xIntegrity */ }; static sqlite3_module fstreeModule = { @@ -861,6 +860,11 @@ static sqlite3_module fstreeModule = { 0, /* xRollback */ 0, /* xFindMethod */ 0, /* xRename */ + 0, /* xSavepoint */ + 0, /* xRelease */ + 0, /* xRollbackTo */ + 0, /* xShadowName */ + 0 /* xIntegrity */ }; /* @@ -871,7 +875,7 @@ extern int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb); /* ** Register the echo virtual table module. */ -static int register_fs_module( +static int SQLITE_TCLAPI register_fs_module( ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ diff --git a/src/test_func.c b/src/test_func.c index 579177b70c..82f7b3d9ca 100644 --- a/src/test_func.c +++ b/src/test_func.c @@ -13,7 +13,7 @@ ** implements new SQL functions used by the test scripts. */ #include "sqlite3.h" -#include "tcl.h" +#include "tclsqlite.h" #include <stdlib.h> #include <string.h> #include <assert.h> @@ -21,7 +21,6 @@ #include "sqliteInt.h" #include "vdbeInt.h" - /* ** Allocate nByte bytes of space using sqlite3_malloc(). If the ** allocation fails, call sqlite3_result_error_nomem() to notify @@ -152,7 +151,7 @@ static void test_destructor_count( ** arguments. It returns the text value returned by the sqlite3_errmsg16() ** API function. */ -#ifndef SQLITE_OMIT_BUILTIN_TEST +#ifndef SQLITE_UNTESTABLE void sqlite3BeginBenignMalloc(void); void sqlite3EndBenignMalloc(void); #else @@ -496,7 +495,8 @@ static void test_extract( mem.db = db; mem.enc = ENC(db); pHdr += sqlite3GetVarint(pHdr, &iSerialType); - pBody += sqlite3VdbeSerialGet(pBody, (u32)iSerialType, &mem); + sqlite3VdbeSerialGet(pBody, (u32)iSerialType, &mem); + pBody += sqlite3VdbeSerialTypeLen((u32)iSerialType); if( iCurrent==iIdx ){ sqlite3_result_value(context, &mem); @@ -544,7 +544,8 @@ static void test_decode( mem.db = db; mem.enc = ENC(db); pHdr += sqlite3GetVarint(pHdr, &iSerialType); - pBody += sqlite3VdbeSerialGet(pBody, (u32)iSerialType, &mem); + sqlite3VdbeSerialGet(pBody, (u32)iSerialType, &mem); + pBody += sqlite3VdbeSerialTypeLen((u32)iSerialType); switch( sqlite3_value_type(&mem) ){ case SQLITE_TEXT: @@ -627,6 +628,24 @@ static void test_getsubtype( sqlite3_result_int(context, (int)sqlite3_value_subtype(argv[0])); } +/* test_frombind(A,B,C,...) +** +** Return an integer bitmask that has a bit set for every argument +** (up to the first 63 arguments) that originates from a bind a parameter. +*/ +static void test_frombind( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + sqlite3_uint64 m = 0; + int i; + for(i=0; i<argc && i<63; i++){ + if( sqlite3_value_frombind(argv[i]) ) m |= ((sqlite3_uint64)1)<<i; + } + sqlite3_result_int64(context, (sqlite3_int64)m); +} + /* test_setsubtype(V, T) ** ** Return the value V with its subtype changed to T @@ -640,7 +659,11 @@ static void test_setsubtype( sqlite3_result_subtype(context, (unsigned int)sqlite3_value_int(argv[1])); } -static int registerTestFunctions(sqlite3 *db){ +static int registerTestFunctions( + sqlite3 *db, + char **pzErrMsg, + const sqlite3_api_routines *pThunk +){ static const struct { char *zName; signed char nArg; @@ -667,7 +690,9 @@ static int registerTestFunctions(sqlite3 *db){ { "test_extract", 2, SQLITE_UTF8, test_extract}, { "test_zeroblob", 1, SQLITE_UTF8|SQLITE_DETERMINISTIC, test_zeroblob}, { "test_getsubtype", 1, SQLITE_UTF8, test_getsubtype}, - { "test_setsubtype", 2, SQLITE_UTF8, test_setsubtype}, + { "test_setsubtype", 2, SQLITE_UTF8|SQLITE_RESULT_SUBTYPE, + test_setsubtype}, + { "test_frombind", -1, SQLITE_UTF8, test_frombind}, }; int i; @@ -689,16 +714,16 @@ static int registerTestFunctions(sqlite3 *db){ ** the standard set of test functions to be loaded into each new ** database connection. */ -static int autoinstall_test_funcs( +static int SQLITE_TCLAPI autoinstall_test_funcs( void * clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[] ){ - extern int Md5_Register(sqlite3*); - int rc = sqlite3_auto_extension((void*)registerTestFunctions); + extern int Md5_Register(sqlite3 *, char **, const sqlite3_api_routines *); + int rc = sqlite3_auto_extension((void(*)(void))registerTestFunctions); if( rc==SQLITE_OK ){ - rc = sqlite3_auto_extension((void*)Md5_Register); + rc = sqlite3_auto_extension((void(*)(void))Md5_Register); } Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); return TCL_OK; @@ -717,7 +742,7 @@ static void tFinal(sqlite3_context *a){} ** Make various calls to sqlite3_create_function that do not have valid ** parameters. Verify that the error condition is detected and reported. */ -static int abuse_create_function( +static int SQLITE_TCLAPI abuse_create_function( void * clientData, Tcl_Interp *interp, int objc, @@ -748,7 +773,7 @@ static int abuse_create_function( rc = sqlite3_create_function(db, "tx", -2, SQLITE_UTF8, 0, tStep, 0, 0); if( rc!=SQLITE_MISUSE ) goto abuse_err; - rc = sqlite3_create_function(db, "tx", 128, SQLITE_UTF8, 0, tStep, 0, 0); + rc = sqlite3_create_function(db, "tx", 32768, SQLITE_UTF8, 0, tStep, 0, 0); if( rc!=SQLITE_MISUSE ) goto abuse_err; rc = sqlite3_create_function(db, "funcxx" @@ -764,7 +789,7 @@ static int abuse_create_function( ** a no-op function (that always returns NULL) and which has the ** maximum-length function name and the maximum number of parameters. */ - sqlite3_limit(db, SQLITE_LIMIT_FUNCTION_ARG, 10000); + sqlite3_limit(db, SQLITE_LIMIT_FUNCTION_ARG, 1000000); mxArg = sqlite3_limit(db, SQLITE_LIMIT_FUNCTION_ARG, -1); rc = sqlite3_create_function(db, "nullx" "_123456789_123456789_123456789_123456789_123456789" @@ -783,6 +808,124 @@ static int abuse_create_function( return TCL_ERROR; } + +/* +** SQLite user defined function to use with matchinfo() to calculate the +** relevancy of an FTS match. The value returned is the relevancy score +** (a real value greater than or equal to zero). A larger value indicates +** a more relevant document. +** +** The overall relevancy returned is the sum of the relevancies of each +** column value in the FTS table. The relevancy of a column value is the +** sum of the following for each reportable phrase in the FTS query: +** +** (<hit count> / <global hit count>) * <column weight> +** +** where <hit count> is the number of instances of the phrase in the +** column value of the current row and <global hit count> is the number +** of instances of the phrase in the same column of all rows in the FTS +** table. The <column weight> is a weighting factor assigned to each +** column by the caller (see below). +** +** The first argument to this function must be the return value of the FTS +** matchinfo() function. Following this must be one argument for each column +** of the FTS table containing a numeric weight factor for the corresponding +** column. Example: +** +** CREATE VIRTUAL TABLE documents USING fts3(title, content) +** +** The following query returns the docids of documents that match the full-text +** query <query> sorted from most to least relevant. When calculating +** relevance, query term instances in the 'title' column are given twice the +** weighting of those in the 'content' column. +** +** SELECT docid FROM documents +** WHERE documents MATCH <query> +** ORDER BY rank(matchinfo(documents), 1.0, 0.5) DESC +*/ +static void rankfunc(sqlite3_context *pCtx, int nVal, sqlite3_value **apVal){ + int *aMatchinfo; /* Return value of matchinfo() */ + int nMatchinfo; /* Number of elements in aMatchinfo[] */ + int nCol = 0; /* Number of columns in the table */ + int nPhrase = 0; /* Number of phrases in the query */ + int iPhrase; /* Current phrase */ + double score = 0.0; /* Value to return */ + + assert( sizeof(int)==4 ); + + /* Check that the number of arguments passed to this function is correct. + ** If not, jump to wrong_number_args. Set aMatchinfo to point to the array + ** of unsigned integer values returned by FTS function matchinfo. Set + ** nPhrase to contain the number of reportable phrases in the users full-text + ** query, and nCol to the number of columns in the table. Then check that the + ** size of the matchinfo blob is as expected. Return an error if it is not. + */ + if( nVal<1 ) goto wrong_number_args; + aMatchinfo = (int*)sqlite3_value_blob(apVal[0]); + nMatchinfo = sqlite3_value_bytes(apVal[0]) / sizeof(int); + if( nMatchinfo>=2 ){ + nPhrase = aMatchinfo[0]; + nCol = aMatchinfo[1]; + } + if( nMatchinfo!=(2+3*nCol*nPhrase) ){ + sqlite3_result_error(pCtx, + "invalid matchinfo blob passed to function rank()", -1); + return; + } + if( nVal!=(1+nCol) ) goto wrong_number_args; + + /* Iterate through each phrase in the users query. */ + for(iPhrase=0; iPhrase<nPhrase; iPhrase++){ + int iCol; /* Current column */ + + /* Now iterate through each column in the users query. For each column, + ** increment the relevancy score by: + ** + ** (<hit count> / <global hit count>) * <column weight> + ** + ** aPhraseinfo[] points to the start of the data for phrase iPhrase. So + ** the hit count and global hit counts for each column are found in + ** aPhraseinfo[iCol*3] and aPhraseinfo[iCol*3+1], respectively. + */ + int *aPhraseinfo = &aMatchinfo[2 + iPhrase*nCol*3]; + for(iCol=0; iCol<nCol; iCol++){ + int nHitCount = aPhraseinfo[3*iCol]; + int nGlobalHitCount = aPhraseinfo[3*iCol+1]; + double weight = sqlite3_value_double(apVal[iCol+1]); + if( nHitCount>0 ){ + score += ((double)nHitCount / (double)nGlobalHitCount) * weight; + } + } + } + + sqlite3_result_double(pCtx, score); + return; + + /* Jump here if the wrong number of arguments are passed to this function */ +wrong_number_args: + sqlite3_result_error(pCtx, "wrong number of arguments to function rank()", -1); +} + +static int SQLITE_TCLAPI install_fts3_rank_function( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + extern int getDbPointer(Tcl_Interp*, const char*, sqlite3**); + sqlite3 *db; + + if( objc!=2 ){ + Tcl_WrongNumArgs(interp, 1, objv, "DB"); + return TCL_ERROR; + } + + if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; + sqlite3_create_function(db, "rank", -1, SQLITE_UTF8, 0, rankfunc, 0, 0); + return TCL_OK; +} + + /* ** Register commands with the TCL interpreter. */ @@ -793,15 +936,16 @@ int Sqlitetest_func_Init(Tcl_Interp *interp){ } aObjCmd[] = { { "autoinstall_test_functions", autoinstall_test_funcs }, { "abuse_create_function", abuse_create_function }, + { "install_fts3_rank_function", install_fts3_rank_function }, }; int i; - extern int Md5_Register(sqlite3*); + extern int Md5_Register(sqlite3 *, char **, const sqlite3_api_routines *); for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){ Tcl_CreateObjCommand(interp, aObjCmd[i].zName, aObjCmd[i].xProc, 0, 0); } sqlite3_initialize(); - sqlite3_auto_extension((void*)registerTestFunctions); - sqlite3_auto_extension((void*)Md5_Register); + sqlite3_auto_extension((void(*)(void))registerTestFunctions); + sqlite3_auto_extension((void(*)(void))Md5_Register); return TCL_OK; } diff --git a/src/test_hexio.c b/src/test_hexio.c index b20b5ce730..048ab13246 100644 --- a/src/test_hexio.c +++ b/src/test_hexio.c @@ -18,7 +18,7 @@ ** easier and safer to build our own mechanism. */ #include "sqliteInt.h" -#include "tcl.h" +#include "tclsqlite.h" #include <stdlib.h> #include <string.h> #include <assert.h> @@ -94,7 +94,7 @@ int sqlite3TestHexToBin(const unsigned char *zIn, int N, unsigned char *aOut){ ** beginning of the file. Convert that information to hexadecimal ** and return the resulting HEX string. */ -static int hexio_read( +static int SQLITE_TCLAPI hexio_read( void * clientData, Tcl_Interp *interp, int objc, @@ -122,7 +122,7 @@ static int hexio_read( in = fopen(zFile, "r"); } if( in==0 ){ - Tcl_AppendResult(interp, "cannot open input file ", zFile, 0); + Tcl_AppendResult(interp, "cannot open input file ", zFile, NULL); return TCL_ERROR; } fseek(in, offset, SEEK_SET); @@ -132,7 +132,7 @@ static int hexio_read( got = 0; } sqlite3TestBinToHex(zBuf, got); - Tcl_AppendResult(interp, zBuf, 0); + Tcl_AppendResult(interp, zBuf, NULL); sqlite3_free(zBuf); return TCL_OK; } @@ -144,14 +144,15 @@ static int hexio_read( ** Write DATA into file FILENAME beginning at OFFSET from the ** beginning of the file. DATA is expressed in hexadecimal. */ -static int hexio_write( +static int SQLITE_TCLAPI hexio_write( void * clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[] ){ int offset; - int nIn, nOut, written; + Tcl_Size nIn; + int nOut, written; const char *zFile; const unsigned char *zIn; unsigned char *aOut; @@ -164,17 +165,17 @@ static int hexio_write( if( Tcl_GetIntFromObj(interp, objv[2], &offset) ) return TCL_ERROR; zFile = Tcl_GetString(objv[1]); zIn = (const unsigned char *)Tcl_GetStringFromObj(objv[3], &nIn); - aOut = sqlite3_malloc( nIn/2 ); + aOut = sqlite3_malloc64( 1 + nIn/2 ); if( aOut==0 ){ return TCL_ERROR; } - nOut = sqlite3TestHexToBin(zIn, nIn, aOut); + nOut = sqlite3TestHexToBin(zIn, (int)nIn, aOut); out = fopen(zFile, "r+b"); if( out==0 ){ out = fopen(zFile, "r+"); } if( out==0 ){ - Tcl_AppendResult(interp, "cannot open output file ", zFile, 0); + Tcl_AppendResult(interp, "cannot open output file ", zFile, NULL); return TCL_ERROR; } fseek(out, offset, SEEK_SET); @@ -186,34 +187,43 @@ static int hexio_write( } /* -** USAGE: hexio_get_int HEXDATA +** USAGE: hexio_get_int [-littleendian] HEXDATA ** ** Interpret the HEXDATA argument as a big-endian integer. Return ** the value of that integer. HEXDATA can contain between 2 and 8 ** hexadecimal digits. */ -static int hexio_get_int( +static int SQLITE_TCLAPI hexio_get_int( void * clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[] ){ int val; - int nIn, nOut; + Tcl_Size nIn; + int nOut; const unsigned char *zIn; unsigned char *aOut; unsigned char aNum[4]; + int bLittle = 0; - if( objc!=2 ){ - Tcl_WrongNumArgs(interp, 1, objv, "HEXDATA"); + if( objc==3 ){ + Tcl_Size n; + char *z = Tcl_GetStringFromObj(objv[1], &n); + if( n>=2 && n<=13 && memcmp(z, "-littleendian", n)==0 ){ + bLittle = 1; + } + } + if( (objc-bLittle)!=2 ){ + Tcl_WrongNumArgs(interp, 1, objv, "[-littleendian] HEXDATA"); return TCL_ERROR; } - zIn = (const unsigned char *)Tcl_GetStringFromObj(objv[1], &nIn); - aOut = sqlite3_malloc( nIn/2 ); + zIn = (const unsigned char *)Tcl_GetStringFromObj(objv[1+bLittle], &nIn); + aOut = sqlite3_malloc64( 1 + nIn/2 ); if( aOut==0 ){ return TCL_ERROR; } - nOut = sqlite3TestHexToBin(zIn, nIn, aOut); + nOut = sqlite3TestHexToBin(zIn, (int)nIn, aOut); if( nOut>=4 ){ memcpy(aNum, aOut, 4); }else{ @@ -221,7 +231,11 @@ static int hexio_get_int( memcpy(&aNum[4-nOut], aOut, nOut); } sqlite3_free(aOut); - val = (aNum[0]<<24) | (aNum[1]<<16) | (aNum[2]<<8) | aNum[3]; + if( bLittle ){ + val = (int)((u32)aNum[3]<<24) | (aNum[2]<<16) | (aNum[1]<<8) | aNum[0]; + }else{ + val = (int)((u32)aNum[0]<<24) | (aNum[1]<<16) | (aNum[2]<<8) | aNum[3]; + } Tcl_SetObjResult(interp, Tcl_NewIntObj(val)); return TCL_OK; } @@ -232,7 +246,7 @@ static int hexio_get_int( ** ** Render INTEGER has a 16-bit big-endian integer in hexadecimal. */ -static int hexio_render_int16( +static int SQLITE_TCLAPI hexio_render_int16( void * clientData, Tcl_Interp *interp, int objc, @@ -259,7 +273,7 @@ static int hexio_render_int16( ** ** Render INTEGER has a 32-bit big-endian integer in hexadecimal. */ -static int hexio_render_int32( +static int SQLITE_TCLAPI hexio_render_int32( void * clientData, Tcl_Interp *interp, int objc, @@ -289,14 +303,14 @@ static int hexio_render_int32( ** The UTF8 might not be well-formed. Run this string through ** sqlite3Utf8to8() convert it back to hex and return the result. */ -static int utf8_to_utf8( +static int SQLITE_TCLAPI utf8_to_utf8( void * clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[] ){ #ifdef SQLITE_DEBUG - int n; + Tcl_Size n; int nOut; const unsigned char *zOrig; unsigned char *z; @@ -305,17 +319,17 @@ static int utf8_to_utf8( return TCL_ERROR; } zOrig = (unsigned char *)Tcl_GetStringFromObj(objv[1], &n); - z = sqlite3_malloc( n+3 ); - n = sqlite3TestHexToBin(zOrig, n, z); + z = sqlite3_malloc64( n+4 ); + n = sqlite3TestHexToBin(zOrig, (int)n, z); z[n] = 0; nOut = sqlite3Utf8To8(z); sqlite3TestBinToHex(z,nOut); - Tcl_AppendResult(interp, (char*)z, 0); + Tcl_AppendResult(interp, (char*)z, NULL); sqlite3_free(z); return TCL_OK; #else Tcl_AppendResult(interp, - "[utf8_to_utf8] unavailable - SQLITE_DEBUG not defined", 0 + "[utf8_to_utf8] unavailable - SQLITE_DEBUG not defined", NULL ); return TCL_ERROR; #endif @@ -333,6 +347,17 @@ static int getFts3Varint(const char *p, sqlite_int64 *v){ return (int) (q - (unsigned char *)p); } +static int putFts3Varint(char *p, sqlite_int64 v){ + unsigned char *q = (unsigned char *) p; + sqlite_uint64 vu = v; + do{ + *q++ = (unsigned char) ((vu & 0x7f) | 0x80); + vu >>= 7; + }while( vu!=0 ); + q[-1] &= 0x7f; /* turn off high bit in final byte */ + assert( q - (unsigned char *)p <= 10 ); + return (int) (q - (unsigned char *)p); +} /* ** USAGE: read_fts3varint BLOB VARNAME @@ -340,13 +365,13 @@ static int getFts3Varint(const char *p, sqlite_int64 *v){ ** Read a varint from the start of BLOB. Set variable VARNAME to contain ** the interpreted value. Return the number of bytes of BLOB consumed. */ -static int read_fts3varint( +static int SQLITE_TCLAPI read_fts3varint( void * clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[] ){ - int nBlob; + Tcl_Size nBlob; unsigned char *zBlob; sqlite3_int64 iVal; int nVal; @@ -363,6 +388,67 @@ static int read_fts3varint( return TCL_OK; } +/* +** USAGE: make_fts3record ARGLIST +*/ +static int SQLITE_TCLAPI make_fts3record( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + Tcl_Obj **aArg = 0; + Tcl_Size nArg = 0; + unsigned char *aOut = 0; + sqlite3_int64 nOut = 0; + sqlite3_int64 nAlloc = 0; + int i; + + if( objc!=2 ){ + Tcl_WrongNumArgs(interp, 1, objv, "LIST"); + return TCL_ERROR; + } + if( Tcl_ListObjGetElements(interp, objv[1], &nArg, &aArg) ){ + return TCL_ERROR; + } + + for(i=0; i<(int)nArg; i++){ + Tcl_WideInt iVal; + if( TCL_OK==Tcl_GetWideIntFromObj(0, aArg[i], &iVal) ){ + if( nOut+10>nAlloc ){ + int nNew = nAlloc?nAlloc*2:128; + unsigned char *aNew = sqlite3_realloc(aOut, nNew); + if( aNew==0 ){ + sqlite3_free(aOut); + return TCL_ERROR; + } + aOut = aNew; + nAlloc = nNew; + } + nOut += putFts3Varint((char*)&aOut[nOut], iVal); + }else{ + Tcl_Size nVal = 0; + char *zVal = Tcl_GetStringFromObj(aArg[i], &nVal); + while( (nOut + nVal)>nAlloc ){ + sqlite3_int64 nNew = nAlloc?nAlloc*2:128; + unsigned char *aNew = sqlite3_realloc64(aOut, nNew); + if( aNew==0 ){ + sqlite3_free(aOut); + return TCL_ERROR; + } + aOut = aNew; + nAlloc = nNew; + } + memcpy(&aOut[nOut], zVal, nVal); + nOut += nVal; + } + } + + Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(aOut, nOut)); + sqlite3_free(aOut); + return TCL_OK; +} + /* ** Register commands with the TCL interpreter. @@ -379,6 +465,7 @@ int Sqlitetest_hexio_Init(Tcl_Interp *interp){ { "hexio_render_int32", hexio_render_int32 }, { "utf8_to_utf8", utf8_to_utf8 }, { "read_fts3varint", read_fts3varint }, + { "make_fts3record", make_fts3record }, }; int i; for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){ diff --git a/src/test_init.c b/src/test_init.c index 502d95c052..0c6ac8eb56 100644 --- a/src/test_init.c +++ b/src/test_init.c @@ -27,19 +27,19 @@ #include "sqliteInt.h" #include <string.h> -#include <tcl.h> +#include "tclsqlite.h" static struct Wrapped { sqlite3_pcache_methods2 pcache; sqlite3_mem_methods mem; sqlite3_mutex_methods mutex; - int mem_init; /* True if mem subsystem is initalized */ - int mem_fail; /* True to fail mem subsystem inialization */ - int mutex_init; /* True if mutex subsystem is initalized */ - int mutex_fail; /* True to fail mutex subsystem inialization */ - int pcache_init; /* True if pcache subsystem is initalized */ - int pcache_fail; /* True to fail pcache subsystem inialization */ + int mem_init; /* True if mem subsystem is initialized */ + int mem_fail; /* True to fail mem subsystem initialization */ + int mutex_init; /* True if mutex subsystem is initialized */ + int mutex_fail; /* True to fail mutex subsystem initialization */ + int pcache_init; /* True if pcache subsystem is initialized */ + int pcache_fail; /* True to fail pcache subsystem initialization */ } wrapped; static int wrMemInit(void *pAppData){ @@ -184,7 +184,7 @@ static void installInitWrappers(void){ sqlite3_config(SQLITE_CONFIG_PCACHE2, &pcachemethods); } -static int init_wrapper_install( +static int SQLITE_TCLAPI init_wrapper_install( ClientData clientData, /* Unused */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ @@ -201,14 +201,14 @@ static int init_wrapper_install( }else if( strcmp(z, "pcache")==0 ){ wrapped.pcache_fail = 1; }else{ - Tcl_AppendResult(interp, "Unknown argument: \"", z, "\""); + Tcl_AppendResult(interp, "Unknown argument: \"", z, "\"", NULL); return TCL_ERROR; } } return TCL_OK; } -static int init_wrapper_uninstall( +static int SQLITE_TCLAPI init_wrapper_uninstall( ClientData clientData, /* Unused */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ @@ -226,7 +226,7 @@ static int init_wrapper_uninstall( return TCL_OK; } -static int init_wrapper_clear( +static int SQLITE_TCLAPI init_wrapper_clear( ClientData clientData, /* Unused */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ @@ -243,7 +243,7 @@ static int init_wrapper_clear( return TCL_OK; } -static int init_wrapper_query( +static int SQLITE_TCLAPI init_wrapper_query( ClientData clientData, /* Unused */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ diff --git a/src/test_intarray.c b/src/test_intarray.c index 70e34db3e1..9e4629467e 100644 --- a/src/test_intarray.c +++ b/src/test_intarray.c @@ -13,6 +13,10 @@ ** This file implements a read-only VIRTUAL TABLE that contains the ** content of a C-language array of integer values. See the corresponding ** header file for full details. +** +** This virtual table is used for internal testing of SQLite only. It is +** not recommended for use in production. For a similar virtual table that +** is production-ready, see the "carray" virtual table over in ext/misc. */ #include "test_intarray.h" #include <string.h> @@ -57,7 +61,8 @@ struct intarray_cursor { /* ** Free an sqlite3_intarray object. */ -static void intarrayFree(sqlite3_intarray *p){ +static void intarrayFree(void *pX){ + sqlite3_intarray *p = (sqlite3_intarray*)pX; if( p->xFree ){ p->xFree(p->a); } @@ -201,6 +206,11 @@ static sqlite3_module intarrayModule = { 0, /* xRollback */ 0, /* xFindMethod */ 0, /* xRename */ + 0, /* xSavepoint */ + 0, /* xRelease */ + 0, /* xRollbackTo */ + 0, /* xShadowName */ + 0 /* xIntegrity */ }; #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */ @@ -270,7 +280,7 @@ SQLITE_API int sqlite3_intarray_bind( ** Everything below is interface for testing this module. */ #ifdef SQLITE_TEST -#include <tcl.h> +#include "tclsqlite.h" /* ** Routines to encode and decode pointers @@ -286,7 +296,7 @@ extern const char *sqlite3ErrName(int); ** Invoke the sqlite3_intarray_create interface. A string that becomes ** the first parameter to sqlite3_intarray_bind. */ -static int test_intarray_create( +static int SQLITE_TCLAPI test_intarray_create( ClientData clientData, /* Not used */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ @@ -308,7 +318,6 @@ static int test_intarray_create( rc = sqlite3_intarray_create(db, zName, &pArray); #endif if( rc!=SQLITE_OK ){ - assert( pArray==0 ); Tcl_AppendResult(interp, sqlite3ErrName(rc), (char*)0); return TCL_ERROR; } @@ -322,7 +331,7 @@ static int test_intarray_create( ** ** Invoke the sqlite3_intarray_bind interface on the given array of integers. */ -static int test_intarray_bind( +static int SQLITE_TCLAPI test_intarray_bind( ClientData clientData, /* Not used */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ diff --git a/src/test_intarray.h b/src/test_intarray.h index 84b1f3fe66..b68233b12a 100644 --- a/src/test_intarray.h +++ b/src/test_intarray.h @@ -13,13 +13,17 @@ ** This is the C-language interface definition for the "intarray" or ** integer array virtual table for SQLite. ** +** This virtual table is used for internal testing of SQLite only. It is +** not recommended for use in production. For a similar virtual table that +** is production-ready, see the "carray" virtual table over in ext/misc. +** ** The intarray virtual table is designed to facilitate using an ** array of integers as the right-hand side of an IN operator. So ** instead of doing a prepared statement like this: ** ** SELECT * FROM table WHERE x IN (?,?,?,...,?); ** -** And then binding indivdual integers to each of ? slots, a C-language +** And then binding individual integers to each of ? slots, a C-language ** application can create an intarray object (named "ex1" in the following ** example), prepare a statement like this: ** @@ -72,11 +76,14 @@ ** virtual table is dropped. Since the virtual tables are created in the ** TEMP database, they are automatically dropped when the database connection ** closes so the application does not normally need to take any special -** action to free the intarray objects. +** action to free the intarray objects. Because of the way virtual tables +** work and the (somewhat goofy) way that the intarray virtual table is +** implemented, it is not allowed to invoke sqlite3_intarray_create(D,N,P) +** more than once with the same D and N values. */ #include "sqlite3.h" -#ifndef _INTARRAY_H_ -#define _INTARRAY_H_ +#ifndef SQLITE_INTARRAY_H +#define SQLITE_INTARRAY_H /* ** Make sure we can call this stuff from C++. @@ -125,4 +132,4 @@ SQLITE_API int sqlite3_intarray_bind( #ifdef __cplusplus } /* End of the 'extern "C"' block */ #endif -#endif /* _INTARRAY_H_ */ +#endif /* SQLITE_INTARRAY_H */ diff --git a/src/test_journal.c b/src/test_journal.c index 6e320b7abb..6a040ea0ce 100644 --- a/src/test_journal.c +++ b/src/test_journal.c @@ -160,6 +160,7 @@ static int jtRandomness(sqlite3_vfs*, int nByte, char *zOut); static int jtSleep(sqlite3_vfs*, int microseconds); static int jtCurrentTime(sqlite3_vfs*, double*); static int jtCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*); +static int jtGetLastError(sqlite3_vfs*, int, char*); static sqlite3_vfs jt_vfs = { 2, /* iVersion */ @@ -179,7 +180,7 @@ static sqlite3_vfs jt_vfs = { jtRandomness, /* xRandomness */ jtSleep, /* xSleep */ jtCurrentTime, /* xCurrentTime */ - 0, /* xGetLastError */ + jtGetLastError, /* xGetLastError */ jtCurrentTimeInt64 /* xCurrentTimeInt64 */ }; @@ -256,7 +257,8 @@ static int jtClose(sqlite3_file *pFile){ *pp = p->pNext; } leaveJtMutex(); - return sqlite3OsClose(p->pReal); + sqlite3OsClose(p->pReal); + return SQLITE_OK; } /* @@ -284,9 +286,10 @@ static int jtRead( ** b) The file-name specified when the file was opened matches ** all but the final 8 characters of the journal file name. ** -** c) There is currently a reserved lock on the file. +** c) There is currently a reserved lock on the file. This +** condition is waived if the noLock argument is non-zero. **/ -static jt_file *locateDatabaseHandle(const char *zJournal){ +static jt_file *locateDatabaseHandle(const char *zJournal, int noLock){ jt_file *pMain = 0; enterJtMutex(); for(pMain=g.pList; pMain; pMain=pMain->pNext){ @@ -294,7 +297,7 @@ static jt_file *locateDatabaseHandle(const char *zJournal){ if( (pMain->flags&SQLITE_OPEN_MAIN_DB) && ((int)strlen(pMain->zName)==nName) && 0==memcmp(pMain->zName, zJournal, nName) - && (pMain->eLock>=SQLITE_LOCK_RESERVED) + && ((pMain->eLock>=SQLITE_LOCK_RESERVED) || noLock) ){ break; } @@ -516,7 +519,7 @@ static int jtWrite( jt_file *p = (jt_file *)pFile; if( p->flags&SQLITE_OPEN_MAIN_JOURNAL ){ if( iOfst==0 ){ - jt_file *pMain = locateDatabaseHandle(p->zName); + jt_file *pMain = locateDatabaseHandle(p->zName, 0); assert( pMain ); if( iAmt==28 ){ @@ -554,14 +557,17 @@ static int jtWrite( u32 pgno = (u32)(iOfst/p->nPagesize + 1); assert( (iAmt==1||iAmt==(int)p->nPagesize) && ((iOfst+iAmt)%p->nPagesize)==0 ); - assert( pgno<=p->nPage || p->nSync>0 ); + /* The following assert() statements may fail if this layer is used + ** with a connection in "PRAGMA synchronous=off" mode. If they + ** fail with sync=normal or sync=full, this may indicate problem. */ + assert( p->nPage==0 || pgno<=p->nPage || p->nSync>0 ); assert( pgno>p->nPage || sqlite3BitvecTest(p->pWritable, pgno) ); } } rc = sqlite3OsWrite(p->pReal, zBuf, iAmt, iOfst); if( (p->flags&SQLITE_OPEN_MAIN_JOURNAL) && iAmt==12 ){ - jt_file *pMain = locateDatabaseHandle(p->zName); + jt_file *pMain = locateDatabaseHandle(p->zName, 0); int rc2 = readJournalFile(p, pMain); if( rc==SQLITE_OK ) rc = rc2; } @@ -575,7 +581,7 @@ static int jtTruncate(sqlite3_file *pFile, sqlite_int64 size){ jt_file *p = (jt_file *)pFile; if( p->flags&SQLITE_OPEN_MAIN_JOURNAL && size==0 ){ /* Truncating a journal file. This is the end of a transaction. */ - jt_file *pMain = locateDatabaseHandle(p->zName); + jt_file *pMain = locateDatabaseHandle(p->zName, 0); closeTransaction(pMain); } if( p->flags&SQLITE_OPEN_MAIN_DB && p->pWritable ){ @@ -603,11 +609,10 @@ static int jtSync(sqlite3_file *pFile, int flags){ ** jt_file.pWritable bitvec of the main database file associated with ** this journal file. */ - pMain = locateDatabaseHandle(p->zName); - assert(pMain); + pMain = locateDatabaseHandle(p->zName, 0); /* Set the bitvec values */ - if( pMain->pWritable ){ + if( pMain && pMain->pWritable ){ pMain->nSync++; rc = readJournalFile(p, pMain); if( rc!=SQLITE_OK ){ @@ -729,7 +734,7 @@ static int jtDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){ int nPath = (int)strlen(zPath); if( nPath>8 && 0==strcmp("-journal", &zPath[nPath-8]) ){ /* Deleting a journal file. The end of a transaction. */ - jt_file *pMain = locateDatabaseHandle(zPath); + jt_file *pMain = locateDatabaseHandle(zPath, 0); if( pMain ){ closeTransaction(pMain); } @@ -824,6 +829,10 @@ static int jtCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){ return g.pVfs->xCurrentTimeInt64(g.pVfs, pTimeOut); } +static int jtGetLastError(sqlite3_vfs *pVfs, int n, char *z){ + return g.pVfs->xGetLastError(g.pVfs, n, z); +} + /************************************************************************** ** Start of public API. */ diff --git a/src/test_malloc.c b/src/test_malloc.c index aaa640b03a..68b2c367da 100644 --- a/src/test_malloc.c +++ b/src/test_malloc.c @@ -14,7 +14,7 @@ ** memory allocation subsystem. */ #include "sqliteInt.h" -#include "tcl.h" +#include "tclsqlite.h" #include <stdlib.h> #include <string.h> #include <assert.h> @@ -28,6 +28,8 @@ static struct MemFault { int nRepeat; /* Number of times to repeat the failure */ int nBenign; /* Number of benign failures seen since last config */ int nFail; /* Number of failures seen since last config */ + int nOkBefore; /* Successful allocations prior to the first fault */ + int nOkAfter; /* Successful allocations after a fault */ u8 enable; /* True if enabled */ int isInstalled; /* True if the fault simulation layer is installed */ int isBenignMode; /* True if malloc failures are considered benign */ @@ -39,8 +41,21 @@ static struct MemFault { ** fire on any simulated malloc() failure. */ static void sqlite3Fault(void){ - static int cnt = 0; + static u64 cnt = 0; cnt++; + if( cnt>((u64)1<<63) ) abort(); +} + +/* +** This routine exists as a place to set a breakpoint that will +** fire the first time any malloc() fails on a single test case. +** The sqlite3Fault() routine above runs on every malloc() failure. +** This routine only runs on the first such failure. +*/ +static void sqlite3FirstFault(void){ + static u64 cnt2 = 0; + cnt2++; + if( cnt2>((u64)1<<63) ) abort(); } /* @@ -49,12 +64,15 @@ static void sqlite3Fault(void){ */ static int faultsimStep(void){ if( likely(!memfault.enable) ){ + memfault.nOkAfter++; return 0; } if( memfault.iCountdown>0 ){ memfault.iCountdown--; + memfault.nOkBefore++; return 0; } + if( memfault.nFail==0 ) sqlite3FirstFault(); sqlite3Fault(); memfault.nFail++; if( memfault.isBenignMode>0 ){ @@ -92,32 +110,6 @@ static void *faultsimRealloc(void *pOld, int n){ return p; } -/* -** The following method calls are passed directly through to the underlying -** malloc system: -** -** xFree -** xSize -** xRoundup -** xInit -** xShutdown -*/ -static void faultsimFree(void *p){ - memfault.m.xFree(p); -} -static int faultsimSize(void *p){ - return memfault.m.xSize(p); -} -static int faultsimRoundup(int n){ - return memfault.m.xRoundup(n); -} -static int faultsimInit(void *p){ - return memfault.m.xInit(memfault.m.pAppData); -} -static void faultsimShutdown(void *p){ - memfault.m.xShutdown(memfault.m.pAppData); -} - /* ** This routine configures the malloc failure simulation. After ** calling this routine, the next nDelay mallocs will succeed, followed @@ -129,6 +121,8 @@ static void faultsimConfig(int nDelay, int nRepeat){ memfault.nRepeat = nRepeat; memfault.nBenign = 0; memfault.nFail = 0; + memfault.nOkBefore = 0; + memfault.nOkAfter = 0; memfault.enable = nDelay>=0; /* Sometimes, when running multi-threaded tests, the isBenignMode @@ -182,16 +176,6 @@ static void faultsimEndBenign(void){ ** the argument is non-zero, the */ static int faultsimInstall(int install){ - static struct sqlite3_mem_methods m = { - faultsimMalloc, /* xMalloc */ - faultsimFree, /* xFree */ - faultsimRealloc, /* xRealloc */ - faultsimSize, /* xSize */ - faultsimRoundup, /* xRoundup */ - faultsimInit, /* xInit */ - faultsimShutdown, /* xShutdown */ - 0 /* pAppData */ - }; int rc; install = (install ? 1 : 0); @@ -205,6 +189,9 @@ static int faultsimInstall(int install){ rc = sqlite3_config(SQLITE_CONFIG_GETMALLOC, &memfault.m); assert(memfault.m.xMalloc); if( rc==SQLITE_OK ){ + sqlite3_mem_methods m = memfault.m; + m.xMalloc = faultsimMalloc; + m.xRealloc = faultsimRealloc; rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &m); } sqlite3_test_control(SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, @@ -306,7 +293,7 @@ static int textToPointer(const char *z, void **pp){ ** ** Raw test interface for sqlite3_malloc(). */ -static int test_malloc( +static int SQLITE_TCLAPI test_malloc( void * clientData, Tcl_Interp *interp, int objc, @@ -331,7 +318,7 @@ static int test_malloc( ** ** Raw test interface for sqlite3_realloc(). */ -static int test_realloc( +static int SQLITE_TCLAPI test_realloc( void * clientData, Tcl_Interp *interp, int objc, @@ -360,7 +347,7 @@ static int test_realloc( ** ** Raw test interface for sqlite3_free(). */ -static int test_free( +static int SQLITE_TCLAPI test_free( void * clientData, Tcl_Interp *interp, int objc, @@ -391,14 +378,15 @@ int sqlite3TestBinToHex(char*,int); ** Set a chunk of memory (obtained from malloc, probably) to a ** specified hex pattern. */ -static int test_memset( +static int SQLITE_TCLAPI test_memset( void * clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[] ){ void *p; - int size, n, i; + int size, i; + Tcl_Size n; char *zHex; char *zOut; char zBin[100]; @@ -420,7 +408,7 @@ static int test_memset( } zHex = Tcl_GetStringFromObj(objv[3], &n); if( n>sizeof(zBin)*2 ) n = sizeof(zBin)*2; - n = sqlite3TestHexToBin(zHex, n, zBin); + n = sqlite3TestHexToBin(zHex, (int)n, zBin); if( n==0 ){ Tcl_AppendResult(interp, "no data", (char*)0); return TCL_ERROR; @@ -437,7 +425,7 @@ static int test_memset( ** ** Return memory as hexadecimal text. */ -static int test_memget( +static int SQLITE_TCLAPI test_memget( void * clientData, Tcl_Interp *interp, int objc, @@ -484,7 +472,7 @@ static int test_memget( ** ** Raw test interface for sqlite3_memory_used(). */ -static int test_memory_used( +static int SQLITE_TCLAPI test_memory_used( void * clientData, Tcl_Interp *interp, int objc, @@ -499,7 +487,7 @@ static int test_memory_used( ** ** Raw test interface for sqlite3_memory_highwater(). */ -static int test_memory_highwater( +static int SQLITE_TCLAPI test_memory_highwater( void * clientData, Tcl_Interp *interp, int objc, @@ -524,7 +512,7 @@ static int test_memory_highwater( ** Set the depth of backtracing. If SQLITE_MEMDEBUG is not defined ** then this routine is a no-op. */ -static int test_memdebug_backtrace( +static int SQLITE_TCLAPI test_memdebug_backtrace( void * clientData, Tcl_Interp *interp, int objc, @@ -550,7 +538,7 @@ static int test_memdebug_backtrace( ** ** Write a summary of unfreed memory to FILENAME. */ -static int test_memdebug_dump( +static int SQLITE_TCLAPI test_memdebug_dump( void * clientData, Tcl_Interp *interp, int objc, @@ -575,7 +563,7 @@ static int test_memdebug_dump( ** ** Return the total number of times malloc() has been called. */ -static int test_memdebug_malloc_count( +static int SQLITE_TCLAPI test_memdebug_malloc_count( void * clientData, Tcl_Interp *interp, int objc, @@ -615,7 +603,7 @@ static int test_memdebug_malloc_count( ** ** To disable simulated failures, use a COUNTER of -1. */ -static int test_memdebug_fail( +static int SQLITE_TCLAPI test_memdebug_fail( void * clientData, Tcl_Interp *interp, int objc, @@ -635,7 +623,7 @@ static int test_memdebug_fail( if( Tcl_GetIntFromObj(interp, objv[1], &iFail) ) return TCL_ERROR; for(ii=2; ii<objc; ii+=2){ - int nOption; + Tcl_Size nOption; char *zOption = Tcl_GetStringFromObj(objv[ii], &nOption); char *zErr = 0; @@ -658,7 +646,7 @@ static int test_memdebug_fail( } if( zErr ){ - Tcl_AppendResult(interp, zErr, zOption, 0); + Tcl_AppendResult(interp, zErr, zOption, NULL); return TCL_ERROR; } } @@ -681,7 +669,7 @@ static int test_memdebug_fail( ** simulated failure occurs. A negative return value indicates that ** no malloc() failure is scheduled. */ -static int test_memdebug_pending( +static int SQLITE_TCLAPI test_memdebug_pending( void * clientData, Tcl_Interp *interp, int objc, @@ -714,7 +702,7 @@ static int sqlite3_memdebug_title_count = 0; ** ** Each title overwrite the previous. */ -static int test_memdebug_settitle( +static int SQLITE_TCLAPI test_memdebug_settitle( void * clientData, Tcl_Interp *interp, int objc, @@ -795,7 +783,7 @@ static void test_memdebug_log_clear(void){ Tcl_InitHashTable(&aMallocLog, MALLOC_LOG_KEYINTS); } -static int test_memdebug_log( +static int SQLITE_TCLAPI test_memdebug_log( void * clientData, Tcl_Interp *interp, int objc, @@ -883,46 +871,6 @@ static int test_memdebug_log( return TCL_OK; } -/* -** Usage: sqlite3_config_scratch SIZE N -** -** Set the scratch memory buffer using SQLITE_CONFIG_SCRATCH. -** The buffer is static and is of limited size. N might be -** adjusted downward as needed to accommodate the requested size. -** The revised value of N is returned. -** -** A negative SIZE causes the buffer pointer to be NULL. -*/ -static int test_config_scratch( - void * clientData, - Tcl_Interp *interp, - int objc, - Tcl_Obj *CONST objv[] -){ - int sz, N, rc; - Tcl_Obj *pResult; - static char *buf = 0; - if( objc!=3 ){ - Tcl_WrongNumArgs(interp, 1, objv, "SIZE N"); - return TCL_ERROR; - } - if( Tcl_GetIntFromObj(interp, objv[1], &sz) ) return TCL_ERROR; - if( Tcl_GetIntFromObj(interp, objv[2], &N) ) return TCL_ERROR; - free(buf); - if( sz<0 ){ - buf = 0; - rc = sqlite3_config(SQLITE_CONFIG_SCRATCH, (void*)0, 0, 0); - }else{ - buf = malloc( sz*N + 1 ); - rc = sqlite3_config(SQLITE_CONFIG_SCRATCH, buf, sz, N); - } - pResult = Tcl_NewObj(); - Tcl_ListObjAppendElement(0, pResult, Tcl_NewIntObj(rc)); - Tcl_ListObjAppendElement(0, pResult, Tcl_NewIntObj(N)); - Tcl_SetObjResult(interp, pResult); - return TCL_OK; -} - /* ** Usage: sqlite3_config_pagecache SIZE N ** @@ -933,7 +881,7 @@ static int test_config_scratch( ** ** A negative SIZE causes the buffer pointer to be NULL. */ -static int test_config_pagecache( +static int SQLITE_TCLAPI test_config_pagecache( void * clientData, Tcl_Interp *interp, int objc, @@ -976,7 +924,7 @@ static int test_config_pagecache( ** is certainty. 0 is never. PRNG_SEED is the pseudo-random number generator ** seed. */ -static int test_alt_pcache( +static int SQLITE_TCLAPI test_alt_pcache( void * clientData, Tcl_Interp *interp, int objc, @@ -1017,7 +965,7 @@ static int test_alt_pcache( ** ** Enable or disable memory status reporting using SQLITE_CONFIG_MEMSTATUS. */ -static int test_config_memstatus( +static int SQLITE_TCLAPI test_config_memstatus( void * clientData, Tcl_Interp *interp, int objc, @@ -1038,7 +986,7 @@ static int test_config_memstatus( ** Usage: sqlite3_config_lookaside SIZE COUNT ** */ -static int test_config_lookaside( +static int SQLITE_TCLAPI test_config_lookaside( void * clientData, Tcl_Interp *interp, int objc, @@ -1072,7 +1020,7 @@ static int test_config_lookaside( ** is 10KB in size. A BUFID of 0 indicates that the buffer should be NULL ** which will cause sqlite3_db_config() to allocate space on its own. */ -static int test_db_config_lookaside( +static int SQLITE_TCLAPI test_db_config_lookaside( void * clientData, Tcl_Interp *interp, int objc, @@ -1093,7 +1041,7 @@ static int test_db_config_lookaside( if( Tcl_GetIntFromObj(interp, objv[3], &sz) ) return TCL_ERROR; if( Tcl_GetIntFromObj(interp, objv[4], &cnt) ) return TCL_ERROR; if( bufid==0 ){ - rc = sqlite3_db_config(db, SQLITE_DBCONFIG_LOOKASIDE, 0, sz, cnt); + rc = sqlite3_db_config(db, SQLITE_DBCONFIG_LOOKASIDE, (void*)0, sz, cnt); }else if( bufid>=1 && bufid<=2 && sz*cnt<=sizeof(azBuf[0]) ){ rc = sqlite3_db_config(db, SQLITE_DBCONFIG_LOOKASIDE, azBuf[bufid], sz,cnt); }else{ @@ -1107,7 +1055,7 @@ static int test_db_config_lookaside( /* ** Usage: sqlite3_config_heap NBYTE NMINALLOC */ -static int test_config_heap( +static int SQLITE_TCLAPI test_config_heap( void * clientData, Tcl_Interp *interp, int objc, @@ -1144,7 +1092,7 @@ static int test_config_heap( /* ** Usage: sqlite3_config_heap_size NBYTE */ -static int test_config_heap_size( +static int SQLITE_TCLAPI test_config_heap_size( void * clientData, Tcl_Interp *interp, int objc, @@ -1174,7 +1122,7 @@ static int test_config_heap_size( ** Invoke sqlite3_config() or sqlite3_db_config() with invalid ** opcodes and verify that they return errors. */ -static int test_config_error( +static int SQLITE_TCLAPI test_config_error( void * clientData, Tcl_Interp *interp, int objc, @@ -1212,7 +1160,7 @@ static int test_config_error( ** Enables or disables interpretation of URI parameters by default using ** SQLITE_CONFIG_URI. */ -static int test_config_uri( +static int SQLITE_TCLAPI test_config_uri( void * clientData, Tcl_Interp *interp, int objc, @@ -1241,7 +1189,7 @@ static int test_config_uri( ** Enables or disables the use of the covering-index scan optimization. ** SQLITE_CONFIG_COVERING_INDEX_SCAN. */ -static int test_config_cis( +static int SQLITE_TCLAPI test_config_cis( void * clientData, Tcl_Interp *interp, int objc, @@ -1269,7 +1217,7 @@ static int test_config_cis( ** ** Set the minimum PMA size. */ -static int test_config_pmasz( +static int SQLITE_TCLAPI test_config_pmasz( void * clientData, Tcl_Interp *interp, int objc, @@ -1299,7 +1247,7 @@ static int test_config_pmasz( ** ** Write a summary of unfreed memsys3 allocations to FILENAME. */ -static int test_dump_memsys3( +static int SQLITE_TCLAPI test_dump_memsys3( void * clientData, Tcl_Interp *interp, int objc, @@ -1335,7 +1283,7 @@ static int test_dump_memsys3( ** Return a list of three elements which are the sqlite3_status() return ** code, the current value, and the high-water mark value. */ -static int test_status( +static int SQLITE_TCLAPI test_status( void * clientData, Tcl_Interp *interp, int objc, @@ -1392,7 +1340,7 @@ static int test_status( ** Return a list of three elements which are the sqlite3_db_status() return ** code, the current value, and the high-water mark value. */ -static int test_db_status( +static int SQLITE_TCLAPI test_db_status( void * clientData, Tcl_Interp *interp, int objc, @@ -1417,7 +1365,10 @@ static int test_db_status( { "CACHE_HIT", SQLITE_DBSTATUS_CACHE_HIT }, { "CACHE_MISS", SQLITE_DBSTATUS_CACHE_MISS }, { "CACHE_WRITE", SQLITE_DBSTATUS_CACHE_WRITE }, - { "DEFERRED_FKS", SQLITE_DBSTATUS_DEFERRED_FKS } + { "DEFERRED_FKS", SQLITE_DBSTATUS_DEFERRED_FKS }, + { "CACHE_USED_SHARED", SQLITE_DBSTATUS_CACHE_USED_SHARED }, + { "CACHE_SPILL", SQLITE_DBSTATUS_CACHE_SPILL }, + { "TEMPBUF_SPILL", SQLITE_DBSTATUS_TEMPBUF_SPILL }, }; Tcl_Obj *pResult; if( objc!=4 ){ @@ -1452,7 +1403,7 @@ static int test_db_status( /* ** install_malloc_faultsim BOOLEAN */ -static int test_install_malloc_faultsim( +static int SQLITE_TCLAPI test_install_malloc_faultsim( void * clientData, Tcl_Interp *interp, int objc, @@ -1476,7 +1427,7 @@ static int test_install_malloc_faultsim( /* ** sqlite3_install_memsys3 */ -static int test_install_memsys3( +static int SQLITE_TCLAPI test_install_memsys3( void * clientData, Tcl_Interp *interp, int objc, @@ -1491,7 +1442,7 @@ static int test_install_memsys3( return TCL_OK; } -static int test_vfs_oom_test( +static int SQLITE_TCLAPI test_vfs_oom_test( void * clientData, Tcl_Interp *interp, int objc, @@ -1533,7 +1484,6 @@ int Sqlitetest_malloc_Init(Tcl_Interp *interp){ { "sqlite3_memdebug_settitle", test_memdebug_settitle ,0 }, { "sqlite3_memdebug_malloc_count", test_memdebug_malloc_count ,0 }, { "sqlite3_memdebug_log", test_memdebug_log ,0 }, - { "sqlite3_config_scratch", test_config_scratch ,0 }, { "sqlite3_config_pagecache", test_config_pagecache ,0 }, { "sqlite3_config_alt_pcache", test_alt_pcache ,0 }, { "sqlite3_status", test_status ,0 }, diff --git a/src/test_md5.c b/src/test_md5.c new file mode 100644 index 0000000000..a09f1ad6c4 --- /dev/null +++ b/src/test_md5.c @@ -0,0 +1,443 @@ +/* +** 2017-10-13 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** This file contains code to implement an MD5 extension to TCL. +*/ +#include "sqlite3.h" +#include <stdlib.h> +#include <string.h> +#include "sqlite3.h" +#include "tclsqlite.h" + +/* + * This code implements the MD5 message-digest algorithm. + * The algorithm is due to Ron Rivest. This code was + * written by Colin Plumb in 1993, no copyright is claimed. + * This code is in the public domain; do with it what you wish. + * + * Equivalent code is available from RSA Data Security, Inc. + * This code has been tested against that, and is equivalent, + * except that you don't need to include two pages of legalese + * with every copy. + * + * To compute the message digest of a chunk of bytes, declare an + * MD5Context structure, pass it to MD5Init, call MD5Update as + * needed on buffers full of bytes, and then call MD5Final, which + * will fill a supplied 16-byte array with the digest. + */ + +/* + * If compiled on a machine that doesn't have a 32-bit integer, + * you just set "uint32" to the appropriate datatype for an + * unsigned 32-bit integer. For example: + * + * cc -Duint32='unsigned long' md5.c + * + */ +#ifndef uint32 +# define uint32 unsigned int +#endif + +struct MD5Context { + int isInit; + uint32 buf[4]; + uint32 bits[2]; + unsigned char in[64]; +}; +typedef struct MD5Context MD5Context; + +/* + * Note: this code is harmless on little-endian machines. + */ +static void byteReverse (unsigned char *buf, unsigned longs){ + uint32 t; + do { + t = (uint32)((unsigned)buf[3]<<8 | buf[2]) << 16 | + ((unsigned)buf[1]<<8 | buf[0]); + *(uint32 *)buf = t; + buf += 4; + } while (--longs); +} +/* The four core functions - F1 is optimized somewhat */ + +/* #define F1(x, y, z) (x & y | ~x & z) */ +#define F1(x, y, z) (z ^ (x & (y ^ z))) +#define F2(x, y, z) F1(z, x, y) +#define F3(x, y, z) (x ^ y ^ z) +#define F4(x, y, z) (y ^ (x | ~z)) + +/* This is the central step in the MD5 algorithm. */ +#define MD5STEP(f, w, x, y, z, data, s) \ + ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x ) + +/* + * The core of the MD5 algorithm, this alters an existing MD5 hash to + * reflect the addition of 16 longwords of new data. MD5Update blocks + * the data and converts bytes into longwords for this routine. + */ +static void MD5Transform(uint32 buf[4], const uint32 in[16]){ + register uint32 a, b, c, d; + + a = buf[0]; + b = buf[1]; + c = buf[2]; + d = buf[3]; + + MD5STEP(F1, a, b, c, d, in[ 0]+0xd76aa478, 7); + MD5STEP(F1, d, a, b, c, in[ 1]+0xe8c7b756, 12); + MD5STEP(F1, c, d, a, b, in[ 2]+0x242070db, 17); + MD5STEP(F1, b, c, d, a, in[ 3]+0xc1bdceee, 22); + MD5STEP(F1, a, b, c, d, in[ 4]+0xf57c0faf, 7); + MD5STEP(F1, d, a, b, c, in[ 5]+0x4787c62a, 12); + MD5STEP(F1, c, d, a, b, in[ 6]+0xa8304613, 17); + MD5STEP(F1, b, c, d, a, in[ 7]+0xfd469501, 22); + MD5STEP(F1, a, b, c, d, in[ 8]+0x698098d8, 7); + MD5STEP(F1, d, a, b, c, in[ 9]+0x8b44f7af, 12); + MD5STEP(F1, c, d, a, b, in[10]+0xffff5bb1, 17); + MD5STEP(F1, b, c, d, a, in[11]+0x895cd7be, 22); + MD5STEP(F1, a, b, c, d, in[12]+0x6b901122, 7); + MD5STEP(F1, d, a, b, c, in[13]+0xfd987193, 12); + MD5STEP(F1, c, d, a, b, in[14]+0xa679438e, 17); + MD5STEP(F1, b, c, d, a, in[15]+0x49b40821, 22); + + MD5STEP(F2, a, b, c, d, in[ 1]+0xf61e2562, 5); + MD5STEP(F2, d, a, b, c, in[ 6]+0xc040b340, 9); + MD5STEP(F2, c, d, a, b, in[11]+0x265e5a51, 14); + MD5STEP(F2, b, c, d, a, in[ 0]+0xe9b6c7aa, 20); + MD5STEP(F2, a, b, c, d, in[ 5]+0xd62f105d, 5); + MD5STEP(F2, d, a, b, c, in[10]+0x02441453, 9); + MD5STEP(F2, c, d, a, b, in[15]+0xd8a1e681, 14); + MD5STEP(F2, b, c, d, a, in[ 4]+0xe7d3fbc8, 20); + MD5STEP(F2, a, b, c, d, in[ 9]+0x21e1cde6, 5); + MD5STEP(F2, d, a, b, c, in[14]+0xc33707d6, 9); + MD5STEP(F2, c, d, a, b, in[ 3]+0xf4d50d87, 14); + MD5STEP(F2, b, c, d, a, in[ 8]+0x455a14ed, 20); + MD5STEP(F2, a, b, c, d, in[13]+0xa9e3e905, 5); + MD5STEP(F2, d, a, b, c, in[ 2]+0xfcefa3f8, 9); + MD5STEP(F2, c, d, a, b, in[ 7]+0x676f02d9, 14); + MD5STEP(F2, b, c, d, a, in[12]+0x8d2a4c8a, 20); + + MD5STEP(F3, a, b, c, d, in[ 5]+0xfffa3942, 4); + MD5STEP(F3, d, a, b, c, in[ 8]+0x8771f681, 11); + MD5STEP(F3, c, d, a, b, in[11]+0x6d9d6122, 16); + MD5STEP(F3, b, c, d, a, in[14]+0xfde5380c, 23); + MD5STEP(F3, a, b, c, d, in[ 1]+0xa4beea44, 4); + MD5STEP(F3, d, a, b, c, in[ 4]+0x4bdecfa9, 11); + MD5STEP(F3, c, d, a, b, in[ 7]+0xf6bb4b60, 16); + MD5STEP(F3, b, c, d, a, in[10]+0xbebfbc70, 23); + MD5STEP(F3, a, b, c, d, in[13]+0x289b7ec6, 4); + MD5STEP(F3, d, a, b, c, in[ 0]+0xeaa127fa, 11); + MD5STEP(F3, c, d, a, b, in[ 3]+0xd4ef3085, 16); + MD5STEP(F3, b, c, d, a, in[ 6]+0x04881d05, 23); + MD5STEP(F3, a, b, c, d, in[ 9]+0xd9d4d039, 4); + MD5STEP(F3, d, a, b, c, in[12]+0xe6db99e5, 11); + MD5STEP(F3, c, d, a, b, in[15]+0x1fa27cf8, 16); + MD5STEP(F3, b, c, d, a, in[ 2]+0xc4ac5665, 23); + + MD5STEP(F4, a, b, c, d, in[ 0]+0xf4292244, 6); + MD5STEP(F4, d, a, b, c, in[ 7]+0x432aff97, 10); + MD5STEP(F4, c, d, a, b, in[14]+0xab9423a7, 15); + MD5STEP(F4, b, c, d, a, in[ 5]+0xfc93a039, 21); + MD5STEP(F4, a, b, c, d, in[12]+0x655b59c3, 6); + MD5STEP(F4, d, a, b, c, in[ 3]+0x8f0ccc92, 10); + MD5STEP(F4, c, d, a, b, in[10]+0xffeff47d, 15); + MD5STEP(F4, b, c, d, a, in[ 1]+0x85845dd1, 21); + MD5STEP(F4, a, b, c, d, in[ 8]+0x6fa87e4f, 6); + MD5STEP(F4, d, a, b, c, in[15]+0xfe2ce6e0, 10); + MD5STEP(F4, c, d, a, b, in[ 6]+0xa3014314, 15); + MD5STEP(F4, b, c, d, a, in[13]+0x4e0811a1, 21); + MD5STEP(F4, a, b, c, d, in[ 4]+0xf7537e82, 6); + MD5STEP(F4, d, a, b, c, in[11]+0xbd3af235, 10); + MD5STEP(F4, c, d, a, b, in[ 2]+0x2ad7d2bb, 15); + MD5STEP(F4, b, c, d, a, in[ 9]+0xeb86d391, 21); + + buf[0] += a; + buf[1] += b; + buf[2] += c; + buf[3] += d; +} + +/* + * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious + * initialization constants. + */ +static void MD5Init(MD5Context *ctx){ + ctx->isInit = 1; + ctx->buf[0] = 0x67452301; + ctx->buf[1] = 0xefcdab89; + ctx->buf[2] = 0x98badcfe; + ctx->buf[3] = 0x10325476; + ctx->bits[0] = 0; + ctx->bits[1] = 0; +} + +/* + * Update context to reflect the concatenation of another buffer full + * of bytes. + */ +static +void MD5Update(MD5Context *ctx, const unsigned char *buf, unsigned int len){ + uint32 t; + + /* Update bitcount */ + + t = ctx->bits[0]; + if ((ctx->bits[0] = t + ((uint32)len << 3)) < t) + ctx->bits[1]++; /* Carry from low to high */ + ctx->bits[1] += len >> 29; + + t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */ + + /* Handle any leading odd-sized chunks */ + + if ( t ) { + unsigned char *p = (unsigned char *)ctx->in + t; + + t = 64-t; + if (len < t) { + memcpy(p, buf, len); + return; + } + memcpy(p, buf, t); + byteReverse(ctx->in, 16); + MD5Transform(ctx->buf, (uint32 *)ctx->in); + buf += t; + len -= t; + } + + /* Process data in 64-byte chunks */ + + while (len >= 64) { + memcpy(ctx->in, buf, 64); + byteReverse(ctx->in, 16); + MD5Transform(ctx->buf, (uint32 *)ctx->in); + buf += 64; + len -= 64; + } + + /* Handle any remaining bytes of data. */ + + memcpy(ctx->in, buf, len); +} + +/* + * Final wrapup - pad to 64-byte boundary with the bit pattern + * 1 0* (64-bit count of bits processed, MSB-first) + */ +static void MD5Final(unsigned char digest[16], MD5Context *ctx){ + unsigned count; + unsigned char *p; + + /* Compute number of bytes mod 64 */ + count = (ctx->bits[0] >> 3) & 0x3F; + + /* Set the first char of padding to 0x80. This is safe since there is + always at least one byte free */ + p = ctx->in + count; + *p++ = 0x80; + + /* Bytes of padding needed to make 64 bytes */ + count = 64 - 1 - count; + + /* Pad out to 56 mod 64 */ + if (count < 8) { + /* Two lots of padding: Pad the first block to 64 bytes */ + memset(p, 0, count); + byteReverse(ctx->in, 16); + MD5Transform(ctx->buf, (uint32 *)ctx->in); + + /* Now fill the next block with 56 bytes */ + memset(ctx->in, 0, 56); + } else { + /* Pad block to 56 bytes */ + memset(p, 0, count-8); + } + byteReverse(ctx->in, 14); + + /* Append length in bits and transform */ + memcpy(ctx->in + 14*4, ctx->bits, 8); + + MD5Transform(ctx->buf, (uint32 *)ctx->in); + byteReverse((unsigned char *)ctx->buf, 4); + memcpy(digest, ctx->buf, 16); +} + +/* +** Convert a 128-bit MD5 digest into a 32-digit base-16 number. +*/ +static void MD5DigestToBase16(unsigned char *digest, char *zBuf){ + static char const zEncode[] = "0123456789abcdef"; + int i, j; + + for(j=i=0; i<16; i++){ + int a = digest[i]; + zBuf[j++] = zEncode[(a>>4)&0xf]; + zBuf[j++] = zEncode[a & 0xf]; + } + zBuf[j] = 0; +} + + +/* +** Convert a 128-bit MD5 digest into sequences of eight 5-digit integers +** each representing 16 bits of the digest and separated from each +** other by a "-" character. +*/ +static void MD5DigestToBase10x8(unsigned char digest[16], char zDigest[50]){ + int i, j; + unsigned int x; + for(i=j=0; i<16; i+=2){ + x = digest[i]*256 + digest[i+1]; + if( i>0 ) zDigest[j++] = '-'; + sqlite3_snprintf(50-j, &zDigest[j], "%05u", x); + j += 5; + } + zDigest[j] = 0; +} + +/* +** A TCL command for md5. The argument is the text to be hashed. The +** Result is the hash in base64. +*/ +static int SQLITE_TCLAPI md5_cmd( + void*cd, + Tcl_Interp *interp, + int argc, + const char **argv +){ + MD5Context ctx; + unsigned char digest[16]; + char zBuf[50]; + void (*converter)(unsigned char*, char*); + + if( argc!=2 ){ + Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0], + " TEXT\"", (char*)0); + return TCL_ERROR; + } + MD5Init(&ctx); + MD5Update(&ctx, (unsigned char*)argv[1], (unsigned)strlen(argv[1])); + MD5Final(digest, &ctx); + converter = (void(*)(unsigned char*,char*))cd; + converter(digest, zBuf); + Tcl_AppendResult(interp, zBuf, (char*)0); + return TCL_OK; +} + +/* +** A TCL command to take the md5 hash of a file. The argument is the +** name of the file. +*/ +static int SQLITE_TCLAPI md5file_cmd( + void*cd, + Tcl_Interp *interp, + int argc, + const char **argv +){ + FILE *in; + int ofst; + int amt; + MD5Context ctx; + void (*converter)(unsigned char*, char*); + unsigned char digest[16]; + char zBuf[10240]; + + if( argc!=2 && argc!=4 ){ + Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0], + " FILENAME [OFFSET AMT]\"", (char*)0); + return TCL_ERROR; + } + if( argc==4 ){ + ofst = atoi(argv[2]); + amt = atoi(argv[3]); + }else{ + ofst = 0; + amt = 2147483647; + } + in = fopen(argv[1],"rb"); + if( in==0 ){ + Tcl_AppendResult(interp,"unable to open file \"", argv[1], + "\" for reading", (char*)0); + return TCL_ERROR; + } + fseek(in, ofst, SEEK_SET); + MD5Init(&ctx); + while( amt>0 ){ + int n; + n = (int)fread(zBuf, 1, sizeof(zBuf)<=amt ? sizeof(zBuf) : amt, in); + if( n<=0 ) break; + MD5Update(&ctx, (unsigned char*)zBuf, (unsigned)n); + amt -= n; + } + fclose(in); + MD5Final(digest, &ctx); + converter = (void(*)(unsigned char*,char*))cd; + converter(digest, zBuf); + Tcl_AppendResult(interp, zBuf, (char*)0); + return TCL_OK; +} + +/* +** Register the four new TCL commands for generating MD5 checksums +** with the TCL interpreter. +*/ +int Md5_Init(Tcl_Interp *interp){ + Tcl_CreateCommand(interp, "md5", (Tcl_CmdProc*)md5_cmd, + MD5DigestToBase16, 0); + Tcl_CreateCommand(interp, "md5-10x8", (Tcl_CmdProc*)md5_cmd, + MD5DigestToBase10x8, 0); + Tcl_CreateCommand(interp, "md5file", (Tcl_CmdProc*)md5file_cmd, + MD5DigestToBase16, 0); + Tcl_CreateCommand(interp, "md5file-10x8", (Tcl_CmdProc*)md5file_cmd, + MD5DigestToBase10x8, 0); + return TCL_OK; +} + +/* +** During testing, the special md5sum() aggregate function is available. +** inside SQLite. The following routines implement that function. +*/ +static void md5step(sqlite3_context *context, int argc, sqlite3_value **argv){ + MD5Context *p; + int i; + if( argc<1 ) return; + p = sqlite3_aggregate_context(context, sizeof(*p)); + if( p==0 ) return; + if( !p->isInit ){ + MD5Init(p); + } + for(i=0; i<argc; i++){ + const char *zData = (char*)sqlite3_value_text(argv[i]); + if( zData ){ + MD5Update(p, (unsigned char*)zData, (int)strlen(zData)); + } + } +} +static void md5finalize(sqlite3_context *context){ + MD5Context *p; + unsigned char digest[16]; + char zBuf[33]; + p = sqlite3_aggregate_context(context, sizeof(*p)); + MD5Final(digest,p); + MD5DigestToBase16(digest, zBuf); + sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); +} +int Md5_Register( + sqlite3 *db, + char **pzErrMsg, + const sqlite3_api_routines *pThunk +){ + int rc = sqlite3_create_function(db, "md5sum", -1, SQLITE_UTF8, 0, 0, + md5step, md5finalize); + sqlite3_overload_function(db, "md5sum", -1); /* To exercise this API */ + return rc; +} diff --git a/src/test_multiplex.c b/src/test_multiplex.c index 82845ea7e1..82551200f2 100644 --- a/src/test_multiplex.c +++ b/src/test_multiplex.c @@ -28,9 +28,9 @@ ** ** The procedure call above will create and register a new VFS shim named ** "multiplex". The multiplex VFS will use the VFS named by zOrigVfsName to -** do the actual disk I/O. (The zOrigVfsName parameter may be NULL, in +** do the actual disk I/O. (The zOrigVfsName parameter may be NULL, in ** which case the default VFS at the moment sqlite3_multiplex_initialize() -** is called will be used as the underlying real VFS.) +** is called will be used as the underlying real VFS.) ** ** If the makeDefault parameter is TRUE then multiplex becomes the new ** default VFS. Otherwise, you can use the multiplex VFS by specifying @@ -39,7 +39,7 @@ ** URI. ** ** The multiplex VFS allows databases up to 32 GiB in size. But it splits -** the files up into smaller pieces, so that they will work even on +** the files up into smaller pieces, so that they will work even on ** filesystems that do not support large files. The default chunk size ** is 2147418112 bytes (which is 64KiB less than 2GiB) but this can be ** changed at compile-time by defining the SQLITE_MULTIPLEX_CHUNK_SIZE @@ -58,29 +58,16 @@ #endif #include "sqlite3ext.h" -/* -** These should be defined to be the same as the values in +/* +** These should be defined to be the same as the values in ** sqliteInt.h. They are defined separately here so that -** the multiplex VFS shim can be built as a loadable +** the multiplex VFS shim can be built as a loadable ** module. */ #define UNUSED_PARAMETER(x) (void)(x) #define MAX_PAGE_SIZE 0x10000 #define DEFAULT_SECTOR_SIZE 0x1000 -/* -** For a build without mutexes, no-op the mutex calls. -*/ -#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE==0 -#define sqlite3_mutex_alloc(X) ((sqlite3_mutex*)8) -#define sqlite3_mutex_free(X) -#define sqlite3_mutex_enter(X) -#define sqlite3_mutex_try(X) SQLITE_OK -#define sqlite3_mutex_leave(X) -#define sqlite3_mutex_held(X) ((void)(X),1) -#define sqlite3_mutex_notheld(X) ((void)(X),1) -#endif /* SQLITE_THREADSAFE==0 */ - /* Maximum chunk number */ #define MX_CHUNK_NUMBER 299 @@ -96,7 +83,7 @@ #endif /* This is the limit on the chunk size. It may be changed by calling -** the xFileControl() interface. It will be rounded up to a +** the xFileControl() interface. It will be rounded up to a ** multiple of MAX_PAGE_SIZE. We default it here to 2GiB less 64KiB. */ #ifndef SQLITE_MULTIPLEX_CHUNK_SIZE @@ -139,12 +126,11 @@ struct multiplexGroup { unsigned int szChunk; /* Chunk size used for this group */ unsigned char bEnabled; /* TRUE to use Multiplex VFS for this file */ unsigned char bTruncate; /* TRUE to enable truncation of databases */ - multiplexGroup *pNext, *pPrev; /* Doubly linked list of all group objects */ }; /* ** An instance of the following object represents each open connection -** to a file that is multiplex'ed. This object is a +** to a file that is multiplex'ed. This object is a ** subclass of sqlite3_file. The sqlite3_file object for the underlying ** VFS is appended to this structure. */ @@ -171,11 +157,11 @@ static struct { */ sqlite3_vfs sThisVfs; - /* The sIoMethods defines the methods used by sqlite3_file objects + /* The sIoMethods defines the methods used by sqlite3_file objects ** associated with this shim. It is initialized at start-time and does ** not require a mutex. ** - ** When the underlying VFS is called to open a file, it might return + ** When the underlying VFS is called to open a file, it might return ** either a version 1 or a version 2 sqlite3_file object. This shim ** has to create a wrapper sqlite3_file of the same version. Hence ** there are two I/O method structures, one for version 1 and the other @@ -187,28 +173,9 @@ static struct { /* True when this shim has been initialized. */ int isInitialized; - - /* For run-time access any of the other global data structures in this - ** shim, the following mutex must be held. In practice, all this mutex - ** protects is add/remove operations to/from the linked list of group objects - ** starting at pGroups below. More specifically, it protects the value of - ** pGroups itself, and the pNext/pPrev fields of each multiplexGroup - ** structure. */ - sqlite3_mutex *pMutex; - - /* List of multiplexGroup objects. - */ - multiplexGroup *pGroups; } gMultiplex; /************************* Utility Routines *********************************/ -/* -** Acquire and release the mutex used to serialize access to the -** list of multiplexGroups. -*/ -static void multiplexEnter(void){ sqlite3_mutex_enter(gMultiplex.pMutex); } -static void multiplexLeave(void){ sqlite3_mutex_leave(gMultiplex.pMutex); } - /* ** Compute a string length that is limited to what can be stored in ** lower 30 bits of a 32-bit signed integer. @@ -232,16 +199,16 @@ static int multiplexStrlen30(const char *z){ ** nul-terminator. ** ** If iChunk is 0 (or 400 - the number for the first journal file chunk), -** the output is a copy of the input string. Otherwise, if +** the output is a copy of the input string. Otherwise, if ** SQLITE_ENABLE_8_3_NAMES is not defined or the input buffer does not contain -** a "." character, then the output is a copy of the input string with the -** three-digit zero-padded decimal representation if iChunk appended to it. +** a "." character, then the output is a copy of the input string with the +** three-digit zero-padded decimal representation if iChunk appended to it. ** For example: ** ** zBase="test.db", iChunk=4 -> zOut="test.db004" ** ** Or, if SQLITE_ENABLE_8_3_NAMES is defined and the input buffer contains -** a "." character, then everything after the "." is replaced by the +** a "." character, then everything after the "." is replaced by the ** three-digit representation of iChunk. ** ** zBase="test.db", iChunk=4 -> zOut="test.004" @@ -265,12 +232,12 @@ static void multiplexFilename( if( i>=n-4 ) n = i+1; if( flags & SQLITE_OPEN_MAIN_JOURNAL ){ /* The extensions on overflow files for main databases are 001, 002, - ** 003 and so forth. To avoid name collisions, add 400 to the + ** 003 and so forth. To avoid name collisions, add 400 to the ** extensions of journal files so that they are 401, 402, 403, .... */ iChunk += SQLITE_MULTIPLEX_JOURNAL_8_3_OFFSET; }else if( flags & SQLITE_OPEN_WAL ){ - /* To avoid name collisions, add 700 to the + /* To avoid name collisions, add 700 to the ** extensions of WAL files so that they are 701, 702, 703, .... */ iChunk += SQLITE_MULTIPLEX_WAL_8_3_OFFSET; @@ -300,11 +267,14 @@ static int multiplexSubFilename(multiplexGroup *pGroup, int iChunk){ if( pGroup->zName && pGroup->aReal[iChunk].z==0 ){ char *z; int n = pGroup->nName; - pGroup->aReal[iChunk].z = z = sqlite3_malloc64( n+5 ); + z = sqlite3_malloc64( n+5 ); if( z==0 ){ return SQLITE_NOMEM; } multiplexFilename(pGroup->zName, pGroup->nName, pGroup->flags, iChunk, z); + pGroup->aReal[iChunk].z = (char*)sqlite3_create_filename(z,"","",0,0); + sqlite3_free(z); + if( pGroup->aReal[iChunk].z==0 ) return SQLITE_NOMEM; } return SQLITE_OK; } @@ -327,8 +297,8 @@ static sqlite3_file *multiplexSubOpen( sqlite3_vfs *pOrigVfs = gMultiplex.pOrigVfs; /* Real VFS */ #ifdef SQLITE_ENABLE_8_3_NAMES - /* If JOURNAL_8_3_OFFSET is set to (say) 400, then any overflow files are - ** part of a database journal are named db.401, db.402, and so on. A + /* If JOURNAL_8_3_OFFSET is set to (say) 400, then any overflow files are + ** part of a database journal are named db.401, db.402, and so on. A ** database may therefore not grow to larger than 400 chunks. Attempting ** to open chunk 401 indicates the database is full. */ if( iChunk>=SQLITE_MULTIPLEX_JOURNAL_8_3_OFFSET ){ @@ -381,8 +351,8 @@ static sqlite3_file *multiplexSubOpen( /* ** Return the size, in bytes, of chunk number iChunk. If that chunk -** does not exist, then return 0. This function does not distingish between -** non-existant files and zero-length files. +** does not exist, then return 0. This function does not distinguish between +** non-existent files and zero-length files. */ static sqlite3_int64 multiplexSubSize( multiplexGroup *pGroup, /* The multiplexor group */ @@ -397,7 +367,7 @@ static sqlite3_int64 multiplexSubSize( if( pSub==0 ) return 0; *rc = pSub->pMethods->xFileSize(pSub, &sz); return sz; -} +} /* ** This is the implementation of the multiplex_control() SQL function. @@ -412,22 +382,22 @@ static void multiplexControlFunc( int op = 0; int iVal; - if( !db || argc!=2 ){ - rc = SQLITE_ERROR; + if( !db || argc!=2 ){ + rc = SQLITE_ERROR; }else{ /* extract params */ op = sqlite3_value_int(argv[0]); iVal = sqlite3_value_int(argv[1]); /* map function op to file_control op */ switch( op ){ - case 1: - op = MULTIPLEX_CTRL_ENABLE; + case 1: + op = MULTIPLEX_CTRL_ENABLE; break; - case 2: - op = MULTIPLEX_CTRL_SET_CHUNK_SIZE; + case 2: + op = MULTIPLEX_CTRL_SET_CHUNK_SIZE; break; - case 3: - op = MULTIPLEX_CTRL_SET_MAX_CHUNKS; + case 3: + op = MULTIPLEX_CTRL_SET_MAX_CHUNKS; break; default: rc = SQLITE_NOTFOUND; @@ -441,16 +411,16 @@ static void multiplexControlFunc( } /* -** This is the entry point to register the auto-extension for the +** This is the entry point to register the auto-extension for the ** multiplex_control() function. */ static int multiplexFuncInit( - sqlite3 *db, - char **pzErrMsg, + sqlite3 *db, + char **pzErrMsg, const sqlite3_api_routines *pApi ){ int rc; - rc = sqlite3_create_function(db, "multiplex_control", 2, SQLITE_ANY, + rc = sqlite3_create_function(db, "multiplex_control", 2, SQLITE_ANY, 0, multiplexControlFunc, 0, 0); return rc; } @@ -471,7 +441,7 @@ static void multiplexSubClose( } sqlite3_free(pGroup->aReal[iChunk].p); } - sqlite3_free(pGroup->aReal[iChunk].z); + sqlite3_free_filename(pGroup->aReal[iChunk].z); memset(&pGroup->aReal[iChunk], 0, sizeof(pGroup->aReal[iChunk])); } @@ -519,7 +489,6 @@ static int multiplexOpen( /* We need to create a group structure and manage ** access to this group of files. */ - multiplexEnter(); pMultiplexOpen = (multiplexConn*)pConn; if( rc==SQLITE_OK ){ @@ -539,7 +508,7 @@ static int multiplexOpen( memset(pGroup, 0, sz); pMultiplexOpen->pGroup = pGroup; pGroup->bEnabled = (unsigned char)-1; - pGroup->bTruncate = sqlite3_uri_boolean(zUri, "truncate", + pGroup->bTruncate = (unsigned char)sqlite3_uri_boolean(zUri, "truncate", (flags & SQLITE_OPEN_MAIN_DB)==0); pGroup->szChunk = (int)sqlite3_uri_int64(zUri, "chunksize", SQLITE_MULTIPLEX_CHUNK_SIZE); @@ -564,7 +533,7 @@ static int multiplexOpen( pGroup->szChunk += 65536; } } - pGroup->flags = flags; + pGroup->flags = (flags & ~SQLITE_OPEN_URI); rc = multiplexSubFilename(pGroup, 1); if( rc==SQLITE_OK ){ pSubOpen = multiplexSubOpen(pGroup, 0, &rc, pOutFlags, 0); @@ -576,17 +545,17 @@ static int multiplexOpen( rc = pSubOpen->pMethods->xFileSize(pSubOpen, &sz64); if( rc==SQLITE_OK && zName ){ int bExists; - if( flags & SQLITE_OPEN_MASTER_JOURNAL ){ + if( flags & SQLITE_OPEN_SUPER_JOURNAL ){ pGroup->bEnabled = 0; }else if( sz64==0 ){ if( flags & SQLITE_OPEN_MAIN_JOURNAL ){ /* If opening a main journal file and the first chunk is zero - ** bytes in size, delete any subsequent chunks from the + ** bytes in size, delete any subsequent chunks from the ** file-system. */ int iChunk = 1; do { - rc = pOrigVfs->xAccess(pOrigVfs, + rc = pOrigVfs->xAccess(pOrigVfs, pGroup->aReal[iChunk].z, SQLITE_ACCESS_EXISTS, &bExists ); if( rc==SQLITE_OK && bExists ){ @@ -604,8 +573,8 @@ static int multiplexOpen( ** ** Or, if the first overflow file does not exist and the main file is ** larger than the chunk size, that means the chunk size is too small. - ** But we have no way of determining the intended chunk size, so - ** just disable the multiplexor all togethre. + ** But we have no way of determining the intended chunk size, so + ** just disable the multiplexor all together. */ rc = pOrigVfs->xAccess(pOrigVfs, pGroup->aReal[1].z, SQLITE_ACCESS_EXISTS, &bExists); @@ -622,20 +591,15 @@ static int multiplexOpen( if( rc==SQLITE_OK ){ if( pSubOpen->pMethods->iVersion==1 ){ - pMultiplexOpen->base.pMethods = &gMultiplex.sIoMethodsV1; + pConn->pMethods = &gMultiplex.sIoMethodsV1; }else{ - pMultiplexOpen->base.pMethods = &gMultiplex.sIoMethodsV2; + pConn->pMethods = &gMultiplex.sIoMethodsV2; } - /* place this group at the head of our list */ - pGroup->pNext = gMultiplex.pGroups; - if( gMultiplex.pGroups ) gMultiplex.pGroups->pPrev = pGroup; - gMultiplex.pGroups = pGroup; }else{ multiplexFreeComponents(pGroup); sqlite3_free(pGroup); } } - multiplexLeave(); sqlite3_free(zToFree); return rc; } @@ -654,7 +618,7 @@ static int multiplexDelete( rc = pOrigVfs->xDelete(pOrigVfs, zName, syncDir); if( rc==SQLITE_OK ){ /* If the main chunk was deleted successfully, also delete any subsequent - ** chunks - starting with the last (highest numbered). + ** chunks - starting with the last (highest numbered). */ int nName = (int)strlen(zName); char *z; @@ -717,7 +681,11 @@ static int multiplexCurrentTime(sqlite3_vfs *a, double *b){ return gMultiplex.pOrigVfs->xCurrentTime(gMultiplex.pOrigVfs, b); } static int multiplexGetLastError(sqlite3_vfs *a, int b, char *c){ - return gMultiplex.pOrigVfs->xGetLastError(gMultiplex.pOrigVfs, b, c); + if( gMultiplex.pOrigVfs->xGetLastError ){ + return gMultiplex.pOrigVfs->xGetLastError(gMultiplex.pOrigVfs, b, c); + }else{ + return 0; + } } static int multiplexCurrentTimeInt64(sqlite3_vfs *a, sqlite3_int64 *b){ return gMultiplex.pOrigVfs->xCurrentTimeInt64(gMultiplex.pOrigVfs, b); @@ -727,24 +695,15 @@ static int multiplexCurrentTimeInt64(sqlite3_vfs *a, sqlite3_int64 *b){ /* xClose requests get passed through to the original VFS. ** We loop over all open chunk handles and close them. -** The group structure for this file is unlinked from +** The group structure for this file is unlinked from ** our list of groups and freed. */ static int multiplexClose(sqlite3_file *pConn){ multiplexConn *p = (multiplexConn*)pConn; multiplexGroup *pGroup = p->pGroup; int rc = SQLITE_OK; - multiplexEnter(); multiplexFreeComponents(pGroup); - /* remove from linked list */ - if( pGroup->pNext ) pGroup->pNext->pPrev = pGroup->pPrev; - if( pGroup->pPrev ){ - pGroup->pPrev->pNext = pGroup->pNext; - }else{ - gMultiplex.pGroups = pGroup->pNext; - } sqlite3_free(pGroup); - multiplexLeave(); return rc; } @@ -841,7 +800,6 @@ static int multiplexTruncate(sqlite3_file *pConn, sqlite3_int64 size){ multiplexConn *p = (multiplexConn*)pConn; multiplexGroup *pGroup = p->pGroup; int rc = SQLITE_OK; - multiplexEnter(); if( !pGroup->bEnabled ){ sqlite3_file *pSubOpen = multiplexSubOpen(pGroup, 0, &rc, NULL, 0); if( pSubOpen==0 ){ @@ -873,7 +831,6 @@ static int multiplexTruncate(sqlite3_file *pConn, sqlite3_int64 size){ } if( rc ) rc = SQLITE_IOERR_TRUNCATE; } - multiplexLeave(); return rc; } @@ -884,7 +841,6 @@ static int multiplexSync(sqlite3_file *pConn, int flags){ multiplexGroup *pGroup = p->pGroup; int rc = SQLITE_OK; int i; - multiplexEnter(); for(i=0; i<pGroup->nReal; i++){ sqlite3_file *pSubOpen = pGroup->aReal[i].p; if( pSubOpen ){ @@ -892,7 +848,6 @@ static int multiplexSync(sqlite3_file *pConn, int flags){ if( rc2!=SQLITE_OK ) rc = rc2; } } - multiplexLeave(); return rc; } @@ -904,7 +859,6 @@ static int multiplexFileSize(sqlite3_file *pConn, sqlite3_int64 *pSize){ multiplexGroup *pGroup = p->pGroup; int rc = SQLITE_OK; int i; - multiplexEnter(); if( !pGroup->bEnabled ){ sqlite3_file *pSubOpen = multiplexSubOpen(pGroup, 0, &rc, NULL, 0); if( pSubOpen==0 ){ @@ -920,7 +874,6 @@ static int multiplexFileSize(sqlite3_file *pConn, sqlite3_int64 *pSize){ *pSize = i*(sqlite3_int64)pGroup->szChunk + sz; } } - multiplexLeave(); return rc; } @@ -974,7 +927,7 @@ static int multiplexFileControl(sqlite3_file *pConn, int op, void *pArg){ case MULTIPLEX_CTRL_ENABLE: if( pArg ) { int bEnabled = *(int *)pArg; - pGroup->bEnabled = bEnabled; + pGroup->bEnabled = (unsigned char)bEnabled; rc = SQLITE_OK; } break; @@ -1009,26 +962,79 @@ static int multiplexFileControl(sqlite3_file *pConn, int op, void *pArg){ ** element is the argument to the pragma or NULL if the pragma has no ** argument. */ - if( aFcntl[1] && sqlite3_stricmp(aFcntl[1],"multiplex_truncate")==0 ){ - if( aFcntl[2] && aFcntl[2][0] ){ - if( sqlite3_stricmp(aFcntl[2], "on")==0 - || sqlite3_stricmp(aFcntl[2], "1")==0 ){ - pGroup->bTruncate = 1; - }else - if( sqlite3_stricmp(aFcntl[2], "off")==0 - || sqlite3_stricmp(aFcntl[2], "0")==0 ){ - pGroup->bTruncate = 0; + if( aFcntl[1] && sqlite3_strnicmp(aFcntl[1],"multiplex_",10)==0 ){ + sqlite3_int64 sz = 0; + (void)multiplexFileSize(pConn, &sz); + /* + ** PRAGMA multiplex_truncate=BOOLEAN; + ** PRAGMA multiplex_truncate; + ** + ** Turn the multiplexor truncate feature on or off. Return either + ** "on" or "off" to indicate the new setting. If the BOOLEAN argument + ** is omitted, just return the current value for the truncate setting. + */ + if( sqlite3_stricmp(aFcntl[1],"multiplex_truncate")==0 ){ + if( aFcntl[2] && aFcntl[2][0] ){ + if( sqlite3_stricmp(aFcntl[2], "on")==0 + || sqlite3_stricmp(aFcntl[2], "1")==0 ){ + pGroup->bTruncate = 1; + }else + if( sqlite3_stricmp(aFcntl[2], "off")==0 + || sqlite3_stricmp(aFcntl[2], "0")==0 ){ + pGroup->bTruncate = 0; + } } + /* EVIDENCE-OF: R-27806-26076 The handler for an SQLITE_FCNTL_PRAGMA + ** file control can optionally make the first element of the char** + ** argument point to a string obtained from sqlite3_mprintf() or the + ** equivalent and that string will become the result of the pragma + ** or the error message if the pragma fails. + */ + aFcntl[0] = sqlite3_mprintf(pGroup->bTruncate ? "on" : "off"); + rc = SQLITE_OK; + break; } - /* EVIDENCE-OF: R-27806-26076 The handler for an SQLITE_FCNTL_PRAGMA - ** file control can optionally make the first element of the char** - ** argument point to a string obtained from sqlite3_mprintf() or the - ** equivalent and that string will become the result of the pragma - ** or the error message if the pragma fails. + /* + ** PRAGMA multiplex_enabled; + ** + ** Return 0 or 1 depending on whether the multiplexor is enabled or + ** disabled, respectively. */ - aFcntl[0] = sqlite3_mprintf(pGroup->bTruncate ? "on" : "off"); - rc = SQLITE_OK; - break; + if( sqlite3_stricmp(aFcntl[1],"multiplex_enabled")==0 ){ + aFcntl[0] = sqlite3_mprintf("%d", pGroup->bEnabled!=0); + rc = SQLITE_OK; + break; + } + /* + ** PRAGMA multiplex_chunksize; + ** + ** Return the chunksize for the multiplexor, or no-op if the + ** multiplexor is not active. + */ + if( sqlite3_stricmp(aFcntl[1],"multiplex_chunksize")==0 + && pGroup->bEnabled + ){ + aFcntl[0] = sqlite3_mprintf("%u", pGroup->szChunk); + rc = SQLITE_OK; + break; + } + /* + ** PRAGMA multiplex_filecount; + ** + ** Return the number of disk files currently in use by the + ** multiplexor. This should be the total database size size + ** divided by the chunksize and rounded up. + */ + if( sqlite3_stricmp(aFcntl[1],"multiplex_filecount")==0 ){ + int n = 0; + int ii; + for(ii=0; ii<pGroup->nReal; ii++){ + if( pGroup->aReal[ii].p!=0 ) n++; + } + aFcntl[0] = sqlite3_mprintf("%d", n); + rc = SQLITE_OK; + break; + } } /* If the multiplexor does not handle the pragma, pass it through ** into the default case. */ @@ -1132,8 +1138,8 @@ static int multiplexShmUnmap(sqlite3_file *pConn, int deleteFlag){ /* ** CAPI: Initialize the multiplex VFS shim - sqlite3_multiplex_initialize() ** -** Use the VFS named zOrigVfsName as the VFS that does the actual work. -** Use the default if zOrigVfsName==NULL. +** Use the VFS named zOrigVfsName as the VFS that does the actual work. +** Use the default if zOrigVfsName==NULL. ** ** The multiplex VFS shim is named "multiplex". It will become the default ** VFS if makeDefault is non-zero. @@ -1147,11 +1153,6 @@ int sqlite3_multiplex_initialize(const char *zOrigVfsName, int makeDefault){ pOrigVfs = sqlite3_vfs_find(zOrigVfsName); if( pOrigVfs==0 ) return SQLITE_ERROR; assert( pOrigVfs!=&gMultiplex.sThisVfs ); - gMultiplex.pMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); - if( !gMultiplex.pMutex ){ - return SQLITE_NOMEM; - } - gMultiplex.pGroups = NULL; gMultiplex.isInitialized = 1; gMultiplex.pOrigVfs = pOrigVfs; gMultiplex.sThisVfs = *pOrigVfs; @@ -1193,7 +1194,7 @@ int sqlite3_multiplex_initialize(const char *zOrigVfsName, int makeDefault){ gMultiplex.sIoMethodsV2.xShmUnmap = multiplexShmUnmap; sqlite3_vfs_register(&gMultiplex.sThisVfs, makeDefault); - sqlite3_auto_extension((void*)multiplexFuncInit); + sqlite3_auto_extension((void(*)(void))multiplexFuncInit); return SQLITE_OK; } @@ -1210,14 +1211,7 @@ int sqlite3_multiplex_initialize(const char *zOrigVfsName, int makeDefault){ int sqlite3_multiplex_shutdown(int eForce){ int rc = SQLITE_OK; if( gMultiplex.isInitialized==0 ) return SQLITE_MISUSE; - if( gMultiplex.pGroups ){ - sqlite3_log(SQLITE_MISUSE, "sqlite3_multiplex_shutdown() called " - "while database connections are still open"); - if( !eForce ) return SQLITE_MISUSE; - rc = SQLITE_MISUSE; - } gMultiplex.isInitialized = 0; - sqlite3_mutex_free(gMultiplex.pMutex); sqlite3_vfs_unregister(&gMultiplex.sThisVfs); memset(&gMultiplex, 0, sizeof(gMultiplex)); return rc; @@ -1225,14 +1219,14 @@ int sqlite3_multiplex_shutdown(int eForce){ /***************************** Test Code ***********************************/ #ifdef SQLITE_TEST -#include <tcl.h> +#include "tclsqlite.h" extern const char *sqlite3ErrName(int); /* ** tclcmd: sqlite3_multiplex_initialize NAME MAKEDEFAULT */ -static int test_multiplex_initialize( +static int SQLITE_TCLAPI test_multiplex_initialize( void * clientData, Tcl_Interp *interp, int objc, @@ -1263,7 +1257,7 @@ static int test_multiplex_initialize( /* ** tclcmd: sqlite3_multiplex_shutdown */ -static int test_multiplex_shutdown( +static int SQLITE_TCLAPI test_multiplex_shutdown( void * clientData, Tcl_Interp *interp, int objc, @@ -1288,65 +1282,10 @@ static int test_multiplex_shutdown( return TCL_OK; } -/* -** tclcmd: sqlite3_multiplex_dump -*/ -static int test_multiplex_dump( - void * clientData, - Tcl_Interp *interp, - int objc, - Tcl_Obj *CONST objv[] -){ - Tcl_Obj *pResult; - Tcl_Obj *pGroupTerm; - multiplexGroup *pGroup; - int i; - int nChunks = 0; - - UNUSED_PARAMETER(clientData); - UNUSED_PARAMETER(objc); - UNUSED_PARAMETER(objv); - - pResult = Tcl_NewObj(); - multiplexEnter(); - for(pGroup=gMultiplex.pGroups; pGroup; pGroup=pGroup->pNext){ - pGroupTerm = Tcl_NewObj(); - - if( pGroup->zName ){ - pGroup->zName[pGroup->nName] = '\0'; - Tcl_ListObjAppendElement(interp, pGroupTerm, - Tcl_NewStringObj(pGroup->zName, -1)); - }else{ - Tcl_ListObjAppendElement(interp, pGroupTerm, Tcl_NewObj()); - } - Tcl_ListObjAppendElement(interp, pGroupTerm, - Tcl_NewIntObj(pGroup->nName)); - Tcl_ListObjAppendElement(interp, pGroupTerm, - Tcl_NewIntObj(pGroup->flags)); - - /* count number of chunks with open handles */ - for(i=0; i<pGroup->nReal; i++){ - if( pGroup->aReal[i].p!=0 ) nChunks++; - } - Tcl_ListObjAppendElement(interp, pGroupTerm, - Tcl_NewIntObj(nChunks)); - - Tcl_ListObjAppendElement(interp, pGroupTerm, - Tcl_NewIntObj(pGroup->szChunk)); - Tcl_ListObjAppendElement(interp, pGroupTerm, - Tcl_NewIntObj(pGroup->nReal)); - - Tcl_ListObjAppendElement(interp, pResult, pGroupTerm); - } - multiplexLeave(); - Tcl_SetObjResult(interp, pResult); - return TCL_OK; -} - /* ** Tclcmd: test_multiplex_control HANDLE DBNAME SUB-COMMAND ?INT-VALUE? */ -static int test_multiplex_control( +static int SQLITE_TCLAPI test_multiplex_control( ClientData cd, Tcl_Interp *interp, int objc, @@ -1376,8 +1315,8 @@ static int test_multiplex_control( } if( 0==Tcl_GetCommandInfo(interp, Tcl_GetString(objv[1]), &cmdInfo) ){ - Tcl_AppendResult(interp, "expected database handle, got \"", 0); - Tcl_AppendResult(interp, Tcl_GetString(objv[1]), "\"", 0); + Tcl_AppendResult(interp, "expected database handle, got \"", NULL); + Tcl_AppendResult(interp, Tcl_GetString(objv[1]), "\"", NULL); return TCL_ERROR; }else{ db = *(sqlite3 **)cmdInfo.objClientData; @@ -1417,7 +1356,6 @@ int Sqlitemultiplex_Init(Tcl_Interp *interp){ } aCmd[] = { { "sqlite3_multiplex_initialize", test_multiplex_initialize }, { "sqlite3_multiplex_shutdown", test_multiplex_shutdown }, - { "sqlite3_multiplex_dump", test_multiplex_dump }, { "sqlite3_multiplex_control", test_multiplex_control }, }; int i; diff --git a/src/test_multiplex.h b/src/test_multiplex.h index d973e4af27..065fac7ea9 100644 --- a/src/test_multiplex.h +++ b/src/test_multiplex.h @@ -13,15 +13,15 @@ ** This file contains a VFS "shim" - a layer that sits in between the ** pager and the real VFS. ** -** This particular shim enforces a multiplex system on DB files. -** This shim shards/partitions a single DB file into smaller +** This particular shim enforces a multiplex system on DB files. +** This shim shards/partitions a single DB file into smaller ** "chunks" such that the total DB file size may exceed the maximum ** file size of the underlying file system. ** */ -#ifndef _TEST_MULTIPLEX_H -#define _TEST_MULTIPLEX_H +#ifndef SQLITE_TEST_MULTIPLEX_H +#define SQLITE_TEST_MULTIPLEX_H /* ** CAPI: File-control Operations Supported by Multiplex VFS @@ -33,14 +33,14 @@ ** shim. ** ** MULTIPLEX_CTRL_SET_CHUNK_SIZE: -** This file control is used to set the maximum allowed chunk -** size for a multiplex file set. The chunk size should be +** This file control is used to set the maximum allowed chunk +** size for a multiplex file set. The chunk size should be ** a multiple of SQLITE_MAX_PAGE_SIZE, and will be rounded up ** if not. ** ** MULTIPLEX_CTRL_SET_MAX_CHUNKS: ** This file control is used to set the maximum number of chunks -** allowed to be used for a mutliplex file set. +** allowed to be used for a multiplex file set. */ #define MULTIPLEX_CTRL_ENABLE 214014 #define MULTIPLEX_CTRL_SET_CHUNK_SIZE 214015 @@ -53,26 +53,26 @@ extern "C" { /* ** CAPI: Initialize the multiplex VFS shim - sqlite3_multiplex_initialize() ** -** Use the VFS named zOrigVfsName as the VFS that does the actual work. -** Use the default if zOrigVfsName==NULL. +** Use the VFS named zOrigVfsName as the VFS that does the actual work. +** Use the default if zOrigVfsName==NULL. ** ** The multiplex VFS shim is named "multiplex". It will become the default ** VFS if makeDefault is non-zero. ** -** An auto-extension is registered which will make the function +** An auto-extension is registered which will make the function ** multiplex_control() available to database connections. This -** function gives access to the xFileControl interface of the +** function gives access to the xFileControl interface of the ** multiplex VFS shim. ** ** SELECT multiplex_control(<op>,<val>); -** +** ** <op>=1 MULTIPLEX_CTRL_ENABLE ** <val>=0 disable ** <val>=1 enable -** +** ** <op>=2 MULTIPLEX_CTRL_SET_CHUNK_SIZE ** <val> int, chunk size -** +** ** <op>=3 MULTIPLEX_CTRL_SET_MAX_CHUNKS ** <val> int, max chunks ** @@ -96,4 +96,4 @@ extern int sqlite3_multiplex_shutdown(int eForce); } /* End of the 'extern "C"' block */ #endif -#endif /* _TEST_MULTIPLEX_H */ +#endif /* SQLITE_TEST_MULTIPLEX_H */ diff --git a/src/test_mutex.c b/src/test_mutex.c index 995b89a4c6..de064de4c4 100644 --- a/src/test_mutex.c +++ b/src/test_mutex.c @@ -11,8 +11,7 @@ ************************************************************************* ** This file contains test logic for the sqlite3_mutex interfaces. */ - -#include "tcl.h" +#include "tclsqlite.h" #include "sqlite3.h" #include "sqliteInt.h" #include <stdlib.h> @@ -26,7 +25,7 @@ extern const char *sqlite3ErrName(int); static const char *aName[MAX_MUTEXES+1] = { - "fast", "recursive", "static_master", "static_mem", + "fast", "recursive", "static_main", "static_mem", "static_open", "static_prng", "static_lru", "static_pmem", "static_app1", "static_app2", "static_app3", "static_vfs1", "static_vfs2", "static_vfs3", 0 @@ -41,7 +40,7 @@ struct sqlite3_mutex { /* State variables */ static struct test_mutex_globals { int isInstalled; /* True if installed */ - int disableInit; /* True to cause sqlite3_initalize() to fail */ + int disableInit; /* True to cause sqlite3_initialize() to fail */ int disableTry; /* True to force sqlite3_mutex_try() to fail */ int isInit; /* True if initialized */ sqlite3_mutex_methods m; /* Interface to "real" mutex system */ @@ -152,7 +151,7 @@ static void counterMutexLeave(sqlite3_mutex *p){ /* ** sqlite3_shutdown */ -static int test_shutdown( +static int SQLITE_TCLAPI test_shutdown( void * clientData, Tcl_Interp *interp, int objc, @@ -173,7 +172,7 @@ static int test_shutdown( /* ** sqlite3_initialize */ -static int test_initialize( +static int SQLITE_TCLAPI test_initialize( void * clientData, Tcl_Interp *interp, int objc, @@ -194,7 +193,7 @@ static int test_initialize( /* ** install_mutex_counters BOOLEAN */ -static int test_install_mutex_counters( +static int SQLITE_TCLAPI test_install_mutex_counters( void * clientData, Tcl_Interp *interp, int objc, @@ -226,8 +225,8 @@ static int test_install_mutex_counters( assert(isInstall==0 || isInstall==1); assert(g.isInstalled==0 || g.isInstalled==1); if( isInstall==g.isInstalled ){ - Tcl_AppendResult(interp, "mutex counters are ", 0); - Tcl_AppendResult(interp, isInstall?"already installed":"not installed", 0); + Tcl_AppendResult(interp, "mutex counters are ", NULL); + Tcl_AppendResult(interp, isInstall?"already installed":"not installed", NULL); return TCL_ERROR; } @@ -255,7 +254,7 @@ static int test_install_mutex_counters( /* ** read_mutex_counters */ -static int test_read_mutex_counters( +static int SQLITE_TCLAPI test_read_mutex_counters( void * clientData, Tcl_Interp *interp, int objc, @@ -284,7 +283,7 @@ static int test_read_mutex_counters( /* ** clear_mutex_counters */ -static int test_clear_mutex_counters( +static int SQLITE_TCLAPI test_clear_mutex_counters( void * clientData, Tcl_Interp *interp, int objc, @@ -308,7 +307,7 @@ static int test_clear_mutex_counters( ** will be invalid since the mutex has already been freed. The ** return pointer just checks to see if the mutex really was allocated. */ -static int test_alloc_mutex( +static int SQLITE_TCLAPI test_alloc_mutex( void * clientData, Tcl_Interp *interp, int objc, @@ -335,7 +334,7 @@ static int test_alloc_mutex( ** ** Or OPTION can be an raw integer. */ -static int test_config( +static int SQLITE_TCLAPI test_config( void * clientData, Tcl_Interp *interp, int objc, @@ -397,7 +396,7 @@ static sqlite3_mutex *getStaticMutexPointer( return counterMutexAlloc(iMutex); } -static int test_enter_static_mutex( +static int SQLITE_TCLAPI test_enter_static_mutex( void * clientData, Tcl_Interp *interp, int objc, @@ -416,7 +415,7 @@ static int test_enter_static_mutex( return TCL_OK; } -static int test_leave_static_mutex( +static int SQLITE_TCLAPI test_leave_static_mutex( void * clientData, Tcl_Interp *interp, int objc, @@ -435,7 +434,7 @@ static int test_leave_static_mutex( return TCL_OK; } -static int test_enter_db_mutex( +static int SQLITE_TCLAPI test_enter_db_mutex( void * clientData, Tcl_Interp *interp, int objc, @@ -454,7 +453,7 @@ static int test_enter_db_mutex( return TCL_OK; } -static int test_leave_db_mutex( +static int SQLITE_TCLAPI test_leave_db_mutex( void * clientData, Tcl_Interp *interp, int objc, diff --git a/src/test_onefile.c b/src/test_onefile.c index 122be700e6..037f4fb3ab 100644 --- a/src/test_onefile.c +++ b/src/test_onefile.c @@ -508,7 +508,7 @@ static int fsSync(sqlite3_file *pFile, int flags){ if( p->eType==DATABASE_FILE ){ unsigned char zSize[4]; zSize[0] = (pReal->nDatabase&0xFF000000)>>24; - zSize[1] = (pReal->nDatabase&0x00FF0000)>>16; + zSize[1] = (unsigned char)((pReal->nDatabase&0x00FF0000)>>16); zSize[2] = (pReal->nDatabase&0x0000FF00)>>8; zSize[3] = (pReal->nDatabase&0x000000FF); rc = pRealFile->pMethods->xWrite(pRealFile, zSize, 4, 0); @@ -560,6 +560,7 @@ static int fsCheckReservedLock(sqlite3_file *pFile, int *pResOut){ ** File control method. For custom operations on an fs-file. */ static int fsFileControl(sqlite3_file *pFile, int op, void *pArg){ + if( op==SQLITE_FCNTL_PRAGMA ) return SQLITE_NOTFOUND; return SQLITE_OK; } diff --git a/src/test_osinst.c b/src/test_osinst.c index 4ae23a87c6..e776d89e55 100644 --- a/src/test_osinst.c +++ b/src/test_osinst.c @@ -71,9 +71,8 @@ #include "sqlite3.h" -#include "os_setup.h" -#if SQLITE_OS_WIN -# include "os_win.h" +#ifdef _WIN32 +#include <windows.h> #endif #include <string.h> @@ -219,14 +218,7 @@ static sqlite3_io_methods vfslog_io_methods = { vfslogShmUnmap /* xShmUnmap */ }; -#if SQLITE_OS_UNIX && !defined(NO_GETTOD) -#include <sys/time.h> -static sqlite3_uint64 vfslog_time(){ - struct timeval sTime; - gettimeofday(&sTime, 0); - return sTime.tv_usec + (sqlite3_uint64)sTime.tv_sec * 1000000; -} -#elif SQLITE_OS_WIN +#ifdef _WIN32 #include <time.h> static sqlite3_uint64 vfslog_time(){ FILETIME ft; @@ -241,6 +233,13 @@ static sqlite3_uint64 vfslog_time(){ /* ft is 100-nanosecond intervals, we want microseconds */ return u64time /(sqlite3_uint64)10; } +#elif !defined(NO_GETTOD) +#include <sys/time.h> +static sqlite3_uint64 vfslog_time(){ + struct timeval sTime; + gettimeofday(&sTime, 0); + return sTime.tv_usec + (sqlite3_uint64)sTime.tv_sec * 1000000; +} #else static sqlite3_uint64 vfslog_time(){ return 0; @@ -644,9 +643,9 @@ static void vfslog_flush(VfslogVfs *p){ static void put32bits(unsigned char *p, unsigned int v){ p[0] = v>>24; - p[1] = v>>16; - p[2] = v>>8; - p[3] = v; + p[1] = (unsigned char)(v>>16); + p[2] = (unsigned char)(v>>8); + p[3] = (unsigned char)v; } static void vfslog_call( @@ -740,7 +739,7 @@ int sqlite3_vfslog_new( zFile = (char *)&p->base.zName[nVfs+1]; pParent->xFullPathname(pParent, zLog, pParent->mxPathname, zFile); - flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_MASTER_JOURNAL; + flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_SUPER_JOURNAL; pParent->xDelete(pParent, zFile, 0); rc = pParent->xOpen(pParent, zFile, p->pLog, flags, &flags); if( rc==SQLITE_OK ){ @@ -893,7 +892,7 @@ static int vlogConnect( pVfs->xFullPathname(pVfs, zFile, pVfs->mxPathname, p->zFile); sqlite3_free(zFile); - flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MASTER_JOURNAL; + flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_SUPER_JOURNAL; rc = pVfs->xOpen(pVfs, p->zFile, p->pFd, flags, &flags); if( rc==SQLITE_OK ){ @@ -1090,7 +1089,12 @@ int sqlite3_vfslog_register(sqlite3 *db){ 0, /* xRollback */ 0, /* xFindMethod */ 0, /* xRename */ - }; + 0, /* xSavepoint */ + 0, /* xRelease */ + 0, /* xRollbackTo */ + 0, /* xShadowName */ + 0 /* xIntegrity */ + }; sqlite3_create_module(db, "vfslog", &vfslog_module, 0); return SQLITE_OK; @@ -1104,9 +1108,9 @@ int sqlite3_vfslog_register(sqlite3 *db){ #if defined(SQLITE_TEST) || defined(TCLSH) -#include <tcl.h> +#include "tclsqlite.h" -static int test_vfslog( +static int SQLITE_TCLAPI test_vfslog( void *clientData, Tcl_Interp *interp, int objc, @@ -1141,7 +1145,7 @@ static int test_vfslog( zMsg = Tcl_GetString(objv[3]); rc = sqlite3_vfslog_annotate(zVfs, zMsg); if( rc!=SQLITE_OK ){ - Tcl_AppendResult(interp, "failed", 0); + Tcl_AppendResult(interp, "failed", (char*)0); return TCL_ERROR; } break; @@ -1155,7 +1159,7 @@ static int test_vfslog( zVfs = Tcl_GetString(objv[2]); rc = sqlite3_vfslog_finalize(zVfs); if( rc!=SQLITE_OK ){ - Tcl_AppendResult(interp, "failed", 0); + Tcl_AppendResult(interp, "failed", (char*)0); return TCL_ERROR; } break; @@ -1175,7 +1179,7 @@ static int test_vfslog( if( *zParent=='\0' ) zParent = 0; rc = sqlite3_vfslog_new(zVfs, zParent, zLog); if( rc!=SQLITE_OK ){ - Tcl_AppendResult(interp, "failed", 0); + Tcl_AppendResult(interp, "failed", (char*)0); return TCL_ERROR; } break; diff --git a/src/test_pcache.c b/src/test_pcache.c index 8fcfe7e26e..ceefa13e57 100644 --- a/src/test_pcache.c +++ b/src/test_pcache.c @@ -9,17 +9,17 @@ ** May you share freely, never taking more than you give. ** ************************************************************************* -** +** ** This file contains code used for testing the SQLite system. ** None of the code in this file goes into a deliverable build. -** +** ** This file contains an application-defined pager cache ** implementation that can be plugged in in place of the ** default pcache. This alternative pager cache will throw ** some errors that the default cache does not. ** ** This pagecache implementation is designed for simplicity -** not speed. +** not speed. */ #include "sqlite3.h" #include <string.h> @@ -36,7 +36,7 @@ struct testpcacheGlobalType { int nInstance; /* Number of current instances */ unsigned discardChance; /* Chance of discarding on an unpin (0-100) */ unsigned prngSeed; /* Seed for the PRNG */ - unsigned highStress; /* Call xStress agressively */ + unsigned highStress; /* Call xStress aggressively */ }; static testpcacheGlobalType testpcacheGlobal; @@ -99,7 +99,7 @@ static void testpcacheShutdown(void *pArg){ */ typedef struct testpcache testpcache; struct testpcache { - int szPage; /* Size of each page. Multiple of 8. */ + sqlite3_int64 szPage; /* Size of each page. Multiple of 8. */ int szExtra; /* Size of extra data that accompanies each page */ int bPurgeable; /* True if the page cache is purgeable */ int nFree; /* Number of unused slots in a[] */ @@ -131,8 +131,8 @@ static unsigned testpcacheRandom(testpcache *p){ ** Allocate a new page cache instance. */ static sqlite3_pcache *testpcacheCreate( - int szPage, - int szExtra, + int szPage, + int szExtra, int bPurgeable ){ int nMem; @@ -141,6 +141,7 @@ static sqlite3_pcache *testpcacheCreate( int i; assert( testpcacheGlobal.pDummy!=0 ); szPage = (szPage+7)&~7; + szExtra = (szPage+7)&~7; nMem = sizeof(testpcache) + TESTPCACHE_NPAGE*(szPage+szExtra); p = sqlite3_malloc( nMem ); if( p==0 ) return 0; @@ -225,7 +226,7 @@ static sqlite3_pcache_page *testpcacheFetch( return 0; } - /* Do not allocate if highStress is enabled and createFlag is not 2. + /* Do not allocate if highStress is enabled and createFlag is not 2. ** ** The highStress setting causes pagerStress() to be called much more ** often, which exercises the pager logic more intensely. @@ -428,7 +429,7 @@ void installTestPCache( int installFlag, /* True to install. False to uninstall. */ unsigned discardChance, /* 0-100. Chance to discard on unpin */ unsigned prngSeed, /* Seed for the PRNG */ - unsigned highStress /* Call xStress agressively */ + unsigned highStress /* Call xStress aggressively */ ){ static const sqlite3_pcache_methods2 testPcache = { 1, diff --git a/src/test_quota.c b/src/test_quota.c index e8e0b34072..d2f9cddd11 100644 --- a/src/test_quota.c +++ b/src/test_quota.c @@ -44,14 +44,12 @@ #define sqlite3_mutex_notheld(X) ((void)(X),1) #endif /* SQLITE_THREADSAFE==0 */ -#include "os_setup.h" -#if SQLITE_OS_UNIX -# include <unistd.h> -#endif -#if SQLITE_OS_WIN -# include "os_win.h" +#ifdef _WIN32 +# include <windows.h> # include <io.h> +#else +# include <unistd.h> #endif @@ -111,7 +109,7 @@ struct quotaFile { /* ** An instance of the following object represents each open connection -** to a file that participates in quota tracking. This object is a +** to a file that participates in quota tracking. This object is a ** subclass of sqlite3_file. The sqlite3_file object for the underlying ** VFS is appended to this structure. */ @@ -130,7 +128,7 @@ struct quota_FILE { FILE *f; /* Open stdio file pointer */ sqlite3_int64 iOfst; /* Current offset into the file */ quotaFile *pFile; /* The file record in the quota system */ -#if SQLITE_OS_WIN +#ifdef _WIN32 char *zMbcsName; /* Full MBCS pathname of the file */ #endif }; @@ -154,11 +152,11 @@ static struct { */ sqlite3_vfs sThisVfs; - /* The sIoMethods defines the methods used by sqlite3_file objects + /* The sIoMethods defines the methods used by sqlite3_file objects ** associated with this shim. It is initialized at start-time and does ** not require a mutex. ** - ** When the underlying VFS is called to open a file, it might return + ** When the underlying VFS is called to open a file, it might return ** either a version 1 or a version 2 sqlite3_file object. This shim ** has to create a wrapper sqlite3_file of the same version. Hence ** there are two I/O method structures, one for version 1 and the other @@ -190,7 +188,7 @@ static struct { static void quotaEnter(void){ sqlite3_mutex_enter(gQuota.pMutex); } static void quotaLeave(void){ sqlite3_mutex_leave(gQuota.pMutex); } -/* Count the number of open files in a quotaGroup +/* Count the number of open files in a quotaGroup */ static int quotaGroupOpenFileCount(quotaGroup *pGroup){ int N = 0; @@ -375,7 +373,7 @@ static quotaFile *quotaFindFile( ** used to store the returned pointer when done. */ static char *quota_utf8_to_mbcs(const char *zUtf8){ -#if SQLITE_OS_WIN +#ifdef _WIN32 size_t n; /* Bytes in zUtf8 */ int nWide; /* number of UTF-16 characters */ int nMbcs; /* Bytes of MBCS */ @@ -389,7 +387,11 @@ static char *quota_utf8_to_mbcs(const char *zUtf8){ zTmpWide = (LPWSTR)sqlite3_malloc( (nWide+1)*sizeof(zTmpWide[0]) ); if( zTmpWide==0 ) return 0; MultiByteToWideChar(CP_UTF8, 0, zUtf8, -1, zTmpWide, nWide); +#ifdef SQLITE_OS_WINRT + codepage = CP_ACP; +#else codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP; +#endif nMbcs = WideCharToMultiByte(codepage, 0, zTmpWide, nWide, 0, 0, 0, 0); zMbcs = nMbcs ? (char*)sqlite3_malloc( nMbcs+1 ) : 0; if( zMbcs ){ @@ -399,18 +401,18 @@ static char *quota_utf8_to_mbcs(const char *zUtf8){ return zMbcs; #else return (char*)zUtf8; /* No-op on unix */ -#endif +#endif } /* ** Deallocate any memory allocated by quota_utf8_to_mbcs(). */ static void quota_mbcs_free(char *zOld){ -#if SQLITE_OS_WIN +#ifdef _WIN32 sqlite3_free(zOld); #else /* No-op on unix */ -#endif +#endif } /************************* VFS Method Wrappers *****************************/ @@ -428,7 +430,7 @@ static int quotaOpen( int flags, /* Flags to control the opening */ int *pOutFlags /* Flags showing results of opening */ ){ - int rc; /* Result code */ + int rc; /* Result code */ quotaConn *pQuotaOpen; /* The new quota file descriptor */ quotaFile *pFile; /* Corresponding quotaFile obj */ quotaGroup *pGroup; /* The group file belongs to */ @@ -488,7 +490,7 @@ static int quotaDelete( const char *zName, /* Name of file to be deleted */ int syncDir /* Do a directory sync after deleting */ ){ - int rc; /* Result code */ + int rc; /* Result code */ quotaFile *pFile; /* Files in the quota */ quotaGroup *pGroup; /* The group file belongs to */ sqlite3_vfs *pOrigVfs = gQuota.pOrigVfs; /* Real VFS */ @@ -581,7 +583,7 @@ static int quotaWrite( szNew = pGroup->iSize - pFile->iSize + iEnd; if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){ if( pGroup->xCallback ){ - pGroup->xCallback(pFile->zFilename, &pGroup->iLimit, szNew, + pGroup->xCallback(pFile->zFilename, &pGroup->iLimit, szNew, pGroup->pArg); } if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){ @@ -738,7 +740,7 @@ static int quotaShmUnmap(sqlite3_file *pConn, int deleteFlag){ /* ** Initialize the quota VFS shim. Use the VFS named zOrigVfsName ** as the VFS that does the actual work. Use the default if -** zOrigVfsName==NULL. +** zOrigVfsName==NULL. ** ** The quota VFS shim is named "quota". It will become the default ** VFS if makeDefault is non-zero. @@ -908,7 +910,7 @@ int sqlite3_quota_file(const char *zFilename){ if( rc==SQLITE_OK ){ zFull[strlen(zFull)+1] = '\0'; - rc = quotaOpen(&gQuota.sThisVfs, zFull, fd, + rc = quotaOpen(&gQuota.sThisVfs, zFull, fd, SQLITE_OPEN_READONLY | SQLITE_OPEN_MAIN_DB, &outFlags); if( rc==SQLITE_OK ){ fd->pMethods->xFileSize(fd, &iSize); @@ -966,7 +968,7 @@ quota_FILE *sqlite3_quota_fopen(const char *zFilename, const char *zMode){ } quotaLeave(); sqlite3_free(zFull); -#if SQLITE_OS_WIN +#ifdef _WIN32 p->zMbcsName = zFullTranslated; #endif return p; @@ -999,7 +1001,7 @@ size_t sqlite3_quota_fwrite( const void *pBuf, /* Take content to write from here */ size_t size, /* Size of each element */ size_t nmemb, /* Number of elements */ - quota_FILE *p /* Write to this quota_FILE objecct */ + quota_FILE *p /* Write to this quota_FILE object */ ){ sqlite3_int64 iOfst; sqlite3_int64 iEnd; @@ -1016,7 +1018,7 @@ size_t sqlite3_quota_fwrite( szNew = pGroup->iSize - pFile->iSize + iEnd; if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){ if( pGroup->xCallback ){ - pGroup->xCallback(pFile->zFilename, &pGroup->iLimit, szNew, + pGroup->xCallback(pFile->zFilename, &pGroup->iLimit, szNew, pGroup->pArg); } if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){ @@ -1069,7 +1071,7 @@ int sqlite3_quota_fclose(quota_FILE *p){ } quotaLeave(); } -#if SQLITE_OS_WIN +#ifdef _WIN32 quota_mbcs_free(p->zMbcsName); #endif sqlite3_free(p); @@ -1083,11 +1085,10 @@ int sqlite3_quota_fflush(quota_FILE *p, int doFsync){ int rc; rc = fflush(p->f); if( rc==0 && doFsync ){ -#if SQLITE_OS_UNIX - rc = fsync(fileno(p->f)); -#endif -#if SQLITE_OS_WIN +#ifdef _WIN32 rc = _commit(_fileno(p->f)); +#else + rc = fsync(fileno(p->f)); #endif } return rc!=0; @@ -1139,17 +1140,16 @@ int sqlite3_quota_ftruncate(quota_FILE *p, sqlite3_int64 szNew){ pGroup->iSize += szNew - pFile->iSize; quotaLeave(); } -#if SQLITE_OS_UNIX - rc = ftruncate(fileno(p->f), szNew); -#endif -#if SQLITE_OS_WIN -# if defined(__MINGW32__) && defined(SQLITE_TEST) +#ifdef _WIN32 +# if defined(__MSVCRT__) && defined(SQLITE_TEST) /* _chsize_s() is missing from MingW (as of 2012-11-06). Use ** _chsize() as a work-around for testing purposes. */ rc = _chsize(_fileno(p->f), (long)szNew); # else rc = _chsize_s(_fileno(p->f), szNew); # endif +#else + rc = ftruncate(fileno(p->f), szNew); #endif if( pFile && rc==0 ){ quotaGroup *pGroup = pFile->pGroup; @@ -1168,13 +1168,12 @@ int sqlite3_quota_ftruncate(quota_FILE *p, sqlite3_int64 szNew){ */ int sqlite3_quota_file_mtime(quota_FILE *p, time_t *pTime){ int rc; -#if SQLITE_OS_UNIX - struct stat buf; - rc = fstat(fileno(p->f), &buf); -#endif -#if SQLITE_OS_WIN +#ifdef _WIN32 struct _stati64 buf; rc = _stati64(p->zMbcsName, &buf); +#else + struct stat buf; + rc = fstat(fileno(p->f), &buf); #endif if( rc==0 ) *pTime = buf.st_mtime; return rc; @@ -1186,13 +1185,12 @@ int sqlite3_quota_file_mtime(quota_FILE *p, time_t *pTime){ */ sqlite3_int64 sqlite3_quota_file_truesize(quota_FILE *p){ int rc; -#if SQLITE_OS_UNIX - struct stat buf; - rc = fstat(fileno(p->f), &buf); -#endif -#if SQLITE_OS_WIN +#ifdef _WIN32 struct _stati64 buf; rc = _stati64(p->zMbcsName, &buf); +#else + struct stat buf; + rc = fstat(fileno(p->f), &buf); #endif return rc==0 ? buf.st_size : -1; } @@ -1203,7 +1201,7 @@ sqlite3_int64 sqlite3_quota_file_truesize(quota_FILE *p){ sqlite3_int64 sqlite3_quota_file_size(quota_FILE *p){ return p->pFile ? p->pFile->iSize : -1; } - + /* ** Determine the amount of data in bytes available for reading ** in the given file. @@ -1275,10 +1273,10 @@ int sqlite3_quota_remove(const char *zFilename){ sqlite3_free(zFull); return rc; } - + /***************************** Test Code ***********************************/ #ifdef SQLITE_TEST -#include <tcl.h> +#include "tclsqlite.h" /* ** Argument passed to a TCL quota-over-limit callback. @@ -1350,7 +1348,7 @@ static void tclCallbackDestructor(void *pObj){ /* ** tclcmd: sqlite3_quota_initialize NAME MAKEDEFAULT */ -static int test_quota_initialize( +static int SQLITE_TCLAPI test_quota_initialize( void * clientData, Tcl_Interp *interp, int objc, @@ -1379,7 +1377,7 @@ static int test_quota_initialize( /* ** tclcmd: sqlite3_quota_shutdown */ -static int test_quota_shutdown( +static int SQLITE_TCLAPI test_quota_shutdown( void * clientData, Tcl_Interp *interp, int objc, @@ -1402,7 +1400,7 @@ static int test_quota_shutdown( /* ** tclcmd: sqlite3_quota_set PATTERN LIMIT SCRIPT */ -static int test_quota_set( +static int SQLITE_TCLAPI test_quota_set( void * clientData, Tcl_Interp *interp, int objc, @@ -1413,7 +1411,7 @@ static int test_quota_set( Tcl_Obj *pScript; /* Tcl script to invoke to increase quota */ int rc; /* Value returned by quota_set() */ TclQuotaCallback *p; /* Callback object */ - int nScript; /* Length of callback script */ + Tcl_Size nScript; /* Length of callback script */ void (*xDestroy)(void*); /* Optional destructor for pArg */ void (*xCallback)(const char *, sqlite3_int64 *, sqlite3_int64, void *); @@ -1456,7 +1454,7 @@ static int test_quota_set( /* ** tclcmd: sqlite3_quota_file FILENAME */ -static int test_quota_file( +static int SQLITE_TCLAPI test_quota_file( void * clientData, Tcl_Interp *interp, int objc, @@ -1482,7 +1480,7 @@ static int test_quota_file( /* ** tclcmd: sqlite3_quota_dump */ -static int test_quota_dump( +static int SQLITE_TCLAPI test_quota_dump( void * clientData, Tcl_Interp *interp, int objc, @@ -1530,7 +1528,7 @@ static int test_quota_dump( /* ** tclcmd: sqlite3_quota_fopen FILENAME MODE */ -static int test_quota_fopen( +static int SQLITE_TCLAPI test_quota_fopen( void * clientData, Tcl_Interp *interp, int objc, @@ -1560,7 +1558,7 @@ extern void *sqlite3TestTextToPtr(const char*); /* ** tclcmd: sqlite3_quota_fread HANDLE SIZE NELEM */ -static int test_quota_fread( +static int SQLITE_TCLAPI test_quota_fread( void * clientData, Tcl_Interp *interp, int objc, @@ -1594,7 +1592,7 @@ static int test_quota_fread( /* ** tclcmd: sqlite3_quota_fwrite HANDLE SIZE NELEM CONTENT */ -static int test_quota_fwrite( +static int SQLITE_TCLAPI test_quota_fwrite( void * clientData, Tcl_Interp *interp, int objc, @@ -1622,7 +1620,7 @@ static int test_quota_fwrite( /* ** tclcmd: sqlite3_quota_fclose HANDLE */ -static int test_quota_fclose( +static int SQLITE_TCLAPI test_quota_fclose( void * clientData, Tcl_Interp *interp, int objc, @@ -1644,7 +1642,7 @@ static int test_quota_fclose( /* ** tclcmd: sqlite3_quota_fflush HANDLE ?HARDSYNC? */ -static int test_quota_fflush( +static int SQLITE_TCLAPI test_quota_fflush( void * clientData, Tcl_Interp *interp, int objc, @@ -1670,7 +1668,7 @@ static int test_quota_fflush( /* ** tclcmd: sqlite3_quota_fseek HANDLE OFFSET WHENCE */ -static int test_quota_fseek( +static int SQLITE_TCLAPI test_quota_fseek( void * clientData, Tcl_Interp *interp, int objc, @@ -1708,7 +1706,7 @@ static int test_quota_fseek( /* ** tclcmd: sqlite3_quota_rewind HANDLE */ -static int test_quota_rewind( +static int SQLITE_TCLAPI test_quota_rewind( void * clientData, Tcl_Interp *interp, int objc, @@ -1727,7 +1725,7 @@ static int test_quota_rewind( /* ** tclcmd: sqlite3_quota_ftell HANDLE */ -static int test_quota_ftell( +static int SQLITE_TCLAPI test_quota_ftell( void * clientData, Tcl_Interp *interp, int objc, @@ -1748,7 +1746,7 @@ static int test_quota_ftell( /* ** tclcmd: sqlite3_quota_ftruncate HANDLE SIZE */ -static int test_quota_ftruncate( +static int SQLITE_TCLAPI test_quota_ftruncate( void * clientData, Tcl_Interp *interp, int objc, @@ -1773,7 +1771,7 @@ static int test_quota_ftruncate( /* ** tclcmd: sqlite3_quota_file_size HANDLE */ -static int test_quota_file_size( +static int SQLITE_TCLAPI test_quota_file_size( void * clientData, Tcl_Interp *interp, int objc, @@ -1794,7 +1792,7 @@ static int test_quota_file_size( /* ** tclcmd: sqlite3_quota_file_truesize HANDLE */ -static int test_quota_file_truesize( +static int SQLITE_TCLAPI test_quota_file_truesize( void * clientData, Tcl_Interp *interp, int objc, @@ -1815,7 +1813,7 @@ static int test_quota_file_truesize( /* ** tclcmd: sqlite3_quota_file_mtime HANDLE */ -static int test_quota_file_mtime( +static int SQLITE_TCLAPI test_quota_file_mtime( void * clientData, Tcl_Interp *interp, int objc, @@ -1838,7 +1836,7 @@ static int test_quota_file_mtime( /* ** tclcmd: sqlite3_quota_remove FILENAME */ -static int test_quota_remove( +static int SQLITE_TCLAPI test_quota_remove( void * clientData, Tcl_Interp *interp, int objc, @@ -1862,14 +1860,14 @@ static int test_quota_remove( ** Test the glob pattern matching. Return 1 if TEXT matches PATTERN ** and return 0 if it does not. */ -static int test_quota_glob( +static int SQLITE_TCLAPI test_quota_glob( void * clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[] ){ const char *zPattern; /* The glob pattern */ - const char *zText; /* Text to compare agains the pattern */ + const char *zText; /* Text to compare against the pattern */ int rc; if( objc!=3 ){ Tcl_WrongNumArgs(interp, 1, objv, "PATTERN TEXT"); @@ -1888,7 +1886,7 @@ static int test_quota_glob( ** Return the number of bytes from the current file point to the end of ** the file. */ -static int test_quota_file_available( +static int SQLITE_TCLAPI test_quota_file_available( void * clientData, Tcl_Interp *interp, int objc, @@ -1911,7 +1909,7 @@ static int test_quota_file_available( ** ** Return true if the file handle is in the error state. */ -static int test_quota_ferror( +static int SQLITE_TCLAPI test_quota_ferror( void * clientData, Tcl_Interp *interp, int objc, diff --git a/src/test_rtree.c b/src/test_rtree.c index 797ec0026c..53af6e5cfe 100644 --- a/src/test_rtree.c +++ b/src/test_rtree.c @@ -14,7 +14,7 @@ */ #include "sqlite3.h" -#include <tcl.h> +#include "tclsqlite.h" /* Solely for the UNUSED_PARAMETER() macro. */ #include "sqliteInt.h" @@ -353,7 +353,7 @@ static int bfs_query_func(sqlite3_rtree_query_info *p){ *************************************************************************/ #include <assert.h> -#include "tcl.h" +#include "tclsqlite.h" typedef struct Cube Cube; struct Cube { @@ -432,7 +432,7 @@ static int cube_geom( } #endif /* SQLITE_ENABLE_RTREE */ -static int register_cube_geom( +static int SQLITE_TCLAPI register_cube_geom( void * clientData, Tcl_Interp *interp, int objc, @@ -460,7 +460,7 @@ static int register_cube_geom( return TCL_OK; } -static int register_circle_geom( +static int SQLITE_TCLAPI register_circle_geom( void * clientData, Tcl_Interp *interp, int objc, diff --git a/src/test_schema.c b/src/test_schema.c index 4ee18193b0..660d21ea4e 100644 --- a/src/test_schema.c +++ b/src/test_schema.c @@ -35,10 +35,10 @@ ** to be compiled into an sqlite dynamic extension. */ #ifdef SQLITE_TEST - #include "sqliteInt.h" - #include "tcl.h" +# include "sqliteInt.h" +# include "tclsqlite.h" #else - #include "sqlite3ext.h" +# include "sqlite3ext.h" SQLITE_EXTENSION_INIT1 #endif @@ -188,18 +188,18 @@ static int schemaNext(sqlite3_vtab_cursor *cur){ } /* Set zSql to the SQL to pull the list of tables from the - ** sqlite_master (or sqlite_temp_master) table of the database + ** sqlite_schema (or sqlite_temp_schema) table of the database ** identified by the row pointed to by the SQL statement pCur->pDbList ** (iterating through a "PRAGMA database_list;" statement). */ if( sqlite3_column_int(pCur->pDbList, 0)==1 ){ zSql = sqlite3_mprintf( - "SELECT name FROM sqlite_temp_master WHERE type='table'" + "SELECT name FROM sqlite_temp_schema WHERE type='table'" ); }else{ sqlite3_stmt *pDbList = pCur->pDbList; zSql = sqlite3_mprintf( - "SELECT name FROM %Q.sqlite_master WHERE type='table'", + "SELECT name FROM %Q.sqlite_schema WHERE type='table'", sqlite3_column_text(pDbList, 1) ); } @@ -288,6 +288,11 @@ static sqlite3_module schemaModule = { 0, /* xRollback */ 0, /* xFindMethod */ 0, /* xRename */ + 0, /* xSavepoint */ + 0, /* xRelease */ + 0, /* xRollbackTo */ + 0, /* xShadowName */ + 0 /* xIntegrity */ }; #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */ @@ -302,7 +307,7 @@ extern int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb); /* ** Register the schema virtual table module. */ -static int register_schema_module( +static int SQLITE_TCLAPI register_schema_module( ClientData clientData, /* Not used */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ diff --git a/src/test_server.c b/src/test_server.c deleted file mode 100644 index 4eb1cf1966..0000000000 --- a/src/test_server.c +++ /dev/null @@ -1,516 +0,0 @@ -/* -** 2006 January 07 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -****************************************************************************** -** -** This file contains demonstration code. Nothing in this file gets compiled -** or linked into the SQLite library unless you use a non-standard option: -** -** -DSQLITE_SERVER=1 -** -** The configure script will never generate a Makefile with the option -** above. You will need to manually modify the Makefile if you want to -** include any of the code from this file in your project. Or, at your -** option, you may copy and paste the code from this file and -** thereby avoiding a recompile of SQLite. -** -** -** This source file demonstrates how to use SQLite to create an SQL database -** server thread in a multiple-threaded program. One or more client threads -** send messages to the server thread and the server thread processes those -** messages in the order received and returns the results to the client. -** -** One might ask: "Why bother? Why not just let each thread connect -** to the database directly?" There are a several of reasons to -** prefer the client/server approach. -** -** (1) Some systems (ex: Redhat9) have broken threading implementations -** that prevent SQLite database connections from being used in -** a thread different from the one where they were created. With -** the client/server approach, all database connections are created -** and used within the server thread. Client calls to the database -** can be made from multiple threads (though not at the same time!) -** -** (2) Beginning with SQLite version 3.3.0, when two or more -** connections to the same database occur within the same thread, -** they can optionally share their database cache. This reduces -** I/O and memory requirements. Cache shared is controlled using -** the sqlite3_enable_shared_cache() API. -** -** (3) Database connections on a shared cache use table-level locking -** instead of file-level locking for improved concurrency. -** -** (4) Database connections on a shared cache can by optionally -** set to READ UNCOMMITTED isolation. (The default isolation for -** SQLite is SERIALIZABLE.) When this occurs, readers will -** never be blocked by a writer and writers will not be -** blocked by readers. There can still only be a single writer -** at a time, but multiple readers can simultaneously exist with -** that writer. This is a huge increase in concurrency. -** -** To summarize the rational for using a client/server approach: prior -** to SQLite version 3.3.0 it probably was not worth the trouble. But -** with SQLite version 3.3.0 and beyond you can get significant performance -** and concurrency improvements and memory usage reductions by going -** client/server. -** -** Note: The extra features of version 3.3.0 described by points (2) -** through (4) above are only available if you compile without the -** option -DSQLITE_OMIT_SHARED_CACHE. -** -** Here is how the client/server approach works: The database server -** thread is started on this procedure: -** -** void *sqlite3_server(void *NotUsed); -** -** The sqlite_server procedure runs as long as the g.serverHalt variable -** is false. A mutex is used to make sure no more than one server runs -** at a time. The server waits for messages to arrive on a message -** queue and processes the messages in order. -** -** Two convenience routines are provided for starting and stopping the -** server thread: -** -** void sqlite3_server_start(void); -** void sqlite3_server_stop(void); -** -** Both of the convenience routines return immediately. Neither will -** ever give an error. If a server is already started or already halted, -** then the routines are effectively no-ops. -** -** Clients use the following interfaces: -** -** sqlite3_client_open -** sqlite3_client_prepare -** sqlite3_client_step -** sqlite3_client_reset -** sqlite3_client_finalize -** sqlite3_client_close -** -** These interfaces work exactly like the standard core SQLite interfaces -** having the same names without the "_client_" infix. Many other SQLite -** interfaces can be used directly without having to send messages to the -** server as long as SQLITE_ENABLE_MEMORY_MANAGEMENT is not defined. -** The following interfaces fall into this second category: -** -** sqlite3_bind_* -** sqlite3_changes -** sqlite3_clear_bindings -** sqlite3_column_* -** sqlite3_complete -** sqlite3_create_collation -** sqlite3_create_function -** sqlite3_data_count -** sqlite3_db_handle -** sqlite3_errcode -** sqlite3_errmsg -** sqlite3_last_insert_rowid -** sqlite3_total_changes -** sqlite3_transfer_bindings -** -** A single SQLite connection (an sqlite3* object) or an SQLite statement -** (an sqlite3_stmt* object) should only be passed to a single interface -** function at a time. The connections and statements can be passed from -** any thread to any of the functions listed in the second group above as -** long as the same connection is not in use by two threads at once and -** as long as SQLITE_ENABLE_MEMORY_MANAGEMENT is not defined. Additional -** information about the SQLITE_ENABLE_MEMORY_MANAGEMENT constraint is -** below. -** -** The busy handler for all database connections should remain turned -** off. That means that any lock contention will cause the associated -** sqlite3_client_step() call to return immediately with an SQLITE_BUSY -** error code. If a busy handler is enabled and lock contention occurs, -** then the entire server thread will block. This will cause not only -** the requesting client to block but every other database client as -** well. It is possible to enhance the code below so that lock -** contention will cause the message to be placed back on the top of -** the queue to be tried again later. But such enhanced processing is -** not included here, in order to keep the example simple. -** -** This example code assumes the use of pthreads. Pthreads -** implementations are available for windows. (See, for example -** http://sourceware.org/pthreads-win32/announcement.html.) Or, you -** can translate the locking and thread synchronization code to use -** windows primitives easily enough. The details are left as an -** exercise to the reader. -** -**** Restrictions Associated With SQLITE_ENABLE_MEMORY_MANAGEMENT **** -** -** If you compile with SQLITE_ENABLE_MEMORY_MANAGEMENT defined, then -** SQLite includes code that tracks how much memory is being used by -** each thread. These memory counts can become confused if memory -** is allocated by one thread and then freed by another. For that -** reason, when SQLITE_ENABLE_MEMORY_MANAGEMENT is used, all operations -** that might allocate or free memory should be performanced in the same -** thread that originally created the database connection. In that case, -** many of the operations that are listed above as safe to be performed -** in separate threads would need to be sent over to the server to be -** done there. If SQLITE_ENABLE_MEMORY_MANAGEMENT is defined, then -** the following functions can be used safely from different threads -** without messing up the allocation counts: -** -** sqlite3_bind_parameter_name -** sqlite3_bind_parameter_index -** sqlite3_changes -** sqlite3_column_blob -** sqlite3_column_count -** sqlite3_complete -** sqlite3_data_count -** sqlite3_db_handle -** sqlite3_errcode -** sqlite3_errmsg -** sqlite3_last_insert_rowid -** sqlite3_total_changes -** -** The remaining functions are not thread-safe when memory management -** is enabled. So one would have to define some new interface routines -** along the following lines: -** -** sqlite3_client_bind_* -** sqlite3_client_clear_bindings -** sqlite3_client_column_* -** sqlite3_client_create_collation -** sqlite3_client_create_function -** sqlite3_client_transfer_bindings -** -** The example code in this file is intended for use with memory -** management turned off. So the implementation of these additional -** client interfaces is left as an exercise to the reader. -** -** It may seem surprising to the reader that the list of safe functions -** above does not include things like sqlite3_bind_int() or -** sqlite3_column_int(). But those routines might, in fact, allocate -** or deallocate memory. In the case of sqlite3_bind_int(), if the -** parameter was previously bound to a string that string might need -** to be deallocated before the new integer value is inserted. In -** the case of sqlite3_column_int(), the value of the column might be -** a UTF-16 string which will need to be converted to UTF-8 then into -** an integer. -*/ - -/* Include this to get the definition of SQLITE_THREADSAFE, in the -** case that default values are used. -*/ -#include "sqliteInt.h" - -/* -** Only compile the code in this file on UNIX with a SQLITE_THREADSAFE build -** and only if the SQLITE_SERVER macro is defined. -*/ -#if defined(SQLITE_SERVER) && !defined(SQLITE_OMIT_SHARED_CACHE) -#if SQLITE_OS_UNIX && SQLITE_THREADSAFE - -/* -** We require only pthreads and the public interface of SQLite. -*/ -#include <pthread.h> -#include "sqlite3.h" - -/* -** Messages are passed from client to server and back again as -** instances of the following structure. -*/ -typedef struct SqlMessage SqlMessage; -struct SqlMessage { - int op; /* Opcode for the message */ - sqlite3 *pDb; /* The SQLite connection */ - sqlite3_stmt *pStmt; /* A specific statement */ - int errCode; /* Error code returned */ - const char *zIn; /* Input filename or SQL statement */ - int nByte; /* Size of the zIn parameter for prepare() */ - const char *zOut; /* Tail of the SQL statement */ - SqlMessage *pNext; /* Next message in the queue */ - SqlMessage *pPrev; /* Previous message in the queue */ - pthread_mutex_t clientMutex; /* Hold this mutex to access the message */ - pthread_cond_t clientWakeup; /* Signal to wake up the client */ -}; - -/* -** Legal values for SqlMessage.op -*/ -#define MSG_Open 1 /* sqlite3_open(zIn, &pDb) */ -#define MSG_Prepare 2 /* sqlite3_prepare(pDb, zIn, nByte, &pStmt, &zOut) */ -#define MSG_Step 3 /* sqlite3_step(pStmt) */ -#define MSG_Reset 4 /* sqlite3_reset(pStmt) */ -#define MSG_Finalize 5 /* sqlite3_finalize(pStmt) */ -#define MSG_Close 6 /* sqlite3_close(pDb) */ -#define MSG_Done 7 /* Server has finished with this message */ - - -/* -** State information about the server is stored in a static variable -** named "g" as follows: -*/ -static struct ServerState { - pthread_mutex_t queueMutex; /* Hold this mutex to access the msg queue */ - pthread_mutex_t serverMutex; /* Held by the server while it is running */ - pthread_cond_t serverWakeup; /* Signal this condvar to wake up the server */ - volatile int serverHalt; /* Server halts itself when true */ - SqlMessage *pQueueHead; /* Head of the message queue */ - SqlMessage *pQueueTail; /* Tail of the message queue */ -} g = { - PTHREAD_MUTEX_INITIALIZER, - PTHREAD_MUTEX_INITIALIZER, - PTHREAD_COND_INITIALIZER, -}; - -/* -** Send a message to the server. Block until we get a reply. -** -** The mutex and condition variable in the message are uninitialized -** when this routine is called. This routine takes care of -** initializing them and destroying them when it has finished. -*/ -static void sendToServer(SqlMessage *pMsg){ - /* Initialize the mutex and condition variable on the message - */ - pthread_mutex_init(&pMsg->clientMutex, 0); - pthread_cond_init(&pMsg->clientWakeup, 0); - - /* Add the message to the head of the server's message queue. - */ - pthread_mutex_lock(&g.queueMutex); - pMsg->pNext = g.pQueueHead; - if( g.pQueueHead==0 ){ - g.pQueueTail = pMsg; - }else{ - g.pQueueHead->pPrev = pMsg; - } - pMsg->pPrev = 0; - g.pQueueHead = pMsg; - pthread_mutex_unlock(&g.queueMutex); - - /* Signal the server that the new message has be queued, then - ** block waiting for the server to process the message. - */ - pthread_mutex_lock(&pMsg->clientMutex); - pthread_cond_signal(&g.serverWakeup); - while( pMsg->op!=MSG_Done ){ - pthread_cond_wait(&pMsg->clientWakeup, &pMsg->clientMutex); - } - pthread_mutex_unlock(&pMsg->clientMutex); - - /* Destroy the mutex and condition variable of the message. - */ - pthread_mutex_destroy(&pMsg->clientMutex); - pthread_cond_destroy(&pMsg->clientWakeup); -} - -/* -** The following 6 routines are client-side implementations of the -** core SQLite interfaces: -** -** sqlite3_open -** sqlite3_prepare -** sqlite3_step -** sqlite3_reset -** sqlite3_finalize -** sqlite3_close -** -** Clients should use the following client-side routines instead of -** the core routines above. -** -** sqlite3_client_open -** sqlite3_client_prepare -** sqlite3_client_step -** sqlite3_client_reset -** sqlite3_client_finalize -** sqlite3_client_close -** -** Each of these routines creates a message for the desired operation, -** sends that message to the server, waits for the server to process -** then message and return a response. -*/ -int sqlite3_client_open(const char *zDatabaseName, sqlite3 **ppDb){ - SqlMessage msg; - msg.op = MSG_Open; - msg.zIn = zDatabaseName; - sendToServer(&msg); - *ppDb = msg.pDb; - return msg.errCode; -} -int sqlite3_client_prepare( - sqlite3 *pDb, - const char *zSql, - int nByte, - sqlite3_stmt **ppStmt, - const char **pzTail -){ - SqlMessage msg; - msg.op = MSG_Prepare; - msg.pDb = pDb; - msg.zIn = zSql; - msg.nByte = nByte; - sendToServer(&msg); - *ppStmt = msg.pStmt; - if( pzTail ) *pzTail = msg.zOut; - return msg.errCode; -} -int sqlite3_client_step(sqlite3_stmt *pStmt){ - SqlMessage msg; - msg.op = MSG_Step; - msg.pStmt = pStmt; - sendToServer(&msg); - return msg.errCode; -} -int sqlite3_client_reset(sqlite3_stmt *pStmt){ - SqlMessage msg; - msg.op = MSG_Reset; - msg.pStmt = pStmt; - sendToServer(&msg); - return msg.errCode; -} -int sqlite3_client_finalize(sqlite3_stmt *pStmt){ - SqlMessage msg; - msg.op = MSG_Finalize; - msg.pStmt = pStmt; - sendToServer(&msg); - return msg.errCode; -} -int sqlite3_client_close(sqlite3 *pDb){ - SqlMessage msg; - msg.op = MSG_Close; - msg.pDb = pDb; - sendToServer(&msg); - return msg.errCode; -} - -/* -** This routine implements the server. To start the server, first -** make sure g.serverHalt is false, then create a new detached thread -** on this procedure. See the sqlite3_server_start() routine below -** for an example. This procedure loops until g.serverHalt becomes -** true. -*/ -void *sqlite3_server(void *NotUsed){ - if( pthread_mutex_trylock(&g.serverMutex) ){ - return 0; /* Another server is already running */ - } - sqlite3_enable_shared_cache(1); - while( !g.serverHalt ){ - SqlMessage *pMsg; - - /* Remove the last message from the message queue. - */ - pthread_mutex_lock(&g.queueMutex); - while( g.pQueueTail==0 && g.serverHalt==0 ){ - pthread_cond_wait(&g.serverWakeup, &g.queueMutex); - } - pMsg = g.pQueueTail; - if( pMsg ){ - if( pMsg->pPrev ){ - pMsg->pPrev->pNext = 0; - }else{ - g.pQueueHead = 0; - } - g.pQueueTail = pMsg->pPrev; - } - pthread_mutex_unlock(&g.queueMutex); - if( pMsg==0 ) break; - - /* Process the message just removed - */ - pthread_mutex_lock(&pMsg->clientMutex); - switch( pMsg->op ){ - case MSG_Open: { - pMsg->errCode = sqlite3_open(pMsg->zIn, &pMsg->pDb); - break; - } - case MSG_Prepare: { - pMsg->errCode = sqlite3_prepare(pMsg->pDb, pMsg->zIn, pMsg->nByte, - &pMsg->pStmt, &pMsg->zOut); - break; - } - case MSG_Step: { - pMsg->errCode = sqlite3_step(pMsg->pStmt); - break; - } - case MSG_Reset: { - pMsg->errCode = sqlite3_reset(pMsg->pStmt); - break; - } - case MSG_Finalize: { - pMsg->errCode = sqlite3_finalize(pMsg->pStmt); - break; - } - case MSG_Close: { - pMsg->errCode = sqlite3_close(pMsg->pDb); - break; - } - } - - /* Signal the client that the message has been processed. - */ - pMsg->op = MSG_Done; - pthread_mutex_unlock(&pMsg->clientMutex); - pthread_cond_signal(&pMsg->clientWakeup); - } - pthread_mutex_unlock(&g.serverMutex); - return 0; -} - -/* -** Start a server thread if one is not already running. If there -** is aleady a server thread running, the new thread will quickly -** die and this routine is effectively a no-op. -*/ -void sqlite3_server_start(void){ - pthread_t x; - int rc; - g.serverHalt = 0; - rc = pthread_create(&x, 0, sqlite3_server, 0); - if( rc==0 ){ - pthread_detach(x); - } -} - -/* -** A wrapper around sqlite3_server() that decrements the int variable -** pointed to by the first argument after the sqlite3_server() call -** returns. -*/ -static void *serverWrapper(void *pnDecr){ - void *p = sqlite3_server(0); - (*(int*)pnDecr)--; - return p; -} - -/* -** This function is the similar to sqlite3_server_start(), except that -** the integer pointed to by the first argument is decremented when -** the server thread exits. -*/ -void sqlite3_server_start2(int *pnDecr){ - pthread_t x; - int rc; - g.serverHalt = 0; - rc = pthread_create(&x, 0, serverWrapper, (void*)pnDecr); - if( rc==0 ){ - pthread_detach(x); - } -} - -/* -** If a server thread is running, then stop it. If no server is -** running, this routine is effectively a no-op. -** -** This routine waits until the server has actually stopped before -** returning. -*/ -void sqlite3_server_stop(void){ - g.serverHalt = 1; - pthread_cond_broadcast(&g.serverWakeup); - pthread_mutex_lock(&g.serverMutex); - pthread_mutex_unlock(&g.serverMutex); -} - -#endif /* SQLITE_OS_UNIX && SQLITE_THREADSAFE */ -#endif /* defined(SQLITE_SERVER) */ diff --git a/src/test_sqllog.c b/src/test_sqllog.c index 31d5ad2f5b..5abf59a8bf 100644 --- a/src/test_sqllog.c +++ b/src/test_sqllog.c @@ -84,7 +84,7 @@ #include <sys/types.h> #include <unistd.h> static int getProcessId(void){ -#if SQLITE_OS_WIN +#ifdef _WIN32 return (int)_getpid(); #else return (int)getpid(); @@ -118,7 +118,7 @@ struct SLConn { /* This object is a singleton that keeps track of all data loggers. */ static struct SLGlobal { - /* Protected by MUTEX_STATIC_MASTER */ + /* Protected by MUTEX_STATIC_MAIN */ sqlite3_mutex *mutex; /* Recursive mutex */ int nConn; /* Size of aConn[] array */ @@ -313,7 +313,7 @@ static void sqllogCopydb(struct SLConn *p, const char *zSearch, int bLog){ /* Generate a file-name to use for the copy of this database */ iDb = sqllogglobal.iNextDb++; - zInit = sqlite3_mprintf("%s_%d.db", sqllogglobal.zPrefix, iDb); + zInit = sqlite3_mprintf("%s_%02d.db", sqllogglobal.zPrefix, iDb); /* Create the backup */ assert( sqllogglobal.bRec==0 ); @@ -376,7 +376,7 @@ static void sqllogOpenlog(struct SLConn *p){ char *zVar = getenv(ENVIRONMENT_VARIABLE1_NAME); if( zVar==0 || strlen(zVar)+10>=(sizeof(sqllogglobal.zPrefix)) ) return; sqlite3_snprintf(sizeof(sqllogglobal.zPrefix), sqllogglobal.zPrefix, - "%s/sqllog_%d", zVar, getProcessId()); + "%s/sqllog_%05d", zVar, getProcessId()); sqlite3_snprintf(sizeof(sqllogglobal.zIdx), sqllogglobal.zIdx, "%s.idx", sqllogglobal.zPrefix); if( getenv(ENVIRONMENT_VARIABLE2_NAME) ){ @@ -387,7 +387,7 @@ static void sqllogOpenlog(struct SLConn *p){ } /* Open the log file */ - zLog = sqlite3_mprintf("%s_%d.sql", sqllogglobal.zPrefix, p->iLog); + zLog = sqlite3_mprintf("%s_%05d.sql", sqllogglobal.zPrefix, p->iLog); p->fd = fopen(zLog, "w"); sqlite3_free(zLog); if( p->fd==0 ){ @@ -467,28 +467,28 @@ static int sqllogTraceDb(sqlite3 *db){ */ static void testSqllog(void *pCtx, sqlite3 *db, const char *zSql, int eType){ struct SLConn *p = 0; - sqlite3_mutex *master = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER); + sqlite3_mutex *mainmtx = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MAIN); assert( eType==0 || eType==1 || eType==2 ); assert( (eType==2)==(zSql==0) ); /* This is a database open command. */ if( eType==0 ){ - sqlite3_mutex_enter(master); + sqlite3_mutex_enter(mainmtx); if( sqllogglobal.mutex==0 ){ sqllogglobal.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_RECURSIVE); } - sqlite3_mutex_leave(master); + sqlite3_mutex_leave(mainmtx); sqlite3_mutex_enter(sqllogglobal.mutex); if( sqllogglobal.bRec==0 && sqllogTraceDb(db) ){ - sqlite3_mutex_enter(master); + sqlite3_mutex_enter(mainmtx); p = &sqllogglobal.aConn[sqllogglobal.nConn++]; p->fd = 0; p->db = db; p->iLog = sqllogglobal.iNextLog++; - sqlite3_mutex_leave(master); + sqlite3_mutex_leave(mainmtx); /* Open the log and take a copy of the main database file */ sqllogOpenlog(p); @@ -507,7 +507,7 @@ static void testSqllog(void *pCtx, sqlite3 *db, const char *zSql, int eType){ /* A database handle close command */ if( eType==2 ){ - sqlite3_mutex_enter(master); + sqlite3_mutex_enter(mainmtx); if( i<sqllogglobal.nConn ){ if( p->fd ) fclose(p->fd); p->db = 0; @@ -524,7 +524,7 @@ static void testSqllog(void *pCtx, sqlite3 *db, const char *zSql, int eType){ memmove(p, &p[1], nShift*sizeof(struct SLConn)); } } - sqlite3_mutex_leave(master); + sqlite3_mutex_leave(mainmtx); /* An ordinary SQL command. */ }else if( i<sqllogglobal.nConn && p->fd ){ diff --git a/src/test_superlock.c b/src/test_superlock.c index cac789842d..82997927c4 100644 --- a/src/test_superlock.c +++ b/src/test_superlock.c @@ -256,7 +256,7 @@ int sqlite3demo_superlock( #ifdef SQLITE_TEST -#include <tcl.h> +#include "tclsqlite.h" struct InterpAndScript { Tcl_Interp *interp; @@ -264,11 +264,11 @@ struct InterpAndScript { }; typedef struct InterpAndScript InterpAndScript; -static void superunlock_del(ClientData cd){ +static void SQLITE_TCLAPI superunlock_del(ClientData cd){ sqlite3demo_superunlock((void *)cd); } -static int superunlock_cmd( +static int SQLITE_TCLAPI superunlock_cmd( ClientData cd, Tcl_Interp *interp, int objc, @@ -300,7 +300,7 @@ static int superlock_busy(void *pCtx, int nBusy){ /* ** Tclcmd: sqlite3demo_superlock CMDNAME PATH VFS BUSY-HANDLER-SCRIPT */ -static int superlock_cmd( +static int SQLITE_TCLAPI superlock_cmd( ClientData cd, Tcl_Interp *interp, int objc, @@ -338,7 +338,7 @@ static int superlock_cmd( if( rc!=SQLITE_OK ){ extern const char *sqlite3ErrStr(int); Tcl_ResetResult(interp); - Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0); + Tcl_AppendResult(interp, sqlite3ErrStr(rc), NULL); return TCL_ERROR; } diff --git a/src/test_syscall.c b/src/test_syscall.c index 0dac2e897e..35c303f8ee 100644 --- a/src/test_syscall.c +++ b/src/test_syscall.c @@ -76,7 +76,7 @@ #include "sqliteInt.h" #include "sqlite3.h" -#include "tcl.h" +#include "tclsqlite.h" #include <stdlib.h> #include <string.h> #include <assert.h> @@ -106,12 +106,15 @@ static int ts_stat(const char *zPath, struct stat *p); static int ts_fstat(int fd, struct stat *p); static int ts_ftruncate(int fd, off_t n); static int ts_fcntl(int fd, int cmd, ... ); -static int ts_read(int fd, void *aBuf, size_t nBuf); -static int ts_pread(int fd, void *aBuf, size_t nBuf, off_t off); -static int ts_pread64(int fd, void *aBuf, size_t nBuf, off_t off); -static int ts_write(int fd, const void *aBuf, size_t nBuf); -static int ts_pwrite(int fd, const void *aBuf, size_t nBuf, off_t off); -static int ts_pwrite64(int fd, const void *aBuf, size_t nBuf, off_t off); +static ssize_t ts_read(int fd, void *aBuf, size_t nBuf); +static ssize_t ts_pread(int fd, void *aBuf, size_t nBuf, off_t off); +/* Note: pread64() and pwrite64() actually use off64_t as the type on their +** last parameter. But that datatype is not defined on many systems +** (ex: Mac, OpenBSD). So substitute a likely equivalent: sqlite3_uint64 */ +static ssize_t ts_pread64(int fd, void *aBuf, size_t nBuf, sqlite3_uint64 off); +static ssize_t ts_write(int fd, const void *aBuf, size_t nBuf); +static ssize_t ts_pwrite(int fd, const void *aBuf, size_t nBuf, off_t off); +static ssize_t ts_pwrite64(int fd, const void *aBuf, size_t nBuf, sqlite3_uint64 off); static int ts_fchmod(int fd, mode_t mode); static int ts_fallocate(int fd, off_t off, off_t len); static void *ts_mmap(void *, size_t, int, int, int, off_t); @@ -155,11 +158,11 @@ struct TestSyscallArray { #define orig_fcntl ((int(*)(int,int,...))aSyscall[7].xOrig) #define orig_read ((ssize_t(*)(int,void*,size_t))aSyscall[8].xOrig) #define orig_pread ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].xOrig) -#define orig_pread64 ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[10].xOrig) +#define orig_pread64 ((ssize_t(*)(int,void*,size_t,sqlite3_uint64))aSyscall[10].xOrig) #define orig_write ((ssize_t(*)(int,const void*,size_t))aSyscall[11].xOrig) #define orig_pwrite ((ssize_t(*)(int,const void*,size_t,off_t))\ aSyscall[12].xOrig) -#define orig_pwrite64 ((ssize_t(*)(int,const void*,size_t,off_t))\ +#define orig_pwrite64 ((ssize_t(*)(int,const void*,size_t,sqlite3_uint64))\ aSyscall[13].xOrig) #define orig_fchmod ((int(*)(int,mode_t))aSyscall[14].xOrig) #define orig_fallocate ((int(*)(int,off_t,off_t))aSyscall[15].xOrig) @@ -190,7 +193,7 @@ static int tsIsFail(void){ */ static int tsErrno(const char *zFunc){ int i; - int nFunc = strlen(zFunc); + size_t nFunc = strlen(zFunc); for(i=0; aSyscall[i].zName; i++){ if( strlen(aSyscall[i].zName)!=nFunc ) continue; if( memcmp(aSyscall[i].zName, zFunc, nFunc) ) continue; @@ -204,7 +207,7 @@ static int tsErrno(const char *zFunc){ /* ** A wrapper around tsIsFail(). If tsIsFail() returns non-zero, set the ** value of errno before returning. -*/ +*/ static int tsIsFailErrno(const char *zFunc){ if( tsIsFail() ){ errno = tsErrno(zFunc); @@ -306,7 +309,7 @@ static int ts_fcntl(int fd, int cmd, ... ){ /* ** A wrapper around read(). */ -static int ts_read(int fd, void *aBuf, size_t nBuf){ +static ssize_t ts_read(int fd, void *aBuf, size_t nBuf){ if( tsIsFailErrno("read") ){ return -1; } @@ -316,7 +319,7 @@ static int ts_read(int fd, void *aBuf, size_t nBuf){ /* ** A wrapper around pread(). */ -static int ts_pread(int fd, void *aBuf, size_t nBuf, off_t off){ +static ssize_t ts_pread(int fd, void *aBuf, size_t nBuf, off_t off){ if( tsIsFailErrno("pread") ){ return -1; } @@ -326,7 +329,7 @@ static int ts_pread(int fd, void *aBuf, size_t nBuf, off_t off){ /* ** A wrapper around pread64(). */ -static int ts_pread64(int fd, void *aBuf, size_t nBuf, off_t off){ +static ssize_t ts_pread64(int fd, void *aBuf, size_t nBuf, sqlite3_uint64 off){ if( tsIsFailErrno("pread64") ){ return -1; } @@ -336,7 +339,7 @@ static int ts_pread64(int fd, void *aBuf, size_t nBuf, off_t off){ /* ** A wrapper around write(). */ -static int ts_write(int fd, const void *aBuf, size_t nBuf){ +static ssize_t ts_write(int fd, const void *aBuf, size_t nBuf){ if( tsIsFailErrno("write") ){ if( tsErrno("write")==EINTR ) orig_write(fd, aBuf, nBuf/2); return -1; @@ -347,7 +350,7 @@ static int ts_write(int fd, const void *aBuf, size_t nBuf){ /* ** A wrapper around pwrite(). */ -static int ts_pwrite(int fd, const void *aBuf, size_t nBuf, off_t off){ +static ssize_t ts_pwrite(int fd, const void *aBuf, size_t nBuf, off_t off){ if( tsIsFailErrno("pwrite") ){ return -1; } @@ -357,7 +360,7 @@ static int ts_pwrite(int fd, const void *aBuf, size_t nBuf, off_t off){ /* ** A wrapper around pwrite64(). */ -static int ts_pwrite64(int fd, const void *aBuf, size_t nBuf, off_t off){ +static ssize_t ts_pwrite64(int fd, const void *aBuf, size_t nBuf, sqlite3_uint64 off){ if( tsIsFailErrno("pwrite64") ){ return -1; } @@ -415,14 +418,14 @@ static void *ts_mremap(void *a, size_t b, size_t c, int d, ...){ return orig_mremap(a, b, c, d, pArg); } -static int test_syscall_install( +static int SQLITE_TCLAPI test_syscall_install( void * clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[] ){ sqlite3_vfs *pVfs; - int nElem; + Tcl_Size nElem; int i; Tcl_Obj **apElem; @@ -435,7 +438,7 @@ static int test_syscall_install( } pVfs = sqlite3_vfs_find(0); - for(i=0; i<nElem; i++){ + for(i=0; i<(int)nElem; i++){ int iCall; int rc = Tcl_GetIndexFromObjStruct(interp, apElem[i], aSyscall, sizeof(aSyscall[0]), "system-call", 0, &iCall @@ -451,7 +454,7 @@ static int test_syscall_install( return TCL_OK; } -static int test_syscall_uninstall( +static int SQLITE_TCLAPI test_syscall_uninstall( void * clientData, Tcl_Interp *interp, int objc, @@ -475,7 +478,7 @@ static int test_syscall_uninstall( return TCL_OK; } -static int test_syscall_reset( +static int SQLITE_TCLAPI test_syscall_reset( void * clientData, Tcl_Interp *interp, int objc, @@ -495,7 +498,7 @@ static int test_syscall_reset( rc = pVfs->xSetSystemCall(pVfs, 0, 0); for(i=0; aSyscall[i].zName; i++) aSyscall[i].xOrig = 0; }else{ - int nFunc; + Tcl_Size nFunc; char *zFunc = Tcl_GetStringFromObj(objv[2], &nFunc); rc = pVfs->xSetSystemCall(pVfs, Tcl_GetString(objv[2]), 0); for(i=0; rc==SQLITE_OK && aSyscall[i].zName; i++){ @@ -513,7 +516,7 @@ static int test_syscall_reset( return TCL_OK; } -static int test_syscall_exists( +static int SQLITE_TCLAPI test_syscall_exists( void * clientData, Tcl_Interp *interp, int objc, @@ -534,7 +537,7 @@ static int test_syscall_exists( return TCL_OK; } -static int test_syscall_fault( +static int SQLITE_TCLAPI test_syscall_fault( void * clientData, Tcl_Interp *interp, int objc, @@ -563,7 +566,7 @@ static int test_syscall_fault( return TCL_OK; } -static int test_syscall_errno( +static int SQLITE_TCLAPI test_syscall_errno( void * clientData, Tcl_Interp *interp, int objc, @@ -609,7 +612,7 @@ static int test_syscall_errno( return TCL_OK; } -static int test_syscall_list( +static int SQLITE_TCLAPI test_syscall_list( void * clientData, Tcl_Interp *interp, int objc, @@ -639,7 +642,7 @@ static int test_syscall_list( return TCL_OK; } -static int test_syscall_defaultvfs( +static int SQLITE_TCLAPI test_syscall_defaultvfs( void * clientData, Tcl_Interp *interp, int objc, @@ -661,7 +664,7 @@ static int ts_getpagesize(void){ return gSyscall.pgsz; } -static int test_syscall_pagesize( +static int SQLITE_TCLAPI test_syscall_pagesize( void * clientData, Tcl_Interp *interp, int objc, @@ -683,7 +686,7 @@ static int test_syscall_pagesize( } }else{ if( pgsz<512 || (pgsz & (pgsz-1)) ){ - Tcl_AppendResult(interp, "pgsz out of range", 0); + Tcl_AppendResult(interp, "pgsz out of range", NULL); return TCL_ERROR; } gSyscall.orig_getpagesize = pVfs->xGetSystemCall(pVfs, "getpagesize"); @@ -696,7 +699,7 @@ static int test_syscall_pagesize( return TCL_OK; } -static int test_syscall( +static int SQLITE_TCLAPI test_syscall( void * clientData, Tcl_Interp *interp, int objc, @@ -719,14 +722,20 @@ static int test_syscall( }; int iCmd; int rc; + sqlite3_vfs *pVfs = sqlite3_vfs_find(0); if( objc<2 ){ Tcl_WrongNumArgs(interp, 1, objv, "SUB-COMMAND ..."); return TCL_ERROR; } - rc = Tcl_GetIndexFromObjStruct(interp, - objv[1], aCmd, sizeof(aCmd[0]), "sub-command", 0, &iCmd - ); + if( pVfs->iVersion<3 || pVfs->xSetSystemCall==0 ){ + Tcl_AppendResult(interp, "VFS does not support xSetSystemCall", NULL); + rc = TCL_ERROR; + }else{ + rc = Tcl_GetIndexFromObjStruct(interp, + objv[1], aCmd, sizeof(aCmd[0]), "sub-command", 0, &iCmd + ); + } if( rc!=TCL_OK ) return rc; return aCmd[iCmd].xCmd(clientData, interp, objc, objv); } diff --git a/src/test_tclsh.c b/src/test_tclsh.c new file mode 100644 index 0000000000..989cb97a62 --- /dev/null +++ b/src/test_tclsh.c @@ -0,0 +1,200 @@ +/* +** 2017-10-13 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** This file contains extensions to the the "tclsqlite.c" module used for +** testing. Basically, all of the other "test_*.c" modules are linked +** into the enhanced tclsh used for testing (and named "testfixture" or +** "testfixture.exe") using logic encoded by this file. +** +** The code in this file used to be found in tclsqlite3.c, contained within +** #if SQLITE_TEST ... #endif. It is factored out into this separate module +** in an effort to keep the tclsqlite.c file pure. +*/ +#include "sqlite3.h" +#include "tclsqlite.h" + +/* Needed for the setrlimit() system call on unix */ +#if defined(unix) +#include <sys/resource.h> +#endif + +/* Forward declaration */ +static int SQLITE_TCLAPI load_testfixture_extensions( + ClientData cd, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +); + +/* +** This routine is the primary export of this file. +** +** Configure the interpreter passed as the first argument to have access +** to the commands and linked variables that make up: +** +** * the [sqlite3] extension itself, +** +** * If SQLITE_TCLMD5 or SQLITE_TEST is defined, the Md5 commands, and +** +** * If SQLITE_TEST is set, the various test interfaces used by the Tcl +** test suite. +*/ +const char *sqlite3TestInit(Tcl_Interp *interp){ + extern int Sqlite3_Init(Tcl_Interp*); + extern int Sqliteconfig_Init(Tcl_Interp*); + extern int Sqlitetest1_Init(Tcl_Interp*); + extern int Sqlitetest2_Init(Tcl_Interp*); + extern int Sqlitetest3_Init(Tcl_Interp*); + extern int Sqlitetest4_Init(Tcl_Interp*); + extern int Sqlitetest5_Init(Tcl_Interp*); + extern int Sqlitetest6_Init(Tcl_Interp*); + extern int Sqlitetest8_Init(Tcl_Interp*); + extern int Sqlitetest9_Init(Tcl_Interp*); + extern int Sqlitetest_autoext_Init(Tcl_Interp*); + extern int Sqlitetest_blob_Init(Tcl_Interp*); + extern int Sqlitetest_demovfs_Init(Tcl_Interp *); + extern int Sqlitetest_func_Init(Tcl_Interp*); + extern int Sqlitetest_hexio_Init(Tcl_Interp*); + extern int Sqlitetest_init_Init(Tcl_Interp*); + extern int Sqlitetest_malloc_Init(Tcl_Interp*); + extern int Sqlitetest_mutex_Init(Tcl_Interp*); + extern int Sqlitetestschema_Init(Tcl_Interp*); + extern int Sqlitetestsse_Init(Tcl_Interp*); + extern int Sqlitetesttclvar_Init(Tcl_Interp*); + extern int Sqlitetestfs_Init(Tcl_Interp*); + extern int SqlitetestThread_Init(Tcl_Interp*); + extern int SqlitetestOnefile_Init(); + extern int SqlitetestOsinst_Init(Tcl_Interp*); + extern int Sqlitetestbackup_Init(Tcl_Interp*); + extern int Sqlitetestintarray_Init(Tcl_Interp*); + extern int Sqlitetestvfs_Init(Tcl_Interp *); + extern int Sqlitetestrtree_Init(Tcl_Interp*); + extern int Sqlitetestrtreedoc_Init(Tcl_Interp*); + extern int Sqlitequota_Init(Tcl_Interp*); + extern int Sqlitemultiplex_Init(Tcl_Interp*); + extern int SqliteSuperlock_Init(Tcl_Interp*); + extern int SqlitetestSyscall_Init(Tcl_Interp*); +#if defined(SQLITE_ENABLE_SESSION) && defined(SQLITE_ENABLE_PREUPDATE_HOOK) + extern int TestSession_Init(Tcl_Interp*); +#endif + extern int Md5_Init(Tcl_Interp*); + extern int Fts5tcl_Init(Tcl_Interp *); + extern int SqliteRbu_Init(Tcl_Interp*); + extern int Sqlitetesttcl_Init(Tcl_Interp*); +#if defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4) + extern int Sqlitetestfts3_Init(Tcl_Interp *interp); +#endif +#ifdef SQLITE_ENABLE_ZIPVFS + extern int Zipvfs_Init(Tcl_Interp*); +#endif + extern int TestExpert_Init(Tcl_Interp*); + extern int Sqlitetest_window_Init(Tcl_Interp *); + extern int Sqlitetestvdbecov_Init(Tcl_Interp *); + extern int TestRecover_Init(Tcl_Interp*); + extern int Sqlitetestintck_Init(Tcl_Interp*); + + Tcl_CmdInfo cmdInfo; + + /* Since the primary use case for this binary is testing of SQLite, + ** be sure to generate core files if we crash */ +#if defined(unix) + { struct rlimit x; + getrlimit(RLIMIT_CORE, &x); + x.rlim_cur = x.rlim_max; + setrlimit(RLIMIT_CORE, &x); + } +#endif /* unix */ + + if( Tcl_GetCommandInfo(interp, "sqlite3", &cmdInfo)==0 ){ + Sqlite3_Init(interp); + } +#ifdef SQLITE_ENABLE_ZIPVFS + Zipvfs_Init(interp); +#endif + Md5_Init(interp); + Sqliteconfig_Init(interp); + Sqlitetest1_Init(interp); + Sqlitetest2_Init(interp); + Sqlitetest3_Init(interp); + Sqlitetest4_Init(interp); + Sqlitetest5_Init(interp); + Sqlitetest6_Init(interp); + Sqlitetest8_Init(interp); + Sqlitetest9_Init(interp); + Sqlitetest_autoext_Init(interp); + Sqlitetest_blob_Init(interp); + Sqlitetest_demovfs_Init(interp); + Sqlitetest_func_Init(interp); + Sqlitetest_hexio_Init(interp); + Sqlitetest_init_Init(interp); + Sqlitetest_malloc_Init(interp); + Sqlitetest_mutex_Init(interp); + Sqlitetestschema_Init(interp); + Sqlitetesttclvar_Init(interp); + Sqlitetestfs_Init(interp); + SqlitetestThread_Init(interp); + SqlitetestOnefile_Init(); + SqlitetestOsinst_Init(interp); + Sqlitetestbackup_Init(interp); + Sqlitetestintarray_Init(interp); + Sqlitetestvfs_Init(interp); + Sqlitetestrtree_Init(interp); + Sqlitetestrtreedoc_Init(interp); + Sqlitequota_Init(interp); + Sqlitemultiplex_Init(interp); + SqliteSuperlock_Init(interp); + SqlitetestSyscall_Init(interp); +#if defined(SQLITE_ENABLE_SESSION) && defined(SQLITE_ENABLE_PREUPDATE_HOOK) + TestSession_Init(interp); +#endif + Fts5tcl_Init(interp); + SqliteRbu_Init(interp); + Sqlitetesttcl_Init(interp); + +#if defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4) + Sqlitetestfts3_Init(interp); +#endif + TestExpert_Init(interp); + Sqlitetest_window_Init(interp); + Sqlitetestvdbecov_Init(interp); + TestRecover_Init(interp); + Sqlitetestintck_Init(interp); + + Tcl_CreateObjCommand( + interp, "load_testfixture_extensions", load_testfixture_extensions,0,0 + ); + return 0; +} + +/* tclcmd: load_testfixture_extensions +*/ +static int SQLITE_TCLAPI load_testfixture_extensions( + ClientData cd, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + + Tcl_Interp *slave; + if( objc!=2 ){ + Tcl_WrongNumArgs(interp, 1, objv, "SLAVE"); + return TCL_ERROR; + } + + slave = Tcl_GetSlave(interp, Tcl_GetString(objv[1])); + if( !slave ){ + return TCL_ERROR; + } + + (void)sqlite3TestInit(slave); + return TCL_OK; +} diff --git a/src/test_tclvar.c b/src/test_tclvar.c index 63ed394734..6299960a6c 100644 --- a/src/test_tclvar.c +++ b/src/test_tclvar.c @@ -15,9 +15,28 @@ ** ** The emphasis of this file is a virtual table that provides ** access to TCL variables. +** +** The TCLVAR eponymous virtual table has a schema like this: +** +** CREATE TABLE tclvar( +** name TEXT, -- base name of the variable: "x" in "$x(y)" +** arrayname TEXT, -- array index name: "y" in "$x(y)" +** value TEXT, -- the value of the variable +** fullname TEXT, -- the full name of the variable +** PRIMARY KEY(fullname) +** ) WITHOUT ROWID; +** +** DELETE, INSERT, and UPDATE operations use the "fullname" field to +** determine the variable to be modified. Changing "value" to NULL +** deletes the variable. +** +** For SELECT operations, the "name" and "arrayname" fields will always +** match the "fullname" field. For DELETE, INSERT, and UPDATE, the +** "name" and "arrayname" fields are ignored and the variable is modified +** according to "fullname" and "value" only. */ #include "sqliteInt.h" -#include "tcl.h" +#include "tclsqlite.h" #include <stdlib.h> #include <string.h> @@ -49,8 +68,8 @@ struct tclvar_cursor { Tcl_Obj *pList1; /* Result of [info vars ?pattern?] */ Tcl_Obj *pList2; /* Result of [array names [lindex $pList1 $i1]] */ - int i1; /* Current item in pList1 */ - int i2; /* Current item (if any) in pList2 */ + Tcl_Size i1; /* Current item in pList1 */ + Tcl_Size i2; /* Current item (if any) in pList2 */ }; /* Methods for the tclvar module */ @@ -63,7 +82,12 @@ static int tclvarConnect( ){ tclvar_vtab *pVtab; static const char zSchema[] = - "CREATE TABLE whatever(name TEXT, arrayname TEXT, value TEXT)"; + "CREATE TABLE x(" + " name TEXT," /* Base name */ + " arrayname TEXT," /* Array index */ + " value TEXT," /* Value */ + " fullname TEXT PRIMARY KEY" /* base(index) name */ + ") WITHOUT ROWID"; pVtab = sqlite3MallocZero( sizeof(*pVtab) ); if( pVtab==0 ) return SQLITE_NOMEM; *ppVtab = &pVtab->base; @@ -122,7 +146,7 @@ static int next2(Tcl_Interp *interp, tclvar_cursor *pCur, Tcl_Obj *pObj){ Tcl_IncrRefCount(pCur->pList2); assert( pCur->i2==0 ); }else{ - int n = 0; + Tcl_Size n = 0; pCur->i2++; Tcl_ListObjLength(0, pCur->pList2, &n); if( pCur->i2>=n ){ @@ -139,7 +163,7 @@ static int next2(Tcl_Interp *interp, tclvar_cursor *pCur, Tcl_Obj *pObj){ static int tclvarNext(sqlite3_vtab_cursor *cur){ Tcl_Obj *pObj; - int n = 0; + Tcl_Size n = 0; int ok = 0; tclvar_cursor *pCur = (tclvar_cursor *)cur; @@ -247,6 +271,16 @@ static int tclvarColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){ sqlite3_result_text(ctx, Tcl_GetString(pVal), -1, SQLITE_TRANSIENT); break; } + case 3: { + char *z3; + if( p2 ){ + z3 = sqlite3_mprintf("%s(%s)", z1, z2); + sqlite3_result_text(ctx, z3, -1, sqlite3_free); + }else{ + sqlite3_result_text(ctx, z1, -1, SQLITE_TRANSIENT); + } + break; + } } return SQLITE_OK; } @@ -372,6 +406,58 @@ static int tclvarBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){ return SQLITE_OK; } +/* +** Invoked for any UPDATE, INSERT, or DELETE against a tclvar table +*/ +static int tclvarUpdate( + sqlite3_vtab *tab, + int argc, + sqlite3_value **argv, + sqlite_int64 *pRowid +){ + tclvar_vtab *pTab = (tclvar_vtab*)tab; + if( argc==1 ){ + /* A DELETE operation. The variable to be deleted is stored in argv[0] */ + const char *zVar = (const char*)sqlite3_value_text(argv[0]); + Tcl_UnsetVar(pTab->interp, zVar, TCL_GLOBAL_ONLY); + return SQLITE_OK; + } + if( sqlite3_value_type(argv[0])==SQLITE_NULL ){ + /* An INSERT operation */ + const char *zValue = (const char*)sqlite3_value_text(argv[4]); + const char *zName; + if( sqlite3_value_type(argv[5])!=SQLITE_TEXT ){ + tab->zErrMsg = sqlite3_mprintf("the 'fullname' column must be TEXT"); + return SQLITE_ERROR; + } + zName = (const char*)sqlite3_value_text(argv[5]); + if( zValue ){ + Tcl_SetVar(pTab->interp, zName, zValue, TCL_GLOBAL_ONLY); + }else{ + Tcl_UnsetVar(pTab->interp, zName, TCL_GLOBAL_ONLY); + } + return SQLITE_OK; + } + if( sqlite3_value_type(argv[0])==SQLITE_TEXT + && sqlite3_value_type(argv[1])==SQLITE_TEXT + ){ + /* An UPDATE operation */ + const char *zOldName = (const char*)sqlite3_value_text(argv[0]); + const char *zNewName = (const char*)sqlite3_value_text(argv[1]); + const char *zValue = (const char*)sqlite3_value_text(argv[4]); + + if( strcmp(zOldName, zNewName)!=0 || zValue==0 ){ + Tcl_UnsetVar(pTab->interp, zOldName, TCL_GLOBAL_ONLY); + } + if( zValue!=0 ){ + Tcl_SetVar(pTab->interp, zNewName, zValue, TCL_GLOBAL_ONLY); + } + return SQLITE_OK; + } + tab->zErrMsg = sqlite3_mprintf("prohibited TCL variable change"); + return SQLITE_ERROR; +} + /* ** A virtual table module that provides read-only access to a ** Tcl global variable namespace. @@ -390,13 +476,18 @@ static sqlite3_module tclvarModule = { tclvarEof, /* xEof - check for end of scan */ tclvarColumn, /* xColumn - read data */ tclvarRowid, /* xRowid - read data */ - 0, /* xUpdate */ + tclvarUpdate, /* xUpdate */ 0, /* xBegin */ 0, /* xSync */ 0, /* xCommit */ 0, /* xRollback */ 0, /* xFindMethod */ 0, /* xRename */ + 0, /* xSavepoint */ + 0, /* xRelease */ + 0, /* xRollbackTo */ + 0, /* xShadowName */ + 0 /* xIntegrity */ }; /* @@ -407,7 +498,7 @@ extern int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb); /* ** Register the echo virtual table module. */ -static int register_tclvar_module( +static int SQLITE_TCLAPI register_tclvar_module( ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int objc, /* Number of arguments */ diff --git a/src/test_thread.c b/src/test_thread.c index a4d96e1942..62d9227caa 100644 --- a/src/test_thread.c +++ b/src/test_thread.c @@ -16,7 +16,7 @@ */ #include "sqliteInt.h" -#include <tcl.h> +#include "tclsqlite.h" #if SQLITE_THREADSAFE @@ -72,7 +72,7 @@ extern int sqlite3TestErrCode(Tcl_Interp *, sqlite3 *, int); /* ** Handler for events of type EvalEvent. */ -static int tclScriptEvent(Tcl_Event *evPtr, int flags){ +static int SQLITE_TCLAPI tclScriptEvent(Tcl_Event *evPtr, int flags){ int rc; EvalEvent *p = (EvalEvent *)evPtr; rc = Tcl_Eval(p->interp, p->zScript); @@ -90,7 +90,7 @@ static int tclScriptEvent(Tcl_Event *evPtr, int flags){ static void postToParent(SqlThread *p, Tcl_Obj *pScript){ EvalEvent *pEvent; char *zMsg; - int nMsg; + Tcl_Size nMsg; zMsg = Tcl_GetStringFromObj(pScript, &nMsg); pEvent = (EvalEvent *)ckalloc(sizeof(EvalEvent)+nMsg+1); @@ -167,7 +167,7 @@ static Tcl_ThreadCreateType tclScriptThread(ClientData pSqlThread){ ** ** The caller can wait for the script to terminate using [vwait VARNAME]. */ -static int sqlthread_spawn( +static int SQLITE_TCLAPI sqlthread_spawn( ClientData clientData, Tcl_Interp *interp, int objc, @@ -177,8 +177,8 @@ static int sqlthread_spawn( SqlThread *pNew; int rc; - int nVarname; char *zVarname; - int nScript; char *zScript; + Tcl_Size nVarname; char *zVarname; + Tcl_Size nScript; char *zScript; /* Parameters for thread creation */ const int nStack = TCL_THREAD_STACK_DEFAULT; @@ -201,7 +201,7 @@ static int sqlthread_spawn( rc = Tcl_CreateThread(&x, tclScriptThread, (void *)pNew, nStack, flags); if( rc!=TCL_OK ){ - Tcl_AppendResult(interp, "Error in Tcl_CreateThread()", 0); + Tcl_AppendResult(interp, "Error in Tcl_CreateThread()", NULL); ckfree((char *)pNew); return TCL_ERROR; } @@ -220,7 +220,7 @@ static int sqlthread_spawn( ** ** NOTE: At the moment, this doesn't work. FIXME. */ -static int sqlthread_parent( +static int SQLITE_TCLAPI sqlthread_parent( ClientData clientData, Tcl_Interp *interp, int objc, @@ -228,14 +228,14 @@ static int sqlthread_parent( ){ EvalEvent *pEvent; char *zMsg; - int nMsg; + Tcl_Size nMsg; SqlThread *p = (SqlThread *)clientData; assert(objc==3); UNUSED_PARAMETER(objc); if( p==0 ){ - Tcl_AppendResult(interp, "no parent thread", 0); + Tcl_AppendResult(interp, "no parent thread", NULL); return TCL_ERROR; } @@ -265,7 +265,7 @@ static int xBusy(void *pArg, int nBusy){ ** Open a database handle and return the string representation of ** the pointer value. */ -static int sqlthread_open( +static int SQLITE_TCLAPI sqlthread_open( ClientData clientData, Tcl_Interp *interp, int objc, @@ -276,13 +276,14 @@ static int sqlthread_open( const char *zFilename; sqlite3 *db; char zBuf[100]; - extern void Md5_Register(sqlite3*); + extern int Md5_Register(sqlite3*,char**,const sqlite3_api_routines*); UNUSED_PARAMETER(clientData); UNUSED_PARAMETER(objc); zFilename = Tcl_GetString(objv[2]); sqlite3_open(zFilename, &db); +/* BEGIN SQLCIPHER */ #ifdef SQLITE_HAS_CODEC if( db && objc>=4 ){ const char *zKey; @@ -299,11 +300,12 @@ static int sqlthread_open( } } #endif - Md5_Register(db); +/* END SQLCIPHER */ + Md5_Register(db, 0, 0); sqlite3_busy_handler(db, xBusy, 0); if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR; - Tcl_AppendResult(interp, zBuf, 0); + Tcl_AppendResult(interp, zBuf, NULL); return TCL_OK; } @@ -315,7 +317,7 @@ static int sqlthread_open( ** Return the current thread-id (Tcl_GetCurrentThread()) cast to ** an integer. */ -static int sqlthread_id( +static int SQLITE_TCLAPI sqlthread_id( ClientData clientData, Tcl_Interp *interp, int objc, @@ -333,7 +335,7 @@ static int sqlthread_id( /* ** Dispatch routine for the sub-commands of [sqlthread]. */ -static int sqlthread_proc( +static int SQLITE_TCLAPI sqlthread_proc( ClientData clientData, Tcl_Interp *interp, int objc, @@ -381,7 +383,7 @@ static int sqlthread_proc( ** implemented as a script in Tcl 8.5, it is not usually available to ** testfixture. */ -static int clock_seconds_proc( +static int SQLITE_TCLAPI clock_seconds_proc( ClientData clientData, Tcl_Interp *interp, int objc, @@ -396,6 +398,27 @@ static int clock_seconds_proc( return TCL_OK; } +/* +** The [clock_milliseconds] command. This is more or less the same as the +** regular tcl [clock milliseconds]. +*/ +static int SQLITE_TCLAPI clock_milliseconds_proc( + ClientData clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + Tcl_Time now; + Tcl_GetTime(&now); + Tcl_SetObjResult(interp, Tcl_NewWideIntObj( + ((Tcl_WideInt)now.sec * 1000) + (now.usec / 1000) + )); + UNUSED_PARAMETER(clientData); + UNUSED_PARAMETER(objc); + UNUSED_PARAMETER(objv); + return TCL_OK; +} + /************************************************************************* ** This block contains the implementation of the [sqlite3_blocking_step] ** command available to threads created by [sqlthread spawn] commands. It @@ -543,7 +566,7 @@ int sqlite3_blocking_prepare_v2( ** ** Advance the statement to the next row. */ -static int blocking_step_proc( +static int SQLITE_TCLAPI blocking_step_proc( void * clientData, Tcl_Interp *interp, int objc, @@ -569,7 +592,7 @@ static int blocking_step_proc( ** Usage: sqlite3_blocking_prepare_v2 DB sql bytes ?tailvar? ** Usage: sqlite3_nonblocking_prepare_v2 DB sql bytes ?tailvar? */ -static int blocking_prepare_v2_proc( +static int SQLITE_TCLAPI blocking_prepare_v2_proc( void * clientData, Tcl_Interp *interp, int objc, @@ -609,13 +632,13 @@ static int blocking_prepare_v2_proc( if( rc!=SQLITE_OK ){ assert( pStmt==0 ); sqlite3_snprintf(sizeof(zBuf), zBuf, "%s ", (char *)sqlite3ErrName(rc)); - Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0); + Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), NULL); return TCL_ERROR; } if( pStmt ){ if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR; - Tcl_AppendResult(interp, zBuf, 0); + Tcl_AppendResult(interp, zBuf, NULL); } return TCL_OK; } @@ -629,15 +652,26 @@ static int blocking_prepare_v2_proc( ** Register commands with the TCL interpreter. */ int SqlitetestThread_Init(Tcl_Interp *interp){ - Tcl_CreateObjCommand(interp, "sqlthread", sqlthread_proc, 0, 0); - Tcl_CreateObjCommand(interp, "clock_seconds", clock_seconds_proc, 0, 0); + struct TclCmd { + int (*xProc)(void*, Tcl_Interp*, int, Tcl_Obj*const*); + const char *zName; + int iCtx; + } aCmd[] = { + { sqlthread_proc, "sqlthread", 0 }, + { clock_seconds_proc, "clock_second", 0 }, + { clock_milliseconds_proc, "clock_milliseconds", 0 }, #if SQLITE_OS_UNIX && defined(SQLITE_ENABLE_UNLOCK_NOTIFY) - Tcl_CreateObjCommand(interp, "sqlite3_blocking_step", blocking_step_proc,0,0); - Tcl_CreateObjCommand(interp, - "sqlite3_blocking_prepare_v2", blocking_prepare_v2_proc, (void *)1, 0); - Tcl_CreateObjCommand(interp, - "sqlite3_nonblocking_prepare_v2", blocking_prepare_v2_proc, 0, 0); + { blocking_step_proc, "sqlite3_blocking_step", 0 }, + { blocking_prepare_v2_proc, "sqlite3_blocking_prepare_v2", 1 }, + { blocking_prepare_v2_proc, "sqlite3_nonblocking_prepare_v2", 0 }, #endif + }; + int ii; + + for(ii=0; ii<sizeof(aCmd)/sizeof(aCmd[0]); ii++){ + void *p = SQLITE_INT_TO_PTR(aCmd[ii].iCtx); + Tcl_CreateObjCommand(interp, aCmd[ii].zName, aCmd[ii].xProc, p, 0); + } return TCL_OK; } #else diff --git a/src/test_vdbecov.c b/src/test_vdbecov.c new file mode 100644 index 0000000000..283936aeb7 --- /dev/null +++ b/src/test_vdbecov.c @@ -0,0 +1,116 @@ +/* +** 2019 April 02 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +*/ +#if SQLITE_TEST /* This file is used for testing only */ + +#include "sqlite3.h" +#include "sqliteInt.h" +#include "tclsqlite.h" + +#ifdef SQLITE_VDBE_COVERAGE + +static u8 aBranchArray[200000]; + +static void test_vdbe_branch( + void *pCtx, + unsigned int iSrc, + unsigned char iBranch, + unsigned char iType +){ + if( iSrc<sizeof(aBranchArray) ){ + aBranchArray[iSrc] |= iBranch; + } +} + +static void appendToList( + Tcl_Obj *pList, + int iLine, + int iPath, + const char *zNever +){ + Tcl_Obj *pNew = Tcl_NewObj(); + Tcl_IncrRefCount(pNew); + Tcl_ListObjAppendElement(0, pNew, Tcl_NewIntObj(iLine)); + Tcl_ListObjAppendElement(0, pNew, Tcl_NewIntObj(iPath)); + Tcl_ListObjAppendElement(0, pNew, Tcl_NewStringObj(zNever, -1)); + Tcl_ListObjAppendElement(0, pList, pNew); + Tcl_DecrRefCount(pNew); +} + + +static int SQLITE_TCLAPI test_vdbe_coverage( + ClientData cd, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + const char *aSub[] = { "start", "report", "stop", 0 }; + int iSub = -1; + if( objc!=2 ){ + Tcl_WrongNumArgs(interp, 1, objv, "sub-command"); + return TCL_ERROR; + } + + if( Tcl_GetIndexFromObj(interp, objv[1], aSub, "sub-command", 0, &iSub) ){ + return TCL_ERROR; + } + + Tcl_ResetResult(interp); + assert( iSub==0 || iSub==1 || iSub==2 ); + switch( iSub ){ + case 0: /* start */ + memset(aBranchArray, 0, sizeof(aBranchArray)); + sqlite3_test_control(SQLITE_TESTCTRL_VDBE_COVERAGE, test_vdbe_branch, 0); + break; + case 1: { /* report */ + int i; + Tcl_Obj *pRes = Tcl_NewObj(); + Tcl_IncrRefCount(pRes); + for(i=0; i<sizeof(aBranchArray); i++){ + u8 b = aBranchArray[i]; + int bFlag = ((b >> 4)==4); + if( b ){ + if( (b & 0x01)==0 ){ + appendToList(pRes, i, 0, bFlag ? "less than" : "falls through"); + } + if( (b & 0x02)==0 ){ + appendToList(pRes, i, 1, bFlag ? "equal" : "taken"); + } + if( (b & 0x04)==0 ){ + appendToList(pRes, i, 2, bFlag ? "greater-than" : "NULL"); + } + } + } + Tcl_SetObjResult(interp, pRes); + Tcl_DecrRefCount(pRes); + break; + }; + + default: /* stop */ + sqlite3_test_control(SQLITE_TESTCTRL_VDBE_COVERAGE, 0, 0); + break; + } + + return TCL_OK; +} + +#endif /* SQLITE_VDBE_COVERAGE */ + +int Sqlitetestvdbecov_Init(Tcl_Interp *interp){ +#ifdef SQLITE_VDBE_COVERAGE + Tcl_CreateObjCommand(interp, "vdbe_coverage", test_vdbe_coverage, 0, 0); +#endif + return TCL_OK; +} + +#endif diff --git a/src/test_vfs.c b/src/test_vfs.c index a8c6ae7bf5..0d90a53a53 100644 --- a/src/test_vfs.c +++ b/src/test_vfs.c @@ -28,7 +28,7 @@ #include "sqlite3.h" #include "sqliteInt.h" -#include <tcl.h> +#include "tclsqlite.h" typedef struct Testvfs Testvfs; typedef struct TestvfsShm TestvfsShm; @@ -129,8 +129,10 @@ struct Testvfs { #define TESTVFS_UNLOCK_MASK 0x00020000 #define TESTVFS_LOCK_MASK 0x00040000 #define TESTVFS_CKLOCK_MASK 0x00080000 +#define TESTVFS_FCNTL_MASK 0x00100000 +#define TESTVFS_SLEEP_MASK 0x00200000 -#define TESTVFS_ALL_MASK 0x000FFFFF +#define TESTVFS_ALL_MASK 0x003FFFFF #define TESTVFS_MAX_PAGES 1024 @@ -223,11 +225,15 @@ static int tvfsResultCode(Testvfs *p, int *pRc){ int eCode; const char *zCode; } aCode[] = { - { SQLITE_OK, "SQLITE_OK" }, - { SQLITE_ERROR, "SQLITE_ERROR" }, - { SQLITE_IOERR, "SQLITE_IOERR" }, - { SQLITE_LOCKED, "SQLITE_LOCKED" }, - { SQLITE_BUSY, "SQLITE_BUSY" }, + { SQLITE_OK, "SQLITE_OK" }, + { SQLITE_ERROR, "SQLITE_ERROR" }, + { SQLITE_IOERR, "SQLITE_IOERR" }, + { SQLITE_LOCKED, "SQLITE_LOCKED" }, + { SQLITE_BUSY, "SQLITE_BUSY" }, + { SQLITE_READONLY, "SQLITE_READONLY" }, + { SQLITE_READONLY_CANTINIT, "SQLITE_READONLY_CANTINIT" }, + { SQLITE_NOTFOUND, "SQLITE_NOTFOUND" }, + { -1, "SQLITE_OMIT" }, }; const char *z; @@ -306,7 +312,6 @@ static void tvfsExecTcl( ** Close an tvfs-file. */ static int tvfsClose(sqlite3_file *pFile){ - int rc; TestvfsFile *pTestfile = (TestvfsFile *)pFile; TestvfsFd *pFd = pTestfile->pFd; Testvfs *p = (Testvfs *)pFd->pVfs->pAppData; @@ -324,10 +329,10 @@ static int tvfsClose(sqlite3_file *pFile){ if( pFile->pMethods ){ ckfree((char *)pFile->pMethods); } - rc = sqlite3OsClose(pFd->pReal); + sqlite3OsClose(pFd->pReal); ckfree((char *)pFd); pTestfile->pFd = 0; - return rc; + return SQLITE_OK; } /* @@ -376,6 +381,7 @@ static int tvfsWrite( Tcl_NewWideIntObj(iOfst), Tcl_NewIntObj(iAmt) ); tvfsResultCode(p, &rc); + if( rc<0 ) return SQLITE_OK; } if( rc==SQLITE_OK && tvfsInjectFullerr(p) ){ @@ -476,6 +482,9 @@ static int tvfsLock(sqlite3_file *pFile, int eLock){ tvfsExecTcl(p, "xLock", Tcl_NewStringObj(pFd->zFilename, -1), Tcl_NewStringObj(zLock, -1), 0, 0); } + if( p->mask&TESTVFS_LOCK_MASK && tvfsInjectIoerr(p) ){ + return SQLITE_IOERR_LOCK; + } return sqlite3OsLock(pFd->pReal, eLock); } @@ -491,7 +500,7 @@ static int tvfsUnlock(sqlite3_file *pFile, int eLock){ tvfsExecTcl(p, "xUnlock", Tcl_NewStringObj(pFd->zFilename, -1), Tcl_NewStringObj(zLock, -1), 0, 0); } - if( p->mask&TESTVFS_WRITE_MASK && tvfsInjectIoerr(p) ){ + if( p->mask&TESTVFS_UNLOCK_MASK && tvfsInjectIoerr(p) ){ return SQLITE_IOERR_UNLOCK; } return sqlite3OsUnlock(pFd->pReal, eLock); @@ -514,7 +523,8 @@ static int tvfsCheckReservedLock(sqlite3_file *pFile, int *pResOut){ ** File control method. For custom operations on an tvfs-file. */ static int tvfsFileControl(sqlite3_file *pFile, int op, void *pArg){ - TestvfsFd *p = tvfsGetFd(pFile); + TestvfsFd *pFd = tvfsGetFd(pFile); + Testvfs *p = (Testvfs *)pFd->pVfs->pAppData; if( op==SQLITE_FCNTL_PRAGMA ){ char **argv = (char**)pArg; if( sqlite3_stricmp(argv[1],"error")==0 ){ @@ -532,11 +542,35 @@ static int tvfsFileControl(sqlite3_file *pFile, int op, void *pArg){ return rc; } if( sqlite3_stricmp(argv[1], "filename")==0 ){ - argv[0] = sqlite3_mprintf("%s", p->zFilename); + argv[0] = sqlite3_mprintf("%s", pFd->zFilename); return SQLITE_OK; } } - return sqlite3OsFileControl(p->pReal, op, pArg); + if( p->pScript && (p->mask&TESTVFS_FCNTL_MASK) ){ + struct Fcntl { + int iFnctl; + const char *zFnctl; + } aF[] = { + { SQLITE_FCNTL_BEGIN_ATOMIC_WRITE, "BEGIN_ATOMIC_WRITE" }, + { SQLITE_FCNTL_COMMIT_ATOMIC_WRITE, "COMMIT_ATOMIC_WRITE" }, + { SQLITE_FCNTL_ZIPVFS, "ZIPVFS" }, + }; + int i; + for(i=0; i<sizeof(aF)/sizeof(aF[0]); i++){ + if( op==aF[i].iFnctl ) break; + } + if( i<sizeof(aF)/sizeof(aF[0]) ){ + int rc = 0; + tvfsExecTcl(p, "xFileControl", + Tcl_NewStringObj(pFd->zFilename, -1), + Tcl_NewStringObj(aF[i].zFnctl, -1), + 0, 0 + ); + tvfsResultCode(p, &rc); + if( rc ) return (rc<0 ? SQLITE_OK : rc); + } + } + return sqlite3OsFileControl(pFd->pReal, op, pArg); } /* @@ -780,6 +814,10 @@ static int tvfsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){ ** actually slept. */ static int tvfsSleep(sqlite3_vfs *pVfs, int nMicro){ + Testvfs *p = (Testvfs *)pVfs->pAppData; + if( p->pScript && (p->mask&TESTVFS_SLEEP_MASK) ){ + tvfsExecTcl(p, "xSleep", Tcl_NewIntObj(nMicro), 0, 0, 0); + } return sqlite3OsSleep(PARENTVFS(pVfs), nMicro); } @@ -837,7 +875,7 @@ static int tvfsShmOpen(sqlite3_file *pFile){ pFd->pNext = pBuffer->pFile; pBuffer->pFile = pFd; pFd->pShm = pBuffer; - return SQLITE_OK; + return rc; } static void tvfsAllocPage(TestvfsBuffer *p, int iPage, int pgsz){ @@ -861,7 +899,8 @@ static int tvfsShmMap( Testvfs *p = (Testvfs *)(pFd->pVfs->pAppData); if( p->isFullshm ){ - return sqlite3OsShmMap(pFd->pReal, iPage, pgsz, isWrite, pp); + sqlite3_file *pReal = pFd->pReal; + return pReal->pMethods->xShmMap(pReal, iPage, pgsz, isWrite, pp); } if( 0==pFd->pShm ){ @@ -890,7 +929,9 @@ static int tvfsShmMap( if( rc==SQLITE_OK && isWrite && !pFd->pShm->aPage[iPage] ){ tvfsAllocPage(pFd->pShm, iPage, pgsz); } - *pp = (void volatile *)pFd->pShm->aPage[iPage]; + if( rc==SQLITE_OK || rc==SQLITE_READONLY ){ + *pp = (void volatile *)pFd->pShm->aPage[iPage]; + } return rc; } @@ -909,7 +950,8 @@ static int tvfsShmLock( char zLock[80]; if( p->isFullshm ){ - return sqlite3OsShmLock(pFd->pReal, ofst, n, flags); + sqlite3_file *pReal = pFd->pReal; + return pReal->pMethods->xShmLock(pReal, ofst, n, flags); } if( p->pScript && p->mask&TESTVFS_SHMLOCK_MASK ){ @@ -973,7 +1015,8 @@ static void tvfsShmBarrier(sqlite3_file *pFile){ } if( p->isFullshm ){ - sqlite3OsShmBarrier(pFd->pReal); + sqlite3_file *pReal = pFd->pReal; + pReal->pMethods->xShmBarrier(pReal); return; } } @@ -989,7 +1032,8 @@ static int tvfsShmUnmap( TestvfsFd **ppFd; if( p->isFullshm ){ - return sqlite3OsShmUnmap(pFd->pReal, deleteFlag); + sqlite3_file *pReal = pFd->pReal; + return pReal->pMethods->xShmUnmap(pReal, deleteFlag); } if( !pBuffer ) return SQLITE_OK; @@ -1037,7 +1081,7 @@ static int tvfsUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *p){ return sqlite3OsUnfetch(pFd->pReal, iOfst, p); } -static int testvfs_obj_cmd( +static int SQLITE_TCLAPI testvfs_obj_cmd( ClientData cd, Tcl_Interp *interp, int objc, @@ -1094,7 +1138,7 @@ static int testvfs_obj_cmd( ); if( rc!=SQLITE_OK ){ Tcl_AppendResult(interp, "failed to get full path: ", - Tcl_GetString(objv[2]), 0); + Tcl_GetString(objv[2]), NULL); ckfree(zName); return TCL_ERROR; } @@ -1103,19 +1147,19 @@ static int testvfs_obj_cmd( } ckfree(zName); if( !pBuffer ){ - Tcl_AppendResult(interp, "no such file: ", Tcl_GetString(objv[2]), 0); + Tcl_AppendResult(interp, "no such file: ", Tcl_GetString(objv[2]), NULL); return TCL_ERROR; } if( objc==4 ){ - int n; + Tcl_Size n; u8 *a = Tcl_GetByteArrayFromObj(objv[3], &n); int pgsz = pBuffer->pgsz; if( pgsz==0 ) pgsz = 65536; - for(i=0; i*pgsz<n; i++){ + for(i=0; i*pgsz<(int)n; i++){ int nByte = pgsz; tvfsAllocPage(pBuffer, i, pgsz); if( n-i*pgsz<pgsz ){ - nByte = n; + nByte = (int)n; } memcpy(pBuffer->aPage[i], &a[i*pgsz], nByte); } @@ -1157,9 +1201,11 @@ static int testvfs_obj_cmd( { "xUnlock", TESTVFS_UNLOCK_MASK }, { "xLock", TESTVFS_LOCK_MASK }, { "xCheckReservedLock", TESTVFS_CKLOCK_MASK }, + { "xFileControl", TESTVFS_FCNTL_MASK }, + { "xSleep", TESTVFS_SLEEP_MASK }, }; Tcl_Obj **apElem = 0; - int nElem = 0; + Tcl_Size nElem = 0; int mask = 0; if( objc!=3 ){ Tcl_WrongNumArgs(interp, 2, objv, "LIST"); @@ -1169,7 +1215,7 @@ static int testvfs_obj_cmd( return TCL_ERROR; } Tcl_ResetResult(interp); - for(i=0; i<nElem; i++){ + for(i=0; i<(int)nElem; i++){ int iMethod; char *zElem = Tcl_GetString(apElem[i]); for(iMethod=0; iMethod<ArraySize(vfsmethod); iMethod++){ @@ -1179,7 +1225,7 @@ static int testvfs_obj_cmd( } } if( iMethod==ArraySize(vfsmethod) ){ - Tcl_AppendResult(interp, "unknown method: ", zElem, 0); + Tcl_AppendResult(interp, "unknown method: ", zElem, NULL); return TCL_ERROR; } } @@ -1195,7 +1241,7 @@ static int testvfs_obj_cmd( */ case CMD_SCRIPT: { if( objc==3 ){ - int nByte; + Tcl_Size nByte; if( p->pScript ){ Tcl_DecrRefCount(p->pScript); p->pScript = 0; @@ -1293,13 +1339,13 @@ static int testvfs_obj_cmd( int j; int iNew = 0; Tcl_Obj **flags = 0; - int nFlags = 0; + Tcl_Size nFlags = 0; if( Tcl_ListObjGetElements(interp, objv[2], &nFlags, &flags) ){ return TCL_ERROR; } - for(j=0; j<nFlags; j++){ + for(j=0; j<(int)nFlags; j++){ int idx = 0; if( Tcl_GetIndexFromObjStruct(interp, flags[j], aFlag, sizeof(aFlag[0]), "flag", 0, &idx) @@ -1307,7 +1353,7 @@ static int testvfs_obj_cmd( return TCL_ERROR; } if( aFlag[idx].iValue<0 && nFlags>1 ){ - Tcl_AppendResult(interp, "bad flags: ", Tcl_GetString(objv[2]), 0); + Tcl_AppendResult(interp, "bad flags: ", Tcl_GetString(objv[2]), NULL); return TCL_ERROR; } iNew |= aFlag[idx].iValue; @@ -1349,11 +1395,13 @@ static int testvfs_obj_cmd( return TCL_OK; } -static void testvfs_obj_del(ClientData cd){ +static void SQLITE_TCLAPI testvfs_obj_del(ClientData cd){ Testvfs *p = (Testvfs *)cd; if( p->pScript ) Tcl_DecrRefCount(p->pScript); sqlite3_vfs_unregister(p->pVfs); + memset(p->pVfs, 0, sizeof(sqlite3_vfs)); ckfree((char *)p->pVfs); + memset(p, 0, sizeof(Testvfs)); ckfree((char *)p); } @@ -1392,7 +1440,7 @@ static void testvfs_obj_del(ClientData cd){ ** ** where LOCK is of the form "OFFSET NBYTE lock/unlock shared/exclusive" */ -static int testvfs_cmd( +static int SQLITE_TCLAPI testvfs_cmd( ClientData cd, Tcl_Interp *interp, int objc, @@ -1445,7 +1493,7 @@ static int testvfs_cmd( if( objc<2 || 0!=(objc%2) ) goto bad_args; for(i=2; i<objc; i += 2){ - int nSwitch; + Tcl_Size nSwitch; char *zSwitch; zSwitch = Tcl_GetStringFromObj(objv[i], &nSwitch); @@ -1534,8 +1582,115 @@ static int testvfs_cmd( return TCL_ERROR; } +extern int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb); +extern const char *sqlite3ErrName(int); + +/* +** tclcmd: vfs_shmlock DB DBNAME (shared|exclusive) (lock|unlock) OFFSET N +*/ +static int SQLITE_TCLAPI test_vfs_shmlock( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + const char *azArg1[] = {"shared", "exclusive", 0}; + const char *azArg2[] = {"lock", "unlock", 0}; + sqlite3 *db = 0; + int rc = SQLITE_OK; + const char *zDbname = 0; + int iArg1 = 0; + int iArg2 = 0; + int iOffset = 0; + int n = 0; + sqlite3_file *pFd; + + if( objc!=7 ){ + Tcl_WrongNumArgs(interp, 1, objv, + "DB DBNAME (shared|exclusive) (lock|unlock) OFFSET N" + ); + return TCL_ERROR; + } + + zDbname = Tcl_GetString(objv[2]); + if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) + || Tcl_GetIndexFromObj(interp, objv[3], azArg1, "ARG", 0, &iArg1) + || Tcl_GetIndexFromObj(interp, objv[4], azArg2, "ARG", 0, &iArg2) + || Tcl_GetIntFromObj(interp, objv[5], &iOffset) + || Tcl_GetIntFromObj(interp, objv[6], &n) + ){ + return TCL_ERROR; + } + + sqlite3_file_control(db, zDbname, SQLITE_FCNTL_FILE_POINTER, (void*)&pFd); + if( pFd==0 ){ + return TCL_ERROR; + } + rc = pFd->pMethods->xShmLock(pFd, iOffset, n, + (iArg1==0 ? SQLITE_SHM_SHARED : SQLITE_SHM_EXCLUSIVE) + | (iArg2==0 ? SQLITE_SHM_LOCK : SQLITE_SHM_UNLOCK) + ); + Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1)); + return TCL_OK; +} + +static int SQLITE_TCLAPI test_vfs_set_readmark( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + sqlite3 *db = 0; + int rc = SQLITE_OK; + const char *zDbname = 0; + int iSlot = 0; + int iVal = -1; + sqlite3_file *pFd; + void volatile *pShm = 0; + u32 *aShm; + int iOff; + + if( objc!=4 && objc!=5 ){ + Tcl_WrongNumArgs(interp, 1, objv, "DB DBNAME SLOT ?VALUE?"); + return TCL_ERROR; + } + + zDbname = Tcl_GetString(objv[2]); + if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) + || Tcl_GetIntFromObj(interp, objv[3], &iSlot) + || (objc==5 && Tcl_GetIntFromObj(interp, objv[4], &iVal)) + ){ + return TCL_ERROR; + } + + sqlite3_file_control(db, zDbname, SQLITE_FCNTL_FILE_POINTER, (void*)&pFd); + if( pFd==0 ){ + return TCL_ERROR; + } + rc = pFd->pMethods->xShmMap(pFd, 0, 32*1024, 0, &pShm); + if( rc!=SQLITE_OK ){ + Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1)); + return TCL_ERROR; + } + if( pShm==0 ){ + Tcl_AppendResult(interp, "*-shm is not yet mapped", NULL); + return TCL_ERROR; + } + aShm = (u32*)pShm; + iOff = 12*2+1+iSlot; + + if( objc==5 ){ + aShm[iOff] = iVal; + } + Tcl_SetObjResult(interp, Tcl_NewIntObj(aShm[iOff])); + + return TCL_OK; +} + int Sqlitetestvfs_Init(Tcl_Interp *interp){ Tcl_CreateObjCommand(interp, "testvfs", testvfs_cmd, 0, 0); + Tcl_CreateObjCommand(interp, "vfs_shmlock", test_vfs_shmlock, 0, 0); + Tcl_CreateObjCommand(interp, "vfs_set_readmark", test_vfs_set_readmark, 0, 0); return TCL_OK; } diff --git a/src/test_windirent.c b/src/test_windirent.c deleted file mode 100644 index 11d7dc07d0..0000000000 --- a/src/test_windirent.c +++ /dev/null @@ -1,157 +0,0 @@ -/* -** 2015 November 30 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -************************************************************************* -** This file contains code to implement most of the opendir() family of -** POSIX functions on Win32 using the MSVCRT. -*/ - -#if defined(_WIN32) && defined(_MSC_VER) - -#include "test_windirent.h" - -/* -** Implementation of the POSIX opendir() function using the MSVCRT. -*/ -LPDIR opendir( - const char *dirname -){ - struct _finddata_t data; - LPDIR dirp = (LPDIR)sqlite3_malloc(sizeof(DIR)); - SIZE_T namesize = sizeof(data.name) / sizeof(data.name[0]); - - if( dirp==NULL ) return NULL; - memset(dirp, 0, sizeof(DIR)); - - /* TODO: Remove this if Unix-style root paths are not used. */ - if( sqlite3_stricmp(dirname, "/")==0 ){ - dirname = getenv("SystemDrive"); - } - - _snprintf(data.name, namesize, "%s\\*", dirname); - dirp->d_handle = _findfirst(data.name, &data); - - if( dirp->d_handle==BAD_INTPTR_T ){ - closedir(dirp); - return NULL; - } - - /* TODO: Remove this block to allow hidden and system files. */ - if( data.attrib&_A_HIDDEN || data.attrib&_A_SYSTEM ){ - if( _findnext(dirp->d_handle, &data)==-1 ){ - closedir(dirp); - return NULL; - } - } - - dirp->d_first.d_attributes = data.attrib; - strncpy(dirp->d_first.d_name, data.name, NAME_MAX); - dirp->d_first.d_name[NAME_MAX] = '\0'; - - return dirp; -} - -/* -** Implementation of the POSIX readdir() function using the MSVCRT. -*/ -LPDIRENT readdir( - LPDIR dirp -){ - struct _finddata_t data; - - if( dirp==NULL ) return NULL; - - if( dirp->d_first.d_ino==0 ){ - dirp->d_first.d_ino++; - dirp->d_next.d_ino++; - - return &dirp->d_first; - } - -next: - - if( _findnext(dirp->d_handle, &data)==-1 ) return NULL; - - /* TODO: Remove this block to allow hidden and system files. */ - if( data.attrib&_A_HIDDEN ) goto next; - if( data.attrib&_A_SYSTEM ) goto next; - - dirp->d_next.d_ino++; - dirp->d_next.d_attributes = data.attrib; - strncpy(dirp->d_next.d_name, data.name, NAME_MAX); - dirp->d_next.d_name[NAME_MAX] = '\0'; - - return &dirp->d_next; -} - -/* -** Implementation of the POSIX readdir_r() function using the MSVCRT. -*/ -INT readdir_r( - LPDIR dirp, - LPDIRENT entry, - LPDIRENT *result -){ - struct _finddata_t data; - - if( dirp==NULL ) return EBADF; - - if( dirp->d_first.d_ino==0 ){ - dirp->d_first.d_ino++; - dirp->d_next.d_ino++; - - entry->d_ino = dirp->d_first.d_ino; - entry->d_attributes = dirp->d_first.d_attributes; - strncpy(entry->d_name, dirp->d_first.d_name, NAME_MAX); - entry->d_name[NAME_MAX] = '\0'; - - *result = entry; - return 0; - } - -next: - - if( _findnext(dirp->d_handle, &data)==-1 ){ - *result = NULL; - return ENOENT; - } - - /* TODO: Remove this block to allow hidden and system files. */ - if( data.attrib&_A_HIDDEN ) goto next; - if( data.attrib&_A_SYSTEM ) goto next; - - entry->d_ino = (ino_t)-1; /* not available */ - entry->d_attributes = data.attrib; - strncpy(entry->d_name, data.name, NAME_MAX); - entry->d_name[NAME_MAX] = '\0'; - - *result = entry; - return 0; -} - -/* -** Implementation of the POSIX closedir() function using the MSVCRT. -*/ -INT closedir( - LPDIR dirp -){ - INT result = 0; - - if( dirp==NULL ) return EINVAL; - - if( dirp->d_handle!=NULL_INTPTR_T && dirp->d_handle!=BAD_INTPTR_T ){ - result = _findclose(dirp->d_handle); - } - - sqlite3_free(dirp); - return result; -} - -#endif /* defined(WIN32) && defined(_MSC_VER) */ diff --git a/src/test_windirent.h b/src/test_windirent.h deleted file mode 100644 index 0b8d1a7b51..0000000000 --- a/src/test_windirent.h +++ /dev/null @@ -1,105 +0,0 @@ -/* -** 2015 November 30 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -************************************************************************* -** This file contains declarations for most of the opendir() family of -** POSIX functions on Win32 using the MSVCRT. -*/ - -#if defined(_WIN32) && defined(_MSC_VER) - -/* -** We need several data types from the Windows SDK header. -*/ - -#define WIN32_LEAN_AND_MEAN -#include "windows.h" - -/* -** We need several support functions from the SQLite core. -*/ - -#include "sqlite3.h" - -/* -** We need several things from the ANSI and MSVCRT headers. -*/ - -#include <stdio.h> -#include <stdlib.h> -#include <errno.h> -#include <io.h> -#include <limits.h> - -/* -** We may need to provide the "ino_t" type. -*/ - -#ifndef INO_T_DEFINED - #define INO_T_DEFINED - typedef unsigned short ino_t; -#endif - -/* -** We need to define "NAME_MAX" if it was not present in "limits.h". -*/ - -#ifndef NAME_MAX -# ifdef FILENAME_MAX -# define NAME_MAX (FILENAME_MAX) -# else -# define NAME_MAX (260) -# endif -#endif - -/* -** We need to define "NULL_INTPTR_T" and "BAD_INTPTR_T". -*/ - -#ifndef NULL_INTPTR_T -# define NULL_INTPTR_T ((intptr_t)(0)) -#endif - -#ifndef BAD_INTPTR_T -# define BAD_INTPTR_T ((intptr_t)(-1)) -#endif - -/* -** We need to provide the necessary structures and related types. -*/ - -typedef struct DIRENT DIRENT; -typedef struct DIR DIR; -typedef DIRENT *LPDIRENT; -typedef DIR *LPDIR; - -struct DIRENT { - ino_t d_ino; /* Sequence number, do not use. */ - unsigned d_attributes; /* Win32 file attributes. */ - char d_name[NAME_MAX + 1]; /* Name within the directory. */ -}; - -struct DIR { - intptr_t d_handle; /* Value returned by "_findfirst". */ - DIRENT d_first; /* DIRENT constructed based on "_findfirst". */ - DIRENT d_next; /* DIRENT constructed based on "_findnext". */ -}; - -/* -** Finally, we can provide the function prototypes for the opendir(), -** readdir(), readdir_r(), and closedir() POSIX functions. -*/ - -extern LPDIR opendir(const char *dirname); -extern LPDIRENT readdir(LPDIR dirp); -extern INT readdir_r(LPDIR dirp, LPDIRENT entry, LPDIRENT *result); -extern INT closedir(LPDIR dirp); - -#endif /* defined(WIN32) && defined(_MSC_VER) */ diff --git a/src/test_window.c b/src/test_window.c new file mode 100644 index 0000000000..631b20162c --- /dev/null +++ b/src/test_window.c @@ -0,0 +1,349 @@ +/* +** 2018 June 17 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +*/ + +#include "sqlite3.h" + +#ifdef SQLITE_TEST + +#include "sqliteInt.h" +#include "tclsqlite.h" + +extern int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb); +extern const char *sqlite3ErrName(int); + +typedef struct TestWindow TestWindow; +struct TestWindow { + Tcl_Obj *xStep; + Tcl_Obj *xFinal; + Tcl_Obj *xValue; + Tcl_Obj *xInverse; + Tcl_Interp *interp; +}; + +typedef struct TestWindowCtx TestWindowCtx; +struct TestWindowCtx { + Tcl_Obj *pVal; +}; + +static void doTestWindowStep( + int bInverse, + sqlite3_context *ctx, + int nArg, + sqlite3_value **apArg +){ + int i; + TestWindow *p = (TestWindow*)sqlite3_user_data(ctx); + Tcl_Obj *pEval = Tcl_DuplicateObj(bInverse ? p->xInverse : p->xStep); + TestWindowCtx *pCtx = sqlite3_aggregate_context(ctx, sizeof(TestWindowCtx)); + + Tcl_IncrRefCount(pEval); + if( pCtx ){ + const char *zResult; + int rc; + if( pCtx->pVal ){ + Tcl_ListObjAppendElement(p->interp, pEval, Tcl_DuplicateObj(pCtx->pVal)); + }else{ + Tcl_ListObjAppendElement(p->interp, pEval, Tcl_NewStringObj("", -1)); + } + for(i=0; i<nArg; i++){ + Tcl_Obj *pArg; + pArg = Tcl_NewStringObj((const char*)sqlite3_value_text(apArg[i]), -1); + Tcl_ListObjAppendElement(p->interp, pEval, pArg); + } + rc = Tcl_EvalObjEx(p->interp, pEval, TCL_EVAL_GLOBAL); + if( rc!=TCL_OK ){ + zResult = Tcl_GetStringResult(p->interp); + sqlite3_result_error(ctx, zResult, -1); + }else{ + if( pCtx->pVal ) Tcl_DecrRefCount(pCtx->pVal); + pCtx->pVal = Tcl_DuplicateObj(Tcl_GetObjResult(p->interp)); + Tcl_IncrRefCount(pCtx->pVal); + } + } + Tcl_DecrRefCount(pEval); +} + +static void doTestWindowFinalize(int bValue, sqlite3_context *ctx){ + TestWindow *p = (TestWindow*)sqlite3_user_data(ctx); + Tcl_Obj *pEval = Tcl_DuplicateObj(bValue ? p->xValue : p->xFinal); + TestWindowCtx *pCtx = sqlite3_aggregate_context(ctx, sizeof(TestWindowCtx)); + + Tcl_IncrRefCount(pEval); + if( pCtx ){ + const char *zResult; + int rc; + if( pCtx->pVal ){ + Tcl_ListObjAppendElement(p->interp, pEval, Tcl_DuplicateObj(pCtx->pVal)); + }else{ + Tcl_ListObjAppendElement(p->interp, pEval, Tcl_NewStringObj("", -1)); + } + + rc = Tcl_EvalObjEx(p->interp, pEval, TCL_EVAL_GLOBAL); + zResult = Tcl_GetStringResult(p->interp); + if( rc!=TCL_OK ){ + sqlite3_result_error(ctx, zResult, -1); + }else{ + sqlite3_result_text(ctx, zResult, -1, SQLITE_TRANSIENT); + } + + if( bValue==0 ){ + if( pCtx->pVal ) Tcl_DecrRefCount(pCtx->pVal); + pCtx->pVal = 0; + } + } + Tcl_DecrRefCount(pEval); +} + +static void testWindowStep( + sqlite3_context *ctx, + int nArg, + sqlite3_value **apArg +){ + doTestWindowStep(0, ctx, nArg, apArg); +} +static void testWindowInverse( + sqlite3_context *ctx, + int nArg, + sqlite3_value **apArg +){ + doTestWindowStep(1, ctx, nArg, apArg); +} + +static void testWindowFinal(sqlite3_context *ctx){ + doTestWindowFinalize(0, ctx); +} +static void testWindowValue(sqlite3_context *ctx){ + doTestWindowFinalize(1, ctx); +} + +static void testWindowDestroy(void *pCtx){ + ckfree(pCtx); +} + +/* +** Usage: sqlite3_create_window_function DB NAME XSTEP XFINAL XVALUE XINVERSE +*/ +static int SQLITE_TCLAPI test_create_window( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + TestWindow *pNew; + sqlite3 *db; + const char *zName; + int rc; + + if( objc!=7 ){ + Tcl_WrongNumArgs(interp, 1, objv, "DB NAME XSTEP XFINAL XVALUE XINVERSE"); + return TCL_ERROR; + } + + if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; + zName = Tcl_GetString(objv[2]); + pNew = (TestWindow*)ckalloc(sizeof(TestWindow)); + memset(pNew, 0, sizeof(TestWindow)); + pNew->xStep = Tcl_DuplicateObj(objv[3]); + pNew->xFinal = Tcl_DuplicateObj(objv[4]); + pNew->xValue = Tcl_DuplicateObj(objv[5]); + pNew->xInverse = Tcl_DuplicateObj(objv[6]); + pNew->interp = interp; + + Tcl_IncrRefCount(pNew->xStep); + Tcl_IncrRefCount(pNew->xFinal); + Tcl_IncrRefCount(pNew->xValue); + Tcl_IncrRefCount(pNew->xInverse); + + rc = sqlite3_create_window_function(db, zName, -1, SQLITE_UTF8, (void*)pNew, + testWindowStep, testWindowFinal, testWindowValue, testWindowInverse, + testWindowDestroy + ); + if( rc!=SQLITE_OK ){ + Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1)); + return TCL_ERROR; + } + + return TCL_OK; +} + +static int SQLITE_TCLAPI test_create_window_misuse( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + sqlite3 *db; + int rc; + + if( objc!=2 ){ + Tcl_WrongNumArgs(interp, 1, objv, "DB"); + return TCL_ERROR; + } + if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; + + rc = sqlite3_create_window_function(db, "fff", -1, SQLITE_UTF8, 0, + 0, testWindowFinal, testWindowValue, testWindowInverse, + 0 + ); + if( rc!=SQLITE_MISUSE ) goto error; + rc = sqlite3_create_window_function(db, "fff", -1, SQLITE_UTF8, 0, + testWindowStep, 0, testWindowValue, testWindowInverse, + 0 + ); + if( rc!=SQLITE_MISUSE ) goto error; + rc = sqlite3_create_window_function(db, "fff", -1, SQLITE_UTF8, 0, + testWindowStep, testWindowFinal, 0, testWindowInverse, + 0 + ); + if( rc!=SQLITE_MISUSE ) goto error; + rc = sqlite3_create_window_function(db, "fff", -1, SQLITE_UTF8, 0, + testWindowStep, testWindowFinal, testWindowValue, 0, + 0 + ); + if( rc!=SQLITE_MISUSE ) goto error; + + return TCL_OK; + + error: + Tcl_SetObjResult(interp, Tcl_NewStringObj("misuse test error", -1)); + return TCL_ERROR; +} + +/* +** xStep for sumint(). +*/ +static void sumintStep( + sqlite3_context *ctx, + int nArg, + sqlite3_value *apArg[] +){ + sqlite3_int64 *pInt; + + assert( nArg==1 ); + if( sqlite3_value_type(apArg[0])!=SQLITE_INTEGER ){ + sqlite3_result_error(ctx, "invalid argument", -1); + return; + } + pInt = (sqlite3_int64*)sqlite3_aggregate_context(ctx, sizeof(sqlite3_int64)); + if( pInt ){ + *pInt += sqlite3_value_int64(apArg[0]); + } +} + +/* +** xInverse for sumint(). +*/ +static void sumintInverse( + sqlite3_context *ctx, + int nArg, + sqlite3_value *apArg[] +){ + sqlite3_int64 *pInt; + pInt = (sqlite3_int64*)sqlite3_aggregate_context(ctx, sizeof(sqlite3_int64)); + *pInt -= sqlite3_value_int64(apArg[0]); +} + +/* +** xFinal for sumint(). +*/ +static void sumintFinal(sqlite3_context *ctx){ + sqlite3_int64 res = 0; + sqlite3_int64 *pInt; + pInt = (sqlite3_int64*)sqlite3_aggregate_context(ctx, 0); + if( pInt ) res = *pInt; + sqlite3_result_int64(ctx, res); +} + +/* +** xValue for sumint(). +*/ +static void sumintValue(sqlite3_context *ctx){ + sqlite3_int64 res = 0; + sqlite3_int64 *pInt; + pInt = (sqlite3_int64*)sqlite3_aggregate_context(ctx, 0); + if( pInt ) res = *pInt; + sqlite3_result_int64(ctx, res); +} + +static int SQLITE_TCLAPI test_create_sumint( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + sqlite3 *db; + int rc; + + if( objc!=2 ){ + Tcl_WrongNumArgs(interp, 1, objv, "DB"); + return TCL_ERROR; + } + if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; + + rc = sqlite3_create_window_function(db, "sumint", 1, SQLITE_UTF8, 0, + sumintStep, sumintFinal, sumintValue, sumintInverse, + 0 + ); + + if( rc!=SQLITE_OK ){ + Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1)); + return TCL_ERROR; + } + return TCL_OK; +} + +static int SQLITE_TCLAPI test_override_sum( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + sqlite3 *db; + int rc; + + if( objc!=2 ){ + Tcl_WrongNumArgs(interp, 1, objv, "DB"); + return TCL_ERROR; + } + if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; + + rc = sqlite3_create_function(db, "sum", -1, SQLITE_UTF8, 0, + 0, sumintStep, sumintFinal + ); + + if( rc!=SQLITE_OK ){ + Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1)); + return TCL_ERROR; + } + return TCL_OK; +} + +int Sqlitetest_window_Init(Tcl_Interp *interp){ + static struct { + char *zName; + Tcl_ObjCmdProc *xProc; + int clientData; + } aObjCmd[] = { + { "sqlite3_create_window_function", test_create_window, 0 }, + { "test_create_window_function_misuse", test_create_window_misuse, 0 }, + { "test_create_sumint", test_create_sumint, 0 }, + { "test_override_sum", test_override_sum, 0 }, + }; + int i; + for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){ + ClientData c = (ClientData)SQLITE_INT_TO_PTR(aObjCmd[i].clientData); + Tcl_CreateObjCommand(interp, aObjCmd[i].zName, aObjCmd[i].xProc, c, 0); + } + return TCL_OK; +} +#endif diff --git a/src/threads.c b/src/threads.c index 251b9b7631..f128d69fc2 100644 --- a/src/threads.c +++ b/src/threads.c @@ -63,7 +63,7 @@ int sqlite3ThreadCreate( *ppThread = 0; p = sqlite3Malloc(sizeof(*p)); - if( p==0 ) return SQLITE_NOMEM; + if( p==0 ) return SQLITE_NOMEM_BKPT; memset(p, 0, sizeof(*p)); p->xTask = xTask; p->pIn = pIn; @@ -89,7 +89,7 @@ int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){ int rc; assert( ppOut!=0 ); - if( NEVER(p==0) ) return SQLITE_NOMEM; + if( NEVER(p==0) ) return SQLITE_NOMEM_BKPT; if( p->done ){ *ppOut = p->pOut; rc = SQLITE_OK; @@ -154,7 +154,7 @@ int sqlite3ThreadCreate( assert( xTask!=0 ); *ppThread = 0; p = sqlite3Malloc(sizeof(*p)); - if( p==0 ) return SQLITE_NOMEM; + if( p==0 ) return SQLITE_NOMEM_BKPT; /* If the SQLITE_TESTCTRL_FAULT_INSTALL callback is registered to a ** function that returns SQLITE_ERROR when passed the argument 200, that ** forces worker threads to run sequentially and deterministically @@ -186,7 +186,7 @@ int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){ BOOL bRc; assert( ppOut!=0 ); - if( NEVER(p==0) ) return SQLITE_NOMEM; + if( NEVER(p==0) ) return SQLITE_NOMEM_BKPT; if( p->xTask==0 ){ /* assert( p->id==GetCurrentThreadId() ); */ rc = WAIT_OBJECT_0; @@ -234,7 +234,7 @@ int sqlite3ThreadCreate( assert( xTask!=0 ); *ppThread = 0; p = sqlite3Malloc(sizeof(*p)); - if( p==0 ) return SQLITE_NOMEM; + if( p==0 ) return SQLITE_NOMEM_BKPT; if( (SQLITE_PTR_TO_INT(p)/17)&1 ){ p->xTask = xTask; p->pIn = pIn; @@ -250,7 +250,7 @@ int sqlite3ThreadCreate( int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){ assert( ppOut!=0 ); - if( NEVER(p==0) ) return SQLITE_NOMEM; + if( NEVER(p==0) ) return SQLITE_NOMEM_BKPT; if( p->xTask ){ *ppOut = p->xTask(p->pIn); }else{ @@ -261,7 +261,7 @@ int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){ #if defined(SQLITE_TEST) { void *pTstAlloc = sqlite3Malloc(10); - if (!pTstAlloc) return SQLITE_NOMEM; + if (!pTstAlloc) return SQLITE_NOMEM_BKPT; sqlite3_free(pTstAlloc); } #endif diff --git a/src/tokenize.c b/src/tokenize.c index 9b3444ac82..152ada64f6 100644 --- a/src/tokenize.c +++ b/src/tokenize.c @@ -27,8 +27,8 @@ ** all of them need to be used within the switch. */ #define CC_X 0 /* The letter 'x', or start of BLOB literal */ -#define CC_KYWD 1 /* Alphabetics or '_'. Usable in a keyword */ -#define CC_ID 2 /* unicode characters usable in IDs */ +#define CC_KYWD0 1 /* First letter of a keyword */ +#define CC_KYWD 2 /* Alphabetics or '_'. Usable in a keyword */ #define CC_DIGIT 3 /* Digits */ #define CC_DOLLAR 4 /* '$' */ #define CC_VARALPHA 5 /* '@', '#', ':'. Alphabetic SQL variables */ @@ -53,46 +53,49 @@ #define CC_AND 24 /* '&' */ #define CC_TILDA 25 /* '~' */ #define CC_DOT 26 /* '.' */ -#define CC_ILLEGAL 27 /* Illegal character */ +#define CC_ID 27 /* unicode characters usable in IDs */ +#define CC_ILLEGAL 28 /* Illegal character */ +#define CC_NUL 29 /* 0x00 */ +#define CC_BOM 30 /* First byte of UTF8 BOM: 0xEF 0xBB 0xBF */ static const unsigned char aiClass[] = { #ifdef SQLITE_ASCII /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */ -/* 0x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 7, 7, 27, 7, 7, 27, 27, -/* 1x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, +/* 0x */ 29, 28, 28, 28, 28, 28, 28, 28, 28, 7, 7, 28, 7, 7, 28, 28, +/* 1x */ 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, /* 2x */ 7, 15, 8, 5, 4, 22, 24, 8, 17, 18, 21, 20, 23, 11, 26, 16, /* 3x */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 19, 12, 14, 13, 6, /* 4x */ 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -/* 5x */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 9, 27, 27, 27, 1, +/* 5x */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 2, 2, 9, 28, 28, 28, 2, /* 6x */ 8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -/* 7x */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 27, 10, 27, 25, 27, -/* 8x */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -/* 9x */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -/* Ax */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -/* Bx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -/* Cx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -/* Dx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -/* Ex */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -/* Fx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 +/* 7x */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 2, 2, 28, 10, 28, 25, 28, +/* 8x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, +/* 9x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, +/* Ax */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, +/* Bx */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, +/* Cx */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, +/* Dx */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, +/* Ex */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 30, +/* Fx */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27 #endif #ifdef SQLITE_EBCDIC /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */ -/* 0x */ 27, 27, 27, 27, 27, 7, 27, 27, 27, 27, 27, 27, 7, 7, 27, 27, -/* 1x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, -/* 2x */ 27, 27, 27, 27, 27, 7, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, -/* 3x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, -/* 4x */ 7, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 12, 17, 20, 10, -/* 5x */ 24, 27, 27, 27, 27, 27, 27, 27, 27, 27, 15, 4, 21, 18, 19, 27, -/* 6x */ 11, 16, 27, 27, 27, 27, 27, 27, 27, 27, 27, 23, 22, 1, 13, 7, -/* 7x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 8, 5, 5, 5, 8, 14, 8, -/* 8x */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27, -/* 9x */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27, -/* 9x */ 25, 1, 1, 1, 1, 1, 1, 0, 1, 1, 27, 27, 27, 27, 27, 27, -/* Bx */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 9, 27, 27, 27, 27, 27, -/* Cx */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27, -/* Dx */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27, -/* Ex */ 27, 27, 1, 1, 1, 1, 1, 0, 1, 1, 27, 27, 27, 27, 27, 27, -/* Fx */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 27, 27, 27, 27, 27, 27, +/* 0x */ 29, 28, 28, 28, 28, 7, 28, 28, 28, 28, 28, 28, 7, 7, 28, 28, +/* 1x */ 28, 28, 28, 28, 28, 7, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, +/* 2x */ 28, 28, 28, 28, 28, 7, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, +/* 3x */ 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, +/* 4x */ 7, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 26, 12, 17, 20, 10, +/* 5x */ 24, 28, 28, 28, 28, 28, 28, 28, 28, 28, 15, 4, 21, 18, 19, 28, +/* 6x */ 11, 16, 28, 28, 28, 28, 28, 28, 28, 28, 28, 23, 22, 2, 13, 6, +/* 7x */ 28, 28, 28, 28, 28, 28, 28, 28, 28, 8, 5, 5, 5, 8, 14, 8, +/* 8x */ 28, 1, 1, 1, 1, 1, 1, 1, 1, 1, 28, 28, 28, 28, 28, 28, +/* 9x */ 28, 1, 1, 1, 1, 1, 1, 1, 1, 1, 28, 28, 28, 28, 28, 28, +/* Ax */ 28, 25, 1, 1, 1, 1, 1, 0, 2, 2, 28, 28, 28, 28, 28, 28, +/* Bx */ 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 9, 28, 28, 28, 28, 28, +/* Cx */ 28, 1, 1, 1, 1, 1, 1, 1, 1, 1, 28, 28, 28, 28, 28, 28, +/* Dx */ 28, 1, 1, 1, 1, 1, 1, 1, 1, 1, 28, 28, 28, 28, 28, 28, +/* Ex */ 28, 28, 1, 1, 1, 1, 1, 0, 2, 2, 28, 28, 28, 28, 28, 28, +/* Fx */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 28, 28, 28, 28, 28, 28, #endif }; @@ -183,18 +186,93 @@ const char sqlite3IsEbcdicIdChar[] = { #define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40])) #endif -/* Make the IdChar function accessible from ctime.c */ -#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS +/* Make the IdChar function accessible from ctime.c and alter.c */ int sqlite3IsIdChar(u8 c){ return IdChar(c); } -#endif +#ifndef SQLITE_OMIT_WINDOWFUNC +/* +** Return the id of the next token in string (*pz). Before returning, set +** (*pz) to point to the byte following the parsed token. +*/ +static int getToken(const unsigned char **pz){ + const unsigned char *z = *pz; + int t; /* Token type to return */ + do { + z += sqlite3GetToken(z, &t); + }while( t==TK_SPACE || t==TK_COMMENT ); + if( t==TK_ID + || t==TK_STRING + || t==TK_JOIN_KW + || t==TK_WINDOW + || t==TK_OVER + || sqlite3ParserFallback(t)==TK_ID + ){ + t = TK_ID; + } + *pz = z; + return t; +} + +/* +** The following three functions are called immediately after the tokenizer +** reads the keywords WINDOW, OVER and FILTER, respectively, to determine +** whether the token should be treated as a keyword or an SQL identifier. +** This cannot be handled by the usual lemon %fallback method, due to +** the ambiguity in some constructions. e.g. +** +** SELECT sum(x) OVER ... +** +** In the above, "OVER" might be a keyword, or it might be an alias for the +** sum(x) expression. If a "%fallback ID OVER" directive were added to +** grammar, then SQLite would always treat "OVER" as an alias, making it +** impossible to call a window-function without a FILTER clause. +** +** WINDOW is treated as a keyword if: +** +** * the following token is an identifier, or a keyword that can fallback +** to being an identifier, and +** * the token after than one is TK_AS. +** +** OVER is a keyword if: +** +** * the previous token was TK_RP, and +** * the next token is either TK_LP or an identifier. +** +** FILTER is a keyword if: +** +** * the previous token was TK_RP, and +** * the next token is TK_LP. +*/ +static int analyzeWindowKeyword(const unsigned char *z){ + int t; + t = getToken(&z); + if( t!=TK_ID ) return TK_ID; + t = getToken(&z); + if( t!=TK_AS ) return TK_ID; + return TK_WINDOW; +} +static int analyzeOverKeyword(const unsigned char *z, int lastToken){ + if( lastToken==TK_RP ){ + int t = getToken(&z); + if( t==TK_LP || t==TK_ID ) return TK_OVER; + } + return TK_ID; +} +static int analyzeFilterKeyword(const unsigned char *z, int lastToken){ + if( lastToken==TK_RP && getToken(&z)==TK_LP ){ + return TK_FILTER; + } + return TK_ID; +} +#endif /* SQLITE_OMIT_WINDOWFUNC */ /* ** Return the length (in bytes) of the token that begins at z[0]. ** Store the token type in *tokenType before returning. */ -int sqlite3GetToken(const unsigned char *z, int *tokenType){ - int i, c; +i64 sqlite3GetToken(const unsigned char *z, int *tokenType){ + i64 i; + int c; switch( aiClass[*z] ){ /* Switch on the character-class of the first byte ** of the token. See the comment on the CC_ defines ** above. */ @@ -211,8 +289,11 @@ int sqlite3GetToken(const unsigned char *z, int *tokenType){ case CC_MINUS: { if( z[1]=='-' ){ for(i=2; (c=z[i])!=0 && c!='\n'; i++){} - *tokenType = TK_SPACE; /* IMP: R-22934-25134 */ + *tokenType = TK_COMMENT; return i; + }else if( z[1]=='>' ){ + *tokenType = TK_PTR; + return 2 + (z[2]=='>'); } *tokenType = TK_MINUS; return 1; @@ -244,7 +325,7 @@ int sqlite3GetToken(const unsigned char *z, int *tokenType){ } for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){} if( c ) i++; - *tokenType = TK_SPACE; /* IMP: R-22934-25134 */ + *tokenType = TK_COMMENT; return i; } case CC_PERCENT: { @@ -285,7 +366,7 @@ int sqlite3GetToken(const unsigned char *z, int *tokenType){ case CC_BANG: { if( z[1]!='=' ){ *tokenType = TK_ILLEGAL; - return 2; + return 1; }else{ *tokenType = TK_NE; return 2; @@ -347,36 +428,68 @@ int sqlite3GetToken(const unsigned char *z, int *tokenType){ } /* If the next character is a digit, this is a floating point ** number that begins with ".". Fall thru into the next case */ + /* no break */ deliberate_fall_through } case CC_DIGIT: { testcase( z[0]=='0' ); testcase( z[0]=='1' ); testcase( z[0]=='2' ); testcase( z[0]=='3' ); testcase( z[0]=='4' ); testcase( z[0]=='5' ); testcase( z[0]=='6' ); testcase( z[0]=='7' ); testcase( z[0]=='8' ); - testcase( z[0]=='9' ); + testcase( z[0]=='9' ); testcase( z[0]=='.' ); *tokenType = TK_INTEGER; #ifndef SQLITE_OMIT_HEX_INTEGER if( z[0]=='0' && (z[1]=='x' || z[1]=='X') && sqlite3Isxdigit(z[2]) ){ - for(i=3; sqlite3Isxdigit(z[i]); i++){} - return i; - } + for(i=3; 1; i++){ + if( sqlite3Isxdigit(z[i])==0 ){ + if( z[i]==SQLITE_DIGIT_SEPARATOR ){ + *tokenType = TK_QNUMBER; + }else{ + break; + } + } + } + }else #endif - for(i=0; sqlite3Isdigit(z[i]); i++){} + { + for(i=0; 1; i++){ + if( sqlite3Isdigit(z[i])==0 ){ + if( z[i]==SQLITE_DIGIT_SEPARATOR ){ + *tokenType = TK_QNUMBER; + }else{ + break; + } + } + } #ifndef SQLITE_OMIT_FLOATING_POINT - if( z[i]=='.' ){ - i++; - while( sqlite3Isdigit(z[i]) ){ i++; } - *tokenType = TK_FLOAT; - } - if( (z[i]=='e' || z[i]=='E') && - ( sqlite3Isdigit(z[i+1]) - || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2])) - ) - ){ - i += 2; - while( sqlite3Isdigit(z[i]) ){ i++; } - *tokenType = TK_FLOAT; - } + if( z[i]=='.' ){ + if( *tokenType==TK_INTEGER ) *tokenType = TK_FLOAT; + for(i++; 1; i++){ + if( sqlite3Isdigit(z[i])==0 ){ + if( z[i]==SQLITE_DIGIT_SEPARATOR ){ + *tokenType = TK_QNUMBER; + }else{ + break; + } + } + } + } + if( (z[i]=='e' || z[i]=='E') && + ( sqlite3Isdigit(z[i+1]) + || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2])) + ) + ){ + if( *tokenType==TK_INTEGER ) *tokenType = TK_FLOAT; + for(i+=2; 1; i++){ + if( sqlite3Isdigit(z[i])==0 ){ + if( z[i]==SQLITE_DIGIT_SEPARATOR ){ + *tokenType = TK_QNUMBER; + }else{ + break; + } + } + } + } #endif + } while( IdChar(z[i]) ){ *tokenType = TK_ILLEGAL; i++; @@ -423,8 +536,9 @@ int sqlite3GetToken(const unsigned char *z, int *tokenType){ if( n==0 ) *tokenType = TK_ILLEGAL; return i; } - case CC_KYWD: { - for(i=1; aiClass[z[i]]<=CC_KYWD; i++){} + case CC_KYWD0: { + if( aiClass[z[1]]>CC_KYWD ){ i = 1; break; } + for(i=2; aiClass[z[i]]<=CC_KYWD; i++){} if( IdChar(z[i]) ){ /* This token started out using characters that can appear in keywords, ** but z[i] is a character not allowed within keywords, so this must @@ -435,8 +549,8 @@ int sqlite3GetToken(const unsigned char *z, int *tokenType){ *tokenType = TK_ID; return keywordCode((char*)z, i, tokenType); } -#ifndef SQLITE_OMIT_BLOB_LITERAL case CC_X: { +#ifndef SQLITE_OMIT_BLOB_LITERAL testcase( z[0]=='x' ); testcase( z[0]=='X' ); if( z[1]=='\'' ){ *tokenType = TK_BLOB; @@ -448,14 +562,28 @@ int sqlite3GetToken(const unsigned char *z, int *tokenType){ if( z[i] ) i++; return i; } +#endif /* If it is not a BLOB literal, then it must be an ID, since no ** SQL keywords start with the letter 'x'. Fall through */ + /* no break */ deliberate_fall_through } -#endif + case CC_KYWD: case CC_ID: { i = 1; break; } + case CC_BOM: { + if( z[1]==0xbb && z[2]==0xbf ){ + *tokenType = TK_SPACE; + return 3; + } + i = 1; + break; + } + case CC_NUL: { + *tokenType = TK_ILLEGAL; + return 0; + } default: { *tokenType = TK_ILLEGAL; return 1; @@ -467,79 +595,128 @@ int sqlite3GetToken(const unsigned char *z, int *tokenType){ } /* -** Run the parser on the given SQL string. The parser structure is -** passed in. An SQLITE_ status code is returned. If an error occurs -** then an and attempt is made to write an error message into -** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that -** error message. +** Run the parser on the given SQL string. */ -int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){ +int sqlite3RunParser(Parse *pParse, const char *zSql){ int nErr = 0; /* Number of errors encountered */ - int i; /* Loop counter */ void *pEngine; /* The LEMON-generated LALR(1) parser */ + i64 n = 0; /* Length of the next token token */ int tokenType; /* type of the next token */ int lastTokenParsed = -1; /* type of the previous token */ sqlite3 *db = pParse->db; /* The database connection */ int mxSqlLen; /* Max length of an SQL string */ + Parse *pParentParse = 0; /* Outer parse context, if any */ +#ifdef sqlite3Parser_ENGINEALWAYSONSTACK + yyParser sEngine; /* Space to hold the Lemon-generated Parser object */ +#endif + VVA_ONLY( u8 startedWithOom = db->mallocFailed ); assert( zSql!=0 ); mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH]; if( db->nVdbeActive==0 ){ - db->u1.isInterrupted = 0; + AtomicStore(&db->u1.isInterrupted, 0); } pParse->rc = SQLITE_OK; pParse->zTail = zSql; - i = 0; - assert( pzErrMsg!=0 ); - /* sqlite3ParserTrace(stdout, "parser: "); */ - pEngine = sqlite3ParserAlloc(sqlite3Malloc); +#ifdef SQLITE_DEBUG + if( db->flags & SQLITE_ParserTrace ){ + printf("parser: [[[%s]]]\n", zSql); + sqlite3ParserTrace(stdout, "parser: "); + }else{ + sqlite3ParserTrace(0, 0); + } +#endif +#ifdef sqlite3Parser_ENGINEALWAYSONSTACK + pEngine = &sEngine; + sqlite3ParserInit(pEngine, pParse); +#else + pEngine = sqlite3ParserAlloc(sqlite3Malloc, pParse); if( pEngine==0 ){ sqlite3OomFault(db); - return SQLITE_NOMEM; + return SQLITE_NOMEM_BKPT; } +#endif assert( pParse->pNewTable==0 ); assert( pParse->pNewTrigger==0 ); assert( pParse->nVar==0 ); - assert( pParse->nzVar==0 ); - assert( pParse->azVar==0 ); - while( zSql[i]!=0 ){ - assert( i>=0 ); - pParse->sLastToken.z = &zSql[i]; - pParse->sLastToken.n = sqlite3GetToken((unsigned char*)&zSql[i],&tokenType); - i += pParse->sLastToken.n; - if( i>mxSqlLen ){ + assert( pParse->pVList==0 ); + pParentParse = db->pParse; + db->pParse = pParse; + while( 1 ){ + n = sqlite3GetToken((u8*)zSql, &tokenType); + mxSqlLen -= n; + if( mxSqlLen<0 ){ pParse->rc = SQLITE_TOOBIG; + pParse->nErr++; break; } +#ifndef SQLITE_OMIT_WINDOWFUNC + if( tokenType>=TK_WINDOW ){ + assert( tokenType==TK_SPACE || tokenType==TK_OVER || tokenType==TK_FILTER + || tokenType==TK_ILLEGAL || tokenType==TK_WINDOW + || tokenType==TK_QNUMBER || tokenType==TK_COMMENT + ); +#else if( tokenType>=TK_SPACE ){ - assert( tokenType==TK_SPACE || tokenType==TK_ILLEGAL ); - if( db->u1.isInterrupted ){ + assert( tokenType==TK_SPACE || tokenType==TK_ILLEGAL + || tokenType==TK_QNUMBER || tokenType==TK_COMMENT + ); +#endif /* SQLITE_OMIT_WINDOWFUNC */ + if( AtomicLoad(&db->u1.isInterrupted) ){ pParse->rc = SQLITE_INTERRUPT; + pParse->nErr++; break; } - if( tokenType==TK_ILLEGAL ){ - sqlite3ErrorMsg(pParse, "unrecognized token: \"%T\"", - &pParse->sLastToken); + if( tokenType==TK_SPACE ){ + zSql += n; + continue; + } + if( zSql[0]==0 ){ + /* Upon reaching the end of input, call the parser two more times + ** with tokens TK_SEMI and 0, in that order. */ + if( lastTokenParsed==TK_SEMI ){ + tokenType = 0; + }else if( lastTokenParsed==0 ){ + break; + }else{ + tokenType = TK_SEMI; + } + n = 0; +#ifndef SQLITE_OMIT_WINDOWFUNC + }else if( tokenType==TK_WINDOW ){ + assert( n==6 ); + tokenType = analyzeWindowKeyword((const u8*)&zSql[6]); + }else if( tokenType==TK_OVER ){ + assert( n==4 ); + tokenType = analyzeOverKeyword((const u8*)&zSql[4], lastTokenParsed); + }else if( tokenType==TK_FILTER ){ + assert( n==6 ); + tokenType = analyzeFilterKeyword((const u8*)&zSql[6], lastTokenParsed); +#endif /* SQLITE_OMIT_WINDOWFUNC */ + }else if( tokenType==TK_COMMENT + && (db->init.busy || (db->flags & SQLITE_Comments)!=0) + ){ + /* Ignore SQL comments if either (1) we are reparsing the schema or + ** (2) SQLITE_DBCONFIG_ENABLE_COMMENTS is turned on (the default). */ + zSql += n; + continue; + }else if( tokenType!=TK_QNUMBER ){ + Token x; + x.z = zSql; + x.n = (u32)n; + sqlite3ErrorMsg(pParse, "unrecognized token: \"%T\"", &x); break; } - }else{ - if( tokenType==TK_SEMI ) pParse->zTail = &zSql[i]; - sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse); - lastTokenParsed = tokenType; - if( pParse->rc!=SQLITE_OK || db->mallocFailed ) break; } + pParse->sLastToken.z = zSql; + pParse->sLastToken.n = (u32)n; + sqlite3Parser(pEngine, tokenType, pParse->sLastToken); + lastTokenParsed = tokenType; + zSql += n; + assert( db->mallocFailed==0 || pParse->rc!=SQLITE_OK || startedWithOom ); + if( pParse->rc!=SQLITE_OK ) break; } assert( nErr==0 ); - if( pParse->rc==SQLITE_OK && db->mallocFailed==0 ){ - assert( zSql[i]==0 ); - if( lastTokenParsed!=TK_SEMI ){ - sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse); - pParse->zTail = &zSql[i]; - } - if( pParse->rc==SQLITE_OK && db->mallocFailed==0 ){ - sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse); - } - } #ifdef YYTRACKMAXSTACKDEPTH sqlite3_mutex_enter(sqlite3MallocMutex()); sqlite3StatusHighwater(SQLITE_STATUS_PARSER_STACK, @@ -547,57 +724,176 @@ int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){ ); sqlite3_mutex_leave(sqlite3MallocMutex()); #endif /* YYDEBUG */ +#ifdef sqlite3Parser_ENGINEALWAYSONSTACK + sqlite3ParserFinalize(pEngine); +#else sqlite3ParserFree(pEngine, sqlite3_free); +#endif if( db->mallocFailed ){ - pParse->rc = SQLITE_NOMEM; - } - if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){ - pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc)); + pParse->rc = SQLITE_NOMEM_BKPT; } - assert( pzErrMsg!=0 ); - if( pParse->zErrMsg ){ - *pzErrMsg = pParse->zErrMsg; - sqlite3_log(pParse->rc, "%s", *pzErrMsg); - pParse->zErrMsg = 0; + if( pParse->zErrMsg || (pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE) ){ + if( pParse->zErrMsg==0 ){ + pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc)); + } + if( (pParse->prepFlags & SQLITE_PREPARE_DONT_LOG)==0 ){ + sqlite3_log(pParse->rc, "%s in \"%s\"", pParse->zErrMsg, pParse->zTail); + } nErr++; } - if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){ - sqlite3VdbeDelete(pParse->pVdbe); - pParse->pVdbe = 0; - } -#ifndef SQLITE_OMIT_SHARED_CACHE - if( pParse->nested==0 ){ - sqlite3DbFree(db, pParse->aTableLock); - pParse->aTableLock = 0; - pParse->nTableLock = 0; - } -#endif + pParse->zTail = zSql; #ifndef SQLITE_OMIT_VIRTUALTABLE sqlite3_free(pParse->apVtabLock); #endif - if( !IN_DECLARE_VTAB ){ + if( pParse->pNewTable && !IN_SPECIAL_PARSE ){ /* If the pParse->declareVtab flag is set, do not delete any table ** structure built up in pParse->pNewTable. The calling code (see vtab.c) ** will take responsibility for freeing the Table structure. */ sqlite3DeleteTable(db, pParse->pNewTable); } - - sqlite3WithDelete(db, pParse->pWithToFree); - sqlite3DeleteTrigger(db, pParse->pNewTrigger); - for(i=pParse->nzVar-1; i>=0; i--) sqlite3DbFree(db, pParse->azVar[i]); - sqlite3DbFree(db, pParse->azVar); - while( pParse->pAinc ){ - AutoincInfo *p = pParse->pAinc; - pParse->pAinc = p->pNext; - sqlite3DbFree(db, p); - } - while( pParse->pZombieTab ){ - Table *p = pParse->pZombieTab; - pParse->pZombieTab = p->pNextZombie; - sqlite3DeleteTable(db, p); + if( pParse->pNewTrigger && !IN_RENAME_OBJECT ){ + sqlite3DeleteTrigger(db, pParse->pNewTrigger); } + if( pParse->pVList ) sqlite3DbNNFreeNN(db, pParse->pVList); + db->pParse = pParentParse; assert( nErr==0 || pParse->rc!=SQLITE_OK ); return nErr; } + + +#ifdef SQLITE_ENABLE_NORMALIZE +/* +** Insert a single space character into pStr if the current string +** ends with an identifier +*/ +static void addSpaceSeparator(sqlite3_str *pStr){ + if( pStr->nChar && sqlite3IsIdChar(pStr->zText[pStr->nChar-1]) ){ + sqlite3_str_append(pStr, " ", 1); + } +} + +/* +** Compute a normalization of the SQL given by zSql[0..nSql-1]. Return +** the normalization in space obtained from sqlite3DbMalloc(). Or return +** NULL if anything goes wrong or if zSql is NULL. +*/ +char *sqlite3Normalize( + Vdbe *pVdbe, /* VM being reprepared */ + const char *zSql /* The original SQL string */ +){ + sqlite3 *db; /* The database connection */ + int i; /* Next unread byte of zSql[] */ + i64 n; /* length of current token */ + int tokenType; /* type of current token */ + int prevType = 0; /* Previous non-whitespace token */ + int nParen; /* Number of nested levels of parentheses */ + int iStartIN; /* Start of RHS of IN operator in z[] */ + int nParenAtIN; /* Value of nParent at start of RHS of IN operator */ + u32 j; /* Bytes of normalized SQL generated so far */ + sqlite3_str *pStr; /* The normalized SQL string under construction */ + + db = sqlite3VdbeDb(pVdbe); + tokenType = -1; + nParen = iStartIN = nParenAtIN = 0; + pStr = sqlite3_str_new(db); + assert( pStr!=0 ); /* sqlite3_str_new() never returns NULL */ + for(i=0; zSql[i] && pStr->accError==0; i+=n){ + if( tokenType!=TK_SPACE ){ + prevType = tokenType; + } + n = sqlite3GetToken((unsigned char*)zSql+i, &tokenType); + if( NEVER(n<=0) ) break; + switch( tokenType ){ + case TK_COMMENT: + case TK_SPACE: { + break; + } + case TK_NULL: { + if( prevType==TK_IS || prevType==TK_NOT ){ + sqlite3_str_append(pStr, " NULL", 5); + break; + } + /* Fall through */ + } + case TK_STRING: + case TK_INTEGER: + case TK_FLOAT: + case TK_VARIABLE: + case TK_BLOB: { + sqlite3_str_append(pStr, "?", 1); + break; + } + case TK_LP: { + nParen++; + if( prevType==TK_IN ){ + iStartIN = pStr->nChar; + nParenAtIN = nParen; + } + sqlite3_str_append(pStr, "(", 1); + break; + } + case TK_RP: { + if( iStartIN>0 && nParen==nParenAtIN ){ + assert( pStr->nChar>=(u32)iStartIN ); + pStr->nChar = iStartIN+1; + sqlite3_str_append(pStr, "?,?,?", 5); + iStartIN = 0; + } + nParen--; + sqlite3_str_append(pStr, ")", 1); + break; + } + case TK_ID: { + iStartIN = 0; + j = pStr->nChar; + if( sqlite3Isquote(zSql[i]) ){ + char *zId = sqlite3DbStrNDup(db, zSql+i, n); + int nId; + int eType = 0; + if( zId==0 ) break; + sqlite3Dequote(zId); + if( zSql[i]=='"' && sqlite3VdbeUsesDoubleQuotedString(pVdbe, zId) ){ + sqlite3_str_append(pStr, "?", 1); + sqlite3DbFree(db, zId); + break; + } + nId = sqlite3Strlen30(zId); + if( sqlite3GetToken((u8*)zId, &eType)==nId && eType==TK_ID ){ + addSpaceSeparator(pStr); + sqlite3_str_append(pStr, zId, nId); + }else{ + sqlite3_str_appendf(pStr, "\"%w\"", zId); + } + sqlite3DbFree(db, zId); + }else{ + addSpaceSeparator(pStr); + sqlite3_str_append(pStr, zSql+i, n); + } + while( j<pStr->nChar ){ + pStr->zText[j] = sqlite3Tolower(pStr->zText[j]); + j++; + } + break; + } + case TK_SELECT: { + iStartIN = 0; + /* fall through */ + } + default: { + if( sqlite3IsIdChar(zSql[i]) ) addSpaceSeparator(pStr); + j = pStr->nChar; + sqlite3_str_append(pStr, zSql+i, n); + while( j<pStr->nChar ){ + pStr->zText[j] = sqlite3Toupper(pStr->zText[j]); + j++; + } + break; + } + } + } + if( tokenType!=TK_SEMI ) sqlite3_str_append(pStr, ";", 1); + return sqlite3_str_finish(pStr); +} +#endif /* SQLITE_ENABLE_NORMALIZE */ diff --git a/src/treeview.c b/src/treeview.c index 2985804314..153fec88d4 100644 --- a/src/treeview.c +++ b/src/treeview.c @@ -24,48 +24,55 @@ ** Add a new subitem to the tree. The moreToFollow flag indicates that this ** is not the last item in the tree. */ -static TreeView *sqlite3TreeViewPush(TreeView *p, u8 moreToFollow){ +static void sqlite3TreeViewPush(TreeView **pp, u8 moreToFollow){ + TreeView *p = *pp; if( p==0 ){ - p = sqlite3_malloc64( sizeof(*p) ); - if( p==0 ) return 0; + *pp = p = sqlite3_malloc64( sizeof(*p) ); + if( p==0 ) return; memset(p, 0, sizeof(*p)); }else{ p->iLevel++; } assert( moreToFollow==0 || moreToFollow==1 ); - if( p->iLevel<sizeof(p->bLine) ) p->bLine[p->iLevel] = moreToFollow; - return p; + if( p->iLevel<(int)sizeof(p->bLine) ) p->bLine[p->iLevel] = moreToFollow; } /* ** Finished with one layer of the tree */ -static void sqlite3TreeViewPop(TreeView *p){ +static void sqlite3TreeViewPop(TreeView **pp){ + TreeView *p = *pp; if( p==0 ) return; p->iLevel--; - if( p->iLevel<0 ) sqlite3_free(p); + if( p->iLevel<0 ){ + sqlite3_free(p); + *pp = 0; + } } /* ** Generate a single line of output for the tree, with a prefix that contains ** all the appropriate tree lines */ -static void sqlite3TreeViewLine(TreeView *p, const char *zFormat, ...){ +void sqlite3TreeViewLine(TreeView *p, const char *zFormat, ...){ va_list ap; int i; StrAccum acc; - char zBuf[500]; + char zBuf[1000]; sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0); if( p ){ - for(i=0; i<p->iLevel && i<sizeof(p->bLine)-1; i++){ - sqlite3StrAccumAppend(&acc, p->bLine[i] ? "| " : " ", 4); + for(i=0; i<p->iLevel && i<(int)sizeof(p->bLine)-1; i++){ + sqlite3_str_append(&acc, p->bLine[i] ? "| " : " ", 4); } - sqlite3StrAccumAppend(&acc, p->bLine[i] ? "|-- " : "'-- ", 4); + sqlite3_str_append(&acc, p->bLine[i] ? "|-- " : "'-- ", 4); + } + if( zFormat!=0 ){ + va_start(ap, zFormat); + sqlite3_str_vappendf(&acc, zFormat, ap); + va_end(ap); + assert( acc.nChar>0 || acc.accError ); + sqlite3_str_append(&acc, "\n", 1); } - va_start(ap, zFormat); - sqlite3VXPrintf(&acc, zFormat, ap); - va_end(ap); - if( zBuf[acc.nChar-1]!='\n' ) sqlite3StrAccumAppend(&acc, "\n", 1); sqlite3StrAccumFinish(&acc); fprintf(stdout,"%s", zBuf); fflush(stdout); @@ -75,10 +82,57 @@ static void sqlite3TreeViewLine(TreeView *p, const char *zFormat, ...){ ** Shorthand for starting a new tree item that consists of a single label */ static void sqlite3TreeViewItem(TreeView *p, const char *zLabel,u8 moreFollows){ - p = sqlite3TreeViewPush(p, moreFollows); + sqlite3TreeViewPush(&p, moreFollows); sqlite3TreeViewLine(p, "%s", zLabel); } +/* +** Show a list of Column objects in tree format. +*/ +void sqlite3TreeViewColumnList( + TreeView *pView, + const Column *aCol, + int nCol, + u8 moreToFollow +){ + int i; + sqlite3TreeViewPush(&pView, moreToFollow); + sqlite3TreeViewLine(pView, "COLUMNS"); + for(i=0; i<nCol; i++){ + u16 flg = aCol[i].colFlags; + int colMoreToFollow = i<(nCol - 1); + sqlite3TreeViewPush(&pView, colMoreToFollow); + sqlite3TreeViewLine(pView, 0); + printf(" %s", aCol[i].zCnName); + switch( aCol[i].eCType ){ + case COLTYPE_ANY: printf(" ANY"); break; + case COLTYPE_BLOB: printf(" BLOB"); break; + case COLTYPE_INT: printf(" INT"); break; + case COLTYPE_INTEGER: printf(" INTEGER"); break; + case COLTYPE_REAL: printf(" REAL"); break; + case COLTYPE_TEXT: printf(" TEXT"); break; + case COLTYPE_CUSTOM: { + if( flg & COLFLAG_HASTYPE ){ + const char *z = aCol[i].zCnName; + z += strlen(z)+1; + printf(" X-%s", z); + break; + } + } + } + if( flg & COLFLAG_PRIMKEY ) printf(" PRIMARY KEY"); + if( flg & COLFLAG_HIDDEN ) printf(" HIDDEN"); +#ifdef COLFLAG_NOEXPAND + if( flg & COLFLAG_NOEXPAND ) printf(" NO-EXPAND"); +#endif + if( flg ) printf(" flags=%04x", flg); + printf("\n"); + fflush(stdout); + sqlite3TreeViewPop(&pView); + } + sqlite3TreeViewPop(&pView); +} + /* ** Generate a human-readable description of a WITH clause. */ @@ -92,104 +146,191 @@ void sqlite3TreeViewWith(TreeView *pView, const With *pWith, u8 moreToFollow){ sqlite3TreeViewLine(pView, "WITH (0x%p)", pWith); } if( pWith->nCte>0 ){ - pView = sqlite3TreeViewPush(pView, 1); + sqlite3TreeViewPush(&pView, moreToFollow); for(i=0; i<pWith->nCte; i++){ StrAccum x; char zLine[1000]; const struct Cte *pCte = &pWith->a[i]; sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0); - sqlite3XPrintf(&x, "%s", pCte->zName); + sqlite3_str_appendf(&x, "%s", pCte->zName); if( pCte->pCols && pCte->pCols->nExpr>0 ){ char cSep = '('; int j; for(j=0; j<pCte->pCols->nExpr; j++){ - sqlite3XPrintf(&x, "%c%s", cSep, pCte->pCols->a[j].zName); + sqlite3_str_appendf(&x, "%c%s", cSep, pCte->pCols->a[j].zEName); cSep = ','; } - sqlite3XPrintf(&x, ")"); + sqlite3_str_appendf(&x, ")"); + } + if( pCte->eM10d!=M10d_Any ){ + sqlite3_str_appendf(&x, " %sMATERIALIZED", + pCte->eM10d==M10d_No ? "NOT " : ""); + } + if( pCte->pUse ){ + sqlite3_str_appendf(&x, " (pUse=0x%p, nUse=%d)", pCte->pUse, + pCte->pUse->nUse); } - sqlite3XPrintf(&x, " AS"); sqlite3StrAccumFinish(&x); sqlite3TreeViewItem(pView, zLine, i<pWith->nCte-1); sqlite3TreeViewSelect(pView, pCte->pSelect, 0); - sqlite3TreeViewPop(pView); + sqlite3TreeViewPop(&pView); } - sqlite3TreeViewPop(pView); + sqlite3TreeViewPop(&pView); } } +/* +** Generate a human-readable description of a SrcList object. +*/ +void sqlite3TreeViewSrcList(TreeView *pView, const SrcList *pSrc){ + int i; + if( pSrc==0 ) return; + for(i=0; i<pSrc->nSrc; i++){ + const SrcItem *pItem = &pSrc->a[i]; + StrAccum x; + int n = 0; + char zLine[1000]; + sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0); + x.printfFlags |= SQLITE_PRINTF_INTERNAL; + sqlite3_str_appendf(&x, "{%d:*} %!S", pItem->iCursor, pItem); + if( pItem->pSTab ){ + sqlite3_str_appendf(&x, " tab=%Q nCol=%d ptr=%p used=%llx%s", + pItem->pSTab->zName, pItem->pSTab->nCol, pItem->pSTab, + pItem->colUsed, + pItem->fg.rowidUsed ? "+rowid" : ""); + } + if( (pItem->fg.jointype & (JT_LEFT|JT_RIGHT))==(JT_LEFT|JT_RIGHT) ){ + sqlite3_str_appendf(&x, " FULL-OUTER-JOIN"); + }else if( pItem->fg.jointype & JT_LEFT ){ + sqlite3_str_appendf(&x, " LEFT-JOIN"); + }else if( pItem->fg.jointype & JT_RIGHT ){ + sqlite3_str_appendf(&x, " RIGHT-JOIN"); + }else if( pItem->fg.jointype & JT_CROSS ){ + sqlite3_str_appendf(&x, " CROSS-JOIN"); + } + if( pItem->fg.jointype & JT_LTORJ ){ + sqlite3_str_appendf(&x, " LTORJ"); + } + if( pItem->fg.fromDDL ){ + sqlite3_str_appendf(&x, " DDL"); + } + if( pItem->fg.isCte ){ + static const char *aMat[] = {",MAT", "", ",NO-MAT"}; + sqlite3_str_appendf(&x, " CteUse=%d%s", + pItem->u2.pCteUse->nUse, + aMat[pItem->u2.pCteUse->eM10d]); + } + if( pItem->fg.isOn || (pItem->fg.isUsing==0 && pItem->u3.pOn!=0) ){ + sqlite3_str_appendf(&x, " isOn"); + } + if( pItem->fg.isTabFunc ) sqlite3_str_appendf(&x, " isTabFunc"); + if( pItem->fg.isCorrelated ) sqlite3_str_appendf(&x, " isCorrelated"); + if( pItem->fg.isMaterialized ) sqlite3_str_appendf(&x, " isMaterialized"); + if( pItem->fg.viaCoroutine ) sqlite3_str_appendf(&x, " viaCoroutine"); + if( pItem->fg.notCte ) sqlite3_str_appendf(&x, " notCte"); + if( pItem->fg.isNestedFrom ) sqlite3_str_appendf(&x, " isNestedFrom"); + if( pItem->fg.fixedSchema ) sqlite3_str_appendf(&x, " fixedSchema"); + if( pItem->fg.hadSchema ) sqlite3_str_appendf(&x, " hadSchema"); + if( pItem->fg.isSubquery ) sqlite3_str_appendf(&x, " isSubquery"); + + sqlite3StrAccumFinish(&x); + sqlite3TreeViewItem(pView, zLine, i<pSrc->nSrc-1); + n = 0; + if( pItem->fg.isSubquery ) n++; + if( pItem->fg.isTabFunc ) n++; + if( pItem->fg.isUsing || pItem->u3.pOn!=0 ) n++; + if( pItem->fg.isUsing ){ + sqlite3TreeViewIdList(pView, pItem->u3.pUsing, (--n)>0, "USING"); + }else if( pItem->u3.pOn!=0 ){ + sqlite3TreeViewItem(pView, "ON", (--n)>0); + sqlite3TreeViewExpr(pView, pItem->u3.pOn, 0); + sqlite3TreeViewPop(&pView); + } + if( pItem->fg.isSubquery ){ + assert( n==1 ); + if( pItem->pSTab ){ + Table *pTab = pItem->pSTab; + sqlite3TreeViewColumnList(pView, pTab->aCol, pTab->nCol, 1); + } + assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem) ); + sqlite3TreeViewSelect(pView, pItem->u4.pSubq->pSelect, 0); + } + if( pItem->fg.isTabFunc ){ + sqlite3TreeViewExprList(pView, pItem->u1.pFuncArg, 0, "func-args:"); + } + sqlite3TreeViewPop(&pView); + } +} /* -** Generate a human-readable description of a the Select object. +** Generate a human-readable description of a Select object. */ void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){ int n = 0; int cnt = 0; - pView = sqlite3TreeViewPush(pView, moreToFollow); + if( p==0 ){ + sqlite3TreeViewLine(pView, "nil-SELECT"); + return; + } + sqlite3TreeViewPush(&pView, moreToFollow); if( p->pWith ){ sqlite3TreeViewWith(pView, p->pWith, 1); cnt = 1; - sqlite3TreeViewPush(pView, 1); + sqlite3TreeViewPush(&pView, 1); } do{ - sqlite3TreeViewLine(pView, "SELECT%s%s (0x%p) selFlags=0x%x", - ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""), - ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""), p, p->selFlags - ); - if( cnt++ ) sqlite3TreeViewPop(pView); + if( p->selFlags & SF_WhereBegin ){ + sqlite3TreeViewLine(pView, "sqlite3WhereBegin()"); + }else{ + sqlite3TreeViewLine(pView, + "SELECT%s%s (%u/%p) selFlags=0x%x nSelectRow=%d", + ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""), + ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""), + p->selId, p, p->selFlags, + (int)p->nSelectRow + ); + } + if( cnt++ ) sqlite3TreeViewPop(&pView); if( p->pPrior ){ n = 1000; }else{ n = 0; - if( p->pSrc && p->pSrc->nSrc ) n++; + if( p->pSrc && p->pSrc->nSrc && p->pSrc->nAlloc ) n++; if( p->pWhere ) n++; if( p->pGroupBy ) n++; if( p->pHaving ) n++; if( p->pOrderBy ) n++; if( p->pLimit ) n++; - if( p->pOffset ) n++; +#ifndef SQLITE_OMIT_WINDOWFUNC + if( p->pWin ) n++; + if( p->pWinDefn ) n++; +#endif } - sqlite3TreeViewExprList(pView, p->pEList, (n--)>0, "result-set"); - if( p->pSrc && p->pSrc->nSrc ){ - int i; - pView = sqlite3TreeViewPush(pView, (n--)>0); - sqlite3TreeViewLine(pView, "FROM"); - for(i=0; i<p->pSrc->nSrc; i++){ - struct SrcList_item *pItem = &p->pSrc->a[i]; - StrAccum x; - char zLine[100]; - sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0); - sqlite3XPrintf(&x, "{%d,*}", pItem->iCursor); - if( pItem->zDatabase ){ - sqlite3XPrintf(&x, " %s.%s", pItem->zDatabase, pItem->zName); - }else if( pItem->zName ){ - sqlite3XPrintf(&x, " %s", pItem->zName); - } - if( pItem->pTab ){ - sqlite3XPrintf(&x, " tabname=%Q", pItem->pTab->zName); - } - if( pItem->zAlias ){ - sqlite3XPrintf(&x, " (AS %s)", pItem->zAlias); - } - if( pItem->fg.jointype & JT_LEFT ){ - sqlite3XPrintf(&x, " LEFT-JOIN"); - } - sqlite3StrAccumFinish(&x); - sqlite3TreeViewItem(pView, zLine, i<p->pSrc->nSrc-1); - if( pItem->pSelect ){ - sqlite3TreeViewSelect(pView, pItem->pSelect, 0); - } - if( pItem->fg.isTabFunc ){ - sqlite3TreeViewExprList(pView, pItem->u1.pFuncArg, 0, "func-args:"); - } - sqlite3TreeViewPop(pView); + if( p->pEList ){ + sqlite3TreeViewExprList(pView, p->pEList, n>0, "result-set"); + } + n--; +#ifndef SQLITE_OMIT_WINDOWFUNC + if( p->pWin ){ + Window *pX; + sqlite3TreeViewPush(&pView, (n--)>0); + sqlite3TreeViewLine(pView, "window-functions"); + for(pX=p->pWin; pX; pX=pX->pNextWin){ + sqlite3TreeViewWinFunc(pView, pX, pX->pNextWin!=0); } - sqlite3TreeViewPop(pView); + sqlite3TreeViewPop(&pView); + } +#endif + if( p->pSrc && p->pSrc->nSrc && p->pSrc->nAlloc ){ + sqlite3TreeViewPush(&pView, (n--)>0); + sqlite3TreeViewLine(pView, "FROM"); + sqlite3TreeViewSrcList(pView, p->pSrc); + sqlite3TreeViewPop(&pView); } if( p->pWhere ){ sqlite3TreeViewItem(pView, "WHERE", (n--)>0); sqlite3TreeViewExpr(pView, p->pWhere, 0); - sqlite3TreeViewPop(pView); + sqlite3TreeViewPop(&pView); } if( p->pGroupBy ){ sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY"); @@ -197,20 +338,30 @@ void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){ if( p->pHaving ){ sqlite3TreeViewItem(pView, "HAVING", (n--)>0); sqlite3TreeViewExpr(pView, p->pHaving, 0); - sqlite3TreeViewPop(pView); + sqlite3TreeViewPop(&pView); + } +#ifndef SQLITE_OMIT_WINDOWFUNC + if( p->pWinDefn ){ + Window *pX; + sqlite3TreeViewItem(pView, "WINDOW", (n--)>0); + for(pX=p->pWinDefn; pX; pX=pX->pNextWin){ + sqlite3TreeViewWindow(pView, pX, pX->pNextWin!=0); + } + sqlite3TreeViewPop(&pView); } +#endif if( p->pOrderBy ){ sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY"); } if( p->pLimit ){ sqlite3TreeViewItem(pView, "LIMIT", (n--)>0); - sqlite3TreeViewExpr(pView, p->pLimit, 0); - sqlite3TreeViewPop(pView); - } - if( p->pOffset ){ - sqlite3TreeViewItem(pView, "OFFSET", (n--)>0); - sqlite3TreeViewExpr(pView, p->pOffset, 0); - sqlite3TreeViewPop(pView); + sqlite3TreeViewExpr(pView, p->pLimit->pLeft, p->pLimit->pRight!=0); + if( p->pLimit->pRight ){ + sqlite3TreeViewItem(pView, "OFFSET", 0); + sqlite3TreeViewExpr(pView, p->pLimit->pRight, 0); + sqlite3TreeViewPop(&pView); + } + sqlite3TreeViewPop(&pView); } if( p->pPrior ){ const char *zOp = "UNION"; @@ -223,24 +374,161 @@ void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){ } p = p->pPrior; }while( p!=0 ); - sqlite3TreeViewPop(pView); + sqlite3TreeViewPop(&pView); } +#ifndef SQLITE_OMIT_WINDOWFUNC +/* +** Generate a description of starting or stopping bounds +*/ +void sqlite3TreeViewBound( + TreeView *pView, /* View context */ + u8 eBound, /* UNBOUNDED, CURRENT, PRECEDING, FOLLOWING */ + Expr *pExpr, /* Value for PRECEDING or FOLLOWING */ + u8 moreToFollow /* True if more to follow */ +){ + switch( eBound ){ + case TK_UNBOUNDED: { + sqlite3TreeViewItem(pView, "UNBOUNDED", moreToFollow); + sqlite3TreeViewPop(&pView); + break; + } + case TK_CURRENT: { + sqlite3TreeViewItem(pView, "CURRENT", moreToFollow); + sqlite3TreeViewPop(&pView); + break; + } + case TK_PRECEDING: { + sqlite3TreeViewItem(pView, "PRECEDING", moreToFollow); + sqlite3TreeViewExpr(pView, pExpr, 0); + sqlite3TreeViewPop(&pView); + break; + } + case TK_FOLLOWING: { + sqlite3TreeViewItem(pView, "FOLLOWING", moreToFollow); + sqlite3TreeViewExpr(pView, pExpr, 0); + sqlite3TreeViewPop(&pView); + break; + } + } +} +#endif /* SQLITE_OMIT_WINDOWFUNC */ + +#ifndef SQLITE_OMIT_WINDOWFUNC +/* +** Generate a human-readable explanation for a Window object +*/ +void sqlite3TreeViewWindow(TreeView *pView, const Window *pWin, u8 more){ + int nElement = 0; + if( pWin==0 ) return; + if( pWin->pFilter ){ + sqlite3TreeViewItem(pView, "FILTER", 1); + sqlite3TreeViewExpr(pView, pWin->pFilter, 0); + sqlite3TreeViewPop(&pView); + if( pWin->eFrmType==TK_FILTER ) return; + } + sqlite3TreeViewPush(&pView, more); + if( pWin->zName ){ + sqlite3TreeViewLine(pView, "OVER %s (%p)", pWin->zName, pWin); + }else{ + sqlite3TreeViewLine(pView, "OVER (%p)", pWin); + } + if( pWin->zBase ) nElement++; + if( pWin->pOrderBy ) nElement++; + if( pWin->eFrmType!=0 && pWin->eFrmType!=TK_FILTER ) nElement++; + if( pWin->eExclude ) nElement++; + if( pWin->zBase ){ + sqlite3TreeViewPush(&pView, (--nElement)>0); + sqlite3TreeViewLine(pView, "window: %s", pWin->zBase); + sqlite3TreeViewPop(&pView); + } + if( pWin->pPartition ){ + sqlite3TreeViewExprList(pView, pWin->pPartition, nElement>0,"PARTITION-BY"); + } + if( pWin->pOrderBy ){ + sqlite3TreeViewExprList(pView, pWin->pOrderBy, (--nElement)>0, "ORDER-BY"); + } + if( pWin->eFrmType!=0 && pWin->eFrmType!=TK_FILTER ){ + char zBuf[30]; + const char *zFrmType = "ROWS"; + if( pWin->eFrmType==TK_RANGE ) zFrmType = "RANGE"; + if( pWin->eFrmType==TK_GROUPS ) zFrmType = "GROUPS"; + sqlite3_snprintf(sizeof(zBuf),zBuf,"%s%s",zFrmType, + pWin->bImplicitFrame ? " (implied)" : ""); + sqlite3TreeViewItem(pView, zBuf, (--nElement)>0); + sqlite3TreeViewBound(pView, pWin->eStart, pWin->pStart, 1); + sqlite3TreeViewBound(pView, pWin->eEnd, pWin->pEnd, 0); + sqlite3TreeViewPop(&pView); + } + if( pWin->eExclude ){ + char zBuf[30]; + const char *zExclude; + switch( pWin->eExclude ){ + case TK_NO: zExclude = "NO OTHERS"; break; + case TK_CURRENT: zExclude = "CURRENT ROW"; break; + case TK_GROUP: zExclude = "GROUP"; break; + case TK_TIES: zExclude = "TIES"; break; + default: + sqlite3_snprintf(sizeof(zBuf),zBuf,"invalid(%d)", pWin->eExclude); + zExclude = zBuf; + break; + } + sqlite3TreeViewPush(&pView, 0); + sqlite3TreeViewLine(pView, "EXCLUDE %s", zExclude); + sqlite3TreeViewPop(&pView); + } + sqlite3TreeViewPop(&pView); +} +#endif /* SQLITE_OMIT_WINDOWFUNC */ + +#ifndef SQLITE_OMIT_WINDOWFUNC +/* +** Generate a human-readable explanation for a Window Function object +*/ +void sqlite3TreeViewWinFunc(TreeView *pView, const Window *pWin, u8 more){ + if( pWin==0 ) return; + sqlite3TreeViewPush(&pView, more); + sqlite3TreeViewLine(pView, "WINFUNC %s(%d)", + pWin->pWFunc->zName, pWin->pWFunc->nArg); + sqlite3TreeViewWindow(pView, pWin, 0); + sqlite3TreeViewPop(&pView); +} +#endif /* SQLITE_OMIT_WINDOWFUNC */ + /* ** Generate a human-readable explanation of an expression tree. */ void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){ const char *zBinOp = 0; /* Binary operator */ const char *zUniOp = 0; /* Unary operator */ - char zFlgs[30]; - pView = sqlite3TreeViewPush(pView, moreToFollow); + char zFlgs[200]; + sqlite3TreeViewPush(&pView, moreToFollow); if( pExpr==0 ){ sqlite3TreeViewLine(pView, "nil"); - sqlite3TreeViewPop(pView); + sqlite3TreeViewPop(&pView); return; } - if( pExpr->flags ){ - sqlite3_snprintf(sizeof(zFlgs),zFlgs," flags=0x%x",pExpr->flags); + if( pExpr->flags || pExpr->affExpr || pExpr->vvaFlags || pExpr->pAggInfo ){ + StrAccum x; + sqlite3StrAccumInit(&x, 0, zFlgs, sizeof(zFlgs), 0); + sqlite3_str_appendf(&x, " fg.af=%x.%c", + pExpr->flags, pExpr->affExpr ? pExpr->affExpr : 'n'); + if( ExprHasProperty(pExpr, EP_OuterON) ){ + sqlite3_str_appendf(&x, " outer.iJoin=%d", pExpr->w.iJoin); + } + if( ExprHasProperty(pExpr, EP_InnerON) ){ + sqlite3_str_appendf(&x, " inner.iJoin=%d", pExpr->w.iJoin); + } + if( ExprHasProperty(pExpr, EP_FromDDL) ){ + sqlite3_str_appendf(&x, " DDL"); + } + if( ExprHasVVAProperty(pExpr, EP_Immutable) ){ + sqlite3_str_appendf(&x, " IMMUTABLE"); + } + if( pExpr->pAggInfo!=0 ){ + sqlite3_str_appendf(&x, " agg-column[%d]", pExpr->iAgg); + } + sqlite3StrAccumFinish(&x); }else{ zFlgs[0] = 0; } @@ -253,10 +541,22 @@ void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){ case TK_COLUMN: { if( pExpr->iTable<0 ){ /* This only happens when coding check constraints */ - sqlite3TreeViewLine(pView, "COLUMN(%d)%s", pExpr->iColumn, zFlgs); + char zOp2[16]; + if( pExpr->op2 ){ + sqlite3_snprintf(sizeof(zOp2),zOp2," op2=0x%02x",pExpr->op2); + }else{ + zOp2[0] = 0; + } + sqlite3TreeViewLine(pView, "COLUMN(%d)%s%s", + pExpr->iColumn, zFlgs, zOp2); }else{ - sqlite3TreeViewLine(pView, "{%d:%d}%s", - pExpr->iTable, pExpr->iColumn, zFlgs); + assert( ExprUseYTab(pExpr) ); + sqlite3TreeViewLine(pView, "{%d:%d} pTab=%p%s", + pExpr->iTable, pExpr->iColumn, + pExpr->y.pTab, zFlgs); + } + if( ExprHasProperty(pExpr, EP_FixedCol) ){ + sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); } break; } @@ -270,11 +570,13 @@ void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){ } #ifndef SQLITE_OMIT_FLOATING_POINT case TK_FLOAT: { + assert( !ExprHasProperty(pExpr, EP_IntValue) ); sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken); break; } #endif case TK_STRING: { + assert( !ExprHasProperty(pExpr, EP_IntValue) ); sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken); break; } @@ -282,13 +584,20 @@ void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){ sqlite3TreeViewLine(pView,"NULL"); break; } + case TK_TRUEFALSE: { + sqlite3TreeViewLine(pView,"%s%s", + sqlite3ExprTruthValue(pExpr) ? "TRUE" : "FALSE", zFlgs); + break; + } #ifndef SQLITE_OMIT_BLOB_LITERAL case TK_BLOB: { + assert( !ExprHasProperty(pExpr, EP_IntValue) ); sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken); break; } #endif case TK_VARIABLE: { + assert( !ExprHasProperty(pExpr, EP_IntValue) ); sqlite3TreeViewLine(pView,"VARIABLE(%s,%d)", pExpr->u.zToken, pExpr->iColumn); break; @@ -298,12 +607,14 @@ void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){ break; } case TK_ID: { + assert( !ExprHasProperty(pExpr, EP_IntValue) ); sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken); break; } #ifndef SQLITE_OMIT_CAST case TK_CAST: { /* Expressions of the form: CAST(pLeft AS token) */ + assert( !ExprHasProperty(pExpr, EP_IntValue) ); sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken); sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); break; @@ -330,6 +641,7 @@ void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){ case TK_RSHIFT: zBinOp = "RSHIFT"; break; case TK_CONCAT: zBinOp = "CONCAT"; break; case TK_DOT: zBinOp = "DOT"; break; + case TK_LIMIT: zBinOp = "LIMIT"; break; case TK_UMINUS: zUniOp = "UMINUS"; break; case TK_UPLUS: zUniOp = "UPLUS"; break; @@ -338,8 +650,37 @@ void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){ case TK_ISNULL: zUniOp = "ISNULL"; break; case TK_NOTNULL: zUniOp = "NOTNULL"; break; + case TK_TRUTH: { + int x; + const char *azOp[] = { + "IS-FALSE", "IS-TRUE", "IS-NOT-FALSE", "IS-NOT-TRUE" + }; + assert( pExpr->op2==TK_IS || pExpr->op2==TK_ISNOT ); + assert( pExpr->pRight ); + assert( sqlite3ExprSkipCollateAndLikely(pExpr->pRight)->op + == TK_TRUEFALSE ); + x = (pExpr->op2==TK_ISNOT)*2 + sqlite3ExprTruthValue(pExpr->pRight); + zUniOp = azOp[x]; + break; + } + + case TK_SPAN: { + assert( !ExprHasProperty(pExpr, EP_IntValue) ); + sqlite3TreeViewLine(pView, "SPAN %Q", pExpr->u.zToken); + sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); + break; + } + case TK_COLLATE: { - sqlite3TreeViewLine(pView, "COLLATE %Q", pExpr->u.zToken); + /* COLLATE operators without the EP_Collate flag are intended to + ** emulate collation associated with a table column. These show + ** up in the treeview output as "SOFT-COLLATE". Explicit COLLATE + ** operators that appear in the original SQL always have the + ** EP_Collate bit set and appear in treeview output as just "COLLATE" */ + assert( !ExprHasProperty(pExpr, EP_IntValue) ); + sqlite3TreeViewLine(pView, "%sCOLLATE %Q%s", + !ExprHasProperty(pExpr, EP_Collate) ? "SOFT-" : "", + pExpr->u.zToken, zFlgs); sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); break; } @@ -347,37 +688,86 @@ void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){ case TK_AGG_FUNCTION: case TK_FUNCTION: { ExprList *pFarg; /* List of function arguments */ + Window *pWin; if( ExprHasProperty(pExpr, EP_TokenOnly) ){ pFarg = 0; + pWin = 0; }else{ + assert( ExprUseXList(pExpr) ); pFarg = pExpr->x.pList; +#ifndef SQLITE_OMIT_WINDOWFUNC + pWin = IsWindowFunc(pExpr) ? pExpr->y.pWin : 0; +#else + pWin = 0; +#endif } + assert( !ExprHasProperty(pExpr, EP_IntValue) ); if( pExpr->op==TK_AGG_FUNCTION ){ - sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q", - pExpr->op2, pExpr->u.zToken); + sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q%s agg=%d[%d]/%p", + pExpr->op2, pExpr->u.zToken, zFlgs, + pExpr->pAggInfo ? pExpr->pAggInfo->selId : 0, + pExpr->iAgg, pExpr->pAggInfo); + }else if( pExpr->op2!=0 ){ + const char *zOp2; + char zBuf[8]; + sqlite3_snprintf(sizeof(zBuf),zBuf,"0x%02x",pExpr->op2); + zOp2 = zBuf; + if( pExpr->op2==NC_IsCheck ) zOp2 = "NC_IsCheck"; + if( pExpr->op2==NC_IdxExpr ) zOp2 = "NC_IdxExpr"; + if( pExpr->op2==NC_PartIdx ) zOp2 = "NC_PartIdx"; + if( pExpr->op2==NC_GenCol ) zOp2 = "NC_GenCol"; + sqlite3TreeViewLine(pView, "FUNCTION %Q%s op2=%s", + pExpr->u.zToken, zFlgs, zOp2); }else{ - sqlite3TreeViewLine(pView, "FUNCTION %Q", pExpr->u.zToken); + sqlite3TreeViewLine(pView, "FUNCTION %Q%s", pExpr->u.zToken, zFlgs); } if( pFarg ){ - sqlite3TreeViewExprList(pView, pFarg, 0, 0); + sqlite3TreeViewExprList(pView, pFarg, pWin!=0 || pExpr->pLeft, 0); + if( pExpr->pLeft ){ + Expr *pOB = pExpr->pLeft; + assert( pOB->op==TK_ORDER ); + assert( ExprUseXList(pOB) ); + sqlite3TreeViewExprList(pView, pOB->x.pList, pWin!=0, "ORDERBY"); + } + } +#ifndef SQLITE_OMIT_WINDOWFUNC + if( pWin ){ + sqlite3TreeViewWindow(pView, pWin, 0); } +#endif + break; + } + case TK_ORDER: { + sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, "ORDERBY"); break; } #ifndef SQLITE_OMIT_SUBQUERY case TK_EXISTS: { - sqlite3TreeViewLine(pView, "EXISTS-expr"); + assert( ExprUseXSelect(pExpr) ); + sqlite3TreeViewLine(pView, "EXISTS-expr flags=0x%x", pExpr->flags); sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); break; } case TK_SELECT: { - sqlite3TreeViewLine(pView, "SELECT-expr"); + assert( ExprUseXSelect(pExpr) ); + sqlite3TreeViewLine(pView, "subquery-expr flags=0x%x", pExpr->flags); sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); break; } case TK_IN: { - sqlite3TreeViewLine(pView, "IN"); + sqlite3_str *pStr = sqlite3_str_new(0); + char *z; + sqlite3_str_appendf(pStr, "IN flags=0x%x", pExpr->flags); + if( pExpr->iTable ) sqlite3_str_appendf(pStr, " iTable=%d",pExpr->iTable); + if( ExprHasProperty(pExpr, EP_Subrtn) ){ + sqlite3_str_appendf(pStr, " subrtn(%d,%d)", + pExpr->y.sub.regReturn, pExpr->y.sub.iAddr); + } + z = sqlite3_str_finish(pStr); + sqlite3TreeViewLine(pView, z); + sqlite3_free(z); sqlite3TreeViewExpr(pView, pExpr->pLeft, 1); - if( ExprHasProperty(pExpr, EP_xIsSelect) ){ + if( ExprUseXSelect(pExpr) ){ sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); }else{ sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0); @@ -398,10 +788,13 @@ void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){ ** Z is stored in pExpr->pList->a[1].pExpr. */ case TK_BETWEEN: { - Expr *pX = pExpr->pLeft; - Expr *pY = pExpr->x.pList->a[0].pExpr; - Expr *pZ = pExpr->x.pList->a[1].pExpr; - sqlite3TreeViewLine(pView, "BETWEEN"); + const Expr *pX, *pY, *pZ; + pX = pExpr->pLeft; + assert( ExprUseXList(pExpr) ); + assert( pExpr->x.pList->nExpr==2 ); + pY = pExpr->x.pList->a[0].pExpr; + pZ = pExpr->x.pList->a[1].pExpr; + sqlite3TreeViewLine(pView, "BETWEEN%s", zFlgs); sqlite3TreeViewExpr(pView, pX, 1); sqlite3TreeViewExpr(pView, pY, 1); sqlite3TreeViewExpr(pView, pZ, 0); @@ -422,22 +815,68 @@ void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){ case TK_CASE: { sqlite3TreeViewLine(pView, "CASE"); sqlite3TreeViewExpr(pView, pExpr->pLeft, 1); + assert( ExprUseXList(pExpr) ); sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0); break; } #ifndef SQLITE_OMIT_TRIGGER case TK_RAISE: { const char *zType = "unk"; - switch( pExpr->affinity ){ + switch( pExpr->affExpr ){ case OE_Rollback: zType = "rollback"; break; case OE_Abort: zType = "abort"; break; case OE_Fail: zType = "fail"; break; case OE_Ignore: zType = "ignore"; break; } - sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken); + assert( !ExprHasProperty(pExpr, EP_IntValue) ); + sqlite3TreeViewLine(pView, "RAISE %s", zType); + sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); break; } #endif + case TK_MATCH: { + sqlite3TreeViewLine(pView, "MATCH {%d:%d}%s", + pExpr->iTable, pExpr->iColumn, zFlgs); + sqlite3TreeViewExpr(pView, pExpr->pRight, 0); + break; + } + case TK_VECTOR: { + char *z = sqlite3_mprintf("VECTOR%s",zFlgs); + assert( ExprUseXList(pExpr) ); + sqlite3TreeViewBareExprList(pView, pExpr->x.pList, z); + sqlite3_free(z); + break; + } + case TK_SELECT_COLUMN: { + sqlite3TreeViewLine(pView, "SELECT-COLUMN %d of [0..%d]%s", + pExpr->iColumn, pExpr->iTable-1, + pExpr->pRight==pExpr->pLeft ? " (SELECT-owner)" : ""); + assert( ExprUseXSelect(pExpr->pLeft) ); + sqlite3TreeViewSelect(pView, pExpr->pLeft->x.pSelect, 0); + break; + } + case TK_IF_NULL_ROW: { + sqlite3TreeViewLine(pView, "IF-NULL-ROW %d", pExpr->iTable); + sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); + break; + } + case TK_ERROR: { + Expr tmp; + sqlite3TreeViewLine(pView, "ERROR"); + tmp = *pExpr; + tmp.op = pExpr->op2; + sqlite3TreeViewExpr(pView, &tmp, 0); + break; + } + case TK_ROW: { + if( pExpr->iColumn<=0 ){ + sqlite3TreeViewLine(pView, "First FROM table rowid"); + }else{ + sqlite3TreeViewLine(pView, "First FROM table column %d", + pExpr->iColumn-1); + } + break; + } default: { sqlite3TreeViewLine(pView, "op=%d", pExpr->op); break; @@ -449,38 +888,435 @@ void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){ sqlite3TreeViewExpr(pView, pExpr->pRight, 0); }else if( zUniOp ){ sqlite3TreeViewLine(pView, "%s%s", zUniOp, zFlgs); - sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); + sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); } - sqlite3TreeViewPop(pView); + sqlite3TreeViewPop(&pView); } + /* ** Generate a human-readable explanation of an expression list. */ -void sqlite3TreeViewExprList( +void sqlite3TreeViewBareExprList( TreeView *pView, const ExprList *pList, - u8 moreToFollow, const char *zLabel ){ - int i; - pView = sqlite3TreeViewPush(pView, moreToFollow); if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST"; if( pList==0 ){ sqlite3TreeViewLine(pView, "%s (empty)", zLabel); }else{ + int i; sqlite3TreeViewLine(pView, "%s", zLabel); for(i=0; i<pList->nExpr; i++){ int j = pList->a[i].u.x.iOrderByCol; - if( j ){ - sqlite3TreeViewPush(pView, 0); - sqlite3TreeViewLine(pView, "iOrderByCol=%d", j); + u8 sortFlags = pList->a[i].fg.sortFlags; + char *zName = pList->a[i].zEName; + int moreToFollow = i<pList->nExpr - 1; + if( j || zName || sortFlags ){ + sqlite3TreeViewPush(&pView, moreToFollow); + moreToFollow = 0; + sqlite3TreeViewLine(pView, 0); + if( zName ){ + switch( pList->a[i].fg.eEName ){ + default: + fprintf(stdout, "AS %s ", zName); + break; + case ENAME_TAB: + fprintf(stdout, "TABLE-ALIAS-NAME(\"%s\") ", zName); + if( pList->a[i].fg.bUsed ) fprintf(stdout, "(used) "); + if( pList->a[i].fg.bUsingTerm ) fprintf(stdout, "(USING-term) "); + if( pList->a[i].fg.bNoExpand ) fprintf(stdout, "(NoExpand) "); + break; + case ENAME_SPAN: + fprintf(stdout, "SPAN(\"%s\") ", zName); + break; + } + } + if( j ){ + fprintf(stdout, "iOrderByCol=%d ", j); + } + if( sortFlags & KEYINFO_ORDER_DESC ){ + fprintf(stdout, "DESC "); + }else if( sortFlags & KEYINFO_ORDER_BIGNULL ){ + fprintf(stdout, "NULLS-LAST"); + } + fprintf(stdout, "\n"); + fflush(stdout); + } + sqlite3TreeViewExpr(pView, pList->a[i].pExpr, moreToFollow); + if( j || zName || sortFlags ){ + sqlite3TreeViewPop(&pView); } - sqlite3TreeViewExpr(pView, pList->a[i].pExpr, i<pList->nExpr-1); - if( j ) sqlite3TreeViewPop(pView); } } - sqlite3TreeViewPop(pView); } +void sqlite3TreeViewExprList( + TreeView *pView, + const ExprList *pList, + u8 moreToFollow, + const char *zLabel +){ + sqlite3TreeViewPush(&pView, moreToFollow); + sqlite3TreeViewBareExprList(pView, pList, zLabel); + sqlite3TreeViewPop(&pView); +} + +/* +** Generate a human-readable explanation of an id-list. +*/ +void sqlite3TreeViewBareIdList( + TreeView *pView, + const IdList *pList, + const char *zLabel +){ + if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST"; + if( pList==0 ){ + sqlite3TreeViewLine(pView, "%s (empty)", zLabel); + }else{ + int i; + sqlite3TreeViewLine(pView, "%s", zLabel); + for(i=0; i<pList->nId; i++){ + char *zName = pList->a[i].zName; + int moreToFollow = i<pList->nId - 1; + if( zName==0 ) zName = "(null)"; + sqlite3TreeViewPush(&pView, moreToFollow); + sqlite3TreeViewLine(pView, 0); + fprintf(stdout, "%s\n", zName); + sqlite3TreeViewPop(&pView); + } + } +} +void sqlite3TreeViewIdList( + TreeView *pView, + const IdList *pList, + u8 moreToFollow, + const char *zLabel +){ + sqlite3TreeViewPush(&pView, moreToFollow); + sqlite3TreeViewBareIdList(pView, pList, zLabel); + sqlite3TreeViewPop(&pView); +} + +/* +** Generate a human-readable explanation of a list of Upsert objects +*/ +void sqlite3TreeViewUpsert( + TreeView *pView, + const Upsert *pUpsert, + u8 moreToFollow +){ + if( pUpsert==0 ) return; + sqlite3TreeViewPush(&pView, moreToFollow); + while( pUpsert ){ + int n; + sqlite3TreeViewPush(&pView, pUpsert->pNextUpsert!=0 || moreToFollow); + sqlite3TreeViewLine(pView, "ON CONFLICT DO %s", + pUpsert->isDoUpdate ? "UPDATE" : "NOTHING"); + n = (pUpsert->pUpsertSet!=0) + (pUpsert->pUpsertWhere!=0); + sqlite3TreeViewExprList(pView, pUpsert->pUpsertTarget, (n--)>0, "TARGET"); + sqlite3TreeViewExprList(pView, pUpsert->pUpsertSet, (n--)>0, "SET"); + if( pUpsert->pUpsertWhere ){ + sqlite3TreeViewItem(pView, "WHERE", (n--)>0); + sqlite3TreeViewExpr(pView, pUpsert->pUpsertWhere, 0); + sqlite3TreeViewPop(&pView); + } + sqlite3TreeViewPop(&pView); + pUpsert = pUpsert->pNextUpsert; + } + sqlite3TreeViewPop(&pView); +} + +#if TREETRACE_ENABLED +/* +** Generate a human-readable diagram of the data structure that go +** into generating an DELETE statement. +*/ +void sqlite3TreeViewDelete( + const With *pWith, + const SrcList *pTabList, + const Expr *pWhere, + const ExprList *pOrderBy, + const Expr *pLimit, + const Trigger *pTrigger +){ + int n = 0; + TreeView *pView = 0; + sqlite3TreeViewPush(&pView, 0); + sqlite3TreeViewLine(pView, "DELETE"); + if( pWith ) n++; + if( pTabList ) n++; + if( pWhere ) n++; + if( pOrderBy ) n++; + if( pLimit ) n++; + if( pTrigger ) n++; + if( pWith ){ + sqlite3TreeViewPush(&pView, (--n)>0); + sqlite3TreeViewWith(pView, pWith, 0); + sqlite3TreeViewPop(&pView); + } + if( pTabList ){ + sqlite3TreeViewPush(&pView, (--n)>0); + sqlite3TreeViewLine(pView, "FROM"); + sqlite3TreeViewSrcList(pView, pTabList); + sqlite3TreeViewPop(&pView); + } + if( pWhere ){ + sqlite3TreeViewPush(&pView, (--n)>0); + sqlite3TreeViewLine(pView, "WHERE"); + sqlite3TreeViewExpr(pView, pWhere, 0); + sqlite3TreeViewPop(&pView); + } + if( pOrderBy ){ + sqlite3TreeViewExprList(pView, pOrderBy, (--n)>0, "ORDER-BY"); + } + if( pLimit ){ + sqlite3TreeViewPush(&pView, (--n)>0); + sqlite3TreeViewLine(pView, "LIMIT"); + sqlite3TreeViewExpr(pView, pLimit, 0); + sqlite3TreeViewPop(&pView); + } + if( pTrigger ){ + sqlite3TreeViewTrigger(pView, pTrigger, (--n)>0, 1); + } + sqlite3TreeViewPop(&pView); +} +#endif /* TREETRACE_ENABLED */ + +#if TREETRACE_ENABLED +/* +** Generate a human-readable diagram of the data structure that go +** into generating an INSERT statement. +*/ +void sqlite3TreeViewInsert( + const With *pWith, + const SrcList *pTabList, + const IdList *pColumnList, + const Select *pSelect, + const ExprList *pExprList, + int onError, + const Upsert *pUpsert, + const Trigger *pTrigger +){ + TreeView *pView = 0; + int n = 0; + const char *zLabel = "INSERT"; + switch( onError ){ + case OE_Replace: zLabel = "REPLACE"; break; + case OE_Ignore: zLabel = "INSERT OR IGNORE"; break; + case OE_Rollback: zLabel = "INSERT OR ROLLBACK"; break; + case OE_Abort: zLabel = "INSERT OR ABORT"; break; + case OE_Fail: zLabel = "INSERT OR FAIL"; break; + } + sqlite3TreeViewPush(&pView, 0); + sqlite3TreeViewLine(pView, zLabel); + if( pWith ) n++; + if( pTabList ) n++; + if( pColumnList ) n++; + if( pSelect ) n++; + if( pExprList ) n++; + if( pUpsert ) n++; + if( pTrigger ) n++; + if( pWith ){ + sqlite3TreeViewPush(&pView, (--n)>0); + sqlite3TreeViewWith(pView, pWith, 0); + sqlite3TreeViewPop(&pView); + } + if( pTabList ){ + sqlite3TreeViewPush(&pView, (--n)>0); + sqlite3TreeViewLine(pView, "INTO"); + sqlite3TreeViewSrcList(pView, pTabList); + sqlite3TreeViewPop(&pView); + } + if( pColumnList ){ + sqlite3TreeViewIdList(pView, pColumnList, (--n)>0, "COLUMNS"); + } + if( pSelect ){ + sqlite3TreeViewPush(&pView, (--n)>0); + sqlite3TreeViewLine(pView, "DATA-SOURCE"); + sqlite3TreeViewSelect(pView, pSelect, 0); + sqlite3TreeViewPop(&pView); + } + if( pExprList ){ + sqlite3TreeViewExprList(pView, pExprList, (--n)>0, "VALUES"); + } + if( pUpsert ){ + sqlite3TreeViewPush(&pView, (--n)>0); + sqlite3TreeViewLine(pView, "UPSERT"); + sqlite3TreeViewUpsert(pView, pUpsert, 0); + sqlite3TreeViewPop(&pView); + } + if( pTrigger ){ + sqlite3TreeViewTrigger(pView, pTrigger, (--n)>0, 1); + } + sqlite3TreeViewPop(&pView); +} +#endif /* TREETRACE_ENABLED */ + +#if TREETRACE_ENABLED +/* +** Generate a human-readable diagram of the data structure that go +** into generating an UPDATE statement. +*/ +void sqlite3TreeViewUpdate( + const With *pWith, + const SrcList *pTabList, + const ExprList *pChanges, + const Expr *pWhere, + int onError, + const ExprList *pOrderBy, + const Expr *pLimit, + const Upsert *pUpsert, + const Trigger *pTrigger +){ + int n = 0; + TreeView *pView = 0; + const char *zLabel = "UPDATE"; + switch( onError ){ + case OE_Replace: zLabel = "UPDATE OR REPLACE"; break; + case OE_Ignore: zLabel = "UPDATE OR IGNORE"; break; + case OE_Rollback: zLabel = "UPDATE OR ROLLBACK"; break; + case OE_Abort: zLabel = "UPDATE OR ABORT"; break; + case OE_Fail: zLabel = "UPDATE OR FAIL"; break; + } + sqlite3TreeViewPush(&pView, 0); + sqlite3TreeViewLine(pView, zLabel); + if( pWith ) n++; + if( pTabList ) n++; + if( pChanges ) n++; + if( pWhere ) n++; + if( pOrderBy ) n++; + if( pLimit ) n++; + if( pUpsert ) n++; + if( pTrigger ) n++; + if( pWith ){ + sqlite3TreeViewPush(&pView, (--n)>0); + sqlite3TreeViewWith(pView, pWith, 0); + sqlite3TreeViewPop(&pView); + } + if( pTabList ){ + sqlite3TreeViewPush(&pView, (--n)>0); + sqlite3TreeViewLine(pView, "FROM"); + sqlite3TreeViewSrcList(pView, pTabList); + sqlite3TreeViewPop(&pView); + } + if( pChanges ){ + sqlite3TreeViewExprList(pView, pChanges, (--n)>0, "SET"); + } + if( pWhere ){ + sqlite3TreeViewPush(&pView, (--n)>0); + sqlite3TreeViewLine(pView, "WHERE"); + sqlite3TreeViewExpr(pView, pWhere, 0); + sqlite3TreeViewPop(&pView); + } + if( pOrderBy ){ + sqlite3TreeViewExprList(pView, pOrderBy, (--n)>0, "ORDER-BY"); + } + if( pLimit ){ + sqlite3TreeViewPush(&pView, (--n)>0); + sqlite3TreeViewLine(pView, "LIMIT"); + sqlite3TreeViewExpr(pView, pLimit, 0); + sqlite3TreeViewPop(&pView); + } + if( pUpsert ){ + sqlite3TreeViewPush(&pView, (--n)>0); + sqlite3TreeViewLine(pView, "UPSERT"); + sqlite3TreeViewUpsert(pView, pUpsert, 0); + sqlite3TreeViewPop(&pView); + } + if( pTrigger ){ + sqlite3TreeViewTrigger(pView, pTrigger, (--n)>0, 1); + } + sqlite3TreeViewPop(&pView); +} +#endif /* TREETRACE_ENABLED */ + +#ifndef SQLITE_OMIT_TRIGGER +/* +** Show a human-readable graph of a TriggerStep +*/ +void sqlite3TreeViewTriggerStep( + TreeView *pView, + const TriggerStep *pStep, + u8 moreToFollow, + u8 showFullList +){ + int cnt = 0; + if( pStep==0 ) return; + sqlite3TreeViewPush(&pView, + moreToFollow || (showFullList && pStep->pNext!=0)); + do{ + if( cnt++ && pStep->pNext==0 ){ + sqlite3TreeViewPop(&pView); + sqlite3TreeViewPush(&pView, 0); + } + sqlite3TreeViewLine(pView, "%s", pStep->zSpan ? pStep->zSpan : "RETURNING"); + }while( showFullList && (pStep = pStep->pNext)!=0 ); + sqlite3TreeViewPop(&pView); +} + +/* +** Show a human-readable graph of a Trigger +*/ +void sqlite3TreeViewTrigger( + TreeView *pView, + const Trigger *pTrigger, + u8 moreToFollow, + u8 showFullList +){ + int cnt = 0; + if( pTrigger==0 ) return; + sqlite3TreeViewPush(&pView, + moreToFollow || (showFullList && pTrigger->pNext!=0)); + do{ + if( cnt++ && pTrigger->pNext==0 ){ + sqlite3TreeViewPop(&pView); + sqlite3TreeViewPush(&pView, 0); + } + sqlite3TreeViewLine(pView, "TRIGGER %s", pTrigger->zName); + sqlite3TreeViewPush(&pView, 0); + sqlite3TreeViewTriggerStep(pView, pTrigger->step_list, 0, 1); + sqlite3TreeViewPop(&pView); + }while( showFullList && (pTrigger = pTrigger->pNext)!=0 ); + sqlite3TreeViewPop(&pView); +} +#endif /* SQLITE_OMIT_TRIGGER */ + + +/* +** These simplified versions of the tree-view routines omit unnecessary +** parameters. These variants are intended to be used from a symbolic +** debugger, such as "gdb", during interactive debugging sessions. +** +** This routines are given external linkage so that they will always be +** accessible to the debugging, and to avoid warnings about unused +** functions. But these routines only exist in debugging builds, so they +** do not contaminate the interface. +** +** See Also: +** +** sqlite3ShowWhereTerm() in where.c +*/ +void sqlite3ShowExpr(const Expr *p){ sqlite3TreeViewExpr(0,p,0); } +void sqlite3ShowExprList(const ExprList *p){ sqlite3TreeViewExprList(0,p,0,0);} +void sqlite3ShowIdList(const IdList *p){ sqlite3TreeViewIdList(0,p,0,0); } +void sqlite3ShowSrcList(const SrcList *p){ sqlite3TreeViewSrcList(0,p); } +void sqlite3ShowSelect(const Select *p){ sqlite3TreeViewSelect(0,p,0); } +void sqlite3ShowWith(const With *p){ sqlite3TreeViewWith(0,p,0); } +void sqlite3ShowUpsert(const Upsert *p){ sqlite3TreeViewUpsert(0,p,0); } +#ifndef SQLITE_OMIT_TRIGGER +void sqlite3ShowTriggerStep(const TriggerStep *p){ + sqlite3TreeViewTriggerStep(0,p,0,0); +} +void sqlite3ShowTriggerStepList(const TriggerStep *p){ + sqlite3TreeViewTriggerStep(0,p,0,1); +} +void sqlite3ShowTrigger(const Trigger *p){ sqlite3TreeViewTrigger(0,p,0,0); } +void sqlite3ShowTriggerList(const Trigger *p){ sqlite3TreeViewTrigger(0,p,0,1);} +#endif +#ifndef SQLITE_OMIT_WINDOWFUNC +void sqlite3ShowWindow(const Window *p){ sqlite3TreeViewWindow(0,p,0); } +void sqlite3ShowWinFunc(const Window *p){ sqlite3TreeViewWinFunc(0,p,0); } +#endif #endif /* SQLITE_DEBUG */ diff --git a/src/trigger.c b/src/trigger.c index 72c31eb2a3..799fbe57fa 100644 --- a/src/trigger.c +++ b/src/trigger.c @@ -25,6 +25,9 @@ void sqlite3DeleteTriggerStep(sqlite3 *db, TriggerStep *pTriggerStep){ sqlite3ExprListDelete(db, pTmp->pExprList); sqlite3SelectDelete(db, pTmp->pSelect); sqlite3IdListDelete(db, pTmp->pIdList); + sqlite3UpsertDelete(db, pTmp->pUpsert); + sqlite3SrcListDelete(db, pTmp->pFrom); + sqlite3DbFree(db, pTmp->zSpan); sqlite3DbFree(db, pTmp); } @@ -45,28 +48,49 @@ void sqlite3DeleteTriggerStep(sqlite3 *db, TriggerStep *pTriggerStep){ ** pTab as well as the triggers lised in pTab->pTrigger. */ Trigger *sqlite3TriggerList(Parse *pParse, Table *pTab){ - Schema * const pTmpSchema = pParse->db->aDb[1].pSchema; - Trigger *pList = 0; /* List of triggers to return */ - - if( pParse->disableTriggers ){ - return 0; + Schema *pTmpSchema; /* Schema of the pTab table */ + Trigger *pList; /* List of triggers to return */ + HashElem *p; /* Loop variable for TEMP triggers */ + + assert( pParse->disableTriggers==0 ); + pTmpSchema = pParse->db->aDb[1].pSchema; + p = sqliteHashFirst(&pTmpSchema->trigHash); + pList = pTab->pTrigger; + while( p ){ + Trigger *pTrig = (Trigger *)sqliteHashData(p); + if( pTrig->pTabSchema==pTab->pSchema + && pTrig->table + && 0==sqlite3StrICmp(pTrig->table, pTab->zName) + && (pTrig->pTabSchema!=pTmpSchema || pTrig->bReturning) + ){ + pTrig->pNext = pList; + pList = pTrig; + }else if( pTrig->op==TK_RETURNING ){ +#ifndef SQLITE_OMIT_VIRTUALTABLE + assert( pParse->db->pVtabCtx==0 ); +#endif + assert( pParse->bReturning ); + assert( !pParse->isCreate ); + assert( &(pParse->u1.d.pReturning->retTrig) == pTrig ); + pTrig->table = pTab->zName; + pTrig->pTabSchema = pTab->pSchema; + pTrig->pNext = pList; + pList = pTrig; + } + p = sqliteHashNext(p); } - - if( pTmpSchema!=pTab->pSchema ){ - HashElem *p; - assert( sqlite3SchemaMutexHeld(pParse->db, 0, pTmpSchema) ); - for(p=sqliteHashFirst(&pTmpSchema->trigHash); p; p=sqliteHashNext(p)){ - Trigger *pTrig = (Trigger *)sqliteHashData(p); - if( pTrig->pTabSchema==pTab->pSchema - && 0==sqlite3StrICmp(pTrig->table, pTab->zName) - ){ - pTrig->pNext = (pList ? pList : pTab->pTrigger); - pList = pTrig; - } +#if 0 + if( pList ){ + Trigger *pX; + printf("Triggers for %s:", pTab->zName); + for(pX=pList; pX; pX=pX->pNext){ + printf(" %s", pX->zName); } + printf("\n"); + fflush(stdout); } - - return (pList ? pList : pTab->pTrigger); +#endif + return pList; } /* @@ -96,7 +120,6 @@ void sqlite3BeginTrigger( int iDb; /* The database to store the trigger in */ Token *pName; /* The unqualified db name */ DbFixer sFix; /* State vector for the DB fixer */ - int iTabDb; /* Index of the database holding pTab */ assert( pName1!=0 ); /* pName1->z might be NULL, but not pName1 itself */ assert( pName2!=0 ); @@ -127,11 +150,13 @@ void sqlite3BeginTrigger( ** ^^^^^^^^ ** ** To maintain backwards compatibility, ignore the database - ** name on pTableName if we are reparsing out of SQLITE_MASTER. + ** name on pTableName if we are reparsing out of the schema table */ if( db->init.busy && iDb!=1 ){ - sqlite3DbFree(db, pTableName->a[0].zDatabase); - pTableName->a[0].zDatabase = 0; + assert( pTableName->a[0].fg.fixedSchema==0 ); + assert( pTableName->a[0].fg.isSubquery==0 ); + sqlite3DbFree(db, pTableName->a[0].u4.zDatabase); + pTableName->a[0].u4.zDatabase = 0; } /* If the trigger name was unqualified, and the table is a temp table, @@ -155,39 +180,39 @@ void sqlite3BeginTrigger( pTab = sqlite3SrcListLookup(pParse, pTableName); if( !pTab ){ /* The table does not exist. */ - if( db->init.iDb==1 ){ - /* Ticket #3810. - ** Normally, whenever a table is dropped, all associated triggers are - ** dropped too. But if a TEMP trigger is created on a non-TEMP table - ** and the table is dropped by a different database connection, the - ** trigger is not visible to the database connection that does the - ** drop so the trigger cannot be dropped. This results in an - ** "orphaned trigger" - a trigger whose associated table is missing. - */ - db->init.orphanTrigger = 1; - } - goto trigger_cleanup; + goto trigger_orphan_error; } if( IsVirtual(pTab) ){ sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables"); - goto trigger_cleanup; + goto trigger_orphan_error; + } + if( (pTab->tabFlags & TF_Shadow)!=0 && sqlite3ReadOnlyShadowTables(db) ){ + sqlite3ErrorMsg(pParse, "cannot create triggers on shadow tables"); + goto trigger_orphan_error; } /* Check that the trigger name is not reserved and that no trigger of the ** specified name exists */ zName = sqlite3NameFromToken(db, pName); - if( !zName || SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ + if( zName==0 ){ + assert( db->mallocFailed ); + goto trigger_cleanup; + } + if( sqlite3CheckObjectName(pParse, zName, "trigger", pTab->zName) ){ goto trigger_cleanup; } assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); - if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),zName) ){ - if( !noErr ){ - sqlite3ErrorMsg(pParse, "trigger %T already exists", pName); - }else{ - assert( !db->init.busy ); - sqlite3CodeVerifySchema(pParse, iDb); + if( !IN_RENAME_OBJECT ){ + if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),zName) ){ + if( !noErr ){ + sqlite3ErrorMsg(pParse, "trigger %T already exists", pName); + }else{ + assert( !db->init.busy ); + sqlite3CodeVerifySchema(pParse, iDb); + VVA_ONLY( pParse->ifNotExists = 1; ) + } + goto trigger_cleanup; } - goto trigger_cleanup; } /* Do not create a trigger on a system table */ @@ -199,23 +224,23 @@ void sqlite3BeginTrigger( /* INSTEAD of triggers are only for views and views only support INSTEAD ** of triggers. */ - if( pTab->pSelect && tr_tm!=TK_INSTEAD ){ + if( IsView(pTab) && tr_tm!=TK_INSTEAD ){ sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S", - (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName, 0); - goto trigger_cleanup; + (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName->a); + goto trigger_orphan_error; } - if( !pTab->pSelect && tr_tm==TK_INSTEAD ){ + if( !IsView(pTab) && tr_tm==TK_INSTEAD ){ sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF" - " trigger on table: %S", pTableName, 0); - goto trigger_cleanup; + " trigger on table: %S", pTableName->a); + goto trigger_orphan_error; } - iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema); #ifndef SQLITE_OMIT_AUTHORIZATION - { + if( !IN_RENAME_OBJECT ){ + int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema); int code = SQLITE_CREATE_TRIGGER; - const char *zDb = db->aDb[iTabDb].zName; - const char *zDbTrig = isTemp ? db->aDb[1].zName : zDb; + const char *zDb = db->aDb[iTabDb].zDbSName; + const char *zDbTrig = isTemp ? db->aDb[1].zDbSName : zDb; if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER; if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){ goto trigger_cleanup; @@ -245,8 +270,15 @@ void sqlite3BeginTrigger( pTrigger->pTabSchema = pTab->pSchema; pTrigger->op = (u8)op; pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER; - pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE); - pTrigger->pColumns = sqlite3IdListDup(db, pColumns); + if( IN_RENAME_OBJECT ){ + sqlite3RenameTokenRemap(pParse, pTrigger->table, pTableName->a[0].zName); + pTrigger->pWhen = pWhen; + pWhen = 0; + }else{ + pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE); + } + pTrigger->pColumns = pColumns; + pColumns = 0; assert( pParse->pNewTrigger==0 ); pParse->pNewTrigger = pTrigger; @@ -260,6 +292,23 @@ void sqlite3BeginTrigger( }else{ assert( pParse->pNewTrigger==pTrigger ); } + return; + +trigger_orphan_error: + if( db->init.iDb==1 ){ + /* Ticket #3810. + ** Normally, whenever a table is dropped, all associated triggers are + ** dropped too. But if a TEMP trigger is created on a non-TEMP table + ** and the table is dropped by a different database connection, the + ** trigger is not visible to the database connection that does the + ** drop so the trigger cannot be dropped. This results in an + ** "orphaned trigger" - a trigger whose associated table is missing. + ** + ** 2020-11-05 see also https://sqlite.org/forum/forumpost/157dc791df + */ + db->init.orphanTrigger = 1; + } + goto trigger_cleanup; } /* @@ -295,32 +344,60 @@ void sqlite3FinishTrigger( goto triggerfinish_cleanup; } +#ifndef SQLITE_OMIT_ALTERTABLE + if( IN_RENAME_OBJECT ){ + assert( !db->init.busy ); + pParse->pNewTrigger = pTrig; + pTrig = 0; + }else +#endif + /* if we are not initializing, - ** build the sqlite_master entry + ** build the sqlite_schema entry */ if( !db->init.busy ){ Vdbe *v; char *z; - /* Make an entry in the sqlite_master table */ + /* If this is a new CREATE TABLE statement, and if shadow tables + ** are read-only, and the trigger makes a change to a shadow table, + ** then raise an error - do not allow the trigger to be created. */ + if( sqlite3ReadOnlyShadowTables(db) ){ + TriggerStep *pStep; + for(pStep=pTrig->step_list; pStep; pStep=pStep->pNext){ + if( pStep->zTarget!=0 + && sqlite3ShadowTableName(db, pStep->zTarget) + ){ + sqlite3ErrorMsg(pParse, + "trigger \"%s\" may not write to shadow table \"%s\"", + pTrig->zName, pStep->zTarget); + goto triggerfinish_cleanup; + } + } + } + + /* Make an entry in the sqlite_schema table */ v = sqlite3GetVdbe(pParse); if( v==0 ) goto triggerfinish_cleanup; sqlite3BeginWriteOperation(pParse, 0, iDb); z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n); + testcase( z==0 ); sqlite3NestedParse(pParse, - "INSERT INTO %Q.%s VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')", - db->aDb[iDb].zName, SCHEMA_TABLE(iDb), zName, + "INSERT INTO %Q." LEGACY_SCHEMA_TABLE + " VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')", + db->aDb[iDb].zDbSName, zName, pTrig->table, z); sqlite3DbFree(db, z); sqlite3ChangeCookie(pParse, iDb); sqlite3VdbeAddParseSchemaOp(v, iDb, - sqlite3MPrintf(db, "type='trigger' AND name='%q'", zName)); + sqlite3MPrintf(db, "type='trigger' AND name='%q'", zName), 0); } if( db->init.busy ){ Trigger *pLink = pTrig; Hash *pHash = &db->aDb[iDb].pSchema->trigHash; assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); + assert( pLink!=0 ); pTrig = sqlite3HashInsert(pHash, zName, pTrig); if( pTrig ){ sqlite3OomFault(db); @@ -335,10 +412,21 @@ void sqlite3FinishTrigger( triggerfinish_cleanup: sqlite3DeleteTrigger(db, pTrig); - assert( !pParse->pNewTrigger ); + assert( IN_RENAME_OBJECT || !pParse->pNewTrigger ); sqlite3DeleteTriggerStep(db, pStepList); } +/* +** Duplicate a range of text from an SQL statement, then convert all +** whitespace characters into ordinary space characters. +*/ +static char *triggerSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){ + char *z = sqlite3DbSpanDup(db, zStart, zEnd); + int i; + if( z ) for(i=0; z[i]; i++) if( sqlite3Isspace(z[i]) ) z[i] = ' '; + return z; +} + /* ** Turn a SELECT statement (that the pSelect parameter points to) into ** a trigger step. Return a pointer to a TriggerStep structure. @@ -346,7 +434,12 @@ void sqlite3FinishTrigger( ** The parser calls this routine when it finds a SELECT statement in ** body of a TRIGGER. */ -TriggerStep *sqlite3TriggerSelectStep(sqlite3 *db, Select *pSelect){ +TriggerStep *sqlite3TriggerSelectStep( + sqlite3 *db, /* Database connection */ + Select *pSelect, /* The SELECT statement */ + const char *zStart, /* Start of SQL text */ + const char *zEnd /* End of SQL text */ +){ TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep)); if( pTriggerStep==0 ) { sqlite3SelectDelete(db, pSelect); @@ -355,6 +448,7 @@ TriggerStep *sqlite3TriggerSelectStep(sqlite3 *db, Select *pSelect){ pTriggerStep->op = TK_SELECT; pTriggerStep->pSelect = pSelect; pTriggerStep->orconf = OE_Default; + pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd); return pTriggerStep; } @@ -365,12 +459,16 @@ TriggerStep *sqlite3TriggerSelectStep(sqlite3 *db, Select *pSelect){ ** If an OOM error occurs, NULL is returned and db->mallocFailed is set. */ static TriggerStep *triggerStepAllocate( - sqlite3 *db, /* Database connection */ + Parse *pParse, /* Parser context */ u8 op, /* Trigger opcode */ - Token *pName /* The target name */ + Token *pName, /* The target name */ + const char *zStart, /* Start of SQL text */ + const char *zEnd /* End of SQL text */ ){ + sqlite3 *db = pParse->db; TriggerStep *pTriggerStep; + if( pParse->nErr ) return 0; pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n + 1); if( pTriggerStep ){ char *z = (char*)&pTriggerStep[1]; @@ -378,6 +476,10 @@ static TriggerStep *triggerStepAllocate( sqlite3Dequote(z); pTriggerStep->zTarget = z; pTriggerStep->op = op; + pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd); + if( IN_RENAME_OBJECT ){ + sqlite3RenameTokenMap(pParse, pTriggerStep->zTarget, pName); + } } return pTriggerStep; } @@ -390,23 +492,39 @@ static TriggerStep *triggerStepAllocate( ** body of a trigger. */ TriggerStep *sqlite3TriggerInsertStep( - sqlite3 *db, /* The database connection */ + Parse *pParse, /* Parser */ Token *pTableName, /* Name of the table into which we insert */ IdList *pColumn, /* List of columns in pTableName to insert into */ Select *pSelect, /* A SELECT statement that supplies values */ - u8 orconf /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */ + u8 orconf, /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */ + Upsert *pUpsert, /* ON CONFLICT clauses for upsert */ + const char *zStart, /* Start of SQL text */ + const char *zEnd /* End of SQL text */ ){ + sqlite3 *db = pParse->db; TriggerStep *pTriggerStep; assert(pSelect != 0 || db->mallocFailed); - pTriggerStep = triggerStepAllocate(db, TK_INSERT, pTableName); + pTriggerStep = triggerStepAllocate(pParse, TK_INSERT, pTableName,zStart,zEnd); if( pTriggerStep ){ - pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE); + if( IN_RENAME_OBJECT ){ + pTriggerStep->pSelect = pSelect; + pSelect = 0; + }else{ + pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE); + } pTriggerStep->pIdList = pColumn; + pTriggerStep->pUpsert = pUpsert; pTriggerStep->orconf = orconf; + if( pUpsert ){ + sqlite3HasExplicitNulls(pParse, pUpsert->pUpsertTarget); + } }else{ + testcase( pColumn ); sqlite3IdListDelete(db, pColumn); + testcase( pUpsert ); + sqlite3UpsertDelete(db, pUpsert); } sqlite3SelectDelete(db, pSelect); @@ -419,22 +537,37 @@ TriggerStep *sqlite3TriggerInsertStep( ** sees an UPDATE statement inside the body of a CREATE TRIGGER. */ TriggerStep *sqlite3TriggerUpdateStep( - sqlite3 *db, /* The database connection */ + Parse *pParse, /* Parser */ Token *pTableName, /* Name of the table to be updated */ + SrcList *pFrom, /* FROM clause for an UPDATE-FROM, or NULL */ ExprList *pEList, /* The SET clause: list of column and new values */ Expr *pWhere, /* The WHERE clause */ - u8 orconf /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */ + u8 orconf, /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */ + const char *zStart, /* Start of SQL text */ + const char *zEnd /* End of SQL text */ ){ + sqlite3 *db = pParse->db; TriggerStep *pTriggerStep; - pTriggerStep = triggerStepAllocate(db, TK_UPDATE, pTableName); + pTriggerStep = triggerStepAllocate(pParse, TK_UPDATE, pTableName,zStart,zEnd); if( pTriggerStep ){ - pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE); - pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE); + if( IN_RENAME_OBJECT ){ + pTriggerStep->pExprList = pEList; + pTriggerStep->pWhere = pWhere; + pTriggerStep->pFrom = pFrom; + pEList = 0; + pWhere = 0; + pFrom = 0; + }else{ + pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE); + pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE); + pTriggerStep->pFrom = sqlite3SrcListDup(db, pFrom, EXPRDUP_REDUCE); + } pTriggerStep->orconf = orconf; } sqlite3ExprListDelete(db, pEList); sqlite3ExprDelete(db, pWhere); + sqlite3SrcListDelete(db, pFrom); return pTriggerStep; } @@ -444,15 +577,23 @@ TriggerStep *sqlite3TriggerUpdateStep( ** sees a DELETE statement inside the body of a CREATE TRIGGER. */ TriggerStep *sqlite3TriggerDeleteStep( - sqlite3 *db, /* Database connection */ + Parse *pParse, /* Parser */ Token *pTableName, /* The table from which rows are deleted */ - Expr *pWhere /* The WHERE clause */ + Expr *pWhere, /* The WHERE clause */ + const char *zStart, /* Start of SQL text */ + const char *zEnd /* End of SQL text */ ){ + sqlite3 *db = pParse->db; TriggerStep *pTriggerStep; - pTriggerStep = triggerStepAllocate(db, TK_DELETE, pTableName); + pTriggerStep = triggerStepAllocate(pParse, TK_DELETE, pTableName,zStart,zEnd); if( pTriggerStep ){ - pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE); + if( IN_RENAME_OBJECT ){ + pTriggerStep->pWhere = pWhere; + pWhere = 0; + }else{ + pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE); + } pTriggerStep->orconf = OE_Default; } sqlite3ExprDelete(db, pWhere); @@ -463,7 +604,7 @@ TriggerStep *sqlite3TriggerDeleteStep( ** Recursively delete a Trigger structure */ void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){ - if( pTrigger==0 ) return; + if( pTrigger==0 || pTrigger->bReturning ) return; sqlite3DeleteTriggerStep(db, pTrigger->step_list); sqlite3DbFree(db, pTrigger->zName); sqlite3DbFree(db, pTrigger->table); @@ -493,19 +634,20 @@ void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){ } assert( pName->nSrc==1 ); - zDb = pName->a[0].zDatabase; + assert( pName->a[0].fg.fixedSchema==0 && pName->a[0].fg.isSubquery==0 ); + zDb = pName->a[0].u4.zDatabase; zName = pName->a[0].zName; assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) ); for(i=OMIT_TEMPDB; i<db->nDb; i++){ int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */ - if( zDb && sqlite3StrICmp(db->aDb[j].zName, zDb) ) continue; + if( zDb && sqlite3DbIsNamed(db, j, zDb)==0 ) continue; assert( sqlite3SchemaMutexHeld(db, j, 0) ); pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName); if( pTrigger ) break; } if( !pTrigger ){ if( !noErr ){ - sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0); + sqlite3ErrorMsg(pParse, "no such trigger: %S", pName->a); }else{ sqlite3CodeVerifyNamedSchema(pParse, zDb); } @@ -539,12 +681,11 @@ void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){ iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema); assert( iDb>=0 && iDb<db->nDb ); pTable = tableOfTrigger(pTrigger); - assert( pTable ); - assert( pTable->pSchema==pTrigger->pSchema || iDb==1 ); + assert( (pTable && pTable->pSchema==pTrigger->pSchema) || iDb==1 ); #ifndef SQLITE_OMIT_AUTHORIZATION - { + if( pTable ){ int code = SQLITE_DROP_TRIGGER; - const char *zDb = db->aDb[iDb].zName; + const char *zDb = db->aDb[iDb].zDbSName; const char *zTab = SCHEMA_TABLE(iDb); if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER; if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) || @@ -556,11 +697,10 @@ void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){ /* Generate code to destroy the database record of the trigger. */ - assert( pTable!=0 ); if( (v = sqlite3GetVdbe(pParse))!=0 ){ sqlite3NestedParse(pParse, - "DELETE FROM %Q.%s WHERE name=%Q AND type='trigger'", - db->aDb[iDb].zName, SCHEMA_TABLE(iDb), pTrigger->zName + "DELETE FROM %Q." LEGACY_SCHEMA_TABLE " WHERE name=%Q AND type='trigger'", + db->aDb[iDb].zDbSName, pTrigger->zName ); sqlite3ChangeCookie(pParse, iDb); sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0); @@ -580,12 +720,18 @@ void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){ if( ALWAYS(pTrigger) ){ if( pTrigger->pSchema==pTrigger->pTabSchema ){ Table *pTab = tableOfTrigger(pTrigger); - Trigger **pp; - for(pp=&pTab->pTrigger; *pp!=pTrigger; pp=&((*pp)->pNext)); - *pp = (*pp)->pNext; + if( pTab ){ + Trigger **pp; + for(pp=&pTab->pTrigger; *pp; pp=&((*pp)->pNext)){ + if( *pp==pTrigger ){ + *pp = (*pp)->pNext; + break; + } + } + } } sqlite3DeleteTrigger(db, pTrigger); - db->flags |= SQLITE_InternChanges; + db->mDbFlags |= DBFLAG_SchemaChange; } } @@ -602,18 +748,27 @@ static int checkColumnOverlap(IdList *pIdList, ExprList *pEList){ int e; if( pIdList==0 || NEVER(pEList==0) ) return 1; for(e=0; e<pEList->nExpr; e++){ - if( sqlite3IdListIndex(pIdList, pEList->a[e].zName)>=0 ) return 1; + if( sqlite3IdListIndex(pIdList, pEList->a[e].zEName)>=0 ) return 1; } return 0; } +/* +** Return true if any TEMP triggers exist +*/ +static int tempTriggersExist(sqlite3 *db){ + if( NEVER(db->aDb[1].pSchema==0) ) return 0; + if( sqliteHashFirst(&db->aDb[1].pSchema->trigHash)==0 ) return 0; + return 1; +} + /* ** Return a list of all triggers on table pTab if there exists at least ** one trigger that must be fired when an operation of type 'op' is ** performed on the table, and, if that operation is an UPDATE, if at ** least one of the columns in pChanges is being modified. */ -Trigger *sqlite3TriggersExist( +static SQLITE_NOINLINE Trigger *triggersReallyExist( Parse *pParse, /* Parse context */ Table *pTab, /* The table the contains the triggers */ int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */ @@ -624,20 +779,74 @@ Trigger *sqlite3TriggersExist( Trigger *pList = 0; Trigger *p; - if( (pParse->db->flags & SQLITE_EnableTrigger)!=0 ){ - pList = sqlite3TriggerList(pParse, pTab); - } - assert( pList==0 || IsVirtual(pTab)==0 ); - for(p=pList; p; p=p->pNext){ - if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){ - mask |= p->tr_tm; + pList = sqlite3TriggerList(pParse, pTab); + assert( pList==0 || IsVirtual(pTab)==0 + || (pList->bReturning && pList->pNext==0) ); + if( pList!=0 ){ + p = pList; + if( (pParse->db->flags & SQLITE_EnableTrigger)==0 + && pTab->pTrigger!=0 + ){ + /* The SQLITE_DBCONFIG_ENABLE_TRIGGER setting is off. That means that + ** only TEMP triggers are allowed. Truncate the pList so that it + ** includes only TEMP triggers */ + if( pList==pTab->pTrigger ){ + pList = 0; + goto exit_triggers_exist; + } + while( ALWAYS(p->pNext) && p->pNext!=pTab->pTrigger ) p = p->pNext; + p->pNext = 0; + p = pList; } + do{ + if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){ + mask |= p->tr_tm; + }else if( p->op==TK_RETURNING ){ + /* The first time a RETURNING trigger is seen, the "op" value tells + ** us what time of trigger it should be. */ + assert( sqlite3IsToplevel(pParse) ); + p->op = op; + if( IsVirtual(pTab) ){ + if( op!=TK_INSERT ){ + sqlite3ErrorMsg(pParse, + "%s RETURNING is not available on virtual tables", + op==TK_DELETE ? "DELETE" : "UPDATE"); + } + p->tr_tm = TRIGGER_BEFORE; + }else{ + p->tr_tm = TRIGGER_AFTER; + } + mask |= p->tr_tm; + }else if( p->bReturning && p->op==TK_INSERT && op==TK_UPDATE + && sqlite3IsToplevel(pParse) ){ + /* Also fire a RETURNING trigger for an UPSERT */ + mask |= p->tr_tm; + } + p = p->pNext; + }while( p ); } +exit_triggers_exist: if( pMask ){ *pMask = mask; } return (mask ? pList : 0); } +Trigger *sqlite3TriggersExist( + Parse *pParse, /* Parse context */ + Table *pTab, /* The table the contains the triggers */ + int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */ + ExprList *pChanges, /* Columns that change in an UPDATE statement */ + int *pMask /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */ +){ + assert( pTab!=0 ); + if( (pTab->pTrigger==0 && !tempTriggersExist(pParse->db)) + || pParse->disableTriggers + ){ + if( pMask ) *pMask = 0; + return 0; + } + return triggersReallyExist(pParse,pTab,op,pChanges,pMask); +} /* ** Convert the pStep->zTarget string into a SrcList and return a pointer @@ -649,27 +858,263 @@ Trigger *sqlite3TriggersExist( ** trigger is in TEMP in which case it can refer to any other database it ** wants. */ -static SrcList *targetSrcList( +SrcList *sqlite3TriggerStepSrc( Parse *pParse, /* The parsing context */ TriggerStep *pStep /* The trigger containing the target token */ ){ sqlite3 *db = pParse->db; - int iDb; /* Index of the database to use */ - SrcList *pSrc; /* SrcList to be returned */ - - pSrc = sqlite3SrcListAppend(db, 0, 0, 0); + SrcList *pSrc; /* SrcList to be returned */ + char *zName = sqlite3DbStrDup(db, pStep->zTarget); + pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0); + assert( pSrc==0 || pSrc->nSrc==1 ); + assert( zName || pSrc==0 ); if( pSrc ){ - assert( pSrc->nSrc>0 ); - pSrc->a[pSrc->nSrc-1].zName = sqlite3DbStrDup(db, pStep->zTarget); - iDb = sqlite3SchemaToIndex(db, pStep->pTrig->pSchema); - if( iDb==0 || iDb>=2 ){ - assert( iDb<db->nDb ); - pSrc->a[pSrc->nSrc-1].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zName); + Schema *pSchema = pStep->pTrig->pSchema; + pSrc->a[0].zName = zName; + if( pSchema!=db->aDb[1].pSchema ){ + assert( pSrc->a[0].fg.fixedSchema || pSrc->a[0].u4.zDatabase==0 ); + pSrc->a[0].u4.pSchema = pSchema; + pSrc->a[0].fg.fixedSchema = 1; + } + if( pStep->pFrom ){ + SrcList *pDup = sqlite3SrcListDup(db, pStep->pFrom, 0); + if( pDup && pDup->nSrc>1 && !IN_RENAME_OBJECT ){ + Select *pSubquery; + Token as; + pSubquery = sqlite3SelectNew(pParse,0,pDup,0,0,0,0,SF_NestedFrom,0); + as.n = 0; + as.z = 0; + pDup = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&as,pSubquery,0); + } + pSrc = sqlite3SrcListAppendList(pParse, pSrc, pDup); } + }else{ + sqlite3DbFree(db, zName); } return pSrc; } +/* +** Return true if the pExpr term from the RETURNING clause argument +** list is of the form "*". Raise an error if the terms if of the +** form "table.*". +*/ +static int isAsteriskTerm( + Parse *pParse, /* Parsing context */ + Expr *pTerm /* A term in the RETURNING clause */ +){ + assert( pTerm!=0 ); + if( pTerm->op==TK_ASTERISK ) return 1; + if( pTerm->op!=TK_DOT ) return 0; + assert( pTerm->pRight!=0 ); + assert( pTerm->pLeft!=0 ); + if( pTerm->pRight->op!=TK_ASTERISK ) return 0; + sqlite3ErrorMsg(pParse, "RETURNING may not use \"TABLE.*\" wildcards"); + return 1; +} + +/* The input list pList is the list of result set terms from a RETURNING +** clause. The table that we are returning from is pTab. +** +** This routine makes a copy of the pList, and at the same time expands +** any "*" wildcards to be the complete set of columns from pTab. +*/ +static ExprList *sqlite3ExpandReturning( + Parse *pParse, /* Parsing context */ + ExprList *pList, /* The arguments to RETURNING */ + Table *pTab /* The table being updated */ +){ + ExprList *pNew = 0; + sqlite3 *db = pParse->db; + int i; + + for(i=0; i<pList->nExpr; i++){ + Expr *pOldExpr = pList->a[i].pExpr; + if( NEVER(pOldExpr==0) ) continue; + if( isAsteriskTerm(pParse, pOldExpr) ){ + int jj; + for(jj=0; jj<pTab->nCol; jj++){ + Expr *pNewExpr; + if( IsHiddenColumn(pTab->aCol+jj) ) continue; + pNewExpr = sqlite3Expr(db, TK_ID, pTab->aCol[jj].zCnName); + pNew = sqlite3ExprListAppend(pParse, pNew, pNewExpr); + if( !db->mallocFailed ){ + struct ExprList_item *pItem = &pNew->a[pNew->nExpr-1]; + pItem->zEName = sqlite3DbStrDup(db, pTab->aCol[jj].zCnName); + pItem->fg.eEName = ENAME_NAME; + } + } + }else{ + Expr *pNewExpr = sqlite3ExprDup(db, pOldExpr, 0); + pNew = sqlite3ExprListAppend(pParse, pNew, pNewExpr); + if( !db->mallocFailed && ALWAYS(pList->a[i].zEName!=0) ){ + struct ExprList_item *pItem = &pNew->a[pNew->nExpr-1]; + pItem->zEName = sqlite3DbStrDup(db, pList->a[i].zEName); + pItem->fg.eEName = pList->a[i].fg.eEName; + } + } + } + return pNew; +} + +/* If the Expr node is a subquery or an EXISTS operator or an IN operator that +** uses a subquery, and if the subquery is SF_Correlated, then mark the +** expression as EP_VarSelect. +*/ +static int sqlite3ReturningSubqueryVarSelect(Walker *NotUsed, Expr *pExpr){ + UNUSED_PARAMETER(NotUsed); + if( ExprUseXSelect(pExpr) + && (pExpr->x.pSelect->selFlags & SF_Correlated)!=0 + ){ + testcase( ExprHasProperty(pExpr, EP_VarSelect) ); + ExprSetProperty(pExpr, EP_VarSelect); + } + return WRC_Continue; +} + + +/* +** If the SELECT references the table pWalker->u.pTab, then do two things: +** +** (1) Mark the SELECT as as SF_Correlated. +** (2) Set pWalker->eCode to non-zero so that the caller will know +** that (1) has happened. +*/ +static int sqlite3ReturningSubqueryCorrelated(Walker *pWalker, Select *pSelect){ + int i; + SrcList *pSrc; + assert( pSelect!=0 ); + pSrc = pSelect->pSrc; + assert( pSrc!=0 ); + for(i=0; i<pSrc->nSrc; i++){ + if( pSrc->a[i].pSTab==pWalker->u.pTab ){ + testcase( pSelect->selFlags & SF_Correlated ); + pSelect->selFlags |= SF_Correlated; + pWalker->eCode = 1; + break; + } + } + return WRC_Continue; +} + +/* +** Scan the expression list that is the argument to RETURNING looking +** for subqueries that depend on the table which is being modified in the +** statement that is hosting the RETURNING clause (pTab). Mark all such +** subqueries as SF_Correlated. If the subqueries are part of an +** expression, mark the expression as EP_VarSelect. +** +** https://sqlite.org/forum/forumpost/2c83569ce8945d39 +*/ +static void sqlite3ProcessReturningSubqueries( + ExprList *pEList, + Table *pTab +){ + Walker w; + memset(&w, 0, sizeof(w)); + w.xExprCallback = sqlite3ExprWalkNoop; + w.xSelectCallback = sqlite3ReturningSubqueryCorrelated; + w.u.pTab = pTab; + sqlite3WalkExprList(&w, pEList); + if( w.eCode ){ + w.xExprCallback = sqlite3ReturningSubqueryVarSelect; + w.xSelectCallback = sqlite3SelectWalkNoop; + sqlite3WalkExprList(&w, pEList); + } +} + +/* +** Generate code for the RETURNING trigger. Unlike other triggers +** that invoke a subprogram in the bytecode, the code for RETURNING +** is generated in-line. +*/ +static void codeReturningTrigger( + Parse *pParse, /* Parse context */ + Trigger *pTrigger, /* The trigger step that defines the RETURNING */ + Table *pTab, /* The table to code triggers from */ + int regIn /* The first in an array of registers */ +){ + Vdbe *v = pParse->pVdbe; + sqlite3 *db = pParse->db; + ExprList *pNew; + Returning *pReturning; + Select sSelect; + SrcList *pFrom; + union { + SrcList sSrc; + u8 fromSpace[SZ_SRCLIST_1]; + } uSrc; + + assert( v!=0 ); + if( !pParse->bReturning ){ + /* This RETURNING trigger must be for a different statement as + ** this statement lacks a RETURNING clause. */ + return; + } + assert( db->pParse==pParse ); + assert( !pParse->isCreate ); + pReturning = pParse->u1.d.pReturning; + if( pTrigger != &(pReturning->retTrig) ){ + /* This RETURNING trigger is for a different statement */ + return; + } + memset(&sSelect, 0, sizeof(sSelect)); + memset(&uSrc, 0, sizeof(uSrc)); + pFrom = &uSrc.sSrc; + sSelect.pEList = sqlite3ExprListDup(db, pReturning->pReturnEL, 0); + sSelect.pSrc = pFrom; + pFrom->nSrc = 1; + pFrom->a[0].pSTab = pTab; + pFrom->a[0].zName = pTab->zName; /* tag-20240424-1 */ + pFrom->a[0].iCursor = -1; + sqlite3SelectPrep(pParse, &sSelect, 0); + if( pParse->nErr==0 ){ + assert( db->mallocFailed==0 ); + sqlite3GenerateColumnNames(pParse, &sSelect); + } + sqlite3ExprListDelete(db, sSelect.pEList); + pNew = sqlite3ExpandReturning(pParse, pReturning->pReturnEL, pTab); + if( pParse->nErr==0 ){ + NameContext sNC; + memset(&sNC, 0, sizeof(sNC)); + if( pReturning->nRetCol==0 ){ + pReturning->nRetCol = pNew->nExpr; + pReturning->iRetCur = pParse->nTab++; + } + sNC.pParse = pParse; + sNC.uNC.iBaseReg = regIn; + sNC.ncFlags = NC_UBaseReg; + pParse->eTriggerOp = pTrigger->op; + pParse->pTriggerTab = pTab; + if( sqlite3ResolveExprListNames(&sNC, pNew)==SQLITE_OK + && ALWAYS(!db->mallocFailed) + ){ + int i; + int nCol = pNew->nExpr; + int reg = pParse->nMem+1; + sqlite3ProcessReturningSubqueries(pNew, pTab); + pParse->nMem += nCol+2; + pReturning->iRetReg = reg; + for(i=0; i<nCol; i++){ + Expr *pCol = pNew->a[i].pExpr; + assert( pCol!=0 ); /* Due to !db->mallocFailed ~9 lines above */ + sqlite3ExprCodeFactorable(pParse, pCol, reg+i); + if( sqlite3ExprAffinity(pCol)==SQLITE_AFF_REAL ){ + sqlite3VdbeAddOp1(v, OP_RealAffinity, reg+i); + } + } + sqlite3VdbeAddOp3(v, OP_MakeRecord, reg, i, reg+i); + sqlite3VdbeAddOp2(v, OP_NewRowid, pReturning->iRetCur, reg+i+1); + sqlite3VdbeAddOp3(v, OP_Insert, pReturning->iRetCur, reg+i, reg+i+1); + } + } + sqlite3ExprListDelete(db, pNew); + pParse->eTriggerOp = 0; + pParse->pTriggerTab = 0; +} + + + /* ** Generate VDBE code for the statements inside the body of a single ** trigger. @@ -703,30 +1148,42 @@ static int codeTriggerProgram( pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf; assert( pParse->okConstFactor==0 ); +#ifndef SQLITE_OMIT_TRACE + if( pStep->zSpan ){ + sqlite3VdbeAddOp4(v, OP_Trace, 0x7fffffff, 1, 0, + sqlite3MPrintf(db, "-- %s", pStep->zSpan), + P4_DYNAMIC); + } +#endif + switch( pStep->op ){ case TK_UPDATE: { sqlite3Update(pParse, - targetSrcList(pParse, pStep), + sqlite3TriggerStepSrc(pParse, pStep), sqlite3ExprListDup(db, pStep->pExprList, 0), sqlite3ExprDup(db, pStep->pWhere, 0), - pParse->eOrconf + pParse->eOrconf, 0, 0, 0 ); + sqlite3VdbeAddOp0(v, OP_ResetCount); break; } case TK_INSERT: { sqlite3Insert(pParse, - targetSrcList(pParse, pStep), + sqlite3TriggerStepSrc(pParse, pStep), sqlite3SelectDup(db, pStep->pSelect, 0), sqlite3IdListDup(db, pStep->pIdList), - pParse->eOrconf + pParse->eOrconf, + sqlite3UpsertDup(db, pStep->pUpsert) ); + sqlite3VdbeAddOp0(v, OP_ResetCount); break; } case TK_DELETE: { sqlite3DeleteFrom(pParse, - targetSrcList(pParse, pStep), - sqlite3ExprDup(db, pStep->pWhere, 0) + sqlite3TriggerStepSrc(pParse, pStep), + sqlite3ExprDup(db, pStep->pWhere, 0), 0, 0 ); + sqlite3VdbeAddOp0(v, OP_ResetCount); break; } default: assert( pStep->op==TK_SELECT ); { @@ -738,9 +1195,6 @@ static int codeTriggerProgram( break; } } - if( pStep->op!=TK_SELECT ){ - sqlite3VdbeAddOp0(v, OP_ResetCount); - } } return 0; @@ -798,8 +1252,8 @@ static TriggerPrg *codeRowTrigger( Vdbe *v; /* Temporary VM */ NameContext sNC; /* Name context for sub-vdbe */ SubProgram *pProgram = 0; /* Sub-vdbe for trigger program */ - Parse *pSubParse; /* Parse context for sub-vdbe */ int iEndTrigger = 0; /* Label to jump to if WHEN is false */ + Parse sSubParse; /* Parse context for sub-vdbe */ assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) ); assert( pTop->pVdbe ); @@ -821,18 +1275,19 @@ static TriggerPrg *codeRowTrigger( /* Allocate and populate a new Parse context to use for coding the ** trigger sub-program. */ - pSubParse = sqlite3StackAllocZero(db, sizeof(Parse)); - if( !pSubParse ) return 0; + sqlite3ParseObjectInit(&sSubParse, db); memset(&sNC, 0, sizeof(sNC)); - sNC.pParse = pSubParse; - pSubParse->db = db; - pSubParse->pTriggerTab = pTab; - pSubParse->pToplevel = pTop; - pSubParse->zAuthContext = pTrigger->zName; - pSubParse->eTriggerOp = pTrigger->op; - pSubParse->nQueryLoop = pParse->nQueryLoop; - - v = sqlite3GetVdbe(pSubParse); + sNC.pParse = &sSubParse; + sSubParse.pTriggerTab = pTab; + sSubParse.pToplevel = pTop; + sSubParse.zAuthContext = pTrigger->zName; + sSubParse.eTriggerOp = pTrigger->op; + sSubParse.nQueryLoop = pParse->nQueryLoop; + sSubParse.prepFlags = pParse->prepFlags; + sSubParse.oldmask = 0; + sSubParse.newmask = 0; + + v = sqlite3GetVdbe(&sSubParse); if( v ){ VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)", pTrigger->zName, onErrorText(orconf), @@ -843,9 +1298,11 @@ static TriggerPrg *codeRowTrigger( pTab->zName )); #ifndef SQLITE_OMIT_TRACE - sqlite3VdbeChangeP4(v, -1, - sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC - ); + if( pTrigger->zName ){ + sqlite3VdbeChangeP4(v, -1, + sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC + ); + } #endif /* If one was specified, code the WHEN clause. If it evaluates to false @@ -853,17 +1310,17 @@ static TriggerPrg *codeRowTrigger( ** OP_Halt inserted at the end of the program. */ if( pTrigger->pWhen ){ pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0); - if( SQLITE_OK==sqlite3ResolveExprNames(&sNC, pWhen) - && db->mallocFailed==0 + if( db->mallocFailed==0 + && SQLITE_OK==sqlite3ResolveExprNames(&sNC, pWhen) ){ - iEndTrigger = sqlite3VdbeMakeLabel(v); - sqlite3ExprIfFalse(pSubParse, pWhen, iEndTrigger, SQLITE_JUMPIFNULL); + iEndTrigger = sqlite3VdbeMakeLabel(&sSubParse); + sqlite3ExprIfFalse(&sSubParse, pWhen, iEndTrigger, SQLITE_JUMPIFNULL); } sqlite3ExprDelete(db, pWhen); } /* Code the trigger program into the sub-vdbe. */ - codeTriggerProgram(pSubParse, pTrigger->step_list, orconf); + codeTriggerProgram(&sSubParse, pTrigger->step_list, orconf); /* Insert an OP_Halt at the end of the sub-program. */ if( iEndTrigger ){ @@ -871,25 +1328,24 @@ static TriggerPrg *codeRowTrigger( } sqlite3VdbeAddOp0(v, OP_Halt); VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf))); + transferParseError(pParse, &sSubParse); - transferParseError(pParse, pSubParse); - if( db->mallocFailed==0 ){ + if( pParse->nErr==0 ){ + assert( db->mallocFailed==0 ); pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg); } - pProgram->nMem = pSubParse->nMem; - pProgram->nCsr = pSubParse->nTab; - pProgram->nOnce = pSubParse->nOnce; + pProgram->nMem = sSubParse.nMem; + pProgram->nCsr = sSubParse.nTab; pProgram->token = (void *)pTrigger; - pPrg->aColmask[0] = pSubParse->oldmask; - pPrg->aColmask[1] = pSubParse->newmask; + pPrg->aColmask[0] = sSubParse.oldmask; + pPrg->aColmask[1] = sSubParse.newmask; sqlite3VdbeDelete(v); + }else{ + transferParseError(pParse, &sSubParse); } - assert( !pSubParse->pAinc && !pSubParse->pZombieTab ); - assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg ); - sqlite3ParserReset(pSubParse); - sqlite3StackFree(db, pSubParse); - + assert( !sSubParse.pTriggerPrg && !sSubParse.nMaxArg ); + sqlite3ParseObjectReset(&sSubParse); return pPrg; } @@ -922,6 +1378,7 @@ static TriggerPrg *getRowTrigger( /* If an existing TriggerPrg could not be located, create a new one. */ if( !pPrg ){ pPrg = codeRowTrigger(pParse, pTrigger, pTab, orconf); + pParse->db->errByteOffset = -1; } return pPrg; @@ -944,7 +1401,7 @@ void sqlite3CodeRowTriggerDirect( Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */ TriggerPrg *pPrg; pPrg = getRowTrigger(pParse, p, pTab, orconf); - assert( pPrg || pParse->nErr || pParse->db->mallocFailed ); + assert( pPrg || pParse->nErr ); /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program ** is a pointer to the sub-vdbe containing the trigger program. */ @@ -961,7 +1418,7 @@ void sqlite3CodeRowTriggerDirect( ** invocation is disallowed if (a) the sub-program is really a trigger, ** not a foreign key action, and (b) the flag to enable recursive triggers ** is clear. */ - sqlite3VdbeChangeP5(v, (u8)bRecursive); + sqlite3VdbeChangeP5(v, (u16)bRecursive); } } @@ -987,7 +1444,7 @@ void sqlite3CodeRowTriggerDirect( ** ... ... ** reg+N OLD.* value of right-most column of pTab ** reg+N+1 NEW.rowid -** reg+N+2 OLD.* value of left-most column of pTab +** reg+N+2 NEW.* value of left-most column of pTab ** ... ... ** reg+N+N+1 NEW.* value of right-most column of pTab ** @@ -1032,12 +1489,20 @@ void sqlite3CodeRowTrigger( assert( p->pSchema==p->pTabSchema || p->pSchema==pParse->db->aDb[1].pSchema ); - /* Determine whether we should code this trigger */ - if( p->op==op + /* Determine whether we should code this trigger. One of two choices: + ** 1. The trigger is an exact match to the current DML statement + ** 2. This is a RETURNING trigger for INSERT but we are currently + ** doing the UPDATE part of an UPSERT. + */ + if( (p->op==op || (p->bReturning && p->op==TK_INSERT && op==TK_UPDATE)) && p->tr_tm==tr_tm && checkColumnOverlap(p->pColumns, pChanges) ){ - sqlite3CodeRowTriggerDirect(pParse, p, pTab, reg, orconf, ignoreJump); + if( !p->bReturning ){ + sqlite3CodeRowTriggerDirect(pParse, p, pTab, reg, orconf, ignoreJump); + }else if( sqlite3IsToplevel(pParse) ){ + codeReturningTrigger(pParse, p, pTab, reg); + } } } } @@ -1081,14 +1546,22 @@ u32 sqlite3TriggerColmask( Trigger *p; assert( isNew==1 || isNew==0 ); + if( IsView(pTab) ){ + return 0xffffffff; + } for(p=pTrigger; p; p=p->pNext){ - if( p->op==op && (tr_tm&p->tr_tm) + if( p->op==op + && (tr_tm&p->tr_tm) && checkColumnOverlap(p->pColumns,pChanges) ){ - TriggerPrg *pPrg; - pPrg = getRowTrigger(pParse, p, pTab, orconf); - if( pPrg ){ - mask |= pPrg->aColmask[isNew]; + if( p->bReturning ){ + mask = 0xffffffff; + }else{ + TriggerPrg *pPrg; + pPrg = getRowTrigger(pParse, p, pTab, orconf); + if( pPrg ){ + mask |= pPrg->aColmask[isNew]; + } } } } diff --git a/src/update.c b/src/update.c index 2a436b9701..979afea1f5 100644 --- a/src/update.c +++ b/src/update.c @@ -30,10 +30,10 @@ static void updateVirtualTable( /* ** The most recently coded instruction was an OP_Column to retrieve the -** i-th column of table pTab. This routine sets the P4 parameter of the +** i-th column of table pTab. This routine sets the P4 parameter of the ** OP_Column to the default value, if any. ** -** The default value of a column is specified by a DEFAULT clause in the +** The default value of a column is specified by a DEFAULT clause in the ** column definition. This was either supplied by the user when the table ** was created, or added later to the table definition by an ALTER TABLE ** command. If the latter, then the row-records in the table btree on disk @@ -42,70 +42,270 @@ static void updateVirtualTable( ** If the former, then all row-records are guaranteed to include a value ** for the column and the P4 value is not required. ** -** Column definitions created by an ALTER TABLE command may only have +** Column definitions created by an ALTER TABLE command may only have ** literal default values specified: a number, null or a string. (If a more -** complicated default expression value was provided, it is evaluated +** complicated default expression value was provided, it is evaluated ** when the ALTER TABLE is executed and one of the literal values written -** into the sqlite_master table.) +** into the sqlite_schema table.) ** ** Therefore, the P4 parameter is only required if the default value for ** the column is a literal number, string or null. The sqlite3ValueFromExpr() ** function is capable of transforming these types of expressions into ** sqlite3_value objects. ** -** If parameter iReg is not negative, code an OP_RealAffinity instruction -** on register iReg. This is used when an equivalent integer value is -** stored in place of an 8-byte floating point value in order to save -** space. +** If column as REAL affinity and the table is an ordinary b-tree table +** (not a virtual table) then the value might have been stored as an +** integer. In that case, add an OP_RealAffinity opcode to make sure +** it has been converted into REAL. */ void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i, int iReg){ + Column *pCol; assert( pTab!=0 ); - if( !pTab->pSelect ){ + assert( pTab->nCol>i ); + pCol = &pTab->aCol[i]; + if( pCol->iDflt ){ sqlite3_value *pValue = 0; u8 enc = ENC(sqlite3VdbeDb(v)); - Column *pCol = &pTab->aCol[i]; - VdbeComment((v, "%s.%s", pTab->zName, pCol->zName)); + assert( !IsView(pTab) ); + VdbeComment((v, "%s.%s", pTab->zName, pCol->zCnName)); assert( i<pTab->nCol ); - sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc, + sqlite3ValueFromExpr(sqlite3VdbeDb(v), + sqlite3ColumnExpr(pTab,pCol), enc, pCol->affinity, &pValue); if( pValue ){ - sqlite3VdbeChangeP4(v, -1, (const char *)pValue, P4_MEM); + sqlite3VdbeAppendP4(v, pValue, P4_MEM); } + } #ifndef SQLITE_OMIT_FLOATING_POINT - if( pTab->aCol[i].affinity==SQLITE_AFF_REAL ){ - sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg); + if( pCol->affinity==SQLITE_AFF_REAL && !IsVirtual(pTab) ){ + sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg); + } +#endif +} + +/* +** Check to see if column iCol of index pIdx references any of the +** columns defined by aXRef and chngRowid. Return true if it does +** and false if not. This is an optimization. False-positives are a +** performance degradation, but false-negatives can result in a corrupt +** index and incorrect answers. +** +** aXRef[j] will be non-negative if column j of the original table is +** being updated. chngRowid will be true if the rowid of the table is +** being updated. +*/ +static int indexColumnIsBeingUpdated( + Index *pIdx, /* The index to check */ + int iCol, /* Which column of the index to check */ + int *aXRef, /* aXRef[j]>=0 if column j is being updated */ + int chngRowid /* true if the rowid is being updated */ +){ + i16 iIdxCol = pIdx->aiColumn[iCol]; + assert( iIdxCol!=XN_ROWID ); /* Cannot index rowid */ + if( iIdxCol>=0 ){ + return aXRef[iIdxCol]>=0; + } + assert( iIdxCol==XN_EXPR ); + assert( pIdx->aColExpr!=0 ); + assert( pIdx->aColExpr->a[iCol].pExpr!=0 ); + return sqlite3ExprReferencesUpdatedColumn(pIdx->aColExpr->a[iCol].pExpr, + aXRef,chngRowid); +} + +/* +** Check to see if index pIdx is a partial index whose conditional +** expression might change values due to an UPDATE. Return true if +** the index is subject to change and false if the index is guaranteed +** to be unchanged. This is an optimization. False-positives are a +** performance degradation, but false-negatives can result in a corrupt +** index and incorrect answers. +** +** aXRef[j] will be non-negative if column j of the original table is +** being updated. chngRowid will be true if the rowid of the table is +** being updated. +*/ +static int indexWhereClauseMightChange( + Index *pIdx, /* The index to check */ + int *aXRef, /* aXRef[j]>=0 if column j is being updated */ + int chngRowid /* true if the rowid is being updated */ +){ + if( pIdx->pPartIdxWhere==0 ) return 0; + return sqlite3ExprReferencesUpdatedColumn(pIdx->pPartIdxWhere, + aXRef, chngRowid); +} + +/* +** Allocate and return a pointer to an expression of type TK_ROW with +** Expr.iColumn set to value (iCol+1). The resolver will modify the +** expression to be a TK_COLUMN reading column iCol of the first +** table in the source-list (pSrc->a[0]). +*/ +static Expr *exprRowColumn(Parse *pParse, int iCol){ + Expr *pRet = sqlite3PExpr(pParse, TK_ROW, 0, 0); + if( pRet ) pRet->iColumn = iCol+1; + return pRet; +} + +/* +** Assuming both the pLimit and pOrderBy parameters are NULL, this function +** generates VM code to run the query: +** +** SELECT <other-columns>, pChanges FROM pTabList WHERE pWhere +** +** and write the results to the ephemeral table already opened as cursor +** iEph. None of pChanges, pTabList or pWhere are modified or consumed by +** this function, they must be deleted by the caller. +** +** Or, if pLimit and pOrderBy are not NULL, and pTab is not a view: +** +** SELECT <other-columns>, pChanges FROM pTabList +** WHERE pWhere +** GROUP BY <other-columns> +** ORDER BY pOrderBy LIMIT pLimit +** +** If pTab is a view, the GROUP BY clause is omitted. +** +** Exactly how results are written to table iEph, and exactly what +** the <other-columns> in the query above are is determined by the type +** of table pTabList->a[0].pTab. +** +** If the table is a WITHOUT ROWID table, then argument pPk must be its +** PRIMARY KEY. In this case <other-columns> are the primary key columns +** of the table, in order. The results of the query are written to ephemeral +** table iEph as index keys, using OP_IdxInsert. +** +** If the table is actually a view, then <other-columns> are all columns of +** the view. The results are written to the ephemeral table iEph as records +** with automatically assigned integer keys. +** +** If the table is a virtual or ordinary intkey table, then <other-columns> +** is its rowid. For a virtual table, the results are written to iEph as +** records with automatically assigned integer keys For intkey tables, the +** rowid value in <other-columns> is used as the integer key, and the +** remaining fields make up the table record. +*/ +static void updateFromSelect( + Parse *pParse, /* Parse context */ + int iEph, /* Cursor for open eph. table */ + Index *pPk, /* PK if table 0 is WITHOUT ROWID */ + ExprList *pChanges, /* List of expressions to return */ + SrcList *pTabList, /* List of tables to select from */ + Expr *pWhere, /* WHERE clause for query */ + ExprList *pOrderBy, /* ORDER BY clause */ + Expr *pLimit /* LIMIT clause */ +){ + int i; + SelectDest dest; + Select *pSelect = 0; + ExprList *pList = 0; + ExprList *pGrp = 0; + Expr *pLimit2 = 0; + ExprList *pOrderBy2 = 0; + sqlite3 *db = pParse->db; + Table *pTab = pTabList->a[0].pSTab; + SrcList *pSrc; + Expr *pWhere2; + int eDest; + +#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT + if( pOrderBy && pLimit==0 ) { + sqlite3ErrorMsg(pParse, "ORDER BY without LIMIT on UPDATE"); + return; + } + pOrderBy2 = sqlite3ExprListDup(db, pOrderBy, 0); + pLimit2 = sqlite3ExprDup(db, pLimit, 0); +#else + UNUSED_PARAMETER(pOrderBy); + UNUSED_PARAMETER(pLimit); +#endif + + pSrc = sqlite3SrcListDup(db, pTabList, 0); + pWhere2 = sqlite3ExprDup(db, pWhere, 0); + + assert( pTabList->nSrc>1 ); + if( pSrc ){ + assert( pSrc->a[0].fg.notCte ); + pSrc->a[0].iCursor = -1; + pSrc->a[0].pSTab->nTabRef--; + pSrc->a[0].pSTab = 0; + } + if( pPk ){ + for(i=0; i<pPk->nKeyCol; i++){ + Expr *pNew = exprRowColumn(pParse, pPk->aiColumn[i]); +#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT + if( pLimit ){ + pGrp = sqlite3ExprListAppend(pParse, pGrp, sqlite3ExprDup(db, pNew, 0)); + } +#endif + pList = sqlite3ExprListAppend(pParse, pList, pNew); + } + eDest = IsVirtual(pTab) ? SRT_Table : SRT_Upfrom; + }else if( IsView(pTab) ){ + for(i=0; i<pTab->nCol; i++){ + pList = sqlite3ExprListAppend(pParse, pList, exprRowColumn(pParse, i)); + } + eDest = SRT_Table; + }else{ + eDest = IsVirtual(pTab) ? SRT_Table : SRT_Upfrom; + pList = sqlite3ExprListAppend(pParse, 0, sqlite3PExpr(pParse,TK_ROW,0,0)); +#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT + if( pLimit ){ + pGrp = sqlite3ExprListAppend(pParse, 0, sqlite3PExpr(pParse,TK_ROW,0,0)); } #endif } + assert( pChanges!=0 || pParse->db->mallocFailed ); + if( pChanges ){ + for(i=0; i<pChanges->nExpr; i++){ + pList = sqlite3ExprListAppend(pParse, pList, + sqlite3ExprDup(db, pChanges->a[i].pExpr, 0) + ); + } + } + pSelect = sqlite3SelectNew(pParse, pList, + pSrc, pWhere2, pGrp, 0, pOrderBy2, + SF_UFSrcCheck|SF_IncludeHidden|SF_UpdateFrom, pLimit2 + ); + if( pSelect ) pSelect->selFlags |= SF_OrderByReqd; + sqlite3SelectDestInit(&dest, eDest, iEph); + dest.iSDParm2 = (pPk ? pPk->nKeyCol : -1); + sqlite3Select(pParse, pSelect, &dest); + sqlite3SelectDelete(db, pSelect); } /* ** Process an UPDATE statement. ** -** UPDATE OR IGNORE table_wxyz SET a=b, c=d WHERE e<5 AND f NOT NULL; -** \_______/ \________/ \______/ \________________/ -* onError pTabList pChanges pWhere +** UPDATE OR IGNORE tbl SET a=b, c=d FROM tbl2... WHERE e<5 AND f NOT NULL; +** \_______/ \_/ \______/ \_____/ \________________/ +** onError | pChanges | pWhere +** \_______________________/ +** pTabList */ void sqlite3Update( Parse *pParse, /* The parser context */ SrcList *pTabList, /* The table in which we should change things */ ExprList *pChanges, /* Things to be changed */ Expr *pWhere, /* The WHERE clause. May be null */ - int onError /* How to handle constraint errors */ + int onError, /* How to handle constraint errors */ + ExprList *pOrderBy, /* ORDER BY clause. May be null */ + Expr *pLimit, /* LIMIT clause. May be null */ + Upsert *pUpsert /* ON CONFLICT clause, or null */ ){ - int i, j; /* Loop counters */ + int i, j, k; /* Loop counters */ Table *pTab; /* The table to be updated */ int addrTop = 0; /* VDBE instruction address of the start of the loop */ - WhereInfo *pWInfo; /* Information about the WHERE clause */ + WhereInfo *pWInfo = 0; /* Information about the WHERE clause */ Vdbe *v; /* The virtual database engine */ Index *pIdx; /* For looping over indices */ Index *pPk; /* The PRIMARY KEY index for WITHOUT ROWID tables */ int nIdx; /* Number of indices that need updating */ + int nAllIdx; /* Total number of indexes */ int iBaseCur; /* Base cursor number */ int iDataCur; /* Cursor for the canonical data btree */ int iIdxCur; /* Cursor for the first index */ sqlite3 *db; /* The database structure */ - int *aRegIdx = 0; /* One register assigned to each index to be updated */ + int *aRegIdx = 0; /* Registers for to each index and the main table */ int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the ** an expression for the i-th column of the table. ** aXRef[i]==-1 if the i-th column is not changed. */ @@ -114,13 +314,15 @@ void sqlite3Update( u8 chngRowid; /* Rowid changed in a normal table */ u8 chngKey; /* Either chngPk or chngRowid */ Expr *pRowidExpr = 0; /* Expression defining the new record number */ + int iRowidExpr = -1; /* Index of "rowid=" (or IPK) assignment in pChanges */ AuthContext sContext; /* The authorization context */ NameContext sNC; /* The name-context to resolve expressions in */ int iDb; /* Database containing the table being updated */ - int okOnePass; /* True for one-pass algorithm without the FIFO */ + int eOnePass; /* ONEPASS_XXX value from where.c */ int hasFK; /* True if foreign key processing is required */ int labelBreak; /* Jump here to break out of UPDATE loop */ int labelContinue; /* Jump here to continue next step of UPDATE loop */ + int flags; /* Flags for sqlite3WhereBegin() */ #ifndef SQLITE_OMIT_TRIGGER int isView; /* True when updating a view (INSTEAD OF trigger) */ @@ -131,6 +333,12 @@ void sqlite3Update( int iEph = 0; /* Ephemeral table holding all primary key values */ int nKey = 0; /* Number of elements in regKey for WITHOUT ROWID */ int aiCurOnePass[2]; /* The write cursors opened by WHERE_ONEPASS */ + int addrOpen = 0; /* Address of OP_OpenEphemeral */ + int iPk = 0; /* First of nPk cells holding PRIMARY KEY value */ + i16 nPk = 0; /* Number of components of the PRIMARY KEY */ + int bReplace = 0; /* True if REPLACE conflict resolution might happen */ + int bFinishSeek = 1; /* The OP_FinishSeek opcode is needed */ + int nChangeFrom = 0; /* If there is a FROM, pChanges->nExpr, else 0 */ /* Register Allocations */ int regRowCount = 0; /* A count of rows changed */ @@ -143,12 +351,13 @@ void sqlite3Update( memset(&sContext, 0, sizeof(sContext)); db = pParse->db; - if( pParse->nErr || db->mallocFailed ){ + assert( db->pParse==pParse ); + if( pParse->nErr ){ goto update_cleanup; } - assert( pTabList->nSrc==1 ); + assert( db->mallocFailed==0 ); - /* Locate the table which we want to update. + /* Locate the table which we want to update. */ pTab = sqlite3SrcListLookup(pParse, pTabList); if( pTab==0 ) goto update_cleanup; @@ -159,7 +368,7 @@ void sqlite3Update( */ #ifndef SQLITE_OMIT_TRIGGER pTrigger = sqlite3TriggersExist(pParse, pTab, TK_UPDATE, pChanges, &tmask); - isView = pTab->pSelect!=0; + isView = IsView(pTab); assert( pTrigger || tmask==0 ); #else # define pTrigger 0 @@ -171,10 +380,35 @@ void sqlite3Update( # define isView 0 #endif +#if TREETRACE_ENABLED + if( sqlite3TreeTrace & 0x10000 ){ + sqlite3TreeViewLine(0, "In sqlite3Update() at %s:%d", __FILE__, __LINE__); + sqlite3TreeViewUpdate(pParse->pWith, pTabList, pChanges, pWhere, + onError, pOrderBy, pLimit, pUpsert, pTrigger); + } +#endif + + /* If there was a FROM clause, set nChangeFrom to the number of expressions + ** in the change-list. Otherwise, set it to 0. There cannot be a FROM + ** clause if this function is being called to generate code for part of + ** an UPSERT statement. */ + nChangeFrom = (pTabList->nSrc>1) ? pChanges->nExpr : 0; + assert( nChangeFrom==0 || pUpsert==0 ); + +#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT + if( !isView && nChangeFrom==0 ){ + pWhere = sqlite3LimitWhere( + pParse, pTabList, pWhere, pOrderBy, pLimit, "UPDATE" + ); + pOrderBy = 0; + pLimit = 0; + } +#endif + if( sqlite3ViewGetColumnNames(pParse, pTab) ){ goto update_cleanup; } - if( sqlite3IsReadOnly(pParse, pTab, tmask) ){ + if( sqlite3IsReadOnly(pParse, pTab, pTrigger) ){ goto update_cleanup; } @@ -183,24 +417,31 @@ void sqlite3Update( ** need to occur right after the database cursor. So go ahead and ** allocate enough space, just in case. */ - pTabList->a[0].iCursor = iBaseCur = iDataCur = pParse->nTab++; + iBaseCur = iDataCur = pParse->nTab++; iIdxCur = iDataCur+1; pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab); + testcase( pPk!=0 && pPk!=pTab->pIndex ); for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){ - if( IsPrimaryKeyIndex(pIdx) && pPk!=0 ){ + if( pPk==pIdx ){ iDataCur = pParse->nTab; - pTabList->a[0].iCursor = iDataCur; } pParse->nTab++; } + if( pUpsert ){ + /* On an UPSERT, reuse the same cursors already opened by INSERT */ + iDataCur = pUpsert->iDataCur; + iIdxCur = pUpsert->iIdxCur; + pParse->nTab = iBaseCur; + } + pTabList->a[0].iCursor = iDataCur; - /* Allocate space for aXRef[], aRegIdx[], and aToOpen[]. + /* Allocate space for aXRef[], aRegIdx[], and aToOpen[]. ** Initialize aXRef[] and aToOpen[] to their default values. */ - aXRef = sqlite3DbMallocRawNN(db, sizeof(int) * (pTab->nCol+nIdx) + nIdx+2 ); + aXRef = sqlite3DbMallocRawNN(db, sizeof(int) * (pTab->nCol+nIdx+1) + nIdx+2 ); if( aXRef==0 ) goto update_cleanup; aRegIdx = aXRef+pTab->nCol; - aToOpen = (u8*)(aRegIdx+nIdx); + aToOpen = (u8*)(aRegIdx+nIdx+1); memset(aToOpen, 1, nIdx+1); aToOpen[nIdx+1] = 0; for(i=0; i<pTab->nCol; i++) aXRef[i] = -1; @@ -209,6 +450,12 @@ void sqlite3Update( memset(&sNC, 0, sizeof(sNC)); sNC.pParse = pParse; sNC.pSrcList = pTabList; + sNC.uNC.pUpsert = pUpsert; + sNC.ncFlags = NC_UUpsert; + + /* Begin generating code. */ + v = sqlite3GetVdbe(pParse); + if( v==0 ) goto update_cleanup; /* Resolve the column names in all the expressions of the ** of the UPDATE statement. Also find the column index @@ -218,28 +465,39 @@ void sqlite3Update( */ chngRowid = chngPk = 0; for(i=0; i<pChanges->nExpr; i++){ - if( sqlite3ResolveExprNames(&sNC, pChanges->a[i].pExpr) ){ + /* If this is an UPDATE with a FROM clause, do not resolve expressions + ** here. The call to sqlite3Select() below will do that. */ + if( nChangeFrom==0 && sqlite3ResolveExprNames(&sNC, pChanges->a[i].pExpr) ){ goto update_cleanup; } - for(j=0; j<pTab->nCol; j++){ - if( sqlite3StrICmp(pTab->aCol[j].zName, pChanges->a[i].zName)==0 ){ - if( j==pTab->iPKey ){ - chngRowid = 1; - pRowidExpr = pChanges->a[i].pExpr; - }else if( pPk && (pTab->aCol[j].colFlags & COLFLAG_PRIMKEY)!=0 ){ - chngPk = 1; - } - aXRef[j] = i; - break; + j = sqlite3ColumnIndex(pTab, pChanges->a[i].zEName); + if( j>=0 ){ + if( j==pTab->iPKey ){ + chngRowid = 1; + pRowidExpr = pChanges->a[i].pExpr; + iRowidExpr = i; + }else if( pPk && (pTab->aCol[j].colFlags & COLFLAG_PRIMKEY)!=0 ){ + chngPk = 1; } - } - if( j>=pTab->nCol ){ - if( pPk==0 && sqlite3IsRowid(pChanges->a[i].zName) ){ +#ifndef SQLITE_OMIT_GENERATED_COLUMNS + else if( pTab->aCol[j].colFlags & COLFLAG_GENERATED ){ + testcase( pTab->aCol[j].colFlags & COLFLAG_VIRTUAL ); + testcase( pTab->aCol[j].colFlags & COLFLAG_STORED ); + sqlite3ErrorMsg(pParse, + "cannot UPDATE generated column \"%s\"", + pTab->aCol[j].zCnName); + goto update_cleanup; + } +#endif + aXRef[j] = i; + }else{ + if( pPk==0 && sqlite3IsRowid(pChanges->a[i].zEName) ){ j = -1; chngRowid = 1; pRowidExpr = pChanges->a[i].pExpr; + iRowidExpr = i; }else{ - sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zName); + sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zEName); pParse->checkSchema = 1; goto update_cleanup; } @@ -248,8 +506,8 @@ void sqlite3Update( { int rc; rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName, - j<0 ? "ROWID" : pTab->aCol[j].zName, - db->aDb[iDb].zName); + j<0 ? "ROWID" : pTab->aCol[j].zCnName, + db->aDb[iDb].zDbSName); if( rc==SQLITE_DENY ){ goto update_cleanup; }else if( rc==SQLITE_IGNORE ){ @@ -263,48 +521,91 @@ void sqlite3Update( assert( chngPk==0 || chngPk==1 ); chngKey = chngRowid + chngPk; - /* The SET expressions are not actually used inside the WHERE loop. +#ifndef SQLITE_OMIT_GENERATED_COLUMNS + /* Mark generated columns as changing if their generator expressions + ** reference any changing column. The actual aXRef[] value for + ** generated expressions is not used, other than to check to see that it + ** is non-negative, so the value of aXRef[] for generated columns can be + ** set to any non-negative number. We use 99999 so that the value is + ** obvious when looking at aXRef[] in a symbolic debugger. + */ + if( pTab->tabFlags & TF_HasGenerated ){ + int bProgress; + testcase( pTab->tabFlags & TF_HasVirtual ); + testcase( pTab->tabFlags & TF_HasStored ); + do{ + bProgress = 0; + for(i=0; i<pTab->nCol; i++){ + if( aXRef[i]>=0 ) continue; + if( (pTab->aCol[i].colFlags & COLFLAG_GENERATED)==0 ) continue; + if( sqlite3ExprReferencesUpdatedColumn( + sqlite3ColumnExpr(pTab, &pTab->aCol[i]), + aXRef, chngRowid) + ){ + aXRef[i] = 99999; + bProgress = 1; + } + } + }while( bProgress ); + } +#endif + + /* The SET expressions are not actually used inside the WHERE loop. ** So reset the colUsed mask. Unless this is a virtual table. In that ** case, set all bits of the colUsed mask (to ensure that the virtual ** table implementation makes all columns available). */ - pTabList->a[0].colUsed = IsVirtual(pTab) ? (Bitmask)-1 : 0; + pTabList->a[0].colUsed = IsVirtual(pTab) ? ALLBITS : 0; hasFK = sqlite3FkRequired(pParse, pTab, aXRef, chngKey); /* There is one entry in the aRegIdx[] array for each index on the table ** being updated. Fill in aRegIdx[] with a register number that will hold ** the key for accessing each index. - ** - ** FIXME: Be smarter about omitting indexes that use expressions. */ - for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ + if( onError==OE_Replace ) bReplace = 1; + for(nAllIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nAllIdx++){ int reg; - if( chngKey || hasFK || pIdx->pPartIdxWhere || pIdx==pPk ){ + if( chngKey || hasFK>1 || pIdx==pPk + || indexWhereClauseMightChange(pIdx,aXRef,chngRowid) + ){ reg = ++pParse->nMem; + pParse->nMem += pIdx->nColumn; }else{ reg = 0; for(i=0; i<pIdx->nKeyCol; i++){ - i16 iIdxCol = pIdx->aiColumn[i]; - if( iIdxCol<0 || aXRef[iIdxCol]>=0 ){ + if( indexColumnIsBeingUpdated(pIdx, i, aXRef, chngRowid) ){ reg = ++pParse->nMem; + pParse->nMem += pIdx->nColumn; + if( onError==OE_Default && pIdx->onError==OE_Replace ){ + bReplace = 1; + } break; } } } - if( reg==0 ) aToOpen[j+1] = 0; - aRegIdx[j] = reg; + if( reg==0 ) aToOpen[nAllIdx+1] = 0; + aRegIdx[nAllIdx] = reg; + } + aRegIdx[nAllIdx] = ++pParse->nMem; /* Register storing the table record */ + if( bReplace ){ + /* If REPLACE conflict resolution might be invoked, open cursors on all + ** indexes in case they are needed to delete records. */ + memset(aToOpen, 1, nIdx+1); } - /* Begin generating code. */ - v = sqlite3GetVdbe(pParse); - if( v==0 ) goto update_cleanup; if( pParse->nested==0 ) sqlite3VdbeCountChanges(v); - sqlite3BeginWriteOperation(pParse, 1, iDb); + sqlite3BeginWriteOperation(pParse, pTrigger || hasFK, iDb); /* Allocate required registers. */ if( !IsVirtual(pTab) ){ - regRowSet = ++pParse->nMem; + /* For now, regRowSet and aRegIdx[nAllIdx] share the same register. + ** If regRowSet turns out to be needed, then aRegIdx[nAllIdx] will be + ** reallocated. aRegIdx[nAllIdx] is the register in which the main + ** table record is written. regRowSet holds the RowSet for the + ** two-pass update algorithm. */ + assert( aRegIdx[nAllIdx]==pParse->nMem ); + regRowSet = aRegIdx[nAllIdx]; regOldRowid = regNewRowid = ++pParse->nMem; if( chngPk || pTrigger || hasFK ){ regOld = pParse->nMem + 1; @@ -326,15 +627,19 @@ void sqlite3Update( ** an ephemeral table. */ #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) - if( isView ){ - sqlite3MaterializeView(pParse, pTab, pWhere, iDataCur); + if( nChangeFrom==0 && isView ){ + sqlite3MaterializeView(pParse, pTab, + pWhere, pOrderBy, pLimit, iDataCur + ); + pOrderBy = 0; + pLimit = 0; } #endif /* Resolve the column names in all the expressions in the ** WHERE clause. */ - if( sqlite3ResolveExprNames(&sNC, pWhere) ){ + if( nChangeFrom==0 && sqlite3ResolveExprNames(&sNC, pWhere) ){ goto update_cleanup; } @@ -347,126 +652,245 @@ void sqlite3Update( } #endif - /* Begin the database scan - */ - if( HasRowid(pTab) ){ + /* Jump to labelBreak to abandon further processing of this UPDATE */ + labelContinue = labelBreak = sqlite3VdbeMakeLabel(pParse); + + /* Not an UPSERT. Normal processing. Begin by + ** initialize the count of updated rows */ + if( (db->flags&SQLITE_CountRows)!=0 + && !pParse->pTriggerTab + && !pParse->nested + && !pParse->bReturning + && pUpsert==0 + ){ + regRowCount = ++pParse->nMem; + sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount); + } + + if( nChangeFrom==0 && HasRowid(pTab) ){ sqlite3VdbeAddOp3(v, OP_Null, 0, regRowSet, regOldRowid); - pWInfo = sqlite3WhereBegin( - pParse, pTabList, pWhere, 0, 0, WHERE_ONEPASS_DESIRED, iIdxCur - ); - if( pWInfo==0 ) goto update_cleanup; - okOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass); - - /* Remember the rowid of every item to be updated. - */ - sqlite3VdbeAddOp2(v, OP_Rowid, iDataCur, regOldRowid); - if( !okOnePass ){ - sqlite3VdbeAddOp2(v, OP_RowSetAdd, regRowSet, regOldRowid); - } - - /* End the database scan loop. - */ - sqlite3WhereEnd(pWInfo); + iEph = pParse->nTab++; + addrOpen = sqlite3VdbeAddOp3(v, OP_OpenEphemeral, iEph, 0, regRowSet); }else{ - int iPk; /* First of nPk memory cells holding PRIMARY KEY value */ - i16 nPk; /* Number of components of the PRIMARY KEY */ - int addrOpen; /* Address of the OpenEphemeral instruction */ - - assert( pPk!=0 ); - nPk = pPk->nKeyCol; + assert( pPk!=0 || HasRowid(pTab) ); + nPk = pPk ? pPk->nKeyCol : 0; iPk = pParse->nMem+1; pParse->nMem += nPk; + pParse->nMem += nChangeFrom; regKey = ++pParse->nMem; - iEph = pParse->nTab++; - sqlite3VdbeAddOp2(v, OP_Null, 0, iPk); - addrOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEph, nPk); - sqlite3VdbeSetP4KeyInfo(pParse, pPk); - pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, - WHERE_ONEPASS_DESIRED, iIdxCur); - if( pWInfo==0 ) goto update_cleanup; - okOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass); - for(i=0; i<nPk; i++){ - assert( pPk->aiColumn[i]>=0 ); - sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, pPk->aiColumn[i], - iPk+i); - } - if( okOnePass ){ - sqlite3VdbeChangeToNoop(v, addrOpen); - nKey = nPk; - regKey = iPk; - }else{ - sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, regKey, - sqlite3IndexAffinityStr(db, pPk), nPk); - sqlite3VdbeAddOp2(v, OP_IdxInsert, iEph, regKey); + if( pUpsert==0 ){ + int nEphCol = nPk + nChangeFrom + (isView ? pTab->nCol : 0); + iEph = pParse->nTab++; + if( pPk ) sqlite3VdbeAddOp3(v, OP_Null, 0, iPk, iPk+nPk-1); + addrOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEph, nEphCol); + if( pPk ){ + KeyInfo *pKeyInfo = sqlite3KeyInfoOfIndex(pParse, pPk); + if( pKeyInfo ){ + pKeyInfo->nAllField = nEphCol; + sqlite3VdbeAppendP4(v, pKeyInfo, P4_KEYINFO); + } + } + if( nChangeFrom ){ + updateFromSelect( + pParse, iEph, pPk, pChanges, pTabList, pWhere, pOrderBy, pLimit + ); +#ifndef SQLITE_OMIT_SUBQUERY + if( isView ) iDataCur = iEph; +#endif + } } - sqlite3WhereEnd(pWInfo); } - /* Initialize the count of updated rows - */ - if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab ){ - regRowCount = ++pParse->nMem; - sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount); - } + if( nChangeFrom ){ + sqlite3MultiWrite(pParse); + eOnePass = ONEPASS_OFF; + nKey = nPk; + regKey = iPk; + }else{ + if( pUpsert ){ + /* If this is an UPSERT, then all cursors have already been opened by + ** the outer INSERT and the data cursor should be pointing at the row + ** that is to be updated. So bypass the code that searches for the + ** row(s) to be updated. + */ + pWInfo = 0; + eOnePass = ONEPASS_SINGLE; + sqlite3ExprIfFalse(pParse, pWhere, labelBreak, SQLITE_JUMPIFNULL); + bFinishSeek = 0; + }else{ + /* Begin the database scan. + ** + ** Do not consider a single-pass strategy for a multi-row update if + ** there is anything that might disrupt the cursor being used to do + ** the UPDATE: + ** (1) This is a nested UPDATE + ** (2) There are triggers + ** (3) There are FOREIGN KEY constraints + ** (4) There are REPLACE conflict handlers + ** (5) There are subqueries in the WHERE clause + */ + flags = WHERE_ONEPASS_DESIRED; + if( !pParse->nested + && !pTrigger + && !hasFK + && !chngKey + && !bReplace + && (pWhere==0 || !ExprHasProperty(pWhere, EP_Subquery)) + ){ + flags |= WHERE_ONEPASS_MULTIROW; + } + pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere,0,0,0,flags,iIdxCur); + if( pWInfo==0 ) goto update_cleanup; - labelBreak = sqlite3VdbeMakeLabel(v); - if( !isView ){ - /* - ** Open every index that needs updating. Note that if any - ** index could potentially invoke a REPLACE conflict resolution - ** action, then we need to open all indices because we might need - ** to be deleting some records. - */ - if( onError==OE_Replace ){ - memset(aToOpen, 1, nIdx+1); + /* A one-pass strategy that might update more than one row may not + ** be used if any column of the index used for the scan is being + ** updated. Otherwise, if there is an index on "b", statements like + ** the following could create an infinite loop: + ** + ** UPDATE t1 SET b=b+1 WHERE b>? + ** + ** Fall back to ONEPASS_OFF if where.c has selected a ONEPASS_MULTI + ** strategy that uses an index for which one or more columns are being + ** updated. */ + eOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass); + bFinishSeek = sqlite3WhereUsesDeferredSeek(pWInfo); + if( eOnePass!=ONEPASS_SINGLE ){ + sqlite3MultiWrite(pParse); + if( eOnePass==ONEPASS_MULTI ){ + int iCur = aiCurOnePass[1]; + if( iCur>=0 && iCur!=iDataCur && aToOpen[iCur-iBaseCur] ){ + eOnePass = ONEPASS_OFF; + } + assert( iCur!=iDataCur || !HasRowid(pTab) ); + } + } + } + + if( HasRowid(pTab) ){ + /* Read the rowid of the current row of the WHERE scan. In ONEPASS_OFF + ** mode, write the rowid into the FIFO. In either of the one-pass modes, + ** leave it in register regOldRowid. */ + sqlite3VdbeAddOp2(v, OP_Rowid, iDataCur, regOldRowid); + if( eOnePass==ONEPASS_OFF ){ + aRegIdx[nAllIdx] = ++pParse->nMem; + sqlite3VdbeAddOp3(v, OP_Insert, iEph, regRowSet, regOldRowid); + }else{ + if( ALWAYS(addrOpen) ) sqlite3VdbeChangeToNoop(v, addrOpen); + } }else{ - for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ - if( pIdx->onError==OE_Replace ){ - memset(aToOpen, 1, nIdx+1); - break; + /* Read the PK of the current row into an array of registers. In + ** ONEPASS_OFF mode, serialize the array into a record and store it in + ** the ephemeral table. Or, in ONEPASS_SINGLE or MULTI mode, change + ** the OP_OpenEphemeral instruction to a Noop (the ephemeral table + ** is not required) and leave the PK fields in the array of registers. */ + for(i=0; i<nPk; i++){ + assert( pPk->aiColumn[i]>=0 ); + sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, + pPk->aiColumn[i], iPk+i); + } + if( eOnePass ){ + if( addrOpen ) sqlite3VdbeChangeToNoop(v, addrOpen); + nKey = nPk; + regKey = iPk; + }else{ + sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, regKey, + sqlite3IndexAffinityStr(db, pPk), nPk); + sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iEph, regKey, iPk, nPk); + } + } + } + + if( pUpsert==0 ){ + if( nChangeFrom==0 && eOnePass!=ONEPASS_MULTI ){ + sqlite3WhereEnd(pWInfo); + } + + if( !isView ){ + int addrOnce = 0; + int iNotUsed1 = 0; + int iNotUsed2 = 0; + + /* Open every index that needs updating. */ + if( eOnePass!=ONEPASS_OFF ){ + if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iBaseCur] = 0; + if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iBaseCur] = 0; + } + + if( eOnePass==ONEPASS_MULTI && (nIdx-(aiCurOnePass[1]>=0))>0 ){ + addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v); + } + sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, iBaseCur, + aToOpen, &iNotUsed1, &iNotUsed2); + if( addrOnce ){ + sqlite3VdbeJumpHereOrPopInst(v, addrOnce); + } + } + + /* Top of the update loop */ + if( eOnePass!=ONEPASS_OFF ){ + if( aiCurOnePass[0]!=iDataCur + && aiCurOnePass[1]!=iDataCur +#ifdef SQLITE_ALLOW_ROWID_IN_VIEW + && !isView +#endif + ){ + assert( pPk ); + sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelBreak, regKey,nKey); + VdbeCoverage(v); + } + if( eOnePass!=ONEPASS_SINGLE ){ + labelContinue = sqlite3VdbeMakeLabel(pParse); + } + sqlite3VdbeAddOp2(v, OP_IsNull, pPk ? regKey : regOldRowid, labelBreak); + VdbeCoverageIf(v, pPk==0); + VdbeCoverageIf(v, pPk!=0); + }else if( pPk || nChangeFrom ){ + labelContinue = sqlite3VdbeMakeLabel(pParse); + sqlite3VdbeAddOp2(v, OP_Rewind, iEph, labelBreak); VdbeCoverage(v); + addrTop = sqlite3VdbeCurrentAddr(v); + if( nChangeFrom ){ + if( !isView ){ + if( pPk ){ + for(i=0; i<nPk; i++){ + sqlite3VdbeAddOp3(v, OP_Column, iEph, i, iPk+i); + } + sqlite3VdbeAddOp4Int( + v, OP_NotFound, iDataCur, labelContinue, iPk, nPk + ); VdbeCoverage(v); + }else{ + sqlite3VdbeAddOp2(v, OP_Rowid, iEph, regOldRowid); + sqlite3VdbeAddOp3( + v, OP_NotExists, iDataCur, labelContinue, regOldRowid + ); VdbeCoverage(v); + } } + }else{ + sqlite3VdbeAddOp2(v, OP_RowData, iEph, regKey); + sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelContinue, regKey,0); + VdbeCoverage(v); } + }else{ + sqlite3VdbeAddOp2(v, OP_Rewind, iEph, labelBreak); VdbeCoverage(v); + labelContinue = sqlite3VdbeMakeLabel(pParse); + addrTop = sqlite3VdbeAddOp2(v, OP_Rowid, iEph, regOldRowid); + VdbeCoverage(v); + sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, labelContinue, regOldRowid); + VdbeCoverage(v); } - if( okOnePass ){ - if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iBaseCur] = 0; - if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iBaseCur] = 0; - } - sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, iBaseCur, aToOpen, - 0, 0); - } - - /* Top of the update loop */ - if( okOnePass ){ - if( aToOpen[iDataCur-iBaseCur] && !isView ){ - assert( pPk ); - sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelBreak, regKey, nKey); - VdbeCoverageNeverTaken(v); - } - labelContinue = labelBreak; - sqlite3VdbeAddOp2(v, OP_IsNull, pPk ? regKey : regOldRowid, labelBreak); - VdbeCoverageIf(v, pPk==0); - VdbeCoverageIf(v, pPk!=0); - }else if( pPk ){ - labelContinue = sqlite3VdbeMakeLabel(v); - sqlite3VdbeAddOp2(v, OP_Rewind, iEph, labelBreak); VdbeCoverage(v); - addrTop = sqlite3VdbeAddOp2(v, OP_RowKey, iEph, regKey); - sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelContinue, regKey, 0); - VdbeCoverage(v); - }else{ - labelContinue = sqlite3VdbeAddOp3(v, OP_RowSetRead, regRowSet, labelBreak, - regOldRowid); - VdbeCoverage(v); - sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, labelContinue, regOldRowid); - VdbeCoverage(v); } - /* If the record number will change, set register regNewRowid to - ** contain the new value. If the record number is not being modified, + /* If the rowid value will change, set register regNewRowid to + ** contain the new value. If the rowid is not being modified, ** then regNewRowid is the same register as regOldRowid, which is ** already populated. */ assert( chngKey || pTrigger || hasFK || regOldRowid==regNewRowid ); if( chngRowid ){ - sqlite3ExprCode(pParse, pRowidExpr, regNewRowid); + assert( iRowidExpr>=0 ); + if( nChangeFrom==0 ){ + sqlite3ExprCode(pParse, pRowidExpr, regNewRowid); + }else{ + sqlite3VdbeAddOp3(v, OP_Column, iEph, iRowidExpr, regNewRowid); + } sqlite3VdbeAddOp1(v, OP_MustBeInt, regNewRowid); VdbeCoverage(v); } @@ -474,21 +898,26 @@ void sqlite3Update( ** information is needed */ if( chngPk || hasFK || pTrigger ){ u32 oldmask = (hasFK ? sqlite3FkOldmask(pParse, pTab) : 0); - oldmask |= sqlite3TriggerColmask(pParse, + oldmask |= sqlite3TriggerColmask(pParse, pTrigger, pChanges, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onError ); for(i=0; i<pTab->nCol; i++){ + u32 colFlags = pTab->aCol[i].colFlags; + k = sqlite3TableColumnToStorage(pTab, i) + regOld; if( oldmask==0xffffffff || (i<32 && (oldmask & MASKBIT32(i))!=0) - || (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY)!=0 + || (colFlags & COLFLAG_PRIMKEY)!=0 ){ testcase( oldmask!=0xffffffff && i==31 ); - sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, regOld+i); + sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, k); }else{ - sqlite3VdbeAddOp2(v, OP_Null, 0, regOld+i); + sqlite3VdbeAddOp2(v, OP_Null, 0, k); } } if( chngRowid==0 && pPk==0 ){ +#ifdef SQLITE_ALLOW_ROWID_IN_VIEW + if( isView ) sqlite3VdbeAddOp2(v, OP_Null, 0, regOldRowid); +#endif sqlite3VdbeAddOp2(v, OP_Copy, regOldRowid, regNewRowid); } } @@ -501,79 +930,119 @@ void sqlite3Update( ** If there are one or more BEFORE triggers, then do not populate the ** registers associated with columns that are (a) not modified by ** this UPDATE statement and (b) not accessed by new.* references. The - ** values for registers not modified by the UPDATE must be reloaded from - ** the database after the BEFORE triggers are fired anyway (as the trigger + ** values for registers not modified by the UPDATE must be reloaded from + ** the database after the BEFORE triggers are fired anyway (as the trigger ** may have modified them). So not loading those that are not going to ** be used eliminates some redundant opcodes. */ newmask = sqlite3TriggerColmask( pParse, pTrigger, pChanges, 1, TRIGGER_BEFORE, pTab, onError ); - for(i=0; i<pTab->nCol; i++){ + for(i=0, k=regNew; i<pTab->nCol; i++, k++){ if( i==pTab->iPKey ){ - sqlite3VdbeAddOp2(v, OP_Null, 0, regNew+i); + sqlite3VdbeAddOp2(v, OP_Null, 0, k); + }else if( (pTab->aCol[i].colFlags & COLFLAG_GENERATED)!=0 ){ + if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ) k--; }else{ j = aXRef[i]; if( j>=0 ){ - sqlite3ExprCode(pParse, pChanges->a[j].pExpr, regNew+i); + if( nChangeFrom ){ + int nOff = (isView ? pTab->nCol : nPk); + assert( eOnePass==ONEPASS_OFF ); + sqlite3VdbeAddOp3(v, OP_Column, iEph, nOff+j, k); + }else{ + sqlite3ExprCode(pParse, pChanges->a[j].pExpr, k); + } }else if( 0==(tmask&TRIGGER_BEFORE) || i>31 || (newmask & MASKBIT32(i)) ){ - /* This branch loads the value of a column that will not be changed + /* This branch loads the value of a column that will not be changed ** into a register. This is done if there are no BEFORE triggers, or ** if there are one or more BEFORE triggers that use this value via ** a new.* reference in a trigger program. */ testcase( i==31 ); testcase( i==32 ); - sqlite3ExprCodeGetColumnToReg(pParse, pTab, i, iDataCur, regNew+i); + sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, k); + bFinishSeek = 0; }else{ - sqlite3VdbeAddOp2(v, OP_Null, 0, regNew+i); + sqlite3VdbeAddOp2(v, OP_Null, 0, k); } } } +#ifndef SQLITE_OMIT_GENERATED_COLUMNS + if( pTab->tabFlags & TF_HasGenerated ){ + testcase( pTab->tabFlags & TF_HasVirtual ); + testcase( pTab->tabFlags & TF_HasStored ); + sqlite3ComputeGeneratedColumns(pParse, regNew, pTab); + } +#endif /* Fire any BEFORE UPDATE triggers. This happens before constraints are ** verified. One could argue that this is wrong. */ if( tmask&TRIGGER_BEFORE ){ sqlite3TableAffinity(v, pTab, regNew); - sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges, + sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges, TRIGGER_BEFORE, pTab, regOldRowid, onError, labelContinue); - /* The row-trigger may have deleted the row being updated. In this - ** case, jump to the next row. No updates or AFTER triggers are - ** required. This behavior - what happens when the row being updated - ** is deleted or renamed by a BEFORE trigger - is left undefined in the - ** documentation. - */ - if( pPk ){ - sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelContinue,regKey,nKey); - VdbeCoverage(v); - }else{ - sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, labelContinue, regOldRowid); - VdbeCoverage(v); - } + if( !isView ){ + /* The row-trigger may have deleted the row being updated. In this + ** case, jump to the next row. No updates or AFTER triggers are + ** required. This behavior - what happens when the row being updated + ** is deleted or renamed by a BEFORE trigger - is left undefined in the + ** documentation. + */ + if( pPk ){ + sqlite3VdbeAddOp4Int(v, OP_NotFound,iDataCur,labelContinue,regKey,nKey); + VdbeCoverage(v); + }else{ + sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, labelContinue,regOldRowid); + VdbeCoverage(v); + } - /* If it did not delete it, the row-trigger may still have modified - ** some of the columns of the row being updated. Load the values for - ** all columns not modified by the update statement into their - ** registers in case this has happened. - */ - for(i=0; i<pTab->nCol; i++){ - if( aXRef[i]<0 && i!=pTab->iPKey ){ - sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, regNew+i); + /* After-BEFORE-trigger-reload-loop: + ** If it did not delete it, the BEFORE trigger may still have modified + ** some of the columns of the row being updated. Load the values for + ** all columns not modified by the update statement into their registers + ** in case this has happened. Only unmodified columns are reloaded. + ** The values computed for modified columns use the values before the + ** BEFORE trigger runs. See test case trigger1-18.0 (added 2018-04-26) + ** for an example. + */ + for(i=0, k=regNew; i<pTab->nCol; i++, k++){ + if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){ + if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ) k--; + }else if( aXRef[i]<0 && i!=pTab->iPKey ){ + sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, k); + } + } +#ifndef SQLITE_OMIT_GENERATED_COLUMNS + if( pTab->tabFlags & TF_HasGenerated ){ + testcase( pTab->tabFlags & TF_HasVirtual ); + testcase( pTab->tabFlags & TF_HasStored ); + sqlite3ComputeGeneratedColumns(pParse, regNew, pTab); } +#endif } } if( !isView ){ - int addr1 = 0; /* Address of jump instruction */ - int bReplace = 0; /* True if REPLACE conflict resolution might happen */ - /* Do constraint checks. */ assert( regOldRowid>0 ); sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur, regNewRowid, regOldRowid, chngKey, onError, labelContinue, &bReplace, - aXRef); + aXRef, 0); + + /* If REPLACE conflict handling may have been used, or if the PK of the + ** row is changing, then the GenerateConstraintChecks() above may have + ** moved cursor iDataCur. Reseek it. */ + if( bReplace || chngKey ){ + if( pPk ){ + sqlite3VdbeAddOp4Int(v, OP_NotFound,iDataCur,labelContinue,regKey,nKey); + }else{ + sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, labelContinue,regOldRowid); + } + VdbeCoverage(v); + } /* Do FK constraint checks. */ if( hasFK ){ @@ -581,88 +1050,103 @@ void sqlite3Update( } /* Delete the index entries associated with the current record. */ - if( bReplace || chngKey ){ - if( pPk ){ - addr1 = sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, 0, regKey, nKey); - }else{ - addr1 = sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, 0, regOldRowid); - } - VdbeCoverageNeverTaken(v); - } sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur, aRegIdx, -1); - - /* If changing the record number, delete the old record. */ - if( hasFK || chngKey || pPk!=0 ){ - sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, 0); + + /* We must run the OP_FinishSeek opcode to resolve a prior + ** OP_DeferredSeek if there is any possibility that there have been + ** no OP_Column opcodes since the OP_DeferredSeek was issued. But + ** we want to avoid the OP_FinishSeek if possible, as running it + ** costs CPU cycles. */ + if( bFinishSeek ){ + sqlite3VdbeAddOp1(v, OP_FinishSeek, iDataCur); } - if( bReplace || chngKey ){ - sqlite3VdbeJumpHere(v, addr1); + + /* If changing the rowid value, or if there are foreign key constraints + ** to process, delete the old record. Otherwise, add a noop OP_Delete + ** to invoke the pre-update hook. + ** + ** That (regNew==regnewRowid+1) is true is also important for the + ** pre-update hook. If the caller invokes preupdate_new(), the returned + ** value is copied from memory cell (regNewRowid+1+iCol), where iCol + ** is the column index supplied by the user. + */ + assert( regNew==regNewRowid+1 ); +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK + sqlite3VdbeAddOp3(v, OP_Delete, iDataCur, + OPFLAG_ISUPDATE | ((hasFK>1 || chngKey) ? 0 : OPFLAG_ISNOOP), + regNewRowid + ); + if( eOnePass==ONEPASS_MULTI ){ + assert( hasFK==0 && chngKey==0 ); + sqlite3VdbeChangeP5(v, OPFLAG_SAVEPOSITION); + } + if( !pParse->nested ){ + sqlite3VdbeAppendP4(v, pTab, P4_TABLE); } +#else + if( hasFK>1 || chngKey ){ + sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, 0); + } +#endif if( hasFK ){ sqlite3FkCheck(pParse, pTab, 0, regNewRowid, aXRef, chngKey); } - + /* Insert the new index entries and the new record. */ - sqlite3CompleteInsertion(pParse, pTab, iDataCur, iIdxCur, - regNewRowid, aRegIdx, 1, 0, 0); + sqlite3CompleteInsertion( + pParse, pTab, iDataCur, iIdxCur, regNewRowid, aRegIdx, + OPFLAG_ISUPDATE | (eOnePass==ONEPASS_MULTI ? OPFLAG_SAVEPOSITION : 0), + 0, 0 + ); /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to ** handle rows (possibly in other tables) that refer via a foreign key - ** to the row just updated. */ + ** to the row just updated. */ if( hasFK ){ sqlite3FkActions(pParse, pTab, pChanges, regOldRowid, aXRef, chngKey); } } - /* Increment the row counter + /* Increment the row counter */ - if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab){ + if( regRowCount ){ sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1); } - sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges, - TRIGGER_AFTER, pTab, regOldRowid, onError, labelContinue); + if( pTrigger ){ + sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges, + TRIGGER_AFTER, pTab, regOldRowid, onError, labelContinue); + } /* Repeat the above with the next record to be updated, until ** all record selected by the WHERE clause have been updated. */ - if( okOnePass ){ + if( eOnePass==ONEPASS_SINGLE ){ /* Nothing to do at end-of-loop for a single-pass */ - }else if( pPk ){ + }else if( eOnePass==ONEPASS_MULTI ){ sqlite3VdbeResolveLabel(v, labelContinue); - sqlite3VdbeAddOp2(v, OP_Next, iEph, addrTop); VdbeCoverage(v); + sqlite3WhereEnd(pWInfo); }else{ - sqlite3VdbeGoto(v, labelContinue); + sqlite3VdbeResolveLabel(v, labelContinue); + sqlite3VdbeAddOp2(v, OP_Next, iEph, addrTop); VdbeCoverage(v); } sqlite3VdbeResolveLabel(v, labelBreak); - /* Close all tables */ - for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ - assert( aRegIdx ); - if( aToOpen[i+1] ){ - sqlite3VdbeAddOp2(v, OP_Close, iIdxCur+i, 0); - } - } - if( iDataCur<iIdxCur ) sqlite3VdbeAddOp2(v, OP_Close, iDataCur, 0); - /* Update the sqlite_sequence table by storing the content of the ** maximum rowid counter values recorded while inserting into ** autoincrement tables. */ - if( pParse->nested==0 && pParse->pTriggerTab==0 ){ + if( pParse->nested==0 && pParse->pTriggerTab==0 && pUpsert==0 ){ sqlite3AutoincrementEnd(pParse); } /* - ** Return the number of rows that were changed. If this routine is - ** generating code because of a call to sqlite3NestedParse(), do not - ** invoke the callback function. + ** Return the number of rows that were changed, if we are tracking + ** that information. */ - if( (db->flags&SQLITE_CountRows) && !pParse->pTriggerTab && !pParse->nested ){ - sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1); - sqlite3VdbeSetNumCols(v, 1); - sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows updated", SQLITE_STATIC); + if( regRowCount ){ + sqlite3CodeChangeCount(v, regRowCount, "rows updated"); } update_cleanup: @@ -671,6 +1155,10 @@ void sqlite3Update( sqlite3SrcListDelete(db, pTabList); sqlite3ExprListDelete(db, pChanges); sqlite3ExprDelete(db, pWhere); +#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) + sqlite3ExprListDelete(db, pOrderBy); + sqlite3ExprDelete(db, pLimit); +#endif return; } /* Make sure "isView" and other macros defined above are undefined. Otherwise @@ -687,8 +1175,8 @@ void sqlite3Update( /* ** Generate code for an UPDATE of a virtual table. ** -** There are two possible strategies - the default and the special -** "onepass" strategy. Onepass is only used if the virtual table +** There are two possible strategies - the default and the special +** "onepass" strategy. Onepass is only used if the virtual table ** implementation indicates that pWhere may match at most one row. ** ** The default strategy is to create an ephemeral table that contains @@ -720,17 +1208,17 @@ static void updateVirtualTable( int i; /* Loop counter */ sqlite3 *db = pParse->db; /* Database connection */ const char *pVTab = (const char*)sqlite3GetVTable(db, pTab); - WhereInfo *pWInfo; + WhereInfo *pWInfo = 0; int nArg = 2 + pTab->nCol; /* Number of arguments to VUpdate */ int regArg; /* First register in VUpdate arg array */ int regRec; /* Register in which to assemble record */ - int regRowid; /* Register for ephem table rowid */ + int regRowid; /* Register for ephemeral table rowid */ int iCsr = pSrc->a[0].iCursor; /* Cursor used for virtual table scan */ int aDummy[2]; /* Unused arg for sqlite3WhereOkOnePass() */ - int bOnePass; /* True to use onepass strategy */ + int eOnePass; /* True to use onepass strategy */ int addr; /* Address of OP_OpenEphemeral */ - /* Allocate nArg registers to martial the arguments to VUpdate. Then + /* Allocate nArg registers in which to gather the arguments for VUpdate. Then ** create and open the ephemeral table in which the records created from ** these arguments will be temporarily stored. */ assert( v ); @@ -738,55 +1226,119 @@ static void updateVirtualTable( addr= sqlite3VdbeAddOp2(v, OP_OpenEphemeral, ephemTab, nArg); regArg = pParse->nMem + 1; pParse->nMem += nArg; - regRec = ++pParse->nMem; - regRowid = ++pParse->nMem; + if( pSrc->nSrc>1 ){ + Index *pPk = 0; + Expr *pRow; + ExprList *pList; + if( HasRowid(pTab) ){ + if( pRowid ){ + pRow = sqlite3ExprDup(db, pRowid, 0); + }else{ + pRow = sqlite3PExpr(pParse, TK_ROW, 0, 0); + } + }else{ + i16 iPk; /* PRIMARY KEY column */ + pPk = sqlite3PrimaryKeyIndex(pTab); + assert( pPk!=0 ); + assert( pPk->nKeyCol==1 ); + iPk = pPk->aiColumn[0]; + if( aXRef[iPk]>=0 ){ + pRow = sqlite3ExprDup(db, pChanges->a[aXRef[iPk]].pExpr, 0); + }else{ + pRow = exprRowColumn(pParse, iPk); + } + } + pList = sqlite3ExprListAppend(pParse, 0, pRow); - /* Start scanning the virtual table */ - pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0,0,WHERE_ONEPASS_DESIRED,0); - if( pWInfo==0 ) return; + for(i=0; i<pTab->nCol; i++){ + if( aXRef[i]>=0 ){ + pList = sqlite3ExprListAppend(pParse, pList, + sqlite3ExprDup(db, pChanges->a[aXRef[i]].pExpr, 0) + ); + }else{ + Expr *pRowExpr = exprRowColumn(pParse, i); + if( pRowExpr ) pRowExpr->op2 = OPFLAG_NOCHNG; + pList = sqlite3ExprListAppend(pParse, pList, pRowExpr); + } + } - /* Populate the argument registers. */ - sqlite3VdbeAddOp2(v, OP_Rowid, iCsr, regArg); - if( pRowid ){ - sqlite3ExprCode(pParse, pRowid, regArg+1); + updateFromSelect(pParse, ephemTab, pPk, pList, pSrc, pWhere, 0, 0); + sqlite3ExprListDelete(db, pList); + eOnePass = ONEPASS_OFF; }else{ - sqlite3VdbeAddOp2(v, OP_Rowid, iCsr, regArg+1); - } - for(i=0; i<pTab->nCol; i++){ - if( aXRef[i]>=0 ){ - sqlite3ExprCode(pParse, pChanges->a[aXRef[i]].pExpr, regArg+2+i); + regRec = ++pParse->nMem; + regRowid = ++pParse->nMem; + + /* Start scanning the virtual table */ + pWInfo = sqlite3WhereBegin( + pParse, pSrc, pWhere, 0, 0, 0, WHERE_ONEPASS_DESIRED, 0 + ); + if( pWInfo==0 ) return; + + /* Populate the argument registers. */ + for(i=0; i<pTab->nCol; i++){ + assert( (pTab->aCol[i].colFlags & COLFLAG_GENERATED)==0 ); + if( aXRef[i]>=0 ){ + sqlite3ExprCode(pParse, pChanges->a[aXRef[i]].pExpr, regArg+2+i); + }else{ + sqlite3VdbeAddOp3(v, OP_VColumn, iCsr, i, regArg+2+i); + sqlite3VdbeChangeP5(v, OPFLAG_NOCHNG);/* For sqlite3_vtab_nochange() */ + } + } + if( HasRowid(pTab) ){ + sqlite3VdbeAddOp2(v, OP_Rowid, iCsr, regArg); + if( pRowid ){ + sqlite3ExprCode(pParse, pRowid, regArg+1); + }else{ + sqlite3VdbeAddOp2(v, OP_Rowid, iCsr, regArg+1); + } }else{ - sqlite3VdbeAddOp3(v, OP_VColumn, iCsr, i, regArg+2+i); + Index *pPk; /* PRIMARY KEY index */ + i16 iPk; /* PRIMARY KEY column */ + pPk = sqlite3PrimaryKeyIndex(pTab); + assert( pPk!=0 ); + assert( pPk->nKeyCol==1 ); + iPk = pPk->aiColumn[0]; + sqlite3VdbeAddOp3(v, OP_VColumn, iCsr, iPk, regArg); + sqlite3VdbeAddOp2(v, OP_SCopy, regArg+2+iPk, regArg+1); } - } - bOnePass = sqlite3WhereOkOnePass(pWInfo, aDummy); + eOnePass = sqlite3WhereOkOnePass(pWInfo, aDummy); + + /* There is no ONEPASS_MULTI on virtual tables */ + assert( eOnePass==ONEPASS_OFF || eOnePass==ONEPASS_SINGLE ); - if( bOnePass ){ - /* If using the onepass strategy, no-op out the OP_OpenEphemeral coded - ** above. Also, if this is a top-level parse (not a trigger), clear the - ** multi-write flag so that the VM does not open a statement journal */ - sqlite3VdbeChangeToNoop(v, addr); - if( sqlite3IsToplevel(pParse) ){ - pParse->isMultiWrite = 0; + if( eOnePass ){ + /* If using the onepass strategy, no-op out the OP_OpenEphemeral coded + ** above. */ + sqlite3VdbeChangeToNoop(v, addr); + sqlite3VdbeAddOp1(v, OP_Close, iCsr); + }else{ + /* Create a record from the argument register contents and insert it into + ** the ephemeral table. */ + sqlite3MultiWrite(pParse); + sqlite3VdbeAddOp3(v, OP_MakeRecord, regArg, nArg, regRec); +#if defined(SQLITE_DEBUG) && !defined(SQLITE_ENABLE_NULL_TRIM) + /* Signal an assert() within OP_MakeRecord that it is allowed to + ** accept no-change records with serial_type 10 */ + sqlite3VdbeChangeP5(v, OPFLAG_NOCHNG_MAGIC); +#endif + sqlite3VdbeAddOp2(v, OP_NewRowid, ephemTab, regRowid); + sqlite3VdbeAddOp3(v, OP_Insert, ephemTab, regRec, regRowid); } - }else{ - /* Create a record from the argument register contents and insert it into - ** the ephemeral table. */ - sqlite3VdbeAddOp3(v, OP_MakeRecord, regArg, nArg, regRec); - sqlite3VdbeAddOp2(v, OP_NewRowid, ephemTab, regRowid); - sqlite3VdbeAddOp3(v, OP_Insert, ephemTab, regRec, regRowid); } - if( bOnePass==0 ){ + if( eOnePass==ONEPASS_OFF ){ /* End the virtual table scan */ - sqlite3WhereEnd(pWInfo); + if( pSrc->nSrc==1 ){ + sqlite3WhereEnd(pWInfo); + } - /* Begin scannning through the ephemeral table. */ + /* Begin scanning through the ephemeral table. */ addr = sqlite3VdbeAddOp1(v, OP_Rewind, ephemTab); VdbeCoverage(v); - /* Extract arguments from the current row of the ephemeral table and + /* Extract arguments from the current row of the ephemeral table and ** invoke the VUpdate method. */ for(i=0; i<nArg; i++){ sqlite3VdbeAddOp3(v, OP_Column, ephemTab, i, regArg+i); @@ -799,7 +1351,7 @@ static void updateVirtualTable( /* End of the ephemeral table scan. Or, if using the onepass strategy, ** jump to here if the scan visited zero rows. */ - if( bOnePass==0 ){ + if( eOnePass==ONEPASS_OFF ){ sqlite3VdbeAddOp2(v, OP_Next, ephemTab, addr+1); VdbeCoverage(v); sqlite3VdbeJumpHere(v, addr); sqlite3VdbeAddOp2(v, OP_Close, ephemTab, 0); diff --git a/src/upsert.c b/src/upsert.c new file mode 100644 index 0000000000..82295d52ae --- /dev/null +++ b/src/upsert.c @@ -0,0 +1,329 @@ +/* +** 2018-04-12 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains code to implement various aspects of UPSERT +** processing and handling of the Upsert object. +*/ +#include "sqliteInt.h" + +#ifndef SQLITE_OMIT_UPSERT +/* +** Free a list of Upsert objects +*/ +static void SQLITE_NOINLINE upsertDelete(sqlite3 *db, Upsert *p){ + do{ + Upsert *pNext = p->pNextUpsert; + sqlite3ExprListDelete(db, p->pUpsertTarget); + sqlite3ExprDelete(db, p->pUpsertTargetWhere); + sqlite3ExprListDelete(db, p->pUpsertSet); + sqlite3ExprDelete(db, p->pUpsertWhere); + sqlite3DbFree(db, p->pToFree); + sqlite3DbFree(db, p); + p = pNext; + }while( p ); +} +void sqlite3UpsertDelete(sqlite3 *db, Upsert *p){ + if( p ) upsertDelete(db, p); +} + + +/* +** Duplicate an Upsert object. +*/ +Upsert *sqlite3UpsertDup(sqlite3 *db, Upsert *p){ + if( p==0 ) return 0; + return sqlite3UpsertNew(db, + sqlite3ExprListDup(db, p->pUpsertTarget, 0), + sqlite3ExprDup(db, p->pUpsertTargetWhere, 0), + sqlite3ExprListDup(db, p->pUpsertSet, 0), + sqlite3ExprDup(db, p->pUpsertWhere, 0), + sqlite3UpsertDup(db, p->pNextUpsert) + ); +} + +/* +** Create a new Upsert object. +*/ +Upsert *sqlite3UpsertNew( + sqlite3 *db, /* Determines which memory allocator to use */ + ExprList *pTarget, /* Target argument to ON CONFLICT, or NULL */ + Expr *pTargetWhere, /* Optional WHERE clause on the target */ + ExprList *pSet, /* UPDATE columns, or NULL for a DO NOTHING */ + Expr *pWhere, /* WHERE clause for the ON CONFLICT UPDATE */ + Upsert *pNext /* Next ON CONFLICT clause in the list */ +){ + Upsert *pNew; + pNew = sqlite3DbMallocZero(db, sizeof(Upsert)); + if( pNew==0 ){ + sqlite3ExprListDelete(db, pTarget); + sqlite3ExprDelete(db, pTargetWhere); + sqlite3ExprListDelete(db, pSet); + sqlite3ExprDelete(db, pWhere); + sqlite3UpsertDelete(db, pNext); + return 0; + }else{ + pNew->pUpsertTarget = pTarget; + pNew->pUpsertTargetWhere = pTargetWhere; + pNew->pUpsertSet = pSet; + pNew->pUpsertWhere = pWhere; + pNew->isDoUpdate = pSet!=0; + pNew->pNextUpsert = pNext; + } + return pNew; +} + +/* +** Analyze the ON CONFLICT clause described by pUpsert. Resolve all +** symbols in the conflict-target. +** +** Return SQLITE_OK if everything works, or an error code is something +** is wrong. +*/ +int sqlite3UpsertAnalyzeTarget( + Parse *pParse, /* The parsing context */ + SrcList *pTabList, /* Table into which we are inserting */ + Upsert *pUpsert, /* The ON CONFLICT clauses */ + Upsert *pAll /* Complete list of all ON CONFLICT clauses */ +){ + Table *pTab; /* That table into which we are inserting */ + int rc; /* Result code */ + int iCursor; /* Cursor used by pTab */ + Index *pIdx; /* One of the indexes of pTab */ + ExprList *pTarget; /* The conflict-target clause */ + Expr *pTerm; /* One term of the conflict-target clause */ + NameContext sNC; /* Context for resolving symbolic names */ + Expr sCol[2]; /* Index column converted into an Expr */ + int nClause = 0; /* Counter of ON CONFLICT clauses */ + + assert( pTabList->nSrc==1 ); + assert( pTabList->a[0].pSTab!=0 ); + assert( pUpsert!=0 ); + assert( pUpsert->pUpsertTarget!=0 ); + + /* Resolve all symbolic names in the conflict-target clause, which + ** includes both the list of columns and the optional partial-index + ** WHERE clause. + */ + memset(&sNC, 0, sizeof(sNC)); + sNC.pParse = pParse; + sNC.pSrcList = pTabList; + for(; pUpsert && pUpsert->pUpsertTarget; + pUpsert=pUpsert->pNextUpsert, nClause++){ + rc = sqlite3ResolveExprListNames(&sNC, pUpsert->pUpsertTarget); + if( rc ) return rc; + rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertTargetWhere); + if( rc ) return rc; + + /* Check to see if the conflict target matches the rowid. */ + pTab = pTabList->a[0].pSTab; + pTarget = pUpsert->pUpsertTarget; + iCursor = pTabList->a[0].iCursor; + if( HasRowid(pTab) + && pTarget->nExpr==1 + && (pTerm = pTarget->a[0].pExpr)->op==TK_COLUMN + && pTerm->iColumn==XN_ROWID + ){ + /* The conflict-target is the rowid of the primary table */ + assert( pUpsert->pUpsertIdx==0 ); + continue; + } + + /* Initialize sCol[0..1] to be an expression parse tree for a + ** single column of an index. The sCol[0] node will be the TK_COLLATE + ** operator and sCol[1] will be the TK_COLUMN operator. Code below + ** will populate the specific collation and column number values + ** prior to comparing against the conflict-target expression. + */ + memset(sCol, 0, sizeof(sCol)); + sCol[0].op = TK_COLLATE; + sCol[0].pLeft = &sCol[1]; + sCol[1].op = TK_COLUMN; + sCol[1].iTable = pTabList->a[0].iCursor; + + /* Check for matches against other indexes */ + for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ + int ii, jj, nn; + if( !IsUniqueIndex(pIdx) ) continue; + if( pTarget->nExpr!=pIdx->nKeyCol ) continue; + if( pIdx->pPartIdxWhere ){ + if( pUpsert->pUpsertTargetWhere==0 ) continue; + if( sqlite3ExprCompare(pParse, pUpsert->pUpsertTargetWhere, + pIdx->pPartIdxWhere, iCursor)!=0 ){ + continue; + } + } + nn = pIdx->nKeyCol; + for(ii=0; ii<nn; ii++){ + Expr *pExpr; + sCol[0].u.zToken = (char*)pIdx->azColl[ii]; + if( pIdx->aiColumn[ii]==XN_EXPR ){ + assert( pIdx->aColExpr!=0 ); + assert( pIdx->aColExpr->nExpr>ii ); + assert( pIdx->bHasExpr ); + pExpr = pIdx->aColExpr->a[ii].pExpr; + if( pExpr->op!=TK_COLLATE ){ + sCol[0].pLeft = pExpr; + pExpr = &sCol[0]; + } + }else{ + sCol[0].pLeft = &sCol[1]; + sCol[1].iColumn = pIdx->aiColumn[ii]; + pExpr = &sCol[0]; + } + for(jj=0; jj<nn; jj++){ + if( sqlite3ExprCompare(0,pTarget->a[jj].pExpr,pExpr,iCursor)<2 ){ + break; /* Column ii of the index matches column jj of target */ + } + } + if( jj>=nn ){ + /* The target contains no match for column jj of the index */ + break; + } + } + if( ii<nn ){ + /* Column ii of the index did not match any term of the conflict target. + ** Continue the search with the next index. */ + continue; + } + pUpsert->pUpsertIdx = pIdx; + if( sqlite3UpsertOfIndex(pAll,pIdx)!=pUpsert ){ + /* Really this should be an error. The isDup ON CONFLICT clause will + ** never fire. But this problem was not discovered until three years + ** after multi-CONFLICT upsert was added, and so we silently ignore + ** the problem to prevent breaking applications that might actually + ** have redundant ON CONFLICT clauses. */ + pUpsert->isDup = 1; + } + break; + } + if( pUpsert->pUpsertIdx==0 ){ + char zWhich[16]; + if( nClause==0 && pUpsert->pNextUpsert==0 ){ + zWhich[0] = 0; + }else{ + sqlite3_snprintf(sizeof(zWhich),zWhich,"%r ", nClause+1); + } + sqlite3ErrorMsg(pParse, "%sON CONFLICT clause does not match any " + "PRIMARY KEY or UNIQUE constraint", zWhich); + return SQLITE_ERROR; + } + } + return SQLITE_OK; +} + +/* +** Return true if pUpsert is the last ON CONFLICT clause with a +** conflict target, or if pUpsert is followed by another ON CONFLICT +** clause that targets the INTEGER PRIMARY KEY. +*/ +int sqlite3UpsertNextIsIPK(Upsert *pUpsert){ + Upsert *pNext; + if( NEVER(pUpsert==0) ) return 0; + pNext = pUpsert->pNextUpsert; + while( 1 /*exit-by-return*/ ){ + if( pNext==0 ) return 1; + if( pNext->pUpsertTarget==0 ) return 1; + if( pNext->pUpsertIdx==0 ) return 1; + if( !pNext->isDup ) return 0; + pNext = pNext->pNextUpsert; + } + return 0; +} + +/* +** Given the list of ON CONFLICT clauses described by pUpsert, and +** a particular index pIdx, return a pointer to the particular ON CONFLICT +** clause that applies to the index. Or, if the index is not subject to +** any ON CONFLICT clause, return NULL. +*/ +Upsert *sqlite3UpsertOfIndex(Upsert *pUpsert, Index *pIdx){ + while( + pUpsert + && pUpsert->pUpsertTarget!=0 + && pUpsert->pUpsertIdx!=pIdx + ){ + pUpsert = pUpsert->pNextUpsert; + } + return pUpsert; +} + +/* +** Generate bytecode that does an UPDATE as part of an upsert. +** +** If pIdx is NULL, then the UNIQUE constraint that failed was the IPK. +** In this case parameter iCur is a cursor open on the table b-tree that +** currently points to the conflicting table row. Otherwise, if pIdx +** is not NULL, then pIdx is the constraint that failed and iCur is a +** cursor points to the conflicting row. +*/ +void sqlite3UpsertDoUpdate( + Parse *pParse, /* The parsing and code-generating context */ + Upsert *pUpsert, /* The ON CONFLICT clause for the upsert */ + Table *pTab, /* The table being updated */ + Index *pIdx, /* The UNIQUE constraint that failed */ + int iCur /* Cursor for pIdx (or pTab if pIdx==NULL) */ +){ + Vdbe *v = pParse->pVdbe; + sqlite3 *db = pParse->db; + SrcList *pSrc; /* FROM clause for the UPDATE */ + int iDataCur; + int i; + Upsert *pTop = pUpsert; + + assert( v!=0 ); + assert( pUpsert!=0 ); + iDataCur = pUpsert->iDataCur; + pUpsert = sqlite3UpsertOfIndex(pTop, pIdx); + VdbeNoopComment((v, "Begin DO UPDATE of UPSERT")); + if( pIdx && iCur!=iDataCur ){ + if( HasRowid(pTab) ){ + int regRowid = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp2(v, OP_IdxRowid, iCur, regRowid); + sqlite3VdbeAddOp3(v, OP_SeekRowid, iDataCur, 0, regRowid); + VdbeCoverage(v); + sqlite3ReleaseTempReg(pParse, regRowid); + }else{ + Index *pPk = sqlite3PrimaryKeyIndex(pTab); + int nPk = pPk->nKeyCol; + int iPk = pParse->nMem+1; + pParse->nMem += nPk; + for(i=0; i<nPk; i++){ + int k; + assert( pPk->aiColumn[i]>=0 ); + k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[i]); + sqlite3VdbeAddOp3(v, OP_Column, iCur, k, iPk+i); + VdbeComment((v, "%s.%s", pIdx->zName, + pTab->aCol[pPk->aiColumn[i]].zCnName)); + } + sqlite3VdbeVerifyAbortable(v, OE_Abort); + i = sqlite3VdbeAddOp4Int(v, OP_Found, iDataCur, 0, iPk, nPk); + VdbeCoverage(v); + sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CORRUPT, OE_Abort, 0, + "corrupt database", P4_STATIC); + sqlite3MayAbort(pParse); + sqlite3VdbeJumpHere(v, i); + } + } + /* pUpsert does not own pTop->pUpsertSrc - the outer INSERT statement does. + ** So we have to make a copy before passing it down into sqlite3Update() */ + pSrc = sqlite3SrcListDup(db, pTop->pUpsertSrc, 0); + /* excluded.* columns of type REAL need to be converted to a hard real */ + for(i=0; i<pTab->nCol; i++){ + if( pTab->aCol[i].affinity==SQLITE_AFF_REAL ){ + sqlite3VdbeAddOp1(v, OP_RealAffinity, pTop->regData+i); + } + } + sqlite3Update(pParse, pSrc, sqlite3ExprListDup(db,pUpsert->pUpsertSet,0), + sqlite3ExprDup(db,pUpsert->pUpsertWhere,0), OE_Abort, 0, 0, pUpsert); + VdbeNoopComment((v, "End DO UPDATE of UPSERT")); +} + +#endif /* SQLITE_OMIT_UPSERT */ diff --git a/src/utf.c b/src/utf.c index e42ab418ad..2efcd67912 100644 --- a/src/utf.c +++ b/src/utf.c @@ -105,24 +105,33 @@ static const unsigned char sqlite3Utf8Trans1[] = { } \ } -#define READ_UTF16LE(zIn, TERM, c){ \ - c = (*zIn++); \ - c += ((*zIn++)<<8); \ - if( c>=0xD800 && c<0xE000 && TERM ){ \ - int c2 = (*zIn++); \ - c2 += ((*zIn++)<<8); \ - c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10); \ - } \ -} - -#define READ_UTF16BE(zIn, TERM, c){ \ - c = ((*zIn++)<<8); \ - c += (*zIn++); \ - if( c>=0xD800 && c<0xE000 && TERM ){ \ - int c2 = ((*zIn++)<<8); \ - c2 += (*zIn++); \ - c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10); \ - } \ +/* +** Write a single UTF8 character whose value is v into the +** buffer starting at zOut. zOut must be sized to hold at +** least four bytes. Return the number of bytes needed +** to encode the new character. +*/ +int sqlite3AppendOneUtf8Character(char *zOut, u32 v){ + if( v<0x00080 ){ + zOut[0] = (u8)(v & 0xff); + return 1; + } + if( v<0x00800 ){ + zOut[0] = 0xc0 + (u8)((v>>6) & 0x1f); + zOut[1] = 0x80 + (u8)(v & 0x3f); + return 2; + } + if( v<0x10000 ){ + zOut[0] = 0xe0 + (u8)((v>>12) & 0x0f); + zOut[1] = 0x80 + (u8)((v>>6) & 0x3f); + zOut[2] = 0x80 + (u8)(v & 0x3f); + return 3; + } + zOut[0] = 0xf0 + (u8)((v>>18) & 0x07); + zOut[1] = 0x80 + (u8)((v>>12) & 0x3f); + zOut[2] = 0x80 + (u8)((v>>6) & 0x3f); + zOut[3] = 0x80 + (u8)(v & 0x3f); + return 4; } /* @@ -156,7 +165,7 @@ static const unsigned char sqlite3Utf8Trans1[] = { c = *(zIn++); \ if( c>=0xc0 ){ \ c = sqlite3Utf8Trans1[c-0xc0]; \ - while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){ \ + while( zIn<zTerm && (*zIn & 0xc0)==0x80 ){ \ c = (c<<6) + (0x3f & *(zIn++)); \ } \ if( c<0x80 \ @@ -184,7 +193,38 @@ u32 sqlite3Utf8Read( return c; } - +/* +** Read a single UTF8 character out of buffer z[], but reading no +** more than n characters from the buffer. z[] is not zero-terminated. +** +** Return the number of bytes used to construct the character. +** +** Invalid UTF8 might generate a strange result. No effort is made +** to detect invalid UTF8. +** +** At most 4 bytes will be read out of z[]. The return value will always +** be between 1 and 4. +*/ +int sqlite3Utf8ReadLimited( + const u8 *z, + int n, + u32 *piOut +){ + u32 c; + int i = 1; + assert( n>0 ); + c = z[0]; + if( c>=0xc0 ){ + c = sqlite3Utf8Trans1[c-0xc0]; + if( n>4 ) n = 4; + while( i<n && (z[i] & 0xc0)==0x80 ){ + c = (c<<6) + (0x3f & z[i]); + i++; + } + } + *piOut = c; + return i; +} /* @@ -200,11 +240,11 @@ u32 sqlite3Utf8Read( ** encoding, or if *pMem does not contain a string value. */ SQLITE_NOINLINE int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){ - int len; /* Maximum length of output string in bytes */ - unsigned char *zOut; /* Output buffer */ - unsigned char *zIn; /* Input iterator */ - unsigned char *zTerm; /* End of input */ - unsigned char *z; /* Output iterator */ + sqlite3_int64 len; /* Maximum length of output string in bytes */ + unsigned char *zOut; /* Output buffer */ + unsigned char *zIn; /* Input iterator */ + unsigned char *zTerm; /* End of input */ + unsigned char *z; /* Output iterator */ unsigned int c; assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); @@ -215,9 +255,11 @@ SQLITE_NOINLINE int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){ #if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG) { - char zBuf[100]; - sqlite3VdbeMemPrettyPrint(pMem, zBuf); - fprintf(stderr, "INPUT: %s\n", zBuf); + StrAccum acc; + char zBuf[1000]; + sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0); + sqlite3VdbeMemPrettyPrint(pMem, &acc); + fprintf(stderr, "INPUT: %s\n", sqlite3StrAccumFinish(&acc)); } #endif @@ -231,7 +273,7 @@ SQLITE_NOINLINE int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){ rc = sqlite3VdbeMemMakeWriteable(pMem); if( rc!=SQLITE_OK ){ assert( rc==SQLITE_NOMEM ); - return SQLITE_NOMEM; + return SQLITE_NOMEM_BKPT; } zIn = (u8*)pMem->z; zTerm = &zIn[pMem->n&~1]; @@ -253,14 +295,14 @@ SQLITE_NOINLINE int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){ ** nul-terminator. */ pMem->n &= ~1; - len = pMem->n * 2 + 1; + len = 2 * (sqlite3_int64)pMem->n + 1; }else{ /* When converting from UTF-8 to UTF-16 the maximum growth is caused ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16 ** character. Two bytes are required in the output buffer for the ** nul-terminator. */ - len = pMem->n * 2 + 2; + len = 2 * (sqlite3_int64)pMem->n + 2; } /* Set zIn to point at the start of the input buffer and zTerm to point 1 @@ -273,7 +315,7 @@ SQLITE_NOINLINE int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){ zTerm = &zIn[pMem->n]; zOut = sqlite3DbMallocRaw(pMem->db, len); if( !zOut ){ - return SQLITE_NOMEM; + return SQLITE_NOMEM_BKPT; } z = zOut; @@ -299,13 +341,59 @@ SQLITE_NOINLINE int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){ if( pMem->enc==SQLITE_UTF16LE ){ /* UTF-16 Little-endian -> UTF-8 */ while( zIn<zTerm ){ - READ_UTF16LE(zIn, zIn<zTerm, c); + c = *(zIn++); + c += (*(zIn++))<<8; + if( c>=0xd800 && c<0xe000 ){ +#ifdef SQLITE_REPLACE_INVALID_UTF + if( c>=0xdc00 || zIn>=zTerm ){ + c = 0xfffd; + }else{ + int c2 = *(zIn++); + c2 += (*(zIn++))<<8; + if( c2<0xdc00 || c2>=0xe000 ){ + zIn -= 2; + c = 0xfffd; + }else{ + c = ((c&0x3ff)<<10) + (c2&0x3ff) + 0x10000; + } + } +#else + if( zIn<zTerm ){ + int c2 = (*zIn++); + c2 += ((*zIn++)<<8); + c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10); + } +#endif + } WRITE_UTF8(z, c); } }else{ /* UTF-16 Big-endian -> UTF-8 */ while( zIn<zTerm ){ - READ_UTF16BE(zIn, zIn<zTerm, c); + c = (*(zIn++))<<8; + c += *(zIn++); + if( c>=0xd800 && c<0xe000 ){ +#ifdef SQLITE_REPLACE_INVALID_UTF + if( c>=0xdc00 || zIn>=zTerm ){ + c = 0xfffd; + }else{ + int c2 = (*(zIn++))<<8; + c2 += *(zIn++); + if( c2<0xdc00 || c2>=0xe000 ){ + zIn -= 2; + c = 0xfffd; + }else{ + c = ((c&0x3ff)<<10) + (c2&0x3ff) + 0x10000; + } + } +#else + if( zIn<zTerm ){ + int c2 = ((*zIn++)<<8); + c2 += (*zIn++); + c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10); + } +#endif + } WRITE_UTF8(z, c); } } @@ -314,9 +402,9 @@ SQLITE_NOINLINE int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){ *z = 0; assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len ); - c = pMem->flags; + c = MEM_Str|MEM_Term|(pMem->flags&(MEM_AffMask|MEM_Subtype)); sqlite3VdbeMemRelease(pMem); - pMem->flags = MEM_Str|MEM_Term|(c&(MEM_AffMask|MEM_Subtype)); + pMem->flags = c; pMem->enc = desiredEnc; pMem->z = (char*)zOut; pMem->zMalloc = pMem->z; @@ -325,14 +413,18 @@ SQLITE_NOINLINE int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){ translate_out: #if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG) { - char zBuf[100]; - sqlite3VdbeMemPrettyPrint(pMem, zBuf); - fprintf(stderr, "OUTPUT: %s\n", zBuf); + StrAccum acc; + char zBuf[1000]; + sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0); + sqlite3VdbeMemPrettyPrint(pMem, &acc); + fprintf(stderr, "OUTPUT: %s\n", sqlite3StrAccumFinish(&acc)); } #endif return SQLITE_OK; } +#endif /* SQLITE_OMIT_UTF16 */ +#ifndef SQLITE_OMIT_UTF16 /* ** This routine checks for a byte-order mark at the beginning of the ** UTF-16 string stored in *pMem. If one is present, it is removed and @@ -451,27 +543,26 @@ char *sqlite3Utf16to8(sqlite3 *db, const void *z, int nByte, u8 enc){ } /* -** zIn is a UTF-16 encoded unicode string at least nChar characters long. +** zIn is a UTF-16 encoded unicode string at least nByte bytes long. ** Return the number of bytes in the first nChar unicode characters -** in pZ. nChar must be non-negative. +** in pZ. nChar must be non-negative. Surrogate pairs count as a single +** character. */ -int sqlite3Utf16ByteLen(const void *zIn, int nChar){ +int sqlite3Utf16ByteLen(const void *zIn, int nByte, int nChar){ int c; unsigned char const *z = zIn; + unsigned char const *zEnd = &z[nByte-1]; int n = 0; - if( SQLITE_UTF16NATIVE==SQLITE_UTF16BE ){ - while( n<nChar ){ - READ_UTF16BE(z, 1, c); - n++; - } - }else{ - while( n<nChar ){ - READ_UTF16LE(z, 1, c); - n++; - } + if( SQLITE_UTF16NATIVE==SQLITE_UTF16LE ) z++; + while( n<nChar && z<=zEnd ){ + c = z[0]; + z += 2; + if( c>=0xd8 && c<0xdc && z<=zEnd && z[0]>=0xdc && z[0]<0xe0 ) z += 2; + n++; } - return (int)(z-(unsigned char const *)zIn); + return (int)(z-(unsigned char const *)zIn) + - (SQLITE_UTF16NATIVE==SQLITE_UTF16LE); } #if defined(SQLITE_TEST) @@ -501,30 +592,6 @@ void sqlite3UtfSelfTest(void){ assert( c==t ); assert( (z-zBuf)==n ); } - for(i=0; i<0x00110000; i++){ - if( i>=0xD800 && i<0xE000 ) continue; - z = zBuf; - WRITE_UTF16LE(z, i); - n = (int)(z-zBuf); - assert( n>0 && n<=4 ); - z[0] = 0; - z = zBuf; - READ_UTF16LE(z, 1, c); - assert( c==i ); - assert( (z-zBuf)==n ); - } - for(i=0; i<0x00110000; i++){ - if( i>=0xD800 && i<0xE000 ) continue; - z = zBuf; - WRITE_UTF16BE(z, i); - n = (int)(z-zBuf); - assert( n>0 && n<=4 ); - z[0] = 0; - z = zBuf; - READ_UTF16BE(z, 1, c); - assert( c==i ); - assert( (z-zBuf)==n ); - } } #endif /* SQLITE_TEST */ #endif /* SQLITE_OMIT_UTF16 */ diff --git a/src/util.c b/src/util.c index 37b585b2ea..8f3e75846b 100644 --- a/src/util.c +++ b/src/util.c @@ -17,32 +17,30 @@ */ #include "sqliteInt.h" #include <stdarg.h> -#if HAVE_ISNAN || SQLITE_HAVE_ISNAN -# include <math.h> -#endif - -/* -** Routine needed to support the testcase() macro. -*/ -#ifdef SQLITE_COVERAGE_TEST -void sqlite3Coverage(int x){ - static unsigned dummy = 0; - dummy += (unsigned)x; -} +#ifndef SQLITE_OMIT_FLOATING_POINT +#include <math.h> #endif /* -** Give a callback to the test harness that can be used to simulate faults -** in places where it is difficult or expensive to do so purely by means -** of inputs. +** Calls to sqlite3FaultSim() are used to simulate a failure during testing, +** or to bypass normal error detection during testing in order to let +** execute proceed further downstream. +** +** In deployment, sqlite3FaultSim() *always* return SQLITE_OK (0). The +** sqlite3FaultSim() function only returns non-zero during testing. ** -** The intent of the integer argument is to let the fault simulator know -** which of multiple sqlite3FaultSim() calls has been hit. +** During testing, if the test harness has set a fault-sim callback using +** a call to sqlite3_test_control(SQLITE_TESTCTRL_FAULT_INSTALL), then +** each call to sqlite3FaultSim() is relayed to that application-supplied +** callback and the integer return value form the application-supplied +** callback is returned by sqlite3FaultSim(). ** -** Return whatever integer value the test callback returns, or return -** SQLITE_OK if no test callback is installed. +** The integer argument to sqlite3FaultSim() is a code to identify which +** sqlite3FaultSim() instance is being invoked. Each call to sqlite3FaultSim() +** should have a unique code. To prevent legacy testing applications from +** breaking, the codes should not be changed or reused. */ -#ifndef SQLITE_OMIT_BUILTIN_TEST +#ifndef SQLITE_UNTESTABLE int sqlite3FaultSim(int iTest){ int (*xCallback)(int) = sqlite3GlobalConfig.xTestCallback; return xCallback ? xCallback(iTest) : SQLITE_OK; @@ -59,36 +57,10 @@ int sqlite3FaultSim(int iTest){ int sqlite3IsNaN(double x){ int rc; /* The value return */ #if !SQLITE_HAVE_ISNAN && !HAVE_ISNAN - /* - ** Systems that support the isnan() library function should probably - ** make use of it by compiling with -DSQLITE_HAVE_ISNAN. But we have - ** found that many systems do not have a working isnan() function so - ** this implementation is provided as an alternative. - ** - ** This NaN test sometimes fails if compiled on GCC with -ffast-math. - ** On the other hand, the use of -ffast-math comes with the following - ** warning: - ** - ** This option [-ffast-math] should never be turned on by any - ** -O option since it can result in incorrect output for programs - ** which depend on an exact implementation of IEEE or ISO - ** rules/specifications for math functions. - ** - ** Under MSVC, this NaN test may fail if compiled with a floating- - ** point precision mode other than /fp:precise. From the MSDN - ** documentation: - ** - ** The compiler [with /fp:precise] will properly handle comparisons - ** involving NaN. For example, x != x evaluates to true if x is NaN - ** ... - */ -#ifdef __FAST_MATH__ -# error SQLite will not work correctly with the -ffast-math option of GCC. -#endif - volatile double y = x; - volatile double z = y; - rc = (y!=z); -#else /* if HAVE_ISNAN */ + u64 y; + memcpy(&y,&x,sizeof(y)); + rc = IsNaN(y); +#else rc = isnan(x); #endif /* HAVE_ISNAN */ testcase( rc ); @@ -96,6 +68,19 @@ int sqlite3IsNaN(double x){ } #endif /* SQLITE_OMIT_FLOATING_POINT */ +#ifndef SQLITE_OMIT_FLOATING_POINT +/* +** Return true if the floating point value is NaN or +Inf or -Inf. +*/ +int sqlite3IsOverflow(double x){ + int rc; /* The value return */ + u64 y; + memcpy(&y,&x,sizeof(y)); + rc = IsOvfl(y); + return rc; +} +#endif /* SQLITE_OMIT_FLOATING_POINT */ + /* ** Compute a string length that is limited to what can be stored in ** lower 30 bits of a 32-bit signed integer. @@ -109,31 +94,96 @@ int sqlite3Strlen30(const char *z){ return 0x3fffffff & (int)strlen(z); } +/* +** Return the declared type of a column. Or return zDflt if the column +** has no declared type. +** +** The column type is an extra string stored after the zero-terminator on +** the column name if and only if the COLFLAG_HASTYPE flag is set. +*/ +char *sqlite3ColumnType(Column *pCol, char *zDflt){ + if( pCol->colFlags & COLFLAG_HASTYPE ){ + return pCol->zCnName + strlen(pCol->zCnName) + 1; + }else if( pCol->eCType ){ + assert( pCol->eCType<=SQLITE_N_STDTYPE ); + return (char*)sqlite3StdType[pCol->eCType-1]; + }else{ + return zDflt; + } +} + +/* +** Helper function for sqlite3Error() - called rarely. Broken out into +** a separate routine to avoid unnecessary register saves on entry to +** sqlite3Error(). +*/ +static SQLITE_NOINLINE void sqlite3ErrorFinish(sqlite3 *db, int err_code){ + if( db->pErr ) sqlite3ValueSetNull(db->pErr); + sqlite3SystemError(db, err_code); +} + /* ** Set the current error code to err_code and clear any prior error message. +** Also set iSysErrno (by calling sqlite3System) if the err_code indicates +** that would be appropriate. */ void sqlite3Error(sqlite3 *db, int err_code){ assert( db!=0 ); db->errCode = err_code; + if( err_code || db->pErr ){ + sqlite3ErrorFinish(db, err_code); + }else{ + db->errByteOffset = -1; + } +} + +/* +** The equivalent of sqlite3Error(db, SQLITE_OK). Clear the error state +** and error message. +*/ +void sqlite3ErrorClear(sqlite3 *db){ + assert( db!=0 ); + db->errCode = SQLITE_OK; + db->errByteOffset = -1; if( db->pErr ) sqlite3ValueSetNull(db->pErr); } +/* +** Load the sqlite3.iSysErrno field if that is an appropriate thing +** to do based on the SQLite error code in rc. +*/ +void sqlite3SystemError(sqlite3 *db, int rc){ + if( rc==SQLITE_IOERR_NOMEM ) return; +#if defined(SQLITE_USE_SEH) && !defined(SQLITE_OMIT_WAL) + if( rc==SQLITE_IOERR_IN_PAGE ){ + int ii; + int iErr; + sqlite3BtreeEnterAll(db); + for(ii=0; ii<db->nDb; ii++){ + if( db->aDb[ii].pBt ){ + iErr = sqlite3PagerWalSystemErrno(sqlite3BtreePager(db->aDb[ii].pBt)); + if( iErr ){ + db->iSysErrno = iErr; + } + } + } + sqlite3BtreeLeaveAll(db); + return; + } +#endif + rc &= 0xff; + if( rc==SQLITE_CANTOPEN || rc==SQLITE_IOERR ){ + db->iSysErrno = sqlite3OsGetLastError(db->pVfs); + } +} + /* ** Set the most recent error code and error string for the sqlite ** handle "db". The error code is set to "err_code". ** ** If it is not NULL, string zFormat specifies the format of the -** error string in the style of the printf functions: The following -** format characters are allowed: -** -** %s Insert a string -** %z A string that should be freed after use -** %d Insert an integer -** %T Insert a token -** %S Insert the first element of a SrcList -** -** zFormat and any string tokens that follow it are assumed to be -** encoded in UTF-8. +** error string. zFormat and any string tokens that follow it are +** assumed to be encoded in UTF-8. ** ** To clear the most recent error for sqlite handle "db", sqlite3Error ** should be called with err_code set to SQLITE_OK and zFormat set @@ -142,6 +192,7 @@ void sqlite3Error(sqlite3 *db, int err_code){ void sqlite3ErrorWithMsg(sqlite3 *db, int err_code, const char *zFormat, ...){ assert( db!=0 ); db->errCode = err_code; + sqlite3SystemError(db, err_code); if( zFormat==0 ){ sqlite3Error(db, err_code); }else if( db->pErr || (db->pErr = sqlite3ValueNew(db))!=0 ){ @@ -154,15 +205,32 @@ void sqlite3ErrorWithMsg(sqlite3 *db, int err_code, const char *zFormat, ...){ } } +/* +** Check for interrupts and invoke progress callback. +*/ +void sqlite3ProgressCheck(Parse *p){ + sqlite3 *db = p->db; + if( AtomicLoad(&db->u1.isInterrupted) ){ + p->nErr++; + p->rc = SQLITE_INTERRUPT; + } +#ifndef SQLITE_OMIT_PROGRESS_CALLBACK + if( db->xProgress ){ + if( p->rc==SQLITE_INTERRUPT ){ + p->nProgressSteps = 0; + }else if( (++p->nProgressSteps)>=db->nProgressOps ){ + if( db->xProgress(db->pProgressArg) ){ + p->nErr++; + p->rc = SQLITE_INTERRUPT; + } + p->nProgressSteps = 0; + } + } +#endif +} + /* ** Add an error message to pParse->zErrMsg and increment pParse->nErr. -** The following formatting characters are allowed: -** -** %s Insert a string -** %z A string that should be freed after use -** %d Insert an integer -** %T Insert a token -** %S Insert the first element of a SrcList ** ** This function should be used to report any error that occurs while ** compiling an SQL statement (i.e. within sqlite3_prepare()). The @@ -175,19 +243,41 @@ void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){ char *zMsg; va_list ap; sqlite3 *db = pParse->db; + assert( db!=0 ); + assert( db->pParse==pParse || db->pParse->pToplevel==pParse ); + db->errByteOffset = -2; va_start(ap, zFormat); zMsg = sqlite3VMPrintf(db, zFormat, ap); va_end(ap); + if( db->errByteOffset<-1 ) db->errByteOffset = -1; if( db->suppressErr ){ sqlite3DbFree(db, zMsg); + if( db->mallocFailed ){ + pParse->nErr++; + pParse->rc = SQLITE_NOMEM; + } }else{ pParse->nErr++; sqlite3DbFree(db, pParse->zErrMsg); pParse->zErrMsg = zMsg; pParse->rc = SQLITE_ERROR; + pParse->pWith = 0; } } +/* +** If database connection db is currently parsing SQL, then transfer +** error code errCode to that parser if the parser has not already +** encountered some other kind of error. +*/ +int sqlite3ErrorToParser(sqlite3 *db, int errCode){ + Parse *pParse; + if( db==0 || (pParse = db->pParse)==0 ) return errCode; + pParse->rc = errCode; + pParse->nErr++; + return errCode; +} + /* ** Convert an SQL-style quoted string into a normal string by removing ** the quote characters. The conversion is done in-place. If the @@ -201,22 +291,17 @@ void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){ ** dequoted string, exclusive of the zero terminator, if dequoting does ** occur. ** -** 2002-Feb-14: This routine is extended to remove MS-Access style +** 2002-02-14: This routine is extended to remove MS-Access style ** brackets from around identifiers. For example: "[a-b-c]" becomes ** "a-b-c". */ -int sqlite3Dequote(char *z){ +void sqlite3Dequote(char *z){ char quote; int i, j; - if( z==0 ) return -1; + if( z==0 ) return; quote = z[0]; - switch( quote ){ - case '\'': break; - case '"': break; - case '`': break; /* For MySQL compatibility */ - case '[': quote = ']'; break; /* For MS SqlServer compatibility */ - default: return -1; - } + if( !sqlite3Isquote(quote) ) return; + if( quote=='[' ) quote = ']'; for(i=1, j=0;; i++){ assert( z[i] ); if( z[i]==quote ){ @@ -231,7 +316,72 @@ int sqlite3Dequote(char *z){ } } z[j] = 0; - return j; +} +void sqlite3DequoteExpr(Expr *p){ + assert( !ExprHasProperty(p, EP_IntValue) ); + assert( sqlite3Isquote(p->u.zToken[0]) ); + p->flags |= p->u.zToken[0]=='"' ? EP_Quoted|EP_DblQuoted : EP_Quoted; + sqlite3Dequote(p->u.zToken); +} + +/* +** Expression p is a QNUMBER (quoted number). Dequote the value in p->u.zToken +** and set the type to INTEGER or FLOAT. "Quoted" integers or floats are those +** that contain '_' characters that must be removed before further processing. +*/ +void sqlite3DequoteNumber(Parse *pParse, Expr *p){ + assert( p!=0 || pParse->db->mallocFailed ); + if( p ){ + const char *pIn = p->u.zToken; + char *pOut = p->u.zToken; + int bHex = (pIn[0]=='0' && (pIn[1]=='x' || pIn[1]=='X')); + int iValue; + assert( p->op==TK_QNUMBER ); + p->op = TK_INTEGER; + do { + if( *pIn!=SQLITE_DIGIT_SEPARATOR ){ + *pOut++ = *pIn; + if( *pIn=='e' || *pIn=='E' || *pIn=='.' ) p->op = TK_FLOAT; + }else{ + if( (bHex==0 && (!sqlite3Isdigit(pIn[-1]) || !sqlite3Isdigit(pIn[1]))) + || (bHex==1 && (!sqlite3Isxdigit(pIn[-1]) || !sqlite3Isxdigit(pIn[1]))) + ){ + sqlite3ErrorMsg(pParse, "unrecognized token: \"%s\"", p->u.zToken); + } + } + }while( *pIn++ ); + if( bHex ) p->op = TK_INTEGER; + + /* tag-20240227-a: If after dequoting, the number is an integer that + ** fits in 32 bits, then it must be converted into EP_IntValue. Other + ** parts of the code expect this. See also tag-20240227-b. */ + if( p->op==TK_INTEGER && sqlite3GetInt32(p->u.zToken, &iValue) ){ + p->u.iValue = iValue; + p->flags |= EP_IntValue; + } + } +} + +/* +** If the input token p is quoted, try to adjust the token to remove +** the quotes. This is not always possible: +** +** "abc" -> abc +** "ab""cd" -> (not possible because of the interior "") +** +** Remove the quotes if possible. This is a optimization. The overall +** system should still return the correct answer even if this routine +** is always a no-op. +*/ +void sqlite3DequoteToken(Token *p){ + unsigned int i; + if( p->n<2 ) return; + if( !sqlite3Isquote(p->z[0]) ) return; + for(i=1; i<p->n-1; i++){ + if( sqlite3Isquote(p->z[i]) ) return; + } + p->n -= 2; + p->z++; } /* @@ -256,16 +406,31 @@ void sqlite3TokenInit(Token *p, char *z){ ** independence" that SQLite uses internally when comparing identifiers. */ int sqlite3_stricmp(const char *zLeft, const char *zRight){ - register unsigned char *a, *b; if( zLeft==0 ){ return zRight ? -1 : 0; }else if( zRight==0 ){ return 1; } + return sqlite3StrICmp(zLeft, zRight); +} +int sqlite3StrICmp(const char *zLeft, const char *zRight){ + unsigned char *a, *b; + int c, x; a = (unsigned char *)zLeft; b = (unsigned char *)zRight; - while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; } - return UpperToLower[*a] - UpperToLower[*b]; + for(;;){ + c = *a; + x = *b; + if( c==x ){ + if( c==0 ) break; + }else{ + c = (int)UpperToLower[c] - (int)UpperToLower[x]; + if( c ) break; + } + a++; + b++; + } + return c; } int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){ register unsigned char *a, *b; @@ -280,6 +445,55 @@ int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){ return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b]; } +/* +** Compute an 8-bit hash on a string that is insensitive to case differences +*/ +u8 sqlite3StrIHash(const char *z){ + u8 h = 0; + if( z==0 ) return 0; + while( z[0] ){ + h += UpperToLower[(unsigned char)z[0]]; + z++; + } + return h; +} + +/* Double-Double multiplication. (x[0],x[1]) *= (y,yy) +** +** Reference: +** T. J. Dekker, "A Floating-Point Technique for Extending the +** Available Precision". 1971-07-26. +*/ +static void dekkerMul2(volatile double *x, double y, double yy){ + /* + ** The "volatile" keywords on parameter x[] and on local variables + ** below are needed force intermediate results to be truncated to + ** binary64 rather than be carried around in an extended-precision + ** format. The truncation is necessary for the Dekker algorithm to + ** work. Intel x86 floating point might omit the truncation without + ** the use of volatile. + */ + volatile double tx, ty, p, q, c, cc; + double hx, hy; + u64 m; + memcpy(&m, (void*)&x[0], 8); + m &= 0xfffffffffc000000LL; + memcpy(&hx, &m, 8); + tx = x[0] - hx; + memcpy(&m, &y, 8); + m &= 0xfffffffffc000000LL; + memcpy(&hy, &m, 8); + ty = y - hy; + p = hx*hy; + q = hx*ty + tx*hy; + c = p+q; + cc = p - c + q + tx*ty; + cc = x[0]*yy + x[1]*y + cc; + x[0] = c + cc; + x[1] = c - x[0]; + x[1] += cc; +} + /* ** The string z[] is an text representation of a real number. ** Convert this string to a double and write it into *pResult. @@ -288,8 +502,15 @@ int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){ ** uses the encoding enc. The string is not necessarily zero-terminated. ** ** Return TRUE if the result is a valid real number (or integer) and FALSE -** if the string is empty or contains extraneous text. Valid numbers -** are in one of these formats: +** if the string is empty or contains extraneous text. More specifically +** return +** 1 => The input string is a pure integer +** 2 or more => The input has a decimal point or eNNN clause +** 0 or less => The input string is not a valid number +** -1 => Not a valid number, but has a valid prefix which +** includes a decimal point and/or an eNNN clause +** +** Valid numbers are in one of these formats: ** ** [+-]digits[E[+-]digits] ** [+-]digits.[digits][E[+-]digits] @@ -302,33 +523,42 @@ int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){ ** returns FALSE but it still converts the prefix and writes the result ** into *pResult. */ +#if defined(_MSC_VER) +#pragma warning(disable : 4756) +#endif int sqlite3AtoF(const char *z, double *pResult, int length, u8 enc){ #ifndef SQLITE_OMIT_FLOATING_POINT int incr; - const char *zEnd = z + length; + const char *zEnd; /* sign * significand * (10 ^ (esign * exponent)) */ int sign = 1; /* sign of significand */ - i64 s = 0; /* significand */ + u64 s = 0; /* significand */ int d = 0; /* adjust exponent for shifting decimal point */ int esign = 1; /* sign of exponent */ int e = 0; /* exponent */ int eValid = 1; /* True exponent is either not used or is well-formed */ - double result; - int nDigits = 0; - int nonNum = 0; + int nDigit = 0; /* Number of digits processed */ + int eType = 1; /* 1: pure integer, 2+: fractional -1 or less: bad UTF16 */ + u64 s2; /* round-tripped significand */ + double rr[2]; assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE ); *pResult = 0.0; /* Default return value, in case of an error */ + if( length==0 ) return 0; if( enc==SQLITE_UTF8 ){ incr = 1; + zEnd = z + length; }else{ int i; incr = 2; + length &= ~1; assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 ); + testcase( enc==SQLITE_UTF16LE ); + testcase( enc==SQLITE_UTF16BE ); for(i=3-enc; i<length && z[i]==0; i+=2){} - nonNum = i<length; - zEnd = z+i+enc-3; + if( i<length ) eType = -100; + zEnd = &z[i^1]; z += (enc&1); } @@ -344,31 +574,32 @@ int sqlite3AtoF(const char *z, double *pResult, int length, u8 enc){ z+=incr; } - /* skip leading zeroes */ - while( z<zEnd && z[0]=='0' ) z+=incr, nDigits++; - /* copy max significant digits to significand */ - while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){ + while( z<zEnd && sqlite3Isdigit(*z) ){ s = s*10 + (*z - '0'); - z+=incr, nDigits++; + z+=incr; nDigit++; + if( s>=((LARGEST_UINT64-9)/10) ){ + /* skip non-significant significand digits + ** (increase exponent by d to shift decimal left) */ + while( z<zEnd && sqlite3Isdigit(*z) ){ z+=incr; d++; } + } } - - /* skip non-significant significand digits - ** (increase exponent by d to shift decimal left) */ - while( z<zEnd && sqlite3Isdigit(*z) ) z+=incr, nDigits++, d++; if( z>=zEnd ) goto do_atof_calc; /* if decimal point is present */ if( *z=='.' ){ z+=incr; + eType++; /* copy digits from after decimal to significand ** (decrease exponent by d to shift decimal right) */ - while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){ - s = s*10 + (*z - '0'); - z+=incr, nDigits++, d--; + while( z<zEnd && sqlite3Isdigit(*z) ){ + if( s<((LARGEST_UINT64-9)/10) ){ + s = s*10 + (*z - '0'); + d--; + nDigit++; + } + z+=incr; } - /* skip non-significant digits */ - while( z<zEnd && sqlite3Isdigit(*z) ) z+=incr, nDigits++; } if( z>=zEnd ) goto do_atof_calc; @@ -376,7 +607,13 @@ int sqlite3AtoF(const char *z, double *pResult, int length, u8 enc){ if( *z=='e' || *z=='E' ){ z+=incr; eValid = 0; - if( z>=zEnd ) goto do_atof_calc; + eType++; + + /* This branch is needed to avoid a (harmless) buffer overread. The + ** special comment alerts the mutation tester that the correct answer + ** is obtained even if the branch is omitted */ + if( z>=zEnd ) goto do_atof_calc; /*PREVENTS-HARMLESS-OVERREAD*/ + /* get sign of exponent */ if( *z=='-' ){ esign = -1; @@ -393,81 +630,123 @@ int sqlite3AtoF(const char *z, double *pResult, int length, u8 enc){ } /* skip trailing spaces */ - if( nDigits && eValid ){ - while( z<zEnd && sqlite3Isspace(*z) ) z+=incr; - } + while( z<zEnd && sqlite3Isspace(*z) ) z+=incr; do_atof_calc: + /* Zero is a special case */ + if( s==0 ){ + *pResult = sign<0 ? -0.0 : +0.0; + goto atof_return; + } + /* adjust exponent by d, and update sign */ e = (e*esign) + d; - if( e<0 ) { - esign = -1; - e *= -1; - } else { - esign = 1; - } - - /* if 0 significand */ - if( !s ) { - /* In the IEEE 754 standard, zero is signed. - ** Add the sign if we've seen at least one digit */ - result = (sign<0 && nDigits) ? -(double)0 : (double)0; - } else { - /* attempt to reduce exponent */ - if( esign>0 ){ - while( s<(LARGEST_INT64/10) && e>0 ) e--,s*=10; - }else{ - while( !(s%10) && e>0 ) e--,s/=10; - } - /* adjust the sign of significand */ - s = sign<0 ? -s : s; - - /* if exponent, scale significand as appropriate - ** and store in result. */ - if( e ){ - LONGDOUBLE_TYPE scale = 1.0; - /* attempt to handle extremely small/large numbers better */ - if( e>307 && e<342 ){ - while( e%308 ) { scale *= 1.0e+1; e -= 1; } - if( esign<0 ){ - result = s / scale; - result /= 1.0e+308; - }else{ - result = s * scale; - result *= 1.0e+308; - } - }else if( e>=342 ){ - if( esign<0 ){ - result = 0.0*s; - }else{ - result = 1e308*1e308*s; /* Infinity */ - } - }else{ - /* 1.0e+22 is the largest power of 10 than can be - ** represented exactly. */ - while( e%22 ) { scale *= 1.0e+1; e -= 1; } - while( e>0 ) { scale *= 1.0e+22; e -= 22; } - if( esign<0 ){ - result = s / scale; - }else{ - result = s * scale; - } - } - } else { - result = (double)s; - } + /* Try to adjust the exponent to make it smaller */ + while( e>0 && s<((LARGEST_UINT64-0x7ff)/10) ){ + s *= 10; + e--; + } + while( e<0 && (s%10)==0 ){ + s /= 10; + e++; } - /* store the result */ - *pResult = result; - - /* return true if number and no extra non-whitespace chracters after */ - return z>=zEnd && nDigits>0 && eValid && nonNum==0; + rr[0] = (double)s; + assert( sizeof(s2)==sizeof(rr[0]) ); +#ifdef SQLITE_DEBUG + rr[1] = 18446744073709549568.0; + memcpy(&s2, &rr[1], sizeof(s2)); + assert( s2==0x43efffffffffffffLL ); +#endif + /* Largest double that can be safely converted to u64 + ** vvvvvvvvvvvvvvvvvvvvvv */ + if( rr[0]<=18446744073709549568.0 ){ + s2 = (u64)rr[0]; + rr[1] = s>=s2 ? (double)(s - s2) : -(double)(s2 - s); + }else{ + rr[1] = 0.0; + } + assert( rr[1]<=1.0e-10*rr[0] ); /* Equal only when rr[0]==0.0 */ + + if( e>0 ){ + while( e>=100 ){ + e -= 100; + dekkerMul2(rr, 1.0e+100, -1.5902891109759918046e+83); + } + while( e>=10 ){ + e -= 10; + dekkerMul2(rr, 1.0e+10, 0.0); + } + while( e>=1 ){ + e -= 1; + dekkerMul2(rr, 1.0e+01, 0.0); + } + }else{ + while( e<=-100 ){ + e += 100; + dekkerMul2(rr, 1.0e-100, -1.99918998026028836196e-117); + } + while( e<=-10 ){ + e += 10; + dekkerMul2(rr, 1.0e-10, -3.6432197315497741579e-27); + } + while( e<=-1 ){ + e += 1; + dekkerMul2(rr, 1.0e-01, -5.5511151231257827021e-18); + } + } + *pResult = rr[0]+rr[1]; + if( sqlite3IsNaN(*pResult) ) *pResult = 1e300*1e300; + if( sign<0 ) *pResult = -*pResult; + assert( !sqlite3IsNaN(*pResult) ); + +atof_return: + /* return true if number and no extra non-whitespace characters after */ + if( z==zEnd && nDigit>0 && eValid && eType>0 ){ + return eType; + }else if( eType>=2 && (eType==3 || eValid) && nDigit>0 ){ + return -1; + }else{ + return 0; + } #else return !sqlite3Atoi64(z, pResult, length, enc); #endif /* SQLITE_OMIT_FLOATING_POINT */ } +#if defined(_MSC_VER) +#pragma warning(default : 4756) +#endif + +/* +** Render an signed 64-bit integer as text. Store the result in zOut[] and +** return the length of the string that was stored, in bytes. The value +** returned does not include the zero terminator at the end of the output +** string. +** +** The caller must ensure that zOut[] is at least 21 bytes in size. +*/ +int sqlite3Int64ToText(i64 v, char *zOut){ + int i; + u64 x; + char zTemp[22]; + if( v<0 ){ + x = (v==SMALLEST_INT64) ? ((u64)1)<<63 : (u64)-v; + }else{ + x = v; + } + i = sizeof(zTemp)-2; + zTemp[sizeof(zTemp)-1] = 0; + while( 1 /*exit-by-break*/ ){ + zTemp[i] = (x%10) + '0'; + x = x/10; + if( x==0 ) break; + i--; + }; + if( v<0 ) zTemp[--i] = '-'; + memcpy(zOut, &zTemp[i], sizeof(zTemp)-i); + return sizeof(zTemp)-1-i; +} /* ** Compare the 19-character string zNum against the text representation @@ -504,16 +783,13 @@ static int compare2pow63(const char *zNum, int incr){ ** Convert zNum to a 64-bit signed integer. zNum must be decimal. This ** routine does *not* accept hexadecimal notation. ** -** If the zNum value is representable as a 64-bit twos-complement -** integer, then write that value into *pNum and return 0. -** -** If zNum is exactly 9223372036854775808, return 2. This special -** case is broken out because while 9223372036854775808 cannot be a -** signed 64-bit integer, its negative -9223372036854775808 can be. +** Returns: ** -** If zNum is too big for a 64-bit integer and is not -** 9223372036854775808 or if zNum contains any non-numeric text, -** then return 1. +** -1 Not even a prefix of the input text looks like an integer +** 0 Successful transformation. Fits in a 64-bit signed integer. +** 1 Excess non-space text after the integer value +** 2 Integer too large for a 64-bit signed integer or is malformed +** 3 Special case of 9223372036854775808 ** ** length is the number of bytes in the string (bytes, not characters). ** The string is not necessarily zero-terminated. The encoding is @@ -525,7 +801,8 @@ int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc){ int neg = 0; /* assume positive */ int i; int c = 0; - int nonNum = 0; + int nonNum = 0; /* True if input contains UTF16 with high byte non-zero */ + int rc; /* Baseline return code */ const char *zStart; const char *zEnd = zNum + length; assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE ); @@ -533,10 +810,11 @@ int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc){ incr = 1; }else{ incr = 2; + length &= ~1; assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 ); for(i=3-enc; i<length && zNum[i]==0; i+=2){} nonNum = i<length; - zEnd = zNum+i+enc-3; + zEnd = &zNum[i^1]; zNum += (enc&1); } while( zNum<zEnd && sqlite3Isspace(*zNum) ) zNum+=incr; @@ -553,40 +831,57 @@ int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc){ for(i=0; &zNum[i]<zEnd && (c=zNum[i])>='0' && c<='9'; i+=incr){ u = u*10 + c - '0'; } + testcase( i==18*incr ); + testcase( i==19*incr ); + testcase( i==20*incr ); if( u>LARGEST_INT64 ){ + /* This test and assignment is needed only to suppress UB warnings + ** from clang and -fsanitize=undefined. This test and assignment make + ** the code a little larger and slower, and no harm comes from omitting + ** them, but we must appease the undefined-behavior pharisees. */ *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64; }else if( neg ){ *pNum = -(i64)u; }else{ *pNum = (i64)u; } - testcase( i==18 ); - testcase( i==19 ); - testcase( i==20 ); - if( (c!=0 && &zNum[i]<zEnd) || (i==0 && zStart==zNum) - || i>19*incr || nonNum ){ - /* zNum is empty or contains non-numeric text or is longer - ** than 19 digits (thus guaranteeing that it is too large) */ - return 1; - }else if( i<19*incr ){ + rc = 0; + if( i==0 && zStart==zNum ){ /* No digits */ + rc = -1; + }else if( nonNum ){ /* UTF16 with high-order bytes non-zero */ + rc = 1; + }else if( &zNum[i]<zEnd ){ /* Extra bytes at the end */ + int jj = i; + do{ + if( !sqlite3Isspace(zNum[jj]) ){ + rc = 1; /* Extra non-space text after the integer */ + break; + } + jj += incr; + }while( &zNum[jj]<zEnd ); + } + if( i<19*incr ){ /* Less than 19 digits, so we know that it fits in 64 bits */ assert( u<=LARGEST_INT64 ); - return 0; + return rc; }else{ /* zNum is a 19-digit numbers. Compare it against 9223372036854775808. */ - c = compare2pow63(zNum, incr); + c = i>19*incr ? 1 : compare2pow63(zNum, incr); if( c<0 ){ /* zNum is less than 9223372036854775808 so it fits */ assert( u<=LARGEST_INT64 ); - return 0; - }else if( c>0 ){ - /* zNum is greater than 9223372036854775808 so it overflows */ - return 1; + return rc; }else{ - /* zNum is exactly 9223372036854775808. Fits if negative. The - ** special case 2 overflow if positive */ - assert( u-1==LARGEST_INT64 ); - return neg ? 0 : 2; + *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64; + if( c>0 ){ + /* zNum is greater than 9223372036854775808 so it overflows */ + return 2; + }else{ + /* zNum is exactly 9223372036854775808. Fits if negative. The + ** special case 2 overflow if positive */ + assert( u-1==LARGEST_INT64 ); + return neg ? rc : 3; + } } } } @@ -599,14 +894,14 @@ int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc){ ** Returns: ** ** 0 Successful transformation. Fits in a 64-bit signed integer. -** 1 Integer too large for a 64-bit signed integer or is malformed -** 2 Special case of 9223372036854775808 +** 1 Excess text after the integer value +** 2 Integer too large for a 64-bit signed integer or is malformed +** 3 Special case of 9223372036854775808 */ int sqlite3DecOrHexToI64(const char *z, i64 *pOut){ #ifndef SQLITE_OMIT_HEX_INTEGER if( z[0]=='0' && (z[1]=='x' || z[1]=='X') - && sqlite3Isxdigit(z[2]) ){ u64 u = 0; int i, k; @@ -615,11 +910,15 @@ int sqlite3DecOrHexToI64(const char *z, i64 *pOut){ u = u*16 + sqlite3HexToInt(z[k]); } memcpy(pOut, &u, 8); - return (z[k]==0 && k-i<=16) ? 0 : 1; + if( k-i>16 ) return 2; + if( z[k]!=0 ) return 1; + return 0; }else #endif /* SQLITE_OMIT_HEX_INTEGER */ { - return sqlite3Atoi64(z, pOut, sqlite3Strlen30(z), SQLITE_UTF8); + int n = (int)(0x3fffffff&strspn(z,"+- \n\t0123456789")); + if( z[n] ) n++; + return sqlite3Atoi64(z, pOut, n, SQLITE_UTF8); } } @@ -651,7 +950,7 @@ int sqlite3GetInt32(const char *zNum, int *pValue){ u32 u = 0; zNum += 2; while( zNum[0]=='0' ) zNum++; - for(i=0; sqlite3Isxdigit(zNum[i]) && i<8; i++){ + for(i=0; i<8 && sqlite3Isxdigit(zNum[i]); i++){ u = u*16 + sqlite3HexToInt(zNum[i]); } if( (u&0x80000000)==0 && sqlite3Isxdigit(zNum[i])==0 ){ @@ -662,6 +961,7 @@ int sqlite3GetInt32(const char *zNum, int *pValue){ } } #endif + if( !sqlite3Isdigit(zNum[0]) ) return 0; while( zNum[0]=='0' ) zNum++; for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){ v = v*10 + c; @@ -693,10 +993,168 @@ int sqlite3GetInt32(const char *zNum, int *pValue){ */ int sqlite3Atoi(const char *z){ int x = 0; - if( z ) sqlite3GetInt32(z, &x); + sqlite3GetInt32(z, &x); return x; } +/* +** Decode a floating-point value into an approximate decimal +** representation. +** +** If iRound<=0 then round to -iRound significant digits to the +** the left of the decimal point, or to a maximum of mxRound total +** significant digits. +** +** If iRound>0 round to min(iRound,mxRound) significant digits total. +** +** mxRound must be positive. +** +** The significant digits of the decimal representation are +** stored in p->z[] which is a often (but not always) a pointer +** into the middle of p->zBuf[]. There are p->n significant digits. +** The p->z[] array is *not* zero-terminated. +*/ +void sqlite3FpDecode(FpDecode *p, double r, int iRound, int mxRound){ + int i; + u64 v; + int e, exp = 0; + double rr[2]; + + p->isSpecial = 0; + p->z = p->zBuf; + assert( mxRound>0 ); + + /* Convert negative numbers to positive. Deal with Infinity, 0.0, and + ** NaN. */ + if( r<0.0 ){ + p->sign = '-'; + r = -r; + }else if( r==0.0 ){ + p->sign = '+'; + p->n = 1; + p->iDP = 1; + p->z = "0"; + return; + }else{ + p->sign = '+'; + } + memcpy(&v,&r,8); + e = v>>52; + if( (e&0x7ff)==0x7ff ){ + p->isSpecial = 1 + (v!=0x7ff0000000000000LL); + p->n = 0; + p->iDP = 0; + return; + } + + /* Multiply r by powers of ten until it lands somewhere in between + ** 1.0e+19 and 1.0e+17. + ** + ** Use Dekker-style double-double computation to increase the + ** precision. + ** + ** The error terms on constants like 1.0e+100 computed using the + ** decimal extension, for example as follows: + ** + ** SELECT decimal_exp(decimal_sub('1.0e+100',decimal(1.0e+100))); + */ + rr[0] = r; + rr[1] = 0.0; + if( rr[0]>9.223372036854774784e+18 ){ + while( rr[0]>9.223372036854774784e+118 ){ + exp += 100; + dekkerMul2(rr, 1.0e-100, -1.99918998026028836196e-117); + } + while( rr[0]>9.223372036854774784e+28 ){ + exp += 10; + dekkerMul2(rr, 1.0e-10, -3.6432197315497741579e-27); + } + while( rr[0]>9.223372036854774784e+18 ){ + exp += 1; + dekkerMul2(rr, 1.0e-01, -5.5511151231257827021e-18); + } + }else{ + while( rr[0]<9.223372036854774784e-83 ){ + exp -= 100; + dekkerMul2(rr, 1.0e+100, -1.5902891109759918046e+83); + } + while( rr[0]<9.223372036854774784e+07 ){ + exp -= 10; + dekkerMul2(rr, 1.0e+10, 0.0); + } + while( rr[0]<9.22337203685477478e+17 ){ + exp -= 1; + dekkerMul2(rr, 1.0e+01, 0.0); + } + } + v = rr[1]<0.0 ? (u64)rr[0]-(u64)(-rr[1]) : (u64)rr[0]+(u64)rr[1]; + + /* Extract significant digits. */ + i = sizeof(p->zBuf)-1; + assert( v>0 ); + while( v ){ p->zBuf[i--] = (v%10) + '0'; v /= 10; } + assert( i>=0 && i<sizeof(p->zBuf)-1 ); + p->n = sizeof(p->zBuf) - 1 - i; + assert( p->n>0 ); + assert( p->n<sizeof(p->zBuf) ); + p->iDP = p->n + exp; + if( iRound<=0 ){ + iRound = p->iDP - iRound; + if( iRound==0 && p->zBuf[i+1]>='5' ){ + iRound = 1; + p->zBuf[i--] = '0'; + p->n++; + p->iDP++; + } + } + if( iRound>0 && (iRound<p->n || p->n>mxRound) ){ + char *z = &p->zBuf[i+1]; + if( iRound>mxRound ) iRound = mxRound; + p->n = iRound; + if( z[iRound]>='5' ){ + int j = iRound-1; + while( 1 /*exit-by-break*/ ){ + z[j]++; + if( z[j]<='9' ) break; + z[j] = '0'; + if( j==0 ){ + p->z[i--] = '1'; + p->n++; + p->iDP++; + break; + }else{ + j--; + } + } + } + } + p->z = &p->zBuf[i+1]; + assert( i+p->n < sizeof(p->zBuf) ); + assert( p->n>0 ); + while( p->z[p->n-1]=='0' ){ + p->n--; + assert( p->n>0 ); + } +} + +/* +** Try to convert z into an unsigned 32-bit integer. Return true on +** success and false if there is an error. +** +** Only decimal notation is accepted. +*/ +int sqlite3GetUInt32(const char *z, u32 *pI){ + u64 v = 0; + int i; + for(i=0; sqlite3Isdigit(z[i]); i++){ + v = v*10 + z[i] - '0'; + if( v>4294967296LL ){ *pI = 0; return 0; } + } + if( i==0 || z[i]!=0 ){ *pI = 0; return 0; } + *pI = (u32)v; + return 1; +} + /* ** The variable-length integer encoding is as follows: ** @@ -737,7 +1195,7 @@ static int SQLITE_NOINLINE putVarint64(unsigned char *p, u64 v){ v >>= 7; } return 9; - } + } n = 0; do{ buf[n++] = (u8)((v & 0x7f) | 0x80); @@ -783,23 +1241,12 @@ int sqlite3PutVarint(unsigned char *p, u64 v){ u8 sqlite3GetVarint(const unsigned char *p, u64 *v){ u32 a,b,s; - a = *p; - /* a: p0 (unmasked) */ - if (!(a&0x80)) - { - *v = a; + if( ((signed char*)p)[0]>=0 ){ + *v = *p; return 1; } - - p++; - b = *p; - /* b: p1 (unmasked) */ - if (!(b&0x80)) - { - a &= 0x7f; - a = a<<7; - a |= b; - *v = a; + if( ((signed char*)p)[1]>=0 ){ + *v = ((u32)(p[0]&0x7f)<<7) | p[1]; return 2; } @@ -807,8 +1254,9 @@ u8 sqlite3GetVarint(const unsigned char *p, u64 *v){ assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) ); assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) ); - p++; - a = a<<14; + a = ((u32)p[0])<<14; + b = p[1]; + p += 2; a |= *p; /* a: p0<<14 | p2 (unmasked) */ if (!(a&0x80)) @@ -947,127 +1395,37 @@ u8 sqlite3GetVarint(const unsigned char *p, u64 *v){ ** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned ** integer, then set *v to 0xffffffff. ** -** A MACRO version, getVarint32, is provided which inlines the -** single-byte case. All code should use the MACRO version as +** A MACRO version, getVarint32, is provided which inlines the +** single-byte case. All code should use the MACRO version as ** this function assumes the single-byte case has already been handled. */ u8 sqlite3GetVarint32(const unsigned char *p, u32 *v){ - u32 a,b; + u64 v64; + u8 n; - /* The 1-byte case. Overwhelmingly the most common. Handled inline - ** by the getVarin32() macro */ - a = *p; - /* a: p0 (unmasked) */ -#ifndef getVarint32 - if (!(a&0x80)) - { - /* Values between 0 and 127 */ - *v = a; - return 1; - } -#endif + /* Assume that the single-byte case has already been handled by + ** the getVarint32() macro */ + assert( (p[0] & 0x80)!=0 ); - /* The 2-byte case */ - p++; - b = *p; - /* b: p1 (unmasked) */ - if (!(b&0x80)) - { - /* Values between 128 and 16383 */ - a &= 0x7f; - a = a<<7; - *v = a | b; + if( (p[1] & 0x80)==0 ){ + /* This is the two-byte case */ + *v = ((p[0]&0x7f)<<7) | p[1]; return 2; } - - /* The 3-byte case */ - p++; - a = a<<14; - a |= *p; - /* a: p0<<14 | p2 (unmasked) */ - if (!(a&0x80)) - { - /* Values between 16384 and 2097151 */ - a &= (0x7f<<14)|(0x7f); - b &= 0x7f; - b = b<<7; - *v = a | b; + if( (p[2] & 0x80)==0 ){ + /* This is the three-byte case */ + *v = ((p[0]&0x7f)<<14) | ((p[1]&0x7f)<<7) | p[2]; return 3; } - - /* A 32-bit varint is used to store size information in btrees. - ** Objects are rarely larger than 2MiB limit of a 3-byte varint. - ** A 3-byte varint is sufficient, for example, to record the size - ** of a 1048569-byte BLOB or string. - ** - ** We only unroll the first 1-, 2-, and 3- byte cases. The very - ** rare larger cases can be handled by the slower 64-bit varint - ** routine. - */ -#if 1 - { - u64 v64; - u8 n; - - p -= 2; - n = sqlite3GetVarint(p, &v64); - assert( n>3 && n<=9 ); - if( (v64 & SQLITE_MAX_U32)!=v64 ){ - *v = 0xffffffff; - }else{ - *v = (u32)v64; - } - return n; - } - -#else - /* For following code (kept for historical record only) shows an - ** unrolling for the 3- and 4-byte varint cases. This code is - ** slightly faster, but it is also larger and much harder to test. - */ - p++; - b = b<<14; - b |= *p; - /* b: p1<<14 | p3 (unmasked) */ - if (!(b&0x80)) - { - /* Values between 2097152 and 268435455 */ - b &= (0x7f<<14)|(0x7f); - a &= (0x7f<<14)|(0x7f); - a = a<<7; - *v = a | b; - return 4; - } - - p++; - a = a<<14; - a |= *p; - /* a: p0<<28 | p2<<14 | p4 (unmasked) */ - if (!(a&0x80)) - { - /* Values between 268435456 and 34359738367 */ - a &= SLOT_4_2_0; - b &= SLOT_4_2_0; - b = b<<7; - *v = a | b; - return 5; - } - - /* We can only reach this point when reading a corrupt database - ** file. In that case we are not in any hurry. Use the (relatively - ** slow) general-purpose sqlite3GetVarint() routine to extract the - ** value. */ - { - u64 v64; - u8 n; - - p -= 4; - n = sqlite3GetVarint(p, &v64); - assert( n>5 && n<=9 ); + /* four or more bytes */ + n = sqlite3GetVarint(p, &v64); + assert( n>3 && n<=9 ); + if( (v64 & SQLITE_MAX_U32)!=v64 ){ + *v = 0xffffffff; + }else{ *v = (u32)v64; - return n; } -#endif + return n; } /* @@ -1076,7 +1434,7 @@ u8 sqlite3GetVarint32(const unsigned char *p, u32 *v){ */ int sqlite3VarintLen(u64 v){ int i; - for(i=1; (v >>= 7)!=0; i++){ assert( i<9 ); } + for(i=1; (v >>= 7)!=0; i++){ assert( i<10 ); } return i; } @@ -1089,13 +1447,11 @@ u32 sqlite3Get4byte(const u8 *p){ u32 x; memcpy(&x,p,4); return x; -#elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \ - && defined(__GNUC__) && GCC_VERSION>=4003000 +#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000 u32 x; memcpy(&x,p,4); return __builtin_bswap32(x); -#elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \ - && defined(_MSC_VER) && _MSC_VER>=1300 +#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300 u32 x; memcpy(&x,p,4); return _byteswap_ulong(x); @@ -1107,10 +1463,10 @@ u32 sqlite3Get4byte(const u8 *p){ void sqlite3Put4byte(unsigned char *p, u32 v){ #if SQLITE_BYTEORDER==4321 memcpy(p,&v,4); -#elif SQLITE_BYTEORDER==1234 && defined(__GNUC__) && GCC_VERSION>=4003000 +#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000 u32 x = __builtin_bswap32(v); memcpy(p,&x,4); -#elif SQLITE_BYTEORDER==1234 && defined(_MSC_VER) && _MSC_VER>=1300 +#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300 u32 x = _byteswap_ulong(v); memcpy(p,&x,4); #else @@ -1139,6 +1495,7 @@ u8 sqlite3HexToInt(int h){ return (u8)(h & 0xf); } +/* BEGIN SQLCIPHER */ #if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC) /* ** Convert a BLOB literal of the form "x'hhhhhh'" into its binary @@ -1161,6 +1518,7 @@ void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){ return zBlob; } #endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */ +/* END SQLCIPHER */ /* ** Log an error that is an API call on a connection pointer that should @@ -1168,7 +1526,7 @@ void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){ ** argument. The zType is a word like "NULL" or "closed" or "invalid". */ static void logBadConnection(const char *zType){ - sqlite3_log(SQLITE_MISUSE, + sqlite3_log(SQLITE_MISUSE, "API call with %s database connection pointer", zType ); @@ -1189,13 +1547,13 @@ static void logBadConnection(const char *zType){ ** used as an argument to sqlite3_errmsg() or sqlite3_close(). */ int sqlite3SafetyCheckOk(sqlite3 *db){ - u32 magic; + u8 eOpenState; if( db==0 ){ logBadConnection("NULL"); return 0; } - magic = db->magic; - if( magic!=SQLITE_MAGIC_OPEN ){ + eOpenState = db->eOpenState; + if( eOpenState!=SQLITE_STATE_OPEN ){ if( sqlite3SafetyCheckSickOrOk(db) ){ testcase( sqlite3GlobalConfig.xLog!=0 ); logBadConnection("unopened"); @@ -1206,11 +1564,11 @@ int sqlite3SafetyCheckOk(sqlite3 *db){ } } int sqlite3SafetyCheckSickOrOk(sqlite3 *db){ - u32 magic; - magic = db->magic; - if( magic!=SQLITE_MAGIC_SICK && - magic!=SQLITE_MAGIC_OPEN && - magic!=SQLITE_MAGIC_BUSY ){ + u8 eOpenState; + eOpenState = db->eOpenState; + if( eOpenState!=SQLITE_STATE_SICK && + eOpenState!=SQLITE_STATE_OPEN && + eOpenState!=SQLITE_STATE_BUSY ){ testcase( sqlite3GlobalConfig.xLog!=0 ); logBadConnection("invalid"); return 0; @@ -1220,12 +1578,15 @@ int sqlite3SafetyCheckSickOrOk(sqlite3 *db){ } /* -** Attempt to add, substract, or multiply the 64-bit signed value iB against +** Attempt to add, subtract, or multiply the 64-bit signed value iB against ** the other 64-bit signed integer at *pA and store the result in *pA. ** Return 0 on success. Or if the operation would have resulted in an ** overflow, leave *pA unchanged and return 1. */ int sqlite3AddInt64(i64 *pA, i64 iB){ +#if GCC_VERSION>=5004000 && !defined(__INTEL_COMPILER) + return __builtin_add_overflow(*pA, iB, pA); +#else i64 iA = *pA; testcase( iA==0 ); testcase( iA==1 ); testcase( iB==-1 ); testcase( iB==0 ); @@ -1239,9 +1600,13 @@ int sqlite3AddInt64(i64 *pA, i64 iB){ if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1; } *pA += iB; - return 0; + return 0; +#endif } int sqlite3SubInt64(i64 *pA, i64 iB){ +#if GCC_VERSION>=5004000 && !defined(__INTEL_COMPILER) + return __builtin_sub_overflow(*pA, iB, pA); +#else testcase( iB==SMALLEST_INT64+1 ); if( iB==SMALLEST_INT64 ){ testcase( (*pA)==(-1) ); testcase( (*pA)==0 ); @@ -1251,42 +1616,32 @@ int sqlite3SubInt64(i64 *pA, i64 iB){ }else{ return sqlite3AddInt64(pA, -iB); } +#endif } -#define TWOPOWER32 (((i64)1)<<32) -#define TWOPOWER31 (((i64)1)<<31) int sqlite3MulInt64(i64 *pA, i64 iB){ +#if GCC_VERSION>=5004000 && !defined(__INTEL_COMPILER) + return __builtin_mul_overflow(*pA, iB, pA); +#else i64 iA = *pA; - i64 iA1, iA0, iB1, iB0, r; - - iA1 = iA/TWOPOWER32; - iA0 = iA % TWOPOWER32; - iB1 = iB/TWOPOWER32; - iB0 = iB % TWOPOWER32; - if( iA1==0 ){ - if( iB1==0 ){ - *pA *= iB; - return 0; + if( iB>0 ){ + if( iA>LARGEST_INT64/iB ) return 1; + if( iA<SMALLEST_INT64/iB ) return 1; + }else if( iB<0 ){ + if( iA>0 ){ + if( iB<SMALLEST_INT64/iA ) return 1; + }else if( iA<0 ){ + if( iB==SMALLEST_INT64 ) return 1; + if( iA==SMALLEST_INT64 ) return 1; + if( -iA>LARGEST_INT64/-iB ) return 1; } - r = iA0*iB1; - }else if( iB1==0 ){ - r = iA1*iB0; - }else{ - /* If both iA1 and iB1 are non-zero, overflow will result */ - return 1; } - testcase( r==(-TWOPOWER31)-1 ); - testcase( r==(-TWOPOWER31) ); - testcase( r==TWOPOWER31 ); - testcase( r==TWOPOWER31-1 ); - if( r<(-TWOPOWER31) || r>=TWOPOWER31 ) return 1; - r *= TWOPOWER32; - if( sqlite3AddInt64(&r, iA0*iB0) ) return 1; - *pA = r; + *pA = iA*iB; return 0; +#endif } /* -** Compute the absolute value of a 32-bit signed integer, of possible. Or +** Compute the absolute value of a 32-bit signed integer, if possible. Or ** if the integer has a value of -2147483648, return +2147483647 */ int sqlite3AbsInt32(int x){ @@ -1326,11 +1681,11 @@ void sqlite3FileSuffix3(const char *zBaseFilename, char *z){ } #endif -/* +/* ** Find (an approximate) sum of two LogEst values. This computation is ** not a simple "+" operator because LogEst is stored as a logarithmic ** value. -** +** */ LogEst sqlite3LogEstAdd(LogEst a, LogEst b){ static const unsigned char x[] = { @@ -1366,13 +1721,18 @@ LogEst sqlite3LogEst(u64 x){ if( x<2 ) return 0; while( x<8 ){ y -= 10; x <<= 1; } }else{ - while( x>255 ){ y += 40; x >>= 4; } +#if GCC_VERSION>=5004000 + int i = 60 - __builtin_clzll(x); + y += i*10; + x >>= i; +#else + while( x>255 ){ y += 40; x >>= 4; } /*OPTIMIZATION-IF-TRUE*/ while( x>15 ){ y += 10; x >>= 1; } +#endif } return a[x&7] + y - 10; } -#ifndef SQLITE_OMIT_VIRTUALTABLE /* ** Convert a double into a LogEst ** In other words, compute an approximation for 10*log2(x). @@ -1387,20 +1747,119 @@ LogEst sqlite3LogEstFromDouble(double x){ e = (a>>52) - 1022; return e*10; } -#endif /* SQLITE_OMIT_VIRTUALTABLE */ /* ** Convert a LogEst into an integer. */ u64 sqlite3LogEstToInt(LogEst x){ u64 n; - if( x<10 ) return 1; n = x%10; x /= 10; if( n>=5 ) n -= 2; else if( n>=1 ) n -= 1; - if( x>=3 ){ - return x>60 ? (u64)LARGEST_INT64 : (n+8)<<(x-3); + if( x>60 ) return (u64)LARGEST_INT64; + return x>=3 ? (n+8)<<(x-3) : (n+8)>>(3-x); +} + +/* +** Add a new name/number pair to a VList. This might require that the +** VList object be reallocated, so return the new VList. If an OOM +** error occurs, the original VList returned and the +** db->mallocFailed flag is set. +** +** A VList is really just an array of integers. To destroy a VList, +** simply pass it to sqlite3DbFree(). +** +** The first integer is the number of integers allocated for the whole +** VList. The second integer is the number of integers actually used. +** Each name/number pair is encoded by subsequent groups of 3 or more +** integers. +** +** Each name/number pair starts with two integers which are the numeric +** value for the pair and the size of the name/number pair, respectively. +** The text name overlays one or more following integers. The text name +** is always zero-terminated. +** +** Conceptually: +** +** struct VList { +** int nAlloc; // Number of allocated slots +** int nUsed; // Number of used slots +** struct VListEntry { +** int iValue; // Value for this entry +** int nSlot; // Slots used by this entry +** // ... variable name goes here +** } a[0]; +** } +** +** During code generation, pointers to the variable names within the +** VList are taken. When that happens, nAlloc is set to zero as an +** indication that the VList may never again be enlarged, since the +** accompanying realloc() would invalidate the pointers. +*/ +VList *sqlite3VListAdd( + sqlite3 *db, /* The database connection used for malloc() */ + VList *pIn, /* The input VList. Might be NULL */ + const char *zName, /* Name of symbol to add */ + int nName, /* Bytes of text in zName */ + int iVal /* Value to associate with zName */ +){ + int nInt; /* number of sizeof(int) objects needed for zName */ + char *z; /* Pointer to where zName will be stored */ + int i; /* Index in pIn[] where zName is stored */ + + nInt = nName/4 + 3; + assert( pIn==0 || pIn[0]>=3 ); /* Verify ok to add new elements */ + if( pIn==0 || pIn[1]+nInt > pIn[0] ){ + /* Enlarge the allocation */ + sqlite3_int64 nAlloc = (pIn ? 2*(sqlite3_int64)pIn[0] : 10) + nInt; + VList *pOut = sqlite3DbRealloc(db, pIn, nAlloc*sizeof(int)); + if( pOut==0 ) return pIn; + if( pIn==0 ) pOut[1] = 2; + pIn = pOut; + pIn[0] = nAlloc; } - return (n+8)>>(3-x); + i = pIn[1]; + pIn[i] = iVal; + pIn[i+1] = nInt; + z = (char*)&pIn[i+2]; + pIn[1] = i+nInt; + assert( pIn[1]<=pIn[0] ); + memcpy(z, zName, nName); + z[nName] = 0; + return pIn; +} + +/* +** Return a pointer to the name of a variable in the given VList that +** has the value iVal. Or return a NULL if there is no such variable in +** the list +*/ +const char *sqlite3VListNumToName(VList *pIn, int iVal){ + int i, mx; + if( pIn==0 ) return 0; + mx = pIn[1]; + i = 2; + do{ + if( pIn[i]==iVal ) return (char*)&pIn[i+2]; + i += pIn[i+1]; + }while( i<mx ); + return 0; +} + +/* +** Return the number of the variable named zName, if it is in VList. +** or return 0 if there is no such variable. +*/ +int sqlite3VListNameToNum(VList *pIn, const char *zName, int nName){ + int i, mx; + if( pIn==0 ) return 0; + mx = pIn[1]; + i = 2; + do{ + const char *z = (const char*)&pIn[i+2]; + if( strncmp(z,zName,nName)==0 && z[nName]==0 ) return pIn[i]; + i += pIn[i+1]; + }while( i<mx ); + return 0; } diff --git a/src/vacuum.c b/src/vacuum.c index adc802e60b..8e3ce29eb1 100644 --- a/src/vacuum.c +++ b/src/vacuum.c @@ -18,57 +18,58 @@ #include "vdbeInt.h" #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH) -/* -** Finalize a prepared statement. If there was an error, store the -** text of the error message in *pzErrMsg. Return the result code. -*/ -static int vacuumFinalize(sqlite3 *db, sqlite3_stmt *pStmt, char **pzErrMsg){ - int rc; - rc = sqlite3VdbeFinalize((Vdbe*)pStmt); - if( rc ){ - sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db)); - } - return rc; -} /* -** Execute zSql on database db. Return an error code. +** Execute zSql on database db. +** +** If zSql returns rows, then each row will have exactly one +** column. (This will only happen if zSql begins with "SELECT".) +** Take each row of result and call execSql() again recursively. +** +** The execSqlF() routine does the same thing, except it accepts +** a format string as its third argument */ static int execSql(sqlite3 *db, char **pzErrMsg, const char *zSql){ - sqlite3_stmt *pStmt; - VVA_ONLY( int rc; ) - if( !zSql ){ - return SQLITE_NOMEM; - } - if( SQLITE_OK!=sqlite3_prepare(db, zSql, -1, &pStmt, 0) ){ - sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db)); - return sqlite3_errcode(db); - } - VVA_ONLY( rc = ) sqlite3_step(pStmt); - assert( rc!=SQLITE_ROW || (db->flags&SQLITE_CountRows) ); - return vacuumFinalize(db, pStmt, pzErrMsg); -} - -/* -** Execute zSql on database db. The statement returns exactly -** one column. Execute this as SQL on the same database. -*/ -static int execExecSql(sqlite3 *db, char **pzErrMsg, const char *zSql){ sqlite3_stmt *pStmt; int rc; - rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0); + /* printf("SQL: [%s]\n", zSql); fflush(stdout); */ + rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); if( rc!=SQLITE_OK ) return rc; - - while( SQLITE_ROW==sqlite3_step(pStmt) ){ - rc = execSql(db, pzErrMsg, (char*)sqlite3_column_text(pStmt, 0)); - if( rc!=SQLITE_OK ){ - vacuumFinalize(db, pStmt, pzErrMsg); - return rc; + while( SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){ + const char *zSubSql = (const char*)sqlite3_column_text(pStmt,0); + assert( sqlite3_strnicmp(zSql,"SELECT",6)==0 ); + /* The secondary SQL must be one of CREATE TABLE, CREATE INDEX, + ** or INSERT. Historically there have been attacks that first + ** corrupt the sqlite_schema.sql field with other kinds of statements + ** then run VACUUM to get those statements to execute at inappropriate + ** times. */ + if( zSubSql + && (strncmp(zSubSql,"CRE",3)==0 || strncmp(zSubSql,"INS",3)==0) + ){ + rc = execSql(db, pzErrMsg, zSubSql); + if( rc!=SQLITE_OK ) break; } } - - return vacuumFinalize(db, pStmt, pzErrMsg); + assert( rc!=SQLITE_ROW ); + if( rc==SQLITE_DONE ) rc = SQLITE_OK; + if( rc ){ + sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db)); + } + (void)sqlite3_finalize(pStmt); + return rc; +} +static int execSqlF(sqlite3 *db, char **pzErrMsg, const char *zSql, ...){ + char *z; + va_list ap; + int rc; + va_start(ap, zSql); + z = sqlite3VMPrintf(db, zSql, ap); + va_end(ap); + if( z==0 ) return SQLITE_NOMEM; + rc = execSql(db, pzErrMsg, z); + sqlite3DbFree(db, z); + return rc; } /* @@ -101,117 +102,184 @@ static int execExecSql(sqlite3 *db, char **pzErrMsg, const char *zSql){ ** transient would cause the database file to appear to be deleted ** following reboot. */ -void sqlite3Vacuum(Parse *pParse){ +void sqlite3Vacuum(Parse *pParse, Token *pNm, Expr *pInto){ Vdbe *v = sqlite3GetVdbe(pParse); - if( v ){ - sqlite3VdbeAddOp2(v, OP_Vacuum, 0, 0); - sqlite3VdbeUsesBtree(v, 0); + int iDb = 0; + if( v==0 ) goto build_vacuum_end; + if( pParse->nErr ) goto build_vacuum_end; + if( pNm ){ +#ifndef SQLITE_BUG_COMPATIBLE_20160819 + /* Default behavior: Report an error if the argument to VACUUM is + ** not recognized */ + iDb = sqlite3TwoPartName(pParse, pNm, pNm, &pNm); + if( iDb<0 ) goto build_vacuum_end; +#else + /* When SQLITE_BUG_COMPATIBLE_20160819 is defined, unrecognized arguments + ** to VACUUM are silently ignored. This is a back-out of a bug fix that + ** occurred on 2016-08-19 (https://sqlite.org/src/info/083f9e6270). + ** The buggy behavior is required for binary compatibility with some + ** legacy applications. */ + iDb = sqlite3FindDb(pParse->db, pNm); + if( iDb<0 ) iDb = 0; +#endif } + if( iDb!=1 ){ + int iIntoReg = 0; + if( pInto && sqlite3ResolveSelfReference(pParse,0,0,pInto,0)==0 ){ + iIntoReg = ++pParse->nMem; + sqlite3ExprCode(pParse, pInto, iIntoReg); + } + sqlite3VdbeAddOp2(v, OP_Vacuum, iDb, iIntoReg); + sqlite3VdbeUsesBtree(v, iDb); + } +build_vacuum_end: + sqlite3ExprDelete(pParse->db, pInto); return; } /* ** This routine implements the OP_Vacuum opcode of the VDBE. */ -int sqlite3RunVacuum(char **pzErrMsg, sqlite3 *db){ +SQLITE_NOINLINE int sqlite3RunVacuum( + char **pzErrMsg, /* Write error message here */ + sqlite3 *db, /* Database connection */ + int iDb, /* Which attached DB to vacuum */ + sqlite3_value *pOut /* Write results here, if not NULL. VACUUM INTO */ +){ int rc = SQLITE_OK; /* Return code from service routines */ Btree *pMain; /* The database being vacuumed */ Btree *pTemp; /* The temporary database we vacuum into */ - char *zSql = 0; /* SQL statements */ - int saved_flags; /* Saved value of the db->flags */ - int saved_nChange; /* Saved value of db->nChange */ - int saved_nTotalChange; /* Saved value of db->nTotalChange */ - void (*saved_xTrace)(void*,const char*); /* Saved db->xTrace */ + u32 saved_mDbFlags; /* Saved value of db->mDbFlags */ + u64 saved_flags; /* Saved value of db->flags */ + i64 saved_nChange; /* Saved value of db->nChange */ + i64 saved_nTotalChange; /* Saved value of db->nTotalChange */ + u32 saved_openFlags; /* Saved value of db->openFlags */ + u8 saved_mTrace; /* Saved trace settings */ Db *pDb = 0; /* Database to detach at end of vacuum */ int isMemDb; /* True if vacuuming a :memory: database */ int nRes; /* Bytes of reserved space at the end of each page */ int nDb; /* Number of attached databases */ + const char *zDbMain; /* Schema name of database to vacuum */ + const char *zOut; /* Name of output file */ + u32 pgflags = PAGER_SYNCHRONOUS_OFF; /* sync flags for output db */ + u64 iRandom; /* Random value used for zDbVacuum[] */ + char zDbVacuum[42]; /* Name of the ATTACH-ed database used for vacuum */ + if( !db->autoCommit ){ sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction"); - return SQLITE_ERROR; + return SQLITE_ERROR; /* IMP: R-12218-18073 */ } if( db->nVdbeActive>1 ){ sqlite3SetString(pzErrMsg, db,"cannot VACUUM - SQL statements in progress"); - return SQLITE_ERROR; + return SQLITE_ERROR; /* IMP: R-15610-35227 */ + } + saved_openFlags = db->openFlags; + if( pOut ){ + if( sqlite3_value_type(pOut)!=SQLITE_TEXT ){ + sqlite3SetString(pzErrMsg, db, "non-text filename"); + return SQLITE_ERROR; + } + zOut = (const char*)sqlite3_value_text(pOut); + db->openFlags &= ~SQLITE_OPEN_READONLY; + db->openFlags |= SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE; + }else{ + zOut = ""; } /* Save the current value of the database flags so that it can be ** restored before returning. Then set the writable-schema flag, and ** disable CHECK and foreign key constraints. */ saved_flags = db->flags; + saved_mDbFlags = db->mDbFlags; saved_nChange = db->nChange; saved_nTotalChange = db->nTotalChange; - saved_xTrace = db->xTrace; - db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks | SQLITE_PreferBuiltin; - db->flags &= ~(SQLITE_ForeignKeys | SQLITE_ReverseOrder); - db->xTrace = 0; - - pMain = db->aDb[0].pBt; + saved_mTrace = db->mTrace; + db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks | SQLITE_Comments + | SQLITE_AttachCreate | SQLITE_AttachWrite; + db->mDbFlags |= DBFLAG_PreferBuiltin | DBFLAG_Vacuum; + db->flags &= ~(u64)(SQLITE_ForeignKeys | SQLITE_ReverseOrder + | SQLITE_Defensive | SQLITE_CountRows); + db->mTrace = 0; + + zDbMain = db->aDb[iDb].zDbSName; + pMain = db->aDb[iDb].pBt; isMemDb = sqlite3PagerIsMemdb(sqlite3BtreePager(pMain)); - /* Attach the temporary database as 'vacuum_db'. The synchronous pragma + /* Attach the temporary database as 'vacuum_XXXXXX'. The synchronous pragma ** can be set to 'off' for this file, as it is not recovered if a crash ** occurs anyway. The integrity of the database is maintained by a ** (possibly synchronous) transaction opened on the main database before ** sqlite3BtreeCopyFile() is called. ** - ** An optimisation would be to use a non-journaled pager. - ** (Later:) I tried setting "PRAGMA vacuum_db.journal_mode=OFF" but + ** An optimization would be to use a non-journaled pager. + ** (Later:) I tried setting "PRAGMA vacuum_XXXXXX.journal_mode=OFF" but ** that actually made the VACUUM run slower. Very little journalling ** actually occurs when doing a vacuum since the vacuum_db is initially ** empty. Only the journal header is written. Apparently it takes more ** time to parse and run the PRAGMA to turn journalling off than it does ** to write the journal header file. */ + sqlite3_randomness(sizeof(iRandom),&iRandom); + sqlite3_snprintf(sizeof(zDbVacuum), zDbVacuum, "vacuum_%016llx", iRandom); nDb = db->nDb; - if( sqlite3TempInMemory(db) ){ - zSql = "ATTACH ':memory:' AS vacuum_db;"; - }else{ - zSql = "ATTACH '' AS vacuum_db;"; - } - rc = execSql(db, pzErrMsg, zSql); - if( db->nDb>nDb ){ - pDb = &db->aDb[db->nDb-1]; - assert( strcmp(pDb->zName,"vacuum_db")==0 ); - } + rc = execSqlF(db, pzErrMsg, "ATTACH %Q AS %s", zOut, zDbVacuum); + db->openFlags = saved_openFlags; if( rc!=SQLITE_OK ) goto end_of_vacuum; - pTemp = db->aDb[db->nDb-1].pBt; - - /* The call to execSql() to attach the temp database has left the file - ** locked (as there was more than one active statement when the transaction - ** to read the schema was concluded. Unlock it here so that this doesn't - ** cause problems for the call to BtreeSetPageSize() below. */ - sqlite3BtreeCommit(pTemp); + assert( (db->nDb-1)==nDb ); + pDb = &db->aDb[nDb]; + assert( strcmp(pDb->zDbSName,zDbVacuum)==0 ); + pTemp = pDb->pBt; + if( pOut ){ + sqlite3_file *id = sqlite3PagerFile(sqlite3BtreePager(pTemp)); + i64 sz = 0; + if( id->pMethods!=0 && (sqlite3OsFileSize(id, &sz)!=SQLITE_OK || sz>0) ){ + rc = SQLITE_ERROR; + sqlite3SetString(pzErrMsg, db, "output file already exists"); + goto end_of_vacuum; + } + db->mDbFlags |= DBFLAG_VacuumInto; - nRes = sqlite3BtreeGetOptimalReserve(pMain); + /* For a VACUUM INTO, the pager-flags are set to the same values as + ** they are for the database being vacuumed, except that PAGER_CACHESPILL + ** is always set. */ + pgflags = db->aDb[iDb].safety_level | (db->flags & PAGER_FLAGS_MASK); + } + nRes = sqlite3BtreeGetRequestedReserve(pMain); /* A VACUUM cannot change the pagesize of an encrypted database. */ +/* BEGIN SQLCIPHER */ #ifdef SQLITE_HAS_CODEC if( db->nextPagesize ){ - extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*); + extern void sqlcipherCodecGetKey(sqlite3*, int, void**, int*); + extern void sqlcipher_free(void*, sqlite3_uint64); int nKey; char *zKey; - sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey); + sqlcipherCodecGetKey(db, iDb, (void**)&zKey, &nKey); if( nKey ) db->nextPagesize = 0; + if(nKey) sqlcipher_free(zKey, nKey); } #endif +/* END SQLCIPHER */ - rc = execSql(db, pzErrMsg, "PRAGMA vacuum_db.synchronous=OFF"); - if( rc!=SQLITE_OK ) goto end_of_vacuum; + sqlite3BtreeSetCacheSize(pTemp, db->aDb[iDb].pSchema->cache_size); + sqlite3BtreeSetSpillSize(pTemp, sqlite3BtreeSetSpillSize(pMain,0)); + sqlite3BtreeSetPagerFlags(pTemp, pgflags|PAGER_CACHESPILL); /* Begin a transaction and take an exclusive lock on the main database ** file. This is done before the sqlite3BtreeGetPageSize(pMain) call below, ** to ensure that we do not try to change the page-size on a WAL database. */ - rc = execSql(db, pzErrMsg, "BEGIN;"); + rc = execSql(db, pzErrMsg, "BEGIN"); if( rc!=SQLITE_OK ) goto end_of_vacuum; - rc = sqlite3BtreeBeginTrans(pMain, 2); + rc = sqlite3BtreeBeginTrans(pMain, pOut==0 ? 2 : 0, 0); if( rc!=SQLITE_OK ) goto end_of_vacuum; /* Do not attempt to change the page size for a WAL database */ if( sqlite3PagerGetJournalMode(sqlite3BtreePager(pMain)) - ==PAGER_JOURNALMODE_WAL ){ + ==PAGER_JOURNALMODE_WAL + && pOut==0 + ){ db->nextPagesize = 0; } @@ -219,7 +287,7 @@ int sqlite3RunVacuum(char **pzErrMsg, sqlite3 *db){ || (!isMemDb && sqlite3BtreeSetPageSize(pTemp, db->nextPagesize, nRes, 0)) || NEVER(db->mallocFailed) ){ - rc = SQLITE_NOMEM; + rc = SQLITE_NOMEM_BKPT; goto end_of_vacuum; } @@ -231,64 +299,48 @@ int sqlite3RunVacuum(char **pzErrMsg, sqlite3 *db){ /* Query the schema of the main database. Create a mirror schema ** in the temporary database. */ - rc = execExecSql(db, pzErrMsg, - "SELECT 'CREATE TABLE vacuum_db.' || substr(sql,14) " - " FROM sqlite_master WHERE type='table' AND name!='sqlite_sequence'" - " AND coalesce(rootpage,1)>0" + db->init.iDb = nDb; /* force new CREATE statements into vacuum_db */ + rc = execSqlF(db, pzErrMsg, + "SELECT sql FROM \"%w\".sqlite_schema" + " WHERE type='table'AND name<>'sqlite_sequence'" + " AND coalesce(rootpage,1)>0", + zDbMain ); if( rc!=SQLITE_OK ) goto end_of_vacuum; - rc = execExecSql(db, pzErrMsg, - "SELECT 'CREATE INDEX vacuum_db.' || substr(sql,14)" - " FROM sqlite_master WHERE sql LIKE 'CREATE INDEX %' "); - if( rc!=SQLITE_OK ) goto end_of_vacuum; - rc = execExecSql(db, pzErrMsg, - "SELECT 'CREATE UNIQUE INDEX vacuum_db.' || substr(sql,21) " - " FROM sqlite_master WHERE sql LIKE 'CREATE UNIQUE INDEX %'"); + rc = execSqlF(db, pzErrMsg, + "SELECT sql FROM \"%w\".sqlite_schema" + " WHERE type='index'", + zDbMain + ); if( rc!=SQLITE_OK ) goto end_of_vacuum; + db->init.iDb = 0; /* Loop through the tables in the main database. For each, do ** an "INSERT INTO vacuum_db.xxx SELECT * FROM main.xxx;" to copy ** the contents to the temporary database. */ - assert( (db->flags & SQLITE_Vacuum)==0 ); - db->flags |= SQLITE_Vacuum; - rc = execExecSql(db, pzErrMsg, - "SELECT 'INSERT INTO vacuum_db.' || quote(name) " - "|| ' SELECT * FROM main.' || quote(name) || ';'" - "FROM main.sqlite_master " - "WHERE type = 'table' AND name!='sqlite_sequence' " - " AND coalesce(rootpage,1)>0" + rc = execSqlF(db, pzErrMsg, + "SELECT'INSERT INTO %s.'||quote(name)" + "||' SELECT*FROM\"%w\".'||quote(name)" + "FROM %s.sqlite_schema " + "WHERE type='table'AND coalesce(rootpage,1)>0", + zDbVacuum, zDbMain, zDbVacuum ); - assert( (db->flags & SQLITE_Vacuum)!=0 ); - db->flags &= ~SQLITE_Vacuum; + assert( (db->mDbFlags & DBFLAG_Vacuum)!=0 ); + db->mDbFlags &= ~DBFLAG_Vacuum; if( rc!=SQLITE_OK ) goto end_of_vacuum; - /* Copy over the sequence table - */ - rc = execExecSql(db, pzErrMsg, - "SELECT 'DELETE FROM vacuum_db.' || quote(name) || ';' " - "FROM vacuum_db.sqlite_master WHERE name='sqlite_sequence' " - ); - if( rc!=SQLITE_OK ) goto end_of_vacuum; - rc = execExecSql(db, pzErrMsg, - "SELECT 'INSERT INTO vacuum_db.' || quote(name) " - "|| ' SELECT * FROM main.' || quote(name) || ';' " - "FROM vacuum_db.sqlite_master WHERE name=='sqlite_sequence';" - ); - if( rc!=SQLITE_OK ) goto end_of_vacuum; - - /* Copy the triggers, views, and virtual tables from the main database ** over to the temporary database. None of these objects has any ** associated storage, so all we have to do is copy their entries - ** from the SQLITE_MASTER table. + ** from the schema table. */ - rc = execSql(db, pzErrMsg, - "INSERT INTO vacuum_db.sqlite_master " - " SELECT type, name, tbl_name, rootpage, sql" - " FROM main.sqlite_master" - " WHERE type='view' OR type='trigger'" - " OR (type='table' AND rootpage=0)" + rc = execSqlF(db, pzErrMsg, + "INSERT INTO %s.sqlite_schema" + " SELECT*FROM \"%w\".sqlite_schema" + " WHERE type IN('view','trigger')" + " OR(type='table'AND rootpage=0)", + zDbVacuum, zDbMain ); if( rc ) goto end_of_vacuum; @@ -316,8 +368,8 @@ int sqlite3RunVacuum(char **pzErrMsg, sqlite3 *db){ BTREE_APPLICATION_ID, 0, /* Preserve the application id */ }; - assert( 1==sqlite3BtreeIsInTrans(pTemp) ); - assert( 1==sqlite3BtreeIsInTrans(pMain) ); + assert( SQLITE_TXN_WRITE==sqlite3BtreeTxnState(pTemp) ); + assert( pOut!=0 || SQLITE_TXN_WRITE==sqlite3BtreeTxnState(pMain) ); /* Copy Btree meta values */ for(i=0; i<ArraySize(aCopy); i+=2){ @@ -328,25 +380,34 @@ int sqlite3RunVacuum(char **pzErrMsg, sqlite3 *db){ if( NEVER(rc!=SQLITE_OK) ) goto end_of_vacuum; } - rc = sqlite3BtreeCopyFile(pMain, pTemp); + if( pOut==0 ){ + rc = sqlite3BtreeCopyFile(pMain, pTemp); + } if( rc!=SQLITE_OK ) goto end_of_vacuum; rc = sqlite3BtreeCommit(pTemp); if( rc!=SQLITE_OK ) goto end_of_vacuum; #ifndef SQLITE_OMIT_AUTOVACUUM - sqlite3BtreeSetAutoVacuum(pMain, sqlite3BtreeGetAutoVacuum(pTemp)); + if( pOut==0 ){ + sqlite3BtreeSetAutoVacuum(pMain, sqlite3BtreeGetAutoVacuum(pTemp)); + } #endif } assert( rc==SQLITE_OK ); - rc = sqlite3BtreeSetPageSize(pMain, sqlite3BtreeGetPageSize(pTemp), nRes,1); + if( pOut==0 ){ + nRes = sqlite3BtreeGetRequestedReserve(pTemp); + rc = sqlite3BtreeSetPageSize(pMain, sqlite3BtreeGetPageSize(pTemp), nRes,1); + } end_of_vacuum: /* Restore the original value of db->flags */ + db->init.iDb = 0; + db->mDbFlags = saved_mDbFlags; db->flags = saved_flags; db->nChange = saved_nChange; db->nTotalChange = saved_nTotalChange; - db->xTrace = saved_xTrace; - sqlite3BtreeSetPageSize(pMain, -1, -1, 1); + db->mTrace = saved_mTrace; + sqlite3BtreeSetPageSize(pMain, -1, 0, 1); /* Currently there is an SQL level transaction open on the vacuum ** database. No locks are held on any other files (since the main file diff --git a/src/vdbe.c b/src/vdbe.c index e7c3e48af5..b5a262e636 100644 --- a/src/vdbe.c +++ b/src/vdbe.c @@ -21,6 +21,15 @@ #include "sqliteInt.h" #include "vdbeInt.h" +/* +** High-resolution hardware timer used for debugging and testing only. +*/ +#if defined(VDBE_PROFILE) \ + || defined(SQLITE_PERFORMANCE_TRACE) \ + || defined(SQLITE_ENABLE_STMT_SCANSTATUS) +# include "hwtime.h" +#endif + /* ** Invoke this macro on memory cells just prior to changing the ** value of the cell. This macro verifies that shallow copies are @@ -86,6 +95,16 @@ static void updateMaxBlobsize(Mem *p){ } #endif +/* +** This macro evaluates to true if either the update hook or the preupdate +** hook are enabled for database connect DB. +*/ +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK +# define HAS_UPDATE_HOOK(DB) ((DB)->xPreUpdateCallback||(DB)->xUpdateCallback) +#else +# define HAS_UPDATE_HOOK(DB) ((DB)->xUpdateCallback) +#endif + /* ** The next global variable is incremented each time the OP_Found opcode ** is executed. This is used to test whether or not the foreign key @@ -101,54 +120,114 @@ int sqlite3_found_count = 0; ** Test a register to see if it exceeds the current maximum blob size. ** If it does, record the new maximum blob size. */ -#if defined(SQLITE_TEST) && !defined(SQLITE_OMIT_BUILTIN_TEST) +#if defined(SQLITE_TEST) && !defined(SQLITE_UNTESTABLE) # define UPDATE_MAX_BLOBSIZE(P) updateMaxBlobsize(P) #else # define UPDATE_MAX_BLOBSIZE(P) #endif +#ifdef SQLITE_DEBUG +/* This routine provides a convenient place to set a breakpoint during +** tracing with PRAGMA vdbe_trace=on. The breakpoint fires right after +** each opcode is printed. Variables "pc" (program counter) and pOp are +** available to add conditionals to the breakpoint. GDB example: +** +** break test_trace_breakpoint if pc=22 +** +** Other useful labels for breakpoints include: +** test_addop_breakpoint(pc,pOp) +** sqlite3CorruptError(lineno) +** sqlite3MisuseError(lineno) +** sqlite3CantopenError(lineno) +*/ +static void test_trace_breakpoint(int pc, Op *pOp, Vdbe *v){ + static u64 n = 0; + (void)pc; + (void)pOp; + (void)v; + n++; + if( n==LARGEST_UINT64 ) abort(); /* So that n is used, preventing a warning */ +} +#endif + /* ** Invoke the VDBE coverage callback, if that callback is defined. This ** feature is used for test suite validation only and does not appear an ** production builds. ** -** M is an integer, 2 or 3, that indices how many different ways the -** branch can go. It is usually 2. "I" is the direction the branch -** goes. 0 means falls through. 1 means branch is taken. 2 means the -** second alternative branch is taken. +** M is the type of branch. I is the direction taken for this instance of +** the branch. +** +** M: 2 - two-way branch (I=0: fall-thru 1: jump ) +** 3 - two-way + NULL (I=0: fall-thru 1: jump 2: NULL ) +** 4 - OP_Jump (I=0: jump p1 1: jump p2 2: jump p3) +** +** In other words, if M is 2, then I is either 0 (for fall-through) or +** 1 (for when the branch is taken). If M is 3, the I is 0 for an +** ordinary fall-through, I is 1 if the branch was taken, and I is 2 +** if the result of comparison is NULL. For M=3, I=2 the jump may or +** may not be taken, depending on the SQLITE_JUMPIFNULL flags in p5. +** When M is 4, that means that an OP_Jump is being run. I is 0, 1, or 2 +** depending on if the operands are less than, equal, or greater than. ** ** iSrcLine is the source code line (from the __LINE__ macro) that -** generated the VDBE instruction. This instrumentation assumes that all -** source code is in a single file (the amalgamation). Special values 1 -** and 2 for the iSrcLine parameter mean that this particular branch is -** always taken or never taken, respectively. +** generated the VDBE instruction combined with flag bits. The source +** code line number is in the lower 24 bits of iSrcLine and the upper +** 8 bytes are flags. The lower three bits of the flags indicate +** values for I that should never occur. For example, if the branch is +** always taken, the flags should be 0x05 since the fall-through and +** alternate branch are never taken. If a branch is never taken then +** flags should be 0x06 since only the fall-through approach is allowed. +** +** Bit 0x08 of the flags indicates an OP_Jump opcode that is only +** interested in equal or not-equal. In other words, I==0 and I==2 +** should be treated as equivalent +** +** Since only a line number is retained, not the filename, this macro +** only works for amalgamation builds. But that is ok, since these macros +** should be no-ops except for special builds used to measure test coverage. */ #if !defined(SQLITE_VDBE_COVERAGE) # define VdbeBranchTaken(I,M) #else # define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M) - static void vdbeTakeBranch(int iSrcLine, u8 I, u8 M){ - if( iSrcLine<=2 && ALWAYS(iSrcLine>0) ){ - M = iSrcLine; - /* Assert the truth of VdbeCoverageAlwaysTaken() and - ** VdbeCoverageNeverTaken() */ - assert( (M & I)==I ); - }else{ - if( sqlite3GlobalConfig.xVdbeBranch==0 ) return; /*NO_TEST*/ - sqlite3GlobalConfig.xVdbeBranch(sqlite3GlobalConfig.pVdbeBranchArg, - iSrcLine,I,M); + static void vdbeTakeBranch(u32 iSrcLine, u8 I, u8 M){ + u8 mNever; + assert( I<=2 ); /* 0: fall through, 1: taken, 2: alternate taken */ + assert( M<=4 ); /* 2: two-way branch, 3: three-way branch, 4: OP_Jump */ + assert( I<M ); /* I can only be 2 if M is 3 or 4 */ + /* Transform I from a integer [0,1,2] into a bitmask of [1,2,4] */ + I = 1<<I; + /* The upper 8 bits of iSrcLine are flags. The lower three bits of + ** the flags indicate directions that the branch can never go. If + ** a branch really does go in one of those directions, assert right + ** away. */ + mNever = iSrcLine >> 24; + assert( (I & mNever)==0 ); + if( sqlite3GlobalConfig.xVdbeBranch==0 ) return; /*NO_TEST*/ + /* Invoke the branch coverage callback with three arguments: + ** iSrcLine - the line number of the VdbeCoverage() macro, with + ** flags removed. + ** I - Mask of bits 0x07 indicating which cases are are + ** fulfilled by this instance of the jump. 0x01 means + ** fall-thru, 0x02 means taken, 0x04 means NULL. Any + ** impossible cases (ex: if the comparison is never NULL) + ** are filled in automatically so that the coverage + ** measurement logic does not flag those impossible cases + ** as missed coverage. + ** M - Type of jump. Same as M argument above + */ + I |= mNever; + if( M==2 ) I |= 0x04; + if( M==4 ){ + I |= 0x08; + if( (mNever&0x08)!=0 && (I&0x05)!=0) I |= 0x05; /*NO_TEST*/ } + sqlite3GlobalConfig.xVdbeBranch(sqlite3GlobalConfig.pVdbeBranchArg, + iSrcLine&0xffffff, I, M); } #endif -/* -** Convert the given register into a string if it isn't one -** already. Return non-zero if a malloc() fails. -*/ -#define Stringify(P, enc) \ - if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc,0)) \ - { goto no_mem; } - /* ** An ephemeral string value (signified by the MEM_Ephem flag) contains ** a pointer to a dynamically allocated string where some other entity @@ -175,11 +254,10 @@ static VdbeCursor *allocateCursor( Vdbe *p, /* The virtual machine */ int iCur, /* Index of the new VdbeCursor */ int nField, /* Number of fields in the table or index */ - int iDb, /* Database the cursor belongs to, or -1 */ u8 eCurType /* Type of the new cursor */ ){ /* Find the memory cell that will be used to store the blob of memory - ** required for this VdbeCursor structure. It is convenient to use a + ** required for this VdbeCursor structure. It is convenient to use a ** vdbe memory cell to manage the memory allocation required for a ** VdbeCursor structure for the following reasons: ** @@ -192,39 +270,72 @@ static VdbeCursor *allocateCursor( ** be freed lazily via the sqlite3_release_memory() API. This ** minimizes the number of malloc calls made by the system. ** - ** Memory cells for cursors are allocated at the top of the address - ** space. Memory cell (p->nMem) corresponds to cursor 0. Space for - ** cursor 1 is managed by memory cell (p->nMem-1), etc. + ** The memory cell for cursor 0 is aMem[0]. The rest are allocated from + ** the top of the register space. Cursor 1 is at Mem[p->nMem-1]. + ** Cursor 2 is at Mem[p->nMem-2]. And so forth. */ - Mem *pMem = &p->aMem[p->nMem-iCur]; + Mem *pMem = iCur>0 ? &p->aMem[p->nMem-iCur] : p->aMem; - int nByte; + i64 nByte; VdbeCursor *pCx = 0; - nByte = - ROUND8(sizeof(VdbeCursor)) + 2*sizeof(u32)*nField + - (eCurType==CURTYPE_BTREE?sqlite3BtreeCursorSize():0); + nByte = SZ_VDBECURSOR(nField); + assert( ROUND8(nByte)==nByte ); + if( eCurType==CURTYPE_BTREE ) nByte += sqlite3BtreeCursorSize(); - assert( iCur<p->nCursor ); - if( p->apCsr[iCur] ){ - sqlite3VdbeFreeCursor(p, p->apCsr[iCur]); + assert( iCur>=0 && iCur<p->nCursor ); + if( p->apCsr[iCur] ){ /*OPTIMIZATION-IF-FALSE*/ + sqlite3VdbeFreeCursorNN(p, p->apCsr[iCur]); p->apCsr[iCur] = 0; } - if( SQLITE_OK==sqlite3VdbeMemClearAndResize(pMem, nByte) ){ - p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z; - memset(pCx, 0, sizeof(VdbeCursor)); - pCx->eCurType = eCurType; - pCx->iDb = iDb; - pCx->nField = nField; - pCx->aOffset = &pCx->aType[nField]; - if( eCurType==CURTYPE_BTREE ){ - pCx->uc.pCursor = (BtCursor*) - &pMem->z[ROUND8(sizeof(VdbeCursor))+2*sizeof(u32)*nField]; - sqlite3BtreeCursorZero(pCx->uc.pCursor); + + /* There used to be a call to sqlite3VdbeMemClearAndResize() to make sure + ** the pMem used to hold space for the cursor has enough storage available + ** in pMem->zMalloc. But for the special case of the aMem[] entries used + ** to hold cursors, it is faster to in-line the logic. */ + assert( pMem->flags==MEM_Undefined ); + assert( (pMem->flags & MEM_Dyn)==0 ); + assert( pMem->szMalloc==0 || pMem->z==pMem->zMalloc ); + if( pMem->szMalloc<nByte ){ + if( pMem->szMalloc>0 ){ + sqlite3DbFreeNN(pMem->db, pMem->zMalloc); } + pMem->z = pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, nByte); + if( pMem->zMalloc==0 ){ + pMem->szMalloc = 0; + return 0; + } + pMem->szMalloc = (int)nByte; + } + + p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->zMalloc; + memset(pCx, 0, offsetof(VdbeCursor,pAltCursor)); + pCx->eCurType = eCurType; + pCx->nField = nField; + pCx->aOffset = &pCx->aType[nField]; + if( eCurType==CURTYPE_BTREE ){ + assert( ROUND8(SZ_VDBECURSOR(nField))==SZ_VDBECURSOR(nField) ); + pCx->uc.pCursor = (BtCursor*)&pMem->z[SZ_VDBECURSOR(nField)]; + sqlite3BtreeCursorZero(pCx->uc.pCursor); } return pCx; } +/* +** The string in pRec is known to look like an integer and to have a +** floating point value of rValue. Return true and set *piValue to the +** integer value if the string is in range to be an integer. Otherwise, +** return false. +*/ +static int alsoAnInt(Mem *pRec, double rValue, i64 *piValue){ + i64 iValue; + iValue = sqlite3RealToI64(rValue); + if( sqlite3RealSameAsInt(rValue,iValue) ){ + *piValue = iValue; + return 1; + } + return 0==sqlite3Atoi64(pRec->z, piValue, pRec->n, pRec->enc); +} + /* ** Try to convert a value into a numeric representation if we can ** do so without loss of information. In other words, if the string @@ -242,18 +353,23 @@ static VdbeCursor *allocateCursor( */ static void applyNumericAffinity(Mem *pRec, int bTryForInt){ double rValue; - i64 iValue; u8 enc = pRec->enc; - assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real))==MEM_Str ); - if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return; - if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){ - pRec->u.i = iValue; + int rc; + assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real|MEM_IntReal))==MEM_Str ); + rc = sqlite3AtoF(pRec->z, &rValue, pRec->n, enc); + if( rc<=0 ) return; + if( rc==1 && alsoAnInt(pRec, rValue, &pRec->u.i) ){ pRec->flags |= MEM_Int; }else{ pRec->u.r = rValue; pRec->flags |= MEM_Real; if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec); } + /* TEXT->NUMERIC is many->one. Hence, it is important to invalidate the + ** string representation after computing a numeric equivalent, because the + ** string representation might not be the canonical representation for the + ** numeric value. Ticket [343634942dd54ab57b7024] 2018-01-31. */ + pRec->flags &= ~MEM_Str; } /* @@ -262,16 +378,21 @@ static void applyNumericAffinity(Mem *pRec, int bTryForInt){ ** SQLITE_AFF_INTEGER: ** SQLITE_AFF_REAL: ** SQLITE_AFF_NUMERIC: -** Try to convert pRec to an integer representation or a +** Try to convert pRec to an integer representation or a ** floating-point representation if an integer representation ** is not possible. Note that the integer representation is ** always preferred, even if the affinity is REAL, because ** an integer representation is more space efficient on disk. ** +** SQLITE_AFF_FLEXNUM: +** If the value is text, then try to convert it into a number of +** some kind (integer or real) but do not make any other changes. +** ** SQLITE_AFF_TEXT: ** Convert pRec to a text representation. ** ** SQLITE_AFF_BLOB: +** SQLITE_AFF_NONE: ** No-op. pRec is unchanged. */ static void applyAffinity( @@ -281,23 +402,29 @@ static void applyAffinity( ){ if( affinity>=SQLITE_AFF_NUMERIC ){ assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL - || affinity==SQLITE_AFF_NUMERIC ); - if( (pRec->flags & MEM_Int)==0 ){ - if( (pRec->flags & MEM_Real)==0 ){ + || affinity==SQLITE_AFF_NUMERIC || affinity==SQLITE_AFF_FLEXNUM ); + if( (pRec->flags & MEM_Int)==0 ){ /*OPTIMIZATION-IF-FALSE*/ + if( (pRec->flags & (MEM_Real|MEM_IntReal))==0 ){ if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1); - }else{ + }else if( affinity<=SQLITE_AFF_REAL ){ sqlite3VdbeIntegerAffinity(pRec); } } }else if( affinity==SQLITE_AFF_TEXT ){ /* Only attempt the conversion to TEXT if there is an integer or real ** representation (blob and NULL do not get converted) but no string - ** representation. - */ - if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){ - sqlite3VdbeMemStringify(pRec, enc, 1); + ** representation. It would be harmless to repeat the conversion if + ** there is already a string rep, but it is pointless to waste those + ** CPU cycles. */ + if( 0==(pRec->flags&MEM_Str) ){ /*OPTIMIZATION-IF-FALSE*/ + if( (pRec->flags&(MEM_Real|MEM_Int|MEM_IntReal)) ){ + testcase( pRec->flags & MEM_Int ); + testcase( pRec->flags & MEM_Real ); + testcase( pRec->flags & MEM_IntReal ); + sqlite3VdbeMemStringify(pRec, enc, 1); + } } - pRec->flags &= ~(MEM_Real|MEM_Int); + pRec->flags &= ~(MEM_Real|MEM_Int|MEM_IntReal); } } @@ -318,12 +445,12 @@ int sqlite3_value_numeric_type(sqlite3_value *pVal){ } /* -** Exported version of applyAffinity(). This one works on sqlite3_value*, +** Exported version of applyAffinity(). This one works on sqlite3_value*, ** not the internal Mem* type. */ void sqlite3ValueApplyAffinity( - sqlite3_value *pVal, - u8 affinity, + sqlite3_value *pVal, + u8 affinity, u8 enc ){ applyAffinity((Mem *)pVal, affinity, enc); @@ -336,12 +463,24 @@ void sqlite3ValueApplyAffinity( ** accordingly. */ static u16 SQLITE_NOINLINE computeNumericType(Mem *pMem){ - assert( (pMem->flags & (MEM_Int|MEM_Real))==0 ); + int rc; + sqlite3_int64 ix; + assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal))==0 ); assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 ); - if( sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc)==0 ){ - return 0; + if( ExpandBlob(pMem) ){ + pMem->u.i = 0; + return MEM_Int; } - if( sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc)==SQLITE_OK ){ + rc = sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc); + if( rc<=0 ){ + if( rc==0 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)<=1 ){ + pMem->u.i = ix; + return MEM_Int; + }else{ + return MEM_Real; + } + }else if( rc==1 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)==0 ){ + pMem->u.i = ix; return MEM_Int; } return MEM_Real; @@ -349,18 +488,24 @@ static u16 SQLITE_NOINLINE computeNumericType(Mem *pMem){ /* ** Return the numeric type for pMem, either MEM_Int or MEM_Real or both or -** none. +** none. ** ** Unlike applyNumericAffinity(), this routine does not modify pMem->flags. ** But it does set pMem->u.r and pMem->u.i appropriately. */ static u16 numericType(Mem *pMem){ - if( pMem->flags & (MEM_Int|MEM_Real) ){ - return pMem->flags & (MEM_Int|MEM_Real); - } - if( pMem->flags & (MEM_Str|MEM_Blob) ){ - return computeNumericType(pMem); - } + assert( (pMem->flags & MEM_Null)==0 + || pMem->db==0 || pMem->db->mallocFailed ); + if( pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null) ){ + testcase( pMem->flags & MEM_Int ); + testcase( pMem->flags & MEM_Real ); + testcase( pMem->flags & MEM_IntReal ); + return pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null); + } + assert( pMem->flags & (MEM_Str|MEM_Blob) ); + testcase( pMem->flags & MEM_Str ); + testcase( pMem->flags & MEM_Blob ); + return computeNumericType(pMem); return 0; } @@ -369,12 +514,9 @@ static u16 numericType(Mem *pMem){ ** Write a nice string representation of the contents of cell pMem ** into buffer zBuf, length nBuf. */ -void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){ - char *zCsr = zBuf; +void sqlite3VdbeMemPrettyPrint(Mem *pMem, StrAccum *pStr){ int f = pMem->flags; - static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"}; - if( f&MEM_Blob ){ int i; char c; @@ -390,59 +532,43 @@ void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){ }else{ c = 's'; } - - sqlite3_snprintf(100, zCsr, "%c", c); - zCsr += sqlite3Strlen30(zCsr); - sqlite3_snprintf(100, zCsr, "%d[", pMem->n); - zCsr += sqlite3Strlen30(zCsr); - for(i=0; i<16 && i<pMem->n; i++){ - sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF)); - zCsr += sqlite3Strlen30(zCsr); + sqlite3_str_appendf(pStr, "%cx[", c); + for(i=0; i<25 && i<pMem->n; i++){ + sqlite3_str_appendf(pStr, "%02X", ((int)pMem->z[i] & 0xFF)); } - for(i=0; i<16 && i<pMem->n; i++){ + sqlite3_str_appendf(pStr, "|"); + for(i=0; i<25 && i<pMem->n; i++){ char z = pMem->z[i]; - if( z<32 || z>126 ) *zCsr++ = '.'; - else *zCsr++ = z; + sqlite3_str_appendchar(pStr, 1, (z<32||z>126)?'.':z); } - - sqlite3_snprintf(100, zCsr, "]%s", encnames[pMem->enc]); - zCsr += sqlite3Strlen30(zCsr); + sqlite3_str_appendf(pStr,"]"); if( f & MEM_Zero ){ - sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero); - zCsr += sqlite3Strlen30(zCsr); + sqlite3_str_appendf(pStr, "+%dz",pMem->u.nZero); } - *zCsr = '\0'; }else if( f & MEM_Str ){ - int j, k; - zBuf[0] = ' '; + int j; + u8 c; if( f & MEM_Dyn ){ - zBuf[1] = 'z'; + c = 'z'; assert( (f & (MEM_Static|MEM_Ephem))==0 ); }else if( f & MEM_Static ){ - zBuf[1] = 't'; + c = 't'; assert( (f & (MEM_Dyn|MEM_Ephem))==0 ); }else if( f & MEM_Ephem ){ - zBuf[1] = 'e'; + c = 'e'; assert( (f & (MEM_Static|MEM_Dyn))==0 ); }else{ - zBuf[1] = 's'; - } - k = 2; - sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n); - k += sqlite3Strlen30(&zBuf[k]); - zBuf[k++] = '['; - for(j=0; j<15 && j<pMem->n; j++){ - u8 c = pMem->z[j]; - if( c>=0x20 && c<0x7f ){ - zBuf[k++] = c; - }else{ - zBuf[k++] = '.'; - } + c = 's'; + } + sqlite3_str_appendf(pStr, " %c%d[", c, pMem->n); + for(j=0; j<25 && j<pMem->n; j++){ + c = pMem->z[j]; + sqlite3_str_appendchar(pStr, 1, (c>=0x20&&c<=0x7f) ? c : '.'); + } + sqlite3_str_appendf(pStr, "]%s", encnames[pMem->enc]); + if( f & MEM_Term ){ + sqlite3_str_appendf(pStr, "(0-term)"); } - zBuf[k++] = ']'; - sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]); - k += sqlite3Strlen30(&zBuf[k]); - zBuf[k++] = 0; } } #endif @@ -455,55 +581,70 @@ static void memTracePrint(Mem *p){ if( p->flags & MEM_Undefined ){ printf(" undefined"); }else if( p->flags & MEM_Null ){ - printf(" NULL"); + printf(p->flags & MEM_Zero ? " NULL-nochng" : " NULL"); }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){ printf(" si:%lld", p->u.i); + }else if( (p->flags & (MEM_IntReal))!=0 ){ + printf(" ir:%lld", p->u.i); }else if( p->flags & MEM_Int ){ printf(" i:%lld", p->u.i); #ifndef SQLITE_OMIT_FLOATING_POINT }else if( p->flags & MEM_Real ){ - printf(" r:%g", p->u.r); + printf(" r:%.17g", p->u.r); #endif - }else if( p->flags & MEM_RowSet ){ + }else if( sqlite3VdbeMemIsRowSet(p) ){ printf(" (rowset)"); }else{ - char zBuf[200]; - sqlite3VdbeMemPrettyPrint(p, zBuf); - printf(" %s", zBuf); + StrAccum acc; + char zBuf[1000]; + sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0); + sqlite3VdbeMemPrettyPrint(p, &acc); + printf(" %s", sqlite3StrAccumFinish(&acc)); } if( p->flags & MEM_Subtype ) printf(" subtype=0x%02x", p->eSubtype); } static void registerTrace(int iReg, Mem *p){ - printf("REG[%d] = ", iReg); + printf("R[%d] = ", iReg); memTracePrint(p); + if( p->pScopyFrom ){ + assert( p->pScopyFrom->bScopy ); + printf(" <== R[%d]", (int)(p->pScopyFrom - &p[-iReg])); + } + printf("\n"); + sqlite3VdbeCheckMemInvariants(p); +} +/**/ void sqlite3PrintMem(Mem *pMem){ + memTracePrint(pMem); printf("\n"); + fflush(stdout); } #endif +#ifdef SQLITE_DEBUG +/* +** Show the values of all registers in the virtual machine. Used for +** interactive debugging. +*/ +void sqlite3VdbeRegisterDump(Vdbe *v){ + int i; + for(i=1; i<v->nMem; i++) registerTrace(i, v->aMem+i); +} +#endif /* SQLITE_DEBUG */ + + #ifdef SQLITE_DEBUG # define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M) #else # define REGISTER_TRACE(R,M) #endif - -#ifdef VDBE_PROFILE - -/* -** hwtime.h contains inline assembler code for implementing -** high-performance timing routines. -*/ -#include "hwtime.h" - -#endif - #ifndef NDEBUG /* ** This function is only called from within an assert() expression. It ** checks that the sqlite3.nTransaction variable is correctly set to -** the number of non-transaction savepoints currently in the +** the number of non-transaction savepoints currently in the ** linked list starting at sqlite3.pSavepoint. -** +** ** Usage: ** ** assert( checkSavepointCount(db) ); @@ -529,10 +670,10 @@ static SQLITE_NOINLINE Mem *out2PrereleaseWithClear(Mem *pOut){ static Mem *out2Prerelease(Vdbe *p, VdbeOp *pOp){ Mem *pOut; assert( pOp->p2>0 ); - assert( pOp->p2<=(p->nMem-p->nCursor) ); + assert( pOp->p2<=(p->nMem+1 - p->nCursor) ); pOut = &p->aMem[pOp->p2]; memAboutToChange(p, pOut); - if( VdbeMemDynamic(pOut) ){ + if( VdbeMemDynamic(pOut) ){ /*OPTIMIZATION-IF-FALSE*/ return out2PrereleaseWithClear(pOut); }else{ pOut->flags = MEM_Int; @@ -540,66 +681,224 @@ static Mem *out2Prerelease(Vdbe *p, VdbeOp *pOp){ } } +/* +** Compute a bloom filter hash using pOp->p4.i registers from aMem[] beginning +** with pOp->p3. Return the hash. +*/ +static u64 filterHash(const Mem *aMem, const Op *pOp){ + int i, mx; + u64 h = 0; + + assert( pOp->p4type==P4_INT32 ); + for(i=pOp->p3, mx=i+pOp->p4.i; i<mx; i++){ + const Mem *p = &aMem[i]; + if( p->flags & (MEM_Int|MEM_IntReal) ){ + h += p->u.i; + }else if( p->flags & MEM_Real ){ + h += sqlite3VdbeIntValue(p); + }else if( p->flags & (MEM_Str|MEM_Blob) ){ + /* All strings have the same hash and all blobs have the same hash, + ** though, at least, those hashes are different from each other and + ** from NULL. */ + h += 4093 + (p->flags & (MEM_Str|MEM_Blob)); + } + } + return h; +} + + +/* +** For OP_Column, factor out the case where content is loaded from +** overflow pages, so that the code to implement this case is separate +** the common case where all content fits on the page. Factoring out +** the code reduces register pressure and helps the common case +** to run faster. +*/ +static SQLITE_NOINLINE int vdbeColumnFromOverflow( + VdbeCursor *pC, /* The BTree cursor from which we are reading */ + int iCol, /* The column to read */ + u32 t, /* The serial-type code for the column value */ + i64 iOffset, /* Offset to the start of the content value */ + u32 cacheStatus, /* Current Vdbe.cacheCtr value */ + u32 colCacheCtr, /* Current value of the column cache counter */ + Mem *pDest /* Store the value into this register. */ +){ + int rc; + sqlite3 *db = pDest->db; + int encoding = pDest->enc; + int len = sqlite3VdbeSerialTypeLen(t); + assert( pC->eCurType==CURTYPE_BTREE ); + if( len>db->aLimit[SQLITE_LIMIT_LENGTH] ) return SQLITE_TOOBIG; + if( len > 4000 && pC->pKeyInfo==0 ){ + /* Cache large column values that are on overflow pages using + ** an RCStr (reference counted string) so that if they are reloaded, + ** that do not have to be copied a second time. The overhead of + ** creating and managing the cache is such that this is only + ** profitable for larger TEXT and BLOB values. + ** + ** Only do this on table-btrees so that writes to index-btrees do not + ** need to clear the cache. This buys performance in the common case + ** in exchange for generality. + */ + VdbeTxtBlbCache *pCache; + char *pBuf; + if( pC->colCache==0 ){ + pC->pCache = sqlite3DbMallocZero(db, sizeof(VdbeTxtBlbCache) ); + if( pC->pCache==0 ) return SQLITE_NOMEM; + pC->colCache = 1; + } + pCache = pC->pCache; + if( pCache->pCValue==0 + || pCache->iCol!=iCol + || pCache->cacheStatus!=cacheStatus + || pCache->colCacheCtr!=colCacheCtr + || pCache->iOffset!=sqlite3BtreeOffset(pC->uc.pCursor) + ){ + if( pCache->pCValue ) sqlite3RCStrUnref(pCache->pCValue); + pBuf = pCache->pCValue = sqlite3RCStrNew( len+3 ); + if( pBuf==0 ) return SQLITE_NOMEM; + rc = sqlite3BtreePayload(pC->uc.pCursor, iOffset, len, pBuf); + if( rc ) return rc; + pBuf[len] = 0; + pBuf[len+1] = 0; + pBuf[len+2] = 0; + pCache->iCol = iCol; + pCache->cacheStatus = cacheStatus; + pCache->colCacheCtr = colCacheCtr; + pCache->iOffset = sqlite3BtreeOffset(pC->uc.pCursor); + }else{ + pBuf = pCache->pCValue; + } + assert( t>=12 ); + sqlite3RCStrRef(pBuf); + if( t&1 ){ + rc = sqlite3VdbeMemSetStr(pDest, pBuf, len, encoding, + sqlite3RCStrUnref); + pDest->flags |= MEM_Term; + }else{ + rc = sqlite3VdbeMemSetStr(pDest, pBuf, len, 0, + sqlite3RCStrUnref); + } + }else{ + rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, iOffset, len, pDest); + if( rc ) return rc; + sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest); + if( (t&1)!=0 && encoding==SQLITE_UTF8 ){ + pDest->z[len] = 0; + pDest->flags |= MEM_Term; + } + } + pDest->flags &= ~MEM_Ephem; + return rc; +} + +/* +** Send a "statement aborts" message to the error log. +*/ +static SQLITE_NOINLINE void sqlite3VdbeLogAbort( + Vdbe *p, /* The statement that is running at the time of failure */ + int rc, /* Error code */ + Op *pOp, /* Opcode that filed */ + Op *aOp /* All opcodes */ +){ + const char *zSql = p->zSql; /* Original SQL text */ + const char *zPrefix = ""; /* Prefix added to SQL text */ + int pc; /* Opcode address */ + char zXtra[100]; /* Buffer space to store zPrefix */ + + if( p->pFrame ){ + assert( aOp[0].opcode==OP_Init ); + if( aOp[0].p4.z!=0 ){ + assert( aOp[0].p4.z[0]=='-' + && aOp[0].p4.z[1]=='-' + && aOp[0].p4.z[2]==' ' ); + sqlite3_snprintf(sizeof(zXtra), zXtra,"/* %s */ ",aOp[0].p4.z+3); + zPrefix = zXtra; + }else{ + zPrefix = "/* unknown trigger */ "; + } + } + pc = (int)(pOp - aOp); + sqlite3_log(rc, "statement aborts at %d: %s; [%s%s]", + pc, p->zErrMsg, zPrefix, zSql); +} + +/* +** Return the symbolic name for the data type of a pMem +*/ +static const char *vdbeMemTypeName(Mem *pMem){ + static const char *azTypes[] = { + /* SQLITE_INTEGER */ "INT", + /* SQLITE_FLOAT */ "REAL", + /* SQLITE_TEXT */ "TEXT", + /* SQLITE_BLOB */ "BLOB", + /* SQLITE_NULL */ "NULL" + }; + return azTypes[sqlite3_value_type(pMem)-1]; +} /* ** Execute as much of a VDBE program as we can. -** This is the core of sqlite3_step(). +** This is the core of sqlite3_step(). */ int sqlite3VdbeExec( Vdbe *p /* The VDBE */ ){ Op *aOp = p->aOp; /* Copy of p->aOp */ Op *pOp = aOp; /* Current operation */ -#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) - Op *pOrigOp; /* Value of pOp at the top of the loop */ -#endif #ifdef SQLITE_DEBUG + Op *pOrigOp; /* Value of pOp at the top of the loop */ int nExtraDelete = 0; /* Verifies FORDELETE and AUXDELETE flags */ + u8 iCompareIsInit = 0; /* iCompare is initialized */ #endif int rc = SQLITE_OK; /* Value to return */ sqlite3 *db = p->db; /* The database */ u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */ u8 encoding = ENC(db); /* The database encoding */ - int iCompare = 0; /* Result of last OP_Compare operation */ - unsigned nVmStep = 0; /* Number of virtual machine steps */ + int iCompare = 0; /* Result of last comparison */ + u64 nVmStep = 0; /* Number of virtual machine steps */ #ifndef SQLITE_OMIT_PROGRESS_CALLBACK - unsigned nProgressLimit = 0;/* Invoke xProgress() when nVmStep reaches this */ + u64 nProgressLimit; /* Invoke xProgress() when nVmStep reaches this */ #endif Mem *aMem = p->aMem; /* Copy of p->aMem */ Mem *pIn1 = 0; /* 1st input operand */ Mem *pIn2 = 0; /* 2nd input operand */ Mem *pIn3 = 0; /* 3rd input operand */ Mem *pOut = 0; /* Output operand */ - int *aPermute = 0; /* Permutation of columns for OP_Compare */ - i64 lastRowid = db->lastRowid; /* Saved value of the last insert ROWID */ -#ifdef VDBE_PROFILE - u64 start; /* CPU clock count at start of opcode */ + u32 colCacheCtr = 0; /* Column cache counter */ +#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || defined(VDBE_PROFILE) + u64 *pnCycle = 0; + int bStmtScanStatus = IS_STMT_SCANSTATUS(db)!=0; #endif /*** INSERT STACK UNION HERE ***/ - assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */ - sqlite3VdbeEnter(p); + assert( p->eVdbeState==VDBE_RUN_STATE ); /* sqlite3_step() verifies this */ + if( DbMaskNonZero(p->lockMask) ){ + sqlite3VdbeEnter(p); + } +#ifndef SQLITE_OMIT_PROGRESS_CALLBACK + if( db->xProgress ){ + u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP]; + assert( 0 < db->nProgressOps ); + nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps); + }else{ + nProgressLimit = LARGEST_UINT64; + } +#endif if( p->rc==SQLITE_NOMEM ){ /* This happens if a malloc() inside a call to sqlite3_column_text() or ** sqlite3_column_text16() failed. */ goto no_mem; } assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY ); - assert( p->bIsReader || p->readOnly!=0 ); + testcase( p->rc!=SQLITE_OK ); p->rc = SQLITE_OK; + assert( p->bIsReader || p->readOnly!=0 ); p->iCurrentTime = 0; assert( p->explain==0 ); - p->pResultSet = 0; db->busyHandler.nBusy = 0; - if( db->u1.isInterrupted ) goto abort_due_to_interrupt; + if( AtomicLoad(&db->u1.isInterrupted) ) goto abort_due_to_interrupt; sqlite3VdbeIOTraceSql(p); -#ifndef SQLITE_OMIT_PROGRESS_CALLBACK - if( db->xProgress ){ - u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP]; - assert( 0 < db->nProgressOps ); - nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps); - } -#endif #ifdef SQLITE_DEBUG sqlite3BeginBenignMalloc(); if( p->pc==0 @@ -627,14 +926,24 @@ int sqlite3VdbeExec( } sqlite3EndBenignMalloc(); #endif - for(pOp=&aOp[p->pc]; rc==SQLITE_OK; pOp++){ + for(pOp=&aOp[p->pc]; 1; pOp++){ + /* Errors are detected by individual opcodes, with an immediate + ** jumps to abort_due_to_error. */ + assert( rc==SQLITE_OK ); + assert( pOp>=aOp && pOp<&aOp[p->nOp]); -#ifdef VDBE_PROFILE - start = sqlite3Hwtime(); -#endif nVmStep++; -#ifdef SQLITE_ENABLE_STMT_SCANSTATUS - if( p->anExec ) p->anExec[(int)(pOp-aOp)]++; + +#if defined(VDBE_PROFILE) + pOp->nExec++; + pnCycle = &pOp->nCycle; + if( sqlite3NProfileCnt==0 ) *pnCycle -= sqlite3Hwtime(); +#elif defined(SQLITE_ENABLE_STMT_SCANSTATUS) + if( bStmtScanStatus ){ + pOp->nExec++; + pnCycle = &pOp->nCycle; + *pnCycle -= sqlite3Hwtime(); + } #endif /* Only allow tracing if SQLITE_DEBUG is defined. @@ -642,9 +951,10 @@ int sqlite3VdbeExec( #ifdef SQLITE_DEBUG if( db->flags & SQLITE_VdbeTrace ){ sqlite3VdbePrintOp(stdout, (int)(pOp - aOp), pOp); + test_trace_breakpoint((int)(pOp - aOp),pOp,p); } #endif - + /* Check to see if we need to simulate an interrupt. This only happens ** if we have a special test build. @@ -660,43 +970,45 @@ int sqlite3VdbeExec( /* Sanity checking on other operands */ #ifdef SQLITE_DEBUG - assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] ); - if( (pOp->opflags & OPFLG_IN1)!=0 ){ - assert( pOp->p1>0 ); - assert( pOp->p1<=(p->nMem-p->nCursor) ); - assert( memIsValid(&aMem[pOp->p1]) ); - assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) ); - REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]); - } - if( (pOp->opflags & OPFLG_IN2)!=0 ){ - assert( pOp->p2>0 ); - assert( pOp->p2<=(p->nMem-p->nCursor) ); - assert( memIsValid(&aMem[pOp->p2]) ); - assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) ); - REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]); - } - if( (pOp->opflags & OPFLG_IN3)!=0 ){ - assert( pOp->p3>0 ); - assert( pOp->p3<=(p->nMem-p->nCursor) ); - assert( memIsValid(&aMem[pOp->p3]) ); - assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) ); - REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]); - } - if( (pOp->opflags & OPFLG_OUT2)!=0 ){ - assert( pOp->p2>0 ); - assert( pOp->p2<=(p->nMem-p->nCursor) ); - memAboutToChange(p, &aMem[pOp->p2]); - } - if( (pOp->opflags & OPFLG_OUT3)!=0 ){ - assert( pOp->p3>0 ); - assert( pOp->p3<=(p->nMem-p->nCursor) ); - memAboutToChange(p, &aMem[pOp->p3]); + { + u8 opProperty = sqlite3OpcodeProperty[pOp->opcode]; + if( (opProperty & OPFLG_IN1)!=0 ){ + assert( pOp->p1>0 ); + assert( pOp->p1<=(p->nMem+1 - p->nCursor) ); + assert( memIsValid(&aMem[pOp->p1]) ); + assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) ); + REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]); + } + if( (opProperty & OPFLG_IN2)!=0 ){ + assert( pOp->p2>0 ); + assert( pOp->p2<=(p->nMem+1 - p->nCursor) ); + assert( memIsValid(&aMem[pOp->p2]) ); + assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) ); + REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]); + } + if( (opProperty & OPFLG_IN3)!=0 ){ + assert( pOp->p3>0 ); + assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); + assert( memIsValid(&aMem[pOp->p3]) ); + assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) ); + REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]); + } + if( (opProperty & OPFLG_OUT2)!=0 ){ + assert( pOp->p2>0 ); + assert( pOp->p2<=(p->nMem+1 - p->nCursor) ); + memAboutToChange(p, &aMem[pOp->p2]); + } + if( (opProperty & OPFLG_OUT3)!=0 ){ + assert( pOp->p3>0 ); + assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); + memAboutToChange(p, &aMem[pOp->p3]); + } } #endif -#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) +#ifdef SQLITE_DEBUG pOrigOp = pOp; #endif - + switch( pOp->opcode ){ /***************************************************************************** @@ -737,7 +1049,7 @@ int sqlite3VdbeExec( /* Opcode: Goto * P2 * * * ** ** An unconditional jump to address P2. -** The next instruction executed will be +** The next instruction executed will be ** the one at index P2 from the beginning of ** the program. ** @@ -747,13 +1059,27 @@ int sqlite3VdbeExec( ** to the current line should be indented for EXPLAIN output. */ case OP_Goto: { /* jump */ + +#ifdef SQLITE_DEBUG + /* In debugging mode, when the p5 flags is set on an OP_Goto, that + ** means we should really jump back to the preceding OP_ReleaseReg + ** instruction. */ + if( pOp->p5 ){ + assert( pOp->p2 < (int)(pOp - aOp) ); + assert( pOp->p2 > 1 ); + pOp = &aOp[pOp->p2 - 2]; + assert( pOp[1].opcode==OP_ReleaseReg ); + goto check_for_interrupt; + } +#endif + jump_to_p2_and_check_for_interrupt: pOp = &aOp[pOp->p2 - 1]; /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev, - ** OP_VNext, OP_RowSetNext, or OP_SorterNext) all jump here upon + ** OP_VNext, or OP_SorterNext) all jump here upon ** completion. Check to see if sqlite3_interrupt() has been called - ** or if the progress callback needs to be invoked. + ** or if the progress callback needs to be invoked. ** ** This code uses unstructured "goto" statements and does not look clean. ** But that is not due to sloppy coding habits. The code is written this @@ -761,7 +1087,7 @@ case OP_Goto: { /* jump */ ** checks on every opcode. This helps sqlite3_step() to run about 1.5% ** faster according to "valgrind --tool=cachegrind" */ check_for_interrupt: - if( db->u1.isInterrupted ) goto abort_due_to_interrupt; + if( AtomicLoad(&db->u1.isInterrupted) ) goto abort_due_to_interrupt; #ifndef SQLITE_OMIT_PROGRESS_CALLBACK /* Call the progress callback if it is configured and the required number ** of VDBE ops have been executed (either since this invocation of @@ -769,16 +1095,17 @@ case OP_Goto: { /* jump */ ** If the progress callback returns non-zero, exit the virtual machine with ** a return code SQLITE_ABORT. */ - if( db->xProgress!=0 && nVmStep>=nProgressLimit ){ + while( nVmStep>=nProgressLimit && db->xProgress!=0 ){ assert( db->nProgressOps!=0 ); - nProgressLimit = nVmStep + db->nProgressOps - (nVmStep%db->nProgressOps); + nProgressLimit += db->nProgressOps; if( db->xProgress(db->pProgressArg) ){ + nProgressLimit = LARGEST_UINT64; rc = SQLITE_INTERRUPT; - goto vdbe_error_halt; + goto abort_due_to_error; } } #endif - + break; } @@ -788,31 +1115,46 @@ case OP_Goto: { /* jump */ ** and then jump to address P2. */ case OP_Gosub: { /* jump */ - assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) ); + assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); pIn1 = &aMem[pOp->p1]; assert( VdbeMemDynamic(pIn1)==0 ); memAboutToChange(p, pIn1); pIn1->flags = MEM_Int; pIn1->u.i = (int)(pOp-aOp); REGISTER_TRACE(pOp->p1, pIn1); - - /* Most jump operations do a goto to this spot in order to update - ** the pOp pointer. */ -jump_to_p2: - pOp = &aOp[pOp->p2 - 1]; - break; + goto jump_to_p2_and_check_for_interrupt; } -/* Opcode: Return P1 * * * * +/* Opcode: Return P1 P2 P3 * * +** +** Jump to the address stored in register P1. If P1 is a return address +** register, then this accomplishes a return from a subroutine. +** +** If P3 is 1, then the jump is only taken if register P1 holds an integer +** values, otherwise execution falls through to the next opcode, and the +** OP_Return becomes a no-op. If P3 is 0, then register P1 must hold an +** integer or else an assert() is raised. P3 should be set to 1 when +** this opcode is used in combination with OP_BeginSubrtn, and set to 0 +** otherwise. +** +** The value in register P1 is unchanged by this opcode. ** -** Jump to the next instruction after the address in register P1. After -** the jump, register P1 becomes undefined. +** P2 is not used by the byte-code engine. However, if P2 is positive +** and also less than the current address, then the "EXPLAIN" output +** formatter in the CLI will indent all opcodes from the P2 opcode up +** to be not including the current Return. P2 should be the first opcode +** in the subroutine from which this opcode is returning. Thus the P2 +** value is a byte-code indentation hint. See tag-20220407a in +** wherecode.c and shell.c. */ case OP_Return: { /* in1 */ pIn1 = &aMem[pOp->p1]; - assert( pIn1->flags==MEM_Int ); - pOp = &aOp[pIn1->u.i]; - pIn1->flags = MEM_Undefined; + if( pIn1->flags & MEM_Int ){ + if( pOp->p3 ){ VdbeBranchTaken(1, 2); } + pOp = &aOp[pIn1->u.i]; + }else if( ALWAYS(pOp->p3) ){ + VdbeBranchTaken(0, 2); + } break; } @@ -827,15 +1169,22 @@ case OP_Return: { /* in1 */ ** ** See also: EndCoroutine */ -case OP_InitCoroutine: { /* jump */ - assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) ); +case OP_InitCoroutine: { /* jump0 */ + assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); assert( pOp->p2>=0 && pOp->p2<p->nOp ); assert( pOp->p3>=0 && pOp->p3<p->nOp ); pOut = &aMem[pOp->p1]; assert( !VdbeMemDynamic(pOut) ); pOut->u.i = pOp->p3 - 1; pOut->flags = MEM_Int; - if( pOp->p2 ) goto jump_to_p2; + if( pOp->p2==0 ) break; + + /* Most jump operations do a goto to this spot in order to update + ** the pOp pointer. */ +jump_to_p2: + assert( pOp->p2>0 ); /* There are never any jumps to instruction 0 */ + assert( pOp->p2<p->nOp ); /* Jumps must be in range */ + pOp = &aOp[pOp->p2 - 1]; break; } @@ -843,7 +1192,9 @@ case OP_InitCoroutine: { /* jump */ ** ** The instruction at the address in register P1 is a Yield. ** Jump to the P2 parameter of that Yield. -** After the jump, register P1 becomes undefined. +** After the jump, the value register P1 is left with a value +** such that subsequent OP_Yields go back to the this same +** OP_EndCoroutine instruction. ** ** See also: InitCoroutine */ @@ -855,8 +1206,8 @@ case OP_EndCoroutine: { /* in1 */ pCaller = &aOp[pIn1->u.i]; assert( pCaller->opcode==OP_Yield ); assert( pCaller->p2>=0 && pCaller->p2<p->nOp ); + pIn1->u.i = (int)(pOp - p->aOp) - 1; pOp = &aOp[pCaller->p2 - 1]; - pIn1->flags = MEM_Undefined; break; } @@ -873,7 +1224,7 @@ case OP_EndCoroutine: { /* in1 */ ** ** See also: InitCoroutine */ -case OP_Yield: { /* in1, jump */ +case OP_Yield: { /* in1, jump0 */ int pcDest; pIn1 = &aMem[pOp->p1]; assert( VdbeMemDynamic(pIn1)==0 ); @@ -886,7 +1237,7 @@ case OP_Yield: { /* in1, jump */ } /* Opcode: HaltIfNull P1 P2 P3 P4 P5 -** Synopsis: if r[P3]=null halt +** Synopsis: if r[P3]=null halt ** ** Check the value in register P3. If it is NULL then Halt using ** parameter P1, P2, and P4 as if this were a Halt instruction. If the @@ -895,11 +1246,15 @@ case OP_Yield: { /* in1, jump */ */ case OP_HaltIfNull: { /* in3 */ pIn3 = &aMem[pOp->p3]; +#ifdef SQLITE_DEBUG + if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); } +#endif if( (pIn3->flags & MEM_Null)==0 ) break; /* Fall through into OP_Halt */ + /* no break */ deliberate_fall_through } -/* Opcode: Halt P1 P2 * P4 P5 +/* Opcode: Halt P1 P2 P3 P4 P5 ** ** Exit immediately. All open cursors, etc are closed ** automatically. @@ -910,42 +1265,54 @@ case OP_HaltIfNull: { /* in3 */ ** whether or not to rollback the current transaction. Do not rollback ** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort, ** then back out all changes that have occurred during this execution of the -** VDBE, but do not rollback the transaction. +** VDBE, but do not rollback the transaction. ** -** If P4 is not null then it is an error message string. +** If P3 is not zero and P4 is NULL, then P3 is a register that holds the +** text of an error message. ** -** P5 is a value between 0 and 4, inclusive, that modifies the P4 string. +** If P3 is zero and P4 is not null then the error message string is held +** in P4. ** -** 0: (no change) -** 1: NOT NULL contraint failed: P4 +** P5 is a value between 1 and 4, inclusive, then the P4 error message +** string is modified as follows: +** +** 1: NOT NULL constraint failed: P4 ** 2: UNIQUE constraint failed: P4 ** 3: CHECK constraint failed: P4 ** 4: FOREIGN KEY constraint failed: P4 ** -** If P5 is not zero and P4 is NULL, then everything after the ":" is -** omitted. +** If P3 is zero and P5 is not zero and P4 is NULL, then everything after +** the ":" is omitted. ** ** There is an implied "Halt 0 0 0" instruction inserted at the very end of ** every program. So a jump past the last instruction of the program ** is the same as executing Halt. */ case OP_Halt: { - const char *zType; - const char *zLogFmt; VdbeFrame *pFrame; int pcx; - pcx = (int)(pOp - aOp); - if( pOp->p1==SQLITE_OK && p->pFrame ){ +#ifdef SQLITE_DEBUG + if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); } +#endif + assert( pOp->p4type==P4_NOTUSED + || pOp->p4type==P4_STATIC + || pOp->p4type==P4_DYNAMIC ); + + /* A deliberately coded "OP_Halt SQLITE_INTERNAL * * * *" opcode indicates + ** something is wrong with the code generator. Raise an assertion in order + ** to bring this to the attention of fuzzers and other testing tools. */ + assert( pOp->p1!=SQLITE_INTERNAL ); + + if( p->pFrame && pOp->p1==SQLITE_OK ){ /* Halt the sub-program. Return control to the parent frame. */ pFrame = p->pFrame; p->pFrame = pFrame->pParent; p->nFrame--; sqlite3VdbeSetChanges(db, p->nChange); pcx = sqlite3VdbeFrameRestore(pFrame); - lastRowid = db->lastRowid; if( pOp->p2==OE_Ignore ){ - /* Instruction pcx is the OP_Program that invoked the sub-program + /* Instruction pcx is the OP_Program that invoked the sub-program ** currently being halted. If the p2 instruction of this OP_Halt ** instruction is set to OE_Ignore, then the sub-program is throwing ** an IGNORE exception. In this case jump to the address specified @@ -959,35 +1326,33 @@ case OP_Halt: { } p->rc = pOp->p1; p->errorAction = (u8)pOp->p2; - p->pc = pcx; + assert( pOp->p5<=4 ); if( p->rc ){ - if( pOp->p5 ){ + if( pOp->p3>0 && pOp->p4type==P4_NOTUSED ){ + const char *zErr; + assert( pOp->p3<=(p->nMem + 1 - p->nCursor) ); + zErr = sqlite3ValueText(&aMem[pOp->p3], SQLITE_UTF8); + sqlite3VdbeError(p, "%s", zErr); + }else if( pOp->p5 ){ static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK", "FOREIGN KEY" }; - assert( pOp->p5>=1 && pOp->p5<=4 ); testcase( pOp->p5==1 ); testcase( pOp->p5==2 ); testcase( pOp->p5==3 ); testcase( pOp->p5==4 ); - zType = azType[pOp->p5-1]; + sqlite3VdbeError(p, "%s constraint failed", azType[pOp->p5-1]); + if( pOp->p4.z ){ + p->zErrMsg = sqlite3MPrintf(db, "%z: %s", p->zErrMsg, pOp->p4.z); + } }else{ - zType = 0; - } - assert( zType!=0 || pOp->p4.z!=0 ); - zLogFmt = "abort at %d in [%s]: %s"; - if( zType && pOp->p4.z ){ - sqlite3VdbeError(p, "%s constraint failed: %s", zType, pOp->p4.z); - }else if( pOp->p4.z ){ sqlite3VdbeError(p, "%s", pOp->p4.z); - }else{ - sqlite3VdbeError(p, "%s constraint failed", zType); } - sqlite3_log(pOp->p1, zLogFmt, pcx, p->zSql, p->zErrMsg); + sqlite3VdbeLogAbort(p, pOp->p1, pOp, aOp); } rc = sqlite3VdbeHalt(p); assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR ); if( rc==SQLITE_BUSY ){ - p->rc = rc = SQLITE_BUSY; + p->rc = SQLITE_BUSY; }else{ assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ); assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 ); @@ -1039,7 +1404,7 @@ case OP_Real: { /* same as TK_FLOAT, out2 */ /* Opcode: String8 * P2 * P4 * ** Synopsis: r[P2]='P4' ** -** P4 points to a nul terminated UTF-8 string. This opcode is transformed +** P4 points to a nul terminated UTF-8 string. This opcode is transformed ** into a String opcode before it is executed for the first time. During ** this transformation, the length of string P4 is computed and stored ** as the P1 parameter. @@ -1047,13 +1412,13 @@ case OP_Real: { /* same as TK_FLOAT, out2 */ case OP_String8: { /* same as TK_STRING, out2 */ assert( pOp->p4.z!=0 ); pOut = out2Prerelease(p, pOp); - pOp->opcode = OP_String; pOp->p1 = sqlite3Strlen30(pOp->p4.z); #ifndef SQLITE_OMIT_UTF16 if( encoding!=SQLITE_UTF8 ){ rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC); - if( rc==SQLITE_TOOBIG ) goto too_big; + assert( rc==SQLITE_OK || rc==SQLITE_TOOBIG ); + if( rc ) goto too_big; if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem; assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z ); assert( VdbeMemDynamic(pOut)==0 ); @@ -1070,18 +1435,23 @@ case OP_String8: { /* same as TK_STRING, out2 */ if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){ goto too_big; } + pOp->opcode = OP_String; + assert( rc==SQLITE_OK ); /* Fall through to the next case, OP_String */ + /* no break */ deliberate_fall_through } - + /* Opcode: String P1 P2 P3 P4 P5 ** Synopsis: r[P2]='P4' (len=P1) ** ** The string value P4 of length P1 (bytes) is stored in register P2. ** -** If P5!=0 and the content of register P3 is greater than zero, then +** If P3 is not zero and the content of register P3 is equal to P5, then ** the datatype of the register P2 is converted to BLOB. The content is ** the same sequence of bytes, it is merely interpreted as a BLOB instead -** of a string, as if it had been CAST. +** of a string, as if it had been CAST. In other words: +** +** if( P3!=0 and reg[P3]==P5 ) reg[P2] := CAST(reg[P2] as BLOB) */ case OP_String: { /* out2 */ assert( pOp->p4.z!=0 ); @@ -1092,19 +1462,40 @@ case OP_String: { /* out2 */ pOut->enc = encoding; UPDATE_MAX_BLOBSIZE(pOut); #ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS - if( pOp->p5 ){ - assert( pOp->p3>0 ); - assert( pOp->p3<=(p->nMem-p->nCursor) ); + if( pOp->p3>0 ){ + assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); pIn3 = &aMem[pOp->p3]; assert( pIn3->flags & MEM_Int ); - if( pIn3->u.i ) pOut->flags = MEM_Blob|MEM_Static|MEM_Term; + if( pIn3->u.i==pOp->p5 ) pOut->flags = MEM_Blob|MEM_Static|MEM_Term; } #endif break; } +/* Opcode: BeginSubrtn * P2 * * * +** Synopsis: r[P2]=NULL +** +** Mark the beginning of a subroutine that can be entered in-line +** or that can be called using OP_Gosub. The subroutine should +** be terminated by an OP_Return instruction that has a P1 operand that +** is the same as the P2 operand to this opcode and that has P3 set to 1. +** If the subroutine is entered in-line, then the OP_Return will simply +** fall through. But if the subroutine is entered using OP_Gosub, then +** the OP_Return will jump back to the first instruction after the OP_Gosub. +** +** This routine works by loading a NULL into the P2 register. When the +** return address register contains a NULL, the OP_Return instruction is +** a no-op that simply falls through to the next instruction (assuming that +** the OP_Return opcode has a P3 value of 1). Thus if the subroutine is +** entered in-line, then the OP_Return will cause in-line execution to +** continue. But if the subroutine is entered via OP_Gosub, then the +** OP_Return will cause a return to the address following the OP_Gosub. +** +** This opcode is identical to OP_Null. It has a different name +** only to make the byte code easier to read and verify. +*/ /* Opcode: Null P1 P2 P3 * * -** Synopsis: r[P2..P3]=NULL +** Synopsis: r[P2..P3]=NULL ** ** Write a NULL into registers P2. If P3 greater than P2, then also write ** NULL into register P3 and every register in between P2 and P3. If P3 @@ -1115,25 +1506,31 @@ case OP_String: { /* out2 */ ** NULL values will not compare equal even if SQLITE_NULLEQ is set on ** OP_Ne or OP_Eq. */ +case OP_BeginSubrtn: case OP_Null: { /* out2 */ int cnt; u16 nullFlag; pOut = out2Prerelease(p, pOp); cnt = pOp->p3-pOp->p2; - assert( pOp->p3<=(p->nMem-p->nCursor) ); + assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null; + pOut->n = 0; +#ifdef SQLITE_DEBUG + pOut->uTemp = 0; +#endif while( cnt>0 ){ pOut++; memAboutToChange(p, pOut); sqlite3VdbeMemSetNull(pOut); pOut->flags = nullFlag; + pOut->n = 0; cnt--; } break; } /* Opcode: SoftNull P1 * * * * -** Synopsis: r[P1]=NULL +** Synopsis: r[P1]=NULL ** ** Set register P1 to have the value NULL as seen by the OP_MakeRecord ** instruction, but do not free any string or blob memory associated with @@ -1141,9 +1538,9 @@ case OP_Null: { /* out2 */ ** previously copied using OP_SCopy, the copies will continue to be valid. */ case OP_SoftNull: { - assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) ); + assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); pOut = &aMem[pOp->p1]; - pOut->flags = (pOut->flags|MEM_Null)&~MEM_Undefined; + pOut->flags = (pOut->flags&~(MEM_Undefined|MEM_AffMask))|MEM_Null; break; } @@ -1151,42 +1548,47 @@ case OP_SoftNull: { ** Synopsis: r[P2]=P4 (len=P1) ** ** P4 points to a blob of data P1 bytes long. Store this -** blob in register P2. +** blob in register P2. If P4 is a NULL pointer, then construct +** a zero-filled blob that is P1 bytes long in P2. */ case OP_Blob: { /* out2 */ assert( pOp->p1 <= SQLITE_MAX_LENGTH ); pOut = out2Prerelease(p, pOp); - sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0); + if( pOp->p4.z==0 ){ + sqlite3VdbeMemSetZeroBlob(pOut, pOp->p1); + if( sqlite3VdbeMemExpandBlob(pOut) ) goto no_mem; + }else{ + sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0); + } pOut->enc = encoding; UPDATE_MAX_BLOBSIZE(pOut); break; } -/* Opcode: Variable P1 P2 * P4 * -** Synopsis: r[P2]=parameter(P1,P4) +/* Opcode: Variable P1 P2 * * * +** Synopsis: r[P2]=parameter(P1) ** ** Transfer the values of bound parameter P1 into register P2 -** -** If the parameter is named, then its name appears in P4. -** The P4 value is used by sqlite3_bind_parameter_name(). */ case OP_Variable: { /* out2 */ Mem *pVar; /* Value being transferred */ assert( pOp->p1>0 && pOp->p1<=p->nVar ); - assert( pOp->p4.z==0 || pOp->p4.z==p->azVar[pOp->p1-1] ); pVar = &p->aVar[pOp->p1 - 1]; if( sqlite3VdbeMemTooBig(pVar) ){ goto too_big; } - pOut = out2Prerelease(p, pOp); - sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static); + pOut = &aMem[pOp->p2]; + if( VdbeMemDynamic(pOut) ) sqlite3VdbeMemSetNull(pOut); + memcpy(pOut, pVar, MEMCELLSIZE); + pOut->flags &= ~(MEM_Dyn|MEM_Ephem); + pOut->flags |= MEM_Static|MEM_FromBind; UPDATE_MAX_BLOBSIZE(pOut); break; } /* Opcode: Move P1 P2 P3 * * -** Synopsis: r[P2@P3]=r[P1@P3] +** Synopsis: r[P2@P3]=r[P1@P3] ** ** Move the P3 values in register P1..P1+P3-1 over into ** registers P2..P2+P3-1. Registers P1..P1+P3-1 are @@ -1208,14 +1610,20 @@ case OP_Move: { pIn1 = &aMem[p1]; pOut = &aMem[p2]; do{ - assert( pOut<=&aMem[(p->nMem-p->nCursor)] ); - assert( pIn1<=&aMem[(p->nMem-p->nCursor)] ); + assert( pOut<=&aMem[(p->nMem+1 - p->nCursor)] ); + assert( pIn1<=&aMem[(p->nMem+1 - p->nCursor)] ); assert( memIsValid(pIn1) ); memAboutToChange(p, pOut); sqlite3VdbeMemMove(pOut, pIn1); #ifdef SQLITE_DEBUG - if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<pOut ){ - pOut->pScopyFrom += pOp->p2 - p1; + pIn1->pScopyFrom = 0; + { int i; + for(i=1; i<p->nMem; i++){ + if( aMem[i].pScopyFrom==pIn1 ){ + assert( aMem[i].bScopy ); + aMem[i].pScopyFrom = pOut; + } + } } #endif Deephemeralize(pOut); @@ -1226,11 +1634,16 @@ case OP_Move: { break; } -/* Opcode: Copy P1 P2 P3 * * +/* Opcode: Copy P1 P2 P3 * P5 ** Synopsis: r[P2@P3+1]=r[P1@P3+1] ** ** Make a copy of registers P1..P1+P3 into registers P2..P2+P3. ** +** If the 0x0002 bit of P5 is set then also clear the MEM_Subtype flag in the +** destination. The 0x0001 bit of P5 indicates that this Copy opcode cannot +** be merged. The 0x0001 bit is used by the query planner and does not +** come into play during query execution. +** ** This instruction makes a deep copy of the value. A duplicate ** is made of any string or blob constant. See also OP_SCopy. */ @@ -1242,8 +1655,12 @@ case OP_Copy: { pOut = &aMem[pOp->p2]; assert( pOut!=pIn1 ); while( 1 ){ + memAboutToChange(p, pOut); sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem); Deephemeralize(pOut); + if( (pOut->flags & MEM_Subtype)!=0 && (pOp->p5 & 0x0002)!=0 ){ + pOut->flags &= ~MEM_Subtype; + } #ifdef SQLITE_DEBUG pOut->pScopyFrom = 0; #endif @@ -1274,7 +1691,9 @@ case OP_SCopy: { /* out2 */ assert( pOut!=pIn1 ); sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem); #ifdef SQLITE_DEBUG - if( pOut->pScopyFrom==0 ) pOut->pScopyFrom = pIn1; + pOut->pScopyFrom = pIn1; + pOut->mScopyFlags = pIn1->flags; + pIn1->bScopy = 1; #endif break; } @@ -1295,8 +1714,26 @@ case OP_IntCopy: { /* out2 */ break; } +/* Opcode: FkCheck * * * * * +** +** Halt with an SQLITE_CONSTRAINT error if there are any unresolved +** foreign key constraint violations. If there are no foreign key +** constraint violations, this is a no-op. +** +** FK constraint violations are also checked when the prepared statement +** exits. This opcode is used to raise foreign key constraint errors prior +** to returning results such as a row change count or the result of a +** RETURNING clause. +*/ +case OP_FkCheck: { + if( (rc = sqlite3VdbeCheckFkImmediate(p))!=SQLITE_OK ){ + goto abort_due_to_error; + } + break; +} + /* Opcode: ResultRow P1 P2 * * * -** Synopsis: output=r[P1@P2] +** Synopsis: output=r[P1@P2] ** ** The registers P1 through P1+P2-1 contain a single row of ** results. This opcode causes the sqlite3_step() call to terminate @@ -1305,74 +1742,32 @@ case OP_IntCopy: { /* out2 */ ** the result row. */ case OP_ResultRow: { - Mem *pMem; - int i; assert( p->nResColumn==pOp->p2 ); - assert( pOp->p1>0 ); - assert( pOp->p1+pOp->p2<=(p->nMem-p->nCursor)+1 ); - -#ifndef SQLITE_OMIT_PROGRESS_CALLBACK - /* Run the progress counter just before returning. - */ - if( db->xProgress!=0 - && nVmStep>=nProgressLimit - && db->xProgress(db->pProgressArg)!=0 - ){ - rc = SQLITE_INTERRUPT; - goto vdbe_error_halt; - } -#endif - - /* If this statement has violated immediate foreign key constraints, do - ** not return the number of rows modified. And do not RELEASE the statement - ** transaction. It needs to be rolled back. */ - if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){ - assert( db->flags&SQLITE_CountRows ); - assert( p->usesStmtJournal ); - break; - } - - /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then - ** DML statements invoke this opcode to return the number of rows - ** modified to the user. This is the only way that a VM that - ** opens a statement transaction may invoke this opcode. - ** - ** In case this is such a statement, close any statement transaction - ** opened by this VM before returning control to the user. This is to - ** ensure that statement-transactions are always nested, not overlapping. - ** If the open statement-transaction is not closed here, then the user - ** may step another VM that opens its own statement transaction. This - ** may lead to overlapping statement transactions. - ** - ** The statement transaction is never a top-level transaction. Hence - ** the RELEASE call below can never fail. - */ - assert( p->iStatement==0 || db->flags&SQLITE_CountRows ); - rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE); - if( NEVER(rc!=SQLITE_OK) ){ - break; - } + assert( pOp->p1>0 || CORRUPT_DB ); + assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 ); - /* Invalidate all ephemeral cursor row caches */ p->cacheCtr = (p->cacheCtr + 2)|1; - - /* Make sure the results of the current row are \000 terminated - ** and have an assigned type. The results are de-ephemeralized as - ** a side effect. - */ - pMem = p->pResultSet = &aMem[pOp->p1]; - for(i=0; i<pOp->p2; i++){ - assert( memIsValid(&pMem[i]) ); - Deephemeralize(&pMem[i]); - assert( (pMem[i].flags & MEM_Ephem)==0 - || (pMem[i].flags & (MEM_Str|MEM_Blob))==0 ); - sqlite3VdbeMemNulTerminate(&pMem[i]); - REGISTER_TRACE(pOp->p1+i, &pMem[i]); + p->pResultRow = &aMem[pOp->p1]; +#ifdef SQLITE_DEBUG + { + Mem *pMem = p->pResultRow; + int i; + for(i=0; i<pOp->p2; i++){ + assert( memIsValid(&pMem[i]) ); + REGISTER_TRACE(pOp->p1+i, &pMem[i]); + /* The registers in the result will not be used again when the + ** prepared statement restarts. This is because sqlite3_column() + ** APIs might have caused type conversions of made other changes to + ** the register values. Therefore, we can go ahead and break any + ** OP_SCopy dependencies. */ + pMem[i].pScopyFrom = 0; + } } +#endif if( db->mallocFailed ) goto no_mem; - - /* Return SQLITE_ROW - */ + if( db->mTrace & SQLITE_TRACE_ROW ){ + db->trace.xV2(SQLITE_TRACE_ROW, db->pTraceArg, p, 0); + } p->pc = (int)(pOp - aOp) + 1; rc = SQLITE_ROW; goto vdbe_return; @@ -1392,31 +1787,58 @@ case OP_ResultRow: { ** to avoid a memcpy(). */ case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */ - i64 nByte; + i64 nByte; /* Total size of the output string or blob */ + u16 flags1; /* Initial flags for P1 */ + u16 flags2; /* Initial flags for P2 */ pIn1 = &aMem[pOp->p1]; pIn2 = &aMem[pOp->p2]; pOut = &aMem[pOp->p3]; + testcase( pOut==pIn2 ); assert( pIn1!=pOut ); - if( (pIn1->flags | pIn2->flags) & MEM_Null ){ + flags1 = pIn1->flags; + testcase( flags1 & MEM_Null ); + testcase( pIn2->flags & MEM_Null ); + if( (flags1 | pIn2->flags) & MEM_Null ){ sqlite3VdbeMemSetNull(pOut); break; } - if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem; - Stringify(pIn1, encoding); - Stringify(pIn2, encoding); - nByte = pIn1->n + pIn2->n; + if( (flags1 & (MEM_Str|MEM_Blob))==0 ){ + if( sqlite3VdbeMemStringify(pIn1,encoding,0) ) goto no_mem; + flags1 = pIn1->flags & ~MEM_Str; + }else if( (flags1 & MEM_Zero)!=0 ){ + if( sqlite3VdbeMemExpandBlob(pIn1) ) goto no_mem; + flags1 = pIn1->flags & ~MEM_Str; + } + flags2 = pIn2->flags; + if( (flags2 & (MEM_Str|MEM_Blob))==0 ){ + if( sqlite3VdbeMemStringify(pIn2,encoding,0) ) goto no_mem; + flags2 = pIn2->flags & ~MEM_Str; + }else if( (flags2 & MEM_Zero)!=0 ){ + if( sqlite3VdbeMemExpandBlob(pIn2) ) goto no_mem; + flags2 = pIn2->flags & ~MEM_Str; + } + nByte = pIn1->n; + nByte += pIn2->n; if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){ goto too_big; } +#if SQLITE_MAX_LENGTH>2147483645 + if( nByte>2147483645 ){ goto too_big; } +#endif if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){ goto no_mem; } MemSetTypeFlag(pOut, MEM_Str); if( pOut!=pIn2 ){ memcpy(pOut->z, pIn2->z, pIn2->n); + assert( (pIn2->flags & MEM_Dyn) == (flags2 & MEM_Dyn) ); + pIn2->flags = flags2; } memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n); + assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) ); + pIn1->flags = flags1; + if( encoding>SQLITE_UTF8 ) nByte &= ~1; pOut->z[nByte]=0; pOut->z[nByte+1] = 0; pOut->flags |= MEM_Term; @@ -1427,14 +1849,14 @@ case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */ } /* Opcode: Add P1 P2 P3 * * -** Synopsis: r[P3]=r[P1]+r[P2] +** Synopsis: r[P3]=r[P1]+r[P2] ** ** Add the value in register P1 to the value in register P2 ** and store the result in register P3. ** If either input is NULL, the result is NULL. */ /* Opcode: Multiply P1 P2 P3 * * -** Synopsis: r[P3]=r[P1]*r[P2] +** Synopsis: r[P3]=r[P1]*r[P2] ** ** ** Multiply the value in register P1 by the value in register P2 @@ -1442,25 +1864,25 @@ case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */ ** If either input is NULL, the result is NULL. */ /* Opcode: Subtract P1 P2 P3 * * -** Synopsis: r[P3]=r[P2]-r[P1] +** Synopsis: r[P3]=r[P2]-r[P1] ** ** Subtract the value in register P1 from the value in register P2 ** and store the result in register P3. ** If either input is NULL, the result is NULL. */ /* Opcode: Divide P1 P2 P3 * * -** Synopsis: r[P3]=r[P2]/r[P1] +** Synopsis: r[P3]=r[P2]/r[P1] ** ** Divide the value in register P1 by the value in register P2 -** and store the result in register P3 (P3=P2/P1). If the value in -** register P1 is zero, then the result is NULL. If either input is +** and store the result in register P3 (P3=P2/P1). If the value in +** register P1 is zero, then the result is NULL. If either input is ** NULL, the result is NULL. */ /* Opcode: Remainder P1 P2 P3 * * -** Synopsis: r[P3]=r[P2]%r[P1] +** Synopsis: r[P3]=r[P2]%r[P1] ** -** Compute the remainder after integer register P2 is divided by -** register P1 and store the result in register P3. +** Compute the remainder after integer register P2 is divided by +** register P1 and store the result in register P3. ** If the value in register P1 is zero the result is NULL. ** If either operand is NULL, the result is NULL. */ @@ -1469,8 +1891,6 @@ case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */ case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */ case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */ case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */ - char bIntint; /* Started out as two integer operands */ - u16 flags; /* Combined MEM_* flags from both inputs */ u16 type1; /* Numeric type of left operand */ u16 type2; /* Numeric type of right operand */ i64 iA; /* Integer value of left operand */ @@ -1479,16 +1899,14 @@ case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */ double rB; /* Real value of right operand */ pIn1 = &aMem[pOp->p1]; - type1 = numericType(pIn1); + type1 = pIn1->flags; pIn2 = &aMem[pOp->p2]; - type2 = numericType(pIn2); + type2 = pIn2->flags; pOut = &aMem[pOp->p3]; - flags = pIn1->flags | pIn2->flags; - if( (flags & MEM_Null)!=0 ) goto arithmetic_result_is_null; if( (type1 & type2 & MEM_Int)!=0 ){ +int_math: iA = pIn1->u.i; iB = pIn2->u.i; - bIntint = 1; switch( pOp->opcode ){ case OP_Add: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break; case OP_Subtract: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break; @@ -1508,8 +1926,12 @@ case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */ } pOut->u.i = iB; MemSetTypeFlag(pOut, MEM_Int); + }else if( ((type1 | type2) & MEM_Null)!=0 ){ + goto arithmetic_result_is_null; }else{ - bIntint = 0; + type1 = numericType(pIn1); + type2 = numericType(pIn2); + if( (type1 & type2 & MEM_Int)!=0 ) goto int_math; fp_math: rA = sqlite3VdbeRealValue(pIn1); rB = sqlite3VdbeRealValue(pIn2); @@ -1524,8 +1946,8 @@ case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */ break; } default: { - iA = (i64)rA; - iB = (i64)rB; + iA = sqlite3VdbeIntValue(pIn1); + iB = sqlite3VdbeIntValue(pIn2); if( iA==0 ) goto arithmetic_result_is_null; if( iA==-1 ) iA = 1; rB = (double)(iB % iA); @@ -1541,9 +1963,6 @@ case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */ } pOut->u.r = rB; MemSetTypeFlag(pOut, MEM_Real); - if( ((type1|type2)&MEM_Real)==0 && !bIntint ){ - sqlite3VdbeIntegerAffinity(pOut); - } #endif } break; @@ -1555,7 +1974,7 @@ case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */ /* Opcode: CollSeq P1 * * P4 ** -** P4 is a pointer to a CollSeq struct. If the next call to a user function +** P4 is a pointer to a CollSeq object. If the next call to a user function ** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will ** be returned. This is used by the built-in min(), max() and nullif() ** functions. @@ -1576,134 +1995,22 @@ case OP_CollSeq: { break; } -/* Opcode: Function0 P1 P2 P3 P4 P5 -** Synopsis: r[P3]=func(r[P2@P5]) -** -** Invoke a user function (P4 is a pointer to a FuncDef object that -** defines the function) with P5 arguments taken from register P2 and -** successors. The result of the function is stored in register P3. -** Register P3 must not be one of the function inputs. -** -** P1 is a 32-bit bitmask indicating whether or not each argument to the -** function was determined to be constant at compile time. If the first -** argument was constant then bit 0 of P1 is set. This is used to determine -** whether meta data associated with a user function argument using the -** sqlite3_set_auxdata() API may be safely retained until the next -** invocation of this opcode. -** -** See also: Function, AggStep, AggFinal -*/ -/* Opcode: Function P1 P2 P3 P4 P5 -** Synopsis: r[P3]=func(r[P2@P5]) -** -** Invoke a user function (P4 is a pointer to an sqlite3_context object that -** contains a pointer to the function to be run) with P5 arguments taken -** from register P2 and successors. The result of the function is stored -** in register P3. Register P3 must not be one of the function inputs. -** -** P1 is a 32-bit bitmask indicating whether or not each argument to the -** function was determined to be constant at compile time. If the first -** argument was constant then bit 0 of P1 is set. This is used to determine -** whether meta data associated with a user function argument using the -** sqlite3_set_auxdata() API may be safely retained until the next -** invocation of this opcode. -** -** SQL functions are initially coded as OP_Function0 with P4 pointing -** to a FuncDef object. But on first evaluation, the P4 operand is -** automatically converted into an sqlite3_context object and the operation -** changed to this OP_Function opcode. In this way, the initialization of -** the sqlite3_context object occurs only once, rather than once for each -** evaluation of the function. -** -** See also: Function0, AggStep, AggFinal -*/ -case OP_Function0: { - int n; - sqlite3_context *pCtx; - - assert( pOp->p4type==P4_FUNCDEF ); - n = pOp->p5; - assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) ); - assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem-p->nCursor)+1) ); - assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n ); - pCtx = sqlite3DbMallocRawNN(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*)); - if( pCtx==0 ) goto no_mem; - pCtx->pOut = 0; - pCtx->pFunc = pOp->p4.pFunc; - pCtx->iOp = (int)(pOp - aOp); - pCtx->pVdbe = p; - pCtx->argc = n; - pOp->p4type = P4_FUNCCTX; - pOp->p4.pCtx = pCtx; - pOp->opcode = OP_Function; - /* Fall through into OP_Function */ -} -case OP_Function: { - int i; - sqlite3_context *pCtx; - - assert( pOp->p4type==P4_FUNCCTX ); - pCtx = pOp->p4.pCtx; - - /* If this function is inside of a trigger, the register array in aMem[] - ** might change from one evaluation to the next. The next block of code - ** checks to see if the register array has changed, and if so it - ** reinitializes the relavant parts of the sqlite3_context object */ - pOut = &aMem[pOp->p3]; - if( pCtx->pOut != pOut ){ - pCtx->pOut = pOut; - for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i]; - } - - memAboutToChange(p, pCtx->pOut); -#ifdef SQLITE_DEBUG - for(i=0; i<pCtx->argc; i++){ - assert( memIsValid(pCtx->argv[i]) ); - REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]); - } -#endif - MemSetTypeFlag(pCtx->pOut, MEM_Null); - pCtx->fErrorOrAux = 0; - db->lastRowid = lastRowid; - (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */ - lastRowid = db->lastRowid; /* Remember rowid changes made by xSFunc */ - - /* If the function returned an error, throw an exception */ - if( pCtx->fErrorOrAux ){ - if( pCtx->isError ){ - sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut)); - rc = pCtx->isError; - } - sqlite3VdbeDeleteAuxData(p, pCtx->iOp, pOp->p1); - } - - /* Copy the result of the function into register P3 */ - if( pOut->flags & (MEM_Str|MEM_Blob) ){ - sqlite3VdbeChangeEncoding(pCtx->pOut, encoding); - if( sqlite3VdbeMemTooBig(pCtx->pOut) ) goto too_big; - } - - REGISTER_TRACE(pOp->p3, pCtx->pOut); - UPDATE_MAX_BLOBSIZE(pCtx->pOut); - break; -} - -/* Opcode: BitAnd P1 P2 P3 * * -** Synopsis: r[P3]=r[P1]&r[P2] +/* Opcode: BitAnd P1 P2 P3 * * +** Synopsis: r[P3]=r[P1]&r[P2] ** ** Take the bit-wise AND of the values in register P1 and P2 and ** store the result in register P3. ** If either input is NULL, the result is NULL. */ /* Opcode: BitOr P1 P2 P3 * * -** Synopsis: r[P3]=r[P1]|r[P2] +** Synopsis: r[P3]=r[P1]|r[P2] ** ** Take the bit-wise OR of the values in register P1 and P2 and ** store the result in register P3. ** If either input is NULL, the result is NULL. */ /* Opcode: ShiftLeft P1 P2 P3 * * -** Synopsis: r[P3]=r[P2]<<r[P1] +** Synopsis: r[P3]=r[P2]<<r[P1] ** ** Shift the integer value in register P2 to the left by the ** number of bits specified by the integer in register P1. @@ -1711,7 +2018,7 @@ case OP_Function: { ** If either input is NULL, the result is NULL. */ /* Opcode: ShiftRight P1 P2 P3 * * -** Synopsis: r[P3]=r[P2]>>r[P1] +** Synopsis: r[P3]=r[P2]>>r[P1] ** ** Shift the integer value in register P2 to the right by the ** number of bits specified by the integer in register P1. @@ -1771,8 +2078,8 @@ case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */ } /* Opcode: AddImm P1 P2 * * * -** Synopsis: r[P1]=r[P1]+P2 -** +** Synopsis: r[P1]=r[P1]+P2 +** ** Add the constant P2 to the value in register P1. ** The result is always an integer. ** @@ -1782,23 +2089,23 @@ case OP_AddImm: { /* in1 */ pIn1 = &aMem[pOp->p1]; memAboutToChange(p, pIn1); sqlite3VdbeMemIntegerify(pIn1); - pIn1->u.i += pOp->p2; + *(u64*)&pIn1->u.i += (u64)pOp->p2; break; } /* Opcode: MustBeInt P1 P2 * * * -** +** ** Force the value in register P1 to be an integer. If the value ** in P1 is not an integer and cannot be converted into an integer ** without data loss, then jump immediately to P2, or if P2==0 ** raise an SQLITE_MISMATCH exception. */ -case OP_MustBeInt: { /* jump, in1 */ +case OP_MustBeInt: { /* jump0, in1 */ pIn1 = &aMem[pOp->p1]; if( (pIn1->flags & MEM_Int)==0 ){ applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding); - VdbeBranchTaken((pIn1->flags&MEM_Int)==0, 2); if( (pIn1->flags & MEM_Int)==0 ){ + VdbeBranchTaken(1, 2); if( pOp->p2==0 ){ rc = SQLITE_MISMATCH; goto abort_due_to_error; @@ -1807,6 +2114,7 @@ case OP_MustBeInt: { /* jump, in1 */ } } } + VdbeBranchTaken(0, 2); MemSetTypeFlag(pIn1, MEM_Int); break; } @@ -1823,25 +2131,28 @@ case OP_MustBeInt: { /* jump, in1 */ */ case OP_RealAffinity: { /* in1 */ pIn1 = &aMem[pOp->p1]; - if( pIn1->flags & MEM_Int ){ + if( pIn1->flags & (MEM_Int|MEM_IntReal) ){ + testcase( pIn1->flags & MEM_Int ); + testcase( pIn1->flags & MEM_IntReal ); sqlite3VdbeMemRealify(pIn1); + REGISTER_TRACE(pOp->p1, pIn1); } break; } #endif -#ifndef SQLITE_OMIT_CAST +#if !defined(SQLITE_OMIT_CAST) || !defined(SQLITE_OMIT_ANALYZE) /* Opcode: Cast P1 P2 * * * ** Synopsis: affinity(r[P1]) ** ** Force the value in register P1 to be the type defined by P2. -** +** ** <ul> -** <li value="97"> TEXT -** <li value="98"> BLOB -** <li value="99"> NUMERIC -** <li value="100"> INTEGER -** <li value="101"> REAL +** <li> P2=='A' &rarr; BLOB +** <li> P2=='B' &rarr; TEXT +** <li> P2=='C' &rarr; NUMERIC +** <li> P2=='D' &rarr; INTEGER +** <li> P2=='E' &rarr; REAL ** </ul> ** ** A NULL value is not changed by this routine. It remains NULL. @@ -1856,89 +2167,102 @@ case OP_Cast: { /* in1 */ pIn1 = &aMem[pOp->p1]; memAboutToChange(p, pIn1); rc = ExpandBlob(pIn1); - sqlite3VdbeMemCast(pIn1, pOp->p2, encoding); + if( rc ) goto abort_due_to_error; + rc = sqlite3VdbeMemCast(pIn1, pOp->p2, encoding); + if( rc ) goto abort_due_to_error; UPDATE_MAX_BLOBSIZE(pIn1); + REGISTER_TRACE(pOp->p1, pIn1); break; } #endif /* SQLITE_OMIT_CAST */ -/* Opcode: Lt P1 P2 P3 P4 P5 -** Synopsis: if r[P1]<r[P3] goto P2 -** -** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then -** jump to address P2. +/* Opcode: Eq P1 P2 P3 P4 P5 +** Synopsis: IF r[P3]==r[P1] ** -** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or -** reg(P3) is NULL then take the jump. If the SQLITE_JUMPIFNULL -** bit is clear then fall through if either operand is NULL. +** Compare the values in register P1 and P3. If reg(P3)==reg(P1) then +** jump to address P2. ** ** The SQLITE_AFF_MASK portion of P5 must be an affinity character - -** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made +** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made ** to coerce both inputs according to this affinity before the ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric ** affinity is used. Note that the affinity conversions are stored ** back into the input registers P1 and P3. So this opcode can cause ** persistent changes to registers P1 and P3. ** -** Once any conversions have taken place, and neither value is NULL, +** Once any conversions have taken place, and neither value is NULL, ** the values are compared. If both values are blobs then memcmp() is ** used to determine the results of the comparison. If both values ** are text, then the appropriate collating function specified in -** P4 is used to do the comparison. If P4 is not specified then +** P4 is used to do the comparison. If P4 is not specified then ** memcmp() is used to compare text string. If both values are ** numeric, then a numeric comparison is used. If the two values ** are of different types, then numbers are considered less than ** strings and strings are considered less than blobs. ** -** If the SQLITE_STOREP2 bit of P5 is set, then do not jump. Instead, -** store a boolean result (either 0, or 1, or NULL) in register P2. +** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either +** true or false and is never NULL. If both operands are NULL then the result +** of comparison is true. If either operand is NULL then the result is false. +** If neither operand is NULL the result is the same as it would be if +** the SQLITE_NULLEQ flag were omitted from P5. ** -** If the SQLITE_NULLEQ bit is set in P5, then NULL values are considered -** equal to one another, provided that they do not have their MEM_Cleared -** bit set. +** This opcode saves the result of comparison for use by the new +** OP_Jump opcode. */ /* Opcode: Ne P1 P2 P3 P4 P5 -** Synopsis: if r[P1]!=r[P3] goto P2 +** Synopsis: IF r[P3]!=r[P1] ** -** This works just like the Lt opcode except that the jump is taken if -** the operands in registers P1 and P3 are not equal. See the Lt opcode for +** This works just like the Eq opcode except that the jump is taken if +** the operands in registers P1 and P3 are not equal. See the Eq opcode for ** additional information. -** -** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either -** true or false and is never NULL. If both operands are NULL then the result -** of comparison is false. If either operand is NULL then the result is true. -** If neither operand is NULL the result is the same as it would be if -** the SQLITE_NULLEQ flag were omitted from P5. */ -/* Opcode: Eq P1 P2 P3 P4 P5 -** Synopsis: if r[P1]==r[P3] goto P2 +/* Opcode: Lt P1 P2 P3 P4 P5 +** Synopsis: IF r[P3]<r[P1] ** -** This works just like the Lt opcode except that the jump is taken if -** the operands in registers P1 and P3 are equal. -** See the Lt opcode for additional information. +** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then +** jump to address P2. ** -** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either -** true or false and is never NULL. If both operands are NULL then the result -** of comparison is true. If either operand is NULL then the result is false. -** If neither operand is NULL the result is the same as it would be if -** the SQLITE_NULLEQ flag were omitted from P5. +** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or +** reg(P3) is NULL then the take the jump. If the SQLITE_JUMPIFNULL +** bit is clear then fall through if either operand is NULL. +** +** The SQLITE_AFF_MASK portion of P5 must be an affinity character - +** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made +** to coerce both inputs according to this affinity before the +** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric +** affinity is used. Note that the affinity conversions are stored +** back into the input registers P1 and P3. So this opcode can cause +** persistent changes to registers P1 and P3. +** +** Once any conversions have taken place, and neither value is NULL, +** the values are compared. If both values are blobs then memcmp() is +** used to determine the results of the comparison. If both values +** are text, then the appropriate collating function specified in +** P4 is used to do the comparison. If P4 is not specified then +** memcmp() is used to compare text string. If both values are +** numeric, then a numeric comparison is used. If the two values +** are of different types, then numbers are considered less than +** strings and strings are considered less than blobs. +** +** This opcode saves the result of comparison for use by the new +** OP_Jump opcode. */ /* Opcode: Le P1 P2 P3 P4 P5 -** Synopsis: if r[P1]<=r[P3] goto P2 +** Synopsis: IF r[P3]<=r[P1] ** ** This works just like the Lt opcode except that the jump is taken if ** the content of register P3 is less than or equal to the content of ** register P1. See the Lt opcode for additional information. */ /* Opcode: Gt P1 P2 P3 P4 P5 -** Synopsis: if r[P1]>r[P3] goto P2 +** Synopsis: IF r[P3]>r[P1] ** ** This works just like the Lt opcode except that the jump is taken if ** the content of register P3 is greater than the content of ** register P1. See the Lt opcode for additional information. */ /* Opcode: Ge P1 P2 P3 P4 P5 -** Synopsis: if r[P1]>=r[P3] goto P2 +** Synopsis: IF r[P3]>=r[P1] ** ** This works just like the Lt opcode except that the jump is taken if ** the content of register P3 is greater than or equal to the content of @@ -1950,7 +2274,7 @@ case OP_Lt: /* same as TK_LT, jump, in1, in3 */ case OP_Le: /* same as TK_LE, jump, in1, in3 */ case OP_Gt: /* same as TK_GT, jump, in1, in3 */ case OP_Ge: { /* same as TK_GE, jump, in1, in3 */ - int res; /* Result of the comparison of pIn1 against pIn3 */ + int res, res2; /* Result of the comparison of pIn1 against pIn3 */ char affinity; /* Affinity to use for comparison */ u16 flags1; /* Copy of initial value of pIn1->flags */ u16 flags3; /* Copy of initial value of pIn3->flags */ @@ -1959,6 +2283,33 @@ case OP_Ge: { /* same as TK_GE, jump, in1, in3 */ pIn3 = &aMem[pOp->p3]; flags1 = pIn1->flags; flags3 = pIn3->flags; + if( (flags1 & flags3 & MEM_Int)!=0 ){ + /* Common case of comparison of two integers */ + if( pIn3->u.i > pIn1->u.i ){ + if( sqlite3aGTb[pOp->opcode] ){ + VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3); + goto jump_to_p2; + } + iCompare = +1; + VVA_ONLY( iCompareIsInit = 1; ) + }else if( pIn3->u.i < pIn1->u.i ){ + if( sqlite3aLTb[pOp->opcode] ){ + VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3); + goto jump_to_p2; + } + iCompare = -1; + VVA_ONLY( iCompareIsInit = 1; ) + }else{ + if( sqlite3aEQb[pOp->opcode] ){ + VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3); + goto jump_to_p2; + } + iCompare = 0; + VVA_ONLY( iCompareIsInit = 1; ) + } + VdbeBranchTaken(0, (pOp->p5 & SQLITE_NULLEQ)?2:3); + break; + } if( (flags1 | flags3)&MEM_Null ){ /* One or both operands are NULL */ if( pOp->p5 & SQLITE_NULLEQ ){ @@ -1966,110 +2317,140 @@ case OP_Ge: { /* same as TK_GE, jump, in1, in3 */ ** OP_Eq or OP_Ne) then take the jump or not depending on whether ** or not both operands are null. */ - assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne ); assert( (flags1 & MEM_Cleared)==0 ); - assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 ); - if( (flags1&MEM_Null)!=0 - && (flags3&MEM_Null)!=0 + assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 || CORRUPT_DB ); + testcase( (pOp->p5 & SQLITE_JUMPIFNULL)!=0 ); + if( (flags1&flags3&MEM_Null)!=0 && (flags3&MEM_Cleared)==0 ){ - res = 0; /* Results are equal */ + res = 0; /* Operands are equal */ }else{ - res = 1; /* Results are not equal */ + res = ((flags3 & MEM_Null) ? -1 : +1); /* Operands are not equal */ } }else{ /* SQLITE_NULLEQ is clear and at least one operand is NULL, ** then the result is always NULL. ** The jump is taken if the SQLITE_JUMPIFNULL bit is set. */ - if( pOp->p5 & SQLITE_STOREP2 ){ - pOut = &aMem[pOp->p2]; - memAboutToChange(p, pOut); - MemSetTypeFlag(pOut, MEM_Null); - REGISTER_TRACE(pOp->p2, pOut); - }else{ - VdbeBranchTaken(2,3); - if( pOp->p5 & SQLITE_JUMPIFNULL ){ - goto jump_to_p2; - } + VdbeBranchTaken(2,3); + if( pOp->p5 & SQLITE_JUMPIFNULL ){ + goto jump_to_p2; } + iCompare = 1; /* Operands are not equal */ + VVA_ONLY( iCompareIsInit = 1; ) break; } }else{ - /* Neither operand is NULL. Do a comparison. */ + /* Neither operand is NULL and we couldn't do the special high-speed + ** integer comparison case. So do a general-case comparison. */ affinity = pOp->p5 & SQLITE_AFF_MASK; if( affinity>=SQLITE_AFF_NUMERIC ){ - if( (flags1 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){ - applyNumericAffinity(pIn1,0); - } - if( (flags3 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){ - applyNumericAffinity(pIn3,0); + if( (flags1 | flags3)&MEM_Str ){ + if( (flags1 & (MEM_Int|MEM_IntReal|MEM_Real|MEM_Str))==MEM_Str ){ + applyNumericAffinity(pIn1,0); + assert( flags3==pIn3->flags || CORRUPT_DB ); + flags3 = pIn3->flags; + } + if( (flags3 & (MEM_Int|MEM_IntReal|MEM_Real|MEM_Str))==MEM_Str ){ + applyNumericAffinity(pIn3,0); + } } - }else if( affinity==SQLITE_AFF_TEXT ){ - if( (flags1 & MEM_Str)==0 && (flags1 & (MEM_Int|MEM_Real))!=0 ){ + }else if( affinity==SQLITE_AFF_TEXT && ((flags1 | flags3) & MEM_Str)!=0 ){ + if( (flags1 & MEM_Str)!=0 ){ + pIn1->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal); + }else if( (flags1&(MEM_Int|MEM_Real|MEM_IntReal))!=0 ){ testcase( pIn1->flags & MEM_Int ); testcase( pIn1->flags & MEM_Real ); + testcase( pIn1->flags & MEM_IntReal ); sqlite3VdbeMemStringify(pIn1, encoding, 1); testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) ); flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask); + if( NEVER(pIn1==pIn3) ) flags3 = flags1 | MEM_Str; } - if( (flags3 & MEM_Str)==0 && (flags3 & (MEM_Int|MEM_Real))!=0 ){ + if( (flags3 & MEM_Str)!=0 ){ + pIn3->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal); + }else if( (flags3&(MEM_Int|MEM_Real|MEM_IntReal))!=0 ){ testcase( pIn3->flags & MEM_Int ); testcase( pIn3->flags & MEM_Real ); + testcase( pIn3->flags & MEM_IntReal ); sqlite3VdbeMemStringify(pIn3, encoding, 1); testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) ); flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask); } } assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 ); - if( flags1 & MEM_Zero ){ - sqlite3VdbeMemExpandBlob(pIn1); - flags1 &= ~MEM_Zero; - } - if( flags3 & MEM_Zero ){ - sqlite3VdbeMemExpandBlob(pIn3); - flags3 &= ~MEM_Zero; - } res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl); } - switch( pOp->opcode ){ - case OP_Eq: res = res==0; break; - case OP_Ne: res = res!=0; break; - case OP_Lt: res = res<0; break; - case OP_Le: res = res<=0; break; - case OP_Gt: res = res>0; break; - default: res = res>=0; break; + + /* At this point, res is negative, zero, or positive if reg[P1] is + ** less than, equal to, or greater than reg[P3], respectively. Compute + ** the answer to this operator in res2, depending on what the comparison + ** operator actually is. The next block of code depends on the fact + ** that the 6 comparison operators are consecutive integers in this + ** order: NE, EQ, GT, LE, LT, GE */ + assert( OP_Eq==OP_Ne+1 ); assert( OP_Gt==OP_Ne+2 ); assert( OP_Le==OP_Ne+3 ); + assert( OP_Lt==OP_Ne+4 ); assert( OP_Ge==OP_Ne+5 ); + if( res<0 ){ + res2 = sqlite3aLTb[pOp->opcode]; + }else if( res==0 ){ + res2 = sqlite3aEQb[pOp->opcode]; + }else{ + res2 = sqlite3aGTb[pOp->opcode]; } + iCompare = res; + VVA_ONLY( iCompareIsInit = 1; ) /* Undo any changes made by applyAffinity() to the input registers. */ - assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) ); - pIn1->flags = flags1; assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) ); pIn3->flags = flags3; + assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) ); + pIn1->flags = flags1; - if( pOp->p5 & SQLITE_STOREP2 ){ - pOut = &aMem[pOp->p2]; - memAboutToChange(p, pOut); - MemSetTypeFlag(pOut, MEM_Int); - pOut->u.i = res; - REGISTER_TRACE(pOp->p2, pOut); - }else{ - VdbeBranchTaken(res!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3); - if( res ){ - goto jump_to_p2; - } + VdbeBranchTaken(res2!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3); + if( res2 ){ + goto jump_to_p2; + } + break; +} + +/* Opcode: ElseEq * P2 * * * +** +** This opcode must follow an OP_Lt or OP_Gt comparison operator. There +** can be zero or more OP_ReleaseReg opcodes intervening, but no other +** opcodes are allowed to occur between this instruction and the previous +** OP_Lt or OP_Gt. +** +** If the result of an OP_Eq comparison on the same two operands as +** the prior OP_Lt or OP_Gt would have been true, then jump to P2. If +** the result of an OP_Eq comparison on the two previous operands +** would have been false or NULL, then fall through. +*/ +case OP_ElseEq: { /* same as TK_ESCAPE, jump */ + +#ifdef SQLITE_DEBUG + /* Verify the preconditions of this opcode - that it follows an OP_Lt or + ** OP_Gt with zero or more intervening OP_ReleaseReg opcodes */ + int iAddr; + for(iAddr = (int)(pOp - aOp) - 1; ALWAYS(iAddr>=0); iAddr--){ + if( aOp[iAddr].opcode==OP_ReleaseReg ) continue; + assert( aOp[iAddr].opcode==OP_Lt || aOp[iAddr].opcode==OP_Gt ); + break; } +#endif /* SQLITE_DEBUG */ + assert( iCompareIsInit ); + VdbeBranchTaken(iCompare==0, 2); + if( iCompare==0 ) goto jump_to_p2; break; } + /* Opcode: Permutation * * * P4 * ** -** Set the permutation used by the OP_Compare operator to be the array -** of integers in P4. +** Set the permutation used by the OP_Compare operator in the next +** instruction. The permutation is stored in the P4 operand. ** -** The permutation is only valid until the next OP_Compare that has -** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should -** occur immediately prior to the OP_Compare. +** The permutation is only valid for the next opcode which must be +** an OP_Compare that has the OPFLAG_PERMUTE bit set in P5. ** ** The first integer in the P4 integer array is the length of the array ** and does not become part of the permutation. @@ -2077,7 +2458,8 @@ case OP_Ge: { /* same as TK_GE, jump, in1, in3 */ case OP_Permutation: { assert( pOp->p4type==P4_INTARRAY ); assert( pOp->p4.ai ); - aPermute = pOp->p4.ai + 1; + assert( pOp[1].opcode==OP_Compare ); + assert( pOp[1].p5 & OPFLAG_PERMUTE ); break; } @@ -2100,6 +2482,8 @@ case OP_Permutation: { ** The comparison is a sort comparison, so NULLs compare equal, ** NULLs are less than numbers, numbers are less than strings, ** and strings are less than blobs. +** +** This opcode must be immediately followed by an OP_Jump opcode. */ case OP_Compare: { int n; @@ -2107,60 +2491,80 @@ case OP_Compare: { int p1; int p2; const KeyInfo *pKeyInfo; - int idx; + u32 idx; CollSeq *pColl; /* Collating sequence to use on this term */ int bRev; /* True for DESCENDING sort order */ + u32 *aPermute; /* The permutation */ - if( (pOp->p5 & OPFLAG_PERMUTE)==0 ) aPermute = 0; + if( (pOp->p5 & OPFLAG_PERMUTE)==0 ){ + aPermute = 0; + }else{ + assert( pOp>aOp ); + assert( pOp[-1].opcode==OP_Permutation ); + assert( pOp[-1].p4type==P4_INTARRAY ); + aPermute = pOp[-1].p4.ai + 1; + assert( aPermute!=0 ); + } n = pOp->p3; pKeyInfo = pOp->p4.pKeyInfo; assert( n>0 ); assert( pKeyInfo!=0 ); + assert( pKeyInfo->aSortFlags!=0 ); p1 = pOp->p1; p2 = pOp->p2; -#if SQLITE_DEBUG +#ifdef SQLITE_DEBUG if( aPermute ){ int k, mx = 0; - for(k=0; k<n; k++) if( aPermute[k]>mx ) mx = aPermute[k]; - assert( p1>0 && p1+mx<=(p->nMem-p->nCursor)+1 ); - assert( p2>0 && p2+mx<=(p->nMem-p->nCursor)+1 ); + for(k=0; k<n; k++) if( aPermute[k]>(u32)mx ) mx = aPermute[k]; + assert( p1>0 && p1+mx<=(p->nMem+1 - p->nCursor)+1 ); + assert( p2>0 && p2+mx<=(p->nMem+1 - p->nCursor)+1 ); }else{ - assert( p1>0 && p1+n<=(p->nMem-p->nCursor)+1 ); - assert( p2>0 && p2+n<=(p->nMem-p->nCursor)+1 ); + assert( p1>0 && p1+n<=(p->nMem+1 - p->nCursor)+1 ); + assert( p2>0 && p2+n<=(p->nMem+1 - p->nCursor)+1 ); } #endif /* SQLITE_DEBUG */ for(i=0; i<n; i++){ - idx = aPermute ? aPermute[i] : i; + idx = aPermute ? aPermute[i] : (u32)i; assert( memIsValid(&aMem[p1+idx]) ); assert( memIsValid(&aMem[p2+idx]) ); REGISTER_TRACE(p1+idx, &aMem[p1+idx]); REGISTER_TRACE(p2+idx, &aMem[p2+idx]); - assert( i<pKeyInfo->nField ); + assert( i<pKeyInfo->nKeyField ); pColl = pKeyInfo->aColl[i]; - bRev = pKeyInfo->aSortOrder[i]; + bRev = (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_DESC); iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl); + VVA_ONLY( iCompareIsInit = 1; ) if( iCompare ){ + if( (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_BIGNULL) + && ((aMem[p1+idx].flags & MEM_Null) || (aMem[p2+idx].flags & MEM_Null)) + ){ + iCompare = -iCompare; + } if( bRev ) iCompare = -iCompare; break; } } - aPermute = 0; + assert( pOp[1].opcode==OP_Jump ); break; } /* Opcode: Jump P1 P2 P3 * * ** ** Jump to the instruction at address P1, P2, or P3 depending on whether -** in the most recent OP_Compare instruction the P1 vector was less than +** in the most recent OP_Compare instruction the P1 vector was less than, ** equal to, or greater than the P2 vector, respectively. +** +** This opcode must immediately follow an OP_Compare opcode. */ case OP_Jump: { /* jump */ + assert( pOp>aOp && pOp[-1].opcode==OP_Compare ); + assert( iCompareIsInit ); if( iCompare<0 ){ - VdbeBranchTaken(0,3); pOp = &aOp[pOp->p1 - 1]; + VdbeBranchTaken(0,4); pOp = &aOp[pOp->p1 - 1]; }else if( iCompare==0 ){ - VdbeBranchTaken(1,3); pOp = &aOp[pOp->p2 - 1]; + VdbeBranchTaken(1,4); pOp = &aOp[pOp->p2 - 1]; }else{ - VdbeBranchTaken(2,3); pOp = &aOp[pOp->p3 - 1]; + VdbeBranchTaken(2,4); pOp = &aOp[pOp->p3 - 1]; } break; } @@ -2190,18 +2594,8 @@ case OP_Or: { /* same as TK_OR, in1, in2, out3 */ int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */ int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */ - pIn1 = &aMem[pOp->p1]; - if( pIn1->flags & MEM_Null ){ - v1 = 2; - }else{ - v1 = sqlite3VdbeIntValue(pIn1)!=0; - } - pIn2 = &aMem[pOp->p2]; - if( pIn2->flags & MEM_Null ){ - v2 = 2; - }else{ - v2 = sqlite3VdbeIntValue(pIn2)!=0; - } + v1 = sqlite3VdbeBooleanValue(&aMem[pOp->p1], 2); + v2 = sqlite3VdbeBooleanValue(&aMem[pOp->p2], 2); if( pOp->opcode==OP_And ){ static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 }; v1 = and_logic[v1*3+v2]; @@ -2219,26 +2613,55 @@ case OP_Or: { /* same as TK_OR, in1, in2, out3 */ break; } +/* Opcode: IsTrue P1 P2 P3 P4 * +** Synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4 +** +** This opcode implements the IS TRUE, IS FALSE, IS NOT TRUE, and +** IS NOT FALSE operators. +** +** Interpret the value in register P1 as a boolean value. Store that +** boolean (a 0 or 1) in register P2. Or if the value in register P1 is +** NULL, then the P3 is stored in register P2. Invert the answer if P4 +** is 1. +** +** The logic is summarized like this: +** +** <ul> +** <li> If P3==0 and P4==0 then r[P2] := r[P1] IS TRUE +** <li> If P3==1 and P4==1 then r[P2] := r[P1] IS FALSE +** <li> If P3==0 and P4==1 then r[P2] := r[P1] IS NOT TRUE +** <li> If P3==1 and P4==0 then r[P2] := r[P1] IS NOT FALSE +** </ul> +*/ +case OP_IsTrue: { /* in1, out2 */ + assert( pOp->p4type==P4_INT32 ); + assert( pOp->p4.i==0 || pOp->p4.i==1 ); + assert( pOp->p3==0 || pOp->p3==1 ); + sqlite3VdbeMemSetInt64(&aMem[pOp->p2], + sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3) ^ pOp->p4.i); + break; +} + /* Opcode: Not P1 P2 * * * ** Synopsis: r[P2]= !r[P1] ** ** Interpret the value in register P1 as a boolean value. Store the -** boolean complement in register P2. If the value in register P1 is +** boolean complement in register P2. If the value in register P1 is ** NULL, then a NULL is stored in P2. */ case OP_Not: { /* same as TK_NOT, in1, out2 */ pIn1 = &aMem[pOp->p1]; pOut = &aMem[pOp->p2]; - sqlite3VdbeMemSetNull(pOut); if( (pIn1->flags & MEM_Null)==0 ){ - pOut->flags = MEM_Int; - pOut->u.i = !sqlite3VdbeIntValue(pIn1); + sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeBooleanValue(pIn1,0)); + }else{ + sqlite3VdbeMemSetNull(pOut); } break; } /* Opcode: BitNot P1 P2 * * * -** Synopsis: r[P1]= ~r[P1] +** Synopsis: r[P2]= ~r[P1] ** ** Interpret the content of register P1 as an integer. Store the ** ones-complement of the P1 value into register P2. If P1 holds @@ -2255,25 +2678,47 @@ case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */ break; } -/* Opcode: Once P1 P2 * * * +/* Opcode: Once P1 P2 P3 * * +** +** Fall through to the next instruction the first time this opcode is +** encountered on each invocation of the byte-code program. Jump to P2 +** on the second and all subsequent encounters during the same invocation. ** -** Check the "once" flag number P1. If it is set, jump to instruction P2. -** Otherwise, set the flag and fall through to the next instruction. -** In other words, this opcode causes all following opcodes up through P2 -** (but not including P2) to run just once and to be skipped on subsequent -** times through the loop. +** Top-level programs determine first invocation by comparing the P1 +** operand against the P1 operand on the OP_Init opcode at the beginning +** of the program. If the P1 values differ, then fall through and make +** the P1 of this opcode equal to the P1 of OP_Init. If P1 values are +** the same then take the jump. ** -** All "once" flags are initially cleared whenever a prepared statement -** first begins to run. +** For subprograms, there is a bitmask in the VdbeFrame that determines +** whether or not the jump should be taken. The bitmask is necessary +** because the self-altering code trick does not work for recursive +** triggers. +** +** The P3 operand is not used directly by this opcode. However P3 is +** used by the code generator as follows: If this opcode is the start +** of a subroutine and that subroutine uses a Bloom filter, then P3 will +** be the register that holds that Bloom filter. See tag-202407032019 +** in the source code for implementation details. */ case OP_Once: { /* jump */ - assert( pOp->p1<p->nOnceFlag ); - VdbeBranchTaken(p->aOnceFlag[pOp->p1]!=0, 2); - if( p->aOnceFlag[pOp->p1] ){ - goto jump_to_p2; + u32 iAddr; /* Address of this instruction */ + assert( p->aOp[0].opcode==OP_Init ); + if( p->pFrame ){ + iAddr = (int)(pOp - p->aOp); + if( (p->pFrame->aOnce[iAddr/8] & (1<<(iAddr & 7)))!=0 ){ + VdbeBranchTaken(1, 2); + goto jump_to_p2; + } + p->pFrame->aOnce[iAddr/8] |= 1<<(iAddr & 7); }else{ - p->aOnceFlag[pOp->p1] = 1; + if( p->aOp[0].p1==pOp->p1 ){ + VdbeBranchTaken(1, 2); + goto jump_to_p2; + } } + VdbeBranchTaken(0, 2); + pOp->p1 = p->aOp[0].p1; break; } @@ -2283,35 +2728,30 @@ case OP_Once: { /* jump */ ** is considered true if it is numeric and non-zero. If the value ** in P1 is NULL then take the jump if and only if P3 is non-zero. */ +case OP_If: { /* jump, in1 */ + int c; + c = sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3); + VdbeBranchTaken(c!=0, 2); + if( c ) goto jump_to_p2; + break; +} + /* Opcode: IfNot P1 P2 P3 * * ** ** Jump to P2 if the value in register P1 is False. The value ** is considered false if it has a numeric value of zero. If the value ** in P1 is NULL then take the jump if and only if P3 is non-zero. */ -case OP_If: /* jump, in1 */ case OP_IfNot: { /* jump, in1 */ int c; - pIn1 = &aMem[pOp->p1]; - if( pIn1->flags & MEM_Null ){ - c = pOp->p3; - }else{ -#ifdef SQLITE_OMIT_FLOATING_POINT - c = sqlite3VdbeIntValue(pIn1)!=0; -#else - c = sqlite3VdbeRealValue(pIn1)!=0.0; -#endif - if( pOp->opcode==OP_IfNot ) c = !c; - } + c = !sqlite3VdbeBooleanValue(&aMem[pOp->p1], !pOp->p3); VdbeBranchTaken(c!=0, 2); - if( c ){ - goto jump_to_p2; - } + if( c ) goto jump_to_p2; break; } /* Opcode: IsNull P1 P2 * * * -** Synopsis: if r[P1]==NULL goto P2 +** Synopsis: if r[P1]==NULL goto P2 ** ** Jump to P2 if the value in register P1 is NULL. */ @@ -2324,10 +2764,121 @@ case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */ break; } +/* Opcode: IsType P1 P2 P3 P4 P5 +** Synopsis: if typeof(P1.P3) in P5 goto P2 +** +** Jump to P2 if the type of a column in a btree is one of the types specified +** by the P5 bitmask. +** +** P1 is normally a cursor on a btree for which the row decode cache is +** valid through at least column P3. In other words, there should have been +** a prior OP_Column for column P3 or greater. If the cursor is not valid, +** then this opcode might give spurious results. +** The the btree row has fewer than P3 columns, then use P4 as the +** datatype. +** +** If P1 is -1, then P3 is a register number and the datatype is taken +** from the value in that register. +** +** P5 is a bitmask of data types. SQLITE_INTEGER is the least significant +** (0x01) bit. SQLITE_FLOAT is the 0x02 bit. SQLITE_TEXT is 0x04. +** SQLITE_BLOB is 0x08. SQLITE_NULL is 0x10. +** +** WARNING: This opcode does not reliably distinguish between NULL and REAL +** when P1>=0. If the database contains a NaN value, this opcode will think +** that the datatype is REAL when it should be NULL. When P1<0 and the value +** is already stored in register P3, then this opcode does reliably +** distinguish between NULL and REAL. The problem only arises then P1>=0. +** +** Take the jump to address P2 if and only if the datatype of the +** value determined by P1 and P3 corresponds to one of the bits in the +** P5 bitmask. +** +*/ +case OP_IsType: { /* jump */ + VdbeCursor *pC; + u16 typeMask; + u32 serialType; + + assert( pOp->p1>=(-1) && pOp->p1<p->nCursor ); + assert( pOp->p1>=0 || (pOp->p3>=0 && pOp->p3<=(p->nMem+1 - p->nCursor)) ); + if( pOp->p1>=0 ){ + pC = p->apCsr[pOp->p1]; + assert( pC!=0 ); + assert( pOp->p3>=0 ); + if( pOp->p3<pC->nHdrParsed ){ + serialType = pC->aType[pOp->p3]; + if( serialType>=12 ){ + if( serialType&1 ){ + typeMask = 0x04; /* SQLITE_TEXT */ + }else{ + typeMask = 0x08; /* SQLITE_BLOB */ + } + }else{ + static const unsigned char aMask[] = { + 0x10, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x2, + 0x01, 0x01, 0x10, 0x10 + }; + testcase( serialType==0 ); + testcase( serialType==1 ); + testcase( serialType==2 ); + testcase( serialType==3 ); + testcase( serialType==4 ); + testcase( serialType==5 ); + testcase( serialType==6 ); + testcase( serialType==7 ); + testcase( serialType==8 ); + testcase( serialType==9 ); + testcase( serialType==10 ); + testcase( serialType==11 ); + typeMask = aMask[serialType]; + } + }else{ + typeMask = 1 << (pOp->p4.i - 1); + testcase( typeMask==0x01 ); + testcase( typeMask==0x02 ); + testcase( typeMask==0x04 ); + testcase( typeMask==0x08 ); + testcase( typeMask==0x10 ); + } + }else{ + assert( memIsValid(&aMem[pOp->p3]) ); + typeMask = 1 << (sqlite3_value_type((sqlite3_value*)&aMem[pOp->p3])-1); + testcase( typeMask==0x01 ); + testcase( typeMask==0x02 ); + testcase( typeMask==0x04 ); + testcase( typeMask==0x08 ); + testcase( typeMask==0x10 ); + } + VdbeBranchTaken( (typeMask & pOp->p5)!=0, 2); + if( typeMask & pOp->p5 ){ + goto jump_to_p2; + } + break; +} + +/* Opcode: ZeroOrNull P1 P2 P3 * * +** Synopsis: r[P2] = 0 OR NULL +** +** If both registers P1 and P3 are NOT NULL, then store a zero in +** register P2. If either registers P1 or P3 are NULL then put +** a NULL in register P2. +*/ +case OP_ZeroOrNull: { /* in1, in2, out2, in3 */ + if( (aMem[pOp->p1].flags & MEM_Null)!=0 + || (aMem[pOp->p3].flags & MEM_Null)!=0 + ){ + sqlite3VdbeMemSetNull(aMem + pOp->p2); + }else{ + sqlite3VdbeMemSetInt64(aMem + pOp->p2, 0); + } + break; +} + /* Opcode: NotNull P1 P2 * * * ** Synopsis: if r[P1]!=NULL goto P2 ** -** Jump to P2 if the value in register P1 is not NULL. +** Jump to P2 if the value in register P1 is not NULL. */ case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */ pIn1 = &aMem[pOp->p1]; @@ -2338,117 +2889,168 @@ case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */ break; } -/* Opcode: Column P1 P2 P3 P4 P5 -** Synopsis: r[P3]=PX -** -** Interpret the data that cursor P1 points to as a structure built using -** the MakeRecord instruction. (See the MakeRecord opcode for additional -** information about the format of the data.) Extract the P2-th column -** from this record. If there are less that (P2+1) -** values in the record, extract a NULL. +/* Opcode: IfNullRow P1 P2 P3 * * +** Synopsis: if P1.nullRow then r[P3]=NULL, goto P2 ** -** The value extracted is stored in register P3. -** -** If the column contains fewer than P2 fields, then extract a NULL. Or, -** if the P4 argument is a P4_MEM use the value of the P4 argument as -** the result. +** Check the cursor P1 to see if it is currently pointing at a NULL row. +** If it is, then set register P3 to NULL and jump immediately to P2. +** If P1 is not on a NULL row, then fall through without making any +** changes. ** -** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor, -** then the cache of the cursor is reset prior to extracting the column. -** The first OP_Column against a pseudo-table after the value of the content -** register has changed should have this bit set. -** -** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 when -** the result is guaranteed to only be used as the argument of a length() -** or typeof() function, respectively. The loading of large blobs can be -** skipped for length() and all content loading can be skipped for typeof(). +** If P1 is not an open cursor, then this opcode is a no-op. */ -case OP_Column: { - i64 payloadSize64; /* Number of bytes in the record */ - int p2; /* column number to retrieve */ - VdbeCursor *pC; /* The VDBE cursor */ - BtCursor *pCrsr; /* The BTree cursor */ - u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */ - int len; /* The length of the serialized data for the column */ +case OP_IfNullRow: { /* jump */ + VdbeCursor *pC; + assert( pOp->p1>=0 && pOp->p1<p->nCursor ); + pC = p->apCsr[pOp->p1]; + if( pC && pC->nullRow ){ + sqlite3VdbeMemSetNull(aMem + pOp->p3); + goto jump_to_p2; + } + break; +} + +#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC +/* Opcode: Offset P1 P2 P3 * * +** Synopsis: r[P3] = sqlite_offset(P1) +** +** Store in register r[P3] the byte offset into the database file that is the +** start of the payload for the record at which that cursor P1 is currently +** pointing. +** +** P2 is the column number for the argument to the sqlite_offset() function. +** This opcode does not use P2 itself, but the P2 value is used by the +** code generator. The P1, P2, and P3 operands to this opcode are the +** same as for OP_Column. +** +** This opcode is only available if SQLite is compiled with the +** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option. +*/ +case OP_Offset: { /* out3 */ + VdbeCursor *pC; /* The VDBE cursor */ + assert( pOp->p1>=0 && pOp->p1<p->nCursor ); + pC = p->apCsr[pOp->p1]; + pOut = &p->aMem[pOp->p3]; + if( pC==0 || pC->eCurType!=CURTYPE_BTREE ){ + sqlite3VdbeMemSetNull(pOut); + }else{ + if( pC->deferredMoveto ){ + rc = sqlite3VdbeFinishMoveto(pC); + if( rc ) goto abort_due_to_error; + } + if( sqlite3BtreeEof(pC->uc.pCursor) ){ + sqlite3VdbeMemSetNull(pOut); + }else{ + sqlite3VdbeMemSetInt64(pOut, sqlite3BtreeOffset(pC->uc.pCursor)); + } + } + break; +} +#endif /* SQLITE_ENABLE_OFFSET_SQL_FUNC */ + +/* Opcode: Column P1 P2 P3 P4 P5 +** Synopsis: r[P3]=PX cursor P1 column P2 +** +** Interpret the data that cursor P1 points to as a structure built using +** the MakeRecord instruction. (See the MakeRecord opcode for additional +** information about the format of the data.) Extract the P2-th column +** from this record. If there are less than (P2+1) +** values in the record, extract a NULL. +** +** The value extracted is stored in register P3. +** +** If the record contains fewer than P2 fields, then extract a NULL. Or, +** if the P4 argument is a P4_MEM use the value of the P4 argument as +** the result. +** +** If the OPFLAG_LENGTHARG bit is set in P5 then the result is guaranteed +** to only be used by the length() function or the equivalent. The content +** of large blobs is not loaded, thus saving CPU cycles. If the +** OPFLAG_TYPEOFARG bit is set then the result will only be used by the +** typeof() function or the IS NULL or IS NOT NULL operators or the +** equivalent. In this case, all content loading can be omitted. +*/ +case OP_Column: { /* ncycle */ + u32 p2; /* column number to retrieve */ + VdbeCursor *pC; /* The VDBE cursor */ + BtCursor *pCrsr; /* The B-Tree cursor corresponding to pC */ + u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */ + int len; /* The length of the serialized data for the column */ int i; /* Loop counter */ Mem *pDest; /* Where to write the extracted value */ Mem sMem; /* For storing the record being decoded */ const u8 *zData; /* Part of the record being decoded */ const u8 *zHdr; /* Next unparsed byte of the header */ const u8 *zEndHdr; /* Pointer to first byte after the header */ - u32 offset; /* Offset into the data */ u64 offset64; /* 64-bit offset */ - u32 avail; /* Number of bytes of available data */ u32 t; /* A type code from the record header */ Mem *pReg; /* PseudoTable input register */ + assert( pOp->p1>=0 && pOp->p1<p->nCursor ); + assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); pC = p->apCsr[pOp->p1]; - p2 = pOp->p2; + p2 = (u32)pOp->p2; - /* If the cursor cache is stale, bring it up-to-date */ - rc = sqlite3VdbeCursorMoveto(&pC, &p2); - - assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) ); - pDest = &aMem[pOp->p3]; - memAboutToChange(p, pDest); - assert( pOp->p1>=0 && pOp->p1<p->nCursor ); +op_column_restart: assert( pC!=0 ); - assert( p2<pC->nField ); + assert( p2<(u32)pC->nField + || (pC->eCurType==CURTYPE_PSEUDO && pC->seekResult==0) ); aOffset = pC->aOffset; + assert( aOffset==pC->aType+pC->nField ); assert( pC->eCurType!=CURTYPE_VTAB ); assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow ); assert( pC->eCurType!=CURTYPE_SORTER ); - pCrsr = pC->uc.pCursor; - if( rc ) goto abort_due_to_error; - if( pC->cacheStatus!=p->cacheCtr ){ + if( pC->cacheStatus!=p->cacheCtr ){ /*OPTIMIZATION-IF-FALSE*/ if( pC->nullRow ){ - if( pC->eCurType==CURTYPE_PSEUDO ){ - assert( pC->uc.pseudoTableReg>0 ); - pReg = &aMem[pC->uc.pseudoTableReg]; + if( pC->eCurType==CURTYPE_PSEUDO && pC->seekResult>0 ){ + /* For the special case of as pseudo-cursor, the seekResult field + ** identifies the register that holds the record */ + pReg = &aMem[pC->seekResult]; assert( pReg->flags & MEM_Blob ); assert( memIsValid(pReg) ); - pC->payloadSize = pC->szRow = avail = pReg->n; + pC->payloadSize = pC->szRow = pReg->n; pC->aRow = (u8*)pReg->z; }else{ + pDest = &aMem[pOp->p3]; + memAboutToChange(p, pDest); sqlite3VdbeMemSetNull(pDest); goto op_column_out; } }else{ + pCrsr = pC->uc.pCursor; + if( pC->deferredMoveto ){ + u32 iMap; + assert( !pC->isEphemeral ); + if( pC->ub.aAltMap && (iMap = pC->ub.aAltMap[1+p2])>0 ){ + pC = pC->pAltCursor; + p2 = iMap - 1; + goto op_column_restart; + } + rc = sqlite3VdbeFinishMoveto(pC); + if( rc ) goto abort_due_to_error; + }else if( sqlite3BtreeCursorHasMoved(pCrsr) ){ + rc = sqlite3VdbeHandleMovedCursor(pC); + if( rc ) goto abort_due_to_error; + goto op_column_restart; + } assert( pC->eCurType==CURTYPE_BTREE ); assert( pCrsr ); - if( pC->isTable==0 ){ - assert( sqlite3BtreeCursorIsValid(pCrsr) ); - VVA_ONLY(rc =) sqlite3BtreeKeySize(pCrsr, &payloadSize64); - assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */ - /* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the - ** payload size, so it is impossible for payloadSize64 to be - ** larger than 32 bits. */ - assert( (payloadSize64 & SQLITE_MAX_U32)==(u64)payloadSize64 ); - pC->aRow = sqlite3BtreeKeyFetch(pCrsr, &avail); - pC->payloadSize = (u32)payloadSize64; - }else{ - assert( sqlite3BtreeCursorIsValid(pCrsr) ); - VVA_ONLY(rc =) sqlite3BtreeDataSize(pCrsr, &pC->payloadSize); - assert( rc==SQLITE_OK ); /* DataSize() cannot fail */ - pC->aRow = sqlite3BtreeDataFetch(pCrsr, &avail); - } - assert( avail<=65536 ); /* Maximum page size is 64KiB */ - if( pC->payloadSize <= (u32)avail ){ - pC->szRow = pC->payloadSize; - }else if( pC->payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){ - goto too_big; - }else{ - pC->szRow = avail; - } + assert( sqlite3BtreeCursorIsValid(pCrsr) ); + pC->payloadSize = sqlite3BtreePayloadSize(pCrsr); + pC->aRow = sqlite3BtreePayloadFetch(pCrsr, &pC->szRow); + assert( pC->szRow<=pC->payloadSize ); + assert( pC->szRow<=65536 ); /* Maximum page size is 64KiB */ } pC->cacheStatus = p->cacheCtr; - pC->iHdrOffset = getVarint32(pC->aRow, offset); + if( (aOffset[0] = pC->aRow[0])<0x80 ){ + pC->iHdrOffset = 1; + }else{ + pC->iHdrOffset = sqlite3GetVarint32(pC->aRow, aOffset); + } pC->nHdrParsed = 0; - aOffset[0] = offset; - - if( avail<offset ){ + if( pC->szRow<aOffset[0] ){ /*OPTIMIZATION-IF-FALSE*/ /* pC->aRow does not have to hold the entire row, but it does at least ** need to cover the header of the record. If pC->aRow does not contain ** the complete header, then set it to zero, forcing the header to be @@ -2465,18 +3067,32 @@ case OP_Column: { ** 3-byte type for each of the maximum of 32768 columns plus three ** extra bytes for the header length itself. 32768*3 + 3 = 98307. */ - if( offset > 98307 || offset > pC->payloadSize ){ - rc = SQLITE_CORRUPT_BKPT; - goto op_column_error; + if( aOffset[0] > 98307 || aOffset[0] > pC->payloadSize ){ + goto op_column_corrupt; } + }else{ + /* This is an optimization. By skipping over the first few tests + ** (ex: pC->nHdrParsed<=p2) in the next section, we achieve a + ** measurable performance gain. + ** + ** This branch is taken even if aOffset[0]==0. Such a record is never + ** generated by SQLite, and could be considered corruption, but we + ** accept it for historical reasons. When aOffset[0]==0, the code this + ** branch jumps to reads past the end of the record, but never more + ** than a few bytes. Even if the record occurs at the end of the page + ** content area, the "page header" comes after the page content and so + ** this overread is harmless. Similar overreads can occur for a corrupt + ** database file. + */ + zData = pC->aRow; + assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */ + testcase( aOffset[0]==0 ); + goto op_column_read_header; } - - /* The following goto is an optimization. It can be omitted and - ** everything will still work. But OP_Column is measurably faster - ** by skipping the subsequent conditional, which is always true. - */ - assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */ - goto op_column_read_header; + }else if( sqlite3BtreeCursorHasMoved(pC->uc.pCursor) ){ + rc = sqlite3VdbeHandleMovedCursor(pC); + if( rc ) goto abort_due_to_error; + goto op_column_restart; } /* Make sure at least the first p2+1 entries of the header have been @@ -2484,41 +3100,38 @@ case OP_Column: { */ if( pC->nHdrParsed<=p2 ){ /* If there is more header available for parsing in the record, try - ** to extract additional fields up through the p2+1-th field + ** to extract additional fields up through the p2+1-th field */ - op_column_read_header: if( pC->iHdrOffset<aOffset[0] ){ /* Make sure zData points to enough of the record to cover the header. */ if( pC->aRow==0 ){ memset(&sMem, 0, sizeof(sMem)); - rc = sqlite3VdbeMemFromBtree(pCrsr, 0, aOffset[0], !pC->isTable, &sMem); - if( rc!=SQLITE_OK ) goto op_column_error; + rc = sqlite3VdbeMemFromBtreeZeroOffset(pC->uc.pCursor,aOffset[0],&sMem); + if( rc!=SQLITE_OK ) goto abort_due_to_error; zData = (u8*)sMem.z; }else{ zData = pC->aRow; } - + /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */ + op_column_read_header: i = pC->nHdrParsed; offset64 = aOffset[i]; zHdr = zData + pC->iHdrOffset; zEndHdr = zData + aOffset[0]; - assert( i<=p2 && zHdr<zEndHdr ); + testcase( zHdr>=zEndHdr ); do{ - if( (t = zHdr[0])<0x80 ){ + if( (pC->aType[i] = t = zHdr[0])<0x80 ){ zHdr++; offset64 += sqlite3VdbeOneByteSerialTypeLen(t); }else{ zHdr += sqlite3GetVarint32(zHdr, &t); + pC->aType[i] = t; offset64 += sqlite3VdbeSerialTypeLen(t); } - pC->aType[i++] = t; - aOffset[i] = (u32)(offset64 & 0xffffffff); - }while( i<=p2 && zHdr<zEndHdr ); - pC->nHdrParsed = i; - pC->iHdrOffset = (u32)(zHdr - zData); - if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem); - + aOffset[++i] = (u32)(offset64 & 0xffffffff); + }while( (u32)i<=p2 && zHdr<zEndHdr ); + /* The record is corrupt if any of the following are true: ** (1) the bytes of the header extend past the declared header size ** (2) the entire header was used but not all data was used @@ -2527,9 +3140,18 @@ case OP_Column: { if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize)) || (offset64 > pC->payloadSize) ){ - rc = SQLITE_CORRUPT_BKPT; - goto op_column_error; + if( aOffset[0]==0 ){ + i = 0; + zHdr = zEndHdr; + }else{ + if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem); + goto op_column_corrupt; + } } + + pC->nHdrParsed = i; + pC->iHdrOffset = (u32)(zHdr - zData); + if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem); }else{ t = 0; } @@ -2539,6 +3161,8 @@ case OP_Column: { ** columns. So the result will be either the default value or a NULL. */ if( pC->nHdrParsed<=p2 ){ + pDest = &aMem[pOp->p3]; + memAboutToChange(p, pDest); if( pOp->p4type==P4_MEM ){ sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static); }else{ @@ -2556,10 +3180,13 @@ case OP_Column: { */ assert( p2<pC->nHdrParsed ); assert( rc==SQLITE_OK ); + pDest = &aMem[pOp->p3]; + memAboutToChange(p, pDest); assert( sqlite3VdbeCheckMemInvariants(pDest) ); - if( VdbeMemDynamic(pDest) ) sqlite3VdbeMemSetNull(pDest); + if( VdbeMemDynamic(pDest) ){ + sqlite3VdbeMemSetNull(pDest); + } assert( t==pC->aType[p2] ); - pDest->enc = encoding; if( pC->szRow>=aOffset[p2+1] ){ /* This is the common case where the desired content fits on the original ** page - where the content is not on an overflow page */ @@ -2573,7 +3200,9 @@ case OP_Column: { */ static const u16 aFlag[] = { MEM_Blob, MEM_Str|MEM_Term }; pDest->n = len = (t-12)/2; + pDest->enc = encoding; if( pDest->szMalloc < len+2 ){ + if( len>db->aLimit[SQLITE_LIMIT_LENGTH] ) goto too_big; pDest->flags = MEM_Null; if( sqlite3VdbeMemGrow(pDest, len+2, 0) ) goto no_mem; }else{ @@ -2585,34 +3214,180 @@ case OP_Column: { pDest->flags = aFlag[t&1]; } }else{ + u8 p5; + pDest->enc = encoding; + assert( pDest->db==db ); /* This branch happens only when content is on overflow pages */ - if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0 - && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0)) - || (len = sqlite3VdbeSerialTypeLen(t))==0 + if( ((p5 = (pOp->p5 & OPFLAG_BYTELENARG))!=0 + && (p5==OPFLAG_TYPEOFARG + || (t>=12 && ((t&1)==0 || p5==OPFLAG_BYTELENARG)) + ) + ) + || sqlite3VdbeSerialTypeLen(t)==0 ){ /* Content is irrelevant for ** 1. the typeof() function, ** 2. the length(X) function if X is a blob, and ** 3. if the content length is zero. ** So we might as well use bogus content rather than reading - ** content from disk. */ - static u8 aZero[8]; /* This is the bogus content */ - sqlite3VdbeSerialGet(aZero, t, pDest); + ** content from disk. + ** + ** Although sqlite3VdbeSerialGet() may read at most 8 bytes from the + ** buffer passed to it, debugging function VdbeMemPrettyPrint() may + ** read more. Use the global constant sqlite3CtypeMap[] as the array, + ** as that array is 256 bytes long (plenty for VdbeMemPrettyPrint()) + ** and it begins with a bunch of zeros. + */ + sqlite3VdbeSerialGet((u8*)sqlite3CtypeMap, t, pDest); }else{ - rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, !pC->isTable, - pDest); - if( rc==SQLITE_OK ){ - sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest); - pDest->flags &= ~MEM_Ephem; + rc = vdbeColumnFromOverflow(pC, p2, t, aOffset[p2], + p->cacheCtr, colCacheCtr, pDest); + if( rc ){ + if( rc==SQLITE_NOMEM ) goto no_mem; + if( rc==SQLITE_TOOBIG ) goto too_big; + goto abort_due_to_error; } } } op_column_out: -op_column_error: UPDATE_MAX_BLOBSIZE(pDest); REGISTER_TRACE(pOp->p3, pDest); break; + +op_column_corrupt: + if( aOp[0].p3>0 ){ + pOp = &aOp[aOp[0].p3-1]; + break; + }else{ + rc = SQLITE_CORRUPT_BKPT; + goto abort_due_to_error; + } +} + +/* Opcode: TypeCheck P1 P2 P3 P4 * +** Synopsis: typecheck(r[P1@P2]) +** +** Apply affinities to the range of P2 registers beginning with P1. +** Take the affinities from the Table object in P4. If any value +** cannot be coerced into the correct type, then raise an error. +** +** If P3==0, then omit checking of VIRTUAL columns. +** +** If P3==1, then omit checking of all generated column, both VIRTUAL +** and STORED. +** +** If P3>=2, then only check column number P3-2 in the table (which will +** be a VIRTUAL column) against the value in reg[P1]. In this case, +** P2 will be 1. +** +** This opcode is similar to OP_Affinity except that this opcode +** forces the register type to the Table column type. This is used +** to implement "strict affinity". +** +** GENERATED ALWAYS AS ... STATIC columns are only checked if P3 +** is zero. When P3 is non-zero, no type checking occurs for +** static generated columns. Virtual columns are computed at query time +** and so they are never checked. +** +** Preconditions: +** +** <ul> +** <li> P2 should be the number of non-virtual columns in the +** table of P4 unless P3>1, in which case P2 will be 1. +** <li> Table P4 is a STRICT table. +** </ul> +** +** If any precondition is false, an assertion fault occurs. +*/ +case OP_TypeCheck: { + Table *pTab; + Column *aCol; + int i; + int nCol; + + assert( pOp->p4type==P4_TABLE ); + pTab = pOp->p4.pTab; + assert( pTab->tabFlags & TF_Strict ); + assert( pOp->p3>=0 && pOp->p3<pTab->nCol+2 ); + aCol = pTab->aCol; + pIn1 = &aMem[pOp->p1]; + if( pOp->p3<2 ){ + assert( pTab->nNVCol==pOp->p2 ); + i = 0; + nCol = pTab->nCol; + }else{ + i = pOp->p3-2; + nCol = i+1; + assert( i<pTab->nCol ); + assert( aCol[i].colFlags & COLFLAG_VIRTUAL ); + assert( pOp->p2==1 ); + } + for(; i<nCol; i++){ + if( (aCol[i].colFlags & COLFLAG_GENERATED)!=0 && pOp->p3<2 ){ + if( (aCol[i].colFlags & COLFLAG_VIRTUAL)!=0 ) continue; + if( pOp->p3 ){ pIn1++; continue; } + } + assert( pIn1 < &aMem[pOp->p1+pOp->p2] ); + applyAffinity(pIn1, aCol[i].affinity, encoding); + if( (pIn1->flags & MEM_Null)==0 ){ + switch( aCol[i].eCType ){ + case COLTYPE_BLOB: { + if( (pIn1->flags & MEM_Blob)==0 ) goto vdbe_type_error; + break; + } + case COLTYPE_INTEGER: + case COLTYPE_INT: { + if( (pIn1->flags & MEM_Int)==0 ) goto vdbe_type_error; + break; + } + case COLTYPE_TEXT: { + if( (pIn1->flags & MEM_Str)==0 ) goto vdbe_type_error; + break; + } + case COLTYPE_REAL: { + testcase( (pIn1->flags & (MEM_Real|MEM_IntReal))==MEM_Real ); + assert( (pIn1->flags & MEM_IntReal)==0 ); + if( pIn1->flags & MEM_Int ){ + /* When applying REAL affinity, if the result is still an MEM_Int + ** that will fit in 6 bytes, then change the type to MEM_IntReal + ** so that we keep the high-resolution integer value but know that + ** the type really wants to be REAL. */ + testcase( pIn1->u.i==140737488355328LL ); + testcase( pIn1->u.i==140737488355327LL ); + testcase( pIn1->u.i==-140737488355328LL ); + testcase( pIn1->u.i==-140737488355329LL ); + if( pIn1->u.i<=140737488355327LL && pIn1->u.i>=-140737488355328LL){ + pIn1->flags |= MEM_IntReal; + pIn1->flags &= ~MEM_Int; + }else{ + pIn1->u.r = (double)pIn1->u.i; + pIn1->flags |= MEM_Real; + pIn1->flags &= ~MEM_Int; + } + }else if( (pIn1->flags & (MEM_Real|MEM_IntReal))==0 ){ + goto vdbe_type_error; + } + break; + } + default: { + /* COLTYPE_ANY. Accept anything. */ + break; + } + } + } + REGISTER_TRACE((int)(pIn1-aMem), pIn1); + pIn1++; + } + assert( pIn1 == &aMem[pOp->p1+pOp->p2] ); + break; + +vdbe_type_error: + sqlite3VdbeError(p, "cannot store %s value in %s column %s.%s", + vdbeMemTypeName(pIn1), sqlite3StdType[aCol[i].eCType-1], + pTab->zName, aCol[i].zCnName); + rc = SQLITE_CONSTRAINT_DATATYPE; + goto abort_due_to_error; } /* Opcode: Affinity P1 P2 * P4 * @@ -2620,22 +3395,43 @@ case OP_Column: { ** ** Apply affinities to a range of P2 registers starting with P1. ** -** P4 is a string that is P2 characters long. The nth character of the -** string indicates the column affinity that should be used for the nth +** P4 is a string that is P2 characters long. The N-th character of the +** string indicates the column affinity that should be used for the N-th ** memory cell in the range. */ case OP_Affinity: { const char *zAffinity; /* The affinity to be applied */ - char cAff; /* A single character of affinity */ zAffinity = pOp->p4.z; assert( zAffinity!=0 ); + assert( pOp->p2>0 ); assert( zAffinity[pOp->p2]==0 ); pIn1 = &aMem[pOp->p1]; - while( (cAff = *(zAffinity++))!=0 ){ - assert( pIn1 <= &p->aMem[(p->nMem-p->nCursor)] ); - assert( memIsValid(pIn1) ); - applyAffinity(pIn1, cAff, encoding); + while( 1 /*exit-by-break*/ ){ + assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] ); + assert( zAffinity[0]==SQLITE_AFF_NONE || memIsValid(pIn1) ); + applyAffinity(pIn1, zAffinity[0], encoding); + if( zAffinity[0]==SQLITE_AFF_REAL && (pIn1->flags & MEM_Int)!=0 ){ + /* When applying REAL affinity, if the result is still an MEM_Int + ** that will fit in 6 bytes, then change the type to MEM_IntReal + ** so that we keep the high-resolution integer value but know that + ** the type really wants to be REAL. */ + testcase( pIn1->u.i==140737488355328LL ); + testcase( pIn1->u.i==140737488355327LL ); + testcase( pIn1->u.i==-140737488355328LL ); + testcase( pIn1->u.i==-140737488355329LL ); + if( pIn1->u.i<=140737488355327LL && pIn1->u.i>=-140737488355328LL ){ + pIn1->flags |= MEM_IntReal; + pIn1->flags &= ~MEM_Int; + }else{ + pIn1->u.r = (double)pIn1->u.i; + pIn1->flags |= MEM_Real; + pIn1->flags &= ~(MEM_Int|MEM_Str); + } + } + REGISTER_TRACE((int)(pIn1-aMem), pIn1); + zAffinity++; + if( zAffinity[0]==0 ) break; pIn1++; } break; @@ -2648,17 +3444,27 @@ case OP_Affinity: { ** use as a data record in a database table or as a key ** in an index. The OP_Column opcode can decode the record later. ** -** P4 may be a string that is P2 characters long. The nth character of the -** string indicates the column affinity that should be used for the nth +** P4 may be a string that is P2 characters long. The N-th character of the +** string indicates the column affinity that should be used for the N-th ** field of the index key. ** ** The mapping from character to affinity is given by the SQLITE_AFF_ ** macros defined in sqliteInt.h. ** ** If P4 is NULL then all index fields have the affinity BLOB. +** +** The meaning of P5 depends on whether or not the SQLITE_ENABLE_NULL_TRIM +** compile-time option is enabled: +** +** * If SQLITE_ENABLE_NULL_TRIM is enabled, then the P5 is the index +** of the right-most table that can be null-trimmed. +** +** * If SQLITE_ENABLE_NULL_TRIM is omitted, then P5 has the value +** OPFLAG_NOCHNG_MAGIC if the OP_MakeRecord opcode is allowed to +** accept no-change records with serial_type 10. This value is +** only used inside an assert() and does not affect the end result. */ case OP_MakeRecord: { - u8 *zNewRecord; /* A buffer to hold the data for the new record */ Mem *pRec; /* The new record */ u64 nData; /* Number of bytes of data space */ int nHdr; /* Number of bytes of header space */ @@ -2670,22 +3476,21 @@ case OP_MakeRecord: { Mem *pLast; /* Last field of the record */ int nField; /* Number of fields in the record */ char *zAffinity; /* The affinity string for the record */ - int file_format; /* File format to use for encoding */ - int i; /* Space used in zNewRecord[] header */ - int j; /* Space used in zNewRecord[] content */ u32 len; /* Length of a field */ + u8 *zHdr; /* Where to write next byte of the header */ + u8 *zPayload; /* Where to write next byte of the payload */ /* Assuming the record contains N fields, the record format looks ** like this: ** ** ------------------------------------------------------------------------ - ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 | + ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 | ** ------------------------------------------------------------------------ ** ** Data(0) is taken from register P1. Data(1) comes from register P1+1 ** and so forth. ** - ** Each type field is a varint representing the serial type of the + ** Each type field is a varint representing the serial type of the ** corresponding data element (see sqlite3VdbeSerialType()). The ** hdr-size field is also a varint which is the offset from the beginning ** of the record to data0. @@ -2695,11 +3500,10 @@ case OP_MakeRecord: { nZero = 0; /* Number of zero bytes at the end of the record */ nField = pOp->p1; zAffinity = pOp->p4.z; - assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem-p->nCursor)+1 ); + assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem+1 - p->nCursor)+1 ); pData0 = &aMem[nField]; nField = pOp->p2; pLast = &pData0[nField-1]; - file_format = p->minWriteFileFormat; /* Identify the output register */ assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 ); @@ -2712,31 +3516,152 @@ case OP_MakeRecord: { if( zAffinity ){ pRec = pData0; do{ - applyAffinity(pRec++, *(zAffinity++), encoding); + applyAffinity(pRec, zAffinity[0], encoding); + if( zAffinity[0]==SQLITE_AFF_REAL && (pRec->flags & MEM_Int) ){ + pRec->flags |= MEM_IntReal; + pRec->flags &= ~(MEM_Int); + } + REGISTER_TRACE((int)(pRec-aMem), pRec); + zAffinity++; + pRec++; assert( zAffinity[0]==0 || pRec<=pLast ); }while( zAffinity[0] ); } +#ifdef SQLITE_ENABLE_NULL_TRIM + /* NULLs can be safely trimmed from the end of the record, as long as + ** as the schema format is 2 or more and none of the omitted columns + ** have a non-NULL default value. Also, the record must be left with + ** at least one field. If P5>0 then it will be one more than the + ** index of the right-most column with a non-NULL default value */ + if( pOp->p5 ){ + while( (pLast->flags & MEM_Null)!=0 && nField>pOp->p5 ){ + pLast--; + nField--; + } + } +#endif + /* Loop through the elements that will make up the record to figure - ** out how much space is required for the new record. + ** out how much space is required for the new record. After this loop, + ** the Mem.uTemp field of each term should hold the serial-type that will + ** be used for that term in the generated record: + ** + ** Mem.uTemp value type + ** --------------- --------------- + ** 0 NULL + ** 1 1-byte signed integer + ** 2 2-byte signed integer + ** 3 3-byte signed integer + ** 4 4-byte signed integer + ** 5 6-byte signed integer + ** 6 8-byte signed integer + ** 7 IEEE float + ** 8 Integer constant 0 + ** 9 Integer constant 1 + ** 10,11 reserved for expansion + ** N>=12 and even BLOB + ** N>=13 and odd text + ** + ** The following additional values are computed: + ** nHdr Number of bytes needed for the record header + ** nData Number of bytes of data space needed for the record + ** nZero Zero bytes at the end of the record */ pRec = pLast; do{ assert( memIsValid(pRec) ); - pRec->uTemp = serial_type = sqlite3VdbeSerialType(pRec, file_format, &len); - if( pRec->flags & MEM_Zero ){ - if( nData ){ - if( sqlite3VdbeMemExpandBlob(pRec) ) goto no_mem; + if( pRec->flags & MEM_Null ){ + if( pRec->flags & MEM_Zero ){ + /* Values with MEM_Null and MEM_Zero are created by xColumn virtual + ** table methods that never invoke sqlite3_result_xxxxx() while + ** computing an unchanging column value in an UPDATE statement. + ** Give such values a special internal-use-only serial-type of 10 + ** so that they can be passed through to xUpdate and have + ** a true sqlite3_value_nochange(). */ +#ifndef SQLITE_ENABLE_NULL_TRIM + assert( pOp->p5==OPFLAG_NOCHNG_MAGIC || CORRUPT_DB ); +#endif + pRec->uTemp = 10; + }else{ + pRec->uTemp = 0; + } + nHdr++; + }else if( pRec->flags & (MEM_Int|MEM_IntReal) ){ + /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */ + i64 i = pRec->u.i; + u64 uu; + testcase( pRec->flags & MEM_Int ); + testcase( pRec->flags & MEM_IntReal ); + if( i<0 ){ + uu = ~i; + }else{ + uu = i; + } + nHdr++; + testcase( uu==127 ); testcase( uu==128 ); + testcase( uu==32767 ); testcase( uu==32768 ); + testcase( uu==8388607 ); testcase( uu==8388608 ); + testcase( uu==2147483647 ); testcase( uu==2147483648LL ); + testcase( uu==140737488355327LL ); testcase( uu==140737488355328LL ); + if( uu<=127 ){ + if( (i&1)==i && p->minWriteFileFormat>=4 ){ + pRec->uTemp = 8+(u32)uu; + }else{ + nData++; + pRec->uTemp = 1; + } + }else if( uu<=32767 ){ + nData += 2; + pRec->uTemp = 2; + }else if( uu<=8388607 ){ + nData += 3; + pRec->uTemp = 3; + }else if( uu<=2147483647 ){ + nData += 4; + pRec->uTemp = 4; + }else if( uu<=140737488355327LL ){ + nData += 6; + pRec->uTemp = 5; }else{ - nZero += pRec->u.nZero; - len -= pRec->u.nZero; + nData += 8; + if( pRec->flags & MEM_IntReal ){ + /* If the value is IntReal and is going to take up 8 bytes to store + ** as an integer, then we might as well make it an 8-byte floating + ** point value */ + pRec->u.r = (double)pRec->u.i; + pRec->flags &= ~MEM_IntReal; + pRec->flags |= MEM_Real; + pRec->uTemp = 7; + }else{ + pRec->uTemp = 6; + } + } + }else if( pRec->flags & MEM_Real ){ + nHdr++; + nData += 8; + pRec->uTemp = 7; + }else{ + assert( db->mallocFailed || pRec->flags&(MEM_Str|MEM_Blob) ); + assert( pRec->n>=0 ); + len = (u32)pRec->n; + serial_type = (len*2) + 12 + ((pRec->flags & MEM_Str)!=0); + if( pRec->flags & MEM_Zero ){ + serial_type += (u32)pRec->u.nZero*2; + if( nData ){ + if( sqlite3VdbeMemExpandBlob(pRec) ) goto no_mem; + len += pRec->u.nZero; + }else{ + nZero += pRec->u.nZero; + } } + nData += len; + nHdr += sqlite3VarintLen(serial_type); + pRec->uTemp = serial_type; } - nData += len; - testcase( serial_type==127 ); - testcase( serial_type==128 ); - nHdr += serial_type<=127 ? 1 : sqlite3VarintLen(serial_type); - }while( (--pRec)>=pData0 ); + if( pRec==pData0 ) break; + pRec--; + }while(1); /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint ** which determines the total number of bytes in the header. The varint @@ -2754,57 +3679,119 @@ case OP_MakeRecord: { if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++; } nByte = nHdr+nData; - if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){ - goto too_big; - } - /* Make sure the output register has a buffer large enough to store + /* Make sure the output register has a buffer large enough to store ** the new record. The output register (pOp->p3) is not allowed to ** be one of the input registers (because the following call to ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used). */ - if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){ - goto no_mem; + if( nByte+nZero<=pOut->szMalloc ){ + /* The output register is already large enough to hold the record. + ** No error checks or buffer enlargement is required */ + pOut->z = pOut->zMalloc; + }else{ + /* Need to make sure that the output is not too big and then enlarge + ** the output register to hold the full result */ + if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){ + goto too_big; + } + if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){ + goto no_mem; + } } - zNewRecord = (u8 *)pOut->z; + pOut->n = (int)nByte; + pOut->flags = MEM_Blob; + if( nZero ){ + pOut->u.nZero = nZero; + pOut->flags |= MEM_Zero; + } + UPDATE_MAX_BLOBSIZE(pOut); + zHdr = (u8 *)pOut->z; + zPayload = zHdr + nHdr; /* Write the record */ - i = putVarint32(zNewRecord, nHdr); - j = nHdr; + if( nHdr<0x80 ){ + *(zHdr++) = nHdr; + }else{ + zHdr += sqlite3PutVarint(zHdr,nHdr); + } assert( pData0<=pLast ); pRec = pData0; - do{ + while( 1 /*exit-by-break*/ ){ serial_type = pRec->uTemp; /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more - ** additional varints, one per column. */ - i += putVarint32(&zNewRecord[i], serial_type); /* serial type */ - /* EVIDENCE-OF: R-64536-51728 The values for each column in the record + ** additional varints, one per column. + ** EVIDENCE-OF: R-64536-51728 The values for each column in the record ** immediately follow the header. */ - j += sqlite3VdbeSerialPut(&zNewRecord[j], pRec, serial_type); /* content */ - }while( (++pRec)<=pLast ); - assert( i==nHdr ); - assert( j==nByte ); - - assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) ); - pOut->n = (int)nByte; - pOut->flags = MEM_Blob; - if( nZero ){ - pOut->u.nZero = nZero; - pOut->flags |= MEM_Zero; + if( serial_type<=7 ){ + *(zHdr++) = serial_type; + if( serial_type==0 ){ + /* NULL value. No change in zPayload */ + }else{ + u64 v; + if( serial_type==7 ){ + assert( sizeof(v)==sizeof(pRec->u.r) ); + memcpy(&v, &pRec->u.r, sizeof(v)); + swapMixedEndianFloat(v); + }else{ + v = pRec->u.i; + } + len = sqlite3SmallTypeSizes[serial_type]; + assert( len>=1 && len<=8 && len!=5 && len!=7 ); + switch( len ){ + default: zPayload[7] = (u8)(v&0xff); v >>= 8; + zPayload[6] = (u8)(v&0xff); v >>= 8; + /* no break */ deliberate_fall_through + case 6: zPayload[5] = (u8)(v&0xff); v >>= 8; + zPayload[4] = (u8)(v&0xff); v >>= 8; + /* no break */ deliberate_fall_through + case 4: zPayload[3] = (u8)(v&0xff); v >>= 8; + /* no break */ deliberate_fall_through + case 3: zPayload[2] = (u8)(v&0xff); v >>= 8; + /* no break */ deliberate_fall_through + case 2: zPayload[1] = (u8)(v&0xff); v >>= 8; + /* no break */ deliberate_fall_through + case 1: zPayload[0] = (u8)(v&0xff); + } + zPayload += len; + } + }else if( serial_type<0x80 ){ + *(zHdr++) = serial_type; + if( serial_type>=14 && pRec->n>0 ){ + assert( pRec->z!=0 ); + memcpy(zPayload, pRec->z, pRec->n); + zPayload += pRec->n; + } + }else{ + zHdr += sqlite3PutVarint(zHdr, serial_type); + if( pRec->n ){ + assert( pRec->z!=0 ); + assert( pRec->z!=(const char*)sqlite3CtypeMap ); + memcpy(zPayload, pRec->z, pRec->n); + zPayload += pRec->n; + } + } + if( pRec==pLast ) break; + pRec++; } - pOut->enc = SQLITE_UTF8; /* In case the blob is ever converted to text */ + assert( nHdr==(int)(zHdr - (u8*)pOut->z) ); + assert( nByte==(int)(zPayload - (u8*)pOut->z) ); + + assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); REGISTER_TRACE(pOp->p3, pOut); - UPDATE_MAX_BLOBSIZE(pOut); break; } -/* Opcode: Count P1 P2 * * * +/* Opcode: Count P1 P2 P3 * * ** Synopsis: r[P2]=count() ** -** Store the number of entries (an integer value) in the table or index -** opened by cursor P1 in register P2 +** Store the number of entries (an integer value) in the table or index +** opened by cursor P1 in register P2. +** +** If P3==0, then an exact count is obtained, which involves visiting +** every btree page of the table. But if P3 is non-zero, an estimate +** is returned based on the current cursor position. */ -#ifndef SQLITE_OMIT_BTREECOUNT case OP_Count: { /* out2 */ i64 nEntry; BtCursor *pCrsr; @@ -2812,19 +3799,24 @@ case OP_Count: { /* out2 */ assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE ); pCrsr = p->apCsr[pOp->p1]->uc.pCursor; assert( pCrsr ); - nEntry = 0; /* Not needed. Only used to silence a warning. */ - rc = sqlite3BtreeCount(pCrsr, &nEntry); + if( pOp->p3 ){ + nEntry = sqlite3BtreeRowCountEst(pCrsr); + }else{ + nEntry = 0; /* Not needed. Only used to silence a warning. */ + rc = sqlite3BtreeCount(db, pCrsr, &nEntry); + if( rc ) goto abort_due_to_error; + } pOut = out2Prerelease(p, pOp); pOut->u.i = nEntry; - break; + goto check_for_interrupt; } -#endif /* Opcode: Savepoint P1 * * P4 * ** ** Open, release or rollback the savepoint named by parameter P4, depending -** on the value of P1. To open a new savepoint, P1==0. To release (commit) an -** existing savepoint, P1==1, or to rollback an existing savepoint P1==2. +** on the value of P1. To open a new savepoint set P1==0 (SAVEPOINT_BEGIN). +** To release (commit) an existing savepoint set P1==1 (SAVEPOINT_RELEASE). +** To rollback an existing savepoint set P1==2 (SAVEPOINT_ROLLBACK). */ case OP_Savepoint: { int p1; /* Value of P1 operand */ @@ -2840,7 +3832,7 @@ case OP_Savepoint: { zName = pOp->p4.z; /* Assert that the p1 parameter is valid. Also that if there is no open - ** transaction, then there cannot be any savepoints. + ** transaction, then there cannot be any savepoints. */ assert( db->pSavepoint==0 || db->autoCommit==0 ); assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK ); @@ -2850,7 +3842,7 @@ case OP_Savepoint: { if( p1==SAVEPOINT_BEGIN ){ if( db->nVdbeWrite>0 ){ - /* A new savepoint cannot be created if there are active write + /* A new savepoint cannot be created if there are active write ** statements (i.e. open read/write incremental blob handles). */ sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress"); @@ -2874,7 +3866,7 @@ case OP_Savepoint: { if( pNew ){ pNew->zName = (char *)&pNew[1]; memcpy(pNew->zName, zName, nName+1); - + /* If there is no open transaction, then mark this as a special ** "transaction savepoint". */ if( db->autoCommit ){ @@ -2883,7 +3875,7 @@ case OP_Savepoint: { }else{ db->nSavepoint++; } - + /* Link the new savepoint into the database handle's list. */ pNew->pNext = db->pSavepoint; db->pSavepoint = pNew; @@ -2892,12 +3884,13 @@ case OP_Savepoint: { } } }else{ + assert( p1==SAVEPOINT_RELEASE || p1==SAVEPOINT_ROLLBACK ); iSavepoint = 0; /* Find the named savepoint. If there is no such savepoint, then an ** an error is returned to the user. */ for( - pSavepoint = db->pSavepoint; + pSavepoint = db->pSavepoint; pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName); pSavepoint = pSavepoint->pNext ){ @@ -2907,7 +3900,7 @@ case OP_Savepoint: { sqlite3VdbeError(p, "no such savepoint: %s", zName); rc = SQLITE_ERROR; }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){ - /* It is not possible to release (commit) a savepoint if there are + /* It is not possible to release (commit) a savepoint if there are ** active write statements. */ sqlite3VdbeError(p, "cannot release savepoint - " @@ -2916,12 +3909,12 @@ case OP_Savepoint: { }else{ /* Determine whether or not this is a transaction savepoint. If so, - ** and this is a RELEASE command, then the current transaction - ** is committed. + ** and this is a RELEASE command, then the current transaction + ** is committed. */ int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint; if( isTransaction && p1==SAVEPOINT_RELEASE ){ - if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){ + if( (rc = sqlite3VdbeCheckFkDeferred(p))!=SQLITE_OK ){ goto vdbe_return; } db->autoCommit = 1; @@ -2931,13 +3924,17 @@ case OP_Savepoint: { p->rc = rc = SQLITE_BUSY; goto vdbe_return; } - db->isTransactionSavepoint = 0; rc = p->rc; + if( rc ){ + db->autoCommit = 0; + }else{ + db->isTransactionSavepoint = 0; + } }else{ int isSchemaChange; iSavepoint = db->nSavepoint - iSavepoint - 1; if( p1==SAVEPOINT_ROLLBACK ){ - isSchemaChange = (db->flags & SQLITE_InternChanges)!=0; + isSchemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0; for(ii=0; ii<db->nDb; ii++){ rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt, SQLITE_ABORT_ROLLBACK, @@ -2945,6 +3942,7 @@ case OP_Savepoint: { if( rc!=SQLITE_OK ) goto abort_due_to_error; } }else{ + assert( p1==SAVEPOINT_RELEASE ); isSchemaChange = 0; } for(ii=0; ii<db->nDb; ii++){ @@ -2954,13 +3952,14 @@ case OP_Savepoint: { } } if( isSchemaChange ){ - sqlite3ExpirePreparedStatements(db); + sqlite3ExpirePreparedStatements(db, 0); sqlite3ResetAllSchemasOfConnection(db); - db->flags = (db->flags | SQLITE_InternChanges); + db->mDbFlags |= DBFLAG_SchemaChange; } } - - /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all + if( rc ) goto abort_due_to_error; + + /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all ** savepoints nested inside of the savepoint being operated on. */ while( db->pSavepoint!=pSavepoint ){ pTmp = db->pSavepoint; @@ -2969,8 +3968,8 @@ case OP_Savepoint: { db->nSavepoint--; } - /* If it is a RELEASE, then destroy the savepoint being operated on - ** too. If it is a ROLLBACK TO, then set the number of deferred + /* If it is a RELEASE, then destroy the savepoint being operated on + ** too. If it is a ROLLBACK TO, then set the number of deferred ** constraint violations present in the database to the value stored ** when the savepoint was created. */ if( p1==SAVEPOINT_RELEASE ){ @@ -2981,6 +3980,7 @@ case OP_Savepoint: { db->nSavepoint--; } }else{ + assert( p1==SAVEPOINT_ROLLBACK ); db->nDeferredCons = pSavepoint->nDeferredCons; db->nDeferredImmCons = pSavepoint->nDeferredImmCons; } @@ -2991,7 +3991,11 @@ case OP_Savepoint: { } } } - + if( rc ) goto abort_due_to_error; + if( p->eVdbeState==VDBE_HALT_STATE ){ + rc = SQLITE_DONE; + goto vdbe_return; + } break; } @@ -3022,13 +4026,13 @@ case OP_AutoCommit: { db->autoCommit = 1; }else if( desiredAutoCommit && db->nVdbeWrite>0 ){ /* If this instruction implements a COMMIT and other VMs are writing - ** return an error indicating that the other VMs must complete first. + ** return an error indicating that the other VMs must complete first. */ sqlite3VdbeError(p, "cannot commit transaction - " "SQL statements in progress"); rc = SQLITE_BUSY; - break; - }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){ + goto abort_due_to_error; + }else if( (rc = sqlite3VdbeCheckFkDeferred(p))!=SQLITE_OK ){ goto vdbe_return; }else{ db->autoCommit = (u8)desiredAutoCommit; @@ -3039,7 +4043,6 @@ case OP_AutoCommit: { p->rc = rc = SQLITE_BUSY; goto vdbe_return; } - assert( db->nStatement==0 ); sqlite3CloseSavepoints(db); if( p->rc==SQLITE_OK ){ rc = SQLITE_DONE; @@ -3052,19 +4055,21 @@ case OP_AutoCommit: { (!desiredAutoCommit)?"cannot start a transaction within a transaction":( (iRollback)?"cannot rollback - no transaction is active": "cannot commit - no transaction is active")); - + rc = SQLITE_ERROR; + goto abort_due_to_error; } - break; + /*NOTREACHED*/ assert(0); } /* Opcode: Transaction P1 P2 P3 P4 P5 ** ** Begin a transaction on database P1 if a transaction is not already ** active. -** If P2 is non-zero, then a write-transaction is started, or if a +** If P2 is non-zero, then a write-transaction is started, or if a ** read-transaction is already active, it is upgraded to a write-transaction. -** If P2 is zero, then a read-transaction is started. +** If P2 is zero, then a read-transaction is started. If P2 is 2 or more +** then an exclusive transaction is started. ** ** P1 is the index of the database file on which the transaction is ** started. Index 0 is the main database file and index 1 is the @@ -3094,39 +4099,50 @@ case OP_AutoCommit: { */ case OP_Transaction: { Btree *pBt; - int iMeta; - int iGen; + Db *pDb; + int iMeta = 0; assert( p->bIsReader ); assert( p->readOnly==0 || pOp->p2==0 ); + assert( pOp->p2>=0 && pOp->p2<=2 ); assert( pOp->p1>=0 && pOp->p1<db->nDb ); assert( DbMaskTest(p->btreeMask, pOp->p1) ); - if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){ - rc = SQLITE_READONLY; + assert( rc==SQLITE_OK ); + if( pOp->p2 && (db->flags & (SQLITE_QueryOnly|SQLITE_CorruptRdOnly))!=0 ){ + if( db->flags & SQLITE_QueryOnly ){ + /* Writes prohibited by the "PRAGMA query_only=TRUE" statement */ + rc = SQLITE_READONLY; + }else{ + /* Writes prohibited due to a prior SQLITE_CORRUPT in the current + ** transaction */ + rc = SQLITE_CORRUPT; + } goto abort_due_to_error; } - pBt = db->aDb[pOp->p1].pBt; + pDb = &db->aDb[pOp->p1]; + pBt = pDb->pBt; if( pBt ){ - rc = sqlite3BtreeBeginTrans(pBt, pOp->p2); + rc = sqlite3BtreeBeginTrans(pBt, pOp->p2, &iMeta); testcase( rc==SQLITE_BUSY_SNAPSHOT ); testcase( rc==SQLITE_BUSY_RECOVERY ); - if( (rc&0xff)==SQLITE_BUSY ){ - p->pc = (int)(pOp - aOp); - p->rc = rc; - goto vdbe_return; - } if( rc!=SQLITE_OK ){ + if( (rc&0xff)==SQLITE_BUSY ){ + p->pc = (int)(pOp - aOp); + p->rc = rc; + goto vdbe_return; + } goto abort_due_to_error; } - if( pOp->p2 && p->usesStmtJournal - && (db->autoCommit==0 || db->nVdbeRead>1) + if( p->usesStmtJournal + && pOp->p2 + && (db->autoCommit==0 || db->nVdbeRead>1) ){ - assert( sqlite3BtreeIsInTrans(pBt) ); + assert( sqlite3BtreeTxnState(pBt)==SQLITE_TXN_WRITE ); if( p->iStatement==0 ){ assert( db->nStatement>=0 && db->nSavepoint>=0 ); - db->nStatement++; + db->nStatement++; p->iStatement = db->nSavepoint + db->nStatement; } @@ -3141,23 +4157,20 @@ case OP_Transaction: { p->nStmtDefCons = db->nDeferredCons; p->nStmtDefImmCons = db->nDeferredImmCons; } - - /* Gather the schema version number for checking: - ** IMPLEMENTATION-OF: R-32195-19465 The schema version is used by SQLite - ** each time a query is executed to ensure that the internal cache of the - ** schema used when compiling the SQL query matches the schema of the - ** database against which the compiled query is actually executed. - */ - sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&iMeta); - iGen = db->aDb[pOp->p1].pSchema->iGeneration; - }else{ - iGen = iMeta = 0; } assert( pOp->p5==0 || pOp->p4type==P4_INT32 ); - if( pOp->p5 && (iMeta!=pOp->p3 || iGen!=pOp->p4.i) ){ + if( rc==SQLITE_OK + && pOp->p5 + && (iMeta!=pOp->p3 || pDb->pSchema->iGeneration!=pOp->p4.i) + ){ + /* + ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema + ** version is checked to ensure that the schema has not changed since the + ** SQL statement was prepared. + */ sqlite3DbFree(db, p->zErrMsg); p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed"); - /* If the schema-cookie from the database file matches the cookie + /* If the schema-cookie from the database file matches the cookie ** stored with the in-memory representation of the schema, do ** not reload the schema from the database file. ** @@ -3167,7 +4180,7 @@ case OP_Transaction: { ** prepared queries. If such a query is out-of-date, we do not want to ** discard the database schema, as the user code implementing the ** v-table would have to be ready for the sqlite3_vtab structure itself - ** to be invalidated whenever sqlite3_step() is called from within + ** to be invalidated whenever sqlite3_step() is called from within ** a v-table method. */ if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){ @@ -3175,7 +4188,13 @@ case OP_Transaction: { } p->expired = 1; rc = SQLITE_SCHEMA; + + /* Set changeCntOn to 0 to prevent the value returned by sqlite3_changes() + ** from being modified in sqlite3VdbeHalt(). If this statement is + ** reprepared, changeCntOn will be set again. */ + p->changeCntOn = 0; } + if( rc ) goto abort_due_to_error; break; } @@ -3210,18 +4229,25 @@ case OP_ReadCookie: { /* out2 */ break; } -/* Opcode: SetCookie P1 P2 P3 * * +/* Opcode: SetCookie P1 P2 P3 * P5 ** ** Write the integer value P3 into cookie number P2 of database P1. ** P2==1 is the schema version. P2==2 is the database format. -** P2==3 is the recommended pager cache -** size, and so forth. P1==0 is the main database file and P1==1 is the +** P2==3 is the recommended pager cache +** size, and so forth. P1==0 is the main database file and P1==1 is the ** database file used to store temporary tables. ** ** A transaction must be started before executing this opcode. +** +** If P2 is the SCHEMA_VERSION cookie (cookie number 1) then the internal +** schema version is set to P3-P5. The "PRAGMA schema_version=N" statement +** has P5 set to 1, so that the internal schema version will be different +** from the database schema version, resulting in a schema reset. */ case OP_SetCookie: { Db *pDb; + + sqlite3VdbeIncrWriteCounter(p, 0); assert( pOp->p2<SQLITE_N_BTREE_META ); assert( pOp->p1>=0 && pOp->p1<db->nDb ); assert( DbMaskTest(p->btreeMask, pOp->p1) ); @@ -3233,8 +4259,9 @@ case OP_SetCookie: { rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, pOp->p3); if( pOp->p2==BTREE_SCHEMA_VERSION ){ /* When the schema cookie changes, record the new cookie internally */ - pDb->pSchema->schema_cookie = pOp->p3; - db->flags |= SQLITE_InternChanges; + *(u32*)&pDb->pSchema->schema_cookie = *(u32*)&pOp->p3 - pOp->p5; + db->mDbFlags |= DBFLAG_SchemaChange; + sqlite3FkClearTriggerCache(db, pOp->p1); }else if( pOp->p2==BTREE_FILE_FORMAT ){ /* Record changes in the file format */ pDb->pSchema->file_format = pOp->p3; @@ -3242,9 +4269,10 @@ case OP_SetCookie: { if( pOp->p1==1 ){ /* Invalidate all prepared statements whenever the TEMP database ** schema is changed. Ticket #1644 */ - sqlite3ExpirePreparedStatements(db); + sqlite3ExpirePreparedStatements(db, 0); p->expired = 0; } + if( rc ) goto abort_due_to_error; break; } @@ -3252,71 +4280,90 @@ case OP_SetCookie: { ** Synopsis: root=P2 iDb=P3 ** ** Open a read-only cursor for the database table whose root page is -** P2 in a database file. The database file is determined by P3. -** P3==0 means the main database, P3==1 means the database used for +** P2 in a database file. The database file is determined by P3. +** P3==0 means the main database, P3==1 means the database used for ** temporary tables, and P3>1 means used the corresponding attached ** database. Give the new cursor an identifier of P1. The P1 ** values need not be contiguous but all P1 values should be small integers. ** It is an error for P1 to be negative. ** -** If P5!=0 then use the content of register P2 as the root page, not -** the value of P2 itself. -** -** There will be a read lock on the database whenever there is an -** open cursor. If the database was unlocked prior to this instruction -** then a read lock is acquired as part of this instruction. A read -** lock allows other processes to read the database but prohibits -** any other process from modifying the database. The read lock is -** released when all cursors are closed. If this instruction attempts -** to get a read lock but fails, the script terminates with an -** SQLITE_BUSY error code. +** Allowed P5 bits: +** <ul> +** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for +** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT +** of OP_SeekLE/OP_IdxLT) +** </ul> ** ** The P4 value may be either an integer (P4_INT32) or a pointer to -** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo -** structure, then said structure defines the content and collating -** sequence of the index being opened. Otherwise, if P4 is an integer -** value, it is set to the number of columns in the table. +** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo +** object, then table being opened must be an [index b-tree] where the +** KeyInfo object defines the content and collating +** sequence of that index b-tree. Otherwise, if P4 is an integer +** value, then the table being opened must be a [table b-tree] with a +** number of columns no less than the value of P4. ** ** See also: OpenWrite, ReopenIdx */ /* Opcode: ReopenIdx P1 P2 P3 P4 P5 ** Synopsis: root=P2 iDb=P3 ** -** The ReopenIdx opcode works exactly like ReadOpen except that it first -** checks to see if the cursor on P1 is already open with a root page -** number of P2 and if it is this opcode becomes a no-op. In other words, +** The ReopenIdx opcode works like OP_OpenRead except that it first +** checks to see if the cursor on P1 is already open on the same +** b-tree and if it is this opcode becomes a no-op. In other words, ** if the cursor is already open, do not reopen it. ** -** The ReopenIdx opcode may only be used with P5==0 and with P4 being -** a P4_KEYINFO object. Furthermore, the P3 value must be the same as -** every other ReopenIdx or OpenRead for the same cursor number. +** The ReopenIdx opcode may only be used with P5==0 or P5==OPFLAG_SEEKEQ +** and with P4 being a P4_KEYINFO object. Furthermore, the P3 value must +** be the same as every other ReopenIdx or OpenRead for the same cursor +** number. ** -** See the OpenRead opcode documentation for additional information. +** Allowed P5 bits: +** <ul> +** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for +** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT +** of OP_SeekLE/OP_IdxLT) +** </ul> +** +** See also: OP_OpenRead, OP_OpenWrite */ /* Opcode: OpenWrite P1 P2 P3 P4 P5 ** Synopsis: root=P2 iDb=P3 ** ** Open a read/write cursor named P1 on the table or index whose root -** page is P2. Or if P5!=0 use the content of register P2 to find the -** root page. +** page is P2 (or whose root page is held in register P2 if the +** OPFLAG_P2ISREG bit is set in P5 - see below). ** ** The P4 value may be either an integer (P4_INT32) or a pointer to -** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo -** structure, then said structure defines the content and collating -** sequence of the index being opened. Otherwise, if P4 is an integer -** value, it is set to the number of columns in the table, or to the -** largest index of any column of the table that is actually used. +** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo +** object, then table being opened must be an [index b-tree] where the +** KeyInfo object defines the content and collating +** sequence of that index b-tree. Otherwise, if P4 is an integer +** value, then the table being opened must be a [table b-tree] with a +** number of columns no less than the value of P4. +** +** Allowed P5 bits: +** <ul> +** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for +** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT +** of OP_SeekLE/OP_IdxLT) +** <li> <b>0x08 OPFLAG_FORDELETE</b>: This cursor is used only to seek +** and subsequently delete entries in an index btree. This is a +** hint to the storage engine that the storage engine is allowed to +** ignore. The hint is not used by the official SQLite b*tree storage +** engine, but is used by COMDB2. +** <li> <b>0x10 OPFLAG_P2ISREG</b>: Use the content of register P2 +** as the root page, not the value of P2 itself. +** </ul> ** -** This instruction works just like OpenRead except that it opens the cursor -** in read/write mode. For a given table, there can be one or more read-only -** cursors or a single read/write cursor but not both. +** This instruction works like OpenRead except that it opens the cursor +** in read/write mode. ** -** See also OpenRead. +** See also: OP_OpenRead, OP_ReopenIdx */ -case OP_ReopenIdx: { +case OP_ReopenIdx: { /* ncycle */ int nField; KeyInfo *pKeyInfo; - int p2; + u32 p2; int iDb; int wrFlag; Btree *pX; @@ -3328,11 +4375,13 @@ case OP_ReopenIdx: { pCur = p->apCsr[pOp->p1]; if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){ assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */ + assert( pCur->eCurType==CURTYPE_BTREE ); + sqlite3BtreeClearCursor(pCur->uc.pCursor); goto open_cursor_set_hints; } /* If the cursor is not currently open or is open on a different ** index, then fall through into OP_OpenRead to force a reopen */ -case OP_OpenRead: +case OP_OpenRead: /* ncycle */ case OP_OpenWrite: assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ ); @@ -3340,14 +4389,14 @@ case OP_OpenWrite: assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx || p->readOnly==0 ); - if( p->expired ){ + if( p->expired==1 ){ rc = SQLITE_ABORT_ROLLBACK; - break; + goto abort_due_to_error; } nField = 0; pKeyInfo = 0; - p2 = pOp->p2; + p2 = (u32)pOp->p2; iDb = pOp->p3; assert( iDb>=0 && iDb<db->nDb ); assert( DbMaskTest(p->btreeMask, iDb) ); @@ -3361,39 +4410,38 @@ case OP_OpenWrite: if( pDb->pSchema->file_format < p->minWriteFileFormat ){ p->minWriteFileFormat = pDb->pSchema->file_format; } + if( pOp->p5 & OPFLAG_P2ISREG ){ + assert( p2>0 ); + assert( p2<=(u32)(p->nMem+1 - p->nCursor) ); + pIn2 = &aMem[p2]; + assert( memIsValid(pIn2) ); + assert( (pIn2->flags & MEM_Int)!=0 ); + sqlite3VdbeMemIntegerify(pIn2); + p2 = (int)pIn2->u.i; + /* The p2 value always comes from a prior OP_CreateBtree opcode and + ** that opcode will always set the p2 value to 2 or more or else fail. + ** If there were a failure, the prepared statement would have halted + ** before reaching this instruction. */ + assert( p2>=2 ); + } }else{ wrFlag = 0; - } - if( pOp->p5 & OPFLAG_P2ISREG ){ - assert( p2>0 ); - assert( p2<=(p->nMem-p->nCursor) ); - pIn2 = &aMem[p2]; - assert( memIsValid(pIn2) ); - assert( (pIn2->flags & MEM_Int)!=0 ); - sqlite3VdbeMemIntegerify(pIn2); - p2 = (int)pIn2->u.i; - /* The p2 value always comes from a prior OP_CreateTable opcode and - ** that opcode will always set the p2 value to 2 or more or else fail. - ** If there were a failure, the prepared statement would have halted - ** before reaching this instruction. */ - if( NEVER(p2<2) ) { - rc = SQLITE_CORRUPT_BKPT; - goto abort_due_to_error; - } + assert( (pOp->p5 & OPFLAG_P2ISREG)==0 ); } if( pOp->p4type==P4_KEYINFO ){ pKeyInfo = pOp->p4.pKeyInfo; assert( pKeyInfo->enc==ENC(db) ); assert( pKeyInfo->db==db ); - nField = pKeyInfo->nField+pKeyInfo->nXField; + nField = pKeyInfo->nAllField; }else if( pOp->p4type==P4_INT32 ){ nField = pOp->p4.i; } assert( pOp->p1>=0 ); assert( nField>=0 ); testcase( nField==0 ); /* Table with INTEGER PRIMARY KEY and nothing else */ - pCur = allocateCursor(p, pOp->p1, nField, iDb, CURTYPE_BTREE); + pCur = allocateCursor(p, pOp->p1, nField, CURTYPE_BTREE); if( pCur==0 ) goto no_mem; + pCur->iDb = iDb; pCur->nullRow = 1; pCur->isOrdered = 1; pCur->pgnoRoot = p2; @@ -3405,38 +4453,81 @@ case OP_OpenWrite: /* Set the VdbeCursor.isTable variable. Previous versions of ** SQLite used to check if the root-page flags were sane at this point ** and report database corruption if they were not, but this check has - ** since moved into the btree layer. */ + ** since moved into the btree layer. */ pCur->isTable = pOp->p4type!=P4_KEYINFO; open_cursor_set_hints: assert( OPFLAG_BULKCSR==BTREE_BULKLOAD ); assert( OPFLAG_SEEKEQ==BTREE_SEEK_EQ ); testcase( pOp->p5 & OPFLAG_BULKCSR ); -#ifdef SQLITE_ENABLE_CURSOR_HINTS testcase( pOp->p2 & OPFLAG_SEEKEQ ); -#endif sqlite3BtreeCursorHintFlags(pCur->uc.pCursor, (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ))); + if( rc ) goto abort_due_to_error; break; } -/* Opcode: OpenEphemeral P1 P2 * P4 P5 -** Synopsis: nColumn=P2 +/* Opcode: OpenDup P1 P2 * * * ** -** Open a new cursor P1 to a transient table. -** The cursor is always opened read/write even if -** the main database is read-only. The ephemeral -** table is deleted automatically when the cursor is closed. +** Open a new cursor P1 that points to the same ephemeral table as +** cursor P2. The P2 cursor must have been opened by a prior OP_OpenEphemeral +** opcode. Only ephemeral cursors may be duplicated. ** -** P2 is the number of columns in the ephemeral table. -** The cursor points to a BTree table if P4==0 and to a BTree index -** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure +** Duplicate ephemeral cursors are used for self-joins of materialized views. +*/ +case OP_OpenDup: { /* ncycle */ + VdbeCursor *pOrig; /* The original cursor to be duplicated */ + VdbeCursor *pCx; /* The new cursor */ + + pOrig = p->apCsr[pOp->p2]; + assert( pOrig ); + assert( pOrig->isEphemeral ); /* Only ephemeral cursors can be duplicated */ + + pCx = allocateCursor(p, pOp->p1, pOrig->nField, CURTYPE_BTREE); + if( pCx==0 ) goto no_mem; + pCx->nullRow = 1; + pCx->isEphemeral = 1; + pCx->pKeyInfo = pOrig->pKeyInfo; + pCx->isTable = pOrig->isTable; + pCx->pgnoRoot = pOrig->pgnoRoot; + pCx->isOrdered = pOrig->isOrdered; + pCx->ub.pBtx = pOrig->ub.pBtx; + pCx->noReuse = 1; + pOrig->noReuse = 1; + rc = sqlite3BtreeCursor(pCx->ub.pBtx, pCx->pgnoRoot, BTREE_WRCSR, + pCx->pKeyInfo, pCx->uc.pCursor); + /* The sqlite3BtreeCursor() routine can only fail for the first cursor + ** opened for a database. Since there is already an open cursor when this + ** opcode is run, the sqlite3BtreeCursor() cannot fail */ + assert( rc==SQLITE_OK ); + break; +} + + +/* Opcode: OpenEphemeral P1 P2 P3 P4 P5 +** Synopsis: nColumn=P2 +** +** Open a new cursor P1 to a transient table. +** The cursor is always opened read/write even if +** the main database is read-only. The ephemeral +** table is deleted automatically when the cursor is closed. +** +** If the cursor P1 is already opened on an ephemeral table, the table +** is cleared (all content is erased). +** +** P2 is the number of columns in the ephemeral table. +** The cursor points to a BTree table if P4==0 and to a BTree index +** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure ** that defines the format of keys in the index. ** ** The P5 parameter can be a mask of the BTREE_* flags defined ** in btree.h. These flags control aspects of the operation of ** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are ** added automatically. +** +** If P3 is positive, then reg[P3] is modified slightly so that it +** can be used as zero-length data for OP_Insert. This is an optimization +** that avoids an extra OP_Blob opcode to initialize that register. */ /* Opcode: OpenAutoindex P1 P2 * P4 * ** Synopsis: nColumn=P2 @@ -3446,12 +4537,12 @@ case OP_OpenWrite: ** by this opcode will be used for automatically created transient ** indices in joins. */ -case OP_OpenAutoindex: -case OP_OpenEphemeral: { +case OP_OpenAutoindex: /* ncycle */ +case OP_OpenEphemeral: { /* ncycle */ VdbeCursor *pCx; KeyInfo *pKeyInfo; - static const int vfsFlags = + static const int vfsFlags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_EXCLUSIVE | @@ -3459,41 +4550,71 @@ case OP_OpenEphemeral: { SQLITE_OPEN_TRANSIENT_DB; assert( pOp->p1>=0 ); assert( pOp->p2>=0 ); - pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_BTREE); - if( pCx==0 ) goto no_mem; - pCx->nullRow = 1; - pCx->isEphemeral = 1; - rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBt, - BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags); - if( rc==SQLITE_OK ){ - rc = sqlite3BtreeBeginTrans(pCx->pBt, 1); - } - if( rc==SQLITE_OK ){ - /* If a transient index is required, create it by calling - ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before - ** opening it. If a transient table is required, just use the - ** automatically created table with root-page 1 (an BLOB_INTKEY table). - */ - if( (pKeyInfo = pOp->p4.pKeyInfo)!=0 ){ - int pgno; - assert( pOp->p4type==P4_KEYINFO ); - rc = sqlite3BtreeCreateTable(pCx->pBt, &pgno, BTREE_BLOBKEY | pOp->p5); + if( pOp->p3>0 ){ + /* Make register reg[P3] into a value that can be used as the data + ** form sqlite3BtreeInsert() where the length of the data is zero. */ + assert( pOp->p2==0 ); /* Only used when number of columns is zero */ + assert( pOp->opcode==OP_OpenEphemeral ); + assert( aMem[pOp->p3].flags & MEM_Null ); + aMem[pOp->p3].n = 0; + aMem[pOp->p3].z = ""; + } + pCx = p->apCsr[pOp->p1]; + if( pCx && !pCx->noReuse && ALWAYS(pOp->p2<=pCx->nField) ){ + /* If the ephemeral table is already open and has no duplicates from + ** OP_OpenDup, then erase all existing content so that the table is + ** empty again, rather than creating a new table. */ + assert( pCx->isEphemeral ); + pCx->seqCount = 0; + pCx->cacheStatus = CACHE_STALE; + rc = sqlite3BtreeClearTable(pCx->ub.pBtx, pCx->pgnoRoot, 0); + }else{ + pCx = allocateCursor(p, pOp->p1, pOp->p2, CURTYPE_BTREE); + if( pCx==0 ) goto no_mem; + pCx->isEphemeral = 1; + rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->ub.pBtx, + BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, + vfsFlags); + if( rc==SQLITE_OK ){ + rc = sqlite3BtreeBeginTrans(pCx->ub.pBtx, 1, 0); if( rc==SQLITE_OK ){ - assert( pgno==MASTER_ROOT+1 ); - assert( pKeyInfo->db==db ); - assert( pKeyInfo->enc==ENC(db) ); - pCx->pKeyInfo = pKeyInfo; - rc = sqlite3BtreeCursor(pCx->pBt, pgno, BTREE_WRCSR, - pKeyInfo, pCx->uc.pCursor); + /* If a transient index is required, create it by calling + ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before + ** opening it. If a transient table is required, just use the + ** automatically created table with root-page 1 (an BLOB_INTKEY table). + */ + if( (pCx->pKeyInfo = pKeyInfo = pOp->p4.pKeyInfo)!=0 ){ + assert( pOp->p4type==P4_KEYINFO ); + rc = sqlite3BtreeCreateTable(pCx->ub.pBtx, &pCx->pgnoRoot, + BTREE_BLOBKEY | pOp->p5); + if( rc==SQLITE_OK ){ + assert( pCx->pgnoRoot==SCHEMA_ROOT+1 ); + assert( pKeyInfo->db==db ); + assert( pKeyInfo->enc==ENC(db) ); + rc = sqlite3BtreeCursor(pCx->ub.pBtx, pCx->pgnoRoot, BTREE_WRCSR, + pKeyInfo, pCx->uc.pCursor); + } + pCx->isTable = 0; + }else{ + pCx->pgnoRoot = SCHEMA_ROOT; + rc = sqlite3BtreeCursor(pCx->ub.pBtx, SCHEMA_ROOT, BTREE_WRCSR, + 0, pCx->uc.pCursor); + pCx->isTable = 1; + } + } + pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED); + assert( p->apCsr[pOp->p1]==pCx ); + if( rc ){ + assert( !sqlite3BtreeClosesWithCursor(pCx->ub.pBtx, pCx->uc.pCursor) ); + sqlite3BtreeClose(pCx->ub.pBtx); + p->apCsr[pOp->p1] = 0; /* Not required; helps with static analysis */ + }else{ + assert( sqlite3BtreeClosesWithCursor(pCx->ub.pBtx, pCx->uc.pCursor) ); } - pCx->isTable = 0; - }else{ - rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, BTREE_WRCSR, - 0, pCx->uc.pCursor); - pCx->isTable = 1; } } - pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED); + if( rc ) goto abort_due_to_error; + pCx->nullRow = 1; break; } @@ -3512,12 +4633,13 @@ case OP_SorterOpen: { assert( pOp->p1>=0 ); assert( pOp->p2>=0 ); - pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_SORTER); + pCx = allocateCursor(p, pOp->p1, pOp->p2, CURTYPE_SORTER); if( pCx==0 ) goto no_mem; pCx->pKeyInfo = pOp->p4.pKeyInfo; assert( pCx->pKeyInfo->db==db ); assert( pCx->pKeyInfo->enc==ENC(db) ); rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx); + if( rc ) goto abort_due_to_error; break; } @@ -3544,7 +4666,7 @@ case OP_SequenceTest: { ** ** Open a new cursor that points to a fake table that contains a single ** row of data. The content of that one row is the content of memory -** register P2. In other words, cursor P1 becomes an alias for the +** register P2. In other words, cursor P1 becomes an alias for the ** MEM_Blob content contained in register P2. ** ** A pseudo-table created by this opcode is used to hold a single @@ -3553,18 +4675,24 @@ case OP_SequenceTest: { ** is the only cursor opcode that works with a pseudo-table. ** ** P3 is the number of fields in the records that will be stored by -** the pseudo-table. +** the pseudo-table. If P2 is 0 or negative then the pseudo-cursor +** will return NULL for every column. */ case OP_OpenPseudo: { VdbeCursor *pCx; assert( pOp->p1>=0 ); assert( pOp->p3>=0 ); - pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, CURTYPE_PSEUDO); + pCx = allocateCursor(p, pOp->p1, pOp->p3, CURTYPE_PSEUDO); if( pCx==0 ) goto no_mem; pCx->nullRow = 1; - pCx->uc.pseudoTableReg = pOp->p2; + pCx->seekResult = pOp->p2; pCx->isTable = 1; + /* Give this pseudo-cursor a fake BtCursor pointer so that pCx + ** can be safely passed to sqlite3VdbeCursorMoveto(). This avoids a test + ** for pCx->eCurType==CURTYPE_BTREE inside of sqlite3VdbeCursorMoveto() + ** which is a performance optimization */ + pCx->uc.pCursor = sqlite3BtreeFakeValidCursor(); assert( pOp->p5==0 ); break; } @@ -3574,7 +4702,7 @@ case OP_OpenPseudo: { ** Close a cursor previously opened as P1. If P1 is not ** currently open, this instruction is a no-op. */ -case OP_Close: { +case OP_Close: { /* ncycle */ assert( pOp->p1>=0 && pOp->p1<p->nCursor ); sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]); p->apCsr[pOp->p1] = 0; @@ -3604,21 +4732,23 @@ case OP_ColumnsUsed: { /* Opcode: SeekGE P1 P2 P3 P4 * ** Synopsis: key=r[P3@P4] ** -** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), -** use the value in register P3 as the key. If cursor P1 refers -** to an SQL index, then P3 is the first in an array of P4 registers -** that are used as an unpacked index key. +** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), +** use the value in register P3 as the key. If cursor P1 refers +** to an SQL index, then P3 is the first in an array of P4 registers +** that are used as an unpacked index key. ** -** Reposition cursor P1 so that it points to the smallest entry that -** is greater than or equal to the key value. If there are no records +** Reposition cursor P1 so that it points to the smallest entry that +** is greater than or equal to the key value. If there are no records ** greater than or equal to the key and P2 is not zero, then jump to P2. ** ** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this -** opcode will always land on a record that equally equals the key, or -** else jump immediately to P2. When the cursor is OPFLAG_SEEKEQ, this -** opcode must be followed by an IdxLE opcode with the same arguments. -** The IdxLE opcode will be skipped if this opcode succeeds, but the -** IdxLE opcode will be used on subsequent loop iterations. +** opcode will either land on a record that exactly matches the key, or +** else it will cause a jump to P2. When the cursor is OPFLAG_SEEKEQ, +** this opcode must be followed by an IdxLE opcode with the same arguments. +** The IdxGT opcode will be skipped if this opcode succeeds, but the +** IdxGT opcode will be used on subsequent loop iterations. The +** OPFLAG_SEEKEQ flags is a hint to the btree layer to say that this +** is an equality search. ** ** This opcode leaves the cursor configured to move in forward order, ** from the beginning toward the end. In other words, the cursor is @@ -3629,13 +4759,13 @@ case OP_ColumnsUsed: { /* Opcode: SeekGT P1 P2 P3 P4 * ** Synopsis: key=r[P3@P4] ** -** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), -** use the value in register P3 as a key. If cursor P1 refers -** to an SQL index, then P3 is the first in an array of P4 registers -** that are used as an unpacked index key. +** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), +** use the value in register P3 as a key. If cursor P1 refers +** to an SQL index, then P3 is the first in an array of P4 registers +** that are used as an unpacked index key. ** -** Reposition cursor P1 so that it points to the smallest entry that -** is greater than the key value. If there are no records greater than +** Reposition cursor P1 so that it points to the smallest entry that +** is greater than the key value. If there are no records greater than ** the key and P2 is not zero, then jump to P2. ** ** This opcode leaves the cursor configured to move in forward order, @@ -3644,16 +4774,16 @@ case OP_ColumnsUsed: { ** ** See also: Found, NotFound, SeekLt, SeekGe, SeekLe */ -/* Opcode: SeekLT P1 P2 P3 P4 * +/* Opcode: SeekLT P1 P2 P3 P4 * ** Synopsis: key=r[P3@P4] ** -** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), -** use the value in register P3 as a key. If cursor P1 refers -** to an SQL index, then P3 is the first in an array of P4 registers -** that are used as an unpacked index key. +** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), +** use the value in register P3 as a key. If cursor P1 refers +** to an SQL index, then P3 is the first in an array of P4 registers +** that are used as an unpacked index key. ** -** Reposition cursor P1 so that it points to the largest entry that -** is less than the key value. If there are no records less than +** Reposition cursor P1 so that it points to the largest entry that +** is less than the key value. If there are no records less than ** the key and P2 is not zero, then jump to P2. ** ** This opcode leaves the cursor configured to move in reverse order, @@ -3665,13 +4795,13 @@ case OP_ColumnsUsed: { /* Opcode: SeekLE P1 P2 P3 P4 * ** Synopsis: key=r[P3@P4] ** -** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), -** use the value in register P3 as a key. If cursor P1 refers -** to an SQL index, then P3 is the first in an array of P4 registers -** that are used as an unpacked index key. +** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), +** use the value in register P3 as a key. If cursor P1 refers +** to an SQL index, then P3 is the first in an array of P4 registers +** that are used as an unpacked index key. ** -** Reposition cursor P1 so that it points to the largest entry that -** is less than or equal to the key value. If there are no records +** Reposition cursor P1 so that it points to the largest entry that +** is less than or equal to the key value. If there are no records ** less than or equal to the key and P2 is not zero, then jump to P2. ** ** This opcode leaves the cursor configured to move in reverse order, @@ -3679,18 +4809,20 @@ case OP_ColumnsUsed: { ** configured to use Prev, not Next. ** ** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this -** opcode will always land on a record that equally equals the key, or -** else jump immediately to P2. When the cursor is OPFLAG_SEEKEQ, this -** opcode must be followed by an IdxGE opcode with the same arguments. +** opcode will either land on a record that exactly matches the key, or +** else it will cause a jump to P2. When the cursor is OPFLAG_SEEKEQ, +** this opcode must be followed by an IdxLE opcode with the same arguments. ** The IdxGE opcode will be skipped if this opcode succeeds, but the -** IdxGE opcode will be used on subsequent loop iterations. +** IdxGE opcode will be used on subsequent loop iterations. The +** OPFLAG_SEEKEQ flags is a hint to the btree layer to say that this +** is an equality search. ** ** See also: Found, NotFound, SeekGt, SeekGe, SeekLt */ -case OP_SeekLT: /* jump, in3 */ -case OP_SeekLE: /* jump, in3 */ -case OP_SeekGE: /* jump, in3 */ -case OP_SeekGT: { /* jump, in3 */ +case OP_SeekLT: /* jump0, in3, group, ncycle */ +case OP_SeekLE: /* jump0, in3, group, ncycle */ +case OP_SeekGE: /* jump0, in3, group, ncycle */ +case OP_SeekGT: { /* jump0, in3, group, ncycle */ int res; /* Comparison result */ int oc; /* Opcode */ VdbeCursor *pC; /* The cursor to seek */ @@ -3716,28 +4848,41 @@ case OP_SeekGT: { /* jump, in3 */ pC->seekOp = pOp->opcode; #endif + pC->deferredMoveto = 0; + pC->cacheStatus = CACHE_STALE; if( pC->isTable ){ - /* The BTREE_SEEK_EQ flag is only set on index cursors */ - assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0 ); + u16 flags3, newType; + /* The OPFLAG_SEEKEQ/BTREE_SEEK_EQ flag is only set on index cursors */ + assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0 + || CORRUPT_DB ); /* The input value in P3 might be of any type: integer, real, string, ** blob, or NULL. But it needs to be an integer before we can do ** the seek, so convert it. */ pIn3 = &aMem[pOp->p3]; - if( (pIn3->flags & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){ + flags3 = pIn3->flags; + if( (flags3 & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Str))==MEM_Str ){ applyNumericAffinity(pIn3, 0); } - iKey = sqlite3VdbeIntValue(pIn3); + iKey = sqlite3VdbeIntValue(pIn3); /* Get the integer key value */ + newType = pIn3->flags; /* Record the type after applying numeric affinity */ + pIn3->flags = flags3; /* But convert the type back to its original */ /* If the P3 value could not be converted into an integer without ** loss of information, then special processing is required... */ - if( (pIn3->flags & MEM_Int)==0 ){ - if( (pIn3->flags & MEM_Real)==0 ){ - /* If the P3 value cannot be converted into any kind of a number, - ** then the seek is not possible, so jump to P2 */ - VdbeBranchTaken(1,2); goto jump_to_p2; - break; + if( (newType & (MEM_Int|MEM_IntReal))==0 ){ + int c; + if( (newType & MEM_Real)==0 ){ + if( (newType & MEM_Null) || oc>=OP_SeekGE ){ + VdbeBranchTaken(1,2); + goto jump_to_p2; + }else{ + rc = sqlite3BtreeLast(pC->uc.pCursor, &res); + if( rc!=SQLITE_OK ) goto abort_due_to_error; + goto seek_not_found; + } } + c = sqlite3IntFloatCompare(iKey, pIn3->u.r); /* If the approximation iKey is larger than the actual real search ** term, substitute >= for > and < for <=. e.g. if the search term @@ -3746,7 +4891,7 @@ case OP_SeekGT: { /* jump, in3 */ ** (x > 4.9) -> (x >= 5) ** (x <= 4.9) -> (x < 5) */ - if( pIn3->u.r<(double)iKey ){ + if( c>0 ){ assert( OP_SeekGE==(OP_SeekGT-1) ); assert( OP_SeekLT==(OP_SeekLE-1) ); assert( (OP_SeekLE & 0x0001)==(OP_SeekGT & 0x0001) ); @@ -3755,27 +4900,30 @@ case OP_SeekGT: { /* jump, in3 */ /* If the approximation iKey is smaller than the actual real search ** term, substitute <= for < and > for >=. */ - else if( pIn3->u.r>(double)iKey ){ + else if( c<0 ){ assert( OP_SeekLE==(OP_SeekLT+1) ); assert( OP_SeekGT==(OP_SeekGE+1) ); assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) ); if( (oc & 0x0001)==(OP_SeekLT & 0x0001) ) oc++; } - } - rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)iKey, 0, &res); + } + rc = sqlite3BtreeTableMoveto(pC->uc.pCursor, (u64)iKey, 0, &res); pC->movetoTarget = iKey; /* Used by OP_Delete */ if( rc!=SQLITE_OK ){ goto abort_due_to_error; } }else{ - /* For a cursor with the BTREE_SEEK_EQ hint, only the OP_SeekGE and - ** OP_SeekLE opcodes are allowed, and these must be immediately followed - ** by an OP_IdxGT or OP_IdxLT opcode, respectively, with the same key. + /* For a cursor with the OPFLAG_SEEKEQ/BTREE_SEEK_EQ hint, only the + ** OP_SeekGE and OP_SeekLE opcodes are allowed, and these must be + ** immediately followed by an OP_IdxGT or OP_IdxLT opcode, respectively, + ** with the same key. */ if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ) ){ eqOnly = 1; assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE ); assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT ); + assert( pOp->opcode==OP_SeekGE || pOp[1].opcode==OP_IdxLT ); + assert( pOp->opcode==OP_SeekLE || pOp[1].opcode==OP_IdxGT ); assert( pOp[1].p1==pOp[0].p1 ); assert( pOp[1].p2==pOp[0].p2 ); assert( pOp[1].p3==pOp[0].p3 ); @@ -3803,11 +4951,16 @@ case OP_SeekGT: { /* jump, in3 */ r.aMem = &aMem[pOp->p3]; #ifdef SQLITE_DEBUG - { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); } + { + int i; + for(i=0; i<r.nField; i++){ + assert( memIsValid(&r.aMem[i]) ); + if( i>0 ) REGISTER_TRACE(pOp->p3+i, &r.aMem[i]); + } + } #endif - ExpandBlob(r.aMem); r.eqSeen = 0; - rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, &r, 0, 0, &res); + rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, &r, &res); if( rc!=SQLITE_OK ){ goto abort_due_to_error; } @@ -3816,16 +4969,21 @@ case OP_SeekGT: { /* jump, in3 */ goto seek_not_found; } } - pC->deferredMoveto = 0; - pC->cacheStatus = CACHE_STALE; #ifdef SQLITE_TEST sqlite3_search_count++; #endif if( oc>=OP_SeekGE ){ assert( oc==OP_SeekGE || oc==OP_SeekGT ); if( res<0 || (res==0 && oc==OP_SeekGT) ){ res = 0; - rc = sqlite3BtreeNext(pC->uc.pCursor, &res); - if( rc!=SQLITE_OK ) goto abort_due_to_error; + rc = sqlite3BtreeNext(pC->uc.pCursor, 0); + if( rc!=SQLITE_OK ){ + if( rc==SQLITE_DONE ){ + rc = SQLITE_OK; + res = 1; + }else{ + goto abort_due_to_error; + } + } }else{ res = 0; } @@ -3833,8 +4991,15 @@ case OP_SeekGT: { /* jump, in3 */ assert( oc==OP_SeekLT || oc==OP_SeekLE ); if( res>0 || (res==0 && oc==OP_SeekLT) ){ res = 0; - rc = sqlite3BtreePrevious(pC->uc.pCursor, &res); - if( rc!=SQLITE_OK ) goto abort_due_to_error; + rc = sqlite3BtreePrevious(pC->uc.pCursor, 0); + if( rc!=SQLITE_OK ){ + if( rc==SQLITE_DONE ){ + rc = SQLITE_OK; + res = 1; + }else{ + goto abort_due_to_error; + } + } }else{ /* res might be negative because the table is empty. Check to ** see if this is the case. @@ -3853,7 +5018,240 @@ case OP_SeekGT: { /* jump, in3 */ } break; } - + + +/* Opcode: SeekScan P1 P2 * * P5 +** Synopsis: Scan-ahead up to P1 rows +** +** This opcode is a prefix opcode to OP_SeekGE. In other words, this +** opcode must be immediately followed by OP_SeekGE. This constraint is +** checked by assert() statements. +** +** This opcode uses the P1 through P4 operands of the subsequent +** OP_SeekGE. In the text that follows, the operands of the subsequent +** OP_SeekGE opcode are denoted as SeekOP.P1 through SeekOP.P4. Only +** the P1, P2 and P5 operands of this opcode are also used, and are called +** This.P1, This.P2 and This.P5. +** +** This opcode helps to optimize IN operators on a multi-column index +** where the IN operator is on the later terms of the index by avoiding +** unnecessary seeks on the btree, substituting steps to the next row +** of the b-tree instead. A correct answer is obtained if this opcode +** is omitted or is a no-op. +** +** The SeekGE.P3 and SeekGE.P4 operands identify an unpacked key which +** is the desired entry that we want the cursor SeekGE.P1 to be pointing +** to. Call this SeekGE.P3/P4 row the "target". +** +** If the SeekGE.P1 cursor is not currently pointing to a valid row, +** then this opcode is a no-op and control passes through into the OP_SeekGE. +** +** If the SeekGE.P1 cursor is pointing to a valid row, then that row +** might be the target row, or it might be near and slightly before the +** target row, or it might be after the target row. If the cursor is +** currently before the target row, then this opcode attempts to position +** the cursor on or after the target row by invoking sqlite3BtreeStep() +** on the cursor between 1 and This.P1 times. +** +** The This.P5 parameter is a flag that indicates what to do if the +** cursor ends up pointing at a valid row that is past the target +** row. If This.P5 is false (0) then a jump is made to SeekGE.P2. If +** This.P5 is true (non-zero) then a jump is made to This.P2. The P5==0 +** case occurs when there are no inequality constraints to the right of +** the IN constraint. The jump to SeekGE.P2 ends the loop. The P5!=0 case +** occurs when there are inequality constraints to the right of the IN +** operator. In that case, the This.P2 will point either directly to or +** to setup code prior to the OP_IdxGT or OP_IdxGE opcode that checks for +** loop terminate. +** +** Possible outcomes from this opcode:<ol> +** +** <li> If the cursor is initially not pointed to any valid row, then +** fall through into the subsequent OP_SeekGE opcode. +** +** <li> If the cursor is left pointing to a row that is before the target +** row, even after making as many as This.P1 calls to +** sqlite3BtreeNext(), then also fall through into OP_SeekGE. +** +** <li> If the cursor is left pointing at the target row, either because it +** was at the target row to begin with or because one or more +** sqlite3BtreeNext() calls moved the cursor to the target row, +** then jump to This.P2.., +** +** <li> If the cursor started out before the target row and a call to +** to sqlite3BtreeNext() moved the cursor off the end of the index +** (indicating that the target row definitely does not exist in the +** btree) then jump to SeekGE.P2, ending the loop. +** +** <li> If the cursor ends up on a valid row that is past the target row +** (indicating that the target row does not exist in the btree) then +** jump to SeekOP.P2 if This.P5==0 or to This.P2 if This.P5>0. +** </ol> +*/ +case OP_SeekScan: { /* ncycle */ + VdbeCursor *pC; + int res; + int nStep; + UnpackedRecord r; + + assert( pOp[1].opcode==OP_SeekGE ); + + /* If pOp->p5 is clear, then pOp->p2 points to the first instruction past the + ** OP_IdxGT that follows the OP_SeekGE. Otherwise, it points to the first + ** opcode past the OP_SeekGE itself. */ + assert( pOp->p2>=(int)(pOp-aOp)+2 ); +#ifdef SQLITE_DEBUG + if( pOp->p5==0 ){ + /* There are no inequality constraints following the IN constraint. */ + assert( pOp[1].p1==aOp[pOp->p2-1].p1 ); + assert( pOp[1].p2==aOp[pOp->p2-1].p2 ); + assert( pOp[1].p3==aOp[pOp->p2-1].p3 ); + assert( aOp[pOp->p2-1].opcode==OP_IdxGT + || aOp[pOp->p2-1].opcode==OP_IdxGE ); + testcase( aOp[pOp->p2-1].opcode==OP_IdxGE ); + }else{ + /* There are inequality constraints. */ + assert( pOp->p2==(int)(pOp-aOp)+2 ); + assert( aOp[pOp->p2-1].opcode==OP_SeekGE ); + } +#endif + + assert( pOp->p1>0 ); + pC = p->apCsr[pOp[1].p1]; + assert( pC!=0 ); + assert( pC->eCurType==CURTYPE_BTREE ); + assert( !pC->isTable ); + if( !sqlite3BtreeCursorIsValidNN(pC->uc.pCursor) ){ +#ifdef SQLITE_DEBUG + if( db->flags&SQLITE_VdbeTrace ){ + printf("... cursor not valid - fall through\n"); + } +#endif + break; + } + nStep = pOp->p1; + assert( nStep>=1 ); + r.pKeyInfo = pC->pKeyInfo; + r.nField = (u16)pOp[1].p4.i; + r.default_rc = 0; + r.aMem = &aMem[pOp[1].p3]; +#ifdef SQLITE_DEBUG + { + int i; + for(i=0; i<r.nField; i++){ + assert( memIsValid(&r.aMem[i]) ); + REGISTER_TRACE(pOp[1].p3+i, &aMem[pOp[1].p3+i]); + } + } +#endif + res = 0; /* Not needed. Only used to silence a warning. */ + while(1){ + rc = sqlite3VdbeIdxKeyCompare(db, pC, &r, &res); + if( rc ) goto abort_due_to_error; + if( res>0 && pOp->p5==0 ){ + seekscan_search_fail: + /* Jump to SeekGE.P2, ending the loop */ +#ifdef SQLITE_DEBUG + if( db->flags&SQLITE_VdbeTrace ){ + printf("... %d steps and then skip\n", pOp->p1 - nStep); + } +#endif + VdbeBranchTaken(1,3); + pOp++; + goto jump_to_p2; + } + if( res>=0 ){ + /* Jump to This.P2, bypassing the OP_SeekGE opcode */ +#ifdef SQLITE_DEBUG + if( db->flags&SQLITE_VdbeTrace ){ + printf("... %d steps and then success\n", pOp->p1 - nStep); + } +#endif + VdbeBranchTaken(2,3); + goto jump_to_p2; + break; + } + if( nStep<=0 ){ +#ifdef SQLITE_DEBUG + if( db->flags&SQLITE_VdbeTrace ){ + printf("... fall through after %d steps\n", pOp->p1); + } +#endif + VdbeBranchTaken(0,3); + break; + } + nStep--; + pC->cacheStatus = CACHE_STALE; + rc = sqlite3BtreeNext(pC->uc.pCursor, 0); + if( rc ){ + if( rc==SQLITE_DONE ){ + rc = SQLITE_OK; + goto seekscan_search_fail; + }else{ + goto abort_due_to_error; + } + } + } + + break; +} + + +/* Opcode: SeekHit P1 P2 P3 * * +** Synopsis: set P2<=seekHit<=P3 +** +** Increase or decrease the seekHit value for cursor P1, if necessary, +** so that it is no less than P2 and no greater than P3. +** +** The seekHit integer represents the maximum of terms in an index for which +** there is known to be at least one match. If the seekHit value is smaller +** than the total number of equality terms in an index lookup, then the +** OP_IfNoHope opcode might run to see if the IN loop can be abandoned +** early, thus saving work. This is part of the IN-early-out optimization. +** +** P1 must be a valid b-tree cursor. +*/ +case OP_SeekHit: { /* ncycle */ + VdbeCursor *pC; + assert( pOp->p1>=0 && pOp->p1<p->nCursor ); + pC = p->apCsr[pOp->p1]; + assert( pC!=0 ); + assert( pOp->p3>=pOp->p2 ); + if( pC->seekHit<pOp->p2 ){ +#ifdef SQLITE_DEBUG + if( db->flags&SQLITE_VdbeTrace ){ + printf("seekHit changes from %d to %d\n", pC->seekHit, pOp->p2); + } +#endif + pC->seekHit = pOp->p2; + }else if( pC->seekHit>pOp->p3 ){ +#ifdef SQLITE_DEBUG + if( db->flags&SQLITE_VdbeTrace ){ + printf("seekHit changes from %d to %d\n", pC->seekHit, pOp->p3); + } +#endif + pC->seekHit = pOp->p3; + } + break; +} + +/* Opcode: IfNotOpen P1 P2 * * * +** Synopsis: if( !csr[P1] ) goto P2 +** +** If cursor P1 is not open or if P1 is set to a NULL row using the +** OP_NullRow opcode, then jump to instruction P2. Otherwise, fall through. +*/ +case OP_IfNotOpen: { /* jump */ + VdbeCursor *pCur; + + assert( pOp->p1>=0 && pOp->p1<p->nCursor ); + pCur = p->apCsr[pOp->p1]; + VdbeBranchTaken(pCur==0 || pCur->nullRow, 2); + if( pCur==0 || pCur->nullRow ){ + goto jump_to_p2_and_check_for_interrupt; + } + break; +} /* Opcode: Found P1 P2 P3 P4 * ** Synopsis: key=r[P3@P4] @@ -3878,9 +5276,9 @@ case OP_SeekGT: { /* jump, in3 */ ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If ** P4>0 then register P3 is the first of P4 registers that form an unpacked ** record. -** +** ** Cursor P1 is on an index btree. If the record identified by P3 and P4 -** is not the prefix of any entry in P1 then a jump is made to P2. If P1 +** is not the prefix of any entry in P1 then a jump is made to P2. If P1 ** does contain an entry whose prefix matches the P3/P4 record then control ** falls through to the next instruction and P1 is left pointing at the ** matching entry. @@ -3889,7 +5287,38 @@ case OP_SeekGT: { /* jump, in3 */ ** advanced in either direction. In other words, the Next and Prev ** opcodes do not work after this operation. ** -** See also: Found, NotExists, NoConflict +** See also: Found, NotExists, NoConflict, IfNoHope +*/ +/* Opcode: IfNoHope P1 P2 P3 P4 * +** Synopsis: key=r[P3@P4] +** +** Register P3 is the first of P4 registers that form an unpacked +** record. Cursor P1 is an index btree. P2 is a jump destination. +** In other words, the operands to this opcode are the same as the +** operands to OP_NotFound and OP_IdxGT. +** +** This opcode is an optimization attempt only. If this opcode always +** falls through, the correct answer is still obtained, but extra work +** is performed. +** +** A value of N in the seekHit flag of cursor P1 means that there exists +** a key P3:N that will match some record in the index. We want to know +** if it is possible for a record P3:P4 to match some record in the +** index. If it is not possible, we can skip some work. So if seekHit +** is less than P4, attempt to find out if a match is possible by running +** OP_NotFound. +** +** This opcode is used in IN clause processing for a multi-column key. +** If an IN clause is attached to an element of the key other than the +** left-most element, and if there are no matches on the most recent +** seek over the whole key, then it might be that one of the key element +** to the left is prohibiting a match, and hence there is "no hope" of +** any match regardless of how many IN clause elements are checked. +** In such a case, we abandon the IN clause search early, using this +** opcode. The opcode name comes from the fact that the +** jump is taken if there is "no hope" of achieving a match. +** +** See also: NotFound, SeekHit */ /* Opcode: NoConflict P1 P2 P3 P4 * ** Synopsis: key=r[P3@P4] @@ -3897,7 +5326,7 @@ case OP_SeekGT: { /* jump, in3 */ ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If ** P4>0 then register P3 is the first of P4 registers that form an unpacked ** record. -** +** ** Cursor P1 is on an index btree. If the record identified by P3 and P4 ** contains any NULL value, jump immediately to P2. If all terms of the ** record are not-NULL then a check is done to determine if any row in the @@ -3914,18 +5343,28 @@ case OP_SeekGT: { /* jump, in3 */ ** ** See also: NotFound, Found, NotExists */ -case OP_NoConflict: /* jump, in3 */ -case OP_NotFound: /* jump, in3 */ -case OP_Found: { /* jump, in3 */ +case OP_IfNoHope: { /* jump, in3, ncycle */ + VdbeCursor *pC; + assert( pOp->p1>=0 && pOp->p1<p->nCursor ); + pC = p->apCsr[pOp->p1]; + assert( pC!=0 ); +#ifdef SQLITE_DEBUG + if( db->flags&SQLITE_VdbeTrace ){ + printf("seekHit is %d\n", pC->seekHit); + } +#endif + if( pC->seekHit>=pOp->p4.i ) break; + /* Fall through into OP_NotFound */ + /* no break */ deliberate_fall_through +} +case OP_NoConflict: /* jump, in3, ncycle */ +case OP_NotFound: /* jump, in3, ncycle */ +case OP_Found: { /* jump, in3, ncycle */ int alreadyExists; - int takeJump; int ii; VdbeCursor *pC; - int res; - char *pFree; UnpackedRecord *pIdxKey; UnpackedRecord r; - char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*4 + 7]; #ifdef SQLITE_TEST if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++; @@ -3938,52 +5377,42 @@ case OP_Found: { /* jump, in3 */ #ifdef SQLITE_DEBUG pC->seekOp = pOp->opcode; #endif - pIn3 = &aMem[pOp->p3]; + r.aMem = &aMem[pOp->p3]; assert( pC->eCurType==CURTYPE_BTREE ); assert( pC->uc.pCursor!=0 ); assert( pC->isTable==0 ); - pFree = 0; - if( pOp->p4.i>0 ){ + r.nField = (u16)pOp->p4.i; + if( r.nField>0 ){ + /* Key values in an array of registers */ r.pKeyInfo = pC->pKeyInfo; - r.nField = (u16)pOp->p4.i; - r.aMem = pIn3; + r.default_rc = 0; +#ifdef SQLITE_DEBUG + (void)sqlite3FaultSim(50); /* For use by --counter in TH3 */ for(ii=0; ii<r.nField; ii++){ assert( memIsValid(&r.aMem[ii]) ); - ExpandBlob(&r.aMem[ii]); -#ifdef SQLITE_DEBUG + assert( (r.aMem[ii].flags & MEM_Zero)==0 || r.aMem[ii].n==0 ); if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]); -#endif } - pIdxKey = &r; +#endif + rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, &r, &pC->seekResult); }else{ - pIdxKey = sqlite3VdbeAllocUnpackedRecord( - pC->pKeyInfo, aTempRec, sizeof(aTempRec), &pFree - ); + /* Composite key generated by OP_MakeRecord */ + assert( r.aMem->flags & MEM_Blob ); + assert( pOp->opcode!=OP_NoConflict ); + rc = ExpandBlob(r.aMem); + assert( rc==SQLITE_OK || rc==SQLITE_NOMEM ); + if( rc ) goto no_mem; + pIdxKey = sqlite3VdbeAllocUnpackedRecord(pC->pKeyInfo); if( pIdxKey==0 ) goto no_mem; - assert( pIn3->flags & MEM_Blob ); - ExpandBlob(pIn3); - sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey); - } - pIdxKey->default_rc = 0; - takeJump = 0; - if( pOp->opcode==OP_NoConflict ){ - /* For the OP_NoConflict opcode, take the jump if any of the - ** input fields are NULL, since any key with a NULL will not - ** conflict */ - for(ii=0; ii<pIdxKey->nField; ii++){ - if( pIdxKey->aMem[ii].flags & MEM_Null ){ - takeJump = 1; - break; - } - } + sqlite3VdbeRecordUnpack(r.aMem->n, r.aMem->z, pIdxKey); + pIdxKey->default_rc = 0; + rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, pIdxKey, &pC->seekResult); + sqlite3DbFreeNN(db, pIdxKey); } - rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, pIdxKey, 0, 0, &res); - sqlite3DbFree(db, pFree); if( rc!=SQLITE_OK ){ - break; + goto abort_due_to_error; } - pC->seekResult = res; - alreadyExists = (res==0); + alreadyExists = (pC->seekResult==0); pC->nullRow = 1-alreadyExists; pC->deferredMoveto = 0; pC->cacheStatus = CACHE_STALE; @@ -3991,22 +5420,67 @@ case OP_Found: { /* jump, in3 */ VdbeBranchTaken(alreadyExists!=0,2); if( alreadyExists ) goto jump_to_p2; }else{ - VdbeBranchTaken(takeJump||alreadyExists==0,2); - if( takeJump || !alreadyExists ) goto jump_to_p2; + if( !alreadyExists ){ + VdbeBranchTaken(1,2); + goto jump_to_p2; + } + if( pOp->opcode==OP_NoConflict ){ + /* For the OP_NoConflict opcode, take the jump if any of the + ** input fields are NULL, since any key with a NULL will not + ** conflict */ + for(ii=0; ii<r.nField; ii++){ + if( r.aMem[ii].flags & MEM_Null ){ + VdbeBranchTaken(1,2); + goto jump_to_p2; + } + } + } + VdbeBranchTaken(0,2); + if( pOp->opcode==OP_IfNoHope ){ + pC->seekHit = pOp->p4.i; + } } break; } +/* Opcode: SeekRowid P1 P2 P3 * * +** Synopsis: intkey=r[P3] +** +** P1 is the index of a cursor open on an SQL table btree (with integer +** keys). If register P3 does not contain an integer or if P1 does not +** contain a record with rowid P3 then jump immediately to P2. +** Or, if P2 is 0, raise an SQLITE_CORRUPT error. If P1 does contain +** a record with rowid P3 then +** leave the cursor pointing at that record and fall through to the next +** instruction. +** +** The OP_NotExists opcode performs the same operation, but with OP_NotExists +** the P3 register must be guaranteed to contain an integer value. With this +** opcode, register P3 might not contain an integer. +** +** The OP_NotFound opcode performs the same operation on index btrees +** (with arbitrary multi-value keys). +** +** This opcode leaves the cursor in a state where it cannot be advanced +** in either direction. In other words, the Next and Prev opcodes will +** not work following this opcode. +** +** See also: Found, NotFound, NoConflict, SeekRowid +*/ /* Opcode: NotExists P1 P2 P3 * * ** Synopsis: intkey=r[P3] ** ** P1 is the index of a cursor open on an SQL table btree (with integer ** keys). P3 is an integer rowid. If P1 does not contain a record with ** rowid P3 then jump immediately to P2. Or, if P2 is 0, raise an -** SQLITE_CORRUPT error. If P1 does contain a record with rowid P3 then +** SQLITE_CORRUPT error. If P1 does contain a record with rowid P3 then ** leave the cursor pointing at that record and fall through to the next ** instruction. ** +** The OP_SeekRowid opcode performs the same operation but also allows the +** P3 register to contain a non-integer value, in which case the jump is +** always taken. This opcode requires that P3 always contain an integer. +** ** The OP_NotFound opcode performs the same operation on index btrees ** (with arbitrary multi-value keys). ** @@ -4014,29 +5488,50 @@ case OP_Found: { /* jump, in3 */ ** in either direction. In other words, the Next and Prev opcodes will ** not work following this opcode. ** -** See also: Found, NotFound, NoConflict +** See also: Found, NotFound, NoConflict, SeekRowid */ -case OP_NotExists: { /* jump, in3 */ +case OP_SeekRowid: { /* jump0, in3, ncycle */ VdbeCursor *pC; BtCursor *pCrsr; int res; u64 iKey; pIn3 = &aMem[pOp->p3]; - assert( pIn3->flags & MEM_Int ); + testcase( pIn3->flags & MEM_Int ); + testcase( pIn3->flags & MEM_IntReal ); + testcase( pIn3->flags & MEM_Real ); + testcase( (pIn3->flags & (MEM_Str|MEM_Int))==MEM_Str ); + if( (pIn3->flags & (MEM_Int|MEM_IntReal))==0 ){ + /* If pIn3->u.i does not contain an integer, compute iKey as the + ** integer value of pIn3. Jump to P2 if pIn3 cannot be converted + ** into an integer without loss of information. Take care to avoid + ** changing the datatype of pIn3, however, as it is used by other + ** parts of the prepared statement. */ + Mem x = pIn3[0]; + applyAffinity(&x, SQLITE_AFF_NUMERIC, encoding); + if( (x.flags & MEM_Int)==0 ) goto jump_to_p2; + iKey = x.u.i; + goto notExistsWithKey; + } + /* Fall through into OP_NotExists */ + /* no break */ deliberate_fall_through +case OP_NotExists: /* jump, in3, ncycle */ + pIn3 = &aMem[pOp->p3]; + assert( (pIn3->flags & MEM_Int)!=0 || pOp->opcode==OP_SeekRowid ); assert( pOp->p1>=0 && pOp->p1<p->nCursor ); + iKey = pIn3->u.i; +notExistsWithKey: pC = p->apCsr[pOp->p1]; assert( pC!=0 ); #ifdef SQLITE_DEBUG - pC->seekOp = 0; + if( pOp->opcode==OP_SeekRowid ) pC->seekOp = OP_SeekRowid; #endif assert( pC->isTable ); assert( pC->eCurType==CURTYPE_BTREE ); pCrsr = pC->uc.pCursor; assert( pCrsr!=0 ); res = 0; - iKey = pIn3->u.i; - rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res); + rc = sqlite3BtreeTableMoveto(pCrsr, iKey, 0, &res); assert( rc==SQLITE_OK || res==0 ); pC->movetoTarget = iKey; /* Used by OP_Delete */ pC->nullRow = 0; @@ -4052,6 +5547,7 @@ case OP_NotExists: { /* jump, in3 */ goto jump_to_p2; } } + if( rc ) goto abort_due_to_error; break; } @@ -4061,7 +5557,7 @@ case OP_NotExists: { /* jump, in3 */ ** Find the next available sequence number for cursor P1. ** Write the sequence number into register P2. ** The sequence number on the cursor is incremented after this -** instruction. +** instruction. */ case OP_Sequence: { /* out2 */ assert( pOp->p1>=0 && pOp->p1<p->nCursor ); @@ -4081,9 +5577,9 @@ case OP_Sequence: { /* out2 */ ** table that cursor P1 points to. The new record number is written ** written to register P2. ** -** If P3>0 then P3 is a register in the root frame of this VDBE that holds +** If P3>0 then P3 is a register in the root frame of this VDBE that holds ** the largest previously generated record number. No new record numbers are -** allowed to be less than this value. When this value reaches its maximum, +** allowed to be less than this value. When this value reaches its maximum, ** an SQLITE_FULL error is generated. The P3 register is updated with the ' ** generated record number. This P3 mechanism is used to help implement the ** AUTOINCREMENT feature. @@ -4093,8 +5589,10 @@ case OP_NewRowid: { /* out2 */ VdbeCursor *pC; /* Cursor of table to get the new rowid */ int res; /* Result of an sqlite3BtreeLast() */ int cnt; /* Counter to limit the number of searches */ +#ifndef SQLITE_OMIT_AUTOINCREMENT Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */ VdbeFrame *pFrame; /* Root frame of VDBE */ +#endif v = 0; res = 0; @@ -4102,6 +5600,7 @@ case OP_NewRowid: { /* out2 */ assert( pOp->p1>=0 && pOp->p1<p->nCursor ); pC = p->apCsr[pOp->p1]; assert( pC!=0 ); + assert( pC->isTable ); assert( pC->eCurType==CURTYPE_BTREE ); assert( pC->uc.pCursor!=0 ); { @@ -4139,8 +5638,7 @@ case OP_NewRowid: { /* out2 */ v = 1; /* IMP: R-61914-48074 */ }else{ assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) ); - rc = sqlite3BtreeKeySize(pC->uc.pCursor, &v); - assert( rc==SQLITE_OK ); /* Cannot fail following BtreeLast() */ + v = sqlite3BtreeIntegerKey(pC->uc.pCursor); if( v>=MAX_ROWID ){ pC->useRandomRowid = 1; }else{ @@ -4160,7 +5658,7 @@ case OP_NewRowid: { /* out2 */ pMem = &pFrame->aMem[pOp->p3]; }else{ /* Assert that P3 is a valid memory cell. */ - assert( pOp->p3<=(p->nMem-p->nCursor) ); + assert( pOp->p3<=(p->nMem+1 - p->nCursor) ); pMem = &aMem[pOp->p3]; memAboutToChange(p, pMem); } @@ -4170,7 +5668,7 @@ case OP_NewRowid: { /* out2 */ sqlite3VdbeMemIntegerify(pMem); assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */ if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){ - rc = SQLITE_FULL; /* IMP: R-12275-61338 */ + rc = SQLITE_FULL; /* IMP: R-17817-00630 */ goto abort_due_to_error; } if( v<pMem->u.i+1 ){ @@ -4190,11 +5688,12 @@ case OP_NewRowid: { /* out2 */ do{ sqlite3_randomness(sizeof(v), &v); v &= (MAX_ROWID>>1); v++; /* Ensure that v is greater than zero */ - }while( ((rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)v, + }while( ((rc = sqlite3BtreeTableMoveto(pC->uc.pCursor, (u64)v, 0, &res))==SQLITE_OK) && (res==0) && (++cnt<100)); - if( rc==SQLITE_OK && res==0 ){ + if( rc ) goto abort_due_to_error; + if( res==0 ){ rc = SQLITE_FULL; /* IMP: R-38219-53002 */ goto abort_due_to_error; } @@ -4221,22 +5720,19 @@ case OP_NewRowid: { /* out2 */ ** then rowid is stored for subsequent return by the ** sqlite3_last_insert_rowid() function (otherwise it is unmodified). ** -** If the OPFLAG_USESEEKRESULT flag of P5 is set and if the result of -** the last seek operation (OP_NotExists) was a success, then this -** operation will not attempt to find the appropriate row before doing -** the insert but will instead overwrite the row that the cursor is -** currently pointing to. Presumably, the prior OP_NotExists opcode -** has already positioned the cursor correctly. This is an optimization -** that boosts performance by avoiding redundant seeks. +** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might +** run faster by avoiding an unnecessary seek on cursor P1. However, +** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior +** seeks on the cursor or if the most recent seek used a key equal to P3. ** ** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an ** UPDATE operation. Otherwise (if the flag is clear) then this opcode ** is part of an INSERT operation. The difference is only important to ** the update hook. ** -** Parameter P4 may point to a string containing the table-name, or -** may be NULL. If it is not NULL, then the update-hook -** (sqlite3.xUpdateCallback) is invoked following a successful insert. +** Parameter P4 may point to a Table structure, or may be NULL. If it is +** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked +** following a successful insert. ** ** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically ** allocated, then ownership of P2 is transferred to the pseudo-cursor @@ -4247,23 +5743,14 @@ case OP_NewRowid: { /* out2 */ ** This instruction only works on tables. The equivalent instruction ** for indices is OP_IdxInsert. */ -/* Opcode: InsertInt P1 P2 P3 P4 P5 -** Synopsis: intkey=P3 data=r[P2] -** -** This works exactly like OP_Insert except that the key is the -** integer value P3, not the value of the integer stored in register P3. -*/ -case OP_Insert: -case OP_InsertInt: { +case OP_Insert: { Mem *pData; /* MEM cell holding data for the record to be inserted */ Mem *pKey; /* MEM cell holding key for the record */ - i64 iKey; /* The integer ROWID or key for the record to be inserted */ VdbeCursor *pC; /* Cursor to table into which insert is written */ - int nZero; /* Number of zero-bytes to append */ int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */ const char *zDb; /* database name - used by the update hook */ - const char *zTbl; /* Table name - used by the opdate hook */ - int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */ + Table *pTab; /* Table structure - used by update and pre-update hooks */ + BtreePayload x; /* Payload to be inserted */ pData = &aMem[pOp->p2]; assert( pOp->p1>=0 && pOp->p1<p->nCursor ); @@ -4271,55 +5758,107 @@ case OP_InsertInt: { pC = p->apCsr[pOp->p1]; assert( pC!=0 ); assert( pC->eCurType==CURTYPE_BTREE ); + assert( pC->deferredMoveto==0 ); assert( pC->uc.pCursor!=0 ); - assert( pC->isTable ); + assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable ); + assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC ); REGISTER_TRACE(pOp->p2, pData); + sqlite3VdbeIncrWriteCounter(p, pC); - if( pOp->opcode==OP_Insert ){ - pKey = &aMem[pOp->p3]; - assert( pKey->flags & MEM_Int ); - assert( memIsValid(pKey) ); - REGISTER_TRACE(pOp->p3, pKey); - iKey = pKey->u.i; + pKey = &aMem[pOp->p3]; + assert( pKey->flags & MEM_Int ); + assert( memIsValid(pKey) ); + REGISTER_TRACE(pOp->p3, pKey); + x.nKey = pKey->u.i; + + if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){ + assert( pC->iDb>=0 ); + zDb = db->aDb[pC->iDb].zDbSName; + pTab = pOp->p4.pTab; + assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) ); }else{ - assert( pOp->opcode==OP_InsertInt ); - iKey = pOp->p3; + pTab = 0; + zDb = 0; } - if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++; - if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = iKey; - if( pData->flags & MEM_Null ){ - pData->z = 0; - pData->n = 0; - }else{ - assert( pData->flags & (MEM_Blob|MEM_Str) ); +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK + /* Invoke the pre-update hook, if any */ + if( pTab ){ + if( db->xPreUpdateCallback && !(pOp->p5 & OPFLAG_ISUPDATE) ){ + sqlite3VdbePreUpdateHook(p,pC,SQLITE_INSERT,zDb,pTab,x.nKey,pOp->p2,-1); + } + if( db->xUpdateCallback==0 || pTab->aCol==0 ){ + /* Prevent post-update hook from running in cases when it should not */ + pTab = 0; + } } + if( pOp->p5 & OPFLAG_ISNOOP ) break; +#endif + + assert( (pOp->p5 & OPFLAG_LASTROWID)==0 || (pOp->p5 & OPFLAG_NCHANGE)!=0 ); + if( pOp->p5 & OPFLAG_NCHANGE ){ + p->nChange++; + if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = x.nKey; + } + assert( (pData->flags & (MEM_Blob|MEM_Str))!=0 || pData->n==0 ); + x.pData = pData->z; + x.nData = pData->n; seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0); if( pData->flags & MEM_Zero ){ - nZero = pData->u.nZero; + x.nZero = pData->u.nZero; }else{ - nZero = 0; + x.nZero = 0; } - rc = sqlite3BtreeInsert(pC->uc.pCursor, 0, iKey, - pData->z, pData->n, nZero, - (pOp->p5 & OPFLAG_APPEND)!=0, seekResult + x.pKey = 0; + assert( BTREE_PREFORMAT==OPFLAG_PREFORMAT ); + rc = sqlite3BtreeInsert(pC->uc.pCursor, &x, + (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION|OPFLAG_PREFORMAT)), + seekResult ); pC->deferredMoveto = 0; pC->cacheStatus = CACHE_STALE; + colCacheCtr++; /* Invoke the update-hook if required. */ - if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){ - zDb = db->aDb[pC->iDb].zName; - zTbl = pOp->p4.z; - op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT); - assert( pC->isTable ); - db->xUpdateCallback(db->pUpdateArg, op, zDb, zTbl, iKey); - assert( pC->iDb>=0 ); + if( rc ) goto abort_due_to_error; + if( pTab ){ + assert( db->xUpdateCallback!=0 ); + assert( pTab->aCol!=0 ); + db->xUpdateCallback(db->pUpdateArg, + (pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT, + zDb, pTab->zName, x.nKey); } break; } -/* Opcode: Delete P1 P2 * P4 P5 +/* Opcode: RowCell P1 P2 P3 * * +** +** P1 and P2 are both open cursors. Both must be opened on the same type +** of table - intkey or index. This opcode is used as part of copying +** the current row from P2 into P1. If the cursors are opened on intkey +** tables, register P3 contains the rowid to use with the new record in +** P1. If they are opened on index tables, P3 is not used. +** +** This opcode must be followed by either an Insert or InsertIdx opcode +** with the OPFLAG_PREFORMAT flag set to complete the insert operation. +*/ +case OP_RowCell: { + VdbeCursor *pDest; /* Cursor to write to */ + VdbeCursor *pSrc; /* Cursor to read from */ + i64 iKey; /* Rowid value to insert with */ + assert( pOp[1].opcode==OP_Insert || pOp[1].opcode==OP_IdxInsert ); + assert( pOp[1].opcode==OP_Insert || pOp->p3==0 ); + assert( pOp[1].opcode==OP_IdxInsert || pOp->p3>0 ); + assert( pOp[1].p5 & OPFLAG_PREFORMAT ); + pDest = p->apCsr[pOp->p1]; + pSrc = p->apCsr[pOp->p2]; + iKey = pOp->p3 ? aMem[pOp->p3].u.i : 0; + rc = sqlite3BtreeTransferRow(pDest->uc.pCursor, pSrc->uc.pCursor, iKey); + if( rc!=SQLITE_OK ) goto abort_due_to_error; + break; +}; + +/* Opcode: Delete P1 P2 P3 P4 P5 ** ** Delete the record at which the P1 cursor is currently pointing. ** @@ -4327,55 +5866,103 @@ case OP_InsertInt: { ** the cursor will be left pointing at either the next or the previous ** record in the table. If it is left pointing at the next record, then ** the next Next instruction will be a no-op. As a result, in this case -** it is ok to delete a record from within a Next loop. If +** it is ok to delete a record from within a Next loop. If ** OPFLAG_SAVEPOSITION bit of P5 is clear, then the cursor will be ** left in an undefined state. ** ** If the OPFLAG_AUXDELETE bit is set on P5, that indicates that this -** delete one of several associated with deleting a table row and all its -** associated index entries. Exactly one of those deletes is the "primary" -** delete. The others are all on OPFLAG_FORDELETE cursors or else are -** marked with the AUXDELETE flag. +** delete is one of several associated with deleting a table row and +** all its associated index entries. Exactly one of those deletes is +** the "primary" delete. The others are all on OPFLAG_FORDELETE +** cursors or else are marked with the AUXDELETE flag. ** -** If the OPFLAG_NCHANGE flag of P2 (NB: P2 not P5) is set, then the row -** change count is incremented (otherwise not). +** If the OPFLAG_NCHANGE (0x01) flag of P2 (NB: P2 not P5) is set, then +** the row change count is incremented (otherwise not). +** +** If the OPFLAG_ISNOOP (0x40) flag of P2 (not P5!) is set, then the +** pre-update-hook for deletes is run, but the btree is otherwise unchanged. +** This happens when the OP_Delete is to be shortly followed by an OP_Insert +** with the same key, causing the btree entry to be overwritten. ** ** P1 must not be pseudo-table. It has to be a real table with ** multiple rows. ** -** If P4 is not NULL, then it is the name of the table that P1 is -** pointing to. The update hook will be invoked, if it exists. -** If P4 is not NULL then the P1 cursor must have been positioned -** using OP_NotFound prior to invoking this opcode. +** If P4 is not NULL then it points to a Table object. In this case either +** the update or pre-update hook, or both, may be invoked. The P1 cursor must +** have been positioned using OP_NotFound prior to invoking this opcode in +** this case. Specifically, if one is configured, the pre-update hook is +** invoked if P4 is not NULL. The update-hook is invoked if one is configured, +** P4 is not NULL, and the OPFLAG_NCHANGE flag is set in P2. +** +** If the OPFLAG_ISUPDATE flag is set in P2, then P3 contains the address +** of the memory cell that contains the value that the rowid of the row will +** be set to by the update. */ case OP_Delete: { VdbeCursor *pC; - u8 hasUpdateCallback; + const char *zDb; + Table *pTab; + int opflags; + opflags = pOp->p2; assert( pOp->p1>=0 && pOp->p1<p->nCursor ); pC = p->apCsr[pOp->p1]; assert( pC!=0 ); assert( pC->eCurType==CURTYPE_BTREE ); assert( pC->uc.pCursor!=0 ); assert( pC->deferredMoveto==0 ); + sqlite3VdbeIncrWriteCounter(p, pC); - hasUpdateCallback = db->xUpdateCallback && pOp->p4.z && pC->isTable; - if( pOp->p5 && hasUpdateCallback ){ - sqlite3BtreeKeySize(pC->uc.pCursor, &pC->movetoTarget); +#ifdef SQLITE_DEBUG + if( pOp->p4type==P4_TABLE + && HasRowid(pOp->p4.pTab) + && pOp->p5==0 + && sqlite3BtreeCursorIsValidNN(pC->uc.pCursor) + ){ + /* If p5 is zero, the seek operation that positioned the cursor prior to + ** OP_Delete will have also set the pC->movetoTarget field to the rowid of + ** the row that is being deleted */ + i64 iKey = sqlite3BtreeIntegerKey(pC->uc.pCursor); + assert( CORRUPT_DB || pC->movetoTarget==iKey ); } +#endif -#ifdef SQLITE_DEBUG - /* The seek operation that positioned the cursor prior to OP_Delete will - ** have also set the pC->movetoTarget field to the rowid of the row that - ** is being deleted */ - if( pOp->p4.z && pC->isTable && pOp->p5==0 ){ - i64 iKey = 0; - sqlite3BtreeKeySize(pC->uc.pCursor, &iKey); - assert( pC->movetoTarget==iKey ); + /* If the update-hook or pre-update-hook will be invoked, set zDb to + ** the name of the db to pass as to it. Also set local pTab to a copy + ** of p4.pTab. Finally, if p5 is true, indicating that this cursor was + ** last moved with OP_Next or OP_Prev, not Seek or NotFound, set + ** VdbeCursor.movetoTarget to the current rowid. */ + if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){ + assert( pC->iDb>=0 ); + assert( pOp->p4.pTab!=0 ); + zDb = db->aDb[pC->iDb].zDbSName; + pTab = pOp->p4.pTab; + if( (pOp->p5 & OPFLAG_SAVEPOSITION)!=0 && pC->isTable ){ + pC->movetoTarget = sqlite3BtreeIntegerKey(pC->uc.pCursor); + } + }else{ + zDb = 0; + pTab = 0; + } + +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK + /* Invoke the pre-update-hook if required. */ + assert( db->xPreUpdateCallback==0 || pTab==pOp->p4.pTab ); + if( db->xPreUpdateCallback && pTab ){ + assert( !(opflags & OPFLAG_ISUPDATE) + || HasRowid(pTab)==0 + || (aMem[pOp->p3].flags & MEM_Int) + ); + sqlite3VdbePreUpdateHook(p, pC, + (opflags & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_DELETE, + zDb, pTab, pC->movetoTarget, + pOp->p3, -1 + ); } + if( opflags & OPFLAG_ISNOOP ) break; #endif - /* Only flags that can be set are SAVEPOISTION and AUXDELETE */ + /* Only flags that can be set are SAVEPOISTION and AUXDELETE */ assert( (pOp->p5 & ~(OPFLAG_SAVEPOSITION|OPFLAG_AUXDELETE))==0 ); assert( OPFLAG_SAVEPOSITION==BTREE_SAVEPOSITION ); assert( OPFLAG_AUXDELETE==BTREE_AUXDELETE ); @@ -4396,14 +5983,20 @@ case OP_Delete: { rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5); pC->cacheStatus = CACHE_STALE; + colCacheCtr++; + pC->seekResult = 0; + if( rc ) goto abort_due_to_error; /* Invoke the update-hook if required. */ - if( rc==SQLITE_OK && hasUpdateCallback ){ - db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, - db->aDb[pC->iDb].zName, pOp->p4.z, pC->movetoTarget); - assert( pC->iDb>=0 ); + if( opflags & OPFLAG_NCHANGE ){ + p->nChange++; + if( db->xUpdateCallback && ALWAYS(pTab!=0) && HasRowid(pTab) ){ + db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, pTab->zName, + pC->movetoTarget); + assert( pC->iDb>=0 ); + } } - if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++; + break; } /* Opcode: ResetCount * * * * * @@ -4420,10 +6013,10 @@ case OP_ResetCount: { } /* Opcode: SorterCompare P1 P2 P3 P4 -** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2 +** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2 ** ** P1 is a sorter cursor. This instruction compares a prefix of the -** record blob in register P3 against a prefix of the entry that +** record blob in register P3 against a prefix of the entry that ** the sorter cursor currently points to. Only the first P4 fields ** of r[P3] and the sorter record are compared. ** @@ -4447,6 +6040,7 @@ case OP_SorterCompare: { res = 0; rc = sqlite3VdbeSorterCompare(pC, pIn3, nKeyCol, &res); VdbeBranchTaken(res!=0,2); + if( rc ) goto abort_due_to_error; if( res ) goto jump_to_p2; break; }; @@ -4457,13 +6051,13 @@ case OP_SorterCompare: { ** Write into register P2 the current sorter data for sorter cursor P1. ** Then clear the column header cache on cursor P3. ** -** This opcode is normally use to move a record out of the sorter and into +** This opcode is normally used to move a record out of the sorter and into ** a register that is the source for a pseudo-table cursor created using ** OpenPseudo. That pseudo-table cursor is the one that is identified by ** parameter P3. Clearing the P3 column cache as part of this opcode saves ** us from having to issue a separate NullRow instruction to clear that cache. */ -case OP_SorterData: { +case OP_SorterData: { /* ncycle */ VdbeCursor *pC; pOut = &aMem[pOp->p2]; @@ -4472,102 +6066,81 @@ case OP_SorterData: { rc = sqlite3VdbeSorterRowkey(pC, pOut); assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) ); assert( pOp->p1>=0 && pOp->p1<p->nCursor ); + if( rc ) goto abort_due_to_error; p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE; break; } -/* Opcode: RowData P1 P2 * * * +/* Opcode: RowData P1 P2 P3 * * ** Synopsis: r[P2]=data ** -** Write into register P2 the complete row data for cursor P1. -** There is no interpretation of the data. -** It is just copied onto the P2 register exactly as +** Write into register P2 the complete row content for the row at +** which cursor P1 is currently pointing. +** There is no interpretation of the data. +** It is just copied onto the P2 register exactly as ** it is found in the database file. ** +** If cursor P1 is an index, then the content is the key of the row. +** If cursor P2 is a table, then the content extracted is the data. +** ** If the P1 cursor must be pointing to a valid row (not a NULL row) ** of a real table, not a pseudo-table. -*/ -/* Opcode: RowKey P1 P2 * * * -** Synopsis: r[P2]=key ** -** Write into register P2 the complete row key for cursor P1. -** There is no interpretation of the data. -** The key is copied onto the P2 register exactly as -** it is found in the database file. +** If P3!=0 then this opcode is allowed to make an ephemeral pointer +** into the database page. That means that the content of the output +** register will be invalidated as soon as the cursor moves - including +** moves caused by other cursors that "save" the current cursors +** position in order that they can write to the same table. If P3==0 +** then a copy of the data is made into memory. P3!=0 is faster, but +** P3==0 is safer. ** -** If the P1 cursor must be pointing to a valid row (not a NULL row) -** of a real table, not a pseudo-table. +** If P3!=0 then the content of the P2 register is unsuitable for use +** in OP_Result and any OP_Result will invalidate the P2 register content. +** The P2 register content is invalidated by opcodes like OP_Function or +** by any use of another cursor pointing to the same table. */ -case OP_RowKey: case OP_RowData: { VdbeCursor *pC; BtCursor *pCrsr; u32 n; - i64 n64; - pOut = &aMem[pOp->p2]; - memAboutToChange(p, pOut); + pOut = out2Prerelease(p, pOp); - /* Note that RowKey and RowData are really exactly the same instruction */ assert( pOp->p1>=0 && pOp->p1<p->nCursor ); pC = p->apCsr[pOp->p1]; assert( pC!=0 ); assert( pC->eCurType==CURTYPE_BTREE ); assert( isSorter(pC)==0 ); - assert( pC->isTable || pOp->opcode!=OP_RowData ); - assert( pC->isTable==0 || pOp->opcode==OP_RowData ); assert( pC->nullRow==0 ); assert( pC->uc.pCursor!=0 ); pCrsr = pC->uc.pCursor; - /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or - ** OP_Rewind/Op_Next with no intervening instructions that might invalidate - ** the cursor. If this where not the case, on of the following assert()s + /* The OP_RowData opcodes always follow OP_NotExists or + ** OP_SeekRowid or OP_Rewind/Op_Next with no intervening instructions + ** that might invalidate the cursor. + ** If this were not the case, one of the following assert()s ** would fail. Should this ever change (because of changes in the code ** generator) then the fix would be to insert a call to ** sqlite3VdbeCursorMoveto(). */ assert( pC->deferredMoveto==0 ); assert( sqlite3BtreeCursorIsValid(pCrsr) ); -#if 0 /* Not required due to the previous to assert() statements */ - rc = sqlite3VdbeCursorMoveto(pC); - if( rc!=SQLITE_OK ) goto abort_due_to_error; -#endif - if( pC->isTable==0 ){ - assert( !pC->isTable ); - VVA_ONLY(rc =) sqlite3BtreeKeySize(pCrsr, &n64); - assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */ - if( n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){ - goto too_big; - } - n = (u32)n64; - }else{ - VVA_ONLY(rc =) sqlite3BtreeDataSize(pCrsr, &n); - assert( rc==SQLITE_OK ); /* DataSize() cannot fail */ - if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){ - goto too_big; - } + n = sqlite3BtreePayloadSize(pCrsr); + if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){ + goto too_big; } testcase( n==0 ); - if( sqlite3VdbeMemClearAndResize(pOut, MAX(n,32)) ){ - goto no_mem; - } - pOut->n = n; - MemSetTypeFlag(pOut, MEM_Blob); - if( pC->isTable==0 ){ - rc = sqlite3BtreeKey(pCrsr, 0, n, pOut->z); - }else{ - rc = sqlite3BtreeData(pCrsr, 0, n, pOut->z); - } - pOut->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */ + rc = sqlite3VdbeMemFromBtreeZeroOffset(pCrsr, n, pOut); + if( rc ) goto abort_due_to_error; + if( !pOp->p3 ) Deephemeralize(pOut); UPDATE_MAX_BLOBSIZE(pOut); REGISTER_TRACE(pOp->p2, pOut); break; } /* Opcode: Rowid P1 P2 * * * -** Synopsis: r[P2]=rowid +** Synopsis: r[P2]=PX rowid of P1 ** ** Store in register P2 an integer which is the key of the table entry that ** P1 is currently point to. @@ -4576,7 +6149,7 @@ case OP_RowData: { ** be a separate OP_VRowid opcode for use with virtual tables, but this ** one opcode now works for both table types. */ -case OP_Rowid: { /* out2 */ +case OP_Rowid: { /* out2, ncycle */ VdbeCursor *pC; i64 v; sqlite3_vtab *pVtab; @@ -4600,6 +6173,7 @@ case OP_Rowid: { /* out2 */ assert( pModule->xRowid ); rc = pModule->xRowid(pC->uc.pVCur, &v); sqlite3VtabImportErrmsg(p, pVtab); + if( rc ) goto abort_due_to_error; #endif /* SQLITE_OMIT_VIRTUALTABLE */ }else{ assert( pC->eCurType==CURTYPE_BTREE ); @@ -4610,8 +6184,7 @@ case OP_Rowid: { /* out2 */ pOut->flags = MEM_Null; break; } - rc = sqlite3BtreeKeySize(pC->uc.pCursor, &v); - assert( rc==SQLITE_OK ); /* Always so because of CursorRestore() above */ + v = sqlite3BtreeIntegerKey(pC->uc.pCursor); } pOut->u.i = v; break; @@ -4622,25 +6195,50 @@ case OP_Rowid: { /* out2 */ ** Move the cursor P1 to a null row. Any OP_Column operations ** that occur while the cursor is on the null row will always ** write a NULL. +** +** If cursor P1 is not previously opened, open it now to a special +** pseudo-cursor that always returns NULL for every column. */ case OP_NullRow: { VdbeCursor *pC; assert( pOp->p1>=0 && pOp->p1<p->nCursor ); pC = p->apCsr[pOp->p1]; - assert( pC!=0 ); + if( pC==0 ){ + /* If the cursor is not already open, create a special kind of + ** pseudo-cursor that always gives null rows. */ + pC = allocateCursor(p, pOp->p1, 1, CURTYPE_PSEUDO); + if( pC==0 ) goto no_mem; + pC->seekResult = 0; + pC->isTable = 1; + pC->noReuse = 1; + pC->uc.pCursor = sqlite3BtreeFakeValidCursor(); + } pC->nullRow = 1; pC->cacheStatus = CACHE_STALE; if( pC->eCurType==CURTYPE_BTREE ){ assert( pC->uc.pCursor!=0 ); sqlite3BtreeClearCursor(pC->uc.pCursor); } +#ifdef SQLITE_DEBUG + if( pC->seekOp==0 ) pC->seekOp = OP_NullRow; +#endif break; } -/* Opcode: Last P1 P2 P3 * * +/* Opcode: SeekEnd P1 * * * * +** +** Position cursor P1 at the end of the btree for the purpose of +** appending a new entry onto the btree. +** +** It is assumed that the cursor is used only for appending and so +** if the cursor is valid, then the cursor must already be pointing +** at the end of the btree and so no changes are made to +** the cursor. +*/ +/* Opcode: Last P1 P2 * * * ** -** The next use of the Rowid or Column or Prev instruction for P1 +** The next use of the Rowid or Column or Prev instruction for P1 ** will refer to the last entry in the database table or index. ** If the table or index is empty and P2>0, then jump immediately to P2. ** If P2 is 0 or if the table or index is not empty, fall through @@ -4650,7 +6248,8 @@ case OP_NullRow: { ** from the end toward the beginning. In other words, the cursor is ** configured to use Prev, not Next. */ -case OP_Last: { /* jump */ +case OP_SeekEnd: /* ncycle */ +case OP_Last: { /* jump0, ncycle */ VdbeCursor *pC; BtCursor *pCrsr; int res; @@ -4662,14 +6261,21 @@ case OP_Last: { /* jump */ pCrsr = pC->uc.pCursor; res = 0; assert( pCrsr!=0 ); +#ifdef SQLITE_DEBUG + pC->seekOp = pOp->opcode; +#endif + if( pOp->opcode==OP_SeekEnd ){ + assert( pOp->p2==0 ); + pC->seekResult = -1; + if( sqlite3BtreeCursorIsValidNN(pCrsr) ){ + break; + } + } rc = sqlite3BtreeLast(pCrsr, &res); pC->nullRow = (u8)res; pC->deferredMoveto = 0; pC->cacheStatus = CACHE_STALE; - pC->seekResult = pOp->p3; -#ifdef SQLITE_DEBUG - pC->seekOp = OP_Last; -#endif + if( rc ) goto abort_due_to_error; if( pOp->p2>0 ){ VdbeBranchTaken(res!=0,2); if( res ) goto jump_to_p2; @@ -4677,7 +6283,53 @@ case OP_Last: { /* jump */ break; } +/* Opcode: IfSizeBetween P1 P2 P3 P4 * +** +** Let N be the approximate number of rows in the table or index +** with cursor P1 and let X be 10*log2(N) if N is positive or -1 +** if N is zero. +** +** Jump to P2 if X is in between P3 and P4, inclusive. +*/ +case OP_IfSizeBetween: { /* jump */ + VdbeCursor *pC; + BtCursor *pCrsr; + int res; + i64 sz; + + assert( pOp->p1>=0 && pOp->p1<p->nCursor ); + assert( pOp->p4type==P4_INT32 ); + assert( pOp->p3>=-1 && pOp->p3<=640*2 ); + assert( pOp->p4.i>=-1 && pOp->p4.i<=640*2 ); + pC = p->apCsr[pOp->p1]; + assert( pC!=0 ); + pCrsr = pC->uc.pCursor; + assert( pCrsr ); + rc = sqlite3BtreeFirst(pCrsr, &res); + if( rc ) goto abort_due_to_error; + if( res!=0 ){ + sz = -1; /* -Infinity encoding */ + }else{ + sz = sqlite3BtreeRowCountEst(pCrsr); + assert( sz>0 ); + sz = sqlite3LogEst((u64)sz); + } + res = sz>=pOp->p3 && sz<=pOp->p4.i; + VdbeBranchTaken(res!=0,2); + if( res ) goto jump_to_p2; + break; +} + +/* Opcode: SorterSort P1 P2 * * * +** +** After all records have been inserted into the Sorter object +** identified by P1, invoke this opcode to actually do the sorting. +** Jump to P2 if there are no records to be sorted. +** +** This opcode is an alias for OP_Sort and OP_Rewind that is used +** for Sorter objects. +*/ /* Opcode: Sort P1 P2 * * * ** ** This opcode does exactly the same thing as OP_Rewind except that @@ -4690,33 +6342,40 @@ case OP_Last: { /* jump */ ** regression tests can determine whether or not the optimizer is ** correctly optimizing out sorts. */ -case OP_SorterSort: /* jump */ -case OP_Sort: { /* jump */ +case OP_SorterSort: /* jump ncycle */ +case OP_Sort: { /* jump ncycle */ #ifdef SQLITE_TEST sqlite3_sort_count++; sqlite3_search_count--; #endif p->aCounter[SQLITE_STMTSTATUS_SORT]++; /* Fall through into OP_Rewind */ + /* no break */ deliberate_fall_through } /* Opcode: Rewind P1 P2 * * * ** -** The next use of the Rowid or Column or Next instruction for P1 +** The next use of the Rowid or Column or Next instruction for P1 ** will refer to the first entry in the database table or index. ** If the table or index is empty, jump immediately to P2. -** If the table or index is not empty, fall through to the following +** If the table or index is not empty, fall through to the following ** instruction. ** +** If P2 is zero, that is an assertion that the P1 table is never +** empty and hence the jump will never be taken. +** ** This opcode leaves the cursor configured to move in forward order, ** from the beginning toward the end. In other words, the cursor is ** configured to use Next, not Prev. */ -case OP_Rewind: { /* jump */ +case OP_Rewind: { /* jump0, ncycle */ VdbeCursor *pC; BtCursor *pCrsr; int res; assert( pOp->p1>=0 && pOp->p1<p->nCursor ); + assert( pOp->p5==0 ); + assert( pOp->p2>=0 && pOp->p2<p->nOp ); + pC = p->apCsr[pOp->p1]; assert( pC!=0 ); assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) ); @@ -4734,14 +6393,42 @@ case OP_Rewind: { /* jump */ pC->deferredMoveto = 0; pC->cacheStatus = CACHE_STALE; } + if( rc ) goto abort_due_to_error; pC->nullRow = (u8)res; - assert( pOp->p2>0 && pOp->p2<p->nOp ); + if( pOp->p2>0 ){ + VdbeBranchTaken(res!=0,2); + if( res ) goto jump_to_p2; + } + break; +} + +/* Opcode: IfEmpty P1 P2 * * * +** Synopsis: if( empty(P1) ) goto P2 +** +** Check to see if the b-tree table that cursor P1 references is empty +** and jump to P2 if it is. +*/ +case OP_IfEmpty: { /* jump */ + VdbeCursor *pC; + BtCursor *pCrsr; + int res; + + assert( pOp->p1>=0 && pOp->p1<p->nCursor ); + assert( pOp->p2>=0 && pOp->p2<p->nOp ); + + pC = p->apCsr[pOp->p1]; + assert( pC!=0 ); + assert( pC->eCurType==CURTYPE_BTREE ); + pCrsr = pC->uc.pCursor; + assert( pCrsr ); + rc = sqlite3BtreeIsEmpty(pCrsr, &res); + if( rc ) goto abort_due_to_error; VdbeBranchTaken(res!=0,2); if( res ) goto jump_to_p2; break; } -/* Opcode: Next P1 P2 P3 P4 P5 +/* Opcode: Next P1 P2 P3 * P5 ** ** Advance cursor P1 so that it points to the next key/data pair in its ** table or index. If there are no more key/value pairs then fall through @@ -4760,20 +6447,12 @@ case OP_Rewind: { /* jump */ ** omitted if that index had been unique. P3 is usually 0. P3 is ** always either 0 or 1. ** -** P4 is always of type P4_ADVANCE. The function pointer points to -** sqlite3BtreeNext(). -** ** If P5 is positive and the jump is taken, then event counter ** number P5-1 in the prepared statement is incremented. ** -** See also: Prev, NextIfOpen -*/ -/* Opcode: NextIfOpen P1 P2 P3 P4 P5 -** -** This opcode works just like Next except that if cursor P1 is not -** open it behaves a no-op. +** See also: Prev */ -/* Opcode: Prev P1 P2 P3 P4 P5 +/* Opcode: Prev P1 P2 P3 * P5 ** ** Back up cursor P1 so that it points to the previous key/data pair in its ** table or index. If there is no previous key/value pairs then fall through @@ -4793,131 +6472,168 @@ case OP_Rewind: { /* jump */ ** omitted if that index had been unique. P3 is usually 0. P3 is ** always either 0 or 1. ** -** P4 is always of type P4_ADVANCE. The function pointer points to -** sqlite3BtreePrevious(). -** ** If P5 is positive and the jump is taken, then event counter ** number P5-1 in the prepared statement is incremented. */ -/* Opcode: PrevIfOpen P1 P2 P3 P4 P5 +/* Opcode: SorterNext P1 P2 * * P5 ** -** This opcode works just like Prev except that if cursor P1 is not -** open it behaves a no-op. +** This opcode works just like OP_Next except that P1 must be a +** sorter object for which the OP_SorterSort opcode has been +** invoked. This opcode advances the cursor to the next sorted +** record, or jumps to P2 if there are no more sorted records. */ case OP_SorterNext: { /* jump */ VdbeCursor *pC; - int res; pC = p->apCsr[pOp->p1]; assert( isSorter(pC) ); - res = 0; - rc = sqlite3VdbeSorterNext(db, pC, &res); + rc = sqlite3VdbeSorterNext(db, pC); + goto next_tail; + +case OP_Prev: /* jump, ncycle */ + assert( pOp->p1>=0 && pOp->p1<p->nCursor ); + assert( pOp->p5==0 + || pOp->p5==SQLITE_STMTSTATUS_FULLSCAN_STEP + || pOp->p5==SQLITE_STMTSTATUS_AUTOINDEX); + pC = p->apCsr[pOp->p1]; + assert( pC!=0 ); + assert( pC->deferredMoveto==0 ); + assert( pC->eCurType==CURTYPE_BTREE ); + assert( pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE + || pC->seekOp==OP_Last || pC->seekOp==OP_IfNoHope + || pC->seekOp==OP_NullRow); + rc = sqlite3BtreePrevious(pC->uc.pCursor, pOp->p3); goto next_tail; -case OP_PrevIfOpen: /* jump */ -case OP_NextIfOpen: /* jump */ - if( p->apCsr[pOp->p1]==0 ) break; - /* Fall through */ -case OP_Prev: /* jump */ -case OP_Next: /* jump */ + +case OP_Next: /* jump, ncycle */ assert( pOp->p1>=0 && pOp->p1<p->nCursor ); - assert( pOp->p5<ArraySize(p->aCounter) ); + assert( pOp->p5==0 + || pOp->p5==SQLITE_STMTSTATUS_FULLSCAN_STEP + || pOp->p5==SQLITE_STMTSTATUS_AUTOINDEX); pC = p->apCsr[pOp->p1]; - res = pOp->p3; assert( pC!=0 ); assert( pC->deferredMoveto==0 ); assert( pC->eCurType==CURTYPE_BTREE ); - assert( res==0 || (res==1 && pC->isTable==0) ); - testcase( res==1 ); - assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext ); - assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious ); - assert( pOp->opcode!=OP_NextIfOpen || pOp->p4.xAdvance==sqlite3BtreeNext ); - assert( pOp->opcode!=OP_PrevIfOpen || pOp->p4.xAdvance==sqlite3BtreePrevious); - - /* The Next opcode is only used after SeekGT, SeekGE, and Rewind. - ** The Prev opcode is only used after SeekLT, SeekLE, and Last. */ - assert( pOp->opcode!=OP_Next || pOp->opcode!=OP_NextIfOpen - || pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE - || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found); - assert( pOp->opcode!=OP_Prev || pOp->opcode!=OP_PrevIfOpen - || pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE - || pC->seekOp==OP_Last ); - - rc = pOp->p4.xAdvance(pC->uc.pCursor, &res); + assert( pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE + || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found + || pC->seekOp==OP_NullRow|| pC->seekOp==OP_SeekRowid + || pC->seekOp==OP_IfNoHope); + rc = sqlite3BtreeNext(pC->uc.pCursor, pOp->p3); + next_tail: pC->cacheStatus = CACHE_STALE; - VdbeBranchTaken(res==0,2); - if( res==0 ){ + VdbeBranchTaken(rc==SQLITE_OK,2); + if( rc==SQLITE_OK ){ pC->nullRow = 0; p->aCounter[pOp->p5]++; #ifdef SQLITE_TEST sqlite3_search_count++; #endif goto jump_to_p2_and_check_for_interrupt; - }else{ - pC->nullRow = 1; } + if( rc!=SQLITE_DONE ) goto abort_due_to_error; + rc = SQLITE_OK; + pC->nullRow = 1; goto check_for_interrupt; } -/* Opcode: IdxInsert P1 P2 P3 * P5 +/* Opcode: IdxInsert P1 P2 P3 P4 P5 ** Synopsis: key=r[P2] ** ** Register P2 holds an SQL index key made using the ** MakeRecord instructions. This opcode writes that key ** into the index P1. Data for the entry is nil. ** -** P3 is a flag that provides a hint to the b-tree layer that this -** insert is likely to be an append. +** If P4 is not zero, then it is the number of values in the unpacked +** key of reg(P2). In that case, P3 is the index of the first register +** for the unpacked key. The availability of the unpacked key can sometimes +** be an optimization. +** +** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer +** that this insert is likely to be an append. ** ** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is ** incremented by this instruction. If the OPFLAG_NCHANGE bit is clear, ** then the change counter is unchanged. ** -** If P5 has the OPFLAG_USESEEKRESULT bit set, then the cursor must have -** just done a seek to the spot where the new entry is to be inserted. -** This flag avoids doing an extra seek. +** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might +** run faster by avoiding an unnecessary seek on cursor P1. However, +** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior +** seeks on the cursor or if the most recent seek used a key equivalent +** to P2. ** ** This instruction only works for indices. The equivalent instruction ** for tables is OP_Insert. */ -case OP_SorterInsert: /* in2 */ case OP_IdxInsert: { /* in2 */ VdbeCursor *pC; - int nKey; - const char *zKey; + BtreePayload x; assert( pOp->p1>=0 && pOp->p1<p->nCursor ); pC = p->apCsr[pOp->p1]; + sqlite3VdbeIncrWriteCounter(p, pC); assert( pC!=0 ); - assert( isSorter(pC)==(pOp->opcode==OP_SorterInsert) ); + assert( !isSorter(pC) ); pIn2 = &aMem[pOp->p2]; - assert( pIn2->flags & MEM_Blob ); + assert( (pIn2->flags & MEM_Blob) || (pOp->p5 & OPFLAG_PREFORMAT) ); if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++; - assert( pC->eCurType==CURTYPE_BTREE || pOp->opcode==OP_SorterInsert ); + assert( pC->eCurType==CURTYPE_BTREE ); assert( pC->isTable==0 ); rc = ExpandBlob(pIn2); - if( rc==SQLITE_OK ){ - if( pOp->opcode==OP_SorterInsert ){ - rc = sqlite3VdbeSorterWrite(pC, pIn2); - }else{ - nKey = pIn2->n; - zKey = pIn2->z; - rc = sqlite3BtreeInsert(pC->uc.pCursor, zKey, nKey, "", 0, 0, pOp->p3, - ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0) - ); - assert( pC->deferredMoveto==0 ); - pC->cacheStatus = CACHE_STALE; - } - } + if( rc ) goto abort_due_to_error; + x.nKey = pIn2->n; + x.pKey = pIn2->z; + x.aMem = aMem + pOp->p3; + x.nMem = (u16)pOp->p4.i; + rc = sqlite3BtreeInsert(pC->uc.pCursor, &x, + (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION|OPFLAG_PREFORMAT)), + ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0) + ); + assert( pC->deferredMoveto==0 ); + pC->cacheStatus = CACHE_STALE; + if( rc) goto abort_due_to_error; + break; +} + +/* Opcode: SorterInsert P1 P2 * * * +** Synopsis: key=r[P2] +** +** Register P2 holds an SQL index key made using the +** MakeRecord instructions. This opcode writes that key +** into the sorter P1. Data for the entry is nil. +*/ +case OP_SorterInsert: { /* in2 */ + VdbeCursor *pC; + + assert( pOp->p1>=0 && pOp->p1<p->nCursor ); + pC = p->apCsr[pOp->p1]; + sqlite3VdbeIncrWriteCounter(p, pC); + assert( pC!=0 ); + assert( isSorter(pC) ); + pIn2 = &aMem[pOp->p2]; + assert( pIn2->flags & MEM_Blob ); + assert( pC->isTable==0 ); + rc = ExpandBlob(pIn2); + if( rc ) goto abort_due_to_error; + rc = sqlite3VdbeSorterWrite(pC, pIn2); + if( rc) goto abort_due_to_error; break; } -/* Opcode: IdxDelete P1 P2 P3 * * +/* Opcode: IdxDelete P1 P2 P3 * P5 ** Synopsis: key=r[P2@P3] ** ** The content of P3 registers starting at register P2 form -** an unpacked index key. This opcode removes that entry from the +** an unpacked index key. This opcode removes that entry from the ** index opened by cursor P1. +** +** If P5 is not zero, then raise an SQLITE_CORRUPT_INDEX error +** if no matching index entry is found. This happens when running +** an UPDATE or DELETE statement and the index entry to be updated +** or deleted is not found. For some uses of IdxDelete +** (example: the EXCEPT operator) it does not matter that no matching +** entry is found. For those cases, P5 is zero. Also, do not raise +** this (self-correcting and non-critical) error if in writable_schema mode. */ case OP_IdxDelete: { VdbeCursor *pC; @@ -4926,29 +6642,35 @@ case OP_IdxDelete: { UnpackedRecord r; assert( pOp->p3>0 ); - assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem-p->nCursor)+1 ); + assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem+1 - p->nCursor)+1 ); assert( pOp->p1>=0 && pOp->p1<p->nCursor ); pC = p->apCsr[pOp->p1]; assert( pC!=0 ); assert( pC->eCurType==CURTYPE_BTREE ); + sqlite3VdbeIncrWriteCounter(p, pC); pCrsr = pC->uc.pCursor; assert( pCrsr!=0 ); - assert( pOp->p5==0 ); r.pKeyInfo = pC->pKeyInfo; r.nField = (u16)pOp->p3; r.default_rc = 0; r.aMem = &aMem[pOp->p2]; - rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res); - if( rc==SQLITE_OK && res==0 ){ + rc = sqlite3BtreeIndexMoveto(pCrsr, &r, &res); + if( rc ) goto abort_due_to_error; + if( res==0 ){ rc = sqlite3BtreeDelete(pCrsr, BTREE_AUXDELETE); + if( rc ) goto abort_due_to_error; + }else if( pOp->p5 && !sqlite3WritableSchema(db) ){ + rc = sqlite3ReportError(SQLITE_CORRUPT_INDEX, __LINE__, "index corruption"); + goto abort_due_to_error; } assert( pC->deferredMoveto==0 ); pC->cacheStatus = CACHE_STALE; + pC->seekResult = 0; break; } -/* Opcode: Seek P1 * P3 P4 * -** Synopsis: Move P3 to P1.rowid +/* Opcode: DeferredSeek P1 * P3 P4 * +** Synopsis: Move P3 to P1.rowid if needed ** ** P1 is an open index cursor and P3 is a cursor on the corresponding ** table. This opcode does a deferred seek of the P3 table cursor @@ -4960,8 +6682,8 @@ case OP_IdxDelete: { ** ** P4 may be an array of integers (type P4_INTARRAY) containing ** one entry for each column in the P3 table. If array entry a(i) -** is non-zero, then reading column a(i)-1 from cursor P3 is -** equivalent to performing the deferred seek and then reading column i +** is non-zero, then reading column a(i)-1 from cursor P3 is +** equivalent to performing the deferred seek and then reading column i ** from P1. This information is stored in P3 and used to redirect ** reads against P3 over to P1, thus possibly avoiding the need to ** seek and read cursor P3. @@ -4975,18 +6697,18 @@ case OP_IdxDelete: { ** ** See also: Rowid, MakeRecord. */ -case OP_Seek: -case OP_IdxRowid: { /* out2 */ - VdbeCursor *pC; /* The P1 index cursor */ - VdbeCursor *pTabCur; /* The P2 table cursor (OP_Seek only) */ - i64 rowid; /* Rowid that P1 current points to */ +case OP_DeferredSeek: /* ncycle */ +case OP_IdxRowid: { /* out2, ncycle */ + VdbeCursor *pC; /* The P1 index cursor */ + VdbeCursor *pTabCur; /* The P2 table cursor (OP_DeferredSeek only) */ + i64 rowid; /* Rowid that P1 current points to */ assert( pOp->p1>=0 && pOp->p1<p->nCursor ); pC = p->apCsr[pOp->p1]; assert( pC!=0 ); - assert( pC->eCurType==CURTYPE_BTREE ); + assert( pC->eCurType==CURTYPE_BTREE || IsNullCursor(pC) ); assert( pC->uc.pCursor!=0 ); - assert( pC->isTable==0 ); + assert( pC->isTable==0 || IsNullCursor(pC) ); assert( pC->deferredMoveto==0 ); assert( !pC->nullRow || pOp->opcode==OP_IdxRowid ); @@ -4994,10 +6716,10 @@ case OP_IdxRowid: { /* out2 */ ** of sqlite3VdbeCursorRestore() and sqlite3VdbeIdxRowid(). */ rc = sqlite3VdbeCursorRestore(pC); - /* sqlite3VbeCursorRestore() can only fail if the record has been deleted - ** out from under the cursor. That will never happens for an IdxRowid - ** or Seek opcode */ - if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error; + /* sqlite3VdbeCursorRestore() may fail if the cursor has been disturbed + ** since it was last positioned and an error (e.g. OOM or an IO error) + ** occurs while trying to reposition it. */ + if( rc!=SQLITE_OK ) goto abort_due_to_error; if( !pC->nullRow ){ rowid = 0; /* Not needed. Only used to silence a warning. */ @@ -5005,7 +6727,7 @@ case OP_IdxRowid: { /* out2 */ if( rc!=SQLITE_OK ){ goto abort_due_to_error; } - if( pOp->opcode==OP_Seek ){ + if( pOp->opcode==OP_DeferredSeek ){ assert( pOp->p3>=0 && pOp->p3<p->nCursor ); pTabCur = p->apCsr[pOp->p3]; assert( pTabCur!=0 ); @@ -5015,13 +6737,15 @@ case OP_IdxRowid: { /* out2 */ pTabCur->nullRow = 0; pTabCur->movetoTarget = rowid; pTabCur->deferredMoveto = 1; + pTabCur->cacheStatus = CACHE_STALE; assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 ); - pTabCur->aAltMap = pOp->p4.ai; + assert( !pTabCur->isEphemeral ); + pTabCur->ub.aAltMap = pOp->p4.ai; + assert( !pC->isEphemeral ); pTabCur->pAltCursor = pC; }else{ pOut = out2Prerelease(p, pOp); pOut->u.i = rowid; - pOut->flags = MEM_Int; } }else{ assert( pOp->opcode==OP_IdxRowid ); @@ -5030,32 +6754,50 @@ case OP_IdxRowid: { /* out2 */ break; } -/* Opcode: IdxGE P1 P2 P3 P4 P5 +/* Opcode: FinishSeek P1 * * * * +** +** If cursor P1 was previously moved via OP_DeferredSeek, complete that +** seek operation now, without further delay. If the cursor seek has +** already occurred, this instruction is a no-op. +*/ +case OP_FinishSeek: { /* ncycle */ + VdbeCursor *pC; /* The P1 index cursor */ + + assert( pOp->p1>=0 && pOp->p1<p->nCursor ); + pC = p->apCsr[pOp->p1]; + if( pC->deferredMoveto ){ + rc = sqlite3VdbeFinishMoveto(pC); + if( rc ) goto abort_due_to_error; + } + break; +} + +/* Opcode: IdxGE P1 P2 P3 P4 * ** Synopsis: key=r[P3@P4] ** -** The P4 register values beginning with P3 form an unpacked index -** key that omits the PRIMARY KEY. Compare this key value against the index -** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID +** The P4 register values beginning with P3 form an unpacked index +** key that omits the PRIMARY KEY. Compare this key value against the index +** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID ** fields at the end. ** ** If the P1 index entry is greater than or equal to the key value ** then jump to P2. Otherwise fall through to the next instruction. */ -/* Opcode: IdxGT P1 P2 P3 P4 P5 +/* Opcode: IdxGT P1 P2 P3 P4 * ** Synopsis: key=r[P3@P4] ** -** The P4 register values beginning with P3 form an unpacked index -** key that omits the PRIMARY KEY. Compare this key value against the index -** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID +** The P4 register values beginning with P3 form an unpacked index +** key that omits the PRIMARY KEY. Compare this key value against the index +** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID ** fields at the end. ** ** If the P1 index entry is greater than the key value ** then jump to P2. Otherwise fall through to the next instruction. */ -/* Opcode: IdxLT P1 P2 P3 P4 P5 +/* Opcode: IdxLT P1 P2 P3 P4 * ** Synopsis: key=r[P3@P4] ** -** The P4 register values beginning with P3 form an unpacked index +** The P4 register values beginning with P3 form an unpacked index ** key that omits the PRIMARY KEY or ROWID. Compare this key value against ** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or ** ROWID on the P1 index. @@ -5063,10 +6805,10 @@ case OP_IdxRowid: { /* out2 */ ** If the P1 index entry is less than the key value then jump to P2. ** Otherwise fall through to the next instruction. */ -/* Opcode: IdxLE P1 P2 P3 P4 P5 +/* Opcode: IdxLE P1 P2 P3 P4 * ** Synopsis: key=r[P3@P4] ** -** The P4 register values beginning with P3 form an unpacked index +** The P4 register values beginning with P3 form an unpacked index ** key that omits the PRIMARY KEY or ROWID. Compare this key value against ** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or ** ROWID on the P1 index. @@ -5074,10 +6816,10 @@ case OP_IdxRowid: { /* out2 */ ** If the P1 index entry is less than or equal to the key value then jump ** to P2. Otherwise fall through to the next instruction. */ -case OP_IdxLE: /* jump */ -case OP_IdxGT: /* jump */ -case OP_IdxLT: /* jump */ -case OP_IdxGE: { /* jump */ +case OP_IdxLE: /* jump, ncycle */ +case OP_IdxGT: /* jump, ncycle */ +case OP_IdxLT: /* jump, ncycle */ +case OP_IdxGE: { /* jump, ncycle */ VdbeCursor *pC; int res; UnpackedRecord r; @@ -5089,7 +6831,6 @@ case OP_IdxGE: { /* jump */ assert( pC->eCurType==CURTYPE_BTREE ); assert( pC->uc.pCursor!=0); assert( pC->deferredMoveto==0 ); - assert( pOp->p5==0 || pOp->p5==1 ); assert( pOp->p4type==P4_INT32 ); r.pKeyInfo = pC->pKeyInfo; r.nField = (u16)pOp->p4.i; @@ -5102,10 +6843,39 @@ case OP_IdxGE: { /* jump */ } r.aMem = &aMem[pOp->p3]; #ifdef SQLITE_DEBUG - { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); } + { + int i; + for(i=0; i<r.nField; i++){ + assert( memIsValid(&r.aMem[i]) ); + REGISTER_TRACE(pOp->p3+i, &aMem[pOp->p3+i]); + } + } #endif - res = 0; /* Not needed. Only used to silence a warning. */ - rc = sqlite3VdbeIdxKeyCompare(db, pC, &r, &res); + + /* Inlined version of sqlite3VdbeIdxKeyCompare() */ + { + i64 nCellKey = 0; + BtCursor *pCur; + Mem m; + + assert( pC->eCurType==CURTYPE_BTREE ); + pCur = pC->uc.pCursor; + assert( sqlite3BtreeCursorIsValid(pCur) ); + nCellKey = sqlite3BtreePayloadSize(pCur); + /* nCellKey will always be between 0 and 0xffffffff because of the way + ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */ + if( nCellKey<=0 || nCellKey>0x7fffffff ){ + rc = SQLITE_CORRUPT_BKPT; + goto abort_due_to_error; + } + sqlite3VdbeMemInit(&m, db, 0); + rc = sqlite3VdbeMemFromBtreeZeroOffset(pCur, (u32)nCellKey, &m); + if( rc ) goto abort_due_to_error; + res = sqlite3VdbeRecordCompareWithSkip(m.n, m.z, &r, 0); + sqlite3VdbeMemReleaseMalloc(&m); + } + /* End of inlined sqlite3VdbeIdxKeyCompare() */ + assert( (OP_IdxLE&1)==(OP_IdxLT&1) && (OP_IdxGE&1)==(OP_IdxGT&1) ); if( (pOp->opcode&1)==(OP_IdxLT&1) ){ assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT ); @@ -5115,6 +6885,7 @@ case OP_IdxGE: { /* jump */ res++; } VdbeBranchTaken(res>0,2); + assert( rc==SQLITE_OK ); if( res>0 ) goto jump_to_p2; break; } @@ -5125,17 +6896,24 @@ case OP_IdxGE: { /* jump */ ** file is given by P1. ** ** The table being destroyed is in the main database file if P3==0. If -** P3==1 then the table to be clear is in the auxiliary database file +** P3==1 then the table to be destroyed is in the auxiliary database file ** that is used to store tables create using CREATE TEMPORARY TABLE. ** ** If AUTOVACUUM is enabled then it is possible that another root page ** might be moved into the newly deleted root page in order to keep all ** root pages contiguous at the beginning of the database. The former ** value of the root page that moved - its value before the move occurred - -** is stored in register P2. If no page -** movement was required (because the table being dropped was already -** the last one in the database) then a zero is stored in register P2. -** If AUTOVACUUM is disabled then a zero is stored in register P2. +** is stored in register P2. If no page movement was required (because the +** table being dropped was already the last one in the database) then a +** zero is stored in register P2. If AUTOVACUUM is disabled then a zero +** is stored in register P2. +** +** This opcode throws an error if there are any active reader VMs when +** it is invoked. This is done to avoid the difficulty associated with +** updating existing cursors when a root page is moved in an AUTOVACUUM +** database. This error is thrown even if the database is not an AUTOVACUUM +** db in order to avoid introducing an incompatibility between autovacuum +** and non-autovacuum modes. ** ** See also: Clear */ @@ -5143,6 +6921,7 @@ case OP_Destroy: { /* out2 */ int iMoved; int iDb; + sqlite3VdbeIncrWriteCounter(p, 0); assert( p->readOnly==0 ); assert( pOp->p1>1 ); pOut = out2Prerelease(p, pOp); @@ -5150,6 +6929,7 @@ case OP_Destroy: { /* out2 */ if( db->nVdbeRead > db->nVDestroy+1 ){ rc = SQLITE_LOCKED; p->errorAction = OE_Abort; + goto abort_due_to_error; }else{ iDb = pOp->p3; assert( DbMaskTest(p->btreeMask, iDb) ); @@ -5157,8 +6937,9 @@ case OP_Destroy: { /* out2 */ rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved); pOut->flags = MEM_Int; pOut->u.i = iMoved; + if( rc ) goto abort_due_to_error; #ifndef SQLITE_OMIT_AUTOVACUUM - if( rc==SQLITE_OK && iMoved!=0 ){ + if( iMoved!=0 ){ sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1); /* All OP_Destroy operations occur on the same btree */ assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 ); @@ -5175,27 +6956,25 @@ case OP_Destroy: { /* out2 */ ** in the database file is given by P1. But, unlike Destroy, do not ** remove the table or index from the database file. ** -** The table being clear is in the main database file if P2==0. If -** P2==1 then the table to be clear is in the auxiliary database file +** The table being cleared is in the main database file if P2==0. If +** P2==1 then the table to be cleared is in the auxiliary database file ** that is used to store tables create using CREATE TEMPORARY TABLE. ** -** If the P3 value is non-zero, then the table referred to must be an -** intkey table (an SQL table, not an index). In this case the row change -** count is incremented by the number of rows in the table being cleared. -** If P3 is greater than zero, then the value stored in register P3 is -** also incremented by the number of rows in the table being cleared. +** If the P3 value is non-zero, then the row change count is incremented +** by the number of rows in the table being cleared. If P3 is greater +** than zero, then the value stored in register P3 is also incremented +** by the number of rows in the table being cleared. ** ** See also: Destroy */ case OP_Clear: { - int nChange; - + i64 nChange; + + sqlite3VdbeIncrWriteCounter(p, 0); nChange = 0; assert( p->readOnly==0 ); assert( DbMaskTest(p->btreeMask, pOp->p2) ); - rc = sqlite3BtreeClearTable( - db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0) - ); + rc = sqlite3BtreeClearTable(db->aDb[pOp->p2].pBt, (u32)pOp->p1, &nChange); if( pOp->p3 ){ p->nChange += nChange; if( pOp->p3>0 ){ @@ -5204,6 +6983,7 @@ case OP_Clear: { aMem[pOp->p3].u.i += nChange; } } + if( rc ) goto abort_due_to_error; break; } @@ -5217,7 +6997,7 @@ case OP_Clear: { */ case OP_ResetSorter: { VdbeCursor *pC; - + assert( pOp->p1>=0 && pOp->p1<p->nCursor ); pC = p->apCsr[pOp->p1]; assert( pC!=0 ); @@ -5227,74 +7007,110 @@ case OP_ResetSorter: { assert( pC->eCurType==CURTYPE_BTREE ); assert( pC->isEphemeral ); rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor); + if( rc ) goto abort_due_to_error; } break; } -/* Opcode: CreateTable P1 P2 * * * -** Synopsis: r[P2]=root iDb=P1 -** -** Allocate a new table in the main database file if P1==0 or in the -** auxiliary database file if P1==1 or in an attached database if -** P1>1. Write the root page number of the new table into -** register P2 -** -** The difference between a table and an index is this: A table must -** have a 4-byte integer key and can have arbitrary data. An index -** has an arbitrary key but no data. +/* Opcode: CreateBtree P1 P2 P3 * * +** Synopsis: r[P2]=root iDb=P1 flags=P3 ** -** See also: CreateIndex +** Allocate a new b-tree in the main database file if P1==0 or in the +** TEMP database file if P1==1 or in an attached database if +** P1>1. The P3 argument must be 1 (BTREE_INTKEY) for a rowid table +** it must be 2 (BTREE_BLOBKEY) for an index or WITHOUT ROWID table. +** The root page number of the new b-tree is stored in register P2. */ -/* Opcode: CreateIndex P1 P2 * * * -** Synopsis: r[P2]=root iDb=P1 -** -** Allocate a new index in the main database file if P1==0 or in the -** auxiliary database file if P1==1 or in an attached database if -** P1>1. Write the root page number of the new table into -** register P2. -** -** See documentation on OP_CreateTable for additional information. -*/ -case OP_CreateIndex: /* out2 */ -case OP_CreateTable: { /* out2 */ - int pgno; - int flags; +case OP_CreateBtree: { /* out2 */ + Pgno pgno; Db *pDb; + sqlite3VdbeIncrWriteCounter(p, 0); pOut = out2Prerelease(p, pOp); pgno = 0; + assert( pOp->p3==BTREE_INTKEY || pOp->p3==BTREE_BLOBKEY ); assert( pOp->p1>=0 && pOp->p1<db->nDb ); assert( DbMaskTest(p->btreeMask, pOp->p1) ); assert( p->readOnly==0 ); pDb = &db->aDb[pOp->p1]; assert( pDb->pBt!=0 ); - if( pOp->opcode==OP_CreateTable ){ - /* flags = BTREE_INTKEY; */ - flags = BTREE_INTKEY; - }else{ - flags = BTREE_BLOBKEY; - } - rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, flags); + rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, pOp->p3); + if( rc ) goto abort_due_to_error; pOut->u.i = pgno; break; } +/* Opcode: SqlExec P1 P2 * P4 * +** +** Run the SQL statement or statements specified in the P4 string. +** +** The P1 parameter is a bitmask of options: +** +** 0x0001 Disable Auth and Trace callbacks while the statements +** in P4 are running. +** +** 0x0002 Set db->nAnalysisLimit to P2 while the statements in +** P4 are running. +** +*/ +case OP_SqlExec: { + char *zErr; +#ifndef SQLITE_OMIT_AUTHORIZATION + sqlite3_xauth xAuth; +#endif + u8 mTrace; + int savedAnalysisLimit; + + sqlite3VdbeIncrWriteCounter(p, 0); + db->nSqlExec++; + zErr = 0; +#ifndef SQLITE_OMIT_AUTHORIZATION + xAuth = db->xAuth; +#endif + mTrace = db->mTrace; + savedAnalysisLimit = db->nAnalysisLimit; + if( pOp->p1 & 0x0001 ){ +#ifndef SQLITE_OMIT_AUTHORIZATION + db->xAuth = 0; +#endif + db->mTrace = 0; + } + if( pOp->p1 & 0x0002 ){ + db->nAnalysisLimit = pOp->p2; + } + rc = sqlite3_exec(db, pOp->p4.z, 0, 0, &zErr); + db->nSqlExec--; +#ifndef SQLITE_OMIT_AUTHORIZATION + db->xAuth = xAuth; +#endif + db->mTrace = mTrace; + db->nAnalysisLimit = savedAnalysisLimit; + if( zErr || rc ){ + sqlite3VdbeError(p, "%s", zErr); + sqlite3_free(zErr); + if( rc==SQLITE_NOMEM ) goto no_mem; + goto abort_due_to_error; + } + break; +} + /* Opcode: ParseSchema P1 * * P4 * ** -** Read and parse all entries from the SQLITE_MASTER table of database P1 -** that match the WHERE clause P4. +** Read and parse all entries from the schema table of database P1 +** that match the WHERE clause P4. If P4 is a NULL pointer, then the +** entire schema for P1 is reparsed. ** ** This opcode invokes the parser to create a new virtual machine, ** then runs the new virtual machine. It is thus a re-entrant opcode. */ case OP_ParseSchema: { int iDb; - const char *zMaster; + const char *zSchema; char *zSql; InitData initData; /* Any prepared statement that invokes this opcode will hold mutexes - ** on every btree. This is a prerequisite for invoking + ** on every btree. This is a prerequisite for invoking ** sqlite3InitCallback(). */ #ifdef SQLITE_DEBUG @@ -5305,33 +7121,57 @@ case OP_ParseSchema: { iDb = pOp->p1; assert( iDb>=0 && iDb<db->nDb ); - assert( DbHasProperty(db, iDb, DB_SchemaLoaded) ); - /* Used to be a conditional */ { - zMaster = SCHEMA_TABLE(iDb); + assert( DbHasProperty(db, iDb, DB_SchemaLoaded) + || db->mallocFailed + || (CORRUPT_DB && (db->flags & SQLITE_NoSchemaError)!=0) ); + +#ifndef SQLITE_OMIT_ALTERTABLE + if( pOp->p4.z==0 ){ + sqlite3SchemaClear(db->aDb[iDb].pSchema); + db->mDbFlags &= ~DBFLAG_SchemaKnownOk; + rc = sqlite3InitOne(db, iDb, &p->zErrMsg, pOp->p5); + db->mDbFlags |= DBFLAG_SchemaChange; + p->expired = 0; + }else +#endif + { + zSchema = LEGACY_SCHEMA_TABLE; initData.db = db; - initData.iDb = pOp->p1; + initData.iDb = iDb; initData.pzErrMsg = &p->zErrMsg; + initData.mInitFlags = 0; + initData.mxPage = sqlite3BtreeLastPage(db->aDb[iDb].pBt); zSql = sqlite3MPrintf(db, - "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid", - db->aDb[iDb].zName, zMaster, pOp->p4.z); + "SELECT*FROM\"%w\".%s WHERE %s ORDER BY rowid", + db->aDb[iDb].zDbSName, zSchema, pOp->p4.z); if( zSql==0 ){ - rc = SQLITE_NOMEM; + rc = SQLITE_NOMEM_BKPT; }else{ assert( db->init.busy==0 ); db->init.busy = 1; initData.rc = SQLITE_OK; + initData.nInitRow = 0; assert( !db->mallocFailed ); rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0); if( rc==SQLITE_OK ) rc = initData.rc; - sqlite3DbFree(db, zSql); + if( rc==SQLITE_OK && initData.nInitRow==0 ){ + /* The OP_ParseSchema opcode with a non-NULL P4 argument should parse + ** at least one SQL statement. Any less than that indicates that + ** the sqlite_schema table is corrupt. */ + rc = SQLITE_CORRUPT_BKPT; + } + sqlite3DbFreeNN(db, zSql); db->init.busy = 0; } } - if( rc ) sqlite3ResetAllSchemasOfConnection(db); - if( rc==SQLITE_NOMEM ){ - goto no_mem; + if( rc ){ + sqlite3ResetAllSchemasOfConnection(db); + if( rc==SQLITE_NOMEM ){ + goto no_mem; + } + goto abort_due_to_error; } - break; + break; } #if !defined(SQLITE_OMIT_ANALYZE) @@ -5344,7 +7184,8 @@ case OP_ParseSchema: { case OP_LoadAnalysis: { assert( pOp->p1>=0 && pOp->p1<db->nDb ); rc = sqlite3AnalysisLoad(db, pOp->p1); - break; + if( rc ) goto abort_due_to_error; + break; } #endif /* !defined(SQLITE_OMIT_ANALYZE) */ @@ -5352,11 +7193,12 @@ case OP_LoadAnalysis: { ** ** Remove the internal (in-memory) data structures that describe ** the table named P4 in database P1. This is called after a table -** is dropped from disk (using the Destroy opcode) in order to keep +** is dropped from disk (using the Destroy opcode) in order to keep ** the internal representation of the ** schema consistent with what is on disk. */ case OP_DropTable: { + sqlite3VdbeIncrWriteCounter(p, 0); sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z); break; } @@ -5370,6 +7212,7 @@ case OP_DropTable: { ** schema consistent with what is on disk. */ case OP_DropIndex: { + sqlite3VdbeIncrWriteCounter(p, 0); sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z); break; } @@ -5378,31 +7221,31 @@ case OP_DropIndex: { ** ** Remove the internal (in-memory) data structures that describe ** the trigger named P4 in database P1. This is called after a trigger -** is dropped from disk (using the Destroy opcode) in order to keep +** is dropped from disk (using the Destroy opcode) in order to keep ** the internal representation of the ** schema consistent with what is on disk. */ case OP_DropTrigger: { + sqlite3VdbeIncrWriteCounter(p, 0); sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z); break; } #ifndef SQLITE_OMIT_INTEGRITY_CHECK -/* Opcode: IntegrityCk P1 P2 P3 * P5 +/* Opcode: IntegrityCk P1 P2 P3 P4 P5 ** ** Do an analysis of the currently open database. Store in -** register P1 the text of an error message describing any problems. -** If no problems are found, store a NULL in register P1. +** register (P1+1) the text of an error message describing any problems. +** If no problems are found, store a NULL in register (P1+1). ** -** The register P3 contains the maximum number of allowed errors. -** At most reg(P3) errors will be reported. -** In other words, the analysis stops as soon as reg(P1) errors are +** The register (P1) contains one less than the maximum number of allowed +** errors. At most reg(P1) errors will be reported. +** In other words, the analysis stops as soon as reg(P1) errors are ** seen. Reg(P1) is updated with the number of errors remaining. ** -** The root page numbers of all tables in the database are integer -** stored in reg(P1), reg(P1+1), reg(P1+2), .... There are P2 tables -** total. +** The root page numbers of all tables in the database are integers +** stored in P4_INTARRAY argument. ** ** If P5 is not zero, the check is done on the auxiliary database ** file, not the main database file. @@ -5411,50 +7254,47 @@ case OP_DropTrigger: { */ case OP_IntegrityCk: { int nRoot; /* Number of tables to check. (Number of root pages.) */ - int *aRoot; /* Array of rootpage numbers for tables to be checked */ - int j; /* Loop counter */ + Pgno *aRoot; /* Array of rootpage numbers for tables to be checked */ int nErr; /* Number of errors reported */ char *z; /* Text of the error report */ Mem *pnErr; /* Register keeping track of errors remaining */ assert( p->bIsReader ); + assert( pOp->p4type==P4_INTARRAY ); nRoot = pOp->p2; + aRoot = pOp->p4.ai; assert( nRoot>0 ); - aRoot = sqlite3DbMallocRawNN(db, sizeof(int)*(nRoot+1) ); - if( aRoot==0 ) goto no_mem; - assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) ); - pnErr = &aMem[pOp->p3]; + assert( aRoot!=0 ); + assert( aRoot[0]==(Pgno)nRoot ); + assert( pOp->p1>0 && (pOp->p1+1)<=(p->nMem+1 - p->nCursor) ); + pnErr = &aMem[pOp->p1]; assert( (pnErr->flags & MEM_Int)!=0 ); assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 ); - pIn1 = &aMem[pOp->p1]; - for(j=0; j<nRoot; j++){ - aRoot[j] = (int)sqlite3VdbeIntValue(&pIn1[j]); - } - aRoot[j] = 0; + pIn1 = &aMem[pOp->p1+1]; assert( pOp->p5<db->nDb ); assert( DbMaskTest(p->btreeMask, pOp->p5) ); - z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot, - (int)pnErr->u.i, &nErr); - sqlite3DbFree(db, aRoot); - pnErr->u.i -= nErr; + rc = sqlite3BtreeIntegrityCheck(db, db->aDb[pOp->p5].pBt, &aRoot[1], + &aMem[pOp->p3], nRoot, (int)pnErr->u.i+1, &nErr, &z); sqlite3VdbeMemSetNull(pIn1); if( nErr==0 ){ assert( z==0 ); - }else if( z==0 ){ - goto no_mem; + }else if( rc ){ + sqlite3_free(z); + goto abort_due_to_error; }else{ + pnErr->u.i -= nErr-1; sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free); } UPDATE_MAX_BLOBSIZE(pIn1); sqlite3VdbeChangeEncoding(pIn1, encoding); - break; + goto check_for_interrupt; } #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ /* Opcode: RowSetAdd P1 P2 * * * -** Synopsis: rowset(P1)=r[P2] +** Synopsis: rowset(P1)=r[P2] ** -** Insert the integer value held by register P2 into a boolean index +** Insert the integer value held by register P2 into a RowSet object ** held in register P1. ** ** An assertion fails if P2 is not an integer. @@ -5463,27 +7303,29 @@ case OP_RowSetAdd: { /* in1, in2 */ pIn1 = &aMem[pOp->p1]; pIn2 = &aMem[pOp->p2]; assert( (pIn2->flags & MEM_Int)!=0 ); - if( (pIn1->flags & MEM_RowSet)==0 ){ - sqlite3VdbeMemSetRowSet(pIn1); - if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem; + if( (pIn1->flags & MEM_Blob)==0 ){ + if( sqlite3VdbeMemSetRowSet(pIn1) ) goto no_mem; } - sqlite3RowSetInsert(pIn1->u.pRowSet, pIn2->u.i); + assert( sqlite3VdbeMemIsRowSet(pIn1) ); + sqlite3RowSetInsert((RowSet*)pIn1->z, pIn2->u.i); break; } /* Opcode: RowSetRead P1 P2 P3 * * -** Synopsis: r[P3]=rowset(P1) +** Synopsis: r[P3]=rowset(P1) ** -** Extract the smallest value from boolean index P1 and put that value into -** register P3. Or, if boolean index P1 is initially empty, leave P3 +** Extract the smallest value from the RowSet object in P1 +** and put that value into register P3. +** Or, if RowSet object P1 is initially empty, leave P3 ** unchanged and jump to instruction P2. */ case OP_RowSetRead: { /* jump, in1, out3 */ i64 val; pIn1 = &aMem[pOp->p1]; - if( (pIn1->flags & MEM_RowSet)==0 - || sqlite3RowSetNext(pIn1->u.pRowSet, &val)==0 + assert( (pIn1->flags & MEM_Blob)==0 || sqlite3VdbeMemIsRowSet(pIn1) ); + if( (pIn1->flags & MEM_Blob)==0 + || sqlite3RowSetNext((RowSet*)pIn1->z, &val)==0 ){ /* The boolean index is empty */ sqlite3VdbeMemSetNull(pIn1); @@ -5506,15 +7348,14 @@ case OP_RowSetRead: { /* jump, in1, out3 */ ** integer in P3 into the RowSet and continue on to the ** next opcode. ** -** The RowSet object is optimized for the case where successive sets -** of integers, where each set contains no duplicates. Each set -** of values is identified by a unique P4 value. The first set -** must have P4==0, the final set P4=-1. P4 must be either -1 or -** non-negative. For non-negative values of P4 only the lower 4 -** bits are significant. +** The RowSet object is optimized for the case where sets of integers +** are inserted in distinct phases, which each set contains no duplicates. +** Each set is identified by a unique P4 value. The first set +** must have P4==0, the final set must have P4==-1, and for all other sets +** must have P4>0. ** ** This allows optimizations: (a) when P4==0 there is no need to test -** the rowset object for P3, as it is guaranteed not to contain it, +** the RowSet object for P3, as it is guaranteed not to contain it, ** (b) when P4==-1 there is no need to insert the value, as it will ** never be tested for, and (c) when a value that is part of set X is ** inserted, there is no need to search to see if the same value was @@ -5533,20 +7374,19 @@ case OP_RowSetTest: { /* jump, in1, in3 */ /* If there is anything other than a rowset object in memory cell P1, ** delete it now and initialize P1 with an empty rowset */ - if( (pIn1->flags & MEM_RowSet)==0 ){ - sqlite3VdbeMemSetRowSet(pIn1); - if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem; + if( (pIn1->flags & MEM_Blob)==0 ){ + if( sqlite3VdbeMemSetRowSet(pIn1) ) goto no_mem; } - + assert( sqlite3VdbeMemIsRowSet(pIn1) ); assert( pOp->p4type==P4_INT32 ); assert( iSet==-1 || iSet>=0 ); if( iSet ){ - exists = sqlite3RowSetTest(pIn1->u.pRowSet, iSet, pIn3->u.i); + exists = sqlite3RowSetTest((RowSet*)pIn1->z, iSet, pIn3->u.i); VdbeBranchTaken(exists!=0,2); if( exists ) goto jump_to_p2; } if( iSet>=0 ){ - sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i); + sqlite3RowSetInsert((RowSet*)pIn1->z, pIn3->u.i); } break; } @@ -5556,22 +7396,24 @@ case OP_RowSetTest: { /* jump, in1, in3 */ /* Opcode: Program P1 P2 P3 P4 P5 ** -** Execute the trigger program passed as P4 (type P4_SUBPROGRAM). +** Execute the trigger program passed as P4 (type P4_SUBPROGRAM). ** -** P1 contains the address of the memory cell that contains the first memory -** cell in an array of values used as arguments to the sub-program. P2 -** contains the address to jump to if the sub-program throws an IGNORE -** exception using the RAISE() function. Register P3 contains the address -** of a memory cell in this (the parent) VM that is used to allocate the +** P1 contains the address of the memory cell that contains the first memory +** cell in an array of values used as arguments to the sub-program. P2 +** contains the address to jump to if the sub-program throws an IGNORE +** exception using the RAISE() function. P2 might be zero, if there is +** no possibility that an IGNORE exception will be raised. +** Register P3 contains the address +** of a memory cell in this (the parent) VM that is used to allocate the ** memory required by the sub-vdbe at runtime. ** ** P4 is a pointer to the VM containing the trigger program. ** ** If P5 is non-zero, then recursive program invocation is enabled. */ -case OP_Program: { /* jump */ +case OP_Program: { /* jump0 */ int nMem; /* Number of memory registers for sub-program */ - int nByte; /* Bytes of runtime space required for sub-program */ + i64 nByte; /* Bytes of runtime space required for sub-program */ Mem *pRt; /* Register to allocate runtime space */ Mem *pMem; /* Used to iterate through memory cells */ Mem *pEnd; /* Last memory cell in new array */ @@ -5582,17 +7424,17 @@ case OP_Program: { /* jump */ pProgram = pOp->p4.pProgram; pRt = &aMem[pOp->p3]; assert( pProgram->nOp>0 ); - - /* If the p5 flag is clear, then recursive invocation of triggers is + + /* If the p5 flag is clear, then recursive invocation of triggers is ** disabled for backwards compatibility (p5 is set if this sub-program ** is really a trigger, not a foreign key action, and the flag set ** and cleared by the "PRAGMA recursive_triggers" command is clear). - ** - ** It is recursive invocation of triggers, at the SQL level, that is - ** disabled. In some cases a single trigger may generate more than one - ** SubProgram (if the trigger may be executed with more than one different + ** + ** It is recursive invocation of triggers, at the SQL level, that is + ** disabled. In some cases a single trigger may generate more than one + ** SubProgram (if the trigger may be executed with more than one different ** ON CONFLICT algorithm). SubProgram structures associated with a - ** single trigger all have the same value for the SubProgram.token + ** single trigger all have the same value for the SubProgram.token ** variable. */ if( pOp->p5 ){ t = pProgram->token; @@ -5603,31 +7445,35 @@ case OP_Program: { /* jump */ if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){ rc = SQLITE_ERROR; sqlite3VdbeError(p, "too many levels of trigger recursion"); - break; + goto abort_due_to_error; } /* Register pRt is used to store the memory required to save the state ** of the current program, and the memory required at runtime to execute - ** the trigger program. If this trigger has been fired before, then pRt + ** the trigger program. If this trigger has been fired before, then pRt ** is already allocated. Otherwise, it must be initialized. */ - if( (pRt->flags&MEM_Frame)==0 ){ - /* SubProgram.nMem is set to the number of memory cells used by the + if( (pRt->flags&MEM_Blob)==0 ){ + /* SubProgram.nMem is set to the number of memory cells used by the ** program stored in SubProgram.aOp. As well as these, one memory ** cell is required for each cursor used by the program. Set local ** variable nMem (and later, VdbeFrame.nChildMem) to this value. */ nMem = pProgram->nMem + pProgram->nCsr; + assert( nMem>0 ); + if( pProgram->nCsr==0 ) nMem++; nByte = ROUND8(sizeof(VdbeFrame)) + nMem * sizeof(Mem) - + pProgram->nCsr * sizeof(VdbeCursor *) - + pProgram->nOnce * sizeof(u8); + + pProgram->nCsr * sizeof(VdbeCursor*) + + (7 + (i64)pProgram->nOp)/8; pFrame = sqlite3DbMallocZero(db, nByte); if( !pFrame ){ goto no_mem; } sqlite3VdbeMemRelease(pRt); - pRt->flags = MEM_Frame; - pRt->u.pFrame = pFrame; + pRt->flags = MEM_Blob|MEM_Dyn; + pRt->z = (char*)pFrame; + pRt->n = (int)nByte; + pRt->xDel = sqlite3VdbeFrameMemDel; pFrame->v = p; pFrame->nChildMem = nMem; @@ -5640,10 +7486,8 @@ case OP_Program: { /* jump */ pFrame->aOp = p->aOp; pFrame->nOp = p->nOp; pFrame->token = pProgram->token; - pFrame->aOnceFlag = p->aOnceFlag; - pFrame->nOnceFlag = p->nOnceFlag; -#ifdef SQLITE_ENABLE_STMT_SCANSTATUS - pFrame->anExec = p->anExec; +#ifdef SQLITE_DEBUG + pFrame->iFrameMagic = SQLITE_FRAME_MAGIC; #endif pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem]; @@ -5652,42 +7496,53 @@ case OP_Program: { /* jump */ pMem->db = db; } }else{ - pFrame = pRt->u.pFrame; - assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem ); + pFrame = (VdbeFrame*)pRt->z; + assert( pRt->xDel==sqlite3VdbeFrameMemDel ); + assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem + || (pProgram->nCsr==0 && pProgram->nMem+1==pFrame->nChildMem) ); assert( pProgram->nCsr==pFrame->nChildCsr ); assert( (int)(pOp - aOp)==pFrame->pc ); } p->nFrame++; pFrame->pParent = p->pFrame; - pFrame->lastRowid = lastRowid; + pFrame->lastRowid = db->lastRowid; pFrame->nChange = p->nChange; pFrame->nDbChange = p->db->nChange; + assert( pFrame->pAuxData==0 ); + pFrame->pAuxData = p->pAuxData; + p->pAuxData = 0; p->nChange = 0; p->pFrame = pFrame; - p->aMem = aMem = &VdbeFrameMem(pFrame)[-1]; + p->aMem = aMem = VdbeFrameMem(pFrame); p->nMem = pFrame->nChildMem; p->nCursor = (u16)pFrame->nChildCsr; - p->apCsr = (VdbeCursor **)&aMem[p->nMem+1]; + p->apCsr = (VdbeCursor **)&aMem[p->nMem]; + pFrame->aOnce = (u8*)&p->apCsr[pProgram->nCsr]; + memset(pFrame->aOnce, 0, (pProgram->nOp + 7)/8); p->aOp = aOp = pProgram->aOp; p->nOp = pProgram->nOp; - p->aOnceFlag = (u8 *)&p->apCsr[p->nCursor]; - p->nOnceFlag = pProgram->nOnce; -#ifdef SQLITE_ENABLE_STMT_SCANSTATUS - p->anExec = 0; +#ifdef SQLITE_DEBUG + /* Verify that second and subsequent executions of the same trigger do not + ** try to reuse register values from the first use. */ + { + int i; + for(i=0; i<p->nMem; i++){ + aMem[i].pScopyFrom = 0; /* Prevent false-positive AboutToChange() errs */ + MemSetTypeFlag(&aMem[i], MEM_Undefined); /* Fault if this reg is reused */ + } + } #endif pOp = &aOp[-1]; - memset(p->aOnceFlag, 0, p->nOnceFlag); - - break; + goto check_for_interrupt; } /* Opcode: Param P1 P2 * * * ** -** This opcode is only ever present in sub-programs called via the -** OP_Program instruction. Copy a value currently stored in a memory -** cell of the calling (parent) frame to cell P2 in the current frames -** address space. This is used by trigger programs to access the new.* +** This opcode is only ever present in sub-programs called via the +** OP_Program instruction. Copy a value currently stored in a memory +** cell of the calling (parent) frame to cell P2 in the current frames +** address space. This is used by trigger programs to access the new.* ** and old.* values. ** ** The address of the cell in the parent frame is determined by adding @@ -5699,7 +7554,7 @@ case OP_Param: { /* out2 */ Mem *pIn; pOut = out2Prerelease(p, pOp); pFrame = p->pFrame; - pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1]; + pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1]; sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem); break; } @@ -5711,17 +7566,19 @@ case OP_Param: { /* out2 */ ** Synopsis: fkctr[P1]+=P2 ** ** Increment a "constraint counter" by P2 (P2 may be negative or positive). -** If P1 is non-zero, the database constraint counter is incremented -** (deferred foreign key constraints). Otherwise, if P1 is zero, the +** If P1 is non-zero, the database constraint counter is incremented +** (deferred foreign key constraints). Otherwise, if P1 is zero, the ** statement counter is incremented (immediate foreign key constraints). */ case OP_FkCounter: { - if( db->flags & SQLITE_DeferFKs ){ - db->nDeferredImmCons += pOp->p2; - }else if( pOp->p1 ){ + if( pOp->p1 ){ db->nDeferredCons += pOp->p2; }else{ - p->nFkConstraint += pOp->p2; + if( db->flags & SQLITE_DeferFKs ){ + db->nDeferredImmCons += pOp->p2; + }else{ + p->nFkConstraint += pOp->p2; + } } break; } @@ -5730,7 +7587,7 @@ case OP_FkCounter: { ** Synopsis: if fkctr[P1]==0 goto P2 ** ** This opcode tests if a foreign key constraint-counter is currently zero. -** If so, jump to instruction P2. Otherwise, fall through to the next +** If so, jump to instruction P2. Otherwise, fall through to the next ** instruction. ** ** If P1 is non-zero, then the jump is taken if the database constraint-counter @@ -5756,7 +7613,7 @@ case OP_FkIfZero: { /* jump */ ** ** P1 is a register in the root frame of this VM (the root frame is ** different from the current frame if this instruction is being executed -** within a sub-program). Set the value of register P1 to the maximum of +** within a sub-program). Set the value of register P1 to the maximum of ** its current value and the value in register P2. ** ** This instruction throws an error if the memory cell is not initially @@ -5806,7 +7663,7 @@ case OP_IfPos: { /* jump, in1 */ ** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) ** ** This opcode performs a commonly used computation associated with -** LIMIT and OFFSET process. r[P1] holds the limit counter. r[P3] +** LIMIT and OFFSET processing. r[P1] holds the limit counter. r[P3] ** holds the offset counter. The opcode computes the combined value ** of the LIMIT and OFFSET and stores that value in r[P2]. The r[P2] ** value computed is the total number of rows that will need to be @@ -5816,34 +7673,47 @@ case OP_IfPos: { /* jump, in1 */ ** and r[P2] is set to be the value of the LIMIT, r[P1]. ** ** if r[P1] is zero or negative, that means there is no LIMIT -** and r[P2] is set to -1. +** and r[P2] is set to -1. ** ** Otherwise, r[P2] is set to the sum of r[P1] and r[P3]. */ case OP_OffsetLimit: { /* in1, out2, in3 */ + i64 x; pIn1 = &aMem[pOp->p1]; pIn3 = &aMem[pOp->p3]; pOut = out2Prerelease(p, pOp); assert( pIn1->flags & MEM_Int ); assert( pIn3->flags & MEM_Int ); - pOut->u.i = pIn1->u.i<=0 ? -1 : pIn1->u.i+(pIn3->u.i>0?pIn3->u.i:0); + x = pIn1->u.i; + if( x<=0 || sqlite3AddInt64(&x, pIn3->u.i>0?pIn3->u.i:0) ){ + /* If the LIMIT is less than or equal to zero, loop forever. This + ** is documented. But also, if the LIMIT+OFFSET exceeds 2^63 then + ** also loop forever. This is undocumented. In fact, one could argue + ** that the loop should terminate. But assuming 1 billion iterations + ** per second (far exceeding the capabilities of any current hardware) + ** it would take nearly 300 years to actually reach the limit. So + ** looping forever is a reasonable approximation. */ + pOut->u.i = -1; + }else{ + pOut->u.i = x; + } break; } -/* Opcode: IfNotZero P1 P2 P3 * * -** Synopsis: if r[P1]!=0 then r[P1]-=P3, goto P2 +/* Opcode: IfNotZero P1 P2 * * * +** Synopsis: if r[P1]!=0 then r[P1]--, goto P2 ** ** Register P1 must contain an integer. If the content of register P1 is -** initially nonzero, then subtract P3 from the value in register P1 and -** jump to P2. If register P1 is initially zero, leave it unchanged -** and fall through. +** initially greater than zero, then decrement the value in register P1. +** If it is non-zero (negative or positive) and then also jump to P2. +** If register P1 is initially zero, leave it unchanged and fall through. */ case OP_IfNotZero: { /* jump, in1 */ pIn1 = &aMem[pOp->p1]; assert( pIn1->flags&MEM_Int ); VdbeBranchTaken(pIn1->u.i<0, 2); if( pIn1->u.i ){ - pIn1->u.i -= pOp->p3; + if( pIn1->u.i>0 ) pIn1->u.i--; goto jump_to_p2; } break; @@ -5852,52 +7722,48 @@ case OP_IfNotZero: { /* jump, in1 */ /* Opcode: DecrJumpZero P1 P2 * * * ** Synopsis: if (--r[P1])==0 goto P2 ** -** Register P1 must hold an integer. Decrement the value in register P1 -** then jump to P2 if the new value is exactly zero. +** Register P1 must hold an integer. Decrement the value in P1 +** and jump to P2 if the new value is exactly zero. */ case OP_DecrJumpZero: { /* jump, in1 */ pIn1 = &aMem[pOp->p1]; assert( pIn1->flags&MEM_Int ); - pIn1->u.i--; + if( pIn1->u.i>SMALLEST_INT64 ) pIn1->u.i--; VdbeBranchTaken(pIn1->u.i==0, 2); if( pIn1->u.i==0 ) goto jump_to_p2; break; } -/* Opcode: JumpZeroIncr P1 P2 * * * -** Synopsis: if (r[P1]++)==0 ) goto P2 +/* Opcode: AggStep * P2 P3 P4 P5 +** Synopsis: accum=r[P3] step(r[P2@P5]) +** +** Execute the xStep function for an aggregate. +** The function has P5 arguments. P4 is a pointer to the +** FuncDef structure that specifies the function. Register P3 is the +** accumulator. ** -** The register P1 must contain an integer. If register P1 is initially -** zero, then jump to P2. Increment register P1 regardless of whether or -** not the jump is taken. +** The P5 arguments are taken from register P2 and its +** successors. */ -case OP_JumpZeroIncr: { /* jump, in1 */ - pIn1 = &aMem[pOp->p1]; - assert( pIn1->flags&MEM_Int ); - VdbeBranchTaken(pIn1->u.i==0, 2); - if( (pIn1->u.i++)==0 ) goto jump_to_p2; - break; -} - -/* Opcode: AggStep0 * P2 P3 P4 P5 -** Synopsis: accum=r[P3] step(r[P2@P5]) +/* Opcode: AggInverse * P2 P3 P4 P5 +** Synopsis: accum=r[P3] inverse(r[P2@P5]) ** -** Execute the step function for an aggregate. The -** function has P5 arguments. P4 is a pointer to the FuncDef -** structure that specifies the function. Register P3 is the +** Execute the xInverse function for an aggregate. +** The function has P5 arguments. P4 is a pointer to the +** FuncDef structure that specifies the function. Register P3 is the ** accumulator. ** ** The P5 arguments are taken from register P2 and its ** successors. */ -/* Opcode: AggStep * P2 P3 P4 P5 +/* Opcode: AggStep1 P1 P2 P3 P4 P5 ** Synopsis: accum=r[P3] step(r[P2@P5]) ** -** Execute the step function for an aggregate. The -** function has P5 arguments. P4 is a pointer to an sqlite3_context -** object that is used to run the function. Register P3 is -** as the accumulator. +** Execute the xStep (if P1==0) or xInverse (if P1!=0) function for an +** aggregate. The function has P5 arguments. P4 is a pointer to the +** FuncDef structure that specifies the function. Register P3 is the +** accumulator. ** ** The P5 arguments are taken from register P2 and its ** successors. @@ -5908,41 +7774,74 @@ case OP_JumpZeroIncr: { /* jump, in1 */ ** sqlite3_context only happens once, instead of on each call to the ** step function. */ -case OP_AggStep0: { +case OP_AggInverse: +case OP_AggStep: { int n; sqlite3_context *pCtx; + u64 nAlloc; assert( pOp->p4type==P4_FUNCDEF ); n = pOp->p5; - assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) ); - assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem-p->nCursor)+1) ); + assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); + assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) ); assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n ); - pCtx = sqlite3DbMallocRawNN(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*)); + + /* Allocate space for (a) the context object and (n-1) extra pointers + ** to append to the sqlite3_context.argv[1] array, and (b) a memory + ** cell in which to store the accumulation. Be careful that the memory + ** cell is 8-byte aligned, even on platforms where a pointer is 32-bits. + ** + ** Note: We could avoid this by using a regular memory cell from aMem[] for + ** the accumulator, instead of allocating one here. */ + nAlloc = ROUND8P( SZ_CONTEXT(n) ); + pCtx = sqlite3DbMallocRawNN(db, nAlloc + sizeof(Mem)); if( pCtx==0 ) goto no_mem; + pCtx->pOut = (Mem*)((u8*)pCtx + nAlloc); + assert( EIGHT_BYTE_ALIGNMENT(pCtx->pOut) ); + + sqlite3VdbeMemInit(pCtx->pOut, db, MEM_Null); pCtx->pMem = 0; pCtx->pFunc = pOp->p4.pFunc; pCtx->iOp = (int)(pOp - aOp); pCtx->pVdbe = p; + pCtx->skipFlag = 0; + pCtx->isError = 0; + pCtx->enc = encoding; pCtx->argc = n; pOp->p4type = P4_FUNCCTX; pOp->p4.pCtx = pCtx; - pOp->opcode = OP_AggStep; + + /* OP_AggInverse must have P1==1 and OP_AggStep must have P1==0 */ + assert( pOp->p1==(pOp->opcode==OP_AggInverse) ); + + pOp->opcode = OP_AggStep1; /* Fall through into OP_AggStep */ + /* no break */ deliberate_fall_through } -case OP_AggStep: { +case OP_AggStep1: { int i; sqlite3_context *pCtx; Mem *pMem; - Mem t; assert( pOp->p4type==P4_FUNCCTX ); pCtx = pOp->p4.pCtx; pMem = &aMem[pOp->p3]; - /* If this function is inside of a trigger, the register array in aMem[] +#ifdef SQLITE_DEBUG + if( pOp->p1 ){ + /* This is an OP_AggInverse call. Verify that xStep has always + ** been called at least once prior to any xInverse call. */ + assert( pMem->uTemp==0x1122e0e3 ); + }else{ + /* This is an OP_AggStep call. Mark it as such. */ + pMem->uTemp = 0x1122e0e3; + } +#endif + + /* If this function is inside of a trigger, the register array in aMem[] ** might change from one evaluation to the next. The next block of code ** checks to see if the register array has changed, and if so it - ** reinitializes the relavant parts of the sqlite3_context object */ + ** reinitializes the relevant parts of the sqlite3_context object */ if( pCtx->pMem != pMem ){ pCtx->pMem = pMem; for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i]; @@ -5956,55 +7855,88 @@ case OP_AggStep: { #endif pMem->n++; - sqlite3VdbeMemInit(&t, db, MEM_Null); - pCtx->pOut = &t; - pCtx->fErrorOrAux = 0; - pCtx->skipFlag = 0; + assert( pCtx->pOut->flags==MEM_Null ); + assert( pCtx->isError==0 ); + assert( pCtx->skipFlag==0 ); +#ifndef SQLITE_OMIT_WINDOWFUNC + if( pOp->p1 ){ + (pCtx->pFunc->xInverse)(pCtx,pCtx->argc,pCtx->argv); + }else +#endif (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */ - if( pCtx->fErrorOrAux ){ - if( pCtx->isError ){ - sqlite3VdbeError(p, "%s", sqlite3_value_text(&t)); + + if( pCtx->isError ){ + if( pCtx->isError>0 ){ + sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut)); rc = pCtx->isError; } - sqlite3VdbeMemRelease(&t); - }else{ - assert( t.flags==MEM_Null ); - } - if( pCtx->skipFlag ){ - assert( pOp[-1].opcode==OP_CollSeq ); - i = pOp[-1].p1; - if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1); + if( pCtx->skipFlag ){ + assert( pOp[-1].opcode==OP_CollSeq ); + i = pOp[-1].p1; + if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1); + pCtx->skipFlag = 0; + } + sqlite3VdbeMemRelease(pCtx->pOut); + pCtx->pOut->flags = MEM_Null; + pCtx->isError = 0; + if( rc ) goto abort_due_to_error; } + assert( pCtx->pOut->flags==MEM_Null ); + assert( pCtx->skipFlag==0 ); break; } /* Opcode: AggFinal P1 P2 * P4 * ** Synopsis: accum=r[P1] N=P2 ** -** Execute the finalizer function for an aggregate. P1 is -** the memory location that is the accumulator for the aggregate. +** P1 is the memory location that is the accumulator for an aggregate +** or window function. Execute the finalizer function +** for an aggregate and store the result in P1. ** ** P2 is the number of arguments that the step function takes and ** P4 is a pointer to the FuncDef for this function. The P2 ** argument is not used by this opcode. It is only there to disambiguate ** functions that can take varying numbers of arguments. The -** P4 argument is only needed for the degenerate case where +** P4 argument is only needed for the case where ** the step function was not previously called. */ +/* Opcode: AggValue * P2 P3 P4 * +** Synopsis: r[P3]=value N=P2 +** +** Invoke the xValue() function and store the result in register P3. +** +** P2 is the number of arguments that the step function takes and +** P4 is a pointer to the FuncDef for this function. The P2 +** argument is not used by this opcode. It is only there to disambiguate +** functions that can take varying numbers of arguments. The +** P4 argument is only needed for the case where +** the step function was not previously called. +*/ +case OP_AggValue: case OP_AggFinal: { Mem *pMem; - assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) ); + assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); + assert( pOp->p3==0 || pOp->opcode==OP_AggValue ); pMem = &aMem[pOp->p1]; assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 ); - rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc); +#ifndef SQLITE_OMIT_WINDOWFUNC + if( pOp->p3 ){ + memAboutToChange(p, &aMem[pOp->p3]); + rc = sqlite3VdbeMemAggValue(pMem, &aMem[pOp->p3], pOp->p4.pFunc); + pMem = &aMem[pOp->p3]; + }else +#endif + { + rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc); + } + if( rc ){ sqlite3VdbeError(p, "%s", sqlite3_value_text(pMem)); + goto abort_due_to_error; } sqlite3VdbeChangeEncoding(pMem, encoding); UPDATE_MAX_BLOBSIZE(pMem); - if( sqlite3VdbeMemTooBig(pMem) ){ - goto too_big; - } + REGISTER_TRACE((int)(pMem-aMem), pMem); break; } @@ -6032,17 +7964,19 @@ case OP_Checkpoint: { || pOp->p2==SQLITE_CHECKPOINT_FULL || pOp->p2==SQLITE_CHECKPOINT_RESTART || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE + || pOp->p2==SQLITE_CHECKPOINT_NOOP ); rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]); - if( rc==SQLITE_BUSY ){ + if( rc ){ + if( rc!=SQLITE_BUSY ) goto abort_due_to_error; rc = SQLITE_OK; aRes[0] = 1; } for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){ sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]); - } + } break; -}; +}; #endif #ifndef SQLITE_OMIT_PRAGMA @@ -6068,9 +8002,9 @@ case OP_JournalMode: { /* out2 */ pOut = out2Prerelease(p, pOp); eNew = pOp->p3; - assert( eNew==PAGER_JOURNALMODE_DELETE - || eNew==PAGER_JOURNALMODE_TRUNCATE - || eNew==PAGER_JOURNALMODE_PERSIST + assert( eNew==PAGER_JOURNALMODE_DELETE + || eNew==PAGER_JOURNALMODE_TRUNCATE + || eNew==PAGER_JOURNALMODE_PERSIST || eNew==PAGER_JOURNALMODE_OFF || eNew==PAGER_JOURNALMODE_MEMORY || eNew==PAGER_JOURNALMODE_WAL @@ -6083,13 +8017,14 @@ case OP_JournalMode: { /* out2 */ pPager = sqlite3BtreePager(pBt); eOld = sqlite3PagerGetJournalMode(pPager); if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld; + assert( sqlite3BtreeHoldsMutex(pBt) ); if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld; #ifndef SQLITE_OMIT_WAL zFilename = sqlite3PagerFilename(pPager, 1); /* Do not allow a transition to journal_mode=WAL for a database - ** in temporary storage or if the VFS does not support shared memory + ** in temporary storage or if the VFS does not support shared memory */ if( eNew==PAGER_JOURNALMODE_WAL && (sqlite3Strlen30(zFilename)==0 /* Temp file */ @@ -6107,16 +8042,16 @@ case OP_JournalMode: { /* out2 */ "cannot change %s wal mode from within a transaction", (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of") ); - break; + goto abort_due_to_error; }else{ - + if( eOld==PAGER_JOURNALMODE_WAL ){ /* If leaving WAL mode, close the log file. If successful, the call - ** to PagerCloseWal() checkpoints and deletes the write-ahead-log - ** file. An EXCLUSIVE lock may still be held on the database file - ** after a successful return. + ** to PagerCloseWal() checkpoints and deletes the write-ahead-log + ** file. An EXCLUSIVE lock may still be held on the database file + ** after a successful return. */ - rc = sqlite3PagerCloseWal(pPager); + rc = sqlite3PagerCloseWal(pPager, db); if( rc==SQLITE_OK ){ sqlite3PagerSetJournalMode(pPager, eNew); } @@ -6125,11 +8060,11 @@ case OP_JournalMode: { /* out2 */ ** as an intermediate */ sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF); } - + /* Open a transaction on the database file. Regardless of the journal ** mode, this transaction always uses a rollback journal. */ - assert( sqlite3BtreeIsInTrans(pBt)==0 ); + assert( sqlite3BtreeTxnState(pBt)!=SQLITE_TXN_WRITE ); if( rc==SQLITE_OK ){ rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1)); } @@ -6137,9 +8072,7 @@ case OP_JournalMode: { /* out2 */ } #endif /* ifndef SQLITE_OMIT_WAL */ - if( rc ){ - eNew = eOld; - } + if( rc ) eNew = eOld; eNew = sqlite3PagerSetJournalMode(pPager, eNew); pOut->flags = MEM_Str|MEM_Static|MEM_Term; @@ -6147,20 +8080,26 @@ case OP_JournalMode: { /* out2 */ pOut->n = sqlite3Strlen30(pOut->z); pOut->enc = SQLITE_UTF8; sqlite3VdbeChangeEncoding(pOut, encoding); + if( rc ) goto abort_due_to_error; break; }; #endif /* SQLITE_OMIT_PRAGMA */ #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH) -/* Opcode: Vacuum * * * * * +/* Opcode: Vacuum P1 P2 * * * ** -** Vacuum the entire database. This opcode will cause other virtual -** machines to be created and run. It may not be called from within -** a transaction. +** Vacuum the entire database P1. P1 is 0 for "main", and 2 or more +** for an attached database. The "temp" database may not be vacuumed. +** +** If P2 is not zero, then it is a register holding a string which is +** the file into which the result of vacuum should be written. When +** P2 is zero, the vacuum overwrites the original database. */ case OP_Vacuum: { assert( p->readOnly==0 ); - rc = sqlite3RunVacuum(&p->zErrMsg, db); + rc = sqlite3RunVacuum(&p->zErrMsg, db, pOp->p1, + pOp->p2 ? &aMem[pOp->p2] : 0); + if( rc ) goto abort_due_to_error; break; } #endif @@ -6181,7 +8120,8 @@ case OP_IncrVacuum: { /* jump */ pBt = db->aDb[pOp->p1].pBt; rc = sqlite3BtreeIncrVacuum(pBt); VdbeBranchTaken(rc==SQLITE_DONE,2); - if( rc==SQLITE_DONE ){ + if( rc ){ + if( rc!=SQLITE_DONE ) goto abort_due_to_error; rc = SQLITE_OK; goto jump_to_p2; } @@ -6189,31 +8129,68 @@ case OP_IncrVacuum: { /* jump */ } #endif -/* Opcode: Expire P1 * * * * +/* Opcode: Expire P1 P2 * * * ** ** Cause precompiled statements to expire. When an expired statement ** is executed using sqlite3_step() it will either automatically ** reprepare itself (if it was originally created using sqlite3_prepare_v2()) ** or it will fail with SQLITE_SCHEMA. -** +** ** If P1 is 0, then all SQL statements become expired. If P1 is non-zero, ** then only the currently executing statement is expired. +** +** If P2 is 0, then SQL statements are expired immediately. If P2 is 1, +** then running SQL statements are allowed to continue to run to completion. +** The P2==1 case occurs when a CREATE INDEX or similar schema change happens +** that might help the statement run faster but which does not affect the +** correctness of operation. */ case OP_Expire: { + assert( pOp->p2==0 || pOp->p2==1 ); if( !pOp->p1 ){ - sqlite3ExpirePreparedStatements(db); + sqlite3ExpirePreparedStatements(db, pOp->p2); }else{ - p->expired = 1; + p->expired = pOp->p2+1; } break; } +/* Opcode: CursorLock P1 * * * * +** +** Lock the btree to which cursor P1 is pointing so that the btree cannot be +** written by an other cursor. +*/ +case OP_CursorLock: { + VdbeCursor *pC; + assert( pOp->p1>=0 && pOp->p1<p->nCursor ); + pC = p->apCsr[pOp->p1]; + assert( pC!=0 ); + assert( pC->eCurType==CURTYPE_BTREE ); + sqlite3BtreeCursorPin(pC->uc.pCursor); + break; +} + +/* Opcode: CursorUnlock P1 * * * * +** +** Unlock the btree to which cursor P1 is pointing so that it can be +** written by other cursors. +*/ +case OP_CursorUnlock: { + VdbeCursor *pC; + assert( pOp->p1>=0 && pOp->p1<p->nCursor ); + pC = p->apCsr[pOp->p1]; + assert( pC!=0 ); + assert( pC->eCurType==CURTYPE_BTREE ); + sqlite3BtreeCursorUnpin(pC->uc.pCursor); + break; +} + #ifndef SQLITE_OMIT_SHARED_CACHE /* Opcode: TableLock P1 P2 P3 P4 * ** Synopsis: iDb=P1 root=P2 write=P3 ** ** Obtain a lock on a particular table. This instruction is only used when -** the shared-cache feature is enabled. +** the shared-cache feature is enabled. ** ** P1 is the index of the database in sqlite3.aDb[] of the database ** on which the lock is acquired. A readlock is obtained if P3==0 or @@ -6226,15 +8203,18 @@ case OP_Expire: { */ case OP_TableLock: { u8 isWriteLock = (u8)pOp->p3; - if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommitted) ){ - int p1 = pOp->p1; + if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommit) ){ + int p1 = pOp->p1; assert( p1>=0 && p1<db->nDb ); assert( DbMaskTest(p->btreeMask, p1) ); assert( isWriteLock==0 || isWriteLock==1 ); rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock); - if( (rc&0xFF)==SQLITE_LOCKED ){ - const char *z = pOp->p4.z; - sqlite3VdbeError(p, "database table is locked: %s", z); + if( rc ){ + if( (rc&0xFF)==SQLITE_LOCKED ){ + const char *z = pOp->p4.z; + sqlite3VdbeError(p, "database table is locked: %s", z); + } + goto abort_due_to_error; } } break; @@ -6244,7 +8224,7 @@ case OP_TableLock: { #ifndef SQLITE_OMIT_VIRTUALTABLE /* Opcode: VBegin * * * P4 * ** -** P4 may be a pointer to an sqlite3_vtab structure. If so, call the +** P4 may be a pointer to an sqlite3_vtab structure. If so, call the ** xBegin method for that table. ** ** Also, whether or not P4 is set, check that this is not being called from @@ -6256,6 +8236,7 @@ case OP_VBegin: { pVTab = pOp->p4.pVtab; rc = sqlite3VtabBegin(db, pVTab); if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab); + if( rc ) goto abort_due_to_error; break; } #endif /* SQLITE_OMIT_VIRTUALTABLE */ @@ -6263,7 +8244,7 @@ case OP_VBegin: { #ifndef SQLITE_OMIT_VIRTUALTABLE /* Opcode: VCreate P1 P2 * * * ** -** P2 is a register that holds the name of a virtual table in database +** P2 is a register that holds the name of a virtual table in database ** P1. Call the xCreate method for that table. */ case OP_VCreate: { @@ -6284,6 +8265,7 @@ case OP_VCreate: { rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg); } sqlite3VdbeMemRelease(&sMem); + if( rc ) goto abort_due_to_error; break; } #endif /* SQLITE_OMIT_VIRTUALTABLE */ @@ -6298,6 +8280,8 @@ case OP_VDestroy: { db->nVDestroy++; rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z); db->nVDestroy--; + assert( p->errorAction==OE_Abort && p->usesStmtJournal ); + if( rc ) goto abort_due_to_error; break; } #endif /* SQLITE_OMIT_VIRTUALTABLE */ @@ -6309,42 +8293,123 @@ case OP_VDestroy: { ** P1 is a cursor number. This opcode opens a cursor to the virtual ** table and stores that cursor in P1. */ -case OP_VOpen: { +case OP_VOpen: { /* ncycle */ VdbeCursor *pCur; sqlite3_vtab_cursor *pVCur; sqlite3_vtab *pVtab; const sqlite3_module *pModule; assert( p->bIsReader ); - pCur = 0; + pCur = p->apCsr[pOp->p1]; + if( pCur!=0 + && ALWAYS( pCur->eCurType==CURTYPE_VTAB ) + && ALWAYS( pCur->uc.pVCur->pVtab==pOp->p4.pVtab->pVtab ) + ){ + /* This opcode is a no-op if the cursor is already open */ + break; + } pVCur = 0; pVtab = pOp->p4.pVtab->pVtab; if( pVtab==0 || NEVER(pVtab->pModule==0) ){ rc = SQLITE_LOCKED; - break; + goto abort_due_to_error; } pModule = pVtab->pModule; rc = pModule->xOpen(pVtab, &pVCur); sqlite3VtabImportErrmsg(p, pVtab); - if( SQLITE_OK==rc ){ - /* Initialize sqlite3_vtab_cursor base class */ - pVCur->pVtab = pVtab; - - /* Initialize vdbe cursor object */ - pCur = allocateCursor(p, pOp->p1, 0, -1, CURTYPE_VTAB); - if( pCur ){ - pCur->uc.pVCur = pVCur; - pVtab->nRef++; - }else{ - assert( db->mallocFailed ); - pModule->xClose(pVCur); - goto no_mem; - } + if( rc ) goto abort_due_to_error; + + /* Initialize sqlite3_vtab_cursor base class */ + pVCur->pVtab = pVtab; + + /* Initialize vdbe cursor object */ + pCur = allocateCursor(p, pOp->p1, 0, CURTYPE_VTAB); + if( pCur ){ + pCur->uc.pVCur = pVCur; + pVtab->nRef++; + }else{ + assert( db->mallocFailed ); + pModule->xClose(pVCur); + goto no_mem; + } + break; +} +#endif /* SQLITE_OMIT_VIRTUALTABLE */ + +#ifndef SQLITE_OMIT_VIRTUALTABLE +/* Opcode: VCheck P1 P2 P3 P4 * +** +** P4 is a pointer to a Table object that is a virtual table in schema P1 +** that supports the xIntegrity() method. This opcode runs the xIntegrity() +** method for that virtual table, using P3 as the integer argument. If +** an error is reported back, the table name is prepended to the error +** message and that message is stored in P2. If no errors are seen, +** register P2 is set to NULL. +*/ +case OP_VCheck: { /* out2 */ + Table *pTab; + sqlite3_vtab *pVtab; + const sqlite3_module *pModule; + char *zErr = 0; + + pOut = &aMem[pOp->p2]; + sqlite3VdbeMemSetNull(pOut); /* Innocent until proven guilty */ + assert( pOp->p4type==P4_TABLEREF ); + pTab = pOp->p4.pTab; + assert( pTab!=0 ); + assert( pTab->nTabRef>0 ); + assert( IsVirtual(pTab) ); + if( pTab->u.vtab.p==0 ) break; + pVtab = pTab->u.vtab.p->pVtab; + assert( pVtab!=0 ); + pModule = pVtab->pModule; + assert( pModule!=0 ); + assert( pModule->iVersion>=4 ); + assert( pModule->xIntegrity!=0 ); + sqlite3VtabLock(pTab->u.vtab.p); + assert( pOp->p1>=0 && pOp->p1<db->nDb ); + rc = pModule->xIntegrity(pVtab, db->aDb[pOp->p1].zDbSName, pTab->zName, + pOp->p3, &zErr); + sqlite3VtabUnlock(pTab->u.vtab.p); + if( rc ){ + sqlite3_free(zErr); + goto abort_due_to_error; + } + if( zErr ){ + sqlite3VdbeMemSetStr(pOut, zErr, -1, SQLITE_UTF8, sqlite3_free); } break; } #endif /* SQLITE_OMIT_VIRTUALTABLE */ +#ifndef SQLITE_OMIT_VIRTUALTABLE +/* Opcode: VInitIn P1 P2 P3 * * +** Synopsis: r[P2]=ValueList(P1,P3) +** +** Set register P2 to be a pointer to a ValueList object for cursor P1 +** with cache register P3 and output register P3+1. This ValueList object +** can be used as the first argument to sqlite3_vtab_in_first() and +** sqlite3_vtab_in_next() to extract all of the values stored in the P1 +** cursor. Register P3 is used to hold the values returned by +** sqlite3_vtab_in_first() and sqlite3_vtab_in_next(). +*/ +case OP_VInitIn: { /* out2, ncycle */ + VdbeCursor *pC; /* The cursor containing the RHS values */ + ValueList *pRhs; /* New ValueList object to put in reg[P2] */ + + pC = p->apCsr[pOp->p1]; + pRhs = sqlite3_malloc64( sizeof(*pRhs) ); + if( pRhs==0 ) goto no_mem; + pRhs->pCsr = pC->uc.pCursor; + pRhs->pOut = &aMem[pOp->p3]; + pOut = out2Prerelease(p, pOp); + pOut->flags = MEM_Null; + sqlite3VdbeMemSetPointer(pOut, pRhs, "ValueList", sqlite3VdbeValueListFree); + break; +} +#endif /* SQLITE_OMIT_VIRTUALTABLE */ + + #ifndef SQLITE_OMIT_VIRTUALTABLE /* Opcode: VFilter P1 P2 P3 P4 * ** Synopsis: iplan=r[P3] zplan='P4' @@ -6365,7 +8430,7 @@ case OP_VOpen: { ** ** A jump is made to P2 if the result set after filtering would be empty. */ -case OP_VFilter: { /* jump */ +case OP_VFilter: { /* jump, ncycle */ int nArg; int iQuery; const sqlite3_module *pModule; @@ -6383,6 +8448,7 @@ case OP_VFilter: { /* jump */ pCur = p->apCsr[pOp->p1]; assert( memIsValid(pQuery) ); REGISTER_TRACE(pOp->p3, pQuery); + assert( pCur!=0 ); assert( pCur->eCurType==CURTYPE_VTAB ); pVCur = pCur->uc.pVCur; pVtab = pVCur->pVtab; @@ -6394,16 +8460,15 @@ case OP_VFilter: { /* jump */ iQuery = (int)pQuery->u.i; /* Invoke the xFilter method */ - res = 0; apArg = p->apArg; + assert( nArg<=p->napArg ); for(i = 0; i<nArg; i++){ apArg[i] = &pArgc[i+1]; } rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg); sqlite3VtabImportErrmsg(p, pVtab); - if( rc==SQLITE_OK ){ - res = pModule->xEof(pVCur); - } + if( rc ) goto abort_due_to_error; + res = pModule->xEof(pVCur); pCur->nullRow = 0; VdbeBranchTaken(res!=0,2); if( res ) goto jump_to_p2; @@ -6412,46 +8477,65 @@ case OP_VFilter: { /* jump */ #endif /* SQLITE_OMIT_VIRTUALTABLE */ #ifndef SQLITE_OMIT_VIRTUALTABLE -/* Opcode: VColumn P1 P2 P3 * * +/* Opcode: VColumn P1 P2 P3 * P5 ** Synopsis: r[P3]=vcolumn(P2) ** -** Store the value of the P2-th column of -** the row of the virtual-table that the -** P1 cursor is pointing to into register P3. +** Store in register P3 the value of the P2-th column of +** the current row of the virtual-table of cursor P1. +** +** If the VColumn opcode is being used to fetch the value of +** an unchanging column during an UPDATE operation, then the P5 +** value is OPFLAG_NOCHNG. This will cause the sqlite3_vtab_nochange() +** function to return true inside the xColumn method of the virtual +** table implementation. The P5 column might also contain other +** bits (OPFLAG_LENGTHARG or OPFLAG_TYPEOFARG) but those bits are +** unused by OP_VColumn. */ -case OP_VColumn: { +case OP_VColumn: { /* ncycle */ sqlite3_vtab *pVtab; const sqlite3_module *pModule; Mem *pDest; sqlite3_context sContext; + FuncDef nullFunc; VdbeCursor *pCur = p->apCsr[pOp->p1]; - assert( pCur->eCurType==CURTYPE_VTAB ); - assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) ); + assert( pCur!=0 ); + assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); pDest = &aMem[pOp->p3]; memAboutToChange(p, pDest); if( pCur->nullRow ){ sqlite3VdbeMemSetNull(pDest); break; } + assert( pCur->eCurType==CURTYPE_VTAB ); pVtab = pCur->uc.pVCur->pVtab; pModule = pVtab->pModule; assert( pModule->xColumn ); memset(&sContext, 0, sizeof(sContext)); sContext.pOut = pDest; - MemSetTypeFlag(pDest, MEM_Null); + sContext.enc = encoding; + nullFunc.pUserData = 0; + nullFunc.funcFlags = SQLITE_RESULT_SUBTYPE; + sContext.pFunc = &nullFunc; + assert( pOp->p5==OPFLAG_NOCHNG || pOp->p5==0 ); + if( pOp->p5 & OPFLAG_NOCHNG ){ + sqlite3VdbeMemSetNull(pDest); + pDest->flags = MEM_Null|MEM_Zero; + pDest->u.nZero = 0; + }else{ + MemSetTypeFlag(pDest, MEM_Null); + } rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2); sqlite3VtabImportErrmsg(p, pVtab); - if( sContext.isError ){ + if( sContext.isError>0 ){ + sqlite3VdbeError(p, "%s", sqlite3_value_text(pDest)); rc = sContext.isError; } sqlite3VdbeChangeEncoding(pDest, encoding); REGISTER_TRACE(pOp->p3, pDest); UPDATE_MAX_BLOBSIZE(pDest); - if( sqlite3VdbeMemTooBig(pDest) ){ - goto too_big; - } + if( rc ) goto abort_due_to_error; break; } #endif /* SQLITE_OMIT_VIRTUALTABLE */ @@ -6463,14 +8547,14 @@ case OP_VColumn: { ** jump to instruction P2. Or, if the virtual table has reached ** the end of its result set, then fall through to the next instruction. */ -case OP_VNext: { /* jump */ +case OP_VNext: { /* jump, ncycle */ sqlite3_vtab *pVtab; const sqlite3_module *pModule; int res; VdbeCursor *pCur; - res = 0; pCur = p->apCsr[pOp->p1]; + assert( pCur!=0 ); assert( pCur->eCurType==CURTYPE_VTAB ); if( pCur->nullRow ){ break; @@ -6481,15 +8565,14 @@ case OP_VNext: { /* jump */ /* Invoke the xNext() method of the module. There is no way for the ** underlying implementation to return an error if one occurs during - ** xNext(). Instead, if an error occurs, true is returned (indicating that + ** xNext(). Instead, if an error occurs, true is returned (indicating that ** data is available) and the error code returned when xColumn or ** some other method is next invoked on the save virtual table cursor. */ rc = pModule->xNext(pCur->uc.pVCur); sqlite3VtabImportErrmsg(p, pVtab); - if( rc==SQLITE_OK ){ - res = pModule->xEof(pCur->uc.pVCur); - } + if( rc ) goto abort_due_to_error; + res = pModule->xEof(pCur->uc.pVCur); VdbeBranchTaken(!res,2); if( !res ){ /* If there is data, jump to P2 */ @@ -6509,7 +8592,10 @@ case OP_VNext: { /* jump */ case OP_VRename: { sqlite3_vtab *pVtab; Mem *pName; - + int isLegacy; + + isLegacy = (db->flags & SQLITE_LegacyAlter); + db->flags |= SQLITE_LegacyAlter; pVtab = pOp->p4.pVtab->pVtab; pName = &aMem[pOp->p1]; assert( pVtab->pModule->xRename ); @@ -6521,11 +8607,12 @@ case OP_VRename: { testcase( pName->enc==SQLITE_UTF16BE ); testcase( pName->enc==SQLITE_UTF16LE ); rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF8); - if( rc==SQLITE_OK ){ - rc = pVtab->pModule->xRename(pVtab, pName->z); - sqlite3VtabImportErrmsg(p, pVtab); - p->expired = 0; - } + if( rc ) goto abort_due_to_error; + rc = pVtab->pModule->xRename(pVtab, pName->z); + if( isLegacy==0 ) db->flags &= ~(u64)SQLITE_LegacyAlter; + sqlite3VtabImportErrmsg(p, pVtab); + p->expired = 0; + if( rc ) goto abort_due_to_error; break; } #endif @@ -6536,23 +8623,23 @@ case OP_VRename: { ** ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. ** This opcode invokes the corresponding xUpdate method. P2 values -** are contiguous memory cells starting at P3 to pass to the xUpdate -** invocation. The value in register (P3+P2-1) corresponds to the +** are contiguous memory cells starting at P3 to pass to the xUpdate +** invocation. The value in register (P3+P2-1) corresponds to the ** p2th element of the argv array passed to xUpdate. ** ** The xUpdate method will do a DELETE or an INSERT or both. ** The argv[0] element (which corresponds to memory cell P3) -** is the rowid of a row to delete. If argv[0] is NULL then no -** deletion occurs. The argv[1] element is the rowid of the new -** row. This can be NULL to have the virtual table select the new -** rowid for itself. The subsequent elements in the array are +** is the rowid of a row to delete. If argv[0] is NULL then no +** deletion occurs. The argv[1] element is the rowid of the new +** row. This can be NULL to have the virtual table select the new +** rowid for itself. The subsequent elements in the array are ** the values of columns in the new row. ** ** If P2==1 then no insert is performed. argv[0] is the rowid of ** a row to delete. ** ** P1 is a boolean flag. If it is set to true and the xUpdate call -** is successful, then the value returned by sqlite3_last_insert_rowid() +** is successful, then the value returned by sqlite3_last_insert_rowid() ** is set to the value of the rowid for the row just inserted. ** ** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to @@ -6563,18 +8650,20 @@ case OP_VUpdate: { const sqlite3_module *pModule; int nArg; int i; - sqlite_int64 rowid; + sqlite_int64 rowid = 0; Mem **apArg; Mem *pX; - assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback + assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace ); assert( p->readOnly==0 ); + if( db->mallocFailed ) goto no_mem; + sqlite3VdbeIncrWriteCounter(p, 0); pVtab = pOp->p4.pVtab->pVtab; if( pVtab==0 || NEVER(pVtab->pModule==0) ){ rc = SQLITE_LOCKED; - break; + goto abort_due_to_error; } pModule = pVtab->pModule; nArg = pOp->p2; @@ -6583,6 +8672,7 @@ case OP_VUpdate: { u8 vtabOnConflict = db->vtabOnConflict; apArg = p->apArg; pX = &aMem[pOp->p3]; + assert( nArg<=p->napArg ); for(i=0; i<nArg; i++){ assert( memIsValid(pX) ); memAboutToChange(p, pX); @@ -6595,7 +8685,7 @@ case OP_VUpdate: { sqlite3VtabImportErrmsg(p, pVtab); if( rc==SQLITE_OK && pOp->p1 ){ assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) ); - db->lastRowid = lastRowid = rowid; + db->lastRowid = rowid; } if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){ if( pOp->p5==OE_Ignore ){ @@ -6606,6 +8696,7 @@ case OP_VUpdate: { }else{ p->nChange++; } + if( rc ) goto abort_due_to_error; } break; } @@ -6649,9 +8740,232 @@ case OP_MaxPgcnt: { /* out2 */ } #endif +/* Opcode: Function P1 P2 P3 P4 * +** Synopsis: r[P3]=func(r[P2@NP]) +** +** Invoke a user function (P4 is a pointer to an sqlite3_context object that +** contains a pointer to the function to be run) with arguments taken +** from register P2 and successors. The number of arguments is in +** the sqlite3_context object that P4 points to. +** The result of the function is stored +** in register P3. Register P3 must not be one of the function inputs. +** +** P1 is a 32-bit bitmask indicating whether or not each argument to the +** function was determined to be constant at compile time. If the first +** argument was constant then bit 0 of P1 is set. This is used to determine +** whether meta data associated with a user function argument using the +** sqlite3_set_auxdata() API may be safely retained until the next +** invocation of this opcode. +** +** See also: AggStep, AggFinal, PureFunc +*/ +/* Opcode: PureFunc P1 P2 P3 P4 * +** Synopsis: r[P3]=func(r[P2@NP]) +** +** Invoke a user function (P4 is a pointer to an sqlite3_context object that +** contains a pointer to the function to be run) with arguments taken +** from register P2 and successors. The number of arguments is in +** the sqlite3_context object that P4 points to. +** The result of the function is stored +** in register P3. Register P3 must not be one of the function inputs. +** +** P1 is a 32-bit bitmask indicating whether or not each argument to the +** function was determined to be constant at compile time. If the first +** argument was constant then bit 0 of P1 is set. This is used to determine +** whether meta data associated with a user function argument using the +** sqlite3_set_auxdata() API may be safely retained until the next +** invocation of this opcode. +** +** This opcode works exactly like OP_Function. The only difference is in +** its name. This opcode is used in places where the function must be +** purely non-deterministic. Some built-in date/time functions can be +** either deterministic of non-deterministic, depending on their arguments. +** When those function are used in a non-deterministic way, they will check +** to see if they were called using OP_PureFunc instead of OP_Function, and +** if they were, they throw an error. +** +** See also: AggStep, AggFinal, Function +*/ +case OP_PureFunc: /* group */ +case OP_Function: { /* group */ + int i; + sqlite3_context *pCtx; + + assert( pOp->p4type==P4_FUNCCTX ); + pCtx = pOp->p4.pCtx; + + /* If this function is inside of a trigger, the register array in aMem[] + ** might change from one evaluation to the next. The next block of code + ** checks to see if the register array has changed, and if so it + ** reinitializes the relevant parts of the sqlite3_context object */ + pOut = &aMem[pOp->p3]; + if( pCtx->pOut != pOut ){ + pCtx->pVdbe = p; + pCtx->pOut = pOut; + pCtx->enc = encoding; + for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i]; + } + assert( pCtx->pVdbe==p ); + + memAboutToChange(p, pOut); +#ifdef SQLITE_DEBUG + for(i=0; i<pCtx->argc; i++){ + assert( memIsValid(pCtx->argv[i]) ); + REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]); + } +#endif + MemSetTypeFlag(pOut, MEM_Null); + assert( pCtx->isError==0 ); + (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */ + + /* If the function returned an error, throw an exception */ + if( pCtx->isError ){ + if( pCtx->isError>0 ){ + sqlite3VdbeError(p, "%s", sqlite3_value_text(pOut)); + rc = pCtx->isError; + } + sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1); + pCtx->isError = 0; + if( rc ) goto abort_due_to_error; + } + + assert( (pOut->flags&MEM_Str)==0 + || pOut->enc==encoding + || db->mallocFailed ); + assert( !sqlite3VdbeMemTooBig(pOut) ); + + REGISTER_TRACE(pOp->p3, pOut); + UPDATE_MAX_BLOBSIZE(pOut); + break; +} + +/* Opcode: ClrSubtype P1 * * * * +** Synopsis: r[P1].subtype = 0 +** +** Clear the subtype from register P1. +*/ +case OP_ClrSubtype: { /* in1 */ + pIn1 = &aMem[pOp->p1]; + pIn1->flags &= ~MEM_Subtype; + break; +} + +/* Opcode: GetSubtype P1 P2 * * * +** Synopsis: r[P2] = r[P1].subtype +** +** Extract the subtype value from register P1 and write that subtype +** into register P2. If P1 has no subtype, then P1 gets a NULL. +*/ +case OP_GetSubtype: { /* in1 out2 */ + pIn1 = &aMem[pOp->p1]; + pOut = &aMem[pOp->p2]; + if( pIn1->flags & MEM_Subtype ){ + sqlite3VdbeMemSetInt64(pOut, pIn1->eSubtype); + }else{ + sqlite3VdbeMemSetNull(pOut); + } + break; +} + +/* Opcode: SetSubtype P1 P2 * * * +** Synopsis: r[P2].subtype = r[P1] +** +** Set the subtype value of register P2 to the integer from register P1. +** If P1 is NULL, clear the subtype from p2. +*/ +case OP_SetSubtype: { /* in1 out2 */ + pIn1 = &aMem[pOp->p1]; + pOut = &aMem[pOp->p2]; + if( pIn1->flags & MEM_Null ){ + pOut->flags &= ~MEM_Subtype; + }else{ + assert( pIn1->flags & MEM_Int ); + pOut->flags |= MEM_Subtype; + pOut->eSubtype = (u8)(pIn1->u.i & 0xff); + } + break; +} + +/* Opcode: FilterAdd P1 * P3 P4 * +** Synopsis: filter(P1) += key(P3@P4) +** +** Compute a hash on the P4 registers starting with r[P3] and +** add that hash to the bloom filter contained in r[P1]. +*/ +case OP_FilterAdd: { + u64 h; + + assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); + pIn1 = &aMem[pOp->p1]; + assert( pIn1->flags & MEM_Blob ); + assert( pIn1->n>0 ); + h = filterHash(aMem, pOp); +#ifdef SQLITE_DEBUG + if( db->flags&SQLITE_VdbeTrace ){ + int ii; + for(ii=pOp->p3; ii<pOp->p3+pOp->p4.i; ii++){ + registerTrace(ii, &aMem[ii]); + } + printf("hash: %llu modulo %d -> %u\n", h, pIn1->n, (int)(h%pIn1->n)); + } +#endif + h %= (pIn1->n*8); + pIn1->z[h/8] |= 1<<(h&7); + break; +} + +/* Opcode: Filter P1 P2 P3 P4 * +** Synopsis: if key(P3@P4) not in filter(P1) goto P2 +** +** Compute a hash on the key contained in the P4 registers starting +** with r[P3]. Check to see if that hash is found in the +** bloom filter hosted by register P1. If it is not present then +** maybe jump to P2. Otherwise fall through. +** +** False negatives are harmless. It is always safe to fall through, +** even if the value is in the bloom filter. A false negative causes +** more CPU cycles to be used, but it should still yield the correct +** answer. However, an incorrect answer may well arise from a +** false positive - if the jump is taken when it should fall through. +*/ +case OP_Filter: { /* jump */ + u64 h; + + assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) ); + pIn1 = &aMem[pOp->p1]; + assert( (pIn1->flags & MEM_Blob)!=0 ); + assert( pIn1->n >= 1 ); + h = filterHash(aMem, pOp); +#ifdef SQLITE_DEBUG + if( db->flags&SQLITE_VdbeTrace ){ + int ii; + for(ii=pOp->p3; ii<pOp->p3+pOp->p4.i; ii++){ + registerTrace(ii, &aMem[ii]); + } + printf("hash: %llu modulo %d -> %u\n", h, pIn1->n, (int)(h%pIn1->n)); + } +#endif + h %= (pIn1->n*8); + if( (pIn1->z[h/8] & (1<<(h&7)))==0 ){ + VdbeBranchTaken(1, 2); + p->aCounter[SQLITE_STMTSTATUS_FILTER_HIT]++; + goto jump_to_p2; + }else{ + p->aCounter[SQLITE_STMTSTATUS_FILTER_MISS]++; + VdbeBranchTaken(0, 2); + } + break; +} -/* Opcode: Init * P2 * P4 * -** Synopsis: Start at P2 +/* Opcode: Trace P1 P2 * P4 * +** +** Write P4 on the statement trace output if statement tracing is +** enabled. +** +** Operand P1 must be 0x7fffffff and P2 must positive. +*/ +/* Opcode: Init P1 P2 P3 P4 * +** Synopsis: Start at P2 ** ** Programs contain a single instance of this opcode as the very first ** opcode. @@ -6661,27 +8975,61 @@ case OP_MaxPgcnt: { /* out2 */ ** Or if P4 is blank, use the string returned by sqlite3_sql(). ** ** If P2 is not zero, jump to instruction P2. +** +** Increment the value of P1 so that OP_Once opcodes will jump the +** first time they are evaluated for this run. +** +** If P3 is not zero, then it is an address to jump to if an SQLITE_CORRUPT +** error is encountered. */ -case OP_Init: { /* jump */ +case OP_Trace: +case OP_Init: { /* jump0 */ + int i; +#ifndef SQLITE_OMIT_TRACE char *zTrace; - char *z; +#endif + + /* If the P4 argument is not NULL, then it must be an SQL comment string. + ** The "--" string is broken up to prevent false-positives with srcck1.c. + ** + ** This assert() provides evidence for: + ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that + ** would have been returned by the legacy sqlite3_trace() interface by + ** using the X argument when X begins with "--" and invoking + ** sqlite3_expanded_sql(P) otherwise. + */ + assert( pOp->p4.z==0 || strncmp(pOp->p4.z, "-" "- ", 3)==0 ); + + /* OP_Init is always instruction 0 */ + assert( pOp==p->aOp || pOp->opcode==OP_Trace ); #ifndef SQLITE_OMIT_TRACE - if( db->xTrace - && !p->doingRerun + if( (db->mTrace & (SQLITE_TRACE_STMT|SQLITE_TRACE_LEGACY))!=0 + && p->minWriteFileFormat!=254 /* tag-20220401a */ && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 ){ - z = sqlite3VdbeExpandSql(p, zTrace); - db->xTrace(db->pTraceArg, z); - sqlite3DbFree(db, z); +#ifndef SQLITE_OMIT_DEPRECATED + if( db->mTrace & SQLITE_TRACE_LEGACY ){ + char *z = sqlite3VdbeExpandSql(p, zTrace); + db->trace.xLegacy(db->pTraceArg, z); + sqlite3_free(z); + }else +#endif + if( db->nVdbeExec>1 ){ + char *z = sqlite3MPrintf(db, "-- %s", zTrace); + (void)db->trace.xV2(SQLITE_TRACE_STMT, db->pTraceArg, p, z); + sqlite3DbFree(db, z); + }else{ + (void)db->trace.xV2(SQLITE_TRACE_STMT, db->pTraceArg, p, zTrace); + } } #ifdef SQLITE_USE_FCNTL_TRACE zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql); if( zTrace ){ - int i; - for(i=0; i<db->nDb; i++){ - if( DbMaskTest(p->btreeMask, i)==0 ) continue; - sqlite3_file_control(db, db->aDb[i].zName, SQLITE_FCNTL_TRACE, zTrace); + int j; + for(j=0; j<db->nDb; j++){ + if( DbMaskTest(p->btreeMask, j)==0 ) continue; + sqlite3_file_control(db, db->aDb[j].zDbSName, SQLITE_FCNTL_TRACE, zTrace); } } #endif /* SQLITE_USE_FCNTL_TRACE */ @@ -6693,8 +9041,17 @@ case OP_Init: { /* jump */ } #endif /* SQLITE_DEBUG */ #endif /* SQLITE_OMIT_TRACE */ - if( pOp->p2 ) goto jump_to_p2; - break; + assert( pOp->p2>0 ); + if( pOp->p1>=sqlite3GlobalConfig.iOnceResetThreshold ){ + if( pOp->opcode==OP_Trace ) break; + for(i=1; i<p->nOp; i++){ + if( p->aOp[i].opcode==OP_Once ) p->aOp[i].p1 = 0; + } + pOp->p1 = 0; + } + pOp->p1++; + p->aCounter[SQLITE_STMTSTATUS_RUN]++; + goto jump_to_p2; } #ifdef SQLITE_ENABLE_CURSOR_HINTS @@ -6720,19 +9077,100 @@ case OP_CursorHint: { } #endif /* SQLITE_ENABLE_CURSOR_HINTS */ +#ifdef SQLITE_DEBUG +/* Opcode: Abortable * * * * * +** +** Verify that an Abort can happen. Assert if an Abort at this point +** might cause database corruption. This opcode only appears in debugging +** builds. +** +** An Abort is safe if either there have been no writes, or if there is +** an active statement journal. +*/ +case OP_Abortable: { + sqlite3VdbeAssertAbortable(p); + break; +} +#endif + +#ifdef SQLITE_DEBUG +/* Opcode: ReleaseReg P1 P2 P3 * P5 +** Synopsis: release r[P1@P2] mask P3 +** +** Release registers from service. Any content that was in the +** the registers is unreliable after this opcode completes. +** +** The registers released will be the P2 registers starting at P1, +** except if bit ii of P3 set, then do not release register P1+ii. +** In other words, P3 is a mask of registers to preserve. +** +** Releasing a register clears the Mem.pScopyFrom pointer. That means +** that if the content of the released register was set using OP_SCopy, +** a change to the value of the source register for the OP_SCopy will no longer +** generate an assertion fault in sqlite3VdbeMemAboutToChange(). +** +** If P5 is set, then all released registers have their type set +** to MEM_Undefined so that any subsequent attempt to read the released +** register (before it is reinitialized) will generate an assertion fault. +** +** P5 ought to be set on every call to this opcode. +** However, there are places in the code generator will release registers +** before their are used, under the (valid) assumption that the registers +** will not be reallocated for some other purpose before they are used and +** hence are safe to release. +** +** This opcode is only available in testing and debugging builds. It is +** not generated for release builds. The purpose of this opcode is to help +** validate the generated bytecode. This opcode does not actually contribute +** to computing an answer. +*/ +case OP_ReleaseReg: { + Mem *pMem; + int i; + u32 constMask; + assert( pOp->p1>0 ); + assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 ); + pMem = &aMem[pOp->p1]; + constMask = pOp->p3; + for(i=0; i<pOp->p2; i++, pMem++){ + if( i>=32 || (constMask & MASKBIT32(i))==0 ){ + pMem->pScopyFrom = 0; + if( i<32 && pOp->p5 ) MemSetTypeFlag(pMem, MEM_Undefined); + } + } + break; +} +#endif + /* Opcode: Noop * * * * * ** -** Do nothing. This instruction is often useful as a jump -** destination. +** Do nothing. Continue downward to the next opcode. */ -/* -** The magic Explain opcode are only inserted when explain==2 (which -** is to say when the EXPLAIN QUERY PLAN syntax is used.) -** This opcode records information from the optimizer. It is the -** the same as a no-op. This opcodesnever appears in a real VM program. +/* Opcode: Explain P1 P2 P3 P4 * +** +** This is the same as OP_Noop during normal query execution. The +** purpose of this opcode is to hold information about the query +** plan for the purpose of EXPLAIN QUERY PLAN output. +** +** The P4 value is human-readable text that describes the query plan +** element. Something like "SCAN t1" or "SEARCH t2 USING INDEX t2x1". +** +** The P1 value is the ID of the current element and P2 is the parent +** element for the case of nested query plan elements. If P2 is zero +** then this element is a top-level element. +** +** For loop elements, P3 is the estimated code of each invocation of this +** element. +** +** As with all opcodes, the meanings of the parameters for OP_Explain +** are subject to change from one release to the next. Applications +** should not attempt to interpret or use any of the information +** contained in the OP_Explain opcode. The information provided by this +** opcode is intended for testing and debugging use only. */ -default: { /* This is really OP_Noop and OP_Explain */ +default: { /* This is really OP_Noop, OP_Explain */ assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain ); + break; } @@ -6744,11 +9182,13 @@ default: { /* This is really OP_Noop and OP_Explain */ *****************************************************************************/ } -#ifdef VDBE_PROFILE - { - u64 endTime = sqlite3Hwtime(); - if( endTime>start ) pOrigOp->cycles += endTime - start; - pOrigOp->cnt++; +#if defined(VDBE_PROFILE) + *pnCycle += sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime(); + pnCycle = 0; +#elif defined(SQLITE_ENABLE_STMT_SCANSTATUS) + if( pnCycle ){ + *pnCycle += sqlite3Hwtime(); + pnCycle = 0; } #endif @@ -6762,13 +9202,20 @@ default: { /* This is really OP_Noop and OP_Explain */ #ifdef SQLITE_DEBUG if( db->flags & SQLITE_VdbeTrace ){ + u8 opProperty = sqlite3OpcodeProperty[pOrigOp->opcode]; if( rc!=0 ) printf("rc=%d\n",rc); - if( pOrigOp->opflags & (OPFLG_OUT2) ){ + if( opProperty & (OPFLG_OUT2) ){ registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]); } - if( pOrigOp->opflags & OPFLG_OUT3 ){ + if( opProperty & OPFLG_OUT3 ){ registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]); } + if( opProperty==0xff ){ + /* Never happens. This code exists to avoid a harmless linkage + ** warning about sqlite3VdbeRegisterDump() being defined but not + ** used. */ + sqlite3VdbeRegisterDump(p); + } } #endif /* SQLITE_DEBUG */ #endif /* NDEBUG */ @@ -6777,14 +9224,37 @@ default: { /* This is really OP_Noop and OP_Explain */ /* If we reach this point, it means that execution is finished with ** an error of some kind. */ -vdbe_error_halt: +abort_due_to_error: + if( db->mallocFailed ){ + rc = SQLITE_NOMEM_BKPT; + }else if( rc==SQLITE_IOERR_CORRUPTFS ){ + rc = SQLITE_CORRUPT_BKPT; + } assert( rc ); +#ifdef SQLITE_DEBUG + if( db->flags & SQLITE_VdbeTrace ){ + const char *zTrace = p->zSql; + if( zTrace==0 ){ + if( aOp[0].opcode==OP_Trace ){ + zTrace = aOp[0].p4.z; + } + if( zTrace==0 ) zTrace = "???"; + } + printf("ABORT-due-to-error (rc=%d): %s\n", rc, zTrace); + } +#endif + if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM ){ + sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc)); + } p->rc = rc; + sqlite3SystemError(db, rc); testcase( sqlite3GlobalConfig.xLog!=0 ); - sqlite3_log(rc, "statement aborts at %d: [%s] %s", - (int)(pOp - aOp), p->zSql, p->zErrMsg); - sqlite3VdbeHalt(p); + sqlite3VdbeLogAbort(p, rc, pOp, aOp); + if( p->eVdbeState==VDBE_RUN_STATE ) sqlite3VdbeHalt(p); if( rc==SQLITE_IOERR_NOMEM ) sqlite3OomFault(db); + if( rc==SQLITE_CORRUPT && db->autoCommit==0 ){ + db->flags |= SQLITE_CorruptRdOnly; + } rc = SQLITE_ERROR; if( resetSchemaOnFault>0 ){ sqlite3ResetOneSchema(db, resetSchemaOnFault-1); @@ -6794,12 +9264,34 @@ default: { /* This is really OP_Noop and OP_Explain */ ** release the mutexes on btrees that were acquired at the ** top. */ vdbe_return: - db->lastRowid = lastRowid; - testcase( nVmStep>0 ); +#if defined(VDBE_PROFILE) + if( pnCycle ){ + *pnCycle += sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime(); + pnCycle = 0; + } +#elif defined(SQLITE_ENABLE_STMT_SCANSTATUS) + if( pnCycle ){ + *pnCycle += sqlite3Hwtime(); + pnCycle = 0; + } +#endif + +#ifndef SQLITE_OMIT_PROGRESS_CALLBACK + while( nVmStep>=nProgressLimit && db->xProgress!=0 ){ + nProgressLimit += db->nProgressOps; + if( db->xProgress(db->pProgressArg) ){ + nProgressLimit = LARGEST_UINT64; + rc = SQLITE_INTERRUPT; + goto abort_due_to_error; + } + } +#endif p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep; - sqlite3VdbeLeave(p); - assert( rc!=SQLITE_OK || nExtraDelete==0 - || sqlite3_strlike("DELETE%",p->zSql,0)!=0 + if( DbMaskNonZero(p->lockMask) ){ + sqlite3VdbeLeave(p); + } + assert( rc!=SQLITE_OK || nExtraDelete==0 + || sqlite3_strlike("DELETE%",p->zSql,0)!=0 ); return rc; @@ -6809,34 +9301,21 @@ default: { /* This is really OP_Noop and OP_Explain */ too_big: sqlite3VdbeError(p, "string or blob too big"); rc = SQLITE_TOOBIG; - goto vdbe_error_halt; + goto abort_due_to_error; /* Jump to here if a malloc() fails. */ no_mem: sqlite3OomFault(db); sqlite3VdbeError(p, "out of memory"); - rc = SQLITE_NOMEM; - goto vdbe_error_halt; - - /* Jump to here for any other kind of fatal error. The "rc" variable - ** should hold the error number. - */ -abort_due_to_error: - assert( p->zErrMsg==0 ); - if( db->mallocFailed ) rc = SQLITE_NOMEM; - if( rc!=SQLITE_IOERR_NOMEM ){ - sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc)); - } - goto vdbe_error_halt; + rc = SQLITE_NOMEM_BKPT; + goto abort_due_to_error; /* Jump to here if the sqlite3_interrupt() API sets the interrupt ** flag. */ abort_due_to_interrupt: - assert( db->u1.isInterrupted ); - rc = db->mallocFailed ? SQLITE_NOMEM : SQLITE_INTERRUPT; - p->rc = rc; - sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc)); - goto vdbe_error_halt; + assert( AtomicLoad(&db->u1.isInterrupted) ); + rc = SQLITE_INTERRUPT; + goto abort_due_to_error; } diff --git a/src/vdbe.h b/src/vdbe.h index 4c02f5844d..28df764bc6 100644 --- a/src/vdbe.h +++ b/src/vdbe.h @@ -15,8 +15,8 @@ ** or VDBE. The VDBE implements an abstract machine that runs a ** simple program to access and modify the underlying database. */ -#ifndef _SQLITE_VDBE_H_ -#define _SQLITE_VDBE_H_ +#ifndef SQLITE_VDBE_H +#define SQLITE_VDBE_H #include <stdio.h> /* @@ -30,8 +30,22 @@ typedef struct Vdbe Vdbe; ** The names of the following types declared in vdbeInt.h are required ** for the VdbeOp definition. */ -typedef struct Mem Mem; +typedef struct sqlite3_value Mem; typedef struct SubProgram SubProgram; +typedef struct SubrtnSig SubrtnSig; + +/* +** A signature for a reusable subroutine that materializes the RHS of +** an IN operator. +*/ +struct SubrtnSig { + int selId; /* SELECT-id for the SELECT statement on the RHS */ + u8 bComplete; /* True if fully coded and available for reusable */ + char *zAff; /* Affinity of the overall IN expression */ + int iTable; /* Ephemeral table generated by the subroutine */ + int iAddr; /* Subroutine entry address */ + int regReturn; /* Register used to hold return address */ +}; /* ** A single instruction of the virtual machine has an opcode @@ -41,8 +55,7 @@ typedef struct SubProgram SubProgram; struct VdbeOp { u8 opcode; /* What operation to perform */ signed char p4type; /* One of the P4_xxx constants for p4 */ - u8 opflags; /* Mask of the OPFLG_* flags in opcodes.h */ - u8 p5; /* Fifth parameter is an unsigned character */ + u16 p5; /* Fifth parameter is an unsigned 16-bit integer */ int p1; /* First operand */ int p2; /* Second parameter (often the jump destination) */ int p3; /* The third parameter */ @@ -58,22 +71,24 @@ struct VdbeOp { Mem *pMem; /* Used when p4type is P4_MEM */ VTable *pVtab; /* Used when p4type is P4_VTAB */ KeyInfo *pKeyInfo; /* Used when p4type is P4_KEYINFO */ - int *ai; /* Used when p4type is P4_INTARRAY */ + u32 *ai; /* Used when p4type is P4_INTARRAY */ SubProgram *pProgram; /* Used when p4type is P4_SUBPROGRAM */ + Table *pTab; /* Used when p4type is P4_TABLE */ + SubrtnSig *pSubrtnSig; /* Used when p4type is P4_SUBRTNSIG */ #ifdef SQLITE_ENABLE_CURSOR_HINTS Expr *pExpr; /* Used when p4type is P4_EXPR */ #endif - int (*xAdvance)(BtCursor *, int *); } p4; #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS char *zComment; /* Comment to improve readability */ #endif -#ifdef VDBE_PROFILE - u32 cnt; /* Number of times this instruction was executed */ - u64 cycles; /* Total time spent executing this instruction */ -#endif #ifdef SQLITE_VDBE_COVERAGE - int iSrcLine; /* Source-code line that generated this opcode */ + u32 iSrcLine; /* Source-code line that generated this opcode + ** with flags in the upper 8 bits */ +#endif +#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || defined(VDBE_PROFILE) + u64 nExec; + u64 nCycle; #endif }; typedef struct VdbeOp VdbeOp; @@ -87,7 +102,7 @@ struct SubProgram { int nOp; /* Elements in aOp[] */ int nMem; /* Number of memory cells required */ int nCsr; /* Number of cursors required */ - int nOnce; /* Number of OP_Once instructions */ + u8 *aOnce; /* Array of OP_Once flags */ void *token; /* id that may be used to recursive triggers */ SubProgram *pNext; /* Next sub-program already visited */ }; @@ -107,24 +122,27 @@ typedef struct VdbeOpList VdbeOpList; /* ** Allowed values of VdbeOp.p4type */ -#define P4_NOTUSED 0 /* The P4 parameter is not used */ -#define P4_DYNAMIC (-1) /* Pointer to a string obtained from sqliteMalloc() */ -#define P4_STATIC (-2) /* Pointer to a static string */ -#define P4_COLLSEQ (-4) /* P4 is a pointer to a CollSeq structure */ -#define P4_FUNCDEF (-5) /* P4 is a pointer to a FuncDef structure */ -#define P4_KEYINFO (-6) /* P4 is a pointer to a KeyInfo structure */ -#define P4_EXPR (-7) /* P4 is a pointer to an Expr tree */ -#define P4_MEM (-8) /* P4 is a pointer to a Mem* structure */ -#define P4_TRANSIENT 0 /* P4 is a pointer to a transient string */ -#define P4_VTAB (-10) /* P4 is a pointer to an sqlite3_vtab structure */ -#define P4_MPRINTF (-11) /* P4 is a string obtained from sqlite3_mprintf() */ -#define P4_REAL (-12) /* P4 is a 64-bit floating point value */ -#define P4_INT64 (-13) /* P4 is a 64-bit signed integer */ -#define P4_INT32 (-14) /* P4 is a 32-bit signed integer */ -#define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */ -#define P4_SUBPROGRAM (-18) /* P4 is a pointer to a SubProgram structure */ -#define P4_ADVANCE (-19) /* P4 is a pointer to BtreeNext() or BtreePrev() */ -#define P4_FUNCCTX (-20) /* P4 is a pointer to an sqlite3_context object */ +#define P4_NOTUSED 0 /* The P4 parameter is not used */ +#define P4_TRANSIENT 0 /* P4 is a pointer to a transient string */ +#define P4_STATIC (-1) /* Pointer to a static string */ +#define P4_COLLSEQ (-2) /* P4 is a pointer to a CollSeq structure */ +#define P4_INT32 (-3) /* P4 is a 32-bit signed integer */ +#define P4_SUBPROGRAM (-4) /* P4 is a pointer to a SubProgram structure */ +#define P4_TABLE (-5) /* P4 is a pointer to a Table structure */ +/* Above do not own any resources. Must free those below */ +#define P4_FREE_IF_LE (-6) +#define P4_DYNAMIC (-6) /* Pointer to memory from sqliteMalloc() */ +#define P4_FUNCDEF (-7) /* P4 is a pointer to a FuncDef structure */ +#define P4_KEYINFO (-8) /* P4 is a pointer to a KeyInfo structure */ +#define P4_EXPR (-9) /* P4 is a pointer to an Expr tree */ +#define P4_MEM (-10) /* P4 is a pointer to a Mem* structure */ +#define P4_VTAB (-11) /* P4 is a pointer to an sqlite3_vtab structure */ +#define P4_REAL (-12) /* P4 is a 64-bit floating point value */ +#define P4_INT64 (-13) /* P4 is a 64-bit signed integer */ +#define P4_INTARRAY (-14) /* P4 is a vector of 32-bit integers */ +#define P4_FUNCCTX (-15) /* P4 is a pointer to an sqlite3_context object */ +#define P4_TABLEREF (-16) /* Like P4_TABLE, but reference counted */ +#define P4_SUBRTNSIG (-17) /* P4 is a SubrtnSig pointer */ /* Error message codes for OP_Halt */ #define P5_ConstraintNotNull 1 @@ -152,12 +170,11 @@ typedef struct VdbeOpList VdbeOpList; #endif /* -** The following macro converts a relative address in the p2 field -** of a VdbeOp structure into a negative number so that -** sqlite3VdbeAddOpList() knows that the address is relative. Calling -** the macro again restores the address. +** The following macro converts a label returned by sqlite3VdbeMakeLabel() +** into an index into the Parse.aLabel[] array that contains the resolved +** address of that label. */ -#define ADDR(X) (-1-(X)) +#define ADDR(X) (~(X)) /* ** The makefile scans the vdbe.c source file and creates the "opcodes.h" @@ -165,11 +182,18 @@ typedef struct VdbeOpList VdbeOpList; */ #include "opcodes.h" +/* +** Additional non-public SQLITE_PREPARE_* flags +*/ +#define SQLITE_PREPARE_SAVESQL 0x80 /* Preserve SQL text */ +#define SQLITE_PREPARE_MASK 0x1f /* Mask of public flags */ + /* ** Prototypes for the VDBE interface. See comments on the implementation ** for a description of what each of these routines does. */ Vdbe *sqlite3VdbeCreate(Parse*); +Parse *sqlite3VdbeParser(Vdbe*); int sqlite3VdbeAddOp0(Vdbe*,int); int sqlite3VdbeAddOp1(Vdbe*,int,int); int sqlite3VdbeAddOp2(Vdbe*,int,int,int); @@ -180,30 +204,73 @@ int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int); int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int); int sqlite3VdbeAddOp4Dup8(Vdbe*,int,int,int,int,const u8*,int); int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int); +int sqlite3VdbeAddFunctionCall(Parse*,int,int,int,int,const FuncDef*,int); void sqlite3VdbeEndCoroutine(Vdbe*,int); #if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS) void sqlite3VdbeVerifyNoMallocRequired(Vdbe *p, int N); + void sqlite3VdbeVerifyNoResultRow(Vdbe *p); #else # define sqlite3VdbeVerifyNoMallocRequired(A,B) +# define sqlite3VdbeVerifyNoResultRow(A) +#endif +#if defined(SQLITE_DEBUG) + void sqlite3VdbeVerifyAbortable(Vdbe *p, int); + void sqlite3VdbeNoJumpsOutsideSubrtn(Vdbe*,int,int,int); +#else +# define sqlite3VdbeVerifyAbortable(A,B) +# define sqlite3VdbeNoJumpsOutsideSubrtn(A,B,C,D) +#endif +VdbeOp *sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp,int iLineno); +#ifndef SQLITE_OMIT_EXPLAIN + int sqlite3VdbeExplain(Parse*,u8,const char*,...); + void sqlite3VdbeExplainPop(Parse*); + int sqlite3VdbeExplainParent(Parse*); +# define ExplainQueryPlan(P) sqlite3VdbeExplain P +# ifdef SQLITE_ENABLE_STMT_SCANSTATUS +# define ExplainQueryPlan2(V,P) (V = sqlite3VdbeExplain P) +# else +# define ExplainQueryPlan2(V,P) ExplainQueryPlan(P) +# endif +# define ExplainQueryPlanPop(P) sqlite3VdbeExplainPop(P) +# define ExplainQueryPlanParent(P) sqlite3VdbeExplainParent(P) +#else +# define ExplainQueryPlan(P) +# define ExplainQueryPlan2(V,P) +# define ExplainQueryPlanPop(P) +# define ExplainQueryPlanParent(P) 0 +# define sqlite3ExplainBreakpoint(A,B) /*no-op*/ +#endif +#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_EXPLAIN) + void sqlite3ExplainBreakpoint(const char*,const char*); +#else +# define sqlite3ExplainBreakpoint(A,B) /*no-op*/ #endif -VdbeOp *sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp, int iLineno); -void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*); -void sqlite3VdbeChangeOpcode(Vdbe*, u32 addr, u8); -void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1); -void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2); -void sqlite3VdbeChangeP3(Vdbe*, u32 addr, int P3); -void sqlite3VdbeChangeP5(Vdbe*, u8 P5); +void sqlite3VdbeAddParseSchemaOp(Vdbe*, int, char*, u16); +void sqlite3VdbeChangeOpcode(Vdbe*, int addr, u8); +void sqlite3VdbeChangeP1(Vdbe*, int addr, int P1); +void sqlite3VdbeChangeP2(Vdbe*, int addr, int P2); +void sqlite3VdbeChangeP3(Vdbe*, int addr, int P3); +void sqlite3VdbeChangeP5(Vdbe*, u16 P5); +void sqlite3VdbeTypeofColumn(Vdbe*, int); void sqlite3VdbeJumpHere(Vdbe*, int addr); +void sqlite3VdbeJumpHereOrPopInst(Vdbe*, int addr); int sqlite3VdbeChangeToNoop(Vdbe*, int addr); int sqlite3VdbeDeletePriorOpcode(Vdbe*, u8 op); +#ifdef SQLITE_DEBUG + void sqlite3VdbeReleaseRegisters(Parse*,int addr, int n, u32 mask, int); +#else +# define sqlite3VdbeReleaseRegisters(P,A,N,M,F) +#endif void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N); +void sqlite3VdbeAppendP4(Vdbe*, void *pP4, int p4type); void sqlite3VdbeSetP4KeyInfo(Parse*, Index*); void sqlite3VdbeUsesBtree(Vdbe*, int); VdbeOp *sqlite3VdbeGetOp(Vdbe*, int); -int sqlite3VdbeMakeLabel(Vdbe*); +VdbeOp *sqlite3VdbeGetLastOp(Vdbe*); +int sqlite3VdbeMakeLabel(Parse*); void sqlite3VdbeRunOnlyOnce(Vdbe*); +void sqlite3VdbeReusable(Vdbe*); void sqlite3VdbeDelete(Vdbe*); -void sqlite3VdbeClearObject(sqlite3*,Vdbe*); void sqlite3VdbeMakeReady(Vdbe*,Parse*); int sqlite3VdbeFinalize(Vdbe*); void sqlite3VdbeResolveLabel(Vdbe*, int); @@ -218,7 +285,12 @@ void sqlite3VdbeSetNumCols(Vdbe*,int); int sqlite3VdbeSetColName(Vdbe*, int, int, const char *, void(*)(void*)); void sqlite3VdbeCountChanges(Vdbe*); sqlite3 *sqlite3VdbeDb(Vdbe*); -void sqlite3VdbeSetSql(Vdbe*, const char *z, int n, int); +u8 sqlite3VdbePrepareFlags(Vdbe*); +void sqlite3VdbeSetSql(Vdbe*, const char *z, int n, u8); +#ifdef SQLITE_ENABLE_NORMALIZE +void sqlite3VdbeAddDblquoteStr(sqlite3*,Vdbe*,const char*); +int sqlite3VdbeUsesDoubleQuotedString(Vdbe*,const char*); +#endif void sqlite3VdbeSwap(Vdbe*,Vdbe*); VdbeOp *sqlite3VdbeTakeOpArray(Vdbe*, int*, int*); sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe*, int, u8); @@ -227,21 +299,33 @@ void sqlite3VdbeSetVarmask(Vdbe*, int); char *sqlite3VdbeExpandSql(Vdbe*, const char*); #endif int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*); +int sqlite3BlobCompare(const Mem*, const Mem*); +#ifdef SQLITE_ENABLE_PERCENTILE + const char *sqlite3VdbeFuncName(const sqlite3_context*); +#endif -void sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,UnpackedRecord*); +void sqlite3VdbeRecordUnpack(int,const void*,UnpackedRecord*); int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*); int sqlite3VdbeRecordCompareWithSkip(int, const void *, UnpackedRecord *, int); -UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(KeyInfo *, char *, int, char **); +UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(KeyInfo*); typedef int (*RecordCompare)(int,const void*,UnpackedRecord*); RecordCompare sqlite3VdbeFindCompare(UnpackedRecord*); -#ifndef SQLITE_OMIT_TRIGGER void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *); +int sqlite3VdbeHasSubProgram(Vdbe*); + +void sqlite3MemSetArrayInt64(sqlite3_value *aMem, int iIdx, i64 val); + +#ifndef SQLITE_OMIT_DATETIME_FUNCS +int sqlite3NotPureFunc(sqlite3_context*); +#endif +#ifdef SQLITE_ENABLE_BYTECODE_VTAB +int sqlite3VdbeBytecodeVtabInit(sqlite3*); #endif -/* Use SQLITE_ENABLE_COMMENTS to enable generation of extra comments on -** each VDBE opcode. +/* Use SQLITE_ENABLE_EXPLAIN_COMMENTS to enable generation of extra +** comments on each VDBE opcode. ** ** Use the SQLITE_ENABLE_MODULE_COMMENTS macro to see some extra no-op ** comments in VDBE programs that show key decision points in the code @@ -267,7 +351,7 @@ void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *); ** The VdbeCoverage macros are used to set a coverage testing point ** for VDBE branch instructions. The coverage testing points are line ** numbers in the sqlite3.c source file. VDBE branch coverage testing -** only works with an amalagmation build. That's ok since a VDBE branch +** only works with an amalgamation build. That's ok since a VDBE branch ** coverage build designed for testing the test suite only. No application ** should ever ship with VDBE branch coverage measuring turned on. ** @@ -280,30 +364,71 @@ void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *); ** ** VdbeCoverageNeverTaken(v) // Previous branch is never taken ** +** VdbeCoverageNeverNull(v) // Previous three-way branch is only +** // taken on the first two ways. The +** // NULL option is not possible +** +** VdbeCoverageEqNe(v) // Previous OP_Jump is only interested +** // in distinguishing equal and not-equal. +** ** Every VDBE branch operation must be tagged with one of the macros above. ** If not, then when "make test" is run with -DSQLITE_VDBE_COVERAGE and ** -DSQLITE_DEBUG then an ALWAYS() will fail in the vdbeTakeBranch() ** routine in vdbe.c, alerting the developer to the missed tag. +** +** During testing, the test application will invoke +** sqlite3_test_control(SQLITE_TESTCTRL_VDBE_COVERAGE,...) to set a callback +** routine that is invoked as each bytecode branch is taken. The callback +** contains the sqlite3.c source line number of the VdbeCoverage macro and +** flags to indicate whether or not the branch was taken. The test application +** is responsible for keeping track of this and reporting byte-code branches +** that are never taken. +** +** See the VdbeBranchTaken() macro and vdbeTakeBranch() function in the +** vdbe.c source file for additional information. */ #ifdef SQLITE_VDBE_COVERAGE void sqlite3VdbeSetLineNumber(Vdbe*,int); # define VdbeCoverage(v) sqlite3VdbeSetLineNumber(v,__LINE__) # define VdbeCoverageIf(v,x) if(x)sqlite3VdbeSetLineNumber(v,__LINE__) -# define VdbeCoverageAlwaysTaken(v) sqlite3VdbeSetLineNumber(v,2); -# define VdbeCoverageNeverTaken(v) sqlite3VdbeSetLineNumber(v,1); +# define VdbeCoverageAlwaysTaken(v) \ + sqlite3VdbeSetLineNumber(v,__LINE__|0x5000000); +# define VdbeCoverageNeverTaken(v) \ + sqlite3VdbeSetLineNumber(v,__LINE__|0x6000000); +# define VdbeCoverageNeverNull(v) \ + sqlite3VdbeSetLineNumber(v,__LINE__|0x4000000); +# define VdbeCoverageNeverNullIf(v,x) \ + if(x)sqlite3VdbeSetLineNumber(v,__LINE__|0x4000000); +# define VdbeCoverageEqNe(v) \ + sqlite3VdbeSetLineNumber(v,__LINE__|0x8000000); # define VDBE_OFFSET_LINENO(x) (__LINE__+x) #else # define VdbeCoverage(v) # define VdbeCoverageIf(v,x) # define VdbeCoverageAlwaysTaken(v) # define VdbeCoverageNeverTaken(v) +# define VdbeCoverageNeverNull(v) +# define VdbeCoverageNeverNullIf(v,x) +# define VdbeCoverageEqNe(v) # define VDBE_OFFSET_LINENO(x) 0 #endif #ifdef SQLITE_ENABLE_STMT_SCANSTATUS void sqlite3VdbeScanStatus(Vdbe*, int, int, int, LogEst, const char*); +void sqlite3VdbeScanStatusRange(Vdbe*, int, int, int); +void sqlite3VdbeScanStatusCounters(Vdbe*, int, int, int); #else -# define sqlite3VdbeScanStatus(a,b,c,d,e) +# define sqlite3VdbeScanStatus(a,b,c,d,e,f) +# define sqlite3VdbeScanStatusRange(a,b,c,d) +# define sqlite3VdbeScanStatusCounters(a,b,c,d) +#endif + +#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) +void sqlite3VdbePrintOp(FILE*, int, VdbeOp*); #endif +#if defined(SQLITE_ENABLE_CURSOR_HINTS) && defined(SQLITE_DEBUG) +int sqlite3CursorRangeHintExprCheck(Walker *pWalker, Expr *pExpr); #endif + +#endif /* SQLITE_VDBE_H */ diff --git a/src/vdbeInt.h b/src/vdbeInt.h index 839c2039be..8b68c339af 100644 --- a/src/vdbeInt.h +++ b/src/vdbeInt.h @@ -15,8 +15,8 @@ ** 6000 lines long) it was split up into several smaller files and ** this header information was factored out. */ -#ifndef _VDBEINT_H_ -#define _VDBEINT_H_ +#ifndef SQLITE_VDBEINT_H +#define SQLITE_VDBEINT_H /* ** The maximum number of times that a statement will try to reparse @@ -31,7 +31,8 @@ ** "explain" P4 display logic is enabled. */ #if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \ - || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG) + || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG) \ + || defined(SQLITE_ENABLE_BYTECODE_VTAB) # define VDBE_DISPLAY_P4 1 #else # define VDBE_DISPLAY_P4 0 @@ -52,12 +53,12 @@ typedef unsigned Bool; /* Opaque type used by code in vdbesort.c */ typedef struct VdbeSorter VdbeSorter; -/* Opaque type used by the explainer */ -typedef struct Explain Explain; - /* Elements of the linked list at Vdbe.pAuxData */ typedef struct AuxData AuxData; +/* A cache of large TEXT or BLOB values in a VdbeCursor */ +typedef struct VdbeTxtBlbCache VdbeTxtBlbCache; + /* Types of VDBE cursors */ #define CURTYPE_BTREE 0 #define CURTYPE_SORTER 1 @@ -76,64 +77,104 @@ typedef struct AuxData AuxData; */ typedef struct VdbeCursor VdbeCursor; struct VdbeCursor { - u8 eCurType; /* One of the CURTYPE_* values above */ - i8 iDb; /* Index of cursor database in db->aDb[] (or -1) */ - u8 nullRow; /* True if pointing to a row with no data */ - u8 deferredMoveto; /* A call to sqlite3BtreeMoveto() is needed */ - u8 isTable; /* True for rowid tables. False for indexes */ + u8 eCurType; /* One of the CURTYPE_* values above */ + i8 iDb; /* Index of cursor database in db->aDb[] */ + u8 nullRow; /* True if pointing to a row with no data */ + u8 deferredMoveto; /* A call to sqlite3BtreeMoveto() is needed */ + u8 isTable; /* True for rowid tables. False for indexes */ #ifdef SQLITE_DEBUG - u8 seekOp; /* Most recent seek operation on this cursor */ - u8 wrFlag; /* The wrFlag argument to sqlite3BtreeCursor() */ + u8 seekOp; /* Most recent seek operation on this cursor */ + u8 wrFlag; /* The wrFlag argument to sqlite3BtreeCursor() */ #endif - Bool isEphemeral:1; /* True for an ephemeral table */ - Bool useRandomRowid:1;/* Generate new record numbers semi-randomly */ - Bool isOrdered:1; /* True if the underlying table is BTREE_UNORDERED */ - Pgno pgnoRoot; /* Root page of the open btree cursor */ - i16 nField; /* Number of fields in the header */ - u16 nHdrParsed; /* Number of header fields parsed so far */ + Bool isEphemeral:1; /* True for an ephemeral table */ + Bool useRandomRowid:1; /* Generate new record numbers semi-randomly */ + Bool isOrdered:1; /* True if the table is not BTREE_UNORDERED */ + Bool noReuse:1; /* OpenEphemeral may not reuse this cursor */ + Bool colCache:1; /* pCache pointer is initialized and non-NULL */ + u16 seekHit; /* See the OP_SeekHit and OP_IfNoHope opcodes */ + union { /* pBtx for isEphermeral. pAltMap otherwise */ + Btree *pBtx; /* Separate file holding temporary table */ + u32 *aAltMap; /* Mapping from table to index column numbers */ + } ub; + i64 seqCount; /* Sequence counter */ + + /* Cached OP_Column parse information is only valid if cacheStatus matches + ** Vdbe.cacheCtr. Vdbe.cacheCtr will never take on the value of + ** CACHE_STALE (0) and so setting cacheStatus=CACHE_STALE guarantees that + ** the cache is out of date. */ + u32 cacheStatus; /* Cache is valid if this matches Vdbe.cacheCtr */ + int seekResult; /* Result of previous sqlite3BtreeMoveto() or 0 + ** if there have been no prior seeks on the cursor. */ + /* seekResult does not distinguish between "no seeks have ever occurred + ** on this cursor" and "the most recent seek was an exact match". + ** For CURTYPE_PSEUDO, seekResult is the register holding the record */ + + /* When a new VdbeCursor is allocated, only the fields above are zeroed. + ** The fields that follow are uninitialized, and must be individually + ** initialized prior to first use. */ + VdbeCursor *pAltCursor; /* Associated index cursor from which to read */ union { - BtCursor *pCursor; /* CURTYPE_BTREE. Btree cursor */ - sqlite3_vtab_cursor *pVCur; /* CURTYPE_VTAB. Vtab cursor */ - int pseudoTableReg; /* CURTYPE_PSEUDO. Reg holding content. */ - VdbeSorter *pSorter; /* CURTYPE_SORTER. Sorter object */ + BtCursor *pCursor; /* CURTYPE_BTREE or _PSEUDO. Btree cursor */ + sqlite3_vtab_cursor *pVCur; /* CURTYPE_VTAB. Vtab cursor */ + VdbeSorter *pSorter; /* CURTYPE_SORTER. Sorter object */ } uc; - Btree *pBt; /* Separate file holding temporary table */ - KeyInfo *pKeyInfo; /* Info about index keys needed by index cursors */ - int seekResult; /* Result of previous sqlite3BtreeMoveto() */ - i64 seqCount; /* Sequence counter */ - i64 movetoTarget; /* Argument to the deferred sqlite3BtreeMoveto() */ - VdbeCursor *pAltCursor; /* Associated index cursor from which to read */ - int *aAltMap; /* Mapping from table to index column numbers */ + KeyInfo *pKeyInfo; /* Info about index keys needed by index cursors */ + u32 iHdrOffset; /* Offset to next unparsed byte of the header */ + Pgno pgnoRoot; /* Root page of the open btree cursor */ + i16 nField; /* Number of fields in the header */ + u16 nHdrParsed; /* Number of header fields parsed so far */ + i64 movetoTarget; /* Argument to the deferred sqlite3BtreeMoveto() */ + u32 *aOffset; /* Pointer to aType[nField] */ + const u8 *aRow; /* Data for the current row, if all on one page */ + u32 payloadSize; /* Total number of bytes in the record */ + u32 szRow; /* Byte available in aRow */ #ifdef SQLITE_ENABLE_COLUMN_USED_MASK - u64 maskUsed; /* Mask of columns used by this cursor */ + u64 maskUsed; /* Mask of columns used by this cursor */ #endif + VdbeTxtBlbCache *pCache; /* Cache of large TEXT or BLOB values */ - /* Cached information about the header for the data record that the - ** cursor is currently pointing to. Only valid if cacheStatus matches - ** Vdbe.cacheCtr. Vdbe.cacheCtr will never take on the value of - ** CACHE_STALE and so setting cacheStatus=CACHE_STALE guarantees that - ** the cache is out of date. - ** - ** aRow might point to (ephemeral) data for the current row, or it might - ** be NULL. - */ - u32 cacheStatus; /* Cache is valid if this matches Vdbe.cacheCtr */ - u32 payloadSize; /* Total number of bytes in the record */ - u32 szRow; /* Byte available in aRow */ - u32 iHdrOffset; /* Offset to next unparsed byte of the header */ - const u8 *aRow; /* Data for the current row, if all on one page */ - u32 *aOffset; /* Pointer to aType[nField] */ - u32 aType[1]; /* Type values for all entries in the record */ - /* 2*nField extra array elements allocated for aType[], beyond the one - ** static element declared in the structure. nField total array slots for - ** aType[] and nField+1 array slots for aOffset[] */ + /* Space is allocated for aType to hold at least 2*nField+1 entries: + ** nField slots for aType[] and nField+1 array slots for aOffset[] */ + u32 aType[FLEXARRAY]; /* Type values record decode. MUST BE LAST */ +}; + +/* +** The size (in bytes) of a VdbeCursor object that has an nField value of N +** or less. The value of SZ_VDBECURSOR(n) is guaranteed to be a multiple +** of 8. +*/ +#define SZ_VDBECURSOR(N) \ + (ROUND8(offsetof(VdbeCursor,aType)) + ((N)+1)*sizeof(u64)) + +/* Return true if P is a null-only cursor +*/ +#define IsNullCursor(P) \ + ((P)->eCurType==CURTYPE_PSEUDO && (P)->nullRow && (P)->seekResult==0) + +/* +** A value for VdbeCursor.cacheStatus that means the cache is always invalid. +*/ +#define CACHE_STALE 0 + +/* +** Large TEXT or BLOB values can be slow to load, so we want to avoid +** loading them more than once. For that reason, large TEXT and BLOB values +** can be stored in a cache defined by this object, and attached to the +** VdbeCursor using the pCache field. +*/ +struct VdbeTxtBlbCache { + char *pCValue; /* A RCStr buffer to hold the value */ + i64 iOffset; /* File offset of the row being cached */ + int iCol; /* Column for which the cache is valid */ + u32 cacheStatus; /* Vdbe.cacheCtr value */ + u32 colCacheCtr; /* Column cache counter */ }; /* ** When a sub-program is executed (OP_Program), a structure of this type ** is allocated to store the current value of the program counter, as ** well as the current memory cell array and various other frame specific -** values stored in the Vdbe struct. When the sub-program is finished, +** values stored in the Vdbe struct. When the sub-program is finished, ** these values are copied back to the Vdbe from the VdbeFrame structure, ** restoring the state of the VM to as it was before the sub-program ** began executing. @@ -155,58 +196,62 @@ struct VdbeFrame { Vdbe *v; /* VM this frame belongs to */ VdbeFrame *pParent; /* Parent of this frame, or NULL if parent is main */ Op *aOp; /* Program instructions for parent frame */ - i64 *anExec; /* Event counters from parent frame */ Mem *aMem; /* Array of memory cells for parent frame */ - u8 *aOnceFlag; /* Array of OP_Once flags for parent frame */ VdbeCursor **apCsr; /* Array of Vdbe cursors for parent frame */ + u8 *aOnce; /* Bitmask used by OP_Once */ void *token; /* Copy of SubProgram.token */ i64 lastRowid; /* Last insert rowid (sqlite3.lastRowid) */ + AuxData *pAuxData; /* Linked list of auxdata allocations */ +#if SQLITE_DEBUG + u32 iFrameMagic; /* magic number for sanity checking */ +#endif int nCursor; /* Number of entries in apCsr */ int pc; /* Program Counter in parent (calling) frame */ int nOp; /* Size of aOp array */ int nMem; /* Number of entries in aMem */ - int nOnceFlag; /* Number of entries in aOnceFlag */ int nChildMem; /* Number of memory cells for child frame */ int nChildCsr; /* Number of cursors for child frame */ - int nChange; /* Statement changes (Vdbe.nChange) */ - int nDbChange; /* Value of db->nChange */ + i64 nChange; /* Statement changes (Vdbe.nChange) */ + i64 nDbChange; /* Value of db->nChange */ }; -#define VdbeFrameMem(p) ((Mem *)&((u8 *)p)[ROUND8(sizeof(VdbeFrame))]) +/* Magic number for sanity checking on VdbeFrame objects */ +#define SQLITE_FRAME_MAGIC 0x879fb71e /* -** A value for VdbeCursor.cacheValid that means the cache is always invalid. +** Return a pointer to the array of registers allocated for use +** by a VdbeFrame. */ -#define CACHE_STALE 0 +#define VdbeFrameMem(p) ((Mem *)&((u8 *)p)[ROUND8(sizeof(VdbeFrame))]) /* ** Internally, the vdbe manipulates nearly all SQL values as Mem ** structures. Each Mem struct may cache multiple representations (string, ** integer etc.) of the same value. */ -struct Mem { +struct sqlite3_value { union MemValue { double r; /* Real value used when MEM_Real is set in flags */ i64 i; /* Integer value used when MEM_Int is set in flags */ - int nZero; /* Used when bit MEM_Zero is set in flags */ + int nZero; /* Extra zero bytes when MEM_Zero and MEM_Blob set */ + const char *zPType; /* Pointer type when MEM_Term|MEM_Subtype|MEM_Null */ FuncDef *pDef; /* Used only when flags==MEM_Agg */ - RowSet *pRowSet; /* Used only when flags==MEM_RowSet */ - VdbeFrame *pFrame; /* Used when flags==MEM_Frame */ } u; + char *z; /* String or BLOB value */ + int n; /* Number of characters in string value, excluding '\0' */ u16 flags; /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */ u8 enc; /* SQLITE_UTF8, SQLITE_UTF16BE, SQLITE_UTF16LE */ u8 eSubtype; /* Subtype for this value */ - int n; /* Number of characters in string value, excluding '\0' */ - char *z; /* String or BLOB value */ /* ShallowCopy only needs to copy the information above */ - char *zMalloc; /* Space to hold MEM_Str or MEM_Blob if szMalloc>0 */ + sqlite3 *db; /* The associated database connection */ int szMalloc; /* Size of the zMalloc allocation */ u32 uTemp; /* Transient storage for serial_type in OP_MakeRecord */ - sqlite3 *db; /* The associated database connection */ + char *zMalloc; /* Space to hold MEM_Str or MEM_Blob if szMalloc>0 */ void (*xDel)(void*);/* Destructor for Mem.z - only valid if MEM_Dyn */ #ifdef SQLITE_DEBUG Mem *pScopyFrom; /* This Mem is a shallow copy of pScopyFrom */ - void *pFiller; /* So that sizeof(Mem) is a multiple of 8 */ + u16 mScopyFlags; /* flags value immediately after the shallow copy */ + u8 bScopy; /* The pScopyFrom of some other Mem *might* point here */ #endif }; @@ -214,55 +259,85 @@ struct Mem { ** Size of struct Mem not including the Mem.zMalloc member or anything that ** follows. */ -#define MEMCELLSIZE offsetof(Mem,zMalloc) +#define MEMCELLSIZE offsetof(Mem,db) -/* One or more of the following flags are set to indicate the validOK +/* One or more of the following flags are set to indicate the ** representations of the value stored in the Mem struct. ** +** * MEM_Null An SQL NULL value +** +** * MEM_Null|MEM_Zero An SQL NULL with the virtual table +** UPDATE no-change flag set +** +** * MEM_Null|MEM_Term| An SQL NULL, but also contains a +** MEM_Subtype pointer accessible using +** sqlite3_value_pointer(). +** +** * MEM_Null|MEM_Cleared Special SQL NULL that compares non-equal +** to other NULLs even using the IS operator. +** +** * MEM_Str A string, stored in Mem.z with +** length Mem.n. Zero-terminated if +** MEM_Term is set. This flag is +** incompatible with MEM_Blob and +** MEM_Null, but can appear with MEM_Int, +** MEM_Real, and MEM_IntReal. +** +** * MEM_Blob A blob, stored in Mem.z length Mem.n. +** Incompatible with MEM_Str, MEM_Null, +** MEM_Int, MEM_Real, and MEM_IntReal. +** +** * MEM_Blob|MEM_Zero A blob in Mem.z of length Mem.n plus +** Mem.u.nZero extra 0x00 bytes at the end. +** +** * MEM_Int Integer stored in Mem.u.i. +** +** * MEM_Real Real stored in Mem.u.r. +** +** * MEM_IntReal Real stored as an integer in Mem.u.i. +** ** If the MEM_Null flag is set, then the value is an SQL NULL value. -** No other flags may be set in this case. +** For a pointer type created using sqlite3_bind_pointer() or +** sqlite3_result_pointer() the MEM_Term and MEM_Subtype flags are also set. ** ** If the MEM_Str flag is set then Mem.z points at a string representation. ** Usually this is encoded in the same unicode encoding as the main ** database (see below for exceptions). If the MEM_Term flag is also -** set, then the string is nul terminated. The MEM_Int and MEM_Real +** set, then the string is nul terminated. The MEM_Int and MEM_Real ** flags may coexist with the MEM_Str flag. */ -#define MEM_Null 0x0001 /* Value is NULL */ +#define MEM_Undefined 0x0000 /* Value is undefined */ +#define MEM_Null 0x0001 /* Value is NULL (or a pointer) */ #define MEM_Str 0x0002 /* Value is a string */ #define MEM_Int 0x0004 /* Value is an integer */ #define MEM_Real 0x0008 /* Value is a real number */ #define MEM_Blob 0x0010 /* Value is a BLOB */ -#define MEM_AffMask 0x001f /* Mask of affinity bits */ -#define MEM_RowSet 0x0020 /* Value is a RowSet object */ -#define MEM_Frame 0x0040 /* Value is a VdbeFrame object */ -#define MEM_Undefined 0x0080 /* Value is undefined */ +#define MEM_IntReal 0x0020 /* MEM_Int that stringifies like MEM_Real */ +#define MEM_AffMask 0x003f /* Mask of affinity bits */ + +/* Extra bits that modify the meanings of the core datatypes above +*/ +#define MEM_FromBind 0x0040 /* Value originates from sqlite3_bind() */ + /* 0x0080 // Available */ #define MEM_Cleared 0x0100 /* NULL set by OP_Null, not from data */ -#define MEM_TypeMask 0x81ff /* Mask of type bits */ - - -/* Whenever Mem contains a valid string or blob representation, one of -** the following flags must be set to determine the memory management -** policy for Mem.z. The MEM_Term flag tells us whether or not the -** string is \000 or \u0000 terminated -*/ -#define MEM_Term 0x0200 /* String rep is nul terminated */ -#define MEM_Dyn 0x0400 /* Need to call Mem.xDel() on Mem.z */ -#define MEM_Static 0x0800 /* Mem.z points to a static string */ -#define MEM_Ephem 0x1000 /* Mem.z points to an ephemeral string */ -#define MEM_Agg 0x2000 /* Mem.z points to an agg function context */ -#define MEM_Zero 0x4000 /* Mem.i contains count of 0s appended to blob */ -#define MEM_Subtype 0x8000 /* Mem.eSubtype is valid */ -#ifdef SQLITE_OMIT_INCRBLOB - #undef MEM_Zero - #define MEM_Zero 0x0000 -#endif +#define MEM_Term 0x0200 /* String in Mem.z is zero terminated */ +#define MEM_Zero 0x0400 /* Mem.i contains count of 0s appended to blob */ +#define MEM_Subtype 0x0800 /* Mem.eSubtype is valid */ +#define MEM_TypeMask 0x0dbf /* Mask of type bits */ + +/* Bits that determine the storage for Mem.z for a string or blob or +** aggregate accumulator. +*/ +#define MEM_Dyn 0x1000 /* Need to call Mem.xDel() on Mem.z */ +#define MEM_Static 0x2000 /* Mem.z points to a static string */ +#define MEM_Ephem 0x4000 /* Mem.z points to an ephemeral string */ +#define MEM_Agg 0x8000 /* Mem.z points to an agg function context */ /* Return TRUE if Mem X contains dynamically allocated content - anything ** that needs to be deallocated to avoid a leak. */ #define VdbeMemDynamic(X) \ - (((X)->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame))!=0) + (((X)->flags&(MEM_Agg|MEM_Dyn))!=0) /* ** Clear any existing type flags from a Mem and replace them with f @@ -271,26 +346,37 @@ struct Mem { ((p)->flags = ((p)->flags&~(MEM_TypeMask|MEM_Zero))|f) /* -** Return true if a memory cell is not marked as invalid. This macro +** True if Mem X is a NULL-nochng type. +*/ +#define MemNullNochng(X) \ + (((X)->flags&MEM_TypeMask)==(MEM_Null|MEM_Zero) \ + && (X)->n==0 && (X)->u.nZero==0) + +/* +** Return true if a memory cell has been initialized and is valid. ** is for use inside assert() statements only. +** +** A Memory cell is initialized if at least one of the +** MEM_Null, MEM_Str, MEM_Int, MEM_Real, MEM_Blob, or MEM_IntReal bits +** is set. It is "undefined" if all those bits are zero. */ #ifdef SQLITE_DEBUG -#define memIsValid(M) ((M)->flags & MEM_Undefined)==0 +#define memIsValid(M) ((M)->flags & MEM_AffMask)!=0 #endif /* -** Each auxiliary data pointer stored by a user defined function +** Each auxiliary data pointer stored by a user defined function ** implementation calling sqlite3_set_auxdata() is stored in an instance ** of this structure. All such structures associated with a single VM ** are stored in a linked list headed at Vdbe.pAuxData. All are destroyed ** when the VM is halted (if not before). */ struct AuxData { - int iOp; /* Instruction number of OP_Function opcode */ - int iArg; /* Index of function argument. */ + int iAuxOp; /* Instruction number of OP_Function opcode */ + int iAuxArg; /* Index of function argument. */ void *pAux; /* Aux data pointer */ - void (*xDelete)(void *); /* Destructor for the aux data */ - AuxData *pNext; /* Next element in list */ + void (*xDeleteAux)(void*); /* Destructor for the aux data */ + AuxData *pNextAux; /* Next element in list */ }; /* @@ -313,32 +399,35 @@ struct sqlite3_context { Vdbe *pVdbe; /* The VM that owns this context */ int iOp; /* Instruction number of OP_Function */ int isError; /* Error code returned by the function. */ + u8 enc; /* Encoding to use for results */ u8 skipFlag; /* Skip accumulator loading if true */ - u8 fErrorOrAux; /* isError!=0 or pVdbe->pAuxData modified */ - u8 argc; /* Number of arguments */ - sqlite3_value *argv[1]; /* Argument set */ + u16 argc; /* Number of arguments */ + sqlite3_value *argv[FLEXARRAY]; /* Argument set */ }; /* -** An Explain object accumulates indented output which is helpful -** in describing recursive data structures. -*/ -struct Explain { - Vdbe *pVdbe; /* Attach the explanation to this Vdbe */ - StrAccum str; /* The string being accumulated */ - int nIndent; /* Number of elements in aIndent */ - u16 aIndent[100]; /* Levels of indentation */ - char zBase[100]; /* Initial space */ -}; - -/* A bitfield type for use inside of structures. Always follow with :N where -** N is the number of bits. +** The size (in bytes) of an sqlite3_context object that holds N +** argv[] arguments. */ -typedef unsigned bft; /* Bit Field Type */ +#define SZ_CONTEXT(N) \ + (offsetof(sqlite3_context,argv)+(N)*sizeof(sqlite3_value*)) + +/* The ScanStatus object holds a single value for the +** sqlite3_stmt_scanstatus() interface. +** +** aAddrRange[]: +** This array is used by ScanStatus elements associated with EQP +** notes that make an SQLITE_SCANSTAT_NCYCLE value available. It is +** an array of up to 3 ranges of VM addresses for which the Vdbe.anCycle[] +** values should be summed to calculate the NCYCLE value. Each pair of +** integer addresses is a start and end address (both inclusive) for a range +** instructions. A start value of 0 indicates an empty range. +*/ typedef struct ScanStatus ScanStatus; struct ScanStatus { int addrExplain; /* OP_Explain for loop */ + int aAddrRange[6]; int addrLoop; /* Address of "loops" counter */ int addrVisit; /* Address of "rows visited" counter */ int iSelectID; /* The "Select-ID" for this loop */ @@ -346,6 +435,19 @@ struct ScanStatus { char *zName; /* Name of table or index */ }; +/* The DblquoteStr object holds the text of a double-quoted +** string for a prepared statement. A linked list of these objects +** is constructed during statement parsing and is held on Vdbe.pDblStr. +** When computing a normalized SQL statement for an SQL statement, that +** list is consulted for each double-quoted identifier to see if the +** identifier should really be a string literal. +*/ +typedef struct DblquoteStr DblquoteStr; +struct DblquoteStr { + DblquoteStr *pNextStr; /* Next string literal in the list */ + char z[8]; /* Dequoted value for the string */ +}; + /* ** An instance of the virtual machine. This structure contains the complete ** state of the virtual machine. @@ -355,101 +457,171 @@ struct ScanStatus { */ struct Vdbe { sqlite3 *db; /* The database connection that owns this statement */ - Op *aOp; /* Space to hold the virtual machine's program */ - Mem *aMem; /* The memory locations */ - Mem **apArg; /* Arguments to currently executing user function */ - Mem *aColName; /* Column names to return */ - Mem *pResultSet; /* Pointer to an array of results */ + Vdbe **ppVPrev,*pVNext; /* Linked list of VDBEs with the same Vdbe.db */ Parse *pParse; /* Parsing context used to create this Vdbe */ + ynVar nVar; /* Number of entries in aVar[] */ int nMem; /* Number of memory locations currently allocated */ - int nOp; /* Number of instructions in the program */ int nCursor; /* Number of slots in apCsr[] */ - u32 magic; /* Magic number for sanity checking */ - char *zErrMsg; /* Error message written here */ - Vdbe *pPrev,*pNext; /* Linked list of VDBEs with the same Vdbe.db */ - VdbeCursor **apCsr; /* One element of this array for each open cursor */ - Mem *aVar; /* Values for the OP_Variable opcode. */ - char **azVar; /* Name of variables */ - ynVar nVar; /* Number of entries in aVar[] */ - ynVar nzVar; /* Number of entries in azVar[] */ u32 cacheCtr; /* VdbeCursor row cache generation counter */ int pc; /* The program counter */ int rc; /* Value to return */ + i64 nChange; /* Number of db changes made since last reset */ + int iStatement; /* Statement number (or 0 if has no opened stmt) */ + i64 iCurrentTime; /* Value of julianday('now') for this statement */ + i64 nFkConstraint; /* Number of imm. FK constraints this VM */ + i64 nStmtDefCons; /* Number of def. constraints when stmt started */ + i64 nStmtDefImmCons; /* Number of def. imm constraints when stmt started */ + Mem *aMem; /* The memory locations */ + Mem **apArg; /* Arguments xUpdate and xFilter vtab methods */ + VdbeCursor **apCsr; /* One element of this array for each open cursor */ + Mem *aVar; /* Values for the OP_Variable opcode. */ + + /* When allocating a new Vdbe object, all of the fields below should be + ** initialized to zero or NULL */ + + Op *aOp; /* Space to hold the virtual machine's program */ + int nOp; /* Number of instructions in the program */ + int nOpAlloc; /* Slots allocated for aOp[] */ + Mem *aColName; /* Column names to return */ + Mem *pResultRow; /* Current output row */ + char *zErrMsg; /* Error message written here */ + VList *pVList; /* Name of variables */ +#ifndef SQLITE_OMIT_TRACE + i64 startTime; /* Time when query started - used for profiling */ +#endif #ifdef SQLITE_DEBUG int rcApp; /* errcode set by sqlite3_result_error_code() */ + u32 nWrite; /* Number of write operations that have occurred */ + int napArg; /* Size of the apArg[] array */ #endif u16 nResColumn; /* Number of columns in one row of the result set */ + u16 nResAlloc; /* Column slots allocated to aColName[] */ u8 errorAction; /* Recovery action to do in case of an error */ u8 minWriteFileFormat; /* Minimum file format for writable database files */ - bft explain:2; /* True if EXPLAIN present on SQL command */ + u8 prepFlags; /* SQLITE_PREPARE_* flags */ + u8 eVdbeState; /* On of the VDBE_*_STATE values */ + bft expired:2; /* 1: recompile VM immediately 2: when convenient */ + bft explain:2; /* 0: normal, 1: EXPLAIN, 2: EXPLAIN QUERY PLAN */ bft changeCntOn:1; /* True to update the change-counter */ - bft expired:1; /* True if the VM needs to be recompiled */ - bft runOnlyOnce:1; /* Automatically expire on reset */ bft usesStmtJournal:1; /* True if uses a statement journal */ bft readOnly:1; /* True for statements that do not write */ bft bIsReader:1; /* True for statements that read */ - bft isPrepareV2:1; /* True if prepared with prepare_v2() */ - bft doingRerun:1; /* True if rerunning after an auto-reprepare */ - int nChange; /* Number of db changes made since last reset */ + bft haveEqpOps:1; /* Bytecode supports EXPLAIN QUERY PLAN */ yDbMask btreeMask; /* Bitmask of db->aDb[] entries referenced */ yDbMask lockMask; /* Subset of btreeMask that requires a lock */ - int iStatement; /* Statement number (or 0 if has not opened stmt) */ - u32 aCounter[5]; /* Counters used by sqlite3_stmt_status() */ -#ifndef SQLITE_OMIT_TRACE - i64 startTime; /* Time when query started - used for profiling */ -#endif - i64 iCurrentTime; /* Value of julianday('now') for this statement */ - i64 nFkConstraint; /* Number of imm. FK constraints this VM */ - i64 nStmtDefCons; /* Number of def. constraints when stmt started */ - i64 nStmtDefImmCons; /* Number of def. imm constraints when stmt started */ + u32 aCounter[9]; /* Counters used by sqlite3_stmt_status() */ char *zSql; /* Text of the SQL statement that generated this */ +#ifdef SQLITE_ENABLE_NORMALIZE + char *zNormSql; /* Normalization of the associated SQL statement */ + DblquoteStr *pDblStr; /* List of double-quoted string literals */ +#endif void *pFree; /* Free this when deleting the vdbe */ VdbeFrame *pFrame; /* Parent frame */ VdbeFrame *pDelFrame; /* List of frame objects to free on VM reset */ int nFrame; /* Number of frames in pFrame list */ u32 expmask; /* Binding to these vars invalidates VM */ SubProgram *pProgram; /* Linked list of all sub-programs used by VM */ - int nOnceFlag; /* Size of array aOnceFlag[] */ - u8 *aOnceFlag; /* Flags for OP_Once */ AuxData *pAuxData; /* Linked list of auxdata allocations */ #ifdef SQLITE_ENABLE_STMT_SCANSTATUS - i64 *anExec; /* Number of times each op has been executed */ int nScan; /* Entries in aScan[] */ ScanStatus *aScan; /* Scan definitions for sqlite3_stmt_scanstatus() */ #endif }; /* -** The following are allowed values for Vdbe.magic +** The following are allowed values for Vdbe.eVdbeState */ -#define VDBE_MAGIC_INIT 0x26bceaa5 /* Building a VDBE program */ -#define VDBE_MAGIC_RUN 0xbdf20da3 /* VDBE is ready to execute */ -#define VDBE_MAGIC_HALT 0x519c2973 /* VDBE has completed execution */ -#define VDBE_MAGIC_DEAD 0xb606c3c8 /* The VDBE has been deallocated */ +#define VDBE_INIT_STATE 0 /* Prepared statement under construction */ +#define VDBE_READY_STATE 1 /* Ready to run but not yet started */ +#define VDBE_RUN_STATE 2 /* Run in progress */ +#define VDBE_HALT_STATE 3 /* Finished. Need reset() or finalize() */ + +/* +** Structure used to store the context required by the +** sqlite3_preupdate_*() API functions. +*/ +struct PreUpdate { + Vdbe *v; + VdbeCursor *pCsr; /* Cursor to read old values from */ + int op; /* One of SQLITE_INSERT, UPDATE, DELETE */ + u8 *aRecord; /* old.* database record */ + KeyInfo *pKeyinfo; /* Key information */ + UnpackedRecord *pUnpacked; /* Unpacked version of aRecord[] */ + UnpackedRecord *pNewUnpacked; /* Unpacked version of new.* record */ + int iNewReg; /* Register for new.* values */ + int iBlobWrite; /* Value returned by preupdate_blobwrite() */ + i64 iKey1; /* First key value passed to hook */ + i64 iKey2; /* Second key value passed to hook */ + Mem oldipk; /* Memory cell holding "old" IPK value */ + Mem *aNew; /* Array of new.* values */ + Table *pTab; /* Schema object being updated */ + Index *pPk; /* PK index if pTab is WITHOUT ROWID */ + sqlite3_value **apDflt; /* Array of default values, if required */ + struct { + u8 keyinfoSpace[SZ_KEYINFO_0]; /* Space to hold pKeyinfo[0] content */ + } uKey; +}; + +/* +** An instance of this object is used to pass an vector of values into +** OP_VFilter, the xFilter method of a virtual table. The vector is the +** set of values on the right-hand side of an IN constraint. +** +** The value as passed into xFilter is an sqlite3_value with a "pointer" +** type, such as is generated by sqlite3_result_pointer() and read by +** sqlite3_value_pointer. Such values have MEM_Term|MEM_Subtype|MEM_Null +** and a subtype of 'p'. The sqlite3_vtab_in_first() and _next() interfaces +** know how to use this object to step through all the values in the +** right operand of the IN constraint. +*/ +typedef struct ValueList ValueList; +struct ValueList { + BtCursor *pCsr; /* An ephemeral table holding all values */ + sqlite3_value *pOut; /* Register to hold each decoded output value */ +}; + +/* Size of content associated with serial types that fit into a +** single-byte varint. +*/ +#ifndef SQLITE_AMALGAMATION +extern const u8 sqlite3SmallTypeSizes[]; +#endif /* ** Function prototypes */ void sqlite3VdbeError(Vdbe*, const char *, ...); void sqlite3VdbeFreeCursor(Vdbe *, VdbeCursor*); +void sqlite3VdbeFreeCursorNN(Vdbe*,VdbeCursor*); void sqliteVdbePopStack(Vdbe*,int); -int sqlite3VdbeCursorMoveto(VdbeCursor**, int*); +int SQLITE_NOINLINE sqlite3VdbeHandleMovedCursor(VdbeCursor *p); +int SQLITE_NOINLINE sqlite3VdbeFinishMoveto(VdbeCursor*); int sqlite3VdbeCursorRestore(VdbeCursor*); -#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) -void sqlite3VdbePrintOp(FILE*, int, Op*); -#endif u32 sqlite3VdbeSerialTypeLen(u32); u8 sqlite3VdbeOneByteSerialTypeLen(u8); -u32 sqlite3VdbeSerialType(Mem*, int, u32*); -u32 sqlite3VdbeSerialPut(unsigned char*, Mem*, u32); -u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*); -void sqlite3VdbeDeleteAuxData(Vdbe*, int, int); +#ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT + u64 sqlite3FloatSwap(u64 in); +# define swapMixedEndianFloat(X) X = sqlite3FloatSwap(X) +#else +# define swapMixedEndianFloat(X) +#endif +void sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*); +void sqlite3VdbeDeleteAuxData(sqlite3*, AuxData**, int, int); int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *); int sqlite3VdbeIdxKeyCompare(sqlite3*,VdbeCursor*,UnpackedRecord*,int*); int sqlite3VdbeIdxRowid(sqlite3*, BtCursor*, i64*); int sqlite3VdbeExec(Vdbe*); +#if !defined(SQLITE_OMIT_EXPLAIN) || defined(SQLITE_ENABLE_BYTECODE_VTAB) +int sqlite3VdbeNextOpcode(Vdbe*,Mem*,int,int*,int*,Op**); +char *sqlite3VdbeDisplayP4(sqlite3*,Op*); +#endif +#if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) +char *sqlite3VdbeDisplayComment(sqlite3*,const Op*,const char*); +#endif +#if !defined(SQLITE_OMIT_EXPLAIN) int sqlite3VdbeList(Vdbe*); +#endif int sqlite3VdbeHalt(Vdbe*); int sqlite3VdbeChangeEncoding(Mem *, int); int sqlite3VdbeMemTooBig(Mem*); @@ -457,47 +629,83 @@ int sqlite3VdbeMemCopy(Mem*, const Mem*); void sqlite3VdbeMemShallowCopy(Mem*, const Mem*, int); void sqlite3VdbeMemMove(Mem*, Mem*); int sqlite3VdbeMemNulTerminate(Mem*); -int sqlite3VdbeMemSetStr(Mem*, const char*, int, u8, void(*)(void*)); +int sqlite3VdbeMemSetStr(Mem*, const char*, i64, u8, void(*)(void*)); void sqlite3VdbeMemSetInt64(Mem*, i64); #ifdef SQLITE_OMIT_FLOATING_POINT # define sqlite3VdbeMemSetDouble sqlite3VdbeMemSetInt64 #else void sqlite3VdbeMemSetDouble(Mem*, double); #endif +void sqlite3VdbeMemSetPointer(Mem*, void*, const char*, void(*)(void*)); void sqlite3VdbeMemInit(Mem*,sqlite3*,u16); void sqlite3VdbeMemSetNull(Mem*); +#ifndef SQLITE_OMIT_INCRBLOB void sqlite3VdbeMemSetZeroBlob(Mem*,int); -void sqlite3VdbeMemSetRowSet(Mem*); +#else +int sqlite3VdbeMemSetZeroBlob(Mem*,int); +#endif +#ifdef SQLITE_DEBUG +int sqlite3VdbeMemIsRowSet(const Mem*); +#endif +int sqlite3VdbeMemSetRowSet(Mem*); +void sqlite3VdbeMemZeroTerminateIfAble(Mem*); int sqlite3VdbeMemMakeWriteable(Mem*); int sqlite3VdbeMemStringify(Mem*, u8, u8); -i64 sqlite3VdbeIntValue(Mem*); +int sqlite3IntFloatCompare(i64,double); +i64 sqlite3VdbeIntValue(const Mem*); int sqlite3VdbeMemIntegerify(Mem*); double sqlite3VdbeRealValue(Mem*); +int sqlite3VdbeBooleanValue(Mem*, int ifNull); void sqlite3VdbeIntegerAffinity(Mem*); int sqlite3VdbeMemRealify(Mem*); int sqlite3VdbeMemNumerify(Mem*); -void sqlite3VdbeMemCast(Mem*,u8,u8); -int sqlite3VdbeMemFromBtree(BtCursor*,u32,u32,int,Mem*); +int sqlite3VdbeMemCast(Mem*,u8,u8); +int sqlite3VdbeMemFromBtree(BtCursor*,u32,u32,Mem*); +int sqlite3VdbeMemFromBtreeZeroOffset(BtCursor*,u32,Mem*); void sqlite3VdbeMemRelease(Mem *p); +void sqlite3VdbeMemReleaseMalloc(Mem*p); int sqlite3VdbeMemFinalize(Mem*, FuncDef*); +#ifndef SQLITE_OMIT_WINDOWFUNC +int sqlite3VdbeMemAggValue(Mem*, Mem*, FuncDef*); +#endif +#if !defined(SQLITE_OMIT_EXPLAIN) || defined(SQLITE_ENABLE_BYTECODE_VTAB) const char *sqlite3OpcodeName(int); +#endif int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve); int sqlite3VdbeMemClearAndResize(Mem *pMem, int n); int sqlite3VdbeCloseStatement(Vdbe *, int); -void sqlite3VdbeFrameDelete(VdbeFrame*); +#ifdef SQLITE_DEBUG +int sqlite3VdbeFrameIsValid(VdbeFrame*); +#endif +void sqlite3VdbeFrameMemDel(void*); /* Destructor on Mem */ +void sqlite3VdbeFrameDelete(VdbeFrame*); /* Actually deletes the Frame */ int sqlite3VdbeFrameRestore(VdbeFrame *); +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK +void sqlite3VdbePreUpdateHook( + Vdbe*,VdbeCursor*,int,const char*,Table*,i64,int,int); +#endif int sqlite3VdbeTransferError(Vdbe *p); int sqlite3VdbeSorterInit(sqlite3 *, int, VdbeCursor *); void sqlite3VdbeSorterReset(sqlite3 *, VdbeSorter *); void sqlite3VdbeSorterClose(sqlite3 *, VdbeCursor *); int sqlite3VdbeSorterRowkey(const VdbeCursor *, Mem *); -int sqlite3VdbeSorterNext(sqlite3 *, const VdbeCursor *, int *); +int sqlite3VdbeSorterNext(sqlite3 *, const VdbeCursor *); int sqlite3VdbeSorterRewind(const VdbeCursor *, int *); int sqlite3VdbeSorterWrite(const VdbeCursor *, Mem *); int sqlite3VdbeSorterCompare(const VdbeCursor *, Mem *, int, int *); -#if !defined(SQLITE_OMIT_SHARED_CACHE) +void sqlite3VdbeValueListFree(void*); + +#ifdef SQLITE_DEBUG + void sqlite3VdbeIncrWriteCounter(Vdbe*, VdbeCursor*); + void sqlite3VdbeAssertAbortable(Vdbe*); +#else +# define sqlite3VdbeIncrWriteCounter(V,C) +# define sqlite3VdbeAssertAbortable(V) +#endif + +#if !defined(SQLITE_OMIT_SHARED_CACHE) void sqlite3VdbeEnter(Vdbe*); #else # define sqlite3VdbeEnter(X) @@ -515,17 +723,21 @@ int sqlite3VdbeCheckMemInvariants(Mem*); #endif #ifndef SQLITE_OMIT_FOREIGN_KEY -int sqlite3VdbeCheckFk(Vdbe *, int); +int sqlite3VdbeCheckFkImmediate(Vdbe*); +int sqlite3VdbeCheckFkDeferred(Vdbe*); #else -# define sqlite3VdbeCheckFk(p,i) 0 +# define sqlite3VdbeCheckFkImmediate(p) 0 +# define sqlite3VdbeCheckFkDeferred(p) 0 #endif -int sqlite3VdbeMemTranslate(Mem*, u8); #ifdef SQLITE_DEBUG void sqlite3VdbePrintSql(Vdbe*); - void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf); + void sqlite3VdbeMemPrettyPrint(Mem *pMem, StrAccum *pStr); +#endif +#ifndef SQLITE_OMIT_UTF16 + int sqlite3VdbeMemTranslate(Mem*, u8); + int sqlite3VdbeMemHandleBom(Mem *pMem); #endif -int sqlite3VdbeMemHandleBom(Mem *pMem); #ifndef SQLITE_OMIT_INCRBLOB int sqlite3VdbeMemExpandBlob(Mem *); @@ -535,4 +747,4 @@ int sqlite3VdbeMemHandleBom(Mem *pMem); #define ExpandBlob(P) SQLITE_OK #endif -#endif /* !defined(_VDBEINT_H_) */ +#endif /* !defined(SQLITE_VDBEINT_H) */ diff --git a/src/vdbeapi.c b/src/vdbeapi.c index fce43630e7..1118481d1c 100644 --- a/src/vdbeapi.c +++ b/src/vdbeapi.c @@ -15,6 +15,7 @@ */ #include "sqliteInt.h" #include "vdbeInt.h" +#include "opcodes.h" #ifndef SQLITE_OMIT_DEPRECATED /* @@ -60,12 +61,20 @@ static int vdbeSafetyNotNull(Vdbe *p){ */ static SQLITE_NOINLINE void invokeProfileCallback(sqlite3 *db, Vdbe *p){ sqlite3_int64 iNow; + sqlite3_int64 iElapse; assert( p->startTime>0 ); - assert( db->xProfile!=0 ); assert( db->init.busy==0 ); assert( p->zSql!=0 ); sqlite3OsCurrentTimeInt64(db->pVfs, &iNow); - db->xProfile(db->pProfileArg, p->zSql, (iNow - p->startTime)*1000000); + iElapse = (iNow - p->startTime)*1000000; +#ifndef SQLITE_OMIT_DEPRECATED + if( db->xProfile ){ + db->xProfile(db->pProfileArg, p->zSql, iElapse); + } +#endif + if( db->mTrace & SQLITE_TRACE_PROFILE ){ + db->trace.xV2(SQLITE_TRACE_PROFILE, db->pTraceArg, p, (void*)&iElapse); + } p->startTime = 0; } /* @@ -99,7 +108,9 @@ int sqlite3_finalize(sqlite3_stmt *pStmt){ if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT; sqlite3_mutex_enter(db->mutex); checkProfileCallback(db, v); - rc = sqlite3VdbeFinalize(v); + assert( v->eVdbeState>=VDBE_READY_STATE ); + rc = sqlite3VdbeReset(v); + sqlite3VdbeDelete(v); rc = sqlite3ApiExit(db, rc); sqlite3LeaveMutexAndCloseZombie(db); } @@ -140,14 +151,23 @@ int sqlite3_clear_bindings(sqlite3_stmt *pStmt){ int rc = SQLITE_OK; Vdbe *p = (Vdbe*)pStmt; #if SQLITE_THREADSAFE - sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex; + sqlite3_mutex *mutex; +#endif +#ifdef SQLITE_ENABLE_API_ARMOR + if( pStmt==0 ){ + return SQLITE_MISUSE_BKPT; + } +#endif +#if SQLITE_THREADSAFE + mutex = p->db->mutex; #endif sqlite3_mutex_enter(mutex); for(i=0; i<p->nVar; i++){ sqlite3VdbeMemRelease(&p->aVar[i]); p->aVar[i].flags = MEM_Null; } - if( p->isPrepareV2 && p->expmask ){ + assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 ); + if( p->expmask ){ p->expired = 1; } sqlite3_mutex_leave(mutex); @@ -162,7 +182,7 @@ int sqlite3_clear_bindings(sqlite3_stmt *pStmt){ const void *sqlite3_value_blob(sqlite3_value *pVal){ Mem *p = (Mem*)pVal; if( p->flags & (MEM_Blob|MEM_Str) ){ - if( sqlite3VdbeMemExpandBlob(p)!=SQLITE_OK ){ + if( ExpandBlob(p)!=SQLITE_OK ){ assert( p->flags==MEM_Null && p->z==0 ); return 0; } @@ -191,6 +211,19 @@ unsigned int sqlite3_value_subtype(sqlite3_value *pVal){ Mem *pMem = (Mem*)pVal; return ((pMem->flags & MEM_Subtype) ? pMem->eSubtype : 0); } +void *sqlite3_value_pointer(sqlite3_value *pVal, const char *zPType){ + Mem *p = (Mem*)pVal; + if( (p->flags&(MEM_TypeMask|MEM_Term|MEM_Subtype)) == + (MEM_Null|MEM_Term|MEM_Subtype) + && zPType!=0 + && p->eSubtype=='p' + && strcmp(p->u.zPType, zPType)==0 + ){ + return (void*)p->z; + }else{ + return 0; + } +} const unsigned char *sqlite3_value_text(sqlite3_value *pVal){ return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8); } @@ -211,41 +244,101 @@ const void *sqlite3_value_text16le(sqlite3_value *pVal){ */ int sqlite3_value_type(sqlite3_value* pVal){ static const u8 aType[] = { - SQLITE_BLOB, /* 0x00 */ - SQLITE_NULL, /* 0x01 */ - SQLITE_TEXT, /* 0x02 */ - SQLITE_NULL, /* 0x03 */ - SQLITE_INTEGER, /* 0x04 */ - SQLITE_NULL, /* 0x05 */ - SQLITE_INTEGER, /* 0x06 */ - SQLITE_NULL, /* 0x07 */ - SQLITE_FLOAT, /* 0x08 */ - SQLITE_NULL, /* 0x09 */ - SQLITE_FLOAT, /* 0x0a */ - SQLITE_NULL, /* 0x0b */ - SQLITE_INTEGER, /* 0x0c */ - SQLITE_NULL, /* 0x0d */ - SQLITE_INTEGER, /* 0x0e */ - SQLITE_NULL, /* 0x0f */ - SQLITE_BLOB, /* 0x10 */ - SQLITE_NULL, /* 0x11 */ - SQLITE_TEXT, /* 0x12 */ - SQLITE_NULL, /* 0x13 */ - SQLITE_INTEGER, /* 0x14 */ - SQLITE_NULL, /* 0x15 */ - SQLITE_INTEGER, /* 0x16 */ - SQLITE_NULL, /* 0x17 */ - SQLITE_FLOAT, /* 0x18 */ - SQLITE_NULL, /* 0x19 */ - SQLITE_FLOAT, /* 0x1a */ - SQLITE_NULL, /* 0x1b */ - SQLITE_INTEGER, /* 0x1c */ - SQLITE_NULL, /* 0x1d */ - SQLITE_INTEGER, /* 0x1e */ - SQLITE_NULL, /* 0x1f */ + SQLITE_BLOB, /* 0x00 (not possible) */ + SQLITE_NULL, /* 0x01 NULL */ + SQLITE_TEXT, /* 0x02 TEXT */ + SQLITE_NULL, /* 0x03 (not possible) */ + SQLITE_INTEGER, /* 0x04 INTEGER */ + SQLITE_NULL, /* 0x05 (not possible) */ + SQLITE_INTEGER, /* 0x06 INTEGER + TEXT */ + SQLITE_NULL, /* 0x07 (not possible) */ + SQLITE_FLOAT, /* 0x08 FLOAT */ + SQLITE_NULL, /* 0x09 (not possible) */ + SQLITE_FLOAT, /* 0x0a FLOAT + TEXT */ + SQLITE_NULL, /* 0x0b (not possible) */ + SQLITE_INTEGER, /* 0x0c (not possible) */ + SQLITE_NULL, /* 0x0d (not possible) */ + SQLITE_INTEGER, /* 0x0e (not possible) */ + SQLITE_NULL, /* 0x0f (not possible) */ + SQLITE_BLOB, /* 0x10 BLOB */ + SQLITE_NULL, /* 0x11 (not possible) */ + SQLITE_TEXT, /* 0x12 (not possible) */ + SQLITE_NULL, /* 0x13 (not possible) */ + SQLITE_INTEGER, /* 0x14 INTEGER + BLOB */ + SQLITE_NULL, /* 0x15 (not possible) */ + SQLITE_INTEGER, /* 0x16 (not possible) */ + SQLITE_NULL, /* 0x17 (not possible) */ + SQLITE_FLOAT, /* 0x18 FLOAT + BLOB */ + SQLITE_NULL, /* 0x19 (not possible) */ + SQLITE_FLOAT, /* 0x1a (not possible) */ + SQLITE_NULL, /* 0x1b (not possible) */ + SQLITE_INTEGER, /* 0x1c (not possible) */ + SQLITE_NULL, /* 0x1d (not possible) */ + SQLITE_INTEGER, /* 0x1e (not possible) */ + SQLITE_NULL, /* 0x1f (not possible) */ + SQLITE_FLOAT, /* 0x20 INTREAL */ + SQLITE_NULL, /* 0x21 (not possible) */ + SQLITE_FLOAT, /* 0x22 INTREAL + TEXT */ + SQLITE_NULL, /* 0x23 (not possible) */ + SQLITE_FLOAT, /* 0x24 (not possible) */ + SQLITE_NULL, /* 0x25 (not possible) */ + SQLITE_FLOAT, /* 0x26 (not possible) */ + SQLITE_NULL, /* 0x27 (not possible) */ + SQLITE_FLOAT, /* 0x28 (not possible) */ + SQLITE_NULL, /* 0x29 (not possible) */ + SQLITE_FLOAT, /* 0x2a (not possible) */ + SQLITE_NULL, /* 0x2b (not possible) */ + SQLITE_FLOAT, /* 0x2c (not possible) */ + SQLITE_NULL, /* 0x2d (not possible) */ + SQLITE_FLOAT, /* 0x2e (not possible) */ + SQLITE_NULL, /* 0x2f (not possible) */ + SQLITE_BLOB, /* 0x30 (not possible) */ + SQLITE_NULL, /* 0x31 (not possible) */ + SQLITE_TEXT, /* 0x32 (not possible) */ + SQLITE_NULL, /* 0x33 (not possible) */ + SQLITE_FLOAT, /* 0x34 (not possible) */ + SQLITE_NULL, /* 0x35 (not possible) */ + SQLITE_FLOAT, /* 0x36 (not possible) */ + SQLITE_NULL, /* 0x37 (not possible) */ + SQLITE_FLOAT, /* 0x38 (not possible) */ + SQLITE_NULL, /* 0x39 (not possible) */ + SQLITE_FLOAT, /* 0x3a (not possible) */ + SQLITE_NULL, /* 0x3b (not possible) */ + SQLITE_FLOAT, /* 0x3c (not possible) */ + SQLITE_NULL, /* 0x3d (not possible) */ + SQLITE_FLOAT, /* 0x3e (not possible) */ + SQLITE_NULL, /* 0x3f (not possible) */ }; +#ifdef SQLITE_DEBUG + { + int eType = SQLITE_BLOB; + if( pVal->flags & MEM_Null ){ + eType = SQLITE_NULL; + }else if( pVal->flags & (MEM_Real|MEM_IntReal) ){ + eType = SQLITE_FLOAT; + }else if( pVal->flags & MEM_Int ){ + eType = SQLITE_INTEGER; + }else if( pVal->flags & MEM_Str ){ + eType = SQLITE_TEXT; + } + assert( eType == aType[pVal->flags&MEM_AffMask] ); + } +#endif return aType[pVal->flags&MEM_AffMask]; } +int sqlite3_value_encoding(sqlite3_value *pVal){ + return pVal->enc; +} + +/* Return true if a parameter to xUpdate represents an unchanged column */ +int sqlite3_value_nochange(sqlite3_value *pVal){ + return (pVal->flags&(MEM_Null|MEM_Zero))==(MEM_Null|MEM_Zero); +} + +/* Return true if a parameter value originated from an sqlite3_bind() */ +int sqlite3_value_frombind(sqlite3_value *pVal){ + return (pVal->flags&MEM_FromBind)!=0; +} /* Make a copy of an sqlite3_value object */ @@ -265,6 +358,9 @@ sqlite3_value *sqlite3_value_dup(const sqlite3_value *pOrig){ sqlite3ValueFree(pNew); pNew = 0; } + }else if( pNew->flags & MEM_Null ){ + /* Do not duplicate pointer values */ + pNew->flags &= ~(MEM_Term|MEM_Subtype); } return pNew; } @@ -275,18 +371,18 @@ sqlite3_value *sqlite3_value_dup(const sqlite3_value *pOrig){ void sqlite3_value_free(sqlite3_value *pOld){ sqlite3ValueFree(pOld); } - + /**************************** sqlite3_result_ ******************************* ** The following routines are used by user-defined functions to specify ** the function result. ** ** The setStrOrError() function calls sqlite3VdbeMemSetStr() to store the -** result as a string or blob but if the string or blob is too large, it -** then sets the error code to SQLITE_TOOBIG +** result as a string or blob. Appropriate errors are set if the string/blob +** is too big or if an OOM occurs. ** ** The invokeValueDestructor(P,X) routine invokes destructor function X() -** on value P is not going to be used and need to be destroyed. +** on value P if P is not going to be used and need to be destroyed. */ static void setResultStrOrError( sqlite3_context *pCtx, /* Function context */ @@ -295,14 +391,28 @@ static void setResultStrOrError( u8 enc, /* Encoding of z. 0 for BLOBs */ void (*xDel)(void*) /* Destructor function */ ){ - if( sqlite3VdbeMemSetStr(pCtx->pOut, z, n, enc, xDel)==SQLITE_TOOBIG ){ + Mem *pOut = pCtx->pOut; + int rc = sqlite3VdbeMemSetStr(pOut, z, n, enc, xDel); + if( rc ){ + if( rc==SQLITE_TOOBIG ){ + sqlite3_result_error_toobig(pCtx); + }else{ + /* The only errors possible from sqlite3VdbeMemSetStr are + ** SQLITE_TOOBIG and SQLITE_NOMEM */ + assert( rc==SQLITE_NOMEM ); + sqlite3_result_error_nomem(pCtx); + } + return; + } + sqlite3VdbeChangeEncoding(pOut, pCtx->enc); + if( sqlite3VdbeMemTooBig(pOut) ){ sqlite3_result_error_toobig(pCtx); } } static int invokeValueDestructor( const void *p, /* Value to destroy */ void (*xDel)(void*), /* The destructor */ - sqlite3_context *pCtx /* Set a SQLITE_TOOBIG error if no NULL */ + sqlite3_context *pCtx /* Set a SQLITE_TOOBIG error if not NULL */ ){ assert( xDel!=SQLITE_DYNAMIC ); if( xDel==0 ){ @@ -312,27 +422,46 @@ static int invokeValueDestructor( }else{ xDel((void*)p); } - if( pCtx ) sqlite3_result_error_toobig(pCtx); +#ifdef SQLITE_ENABLE_API_ARMOR + if( pCtx!=0 ){ + sqlite3_result_error_toobig(pCtx); + } +#else + assert( pCtx!=0 ); + sqlite3_result_error_toobig(pCtx); +#endif return SQLITE_TOOBIG; } void sqlite3_result_blob( - sqlite3_context *pCtx, - const void *z, - int n, + sqlite3_context *pCtx, + const void *z, + int n, void (*xDel)(void *) ){ +#ifdef SQLITE_ENABLE_API_ARMOR + if( pCtx==0 || n<0 ){ + invokeValueDestructor(z, xDel, pCtx); + return; + } +#endif assert( n>=0 ); assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); setResultStrOrError(pCtx, z, n, 0, xDel); } void sqlite3_result_blob64( - sqlite3_context *pCtx, - const void *z, + sqlite3_context *pCtx, + const void *z, sqlite3_uint64 n, void (*xDel)(void *) ){ - assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); assert( xDel!=SQLITE_DYNAMIC ); +#ifdef SQLITE_ENABLE_API_ARMOR + if( pCtx==0 ){ + invokeValueDestructor(z, xDel, 0); + return; + } +#endif + assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); if( n>0x7fffffff ){ (void)invokeValueDestructor(z, xDel, pCtx); }else{ @@ -340,144 +469,255 @@ void sqlite3_result_blob64( } } void sqlite3_result_double(sqlite3_context *pCtx, double rVal){ +#ifdef SQLITE_ENABLE_API_ARMOR + if( pCtx==0 ) return; +#endif assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); sqlite3VdbeMemSetDouble(pCtx->pOut, rVal); } void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){ +#ifdef SQLITE_ENABLE_API_ARMOR + if( pCtx==0 ) return; +#endif assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); pCtx->isError = SQLITE_ERROR; - pCtx->fErrorOrAux = 1; sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF8, SQLITE_TRANSIENT); } #ifndef SQLITE_OMIT_UTF16 void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){ +#ifdef SQLITE_ENABLE_API_ARMOR + if( pCtx==0 ) return; +#endif assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); pCtx->isError = SQLITE_ERROR; - pCtx->fErrorOrAux = 1; sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT); } #endif void sqlite3_result_int(sqlite3_context *pCtx, int iVal){ +#ifdef SQLITE_ENABLE_API_ARMOR + if( pCtx==0 ) return; +#endif assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); sqlite3VdbeMemSetInt64(pCtx->pOut, (i64)iVal); } void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){ +#ifdef SQLITE_ENABLE_API_ARMOR + if( pCtx==0 ) return; +#endif assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); sqlite3VdbeMemSetInt64(pCtx->pOut, iVal); } void sqlite3_result_null(sqlite3_context *pCtx){ +#ifdef SQLITE_ENABLE_API_ARMOR + if( pCtx==0 ) return; +#endif assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); sqlite3VdbeMemSetNull(pCtx->pOut); } +void sqlite3_result_pointer( + sqlite3_context *pCtx, + void *pPtr, + const char *zPType, + void (*xDestructor)(void*) +){ + Mem *pOut; +#ifdef SQLITE_ENABLE_API_ARMOR + if( pCtx==0 ){ + invokeValueDestructor(pPtr, xDestructor, 0); + return; + } +#endif + pOut = pCtx->pOut; + assert( sqlite3_mutex_held(pOut->db->mutex) ); + sqlite3VdbeMemRelease(pOut); + pOut->flags = MEM_Null; + sqlite3VdbeMemSetPointer(pOut, pPtr, zPType, xDestructor); +} void sqlite3_result_subtype(sqlite3_context *pCtx, unsigned int eSubtype){ - Mem *pOut = pCtx->pOut; + Mem *pOut; +#ifdef SQLITE_ENABLE_API_ARMOR + if( pCtx==0 ) return; +#endif +#if defined(SQLITE_STRICT_SUBTYPE) && SQLITE_STRICT_SUBTYPE+0!=0 + if( pCtx->pFunc!=0 + && (pCtx->pFunc->funcFlags & SQLITE_RESULT_SUBTYPE)==0 + ){ + char zErr[200]; + sqlite3_snprintf(sizeof(zErr), zErr, + "misuse of sqlite3_result_subtype() by %s()", + pCtx->pFunc->zName); + sqlite3_result_error(pCtx, zErr, -1); + return; + } +#endif /* SQLITE_STRICT_SUBTYPE */ + pOut = pCtx->pOut; assert( sqlite3_mutex_held(pOut->db->mutex) ); pOut->eSubtype = eSubtype & 0xff; pOut->flags |= MEM_Subtype; } void sqlite3_result_text( - sqlite3_context *pCtx, - const char *z, + sqlite3_context *pCtx, + const char *z, int n, void (*xDel)(void *) ){ +#ifdef SQLITE_ENABLE_API_ARMOR + if( pCtx==0 ){ + invokeValueDestructor(z, xDel, 0); + return; + } +#endif assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel); } void sqlite3_result_text64( - sqlite3_context *pCtx, - const char *z, + sqlite3_context *pCtx, + const char *z, sqlite3_uint64 n, void (*xDel)(void *), unsigned char enc ){ +#ifdef SQLITE_ENABLE_API_ARMOR + if( pCtx==0 ){ + invokeValueDestructor(z, xDel, 0); + return; + } +#endif assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); assert( xDel!=SQLITE_DYNAMIC ); - if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE; + if( enc!=SQLITE_UTF8 ){ + if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE; + n &= ~(u64)1; + } if( n>0x7fffffff ){ (void)invokeValueDestructor(z, xDel, pCtx); }else{ setResultStrOrError(pCtx, z, (int)n, enc, xDel); + sqlite3VdbeMemZeroTerminateIfAble(pCtx->pOut); } } #ifndef SQLITE_OMIT_UTF16 void sqlite3_result_text16( - sqlite3_context *pCtx, - const void *z, - int n, + sqlite3_context *pCtx, + const void *z, + int n, void (*xDel)(void *) ){ assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); - setResultStrOrError(pCtx, z, n, SQLITE_UTF16NATIVE, xDel); + setResultStrOrError(pCtx, z, n & ~(u64)1, SQLITE_UTF16NATIVE, xDel); } void sqlite3_result_text16be( - sqlite3_context *pCtx, - const void *z, - int n, + sqlite3_context *pCtx, + const void *z, + int n, void (*xDel)(void *) ){ assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); - setResultStrOrError(pCtx, z, n, SQLITE_UTF16BE, xDel); + setResultStrOrError(pCtx, z, n & ~(u64)1, SQLITE_UTF16BE, xDel); } void sqlite3_result_text16le( - sqlite3_context *pCtx, - const void *z, - int n, + sqlite3_context *pCtx, + const void *z, + int n, void (*xDel)(void *) ){ assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); - setResultStrOrError(pCtx, z, n, SQLITE_UTF16LE, xDel); + setResultStrOrError(pCtx, z, n & ~(u64)1, SQLITE_UTF16LE, xDel); } #endif /* SQLITE_OMIT_UTF16 */ void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){ + Mem *pOut; + +#ifdef SQLITE_ENABLE_API_ARMOR + if( pCtx==0 ) return; + if( pValue==0 ){ + sqlite3_result_null(pCtx); + return; + } +#endif + pOut = pCtx->pOut; assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); - sqlite3VdbeMemCopy(pCtx->pOut, pValue); + sqlite3VdbeMemCopy(pOut, pValue); + sqlite3VdbeChangeEncoding(pOut, pCtx->enc); + if( sqlite3VdbeMemTooBig(pOut) ){ + sqlite3_result_error_toobig(pCtx); + } } void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){ - assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); - sqlite3VdbeMemSetZeroBlob(pCtx->pOut, n); + sqlite3_result_zeroblob64(pCtx, n>0 ? n : 0); } int sqlite3_result_zeroblob64(sqlite3_context *pCtx, u64 n){ - Mem *pOut = pCtx->pOut; + Mem *pOut; + +#ifdef SQLITE_ENABLE_API_ARMOR + if( pCtx==0 ) return SQLITE_MISUSE_BKPT; +#endif + pOut = pCtx->pOut; assert( sqlite3_mutex_held(pOut->db->mutex) ); if( n>(u64)pOut->db->aLimit[SQLITE_LIMIT_LENGTH] ){ + sqlite3_result_error_toobig(pCtx); return SQLITE_TOOBIG; } +#ifndef SQLITE_OMIT_INCRBLOB sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n); return SQLITE_OK; +#else + return sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n); +#endif } void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){ - pCtx->isError = errCode; - pCtx->fErrorOrAux = 1; +#ifdef SQLITE_ENABLE_API_ARMOR + if( pCtx==0 ) return; +#endif + pCtx->isError = errCode ? errCode : -1; #ifdef SQLITE_DEBUG if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode; #endif if( pCtx->pOut->flags & MEM_Null ){ - sqlite3VdbeMemSetStr(pCtx->pOut, sqlite3ErrStr(errCode), -1, - SQLITE_UTF8, SQLITE_STATIC); + setResultStrOrError(pCtx, sqlite3ErrStr(errCode), -1, SQLITE_UTF8, + SQLITE_STATIC); } } /* Force an SQLITE_TOOBIG error. */ void sqlite3_result_error_toobig(sqlite3_context *pCtx){ +#ifdef SQLITE_ENABLE_API_ARMOR + if( pCtx==0 ) return; +#endif assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); pCtx->isError = SQLITE_TOOBIG; - pCtx->fErrorOrAux = 1; - sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1, + sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1, SQLITE_UTF8, SQLITE_STATIC); } /* An SQLITE_NOMEM error. */ void sqlite3_result_error_nomem(sqlite3_context *pCtx){ +#ifdef SQLITE_ENABLE_API_ARMOR + if( pCtx==0 ) return; +#endif assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); sqlite3VdbeMemSetNull(pCtx->pOut); - pCtx->isError = SQLITE_NOMEM; - pCtx->fErrorOrAux = 1; + pCtx->isError = SQLITE_NOMEM_BKPT; sqlite3OomFault(pCtx->pOut->db); } +#ifndef SQLITE_UNTESTABLE +/* Force the INT64 value currently stored as the result to be +** a MEM_IntReal value. See the SQLITE_TESTCTRL_RESULT_INTREAL +** test-control. +*/ +void sqlite3ResultIntReal(sqlite3_context *pCtx){ + assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); + if( pCtx->pOut->flags & MEM_Int ){ + pCtx->pOut->flags &= ~MEM_Int; + pCtx->pOut->flags |= MEM_IntReal; + } +} +#endif + + /* -** This function is called after a transaction has been committed. It +** This function is called after a transaction has been committed. It ** invokes callbacks registered with sqlite3_wal_hook() as required. */ static int doWalCallbacks(sqlite3 *db){ @@ -491,8 +731,8 @@ static int doWalCallbacks(sqlite3 *db){ sqlite3BtreeEnter(pBt); nEntry = sqlite3PagerWalCallback(sqlite3BtreePager(pBt)); sqlite3BtreeLeave(pBt); - if( db->xWalCallback && nEntry>0 && rc==SQLITE_OK ){ - rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zName, nEntry); + if( nEntry>0 && db->xWalCallback && rc==SQLITE_OK ){ + rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zDbSName, nEntry); } } } @@ -506,7 +746,7 @@ static int doWalCallbacks(sqlite3 *db){ ** statement is completely executed or an error occurs. ** ** This routine implements the bulk of the logic behind the sqlite_step() -** API. The only thing omitted is the automatic recompile if a +** API. The only thing omitted is the automatic recompile if a ** schema change has occurred. That detail is handled by the ** outer sqlite3_step() wrapper procedure. */ @@ -515,72 +755,83 @@ static int sqlite3Step(Vdbe *p){ int rc; assert(p); - if( p->magic!=VDBE_MAGIC_RUN ){ - /* We used to require that sqlite3_reset() be called before retrying - ** sqlite3_step() after any error or after SQLITE_DONE. But beginning - ** with version 3.7.0, we changed this so that sqlite3_reset() would - ** be called automatically instead of throwing the SQLITE_MISUSE error. - ** This "automatic-reset" change is not technically an incompatibility, - ** since any application that receives an SQLITE_MISUSE is broken by - ** definition. - ** - ** Nevertheless, some published applications that were originally written - ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE - ** returns, and those were broken by the automatic-reset change. As a - ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the - ** legacy behavior of returning SQLITE_MISUSE for cases where the - ** previous sqlite3_step() returned something other than a SQLITE_LOCKED - ** or SQLITE_BUSY error. - */ -#ifdef SQLITE_OMIT_AUTORESET - if( (rc = p->rc&0xff)==SQLITE_BUSY || rc==SQLITE_LOCKED ){ - sqlite3_reset((sqlite3_stmt*)p); - }else{ - return SQLITE_MISUSE_BKPT; - } -#else - sqlite3_reset((sqlite3_stmt*)p); -#endif - } - - /* Check that malloc() has not failed. If it has, return early. */ db = p->db; - if( db->mallocFailed ){ - p->rc = SQLITE_NOMEM; - return SQLITE_NOMEM; - } + if( p->eVdbeState!=VDBE_RUN_STATE ){ + restart_step: + if( p->eVdbeState==VDBE_READY_STATE ){ + if( p->expired ){ + p->rc = SQLITE_SCHEMA; + rc = SQLITE_ERROR; + if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ){ + /* If this statement was prepared using saved SQL and an + ** error has occurred, then return the error code in p->rc to the + ** caller. Set the error code in the database handle to the same + ** value. + */ + rc = sqlite3VdbeTransferError(p); + } + goto end_of_step; + } - if( p->pc<=0 && p->expired ){ - p->rc = SQLITE_SCHEMA; - rc = SQLITE_ERROR; - goto end_of_step; - } - if( p->pc<0 ){ - /* If there are no other statements currently running, then - ** reset the interrupt flag. This prevents a call to sqlite3_interrupt - ** from interrupting a statement that has not yet started. - */ - if( db->nVdbeActive==0 ){ - db->u1.isInterrupted = 0; - } + /* If there are no other statements currently running, then + ** reset the interrupt flag. This prevents a call to sqlite3_interrupt + ** from interrupting a statement that has not yet started. + */ + if( db->nVdbeActive==0 ){ + AtomicStore(&db->u1.isInterrupted, 0); + } - assert( db->nVdbeWrite>0 || db->autoCommit==0 - || (db->nDeferredCons==0 && db->nDeferredImmCons==0) - ); + assert( db->nVdbeWrite>0 || db->autoCommit==0 + || ((db->nDeferredCons + db->nDeferredImmCons)==0) + ); #ifndef SQLITE_OMIT_TRACE - if( db->xProfile && !db->init.busy && p->zSql ){ - sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime); - }else{ - assert( p->startTime==0 ); - } + if( (db->mTrace & (SQLITE_TRACE_PROFILE|SQLITE_TRACE_XPROFILE))!=0 + && !db->init.busy && p->zSql ){ + sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime); + }else{ + assert( p->startTime==0 ); + } #endif - db->nVdbeActive++; - if( p->readOnly==0 ) db->nVdbeWrite++; - if( p->bIsReader ) db->nVdbeRead++; - p->pc = 0; + db->nVdbeActive++; + if( p->readOnly==0 ) db->nVdbeWrite++; + if( p->bIsReader ) db->nVdbeRead++; + p->pc = 0; + p->eVdbeState = VDBE_RUN_STATE; + }else + + if( ALWAYS(p->eVdbeState==VDBE_HALT_STATE) ){ + /* We used to require that sqlite3_reset() be called before retrying + ** sqlite3_step() after any error or after SQLITE_DONE. But beginning + ** with version 3.7.0, we changed this so that sqlite3_reset() would + ** be called automatically instead of throwing the SQLITE_MISUSE error. + ** This "automatic-reset" change is not technically an incompatibility, + ** since any application that receives an SQLITE_MISUSE is broken by + ** definition. + ** + ** Nevertheless, some published applications that were originally written + ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE + ** returns, and those were broken by the automatic-reset change. As a + ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the + ** legacy behavior of returning SQLITE_MISUSE for cases where the + ** previous sqlite3_step() returned something other than a SQLITE_LOCKED + ** or SQLITE_BUSY error. + */ +#ifdef SQLITE_OMIT_AUTORESET + if( (rc = p->rc&0xff)==SQLITE_BUSY || rc==SQLITE_LOCKED ){ + sqlite3_reset((sqlite3_stmt*)p); + }else{ + return SQLITE_MISUSE_BKPT; + } +#else + sqlite3_reset((sqlite3_stmt*)p); +#endif + assert( p->eVdbeState==VDBE_READY_STATE ); + goto restart_step; + } } + #ifdef SQLITE_DEBUG p->rcApp = SQLITE_OK; #endif @@ -595,42 +846,44 @@ static int sqlite3Step(Vdbe *p){ db->nVdbeExec--; } + if( rc==SQLITE_ROW ){ + assert( p->rc==SQLITE_OK ); + assert( db->mallocFailed==0 ); + db->errCode = SQLITE_ROW; + return SQLITE_ROW; + }else{ #ifndef SQLITE_OMIT_TRACE - /* If the statement completed successfully, invoke the profile callback */ - if( rc!=SQLITE_ROW ) checkProfileCallback(db, p); + /* If the statement completed successfully, invoke the profile callback */ + checkProfileCallback(db, p); #endif - - if( rc==SQLITE_DONE ){ - assert( p->rc==SQLITE_OK ); - p->rc = doWalCallbacks(db); - if( p->rc!=SQLITE_OK ){ - rc = SQLITE_ERROR; + p->pResultRow = 0; + if( rc==SQLITE_DONE && db->autoCommit ){ + assert( p->rc==SQLITE_OK ); + p->rc = doWalCallbacks(db); + if( p->rc!=SQLITE_OK ){ + rc = SQLITE_ERROR; + } + }else if( rc!=SQLITE_DONE && (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ){ + /* If this statement was prepared using saved SQL and an + ** error has occurred, then return the error code in p->rc to the + ** caller. Set the error code in the database handle to the same value. + */ + rc = sqlite3VdbeTransferError(p); } } db->errCode = rc; if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){ - p->rc = SQLITE_NOMEM; + p->rc = SQLITE_NOMEM_BKPT; + if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ) rc = p->rc; } end_of_step: - /* At this point local variable rc holds the value that should be - ** returned if this statement was compiled using the legacy - ** sqlite3_prepare() interface. According to the docs, this can only - ** be one of the values in the first assert() below. Variable p->rc - ** contains the value that would be returned if sqlite3_finalize() - ** were called on statement p. - */ - assert( rc==SQLITE_ROW || rc==SQLITE_DONE || rc==SQLITE_ERROR + /* There are only a limited number of result codes allowed from the + ** statements prepared using the legacy sqlite3_prepare() interface */ + assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 + || rc==SQLITE_ROW || rc==SQLITE_DONE || rc==SQLITE_ERROR || (rc&0xff)==SQLITE_BUSY || rc==SQLITE_MISUSE ); - assert( (p->rc!=SQLITE_ROW && p->rc!=SQLITE_DONE) || p->rc==p->rcApp ); - if( p->isPrepareV2 && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){ - /* If this statement was prepared using sqlite3_prepare_v2(), and an - ** error has occurred, then return the error code in p->rc to the - ** caller. Set the error code in the database handle to the same value. - */ - rc = sqlite3VdbeTransferError(p); - } return (rc&db->errMask); } @@ -641,7 +894,6 @@ static int sqlite3Step(Vdbe *p){ */ int sqlite3_step(sqlite3_stmt *pStmt){ int rc = SQLITE_OK; /* Result from sqlite3Step() */ - int rc2 = SQLITE_OK; /* Result from sqlite3Reprepare() */ Vdbe *v = (Vdbe*)pStmt; /* the prepared statement */ int cnt = 0; /* Counter to prevent infinite loop of reprepares */ sqlite3 *db; /* The database connection */ @@ -651,36 +903,40 @@ int sqlite3_step(sqlite3_stmt *pStmt){ } db = v->db; sqlite3_mutex_enter(db->mutex); - v->doingRerun = 0; while( (rc = sqlite3Step(v))==SQLITE_SCHEMA && cnt++ < SQLITE_MAX_SCHEMA_RETRY ){ int savedPc = v->pc; - rc2 = rc = sqlite3Reprepare(v); - if( rc!=SQLITE_OK) break; + rc = sqlite3Reprepare(v); + if( rc!=SQLITE_OK ){ + /* This case occurs after failing to recompile an sql statement. + ** The error message from the SQL compiler has already been loaded + ** into the database handle. This block copies the error message + ** from the database handle into the statement and sets the statement + ** program counter to 0 to ensure that when the statement is + ** finalized or reset the parser error message is available via + ** sqlite3_errmsg() and sqlite3_errcode(). + */ + const char *zErr = (const char *)sqlite3_value_text(db->pErr); + sqlite3DbFree(db, v->zErrMsg); + if( !db->mallocFailed ){ + v->zErrMsg = sqlite3DbStrDup(db, zErr); + v->rc = rc = sqlite3ApiExit(db, rc); + } else { + v->zErrMsg = 0; + v->rc = rc = SQLITE_NOMEM_BKPT; + } + break; + } sqlite3_reset(pStmt); - if( savedPc>=0 ) v->doingRerun = 1; - assert( v->expired==0 ); - } - if( rc2!=SQLITE_OK ){ - /* This case occurs after failing to recompile an sql statement. - ** The error message from the SQL compiler has already been loaded - ** into the database handle. This block copies the error message - ** from the database handle into the statement and sets the statement - ** program counter to 0 to ensure that when the statement is - ** finalized or reset the parser error message is available via - ** sqlite3_errmsg() and sqlite3_errcode(). - */ - const char *zErr = (const char *)sqlite3_value_text(db->pErr); - sqlite3DbFree(db, v->zErrMsg); - if( !db->mallocFailed ){ - v->zErrMsg = sqlite3DbStrDup(db, zErr); - v->rc = rc2; - } else { - v->zErrMsg = 0; - v->rc = rc = SQLITE_NOMEM; + if( savedPc>=0 ){ + /* Setting minWriteFileFormat to 254 is a signal to the OP_Init and + ** OP_Trace opcodes to *not* perform SQLITE_TRACE_STMT because it has + ** already been done once on a prior invocation that failed due to + ** SQLITE_SCHEMA. tag-20220401a */ + v->minWriteFileFormat = 254; } + assert( v->expired==0 ); } - rc = sqlite3ApiExit(db, rc); sqlite3_mutex_leave(db->mutex); return rc; } @@ -691,6 +947,9 @@ int sqlite3_step(sqlite3_stmt *pStmt){ ** pointer to it. */ void *sqlite3_user_data(sqlite3_context *p){ +#ifdef SQLITE_ENABLE_API_ARMOR + if( p==0 ) return 0; +#endif assert( p && p->pFunc ); return p->pFunc->pUserData; } @@ -706,10 +965,119 @@ void *sqlite3_user_data(sqlite3_context *p){ ** application defined function. */ sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){ +#ifdef SQLITE_ENABLE_API_ARMOR + if( p==0 ) return 0; +#else assert( p && p->pOut ); +#endif return p->pOut->db; } +/* +** If this routine is invoked from within an xColumn method of a virtual +** table, then it returns true if and only if the the call is during an +** UPDATE operation and the value of the column will not be modified +** by the UPDATE. +** +** If this routine is called from any context other than within the +** xColumn method of a virtual table, then the return value is meaningless +** and arbitrary. +** +** Virtual table implements might use this routine to optimize their +** performance by substituting a NULL result, or some other light-weight +** value, as a signal to the xUpdate routine that the column is unchanged. +*/ +int sqlite3_vtab_nochange(sqlite3_context *p){ +#ifdef SQLITE_ENABLE_API_ARMOR + if( p==0 ) return 0; +#else + assert( p ); +#endif + return sqlite3_value_nochange(p->pOut); +} + +/* +** The destructor function for a ValueList object. This needs to be +** a separate function, unknowable to the application, to ensure that +** calls to sqlite3_vtab_in_first()/sqlite3_vtab_in_next() that are not +** preceded by activation of IN processing via sqlite3_vtab_int() do not +** try to access a fake ValueList object inserted by a hostile extension. +*/ +void sqlite3VdbeValueListFree(void *pToDelete){ + sqlite3_free(pToDelete); +} + +/* +** Implementation of sqlite3_vtab_in_first() (if bNext==0) and +** sqlite3_vtab_in_next() (if bNext!=0). +*/ +static int valueFromValueList( + sqlite3_value *pVal, /* Pointer to the ValueList object */ + sqlite3_value **ppOut, /* Store the next value from the list here */ + int bNext /* 1 for _next(). 0 for _first() */ +){ + int rc; + ValueList *pRhs; + + *ppOut = 0; + if( pVal==0 ) return SQLITE_MISUSE_BKPT; + if( (pVal->flags & MEM_Dyn)==0 || pVal->xDel!=sqlite3VdbeValueListFree ){ + return SQLITE_ERROR; + }else{ + assert( (pVal->flags&(MEM_TypeMask|MEM_Term|MEM_Subtype)) == + (MEM_Null|MEM_Term|MEM_Subtype) ); + assert( pVal->eSubtype=='p' ); + assert( pVal->u.zPType!=0 && strcmp(pVal->u.zPType,"ValueList")==0 ); + pRhs = (ValueList*)pVal->z; + } + if( bNext ){ + rc = sqlite3BtreeNext(pRhs->pCsr, 0); + }else{ + int dummy = 0; + rc = sqlite3BtreeFirst(pRhs->pCsr, &dummy); + assert( rc==SQLITE_OK || sqlite3BtreeEof(pRhs->pCsr) ); + if( sqlite3BtreeEof(pRhs->pCsr) ) rc = SQLITE_DONE; + } + if( rc==SQLITE_OK ){ + u32 sz; /* Size of current row in bytes */ + Mem sMem; /* Raw content of current row */ + memset(&sMem, 0, sizeof(sMem)); + sz = sqlite3BtreePayloadSize(pRhs->pCsr); + rc = sqlite3VdbeMemFromBtreeZeroOffset(pRhs->pCsr,(int)sz,&sMem); + if( rc==SQLITE_OK ){ + u8 *zBuf = (u8*)sMem.z; + u32 iSerial; + sqlite3_value *pOut = pRhs->pOut; + int iOff = 1 + getVarint32(&zBuf[1], iSerial); + sqlite3VdbeSerialGet(&zBuf[iOff], iSerial, pOut); + pOut->enc = ENC(pOut->db); + if( (pOut->flags & MEM_Ephem)!=0 && sqlite3VdbeMemMakeWriteable(pOut) ){ + rc = SQLITE_NOMEM; + }else{ + *ppOut = pOut; + } + } + sqlite3VdbeMemRelease(&sMem); + } + return rc; +} + +/* +** Set the iterator value pVal to point to the first value in the set. +** Set (*ppOut) to point to this value before returning. +*/ +int sqlite3_vtab_in_first(sqlite3_value *pVal, sqlite3_value **ppOut){ + return valueFromValueList(pVal, ppOut, 0); +} + +/* +** Set the iterator value pVal to point to the next value in the set. +** Set (*ppOut) to point to this value before returning. +*/ +int sqlite3_vtab_in_next(sqlite3_value *pVal, sqlite3_value **ppOut){ + return valueFromValueList(pVal, ppOut, 1); +} + /* ** Return the current time for a statement. If the current time ** is requested more than once within the same run of a single prepared @@ -719,7 +1087,7 @@ sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){ */ sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context *p){ int rc; -#ifndef SQLITE_ENABLE_STAT3_OR_STAT4 +#ifndef SQLITE_ENABLE_STAT4 sqlite3_int64 *piTime = &p->pVdbe->iCurrentTime; assert( p->pVdbe!=0 ); #else @@ -733,28 +1101,6 @@ sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context *p){ return *piTime; } -/* -** The following is the implementation of an SQL function that always -** fails with an error message stating that the function is used in the -** wrong context. The sqlite3_overload_function() API might construct -** SQL function that use this routine so that the functions will exist -** for name resolution but are actually overloaded by the xFindFunction -** method of virtual tables. -*/ -void sqlite3InvalidFunction( - sqlite3_context *context, /* The function calling context */ - int NotUsed, /* Number of arguments to the function */ - sqlite3_value **NotUsed2 /* Value of each argument */ -){ - const char *zName = context->pFunc->zName; - char *zErr; - UNUSED_PARAMETER2(NotUsed, NotUsed2); - zErr = sqlite3_mprintf( - "unable to use function %s in the requested context", zName); - sqlite3_result_error(context, zErr, -1); - sqlite3_free(zErr); -} - /* ** Create a new aggregate context for p and return a pointer to ** its pMem->z element. @@ -795,65 +1141,83 @@ void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){ /* ** Return the auxiliary data pointer, if any, for the iArg'th argument to ** the user-function defined by pCtx. +** +** The left-most argument is 0. +** +** Undocumented behavior: If iArg is negative then access a cache of +** auxiliary data pointers that is available to all functions within a +** single prepared statement. The iArg values must match. */ void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){ AuxData *pAuxData; +#ifdef SQLITE_ENABLE_API_ARMOR + if( pCtx==0 ) return 0; +#endif assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); -#if SQLITE_ENABLE_STAT3_OR_STAT4 +#if SQLITE_ENABLE_STAT4 if( pCtx->pVdbe==0 ) return 0; #else assert( pCtx->pVdbe!=0 ); #endif - for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNext){ - if( pAuxData->iOp==pCtx->iOp && pAuxData->iArg==iArg ) break; + for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){ + if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){ + return pAuxData->pAux; + } } - - return (pAuxData ? pAuxData->pAux : 0); + return 0; } /* ** Set the auxiliary data pointer and delete function, for the iArg'th ** argument to the user-function defined by pCtx. Any previous value is ** deleted by calling the delete function specified when it was set. +** +** The left-most argument is 0. +** +** Undocumented behavior: If iArg is negative then make the data available +** to all functions within the current prepared statement using iArg as an +** access code. */ void sqlite3_set_auxdata( - sqlite3_context *pCtx, - int iArg, - void *pAux, + sqlite3_context *pCtx, + int iArg, + void *pAux, void (*xDelete)(void*) ){ AuxData *pAuxData; - Vdbe *pVdbe = pCtx->pVdbe; + Vdbe *pVdbe; +#ifdef SQLITE_ENABLE_API_ARMOR + if( pCtx==0 ) return; +#endif + pVdbe= pCtx->pVdbe; assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); - if( iArg<0 ) goto failed; -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 +#ifdef SQLITE_ENABLE_STAT4 if( pVdbe==0 ) goto failed; #else assert( pVdbe!=0 ); #endif - for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNext){ - if( pAuxData->iOp==pCtx->iOp && pAuxData->iArg==iArg ) break; + for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){ + if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){ + break; + } } if( pAuxData==0 ){ pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData)); if( !pAuxData ) goto failed; - pAuxData->iOp = pCtx->iOp; - pAuxData->iArg = iArg; - pAuxData->pNext = pVdbe->pAuxData; + pAuxData->iAuxOp = pCtx->iOp; + pAuxData->iAuxArg = iArg; + pAuxData->pNextAux = pVdbe->pAuxData; pVdbe->pAuxData = pAuxData; - if( pCtx->fErrorOrAux==0 ){ - pCtx->isError = 0; - pCtx->fErrorOrAux = 1; - } - }else if( pAuxData->xDelete ){ - pAuxData->xDelete(pAuxData->pAux); + if( pCtx->isError==0 ) pCtx->isError = -1; + }else if( pAuxData->xDeleteAux ){ + pAuxData->xDeleteAux(pAuxData->pAux); } pAuxData->pAux = pAux; - pAuxData->xDelete = xDelete; + pAuxData->xDeleteAux = xDelete; return; failed: @@ -864,7 +1228,7 @@ void sqlite3_set_auxdata( #ifndef SQLITE_OMIT_DEPRECATED /* -** Return the number of times the Step function of an aggregate has been +** Return the number of times the Step function of an aggregate has been ** called. ** ** This function is deprecated. Do not use it for new code. It is @@ -883,7 +1247,8 @@ int sqlite3_aggregate_count(sqlite3_context *p){ */ int sqlite3_column_count(sqlite3_stmt *pStmt){ Vdbe *pVm = (Vdbe *)pStmt; - return pVm ? pVm->nResColumn : 0; + if( pVm==0 ) return 0; + return pVm->nResColumn; } /* @@ -892,7 +1257,7 @@ int sqlite3_column_count(sqlite3_stmt *pStmt){ */ int sqlite3_data_count(sqlite3_stmt *pStmt){ Vdbe *pVm = (Vdbe *)pStmt; - if( pVm==0 || pVm->pResultSet==0 ) return 0; + if( pVm==0 || pVm->pResultRow==0 ) return 0; return pVm->nResColumn; } @@ -909,25 +1274,26 @@ static const Mem *columnNullValue(void){ ** these assert()s from failing, when building with SQLITE_DEBUG defined ** using gcc, we force nullMem to be 8-byte aligned using the magical ** __attribute__((aligned(8))) macro. */ - static const Mem nullMem + static const Mem nullMem #if defined(SQLITE_DEBUG) && defined(__GNUC__) - __attribute__((aligned(8))) + __attribute__((aligned(8))) #endif = { /* .u = */ {0}, + /* .z = */ (char*)0, + /* .n = */ (int)0, /* .flags = */ (u16)MEM_Null, /* .enc = */ (u8)0, /* .eSubtype = */ (u8)0, - /* .n = */ (int)0, - /* .z = */ (char*)0, - /* .zMalloc = */ (char*)0, + /* .db = */ (sqlite3*)0, /* .szMalloc = */ (int)0, /* .uTemp = */ (u32)0, - /* .db = */ (sqlite3*)0, + /* .zMalloc = */ (char*)0, /* .xDel = */ (void(*)(void*))0, #ifdef SQLITE_DEBUG /* .pScopyFrom = */ (Mem*)0, - /* .pFiller = */ (void*)0, + /* .mScopyFlags= */ 0, + /* .bScopy = */ 0, #endif }; return &nullMem; @@ -944,23 +1310,22 @@ static Mem *columnMem(sqlite3_stmt *pStmt, int i){ Mem *pOut; pVm = (Vdbe *)pStmt; - if( pVm && pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){ - sqlite3_mutex_enter(pVm->db->mutex); - pOut = &pVm->pResultSet[i]; + if( pVm==0 ) return (Mem*)columnNullValue(); + assert( pVm->db ); + sqlite3_mutex_enter(pVm->db->mutex); + if( pVm->pResultRow!=0 && i<pVm->nResColumn && i>=0 ){ + pOut = &pVm->pResultRow[i]; }else{ - if( pVm && ALWAYS(pVm->db) ){ - sqlite3_mutex_enter(pVm->db->mutex); - sqlite3Error(pVm->db, SQLITE_RANGE); - } + sqlite3Error(pVm->db, SQLITE_RANGE); pOut = (Mem*)columnNullValue(); } return pOut; } /* -** This function is called after invoking an sqlite3_value_XXX function on a +** This function is called after invoking an sqlite3_value_XXX function on a ** column value (i.e. a value returned by evaluating an SQL expression in the -** select list of a SELECT statement) that may cause a malloc() failure. If +** select list of a SELECT statement) that may cause a malloc() failure. If ** malloc() has failed, the threads mallocFailed flag is cleared and the result ** code of statement pStmt set to SQLITE_NOMEM. ** @@ -970,10 +1335,10 @@ static Mem *columnMem(sqlite3_stmt *pStmt, int i){ ** sqlite3_column_int64() ** sqlite3_column_text() ** sqlite3_column_text16() -** sqlite3_column_real() +** sqlite3_column_double() ** sqlite3_column_bytes() ** sqlite3_column_bytes16() -** sqiite3_column_blob() +** sqlite3_column_blob() */ static void columnMallocFailure(sqlite3_stmt *pStmt) { @@ -984,6 +1349,8 @@ static void columnMallocFailure(sqlite3_stmt *pStmt) */ Vdbe *p = (Vdbe *)pStmt; if( p ){ + assert( p->db!=0 ); + assert( sqlite3_mutex_held(p->db->mutex) ); p->rc = sqlite3ApiExit(p->db, p->rc); sqlite3_mutex_leave(p->db->mutex); } @@ -997,8 +1364,8 @@ const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){ const void *val; val = sqlite3_value_blob( columnMem(pStmt,i) ); /* Even though there is no encoding conversion, value_blob() might - ** need to call malloc() to expand the result of a zeroblob() - ** expression. + ** need to call malloc() to expand the result of a zeroblob() + ** expression. */ columnMallocFailure(pStmt); return val; @@ -1055,6 +1422,32 @@ int sqlite3_column_type(sqlite3_stmt *pStmt, int i){ return iType; } +/* +** Column names appropriate for EXPLAIN or EXPLAIN QUERY PLAN. +*/ +static const char * const azExplainColNames8[] = { + "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment", /* EXPLAIN */ + "id", "parent", "notused", "detail" /* EQP */ +}; +static const u16 azExplainColNames16data[] = { + /* 0 */ 'a', 'd', 'd', 'r', 0, + /* 5 */ 'o', 'p', 'c', 'o', 'd', 'e', 0, + /* 12 */ 'p', '1', 0, + /* 15 */ 'p', '2', 0, + /* 18 */ 'p', '3', 0, + /* 21 */ 'p', '4', 0, + /* 24 */ 'p', '5', 0, + /* 27 */ 'c', 'o', 'm', 'm', 'e', 'n', 't', 0, + /* 35 */ 'i', 'd', 0, + /* 38 */ 'p', 'a', 'r', 'e', 'n', 't', 0, + /* 45 */ 'n', 'o', 't', 'u', 's', 'e', 'd', 0, + /* 53 */ 'd', 'e', 't', 'a', 'i', 'l', 0 +}; +static const u8 iExplainColNames16[] = { + 0, 5, 12, 15, 18, 21, 24, 27, + 35, 38, 45, 53 +}; + /* ** Convert the N-th element of pStmt->pColName[] into a string using ** xFunc() then return that string. If N is out of range, return 0. @@ -1072,10 +1465,10 @@ int sqlite3_column_type(sqlite3_stmt *pStmt, int i){ ** or a constant) then useTypes 2, 3, and 4 return NULL. */ static const void *columnName( - sqlite3_stmt *pStmt, - int N, - const void *(*xFunc)(Mem*), - int useType + sqlite3_stmt *pStmt, /* The statement */ + int N, /* Which column to get the name for */ + int useUtf16, /* True to return the name as UTF16 */ + int useType /* What type of name */ ){ const void *ret; Vdbe *p; @@ -1087,25 +1480,48 @@ static const void *columnName( return 0; } #endif + if( N<0 ) return 0; ret = 0; p = (Vdbe *)pStmt; db = p->db; assert( db!=0 ); - n = sqlite3_column_count(pStmt); - if( N<n && N>=0 ){ + sqlite3_mutex_enter(db->mutex); + + if( p->explain ){ + if( useType>0 ) goto columnName_end; + n = p->explain==1 ? 8 : 4; + if( N>=n ) goto columnName_end; + if( useUtf16 ){ + int i = iExplainColNames16[N + 8*p->explain - 8]; + ret = (void*)&azExplainColNames16data[i]; + }else{ + ret = (void*)azExplainColNames8[N + 8*p->explain - 8]; + } + goto columnName_end; + } + n = p->nResColumn; + if( N<n ){ + u8 prior_mallocFailed = db->mallocFailed; N += useType*n; - sqlite3_mutex_enter(db->mutex); - assert( db->mallocFailed==0 ); - ret = xFunc(&p->aColName[N]); - /* A malloc may have failed inside of the xFunc() call. If this +#ifndef SQLITE_OMIT_UTF16 + if( useUtf16 ){ + ret = sqlite3_value_text16((sqlite3_value*)&p->aColName[N]); + }else +#endif + { + ret = sqlite3_value_text((sqlite3_value*)&p->aColName[N]); + } + /* A malloc may have failed inside of the _text() call. If this ** is the case, clear the mallocFailed flag and return NULL. */ - if( db->mallocFailed ){ + assert( db->mallocFailed==0 || db->mallocFailed==1 ); + if( db->mallocFailed > prior_mallocFailed ){ sqlite3OomClear(db); ret = 0; } - sqlite3_mutex_leave(db->mutex); } +columnName_end: + sqlite3_mutex_leave(db->mutex); return ret; } @@ -1114,13 +1530,11 @@ static const void *columnName( ** statement pStmt. */ const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){ - return columnName( - pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME); + return columnName(pStmt, N, 0, COLNAME_NAME); } #ifndef SQLITE_OMIT_UTF16 const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){ - return columnName( - pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME); + return columnName(pStmt, N, 1, COLNAME_NAME); } #endif @@ -1139,13 +1553,11 @@ const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){ ** of the result set of SQL statement pStmt. */ const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){ - return columnName( - pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE); + return columnName(pStmt, N, 0, COLNAME_DECLTYPE); } #ifndef SQLITE_OMIT_UTF16 const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){ - return columnName( - pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE); + return columnName(pStmt, N, 1, COLNAME_DECLTYPE); } #endif /* SQLITE_OMIT_UTF16 */ #endif /* SQLITE_OMIT_DECLTYPE */ @@ -1157,13 +1569,11 @@ const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){ ** anything else which is not an unambiguous reference to a database column. */ const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){ - return columnName( - pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE); + return columnName(pStmt, N, 0, COLNAME_DATABASE); } #ifndef SQLITE_OMIT_UTF16 const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){ - return columnName( - pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE); + return columnName(pStmt, N, 1, COLNAME_DATABASE); } #endif /* SQLITE_OMIT_UTF16 */ @@ -1173,13 +1583,11 @@ const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){ ** anything else which is not an unambiguous reference to a database column. */ const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){ - return columnName( - pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE); + return columnName(pStmt, N, 0, COLNAME_TABLE); } #ifndef SQLITE_OMIT_UTF16 const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){ - return columnName( - pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE); + return columnName(pStmt, N, 1, COLNAME_TABLE); } #endif /* SQLITE_OMIT_UTF16 */ @@ -1189,69 +1597,76 @@ const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){ ** anything else which is not an unambiguous reference to a database column. */ const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){ - return columnName( - pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN); + return columnName(pStmt, N, 0, COLNAME_COLUMN); } #ifndef SQLITE_OMIT_UTF16 const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){ - return columnName( - pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN); + return columnName(pStmt, N, 1, COLNAME_COLUMN); } #endif /* SQLITE_OMIT_UTF16 */ #endif /* SQLITE_ENABLE_COLUMN_METADATA */ /******************************* sqlite3_bind_ *************************** -** +** ** Routines used to attach values to wildcards in a compiled SQL statement. */ /* -** Unbind the value bound to variable i in virtual machine p. This is the +** Unbind the value bound to variable i in virtual machine p. This is the ** the same as binding a NULL value to the column. If the "i" parameter is -** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK. +** out of range, then SQLITE_RANGE is returned. Otherwise SQLITE_OK. ** ** A successful evaluation of this routine acquires the mutex on p. ** the mutex is released if any kind of error occurs. ** ** The error code stored in database p->db is overwritten with the return ** value in any case. +** +** (tag-20240917-01) If vdbeUnbind(p,(u32)(i-1)) returns SQLITE_OK, +** that means all of the the following will be true: +** +** p!=0 +** p->pVar!=0 +** i>0 +** i<=p->nVar +** +** An assert() is normally added after vdbeUnbind() to help static analyzers +** realize this. */ -static int vdbeUnbind(Vdbe *p, int i){ +static int vdbeUnbind(Vdbe *p, unsigned int i){ Mem *pVar; if( vdbeSafetyNotNull(p) ){ return SQLITE_MISUSE_BKPT; } sqlite3_mutex_enter(p->db->mutex); - if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){ - sqlite3Error(p->db, SQLITE_MISUSE); + if( p->eVdbeState!=VDBE_READY_STATE ){ + sqlite3Error(p->db, SQLITE_MISUSE_BKPT); sqlite3_mutex_leave(p->db->mutex); - sqlite3_log(SQLITE_MISUSE, + sqlite3_log(SQLITE_MISUSE, "bind on a busy prepared statement: [%s]", p->zSql); return SQLITE_MISUSE_BKPT; } - if( i<1 || i>p->nVar ){ + if( i>=(unsigned int)p->nVar ){ sqlite3Error(p->db, SQLITE_RANGE); sqlite3_mutex_leave(p->db->mutex); return SQLITE_RANGE; } - i--; pVar = &p->aVar[i]; sqlite3VdbeMemRelease(pVar); pVar->flags = MEM_Null; - sqlite3Error(p->db, SQLITE_OK); + p->db->errCode = SQLITE_OK; - /* If the bit corresponding to this variable in Vdbe.expmask is set, then + /* If the bit corresponding to this variable in Vdbe.expmask is set, then ** binding a new value to this variable invalidates the current query plan. ** - ** IMPLEMENTATION-OF: R-48440-37595 If the specific value bound to host + ** IMPLEMENTATION-OF: R-57496-20354 If the specific value bound to a host ** parameter in the WHERE clause might influence the choice of query plan ** for a statement, then the statement will be automatically recompiled, ** as if there had been a schema change, on the first sqlite3_step() call ** following any change to the bindings of that parameter. */ - if( p->isPrepareV2 && - ((i<32 && p->expmask & ((u32)1 << i)) || p->expmask==0xffffffff) - ){ + assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 ); + if( p->expmask!=0 && (p->expmask & (i>=31 ? 0x80000000 : (u32)1<<i))!=0 ){ p->expired = 1; } return SQLITE_OK; @@ -1264,7 +1679,7 @@ static int bindText( sqlite3_stmt *pStmt, /* The statement to bind against */ int i, /* Index of the parameter to bind */ const void *zData, /* Pointer to the data to be bound */ - int nData, /* Number of bytes of data to be bound */ + i64 nData, /* Number of bytes of data to be bound */ void (*xDel)(void*), /* Destructor for the data */ u8 encoding /* Encoding for the data */ ){ @@ -1272,16 +1687,23 @@ static int bindText( Mem *pVar; int rc; - rc = vdbeUnbind(p, i); + rc = vdbeUnbind(p, (u32)(i-1)); if( rc==SQLITE_OK ){ + assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar ); /* tag-20240917-01 */ if( zData!=0 ){ pVar = &p->aVar[i-1]; rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel); - if( rc==SQLITE_OK && encoding!=0 ){ - rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db)); + if( rc==SQLITE_OK ){ + if( encoding==0 ){ + pVar->enc = ENC(p->db); + }else{ + rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db)); + } + } + if( rc ){ + sqlite3Error(p->db, rc); + rc = sqlite3ApiExit(p->db, rc); } - sqlite3Error(p->db, rc); - rc = sqlite3ApiExit(p->db, rc); } sqlite3_mutex_leave(p->db->mutex); }else if( xDel!=SQLITE_STATIC && xDel!=SQLITE_TRANSIENT ){ @@ -1295,33 +1717,33 @@ static int bindText( ** Bind a blob value to an SQL statement variable. */ int sqlite3_bind_blob( - sqlite3_stmt *pStmt, - int i, - const void *zData, - int nData, + sqlite3_stmt *pStmt, + int i, + const void *zData, + int nData, void (*xDel)(void*) ){ +#ifdef SQLITE_ENABLE_API_ARMOR + if( nData<0 ) return SQLITE_MISUSE_BKPT; +#endif return bindText(pStmt, i, zData, nData, xDel, 0); } int sqlite3_bind_blob64( - sqlite3_stmt *pStmt, - int i, - const void *zData, - sqlite3_uint64 nData, + sqlite3_stmt *pStmt, + int i, + const void *zData, + sqlite3_uint64 nData, void (*xDel)(void*) ){ assert( xDel!=SQLITE_DYNAMIC ); - if( nData>0x7fffffff ){ - return invokeValueDestructor(zData, xDel, 0); - }else{ - return bindText(pStmt, i, zData, (int)nData, xDel, 0); - } + return bindText(pStmt, i, zData, nData, xDel, 0); } int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){ int rc; Vdbe *p = (Vdbe *)pStmt; - rc = vdbeUnbind(p, i); + rc = vdbeUnbind(p, (u32)(i-1)); if( rc==SQLITE_OK ){ + assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar ); /* tag-20240917-01 */ sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue); sqlite3_mutex_leave(p->db->mutex); } @@ -1333,8 +1755,9 @@ int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){ int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){ int rc; Vdbe *p = (Vdbe *)pStmt; - rc = vdbeUnbind(p, i); + rc = vdbeUnbind(p, (u32)(i-1)); if( rc==SQLITE_OK ){ + assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar ); /* tag-20240917-01 */ sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue); sqlite3_mutex_leave(p->db->mutex); } @@ -1343,46 +1766,65 @@ int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){ int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){ int rc; Vdbe *p = (Vdbe*)pStmt; - rc = vdbeUnbind(p, i); + rc = vdbeUnbind(p, (u32)(i-1)); + if( rc==SQLITE_OK ){ + assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar ); /* tag-20240917-01 */ + sqlite3_mutex_leave(p->db->mutex); + } + return rc; +} +int sqlite3_bind_pointer( + sqlite3_stmt *pStmt, + int i, + void *pPtr, + const char *zPTtype, + void (*xDestructor)(void*) +){ + int rc; + Vdbe *p = (Vdbe*)pStmt; + rc = vdbeUnbind(p, (u32)(i-1)); if( rc==SQLITE_OK ){ + assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar ); /* tag-20240917-01 */ + sqlite3VdbeMemSetPointer(&p->aVar[i-1], pPtr, zPTtype, xDestructor); sqlite3_mutex_leave(p->db->mutex); + }else if( xDestructor ){ + xDestructor(pPtr); } return rc; } -int sqlite3_bind_text( - sqlite3_stmt *pStmt, - int i, - const char *zData, - int nData, +int sqlite3_bind_text( + sqlite3_stmt *pStmt, + int i, + const char *zData, + int nData, void (*xDel)(void*) ){ return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8); } -int sqlite3_bind_text64( - sqlite3_stmt *pStmt, - int i, - const char *zData, - sqlite3_uint64 nData, +int sqlite3_bind_text64( + sqlite3_stmt *pStmt, + int i, + const char *zData, + sqlite3_uint64 nData, void (*xDel)(void*), unsigned char enc ){ assert( xDel!=SQLITE_DYNAMIC ); - if( nData>0x7fffffff ){ - return invokeValueDestructor(zData, xDel, 0); - }else{ + if( enc!=SQLITE_UTF8 ){ if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE; - return bindText(pStmt, i, zData, (int)nData, xDel, enc); + nData &= ~(u64)1; } + return bindText(pStmt, i, zData, nData, xDel, enc); } #ifndef SQLITE_OMIT_UTF16 int sqlite3_bind_text16( - sqlite3_stmt *pStmt, - int i, - const void *zData, - int nData, + sqlite3_stmt *pStmt, + int i, + const void *zData, + int n, void (*xDel)(void*) ){ - return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE); + return bindText(pStmt, i, zData, n & ~(u64)1, xDel, SQLITE_UTF16NATIVE); } #endif /* SQLITE_OMIT_UTF16 */ int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){ @@ -1393,7 +1835,10 @@ int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){ break; } case SQLITE_FLOAT: { - rc = sqlite3_bind_double(pStmt, i, pValue->u.r); + assert( pValue->flags & (MEM_Real|MEM_IntReal) ); + rc = sqlite3_bind_double(pStmt, i, + (pValue->flags & MEM_Real) ? pValue->u.r : (double)pValue->u.i + ); break; } case SQLITE_BLOB: { @@ -1419,9 +1864,14 @@ int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){ int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){ int rc; Vdbe *p = (Vdbe *)pStmt; - rc = vdbeUnbind(p, i); + rc = vdbeUnbind(p, (u32)(i-1)); if( rc==SQLITE_OK ){ + assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar ); /* tag-20240917-01 */ +#ifndef SQLITE_OMIT_INCRBLOB sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n); +#else + rc = sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n); +#endif sqlite3_mutex_leave(p->db->mutex); } return rc; @@ -1429,6 +1879,9 @@ int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){ int sqlite3_bind_zeroblob64(sqlite3_stmt *pStmt, int i, sqlite3_uint64 n){ int rc; Vdbe *p = (Vdbe *)pStmt; +#ifdef SQLITE_ENABLE_API_ARMOR + if( p==0 ) return SQLITE_MISUSE_BKPT; +#endif sqlite3_mutex_enter(p->db->mutex); if( n>(u64)p->db->aLimit[SQLITE_LIMIT_LENGTH] ){ rc = SQLITE_TOOBIG; @@ -1443,7 +1896,7 @@ int sqlite3_bind_zeroblob64(sqlite3_stmt *pStmt, int i, sqlite3_uint64 n){ /* ** Return the number of wildcards that can be potentially bound to. -** This routine is added to support DBD::SQLite. +** This routine is added to support DBD::SQLite. */ int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){ Vdbe *p = (Vdbe*)pStmt; @@ -1458,10 +1911,8 @@ int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){ */ const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){ Vdbe *p = (Vdbe*)pStmt; - if( p==0 || i<1 || i>p->nzVar ){ - return 0; - } - return p->azVar[i-1]; + if( p==0 ) return 0; + return sqlite3VListNumToName(p->pVList, i); } /* @@ -1470,19 +1921,8 @@ const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){ ** return 0. */ int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nName){ - int i; - if( p==0 ){ - return 0; - } - if( zName ){ - for(i=0; i<p->nzVar; i++){ - const char *z = p->azVar[i]; - if( z && strncmp(z,zName,nName)==0 && z[nName]==0 ){ - return i+1; - } - } - } - return 0; + if( p==0 || zName==0 ) return 0; + return sqlite3VListNameToNum(p->pVList, zName, nName); } int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){ return sqlite3VdbeParameterIndex((Vdbe*)pStmt, zName, sqlite3Strlen30(zName)); @@ -1524,10 +1964,12 @@ int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){ if( pFrom->nVar!=pTo->nVar ){ return SQLITE_ERROR; } - if( pTo->isPrepareV2 && pTo->expmask ){ + assert( (pTo->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pTo->expmask==0 ); + if( pTo->expmask ){ pTo->expired = 1; } - if( pFrom->isPrepareV2 && pFrom->expmask ){ + assert( (pFrom->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pFrom->expmask==0 ); + if( pFrom->expmask ){ pFrom->expired = 1; } return sqlite3TransferBindings(pFromStmt, pToStmt); @@ -1552,12 +1994,56 @@ int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){ return pStmt ? ((Vdbe*)pStmt)->readOnly : 1; } +/* +** Return 1 if the statement is an EXPLAIN and return 2 if the +** statement is an EXPLAIN QUERY PLAN +*/ +int sqlite3_stmt_isexplain(sqlite3_stmt *pStmt){ + return pStmt ? ((Vdbe*)pStmt)->explain : 0; +} + +/* +** Set the explain mode for a statement. +*/ +int sqlite3_stmt_explain(sqlite3_stmt *pStmt, int eMode){ + Vdbe *v = (Vdbe*)pStmt; + int rc; +#ifdef SQLITE_ENABLE_API_ARMOR + if( pStmt==0 ) return SQLITE_MISUSE_BKPT; +#endif + sqlite3_mutex_enter(v->db->mutex); + if( ((int)v->explain)==eMode ){ + rc = SQLITE_OK; + }else if( eMode<0 || eMode>2 ){ + rc = SQLITE_ERROR; + }else if( (v->prepFlags & SQLITE_PREPARE_SAVESQL)==0 ){ + rc = SQLITE_ERROR; + }else if( v->eVdbeState!=VDBE_READY_STATE ){ + rc = SQLITE_BUSY; + }else if( v->nMem>=10 && (eMode!=2 || v->haveEqpOps) ){ + /* No reprepare necessary */ + v->explain = eMode; + rc = SQLITE_OK; + }else{ + v->explain = eMode; + rc = sqlite3Reprepare(v); + v->haveEqpOps = eMode==2; + } + if( v->explain ){ + v->nResColumn = 12 - 4*v->explain; + }else{ + v->nResColumn = v->nResAlloc; + } + sqlite3_mutex_leave(v->db->mutex); + return rc; +} + /* ** Return true if the prepared statement is in need of being reset. */ int sqlite3_stmt_busy(sqlite3_stmt *pStmt){ Vdbe *v = (Vdbe*)pStmt; - return v!=0 && v->pc>=0 && v->magic==VDBE_MAGIC_RUN; + return v!=0 && v->eVdbeState==VDBE_RUN_STATE; } /* @@ -1578,7 +2064,7 @@ sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){ if( pStmt==0 ){ pNext = (sqlite3_stmt*)pDb->pVdbe; }else{ - pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext; + pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pVNext; } sqlite3_mutex_leave(pDb->mutex); return pNext; @@ -1591,37 +2077,426 @@ int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){ Vdbe *pVdbe = (Vdbe*)pStmt; u32 v; #ifdef SQLITE_ENABLE_API_ARMOR - if( !pStmt ){ + if( !pStmt + || (op!=SQLITE_STMTSTATUS_MEMUSED && (op<0||op>=ArraySize(pVdbe->aCounter))) + ){ (void)SQLITE_MISUSE_BKPT; return 0; } #endif - v = pVdbe->aCounter[op]; - if( resetFlag ) pVdbe->aCounter[op] = 0; + if( op==SQLITE_STMTSTATUS_MEMUSED ){ + sqlite3 *db = pVdbe->db; + sqlite3_mutex_enter(db->mutex); + v = 0; + db->pnBytesFreed = (int*)&v; + assert( db->lookaside.pEnd==db->lookaside.pTrueEnd ); + db->lookaside.pEnd = db->lookaside.pStart; + sqlite3VdbeDelete(pVdbe); + db->pnBytesFreed = 0; + db->lookaside.pEnd = db->lookaside.pTrueEnd; + sqlite3_mutex_leave(db->mutex); + }else{ + v = pVdbe->aCounter[op]; + if( resetFlag ) pVdbe->aCounter[op] = 0; + } return (int)v; } +/* +** Return the SQL associated with a prepared statement +*/ +const char *sqlite3_sql(sqlite3_stmt *pStmt){ + Vdbe *p = (Vdbe *)pStmt; + return p ? p->zSql : 0; +} + +/* +** Return the SQL associated with a prepared statement with +** bound parameters expanded. Space to hold the returned string is +** obtained from sqlite3_malloc(). The caller is responsible for +** freeing the returned string by passing it to sqlite3_free(). +** +** The SQLITE_TRACE_SIZE_LIMIT puts an upper bound on the size of +** expanded bound parameters. +*/ +char *sqlite3_expanded_sql(sqlite3_stmt *pStmt){ +#ifdef SQLITE_OMIT_TRACE + return 0; +#else + char *z = 0; + const char *zSql = sqlite3_sql(pStmt); + if( zSql ){ + Vdbe *p = (Vdbe *)pStmt; + sqlite3_mutex_enter(p->db->mutex); + z = sqlite3VdbeExpandSql(p, zSql); + sqlite3_mutex_leave(p->db->mutex); + } + return z; +#endif +} + +#ifdef SQLITE_ENABLE_NORMALIZE +/* +** Return the normalized SQL associated with a prepared statement. +*/ +const char *sqlite3_normalized_sql(sqlite3_stmt *pStmt){ + Vdbe *p = (Vdbe *)pStmt; + if( p==0 ) return 0; + if( p->zNormSql==0 && ALWAYS(p->zSql!=0) ){ + sqlite3_mutex_enter(p->db->mutex); + p->zNormSql = sqlite3Normalize(p, p->zSql); + sqlite3_mutex_leave(p->db->mutex); + } + return p->zNormSql; +} +#endif /* SQLITE_ENABLE_NORMALIZE */ + +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK +/* +** Allocate and populate an UnpackedRecord structure based on the serialized +** record in nKey/pKey. Return a pointer to the new UnpackedRecord structure +** if successful, or a NULL pointer if an OOM error is encountered. +*/ +static UnpackedRecord *vdbeUnpackRecord( + KeyInfo *pKeyInfo, + int nKey, + const void *pKey +){ + UnpackedRecord *pRet; /* Return value */ + + pRet = sqlite3VdbeAllocUnpackedRecord(pKeyInfo); + if( pRet ){ + memset(pRet->aMem, 0, sizeof(Mem)*(pKeyInfo->nKeyField+1)); + sqlite3VdbeRecordUnpack(nKey, pKey, pRet); + } + return pRet; +} + +/* +** This function is called from within a pre-update callback to retrieve +** a field of the row currently being updated or deleted. +*/ +int sqlite3_preupdate_old(sqlite3 *db, int iIdx, sqlite3_value **ppValue){ + PreUpdate *p; + Mem *pMem; + int rc = SQLITE_OK; + int iStore = 0; + +#ifdef SQLITE_ENABLE_API_ARMOR + if( db==0 || ppValue==0 ){ + return SQLITE_MISUSE_BKPT; + } +#endif + p = db->pPreUpdate; + /* Test that this call is being made from within an SQLITE_DELETE or + ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */ + if( !p || p->op==SQLITE_INSERT ){ + rc = SQLITE_MISUSE_BKPT; + goto preupdate_old_out; + } + if( p->pPk ){ + iStore = sqlite3TableColumnToIndex(p->pPk, iIdx); + }else if( iIdx >= p->pTab->nCol ){ + rc = SQLITE_MISUSE_BKPT; + goto preupdate_old_out; + }else{ + iStore = sqlite3TableColumnToStorage(p->pTab, iIdx); + } + if( iStore>=p->pCsr->nField || iStore<0 ){ + rc = SQLITE_RANGE; + goto preupdate_old_out; + } + + if( iIdx==p->pTab->iPKey ){ + *ppValue = pMem = &p->oldipk; + sqlite3VdbeMemSetInt64(pMem, p->iKey1); + }else{ + + /* If the old.* record has not yet been loaded into memory, do so now. */ + if( p->pUnpacked==0 ){ + u32 nRec; + u8 *aRec; + + assert( p->pCsr->eCurType==CURTYPE_BTREE ); + nRec = sqlite3BtreePayloadSize(p->pCsr->uc.pCursor); + aRec = sqlite3DbMallocRaw(db, nRec); + if( !aRec ) goto preupdate_old_out; + rc = sqlite3BtreePayload(p->pCsr->uc.pCursor, 0, nRec, aRec); + if( rc==SQLITE_OK ){ + p->pUnpacked = vdbeUnpackRecord(p->pKeyinfo, nRec, aRec); + if( !p->pUnpacked ) rc = SQLITE_NOMEM; + } + if( rc!=SQLITE_OK ){ + sqlite3DbFree(db, aRec); + goto preupdate_old_out; + } + p->aRecord = aRec; + } + + pMem = *ppValue = &p->pUnpacked->aMem[iStore]; + if( iStore>=p->pUnpacked->nField ){ + /* This occurs when the table has been extended using ALTER TABLE + ** ADD COLUMN. The value to return is the default value of the column. */ + Column *pCol = &p->pTab->aCol[iIdx]; + if( pCol->iDflt>0 ){ + if( p->apDflt==0 ){ + int nByte; + assert( sizeof(sqlite3_value*)*UMXV(p->pTab->nCol) < 0x7fffffff ); + nByte = sizeof(sqlite3_value*)*p->pTab->nCol; + p->apDflt = (sqlite3_value**)sqlite3DbMallocZero(db, nByte); + if( p->apDflt==0 ) goto preupdate_old_out; + } + if( p->apDflt[iIdx]==0 ){ + sqlite3_value *pVal = 0; + Expr *pDflt; + assert( p->pTab!=0 && IsOrdinaryTable(p->pTab) ); + pDflt = p->pTab->u.tab.pDfltList->a[pCol->iDflt-1].pExpr; + rc = sqlite3ValueFromExpr(db, pDflt, ENC(db), pCol->affinity, &pVal); + if( rc==SQLITE_OK && pVal==0 ){ + rc = SQLITE_CORRUPT_BKPT; + } + p->apDflt[iIdx] = pVal; + } + *ppValue = p->apDflt[iIdx]; + }else{ + *ppValue = (sqlite3_value *)columnNullValue(); + } + }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){ + if( pMem->flags & (MEM_Int|MEM_IntReal) ){ + testcase( pMem->flags & MEM_Int ); + testcase( pMem->flags & MEM_IntReal ); + sqlite3VdbeMemRealify(pMem); + } + } + } + + preupdate_old_out: + sqlite3Error(db, rc); + return sqlite3ApiExit(db, rc); +} +#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ + +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK +/* +** This function is called from within a pre-update callback to retrieve +** the number of columns in the row being updated, deleted or inserted. +*/ +int sqlite3_preupdate_count(sqlite3 *db){ + PreUpdate *p; +#ifdef SQLITE_ENABLE_API_ARMOR + p = db!=0 ? db->pPreUpdate : 0; +#else + p = db->pPreUpdate; +#endif + return (p ? p->pKeyinfo->nKeyField : 0); +} +#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ + +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK +/* +** This function is designed to be called from within a pre-update callback +** only. It returns zero if the change that caused the callback was made +** immediately by a user SQL statement. Or, if the change was made by a +** trigger program, it returns the number of trigger programs currently +** on the stack (1 for a top-level trigger, 2 for a trigger fired by a +** top-level trigger etc.). +** +** For the purposes of the previous paragraph, a foreign key CASCADE, SET NULL +** or SET DEFAULT action is considered a trigger. +*/ +int sqlite3_preupdate_depth(sqlite3 *db){ + PreUpdate *p; +#ifdef SQLITE_ENABLE_API_ARMOR + p = db!=0 ? db->pPreUpdate : 0; +#else + p = db->pPreUpdate; +#endif + return (p ? p->v->nFrame : 0); +} +#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ + +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK +/* +** This function is designed to be called from within a pre-update callback +** only. +*/ +int sqlite3_preupdate_blobwrite(sqlite3 *db){ + PreUpdate *p; +#ifdef SQLITE_ENABLE_API_ARMOR + p = db!=0 ? db->pPreUpdate : 0; +#else + p = db->pPreUpdate; +#endif + return (p ? p->iBlobWrite : -1); +} +#endif + +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK +/* +** This function is called from within a pre-update callback to retrieve +** a field of the row currently being updated or inserted. +*/ +int sqlite3_preupdate_new(sqlite3 *db, int iIdx, sqlite3_value **ppValue){ + PreUpdate *p; + int rc = SQLITE_OK; + Mem *pMem; + int iStore = 0; + +#ifdef SQLITE_ENABLE_API_ARMOR + if( db==0 || ppValue==0 ){ + return SQLITE_MISUSE_BKPT; + } +#endif + p = db->pPreUpdate; + if( !p || p->op==SQLITE_DELETE ){ + rc = SQLITE_MISUSE_BKPT; + goto preupdate_new_out; + } + if( p->pPk && p->op!=SQLITE_UPDATE ){ + iStore = sqlite3TableColumnToIndex(p->pPk, iIdx); + }else if( iIdx >= p->pTab->nCol ){ + return SQLITE_MISUSE_BKPT; + }else{ + iStore = sqlite3TableColumnToStorage(p->pTab, iIdx); + } + + if( iStore>=p->pCsr->nField || iStore<0 ){ + rc = SQLITE_RANGE; + goto preupdate_new_out; + } + + if( p->op==SQLITE_INSERT ){ + /* For an INSERT, memory cell p->iNewReg contains the serialized record + ** that is being inserted. Deserialize it. */ + UnpackedRecord *pUnpack = p->pNewUnpacked; + if( !pUnpack ){ + Mem *pData = &p->v->aMem[p->iNewReg]; + rc = ExpandBlob(pData); + if( rc!=SQLITE_OK ) goto preupdate_new_out; + pUnpack = vdbeUnpackRecord(p->pKeyinfo, pData->n, pData->z); + if( !pUnpack ){ + rc = SQLITE_NOMEM; + goto preupdate_new_out; + } + p->pNewUnpacked = pUnpack; + } + pMem = &pUnpack->aMem[iStore]; + if( iIdx==p->pTab->iPKey ){ + sqlite3VdbeMemSetInt64(pMem, p->iKey2); + }else if( iStore>=pUnpack->nField ){ + pMem = (sqlite3_value *)columnNullValue(); + } + }else{ + /* For an UPDATE, memory cell (p->iNewReg+1+iStore) contains the required + ** value. Make a copy of the cell contents and return a pointer to it. + ** It is not safe to return a pointer to the memory cell itself as the + ** caller may modify the value text encoding. + */ + assert( p->op==SQLITE_UPDATE ); + if( !p->aNew ){ + assert( sizeof(Mem)*UMXV(p->pCsr->nField) < 0x7fffffff ); + p->aNew = (Mem *)sqlite3DbMallocZero(db, sizeof(Mem)*p->pCsr->nField); + if( !p->aNew ){ + rc = SQLITE_NOMEM; + goto preupdate_new_out; + } + } + assert( iStore>=0 && iStore<p->pCsr->nField ); + pMem = &p->aNew[iStore]; + if( pMem->flags==0 ){ + if( iIdx==p->pTab->iPKey ){ + sqlite3VdbeMemSetInt64(pMem, p->iKey2); + }else{ + rc = sqlite3VdbeMemCopy(pMem, &p->v->aMem[p->iNewReg+1+iStore]); + if( rc!=SQLITE_OK ) goto preupdate_new_out; + } + } + } + *ppValue = pMem; + + preupdate_new_out: + sqlite3Error(db, rc); + return sqlite3ApiExit(db, rc); +} +#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ + #ifdef SQLITE_ENABLE_STMT_SCANSTATUS /* ** Return status data for a single loop within query pStmt. */ -int sqlite3_stmt_scanstatus( +int sqlite3_stmt_scanstatus_v2( sqlite3_stmt *pStmt, /* Prepared statement being queried */ - int idx, /* Index of loop to report on */ + int iScan, /* Index of loop to report on */ int iScanStatusOp, /* Which metric to return */ + int flags, void *pOut /* OUT: Write the answer here */ ){ Vdbe *p = (Vdbe*)pStmt; - ScanStatus *pScan; - if( idx<0 || idx>=p->nScan ) return 1; + VdbeOp *aOp; + int nOp; + ScanStatus *pScan = 0; + int idx; + +#ifdef SQLITE_ENABLE_API_ARMOR + if( p==0 || pOut==0 + || iScanStatusOp<SQLITE_SCANSTAT_NLOOP + || iScanStatusOp>SQLITE_SCANSTAT_NCYCLE ){ + return 1; + } +#endif + aOp = p->aOp; + nOp = p->nOp; + if( p->pFrame ){ + VdbeFrame *pFrame; + for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent); + aOp = pFrame->aOp; + nOp = pFrame->nOp; + } + + if( iScan<0 ){ + int ii; + if( iScanStatusOp==SQLITE_SCANSTAT_NCYCLE ){ + i64 res = 0; + for(ii=0; ii<nOp; ii++){ + res += aOp[ii].nCycle; + } + *(i64*)pOut = res; + return 0; + } + return 1; + } + if( flags & SQLITE_SCANSTAT_COMPLEX ){ + idx = iScan; + }else{ + /* If the COMPLEX flag is clear, then this function must ignore any + ** ScanStatus structures with ScanStatus.addrLoop set to 0. */ + for(idx=0; idx<p->nScan; idx++){ + pScan = &p->aScan[idx]; + if( pScan->zName ){ + iScan--; + if( iScan<0 ) break; + } + } + } + if( idx>=p->nScan ) return 1; + assert( pScan==0 || pScan==&p->aScan[idx] ); pScan = &p->aScan[idx]; + switch( iScanStatusOp ){ case SQLITE_SCANSTAT_NLOOP: { - *(sqlite3_int64*)pOut = p->anExec[pScan->addrLoop]; + if( pScan->addrLoop>0 ){ + *(sqlite3_int64*)pOut = aOp[pScan->addrLoop].nExec; + }else{ + *(sqlite3_int64*)pOut = -1; + } break; } case SQLITE_SCANSTAT_NVISIT: { - *(sqlite3_int64*)pOut = p->anExec[pScan->addrVisit]; + if( pScan->addrVisit>0 ){ + *(sqlite3_int64*)pOut = aOp[pScan->addrVisit].nExec; + }else{ + *(sqlite3_int64*)pOut = -1; + } break; } case SQLITE_SCANSTAT_EST: { @@ -1640,7 +2515,7 @@ int sqlite3_stmt_scanstatus( } case SQLITE_SCANSTAT_EXPLAIN: { if( pScan->addrExplain ){ - *(const char**)pOut = p->aOp[ pScan->addrExplain ].p4.z; + *(const char**)pOut = aOp[ pScan->addrExplain ].p4.z; }else{ *(const char**)pOut = 0; } @@ -1648,12 +2523,51 @@ int sqlite3_stmt_scanstatus( } case SQLITE_SCANSTAT_SELECTID: { if( pScan->addrExplain ){ - *(int*)pOut = p->aOp[ pScan->addrExplain ].p1; + *(int*)pOut = aOp[ pScan->addrExplain ].p1; }else{ *(int*)pOut = -1; } break; } + case SQLITE_SCANSTAT_PARENTID: { + if( pScan->addrExplain ){ + *(int*)pOut = aOp[ pScan->addrExplain ].p2; + }else{ + *(int*)pOut = -1; + } + break; + } + case SQLITE_SCANSTAT_NCYCLE: { + i64 res = 0; + if( pScan->aAddrRange[0]==0 ){ + res = -1; + }else{ + int ii; + for(ii=0; ii<ArraySize(pScan->aAddrRange); ii+=2){ + int iIns = pScan->aAddrRange[ii]; + int iEnd = pScan->aAddrRange[ii+1]; + if( iIns==0 ) break; + if( iIns>0 ){ + while( iIns<=iEnd ){ + res += aOp[iIns].nCycle; + iIns++; + } + }else{ + int iOp; + for(iOp=0; iOp<nOp; iOp++){ + Op *pOp = &aOp[iOp]; + if( pOp->p1!=iEnd ) continue; + if( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_NCYCLE)==0 ){ + continue; + } + res += aOp[iOp].nCycle; + } + } + } + } + *(i64*)pOut = res; + break; + } default: { return 1; } @@ -1661,11 +2575,28 @@ int sqlite3_stmt_scanstatus( return 0; } +/* +** Return status data for a single loop within query pStmt. +*/ +int sqlite3_stmt_scanstatus( + sqlite3_stmt *pStmt, /* Prepared statement being queried */ + int iScan, /* Index of loop to report on */ + int iScanStatusOp, /* Which metric to return */ + void *pOut /* OUT: Write the answer here */ +){ + return sqlite3_stmt_scanstatus_v2(pStmt, iScan, iScanStatusOp, 0, pOut); +} + /* ** Zero all counters associated with the sqlite3_stmt_scanstatus() data. */ void sqlite3_stmt_scanstatus_reset(sqlite3_stmt *pStmt){ Vdbe *p = (Vdbe*)pStmt; - memset(p->anExec, 0, p->nOp * sizeof(i64)); + int ii; + for(ii=0; p!=0 && ii<p->nOp; ii++){ + Op *pOp = &p->aOp[ii]; + pOp->nExec = 0; + pOp->nCycle = 0; + } } #endif /* SQLITE_ENABLE_STMT_SCANSTATUS */ diff --git a/src/vdbeaux.c b/src/vdbeaux.c index ce98edd0a5..5368c0c420 100644 --- a/src/vdbeaux.c +++ b/src/vdbeaux.c @@ -10,35 +10,49 @@ ** ************************************************************************* ** This file contains code used for creating, destroying, and populating -** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.) +** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.) */ #include "sqliteInt.h" #include "vdbeInt.h" +/* Forward references */ +static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef); +static void vdbeFreeOpArray(sqlite3 *, Op *, int); + /* ** Create a new virtual database engine. */ Vdbe *sqlite3VdbeCreate(Parse *pParse){ sqlite3 *db = pParse->db; Vdbe *p; - p = sqlite3DbMallocZero(db, sizeof(Vdbe) ); + p = sqlite3DbMallocRawNN(db, sizeof(Vdbe) ); if( p==0 ) return 0; + memset(&p->aOp, 0, sizeof(Vdbe)-offsetof(Vdbe,aOp)); p->db = db; if( db->pVdbe ){ - db->pVdbe->pPrev = p; + db->pVdbe->ppVPrev = &p->pVNext; } - p->pNext = db->pVdbe; - p->pPrev = 0; + p->pVNext = db->pVdbe; + p->ppVPrev = &db->pVdbe; db->pVdbe = p; - p->magic = VDBE_MAGIC_INIT; + assert( p->eVdbeState==VDBE_INIT_STATE ); p->pParse = pParse; + pParse->pVdbe = p; assert( pParse->aLabel==0 ); assert( pParse->nLabel==0 ); - assert( pParse->nOpAlloc==0 ); + assert( p->nOpAlloc==0 ); assert( pParse->szOpAlloc==0 ); + sqlite3VdbeAddOp2(p, OP_Init, 0, 1); return p; } +/* +** Return the Parse object that owns a Vdbe object. +*/ +Parse *sqlite3VdbeParser(Vdbe *p){ + return p->pParse; +} + /* ** Change the error string stored in Vdbe.zErrMsg */ @@ -53,54 +67,98 @@ void sqlite3VdbeError(Vdbe *p, const char *zFormat, ...){ /* ** Remember the SQL string for a prepared statement. */ -void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, int isPrepareV2){ - assert( isPrepareV2==1 || isPrepareV2==0 ); +void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, u8 prepFlags){ if( p==0 ) return; -#if defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_ENABLE_SQLLOG) - if( !isPrepareV2 ) return; -#endif + p->prepFlags = prepFlags; + if( (prepFlags & SQLITE_PREPARE_SAVESQL)==0 ){ + p->expmask = 0; + } assert( p->zSql==0 ); p->zSql = sqlite3DbStrNDup(p->db, z, n); - p->isPrepareV2 = (u8)isPrepareV2; } +#ifdef SQLITE_ENABLE_NORMALIZE /* -** Return the SQL associated with a prepared statement +** Add a new element to the Vdbe->pDblStr list. */ -const char *sqlite3_sql(sqlite3_stmt *pStmt){ - Vdbe *p = (Vdbe *)pStmt; - return p ? p->zSql : 0; +void sqlite3VdbeAddDblquoteStr(sqlite3 *db, Vdbe *p, const char *z){ + if( p ){ + int n = sqlite3Strlen30(z); + DblquoteStr *pStr = sqlite3DbMallocRawNN(db, + sizeof(*pStr)+n+1-sizeof(pStr->z)); + if( pStr ){ + pStr->pNextStr = p->pDblStr; + p->pDblStr = pStr; + memcpy(pStr->z, z, n+1); + } + } } +#endif +#ifdef SQLITE_ENABLE_NORMALIZE /* -** Swap all content between two VDBE structures. +** zId of length nId is a double-quoted identifier. Check to see if +** that identifier is really used as a string literal. +*/ +int sqlite3VdbeUsesDoubleQuotedString( + Vdbe *pVdbe, /* The prepared statement */ + const char *zId /* The double-quoted identifier, already dequoted */ +){ + DblquoteStr *pStr; + assert( zId!=0 ); + if( pVdbe->pDblStr==0 ) return 0; + for(pStr=pVdbe->pDblStr; pStr; pStr=pStr->pNextStr){ + if( strcmp(zId, pStr->z)==0 ) return 1; + } + return 0; +} +#endif + +/* +** Swap byte-code between two VDBE structures. +** +** This happens after pB was previously run and returned +** SQLITE_SCHEMA. The statement was then reprepared in pA. +** This routine transfers the new bytecode in pA over to pB +** so that pB can be run again. The old pB byte code is +** moved back to pA so that it will be cleaned up when pA is +** finalized. */ void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){ - Vdbe tmp, *pTmp; + Vdbe tmp, *pTmp, **ppTmp; char *zTmp; + assert( pA->db==pB->db ); tmp = *pA; *pA = *pB; *pB = tmp; - pTmp = pA->pNext; - pA->pNext = pB->pNext; - pB->pNext = pTmp; - pTmp = pA->pPrev; - pA->pPrev = pB->pPrev; - pB->pPrev = pTmp; + pTmp = pA->pVNext; + pA->pVNext = pB->pVNext; + pB->pVNext = pTmp; + ppTmp = pA->ppVPrev; + pA->ppVPrev = pB->ppVPrev; + pB->ppVPrev = ppTmp; zTmp = pA->zSql; pA->zSql = pB->zSql; pB->zSql = zTmp; - pB->isPrepareV2 = pA->isPrepareV2; +#ifdef SQLITE_ENABLE_NORMALIZE + zTmp = pA->zNormSql; + pA->zNormSql = pB->zNormSql; + pB->zNormSql = zTmp; +#endif + pB->expmask = pA->expmask; + pB->prepFlags = pA->prepFlags; + memcpy(pB->aCounter, pA->aCounter, sizeof(pB->aCounter)); + pB->aCounter[SQLITE_STMTSTATUS_REPREPARE]++; } /* -** Resize the Vdbe.aOp array so that it is at least nOp elements larger +** Resize the Vdbe.aOp array so that it is at least nOp elements larger ** than its current size. nOp is guaranteed to be less than or equal ** to 1024/sizeof(Op). ** ** If an out-of-memory error occurs while resizing the array, return -** SQLITE_NOMEM. In this case Vdbe.aOp and Parse.nOpAlloc remain -** unchanged (this is so that any opcodes already allocated can be +** SQLITE_NOMEM. In this case Vdbe.aOp and Vdbe.nOpAlloc remain +** unchanged (this is so that any opcodes already allocated can be ** correctly deallocated along with the rest of the Vdbe). */ static int growOpArray(Vdbe *v, int nOp){ @@ -108,41 +166,88 @@ static int growOpArray(Vdbe *v, int nOp){ Parse *p = v->pParse; /* The SQLITE_TEST_REALLOC_STRESS compile-time option is designed to force - ** more frequent reallocs and hence provide more opportunities for + ** more frequent reallocs and hence provide more opportunities for ** simulated OOM faults. SQLITE_TEST_REALLOC_STRESS is generally used ** during testing only. With SQLITE_TEST_REALLOC_STRESS grow the op array ** by the minimum* amount required until the size reaches 512. Normal ** operation (without SQLITE_TEST_REALLOC_STRESS) is to double the current ** size of the op array or add 1KB of space, whichever is smaller. */ #ifdef SQLITE_TEST_REALLOC_STRESS - int nNew = (p->nOpAlloc>=512 ? p->nOpAlloc*2 : p->nOpAlloc+nOp); + sqlite3_int64 nNew = (v->nOpAlloc>=512 ? 2*(sqlite3_int64)v->nOpAlloc + : (sqlite3_int64)v->nOpAlloc+nOp); #else - int nNew = (p->nOpAlloc ? p->nOpAlloc*2 : (int)(1024/sizeof(Op))); + sqlite3_int64 nNew = (v->nOpAlloc ? 2*(sqlite3_int64)v->nOpAlloc + : (sqlite3_int64)(1024/sizeof(Op))); UNUSED_PARAMETER(nOp); #endif - assert( nOp<=(1024/sizeof(Op)) ); - assert( nNew>=(p->nOpAlloc+nOp) ); + /* Ensure that the size of a VDBE does not grow too large */ + if( nNew > p->db->aLimit[SQLITE_LIMIT_VDBE_OP] ){ + sqlite3OomFault(p->db); + return SQLITE_NOMEM; + } + + assert( nOp<=(int)(1024/sizeof(Op)) ); + assert( nNew>=(v->nOpAlloc+nOp) ); pNew = sqlite3DbRealloc(p->db, v->aOp, nNew*sizeof(Op)); if( pNew ){ p->szOpAlloc = sqlite3DbMallocSize(p->db, pNew); - p->nOpAlloc = p->szOpAlloc/sizeof(Op); + v->nOpAlloc = p->szOpAlloc/sizeof(Op); v->aOp = pNew; } - return (pNew ? SQLITE_OK : SQLITE_NOMEM); + return (pNew ? SQLITE_OK : SQLITE_NOMEM_BKPT); } #ifdef SQLITE_DEBUG /* This routine is just a convenient place to set a breakpoint that will ** fire after each opcode is inserted and displayed using -** "PRAGMA vdbe_addoptrace=on". +** "PRAGMA vdbe_addoptrace=on". Parameters "pc" (program counter) and +** pOp are available to make the breakpoint conditional. +** +** Other useful labels for breakpoints include: +** test_trace_breakpoint(pc,pOp) +** sqlite3CorruptError(lineno) +** sqlite3MisuseError(lineno) +** sqlite3CantopenError(lineno) */ -static void test_addop_breakpoint(void){ - static int n = 0; +static void test_addop_breakpoint(int pc, Op *pOp){ + static u64 n = 0; + (void)pc; + (void)pOp; n++; + if( n==LARGEST_UINT64 ) abort(); /* so that n is used, preventing a warning */ } #endif +/* +** Slow paths for sqlite3VdbeAddOp3() and sqlite3VdbeAddOp4Int() for the +** unusual case when we need to increase the size of the Vdbe.aOp[] array +** before adding the new opcode. +*/ +static SQLITE_NOINLINE int growOp3(Vdbe *p, int op, int p1, int p2, int p3){ + assert( p->nOpAlloc<=p->nOp ); + if( growOpArray(p, 1) ) return 1; + assert( p->nOpAlloc>p->nOp ); + return sqlite3VdbeAddOp3(p, op, p1, p2, p3); +} +static SQLITE_NOINLINE int addOp4IntSlow( + Vdbe *p, /* Add the opcode to this VM */ + int op, /* The new opcode */ + int p1, /* The P1 operand */ + int p2, /* The P2 operand */ + int p3, /* The P3 operand */ + int p4 /* The P4 operand as an integer */ +){ + int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3); + if( p->db->mallocFailed==0 ){ + VdbeOp *pOp = &p->aOp[addr]; + pOp->p4type = P4_INT32; + pOp->p4.i = p4; + } + return addr; +} + + /* ** Add a new instruction to the list of instructions current in the ** VDBE. Return the address of the new instruction. @@ -153,30 +258,31 @@ static void test_addop_breakpoint(void){ ** ** op The opcode for this instruction ** -** p1, p2, p3 Operands -** -** Use the sqlite3VdbeResolveLabel() function to fix an address and -** the sqlite3VdbeChangeP4() function to change the value of the P4 -** operand. +** p1, p2, p3, p4 Operands */ -static SQLITE_NOINLINE int growOp3(Vdbe *p, int op, int p1, int p2, int p3){ - assert( p->pParse->nOpAlloc<=p->nOp ); - if( growOpArray(p, 1) ) return 1; - assert( p->pParse->nOpAlloc>p->nOp ); - return sqlite3VdbeAddOp3(p, op, p1, p2, p3); +int sqlite3VdbeAddOp0(Vdbe *p, int op){ + return sqlite3VdbeAddOp3(p, op, 0, 0, 0); +} +int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){ + return sqlite3VdbeAddOp3(p, op, p1, 0, 0); +} +int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){ + return sqlite3VdbeAddOp3(p, op, p1, p2, 0); } int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){ int i; VdbeOp *pOp; i = p->nOp; - assert( p->magic==VDBE_MAGIC_INIT ); + assert( p->eVdbeState==VDBE_INIT_STATE ); assert( op>=0 && op<0xff ); - if( p->pParse->nOpAlloc<=i ){ + if( p->nOpAlloc<=i ){ return growOp3(p, op, p1, p2, p3); } + assert( p->aOp!=0 ); p->nOp++; pOp = &p->aOp[i]; + assert( pOp!=0 ); pOp->opcode = (u8)op; pOp->p5 = 0; pOp->p1 = p1; @@ -184,41 +290,78 @@ int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){ pOp->p3 = p3; pOp->p4.p = 0; pOp->p4type = P4_NOTUSED; + + /* Replicate this logic in sqlite3VdbeAddOp4Int() + ** vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv */ #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS pOp->zComment = 0; #endif +#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || defined(VDBE_PROFILE) + pOp->nExec = 0; + pOp->nCycle = 0; +#endif #ifdef SQLITE_DEBUG if( p->db->flags & SQLITE_VdbeAddopTrace ){ - int jj, kk; - Parse *pParse = p->pParse; - for(jj=kk=0; jj<SQLITE_N_COLCACHE; jj++){ - struct yColCache *x = pParse->aColCache + jj; - if( x->iLevel>pParse->iCacheLevel || x->iReg==0 ) continue; - printf(" r[%d]={%d:%d}", x->iReg, x->iTable, x->iColumn); - kk++; - } - if( kk ) printf("\n"); sqlite3VdbePrintOp(0, i, &p->aOp[i]); - test_addop_breakpoint(); + test_addop_breakpoint(i, &p->aOp[i]); } #endif -#ifdef VDBE_PROFILE - pOp->cycles = 0; - pOp->cnt = 0; -#endif #ifdef SQLITE_VDBE_COVERAGE pOp->iSrcLine = 0; #endif + /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + ** Replicate in sqlite3VdbeAddOp4Int() */ + return i; } -int sqlite3VdbeAddOp0(Vdbe *p, int op){ - return sqlite3VdbeAddOp3(p, op, 0, 0, 0); -} -int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){ - return sqlite3VdbeAddOp3(p, op, p1, 0, 0); -} -int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){ - return sqlite3VdbeAddOp3(p, op, p1, p2, 0); +int sqlite3VdbeAddOp4Int( + Vdbe *p, /* Add the opcode to this VM */ + int op, /* The new opcode */ + int p1, /* The P1 operand */ + int p2, /* The P2 operand */ + int p3, /* The P3 operand */ + int p4 /* The P4 operand as an integer */ +){ + int i; + VdbeOp *pOp; + + i = p->nOp; + if( p->nOpAlloc<=i ){ + return addOp4IntSlow(p, op, p1, p2, p3, p4); + } + p->nOp++; + pOp = &p->aOp[i]; + assert( pOp!=0 ); + pOp->opcode = (u8)op; + pOp->p5 = 0; + pOp->p1 = p1; + pOp->p2 = p2; + pOp->p3 = p3; + pOp->p4.i = p4; + pOp->p4type = P4_INT32; + + /* Replicate this logic in sqlite3VdbeAddOp3() + ** vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv */ +#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS + pOp->zComment = 0; +#endif +#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || defined(VDBE_PROFILE) + pOp->nExec = 0; + pOp->nCycle = 0; +#endif +#ifdef SQLITE_DEBUG + if( p->db->flags & SQLITE_VdbeAddopTrace ){ + sqlite3VdbePrintOp(0, i, &p->aOp[i]); + test_addop_breakpoint(i, &p->aOp[i]); + } +#endif +#ifdef SQLITE_VDBE_COVERAGE + pOp->iSrcLine = 0; +#endif + /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + ** Replicate in sqlite3VdbeAddOp3() */ + + return i; } /* Generate code for an unconditional jump to instruction iDest @@ -241,6 +384,9 @@ int sqlite3VdbeLoadString(Vdbe *p, int iDest, const char *zStr){ ** "s" character in zTypes[], the register is a string if the argument is ** not NULL, or OP_Null if the value is a null pointer. For each "i" character ** in zTypes[], the register is initialized to an integer. +** +** If the input string does not end with "X" then an OP_ResultRow instruction +** is generated for the values inserted. */ void sqlite3VdbeMultiLoad(Vdbe *p, int iDest, const char *zTypes, ...){ va_list ap; @@ -250,12 +396,15 @@ void sqlite3VdbeMultiLoad(Vdbe *p, int iDest, const char *zTypes, ...){ for(i=0; (c = zTypes[i])!=0; i++){ if( c=='s' ){ const char *z = va_arg(ap, const char*); - sqlite3VdbeAddOp4(p, z==0 ? OP_Null : OP_String8, 0, iDest++, 0, z, 0); + sqlite3VdbeAddOp4(p, z==0 ? OP_Null : OP_String8, 0, iDest+i, 0, z, 0); + }else if( c=='i' ){ + sqlite3VdbeAddOp2(p, OP_Integer, va_arg(ap, int), iDest+i); }else{ - assert( c=='i' ); - sqlite3VdbeAddOp2(p, OP_Integer, va_arg(ap, int), iDest++); + goto skip_op_resultrow; } } + sqlite3VdbeAddOp2(p, OP_ResultRow, iDest, i); +skip_op_resultrow: va_end(ap); } @@ -276,6 +425,48 @@ int sqlite3VdbeAddOp4( return addr; } +/* +** Add an OP_Function or OP_PureFunc opcode. +** +** The eCallCtx argument is information (typically taken from Expr.op2) +** that describes the calling context of the function. 0 means a general +** function call. NC_IsCheck means called by a check constraint, +** NC_IdxExpr means called as part of an index expression. NC_PartIdx +** means in the WHERE clause of a partial index. NC_GenCol means called +** while computing a generated column value. 0 is the usual case. +*/ +int sqlite3VdbeAddFunctionCall( + Parse *pParse, /* Parsing context */ + int p1, /* Constant argument mask */ + int p2, /* First argument register */ + int p3, /* Register into which results are written */ + int nArg, /* Number of argument */ + const FuncDef *pFunc, /* The function to be invoked */ + int eCallCtx /* Calling context */ +){ + Vdbe *v = pParse->pVdbe; + int addr; + sqlite3_context *pCtx; + assert( v ); + pCtx = sqlite3DbMallocRawNN(pParse->db, SZ_CONTEXT(nArg)); + if( pCtx==0 ){ + assert( pParse->db->mallocFailed ); + freeEphemeralFunction(pParse->db, (FuncDef*)pFunc); + return 0; + } + pCtx->pOut = 0; + pCtx->pFunc = (FuncDef*)pFunc; + pCtx->pVdbe = 0; + pCtx->isError = 0; + pCtx->argc = nArg; + pCtx->iOp = sqlite3VdbeCurrentAddr(v); + addr = sqlite3VdbeAddOp4(v, eCallCtx ? OP_PureFunc : OP_Function, + p1, p2, p3, (char*)pCtx, P4_FUNCCTX); + sqlite3VdbeChangeP5(v, eCallCtx & NC_SelfRef); + sqlite3MayAbort(pParse); + return addr; +} + /* ** Add an opcode that includes the p4 value with a P4_INT64 or ** P4_REAL type. @@ -294,6 +485,72 @@ int sqlite3VdbeAddOp4Dup8( return sqlite3VdbeAddOp4(p, op, p1, p2, p3, p4copy, p4type); } +#ifndef SQLITE_OMIT_EXPLAIN +/* +** Return the address of the current EXPLAIN QUERY PLAN baseline. +** 0 means "none". +*/ +int sqlite3VdbeExplainParent(Parse *pParse){ + VdbeOp *pOp; + if( pParse->addrExplain==0 ) return 0; + pOp = sqlite3VdbeGetOp(pParse->pVdbe, pParse->addrExplain); + return pOp->p2; +} + +/* +** Set a debugger breakpoint on the following routine in order to +** monitor the EXPLAIN QUERY PLAN code generation. +*/ +#if defined(SQLITE_DEBUG) +void sqlite3ExplainBreakpoint(const char *z1, const char *z2){ + (void)z1; + (void)z2; +} +#endif + +/* +** Add a new OP_Explain opcode. +** +** If the bPush flag is true, then make this opcode the parent for +** subsequent Explains until sqlite3VdbeExplainPop() is called. +*/ +int sqlite3VdbeExplain(Parse *pParse, u8 bPush, const char *zFmt, ...){ + int addr = 0; +#if !defined(SQLITE_DEBUG) + /* Always include the OP_Explain opcodes if SQLITE_DEBUG is defined. + ** But omit them (for performance) during production builds */ + if( pParse->explain==2 || IS_STMT_SCANSTATUS(pParse->db) ) +#endif + { + char *zMsg; + Vdbe *v; + va_list ap; + int iThis; + va_start(ap, zFmt); + zMsg = sqlite3VMPrintf(pParse->db, zFmt, ap); + va_end(ap); + v = pParse->pVdbe; + iThis = v->nOp; + addr = sqlite3VdbeAddOp4(v, OP_Explain, iThis, pParse->addrExplain, 0, + zMsg, P4_DYNAMIC); + sqlite3ExplainBreakpoint(bPush?"PUSH":"", sqlite3VdbeGetLastOp(v)->p4.z); + if( bPush){ + pParse->addrExplain = iThis; + } + sqlite3VdbeScanStatus(v, iThis, -1, -1, 0, 0); + } + return addr; +} + +/* +** Pop the EXPLAIN QUERY PLAN stack one level. +*/ +void sqlite3VdbeExplainPop(Parse *pParse){ + sqlite3ExplainBreakpoint("POP", 0); + pParse->addrExplain = sqlite3VdbeExplainParent(pParse); +} +#endif /* SQLITE_OMIT_EXPLAIN */ + /* ** Add an OP_ParseSchema opcode. This routine is broken out from ** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees @@ -302,26 +559,12 @@ int sqlite3VdbeAddOp4Dup8( ** The zWhere string must have been obtained from sqlite3_malloc(). ** This routine will take ownership of the allocated memory. */ -void sqlite3VdbeAddParseSchemaOp(Vdbe *p, int iDb, char *zWhere){ +void sqlite3VdbeAddParseSchemaOp(Vdbe *p, int iDb, char *zWhere, u16 p5){ int j; sqlite3VdbeAddOp4(p, OP_ParseSchema, iDb, 0, 0, zWhere, P4_DYNAMIC); + sqlite3VdbeChangeP5(p, p5); for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j); -} - -/* -** Add an opcode that includes the p4 value as an integer. -*/ -int sqlite3VdbeAddOp4Int( - Vdbe *p, /* Add the opcode to this VM */ - int op, /* The new opcode */ - int p1, /* The P1 operand */ - int p2, /* The P2 operand */ - int p3, /* The P3 operand */ - int p4 /* The P4 operand as an integer */ -){ - int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3); - sqlite3VdbeChangeP4(p, addr, SQLITE_INT_TO_PTR(p4), P4_INT32); - return addr; + sqlite3MayAbort(p->pParse); } /* Insert the end of a co-routine @@ -350,21 +593,22 @@ void sqlite3VdbeEndCoroutine(Vdbe *v, int regYield){ ** The VDBE knows that a P2 value is a label because labels are ** always negative and P2 values are suppose to be non-negative. ** Hence, a negative P2 value is a label that has yet to be resolved. +** (Later:) This is only true for opcodes that have the OPFLG_JUMP +** property. +** +** Variable usage notes: ** -** Zero is returned if a malloc() fails. +** Parse.aLabel[x] Stores the address that the x-th label resolves +** into. For testing (SQLITE_DEBUG), unresolved +** labels stores -1, but that is not required. +** Parse.nLabelAlloc Number of slots allocated to Parse.aLabel[] +** Parse.nLabel The *negative* of the number of labels that have +** been issued. The negative is stored because +** that gives a performance improvement over storing +** the equivalent positive value. */ -int sqlite3VdbeMakeLabel(Vdbe *v){ - Parse *p = v->pParse; - int i = p->nLabel++; - assert( v->magic==VDBE_MAGIC_INIT ); - if( (i & (i-1))==0 ){ - p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel, - (i*2+1)*sizeof(p->aLabel[0])); - } - if( p->aLabel ){ - p->aLabel[i] = -1; - } - return ADDR(i); +int sqlite3VdbeMakeLabel(Parse *pParse){ + return --pParse->nLabel; } /* @@ -372,42 +616,80 @@ int sqlite3VdbeMakeLabel(Vdbe *v){ ** be inserted. The parameter "x" must have been obtained from ** a prior call to sqlite3VdbeMakeLabel(). */ +static SQLITE_NOINLINE void resizeResolveLabel(Parse *p, Vdbe *v, int j){ + int nNewSize = 10 - p->nLabel; + p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel, + nNewSize*sizeof(p->aLabel[0])); + if( p->aLabel==0 ){ + p->nLabelAlloc = 0; + }else{ +#ifdef SQLITE_DEBUG + int i; + for(i=p->nLabelAlloc; i<nNewSize; i++) p->aLabel[i] = -1; +#endif + if( nNewSize>=100 && (nNewSize/100)>(p->nLabelAlloc/100) ){ + sqlite3ProgressCheck(p); + } + p->nLabelAlloc = nNewSize; + p->aLabel[j] = v->nOp; + } +} void sqlite3VdbeResolveLabel(Vdbe *v, int x){ Parse *p = v->pParse; int j = ADDR(x); - assert( v->magic==VDBE_MAGIC_INIT ); - assert( j<p->nLabel ); + assert( v->eVdbeState==VDBE_INIT_STATE ); + assert( j<-p->nLabel ); assert( j>=0 ); - if( p->aLabel ){ +#ifdef SQLITE_DEBUG + if( p->db->flags & SQLITE_VdbeAddopTrace ){ + printf("RESOLVE LABEL %d to %d\n", x, v->nOp); + } +#endif + if( p->nLabelAlloc + p->nLabel < 0 ){ + resizeResolveLabel(p,v,j); + }else{ + assert( p->aLabel[j]==(-1) ); /* Labels may only be resolved once */ p->aLabel[j] = v->nOp; } - p->iFixedOp = v->nOp - 1; } /* ** Mark the VDBE as one that can only be run one time. */ void sqlite3VdbeRunOnlyOnce(Vdbe *p){ - p->runOnlyOnce = 1; + sqlite3VdbeAddOp2(p, OP_Expire, 1, 1); +} + +/* +** Mark the VDBE as one that can be run multiple times. +*/ +void sqlite3VdbeReusable(Vdbe *p){ + int i; + for(i=1; ALWAYS(i<p->nOp); i++){ + if( ALWAYS(p->aOp[i].opcode==OP_Expire) ){ + p->aOp[1].opcode = OP_Noop; + break; + } + } } #ifdef SQLITE_DEBUG /* sqlite3AssertMayAbort() logic */ /* ** The following type and function are used to iterate through all opcodes -** in a Vdbe main program and each of the sub-programs (triggers) it may +** in a Vdbe main program and each of the sub-programs (triggers) it may ** invoke directly or indirectly. It should be used as follows: ** ** Op *pOp; ** VdbeOpIter sIter; ** ** memset(&sIter, 0, sizeof(sIter)); -** sIter.v = v; // v is of type Vdbe* +** sIter.v = v; // v is of type Vdbe* ** while( (pOp = opIterNext(&sIter)) ){ ** // Do something with pOp ** } ** sqlite3DbFree(v->db, sIter.apSub); -** +** */ typedef struct VdbeOpIter VdbeOpIter; struct VdbeOpIter { @@ -440,9 +722,9 @@ static Op *opIterNext(VdbeOpIter *p){ p->iSub++; p->iAddr = 0; } - + if( pRet->p4type==P4_SUBPROGRAM ){ - int nByte = (p->nSub+1)*sizeof(SubProgram*); + i64 nByte = (1+(u64)p->nSub)*sizeof(SubProgram*); int j; for(j=0; j<p->nSub; j++){ if( p->apSub[j]==pRet->p4.pProgram ) break; @@ -471,9 +753,11 @@ static Op *opIterNext(VdbeOpIter *p){ ** * OP_HaltIfNull with P1=SQLITE_CONSTRAINT and P2=OE_Abort. ** * OP_Destroy ** * OP_VUpdate +** * OP_VCreate ** * OP_VRename ** * OP_FkCounter with P2==0 (immediate foreign key constraint) -** * OP_CreateTable and OP_InitCoroutine (for CREATE TABLE AS SELECT ...) +** * OP_CreateBtree/BTREE_INTKEY and OP_InitCoroutine +** (for CREATE TABLE AS SELECT ...) ** ** Then check that the value of Parse.mayAbort is true if an ** ABORT may be thrown, or false otherwise. Return true if it does @@ -486,22 +770,37 @@ int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){ int hasAbort = 0; int hasFkCounter = 0; int hasCreateTable = 0; + int hasCreateIndex = 0; int hasInitCoroutine = 0; Op *pOp; VdbeOpIter sIter; + + if( v==0 ) return 0; memset(&sIter, 0, sizeof(sIter)); sIter.v = v; while( (pOp = opIterNext(&sIter))!=0 ){ int opcode = pOp->opcode; - if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename - || ((opcode==OP_Halt || opcode==OP_HaltIfNull) - && ((pOp->p1&0xff)==SQLITE_CONSTRAINT && pOp->p2==OE_Abort)) + if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename + || opcode==OP_VDestroy + || opcode==OP_VCreate + || opcode==OP_ParseSchema + || opcode==OP_Function || opcode==OP_PureFunc + || ((opcode==OP_Halt || opcode==OP_HaltIfNull) + && ((pOp->p1)!=SQLITE_OK && pOp->p2==OE_Abort)) ){ hasAbort = 1; break; } - if( opcode==OP_CreateTable ) hasCreateTable = 1; + if( opcode==OP_CreateBtree && pOp->p3==BTREE_INTKEY ) hasCreateTable = 1; + if( mayAbort ){ + /* hasCreateIndex may also be set for some DELETE statements that use + ** OP_Clear. So this routine may end up returning true in the case + ** where a "DELETE FROM tbl" has a statement-journal but does not + ** require one. This is not so bad - it is an inefficiency, not a bug. */ + if( opcode==OP_CreateBtree && pOp->p3==BTREE_BLOBKEY ) hasCreateIndex = 1; + if( opcode==OP_Clear ) hasCreateIndex = 1; + } if( opcode==OP_InitCoroutine ) hasInitCoroutine = 1; #ifndef SQLITE_OMIT_FOREIGN_KEY if( opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1 ){ @@ -517,10 +816,37 @@ int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){ ** true for this case to prevent the assert() in the callers frame ** from failing. */ return ( v->db->mallocFailed || hasAbort==mayAbort || hasFkCounter - || (hasCreateTable && hasInitCoroutine) ); + || (hasCreateTable && hasInitCoroutine) || hasCreateIndex + ); } #endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */ +#ifdef SQLITE_DEBUG +/* +** Increment the nWrite counter in the VDBE if the cursor is not an +** ephemeral cursor, or if the cursor argument is NULL. +*/ +void sqlite3VdbeIncrWriteCounter(Vdbe *p, VdbeCursor *pC){ + if( pC==0 + || (pC->eCurType!=CURTYPE_SORTER + && pC->eCurType!=CURTYPE_PSEUDO + && !pC->isEphemeral) + ){ + p->nWrite++; + } +} +#endif + +#ifdef SQLITE_DEBUG +/* +** Assert if an Abort at this point in time might result in a corrupt +** database. +*/ +void sqlite3VdbeAssertAbortable(Vdbe *p){ + assert( p->nWrite==0 || p->usesStmtJournal ); +} +#endif + /* ** This routine is called after all opcodes have been inserted. It loops ** through all the opcodes and fixes up some details. @@ -528,95 +854,216 @@ int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){ ** (1) For each jump instruction with a negative P2 value (a label) ** resolve the P2 value to an actual address. ** -** (2) Compute the maximum number of arguments used by any SQL function -** and store that value in *pMaxFuncArgs. +** (2) Compute the maximum number of arguments used by the xUpdate/xFilter +** methods of any virtual table and store that value in *pMaxVtabArgs. ** ** (3) Update the Vdbe.readOnly and Vdbe.bIsReader flags to accurately ** indicate what the prepared statement actually does. ** -** (4) Initialize the p4.xAdvance pointer on opcodes that use it. +** (4) (discontinued) ** ** (5) Reclaim the memory allocated for storing labels. +** +** This routine will only function correctly if the mkopcodeh.tcl generator +** script numbers the opcodes correctly. Changes to this routine must be +** coordinated with changes to mkopcodeh.tcl. */ -static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){ - int i; - int nMaxArgs = *pMaxFuncArgs; +static void resolveP2Values(Vdbe *p, int *pMaxVtabArgs){ + int nMaxVtabArgs = *pMaxVtabArgs; Op *pOp; Parse *pParse = p->pParse; int *aLabel = pParse->aLabel; + + assert( pParse->db->mallocFailed==0 ); /* tag-20230419-1 */ p->readOnly = 1; p->bIsReader = 0; - for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){ - u8 opcode = pOp->opcode; - - /* NOTE: Be sure to update mkopcodeh.tcl when adding or removing - ** cases from this switch! */ - switch( opcode ){ - case OP_Transaction: { - if( pOp->p2!=0 ) p->readOnly = 0; - /* fall thru */ - } - case OP_AutoCommit: - case OP_Savepoint: { - p->bIsReader = 1; - break; - } + pOp = &p->aOp[p->nOp-1]; + assert( p->aOp[0].opcode==OP_Init ); + while( 1 /* Loop terminates when it reaches the OP_Init opcode */ ){ + /* Only JUMP opcodes and the short list of special opcodes in the switch + ** below need to be considered. The mkopcodeh.tcl generator script groups + ** all these opcodes together near the front of the opcode list. Skip + ** any opcode that does not need processing by virtual of the fact that + ** it is larger than SQLITE_MX_JUMP_OPCODE, as a performance optimization. + */ + if( pOp->opcode<=SQLITE_MX_JUMP_OPCODE ){ + /* NOTE: Be sure to update mkopcodeh.tcl when adding or removing + ** cases from this switch! */ + switch( pOp->opcode ){ + case OP_Transaction: { + if( pOp->p2!=0 ) p->readOnly = 0; + /* no break */ deliberate_fall_through + } + case OP_AutoCommit: + case OP_Savepoint: { + p->bIsReader = 1; + break; + } #ifndef SQLITE_OMIT_WAL - case OP_Checkpoint: + case OP_Checkpoint: #endif - case OP_Vacuum: - case OP_JournalMode: { - p->readOnly = 0; - p->bIsReader = 1; - break; - } + case OP_Vacuum: + case OP_JournalMode: { + p->readOnly = 0; + p->bIsReader = 1; + break; + } + case OP_Init: { + assert( pOp->p2>=0 ); + goto resolve_p2_values_loop_exit; + } #ifndef SQLITE_OMIT_VIRTUALTABLE - case OP_VUpdate: { - if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2; - break; - } - case OP_VFilter: { - int n; - assert( p->nOp - i >= 3 ); - assert( pOp[-1].opcode==OP_Integer ); - n = pOp[-1].p1; - if( n>nMaxArgs ) nMaxArgs = n; - break; - } + case OP_VUpdate: { + if( pOp->p2>nMaxVtabArgs ) nMaxVtabArgs = pOp->p2; + break; + } + case OP_VFilter: { + int n; + /* The instruction immediately prior to VFilter will be an + ** OP_Integer that sets the "argc" value for the VFilter. See + ** the code where OP_VFilter is generated at tag-20250207a. */ + assert( (pOp - p->aOp) >= 3 ); + assert( pOp[-1].opcode==OP_Integer ); + assert( pOp[-1].p2==pOp->p3+1 ); + n = pOp[-1].p1; + if( n>nMaxVtabArgs ) nMaxVtabArgs = n; + /* Fall through into the default case */ + /* no break */ deliberate_fall_through + } #endif - case OP_Next: - case OP_NextIfOpen: - case OP_SorterNext: { - pOp->p4.xAdvance = sqlite3BtreeNext; - pOp->p4type = P4_ADVANCE; - break; - } - case OP_Prev: - case OP_PrevIfOpen: { - pOp->p4.xAdvance = sqlite3BtreePrevious; - pOp->p4type = P4_ADVANCE; - break; - } - } + default: { + if( pOp->p2<0 ){ + /* The mkopcodeh.tcl script has so arranged things that the only + ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to + ** have non-negative values for P2. */ + assert( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP)!=0 ); + assert( ADDR(pOp->p2)<-pParse->nLabel ); + assert( aLabel!=0 ); /* True because of tag-20230419-1 */ + pOp->p2 = aLabel[ADDR(pOp->p2)]; + } + + /* OPFLG_JUMP opcodes never have P2==0, though OPFLG_JUMP0 opcodes + ** might */ + assert( pOp->p2>0 + || (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP0)!=0 ); - pOp->opflags = sqlite3OpcodeProperty[opcode]; - if( (pOp->opflags & OPFLG_JUMP)!=0 && pOp->p2<0 ){ - assert( ADDR(pOp->p2)<pParse->nLabel ); - pOp->p2 = aLabel[ADDR(pOp->p2)]; + /* Jumps never go off the end of the bytecode array */ + assert( pOp->p2<p->nOp + || (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP)==0 ); + break; + } + } + /* The mkopcodeh.tcl script has so arranged things that the only + ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to + ** have non-negative values for P2. */ + assert( (sqlite3OpcodeProperty[pOp->opcode]&OPFLG_JUMP)==0 || pOp->p2>=0); } + assert( pOp>p->aOp ); + pOp--; + } +resolve_p2_values_loop_exit: + if( aLabel ){ + sqlite3DbNNFreeNN(p->db, pParse->aLabel); + pParse->aLabel = 0; } - sqlite3DbFree(p->db, pParse->aLabel); - pParse->aLabel = 0; pParse->nLabel = 0; - *pMaxFuncArgs = nMaxArgs; + *pMaxVtabArgs = nMaxVtabArgs; assert( p->bIsReader!=0 || DbMaskAllZero(p->btreeMask) ); } +#ifdef SQLITE_DEBUG +/* +** Check to see if a subroutine contains a jump to a location outside of +** the subroutine. If a jump outside the subroutine is detected, add code +** that will cause the program to halt with an error message. +** +** The subroutine consists of opcodes between iFirst and iLast. Jumps to +** locations within the subroutine are acceptable. iRetReg is a register +** that contains the return address. Jumps to outside the range of iFirst +** through iLast are also acceptable as long as the jump destination is +** an OP_Return to iReturnAddr. +** +** A jump to an unresolved label means that the jump destination will be +** beyond the current address. That is normally a jump to an early +** termination and is consider acceptable. +** +** This routine only runs during debug builds. The purpose is (of course) +** to detect invalid escapes out of a subroutine. The OP_Halt opcode +** is generated rather than an assert() or other error, so that ".eqp full" +** will still work to show the original bytecode, to aid in debugging. +*/ +void sqlite3VdbeNoJumpsOutsideSubrtn( + Vdbe *v, /* The byte-code program under construction */ + int iFirst, /* First opcode of the subroutine */ + int iLast, /* Last opcode of the subroutine */ + int iRetReg /* Subroutine return address register */ +){ + VdbeOp *pOp; + Parse *pParse; + int i; + sqlite3_str *pErr = 0; + assert( v!=0 ); + pParse = v->pParse; + assert( pParse!=0 ); + if( pParse->nErr ) return; + assert( iLast>=iFirst ); + assert( iLast<v->nOp ); + pOp = &v->aOp[iFirst]; + for(i=iFirst; i<=iLast; i++, pOp++){ + if( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP)!=0 ){ + int iDest = pOp->p2; /* Jump destination */ + if( iDest==0 ) continue; + if( pOp->opcode==OP_Gosub ) continue; + if( pOp->p3==20230325 && pOp->opcode==OP_NotNull ){ + /* This is a deliberately taken illegal branch. tag-20230325-2 */ + continue; + } + if( iDest<0 ){ + int j = ADDR(iDest); + assert( j>=0 ); + if( j>=-pParse->nLabel || pParse->aLabel[j]<0 ){ + continue; + } + iDest = pParse->aLabel[j]; + } + if( iDest<iFirst || iDest>iLast ){ + int j = iDest; + for(; j<v->nOp; j++){ + VdbeOp *pX = &v->aOp[j]; + if( pX->opcode==OP_Return ){ + if( pX->p1==iRetReg ) break; + continue; + } + if( pX->opcode==OP_Noop ) continue; + if( pX->opcode==OP_Explain ) continue; + if( pErr==0 ){ + pErr = sqlite3_str_new(0); + }else{ + sqlite3_str_appendchar(pErr, 1, '\n'); + } + sqlite3_str_appendf(pErr, + "Opcode at %d jumps to %d which is outside the " + "subroutine at %d..%d", + i, iDest, iFirst, iLast); + break; + } + } + } + } + if( pErr ){ + char *zErr = sqlite3_str_finish(pErr); + sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_INTERNAL, OE_Abort, 0, zErr, 0); + sqlite3_free(zErr); + sqlite3MayAbort(pParse); + } +} +#endif /* SQLITE_DEBUG */ + /* ** Return the address of the next instruction to be inserted. */ int sqlite3VdbeCurrentAddr(Vdbe *p){ - assert( p->magic==VDBE_MAGIC_INIT ); + assert( p->eVdbeState==VDBE_INIT_STATE ); return p->nOp; } @@ -630,19 +1077,46 @@ int sqlite3VdbeCurrentAddr(Vdbe *p){ */ #if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS) void sqlite3VdbeVerifyNoMallocRequired(Vdbe *p, int N){ - assert( p->nOp + N <= p->pParse->nOpAlloc ); + assert( p->nOp + N <= p->nOpAlloc ); +} +#endif + +/* +** Verify that the VM passed as the only argument does not contain +** an OP_ResultRow opcode. Fail an assert() if it does. This is used +** by code in pragma.c to ensure that the implementation of certain +** pragmas comports with the flags specified in the mkpragmatab.tcl +** script. +*/ +#if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS) +void sqlite3VdbeVerifyNoResultRow(Vdbe *p){ + int i; + for(i=0; i<p->nOp; i++){ + assert( p->aOp[i].opcode!=OP_ResultRow ); + } +} +#endif + +/* +** Generate code (a single OP_Abortable opcode) that will +** verify that the VDBE program can safely call Abort in the current +** context. +*/ +#if defined(SQLITE_DEBUG) +void sqlite3VdbeVerifyAbortable(Vdbe *p, int onError){ + if( onError==OE_Abort ) sqlite3VdbeAddOp0(p, OP_Abortable); } #endif /* ** This function returns a pointer to the array of opcodes associated with ** the Vdbe passed as the first argument. It is the callers responsibility -** to arrange for the returned array to be eventually freed using the +** to arrange for the returned array to be eventually freed using the ** vdbeFreeOpArray() function. ** ** Before returning, *pnOp is set to the number of entries in the returned -** array. Also, *pnMaxArg is set to the larger of its current value and -** the number of entries in the Vdbe.apArg[] array required to execute the +** array. Also, *pnMaxArg is set to the larger of its current value and +** the number of entries in the Vdbe.apArg[] array required to execute the ** returned program. */ VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){ @@ -674,8 +1148,8 @@ VdbeOp *sqlite3VdbeAddOpList( int i; VdbeOp *pOut, *pFirst; assert( nOp>0 ); - assert( p->magic==VDBE_MAGIC_INIT ); - if( p->nOp + nOp > p->pParse->nOpAlloc && growOpArray(p, nOp) ){ + assert( p->eVdbeState==VDBE_INIT_STATE ); + if( p->nOp + nOp > p->nOpAlloc && growOpArray(p, nOp) ){ return 0; } pFirst = pOut = &p->aOp[p->nOp]; @@ -716,45 +1190,128 @@ VdbeOp *sqlite3VdbeAddOpList( void sqlite3VdbeScanStatus( Vdbe *p, /* VM to add scanstatus() to */ int addrExplain, /* Address of OP_Explain (or 0) */ - int addrLoop, /* Address of loop counter */ + int addrLoop, /* Address of loop counter */ int addrVisit, /* Address of rows visited counter */ LogEst nEst, /* Estimated number of output rows */ const char *zName /* Name of table or index being scanned */ ){ - int nByte = (p->nScan+1) * sizeof(ScanStatus); - ScanStatus *aNew; - aNew = (ScanStatus*)sqlite3DbRealloc(p->db, p->aScan, nByte); - if( aNew ){ - ScanStatus *pNew = &aNew[p->nScan++]; - pNew->addrExplain = addrExplain; - pNew->addrLoop = addrLoop; - pNew->addrVisit = addrVisit; - pNew->nEst = nEst; - pNew->zName = sqlite3DbStrDup(p->db, zName); - p->aScan = aNew; + if( IS_STMT_SCANSTATUS(p->db) ){ + i64 nByte = (1+(i64)p->nScan) * sizeof(ScanStatus); + ScanStatus *aNew; + aNew = (ScanStatus*)sqlite3DbRealloc(p->db, p->aScan, nByte); + if( aNew ){ + ScanStatus *pNew = &aNew[p->nScan++]; + memset(pNew, 0, sizeof(ScanStatus)); + pNew->addrExplain = addrExplain; + pNew->addrLoop = addrLoop; + pNew->addrVisit = addrVisit; + pNew->nEst = nEst; + pNew->zName = sqlite3DbStrDup(p->db, zName); + p->aScan = aNew; + } } } -#endif + +/* +** Add the range of instructions from addrStart to addrEnd (inclusive) to +** the set of those corresponding to the sqlite3_stmt_scanstatus() counters +** associated with the OP_Explain instruction at addrExplain. The +** sum of the sqlite3Hwtime() values for each of these instructions +** will be returned for SQLITE_SCANSTAT_NCYCLE requests. +*/ +void sqlite3VdbeScanStatusRange( + Vdbe *p, + int addrExplain, + int addrStart, + int addrEnd +){ + if( IS_STMT_SCANSTATUS(p->db) ){ + ScanStatus *pScan = 0; + int ii; + for(ii=p->nScan-1; ii>=0; ii--){ + pScan = &p->aScan[ii]; + if( pScan->addrExplain==addrExplain ) break; + pScan = 0; + } + if( pScan ){ + if( addrEnd<0 ) addrEnd = sqlite3VdbeCurrentAddr(p)-1; + for(ii=0; ii<ArraySize(pScan->aAddrRange); ii+=2){ + if( pScan->aAddrRange[ii]==0 ){ + pScan->aAddrRange[ii] = addrStart; + pScan->aAddrRange[ii+1] = addrEnd; + break; + } + } + } + } +} + +/* +** Set the addresses for the SQLITE_SCANSTAT_NLOOP and SQLITE_SCANSTAT_NROW +** counters for the query element associated with the OP_Explain at +** addrExplain. +*/ +void sqlite3VdbeScanStatusCounters( + Vdbe *p, + int addrExplain, + int addrLoop, + int addrVisit +){ + if( IS_STMT_SCANSTATUS(p->db) ){ + ScanStatus *pScan = 0; + int ii; + for(ii=p->nScan-1; ii>=0; ii--){ + pScan = &p->aScan[ii]; + if( pScan->addrExplain==addrExplain ) break; + pScan = 0; + } + if( pScan ){ + if( addrLoop>0 ) pScan->addrLoop = addrLoop; + if( addrVisit>0 ) pScan->addrVisit = addrVisit; + } + } +} +#endif /* defined(SQLITE_ENABLE_STMT_SCANSTATUS) */ /* ** Change the value of the opcode, or P1, P2, P3, or P5 operands ** for a specific instruction. */ -void sqlite3VdbeChangeOpcode(Vdbe *p, u32 addr, u8 iNewOpcode){ +void sqlite3VdbeChangeOpcode(Vdbe *p, int addr, u8 iNewOpcode){ + assert( addr>=0 ); sqlite3VdbeGetOp(p,addr)->opcode = iNewOpcode; } -void sqlite3VdbeChangeP1(Vdbe *p, u32 addr, int val){ +void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){ + assert( addr>=0 ); sqlite3VdbeGetOp(p,addr)->p1 = val; } -void sqlite3VdbeChangeP2(Vdbe *p, u32 addr, int val){ +void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){ + assert( addr>=0 || p->db->mallocFailed ); sqlite3VdbeGetOp(p,addr)->p2 = val; } -void sqlite3VdbeChangeP3(Vdbe *p, u32 addr, int val){ +void sqlite3VdbeChangeP3(Vdbe *p, int addr, int val){ + assert( addr>=0 ); sqlite3VdbeGetOp(p,addr)->p3 = val; } -void sqlite3VdbeChangeP5(Vdbe *p, u8 p5){ - if( !p->db->mallocFailed ) p->aOp[p->nOp-1].p5 = p5; +void sqlite3VdbeChangeP5(Vdbe *p, u16 p5){ + assert( p->nOp>0 || p->db->mallocFailed ); + if( p->nOp>0 ) p->aOp[p->nOp-1].p5 = p5; +} + +/* +** If the previous opcode is an OP_Column that delivers results +** into register iDest, then add the OPFLAG_TYPEOFARG flag to that +** opcode. +*/ +void sqlite3VdbeTypeofColumn(Vdbe *p, int iDest){ + VdbeOp *pOp = sqlite3VdbeGetLastOp(p); +#ifdef SQLITE_DEBUG + while( pOp->opcode==OP_ReleaseReg ) pOp--; +#endif + if( pOp->p3==iDest && pOp->opcode==OP_Column ){ + pOp->p5 |= OPFLAG_TYPEOFARG; + } } /* @@ -762,93 +1319,134 @@ void sqlite3VdbeChangeP5(Vdbe *p, u8 p5){ ** the address of the next instruction to be coded. */ void sqlite3VdbeJumpHere(Vdbe *p, int addr){ - p->pParse->iFixedOp = p->nOp - 1; sqlite3VdbeChangeP2(p, addr, p->nOp); } +/* +** Change the P2 operand of the jump instruction at addr so that +** the jump lands on the next opcode. Or if the jump instruction was +** the previous opcode (and is thus a no-op) then simply back up +** the next instruction counter by one slot so that the jump is +** overwritten by the next inserted opcode. +** +** This routine is an optimization of sqlite3VdbeJumpHere() that +** strives to omit useless byte-code like this: +** +** 7 Once 0 8 0 +** 8 ... +*/ +void sqlite3VdbeJumpHereOrPopInst(Vdbe *p, int addr){ + if( addr==p->nOp-1 ){ + assert( p->aOp[addr].opcode==OP_Once + || p->aOp[addr].opcode==OP_If + || p->aOp[addr].opcode==OP_FkIfZero ); + assert( p->aOp[addr].p4type==0 ); +#ifdef SQLITE_VDBE_COVERAGE + sqlite3VdbeGetLastOp(p)->iSrcLine = 0; /* Erase VdbeCoverage() macros */ +#endif + p->nOp--; + }else{ + sqlite3VdbeChangeP2(p, addr, p->nOp); + } +} + /* ** If the input FuncDef structure is ephemeral, then free it. If -** the FuncDef is not ephermal, then do nothing. +** the FuncDef is not ephemeral, then do nothing. */ static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){ - if( ALWAYS(pDef) && (pDef->funcFlags & SQLITE_FUNC_EPHEM)!=0 ){ - sqlite3DbFree(db, pDef); + assert( db!=0 ); + if( (pDef->funcFlags & SQLITE_FUNC_EPHEM)!=0 ){ + sqlite3DbNNFreeNN(db, pDef); } } -static void vdbeFreeOpArray(sqlite3 *, Op *, int); - /* ** Delete a P4 value if necessary. */ +static SQLITE_NOINLINE void freeP4Mem(sqlite3 *db, Mem *p){ + if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc); + sqlite3DbNNFreeNN(db, p); +} +static SQLITE_NOINLINE void freeP4FuncCtx(sqlite3 *db, sqlite3_context *p){ + assert( db!=0 ); + freeEphemeralFunction(db, p->pFunc); + sqlite3DbNNFreeNN(db, p); +} static void freeP4(sqlite3 *db, int p4type, void *p4){ - if( p4 ){ - assert( db ); - switch( p4type ){ - case P4_FUNCCTX: { - freeEphemeralFunction(db, ((sqlite3_context*)p4)->pFunc); - /* Fall through into the next case */ - } - case P4_REAL: - case P4_INT64: - case P4_DYNAMIC: - case P4_INTARRAY: { - sqlite3DbFree(db, p4); - break; - } - case P4_KEYINFO: { - if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4); - break; - } + assert( db ); + switch( p4type ){ + case P4_FUNCCTX: { + freeP4FuncCtx(db, (sqlite3_context*)p4); + break; + } + case P4_REAL: + case P4_INT64: + case P4_DYNAMIC: + case P4_INTARRAY: { + if( p4 ) sqlite3DbNNFreeNN(db, p4); + break; + } + case P4_KEYINFO: { + if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4); + break; + } #ifdef SQLITE_ENABLE_CURSOR_HINTS - case P4_EXPR: { - sqlite3ExprDelete(db, (Expr*)p4); - break; - } + case P4_EXPR: { + sqlite3ExprDelete(db, (Expr*)p4); + break; + } #endif - case P4_MPRINTF: { - if( db->pnBytesFreed==0 ) sqlite3_free(p4); - break; - } - case P4_FUNCDEF: { - freeEphemeralFunction(db, (FuncDef*)p4); - break; - } - case P4_MEM: { - if( db->pnBytesFreed==0 ){ - sqlite3ValueFree((sqlite3_value*)p4); - }else{ - Mem *p = (Mem*)p4; - if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc); - sqlite3DbFree(db, p); - } - break; - } - case P4_VTAB : { - if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4); - break; + case P4_FUNCDEF: { + freeEphemeralFunction(db, (FuncDef*)p4); + break; + } + case P4_MEM: { + if( db->pnBytesFreed==0 ){ + sqlite3ValueFree((sqlite3_value*)p4); + }else{ + freeP4Mem(db, (Mem*)p4); } + break; + } + case P4_VTAB : { + if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4); + break; + } + case P4_TABLEREF: { + if( db->pnBytesFreed==0 ) sqlite3DeleteTable(db, (Table*)p4); + break; + } + case P4_SUBRTNSIG: { + SubrtnSig *pSig = (SubrtnSig*)p4; + sqlite3DbFree(db, pSig->zAff); + sqlite3DbFree(db, pSig); + break; } } } /* ** Free the space allocated for aOp and any p4 values allocated for the -** opcodes contained within. If aOp is not NULL it is assumed to contain -** nOp entries. +** opcodes contained within. If aOp is not NULL it is assumed to contain +** nOp entries. */ static void vdbeFreeOpArray(sqlite3 *db, Op *aOp, int nOp){ + assert( nOp>=0 ); + assert( db!=0 ); if( aOp ){ - Op *pOp; - for(pOp=aOp; pOp<&aOp[nOp]; pOp++){ - if( pOp->p4type ) freeP4(db, pOp->p4type, pOp->p4.p); + Op *pOp = &aOp[nOp-1]; + while(1){ /* Exit via break */ + if( pOp->p4type <= P4_FREE_IF_LE ) freeP4(db, pOp->p4type, pOp->p4.p); #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS sqlite3DbFree(db, pOp->zComment); -#endif +#endif + if( pOp==aOp ) break; + pOp--; } + sqlite3DbNNFreeNN(db, aOp); } - sqlite3DbFree(db, aOp); } /* @@ -861,6 +1459,13 @@ void sqlite3VdbeLinkSubProgram(Vdbe *pVdbe, SubProgram *p){ pVdbe->pProgram = p; } +/* +** Return true if the given Vdbe has any SubPrograms. +*/ +int sqlite3VdbeHasSubProgram(Vdbe *pVdbe){ + return pVdbe->pProgram!=0; +} + /* ** Change the opcode at addr into OP_Noop */ @@ -881,13 +1486,47 @@ int sqlite3VdbeChangeToNoop(Vdbe *p, int addr){ ** then remove it. Return true if and only if an opcode was removed. */ int sqlite3VdbeDeletePriorOpcode(Vdbe *p, u8 op){ - if( (p->nOp-1)>(p->pParse->iFixedOp) && p->aOp[p->nOp-1].opcode==op ){ + if( p->nOp>0 && p->aOp[p->nOp-1].opcode==op ){ return sqlite3VdbeChangeToNoop(p, p->nOp-1); }else{ return 0; } } +#ifdef SQLITE_DEBUG +/* +** Generate an OP_ReleaseReg opcode to indicate that a range of +** registers, except any identified by mask, are no longer in use. +*/ +void sqlite3VdbeReleaseRegisters( + Parse *pParse, /* Parsing context */ + int iFirst, /* Index of first register to be released */ + int N, /* Number of registers to release */ + u32 mask, /* Mask of registers to NOT release */ + int bUndefine /* If true, mark registers as undefined */ +){ + if( N==0 || OptimizationDisabled(pParse->db, SQLITE_ReleaseReg) ) return; + assert( pParse->pVdbe ); + assert( iFirst>=1 ); + assert( iFirst+N-1<=pParse->nMem ); + if( N<=31 && mask!=0 ){ + while( N>0 && (mask&1)!=0 ){ + mask >>= 1; + iFirst++; + N--; + } + while( N>0 && N<=32 && (mask & MASKBIT32(N-1))!=0 ){ + mask &= ~MASKBIT32(N-1); + N--; + } + } + if( N>0 ){ + sqlite3VdbeAddOp3(pParse->pVdbe, OP_ReleaseReg, iFirst, N, *(int*)&mask); + if( bUndefine ) sqlite3VdbeChangeP5(pParse->pVdbe, 1); + } +} +#endif /* SQLITE_DEBUG */ + /* ** Change the value of the P4 operand for a specific instruction. ** This routine is useful when a large program is loaded from a @@ -898,7 +1537,7 @@ int sqlite3VdbeDeletePriorOpcode(Vdbe *p, u8 op){ ** the string is made into memory obtained from sqlite3_malloc(). ** A value of n==0 means copy bytes of zP4 up to and including the ** first null byte. If n>0 then copy n+1 bytes of zP4. -** +** ** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points ** to a string or structure that is guaranteed to exist for the lifetime of ** the Vdbe. In these cases we can just copy the pointer. @@ -912,7 +1551,7 @@ static void SQLITE_NOINLINE vdbeChangeP4Full( int n ){ if( pOp->p4type ){ - freeP4(p->db, pOp->p4type, pOp->p4.p); + assert( pOp->p4type > P4_FREE_IF_LE ); pOp->p4type = 0; pOp->p4.p = 0; } @@ -929,7 +1568,7 @@ void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){ sqlite3 *db; assert( p!=0 ); db = p->db; - assert( p->magic==VDBE_MAGIC_INIT ); + assert( p->eVdbeState==VDBE_INIT_STATE ); assert( p->aOp!=0 || db->mallocFailed ); if( db->mallocFailed ){ if( n!=P4_VTAB ) freeP4(db, n, (void*)*(char**)&zP4); @@ -958,16 +1597,42 @@ void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){ } } +/* +** Change the P4 operand of the most recently coded instruction +** to the value defined by the arguments. This is a high-speed +** version of sqlite3VdbeChangeP4(). +** +** The P4 operand must not have been previously defined. And the new +** P4 must not be P4_INT32. Use sqlite3VdbeChangeP4() in either of +** those cases. +*/ +void sqlite3VdbeAppendP4(Vdbe *p, void *pP4, int n){ + VdbeOp *pOp; + assert( n!=P4_INT32 && n!=P4_VTAB ); + assert( n<=0 ); + if( p->db->mallocFailed ){ + freeP4(p->db, n, pP4); + }else{ + assert( pP4!=0 || n==P4_DYNAMIC ); + assert( p->nOp>0 ); + pOp = &p->aOp[p->nOp-1]; + assert( pOp->p4type==P4_NOTUSED ); + pOp->p4type = n; + pOp->p4.p = pP4; + } +} + /* ** Set the P4 on the most recently added opcode to the KeyInfo for the ** index given. */ void sqlite3VdbeSetP4KeyInfo(Parse *pParse, Index *pIdx){ Vdbe *v = pParse->pVdbe; + KeyInfo *pKeyInfo; assert( v!=0 ); assert( pIdx!=0 ); - sqlite3VdbeChangeP4(v, -1, (char*)sqlite3KeyInfoOfIndex(pParse, pIdx), - P4_KEYINFO); + pKeyInfo = sqlite3KeyInfoOfIndex(pParse, pIdx); + if( pKeyInfo ) sqlite3VdbeAppendP4(v, pKeyInfo, P4_KEYINFO); } #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS @@ -979,7 +1644,7 @@ void sqlite3VdbeSetP4KeyInfo(Parse *pParse, Index *pIdx){ */ static void vdbeVComment(Vdbe *p, const char *zFormat, va_list ap){ assert( p->nOp>0 || p->aOp==0 ); - assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed ); + assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->pParse->nErr>0 ); if( p->nOp ){ assert( p->aOp ); sqlite3DbFree(p->db, p->aOp[p->nOp-1].zComment); @@ -1010,19 +1675,19 @@ void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){ ** Set the value if the iSrcLine field for the previously coded instruction. */ void sqlite3VdbeSetLineNumber(Vdbe *v, int iLine){ - sqlite3VdbeGetOp(v,-1)->iSrcLine = iLine; + sqlite3VdbeGetLastOp(v)->iSrcLine = iLine; } #endif /* SQLITE_VDBE_COVERAGE */ /* -** Return the opcode for a given address. If the address is -1, then -** return the most recently inserted opcode. +** Return the opcode for a given address. The address must be non-negative. +** See sqlite3VdbeGetLastOp() to get the most recently added opcode. ** ** If a memory allocation error has occurred prior to the calling of this ** routine, then a pointer to a dummy VdbeOp will be returned. That opcode ** is readable but not writable, though it is cast to a writable value. ** The return of a dummy opcode allows the call to continue functioning -** after an OOM fault without having to check to see if the return from +** after an OOM fault without having to check to see if the return from ** this routine is a valid pointer. But because the dummy.opcode is 0, ** dummy will never be written to. This is verified by code inspection and ** by running with Valgrind. @@ -1031,10 +1696,7 @@ VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){ /* C89 specifies that the constant "dummy" will be initialized to all ** zeros, which is correct. MSVC generates a warning, nevertheless. */ static VdbeOp dummy; /* Ignore the MSVC warning about no initializer */ - assert( p->magic==VDBE_MAGIC_INIT ); - if( addr<0 ){ - addr = p->nOp - 1; - } + assert( p->eVdbeState==VDBE_INIT_STATE ); assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed ); if( p->db->mallocFailed ){ return (VdbeOp*)&dummy; @@ -1043,6 +1705,12 @@ VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){ } } +/* Return the most recently added opcode +*/ +VdbeOp *sqlite3VdbeGetLastOp(Vdbe *p){ + return sqlite3VdbeGetOp(p, p->nOp - 1); +} + #if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) /* ** Return an integer value for one of the parameters to the opcode pOp @@ -1069,69 +1737,90 @@ static int translateP(char c, const Op *pOp){ ** "PX@PY+1" -> "r[X..X+Y]" or "r[x]" if y is 0 ** "PY..PY" -> "r[X..Y]" or "r[x]" if y<=x */ -static int displayComment( +char *sqlite3VdbeDisplayComment( + sqlite3 *db, /* Optional - Oom error reporting only */ const Op *pOp, /* The opcode to be commented */ - const char *zP4, /* Previously obtained value for P4 */ - char *zTemp, /* Write result here */ - int nTemp /* Space available in zTemp[] */ + const char *zP4 /* Previously obtained value for P4 */ ){ const char *zOpName; const char *zSynopsis; int nOpName; - int ii, jj; + int ii; + char zAlt[50]; + StrAccum x; + + sqlite3StrAccumInit(&x, 0, 0, 0, SQLITE_MAX_LENGTH); zOpName = sqlite3OpcodeName(pOp->opcode); nOpName = sqlite3Strlen30(zOpName); if( zOpName[nOpName+1] ){ int seenCom = 0; char c; - zSynopsis = zOpName += nOpName + 1; - for(ii=jj=0; jj<nTemp-1 && (c = zSynopsis[ii])!=0; ii++){ + zSynopsis = zOpName + nOpName + 1; + if( strncmp(zSynopsis,"IF ",3)==0 ){ + sqlite3_snprintf(sizeof(zAlt), zAlt, "if %s goto P2", zSynopsis+3); + zSynopsis = zAlt; + } + for(ii=0; (c = zSynopsis[ii])!=0; ii++){ if( c=='P' ){ c = zSynopsis[++ii]; if( c=='4' ){ - sqlite3_snprintf(nTemp-jj, zTemp+jj, "%s", zP4); + sqlite3_str_appendall(&x, zP4); }else if( c=='X' ){ - sqlite3_snprintf(nTemp-jj, zTemp+jj, "%s", pOp->zComment); - seenCom = 1; + if( pOp->zComment && pOp->zComment[0] ){ + sqlite3_str_appendall(&x, pOp->zComment); + seenCom = 1; + break; + } }else{ int v1 = translateP(c, pOp); int v2; - sqlite3_snprintf(nTemp-jj, zTemp+jj, "%d", v1); if( strncmp(zSynopsis+ii+1, "@P", 2)==0 ){ ii += 3; - jj += sqlite3Strlen30(zTemp+jj); v2 = translateP(zSynopsis[ii], pOp); if( strncmp(zSynopsis+ii+1,"+1",2)==0 ){ ii += 2; v2++; } - if( v2>1 ){ - sqlite3_snprintf(nTemp-jj, zTemp+jj, "..%d", v1+v2-1); + if( v2<2 ){ + sqlite3_str_appendf(&x, "%d", v1); + }else{ + sqlite3_str_appendf(&x, "%d..%d", v1, v1+v2-1); + } + }else if( strncmp(zSynopsis+ii+1, "@NP", 3)==0 ){ + sqlite3_context *pCtx = pOp->p4.pCtx; + if( pOp->p4type!=P4_FUNCCTX || pCtx->argc==1 ){ + sqlite3_str_appendf(&x, "%d", v1); + }else if( pCtx->argc>1 ){ + sqlite3_str_appendf(&x, "%d..%d", v1, v1+pCtx->argc-1); + }else if( x.accError==0 ){ + assert( x.nChar>2 ); + x.nChar -= 2; + ii++; + } + ii += 3; + }else{ + sqlite3_str_appendf(&x, "%d", v1); + if( strncmp(zSynopsis+ii+1, "..P3", 4)==0 && pOp->p3==0 ){ + ii += 4; } - }else if( strncmp(zSynopsis+ii+1, "..P3", 4)==0 && pOp->p3==0 ){ - ii += 4; } } - jj += sqlite3Strlen30(zTemp+jj); }else{ - zTemp[jj++] = c; + sqlite3_str_appendchar(&x, 1, c); } } - if( !seenCom && jj<nTemp-5 && pOp->zComment ){ - sqlite3_snprintf(nTemp-jj, zTemp+jj, "; %s", pOp->zComment); - jj += sqlite3Strlen30(zTemp+jj); + if( !seenCom && pOp->zComment ){ + sqlite3_str_appendf(&x, "; %s", pOp->zComment); } - if( jj<nTemp ) zTemp[jj] = 0; }else if( pOp->zComment ){ - sqlite3_snprintf(nTemp, zTemp, "%s", pOp->zComment); - jj = sqlite3Strlen30(zTemp); - }else{ - zTemp[0] = 0; - jj = 0; + sqlite3_str_appendall(&x, pOp->zComment); + } + if( (x.accError & SQLITE_NOMEM)!=0 && db!=0 ){ + sqlite3OomFault(db); } - return jj; + return sqlite3StrAccumFinish(&x); } -#endif /* SQLITE_DEBUG */ +#endif /* SQLITE_ENABLE_EXPLAIN_COMMENTS */ #if VDBE_DISPLAY_P4 && defined(SQLITE_ENABLE_CURSOR_HINTS) /* @@ -1142,23 +1831,24 @@ static void displayP4Expr(StrAccum *p, Expr *pExpr){ const char *zOp = 0; switch( pExpr->op ){ case TK_STRING: - sqlite3XPrintf(p, "%Q", pExpr->u.zToken); + assert( !ExprHasProperty(pExpr, EP_IntValue) ); + sqlite3_str_appendf(p, "%Q", pExpr->u.zToken); break; case TK_INTEGER: - sqlite3XPrintf(p, "%d", pExpr->u.iValue); + sqlite3_str_appendf(p, "%d", pExpr->u.iValue); break; case TK_NULL: - sqlite3XPrintf(p, "NULL"); + sqlite3_str_appendf(p, "NULL"); break; case TK_REGISTER: { - sqlite3XPrintf(p, "r[%d]", pExpr->iTable); + sqlite3_str_appendf(p, "r[%d]", pExpr->iTable); break; } case TK_COLUMN: { if( pExpr->iColumn<0 ){ - sqlite3XPrintf(p, "rowid"); + sqlite3_str_appendf(p, "rowid"); }else{ - sqlite3XPrintf(p, "c%d", (int)pExpr->iColumn); + sqlite3_str_appendf(p, "c%d", (int)pExpr->iColumn); } break; } @@ -1190,18 +1880,18 @@ static void displayP4Expr(StrAccum *p, Expr *pExpr){ case TK_NOTNULL: zOp = "NOTNULL"; break; default: - sqlite3XPrintf(p, "%s", "expr"); + sqlite3_str_appendf(p, "%s", "expr"); break; } if( zOp ){ - sqlite3XPrintf(p, "%s(", zOp); + sqlite3_str_appendf(p, "%s(", zOp); displayP4Expr(p, pExpr->pLeft); if( pExpr->pRight ){ - sqlite3StrAccumAppend(p, ",", 1); + sqlite3_str_append(p, ",", 1); displayP4Expr(p, pExpr->pRight); } - sqlite3StrAccumAppend(p, ")", 1); + sqlite3_str_append(p, ")", 1); } } #endif /* VDBE_DISPLAY_P4 && defined(SQLITE_ENABLE_CURSOR_HINTS) */ @@ -1212,24 +1902,27 @@ static void displayP4Expr(StrAccum *p, Expr *pExpr){ ** Compute a string that describes the P4 parameter for an opcode. ** Use zTemp for any required temporary buffer space. */ -static char *displayP4(Op *pOp, char *zTemp, int nTemp){ - char *zP4 = zTemp; +char *sqlite3VdbeDisplayP4(sqlite3 *db, Op *pOp){ + char *zP4 = 0; StrAccum x; - assert( nTemp>=20 ); - sqlite3StrAccumInit(&x, 0, zTemp, nTemp, 0); + + sqlite3StrAccumInit(&x, 0, 0, 0, SQLITE_MAX_LENGTH); switch( pOp->p4type ){ case P4_KEYINFO: { int j; KeyInfo *pKeyInfo = pOp->p4.pKeyInfo; - assert( pKeyInfo->aSortOrder!=0 ); - sqlite3XPrintf(&x, "k(%d", pKeyInfo->nField); - for(j=0; j<pKeyInfo->nField; j++){ + assert( pKeyInfo->aSortFlags!=0 ); + sqlite3_str_appendf(&x, "k(%d", pKeyInfo->nKeyField); + for(j=0; j<pKeyInfo->nKeyField; j++){ CollSeq *pColl = pKeyInfo->aColl[j]; const char *zColl = pColl ? pColl->zName : ""; if( strcmp(zColl, "BINARY")==0 ) zColl = "B"; - sqlite3XPrintf(&x, ",%s%s", pKeyInfo->aSortOrder[j] ? "-" : "", zColl); + sqlite3_str_appendf(&x, ",%s%s%s", + (pKeyInfo->aSortFlags[j] & KEYINFO_ORDER_DESC) ? "-" : "", + (pKeyInfo->aSortFlags[j] & KEYINFO_ORDER_BIGNULL)? "N." : "", + zColl); } - sqlite3StrAccumAppend(&x, ")", 1); + sqlite3_str_append(&x, ")", 1); break; } #ifdef SQLITE_ENABLE_CURSOR_HINTS @@ -1239,42 +1932,43 @@ static char *displayP4(Op *pOp, char *zTemp, int nTemp){ } #endif case P4_COLLSEQ: { + static const char *const encnames[] = {"?", "8", "16LE", "16BE"}; CollSeq *pColl = pOp->p4.pColl; - sqlite3XPrintf(&x, "(%.20s)", pColl->zName); + assert( pColl->enc<4 ); + sqlite3_str_appendf(&x, "%.18s-%s", pColl->zName, + encnames[pColl->enc]); break; } case P4_FUNCDEF: { FuncDef *pDef = pOp->p4.pFunc; - sqlite3XPrintf(&x, "%s(%d)", pDef->zName, pDef->nArg); + sqlite3_str_appendf(&x, "%s(%d)", pDef->zName, pDef->nArg); break; } -#ifdef SQLITE_DEBUG case P4_FUNCCTX: { FuncDef *pDef = pOp->p4.pCtx->pFunc; - sqlite3XPrintf(&x, "%s(%d)", pDef->zName, pDef->nArg); + sqlite3_str_appendf(&x, "%s(%d)", pDef->zName, pDef->nArg); break; } -#endif case P4_INT64: { - sqlite3XPrintf(&x, "%lld", *pOp->p4.pI64); + sqlite3_str_appendf(&x, "%lld", *pOp->p4.pI64); break; } case P4_INT32: { - sqlite3XPrintf(&x, "%d", pOp->p4.i); + sqlite3_str_appendf(&x, "%d", pOp->p4.i); break; } case P4_REAL: { - sqlite3XPrintf(&x, "%.16g", *pOp->p4.pReal); + sqlite3_str_appendf(&x, "%.16g", *pOp->p4.pReal); break; } case P4_MEM: { Mem *pMem = pOp->p4.pMem; if( pMem->flags & MEM_Str ){ zP4 = pMem->z; - }else if( pMem->flags & MEM_Int ){ - sqlite3XPrintf(&x, "%lld", pMem->u.i); + }else if( pMem->flags & (MEM_Int|MEM_IntReal) ){ + sqlite3_str_appendf(&x, "%lld", pMem->u.i); }else if( pMem->flags & MEM_Real ){ - sqlite3XPrintf(&x, "%.16g", pMem->u.r); + sqlite3_str_appendf(&x, "%.16g", pMem->u.r); }else if( pMem->flags & MEM_Null ){ zP4 = "NULL"; }else{ @@ -1286,41 +1980,43 @@ static char *displayP4(Op *pOp, char *zTemp, int nTemp){ #ifndef SQLITE_OMIT_VIRTUALTABLE case P4_VTAB: { sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab; - sqlite3XPrintf(&x, "vtab:%p", pVtab); + sqlite3_str_appendf(&x, "vtab:%p", pVtab); break; } #endif case P4_INTARRAY: { - int i; - int *ai = pOp->p4.ai; - int n = ai[0]; /* The first element of an INTARRAY is always the + u32 i; + u32 *ai = pOp->p4.ai; + u32 n = ai[0]; /* The first element of an INTARRAY is always the ** count of the number of elements to follow */ - for(i=1; i<n; i++){ - sqlite3XPrintf(&x, ",%d", ai[i]); + for(i=1; i<=n; i++){ + sqlite3_str_appendf(&x, "%c%u", (i==1 ? '[' : ','), ai[i]); } - zTemp[0] = '['; - sqlite3StrAccumAppend(&x, "]", 1); + sqlite3_str_append(&x, "]", 1); break; } case P4_SUBPROGRAM: { - sqlite3XPrintf(&x, "program"); + zP4 = "program"; + break; + } + case P4_TABLE: { + zP4 = pOp->p4.pTab->zName; break; } - case P4_ADVANCE: { - zTemp[0] = 0; + case P4_SUBRTNSIG: { + SubrtnSig *pSig = pOp->p4.pSubrtnSig; + sqlite3_str_appendf(&x, "subrtnsig:%d,%s", pSig->selId, pSig->zAff); break; } default: { zP4 = pOp->p4.z; - if( zP4==0 ){ - zP4 = zTemp; - zTemp[0] = 0; - } } } - sqlite3StrAccumFinish(&x); - assert( zP4!=0 ); - return zP4; + if( zP4 ) sqlite3_str_appendall(&x, zP4); + if( (x.accError & SQLITE_NOMEM)!=0 ){ + sqlite3OomFault(db); + } + return sqlite3StrAccumFinish(&x); } #endif /* VDBE_DISPLAY_P4 */ @@ -1351,13 +2047,13 @@ void sqlite3VdbeUsesBtree(Vdbe *p, int i){ ** ** If SQLite is not threadsafe but does support shared-cache mode, then ** sqlite3BtreeEnter() is invoked to set the BtShared.db variables -** of all of BtShared structures accessible via the database handle +** of all of BtShared structures accessible via the database handle ** associated with the VM. ** ** If SQLite is not threadsafe and does not support shared-cache mode, this ** function is a no-op. ** -** The p->btreeMask field is a bitmask of all btrees that the prepared +** The p->btreeMask field is a bitmask of all btrees that the prepared ** statement p will ever use. Let N be the number of bits in p->btreeMask ** corresponding to btrees that use shared cache. Then the runtime of ** this routine is N*N. But as N is rarely more than 1, this should not @@ -1408,36 +2104,79 @@ void sqlite3VdbeLeave(Vdbe *p){ /* ** Print a single opcode. This routine is used for debugging only. */ -void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){ +void sqlite3VdbePrintOp(FILE *pOut, int pc, VdbeOp *pOp){ char *zP4; - char zPtr[50]; - char zCom[100]; + char *zCom; + sqlite3 dummyDb; static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-13s %.2X %s\n"; if( pOut==0 ) pOut = stdout; - zP4 = displayP4(pOp, zPtr, sizeof(zPtr)); + sqlite3BeginBenignMalloc(); + dummyDb.mallocFailed = 1; + zP4 = sqlite3VdbeDisplayP4(&dummyDb, pOp); #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS - displayComment(pOp, zP4, zCom, sizeof(zCom)); + zCom = sqlite3VdbeDisplayComment(0, pOp, zP4); #else - zCom[0] = 0; + zCom = 0; #endif /* NB: The sqlite3OpcodeName() function is implemented by code created ** by the mkopcodeh.awk and mkopcodec.awk scripts which extract the ** information from the vdbe.c source text */ - fprintf(pOut, zFormat1, pc, - sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5, - zCom + fprintf(pOut, zFormat1, pc, + sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, + zP4 ? zP4 : "", pOp->p5, + zCom ? zCom : "" ); fflush(pOut); + sqlite3_free(zP4); + sqlite3_free(zCom); + sqlite3EndBenignMalloc(); } #endif /* -** Release an array of N Mem elements +** Initialize an array of N Mem element. +** +** This is a high-runner, so only those fields that really do need to +** be initialized are set. The Mem structure is organized so that +** the fields that get initialized are nearby and hopefully on the same +** cache line. +** +** Mem.flags = flags +** Mem.db = db +** Mem.szMalloc = 0 +** +** All other fields of Mem can safely remain uninitialized for now. They +** will be initialized before use. +*/ +static void initMemArray(Mem *p, int N, sqlite3 *db, u16 flags){ + assert( db!=0 ); + if( N>0 ){ + do{ + p->flags = flags; + p->db = db; + p->szMalloc = 0; +#ifdef SQLITE_DEBUG + p->pScopyFrom = 0; + p->bScopy = 0; +#endif + p++; + }while( (--N)>0 ); + } +} + +/* +** Release auxiliary memory held in an array of N Mem elements. +** +** After this routine returns, all Mem elements in the array will still +** be valid. Those Mem elements that were not holding auxiliary resources +** will be unchanged. Mem elements which had something freed will be +** set to MEM_Undefined. */ static void releaseMemArray(Mem *p, int N){ if( p && N ){ Mem *pEnd = &p[N]; sqlite3 *db = p->db; + assert( db!=0 ); if( db->pnBytesFreed ){ do{ if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc); @@ -1449,33 +2188,181 @@ static void releaseMemArray(Mem *p, int N){ assert( sqlite3VdbeCheckMemInvariants(p) ); /* This block is really an inlined version of sqlite3VdbeMemRelease() - ** that takes advantage of the fact that the memory cell value is + ** that takes advantage of the fact that the memory cell value is ** being set to NULL after releasing any dynamic resources. ** - ** The justification for duplicating code is that according to - ** callgrind, this causes a certain test case to hit the CPU 4.7 - ** percent less (x86 linux, gcc version 4.1.2, -O6) than if + ** The justification for duplicating code is that according to + ** callgrind, this causes a certain test case to hit the CPU 4.7 + ** percent less (x86 linux, gcc version 4.1.2, -O6) than if ** sqlite3MemRelease() were called from here. With -O2, this jumps - ** to 6.6 percent. The test case is inserting 1000 rows into a table - ** with no indexes using a single prepared INSERT statement, bind() + ** to 6.6 percent. The test case is inserting 1000 rows into a table + ** with no indexes using a single prepared INSERT statement, bind() ** and reset(). Inserts are grouped into a transaction. */ testcase( p->flags & MEM_Agg ); testcase( p->flags & MEM_Dyn ); - testcase( p->flags & MEM_Frame ); - testcase( p->flags & MEM_RowSet ); - if( p->flags&(MEM_Agg|MEM_Dyn|MEM_Frame|MEM_RowSet) ){ + if( p->flags&(MEM_Agg|MEM_Dyn) ){ + testcase( (p->flags & MEM_Dyn)!=0 && p->xDel==sqlite3VdbeFrameMemDel ); sqlite3VdbeMemRelease(p); + p->flags = MEM_Undefined; }else if( p->szMalloc ){ - sqlite3DbFree(db, p->zMalloc); + sqlite3DbNNFreeNN(db, p->zMalloc); p->szMalloc = 0; + p->flags = MEM_Undefined; } - - p->flags = MEM_Undefined; +#ifdef SQLITE_DEBUG + else{ + p->flags = MEM_Undefined; + } +#endif }while( (++p)<pEnd ); } } +#ifdef SQLITE_DEBUG +/* +** Verify that pFrame is a valid VdbeFrame pointer. Return true if it is +** and false if something is wrong. +** +** This routine is intended for use inside of assert() statements only. +*/ +int sqlite3VdbeFrameIsValid(VdbeFrame *pFrame){ + if( pFrame->iFrameMagic!=SQLITE_FRAME_MAGIC ) return 0; + return 1; +} +#endif + + +/* +** This is a destructor on a Mem object (which is really an sqlite3_value) +** that deletes the Frame object that is attached to it as a blob. +** +** This routine does not delete the Frame right away. It merely adds the +** frame to a list of frames to be deleted when the Vdbe halts. +*/ +void sqlite3VdbeFrameMemDel(void *pArg){ + VdbeFrame *pFrame = (VdbeFrame*)pArg; + assert( sqlite3VdbeFrameIsValid(pFrame) ); + pFrame->pParent = pFrame->v->pDelFrame; + pFrame->v->pDelFrame = pFrame; +} + +#if defined(SQLITE_ENABLE_BYTECODE_VTAB) || !defined(SQLITE_OMIT_EXPLAIN) +/* +** Locate the next opcode to be displayed in EXPLAIN or EXPLAIN +** QUERY PLAN output. +** +** Return SQLITE_ROW on success. Return SQLITE_DONE if there are no +** more opcodes to be displayed. +*/ +int sqlite3VdbeNextOpcode( + Vdbe *p, /* The statement being explained */ + Mem *pSub, /* Storage for keeping track of subprogram nesting */ + int eMode, /* 0: normal. 1: EQP. 2: TablesUsed */ + int *piPc, /* IN/OUT: Current rowid. Overwritten with next rowid */ + int *piAddr, /* OUT: Write index into (*paOp)[] here */ + Op **paOp /* OUT: Write the opcode array here */ +){ + int nRow; /* Stop when row count reaches this */ + int nSub = 0; /* Number of sub-vdbes seen so far */ + SubProgram **apSub = 0; /* Array of sub-vdbes */ + int i; /* Next instruction address */ + int rc = SQLITE_OK; /* Result code */ + Op *aOp = 0; /* Opcode array */ + int iPc; /* Rowid. Copy of value in *piPc */ + + /* When the number of output rows reaches nRow, that means the + ** listing has finished and sqlite3_step() should return SQLITE_DONE. + ** nRow is the sum of the number of rows in the main program, plus + ** the sum of the number of rows in all trigger subprograms encountered + ** so far. The nRow value will increase as new trigger subprograms are + ** encountered, but p->pc will eventually catch up to nRow. + */ + nRow = p->nOp; + if( pSub!=0 ){ + if( pSub->flags&MEM_Blob ){ + /* pSub is initiallly NULL. It is initialized to a BLOB by + ** the P4_SUBPROGRAM processing logic below */ + nSub = pSub->n/sizeof(Vdbe*); + apSub = (SubProgram **)pSub->z; + } + for(i=0; i<nSub; i++){ + nRow += apSub[i]->nOp; + } + } + iPc = *piPc; + while(1){ /* Loop exits via break */ + i = iPc++; + if( i>=nRow ){ + p->rc = SQLITE_OK; + rc = SQLITE_DONE; + break; + } + if( i<p->nOp ){ + /* The rowid is small enough that we are still in the + ** main program. */ + aOp = p->aOp; + }else{ + /* We are currently listing subprograms. Figure out which one and + ** pick up the appropriate opcode. */ + int j; + i -= p->nOp; + assert( apSub!=0 ); + assert( nSub>0 ); + for(j=0; i>=apSub[j]->nOp; j++){ + i -= apSub[j]->nOp; + assert( i<apSub[j]->nOp || j+1<nSub ); + } + aOp = apSub[j]->aOp; + } + + /* When an OP_Program opcode is encounter (the only opcode that has + ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms + ** kept in p->aMem[9].z to hold the new program - assuming this subprogram + ** has not already been seen. + */ + if( pSub!=0 && aOp[i].p4type==P4_SUBPROGRAM ){ + int nByte = (nSub+1)*sizeof(SubProgram*); + int j; + for(j=0; j<nSub; j++){ + if( apSub[j]==aOp[i].p4.pProgram ) break; + } + if( j==nSub ){ + p->rc = sqlite3VdbeMemGrow(pSub, nByte, nSub!=0); + if( p->rc!=SQLITE_OK ){ + rc = SQLITE_ERROR; + break; + } + apSub = (SubProgram **)pSub->z; + apSub[nSub++] = aOp[i].p4.pProgram; + MemSetTypeFlag(pSub, MEM_Blob); + pSub->n = nSub*sizeof(SubProgram*); + nRow += aOp[i].p4.pProgram->nOp; + } + } + if( eMode==0 ) break; +#ifdef SQLITE_ENABLE_BYTECODE_VTAB + if( eMode==2 ){ + Op *pOp = aOp + i; + if( pOp->opcode==OP_OpenRead ) break; + if( pOp->opcode==OP_OpenWrite && (pOp->p5 & OPFLAG_P2ISREG)==0 ) break; + if( pOp->opcode==OP_ReopenIdx ) break; + }else +#endif + { + assert( eMode==1 ); + if( aOp[i].opcode==OP_Explain ) break; + if( aOp[i].opcode==OP_Init && iPc>1 ) break; + } + } + *piPc = iPc; + *piAddr = i; + *paOp = aOp; + return rc; +} +#endif /* SQLITE_ENABLE_BYTECODE_VTAB || !SQLITE_OMIT_EXPLAIN */ + + /* ** Delete a VdbeFrame object and its contents. VdbeFrame objects are ** allocated by the OP_Program opcode in sqlite3VdbeExec(). @@ -1484,10 +2371,12 @@ void sqlite3VdbeFrameDelete(VdbeFrame *p){ int i; Mem *aMem = VdbeFrameMem(p); VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem]; + assert( sqlite3VdbeFrameIsValid(p) ); for(i=0; i<p->nChildCsr; i++){ - sqlite3VdbeFreeCursor(p->v, apCsr[i]); + if( apCsr[i] ) sqlite3VdbeFreeCursorNN(p->v, apCsr[i]); } releaseMemArray(aMem, p->nChildMem); + sqlite3VdbeDeleteAuxData(p->v->db, &p->pAuxData, -1, 0); sqlite3DbFree(p->v->db, p); } @@ -1503,6 +2392,9 @@ void sqlite3VdbeFrameDelete(VdbeFrame *p){ ** p->explain==2, only OP_Explain instructions are listed and these ** are shown in a different format. p->explain==2 is used to implement ** EXPLAIN QUERY PLAN. +** 2018-04-24: In p->explain==2 mode, the OP_Init opcodes of triggers +** are also shown, so that the boundaries between the main program and +** each trigger are clear. ** ** When p->explain==1, first the main program is listed, then each of ** the trigger subprograms are listed one by one. @@ -1510,17 +2402,17 @@ void sqlite3VdbeFrameDelete(VdbeFrame *p){ int sqlite3VdbeList( Vdbe *p /* The VDBE */ ){ - int nRow; /* Stop when row count reaches this */ - int nSub = 0; /* Number of sub-vdbes seen so far */ - SubProgram **apSub = 0; /* Array of sub-vdbes */ Mem *pSub = 0; /* Memory cell hold array of subprogs */ sqlite3 *db = p->db; /* The database connection */ int i; /* Loop counter */ int rc = SQLITE_OK; /* Return code */ Mem *pMem = &p->aMem[1]; /* First Mem of result set */ + int bListSubprogs = (p->explain==1 || (db->flags & SQLITE_TriggerEQP)!=0); + Op *aOp; /* Array of opcodes */ + Op *pOp; /* Current opcode */ assert( p->explain ); - assert( p->magic==VDBE_MAGIC_RUN ); + assert( p->eVdbeState==VDBE_RUN_STATE ); assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM ); /* Even though this opcode does not use dynamic strings for @@ -1528,7 +2420,6 @@ int sqlite3VdbeList( ** sqlite3_column_text16(), causing a translation to UTF-16 encoding. */ releaseMemArray(pMem, 8); - p->pResultSet = 0; if( p->rc==SQLITE_NOMEM ){ /* This happens if a malloc() inside a call to sqlite3_column_text() or @@ -1537,146 +2428,63 @@ int sqlite3VdbeList( return SQLITE_ERROR; } - /* When the number of output rows reaches nRow, that means the - ** listing has finished and sqlite3_step() should return SQLITE_DONE. - ** nRow is the sum of the number of rows in the main program, plus - ** the sum of the number of rows in all trigger subprograms encountered - ** so far. The nRow value will increase as new trigger subprograms are - ** encountered, but p->pc will eventually catch up to nRow. - */ - nRow = p->nOp; - if( p->explain==1 ){ + if( bListSubprogs ){ /* The first 8 memory cells are used for the result set. So we will ** commandeer the 9th cell to use as storage for an array of pointers ** to trigger subprograms. The VDBE is guaranteed to have at least 9 ** cells. */ assert( p->nMem>9 ); pSub = &p->aMem[9]; - if( pSub->flags&MEM_Blob ){ - /* On the first call to sqlite3_step(), pSub will hold a NULL. It is - ** initialized to a BLOB by the P4_SUBPROGRAM processing logic below */ - nSub = pSub->n/sizeof(Vdbe*); - apSub = (SubProgram **)pSub->z; - } - for(i=0; i<nSub; i++){ - nRow += apSub[i]->nOp; - } - } - - do{ - i = p->pc++; - }while( i<nRow && p->explain==2 && p->aOp[i].opcode!=OP_Explain ); - if( i>=nRow ){ - p->rc = SQLITE_OK; - rc = SQLITE_DONE; - }else if( db->u1.isInterrupted ){ - p->rc = SQLITE_INTERRUPT; - rc = SQLITE_ERROR; - sqlite3VdbeError(p, sqlite3ErrStr(p->rc)); }else{ - char *zP4; - Op *pOp; - if( i<p->nOp ){ - /* The output line number is small enough that we are still in the - ** main program. */ - pOp = &p->aOp[i]; - }else{ - /* We are currently listing subprograms. Figure out which one and - ** pick up the appropriate opcode. */ - int j; - i -= p->nOp; - for(j=0; i>=apSub[j]->nOp; j++){ - i -= apSub[j]->nOp; - } - pOp = &apSub[j]->aOp[i]; - } - if( p->explain==1 ){ - pMem->flags = MEM_Int; - pMem->u.i = i; /* Program counter */ - pMem++; - - pMem->flags = MEM_Static|MEM_Str|MEM_Term; - pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */ - assert( pMem->z!=0 ); - pMem->n = sqlite3Strlen30(pMem->z); - pMem->enc = SQLITE_UTF8; - pMem++; - - /* When an OP_Program opcode is encounter (the only opcode that has - ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms - ** kept in p->aMem[9].z to hold the new program - assuming this subprogram - ** has not already been seen. - */ - if( pOp->p4type==P4_SUBPROGRAM ){ - int nByte = (nSub+1)*sizeof(SubProgram*); - int j; - for(j=0; j<nSub; j++){ - if( apSub[j]==pOp->p4.pProgram ) break; - } - if( j==nSub && SQLITE_OK==sqlite3VdbeMemGrow(pSub, nByte, nSub!=0) ){ - apSub = (SubProgram **)pSub->z; - apSub[nSub++] = pOp->p4.pProgram; - pSub->flags |= MEM_Blob; - pSub->n = nSub*sizeof(SubProgram*); - } - } - } - - pMem->flags = MEM_Int; - pMem->u.i = pOp->p1; /* P1 */ - pMem++; - - pMem->flags = MEM_Int; - pMem->u.i = pOp->p2; /* P2 */ - pMem++; + pSub = 0; + } - pMem->flags = MEM_Int; - pMem->u.i = pOp->p3; /* P3 */ - pMem++; + /* Figure out which opcode is next to display */ + rc = sqlite3VdbeNextOpcode(p, pSub, p->explain==2, &p->pc, &i, &aOp); - if( sqlite3VdbeMemClearAndResize(pMem, 100) ){ /* P4 */ - assert( p->db->mallocFailed ); - return SQLITE_ERROR; - } - pMem->flags = MEM_Str|MEM_Term; - zP4 = displayP4(pOp, pMem->z, pMem->szMalloc); - if( zP4!=pMem->z ){ - sqlite3VdbeMemSetStr(pMem, zP4, -1, SQLITE_UTF8, 0); + if( rc==SQLITE_OK ){ + pOp = aOp + i; + if( AtomicLoad(&db->u1.isInterrupted) ){ + p->rc = SQLITE_INTERRUPT; + rc = SQLITE_ERROR; + sqlite3VdbeError(p, sqlite3ErrStr(p->rc)); }else{ - assert( pMem->z!=0 ); - pMem->n = sqlite3Strlen30(pMem->z); - pMem->enc = SQLITE_UTF8; - } - pMem++; - - if( p->explain==1 ){ - if( sqlite3VdbeMemClearAndResize(pMem, 4) ){ - assert( p->db->mallocFailed ); - return SQLITE_ERROR; - } - pMem->flags = MEM_Str|MEM_Term; - pMem->n = 2; - sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5); /* P5 */ - pMem->enc = SQLITE_UTF8; - pMem++; - + char *zP4 = sqlite3VdbeDisplayP4(db, pOp); + if( p->explain==2 ){ + sqlite3VdbeMemSetInt64(pMem, pOp->p1); + sqlite3VdbeMemSetInt64(pMem+1, pOp->p2); + sqlite3VdbeMemSetInt64(pMem+2, pOp->p3); + sqlite3VdbeMemSetStr(pMem+3, zP4, -1, SQLITE_UTF8, sqlite3_free); + assert( p->nResColumn==4 ); + }else{ + sqlite3VdbeMemSetInt64(pMem+0, i); + sqlite3VdbeMemSetStr(pMem+1, (char*)sqlite3OpcodeName(pOp->opcode), + -1, SQLITE_UTF8, SQLITE_STATIC); + sqlite3VdbeMemSetInt64(pMem+2, pOp->p1); + sqlite3VdbeMemSetInt64(pMem+3, pOp->p2); + sqlite3VdbeMemSetInt64(pMem+4, pOp->p3); + /* pMem+5 for p4 is done last */ + sqlite3VdbeMemSetInt64(pMem+6, pOp->p5); #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS - if( sqlite3VdbeMemClearAndResize(pMem, 500) ){ - assert( p->db->mallocFailed ); - return SQLITE_ERROR; - } - pMem->flags = MEM_Str|MEM_Term; - pMem->n = displayComment(pOp, zP4, pMem->z, 500); - pMem->enc = SQLITE_UTF8; + { + char *zCom = sqlite3VdbeDisplayComment(db, pOp, zP4); + sqlite3VdbeMemSetStr(pMem+7, zCom, -1, SQLITE_UTF8, sqlite3_free); + } #else - pMem->flags = MEM_Null; /* Comment */ + sqlite3VdbeMemSetNull(pMem+7); #endif + sqlite3VdbeMemSetStr(pMem+5, zP4, -1, SQLITE_UTF8, sqlite3_free); + assert( p->nResColumn==8 ); + } + p->pResultRow = pMem; + if( db->mallocFailed ){ + p->rc = SQLITE_NOMEM; + rc = SQLITE_ERROR; + }else{ + p->rc = SQLITE_OK; + rc = SQLITE_ROW; + } } - - p->nResColumn = 8 - 4*(p->explain-1); - p->pResultSet = &p->aMem[1]; - p->rc = SQLITE_OK; - rc = SQLITE_ROW; } return rc; } @@ -1736,9 +2544,9 @@ void sqlite3VdbeIOTraceSql(Vdbe *p){ ** of a ReusableSpace object by the allocSpace() routine below. */ struct ReusableSpace { - u8 *pSpace; /* Available memory */ - int nFree; /* Bytes of available memory */ - int nNeeded; /* Total bytes that could not be allocated */ + u8 *pSpace; /* Available memory */ + sqlite3_int64 nFree; /* Bytes of available memory */ + sqlite3_int64 nNeeded; /* Total bytes that could not be allocated */ }; /* Try to allocate nByte bytes of 8-byte aligned bulk memory for pBuf @@ -1758,11 +2566,11 @@ struct ReusableSpace { static void *allocSpace( struct ReusableSpace *p, /* Bulk memory available for allocation */ void *pBuf, /* Pointer to a prior allocation */ - int nByte /* Bytes of memory needed */ + sqlite3_int64 nByte /* Bytes of memory needed. */ ){ assert( EIGHT_BYTE_ALIGNMENT(p->pSpace) ); if( pBuf==0 ){ - nByte = ROUND8(nByte); + nByte = ROUND8P(nByte); if( nByte <= p->nFree ){ p->nFree -= nByte; pBuf = &p->pSpace[p->nFree]; @@ -1779,21 +2587,22 @@ static void *allocSpace( ** running it. */ void sqlite3VdbeRewind(Vdbe *p){ -#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) +#if defined(SQLITE_DEBUG) int i; #endif assert( p!=0 ); - assert( p->magic==VDBE_MAGIC_INIT ); + assert( p->eVdbeState==VDBE_INIT_STATE + || p->eVdbeState==VDBE_READY_STATE + || p->eVdbeState==VDBE_HALT_STATE ); /* There should be at least one opcode. */ assert( p->nOp>0 ); - /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. */ - p->magic = VDBE_MAGIC_RUN; + p->eVdbeState = VDBE_READY_STATE; #ifdef SQLITE_DEBUG - for(i=1; i<p->nMem; i++){ + for(i=0; i<p->nMem; i++){ assert( p->aMem[i].db==p->db ); } #endif @@ -1807,8 +2616,8 @@ void sqlite3VdbeRewind(Vdbe *p){ p->nFkConstraint = 0; #ifdef VDBE_PROFILE for(i=0; i<p->nOp; i++){ - p->aOp[i].cnt = 0; - p->aOp[i].cycles = 0; + p->aOp[i].nExec = 0; + p->aOp[i].nCycle = 0; } #endif } @@ -1818,11 +2627,11 @@ void sqlite3VdbeRewind(Vdbe *p){ ** creating the virtual machine. This involves things such ** as allocating registers and initializing the program counter. ** After the VDBE has be prepped, it can be executed by one or more -** calls to sqlite3VdbeExec(). +** calls to sqlite3VdbeExec(). ** ** This function may be called exactly once on each virtual machine. ** After this routine is called the VM has been "packaged" and is ready -** to run. After this routine is called, further calls to +** to run. After this routine is called, further calls to ** sqlite3VdbeAddOp() functions are prohibited. This routine disconnects ** the Vdbe from the Parse object that helped generate it so that the ** the Vdbe becomes an independent entity and the Parse object can be @@ -1839,130 +2648,129 @@ void sqlite3VdbeMakeReady( int nVar; /* Number of parameters */ int nMem; /* Number of VM memory registers */ int nCursor; /* Number of cursors required */ - int nArg; /* Number of arguments in subprograms */ - int nOnce; /* Number of OP_Once instructions */ + int nArg; /* Max number args to xFilter or xUpdate */ int n; /* Loop counter */ struct ReusableSpace x; /* Reusable bulk memory */ assert( p!=0 ); assert( p->nOp>0 ); assert( pParse!=0 ); - assert( p->magic==VDBE_MAGIC_INIT ); + assert( p->eVdbeState==VDBE_INIT_STATE ); assert( pParse==p->pParse ); + assert( pParse->db==p->db ); + p->pVList = pParse->pVList; + pParse->pVList = 0; db = p->db; assert( db->mallocFailed==0 ); nVar = pParse->nVar; nMem = pParse->nMem; nCursor = pParse->nTab; nArg = pParse->nMaxArg; - nOnce = pParse->nOnce; - if( nOnce==0 ) nOnce = 1; /* Ensure at least one byte in p->aOnceFlag[] */ - - /* For each cursor required, also allocate a memory cell. Memory - ** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by - ** the vdbe program. Instead they are used to allocate memory for - ** VdbeCursor/BtCursor structures. The blob of memory associated with - ** cursor 0 is stored in memory cell nMem. Memory cell (nMem-1) - ** stores the blob of memory associated with cursor 1, etc. - ** + + /* Each cursor uses a memory cell. The first cursor (cursor 0) can + ** use aMem[0] which is not otherwise used by the VDBE program. Allocate + ** space at the end of aMem[] for cursors 1 and greater. ** See also: allocateCursor(). */ nMem += nCursor; + if( nCursor==0 && nMem>0 ) nMem++; /* Space for aMem[0] even if not used */ /* Figure out how much reusable memory is available at the end of the ** opcode array. This extra memory will be reallocated for other elements ** of the prepared statement. */ - n = ROUND8(sizeof(Op)*p->nOp); /* Bytes of opcode memory used */ + n = ROUND8P(sizeof(Op)*p->nOp); /* Bytes of opcode memory used */ x.pSpace = &((u8*)p->aOp)[n]; /* Unused opcode memory */ assert( EIGHT_BYTE_ALIGNMENT(x.pSpace) ); x.nFree = ROUNDDOWN8(pParse->szOpAlloc - n); /* Bytes of unused memory */ assert( x.nFree>=0 ); - if( x.nFree>0 ){ - memset(x.pSpace, 0, x.nFree); - assert( EIGHT_BYTE_ALIGNMENT(&x.pSpace[x.nFree]) ); - } + assert( EIGHT_BYTE_ALIGNMENT(&x.pSpace[x.nFree]) ); resolveP2Values(p, &nArg); p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort); - if( pParse->explain && nMem<10 ){ - nMem = 10; + if( pParse->explain ){ + if( nMem<10 ) nMem = 10; + p->explain = pParse->explain; + p->nResColumn = 12 - 4*p->explain; } p->expired = 0; /* Memory for registers, parameters, cursor, etc, is allocated in one or two - ** passes. On the first pass, we try to reuse unused memory at the + ** passes. On the first pass, we try to reuse unused memory at the ** end of the opcode array. If we are unable to satisfy all memory ** requirements by reusing the opcode array tail, then the second - ** pass will fill in the remainder using a fresh memory allocation. + ** pass will fill in the remainder using a fresh memory allocation. ** ** This two-pass approach that reuses as much memory as possible from ** the leftover memory at the end of the opcode array. This can significantly ** reduce the amount of memory held by a prepared statement. */ - do { - x.nNeeded = 0; - p->aMem = allocSpace(&x, p->aMem, nMem*sizeof(Mem)); - p->aVar = allocSpace(&x, p->aVar, nVar*sizeof(Mem)); - p->apArg = allocSpace(&x, p->apArg, nArg*sizeof(Mem*)); - p->apCsr = allocSpace(&x, p->apCsr, nCursor*sizeof(VdbeCursor*)); - p->aOnceFlag = allocSpace(&x, p->aOnceFlag, nOnce); -#ifdef SQLITE_ENABLE_STMT_SCANSTATUS - p->anExec = allocSpace(&x, p->anExec, p->nOp*sizeof(i64)); -#endif - if( x.nNeeded==0 ) break; - x.pSpace = p->pFree = sqlite3DbMallocZero(db, x.nNeeded); + x.nNeeded = 0; + p->aMem = allocSpace(&x, 0, nMem*sizeof(Mem)); + p->aVar = allocSpace(&x, 0, nVar*sizeof(Mem)); + p->apArg = allocSpace(&x, 0, nArg*sizeof(Mem*)); + p->apCsr = allocSpace(&x, 0, nCursor*sizeof(VdbeCursor*)); + if( x.nNeeded ){ + x.pSpace = p->pFree = sqlite3DbMallocRawNN(db, x.nNeeded); x.nFree = x.nNeeded; - }while( !db->mallocFailed ); - - p->nCursor = nCursor; - p->nOnceFlag = nOnce; - if( p->aVar ){ - p->nVar = (ynVar)nVar; - for(n=0; n<nVar; n++){ - p->aVar[n].flags = MEM_Null; - p->aVar[n].db = db; + if( !db->mallocFailed ){ + p->aMem = allocSpace(&x, p->aMem, nMem*sizeof(Mem)); + p->aVar = allocSpace(&x, p->aVar, nVar*sizeof(Mem)); + p->apArg = allocSpace(&x, p->apArg, nArg*sizeof(Mem*)); + p->apCsr = allocSpace(&x, p->apCsr, nCursor*sizeof(VdbeCursor*)); } } - p->nzVar = pParse->nzVar; - p->azVar = pParse->azVar; - pParse->nzVar = 0; - pParse->azVar = 0; - if( p->aMem ){ - p->aMem--; /* aMem[] goes from 1..nMem */ - p->nMem = nMem; /* not from 0..nMem-1 */ - for(n=1; n<=nMem; n++){ - p->aMem[n].flags = MEM_Undefined; - p->aMem[n].db = db; - } +#ifdef SQLITE_DEBUG + p->napArg = nArg; +#endif + + if( db->mallocFailed ){ + p->nVar = 0; + p->nCursor = 0; + p->nMem = 0; + }else{ + p->nCursor = nCursor; + p->nVar = (ynVar)nVar; + initMemArray(p->aVar, nVar, db, MEM_Null); + p->nMem = nMem; + initMemArray(p->aMem, nMem, db, MEM_Undefined); + memset(p->apCsr, 0, nCursor*sizeof(VdbeCursor*)); } - p->explain = pParse->explain; sqlite3VdbeRewind(p); } /* -** Close a VDBE cursor and release all the resources that cursor +** Close a VDBE cursor and release all the resources that cursor ** happens to hold. */ void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){ - if( pCx==0 ){ + if( pCx ) sqlite3VdbeFreeCursorNN(p,pCx); +} +static SQLITE_NOINLINE void freeCursorWithCache(Vdbe *p, VdbeCursor *pCx){ + VdbeTxtBlbCache *pCache = pCx->pCache; + assert( pCx->colCache ); + pCx->colCache = 0; + pCx->pCache = 0; + if( pCache->pCValue ){ + sqlite3RCStrUnref(pCache->pCValue); + pCache->pCValue = 0; + } + sqlite3DbFree(p->db, pCache); + sqlite3VdbeFreeCursorNN(p, pCx); +} +void sqlite3VdbeFreeCursorNN(Vdbe *p, VdbeCursor *pCx){ + if( pCx->colCache ){ + freeCursorWithCache(p, pCx); return; } - assert( pCx->pBt==0 || pCx->eCurType==CURTYPE_BTREE ); switch( pCx->eCurType ){ case CURTYPE_SORTER: { sqlite3VdbeSorterClose(p->db, pCx); break; } case CURTYPE_BTREE: { - if( pCx->pBt ){ - sqlite3BtreeClose(pCx->pBt); - /* The pCx->pCursor will be close automatically, if it exists, by - ** the call above. */ - }else{ - assert( pCx->uc.pCursor!=0 ); - sqlite3BtreeCloseCursor(pCx->uc.pCursor); - } + assert( pCx->uc.pCursor!=0 ); + sqlite3BtreeCloseCursor(pCx->uc.pCursor); break; } #ifndef SQLITE_OMIT_VIRTUALTABLE @@ -1982,14 +2790,12 @@ void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){ ** Close all cursors in the current frame. */ static void closeCursorsInFrame(Vdbe *p){ - if( p->apCsr ){ - int i; - for(i=0; i<p->nCursor; i++){ - VdbeCursor *pC = p->apCsr[i]; - if( pC ){ - sqlite3VdbeFreeCursor(p, pC); - p->apCsr[i] = 0; - } + int i; + for(i=0; i<p->nCursor; i++){ + VdbeCursor *pC = p->apCsr[i]; + if( pC ){ + sqlite3VdbeFreeCursorNN(p, pC); + p->apCsr[i] = 0; } } } @@ -2002,11 +2808,6 @@ static void closeCursorsInFrame(Vdbe *p){ int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){ Vdbe *v = pFrame->v; closeCursorsInFrame(v); -#ifdef SQLITE_ENABLE_STMT_SCANSTATUS - v->anExec = pFrame->anExec; -#endif - v->aOnceFlag = pFrame->aOnceFlag; - v->nOnceFlag = pFrame->nOnceFlag; v->aOp = pFrame->aOp; v->nOp = pFrame->nOp; v->aMem = pFrame->aMem; @@ -2016,13 +2817,16 @@ int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){ v->db->lastRowid = pFrame->lastRowid; v->nChange = pFrame->nChange; v->db->nChange = pFrame->nDbChange; + sqlite3VdbeDeleteAuxData(v->db, &v->pAuxData, -1, 0); + v->pAuxData = pFrame->pAuxData; + pFrame->pAuxData = 0; return pFrame->pc; } /* ** Close all cursors. ** -** Also release any dynamic memory held by the VM in the Vdbe.aMem memory +** Also release any dynamic memory held by the VM in the Vdbe.aMem memory ** cell array. This is necessary as the memory cell array may contain ** pointers to VdbeFrame objects, which may in turn contain pointers to ** open cursors. @@ -2037,9 +2841,7 @@ static void closeAllCursors(Vdbe *p){ } assert( p->nFrame==0 ); closeCursorsInFrame(p); - if( p->aMem ){ - releaseMemArray(&p->aMem[1], p->nMem); - } + releaseMemArray(p->aMem, p->nMem); while( p->pDelFrame ){ VdbeFrame *pDel = p->pDelFrame; p->pDelFrame = pDel->pParent; @@ -2047,31 +2849,10 @@ static void closeAllCursors(Vdbe *p){ } /* Delete any auxdata allocations made by the VM */ - if( p->pAuxData ) sqlite3VdbeDeleteAuxData(p, -1, 0); + if( p->pAuxData ) sqlite3VdbeDeleteAuxData(p->db, &p->pAuxData, -1, 0); assert( p->pAuxData==0 ); } -/* -** Clean up the VM after a single run. -*/ -static void Cleanup(Vdbe *p){ - sqlite3 *db = p->db; - -#ifdef SQLITE_DEBUG - /* Execute assert() statements to ensure that the Vdbe.apCsr[] and - ** Vdbe.aMem[] arrays have already been cleaned up. */ - int i; - if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 ); - if( p->aMem ){ - for(i=1; i<=p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined ); - } -#endif - - sqlite3DbFree(db, p->zErrMsg); - p->zErrMsg = 0; - p->pResultSet = 0; -} - /* ** Set the number of result columns that will be returned by this SQL ** statement. This is now set at compile time, rather than during @@ -2079,21 +2860,18 @@ static void Cleanup(Vdbe *p){ ** be called on an SQL statement before sqlite3_step(). */ void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){ - Mem *pColName; int n; sqlite3 *db = p->db; - releaseMemArray(p->aColName, p->nResColumn*COLNAME_N); - sqlite3DbFree(db, p->aColName); + if( p->nResAlloc ){ + releaseMemArray(p->aColName, p->nResAlloc*COLNAME_N); + sqlite3DbFree(db, p->aColName); + } n = nResColumn*COLNAME_N; - p->nResColumn = (u16)nResColumn; - p->aColName = pColName = (Mem*)sqlite3DbMallocZero(db, sizeof(Mem)*n ); + p->nResColumn = p->nResAlloc = (u16)nResColumn; + p->aColName = (Mem*)sqlite3DbMallocRawNN(db, sizeof(Mem)*n ); if( p->aColName==0 ) return; - while( n-- > 0 ){ - pColName->flags = MEM_Null; - pColName->db = p->db; - pColName++; - } + initMemArray(p->aColName, n, db, MEM_Null); } /* @@ -2115,14 +2893,14 @@ int sqlite3VdbeSetColName( ){ int rc; Mem *pColName; - assert( idx<p->nResColumn ); + assert( idx<p->nResAlloc ); assert( var<COLNAME_N ); if( p->db->mallocFailed ){ assert( !zName || xDel!=SQLITE_DYNAMIC ); - return SQLITE_NOMEM; + return SQLITE_NOMEM_BKPT; } assert( p->aColName!=0 ); - pColName = &(p->aColName[idx+var*p->nResColumn]); + pColName = &(p->aColName[idx+var*p->nResAlloc]); rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel); assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 ); return rc; @@ -2132,42 +2910,63 @@ int sqlite3VdbeSetColName( ** A read or write transaction may or may not be active on database handle ** db. If a transaction is active, commit it. If there is a ** write-transaction spanning more than one database file, this routine -** takes care of the master journal trickery. +** takes care of the super-journal trickery. */ static int vdbeCommit(sqlite3 *db, Vdbe *p){ int i; - int nTrans = 0; /* Number of databases with an active write-transaction */ + int nTrans = 0; /* Number of databases with an active write-transaction + ** that are candidates for a two-phase commit using a + ** super-journal */ int rc = SQLITE_OK; int needXcommit = 0; #ifdef SQLITE_OMIT_VIRTUALTABLE - /* With this option, sqlite3VtabSync() is defined to be simply - ** SQLITE_OK so p is not used. + /* With this option, sqlite3VtabSync() is defined to be simply + ** SQLITE_OK so p is not used. */ UNUSED_PARAMETER(p); #endif /* Before doing anything else, call the xSync() callback for any ** virtual module tables written in this transaction. This has to - ** be done before determining whether a master journal file is + ** be done before determining whether a super-journal file is ** required, as an xSync() callback may add an attached database ** to the transaction. */ rc = sqlite3VtabSync(db, p); /* This loop determines (a) if the commit hook should be invoked and - ** (b) how many database files have open write transactions, not - ** including the temp database. (b) is important because if more than - ** one database file has an open write transaction, a master journal + ** (b) how many database files have open write transactions, not + ** including the temp database. (b) is important because if more than + ** one database file has an open write transaction, a super-journal ** file is required for an atomic commit. - */ - for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ + */ + for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ Btree *pBt = db->aDb[i].pBt; - if( sqlite3BtreeIsInTrans(pBt) ){ + if( sqlite3BtreeTxnState(pBt)==SQLITE_TXN_WRITE ){ + /* Whether or not a database might need a super-journal depends upon + ** its journal mode (among other things). This matrix determines which + ** journal modes use a super-journal and which do not */ + static const u8 aMJNeeded[] = { + /* DELETE */ 1, + /* PERSIST */ 1, + /* OFF */ 0, + /* TRUNCATE */ 1, + /* MEMORY */ 0, + /* WAL */ 0 + }; + Pager *pPager; /* Pager associated with pBt */ needXcommit = 1; - if( i!=1 ) nTrans++; sqlite3BtreeEnter(pBt); - rc = sqlite3PagerExclusiveLock(sqlite3BtreePager(pBt)); + pPager = sqlite3BtreePager(pBt); + if( db->aDb[i].safety_level!=PAGER_SYNCHRONOUS_OFF + && aMJNeeded[sqlite3PagerGetJournalMode(pPager)] + && sqlite3PagerIsMemdb(pPager)==0 + ){ + assert( i!=1 ); + nTrans++; + } + rc = sqlite3PagerExclusiveLock(pPager); sqlite3BtreeLeave(pBt); } } @@ -2185,31 +2984,35 @@ static int vdbeCommit(sqlite3 *db, Vdbe *p){ /* The simple case - no more than one database file (not counting the ** TEMP database) has a transaction active. There is no need for the - ** master-journal. + ** super-journal. ** ** If the return value of sqlite3BtreeGetFilename() is a zero length - ** string, it means the main database is :memory: or a temp file. In - ** that case we do not support atomic multi-file commits, so use the + ** string, it means the main database is :memory: or a temp file. In + ** that case we do not support atomic multi-file commits, so use the ** simple case then too. */ if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt)) || nTrans<=1 ){ - for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ - Btree *pBt = db->aDb[i].pBt; - if( pBt ){ - rc = sqlite3BtreeCommitPhaseOne(pBt, 0); + if( needXcommit ){ + for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ + Btree *pBt = db->aDb[i].pBt; + if( sqlite3BtreeTxnState(pBt)>=SQLITE_TXN_WRITE ){ + rc = sqlite3BtreeCommitPhaseOne(pBt, 0); + } } } - /* Do the commit only if all databases successfully complete phase 1. + /* Do the commit only if all databases successfully complete phase 1. ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an ** IO error while deleting or truncating a journal file. It is unlikely, ** but could happen. In this case abandon processing and return the error. */ for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ Btree *pBt = db->aDb[i].pBt; - if( pBt ){ + int txn = sqlite3BtreeTxnState(pBt); + if( txn!=SQLITE_TXN_NONE ){ + assert( needXcommit || txn==SQLITE_TXN_READ ); rc = sqlite3BtreeCommitPhaseTwo(pBt, 0); } } @@ -2219,129 +3022,125 @@ static int vdbeCommit(sqlite3 *db, Vdbe *p){ } /* The complex case - There is a multi-file write-transaction active. - ** This requires a master journal file to ensure the transaction is + ** This requires a super-journal file to ensure the transaction is ** committed atomically. */ #ifndef SQLITE_OMIT_DISKIO else{ sqlite3_vfs *pVfs = db->pVfs; - int needSync = 0; - char *zMaster = 0; /* File-name for the master journal */ + char *zSuper = 0; /* File-name for the super-journal */ char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt); - sqlite3_file *pMaster = 0; + sqlite3_file *pSuperJrnl = 0; i64 offset = 0; int res; int retryCount = 0; int nMainFile; - /* Select a master journal file name */ + /* Select a super-journal file name */ nMainFile = sqlite3Strlen30(zMainFile); - zMaster = sqlite3MPrintf(db, "%s-mjXXXXXX9XXz", zMainFile); - if( zMaster==0 ) return SQLITE_NOMEM; + zSuper = sqlite3MPrintf(db, "%.4c%s%.16c", 0,zMainFile,0); + if( zSuper==0 ) return SQLITE_NOMEM_BKPT; + zSuper += 4; do { u32 iRandom; if( retryCount ){ if( retryCount>100 ){ - sqlite3_log(SQLITE_FULL, "MJ delete: %s", zMaster); - sqlite3OsDelete(pVfs, zMaster, 0); + sqlite3_log(SQLITE_FULL, "MJ delete: %s", zSuper); + sqlite3OsDelete(pVfs, zSuper, 0); break; }else if( retryCount==1 ){ - sqlite3_log(SQLITE_FULL, "MJ collide: %s", zMaster); + sqlite3_log(SQLITE_FULL, "MJ collide: %s", zSuper); } } retryCount++; sqlite3_randomness(sizeof(iRandom), &iRandom); - sqlite3_snprintf(13, &zMaster[nMainFile], "-mj%06X9%02X", + sqlite3_snprintf(13, &zSuper[nMainFile], "-mj%06X9%02X", (iRandom>>8)&0xffffff, iRandom&0xff); - /* The antipenultimate character of the master journal name must + /* The antipenultimate character of the super-journal name must ** be "9" to avoid name collisions when using 8+3 filenames. */ - assert( zMaster[sqlite3Strlen30(zMaster)-3]=='9' ); - sqlite3FileSuffix3(zMainFile, zMaster); - rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res); + assert( zSuper[sqlite3Strlen30(zSuper)-3]=='9' ); + sqlite3FileSuffix3(zMainFile, zSuper); + rc = sqlite3OsAccess(pVfs, zSuper, SQLITE_ACCESS_EXISTS, &res); }while( rc==SQLITE_OK && res ); if( rc==SQLITE_OK ){ - /* Open the master journal. */ - rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster, + /* Open the super-journal. */ + rc = sqlite3OsOpenMalloc(pVfs, zSuper, &pSuperJrnl, SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE| - SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0 + SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_SUPER_JOURNAL, 0 ); } if( rc!=SQLITE_OK ){ - sqlite3DbFree(db, zMaster); + sqlite3DbFree(db, zSuper-4); return rc; } - + /* Write the name of each database file in the transaction into the new - ** master journal file. If an error occurs at this point close - ** and delete the master journal file. All the individual journal files - ** still have 'null' as the master journal pointer, so they will roll + ** super-journal file. If an error occurs at this point close + ** and delete the super-journal file. All the individual journal files + ** still have 'null' as the super-journal pointer, so they will roll ** back independently if a failure occurs. */ for(i=0; i<db->nDb; i++){ Btree *pBt = db->aDb[i].pBt; - if( sqlite3BtreeIsInTrans(pBt) ){ + if( sqlite3BtreeTxnState(pBt)==SQLITE_TXN_WRITE ){ char const *zFile = sqlite3BtreeGetJournalname(pBt); if( zFile==0 ){ continue; /* Ignore TEMP and :memory: databases */ } assert( zFile[0]!=0 ); - if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){ - needSync = 1; - } - rc = sqlite3OsWrite(pMaster, zFile, sqlite3Strlen30(zFile)+1, offset); + rc = sqlite3OsWrite(pSuperJrnl, zFile, sqlite3Strlen30(zFile)+1,offset); offset += sqlite3Strlen30(zFile)+1; if( rc!=SQLITE_OK ){ - sqlite3OsCloseFree(pMaster); - sqlite3OsDelete(pVfs, zMaster, 0); - sqlite3DbFree(db, zMaster); + sqlite3OsCloseFree(pSuperJrnl); + sqlite3OsDelete(pVfs, zSuper, 0); + sqlite3DbFree(db, zSuper-4); return rc; } } } - /* Sync the master journal file. If the IOCAP_SEQUENTIAL device + /* Sync the super-journal file. If the IOCAP_SEQUENTIAL device ** flag is set this is not required. */ - if( needSync - && 0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL) - && SQLITE_OK!=(rc = sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL)) + if( 0==(sqlite3OsDeviceCharacteristics(pSuperJrnl)&SQLITE_IOCAP_SEQUENTIAL) + && SQLITE_OK!=(rc = sqlite3OsSync(pSuperJrnl, SQLITE_SYNC_NORMAL)) ){ - sqlite3OsCloseFree(pMaster); - sqlite3OsDelete(pVfs, zMaster, 0); - sqlite3DbFree(db, zMaster); + sqlite3OsCloseFree(pSuperJrnl); + sqlite3OsDelete(pVfs, zSuper, 0); + sqlite3DbFree(db, zSuper-4); return rc; } /* Sync all the db files involved in the transaction. The same call - ** sets the master journal pointer in each individual journal. If - ** an error occurs here, do not delete the master journal file. + ** sets the super-journal pointer in each individual journal. If + ** an error occurs here, do not delete the super-journal file. ** ** If the error occurs during the first call to ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the - ** master journal file will be orphaned. But we cannot delete it, - ** in case the master journal file name was written into the journal + ** super-journal file will be orphaned. But we cannot delete it, + ** in case the super-journal file name was written into the journal ** file before the failure occurred. */ - for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ + for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ Btree *pBt = db->aDb[i].pBt; if( pBt ){ - rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster); + rc = sqlite3BtreeCommitPhaseOne(pBt, zSuper); } } - sqlite3OsCloseFree(pMaster); + sqlite3OsCloseFree(pSuperJrnl); assert( rc!=SQLITE_BUSY ); if( rc!=SQLITE_OK ){ - sqlite3DbFree(db, zMaster); + sqlite3DbFree(db, zSuper-4); return rc; } - /* Delete the master journal file. This commits the transaction. After + /* Delete the super-journal file. This commits the transaction. After ** doing this the directory is synced again before any individual ** transaction files are deleted. */ - rc = sqlite3OsDelete(pVfs, zMaster, needSync); - sqlite3DbFree(db, zMaster); - zMaster = 0; + rc = sqlite3OsDelete(pVfs, zSuper, 1); + sqlite3DbFree(db, zSuper-4); + zSuper = 0; if( rc ){ return rc; } @@ -2355,7 +3154,7 @@ static int vdbeCommit(sqlite3 *db, Vdbe *p){ */ disable_simulated_io_errors(); sqlite3BeginBenignMalloc(); - for(i=0; i<db->nDb; i++){ + for(i=0; i<db->nDb; i++){ Btree *pBt = db->aDb[i].pBt; if( pBt ){ sqlite3BtreeCommitPhaseTwo(pBt, 1); @@ -2371,7 +3170,7 @@ static int vdbeCommit(sqlite3 *db, Vdbe *p){ return rc; } -/* +/* ** This routine checks that the sqlite3.nVdbeActive count variable ** matches the number of vdbe's in the list sqlite3.pVdbe that are ** currently active. An assertion fails if the two counts do not match. @@ -2393,7 +3192,7 @@ static void checkActiveVdbeCnt(sqlite3 *db){ if( p->readOnly==0 ) nWrite++; if( p->bIsReader ) nRead++; } - p = p->pNext; + p = p->pVNext; } assert( cnt==db->nVdbeActive ); assert( nWrite==db->nVdbeWrite ); @@ -2407,89 +3206,92 @@ static void checkActiveVdbeCnt(sqlite3 *db){ ** If the Vdbe passed as the first argument opened a statement-transaction, ** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or ** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement -** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the +** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the ** statement transaction is committed. ** -** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned. +** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned. ** Otherwise SQLITE_OK. */ -int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){ +static SQLITE_NOINLINE int vdbeCloseStatement(Vdbe *p, int eOp){ sqlite3 *const db = p->db; int rc = SQLITE_OK; + int i; + const int iSavepoint = p->iStatement-1; - /* If p->iStatement is greater than zero, then this Vdbe opened a - ** statement transaction that should be closed here. The only exception - ** is that an IO error may have occurred, causing an emergency rollback. - ** In this case (db->nStatement==0), and there is nothing to do. - */ - if( db->nStatement && p->iStatement ){ - int i; - const int iSavepoint = p->iStatement-1; - - assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE); - assert( db->nStatement>0 ); - assert( p->iStatement==(db->nStatement+db->nSavepoint) ); - - for(i=0; i<db->nDb; i++){ - int rc2 = SQLITE_OK; - Btree *pBt = db->aDb[i].pBt; - if( pBt ){ - if( eOp==SAVEPOINT_ROLLBACK ){ - rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint); - } - if( rc2==SQLITE_OK ){ - rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint); - } - if( rc==SQLITE_OK ){ - rc = rc2; - } - } - } - db->nStatement--; - p->iStatement = 0; + assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE); + assert( db->nStatement>0 ); + assert( p->iStatement==(db->nStatement+db->nSavepoint) ); - if( rc==SQLITE_OK ){ + for(i=0; i<db->nDb; i++){ + int rc2 = SQLITE_OK; + Btree *pBt = db->aDb[i].pBt; + if( pBt ){ if( eOp==SAVEPOINT_ROLLBACK ){ - rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK, iSavepoint); + rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint); + } + if( rc2==SQLITE_OK ){ + rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint); } if( rc==SQLITE_OK ){ - rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE, iSavepoint); + rc = rc2; } } + } + db->nStatement--; + p->iStatement = 0; - /* If the statement transaction is being rolled back, also restore the - ** database handles deferred constraint counter to the value it had when - ** the statement transaction was opened. */ + if( rc==SQLITE_OK ){ if( eOp==SAVEPOINT_ROLLBACK ){ - db->nDeferredCons = p->nStmtDefCons; - db->nDeferredImmCons = p->nStmtDefImmCons; + rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK, iSavepoint); + } + if( rc==SQLITE_OK ){ + rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE, iSavepoint); } } + + /* If the statement transaction is being rolled back, also restore the + ** database handles deferred constraint counter to the value it had when + ** the statement transaction was opened. */ + if( eOp==SAVEPOINT_ROLLBACK ){ + db->nDeferredCons = p->nStmtDefCons; + db->nDeferredImmCons = p->nStmtDefImmCons; + } return rc; } +int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){ + if( p->db->nStatement && p->iStatement ){ + return vdbeCloseStatement(p, eOp); + } + return SQLITE_OK; +} + /* -** This function is called when a transaction opened by the database -** handle associated with the VM passed as an argument is about to be -** committed. If there are outstanding deferred foreign key constraint -** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK. +** These functions are called when a transaction opened by the database +** handle associated with the VM passed as an argument is about to be +** committed. If there are outstanding foreign key constraint violations +** return an error code. Otherwise, SQLITE_OK. ** -** If there are outstanding FK violations and this function returns -** SQLITE_ERROR, set the result of the VM to SQLITE_CONSTRAINT_FOREIGNKEY -** and write an error message to it. Then return SQLITE_ERROR. +** If there are outstanding FK violations and this function returns +** non-zero, set the result of the VM to SQLITE_CONSTRAINT_FOREIGNKEY +** and write an error message to it. */ #ifndef SQLITE_OMIT_FOREIGN_KEY -int sqlite3VdbeCheckFk(Vdbe *p, int deferred){ +static SQLITE_NOINLINE int vdbeFkError(Vdbe *p){ + p->rc = SQLITE_CONSTRAINT_FOREIGNKEY; + p->errorAction = OE_Abort; + sqlite3VdbeError(p, "FOREIGN KEY constraint failed"); + if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)==0 ) return SQLITE_ERROR; + return SQLITE_CONSTRAINT_FOREIGNKEY; +} +int sqlite3VdbeCheckFkImmediate(Vdbe *p){ + if( p->nFkConstraint==0 ) return SQLITE_OK; + return vdbeFkError(p); +} +int sqlite3VdbeCheckFkDeferred(Vdbe *p){ sqlite3 *db = p->db; - if( (deferred && (db->nDeferredCons+db->nDeferredImmCons)>0) - || (!deferred && p->nFkConstraint>0) - ){ - p->rc = SQLITE_CONSTRAINT_FOREIGNKEY; - p->errorAction = OE_Abort; - sqlite3VdbeError(p, "FOREIGN KEY constraint failed"); - return SQLITE_ERROR; - } - return SQLITE_OK; + if( (db->nDeferredCons+db->nDeferredImmCons)==0 ) return SQLITE_OK; + return vdbeFkError(p); } #endif @@ -2498,9 +3300,9 @@ int sqlite3VdbeCheckFk(Vdbe *p, int deferred){ ** has made changes and is in autocommit mode, then commit those ** changes. If a rollback is needed, then do the rollback. ** -** This routine is the only way to move the state of a VM from -** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT. It is harmless to -** call this on a VM that is in the SQLITE_MAGIC_HALT state. +** This routine is the only way to move the sqlite3eOpenState of a VM from +** SQLITE_STATE_RUN to SQLITE_STATE_HALT. It is harmless to +** call this on a VM that is in the SQLITE_STATE_HALT state. ** ** Return an error code. If the commit could not complete because of ** lock contention, return SQLITE_BUSY. If SQLITE_BUSY is returned, it @@ -2512,7 +3314,7 @@ int sqlite3VdbeHalt(Vdbe *p){ /* This function contains the logic that determines if a statement or ** transaction will be committed or rolled back as a result of the - ** execution of this virtual machine. + ** execution of this virtual machine. ** ** If any of the following errors occur: ** @@ -2526,19 +3328,16 @@ int sqlite3VdbeHalt(Vdbe *p){ ** one, or the complete transaction if there is no statement transaction. */ + assert( p->eVdbeState==VDBE_RUN_STATE ); if( db->mallocFailed ){ - p->rc = SQLITE_NOMEM; + p->rc = SQLITE_NOMEM_BKPT; } - if( p->aOnceFlag ) memset(p->aOnceFlag, 0, p->nOnceFlag); closeAllCursors(p); - if( p->magic!=VDBE_MAGIC_RUN ){ - return SQLITE_OK; - } checkActiveVdbeCnt(db); /* No commit or rollback needed if the program never started or if the ** SQL statement does not read or write a database file. */ - if( p->pc>=0 && p->bIsReader ){ + if( p->bIsReader ){ int mrc; /* Primary error code from p->rc */ int eStatementOp = 0; int isSpecialError; /* Set to true if a 'special' error */ @@ -2547,20 +3346,26 @@ int sqlite3VdbeHalt(Vdbe *p){ sqlite3VdbeEnter(p); /* Check for one of the special errors */ - mrc = p->rc & 0xff; - isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR - || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL; + if( p->rc ){ + mrc = p->rc & 0xff; + isSpecialError = mrc==SQLITE_NOMEM + || mrc==SQLITE_IOERR + || mrc==SQLITE_INTERRUPT + || mrc==SQLITE_FULL; + }else{ + mrc = isSpecialError = 0; + } if( isSpecialError ){ - /* If the query was read-only and the error code is SQLITE_INTERRUPT, - ** no rollback is necessary. Otherwise, at least a savepoint - ** transaction must be rolled back to restore the database to a + /* If the query was read-only and the error code is SQLITE_INTERRUPT, + ** no rollback is necessary. Otherwise, at least a savepoint + ** transaction must be rolled back to restore the database to a ** consistent state. ** ** Even if the statement is read-only, it is important to perform - ** a statement or transaction rollback operation. If the error + ** a statement or transaction rollback operation. If the error ** occurred while writing to the journal, sub-journal or database ** file as part of an effort to free up cache space (see function - ** pagerStress() in pager.c), the rollback is required to restore + ** pagerStress() in pager.c), the rollback is required to restore ** the pager to a consistent state. */ if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){ @@ -2579,32 +3384,35 @@ int sqlite3VdbeHalt(Vdbe *p){ } /* Check for immediate foreign key violations. */ - if( p->rc==SQLITE_OK ){ - sqlite3VdbeCheckFk(p, 0); + if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){ + (void)sqlite3VdbeCheckFkImmediate(p); } - - /* If the auto-commit flag is set and this is the only active writer - ** VM, then we do either a commit or rollback of the current transaction. + + /* If the auto-commit flag is set and this is the only active writer + ** VM, then we do either a commit or rollback of the current transaction. ** - ** Note: This block also runs if one of the special errors handled - ** above has occurred. + ** Note: This block also runs if one of the special errors handled + ** above has occurred. */ - if( !sqlite3VtabInSync(db) - && db->autoCommit - && db->nVdbeWrite==(p->readOnly==0) + if( !sqlite3VtabInSync(db) + && db->autoCommit + && db->nVdbeWrite==(p->readOnly==0) ){ if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){ - rc = sqlite3VdbeCheckFk(p, 1); + rc = sqlite3VdbeCheckFkDeferred(p); if( rc!=SQLITE_OK ){ if( NEVER(p->readOnly) ){ sqlite3VdbeLeave(p); return SQLITE_ERROR; } rc = SQLITE_CONSTRAINT_FOREIGNKEY; - }else{ - /* The auto-commit flag is true, the vdbe program was successful + }else if( db->flags & SQLITE_CorruptRdOnly ){ + rc = SQLITE_CORRUPT; + db->flags &= ~SQLITE_CorruptRdOnly; + }else{ + /* The auto-commit flag is true, the vdbe program was successful ** or hit an 'OR FAIL' constraint and there are no deferred foreign - ** key constraints to hold up the transaction. This means a commit + ** key constraints to hold up the transaction. This means a commit ** is required. */ rc = vdbeCommit(db, p); } @@ -2612,15 +3420,18 @@ int sqlite3VdbeHalt(Vdbe *p){ sqlite3VdbeLeave(p); return SQLITE_BUSY; }else if( rc!=SQLITE_OK ){ + sqlite3SystemError(db, rc); p->rc = rc; sqlite3RollbackAll(db, SQLITE_OK); p->nChange = 0; }else{ db->nDeferredCons = 0; db->nDeferredImmCons = 0; - db->flags &= ~SQLITE_DeferFKs; + db->flags &= ~(u64)SQLITE_DeferFKs; sqlite3CommitInternalChanges(db); } + }else if( p->rc==SQLITE_SCHEMA && db->nVdbeActive>1 ){ + p->nChange = 0; }else{ sqlite3RollbackAll(db, SQLITE_OK); p->nChange = 0; @@ -2638,7 +3449,7 @@ int sqlite3VdbeHalt(Vdbe *p){ p->nChange = 0; } } - + /* If eStatementOp is non-zero, then a statement transaction needs to ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to ** do so. If this operation returns an error, and the current statement @@ -2659,9 +3470,9 @@ int sqlite3VdbeHalt(Vdbe *p){ p->nChange = 0; } } - + /* If this was an INSERT, UPDATE or DELETE and no statement transaction - ** has been rolled back, update the database connection change-counter. + ** has been rolled back, update the database connection change-counter. */ if( p->changeCntOn ){ if( eStatementOp!=SAVEPOINT_ROLLBACK ){ @@ -2677,22 +3488,20 @@ int sqlite3VdbeHalt(Vdbe *p){ } /* We have successfully halted and closed the VM. Record this fact. */ - if( p->pc>=0 ){ - db->nVdbeActive--; - if( !p->readOnly ) db->nVdbeWrite--; - if( p->bIsReader ) db->nVdbeRead--; - assert( db->nVdbeActive>=db->nVdbeRead ); - assert( db->nVdbeRead>=db->nVdbeWrite ); - assert( db->nVdbeWrite>=0 ); - } - p->magic = VDBE_MAGIC_HALT; + db->nVdbeActive--; + if( !p->readOnly ) db->nVdbeWrite--; + if( p->bIsReader ) db->nVdbeRead--; + assert( db->nVdbeActive>=db->nVdbeRead ); + assert( db->nVdbeRead>=db->nVdbeWrite ); + assert( db->nVdbeWrite>=0 ); + p->eVdbeState = VDBE_HALT_STATE; checkActiveVdbeCnt(db); if( db->mallocFailed ){ - p->rc = SQLITE_NOMEM; + p->rc = SQLITE_NOMEM_BKPT; } /* If the auto-commit flag is set to true, then any locks that were held - ** by connection db have now been released. Call sqlite3ConnectionUnlocked() + ** by connection db have now been released. Call sqlite3ConnectionUnlocked() ** to invoke any required unlock-notify callbacks. */ if( db->autoCommit ){ @@ -2714,7 +3523,7 @@ void sqlite3VdbeResetStepResult(Vdbe *p){ /* ** Copy the error code and error message belonging to the VDBE passed -** as the first argument to its database handle (so that they will be +** as the first argument to its database handle (so that they will be ** returned by calls to sqlite3_errcode() and sqlite3_errmsg()). ** ** This function does not clear the VDBE error code or message, just @@ -2730,16 +3539,17 @@ int sqlite3VdbeTransferError(Vdbe *p){ sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT); sqlite3EndBenignMalloc(); db->bBenignMalloc--; - db->errCode = rc; - }else{ - sqlite3Error(db, rc); + }else if( db->pErr ){ + sqlite3ValueSetNull(db->pErr); } + db->errCode = rc; + db->errByteOffset = -1; return rc; } #ifdef SQLITE_ENABLE_SQLLOG /* -** If an SQLITE_CONFIG_SQLLOG hook is registered and the VM has been run, +** If an SQLITE_CONFIG_SQLLOG hook is registered and the VM has been run, ** invoke it. */ static void vdbeInvokeSqllog(Vdbe *v){ @@ -2766,10 +3576,14 @@ static void vdbeInvokeSqllog(Vdbe *v){ ** again. ** ** To look at it another way, this routine resets the state of the -** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to -** VDBE_MAGIC_INIT. +** virtual machine from VDBE_RUN_STATE or VDBE_HALT_STATE back to +** VDBE_READY_STATE. */ int sqlite3VdbeReset(Vdbe *p){ +#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) + int i; +#endif + sqlite3 *db; db = p->db; @@ -2777,32 +3591,40 @@ int sqlite3VdbeReset(Vdbe *p){ ** error, then it might not have been halted properly. So halt ** it now. */ - sqlite3VdbeHalt(p); + if( p->eVdbeState==VDBE_RUN_STATE ) sqlite3VdbeHalt(p); - /* If the VDBE has be run even partially, then transfer the error code + /* If the VDBE has been run even partially, then transfer the error code ** and error message from the VDBE into the main database structure. But ** if the VDBE has just been set to run but has not actually executed any ** instructions yet, leave the main database error information unchanged. */ if( p->pc>=0 ){ vdbeInvokeSqllog(p); - sqlite3VdbeTransferError(p); - sqlite3DbFree(db, p->zErrMsg); - p->zErrMsg = 0; - if( p->runOnlyOnce ) p->expired = 1; - }else if( p->rc && p->expired ){ - /* The expired flag was set on the VDBE before the first call - ** to sqlite3_step(). For consistency (since sqlite3_step() was - ** called), set the database error in this case as well. - */ - sqlite3ErrorWithMsg(db, p->rc, p->zErrMsg ? "%s" : 0, p->zErrMsg); - sqlite3DbFree(db, p->zErrMsg); - p->zErrMsg = 0; + if( db->pErr || p->zErrMsg ){ + sqlite3VdbeTransferError(p); + }else{ + db->errCode = p->rc; + } } - /* Reclaim all memory used by the VDBE + /* Reset register contents and reclaim error message memory. */ - Cleanup(p); +#ifdef SQLITE_DEBUG + /* Execute assert() statements to ensure that the Vdbe.apCsr[] and + ** Vdbe.aMem[] arrays have already been cleaned up. */ + if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 ); + if( p->aMem ){ + for(i=0; i<p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined ); + } +#endif + if( p->zErrMsg ){ + sqlite3DbFree(db, p->zErrMsg); + p->zErrMsg = 0; + } + p->pResultRow = 0; +#ifdef SQLITE_DEBUG + p->nWrite = 0; +#endif /* Save profiling information from this VDBE run. */ @@ -2810,7 +3632,6 @@ int sqlite3VdbeReset(Vdbe *p){ { FILE *out = fopen("vdbe_profile.out", "a"); if( out ){ - int i; fprintf(out, "---- "); for(i=0; i<p->nOp; i++){ fprintf(out, "%02x", p->aOp[i].opcode); @@ -2828,10 +3649,12 @@ int sqlite3VdbeReset(Vdbe *p){ } for(i=0; i<p->nOp; i++){ char zHdr[100]; + i64 cnt = p->aOp[i].nExec; + i64 cycles = p->aOp[i].nCycle; sqlite3_snprintf(sizeof(zHdr), zHdr, "%6u %12llu %8llu ", - p->aOp[i].cnt, - p->aOp[i].cycles, - p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0 + cnt, + cycles, + cnt>0 ? cycles/cnt : 0 ); fprintf(out, "%s", zHdr); sqlite3VdbePrintOp(out, i, &p->aOp[i]); @@ -2840,18 +3663,19 @@ int sqlite3VdbeReset(Vdbe *p){ } } #endif - p->iCurrentTime = 0; - p->magic = VDBE_MAGIC_INIT; return p->rc & db->errMask; } - + /* ** Clean up and delete a VDBE after execution. Return an integer which is ** the result code. Write any error message text into *pzErrMsg. */ int sqlite3VdbeFinalize(Vdbe *p){ int rc = SQLITE_OK; - if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){ + assert( VDBE_RUN_STATE>VDBE_READY_STATE ); + assert( VDBE_HALT_STATE>VDBE_READY_STATE ); + assert( VDBE_INIT_STATE<VDBE_READY_STATE ); + if( p->eVdbeState>=VDBE_READY_STATE ){ rc = sqlite3VdbeReset(p); assert( (rc & p->db->errMask)==rc ); } @@ -2865,8 +3689,8 @@ int sqlite3VdbeFinalize(Vdbe *p){ ** the first argument. ** ** Or, if iOp is greater than or equal to zero, then the destructor is -** only invoked for those auxiliary data pointers created by the user -** function invoked by the OP_Function opcode at instruction iOp of +** only invoked for those auxiliary data pointers created by the user +** function invoked by the OP_Function opcode at instruction iOp of ** VM pVdbe, and only then if: ** ** * the associated function parameter is the 32nd or later (counting @@ -2875,21 +3699,22 @@ int sqlite3VdbeFinalize(Vdbe *p){ ** * the corresponding bit in argument mask is clear (where the first ** function parameter corresponds to bit 0 etc.). */ -void sqlite3VdbeDeleteAuxData(Vdbe *pVdbe, int iOp, int mask){ - AuxData **pp = &pVdbe->pAuxData; +void sqlite3VdbeDeleteAuxData(sqlite3 *db, AuxData **pp, int iOp, int mask){ while( *pp ){ AuxData *pAux = *pp; if( (iOp<0) - || (pAux->iOp==iOp && (pAux->iArg>31 || !(mask & MASKBIT32(pAux->iArg)))) + || (pAux->iAuxOp==iOp + && pAux->iAuxArg>=0 + && (pAux->iAuxArg>31 || !(mask & MASKBIT32(pAux->iAuxArg)))) ){ - testcase( pAux->iArg==31 ); - if( pAux->xDelete ){ - pAux->xDelete(pAux->pAux); + testcase( pAux->iAuxArg==31 ); + if( pAux->xDeleteAux ){ + pAux->xDeleteAux(pAux->pAux); } - *pp = pAux->pNext; - sqlite3DbFree(pVdbe->db, pAux); + *pp = pAux->pNextAux; + sqlite3DbFree(db, pAux); }else{ - pp= &pAux->pNext; + pp= &pAux->pNextAux; } } } @@ -2902,28 +3727,44 @@ void sqlite3VdbeDeleteAuxData(Vdbe *pVdbe, int iOp, int mask){ ** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with ** the database connection and frees the object itself. */ -void sqlite3VdbeClearObject(sqlite3 *db, Vdbe *p){ +static void sqlite3VdbeClearObject(sqlite3 *db, Vdbe *p){ SubProgram *pSub, *pNext; - int i; + assert( db!=0 ); assert( p->db==0 || p->db==db ); - releaseMemArray(p->aVar, p->nVar); - releaseMemArray(p->aColName, p->nResColumn*COLNAME_N); + if( p->aColName ){ + releaseMemArray(p->aColName, p->nResAlloc*COLNAME_N); + sqlite3DbNNFreeNN(db, p->aColName); + } for(pSub=p->pProgram; pSub; pSub=pNext){ pNext = pSub->pNext; vdbeFreeOpArray(db, pSub->aOp, pSub->nOp); sqlite3DbFree(db, pSub); } - for(i=p->nzVar-1; i>=0; i--) sqlite3DbFree(db, p->azVar[i]); - sqlite3DbFree(db, p->azVar); + if( p->eVdbeState!=VDBE_INIT_STATE ){ + releaseMemArray(p->aVar, p->nVar); + if( p->pVList ) sqlite3DbNNFreeNN(db, p->pVList); + if( p->pFree ) sqlite3DbNNFreeNN(db, p->pFree); + } vdbeFreeOpArray(db, p->aOp, p->nOp); - sqlite3DbFree(db, p->aColName); - sqlite3DbFree(db, p->zSql); - sqlite3DbFree(db, p->pFree); + if( p->zSql ) sqlite3DbNNFreeNN(db, p->zSql); +#ifdef SQLITE_ENABLE_NORMALIZE + sqlite3DbFree(db, p->zNormSql); + { + DblquoteStr *pThis, *pNxt; + for(pThis=p->pDblStr; pThis; pThis=pNxt){ + pNxt = pThis->pNextStr; + sqlite3DbFree(db, pThis); + } + } +#endif #ifdef SQLITE_ENABLE_STMT_SCANSTATUS - for(i=0; i<p->nScan; i++){ - sqlite3DbFree(db, p->aScan[i].zName); + { + int i; + for(i=0; i<p->nScan; i++){ + sqlite3DbFree(db, p->aScan[i].zName); + } + sqlite3DbFree(db, p->aScan); } - sqlite3DbFree(db, p->aScan); #endif } @@ -2933,22 +3774,19 @@ void sqlite3VdbeClearObject(sqlite3 *db, Vdbe *p){ void sqlite3VdbeDelete(Vdbe *p){ sqlite3 *db; - if( NEVER(p==0) ) return; + assert( p!=0 ); db = p->db; + assert( db!=0 ); assert( sqlite3_mutex_held(db->mutex) ); sqlite3VdbeClearObject(db, p); - if( p->pPrev ){ - p->pPrev->pNext = p->pNext; - }else{ - assert( db->pVdbe==p ); - db->pVdbe = p->pNext; - } - if( p->pNext ){ - p->pNext->pPrev = p->pPrev; + if( db->pnBytesFreed==0 ){ + assert( p->ppVPrev!=0 ); + *p->ppVPrev = p->pVNext; + if( p->pVNext ){ + p->pVNext->ppVPrev = p->ppVPrev; + } } - p->magic = VDBE_MAGIC_DEAD; - p->db = 0; - sqlite3DbFree(db, p); + sqlite3DbNNFreeNN(db, p); } /* @@ -2956,7 +3794,7 @@ void sqlite3VdbeDelete(Vdbe *p){ ** carried out. Seek the cursor now. If an error occurs, return ** the appropriate error code. */ -static int SQLITE_NOINLINE handleDeferredMoveto(VdbeCursor *p){ +int SQLITE_NOINLINE sqlite3VdbeFinishMoveto(VdbeCursor *p){ int res, rc; #ifdef SQLITE_TEST extern int sqlite3_search_count; @@ -2964,7 +3802,7 @@ static int SQLITE_NOINLINE handleDeferredMoveto(VdbeCursor *p){ assert( p->deferredMoveto ); assert( p->isTable ); assert( p->eCurType==CURTYPE_BTREE ); - rc = sqlite3BtreeMovetoUnpacked(p->uc.pCursor, 0, p->movetoTarget, 0, &res); + rc = sqlite3BtreeTableMoveto(p->uc.pCursor, p->movetoTarget, 0, &res); if( rc ) return rc; if( res!=0 ) return SQLITE_CORRUPT_BKPT; #ifdef SQLITE_TEST @@ -2982,7 +3820,7 @@ static int SQLITE_NOINLINE handleDeferredMoveto(VdbeCursor *p){ ** is supposed to be pointing. If the row was deleted out from under the ** cursor, set the cursor to point to a NULL row. */ -static int SQLITE_NOINLINE handleMovedCursor(VdbeCursor *p){ +int SQLITE_NOINLINE sqlite3VdbeHandleMovedCursor(VdbeCursor *p){ int isDifferentRow, rc; assert( p->eCurType==CURTYPE_BTREE ); assert( p->uc.pCursor!=0 ); @@ -2998,41 +3836,9 @@ static int SQLITE_NOINLINE handleMovedCursor(VdbeCursor *p){ ** if need be. Return any I/O error from the restore operation. */ int sqlite3VdbeCursorRestore(VdbeCursor *p){ - assert( p->eCurType==CURTYPE_BTREE ); + assert( p->eCurType==CURTYPE_BTREE || IsNullCursor(p) ); if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){ - return handleMovedCursor(p); - } - return SQLITE_OK; -} - -/* -** Make sure the cursor p is ready to read or write the row to which it -** was last positioned. Return an error code if an OOM fault or I/O error -** prevents us from positioning the cursor to its correct position. -** -** If a MoveTo operation is pending on the given cursor, then do that -** MoveTo now. If no move is pending, check to see if the row has been -** deleted out from under the cursor and if it has, mark the row as -** a NULL row. -** -** If the cursor is already pointing to the correct row and that row has -** not been deleted out from under the cursor, then this routine is a no-op. -*/ -int sqlite3VdbeCursorMoveto(VdbeCursor **pp, int *piCol){ - VdbeCursor *p = *pp; - if( p->eCurType==CURTYPE_BTREE ){ - if( p->deferredMoveto ){ - int iMap; - if( p->aAltMap && (iMap = p->aAltMap[1+*piCol])>0 ){ - *pp = p->pAltCursor; - *piCol = iMap - 1; - return SQLITE_OK; - } - return handleDeferredMoveto(p); - } - if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){ - return handleMovedCursor(p); - } + return sqlite3VdbeHandleMovedCursor(p); } return SQLITE_OK; } @@ -3043,7 +3849,7 @@ int sqlite3VdbeCursorMoveto(VdbeCursor **pp, int *piCol){ ** sqlite3VdbeSerialType() ** sqlite3VdbeSerialTypeLen() ** sqlite3VdbeSerialLen() -** sqlite3VdbeSerialPut() +** sqlite3VdbeSerialPut() <--- in-lined into OP_MakeRecord as of 2022-04-02 ** sqlite3VdbeSerialGet() ** ** encapsulate the code that serializes values for storage in SQLite @@ -3079,8 +3885,17 @@ int sqlite3VdbeCursorMoveto(VdbeCursor **pp, int *piCol){ ** of SQLite will not understand those serial types. */ +#if 0 /* Inlined into the OP_MakeRecord opcode */ /* ** Return the serial-type for the value stored in pMem. +** +** This routine might convert a large MEM_IntReal value into MEM_Real. +** +** 2019-07-11: The primary user of this subroutine was the OP_MakeRecord +** opcode in the byte-code engine. But by moving this routine in-line, we +** can omit some redundant tests and make that opcode a lot faster. So +** this routine is now only used by the STAT3 logic and STAT3 support has +** ended. The code is kept here for historical reference only. */ u32 sqlite3VdbeSerialType(Mem *pMem, int file_format, u32 *pLen){ int flags = pMem->flags; @@ -3091,11 +3906,13 @@ u32 sqlite3VdbeSerialType(Mem *pMem, int file_format, u32 *pLen){ *pLen = 0; return 0; } - if( flags&MEM_Int ){ + if( flags&(MEM_Int|MEM_IntReal) ){ /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */ # define MAX_6BYTE ((((i64)0x00008000)<<32)-1) i64 i = pMem->u.i; u64 u; + testcase( flags & MEM_Int ); + testcase( flags & MEM_IntReal ); if( i<0 ){ u = ~i; }else{ @@ -3115,6 +3932,15 @@ u32 sqlite3VdbeSerialType(Mem *pMem, int file_format, u32 *pLen){ if( u<=2147483647 ){ *pLen = 4; return 4; } if( u<=MAX_6BYTE ){ *pLen = 6; return 5; } *pLen = 8; + if( flags&MEM_IntReal ){ + /* If the value is IntReal and is going to take up 8 bytes to store + ** as an integer, then we might as well make it an 8-byte floating + ** point value */ + pMem->u.r = (double)pMem->u.i; + pMem->flags &= ~MEM_IntReal; + pMem->flags |= MEM_Real; + return 7; + } return 6; } if( flags&MEM_Real ){ @@ -3130,12 +3956,13 @@ u32 sqlite3VdbeSerialType(Mem *pMem, int file_format, u32 *pLen){ *pLen = n; return ((n*2) + 12 + ((flags&MEM_Str)!=0)); } +#endif /* inlined into OP_MakeRecord */ /* ** The sizes for serial types less than 128 */ -static const u8 sqlite3SmallTypeSizes[] = { - /* 0 1 2 3 4 5 6 7 8 9 */ +const u8 sqlite3SmallTypeSizes[128] = { + /* 0 1 2 3 4 5 6 7 8 9 */ /* 0 */ 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, /* 10 */ 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, /* 20 */ 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, @@ -3158,19 +3985,19 @@ u32 sqlite3VdbeSerialTypeLen(u32 serial_type){ if( serial_type>=128 ){ return (serial_type-12)/2; }else{ - assert( serial_type<12 + assert( serial_type<12 || sqlite3SmallTypeSizes[serial_type]==(serial_type - 12)/2 ); return sqlite3SmallTypeSizes[serial_type]; } } u8 sqlite3VdbeOneByteSerialTypeLen(u8 serial_type){ assert( serial_type<128 ); - return sqlite3SmallTypeSizes[serial_type]; + return sqlite3SmallTypeSizes[serial_type]; } /* -** If we are on an architecture with mixed-endian floating -** points (ex: ARM7) then swap the lower 4 bytes with the +** If we are on an architecture with mixed-endian floating +** points (ex: ARM7) then swap the lower 4 bytes with the ** upper 4 bytes. Return the result. ** ** For most architectures, this is a no-op. @@ -3192,7 +4019,7 @@ u8 sqlite3VdbeOneByteSerialTypeLen(u8 serial_type){ ** (2007-08-30) Frank van Vugt has studied this problem closely ** and has send his findings to the SQLite developers. Frank ** writes that some Linux kernels offer floating point hardware -** emulation that uses only 32-bit mantissas instead of a full +** emulation that uses only 32-bit mantissas instead of a full ** 48-bits as required by the IEEE standard. (This is the ** CONFIG_FPE_FASTFPE option.) On such systems, floating point ** byte swapping becomes very complicated. To avoid problems, @@ -3203,7 +4030,7 @@ u8 sqlite3VdbeOneByteSerialTypeLen(u8 serial_type){ ** so we trust him. */ #ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT -static u64 floatSwap(u64 in){ +u64 sqlite3FloatSwap(u64 in){ union { u64 r; u32 i[2]; @@ -3216,59 +4043,8 @@ static u64 floatSwap(u64 in){ u.i[1] = t; return u.r; } -# define swapMixedEndianFloat(X) X = floatSwap(X) -#else -# define swapMixedEndianFloat(X) -#endif - -/* -** Write the serialized data blob for the value stored in pMem into -** buf. It is assumed that the caller has allocated sufficient space. -** Return the number of bytes written. -** -** nBuf is the amount of space left in buf[]. The caller is responsible -** for allocating enough space to buf[] to hold the entire field, exclusive -** of the pMem->u.nZero bytes for a MEM_Zero value. -** -** Return the number of bytes actually written into buf[]. The number -** of bytes in the zero-filled tail is included in the return value only -** if those bytes were zeroed in buf[]. -*/ -u32 sqlite3VdbeSerialPut(u8 *buf, Mem *pMem, u32 serial_type){ - u32 len; - - /* Integer and Real */ - if( serial_type<=7 && serial_type>0 ){ - u64 v; - u32 i; - if( serial_type==7 ){ - assert( sizeof(v)==sizeof(pMem->u.r) ); - memcpy(&v, &pMem->u.r, sizeof(v)); - swapMixedEndianFloat(v); - }else{ - v = pMem->u.i; - } - len = i = sqlite3SmallTypeSizes[serial_type]; - assert( i>0 ); - do{ - buf[--i] = (u8)(v&0xFF); - v >>= 8; - }while( i ); - return len; - } - - /* String or blob */ - if( serial_type>=12 ){ - assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0) - == (int)sqlite3VdbeSerialTypeLen(serial_type) ); - len = pMem->n; - if( len>0 ) memcpy(buf, pMem->z, len); - return len; - } +#endif /* SQLITE_MIXED_ENDIAN_64BIT_FLOAT */ - /* NULL or constants 0 or 1 */ - return 0; -} /* Input "x" is a sequence of unsigned characters that represent a ** big-endian integer. Return the equivalent native integer @@ -3281,14 +4057,14 @@ u32 sqlite3VdbeSerialPut(u8 *buf, Mem *pMem, u32 serial_type){ /* ** Deserialize the data blob pointed to by buf as serial type serial_type -** and store the result in pMem. Return the number of bytes read. +** and store the result in pMem. ** ** This function is implemented as two separate routines for performance. ** The few cases that require local variables are broken out into a separate ** routine so that in most cases the overhead of moving the stack pointer ** is avoided. -*/ -static u32 SQLITE_NOINLINE serialGet( +*/ +static void serialGet( const unsigned char *buf, /* Buffer to deserialize from */ u32 serial_type, /* Serial type to deserialize */ Mem *pMem /* Memory cell to write value into */ @@ -3320,22 +4096,44 @@ static u32 SQLITE_NOINLINE serialGet( assert( sizeof(x)==8 && sizeof(pMem->u.r)==8 ); swapMixedEndianFloat(x); memcpy(&pMem->u.r, &x, sizeof(x)); - pMem->flags = sqlite3IsNaN(pMem->u.r) ? MEM_Null : MEM_Real; + pMem->flags = IsNaN(x) ? MEM_Null : MEM_Real; } - return 8; } -u32 sqlite3VdbeSerialGet( +static int serialGet7( + const unsigned char *buf, /* Buffer to deserialize from */ + Mem *pMem /* Memory cell to write value into */ +){ + u64 x = FOUR_BYTE_UINT(buf); + u32 y = FOUR_BYTE_UINT(buf+4); + x = (x<<32) + y; + assert( sizeof(x)==8 && sizeof(pMem->u.r)==8 ); + swapMixedEndianFloat(x); + memcpy(&pMem->u.r, &x, sizeof(x)); + if( IsNaN(x) ){ + pMem->flags = MEM_Null; + return 1; + } + pMem->flags = MEM_Real; + return 0; +} +void sqlite3VdbeSerialGet( const unsigned char *buf, /* Buffer to deserialize from */ u32 serial_type, /* Serial type to deserialize */ Mem *pMem /* Memory cell to write value into */ ){ switch( serial_type ){ - case 10: /* Reserved for future use */ + case 10: { /* Internal use only: NULL with virtual table + ** UPDATE no-change flag set */ + pMem->flags = MEM_Null|MEM_Zero; + pMem->n = 0; + pMem->u.nZero = 0; + return; + } case 11: /* Reserved for future use */ case 0: { /* Null */ /* EVIDENCE-OF: R-24078-09375 Value is a NULL. */ pMem->flags = MEM_Null; - break; + return; } case 1: { /* EVIDENCE-OF: R-44885-25196 Value is an 8-bit twos-complement @@ -3343,7 +4141,7 @@ u32 sqlite3VdbeSerialGet( pMem->u.i = ONE_BYTE_INT(buf); pMem->flags = MEM_Int; testcase( pMem->u.i<0 ); - return 1; + return; } case 2: { /* 2-byte signed integer */ /* EVIDENCE-OF: R-49794-35026 Value is a big-endian 16-bit @@ -3351,7 +4149,7 @@ u32 sqlite3VdbeSerialGet( pMem->u.i = TWO_BYTE_INT(buf); pMem->flags = MEM_Int; testcase( pMem->u.i<0 ); - return 2; + return; } case 3: { /* 3-byte signed integer */ /* EVIDENCE-OF: R-37839-54301 Value is a big-endian 24-bit @@ -3359,19 +4157,19 @@ u32 sqlite3VdbeSerialGet( pMem->u.i = THREE_BYTE_INT(buf); pMem->flags = MEM_Int; testcase( pMem->u.i<0 ); - return 3; + return; } case 4: { /* 4-byte signed integer */ /* EVIDENCE-OF: R-01849-26079 Value is a big-endian 32-bit ** twos-complement integer. */ pMem->u.i = FOUR_BYTE_INT(buf); -#ifdef __HP_cc +#ifdef __HP_cc /* Work around a sign-extension bug in the HP compiler for HP/UX */ if( buf[0]&0x80 ) pMem->u.i |= 0xffffffff80000000LL; #endif pMem->flags = MEM_Int; testcase( pMem->u.i<0 ); - return 4; + return; } case 5: { /* 6-byte signed integer */ /* EVIDENCE-OF: R-50385-09674 Value is a big-endian 48-bit @@ -3379,13 +4177,14 @@ u32 sqlite3VdbeSerialGet( pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf); pMem->flags = MEM_Int; testcase( pMem->u.i<0 ); - return 6; + return; } case 6: /* 8-byte signed integer */ case 7: { /* IEEE floating point */ /* These use local variables, so do them in a separate routine ** to avoid having to move the frame pointer in the common case */ - return serialGet(buf,serial_type,pMem); + serialGet(buf,serial_type,pMem); + return; } case 8: /* Integer 0 */ case 9: { /* Integer 1 */ @@ -3393,7 +4192,7 @@ u32 sqlite3VdbeSerialGet( /* EVIDENCE-OF: R-18143-12121 Value is the integer 1. */ pMem->u.i = serial_type-8; pMem->flags = MEM_Int; - return 0; + return; } default: { /* EVIDENCE-OF: R-14606-31564 Value is a BLOB that is (N-12)/2 bytes in @@ -3404,81 +4203,57 @@ u32 sqlite3VdbeSerialGet( pMem->z = (char *)buf; pMem->n = (serial_type-12)/2; pMem->flags = aFlag[serial_type&1]; - return pMem->n; + return; } } - return 0; + return; } /* -** This routine is used to allocate sufficient space for an UnpackedRecord -** structure large enough to be used with sqlite3VdbeRecordUnpack() if -** the first argument is a pointer to KeyInfo structure pKeyInfo. +** Allocate sufficient space for an UnpackedRecord structure large enough +** to hold a decoded index record for pKeyInfo. ** -** The space is either allocated using sqlite3DbMallocRaw() or from within -** the unaligned buffer passed via the second and third arguments (presumably -** stack space). If the former, then *ppFree is set to a pointer that should -** be eventually freed by the caller using sqlite3DbFree(). Or, if the -** allocation comes from the pSpace/szSpace buffer, *ppFree is set to NULL -** before returning. -** -** If an OOM error occurs, NULL is returned. +** The space is allocated using sqlite3DbMallocRaw(). If an OOM error +** occurs, NULL is returned. */ UnpackedRecord *sqlite3VdbeAllocUnpackedRecord( - KeyInfo *pKeyInfo, /* Description of the record */ - char *pSpace, /* Unaligned space available */ - int szSpace, /* Size of pSpace[] in bytes */ - char **ppFree /* OUT: Caller should free this pointer */ + KeyInfo *pKeyInfo /* Description of the record */ ){ UnpackedRecord *p; /* Unpacked record to return */ - int nOff; /* Increment pSpace by nOff to align it */ - int nByte; /* Number of bytes required for *p */ - - /* We want to shift the pointer pSpace up such that it is 8-byte aligned. - ** Thus, we need to calculate a value, nOff, between 0 and 7, to shift - ** it by. If pSpace is already 8-byte aligned, nOff should be zero. - */ - nOff = (8 - (SQLITE_PTR_TO_INT(pSpace) & 7)) & 7; - nByte = ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nField+1); - if( nByte>szSpace+nOff ){ - p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte); - *ppFree = (char *)p; - if( !p ) return 0; - }else{ - p = (UnpackedRecord*)&pSpace[nOff]; - *ppFree = 0; - } - - p->aMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))]; - assert( pKeyInfo->aSortOrder!=0 ); + u64 nByte; /* Number of bytes required for *p */ + assert( sizeof(UnpackedRecord) + sizeof(Mem)*65536 < 0x7fffffff ); + nByte = ROUND8P(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nKeyField+1); + p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte); + if( !p ) return 0; + p->aMem = (Mem*)&((char*)p)[ROUND8P(sizeof(UnpackedRecord))]; p->pKeyInfo = pKeyInfo; - p->nField = pKeyInfo->nField + 1; + p->nField = pKeyInfo->nKeyField + 1; return p; } /* -** Given the nKey-byte encoding of a record in pKey[], populate the +** Given the nKey-byte encoding of a record in pKey[], populate the ** UnpackedRecord structure indicated by the fourth argument with the ** contents of the decoded record. -*/ +*/ void sqlite3VdbeRecordUnpack( - KeyInfo *pKeyInfo, /* Information about the record format */ int nKey, /* Size of the binary record */ const void *pKey, /* The binary record */ UnpackedRecord *p /* Populate this structure before returning. */ ){ const unsigned char *aKey = (const unsigned char *)pKey; - int d; + u32 d; u32 idx; /* Offset in aKey[] to read from */ u16 u; /* Unsigned loop counter */ u32 szHdr; Mem *pMem = p->aMem; + KeyInfo *pKeyInfo = p->pKeyInfo; p->default_rc = 0; assert( EIGHT_BYTE_ALIGNMENT(pMem) ); idx = getVarint32(aKey, szHdr); d = szHdr; u = 0; - while( idx<szHdr && d<=nKey ){ + while( idx<szHdr && d<=(u32)nKey ){ u32 serial_type; idx += getVarint32(&aKey[idx], serial_type); @@ -3486,15 +4261,26 @@ void sqlite3VdbeRecordUnpack( pMem->db = pKeyInfo->db; /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */ pMem->szMalloc = 0; - d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem); - pMem++; + pMem->z = 0; + sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem); + d += sqlite3VdbeSerialTypeLen(serial_type); if( (++u)>=p->nField ) break; + pMem++; + } + if( d>(u32)nKey && u ){ + assert( CORRUPT_DB ); + /* In a corrupt record entry, the last pMem might have been set up using + ** uninitialized memory. Overwrite its value with NULL, to prevent + ** warnings from MSAN. */ + sqlite3VdbeMemSetNull(pMem-(u<p->nField)); } - assert( u<=pKeyInfo->nField + 1 ); + testcase( u == pKeyInfo->nKeyField + 1 ); + testcase( u < pKeyInfo->nKeyField + 1 ); + assert( u<=pKeyInfo->nKeyField + 1 ); p->nField = u; } -#if SQLITE_DEBUG +#ifdef SQLITE_DEBUG /* ** This function compares two index or table record keys in the same way ** as the sqlite3VdbeRecordCompare() routine. Unlike VdbeRecordCompare(), @@ -3529,19 +4315,19 @@ static int vdbeRecordCompareDebug( /* Compilers may complain that mem1.u.i is potentially uninitialized. ** We could initialize it, as shown here, to silence those complaints. - ** But in fact, mem1.u.i will never actually be used uninitialized, and doing + ** But in fact, mem1.u.i will never actually be used uninitialized, and doing ** the unnecessary initialization has a measurable negative performance ** impact, since this routine is a very high runner. And so, we choose ** to ignore the compiler warnings and leave this variable uninitialized. */ /* mem1.u.i = 0; // not needed, here to silence compiler warning */ - + idx1 = getVarint32(aKey1, szHdr1); if( szHdr1>98307 ) return SQLITE_CORRUPT; d1 = szHdr1; - assert( pKeyInfo->nField+pKeyInfo->nXField>=pPKey2->nField || CORRUPT_DB ); - assert( pKeyInfo->aSortOrder!=0 ); - assert( pKeyInfo->nField>0 ); + assert( pKeyInfo->nAllField>=pPKey2->nField || CORRUPT_DB ); + assert( pKeyInfo->aSortFlags!=0 ); + assert( pKeyInfo->nKeyField>0 ); assert( idx1<=szHdr1 || CORRUPT_DB ); do{ u32 serial_type1; @@ -3555,22 +4341,38 @@ static int vdbeRecordCompareDebug( ** Use that approximation to avoid the more expensive call to ** sqlite3VdbeSerialTypeLen() in the common case. */ - if( d1+serial_type1+2>(u32)nKey1 - && d1+sqlite3VdbeSerialTypeLen(serial_type1)>(u32)nKey1 + if( d1+(u64)serial_type1+2>(u64)nKey1 + && d1+(u64)sqlite3VdbeSerialTypeLen(serial_type1)>(u64)nKey1 ){ + if( serial_type1>=1 + && serial_type1<=7 + && d1+(u64)sqlite3VdbeSerialTypeLen(serial_type1)<=(u64)nKey1+8 + && CORRUPT_DB + ){ + return 1; /* corrupt record not detected by + ** sqlite3VdbeRecordCompareWithSkip(). Return true + ** to avoid firing the assert() */ + } break; } /* Extract the values to be compared. */ - d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1); + sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1); + d1 += sqlite3VdbeSerialTypeLen(serial_type1); /* Do the comparison */ - rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i], pKeyInfo->aColl[i]); + rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i], + pKeyInfo->nAllField>i ? pKeyInfo->aColl[i] : 0); if( rc!=0 ){ assert( mem1.szMalloc==0 ); /* See comment below */ - if( pKeyInfo->aSortOrder[i] ){ + if( (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_BIGNULL) + && ((mem1.flags & MEM_Null) || (pPKey2->aMem[i].flags & MEM_Null)) + ){ + rc = -rc; + } + if( pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_DESC ){ rc = -rc; /* Invert the result for DESC sort order. */ } goto debugCompareEnd; @@ -3599,20 +4401,20 @@ static int vdbeRecordCompareDebug( } #endif -#if SQLITE_DEBUG +#ifdef SQLITE_DEBUG /* ** Count the number of fields (a.k.a. columns) in the record given by ** pKey,nKey. The verify that this count is less than or equal to the -** limit given by pKeyInfo->nField + pKeyInfo->nXField. +** limit given by pKeyInfo->nAllField. ** ** If this constraint is not satisfied, it means that the high-speed ** vdbeRecordCompareInt() and vdbeRecordCompareString() routines will ** not work correctly. If this assert() ever fires, it probably means -** that the KeyInfo.nField or KeyInfo.nXField values were computed +** that the KeyInfo.nKeyField or KeyInfo.nAllField values were computed ** incorrectly. */ static void vdbeAssertFieldCountWithinLimits( - int nKey, const void *pKey, /* The record to verify */ + int nKey, const void *pKey, /* The record to verify */ const KeyInfo *pKeyInfo /* Compare size with this KeyInfo */ ){ int nField = 0; @@ -3629,7 +4431,7 @@ static void vdbeAssertFieldCountWithinLimits( idx += getVarint32(aKey+idx, notUsed); nField++; } - assert( nField <= pKeyInfo->nField+pKeyInfo->nXField ); + assert( nField <= pKeyInfo->nAllField ); } #else # define vdbeAssertFieldCountWithinLimits(A,B,C) @@ -3638,9 +4440,35 @@ static void vdbeAssertFieldCountWithinLimits( /* ** Both *pMem1 and *pMem2 contain string values. Compare the two values ** using the collation sequence pColl. As usual, return a negative , zero -** or positive value if *pMem1 is less than, equal to or greater than +** or positive value if *pMem1 is less than, equal to or greater than ** *pMem2, respectively. Similar in spirit to "rc = (*pMem1) - (*pMem2);". */ +static SQLITE_NOINLINE int vdbeCompareMemStringWithEncodingChange( + const Mem *pMem1, + const Mem *pMem2, + const CollSeq *pColl, + u8 *prcErr /* If an OOM occurs, set to SQLITE_NOMEM */ +){ + int rc; + const void *v1, *v2; + Mem c1; + Mem c2; + sqlite3VdbeMemInit(&c1, pMem1->db, MEM_Null); + sqlite3VdbeMemInit(&c2, pMem1->db, MEM_Null); + sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem); + sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem); + v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc); + v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc); + if( (v1==0 || v2==0) ){ + if( prcErr ) *prcErr = SQLITE_NOMEM_BKPT; + rc = 0; + }else{ + rc = pColl->xCmp(pColl->pUser, c1.n, v1, c2.n, v2); + } + sqlite3VdbeMemReleaseMalloc(&c1); + sqlite3VdbeMemReleaseMalloc(&c2); + return rc; +} static int vdbeCompareMemString( const Mem *pMem1, const Mem *pMem2, @@ -3652,25 +4480,20 @@ static int vdbeCompareMemString( ** comparison function directly */ return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z); }else{ - int rc; - const void *v1, *v2; - int n1, n2; - Mem c1; - Mem c2; - sqlite3VdbeMemInit(&c1, pMem1->db, MEM_Null); - sqlite3VdbeMemInit(&c2, pMem1->db, MEM_Null); - sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem); - sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem); - v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc); - n1 = v1==0 ? 0 : c1.n; - v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc); - n2 = v2==0 ? 0 : c2.n; - rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2); - if( (v1==0 || v2==0) && prcErr ) *prcErr = SQLITE_NOMEM; - sqlite3VdbeMemRelease(&c1); - sqlite3VdbeMemRelease(&c2); - return rc; + return vdbeCompareMemStringWithEncodingChange(pMem1,pMem2,pColl,prcErr); + } +} + +/* +** The input pBlob is guaranteed to be a Blob that is not marked +** with MEM_Zero. Return true if it could be a zero-blob. +*/ +static int isAllZero(const char *z, int n){ + int i; + for(i=0; i<n; i++){ + if( z[i] ) return 0; } + return 1; } /* @@ -3678,38 +4501,65 @@ static int vdbeCompareMemString( ** is less than, equal to, or greater than the second, respectively. ** If one blob is a prefix of the other, then the shorter is the lessor. */ -static SQLITE_NOINLINE int sqlite3BlobCompare(const Mem *pB1, const Mem *pB2){ - int c = memcmp(pB1->z, pB2->z, pB1->n>pB2->n ? pB2->n : pB1->n); +SQLITE_NOINLINE int sqlite3BlobCompare(const Mem *pB1, const Mem *pB2){ + int c; + int n1 = pB1->n; + int n2 = pB2->n; + + /* It is possible to have a Blob value that has some non-zero content + ** followed by zero content. But that only comes up for Blobs formed + ** by the OP_MakeRecord opcode, and such Blobs never get passed into + ** sqlite3MemCompare(). */ + assert( (pB1->flags & MEM_Zero)==0 || n1==0 ); + assert( (pB2->flags & MEM_Zero)==0 || n2==0 ); + + if( (pB1->flags|pB2->flags) & MEM_Zero ){ + if( pB1->flags & pB2->flags & MEM_Zero ){ + return pB1->u.nZero - pB2->u.nZero; + }else if( pB1->flags & MEM_Zero ){ + if( !isAllZero(pB2->z, pB2->n) ) return -1; + return pB1->u.nZero - n2; + }else{ + if( !isAllZero(pB1->z, pB1->n) ) return +1; + return n1 - pB2->u.nZero; + } + } + c = memcmp(pB1->z, pB2->z, n1>n2 ? n2 : n1); if( c ) return c; - return pB1->n - pB2->n; + return n1 - n2; } +/* The following two functions are used only within testcase() to prove +** test coverage. These functions do no exist for production builds. +** We must use separate SQLITE_NOINLINE functions here, since otherwise +** optimizer code movement causes gcov to become very confused. +*/ +#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_DEBUG) +static int SQLITE_NOINLINE doubleLt(double a, double b){ return a<b; } +static int SQLITE_NOINLINE doubleEq(double a, double b){ return a==b; } +#endif + /* ** Do a comparison between a 64-bit signed integer and a 64-bit floating-point ** number. Return negative, zero, or positive if the first (i64) is less than, ** equal to, or greater than the second (double). */ -static int sqlite3IntFloatCompare(i64 i, double r){ - if( sizeof(LONGDOUBLE_TYPE)>8 ){ - LONGDOUBLE_TYPE x = (LONGDOUBLE_TYPE)i; - if( x<r ) return -1; - if( x>r ) return +1; - return 0; +int sqlite3IntFloatCompare(i64 i, double r){ + if( sqlite3IsNaN(r) ){ + /* SQLite considers NaN to be a NULL. And all integer values are greater + ** than NULL */ + return 1; }else{ i64 y; - double s; if( r<-9223372036854775808.0 ) return +1; - if( r>9223372036854775807.0 ) return -1; + if( r>=9223372036854775808.0 ) return -1; y = (i64)r; if( i<y ) return -1; - if( i>y ){ - if( y==SMALLEST_INT64 && r>0.0 ) return -1; - return +1; - } - s = (double)i; - if( s<r ) return -1; - if( s>r ) return +1; - return 0; + if( i>y ) return +1; + testcase( doubleLt(((double)i),r) ); + testcase( doubleLt(r,((double)i)) ); + testcase( doubleEq(r,((double)i)) ); + return (((double)i)<r) ? -1 : (((double)i)>r); } } @@ -3729,8 +4579,8 @@ int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){ f1 = pMem1->flags; f2 = pMem2->flags; combined_flags = f1|f2; - assert( (combined_flags & MEM_RowSet)==0 ); - + assert( !sqlite3VdbeMemIsRowSet(pMem1) && !sqlite3VdbeMemIsRowSet(pMem2) ); + /* If one value is NULL, it is less than the other. If both values ** are NULL, return 0. */ @@ -3740,8 +4590,13 @@ int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){ /* At least one of the two values is a number */ - if( combined_flags&(MEM_Int|MEM_Real) ){ - if( (f1 & f2 & MEM_Int)!=0 ){ + if( combined_flags&(MEM_Int|MEM_Real|MEM_IntReal) ){ + testcase( combined_flags & MEM_Int ); + testcase( combined_flags & MEM_Real ); + testcase( combined_flags & MEM_IntReal ); + if( (f1 & f2 & (MEM_Int|MEM_IntReal))!=0 ){ + testcase( f1 & f2 & MEM_Int ); + testcase( f1 & f2 & MEM_IntReal ); if( pMem1->u.i < pMem2->u.i ) return -1; if( pMem1->u.i > pMem2->u.i ) return +1; return 0; @@ -3751,15 +4606,23 @@ int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){ if( pMem1->u.r > pMem2->u.r ) return +1; return 0; } - if( (f1&MEM_Int)!=0 ){ + if( (f1&(MEM_Int|MEM_IntReal))!=0 ){ + testcase( f1 & MEM_Int ); + testcase( f1 & MEM_IntReal ); if( (f2&MEM_Real)!=0 ){ return sqlite3IntFloatCompare(pMem1->u.i, pMem2->u.r); + }else if( (f2&(MEM_Int|MEM_IntReal))!=0 ){ + if( pMem1->u.i < pMem2->u.i ) return -1; + if( pMem1->u.i > pMem2->u.i ) return +1; + return 0; }else{ return -1; } } if( (f1&MEM_Real)!=0 ){ - if( (f2&MEM_Int)!=0 ){ + if( (f2&(MEM_Int|MEM_IntReal))!=0 ){ + testcase( f2 & MEM_Int ); + testcase( f2 & MEM_IntReal ); return -sqlite3IntFloatCompare(pMem2->u.i, pMem1->u.r); }else{ return -1; @@ -3780,7 +4643,7 @@ int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){ } assert( pMem1->enc==pMem2->enc || pMem1->db->mallocFailed ); - assert( pMem1->enc==SQLITE_UTF8 || + assert( pMem1->enc==SQLITE_UTF8 || pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE ); /* The collation sequence must be defined at this point, even if @@ -3795,7 +4658,7 @@ int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){ /* If a NULL pointer was passed as the collate function, fall through ** to the blob case and use memcmp(). */ } - + /* Both values must be blobs. Compare using memcmp(). */ return sqlite3BlobCompare(pMem1, pMem2); } @@ -3803,7 +4666,7 @@ int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){ /* ** The first argument passed to this function is a serial-type that -** corresponds to an integer - all values between 1 and 9 inclusive +** corresponds to an integer - all values between 1 and 9 inclusive ** except 7. The second points to a buffer containing an integer value ** serialized according to serial_type. This function deserializes ** and returns the value. @@ -3845,7 +4708,7 @@ static i64 vdbeRecordDecodeInt(u32 serial_type, const u8 *aKey){ /* ** This function compares the two table rows or index records ** specified by {nKey1, pKey1} and pPKey2. It returns a negative, zero -** or positive integer if key1 is less than, equal to or +** or positive integer if key1 is less than, equal to or ** greater than key2. The {nKey1, pKey1} key must be a blob ** created by the OP_MakeRecord opcode of the VDBE. The pPKey2 ** key must be a parsed key such as obtained from @@ -3854,12 +4717,12 @@ static i64 vdbeRecordDecodeInt(u32 serial_type, const u8 *aKey){ ** If argument bSkip is non-zero, it is assumed that the caller has already ** determined that the first fields of the keys are equal. ** -** Key1 and Key2 do not have to contain the same number of fields. If all -** fields that appear in both keys are equal, then pPKey2->default_rc is +** Key1 and Key2 do not have to contain the same number of fields. If all +** fields that appear in both keys are equal, then pPKey2->default_rc is ** returned. ** -** If database corruption is discovered, set pPKey2->errCode to -** SQLITE_CORRUPT and return 0. If an OOM error is encountered, +** If database corruption is discovered, set pPKey2->errCode to +** SQLITE_CORRUPT and return 0. If an OOM error is encountered, ** pPKey2->errCode is set to SQLITE_NOMEM and, if it is not NULL, the ** malloc-failed flag set on database handle (pPKey2->pKeyInfo->db). */ @@ -3874,7 +4737,7 @@ int sqlite3VdbeRecordCompareWithSkip( u32 idx1; /* Offset of first type in header */ int rc = 0; /* Return value */ Mem *pRhs = pPKey2->aMem; /* Next field of pPKey2 to compare */ - KeyInfo *pKeyInfo = pPKey2->pKeyInfo; + KeyInfo *pKeyInfo; const unsigned char *aKey1 = (const unsigned char *)pKey1; Mem mem1; @@ -3882,41 +4745,51 @@ int sqlite3VdbeRecordCompareWithSkip( ** two elements in the keys are equal. Fix the various stack variables so ** that this routine begins comparing at the second field. */ if( bSkip ){ - u32 s1; - idx1 = 1 + getVarint32(&aKey1[1], s1); + u32 s1 = aKey1[1]; + if( s1<0x80 ){ + idx1 = 2; + }else{ + idx1 = 1 + sqlite3GetVarint32(&aKey1[1], &s1); + } szHdr1 = aKey1[0]; d1 = szHdr1 + sqlite3VdbeSerialTypeLen(s1); i = 1; pRhs++; }else{ - idx1 = getVarint32(aKey1, szHdr1); - d1 = szHdr1; - if( d1>(unsigned)nKey1 ){ - pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT; - return 0; /* Corruption */ + if( (szHdr1 = aKey1[0])<0x80 ){ + idx1 = 1; + }else{ + idx1 = sqlite3GetVarint32(aKey1, &szHdr1); } + d1 = szHdr1; i = 0; } + if( d1>(unsigned)nKey1 ){ + pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT; + return 0; /* Corruption */ + } VVA_ONLY( mem1.szMalloc = 0; ) /* Only needed by assert() statements */ - assert( pPKey2->pKeyInfo->nField+pPKey2->pKeyInfo->nXField>=pPKey2->nField + assert( pPKey2->pKeyInfo->nAllField>=pPKey2->nField || CORRUPT_DB ); - assert( pPKey2->pKeyInfo->aSortOrder!=0 ); - assert( pPKey2->pKeyInfo->nField>0 ); + assert( pPKey2->pKeyInfo->aSortFlags!=0 ); + assert( pPKey2->pKeyInfo->nKeyField>0 ); assert( idx1<=szHdr1 || CORRUPT_DB ); - do{ + while( 1 /*exit-by-break*/ ){ u32 serial_type; /* RHS is an integer */ - if( pRhs->flags & MEM_Int ){ + if( pRhs->flags & (MEM_Int|MEM_IntReal) ){ + testcase( pRhs->flags & MEM_Int ); + testcase( pRhs->flags & MEM_IntReal ); serial_type = aKey1[idx1]; testcase( serial_type==12 ); if( serial_type>=10 ){ - rc = +1; + rc = serial_type==10 ? -1 : +1; }else if( serial_type==0 ){ rc = -1; }else if( serial_type==7 ){ - sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1); + serialGet7(&aKey1[d1], &mem1); rc = -sqlite3IntFloatCompare(pRhs->u.i, mem1.u.r); }else{ i64 lhs = vdbeRecordDecodeInt(serial_type, &aKey1[d1]); @@ -3934,21 +4807,25 @@ int sqlite3VdbeRecordCompareWithSkip( serial_type = aKey1[idx1]; if( serial_type>=10 ){ /* Serial types 12 or greater are strings and blobs (greater than - ** numbers). Types 10 and 11 are currently "reserved for future + ** numbers). Types 10 and 11 are currently "reserved for future ** use", so it doesn't really matter what the results of comparing - ** them to numberic values are. */ - rc = +1; + ** them to numeric values are. */ + rc = serial_type==10 ? -1 : +1; }else if( serial_type==0 ){ rc = -1; }else{ - sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1); if( serial_type==7 ){ - if( mem1.u.r<pRhs->u.r ){ + if( serialGet7(&aKey1[d1], &mem1) ){ + rc = -1; /* mem1 is a NaN */ + }else if( mem1.u.r<pRhs->u.r ){ rc = -1; }else if( mem1.u.r>pRhs->u.r ){ rc = +1; + }else{ + assert( rc==0 ); } }else{ + sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1); rc = sqlite3IntFloatCompare(mem1.u.i, pRhs->u.r); } } @@ -3956,7 +4833,7 @@ int sqlite3VdbeRecordCompareWithSkip( /* RHS is a string */ else if( pRhs->flags & MEM_Str ){ - getVarint32(&aKey1[idx1], serial_type); + getVarint32NR(&aKey1[idx1], serial_type); testcase( serial_type==12 ); if( serial_type<12 ){ rc = -1; @@ -3966,7 +4843,9 @@ int sqlite3VdbeRecordCompareWithSkip( mem1.n = (serial_type - 12) / 2; testcase( (d1+mem1.n)==(unsigned)nKey1 ); testcase( (d1+mem1.n+1)==(unsigned)nKey1 ); - if( (d1+mem1.n) > (unsigned)nKey1 ){ + if( (d1+mem1.n) > (unsigned)nKey1 + || (pKeyInfo = pPKey2->pKeyInfo)->nAllField<=i + ){ pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT; return 0; /* Corruption */ }else if( pKeyInfo->aColl[i] ){ @@ -3980,14 +4859,15 @@ int sqlite3VdbeRecordCompareWithSkip( }else{ int nCmp = MIN(mem1.n, pRhs->n); rc = memcmp(&aKey1[d1], pRhs->z, nCmp); - if( rc==0 ) rc = mem1.n - pRhs->n; + if( rc==0 ) rc = mem1.n - pRhs->n; } } } /* RHS is a blob */ else if( pRhs->flags & MEM_Blob ){ - getVarint32(&aKey1[idx1], serial_type); + assert( (pRhs->flags & MEM_Zero)==0 || pRhs->n==0 ); + getVarint32NR(&aKey1[idx1], serial_type); testcase( serial_type==12 ); if( serial_type<12 || (serial_type & 0x01) ){ rc = -1; @@ -3998,6 +4878,12 @@ int sqlite3VdbeRecordCompareWithSkip( if( (d1+nStr) > (unsigned)nKey1 ){ pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT; return 0; /* Corruption */ + }else if( pRhs->flags & MEM_Zero ){ + if( !isAllZero((const char*)&aKey1[d1],nStr) ){ + rc = 1; + }else{ + rc = nStr - pRhs->u.nZero; + } }else{ int nCmp = MIN(nStr, pRhs->n); rc = memcmp(&aKey1[d1], pRhs->z, nCmp); @@ -4009,12 +4895,25 @@ int sqlite3VdbeRecordCompareWithSkip( /* RHS is null */ else{ serial_type = aKey1[idx1]; - rc = (serial_type!=0); + if( serial_type==0 + || serial_type==10 + || (serial_type==7 && serialGet7(&aKey1[d1], &mem1)!=0) + ){ + assert( rc==0 ); + }else{ + rc = 1; + } } if( rc!=0 ){ - if( pKeyInfo->aSortOrder[i] ){ - rc = -rc; + int sortFlags = pPKey2->pKeyInfo->aSortFlags[i]; + if( sortFlags ){ + if( (sortFlags & KEYINFO_ORDER_BIGNULL)==0 + || ((sortFlags & KEYINFO_ORDER_DESC) + !=(serial_type==0 || (pRhs->flags&MEM_Null))) + ){ + rc = -rc; + } } assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, rc) ); assert( mem1.szMalloc==0 ); /* See comment below */ @@ -4022,10 +4921,16 @@ int sqlite3VdbeRecordCompareWithSkip( } i++; + if( i==pPKey2->nField ) break; pRhs++; d1 += sqlite3VdbeSerialTypeLen(serial_type); + if( d1>(unsigned)nKey1 ) break; idx1 += sqlite3VarintLen(serial_type); - }while( idx1<(unsigned)szHdr1 && i<pPKey2->nField && d1<=(unsigned)nKey1 ); + if( idx1>=(unsigned)szHdr1 ){ + pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT; + return 0; /* Corrupt index */ + } + } /* No memory allocation is ever used on mem1. Prove this using ** the following assert(). If the assert() fails, it indicates a @@ -4035,9 +4940,9 @@ int sqlite3VdbeRecordCompareWithSkip( /* rc==0 here means that one or both of the keys ran out of fields and ** all the fields up to that point were equal. Return the default_rc ** value. */ - assert( CORRUPT_DB - || vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, pPKey2->default_rc) - || pKeyInfo->db->mallocFailed + assert( CORRUPT_DB + || vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, pPKey2->default_rc) + || pPKey2->pKeyInfo->db->mallocFailed ); pPKey2->eqSeen = 1; return pPKey2->default_rc; @@ -4051,8 +4956,8 @@ int sqlite3VdbeRecordCompare( /* -** This function is an optimized version of sqlite3VdbeRecordCompare() -** that (a) the first field of pPKey2 is an integer, and (b) the +** This function is an optimized version of sqlite3VdbeRecordCompare() +** that (a) the first field of pPKey2 is an integer, and (b) the ** size-of-header varint at the start of (pKey1/nKey1) fits in a single ** byte (i.e. is less than 128). ** @@ -4068,7 +4973,7 @@ static int vdbeRecordCompareInt( int res; u32 y; u64 x; - i64 v = pPKey2->aMem[0].u.i; + i64 v; i64 lhs; vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo); @@ -4107,7 +5012,7 @@ static int vdbeRecordCompareInt( testcase( lhs<0 ); break; } - case 8: + case 8: lhs = 0; break; case 9: @@ -4115,11 +5020,11 @@ static int vdbeRecordCompareInt( break; /* This case could be removed without changing the results of running - ** this code. Including it causes gcc to generate a faster switch + ** this code. Including it causes gcc to generate a faster switch ** statement (since the range of switch targets now starts at zero and ** is contiguous) but does not cause any duplicate code to be generated - ** (as gcc is clever enough to combine the two like cases). Other - ** compilers might be similar. */ + ** (as gcc is clever enough to combine the two like cases). Other + ** compilers might be similar. */ case 0: case 7: return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2); @@ -4127,12 +5032,14 @@ static int vdbeRecordCompareInt( return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2); } + assert( pPKey2->u.i == pPKey2->aMem[0].u.i ); + v = pPKey2->u.i; if( v>lhs ){ res = pPKey2->r1; }else if( v<lhs ){ res = pPKey2->r2; }else if( pPKey2->nField>1 ){ - /* The first fields of the two keys are equal. Compare the trailing + /* The first fields of the two keys are equal. Compare the trailing ** fields. */ res = sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1); }else{ @@ -4147,9 +5054,9 @@ static int vdbeRecordCompareInt( } /* -** This function is an optimized version of sqlite3VdbeRecordCompare() +** This function is an optimized version of sqlite3VdbeRecordCompare() ** that (a) the first field of pPKey2 is a string, that (b) the first field -** uses the collation sequence BINARY and (c) that the size-of-header varint +** uses the collation sequence BINARY and (c) that the size-of-header varint ** at the start of (pKey1/nKey1) fits in a single byte. */ static int vdbeRecordCompareString( @@ -4161,11 +5068,20 @@ static int vdbeRecordCompareString( int res; assert( pPKey2->aMem[0].flags & MEM_Str ); + assert( pPKey2->aMem[0].n == pPKey2->n ); + assert( pPKey2->aMem[0].z == pPKey2->u.z ); vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo); - getVarint32(&aKey1[1], serial_type); + serial_type = (signed char)(aKey1[1]); + +vrcs_restart: if( serial_type<12 ){ + if( serial_type<0 ){ + sqlite3GetVarint32(&aKey1[1], (u32*)&serial_type); + if( serial_type>=12 ) goto vrcs_restart; + assert( CORRUPT_DB ); + } res = pPKey2->r1; /* (pKey1/nKey1) is a number or a null */ - }else if( !(serial_type & 0x01) ){ + }else if( !(serial_type & 0x01) ){ res = pPKey2->r2; /* (pKey1/nKey1) is a blob */ }else{ int nCmp; @@ -4177,11 +5093,15 @@ static int vdbeRecordCompareString( pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT; return 0; /* Corruption */ } - nCmp = MIN( pPKey2->aMem[0].n, nStr ); - res = memcmp(&aKey1[szHdr], pPKey2->aMem[0].z, nCmp); + nCmp = MIN( pPKey2->n, nStr ); + res = memcmp(&aKey1[szHdr], pPKey2->u.z, nCmp); - if( res==0 ){ - res = nStr - pPKey2->aMem[0].n; + if( res>0 ){ + res = pPKey2->r2; + }else if( res<0 ){ + res = pPKey2->r1; + }else{ + res = nStr - pPKey2->n; if( res==0 ){ if( pPKey2->nField>1 ){ res = sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1); @@ -4194,10 +5114,6 @@ static int vdbeRecordCompareString( }else{ res = pPKey2->r1; } - }else if( res>0 ){ - res = pPKey2->r2; - }else{ - res = pPKey2->r1; } } @@ -4217,7 +5133,7 @@ RecordCompare sqlite3VdbeFindCompare(UnpackedRecord *p){ /* varintRecordCompareInt() and varintRecordCompareString() both assume ** that the size-of-header varint that occurs at the start of each record ** fits in a single byte (i.e. is 127 or less). varintRecordCompareInt() - ** also assumes that it is safe to overread a buffer by at least the + ** also assumes that it is safe to overread a buffer by at least the ** maximum possible legal header size plus 8 bytes. Because there is ** guaranteed to be at least 74 (but not 136) bytes of padding following each ** buffer passed to varintRecordCompareInt() this makes it convenient to @@ -4227,9 +5143,13 @@ RecordCompare sqlite3VdbeFindCompare(UnpackedRecord *p){ ** The easiest way to enforce this limit is to consider only records with ** 13 fields or less. If the first field is an integer, the maximum legal ** header size is (12*5 + 1 + 1) bytes. */ - if( (p->pKeyInfo->nField + p->pKeyInfo->nXField)<=13 ){ + assert( p->pKeyInfo->aSortFlags!=0 ); + if( p->pKeyInfo->nAllField<=13 ){ int flags = p->aMem[0].flags; - if( p->pKeyInfo->aSortOrder[0] ){ + if( p->pKeyInfo->aSortFlags[0] ){ + if( p->pKeyInfo->aSortFlags[0] & KEYINFO_ORDER_BIGNULL ){ + return sqlite3VdbeRecordCompare; + } p->r1 = 1; p->r2 = -1; }else{ @@ -4237,13 +5157,18 @@ RecordCompare sqlite3VdbeFindCompare(UnpackedRecord *p){ p->r2 = 1; } if( (flags & MEM_Int) ){ + p->u.i = p->aMem[0].u.i; return vdbeRecordCompareInt; } testcase( flags & MEM_Real ); testcase( flags & MEM_Null ); testcase( flags & MEM_Blob ); - if( (flags & (MEM_Real|MEM_Null|MEM_Blob))==0 && p->pKeyInfo->aColl[0]==0 ){ + if( (flags & (MEM_Real|MEM_IntReal|MEM_Null|MEM_Blob))==0 + && p->pKeyInfo->aColl[0]==0 + ){ assert( flags & MEM_Str ); + p->u.z = p->aMem[0].z; + p->n = p->aMem[0].n; return vdbeRecordCompareString; } } @@ -4270,31 +5195,32 @@ int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){ /* Get the size of the index entry. Only indices entries of less ** than 2GiB are support - anything large must be database corruption. ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so - ** this code can safely assume that nCellKey is 32-bits + ** this code can safely assume that nCellKey is 32-bits */ assert( sqlite3BtreeCursorIsValid(pCur) ); - VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey); - assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */ + nCellKey = sqlite3BtreePayloadSize(pCur); assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey ); /* Read in the complete content of the index entry */ sqlite3VdbeMemInit(&m, db, 0); - rc = sqlite3VdbeMemFromBtree(pCur, 0, (u32)nCellKey, 1, &m); + rc = sqlite3VdbeMemFromBtreeZeroOffset(pCur, (u32)nCellKey, &m); if( rc ){ return rc; } /* The index entry must begin with a header size */ - (void)getVarint32((u8*)m.z, szHdr); + getVarint32NR((u8*)m.z, szHdr); testcase( szHdr==3 ); - testcase( szHdr==m.n ); - if( unlikely(szHdr<3 || (int)szHdr>m.n) ){ + testcase( szHdr==(u32)m.n ); + testcase( szHdr>0x7fffffff ); + assert( m.n>=0 ); + if( unlikely(szHdr<3 || szHdr>(unsigned)m.n) ){ goto idx_rowid_corruption; } /* The last field of the index should be an integer - the ROWID. ** Verify that the last entry really is an integer. */ - (void)getVarint32((u8*)&m.z[szHdr-1], typeRowid); + getVarint32NR((u8*)&m.z[szHdr-1], typeRowid); testcase( typeRowid==1 ); testcase( typeRowid==2 ); testcase( typeRowid==3 ); @@ -4315,14 +5241,14 @@ int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){ /* Fetch the integer off the end of the index record */ sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v); *rowid = v.u.i; - sqlite3VdbeMemRelease(&m); + sqlite3VdbeMemReleaseMalloc(&m); return SQLITE_OK; /* Jump here if database corruption is detected after m has been ** allocated. Free the m object and return SQLITE_CORRUPT. */ idx_rowid_corruption: testcase( m.szMalloc!=0 ); - sqlite3VdbeMemRelease(&m); + sqlite3VdbeMemReleaseMalloc(&m); return SQLITE_CORRUPT_BKPT; } @@ -4334,7 +5260,7 @@ int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){ ** ** pUnpacked is either created without a rowid or is truncated so that it ** omits the rowid at the end. The rowid at the end of the index entry -** is ignored as well. Hence, this routine only compares the prefixes +** is ignored as well. Hence, this routine only compares the prefixes ** of the keys prior to the final rowid, not the entire key. */ int sqlite3VdbeIdxKeyCompare( @@ -4351,8 +5277,7 @@ int sqlite3VdbeIdxKeyCompare( assert( pC->eCurType==CURTYPE_BTREE ); pCur = pC->uc.pCursor; assert( sqlite3BtreeCursorIsValid(pCur) ); - VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey); - assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */ + nCellKey = sqlite3BtreePayloadSize(pCur); /* nCellKey will always be between 0 and 0xffffffff because of the way ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */ if( nCellKey<=0 || nCellKey>0x7fffffff ){ @@ -4360,20 +5285,20 @@ int sqlite3VdbeIdxKeyCompare( return SQLITE_CORRUPT_BKPT; } sqlite3VdbeMemInit(&m, db, 0); - rc = sqlite3VdbeMemFromBtree(pCur, 0, (u32)nCellKey, 1, &m); + rc = sqlite3VdbeMemFromBtreeZeroOffset(pCur, (u32)nCellKey, &m); if( rc ){ return rc; } - *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked); - sqlite3VdbeMemRelease(&m); + *res = sqlite3VdbeRecordCompareWithSkip(m.n, m.z, pUnpacked, 0); + sqlite3VdbeMemReleaseMalloc(&m); return SQLITE_OK; } /* ** This routine sets the value to be returned by subsequent calls to -** sqlite3_changes() on the database handle 'db'. +** sqlite3_changes() on the database handle 'db'. */ -void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){ +void sqlite3VdbeSetChanges(sqlite3 *db, i64 nChange){ assert( sqlite3_mutex_held(db->mutex) ); db->nChange = nChange; db->nTotalChange += nChange; @@ -4396,11 +5321,19 @@ void sqlite3VdbeCountChanges(Vdbe *v){ ** programs obsolete. Removing user-defined functions or collating ** sequences, or changing an authorization function are the types of ** things that make prepared statements obsolete. +** +** If iCode is 1, then expiration is advisory. The statement should +** be reprepared before being restarted, but if it is already running +** it is allowed to run to completion. +** +** Internally, this function just sets the Vdbe.expired flag on all +** prepared statements. The flag is set to 1 for an immediate expiration +** and set to 2 for an advisory expiration. */ -void sqlite3ExpirePreparedStatements(sqlite3 *db){ +void sqlite3ExpirePreparedStatements(sqlite3 *db, int iCode){ Vdbe *p; - for(p = db->pVdbe; p; p=p->pNext){ - p->expired = 1; + for(p = db->pVdbe; p; p=p->pVNext){ + p->expired = iCode+1; } } @@ -4411,9 +5344,16 @@ sqlite3 *sqlite3VdbeDb(Vdbe *v){ return v->db; } +/* +** Return the SQLITE_PREPARE flags for a Vdbe. +*/ +u8 sqlite3VdbePrepareFlags(Vdbe *v){ + return v->prepFlags; +} + /* ** Return a pointer to an sqlite3_value structure containing the value bound -** parameter iVar of VM v. Except, if the value is an SQL NULL, return +** parameter iVar of VM v. Except, if the value is an SQL NULL, return ** 0 instead. Unless it is NULL, apply affinity aff (one of the SQLITE_AFF_* ** constants) to the value before returning it. ** @@ -4423,6 +5363,8 @@ sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe *v, int iVar, u8 aff){ assert( iVar>0 ); if( v ){ Mem *pMem = &v->aVar[iVar-1]; + assert( (v->db->flags & SQLITE_EnableQPSG)==0 + || (v->db->mDbFlags & DBFLAG_InternalFunc)!=0 ); if( 0==(pMem->flags & MEM_Null) ){ sqlite3_value *pRet = sqlite3ValueNew(v->db); if( pRet ){ @@ -4442,13 +5384,65 @@ sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe *v, int iVar, u8 aff){ */ void sqlite3VdbeSetVarmask(Vdbe *v, int iVar){ assert( iVar>0 ); - if( iVar>32 ){ - v->expmask = 0xffffffff; + assert( (v->db->flags & SQLITE_EnableQPSG)==0 + || (v->db->mDbFlags & DBFLAG_InternalFunc)!=0 ); + if( iVar>=32 ){ + v->expmask |= 0x80000000; }else{ v->expmask |= ((u32)1 << (iVar-1)); } } +#ifndef SQLITE_OMIT_DATETIME_FUNCS +/* +** Cause a function to throw an error if it was call from OP_PureFunc +** rather than OP_Function. +** +** OP_PureFunc means that the function must be deterministic, and should +** throw an error if it is given inputs that would make it non-deterministic. +** This routine is invoked by date/time functions that use non-deterministic +** features such as 'now'. +*/ +int sqlite3NotPureFunc(sqlite3_context *pCtx){ + const VdbeOp *pOp; +#ifdef SQLITE_ENABLE_STAT4 + if( pCtx->pVdbe==0 ) return 1; +#endif + pOp = pCtx->pVdbe->aOp + pCtx->iOp; + if( pOp->opcode==OP_PureFunc ){ + const char *zContext; + char *zMsg; + if( pOp->p5 & NC_IsCheck ){ + zContext = "a CHECK constraint"; + }else if( pOp->p5 & NC_GenCol ){ + zContext = "a generated column"; + }else{ + zContext = "an index"; + } + zMsg = sqlite3_mprintf("non-deterministic use of %s() in %s", + pCtx->pFunc->zName, zContext); + sqlite3_result_error(pCtx, zMsg, -1); + sqlite3_free(zMsg); + return 0; + } + return 1; +} +#endif /* SQLITE_OMIT_DATETIME_FUNCS */ + +#if defined(SQLITE_ENABLE_CURSOR_HINTS) && defined(SQLITE_DEBUG) +/* +** This Walker callback is used to help verify that calls to +** sqlite3BtreeCursorHint() with opcode BTREE_HINT_RANGE have +** byte-code register values correctly initialized. +*/ +int sqlite3CursorRangeHintExprCheck(Walker *pWalker, Expr *pExpr){ + if( pExpr->op==TK_REGISTER ){ + assert( (pWalker->u.aMem[pExpr->iTable].flags & MEM_Undefined)==0 ); + } + return WRC_Continue; +} +#endif /* SQLITE_ENABLE_CURSOR_HINTS && SQLITE_DEBUG */ + #ifndef SQLITE_OMIT_VIRTUALTABLE /* ** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored @@ -4465,3 +5459,126 @@ void sqlite3VtabImportErrmsg(Vdbe *p, sqlite3_vtab *pVtab){ } } #endif /* SQLITE_OMIT_VIRTUALTABLE */ + +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK + +/* +** If the second argument is not NULL, release any allocations associated +** with the memory cells in the p->aMem[] array. Also free the UnpackedRecord +** structure itself, using sqlite3DbFree(). +** +** This function is used to free UnpackedRecord structures allocated by +** the vdbeUnpackRecord() function found in vdbeapi.c. +*/ +static void vdbeFreeUnpacked(sqlite3 *db, int nField, UnpackedRecord *p){ + assert( db!=0 ); + if( p ){ + int i; + for(i=0; i<nField; i++){ + Mem *pMem = &p->aMem[i]; + if( pMem->zMalloc ) sqlite3VdbeMemReleaseMalloc(pMem); + } + sqlite3DbNNFreeNN(db, p); + } +} +#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ + +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK +/* +** Invoke the pre-update hook. If this is an UPDATE or DELETE pre-update call, +** then cursor passed as the second argument should point to the row about +** to be update or deleted. If the application calls sqlite3_preupdate_old(), +** the required value will be read from the row the cursor points to. +*/ +void sqlite3VdbePreUpdateHook( + Vdbe *v, /* Vdbe pre-update hook is invoked by */ + VdbeCursor *pCsr, /* Cursor to grab old.* values from */ + int op, /* SQLITE_INSERT, UPDATE or DELETE */ + const char *zDb, /* Database name */ + Table *pTab, /* Modified table */ + i64 iKey1, /* Initial key value */ + int iReg, /* Register for new.* record */ + int iBlobWrite +){ + sqlite3 *db = v->db; + i64 iKey2; + PreUpdate preupdate; + const char *zTbl = pTab->zName; +#ifdef SQLITE_DEBUG + int nRealCol; + if( pTab->tabFlags & TF_WithoutRowid ){ + nRealCol = sqlite3PrimaryKeyIndex(pTab)->nColumn; + }else if( pTab->tabFlags & TF_HasVirtual ){ + nRealCol = pTab->nNVCol; + }else{ + nRealCol = pTab->nCol; + } +#endif + + assert( db->pPreUpdate==0 ); + memset(&preupdate, 0, sizeof(PreUpdate)); + if( HasRowid(pTab)==0 ){ + iKey1 = iKey2 = 0; + preupdate.pPk = sqlite3PrimaryKeyIndex(pTab); + }else{ + if( op==SQLITE_UPDATE ){ + iKey2 = v->aMem[iReg].u.i; + }else{ + iKey2 = iKey1; + } + } + + assert( pCsr!=0 ); + assert( pCsr->eCurType==CURTYPE_BTREE ); + assert( pCsr->nField==nRealCol + || (pCsr->nField==nRealCol+1 && op==SQLITE_DELETE && iReg==-1) + ); + + preupdate.v = v; + preupdate.pCsr = pCsr; + preupdate.op = op; + preupdate.iNewReg = iReg; + preupdate.pKeyinfo = (KeyInfo*)&preupdate.uKey; + preupdate.pKeyinfo->db = db; + preupdate.pKeyinfo->enc = ENC(db); + preupdate.pKeyinfo->nKeyField = pTab->nCol; + preupdate.pKeyinfo->aSortFlags = 0; /* Indicate .aColl, .nAllField uninit */ + preupdate.iKey1 = iKey1; + preupdate.iKey2 = iKey2; + preupdate.pTab = pTab; + preupdate.iBlobWrite = iBlobWrite; + + db->pPreUpdate = &preupdate; + db->xPreUpdateCallback(db->pPreUpdateArg, db, op, zDb, zTbl, iKey1, iKey2); + db->pPreUpdate = 0; + sqlite3DbFree(db, preupdate.aRecord); + vdbeFreeUnpacked(db, preupdate.pKeyinfo->nKeyField+1,preupdate.pUnpacked); + vdbeFreeUnpacked(db, preupdate.pKeyinfo->nKeyField+1,preupdate.pNewUnpacked); + sqlite3VdbeMemRelease(&preupdate.oldipk); + if( preupdate.aNew ){ + int i; + for(i=0; i<pCsr->nField; i++){ + sqlite3VdbeMemRelease(&preupdate.aNew[i]); + } + sqlite3DbNNFreeNN(db, preupdate.aNew); + } + if( preupdate.apDflt ){ + int i; + for(i=0; i<pTab->nCol; i++){ + sqlite3ValueFree(preupdate.apDflt[i]); + } + sqlite3DbFree(db, preupdate.apDflt); + } +} +#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ + +#ifdef SQLITE_ENABLE_PERCENTILE +/* +** Return the name of an SQL function associated with the sqlite3_context. +*/ +const char *sqlite3VdbeFuncName(const sqlite3_context *pCtx){ + assert( pCtx!=0 ); + assert( pCtx->pFunc!=0 ); + return pCtx->pFunc->zName; +} +#endif /* SQLITE_ENABLE_PERCENTILE */ diff --git a/src/vdbeblob.c b/src/vdbeblob.c index 2ba5301e7a..a15fec6c48 100644 --- a/src/vdbeblob.c +++ b/src/vdbeblob.c @@ -23,13 +23,14 @@ */ typedef struct Incrblob Incrblob; struct Incrblob { - int flags; /* Copy of "flags" passed to sqlite3_blob_open() */ int nByte; /* Size of open blob, in bytes */ int iOffset; /* Byte offset of blob in cursor data */ - int iCol; /* Table column this handle is open on */ + u16 iCol; /* Table column this handle is open on */ BtCursor *pCsr; /* Cursor pointing at blob row */ sqlite3_stmt *pStmt; /* Statement holding cursor open */ sqlite3 *db; /* The associated database */ + char *zDb; /* Database name */ + Table *pTab; /* Table object */ }; @@ -55,17 +56,30 @@ static int blobSeekToRow(Incrblob *p, sqlite3_int64 iRow, char **pzErr){ char *zErr = 0; /* Error message */ Vdbe *v = (Vdbe *)p->pStmt; - /* Set the value of the SQL statements only variable to integer iRow. - ** This is done directly instead of using sqlite3_bind_int64() to avoid - ** triggering asserts related to mutexes. + /* Set the value of register r[1] in the SQL statement to integer iRow. + ** This is done directly as a performance optimization */ - assert( v->aVar[0].flags&MEM_Int ); - v->aVar[0].u.i = iRow; - - rc = sqlite3_step(p->pStmt); + sqlite3VdbeMemSetInt64(&v->aMem[1], iRow); + + /* If the statement has been run before (and is paused at the OP_ResultRow) + ** then back it up to the point where it does the OP_NotExists. This could + ** have been down with an extra OP_Goto, but simply setting the program + ** counter is faster. */ + if( v->pc>4 ){ + v->pc = 4; + assert( v->aOp[v->pc].opcode==OP_NotExists ); + rc = sqlite3VdbeExec(v); + }else{ + rc = sqlite3_step(p->pStmt); + } if( rc==SQLITE_ROW ){ VdbeCursor *pC = v->apCsr[0]; - u32 type = pC->aType[p->iCol]; + u32 type; + assert( pC!=0 ); + assert( pC->eCurType==CURTYPE_BTREE ); + type = pC->nHdrParsed>p->iCol ? pC->aType[p->iCol] : 0; + testcase( pC->nHdrParsed==p->iCol ); + testcase( pC->nHdrParsed==p->iCol+1 ); if( type<12 ){ zErr = sqlite3MPrintf(p->db, "cannot open value of type %s", type==0?"null": type==7?"real": "integer" @@ -110,7 +124,7 @@ int sqlite3_blob_open( const char *zTable, /* The table containing the blob */ const char *zColumn, /* The column containing the blob */ sqlite_int64 iRow, /* The row containing the glob */ - int flags, /* True -> read/write access, false -> read-only */ + int wrFlag, /* True -> read/write access, false -> read-only */ sqlite3_blob **ppBlob /* Handle for accessing the blob returned here */ ){ int nAttempt = 0; @@ -118,8 +132,9 @@ int sqlite3_blob_open( int rc = SQLITE_OK; char *zErr = 0; Table *pTab; - Parse *pParse = 0; Incrblob *pBlob = 0; + int iDb; + Parse sParse; #ifdef SQLITE_ENABLE_API_ARMOR if( ppBlob==0 ){ @@ -128,59 +143,61 @@ int sqlite3_blob_open( #endif *ppBlob = 0; #ifdef SQLITE_ENABLE_API_ARMOR - if( !sqlite3SafetyCheckOk(db) || zTable==0 ){ + if( !sqlite3SafetyCheckOk(db) || zTable==0 || zColumn==0 ){ return SQLITE_MISUSE_BKPT; } #endif - flags = !!flags; /* flags = (flags ? 1 : 0); */ + wrFlag = !!wrFlag; /* wrFlag = (wrFlag ? 1 : 0); */ sqlite3_mutex_enter(db->mutex); pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob)); - if( !pBlob ) goto blob_open_out; - pParse = sqlite3StackAllocRaw(db, sizeof(*pParse)); - if( !pParse ) goto blob_open_out; - - do { - memset(pParse, 0, sizeof(Parse)); - pParse->db = db; + while(1){ + sqlite3ParseObjectInit(&sParse,db); + if( !pBlob ) goto blob_open_out; sqlite3DbFree(db, zErr); zErr = 0; sqlite3BtreeEnterAll(db); - pTab = sqlite3LocateTable(pParse, 0, zTable, zDb); + pTab = sqlite3LocateTable(&sParse, 0, zTable, zDb); if( pTab && IsVirtual(pTab) ){ pTab = 0; - sqlite3ErrorMsg(pParse, "cannot open virtual table: %s", zTable); + sqlite3ErrorMsg(&sParse, "cannot open virtual table: %s", zTable); } if( pTab && !HasRowid(pTab) ){ pTab = 0; - sqlite3ErrorMsg(pParse, "cannot open table without rowid: %s", zTable); + sqlite3ErrorMsg(&sParse, "cannot open table without rowid: %s", zTable); + } + if( pTab && (pTab->tabFlags&TF_HasGenerated)!=0 ){ + pTab = 0; + sqlite3ErrorMsg(&sParse, "cannot open table with generated columns: %s", + zTable); } #ifndef SQLITE_OMIT_VIEW - if( pTab && pTab->pSelect ){ + if( pTab && IsView(pTab) ){ pTab = 0; - sqlite3ErrorMsg(pParse, "cannot open view: %s", zTable); + sqlite3ErrorMsg(&sParse, "cannot open view: %s", zTable); } #endif - if( !pTab ){ - if( pParse->zErrMsg ){ + if( pTab==0 + || ((iDb = sqlite3SchemaToIndex(db, pTab->pSchema))==1 && + sqlite3OpenTempDatabase(&sParse)) + ){ + if( sParse.zErrMsg ){ sqlite3DbFree(db, zErr); - zErr = pParse->zErrMsg; - pParse->zErrMsg = 0; + zErr = sParse.zErrMsg; + sParse.zErrMsg = 0; } rc = SQLITE_ERROR; sqlite3BtreeLeaveAll(db); goto blob_open_out; } + pBlob->pTab = pTab; + pBlob->zDb = db->aDb[iDb].zDbSName; /* Now search pTab for the exact column. */ - for(iCol=0; iCol<pTab->nCol; iCol++) { - if( sqlite3StrICmp(pTab->aCol[iCol].zName, zColumn)==0 ){ - break; - } - } - if( iCol==pTab->nCol ){ + iCol = sqlite3ColumnIndex(pTab, zColumn); + if( iCol<0 ){ sqlite3DbFree(db, zErr); zErr = sqlite3MPrintf(db, "no such column: \"%s\"", zColumn); rc = SQLITE_ERROR; @@ -190,9 +207,8 @@ int sqlite3_blob_open( /* If the value is being opened for writing, check that the ** column is not indexed, and that it is not part of a foreign key. - ** It is against the rules to open a column to which either of these - ** descriptions applies for writing. */ - if( flags ){ + */ + if( wrFlag ){ const char *zFault = 0; Index *pIdx; #ifndef SQLITE_OMIT_FOREIGN_KEY @@ -202,7 +218,8 @@ int sqlite3_blob_open( ** key columns must be indexed. The check below will pick up this ** case. */ FKey *pFKey; - for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){ + assert( IsOrdinaryTable(pTab) ); + for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){ int j; for(j=0; j<pFKey->nCol; j++){ if( pFKey->aCol[j].iFrom==iCol ){ @@ -230,7 +247,7 @@ int sqlite3_blob_open( } } - pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(pParse); + pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(&sParse); assert( pBlob->pStmt || db->mallocFailed ); if( pBlob->pStmt ){ @@ -253,22 +270,20 @@ int sqlite3_blob_open( static const VdbeOpList openBlob[] = { {OP_TableLock, 0, 0, 0}, /* 0: Acquire a read or write lock */ {OP_OpenRead, 0, 0, 0}, /* 1: Open a cursor */ - {OP_Variable, 1, 1, 0}, /* 2: Move ?1 into reg[1] */ - {OP_NotExists, 0, 7, 1}, /* 3: Seek the cursor */ - {OP_Column, 0, 0, 1}, /* 4 */ - {OP_ResultRow, 1, 0, 0}, /* 5 */ - {OP_Goto, 0, 2, 0}, /* 6 */ - {OP_Close, 0, 0, 0}, /* 7 */ - {OP_Halt, 0, 0, 0}, /* 8 */ + /* blobSeekToRow() will initialize r[1] to the desired rowid */ + {OP_NotExists, 0, 5, 1}, /* 2: Seek the cursor to rowid=r[1] */ + {OP_Column, 0, 0, 1}, /* 3 */ + {OP_ResultRow, 1, 0, 0}, /* 4 */ + {OP_Halt, 0, 0, 0}, /* 5 */ }; Vdbe *v = (Vdbe *)pBlob->pStmt; - int iDb = sqlite3SchemaToIndex(db, pTab->pSchema); VdbeOp *aOp; - sqlite3VdbeAddOp4Int(v, OP_Transaction, iDb, flags, + sqlite3VdbeAddOp4Int(v, OP_Transaction, iDb, wrFlag, pTab->pSchema->schema_cookie, pTab->pSchema->iGeneration); - sqlite3VdbeChangeP5(v, 1); + sqlite3VdbeChangeP5(v, 1); + assert( sqlite3VdbeCurrentAddr(v)==2 || db->mallocFailed ); aOp = sqlite3VdbeAddOpList(v, ArraySize(openBlob), openBlob, iLn); /* Make sure a mutex is held on the table to be accessed */ @@ -282,15 +297,15 @@ int sqlite3_blob_open( #else aOp[0].p1 = iDb; aOp[0].p2 = pTab->tnum; - aOp[0].p3 = flags; - sqlite3VdbeChangeP4(v, 1, pTab->zName, P4_TRANSIENT); + aOp[0].p3 = wrFlag; + sqlite3VdbeChangeP4(v, 2, pTab->zName, P4_TRANSIENT); } if( db->mallocFailed==0 ){ #endif /* Remove either the OP_OpenWrite or OpenRead. Set the P2 ** parameter of the other to pTab->tnum. */ - if( flags ) aOp[1].opcode = OP_OpenWrite; + if( wrFlag ) aOp[1].opcode = OP_OpenWrite; aOp[1].p2 = pTab->tnum; aOp[1].p3 = iDb; @@ -303,25 +318,25 @@ int sqlite3_blob_open( */ aOp[1].p4type = P4_INT32; aOp[1].p4.i = pTab->nCol+1; - aOp[4].p2 = pTab->nCol; + aOp[3].p2 = pTab->nCol; - pParse->nVar = 1; - pParse->nMem = 1; - pParse->nTab = 1; - sqlite3VdbeMakeReady(v, pParse); + sParse.nVar = 0; + sParse.nMem = 1; + sParse.nTab = 1; + sqlite3VdbeMakeReady(v, &sParse); } } - pBlob->flags = flags; pBlob->iCol = iCol; pBlob->db = db; sqlite3BtreeLeaveAll(db); if( db->mallocFailed ){ goto blob_open_out; } - sqlite3_bind_int64(pBlob->pStmt, 1, iRow); rc = blobSeekToRow(pBlob, iRow, &zErr); - } while( (++nAttempt)<SQLITE_MAX_SCHEMA_RETRY && rc==SQLITE_SCHEMA ); + if( (++nAttempt)>=SQLITE_MAX_SCHEMA_RETRY || rc!=SQLITE_SCHEMA ) break; + sqlite3ParseObjectReset(&sParse); + } blob_open_out: if( rc==SQLITE_OK && db->mallocFailed==0 ){ @@ -330,10 +345,9 @@ int sqlite3_blob_open( if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt); sqlite3DbFree(db, pBlob); } - sqlite3ErrorWithMsg(db, rc, (zErr ? "%s" : 0), zErr); + sqlite3ErrorWithMsg(db, rc, (zErr ? "%s" : (char*)0), zErr); sqlite3DbFree(db, zErr); - sqlite3ParserReset(pParse); - sqlite3StackFree(db, pParse); + sqlite3ParseObjectReset(&sParse); rc = sqlite3ApiExit(db, rc); sqlite3_mutex_leave(db->mutex); return rc; @@ -349,11 +363,12 @@ int sqlite3_blob_close(sqlite3_blob *pBlob){ sqlite3 *db; if( p ){ + sqlite3_stmt *pStmt = p->pStmt; db = p->db; sqlite3_mutex_enter(db->mutex); - rc = sqlite3_finalize(p->pStmt); sqlite3DbFree(db, p); sqlite3_mutex_leave(db->mutex); + rc = sqlite3_finalize(pStmt); }else{ rc = SQLITE_OK; } @@ -370,7 +385,7 @@ static int blobReadWrite( int iOffset, int (*xCall)(BtCursor*, u32, u32, void*) ){ - int rc; + int rc = SQLITE_OK; Incrblob *p = (Incrblob *)pBlob; Vdbe *v; sqlite3 *db; @@ -394,7 +409,48 @@ static int blobReadWrite( */ assert( db == v->db ); sqlite3BtreeEnterCursor(p->pCsr); + +#ifdef SQLITE_ENABLE_PREUPDATE_HOOK + if( xCall==sqlite3BtreePutData && db->xPreUpdateCallback ){ + /* If a pre-update hook is registered and this is a write cursor, + ** invoke it here. + ** + ** TODO: The preupdate-hook is passed SQLITE_DELETE, even though this + ** operation should really be an SQLITE_UPDATE. This is probably + ** incorrect, but is convenient because at this point the new.* values + ** are not easily obtainable. And for the sessions module, an + ** SQLITE_UPDATE where the PK columns do not change is handled in the + ** same way as an SQLITE_DELETE (the SQLITE_DELETE code is actually + ** slightly more efficient). Since you cannot write to a PK column + ** using the incremental-blob API, this works. For the sessions module + ** anyhow. + */ + if( sqlite3BtreeCursorIsValidNN(p->pCsr)==0 ){ + /* If the cursor is not currently valid, try to reseek it. This + ** always either fails or finds the correct row - the cursor will + ** have been marked permanently CURSOR_INVALID if the open row has + ** been deleted. */ + int bDiff = 0; + rc = sqlite3BtreeCursorRestore(p->pCsr, &bDiff); + assert( bDiff==0 || sqlite3BtreeCursorIsValidNN(p->pCsr)==0 ); + } + if( sqlite3BtreeCursorIsValidNN(p->pCsr) ){ + sqlite3_int64 iKey; + iKey = sqlite3BtreeIntegerKey(p->pCsr); + assert( v->apCsr[0]!=0 ); + assert( v->apCsr[0]->eCurType==CURTYPE_BTREE ); + sqlite3VdbePreUpdateHook( + v, v->apCsr[0], SQLITE_DELETE, p->zDb, p->pTab, iKey, -1, p->iCol + ); + } + } + if( rc==SQLITE_OK ){ + rc = xCall(p->pCsr, iOffset+p->iOffset, n, z); + } +#else rc = xCall(p->pCsr, iOffset+p->iOffset, n, z); +#endif + sqlite3BtreeLeaveCursor(p->pCsr); if( rc==SQLITE_ABORT ){ sqlite3VdbeFinalize(v); @@ -413,7 +469,7 @@ static int blobReadWrite( ** Read data from a blob handle. */ int sqlite3_blob_read(sqlite3_blob *pBlob, void *z, int n, int iOffset){ - return blobReadWrite(pBlob, z, n, iOffset, sqlite3BtreeData); + return blobReadWrite(pBlob, z, n, iOffset, sqlite3BtreePayloadChecked); } /* @@ -460,9 +516,10 @@ int sqlite3_blob_reopen(sqlite3_blob *pBlob, sqlite3_int64 iRow){ rc = SQLITE_ABORT; }else{ char *zErr; + ((Vdbe*)p->pStmt)->rc = SQLITE_OK; rc = blobSeekToRow(p, iRow, &zErr); if( rc!=SQLITE_OK ){ - sqlite3ErrorWithMsg(db, rc, (zErr ? "%s" : 0), zErr); + sqlite3ErrorWithMsg(db, rc, (zErr ? "%s" : (char*)0), zErr); sqlite3DbFree(db, zErr); } assert( rc!=SQLITE_SCHEMA ); diff --git a/src/vdbemem.c b/src/vdbemem.c index 6fb7cebbcc..2c4d1c5681 100644 --- a/src/vdbemem.c +++ b/src/vdbemem.c @@ -18,6 +18,11 @@ #include "sqliteInt.h" #include "vdbeInt.h" +/* True if X is a power of two. 0 is considered a power of two here. +** In other words, return true if X has at most one bit set. +*/ +#define ISPOWEROF2(X) (((X)&((X)-1))==0) + #ifdef SQLITE_DEBUG /* ** Check invariants on a Mem object. @@ -27,7 +32,7 @@ */ int sqlite3VdbeCheckMemInvariants(Mem *p){ /* If MEM_Dyn is set then Mem.xDel!=0. - ** Mem.xDel is might not be initialized if MEM_Dyn is clear. + ** Mem.xDel might not be initialized if MEM_Dyn is clear. */ assert( (p->flags & MEM_Dyn)==0 || p->xDel!=0 ); @@ -37,12 +42,42 @@ int sqlite3VdbeCheckMemInvariants(Mem *p){ ** That saves a few cycles in inner loops. */ assert( (p->flags & MEM_Dyn)==0 || p->szMalloc==0 ); - /* Cannot be both MEM_Int and MEM_Real at the same time */ - assert( (p->flags & (MEM_Int|MEM_Real))!=(MEM_Int|MEM_Real) ); + /* Cannot have more than one of MEM_Int, MEM_Real, or MEM_IntReal */ + assert( ISPOWEROF2(p->flags & (MEM_Int|MEM_Real|MEM_IntReal)) ); + + if( p->flags & MEM_Null ){ + /* Cannot be both MEM_Null and some other type */ + assert( (p->flags & (MEM_Int|MEM_Real|MEM_Str|MEM_Blob|MEM_Agg))==0 ); + + /* If MEM_Null is set, then either the value is a pure NULL (the usual + ** case) or it is a pointer set using sqlite3_bind_pointer() or + ** sqlite3_result_pointer(). If a pointer, then MEM_Term must also be + ** set. + */ + if( (p->flags & (MEM_Term|MEM_Subtype))==(MEM_Term|MEM_Subtype) ){ + /* This is a pointer type. There may be a flag to indicate what to + ** do with the pointer. */ + assert( ((p->flags&MEM_Dyn)!=0 ? 1 : 0) + + ((p->flags&MEM_Ephem)!=0 ? 1 : 0) + + ((p->flags&MEM_Static)!=0 ? 1 : 0) <= 1 ); + + /* No other bits set */ + assert( (p->flags & ~(MEM_Null|MEM_Term|MEM_Subtype|MEM_FromBind + |MEM_Dyn|MEM_Ephem|MEM_Static))==0 ); + }else{ + /* A pure NULL might have other flags, such as MEM_Static, MEM_Dyn, + ** MEM_Ephem, MEM_Cleared, or MEM_Subtype */ + } + }else{ + /* The MEM_Cleared bit is only allowed on NULLs */ + assert( (p->flags & MEM_Cleared)==0 ); + } /* The szMalloc field holds the correct memory allocation size */ assert( p->szMalloc==0 - || p->szMalloc==sqlite3DbMallocSize(p->db,p->zMalloc) ); + || (p->flags==MEM_Undefined + && p->szMalloc<=sqlite3DbMallocSize(p->db,p->zMalloc)) + || p->szMalloc==sqlite3DbMallocSize(p->db,p->zMalloc)); /* If p holds a string or blob, the Mem.z must point to exactly ** one of the following: @@ -64,6 +99,93 @@ int sqlite3VdbeCheckMemInvariants(Mem *p){ } #endif +/* +** Render a Mem object which is one of MEM_Int, MEM_Real, or MEM_IntReal +** into a buffer. +*/ +static void vdbeMemRenderNum(int sz, char *zBuf, Mem *p){ + StrAccum acc; + assert( p->flags & (MEM_Int|MEM_Real|MEM_IntReal) ); + assert( sz>22 ); + if( p->flags & MEM_Int ){ +#if GCC_VERSION>=7000000 + /* Work-around for GCC bug + ** https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96270 */ + i64 x; + assert( (p->flags&MEM_Int)*2==sizeof(x) ); + memcpy(&x, (char*)&p->u, (p->flags&MEM_Int)*2); + p->n = sqlite3Int64ToText(x, zBuf); +#else + p->n = sqlite3Int64ToText(p->u.i, zBuf); +#endif + }else{ + sqlite3StrAccumInit(&acc, 0, zBuf, sz, 0); + sqlite3_str_appendf(&acc, "%!.15g", + (p->flags & MEM_IntReal)!=0 ? (double)p->u.i : p->u.r); + assert( acc.zText==zBuf && acc.mxAlloc<=0 ); + zBuf[acc.nChar] = 0; /* Fast version of sqlite3StrAccumFinish(&acc) */ + p->n = acc.nChar; + } +} + +#ifdef SQLITE_DEBUG +/* +** Validity checks on pMem. pMem holds a string. +** +** (1) Check that string value of pMem agrees with its integer or real value. +** (2) Check that the string is correctly zero terminated +** +** A single int or real value always converts to the same strings. But +** many different strings can be converted into the same int or real. +** If a table contains a numeric value and an index is based on the +** corresponding string value, then it is important that the string be +** derived from the numeric value, not the other way around, to ensure +** that the index and table are consistent. See ticket +** https://sqlite.org/src/info/343634942dd54ab (2018-01-31) for +** an example. +** +** This routine looks at pMem to verify that if it has both a numeric +** representation and a string representation then the string rep has +** been derived from the numeric and not the other way around. It returns +** true if everything is ok and false if there is a problem. +** +** This routine is for use inside of assert() statements only. +*/ +int sqlite3VdbeMemValidStrRep(Mem *p){ + Mem tmp; + char zBuf[100]; + char *z; + int i, j, incr; + if( (p->flags & MEM_Str)==0 ) return 1; + if( p->db && p->db->mallocFailed ) return 1; + if( p->flags & MEM_Term ){ + /* Insure that the string is properly zero-terminated. Pay particular + ** attention to the case where p->n is odd */ + if( p->szMalloc>0 && p->z==p->zMalloc ){ + assert( p->enc==SQLITE_UTF8 || p->szMalloc >= ((p->n+1)&~1)+2 ); + assert( p->enc!=SQLITE_UTF8 || p->szMalloc >= p->n+1 ); + } + assert( p->z[p->n]==0 ); + assert( p->enc==SQLITE_UTF8 || p->z[(p->n+1)&~1]==0 ); + assert( p->enc==SQLITE_UTF8 || p->z[((p->n+1)&~1)+1]==0 ); + } + if( (p->flags & (MEM_Int|MEM_Real|MEM_IntReal))==0 ) return 1; + memcpy(&tmp, p, sizeof(tmp)); + vdbeMemRenderNum(sizeof(zBuf), zBuf, &tmp); + z = p->z; + i = j = 0; + incr = 1; + if( p->enc!=SQLITE_UTF8 ){ + incr = 2; + if( p->enc==SQLITE_UTF16BE ) z++; + } + while( zBuf[j] ){ + if( zBuf[j++]!=z[i] ) return 0; + i += incr; + } + return 1; +} +#endif /* SQLITE_DEBUG */ /* ** If pMem is an object with a valid string representation, this routine @@ -82,10 +204,15 @@ int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){ #ifndef SQLITE_OMIT_UTF16 int rc; #endif - assert( (pMem->flags&MEM_RowSet)==0 ); + assert( pMem!=0 ); + assert( !sqlite3VdbeMemIsRowSet(pMem) ); assert( desiredEnc==SQLITE_UTF8 || desiredEnc==SQLITE_UTF16LE || desiredEnc==SQLITE_UTF16BE ); - if( !(pMem->flags&MEM_Str) || pMem->enc==desiredEnc ){ + if( !(pMem->flags&MEM_Str) ){ + pMem->enc = desiredEnc; + return SQLITE_OK; + } + if( pMem->enc==desiredEnc ){ return SQLITE_OK; } assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); @@ -105,8 +232,7 @@ int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){ } /* -** Make sure pMem->z points to a writable allocation of at least -** min(n,32) bytes. +** Make sure pMem->z points to a writable allocation of at least n bytes. ** ** If the bPreserve argument is true, then copy of the content of ** pMem->z into the new allocation. pMem must be either a string or @@ -115,7 +241,7 @@ int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){ */ SQLITE_NOINLINE int sqlite3VdbeMemGrow(Mem *pMem, int n, int bPreserve){ assert( sqlite3VdbeCheckMemInvariants(pMem) ); - assert( (pMem->flags&MEM_RowSet)==0 ); + assert( !sqlite3VdbeMemIsRowSet(pMem) ); testcase( pMem->db==0 ); /* If the bPreserve flag is set to true, then the memory cell must already @@ -124,27 +250,33 @@ SQLITE_NOINLINE int sqlite3VdbeMemGrow(Mem *pMem, int n, int bPreserve){ testcase( bPreserve && pMem->z==0 ); assert( pMem->szMalloc==0 - || pMem->szMalloc==sqlite3DbMallocSize(pMem->db, pMem->zMalloc) ); - if( pMem->szMalloc<n ){ - if( n<32 ) n = 32; - if( bPreserve && pMem->szMalloc>0 && pMem->z==pMem->zMalloc ){ + || (pMem->flags==MEM_Undefined + && pMem->szMalloc<=sqlite3DbMallocSize(pMem->db,pMem->zMalloc)) + || pMem->szMalloc==sqlite3DbMallocSize(pMem->db,pMem->zMalloc)); + if( pMem->szMalloc>0 && bPreserve && pMem->z==pMem->zMalloc ){ + if( pMem->db ){ pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n); - bPreserve = 0; }else{ - if( pMem->szMalloc>0 ) sqlite3DbFree(pMem->db, pMem->zMalloc); - pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n); - } - if( pMem->zMalloc==0 ){ - sqlite3VdbeMemSetNull(pMem); - pMem->z = 0; - pMem->szMalloc = 0; - return SQLITE_NOMEM; - }else{ - pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc); + pMem->zMalloc = sqlite3Realloc(pMem->z, n); + if( pMem->zMalloc==0 ) sqlite3_free(pMem->z); + pMem->z = pMem->zMalloc; } + bPreserve = 0; + }else{ + if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc); + pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n); + } + if( pMem->zMalloc==0 ){ + sqlite3VdbeMemSetNull(pMem); + pMem->z = 0; + pMem->szMalloc = 0; + return SQLITE_NOMEM_BKPT; + }else{ + pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc); } - if( bPreserve && pMem->z && pMem->z!=pMem->zMalloc ){ + if( bPreserve && pMem->z ){ + assert( pMem->z!=pMem->zMalloc ); memcpy(pMem->zMalloc, pMem->z, pMem->n); } if( (pMem->flags&MEM_Dyn)!=0 ){ @@ -164,21 +296,75 @@ SQLITE_NOINLINE int sqlite3VdbeMemGrow(Mem *pMem, int n, int bPreserve){ ** ** Any prior string or blob content in the pMem object may be discarded. ** The pMem->xDel destructor is called, if it exists. Though MEM_Str -** and MEM_Blob values may be discarded, MEM_Int, MEM_Real, and MEM_Null -** values are preserved. +** and MEM_Blob values may be discarded, MEM_Int, MEM_Real, MEM_IntReal, +** and MEM_Null values are preserved. ** ** Return SQLITE_OK on success or an error code (probably SQLITE_NOMEM) ** if unable to complete the resizing. */ int sqlite3VdbeMemClearAndResize(Mem *pMem, int szNew){ - assert( szNew>0 ); + assert( CORRUPT_DB || szNew>0 ); assert( (pMem->flags & MEM_Dyn)==0 || pMem->szMalloc==0 ); if( pMem->szMalloc<szNew ){ return sqlite3VdbeMemGrow(pMem, szNew, 0); } assert( (pMem->flags & MEM_Dyn)==0 ); pMem->z = pMem->zMalloc; - pMem->flags &= (MEM_Null|MEM_Int|MEM_Real); + pMem->flags &= (MEM_Null|MEM_Int|MEM_Real|MEM_IntReal); + return SQLITE_OK; +} + +/* +** If pMem is already a string, detect if it is a zero-terminated +** string, or make it into one if possible, and mark it as such. +** +** This is an optimization. Correct operation continues even if +** this routine is a no-op. +*/ +void sqlite3VdbeMemZeroTerminateIfAble(Mem *pMem){ + if( (pMem->flags & (MEM_Str|MEM_Term|MEM_Ephem|MEM_Static))!=MEM_Str ){ + /* pMem must be a string, and it cannot be an ephemeral or static string */ + return; + } + if( pMem->enc!=SQLITE_UTF8 ) return; + assert( pMem->z!=0 ); + if( pMem->flags & MEM_Dyn ){ + if( pMem->xDel==sqlite3_free + && sqlite3_msize(pMem->z) >= (u64)(pMem->n+1) + ){ + pMem->z[pMem->n] = 0; + pMem->flags |= MEM_Term; + return; + } + if( pMem->xDel==sqlite3RCStrUnref ){ + /* Blindly assume that all RCStr objects are zero-terminated */ + pMem->flags |= MEM_Term; + return; + } + }else if( pMem->szMalloc >= pMem->n+1 ){ + pMem->z[pMem->n] = 0; + pMem->flags |= MEM_Term; + return; + } +} + +/* +** It is already known that pMem contains an unterminated string. +** Add the zero terminator. +** +** Three bytes of zero are added. In this way, there is guaranteed +** to be a double-zero byte at an even byte boundary in order to +** terminate a UTF16 string, even if the initial size of the buffer +** is an odd number of bytes. +*/ +static SQLITE_NOINLINE int vdbeMemAddTerminator(Mem *pMem){ + if( sqlite3VdbeMemGrow(pMem, pMem->n+3, 1) ){ + return SQLITE_NOMEM_BKPT; + } + pMem->z[pMem->n] = 0; + pMem->z[pMem->n+1] = 0; + pMem->z[pMem->n+2] = 0; + pMem->flags |= MEM_Term; return SQLITE_OK; } @@ -189,18 +375,15 @@ int sqlite3VdbeMemClearAndResize(Mem *pMem, int szNew){ ** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails. */ int sqlite3VdbeMemMakeWriteable(Mem *pMem){ - int f; + assert( pMem!=0 ); assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); - assert( (pMem->flags&MEM_RowSet)==0 ); - ExpandBlob(pMem); - f = pMem->flags; - if( (f&(MEM_Str|MEM_Blob)) && (pMem->szMalloc==0 || pMem->z!=pMem->zMalloc) ){ - if( sqlite3VdbeMemGrow(pMem, pMem->n + 2, 1) ){ - return SQLITE_NOMEM; + assert( !sqlite3VdbeMemIsRowSet(pMem) ); + if( (pMem->flags & (MEM_Str|MEM_Blob))!=0 ){ + if( ExpandBlob(pMem) ) return SQLITE_NOMEM; + if( pMem->szMalloc==0 || pMem->z!=pMem->zMalloc ){ + int rc = vdbeMemAddTerminator(pMem); + if( rc ) return rc; } - pMem->z[pMem->n] = 0; - pMem->z[pMem->n+1] = 0; - pMem->flags |= MEM_Term; } pMem->flags &= ~MEM_Ephem; #ifdef SQLITE_DEBUG @@ -216,47 +399,38 @@ int sqlite3VdbeMemMakeWriteable(Mem *pMem){ */ #ifndef SQLITE_OMIT_INCRBLOB int sqlite3VdbeMemExpandBlob(Mem *pMem){ - if( pMem->flags & MEM_Zero ){ - int nByte; - assert( pMem->flags&MEM_Blob ); - assert( (pMem->flags&MEM_RowSet)==0 ); - assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); - - /* Set nByte to the number of bytes required to store the expanded blob. */ - nByte = pMem->n + pMem->u.nZero; - if( nByte<=0 ){ - nByte = 1; - } - if( sqlite3VdbeMemGrow(pMem, nByte, 1) ){ - return SQLITE_NOMEM; - } + int nByte; + assert( pMem!=0 ); + assert( pMem->flags & MEM_Zero ); + assert( (pMem->flags&MEM_Blob)!=0 || MemNullNochng(pMem) ); + testcase( sqlite3_value_nochange(pMem) ); + assert( !sqlite3VdbeMemIsRowSet(pMem) ); + assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); - memset(&pMem->z[pMem->n], 0, pMem->u.nZero); - pMem->n += pMem->u.nZero; - pMem->flags &= ~(MEM_Zero|MEM_Term); + /* Set nByte to the number of bytes required to store the expanded blob. */ + nByte = pMem->n + pMem->u.nZero; + if( nByte<=0 ){ + if( (pMem->flags & MEM_Blob)==0 ) return SQLITE_OK; + nByte = 1; } - return SQLITE_OK; -} -#endif - -/* -** It is already known that pMem contains an unterminated string. -** Add the zero terminator. -*/ -static SQLITE_NOINLINE int vdbeMemAddTerminator(Mem *pMem){ - if( sqlite3VdbeMemGrow(pMem, pMem->n+2, 1) ){ - return SQLITE_NOMEM; + if( sqlite3VdbeMemGrow(pMem, nByte, 1) ){ + return SQLITE_NOMEM_BKPT; } - pMem->z[pMem->n] = 0; - pMem->z[pMem->n+1] = 0; - pMem->flags |= MEM_Term; + assert( pMem->z!=0 ); + assert( sqlite3DbMallocSize(pMem->db,pMem->z) >= nByte ); + + memset(&pMem->z[pMem->n], 0, pMem->u.nZero); + pMem->n += pMem->u.nZero; + pMem->flags &= ~(MEM_Zero|MEM_Term); return SQLITE_OK; } +#endif /* ** Make sure the given Mem is \u0000 terminated. */ int sqlite3VdbeMemNulTerminate(Mem *pMem){ + assert( pMem!=0 ); assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); testcase( (pMem->flags & (MEM_Term|MEM_Str))==(MEM_Term|MEM_Str) ); testcase( (pMem->flags & (MEM_Term|MEM_Str))==0 ); @@ -268,12 +442,12 @@ int sqlite3VdbeMemNulTerminate(Mem *pMem){ } /* -** Add MEM_Str to the set of representations for the given Mem. Numbers -** are converted using sqlite3_snprintf(). Converting a BLOB to a string -** is a no-op. +** Add MEM_Str to the set of representations for the given Mem. This +** routine is only called if pMem is a number of some kind, not a NULL +** or a BLOB. ** -** Existing representations MEM_Int and MEM_Real are invalidated if -** bForce is true but are retained if bForce is false. +** Existing representations MEM_Int, MEM_Real, or MEM_IntReal are invalidated +** if bForce is true but are retained if bForce is false. ** ** A MEM_Null value will never be passed to this function. This function is ** used for converting values to text for returning to the user (i.e. via @@ -282,37 +456,28 @@ int sqlite3VdbeMemNulTerminate(Mem *pMem){ ** user and the latter is an internal programming error. */ int sqlite3VdbeMemStringify(Mem *pMem, u8 enc, u8 bForce){ - int fg = pMem->flags; const int nByte = 32; + assert( pMem!=0 ); assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); - assert( !(fg&MEM_Zero) ); - assert( !(fg&(MEM_Str|MEM_Blob)) ); - assert( fg&(MEM_Int|MEM_Real) ); - assert( (pMem->flags&MEM_RowSet)==0 ); + assert( !(pMem->flags&MEM_Zero) ); + assert( !(pMem->flags&(MEM_Str|MEM_Blob)) ); + assert( pMem->flags&(MEM_Int|MEM_Real|MEM_IntReal) ); + assert( !sqlite3VdbeMemIsRowSet(pMem) ); assert( EIGHT_BYTE_ALIGNMENT(pMem) ); if( sqlite3VdbeMemClearAndResize(pMem, nByte) ){ - return SQLITE_NOMEM; + pMem->enc = 0; + return SQLITE_NOMEM_BKPT; } - /* For a Real or Integer, use sqlite3_snprintf() to produce the UTF-8 - ** string representation of the value. Then, if the required encoding - ** is UTF-16le or UTF-16be do a translation. - ** - ** FIX ME: It would be better if sqlite3_snprintf() could do UTF-16. - */ - if( fg & MEM_Int ){ - sqlite3_snprintf(nByte, pMem->z, "%lld", pMem->u.i); - }else{ - assert( fg & MEM_Real ); - sqlite3_snprintf(nByte, pMem->z, "%!.15g", pMem->u.r); - } - pMem->n = sqlite3Strlen30(pMem->z); + vdbeMemRenderNum(nByte, pMem->z, pMem); + assert( pMem->z!=0 ); + assert( pMem->n==(int)sqlite3Strlen30NN(pMem->z) ); pMem->enc = SQLITE_UTF8; pMem->flags |= MEM_Str|MEM_Term; - if( bForce ) pMem->flags &= ~(MEM_Int|MEM_Real); + if( bForce ) pMem->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal); sqlite3VdbeChangeEncoding(pMem, enc); return SQLITE_OK; } @@ -326,27 +491,55 @@ int sqlite3VdbeMemStringify(Mem *pMem, u8 enc, u8 bForce){ ** otherwise. */ int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){ - int rc = SQLITE_OK; - if( ALWAYS(pFunc && pFunc->xFinalize) ){ - sqlite3_context ctx; - Mem t; - assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef ); - assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); - memset(&ctx, 0, sizeof(ctx)); - memset(&t, 0, sizeof(t)); - t.flags = MEM_Null; - t.db = pMem->db; - ctx.pOut = &t; - ctx.pMem = pMem; - ctx.pFunc = pFunc; - pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */ - assert( (pMem->flags & MEM_Dyn)==0 ); - if( pMem->szMalloc>0 ) sqlite3DbFree(pMem->db, pMem->zMalloc); - memcpy(pMem, &t, sizeof(t)); - rc = ctx.isError; - } - return rc; + sqlite3_context ctx; + Mem t; + assert( pFunc!=0 ); + assert( pMem!=0 ); + assert( pMem->db!=0 ); + assert( pFunc->xFinalize!=0 ); + assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef ); + assert( sqlite3_mutex_held(pMem->db->mutex) ); + memset(&ctx, 0, sizeof(ctx)); + memset(&t, 0, sizeof(t)); + t.flags = MEM_Null; + t.db = pMem->db; + ctx.pOut = &t; + ctx.pMem = pMem; + ctx.pFunc = pFunc; + ctx.enc = ENC(t.db); + pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */ + assert( (pMem->flags & MEM_Dyn)==0 ); + if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc); + memcpy(pMem, &t, sizeof(t)); + return ctx.isError; +} + +/* +** Memory cell pAccum contains the context of an aggregate function. +** This routine calls the xValue method for that function and stores +** the results in memory cell pMem. +** +** SQLITE_ERROR is returned if xValue() reports an error. SQLITE_OK +** otherwise. +*/ +#ifndef SQLITE_OMIT_WINDOWFUNC +int sqlite3VdbeMemAggValue(Mem *pAccum, Mem *pOut, FuncDef *pFunc){ + sqlite3_context ctx; + assert( pFunc!=0 ); + assert( pFunc->xValue!=0 ); + assert( (pAccum->flags & MEM_Null)!=0 || pFunc==pAccum->u.pDef ); + assert( pAccum->db!=0 ); + assert( sqlite3_mutex_held(pAccum->db->mutex) ); + memset(&ctx, 0, sizeof(ctx)); + sqlite3VdbeMemSetNull(pOut); + ctx.pOut = pOut; + ctx.pMem = pAccum; + ctx.pFunc = pFunc; + ctx.enc = ENC(pAccum->db); + pFunc->xValue(&ctx); + return ctx.isError; } +#endif /* SQLITE_OMIT_WINDOWFUNC */ /* ** If the memory cell contains a value that must be freed by @@ -366,15 +559,8 @@ static SQLITE_NOINLINE void vdbeMemClearExternAndSetNull(Mem *p){ testcase( p->flags & MEM_Dyn ); } if( p->flags&MEM_Dyn ){ - assert( (p->flags&MEM_RowSet)==0 ); assert( p->xDel!=SQLITE_DYNAMIC && p->xDel!=0 ); p->xDel((void *)p->z); - }else if( p->flags&MEM_RowSet ){ - sqlite3RowSetClear(p->u.pRowSet); - }else if( p->flags&MEM_Frame ){ - VdbeFrame *pFrame = p->u.pFrame; - pFrame->pParent = pFrame->v->pDelFrame; - pFrame->v->pDelFrame = pFrame; } p->flags = MEM_Null; } @@ -392,7 +578,7 @@ static SQLITE_NOINLINE void vdbeMemClear(Mem *p){ vdbeMemClearExternAndSetNull(p); } if( p->szMalloc ){ - sqlite3DbFree(p->db, p->zMalloc); + sqlite3DbFreeNN(p->db, p->zMalloc); p->szMalloc = 0; } p->z = 0; @@ -415,34 +601,12 @@ void sqlite3VdbeMemRelease(Mem *p){ } } -/* -** Convert a 64-bit IEEE double into a 64-bit signed integer. -** If the double is out of range of a 64-bit signed integer then -** return the closest available 64-bit signed integer. -*/ -static i64 doubleToInt64(double r){ -#ifdef SQLITE_OMIT_FLOATING_POINT - /* When floating-point is omitted, double and int64 are the same thing */ - return r; -#else - /* - ** Many compilers we encounter do not define constants for the - ** minimum and maximum 64-bit integers, or they define them - ** inconsistently. And many do not understand the "LL" notation. - ** So we define our own static constants here using nothing - ** larger than a 32-bit integer constant. - */ - static const i64 maxInt = LARGEST_INT64; - static const i64 minInt = SMALLEST_INT64; - - if( r<=(double)minInt ){ - return minInt; - }else if( r>=(double)maxInt ){ - return maxInt; - }else{ - return (i64)r; - } -#endif +/* Like sqlite3VdbeMemRelease() but faster for cases where we +** know in advance that the Mem is not MEM_Dyn or MEM_Agg. +*/ +void sqlite3VdbeMemReleaseMalloc(Mem *p){ + assert( !VdbeMemDynamic(p) ); + if( p->szMalloc ) vdbeMemClear(p); } /* @@ -456,20 +620,24 @@ static i64 doubleToInt64(double r){ ** ** If pMem represents a string value, its encoding might be changed. */ -i64 sqlite3VdbeIntValue(Mem *pMem){ +static SQLITE_NOINLINE i64 memIntValue(const Mem *pMem){ + i64 value = 0; + sqlite3Atoi64(pMem->z, &value, pMem->n, pMem->enc); + return value; +} +i64 sqlite3VdbeIntValue(const Mem *pMem){ int flags; + assert( pMem!=0 ); assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); assert( EIGHT_BYTE_ALIGNMENT(pMem) ); flags = pMem->flags; - if( flags & MEM_Int ){ + if( flags & (MEM_Int|MEM_IntReal) ){ + testcase( flags & MEM_IntReal ); return pMem->u.i; }else if( flags & MEM_Real ){ - return doubleToInt64(pMem->u.r); - }else if( flags & (MEM_Str|MEM_Blob) ){ - i64 value = 0; - assert( pMem->z || pMem->n==0 ); - sqlite3Atoi64(pMem->z, &value, pMem->n, pMem->enc); - return value; + return sqlite3RealToI64(pMem->u.r); + }else if( (flags & (MEM_Str|MEM_Blob))!=0 && pMem->z!=0 ){ + return memIntValue(pMem); }else{ return 0; } @@ -481,18 +649,23 @@ i64 sqlite3VdbeIntValue(Mem *pMem){ ** value. If it is a string or blob, try to convert it to a double. ** If it is a NULL, return 0.0. */ +static SQLITE_NOINLINE double memRealValue(Mem *pMem){ + /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */ + double val = (double)0; + sqlite3AtoF(pMem->z, &val, pMem->n, pMem->enc); + return val; +} double sqlite3VdbeRealValue(Mem *pMem){ + assert( pMem!=0 ); assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); assert( EIGHT_BYTE_ALIGNMENT(pMem) ); if( pMem->flags & MEM_Real ){ return pMem->u.r; - }else if( pMem->flags & MEM_Int ){ + }else if( pMem->flags & (MEM_Int|MEM_IntReal) ){ + testcase( pMem->flags & MEM_IntReal ); return (double)pMem->u.i; }else if( pMem->flags & (MEM_Str|MEM_Blob) ){ - /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */ - double val = (double)0; - sqlite3AtoF(pMem->z, &val, pMem->n, pMem->enc); - return val; + return memRealValue(pMem); }else{ /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */ return (double)0; @@ -500,31 +673,46 @@ double sqlite3VdbeRealValue(Mem *pMem){ } /* -** The MEM structure is already a MEM_Real. Try to also make it a -** MEM_Int if we can. +** Return 1 if pMem represents true, and return 0 if pMem represents false. +** Return the value ifNull if pMem is NULL. +*/ +int sqlite3VdbeBooleanValue(Mem *pMem, int ifNull){ + testcase( pMem->flags & MEM_IntReal ); + if( pMem->flags & (MEM_Int|MEM_IntReal) ) return pMem->u.i!=0; + if( pMem->flags & MEM_Null ) return ifNull; + return sqlite3VdbeRealValue(pMem)!=0.0; +} + +/* +** The MEM structure is already a MEM_Real or MEM_IntReal. Try to +** make it a MEM_Int if we can. */ void sqlite3VdbeIntegerAffinity(Mem *pMem){ - i64 ix; - assert( pMem->flags & MEM_Real ); - assert( (pMem->flags & MEM_RowSet)==0 ); + assert( pMem!=0 ); + assert( pMem->flags & (MEM_Real|MEM_IntReal) ); + assert( !sqlite3VdbeMemIsRowSet(pMem) ); assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); assert( EIGHT_BYTE_ALIGNMENT(pMem) ); - ix = doubleToInt64(pMem->u.r); - - /* Only mark the value as an integer if - ** - ** (1) the round-trip conversion real->int->real is a no-op, and - ** (2) The integer is neither the largest nor the smallest - ** possible integer (ticket #3922) - ** - ** The second and third terms in the following conditional enforces - ** the second condition under the assumption that addition overflow causes - ** values to wrap around. - */ - if( pMem->u.r==ix && ix>SMALLEST_INT64 && ix<LARGEST_INT64 ){ - pMem->u.i = ix; + if( pMem->flags & MEM_IntReal ){ MemSetTypeFlag(pMem, MEM_Int); + }else{ + i64 ix = sqlite3RealToI64(pMem->u.r); + + /* Only mark the value as an integer if + ** + ** (1) the round-trip conversion real->int->real is a no-op, and + ** (2) The integer is neither the largest nor the smallest + ** possible integer (ticket #3922) + ** + ** The second and third terms in the following conditional enforces + ** the second condition under the assumption that addition overflow causes + ** values to wrap around. + */ + if( pMem->u.r==ix && ix>SMALLEST_INT64 && ix<LARGEST_INT64 ){ + pMem->u.i = ix; + MemSetTypeFlag(pMem, MEM_Int); + } } } @@ -532,8 +720,9 @@ void sqlite3VdbeIntegerAffinity(Mem *pMem){ ** Convert pMem to type integer. Invalidate any prior representations. */ int sqlite3VdbeMemIntegerify(Mem *pMem){ + assert( pMem!=0 ); assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); - assert( (pMem->flags & MEM_RowSet)==0 ); + assert( !sqlite3VdbeMemIsRowSet(pMem) ); assert( EIGHT_BYTE_ALIGNMENT(pMem) ); pMem->u.i = sqlite3VdbeIntValue(pMem); @@ -546,6 +735,7 @@ int sqlite3VdbeMemIntegerify(Mem *pMem){ ** Invalidate any prior representations. */ int sqlite3VdbeMemRealify(Mem *pMem){ + assert( pMem!=0 ); assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); assert( EIGHT_BYTE_ALIGNMENT(pMem) ); @@ -554,8 +744,34 @@ int sqlite3VdbeMemRealify(Mem *pMem){ return SQLITE_OK; } +/* Compare a floating point value to an integer. Return true if the two +** values are the same within the precision of the floating point value. +** +** This function assumes that i was obtained by assignment from r1. +** +** For some versions of GCC on 32-bit machines, if you do the more obvious +** comparison of "r1==(double)i" you sometimes get an answer of false even +** though the r1 and (double)i values are bit-for-bit the same. +*/ +int sqlite3RealSameAsInt(double r1, sqlite3_int64 i){ + double r2 = (double)i; + return r1==0.0 + || (memcmp(&r1, &r2, sizeof(r1))==0 + && i >= -2251799813685248LL && i < 2251799813685248LL); +} + +/* Convert a floating point value to its closest integer. Do so in +** a way that avoids 'outside the range of representable values' warnings +** from UBSAN. +*/ +i64 sqlite3RealToI64(double r){ + if( r<-9223372036854774784.0 ) return SMALLEST_INT64; + if( r>+9223372036854774784.0 ) return LARGEST_INT64; + return (i64)r; +} + /* -** Convert pMem so that it has types MEM_Real or MEM_Int or both. +** Convert pMem so that it has type MEM_Real or MEM_Int. ** Invalidate any prior representations. ** ** Every effort is made to force the conversion, even if the input @@ -563,19 +779,28 @@ int sqlite3VdbeMemRealify(Mem *pMem){ ** as much of the string as we can and ignore the rest. */ int sqlite3VdbeMemNumerify(Mem *pMem){ - if( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))==0 ){ + assert( pMem!=0 ); + testcase( pMem->flags & MEM_Int ); + testcase( pMem->flags & MEM_Real ); + testcase( pMem->flags & MEM_IntReal ); + testcase( pMem->flags & MEM_Null ); + if( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null))==0 ){ + int rc; + sqlite3_int64 ix; assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 ); assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); - if( 0==sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc) ){ + rc = sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc); + if( ((rc==0 || rc==1) && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)<=1) + || sqlite3RealSameAsInt(pMem->u.r, (ix = sqlite3RealToI64(pMem->u.r))) + ){ + pMem->u.i = ix; MemSetTypeFlag(pMem, MEM_Int); }else{ - pMem->u.r = sqlite3VdbeRealValue(pMem); MemSetTypeFlag(pMem, MEM_Real); - sqlite3VdbeIntegerAffinity(pMem); } } - assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))!=0 ); - pMem->flags &= ~(MEM_Str|MEM_Blob); + assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null))!=0 ); + pMem->flags &= ~(MEM_Str|MEM_Blob|MEM_Zero); return SQLITE_OK; } @@ -586,14 +811,14 @@ int sqlite3VdbeMemNumerify(Mem *pMem){ ** affinity even if that results in loss of data. This routine is ** used (for example) to implement the SQL "cast()" operator. */ -void sqlite3VdbeMemCast(Mem *pMem, u8 aff, u8 encoding){ - if( pMem->flags & MEM_Null ) return; +int sqlite3VdbeMemCast(Mem *pMem, u8 aff, u8 encoding){ + if( pMem->flags & MEM_Null ) return SQLITE_OK; switch( aff ){ case SQLITE_AFF_BLOB: { /* Really a cast to BLOB */ if( (pMem->flags & MEM_Blob)==0 ){ sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT, encoding); assert( pMem->flags & MEM_Str || pMem->db->mallocFailed ); - MemSetTypeFlag(pMem, MEM_Blob); + if( pMem->flags & MEM_Str ) MemSetTypeFlag(pMem, MEM_Blob); }else{ pMem->flags &= ~(MEM_TypeMask&~MEM_Blob); } @@ -612,15 +837,20 @@ void sqlite3VdbeMemCast(Mem *pMem, u8 aff, u8 encoding){ break; } default: { + int rc; assert( aff==SQLITE_AFF_TEXT ); assert( MEM_Str==(MEM_Blob>>3) ); pMem->flags |= (pMem->flags&MEM_Blob)>>3; sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT, encoding); assert( pMem->flags & MEM_Str || pMem->db->mallocFailed ); - pMem->flags &= ~(MEM_Int|MEM_Real|MEM_Blob|MEM_Zero); - break; + pMem->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal|MEM_Blob|MEM_Zero); + if( encoding!=SQLITE_UTF8 ) pMem->n &= ~1; + rc = sqlite3VdbeChangeEncoding(pMem, encoding); + if( rc ) return rc; + sqlite3VdbeMemZeroTerminateIfAble(pMem); } } + return SQLITE_OK; } /* @@ -663,6 +893,7 @@ void sqlite3ValueSetNull(sqlite3_value *p){ ** Delete any previous value and set the value to be a BLOB of length ** n containing all zeros. */ +#ifndef SQLITE_OMIT_INCRBLOB void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){ sqlite3VdbeMemRelease(pMem); pMem->flags = MEM_Blob|MEM_Zero; @@ -672,6 +903,21 @@ void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){ pMem->enc = SQLITE_UTF8; pMem->z = 0; } +#else +int sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){ + int nByte = n>0?n:1; + if( sqlite3VdbeMemGrow(pMem, nByte, 0) ){ + return SQLITE_NOMEM_BKPT; + } + assert( pMem->z!=0 ); + assert( sqlite3DbMallocSize(pMem->db, pMem->z)>=nByte ); + memset(pMem->z, 0, nByte); + pMem->n = n>0?n:0; + pMem->flags = MEM_Blob; + pMem->enc = SQLITE_UTF8; + return SQLITE_OK; +} +#endif /* ** The pMem is known to contain content that needs to be destroyed prior @@ -697,6 +943,35 @@ void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){ } } +/* +** Set the iIdx'th entry of array aMem[] to contain integer value val. +*/ +void sqlite3MemSetArrayInt64(sqlite3_value *aMem, int iIdx, i64 val){ + sqlite3VdbeMemSetInt64(&aMem[iIdx], val); +} + +/* A no-op destructor */ +void sqlite3NoopDestructor(void *p){ UNUSED_PARAMETER(p); } + +/* +** Set the value stored in *pMem should already be a NULL. +** Also store a pointer to go with it. +*/ +void sqlite3VdbeMemSetPointer( + Mem *pMem, + void *pPtr, + const char *zPType, + void (*xDestructor)(void*) +){ + assert( pMem->flags==MEM_Null ); + vdbeMemClear(pMem); + pMem->u.zPType = zPType ? zPType : ""; + pMem->z = pPtr; + pMem->flags = MEM_Null|MEM_Dyn|MEM_Subtype|MEM_Term; + pMem->eSubtype = 'p'; + pMem->xDel = xDestructor ? xDestructor : sqlite3NoopDestructor; +} + #ifndef SQLITE_OMIT_FLOATING_POINT /* ** Delete any previous value and set the value stored in *pMem to val, @@ -711,26 +986,36 @@ void sqlite3VdbeMemSetDouble(Mem *pMem, double val){ } #endif +#ifdef SQLITE_DEBUG +/* +** Return true if the Mem holds a RowSet object. This routine is intended +** for use inside of assert() statements. +*/ +int sqlite3VdbeMemIsRowSet(const Mem *pMem){ + return (pMem->flags&(MEM_Blob|MEM_Dyn))==(MEM_Blob|MEM_Dyn) + && pMem->xDel==sqlite3RowSetDelete; +} +#endif + /* ** Delete any previous value and set the value of pMem to be an ** empty boolean index. +** +** Return SQLITE_OK on success and SQLITE_NOMEM if a memory allocation +** error occurs. */ -void sqlite3VdbeMemSetRowSet(Mem *pMem){ +int sqlite3VdbeMemSetRowSet(Mem *pMem){ sqlite3 *db = pMem->db; + RowSet *p; assert( db!=0 ); - assert( (pMem->flags & MEM_RowSet)==0 ); + assert( !sqlite3VdbeMemIsRowSet(pMem) ); sqlite3VdbeMemRelease(pMem); - pMem->zMalloc = sqlite3DbMallocRawNN(db, 64); - if( db->mallocFailed ){ - pMem->flags = MEM_Null; - pMem->szMalloc = 0; - }else{ - assert( pMem->zMalloc ); - pMem->szMalloc = sqlite3DbMallocSize(db, pMem->zMalloc); - pMem->u.pRowSet = sqlite3RowSetInit(db, pMem->zMalloc, pMem->szMalloc); - assert( pMem->u.pRowSet!=0 ); - pMem->flags = MEM_RowSet; - } + p = sqlite3RowSetInit(db); + if( p==0 ) return SQLITE_NOMEM; + pMem->z = (char*)p; + pMem->flags = MEM_Blob|MEM_Dyn; + pMem->xDel = sqlite3RowSetDelete; + return SQLITE_OK; } /* @@ -755,23 +1040,41 @@ int sqlite3VdbeMemTooBig(Mem *p){ ** its link to a shallow copy and by marking any current shallow ** copies of this cell as invalid. ** -** This is used for testing and debugging only - to make sure shallow -** copies are not misused. +** This is used for testing and debugging only - to help ensure that shallow +** copies (created by OP_SCopy) are not misused. */ void sqlite3VdbeMemAboutToChange(Vdbe *pVdbe, Mem *pMem){ int i; Mem *pX; - for(i=1, pX=&pVdbe->aMem[1]; i<=pVdbe->nMem; i++, pX++){ - if( pX->pScopyFrom==pMem ){ - pX->flags |= MEM_Undefined; - pX->pScopyFrom = 0; + if( pMem->bScopy ){ + for(i=1, pX=pVdbe->aMem+1; i<pVdbe->nMem; i++, pX++){ + if( pX->pScopyFrom==pMem ){ + u16 mFlags; + if( pVdbe->db->flags & SQLITE_VdbeTrace ){ + sqlite3DebugPrintf("Invalidate R[%d] due to change in R[%d]\n", + (int)(pX - pVdbe->aMem), (int)(pMem - pVdbe->aMem)); + } + /* If pX is marked as a shallow copy of pMem, then try to verify that + ** no significant changes have been made to pX since the OP_SCopy. + ** A significant change would indicated a missed call to this + ** function for pX. Minor changes, such as adding or removing a + ** dual type, are allowed, as long as the underlying value is the + ** same. */ + mFlags = pMem->flags & pX->flags & pX->mScopyFlags; + assert( (mFlags&(MEM_Int|MEM_IntReal))==0 || pMem->u.i==pX->u.i ); + + /* pMem is the register that is changing. But also mark pX as + ** undefined so that we can quickly detect the shallow-copy error */ + pX->flags = MEM_Undefined; + pX->pScopyFrom = 0; + } } + pMem->bScopy = 0; } pMem->pScopyFrom = 0; } #endif /* SQLITE_DEBUG */ - /* ** Make an shallow copy of pFrom into pTo. Prior contents of ** pTo are freed. The pFrom->z field is not duplicated. If @@ -784,7 +1087,7 @@ static SQLITE_NOINLINE void vdbeClrCopy(Mem *pTo, const Mem *pFrom, int eType){ sqlite3VdbeMemShallowCopy(pTo, pFrom, eType); } void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){ - assert( (pFrom->flags & MEM_RowSet)==0 ); + assert( !sqlite3VdbeMemIsRowSet(pFrom) ); assert( pTo->db==pFrom->db ); if( VdbeMemDynamic(pTo) ){ vdbeClrCopy(pTo,pFrom,srcType); return; } memcpy(pTo, pFrom, MEMCELLSIZE); @@ -802,11 +1105,7 @@ void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){ int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){ int rc = SQLITE_OK; - /* The pFrom==0 case in the following assert() is when an sqlite3_value - ** from sqlite3_value_dup() is used as the argument - ** to sqlite3_result_value(). */ - assert( pTo->db==pFrom->db || pFrom->db==0 ); - assert( (pFrom->flags & MEM_RowSet)==0 ); + assert( !sqlite3VdbeMemIsRowSet(pFrom) ); if( VdbeMemDynamic(pTo) ) vdbeMemClearExternAndSetNull(pTo); memcpy(pTo, pFrom, MEMCELLSIZE); pTo->flags &= ~MEM_Dyn; @@ -851,20 +1150,29 @@ void sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){ ** stored without allocating memory, then it is. If a memory allocation ** is required to store the string, then value of pMem is unchanged. In ** either case, SQLITE_TOOBIG is returned. +** +** The "enc" parameter is the text encoding for the string, or zero +** to store a blob. +** +** If n is negative, then the string consists of all bytes up to but +** excluding the first zero character. The n parameter must be +** non-negative for blobs. */ int sqlite3VdbeMemSetStr( Mem *pMem, /* Memory cell to set to string value */ const char *z, /* String pointer */ - int n, /* Bytes in string, or negative */ + i64 n, /* Bytes in string, or negative */ u8 enc, /* Encoding of z. 0 for BLOBs */ void (*xDel)(void*) /* Destructor function */ ){ - int nByte = n; /* New value for pMem->n */ + i64 nByte = n; /* New value for pMem->n */ int iLimit; /* Maximum allowed string or blob size */ - u16 flags = 0; /* New value for pMem->flags */ + u16 flags; /* New value for pMem->flags */ + assert( pMem!=0 ); assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); - assert( (pMem->flags & MEM_RowSet)==0 ); + assert( !sqlite3VdbeMemIsRowSet(pMem) ); + assert( enc!=0 || n>=0 ); /* If z is a NULL pointer, set pMem to contain an SQL NULL. */ if( !z ){ @@ -877,16 +1185,30 @@ int sqlite3VdbeMemSetStr( }else{ iLimit = SQLITE_MAX_LENGTH; } - flags = (enc==0?MEM_Blob:MEM_Str); if( nByte<0 ){ assert( enc!=0 ); if( enc==SQLITE_UTF8 ){ - nByte = sqlite3Strlen30(z); - if( nByte>iLimit ) nByte = iLimit+1; + nByte = strlen(z); }else{ for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){} } - flags |= MEM_Term; + flags= MEM_Str|MEM_Term; + }else if( enc==0 ){ + flags = MEM_Blob; + enc = SQLITE_UTF8; + }else{ + flags = MEM_Str; + } + if( nByte>iLimit ){ + if( xDel && xDel!=SQLITE_TRANSIENT ){ + if( xDel==SQLITE_DYNAMIC ){ + sqlite3DbFree(pMem->db, (void*)z); + }else{ + xDel((void*)z); + } + } + sqlite3VdbeMemSetNull(pMem); + return sqlite3ErrorToParser(pMem->db, SQLITE_TOOBIG); } /* The following block sets the new values of Mem.z and Mem.xDel. It @@ -894,54 +1216,49 @@ int sqlite3VdbeMemSetStr( ** management (one of MEM_Dyn or MEM_Static). */ if( xDel==SQLITE_TRANSIENT ){ - int nAlloc = nByte; + i64 nAlloc = nByte; if( flags&MEM_Term ){ nAlloc += (enc==SQLITE_UTF8?1:2); } - if( nByte>iLimit ){ - return SQLITE_TOOBIG; - } testcase( nAlloc==0 ); testcase( nAlloc==31 ); testcase( nAlloc==32 ); - if( sqlite3VdbeMemClearAndResize(pMem, MAX(nAlloc,32)) ){ - return SQLITE_NOMEM; + if( sqlite3VdbeMemClearAndResize(pMem, (int)MAX(nAlloc,32)) ){ + return SQLITE_NOMEM_BKPT; } + assert( pMem->z!=0 ); memcpy(pMem->z, z, nAlloc); - }else if( xDel==SQLITE_DYNAMIC ){ - sqlite3VdbeMemRelease(pMem); - pMem->zMalloc = pMem->z = (char *)z; - pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc); }else{ sqlite3VdbeMemRelease(pMem); pMem->z = (char *)z; - pMem->xDel = xDel; - flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn); + if( xDel==SQLITE_DYNAMIC ){ + pMem->zMalloc = pMem->z; + pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc); + }else{ + pMem->xDel = xDel; + flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn); + } } - pMem->n = nByte; + pMem->n = (int)(nByte & 0x7fffffff); pMem->flags = flags; - pMem->enc = (enc==0 ? SQLITE_UTF8 : enc); + pMem->enc = enc; #ifndef SQLITE_OMIT_UTF16 - if( pMem->enc!=SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){ - return SQLITE_NOMEM; + if( enc>SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){ + return SQLITE_NOMEM_BKPT; } #endif - if( nByte>iLimit ){ - return SQLITE_TOOBIG; - } return SQLITE_OK; } /* ** Move data out of a btree key or data field and into a Mem structure. -** The data or key is taken from the entry that pCur is currently pointing +** The data is payload from the entry that pCur is currently pointing ** to. offset and amt determine what portion of the data or key to retrieve. -** key is true to get the key or false to get data. The result is written -** into the pMem element. +** The result is written into the pMem element. ** ** The pMem object must have been initialized. This routine will use ** pMem->zMalloc to hold the content from the btree, if possible. New @@ -952,25 +1269,22 @@ int sqlite3VdbeMemSetStr( ** If this routine fails for any reason (malloc returns NULL or unable ** to read from the disk) then the pMem is left in an inconsistent state. */ -static SQLITE_NOINLINE int vdbeMemFromBtreeResize( +int sqlite3VdbeMemFromBtree( BtCursor *pCur, /* Cursor pointing at record to retrieve. */ u32 offset, /* Offset from the start of data to return bytes from. */ u32 amt, /* Number of bytes to return. */ - int key, /* If true, retrieve from the btree key, not data. */ Mem *pMem /* OUT: Return data in this Mem structure. */ ){ int rc; pMem->flags = MEM_Null; - if( SQLITE_OK==(rc = sqlite3VdbeMemClearAndResize(pMem, amt+2)) ){ - if( key ){ - rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z); - }else{ - rc = sqlite3BtreeData(pCur, offset, amt, pMem->z); - } + if( sqlite3BtreeMaxRecordSize(pCur)<offset+amt ){ + return SQLITE_CORRUPT_BKPT; + } + if( SQLITE_OK==(rc = sqlite3VdbeMemClearAndResize(pMem, amt+1)) ){ + rc = sqlite3BtreePayload(pCur, offset, amt, pMem->z); if( rc==SQLITE_OK ){ - pMem->z[amt] = 0; - pMem->z[amt+1] = 0; - pMem->flags = MEM_Blob|MEM_Term; + pMem->z[amt] = 0; /* Overrun area used when reading malformed records */ + pMem->flags = MEM_Blob; pMem->n = (int)amt; }else{ sqlite3VdbeMemRelease(pMem); @@ -978,14 +1292,11 @@ static SQLITE_NOINLINE int vdbeMemFromBtreeResize( } return rc; } -int sqlite3VdbeMemFromBtree( +int sqlite3VdbeMemFromBtreeZeroOffset( BtCursor *pCur, /* Cursor pointing at record to retrieve. */ - u32 offset, /* Offset from the start of data to return bytes from. */ u32 amt, /* Number of bytes to return. */ - int key, /* If true, retrieve from the btree key, not data. */ Mem *pMem /* OUT: Return data in this Mem structure. */ ){ - char *zData; /* Data from the btree layer */ u32 available = 0; /* Number of bytes available on the local btree page */ int rc = SQLITE_OK; /* Return code */ @@ -994,20 +1305,15 @@ int sqlite3VdbeMemFromBtree( /* Note: the calls to BtreeKeyFetch() and DataFetch() below assert() ** that both the BtShared and database handle mutexes are held. */ - assert( (pMem->flags & MEM_RowSet)==0 ); - if( key ){ - zData = (char *)sqlite3BtreeKeyFetch(pCur, &available); - }else{ - zData = (char *)sqlite3BtreeDataFetch(pCur, &available); - } - assert( zData!=0 ); + assert( !sqlite3VdbeMemIsRowSet(pMem) ); + pMem->z = (char *)sqlite3BtreePayloadFetch(pCur, &available); + assert( pMem->z!=0 ); - if( offset+amt<=available ){ - pMem->z = &zData[offset]; + if( amt<=available ){ pMem->flags = MEM_Blob|MEM_Ephem; pMem->n = (int)amt; }else{ - rc = vdbeMemFromBtreeResize(pCur, offset, amt, key, pMem); + rc = sqlite3VdbeMemFromBtree(pCur, 0, amt, pMem); } return rc; @@ -1022,13 +1328,11 @@ static SQLITE_NOINLINE const void *valueToText(sqlite3_value* pVal, u8 enc){ assert( pVal!=0 ); assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) ); assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) ); - assert( (pVal->flags & MEM_RowSet)==0 ); + assert( !sqlite3VdbeMemIsRowSet(pVal) ); assert( (pVal->flags & (MEM_Null))==0 ); if( pVal->flags & (MEM_Blob|MEM_Str) ){ + if( ExpandBlob(pVal) ) return 0; pVal->flags |= MEM_Str; - if( pVal->flags & MEM_Zero ){ - sqlite3VdbeMemExpandBlob(pVal); - } if( pVal->enc != (enc & ~SQLITE_UTF16_ALIGNED) ){ sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED); } @@ -1046,6 +1350,7 @@ static SQLITE_NOINLINE const void *valueToText(sqlite3_value* pVal, u8 enc){ assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0 || pVal->db->mallocFailed ); if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){ + assert( sqlite3VdbeMemValidStrRep(pVal) ); return pVal->z; }else{ return 0; @@ -1066,8 +1371,9 @@ const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){ if( !pVal ) return 0; assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) ); assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) ); - assert( (pVal->flags & MEM_RowSet)==0 ); + assert( !sqlite3VdbeMemIsRowSet(pVal) ); if( (pVal->flags&(MEM_Str|MEM_Term))==(MEM_Str|MEM_Term) && pVal->enc==enc ){ + assert( sqlite3VdbeMemValidStrRep(pVal) ); return pVal->z; } if( pVal->flags&MEM_Null ){ @@ -1076,6 +1382,24 @@ const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){ return valueToText(pVal, enc); } +/* Return true if sqlit3_value object pVal is a string or blob value +** that uses the destructor specified in the second argument. +** +** TODO: Maybe someday promote this interface into a published API so +** that third-party extensions can get access to it? +*/ +int sqlite3ValueIsOfClass(const sqlite3_value *pVal, void(*xFree)(void*)){ + if( ALWAYS(pVal!=0) + && ALWAYS((pVal->flags & (MEM_Str|MEM_Blob))!=0) + && (pVal->flags & MEM_Dyn)!=0 + && pVal->xDel==xFree + ){ + return 1; + }else{ + return 0; + } +} + /* ** Create a new sqlite3_value object. */ @@ -1111,13 +1435,13 @@ struct ValueNewStat4Ctx { ** an sqlite3_value within the UnpackedRecord.a[] array. */ static sqlite3_value *valueNew(sqlite3 *db, struct ValueNewStat4Ctx *p){ -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 +#ifdef SQLITE_ENABLE_STAT4 if( p ){ UnpackedRecord *pRec = p->ppRec[0]; if( pRec==0 ){ Index *pIdx = p->pIdx; /* Index being probed */ - int nByte; /* Bytes of space to allocate */ + i64 nByte; /* Bytes of space to allocate */ int i; /* Counter variable */ int nCol = pIdx->nColumn; /* Number of index columns including rowid */ @@ -1126,7 +1450,7 @@ static sqlite3_value *valueNew(sqlite3 *db, struct ValueNewStat4Ctx *p){ if( pRec ){ pRec->pKeyInfo = sqlite3KeyInfoOfIndex(p->pParse, pIdx); if( pRec->pKeyInfo ){ - assert( pRec->pKeyInfo->nField+pRec->pKeyInfo->nXField==nCol ); + assert( pRec->pKeyInfo->nAllField==nCol ); assert( pRec->pKeyInfo->enc==ENC(db) ); pRec->aMem = (Mem *)((u8*)pRec + ROUND8(sizeof(UnpackedRecord))); for(i=0; i<nCol; i++){ @@ -1134,7 +1458,7 @@ static sqlite3_value *valueNew(sqlite3 *db, struct ValueNewStat4Ctx *p){ pRec->aMem[i].db = db; } }else{ - sqlite3DbFree(db, pRec); + sqlite3DbFreeNN(db, pRec); pRec = 0; } } @@ -1143,11 +1467,12 @@ static sqlite3_value *valueNew(sqlite3 *db, struct ValueNewStat4Ctx *p){ } pRec->nField = p->iVal+1; + sqlite3VdbeMemSetNull(&pRec->aMem[p->iVal]); return &pRec->aMem[p->iVal]; } #else UNUSED_PARAMETER(p); -#endif /* defined(SQLITE_ENABLE_STAT3_OR_STAT4) */ +#endif /* defined(SQLITE_ENABLE_STAT4) */ return sqlite3ValueNew(db); } @@ -1171,10 +1496,10 @@ static sqlite3_value *valueNew(sqlite3 *db, struct ValueNewStat4Ctx *p){ ** and sets (*ppVal) to NULL. Or, if an error occurs, (*ppVal) is set to ** NULL and an SQLite error code returned. */ -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 +#ifdef SQLITE_ENABLE_STAT4 static int valueFromFunction( sqlite3 *db, /* The database connection */ - Expr *p, /* The expression to evaluate */ + const Expr *p, /* The expression to evaluate */ u8 enc, /* Encoding to use */ u8 aff, /* Affinity to use */ sqlite3_value **ppVal, /* Write the new value here */ @@ -1182,23 +1507,26 @@ static int valueFromFunction( ){ sqlite3_context ctx; /* Context object for function invocation */ sqlite3_value **apVal = 0; /* Function arguments */ - int nVal = 0; /* Size of apVal[] array */ + int nVal = 0; /* Number of function arguments */ FuncDef *pFunc = 0; /* Function definition */ sqlite3_value *pVal = 0; /* New value */ int rc = SQLITE_OK; /* Return code */ - int nName; /* Size of function name in bytes */ ExprList *pList = 0; /* Function arguments */ int i; /* Iterator variable */ assert( pCtx!=0 ); assert( (p->flags & EP_TokenOnly)==0 ); + assert( ExprUseXList(p) ); pList = p->x.pList; if( pList ) nVal = pList->nExpr; - nName = sqlite3Strlen30(p->u.zToken); - pFunc = sqlite3FindFunction(db, p->u.zToken, nName, nVal, enc, 0); + assert( !ExprHasProperty(p, EP_IntValue) ); + pFunc = sqlite3FindFunction(db, p->u.zToken, nVal, enc, 0); +#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION + if( pFunc==0 ) return SQLITE_OK; +#endif assert( pFunc ); - if( (pFunc->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG))==0 - || (pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL) + if( (pFunc->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG))==0 + || (pFunc->funcFlags & (SQLITE_FUNC_NEEDCOLL|SQLITE_FUNC_RUNONLY))!=0 ){ return SQLITE_OK; } @@ -1206,25 +1534,26 @@ static int valueFromFunction( if( pList ){ apVal = (sqlite3_value**)sqlite3DbMallocZero(db, sizeof(apVal[0]) * nVal); if( apVal==0 ){ - rc = SQLITE_NOMEM; + rc = SQLITE_NOMEM_BKPT; goto value_from_function_out; } for(i=0; i<nVal; i++){ - rc = sqlite3ValueFromExpr(db, pList->a[i].pExpr, enc, aff, &apVal[i]); + rc = sqlite3Stat4ValueFromExpr(pCtx->pParse, pList->a[i].pExpr, aff, + &apVal[i]); if( apVal[i]==0 || rc!=SQLITE_OK ) goto value_from_function_out; } } pVal = valueNew(db, pCtx); if( pVal==0 ){ - rc = SQLITE_NOMEM; + rc = SQLITE_NOMEM_BKPT; goto value_from_function_out; } - assert( pCtx->pParse->rc==SQLITE_OK ); memset(&ctx, 0, sizeof(ctx)); ctx.pOut = pVal; ctx.pFunc = pFunc; + ctx.enc = ENC(db); pFunc->xSFunc(&ctx, nVal, apVal); if( ctx.isError ){ rc = ctx.isError; @@ -1233,22 +1562,22 @@ static int valueFromFunction( sqlite3ValueApplyAffinity(pVal, aff, SQLITE_UTF8); assert( rc==SQLITE_OK ); rc = sqlite3VdbeChangeEncoding(pVal, enc); - if( rc==SQLITE_OK && sqlite3VdbeMemTooBig(pVal) ){ + if( NEVER(rc==SQLITE_OK && sqlite3VdbeMemTooBig(pVal)) ){ rc = SQLITE_TOOBIG; pCtx->pParse->nErr++; } } - pCtx->pParse->rc = rc; value_from_function_out: if( rc!=SQLITE_OK ){ pVal = 0; + pCtx->pParse->rc = rc; } if( apVal ){ for(i=0; i<nVal; i++){ sqlite3ValueFree(apVal[i]); } - sqlite3DbFree(db, apVal); + sqlite3DbFreeNN(db, apVal); } *ppVal = pVal; @@ -1256,7 +1585,7 @@ static int valueFromFunction( } #else # define valueFromFunction(a,b,c,d,e,f) SQLITE_OK -#endif /* defined(SQLITE_ENABLE_STAT3_OR_STAT4) */ +#endif /* defined(SQLITE_ENABLE_STAT4) */ /* ** Extract a value from the supplied expression in the manner described @@ -1270,7 +1599,7 @@ static int valueFromFunction( */ static int valueFromExpr( sqlite3 *db, /* The database connection */ - Expr *pExpr, /* The expression to evaluate */ + const Expr *pExpr, /* The expression to evaluate */ u8 enc, /* Encoding to use */ u8 affinity, /* Affinity to use */ sqlite3_value **ppVal, /* Write the new value here */ @@ -1283,12 +1612,9 @@ static int valueFromExpr( const char *zNeg = ""; int rc = SQLITE_OK; - if( !pExpr ){ - *ppVal = 0; - return SQLITE_OK; - } - while( (op = pExpr->op)==TK_UPLUS ) pExpr = pExpr->pLeft; - if( NEVER(op==TK_REGISTER) ) op = pExpr->op2; + assert( pExpr!=0 ); + while( (op = pExpr->op)==TK_UPLUS || op==TK_SPAN ) pExpr = pExpr->pLeft; + if( op==TK_REGISTER ) op = pExpr->op2; /* Compressed expressions only appear when parsing the DEFAULT clause ** on a table column definition, and hence only when pCtx==0. This @@ -1297,25 +1623,40 @@ static int valueFromExpr( assert( (pExpr->flags & EP_TokenOnly)==0 || pCtx==0 ); if( op==TK_CAST ){ - u8 aff = sqlite3AffinityType(pExpr->u.zToken,0); + u8 aff; + assert( !ExprHasProperty(pExpr, EP_IntValue) ); + aff = sqlite3AffinityType(pExpr->u.zToken,0); rc = valueFromExpr(db, pExpr->pLeft, enc, aff, ppVal, pCtx); testcase( rc!=SQLITE_OK ); if( *ppVal ){ - sqlite3VdbeMemCast(*ppVal, aff, SQLITE_UTF8); - sqlite3ValueApplyAffinity(*ppVal, affinity, SQLITE_UTF8); +#ifdef SQLITE_ENABLE_STAT4 + rc = ExpandBlob(*ppVal); +#else + /* zero-blobs only come from functions, not literal values. And + ** functions are only processed under STAT4 */ + assert( (ppVal[0][0].flags & MEM_Zero)==0 ); +#endif + sqlite3VdbeMemCast(*ppVal, aff, enc); + sqlite3ValueApplyAffinity(*ppVal, affinity, enc); } return rc; } /* Handle negative integers in a single step. This is needed in the - ** case when the value is -9223372036854775808. - */ - if( op==TK_UMINUS - && (pExpr->pLeft->op==TK_INTEGER || pExpr->pLeft->op==TK_FLOAT) ){ - pExpr = pExpr->pLeft; - op = pExpr->op; - negInt = -1; - zNeg = "-"; + ** case when the value is -9223372036854775808. Except - do not do this + ** for hexadecimal literals. */ + if( op==TK_UMINUS ){ + Expr *pLeft = pExpr->pLeft; + if( (pLeft->op==TK_INTEGER || pLeft->op==TK_FLOAT) ){ + if( ExprHasProperty(pLeft, EP_IntValue) + || pLeft->u.zToken[0]!='0' || (pLeft->u.zToken[1] & ~0x20)!='X' + ){ + pExpr = pLeft; + op = pExpr->op; + negInt = -1; + zNeg = "-"; + } + } } if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){ @@ -1324,29 +1665,52 @@ static int valueFromExpr( if( ExprHasProperty(pExpr, EP_IntValue) ){ sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt); }else{ - zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken); - if( zVal==0 ) goto no_mem; - sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC); + i64 iVal; + if( op==TK_INTEGER && 0==sqlite3DecOrHexToI64(pExpr->u.zToken, &iVal) ){ + sqlite3VdbeMemSetInt64(pVal, iVal*negInt); + }else{ + zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken); + if( zVal==0 ) goto no_mem; + sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC); + } } - if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_BLOB ){ - sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8); + if( affinity==SQLITE_AFF_BLOB ){ + if( op==TK_FLOAT ){ + assert( pVal && pVal->z && pVal->flags==(MEM_Str|MEM_Term) ); + sqlite3AtoF(pVal->z, &pVal->u.r, pVal->n, SQLITE_UTF8); + pVal->flags = MEM_Real; + }else if( op==TK_INTEGER ){ + /* This case is required by -9223372036854775808 and other strings + ** that look like integers but cannot be handled by the + ** sqlite3DecOrHexToI64() call above. */ + sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8); + } }else{ sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8); } - if( pVal->flags & (MEM_Int|MEM_Real) ) pVal->flags &= ~MEM_Str; + assert( (pVal->flags & MEM_IntReal)==0 ); + if( pVal->flags & (MEM_Int|MEM_IntReal|MEM_Real) ){ + testcase( pVal->flags & MEM_Int ); + testcase( pVal->flags & MEM_Real ); + pVal->flags &= ~MEM_Str; + } if( enc!=SQLITE_UTF8 ){ rc = sqlite3VdbeChangeEncoding(pVal, enc); } }else if( op==TK_UMINUS ) { /* This branch happens for multiple negative signs. Ex: -(-5) */ - if( SQLITE_OK==sqlite3ValueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal) + if( SQLITE_OK==valueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal,pCtx) && pVal!=0 ){ sqlite3VdbeMemNumerify(pVal); if( pVal->flags & MEM_Real ){ pVal->u.r = -pVal->u.r; }else if( pVal->u.i==SMALLEST_INT64 ){ +#ifndef SQLITE_OMIT_FLOATING_POINT pVal->u.r = -(double)SMALLEST_INT64; +#else + pVal->u.r = LARGEST_INT64; +#endif MemSetTypeFlag(pVal, MEM_Real); }else{ pVal->u.i = -pVal->u.i; @@ -1356,10 +1720,12 @@ static int valueFromExpr( }else if( op==TK_NULL ){ pVal = valueNew(db, pCtx); if( pVal==0 ) goto no_mem; + sqlite3VdbeMemSetNull(pVal); } #ifndef SQLITE_OMIT_BLOB_LITERAL else if( op==TK_BLOB ){ int nVal; + assert( !ExprHasProperty(pExpr, EP_IntValue) ); assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' ); assert( pExpr->u.zToken[1]=='\'' ); pVal = valueNew(db, pCtx); @@ -1371,26 +1737,37 @@ static int valueFromExpr( 0, SQLITE_DYNAMIC); } #endif - -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 +#ifdef SQLITE_ENABLE_STAT4 else if( op==TK_FUNCTION && pCtx!=0 ){ rc = valueFromFunction(db, pExpr, enc, affinity, &pVal, pCtx); } #endif + else if( op==TK_TRUEFALSE ){ + assert( !ExprHasProperty(pExpr, EP_IntValue) ); + pVal = valueNew(db, pCtx); + if( pVal ){ + pVal->flags = MEM_Int; + pVal->u.i = pExpr->u.zToken[4]==0; + sqlite3ValueApplyAffinity(pVal, affinity, enc); + } + } *ppVal = pVal; return rc; no_mem: - sqlite3OomFault(db); +#ifdef SQLITE_ENABLE_STAT4 + if( pCtx==0 || NEVER(pCtx->pParse->nErr==0) ) +#endif + sqlite3OomFault(db); sqlite3DbFree(db, zVal); assert( *ppVal==0 ); -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 +#ifdef SQLITE_ENABLE_STAT4 if( pCtx==0 ) sqlite3ValueFree(pVal); #else assert( pCtx==0 ); sqlite3ValueFree(pVal); #endif - return SQLITE_NOMEM; + return SQLITE_NOMEM_BKPT; } /* @@ -1405,69 +1782,15 @@ static int valueFromExpr( */ int sqlite3ValueFromExpr( sqlite3 *db, /* The database connection */ - Expr *pExpr, /* The expression to evaluate */ + const Expr *pExpr, /* The expression to evaluate */ u8 enc, /* Encoding to use */ u8 affinity, /* Affinity to use */ sqlite3_value **ppVal /* Write the new value here */ ){ - return valueFromExpr(db, pExpr, enc, affinity, ppVal, 0); -} - -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 -/* -** The implementation of the sqlite_record() function. This function accepts -** a single argument of any type. The return value is a formatted database -** record (a blob) containing the argument value. -** -** This is used to convert the value stored in the 'sample' column of the -** sqlite_stat3 table to the record format SQLite uses internally. -*/ -static void recordFunc( - sqlite3_context *context, - int argc, - sqlite3_value **argv -){ - const int file_format = 1; - u32 iSerial; /* Serial type */ - int nSerial; /* Bytes of space for iSerial as varint */ - u32 nVal; /* Bytes of space required for argv[0] */ - int nRet; - sqlite3 *db; - u8 *aRet; - - UNUSED_PARAMETER( argc ); - iSerial = sqlite3VdbeSerialType(argv[0], file_format, &nVal); - nSerial = sqlite3VarintLen(iSerial); - db = sqlite3_context_db_handle(context); - - nRet = 1 + nSerial + nVal; - aRet = sqlite3DbMallocRawNN(db, nRet); - if( aRet==0 ){ - sqlite3_result_error_nomem(context); - }else{ - aRet[0] = nSerial+1; - putVarint32(&aRet[1], iSerial); - sqlite3VdbeSerialPut(&aRet[1+nSerial], argv[0], iSerial); - sqlite3_result_blob(context, aRet, nRet, SQLITE_TRANSIENT); - sqlite3DbFree(db, aRet); - } -} - -/* -** Register built-in functions used to help read ANALYZE data. -*/ -void sqlite3AnalyzeFunctions(void){ - static SQLITE_WSD FuncDef aAnalyzeTableFuncs[] = { - FUNCTION(sqlite_record, 1, 0, 0, recordFunc), - }; - int i; - FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions); - FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aAnalyzeTableFuncs); - for(i=0; i<ArraySize(aAnalyzeTableFuncs); i++){ - sqlite3FuncDefInsert(pHash, &aFunc[i]); - } + return pExpr ? valueFromExpr(db, pExpr, enc, affinity, ppVal, 0) : 0; } +#ifdef SQLITE_ENABLE_STAT4 /* ** Attempt to extract a value from pExpr and use it to construct *ppVal. ** @@ -1500,14 +1823,13 @@ static int stat4ValueFromExpr( /* Skip over any TK_COLLATE nodes */ pExpr = sqlite3ExprSkipCollate(pExpr); + assert( pExpr==0 || pExpr->op!=TK_REGISTER || pExpr->op2!=TK_VARIABLE ); if( !pExpr ){ pVal = valueNew(db, pAlloc); if( pVal ){ sqlite3VdbeMemSetNull((Mem*)pVal); } - }else if( pExpr->op==TK_VARIABLE - || NEVER(pExpr->op==TK_REGISTER && pExpr->op2==TK_VARIABLE) - ){ + }else if( pExpr->op==TK_VARIABLE && (db->flags & SQLITE_EnableQPSG)==0 ){ Vdbe *v; int iBindVar = pExpr->iColumn; sqlite3VdbeSetVarmask(pParse->pVdbe, iBindVar); @@ -1515,9 +1837,7 @@ static int stat4ValueFromExpr( pVal = valueNew(db, pAlloc); if( pVal ){ rc = sqlite3VdbeMemCopy((Mem*)pVal, &v->aVar[iBindVar-1]); - if( rc==SQLITE_OK ){ - sqlite3ValueApplyAffinity(pVal, affinity, ENC(db)); - } + sqlite3ValueApplyAffinity(pVal, affinity, ENC(db)); pVal->db = pParse->db; } } @@ -1535,9 +1855,9 @@ static int stat4ValueFromExpr( ** structures intended to be compared against sample index keys stored ** in the sqlite_stat4 table. ** -** A single call to this function attempts to populates field iVal (leftmost -** is 0 etc.) of the unpacked record with a value extracted from expression -** pExpr. Extraction of values is possible if: +** A single call to this function populates zero or more fields of the +** record starting with field iVal (fields are numbered from left to +** right starting with 0). A single field is populated if: ** ** * (pExpr==0). In this case the value is assumed to be an SQL NULL, ** @@ -1546,10 +1866,14 @@ static int stat4ValueFromExpr( ** * The sqlite3ValueFromExpr() function is able to extract a value ** from the expression (i.e. the expression is a literal value). ** -** If a value can be extracted, the affinity passed as the 5th argument -** is applied to it before it is copied into the UnpackedRecord. Output -** parameter *pbOk is set to true if a value is extracted, or false -** otherwise. +** Or, if pExpr is a TK_VECTOR, one field is populated for each of the +** vector components that match either of the two latter criteria listed +** above. +** +** Before any value is appended to the record, the affinity of the +** corresponding column within index pIdx is applied to it. Before +** this function returns, output parameter *pnExtract is set to the +** number of values appended to the record. ** ** When this function is called, *ppRec must either point to an object ** allocated by an earlier call to this function, or must be NULL. If it @@ -1565,22 +1889,33 @@ int sqlite3Stat4ProbeSetValue( Index *pIdx, /* Index being probed */ UnpackedRecord **ppRec, /* IN/OUT: Probe record */ Expr *pExpr, /* The expression to extract a value from */ - u8 affinity, /* Affinity to use */ + int nElem, /* Maximum number of values to append */ int iVal, /* Array element to populate */ - int *pbOk /* OUT: True if value was extracted */ + int *pnExtract /* OUT: Values appended to the record */ ){ - int rc; - sqlite3_value *pVal = 0; - struct ValueNewStat4Ctx alloc; + int rc = SQLITE_OK; + int nExtract = 0; - alloc.pParse = pParse; - alloc.pIdx = pIdx; - alloc.ppRec = ppRec; - alloc.iVal = iVal; + if( pExpr==0 || pExpr->op!=TK_SELECT ){ + int i; + struct ValueNewStat4Ctx alloc; + + alloc.pParse = pParse; + alloc.pIdx = pIdx; + alloc.ppRec = ppRec; + + for(i=0; i<nElem; i++){ + sqlite3_value *pVal = 0; + Expr *pElem = (pExpr ? sqlite3VectorFieldSubexpr(pExpr, i) : 0); + u8 aff = sqlite3IndexColumnAffinity(pParse->db, pIdx, iVal+i); + alloc.iVal = iVal+i; + rc = stat4ValueFromExpr(pParse, pElem, aff, &alloc, &pVal); + if( !pVal ) break; + nExtract++; + } + } - rc = stat4ValueFromExpr(pParse, pExpr, affinity, &alloc, &pVal); - assert( pVal==0 || pVal->db==pParse->db ); - *pbOk = (pVal!=0); + *pnExtract = nExtract; return rc; } @@ -1618,18 +1953,18 @@ int sqlite3Stat4Column( int iCol, /* Column to extract */ sqlite3_value **ppVal /* OUT: Extracted value */ ){ - u32 t; /* a column type code */ - int nHdr; /* Size of the header in the record */ - int iHdr; /* Next unread header byte */ - int iField; /* Next unread data byte */ - int szField; /* Size of the current data field */ + u32 t = 0; /* a column type code */ + u32 nHdr; /* Size of the header in the record */ + u32 iHdr; /* Next unread header byte */ + i64 iField; /* Next unread data byte */ + u32 szField = 0; /* Size of the current data field */ int i; /* Column index */ u8 *a = (u8*)pRec; /* Typecast byte array */ Mem *pMem = *ppVal; /* Write result into this Mem object */ assert( iCol>0 ); iHdr = getVarint32(a, nHdr); - if( nHdr>nRec || iHdr>=nHdr ) return SQLITE_CORRUPT_BKPT; + if( nHdr>(u32)nRec || iHdr>=nHdr ) return SQLITE_CORRUPT_BKPT; iField = nHdr; for(i=0; i<=iCol; i++){ iHdr += getVarint32(&a[iHdr], t); @@ -1644,7 +1979,7 @@ int sqlite3Stat4Column( if( iField>nRec ) return SQLITE_CORRUPT_BKPT; if( pMem==0 ){ pMem = *ppVal = sqlite3ValueNew(db); - if( pMem==0 ) return SQLITE_NOMEM; + if( pMem==0 ) return SQLITE_NOMEM_BKPT; } sqlite3VdbeSerialGet(&a[iField-szField], t, pMem); pMem->enc = ENC(db); @@ -1659,14 +1994,14 @@ int sqlite3Stat4Column( void sqlite3Stat4ProbeFree(UnpackedRecord *pRec){ if( pRec ){ int i; - int nCol = pRec->pKeyInfo->nField+pRec->pKeyInfo->nXField; + int nCol = pRec->pKeyInfo->nAllField; Mem *aMem = pRec->aMem; sqlite3 *db = aMem[0].db; for(i=0; i<nCol; i++){ sqlite3VdbeMemRelease(&aMem[i]); } sqlite3KeyInfoUnref(pRec->pKeyInfo); - sqlite3DbFree(db, pRec); + sqlite3DbFreeNN(db, pRec); } } #endif /* ifdef SQLITE_ENABLE_STAT4 */ @@ -1690,7 +2025,7 @@ void sqlite3ValueSetStr( void sqlite3ValueFree(sqlite3_value *v){ if( !v ) return; sqlite3VdbeMemRelease((Mem *)v); - sqlite3DbFree(((Mem*)v)->db, v); + sqlite3DbFreeNN(((Mem*)v)->db, v); } /* @@ -1707,6 +2042,9 @@ int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){ if( (p->flags & MEM_Str)!=0 && pVal->enc==enc ){ return p->n; } + if( (p->flags & MEM_Str)!=0 && enc!=SQLITE_UTF8 && pVal->enc!=SQLITE_UTF8 ){ + return p->n; + } if( (p->flags & MEM_Blob)!=0 ){ if( p->flags & MEM_Zero ){ return p->n + p->u.nZero; diff --git a/src/vdbesort.c b/src/vdbesort.c index 380772ee16..73f3301116 100644 --- a/src/vdbesort.c +++ b/src/vdbesort.c @@ -55,7 +55,7 @@ ** is like Close() followed by Init() only ** much faster. ** -** The interfaces above must be called in a particular order. Write() can +** The interfaces above must be called in a particular order. Write() can ** only occur in between Init()/Reset() and Rewind(). Next(), Rowkey(), and ** Compare() can only occur in between Rewind() and Close()/Reset(). i.e. ** @@ -63,16 +63,16 @@ ** for each record: Write() ** Rewind() ** Rowkey()/Compare() -** Next() +** Next() ** Close() ** ** Algorithm: ** -** Records passed to the sorter via calls to Write() are initially held +** Records passed to the sorter via calls to Write() are initially held ** unsorted in main memory. Assuming the amount of memory used never exceeds ** a threshold, when Rewind() is called the set of records is sorted using ** an in-memory merge sort. In this case, no temporary files are required -** and subsequent calls to Rowkey(), Next() and Compare() read records +** and subsequent calls to Rowkey(), Next() and Compare() read records ** directly from main memory. ** ** If the amount of space used to store records in main memory exceeds the @@ -82,10 +82,10 @@ ** of PMAs may be created by merging existing PMAs together - for example ** merging two or more level-0 PMAs together creates a level-1 PMA. ** -** The threshold for the amount of main memory to use before flushing +** The threshold for the amount of main memory to use before flushing ** records to a PMA is roughly the same as the limit configured for the -** page-cache of the main database. Specifically, the threshold is set to -** the value returned by "PRAGMA main.page_size" multipled by +** page-cache of the main database. Specifically, the threshold is set to +** the value returned by "PRAGMA main.page_size" multiplied by ** that returned by "PRAGMA main.cache_size", in bytes. ** ** If the sorter is running in single-threaded mode, then all PMAs generated @@ -102,28 +102,28 @@ ** than zero, and (b) worker threads have been enabled at runtime by calling ** "PRAGMA threads=N" with some value of N greater than 0. ** -** When Rewind() is called, any data remaining in memory is flushed to a +** When Rewind() is called, any data remaining in memory is flushed to a ** final PMA. So at this point the data is stored in some number of sorted ** PMAs within temporary files on disk. ** ** If there are fewer than SORTER_MAX_MERGE_COUNT PMAs in total and the ** sorter is running in single-threaded mode, then these PMAs are merged -** incrementally as keys are retreived from the sorter by the VDBE. The +** incrementally as keys are retrieved from the sorter by the VDBE. The ** MergeEngine object, described in further detail below, performs this ** merge. ** ** Or, if running in multi-threaded mode, then a background thread is ** launched to merge the existing PMAs. Once the background thread has -** merged T bytes of data into a single sorted PMA, the main thread +** merged T bytes of data into a single sorted PMA, the main thread ** begins reading keys from that PMA while the background thread proceeds ** with merging the next T bytes of data. And so on. ** -** Parameter T is set to half the value of the memory threshold used +** Parameter T is set to half the value of the memory threshold used ** by Write() above to determine when to create a new PMA. ** -** If there are more than SORTER_MAX_MERGE_COUNT PMAs in total when -** Rewind() is called, then a hierarchy of incremental-merges is used. -** First, T bytes of data from the first SORTER_MAX_MERGE_COUNT PMAs on +** If there are more than SORTER_MAX_MERGE_COUNT PMAs in total when +** Rewind() is called, then a hierarchy of incremental-merges is used. +** First, T bytes of data from the first SORTER_MAX_MERGE_COUNT PMAs on ** disk are merged together. Then T bytes of data from the second set, and ** so on, such that no operation ever merges more than SORTER_MAX_MERGE_COUNT ** PMAs at a time. This done is to improve locality. @@ -138,7 +138,7 @@ #include "sqliteInt.h" #include "vdbeInt.h" -/* +/* ** If SQLITE_DEBUG_SORTER_THREADS is defined, this module outputs various ** messages to stderr that may be helpful in understanding the performance ** characteristics of the sorter in multi-threaded mode. @@ -167,7 +167,7 @@ typedef struct SorterList SorterList; /* In-memory list of records */ typedef struct IncrMerger IncrMerger; /* Read & merge multiple PMAs */ /* -** A container for a temp file handle and the current amount of data +** A container for a temp file handle and the current amount of data ** stored in the file. */ struct SorterFile { @@ -186,7 +186,7 @@ struct SorterFile { struct SorterList { SorterRecord *pList; /* Linked list of records */ u8 *aMemory; /* If non-NULL, bulk memory to hold pList */ - int szPMA; /* Size of pList as PMA in bytes */ + i64 szPMA; /* Size of pList as PMA in bytes */ }; /* @@ -207,17 +207,17 @@ struct SorterList { ** the MergeEngine.nTree variable. ** ** The final (N/2) elements of aTree[] contain the results of comparing -** pairs of PMA keys together. Element i contains the result of +** pairs of PMA keys together. Element i contains the result of ** comparing aReadr[2*i-N] and aReadr[2*i-N+1]. Whichever key is smaller, the -** aTree element is set to the index of it. +** aTree element is set to the index of it. ** ** For the purposes of this comparison, EOF is considered greater than any ** other key value. If the keys are equal (only possible with two EOF ** values), it doesn't matter which index is stored. ** -** The (N/4) elements of aTree[] that precede the final (N/2) described +** The (N/4) elements of aTree[] that precede the final (N/2) described ** above contains the index of the smallest of each block of 4 PmaReaders -** And so on. So that aTree[1] contains the index of the PmaReader that +** And so on. So that aTree[1] contains the index of the PmaReader that ** currently points to the smallest key value. aTree[0] is unused. ** ** Example: @@ -233,7 +233,7 @@ struct SorterList { ** ** aTree[] = { X, 5 0, 5 0, 3, 5, 6 } ** -** The current element is "Apple" (the value of the key indicated by +** The current element is "Apple" (the value of the key indicated by ** PmaReader 5). When the Next() operation is invoked, PmaReader 5 will ** be advanced to the next key in its segment. Say the next key is ** "Eggplant": @@ -271,11 +271,11 @@ struct MergeEngine { ** ** Essentially, this structure contains all those fields of the VdbeSorter ** structure for which each thread requires a separate instance. For example, -** each thread requries its own UnpackedRecord object to unpack records in +** each thread requeries its own UnpackedRecord object to unpack records in ** as part of comparison operations. ** -** Before a background thread is launched, variable bDone is set to 0. Then, -** right before it exits, the thread itself sets bDone to 1. This is used for +** Before a background thread is launched, variable bDone is set to 0. Then, +** right before it exits, the thread itself sets bDone to 1. This is used for ** two purposes: ** ** 1. When flushing the contents of memory to a level-0 PMA on disk, to @@ -295,18 +295,19 @@ typedef int (*SorterCompare)(SortSubtask*,int*,const void*,int,const void*,int); struct SortSubtask { SQLiteThread *pThread; /* Background thread, if any */ int bDone; /* Set if thread is finished but not joined */ + int nPMA; /* Number of PMAs currently in file */ VdbeSorter *pSorter; /* Sorter that owns this sub-task */ UnpackedRecord *pUnpacked; /* Space to unpack a record */ SorterList list; /* List for thread to write to a PMA */ - int nPMA; /* Number of PMAs currently in file */ SorterCompare xCompare; /* Compare function to use */ SorterFile file; /* Temp file for level-0 PMAs */ SorterFile file2; /* Space for other PMAs */ + u64 nSpill; /* Total bytes written by this task */ }; /* -** Main sorter structure. A single instance of this is allocated for each +** Main sorter structure. A single instance of this is allocated for each ** sorter cursor created by the VDBE. ** ** mxKeysize: @@ -332,9 +333,12 @@ struct VdbeSorter { u8 iPrev; /* Previous thread used to flush PMA */ u8 nTask; /* Size of aTask[] array */ u8 typeMask; - SortSubtask aTask[1]; /* One or more subtasks */ + SortSubtask aTask[FLEXARRAY]; /* One or more subtasks */ }; +/* Size (in bytes) of a VdbeSorter object that works with N or fewer subtasks */ +#define SZ_VDBESORTER(N) (offsetof(VdbeSorter,aTask)+(N)*sizeof(SortSubtask)) + #define SORTER_TYPE_INTEGER 0x01 #define SORTER_TYPE_TEXT 0x02 @@ -343,7 +347,7 @@ struct VdbeSorter { ** PMA, in sorted order. The next key to be read is cached in nKey/aKey. ** aKey might point into aMap or into aBuffer. If neither of those locations ** contain a contiguous representation of the key, then aAlloc is allocated -** and the key is copied into aAlloc and aKey is made to poitn to aAlloc. +** and the key is copied into aAlloc and aKey is made to point to aAlloc. ** ** pFd==0 at EOF. */ @@ -362,21 +366,21 @@ struct PmaReader { }; /* -** Normally, a PmaReader object iterates through an existing PMA stored +** Normally, a PmaReader object iterates through an existing PMA stored ** within a temp file. However, if the PmaReader.pIncr variable points to ** an object of the following type, it may be used to iterate/merge through ** multiple PMAs simultaneously. ** -** There are two types of IncrMerger object - single (bUseThread==0) and -** multi-threaded (bUseThread==1). +** There are two types of IncrMerger object - single (bUseThread==0) and +** multi-threaded (bUseThread==1). ** -** A multi-threaded IncrMerger object uses two temporary files - aFile[0] -** and aFile[1]. Neither file is allowed to grow to more than mxSz bytes in -** size. When the IncrMerger is initialized, it reads enough data from -** pMerger to populate aFile[0]. It then sets variables within the -** corresponding PmaReader object to read from that file and kicks off -** a background thread to populate aFile[1] with the next mxSz bytes of -** sorted record data from pMerger. +** A multi-threaded IncrMerger object uses two temporary files - aFile[0] +** and aFile[1]. Neither file is allowed to grow to more than mxSz bytes in +** size. When the IncrMerger is initialized, it reads enough data from +** pMerger to populate aFile[0]. It then sets variables within the +** corresponding PmaReader object to read from that file and kicks off +** a background thread to populate aFile[1] with the next mxSz bytes of +** sorted record data from pMerger. ** ** When the PmaReader reaches the end of aFile[0], it blocks until the ** background thread has finished populating aFile[1]. It then exchanges @@ -387,7 +391,7 @@ struct PmaReader { ** ** A single-threaded IncrMerger does not open any temporary files of its ** own. Instead, it has exclusive access to mxSz bytes of space beginning -** at offset iStartOff of file pTask->file2. And instead of using a +** at offset iStartOff of file pTask->file2. And instead of using a ** background thread to prepare data for the PmaReader, with a single ** threaded IncrMerger the allocate part of pTask->file2 is "refilled" with ** keys from pMerger by the calling thread whenever the PmaReader runs out @@ -419,6 +423,7 @@ struct PmaWriter { int iBufEnd; /* Last byte of buffer to write */ i64 iWriteOff; /* Offset of start of buffer in file */ sqlite3_file *pFd; /* File handle to write to */ + u64 nPmaSpill; /* Total number of bytes written */ }; /* @@ -499,7 +504,7 @@ static int vdbePmaReadBlob( assert( p->aBuffer ); - /* If there is no more data to be read from the buffer, read the next + /* If there is no more data to be read from the buffer, read the next ** p->nBuffer bytes of data from the file into it. Or, if there are less ** than p->nBuffer bytes remaining in the PMA, read all remaining data. */ iBuf = p->iReadOff % p->nBuffer; @@ -520,11 +525,11 @@ static int vdbePmaReadBlob( assert( rc!=SQLITE_IOERR_SHORT_READ ); if( rc!=SQLITE_OK ) return rc; } - nAvail = p->nBuffer - iBuf; + nAvail = p->nBuffer - iBuf; if( nByte<=nAvail ){ /* The requested data is available in the in-memory buffer. In this - ** case there is no need to make a copy of the data, just return a + ** case there is no need to make a copy of the data, just return a ** pointer into the buffer to the caller. */ *ppOut = &p->aBuffer[iBuf]; p->iReadOff += nByte; @@ -537,10 +542,10 @@ static int vdbePmaReadBlob( /* Extend the p->aAlloc[] allocation if required. */ if( p->nAlloc<nByte ){ u8 *aNew; - int nNew = MAX(128, p->nAlloc*2); + sqlite3_int64 nNew = MAX(128, 2*(sqlite3_int64)p->nAlloc); while( nByte>nNew ) nNew = nNew*2; aNew = sqlite3Realloc(p->aAlloc, nNew); - if( !aNew ) return SQLITE_NOMEM; + if( !aNew ) return SQLITE_NOMEM_BKPT; p->nAlloc = nNew; p->aAlloc = aNew; } @@ -556,13 +561,14 @@ static int vdbePmaReadBlob( while( nRem>0 ){ int rc; /* vdbePmaReadBlob() return code */ int nCopy; /* Number of bytes to copy */ - u8 *aNext; /* Pointer to buffer to copy data from */ + u8 *aNext = 0; /* Pointer to buffer to copy data from */ nCopy = nRem; if( nRem>p->nBuffer ) nCopy = p->nBuffer; rc = vdbePmaReadBlob(p, nCopy, &aNext); if( rc!=SQLITE_OK ) return rc; assert( aNext!=p->aAlloc ); + assert( aNext!=0 ); memcpy(&p->aAlloc[nByte - nRem], aNext, nCopy); nRem -= nCopy; } @@ -603,7 +609,7 @@ static int vdbePmaReadVarint(PmaReader *p, u64 *pnOut){ /* ** Attempt to memory map file pFile. If successful, set *pp to point to the -** new mapping and return SQLITE_OK. If the mapping is not attempted +** new mapping and return SQLITE_OK. If the mapping is not attempted ** (because the file is too large or the VFS layer is configured not to use ** mmap), return SQLITE_OK and set *pp to NULL. ** @@ -624,7 +630,7 @@ static int vdbeSorterMapFile(SortSubtask *pTask, SorterFile *pFile, u8 **pp){ /* ** Attach PmaReader pReadr to file pFile (if it is not already attached to -** that file) and seek it to offset iOff within the file. Return SQLITE_OK +** that file) and seek it to offset iOff within the file. Return SQLITE_OK ** if successful, or an SQLite error code if an error occurs. */ static int vdbePmaReaderSeek( @@ -652,7 +658,7 @@ static int vdbePmaReaderSeek( int iBuf = pReadr->iReadOff % pgsz; if( pReadr->aBuffer==0 ){ pReadr->aBuffer = (u8*)sqlite3Malloc(pgsz); - if( pReadr->aBuffer==0 ) rc = SQLITE_NOMEM; + if( pReadr->aBuffer==0 ) rc = SQLITE_NOMEM_BKPT; pReadr->nBuffer = pgsz; } if( rc==SQLITE_OK && iBuf ){ @@ -714,11 +720,11 @@ static int vdbePmaReaderNext(PmaReader *pReadr){ /* ** Initialize PmaReader pReadr to scan through the PMA stored in file pFile -** starting at offset iStart and ending at offset iEof-1. This function -** leaves the PmaReader pointing to the first key in the PMA (or EOF if the +** starting at offset iStart and ending at offset iEof-1. This function +** leaves the PmaReader pointing to the first key in the PMA (or EOF if the ** PMA is empty). ** -** If the pnByte parameter is NULL, then it is assumed that the file +** If the pnByte parameter is NULL, then it is assumed that the file ** contains a single PMA, and that that PMA omits the initial length varint. */ static int vdbePmaReaderInit( @@ -751,7 +757,7 @@ static int vdbePmaReaderInit( /* ** A version of vdbeSorterCompare() that assumes that it has already been -** determined that the first field of key1 is equal to the first field of +** determined that the first field of key1 is equal to the first field of ** key2. */ static int vdbeSorterCompareTail( @@ -762,14 +768,14 @@ static int vdbeSorterCompareTail( ){ UnpackedRecord *r2 = pTask->pUnpacked; if( *pbKey2Cached==0 ){ - sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2); + sqlite3VdbeRecordUnpack(nKey2, pKey2, r2); *pbKey2Cached = 1; } return sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, r2, 1); } /* -** Compare key1 (buffer pKey1, size nKey1 bytes) with key2 (buffer pKey2, +** Compare key1 (buffer pKey1, size nKey1 bytes) with key2 (buffer pKey2, ** size nKey2 bytes). Use (pTask->pKeyInfo) for the collation sequences ** used by the comparison. Return the result of the comparison. ** @@ -789,7 +795,7 @@ static int vdbeSorterCompare( ){ UnpackedRecord *r2 = pTask->pUnpacked; if( !*pbKey2Cached ){ - sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2); + sqlite3VdbeRecordUnpack(nKey2, pKey2, r2); *pbKey2Cached = 1; } return sqlite3VdbeRecordCompare(nKey1, pKey1, r2); @@ -815,21 +821,23 @@ static int vdbeSorterCompareText( int n2; int res; - getVarint32(&p1[1], n1); n1 = (n1 - 13) / 2; - getVarint32(&p2[1], n2); n2 = (n2 - 13) / 2; - res = memcmp(v1, v2, MIN(n1, n2)); + getVarint32NR(&p1[1], n1); + getVarint32NR(&p2[1], n2); + res = memcmp(v1, v2, (MIN(n1, n2) - 13)/2); if( res==0 ){ res = n1 - n2; } if( res==0 ){ - if( pTask->pSorter->pKeyInfo->nField>1 ){ + if( pTask->pSorter->pKeyInfo->nKeyField>1 ){ res = vdbeSorterCompareTail( pTask, pbKey2Cached, pKey1, nKey1, pKey2, nKey2 ); } }else{ - if( pTask->pSorter->pKeyInfo->aSortOrder[0] ){ + assert( pTask->pSorter->pKeyInfo->aSortFlags!=0 ); + assert( !(pTask->pSorter->pKeyInfo->aSortFlags[0]&KEYINFO_ORDER_BIGNULL) ); + if( pTask->pSorter->pKeyInfo->aSortFlags[0] ){ res = res * -1; } } @@ -858,47 +866,48 @@ static int vdbeSorterCompareInt( assert( (s1>0 && s1<7) || s1==8 || s1==9 ); assert( (s2>0 && s2<7) || s2==8 || s2==9 ); - if( s1>7 && s2>7 ){ - res = s1 - s2; - }else{ - if( s1==s2 ){ - if( (*v1 ^ *v2) & 0x80 ){ - /* The two values have different signs */ - res = (*v1 & 0x80) ? -1 : +1; - }else{ - /* The two values have the same sign. Compare using memcmp(). */ - static const u8 aLen[] = {0, 1, 2, 3, 4, 6, 8 }; - int i; - res = 0; - for(i=0; i<aLen[s1]; i++){ - if( (res = v1[i] - v2[i]) ) break; + if( s1==s2 ){ + /* The two values have the same sign. Compare using memcmp(). */ + static const u8 aLen[] = {0, 1, 2, 3, 4, 6, 8, 0, 0, 0 }; + const u8 n = aLen[s1]; + int i; + res = 0; + for(i=0; i<n; i++){ + if( (res = v1[i] - v2[i])!=0 ){ + if( ((v1[0] ^ v2[0]) & 0x80)!=0 ){ + res = v1[0] & 0x80 ? -1 : +1; } + break; } + } + }else if( s1>7 && s2>7 ){ + res = s1 - s2; + }else{ + if( s2>7 ){ + res = +1; + }else if( s1>7 ){ + res = -1; }else{ - if( s2>7 ){ - res = +1; - }else if( s1>7 ){ - res = -1; - }else{ - res = s1 - s2; - } - assert( res!=0 ); + res = s1 - s2; + } + assert( res!=0 ); - if( res>0 ){ - if( *v1 & 0x80 ) res = -1; - }else{ - if( *v2 & 0x80 ) res = +1; - } + if( res>0 ){ + if( *v1 & 0x80 ) res = -1; + }else{ + if( *v2 & 0x80 ) res = +1; } } + assert( pTask->pSorter->pKeyInfo->aSortFlags!=0 ); if( res==0 ){ - if( pTask->pSorter->pKeyInfo->nField>1 ){ + if( pTask->pSorter->pKeyInfo->nKeyField>1 ){ res = vdbeSorterCompareTail( pTask, pbKey2Cached, pKey1, nKey1, pKey2, nKey2 ); } - }else if( pTask->pSorter->pKeyInfo->aSortOrder[0] ){ + }else if( pTask->pSorter->pKeyInfo->aSortFlags[0] ){ + assert( !(pTask->pSorter->pKeyInfo->aSortFlags[0]&KEYINFO_ORDER_BIGNULL) ); res = res * -1; } @@ -908,13 +917,13 @@ static int vdbeSorterCompareInt( /* ** Initialize the temporary index cursor just opened as a sorter cursor. ** -** Usually, the sorter module uses the value of (pCsr->pKeyInfo->nField) +** Usually, the sorter module uses the value of (pCsr->pKeyInfo->nKeyField) ** to determine the number of fields that should be compared from the ** records being sorted. However, if the value passed as argument nField ** is non-zero and the sorter is able to guarantee a stable sort, nField ** is used instead. This is used when sorting records for a CREATE INDEX ** statement. In this case, keys are always delivered to the sorter in -** order of the primary key, which happens to be make up the final part +** order of the primary key, which happens to be make up the final part ** of the records being sorted. So if the sort is stable, there is never ** any reason to compare PK fields and they can be ignored for a small ** performance boost. @@ -931,11 +940,10 @@ int sqlite3VdbeSorterInit( ){ int pgsz; /* Page size of main database */ int i; /* Used to iterate through aTask[] */ - int mxCache; /* Cache size */ VdbeSorter *pSorter; /* The new sorter */ KeyInfo *pKeyInfo; /* Copy of pCsr->pKeyInfo with db==0 */ int szKeyInfo; /* Size of pCsr->pKeyInfo in bytes */ - int sz; /* Size of pSorter in bytes */ + i64 sz; /* Size of pSorter in bytes */ int rc = SQLITE_OK; #if SQLITE_MAX_WORKER_THREADS==0 # define nWorker 0 @@ -960,24 +968,35 @@ int sqlite3VdbeSorterInit( } #endif - assert( pCsr->pKeyInfo && pCsr->pBt==0 ); + assert( pCsr->pKeyInfo ); + assert( !pCsr->isEphemeral ); assert( pCsr->eCurType==CURTYPE_SORTER ); - szKeyInfo = sizeof(KeyInfo) + (pCsr->pKeyInfo->nField-1)*sizeof(CollSeq*); - sz = sizeof(VdbeSorter) + nWorker * sizeof(SortSubtask); + assert( sizeof(KeyInfo) + UMXV(pCsr->pKeyInfo->nKeyField)*sizeof(CollSeq*) + < 0x7fffffff ); + assert( pCsr->pKeyInfo->nKeyField<=pCsr->pKeyInfo->nAllField ); + szKeyInfo = SZ_KEYINFO(pCsr->pKeyInfo->nAllField); + sz = SZ_VDBESORTER(nWorker+1); pSorter = (VdbeSorter*)sqlite3DbMallocZero(db, sz + szKeyInfo); pCsr->uc.pSorter = pSorter; if( pSorter==0 ){ - rc = SQLITE_NOMEM; + rc = SQLITE_NOMEM_BKPT; }else{ + Btree *pBt = db->aDb[0].pBt; pSorter->pKeyInfo = pKeyInfo = (KeyInfo*)((u8*)pSorter + sz); memcpy(pKeyInfo, pCsr->pKeyInfo, szKeyInfo); pKeyInfo->db = 0; if( nField && nWorker==0 ){ - pKeyInfo->nXField += (pKeyInfo->nField - nField); - pKeyInfo->nField = nField; + pKeyInfo->nKeyField = nField; + assert( nField<=pCsr->pKeyInfo->nAllField ); } - pSorter->pgsz = pgsz = sqlite3BtreeGetPageSize(db->aDb[0].pBt); + /* It is OK that pKeyInfo reuses the aSortFlags field from pCsr->pKeyInfo, + ** since the pCsr->pKeyInfo->aSortFlags[] array is invariant and lives + ** longer that pSorter. */ + assert( pKeyInfo->aSortFlags==pCsr->pKeyInfo->aSortFlags ); + sqlite3BtreeEnter(pBt); + pSorter->pgsz = pgsz = sqlite3BtreeGetPageSize(pBt); + sqlite3BtreeLeave(pBt); pSorter->nTask = nWorker + 1; pSorter->iPrev = (u8)(nWorker - 1); pSorter->bUseThreads = (pSorter->nTask>1); @@ -988,26 +1007,34 @@ int sqlite3VdbeSorterInit( } if( !sqlite3TempInMemory(db) ){ + i64 mxCache; /* Cache size in bytes*/ u32 szPma = sqlite3GlobalConfig.szPma; pSorter->mnPmaSize = szPma * pgsz; + mxCache = db->aDb[0].pSchema->cache_size; - if( mxCache<(int)szPma ) mxCache = (int)szPma; - pSorter->mxPmaSize = MIN((i64)mxCache*pgsz, SQLITE_MAX_PMASZ); - - /* EVIDENCE-OF: R-26747-61719 When the application provides any amount of - ** scratch memory using SQLITE_CONFIG_SCRATCH, SQLite avoids unnecessary - ** large heap allocations. - */ - if( sqlite3GlobalConfig.pScratch==0 ){ + if( mxCache<0 ){ + /* A negative cache-size value C indicates that the cache is abs(C) + ** KiB in size. */ + mxCache = mxCache * -1024; + }else{ + mxCache = mxCache * pgsz; + } + mxCache = MIN(mxCache, SQLITE_MAX_PMASZ); + pSorter->mxPmaSize = MAX(pSorter->mnPmaSize, (int)mxCache); + + /* Avoid large memory allocations if the application has requested + ** SQLITE_CONFIG_SMALL_MALLOC. */ + if( sqlite3GlobalConfig.bSmallMalloc==0 ){ assert( pSorter->iMemory==0 ); pSorter->nMemory = pgsz; pSorter->list.aMemory = (u8*)sqlite3Malloc(pgsz); - if( !pSorter->list.aMemory ) rc = SQLITE_NOMEM; + if( !pSorter->list.aMemory ) rc = SQLITE_NOMEM_BKPT; } } - if( (pKeyInfo->nField+pKeyInfo->nXField)<13 + if( pKeyInfo->nAllField<13 && (pKeyInfo->aColl[0]==0 || pKeyInfo->aColl[0]==db->pDfltColl) + && (pKeyInfo->aSortFlags[0] & KEYINFO_ORDER_BIGNULL)==0 ){ pSorter->typeMask = SORTER_TYPE_INTEGER | SORTER_TYPE_TEXT; } @@ -1030,7 +1057,7 @@ static void vdbeSorterRecordFree(sqlite3 *db, SorterRecord *pRecord){ } /* -** Free all resources owned by the object indicated by argument pTask. All +** Free all resources owned by the object indicated by argument pTask. All ** fields of *pTask are zeroed before returning. */ static void vdbeSortSubtaskCleanup(sqlite3 *db, SortSubtask *pTask){ @@ -1063,8 +1090,9 @@ static void vdbeSorterWorkDebug(SortSubtask *pTask, const char *zEvent){ fprintf(stderr, "%lld:%d %s\n", t, iTask, zEvent); } static void vdbeSorterRewindDebug(const char *zEvent){ - i64 t; - sqlite3OsCurrentTimeInt64(sqlite3_vfs_find(0), &t); + i64 t = 0; + sqlite3_vfs *pVfs = sqlite3_vfs_find(0); + if( ALWAYS(pVfs) ) sqlite3OsCurrentTimeInt64(pVfs, &t); fprintf(stderr, "%lld:X %s\n", t, zEvent); } static void vdbeSorterPopulateDebug( @@ -1129,7 +1157,7 @@ static int vdbeSorterCreateThread( } /* -** Join all outstanding threads launched by SorterWrite() to create +** Join all outstanding threads launched by SorterWrite() to create ** level-0 PMAs. */ static int vdbeSorterJoinAll(VdbeSorter *pSorter, int rcin){ @@ -1138,10 +1166,10 @@ static int vdbeSorterJoinAll(VdbeSorter *pSorter, int rcin){ /* This function is always called by the main user thread. ** - ** If this function is being called after SorterRewind() has been called, + ** If this function is being called after SorterRewind() has been called, ** it is possible that thread pSorter->aTask[pSorter->nTask-1].pThread ** is currently attempt to join one of the other threads. To avoid a race - ** condition where this thread also attempts to join the same object, join + ** condition where this thread also attempts to join the same object, join ** thread pSorter->aTask[pSorter->nTask-1].pThread first. */ for(i=pSorter->nTask-1; i>=0; i--){ SortSubtask *pTask = &pSorter->aTask[i]; @@ -1164,7 +1192,7 @@ static int vdbeSorterJoinAll(VdbeSorter *pSorter, int rcin){ */ static MergeEngine *vdbeMergeEngineNew(int nReader){ int N = 2; /* Smallest power of two >= nReader */ - int nByte; /* Total bytes of space to allocate */ + i64 nByte; /* Total bytes of space to allocate */ MergeEngine *pNew; /* Pointer to allocated object to return */ assert( nReader<=SORTER_MAX_MERGE_COUNT ); @@ -1254,6 +1282,12 @@ void sqlite3VdbeSorterClose(sqlite3 *db, VdbeCursor *pCsr){ assert( pCsr->eCurType==CURTYPE_SORTER ); pSorter = pCsr->uc.pSorter; if( pSorter ){ + /* Increment db->nSpill by the total number of bytes of data written + ** to temp files by this sort operation. */ + int ii; + for(ii=0; ii<pSorter->nTask; ii++){ + db->nSpill += pSorter->aTask[ii].nSpill; + } sqlite3VdbeSorterReset(db, pSorter); sqlite3_free(pSorter->list.aMemory); sqlite3DbFree(db, pSorter); @@ -1278,7 +1312,7 @@ static void vdbeSorterExtendFile(sqlite3 *db, sqlite3_file *pFd, i64 nByte){ sqlite3OsFileControlHint(pFd, SQLITE_FCNTL_CHUNK_SIZE, &chunksize); sqlite3OsFileControlHint(pFd, SQLITE_FCNTL_SIZE_HINT, &nByte); sqlite3OsFetch(pFd, 0, (int)nByte, &p); - sqlite3OsUnfetch(pFd, 0, p); + if( p ) sqlite3OsUnfetch(pFd, 0, p); } } #else @@ -1313,19 +1347,15 @@ static int vdbeSorterOpenTempFile( } /* -** If it has not already been allocated, allocate the UnpackedRecord -** structure at pTask->pUnpacked. Return SQLITE_OK if successful (or +** If it has not already been allocated, allocate the UnpackedRecord +** structure at pTask->pUnpacked. Return SQLITE_OK if successful (or ** if no allocation was required), or SQLITE_NOMEM otherwise. */ static int vdbeSortAllocUnpacked(SortSubtask *pTask){ if( pTask->pUnpacked==0 ){ - char *pFree; - pTask->pUnpacked = sqlite3VdbeAllocUnpackedRecord( - pTask->pSorter->pKeyInfo, 0, 0, &pFree - ); - assert( pTask->pUnpacked==(UnpackedRecord*)pFree ); - if( pFree==0 ) return SQLITE_NOMEM; - pTask->pUnpacked->nField = pTask->pSorter->pKeyInfo->nField; + pTask->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pTask->pSorter->pKeyInfo); + if( pTask->pUnpacked==0 ) return SQLITE_NOMEM_BKPT; + pTask->pUnpacked->nField = pTask->pSorter->pKeyInfo->nKeyField; pTask->pUnpacked->errCode = 0; } return SQLITE_OK; @@ -1334,19 +1364,18 @@ static int vdbeSortAllocUnpacked(SortSubtask *pTask){ /* ** Merge the two sorted lists p1 and p2 into a single list. -** Set *ppOut to the head of the new list. */ -static void vdbeSorterMerge( +static SorterRecord *vdbeSorterMerge( SortSubtask *pTask, /* Calling thread context */ SorterRecord *p1, /* First list to merge */ - SorterRecord *p2, /* Second list to merge */ - SorterRecord **ppOut /* OUT: Head of merged list */ + SorterRecord *p2 /* Second list to merge */ ){ SorterRecord *pFinal = 0; SorterRecord **pp = &pFinal; int bCached = 0; - while( p1 && p2 ){ + assert( p1!=0 && p2!=0 ); + for(;;){ int res; res = pTask->xCompare( pTask, &bCached, SRVAL(p1), p1->nVal, SRVAL(p2), p2->nVal @@ -1356,15 +1385,22 @@ static void vdbeSorterMerge( *pp = p1; pp = &p1->u.pNext; p1 = p1->u.pNext; + if( p1==0 ){ + *pp = p2; + break; + } }else{ *pp = p2; pp = &p2->u.pNext; p2 = p2->u.pNext; bCached = 0; + if( p2==0 ){ + *pp = p1; + break; + } } } - *pp = p1 ? p1 : p2; - *ppOut = pFinal; + return pFinal; } /* @@ -1375,32 +1411,28 @@ static SorterCompare vdbeSorterGetCompare(VdbeSorter *p){ if( p->typeMask==SORTER_TYPE_INTEGER ){ return vdbeSorterCompareInt; }else if( p->typeMask==SORTER_TYPE_TEXT ){ - return vdbeSorterCompareText; + return vdbeSorterCompareText; } return vdbeSorterCompare; } /* -** Sort the linked list of records headed at pTask->pList. Return -** SQLITE_OK if successful, or an SQLite error code (i.e. SQLITE_NOMEM) if +** Sort the linked list of records headed at pTask->pList. Return +** SQLITE_OK if successful, or an SQLite error code (i.e. SQLITE_NOMEM) if ** an error occurs. */ static int vdbeSorterSort(SortSubtask *pTask, SorterList *pList){ int i; - SorterRecord **aSlot; SorterRecord *p; int rc; + SorterRecord *aSlot[64]; rc = vdbeSortAllocUnpacked(pTask); if( rc!=SQLITE_OK ) return rc; p = pList->pList; pTask->xCompare = vdbeSorterGetCompare(pTask->pSorter); - - aSlot = (SorterRecord **)sqlite3MallocZero(64 * sizeof(SorterRecord *)); - if( !aSlot ){ - return SQLITE_NOMEM; - } + memset(aSlot, 0, sizeof(aSlot)); while( p ){ SorterRecord *pNext; @@ -1417,7 +1449,11 @@ static int vdbeSorterSort(SortSubtask *pTask, SorterList *pList){ p->u.pNext = 0; for(i=0; aSlot[i]; i++){ - vdbeSorterMerge(pTask, p, aSlot[i], &p); + p = vdbeSorterMerge(pTask, p, aSlot[i]); + /* ,--Each aSlot[] holds twice as much as the previous. So we cannot use + ** | up all 64 aSlots[] with only a 64-bit address space. + ** v */ + assert( i<ArraySize(aSlot) ); aSlot[i] = 0; } aSlot[i] = p; @@ -1425,14 +1461,14 @@ static int vdbeSorterSort(SortSubtask *pTask, SorterList *pList){ } p = 0; - for(i=0; i<64; i++){ - vdbeSorterMerge(pTask, p, aSlot[i], &p); + for(i=0; i<ArraySize(aSlot); i++){ + if( aSlot[i]==0 ) continue; + p = p ? vdbeSorterMerge(pTask, p, aSlot[i]) : aSlot[i]; } pList->pList = p; - sqlite3_free(aSlot); - assert( pTask->pUnpacked->errCode==SQLITE_OK - || pTask->pUnpacked->errCode==SQLITE_NOMEM + assert( pTask->pUnpacked->errCode==SQLITE_OK + || pTask->pUnpacked->errCode==SQLITE_NOMEM ); return pTask->pUnpacked->errCode; } @@ -1449,7 +1485,7 @@ static void vdbePmaWriterInit( memset(p, 0, sizeof(PmaWriter)); p->aBuffer = (u8*)sqlite3Malloc(nBuf); if( !p->aBuffer ){ - p->eFWErr = SQLITE_NOMEM; + p->eFWErr = SQLITE_NOMEM_BKPT; }else{ p->iBufEnd = p->iBufStart = (iStart % nBuf); p->iWriteOff = iStart - p->iBufStart; @@ -1473,10 +1509,11 @@ static void vdbePmaWriteBlob(PmaWriter *p, u8 *pData, int nData){ memcpy(&p->aBuffer[p->iBufEnd], &pData[nData-nRem], nCopy); p->iBufEnd += nCopy; if( p->iBufEnd==p->nBuffer ){ - p->eFWErr = sqlite3OsWrite(p->pFd, - &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart, + p->eFWErr = sqlite3OsWrite(p->pFd, + &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart, p->iWriteOff + p->iBufStart ); + p->nPmaSpill += (p->iBufEnd - p->iBufStart); p->iBufStart = p->iBufEnd = 0; p->iWriteOff += p->nBuffer; } @@ -1489,21 +1526,24 @@ static void vdbePmaWriteBlob(PmaWriter *p, u8 *pData, int nData){ /* ** Flush any buffered data to disk and clean up the PMA-writer object. ** The results of using the PMA-writer after this call are undefined. -** Return SQLITE_OK if flushing the buffered data succeeds or is not +** Return SQLITE_OK if flushing the buffered data succeeds or is not ** required. Otherwise, return an SQLite error code. ** ** Before returning, set *piEof to the offset immediately following the -** last byte written to the file. +** last byte written to the file. Also, increment (*pnSpill) by the total +** number of bytes written to the file. */ -static int vdbePmaWriterFinish(PmaWriter *p, i64 *piEof){ +static int vdbePmaWriterFinish(PmaWriter *p, i64 *piEof, u64 *pnSpill){ int rc; if( p->eFWErr==0 && ALWAYS(p->aBuffer) && p->iBufEnd>p->iBufStart ){ - p->eFWErr = sqlite3OsWrite(p->pFd, - &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart, + p->eFWErr = sqlite3OsWrite(p->pFd, + &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart, p->iWriteOff + p->iBufStart ); + p->nPmaSpill += (p->iBufEnd - p->iBufStart); } *piEof = (p->iWriteOff + p->iBufEnd); + *pnSpill += p->nPmaSpill; sqlite3_free(p->aBuffer); rc = p->eFWErr; memset(p, 0, sizeof(PmaWriter)); @@ -1511,11 +1551,11 @@ static int vdbePmaWriterFinish(PmaWriter *p, i64 *piEof){ } /* -** Write value iVal encoded as a varint to the PMA. Return +** Write value iVal encoded as a varint to the PMA. Return ** SQLITE_OK if successful, or an SQLite error code if an error occurs. */ static void vdbePmaWriteVarint(PmaWriter *p, u64 iVal){ - int nByte; + int nByte; u8 aByte[10]; nByte = sqlite3PutVarint(aByte, iVal); vdbePmaWriteBlob(p, aByte, nByte); @@ -1523,7 +1563,7 @@ static void vdbePmaWriteVarint(PmaWriter *p, u64 iVal){ /* ** Write the current contents of in-memory linked-list pList to a level-0 -** PMA in the temp file belonging to sub-task pTask. Return SQLITE_OK if +** PMA in the temp file belonging to sub-task pTask. Return SQLITE_OK if ** successful, or an SQLite error code otherwise. ** ** The format of a PMA is: @@ -1531,8 +1571,8 @@ static void vdbePmaWriteVarint(PmaWriter *p, u64 iVal){ ** * A varint. This varint contains the total number of bytes of content ** in the PMA (not including the varint itself). ** -** * One or more records packed end-to-end in order of ascending keys. -** Each record consists of a varint followed by a blob of data (the +** * One or more records packed end-to-end in order of ascending keys. +** Each record consists of a varint followed by a blob of data (the ** key). The varint is the number of bytes in the blob of data. */ static int vdbeSorterListToPMA(SortSubtask *pTask, SorterList *pList){ @@ -1541,7 +1581,7 @@ static int vdbeSorterListToPMA(SortSubtask *pTask, SorterList *pList){ PmaWriter writer; /* Object used to write to the file */ #ifdef SQLITE_DEBUG - /* Set iSz to the expected size of file pTask->file after writing the PMA. + /* Set iSz to the expected size of file pTask->file after writing the PMA. ** This is used by an assert() statement at the end of this function. */ i64 iSz = pList->szPMA + sqlite3VarintLen(pList->szPMA) + pTask->file.iEof; #endif @@ -1583,7 +1623,7 @@ static int vdbeSorterListToPMA(SortSubtask *pTask, SorterList *pList){ if( pList->aMemory==0 ) sqlite3_free(p); } pList->pList = p; - rc = vdbePmaWriterFinish(&writer, &pTask->file.iEof); + rc = vdbePmaWriterFinish(&writer, &pTask->file.iEof, &pTask->nSpill); } vdbeSorterWorkDebug(pTask, "exit"); @@ -1694,7 +1734,7 @@ static int vdbeSorterFlushPMA(VdbeSorter *pSorter){ SortSubtask *pTask = 0; /* Thread context used to create new PMA */ int nWorker = (pSorter->nTask-1); - /* Set the flag to indicate that at least one PMA has been written. + /* Set the flag to indicate that at least one PMA has been written. ** Or will be, anyhow. */ pSorter->bUsePMA = 1; @@ -1704,7 +1744,7 @@ static int vdbeSorterFlushPMA(VdbeSorter *pSorter){ ** the background thread from a sub-tasks previous turn is still running, ** skip it. If the first (pSorter->nTask-1) sub-tasks are all still busy, ** fall back to using the final sub-task. The first (pSorter->nTask-1) - ** sub-tasks are prefered as they use background threads - the final + ** sub-tasks are preferred as they use background threads - the final ** sub-task uses the main thread. */ for(i=0; i<nWorker; i++){ int iTest = (pSorter->iPrev + i + 1) % nWorker; @@ -1721,13 +1761,16 @@ static int vdbeSorterFlushPMA(VdbeSorter *pSorter){ rc = vdbeSorterListToPMA(&pSorter->aTask[nWorker], &pSorter->list); }else{ /* Launch a background thread for this operation */ - u8 *aMem = pTask->list.aMemory; - void *pCtx = (void*)pTask; + u8 *aMem; + void *pCtx; + assert( pTask!=0 ); assert( pTask->pThread==0 && pTask->bDone==0 ); assert( pTask->list.pList==0 ); assert( pTask->list.aMemory==0 || pSorter->list.aMemory!=0 ); + aMem = pTask->list.aMemory; + pCtx = (void*)pTask; pSorter->iPrev = (u8)(pTask - pSorter->aTask); pTask->list = pSorter->list; pSorter->list.pList = 0; @@ -1737,7 +1780,7 @@ static int vdbeSorterFlushPMA(VdbeSorter *pSorter){ pSorter->nMemory = sqlite3MallocSize(aMem); }else if( pSorter->list.aMemory ){ pSorter->list.aMemory = sqlite3Malloc(pSorter->nMemory); - if( !pSorter->list.aMemory ) return SQLITE_NOMEM; + if( !pSorter->list.aMemory ) return SQLITE_NOMEM_BKPT; } rc = vdbeSorterCreateThread(pTask, vdbeSorterFlushThread, pCtx); @@ -1759,13 +1802,13 @@ int sqlite3VdbeSorterWrite( int rc = SQLITE_OK; /* Return Code */ SorterRecord *pNew; /* New list element */ int bFlush; /* True to flush contents of memory to PMA */ - int nReq; /* Bytes of memory required */ - int nPMA; /* Bytes of PMA space required */ + i64 nReq; /* Bytes of memory required */ + i64 nPMA; /* Bytes of PMA space required */ int t; /* serial type of first record field */ assert( pCsr->eCurType==CURTYPE_SORTER ); pSorter = pCsr->uc.pSorter; - getVarint32((const u8*)&pVal->z[1], t); + getVarint32NR((const u8*)&pVal->z[1], t); if( t>0 && t<10 && t!=7 ){ pSorter->typeMask &= SORTER_TYPE_INTEGER; }else if( t>10 && (t & 0x01) ){ @@ -1782,14 +1825,14 @@ int sqlite3VdbeSorterWrite( ** If using the single large allocation mode (pSorter->aMemory!=0), then ** flush the contents of memory to a new PMA if (a) at least one value is ** already in memory and (b) the new value will not fit in memory. - ** + ** ** Or, if using separate allocations for each record, flush the contents ** of memory to a PMA if either of the following are true: ** - ** * The total memory allocated for the in-memory list is greater + ** * The total memory allocated for the in-memory list is greater ** than (page-size * cache-size), or ** - ** * The total memory allocated for the in-memory list is greater + ** * The total memory allocated for the in-memory list is greater ** than (page-size * 10) and sqlite3HeapNearlyFull() returns true. */ nReq = pVal->n + sizeof(SorterRecord); @@ -1821,15 +1864,19 @@ int sqlite3VdbeSorterWrite( if( nMin>pSorter->nMemory ){ u8 *aNew; - int iListOff = (u8*)pSorter->list.pList - pSorter->list.aMemory; - int nNew = pSorter->nMemory * 2; + sqlite3_int64 nNew = 2 * (sqlite3_int64)pSorter->nMemory; + int iListOff = -1; + if( pSorter->list.pList ){ + iListOff = (u8*)pSorter->list.pList - pSorter->list.aMemory; + } while( nNew < nMin ) nNew = nNew*2; if( nNew > pSorter->mxPmaSize ) nNew = pSorter->mxPmaSize; if( nNew < nMin ) nNew = nMin; - aNew = sqlite3Realloc(pSorter->list.aMemory, nNew); - if( !aNew ) return SQLITE_NOMEM; - pSorter->list.pList = (SorterRecord*)&aNew[iListOff]; + if( !aNew ) return SQLITE_NOMEM_BKPT; + if( iListOff>=0 ){ + pSorter->list.pList = (SorterRecord*)&aNew[iListOff]; + } pSorter->list.aMemory = aNew; pSorter->nMemory = nNew; } @@ -1842,7 +1889,7 @@ int sqlite3VdbeSorterWrite( }else{ pNew = (SorterRecord *)sqlite3Malloc(nReq); if( pNew==0 ){ - return SQLITE_NOMEM; + return SQLITE_NOMEM_BKPT; } pNew->u.pNext = pSorter->list.pList; } @@ -1890,7 +1937,7 @@ static int vdbeIncrPopulate(IncrMerger *pIncr){ rc = vdbeMergeEngineStep(pIncr->pMerger, &dummy); } - rc2 = vdbePmaWriterFinish(&writer, &pOut->iEof); + rc2 = vdbePmaWriterFinish(&writer, &pOut->iEof, &pTask->nSpill); if( rc==SQLITE_OK ) rc = rc2; vdbeSorterPopulateDebug(pTask, "exit"); return rc; @@ -1924,11 +1971,11 @@ static int vdbeIncrBgPopulate(IncrMerger *pIncr){ ** aFile[0] such that the PmaReader should start rereading it from the ** beginning. ** -** For single-threaded objects, this is accomplished by literally reading -** keys from pIncr->pMerger and repopulating aFile[0]. +** For single-threaded objects, this is accomplished by literally reading +** keys from pIncr->pMerger and repopulating aFile[0]. ** -** For multi-threaded objects, all that is required is to wait until the -** background thread is finished (if it is not already) and then swap +** For multi-threaded objects, all that is required is to wait until the +** background thread is finished (if it is not already) and then swap ** aFile[0] and aFile[1] in place. If the contents of pMerger have not ** been exhausted, this function also launches a new background thread ** to populate the new aFile[1]. @@ -1989,8 +2036,9 @@ static int vdbeIncrMergerNew( pTask->file2.iEof += pIncr->mxSz; }else{ vdbeMergeEngineFree(pMerger); - rc = SQLITE_NOMEM; + rc = SQLITE_NOMEM_BKPT; } + assert( *ppOut!=0 || rc!=SQLITE_OK ); return rc; } @@ -2068,7 +2116,7 @@ static void vdbeMergeEngineCompare( #define INCRINIT_TASK 1 #define INCRINIT_ROOT 2 -/* +/* ** Forward reference required as the vdbeIncrMergeInit() and ** vdbePmaReaderIncrInit() routines are called mutually recursively when ** building a merge tree. @@ -2077,7 +2125,7 @@ static int vdbePmaReaderIncrInit(PmaReader *pReadr, int eMode); /* ** Initialize the MergeEngine object passed as the second argument. Once this -** function returns, the first key of merged data may be read from the +** function returns, the first key of merged data may be read from the ** MergeEngine object in the usual fashion. ** ** If argument eMode is INCRINIT_ROOT, then it is assumed that any IncrMerge @@ -2087,8 +2135,8 @@ static int vdbePmaReaderIncrInit(PmaReader *pReadr, int eMode); ** required is to call vdbePmaReaderNext() on each PmaReader to point it at ** its first key. ** -** Otherwise, if eMode is any value other than INCRINIT_ROOT, then use -** vdbePmaReaderIncrMergeInit() to initialize each PmaReader that feeds data +** Otherwise, if eMode is any value other than INCRINIT_ROOT, then use +** vdbePmaReaderIncrMergeInit() to initialize each PmaReader that feeds data ** to pMerger. ** ** SQLITE_OK is returned if successful, or an SQLite error code otherwise. @@ -2100,7 +2148,11 @@ static int vdbeMergeEngineInit( ){ int rc = SQLITE_OK; /* Return code */ int i; /* For looping over PmaReader objects */ - int nTree = pMerger->nTree; + int nTree; /* Number of subtrees to merge */ + + /* Failure to allocate the merge would have been detected prior to + ** invoking this routine */ + assert( pMerger!=0 ); /* eMode is always INCRINIT_NORMAL in single-threaded mode */ assert( SQLITE_MAX_WORKER_THREADS>0 || eMode==INCRINIT_NORMAL ); @@ -2109,6 +2161,7 @@ static int vdbeMergeEngineInit( assert( pMerger->pTask==0 ); pMerger->pTask = pTask; + nTree = pMerger->nTree; for(i=0; i<nTree; i++){ if( SQLITE_MAX_WORKER_THREADS>0 && eMode==INCRINIT_ROOT ){ /* PmaReaders should be normally initialized in order, as if they are @@ -2138,19 +2191,19 @@ static int vdbeMergeEngineInit( ** object at (pReadr->pIncr). ** ** If argument eMode is set to INCRINIT_NORMAL, then all PmaReaders -** in the sub-tree headed by pReadr are also initialized. Data is then -** loaded into the buffers belonging to pReadr and it is set to point to +** in the sub-tree headed by pReadr are also initialized. Data is then +** loaded into the buffers belonging to pReadr and it is set to point to ** the first key in its range. ** ** If argument eMode is set to INCRINIT_TASK, then pReadr is guaranteed ** to be a multi-threaded PmaReader and this function is being called in a -** background thread. In this case all PmaReaders in the sub-tree are +** background thread. In this case all PmaReaders in the sub-tree are ** initialized as for INCRINIT_NORMAL and the aFile[1] buffer belonging to ** pReadr is populated. However, pReadr itself is not set up to point ** to its first key. A call to vdbePmaReaderNext() is still required to do -** that. +** that. ** -** The reason this function does not call vdbePmaReaderNext() immediately +** The reason this function does not call vdbePmaReaderNext() immediately ** in the INCRINIT_TASK case is that vdbePmaReaderNext() assumes that it has ** to block on thread (pTask->thread) before accessing aFile[1]. But, since ** this entire function is being run by thread (pTask->thread), that will @@ -2175,7 +2228,7 @@ static int vdbePmaReaderIncrMergeInit(PmaReader *pReadr, int eMode){ rc = vdbeMergeEngineInit(pTask, pIncr->pMerger, eMode); - /* Set up the required files for pIncr. A multi-theaded IncrMerge object + /* Set up the required files for pIncr. A multi-threaded IncrMerge object ** requires two temp files to itself, whereas a single-threaded object ** only requires a region of pTask->file2. */ if( rc==SQLITE_OK ){ @@ -2206,12 +2259,12 @@ static int vdbePmaReaderIncrMergeInit(PmaReader *pReadr, int eMode){ if( rc==SQLITE_OK && pIncr->bUseThread ){ /* Use the current thread to populate aFile[1], even though this ** PmaReader is multi-threaded. If this is an INCRINIT_TASK object, - ** then this function is already running in background thread - ** pIncr->pTask->thread. + ** then this function is already running in background thread + ** pIncr->pTask->thread. ** - ** If this is the INCRINIT_ROOT object, then it is running in the + ** If this is the INCRINIT_ROOT object, then it is running in the ** main VDBE thread. But that is Ok, as that thread cannot return - ** control to the VDBE or proceed with anything useful until the + ** control to the VDBE or proceed with anything useful until the ** first results are ready from this merger object anyway. */ assert( eMode==INCRINIT_ROOT || eMode==INCRINIT_TASK ); @@ -2228,7 +2281,7 @@ static int vdbePmaReaderIncrMergeInit(PmaReader *pReadr, int eMode){ #if SQLITE_MAX_WORKER_THREADS>0 /* -** The main routine for vdbePmaReaderIncrMergeInit() operations run in +** The main routine for vdbePmaReaderIncrMergeInit() operations run in ** background threads. */ static void *vdbePmaReaderBgIncrInit(void *pCtx){ @@ -2246,8 +2299,8 @@ static void *vdbePmaReaderBgIncrInit(void *pCtx){ ** (if pReadr->pIncr==0), then this function is a no-op. Otherwise, it invokes ** the vdbePmaReaderIncrMergeInit() function with the parameters passed to ** this routine to initialize the incremental merge. -** -** If the IncrMerger object is multi-threaded (IncrMerger.bUseThread==1), +** +** If the IncrMerger object is multi-threaded (IncrMerger.bUseThread==1), ** then a background thread is launched to call vdbePmaReaderIncrMergeInit(). ** Or, if the IncrMerger is single threaded, the same function is called ** using the current thread. @@ -2277,7 +2330,7 @@ static int vdbePmaReaderIncrInit(PmaReader *pReadr, int eMode){ ** to NULL and return an SQLite error code. ** ** When this function is called, *piOffset is set to the offset of the -** first PMA to read from pTask->file. Assuming no error occurs, it is +** first PMA to read from pTask->file. Assuming no error occurs, it is ** set to the offset immediately following the last byte of the last ** PMA before returning. If an error does occur, then the final value of ** *piOffset is undefined. @@ -2294,10 +2347,10 @@ static int vdbeMergeEngineLevel0( int rc = SQLITE_OK; *ppOut = pNew = vdbeMergeEngineNew(nPMA); - if( pNew==0 ) rc = SQLITE_NOMEM; + if( pNew==0 ) rc = SQLITE_NOMEM_BKPT; for(i=0; i<nPMA && rc==SQLITE_OK; i++){ - i64 nDummy; + i64 nDummy = 0; PmaReader *pReadr = &pNew->aReadr[i]; rc = vdbePmaReaderInit(pTask, &pTask->file, iOff, pReadr, &nDummy); iOff = pReadr->iEof; @@ -2365,7 +2418,7 @@ static int vdbeSorterAddToTree( if( pReadr->pIncr==0 ){ MergeEngine *pNew = vdbeMergeEngineNew(SORTER_MAX_MERGE_COUNT); if( pNew==0 ){ - rc = SQLITE_NOMEM; + rc = SQLITE_NOMEM_BKPT; }else{ rc = vdbeIncrMergerNew(pTask, pNew, &pReadr->pIncr); } @@ -2387,12 +2440,12 @@ static int vdbeSorterAddToTree( /* ** This function is called as part of a SorterRewind() operation on a sorter ** that has already written two or more level-0 PMAs to one or more temp -** files. It builds a tree of MergeEngine/IncrMerger/PmaReader objects that +** files. It builds a tree of MergeEngine/IncrMerger/PmaReader objects that ** can be used to incrementally merge all PMAs on disk. ** ** If successful, SQLITE_OK is returned and *ppOut set to point to the ** MergeEngine object at the root of the tree before returning. Or, if an -** error occurs, an SQLite error code is returned and the final value +** error occurs, an SQLite error code is returned and the final value ** of *ppOut is undefined. */ static int vdbeSorterMergeTreeBuild( @@ -2404,13 +2457,13 @@ static int vdbeSorterMergeTreeBuild( int iTask; #if SQLITE_MAX_WORKER_THREADS>0 - /* If the sorter uses more than one task, then create the top-level - ** MergeEngine here. This MergeEngine will read data from exactly + /* If the sorter uses more than one task, then create the top-level + ** MergeEngine here. This MergeEngine will read data from exactly ** one PmaReader per sub-task. */ assert( pSorter->bUseThreads || pSorter->nTask==1 ); if( pSorter->nTask>1 ){ pMain = vdbeMergeEngineNew(pSorter->nTask); - if( pMain==0 ) rc = SQLITE_NOMEM; + if( pMain==0 ) rc = SQLITE_NOMEM_BKPT; } #endif @@ -2428,7 +2481,7 @@ static int vdbeSorterMergeTreeBuild( int i; int iSeq = 0; pRoot = vdbeMergeEngineNew(SORTER_MAX_MERGE_COUNT); - if( pRoot==0 ) rc = SQLITE_NOMEM; + if( pRoot==0 ) rc = SQLITE_NOMEM_BKPT; for(i=0; i<pTask->nPMA && rc==SQLITE_OK; i += SORTER_MAX_MERGE_COUNT){ MergeEngine *pMerger = 0; /* New level-0 PMA merger */ int nReader; /* Number of level-0 PMAs to merge */ @@ -2499,7 +2552,7 @@ static int vdbeSorterSetupMerge(VdbeSorter *pSorter){ if( rc==SQLITE_OK ){ pReadr = (PmaReader*)sqlite3DbMallocZero(db, sizeof(PmaReader)); pSorter->pReader = pReadr; - if( pReadr==0 ) rc = SQLITE_NOMEM; + if( pReadr==0 ) rc = SQLITE_NOMEM_BKPT; } if( rc==SQLITE_OK ){ rc = vdbeIncrMergerNew(pLast, pMain, &pReadr->pIncr); @@ -2514,7 +2567,7 @@ static int vdbeSorterSetupMerge(VdbeSorter *pSorter){ } for(iTask=0; rc==SQLITE_OK && iTask<pSorter->nTask; iTask++){ /* Check that: - ** + ** ** a) The incremental merge object is configured to use the ** right task, and ** b) If it is using task (nTask-1), it is configured to run @@ -2577,7 +2630,7 @@ int sqlite3VdbeSorterRewind(const VdbeCursor *pCsr, int *pbEof){ return rc; } - /* Write the current in-memory list to a PMA. When the VdbeSorterWrite() + /* Write the current in-memory list to a PMA. When the VdbeSorterWrite() ** function flushes the contents of memory to disk, it immediately always ** creates a new list consisting of a single key immediately afterwards. ** So the list is never empty at this point. */ @@ -2589,7 +2642,7 @@ int sqlite3VdbeSorterRewind(const VdbeCursor *pCsr, int *pbEof){ vdbeSorterRewindDebug("rewind"); - /* Assuming no errors have occurred, set up a merger structure to + /* Assuming no errors have occurred, set up a merger structure to ** incrementally read and merge all remaining PMAs. */ assert( pSorter->pReader==0 ); if( rc==SQLITE_OK ){ @@ -2602,9 +2655,13 @@ int sqlite3VdbeSorterRewind(const VdbeCursor *pCsr, int *pbEof){ } /* -** Advance to the next element in the sorter. +** Advance to the next element in the sorter. Return value: +** +** SQLITE_OK success +** SQLITE_DONE end of data +** otherwise some kind of error. */ -int sqlite3VdbeSorterNext(sqlite3 *db, const VdbeCursor *pCsr, int *pbEof){ +int sqlite3VdbeSorterNext(sqlite3 *db, const VdbeCursor *pCsr){ VdbeSorter *pSorter; int rc; /* Return code */ @@ -2618,27 +2675,28 @@ int sqlite3VdbeSorterNext(sqlite3 *db, const VdbeCursor *pCsr, int *pbEof){ #if SQLITE_MAX_WORKER_THREADS>0 if( pSorter->bUseThreads ){ rc = vdbePmaReaderNext(pSorter->pReader); - *pbEof = (pSorter->pReader->pFd==0); + if( rc==SQLITE_OK && pSorter->pReader->pFd==0 ) rc = SQLITE_DONE; }else #endif /*if( !pSorter->bUseThreads )*/ { + int res = 0; assert( pSorter->pMerger!=0 ); assert( pSorter->pMerger->pTask==(&pSorter->aTask[0]) ); - rc = vdbeMergeEngineStep(pSorter->pMerger, pbEof); + rc = vdbeMergeEngineStep(pSorter->pMerger, &res); + if( rc==SQLITE_OK && res ) rc = SQLITE_DONE; } }else{ SorterRecord *pFree = pSorter->list.pList; pSorter->list.pList = pFree->u.pNext; pFree->u.pNext = 0; if( pSorter->list.aMemory==0 ) vdbeSorterRecordFree(db, pFree); - *pbEof = !pSorter->list.pList; - rc = SQLITE_OK; + rc = pSorter->list.pList ? SQLITE_OK : SQLITE_DONE; } return rc; } /* -** Return a pointer to a buffer owned by the sorter that contains the +** Return a pointer to a buffer owned by the sorter that contains the ** current key. */ static void *vdbeSorterRowkey( @@ -2676,7 +2734,7 @@ int sqlite3VdbeSorterRowkey(const VdbeCursor *pCsr, Mem *pOut){ pSorter = pCsr->uc.pSorter; pKey = vdbeSorterRowkey(pSorter, &nKey); if( sqlite3VdbeMemClearAndResize(pOut, nKey) ){ - return SQLITE_NOMEM; + return SQLITE_NOMEM_BKPT; } pOut->n = nKey; MemSetTypeFlag(pOut, MEM_Blob); @@ -2718,16 +2776,14 @@ int sqlite3VdbeSorterCompare( r2 = pSorter->pUnpacked; pKeyInfo = pCsr->pKeyInfo; if( r2==0 ){ - char *p; - r2 = pSorter->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pKeyInfo,0,0,&p); - assert( pSorter->pUnpacked==(UnpackedRecord*)p ); - if( r2==0 ) return SQLITE_NOMEM; + r2 = pSorter->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pKeyInfo); + if( r2==0 ) return SQLITE_NOMEM_BKPT; r2->nField = nKeyCol; } assert( r2->nField==nKeyCol ); pKey = vdbeSorterRowkey(pSorter, &nKey); - sqlite3VdbeRecordUnpack(pKeyInfo, nKey, pKey, r2); + sqlite3VdbeRecordUnpack(nKey, pKey, r2); for(i=0; i<nKeyCol; i++){ if( r2->aMem[i].flags & MEM_Null ){ *pRes = -1; diff --git a/src/vdbetrace.c b/src/vdbetrace.c index 07235c931b..1a59f0e4d5 100644 --- a/src/vdbetrace.c +++ b/src/vdbetrace.c @@ -26,10 +26,10 @@ ** a host parameter. If the text contains no host parameters, return ** the total number of bytes in the text. */ -static int findNextHostParameter(const char *zSql, int *pnToken){ +static i64 findNextHostParameter(const char *zSql, i64 *pnToken){ int tokenType; - int nTotal = 0; - int n; + i64 nTotal = 0; + i64 n; *pnToken = 0; while( zSql[0] ){ @@ -76,31 +76,32 @@ char *sqlite3VdbeExpandSql( sqlite3 *db; /* The database connection */ int idx = 0; /* Index of a host parameter */ int nextIndex = 1; /* Index of next ? host parameter */ - int n; /* Length of a token prefix */ - int nToken; /* Length of the parameter token */ + i64 n; /* Length of a token prefix */ + i64 nToken; /* Length of the parameter token */ int i; /* Loop counter */ Mem *pVar; /* Value of a host parameter */ StrAccum out; /* Accumulate the output here */ - char zBase[100]; /* Initial working space */ +#ifndef SQLITE_OMIT_UTF16 + Mem utf8; /* Used to convert UTF16 into UTF8 for display */ +#endif db = p->db; - sqlite3StrAccumInit(&out, db, zBase, sizeof(zBase), - db->aLimit[SQLITE_LIMIT_LENGTH]); + sqlite3StrAccumInit(&out, 0, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]); if( db->nVdbeExec>1 ){ while( *zRawSql ){ const char *zStart = zRawSql; while( *(zRawSql++)!='\n' && *zRawSql ); - sqlite3StrAccumAppend(&out, "-- ", 3); + sqlite3_str_append(&out, "-- ", 3); assert( (zRawSql - zStart) > 0 ); - sqlite3StrAccumAppend(&out, zStart, (int)(zRawSql-zStart)); + sqlite3_str_append(&out, zStart, (int)(zRawSql-zStart)); } }else if( p->nVar==0 ){ - sqlite3StrAccumAppend(&out, zRawSql, sqlite3Strlen30(zRawSql)); + sqlite3_str_append(&out, zRawSql, sqlite3Strlen30(zRawSql)); }else{ while( zRawSql[0] ){ n = findNextHostParameter(zRawSql, &nToken); assert( n>0 ); - sqlite3StrAccumAppend(&out, zRawSql, n); + sqlite3_str_append(&out, zRawSql, n); zRawSql += n; assert( zRawSql[0] || nToken==0 ); if( nToken==0 ) break; @@ -122,25 +123,27 @@ char *sqlite3VdbeExpandSql( assert( idx>0 ); } zRawSql += nToken; - nextIndex = idx + 1; + nextIndex = MAX(idx + 1, nextIndex); assert( idx>0 && idx<=p->nVar ); pVar = &p->aVar[idx-1]; if( pVar->flags & MEM_Null ){ - sqlite3StrAccumAppend(&out, "NULL", 4); - }else if( pVar->flags & MEM_Int ){ - sqlite3XPrintf(&out, "%lld", pVar->u.i); + sqlite3_str_append(&out, "NULL", 4); + }else if( pVar->flags & (MEM_Int|MEM_IntReal) ){ + sqlite3_str_appendf(&out, "%lld", pVar->u.i); }else if( pVar->flags & MEM_Real ){ - sqlite3XPrintf(&out, "%!.15g", pVar->u.r); + sqlite3_str_appendf(&out, "%!.15g", pVar->u.r); }else if( pVar->flags & MEM_Str ){ int nOut; /* Number of bytes of the string text to include in output */ #ifndef SQLITE_OMIT_UTF16 u8 enc = ENC(db); - Mem utf8; if( enc!=SQLITE_UTF8 ){ memset(&utf8, 0, sizeof(utf8)); utf8.db = db; sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC); - sqlite3VdbeChangeEncoding(&utf8, SQLITE_UTF8); + if( SQLITE_NOMEM==sqlite3VdbeChangeEncoding(&utf8, SQLITE_UTF8) ){ + out.accError = SQLITE_NOMEM; + out.nAlloc = 0; + } pVar = &utf8; } #endif @@ -151,37 +154,38 @@ char *sqlite3VdbeExpandSql( while( nOut<pVar->n && (pVar->z[nOut]&0xc0)==0x80 ){ nOut++; } } #endif - sqlite3XPrintf(&out, "'%.*q'", nOut, pVar->z); + sqlite3_str_appendf(&out, "'%.*q'", nOut, pVar->z); #ifdef SQLITE_TRACE_SIZE_LIMIT if( nOut<pVar->n ){ - sqlite3XPrintf(&out, "/*+%d bytes*/", pVar->n-nOut); + sqlite3_str_appendf(&out, "/*+%d bytes*/", pVar->n-nOut); } #endif #ifndef SQLITE_OMIT_UTF16 if( enc!=SQLITE_UTF8 ) sqlite3VdbeMemRelease(&utf8); #endif }else if( pVar->flags & MEM_Zero ){ - sqlite3XPrintf(&out, "zeroblob(%d)", pVar->u.nZero); + sqlite3_str_appendf(&out, "zeroblob(%d)", pVar->u.nZero); }else{ int nOut; /* Number of bytes of the blob to include in output */ assert( pVar->flags & MEM_Blob ); - sqlite3StrAccumAppend(&out, "x'", 2); + sqlite3_str_append(&out, "x'", 2); nOut = pVar->n; #ifdef SQLITE_TRACE_SIZE_LIMIT if( nOut>SQLITE_TRACE_SIZE_LIMIT ) nOut = SQLITE_TRACE_SIZE_LIMIT; #endif for(i=0; i<nOut; i++){ - sqlite3XPrintf(&out, "%02x", pVar->z[i]&0xff); + sqlite3_str_appendf(&out, "%02x", pVar->z[i]&0xff); } - sqlite3StrAccumAppend(&out, "'", 1); + sqlite3_str_append(&out, "'", 1); #ifdef SQLITE_TRACE_SIZE_LIMIT if( nOut<pVar->n ){ - sqlite3XPrintf(&out, "/*+%d bytes*/", pVar->n-nOut); + sqlite3_str_appendf(&out, "/*+%d bytes*/", pVar->n-nOut); } #endif } } } + if( out.accError ) sqlite3_str_reset(&out); return sqlite3StrAccumFinish(&out); } diff --git a/src/vdbevtab.c b/src/vdbevtab.c new file mode 100644 index 0000000000..1c9909a1aa --- /dev/null +++ b/src/vdbevtab.c @@ -0,0 +1,446 @@ +/* +** 2020-03-23 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** This file implements virtual-tables for examining the bytecode content +** of a prepared statement. +*/ +#include "sqliteInt.h" +#if defined(SQLITE_ENABLE_BYTECODE_VTAB) && !defined(SQLITE_OMIT_VIRTUALTABLE) +#include "vdbeInt.h" + +/* An instance of the bytecode() table-valued function. +*/ +typedef struct bytecodevtab bytecodevtab; +struct bytecodevtab { + sqlite3_vtab base; /* Base class - must be first */ + sqlite3 *db; /* Database connection */ + int bTablesUsed; /* 2 for tables_used(). 0 for bytecode(). */ +}; + +/* A cursor for scanning through the bytecode +*/ +typedef struct bytecodevtab_cursor bytecodevtab_cursor; +struct bytecodevtab_cursor { + sqlite3_vtab_cursor base; /* Base class - must be first */ + sqlite3_stmt *pStmt; /* The statement whose bytecode is displayed */ + int iRowid; /* The rowid of the output table */ + int iAddr; /* Address */ + int needFinalize; /* Cursors owns pStmt and must finalize it */ + int showSubprograms; /* Provide a listing of subprograms */ + Op *aOp; /* Operand array */ + char *zP4; /* Rendered P4 value */ + const char *zType; /* tables_used.type */ + const char *zSchema; /* tables_used.schema */ + const char *zName; /* tables_used.name */ + Mem sub; /* Subprograms */ +}; + +/* +** Create a new bytecode() table-valued function. +*/ +static int bytecodevtabConnect( + sqlite3 *db, + void *pAux, + int argc, const char *const*argv, + sqlite3_vtab **ppVtab, + char **pzErr +){ + bytecodevtab *pNew; + int rc; + int isTabUsed = pAux!=0; + const char *azSchema[2] = { + /* bytecode() schema */ + "CREATE TABLE x(" + "addr INT," + "opcode TEXT," + "p1 INT," + "p2 INT," + "p3 INT," + "p4 TEXT," + "p5 INT," + "comment TEXT," + "subprog TEXT," + "nexec INT," + "ncycle INT," + "stmt HIDDEN" + ");", + + /* Tables_used() schema */ + "CREATE TABLE x(" + "type TEXT," + "schema TEXT," + "name TEXT," + "wr INT," + "subprog TEXT," + "stmt HIDDEN" + ");" + }; + + (void)argc; + (void)argv; + (void)pzErr; + rc = sqlite3_declare_vtab(db, azSchema[isTabUsed]); + if( rc==SQLITE_OK ){ + pNew = sqlite3_malloc( sizeof(*pNew) ); + *ppVtab = (sqlite3_vtab*)pNew; + if( pNew==0 ) return SQLITE_NOMEM; + memset(pNew, 0, sizeof(*pNew)); + pNew->db = db; + pNew->bTablesUsed = isTabUsed*2; + } + return rc; +} + +/* +** This method is the destructor for bytecodevtab objects. +*/ +static int bytecodevtabDisconnect(sqlite3_vtab *pVtab){ + bytecodevtab *p = (bytecodevtab*)pVtab; + sqlite3_free(p); + return SQLITE_OK; +} + +/* +** Constructor for a new bytecodevtab_cursor object. +*/ +static int bytecodevtabOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){ + bytecodevtab *pVTab = (bytecodevtab*)p; + bytecodevtab_cursor *pCur; + pCur = sqlite3_malloc( sizeof(*pCur) ); + if( pCur==0 ) return SQLITE_NOMEM; + memset(pCur, 0, sizeof(*pCur)); + sqlite3VdbeMemInit(&pCur->sub, pVTab->db, 1); + *ppCursor = &pCur->base; + return SQLITE_OK; +} + +/* +** Clear all internal content from a bytecodevtab cursor. +*/ +static void bytecodevtabCursorClear(bytecodevtab_cursor *pCur){ + sqlite3_free(pCur->zP4); + pCur->zP4 = 0; + sqlite3VdbeMemRelease(&pCur->sub); + sqlite3VdbeMemSetNull(&pCur->sub); + if( pCur->needFinalize ){ + sqlite3_finalize(pCur->pStmt); + } + pCur->pStmt = 0; + pCur->needFinalize = 0; + pCur->zType = 0; + pCur->zSchema = 0; + pCur->zName = 0; +} + +/* +** Destructor for a bytecodevtab_cursor. +*/ +static int bytecodevtabClose(sqlite3_vtab_cursor *cur){ + bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur; + bytecodevtabCursorClear(pCur); + sqlite3_free(pCur); + return SQLITE_OK; +} + + +/* +** Advance a bytecodevtab_cursor to its next row of output. +*/ +static int bytecodevtabNext(sqlite3_vtab_cursor *cur){ + bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur; + bytecodevtab *pTab = (bytecodevtab*)cur->pVtab; + int rc; + if( pCur->zP4 ){ + sqlite3_free(pCur->zP4); + pCur->zP4 = 0; + } + if( pCur->zName ){ + pCur->zName = 0; + pCur->zType = 0; + pCur->zSchema = 0; + } + rc = sqlite3VdbeNextOpcode( + (Vdbe*)pCur->pStmt, + pCur->showSubprograms ? &pCur->sub : 0, + pTab->bTablesUsed, + &pCur->iRowid, + &pCur->iAddr, + &pCur->aOp); + if( rc!=SQLITE_OK ){ + sqlite3VdbeMemSetNull(&pCur->sub); + pCur->aOp = 0; + } + return SQLITE_OK; +} + +/* +** Return TRUE if the cursor has been moved off of the last +** row of output. +*/ +static int bytecodevtabEof(sqlite3_vtab_cursor *cur){ + bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur; + return pCur->aOp==0; +} + +/* +** Return values of columns for the row at which the bytecodevtab_cursor +** is currently pointing. +*/ +static int bytecodevtabColumn( + sqlite3_vtab_cursor *cur, /* The cursor */ + sqlite3_context *ctx, /* First argument to sqlite3_result_...() */ + int i /* Which column to return */ +){ + bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur; + bytecodevtab *pVTab = (bytecodevtab*)cur->pVtab; + Op *pOp = pCur->aOp + pCur->iAddr; + if( pVTab->bTablesUsed ){ + if( i==4 ){ + i = 8; + }else{ + if( i<=2 && pCur->zType==0 ){ + Schema *pSchema; + HashElem *k; + int iDb = pOp->p3; + Pgno iRoot = (Pgno)pOp->p2; + sqlite3 *db = pVTab->db; + pSchema = db->aDb[iDb].pSchema; + pCur->zSchema = db->aDb[iDb].zDbSName; + for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){ + Table *pTab = (Table*)sqliteHashData(k); + if( !IsVirtual(pTab) && pTab->tnum==iRoot ){ + pCur->zName = pTab->zName; + pCur->zType = "table"; + break; + } + } + if( pCur->zName==0 ){ + for(k=sqliteHashFirst(&pSchema->idxHash); k; k=sqliteHashNext(k)){ + Index *pIdx = (Index*)sqliteHashData(k); + if( pIdx->tnum==iRoot ){ + pCur->zName = pIdx->zName; + pCur->zType = "index"; + } + } + } + } + i += 20; + } + } + switch( i ){ + case 0: /* addr */ + sqlite3_result_int(ctx, pCur->iAddr); + break; + case 1: /* opcode */ + sqlite3_result_text(ctx, (char*)sqlite3OpcodeName(pOp->opcode), + -1, SQLITE_STATIC); + break; + case 2: /* p1 */ + sqlite3_result_int(ctx, pOp->p1); + break; + case 3: /* p2 */ + sqlite3_result_int(ctx, pOp->p2); + break; + case 4: /* p3 */ + sqlite3_result_int(ctx, pOp->p3); + break; + case 5: /* p4 */ + case 7: /* comment */ + if( pCur->zP4==0 ){ + pCur->zP4 = sqlite3VdbeDisplayP4(pVTab->db, pOp); + } + if( i==5 ){ + sqlite3_result_text(ctx, pCur->zP4, -1, SQLITE_STATIC); + }else{ +#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS + char *zCom = sqlite3VdbeDisplayComment(pVTab->db, pOp, pCur->zP4); + sqlite3_result_text(ctx, zCom, -1, sqlite3_free); +#endif + } + break; + case 6: /* p5 */ + sqlite3_result_int(ctx, pOp->p5); + break; + case 8: { /* subprog */ + Op *aOp = pCur->aOp; + assert( aOp[0].opcode==OP_Init ); + assert( aOp[0].p4.z==0 || strncmp(aOp[0].p4.z,"-" "- ",3)==0 ); + if( pCur->iRowid==pCur->iAddr+1 ){ + break; /* Result is NULL for the main program */ + }else if( aOp[0].p4.z!=0 ){ + sqlite3_result_text(ctx, aOp[0].p4.z+3, -1, SQLITE_STATIC); + }else{ + sqlite3_result_text(ctx, "(FK)", 4, SQLITE_STATIC); + } + break; + } + +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS + case 9: /* nexec */ + sqlite3_result_int64(ctx, pOp->nExec); + break; + case 10: /* ncycle */ + sqlite3_result_int64(ctx, pOp->nCycle); + break; +#else + case 9: /* nexec */ + case 10: /* ncycle */ + sqlite3_result_int(ctx, 0); + break; +#endif + + case 20: /* tables_used.type */ + sqlite3_result_text(ctx, pCur->zType, -1, SQLITE_STATIC); + break; + case 21: /* tables_used.schema */ + sqlite3_result_text(ctx, pCur->zSchema, -1, SQLITE_STATIC); + break; + case 22: /* tables_used.name */ + sqlite3_result_text(ctx, pCur->zName, -1, SQLITE_STATIC); + break; + case 23: /* tables_used.wr */ + sqlite3_result_int(ctx, pOp->opcode==OP_OpenWrite); + break; + } + return SQLITE_OK; +} + +/* +** Return the rowid for the current row. In this implementation, the +** rowid is the same as the output value. +*/ +static int bytecodevtabRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ + bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur; + *pRowid = pCur->iRowid; + return SQLITE_OK; +} + +/* +** Initialize a cursor. +** +** idxNum==0 means show all subprograms +** idxNum==1 means show only the main bytecode and omit subprograms. +*/ +static int bytecodevtabFilter( + sqlite3_vtab_cursor *pVtabCursor, + int idxNum, const char *idxStr, + int argc, sqlite3_value **argv +){ + bytecodevtab_cursor *pCur = (bytecodevtab_cursor *)pVtabCursor; + bytecodevtab *pVTab = (bytecodevtab *)pVtabCursor->pVtab; + int rc = SQLITE_OK; + (void)idxStr; + + bytecodevtabCursorClear(pCur); + pCur->iRowid = 0; + pCur->iAddr = 0; + pCur->showSubprograms = idxNum==0; + assert( argc==1 ); + if( sqlite3_value_type(argv[0])==SQLITE_TEXT ){ + const char *zSql = (const char*)sqlite3_value_text(argv[0]); + if( zSql==0 ){ + rc = SQLITE_NOMEM; + }else{ + rc = sqlite3_prepare_v2(pVTab->db, zSql, -1, &pCur->pStmt, 0); + pCur->needFinalize = 1; + } + }else{ + pCur->pStmt = (sqlite3_stmt*)sqlite3_value_pointer(argv[0],"stmt-pointer"); + } + if( pCur->pStmt==0 ){ + pVTab->base.zErrMsg = sqlite3_mprintf( + "argument to %s() is not a valid SQL statement", + pVTab->bTablesUsed ? "tables_used" : "bytecode" + ); + rc = SQLITE_ERROR; + }else{ + bytecodevtabNext(pVtabCursor); + } + return rc; +} + +/* +** We must have a single stmt=? constraint that will be passed through +** into the xFilter method. If there is no valid stmt=? constraint, +** then return an SQLITE_CONSTRAINT error. +*/ +static int bytecodevtabBestIndex( + sqlite3_vtab *tab, + sqlite3_index_info *pIdxInfo +){ + int i; + int rc = SQLITE_CONSTRAINT; + struct sqlite3_index_constraint *p; + bytecodevtab *pVTab = (bytecodevtab*)tab; + int iBaseCol = pVTab->bTablesUsed ? 4 : 10; + pIdxInfo->estimatedCost = (double)100; + pIdxInfo->estimatedRows = 100; + pIdxInfo->idxNum = 0; + for(i=0, p=pIdxInfo->aConstraint; i<pIdxInfo->nConstraint; i++, p++){ + if( p->usable==0 ) continue; + if( p->op==SQLITE_INDEX_CONSTRAINT_EQ && p->iColumn==iBaseCol+1 ){ + rc = SQLITE_OK; + pIdxInfo->aConstraintUsage[i].omit = 1; + pIdxInfo->aConstraintUsage[i].argvIndex = 1; + } + if( p->op==SQLITE_INDEX_CONSTRAINT_ISNULL && p->iColumn==iBaseCol ){ + pIdxInfo->aConstraintUsage[i].omit = 1; + pIdxInfo->idxNum = 1; + } + } + return rc; +} + +/* +** This following structure defines all the methods for the +** virtual table. +*/ +static sqlite3_module bytecodevtabModule = { + /* iVersion */ 0, + /* xCreate */ 0, + /* xConnect */ bytecodevtabConnect, + /* xBestIndex */ bytecodevtabBestIndex, + /* xDisconnect */ bytecodevtabDisconnect, + /* xDestroy */ 0, + /* xOpen */ bytecodevtabOpen, + /* xClose */ bytecodevtabClose, + /* xFilter */ bytecodevtabFilter, + /* xNext */ bytecodevtabNext, + /* xEof */ bytecodevtabEof, + /* xColumn */ bytecodevtabColumn, + /* xRowid */ bytecodevtabRowid, + /* xUpdate */ 0, + /* xBegin */ 0, + /* xSync */ 0, + /* xCommit */ 0, + /* xRollback */ 0, + /* xFindMethod */ 0, + /* xRename */ 0, + /* xSavepoint */ 0, + /* xRelease */ 0, + /* xRollbackTo */ 0, + /* xShadowName */ 0, + /* xIntegrity */ 0 +}; + + +int sqlite3VdbeBytecodeVtabInit(sqlite3 *db){ + int rc; + rc = sqlite3_create_module(db, "bytecode", &bytecodevtabModule, 0); + if( rc==SQLITE_OK ){ + rc = sqlite3_create_module(db, "tables_used", &bytecodevtabModule, &db); + } + return rc; +} +#elif defined(SQLITE_ENABLE_BYTECODE_VTAB) +int sqlite3VdbeBytecodeVtabInit(sqlite3 *db){ return SQLITE_OK; } +#endif /* SQLITE_ENABLE_BYTECODE_VTAB */ diff --git a/src/vtab.c b/src/vtab.c index e8794e687d..ed4b0afaf4 100644 --- a/src/vtab.c +++ b/src/vtab.c @@ -17,7 +17,7 @@ /* ** Before a virtual table xCreate() or xConnect() method is invoked, the ** sqlite3.pVtabCtx member variable is set to point to an instance of -** this struct allocated on the stack. It is used by the implementation of +** this struct allocated on the stack. It is used by the implementation of ** the sqlite3_declare_vtab() and sqlite3_vtab_config() APIs, both of which ** are invoked only from within xCreate and xConnect methods. */ @@ -28,6 +28,57 @@ struct VtabCtx { int bDeclared; /* True after sqlite3_declare_vtab() is called */ }; +/* +** Construct and install a Module object for a virtual table. When this +** routine is called, it is guaranteed that all appropriate locks are held +** and the module is not already part of the connection. +** +** If there already exists a module with zName, replace it with the new one. +** If pModule==0, then delete the module zName if it exists. +*/ +Module *sqlite3VtabCreateModule( + sqlite3 *db, /* Database in which module is registered */ + const char *zName, /* Name assigned to this module */ + const sqlite3_module *pModule, /* The definition of the module */ + void *pAux, /* Context pointer for xCreate/xConnect */ + void (*xDestroy)(void *) /* Module destructor function */ +){ + Module *pMod; + Module *pDel; + char *zCopy; + if( pModule==0 ){ + zCopy = (char*)zName; + pMod = 0; + }else{ + int nName = sqlite3Strlen30(zName); + pMod = (Module *)sqlite3Malloc(sizeof(Module) + nName + 1); + if( pMod==0 ){ + sqlite3OomFault(db); + return 0; + } + zCopy = (char *)(&pMod[1]); + memcpy(zCopy, zName, nName+1); + pMod->zName = zCopy; + pMod->pModule = pModule; + pMod->pAux = pAux; + pMod->xDestroy = xDestroy; + pMod->pEpoTab = 0; + pMod->nRefModule = 1; + } + pDel = (Module *)sqlite3HashInsert(&db->aModule,zCopy,(void*)pMod); + if( pDel ){ + if( pDel==pMod ){ + sqlite3OomFault(db); + sqlite3DbFree(db, pDel); + pMod = 0; + }else{ + sqlite3VtabEponymousTableClear(db, pDel); + sqlite3VtabModuleUnref(db, pDel); + } + } + return pMod; +} + /* ** The actual function that does the work of creating a new module. ** This function implements the sqlite3_create_module() and @@ -41,35 +92,11 @@ static int createModule( void (*xDestroy)(void *) /* Module destructor function */ ){ int rc = SQLITE_OK; - int nName; sqlite3_mutex_enter(db->mutex); - nName = sqlite3Strlen30(zName); - if( sqlite3HashFind(&db->aModule, zName) ){ - rc = SQLITE_MISUSE_BKPT; - }else{ - Module *pMod; - pMod = (Module *)sqlite3DbMallocRawNN(db, sizeof(Module) + nName + 1); - if( pMod ){ - Module *pDel; - char *zCopy = (char *)(&pMod[1]); - memcpy(zCopy, zName, nName+1); - pMod->zName = zCopy; - pMod->pModule = pModule; - pMod->pAux = pAux; - pMod->xDestroy = xDestroy; - pMod->pEpoTab = 0; - pDel = (Module *)sqlite3HashInsert(&db->aModule,zCopy,(void*)pMod); - assert( pDel==0 || pDel==pMod ); - if( pDel ){ - sqlite3OomFault(db); - sqlite3DbFree(db, pDel); - } - } - } + (void)sqlite3VtabCreateModule(db, zName, pModule, pAux, xDestroy); rc = sqlite3ApiExit(db, rc); if( rc!=SQLITE_OK && xDestroy ) xDestroy(pAux); - sqlite3_mutex_leave(db->mutex); return rc; } @@ -106,10 +133,48 @@ int sqlite3_create_module_v2( return createModule(db, zName, pModule, pAux, xDestroy); } +/* +** External API to drop all virtual-table modules, except those named +** on the azNames list. +*/ +int sqlite3_drop_modules(sqlite3 *db, const char** azNames){ + HashElem *pThis, *pNext; +#ifdef SQLITE_ENABLE_API_ARMOR + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; +#endif + for(pThis=sqliteHashFirst(&db->aModule); pThis; pThis=pNext){ + Module *pMod = (Module*)sqliteHashData(pThis); + pNext = sqliteHashNext(pThis); + if( azNames ){ + int ii; + for(ii=0; azNames[ii]!=0 && strcmp(azNames[ii],pMod->zName)!=0; ii++){} + if( azNames[ii]!=0 ) continue; + } + createModule(db, pMod->zName, 0, 0, 0); + } + return SQLITE_OK; +} + +/* +** Decrement the reference count on a Module object. Destroy the +** module when the reference count reaches zero. +*/ +void sqlite3VtabModuleUnref(sqlite3 *db, Module *pMod){ + assert( pMod->nRefModule>0 ); + pMod->nRefModule--; + if( pMod->nRefModule==0 ){ + if( pMod->xDestroy ){ + pMod->xDestroy(pMod->pAux); + } + assert( pMod->pEpoTab==0 ); + sqlite3DbFree(db, pMod); + } +} + /* ** Lock the virtual table so that it cannot be disconnected. ** Locks nest. Every lock should have a corresponding unlock. -** If an unlock is omitted, resources leaks will occur. +** If an unlock is omitted, resources leaks will occur. ** ** If a disconnect is attempted while a virtual table is locked, ** the disconnect is deferred until all locks have been removed. @@ -121,13 +186,13 @@ void sqlite3VtabLock(VTable *pVTab){ /* ** pTab is a pointer to a Table structure representing a virtual-table. -** Return a pointer to the VTable object used by connection db to access +** Return a pointer to the VTable object used by connection db to access ** this virtual-table, if one has been created, or NULL otherwise. */ VTable *sqlite3GetVTable(sqlite3 *db, Table *pTab){ VTable *pVtab; assert( IsVirtual(pTab) ); - for(pVtab=pTab->pVTable; pVtab && pVtab->db!=db; pVtab=pVtab->pNext); + for(pVtab=pTab->u.vtab.p; pVtab && pVtab->db!=db; pVtab=pVtab->pNext); return pVtab; } @@ -140,7 +205,8 @@ void sqlite3VtabUnlock(VTable *pVTab){ assert( db ); assert( pVTab->nRef>0 ); - assert( db->magic==SQLITE_MAGIC_OPEN || db->magic==SQLITE_MAGIC_ZOMBIE ); + assert( db->eOpenState==SQLITE_STATE_OPEN + || db->eOpenState==SQLITE_STATE_ZOMBIE ); pVTab->nRef--; if( pVTab->nRef==0 ){ @@ -148,27 +214,31 @@ void sqlite3VtabUnlock(VTable *pVTab){ if( p ){ p->pModule->xDisconnect(p); } + sqlite3VtabModuleUnref(pVTab->db, pVTab->pMod); sqlite3DbFree(db, pVTab); } } /* ** Table p is a virtual table. This function moves all elements in the -** p->pVTable list to the sqlite3.pDisconnect lists of their associated -** database connections to be disconnected at the next opportunity. +** p->u.vtab.p list to the sqlite3.pDisconnect lists of their associated +** database connections to be disconnected at the next opportunity. ** Except, if argument db is not NULL, then the entry associated with -** connection db is left in the p->pVTable list. +** connection db is left in the p->u.vtab.p list. */ static VTable *vtabDisconnectAll(sqlite3 *db, Table *p){ VTable *pRet = 0; - VTable *pVTable = p->pVTable; - p->pVTable = 0; + VTable *pVTable; - /* Assert that the mutex (if any) associated with the BtShared database - ** that contains table p is held by the caller. See header comments + assert( IsVirtual(p) ); + pVTable = p->u.vtab.p; + p->u.vtab.p = 0; + + /* Assert that the mutex (if any) associated with the BtShared database + ** that contains table p is held by the caller. See header comments ** above function sqlite3VtabUnlockList() for an explanation of why ** this makes it safe to access the sqlite3.pDisconnect list of any - ** database connection that may have an entry in the p->pVTable list. + ** database connection that may have an entry in the p->u.vtab.p list. */ assert( db==0 || sqlite3SchemaMutexHeld(db, 0, p->pSchema) ); @@ -178,7 +248,7 @@ static VTable *vtabDisconnectAll(sqlite3 *db, Table *p){ assert( db2 ); if( db2==db ){ pRet = pVTable; - p->pVTable = pRet; + p->u.vtab.p = pRet; pRet->pNext = 0; }else{ pVTable->pNext = db2->pDisconnect; @@ -206,7 +276,7 @@ void sqlite3VtabDisconnect(sqlite3 *db, Table *p){ assert( sqlite3BtreeHoldsAllMutexes(db) ); assert( sqlite3_mutex_held(db->mutex) ); - for(ppVTab=&p->pVTable; *ppVTab; ppVTab=&(*ppVTab)->pNext){ + for(ppVTab=&p->u.vtab.p; *ppVTab; ppVTab=&(*ppVTab)->pNext){ if( (*ppVTab)->db==db ){ VTable *pVTab = *ppVTab; *ppVTab = pVTab->pNext; @@ -221,7 +291,7 @@ void sqlite3VtabDisconnect(sqlite3 *db, Table *p){ ** Disconnect all the virtual table objects in the sqlite3.pDisconnect list. ** ** This function may only be called when the mutexes associated with all -** shared b-tree databases opened using connection db are held by the +** shared b-tree databases opened using connection db are held by the ** caller. This is done to protect the sqlite3.pDisconnect list. The ** sqlite3.pDisconnect list is accessed only as follows: ** @@ -234,18 +304,17 @@ void sqlite3VtabDisconnect(sqlite3 *db, Table *p){ ** or, if the virtual table is stored in a non-sharable database, then ** the database handle mutex is held. ** -** As a result, a sqlite3.pDisconnect cannot be accessed simultaneously +** As a result, a sqlite3.pDisconnect cannot be accessed simultaneously ** by multiple threads. It is thread-safe. */ void sqlite3VtabUnlockList(sqlite3 *db){ VTable *p = db->pDisconnect; - db->pDisconnect = 0; assert( sqlite3BtreeHoldsAllMutexes(db) ); assert( sqlite3_mutex_held(db->mutex) ); if( p ){ - sqlite3ExpirePreparedStatements(db); + db->pDisconnect = 0; do { VTable *pNext = p->pNext; sqlite3VtabUnlock(p); @@ -260,42 +329,51 @@ void sqlite3VtabUnlockList(sqlite3 *db){ ** record. ** ** Since it is a virtual-table, the Table structure contains a pointer -** to the head of a linked list of VTable structures. Each VTable +** to the head of a linked list of VTable structures. Each VTable ** structure is associated with a single sqlite3* user of the schema. -** The reference count of the VTable structure associated with database -** connection db is decremented immediately (which may lead to the +** The reference count of the VTable structure associated with database +** connection db is decremented immediately (which may lead to the ** structure being xDisconnected and free). Any other VTable structures -** in the list are moved to the sqlite3.pDisconnect list of the associated +** in the list are moved to the sqlite3.pDisconnect list of the associated ** database connection. */ void sqlite3VtabClear(sqlite3 *db, Table *p){ - if( !db || db->pnBytesFreed==0 ) vtabDisconnectAll(0, p); - if( p->azModuleArg ){ + assert( IsVirtual(p) ); + assert( db!=0 ); + if( db->pnBytesFreed==0 ) vtabDisconnectAll(0, p); + if( p->u.vtab.azArg ){ int i; - for(i=0; i<p->nModuleArg; i++){ - if( i!=1 ) sqlite3DbFree(db, p->azModuleArg[i]); + for(i=0; i<p->u.vtab.nArg; i++){ + if( i!=1 ) sqlite3DbFree(db, p->u.vtab.azArg[i]); } - sqlite3DbFree(db, p->azModuleArg); + sqlite3DbFree(db, p->u.vtab.azArg); } } /* -** Add a new module argument to pTable->azModuleArg[]. +** Add a new module argument to pTable->u.vtab.azArg[]. ** The string is not copied - the pointer is stored. The ** string will be freed automatically when the table is ** deleted. */ -static void addModuleArgument(sqlite3 *db, Table *pTable, char *zArg){ - int nBytes = sizeof(char *)*(2+pTable->nModuleArg); +static void addModuleArgument(Parse *pParse, Table *pTable, char *zArg){ + sqlite3_int64 nBytes; char **azModuleArg; - azModuleArg = sqlite3DbRealloc(db, pTable->azModuleArg, nBytes); + sqlite3 *db = pParse->db; + + assert( IsVirtual(pTable) ); + nBytes = sizeof(char *)*(2+pTable->u.vtab.nArg); + if( pTable->u.vtab.nArg+3>=db->aLimit[SQLITE_LIMIT_COLUMN] ){ + sqlite3ErrorMsg(pParse, "too many columns on %s", pTable->zName); + } + azModuleArg = sqlite3DbRealloc(db, pTable->u.vtab.azArg, nBytes); if( azModuleArg==0 ){ sqlite3DbFree(db, zArg); }else{ - int i = pTable->nModuleArg++; + int i = pTable->u.vtab.nArg++; azModuleArg[i] = zArg; azModuleArg[i+1] = 0; - pTable->azModuleArg = azModuleArg; + pTable->u.vtab.azArg = azModuleArg; } } @@ -311,7 +389,6 @@ void sqlite3VtabBeginParse( Token *pModuleName, /* Name of the module for the virtual table */ int ifNotExists /* No error if the table already exists */ ){ - int iDb; /* The database the table is being created in */ Table *pTable; /* The new virtual table */ sqlite3 *db; /* Database connection */ @@ -319,16 +396,14 @@ void sqlite3VtabBeginParse( pTable = pParse->pNewTable; if( pTable==0 ) return; assert( 0==pTable->pIndex ); + pTable->eTabType = TABTYP_VTAB; db = pParse->db; - iDb = sqlite3SchemaToIndex(db, pTable->pSchema); - assert( iDb>=0 ); - - pTable->tabFlags |= TF_Virtual; - pTable->nModuleArg = 0; - addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName)); - addModuleArgument(db, pTable, 0); - addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName)); + + assert( pTable->u.vtab.nArg==0 ); + addModuleArgument(pParse, pTable, sqlite3NameFromToken(db, pModuleName)); + addModuleArgument(pParse, pTable, 0); + addModuleArgument(pParse, pTable, sqlite3DbStrDup(db, pTable->zName)); assert( (pParse->sNameToken.z==pName2->z && pName2->z!=0) || (pParse->sNameToken.z==pName1->z && pName2->z==0) ); @@ -339,12 +414,14 @@ void sqlite3VtabBeginParse( #ifndef SQLITE_OMIT_AUTHORIZATION /* Creating a virtual table invokes the authorization callback twice. ** The first invocation, to obtain permission to INSERT a row into the - ** sqlite_master table, has already been made by sqlite3StartTable(). + ** sqlite_schema table, has already been made by sqlite3StartTable(). ** The second call, to obtain permission to create the table, is made now. */ - if( pTable->azModuleArg ){ - sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName, - pTable->azModuleArg[0], pParse->db->aDb[iDb].zName); + if( pTable->u.vtab.azArg ){ + int iDb = sqlite3SchemaToIndex(db, pTable->pSchema); + assert( iDb>=0 ); /* The database the table is being created in */ + sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName, + pTable->u.vtab.azArg[0], pParse->db->aDb[iDb].zDbSName); } #endif } @@ -359,7 +436,7 @@ static void addArgumentToVtab(Parse *pParse){ const char *z = (const char*)pParse->sArg.z; int n = pParse->sArg.n; sqlite3 *db = pParse->db; - addModuleArgument(db, pParse->pNewTable, sqlite3DbStrNDup(db, z, n)); + addModuleArgument(pParse, pParse->pNewTable, sqlite3DbStrNDup(db, z, n)); } } @@ -372,15 +449,16 @@ void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){ sqlite3 *db = pParse->db; /* The database connection */ if( pTab==0 ) return; + assert( IsVirtual(pTab) ); addArgumentToVtab(pParse); pParse->sArg.z = 0; - if( pTab->nModuleArg<1 ) return; - + if( pTab->u.vtab.nArg<1 ) return; + /* If the CREATE VIRTUAL TABLE statement is being entered for the ** first time (in other words if the virtual table is actually being - ** created now instead of just being read out of sqlite_master) then + ** created now instead of just being read out of sqlite_schema) then ** do additional initialization work and store the statement text - ** in the sqlite_master table. + ** in the sqlite_schema table. */ if( !db->init.busy ){ char *zStmt; @@ -389,54 +467,53 @@ void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){ int iReg; Vdbe *v; + sqlite3MayAbort(pParse); + /* Compute the complete text of the CREATE VIRTUAL TABLE statement */ if( pEnd ){ pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n; } zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken); - /* A slot for the record has already been allocated in the - ** SQLITE_MASTER table. We just need to update that slot with all - ** the information we've collected. + /* A slot for the record has already been allocated in the + ** schema table. We just need to update that slot with all + ** the information we've collected. ** - ** The VM register number pParse->regRowid holds the rowid of an - ** entry in the sqlite_master table tht was created for this vtab + ** The VM register number pParse->u1.cr.regRowid holds the rowid of an + ** entry in the sqlite_schema table that was created for this vtab ** by sqlite3StartTable(). */ iDb = sqlite3SchemaToIndex(db, pTab->pSchema); + assert( pParse->isCreate ); sqlite3NestedParse(pParse, - "UPDATE %Q.%s " + "UPDATE %Q." LEGACY_SCHEMA_TABLE " " "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q " "WHERE rowid=#%d", - db->aDb[iDb].zName, SCHEMA_TABLE(iDb), + db->aDb[iDb].zDbSName, pTab->zName, pTab->zName, zStmt, - pParse->regRowid + pParse->u1.cr.regRowid ); - sqlite3DbFree(db, zStmt); v = sqlite3GetVdbe(pParse); sqlite3ChangeCookie(pParse, iDb); - sqlite3VdbeAddOp2(v, OP_Expire, 0, 0); - zWhere = sqlite3MPrintf(db, "name='%q' AND type='table'", pTab->zName); - sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere); + sqlite3VdbeAddOp0(v, OP_Expire); + zWhere = sqlite3MPrintf(db, "name=%Q AND sql=%Q", pTab->zName, zStmt); + sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere, 0); + sqlite3DbFree(db, zStmt); iReg = ++pParse->nMem; sqlite3VdbeLoadString(v, iReg, pTab->zName); sqlite3VdbeAddOp2(v, OP_VCreate, iDb, iReg); - } - - /* If we are rereading the sqlite_master table create the in-memory - ** record of the table. The xConnect() method is not called until - ** the first time the virtual table is used in an SQL statement. This - ** allows a schema that contains virtual tables to be loaded before - ** the required virtual table implementations are registered. */ - else { + }else{ + /* If we are rereading the sqlite_schema table create the in-memory + ** record of the table. */ Table *pOld; Schema *pSchema = pTab->pSchema; const char *zName = pTab->zName; - assert( sqlite3SchemaMutexHeld(db, 0, pSchema) ); + assert( zName!=0 ); + sqlite3MarkAllShadowTablesOf(db, pTab); pOld = sqlite3HashInsert(&pSchema->tblHash, zName, pTab); if( pOld ){ sqlite3OomFault(db); @@ -478,7 +555,7 @@ void sqlite3VtabArgExtend(Parse *pParse, Token *p){ ** to this procedure. */ static int vtabCallConstructor( - sqlite3 *db, + sqlite3 *db, Table *pTab, Module *pMod, int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**), @@ -487,38 +564,43 @@ static int vtabCallConstructor( VtabCtx sCtx; VTable *pVTable; int rc; - const char *const*azArg = (const char *const*)pTab->azModuleArg; - int nArg = pTab->nModuleArg; + const char *const*azArg; + int nArg = pTab->u.vtab.nArg; char *zErr = 0; char *zModuleName; int iDb; VtabCtx *pCtx; + assert( IsVirtual(pTab) ); + azArg = (const char *const*)pTab->u.vtab.azArg; + /* Check that the virtual-table is not already being initialized */ for(pCtx=db->pVtabCtx; pCtx; pCtx=pCtx->pPrior){ if( pCtx->pTab==pTab ){ - *pzErr = sqlite3MPrintf(db, + *pzErr = sqlite3MPrintf(db, "vtable constructor called recursively: %s", pTab->zName ); return SQLITE_LOCKED; } } - zModuleName = sqlite3MPrintf(db, "%s", pTab->zName); + zModuleName = sqlite3DbStrDup(db, pTab->zName); if( !zModuleName ){ - return SQLITE_NOMEM; + return SQLITE_NOMEM_BKPT; } - pVTable = sqlite3DbMallocZero(db, sizeof(VTable)); + pVTable = sqlite3MallocZero(sizeof(VTable)); if( !pVTable ){ + sqlite3OomFault(db); sqlite3DbFree(db, zModuleName); - return SQLITE_NOMEM; + return SQLITE_NOMEM_BKPT; } pVTable->db = db; pVTable->pMod = pMod; + pVTable->eVtabRisk = SQLITE_VTABRISK_Normal; iDb = sqlite3SchemaToIndex(db, pTab->pSchema); - pTab->azModuleArg[1] = db->aDb[iDb].zName; + pTab->u.vtab.azArg[1] = db->aDb[iDb].zDbSName; /* Invoke the virtual table constructor */ assert( &db->pVtabCtx ); @@ -528,7 +610,11 @@ static int vtabCallConstructor( sCtx.pPrior = db->pVtabCtx; sCtx.bDeclared = 0; db->pVtabCtx = &sCtx; + pTab->nTabRef++; rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr); + assert( pTab!=0 ); + assert( pTab->nTabRef>1 || rc!=SQLITE_OK ); + sqlite3DeleteTable(db, pTab); db->pVtabCtx = sCtx.pPrior; if( rc==SQLITE_NOMEM ) sqlite3OomFault(db); assert( sCtx.pTab==pTab ); @@ -546,40 +632,35 @@ static int vtabCallConstructor( ** the sqlite3_vtab object if successful. */ memset(pVTable->pVtab, 0, sizeof(pVTable->pVtab[0])); pVTable->pVtab->pModule = pMod->pModule; + pMod->nRefModule++; pVTable->nRef = 1; if( sCtx.bDeclared==0 ){ const char *zFormat = "vtable constructor did not declare schema: %s"; - *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName); + *pzErr = sqlite3MPrintf(db, zFormat, zModuleName); sqlite3VtabUnlock(pVTable); rc = SQLITE_ERROR; }else{ int iCol; - u8 oooHidden = 0; + u16 oooHidden = 0; /* If everything went according to plan, link the new VTable structure - ** into the linked list headed by pTab->pVTable. Then loop through the + ** into the linked list headed by pTab->u.vtab.p. Then loop through the ** columns of the table to see if any of them contain the token "hidden". ** If so, set the Column COLFLAG_HIDDEN flag and remove the token from ** the type string. */ - pVTable->pNext = pTab->pVTable; - pTab->pVTable = pVTable; + pVTable->pNext = pTab->u.vtab.p; + pTab->u.vtab.p = pVTable; for(iCol=0; iCol<pTab->nCol; iCol++){ - char *zType = pTab->aCol[iCol].zType; + char *zType = sqlite3ColumnType(&pTab->aCol[iCol], ""); int nType; int i = 0; - if( !zType ){ - pTab->tabFlags |= oooHidden; - continue; - } nType = sqlite3Strlen30(zType); - if( sqlite3StrNICmp("hidden", zType, 6)||(zType[6] && zType[6]!=' ') ){ - for(i=0; i<nType; i++){ - if( (0==sqlite3StrNICmp(" hidden", &zType[i], 7)) - && (zType[i+7]=='\0' || zType[i+7]==' ') - ){ - i++; - break; - } + for(i=0; i<nType; i++){ + if( 0==sqlite3StrNICmp("hidden", &zType[i], 6) + && (i==0 || zType[i-1]==' ') + && (zType[i+6]=='\0' || zType[i+6]==' ') + ){ + break; } } if( i<nType ){ @@ -593,6 +674,7 @@ static int vtabCallConstructor( zType[i-1] = '\0'; } pTab->aCol[iCol].colFlags |= COLFLAG_HIDDEN; + pTab->tabFlags |= TF_HasHidden; oooHidden = TF_OOOHidden; }else{ pTab->tabFlags |= oooHidden; @@ -607,7 +689,7 @@ static int vtabCallConstructor( /* ** This function is invoked by the parser to call the xConnect() method -** of the virtual table pTab. If an error occurs, an error code is returned +** of the virtual table pTab. If an error occurs, an error code is returned ** and an error left in pParse. ** ** This call is a no-op if table pTab is not a virtual table. @@ -619,16 +701,17 @@ int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){ int rc; assert( pTab ); - if( (pTab->tabFlags & TF_Virtual)==0 || sqlite3GetVTable(db, pTab) ){ + assert( IsVirtual(pTab) ); + if( sqlite3GetVTable(db, pTab) ){ return SQLITE_OK; } /* Locate the required virtual table module */ - zMod = pTab->azModuleArg[0]; + zMod = pTab->u.vtab.azArg[0]; pMod = (Module*)sqlite3HashFind(&db->aModule, zMod); if( !pMod ){ - const char *zModule = pTab->azModuleArg[0]; + const char *zModule = pTab->u.vtab.azArg[0]; sqlite3ErrorMsg(pParse, "no such module: %s", zModule); rc = SQLITE_ERROR; }else{ @@ -636,6 +719,7 @@ int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){ rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr); if( rc!=SQLITE_OK ){ sqlite3ErrorMsg(pParse, "%s", zErr); + pParse->rc = rc; } sqlite3DbFree(db, zErr); } @@ -652,10 +736,11 @@ static int growVTrans(sqlite3 *db){ /* Grow the sqlite3.aVTrans array if required */ if( (db->nVTrans%ARRAY_INCR)==0 ){ VTable **aVTrans; - int nBytes = sizeof(sqlite3_vtab *) * (db->nVTrans + ARRAY_INCR); + sqlite3_int64 nBytes = sizeof(sqlite3_vtab*)* + ((sqlite3_int64)db->nVTrans + ARRAY_INCR); aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes); if( !aVTrans ){ - return SQLITE_NOMEM; + return SQLITE_NOMEM_BKPT; } memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR); db->aVTrans = aVTrans; @@ -676,9 +761,9 @@ static void addToVTrans(sqlite3 *db, VTable *pVTab){ /* ** This function is invoked by the vdbe to call the xCreate method -** of the virtual table named zTab in database iDb. +** of the virtual table named zTab in database iDb. ** -** If an error occurs, *pzErr is set to point an an English language +** If an error occurs, *pzErr is set to point to an English language ** description of the error and an SQLITE_XXX error code is returned. ** In this case the caller must call sqlite3DbFree(db, ) on *pzErr. */ @@ -688,15 +773,15 @@ int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){ Module *pMod; const char *zMod; - pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName); - assert( pTab && (pTab->tabFlags & TF_Virtual)!=0 && !pTab->pVTable ); + pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zDbSName); + assert( pTab && IsVirtual(pTab) && !pTab->u.vtab.p ); /* Locate the required virtual table module */ - zMod = pTab->azModuleArg[0]; + zMod = pTab->u.vtab.azArg[0]; pMod = (Module*)sqlite3HashFind(&db->aModule, zMod); - /* If the module has been registered and includes a Create method, - ** invoke it now. If the module has not been registered, return an + /* If the module has been registered and includes a Create method, + ** invoke it now. If the module has not been registered, return an ** error. Otherwise, do nothing. */ if( pMod==0 || pMod->pModule->xCreate==0 || pMod->pModule->xDestroy==0 ){ @@ -725,61 +810,105 @@ int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){ */ int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){ VtabCtx *pCtx; - Parse *pParse; int rc = SQLITE_OK; Table *pTab; - char *zErr = 0; + Parse sParse; + int initBusy; + int i; + const unsigned char *z; + static const u8 aKeyword[] = { TK_CREATE, TK_TABLE, 0 }; #ifdef SQLITE_ENABLE_API_ARMOR if( !sqlite3SafetyCheckOk(db) || zCreateTable==0 ){ return SQLITE_MISUSE_BKPT; } #endif + + /* Verify that the first two keywords in the CREATE TABLE statement + ** really are "CREATE" and "TABLE". If this is not the case, then + ** sqlite3_declare_vtab() is being misused. + */ + z = (const unsigned char*)zCreateTable; + for(i=0; aKeyword[i]; i++){ + int tokenType = 0; + do{ + z += sqlite3GetToken(z, &tokenType); + }while( tokenType==TK_SPACE || tokenType==TK_COMMENT ); + if( tokenType!=aKeyword[i] ){ + sqlite3ErrorWithMsg(db, SQLITE_ERROR, "syntax error"); + return SQLITE_ERROR; + } + } + sqlite3_mutex_enter(db->mutex); pCtx = db->pVtabCtx; if( !pCtx || pCtx->bDeclared ){ - sqlite3Error(db, SQLITE_MISUSE); + sqlite3Error(db, SQLITE_MISUSE_BKPT); sqlite3_mutex_leave(db->mutex); return SQLITE_MISUSE_BKPT; } + pTab = pCtx->pTab; - assert( (pTab->tabFlags & TF_Virtual)!=0 ); + assert( IsVirtual(pTab) ); - pParse = sqlite3StackAllocZero(db, sizeof(*pParse)); - if( pParse==0 ){ - rc = SQLITE_NOMEM; - }else{ - pParse->declareVtab = 1; - pParse->db = db; - pParse->nQueryLoop = 1; - - if( SQLITE_OK==sqlite3RunParser(pParse, zCreateTable, &zErr) - && pParse->pNewTable - && !db->mallocFailed - && !pParse->pNewTable->pSelect - && (pParse->pNewTable->tabFlags & TF_Virtual)==0 - ){ - if( !pTab->aCol ){ - pTab->aCol = pParse->pNewTable->aCol; - pTab->nCol = pParse->pNewTable->nCol; - pParse->pNewTable->nCol = 0; - pParse->pNewTable->aCol = 0; + sqlite3ParseObjectInit(&sParse, db); + sParse.eParseMode = PARSE_MODE_DECLARE_VTAB; + sParse.disableTriggers = 1; + /* We should never be able to reach this point while loading the + ** schema. Nevertheless, defend against that (turn off db->init.busy) + ** in case a bug arises. */ + assert( db->init.busy==0 ); + initBusy = db->init.busy; + db->init.busy = 0; + sParse.nQueryLoop = 1; + if( SQLITE_OK==sqlite3RunParser(&sParse, zCreateTable) ){ + assert( sParse.pNewTable!=0 ); + assert( !db->mallocFailed ); + assert( IsOrdinaryTable(sParse.pNewTable) ); + assert( sParse.zErrMsg==0 ); + if( !pTab->aCol ){ + Table *pNew = sParse.pNewTable; + Index *pIdx; + pTab->aCol = pNew->aCol; + assert( IsOrdinaryTable(pNew) ); + sqlite3ExprListDelete(db, pNew->u.tab.pDfltList); + pTab->nNVCol = pTab->nCol = pNew->nCol; + pTab->tabFlags |= pNew->tabFlags & (TF_WithoutRowid|TF_NoVisibleRowid); + pNew->nCol = 0; + pNew->aCol = 0; + assert( pTab->pIndex==0 ); + assert( HasRowid(pNew) || sqlite3PrimaryKeyIndex(pNew)!=0 ); + if( !HasRowid(pNew) + && pCtx->pVTable->pMod->pModule->xUpdate!=0 + && sqlite3PrimaryKeyIndex(pNew)->nKeyCol!=1 + ){ + /* WITHOUT ROWID virtual tables must either be read-only (xUpdate==0) + ** or else must have a single-column PRIMARY KEY */ + rc = SQLITE_ERROR; + } + pIdx = pNew->pIndex; + if( pIdx ){ + assert( pIdx->pNext==0 ); + pTab->pIndex = pIdx; + pNew->pIndex = 0; + pIdx->pTable = pTab; } - pCtx->bDeclared = 1; - }else{ - sqlite3ErrorWithMsg(db, SQLITE_ERROR, (zErr ? "%s" : 0), zErr); - sqlite3DbFree(db, zErr); - rc = SQLITE_ERROR; - } - pParse->declareVtab = 0; - - if( pParse->pVdbe ){ - sqlite3VdbeFinalize(pParse->pVdbe); } - sqlite3DeleteTable(db, pParse->pNewTable); - sqlite3ParserReset(pParse); - sqlite3StackFree(db, pParse); + pCtx->bDeclared = 1; + }else{ + sqlite3ErrorWithMsg(db, SQLITE_ERROR, + (sParse.zErrMsg ? "%s" : 0), sParse.zErrMsg); + sqlite3DbFree(db, sParse.zErrMsg); + rc = SQLITE_ERROR; + } + sParse.eParseMode = PARSE_MODE_NORMAL; + + if( sParse.pVdbe ){ + sqlite3VdbeFinalize(sParse.pVdbe); } + sqlite3DeleteTable(db, sParse.pNewTable); + sqlite3ParseObjectReset(&sParse); + db->init.busy = initBusy; assert( (rc&0xff)==rc ); rc = sqlite3ApiExit(db, rc); @@ -798,11 +927,14 @@ int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab){ int rc = SQLITE_OK; Table *pTab; - pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName); - if( ALWAYS(pTab!=0 && pTab->pVTable!=0) ){ + pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zDbSName); + if( ALWAYS(pTab!=0) + && ALWAYS(IsVirtual(pTab)) + && ALWAYS(pTab->u.vtab.p!=0) + ){ VTable *p; int (*xDestroy)(sqlite3_vtab *); - for(p=pTab->pVTable; p; p=p->pNext){ + for(p=pTab->u.vtab.p; p; p=p->pNext){ assert( p->pVtab ); if( p->pVtab->nRef>0 ){ return SQLITE_LOCKED; @@ -810,15 +942,18 @@ int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab){ } p = vtabDisconnectAll(db, pTab); xDestroy = p->pMod->pModule->xDestroy; - assert( xDestroy!=0 ); /* Checked before the virtual table is created */ + if( xDestroy==0 ) xDestroy = p->pMod->pModule->xDisconnect; + assert( xDestroy!=0 ); + pTab->nTabRef++; rc = xDestroy(p->pVtab); /* Remove the sqlite3_vtab* from the aVTrans[] array, if applicable */ if( rc==SQLITE_OK ){ - assert( pTab->pVTable==p && p->pNext==0 ); + assert( pTab->u.vtab.p==p && p->pNext==0 ); p->pVtab = 0; - pTab->pVTable = 0; + pTab->u.vtab.p = 0; sqlite3VtabUnlock(p); } + sqlite3DeleteTable(db, pTab); } return rc; @@ -830,7 +965,7 @@ int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab){ ** called is identified by the second argument, "offset", which is ** the offset of the method to call in the sqlite3_module structure. ** -** The array is cleared after invoking the callbacks. +** The array is cleared after invoking the callbacks. */ static void callFinaliser(sqlite3 *db, int offset){ int i; @@ -879,7 +1014,7 @@ int sqlite3VtabSync(sqlite3 *db, Vdbe *p){ } /* -** Invoke the xRollback method of all virtual tables in the +** Invoke the xRollback method of all virtual tables in the ** sqlite3.aVTrans array. Then clear the array itself. */ int sqlite3VtabRollback(sqlite3 *db){ @@ -888,7 +1023,7 @@ int sqlite3VtabRollback(sqlite3 *db){ } /* -** Invoke the xCommit method of all virtual tables in the +** Invoke the xCommit method of all virtual tables in the ** sqlite3.aVTrans array. Then clear the array itself. */ int sqlite3VtabCommit(sqlite3 *db){ @@ -910,7 +1045,7 @@ int sqlite3VtabBegin(sqlite3 *db, VTable *pVTab){ /* Special case: If db->aVTrans is NULL and db->nVTrans is greater ** than zero, then this function is being called from within a - ** virtual module xSync() callback. It is illegal to write to + ** virtual module xSync() callback. It is illegal to write to ** virtual module tables in this case, so return SQLITE_LOCKED. */ if( sqlite3VtabInSync(db) ){ @@ -918,7 +1053,7 @@ int sqlite3VtabBegin(sqlite3 *db, VTable *pVTab){ } if( !pVTab ){ return SQLITE_OK; - } + } pModule = pVTab->pVtab->pModule; if( pModule->xBegin ){ @@ -931,7 +1066,7 @@ int sqlite3VtabBegin(sqlite3 *db, VTable *pVTab){ } } - /* Invoke the xBegin method. If successful, add the vtab to the + /* Invoke the xBegin method. If successful, add the vtab to the ** sqlite3.aVTrans[] array. */ rc = growVTrans(db); if( rc==SQLITE_OK ){ @@ -939,7 +1074,10 @@ int sqlite3VtabBegin(sqlite3 *db, VTable *pVTab){ if( rc==SQLITE_OK ){ int iSvpt = db->nStatement + db->nSavepoint; addToVTrans(db, pVTab); - if( iSvpt ) rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, iSvpt-1); + if( iSvpt && pModule->xSavepoint ){ + pVTab->iSavepoint = iSvpt; + rc = pModule->xSavepoint(pVTab->pVtab, iSvpt-1); + } } } } @@ -952,11 +1090,11 @@ int sqlite3VtabBegin(sqlite3 *db, VTable *pVTab){ ** as the second argument to the virtual table method invoked. ** ** If op is SAVEPOINT_BEGIN, the xSavepoint method is invoked. If it is -** SAVEPOINT_ROLLBACK, the xRollbackTo method. Otherwise, if op is +** SAVEPOINT_ROLLBACK, the xRollbackTo method. Otherwise, if op is ** SAVEPOINT_RELEASE, then the xRelease method of each virtual table with ** an open transaction is invoked. ** -** If any virtual table method returns an error code other than SQLITE_OK, +** If any virtual table method returns an error code other than SQLITE_OK, ** processing is abandoned and the error returned to the caller of this ** function immediately. If all calls to virtual table methods are successful, ** SQLITE_OK is returned. @@ -973,6 +1111,7 @@ int sqlite3VtabSavepoint(sqlite3 *db, int op, int iSavepoint){ const sqlite3_module *pMod = pVTab->pMod->pModule; if( pVTab->pVtab && pMod->iVersion>=2 ){ int (*xMethod)(sqlite3_vtab *, int); + sqlite3VtabLock(pVTab); switch( op ){ case SAVEPOINT_BEGIN: xMethod = pMod->xSavepoint; @@ -986,8 +1125,12 @@ int sqlite3VtabSavepoint(sqlite3 *db, int op, int iSavepoint){ break; } if( xMethod && pVTab->iSavepoint>iSavepoint ){ + u64 savedFlags = (db->flags & SQLITE_Defensive); + db->flags &= ~(u64)SQLITE_Defensive; rc = xMethod(pVTab->pVtab, iSavepoint); + db->flags |= savedFlags; } + sqlite3VtabUnlock(pVTab); } } } @@ -1003,7 +1146,7 @@ int sqlite3VtabSavepoint(sqlite3 *db, int op, int iSavepoint){ ** This routine is used to allow virtual table implementations to ** overload MATCH, LIKE, GLOB, and REGEXP operators. ** -** Return either the pDef argument (indicating no change) or a +** Return either the pDef argument (indicating no change) or a ** new FuncDef structure that is marked as ephemeral using the ** SQLITE_FUNC_EPHEM flag. */ @@ -1020,33 +1163,37 @@ FuncDef *sqlite3VtabOverloadFunction( void *pArg = 0; FuncDef *pNew; int rc = 0; - char *zLowerName; - unsigned char *z; - /* Check to see the left operand is a column in a virtual table */ if( NEVER(pExpr==0) ) return pDef; if( pExpr->op!=TK_COLUMN ) return pDef; - pTab = pExpr->pTab; + assert( ExprUseYTab(pExpr) ); + pTab = pExpr->y.pTab; if( NEVER(pTab==0) ) return pDef; - if( (pTab->tabFlags & TF_Virtual)==0 ) return pDef; + if( !IsVirtual(pTab) ) return pDef; pVtab = sqlite3GetVTable(db, pTab)->pVtab; assert( pVtab!=0 ); assert( pVtab->pModule!=0 ); pMod = (sqlite3_module *)pVtab->pModule; if( pMod->xFindFunction==0 ) return pDef; - + /* Call the xFindFunction method on the virtual table implementation - ** to see if the implementation wants to overload this function + ** to see if the implementation wants to overload this function. + ** + ** Though undocumented, we have historically always invoked xFindFunction + ** with an all lower-case function name. Continue in this tradition to + ** avoid any chance of an incompatibility. */ - zLowerName = sqlite3DbStrDup(db, pDef->zName); - if( zLowerName ){ - for(z=(unsigned char*)zLowerName; *z; z++){ - *z = sqlite3UpperToLower[*z]; +#ifdef SQLITE_DEBUG + { + int i; + for(i=0; pDef->zName[i]; i++){ + unsigned char x = (unsigned char)pDef->zName[i]; + assert( x==sqlite3UpperToLower[x] ); } - rc = pMod->xFindFunction(pVtab, nArg, zLowerName, &xSFunc, &pArg); - sqlite3DbFree(db, zLowerName); } +#endif + rc = pMod->xFindFunction(pVtab, nArg, pDef->zName, &xSFunc, &pArg); if( rc==0 ){ return pDef; } @@ -1059,8 +1206,8 @@ FuncDef *sqlite3VtabOverloadFunction( return pDef; } *pNew = *pDef; - pNew->zName = (char *)&pNew[1]; - memcpy(pNew->zName, pDef->zName, sqlite3Strlen30(pDef->zName)+1); + pNew->zName = (const char*)&pNew[1]; + memcpy((char*)&pNew[1], pDef->zName, sqlite3Strlen30(pDef->zName)+1); pNew->xSFunc = xSFunc; pNew->pUserData = pArg; pNew->funcFlags |= SQLITE_FUNC_EPHEM; @@ -1083,7 +1230,7 @@ void sqlite3VtabMakeWritable(Parse *pParse, Table *pTab){ if( pTab==pToplevel->apVtabLock[i] ) return; } n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]); - apVtabLock = sqlite3_realloc64(pToplevel->apVtabLock, n); + apVtabLock = sqlite3Realloc(pToplevel->apVtabLock, n); if( apVtabLock ){ pToplevel->apVtabLock = apVtabLock; pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab; @@ -1093,14 +1240,15 @@ void sqlite3VtabMakeWritable(Parse *pParse, Table *pTab){ } /* -** Check to see if virtual tale module pMod can be have an eponymous +** Check to see if virtual table module pMod can be have an eponymous ** virtual table instance. If it can, create one if one does not already -** exist. Return non-zero if the eponymous virtual table instance exists -** when this routine returns, and return zero if it does not exist. +** exist. Return non-zero if either the eponymous virtual table instance +** exists when this routine returns or if an attempt to create it failed +** and an error message was left in pParse. ** ** An eponymous virtual table instance is one that is named after its ** module, and more importantly, does not require a CREATE VIRTUAL TABLE -** statement in order to come into existance. Eponymous virtual table +** statement in order to come into existence. Eponymous virtual table ** instances always exist. They cannot be DROP-ed. ** ** Any virtual table module for which xConnect and xCreate are the same @@ -1110,31 +1258,35 @@ int sqlite3VtabEponymousTableInit(Parse *pParse, Module *pMod){ const sqlite3_module *pModule = pMod->pModule; Table *pTab; char *zErr = 0; - int nName; int rc; sqlite3 *db = pParse->db; if( pMod->pEpoTab ) return 1; if( pModule->xCreate!=0 && pModule->xCreate!=pModule->xConnect ) return 0; - nName = sqlite3Strlen30(pMod->zName) + 1; - pTab = sqlite3DbMallocZero(db, sizeof(Table) + nName); + pTab = sqlite3DbMallocZero(db, sizeof(Table)); if( pTab==0 ) return 0; + pTab->zName = sqlite3DbStrDup(db, pMod->zName); + if( pTab->zName==0 ){ + sqlite3DbFree(db, pTab); + return 0; + } pMod->pEpoTab = pTab; - pTab->zName = (char*)&pTab[1]; - memcpy(pTab->zName, pMod->zName, nName); - pTab->nRef = 1; + pTab->nTabRef = 1; + pTab->eTabType = TABTYP_VTAB; pTab->pSchema = db->aDb[0].pSchema; - pTab->tabFlags |= TF_Virtual; - pTab->nModuleArg = 0; + assert( pTab->u.vtab.nArg==0 ); pTab->iPKey = -1; - addModuleArgument(db, pTab, sqlite3DbStrDup(db, pTab->zName)); - addModuleArgument(db, pTab, 0); - addModuleArgument(db, pTab, sqlite3DbStrDup(db, pTab->zName)); + pTab->tabFlags |= TF_Eponymous; + addModuleArgument(pParse, pTab, sqlite3DbStrDup(db, pTab->zName)); + addModuleArgument(pParse, pTab, 0); + addModuleArgument(pParse, pTab, sqlite3DbStrDup(db, pTab->zName)); + db->nSchemaLock++; rc = vtabCallConstructor(db, pTab, pMod, pModule->xConnect, &zErr); + db->nSchemaLock--; if( rc ){ sqlite3ErrorMsg(pParse, "%s", zErr); + pParse->rc = rc; sqlite3DbFree(db, zErr); sqlite3VtabEponymousTableClear(db, pMod); - return 0; } return 1; } @@ -1146,9 +1298,11 @@ int sqlite3VtabEponymousTableInit(Parse *pParse, Module *pMod){ void sqlite3VtabEponymousTableClear(sqlite3 *db, Module *pMod){ Table *pTab = pMod->pEpoTab; if( pTab!=0 ){ - sqlite3DeleteColumnNames(db, pTab); - sqlite3VtabClear(db, pTab); - sqlite3DbFree(db, pTab); + /* Mark the table as Ephemeral prior to deleting it, so that the + ** sqlite3DeleteTable() routine will know that it is not stored in + ** the schema. */ + pTab->tabFlags |= TF_Ephemeral; + sqlite3DeleteTable(db, pTab); pMod->pEpoTab = 0; } } @@ -1161,8 +1315,8 @@ void sqlite3VtabEponymousTableClear(sqlite3 *db, Module *pMod){ ** within an xUpdate method. */ int sqlite3_vtab_on_conflict(sqlite3 *db){ - static const unsigned char aMap[] = { - SQLITE_ROLLBACK, SQLITE_ABORT, SQLITE_FAIL, SQLITE_IGNORE, SQLITE_REPLACE + static const unsigned char aMap[] = { + SQLITE_ROLLBACK, SQLITE_ABORT, SQLITE_FAIL, SQLITE_IGNORE, SQLITE_REPLACE }; #ifdef SQLITE_ENABLE_API_ARMOR if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; @@ -1174,35 +1328,49 @@ int sqlite3_vtab_on_conflict(sqlite3 *db){ } /* -** Call from within the xCreate() or xConnect() methods to provide +** Call from within the xCreate() or xConnect() methods to provide ** the SQLite core with additional information about the behavior ** of the virtual table being implemented. */ int sqlite3_vtab_config(sqlite3 *db, int op, ...){ va_list ap; int rc = SQLITE_OK; + VtabCtx *p; #ifdef SQLITE_ENABLE_API_ARMOR if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; #endif sqlite3_mutex_enter(db->mutex); - va_start(ap, op); - switch( op ){ - case SQLITE_VTAB_CONSTRAINT_SUPPORT: { - VtabCtx *p = db->pVtabCtx; - if( !p ){ - rc = SQLITE_MISUSE_BKPT; - }else{ - assert( p->pTab==0 || (p->pTab->tabFlags & TF_Virtual)!=0 ); + p = db->pVtabCtx; + if( !p ){ + rc = SQLITE_MISUSE_BKPT; + }else{ + assert( p->pTab==0 || IsVirtual(p->pTab) ); + va_start(ap, op); + switch( op ){ + case SQLITE_VTAB_CONSTRAINT_SUPPORT: { p->pVTable->bConstraint = (u8)va_arg(ap, int); + break; + } + case SQLITE_VTAB_INNOCUOUS: { + p->pVTable->eVtabRisk = SQLITE_VTABRISK_Low; + break; + } + case SQLITE_VTAB_DIRECTONLY: { + p->pVTable->eVtabRisk = SQLITE_VTABRISK_High; + break; + } + case SQLITE_VTAB_USES_ALL_SCHEMAS: { + p->pVTable->bAllSchemas = 1; + break; + } + default: { + rc = SQLITE_MISUSE_BKPT; + break; } - break; } - default: - rc = SQLITE_MISUSE_BKPT; - break; + va_end(ap); } - va_end(ap); if( rc!=SQLITE_OK ) sqlite3Error(db, rc); sqlite3_mutex_leave(db->mutex); diff --git a/src/vxworks.h b/src/vxworks.h index e7013c3f66..3a95779d2c 100644 --- a/src/vxworks.h +++ b/src/vxworks.h @@ -25,7 +25,9 @@ #define HAVE_UTIME 1 #else /* This is not VxWorks. */ -#define OS_VXWORKS 0 +#ifndef OS_VXWORKS +# define OS_VXWORKS 0 +#endif #define HAVE_FCHOWN 1 #define HAVE_READLINK 1 #define HAVE_LSTAT 1 diff --git a/src/wal.c b/src/wal.c index d7a6b701e6..d4d568d628 100644 --- a/src/wal.c +++ b/src/wal.c @@ -10,7 +10,7 @@ ** ************************************************************************* ** -** This file contains the implementation of a write-ahead log (WAL) used in +** This file contains the implementation of a write-ahead log (WAL) used in ** "journal_mode=WAL" mode. ** ** WRITE-AHEAD LOG (WAL) FILE FORMAT @@ -19,7 +19,7 @@ ** Each frame records the revised content of a single page from the ** database file. All changes to the database are recorded by writing ** frames into the WAL. Transactions commit when a frame is written that -** contains a commit marker. A single WAL can and usually does record +** contains a commit marker. A single WAL can and usually does record ** multiple transactions. Periodically, the content of the WAL is ** transferred back into the database file in an operation called a ** "checkpoint". @@ -44,12 +44,12 @@ ** 28: Checksum-2 (second part of checksum for first 24 bytes of header). ** ** Immediately following the wal-header are zero or more frames. Each -** frame consists of a 24-byte frame-header followed by a <page-size> bytes -** of page data. The frame-header is six big-endian 32-bit unsigned +** frame consists of a 24-byte frame-header followed by <page-size> bytes +** of page data. The frame-header is six big-endian 32-bit unsigned ** integer values, as follows: ** ** 0: Page number. -** 4: For commit records, the size of the database image in pages +** 4: For commit records, the size of the database image in pages ** after the commit. For all other records, zero. ** 8: Salt-1 (copied from the header) ** 12: Salt-2 (copied from the header) @@ -75,7 +75,7 @@ ** the checksum. The checksum is computed by interpreting the input as ** an even number of unsigned 32-bit integers: x[0] through x[N]. The ** algorithm used for the checksum is as follows: -** +** ** for i from 0 to n-1 step 2: ** s0 += x[i] + s1; ** s1 += x[i+1] + s0; @@ -83,7 +83,7 @@ ** ** Note that s0 and s1 are both weighted checksums using fibonacci weights ** in reverse order (the largest fibonacci weight occurs on the first element -** of the sequence being summed.) The s1 value spans all 32-bit +** of the sequence being summed.) The s1 value spans all 32-bit ** terms of the sequence whereas s0 omits the final term. ** ** On a checkpoint, the WAL is first VFS.xSync-ed, then valid content of the @@ -116,22 +116,26 @@ ** multiple concurrent readers to view different versions of the database ** content simultaneously. ** -** The reader algorithm in the previous paragraphs works correctly, but +** The reader algorithm in the previous paragraphs works correctly, but ** because frames for page P can appear anywhere within the WAL, the ** reader has to scan the entire WAL looking for page P frames. If the ** WAL is large (multiple megabytes is typical) that scan can be slow, ** and read performance suffers. To overcome this problem, a separate ** data structure called the wal-index is maintained to expedite the ** search for frames of a particular page. -** +** ** WAL-INDEX FORMAT ** ** Conceptually, the wal-index is shared memory, though VFS implementations ** might choose to implement the wal-index using a mmapped file. Because -** the wal-index is shared memory, SQLite does not support journal_mode=WAL +** the wal-index is shared memory, SQLite does not support journal_mode=WAL ** on a network filesystem. All users of the database must be able to ** share memory. ** +** In the default unix and windows implementation, the wal-index is a mmapped +** file whose name is the database name with a "-shm" suffix added. For that +** reason, the wal-index is sometimes called the "shm" file. +** ** The wal-index is transient. After a crash, the wal-index can (and should ** be) reconstructed from the original WAL file. In fact, the VFS is required ** to either truncate or zero the header of the wal-index when the last @@ -142,28 +146,31 @@ ** byte order of the host computer. ** ** The purpose of the wal-index is to answer this question quickly: Given -** a page number P and a maximum frame index M, return the index of the +** a page number P and a maximum frame index M, return the index of the ** last frame in the wal before frame M for page P in the WAL, or return ** NULL if there are no frames for page P in the WAL prior to M. ** ** The wal-index consists of a header region, followed by an one or -** more index blocks. +** more index blocks. ** ** The wal-index header contains the total number of frames within the WAL ** in the mxFrame field. ** -** Each index block except for the first contains information on +** Each index block except for the first contains information on ** HASHTABLE_NPAGE frames. The first index block contains information on -** HASHTABLE_NPAGE_ONE frames. The values of HASHTABLE_NPAGE_ONE and +** HASHTABLE_NPAGE_ONE frames. The values of HASHTABLE_NPAGE_ONE and ** HASHTABLE_NPAGE are selected so that together the wal-index header and ** first index block are the same size as all other index blocks in the -** wal-index. +** wal-index. The values are: +** +** HASHTABLE_NPAGE 4096 +** HASHTABLE_NPAGE_ONE 4062 ** ** Each index block contains two sections, a page-mapping that contains the -** database page number associated with each wal frame, and a hash-table +** database page number associated with each wal frame, and a hash-table ** that allows readers to query an index block for a specific page number. ** The page-mapping is an array of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE -** for the first index block) 32-bit page numbers. The first entry in the +** for the first index block) 32-bit page numbers. The first entry in the ** first index-block contains the database page number corresponding to the ** first frame in the WAL file. The first entry in the second index block ** in the WAL file corresponds to the (HASHTABLE_NPAGE_ONE+1)th frame in @@ -184,8 +191,8 @@ ** ** The hash table consists of HASHTABLE_NSLOT 16-bit unsigned integers. ** HASHTABLE_NSLOT = 2*HASHTABLE_NPAGE, and there is one entry in the -** hash table for each page number in the mapping section, so the hash -** table is never more than half full. The expected number of collisions +** hash table for each page number in the mapping section, so the hash +** table is never more than half full. The expected number of collisions ** prior to finding a match is 1. Each entry of the hash table is an ** 1-based index of an entry in the mapping section of the same ** index block. Let K be the 1-based index of the largest entry in @@ -204,12 +211,12 @@ ** reached) until an unused hash slot is found. Let the first unused slot ** be at index iUnused. (iUnused might be less than iKey if there was ** wrap-around.) Because the hash table is never more than half full, -** the search is guaranteed to eventually hit an unused entry. Let +** the search is guaranteed to eventually hit an unused entry. Let ** iMax be the value between iKey and iUnused, closest to iUnused, ** where aHash[iMax]==P. If there is no iMax entry (if there exists ** no hash slot such that aHash[i]==p) then page P is not in the ** current index block. Otherwise the iMax-th mapping entry of the -** current index block corresponds to the last entry that references +** current index block corresponds to the last entry that references ** page P. ** ** A hash search begins with the last index block and moves toward the @@ -234,7 +241,7 @@ ** if no values greater than K0 had ever been inserted into the hash table ** in the first place - which is what reader one wants. Meanwhile, the ** second reader using K1 will see additional values that were inserted -** later, which is exactly what reader two wants. +** later, which is exactly what reader two wants. ** ** When a rollback occurs, the value of K is decreased. Hash table entries ** that correspond to frames greater than the new K value are removed @@ -262,7 +269,7 @@ int sqlite3WalTrace = 0; ** values in the wal-header are correct and (b) the version field is not ** WAL_MAX_VERSION, recovery fails and SQLite returns SQLITE_CANTOPEN. ** -** Similarly, if a client successfully reads a wal-index header (i.e. the +** Similarly, if a client successfully reads a wal-index header (i.e. the ** checksum test is successful) and finds that the version field is not ** WALINDEX_MAX_VERSION, then no read-transaction is opened and SQLite ** returns SQLITE_CANTOPEN. @@ -271,9 +278,18 @@ int sqlite3WalTrace = 0; #define WALINDEX_MAX_VERSION 3007000 /* -** Indices of various locking bytes. WAL_NREADER is the number +** Index numbers for various locking bytes. WAL_NREADER is the number ** of available reader locks and should be at least 3. The default ** is SQLITE_SHM_NLOCK==8 and WAL_NREADER==5. +** +** Technically, the various VFSes are free to implement these locks however +** they see fit. However, compatibility is encouraged so that VFSes can +** interoperate. The standard implementation used on both unix and windows +** is for the index number to indicate a byte offset into the +** WalCkptInfo.aLock[] array in the wal-index header. In other words, all +** locks are on the shm file. The WALINDEX_LOCK_OFFSET constant (which +** should be 120) is the location in the shm file for the first locking +** byte. */ #define WAL_WRITE_LOCK 0 #define WAL_ALL_BUT_WRITE 1 @@ -300,7 +316,7 @@ typedef struct WalCkptInfo WalCkptInfo; ** ** The szPage value can be any power of 2 between 512 and 32768, inclusive. ** Or it can be 1 to represent a 65536-byte page. The latter case was -** added in 3.7.1 when support for 64K pages was added. +** added in 3.7.1 when support for 64K pages was added. */ struct WalIndexHdr { u32 iVersion; /* Wal-index version */ @@ -342,9 +358,9 @@ struct WalIndexHdr { ** There is one entry in aReadMark[] for each reader lock. If a reader ** holds read-lock K, then the value in aReadMark[K] is no greater than ** the mxFrame for that reader. The value READMARK_NOT_USED (0xffffffff) -** for any aReadMark[] means that entry is unused. aReadMark[0] is +** for any aReadMark[] means that entry is unused. aReadMark[0] is ** a special case; its value is never used and it exists as a place-holder -** to avoid having to offset aReadMark[] indexs by one. Readers holding +** to avoid having to offset aReadMark[] indexes by one. Readers holding ** WAL_READ_LOCK(0) always ignore the entire WAL and read all content ** directly from the database. ** @@ -362,7 +378,7 @@ struct WalIndexHdr { ** previous sentence is when nBackfill equals mxFrame (meaning that everything ** in the WAL has been backfilled into the database) then new readers ** will choose aReadMark[0] which has value 0 and hence such reader will -** get all their all content directly from the database file and ignore +** get all their all content directly from the database file and ignore ** the WAL. ** ** Writers normally append new frames to the end of the WAL. However, @@ -384,6 +400,70 @@ struct WalCkptInfo { }; #define READMARK_NOT_USED 0xffffffff +/* +** This is a schematic view of the complete 136-byte header of the +** wal-index file (also known as the -shm file): +** +** +-----------------------------+ +** 0: | iVersion | \ +** +-----------------------------+ | +** 4: | (unused padding) | | +** +-----------------------------+ | +** 8: | iChange | | +** +-------+-------+-------------+ | +** 12: | bInit | bBig | szPage | | +** +-------+-------+-------------+ | +** 16: | mxFrame | | First copy of the +** +-----------------------------+ | WalIndexHdr object +** 20: | nPage | | +** +-----------------------------+ | +** 24: | aFrameCksum | | +** | | | +** +-----------------------------+ | +** 32: | aSalt | | +** | | | +** +-----------------------------+ | +** 40: | aCksum | | +** | | / +** +-----------------------------+ +** 48: | iVersion | \ +** +-----------------------------+ | +** 52: | (unused padding) | | +** +-----------------------------+ | +** 56: | iChange | | +** +-------+-------+-------------+ | +** 60: | bInit | bBig | szPage | | +** +-------+-------+-------------+ | Second copy of the +** 64: | mxFrame | | WalIndexHdr +** +-----------------------------+ | +** 68: | nPage | | +** +-----------------------------+ | +** 72: | aFrameCksum | | +** | | | +** +-----------------------------+ | +** 80: | aSalt | | +** | | | +** +-----------------------------+ | +** 88: | aCksum | | +** | | / +** +-----------------------------+ +** 96: | nBackfill | +** +-----------------------------+ +** 100: | 5 read marks | +** | | +** | | +** | | +** | | +** +-------+-------+------+------+ +** 120: | Write | Ckpt | Rcvr | Rd0 | \ +** +-------+-------+------+------+ ) 8 lock bytes +** | Read1 | Read2 | Rd3 | Rd4 | / +** +-------+-------+------+------+ +** 128: | nBackfillAttempted | +** +-----------------------------+ +** 132: | (unused padding) | +** +-----------------------------+ +*/ /* A block of WALINDEX_LOCK_RESERVED bytes beginning at ** WALINDEX_LOCK_OFFSET is reserved for locks. Since some systems @@ -397,7 +477,6 @@ struct WalCkptInfo { #define WAL_FRAME_HDRSIZE 24 /* Size of write ahead log header, including checksum. */ -/* #define WAL_HDRSIZE 24 */ #define WAL_HDRSIZE 32 /* WAL magic value. Either this value, or the same value with the least @@ -405,14 +484,14 @@ struct WalCkptInfo { ** big-endian format in the first 4 bytes of a WAL file. ** ** If the LSB is set, then the checksums for each frame within the WAL -** file are calculated by treating all data as an array of 32-bit -** big-endian words. Otherwise, they are calculated by interpreting +** file are calculated by treating all data as an array of 32-bit +** big-endian words. Otherwise, they are calculated by interpreting ** all data as 32-bit little-endian words. */ #define WAL_MAGIC 0x377f0682 /* -** Return the offset of frame iFrame in the write-ahead log file, +** Return the offset of frame iFrame in the write-ahead log file, ** assuming a database page size of szPage bytes. The offset returned ** is to the start of the write-ahead log frame-header. */ @@ -423,6 +502,11 @@ struct WalCkptInfo { /* ** An open write-ahead log file is represented by an instance of the ** following object. +** +** writeLock: +** This is usually set to 1 whenever the WRITER lock is held. However, +** if it is set to 2, then the WRITER lock is held but must be released +** by walHandleException() if a SEH exception is thrown. */ struct Wal { sqlite3_vfs *pVfs; /* The VFS used to create pDbFd */ @@ -443,16 +527,29 @@ struct Wal { u8 truncateOnCommit; /* True to truncate WAL file on commit */ u8 syncHeader; /* Fsync the WAL header if true */ u8 padToSectorBoundary; /* Pad transactions out to the next sector */ + u8 bShmUnreliable; /* SHM content is read-only and unreliable */ WalIndexHdr hdr; /* Wal-index header for current transaction */ u32 minFrame; /* Ignore wal frames before this one */ u32 iReCksum; /* On commit, recalculate checksums from here */ const char *zWalName; /* Name of WAL file */ u32 nCkpt; /* Checkpoint sequence counter in the wal-header */ +#ifdef SQLITE_USE_SEH + u32 lockMask; /* Mask of locks held */ + void *pFree; /* Pointer to sqlite3_free() if exception thrown */ + u32 *pWiValue; /* Value to write into apWiData[iWiPg] */ + int iWiPg; /* Write pWiValue into apWiData[iWiPg] */ + int iSysErrno; /* System error code following exception */ +#endif #ifdef SQLITE_DEBUG + int nSehTry; /* Number of nested SEH_TRY{} blocks */ u8 lockError; /* True if a locking error has occurred */ #endif #ifdef SQLITE_ENABLE_SNAPSHOT WalIndexHdr *pSnapshot; /* Start transaction here if not NULL */ + int bGetSnapshot; /* Transaction opened for sqlite3_get_snapshot() */ +#endif +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT + sqlite3 *db; #endif }; @@ -460,7 +557,7 @@ struct Wal { ** Candidate values for Wal.exclusiveMode. */ #define WAL_NORMAL_MODE 0 -#define WAL_EXCLUSIVE_MODE 1 +#define WAL_EXCLUSIVE_MODE 1 #define WAL_HEAPMEMORY_MODE 2 /* @@ -479,7 +576,7 @@ typedef u16 ht_slot; /* ** This structure is used to implement an iterator that loops through ** all frames in the WAL in database page order. Where two or more frames -** correspond to the same database page, the iterator visits only the +** correspond to the same database page, the iterator visits only the ** frame most recently written to the WAL (in other words, the frame with ** the largest index). ** @@ -492,7 +589,7 @@ typedef u16 ht_slot; ** This functionality is used by the checkpoint code (see walCheckpoint()). */ struct WalIterator { - int iPrior; /* Last result returned from the iterator */ + u32 iPrior; /* Last result returned from the iterator */ int nSegment; /* Number of entries in aSegment[] */ struct WalSegment { int iNext; /* Next slot in aIndex[] not yet returned */ @@ -500,9 +597,13 @@ struct WalIterator { u32 *aPgno; /* Array of page numbers. */ int nEntry; /* Nr. of entries in aPgno[] and aIndex[] */ int iZero; /* Frame number associated with aPgno[0] */ - } aSegment[1]; /* One for every 32KB page in the wal-index */ + } aSegment[FLEXARRAY]; /* One for every 32KB page in the wal-index */ }; +/* Size (in bytes) of a WalIterator object suitable for N or fewer segments */ +#define SZ_WALITERATOR(N) \ + (offsetof(WalIterator,aSegment)+(N)*sizeof(struct WalSegment)) + /* ** Define the parameters of the hash tables in the wal-index file. There ** is a hash-table following every HASHTABLE_NPAGE page numbers in the @@ -515,7 +616,7 @@ struct WalIterator { #define HASHTABLE_HASH_1 383 /* Should be prime */ #define HASHTABLE_NSLOT (HASHTABLE_NPAGE*2) /* Must be a power of 2 */ -/* +/* ** The block of page numbers associated with the first hash-table in a ** wal-index is smaller than usual. This is so that there is a complete ** hash-table on each aligned 32KB page of the wal-index. @@ -527,26 +628,146 @@ struct WalIterator { sizeof(ht_slot)*HASHTABLE_NSLOT + HASHTABLE_NPAGE*sizeof(u32) \ ) +/* +** Structured Exception Handling (SEH) is a Windows-specific technique +** for catching exceptions raised while accessing memory-mapped files. +** +** The -DSQLITE_USE_SEH compile-time option means to use SEH to catch and +** deal with system-level errors that arise during WAL -shm file processing. +** Without this compile-time option, any system-level faults that appear +** while accessing the memory-mapped -shm file will cause a process-wide +** signal to be deliver, which will more than likely cause the entire +** process to exit. +*/ +#ifdef SQLITE_USE_SEH +#include <Windows.h> + +/* Beginning of a block of code in which an exception might occur */ +# define SEH_TRY __try { \ + assert( walAssertLockmask(pWal) && pWal->nSehTry==0 ); \ + VVA_ONLY(pWal->nSehTry++); + +/* The end of a block of code in which an exception might occur */ +# define SEH_EXCEPT(X) \ + VVA_ONLY(pWal->nSehTry--); \ + assert( pWal->nSehTry==0 ); \ + } __except( sehExceptionFilter(pWal, GetExceptionCode(), GetExceptionInformation() ) ){ X } + +/* Simulate a memory-mapping fault in the -shm file for testing purposes */ +# define SEH_INJECT_FAULT sehInjectFault(pWal) + +/* +** The second argument is the return value of GetExceptionCode() for the +** current exception. Return EXCEPTION_EXECUTE_HANDLER if the exception code +** indicates that the exception may have been caused by accessing the *-shm +** file mapping. Or EXCEPTION_CONTINUE_SEARCH otherwise. +*/ +static int sehExceptionFilter(Wal *pWal, int eCode, EXCEPTION_POINTERS *p){ + VVA_ONLY(pWal->nSehTry--); + if( eCode==EXCEPTION_IN_PAGE_ERROR ){ + if( p && p->ExceptionRecord && p->ExceptionRecord->NumberParameters>=3 ){ + /* From MSDN: For this type of exception, the first element of the + ** ExceptionInformation[] array is a read-write flag - 0 if the exception + ** was thrown while reading, 1 if while writing. The second element is + ** the virtual address being accessed. The "third array element specifies + ** the underlying NTSTATUS code that resulted in the exception". */ + pWal->iSysErrno = (int)p->ExceptionRecord->ExceptionInformation[2]; + } + return EXCEPTION_EXECUTE_HANDLER; + } + return EXCEPTION_CONTINUE_SEARCH; +} + +/* +** If one is configured, invoke the xTestCallback callback with 650 as +** the argument. If it returns true, throw the same exception that is +** thrown by the system if the *-shm file mapping is accessed after it +** has been invalidated. +*/ +static void sehInjectFault(Wal *pWal){ + int res; + assert( pWal->nSehTry>0 ); + + res = sqlite3FaultSim(650); + if( res!=0 ){ + ULONG_PTR aArg[3]; + aArg[0] = 0; + aArg[1] = 0; + aArg[2] = (ULONG_PTR)res; + RaiseException(EXCEPTION_IN_PAGE_ERROR, 0, 3, (const ULONG_PTR*)aArg); + } +} + +/* +** There are two ways to use this macro. To set a pointer to be freed +** if an exception is thrown: +** +** SEH_FREE_ON_ERROR(0, pPtr); +** +** and to cancel the same: +** +** SEH_FREE_ON_ERROR(pPtr, 0); +** +** In the first case, there must not already be a pointer registered to +** be freed. In the second case, pPtr must be the registered pointer. +*/ +#define SEH_FREE_ON_ERROR(X,Y) \ + assert( (X==0 || Y==0) && pWal->pFree==X ); pWal->pFree = Y + +/* +** There are two ways to use this macro. To arrange for pWal->apWiData[iPg] +** to be set to pValue if an exception is thrown: +** +** SEH_SET_ON_ERROR(iPg, pValue); +** +** and to cancel the same: +** +** SEH_SET_ON_ERROR(0, 0); +*/ +#define SEH_SET_ON_ERROR(X,Y) pWal->iWiPg = X; pWal->pWiValue = Y + +#else +# define SEH_TRY VVA_ONLY(pWal->nSehTry++); +# define SEH_EXCEPT(X) VVA_ONLY(pWal->nSehTry--); assert( pWal->nSehTry==0 ); +# define SEH_INJECT_FAULT assert( pWal->nSehTry>0 ); +# define SEH_FREE_ON_ERROR(X,Y) +# define SEH_SET_ON_ERROR(X,Y) +#endif /* ifdef SQLITE_USE_SEH */ + + /* ** Obtain a pointer to the iPage'th page of the wal-index. The wal-index ** is broken into pages of WALINDEX_PGSZ bytes. Wal-index pages are ** numbered from zero. ** -** If this call is successful, *ppPage is set to point to the wal-index -** page and SQLITE_OK is returned. If an error (an OOM or VFS error) occurs, -** then an SQLite error code is returned and *ppPage is set to 0. +** If the wal-index is currently smaller the iPage pages then the size +** of the wal-index might be increased, but only if it is safe to do +** so. It is safe to enlarge the wal-index if pWal->writeLock is true +** or pWal->exclusiveMode==WAL_HEAPMEMORY_MODE. +** +** Three possible result scenarios: +** +** (1) rc==SQLITE_OK and *ppPage==Requested-Wal-Index-Page +** (2) rc>=SQLITE_ERROR and *ppPage==NULL +** (3) rc==SQLITE_OK and *ppPage==NULL // only if iPage==0 +** +** Scenario (3) can only occur when pWal->writeLock is false and iPage==0 */ -static int walIndexPage(Wal *pWal, int iPage, volatile u32 **ppPage){ +static SQLITE_NOINLINE int walIndexPageRealloc( + Wal *pWal, /* The WAL context */ + int iPage, /* The page we seek */ + volatile u32 **ppPage /* Write the page pointer here */ +){ int rc = SQLITE_OK; /* Enlarge the pWal->apWiData[] array if required */ if( pWal->nWiData<=iPage ){ - int nByte = sizeof(u32*)*(iPage+1); + sqlite3_int64 nByte = sizeof(u32*)*(1+(i64)iPage); volatile u32 **apNew; - apNew = (volatile u32 **)sqlite3_realloc64((void *)pWal->apWiData, nByte); + apNew = (volatile u32 **)sqlite3Realloc((void *)pWal->apWiData, nByte); if( !apNew ){ *ppPage = 0; - return SQLITE_NOMEM; + return SQLITE_NOMEM_BKPT; } memset((void*)&apNew[pWal->nWiData], 0, sizeof(u32*)*(iPage+1-pWal->nWiData)); @@ -555,16 +776,23 @@ static int walIndexPage(Wal *pWal, int iPage, volatile u32 **ppPage){ } /* Request a pointer to the required page from the VFS */ - if( pWal->apWiData[iPage]==0 ){ - if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){ - pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ); - if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM; - }else{ - rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ, - pWal->writeLock, (void volatile **)&pWal->apWiData[iPage] - ); + assert( pWal->apWiData[iPage]==0 ); + if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){ + pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ); + if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM_BKPT; + }else{ + rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ, + pWal->writeLock, (void volatile **)&pWal->apWiData[iPage] + ); + assert( pWal->apWiData[iPage]!=0 + || rc!=SQLITE_OK + || (pWal->writeLock==0 && iPage==0) ); + testcase( pWal->apWiData[iPage]==0 && rc==SQLITE_OK ); + if( rc==SQLITE_OK ){ + if( iPage>0 && sqlite3FaultSim(600) ) rc = SQLITE_NOMEM; + }else if( (rc&0xff)==SQLITE_READONLY ){ + pWal->readOnly |= WAL_SHM_RDONLY; if( rc==SQLITE_READONLY ){ - pWal->readOnly |= WAL_SHM_RDONLY; rc = SQLITE_OK; } } @@ -574,12 +802,24 @@ static int walIndexPage(Wal *pWal, int iPage, volatile u32 **ppPage){ assert( iPage==0 || *ppPage || rc!=SQLITE_OK ); return rc; } +static int walIndexPage( + Wal *pWal, /* The WAL context */ + int iPage, /* The page we seek */ + volatile u32 **ppPage /* Write the page pointer here */ +){ + SEH_INJECT_FAULT; + if( pWal->nWiData<=iPage || (*ppPage = pWal->apWiData[iPage])==0 ){ + return walIndexPageRealloc(pWal, iPage, ppPage); + } + return SQLITE_OK; +} /* ** Return a pointer to the WalCkptInfo structure in the wal-index. */ static volatile WalCkptInfo *walCkptInfo(Wal *pWal){ assert( pWal->nWiData>0 && pWal->apWiData[0] ); + SEH_INJECT_FAULT; return (volatile WalCkptInfo*)&(pWal->apWiData[0][sizeof(WalIndexHdr)/2]); } @@ -588,6 +828,7 @@ static volatile WalCkptInfo *walCkptInfo(Wal *pWal){ */ static volatile WalIndexHdr *walIndexHdr(Wal *pWal){ assert( pWal->nWiData>0 && pWal->apWiData[0] ); + SEH_INJECT_FAULT; return (volatile WalIndexHdr*)pWal->apWiData[0]; } @@ -604,7 +845,7 @@ static volatile WalIndexHdr *walIndexHdr(Wal *pWal){ ) /* -** Generate or extend an 8 byte checksum based on the data in +** Generate or extend an 8 byte checksum based on the data in ** array aByte[] and the initial values of aIn[0] and aIn[1] (or ** initial values of 0 and 0 if aIn==NULL). ** @@ -630,38 +871,75 @@ static void walChecksumBytes( s1 = s2 = 0; } - assert( nByte>=8 ); - assert( (nByte&0x00000007)==0 ); + /* nByte is a multiple of 8 between 8 and 65536 */ + assert( nByte>=8 && (nByte&7)==0 && nByte<=65536 ); - if( nativeCksum ){ + if( !nativeCksum ){ + do { + s1 += BYTESWAP32(aData[0]) + s2; + s2 += BYTESWAP32(aData[1]) + s1; + aData += 2; + }while( aData<aEnd ); + }else if( nByte%64==0 ){ do { s1 += *aData++ + s2; s2 += *aData++ + s1; + s1 += *aData++ + s2; + s2 += *aData++ + s1; + s1 += *aData++ + s2; + s2 += *aData++ + s1; + s1 += *aData++ + s2; + s2 += *aData++ + s1; + s1 += *aData++ + s2; + s2 += *aData++ + s1; + s1 += *aData++ + s2; + s2 += *aData++ + s1; + s1 += *aData++ + s2; + s2 += *aData++ + s1; + s1 += *aData++ + s2; + s2 += *aData++ + s1; }while( aData<aEnd ); }else{ do { - s1 += BYTESWAP32(aData[0]) + s2; - s2 += BYTESWAP32(aData[1]) + s1; - aData += 2; + s1 += *aData++ + s2; + s2 += *aData++ + s1; }while( aData<aEnd ); } + assert( aData==aEnd ); aOut[0] = s1; aOut[1] = s2; } +/* +** If there is the possibility of concurrent access to the SHM file +** from multiple threads and/or processes, then do a memory barrier. +*/ static void walShmBarrier(Wal *pWal){ if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){ sqlite3OsShmBarrier(pWal->pDbFd); } } +/* +** Add the SQLITE_NO_TSAN as part of the return-type of a function +** definition as a hint that the function contains constructs that +** might give false-positive TSAN warnings. +** +** See tag-20200519-1. +*/ +#if defined(__clang__) && !defined(SQLITE_NO_TSAN) +# define SQLITE_NO_TSAN __attribute__((no_sanitize_thread)) +#else +# define SQLITE_NO_TSAN +#endif + /* ** Write the header information in pWal->hdr into the wal-index. ** ** The checksum on pWal->hdr is updated before it is written. */ -static void walIndexWriteHdr(Wal *pWal){ +static SQLITE_NO_TSAN void walIndexWriteHdr(Wal *pWal){ volatile WalIndexHdr *aHdr = walIndexHdr(pWal); const int nCksum = offsetof(WalIndexHdr, aCksum); @@ -669,6 +947,7 @@ static void walIndexWriteHdr(Wal *pWal){ pWal->hdr.isInit = 1; pWal->hdr.iVersion = WALINDEX_MAX_VERSION; walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum); + /* Possible TSAN false-positive. See tag-20200519-1 */ memcpy((void*)&aHdr[1], (const void*)&pWal->hdr, sizeof(WalIndexHdr)); walShmBarrier(pWal); memcpy((void*)&aHdr[0], (const void*)&pWal->hdr, sizeof(WalIndexHdr)); @@ -676,11 +955,11 @@ static void walIndexWriteHdr(Wal *pWal){ /* ** This function encodes a single frame header and writes it to a buffer -** supplied by the caller. A frame-header is made up of a series of +** supplied by the caller. A frame-header is made up of a series of ** 4-byte big-endian integers, as follows: ** ** 0: Page number. -** 4: For commit records, the size of the database image in pages +** 4: For commit records, the size of the database image in pages ** after the commit. For all other records, zero. ** 8: Salt-1 (copied from the wal-header) ** 12: Salt-2 (copied from the wal-header) @@ -731,13 +1010,13 @@ static int walDecodeFrame( assert( WAL_FRAME_HDRSIZE==24 ); /* A frame is only valid if the salt values in the frame-header - ** match the salt values in the wal-header. + ** match the salt values in the wal-header. */ if( memcmp(&pWal->hdr.aSalt, &aFrame[8], 8)!=0 ){ return 0; } - /* A frame is only valid if the page number is creater than zero. + /* A frame is only valid if the page number is greater than zero. */ pgno = sqlite3Get4byte(&aFrame[0]); if( pgno==0 ){ @@ -745,15 +1024,15 @@ static int walDecodeFrame( } /* A frame is only valid if a checksum of the WAL header, - ** all prior frams, the first 16 bytes of this frame-header, - ** and the frame-data matches the checksum in the last 8 + ** all prior frames, the first 16 bytes of this frame-header, + ** and the frame-data matches the checksum in the last 8 ** bytes of this frame-header. */ nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN); walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum); walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum); - if( aCksum[0]!=sqlite3Get4byte(&aFrame[16]) - || aCksum[1]!=sqlite3Get4byte(&aFrame[20]) + if( aCksum[0]!=sqlite3Get4byte(&aFrame[16]) + || aCksum[1]!=sqlite3Get4byte(&aFrame[20]) ){ /* Checksum failed. */ return 0; @@ -788,7 +1067,7 @@ static const char *walLockName(int lockIdx){ } } #endif /*defined(SQLITE_TEST) || defined(SQLITE_DEBUG) */ - + /* ** Set or release locks on the WAL. Locks are either shared or exclusive. @@ -804,13 +1083,19 @@ static int walLockShared(Wal *pWal, int lockIdx){ SQLITE_SHM_LOCK | SQLITE_SHM_SHARED); WALTRACE(("WAL%p: acquire SHARED-%s %s\n", pWal, walLockName(lockIdx), rc ? "failed" : "ok")); - VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && rc!=SQLITE_BUSY); ) + VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && (rc&0xFF)!=SQLITE_BUSY); ) +#ifdef SQLITE_USE_SEH + if( rc==SQLITE_OK ) pWal->lockMask |= (1 << lockIdx); +#endif return rc; } static void walUnlockShared(Wal *pWal, int lockIdx){ if( pWal->exclusiveMode ) return; (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1, SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED); +#ifdef SQLITE_USE_SEH + pWal->lockMask &= ~(1 << lockIdx); +#endif WALTRACE(("WAL%p: release SHARED-%s\n", pWal, walLockName(lockIdx))); } static int walLockExclusive(Wal *pWal, int lockIdx, int n){ @@ -820,13 +1105,21 @@ static int walLockExclusive(Wal *pWal, int lockIdx, int n){ SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE); WALTRACE(("WAL%p: acquire EXCLUSIVE-%s cnt=%d %s\n", pWal, walLockName(lockIdx), n, rc ? "failed" : "ok")); - VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && rc!=SQLITE_BUSY); ) + VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && (rc&0xFF)!=SQLITE_BUSY); ) +#ifdef SQLITE_USE_SEH + if( rc==SQLITE_OK ){ + pWal->lockMask |= (((1<<n)-1) << (SQLITE_SHM_NLOCK+lockIdx)); + } +#endif return rc; } static void walUnlockExclusive(Wal *pWal, int lockIdx, int n){ if( pWal->exclusiveMode ) return; (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, n, SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE); +#ifdef SQLITE_USE_SEH + pWal->lockMask &= ~(((1<<n)-1) << (SQLITE_SHM_NLOCK+lockIdx)); +#endif WALTRACE(("WAL%p: release EXCLUSIVE-%s cnt=%d\n", pWal, walLockName(lockIdx), n)); } @@ -845,48 +1138,52 @@ static int walNextHash(int iPriorHash){ return (iPriorHash+1)&(HASHTABLE_NSLOT-1); } -/* +/* +** An instance of the WalHashLoc object is used to describe the location +** of a page hash table in the wal-index. This becomes the return value +** from walHashGet(). +*/ +typedef struct WalHashLoc WalHashLoc; +struct WalHashLoc { + volatile ht_slot *aHash; /* Start of the wal-index hash table */ + volatile u32 *aPgno; /* aPgno[1] is the page of first frame indexed */ + u32 iZero; /* One less than the frame number of first indexed*/ +}; + +/* ** Return pointers to the hash table and page number array stored on ** page iHash of the wal-index. The wal-index is broken into 32KB pages ** numbered starting from 0. ** -** Set output variable *paHash to point to the start of the hash table -** in the wal-index file. Set *piZero to one less than the frame +** Set output variable pLoc->aHash to point to the start of the hash table +** in the wal-index file. Set pLoc->iZero to one less than the frame ** number of the first frame indexed by this hash table. If a -** slot in the hash table is set to N, it refers to frame number -** (*piZero+N) in the log. +** slot in the hash table is set to N, it refers to frame number +** (pLoc->iZero+N) in the log. ** -** Finally, set *paPgno so that *paPgno[1] is the page number of the -** first frame indexed by the hash table, frame (*piZero+1). +** Finally, set pLoc->aPgno so that pLoc->aPgno[0] is the page number of the +** first frame indexed by the hash table, frame (pLoc->iZero). */ static int walHashGet( Wal *pWal, /* WAL handle */ int iHash, /* Find the iHash'th table */ - volatile ht_slot **paHash, /* OUT: Pointer to hash index */ - volatile u32 **paPgno, /* OUT: Pointer to page number array */ - u32 *piZero /* OUT: Frame associated with *paPgno[0] */ + WalHashLoc *pLoc /* OUT: Hash table location */ ){ int rc; /* Return code */ - volatile u32 *aPgno; - rc = walIndexPage(pWal, iHash, &aPgno); + rc = walIndexPage(pWal, iHash, &pLoc->aPgno); assert( rc==SQLITE_OK || iHash>0 ); - if( rc==SQLITE_OK ){ - u32 iZero; - volatile ht_slot *aHash; - - aHash = (volatile ht_slot *)&aPgno[HASHTABLE_NPAGE]; + if( pLoc->aPgno ){ + pLoc->aHash = (volatile ht_slot *)&pLoc->aPgno[HASHTABLE_NPAGE]; if( iHash==0 ){ - aPgno = &aPgno[WALINDEX_HDR_SIZE/sizeof(u32)]; - iZero = 0; + pLoc->aPgno = &pLoc->aPgno[WALINDEX_HDR_SIZE/sizeof(u32)]; + pLoc->iZero = 0; }else{ - iZero = HASHTABLE_NPAGE_ONE + (iHash-1)*HASHTABLE_NPAGE; + pLoc->iZero = HASHTABLE_NPAGE_ONE + (iHash-1)*HASHTABLE_NPAGE; } - - *paPgno = &aPgno[-1]; - *paHash = aHash; - *piZero = iZero; + }else if( NEVER(rc==SQLITE_OK) ){ + rc = SQLITE_ERROR; } return rc; } @@ -894,7 +1191,7 @@ static int walHashGet( /* ** Return the number of the wal-index page that contains the hash-table ** and page-number array that contain entries corresponding to WAL frame -** iFrame. The wal-index is broken up into 32KB pages. Wal-index pages +** iFrame. The wal-index is broken up into 32KB pages. Wal-index pages ** are numbered starting from 0. */ static int walFramePage(u32 iFrame){ @@ -905,6 +1202,7 @@ static int walFramePage(u32 iFrame){ && (iHash>=2 || iFrame<=HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE) && (iHash<=2 || iFrame>(HASHTABLE_NPAGE_ONE+2*HASHTABLE_NPAGE)) ); + assert( iHash>=0 ); return iHash; } @@ -913,6 +1211,7 @@ static int walFramePage(u32 iFrame){ */ static u32 walFramePgno(Wal *pWal, u32 iFrame){ int iHash = walFramePage(iFrame); + SEH_INJECT_FAULT; if( iHash==0 ){ return pWal->apWiData[0][WALINDEX_HDR_SIZE/sizeof(u32) + iFrame - 1]; } @@ -932,9 +1231,7 @@ static u32 walFramePgno(Wal *pWal, u32 iFrame){ ** actually needed. */ static void walCleanupHash(Wal *pWal){ - volatile ht_slot *aHash = 0; /* Pointer to hash table to clear */ - volatile u32 *aPgno = 0; /* Page number array for hash table */ - u32 iZero = 0; /* frame == (aHash[x]+iZero) */ + WalHashLoc sLoc; /* Hash table location */ int iLimit = 0; /* Zero values greater than this */ int nByte; /* Number of bytes to zero in aPgno[] */ int i; /* Used to iterate through aHash[] */ @@ -946,30 +1243,32 @@ static void walCleanupHash(Wal *pWal){ if( pWal->hdr.mxFrame==0 ) return; - /* Obtain pointers to the hash-table and page-number array containing + /* Obtain pointers to the hash-table and page-number array containing ** the entry that corresponds to frame pWal->hdr.mxFrame. It is guaranteed - ** that the page said hash-table and array reside on is already mapped. + ** that the page said hash-table and array reside on is already mapped.(1) */ assert( pWal->nWiData>walFramePage(pWal->hdr.mxFrame) ); assert( pWal->apWiData[walFramePage(pWal->hdr.mxFrame)] ); - walHashGet(pWal, walFramePage(pWal->hdr.mxFrame), &aHash, &aPgno, &iZero); + i = walHashGet(pWal, walFramePage(pWal->hdr.mxFrame), &sLoc); + if( NEVER(i) ) return; /* Defense-in-depth, in case (1) above is wrong */ /* Zero all hash-table entries that correspond to frame numbers greater ** than pWal->hdr.mxFrame. */ - iLimit = pWal->hdr.mxFrame - iZero; + iLimit = pWal->hdr.mxFrame - sLoc.iZero; assert( iLimit>0 ); for(i=0; i<HASHTABLE_NSLOT; i++){ - if( aHash[i]>iLimit ){ - aHash[i] = 0; + if( sLoc.aHash[i]>iLimit ){ + sLoc.aHash[i] = 0; } } - + /* Zero the entries in the aPgno array that correspond to frames with - ** frame numbers greater than pWal->hdr.mxFrame. + ** frame numbers greater than pWal->hdr.mxFrame. */ - nByte = (int)((char *)aHash - (char *)&aPgno[iLimit+1]); - memset((void *)&aPgno[iLimit+1], 0, nByte); + nByte = (int)((char *)sLoc.aHash - (char *)&sLoc.aPgno[iLimit]); + assert( nByte>=0 ); + memset((void *)&sLoc.aPgno[iLimit], 0, nByte); #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT /* Verify that the every entry in the mapping region is still reachable @@ -978,11 +1277,11 @@ static void walCleanupHash(Wal *pWal){ if( iLimit ){ int j; /* Loop counter */ int iKey; /* Hash key */ - for(j=1; j<=iLimit; j++){ - for(iKey=walHash(aPgno[j]); aHash[iKey]; iKey=walNextHash(iKey)){ - if( aHash[iKey]==j ) break; + for(j=0; j<iLimit; j++){ + for(iKey=walHash(sLoc.aPgno[j]);sLoc.aHash[iKey];iKey=walNextHash(iKey)){ + if( sLoc.aHash[iKey]==j+1 ) break; } - assert( aHash[iKey]==j ); + assert( sLoc.aHash[iKey]==j+1 ); } } #endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */ @@ -995,11 +1294,9 @@ static void walCleanupHash(Wal *pWal){ */ static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){ int rc; /* Return code */ - u32 iZero = 0; /* One less than frame number of aPgno[1] */ - volatile u32 *aPgno = 0; /* Page number array */ - volatile ht_slot *aHash = 0; /* Hash table */ + WalHashLoc sLoc; /* Wal-index hash table location */ - rc = walHashGet(pWal, walFramePage(iFrame), &aHash, &aPgno, &iZero); + rc = walHashGet(pWal, walFramePage(iFrame), &sLoc); /* Assuming the wal-index file was successfully mapped, populate the ** page number array and hash table entry. @@ -1009,35 +1306,36 @@ static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){ int idx; /* Value to write to hash-table slot */ int nCollide; /* Number of hash collisions */ - idx = iFrame - iZero; + idx = iFrame - sLoc.iZero; assert( idx <= HASHTABLE_NSLOT/2 + 1 ); - + /* If this is the first entry to be added to this hash-table, zero the - ** entire hash table and aPgno[] array before proceeding. + ** entire hash table and aPgno[] array before proceeding. */ if( idx==1 ){ - int nByte = (int)((u8 *)&aHash[HASHTABLE_NSLOT] - (u8 *)&aPgno[1]); - memset((void*)&aPgno[1], 0, nByte); + int nByte = (int)((u8*)&sLoc.aHash[HASHTABLE_NSLOT] - (u8*)sLoc.aPgno); + assert( nByte>=0 ); + memset((void*)sLoc.aPgno, 0, nByte); } /* If the entry in aPgno[] is already set, then the previous writer ** must have exited unexpectedly in the middle of a transaction (after - ** writing one or more dirty pages to the WAL to free up memory). - ** Remove the remnants of that writers uncommitted transaction from + ** writing one or more dirty pages to the WAL to free up memory). + ** Remove the remnants of that writers uncommitted transaction from ** the hash-table before writing any new entries. */ - if( aPgno[idx] ){ + if( sLoc.aPgno[idx-1] ){ walCleanupHash(pWal); - assert( !aPgno[idx] ); + assert( !sLoc.aPgno[idx-1] ); } /* Write the aPgno[] array entry and the hash-table slot. */ nCollide = idx; - for(iKey=walHash(iPage); aHash[iKey]; iKey=walNextHash(iKey)){ + for(iKey=walHash(iPage); sLoc.aHash[iKey]; iKey=walNextHash(iKey)){ if( (nCollide--)==0 ) return SQLITE_CORRUPT_BKPT; } - aPgno[idx] = iPage; - aHash[iKey] = (ht_slot)idx; + sLoc.aPgno[idx-1] = iPage; + AtomicStore(&sLoc.aHash[iKey], (ht_slot)idx); #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT /* Verify that the number of entries in the hash table exactly equals @@ -1046,7 +1344,7 @@ static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){ { int i; /* Loop counter */ int nEntry = 0; /* Number of entries in the hash table */ - for(i=0; i<HASHTABLE_NSLOT; i++){ if( aHash[i] ) nEntry++; } + for(i=0; i<HASHTABLE_NSLOT; i++){ if( sLoc.aHash[i] ) nEntry++; } assert( nEntry==idx ); } @@ -1057,23 +1355,24 @@ static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){ */ if( (idx&0x3ff)==0 ){ int i; /* Loop counter */ - for(i=1; i<=idx; i++){ - for(iKey=walHash(aPgno[i]); aHash[iKey]; iKey=walNextHash(iKey)){ - if( aHash[iKey]==i ) break; + for(i=0; i<idx; i++){ + for(iKey=walHash(sLoc.aPgno[i]); + sLoc.aHash[iKey]; + iKey=walNextHash(iKey)){ + if( sLoc.aHash[iKey]==i+1 ) break; } - assert( aHash[iKey]==i ); + assert( sLoc.aHash[iKey]==i+1 ); } } #endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */ } - return rc; } /* -** Recover the wal-index by reading the write-ahead log file. +** Recover the wal-index by reading the write-ahead log file. ** ** This routine first tries to establish an exclusive lock on the ** wal-index to prevent other threads/processes from doing anything @@ -1087,7 +1386,6 @@ static int walIndexRecover(Wal *pWal){ i64 nSize; /* Size of log file */ u32 aFrameCksum[2] = {0, 0}; int iLock; /* Lock offset to lock for checkpoint */ - int nLock; /* Number of locks to hold */ /* Obtain an exclusive lock on all byte in the locking range not already ** locked by the caller. The caller is guaranteed to have locked the @@ -1100,11 +1398,11 @@ static int walIndexRecover(Wal *pWal){ assert( WAL_CKPT_LOCK==WAL_ALL_BUT_WRITE ); assert( pWal->writeLock ); iLock = WAL_ALL_BUT_WRITE + pWal->ckptLock; - nLock = SQLITE_SHM_NLOCK - iLock; - rc = walLockExclusive(pWal, iLock, nLock); + rc = walLockExclusive(pWal, iLock, WAL_READ_LOCK(0)-iLock); if( rc ){ return rc; } + WALTRACE(("WAL%p: recovery begin...\n", pWal)); memset(&pWal->hdr, 0, sizeof(WalIndexHdr)); @@ -1116,15 +1414,16 @@ static int walIndexRecover(Wal *pWal){ if( nSize>WAL_HDRSIZE ){ u8 aBuf[WAL_HDRSIZE]; /* Buffer to load WAL header into */ + u32 *aPrivate = 0; /* Heap copy of *-shm hash being populated */ u8 *aFrame = 0; /* Malloc'd buffer to load entire frame */ int szFrame; /* Number of bytes in buffer aFrame[] */ u8 *aData; /* Pointer to data part of aFrame buffer */ - int iFrame; /* Index of last frame read */ - i64 iOffset; /* Next offset to read from log file */ int szPage; /* Page size according to the log */ u32 magic; /* Magic value read from WAL header */ u32 version; /* Magic value read from WAL header */ int isValid; /* True if this frame is valid */ + u32 iPg; /* Current 32KB wal-index page */ + u32 iLastFrame; /* Last frame in wal, based on nSize alone */ /* Read in the WAL header. */ rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0); @@ -1133,16 +1432,16 @@ static int walIndexRecover(Wal *pWal){ } /* If the database page size is not a power of two, or is greater than - ** SQLITE_MAX_PAGE_SIZE, conclude that the WAL file contains no valid + ** SQLITE_MAX_PAGE_SIZE, conclude that the WAL file contains no valid ** data. Similarly, if the 'magic' value is invalid, ignore the whole ** WAL file. */ magic = sqlite3Get4byte(&aBuf[0]); szPage = sqlite3Get4byte(&aBuf[8]); - if( (magic&0xFFFFFFFE)!=WAL_MAGIC - || szPage&(szPage-1) - || szPage>SQLITE_MAX_PAGE_SIZE - || szPage<512 + if( (magic&0xFFFFFFFE)!=WAL_MAGIC + || szPage&(szPage-1) + || szPage>SQLITE_MAX_PAGE_SIZE + || szPage<512 ){ goto finished; } @@ -1152,7 +1451,7 @@ static int walIndexRecover(Wal *pWal){ memcpy(&pWal->hdr.aSalt, &aBuf[16], 8); /* Verify that the WAL header checksum is correct */ - walChecksumBytes(pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN, + walChecksumBytes(pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN, aBuf, WAL_HDRSIZE-2*4, 0, pWal->hdr.aFrameCksum ); if( pWal->hdr.aFrameCksum[0]!=sqlite3Get4byte(&aBuf[24]) @@ -1171,40 +1470,90 @@ static int walIndexRecover(Wal *pWal){ /* Malloc a buffer to read frames into. */ szFrame = szPage + WAL_FRAME_HDRSIZE; - aFrame = (u8 *)sqlite3_malloc64(szFrame); + aFrame = (u8 *)sqlite3_malloc64(szFrame + WALINDEX_PGSZ); + SEH_FREE_ON_ERROR(0, aFrame); if( !aFrame ){ - rc = SQLITE_NOMEM; + rc = SQLITE_NOMEM_BKPT; goto recovery_error; } aData = &aFrame[WAL_FRAME_HDRSIZE]; + aPrivate = (u32*)&aData[szPage]; /* Read all frames from the log file. */ - iFrame = 0; - for(iOffset=WAL_HDRSIZE; (iOffset+szFrame)<=nSize; iOffset+=szFrame){ - u32 pgno; /* Database page number for frame */ - u32 nTruncate; /* dbsize field from frame header */ - - /* Read and decode the next log frame. */ - iFrame++; - rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset); - if( rc!=SQLITE_OK ) break; - isValid = walDecodeFrame(pWal, &pgno, &nTruncate, aData, aFrame); - if( !isValid ) break; - rc = walIndexAppend(pWal, iFrame, pgno); - if( rc!=SQLITE_OK ) break; - - /* If nTruncate is non-zero, this is a commit record. */ - if( nTruncate ){ - pWal->hdr.mxFrame = iFrame; - pWal->hdr.nPage = nTruncate; - pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16)); - testcase( szPage<=32768 ); - testcase( szPage>=65536 ); - aFrameCksum[0] = pWal->hdr.aFrameCksum[0]; - aFrameCksum[1] = pWal->hdr.aFrameCksum[1]; + iLastFrame = (nSize - WAL_HDRSIZE) / szFrame; + for(iPg=0; iPg<=(u32)walFramePage(iLastFrame); iPg++){ + u32 *aShare; + u32 iFrame; /* Index of last frame read */ + u32 iLast = MIN(iLastFrame, HASHTABLE_NPAGE_ONE+iPg*HASHTABLE_NPAGE); + u32 iFirst = 1 + (iPg==0?0:HASHTABLE_NPAGE_ONE+(iPg-1)*HASHTABLE_NPAGE); + u32 nHdr, nHdr32; + rc = walIndexPage(pWal, iPg, (volatile u32**)&aShare); + assert( aShare!=0 || rc!=SQLITE_OK ); + if( aShare==0 ) break; + SEH_SET_ON_ERROR(iPg, aShare); + pWal->apWiData[iPg] = aPrivate; + + for(iFrame=iFirst; iFrame<=iLast; iFrame++){ + i64 iOffset = walFrameOffset(iFrame, szPage); + u32 pgno; /* Database page number for frame */ + u32 nTruncate; /* dbsize field from frame header */ + + /* Read and decode the next log frame. */ + rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset); + if( rc!=SQLITE_OK ) break; + isValid = walDecodeFrame(pWal, &pgno, &nTruncate, aData, aFrame); + if( !isValid ) break; + rc = walIndexAppend(pWal, iFrame, pgno); + if( NEVER(rc!=SQLITE_OK) ) break; + + /* If nTruncate is non-zero, this is a commit record. */ + if( nTruncate ){ + pWal->hdr.mxFrame = iFrame; + pWal->hdr.nPage = nTruncate; + pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16)); + testcase( szPage<=32768 ); + testcase( szPage>=65536 ); + aFrameCksum[0] = pWal->hdr.aFrameCksum[0]; + aFrameCksum[1] = pWal->hdr.aFrameCksum[1]; + } + } + pWal->apWiData[iPg] = aShare; + SEH_SET_ON_ERROR(0,0); + nHdr = (iPg==0 ? WALINDEX_HDR_SIZE : 0); + nHdr32 = nHdr / sizeof(u32); +#ifndef SQLITE_SAFER_WALINDEX_RECOVERY + /* Memcpy() should work fine here, on all reasonable implementations. + ** Technically, memcpy() might change the destination to some + ** intermediate value before setting to the final value, and that might + ** cause a concurrent reader to malfunction. Memcpy() is allowed to + ** do that, according to the spec, but no memcpy() implementation that + ** we know of actually does that, which is why we say that memcpy() + ** is safe for this. Memcpy() is certainly a lot faster. + */ + memcpy(&aShare[nHdr32], &aPrivate[nHdr32], WALINDEX_PGSZ-nHdr); +#else + /* In the event that some platform is found for which memcpy() + ** changes the destination to some intermediate value before + ** setting the final value, this alternative copy routine is + ** provided. + */ + { + int i; + for(i=nHdr32; i<WALINDEX_PGSZ/sizeof(u32); i++){ + if( aShare[i]!=aPrivate[i] ){ + /* Atomic memory operations are not required here because if + ** the value needs to be changed, that means it is not being + ** accessed concurrently. */ + aShare[i] = aPrivate[i]; + } + } } +#endif + SEH_INJECT_FAULT; + if( iFrame<=iLast ) break; } + SEH_FREE_ON_ERROR(aFrame, 0); sqlite3_free(aFrame); } @@ -1216,16 +1565,28 @@ static int walIndexRecover(Wal *pWal){ pWal->hdr.aFrameCksum[1] = aFrameCksum[1]; walIndexWriteHdr(pWal); - /* Reset the checkpoint-header. This is safe because this thread is - ** currently holding locks that exclude all other readers, writers and - ** checkpointers. + /* Reset the checkpoint-header. This is safe because this thread is + ** currently holding locks that exclude all other writers and + ** checkpointers. Then set the values of read-mark slots 1 through N. */ pInfo = walCkptInfo(pWal); pInfo->nBackfill = 0; pInfo->nBackfillAttempted = pWal->hdr.mxFrame; pInfo->aReadMark[0] = 0; - for(i=1; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED; - if( pWal->hdr.mxFrame ) pInfo->aReadMark[1] = pWal->hdr.mxFrame; + for(i=1; i<WAL_NREADER; i++){ + rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1); + if( rc==SQLITE_OK ){ + if( i==1 && pWal->hdr.mxFrame ){ + pInfo->aReadMark[i] = pWal->hdr.mxFrame; + }else{ + pInfo->aReadMark[i] = READMARK_NOT_USED; + } + SEH_INJECT_FAULT; + walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1); + }else if( rc!=SQLITE_BUSY ){ + goto recovery_error; + } + } /* If more than one frame was recovered from the log file, report an ** event via sqlite3_log(). This is to help with identifying performance @@ -1242,7 +1603,7 @@ static int walIndexRecover(Wal *pWal){ recovery_error: WALTRACE(("WAL%p: recovery %s\n", pWal, rc ? "failed" : "ok")); - walUnlockExclusive(pWal, iLock, nLock); + walUnlockExclusive(pWal, iLock, WAL_READ_LOCK(0)-iLock); return rc; } @@ -1250,19 +1611,20 @@ static int walIndexRecover(Wal *pWal){ ** Close an open wal-index. */ static void walIndexClose(Wal *pWal, int isDelete){ - if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){ + if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE || pWal->bShmUnreliable ){ int i; for(i=0; i<pWal->nWiData; i++){ sqlite3_free((void *)pWal->apWiData[i]); pWal->apWiData[i] = 0; } - }else{ + } + if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){ sqlite3OsShmUnmap(pWal->pDbFd, isDelete); } } -/* -** Open a connection to the WAL file zWalName. The database file must +/* +** Open a connection to the WAL file zWalName. The database file must ** already be opened on connection pDbFd. The buffer that zWalName points ** to must remain valid for the lifetime of the returned Wal* handle. ** @@ -1272,7 +1634,7 @@ static void walIndexClose(Wal *pWal, int isDelete){ ** were to do this just after this client opened one of these files, the ** system would be badly broken. ** -** If the log file is successfully opened, SQLITE_OK is returned and +** If the log file is successfully opened, SQLITE_OK is returned and ** *ppWal is set to point to a new WAL handle. If an error occurs, ** an SQLite error code is returned and *ppWal is left unmodified. */ @@ -1291,14 +1653,43 @@ int sqlite3WalOpen( assert( zWalName && zWalName[0] ); assert( pDbFd ); + /* Verify the values of various constants. Any changes to the values + ** of these constants would result in an incompatible on-disk format + ** for the -shm file. Any change that causes one of these asserts to + ** fail is a backward compatibility problem, even if the change otherwise + ** works. + ** + ** This table also serves as a helpful cross-reference when trying to + ** interpret hex dumps of the -shm file. + */ + assert( 48 == sizeof(WalIndexHdr) ); + assert( 40 == sizeof(WalCkptInfo) ); + assert( 120 == WALINDEX_LOCK_OFFSET ); + assert( 136 == WALINDEX_HDR_SIZE ); + assert( 4096 == HASHTABLE_NPAGE ); + assert( 4062 == HASHTABLE_NPAGE_ONE ); + assert( 8192 == HASHTABLE_NSLOT ); + assert( 383 == HASHTABLE_HASH_1 ); + assert( 32768 == WALINDEX_PGSZ ); + assert( 8 == SQLITE_SHM_NLOCK ); + assert( 5 == WAL_NREADER ); + assert( 24 == WAL_FRAME_HDRSIZE ); + assert( 32 == WAL_HDRSIZE ); + assert( 120 == WALINDEX_LOCK_OFFSET + WAL_WRITE_LOCK ); + assert( 121 == WALINDEX_LOCK_OFFSET + WAL_CKPT_LOCK ); + assert( 122 == WALINDEX_LOCK_OFFSET + WAL_RECOVER_LOCK ); + assert( 123 == WALINDEX_LOCK_OFFSET + WAL_READ_LOCK(0) ); + assert( 124 == WALINDEX_LOCK_OFFSET + WAL_READ_LOCK(1) ); + assert( 125 == WALINDEX_LOCK_OFFSET + WAL_READ_LOCK(2) ); + assert( 126 == WALINDEX_LOCK_OFFSET + WAL_READ_LOCK(3) ); + assert( 127 == WALINDEX_LOCK_OFFSET + WAL_READ_LOCK(4) ); + /* In the amalgamation, the os_unix.c and os_win.c source files come before ** this source file. Verify that the #defines of the locking byte offsets ** in os_unix.c and os_win.c agree with the WALINDEX_LOCK_OFFSET value. ** For that matter, if the lock offset ever changes from its initial design ** value of 120, we need to know that so there is an assert() to check it. */ - assert( 120==WALINDEX_LOCK_OFFSET ); - assert( 136==WALINDEX_HDR_SIZE ); #ifdef WIN_SHM_BASE assert( WIN_SHM_BASE==WALINDEX_LOCK_OFFSET ); #endif @@ -1311,7 +1702,7 @@ int sqlite3WalOpen( *ppWal = 0; pRet = (Wal*)sqlite3MallocZero(sizeof(Wal) + pVfs->szOsFile); if( !pRet ){ - return SQLITE_NOMEM; + return SQLITE_NOMEM_BKPT; } pRet->pVfs = pVfs; @@ -1348,7 +1739,7 @@ int sqlite3WalOpen( } /* -** Change the size to which the WAL file is trucated on each reset. +** Change the size to which the WAL file is truncated on each reset. */ void sqlite3WalLimit(Wal *pWal, i64 iLimit){ if( pWal ) pWal->mxWalSize = iLimit; @@ -1436,7 +1827,7 @@ static void walMerge( ht_slot logpage; Pgno dbpage; - if( (iLeft<nLeft) + if( (iLeft<nLeft) && (iRight>=nRight || aContent[aLeft[iLeft]]<aContent[aRight[iRight]]) ){ logpage = aLeft[iLeft++]; @@ -1534,7 +1925,7 @@ static void walMergesort( #endif } -/* +/* ** Free an iterator allocated by walIteratorInit(). */ static void walIteratorFree(WalIterator *p){ @@ -1542,9 +1933,10 @@ static void walIteratorFree(WalIterator *p){ } /* -** Construct a WalInterator object that can be used to loop over all -** pages in the WAL in ascending order. The caller must hold the checkpoint -** lock. +** Construct a WalInterator object that can be used to loop over all +** pages in the WAL following frame nBackfill in ascending order. Frames +** nBackfill or earlier may be included - excluding them is an optimization +** only. The caller must hold the checkpoint lock. ** ** On success, make *pp point to the newly allocated WalInterator object ** return SQLITE_OK. Otherwise, return an error code. If this routine @@ -1553,11 +1945,11 @@ static void walIteratorFree(WalIterator *p){ ** The calling routine should invoke walIteratorFree() to destroy the ** WalIterator object when it has finished with it. */ -static int walIteratorInit(Wal *pWal, WalIterator **pp){ +static int walIteratorInit(Wal *pWal, u32 nBackfill, WalIterator **pp){ WalIterator *p; /* Return value */ int nSegment; /* Number of segments to merge */ u32 iLast; /* Last frame in log */ - int nByte; /* Number of bytes to allocate */ + sqlite3_int64 nByte; /* Number of bytes to allocate */ int i; /* Iterator variable */ ht_slot *aTmp; /* Temp space used by merge-sort */ int rc = SQLITE_OK; /* Return Code */ @@ -1570,65 +1962,136 @@ static int walIteratorInit(Wal *pWal, WalIterator **pp){ /* Allocate space for the WalIterator object. */ nSegment = walFramePage(iLast) + 1; - nByte = sizeof(WalIterator) - + (nSegment-1)*sizeof(struct WalSegment) + nByte = SZ_WALITERATOR(nSegment) + iLast*sizeof(ht_slot); - p = (WalIterator *)sqlite3_malloc64(nByte); + p = (WalIterator *)sqlite3_malloc64(nByte + + sizeof(ht_slot) * (iLast>HASHTABLE_NPAGE?HASHTABLE_NPAGE:iLast) + ); if( !p ){ - return SQLITE_NOMEM; + return SQLITE_NOMEM_BKPT; } memset(p, 0, nByte); p->nSegment = nSegment; + aTmp = (ht_slot*)&(((u8*)p)[nByte]); + SEH_FREE_ON_ERROR(0, p); + for(i=walFramePage(nBackfill+1); rc==SQLITE_OK && i<nSegment; i++){ + WalHashLoc sLoc; - /* Allocate temporary space used by the merge-sort routine. This block - ** of memory will be freed before this function returns. - */ - aTmp = (ht_slot *)sqlite3_malloc64( - sizeof(ht_slot) * (iLast>HASHTABLE_NPAGE?HASHTABLE_NPAGE:iLast) - ); - if( !aTmp ){ - rc = SQLITE_NOMEM; - } - - for(i=0; rc==SQLITE_OK && i<nSegment; i++){ - volatile ht_slot *aHash; - u32 iZero; - volatile u32 *aPgno; - - rc = walHashGet(pWal, i, &aHash, &aPgno, &iZero); + rc = walHashGet(pWal, i, &sLoc); if( rc==SQLITE_OK ){ int j; /* Counter variable */ int nEntry; /* Number of entries in this segment */ ht_slot *aIndex; /* Sorted index for this segment */ - aPgno++; if( (i+1)==nSegment ){ - nEntry = (int)(iLast - iZero); + nEntry = (int)(iLast - sLoc.iZero); }else{ - nEntry = (int)((u32*)aHash - (u32*)aPgno); + nEntry = (int)((u32*)sLoc.aHash - (u32*)sLoc.aPgno); } - aIndex = &((ht_slot *)&p->aSegment[p->nSegment])[iZero]; - iZero++; - + aIndex = &((ht_slot *)&p->aSegment[p->nSegment])[sLoc.iZero]; + sLoc.iZero++; + for(j=0; j<nEntry; j++){ aIndex[j] = (ht_slot)j; } - walMergesort((u32 *)aPgno, aTmp, aIndex, &nEntry); - p->aSegment[i].iZero = iZero; + walMergesort((u32 *)sLoc.aPgno, aTmp, aIndex, &nEntry); + p->aSegment[i].iZero = sLoc.iZero; p->aSegment[i].nEntry = nEntry; p->aSegment[i].aIndex = aIndex; - p->aSegment[i].aPgno = (u32 *)aPgno; + p->aSegment[i].aPgno = (u32 *)sLoc.aPgno; } } - sqlite3_free(aTmp); - if( rc!=SQLITE_OK ){ + SEH_FREE_ON_ERROR(p, 0); walIteratorFree(p); + p = 0; } *pp = p; return rc; } +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT + + +/* +** Attempt to enable blocking locks that block for nMs ms. Return 1 if +** blocking locks are successfully enabled, or 0 otherwise. +*/ +static int walEnableBlockingMs(Wal *pWal, int nMs){ + int rc = sqlite3OsFileControl( + pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT, (void*)&nMs + ); + return (rc==SQLITE_OK); +} + +/* +** Attempt to enable blocking locks. Blocking locks are enabled only if (a) +** they are supported by the VFS, and (b) the database handle is configured +** with a busy-timeout. Return 1 if blocking locks are successfully enabled, +** or 0 otherwise. +*/ +static int walEnableBlocking(Wal *pWal){ + int res = 0; + if( pWal->db ){ + int tmout = pWal->db->setlkTimeout; + if( tmout ){ + res = walEnableBlockingMs(pWal, tmout); + } + } + return res; +} + +/* +** Disable blocking locks. +*/ +static void walDisableBlocking(Wal *pWal){ + int tmout = 0; + sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT, (void*)&tmout); +} + +/* +** If parameter bLock is true, attempt to enable blocking locks, take +** the WRITER lock, and then disable blocking locks. If blocking locks +** cannot be enabled, no attempt to obtain the WRITER lock is made. Return +** an SQLite error code if an error occurs, or SQLITE_OK otherwise. It is not +** an error if blocking locks can not be enabled. +** +** If the bLock parameter is false and the WRITER lock is held, release it. +*/ +int sqlite3WalWriteLock(Wal *pWal, int bLock){ + int rc = SQLITE_OK; + assert( pWal->readLock<0 || bLock==0 ); + if( bLock ){ + assert( pWal->db ); + if( walEnableBlocking(pWal) ){ + rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1); + if( rc==SQLITE_OK ){ + pWal->writeLock = 1; + } + walDisableBlocking(pWal); + } + }else if( pWal->writeLock ){ + walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1); + pWal->writeLock = 0; + } + return rc; +} + +/* +** Set the database handle used to determine if blocking locks are required. +*/ +void sqlite3WalDb(Wal *pWal, sqlite3 *db){ + pWal->db = db; +} + +#else +# define walEnableBlocking(x) 0 +# define walDisableBlocking(x) +# define walEnableBlockingMs(pWal, ms) 0 +# define sqlite3WalDb(pWal, db) +#endif /* ifdef SQLITE_ENABLE_SETLK_TIMEOUT */ + + /* ** Attempt to obtain the exclusive WAL lock defined by parameters lockIdx and ** n. If the attempt fails and parameter xBusy is not NULL, then it is a @@ -1646,6 +2109,12 @@ static int walBusyLock( do { rc = walLockExclusive(pWal, lockIdx, n); }while( xBusy && rc==SQLITE_BUSY && xBusy(pBusyArg) ); +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT + if( rc==SQLITE_BUSY_TIMEOUT ){ + walDisableBlocking(pWal); + rc = SQLITE_BUSY; + } +#endif return rc; } @@ -1670,8 +2139,8 @@ static int walPagesize(Wal *pWal){ ** client to write to the database (which may be this one) does so by ** writing frames into the start of the log file. ** -** The value of parameter salt1 is used as the aSalt[1] value in the -** new wal-index header. It should be passed a pseudo-random value (i.e. +** The value of parameter salt1 is used as the aSalt[1] value in the +** new wal-index header. It should be passed a pseudo-random value (i.e. ** one obtained from sqlite3_randomness()). */ static void walRestartHdr(Wal *pWal, u32 salt1){ @@ -1683,7 +2152,7 @@ static void walRestartHdr(Wal *pWal, u32 salt1){ sqlite3Put4byte((u8*)&aSalt[0], 1 + sqlite3Get4byte((u8*)&aSalt[0])); memcpy(&pWal->hdr.aSalt[1], &salt1, 4); walIndexWriteHdr(pWal); - pInfo->nBackfill = 0; + AtomicStore(&pInfo->nBackfill, 0); pInfo->nBackfillAttempted = 0; pInfo->aReadMark[1] = 0; for(i=2; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED; @@ -1699,8 +2168,8 @@ static void walRestartHdr(Wal *pWal, u32 salt1){ ** that a concurrent reader might be using. ** ** All I/O barrier operations (a.k.a fsyncs) occur in this routine when -** SQLite is in WAL-mode in synchronous=NORMAL. That means that if -** checkpoints are always run by a background thread or background +** SQLite is in WAL-mode in synchronous=NORMAL. That means that if +** checkpoints are always run by a background thread or background ** process, foreground threads will never block on a lengthy fsync call. ** ** Fsync is called on the WAL before writing content out of the WAL and @@ -1713,7 +2182,7 @@ static void walRestartHdr(Wal *pWal, u32 salt1){ ** database file. ** ** This routine uses and updates the nBackfill field of the wal-index header. -** This is the only routine that will increase the value of nBackfill. +** This is the only routine that will increase the value of nBackfill. ** (A WAL reset or recovery will revert nBackfill to zero, but not increase ** its value.) ** @@ -1723,6 +2192,7 @@ static void walRestartHdr(Wal *pWal, u32 salt1){ */ static int walCheckpoint( Wal *pWal, /* Wal connection */ + sqlite3 *db, /* Check for interrupts on this handle */ int eMode, /* One of PASSIVE, FULL or RESTART */ int (*xBusy)(void*), /* Function to call when busy */ void *pBusyArg, /* Context argument for xBusyHandler */ @@ -1745,13 +2215,6 @@ static int walCheckpoint( pInfo = walCkptInfo(pWal); if( pInfo->nBackfill<pWal->hdr.mxFrame ){ - /* Allocate the iterator */ - rc = walIteratorInit(pWal, &pIter); - if( rc!=SQLITE_OK ){ - return rc; - } - assert( pIter ); - /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked ** in the SQLITE_CHECKPOINT_PASSIVE mode. */ assert( eMode!=SQLITE_CHECKPOINT_PASSIVE || xBusy==0 ); @@ -1764,20 +2227,13 @@ static int walCheckpoint( mxSafeFrame = pWal->hdr.mxFrame; mxPage = pWal->hdr.nPage; for(i=1; i<WAL_NREADER; i++){ - /* Thread-sanitizer reports that the following is an unsafe read, - ** as some other thread may be in the process of updating the value - ** of the aReadMark[] slot. The assumption here is that if that is - ** happening, the other client may only be increasing the value, - ** not decreasing it. So assuming either that either the "old" or - ** "new" version of the value is read, and not some arbitrary value - ** that would never be written by a real client, things are still - ** safe. */ - u32 y = pInfo->aReadMark[i]; + u32 y = AtomicLoad(pInfo->aReadMark+i); SEH_INJECT_FAULT; if( mxSafeFrame>y ){ assert( y<=pWal->hdr.mxFrame ); rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(i), 1); if( rc==SQLITE_OK ){ - pInfo->aReadMark[i] = (i==1 ? mxSafeFrame : READMARK_NOT_USED); + u32 iMark = (i==1 ? mxSafeFrame : READMARK_NOT_USED); + AtomicStore(pInfo->aReadMark+i, iMark); SEH_INJECT_FAULT; walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1); }else if( rc==SQLITE_BUSY ){ mxSafeFrame = y; @@ -1788,35 +2244,52 @@ static int walCheckpoint( } } - if( pInfo->nBackfill<mxSafeFrame - && (rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(0),1))==SQLITE_OK + /* Allocate the iterator */ + if( pInfo->nBackfill<mxSafeFrame ){ + rc = walIteratorInit(pWal, pInfo->nBackfill, &pIter); + assert( rc==SQLITE_OK || pIter==0 ); + } + + if( pIter + && (rc = walBusyLock(pWal,xBusy,pBusyArg,WAL_READ_LOCK(0),1))==SQLITE_OK ){ - i64 nSize; /* Current size of database file */ u32 nBackfill = pInfo->nBackfill; - - pInfo->nBackfillAttempted = mxSafeFrame; + pInfo->nBackfillAttempted = mxSafeFrame; SEH_INJECT_FAULT; /* Sync the WAL to disk */ - if( sync_flags ){ - rc = sqlite3OsSync(pWal->pWalFd, sync_flags); - } + rc = sqlite3OsSync(pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags)); /* If the database may grow as a result of this checkpoint, hint ** about the eventual size of the db file to the VFS layer. */ if( rc==SQLITE_OK ){ i64 nReq = ((i64)mxPage * szPage); + i64 nSize; /* Current size of database file */ + sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_CKPT_START, 0); rc = sqlite3OsFileSize(pWal->pDbFd, &nSize); if( rc==SQLITE_OK && nSize<nReq ){ - sqlite3OsFileControlHint(pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT, &nReq); + if( (nSize+65536+(i64)pWal->hdr.mxFrame*szPage)<nReq ){ + /* If the size of the final database is larger than the current + ** database plus the amount of data in the wal file, plus the + ** maximum size of the pending-byte page (65536 bytes), then + ** must be corruption somewhere. */ + rc = SQLITE_CORRUPT_BKPT; + }else{ + sqlite3OsFileControlHint(pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT,&nReq); + } } - } + } /* Iterate through the contents of the WAL, copying data to the db file */ while( rc==SQLITE_OK && 0==walIteratorNext(pIter, &iDbpage, &iFrame) ){ i64 iOffset; assert( walFramePgno(pWal, iFrame)==iDbpage ); + SEH_INJECT_FAULT; + if( AtomicLoad(&db->u1.isInterrupted) ){ + rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_INTERRUPT; + break; + } if( iFrame<=nBackfill || iFrame>mxSafeFrame || iDbpage>mxPage ){ continue; } @@ -1829,6 +2302,7 @@ static int walCheckpoint( rc = sqlite3OsWrite(pWal->pDbFd, zBuf, szPage, iOffset); if( rc!=SQLITE_OK ) break; } + sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_CKPT_DONE, 0); /* If work was actually accomplished... */ if( rc==SQLITE_OK ){ @@ -1836,12 +2310,12 @@ static int walCheckpoint( i64 szDb = pWal->hdr.nPage*(i64)szPage; testcase( IS_BIG_INT(szDb) ); rc = sqlite3OsTruncate(pWal->pDbFd, szDb); - if( rc==SQLITE_OK && sync_flags ){ - rc = sqlite3OsSync(pWal->pDbFd, sync_flags); + if( rc==SQLITE_OK ){ + rc = sqlite3OsSync(pWal->pDbFd, CKPT_SYNC_FLAGS(sync_flags)); } } if( rc==SQLITE_OK ){ - pInfo->nBackfill = mxSafeFrame; + AtomicStore(&pInfo->nBackfill, mxSafeFrame); SEH_INJECT_FAULT; } } @@ -1857,12 +2331,13 @@ static int walCheckpoint( } /* If this is an SQLITE_CHECKPOINT_RESTART or TRUNCATE operation, and the - ** entire wal file has been copied into the database file, then block - ** until all readers have finished using the wal file. This ensures that + ** entire wal file has been copied into the database file, then block + ** until all readers have finished using the wal file. This ensures that ** the next process to write to the database restarts the wal file. */ if( rc==SQLITE_OK && eMode!=SQLITE_CHECKPOINT_PASSIVE ){ assert( pWal->writeLock ); + SEH_INJECT_FAULT; if( pInfo->nBackfill<pWal->hdr.mxFrame ){ rc = SQLITE_BUSY; }else if( eMode>=SQLITE_CHECKPOINT_RESTART ){ @@ -1882,7 +2357,7 @@ static int walCheckpoint( ** writer clients should see that the entire log file has been ** checkpointed and behave accordingly. This seems unsafe though, ** as it would leave the system in a state where the contents of - ** the wal-index header do not match the contents of the + ** the wal-index header do not match the contents of the ** file-system. To avoid this, update the wal-index header to ** indicate that the log file contains zero valid frames. */ walRestartHdr(pWal, salt1); @@ -1894,6 +2369,7 @@ static int walCheckpoint( } walcheckpoint_out: + SEH_FREE_ON_ERROR(pIter, 0); walIteratorFree(pIter); return rc; } @@ -1916,11 +2392,101 @@ static void walLimitSize(Wal *pWal, i64 nMax){ } } +#ifdef SQLITE_USE_SEH +/* +** This is the "standard" exception handler used in a few places to handle +** an exception thrown by reading from the *-shm mapping after it has become +** invalid in SQLITE_USE_SEH builds. It is used as follows: +** +** SEH_TRY { ... } +** SEH_EXCEPT( rc = walHandleException(pWal); ) +** +** This function does three things: +** +** 1) Determines the locks that should be held, based on the contents of +** the Wal.readLock, Wal.writeLock and Wal.ckptLock variables. All other +** held locks are assumed to be transient locks that would have been +** released had the exception not been thrown and are dropped. +** +** 2) Frees the pointer at Wal.pFree, if any, using sqlite3_free(). +** +** 3) Set pWal->apWiData[pWal->iWiPg] to pWal->pWiValue if not NULL +** +** 4) Returns SQLITE_IOERR. +*/ +static int walHandleException(Wal *pWal){ + if( pWal->exclusiveMode==0 ){ + static const int S = 1; + static const int E = (1<<SQLITE_SHM_NLOCK); + int ii; + u32 mUnlock; + if( pWal->writeLock==2 ) pWal->writeLock = 0; + mUnlock = pWal->lockMask & ~( + (pWal->readLock<0 ? 0 : (S << WAL_READ_LOCK(pWal->readLock))) + | (pWal->writeLock ? (E << WAL_WRITE_LOCK) : 0) + | (pWal->ckptLock ? (E << WAL_CKPT_LOCK) : 0) + ); + for(ii=0; ii<SQLITE_SHM_NLOCK; ii++){ + if( (S<<ii) & mUnlock ) walUnlockShared(pWal, ii); + if( (E<<ii) & mUnlock ) walUnlockExclusive(pWal, ii, 1); + } + } + sqlite3_free(pWal->pFree); + pWal->pFree = 0; + if( pWal->pWiValue ){ + pWal->apWiData[pWal->iWiPg] = pWal->pWiValue; + pWal->pWiValue = 0; + } + return SQLITE_IOERR_IN_PAGE; +} + +/* +** Assert that the Wal.lockMask mask, which indicates the locks held +** by the connection, is consistent with the Wal.readLock, Wal.writeLock +** and Wal.ckptLock variables. To be used as: +** +** assert( walAssertLockmask(pWal) ); +*/ +static int walAssertLockmask(Wal *pWal){ + if( pWal->exclusiveMode==0 ){ + static const int S = 1; + static const int E = (1<<SQLITE_SHM_NLOCK); + u32 mExpect = ( + (pWal->readLock<0 ? 0 : (S << WAL_READ_LOCK(pWal->readLock))) + | (pWal->writeLock ? (E << WAL_WRITE_LOCK) : 0) + | (pWal->ckptLock ? (E << WAL_CKPT_LOCK) : 0) +#ifdef SQLITE_ENABLE_SNAPSHOT + | (pWal->pSnapshot ? (pWal->lockMask & (1 << WAL_CKPT_LOCK)) : 0) +#endif + ); + assert( mExpect==pWal->lockMask ); + } + return 1; +} + +/* +** Return and zero the "system error" field set when an +** EXCEPTION_IN_PAGE_ERROR exception is caught. +*/ +int sqlite3WalSystemErrno(Wal *pWal){ + int iRet = 0; + if( pWal ){ + iRet = pWal->iSysErrno; + pWal->iSysErrno = 0; + } + return iRet; +} + +#else +# define walAssertLockmask(x) 1 +#endif /* ifdef SQLITE_USE_SEH */ + /* ** Close a connection to a log file. */ int sqlite3WalClose( Wal *pWal, /* Wal to close */ + sqlite3 *db, /* For interrupt flag */ int sync_flags, /* Flags to pass to OsSync() (or 0) */ int nBuf, u8 *zBuf /* Buffer of at least nBuf bytes */ @@ -1929,6 +2495,8 @@ int sqlite3WalClose( if( pWal ){ int isDelete = 0; /* True to unlink wal and wal-index files */ + assert( walAssertLockmask(pWal) ); + /* If an EXCLUSIVE lock can be obtained on the database file (using the ** ordinary, rollback-mode locking methods, this guarantees that the ** connection associated with this log file is the only connection to @@ -1937,13 +2505,14 @@ int sqlite3WalClose( ** ** The EXCLUSIVE lock is not released before returning. */ - rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE); - if( rc==SQLITE_OK ){ + if( zBuf!=0 + && SQLITE_OK==(rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE)) + ){ if( pWal->exclusiveMode==WAL_NORMAL_MODE ){ pWal->exclusiveMode = WAL_EXCLUSIVE_MODE; } - rc = sqlite3WalCheckpoint( - pWal, SQLITE_CHECKPOINT_PASSIVE, 0, 0, sync_flags, nBuf, zBuf, 0, 0 + rc = sqlite3WalCheckpoint(pWal, db, + SQLITE_CHECKPOINT_PASSIVE, 0, 0, sync_flags, nBuf, zBuf, 0, 0 ); if( rc==SQLITE_OK ){ int bPersist = -1; @@ -1952,7 +2521,7 @@ int sqlite3WalClose( ); if( bPersist!=1 ){ /* Try to delete the WAL file if the checkpoint completed and - ** fsyned (rc==SQLITE_OK) and if we are not in persistent-wal + ** fsynced (rc==SQLITE_OK) and if we are not in persistent-wal ** mode (!bPersist) */ isDelete = 1; }else if( pWal->mxWalSize>=0 ){ @@ -1998,7 +2567,7 @@ int sqlite3WalClose( ** If the checksum cannot be verified return non-zero. If the header ** is read successfully and the checksum verified, return zero. */ -static int walIndexTryHdr(Wal *pWal, int *pChanged){ +static SQLITE_NO_TSAN int walIndexTryHdr(Wal *pWal, int *pChanged){ u32 aCksum[2]; /* Checksum on the header content */ WalIndexHdr h1, h2; /* Two copies of the header content */ WalIndexHdr volatile *aHdr; /* Header in shared memory */ @@ -2011,19 +2580,25 @@ static int walIndexTryHdr(Wal *pWal, int *pChanged){ ** meaning it is possible that an inconsistent snapshot is read ** from the file. If this happens, return non-zero. ** + ** tag-20200519-1: ** There are two copies of the header at the beginning of the wal-index. ** When reading, read [0] first then [1]. Writes are in the reverse order. ** Memory barriers are used to prevent the compiler or the hardware from - ** reordering the reads and writes. + ** reordering the reads and writes. TSAN and similar tools can sometimes + ** give false-positive warnings about these accesses because the tools do not + ** account for the double-read and the memory barrier. The use of mutexes + ** here would be problematic as the memory being accessed is potentially + ** shared among multiple processes and not all mutex implementations work + ** reliably in that environment. */ aHdr = walIndexHdr(pWal); - memcpy(&h1, (void *)&aHdr[0], sizeof(h1)); + memcpy(&h1, (void *)&aHdr[0], sizeof(h1)); /* Possible TSAN false-positive */ walShmBarrier(pWal); memcpy(&h2, (void *)&aHdr[1], sizeof(h2)); if( memcmp(&h1, &h2, sizeof(h1))!=0 ){ return 1; /* Dirty read */ - } + } if( h1.isInit==0 ){ return 1; /* Malformed header - probably all zeros */ } @@ -2044,6 +2619,12 @@ static int walIndexTryHdr(Wal *pWal, int *pChanged){ return 0; } +/* +** This is the value that walTryBeginRead returns when it needs to +** be retried. +*/ +#define WAL_RETRY (-1) + /* ** Read the wal-index header from the wal-index and into pWal->hdr. ** If the wal-header appears to be corrupt, try to reconstruct the @@ -2053,7 +2634,7 @@ static int walIndexTryHdr(Wal *pWal, int *pChanged){ ** changed by this operation. If pWal->hdr is unchanged, set *pChanged ** to 0. ** -** If the wal-index header is successfully read, return SQLITE_OK. +** If the wal-index header is successfully read, return SQLITE_OK. ** Otherwise an SQLite error code. */ static int walIndexReadHdr(Wal *pWal, int *pChanged){ @@ -2061,19 +2642,39 @@ static int walIndexReadHdr(Wal *pWal, int *pChanged){ int badHdr; /* True if a header read failed */ volatile u32 *page0; /* Chunk of wal-index containing header */ - /* Ensure that page 0 of the wal-index (the page that contains the + /* Ensure that page 0 of the wal-index (the page that contains the ** wal-index header) is mapped. Return early if an error occurs here. */ assert( pChanged ); rc = walIndexPage(pWal, 0, &page0); if( rc!=SQLITE_OK ){ - return rc; - }; - assert( page0 || pWal->writeLock==0 ); - + assert( rc!=SQLITE_READONLY ); /* READONLY changed to OK in walIndexPage */ + if( rc==SQLITE_READONLY_CANTINIT ){ + /* The SQLITE_READONLY_CANTINIT return means that the shared-memory + ** was openable but is not writable, and this thread is unable to + ** confirm that another write-capable connection has the shared-memory + ** open, and hence the content of the shared-memory is unreliable, + ** since the shared-memory might be inconsistent with the WAL file + ** and there is no writer on hand to fix it. */ + assert( page0==0 ); + assert( pWal->writeLock==0 ); + assert( pWal->readOnly & WAL_SHM_RDONLY ); + pWal->bShmUnreliable = 1; + pWal->exclusiveMode = WAL_HEAPMEMORY_MODE; + *pChanged = 1; + }else{ + return rc; /* Any other non-OK return is just an error */ + } + }else{ + /* page0 can be NULL if the SHM is zero bytes in size and pWal->writeLock + ** is zero, which prevents the SHM from growing */ + testcase( page0!=0 ); + } + assert( page0!=0 || pWal->writeLock==0 ); + /* If the first page of the wal-index has been mapped, try to read the ** wal-index header immediately, without holding any lock. This usually - ** works, but may fail if the wal-index header is corrupt or currently + ** works, but may fail if the wal-index header is corrupt or currently ** being modified by another thread or process. */ badHdr = (page0 ? walIndexTryHdr(pWal, pChanged) : 1); @@ -2081,28 +2682,40 @@ static int walIndexReadHdr(Wal *pWal, int *pChanged){ /* If the first attempt failed, it might have been due to a race ** with a writer. So get a WRITE lock and try again. */ - assert( badHdr==0 || pWal->writeLock==0 ); if( badHdr ){ - if( pWal->readOnly & WAL_SHM_RDONLY ){ + if( pWal->bShmUnreliable==0 && (pWal->readOnly & WAL_SHM_RDONLY) ){ if( SQLITE_OK==(rc = walLockShared(pWal, WAL_WRITE_LOCK)) ){ walUnlockShared(pWal, WAL_WRITE_LOCK); rc = SQLITE_READONLY_RECOVERY; } - }else if( SQLITE_OK==(rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1)) ){ - pWal->writeLock = 1; - if( SQLITE_OK==(rc = walIndexPage(pWal, 0, &page0)) ){ - badHdr = walIndexTryHdr(pWal, pChanged); - if( badHdr ){ - /* If the wal-index header is still malformed even while holding - ** a WRITE lock, it can only mean that the header is corrupted and - ** needs to be reconstructed. So run recovery to do exactly that. - */ - rc = walIndexRecover(pWal); - *pChanged = 1; + }else{ + int bWriteLock = pWal->writeLock; + if( bWriteLock + || SQLITE_OK==(rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1)) + ){ + /* If the write-lock was just obtained, set writeLock to 2 instead of + ** the usual 1. This causes walIndexPage() to behave as if the + ** write-lock were held (so that it allocates new pages as required), + ** and walHandleException() to unlock the write-lock if a SEH exception + ** is thrown. */ + if( !bWriteLock ) pWal->writeLock = 2; + if( SQLITE_OK==(rc = walIndexPage(pWal, 0, &page0)) ){ + badHdr = walIndexTryHdr(pWal, pChanged); + if( badHdr ){ + /* If the wal-index header is still malformed even while holding + ** a WRITE lock, it can only mean that the header is corrupted and + ** needs to be reconstructed. So run recovery to do exactly that. + ** Disable blocking locks first. */ + walDisableBlocking(pWal); + rc = walIndexRecover(pWal); + *pChanged = 1; + } + } + if( bWriteLock==0 ){ + pWal->writeLock = 0; + walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1); } } - pWal->writeLock = 0; - walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1); } } @@ -2113,15 +2726,226 @@ static int walIndexReadHdr(Wal *pWal, int *pChanged){ if( badHdr==0 && pWal->hdr.iVersion!=WALINDEX_MAX_VERSION ){ rc = SQLITE_CANTOPEN_BKPT; } + if( pWal->bShmUnreliable ){ + if( rc!=SQLITE_OK ){ + walIndexClose(pWal, 0); + pWal->bShmUnreliable = 0; + assert( pWal->nWiData>0 && pWal->apWiData[0]==0 ); + /* walIndexRecover() might have returned SHORT_READ if a concurrent + ** writer truncated the WAL out from under it. If that happens, it + ** indicates that a writer has fixed the SHM file for us, so retry */ + if( rc==SQLITE_IOERR_SHORT_READ ) rc = WAL_RETRY; + } + pWal->exclusiveMode = WAL_NORMAL_MODE; + } return rc; } /* -** This is the value that walTryBeginRead returns when it needs to -** be retried. +** Open a transaction in a connection where the shared-memory is read-only +** and where we cannot verify that there is a separate write-capable connection +** on hand to keep the shared-memory up-to-date with the WAL file. +** +** This can happen, for example, when the shared-memory is implemented by +** memory-mapping a *-shm file, where a prior writer has shut down and +** left the *-shm file on disk, and now the present connection is trying +** to use that database but lacks write permission on the *-shm file. +** Other scenarios are also possible, depending on the VFS implementation. +** +** Precondition: +** +** The *-wal file has been read and an appropriate wal-index has been +** constructed in pWal->apWiData[] using heap memory instead of shared +** memory. +** +** If this function returns SQLITE_OK, then the read transaction has +** been successfully opened. In this case output variable (*pChanged) +** is set to true before returning if the caller should discard the +** contents of the page cache before proceeding. Or, if it returns +** WAL_RETRY, then the heap memory wal-index has been discarded and +** the caller should retry opening the read transaction from the +** beginning (including attempting to map the *-shm file). +** +** If an error occurs, an SQLite error code is returned. */ -#define WAL_RETRY (-1) +static int walBeginShmUnreliable(Wal *pWal, int *pChanged){ + i64 szWal; /* Size of wal file on disk in bytes */ + i64 iOffset; /* Current offset when reading wal file */ + u8 aBuf[WAL_HDRSIZE]; /* Buffer to load WAL header into */ + u8 *aFrame = 0; /* Malloc'd buffer to load entire frame */ + int szFrame; /* Number of bytes in buffer aFrame[] */ + u8 *aData; /* Pointer to data part of aFrame buffer */ + volatile void *pDummy; /* Dummy argument for xShmMap */ + int rc; /* Return code */ + u32 aSaveCksum[2]; /* Saved copy of pWal->hdr.aFrameCksum */ + + assert( pWal->bShmUnreliable ); + assert( pWal->readOnly & WAL_SHM_RDONLY ); + assert( pWal->nWiData>0 && pWal->apWiData[0] ); + + /* Take WAL_READ_LOCK(0). This has the effect of preventing any + ** writers from running a checkpoint, but does not stop them + ** from running recovery. */ + rc = walLockShared(pWal, WAL_READ_LOCK(0)); + if( rc!=SQLITE_OK ){ + if( rc==SQLITE_BUSY ) rc = WAL_RETRY; + goto begin_unreliable_shm_out; + } + pWal->readLock = 0; + + /* Check to see if a separate writer has attached to the shared-memory area, + ** thus making the shared-memory "reliable" again. Do this by invoking + ** the xShmMap() routine of the VFS and looking to see if the return + ** is SQLITE_READONLY instead of SQLITE_READONLY_CANTINIT. + ** + ** If the shared-memory is now "reliable" return WAL_RETRY, which will + ** cause the heap-memory WAL-index to be discarded and the actual + ** shared memory to be used in its place. + ** + ** This step is important because, even though this connection is holding + ** the WAL_READ_LOCK(0) which prevents a checkpoint, a writer might + ** have already checkpointed the WAL file and, while the current + ** is active, wrap the WAL and start overwriting frames that this + ** process wants to use. + ** + ** Once sqlite3OsShmMap() has been called for an sqlite3_file and has + ** returned any SQLITE_READONLY value, it must return only SQLITE_READONLY + ** or SQLITE_READONLY_CANTINIT or some error for all subsequent invocations, + ** even if some external agent does a "chmod" to make the shared-memory + ** writable by us, until sqlite3OsShmUnmap() has been called. + ** This is a requirement on the VFS implementation. + */ + rc = sqlite3OsShmMap(pWal->pDbFd, 0, WALINDEX_PGSZ, 0, &pDummy); + assert( rc!=SQLITE_OK ); /* SQLITE_OK not possible for read-only connection */ + if( rc!=SQLITE_READONLY_CANTINIT ){ + rc = (rc==SQLITE_READONLY ? WAL_RETRY : rc); + goto begin_unreliable_shm_out; + } + + /* We reach this point only if the real shared-memory is still unreliable. + ** Assume the in-memory WAL-index substitute is correct and load it + ** into pWal->hdr. + */ + memcpy(&pWal->hdr, (void*)walIndexHdr(pWal), sizeof(WalIndexHdr)); + + /* Make sure some writer hasn't come in and changed the WAL file out + ** from under us, then disconnected, while we were not looking. + */ + rc = sqlite3OsFileSize(pWal->pWalFd, &szWal); + if( rc!=SQLITE_OK ){ + goto begin_unreliable_shm_out; + } + if( szWal<WAL_HDRSIZE ){ + /* If the wal file is too small to contain a wal-header and the + ** wal-index header has mxFrame==0, then it must be safe to proceed + ** reading the database file only. However, the page cache cannot + ** be trusted, as a read/write connection may have connected, written + ** the db, run a checkpoint, truncated the wal file and disconnected + ** since this client's last read transaction. */ + *pChanged = 1; + rc = (pWal->hdr.mxFrame==0 ? SQLITE_OK : WAL_RETRY); + goto begin_unreliable_shm_out; + } + + /* Check the salt keys at the start of the wal file still match. */ + rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0); + if( rc!=SQLITE_OK ){ + goto begin_unreliable_shm_out; + } + if( memcmp(&pWal->hdr.aSalt, &aBuf[16], 8) ){ + /* Some writer has wrapped the WAL file while we were not looking. + ** Return WAL_RETRY which will cause the in-memory WAL-index to be + ** rebuilt. */ + rc = WAL_RETRY; + goto begin_unreliable_shm_out; + } + + /* Allocate a buffer to read frames into */ + assert( (pWal->szPage & (pWal->szPage-1))==0 ); + assert( pWal->szPage>=512 && pWal->szPage<=65536 ); + szFrame = pWal->szPage + WAL_FRAME_HDRSIZE; + aFrame = (u8 *)sqlite3_malloc64(szFrame); + if( aFrame==0 ){ + rc = SQLITE_NOMEM_BKPT; + goto begin_unreliable_shm_out; + } + aData = &aFrame[WAL_FRAME_HDRSIZE]; + + /* Check to see if a complete transaction has been appended to the + ** wal file since the heap-memory wal-index was created. If so, the + ** heap-memory wal-index is discarded and WAL_RETRY returned to + ** the caller. */ + aSaveCksum[0] = pWal->hdr.aFrameCksum[0]; + aSaveCksum[1] = pWal->hdr.aFrameCksum[1]; + for(iOffset=walFrameOffset(pWal->hdr.mxFrame+1, pWal->szPage); + iOffset+szFrame<=szWal; + iOffset+=szFrame + ){ + u32 pgno; /* Database page number for frame */ + u32 nTruncate; /* dbsize field from frame header */ + + /* Read and decode the next log frame. */ + rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset); + if( rc!=SQLITE_OK ) break; + if( !walDecodeFrame(pWal, &pgno, &nTruncate, aData, aFrame) ) break; + + /* If nTruncate is non-zero, then a complete transaction has been + ** appended to this wal file. Set rc to WAL_RETRY and break out of + ** the loop. */ + if( nTruncate ){ + rc = WAL_RETRY; + break; + } + } + pWal->hdr.aFrameCksum[0] = aSaveCksum[0]; + pWal->hdr.aFrameCksum[1] = aSaveCksum[1]; + + begin_unreliable_shm_out: + sqlite3_free(aFrame); + if( rc!=SQLITE_OK ){ + int i; + for(i=0; i<pWal->nWiData; i++){ + sqlite3_free((void*)pWal->apWiData[i]); + pWal->apWiData[i] = 0; + } + pWal->bShmUnreliable = 0; + sqlite3WalEndReadTransaction(pWal); + *pChanged = 1; + } + return rc; +} + +/* +** The final argument passed to walTryBeginRead() is of type (int*). The +** caller should invoke walTryBeginRead as follows: +** +** int cnt = 0; +** do { +** rc = walTryBeginRead(..., &cnt); +** }while( rc==WAL_RETRY ); +** +** The final value of "cnt" is of no use to the caller. It is used by +** the implementation of walTryBeginRead() as follows: +** +** + Each time walTryBeginRead() is called, it is incremented. Once +** it reaches WAL_RETRY_PROTOCOL_LIMIT - indicating that walTryBeginRead() +** has many times been invoked and failed with WAL_RETRY - walTryBeginRead() +** returns SQLITE_PROTOCOL. +** +** + If SQLITE_ENABLE_SETLK_TIMEOUT is defined and walTryBeginRead() failed +** because a blocking lock timed out (SQLITE_BUSY_TIMEOUT from the OS +** layer), the WAL_RETRY_BLOCKED_MASK bit is set in "cnt". In this case +** the next invocation of walTryBeginRead() may omit an expected call to +** sqlite3OsSleep(). There has already been a delay when the previous call +** waited on a lock. +*/ +#define WAL_RETRY_PROTOCOL_LIMIT 100 +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT +# define WAL_RETRY_BLOCKED_MASK 0x10000000 +#else +# define WAL_RETRY_BLOCKED_MASK 0 +#endif /* ** Attempt to start a read transaction. This might fail due to a race or @@ -2134,10 +2958,10 @@ static int walIndexReadHdr(Wal *pWal, int *pChanged){ ** ** The useWal parameter is true to force the use of the WAL and disable ** the case where the WAL is bypassed because it has been completely -** checkpointed. If useWal==0 then this routine calls walIndexReadHdr() -** to make a copy of the wal-index header into pWal->hdr. If the -** wal-index header has changed, *pChanged is set to 1 (as an indication -** to the caller that the local paget cache is obsolete and needs to be +** checkpointed. If useWal==0 then this routine calls walIndexReadHdr() +** to make a copy of the wal-index header into pWal->hdr. If the +** wal-index header has changed, *pChanged is set to 1 (as an indication +** to the caller that the local page cache is obsolete and needs to be ** flushed.) When useWal==1, the wal-index header is assumed to already ** be loaded and the pChanged parameter is unused. ** @@ -2152,7 +2976,7 @@ static int walIndexReadHdr(Wal *pWal, int *pChanged){ ** bad luck when there is lots of contention for the wal-index, but that ** possibility is so small that it can be safely neglected, we believe. ** -** On success, this routine obtains a read lock on +** On success, this routine obtains a read lock on ** WAL_READ_LOCK(pWal->readLock). The pWal->readLock integer is ** in the range 0 <= pWal->readLock < WAL_NREADER. If pWal->readLock==(-1) ** that means the Wal does not hold any read lock. The reader must not @@ -2173,45 +2997,76 @@ static int walIndexReadHdr(Wal *pWal, int *pChanged){ ** so it takes care to hold an exclusive lock on the corresponding ** WAL_READ_LOCK() while changing values. */ -static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int cnt){ +static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int *pCnt){ volatile WalCkptInfo *pInfo; /* Checkpoint information in wal-index */ - u32 mxReadMark; /* Largest aReadMark[] value */ - int mxI; /* Index of largest aReadMark[] value */ - int i; /* Loop counter */ int rc = SQLITE_OK; /* Return code */ - u32 mxFrame; /* Wal frame to lock to */ +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT + int nBlockTmout = 0; +#endif assert( pWal->readLock<0 ); /* Not currently locked */ + /* useWal may only be set for read/write connections */ + assert( (pWal->readOnly & WAL_SHM_RDONLY)==0 || useWal==0 ); + /* Take steps to avoid spinning forever if there is a protocol error. ** ** Circumstances that cause a RETRY should only last for the briefest ** instances of time. No I/O or other system calls are done while the - ** locks are held, so the locks should not be held for very long. But + ** locks are held, so the locks should not be held for very long. But ** if we are unlucky, another process that is holding a lock might get - ** paged out or take a page-fault that is time-consuming to resolve, + ** paged out or take a page-fault that is time-consuming to resolve, ** during the few nanoseconds that it is holding the lock. In that case, ** it might take longer than normal for the lock to free. ** ** After 5 RETRYs, we begin calling sqlite3OsSleep(). The first few ** calls to sqlite3OsSleep() have a delay of 1 microsecond. Really this ** is more of a scheduler yield than an actual delay. But on the 10th - ** an subsequent retries, the delays start becoming longer and longer, + ** an subsequent retries, the delays start becoming longer and longer, ** so that on the 100th (and last) RETRY we delay for 323 milliseconds. ** The total delay time before giving up is less than 10 seconds. */ - if( cnt>5 ){ + (*pCnt)++; + if( *pCnt>5 ){ int nDelay = 1; /* Pause time in microseconds */ - if( cnt>100 ){ + int cnt = (*pCnt & ~WAL_RETRY_BLOCKED_MASK); + if( cnt>WAL_RETRY_PROTOCOL_LIMIT ){ VVA_ONLY( pWal->lockError = 1; ) return SQLITE_PROTOCOL; } - if( cnt>=10 ) nDelay = (cnt-9)*(cnt-9)*39; + if( *pCnt>=10 ) nDelay = (cnt-9)*(cnt-9)*39; +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT + /* In SQLITE_ENABLE_SETLK_TIMEOUT builds, configure the file-descriptor + ** to block for locks for approximately nDelay us. This affects three + ** locks: (a) the shared lock taken on the DMS slot in os_unix.c (if + ** using os_unix.c), (b) the WRITER lock taken in walIndexReadHdr() if the + ** first attempted read fails, and (c) the shared lock taken on the + ** read-mark. + ** + ** If the previous call failed due to an SQLITE_BUSY_TIMEOUT error, + ** then sleep for the minimum of 1us. The previous call already provided + ** an extra delay while it was blocking on the lock. + */ + nBlockTmout = (nDelay+998) / 1000; + if( !useWal && walEnableBlockingMs(pWal, nBlockTmout) ){ + if( *pCnt & WAL_RETRY_BLOCKED_MASK ) nDelay = 1; + } +#endif sqlite3OsSleep(pWal->pVfs, nDelay); + *pCnt &= ~WAL_RETRY_BLOCKED_MASK; } if( !useWal ){ - rc = walIndexReadHdr(pWal, pChanged); + assert( rc==SQLITE_OK ); + if( pWal->bShmUnreliable==0 ){ + rc = walIndexReadHdr(pWal, pChanged); + } +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT + if( rc==SQLITE_BUSY_TIMEOUT ){ + rc = SQLITE_BUSY; + *pCnt |= WAL_RETRY_BLOCKED_MASK; + } +#endif if( rc==SQLITE_BUSY ){ /* If there is not a recovery running in another thread or process ** then convert BUSY errors to WAL_RETRY. If recovery is known to @@ -2221,12 +3076,13 @@ static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int cnt){ ** WAL_RETRY this routine will be called again and will probably be ** right on the second iteration. */ + (void)walEnableBlocking(pWal); if( pWal->apWiData[0]==0 ){ /* This branch is taken when the xShmMap() method returns SQLITE_BUSY. ** We assume this is a transient condition, so return WAL_RETRY. The - ** xShmMap() implementation used by the default unix and win32 VFS - ** modules may return SQLITE_BUSY due to a race condition in the - ** code that determines whether or not the shared-memory region + ** xShmMap() implementation used by the default unix and win32 VFS + ** modules may return SQLITE_BUSY due to a race condition in the + ** code that determines whether or not the shared-memory region ** must be zeroed before the requested page is returned. */ rc = WAL_RETRY; @@ -2237,169 +3093,303 @@ static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int cnt){ rc = SQLITE_BUSY_RECOVERY; } } + walDisableBlocking(pWal); if( rc!=SQLITE_OK ){ return rc; } + else if( pWal->bShmUnreliable ){ + return walBeginShmUnreliable(pWal, pChanged); + } } + assert( pWal->nWiData>0 ); + assert( pWal->apWiData[0]!=0 ); pInfo = walCkptInfo(pWal); - if( !useWal && pInfo->nBackfill==pWal->hdr.mxFrame + SEH_INJECT_FAULT; + { + u32 mxReadMark; /* Largest aReadMark[] value */ + int mxI; /* Index of largest aReadMark[] value */ + int i; /* Loop counter */ + u32 mxFrame; /* Wal frame to lock to */ + if( !useWal && AtomicLoad(&pInfo->nBackfill)==pWal->hdr.mxFrame #ifdef SQLITE_ENABLE_SNAPSHOT - && (pWal->pSnapshot==0 || pWal->hdr.mxFrame==0 - || 0==memcmp(&pWal->hdr, pWal->pSnapshot, sizeof(WalIndexHdr))) + && ((pWal->bGetSnapshot==0 && pWal->pSnapshot==0) || pWal->hdr.mxFrame==0) #endif - ){ - /* The WAL has been completely backfilled (or it is empty). - ** and can be safely ignored. - */ - rc = walLockShared(pWal, WAL_READ_LOCK(0)); - walShmBarrier(pWal); - if( rc==SQLITE_OK ){ - if( memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) ){ - /* It is not safe to allow the reader to continue here if frames - ** may have been appended to the log before READ_LOCK(0) was obtained. - ** When holding READ_LOCK(0), the reader ignores the entire log file, - ** which implies that the database file contains a trustworthy - ** snapshot. Since holding READ_LOCK(0) prevents a checkpoint from - ** happening, this is usually correct. - ** - ** However, if frames have been appended to the log (or if the log - ** is wrapped and written for that matter) before the READ_LOCK(0) - ** is obtained, that is not necessarily true. A checkpointer may - ** have started to backfill the appended frames but crashed before - ** it finished. Leaving a corrupt image in the database file. - */ - walUnlockShared(pWal, WAL_READ_LOCK(0)); - return WAL_RETRY; + ){ + /* The WAL has been completely backfilled (or it is empty). + ** and can be safely ignored. + */ + rc = walLockShared(pWal, WAL_READ_LOCK(0)); + walShmBarrier(pWal); + if( rc==SQLITE_OK ){ + if( memcmp((void *)walIndexHdr(pWal), &pWal->hdr,sizeof(WalIndexHdr)) ){ + /* It is not safe to allow the reader to continue here if frames + ** may have been appended to the log before READ_LOCK(0) was obtained. + ** When holding READ_LOCK(0), the reader ignores the entire log file, + ** which implies that the database file contains a trustworthy + ** snapshot. Since holding READ_LOCK(0) prevents a checkpoint from + ** happening, this is usually correct. + ** + ** However, if frames have been appended to the log (or if the log + ** is wrapped and written for that matter) before the READ_LOCK(0) + ** is obtained, that is not necessarily true. A checkpointer may + ** have started to backfill the appended frames but crashed before + ** it finished. Leaving a corrupt image in the database file. + */ + walUnlockShared(pWal, WAL_READ_LOCK(0)); + return WAL_RETRY; + } + pWal->readLock = 0; + return SQLITE_OK; + }else if( rc!=SQLITE_BUSY ){ + return rc; } - pWal->readLock = 0; - return SQLITE_OK; - }else if( rc!=SQLITE_BUSY ){ - return rc; } - } - - /* If we get this far, it means that the reader will want to use - ** the WAL to get at content from recent commits. The job now is - ** to select one of the aReadMark[] entries that is closest to - ** but not exceeding pWal->hdr.mxFrame and lock that entry. - */ - mxReadMark = 0; - mxI = 0; - mxFrame = pWal->hdr.mxFrame; + + /* If we get this far, it means that the reader will want to use + ** the WAL to get at content from recent commits. The job now is + ** to select one of the aReadMark[] entries that is closest to + ** but not exceeding pWal->hdr.mxFrame and lock that entry. + */ + mxReadMark = 0; + mxI = 0; + mxFrame = pWal->hdr.mxFrame; #ifdef SQLITE_ENABLE_SNAPSHOT - if( pWal->pSnapshot && pWal->pSnapshot->mxFrame<mxFrame ){ - mxFrame = pWal->pSnapshot->mxFrame; - } -#endif - for(i=1; i<WAL_NREADER; i++){ - u32 thisMark = pInfo->aReadMark[i]; - if( mxReadMark<=thisMark && thisMark<=mxFrame ){ - assert( thisMark!=READMARK_NOT_USED ); - mxReadMark = thisMark; - mxI = i; + if( pWal->pSnapshot && pWal->pSnapshot->mxFrame<mxFrame ){ + mxFrame = pWal->pSnapshot->mxFrame; } - } - if( (pWal->readOnly & WAL_SHM_RDONLY)==0 - && (mxReadMark<mxFrame || mxI==0) - ){ +#endif for(i=1; i<WAL_NREADER; i++){ - rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1); - if( rc==SQLITE_OK ){ - mxReadMark = pInfo->aReadMark[i] = mxFrame; + u32 thisMark = AtomicLoad(pInfo->aReadMark+i); SEH_INJECT_FAULT; + if( mxReadMark<=thisMark && thisMark<=mxFrame ){ + assert( thisMark!=READMARK_NOT_USED ); + mxReadMark = thisMark; mxI = i; - walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1); - break; - }else if( rc!=SQLITE_BUSY ){ - return rc; } } + if( (pWal->readOnly & WAL_SHM_RDONLY)==0 + && (mxReadMark<mxFrame || mxI==0) + ){ + for(i=1; i<WAL_NREADER; i++){ + rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1); + if( rc==SQLITE_OK ){ + AtomicStore(pInfo->aReadMark+i,mxFrame); + mxReadMark = mxFrame; + mxI = i; + walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1); + break; + }else if( rc!=SQLITE_BUSY ){ + return rc; + } + } + } + if( mxI==0 ){ + assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 ); + return rc==SQLITE_BUSY ? WAL_RETRY : SQLITE_READONLY_CANTINIT; + } + + (void)walEnableBlockingMs(pWal, nBlockTmout); + rc = walLockShared(pWal, WAL_READ_LOCK(mxI)); + walDisableBlocking(pWal); + if( rc ){ +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT + if( rc==SQLITE_BUSY_TIMEOUT ){ + *pCnt |= WAL_RETRY_BLOCKED_MASK; + } +#else + assert( rc!=SQLITE_BUSY_TIMEOUT ); +#endif + assert((rc&0xFF)!=SQLITE_BUSY||rc==SQLITE_BUSY||rc==SQLITE_BUSY_TIMEOUT); + return (rc&0xFF)==SQLITE_BUSY ? WAL_RETRY : rc; + } + /* Now that the read-lock has been obtained, check that neither the + ** value in the aReadMark[] array or the contents of the wal-index + ** header have changed. + ** + ** It is necessary to check that the wal-index header did not change + ** between the time it was read and when the shared-lock was obtained + ** on WAL_READ_LOCK(mxI) was obtained to account for the possibility + ** that the log file may have been wrapped by a writer, or that frames + ** that occur later in the log than pWal->hdr.mxFrame may have been + ** copied into the database by a checkpointer. If either of these things + ** happened, then reading the database with the current value of + ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry + ** instead. + ** + ** Before checking that the live wal-index header has not changed + ** since it was read, set Wal.minFrame to the first frame in the wal + ** file that has not yet been checkpointed. This client will not need + ** to read any frames earlier than minFrame from the wal file - they + ** can be safely read directly from the database file. + ** + ** Because a ShmBarrier() call is made between taking the copy of + ** nBackfill and checking that the wal-header in shared-memory still + ** matches the one cached in pWal->hdr, it is guaranteed that the + ** checkpointer that set nBackfill was not working with a wal-index + ** header newer than that cached in pWal->hdr. If it were, that could + ** cause a problem. The checkpointer could omit to checkpoint + ** a version of page X that lies before pWal->minFrame (call that version + ** A) on the basis that there is a newer version (version B) of the same + ** page later in the wal file. But if version B happens to like past + ** frame pWal->hdr.mxFrame - then the client would incorrectly assume + ** that it can read version A from the database file. However, since + ** we can guarantee that the checkpointer that set nBackfill could not + ** see any pages past pWal->hdr.mxFrame, this problem does not come up. + */ + pWal->minFrame = AtomicLoad(&pInfo->nBackfill)+1; SEH_INJECT_FAULT; + walShmBarrier(pWal); + if( AtomicLoad(pInfo->aReadMark+mxI)!=mxReadMark + || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) + ){ + walUnlockShared(pWal, WAL_READ_LOCK(mxI)); + return WAL_RETRY; + }else{ + assert( mxReadMark<=pWal->hdr.mxFrame ); + pWal->readLock = (i16)mxI; + } } - if( mxI==0 ){ - assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 ); - return rc==SQLITE_BUSY ? WAL_RETRY : SQLITE_READONLY_CANTLOCK; - } + return rc; +} - rc = walLockShared(pWal, WAL_READ_LOCK(mxI)); - if( rc ){ - return rc==SQLITE_BUSY ? WAL_RETRY : rc; - } - /* Now that the read-lock has been obtained, check that neither the - ** value in the aReadMark[] array or the contents of the wal-index - ** header have changed. - ** - ** It is necessary to check that the wal-index header did not change - ** between the time it was read and when the shared-lock was obtained - ** on WAL_READ_LOCK(mxI) was obtained to account for the possibility - ** that the log file may have been wrapped by a writer, or that frames - ** that occur later in the log than pWal->hdr.mxFrame may have been - ** copied into the database by a checkpointer. If either of these things - ** happened, then reading the database with the current value of - ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry - ** instead. - ** - ** Before checking that the live wal-index header has not changed - ** since it was read, set Wal.minFrame to the first frame in the wal - ** file that has not yet been checkpointed. This client will not need - ** to read any frames earlier than minFrame from the wal file - they - ** can be safely read directly from the database file. - ** - ** Because a ShmBarrier() call is made between taking the copy of - ** nBackfill and checking that the wal-header in shared-memory still - ** matches the one cached in pWal->hdr, it is guaranteed that the - ** checkpointer that set nBackfill was not working with a wal-index - ** header newer than that cached in pWal->hdr. If it were, that could - ** cause a problem. The checkpointer could omit to checkpoint - ** a version of page X that lies before pWal->minFrame (call that version - ** A) on the basis that there is a newer version (version B) of the same - ** page later in the wal file. But if version B happens to like past - ** frame pWal->hdr.mxFrame - then the client would incorrectly assume - ** that it can read version A from the database file. However, since - ** we can guarantee that the checkpointer that set nBackfill could not - ** see any pages past pWal->hdr.mxFrame, this problem does not come up. - */ - pWal->minFrame = pInfo->nBackfill+1; - walShmBarrier(pWal); - if( pInfo->aReadMark[mxI]!=mxReadMark - || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) - ){ - walUnlockShared(pWal, WAL_READ_LOCK(mxI)); - return WAL_RETRY; - }else{ - assert( mxReadMark<=pWal->hdr.mxFrame ); - pWal->readLock = (i16)mxI; +#ifdef SQLITE_ENABLE_SNAPSHOT +/* +** This function does the work of sqlite3WalSnapshotRecover(). +*/ +static int walSnapshotRecover( + Wal *pWal, /* WAL handle */ + void *pBuf1, /* Temp buffer pWal->szPage bytes in size */ + void *pBuf2 /* Temp buffer pWal->szPage bytes in size */ +){ + int szPage = (int)pWal->szPage; + int rc; + i64 szDb; /* Size of db file in bytes */ + + rc = sqlite3OsFileSize(pWal->pDbFd, &szDb); + if( rc==SQLITE_OK ){ + volatile WalCkptInfo *pInfo = walCkptInfo(pWal); + u32 i = pInfo->nBackfillAttempted; + for(i=pInfo->nBackfillAttempted; i>AtomicLoad(&pInfo->nBackfill); i--){ + WalHashLoc sLoc; /* Hash table location */ + u32 pgno; /* Page number in db file */ + i64 iDbOff; /* Offset of db file entry */ + i64 iWalOff; /* Offset of wal file entry */ + + rc = walHashGet(pWal, walFramePage(i), &sLoc); + if( rc!=SQLITE_OK ) break; + assert( i - sLoc.iZero - 1 >=0 ); + pgno = sLoc.aPgno[i-sLoc.iZero-1]; + iDbOff = (i64)(pgno-1) * szPage; + + if( iDbOff+szPage<=szDb ){ + iWalOff = walFrameOffset(i, szPage) + WAL_FRAME_HDRSIZE; + rc = sqlite3OsRead(pWal->pWalFd, pBuf1, szPage, iWalOff); + + if( rc==SQLITE_OK ){ + rc = sqlite3OsRead(pWal->pDbFd, pBuf2, szPage, iDbOff); + } + + if( rc!=SQLITE_OK || 0==memcmp(pBuf1, pBuf2, szPage) ){ + break; + } + } + + pInfo->nBackfillAttempted = i-1; + } } + return rc; } /* -** Begin a read transaction on the database. +** Attempt to reduce the value of the WalCkptInfo.nBackfillAttempted +** variable so that older snapshots can be accessed. To do this, loop +** through all wal frames from nBackfillAttempted to (nBackfill+1), +** comparing their content to the corresponding page with the database +** file, if any. Set nBackfillAttempted to the frame number of the +** first frame for which the wal file content matches the db file. ** -** This routine used to be called sqlite3OpenSnapshot() and with good reason: -** it takes a snapshot of the state of the WAL and wal-index for the current -** instant in time. The current thread will continue to use this snapshot. -** Other threads might append new content to the WAL and wal-index but -** that extra content is ignored by the current thread. +** This is only really safe if the file-system is such that any page +** writes made by earlier checkpointers were atomic operations, which +** is not always true. It is also possible that nBackfillAttempted +** may be left set to a value larger than expected, if a wal frame +** contains content that duplicate of an earlier version of the same +** page. ** -** If the database contents have changes since the previous read -** transaction, then *pChanged is set to 1 before returning. The -** Pager layer will use this to know that is cache is stale and -** needs to be flushed. +** SQLITE_OK is returned if successful, or an SQLite error code if an +** error occurs. It is not an error if nBackfillAttempted cannot be +** decreased at all. */ -int sqlite3WalBeginReadTransaction(Wal *pWal, int *pChanged){ +int sqlite3WalSnapshotRecover(Wal *pWal){ + int rc; + + assert( pWal->readLock>=0 ); + rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1); + if( rc==SQLITE_OK ){ + void *pBuf1 = sqlite3_malloc(pWal->szPage); + void *pBuf2 = sqlite3_malloc(pWal->szPage); + if( pBuf1==0 || pBuf2==0 ){ + rc = SQLITE_NOMEM; + }else{ + pWal->ckptLock = 1; + SEH_TRY { + rc = walSnapshotRecover(pWal, pBuf1, pBuf2); + } + SEH_EXCEPT( rc = SQLITE_IOERR_IN_PAGE; ) + pWal->ckptLock = 0; + } + + sqlite3_free(pBuf1); + sqlite3_free(pBuf2); + walUnlockExclusive(pWal, WAL_CKPT_LOCK, 1); + } + + return rc; +} +#endif /* SQLITE_ENABLE_SNAPSHOT */ + +/* +** This function does the work of sqlite3WalBeginReadTransaction() (see +** below). That function simply calls this one inside an SEH_TRY{...} block. +*/ +static int walBeginReadTransaction(Wal *pWal, int *pChanged){ int rc; /* Return code */ int cnt = 0; /* Number of TryBeginRead attempts */ - #ifdef SQLITE_ENABLE_SNAPSHOT + int ckptLock = 0; int bChanged = 0; WalIndexHdr *pSnapshot = pWal->pSnapshot; - if( pSnapshot && memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){ - bChanged = 1; +#endif + + assert( pWal->ckptLock==0 ); + assert( pWal->nSehTry>0 ); + +#ifdef SQLITE_ENABLE_SNAPSHOT + if( pSnapshot ){ + if( memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){ + bChanged = 1; + } + + /* It is possible that there is a checkpointer thread running + ** concurrent with this code. If this is the case, it may be that the + ** checkpointer has already determined that it will checkpoint + ** snapshot X, where X is later in the wal file than pSnapshot, but + ** has not yet set the pInfo->nBackfillAttempted variable to indicate + ** its intent. To avoid the race condition this leads to, ensure that + ** there is no checkpointer process by taking a shared CKPT lock + ** before checking pInfo->nBackfillAttempted. */ + (void)walEnableBlocking(pWal); + rc = walLockShared(pWal, WAL_CKPT_LOCK); + walDisableBlocking(pWal); + + if( rc!=SQLITE_OK ){ + return rc; + } + ckptLock = 1; } #endif do{ - rc = walTryBeginRead(pWal, pChanged, 0, ++cnt); + rc = walTryBeginRead(pWal, pChanged, 0, &cnt); }while( rc==WAL_RETRY ); testcase( (rc&0xff)==SQLITE_BUSY ); testcase( (rc&0xff)==SQLITE_IOERR ); @@ -2427,54 +3417,78 @@ int sqlite3WalBeginReadTransaction(Wal *pWal, int *pChanged){ assert( pWal->readLock>0 || pWal->hdr.mxFrame==0 ); assert( pInfo->aReadMark[pWal->readLock]<=pSnapshot->mxFrame ); - /* It is possible that there is a checkpointer thread running - ** concurrent with this code. If this is the case, it may be that the - ** checkpointer has already determined that it will checkpoint - ** snapshot X, where X is later in the wal file than pSnapshot, but - ** has not yet set the pInfo->nBackfillAttempted variable to indicate - ** its intent. To avoid the race condition this leads to, ensure that - ** there is no checkpointer process by taking a shared CKPT lock - ** before checking pInfo->nBackfillAttempted. */ - rc = walLockShared(pWal, WAL_CKPT_LOCK); - - if( rc==SQLITE_OK ){ - /* Check that the wal file has not been wrapped. Assuming that it has - ** not, also check that no checkpointer has attempted to checkpoint any - ** frames beyond pSnapshot->mxFrame. If either of these conditions are - ** true, return SQLITE_BUSY_SNAPSHOT. Otherwise, overwrite pWal->hdr - ** with *pSnapshot and set *pChanged as appropriate for opening the - ** snapshot. */ - if( !memcmp(pSnapshot->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt)) - && pSnapshot->mxFrame>=pInfo->nBackfillAttempted - ){ - assert( pWal->readLock>0 ); - memcpy(&pWal->hdr, pSnapshot, sizeof(WalIndexHdr)); - *pChanged = bChanged; - }else{ - rc = SQLITE_BUSY_SNAPSHOT; - } - - /* Release the shared CKPT lock obtained above. */ - walUnlockShared(pWal, WAL_CKPT_LOCK); + /* Check that the wal file has not been wrapped. Assuming that it has + ** not, also check that no checkpointer has attempted to checkpoint any + ** frames beyond pSnapshot->mxFrame. If either of these conditions are + ** true, return SQLITE_ERROR_SNAPSHOT. Otherwise, overwrite pWal->hdr + ** with *pSnapshot and set *pChanged as appropriate for opening the + ** snapshot. */ + if( !memcmp(pSnapshot->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt)) + && pSnapshot->mxFrame>=pInfo->nBackfillAttempted + ){ + assert( pWal->readLock>0 ); + memcpy(&pWal->hdr, pSnapshot, sizeof(WalIndexHdr)); + *pChanged = bChanged; + }else{ + rc = SQLITE_ERROR_SNAPSHOT; } + /* A client using a non-current snapshot may not ignore any frames + ** from the start of the wal file. This is because, for a system + ** where (minFrame < iSnapshot < maxFrame), a checkpointer may + ** have omitted to checkpoint a frame earlier than minFrame in + ** the file because there exists a frame after iSnapshot that + ** is the same database page. */ + pWal->minFrame = 1; if( rc!=SQLITE_OK ){ sqlite3WalEndReadTransaction(pWal); } } } + + /* Release the shared CKPT lock obtained above. */ + if( ckptLock ){ + assert( pSnapshot ); + walUnlockShared(pWal, WAL_CKPT_LOCK); + } #endif return rc; } +/* +** Begin a read transaction on the database. +** +** This routine used to be called sqlite3OpenSnapshot() and with good reason: +** it takes a snapshot of the state of the WAL and wal-index for the current +** instant in time. The current thread will continue to use this snapshot. +** Other threads might append new content to the WAL and wal-index but +** that extra content is ignored by the current thread. +** +** If the database contents have changes since the previous read +** transaction, then *pChanged is set to 1 before returning. The +** Pager layer will use this to know that its cache is stale and +** needs to be flushed. +*/ +int sqlite3WalBeginReadTransaction(Wal *pWal, int *pChanged){ + int rc; + SEH_TRY { + rc = walBeginReadTransaction(pWal, pChanged); + } + SEH_EXCEPT( rc = walHandleException(pWal); ) + return rc; +} + /* ** Finish with a read transaction. All this does is release the ** read-lock. */ void sqlite3WalEndReadTransaction(Wal *pWal){ - sqlite3WalEndWriteTransaction(pWal); +#ifndef SQLITE_ENABLE_SETLK_TIMEOUT + assert( pWal->writeLock==0 || pWal->readLock<0 ); +#endif if( pWal->readLock>=0 ){ + (void)sqlite3WalEndWriteTransaction(pWal); walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock)); pWal->readLock = -1; } @@ -2488,7 +3502,7 @@ void sqlite3WalEndReadTransaction(Wal *pWal){ ** Return SQLITE_OK if successful, or an error code if an error occurs. If an ** error does occur, the final value of *piRead is undefined. */ -int sqlite3WalFindFrame( +static int walFindFrame( Wal *pWal, /* WAL handle */ Pgno pgno, /* Database page number to read data for */ u32 *piRead /* OUT: Frame number (or zero) */ @@ -2503,11 +3517,11 @@ int sqlite3WalFindFrame( /* If the "last page" field of the wal-index header snapshot is 0, then ** no data will be read from the wal under any circumstances. Return early - ** in this case as an optimization. Likewise, if pWal->readLock==0, - ** then the WAL is ignored by the reader so return early, as if the + ** in this case as an optimization. Likewise, if pWal->readLock==0, + ** then the WAL is ignored by the reader so return early, as if the ** WAL were empty. */ - if( iLast==0 || pWal->readLock==0 ){ + if( iLast==0 || (pWal->readLock==0 && pWal->bShmUnreliable==0) ){ *piRead = 0; return SQLITE_OK; } @@ -2517,9 +3531,9 @@ int sqlite3WalFindFrame( ** hash table (each hash table indexes up to HASHTABLE_NPAGE frames). ** ** This code might run concurrently to the code in walIndexAppend() - ** that adds entries to the wal-index (and possibly to this hash - ** table). This means the value just read from the hash - ** slot (aHash[iKey]) may have been added before or after the + ** that adds entries to the wal-index (and possibly to this hash + ** table). This means the value just read from the hash + ** slot (aHash[iKey]) may have been added before or after the ** current read transaction was opened. Values added after the ** read transaction was opened may have been written incorrectly - ** i.e. these slots may contain garbage data. However, we assume @@ -2527,40 +3541,44 @@ int sqlite3WalFindFrame( ** opened remain unmodified. ** ** For the reasons above, the if(...) condition featured in the inner - ** loop of the following block is more stringent that would be required + ** loop of the following block is more stringent that would be required ** if we had exclusive access to the hash-table: ** - ** (aPgno[iFrame]==pgno): + ** (aPgno[iFrame]==pgno): ** This condition filters out normal hash-table collisions. ** - ** (iFrame<=iLast): + ** (iFrame<=iLast): ** This condition filters out entries that were added to the hash ** table after the current read-transaction had started. */ iMinHash = walFramePage(pWal->minFrame); - for(iHash=walFramePage(iLast); iHash>=iMinHash && iRead==0; iHash--){ - volatile ht_slot *aHash; /* Pointer to hash table */ - volatile u32 *aPgno; /* Pointer to array of page numbers */ - u32 iZero; /* Frame number corresponding to aPgno[0] */ + for(iHash=walFramePage(iLast); iHash>=iMinHash; iHash--){ + WalHashLoc sLoc; /* Hash table location */ int iKey; /* Hash slot index */ int nCollide; /* Number of hash collisions remaining */ int rc; /* Error code */ + u32 iH; - rc = walHashGet(pWal, iHash, &aHash, &aPgno, &iZero); + rc = walHashGet(pWal, iHash, &sLoc); if( rc!=SQLITE_OK ){ return rc; } nCollide = HASHTABLE_NSLOT; - for(iKey=walHash(pgno); aHash[iKey]; iKey=walNextHash(iKey)){ - u32 iFrame = aHash[iKey] + iZero; - if( iFrame<=iLast && iFrame>=pWal->minFrame && aPgno[aHash[iKey]]==pgno ){ + iKey = walHash(pgno); + SEH_INJECT_FAULT; + while( (iH = AtomicLoad(&sLoc.aHash[iKey]))!=0 ){ + u32 iFrame = iH + sLoc.iZero; + if( iFrame<=iLast && iFrame>=pWal->minFrame && sLoc.aPgno[iH-1]==pgno ){ assert( iFrame>iRead || CORRUPT_DB ); iRead = iFrame; } if( (nCollide--)==0 ){ + *piRead = 0; return SQLITE_CORRUPT_BKPT; } + iKey = walNextHash(iKey); } + if( iRead ) break; } #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT @@ -2570,8 +3588,8 @@ int sqlite3WalFindFrame( { u32 iRead2 = 0; u32 iTest; - assert( pWal->minFrame>0 ); - for(iTest=iLast; iTest>=pWal->minFrame; iTest--){ + assert( pWal->bShmUnreliable || pWal->minFrame>0 ); + for(iTest=iLast; iTest>=pWal->minFrame && iTest>0; iTest--){ if( walFramePgno(pWal, iTest)==pgno ){ iRead2 = iTest; break; @@ -2585,6 +3603,30 @@ int sqlite3WalFindFrame( return SQLITE_OK; } +/* +** Search the wal file for page pgno. If found, set *piRead to the frame that +** contains the page. Otherwise, if pgno is not in the wal file, set *piRead +** to zero. +** +** Return SQLITE_OK if successful, or an error code if an error occurs. If an +** error does occur, the final value of *piRead is undefined. +** +** The difference between this function and walFindFrame() is that this +** function wraps walFindFrame() in an SEH_TRY{...} block. +*/ +int sqlite3WalFindFrame( + Wal *pWal, /* WAL handle */ + Pgno pgno, /* Database page number to read data for */ + u32 *piRead /* OUT: Frame number (or zero) */ +){ + int rc; + SEH_TRY { + rc = walFindFrame(pWal, pgno, piRead); + } + SEH_EXCEPT( rc = SQLITE_IOERR_IN_PAGE; ) + return rc; +} + /* ** Read the contents of frame iRead from the wal file into buffer pOut ** (which is nOut bytes in size). Return SQLITE_OK if successful, or an @@ -2607,7 +3649,7 @@ int sqlite3WalReadFrame( return sqlite3OsRead(pWal->pWalFd, pOut, (nOut>sz ? sz : nOut), iOffset); } -/* +/* ** Return the size of the database in pages (or zero, if unknown). */ Pgno sqlite3WalDbsize(Wal *pWal){ @@ -2618,7 +3660,7 @@ Pgno sqlite3WalDbsize(Wal *pWal){ } -/* +/* ** This function starts a write transaction on the WAL. ** ** A read transaction must have already been started by a prior call @@ -2634,6 +3676,16 @@ Pgno sqlite3WalDbsize(Wal *pWal){ int sqlite3WalBeginWriteTransaction(Wal *pWal){ int rc; +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT + /* If the write-lock is already held, then it was obtained before the + ** read-transaction was even opened, making this call a no-op. + ** Return early. */ + if( pWal->writeLock ){ + assert( !memcmp(&pWal->hdr,(void*)pWal->apWiData[0],sizeof(WalIndexHdr)) ); + return SQLITE_OK; + } +#endif + /* Cannot start a write transaction without first holding a read ** transaction. */ assert( pWal->readLock>=0 ); @@ -2656,12 +3708,17 @@ int sqlite3WalBeginWriteTransaction(Wal *pWal){ ** time the read transaction on this connection was started, then ** the write is disallowed. */ - if( memcmp(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr))!=0 ){ + SEH_TRY { + if( memcmp(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr))!=0 ){ + rc = SQLITE_BUSY_SNAPSHOT; + } + } + SEH_EXCEPT( rc = SQLITE_IOERR_IN_PAGE; ) + + if( rc!=SQLITE_OK ){ walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1); pWal->writeLock = 0; - rc = SQLITE_BUSY_SNAPSHOT; } - return rc; } @@ -2696,39 +3753,43 @@ int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx){ if( ALWAYS(pWal->writeLock) ){ Pgno iMax = pWal->hdr.mxFrame; Pgno iFrame; - - /* Restore the clients cache of the wal-index header to the state it - ** was in before the client began writing to the database. - */ - memcpy(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr)); - for(iFrame=pWal->hdr.mxFrame+1; - ALWAYS(rc==SQLITE_OK) && iFrame<=iMax; - iFrame++ - ){ - /* This call cannot fail. Unless the page for which the page number - ** is passed as the second argument is (a) in the cache and - ** (b) has an outstanding reference, then xUndo is either a no-op - ** (if (a) is false) or simply expels the page from the cache (if (b) - ** is false). - ** - ** If the upper layer is doing a rollback, it is guaranteed that there - ** are no outstanding references to any page other than page 1. And - ** page 1 is never written to the log until the transaction is - ** committed. As a result, the call to xUndo may not fail. + SEH_TRY { + /* Restore the clients cache of the wal-index header to the state it + ** was in before the client began writing to the database. */ - assert( walFramePgno(pWal, iFrame)!=1 ); - rc = xUndo(pUndoCtx, walFramePgno(pWal, iFrame)); + memcpy(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr)); + + for(iFrame=pWal->hdr.mxFrame+1; + ALWAYS(rc==SQLITE_OK) && iFrame<=iMax; + iFrame++ + ){ + /* This call cannot fail. Unless the page for which the page number + ** is passed as the second argument is (a) in the cache and + ** (b) has an outstanding reference, then xUndo is either a no-op + ** (if (a) is false) or simply expels the page from the cache (if (b) + ** is false). + ** + ** If the upper layer is doing a rollback, it is guaranteed that there + ** are no outstanding references to any page other than page 1. And + ** page 1 is never written to the log until the transaction is + ** committed. As a result, the call to xUndo may not fail. + */ + assert( walFramePgno(pWal, iFrame)!=1 ); + rc = xUndo(pUndoCtx, walFramePgno(pWal, iFrame)); + } + if( iMax!=pWal->hdr.mxFrame ) walCleanupHash(pWal); } - if( iMax!=pWal->hdr.mxFrame ) walCleanupHash(pWal); + SEH_EXCEPT( rc = SQLITE_IOERR_IN_PAGE; ) + pWal->iReCksum = 0; } return rc; } -/* -** Argument aWalData must point to an array of WAL_SAVEPOINT_NDATA u32 -** values. This function populates the array with values required to -** "rollback" the write position of the WAL handle back to the current +/* +** Argument aWalData must point to an array of WAL_SAVEPOINT_NDATA u32 +** values. This function populates the array with values required to +** "rollback" the write position of the WAL handle back to the current ** point in the event of a savepoint rollback (via WalSavepointUndo()). */ void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData){ @@ -2739,7 +3800,7 @@ void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData){ aWalData[3] = pWal->nCkpt; } -/* +/* ** Move the write position of the WAL back to the point identified by ** the values in the aWalData[] array. aWalData must point to an array ** of WAL_SAVEPOINT_NDATA u32 values that has been previously populated @@ -2764,7 +3825,13 @@ int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData){ pWal->hdr.mxFrame = aWalData[0]; pWal->hdr.aFrameCksum[0] = aWalData[1]; pWal->hdr.aFrameCksum[1] = aWalData[2]; - walCleanupHash(pWal); + SEH_TRY { + walCleanupHash(pWal); + } + SEH_EXCEPT( rc = SQLITE_IOERR_IN_PAGE; ) + if( pWal->iReCksum>pWal->hdr.mxFrame ){ + pWal->iReCksum = 0; + } } return rc; @@ -2814,7 +3881,7 @@ static int walRestartLog(Wal *pWal){ cnt = 0; do{ int notUsed; - rc = walTryBeginRead(pWal, &notUsed, 1, ++cnt); + rc = walTryBeginRead(pWal, &notUsed, 1, &cnt); }while( rc==WAL_RETRY ); assert( (rc&0xff)!=SQLITE_BUSY ); /* BUSY not possible when useWal==1 */ testcase( (rc&0xff)==SQLITE_IOERR ); @@ -2859,8 +3926,8 @@ static int walWriteToLog( iOffset += iFirstAmt; iAmt -= iFirstAmt; pContent = (void*)(iFirstAmt + (char*)pContent); - assert( p->syncFlags & (SQLITE_SYNC_NORMAL|SQLITE_SYNC_FULL) ); - rc = sqlite3OsSync(p->pFd, p->syncFlags & SQLITE_SYNC_MASK); + assert( WAL_SYNC_FLAGS(p->syncFlags)!=0 ); + rc = sqlite3OsSync(p->pFd, WAL_SYNC_FLAGS(p->syncFlags)); if( iAmt==0 || rc ) return rc; } rc = sqlite3OsWrite(p->pFd, pContent, iAmt, iOffset); @@ -2880,7 +3947,7 @@ static int walWriteOneFrame( void *pData; /* Data actually written */ u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-header in */ #if defined(SQLITE_HAS_CODEC) - if( (pData = sqlite3PagerCodec(pPage))==0 ) return SQLITE_NOMEM; + if( (pData = sqlcipherPagerCodec(pPage))==0 ) return SQLITE_NOMEM_BKPT; #else pData = pPage->pData; #endif @@ -2909,7 +3976,7 @@ static int walRewriteChecksums(Wal *pWal, u32 iLast){ i64 iCksumOff; aBuf = sqlite3_malloc(szPage + WAL_FRAME_HDRSIZE); - if( aBuf==0 ) return SQLITE_NOMEM; + if( aBuf==0 ) return SQLITE_NOMEM_BKPT; /* Find the checksum values to use as input for the recalculating the ** first checksum. If the first frame is frame 1 (implying that the current @@ -2945,11 +4012,11 @@ static int walRewriteChecksums(Wal *pWal, u32 iLast){ return rc; } -/* +/* ** Write a set of frames to the log. The caller must hold the write-lock ** on the log file (obtained using sqlite3WalBeginWriteTransaction()). */ -int sqlite3WalFrames( +static int walFrames( Wal *pWal, /* Wal handle to write to */ int szPage, /* Database page-size in bytes */ PgHdr *pList, /* List of dirty pages to write */ @@ -3012,7 +4079,7 @@ int sqlite3WalFrames( walChecksumBytes(1, aWalHdr, WAL_HDRSIZE-2*4, 0, aCksum); sqlite3Put4byte(&aWalHdr[24], aCksum[0]); sqlite3Put4byte(&aWalHdr[28], aCksum[1]); - + pWal->szPage = szPage; pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN; pWal->hdr.aFrameCksum[0] = aCksum[0]; @@ -3030,14 +4097,16 @@ int sqlite3WalFrames( ** an out-of-order write following a WAL restart could result in ** database corruption. See the ticket: ** - ** http://localhost:591/sqlite/info/ff5be73dee + ** https://sqlite.org/src/info/ff5be73dee */ - if( pWal->syncHeader && sync_flags ){ - rc = sqlite3OsSync(pWal->pWalFd, sync_flags & SQLITE_SYNC_MASK); + if( pWal->syncHeader ){ + rc = sqlite3OsSync(pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags)); if( rc ) return rc; } } - assert( (int)pWal->szPage==szPage ); + if( (int)pWal->szPage!=szPage ){ + return SQLITE_CORRUPT_BKPT; /* TH3 test case: cov1/corrupt155.test */ + } /* Setup information needed to write frames into the WAL */ w.pWal = pWal; @@ -3054,11 +4123,11 @@ int sqlite3WalFrames( /* Check if this page has already been written into the wal file by ** the current transaction. If so, overwrite the existing frame and - ** set Wal.writeLock to WAL_WRITELOCK_RECKSUM - indicating that + ** set Wal.writeLock to WAL_WRITELOCK_RECKSUM - indicating that ** checksums must be recomputed when the transaction is committed. */ if( iFirst && (p->pDirty || isCommit==0) ){ u32 iWrite = 0; - VVA_ONLY(rc =) sqlite3WalFindFrame(pWal, p->pgno, &iWrite); + VVA_ONLY(rc =) walFindFrame(pWal, p->pgno, &iWrite); assert( rc==SQLITE_OK || iWrite==0 ); if( iWrite>=iFirst ){ i64 iOff = walFrameOffset(iWrite, szPage) + WAL_FRAME_HDRSIZE; @@ -3067,7 +4136,7 @@ int sqlite3WalFrames( pWal->iReCksum = iWrite; } #if defined(SQLITE_HAS_CODEC) - if( (pData = sqlite3PagerCodec(p))==0 ) return SQLITE_NOMEM; + if( (pData = sqlcipherPagerCodec(p))==0 ) return SQLITE_NOMEM; #else pData = p->pData; #endif @@ -3108,18 +4177,24 @@ int sqlite3WalFrames( ** sector boundary is synced; the part of the last frame that extends ** past the sector boundary is written after the sync. */ - if( isCommit && (sync_flags & WAL_SYNC_TRANSACTIONS)!=0 ){ + if( isCommit && WAL_SYNC_FLAGS(sync_flags)!=0 ){ + int bSync = 1; if( pWal->padToSectorBoundary ){ int sectorSize = sqlite3SectorSize(pWal->pWalFd); w.iSyncPoint = ((iOffset+sectorSize-1)/sectorSize)*sectorSize; + bSync = (w.iSyncPoint==iOffset); + testcase( bSync ); while( iOffset<w.iSyncPoint ){ rc = walWriteOneFrame(&w, pLast, nTruncate, iOffset); if( rc ) return rc; iOffset += szFrame; nExtra++; + assert( pLast!=0 ); } - }else{ - rc = sqlite3OsSync(w.pFd, sync_flags & SQLITE_SYNC_MASK); + } + if( bSync ){ + assert( rc==SQLITE_OK ); + rc = sqlite3OsSync(w.pFd, WAL_SYNC_FLAGS(sync_flags)); } } @@ -3136,7 +4211,7 @@ int sqlite3WalFrames( pWal->truncateOnCommit = 0; } - /* Append data to the wal-index. It is not necessary to lock the + /* Append data to the wal-index. It is not necessary to lock the ** wal-index to do this as the SQLITE_SHM_WRITE lock held on the wal-index ** guarantees that there are no other writers, and no data that may ** be in use by existing readers is being overwritten. @@ -3147,6 +4222,7 @@ int sqlite3WalFrames( iFrame++; rc = walIndexAppend(pWal, iFrame, p->pgno); } + assert( pLast!=0 || nExtra==0 ); while( rc==SQLITE_OK && nExtra>0 ){ iFrame++; nExtra--; @@ -3175,6 +4251,29 @@ int sqlite3WalFrames( } /* +** Write a set of frames to the log. The caller must hold the write-lock +** on the log file (obtained using sqlite3WalBeginWriteTransaction()). +** +** The difference between this function and walFrames() is that this +** function wraps walFrames() in an SEH_TRY{...} block. +*/ +int sqlite3WalFrames( + Wal *pWal, /* Wal handle to write to */ + int szPage, /* Database page-size in bytes */ + PgHdr *pList, /* List of dirty pages to write */ + Pgno nTruncate, /* Database size after this commit */ + int isCommit, /* True if this is a commit */ + int sync_flags /* Flags to pass to OsSync() (or 0) */ +){ + int rc; + SEH_TRY { + rc = walFrames(pWal, szPage, pList, nTruncate, isCommit, sync_flags); + } + SEH_EXCEPT( rc = walHandleException(pWal); ) + return rc; +} + +/* ** This routine is called to implement sqlite3_wal_checkpoint() and ** related interfaces. ** @@ -3186,6 +4285,7 @@ int sqlite3WalFrames( */ int sqlite3WalCheckpoint( Wal *pWal, /* Wal connection */ + sqlite3 *db, /* Check this handle's interrupt flag */ int eMode, /* PASSIVE, FULL, RESTART, or TRUNCATE */ int (*xBusy)(void*), /* Function to call when busy */ void *pBusyArg, /* Context argument for xBusyHandler */ @@ -3205,73 +4305,92 @@ int sqlite3WalCheckpoint( /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked ** in the SQLITE_CHECKPOINT_PASSIVE mode. */ - assert( eMode!=SQLITE_CHECKPOINT_PASSIVE || xBusy==0 ); + assert( SQLITE_CHECKPOINT_NOOP<SQLITE_CHECKPOINT_PASSIVE ); + assert( eMode>SQLITE_CHECKPOINT_PASSIVE || xBusy==0 ); if( pWal->readOnly ) return SQLITE_READONLY; WALTRACE(("WAL%p: checkpoint begins\n", pWal)); - /* IMPLEMENTATION-OF: R-62028-47212 All calls obtain an exclusive - ** "checkpoint" lock on the database file. */ - rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1); - if( rc ){ - /* EVIDENCE-OF: R-10421-19736 If any other process is running a - ** checkpoint operation at the same time, the lock cannot be obtained and - ** SQLITE_BUSY is returned. - ** EVIDENCE-OF: R-53820-33897 Even if there is a busy-handler configured, - ** it will not be invoked in this case. - */ - testcase( rc==SQLITE_BUSY ); - testcase( xBusy!=0 ); - return rc; - } - pWal->ckptLock = 1; - - /* IMPLEMENTATION-OF: R-59782-36818 The SQLITE_CHECKPOINT_FULL, RESTART and - ** TRUNCATE modes also obtain the exclusive "writer" lock on the database - ** file. - ** - ** EVIDENCE-OF: R-60642-04082 If the writer lock cannot be obtained - ** immediately, and a busy-handler is configured, it is invoked and the - ** writer lock retried until either the busy-handler returns 0 or the - ** lock is successfully obtained. + /* Enable blocking locks, if possible. */ + sqlite3WalDb(pWal, db); + if( xBusy2 ) (void)walEnableBlocking(pWal); + + /* IMPLEMENTATION-OF: R-62028-47212 All calls obtain an exclusive + ** "checkpoint" lock on the database file. + ** EVIDENCE-OF: R-10421-19736 If any other process is running a + ** checkpoint operation at the same time, the lock cannot be obtained and + ** SQLITE_BUSY is returned. + ** EVIDENCE-OF: R-53820-33897 Even if there is a busy-handler configured, + ** it will not be invoked in this case. */ - if( eMode!=SQLITE_CHECKPOINT_PASSIVE ){ - rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_WRITE_LOCK, 1); + if( eMode!=SQLITE_CHECKPOINT_NOOP ){ + rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1); + testcase( rc==SQLITE_BUSY ); + testcase( rc!=SQLITE_OK && xBusy2!=0 ); if( rc==SQLITE_OK ){ - pWal->writeLock = 1; - }else if( rc==SQLITE_BUSY ){ - eMode2 = SQLITE_CHECKPOINT_PASSIVE; - xBusy2 = 0; - rc = SQLITE_OK; - } - } + pWal->ckptLock = 1; - /* Read the wal-index header. */ - if( rc==SQLITE_OK ){ - rc = walIndexReadHdr(pWal, &isChanged); - if( isChanged && pWal->pDbFd->pMethods->iVersion>=3 ){ - sqlite3OsUnfetch(pWal->pDbFd, 0, 0); + /* IMPLEMENTATION-OF: R-59782-36818 The SQLITE_CHECKPOINT_FULL, RESTART + ** and TRUNCATE modes also obtain the exclusive "writer" lock on the + ** database file. + ** + ** EVIDENCE-OF: R-60642-04082 If the writer lock cannot be obtained + ** immediately, and a busy-handler is configured, it is invoked and the + ** writer lock retried until either the busy-handler returns 0 or the + ** lock is successfully obtained. + */ + if( eMode!=SQLITE_CHECKPOINT_PASSIVE ){ + rc = walBusyLock(pWal, xBusy2, pBusyArg, WAL_WRITE_LOCK, 1); + if( rc==SQLITE_OK ){ + pWal->writeLock = 1; + }else if( rc==SQLITE_BUSY ){ + eMode2 = SQLITE_CHECKPOINT_PASSIVE; + xBusy2 = 0; + rc = SQLITE_OK; + } + } } + }else{ + rc = SQLITE_OK; } - /* Copy data from the log to the database file. */ - if( rc==SQLITE_OK ){ - if( pWal->hdr.mxFrame && walPagesize(pWal)!=nBuf ){ - rc = SQLITE_CORRUPT_BKPT; - }else{ - rc = walCheckpoint(pWal, eMode2, xBusy2, pBusyArg, sync_flags, zBuf); + /* Read the wal-index header. */ + SEH_TRY { + if( rc==SQLITE_OK ){ + /* For a passive checkpoint, do not re-enable blocking locks after + ** reading the wal-index header. A passive checkpoint should not block + ** or invoke the busy handler. The only lock such a checkpoint may + ** attempt to obtain is a lock on a read-slot, and it should give up + ** immediately and do a partial checkpoint if it cannot obtain it. */ + walDisableBlocking(pWal); + rc = walIndexReadHdr(pWal, &isChanged); + if( eMode2>SQLITE_CHECKPOINT_PASSIVE ) (void)walEnableBlocking(pWal); + if( isChanged && pWal->pDbFd->pMethods->iVersion>=3 ){ + sqlite3OsUnfetch(pWal->pDbFd, 0, 0); + } } + + /* Copy data from the log to the database file. */ + if( rc==SQLITE_OK ){ + if( pWal->hdr.mxFrame && walPagesize(pWal)!=nBuf ){ + rc = SQLITE_CORRUPT_BKPT; + }else if( eMode2!=SQLITE_CHECKPOINT_NOOP ){ + rc = walCheckpoint(pWal, db, eMode2, xBusy2, pBusyArg, sync_flags,zBuf); + } - /* If no error occurred, set the output variables. */ - if( rc==SQLITE_OK || rc==SQLITE_BUSY ){ - if( pnLog ) *pnLog = (int)pWal->hdr.mxFrame; - if( pnCkpt ) *pnCkpt = (int)(walCkptInfo(pWal)->nBackfill); + /* If no error occurred, set the output variables. */ + if( rc==SQLITE_OK || rc==SQLITE_BUSY ){ + if( pnLog ) *pnLog = (int)pWal->hdr.mxFrame; + SEH_INJECT_FAULT; + if( pnCkpt ) *pnCkpt = (int)(walCkptInfo(pWal)->nBackfill); + } } } + SEH_EXCEPT( rc = walHandleException(pWal); ) if( isChanged ){ - /* If a new wal-index header was loaded before the checkpoint was + /* If a new wal-index header was loaded before the checkpoint was ** performed, then the pager-cache associated with pWal is now ** out of date. So zero the cached wal-index header to ensure that ** next time the pager opens a snapshot on this database it knows that @@ -3280,11 +4399,19 @@ int sqlite3WalCheckpoint( memset(&pWal->hdr, 0, sizeof(WalIndexHdr)); } + walDisableBlocking(pWal); + sqlite3WalDb(pWal, 0); + /* Release the locks. */ - sqlite3WalEndWriteTransaction(pWal); - walUnlockExclusive(pWal, WAL_CKPT_LOCK, 1); - pWal->ckptLock = 0; + (void)sqlite3WalEndWriteTransaction(pWal); + if( pWal->ckptLock ){ + walUnlockExclusive(pWal, WAL_CKPT_LOCK, 1); + pWal->ckptLock = 0; + } WALTRACE(("WAL%p: checkpoint %s\n", pWal, rc ? "failed" : "ok")); +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT + if( rc==SQLITE_BUSY_TIMEOUT ) rc = SQLITE_BUSY; +#endif return (rc==SQLITE_OK && eMode!=eMode2 ? SQLITE_BUSY : rc); } @@ -3314,7 +4441,7 @@ int sqlite3WalCallback(Wal *pWal){ ** operation must occur while the pager is still holding the exclusive ** lock on the main database file. ** -** If op is one, then change from locking_mode=NORMAL into +** If op is one, then change from locking_mode=NORMAL into ** locking_mode=EXCLUSIVE. This means that the pWal->readLock must ** be released. Return 1 if the transition is made and 0 if the ** WAL is already in exclusive-locking mode - meaning that this @@ -3331,42 +4458,44 @@ int sqlite3WalExclusiveMode(Wal *pWal, int op){ assert( pWal->writeLock==0 ); assert( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE || op==-1 ); - /* pWal->readLock is usually set, but might be -1 if there was a - ** prior error while attempting to acquire are read-lock. This cannot + /* pWal->readLock is usually set, but might be -1 if there was a + ** prior error while attempting to acquire are read-lock. This cannot ** happen if the connection is actually in exclusive mode (as no xShmLock ** locks are taken in this case). Nor should the pager attempt to ** upgrade to exclusive-mode following such an error. */ +#ifndef SQLITE_USE_SEH assert( pWal->readLock>=0 || pWal->lockError ); +#endif assert( pWal->readLock>=0 || (op<=0 && pWal->exclusiveMode==0) ); if( op==0 ){ - if( pWal->exclusiveMode ){ - pWal->exclusiveMode = 0; + if( pWal->exclusiveMode!=WAL_NORMAL_MODE ){ + pWal->exclusiveMode = WAL_NORMAL_MODE; if( walLockShared(pWal, WAL_READ_LOCK(pWal->readLock))!=SQLITE_OK ){ - pWal->exclusiveMode = 1; + pWal->exclusiveMode = WAL_EXCLUSIVE_MODE; } - rc = pWal->exclusiveMode==0; + rc = pWal->exclusiveMode==WAL_NORMAL_MODE; }else{ /* Already in locking_mode=NORMAL */ rc = 0; } }else if( op>0 ){ - assert( pWal->exclusiveMode==0 ); + assert( pWal->exclusiveMode==WAL_NORMAL_MODE ); assert( pWal->readLock>=0 ); walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock)); - pWal->exclusiveMode = 1; + pWal->exclusiveMode = WAL_EXCLUSIVE_MODE; rc = 1; }else{ - rc = pWal->exclusiveMode==0; + rc = pWal->exclusiveMode==WAL_NORMAL_MODE; } return rc; } -/* +/* ** Return true if the argument is non-NULL and the WAL module is using ** heap-memory for the wal-index. Otherwise, if the argument is NULL or the -** WAL module is using shared-memory, return false. +** WAL module is using shared-memory, return false. */ int sqlite3WalHeapMemory(Wal *pWal){ return (pWal && pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ); @@ -3380,12 +4509,17 @@ int sqlite3WalHeapMemory(Wal *pWal){ int sqlite3WalSnapshotGet(Wal *pWal, sqlite3_snapshot **ppSnapshot){ int rc = SQLITE_OK; WalIndexHdr *pRet; + static const u32 aZero[4] = { 0, 0, 0, 0 }; assert( pWal->readLock>=0 && pWal->writeLock==0 ); + if( memcmp(&pWal->hdr.aFrameCksum[0],aZero,16)==0 ){ + *ppSnapshot = 0; + return SQLITE_ERROR; + } pRet = (WalIndexHdr*)sqlite3_malloc(sizeof(WalIndexHdr)); if( pRet==0 ){ - rc = SQLITE_NOMEM; + rc = SQLITE_NOMEM_BKPT; }else{ memcpy(pRet, &pWal->hdr, sizeof(WalIndexHdr)); *ppSnapshot = (sqlite3_snapshot*)pRet; @@ -3396,9 +4530,82 @@ int sqlite3WalSnapshotGet(Wal *pWal, sqlite3_snapshot **ppSnapshot){ /* Try to open on pSnapshot when the next read-transaction starts */ -void sqlite3WalSnapshotOpen(Wal *pWal, sqlite3_snapshot *pSnapshot){ - pWal->pSnapshot = (WalIndexHdr*)pSnapshot; +void sqlite3WalSnapshotOpen( + Wal *pWal, + sqlite3_snapshot *pSnapshot +){ + if( pSnapshot && ((WalIndexHdr*)pSnapshot)->iVersion==0 ){ + /* iVersion==0 means that this is a call to sqlite3_snapshot_get(). In + ** this case set the bGetSnapshot flag so that if the call to + ** sqlite3_snapshot_get() is about to read transaction on this wal + ** file, it does not take read-lock 0 if the wal file has been completely + ** checkpointed. Taking read-lock 0 would work, but then it would be + ** possible for a subsequent writer to destroy the snapshot even while + ** this connection is holding its read-transaction open. This is contrary + ** to user expectations, so we avoid it by not taking read-lock 0. */ + pWal->bGetSnapshot = 1; + }else{ + pWal->pSnapshot = (WalIndexHdr*)pSnapshot; + pWal->bGetSnapshot = 0; + } +} + +/* +** Return a +ve value if snapshot p1 is newer than p2. A -ve value if +** p1 is older than p2 and zero if p1 and p2 are the same snapshot. +*/ +int sqlite3_snapshot_cmp(sqlite3_snapshot *p1, sqlite3_snapshot *p2){ + WalIndexHdr *pHdr1 = (WalIndexHdr*)p1; + WalIndexHdr *pHdr2 = (WalIndexHdr*)p2; + + /* aSalt[0] is a copy of the value stored in the wal file header. It + ** is incremented each time the wal file is restarted. */ + if( pHdr1->aSalt[0]<pHdr2->aSalt[0] ) return -1; + if( pHdr1->aSalt[0]>pHdr2->aSalt[0] ) return +1; + if( pHdr1->mxFrame<pHdr2->mxFrame ) return -1; + if( pHdr1->mxFrame>pHdr2->mxFrame ) return +1; + return 0; } + +/* +** The caller currently has a read transaction open on the database. +** This function takes a SHARED lock on the CHECKPOINTER slot and then +** checks if the snapshot passed as the second argument is still +** available. If so, SQLITE_OK is returned. +** +** If the snapshot is not available, SQLITE_ERROR is returned. Or, if +** the CHECKPOINTER lock cannot be obtained, SQLITE_BUSY. If any error +** occurs (any value other than SQLITE_OK is returned), the CHECKPOINTER +** lock is released before returning. +*/ +int sqlite3WalSnapshotCheck(Wal *pWal, sqlite3_snapshot *pSnapshot){ + int rc; + SEH_TRY { + rc = walLockShared(pWal, WAL_CKPT_LOCK); + if( rc==SQLITE_OK ){ + WalIndexHdr *pNew = (WalIndexHdr*)pSnapshot; + if( memcmp(pNew->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt)) + || pNew->mxFrame<walCkptInfo(pWal)->nBackfillAttempted + ){ + rc = SQLITE_ERROR_SNAPSHOT; + walUnlockShared(pWal, WAL_CKPT_LOCK); + } + } + } + SEH_EXCEPT( rc = walHandleException(pWal); ) + return rc; +} + +/* +** Release a lock obtained by an earlier successful call to +** sqlite3WalSnapshotCheck(). +*/ +void sqlite3WalSnapshotUnlock(Wal *pWal){ + assert( pWal ); + walUnlockShared(pWal, WAL_CKPT_LOCK); +} + + #endif /* SQLITE_ENABLE_SNAPSHOT */ #ifdef SQLITE_ENABLE_ZIPVFS diff --git a/src/wal.h b/src/wal.h index 97e6ab4f10..1b17d2dfbe 100644 --- a/src/wal.h +++ b/src/wal.h @@ -14,21 +14,21 @@ ** the implementation of each function in log.c for further details. */ -#ifndef _WAL_H_ -#define _WAL_H_ +#ifndef SQLITE_WAL_H +#define SQLITE_WAL_H #include "sqliteInt.h" -/* Additional values that can be added to the sync_flags argument of -** sqlite3WalFrames(): +/* Macros for extracting appropriate sync flags for either transaction +** commits (WAL_SYNC_FLAGS(X)) or for checkpoint ops (CKPT_SYNC_FLAGS(X)): */ -#define WAL_SYNC_TRANSACTIONS 0x20 /* Sync at the end of each transaction */ -#define SQLITE_SYNC_MASK 0x13 /* Mask off the SQLITE_SYNC_* values */ +#define WAL_SYNC_FLAGS(X) ((X)&0x03) +#define CKPT_SYNC_FLAGS(X) (((X)>>2)&0x03) #ifdef SQLITE_OMIT_WAL # define sqlite3WalOpen(x,y,z) 0 # define sqlite3WalLimit(x,y) -# define sqlite3WalClose(w,x,y,z) 0 +# define sqlite3WalClose(v,w,x,y,z) 0 # define sqlite3WalBeginReadTransaction(y,z) 0 # define sqlite3WalEndReadTransaction(z) # define sqlite3WalDbsize(y) 0 @@ -38,13 +38,14 @@ # define sqlite3WalSavepoint(y,z) # define sqlite3WalSavepointUndo(y,z) 0 # define sqlite3WalFrames(u,v,w,x,y,z) 0 -# define sqlite3WalCheckpoint(r,s,t,u,v,w,x,y,z) 0 +# define sqlite3WalCheckpoint(q,r,s,t,u,v,w,x,y,z) 0 # define sqlite3WalCallback(z) 0 # define sqlite3WalExclusiveMode(y,z) 0 # define sqlite3WalHeapMemory(z) 0 # define sqlite3WalFramesize(z) 0 # define sqlite3WalFindFrame(x,y,z) 0 # define sqlite3WalFile(x) 0 +# undef SQLITE_USE_SEH #else #define WAL_SAVEPOINT_NDATA 4 @@ -56,7 +57,7 @@ typedef struct Wal Wal; /* Open and close a connection to a write-ahead log. */ int sqlite3WalOpen(sqlite3_vfs*, sqlite3_file*, const char *, int, i64, Wal**); -int sqlite3WalClose(Wal *pWal, int sync_flags, int, u8 *); +int sqlite3WalClose(Wal *pWal, sqlite3*, int sync_flags, int, u8 *); /* Set the limiting size of a WAL file. */ void sqlite3WalLimit(Wal*, i64); @@ -99,6 +100,7 @@ int sqlite3WalFrames(Wal *pWal, int, PgHdr *, Pgno, int, int); /* Copy pages from the log to the database file */ int sqlite3WalCheckpoint( Wal *pWal, /* Write-ahead log connection */ + sqlite3 *db, /* Check this handle's interrupt flag */ int eMode, /* One of PASSIVE, FULL and RESTART */ int (*xBusy)(void*), /* Function to call when busy */ void *pBusyArg, /* Context argument for xBusyHandler */ @@ -130,6 +132,9 @@ int sqlite3WalHeapMemory(Wal *pWal); #ifdef SQLITE_ENABLE_SNAPSHOT int sqlite3WalSnapshotGet(Wal *pWal, sqlite3_snapshot **ppSnapshot); void sqlite3WalSnapshotOpen(Wal *pWal, sqlite3_snapshot *pSnapshot); +int sqlite3WalSnapshotRecover(Wal *pWal); +int sqlite3WalSnapshotCheck(Wal *pWal, sqlite3_snapshot *pSnapshot); +void sqlite3WalSnapshotUnlock(Wal *pWal); #endif #ifdef SQLITE_ENABLE_ZIPVFS @@ -142,5 +147,14 @@ int sqlite3WalFramesize(Wal *pWal); /* Return the sqlite3_file object for the WAL file */ sqlite3_file *sqlite3WalFile(Wal *pWal); +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT +int sqlite3WalWriteLock(Wal *pWal, int bLock); +void sqlite3WalDb(Wal *pWal, sqlite3 *db); +#endif + +#ifdef SQLITE_USE_SEH +int sqlite3WalSystemErrno(Wal*); +#endif + #endif /* ifndef SQLITE_OMIT_WAL */ -#endif /* _WAL_H_ */ +#endif /* SQLITE_WAL_H */ diff --git a/src/walker.c b/src/walker.c index 1e0ad32871..c8735e39b8 100644 --- a/src/walker.c +++ b/src/walker.c @@ -17,6 +17,31 @@ #include <string.h> +#if !defined(SQLITE_OMIT_WINDOWFUNC) +/* +** Walk all expressions linked into the list of Window objects passed +** as the second argument. +*/ +static int walkWindowList(Walker *pWalker, Window *pList, int bOneOnly){ + Window *pWin; + for(pWin=pList; pWin; pWin=pWin->pNextWin){ + int rc; + rc = sqlite3WalkExprList(pWalker, pWin->pOrderBy); + if( rc ) return WRC_Abort; + rc = sqlite3WalkExprList(pWalker, pWin->pPartition); + if( rc ) return WRC_Abort; + rc = sqlite3WalkExpr(pWalker, pWin->pFilter); + if( rc ) return WRC_Abort; + rc = sqlite3WalkExpr(pWalker, pWin->pStart); + if( rc ) return WRC_Abort; + rc = sqlite3WalkExpr(pWalker, pWin->pEnd); + if( rc ) return WRC_Abort; + if( bOneOnly ) break; + } + return WRC_Continue; +} +#endif + /* ** Walk an expression tree. Invoke the callback once for each node ** of the expression, while descending. (In other words, the callback @@ -27,34 +52,51 @@ ** ** WRC_Continue Continue descending down the tree. ** -** WRC_Prune Do not descend into child nodes. But allow +** WRC_Prune Do not descend into child nodes, but allow ** the walk to continue with sibling nodes. ** ** WRC_Abort Do no more callbacks. Unwind the stack and -** return the top-level walk call. +** return from the top-level walk call. ** ** The return value from this routine is WRC_Abort to abandon the tree walk ** and WRC_Continue to continue. */ -static SQLITE_NOINLINE int walkExpr(Walker *pWalker, Expr *pExpr){ +SQLITE_NOINLINE int sqlite3WalkExprNN(Walker *pWalker, Expr *pExpr){ int rc; testcase( ExprHasProperty(pExpr, EP_TokenOnly) ); testcase( ExprHasProperty(pExpr, EP_Reduced) ); - rc = pWalker->xExprCallback(pWalker, pExpr); - if( rc==WRC_Continue - && !ExprHasProperty(pExpr,EP_TokenOnly) ){ - if( sqlite3WalkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort; - if( sqlite3WalkExpr(pWalker, pExpr->pRight) ) return WRC_Abort; - if( ExprHasProperty(pExpr, EP_xIsSelect) ){ - if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort; - }else{ - if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort; + while(1){ + rc = pWalker->xExprCallback(pWalker, pExpr); + if( rc ) return rc & WRC_Abort; + if( !ExprHasProperty(pExpr,(EP_TokenOnly|EP_Leaf)) ){ + assert( pExpr->x.pList==0 || pExpr->pRight==0 ); + if( pExpr->pLeft && sqlite3WalkExprNN(pWalker, pExpr->pLeft) ){ + return WRC_Abort; + } + if( pExpr->pRight ){ + assert( !ExprHasProperty(pExpr, EP_WinFunc) ); + pExpr = pExpr->pRight; + continue; + }else if( ExprUseXSelect(pExpr) ){ + assert( !ExprHasProperty(pExpr, EP_WinFunc) ); + if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort; + }else{ + if( pExpr->x.pList ){ + if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort; + } +#ifndef SQLITE_OMIT_WINDOWFUNC + if( ExprHasProperty(pExpr, EP_WinFunc) ){ + if( walkWindowList(pWalker, pExpr->y.pWin, 1) ) return WRC_Abort; + } +#endif + } } + break; } - return rc & WRC_Abort; + return WRC_Continue; } int sqlite3WalkExpr(Walker *pWalker, Expr *pExpr){ - return pExpr ? walkExpr(pWalker,pExpr) : WRC_Continue; + return pExpr ? sqlite3WalkExprNN(pWalker,pExpr) : WRC_Continue; } /* @@ -72,6 +114,16 @@ int sqlite3WalkExprList(Walker *pWalker, ExprList *p){ return WRC_Continue; } +/* +** This is a no-op callback for Walker->xSelectCallback2. If this +** callback is set, then the Select->pWinDefn list is traversed. +*/ +void sqlite3WalkWinDefnDummyCallback(Walker *pWalker, Select *p){ + UNUSED_PARAMETER(pWalker); + UNUSED_PARAMETER(p); + /* No-op */ +} + /* ** Walk all expressions associated with SELECT statement p. Do ** not invoke the SELECT callback on p, but do (of course) invoke @@ -85,7 +137,22 @@ int sqlite3WalkSelectExpr(Walker *pWalker, Select *p){ if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort; if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort; if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort; - if( sqlite3WalkExpr(pWalker, p->pOffset) ) return WRC_Abort; +#if !defined(SQLITE_OMIT_WINDOWFUNC) + if( p->pWinDefn ){ + Parse *pParse; + if( pWalker->xSelectCallback2==sqlite3WalkWinDefnDummyCallback + || ((pParse = pWalker->pParse)!=0 && IN_RENAME_OBJECT) +#ifndef SQLITE_OMIT_CTE + || pWalker->xSelectCallback2==sqlite3SelectPopWith +#endif + ){ + /* The following may return WRC_Abort if there are unresolvable + ** symbols (e.g. a table that does not exist) in a window definition. */ + int rc = walkWindowList(pWalker, p->pWinDefn, 0); + return rc; + } + } +#endif return WRC_Continue; } @@ -99,12 +166,14 @@ int sqlite3WalkSelectExpr(Walker *pWalker, Select *p){ int sqlite3WalkSelectFrom(Walker *pWalker, Select *p){ SrcList *pSrc; int i; - struct SrcList_item *pItem; + SrcItem *pItem; pSrc = p->pSrc; if( ALWAYS(pSrc) ){ for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){ - if( sqlite3WalkSelect(pWalker, pItem->pSelect) ){ + if( pItem->fg.isSubquery + && sqlite3WalkSelect(pWalker, pItem->u4.pSubq->pSelect) + ){ return WRC_Abort; } if( pItem->fg.isTabFunc @@ -115,7 +184,7 @@ int sqlite3WalkSelectFrom(Walker *pWalker, Select *p){ } } return WRC_Continue; -} +} /* ** Call sqlite3WalkExpr() for every expression in Select statement p. @@ -124,8 +193,9 @@ int sqlite3WalkSelectFrom(Walker *pWalker, Select *p){ ** ** If it is not NULL, the xSelectCallback() callback is invoked before ** the walk of the expressions and FROM clause. The xSelectCallback2() -** method, if it is not NULL, is invoked following the walk of the -** expressions and FROM clause. +** method is invoked following the walk of the expressions and FROM clause, +** but only if both xSelectCallback and xSelectCallback2 are both non-NULL +** and if the expressions and FROM clause both return WRC_Continue; ** ** Return WRC_Continue under normal conditions. Return WRC_Abort if ** there is an abort request. @@ -135,27 +205,57 @@ int sqlite3WalkSelectFrom(Walker *pWalker, Select *p){ */ int sqlite3WalkSelect(Walker *pWalker, Select *p){ int rc; - if( p==0 || (pWalker->xSelectCallback==0 && pWalker->xSelectCallback2==0) ){ - return WRC_Continue; - } - rc = WRC_Continue; - pWalker->walkerDepth++; - while( p ){ - if( pWalker->xSelectCallback ){ - rc = pWalker->xSelectCallback(pWalker, p); - if( rc ) break; - } + if( p==0 ) return WRC_Continue; + if( pWalker->xSelectCallback==0 ) return WRC_Continue; + do{ + rc = pWalker->xSelectCallback(pWalker, p); + if( rc ) return rc & WRC_Abort; if( sqlite3WalkSelectExpr(pWalker, p) || sqlite3WalkSelectFrom(pWalker, p) ){ - pWalker->walkerDepth--; return WRC_Abort; } if( pWalker->xSelectCallback2 ){ pWalker->xSelectCallback2(pWalker, p); } p = p->pPrior; - } + }while( p!=0 ); + return WRC_Continue; +} + +/* Increase the walkerDepth when entering a subquery, and +** decrease when leaving the subquery. +*/ +int sqlite3WalkerDepthIncrease(Walker *pWalker, Select *pSelect){ + UNUSED_PARAMETER(pSelect); + pWalker->walkerDepth++; + return WRC_Continue; +} +void sqlite3WalkerDepthDecrease(Walker *pWalker, Select *pSelect){ + UNUSED_PARAMETER(pSelect); pWalker->walkerDepth--; - return rc & WRC_Abort; +} + + +/* +** No-op routine for the parse-tree walker. +** +** When this routine is the Walker.xExprCallback then expression trees +** are walked without any actions being taken at each node. Presumably, +** when this routine is used for Walker.xExprCallback then +** Walker.xSelectCallback is set to do something useful for every +** subquery in the parser tree. +*/ +int sqlite3ExprWalkNoop(Walker *NotUsed, Expr *NotUsed2){ + UNUSED_PARAMETER2(NotUsed, NotUsed2); + return WRC_Continue; +} + +/* +** No-op routine for the parse-tree walker for SELECT statements. +** subquery in the parser tree. +*/ +int sqlite3SelectWalkNoop(Walker *NotUsed, Select *NotUsed2){ + UNUSED_PARAMETER2(NotUsed, NotUsed2); + return WRC_Continue; } diff --git a/src/where.c b/src/where.c index 8ecdd840f3..c4f2c55435 100644 --- a/src/where.c +++ b/src/where.c @@ -19,20 +19,40 @@ #include "sqliteInt.h" #include "whereInt.h" +/* +** Extra information appended to the end of sqlite3_index_info but not +** visible to the xBestIndex function, at least not directly. The +** sqlite3_vtab_collation() interface knows how to reach it, however. +** +** This object is not an API and can be changed from one release to the +** next. As long as allocateIndexInfo() and sqlite3_vtab_collation() +** agree on the structure, all will be well. +*/ +typedef struct HiddenIndexInfo HiddenIndexInfo; +struct HiddenIndexInfo { + WhereClause *pWC; /* The Where clause being analyzed */ + Parse *pParse; /* The parsing context */ + int eDistinct; /* Value to return from sqlite3_vtab_distinct() */ + u32 mIn; /* Mask of terms that are <col> IN (...) */ + u32 mHandleIn; /* Terms that vtab will handle as <col> IN (...) */ + sqlite3_value *aRhs[FLEXARRAY]; /* RHS values for constraints. MUST BE LAST + ** Extra space is allocated to hold up + ** to nTerm such values */ +}; + +/* Size (in bytes) of a HiddenIndeInfo object sufficient to hold as +** many as N constraints */ +#define SZ_HIDDENINDEXINFO(N) \ + (offsetof(HiddenIndexInfo,aRhs) + (N)*sizeof(sqlite3_value*)) + /* Forward declaration of methods */ static int whereLoopResize(sqlite3*, WhereLoop*, int); -/* Test variable that can be set to enable WHERE tracing */ -#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) -/***/ int sqlite3WhereTrace = 0; -#endif - - /* ** Return the estimated number of output rows from a WHERE clause */ -u64 sqlite3WhereOutputRowCount(WhereInfo *pWInfo){ - return sqlite3LogEstToInt(pWInfo->nRowOut); +LogEst sqlite3WhereOutputRowCount(WhereInfo *pWInfo){ + return pWInfo->nRowOut; } /* @@ -44,11 +64,76 @@ int sqlite3WhereIsDistinct(WhereInfo *pWInfo){ } /* -** Return TRUE if the WHERE clause returns rows in ORDER BY order. -** Return FALSE if the output needs to be sorted. +** Return the number of ORDER BY terms that are satisfied by the +** WHERE clause. A return of 0 means that the output must be +** completely sorted. A return equal to the number of ORDER BY +** terms means that no sorting is needed at all. A return that +** is positive but less than the number of ORDER BY terms means that +** block sorting is required. */ int sqlite3WhereIsOrdered(WhereInfo *pWInfo){ - return pWInfo->nOBSat; + return pWInfo->nOBSat<0 ? 0 : pWInfo->nOBSat; +} + +/* +** In the ORDER BY LIMIT optimization, if the inner-most loop is known +** to emit rows in increasing order, and if the last row emitted by the +** inner-most loop did not fit within the sorter, then we can skip all +** subsequent rows for the current iteration of the inner loop (because they +** will not fit in the sorter either) and continue with the second inner +** loop - the loop immediately outside the inner-most. +** +** When a row does not fit in the sorter (because the sorter already +** holds LIMIT+OFFSET rows that are smaller), then a jump is made to the +** label returned by this function. +** +** If the ORDER BY LIMIT optimization applies, the jump destination should +** be the continuation for the second-inner-most loop. If the ORDER BY +** LIMIT optimization does not apply, then the jump destination should +** be the continuation for the inner-most loop. +** +** It is always safe for this routine to return the continuation of the +** inner-most loop, in the sense that a correct answer will result. +** Returning the continuation the second inner loop is an optimization +** that might make the code run a little faster, but should not change +** the final answer. +*/ +int sqlite3WhereOrderByLimitOptLabel(WhereInfo *pWInfo){ + WhereLevel *pInner; + if( !pWInfo->bOrderedInnerLoop ){ + /* The ORDER BY LIMIT optimization does not apply. Jump to the + ** continuation of the inner-most loop. */ + return pWInfo->iContinue; + } + pInner = &pWInfo->a[pWInfo->nLevel-1]; + assert( pInner->addrNxt!=0 ); + return pInner->pRJ ? pWInfo->iContinue : pInner->addrNxt; +} + +/* +** While generating code for the min/max optimization, after handling +** the aggregate-step call to min() or max(), check to see if any +** additional looping is required. If the output order is such that +** we are certain that the correct answer has already been found, then +** code an OP_Goto to by pass subsequent processing. +** +** Any extra OP_Goto that is coded here is an optimization. The +** correct answer should be obtained regardless. This OP_Goto just +** makes the answer appear faster. +*/ +void sqlite3WhereMinMaxOptEarlyOut(Vdbe *v, WhereInfo *pWInfo){ + WhereLevel *pInner; + int i; + if( !pWInfo->bOrderedInnerLoop ) return; + if( pWInfo->nOBSat==0 ) return; + for(i=pWInfo->nLevel-1; i>=0; i--){ + pInner = &pWInfo->a[i]; + if( (pInner->pWLoop->wsFlags & WHERE_COLUMN_IN)!=0 ){ + sqlite3VdbeGoto(v, pInner->addrNxt); + return; + } + } + sqlite3VdbeGoto(v, pWInfo->iBreak); } /* @@ -70,10 +155,10 @@ int sqlite3WhereBreakLabel(WhereInfo *pWInfo){ /* ** Return ONEPASS_OFF (0) if an UPDATE or DELETE statement is unable to -** operate directly on the rowis returned by a WHERE clause. Return +** operate directly on the rowids returned by a WHERE clause. Return ** ONEPASS_SINGLE (1) if the statement can operation directly because only ** a single row is to be changed. Return ONEPASS_MULTI (2) if the one-pass -** optimization can be used on multiple +** optimization can be used on multiple ** ** If the ONEPASS optimization is used (if this routine returns true) ** then also write the indices of open cursors used by ONEPASS @@ -97,6 +182,14 @@ int sqlite3WhereOkOnePass(WhereInfo *pWInfo, int *aiCur){ return pWInfo->eOnePass; } +/* +** Return TRUE if the WHERE loop uses the OP_DeferredSeek opcode to move +** the data cursor to the row selected by the index cursor. +*/ +int sqlite3WhereUsesDeferredSeek(WhereInfo *pWInfo){ + return pWInfo->bDeferredSeek; +} + /* ** Move the content of pSrc into pDest */ @@ -152,7 +245,12 @@ static int whereOrInsert( Bitmask sqlite3WhereGetMask(WhereMaskSet *pMaskSet, int iCursor){ int i; assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 ); - for(i=0; i<pMaskSet->n; i++){ + assert( pMaskSet->n>0 || pMaskSet->ix[0]<0 ); + assert( iCursor>=-1 ); + if( pMaskSet->ix[0]==iCursor ){ + return 1; + } + for(i=1; i<pMaskSet->n; i++){ if( pMaskSet->ix[i]==iCursor ){ return MASKBIT(i); } @@ -160,6 +258,30 @@ Bitmask sqlite3WhereGetMask(WhereMaskSet *pMaskSet, int iCursor){ return 0; } +/* Allocate memory that is automatically freed when pWInfo is freed. +*/ +void *sqlite3WhereMalloc(WhereInfo *pWInfo, u64 nByte){ + WhereMemBlock *pBlock; + pBlock = sqlite3DbMallocRawNN(pWInfo->pParse->db, nByte+sizeof(*pBlock)); + if( pBlock ){ + pBlock->pNext = pWInfo->pMemToFree; + pBlock->sz = nByte; + pWInfo->pMemToFree = pBlock; + pBlock++; + } + return (void*)pBlock; +} +void *sqlite3WhereRealloc(WhereInfo *pWInfo, void *pOld, u64 nByte){ + void *pNew = sqlite3WhereMalloc(pWInfo, nByte); + if( pNew && pOld ){ + WhereMemBlock *pOldBlk = (WhereMemBlock*)pOld; + pOldBlk--; + assert( pOldBlk->sz<nByte ); + memcpy(pNew, pOld, pOldBlk->sz); + } + return pNew; +} + /* ** Create a new mask for cursor iCursor. ** @@ -173,6 +295,54 @@ static void createMask(WhereMaskSet *pMaskSet, int iCursor){ pMaskSet->ix[pMaskSet->n++] = iCursor; } +/* +** If the right-hand branch of the expression is a TK_COLUMN, then return +** a pointer to the right-hand branch. Otherwise, return NULL. +*/ +static Expr *whereRightSubexprIsColumn(Expr *p){ + p = sqlite3ExprSkipCollateAndLikely(p->pRight); + if( ALWAYS(p!=0) && p->op==TK_COLUMN && !ExprHasProperty(p, EP_FixedCol) ){ + return p; + } + return 0; +} + +/* +** Term pTerm is guaranteed to be a WO_IN term. It may be a component term +** of a vector IN expression of the form "(x, y, ...) IN (SELECT ...)". +** This function checks to see if the term is compatible with an index +** column with affinity idxaff (one of the SQLITE_AFF_XYZ values). If so, +** it returns a pointer to the name of the collation sequence (e.g. "BINARY" +** or "NOCASE") used by the comparison in pTerm. If it is not compatible +** with affinity idxaff, NULL is returned. +*/ +static SQLITE_NOINLINE const char *indexInAffinityOk( + Parse *pParse, + WhereTerm *pTerm, + u8 idxaff +){ + Expr *pX = pTerm->pExpr; + Expr inexpr; + + assert( pTerm->eOperator & WO_IN ); + + if( sqlite3ExprIsVector(pX->pLeft) ){ + int iField = pTerm->u.x.iField - 1; + inexpr.flags = 0; + inexpr.op = TK_EQ; + inexpr.pLeft = pX->pLeft->x.pList->a[iField].pExpr; + assert( ExprUseXSelect(pX) ); + inexpr.pRight = pX->x.pSelect->pEList->a[iField].pExpr; + pX = &inexpr; + } + + if( sqlite3IndexAffinityOk(pX, idxaff) ){ + CollSeq *pRet = sqlite3ExprCompareCollSeq(pParse, pX); + return pRet ? pRet->zName : sqlite3StrBINARY; + } + return 0; +} + /* ** Advance to the next WhereTerm that matches according to the criteria ** established when the pScan object was initialized by whereScanInit(). @@ -186,21 +356,26 @@ static WhereTerm *whereScanNext(WhereScan *pScan){ WhereTerm *pTerm; /* The term being tested */ int k = pScan->k; /* Where to start scanning */ - while( pScan->iEquiv<=pScan->nEquiv ){ - iCur = pScan->aiCur[pScan->iEquiv-1]; + assert( pScan->iEquiv<=pScan->nEquiv ); + pWC = pScan->pWC; + while(1){ iColumn = pScan->aiColumn[pScan->iEquiv-1]; - if( iColumn==XN_EXPR && pScan->pIdxExpr==0 ) return 0; - while( (pWC = pScan->pWC)!=0 ){ + iCur = pScan->aiCur[pScan->iEquiv-1]; + assert( pWC!=0 ); + assert( iCur>=0 ); + do{ for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){ + assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 || pTerm->leftCursor<0 ); if( pTerm->leftCursor==iCur - && pTerm->u.leftColumn==iColumn + && pTerm->u.x.leftColumn==iColumn && (iColumn!=XN_EXPR - || sqlite3ExprCompare(pTerm->pExpr->pLeft,pScan->pIdxExpr,iCur)==0) - && (pScan->iEquiv<=1 || !ExprHasProperty(pTerm->pExpr, EP_FromJoin)) + || sqlite3ExprCompareSkip(pTerm->pExpr->pLeft, + pScan->pIdxExpr,iCur)==0) + && (pScan->iEquiv<=1 || !ExprHasProperty(pTerm->pExpr, EP_OuterON)) ){ if( (pTerm->eOperator & WO_EQUIV)!=0 && pScan->nEquiv<ArraySize(pScan->aiCur) - && (pX = sqlite3ExprSkipCollate(pTerm->pExpr->pRight))->op==TK_COLUMN + && (pX = whereRightSubexprIsColumn(pTerm->pExpr))!=0 ){ int j; for(j=0; j<pScan->nEquiv; j++){ @@ -218,50 +393,86 @@ static WhereTerm *whereScanNext(WhereScan *pScan){ if( (pTerm->eOperator & pScan->opMask)!=0 ){ /* Verify the affinity and collating sequence match */ if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){ - CollSeq *pColl; + const char *zCollName; Parse *pParse = pWC->pWInfo->pParse; pX = pTerm->pExpr; - if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){ - continue; + + if( (pTerm->eOperator & WO_IN) ){ + zCollName = indexInAffinityOk(pParse, pTerm, pScan->idxaff); + if( !zCollName ) continue; + }else{ + CollSeq *pColl; + if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){ + continue; + } + assert(pX->pLeft); + pColl = sqlite3ExprCompareCollSeq(pParse, pX); + zCollName = pColl ? pColl->zName : sqlite3StrBINARY; } - assert(pX->pLeft); - pColl = sqlite3BinaryCompareCollSeq(pParse, - pX->pLeft, pX->pRight); - if( pColl==0 ) pColl = pParse->db->pDfltColl; - if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){ + + if( sqlite3StrICmp(zCollName, pScan->zCollName) ){ continue; } } if( (pTerm->eOperator & (WO_EQ|WO_IS))!=0 - && (pX = pTerm->pExpr->pRight)->op==TK_COLUMN + && (pX = pTerm->pExpr->pRight, ALWAYS(pX!=0)) + && pX->op==TK_COLUMN && pX->iTable==pScan->aiCur[0] && pX->iColumn==pScan->aiColumn[0] ){ testcase( pTerm->eOperator & WO_IS ); continue; } + pScan->pWC = pWC; pScan->k = k+1; +#ifdef WHERETRACE_ENABLED + if( (sqlite3WhereTrace & 0x20000)!=0 && pScan->nEquiv>1 ){ + int ii; + sqlite3DebugPrintf("EQUIVALENT TO {%d:%d} (due to TERM-%d):", + pScan->aiCur[0], pScan->aiColumn[0], pTerm->iTerm); + for(ii=1; ii<pScan->nEquiv; ii++){ + sqlite3DebugPrintf(" {%d:%d}", + pScan->aiCur[ii], pScan->aiColumn[ii]); + } + sqlite3DebugPrintf("\n"); + } +#endif return pTerm; } } } - pScan->pWC = pScan->pWC->pOuter; + pWC = pWC->pOuter; k = 0; - } - pScan->pWC = pScan->pOrigWC; + }while( pWC!=0 ); + if( pScan->iEquiv>=pScan->nEquiv ) break; + pWC = pScan->pOrigWC; k = 0; pScan->iEquiv++; } return 0; } +/* +** This is whereScanInit() for the case of an index on an expression. +** It is factored out into a separate tail-recursion subroutine so that +** the normal whereScanInit() routine, which is a high-runner, does not +** need to push registers onto the stack as part of its prologue. +*/ +static SQLITE_NOINLINE WhereTerm *whereScanInitIndexExpr(WhereScan *pScan){ + pScan->idxaff = sqlite3ExprAffinity(pScan->pIdxExpr); + return whereScanNext(pScan); +} + /* ** Initialize a WHERE clause scanner object. Return a pointer to the ** first match. Return NULL if there are no matches. ** ** The scanner will be searching the WHERE clause pWC. It will look ** for terms of the form "X <op> <expr>" where X is column iColumn of table -** iCur. The <op> must be one of the operators described by opMask. +** iCur. Or if pIdx!=0 then X is column iColumn of index pIdx. pIdx +** must be one of the indexes of table iCur. +** +** The <op> must be one of the operators described by opMask. ** ** If the search is for X and the WHERE clause contains terms of the ** form X=Y then this routine might also return terms of the form @@ -279,40 +490,45 @@ static WhereTerm *whereScanInit( u32 opMask, /* Operator(s) to scan for */ Index *pIdx /* Must be compatible with this index */ ){ - int j = 0; - - /* memset(pScan, 0, sizeof(*pScan)); */ pScan->pOrigWC = pWC; pScan->pWC = pWC; pScan->pIdxExpr = 0; - if( pIdx ){ - j = iColumn; - iColumn = pIdx->aiColumn[j]; - if( iColumn==XN_EXPR ) pScan->pIdxExpr = pIdx->aColExpr->a[j].pExpr; - } - if( pIdx && iColumn>=0 ){ - pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity; - pScan->zCollName = pIdx->azColl[j]; - }else{ - pScan->idxaff = 0; - pScan->zCollName = 0; - } + pScan->idxaff = 0; + pScan->zCollName = 0; pScan->opMask = opMask; pScan->k = 0; pScan->aiCur[0] = iCur; - pScan->aiColumn[0] = iColumn; pScan->nEquiv = 1; pScan->iEquiv = 1; + if( pIdx ){ + int j = iColumn; + iColumn = pIdx->aiColumn[j]; + if( iColumn==pIdx->pTable->iPKey ){ + iColumn = XN_ROWID; + }else if( iColumn>=0 ){ + pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity; + pScan->zCollName = pIdx->azColl[j]; + }else if( iColumn==XN_EXPR ){ + pScan->pIdxExpr = pIdx->aColExpr->a[j].pExpr; + pScan->zCollName = pIdx->azColl[j]; + pScan->aiColumn[0] = XN_EXPR; + return whereScanInitIndexExpr(pScan); + } + }else if( iColumn==XN_EXPR ){ + return 0; + } + pScan->aiColumn[0] = iColumn; return whereScanNext(pScan); } /* ** Search for a term in the WHERE clause that is of the form "X <op> <expr>" -** where X is a reference to the iColumn of table iCur and <op> is one of -** the WO_xx operator codes specified by the op parameter. -** Return a pointer to the term. Return 0 if not found. +** where X is a reference to the iColumn of table iCur or of index pIdx +** if pIdx!=0 and <op> is one of the WO_xx operator codes specified by +** the op parameter. Return a pointer to the term. Return 0 if not found. ** -** If pIdx!=0 then search for terms matching the iColumn-th column of pIdx +** If pIdx!=0 then it must be one of the indexes of table iCur. +** Search for terms matching the iColumn-th column of pIdx ** rather than the iColumn-th column of table iCur. ** ** The term returned might by Y=<expr> if there is another constraint in @@ -375,13 +591,14 @@ static int findIndexCol( const char *zColl = pIdx->azColl[iCol]; for(i=0; i<pList->nExpr; i++){ - Expr *p = sqlite3ExprSkipCollate(pList->a[i].pExpr); - if( p->op==TK_COLUMN + Expr *p = sqlite3ExprSkipCollateAndLikely(pList->a[i].pExpr); + if( ALWAYS(p!=0) + && (p->op==TK_COLUMN || p->op==TK_AGG_COLUMN) && p->iColumn==pIdx->aiColumn[iCol] && p->iTable==iBase ){ - CollSeq *pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr); - if( pColl && 0==sqlite3StrICmp(pColl->zName, zColl) ){ + CollSeq *pColl = sqlite3ExprNNCollSeq(pParse, pList->a[i].pExpr); + if( 0==sqlite3StrICmp(pColl->zName, zColl) ){ return i; } } @@ -424,23 +641,25 @@ static int isDistinctRedundant( ){ Table *pTab; Index *pIdx; - int i; + int i; int iBase; /* If there is more than one table or sub-select in the FROM clause of - ** this query, then it will not be possible to show that the DISTINCT + ** this query, then it will not be possible to show that the DISTINCT ** clause is redundant. */ if( pTabList->nSrc!=1 ) return 0; iBase = pTabList->a[0].iCursor; - pTab = pTabList->a[0].pTab; + pTab = pTabList->a[0].pSTab; - /* If any of the expressions is an IPK column on table iBase, then return + /* If any of the expressions is an IPK column on table iBase, then return ** true. Note: The (p->iTable==iBase) part of this test may be false if the ** current SELECT is a correlated sub-query. */ for(i=0; i<pDistinct->nExpr; i++){ - Expr *p = sqlite3ExprSkipCollate(pDistinct->a[i].pExpr); - if( p->op==TK_COLUMN && p->iTable==iBase && p->iColumn<0 ) return 1; + Expr *p = sqlite3ExprSkipCollateAndLikely(pDistinct->a[i].pExpr); + if( NEVER(p==0) ) continue; + if( p->op!=TK_COLUMN && p->op!=TK_AGG_COLUMN ) continue; + if( p->iTable==iBase && p->iColumn<0 ) return 1; } /* Loop through all indices on the table, checking each to see if it makes @@ -458,6 +677,7 @@ static int isDistinctRedundant( */ for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ if( !IsUniqueIndex(pIdx) ) continue; + if( pIdx->pPartIdxWhere ) continue; for(i=0; i<pIdx->nKeyCol; i++){ if( 0==sqlite3WhereFindTerm(pWC, iBase, i, ~(Bitmask)0, WO_EQ, pIdx) ){ if( findIndexCol(pParse, pDistinct, iBase, pIdx, i)<0 ) break; @@ -485,41 +705,58 @@ static LogEst estLog(LogEst N){ ** Convert OP_Column opcodes to OP_Copy in previously generated code. ** ** This routine runs over generated VDBE code and translates OP_Column -** opcodes into OP_Copy when the table is being accessed via co-routine +** opcodes into OP_Copy when the table is being accessed via co-routine ** instead of via table lookup. ** -** If the bIncrRowid parameter is 0, then any OP_Rowid instructions on -** cursor iTabCur are transformed into OP_Null. Or, if bIncrRowid is non-zero, -** then each OP_Rowid is transformed into an instruction to increment the -** value stored in its output register. +** If the iAutoidxCur is not zero, then any OP_Rowid instructions on +** cursor iTabCur are transformed into OP_Sequence opcode for the +** iAutoidxCur cursor, in order to generate unique rowids for the +** automatic index being generated. */ static void translateColumnToCopy( - Vdbe *v, /* The VDBE containing code to translate */ + Parse *pParse, /* Parsing context */ int iStart, /* Translate from this opcode to the end */ int iTabCur, /* OP_Column/OP_Rowid references to this table */ int iRegister, /* The first column is in this register */ - int bIncrRowid /* If non-zero, transform OP_rowid to OP_AddImm(1) */ + int iAutoidxCur /* If non-zero, cursor of autoindex being generated */ ){ + Vdbe *v = pParse->pVdbe; VdbeOp *pOp = sqlite3VdbeGetOp(v, iStart); int iEnd = sqlite3VdbeCurrentAddr(v); + if( pParse->db->mallocFailed ) return; +#ifdef SQLITE_DEBUG + if( pParse->db->flags & SQLITE_VdbeAddopTrace ){ + printf("CHECKING for column-to-copy on cursor %d for %d..%d\n", + iTabCur, iStart, iEnd); + } +#endif for(; iStart<iEnd; iStart++, pOp++){ if( pOp->p1!=iTabCur ) continue; if( pOp->opcode==OP_Column ){ +#ifdef SQLITE_DEBUG + if( pParse->db->flags & SQLITE_VdbeAddopTrace ){ + printf("TRANSLATE OP_Column to OP_Copy at %d\n", iStart); + } +#endif pOp->opcode = OP_Copy; pOp->p1 = pOp->p2 + iRegister; pOp->p2 = pOp->p3; pOp->p3 = 0; + pOp->p5 = 2; /* Cause the MEM_Subtype flag to be cleared */ }else if( pOp->opcode==OP_Rowid ){ - if( bIncrRowid ){ - /* Increment the value stored in the P2 operand of the OP_Rowid. */ - pOp->opcode = OP_AddImm; - pOp->p1 = pOp->p2; - pOp->p2 = 1; - }else{ +#ifdef SQLITE_DEBUG + if( pParse->db->flags & SQLITE_VdbeAddopTrace ){ + printf("TRANSLATE OP_Rowid to OP_Sequence at %d\n", iStart); + } +#endif + pOp->opcode = OP_Sequence; + pOp->p1 = iAutoidxCur; +#ifdef SQLITE_ALLOW_ROWID_IN_VIEW + if( iAutoidxCur==0 ){ pOp->opcode = OP_Null; - pOp->p1 = 0; pOp->p3 = 0; } +#endif } } } @@ -531,16 +768,22 @@ static void translateColumnToCopy( ** are no-ops. */ #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(WHERETRACE_ENABLED) -static void TRACE_IDX_INPUTS(sqlite3_index_info *p){ +static void whereTraceIndexInfoInputs( + sqlite3_index_info *p, /* The IndexInfo object */ + Table *pTab /* The TABLE that is the virtual table */ +){ int i; - if( !sqlite3WhereTrace ) return; + if( (sqlite3WhereTrace & 0x10)==0 ) return; + sqlite3DebugPrintf("sqlite3_index_info inputs for %s:\n", pTab->zName); for(i=0; i<p->nConstraint; i++){ - sqlite3DebugPrintf(" constraint[%d]: col=%d termid=%d op=%d usabled=%d\n", + sqlite3DebugPrintf( + " constraint[%d]: col=%d termid=%d op=%d usabled=%d collseq=%s\n", i, p->aConstraint[i].iColumn, p->aConstraint[i].iTermOffset, p->aConstraint[i].op, - p->aConstraint[i].usable); + p->aConstraint[i].usable, + sqlite3_vtab_collation(p,i)); } for(i=0; i<p->nOrderBy; i++){ sqlite3DebugPrintf(" orderby[%d]: col=%d desc=%d\n", @@ -549,9 +792,13 @@ static void TRACE_IDX_INPUTS(sqlite3_index_info *p){ p->aOrderBy[i].desc); } } -static void TRACE_IDX_OUTPUTS(sqlite3_index_info *p){ +static void whereTraceIndexInfoOutputs( + sqlite3_index_info *p, /* The IndexInfo object */ + Table *pTab /* The TABLE that is the virtual table */ +){ int i; - if( !sqlite3WhereTrace ) return; + if( (sqlite3WhereTrace & 0x10)==0 ) return; + sqlite3DebugPrintf("sqlite3_index_info outputs for %s:\n", pTab->zName); for(i=0; i<p->nConstraint; i++){ sqlite3DebugPrintf(" usage[%d]: argvIdx=%d omit=%d\n", i, @@ -565,10 +812,86 @@ static void TRACE_IDX_OUTPUTS(sqlite3_index_info *p){ sqlite3DebugPrintf(" estimatedRows=%lld\n", p->estimatedRows); } #else -#define TRACE_IDX_INPUTS(A) -#define TRACE_IDX_OUTPUTS(A) +#define whereTraceIndexInfoInputs(A,B) +#define whereTraceIndexInfoOutputs(A,B) #endif +/* +** We know that pSrc is an operand of an outer join. Return true if +** pTerm is a constraint that is compatible with that join. +** +** pTerm must be EP_OuterON if pSrc is the right operand of an +** outer join. pTerm can be either EP_OuterON or EP_InnerON if pSrc +** is the left operand of a RIGHT join. +** +** See https://sqlite.org/forum/forumpost/206d99a16dd9212f +** for an example of a WHERE clause constraints that may not be used on +** the right table of a RIGHT JOIN because the constraint implies a +** not-NULL condition on the left table of the RIGHT JOIN. +*/ +static int constraintCompatibleWithOuterJoin( + const WhereTerm *pTerm, /* WHERE clause term to check */ + const SrcItem *pSrc /* Table we are trying to access */ +){ + assert( (pSrc->fg.jointype&(JT_LEFT|JT_LTORJ|JT_RIGHT))!=0 ); /* By caller */ + testcase( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))==JT_LEFT ); + testcase( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))==JT_LTORJ ); + testcase( ExprHasProperty(pTerm->pExpr, EP_OuterON) ) + testcase( ExprHasProperty(pTerm->pExpr, EP_InnerON) ); + if( !ExprHasProperty(pTerm->pExpr, EP_OuterON|EP_InnerON) + || pTerm->pExpr->w.iJoin != pSrc->iCursor + ){ + return 0; + } + if( (pSrc->fg.jointype & (JT_LEFT|JT_RIGHT))!=0 + && NEVER(ExprHasProperty(pTerm->pExpr, EP_InnerON)) + ){ + return 0; + } + return 1; +} + +#ifndef SQLITE_OMIT_AUTOMATIC_INDEX +/* +** Return true if column iCol of table pTab seem like it might be a +** good column to use as part of a query-time index. +** +** Current algorithm (subject to improvement!): +** +** 1. If iCol is already the left-most column of some other index, +** then return false. +** +** 2. If iCol is part of an existing index that has an aiRowLogEst of +** more than 20, then return false. +** +** 3. If no disqualifying conditions above are found, return true. +** +** 2025-01-03: I experimented with a new rule that returns false if the +** the datatype of the column is "BOOLEAN". This did not improve +** performance on any queries at hand, but it did burn CPU cycles, so the +** idea was not committed. +*/ +static SQLITE_NOINLINE int columnIsGoodIndexCandidate( + const Table *pTab, + int iCol +){ + const Index *pIdx; + for(pIdx = pTab->pIndex; pIdx!=0; pIdx=pIdx->pNext){ + int j; + for(j=0; j<pIdx->nKeyCol; j++){ + if( pIdx->aiColumn[j]==iCol ){ + if( j==0 ) return 0; + if( pIdx->hasStat1 && pIdx->aiRowLogEst[j+1]>20 ) return 0; + break; + } + } + } + return 1; +} +#endif /* SQLITE_OMIT_AUTOMATIC_INDEX */ + + + #ifndef SQLITE_OMIT_AUTOMATIC_INDEX /* ** Return TRUE if the WHERE clause term pTerm is of a form where it @@ -576,34 +899,94 @@ static void TRACE_IDX_OUTPUTS(sqlite3_index_info *p){ ** index existed. */ static int termCanDriveIndex( - WhereTerm *pTerm, /* WHERE clause term to check */ - struct SrcList_item *pSrc, /* Table we are trying to access */ - Bitmask notReady /* Tables in outer loops of the join */ + const WhereTerm *pTerm, /* WHERE clause term to check */ + const SrcItem *pSrc, /* Table we are trying to access */ + const Bitmask notReady /* Tables in outer loops of the join */ ){ char aff; + int leftCol; + if( pTerm->leftCursor!=pSrc->iCursor ) return 0; if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) return 0; + assert( (pSrc->fg.jointype & JT_RIGHT)==0 ); + if( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))!=0 + && !constraintCompatibleWithOuterJoin(pTerm,pSrc) + ){ + return 0; /* See https://sqlite.org/forum/forumpost/51e6959f61 */ + } if( (pTerm->prereqRight & notReady)!=0 ) return 0; - if( pTerm->u.leftColumn<0 ) return 0; - aff = pSrc->pTab->aCol[pTerm->u.leftColumn].affinity; + assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 ); + leftCol = pTerm->u.x.leftColumn; + if( leftCol<0 ) return 0; + aff = pSrc->pSTab->aCol[leftCol].affinity; if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0; testcase( pTerm->pExpr->op==TK_IS ); - return 1; + return columnIsGoodIndexCandidate(pSrc->pSTab, leftCol); } #endif #ifndef SQLITE_OMIT_AUTOMATIC_INDEX + +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS +/* +** Argument pIdx represents an automatic index that the current statement +** will create and populate. Add an OP_Explain with text of the form: +** +** CREATE AUTOMATIC INDEX ON <table>(<cols>) [WHERE <expr>] +** +** This is only required if sqlite3_stmt_scanstatus() is enabled, to +** associate an SQLITE_SCANSTAT_NCYCLE and SQLITE_SCANSTAT_NLOOP +** values with. In order to avoid breaking legacy code and test cases, +** the OP_Explain is not added if this is an EXPLAIN QUERY PLAN command. +*/ +static void explainAutomaticIndex( + Parse *pParse, + Index *pIdx, /* Automatic index to explain */ + int bPartial, /* True if pIdx is a partial index */ + int *pAddrExplain /* OUT: Address of OP_Explain */ +){ + if( IS_STMT_SCANSTATUS(pParse->db) && pParse->explain!=2 ){ + Table *pTab = pIdx->pTable; + const char *zSep = ""; + char *zText = 0; + int ii = 0; + sqlite3_str *pStr = sqlite3_str_new(pParse->db); + sqlite3_str_appendf(pStr,"CREATE AUTOMATIC INDEX ON %s(", pTab->zName); + assert( pIdx->nColumn>1 ); + assert( pIdx->aiColumn[pIdx->nColumn-1]==XN_ROWID || !HasRowid(pTab) ); + for(ii=0; ii<(pIdx->nColumn-1); ii++){ + const char *zName = 0; + int iCol = pIdx->aiColumn[ii]; + + zName = pTab->aCol[iCol].zCnName; + sqlite3_str_appendf(pStr, "%s%s", zSep, zName); + zSep = ", "; + } + zText = sqlite3_str_finish(pStr); + if( zText==0 ){ + sqlite3OomFault(pParse->db); + }else{ + *pAddrExplain = sqlite3VdbeExplain( + pParse, 0, "%s)%s", zText, (bPartial ? " WHERE <expr>" : "") + ); + sqlite3_free(zText); + } + } +} +#else +# define explainAutomaticIndex(a,b,c,d) +#endif + /* ** Generate code to construct the Index object for an automatic index ** and to set up the WhereLevel object pLevel so that the code generator ** makes use of the automatic index. */ -static void constructAutomaticIndex( +static SQLITE_NOINLINE void constructAutomaticIndex( Parse *pParse, /* The parsing context */ WhereClause *pWC, /* The WHERE clause */ - struct SrcList_item *pSrc, /* The FROM clause term to get the next index */ - Bitmask notReady, /* Mask of cursors that are not available */ + const Bitmask notReady, /* Mask of cursors that are not available */ WhereLevel *pLevel /* Write new index here */ ){ int nKeyCol; /* Number of columns in the constructed index */ @@ -623,47 +1006,56 @@ static void constructAutomaticIndex( char *zNotUsed; /* Extra space on the end of pIdx */ Bitmask idxCols; /* Bitmap of columns used for indexing */ Bitmask extraCols; /* Bitmap of additional columns */ - u8 sentWarning = 0; /* True if a warnning has been issued */ + u8 sentWarning = 0; /* True if a warning has been issued */ + u8 useBloomFilter = 0; /* True to also add a Bloom filter */ Expr *pPartial = 0; /* Partial Index Expression */ int iContinue = 0; /* Jump here to skip excluded rows */ - struct SrcList_item *pTabItem; /* FROM clause term being indexed */ + SrcList *pTabList; /* The complete FROM clause */ + SrcItem *pSrc; /* The FROM clause term to get the next index */ int addrCounter = 0; /* Address where integer counter is initialized */ int regBase; /* Array of registers where record is assembled */ +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS + int addrExp = 0; /* Address of OP_Explain */ +#endif /* Generate code to skip over the creation and initialization of the ** transient index on 2nd and subsequent iterations of the loop. */ v = pParse->pVdbe; assert( v!=0 ); - addrInit = sqlite3CodeOnce(pParse); VdbeCoverage(v); + addrInit = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v); /* Count the number of columns that will be added to the index ** and used to match WHERE clause constraints */ nKeyCol = 0; - pTable = pSrc->pTab; + pTabList = pWC->pWInfo->pTabList; + pSrc = &pTabList->a[pLevel->iFrom]; + pTable = pSrc->pSTab; pWCEnd = &pWC->a[pWC->nTerm]; pLoop = pLevel->pWLoop; idxCols = 0; for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){ Expr *pExpr = pTerm->pExpr; - assert( !ExprHasProperty(pExpr, EP_FromJoin) /* prereq always non-zero */ - || pExpr->iRightJoinTable!=pSrc->iCursor /* for the right-hand */ - || pLoop->prereq!=0 ); /* table of a LEFT JOIN */ - if( pLoop->prereq==0 - && (pTerm->wtFlags & TERM_VIRTUAL)==0 - && !ExprHasProperty(pExpr, EP_FromJoin) - && sqlite3ExprIsTableConstant(pExpr, pSrc->iCursor) ){ - pPartial = sqlite3ExprAnd(pParse->db, pPartial, + /* Make the automatic index a partial index if there are terms in the + ** WHERE clause (or the ON clause of a LEFT join) that constrain which + ** rows of the target table (pSrc) that can be used. */ + if( (pTerm->wtFlags & TERM_VIRTUAL)==0 + && sqlite3ExprIsSingleTableConstraint(pExpr, pTabList, pLevel->iFrom, 0) + ){ + pPartial = sqlite3ExprAnd(pParse, pPartial, sqlite3ExprDup(pParse->db, pExpr, 0)); } if( termCanDriveIndex(pTerm, pSrc, notReady) ){ - int iCol = pTerm->u.leftColumn; - Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol); + int iCol; + Bitmask cMask; + assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 ); + iCol = pTerm->u.x.leftColumn; + cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol); testcase( iCol==BMS ); testcase( iCol==BMS-1 ); if( !sentWarning ){ sqlite3_log(SQLITE_WARNING_AUTOINDEX, "automatic index on %s(%s)", pTable->zName, - pTable->aCol[iCol].zName); + pTable->aCol[iCol].zCnName); sentWarning = 1; } if( (idxCols & cMask)==0 ){ @@ -675,7 +1067,7 @@ static void constructAutomaticIndex( } } } - assert( nKeyCol>0 ); + assert( nKeyCol>0 || pParse->db->mallocFailed ); pLoop->u.btree.nEq = pLoop->nLTerm = nKeyCol; pLoop->wsFlags = WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WHERE_INDEXED | WHERE_AUTO_INDEX; @@ -688,7 +1080,24 @@ static void constructAutomaticIndex( ** original table changes and the index and table cannot both be used ** if they go out of sync. */ - extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1)); + if( IsView(pTable) ){ + extraCols = ALLBITS & ~idxCols; + }else{ + extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1)); + } + if( !HasRowid(pTable) ){ + /* For WITHOUT ROWID tables, ensure that all PRIMARY KEY columns are + ** either in the idxCols mask or in the extraCols mask */ + for(i=0; i<pTable->nCol; i++){ + if( (pTable->aCol[i].colFlags & COLFLAG_PRIMKEY)==0 ) continue; + if( i>=BMS-1 ){ + extraCols |= MASKBIT(BMS-1); + break; + } + if( idxCols & MASKBIT(i) ) continue; + extraCols |= MASKBIT(i); + } + } mxBitCol = MIN(BMS-1,pTable->nCol); testcase( pTable->nCol==BMS-1 ); testcase( pTable->nCol==BMS-2 ); @@ -700,7 +1109,10 @@ static void constructAutomaticIndex( } /* Construct the Index object to describe this index */ - pIdx = sqlite3AllocateIndexObject(pParse->db, nKeyCol+1, 0, &zNotUsed); + assert( nKeyCol <= pTable->nCol + MAX(0, pTable->nCol - BMS + 1) ); + /* ^-- This guarantees that the number of index columns will fit in the u16 */ + pIdx = sqlite3AllocateIndexObject(pParse->db, nKeyCol+HasRowid(pTable), + 0, &zNotUsed); if( pIdx==0 ) goto end_auto_index_create; pLoop->u.btree.pIndex = pIdx; pIdx->zName = "auto-index"; @@ -709,17 +1121,31 @@ static void constructAutomaticIndex( idxCols = 0; for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){ if( termCanDriveIndex(pTerm, pSrc, notReady) ){ - int iCol = pTerm->u.leftColumn; - Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol); + int iCol; + Bitmask cMask; + assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 ); + iCol = pTerm->u.x.leftColumn; + cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol); testcase( iCol==BMS-1 ); testcase( iCol==BMS ); if( (idxCols & cMask)==0 ){ Expr *pX = pTerm->pExpr; idxCols |= cMask; - pIdx->aiColumn[n] = pTerm->u.leftColumn; - pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight); + pIdx->aiColumn[n] = pTerm->u.x.leftColumn; + pColl = sqlite3ExprCompareCollSeq(pParse, pX); + assert( pColl!=0 || pParse->nErr>0 ); /* TH3 collate01.800 */ pIdx->azColl[n] = pColl ? pColl->zName : sqlite3StrBINARY; n++; + if( ALWAYS(pX->pLeft!=0) + && sqlite3ExprAffinity(pX->pLeft)!=SQLITE_AFF_TEXT + ){ + /* TUNING: only use a Bloom filter on an automatic index + ** if one or more key columns has the ability to hold numeric + ** values, since strings all have the same hash in the Bloom + ** filter implementation and hence a Bloom filter on a text column + ** is not usually helpful. */ + useBloomFilter = 1; + } } } } @@ -742,31 +1168,45 @@ static void constructAutomaticIndex( } } assert( n==nKeyCol ); - pIdx->aiColumn[n] = XN_ROWID; - pIdx->azColl[n] = sqlite3StrBINARY; + if( HasRowid(pTable) ){ + pIdx->aiColumn[n] = XN_ROWID; + pIdx->azColl[n] = sqlite3StrBINARY; + } /* Create the automatic index */ + explainAutomaticIndex(pParse, pIdx, pPartial!=0, &addrExp); assert( pLevel->iIdxCur>=0 ); pLevel->iIdxCur = pParse->nTab++; sqlite3VdbeAddOp2(v, OP_OpenAutoindex, pLevel->iIdxCur, nKeyCol+1); sqlite3VdbeSetP4KeyInfo(pParse, pIdx); VdbeComment((v, "for %s", pTable->zName)); + if( OptimizationEnabled(pParse->db, SQLITE_BloomFilter) && useBloomFilter ){ + sqlite3WhereExplainBloomFilter(pParse, pWC->pWInfo, pLevel); + pLevel->regFilter = ++pParse->nMem; + sqlite3VdbeAddOp2(v, OP_Blob, 10000, pLevel->regFilter); + } /* Fill the automatic index with content */ - sqlite3ExprCachePush(pParse); - pTabItem = &pWC->pWInfo->pTabList->a[pLevel->iFrom]; - if( pTabItem->fg.viaCoroutine ){ - int regYield = pTabItem->regReturn; + assert( pSrc == &pWC->pWInfo->pTabList->a[pLevel->iFrom] ); + if( pSrc->fg.viaCoroutine ){ + int regYield; + Subquery *pSubq; + assert( pSrc->fg.isSubquery ); + pSubq = pSrc->u4.pSubq; + assert( pSubq!=0 ); + regYield = pSubq->regReturn; addrCounter = sqlite3VdbeAddOp2(v, OP_Integer, 0, 0); - sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub); + sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pSubq->addrFillSub); addrTop = sqlite3VdbeAddOp1(v, OP_Yield, regYield); VdbeCoverage(v); - VdbeComment((v, "next row of \"%s\"", pTabItem->pTab->zName)); + VdbeComment((v, "next row of %s", pSrc->pSTab->zName)); }else{ - addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); VdbeCoverage(v); + assert( pLevel->addrHalt ); + addrTop = sqlite3VdbeAddOp2(v, OP_Rewind,pLevel->iTabCur,pLevel->addrHalt); + VdbeCoverage(v); } if( pPartial ){ - iContinue = sqlite3VdbeMakeLabel(v); + iContinue = sqlite3VdbeMakeLabel(pParse); sqlite3ExprIfFalse(pParse, pPartial, iContinue, SQLITE_JUMPIFNULL); pLoop->wsFlags |= WHERE_PARTIALIDX; } @@ -774,69 +1214,261 @@ static void constructAutomaticIndex( regBase = sqlite3GenerateIndexKey( pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0, 0, 0 ); + if( pLevel->regFilter ){ + sqlite3VdbeAddOp4Int(v, OP_FilterAdd, pLevel->regFilter, 0, + regBase, pLoop->u.btree.nEq); + } + sqlite3VdbeScanStatusCounters(v, addrExp, addrExp, sqlite3VdbeCurrentAddr(v)); sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord); sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); if( pPartial ) sqlite3VdbeResolveLabel(v, iContinue); - if( pTabItem->fg.viaCoroutine ){ + if( pSrc->fg.viaCoroutine ){ + assert( pSrc->fg.isSubquery && pSrc->u4.pSubq!=0 ); sqlite3VdbeChangeP2(v, addrCounter, regBase+n); - translateColumnToCopy(v, addrTop, pLevel->iTabCur, pTabItem->regResult, 1); + testcase( pParse->db->mallocFailed ); + assert( pLevel->iIdxCur>0 ); + translateColumnToCopy(pParse, addrTop, pLevel->iTabCur, + pSrc->u4.pSubq->regResult, pLevel->iIdxCur); sqlite3VdbeGoto(v, addrTop); - pTabItem->fg.viaCoroutine = 0; + pSrc->fg.viaCoroutine = 0; + sqlite3VdbeJumpHere(v, addrTop); }else{ sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); VdbeCoverage(v); + sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX); + if( (pSrc->fg.jointype & JT_LEFT)!=0 ){ + sqlite3VdbeJumpHere(v, addrTop); + } } - sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX); - sqlite3VdbeJumpHere(v, addrTop); sqlite3ReleaseTempReg(pParse, regRecord); - sqlite3ExprCachePop(pParse); - + /* Jump here when skipping the initialization */ sqlite3VdbeJumpHere(v, addrInit); + sqlite3VdbeScanStatusRange(v, addrExp, addrExp, -1); end_auto_index_create: sqlite3ExprDelete(pParse->db, pPartial); } #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */ +/* +** Generate bytecode that will initialize a Bloom filter that is appropriate +** for pLevel. +** +** If there are inner loops within pLevel that have the WHERE_BLOOMFILTER +** flag set, initialize a Bloomfilter for them as well. Except don't do +** this recursive initialization if the SQLITE_BloomPulldown optimization has +** been turned off. +** +** When the Bloom filter is initialized, the WHERE_BLOOMFILTER flag is cleared +** from the loop, but the regFilter value is set to a register that implements +** the Bloom filter. When regFilter is positive, the +** sqlite3WhereCodeOneLoopStart() will generate code to test the Bloom filter +** and skip the subsequence B-Tree seek if the Bloom filter indicates that +** no matching rows exist. +** +** This routine may only be called if it has previously been determined that +** the loop would benefit from a Bloom filter, and the WHERE_BLOOMFILTER bit +** is set. +*/ +static SQLITE_NOINLINE void sqlite3ConstructBloomFilter( + WhereInfo *pWInfo, /* The WHERE clause */ + int iLevel, /* Index in pWInfo->a[] that is pLevel */ + WhereLevel *pLevel, /* Make a Bloom filter for this FROM term */ + Bitmask notReady /* Loops that are not ready */ +){ + int addrOnce; /* Address of opening OP_Once */ + int addrTop; /* Address of OP_Rewind */ + int addrCont; /* Jump here to skip a row */ + const WhereTerm *pTerm; /* For looping over WHERE clause terms */ + const WhereTerm *pWCEnd; /* Last WHERE clause term */ + Parse *pParse = pWInfo->pParse; /* Parsing context */ + Vdbe *v = pParse->pVdbe; /* VDBE under construction */ + WhereLoop *pLoop = pLevel->pWLoop; /* The loop being coded */ + int iCur; /* Cursor for table getting the filter */ + IndexedExpr *saved_pIdxEpr; /* saved copy of Parse.pIdxEpr */ + IndexedExpr *saved_pIdxPartExpr; /* saved copy of Parse.pIdxPartExpr */ + + saved_pIdxEpr = pParse->pIdxEpr; + saved_pIdxPartExpr = pParse->pIdxPartExpr; + pParse->pIdxEpr = 0; + pParse->pIdxPartExpr = 0; + + assert( pLoop!=0 ); + assert( v!=0 ); + assert( pLoop->wsFlags & WHERE_BLOOMFILTER ); + assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 ); + + addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v); + do{ + const SrcList *pTabList; + const SrcItem *pItem; + const Table *pTab; + u64 sz; + int iSrc; + sqlite3WhereExplainBloomFilter(pParse, pWInfo, pLevel); + addrCont = sqlite3VdbeMakeLabel(pParse); + iCur = pLevel->iTabCur; + pLevel->regFilter = ++pParse->nMem; + + /* The Bloom filter is a Blob held in a register. Initialize it + ** to zero-filled blob of at least 80K bits, but maybe more if the + ** estimated size of the table is larger. We could actually + ** measure the size of the table at run-time using OP_Count with + ** P3==1 and use that value to initialize the blob. But that makes + ** testing complicated. By basing the blob size on the value in the + ** sqlite_stat1 table, testing is much easier. + */ + pTabList = pWInfo->pTabList; + iSrc = pLevel->iFrom; + pItem = &pTabList->a[iSrc]; + assert( pItem!=0 ); + pTab = pItem->pSTab; + assert( pTab!=0 ); + sz = sqlite3LogEstToInt(pTab->nRowLogEst); + if( sz<10000 ){ + sz = 10000; + }else if( sz>10000000 ){ + sz = 10000000; + } + sqlite3VdbeAddOp2(v, OP_Blob, (int)sz, pLevel->regFilter); + + addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, iCur); VdbeCoverage(v); + pWCEnd = &pWInfo->sWC.a[pWInfo->sWC.nTerm]; + for(pTerm=pWInfo->sWC.a; pTerm<pWCEnd; pTerm++){ + Expr *pExpr = pTerm->pExpr; + if( (pTerm->wtFlags & TERM_VIRTUAL)==0 + && sqlite3ExprIsSingleTableConstraint(pExpr, pTabList, iSrc, 0) + ){ + sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL); + } + } + if( pLoop->wsFlags & WHERE_IPK ){ + int r1 = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp2(v, OP_Rowid, iCur, r1); + sqlite3VdbeAddOp4Int(v, OP_FilterAdd, pLevel->regFilter, 0, r1, 1); + sqlite3ReleaseTempReg(pParse, r1); + }else{ + Index *pIdx = pLoop->u.btree.pIndex; + int n = pLoop->u.btree.nEq; + int r1 = sqlite3GetTempRange(pParse, n); + int jj; + for(jj=0; jj<n; jj++){ + assert( pIdx->pTable==pItem->pSTab ); + sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iCur, jj, r1+jj); + } + sqlite3VdbeAddOp4Int(v, OP_FilterAdd, pLevel->regFilter, 0, r1, n); + sqlite3ReleaseTempRange(pParse, r1, n); + } + sqlite3VdbeResolveLabel(v, addrCont); + sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); + VdbeCoverage(v); + sqlite3VdbeJumpHere(v, addrTop); + pLoop->wsFlags &= ~WHERE_BLOOMFILTER; + if( OptimizationDisabled(pParse->db, SQLITE_BloomPulldown) ) break; + while( ++iLevel < pWInfo->nLevel ){ + const SrcItem *pTabItem; + pLevel = &pWInfo->a[iLevel]; + pTabItem = &pWInfo->pTabList->a[pLevel->iFrom]; + if( pTabItem->fg.jointype & (JT_LEFT|JT_LTORJ) ) continue; + pLoop = pLevel->pWLoop; + if( NEVER(pLoop==0) ) continue; + if( pLoop->prereq & notReady ) continue; + if( (pLoop->wsFlags & (WHERE_BLOOMFILTER|WHERE_COLUMN_IN)) + ==WHERE_BLOOMFILTER + ){ + /* This is a candidate for bloom-filter pull-down (early evaluation). + ** The test that WHERE_COLUMN_IN is omitted is important, as we are + ** not able to do early evaluation of bloom filters that make use of + ** the IN operator */ + break; + } + } + }while( iLevel < pWInfo->nLevel ); + sqlite3VdbeJumpHere(v, addrOnce); + pParse->pIdxEpr = saved_pIdxEpr; + pParse->pIdxPartExpr = saved_pIdxPartExpr; +} + + #ifndef SQLITE_OMIT_VIRTUALTABLE /* -** Allocate and populate an sqlite3_index_info structure. It is the +** Return term iTerm of the WhereClause passed as the first argument. Terms +** are numbered from 0 upwards, starting with the terms in pWC->a[], then +** those in pWC->pOuter->a[] (if any), and so on. +*/ +static WhereTerm *termFromWhereClause(WhereClause *pWC, int iTerm){ + WhereClause *p; + for(p=pWC; p; p=p->pOuter){ + if( iTerm<p->nTerm ) return &p->a[iTerm]; + iTerm -= p->nTerm; + } + return 0; +} + +/* +** Allocate and populate an sqlite3_index_info structure. It is the ** responsibility of the caller to eventually release the structure -** by passing the pointer returned by this function to sqlite3_free(). +** by passing the pointer returned by this function to freeIndexInfo(). */ static sqlite3_index_info *allocateIndexInfo( - Parse *pParse, - WhereClause *pWC, + WhereInfo *pWInfo, /* The WHERE clause */ + WhereClause *pWC, /* The WHERE clause being analyzed */ Bitmask mUnusable, /* Ignore terms with these prereqs */ - struct SrcList_item *pSrc, - ExprList *pOrderBy + SrcItem *pSrc, /* The FROM clause term that is the vtab */ + u16 *pmNoOmit /* Mask of terms not to omit */ ){ int i, j; int nTerm; + Parse *pParse = pWInfo->pParse; struct sqlite3_index_constraint *pIdxCons; struct sqlite3_index_orderby *pIdxOrderBy; struct sqlite3_index_constraint_usage *pUsage; + struct HiddenIndexInfo *pHidden; WhereTerm *pTerm; int nOrderBy; sqlite3_index_info *pIdxInfo; + u16 mNoOmit = 0; + const Table *pTab; + int eDistinct = 0; + ExprList *pOrderBy = pWInfo->pOrderBy; + WhereClause *p; + + assert( pSrc!=0 ); + pTab = pSrc->pSTab; + assert( pTab!=0 ); + assert( IsVirtual(pTab) ); - /* Count the number of possible WHERE clause constraints referring - ** to this virtual table */ - for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){ - if( pTerm->leftCursor != pSrc->iCursor ) continue; - if( pTerm->prereqRight & mUnusable ) continue; - assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) ); - testcase( pTerm->eOperator & WO_IN ); - testcase( pTerm->eOperator & WO_ISNULL ); - testcase( pTerm->eOperator & WO_IS ); - testcase( pTerm->eOperator & WO_ALL ); - if( (pTerm->eOperator & ~(WO_ISNULL|WO_EQUIV|WO_IS))==0 ) continue; - if( pTerm->wtFlags & TERM_VNULL ) continue; - assert( pTerm->u.leftColumn>=(-1) ); - nTerm++; + /* Find all WHERE clause constraints referring to this virtual table. + ** Mark each term with the TERM_OK flag. Set nTerm to the number of + ** terms found. + */ + for(p=pWC, nTerm=0; p; p=p->pOuter){ + for(i=0, pTerm=p->a; i<p->nTerm; i++, pTerm++){ + pTerm->wtFlags &= ~TERM_OK; + if( pTerm->leftCursor != pSrc->iCursor ) continue; + if( pTerm->prereqRight & mUnusable ) continue; + assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) ); + testcase( pTerm->eOperator & WO_IN ); + testcase( pTerm->eOperator & WO_ISNULL ); + testcase( pTerm->eOperator & WO_IS ); + testcase( pTerm->eOperator & WO_ALL ); + if( (pTerm->eOperator & ~(WO_EQUIV))==0 ) continue; + if( pTerm->wtFlags & TERM_VNULL ) continue; + + assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 ); + assert( pTerm->u.x.leftColumn>=XN_ROWID ); + assert( pTerm->u.x.leftColumn<pTab->nCol ); + if( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))!=0 + && !constraintCompatibleWithOuterJoin(pTerm,pSrc) + ){ + continue; + } + nTerm++; + pTerm->wtFlags |= TERM_OK; + } } - /* If the ORDER BY clause contains only columns in the current + /* If the ORDER BY clause contains only columns in the current ** virtual table then allocate space for the aOrderBy part of ** the sqlite3_index_info structure. */ @@ -845,10 +1477,49 @@ static sqlite3_index_info *allocateIndexInfo( int n = pOrderBy->nExpr; for(i=0; i<n; i++){ Expr *pExpr = pOrderBy->a[i].pExpr; - if( pExpr->op!=TK_COLUMN || pExpr->iTable!=pSrc->iCursor ) break; + Expr *pE2; + + /* Skip over constant terms in the ORDER BY clause */ + if( sqlite3ExprIsConstant(0, pExpr) ){ + continue; + } + + /* Virtual tables are unable to deal with NULLS FIRST */ + if( pOrderBy->a[i].fg.sortFlags & KEYINFO_ORDER_BIGNULL ) break; + + /* First case - a direct column references without a COLLATE operator */ + if( pExpr->op==TK_COLUMN && pExpr->iTable==pSrc->iCursor ){ + assert( pExpr->iColumn>=XN_ROWID && pExpr->iColumn<pTab->nCol ); + continue; + } + + /* 2nd case - a column reference with a COLLATE operator. Only match + ** of the COLLATE operator matches the collation of the column. */ + if( pExpr->op==TK_COLLATE + && (pE2 = pExpr->pLeft)->op==TK_COLUMN + && pE2->iTable==pSrc->iCursor + ){ + const char *zColl; /* The collating sequence name */ + assert( !ExprHasProperty(pExpr, EP_IntValue) ); + assert( pExpr->u.zToken!=0 ); + assert( pE2->iColumn>=XN_ROWID && pE2->iColumn<pTab->nCol ); + pExpr->iColumn = pE2->iColumn; + if( pE2->iColumn<0 ) continue; /* Collseq does not matter for rowid */ + zColl = sqlite3ColumnColl(&pTab->aCol[pE2->iColumn]); + if( zColl==0 ) zColl = sqlite3StrBINARY; + if( sqlite3_stricmp(pExpr->u.zToken, zColl)==0 ) continue; + } + + /* No matches cause a break out of the loop */ + break; } - if( i==n){ + if( i==n ){ nOrderBy = n; + if( (pWInfo->wctrlFlags & WHERE_DISTINCTBY) && !pSrc->fg.rowidUsed ){ + eDistinct = 2 + ((pWInfo->wctrlFlags & WHERE_SORTBYGROUP)!=0); + }else if( pWInfo->wctrlFlags & WHERE_GROUPBY ){ + eDistinct = 1; + } } } @@ -856,92 +1527,160 @@ static sqlite3_index_info *allocateIndexInfo( */ pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo) + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm - + sizeof(*pIdxOrderBy)*nOrderBy ); + + sizeof(*pIdxOrderBy)*nOrderBy + + SZ_HIDDENINDEXINFO(nTerm) ); if( pIdxInfo==0 ){ sqlite3ErrorMsg(pParse, "out of memory"); return 0; } - - /* Initialize the structure. The sqlite3_index_info structure contains - ** many fields that are declared "const" to prevent xBestIndex from - ** changing them. We have to do some funky casting in order to - ** initialize those fields. - */ - pIdxCons = (struct sqlite3_index_constraint*)&pIdxInfo[1]; + pHidden = (struct HiddenIndexInfo*)&pIdxInfo[1]; + pIdxCons = (struct sqlite3_index_constraint*)&pHidden->aRhs[nTerm]; pIdxOrderBy = (struct sqlite3_index_orderby*)&pIdxCons[nTerm]; pUsage = (struct sqlite3_index_constraint_usage*)&pIdxOrderBy[nOrderBy]; - *(int*)&pIdxInfo->nConstraint = nTerm; - *(int*)&pIdxInfo->nOrderBy = nOrderBy; - *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint = pIdxCons; - *(struct sqlite3_index_orderby**)&pIdxInfo->aOrderBy = pIdxOrderBy; - *(struct sqlite3_index_constraint_usage**)&pIdxInfo->aConstraintUsage = - pUsage; - - for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){ - u8 op; - if( pTerm->leftCursor != pSrc->iCursor ) continue; - if( pTerm->prereqRight & mUnusable ) continue; - assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) ); - testcase( pTerm->eOperator & WO_IN ); - testcase( pTerm->eOperator & WO_IS ); - testcase( pTerm->eOperator & WO_ISNULL ); - testcase( pTerm->eOperator & WO_ALL ); - if( (pTerm->eOperator & ~(WO_ISNULL|WO_EQUIV|WO_IS))==0 ) continue; - if( pTerm->wtFlags & TERM_VNULL ) continue; - assert( pTerm->u.leftColumn>=(-1) ); - pIdxCons[j].iColumn = pTerm->u.leftColumn; - pIdxCons[j].iTermOffset = i; - op = (u8)pTerm->eOperator & WO_ALL; - if( op==WO_IN ) op = WO_EQ; - if( op==WO_MATCH ){ - op = pTerm->eMatchOp; - } - pIdxCons[j].op = op; - /* The direct assignment in the previous line is possible only because - ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical. The - ** following asserts verify this fact. */ - assert( WO_EQ==SQLITE_INDEX_CONSTRAINT_EQ ); - assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT ); - assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE ); - assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT ); - assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE ); - assert( WO_MATCH==SQLITE_INDEX_CONSTRAINT_MATCH ); - assert( pTerm->eOperator & (WO_IN|WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_MATCH) ); - j++; + pIdxInfo->aConstraint = pIdxCons; + pIdxInfo->aOrderBy = pIdxOrderBy; + pIdxInfo->aConstraintUsage = pUsage; + pIdxInfo->colUsed = (sqlite3_int64)pSrc->colUsed; + if( HasRowid(pTab)==0 ){ + /* Ensure that all bits associated with PK columns are set. This is to + ** ensure they are available for cases like RIGHT joins or OR loops. */ + Index *pPk = sqlite3PrimaryKeyIndex((Table*)pTab); + assert( pPk!=0 ); + for(i=0; i<pPk->nKeyCol; i++){ + int iCol = pPk->aiColumn[i]; + assert( iCol>=0 ); + if( iCol>=BMS-1 ) iCol = BMS-1; + pIdxInfo->colUsed |= MASKBIT(iCol); + } + } + pHidden->pWC = pWC; + pHidden->pParse = pParse; + pHidden->eDistinct = eDistinct; + pHidden->mIn = 0; + for(p=pWC, i=j=0; p; p=p->pOuter){ + int nLast = i+p->nTerm;; + for(pTerm=p->a; i<nLast; i++, pTerm++){ + u16 op; + if( (pTerm->wtFlags & TERM_OK)==0 ) continue; + pIdxCons[j].iColumn = pTerm->u.x.leftColumn; + pIdxCons[j].iTermOffset = i; + op = pTerm->eOperator & WO_ALL; + if( op==WO_IN ){ + if( (pTerm->wtFlags & TERM_SLICE)==0 ){ + pHidden->mIn |= SMASKBIT32(j); + } + op = WO_EQ; + } + if( op==WO_AUX ){ + pIdxCons[j].op = pTerm->eMatchOp; + }else if( op & (WO_ISNULL|WO_IS) ){ + if( op==WO_ISNULL ){ + pIdxCons[j].op = SQLITE_INDEX_CONSTRAINT_ISNULL; + }else{ + pIdxCons[j].op = SQLITE_INDEX_CONSTRAINT_IS; + } + }else{ + pIdxCons[j].op = (u8)op; + /* The direct assignment in the previous line is possible only because + ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical. The + ** following asserts verify this fact. */ + assert( WO_EQ==SQLITE_INDEX_CONSTRAINT_EQ ); + assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT ); + assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE ); + assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT ); + assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE ); + assert( pTerm->eOperator&(WO_IN|WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_AUX) ); + + if( op & (WO_LT|WO_LE|WO_GT|WO_GE) + && sqlite3ExprIsVector(pTerm->pExpr->pRight) + ){ + testcase( j!=i ); + if( j<16 ) mNoOmit |= (1 << j); + if( op==WO_LT ) pIdxCons[j].op = WO_LE; + if( op==WO_GT ) pIdxCons[j].op = WO_GE; + } + } + + j++; + } } - for(i=0; i<nOrderBy; i++){ + assert( j==nTerm ); + pIdxInfo->nConstraint = j; + for(i=j=0; i<nOrderBy; i++){ Expr *pExpr = pOrderBy->a[i].pExpr; - pIdxOrderBy[i].iColumn = pExpr->iColumn; - pIdxOrderBy[i].desc = pOrderBy->a[i].sortOrder; + if( sqlite3ExprIsConstant(0, pExpr) ) continue; + assert( pExpr->op==TK_COLUMN + || (pExpr->op==TK_COLLATE && pExpr->pLeft->op==TK_COLUMN + && pExpr->iColumn==pExpr->pLeft->iColumn) ); + pIdxOrderBy[j].iColumn = pExpr->iColumn; + pIdxOrderBy[j].desc = pOrderBy->a[i].fg.sortFlags & KEYINFO_ORDER_DESC; + j++; } + pIdxInfo->nOrderBy = j; + *pmNoOmit = mNoOmit; return pIdxInfo; } +/* +** Free and zero the sqlite3_index_info.idxStr value if needed. +*/ +static void freeIdxStr(sqlite3_index_info *pIdxInfo){ + if( pIdxInfo->needToFreeIdxStr ){ + sqlite3_free(pIdxInfo->idxStr); + pIdxInfo->idxStr = 0; + pIdxInfo->needToFreeIdxStr = 0; + } +} + +/* +** Free an sqlite3_index_info structure allocated by allocateIndexInfo() +** and possibly modified by xBestIndex methods. +*/ +static void freeIndexInfo(sqlite3 *db, sqlite3_index_info *pIdxInfo){ + HiddenIndexInfo *pHidden; + int i; + assert( pIdxInfo!=0 ); + pHidden = (HiddenIndexInfo*)&pIdxInfo[1]; + assert( pHidden->pParse!=0 ); + assert( pHidden->pParse->db==db ); + for(i=0; i<pIdxInfo->nConstraint; i++){ + sqlite3ValueFree(pHidden->aRhs[i]); /* IMP: R-14553-25174 */ + pHidden->aRhs[i] = 0; + } + freeIdxStr(pIdxInfo); + sqlite3DbFree(db, pIdxInfo); +} + /* ** The table object reference passed as the second argument to this function ** must represent a virtual table. This function invokes the xBestIndex() ** method of the virtual table with the sqlite3_index_info object that ** comes in as the 3rd argument to this function. ** -** If an error occurs, pParse is populated with an error message and a -** non-zero value is returned. Otherwise, 0 is returned and the output -** part of the sqlite3_index_info structure is left populated. +** If an error occurs, pParse is populated with an error message and an +** appropriate error code is returned. A return of SQLITE_CONSTRAINT from +** xBestIndex is not considered an error. SQLITE_CONSTRAINT indicates that +** the current configuration of "unusable" flags in sqlite3_index_info can +** not result in a valid plan. ** ** Whether or not an error is returned, it is the responsibility of the ** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates ** that this is required. */ static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){ - sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab; - int i; int rc; + sqlite3_vtab *pVtab; - TRACE_IDX_INPUTS(p); + assert( IsVirtual(pTab) ); + pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab; + whereTraceIndexInfoInputs(p, pTab); + pParse->db->nSchemaLock++; rc = pVtab->pModule->xBestIndex(pVtab, p); - TRACE_IDX_OUTPUTS(p); + pParse->db->nSchemaLock--; + whereTraceIndexInfoOutputs(p, pTab); - if( rc!=SQLITE_OK ){ + if( rc!=SQLITE_OK && rc!=SQLITE_CONSTRAINT ){ if( rc==SQLITE_NOMEM ){ sqlite3OomFault(pParse->db); }else if( !pVtab->zErrMsg ){ @@ -950,21 +1689,16 @@ static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){ sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg); } } + if( pTab->u.vtab.p->bAllSchemas ){ + sqlite3VtabUsesAllSchemas(pParse); + } sqlite3_free(pVtab->zErrMsg); pVtab->zErrMsg = 0; - - for(i=0; i<p->nConstraint; i++){ - if( !p->aConstraint[i].usable && p->aConstraintUsage[i].argvIndex>0 ){ - sqlite3ErrorMsg(pParse, - "table %s: xBestIndex returned an invalid plan", pTab->zName); - } - } - - return pParse->nErr; + return rc; } #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */ -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 +#ifdef SQLITE_ENABLE_STAT4 /* ** Estimate the location of a particular key among all keys in an ** index. Store the results in aStat as follows: @@ -975,8 +1709,8 @@ static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){ ** Return the index of the sample that is the smallest sample that ** is greater than or equal to pRec. Note that this index is not an index ** into the aSample[] array - it is an index into a virtual set of samples -** based on the contents of aSample[] and the number of fields in record -** pRec. +** based on the contents of aSample[] and the number of fields in record +** pRec. */ static int whereKeyStats( Parse *pParse, /* Database connection */ @@ -1000,7 +1734,8 @@ static int whereKeyStats( #endif assert( pRec!=0 ); assert( pIdx->nSample>0 ); - assert( pRec->nField>0 && pRec->nField<=pIdx->nSampleCol ); + assert( pRec->nField>0 ); + /* Do a binary search to find the first sample greater than or equal ** to pRec. If pRec contains a single field, the set of samples to search @@ -1012,41 +1747,46 @@ static int whereKeyStats( ** consider prefixes of those samples. For example, if the set of samples ** in aSample is: ** - ** aSample[0] = (a, 5) - ** aSample[1] = (a, 10) - ** aSample[2] = (b, 5) - ** aSample[3] = (c, 100) + ** aSample[0] = (a, 5) + ** aSample[1] = (a, 10) + ** aSample[2] = (b, 5) + ** aSample[3] = (c, 100) ** aSample[4] = (c, 105) ** - ** Then the search space should ideally be the samples above and the - ** unique prefixes [a], [b] and [c]. But since that is hard to organize, + ** Then the search space should ideally be the samples above and the + ** unique prefixes [a], [b] and [c]. But since that is hard to organize, ** the code actually searches this set: ** - ** 0: (a) - ** 1: (a, 5) - ** 2: (a, 10) - ** 3: (a, 10) - ** 4: (b) - ** 5: (b, 5) - ** 6: (c) - ** 7: (c, 100) + ** 0: (a) + ** 1: (a, 5) + ** 2: (a, 10) + ** 3: (a, 10) + ** 4: (b) + ** 5: (b, 5) + ** 6: (c) + ** 7: (c, 100) ** 8: (c, 105) ** 9: (c, 105) ** ** For each sample in the aSample[] array, N samples are present in the - ** effective sample array. In the above, samples 0 and 1 are based on + ** effective sample array. In the above, samples 0 and 1 are based on ** sample aSample[0]. Samples 2 and 3 on aSample[1] etc. ** ** Often, sample i of each block of N effective samples has (i+1) fields. ** Except, each sample may be extended to ensure that it is greater than or - ** equal to the previous sample in the array. For example, in the above, - ** sample 2 is the first sample of a block of N samples, so at first it - ** appears that it should be 1 field in size. However, that would make it - ** smaller than sample 1, so the binary search would not work. As a result, - ** it is extended to two fields. The duplicates that this creates do not + ** equal to the previous sample in the array. For example, in the above, + ** sample 2 is the first sample of a block of N samples, so at first it + ** appears that it should be 1 field in size. However, that would make it + ** smaller than sample 1, so the binary search would not work. As a result, + ** it is extended to two fields. The duplicates that this creates do not ** cause any problems. */ - nField = pRec->nField; + if( !HasRowid(pIdx->pTable) && IsPrimaryKeyIndex(pIdx) ){ + nField = pIdx->nKeyCol; + }else{ + nField = pIdx->nColumn; + } + nField = MIN(pRec->nField, nField); iCol = 0; iSample = pIdx->nSample * nField; do{ @@ -1057,7 +1797,7 @@ static int whereKeyStats( iSamp = iTest / nField; if( iSamp>0 ){ /* The proposed effective sample is a prefix of sample aSample[iSamp]. - ** Specifically, the shortest prefix of at least (1 + iTest%nField) + ** Specifically, the shortest prefix of at least (1 + iTest%nField) ** fields that is greater than the previous effective sample. */ for(n=(iTest % nField) + 1; n<nField; n++){ if( aSample[iSamp-1].anLt[n-1]!=aSample[iSamp].anLt[n-1] ) break; @@ -1092,8 +1832,8 @@ static int whereKeyStats( assert( i<pIdx->nSample ); assert( iCol==nField-1 ); pRec->nField = nField; - assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec) - || pParse->db->mallocFailed + assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec) + || pParse->db->mallocFailed ); }else{ /* Unless i==pIdx->nSample, indicating that pRec is larger than @@ -1101,7 +1841,7 @@ static int whereKeyStats( ** (iCol+1) field prefix of sample i. */ assert( i<=pIdx->nSample && i>=0 ); pRec->nField = iCol+1; - assert( i==pIdx->nSample + assert( i==pIdx->nSample || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)>0 || pParse->db->mallocFailed ); @@ -1112,12 +1852,12 @@ static int whereKeyStats( if( iCol>0 ){ pRec->nField = iCol; assert( sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)<=0 - || pParse->db->mallocFailed ); + || pParse->db->mallocFailed || CORRUPT_DB ); } if( i>0 ){ pRec->nField = nField; assert( sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0 - || pParse->db->mallocFailed ); + || pParse->db->mallocFailed || CORRUPT_DB ); } } } @@ -1129,12 +1869,12 @@ static int whereKeyStats( aStat[0] = aSample[i].anLt[iCol]; aStat[1] = aSample[i].anEq[iCol]; }else{ - /* At this point, the (iCol+1) field prefix of aSample[i] is the first + /* At this point, the (iCol+1) field prefix of aSample[i] is the first ** sample that is greater than pRec. Or, if i==pIdx->nSample then pRec ** is larger than all samples in the array. */ tRowcnt iUpper, iGap; if( i>=pIdx->nSample ){ - iUpper = sqlite3LogEstToInt(pIdx->aiRowLogEst[0]); + iUpper = pIdx->nRowEst0; }else{ iUpper = aSample[i].anLt[iCol]; } @@ -1150,18 +1890,18 @@ static int whereKeyStats( iGap = iGap/3; } aStat[0] = iLower + iGap; - aStat[1] = pIdx->aAvgEq[iCol]; + aStat[1] = pIdx->aAvgEq[nField-1]; } /* Restore the pRec->nField value before returning. */ pRec->nField = nField; return i; } -#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ +#endif /* SQLITE_ENABLE_STAT4 */ /* ** If it is not NULL, pTerm is a term that provides an upper or lower -** bound on a range scan. Without considering pTerm, it is estimated +** bound on a range scan. Without considering pTerm, it is estimated ** that the scan will visit nNew rows. This function returns the number ** estimated to be visited after taking pTerm into account. ** @@ -1183,33 +1923,34 @@ static LogEst whereRangeAdjust(WhereTerm *pTerm, LogEst nNew){ } -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 +#ifdef SQLITE_ENABLE_STAT4 /* ** Return the affinity for a single column of an index. */ -static char sqlite3IndexColumnAffinity(sqlite3 *db, Index *pIdx, int iCol){ +char sqlite3IndexColumnAffinity(sqlite3 *db, Index *pIdx, int iCol){ assert( iCol>=0 && iCol<pIdx->nColumn ); if( !pIdx->zColAff ){ if( sqlite3IndexAffinityStr(db, pIdx)==0 ) return SQLITE_AFF_BLOB; } + assert( pIdx->zColAff[iCol]!=0 ); return pIdx->zColAff[iCol]; } #endif -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 -/* +#ifdef SQLITE_ENABLE_STAT4 +/* ** This function is called to estimate the number of rows visited by a ** range-scan on a skip-scan index. For example: ** ** CREATE INDEX i1 ON t1(a, b, c); ** SELECT * FROM t1 WHERE a=? AND c BETWEEN ? AND ?; ** -** Value pLoop->nOut is currently set to the estimated number of rows -** visited for scanning (a=? AND b=?). This function reduces that estimate +** Value pLoop->nOut is currently set to the estimated number of rows +** visited for scanning (a=? AND b=?). This function reduces that estimate ** by some factor to account for the (c BETWEEN ? AND ?) expression based -** on the stat4 data for the index. this scan will be peformed multiple -** times (once for each (a,b) combination that matches a=?) is dealt with +** on the stat4 data for the index. this scan will be performed multiple +** times (once for each (a,b) combination that matches a=?) is dealt with ** by the caller. ** ** It does this by scanning through all stat4 samples, comparing values @@ -1230,7 +1971,7 @@ static char sqlite3IndexColumnAffinity(sqlite3 *db, Index *pIdx, int iCol){ ** estimate of the number of rows delivered remains unchanged), *pbDone ** is left as is. ** -** If an error occurs, an SQLite error code is returned. Otherwise, +** If an error occurs, an SQLite error code is returned. Otherwise, ** SQLITE_OK. */ static int whereRangeSkipScanEst( @@ -1248,7 +1989,7 @@ static int whereRangeSkipScanEst( int rc = SQLITE_OK; u8 aff = sqlite3IndexColumnAffinity(db, p, nEq); CollSeq *pColl; - + sqlite3_value *p1 = 0; /* Value extracted from pLower */ sqlite3_value *p2 = 0; /* Value extracted from pUpper */ sqlite3_value *pVal = 0; /* Value extracted from record */ @@ -1280,7 +2021,7 @@ static int whereRangeSkipScanEst( nDiff = (nUpper - nLower); if( nDiff<=0 ) nDiff = 1; - /* If there is both an upper and lower bound specified, and the + /* If there is both an upper and lower bound specified, and the ** comparisons indicate that they are close together, use the fallback ** method (assume that the scan visits 1/64 of the rows) for estimating ** the number of rows visited. Otherwise, estimate the number of rows @@ -1289,7 +2030,7 @@ static int whereRangeSkipScanEst( int nAdjust = (sqlite3LogEst(p->nSample) - sqlite3LogEst(nDiff)); pLoop->nOut -= nAdjust; *pbDone = 1; - WHERETRACE(0x10, ("range skip-scan regions: %u..%u adjust=%d est=%d\n", + WHERETRACE(0x20, ("range skip-scan regions: %u..%u adjust=%d est=%d\n", nLower, nUpper, nAdjust*-1, pLoop->nOut)); } @@ -1303,7 +2044,7 @@ static int whereRangeSkipScanEst( return rc; } -#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ +#endif /* SQLITE_ENABLE_STAT4 */ /* ** This function is used to estimate the number of rows that will be visited @@ -1327,7 +2068,7 @@ static int whereRangeSkipScanEst( ** ** ... FROM t1 WHERE a = ? AND b > ? AND b < ? ... ** -** then nEq is set to 1 (as the range restricted column, b, is the second +** then nEq is set to 1 (as the range restricted column, b, is the second ** left-most column of the index). Or, if the query is: ** ** ... FROM t1 WHERE a > ? AND a < ? ... @@ -1335,13 +2076,13 @@ static int whereRangeSkipScanEst( ** then nEq is set to 0. ** ** When this function is called, *pnOut is set to the sqlite3LogEst() of the -** number of rows that the index scan is expected to visit without -** considering the range constraints. If nEq is 0, then *pnOut is the number of +** number of rows that the index scan is expected to visit without +** considering the range constraints. If nEq is 0, then *pnOut is the number of ** rows in the index. Assuming no error occurs, *pnOut is adjusted (reduced) ** to account for the range constraints pLower and pUpper. -** +** ** In the absence of sqlite_stat4 ANALYZE data, or if such data cannot be -** used, a single range inequality reduces the search space by a factor of 4. +** used, a single range inequality reduces the search space by a factor of 4. ** and a pair of constraints (x>? AND x<?) reduces the expected number of ** rows visited by a factor of 64. */ @@ -1356,17 +2097,20 @@ static int whereRangeScanEst( int nOut = pLoop->nOut; LogEst nNew; -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 +#ifdef SQLITE_ENABLE_STAT4 Index *p = pLoop->u.btree.pIndex; int nEq = pLoop->u.btree.nEq; - if( p->nSample>0 && nEq<p->nSampleCol ){ + if( p->nSample>0 && ALWAYS(nEq<p->nSampleCol) + && OptimizationEnabled(pParse->db, SQLITE_Stat4) + ){ if( nEq==pBuilder->nRecValid ){ UnpackedRecord *pRec = pBuilder->pRec; tRowcnt a[2]; - u8 aff; + int nBtm = pLoop->u.btree.nBtm; + int nTop = pLoop->u.btree.nTop; - /* Variable iLower will be set to the estimate of the number of rows in + /* Variable iLower will be set to the estimate of the number of rows in ** the index that are less than the lower bound of the range query. The ** lower bound being the concatenation of $P and $L, where $P is the ** key-prefix formed by the nEq values matched against the nEq left-most @@ -1375,7 +2119,7 @@ static int whereRangeScanEst( ** Or, if pLower is NULL or $L cannot be extracted from it (because it ** is not a simple variable or literal value), the lower bound of the ** range is $P. Due to a quirk in the way whereKeyStats() works, even - ** if $L is available, whereKeyStats() is called for both ($P) and + ** if $L is available, whereKeyStats() is called for both ($P) and ** ($P:$L) and the larger of the two returned values is used. ** ** Similarly, iUpper is to be set to the estimate of the number of rows @@ -1394,14 +2138,12 @@ static int whereRangeScanEst( testcase( pRec->nField!=pBuilder->nRecValid ); pRec->nField = pBuilder->nRecValid; } - aff = sqlite3IndexColumnAffinity(pParse->db, p, nEq); - assert( nEq!=p->nKeyCol || aff==SQLITE_AFF_INTEGER ); /* Determine iLower and iUpper using ($P) only. */ if( nEq==0 ){ iLower = 0; iUpper = p->nRowEst0; }else{ - /* Note: this call could be optimized away - since the same values must + /* Note: this call could be optimized away - since the same values must ** have been requested when testing key $P in whereEqualScanEst(). */ whereKeyStats(pParse, p, pRec, 0, a); iLower = a[0]; @@ -1414,17 +2156,20 @@ static int whereRangeScanEst( if( p->aSortOrder[nEq] ){ /* The roles of pLower and pUpper are swapped for a DESC index */ SWAP(WhereTerm*, pLower, pUpper); + SWAP(int, nBtm, nTop); } /* If possible, improve on the iLower estimate using ($P:$L). */ if( pLower ){ - int bOk; /* True if value is extracted from pExpr */ + int n; /* Values extracted from pExpr */ Expr *pExpr = pLower->pExpr->pRight; - rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk); - if( rc==SQLITE_OK && bOk ){ + rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, nBtm, nEq, &n); + if( rc==SQLITE_OK && n ){ tRowcnt iNew; + u16 mask = WO_GT|WO_LE; + if( sqlite3ExprVectorSize(pExpr)>n ) mask = (WO_LE|WO_LT); iLwrIdx = whereKeyStats(pParse, p, pRec, 0, a); - iNew = a[0] + ((pLower->eOperator & (WO_GT|WO_LE)) ? a[1] : 0); + iNew = a[0] + ((pLower->eOperator & mask) ? a[1] : 0); if( iNew>iLower ) iLower = iNew; nOut--; pLower = 0; @@ -1433,13 +2178,15 @@ static int whereRangeScanEst( /* If possible, improve on the iUpper estimate using ($P:$U). */ if( pUpper ){ - int bOk; /* True if value is extracted from pExpr */ + int n; /* Values extracted from pExpr */ Expr *pExpr = pUpper->pExpr->pRight; - rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk); - if( rc==SQLITE_OK && bOk ){ + rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, nTop, nEq, &n); + if( rc==SQLITE_OK && n ){ tRowcnt iNew; + u16 mask = WO_GT|WO_LE; + if( sqlite3ExprVectorSize(pExpr)>n ) mask = (WO_LE|WO_LT); iUprIdx = whereKeyStats(pParse, p, pRec, 1, a); - iNew = a[0] + ((pUpper->eOperator & (WO_GT|WO_LE)) ? a[1] : 0); + iNew = a[0] + ((pUpper->eOperator & mask) ? a[1] : 0); if( iNew<iUpper ) iUpper = iNew; nOut--; pUpper = 0; @@ -1453,15 +2200,16 @@ static int whereRangeScanEst( /* TUNING: If both iUpper and iLower are derived from the same ** sample, then assume they are 4x more selective. This brings ** the estimated selectivity more in line with what it would be - ** if estimated without the use of STAT3/4 tables. */ - if( iLwrIdx==iUprIdx ) nNew -= 20; assert( 20==sqlite3LogEst(4) ); + ** if estimated without the use of STAT4 tables. */ + if( iLwrIdx==iUprIdx ){ nNew -= 20; } + assert( 20==sqlite3LogEst(4) ); }else{ nNew = 10; assert( 10==sqlite3LogEst(2) ); } if( nNew<nOut ){ nOut = nNew; } - WHERETRACE(0x10, ("STAT4 range scan: %u..%u est=%d\n", + WHERETRACE(0x20, ("STAT4 range scan: %u..%u est=%d\n", (u32)iLower, (u32)iUpper, nOut)); } }else{ @@ -1475,7 +2223,7 @@ static int whereRangeScanEst( UNUSED_PARAMETER(pBuilder); assert( pLower || pUpper ); #endif - assert( pUpper==0 || (pUpper->wtFlags & TERM_VNULL)==0 ); + assert( pUpper==0 || (pUpper->wtFlags & TERM_VNULL)==0 || pParse->nErr>0 ); nNew = whereRangeAdjust(pLower, nOut); nNew = whereRangeAdjust(pUpper, nNew); @@ -1484,7 +2232,7 @@ static int whereRangeScanEst( ** reduced by an additional 75%. This means that, by default, an open-ended ** range query (e.g. col > ?) is assumed to match 1/4 of the rows in the ** index. While a closed range (e.g. col BETWEEN ? AND ?) is estimated to - ** match 1/64 of the index. */ + ** match 1/64 of the index. */ if( pLower && pLower->truthProb>0 && pUpper && pUpper->truthProb>0 ){ nNew -= 20; } @@ -1494,7 +2242,7 @@ static int whereRangeScanEst( if( nNew<nOut ) nOut = nNew; #if defined(WHERETRACE_ENABLED) if( pLoop->nOut>nOut ){ - WHERETRACE(0x10,("Range scan lowers nOut from %d to %d\n", + WHERETRACE(0x20,("Range scan lowers nOut from %d to %d\n", pLoop->nOut, nOut)); } #endif @@ -1502,16 +2250,16 @@ static int whereRangeScanEst( return rc; } -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 +#ifdef SQLITE_ENABLE_STAT4 /* ** Estimate the number of rows that will be returned based on ** an equality constraint x=VALUE and where that VALUE occurs in ** the histogram data. This only works when x is the left-most -** column of an index and sqlite_stat3 histogram data is available +** column of an index and sqlite_stat4 histogram data is available ** for that index. When pExpr==NULL that means the constraint is ** "x IS NULL" instead of "x=VALUE". ** -** Write the estimated row count into *pnRow and return SQLITE_OK. +** Write the estimated row count into *pnRow and return SQLITE_OK. ** If unable to make an estimate, leave *pnRow unchanged and return ** non-zero. ** @@ -1529,7 +2277,6 @@ static int whereEqualScanEst( Index *p = pBuilder->pNew->u.btree.pIndex; int nEq = pBuilder->pNew->u.btree.nEq; UnpackedRecord *pRec = pBuilder->pRec; - u8 aff; /* Column affinity */ int rc; /* Subfunction return code */ tRowcnt a[2]; /* Statistics */ int bOk; @@ -1553,22 +2300,22 @@ static int whereEqualScanEst( return SQLITE_OK; } - aff = sqlite3IndexColumnAffinity(pParse->db, p, nEq-1); - rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq-1, &bOk); + rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, 1, nEq-1, &bOk); pBuilder->pRec = pRec; if( rc!=SQLITE_OK ) return rc; if( bOk==0 ) return SQLITE_NOTFOUND; pBuilder->nRecValid = nEq; whereKeyStats(pParse, p, pRec, 0, a); - WHERETRACE(0x10,("equality scan regions: %d\n", (int)a[1])); + WHERETRACE(0x20,("equality scan regions %s(%d): %d\n", + p->zName, nEq-1, (int)a[1])); *pnRow = a[1]; - + return rc; } -#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ +#endif /* SQLITE_ENABLE_STAT4 */ -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 +#ifdef SQLITE_ENABLE_STAT4 /* ** Estimate the number of rows that will be returned based on ** an IN constraint where the right-hand side of the IN operator @@ -1576,7 +2323,7 @@ static int whereEqualScanEst( ** ** WHERE x IN (1,2,3,4) ** -** Write the estimated row count into *pnRow and return SQLITE_OK. +** Write the estimated row count into *pnRow and return SQLITE_OK. ** If unable to make an estimate, leave *pnRow unchanged and return ** non-zero. ** @@ -1608,51 +2355,111 @@ static int whereInScanEst( } if( rc==SQLITE_OK ){ - if( nRowEst > nRow0 ) nRowEst = nRow0; + if( nRowEst > (tRowcnt)nRow0 ) nRowEst = nRow0; *pnRow = nRowEst; - WHERETRACE(0x10,("IN row estimate: est=%d\n", nRowEst)); + WHERETRACE(0x20,("IN row estimate: est=%d\n", nRowEst)); } assert( pBuilder->nRecValid==nRecValid ); return rc; } -#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ +#endif /* SQLITE_ENABLE_STAT4 */ -#ifdef WHERETRACE_ENABLED +#if defined(WHERETRACE_ENABLED) || defined(SQLITE_DEBUG) /* ** Print the content of a WhereTerm object */ -static void whereTermPrint(WhereTerm *pTerm, int iTerm){ +void sqlite3WhereTermPrint(WhereTerm *pTerm, int iTerm){ if( pTerm==0 ){ sqlite3DebugPrintf("TERM-%-3d NULL\n", iTerm); }else{ - char zType[4]; - memcpy(zType, "...", 4); + char zType[8]; + char zLeft[50]; + memcpy(zType, "....", 5); if( pTerm->wtFlags & TERM_VIRTUAL ) zType[0] = 'V'; if( pTerm->eOperator & WO_EQUIV ) zType[1] = 'E'; - if( ExprHasProperty(pTerm->pExpr, EP_FromJoin) ) zType[2] = 'L'; + if( ExprHasProperty(pTerm->pExpr, EP_OuterON) ) zType[2] = 'L'; + if( pTerm->wtFlags & TERM_CODED ) zType[3] = 'C'; + if( pTerm->eOperator & WO_SINGLE ){ + assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 ); + sqlite3_snprintf(sizeof(zLeft),zLeft,"left={%d:%d}", + pTerm->leftCursor, pTerm->u.x.leftColumn); + }else if( (pTerm->eOperator & WO_OR)!=0 && pTerm->u.pOrInfo!=0 ){ + sqlite3_snprintf(sizeof(zLeft),zLeft,"indexable=0x%llx", + pTerm->u.pOrInfo->indexable); + }else{ + sqlite3_snprintf(sizeof(zLeft),zLeft,"left=%d", pTerm->leftCursor); + } + iTerm = pTerm->iTerm = MAX(iTerm,pTerm->iTerm); sqlite3DebugPrintf( - "TERM-%-3d %p %s cursor=%-3d prob=%-3d op=0x%03x wtFlags=0x%04x\n", - iTerm, pTerm, zType, pTerm->leftCursor, pTerm->truthProb, - pTerm->eOperator, pTerm->wtFlags); + "TERM-%-3d %p %s %-12s op=%03x wtFlags=%04x", + iTerm, pTerm, zType, zLeft, pTerm->eOperator, pTerm->wtFlags); + /* The 0x10000 .wheretrace flag causes extra information to be + ** shown about each Term */ + if( sqlite3WhereTrace & 0x10000 ){ + sqlite3DebugPrintf(" prob=%-3d prereq=%llx,%llx", + pTerm->truthProb, (u64)pTerm->prereqAll, (u64)pTerm->prereqRight); + } + if( (pTerm->eOperator & (WO_OR|WO_AND))==0 && pTerm->u.x.iField ){ + sqlite3DebugPrintf(" iField=%d", pTerm->u.x.iField); + } + if( pTerm->iParent>=0 ){ + sqlite3DebugPrintf(" iParent=%d", pTerm->iParent); + } + sqlite3DebugPrintf("\n"); sqlite3TreeViewExpr(0, pTerm->pExpr, 0); } } +void sqlite3ShowWhereTerm(WhereTerm *pTerm){ + sqlite3WhereTermPrint(pTerm, 0); +} +#endif + +#ifdef WHERETRACE_ENABLED +/* +** Show the complete content of a WhereClause +*/ +void sqlite3WhereClausePrint(WhereClause *pWC){ + int i; + for(i=0; i<pWC->nTerm; i++){ + sqlite3WhereTermPrint(&pWC->a[i], i); + } +} #endif #ifdef WHERETRACE_ENABLED /* ** Print a WhereLoop object for debugging purposes +** +** Format example: +** +** .--- Position in WHERE clause rSetup, rRun, nOut ---. +** | | +** | .--- selfMask nTerm ------. | +** | | | | +** | | .-- prereq Idx wsFlags----. | | +** | | | Name | | | +** | | | __|__ nEq ---. ___|__ | __|__ +** | / \ / \ / \ | / \ / \ / \ +** 1.002.001 t2.t2xy 2 f 010241 N 2 cost 0,56,31 */ -static void whereLoopPrint(WhereLoop *p, WhereClause *pWC){ - WhereInfo *pWInfo = pWC->pWInfo; - int nb = 1+(pWInfo->pTabList->nSrc+7)/8; - struct SrcList_item *pItem = pWInfo->pTabList->a + p->iTab; - Table *pTab = pItem->pTab; - sqlite3DebugPrintf("%c%2d.%0*llx.%0*llx", p->cId, - p->iTab, nb, p->maskSelf, nb, p->prereq); - sqlite3DebugPrintf(" %12s", - pItem->zAlias ? pItem->zAlias : pTab->zName); +void sqlite3WhereLoopPrint(const WhereLoop *p, const WhereClause *pWC){ + WhereInfo *pWInfo; + if( pWC ){ + pWInfo = pWC->pWInfo; + int nb = 1+(pWInfo->pTabList->nSrc+3)/4; + SrcItem *pItem = pWInfo->pTabList->a + p->iTab; + Table *pTab = pItem->pSTab; + Bitmask mAll = (((Bitmask)1)<<(nb*4)) - 1; + sqlite3DebugPrintf("%c%2d.%0*llx.%0*llx", p->cId, + p->iTab, nb, p->maskSelf, nb, p->prereq & mAll); + sqlite3DebugPrintf(" %12s", + pItem->zAlias ? pItem->zAlias : pTab->zName); + }else{ + pWInfo = 0; + sqlite3DebugPrintf("%c%2d.%03llx.%03llx %c%d", + p->cId, p->iTab, p->maskSelf, p->prereq & 0xfff, p->cId, p->iTab); + } if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){ const char *zName; if( p->u.btree.pIndex && (zName = p->u.btree.pIndex->zName)!=0 ){ @@ -1668,7 +2475,7 @@ static void whereLoopPrint(WhereLoop *p, WhereClause *pWC){ }else{ char *z; if( p->u.vtab.idxStr ){ - z = sqlite3_mprintf("(%d,\"%s\",%x)", + z = sqlite3_mprintf("(%d,\"%s\",%#x)", p->u.vtab.idxNum, p->u.vtab.idxStr, p->u.vtab.omitMask); }else{ z = sqlite3_mprintf("(%d,%x)", p->u.vtab.idxNum, p->u.vtab.omitMask); @@ -1677,18 +2484,32 @@ static void whereLoopPrint(WhereLoop *p, WhereClause *pWC){ sqlite3_free(z); } if( p->wsFlags & WHERE_SKIPSCAN ){ - sqlite3DebugPrintf(" f %05x %d-%d", p->wsFlags, p->nLTerm,p->nSkip); + sqlite3DebugPrintf(" f %06x %d-%d", p->wsFlags, p->nLTerm,p->nSkip); + }else{ + sqlite3DebugPrintf(" f %06x N %d", p->wsFlags, p->nLTerm); + } + if( pWInfo && pWInfo->bStarUsed && p->rStarDelta!=0 ){ + sqlite3DebugPrintf(" cost %d,%d,%d delta=%d\n", + p->rSetup, p->rRun, p->nOut, p->rStarDelta); }else{ - sqlite3DebugPrintf(" f %05x N %d", p->wsFlags, p->nLTerm); + sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut); } - sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut); - if( p->nLTerm && (sqlite3WhereTrace & 0x100)!=0 ){ + if( p->nLTerm && (sqlite3WhereTrace & 0x4000)!=0 ){ int i; for(i=0; i<p->nLTerm; i++){ - whereTermPrint(p->aLTerm[i], i); + sqlite3WhereTermPrint(p->aLTerm[i], i); } } } +void sqlite3ShowWhereLoop(const WhereLoop *p){ + if( p ) sqlite3WhereLoopPrint(p, 0); +} +void sqlite3ShowWhereLoopList(const WhereLoop *p){ + while( p ){ + sqlite3ShowWhereLoop(p); + p = p->pNextLoop; + } +} #endif /* @@ -1713,19 +2534,25 @@ static void whereLoopClearUnion(sqlite3 *db, WhereLoop *p){ p->u.vtab.idxStr = 0; }else if( (p->wsFlags & WHERE_AUTO_INDEX)!=0 && p->u.btree.pIndex!=0 ){ sqlite3DbFree(db, p->u.btree.pIndex->zColAff); - sqlite3DbFree(db, p->u.btree.pIndex); + sqlite3DbFreeNN(db, p->u.btree.pIndex); p->u.btree.pIndex = 0; } } } /* -** Deallocate internal memory used by a WhereLoop object +** Deallocate internal memory used by a WhereLoop object. Leave the +** object in an initialized state, as if it had been newly allocated. */ static void whereLoopClear(sqlite3 *db, WhereLoop *p){ - if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm); + if( p->aLTerm!=p->aLTermSpace ){ + sqlite3DbFreeNN(db, p->aLTerm); + p->aLTerm = p->aLTermSpace; + p->nLSlot = ArraySize(p->aLTermSpace); + } whereLoopClearUnion(db, p); - whereLoopInit(p); + p->nLTerm = 0; + p->wsFlags = 0; } /* @@ -1736,9 +2563,9 @@ static int whereLoopResize(sqlite3 *db, WhereLoop *p, int n){ if( p->nLSlot>=n ) return SQLITE_OK; n = (n+7)&~7; paNew = sqlite3DbMallocRawNN(db, sizeof(p->aLTerm[0])*n); - if( paNew==0 ) return SQLITE_NOMEM; + if( paNew==0 ) return SQLITE_NOMEM_BKPT; memcpy(paNew, p->aLTerm, sizeof(p->aLTerm[0])*p->nLSlot); - if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm); + if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFreeNN(db, p->aLTerm); p->aLTerm = paNew; p->nLSlot = n; return SQLITE_OK; @@ -1749,9 +2576,11 @@ static int whereLoopResize(sqlite3 *db, WhereLoop *p, int n){ */ static int whereLoopXfer(sqlite3 *db, WhereLoop *pTo, WhereLoop *pFrom){ whereLoopClearUnion(db, pTo); - if( whereLoopResize(db, pTo, pFrom->nLTerm) ){ - memset(&pTo->u, 0, sizeof(pTo->u)); - return SQLITE_NOMEM; + if( pFrom->nLTerm > pTo->nLSlot + && whereLoopResize(db, pTo, pFrom->nLTerm) + ){ + memset(pTo, 0, WHERE_LOOP_XFER_SZ); + return SQLITE_NOMEM_BKPT; } memcpy(pTo, pFrom, WHERE_LOOP_XFER_SZ); memcpy(pTo->aLTerm, pFrom->aLTerm, pTo->nLTerm*sizeof(pTo->aLTerm[0])); @@ -1767,75 +2596,91 @@ static int whereLoopXfer(sqlite3 *db, WhereLoop *pTo, WhereLoop *pFrom){ ** Delete a WhereLoop object */ static void whereLoopDelete(sqlite3 *db, WhereLoop *p){ + assert( db!=0 ); whereLoopClear(db, p); - sqlite3DbFree(db, p); + sqlite3DbNNFreeNN(db, p); } /* ** Free a WhereInfo structure */ static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){ - if( ALWAYS(pWInfo) ){ - int i; - for(i=0; i<pWInfo->nLevel; i++){ - WhereLevel *pLevel = &pWInfo->a[i]; - if( pLevel->pWLoop && (pLevel->pWLoop->wsFlags & WHERE_IN_ABLE) ){ - sqlite3DbFree(db, pLevel->u.in.aInLoop); - } - } - sqlite3WhereClauseClear(&pWInfo->sWC); - while( pWInfo->pLoops ){ - WhereLoop *p = pWInfo->pLoops; - pWInfo->pLoops = p->pNextLoop; - whereLoopDelete(db, p); - } - sqlite3DbFree(db, pWInfo); + assert( pWInfo!=0 ); + assert( db!=0 ); + sqlite3WhereClauseClear(&pWInfo->sWC); + while( pWInfo->pLoops ){ + WhereLoop *p = pWInfo->pLoops; + pWInfo->pLoops = p->pNextLoop; + whereLoopDelete(db, p); + } + while( pWInfo->pMemToFree ){ + WhereMemBlock *pNext = pWInfo->pMemToFree->pNext; + sqlite3DbNNFreeNN(db, pWInfo->pMemToFree); + pWInfo->pMemToFree = pNext; } + sqlite3DbNNFreeNN(db, pWInfo); } /* -** Return TRUE if all of the following are true: +** Return TRUE if X is a proper subset of Y but is of equal or less cost. +** In other words, return true if all constraints of X are also part of Y +** and Y has additional constraints that might speed the search that X lacks +** but the cost of running X is not more than the cost of running Y. +** +** In other words, return true if the cost relationship between X and Y +** is inverted and needs to be adjusted. +** +** Case 1: ** -** (1) X has the same or lower cost that Y -** (2) X is a proper subset of Y -** (3) X skips at least as many columns as Y +** (1a) X and Y use the same index. +** (1b) X has fewer == terms than Y +** (1c) Neither X nor Y use skip-scan +** (1d) X does not have a a greater cost than Y ** -** By "proper subset" we mean that X uses fewer WHERE clause terms -** than Y and that every WHERE clause term used by X is also used -** by Y. +** Case 2: ** -** If X is a proper subset of Y then Y is a better choice and ought -** to have a lower cost. This routine returns TRUE when that cost -** relationship is inverted and needs to be adjusted. The third rule -** was added because if X uses skip-scan less than Y it still might -** deserve a lower cost even if it is a proper subset of Y. +** (2a) X has the same or lower cost, or returns the same or fewer rows, +** than Y. +** (2b) X uses fewer WHERE clause terms than Y +** (2c) Every WHERE clause term used by X is also used by Y +** (2d) X skips at least as many columns as Y +** (2e) If X is a covering index, than Y is too */ static int whereLoopCheaperProperSubset( const WhereLoop *pX, /* First WhereLoop to compare */ const WhereLoop *pY /* Compare against this WhereLoop */ ){ int i, j; - if( pX->nLTerm-pX->nSkip >= pY->nLTerm-pY->nSkip ){ - return 0; /* X is not a subset of Y */ + if( pX->rRun>pY->rRun && pX->nOut>pY->nOut ) return 0; /* (1d) and (2a) */ + assert( (pX->wsFlags & WHERE_VIRTUALTABLE)==0 ); + assert( (pY->wsFlags & WHERE_VIRTUALTABLE)==0 ); + if( pX->u.btree.nEq < pY->u.btree.nEq /* (1b) */ + && pX->u.btree.pIndex==pY->u.btree.pIndex /* (1a) */ + && pX->nSkip==0 && pY->nSkip==0 /* (1c) */ + ){ + return 1; /* Case 1 is true */ } - if( pY->nSkip > pX->nSkip ) return 0; - if( pX->rRun >= pY->rRun ){ - if( pX->rRun > pY->rRun ) return 0; /* X costs more than Y */ - if( pX->nOut > pY->nOut ) return 0; /* X costs more than Y */ + if( pX->nLTerm-pX->nSkip >= pY->nLTerm-pY->nSkip ){ + return 0; /* (2b) */ } + if( pY->nSkip > pX->nSkip ) return 0; /* (2d) */ for(i=pX->nLTerm-1; i>=0; i--){ if( pX->aLTerm[i]==0 ) continue; for(j=pY->nLTerm-1; j>=0; j--){ if( pY->aLTerm[j]==pX->aLTerm[i] ) break; } - if( j<0 ) return 0; /* X not a subset of Y since term X[i] not used by Y */ + if( j<0 ) return 0; /* (2c) */ + } + if( (pX->wsFlags&WHERE_IDX_ONLY)!=0 + && (pY->wsFlags&WHERE_IDX_ONLY)==0 ){ + return 0; /* (2e) */ } - return 1; /* All conditions meet */ + return 1; /* Case 2 is true */ } /* -** Try to adjust the cost of WhereLoop pTemplate upwards or downwards so -** that: +** Try to adjust the cost and number of output rows of WhereLoop pTemplate +** upwards or downwards so that: ** ** (1) pTemplate costs less than any other WhereLoops that are a proper ** subset of pTemplate @@ -1853,35 +2698,40 @@ static void whereLoopAdjustCost(const WhereLoop *p, WhereLoop *pTemplate){ if( p->iTab!=pTemplate->iTab ) continue; if( (p->wsFlags & WHERE_INDEXED)==0 ) continue; if( whereLoopCheaperProperSubset(p, pTemplate) ){ - /* Adjust pTemplate cost downward so that it is cheaper than its + /* Adjust pTemplate cost downward so that it is cheaper than its ** subset p. */ WHERETRACE(0x80,("subset cost adjustment %d,%d to %d,%d\n", - pTemplate->rRun, pTemplate->nOut, p->rRun, p->nOut-1)); - pTemplate->rRun = p->rRun; - pTemplate->nOut = p->nOut - 1; + pTemplate->rRun, pTemplate->nOut, + MIN(p->rRun, pTemplate->rRun), + MIN(p->nOut - 1, pTemplate->nOut))); + pTemplate->rRun = MIN(p->rRun, pTemplate->rRun); + pTemplate->nOut = MIN(p->nOut - 1, pTemplate->nOut); }else if( whereLoopCheaperProperSubset(pTemplate, p) ){ /* Adjust pTemplate cost upward so that it is costlier than p since ** pTemplate is a proper subset of p */ WHERETRACE(0x80,("subset cost adjustment %d,%d to %d,%d\n", - pTemplate->rRun, pTemplate->nOut, p->rRun, p->nOut+1)); - pTemplate->rRun = p->rRun; - pTemplate->nOut = p->nOut + 1; + pTemplate->rRun, pTemplate->nOut, + MAX(p->rRun, pTemplate->rRun), + MAX(p->nOut + 1, pTemplate->nOut))); + pTemplate->rRun = MAX(p->rRun, pTemplate->rRun); + pTemplate->nOut = MAX(p->nOut + 1, pTemplate->nOut); } } } /* ** Search the list of WhereLoops in *ppPrev looking for one that can be -** supplanted by pTemplate. +** replaced by pTemplate. ** -** Return NULL if the WhereLoop list contains an entry that can supplant -** pTemplate, in other words if pTemplate does not belong on the list. +** Return NULL if pTemplate does not belong on the WhereLoop list. +** In other words if pTemplate ought to be dropped from further consideration. ** -** If pX is a WhereLoop that pTemplate can supplant, then return the +** If pX is a WhereLoop that pTemplate can replace, then return the ** link that points to pX. ** -** If pTemplate cannot supplant any existing element of the list but needs -** to be added to the list, then return a pointer to the tail of the list. +** If pTemplate cannot replace any existing element of the list but needs +** to be added to the list as a new entry, then return a pointer to the +** tail of the list. */ static WhereLoop **whereLoopFindLesser( WhereLoop **ppPrev, @@ -1898,7 +2748,7 @@ static WhereLoop **whereLoopFindLesser( /* In the current implementation, the rSetup value is either zero ** or the cost of building an automatic index (NlogN) and the NlogN ** is the same for compatible WhereLoops. */ - assert( p->rSetup==0 || pTemplate->rSetup==0 + assert( p->rSetup==0 || pTemplate->rSetup==0 || p->rSetup==pTemplate->rSetup ); /* whereLoopAddBtree() always generates and inserts the automatic index @@ -1906,7 +2756,7 @@ static WhereLoop **whereLoopFindLesser( ** rSetup. Call this SETUP-INVARIANT */ assert( p->rSetup>=pTemplate->rSetup ); - /* Any loop using an appliation-defined index (or PRIMARY KEY or + /* Any loop using an application-defined index (or PRIMARY KEY or ** UNIQUE constraint) with one or more == constraints is better ** than an automatic index. Unless it is a skip-scan. */ if( (p->wsFlags & WHERE_AUTO_INDEX)!=0 @@ -1933,7 +2783,7 @@ static WhereLoop **whereLoopFindLesser( /* If pTemplate is always better than p, then cause p to be overwritten ** with pTemplate. pTemplate is better than p if: - ** (1) pTemplate has no more dependences than p, and + ** (1) pTemplate has no more dependencies than p, and ** (2) pTemplate has an equal or lower cost than p. */ if( (p->prereq & pTemplate->prereq)==pTemplate->prereq /* (1) */ @@ -1963,7 +2813,7 @@ static WhereLoop **whereLoopFindLesser( ** ** When accumulating multiple loops (when pBuilder->pOrSet is NULL) we ** still might overwrite similar loops with the new template if the -** new template is better. Loops may be overwritten if the following +** new template is better. Loops may be overwritten if the following ** conditions are met: ** ** (1) They have the same iTab. @@ -1975,6 +2825,17 @@ static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){ WhereLoop **ppPrev, *p; WhereInfo *pWInfo = pBuilder->pWInfo; sqlite3 *db = pWInfo->pParse->db; + int rc; + + /* Stop the search once we hit the query planner search limit */ + if( pBuilder->iPlanLimit==0 ){ + WHERETRACE(0xffffffff,("=== query planner search limit reached ===\n")); + if( pBuilder->pOrSet ) pBuilder->pOrSet->n = 0; + return SQLITE_DONE; + } + pBuilder->iPlanLimit--; + + whereLoopAdjustCost(pWInfo->pLoops, pTemplate); /* If pBuilder->pOrSet is defined, then only keep track of the costs ** and prereqs. @@ -1990,7 +2851,7 @@ static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){ #if WHERETRACE_ENABLED /* 0x8 */ if( sqlite3WhereTrace & 0x8 ){ sqlite3DebugPrintf(x?" or-%d: ":" or-X: ", n); - whereLoopPrint(pTemplate, pBuilder->pWC); + sqlite3WhereLoopPrint(pTemplate, pBuilder->pWC); } #endif } @@ -1999,7 +2860,6 @@ static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){ /* Look for an existing WhereLoop to replace with pTemplate */ - whereLoopAdjustCost(pWInfo->pLoops, pTemplate); ppPrev = whereLoopFindLesser(&pWInfo->pLoops, pTemplate); if( ppPrev==0 ){ @@ -2008,10 +2868,10 @@ static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){ #if WHERETRACE_ENABLED /* 0x8 */ if( sqlite3WhereTrace & 0x8 ){ sqlite3DebugPrintf(" skip: "); - whereLoopPrint(pTemplate, pBuilder->pWC); + sqlite3WhereLoopPrint(pTemplate, pBuilder->pWC); } #endif - return SQLITE_OK; + return SQLITE_OK; }else{ p = *ppPrev; } @@ -2024,22 +2884,24 @@ static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){ if( sqlite3WhereTrace & 0x8 ){ if( p!=0 ){ sqlite3DebugPrintf("replace: "); - whereLoopPrint(p, pBuilder->pWC); + sqlite3WhereLoopPrint(p, pBuilder->pWC); + sqlite3DebugPrintf(" with: "); + }else{ + sqlite3DebugPrintf(" add: "); } - sqlite3DebugPrintf(" add: "); - whereLoopPrint(pTemplate, pBuilder->pWC); + sqlite3WhereLoopPrint(pTemplate, pBuilder->pWC); } #endif if( p==0 ){ /* Allocate a new WhereLoop to add to the end of the list */ *ppPrev = p = sqlite3DbMallocRawNN(db, sizeof(WhereLoop)); - if( p==0 ) return SQLITE_NOMEM; + if( p==0 ) return SQLITE_NOMEM_BKPT; whereLoopInit(p); p->pNextLoop = 0; }else{ /* We will be overwriting WhereLoop p[]. But before we do, first ** go through the rest of the list and delete any other entries besides - ** p[] that are also supplated by pTemplate */ + ** p[] that are also supplanted by pTemplate */ WhereLoop **ppTail = &p->pNextLoop; WhereLoop *pToDel; while( *ppTail ){ @@ -2051,20 +2913,20 @@ static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){ #if WHERETRACE_ENABLED /* 0x8 */ if( sqlite3WhereTrace & 0x8 ){ sqlite3DebugPrintf(" delete: "); - whereLoopPrint(pToDel, pBuilder->pWC); + sqlite3WhereLoopPrint(pToDel, pBuilder->pWC); } #endif whereLoopDelete(db, pToDel); } } - whereLoopXfer(db, p, pTemplate); + rc = whereLoopXfer(db, p, pTemplate); if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){ Index *pIndex = p->u.btree.pIndex; - if( pIndex && pIndex->tnum==0 ){ + if( pIndex && pIndex->idxType==SQLITE_IDXTYPE_IPK ){ p->u.btree.pIndex = 0; } } - return SQLITE_OK; + return rc; } /* @@ -2103,14 +2965,15 @@ static void whereLoopOutputAdjust( ){ WhereTerm *pTerm, *pX; Bitmask notAllowed = ~(pLoop->prereq|pLoop->maskSelf); - int i, j, k; + int i, j; LogEst iReduce = 0; /* pLoop->nOut should not exceed nRow-iReduce */ assert( (pLoop->wsFlags & WHERE_AUTO_INDEX)==0 ); - for(i=pWC->nTerm, pTerm=pWC->a; i>0; i--, pTerm++){ - if( (pTerm->wtFlags & TERM_VIRTUAL)!=0 ) break; - if( (pTerm->prereqAll & pLoop->maskSelf)==0 ) continue; + for(i=pWC->nBase, pTerm=pWC->a; i>0; i--, pTerm++){ + assert( pTerm!=0 ); if( (pTerm->prereqAll & notAllowed)!=0 ) continue; + if( (pTerm->prereqAll & pLoop->maskSelf)==0 ) continue; + if( (pTerm->wtFlags & TERM_VIRTUAL)!=0 ) continue; for(j=pLoop->nLTerm-1; j>=0; j--){ pX = pLoop->aLTerm[j]; if( pX==0 ) continue; @@ -2118,6 +2981,24 @@ static void whereLoopOutputAdjust( if( pX->iParent>=0 && (&pWC->a[pX->iParent])==pTerm ) break; } if( j<0 ){ + sqlite3ProgressCheck(pWC->pWInfo->pParse); + if( pLoop->maskSelf==pTerm->prereqAll ){ + /* If there are extra terms in the WHERE clause not used by an index + ** that depend only on the table being scanned, and that will tend to + ** cause many rows to be omitted, then mark that table as + ** "self-culling". + ** + ** 2022-03-24: Self-culling only applies if either the extra terms + ** are straight comparison operators that are non-true with NULL + ** operand, or if the loop is not an OUTER JOIN. + */ + if( (pTerm->eOperator & 0x3f)!=0 + || (pWC->pWInfo->pTabList->a[pLoop->iTab].fg.jointype + & (JT_LEFT|JT_LTORJ))==0 + ){ + pLoop->wsFlags |= WHERE_SELFCULL; + } + } if( pTerm->truthProb<=0 ){ /* If a truth probability is specified using the likelihood() hints, ** then use the probability provided by the application. */ @@ -2126,24 +3007,101 @@ static void whereLoopOutputAdjust( /* In the absence of explicit truth probabilities, use heuristics to ** guess a reasonable truth probability. */ pLoop->nOut--; - if( pTerm->eOperator&(WO_EQ|WO_IS) ){ + if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0 + && (pTerm->wtFlags & TERM_HIGHTRUTH)==0 /* tag-20200224-1 */ + ){ Expr *pRight = pTerm->pExpr->pRight; + int k = 0; testcase( pTerm->pExpr->op==TK_IS ); - if( sqlite3ExprIsInteger(pRight, &k) && k>=(-1) && k<=1 ){ + if( sqlite3ExprIsInteger(pRight, &k, 0) && k>=(-1) && k<=1 ){ k = 10; }else{ k = 20; } - if( iReduce<k ) iReduce = k; + if( iReduce<k ){ + pTerm->wtFlags |= TERM_HEURTRUTH; + iReduce = k; + } } } } } - if( pLoop->nOut > nRow-iReduce ) pLoop->nOut = nRow - iReduce; + if( pLoop->nOut > nRow-iReduce ){ + pLoop->nOut = nRow - iReduce; + } +} + +/* +** Term pTerm is a vector range comparison operation. The first comparison +** in the vector can be optimized using column nEq of the index. This +** function returns the total number of vector elements that can be used +** as part of the range comparison. +** +** For example, if the query is: +** +** WHERE a = ? AND (b, c, d) > (?, ?, ?) +** +** and the index: +** +** CREATE INDEX ... ON (a, b, c, d, e) +** +** then this function would be invoked with nEq=1. The value returned in +** this case is 3. +*/ +static int whereRangeVectorLen( + Parse *pParse, /* Parsing context */ + int iCur, /* Cursor open on pIdx */ + Index *pIdx, /* The index to be used for a inequality constraint */ + int nEq, /* Number of prior equality constraints on same index */ + WhereTerm *pTerm /* The vector inequality constraint */ +){ + int nCmp = sqlite3ExprVectorSize(pTerm->pExpr->pLeft); + int i; + + nCmp = MIN(nCmp, (pIdx->nColumn - nEq)); + for(i=1; i<nCmp; i++){ + /* Test if comparison i of pTerm is compatible with column (i+nEq) + ** of the index. If not, exit the loop. */ + char aff; /* Comparison affinity */ + char idxaff = 0; /* Indexed columns affinity */ + CollSeq *pColl; /* Comparison collation sequence */ + Expr *pLhs, *pRhs; + + assert( ExprUseXList(pTerm->pExpr->pLeft) ); + pLhs = pTerm->pExpr->pLeft->x.pList->a[i].pExpr; + pRhs = pTerm->pExpr->pRight; + if( ExprUseXSelect(pRhs) ){ + pRhs = pRhs->x.pSelect->pEList->a[i].pExpr; + }else{ + pRhs = pRhs->x.pList->a[i].pExpr; + } + + /* Check that the LHS of the comparison is a column reference to + ** the right column of the right source table. And that the sort + ** order of the index column is the same as the sort order of the + ** leftmost index column. */ + if( pLhs->op!=TK_COLUMN + || pLhs->iTable!=iCur + || pLhs->iColumn!=pIdx->aiColumn[i+nEq] + || pIdx->aSortOrder[i+nEq]!=pIdx->aSortOrder[nEq] + ){ + break; + } + + testcase( pLhs->iColumn==XN_ROWID ); + aff = sqlite3CompareAffinity(pRhs, sqlite3ExprAffinity(pLhs)); + idxaff = sqlite3TableColumnAffinity(pIdx->pTable, pLhs->iColumn); + if( aff!=idxaff ) break; + + pColl = sqlite3BinaryCompareCollSeq(pParse, pLhs, pRhs); + if( pColl==0 ) break; + if( sqlite3StrICmp(pColl->zName, pIdx->azColl[i+nEq]) ) break; + } + return i; } /* -** Adjust the cost C by the costMult facter T. This only occurs if +** Adjust the cost C by the costMult factor T. This only occurs if ** compiled with -DSQLITE_ENABLE_COSTMULT */ #ifdef SQLITE_ENABLE_COSTMULT @@ -2153,24 +3111,24 @@ static void whereLoopOutputAdjust( #endif /* -** We have so far matched pBuilder->pNew->u.btree.nEq terms of the +** We have so far matched pBuilder->pNew->u.btree.nEq terms of the ** index pIndex. Try to match one more. ** -** When this function is called, pBuilder->pNew->nOut contains the -** number of rows expected to be visited by filtering using the nEq -** terms only. If it is modified, this value is restored before this +** When this function is called, pBuilder->pNew->nOut contains the +** number of rows expected to be visited by filtering using the nEq +** terms only. If it is modified, this value is restored before this ** function returns. ** -** If pProbe->tnum==0, that means pIndex is a fake index used for the -** INTEGER PRIMARY KEY. +** If pProbe->idxType==SQLITE_IDXTYPE_IPK, that means pIndex is +** a fake index used for the INTEGER PRIMARY KEY. */ static int whereLoopAddBtreeIndex( WhereLoopBuilder *pBuilder, /* The WhereLoop factory */ - struct SrcList_item *pSrc, /* FROM clause term being analyzed */ + SrcItem *pSrc, /* FROM clause term being analyzed */ Index *pProbe, /* An index on pSrc */ LogEst nInMul /* log(Number of iterations due to IN) */ ){ - WhereInfo *pWInfo = pBuilder->pWInfo; /* WHERE analyse context */ + WhereInfo *pWInfo = pBuilder->pWInfo; /* WHERE analyze context */ Parse *pParse = pWInfo->pParse; /* Parsing context */ sqlite3 *db = pParse->db; /* Database connection malloc context */ WhereLoop *pNew; /* Template WhereLoop under construction */ @@ -2180,6 +3138,8 @@ static int whereLoopAddBtreeIndex( Bitmask saved_prereq; /* Original value of pNew->prereq */ u16 saved_nLTerm; /* Original value of pNew->nLTerm */ u16 saved_nEq; /* Original value of pNew->u.btree.nEq */ + u16 saved_nBtm; /* Original value of pNew->u.btree.nBtm */ + u16 saved_nTop; /* Original value of pNew->u.btree.nTop */ u16 saved_nSkip; /* Original value of pNew->nSkip */ u32 saved_wsFlags; /* Original value of pNew->wsFlags */ LogEst saved_nOut; /* Original value of pNew->nOut */ @@ -2189,22 +3149,33 @@ static int whereLoopAddBtreeIndex( WhereTerm *pTop = 0, *pBtm = 0; /* Top and bottom range constraints */ pNew = pBuilder->pNew; - if( db->mallocFailed ) return SQLITE_NOMEM; + assert( db->mallocFailed==0 || pParse->nErr>0 ); + if( pParse->nErr ){ + return pParse->rc; + } + WHERETRACE(0x800, ("BEGIN %s.addBtreeIdx(%s), nEq=%d, nSkip=%d, rRun=%d\n", + pProbe->pTable->zName,pProbe->zName, + pNew->u.btree.nEq, pNew->nSkip, pNew->rRun)); assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 ); assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 ); if( pNew->wsFlags & WHERE_BTM_LIMIT ){ opMask = WO_LT|WO_LE; - }else if( /*pProbe->tnum<=0 ||*/ (pSrc->fg.jointype & JT_LEFT)!=0 ){ - opMask = WO_EQ|WO_IN|WO_GT|WO_GE|WO_LT|WO_LE; }else{ + assert( pNew->u.btree.nBtm==0 ); opMask = WO_EQ|WO_IN|WO_GT|WO_GE|WO_LT|WO_LE|WO_ISNULL|WO_IS; } - if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE); + if( pProbe->bUnordered ){ + opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE); + } assert( pNew->u.btree.nEq<pProbe->nColumn ); + assert( pNew->u.btree.nEq<pProbe->nKeyCol + || pProbe->idxType!=SQLITE_IDXTYPE_PRIMARYKEY ); saved_nEq = pNew->u.btree.nEq; + saved_nBtm = pNew->u.btree.nBtm; + saved_nTop = pNew->u.btree.nTop; saved_nSkip = pNew->nSkip; saved_nLTerm = pNew->nLTerm; saved_wsFlags = pNew->wsFlags; @@ -2220,7 +3191,7 @@ static int whereLoopAddBtreeIndex( LogEst rCostIdx; LogEst nOutUnadjusted; /* nOut before IN() and WHERE adjustments */ int nIn = 0; -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 +#ifdef SQLITE_ENABLE_STAT4 int nRecValid = pBuilder->nRecValid; #endif if( (eOp==WO_ISNULL || (pTerm->wtFlags&TERM_VNULL)!=0) @@ -2234,82 +3205,175 @@ static int whereLoopAddBtreeIndex( ** to mix with a lower range bound from some other source */ if( pTerm->wtFlags & TERM_LIKEOPT && pTerm->eOperator==WO_LT ) continue; + if( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))!=0 + && !constraintCompatibleWithOuterJoin(pTerm,pSrc) + ){ + continue; + } + if( IsUniqueIndex(pProbe) && saved_nEq==pProbe->nKeyCol-1 ){ + pBuilder->bldFlags1 |= SQLITE_BLDF1_UNIQUE; + }else{ + pBuilder->bldFlags1 |= SQLITE_BLDF1_INDEXED; + } pNew->wsFlags = saved_wsFlags; pNew->u.btree.nEq = saved_nEq; + pNew->u.btree.nBtm = saved_nBtm; + pNew->u.btree.nTop = saved_nTop; pNew->nLTerm = saved_nLTerm; - if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */ + if( pNew->nLTerm>=pNew->nLSlot + && whereLoopResize(db, pNew, pNew->nLTerm+1) + ){ + break; /* OOM while trying to enlarge the pNew->aLTerm array */ + } pNew->aLTerm[pNew->nLTerm++] = pTerm; pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf; assert( nInMul==0 - || (pNew->wsFlags & WHERE_COLUMN_NULL)!=0 - || (pNew->wsFlags & WHERE_COLUMN_IN)!=0 - || (pNew->wsFlags & WHERE_SKIPSCAN)!=0 + || (pNew->wsFlags & WHERE_COLUMN_NULL)!=0 + || (pNew->wsFlags & WHERE_COLUMN_IN)!=0 + || (pNew->wsFlags & WHERE_SKIPSCAN)!=0 ); if( eOp & WO_IN ){ Expr *pExpr = pTerm->pExpr; - pNew->wsFlags |= WHERE_COLUMN_IN; - if( ExprHasProperty(pExpr, EP_xIsSelect) ){ + if( ExprUseXSelect(pExpr) ){ /* "x IN (SELECT ...)": TUNING: the SELECT returns 25 rows */ + int i; + int bRedundant = 0; nIn = 46; assert( 46==sqlite3LogEst(25) ); + + /* The expression may actually be of the form (x, y) IN (SELECT...). + ** In this case there is a separate term for each of (x) and (y). + ** However, the nIn multiplier should only be applied once, not once + ** for each such term. The following loop checks that pTerm is the + ** first such term in use, and sets nIn back to 0 if it is not. */ + for(i=0; i<pNew->nLTerm-1; i++){ + if( pNew->aLTerm[i] && pNew->aLTerm[i]->pExpr==pExpr ){ + nIn = 0; + if( pNew->aLTerm[i]->u.x.iField == pTerm->u.x.iField ){ + /* Detect when two or more columns of an index match the same + ** column of a vector IN operater, and avoid adding the column + ** to the WhereLoop more than once. See tag-20250707-01 + ** in test/rowvalue.test */ + bRedundant = 1; + } + } + } + if( bRedundant ){ + pNew->nLTerm--; + continue; + } }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){ /* "x IN (value, value, ...)" */ nIn = sqlite3LogEst(pExpr->x.pList->nExpr); } - assert( nIn>0 ); /* RHS always has 2 or more terms... The parser - ** changes "x IN (?)" into "x=?". */ - + if( pProbe->hasStat1 && rLogSize>=10 ){ + LogEst M, logK, x; + /* Let: + ** N = the total number of rows in the table + ** K = the number of entries on the RHS of the IN operator + ** M = the number of rows in the table that match terms to the + ** to the left in the same index. If the IN operator is on + ** the left-most index column, M==N. + ** + ** Given the definitions above, it is better to omit the IN operator + ** from the index lookup and instead do a scan of the M elements, + ** testing each scanned row against the IN operator separately, if: + ** + ** M*log(K) < K*log(N) + ** + ** Our estimates for M, K, and N might be inaccurate, so we build in + ** a safety margin of 2 (LogEst: 10) that favors using the IN operator + ** with the index, as using an index has better worst-case behavior. + ** If we do not have real sqlite_stat1 data, always prefer to use + ** the index. Do not bother with this optimization on very small + ** tables (less than 2 rows) as it is pointless in that case. + */ + M = pProbe->aiRowLogEst[saved_nEq]; + logK = estLog(nIn); + /* TUNING v----- 10 to bias toward indexed IN */ + x = M + logK + 10 - (nIn + rLogSize); + if( x>=0 ){ + WHERETRACE(0x40, + ("IN operator (N=%d M=%d logK=%d nIn=%d rLogSize=%d x=%d) " + "prefers indexed lookup\n", + saved_nEq, M, logK, nIn, rLogSize, x)); + }else if( nInMul<2 && OptimizationEnabled(db, SQLITE_SeekScan) ){ + WHERETRACE(0x40, + ("IN operator (N=%d M=%d logK=%d nIn=%d rLogSize=%d x=%d" + " nInMul=%d) prefers skip-scan\n", + saved_nEq, M, logK, nIn, rLogSize, x, nInMul)); + pNew->wsFlags |= WHERE_IN_SEEKSCAN; + }else{ + WHERETRACE(0x40, + ("IN operator (N=%d M=%d logK=%d nIn=%d rLogSize=%d x=%d" + " nInMul=%d) prefers normal scan\n", + saved_nEq, M, logK, nIn, rLogSize, x, nInMul)); + continue; + } + } + pNew->wsFlags |= WHERE_COLUMN_IN; }else if( eOp & (WO_EQ|WO_IS) ){ int iCol = pProbe->aiColumn[saved_nEq]; pNew->wsFlags |= WHERE_COLUMN_EQ; assert( saved_nEq==pNew->u.btree.nEq ); - if( iCol==XN_ROWID - || (iCol>0 && nInMul==0 && saved_nEq==pProbe->nKeyCol-1) + if( iCol==XN_ROWID + || (iCol>=0 && nInMul==0 && saved_nEq==pProbe->nKeyCol-1) ){ - if( iCol>=0 && pProbe->uniqNotNull==0 ){ - pNew->wsFlags |= WHERE_UNQ_WANTED; - }else{ + if( iCol==XN_ROWID || pProbe->uniqNotNull + || (pProbe->nKeyCol==1 && pProbe->onError && (eOp & WO_EQ)) + ){ pNew->wsFlags |= WHERE_ONEROW; + }else{ + pNew->wsFlags |= WHERE_UNQ_WANTED; } } + if( scan.iEquiv>1 ) pNew->wsFlags |= WHERE_TRANSCONS; }else if( eOp & WO_ISNULL ){ pNew->wsFlags |= WHERE_COLUMN_NULL; - }else if( eOp & (WO_GT|WO_GE) ){ - testcase( eOp & WO_GT ); - testcase( eOp & WO_GE ); - pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT; - pBtm = pTerm; - pTop = 0; - if( pTerm->wtFlags & TERM_LIKEOPT ){ - /* Range contraints that come from the LIKE optimization are - ** always used in pairs. */ - pTop = &pTerm[1]; - assert( (pTop-(pTerm->pWC->a))<pTerm->pWC->nTerm ); - assert( pTop->wtFlags & TERM_LIKEOPT ); - assert( pTop->eOperator==WO_LT ); - if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */ - pNew->aLTerm[pNew->nLTerm++] = pTop; - pNew->wsFlags |= WHERE_TOP_LIMIT; - } }else{ - assert( eOp & (WO_LT|WO_LE) ); - testcase( eOp & WO_LT ); - testcase( eOp & WO_LE ); - pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT; - pTop = pTerm; - pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ? - pNew->aLTerm[pNew->nLTerm-2] : 0; + int nVecLen = whereRangeVectorLen( + pParse, pSrc->iCursor, pProbe, saved_nEq, pTerm + ); + if( eOp & (WO_GT|WO_GE) ){ + testcase( eOp & WO_GT ); + testcase( eOp & WO_GE ); + pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT; + pNew->u.btree.nBtm = nVecLen; + pBtm = pTerm; + pTop = 0; + if( pTerm->wtFlags & TERM_LIKEOPT ){ + /* Range constraints that come from the LIKE optimization are + ** always used in pairs. */ + pTop = &pTerm[1]; + assert( (pTop-(pTerm->pWC->a))<pTerm->pWC->nTerm ); + assert( pTop->wtFlags & TERM_LIKEOPT ); + assert( pTop->eOperator==WO_LT ); + if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */ + pNew->aLTerm[pNew->nLTerm++] = pTop; + pNew->wsFlags |= WHERE_TOP_LIMIT; + pNew->u.btree.nTop = 1; + } + }else{ + assert( eOp & (WO_LT|WO_LE) ); + testcase( eOp & WO_LT ); + testcase( eOp & WO_LE ); + pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT; + pNew->u.btree.nTop = nVecLen; + pTop = pTerm; + pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ? + pNew->aLTerm[pNew->nLTerm-2] : 0; + } } /* At this point pNew->nOut is set to the number of rows expected to ** be visited by the index scan before considering term pTerm, or the - ** values of nIn and nInMul. In other words, assuming that all + ** values of nIn and nInMul. In other words, assuming that all ** "x IN(...)" terms are replaced with "x = ?". This block updates ** the value of pNew->nOut to account for pTerm (but not nIn/nInMul). */ assert( pNew->nOut==saved_nOut ); if( pNew->wsFlags & WHERE_COLUMN_RANGE ){ - /* Adjust nOut using stat3/stat4 data. Or, if there is no stat3/stat4 + /* Adjust nOut using stat4 data. Or, if there is no stat4 ** data, using some other estimate. */ whereRangeScanEst(pParse, pBuilder, pBtm, pTop, pNew); }else{ @@ -2323,12 +3387,13 @@ static int whereLoopAddBtreeIndex( pNew->nOut += pTerm->truthProb; pNew->nOut -= nIn; }else{ -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 +#ifdef SQLITE_ENABLE_STAT4 tRowcnt nOut = 0; - if( nInMul==0 - && pProbe->nSample - && pNew->u.btree.nEq<=pProbe->nSampleCol - && ((eOp & WO_IN)==0 || !ExprHasProperty(pTerm->pExpr, EP_xIsSelect)) + if( nInMul==0 + && pProbe->nSample + && ALWAYS(pNew->u.btree.nEq<=pProbe->nSampleCol) + && ((eOp & WO_IN)==0 || ExprUseXList(pTerm->pExpr)) + && OptimizationEnabled(db, SQLITE_Stat4) ){ Expr *pExpr = pTerm->pExpr; if( (eOp & (WO_EQ|WO_ISNULL|WO_IS))!=0 ){ @@ -2343,6 +3408,27 @@ static int whereLoopAddBtreeIndex( if( rc!=SQLITE_OK ) break; /* Jump out of the pTerm loop */ if( nOut ){ pNew->nOut = sqlite3LogEst(nOut); + if( nEq==1 + /* TUNING: Mark terms as "low selectivity" if they seem likely + ** to be true for half or more of the rows in the table. + ** See tag-202002240-1 */ + && pNew->nOut+10 > pProbe->aiRowLogEst[0] + ){ +#if WHERETRACE_ENABLED /* 0x01 */ + if( sqlite3WhereTrace & 0x20 ){ + sqlite3DebugPrintf( + "STAT4 determines term has low selectivity:\n"); + sqlite3WhereTermPrint(pTerm, 999); + } +#endif + pTerm->wtFlags |= TERM_HIGHTRUTH; + if( pTerm->wtFlags & TERM_HEURTRUTH ){ + /* If the term has previously been used with an assumption of + ** higher selectivity, then set the flag to rerun the + ** loop computations. */ + pBuilder->bldFlags2 |= SQLITE_BLDF2_2NDPASS; + } + } if( pNew->nOut>saved_nOut ) pNew->nOut = saved_nOut; pNew->nOut -= nIn; } @@ -2352,8 +3438,8 @@ static int whereLoopAddBtreeIndex( { pNew->nOut += (pProbe->aiRowLogEst[nEq] - pProbe->aiRowLogEst[nEq-1]); if( eOp & WO_ISNULL ){ - /* TUNING: If there is no likelihood() value, assume that a - ** "col IS NULL" expression matches twice as many rows + /* TUNING: If there is no likelihood() value, assume that a + ** "col IS NULL" expression matches twice as many rows ** as (col=?). */ pNew->nOut += 10; } @@ -2361,13 +3447,33 @@ static int whereLoopAddBtreeIndex( } } - /* Set rCostIdx to the cost of visiting selected rows in index. Add - ** it to pNew->rRun, which is currently set to the cost of the index - ** seek only. Then, if this is a non-covering index, add the cost of - ** visiting the rows in the main table. */ - rCostIdx = pNew->nOut + 1 + (15*pProbe->szIdxRow)/pSrc->pTab->szTabRow; - pNew->rRun = sqlite3LogEstAdd(rLogSize, rCostIdx); - if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){ + /* Set rCostIdx to the estimated cost of visiting selected rows in the + ** index. The estimate is the sum of two values: + ** 1. The cost of doing one search-by-key to find the first matching + ** entry + ** 2. Stepping forward in the index pNew->nOut times to find all + ** additional matching entries. + */ + assert( pSrc->pSTab->szTabRow>0 ); + if( pProbe->idxType==SQLITE_IDXTYPE_IPK ){ + /* The pProbe->szIdxRow is low for an IPK table since the interior + ** pages are small. Thus szIdxRow gives a good estimate of seek cost. + ** But the leaf pages are full-size, so pProbe->szIdxRow would badly + ** under-estimate the scanning cost. */ + rCostIdx = pNew->nOut + 16; + }else{ + rCostIdx = pNew->nOut + 1 + (15*pProbe->szIdxRow)/pSrc->pSTab->szTabRow; + } + rCostIdx = sqlite3LogEstAdd(rLogSize, rCostIdx); + + /* Estimate the cost of running the loop. If all data is coming + ** from the index, then this is just the cost of doing the index + ** lookup and scan. But if some data is coming out of the main table, + ** we also have to add in the cost of doing pNew->nOut searches to + ** locate the row in the main table that corresponds to the index entry. + */ + pNew->rRun = rCostIdx; + if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK|WHERE_EXPRIDX))==0 ){ pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut + 16); } ApplyCostMultiplier(pNew->rRun, pProbe->pTable->costMult); @@ -2386,16 +3492,23 @@ static int whereLoopAddBtreeIndex( if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 && pNew->u.btree.nEq<pProbe->nColumn + && (pNew->u.btree.nEq<pProbe->nKeyCol || + pProbe->idxType!=SQLITE_IDXTYPE_PRIMARYKEY) ){ + if( pNew->u.btree.nEq>3 ){ + sqlite3ProgressCheck(pParse); + } whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul+nIn); } pNew->nOut = saved_nOut; -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 +#ifdef SQLITE_ENABLE_STAT4 pBuilder->nRecValid = nRecValid; #endif } pNew->prereq = saved_prereq; pNew->u.btree.nEq = saved_nEq; + pNew->u.btree.nBtm = saved_nBtm; + pNew->u.btree.nTop = saved_nTop; pNew->nSkip = saved_nSkip; pNew->wsFlags = saved_wsFlags; pNew->nOut = saved_nOut; @@ -2403,19 +3516,23 @@ static int whereLoopAddBtreeIndex( /* Consider using a skip-scan if there are no WHERE clause constraints ** available for the left-most terms of the index, and if the average - ** number of repeats in the left-most terms is at least 18. + ** number of repeats in the left-most terms is at least 18. ** ** The magic number 18 is selected on the basis that scanning 17 rows ** is almost always quicker than an index seek (even though if the index ** contains fewer than 2^17 rows we assume otherwise in other parts of - ** the code). And, even if it is not, it should not be too much slower. + ** the code). And, even if it is not, it should not be too much slower. ** On the other hand, the extra seeks could end up being significantly ** more expensive. */ assert( 42==sqlite3LogEst(18) ); if( saved_nEq==saved_nSkip && saved_nEq+1<pProbe->nKeyCol + && saved_nEq==pNew->nLTerm && pProbe->noSkipScan==0 + && pProbe->hasStat1!=0 + && OptimizationEnabled(db, SQLITE_SkipScan) && pProbe->aiRowLogEst[saved_nEq+1]>=42 /* TUNING: Minimum for skip-scan */ + && pSrc->fg.fromExists==0 && (rc = whereLoopResize(db, pNew, pNew->nLTerm+1))==SQLITE_OK ){ LogEst nIter; @@ -2435,6 +3552,8 @@ static int whereLoopAddBtreeIndex( pNew->wsFlags = saved_wsFlags; } + WHERETRACE(0x800, ("END %s.addBtreeIdx(%s), nEq=%d, rc=%d\n", + pProbe->pTable->zName, pProbe->zName, saved_nEq, rc)); return rc; } @@ -2458,8 +3577,11 @@ static int indexMightHelpWithOrderBy( if( pIndex->bUnordered ) return 0; if( (pOB = pBuilder->pWInfo->pOrderBy)==0 ) return 0; for(ii=0; ii<pOB->nExpr; ii++){ - Expr *pExpr = sqlite3ExprSkipCollate(pOB->a[ii].pExpr); - if( pExpr->op==TK_COLUMN && pExpr->iTable==iCursor ){ + Expr *pExpr = sqlite3ExprSkipCollateAndLikely(pOB->a[ii].pExpr); + if( NEVER(pExpr==0) ) continue; + if( (pExpr->op==TK_COLUMN || pExpr->op==TK_AGG_COLUMN) + && pExpr->iTable==iCursor + ){ if( pExpr->iColumn<0 ) return 1; for(jj=0; jj<pIndex->nKeyCol; jj++){ if( pExpr->iColumn==pIndex->aiColumn[jj] ) return 1; @@ -2467,7 +3589,7 @@ static int indexMightHelpWithOrderBy( }else if( (aColExpr = pIndex->aColExpr)!=0 ){ for(jj=0; jj<pIndex->nKeyCol; jj++){ if( pIndex->aiColumn[jj]!=XN_EXPR ) continue; - if( sqlite3ExprCompare(pExpr,aColExpr->a[jj].pExpr,iCursor)==0 ){ + if( sqlite3ExprCompareSkip(pExpr,aColExpr->a[jj].pExpr,iCursor)==0 ){ return 1; } } @@ -2476,38 +3598,53 @@ static int indexMightHelpWithOrderBy( return 0; } -/* -** Return a bitmask where 1s indicate that the corresponding column of -** the table is used by an index. Only the first 63 columns are considered. -*/ -static Bitmask columnsInIndex(Index *pIdx){ - Bitmask m = 0; - int j; - for(j=pIdx->nColumn-1; j>=0; j--){ - int x = pIdx->aiColumn[j]; - if( x>=0 ){ - testcase( x==BMS-1 ); - testcase( x==BMS-2 ); - if( x<BMS-1 ) m |= MASKBIT(x); - } - } - return m; -} - /* Check to see if a partial index with pPartIndexWhere can be used ** in the current query. Return true if it can be and false if not. */ -static int whereUsablePartialIndex(int iTab, WhereClause *pWC, Expr *pWhere){ +static int whereUsablePartialIndex( + int iTab, /* The table for which we want an index */ + u8 jointype, /* The JT_* flags on the join */ + WhereClause *pWC, /* The WHERE clause of the query */ + Expr *pWhere /* The WHERE clause from the partial index */ +){ int i; WhereTerm *pTerm; + Parse *pParse; + + if( jointype & JT_LTORJ ) return 0; + pParse = pWC->pWInfo->pParse; while( pWhere->op==TK_AND ){ - if( !whereUsablePartialIndex(iTab,pWC,pWhere->pLeft) ) return 0; + if( !whereUsablePartialIndex(iTab,jointype,pWC,pWhere->pLeft) ) return 0; pWhere = pWhere->pRight; } for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){ - Expr *pExpr = pTerm->pExpr; - if( sqlite3ExprImpliesExpr(pExpr, pWhere, iTab) - && (!ExprHasProperty(pExpr, EP_FromJoin) || pExpr->iRightJoinTable==iTab) + Expr *pExpr; + pExpr = pTerm->pExpr; + if( (!ExprHasProperty(pExpr, EP_OuterON) || pExpr->w.iJoin==iTab) + && ((jointype & JT_OUTER)==0 || ExprHasProperty(pExpr, EP_OuterON)) + && sqlite3ExprImpliesExpr(pParse, pExpr, pWhere, iTab) + && !sqlite3ExprImpliesExpr(pParse, pExpr, pWhere, -1) + && (pTerm->wtFlags & TERM_VNULL)==0 + ){ + return 1; + } + } + return 0; +} + +/* +** pIdx is an index containing expressions. Check it see if any of the +** expressions in the index match the pExpr expression. +*/ +static int exprIsCoveredByIndex( + const Expr *pExpr, + const Index *pIdx, + int iTabCur +){ + int i; + for(i=0; i<pIdx->nColumn; i++){ + if( pIdx->aiColumn[i]==XN_EXPR + && sqlite3ExprCompare(0, pExpr, pIdx->aColExpr->a[i].pExpr, iTabCur)==0 ){ return 1; } @@ -2515,9 +3652,226 @@ static int whereUsablePartialIndex(int iTab, WhereClause *pWC, Expr *pWhere){ return 0; } +/* +** Structure passed to the whereIsCoveringIndex Walker callback. +*/ +typedef struct CoveringIndexCheck CoveringIndexCheck; +struct CoveringIndexCheck { + Index *pIdx; /* The index */ + int iTabCur; /* Cursor number for the corresponding table */ + u8 bExpr; /* Uses an indexed expression */ + u8 bUnidx; /* Uses an unindexed column not within an indexed expr */ +}; + +/* +** Information passed in is pWalk->u.pCovIdxCk. Call it pCk. +** +** If the Expr node references the table with cursor pCk->iTabCur, then +** make sure that column is covered by the index pCk->pIdx. We know that +** all columns less than 63 (really BMS-1) are covered, so we don't need +** to check them. But we do need to check any column at 63 or greater. +** +** If the index does not cover the column, then set pWalk->eCode to +** non-zero and return WRC_Abort to stop the search. +** +** If this node does not disprove that the index can be a covering index, +** then just return WRC_Continue, to continue the search. +** +** If pCk->pIdx contains indexed expressions and one of those expressions +** matches pExpr, then prune the search. +*/ +static int whereIsCoveringIndexWalkCallback(Walker *pWalk, Expr *pExpr){ + int i; /* Loop counter */ + const Index *pIdx; /* The index of interest */ + const i16 *aiColumn; /* Columns contained in the index */ + u16 nColumn; /* Number of columns in the index */ + CoveringIndexCheck *pCk; /* Info about this search */ + + pCk = pWalk->u.pCovIdxCk; + pIdx = pCk->pIdx; + if( (pExpr->op==TK_COLUMN || pExpr->op==TK_AGG_COLUMN) ){ + /* if( pExpr->iColumn<(BMS-1) && pIdx->bHasExpr==0 ) return WRC_Continue;*/ + if( pExpr->iTable!=pCk->iTabCur ) return WRC_Continue; + pIdx = pWalk->u.pCovIdxCk->pIdx; + aiColumn = pIdx->aiColumn; + nColumn = pIdx->nColumn; + for(i=0; i<nColumn; i++){ + if( aiColumn[i]==pExpr->iColumn ) return WRC_Continue; + } + pCk->bUnidx = 1; + return WRC_Abort; + }else if( pIdx->bHasExpr + && exprIsCoveredByIndex(pExpr, pIdx, pWalk->u.pCovIdxCk->iTabCur) ){ + pCk->bExpr = 1; + return WRC_Prune; + } + return WRC_Continue; +} + + +/* +** pIdx is an index that covers all of the low-number columns used by +** pWInfo->pSelect (columns from 0 through 62) or an index that has +** expressions terms. Hence, we cannot determine whether or not it is +** a covering index by using the colUsed bitmasks. We have to do a search +** to see if the index is covering. This routine does that search. +** +** The return value is one of these: +** +** 0 The index is definitely not a covering index +** +** WHERE_IDX_ONLY The index is definitely a covering index +** +** WHERE_EXPRIDX The index is likely a covering index, but it is +** difficult to determine precisely because of the +** expressions that are indexed. Score it as a +** covering index, but still keep the main table open +** just in case we need it. +** +** This routine is an optimization. It is always safe to return zero. +** But returning one of the other two values when zero should have been +** returned can lead to incorrect bytecode and assertion faults. +*/ +static SQLITE_NOINLINE u32 whereIsCoveringIndex( + WhereInfo *pWInfo, /* The WHERE clause context */ + Index *pIdx, /* Index that is being tested */ + int iTabCur /* Cursor for the table being indexed */ +){ + int i, rc; + struct CoveringIndexCheck ck; + Walker w; + if( pWInfo->pSelect==0 ){ + /* We don't have access to the full query, so we cannot check to see + ** if pIdx is covering. Assume it is not. */ + return 0; + } + if( pIdx->bHasExpr==0 ){ + for(i=0; i<pIdx->nColumn; i++){ + if( pIdx->aiColumn[i]>=BMS-1 ) break; + } + if( i>=pIdx->nColumn ){ + /* pIdx does not index any columns greater than 62, but we know from + ** colMask that columns greater than 62 are used, so this is not a + ** covering index */ + return 0; + } + } + ck.pIdx = pIdx; + ck.iTabCur = iTabCur; + ck.bExpr = 0; + ck.bUnidx = 0; + memset(&w, 0, sizeof(w)); + w.xExprCallback = whereIsCoveringIndexWalkCallback; + w.xSelectCallback = sqlite3SelectWalkNoop; + w.u.pCovIdxCk = &ck; + sqlite3WalkSelect(&w, pWInfo->pSelect); + if( ck.bUnidx ){ + rc = 0; + }else if( ck.bExpr ){ + rc = WHERE_EXPRIDX; + }else{ + rc = WHERE_IDX_ONLY; + } + return rc; +} + +/* +** This is an sqlite3ParserAddCleanup() callback that is invoked to +** free the Parse->pIdxEpr list when the Parse object is destroyed. +*/ +static void whereIndexedExprCleanup(sqlite3 *db, void *pObject){ + IndexedExpr **pp = (IndexedExpr**)pObject; + while( *pp!=0 ){ + IndexedExpr *p = *pp; + *pp = p->pIENext; + sqlite3ExprDelete(db, p->pExpr); + sqlite3DbFreeNN(db, p); + } +} + +/* +** This function is called for a partial index - one with a WHERE clause - in +** two scenarios. In both cases, it determines whether or not the WHERE +** clause on the index implies that a column of the table may be safely +** replaced by a constant expression. For example, in the following +** SELECT: +** +** CREATE INDEX i1 ON t1(b, c) WHERE a=<expr>; +** SELECT a, b, c FROM t1 WHERE a=<expr> AND b=?; +** +** The "a" in the select-list may be replaced by <expr>, iff: +** +** (a) <expr> is a constant expression, and +** (b) The (a=<expr>) comparison uses the BINARY collation sequence, and +** (c) Column "a" has an affinity other than NONE or BLOB. +** +** If argument pItem is NULL, then pMask must not be NULL. In this case this +** function is being called as part of determining whether or not pIdx +** is a covering index. This function clears any bits in (*pMask) +** corresponding to columns that may be replaced by constants as described +** above. +** +** Otherwise, if pItem is not NULL, then this function is being called +** as part of coding a loop that uses index pIdx. In this case, add entries +** to the Parse.pIdxPartExpr list for each column that can be replaced +** by a constant. +*/ +static void wherePartIdxExpr( + Parse *pParse, /* Parse context */ + Index *pIdx, /* Partial index being processed */ + Expr *pPart, /* WHERE clause being processed */ + Bitmask *pMask, /* Mask to clear bits in */ + int iIdxCur, /* Cursor number for index */ + SrcItem *pItem /* The FROM clause entry for the table */ +){ + assert( pItem==0 || (pItem->fg.jointype & JT_RIGHT)==0 ); + assert( (pItem==0 || pMask==0) && (pMask!=0 || pItem!=0) ); + + if( pPart->op==TK_AND ){ + wherePartIdxExpr(pParse, pIdx, pPart->pRight, pMask, iIdxCur, pItem); + pPart = pPart->pLeft; + } + + if( (pPart->op==TK_EQ || pPart->op==TK_IS) ){ + Expr *pLeft = pPart->pLeft; + Expr *pRight = pPart->pRight; + u8 aff; + + if( pLeft->op!=TK_COLUMN ) return; + if( !sqlite3ExprIsConstant(0, pRight) ) return; + if( !sqlite3IsBinary(sqlite3ExprCompareCollSeq(pParse, pPart)) ) return; + if( pLeft->iColumn<0 ) return; + aff = pIdx->pTable->aCol[pLeft->iColumn].affinity; + if( aff>=SQLITE_AFF_TEXT ){ + if( pItem ){ + sqlite3 *db = pParse->db; + IndexedExpr *p = (IndexedExpr*)sqlite3DbMallocRaw(db, sizeof(*p)); + if( p ){ + int bNullRow = (pItem->fg.jointype&(JT_LEFT|JT_LTORJ))!=0; + p->pExpr = sqlite3ExprDup(db, pRight, 0); + p->iDataCur = pItem->iCursor; + p->iIdxCur = iIdxCur; + p->iIdxCol = pLeft->iColumn; + p->bMaybeNullRow = bNullRow; + p->pIENext = pParse->pIdxPartExpr; + p->aff = aff; + pParse->pIdxPartExpr = p; + if( p->pIENext==0 ){ + void *pArg = (void*)&pParse->pIdxPartExpr; + sqlite3ParserAddCleanup(pParse, whereIndexedExprCleanup, pArg); + } + } + }else if( pLeft->iColumn<(BMS-1) ){ + *pMask &= ~((Bitmask)1 << pLeft->iColumn); + } + } + } +} + + /* ** Add all WhereLoop objects for a single table of the join where the table -** is idenfied by pBuilder->pNew->iTab. That table is guaranteed to be +** is identified by pBuilder->pNew->iTab. That table is guaranteed to be ** a b-tree table, not a virtual table. ** ** The costs (WhereLoop.rRun) of the b-tree loops added by this function @@ -2529,18 +3883,18 @@ static int whereUsablePartialIndex(int iTab, WhereClause *pWC, Expr *pWhere){ ** cost = nRow * K // scan of covering index ** cost = nRow * (K+3.0) // scan of non-covering index ** -** where K is a value between 1.1 and 3.0 set based on the relative +** where K is a value between 1.1 and 3.0 set based on the relative ** estimated average size of the index and table records. ** ** For an index scan, where nVisit is the number of index rows visited -** by the scan, and nSeek is the number of seek operations required on +** by the scan, and nSeek is the number of seek operations required on ** the index b-tree: ** ** cost = nSeek * (log(nRow) + K * nVisit) // covering index ** cost = nSeek * (log(nRow) + (K+3.0) * nVisit) // non-covering index ** -** Normally, nSeek is 1. nSeek values greater than 1 come about if the -** WHERE clause includes "x IN (....)" terms used in place of "x=?". Or when +** Normally, nSeek is 1. nSeek values greater than 1 come about if the +** WHERE clause includes "x IN (....)" terms used in place of "x=?". Or when ** implicit "x IN (SELECT x FROM tbl)" terms are added for skip-scans. ** ** The estimated values (nRow, nVisit, nSeek) often contain a large amount @@ -2553,7 +3907,7 @@ static int whereUsablePartialIndex(int iTab, WhereClause *pWC, Expr *pWhere){ */ static int whereLoopAddBtree( WhereLoopBuilder *pBuilder, /* WHERE clause information */ - Bitmask mExtra /* Extra prerequesites for using this table */ + Bitmask mPrereq /* Extra prerequisites for using this table */ ){ WhereInfo *pWInfo; /* WHERE analysis context */ Index *pProbe; /* An index we are evaluating */ @@ -2561,27 +3915,27 @@ static int whereLoopAddBtree( LogEst aiRowEstPk[2]; /* The aiRowLogEst[] value for the sPk index */ i16 aiColumnPk = -1; /* The aColumn[] value for the sPk index */ SrcList *pTabList; /* The FROM clause */ - struct SrcList_item *pSrc; /* The FROM clause btree term to add */ + SrcItem *pSrc; /* The FROM clause btree term to add */ WhereLoop *pNew; /* Template WhereLoop object */ int rc = SQLITE_OK; /* Return code */ int iSortIdx = 1; /* Index number */ int b; /* A boolean value */ LogEst rSize; /* number of rows in the table */ - LogEst rLogSize; /* Logarithm of the number of rows in the table */ WhereClause *pWC; /* The parsed WHERE clause */ Table *pTab; /* Table being queried */ - + pNew = pBuilder->pNew; pWInfo = pBuilder->pWInfo; pTabList = pWInfo->pTabList; pSrc = pTabList->a + pNew->iTab; - pTab = pSrc->pTab; + pTab = pSrc->pSTab; pWC = pBuilder->pWC; - assert( !IsVirtual(pSrc->pTab) ); + assert( !IsVirtual(pSrc->pSTab) ); - if( pSrc->pIBIndex ){ + if( pSrc->fg.isIndexedBy ){ + assert( pSrc->fg.isCte==0 ); /* An INDEXED BY clause specifies a particular index to use */ - pProbe = pSrc->pIBIndex; + pProbe = pSrc->u2.pIBIndex; }else if( !HasRowid(pTab) ){ pProbe = pTab->pIndex; }else{ @@ -2597,10 +3951,11 @@ static int whereLoopAddBtree( sPk.aiRowLogEst = aiRowEstPk; sPk.onError = OE_Replace; sPk.pTable = pTab; - sPk.szIdxRow = pTab->szTabRow; + sPk.szIdxRow = 3; /* TUNING: Interior rows of IPK table are very small */ + sPk.idxType = SQLITE_IDXTYPE_IPK; aiRowEstPk[0] = pTab->nRowLogEst; aiRowEstPk[1] = 0; - pFirst = pSrc->pTab->pIndex; + pFirst = pSrc->pSTab->pIndex; if( pSrc->fg.notIndexed==0 ){ /* The real indices of the table are only considered if the ** NOT INDEXED qualifier is omitted from the FROM clause */ @@ -2609,22 +3964,23 @@ static int whereLoopAddBtree( pProbe = &sPk; } rSize = pTab->nRowLogEst; - rLogSize = estLog(rSize); #ifndef SQLITE_OMIT_AUTOMATIC_INDEX /* Automatic indexes */ if( !pBuilder->pOrSet /* Not part of an OR optimization */ - && (pWInfo->wctrlFlags & WHERE_NO_AUTOINDEX)==0 + && (pWInfo->wctrlFlags & (WHERE_RIGHT_JOIN|WHERE_OR_SUBCLAUSE))==0 && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0 - && pSrc->pIBIndex==0 /* Has no INDEXED BY clause */ + && !pSrc->fg.isIndexedBy /* Has no INDEXED BY clause */ && !pSrc->fg.notIndexed /* Has no NOT INDEXED clause */ - && HasRowid(pTab) /* Not WITHOUT ROWID table. (FIXME: Why not?) */ && !pSrc->fg.isCorrelated /* Not a correlated subquery */ && !pSrc->fg.isRecursive /* Not a recursive common table expression. */ + && (pSrc->fg.jointype & JT_RIGHT)==0 /* Not the right tab of a RIGHT JOIN */ ){ /* Generate auto-index WhereLoops */ + LogEst rLogSize; /* Logarithm of the number of rows in the table */ WhereTerm *pTerm; WhereTerm *pWCEnd = pWC->a + pWC->nTerm; + rLogSize = estLog(rSize); for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){ if( pTerm->prereqRight & pNew->maskSelf ) continue; if( termCanDriveIndex(pTerm, pSrc, 0) ){ @@ -2636,16 +3992,20 @@ static int whereLoopAddBtree( /* TUNING: One-time cost for computing the automatic index is ** estimated to be X*N*log2(N) where N is the number of rows in ** the table being indexed and where X is 7 (LogEst=28) for normal - ** tables or 1.375 (LogEst=4) for views and subqueries. The value + ** tables or 0.5 (LogEst=-10) for views and subqueries. The value ** of X is smaller for views and subqueries so that the query planner ** will be more aggressive about generating automatic indexes for ** those objects, since there is no opportunity to add schema ** indexes on subqueries and views. */ - pNew->rSetup = rLogSize + rSize + 4; - if( pTab->pSelect==0 && (pTab->tabFlags & TF_Ephemeral)==0 ){ - pNew->rSetup += 24; + pNew->rSetup = rLogSize + rSize; + if( !IsView(pTab) && (pTab->tabFlags & TF_Ephemeral)==0 ){ + pNew->rSetup += 28; + }else{ + pNew->rSetup -= 25; /* Greatly reduced setup cost for auto indexes + ** on ephemeral materializations of views */ } ApplyCostMultiplier(pNew->rSetup, pTab->costMult); + if( pNew->rSetup<0 ) pNew->rSetup = 0; /* TUNING: Each index lookup yields 20 rows in the table. This ** is more than the usual guess of 10 rows, since we have no way ** of knowing how selective the index will ultimately be. It would @@ -2653,59 +4013,124 @@ static int whereLoopAddBtree( pNew->nOut = 43; assert( 43==sqlite3LogEst(20) ); pNew->rRun = sqlite3LogEstAdd(rLogSize,pNew->nOut); pNew->wsFlags = WHERE_AUTO_INDEX; - pNew->prereq = mExtra | pTerm->prereqRight; + pNew->prereq = mPrereq | pTerm->prereqRight; rc = whereLoopInsert(pBuilder, pNew); } } } #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */ - /* Loop over all indices - */ - for(; rc==SQLITE_OK && pProbe; pProbe=pProbe->pNext, iSortIdx++){ + /* Loop over all indices. If there was an INDEXED BY clause, then only + ** consider index pProbe. */ + for(; rc==SQLITE_OK && pProbe; + pProbe=(pSrc->fg.isIndexedBy ? 0 : pProbe->pNext), iSortIdx++ + ){ if( pProbe->pPartIdxWhere!=0 - && !whereUsablePartialIndex(pSrc->iCursor, pWC, pProbe->pPartIdxWhere) ){ + && !whereUsablePartialIndex(pSrc->iCursor, pSrc->fg.jointype, pWC, + pProbe->pPartIdxWhere) + ){ testcase( pNew->iTab!=pSrc->iCursor ); /* See ticket [98d973b8f5] */ continue; /* Partial index inappropriate for this query */ } + if( pProbe->bNoQuery ) continue; rSize = pProbe->aiRowLogEst[0]; pNew->u.btree.nEq = 0; + pNew->u.btree.nBtm = 0; + pNew->u.btree.nTop = 0; + pNew->u.btree.nDistinctCol = 0; pNew->nSkip = 0; pNew->nLTerm = 0; pNew->iSortIdx = 0; pNew->rSetup = 0; - pNew->prereq = mExtra; + pNew->prereq = mPrereq; pNew->nOut = rSize; pNew->u.btree.pIndex = pProbe; + pNew->u.btree.pOrderBy = 0; b = indexMightHelpWithOrderBy(pBuilder, pProbe, pSrc->iCursor); + /* The ONEPASS_DESIRED flags never occurs together with ORDER BY */ assert( (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || b==0 ); - if( pProbe->tnum<=0 ){ + if( pProbe->idxType==SQLITE_IDXTYPE_IPK ){ /* Integer primary key index */ pNew->wsFlags = WHERE_IPK; /* Full table scan */ pNew->iSortIdx = b ? iSortIdx : 0; - /* TUNING: Cost of full table scan is (N*3.0). */ + /* TUNING: Cost of full table scan is 3.0*N. The 3.0 factor is an + ** extra cost designed to discourage the use of full table scans, + ** since index lookups have better worst-case performance if our + ** stat guesses are wrong. Reduce the 3.0 penalty slightly + ** (to 2.75) if we have valid STAT4 information for the table. + ** At 2.75, a full table scan is preferred over using an index on + ** a column with just two distinct values where each value has about + ** an equal number of appearances. Without STAT4 data, we still want + ** to use an index in that case, since the constraint might be for + ** the scarcer of the two values, and in that case an index lookup is + ** better. + */ +#ifdef SQLITE_ENABLE_STAT4 + pNew->rRun = rSize + 16 - 2*((pTab->tabFlags & TF_HasStat4)!=0); +#else pNew->rRun = rSize + 16; +#endif ApplyCostMultiplier(pNew->rRun, pTab->costMult); whereLoopOutputAdjust(pWC, pNew, rSize); + if( pSrc->fg.isSubquery ){ + if( pSrc->fg.viaCoroutine ) pNew->wsFlags |= WHERE_COROUTINE; + pNew->u.btree.pOrderBy = pSrc->u4.pSubq->pSelect->pOrderBy; + } rc = whereLoopInsert(pBuilder, pNew); pNew->nOut = rSize; if( rc ) break; }else{ Bitmask m; if( pProbe->isCovering ){ - pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED; m = 0; + pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED; }else{ - m = pSrc->colUsed & ~columnsInIndex(pProbe); - pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY|WHERE_INDEXED) : WHERE_INDEXED; + m = pSrc->colUsed & pProbe->colNotIdxed; + if( pProbe->pPartIdxWhere ){ + wherePartIdxExpr( + pWInfo->pParse, pProbe, pProbe->pPartIdxWhere, &m, 0, 0 + ); + } + pNew->wsFlags = WHERE_INDEXED; + if( m==TOPBIT || (pProbe->bHasExpr && !pProbe->bHasVCol && m!=0) ){ + u32 isCov = whereIsCoveringIndex(pWInfo, pProbe, pSrc->iCursor); + if( isCov==0 ){ + WHERETRACE(0x200, + ("-> %s is not a covering index" + " according to whereIsCoveringIndex()\n", pProbe->zName)); + assert( m!=0 ); + }else{ + m = 0; + pNew->wsFlags |= isCov; + if( isCov & WHERE_IDX_ONLY ){ + WHERETRACE(0x200, + ("-> %s is a covering expression index" + " according to whereIsCoveringIndex()\n", pProbe->zName)); + }else{ + assert( isCov==WHERE_EXPRIDX ); + WHERETRACE(0x200, + ("-> %s might be a covering expression index" + " according to whereIsCoveringIndex()\n", pProbe->zName)); + } + } + }else if( m==0 + && (HasRowid(pTab) || pWInfo->pSelect!=0 || sqlite3FaultSim(700)) + ){ + WHERETRACE(0x200, + ("-> %s is a covering index according to bitmasks\n", + pProbe->zName, m==0 ? "is" : "is not")); + pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED; + } } /* Full scan via index */ if( b || !HasRowid(pTab) + || pProbe->pPartIdxWhere!=0 + || pSrc->fg.isIndexedBy || ( m==0 && pProbe->bUnordered==0 && (pProbe->szIdxRow<pTab->szTabRow) @@ -2718,233 +4143,559 @@ static int whereLoopAddBtree( /* The cost of visiting the index rows is N*K, where K is ** between 1.1 and 3.0, depending on the relative sizes of the - ** index and table rows. If this is a non-covering index scan, - ** also add the cost of visiting table rows (N*3.0). */ + ** index and table rows. */ pNew->rRun = rSize + 1 + (15*pProbe->szIdxRow)/pTab->szTabRow; if( m!=0 ){ - pNew->rRun = sqlite3LogEstAdd(pNew->rRun, rSize+16); + /* If this is a non-covering index scan, add in the cost of + ** doing table lookups. The cost will be 3x the number of + ** lookups. Take into account WHERE clause terms that can be + ** satisfied using just the index, and that do not require a + ** table lookup. */ + LogEst nLookup = rSize + 16; /* Base cost: N*3 */ + int ii; + int iCur = pSrc->iCursor; + WhereClause *pWC2 = &pWInfo->sWC; + for(ii=0; ii<pWC2->nTerm; ii++){ + WhereTerm *pTerm = &pWC2->a[ii]; + if( !sqlite3ExprCoveredByIndex(pTerm->pExpr, iCur, pProbe) ){ + break; + } + /* pTerm can be evaluated using just the index. So reduce + ** the expected number of table lookups accordingly */ + if( pTerm->truthProb<=0 ){ + nLookup += pTerm->truthProb; + }else{ + nLookup--; + if( pTerm->eOperator & (WO_EQ|WO_IS) ) nLookup -= 19; + } + } + + pNew->rRun = sqlite3LogEstAdd(pNew->rRun, nLookup); } ApplyCostMultiplier(pNew->rRun, pTab->costMult); whereLoopOutputAdjust(pWC, pNew, rSize); - rc = whereLoopInsert(pBuilder, pNew); + if( (pSrc->fg.jointype & JT_RIGHT)!=0 && pProbe->aColExpr ){ + /* Do not do an SCAN of a index-on-expression in a RIGHT JOIN + ** because the cursor used to access the index might not be + ** positioned to the correct row during the right-join no-match + ** loop. */ + }else{ + rc = whereLoopInsert(pBuilder, pNew); + } pNew->nOut = rSize; if( rc ) break; } } + pBuilder->bldFlags1 = 0; rc = whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, 0); -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 + if( pBuilder->bldFlags1==SQLITE_BLDF1_INDEXED ){ + /* If a non-unique index is used, or if a prefix of the key for + ** unique index is used (making the index functionally non-unique) + ** then the sqlite_stat1 data becomes important for scoring the + ** plan */ + pTab->tabFlags |= TF_MaybeReanalyze; + } +#ifdef SQLITE_ENABLE_STAT4 sqlite3Stat4ProbeFree(pBuilder->pRec); pBuilder->nRecValid = 0; pBuilder->pRec = 0; #endif - - /* If there was an INDEXED BY clause, then only that one index is - ** considered. */ - if( pSrc->pIBIndex ) break; } return rc; } #ifndef SQLITE_OMIT_VIRTUALTABLE + /* -** Add all WhereLoop objects for a table of the join identified by -** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table. -** -** If there are no LEFT or CROSS JOIN joins in the query, both mExtra and -** mUnusable are set to 0. Otherwise, mExtra is a mask of all FROM clause -** entries that occur before the virtual table in the FROM clause and are -** separated from it by at least one LEFT or CROSS JOIN. Similarly, the -** mUnusable mask contains all FROM clause entries that occur after the -** virtual table and are separated from it by at least one LEFT or -** CROSS JOIN. -** +** Return true if pTerm is a virtual table LIMIT or OFFSET term. +*/ +static int isLimitTerm(WhereTerm *pTerm){ + assert( pTerm->eOperator==WO_AUX || pTerm->eMatchOp==0 ); + return pTerm->eMatchOp>=SQLITE_INDEX_CONSTRAINT_LIMIT + && pTerm->eMatchOp<=SQLITE_INDEX_CONSTRAINT_OFFSET; +} + +/* +** Return true if the first nCons constraints in the pUsage array are +** marked as in-use (have argvIndex>0). False otherwise. +*/ +static int allConstraintsUsed( + struct sqlite3_index_constraint_usage *aUsage, + int nCons +){ + int ii; + for(ii=0; ii<nCons; ii++){ + if( aUsage[ii].argvIndex<=0 ) return 0; + } + return 1; +} + +/* +** Argument pIdxInfo is already populated with all constraints that may +** be used by the virtual table identified by pBuilder->pNew->iTab. This +** function marks a subset of those constraints usable, invokes the +** xBestIndex method and adds the returned plan to pBuilder. +** +** A constraint is marked usable if: +** +** * Argument mUsable indicates that its prerequisites are available, and +** +** * It is not one of the operators specified in the mExclude mask passed +** as the fourth argument (which in practice is either WO_IN or 0). +** +** Argument mPrereq is a mask of tables that must be scanned before the +** virtual table in question. These are added to the plans prerequisites +** before it is added to pBuilder. +** +** Output parameter *pbIn is set to true if the plan added to pBuilder +** uses one or more WO_IN terms, or false otherwise. +*/ +static int whereLoopAddVirtualOne( + WhereLoopBuilder *pBuilder, + Bitmask mPrereq, /* Mask of tables that must be used. */ + Bitmask mUsable, /* Mask of usable tables */ + u16 mExclude, /* Exclude terms using these operators */ + sqlite3_index_info *pIdxInfo, /* Populated object for xBestIndex */ + u16 mNoOmit, /* Do not omit these constraints */ + int *pbIn, /* OUT: True if plan uses an IN(...) op */ + int *pbRetryLimit /* OUT: Retry without LIMIT/OFFSET */ +){ + WhereClause *pWC = pBuilder->pWC; + HiddenIndexInfo *pHidden = (HiddenIndexInfo*)&pIdxInfo[1]; + struct sqlite3_index_constraint *pIdxCons; + struct sqlite3_index_constraint_usage *pUsage = pIdxInfo->aConstraintUsage; + int i; + int mxTerm; + int rc = SQLITE_OK; + WhereLoop *pNew = pBuilder->pNew; + Parse *pParse = pBuilder->pWInfo->pParse; + SrcItem *pSrc = &pBuilder->pWInfo->pTabList->a[pNew->iTab]; + int nConstraint = pIdxInfo->nConstraint; + + assert( (mUsable & mPrereq)==mPrereq ); + *pbIn = 0; + pNew->prereq = mPrereq; + + /* Set the usable flag on the subset of constraints identified by + ** arguments mUsable and mExclude. */ + pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint; + for(i=0; i<nConstraint; i++, pIdxCons++){ + WhereTerm *pTerm = termFromWhereClause(pWC, pIdxCons->iTermOffset); + pIdxCons->usable = 0; + if( (pTerm->prereqRight & mUsable)==pTerm->prereqRight + && (pTerm->eOperator & mExclude)==0 + && (pbRetryLimit || !isLimitTerm(pTerm)) + ){ + pIdxCons->usable = 1; + } + } + + /* Initialize the output fields of the sqlite3_index_info structure */ + memset(pUsage, 0, sizeof(pUsage[0])*nConstraint); + assert( pIdxInfo->needToFreeIdxStr==0 ); + pIdxInfo->idxStr = 0; + pIdxInfo->idxNum = 0; + pIdxInfo->orderByConsumed = 0; + pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2; + pIdxInfo->estimatedRows = 25; + pIdxInfo->idxFlags = 0; + pHidden->mHandleIn = 0; + + /* Invoke the virtual table xBestIndex() method */ + rc = vtabBestIndex(pParse, pSrc->pSTab, pIdxInfo); + if( rc ){ + if( rc==SQLITE_CONSTRAINT ){ + /* If the xBestIndex method returns SQLITE_CONSTRAINT, that means + ** that the particular combination of parameters provided is unusable. + ** Make no entries in the loop table. + */ + WHERETRACE(0xffffffff, (" ^^^^--- non-viable plan rejected!\n")); + freeIdxStr(pIdxInfo); + return SQLITE_OK; + } + return rc; + } + + mxTerm = -1; + assert( pNew->nLSlot>=nConstraint ); + memset(pNew->aLTerm, 0, sizeof(pNew->aLTerm[0])*nConstraint ); + memset(&pNew->u.vtab, 0, sizeof(pNew->u.vtab)); + pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint; + for(i=0; i<nConstraint; i++, pIdxCons++){ + int iTerm; + if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){ + WhereTerm *pTerm; + int j = pIdxCons->iTermOffset; + if( iTerm>=nConstraint + || j<0 + || (pTerm = termFromWhereClause(pWC, j))==0 + || pNew->aLTerm[iTerm]!=0 + || pIdxCons->usable==0 + ){ + sqlite3ErrorMsg(pParse,"%s.xBestIndex malfunction",pSrc->pSTab->zName); + freeIdxStr(pIdxInfo); + return SQLITE_ERROR; + } + testcase( iTerm==nConstraint-1 ); + testcase( j==0 ); + testcase( j==pWC->nTerm-1 ); + pNew->prereq |= pTerm->prereqRight; + assert( iTerm<pNew->nLSlot ); + pNew->aLTerm[iTerm] = pTerm; + if( iTerm>mxTerm ) mxTerm = iTerm; + testcase( iTerm==15 ); + testcase( iTerm==16 ); + if( pUsage[i].omit ){ + if( i<16 && ((1<<i)&mNoOmit)==0 ){ + testcase( i!=iTerm ); + pNew->u.vtab.omitMask |= 1<<iTerm; + }else{ + testcase( i!=iTerm ); + } + if( pTerm->eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET ){ + pNew->u.vtab.bOmitOffset = 1; + } + } + if( SMASKBIT32(i) & pHidden->mHandleIn ){ + pNew->u.vtab.mHandleIn |= MASKBIT32(iTerm); + }else if( (pTerm->eOperator & WO_IN)!=0 ){ + /* A virtual table that is constrained by an IN clause may not + ** consume the ORDER BY clause because (1) the order of IN terms + ** is not necessarily related to the order of output terms and + ** (2) Multiple outputs from a single IN value will not merge + ** together. */ + pIdxInfo->orderByConsumed = 0; + pIdxInfo->idxFlags &= ~SQLITE_INDEX_SCAN_UNIQUE; + *pbIn = 1; assert( (mExclude & WO_IN)==0 ); + } + + /* Unless pbRetryLimit is non-NULL, there should be no LIMIT/OFFSET + ** terms. And if there are any, they should follow all other terms. */ + assert( pbRetryLimit || !isLimitTerm(pTerm) ); + assert( !isLimitTerm(pTerm) || i>=nConstraint-2 ); + assert( !isLimitTerm(pTerm) || i==nConstraint-1 || isLimitTerm(pTerm+1) ); + + if( isLimitTerm(pTerm) && (*pbIn || !allConstraintsUsed(pUsage, i)) ){ + /* If there is an IN(...) term handled as an == (separate call to + ** xFilter for each value on the RHS of the IN) and a LIMIT or + ** OFFSET term handled as well, the plan is unusable. Similarly, + ** if there is a LIMIT/OFFSET and there are other unused terms, + ** the plan cannot be used. In these cases set variable *pbRetryLimit + ** to true to tell the caller to retry with LIMIT and OFFSET + ** disabled. */ + freeIdxStr(pIdxInfo); + *pbRetryLimit = 1; + return SQLITE_OK; + } + } + } + + pNew->nLTerm = mxTerm+1; + for(i=0; i<=mxTerm; i++){ + if( pNew->aLTerm[i]==0 ){ + /* The non-zero argvIdx values must be contiguous. Raise an + ** error if they are not */ + sqlite3ErrorMsg(pParse,"%s.xBestIndex malfunction",pSrc->pSTab->zName); + freeIdxStr(pIdxInfo); + return SQLITE_ERROR; + } + } + assert( pNew->nLTerm<=pNew->nLSlot ); + pNew->u.vtab.idxNum = pIdxInfo->idxNum; + pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr; + pIdxInfo->needToFreeIdxStr = 0; + pNew->u.vtab.idxStr = pIdxInfo->idxStr; + pNew->u.vtab.isOrdered = (i8)(pIdxInfo->orderByConsumed ? + pIdxInfo->nOrderBy : 0); + pNew->u.vtab.bIdxNumHex = (pIdxInfo->idxFlags&SQLITE_INDEX_SCAN_HEX)!=0; + pNew->rSetup = 0; + pNew->rRun = sqlite3LogEstFromDouble(pIdxInfo->estimatedCost); + pNew->nOut = sqlite3LogEst(pIdxInfo->estimatedRows); + + /* Set the WHERE_ONEROW flag if the xBestIndex() method indicated + ** that the scan will visit at most one row. Clear it otherwise. */ + if( pIdxInfo->idxFlags & SQLITE_INDEX_SCAN_UNIQUE ){ + pNew->wsFlags |= WHERE_ONEROW; + }else{ + pNew->wsFlags &= ~WHERE_ONEROW; + } + rc = whereLoopInsert(pBuilder, pNew); + if( pNew->u.vtab.needFree ){ + sqlite3_free(pNew->u.vtab.idxStr); + pNew->u.vtab.needFree = 0; + } + WHERETRACE(0xffffffff, (" bIn=%d prereqIn=%04llx prereqOut=%04llx\n", + *pbIn, (sqlite3_uint64)mPrereq, + (sqlite3_uint64)(pNew->prereq & ~mPrereq))); + + return rc; +} + +/* +** Return the collating sequence for a constraint passed into xBestIndex. +** +** pIdxInfo must be an sqlite3_index_info structure passed into xBestIndex. +** This routine depends on there being a HiddenIndexInfo structure immediately +** following the sqlite3_index_info structure. +** +** Return a pointer to the collation name: +** +** 1. If there is an explicit COLLATE operator on the constraint, return it. +** +** 2. Else, if the column has an alternative collation, return that. +** +** 3. Otherwise, return "BINARY". +*/ +const char *sqlite3_vtab_collation(sqlite3_index_info *pIdxInfo, int iCons){ + HiddenIndexInfo *pHidden = (HiddenIndexInfo*)&pIdxInfo[1]; + const char *zRet = 0; + if( iCons>=0 && iCons<pIdxInfo->nConstraint ){ + CollSeq *pC = 0; + int iTerm = pIdxInfo->aConstraint[iCons].iTermOffset; + Expr *pX = termFromWhereClause(pHidden->pWC, iTerm)->pExpr; + if( pX->pLeft ){ + pC = sqlite3ExprCompareCollSeq(pHidden->pParse, pX); + } + zRet = (pC ? pC->zName : sqlite3StrBINARY); + } + return zRet; +} + +/* +** Return true if constraint iCons is really an IN(...) constraint, or +** false otherwise. If iCons is an IN(...) constraint, set (if bHandle!=0) +** or clear (if bHandle==0) the flag to handle it using an iterator. +*/ +int sqlite3_vtab_in(sqlite3_index_info *pIdxInfo, int iCons, int bHandle){ + HiddenIndexInfo *pHidden = (HiddenIndexInfo*)&pIdxInfo[1]; + u32 m = SMASKBIT32(iCons); + if( m & pHidden->mIn ){ + if( bHandle==0 ){ + pHidden->mHandleIn &= ~m; + }else if( bHandle>0 ){ + pHidden->mHandleIn |= m; + } + return 1; + } + return 0; +} + +/* +** This interface is callable from within the xBestIndex callback only. +** +** If possible, set (*ppVal) to point to an object containing the value +** on the right-hand-side of constraint iCons. +*/ +int sqlite3_vtab_rhs_value( + sqlite3_index_info *pIdxInfo, /* Copy of first argument to xBestIndex */ + int iCons, /* Constraint for which RHS is wanted */ + sqlite3_value **ppVal /* Write value extracted here */ +){ + HiddenIndexInfo *pH = (HiddenIndexInfo*)&pIdxInfo[1]; + sqlite3_value *pVal = 0; + int rc = SQLITE_OK; + if( iCons<0 || iCons>=pIdxInfo->nConstraint ){ + rc = SQLITE_MISUSE_BKPT; /* EV: R-30545-25046 */ + }else{ + if( pH->aRhs[iCons]==0 ){ + WhereTerm *pTerm = termFromWhereClause( + pH->pWC, pIdxInfo->aConstraint[iCons].iTermOffset + ); + rc = sqlite3ValueFromExpr( + pH->pParse->db, pTerm->pExpr->pRight, ENC(pH->pParse->db), + SQLITE_AFF_BLOB, &pH->aRhs[iCons] + ); + testcase( rc!=SQLITE_OK ); + } + pVal = pH->aRhs[iCons]; + } + *ppVal = pVal; + + if( rc==SQLITE_OK && pVal==0 ){ /* IMP: R-19933-32160 */ + rc = SQLITE_NOTFOUND; /* IMP: R-36424-56542 */ + } + + return rc; +} + +/* +** Return true if ORDER BY clause may be handled as DISTINCT. +*/ +int sqlite3_vtab_distinct(sqlite3_index_info *pIdxInfo){ + HiddenIndexInfo *pHidden = (HiddenIndexInfo*)&pIdxInfo[1]; + assert( pHidden->eDistinct>=0 && pHidden->eDistinct<=3 ); + return pHidden->eDistinct; +} + +/* +** Cause the prepared statement that is associated with a call to +** xBestIndex to potentially use all schemas. If the statement being +** prepared is read-only, then just start read transactions on all +** schemas. But if this is a write operation, start writes on all +** schemas. +** +** This is used by the (built-in) sqlite_dbpage virtual table. +*/ +void sqlite3VtabUsesAllSchemas(Parse *pParse){ + int nDb = pParse->db->nDb; + int i; + for(i=0; i<nDb; i++){ + sqlite3CodeVerifySchema(pParse, i); + } + if( DbMaskNonZero(pParse->writeMask) ){ + for(i=0; i<nDb; i++){ + sqlite3BeginWriteOperation(pParse, 0, i); + } + } +} + +/* +** Add all WhereLoop objects for a table of the join identified by +** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table. +** +** If there are no LEFT or CROSS JOIN joins in the query, both mPrereq and +** mUnusable are set to 0. Otherwise, mPrereq is a mask of all FROM clause +** entries that occur before the virtual table in the FROM clause and are +** separated from it by at least one LEFT or CROSS JOIN. Similarly, the +** mUnusable mask contains all FROM clause entries that occur after the +** virtual table and are separated from it by at least one LEFT or +** CROSS JOIN. +** ** For example, if the query were: ** ** ... FROM t1, t2 LEFT JOIN t3, t4, vt CROSS JOIN t5, t6; ** -** then mExtra corresponds to (t1, t2) and mUnusable to (t5, t6). +** then mPrereq corresponds to (t1, t2) and mUnusable to (t5, t6). ** -** All the tables in mExtra must be scanned before the current virtual -** table. So any terms for which all prerequisites are satisfied by -** mExtra may be specified as "usable" in all calls to xBestIndex. +** All the tables in mPrereq must be scanned before the current virtual +** table. So any terms for which all prerequisites are satisfied by +** mPrereq may be specified as "usable" in all calls to xBestIndex. ** Conversely, all tables in mUnusable must be scanned after the current ** virtual table, so any terms for which the prerequisites overlap with ** mUnusable should always be configured as "not-usable" for xBestIndex. */ static int whereLoopAddVirtual( WhereLoopBuilder *pBuilder, /* WHERE clause information */ - Bitmask mExtra, /* Tables that must be scanned before this one */ + Bitmask mPrereq, /* Tables that must be scanned before this one */ Bitmask mUnusable /* Tables that must be scanned after this one */ ){ + int rc = SQLITE_OK; /* Return code */ WhereInfo *pWInfo; /* WHERE analysis context */ Parse *pParse; /* The parsing context */ WhereClause *pWC; /* The WHERE clause */ - struct SrcList_item *pSrc; /* The FROM clause term to search */ - Table *pTab; - sqlite3 *db; - sqlite3_index_info *pIdxInfo; - struct sqlite3_index_constraint *pIdxCons; - struct sqlite3_index_constraint_usage *pUsage; - WhereTerm *pTerm; - int i, j; - int iTerm, mxTerm; - int nConstraint; - int seenIn = 0; /* True if an IN operator is seen */ - int seenVar = 0; /* True if a non-constant constraint is seen */ - int iPhase; /* 0: const w/o IN, 1: const, 2: no IN, 2: IN */ + SrcItem *pSrc; /* The FROM clause term to search */ + sqlite3_index_info *p; /* Object to pass to xBestIndex() */ + int nConstraint; /* Number of constraints in p */ + int bIn; /* True if plan uses IN(...) operator */ WhereLoop *pNew; - int rc = SQLITE_OK; + Bitmask mBest; /* Tables used by best possible plan */ + u16 mNoOmit; + int bRetry = 0; /* True to retry with LIMIT/OFFSET disabled */ - assert( (mExtra & mUnusable)==0 ); + assert( (mPrereq & mUnusable)==0 ); pWInfo = pBuilder->pWInfo; pParse = pWInfo->pParse; - db = pParse->db; pWC = pBuilder->pWC; pNew = pBuilder->pNew; pSrc = &pWInfo->pTabList->a[pNew->iTab]; - pTab = pSrc->pTab; - assert( IsVirtual(pTab) ); - pIdxInfo = allocateIndexInfo(pParse, pWC, mUnusable, pSrc,pBuilder->pOrderBy); - if( pIdxInfo==0 ) return SQLITE_NOMEM; - pNew->prereq = 0; + assert( IsVirtual(pSrc->pSTab) ); + p = allocateIndexInfo(pWInfo, pWC, mUnusable, pSrc, &mNoOmit); + if( p==0 ) return SQLITE_NOMEM_BKPT; pNew->rSetup = 0; pNew->wsFlags = WHERE_VIRTUALTABLE; pNew->nLTerm = 0; pNew->u.vtab.needFree = 0; - pUsage = pIdxInfo->aConstraintUsage; - nConstraint = pIdxInfo->nConstraint; - if( whereLoopResize(db, pNew, nConstraint) ){ - sqlite3DbFree(db, pIdxInfo); - return SQLITE_NOMEM; - } - - for(iPhase=0; iPhase<=3; iPhase++){ - if( !seenIn && (iPhase&1)!=0 ){ - iPhase++; - if( iPhase>3 ) break; - } - if( !seenVar && iPhase>1 ) break; - pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint; - for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){ - j = pIdxCons->iTermOffset; - pTerm = &pWC->a[j]; - switch( iPhase ){ - case 0: /* Constants without IN operator */ - pIdxCons->usable = 0; - if( (pTerm->eOperator & WO_IN)!=0 ){ - seenIn = 1; - } - if( (pTerm->prereqRight & ~mExtra)!=0 ){ - seenVar = 1; - }else if( (pTerm->eOperator & WO_IN)==0 ){ - pIdxCons->usable = 1; - } - break; - case 1: /* Constants with IN operators */ - assert( seenIn ); - pIdxCons->usable = (pTerm->prereqRight & ~mExtra)==0; - break; - case 2: /* Variables without IN */ - assert( seenVar ); - pIdxCons->usable = (pTerm->eOperator & WO_IN)==0; - break; - default: /* Variables with IN */ - assert( seenVar && seenIn ); - pIdxCons->usable = 1; - break; - } - } - memset(pUsage, 0, sizeof(pUsage[0])*pIdxInfo->nConstraint); - if( pIdxInfo->needToFreeIdxStr ) sqlite3_free(pIdxInfo->idxStr); - pIdxInfo->idxStr = 0; - pIdxInfo->idxNum = 0; - pIdxInfo->needToFreeIdxStr = 0; - pIdxInfo->orderByConsumed = 0; - pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2; - pIdxInfo->estimatedRows = 25; - pIdxInfo->idxFlags = 0; - pIdxInfo->colUsed = (sqlite3_int64)pSrc->colUsed; - rc = vtabBestIndex(pParse, pTab, pIdxInfo); - if( rc ) goto whereLoopAddVtab_exit; - pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint; - pNew->prereq = mExtra; - mxTerm = -1; - assert( pNew->nLSlot>=nConstraint ); - for(i=0; i<nConstraint; i++) pNew->aLTerm[i] = 0; - pNew->u.vtab.omitMask = 0; - for(i=0; i<nConstraint; i++, pIdxCons++){ - if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){ - j = pIdxCons->iTermOffset; - if( iTerm>=nConstraint - || j<0 - || j>=pWC->nTerm - || pNew->aLTerm[iTerm]!=0 - ){ - rc = SQLITE_ERROR; - sqlite3ErrorMsg(pParse, "%s.xBestIndex() malfunction", pTab->zName); - goto whereLoopAddVtab_exit; - } - testcase( iTerm==nConstraint-1 ); - testcase( j==0 ); - testcase( j==pWC->nTerm-1 ); - pTerm = &pWC->a[j]; - pNew->prereq |= pTerm->prereqRight; - assert( iTerm<pNew->nLSlot ); - pNew->aLTerm[iTerm] = pTerm; - if( iTerm>mxTerm ) mxTerm = iTerm; - testcase( iTerm==15 ); - testcase( iTerm==16 ); - if( iTerm<16 && pUsage[i].omit ) pNew->u.vtab.omitMask |= 1<<iTerm; - if( (pTerm->eOperator & WO_IN)!=0 ){ - if( pUsage[i].omit==0 ){ - /* Do not attempt to use an IN constraint if the virtual table - ** says that the equivalent EQ constraint cannot be safely omitted. - ** If we do attempt to use such a constraint, some rows might be - ** repeated in the output. */ - break; - } - /* A virtual table that is constrained by an IN clause may not - ** consume the ORDER BY clause because (1) the order of IN terms - ** is not necessarily related to the order of output terms and - ** (2) Multiple outputs from a single IN value will not merge - ** together. */ - pIdxInfo->orderByConsumed = 0; - pIdxInfo->idxFlags &= ~SQLITE_INDEX_SCAN_UNIQUE; - } + nConstraint = p->nConstraint; + if( whereLoopResize(pParse->db, pNew, nConstraint) ){ + freeIndexInfo(pParse->db, p); + return SQLITE_NOMEM_BKPT; + } + + /* First call xBestIndex() with all constraints usable. */ + WHERETRACE(0x800, ("BEGIN %s.addVirtual()\n", pSrc->pSTab->zName)); + WHERETRACE(0x800, (" VirtualOne: all usable\n")); + rc = whereLoopAddVirtualOne( + pBuilder, mPrereq, ALLBITS, 0, p, mNoOmit, &bIn, &bRetry + ); + if( bRetry ){ + assert( rc==SQLITE_OK ); + rc = whereLoopAddVirtualOne( + pBuilder, mPrereq, ALLBITS, 0, p, mNoOmit, &bIn, 0 + ); + } + + /* If the call to xBestIndex() with all terms enabled produced a plan + ** that does not require any source tables (IOW: a plan with mBest==0) + ** and does not use an IN(...) operator, then there is no point in making + ** any further calls to xBestIndex() since they will all return the same + ** result (if the xBestIndex() implementation is sane). */ + if( rc==SQLITE_OK && ((mBest = (pNew->prereq & ~mPrereq))!=0 || bIn) ){ + int seenZero = 0; /* True if a plan with no prereqs seen */ + int seenZeroNoIN = 0; /* Plan with no prereqs and no IN(...) seen */ + Bitmask mPrev = 0; + Bitmask mBestNoIn = 0; + + /* If the plan produced by the earlier call uses an IN(...) term, call + ** xBestIndex again, this time with IN(...) terms disabled. */ + if( bIn ){ + WHERETRACE(0x800, (" VirtualOne: all usable w/o IN\n")); + rc = whereLoopAddVirtualOne( + pBuilder, mPrereq, ALLBITS, WO_IN, p, mNoOmit, &bIn, 0); + assert( bIn==0 ); + mBestNoIn = pNew->prereq & ~mPrereq; + if( mBestNoIn==0 ){ + seenZero = 1; + seenZeroNoIN = 1; } } - if( i>=nConstraint ){ - pNew->nLTerm = mxTerm+1; - assert( pNew->nLTerm<=pNew->nLSlot ); - pNew->u.vtab.idxNum = pIdxInfo->idxNum; - pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr; - pIdxInfo->needToFreeIdxStr = 0; - pNew->u.vtab.idxStr = pIdxInfo->idxStr; - pNew->u.vtab.isOrdered = (i8)(pIdxInfo->orderByConsumed ? - pIdxInfo->nOrderBy : 0); - pNew->rSetup = 0; - pNew->rRun = sqlite3LogEstFromDouble(pIdxInfo->estimatedCost); - pNew->nOut = sqlite3LogEst(pIdxInfo->estimatedRows); - /* Set the WHERE_ONEROW flag if the xBestIndex() method indicated - ** that the scan will visit at most one row. Clear it otherwise. */ - if( pIdxInfo->idxFlags & SQLITE_INDEX_SCAN_UNIQUE ){ - pNew->wsFlags |= WHERE_ONEROW; - }else{ - pNew->wsFlags &= ~WHERE_ONEROW; + /* Call xBestIndex once for each distinct value of (prereqRight & ~mPrereq) + ** in the set of terms that apply to the current virtual table. */ + while( rc==SQLITE_OK ){ + int i; + Bitmask mNext = ALLBITS; + assert( mNext>0 ); + for(i=0; i<nConstraint; i++){ + int iTerm = p->aConstraint[i].iTermOffset; + Bitmask mThis = termFromWhereClause(pWC, iTerm)->prereqRight & ~mPrereq; + if( mThis>mPrev && mThis<mNext ) mNext = mThis; } - whereLoopInsert(pBuilder, pNew); - if( pNew->u.vtab.needFree ){ - sqlite3_free(pNew->u.vtab.idxStr); - pNew->u.vtab.needFree = 0; + mPrev = mNext; + if( mNext==ALLBITS ) break; + if( mNext==mBest || mNext==mBestNoIn ) continue; + WHERETRACE(0x800, (" VirtualOne: mPrev=%04llx mNext=%04llx\n", + (sqlite3_uint64)mPrev, (sqlite3_uint64)mNext)); + rc = whereLoopAddVirtualOne( + pBuilder, mPrereq, mNext|mPrereq, 0, p, mNoOmit, &bIn, 0); + if( pNew->prereq==mPrereq ){ + seenZero = 1; + if( bIn==0 ) seenZeroNoIN = 1; } } - } -whereLoopAddVtab_exit: - if( pIdxInfo->needToFreeIdxStr ) sqlite3_free(pIdxInfo->idxStr); - sqlite3DbFree(db, pIdxInfo); + /* If the calls to xBestIndex() in the above loop did not find a plan + ** that requires no source tables at all (i.e. one guaranteed to be + ** usable), make a call here with all source tables disabled */ + if( rc==SQLITE_OK && seenZero==0 ){ + WHERETRACE(0x800, (" VirtualOne: all disabled\n")); + rc = whereLoopAddVirtualOne( + pBuilder, mPrereq, mPrereq, 0, p, mNoOmit, &bIn, 0); + if( bIn==0 ) seenZeroNoIN = 1; + } + + /* If the calls to xBestIndex() have so far failed to find a plan + ** that requires no source tables at all and does not use an IN(...) + ** operator, make a final call to obtain one here. */ + if( rc==SQLITE_OK && seenZeroNoIN==0 ){ + WHERETRACE(0x800, (" VirtualOne: all disabled and w/o IN\n")); + rc = whereLoopAddVirtualOne( + pBuilder, mPrereq, mPrereq, WO_IN, p, mNoOmit, &bIn, 0); + } + } + + freeIndexInfo(pParse->db, p); + WHERETRACE(0x800, ("END %s.addVirtual(), rc=%d\n", pSrc->pSTab->zName, rc)); return rc; } #endif /* SQLITE_OMIT_VIRTUALTABLE */ @@ -2954,8 +4705,8 @@ static int whereLoopAddVirtual( ** btrees or virtual tables. */ static int whereLoopAddOr( - WhereLoopBuilder *pBuilder, - Bitmask mExtra, + WhereLoopBuilder *pBuilder, + Bitmask mPrereq, Bitmask mUnusable ){ WhereInfo *pWInfo = pBuilder->pWInfo; @@ -2967,8 +4718,8 @@ static int whereLoopAddOr( WhereClause tempWC; WhereLoopBuilder sSubBuild; WhereOrSet sSum, sCur; - struct SrcList_item *pItem; - + SrcItem *pItem; + pWC = pBuilder->pWC; pWCEnd = pWC->a + pWC->nTerm; pNew = pBuilder->pNew; @@ -2976,21 +4727,23 @@ static int whereLoopAddOr( pItem = pWInfo->pTabList->a + pNew->iTab; iCur = pItem->iCursor; + /* The multi-index OR optimization does not work for RIGHT and FULL JOIN */ + if( pItem->fg.jointype & JT_RIGHT ) return SQLITE_OK; + for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){ if( (pTerm->eOperator & WO_OR)!=0 - && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0 + && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0 ){ WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc; WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm]; WhereTerm *pOrTerm; int once = 1; int i, j; - + sSubBuild = *pBuilder; - sSubBuild.pOrderBy = 0; sSubBuild.pOrSet = &sCur; - WHERETRACE(0x200, ("Begin processing OR-clause %p\n", pTerm)); + WHERETRACE(0x400, ("Begin processing OR-clause %p\n", pTerm)); for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){ if( (pOrTerm->eOperator & WO_AND)!=0 ){ sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc; @@ -2999,6 +4752,7 @@ static int whereLoopAddOr( tempWC.pOuter = pWC; tempWC.op = TK_AND; tempWC.nTerm = 1; + tempWC.nBase = 1; tempWC.a = pOrTerm; sSubBuild.pWC = &tempWC; }else{ @@ -3006,26 +4760,25 @@ static int whereLoopAddOr( } sCur.n = 0; #ifdef WHERETRACE_ENABLED - WHERETRACE(0x200, ("OR-term %d of %p has %d subterms:\n", + WHERETRACE(0x400, ("OR-term %d of %p has %d subterms:\n", (int)(pOrTerm-pOrWC->a), pTerm, sSubBuild.pWC->nTerm)); - if( sqlite3WhereTrace & 0x400 ){ - for(i=0; i<sSubBuild.pWC->nTerm; i++){ - whereTermPrint(&sSubBuild.pWC->a[i], i); - } + if( sqlite3WhereTrace & 0x20000 ){ + sqlite3WhereClausePrint(sSubBuild.pWC); } #endif #ifndef SQLITE_OMIT_VIRTUALTABLE - if( IsVirtual(pItem->pTab) ){ - rc = whereLoopAddVirtual(&sSubBuild, mExtra, mUnusable); + if( IsVirtual(pItem->pSTab) ){ + rc = whereLoopAddVirtual(&sSubBuild, mPrereq, mUnusable); }else #endif { - rc = whereLoopAddBtree(&sSubBuild, mExtra); + rc = whereLoopAddBtree(&sSubBuild, mPrereq); } if( rc==SQLITE_OK ){ - rc = whereLoopAddOr(&sSubBuild, mExtra, mUnusable); + rc = whereLoopAddOr(&sSubBuild, mPrereq, mUnusable); } - assert( rc==SQLITE_OK || sCur.n==0 ); + testcase( rc==SQLITE_NOMEM && sCur.n>0 ); + testcase( rc==SQLITE_DONE ); if( sCur.n==0 ){ sSum.n = 0; break; @@ -3055,8 +4808,8 @@ static int whereLoopAddOr( /* TUNING: Currently sSum.a[i].rRun is set to the sum of the costs ** of all sub-scans required by the OR-scan. However, due to rounding ** errors, it may be that the cost of the OR-scan is equal to its - ** most expensive sub-scan. Add the smallest possible penalty - ** (equivalent to multiplying the cost by 1.07) to ensure that + ** most expensive sub-scan. Add the smallest possible penalty + ** (equivalent to multiplying the cost by 1.07) to ensure that ** this does not happen. Otherwise, for WHERE clauses such as the ** following where there is an index on "y": ** @@ -3069,85 +4822,210 @@ static int whereLoopAddOr( pNew->prereq = sSum.a[i].prereq; rc = whereLoopInsert(pBuilder, pNew); } - WHERETRACE(0x200, ("End processing OR-clause %p\n", pTerm)); + WHERETRACE(0x400, ("End processing OR-clause %p\n", pTerm)); } } return rc; } /* -** Add all WhereLoop objects for all tables +** Add all WhereLoop objects for all tables */ static int whereLoopAddAll(WhereLoopBuilder *pBuilder){ WhereInfo *pWInfo = pBuilder->pWInfo; - Bitmask mExtra = 0; + Bitmask mPrereq = 0; Bitmask mPrior = 0; int iTab; SrcList *pTabList = pWInfo->pTabList; - struct SrcList_item *pItem; - struct SrcList_item *pEnd = &pTabList->a[pWInfo->nLevel]; + SrcItem *pItem; + SrcItem *pEnd = &pTabList->a[pWInfo->nLevel]; sqlite3 *db = pWInfo->pParse->db; int rc = SQLITE_OK; + int bFirstPastRJ = 0; + int hasRightJoin = 0; WhereLoop *pNew; - u8 priorJointype = 0; + /* Loop over the tables in the join, from left to right */ pNew = pBuilder->pNew; - whereLoopInit(pNew); + + /* Verify that pNew has already been initialized */ + assert( pNew->nLTerm==0 ); + assert( pNew->wsFlags==0 ); + assert( pNew->nLSlot>=ArraySize(pNew->aLTermSpace) ); + assert( pNew->aLTerm!=0 ); + + pBuilder->iPlanLimit = SQLITE_QUERY_PLANNER_LIMIT; for(iTab=0, pItem=pTabList->a; pItem<pEnd; iTab++, pItem++){ Bitmask mUnusable = 0; pNew->iTab = iTab; + pBuilder->iPlanLimit += SQLITE_QUERY_PLANNER_LIMIT_INCR; pNew->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, pItem->iCursor); - if( ((pItem->fg.jointype|priorJointype) & (JT_LEFT|JT_CROSS))!=0 ){ - /* This condition is true when pItem is the FROM clause term on the - ** right-hand-side of a LEFT or CROSS JOIN. */ - mExtra = mPrior; - } - priorJointype = pItem->fg.jointype; - if( IsVirtual(pItem->pTab) ){ - struct SrcList_item *p; + if( bFirstPastRJ + || (pItem->fg.jointype & (JT_OUTER|JT_CROSS|JT_LTORJ))!=0 + ){ + /* Add prerequisites to prevent reordering of FROM clause terms + ** across CROSS joins and outer joins. The bFirstPastRJ boolean + ** prevents the right operand of a RIGHT JOIN from being swapped with + ** other elements even further to the right. + ** + ** The JT_LTORJ case and the hasRightJoin flag work together to + ** prevent FROM-clause terms from moving from the right side of + ** a LEFT JOIN over to the left side of that join if the LEFT JOIN + ** is itself on the left side of a RIGHT JOIN. + */ + if( pItem->fg.jointype & JT_LTORJ ) hasRightJoin = 1; + mPrereq |= mPrior; + bFirstPastRJ = (pItem->fg.jointype & JT_RIGHT)!=0; + }else if( !hasRightJoin ){ + mPrereq = 0; + } +#ifndef SQLITE_OMIT_VIRTUALTABLE + if( IsVirtual(pItem->pSTab) ){ + SrcItem *p; for(p=&pItem[1]; p<pEnd; p++){ - if( mUnusable || (p->fg.jointype & (JT_LEFT|JT_CROSS)) ){ + if( mUnusable || (p->fg.jointype & (JT_OUTER|JT_CROSS)) ){ mUnusable |= sqlite3WhereGetMask(&pWInfo->sMaskSet, p->iCursor); } } - rc = whereLoopAddVirtual(pBuilder, mExtra, mUnusable); - }else{ - rc = whereLoopAddBtree(pBuilder, mExtra); + rc = whereLoopAddVirtual(pBuilder, mPrereq, mUnusable); + }else +#endif /* SQLITE_OMIT_VIRTUALTABLE */ + { + rc = whereLoopAddBtree(pBuilder, mPrereq); } - if( rc==SQLITE_OK ){ - rc = whereLoopAddOr(pBuilder, mExtra, mUnusable); + if( rc==SQLITE_OK && pBuilder->pWC->hasOr ){ + rc = whereLoopAddOr(pBuilder, mPrereq, mUnusable); } mPrior |= pNew->maskSelf; - if( rc || db->mallocFailed ) break; + if( rc || db->mallocFailed ){ + if( rc==SQLITE_DONE ){ + /* We hit the query planner search limit set by iPlanLimit */ + sqlite3_log(SQLITE_WARNING, "abbreviated query algorithm search"); + rc = SQLITE_OK; + }else{ + break; + } + } } whereLoopClear(db, pNew); return rc; } +/* Implementation of the order-by-subquery optimization: +** +** WhereLoop pLoop, which the iLoop-th term of the nested loop, is really +** a subquery or CTE that has an ORDER BY clause. See if any of the terms +** in the subquery ORDER BY clause will satisfy pOrderBy from the outer +** query. Mark off all satisfied terms (by setting bits in *pOBSat) and +** return TRUE if they do. If not, return false. +** +** Example: +** +** CREATE TABLE t1(a,b,c, PRIMARY KEY(a,b)); +** CREATE TABLE t2(x,y); +** WITH t3(p,q) AS MATERIALIZED (SELECT x+y, x-y FROM t2 ORDER BY x+y) +** SELECT * FROM t3 JOIN t1 ON a=q ORDER BY p, b; +** +** The CTE named "t3" comes out in the natural order of "p", so the first +** first them of "ORDER BY p,b" is satisfied by a sequential scan of "t3" +** and sorting only needs to occur on the second term "b". +** +** Limitations: +** +** (1) The optimization is not applied if the outer ORDER BY contains +** a COLLATE clause. The optimization might be applied if the +** outer ORDER BY uses NULLS FIRST, NULLS LAST, ASC, and/or DESC as +** long as the subquery ORDER BY does the same. But if the +** outer ORDER BY uses COLLATE, even a redundant COLLATE, the +** optimization is bypassed. +** +** (2) The subquery ORDER BY terms must exactly match subquery result +** columns, including any COLLATE annotations. This routine relies +** on iOrderByCol to do matching between order by terms and result +** columns, and iOrderByCol will not be set if the result column +** and ORDER BY collations differ. +** +** (3) The subquery and outer ORDER BY can be in opposite directions as +** long as the subquery is materialized. If the subquery is +** implemented as a co-routine, the sort orders must be in the same +** direction because there is no way to run a co-routine backwards. +*/ +static SQLITE_NOINLINE int wherePathMatchSubqueryOB( + WhereInfo *pWInfo, /* The WHERE clause */ + WhereLoop *pLoop, /* The nested loop term that is a subquery */ + int iLoop, /* Which level of the nested loop. 0==outermost */ + int iCur, /* Cursor used by the this loop */ + ExprList *pOrderBy, /* The ORDER BY clause on the whole query */ + Bitmask *pRevMask, /* When loops need to go in reverse order */ + Bitmask *pOBSat /* Which terms of pOrderBy are satisfied so far */ +){ + int iOB; /* Index into pOrderBy->a[] */ + int jSub; /* Index into pSubOB->a[] */ + u8 rev = 0; /* True if iOB and jSub sort in opposite directions */ + u8 revIdx = 0; /* Sort direction for jSub */ + Expr *pOBExpr; /* Current term of outer ORDER BY */ + ExprList *pSubOB; /* Complete ORDER BY on the subquery */ + + pSubOB = pLoop->u.btree.pOrderBy; + assert( pSubOB!=0 ); + for(iOB=0; (MASKBIT(iOB) & *pOBSat)!=0; iOB++){} + for(jSub=0; jSub<pSubOB->nExpr && iOB<pOrderBy->nExpr; jSub++, iOB++){ + if( pSubOB->a[jSub].u.x.iOrderByCol==0 ) break; + pOBExpr = pOrderBy->a[iOB].pExpr; + if( pOBExpr->op!=TK_COLUMN && pOBExpr->op!=TK_AGG_COLUMN ) break; + if( pOBExpr->iTable!=iCur ) break; + if( pOBExpr->iColumn!=pSubOB->a[jSub].u.x.iOrderByCol-1 ) break; + if( (pWInfo->wctrlFlags & WHERE_GROUPBY)==0 ){ + u8 sfOB = pOrderBy->a[iOB].fg.sortFlags; /* sortFlags for iOB */ + u8 sfSub = pSubOB->a[jSub].fg.sortFlags; /* sortFlags for jSub */ + if( (sfSub & KEYINFO_ORDER_BIGNULL) != (sfOB & KEYINFO_ORDER_BIGNULL) ){ + break; + } + revIdx = sfSub & KEYINFO_ORDER_DESC; + if( jSub>0 ){ + if( (rev^revIdx)!=(sfOB & KEYINFO_ORDER_DESC) ){ + break; + } + }else{ + rev = revIdx ^ (sfOB & KEYINFO_ORDER_DESC); + if( rev ){ + if( (pLoop->wsFlags & WHERE_COROUTINE)!=0 ){ + /* Cannot run a co-routine in reverse order */ + break; + } + *pRevMask |= MASKBIT(iLoop); + } + } + } + *pOBSat |= MASKBIT(iOB); + } + return jSub>0; +} + /* -** Examine a WherePath (with the addition of the extra WhereLoop of the 5th +** Examine a WherePath (with the addition of the extra WhereLoop of the 6th ** parameters) to see if it outputs rows in the requested ORDER BY ** (or GROUP BY) without requiring a separate sort operation. Return N: -** +** ** N>0: N terms of the ORDER BY clause are satisfied ** N==0: No terms of the ORDER BY clause are satisfied -** N<0: Unknown yet how many terms of ORDER BY might be satisfied. +** N<0: Unknown yet how many terms of ORDER BY might be satisfied. ** ** Note that processing for WHERE_GROUPBY and WHERE_DISTINCTBY is not as ** strict. With GROUP BY and DISTINCT the only requirement is that ** equivalent rows appear immediately adjacent to one another. GROUP BY ** and DISTINCT do not require rows to appear in any particular order as long ** as equivalent rows are grouped together. Thus for GROUP BY and DISTINCT -** the pOrderBy terms can be matched in any order. With ORDER BY, the +** the pOrderBy terms can be matched in any order. With ORDER BY, the ** pOrderBy terms must be matched in strict left-to-right order. */ static i8 wherePathSatisfiesOrderBy( WhereInfo *pWInfo, /* The WHERE clause */ ExprList *pOrderBy, /* ORDER BY or GROUP BY or DISTINCT clause to check */ WherePath *pPath, /* The WherePath to check */ - u16 wctrlFlags, /* Might contain WHERE_GROUPBY or WHERE_DISTINCTBY */ + u16 wctrlFlags, /* WHERE_GROUPBY or _DISTINCTBY or _ORDERBY_LIMIT */ u16 nLoop, /* Number of entries in pPath->aLoop[] */ WhereLoop *pLast, /* Add this WhereLoop to the end of pPath->aLoop[] */ Bitmask *pRevMask /* OUT: Mask of WhereLoops to run in reverse order */ @@ -3158,6 +5036,7 @@ static i8 wherePathSatisfiesOrderBy( u8 isOrderDistinct; /* All prior WhereLoops are order-distinct */ u8 distinctColumns; /* True if the loop has UNIQUE NOT NULL columns */ u8 isMatch; /* iColumn matches a term of the ORDER BY clause */ + u16 eqOpMask; /* Allowed equality operators */ u16 nKeyCol; /* Number of key columns in pIndex */ u16 nColumn; /* Total number of ordered columns in the index */ u16 nOrderBy; /* Number terms in the ORDER BY clause */ @@ -3189,7 +5068,7 @@ static i8 wherePathSatisfiesOrderBy( ** row of the WhereLoop. Every one-row WhereLoop is automatically ** order-distinct. A WhereLoop that has no columns in the ORDER BY clause ** is not order-distinct. To be order-distinct is not quite the same as being - ** UNIQUE since a UNIQUE column or index can have multiple rows that + ** UNIQUE since a UNIQUE column or index can have multiple rows that ** are NULL and NULL values are equivalent for the purpose of order-distinct. ** To be order-distinct, the columns must be UNIQUE and NOT NULL. ** @@ -3208,11 +5087,28 @@ static i8 wherePathSatisfiesOrderBy( obDone = MASKBIT(nOrderBy)-1; orderDistinctMask = 0; ready = 0; + eqOpMask = WO_EQ | WO_IS | WO_ISNULL; + if( wctrlFlags & (WHERE_ORDERBY_LIMIT|WHERE_ORDERBY_MAX|WHERE_ORDERBY_MIN) ){ + eqOpMask |= WO_IN; + } for(iLoop=0; isOrderDistinct && obSat<obDone && iLoop<=nLoop; iLoop++){ if( iLoop>0 ) ready |= pLoop->maskSelf; - pLoop = iLoop<nLoop ? pPath->aLoop[iLoop] : pLast; + if( iLoop<nLoop ){ + pLoop = pPath->aLoop[iLoop]; + if( wctrlFlags & WHERE_ORDERBY_LIMIT ) continue; + }else{ + pLoop = pLast; + } if( pLoop->wsFlags & WHERE_VIRTUALTABLE ){ - if( pLoop->u.vtab.isOrdered ) obSat = obDone; + if( pLoop->u.vtab.isOrdered + && ((wctrlFlags&(WHERE_DISTINCTBY|WHERE_SORTBYGROUP))!=WHERE_DISTINCTBY) + ){ + obSat = obDone; + }else{ + /* No further ORDER BY terms may be matched. So this call should + ** return >=0, not -1. Clear isOrderDistinct to ensure it does so. */ + isOrderDistinct = 0; + } break; } iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor; @@ -3224,21 +5120,30 @@ static i8 wherePathSatisfiesOrderBy( */ for(i=0; i<nOrderBy; i++){ if( MASKBIT(i) & obSat ) continue; - pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr); - if( pOBExpr->op!=TK_COLUMN ) continue; + pOBExpr = sqlite3ExprSkipCollateAndLikely(pOrderBy->a[i].pExpr); + if( NEVER(pOBExpr==0) ) continue; + if( pOBExpr->op!=TK_COLUMN && pOBExpr->op!=TK_AGG_COLUMN ) continue; if( pOBExpr->iTable!=iCur ) continue; pTerm = sqlite3WhereFindTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn, - ~ready, WO_EQ|WO_ISNULL|WO_IS, 0); + ~ready, eqOpMask, 0); if( pTerm==0 ) continue; + if( pTerm->eOperator==WO_IN ){ + /* IN terms are only valid for sorting in the ORDER BY LIMIT + ** optimization, and then only if they are actually used + ** by the query plan */ + assert( wctrlFlags & + (WHERE_ORDERBY_LIMIT|WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX) ); + for(j=0; j<pLoop->nLTerm && pTerm!=pLoop->aLTerm[j]; j++){} + if( j>=pLoop->nLTerm ) continue; + } if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0 && pOBExpr->iColumn>=0 ){ - const char *z1, *z2; - pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr); - if( !pColl ) pColl = db->pDfltColl; - z1 = pColl->zName; - pColl = sqlite3ExprCollSeq(pWInfo->pParse, pTerm->pExpr); - if( !pColl ) pColl = db->pDfltColl; - z2 = pColl->zName; - if( sqlite3StrICmp(z1, z2)!=0 ) continue; + Parse *pParse = pWInfo->pParse; + CollSeq *pColl1 = sqlite3ExprNNCollSeq(pParse, pOrderBy->a[i].pExpr); + CollSeq *pColl2 = sqlite3ExprCompareCollSeq(pParse, pTerm->pExpr); + assert( pColl1 ); + if( pColl2==0 || sqlite3StrICmp(pColl1->zName, pColl2->zName) ){ + continue; + } testcase( pTerm->pExpr->op==TK_IS ); } obSat |= MASKBIT(i); @@ -3246,9 +5151,18 @@ static i8 wherePathSatisfiesOrderBy( if( (pLoop->wsFlags & WHERE_ONEROW)==0 ){ if( pLoop->wsFlags & WHERE_IPK ){ + if( pLoop->u.btree.pOrderBy + && OptimizationEnabled(db, SQLITE_OrderBySubq) + && wherePathMatchSubqueryOB(pWInfo,pLoop,iLoop,iCur, + pOrderBy,pRevMask, &obSat) + ){ + nColumn = 0; + isOrderDistinct = 0; + }else{ + nColumn = 1; + } pIndex = 0; nKeyCol = 0; - nColumn = 1; }else if( (pIndex = pLoop->u.btree.pIndex)==0 || pIndex->bUnordered ){ return 0; }else{ @@ -3257,7 +5171,12 @@ static i8 wherePathSatisfiesOrderBy( assert( nColumn==nKeyCol+1 || !HasRowid(pIndex->pTable) ); assert( pIndex->aiColumn[nColumn-1]==XN_ROWID || !HasRowid(pIndex->pTable)); - isOrderDistinct = IsUniqueIndex(pIndex); + /* All relevant terms of the index must also be non-NULL in order + ** for isOrderDistinct to be true. So the isOrderDistinct value + ** computed here might be a false positive. Corrections will be + ** made at tag-20210426-1 below */ + isOrderDistinct = IsUniqueIndex(pIndex) + && (pLoop->wsFlags & WHERE_SKIPSCAN)==0; } /* Loop through all columns of the index and deal with the ones @@ -3266,18 +5185,48 @@ static i8 wherePathSatisfiesOrderBy( rev = revSet = 0; distinctColumns = 0; for(j=0; j<nColumn; j++){ - u8 bOnce; /* True to run the ORDER BY search loop */ - - /* Skip over == and IS NULL terms */ - if( j<pLoop->u.btree.nEq - && pLoop->nSkip==0 - && ((i = pLoop->aLTerm[j]->eOperator) & (WO_EQ|WO_ISNULL|WO_IS))!=0 - ){ - if( i & WO_ISNULL ){ - testcase( isOrderDistinct ); - isOrderDistinct = 0; + u8 bOnce = 1; /* True to run the ORDER BY search loop */ + + assert( j>=pLoop->u.btree.nEq + || (pLoop->aLTerm[j]==0)==(j<pLoop->nSkip) + ); + if( j<pLoop->u.btree.nEq && j>=pLoop->nSkip ){ + u16 eOp = pLoop->aLTerm[j]->eOperator; + + /* Skip over == and IS and ISNULL terms. (Also skip IN terms when + ** doing WHERE_ORDERBY_LIMIT processing). Except, IS and ISNULL + ** terms imply that the index is not UNIQUE NOT NULL in which case + ** the loop need to be marked as not order-distinct because it can + ** have repeated NULL rows. + ** + ** If the current term is a column of an ((?,?) IN (SELECT...)) + ** expression for which the SELECT returns more than one column, + ** check that it is the only column used by this loop. Otherwise, + ** if it is one of two or more, none of the columns can be + ** considered to match an ORDER BY term. + */ + if( (eOp & eqOpMask)!=0 ){ + if( eOp & (WO_ISNULL|WO_IS) ){ + testcase( eOp & WO_ISNULL ); + testcase( eOp & WO_IS ); + testcase( isOrderDistinct ); + isOrderDistinct = 0; + } + continue; + }else if( ALWAYS(eOp & WO_IN) ){ + /* ALWAYS() justification: eOp is an equality operator due to the + ** j<pLoop->u.btree.nEq constraint above. Any equality other + ** than WO_IN is captured by the previous "if". So this one + ** always has to be WO_IN. */ + Expr *pX = pLoop->aLTerm[j]->pExpr; + for(i=j+1; i<pLoop->u.btree.nEq; i++){ + if( pLoop->aLTerm[i]->pExpr==pX ){ + assert( (pLoop->aLTerm[i]->eOperator & WO_IN) ); + bOnce = 0; + break; + } + } } - continue; } /* Get the column number in the table (iColumn) and sort order @@ -3285,49 +5234,56 @@ static i8 wherePathSatisfiesOrderBy( */ if( pIndex ){ iColumn = pIndex->aiColumn[j]; - revIdx = pIndex->aSortOrder[j]; - if( iColumn==pIndex->pTable->iPKey ) iColumn = -1; + revIdx = pIndex->aSortOrder[j] & KEYINFO_ORDER_DESC; + if( iColumn==pIndex->pTable->iPKey ) iColumn = XN_ROWID; }else{ iColumn = XN_ROWID; revIdx = 0; } /* An unconstrained column that might be NULL means that this - ** WhereLoop is not well-ordered + ** WhereLoop is not well-ordered. tag-20210426-1 */ - if( isOrderDistinct - && iColumn>=0 - && j>=pLoop->u.btree.nEq - && pIndex->pTable->aCol[iColumn].notNull==0 - ){ - isOrderDistinct = 0; + if( isOrderDistinct ){ + if( iColumn>=0 + && j>=pLoop->u.btree.nEq + && pIndex->pTable->aCol[iColumn].notNull==0 + ){ + isOrderDistinct = 0; + } + if( iColumn==XN_EXPR ){ + isOrderDistinct = 0; + } } /* Find the ORDER BY term that corresponds to the j-th column - ** of the index and mark that ORDER BY term off + ** of the index and mark that ORDER BY term having been satisfied. */ - bOnce = 1; isMatch = 0; for(i=0; bOnce && i<nOrderBy; i++){ if( MASKBIT(i) & obSat ) continue; - pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr); + pOBExpr = sqlite3ExprSkipCollateAndLikely(pOrderBy->a[i].pExpr); testcase( wctrlFlags & WHERE_GROUPBY ); testcase( wctrlFlags & WHERE_DISTINCTBY ); + if( NEVER(pOBExpr==0) ) continue; if( (wctrlFlags & (WHERE_GROUPBY|WHERE_DISTINCTBY))==0 ) bOnce = 0; - if( iColumn>=(-1) ){ - if( pOBExpr->op!=TK_COLUMN ) continue; + if( iColumn>=XN_ROWID ){ + if( pOBExpr->op!=TK_COLUMN && pOBExpr->op!=TK_AGG_COLUMN ) continue; if( pOBExpr->iTable!=iCur ) continue; if( pOBExpr->iColumn!=iColumn ) continue; }else{ - if( sqlite3ExprCompare(pOBExpr,pIndex->aColExpr->a[j].pExpr,iCur) ){ + Expr *pIxExpr = pIndex->aColExpr->a[j].pExpr; + if( sqlite3ExprCompareSkip(pOBExpr, pIxExpr, iCur) ){ continue; } } - if( iColumn>=0 ){ - pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr); - if( !pColl ) pColl = db->pDfltColl; + if( iColumn!=XN_ROWID ){ + pColl = sqlite3ExprNNCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr); if( sqlite3StrICmp(pColl->zName, pIndex->azColl[j])!=0 ) continue; } + if( wctrlFlags & WHERE_DISTINCTBY ){ + pLoop->u.btree.nDistinctCol = j+1; + } isMatch = 1; break; } @@ -3335,15 +5291,26 @@ static i8 wherePathSatisfiesOrderBy( /* Make sure the sort order is compatible in an ORDER BY clause. ** Sort order is irrelevant for a GROUP BY clause. */ if( revSet ){ - if( (rev ^ revIdx)!=pOrderBy->a[i].sortOrder ) isMatch = 0; + if( (rev ^ revIdx) + != (pOrderBy->a[i].fg.sortFlags&KEYINFO_ORDER_DESC) + ){ + isMatch = 0; + } }else{ - rev = revIdx ^ pOrderBy->a[i].sortOrder; + rev = revIdx ^ (pOrderBy->a[i].fg.sortFlags & KEYINFO_ORDER_DESC); if( rev ) *pRevMask |= MASKBIT(iLoop); revSet = 1; } } + if( isMatch && (pOrderBy->a[i].fg.sortFlags & KEYINFO_ORDER_BIGNULL) ){ + if( j==pLoop->u.btree.nEq ){ + pLoop->wsFlags |= WHERE_BIGNULL_SORT; + }else{ + isMatch = 0; + } + } if( isMatch ){ - if( iColumn<0 ){ + if( iColumn==XN_ROWID ){ testcase( distinctColumns==0 ); distinctColumns = 1; } @@ -3372,7 +5339,7 @@ static i8 wherePathSatisfiesOrderBy( if( MASKBIT(i) & obSat ) continue; p = pOrderBy->a[i].pExpr; mTerm = sqlite3WhereExprUsage(&pWInfo->sMaskSet,p); - if( mTerm==0 && !sqlite3ExprIsConstant(p) ) continue; + if( mTerm==0 && !sqlite3ExprIsConstant(0,p) ) continue; if( (mTerm&~orderDistinctMask)==0 ){ obSat |= MASKBIT(i); } @@ -3382,7 +5349,7 @@ static i8 wherePathSatisfiesOrderBy( if( obSat==obDone ) return (i8)nOrderBy; if( !isOrderDistinct ){ for(i=nOrderBy-1; i>0; i--){ - Bitmask m = MASKBIT(i) - 1; + Bitmask m = ALWAYS(i<BMS) ? MASKBIT(i) - 1 : 0; if( (obSat&m)==m ) return i; } return 0; @@ -3415,7 +5382,7 @@ static i8 wherePathSatisfiesOrderBy( ** SELECT * FROM t1 GROUP BY y,x ORDER BY y,x; -- IsSorted()==0 */ int sqlite3WhereIsSorted(WhereInfo *pWInfo){ - assert( pWInfo->wctrlFlags & WHERE_GROUPBY ); + assert( pWInfo->wctrlFlags & (WHERE_GROUPBY|WHERE_DISTINCTBY) ); assert( pWInfo->wctrlFlags & WHERE_SORTBYGROUP ); return pWInfo->sorted; } @@ -3433,35 +5400,280 @@ static const char *wherePathName(WherePath *pPath, int nLoop, WhereLoop *pLast){ #endif /* -** Return the cost of sorting nRow rows, assuming that the keys have +** Return the cost of sorting nRow rows, assuming that the keys have ** nOrderby columns and that the first nSorted columns are already in ** order. */ static LogEst whereSortingCost( - LogEst nRow, - int nOrderBy, - int nSorted + WhereInfo *pWInfo, /* Query planning context */ + LogEst nRow, /* Estimated number of rows to sort */ + int nOrderBy, /* Number of ORDER BY clause terms */ + int nSorted /* Number of initial ORDER BY terms naturally in order */ ){ - /* TUNING: Estimated cost of a full external sort, where N is + /* Estimated cost of a full external sort, where N is ** the number of rows to sort is: ** - ** cost = (3.0 * N * log(N)). - ** - ** Or, if the order-by clause has X terms but only the last Y - ** terms are out of order, then block-sorting will reduce the + ** cost = (K * N * log(N)). + ** + ** Or, if the order-by clause has X terms but only the last Y + ** terms are out of order, then block-sorting will reduce the ** sorting cost to: ** - ** cost = (3.0 * N * log(N)) * (Y/X) + ** cost = (K * N * log(N)) * (Y/X) ** - ** The (Y/X) term is implemented using stack variable rScale - ** below. */ - LogEst rScale, rSortCost; - assert( nOrderBy>0 && 66==sqlite3LogEst(100) ); - rScale = sqlite3LogEst((nOrderBy-nSorted)*100/nOrderBy) - 66; - rSortCost = nRow + estLog(nRow) + rScale + 16; + ** The constant K is at least 2.0 but will be larger if there are a + ** large number of columns to be sorted, as the sorting time is + ** proportional to the amount of content to be sorted. The algorithm + ** does not currently distinguish between fat columns (BLOBs and TEXTs) + ** and skinny columns (INTs). It just uses the number of columns as + ** an approximation for the row width. + ** + ** And extra factor of 2.0 or 3.0 is added to the sorting cost if the sort + ** is built using OP_IdxInsert and OP_Sort rather than with OP_SorterInsert. + */ + LogEst rSortCost, nCol; + assert( pWInfo->pSelect!=0 ); + assert( pWInfo->pSelect->pEList!=0 ); + /* TUNING: sorting cost proportional to the number of output columns: */ + nCol = sqlite3LogEst((pWInfo->pSelect->pEList->nExpr+59)/30); + rSortCost = nRow + nCol; + if( nSorted>0 ){ + /* Scale the result by (Y/X) */ + rSortCost += sqlite3LogEst((nOrderBy-nSorted)*100/nOrderBy) - 66; + } + + /* Multiple by log(M) where M is the number of output rows. + ** Use the LIMIT for M if it is smaller. Or if this sort is for + ** a DISTINCT operator, M will be the number of distinct output + ** rows, so fudge it downwards a bit. + */ + if( (pWInfo->wctrlFlags & WHERE_USE_LIMIT)!=0 ){ + rSortCost += 10; /* TUNING: Extra 2.0x if using LIMIT */ + if( nSorted!=0 ){ + rSortCost += 6; /* TUNING: Extra 1.5x if also using partial sort */ + } + if( pWInfo->iLimit<nRow ){ + nRow = pWInfo->iLimit; + } + }else if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT) ){ + /* TUNING: In the sort for a DISTINCT operator, assume that the DISTINCT + ** reduces the number of output rows by a factor of 2 */ + if( nRow>10 ){ nRow -= 10; assert( 10==sqlite3LogEst(2) ); } + } + rSortCost += estLog(nRow); return rSortCost; } +/* +** Compute the maximum number of paths in the solver algorithm, for +** queries that have three or more terms in the FROM clause. Queries with +** two or fewer FROM clause terms are handled by the caller. +** +** Query planning is NP-hard. We must limit the number of paths at +** each step of the solver search algorithm to avoid exponential behavior. +** +** The value returned is a tuning parameter. Currently the value is: +** +** 18 for star queries +** 12 otherwise +** +** For the purposes of this heuristic, a star-query is defined as a query +** with a large central table that is joined using an INNER JOIN, +** not CROSS or OUTER JOINs, against four or more smaller tables. +** The central table is called the "fact" table. The smaller tables +** that get joined are "dimension tables". Also, any table that is +** self-joined cannot be a dimension table; we assume that dimension +** tables may only be joined against fact tables. +** +** SIDE EFFECT: (and really the whole point of this subroutine) +** +** If pWInfo describes a star-query, then the cost for SCANs of dimension +** WhereLoops is increased to be slightly larger than the cost of a SCAN +** in the fact table. Only SCAN costs are increased. SEARCH costs are +** unchanged. This heuristic helps keep fact tables in outer loops. Without +** this heuristic, paths with fact tables in outer loops tend to get pruned +** by the mxChoice limit on the number of paths, resulting in poor query +** plans. See the starschema1.test test module for examples of queries +** that need this heuristic to find good query plans. +** +** This heuristic can be completely disabled, so that no query is +** considered a star-query, using SQLITE_TESTCTRL_OPTIMIZATION to +** disable the SQLITE_StarQuery optimization. In the CLI, the command +** to do that is: ".testctrl opt -starquery". +** +** HISTORICAL NOTES: +** +** This optimization was first added on 2024-05-09 by check-in 38db9b5c83d. +** The original optimization reduced the cost and output size estimate for +** fact tables to help them move to outer loops. But months later (as people +** started upgrading) performance regression reports started caming in, +** including: +** +** forum post b18ef983e68d06d1 (2024-12-21) +** forum post 0025389d0860af82 (2025-01-14) +** forum post d87570a145599033 (2025-01-17) +** +** To address these, the criteria for a star-query was tightened to exclude +** cases where the fact and dimensions are separated by an outer join, and +** the affect of star-schema detection was changed to increase the rRun cost +** on just full table scans of dimension tables, rather than reducing costs +** in the all access methods of the fact table. +*/ +static int computeMxChoice(WhereInfo *pWInfo){ + int nLoop = pWInfo->nLevel; /* Number of terms in the join */ + WhereLoop *pWLoop; /* For looping over WhereLoops */ + +#ifdef SQLITE_DEBUG + /* The star-query detection code below makes use of the following + ** properties of the WhereLoop list, so verify them before + ** continuing: + ** (1) .maskSelf is the bitmask corresponding to .iTab + ** (2) The WhereLoop list is in ascending .iTab order + */ + for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){ + assert( pWLoop->maskSelf==MASKBIT(pWLoop->iTab) ); + assert( pWLoop->pNextLoop==0 || pWLoop->iTab<=pWLoop->pNextLoop->iTab ); + } +#endif /* SQLITE_DEBUG */ + + if( nLoop>=5 + && !pWInfo->bStarDone + && OptimizationEnabled(pWInfo->pParse->db, SQLITE_StarQuery) + ){ + SrcItem *aFromTabs; /* All terms of the FROM clause */ + int iFromIdx; /* Term of FROM clause is the candidate fact-table */ + Bitmask m; /* Bitmask for candidate fact-table */ + Bitmask mSelfJoin = 0; /* Tables that cannot be dimension tables */ + WhereLoop *pStart; /* Where to start searching for dimension-tables */ + + pWInfo->bStarDone = 1; /* Only do this computation once */ + + /* Look for fact tables with four or more dimensions where the + ** dimension tables are not separately from the fact tables by an outer + ** or cross join. Adjust cost weights if found. + */ + assert( !pWInfo->bStarUsed ); + aFromTabs = pWInfo->pTabList->a; + pStart = pWInfo->pLoops; + for(iFromIdx=0, m=1; iFromIdx<nLoop; iFromIdx++, m<<=1){ + int nDep = 0; /* Number of dimension tables */ + LogEst mxRun; /* Maximum SCAN cost of a fact table */ + Bitmask mSeen = 0; /* Mask of dimension tables */ + SrcItem *pFactTab; /* The candidate fact table */ + + pFactTab = aFromTabs + iFromIdx; + if( (pFactTab->fg.jointype & (JT_OUTER|JT_CROSS))!=0 ){ + /* If the candidate fact-table is the right table of an outer join + ** restrict the search for dimension-tables to be tables to the right + ** of the fact-table. */ + if( iFromIdx+4 > nLoop ) break; /* Impossible to reach nDep>=4 */ + while( pStart && pStart->iTab<=iFromIdx ){ + pStart = pStart->pNextLoop; + } + } + for(pWLoop=pStart; pWLoop; pWLoop=pWLoop->pNextLoop){ + if( (aFromTabs[pWLoop->iTab].fg.jointype & (JT_OUTER|JT_CROSS))!=0 ){ + /* Fact-tables and dimension-tables cannot be separated by an + ** outer join (at least for the definition of fact- and dimension- + ** used by this heuristic). */ + break; + } + if( (pWLoop->prereq & m)!=0 /* pWInfo depends on iFromIdx */ + && (pWLoop->maskSelf & mSeen)==0 /* pWInfo not already a dependency */ + && (pWLoop->maskSelf & mSelfJoin)==0 /* Not a self-join */ + ){ + if( aFromTabs[pWLoop->iTab].pSTab==pFactTab->pSTab ){ + mSelfJoin |= m; + }else{ + nDep++; + mSeen |= pWLoop->maskSelf; + } + } + } + if( nDep<=3 ) continue; + + /* If we reach this point, it means that pFactTab is a fact table + ** with four or more dimensions connected by inner joins. Proceed + ** to make cost adjustments. */ + +#ifdef WHERETRACE_ENABLED + /* Make sure rStarDelta values are initialized */ + if( !pWInfo->bStarUsed ){ + for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){ + pWLoop->rStarDelta = 0; + } + } +#endif + pWInfo->bStarUsed = 1; + + /* Compute the maximum cost of any WhereLoop for the + ** fact table plus one epsilon */ + mxRun = LOGEST_MIN; + for(pWLoop=pStart; pWLoop; pWLoop=pWLoop->pNextLoop){ + if( pWLoop->iTab<iFromIdx ) continue; + if( pWLoop->iTab>iFromIdx ) break; + if( pWLoop->rRun>mxRun ) mxRun = pWLoop->rRun; + } + if( ALWAYS(mxRun<LOGEST_MAX) ) mxRun++; + + /* Increase the cost of table scans for dimension tables to be + ** slightly more than the maximum cost of the fact table */ + for(pWLoop=pStart; pWLoop; pWLoop=pWLoop->pNextLoop){ + if( (pWLoop->maskSelf & mSeen)==0 ) continue; + if( pWLoop->nLTerm ) continue; + if( pWLoop->rRun<mxRun ){ +#ifdef WHERETRACE_ENABLED /* 0x80000 */ + if( sqlite3WhereTrace & 0x80000 ){ + SrcItem *pDim = aFromTabs + pWLoop->iTab; + sqlite3DebugPrintf( + "Increase SCAN cost of dimension %s(%d) of fact %s(%d) to %d\n", + pDim->zAlias ? pDim->zAlias: pDim->pSTab->zName, pWLoop->iTab, + pFactTab->zAlias ? pFactTab->zAlias : pFactTab->pSTab->zName, + iFromIdx, mxRun + ); + } + pWLoop->rStarDelta = mxRun - pWLoop->rRun; +#endif /* WHERETRACE_ENABLED */ + pWLoop->rRun = mxRun; + } + } + } +#ifdef WHERETRACE_ENABLED /* 0x80000 */ + if( (sqlite3WhereTrace & 0x80000)!=0 && pWInfo->bStarUsed ){ + sqlite3DebugPrintf("WhereLoops changed by star-query heuristic:\n"); + for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){ + if( pWLoop->rStarDelta ){ + sqlite3WhereLoopPrint(pWLoop, &pWInfo->sWC); + } + } + } +#endif + } + return pWInfo->bStarUsed ? 18 : 12; +} + +/* +** Two WhereLoop objects, pCandidate and pBaseline, are known to have the +** same cost. Look deep into each to see if pCandidate is even slightly +** better than pBaseline. Return false if it is, if pCandidate is is preferred. +** Return true if pBaseline is preferred or if we cannot tell the difference. +** +** Result Meaning +** -------- ---------------------------------------------------------- +** true We cannot tell the difference in pCandidate and pBaseline +** false pCandidate seems like a better choice than pBaseline +*/ +static SQLITE_NOINLINE int whereLoopIsNoBetter( + const WhereLoop *pCandidate, + const WhereLoop *pBaseline +){ + if( (pCandidate->wsFlags & WHERE_INDEXED)==0 ) return 1; + if( (pBaseline->wsFlags & WHERE_INDEXED)==0 ) return 1; + if( pCandidate->u.btree.pIndex->szIdxRow < + pBaseline->u.btree.pIndex->szIdxRow ) return 0; + return 1; +} + /* ** Given the list of WhereLoop objects at pWInfo->pLoops, this routine ** attempts to find the lowest cost path that visits each WhereLoop @@ -3478,13 +5690,12 @@ static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){ int mxChoice; /* Maximum number of simultaneous paths tracked */ int nLoop; /* Number of terms in the join */ Parse *pParse; /* Parsing context */ - sqlite3 *db; /* The database connection */ int iLoop; /* Loop counter over the terms of the join */ int ii, jj; /* Loop counters */ int mxI = 0; /* Index of next entry to replace */ int nOrderBy; /* Number of ORDER BY clause terms */ LogEst mxCost = 0; /* Maximum cost of a set of paths */ - LogEst mxUnsorted = 0; /* Maximum unsorted cost of a set of path */ + LogEst mxUnsort = 0; /* Maximum unsorted cost of a set of path */ int nTo, nFrom; /* Number of valid entries in aTo[] and aFrom[] */ WherePath *aFrom; /* All nFrom paths at the previous level */ WherePath *aTo; /* The nTo best paths at the current level */ @@ -3497,14 +5708,28 @@ static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){ int nSpace; /* Bytes of space allocated at pSpace */ pParse = pWInfo->pParse; - db = pParse->db; nLoop = pWInfo->nLevel; - /* TUNING: For simple queries, only the best path is tracked. - ** For 2-way joins, the 5 best paths are followed. - ** For joins of 3 or more tables, track the 10 best paths */ - mxChoice = (nLoop<=1) ? 1 : (nLoop==2 ? 5 : 10); + WHERETRACE(0x002, ("---- begin solver. (nRowEst=%d, nQueryLoop=%d)\n", + nRowEst, pParse->nQueryLoop)); + /* TUNING: mxChoice is the maximum number of possible paths to preserve + ** at each step. Based on the number of loops in the FROM clause: + ** + ** nLoop mxChoice + ** ----- -------- + ** 1 1 // the most common case + ** 2 5 + ** 3+ 12 or 18 // see computeMxChoice() + */ + if( nLoop<=1 ){ + mxChoice = 1; + }else if( nLoop==2 ){ + mxChoice = 5; + }else if( pParse->nErr ){ + mxChoice = 1; + }else{ + mxChoice = computeMxChoice(pWInfo); + } assert( nLoop<=pWInfo->pTabList->nSrc ); - WHERETRACE(0x002, ("---- begin solver. (nRowEst=%d)\n", nRowEst)); /* If nRowEst is zero and there is an ORDER BY clause, ignore it. In this ** case the purpose of this call is to estimate the number of rows returned @@ -3520,8 +5745,8 @@ static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){ /* Allocate and initialize space for aTo, aFrom and aSortCost[] */ nSpace = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2; nSpace += sizeof(LogEst) * nOrderBy; - pSpace = sqlite3DbMallocRawNN(db, nSpace); - if( pSpace==0 ) return SQLITE_NOMEM; + pSpace = sqlite3StackAllocRawNN(pParse->db, nSpace); + if( pSpace==0 ) return SQLITE_NOMEM_BKPT; aTo = (WherePath*)pSpace; aFrom = aTo+mxChoice; memset(aFrom, 0, sizeof(aFrom[0])); @@ -3534,7 +5759,7 @@ static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){ ** space for the aSortCost[] array. Each element of the aSortCost array ** is either zero - meaning it has not yet been initialized - or the ** cost of sorting nRowEst rows of data where the first X terms of - ** the ORDER BY clause are already in order, where X is the array + ** the ORDER BY clause are already in order, where X is the array ** index. */ aSortCost = (LogEst*)pX; memset(aSortCost, 0, sizeof(LogEst) * nOrderBy); @@ -3555,7 +5780,7 @@ static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){ ** in this case the query may return a maximum of one row, the results ** are already in the requested order. Set isOrdered to nOrderBy to ** indicate this. Or, if nLoop is greater than zero, set isOrdered to - ** -1, indicating that the result set may or may not be ordered, + ** -1, indicating that the result set may or may not be ordered, ** depending on the loops added to the current plan. */ aFrom[0].isOrdered = nLoop>0 ? -1 : nOrderBy; } @@ -3569,20 +5794,34 @@ static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){ for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){ LogEst nOut; /* Rows visited by (pFrom+pWLoop) */ LogEst rCost; /* Cost of path (pFrom+pWLoop) */ - LogEst rUnsorted; /* Unsorted cost of (pFrom+pWLoop) */ - i8 isOrdered = pFrom->isOrdered; /* isOrdered for (pFrom+pWLoop) */ + LogEst rUnsort; /* Unsorted cost of (pFrom+pWLoop) */ + i8 isOrdered; /* isOrdered for (pFrom+pWLoop) */ Bitmask maskNew; /* Mask of src visited by (..) */ - Bitmask revMask = 0; /* Mask of rev-order loops for (..) */ + Bitmask revMask; /* Mask of rev-order loops for (..) */ if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue; if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue; - /* At this point, pWLoop is a candidate to be the next loop. + if( (pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 && pFrom->nRow<3 ){ + /* Do not use an automatic index if the this loop is expected + ** to run less than 1.25 times. It is tempting to also exclude + ** automatic index usage on an outer loop, but sometimes an automatic + ** index is useful in the outer loop of a correlated subquery. */ + assert( 10==sqlite3LogEst(2) ); + continue; + } + + /* At this point, pWLoop is a candidate to be the next loop. ** Compute its cost */ - rUnsorted = sqlite3LogEstAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow); - rUnsorted = sqlite3LogEstAdd(rUnsorted, pFrom->rUnsorted); + rUnsort = pWLoop->rRun + pFrom->nRow; + if( pWLoop->rSetup ){ + rUnsort = sqlite3LogEstAdd(pWLoop->rSetup, rUnsort); + } + rUnsort = sqlite3LogEstAdd(rUnsort, pFrom->rUnsort); nOut = pFrom->nRow + pWLoop->nOut; maskNew = pFrom->maskLoop | pWLoop->maskSelf; + isOrdered = pFrom->isOrdered; if( isOrdered<0 ){ + revMask = 0; isOrdered = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags, iLoop, pWLoop, &revMask); @@ -3592,33 +5831,46 @@ static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){ if( isOrdered>=0 && isOrdered<nOrderBy ){ if( aSortCost[isOrdered]==0 ){ aSortCost[isOrdered] = whereSortingCost( - nRowEst, nOrderBy, isOrdered + pWInfo, nRowEst, nOrderBy, isOrdered ); } - rCost = sqlite3LogEstAdd(rUnsorted, aSortCost[isOrdered]); + /* TUNING: Add a small extra penalty (3) to sorting as an + ** extra encouragement to the query planner to select a plan + ** where the rows emerge in the correct order without any sorting + ** required. */ + rCost = sqlite3LogEstAdd(rUnsort, aSortCost[isOrdered]) + 3; WHERETRACE(0x002, ("---- sort cost=%-3d (%d/%d) increases cost %3d to %-3d\n", - aSortCost[isOrdered], (nOrderBy-isOrdered), nOrderBy, - rUnsorted, rCost)); + aSortCost[isOrdered], (nOrderBy-isOrdered), nOrderBy, + rUnsort, rCost)); }else{ - rCost = rUnsorted; + rCost = rUnsort; + rUnsort -= 2; /* TUNING: Slight bias in favor of no-sort plans */ } /* Check to see if pWLoop should be added to the set of ** mxChoice best-so-far paths. ** ** First look for an existing path among best-so-far paths - ** that covers the same set of loops and has the same isOrdered - ** setting as the current path candidate. + ** that: + ** (1) covers the same set of loops, and + ** (2) has a compatible isOrdered value. + ** + ** "Compatible isOrdered value" means either + ** (A) both have isOrdered==-1, or + ** (B) both have isOrder>=0, or + ** (C) ordering does not matter because this is the last round + ** of the solver. ** ** The term "((pTo->isOrdered^isOrdered)&0x80)==0" is equivalent ** to (pTo->isOrdered==(-1))==(isOrdered==(-1))" for the range ** of legal values for isOrdered, -1..64. */ + testcase( nTo==0 ); for(jj=0, pTo=aTo; jj<nTo; jj++, pTo++){ if( pTo->maskLoop==maskNew - && ((pTo->isOrdered^isOrdered)&0x80)==0 + && ( ((pTo->isOrdered^isOrdered)&0x80)==0 || iLoop==nLoop-1 ) ){ testcase( jj==nTo-1 ); break; @@ -3627,15 +5879,15 @@ static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){ if( jj>=nTo ){ /* None of the existing best-so-far paths match the candidate. */ if( nTo>=mxChoice - && (rCost>mxCost || (rCost==mxCost && rUnsorted>=mxUnsorted)) + && (rCost>mxCost || (rCost==mxCost && rUnsort>=mxUnsort)) ){ /* The current candidate is no better than any of the mxChoice ** paths currently in the best-so-far buffer. So discard ** this candidate as not viable. */ #ifdef WHERETRACE_ENABLED /* 0x4 */ if( sqlite3WhereTrace&0x4 ){ - sqlite3DebugPrintf("Skip %s cost=%-3d,%3d order=%c\n", - wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, + sqlite3DebugPrintf("Skip %s cost=%-3d,%3d,%3d order=%c\n", + wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsort, isOrdered>=0 ? isOrdered+'0' : '?'); } #endif @@ -3653,26 +5905,35 @@ static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){ pTo = &aTo[jj]; #ifdef WHERETRACE_ENABLED /* 0x4 */ if( sqlite3WhereTrace&0x4 ){ - sqlite3DebugPrintf("New %s cost=%-3d,%3d order=%c\n", - wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, + sqlite3DebugPrintf("New %s cost=%-3d,%3d,%3d order=%c\n", + wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsort, isOrdered>=0 ? isOrdered+'0' : '?'); } #endif }else{ /* Control reaches here if best-so-far path pTo=aTo[jj] covers the - ** same set of loops and has the sam isOrdered setting as the + ** same set of loops and has the same isOrdered setting as the ** candidate path. Check to see if the candidate should replace - ** pTo or if the candidate should be skipped */ - if( pTo->rCost<rCost || (pTo->rCost==rCost && pTo->nRow<=nOut) ){ + ** pTo or if the candidate should be skipped. + ** + ** The conditional is an expanded vector comparison equivalent to: + ** (pTo->rCost,pTo->nRow,pTo->rUnsort) <= (rCost,nOut,rUnsort) + */ + if( (pTo->rCost<rCost) + || (pTo->rCost==rCost && pTo->nRow<nOut) + || (pTo->rCost==rCost && pTo->nRow==nOut && pTo->rUnsort<rUnsort) + || (pTo->rCost==rCost && pTo->nRow==nOut && pTo->rUnsort==rUnsort + && whereLoopIsNoBetter(pWLoop, pTo->aLoop[iLoop]) ) + ){ #ifdef WHERETRACE_ENABLED /* 0x4 */ if( sqlite3WhereTrace&0x4 ){ sqlite3DebugPrintf( - "Skip %s cost=%-3d,%3d order=%c", - wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, + "Skip %s cost=%-3d,%3d,%3d order=%c", + wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsort, isOrdered>=0 ? isOrdered+'0' : '?'); - sqlite3DebugPrintf(" vs %s cost=%-3d,%d order=%c\n", + sqlite3DebugPrintf(" vs %s cost=%-3d,%3d,%3d order=%c\n", wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow, - pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?'); + pTo->rUnsort, pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?'); } #endif /* Discard the candidate path from further consideration */ @@ -3685,12 +5946,12 @@ static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){ #ifdef WHERETRACE_ENABLED /* 0x4 */ if( sqlite3WhereTrace&0x4 ){ sqlite3DebugPrintf( - "Update %s cost=%-3d,%3d order=%c", - wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, + "Update %s cost=%-3d,%3d,%3d order=%c", + wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsort, isOrdered>=0 ? isOrdered+'0' : '?'); - sqlite3DebugPrintf(" was %s cost=%-3d,%3d order=%c\n", + sqlite3DebugPrintf(" was %s cost=%-3d,%3d,%3d order=%c\n", wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow, - pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?'); + pTo->rUnsort, pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?'); } #endif } @@ -3699,20 +5960,20 @@ static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){ pTo->revLoop = revMask; pTo->nRow = nOut; pTo->rCost = rCost; - pTo->rUnsorted = rUnsorted; + pTo->rUnsort = rUnsort; pTo->isOrdered = isOrdered; memcpy(pTo->aLoop, pFrom->aLoop, sizeof(WhereLoop*)*iLoop); pTo->aLoop[iLoop] = pWLoop; if( nTo>=mxChoice ){ mxI = 0; mxCost = aTo[0].rCost; - mxUnsorted = aTo[0].nRow; + mxUnsort = aTo[0].nRow; for(jj=1, pTo=&aTo[1]; jj<mxChoice; jj++, pTo++){ - if( pTo->rCost>mxCost - || (pTo->rCost==mxCost && pTo->rUnsorted>mxUnsorted) + if( pTo->rCost>mxCost + || (pTo->rCost==mxCost && pTo->rUnsort>mxUnsort) ){ mxCost = pTo->rCost; - mxUnsorted = pTo->rUnsorted; + mxUnsort = pTo->rUnsort; mxI = jj; } } @@ -3722,17 +5983,32 @@ static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){ #ifdef WHERETRACE_ENABLED /* >=2 */ if( sqlite3WhereTrace & 0x02 ){ + LogEst rMin, rFloor = 0; + int nDone = 0; + int nProgress; sqlite3DebugPrintf("---- after round %d ----\n", iLoop); - for(ii=0, pTo=aTo; ii<nTo; ii++, pTo++){ - sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c", - wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow, - pTo->isOrdered>=0 ? (pTo->isOrdered+'0') : '?'); - if( pTo->isOrdered>0 ){ - sqlite3DebugPrintf(" rev=0x%llx\n", pTo->revLoop); - }else{ - sqlite3DebugPrintf("\n"); + do{ + nProgress = 0; + rMin = 0x7fff; + for(ii=0, pTo=aTo; ii<nTo; ii++, pTo++){ + if( pTo->rCost>rFloor && pTo->rCost<rMin ) rMin = pTo->rCost; } - } + for(ii=0, pTo=aTo; ii<nTo; ii++, pTo++){ + if( pTo->rCost==rMin ){ + sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c", + wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow, + pTo->isOrdered>=0 ? (pTo->isOrdered+'0') : '?'); + if( pTo->isOrdered>0 ){ + sqlite3DebugPrintf(" rev=0x%llx\n", pTo->revLoop); + }else{ + sqlite3DebugPrintf("\n"); + } + nDone++; + nProgress++; + } + } + rFloor = rMin; + }while( nDone<nTo && nProgress>0 ); } #endif @@ -3745,15 +6021,14 @@ static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){ if( nFrom==0 ){ sqlite3ErrorMsg(pParse, "no query solution"); - sqlite3DbFree(db, pSpace); + sqlite3StackFreeNN(pParse->db, pSpace); return SQLITE_ERROR; } - - /* Find the lowest cost path. pFrom will be left pointing to that path */ + + /* Only one path is available, which is the best path */ + assert( nFrom==1 ); pFrom = aFrom; - for(ii=1; ii<nFrom; ii++){ - if( pFrom->rCost>aFrom[ii].rCost ) pFrom = &aFrom[ii]; - } + assert( pWInfo->nLevel==nLoop ); /* Load the lowest cost path into pWInfo */ for(iLoop=0; iLoop<nLoop; iLoop++){ @@ -3774,21 +6049,48 @@ static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){ pWInfo->eDistinct = WHERE_DISTINCT_ORDERED; } } + pWInfo->bOrderedInnerLoop = 0; if( pWInfo->pOrderBy ){ + pWInfo->nOBSat = pFrom->isOrdered; if( pWInfo->wctrlFlags & WHERE_DISTINCTBY ){ if( pFrom->isOrdered==pWInfo->pOrderBy->nExpr ){ pWInfo->eDistinct = WHERE_DISTINCT_ORDERED; } + /* vvv--- See check-in [12ad822d9b827777] on 2023-03-16 ---vvv */ + assert( pWInfo->pSelect->pOrderBy==0 + || pWInfo->nOBSat <= pWInfo->pSelect->pOrderBy->nExpr ); }else{ - pWInfo->nOBSat = pFrom->isOrdered; - if( pWInfo->nOBSat<0 ) pWInfo->nOBSat = 0; pWInfo->revMask = pFrom->revLoop; + if( pWInfo->nOBSat<=0 ){ + pWInfo->nOBSat = 0; + if( nLoop>0 ){ + u32 wsFlags = pFrom->aLoop[nLoop-1]->wsFlags; + if( (wsFlags & WHERE_ONEROW)==0 + && (wsFlags&(WHERE_IPK|WHERE_COLUMN_IN))!=(WHERE_IPK|WHERE_COLUMN_IN) + ){ + Bitmask m = 0; + int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy, pFrom, + WHERE_ORDERBY_LIMIT, nLoop-1, pFrom->aLoop[nLoop-1], &m); + testcase( wsFlags & WHERE_IPK ); + testcase( wsFlags & WHERE_COLUMN_IN ); + if( rc==pWInfo->pOrderBy->nExpr ){ + pWInfo->bOrderedInnerLoop = 1; + pWInfo->revMask = m; + } + } + } + }else if( nLoop + && pWInfo->nOBSat==1 + && (pWInfo->wctrlFlags & (WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX))!=0 + ){ + pWInfo->bOrderedInnerLoop = 1; + } } if( (pWInfo->wctrlFlags & WHERE_SORTBYGROUP) && pWInfo->nOBSat==pWInfo->pOrderBy->nExpr && nLoop>0 ){ Bitmask revMask = 0; - int nOrder = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy, + int nOrder = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy, pFrom, 0, nLoop-1, pFrom->aLoop[nLoop-1], &revMask ); assert( pWInfo->sorted==0 ); @@ -3799,101 +6101,494 @@ static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){ } } - pWInfo->nRowOut = pFrom->nRow; +#ifdef WHERETRACE_ENABLED + pWInfo->rTotalCost = pFrom->rCost; +#endif /* Free temporary memory and return success */ - sqlite3DbFree(db, pSpace); + sqlite3StackFreeNN(pParse->db, pSpace); return SQLITE_OK; } /* -** Most queries use only a single table (they are not joins) and have -** simple == constraints against indexed fields. This routine attempts -** to plan those simple cases using much less ceremony than the -** general-purpose query planner, and thereby yield faster sqlite3_prepare() -** times for the common case. +** This routine implements a heuristic designed to improve query planning. +** This routine is called in between the first and second call to +** wherePathSolver(). Hence the name "Interstage" "Heuristic". +** +** The first call to wherePathSolver() (hereafter just "solver()") computes +** the best path without regard to the order of the outputs. The second call +** to the solver() builds upon the first call to try to find an alternative +** path that satisfies the ORDER BY clause. +** +** This routine looks at the results of the first solver() run, and for +** every FROM clause term in the resulting query plan that uses an equality +** constraint against an index, disable other WhereLoops for that same +** FROM clause term that would try to do a full-table scan. This prevents +** an index search from being converted into a full-table scan in order to +** satisfy an ORDER BY clause, since even though we might get slightly better +** performance using the full-scan without sorting if the output size +** estimates are very precise, we might also get severe performance +** degradation using the full-scan if the output size estimate is too large. +** It is better to err on the side of caution. +** +** Except, if the first solver() call generated a full-table scan in an outer +** loop then stop this analysis at the first full-scan, since the second +** solver() run might try to swap that full-scan for another in order to +** get the output into the correct order. In other words, we allow a +** rewrite like this: +** +** First Solver() Second Solver() +** |-- SCAN t1 |-- SCAN t2 +** |-- SEARCH t2 `-- SEARCH t1 +** `-- SORT USING B-TREE +** +** The purpose of this routine is to disallow rewrites such as: +** +** First Solver() Second Solver() +** |-- SEARCH t1 |-- SCAN t2 <--- bad! +** |-- SEARCH t2 `-- SEARCH t1 +** `-- SORT USING B-TREE +** +** See test cases in test/whereN.test for the real-world query that +** originally provoked this heuristic. +*/ +static SQLITE_NOINLINE void whereInterstageHeuristic(WhereInfo *pWInfo){ + int i; +#ifdef WHERETRACE_ENABLED + int once = 0; +#endif + for(i=0; i<pWInfo->nLevel; i++){ + WhereLoop *p = pWInfo->a[i].pWLoop; + if( p==0 ) break; + if( (p->wsFlags & WHERE_VIRTUALTABLE)!=0 ){ + /* Treat a vtab scan as similar to a full-table scan */ + break; + } + if( (p->wsFlags & (WHERE_COLUMN_EQ|WHERE_COLUMN_NULL|WHERE_COLUMN_IN))!=0 ){ + u8 iTab = p->iTab; + WhereLoop *pLoop; + for(pLoop=pWInfo->pLoops; pLoop; pLoop=pLoop->pNextLoop){ + if( pLoop->iTab!=iTab ) continue; + if( (pLoop->wsFlags & (WHERE_CONSTRAINT|WHERE_AUTO_INDEX))!=0 ){ + /* Auto-index and index-constrained loops allowed to remain */ + continue; + } +#ifdef WHERETRACE_ENABLED + if( sqlite3WhereTrace & 0x80 ){ + if( once==0 ){ + sqlite3DebugPrintf("Loops disabled by interstage heuristic:\n"); + once = 1; + } + sqlite3WhereLoopPrint(pLoop, &pWInfo->sWC); + } +#endif /* WHERETRACE_ENABLED */ + pLoop->prereq = ALLBITS; /* Prevent 2nd solver() from using this one */ + } + }else{ + break; + } + } +} + +/* +** Most queries use only a single table (they are not joins) and have +** simple == constraints against indexed fields. This routine attempts +** to plan those simple cases using much less ceremony than the +** general-purpose query planner, and thereby yield faster sqlite3_prepare() +** times for the common case. +** +** Return non-zero on success, if this query can be handled by this +** no-frills query planner. Return zero if this query needs the +** general-purpose query planner. +*/ +static int whereShortCut(WhereLoopBuilder *pBuilder){ + WhereInfo *pWInfo; + SrcItem *pItem; + WhereClause *pWC; + WhereTerm *pTerm; + WhereLoop *pLoop; + int iCur; + int j; + Table *pTab; + Index *pIdx; + WhereScan scan; + + pWInfo = pBuilder->pWInfo; + if( pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE ) return 0; + assert( pWInfo->pTabList->nSrc>=1 ); + pItem = pWInfo->pTabList->a; + pTab = pItem->pSTab; + if( IsVirtual(pTab) ) return 0; + if( pItem->fg.isIndexedBy || pItem->fg.notIndexed ){ + testcase( pItem->fg.isIndexedBy ); + testcase( pItem->fg.notIndexed ); + return 0; + } + iCur = pItem->iCursor; + pWC = &pWInfo->sWC; + pLoop = pBuilder->pNew; + pLoop->wsFlags = 0; + pLoop->nSkip = 0; + pTerm = whereScanInit(&scan, pWC, iCur, -1, WO_EQ|WO_IS, 0); + while( pTerm && pTerm->prereqRight ) pTerm = whereScanNext(&scan); + if( pTerm ){ + testcase( pTerm->eOperator & WO_IS ); + pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW; + pLoop->aLTerm[0] = pTerm; + pLoop->nLTerm = 1; + pLoop->u.btree.nEq = 1; + /* TUNING: Cost of a rowid lookup is 10 */ + pLoop->rRun = 33; /* 33==sqlite3LogEst(10) */ + }else{ + for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ + int opMask; + assert( pLoop->aLTermSpace==pLoop->aLTerm ); + if( !IsUniqueIndex(pIdx) + || pIdx->pPartIdxWhere!=0 + || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace) + ) continue; + opMask = pIdx->uniqNotNull ? (WO_EQ|WO_IS) : WO_EQ; + for(j=0; j<pIdx->nKeyCol; j++){ + pTerm = whereScanInit(&scan, pWC, iCur, j, opMask, pIdx); + while( pTerm && pTerm->prereqRight ) pTerm = whereScanNext(&scan); + if( pTerm==0 ) break; + testcase( pTerm->eOperator & WO_IS ); + pLoop->aLTerm[j] = pTerm; + } + if( j!=pIdx->nKeyCol ) continue; + pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_ONEROW|WHERE_INDEXED; + if( pIdx->isCovering || (pItem->colUsed & pIdx->colNotIdxed)==0 ){ + pLoop->wsFlags |= WHERE_IDX_ONLY; + } + pLoop->nLTerm = j; + pLoop->u.btree.nEq = j; + pLoop->u.btree.pIndex = pIdx; + /* TUNING: Cost of a unique index lookup is 15 */ + pLoop->rRun = 39; /* 39==sqlite3LogEst(15) */ + break; + } + } + if( pLoop->wsFlags ){ + pLoop->nOut = (LogEst)1; + pWInfo->a[0].pWLoop = pLoop; + assert( pWInfo->sMaskSet.n==1 && iCur==pWInfo->sMaskSet.ix[0] ); + pLoop->maskSelf = 1; /* sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur); */ + pWInfo->a[0].iTabCur = iCur; + pWInfo->nRowOut = 1; + if( pWInfo->pOrderBy ) pWInfo->nOBSat = pWInfo->pOrderBy->nExpr; + if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){ + pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; + } + if( scan.iEquiv>1 ) pLoop->wsFlags |= WHERE_TRANSCONS; +#ifdef SQLITE_DEBUG + pLoop->cId = '0'; +#endif +#ifdef WHERETRACE_ENABLED + if( sqlite3WhereTrace & 0x02 ){ + sqlite3DebugPrintf("whereShortCut() used to compute solution\n"); + } +#endif + return 1; + } + return 0; +} + +/* +** Helper function for exprIsDeterministic(). +*/ +static int exprNodeIsDeterministic(Walker *pWalker, Expr *pExpr){ + if( pExpr->op==TK_FUNCTION && ExprHasProperty(pExpr, EP_ConstFunc)==0 ){ + pWalker->eCode = 0; + return WRC_Abort; + } + return WRC_Continue; +} + +/* +** Return true if the expression contains no non-deterministic SQL +** functions. Do not consider non-deterministic SQL functions that are +** part of sub-select statements. +*/ +static int exprIsDeterministic(Expr *p){ + Walker w; + memset(&w, 0, sizeof(w)); + w.eCode = 1; + w.xExprCallback = exprNodeIsDeterministic; + w.xSelectCallback = sqlite3SelectWalkFail; + sqlite3WalkExpr(&w, p); + return w.eCode; +} + + +#ifdef WHERETRACE_ENABLED +/* +** Display all WhereLoops in pWInfo +*/ +static void showAllWhereLoops(WhereInfo *pWInfo, WhereClause *pWC){ + if( sqlite3WhereTrace ){ /* Display all of the WhereLoop objects */ + WhereLoop *p; + int i; + static const char zLabel[] = "0123456789abcdefghijklmnopqrstuvwyxz" + "ABCDEFGHIJKLMNOPQRSTUVWYXZ"; + for(p=pWInfo->pLoops, i=0; p; p=p->pNextLoop, i++){ + p->cId = zLabel[i%(sizeof(zLabel)-1)]; + sqlite3WhereLoopPrint(p, pWC); + } + } +} +# define WHERETRACE_ALL_LOOPS(W,C) showAllWhereLoops(W,C) +#else +# define WHERETRACE_ALL_LOOPS(W,C) +#endif + +/* Attempt to omit tables from a join that do not affect the result. +** For a table to not affect the result, the following must be true: +** +** 1) The query must not be an aggregate. +** 2) The table must be the RHS of a LEFT JOIN. +** 3) Either the query must be DISTINCT, or else the ON or USING clause +** must contain a constraint that limits the scan of the table to +** at most a single row. +** 4) The table must not be referenced by any part of the query apart +** from its own USING or ON clause. +** 5) The table must not have an inner-join ON or USING clause if there is +** a RIGHT JOIN anywhere in the query. Otherwise the ON/USING clause +** might move from the right side to the left side of the RIGHT JOIN. +** Note: Due to (2), this condition can only arise if the table is +** the right-most table of a subquery that was flattened into the +** main query and that subquery was the right-hand operand of an +** inner join that held an ON or USING clause. +** 6) The ORDER BY clause has 63 or fewer terms +** 7) The omit-noop-join optimization is enabled. +** +** Items (1), (6), and (7) are checked by the caller. +** +** For example, given: +** +** CREATE TABLE t1(ipk INTEGER PRIMARY KEY, v1); +** CREATE TABLE t2(ipk INTEGER PRIMARY KEY, v2); +** CREATE TABLE t3(ipk INTEGER PRIMARY KEY, v3); +** +** then table t2 can be omitted from the following: +** +** SELECT v1, v3 FROM t1 +** LEFT JOIN t2 ON (t1.ipk=t2.ipk) +** LEFT JOIN t3 ON (t1.ipk=t3.ipk) +** +** or from: +** +** SELECT DISTINCT v1, v3 FROM t1 +** LEFT JOIN t2 +** LEFT JOIN t3 ON (t1.ipk=t3.ipk) +*/ +static SQLITE_NOINLINE Bitmask whereOmitNoopJoin( + WhereInfo *pWInfo, + Bitmask notReady +){ + int i; + Bitmask tabUsed; + int hasRightJoin; + + /* Preconditions checked by the caller */ + assert( pWInfo->nLevel>=2 ); + assert( OptimizationEnabled(pWInfo->pParse->db, SQLITE_OmitNoopJoin) ); + + /* These two preconditions checked by the caller combine to guarantee + ** condition (1) of the header comment */ + assert( pWInfo->pResultSet!=0 ); + assert( 0==(pWInfo->wctrlFlags & WHERE_AGG_DISTINCT) ); + + tabUsed = sqlite3WhereExprListUsage(&pWInfo->sMaskSet, pWInfo->pResultSet); + if( pWInfo->pOrderBy ){ + tabUsed |= sqlite3WhereExprListUsage(&pWInfo->sMaskSet, pWInfo->pOrderBy); + } + hasRightJoin = (pWInfo->pTabList->a[0].fg.jointype & JT_LTORJ)!=0; + for(i=pWInfo->nLevel-1; i>=1; i--){ + WhereTerm *pTerm, *pEnd; + SrcItem *pItem; + WhereLoop *pLoop; + Bitmask m1; + pLoop = pWInfo->a[i].pWLoop; + pItem = &pWInfo->pTabList->a[pLoop->iTab]; + if( (pItem->fg.jointype & (JT_LEFT|JT_RIGHT))!=JT_LEFT ) continue; + if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT)==0 + && (pLoop->wsFlags & WHERE_ONEROW)==0 + ){ + continue; + } + if( (tabUsed & pLoop->maskSelf)!=0 ) continue; + pEnd = pWInfo->sWC.a + pWInfo->sWC.nTerm; + for(pTerm=pWInfo->sWC.a; pTerm<pEnd; pTerm++){ + if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){ + if( !ExprHasProperty(pTerm->pExpr, EP_OuterON) + || pTerm->pExpr->w.iJoin!=pItem->iCursor + ){ + break; + } + } + if( hasRightJoin + && ExprHasProperty(pTerm->pExpr, EP_InnerON) + && NEVER(pTerm->pExpr->w.iJoin==pItem->iCursor) + ){ + break; /* restriction (5) */ + } + } + if( pTerm<pEnd ) continue; + WHERETRACE(0xffffffff,("-> omit unused FROM-clause term %c\n",pLoop->cId)); + m1 = MASKBIT(i)-1; + testcase( ((pWInfo->revMask>>1) & ~m1)!=0 ); + pWInfo->revMask = (m1 & pWInfo->revMask) | ((pWInfo->revMask>>1) & ~m1); + notReady &= ~pLoop->maskSelf; + for(pTerm=pWInfo->sWC.a; pTerm<pEnd; pTerm++){ + if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){ + pTerm->wtFlags |= TERM_CODED; + } + } + if( i!=pWInfo->nLevel-1 ){ + int nByte = (pWInfo->nLevel-1-i) * sizeof(WhereLevel); + memmove(&pWInfo->a[i], &pWInfo->a[i+1], nByte); + } + pWInfo->nLevel--; + assert( pWInfo->nLevel>0 ); + } + return notReady; +} + +/* +** Check to see if there are any SEARCH loops that might benefit from +** using a Bloom filter. Consider a Bloom filter if: +** +** (1) The SEARCH happens more than N times where N is the number +** of rows in the table that is being considered for the Bloom +** filter. +** (2) Some searches are expected to find zero rows. (This is determined +** by the WHERE_SELFCULL flag on the term.) +** (3) Bloom-filter processing is not disabled. (Checked by the +** caller.) +** (4) The size of the table being searched is known by ANALYZE. +** +** This block of code merely checks to see if a Bloom filter would be +** appropriate, and if so sets the WHERE_BLOOMFILTER flag on the +** WhereLoop. The implementation of the Bloom filter comes further +** down where the code for each WhereLoop is generated. +*/ +static SQLITE_NOINLINE void whereCheckIfBloomFilterIsUseful( + const WhereInfo *pWInfo +){ + int i; + LogEst nSearch = 0; + + assert( pWInfo->nLevel>=2 ); + assert( OptimizationEnabled(pWInfo->pParse->db, SQLITE_BloomFilter) ); + for(i=0; i<pWInfo->nLevel; i++){ + WhereLoop *pLoop = pWInfo->a[i].pWLoop; + const unsigned int reqFlags = (WHERE_SELFCULL|WHERE_COLUMN_EQ); + SrcItem *pItem = &pWInfo->pTabList->a[pLoop->iTab]; + Table *pTab = pItem->pSTab; + if( (pTab->tabFlags & TF_HasStat1)==0 ) break; + pTab->tabFlags |= TF_MaybeReanalyze; + if( i>=1 + && (pLoop->wsFlags & reqFlags)==reqFlags + /* vvvvvv--- Always the case if WHERE_COLUMN_EQ is defined */ + && ALWAYS((pLoop->wsFlags & (WHERE_IPK|WHERE_INDEXED))!=0) + ){ + if( nSearch > pTab->nRowLogEst ){ + testcase( pItem->fg.jointype & JT_LEFT ); + pLoop->wsFlags |= WHERE_BLOOMFILTER; + pLoop->wsFlags &= ~WHERE_IDX_ONLY; + WHERETRACE(0xffffffff, ( + "-> use Bloom-filter on loop %c because there are ~%.1e " + "lookups into %s which has only ~%.1e rows\n", + pLoop->cId, (double)sqlite3LogEstToInt(nSearch), pTab->zName, + (double)sqlite3LogEstToInt(pTab->nRowLogEst))); + } + } + nSearch += pLoop->nOut; + } +} + +/* +** The index pIdx is used by a query and contains one or more expressions. +** In other words pIdx is an index on an expression. iIdxCur is the cursor +** number for the index and iDataCur is the cursor number for the corresponding +** table. ** -** Return non-zero on success, if this query can be handled by this -** no-frills query planner. Return zero if this query needs the -** general-purpose query planner. +** This routine adds IndexedExpr entries to the Parse->pIdxEpr field for +** each of the expressions in the index so that the expression code generator +** will know to replace occurrences of the indexed expression with +** references to the corresponding column of the index. */ -static int whereShortCut(WhereLoopBuilder *pBuilder){ - WhereInfo *pWInfo; - struct SrcList_item *pItem; - WhereClause *pWC; - WhereTerm *pTerm; - WhereLoop *pLoop; - int iCur; - int j; +static SQLITE_NOINLINE void whereAddIndexedExpr( + Parse *pParse, /* Add IndexedExpr entries to pParse->pIdxEpr */ + Index *pIdx, /* The index-on-expression that contains the expressions */ + int iIdxCur, /* Cursor number for pIdx */ + SrcItem *pTabItem /* The FROM clause entry for the table */ +){ + int i; + IndexedExpr *p; Table *pTab; - Index *pIdx; - - pWInfo = pBuilder->pWInfo; - if( pWInfo->wctrlFlags & WHERE_FORCE_TABLE ) return 0; - assert( pWInfo->pTabList->nSrc>=1 ); - pItem = pWInfo->pTabList->a; - pTab = pItem->pTab; - if( IsVirtual(pTab) ) return 0; - if( pItem->fg.isIndexedBy ) return 0; - iCur = pItem->iCursor; - pWC = &pWInfo->sWC; - pLoop = pBuilder->pNew; - pLoop->wsFlags = 0; - pLoop->nSkip = 0; - pTerm = sqlite3WhereFindTerm(pWC, iCur, -1, 0, WO_EQ|WO_IS, 0); - if( pTerm ){ - testcase( pTerm->eOperator & WO_IS ); - pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW; - pLoop->aLTerm[0] = pTerm; - pLoop->nLTerm = 1; - pLoop->u.btree.nEq = 1; - /* TUNING: Cost of a rowid lookup is 10 */ - pLoop->rRun = 33; /* 33==sqlite3LogEst(10) */ - }else{ - for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ - int opMask; - assert( pLoop->aLTermSpace==pLoop->aLTerm ); - if( !IsUniqueIndex(pIdx) - || pIdx->pPartIdxWhere!=0 - || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace) - ) continue; - opMask = pIdx->uniqNotNull ? (WO_EQ|WO_IS) : WO_EQ; - for(j=0; j<pIdx->nKeyCol; j++){ - pTerm = sqlite3WhereFindTerm(pWC, iCur, j, 0, opMask, pIdx); - if( pTerm==0 ) break; - testcase( pTerm->eOperator & WO_IS ); - pLoop->aLTerm[j] = pTerm; - } - if( j!=pIdx->nKeyCol ) continue; - pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_ONEROW|WHERE_INDEXED; - if( pIdx->isCovering || (pItem->colUsed & ~columnsInIndex(pIdx))==0 ){ - pLoop->wsFlags |= WHERE_IDX_ONLY; - } - pLoop->nLTerm = j; - pLoop->u.btree.nEq = j; - pLoop->u.btree.pIndex = pIdx; - /* TUNING: Cost of a unique index lookup is 15 */ - pLoop->rRun = 39; /* 39==sqlite3LogEst(15) */ - break; + assert( pIdx->bHasExpr ); + pTab = pIdx->pTable; + for(i=0; i<pIdx->nColumn; i++){ + Expr *pExpr; + int j = pIdx->aiColumn[i]; + if( j==XN_EXPR ){ + pExpr = pIdx->aColExpr->a[i].pExpr; + }else if( j>=0 && (pTab->aCol[j].colFlags & COLFLAG_VIRTUAL)!=0 ){ + pExpr = sqlite3ColumnExpr(pTab, &pTab->aCol[j]); + }else{ + continue; } - } - if( pLoop->wsFlags ){ - pLoop->nOut = (LogEst)1; - pWInfo->a[0].pWLoop = pLoop; - pLoop->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur); - pWInfo->a[0].iTabCur = iCur; - pWInfo->nRowOut = 1; - if( pWInfo->pOrderBy ) pWInfo->nOBSat = pWInfo->pOrderBy->nExpr; - if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){ - pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; + if( sqlite3ExprIsConstant(0,pExpr) ) continue; + p = sqlite3DbMallocRaw(pParse->db, sizeof(IndexedExpr)); + if( p==0 ) break; + p->pIENext = pParse->pIdxEpr; +#ifdef WHERETRACE_ENABLED + if( sqlite3WhereTrace & 0x200 ){ + sqlite3DebugPrintf("New pParse->pIdxEpr term {%d,%d}\n", iIdxCur, i); + if( sqlite3WhereTrace & 0x5000 ) sqlite3ShowExpr(pExpr); } -#ifdef SQLITE_DEBUG - pLoop->cId = '0'; #endif - return 1; + p->pExpr = sqlite3ExprDup(pParse->db, pExpr, 0); + p->iDataCur = pTabItem->iCursor; + p->iIdxCur = iIdxCur; + p->iIdxCol = i; + p->bMaybeNullRow = (pTabItem->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))!=0; + if( sqlite3IndexAffinityStr(pParse->db, pIdx) ){ + p->aff = pIdx->zColAff[i]; + } +#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS + p->zIdxName = pIdx->zName; +#endif + pParse->pIdxEpr = p; + if( p->pIENext==0 ){ + void *pArg = (void*)&pParse->pIdxEpr; + sqlite3ParserAddCleanup(pParse, whereIndexedExprCleanup, pArg); + } + } +} + +/* +** Set the reverse-scan order mask to one for all tables in the query +** with the exception of MATERIALIZED common table expressions that have +** their own internal ORDER BY clauses. +** +** This implements the PRAGMA reverse_unordered_selects=ON setting. +** (Also SQLITE_DBCONFIG_REVERSE_SCANORDER). +*/ +static SQLITE_NOINLINE void whereReverseScanOrder(WhereInfo *pWInfo){ + int ii; + for(ii=0; ii<pWInfo->pTabList->nSrc; ii++){ + SrcItem *pItem = &pWInfo->pTabList->a[ii]; + if( !pItem->fg.isCte + || pItem->u2.pCteUse->eM10d!=M10d_Yes + || NEVER(pItem->fg.isSubquery==0) + || pItem->u4.pSubq->pSelect->pOrderBy==0 + ){ + pWInfo->revMask |= MASKBIT(ii); + } } - return 0; } /* @@ -3954,7 +6649,7 @@ static int whereShortCut(WhereLoopBuilder *pBuilder){ ** ** OUTER JOINS ** -** An outer join of tables t1 and t2 is conceptally coded as follows: +** An outer join of tables t1 and t2 is conceptually coded as follows: ** ** foreach row1 in t1 do ** flag = 0 @@ -3976,8 +6671,8 @@ static int whereShortCut(WhereLoopBuilder *pBuilder){ ** if there is one. If there is no ORDER BY clause or if this routine ** is called from an UPDATE or DELETE statement, then pOrderBy is NULL. ** -** The iIdxCur parameter is the cursor number of an index. If -** WHERE_ONETABLE_ONLY is set, iIdxCur is the cursor number of an index +** The iIdxCur parameter is the cursor number of an index. If +** WHERE_OR_SUBCLAUSE is set, iIdxCur is the cursor number of an index ** to use for OR clause processing. The WHERE clause should use this ** specific cursor. If WHERE_ONEPASS_DESIRED is set, then iIdxCur is ** the first cursor in an array of cursors for all indices. iIdxCur should @@ -3985,13 +6680,15 @@ static int whereShortCut(WhereLoopBuilder *pBuilder){ ** used. */ WhereInfo *sqlite3WhereBegin( - Parse *pParse, /* The parser context */ - SrcList *pTabList, /* FROM clause: A list of all tables to be scanned */ - Expr *pWhere, /* The WHERE clause */ - ExprList *pOrderBy, /* An ORDER BY (or GROUP BY) clause, or NULL */ - ExprList *pResultSet, /* Result set of the query */ - u16 wctrlFlags, /* One of the WHERE_* flags defined in sqliteInt.h */ - int iIdxCur /* If WHERE_ONETABLE_ONLY is set, index cursor number */ + Parse *pParse, /* The parser context */ + SrcList *pTabList, /* FROM clause: A list of all tables to be scanned */ + Expr *pWhere, /* The WHERE clause */ + ExprList *pOrderBy, /* An ORDER BY (or GROUP BY) clause, or NULL */ + ExprList *pResultSet, /* Query result set. Req'd for DISTINCT */ + Select *pSelect, /* The entire SELECT statement */ + u16 wctrlFlags, /* The WHERE_* flags defined in sqliteInt.h */ + int iAuxArg /* If WHERE_OR_SUBCLAUSE is set, index cursor number + ** If WHERE_USE_LIMIT, then the limit amount */ ){ int nByteWInfo; /* Num. bytes allocated for WhereInfo struct */ int nTabList; /* Number of elements in pTabList */ @@ -4008,27 +6705,28 @@ WhereInfo *sqlite3WhereBegin( u8 bFordelete = 0; /* OPFLAG_FORDELETE or zero, as appropriate */ assert( (wctrlFlags & WHERE_ONEPASS_MULTIROW)==0 || ( - (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 - && (wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 + (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 + && (wctrlFlags & WHERE_OR_SUBCLAUSE)==0 )); + /* Only one of WHERE_OR_SUBCLAUSE or WHERE_USE_LIMIT */ + assert( (wctrlFlags & WHERE_OR_SUBCLAUSE)==0 + || (wctrlFlags & WHERE_USE_LIMIT)==0 ); + /* Variable initialization */ db = pParse->db; memset(&sWLB, 0, sizeof(sWLB)); /* An ORDER/GROUP BY clause of more than 63 terms cannot be optimized */ testcase( pOrderBy && pOrderBy->nExpr==BMS-1 ); - if( pOrderBy && pOrderBy->nExpr>=BMS ) pOrderBy = 0; - sWLB.pOrderBy = pOrderBy; - - /* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via - ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */ - if( OptimizationDisabled(db, SQLITE_DistinctOpt) ){ + if( pOrderBy && pOrderBy->nExpr>=BMS ){ + pOrderBy = 0; wctrlFlags &= ~WHERE_WANT_DISTINCT; + wctrlFlags |= WHERE_KEEP_ALL_JOINS; /* Disable omit-noop-join opt */ } /* The number of tables in the FROM clause is limited by the number of - ** bits in a Bitmask + ** bits in a Bitmask */ testcase( pTabList->nSrc==BMS ); if( pTabList->nSrc>BMS ){ @@ -4036,12 +6734,12 @@ WhereInfo *sqlite3WhereBegin( return 0; } - /* This function normally generates a nested loop for all tables in - ** pTabList. But if the WHERE_ONETABLE_ONLY flag is set, then we should + /* This function normally generates a nested loop for all tables in + ** pTabList. But if the WHERE_OR_SUBCLAUSE flag is set, then we should ** only generate code for the first table in pTabList and assume that ** any cursors associated with subsequent tables are uninitialized. */ - nTabList = (wctrlFlags & WHERE_ONETABLE_ONLY) ? 1 : pTabList->nSrc; + nTabList = (wctrlFlags & WHERE_OR_SUBCLAUSE) ? 1 : pTabList->nSrc; /* Allocate and initialize the WhereInfo structure that will become the ** return value. A single allocation is used to store the WhereInfo @@ -4050,24 +6748,36 @@ WhereInfo *sqlite3WhereBegin( ** field (type Bitmask) it must be aligned on an 8-byte boundary on ** some architectures. Hence the ROUND8() below. */ - nByteWInfo = ROUND8(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel)); - pWInfo = sqlite3DbMallocZero(db, nByteWInfo + sizeof(WhereLoop)); + nByteWInfo = SZ_WHEREINFO(nTabList); + pWInfo = sqlite3DbMallocRawNN(db, nByteWInfo + sizeof(WhereLoop)); if( db->mallocFailed ){ sqlite3DbFree(db, pWInfo); pWInfo = 0; goto whereBeginError; } - pWInfo->aiCurOnePass[0] = pWInfo->aiCurOnePass[1] = -1; - pWInfo->nLevel = nTabList; pWInfo->pParse = pParse; pWInfo->pTabList = pTabList; pWInfo->pOrderBy = pOrderBy; +#if WHERETRACE_ENABLED + pWInfo->pWhere = pWhere; +#endif pWInfo->pResultSet = pResultSet; - pWInfo->iBreak = pWInfo->iContinue = sqlite3VdbeMakeLabel(v); + pWInfo->aiCurOnePass[0] = pWInfo->aiCurOnePass[1] = -1; + pWInfo->nLevel = nTabList; + pWInfo->iBreak = pWInfo->iContinue = sqlite3VdbeMakeLabel(pParse); pWInfo->wctrlFlags = wctrlFlags; + pWInfo->iLimit = iAuxArg; pWInfo->savedNQueryLoop = pParse->nQueryLoop; + pWInfo->pSelect = pSelect; + memset(&pWInfo->nOBSat, 0, + offsetof(WhereInfo,sWC) - offsetof(WhereInfo,nOBSat)); + memset(&pWInfo->a[0], 0, sizeof(WhereLoop)+nTabList*sizeof(WhereLevel)); assert( pWInfo->eOnePass==ONEPASS_OFF ); /* ONEPASS defaults to OFF */ pMaskSet = &pWInfo->sMaskSet; + pMaskSet->n = 0; + pMaskSet->ix[0] = -99; /* Initialize ix[0] to a value that can never be + ** a valid cursor number, to avoid an initial + ** test for pMaskSet->n==0 in sqlite3WhereGetMask() */ sWLB.pWInfo = pWInfo; sWLB.pWC = &pWInfo->sWC; sWLB.pNew = (WhereLoop*)(((char*)pWInfo)+nByteWInfo); @@ -4080,61 +6790,112 @@ WhereInfo *sqlite3WhereBegin( /* Split the WHERE clause into separate subexpressions where each ** subexpression is separated by an AND operator. */ - initMaskSet(pMaskSet); sqlite3WhereClauseInit(&pWInfo->sWC, pWInfo); sqlite3WhereSplit(&pWInfo->sWC, pWhere, TK_AND); - - /* Special case: a WHERE clause that is constant. Evaluate the - ** expression and either jump over all of the code or fall thru. - */ - for(ii=0; ii<sWLB.pWC->nTerm; ii++){ - if( nTabList==0 || sqlite3ExprIsConstantNotJoin(sWLB.pWC->a[ii].pExpr) ){ - sqlite3ExprIfFalse(pParse, sWLB.pWC->a[ii].pExpr, pWInfo->iBreak, - SQLITE_JUMPIFNULL); - sWLB.pWC->a[ii].wtFlags |= TERM_CODED; - } - } - + /* Special case: No FROM clause */ if( nTabList==0 ){ if( pOrderBy ) pWInfo->nOBSat = pOrderBy->nExpr; - if( wctrlFlags & WHERE_WANT_DISTINCT ){ + if( (wctrlFlags & WHERE_WANT_DISTINCT)!=0 + && OptimizationEnabled(db, SQLITE_DistinctOpt) + ){ pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; } + if( ALWAYS(pWInfo->pSelect) + && (pWInfo->pSelect->selFlags & SF_MultiValue)==0 + ){ + ExplainQueryPlan((pParse, 0, "SCAN CONSTANT ROW")); + } + }else{ + /* Assign a bit from the bitmask to every term in the FROM clause. + ** + ** The N-th term of the FROM clause is assigned a bitmask of 1<<N. + ** + ** The rule of the previous sentence ensures that if X is the bitmask for + ** a table T, then X-1 is the bitmask for all other tables to the left of T. + ** Knowing the bitmask for all tables to the left of a left join is + ** important. Ticket #3015. + ** + ** Note that bitmasks are created for all pTabList->nSrc tables in + ** pTabList, not just the first nTabList tables. nTabList is normally + ** equal to pTabList->nSrc but might be shortened to 1 if the + ** WHERE_OR_SUBCLAUSE flag is set. + */ + ii = 0; + do{ + createMask(pMaskSet, pTabList->a[ii].iCursor); + sqlite3WhereTabFuncArgs(pParse, &pTabList->a[ii], &pWInfo->sWC); + }while( (++ii)<pTabList->nSrc ); + #ifdef SQLITE_DEBUG + { + Bitmask mx = 0; + for(ii=0; ii<pTabList->nSrc; ii++){ + Bitmask m = sqlite3WhereGetMask(pMaskSet, pTabList->a[ii].iCursor); + assert( m>=mx ); + mx = m; + } + } + #endif + } + + /* Analyze all of the subexpressions. */ + sqlite3WhereExprAnalyze(pTabList, &pWInfo->sWC); + if( pSelect && pSelect->pLimit ){ + sqlite3WhereAddLimit(&pWInfo->sWC, pSelect); } + if( pParse->nErr ) goto whereBeginError; - /* Assign a bit from the bitmask to every term in the FROM clause. + /* The False-WHERE-Term-Bypass optimization: + ** + ** If there are WHERE terms that are false, then no rows will be output, + ** so skip over all of the code generated here. + ** + ** Conditions: ** - ** The N-th term of the FROM clause is assigned a bitmask of 1<<N. + ** (1) The WHERE term must not refer to any tables in the join. + ** (2) The term must not come from an ON clause on the + ** right-hand side of a LEFT or FULL JOIN. + ** (3) The term must not come from an ON clause, or there must be + ** no RIGHT or FULL OUTER joins in pTabList. + ** (4) If the expression contains non-deterministic functions + ** that are not within a sub-select. This is not required + ** for correctness but rather to preserves SQLite's legacy + ** behaviour in the following two cases: ** - ** The rule of the previous sentence ensures thta if X is the bitmask for - ** a table T, then X-1 is the bitmask for all other tables to the left of T. - ** Knowing the bitmask for all tables to the left of a left join is - ** important. Ticket #3015. + ** WHERE random()>0; -- eval random() once per row + ** WHERE (SELECT random())>0; -- eval random() just once overall ** - ** Note that bitmasks are created for all pTabList->nSrc tables in - ** pTabList, not just the first nTabList tables. nTabList is normally - ** equal to pTabList->nSrc but might be shortened to 1 if the - ** WHERE_ONETABLE_ONLY flag is set. + ** Note that the Where term need not be a constant in order for this + ** optimization to apply, though it does need to be constant relative to + ** the current subquery (condition 1). The term might include variables + ** from outer queries so that the value of the term changes from one + ** invocation of the current subquery to the next. */ - for(ii=0; ii<pTabList->nSrc; ii++){ - createMask(pMaskSet, pTabList->a[ii].iCursor); - sqlite3WhereTabFuncArgs(pParse, &pTabList->a[ii], &pWInfo->sWC); - } -#ifdef SQLITE_DEBUG - for(ii=0; ii<pTabList->nSrc; ii++){ - Bitmask m = sqlite3WhereGetMask(pMaskSet, pTabList->a[ii].iCursor); - assert( m==MASKBIT(ii) ); + for(ii=0; ii<sWLB.pWC->nBase; ii++){ + WhereTerm *pT = &sWLB.pWC->a[ii]; /* A term of the WHERE clause */ + Expr *pX; /* The expression of pT */ + if( pT->wtFlags & TERM_VIRTUAL ) continue; + pX = pT->pExpr; + assert( pX!=0 ); + assert( pT->prereqAll!=0 || !ExprHasProperty(pX, EP_OuterON) ); + if( pT->prereqAll==0 /* Conditions (1) and (2) */ + && (nTabList==0 || exprIsDeterministic(pX)) /* Condition (4) */ + && !(ExprHasProperty(pX, EP_InnerON) /* Condition (3) */ + && (pTabList->a[0].fg.jointype & JT_LTORJ)!=0 ) + ){ + sqlite3ExprIfFalse(pParse, pX, pWInfo->iBreak, SQLITE_JUMPIFNULL); + pT->wtFlags |= TERM_CODED; + } } -#endif - - /* Analyze all of the subexpressions. */ - sqlite3WhereExprAnalyze(pTabList, &pWInfo->sWC); - if( db->mallocFailed ) goto whereBeginError; if( wctrlFlags & WHERE_WANT_DISTINCT ){ - if( isDistinctRedundant(pParse, pTabList, &pWInfo->sWC, pResultSet) ){ + if( OptimizationDisabled(db, SQLITE_DistinctOpt) ){ + /* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via + ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */ + wctrlFlags &= ~WHERE_WANT_DISTINCT; + pWInfo->wctrlFlags &= ~WHERE_WANT_DISTINCT; + }else if( isDistinctRedundant(pParse, pTabList, &pWInfo->sWC, pResultSet) ){ /* The DISTINCT marking is pointless. Ignore it. */ pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; }else if( pOrderBy==0 ){ @@ -4145,13 +6906,26 @@ WhereInfo *sqlite3WhereBegin( } /* Construct the WhereLoop objects */ - WHERETRACE(0xffff,("*** Optimizer Start *** (wctrlFlags: 0x%x)\n", - wctrlFlags)); #if defined(WHERETRACE_ENABLED) - if( sqlite3WhereTrace & 0x100 ){ /* Display all terms of the WHERE clause */ - int i; - for(i=0; i<sWLB.pWC->nTerm; i++){ - whereTermPrint(&sWLB.pWC->a[i], i); + if( sqlite3WhereTrace & 0xffffffff ){ + sqlite3DebugPrintf("*** Optimizer Start *** (wctrlFlags: 0x%x",wctrlFlags); + if( wctrlFlags & WHERE_USE_LIMIT ){ + sqlite3DebugPrintf(", limit: %d", iAuxArg); + } + sqlite3DebugPrintf(")\n"); + if( sqlite3WhereTrace & 0x8000 ){ + Select sSelect; + memset(&sSelect, 0, sizeof(sSelect)); + sSelect.selFlags = SF_WhereBegin; + sSelect.pSrc = pTabList; + sSelect.pWhere = pWhere; + sSelect.pOrderBy = pOrderBy; + sSelect.pEList = pResultSet; + sqlite3TreeViewSelect(0, &sSelect, 0); + } + if( sqlite3WhereTrace & 0x4000 ){ /* Display all WHERE clause terms */ + sqlite3DebugPrintf("---- WHERE clause at start of analysis:\n"); + sqlite3WhereClausePrint(sWLB.pWC); } } #endif @@ -4159,36 +6933,60 @@ WhereInfo *sqlite3WhereBegin( if( nTabList!=1 || whereShortCut(&sWLB)==0 ){ rc = whereLoopAddAll(&sWLB); if( rc ) goto whereBeginError; - -#ifdef WHERETRACE_ENABLED - if( sqlite3WhereTrace ){ /* Display all of the WhereLoop objects */ - WhereLoop *p; - int i; - static const char zLabel[] = "0123456789abcdefghijklmnopqrstuvwyxz" - "ABCDEFGHIJKLMNOPQRSTUVWYXZ"; - for(p=pWInfo->pLoops, i=0; p; p=p->pNextLoop, i++){ - p->cId = zLabel[i%sizeof(zLabel)]; - whereLoopPrint(p, sWLB.pWC); + +#ifdef SQLITE_ENABLE_STAT4 + /* If one or more WhereTerm.truthProb values were used in estimating + ** loop parameters, but then those truthProb values were subsequently + ** changed based on STAT4 information while computing subsequent loops, + ** then we need to rerun the whole loop building process so that all + ** loops will be built using the revised truthProb values. */ + if( sWLB.bldFlags2 & SQLITE_BLDF2_2NDPASS ){ + WHERETRACE_ALL_LOOPS(pWInfo, sWLB.pWC); + WHERETRACE(0xffffffff, + ("**** Redo all loop computations due to" + " TERM_HIGHTRUTH changes ****\n")); + while( pWInfo->pLoops ){ + WhereLoop *p = pWInfo->pLoops; + pWInfo->pLoops = p->pNextLoop; + whereLoopDelete(db, p); } + rc = whereLoopAddAll(&sWLB); + if( rc ) goto whereBeginError; } #endif - + WHERETRACE_ALL_LOOPS(pWInfo, sWLB.pWC); + wherePathSolver(pWInfo, 0); if( db->mallocFailed ) goto whereBeginError; if( pWInfo->pOrderBy ){ - wherePathSolver(pWInfo, pWInfo->nRowOut+1); + whereInterstageHeuristic(pWInfo); + wherePathSolver(pWInfo, pWInfo->nRowOut<0 ? 1 : pWInfo->nRowOut+1); if( db->mallocFailed ) goto whereBeginError; } + + /* TUNING: Assume that a DISTINCT clause on a subquery reduces + ** the output size by a factor of 8 (LogEst -30). Search for + ** tag-20250414a to see other cases. + */ + if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT)!=0 ){ + WHERETRACE(0x0080,("nRowOut reduced from %d to %d due to DISTINCT\n", + pWInfo->nRowOut, pWInfo->nRowOut-30)); + pWInfo->nRowOut -= 30; + } + } + assert( pWInfo->pTabList!=0 ); if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder)!=0 ){ - pWInfo->revMask = (Bitmask)(-1); + whereReverseScanOrder(pWInfo); } - if( pParse->nErr || NEVER(db->mallocFailed) ){ + if( pParse->nErr ){ goto whereBeginError; } + assert( db->mallocFailed==0 ); #ifdef WHERETRACE_ENABLED if( sqlite3WhereTrace ){ - sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut); + sqlite3DebugPrintf("---- Solution cost=%d, nRow=%d", + pWInfo->rTotalCost, pWInfo->nRowOut); if( pWInfo->nOBSat>0 ){ sqlite3DebugPrintf(" ORDERBY=%d,0x%llx", pWInfo->nOBSat, pWInfo->revMask); } @@ -4208,59 +7006,82 @@ WhereInfo *sqlite3WhereBegin( } sqlite3DebugPrintf("\n"); for(ii=0; ii<pWInfo->nLevel; ii++){ - whereLoopPrint(pWInfo->a[ii].pWLoop, sWLB.pWC); + sqlite3WhereLoopPrint(pWInfo->a[ii].pWLoop, sWLB.pWC); } } #endif - /* Attempt to omit tables from the join that do not effect the result */ + + /* Attempt to omit tables from a join that do not affect the result. + ** See the comment on whereOmitNoopJoin() for further information. + ** + ** This query optimization is factored out into a separate "no-inline" + ** procedure to keep the sqlite3WhereBegin() procedure from becoming + ** too large. If sqlite3WhereBegin() becomes too large, that prevents + ** some C-compiler optimizers from in-lining the + ** sqlite3WhereCodeOneLoopStart() procedure, and it is important to + ** in-line sqlite3WhereCodeOneLoopStart() for performance reasons. + */ + notReady = ~(Bitmask)0; + if( pWInfo->nLevel>=2 /* Must be a join, or this opt8n is pointless */ + && pResultSet!=0 /* Condition (1) */ + && 0==(wctrlFlags & (WHERE_AGG_DISTINCT|WHERE_KEEP_ALL_JOINS)) /* (1),(6) */ + && OptimizationEnabled(db, SQLITE_OmitNoopJoin) /* (7) */ + ){ + notReady = whereOmitNoopJoin(pWInfo, notReady); + nTabList = pWInfo->nLevel; + assert( nTabList>0 ); + } + + /* Check to see if there are any SEARCH loops that might benefit from + ** using a Bloom filter. + */ if( pWInfo->nLevel>=2 - && pResultSet!=0 - && OptimizationEnabled(db, SQLITE_OmitNoopJoin) + && OptimizationEnabled(db, SQLITE_BloomFilter) ){ - Bitmask tabUsed = sqlite3WhereExprListUsage(pMaskSet, pResultSet); - if( sWLB.pOrderBy ){ - tabUsed |= sqlite3WhereExprListUsage(pMaskSet, sWLB.pOrderBy); - } - while( pWInfo->nLevel>=2 ){ - WhereTerm *pTerm, *pEnd; - pLoop = pWInfo->a[pWInfo->nLevel-1].pWLoop; - if( (pWInfo->pTabList->a[pLoop->iTab].fg.jointype & JT_LEFT)==0 ) break; - if( (wctrlFlags & WHERE_WANT_DISTINCT)==0 - && (pLoop->wsFlags & WHERE_ONEROW)==0 - ){ - break; - } - if( (tabUsed & pLoop->maskSelf)!=0 ) break; - pEnd = sWLB.pWC->a + sWLB.pWC->nTerm; - for(pTerm=sWLB.pWC->a; pTerm<pEnd; pTerm++){ - if( (pTerm->prereqAll & pLoop->maskSelf)!=0 - && !ExprHasProperty(pTerm->pExpr, EP_FromJoin) - ){ - break; - } - } - if( pTerm<pEnd ) break; - WHERETRACE(0xffff, ("-> drop loop %c not used\n", pLoop->cId)); - pWInfo->nLevel--; - nTabList--; - } + whereCheckIfBloomFilterIsUseful(pWInfo); + } + +#if defined(WHERETRACE_ENABLED) + if( sqlite3WhereTrace & 0x4000 ){ /* Display all terms of the WHERE clause */ + sqlite3DebugPrintf("---- WHERE clause at end of analysis:\n"); + sqlite3WhereClausePrint(sWLB.pWC); } - WHERETRACE(0xffff,("*** Optimizer Finished ***\n")); + WHERETRACE(0xffffffff,("*** Optimizer Finished ***\n")); +#endif pWInfo->pParse->nQueryLoop += pWInfo->nRowOut; /* If the caller is an UPDATE or DELETE statement that is requesting ** to use a one-pass algorithm, determine if this is appropriate. + ** + ** A one-pass approach can be used if the caller has requested one + ** and either (a) the scan visits at most one row or (b) each + ** of the following are true: + ** + ** * the caller has indicated that a one-pass approach can be used + ** with multiple rows (by setting WHERE_ONEPASS_MULTIROW), and + ** * the table is not a virtual table, and + ** * either the scan does not use the OR optimization or the caller + ** is a DELETE operation (WHERE_DUPLICATES_OK is only specified + ** for DELETE). + ** + ** The last qualification is because an UPDATE statement uses + ** WhereInfo.aiCurOnePass[1] to determine whether or not it really can + ** use a one-pass approach, and this is not set accurately for scans + ** that use the OR optimization. */ assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 ); if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 ){ int wsFlags = pWInfo->a[0].pWLoop->wsFlags; int bOnerow = (wsFlags & WHERE_ONEROW)!=0; - if( bOnerow - || ((wctrlFlags & WHERE_ONEPASS_MULTIROW)!=0 - && 0==(wsFlags & WHERE_VIRTUALTABLE)) - ){ + assert( !(wsFlags&WHERE_VIRTUALTABLE) || IsVirtual(pTabList->a[0].pSTab) ); + if( bOnerow || ( + 0!=(wctrlFlags & WHERE_ONEPASS_MULTIROW) + && !IsVirtual(pTabList->a[0].pSTab) + && (0==(wsFlags & WHERE_MULTI_OR) || (wctrlFlags & WHERE_DUPLICATES_OK)) + && OptimizationEnabled(db, SQLITE_OnePass) + )){ pWInfo->eOnePass = bOnerow ? ONEPASS_SINGLE : ONEPASS_MULTI; - if( HasRowid(pTabList->a[0].pTab) && (wsFlags & WHERE_IDX_ONLY) ){ + if( HasRowid(pTabList->a[0].pSTab) && (wsFlags & WHERE_IDX_ONLY) ){ if( wctrlFlags & WHERE_ONEPASS_MULTIROW ){ bFordelete = OPFLAG_FORDELETE; } @@ -4275,13 +7096,21 @@ WhereInfo *sqlite3WhereBegin( for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){ Table *pTab; /* Table to open */ int iDb; /* Index of database containing table/index */ - struct SrcList_item *pTabItem; + SrcItem *pTabItem; pTabItem = &pTabList->a[pLevel->iFrom]; - pTab = pTabItem->pTab; + pTab = pTabItem->pSTab; iDb = sqlite3SchemaToIndex(db, pTab->pSchema); pLoop = pLevel->pWLoop; - if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ){ + pLevel->addrBrk = sqlite3VdbeMakeLabel(pParse); + if( ii==0 || (pTabItem[0].fg.jointype & JT_LEFT)!=0 ){ + pLevel->addrHalt = pLevel->addrBrk; + }else if( pWInfo->a[ii-1].pRJ ){ + pLevel->addrHalt = pWInfo->a[ii-1].addrBrk; + }else{ + pLevel->addrHalt = pWInfo->a[ii-1].addrHalt; + } + if( (pTab->tabFlags & TF_Ephemeral)!=0 || IsView(pTab) ){ /* Do nothing */ }else #ifndef SQLITE_OMIT_VIRTUALTABLE @@ -4293,8 +7122,10 @@ WhereInfo *sqlite3WhereBegin( /* noop */ }else #endif - if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 - && (wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 ){ + if( ((pLoop->wsFlags & WHERE_IDX_ONLY)==0 + && (wctrlFlags & WHERE_OR_SUBCLAUSE)==0) + || (pTabItem->fg.jointype & (JT_LTORJ|JT_RIGHT))!=0 + ){ int op = OP_OpenRead; if( pWInfo->eOnePass!=ONEPASS_OFF ){ op = OP_OpenWrite; @@ -4304,7 +7135,14 @@ WhereInfo *sqlite3WhereBegin( assert( pTabItem->iCursor==pLevel->iTabCur ); testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS-1 ); testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS ); - if( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol<BMS && HasRowid(pTab) ){ + if( pWInfo->eOnePass==ONEPASS_OFF + && pTab->nCol<BMS + && (pTab->tabFlags & (TF_HasGenerated|TF_WithoutRowid))==0 + && (pLoop->wsFlags & (WHERE_AUTO_INDEX|WHERE_BLOOMFILTER))==0 + ){ + /* If we know that only a prefix of the record will be used, + ** it is advantageous to reduce the "column count" field in + ** the P4 operand of the OP_OpenRead/Write opcode. */ Bitmask b = pTabItem->colUsed; int n = 0; for(; b; b=b>>1, n++){} @@ -4312,7 +7150,7 @@ WhereInfo *sqlite3WhereBegin( assert( n<=pTab->nCol ); } #ifdef SQLITE_ENABLE_CURSOR_HINTS - if( pLoop->u.btree.pIndex!=0 ){ + if( pLoop->u.btree.pIndex!=0 && (pTab->tabFlags & TF_WithoutRowid)==0 ){ sqlite3VdbeChangeP5(v, OPFLAG_SEEKEQ|bFordelete); }else #endif @@ -4323,6 +7161,13 @@ WhereInfo *sqlite3WhereBegin( sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, pTabItem->iCursor, 0, 0, (const u8*)&pTabItem->colUsed, P4_INT64); #endif + if( ii>=2 + && (pTabItem[0].fg.jointype & (JT_LTORJ|JT_LEFT))==0 + && pLevel->addrHalt==pWInfo->a[0].addrHalt + ){ + sqlite3VdbeAddOp2(v, OP_IfEmpty, pTabItem->iCursor, pWInfo->iBreak); + VdbeCoverage(v); + } }else{ sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); } @@ -4330,18 +7175,18 @@ WhereInfo *sqlite3WhereBegin( Index *pIx = pLoop->u.btree.pIndex; int iIndexCur; int op = OP_OpenRead; - /* iIdxCur is always set if to a positive value if ONEPASS is possible */ - assert( iIdxCur!=0 || (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 ); + /* iAuxArg is always set to a positive value if ONEPASS is possible */ + assert( iAuxArg!=0 || (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 ); if( !HasRowid(pTab) && IsPrimaryKeyIndex(pIx) - && (wctrlFlags & WHERE_ONETABLE_ONLY)!=0 + && (wctrlFlags & WHERE_OR_SUBCLAUSE)!=0 ){ /* This is one term of an OR-optimization using the PRIMARY KEY of a ** WITHOUT ROWID table. No need for a separate index */ iIndexCur = pLevel->iTabCur; op = 0; }else if( pWInfo->eOnePass!=ONEPASS_OFF ){ - Index *pJ = pTabItem->pTab->pIndex; - iIndexCur = iIdxCur; + Index *pJ = pTabItem->pSTab->pIndex; + iIndexCur = iAuxArg; assert( wctrlFlags & WHERE_ONEPASS_DESIRED ); while( ALWAYS(pJ) && pJ!=pIx ){ iIndexCur++; @@ -4349,13 +7194,22 @@ WhereInfo *sqlite3WhereBegin( } op = OP_OpenWrite; pWInfo->aiCurOnePass[1] = iIndexCur; - }else if( iIdxCur && (wctrlFlags & WHERE_ONETABLE_ONLY)!=0 ){ - iIndexCur = iIdxCur; - if( wctrlFlags & WHERE_REOPEN_IDX ) op = OP_ReopenIdx; + }else if( iAuxArg && (wctrlFlags & WHERE_OR_SUBCLAUSE)!=0 ){ + iIndexCur = iAuxArg; + op = OP_ReopenIdx; }else{ iIndexCur = pParse->nTab++; + if( pIx->bHasExpr && OptimizationEnabled(db, SQLITE_IndexedExpr) ){ + whereAddIndexedExpr(pParse, pIx, iIndexCur, pTabItem); + } + if( pIx->pPartIdxWhere && (pTabItem->fg.jointype & JT_RIGHT)==0 ){ + wherePartIdxExpr( + pParse, pIx, pIx->pPartIdxWhere, 0, iIndexCur, pTabItem + ); + } } pLevel->iIdxCur = iIndexCur; + assert( pIx!=0 ); assert( pIx->pSchema==pTab->pSchema ); assert( iIndexCur>=0 ); if( op ){ @@ -4363,9 +7217,12 @@ WhereInfo *sqlite3WhereBegin( sqlite3VdbeSetP4KeyInfo(pParse, pIx); if( (pLoop->wsFlags & WHERE_CONSTRAINT)!=0 && (pLoop->wsFlags & (WHERE_COLUMN_RANGE|WHERE_SKIPSCAN))==0 + && (pLoop->wsFlags & WHERE_BIGNULL_SORT)==0 + && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)==0 && (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0 + && pWInfo->eDistinct!=WHERE_DISTINCT_ORDERED ){ - sqlite3VdbeChangeP5(v, OPFLAG_SEEKEQ); /* Hint to COMDB2 */ + sqlite3VdbeChangeP5(v, OPFLAG_SEEKEQ); } VdbeComment((v, "%s", pIx->zName)); #ifdef SQLITE_ENABLE_COLUMN_USED_MASK @@ -4386,6 +7243,37 @@ WhereInfo *sqlite3WhereBegin( } } if( iDb>=0 ) sqlite3CodeVerifySchema(pParse, iDb); + if( (pTabItem->fg.jointype & JT_RIGHT)!=0 + && (pLevel->pRJ = sqlite3WhereMalloc(pWInfo, sizeof(WhereRightJoin)))!=0 + ){ + WhereRightJoin *pRJ = pLevel->pRJ; + pRJ->iMatch = pParse->nTab++; + pRJ->regBloom = ++pParse->nMem; + sqlite3VdbeAddOp2(v, OP_Blob, 65536, pRJ->regBloom); + pRJ->regReturn = ++pParse->nMem; + sqlite3VdbeAddOp2(v, OP_Null, 0, pRJ->regReturn); + assert( pTab==pTabItem->pSTab ); + if( HasRowid(pTab) ){ + KeyInfo *pInfo; + sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pRJ->iMatch, 1); + pInfo = sqlite3KeyInfoAlloc(pParse->db, 1, 0); + if( pInfo ){ + pInfo->aColl[0] = 0; + pInfo->aSortFlags[0] = 0; + sqlite3VdbeAppendP4(v, pInfo, P4_KEYINFO); + } + }else{ + Index *pPk = sqlite3PrimaryKeyIndex(pTab); + sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pRJ->iMatch, pPk->nKeyCol); + sqlite3VdbeSetP4KeyInfo(pParse, pPk); + } + pLoop->wsFlags &= ~WHERE_IDX_ONLY; + /* The nature of RIGHT JOIN processing is such that it messes up + ** the output order. So omit any ORDER BY/GROUP BY elimination + ** optimizations. We need to do an actual sort for RIGHT JOIN. */ + pWInfo->nOBSat = 0; + pWInfo->eDistinct = WHERE_DISTINCT_UNORDERED; + } } pWInfo->iTop = sqlite3VdbeCurrentAddr(v); if( db->mallocFailed ) goto whereBeginError; @@ -4394,32 +7282,53 @@ WhereInfo *sqlite3WhereBegin( ** loop below generates code for a single nested loop of the VM ** program. */ - notReady = ~(Bitmask)0; for(ii=0; ii<nTabList; ii++){ int addrExplain; int wsFlags; + SrcItem *pSrc; + if( pParse->nErr ) goto whereBeginError; pLevel = &pWInfo->a[ii]; wsFlags = pLevel->pWLoop->wsFlags; + pSrc = &pTabList->a[pLevel->iFrom]; + if( pSrc->fg.isMaterialized ){ + Subquery *pSubq; + int iOnce = 0; + assert( pSrc->fg.isSubquery ); + pSubq = pSrc->u4.pSubq; + if( pSrc->fg.isCorrelated==0 ){ + iOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v); + }else{ + iOnce = 0; + } + sqlite3VdbeAddOp2(v, OP_Gosub, pSubq->regReturn, pSubq->addrFillSub); + VdbeComment((v, "materialize %!S", pSrc)); + if( iOnce ) sqlite3VdbeJumpHere(v, iOnce); + } + assert( pTabList == pWInfo->pTabList ); + if( (wsFlags & (WHERE_AUTO_INDEX|WHERE_BLOOMFILTER))!=0 ){ + if( (wsFlags & WHERE_AUTO_INDEX)!=0 ){ #ifndef SQLITE_OMIT_AUTOMATIC_INDEX - if( (pLevel->pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 ){ - constructAutomaticIndex(pParse, &pWInfo->sWC, - &pTabList->a[pLevel->iFrom], notReady, pLevel); + constructAutomaticIndex(pParse, &pWInfo->sWC, notReady, pLevel); +#endif + }else{ + sqlite3ConstructBloomFilter(pWInfo, ii, pLevel, notReady); + } if( db->mallocFailed ) goto whereBeginError; } -#endif addrExplain = sqlite3WhereExplainOneScan( - pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags + pParse, pTabList, pLevel, wctrlFlags ); pLevel->addrBody = sqlite3VdbeCurrentAddr(v); - notReady = sqlite3WhereCodeOneLoopStart(pWInfo, ii, notReady); + notReady = sqlite3WhereCodeOneLoopStart(pParse,v,pWInfo,ii,pLevel,notReady); pWInfo->iContinue = pLevel->addrCont; - if( (wsFlags&WHERE_MULTI_OR)==0 && (wctrlFlags&WHERE_ONETABLE_ONLY)==0 ){ + if( (wsFlags&WHERE_MULTI_OR)==0 && (wctrlFlags&WHERE_OR_SUBCLAUSE)==0 ){ sqlite3WhereAddScanStatus(v, pTabList, pLevel, addrExplain); } } /* Done. */ VdbeModuleComment((v, "Begin WHERE-core")); + pWInfo->iEndWhere = sqlite3VdbeCurrentAddr(v); return pWInfo; /* Jump here if malloc fails */ @@ -4428,11 +7337,37 @@ WhereInfo *sqlite3WhereBegin( pParse->nQueryLoop = pWInfo->savedNQueryLoop; whereInfoFree(db, pWInfo); } +#ifdef WHERETRACE_ENABLED + /* Prevent harmless compiler warnings about debugging routines + ** being declared but never used */ + sqlite3ShowWhereLoopList(0); +#endif /* WHERETRACE_ENABLED */ return 0; } /* -** Generate the end of the WHERE loop. See comments on +** Part of sqlite3WhereEnd() will rewrite opcodes to reference the +** index rather than the main table. In SQLITE_DEBUG mode, we want +** to trace those changes if PRAGMA vdbe_addoptrace=on. This routine +** does that. +*/ +#ifndef SQLITE_DEBUG +# define OpcodeRewriteTrace(D,K,P) /* no-op */ +#else +# define OpcodeRewriteTrace(D,K,P) sqlite3WhereOpcodeRewriteTrace(D,K,P) + static void sqlite3WhereOpcodeRewriteTrace( + sqlite3 *db, + int pc, + VdbeOp *pOp + ){ + if( (db->flags & SQLITE_VdbeAddopTrace)==0 ) return; + sqlite3VdbePrintOp(0, pc, pOp); + sqlite3ShowWhereTerm(0); /* So compiler won't complain about unused func */ + } +#endif + +/* +** Generate the end of the WHERE loop. See comments on ** sqlite3WhereBegin() for additional information. */ void sqlite3WhereEnd(WhereInfo *pWInfo){ @@ -4443,15 +7378,75 @@ void sqlite3WhereEnd(WhereInfo *pWInfo){ WhereLoop *pLoop; SrcList *pTabList = pWInfo->pTabList; sqlite3 *db = pParse->db; + int iEnd = sqlite3VdbeCurrentAddr(v); + int nRJ = 0; +#ifndef SQLITE_DISABLE_SKIPAHEAD_DISTINCT + int addrSeek = 0; +#endif /* Generate loop termination code. */ VdbeModuleComment((v, "End WHERE-core")); - sqlite3ExprCacheClear(pParse); for(i=pWInfo->nLevel-1; i>=0; i--){ int addr; pLevel = &pWInfo->a[i]; + if( pLevel->pRJ ){ + /* Terminate the subroutine that forms the interior of the loop of + ** the RIGHT JOIN table */ + WhereRightJoin *pRJ = pLevel->pRJ; + sqlite3VdbeResolveLabel(v, pLevel->addrCont); + /* Replace addrCont with a new label that will never be used, just so + ** the subsequent call to resolve pLevel->addrCont will have something + ** to resolve. */ + pLevel->addrCont = sqlite3VdbeMakeLabel(pParse); + pRJ->endSubrtn = sqlite3VdbeCurrentAddr(v); + sqlite3VdbeAddOp3(v, OP_Return, pRJ->regReturn, pRJ->addrSubrtn, 1); + VdbeCoverage(v); + nRJ++; + } pLoop = pLevel->pWLoop; + if( pLevel->op!=OP_Noop ){ +#ifndef SQLITE_DISABLE_SKIPAHEAD_DISTINCT + Index *pIdx; + int n; + if( pWInfo->eDistinct==WHERE_DISTINCT_ORDERED + && i==pWInfo->nLevel-1 /* Ticket [ef9318757b152e3] 2017-10-21 */ + && (pLoop->wsFlags & WHERE_INDEXED)!=0 + && (pIdx = pLoop->u.btree.pIndex)->hasStat1 + && (n = pLoop->u.btree.nDistinctCol)>0 + && pIdx->aiRowLogEst[n]>=36 + ){ + int r1 = pParse->nMem+1; + int j, op; + for(j=0; j<n; j++){ + sqlite3VdbeAddOp3(v, OP_Column, pLevel->iIdxCur, j, r1+j); + } + pParse->nMem += n+1; + op = pLevel->op==OP_Prev ? OP_SeekLT : OP_SeekGT; + addrSeek = sqlite3VdbeAddOp4Int(v, op, pLevel->iIdxCur, 0, r1, n); + VdbeCoverageIf(v, op==OP_SeekLT); + VdbeCoverageIf(v, op==OP_SeekGT); + sqlite3VdbeAddOp2(v, OP_Goto, 1, pLevel->p2); + } +#endif /* SQLITE_DISABLE_SKIPAHEAD_DISTINCT */ + } + if( pTabList->a[pLevel->iFrom].fg.fromExists && i==pWInfo->nLevel-1 ){ + /* If the EXISTS-to-JOIN optimization was applied, then the EXISTS + ** loop(s) will be the inner-most loops of the join. There might be + ** multiple EXISTS loops, but they will all be nested, and the join + ** order will not have been changed by the query planner. If the + ** inner-most EXISTS loop sees a single successful row, it should + ** break out of *all* EXISTS loops. But only the inner-most of the + ** nested EXISTS loops should do this breakout. */ + int nOuter = 0; /* Nr of outer EXISTS that this one is nested within */ + while( nOuter<i ){ + if( !pTabList->a[pLevel[-nOuter-1].iFrom].fg.fromExists ) break; + nOuter++; + } + testcase( nOuter>0 ); + sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel[-nOuter].addrBrk); + VdbeComment((v, "EXISTS break")); + } sqlite3VdbeResolveLabel(v, pLevel->addrCont); if( pLevel->op!=OP_Noop ){ sqlite3VdbeAddOp3(v, pLevel->op, pLevel->p1, pLevel->p2, pLevel->p3); @@ -4460,21 +7455,69 @@ void sqlite3WhereEnd(WhereInfo *pWInfo){ VdbeCoverageIf(v, pLevel->op==OP_Next); VdbeCoverageIf(v, pLevel->op==OP_Prev); VdbeCoverageIf(v, pLevel->op==OP_VNext); + if( pLevel->regBignull ){ + sqlite3VdbeResolveLabel(v, pLevel->addrBignull); + sqlite3VdbeAddOp2(v, OP_DecrJumpZero, pLevel->regBignull, pLevel->p2-1); + VdbeCoverage(v); + } +#ifndef SQLITE_DISABLE_SKIPAHEAD_DISTINCT + if( addrSeek ){ + sqlite3VdbeJumpHere(v, addrSeek); + addrSeek = 0; + } +#endif } - if( pLoop->wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){ + if( (pLoop->wsFlags & WHERE_IN_ABLE)!=0 && pLevel->u.in.nIn>0 ){ struct InLoop *pIn; int j; sqlite3VdbeResolveLabel(v, pLevel->addrNxt); for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){ + assert( sqlite3VdbeGetOp(v, pIn->addrInTop+1)->opcode==OP_IsNull + || pParse->db->mallocFailed ); sqlite3VdbeJumpHere(v, pIn->addrInTop+1); - sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop); - VdbeCoverage(v); - VdbeCoverageIf(v, pIn->eEndLoopOp==OP_PrevIfOpen); - VdbeCoverageIf(v, pIn->eEndLoopOp==OP_NextIfOpen); + if( pIn->eEndLoopOp!=OP_Noop ){ + if( pIn->nPrefix ){ + int bEarlyOut = + (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 + && (pLoop->wsFlags & WHERE_IN_EARLYOUT)!=0; + if( pLevel->iLeftJoin ){ + /* For LEFT JOIN queries, cursor pIn->iCur may not have been + ** opened yet. This occurs for WHERE clauses such as + ** "a = ? AND b IN (...)", where the index is on (a, b). If + ** the RHS of the (a=?) is NULL, then the "b IN (...)" may + ** never have been coded, but the body of the loop run to + ** return the null-row. So, if the cursor is not open yet, + ** jump over the OP_Next or OP_Prev instruction about to + ** be coded. */ + sqlite3VdbeAddOp2(v, OP_IfNotOpen, pIn->iCur, + sqlite3VdbeCurrentAddr(v) + 2 + bEarlyOut); + VdbeCoverage(v); + } + if( bEarlyOut ){ + sqlite3VdbeAddOp4Int(v, OP_IfNoHope, pLevel->iIdxCur, + sqlite3VdbeCurrentAddr(v)+2, + pIn->iBase, pIn->nPrefix); + VdbeCoverage(v); + /* Retarget the OP_IsNull against the left operand of IN so + ** it jumps past the OP_IfNoHope. This is because the + ** OP_IsNull also bypasses the OP_Affinity opcode that is + ** required by OP_IfNoHope. */ + sqlite3VdbeJumpHere(v, pIn->addrInTop+1); + } + } + sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop); + VdbeCoverage(v); + VdbeCoverageIf(v, pIn->eEndLoopOp==OP_Prev); + VdbeCoverageIf(v, pIn->eEndLoopOp==OP_Next); + } sqlite3VdbeJumpHere(v, pIn->addrInTop-1); } } sqlite3VdbeResolveLabel(v, pLevel->addrBrk); + if( pLevel->pRJ ){ + sqlite3VdbeAddOp3(v, OP_Return, pLevel->pRJ->regReturn, 0, 1); + VdbeCoverage(v); + } if( pLevel->addrSkip ){ sqlite3VdbeGoto(v, pLevel->addrSkip); VdbeComment((v, "next skip-scan on %s", pLoop->u.btree.pIndex->zName)); @@ -4483,24 +7526,37 @@ void sqlite3WhereEnd(WhereInfo *pWInfo){ } #ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS if( pLevel->addrLikeRep ){ - int op; - if( sqlite3VdbeGetOp(v, pLevel->addrLikeRep-1)->p1 ){ - op = OP_DecrJumpZero; - }else{ - op = OP_JumpZeroIncr; - } - sqlite3VdbeAddOp2(v, op, pLevel->iLikeRepCntr, pLevel->addrLikeRep); + sqlite3VdbeAddOp2(v, OP_DecrJumpZero, (int)(pLevel->iLikeRepCntr>>1), + pLevel->addrLikeRep); VdbeCoverage(v); } #endif if( pLevel->iLeftJoin ){ + int ws = pLoop->wsFlags; addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin); VdbeCoverage(v); - assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 - || (pLoop->wsFlags & WHERE_INDEXED)!=0 ); - if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 ){ - sqlite3VdbeAddOp1(v, OP_NullRow, pTabList->a[i].iCursor); + assert( (ws & WHERE_IDX_ONLY)==0 || (ws & WHERE_INDEXED)!=0 ); + if( (ws & WHERE_IDX_ONLY)==0 ){ + SrcItem *pSrc = &pTabList->a[pLevel->iFrom]; + assert( pLevel->iTabCur==pSrc->iCursor ); + if( pSrc->fg.viaCoroutine ){ + int m, n; + assert( pSrc->fg.isSubquery ); + n = pSrc->u4.pSubq->regResult; + assert( pSrc->pSTab!=0 ); + m = pSrc->pSTab->nCol; + sqlite3VdbeAddOp3(v, OP_Null, 0, n, n+m-1); + } + sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iTabCur); } - if( pLoop->wsFlags & WHERE_INDEXED ){ + if( (ws & WHERE_INDEXED) + || ((ws & WHERE_MULTI_OR) && pLevel->u.pCoveringIdx) + ){ + if( ws & WHERE_MULTI_OR ){ + Index *pIx = pLevel->u.pCoveringIdx; + int iDb = sqlite3SchemaToIndex(db, pIx->pSchema); + sqlite3VdbeAddOp3(v, OP_ReopenIdx, pLevel->iIdxCur, pIx->tnum, iDb); + sqlite3VdbeSetP4KeyInfo(pParse, pIx); + } sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur); } if( pLevel->op==OP_Return ){ @@ -4511,60 +7567,46 @@ void sqlite3WhereEnd(WhereInfo *pWInfo){ sqlite3VdbeJumpHere(v, addr); } VdbeModuleComment((v, "End WHERE-loop%d: %s", i, - pWInfo->pTabList->a[pLevel->iFrom].pTab->zName)); + pWInfo->pTabList->a[pLevel->iFrom].pSTab->zName)); } - /* The "break" point is here, just past the end of the outer loop. - ** Set it. - */ - sqlite3VdbeResolveLabel(v, pWInfo->iBreak); - assert( pWInfo->nLevel<=pTabList->nSrc ); for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){ int k, last; - VdbeOp *pOp; + VdbeOp *pOp, *pLastOp; Index *pIdx = 0; - struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom]; - Table *pTab = pTabItem->pTab; + SrcItem *pTabItem = &pTabList->a[pLevel->iFrom]; + Table *pTab = pTabItem->pSTab; assert( pTab!=0 ); pLoop = pLevel->pWLoop; - /* For a co-routine, change all OP_Column references to the table of - ** the co-routine into OP_Copy of result contained in a register. - ** OP_Rowid becomes OP_Null. + /* Do RIGHT JOIN processing. Generate code that will output the + ** unmatched rows of the right operand of the RIGHT JOIN with + ** all of the columns of the left operand set to NULL. */ - if( pTabItem->fg.viaCoroutine && !db->mallocFailed ){ - translateColumnToCopy(v, pLevel->addrBody, pLevel->iTabCur, - pTabItem->regResult, 0); + if( pLevel->pRJ ){ + sqlite3WhereRightJoinLoop(pWInfo, i, pLevel); continue; } - /* Close all of the cursors that were opened by sqlite3WhereBegin. - ** Except, do not close cursors that will be reused by the OR optimization - ** (WHERE_OMIT_OPEN_CLOSE). And do not close the OP_OpenWrite cursors - ** created for the ONEPASS optimization. + /* For a co-routine, change all OP_Column references to the table of + ** the co-routine into OP_Copy of result contained in a register. + ** OP_Rowid becomes OP_Null. */ - if( (pTab->tabFlags & TF_Ephemeral)==0 - && pTab->pSelect==0 - && (pWInfo->wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 - ){ - int ws = pLoop->wsFlags; - if( pWInfo->eOnePass==ONEPASS_OFF && (ws & WHERE_IDX_ONLY)==0 ){ - sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor); - } - if( (ws & WHERE_INDEXED)!=0 - && (ws & (WHERE_IPK|WHERE_AUTO_INDEX))==0 - && pLevel->iIdxCur!=pWInfo->aiCurOnePass[1] - ){ - sqlite3VdbeAddOp1(v, OP_Close, pLevel->iIdxCur); - } + if( pTabItem->fg.viaCoroutine ){ + testcase( pParse->db->mallocFailed ); + assert( pTabItem->fg.isSubquery ); + assert( pTabItem->u4.pSubq->regResult>=0 ); + translateColumnToCopy(pParse, pLevel->addrBody, pLevel->iTabCur, + pTabItem->u4.pSubq->regResult, 0); + continue; } /* If this scan uses an index, make VDBE code substitutions to read data ** from the index instead of from the table where possible. In some cases ** this optimization prevents the table from ever being read, which can ** yield a significant performance boost. - ** + ** ** Calls to the code generator in between sqlite3WhereBegin and ** sqlite3WhereEnd will have created code that references the table ** directly. This loop scans all that code looking for opcodes @@ -4574,42 +7616,127 @@ void sqlite3WhereEnd(WhereInfo *pWInfo){ if( pLoop->wsFlags & (WHERE_INDEXED|WHERE_IDX_ONLY) ){ pIdx = pLoop->u.btree.pIndex; }else if( pLoop->wsFlags & WHERE_MULTI_OR ){ - pIdx = pLevel->u.pCovidx; + pIdx = pLevel->u.pCoveringIdx; } if( pIdx - && (pWInfo->eOnePass==ONEPASS_OFF || !HasRowid(pIdx->pTable)) && !db->mallocFailed ){ - last = sqlite3VdbeCurrentAddr(v); - k = pLevel->addrBody; + if( pWInfo->eOnePass==ONEPASS_OFF || !HasRowid(pIdx->pTable) ){ + last = iEnd; + }else{ + last = pWInfo->iEndWhere; + } + if( pIdx->bHasExpr ){ + IndexedExpr *p = pParse->pIdxEpr; + while( p ){ + if( p->iIdxCur==pLevel->iIdxCur ){ +#ifdef WHERETRACE_ENABLED + if( sqlite3WhereTrace & 0x200 ){ + sqlite3DebugPrintf("Disable pParse->pIdxEpr term {%d,%d}\n", + p->iIdxCur, p->iIdxCol); + if( sqlite3WhereTrace & 0x5000 ) sqlite3ShowExpr(p->pExpr); + } +#endif + p->iDataCur = -1; + p->iIdxCur = -1; + } + p = p->pIENext; + } + } + k = pLevel->addrBody + 1; +#ifdef SQLITE_DEBUG + if( db->flags & SQLITE_VdbeAddopTrace ){ + printf("TRANSLATE cursor %d->%d in opcode range %d..%d\n", + pLevel->iTabCur, pLevel->iIdxCur, k, last-1); + } + /* Proof that the "+1" on the k value above is safe */ + pOp = sqlite3VdbeGetOp(v, k - 1); + assert( pOp->opcode!=OP_Column || pOp->p1!=pLevel->iTabCur ); + assert( pOp->opcode!=OP_Rowid || pOp->p1!=pLevel->iTabCur ); + assert( pOp->opcode!=OP_IfNullRow || pOp->p1!=pLevel->iTabCur ); +#endif pOp = sqlite3VdbeGetOp(v, k); - for(; k<last; k++, pOp++){ - if( pOp->p1!=pLevel->iTabCur ) continue; - if( pOp->opcode==OP_Column ){ + pLastOp = pOp + (last - k); + assert( pOp<=pLastOp ); + do{ + if( pOp->p1!=pLevel->iTabCur ){ + /* no-op */ + }else if( pOp->opcode==OP_Column +#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC + || pOp->opcode==OP_Offset +#endif + ){ int x = pOp->p2; assert( pIdx->pTable==pTab ); +#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC + if( pOp->opcode==OP_Offset ){ + /* Do not need to translate the column number */ + }else +#endif if( !HasRowid(pTab) ){ Index *pPk = sqlite3PrimaryKeyIndex(pTab); x = pPk->aiColumn[x]; assert( x>=0 ); + }else{ + testcase( x!=sqlite3StorageColumnToTable(pTab,x) ); + x = sqlite3StorageColumnToTable(pTab,x); } - x = sqlite3ColumnOfIndex(pIdx, x); + x = sqlite3TableColumnToIndex(pIdx, x); if( x>=0 ){ pOp->p2 = x; pOp->p1 = pLevel->iIdxCur; + OpcodeRewriteTrace(db, k, pOp); + }else if( pLoop->wsFlags & (WHERE_IDX_ONLY|WHERE_EXPRIDX) ){ + if( pLoop->wsFlags & WHERE_IDX_ONLY ){ + /* An error. pLoop is supposed to be a covering index loop, + ** and yet the VM code refers to a column of the table that + ** is not part of the index. */ + sqlite3ErrorMsg(pParse, "internal query planner error"); + pParse->rc = SQLITE_INTERNAL; + }else{ + /* The WHERE_EXPRIDX flag is set by the planner when it is likely + ** that pLoop is a covering index loop, but it is not possible + ** to be 100% sure. In this case, any OP_Explain opcode + ** corresponding to this loop describes the index as a "COVERING + ** INDEX". But, pOp proves that pLoop is not actually a covering + ** index loop. So clear the WHERE_EXPRIDX flag and rewrite the + ** text that accompanies the OP_Explain opcode, if any. */ + pLoop->wsFlags &= ~WHERE_EXPRIDX; + sqlite3WhereAddExplainText(pParse, + pLevel->addrBody-1, + pTabList, + pLevel, + pWInfo->wctrlFlags + ); + } } - assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || x>=0 ); }else if( pOp->opcode==OP_Rowid ){ pOp->p1 = pLevel->iIdxCur; pOp->opcode = OP_IdxRowid; + OpcodeRewriteTrace(db, k, pOp); + }else if( pOp->opcode==OP_IfNullRow ){ + pOp->p1 = pLevel->iIdxCur; + OpcodeRewriteTrace(db, k, pOp); } - } +#ifdef SQLITE_DEBUG + k++; +#endif + }while( (++pOp)<pLastOp ); +#ifdef SQLITE_DEBUG + if( db->flags & SQLITE_VdbeAddopTrace ) printf("TRANSLATE complete\n"); +#endif } } + /* The "break" point is here, just past the end of the outer loop. + ** Set it. + */ + sqlite3VdbeResolveLabel(v, pWInfo->iBreak); + /* Final cleanup */ pParse->nQueryLoop = pWInfo->savedNQueryLoop; whereInfoFree(db, pWInfo); + pParse->withinRJSubrtn -= nRJ; return; } diff --git a/src/whereInt.h b/src/whereInt.h index 1a189980ef..09e02c8c73 100644 --- a/src/whereInt.h +++ b/src/whereInt.h @@ -14,20 +14,9 @@ ** planner logic in "where.c". These definitions are broken out into ** a separate source file for easier editing. */ +#ifndef SQLITE_WHEREINT_H +#define SQLITE_WHEREINT_H -/* -** Trace output macros -*/ -#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) -/***/ int sqlite3WhereTrace; -#endif -#if defined(SQLITE_DEBUG) \ - && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_WHERETRACE)) -# define WHERETRACE(K,X) if(sqlite3WhereTrace&(K)) sqlite3DebugPrintf X -# define WHERETRACE_ENABLED 1 -#else -# define WHERETRACE(K,X) -#endif /* Forward references */ @@ -43,6 +32,28 @@ typedef struct WhereLoopBuilder WhereLoopBuilder; typedef struct WhereScan WhereScan; typedef struct WhereOrCost WhereOrCost; typedef struct WhereOrSet WhereOrSet; +typedef struct WhereMemBlock WhereMemBlock; +typedef struct WhereRightJoin WhereRightJoin; + +/* +** This object is a header on a block of allocated memory that will be +** automatically freed when its WInfo object is destructed. +*/ +struct WhereMemBlock { + WhereMemBlock *pNext; /* Next block in the chain */ + u64 sz; /* Bytes of space */ +}; + +/* +** Extra information attached to a WhereLevel that is a RIGHT JOIN. +*/ +struct WhereRightJoin { + int iMatch; /* Cursor used to determine prior matched rows */ + int regBloom; /* Bloom filter for iRJMatch */ + int regReturn; /* Return register for the interior subroutine */ + int addrSubrtn; /* Starting address for the interior subroutine */ + int endSubrtn; /* The last opcode in the interior subroutine */ +}; /* ** This object contains information needed to implement a single nested @@ -64,28 +75,35 @@ struct WhereLevel { int iTabCur; /* The VDBE cursor used to access the table */ int iIdxCur; /* The VDBE cursor used to access pIdx */ int addrBrk; /* Jump here to break out of the loop */ + int addrHalt; /* Abort the query due to empty table or similar */ int addrNxt; /* Jump here to start the next IN combination */ int addrSkip; /* Jump here for next iteration of skip-scan */ int addrCont; /* Jump here to continue with the next loop cycle */ int addrFirst; /* First instruction of interior of the loop */ int addrBody; /* Beginning of the body of this loop */ + int regBignull; /* big-null flag reg. True if a NULL-scan is needed */ + int addrBignull; /* Jump here for next part of big-null scan */ #ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS - int iLikeRepCntr; /* LIKE range processing counter register */ + u32 iLikeRepCntr; /* LIKE range processing counter register (times 2) */ int addrLikeRep; /* LIKE range processing address */ #endif + int regFilter; /* Bloom filter */ + WhereRightJoin *pRJ; /* Extra information for RIGHT JOIN */ u8 iFrom; /* Which entry in the FROM clause */ u8 op, p3, p5; /* Opcode, P3 & P5 of the opcode that ends the loop */ - int p1, p2; /* Operands of the opcode used to ends the loop */ + int p1, p2; /* Operands of the opcode used to end the loop */ union { /* Information that depends on pWLoop->wsFlags */ struct { int nIn; /* Number of entries in aInLoop[] */ struct InLoop { int iCur; /* The VDBE cursor used by this IN operator */ int addrInTop; /* Top of the IN loop */ + int iBase; /* Base register of multi-key index record */ + int nPrefix; /* Number of prior entries in the key */ u8 eEndLoopOp; /* IN Loop terminator. OP_Next or OP_Prev */ } *aInLoop; /* Information about each nested IN operator */ } in; /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */ - Index *pCovidx; /* Possible covering index for WHERE_MULTI_OR */ + Index *pCoveringIdx; /* Possible covering index for WHERE_MULTI_OR */ } u; struct WhereLoop *pWLoop; /* The selected WhereLoop object */ Bitmask notReady; /* FROM entries not usable at this level */ @@ -122,14 +140,21 @@ struct WhereLoop { union { struct { /* Information for internal btree tables */ u16 nEq; /* Number of equality constraints */ + u16 nBtm; /* Size of BTM vector */ + u16 nTop; /* Size of TOP vector */ + u16 nDistinctCol; /* Index columns used to sort for DISTINCT */ Index *pIndex; /* Index used, or NULL */ + ExprList *pOrderBy; /* ORDER BY clause if this is really a subquery */ } btree; struct { /* Information for virtual tables */ int idxNum; /* Index number */ - u8 needFree; /* True if sqlite3_free(idxStr) is needed */ + u32 needFree : 1; /* True if sqlite3_free(idxStr) is needed */ + u32 bOmitOffset : 1; /* True to let virtual table handle offset */ + u32 bIdxNumHex : 1; /* Show idxNum as hex in EXPLAIN QUERY PLAN */ i8 isOrdered; /* True if satisfies ORDER BY */ u16 omitMask; /* Terms that may be omitted */ char *idxStr; /* Index identifier string */ + u32 mHandleIn; /* Terms to handle as IN(...) instead of == */ } vtab; } u; u32 wsFlags; /* WHERE_* flags describing the plan */ @@ -138,6 +163,10 @@ struct WhereLoop { /**** whereLoopXfer() copies fields above ***********************/ # define WHERE_LOOP_XFER_SZ offsetof(WhereLoop,nLSlot) u16 nLSlot; /* Number of slots allocated for aLTerm[] */ +#ifdef WHERETRACE_ENABLED + LogEst rStarDelta; /* Cost delta due to star-schema heuristic. Not + ** initialized unless pWInfo->bStarUsed */ +#endif WhereTerm **aLTerm; /* WhereTerms used */ WhereLoop *pNextLoop; /* Next WhereLoop object in the WhereClause */ WhereTerm *aLTermSpace[3]; /* Initial aLTerm[] space */ @@ -145,7 +174,7 @@ struct WhereLoop { /* This object holds the prerequisites and the cost of running a ** subquery on one operand of an OR operator in the WHERE clause. -** See WhereOrSet for additional information +** See WhereOrSet for additional information */ struct WhereOrCost { Bitmask prereq; /* Prerequisites */ @@ -186,7 +215,7 @@ struct WherePath { Bitmask revLoop; /* aLoop[]s that should be reversed for ORDER BY */ LogEst nRow; /* Estimated number of rows generated by this path */ LogEst rCost; /* Total cost of this path */ - LogEst rUnsorted; /* Total cost of this path ignoring sorting costs */ + LogEst rUnsort; /* Total cost of this path ignoring sorting costs */ i8 isOrdered; /* No. of ORDER BY terms satisfied. -1 for unknown */ WhereLoop **aLoop; /* Array of WhereLoop objects implementing this path */ }; @@ -197,7 +226,7 @@ struct WherePath { ** clause subexpression is separated from the others by AND operators, ** usually, or sometimes subexpressions separated by OR. ** -** All WhereTerms are collected into a single WhereClause structure. +** All WhereTerms are collected into a single WhereClause structure. ** The following identity holds: ** ** WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm @@ -244,19 +273,25 @@ struct WherePath { */ struct WhereTerm { Expr *pExpr; /* Pointer to the subexpression that is this term */ + WhereClause *pWC; /* The clause this term is part of */ + LogEst truthProb; /* Probability of truth for this expression */ + u16 wtFlags; /* TERM_xxx bit flags. See below */ + u16 eOperator; /* A WO_xx value describing <op> */ + u8 nChild; /* Number of children that must disable us */ + u8 eMatchOp; /* Op for vtab MATCH/LIKE/GLOB/REGEXP terms */ int iParent; /* Disable pWC->a[iParent] when this term disabled */ int leftCursor; /* Cursor number of X in "X <op> <expr>" */ +#ifdef SQLITE_DEBUG + int iTerm; /* Which WhereTerm is this, for debug purposes */ +#endif union { - int leftColumn; /* Column number of X in "X <op> <expr>" */ + struct { + int leftColumn; /* Column number of X in "X <op> <expr>" */ + int iField; /* Field in (?,?,?) IN (SELECT...) vector */ + } x; /* Opcode other than OP_OR or OP_AND */ WhereOrInfo *pOrInfo; /* Extra information if (eOperator & WO_OR)!=0 */ WhereAndInfo *pAndInfo; /* Extra information if (eOperator& WO_AND)!=0 */ } u; - LogEst truthProb; /* Probability of truth for this expression */ - u16 eOperator; /* A WO_xx value describing <op> */ - u16 wtFlags; /* TERM_xxx bit flags. See below */ - u8 nChild; /* Number of children that must disable us */ - u8 eMatchOp; /* Op for vtab MATCH/LIKE/GLOB/REGEXP terms */ - WhereClause *pWC; /* The clause this term is part of */ Bitmask prereqRight; /* Bitmask of tables used by pExpr->pRight */ Bitmask prereqAll; /* Bitmask of tables referenced by pExpr */ }; @@ -264,22 +299,26 @@ struct WhereTerm { /* ** Allowed values of WhereTerm.wtFlags */ -#define TERM_DYNAMIC 0x01 /* Need to call sqlite3ExprDelete(db, pExpr) */ -#define TERM_VIRTUAL 0x02 /* Added by the optimizer. Do not code */ -#define TERM_CODED 0x04 /* This term is already coded */ -#define TERM_COPIED 0x08 /* Has a child */ -#define TERM_ORINFO 0x10 /* Need to free the WhereTerm.u.pOrInfo object */ -#define TERM_ANDINFO 0x20 /* Need to free the WhereTerm.u.pAndInfo obj */ -#define TERM_OR_OK 0x40 /* Used during OR-clause processing */ -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 -# define TERM_VNULL 0x80 /* Manufactured x>NULL or x<=NULL term */ +#define TERM_DYNAMIC 0x0001 /* Need to call sqlite3ExprDelete(db, pExpr) */ +#define TERM_VIRTUAL 0x0002 /* Added by the optimizer. Do not code */ +#define TERM_CODED 0x0004 /* This term is already coded */ +#define TERM_COPIED 0x0008 /* Has a child */ +#define TERM_ORINFO 0x0010 /* Need to free the WhereTerm.u.pOrInfo object */ +#define TERM_ANDINFO 0x0020 /* Need to free the WhereTerm.u.pAndInfo obj */ +#define TERM_OK 0x0040 /* Used during OR-clause processing */ +#define TERM_VNULL 0x0080 /* Manufactured x>NULL or x<=NULL term */ +#define TERM_LIKEOPT 0x0100 /* Virtual terms from the LIKE optimization */ +#define TERM_LIKECOND 0x0200 /* Conditionally this LIKE operator term */ +#define TERM_LIKE 0x0400 /* The original LIKE operator */ +#define TERM_IS 0x0800 /* Term.pExpr is an IS operator */ +#define TERM_VARSELECT 0x1000 /* Term.pExpr contains a correlated sub-query */ +#define TERM_HEURTRUTH 0x2000 /* Heuristic truthProb used */ +#ifdef SQLITE_ENABLE_STAT4 +# define TERM_HIGHTRUTH 0x4000 /* Term excludes few rows */ #else -# define TERM_VNULL 0x00 /* Disabled if not using stat3 */ +# define TERM_HIGHTRUTH 0 /* Only used with STAT4 */ #endif -#define TERM_LIKEOPT 0x100 /* Virtual terms from the LIKE optimization */ -#define TERM_LIKECOND 0x200 /* Conditionally this LIKE operator term */ -#define TERM_LIKE 0x400 /* The original LIKE operator */ -#define TERM_IS 0x800 /* Term.pExpr is an IS operator */ +#define TERM_SLICE 0x8000 /* One slice of a row-value/vector comparison */ /* ** An instance of the WhereScan object is used as an iterator for locating @@ -290,11 +329,11 @@ struct WhereScan { WhereClause *pWC; /* WhereClause currently being scanned */ const char *zCollName; /* Required collating sequence, if not NULL */ Expr *pIdxExpr; /* Search for this index expression */ - char idxaff; /* Must match this affinity, if zCollName!=NULL */ - unsigned char nEquiv; /* Number of entries in aEquiv[] */ - unsigned char iEquiv; /* Next unused slot in aEquiv[] */ - u32 opMask; /* Acceptable operators */ int k; /* Resume scanning at this->pWC->a[this->k] */ + u32 opMask; /* Acceptable operators */ + char idxaff; /* Must match this affinity, if zCollName!=NULL */ + unsigned char iEquiv; /* Current slot in aiCur[] and aiColumn[] */ + unsigned char nEquiv; /* Number of entries in aiCur[] and aiColumn[] */ int aiCur[11]; /* Cursors in the equivalence class */ i16 aiColumn[11]; /* Corresponding column number in the eq-class */ }; @@ -315,9 +354,11 @@ struct WhereClause { WhereInfo *pWInfo; /* WHERE clause processing context */ WhereClause *pOuter; /* Outer conjunction */ u8 op; /* Split operator. TK_AND or TK_OR */ + u8 hasOr; /* True if any a[].eOperator is WO_OR */ int nTerm; /* Number of terms */ int nSlot; /* Number of entries in a[] */ - WhereTerm *a; /* Each a[] describes a term of the WHERE cluase */ + int nBase; /* Number of terms through the last non-Virtual */ + WhereTerm *a; /* Each a[] describes a term of the WHERE clause */ #if defined(SQLITE_SMALL_STACK) WhereTerm aStatic[1]; /* Initial static space for a[] */ #else @@ -346,8 +387,8 @@ struct WhereAndInfo { ** An instance of the following structure keeps track of a mapping ** between VDBE cursor numbers and bits of the bitmasks in WhereTerm. ** -** The VDBE cursor numbers are small integers contained in -** SrcList_item.iCursor and Expr.iTable fields. For any given WHERE +** The VDBE cursor numbers are small integers contained in +** SrcItem.iCursor and Expr.iTable fields. For any given WHERE ** clause, the cursor numbers might not begin with 0 and they might ** contain gaps in the numbering sequence. But we want to make maximum ** use of the bits in our bitmasks. This structure provides a mapping @@ -369,15 +410,11 @@ struct WhereAndInfo { ** no gaps. */ struct WhereMaskSet { + int bVarSelect; /* Used by sqlite3WhereExprUsage() */ int n; /* Number of assigned cursor values */ int ix[BMS]; /* Cursor assigned to each bit */ }; -/* -** Initialize a WhereMaskSet object -*/ -#define initMaskSet(P) (P)->n=0 - /* ** This object is a convenience wrapper holding all information needed ** to construct WhereLoop objects for a particular query. @@ -385,15 +422,43 @@ struct WhereMaskSet { struct WhereLoopBuilder { WhereInfo *pWInfo; /* Information about this WHERE */ WhereClause *pWC; /* WHERE clause terms */ - ExprList *pOrderBy; /* ORDER BY clause */ WhereLoop *pNew; /* Template WhereLoop */ WhereOrSet *pOrSet; /* Record best loops here, if not NULL */ -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 +#ifdef SQLITE_ENABLE_STAT4 UnpackedRecord *pRec; /* Probe for stat4 (if required) */ int nRecValid; /* Number of valid fields currently in pRec */ #endif + unsigned char bldFlags1; /* First set of SQLITE_BLDF_* flags */ + unsigned char bldFlags2; /* Second set of SQLITE_BLDF_* flags */ + unsigned int iPlanLimit; /* Search limiter */ }; +/* Allowed values for WhereLoopBuider.bldFlags */ +#define SQLITE_BLDF1_INDEXED 0x0001 /* An index is used */ +#define SQLITE_BLDF1_UNIQUE 0x0002 /* All keys of a UNIQUE index used */ + +#define SQLITE_BLDF2_2NDPASS 0x0004 /* Second builder pass needed */ + +/* The WhereLoopBuilder.iPlanLimit is used to limit the number of +** index+constraint combinations the query planner will consider for a +** particular query. If this parameter is unlimited, then certain +** pathological queries can spend excess time in the sqlite3WhereBegin() +** routine. The limit is high enough that is should not impact real-world +** queries. +** +** SQLITE_QUERY_PLANNER_LIMIT is the baseline limit. The limit is +** increased by SQLITE_QUERY_PLANNER_LIMIT_INCR before each term of the FROM +** clause is processed, so that every table in a join is guaranteed to be +** able to propose a some index+constraint combinations even if the initial +** baseline limit was exhausted by prior tables of the join. +*/ +#ifndef SQLITE_QUERY_PLANNER_LIMIT +# define SQLITE_QUERY_PLANNER_LIMIT 20000 +#endif +#ifndef SQLITE_QUERY_PLANNER_LIMIT_INCR +# define SQLITE_QUERY_PLANNER_LIMIT_INCR 1000 +#endif + /* ** The WHERE clause processing routine has two halves. The ** first part does the start of the WHERE loop and the second @@ -408,33 +473,57 @@ struct WhereInfo { Parse *pParse; /* Parsing and code generating context */ SrcList *pTabList; /* List of tables in the join */ ExprList *pOrderBy; /* The ORDER BY clause or NULL */ - ExprList *pResultSet; /* Result set. DISTINCT operates on these */ - WhereLoop *pLoops; /* List of all WhereLoop objects */ - Bitmask revMask; /* Mask of ORDER BY terms that need reversing */ - LogEst nRowOut; /* Estimated number of output rows */ + ExprList *pResultSet; /* Result set of the query */ +#if WHERETRACE_ENABLED + Expr *pWhere; /* The complete WHERE clause */ +#endif + Select *pSelect; /* The entire SELECT statement containing WHERE */ + int aiCurOnePass[2]; /* OP_OpenWrite cursors for the ONEPASS opt */ + int iContinue; /* Jump here to continue with next record */ + int iBreak; /* Jump here to break out of the loop */ + int savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */ u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */ + LogEst iLimit; /* LIMIT if wctrlFlags has WHERE_USE_LIMIT */ + u8 nLevel; /* Number of nested loop */ i8 nOBSat; /* Number of ORDER BY terms satisfied by indices */ - u8 sorted; /* True if really sorted (not just grouped) */ u8 eOnePass; /* ONEPASS_OFF, or _SINGLE, or _MULTI */ - u8 untestedTerms; /* Not all WHERE terms resolved by outer loop */ - u8 eDistinct; /* One of the WHERE_DISTINCT_* values below */ - u8 nLevel; /* Number of nested loop */ + u8 eDistinct; /* One of the WHERE_DISTINCT_* values */ + unsigned bDeferredSeek :1; /* Uses OP_DeferredSeek */ + unsigned untestedTerms :1; /* Not all WHERE terms resolved by outer loop */ + unsigned bOrderedInnerLoop:1;/* True if only the inner-most loop is ordered */ + unsigned sorted :1; /* True if really sorted (not just grouped) */ + unsigned bStarDone :1; /* True if check for star-query is complete */ + unsigned bStarUsed :1; /* True if star-query heuristic is used */ + LogEst nRowOut; /* Estimated number of output rows */ +#ifdef WHERETRACE_ENABLED + LogEst rTotalCost; /* Total cost of the solution */ +#endif int iTop; /* The very beginning of the WHERE loop */ - int iContinue; /* Jump here to continue with next record */ - int iBreak; /* Jump here to break out of the loop */ - int savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */ - int aiCurOnePass[2]; /* OP_OpenWrite cursors for the ONEPASS opt */ - WhereMaskSet sMaskSet; /* Map cursor numbers to bitmasks */ + int iEndWhere; /* End of the WHERE clause itself */ + WhereLoop *pLoops; /* List of all WhereLoop objects */ + WhereMemBlock *pMemToFree;/* Memory to free when this object destroyed */ + Bitmask revMask; /* Mask of ORDER BY terms that need reversing */ WhereClause sWC; /* Decomposition of the WHERE clause */ - WhereLevel a[1]; /* Information about each nest loop in WHERE */ + WhereMaskSet sMaskSet; /* Map cursor numbers to bitmasks */ + WhereLevel a[FLEXARRAY]; /* Information about each nest loop in WHERE */ }; +/* +** The size (in bytes) of a WhereInfo object that holds N WhereLevels. +*/ +#define SZ_WHEREINFO(N) ROUND8(offsetof(WhereInfo,a)+(N)*sizeof(WhereLevel)) + /* ** Private interfaces - callable only by other where.c routines. ** ** where.c: */ Bitmask sqlite3WhereGetMask(WhereMaskSet*,int); +#ifdef WHERETRACE_ENABLED +void sqlite3WhereClausePrint(WhereClause *pWC); +void sqlite3WhereTermPrint(WhereTerm *pTerm, int iTerm); +void sqlite3WhereLoopPrint(const WhereLoop *p, const WhereClause *pWC); +#endif WhereTerm *sqlite3WhereFindTerm( WhereClause *pWC, /* The WHERE clause to be searched */ int iCur, /* Cursor number of LHS */ @@ -443,6 +532,8 @@ WhereTerm *sqlite3WhereFindTerm( u32 op, /* Mask of WO_xx values describing operator */ Index *pIdx /* Must be compatible with this index, if not NULL */ ); +void *sqlite3WhereMalloc(WhereInfo *pWInfo, u64 nByte); +void *sqlite3WhereRealloc(WhereInfo *pWInfo, void *pOld, u64 nByte); /* wherecode.c: */ #ifndef SQLITE_OMIT_EXPLAIN @@ -450,12 +541,24 @@ int sqlite3WhereExplainOneScan( Parse *pParse, /* Parse context */ SrcList *pTabList, /* Table list this loop refers to */ WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */ - int iLevel, /* Value for "level" column of output */ - int iFrom, /* Value for "from" column of output */ + u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */ +); +int sqlite3WhereExplainBloomFilter( + const Parse *pParse, /* Parse context */ + const WhereInfo *pWInfo, /* WHERE clause */ + const WhereLevel *pLevel /* Bloom filter on this level */ +); +void sqlite3WhereAddExplainText( + Parse *pParse, /* Parse context */ + int addr, + SrcList *pTabList, /* Table list this loop refers to */ + WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */ u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */ ); #else -# define sqlite3WhereExplainOneScan(u,v,w,x,y,z) 0 +# define sqlite3WhereExplainOneScan(u,v,w,x) 0 +# define sqlite3WhereExplainBloomFilter(u,v,w) 0 +# define sqlite3WhereAddExplainText(u,v,w,x,y) #endif /* SQLITE_OMIT_EXPLAIN */ #ifdef SQLITE_ENABLE_STMT_SCANSTATUS void sqlite3WhereAddScanStatus( @@ -468,19 +571,29 @@ void sqlite3WhereAddScanStatus( # define sqlite3WhereAddScanStatus(a, b, c, d) ((void)d) #endif Bitmask sqlite3WhereCodeOneLoopStart( + Parse *pParse, /* Parsing context */ + Vdbe *v, /* Prepared statement under construction */ WhereInfo *pWInfo, /* Complete information about the WHERE clause */ int iLevel, /* Which level of pWInfo->a[] should be coded */ + WhereLevel *pLevel, /* The current level pointer */ Bitmask notReady /* Which tables are currently available */ ); +SQLITE_NOINLINE void sqlite3WhereRightJoinLoop( + WhereInfo *pWInfo, + int iLevel, + WhereLevel *pLevel +); /* whereexpr.c: */ void sqlite3WhereClauseInit(WhereClause*,WhereInfo*); void sqlite3WhereClauseClear(WhereClause*); void sqlite3WhereSplit(WhereClause*,Expr*,u8); +void sqlite3WhereAddLimit(WhereClause*, Select*); Bitmask sqlite3WhereExprUsage(WhereMaskSet*, Expr*); +Bitmask sqlite3WhereExprUsageNN(WhereMaskSet*, Expr*); Bitmask sqlite3WhereExprListUsage(WhereMaskSet*, ExprList*); void sqlite3WhereExprAnalyze(SrcList*, WhereClause*); -void sqlite3WhereTabFuncArgs(Parse*, struct SrcList_item*, WhereClause*); +void sqlite3WhereTabFuncArgs(Parse*, SrcItem*, WhereClause*); @@ -491,6 +604,13 @@ void sqlite3WhereTabFuncArgs(Parse*, struct SrcList_item*, WhereClause*); ** operators that are of interest to the query planner. An ** OR-ed combination of these values can be used when searching for ** particular WhereTerms within a WhereClause. +** +** Value constraints: +** WO_EQ == SQLITE_INDEX_CONSTRAINT_EQ +** WO_LT == SQLITE_INDEX_CONSTRAINT_LT +** WO_LE == SQLITE_INDEX_CONSTRAINT_LE +** WO_GT == SQLITE_INDEX_CONSTRAINT_GT +** WO_GE == SQLITE_INDEX_CONSTRAINT_GE */ #define WO_IN 0x0001 #define WO_EQ 0x0002 @@ -498,15 +618,16 @@ void sqlite3WhereTabFuncArgs(Parse*, struct SrcList_item*, WhereClause*); #define WO_LE (WO_EQ<<(TK_LE-TK_EQ)) #define WO_GT (WO_EQ<<(TK_GT-TK_EQ)) #define WO_GE (WO_EQ<<(TK_GE-TK_EQ)) -#define WO_MATCH 0x0040 +#define WO_AUX 0x0040 /* Op useful to virtual tables only */ #define WO_IS 0x0080 #define WO_ISNULL 0x0100 #define WO_OR 0x0200 /* Two or more OR-connected terms */ #define WO_AND 0x0400 /* Two or more AND-connected terms */ #define WO_EQUIV 0x0800 /* Of the form A==B, both columns */ #define WO_NOOP 0x1000 /* This term does not restrict search space */ +#define WO_ROWVAL 0x2000 /* A row-value term */ -#define WO_ALL 0x1fff /* Mask of all possible WO_* values */ +#define WO_ALL 0x3fff /* Mask of all possible WO_* values */ #define WO_SINGLE 0x01ff /* Mask of all non-compound WO_* values */ /* @@ -533,3 +654,15 @@ void sqlite3WhereTabFuncArgs(Parse*, struct SrcList_item*, WhereClause*); #define WHERE_SKIPSCAN 0x00008000 /* Uses the skip-scan algorithm */ #define WHERE_UNQ_WANTED 0x00010000 /* WHERE_ONEROW would have been helpful*/ #define WHERE_PARTIALIDX 0x00020000 /* The automatic index is partial */ +#define WHERE_IN_EARLYOUT 0x00040000 /* Perhaps quit IN loops early */ +#define WHERE_BIGNULL_SORT 0x00080000 /* Column nEq of index is BIGNULL */ +#define WHERE_IN_SEEKSCAN 0x00100000 /* Seek-scan optimization for IN */ +#define WHERE_TRANSCONS 0x00200000 /* Uses a transitive constraint */ +#define WHERE_BLOOMFILTER 0x00400000 /* Consider using a Bloom-filter */ +#define WHERE_SELFCULL 0x00800000 /* nOut reduced by extra WHERE terms */ +#define WHERE_OMIT_OFFSET 0x01000000 /* Set offset counter to zero */ +#define WHERE_COROUTINE 0x02000000 /* Implemented by co-routine. + ** NB: False-negatives are possible */ +#define WHERE_EXPRIDX 0x04000000 /* Uses an index-on-expressions */ + +#endif /* !defined(SQLITE_WHEREINT_H) */ diff --git a/src/wherecode.c b/src/wherecode.c index bb48e5dc70..1efa34a5da 100644 --- a/src/wherecode.c +++ b/src/wherecode.c @@ -21,6 +21,17 @@ #include "whereInt.h" #ifndef SQLITE_OMIT_EXPLAIN + +/* +** Return the name of the i-th column of the pIdx index. +*/ +static const char *explainIndexColumnName(Index *pIdx, int i){ + i = pIdx->aiColumn[i]; + if( i==XN_EXPR ) return "<expr>"; + if( i==XN_ROWID ) return "rowid"; + return pIdx->pTable->aCol[i].zCnName; +} + /* ** This routine is a helper for explainIndexRange() below ** @@ -31,28 +42,36 @@ */ static void explainAppendTerm( StrAccum *pStr, /* The text expression being built */ - int iTerm, /* Index of this term. First is zero */ - const char *zColumn, /* Name of the column */ + Index *pIdx, /* Index to read column names from */ + int nTerm, /* Number of terms */ + int iTerm, /* Zero-based index of first term. */ + int bAnd, /* Non-zero to append " AND " */ const char *zOp /* Name of the operator */ ){ - if( iTerm ) sqlite3StrAccumAppend(pStr, " AND ", 5); - sqlite3StrAccumAppendAll(pStr, zColumn); - sqlite3StrAccumAppend(pStr, zOp, 1); - sqlite3StrAccumAppend(pStr, "?", 1); -} + int i; -/* -** Return the name of the i-th column of the pIdx index. -*/ -static const char *explainIndexColumnName(Index *pIdx, int i){ - i = pIdx->aiColumn[i]; - if( i==XN_EXPR ) return "<expr>"; - if( i==XN_ROWID ) return "rowid"; - return pIdx->pTable->aCol[i].zName; + assert( nTerm>=1 ); + if( bAnd ) sqlite3_str_append(pStr, " AND ", 5); + + if( nTerm>1 ) sqlite3_str_append(pStr, "(", 1); + for(i=0; i<nTerm; i++){ + if( i ) sqlite3_str_append(pStr, ",", 1); + sqlite3_str_appendall(pStr, explainIndexColumnName(pIdx, iTerm+i)); + } + if( nTerm>1 ) sqlite3_str_append(pStr, ")", 1); + + sqlite3_str_append(pStr, zOp, 1); + + if( nTerm>1 ) sqlite3_str_append(pStr, "(", 1); + for(i=0; i<nTerm; i++){ + if( i ) sqlite3_str_append(pStr, ",", 1); + sqlite3_str_append(pStr, "?", 1); + } + if( nTerm>1 ) sqlite3_str_append(pStr, ")", 1); } /* -** Argument pLevel describes a strategy for scanning table pTab. This +** Argument pLevel describes a strategy for scanning table pTab. This ** function appends text to pStr that describes the subset of table ** rows scanned by the strategy in the form of an SQL expression. ** @@ -72,77 +91,67 @@ static void explainIndexRange(StrAccum *pStr, WhereLoop *pLoop){ int i, j; if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return; - sqlite3StrAccumAppend(pStr, " (", 2); + sqlite3_str_append(pStr, " (", 2); for(i=0; i<nEq; i++){ const char *z = explainIndexColumnName(pIndex, i); - if( i ) sqlite3StrAccumAppend(pStr, " AND ", 5); - sqlite3XPrintf(pStr, i>=nSkip ? "%s=?" : "ANY(%s)", z); + if( i ) sqlite3_str_append(pStr, " AND ", 5); + sqlite3_str_appendf(pStr, i>=nSkip ? "%s=?" : "ANY(%s)", z); } j = i; if( pLoop->wsFlags&WHERE_BTM_LIMIT ){ - const char *z = explainIndexColumnName(pIndex, i); - explainAppendTerm(pStr, i++, z, ">"); + explainAppendTerm(pStr, pIndex, pLoop->u.btree.nBtm, j, i, ">"); + i = 1; } if( pLoop->wsFlags&WHERE_TOP_LIMIT ){ - const char *z = explainIndexColumnName(pIndex, j); - explainAppendTerm(pStr, i, z, "<"); + explainAppendTerm(pStr, pIndex, pLoop->u.btree.nTop, j, i, "<"); } - sqlite3StrAccumAppend(pStr, ")", 1); + sqlite3_str_append(pStr, ")", 1); } /* -** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN -** command, or if either SQLITE_DEBUG or SQLITE_ENABLE_STMT_SCANSTATUS was -** defined at compile-time. If it is not a no-op, a single OP_Explain opcode -** is added to the output to describe the table scan strategy in pLevel. -** -** If an OP_Explain opcode is added to the VM, its address is returned. -** Otherwise, if no OP_Explain is coded, zero is returned. +** This function sets the P4 value of an existing OP_Explain opcode to +** text describing the loop in pLevel. If the OP_Explain opcode already has +** a P4 value, it is freed before it is overwritten. */ -int sqlite3WhereExplainOneScan( +void sqlite3WhereAddExplainText( Parse *pParse, /* Parse context */ + int addr, /* Address of OP_Explain opcode */ SrcList *pTabList, /* Table list this loop refers to */ WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */ - int iLevel, /* Value for "level" column of output */ - int iFrom, /* Value for "from" column of output */ u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */ ){ - int ret = 0; -#if !defined(SQLITE_DEBUG) && !defined(SQLITE_ENABLE_STMT_SCANSTATUS) - if( pParse->explain==2 ) +#if !defined(SQLITE_DEBUG) + if( sqlite3ParseToplevel(pParse)->explain==2 || IS_STMT_SCANSTATUS(pParse->db) ) #endif { - struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom]; - Vdbe *v = pParse->pVdbe; /* VM being constructed */ + VdbeOp *pOp = sqlite3VdbeGetOp(pParse->pVdbe, addr); + SrcItem *pItem = &pTabList->a[pLevel->iFrom]; sqlite3 *db = pParse->db; /* Database handle */ - int iId = pParse->iSelectId; /* Select id (left-most output column) */ int isSearch; /* True for a SEARCH. False for SCAN. */ WhereLoop *pLoop; /* The controlling WhereLoop object */ u32 flags; /* Flags that describe this loop */ +#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_EXPLAIN) char *zMsg; /* Text to add to EQP output */ +#endif StrAccum str; /* EQP output string */ char zBuf[100]; /* Initial space for EQP output string */ + if( db->mallocFailed ) return; + pLoop = pLevel->pWLoop; flags = pLoop->wsFlags; - if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return 0; isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0)) || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX)); sqlite3StrAccumInit(&str, db, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH); - sqlite3StrAccumAppendAll(&str, isSearch ? "SEARCH" : "SCAN"); - if( pItem->pSelect ){ - sqlite3XPrintf(&str, " SUBQUERY %d", pItem->iSelectId); - }else{ - sqlite3XPrintf(&str, " TABLE %s", pItem->zName); - } - - if( pItem->zAlias ){ - sqlite3XPrintf(&str, " AS %s", pItem->zAlias); - } + str.printfFlags = SQLITE_PRINTF_INTERNAL; + sqlite3_str_appendf(&str, "%s %S%s", + isSearch ? "SEARCH" : "SCAN", + pItem, + pItem->fg.fromExists ? " EXISTS" : ""); if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 ){ const char *zFmt = 0; Index *pIdx; @@ -150,7 +159,7 @@ int sqlite3WhereExplainOneScan( assert( pLoop->u.btree.pIndex!=0 ); pIdx = pLoop->u.btree.pIndex; assert( !(flags&WHERE_AUTO_INDEX) || (flags&WHERE_IDX_ONLY) ); - if( !HasRowid(pItem->pTab) && IsPrimaryKeyIndex(pIdx) ){ + if( !HasRowid(pItem->pSTab) && IsPrimaryKeyIndex(pIdx) ){ if( isSearch ){ zFmt = "PRIMARY KEY"; } @@ -158,46 +167,155 @@ int sqlite3WhereExplainOneScan( zFmt = "AUTOMATIC PARTIAL COVERING INDEX"; }else if( flags & WHERE_AUTO_INDEX ){ zFmt = "AUTOMATIC COVERING INDEX"; - }else if( flags & WHERE_IDX_ONLY ){ + }else if( flags & (WHERE_IDX_ONLY|WHERE_EXPRIDX) ){ zFmt = "COVERING INDEX %s"; }else{ zFmt = "INDEX %s"; } if( zFmt ){ - sqlite3StrAccumAppend(&str, " USING ", 7); - sqlite3XPrintf(&str, zFmt, pIdx->zName); + sqlite3_str_append(&str, " USING ", 7); + sqlite3_str_appendf(&str, zFmt, pIdx->zName); explainIndexRange(&str, pLoop); } }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){ - const char *zRangeOp; + char cRangeOp; +#if 0 /* Better output, but breaks many tests */ + const Table *pTab = pItem->pTab; + const char *zRowid = pTab->iPKey>=0 ? pTab->aCol[pTab->iPKey].zCnName: + "rowid"; +#else + const char *zRowid = "rowid"; +#endif + sqlite3_str_appendf(&str, " USING INTEGER PRIMARY KEY (%s", zRowid); if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){ - zRangeOp = "="; + cRangeOp = '='; }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){ - zRangeOp = ">? AND rowid<"; + sqlite3_str_appendf(&str, ">? AND %s", zRowid); + cRangeOp = '<'; }else if( flags&WHERE_BTM_LIMIT ){ - zRangeOp = ">"; + cRangeOp = '>'; }else{ assert( flags&WHERE_TOP_LIMIT); - zRangeOp = "<"; + cRangeOp = '<'; } - sqlite3XPrintf(&str, " USING INTEGER PRIMARY KEY (rowid%s?)",zRangeOp); + sqlite3_str_appendf(&str, "%c?)", cRangeOp); } #ifndef SQLITE_OMIT_VIRTUALTABLE else if( (flags & WHERE_VIRTUALTABLE)!=0 ){ - sqlite3XPrintf(&str, " VIRTUAL TABLE INDEX %d:%s", + sqlite3_str_appendall(&str, " VIRTUAL TABLE INDEX "); + sqlite3_str_appendf(&str, + pLoop->u.vtab.bIdxNumHex ? "0x%x:%s" : "%d:%s", pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr); } #endif + if( pItem->fg.jointype & JT_LEFT ){ + sqlite3_str_appendf(&str, " LEFT-JOIN"); + } #ifdef SQLITE_EXPLAIN_ESTIMATED_ROWS if( pLoop->nOut>=10 ){ - sqlite3XPrintf(&str, " (~%llu rows)", sqlite3LogEstToInt(pLoop->nOut)); + sqlite3_str_appendf(&str, " (~%llu rows)", + sqlite3LogEstToInt(pLoop->nOut)); }else{ - sqlite3StrAccumAppend(&str, " (~1 row)", 9); + sqlite3_str_append(&str, " (~1 row)", 9); } #endif +#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_EXPLAIN) zMsg = sqlite3StrAccumFinish(&str); - ret = sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg,P4_DYNAMIC); + sqlite3ExplainBreakpoint("",zMsg); +#endif + + assert( pOp->opcode==OP_Explain ); + assert( pOp->p4type==P4_DYNAMIC || pOp->p4.z==0 ); + sqlite3DbFree(db, pOp->p4.z); + pOp->p4type = P4_DYNAMIC; + pOp->p4.z = sqlite3StrAccumFinish(&str); } +} + + +/* +** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN +** command, or if stmt_scanstatus_v2() stats are enabled, or if SQLITE_DEBUG +** was defined at compile-time. If it is not a no-op, a single OP_Explain +** opcode is added to the output to describe the table scan strategy in pLevel. +** +** If an OP_Explain opcode is added to the VM, its address is returned. +** Otherwise, if no OP_Explain is coded, zero is returned. +*/ +int sqlite3WhereExplainOneScan( + Parse *pParse, /* Parse context */ + SrcList *pTabList, /* Table list this loop refers to */ + WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */ + u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */ +){ + int ret = 0; +#if !defined(SQLITE_DEBUG) + if( sqlite3ParseToplevel(pParse)->explain==2 || IS_STMT_SCANSTATUS(pParse->db) ) +#endif + { + if( (pLevel->pWLoop->wsFlags & WHERE_MULTI_OR)==0 + && (wctrlFlags & WHERE_OR_SUBCLAUSE)==0 + ){ + Vdbe *v = pParse->pVdbe; + int addr = sqlite3VdbeCurrentAddr(v); + ret = sqlite3VdbeAddOp3( + v, OP_Explain, addr, pParse->addrExplain, pLevel->pWLoop->rRun + ); + sqlite3WhereAddExplainText(pParse, addr, pTabList, pLevel, wctrlFlags); + } + } + return ret; +} + +/* +** Add a single OP_Explain opcode that describes a Bloom filter. +** +** Or if not processing EXPLAIN QUERY PLAN and not in a SQLITE_DEBUG and/or +** SQLITE_ENABLE_STMT_SCANSTATUS build, then OP_Explain opcodes are not +** required and this routine is a no-op. +** +** If an OP_Explain opcode is added to the VM, its address is returned. +** Otherwise, if no OP_Explain is coded, zero is returned. +*/ +int sqlite3WhereExplainBloomFilter( + const Parse *pParse, /* Parse context */ + const WhereInfo *pWInfo, /* WHERE clause */ + const WhereLevel *pLevel /* Bloom filter on this level */ +){ + int ret = 0; + SrcItem *pItem = &pWInfo->pTabList->a[pLevel->iFrom]; + Vdbe *v = pParse->pVdbe; /* VM being constructed */ + sqlite3 *db = pParse->db; /* Database handle */ + char *zMsg; /* Text to add to EQP output */ + int i; /* Loop counter */ + WhereLoop *pLoop; /* The where loop */ + StrAccum str; /* EQP output string */ + char zBuf[100]; /* Initial space for EQP output string */ + + sqlite3StrAccumInit(&str, db, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH); + str.printfFlags = SQLITE_PRINTF_INTERNAL; + sqlite3_str_appendf(&str, "BLOOM FILTER ON %S (", pItem); + pLoop = pLevel->pWLoop; + if( pLoop->wsFlags & WHERE_IPK ){ + const Table *pTab = pItem->pSTab; + if( pTab->iPKey>=0 ){ + sqlite3_str_appendf(&str, "%s=?", pTab->aCol[pTab->iPKey].zCnName); + }else{ + sqlite3_str_appendf(&str, "rowid=?"); + } + }else{ + for(i=pLoop->nSkip; i<pLoop->u.btree.nEq; i++){ + const char *z = explainIndexColumnName(pLoop->u.btree.pIndex, i); + if( i>pLoop->nSkip ) sqlite3_str_append(&str, " AND ", 5); + sqlite3_str_appendf(&str, "%s=?", z); + } + } + sqlite3_str_append(&str, ")", 1); + zMsg = sqlite3StrAccumFinish(&str); + ret = sqlite3VdbeAddOp4(v, OP_Explain, sqlite3VdbeCurrentAddr(v), + pParse->addrExplain, 0, zMsg,P4_DYNAMIC); + + sqlite3VdbeScanStatus(v, sqlite3VdbeCurrentAddr(v)-1, 0, 0, 0, 0); return ret; } #endif /* SQLITE_OMIT_EXPLAIN */ @@ -205,11 +323,11 @@ int sqlite3WhereExplainOneScan( #ifdef SQLITE_ENABLE_STMT_SCANSTATUS /* ** Configure the VM passed as the first argument with an -** sqlite3_stmt_scanstatus() entry corresponding to the scan used to -** implement level pLvl. Argument pSrclist is a pointer to the FROM +** sqlite3_stmt_scanstatus() entry corresponding to the scan used to +** implement level pLvl. Argument pSrclist is a pointer to the FROM ** clause that the scan reads data from. ** -** If argument addrExplain is not 0, it must be the address of an +** If argument addrExplain is not 0, it must be the address of an ** OP_Explain instruction that describes the same loop. */ void sqlite3WhereAddScanStatus( @@ -218,16 +336,40 @@ void sqlite3WhereAddScanStatus( WhereLevel *pLvl, /* Level to add scanstatus() entry for */ int addrExplain /* Address of OP_Explain (or 0) */ ){ - const char *zObj = 0; - WhereLoop *pLoop = pLvl->pWLoop; - if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 && pLoop->u.btree.pIndex!=0 ){ - zObj = pLoop->u.btree.pIndex->zName; - }else{ - zObj = pSrclist->a[pLvl->iFrom].zName; + if( IS_STMT_SCANSTATUS( sqlite3VdbeDb(v) ) ){ + const char *zObj = 0; + WhereLoop *pLoop = pLvl->pWLoop; + int wsFlags = pLoop->wsFlags; + int viaCoroutine = 0; + + if( (wsFlags & WHERE_VIRTUALTABLE)==0 && pLoop->u.btree.pIndex!=0 ){ + zObj = pLoop->u.btree.pIndex->zName; + }else{ + zObj = pSrclist->a[pLvl->iFrom].zName; + viaCoroutine = pSrclist->a[pLvl->iFrom].fg.viaCoroutine; + } + sqlite3VdbeScanStatus( + v, addrExplain, pLvl->addrBody, pLvl->addrVisit, pLoop->nOut, zObj + ); + + if( viaCoroutine==0 ){ + if( (wsFlags & (WHERE_MULTI_OR|WHERE_AUTO_INDEX))==0 ){ + sqlite3VdbeScanStatusRange(v, addrExplain, -1, pLvl->iTabCur); + } + if( wsFlags & WHERE_INDEXED ){ + sqlite3VdbeScanStatusRange(v, addrExplain, -1, pLvl->iIdxCur); + } + }else{ + int addr; + VdbeOp *pOp; + assert( pSrclist->a[pLvl->iFrom].fg.isSubquery ); + addr = pSrclist->a[pLvl->iFrom].u4.pSubq->addrFillSub; + pOp = sqlite3VdbeGetOp(v, addr-1); + assert( sqlite3VdbeDb(v)->mallocFailed || pOp->opcode==OP_InitCoroutine ); + assert( sqlite3VdbeDb(v)->mallocFailed || pOp->p2>addr ); + sqlite3VdbeScanStatusRange(v, addrExplain, addr, pOp->p2-1); + } } - sqlite3VdbeScanStatus( - v, addrExplain, pLvl->addrBody, pLvl->addrVisit, pLoop->nOut, zObj - ); } #endif @@ -265,7 +407,7 @@ void sqlite3WhereAddScanStatus( ** ** Only the parent term was in the original WHERE clause. The child1 ** and child2 terms were added by the LIKE optimization. If both of -** the virtual child terms are valid, then testing of the parent can be +** the virtual child terms are valid, then testing of the parent can be ** skipped. ** ** Usually the parent term is marked as TERM_CODED. But if the parent @@ -276,9 +418,9 @@ void sqlite3WhereAddScanStatus( */ static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){ int nLoop = 0; - while( pTerm - && (pTerm->wtFlags & TERM_CODED)==0 - && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin)) + assert( pTerm!=0 ); + while( (pTerm->wtFlags & TERM_CODED)==0 + && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_OuterON)) && (pLevel->notReady & pTerm->prereqAll)==0 ){ if( nLoop && (pTerm->wtFlags & TERM_LIKE)!=0 ){ @@ -286,8 +428,15 @@ static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){ }else{ pTerm->wtFlags |= TERM_CODED; } +#ifdef WHERETRACE_ENABLED + if( (sqlite3WhereTrace & 0x4001)==0x4001 ){ + sqlite3DebugPrintf("DISABLE-"); + sqlite3WhereTermPrint(pTerm, (int)(pTerm - (pTerm->pWC->a))); + } +#endif if( pTerm->iParent<0 ) break; pTerm = &pTerm->pWC->a[pTerm->iParent]; + assert( pTerm!=0 ); pTerm->nChild--; if( pTerm->nChild!=0 ) break; nLoop++; @@ -296,11 +445,11 @@ static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){ /* ** Code an OP_Affinity opcode to apply the column affinity string zAff -** to the n registers starting at base. +** to the n registers starting at base. ** -** As an optimization, SQLITE_AFF_BLOB entries (which are no-ops) at the -** beginning and end of zAff are ignored. If all entries in zAff are -** SQLITE_AFF_BLOB, then no code gets generated. +** As an optimization, SQLITE_AFF_BLOB and SQLITE_AFF_NONE entries (which +** are no-ops) at the beginning and end of zAff are ignored. If all entries +** in zAff are SQLITE_AFF_BLOB or SQLITE_AFF_NONE, then no code gets generated. ** ** This routine makes its own copy of zAff so that the caller is free ** to modify zAff after this routine returns. @@ -313,35 +462,342 @@ static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){ } assert( v!=0 ); - /* Adjust base and n to skip over SQLITE_AFF_BLOB entries at the beginning - ** and end of the affinity string. + /* Adjust base and n to skip over SQLITE_AFF_BLOB and SQLITE_AFF_NONE + ** entries at the beginning and end of the affinity string. */ - while( n>0 && zAff[0]==SQLITE_AFF_BLOB ){ + assert( SQLITE_AFF_NONE<SQLITE_AFF_BLOB ); + while( n>0 && zAff[0]<=SQLITE_AFF_BLOB ){ n--; base++; zAff++; } - while( n>1 && zAff[n-1]==SQLITE_AFF_BLOB ){ + while( n>1 && zAff[n-1]<=SQLITE_AFF_BLOB ){ n--; } /* Code the OP_Affinity opcode if there is anything left to do. */ if( n>0 ){ sqlite3VdbeAddOp4(v, OP_Affinity, base, n, 0, zAff, n); - sqlite3ExprCacheAffinityChange(pParse, base, n); } } +/* +** Expression pRight, which is the RHS of a comparison operation, is +** either a vector of n elements or, if n==1, a scalar expression. +** Before the comparison operation, affinity zAff is to be applied +** to the pRight values. This function modifies characters within the +** affinity string to SQLITE_AFF_BLOB if either: +** +** * the comparison will be performed with no affinity, or +** * the affinity change in zAff is guaranteed not to change the value. +*/ +static void updateRangeAffinityStr( + Expr *pRight, /* RHS of comparison */ + int n, /* Number of vector elements in comparison */ + char *zAff /* Affinity string to modify */ +){ + int i; + for(i=0; i<n; i++){ + Expr *p = sqlite3VectorFieldSubexpr(pRight, i); + if( sqlite3CompareAffinity(p, zAff[i])==SQLITE_AFF_BLOB + || sqlite3ExprNeedsNoAffinityChange(p, zAff[i]) + ){ + zAff[i] = SQLITE_AFF_BLOB; + } + } +} + +/* +** The pOrderBy->a[].u.x.iOrderByCol values might be incorrect because +** columns might have been rearranged in the result set. This routine +** fixes them up. +** +** pEList is the new result set. The pEList->a[].u.x.iOrderByCol values +** contain the *old* locations of each expression. This is a temporary +** use of u.x.iOrderByCol, not its intended use. The caller must reset +** u.x.iOrderByCol back to zero for all entries in pEList before the +** caller returns. +** +** This routine changes pOrderBy->a[].u.x.iOrderByCol values from +** pEList->a[N].u.x.iOrderByCol into N+1. (The "+1" is because of the 1-based +** indexing used by iOrderByCol.) Or if no match, iOrderByCol is set to zero. +*/ +static void adjustOrderByCol(ExprList *pOrderBy, ExprList *pEList){ + int i, j; + if( pOrderBy==0 ) return; + for(i=0; i<pOrderBy->nExpr; i++){ + int t = pOrderBy->a[i].u.x.iOrderByCol; + if( t==0 ) continue; + for(j=0; j<pEList->nExpr; j++){ + if( pEList->a[j].u.x.iOrderByCol==t ){ + pOrderBy->a[i].u.x.iOrderByCol = j+1; + break; + } + } + if( j>=pEList->nExpr ){ + pOrderBy->a[i].u.x.iOrderByCol = 0; + } + } +} + + +/* +** pX is an expression of the form: (vector) IN (SELECT ...) +** In other words, it is a vector IN operator with a SELECT clause on the +** RHS. But not all terms in the vector are indexable and the terms might +** not be in the correct order for indexing. +** +** This routine makes a copy of the input pX expression and then adjusts +** the vector on the LHS with corresponding changes to the SELECT so that +** the vector contains only index terms and those terms are in the correct +** order. The modified IN expression is returned. The caller is responsible +** for deleting the returned expression. +** +** Example: +** +** CREATE TABLE t1(a,b,c,d,e,f); +** CREATE INDEX t1x1 ON t1(e,c); +** SELECT * FROM t1 WHERE (a,b,c,d,e) IN (SELECT v,w,x,y,z FROM t2) +** \_______________________________________/ +** The pX expression +** +** Since only columns e and c can be used with the index, in that order, +** the modified IN expression that is returned will be: +** +** (e,c) IN (SELECT z,x FROM t2) +** +** The reduced pX is different from the original (obviously) and thus is +** only used for indexing, to improve performance. The original unaltered +** IN expression must also be run on each output row for correctness. +*/ +static Expr *removeUnindexableInClauseTerms( + Parse *pParse, /* The parsing context */ + int iEq, /* Look at loop terms starting here */ + WhereLoop *pLoop, /* The current loop */ + Expr *pX /* The IN expression to be reduced */ +){ + sqlite3 *db = pParse->db; + Select *pSelect; /* Pointer to the SELECT on the RHS */ + Expr *pNew; + pNew = sqlite3ExprDup(db, pX, 0); + if( db->mallocFailed==0 ){ + for(pSelect=pNew->x.pSelect; pSelect; pSelect=pSelect->pPrior){ + ExprList *pOrigRhs; /* Original unmodified RHS */ + ExprList *pOrigLhs = 0; /* Original unmodified LHS */ + ExprList *pRhs = 0; /* New RHS after modifications */ + ExprList *pLhs = 0; /* New LHS after mods */ + int i; /* Loop counter */ + + assert( ExprUseXSelect(pNew) ); + pOrigRhs = pSelect->pEList; + assert( pNew->pLeft!=0 ); + assert( ExprUseXList(pNew->pLeft) ); + if( pSelect==pNew->x.pSelect ){ + pOrigLhs = pNew->pLeft->x.pList; + } + for(i=iEq; i<pLoop->nLTerm; i++){ + if( pLoop->aLTerm[i]->pExpr==pX ){ + int iField; + assert( (pLoop->aLTerm[i]->eOperator & (WO_OR|WO_AND))==0 ); + iField = pLoop->aLTerm[i]->u.x.iField - 1; + if( NEVER(pOrigRhs->a[iField].pExpr==0) ){ + continue; /* Duplicate PK column */ + } + pRhs = sqlite3ExprListAppend(pParse, pRhs, pOrigRhs->a[iField].pExpr); + pOrigRhs->a[iField].pExpr = 0; + if( pRhs ) pRhs->a[pRhs->nExpr-1].u.x.iOrderByCol = iField+1; + if( pOrigLhs ){ + assert( pOrigLhs->a[iField].pExpr!=0 ); + pLhs = sqlite3ExprListAppend(pParse,pLhs,pOrigLhs->a[iField].pExpr); + pOrigLhs->a[iField].pExpr = 0; + } + } + } + sqlite3ExprListDelete(db, pOrigRhs); + if( pOrigLhs ){ + sqlite3ExprListDelete(db, pOrigLhs); + pNew->pLeft->x.pList = pLhs; + } + pSelect->pEList = pRhs; + pSelect->selId = ++pParse->nSelect; /* Req'd for SubrtnSig validity */ + if( pLhs && pLhs->nExpr==1 ){ + /* Take care here not to generate a TK_VECTOR containing only a + ** single value. Since the parser never creates such a vector, some + ** of the subroutines do not handle this case. */ + Expr *p = pLhs->a[0].pExpr; + pLhs->a[0].pExpr = 0; + sqlite3ExprDelete(db, pNew->pLeft); + pNew->pLeft = p; + } + + /* If either the ORDER BY clause or the GROUP BY clause contains + ** references to result-set columns, those references might now be + ** obsolete. So fix them up. + */ + assert( pRhs!=0 || db->mallocFailed ); + if( pRhs ){ + adjustOrderByCol(pSelect->pOrderBy, pRhs); + adjustOrderByCol(pSelect->pGroupBy, pRhs); + for(i=0; i<pRhs->nExpr; i++) pRhs->a[i].u.x.iOrderByCol = 0; + } + +#if 0 + printf("For indexing, change the IN expr:\n"); + sqlite3TreeViewExpr(0, pX, 0); + printf("Into:\n"); + sqlite3TreeViewExpr(0, pNew, 0); +#endif + } + } + return pNew; +} + + +#ifndef SQLITE_OMIT_SUBQUERY +/* +** Generate code for a single X IN (....) term of the WHERE clause. +** +** This is a special-case of codeEqualityTerm() that works for IN operators +** only. It is broken out into a subroutine because this case is +** uncommon and by splitting it off into a subroutine, the common case +** runs faster. +** +** The current value for the constraint is left in register iTarget. +** This routine sets up a loop that will iterate over all values of X. +*/ +static SQLITE_NOINLINE void codeINTerm( + Parse *pParse, /* The parsing context */ + WhereTerm *pTerm, /* The term of the WHERE clause to be coded */ + WhereLevel *pLevel, /* The level of the FROM clause we are working on */ + int iEq, /* Index of the equality term within this level */ + int bRev, /* True for reverse-order IN operations */ + int iTarget /* Attempt to leave results in this register */ +){ + Expr *pX = pTerm->pExpr; + int eType = IN_INDEX_NOOP; + int iTab; + struct InLoop *pIn; + WhereLoop *pLoop = pLevel->pWLoop; + Vdbe *v = pParse->pVdbe; + int i; + int nEq = 0; + int *aiMap = 0; + + if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 + && pLoop->u.btree.pIndex!=0 + && pLoop->u.btree.pIndex->aSortOrder[iEq] + ){ + testcase( iEq==0 ); + testcase( bRev ); + bRev = !bRev; + } + assert( pX->op==TK_IN ); + + for(i=0; i<iEq; i++){ + if( pLoop->aLTerm[i] && pLoop->aLTerm[i]->pExpr==pX ){ + disableTerm(pLevel, pTerm); + return; + } + } + for(i=iEq; i<pLoop->nLTerm; i++){ + assert( pLoop->aLTerm[i]!=0 ); + if( pLoop->aLTerm[i]->pExpr==pX ) nEq++; + } + + iTab = 0; + if( !ExprUseXSelect(pX) || pX->x.pSelect->pEList->nExpr==1 ){ + eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0, 0, &iTab); + }else{ + sqlite3 *db = pParse->db; + Expr *pXMod = removeUnindexableInClauseTerms(pParse, iEq, pLoop, pX); + if( !db->mallocFailed ){ + aiMap = (int*)sqlite3DbMallocZero(db, sizeof(int)*nEq); + eType = sqlite3FindInIndex(pParse, pXMod, IN_INDEX_LOOP, 0, aiMap, &iTab); + } + sqlite3ExprDelete(db, pXMod); + } + + if( eType==IN_INDEX_INDEX_DESC ){ + testcase( bRev ); + bRev = !bRev; + } + sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iTab, 0); + VdbeCoverageIf(v, bRev); + VdbeCoverageIf(v, !bRev); + + assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 ); + pLoop->wsFlags |= WHERE_IN_ABLE; + if( pLevel->u.in.nIn==0 ){ + pLevel->addrNxt = sqlite3VdbeMakeLabel(pParse); + } + if( iEq>0 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)==0 ){ + pLoop->wsFlags |= WHERE_IN_EARLYOUT; + } + + i = pLevel->u.in.nIn; + pLevel->u.in.nIn += nEq; + pLevel->u.in.aInLoop = + sqlite3WhereRealloc(pTerm->pWC->pWInfo, + pLevel->u.in.aInLoop, + sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn); + pIn = pLevel->u.in.aInLoop; + if( pIn ){ + int iMap = 0; /* Index in aiMap[] */ + pIn += i; + for(i=iEq; i<pLoop->nLTerm; i++){ + if( pLoop->aLTerm[i]->pExpr==pX ){ + int iOut = iTarget + i - iEq; + if( eType==IN_INDEX_ROWID ){ + pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iOut); + }else{ + int iCol = aiMap ? aiMap[iMap++] : 0; + pIn->addrInTop = sqlite3VdbeAddOp3(v,OP_Column,iTab, iCol, iOut); + } + sqlite3VdbeAddOp1(v, OP_IsNull, iOut); VdbeCoverage(v); + if( i==iEq ){ + pIn->iCur = iTab; + pIn->eEndLoopOp = bRev ? OP_Prev : OP_Next; + if( iEq>0 ){ + pIn->iBase = iTarget - i; + pIn->nPrefix = i; + }else{ + pIn->nPrefix = 0; + } + }else{ + pIn->eEndLoopOp = OP_Noop; + } + pIn++; + } + } + testcase( iEq>0 + && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)==0 + && (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ); + if( iEq>0 + && (pLoop->wsFlags & (WHERE_IN_SEEKSCAN|WHERE_VIRTUALTABLE))==0 + ){ + sqlite3VdbeAddOp3(v, OP_SeekHit, pLevel->iIdxCur, 0, iEq); + } + }else{ + pLevel->u.in.nIn = 0; + } + sqlite3DbFree(pParse->db, aiMap); +} +#endif + /* ** Generate code for a single equality term of the WHERE clause. An equality -** term can be either X=expr or X IN (...). pTerm is the term to be +** term can be either X=expr or X IN (...). pTerm is the term to be ** coded. ** -** The current value for the constraint is left in register iReg. +** The current value for the constraint is left in a register, the index +** of which is returned. An attempt is made store the result in iTarget but +** this is only guaranteed for TK_ISNULL and TK_IN constraints. If the +** constraint is a TK_EQ or TK_IS, then the current value might be left in +** some other register and it is the caller's responsibility to compensate. ** -** For a constraint of the form X=expr, the expression is evaluated and its -** result is left on the stack. For constraints of the form X IN (...) +** For a constraint of the form X=expr, the expression is evaluated in +** straight-line code. For constraints of the form X IN (...) ** this routine sets up a loop that will iterate over all values of X. */ static int codeEqualityTerm( @@ -353,67 +809,38 @@ static int codeEqualityTerm( int iTarget /* Attempt to leave results in this register */ ){ Expr *pX = pTerm->pExpr; - Vdbe *v = pParse->pVdbe; int iReg; /* Register holding results */ + assert( pLevel->pWLoop->aLTerm[iEq]==pTerm ); assert( iTarget>0 ); if( pX->op==TK_EQ || pX->op==TK_IS ){ iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget); }else if( pX->op==TK_ISNULL ){ iReg = iTarget; - sqlite3VdbeAddOp2(v, OP_Null, 0, iReg); + sqlite3VdbeAddOp2(pParse->pVdbe, OP_Null, 0, iReg); #ifndef SQLITE_OMIT_SUBQUERY }else{ - int eType; - int iTab; - struct InLoop *pIn; - WhereLoop *pLoop = pLevel->pWLoop; - - if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 - && pLoop->u.btree.pIndex!=0 - && pLoop->u.btree.pIndex->aSortOrder[iEq] - ){ - testcase( iEq==0 ); - testcase( bRev ); - bRev = !bRev; - } assert( pX->op==TK_IN ); iReg = iTarget; - eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0); - if( eType==IN_INDEX_INDEX_DESC ){ - testcase( bRev ); - bRev = !bRev; - } - iTab = pX->iTable; - sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iTab, 0); - VdbeCoverageIf(v, bRev); - VdbeCoverageIf(v, !bRev); - assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 ); - pLoop->wsFlags |= WHERE_IN_ABLE; - if( pLevel->u.in.nIn==0 ){ - pLevel->addrNxt = sqlite3VdbeMakeLabel(v); - } - pLevel->u.in.nIn++; - pLevel->u.in.aInLoop = - sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop, - sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn); - pIn = pLevel->u.in.aInLoop; - if( pIn ){ - pIn += pLevel->u.in.nIn - 1; - pIn->iCur = iTab; - if( eType==IN_INDEX_ROWID ){ - pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iReg); - }else{ - pIn->addrInTop = sqlite3VdbeAddOp3(v, OP_Column, iTab, 0, iReg); - } - pIn->eEndLoopOp = bRev ? OP_PrevIfOpen : OP_NextIfOpen; - sqlite3VdbeAddOp1(v, OP_IsNull, iReg); VdbeCoverage(v); - }else{ - pLevel->u.in.nIn = 0; - } + codeINTerm(pParse, pTerm, pLevel, iEq, bRev, iTarget); #endif } - disableTerm(pLevel, pTerm); + + /* As an optimization, try to disable the WHERE clause term that is + ** driving the index as it will always be true. The correct answer is + ** obtained regardless, but we might get the answer with fewer CPU cycles + ** by omitting the term. + ** + ** But do not disable the term unless we are certain that the term is + ** not a transitive constraint. For an example of where that does not + ** work, see https://sqlite.org/forum/forumpost/eb8613976a (2021-05-04) + */ + if( (pLevel->pWLoop->wsFlags & WHERE_TRANSCONS)==0 + || (pTerm->eOperator & WO_EQUIV)==0 + ){ + disableTerm(pLevel, pTerm); + } + return iReg; } @@ -424,7 +851,7 @@ static int codeEqualityTerm( ** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c). ** Suppose the WHERE clause is this: a==5 AND b IN (1,2,3) AND c>5 AND c<10 ** The index has as many as three equality constraints, but in this -** example, the third "c" value is an inequality. So only two +** example, the third "c" value is an inequality. So only two ** constraints are coded. This routine will generate code to evaluate ** a==5 and b IN (1,2,3). The current values for a and b will be stored ** in consecutive registers and the index of the first register is returned. @@ -491,7 +918,7 @@ static int codeAllEqualityTerms( /* Figure out how many memory cells we will need then allocate them. */ regBase = pParse->nMem + 1; - nReg = pLoop->u.btree.nEq + nExtraReg; + nReg = nEq + nExtraReg; pParse->nMem += nReg; zAff = sqlite3DbStrDup(pParse->db,sqlite3IndexAffinityStr(pParse->db,pIdx)); @@ -499,11 +926,13 @@ static int codeAllEqualityTerms( if( nSkip ){ int iIdxCur = pLevel->iIdxCur; + sqlite3VdbeAddOp3(v, OP_Null, 0, regBase, regBase+nSkip-1); sqlite3VdbeAddOp1(v, (bRev?OP_Last:OP_Rewind), iIdxCur); VdbeCoverageIf(v, bRev==0); VdbeCoverageIf(v, bRev!=0); VdbeComment((v, "begin skip-scan on %s", pIdx->zName)); j = sqlite3VdbeAddOp0(v, OP_Goto); + assert( pLevel->addrSkip==0 ); pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLT:OP_SeekGT), iIdxCur, 0, regBase, nSkip); VdbeCoverageIf(v, bRev==0); @@ -514,7 +943,7 @@ static int codeAllEqualityTerms( testcase( pIdx->aiColumn[j]==XN_EXPR ); VdbeComment((v, "%s", explainIndexColumnName(pIdx, j))); } - } + } /* Evaluate the equality constraints */ @@ -523,7 +952,7 @@ static int codeAllEqualityTerms( int r1; pTerm = pLoop->aLTerm[j]; assert( pTerm!=0 ); - /* The following testcase is true for indices with redundant columns. + /* The following testcase is true for indices with redundant columns. ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */ testcase( (pTerm->wtFlags & TERM_CODED)!=0 ); testcase( pTerm->wtFlags & TERM_VIRTUAL ); @@ -533,18 +962,25 @@ static int codeAllEqualityTerms( sqlite3ReleaseTempReg(pParse, regBase); regBase = r1; }else{ - sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j); + sqlite3VdbeAddOp2(v, OP_Copy, r1, regBase+j); } } - testcase( pTerm->eOperator & WO_ISNULL ); - testcase( pTerm->eOperator & WO_IN ); - if( (pTerm->eOperator & (WO_ISNULL|WO_IN))==0 ){ + if( pTerm->eOperator & WO_IN ){ + if( pTerm->pExpr->flags & EP_xIsSelect ){ + /* No affinity ever needs to be (or should be) applied to a value + ** from the RHS of an "? IN (SELECT ...)" expression. The + ** sqlite3FindInIndex() routine has already ensured that the + ** affinity of the comparison has been applied to the value. */ + if( zAff ) zAff[j] = SQLITE_AFF_BLOB; + } + }else if( (pTerm->eOperator & WO_ISNULL)==0 ){ Expr *pRight = pTerm->pExpr->pRight; if( (pTerm->wtFlags & TERM_IS)==0 && sqlite3ExprCanBeNull(pRight) ){ sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->addrBrk); VdbeCoverage(v); } - if( zAff ){ + if( pParse->nErr==0 ){ + assert( pParse->db->mallocFailed==0 ); if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_BLOB ){ zAff[j] = SQLITE_AFF_BLOB; } @@ -560,15 +996,16 @@ static int codeAllEqualityTerms( #ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS /* -** If the most recently coded instruction is a constant range contraint -** that originated from the LIKE optimization, then change the P3 to be -** pLoop->iLikeRepCntr and set P5. +** If the most recently coded instruction is a constant range constraint +** (a string literal) that originated from the LIKE optimization, then +** set P3 and P5 on the OP_String opcode so that the string will be cast +** to a BLOB at appropriate times. ** ** The LIKE optimization trys to evaluate "x LIKE 'abc%'" as a range ** expression: "x>='ABC' AND x<'abd'". But this requires that the range ** scan loop run twice, once for strings and a second time for BLOBs. ** The OP_String opcodes on the second pass convert the upper and lower -** bound string contants to blobs. This routine makes the necessary changes +** bound string constants to blobs. This routine makes the necessary changes ** to the OP_String opcodes for that to happen. ** ** Except, of course, if SQLITE_LIKE_DOESNT_MATCH_BLOBS is defined, then @@ -583,12 +1020,12 @@ static void whereLikeOptimizationStringFixup( if( pTerm->wtFlags & TERM_LIKEOPT ){ VdbeOp *pOp; assert( pLevel->iLikeRepCntr>0 ); - pOp = sqlite3VdbeGetOp(v, -1); + pOp = sqlite3VdbeGetLastOp(v); assert( pOp!=0 ); - assert( pOp->opcode==OP_String8 + assert( pOp->opcode==OP_String8 || pTerm->pWC->pWInfo->pParse->db->mallocFailed ); - pOp->p3 = pLevel->iLikeRepCntr; - pOp->p5 = 1; + pOp->p3 = (int)(pLevel->iLikeRepCntr>>1); /* Register holding counter */ + pOp->p5 = (u8)(pLevel->iLikeRepCntr&1); /* ASC or DESC */ } } #else @@ -618,13 +1055,45 @@ static int codeCursorHintCheckExpr(Walker *pWalker, Expr *pExpr){ assert( pHint->pIdx!=0 ); if( pExpr->op==TK_COLUMN && pExpr->iTable==pHint->iTabCur - && sqlite3ColumnOfIndex(pHint->pIdx, pExpr->iColumn)<0 + && sqlite3TableColumnToIndex(pHint->pIdx, pExpr->iColumn)<0 ){ pWalker->eCode = 1; } return WRC_Continue; } +/* +** Test whether or not expression pExpr, which was part of a WHERE clause, +** should be included in the cursor-hint for a table that is on the rhs +** of a LEFT JOIN. Set Walker.eCode to non-zero before returning if the +** expression is not suitable. +** +** An expression is unsuitable if it might evaluate to non NULL even if +** a TK_COLUMN node that does affect the value of the expression is set +** to NULL. For example: +** +** col IS NULL +** col IS NOT NULL +** coalesce(col, 1) +** CASE WHEN col THEN 0 ELSE 1 END +*/ +static int codeCursorHintIsOrFunction(Walker *pWalker, Expr *pExpr){ + if( pExpr->op==TK_IS + || pExpr->op==TK_ISNULL || pExpr->op==TK_ISNOT + || pExpr->op==TK_NOTNULL || pExpr->op==TK_CASE + ){ + pWalker->eCode = 1; + }else if( pExpr->op==TK_FUNCTION ){ + int d1; + char d2[4]; + if( 0==sqlite3IsLikeFunction(pWalker->pParse->db, pExpr, &d1, d2) ){ + pWalker->eCode = 1; + } + } + + return WRC_Continue; +} + /* ** This function is called on every node of an expression tree used as an @@ -632,43 +1101,41 @@ static int codeCursorHintCheckExpr(Walker *pWalker, Expr *pExpr){ ** that accesses any table other than the one identified by ** CCurHint.iTabCur, then do the following: ** -** 1) allocate a register and code an OP_Column instruction to read +** 1) allocate a register and code an OP_Column instruction to read ** the specified column into the new register, and ** -** 2) transform the expression node to a TK_REGISTER node that reads +** 2) transform the expression node to a TK_REGISTER node that reads ** from the newly populated register. ** -** Also, if the node is a TK_COLUMN that does access the table idenified +** Also, if the node is a TK_COLUMN that does access the table identified ** by pCCurHint.iTabCur, and an index is being used (which we will ** know because CCurHint.pIdx!=0) then transform the TK_COLUMN into ** an access of the index rather than the original table. */ static int codeCursorHintFixExpr(Walker *pWalker, Expr *pExpr){ int rc = WRC_Continue; + int reg; struct CCurHint *pHint = pWalker->u.pCCurHint; if( pExpr->op==TK_COLUMN ){ if( pExpr->iTable!=pHint->iTabCur ){ - Vdbe *v = pWalker->pParse->pVdbe; - int reg = ++pWalker->pParse->nMem; /* Register for column value */ - sqlite3ExprCodeGetColumnOfTable( - v, pExpr->pTab, pExpr->iTable, pExpr->iColumn, reg - ); + reg = ++pWalker->pParse->nMem; /* Register for column value */ + reg = sqlite3ExprCodeTarget(pWalker->pParse, pExpr, reg); pExpr->op = TK_REGISTER; pExpr->iTable = reg; }else if( pHint->pIdx!=0 ){ pExpr->iTable = pHint->iIdxCur; - pExpr->iColumn = sqlite3ColumnOfIndex(pHint->pIdx, pExpr->iColumn); + pExpr->iColumn = sqlite3TableColumnToIndex(pHint->pIdx, pExpr->iColumn); assert( pExpr->iColumn>=0 ); } - }else if( pExpr->op==TK_AGG_FUNCTION ){ - /* An aggregate function in the WHERE clause of a query means this must - ** be a correlated sub-query, and expression pExpr is an aggregate from - ** the parent context. Do not walk the function arguments in this case. - ** - ** todo: It should be possible to replace this node with a TK_REGISTER - ** expression, as the result of the expression must be stored in a - ** register at this point. The same holds for TK_AGG_COLUMN nodes. */ + }else if( pExpr->pAggInfo ){ rc = WRC_Prune; + reg = ++pWalker->pParse->nMem; /* Register for column value */ + reg = sqlite3ExprCodeTarget(pWalker->pParse, pExpr, reg); + pExpr->op = TK_REGISTER; + pExpr->iTable = reg; + }else if( pExpr->op==TK_TRUEFALSE ){ + /* Do not walk disabled expressions. tag-20230504-1 */ + return WRC_Prune; } return rc; } @@ -677,6 +1144,7 @@ static int codeCursorHintFixExpr(Walker *pWalker, Expr *pExpr){ ** Insert an OP_CursorHint instruction if it is appropriate to do so. */ static void codeCursorHint( + SrcItem *pTabItem, /* FROM clause item */ WhereInfo *pWInfo, /* The where clause */ WhereLevel *pLevel, /* Which loop to provide hints for */ WhereTerm *pEndRange /* Hint this end-of-scan boundary term if not NULL */ @@ -703,11 +1171,46 @@ static void codeCursorHint( sWalker.pParse = pParse; sWalker.u.pCCurHint = &sHint; pWC = &pWInfo->sWC; - for(i=0; i<pWC->nTerm; i++){ + for(i=0; i<pWC->nBase; i++){ pTerm = &pWC->a[i]; if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; if( pTerm->prereqAll & pLevel->notReady ) continue; - if( ExprHasProperty(pTerm->pExpr, EP_FromJoin) ) continue; + + /* Any terms specified as part of the ON(...) clause for any LEFT + ** JOIN for which the current table is not the rhs are omitted + ** from the cursor-hint. + ** + ** If this table is the rhs of a LEFT JOIN, "IS" or "IS NULL" terms + ** that were specified as part of the WHERE clause must be excluded. + ** This is to address the following: + ** + ** SELECT ... t1 LEFT JOIN t2 ON (t1.a=t2.b) WHERE t2.c IS NULL; + ** + ** Say there is a single row in t2 that matches (t1.a=t2.b), but its + ** t2.c values is not NULL. If the (t2.c IS NULL) constraint is + ** pushed down to the cursor, this row is filtered out, causing + ** SQLite to synthesize a row of NULL values. Which does match the + ** WHERE clause, and so the query returns a row. Which is incorrect. + ** + ** For the same reason, WHERE terms such as: + ** + ** WHERE 1 = (t2.c IS NULL) + ** + ** are also excluded. See codeCursorHintIsOrFunction() for details. + */ + if( pTabItem->fg.jointype & JT_LEFT ){ + Expr *pExpr = pTerm->pExpr; + if( !ExprHasProperty(pExpr, EP_OuterON) + || pExpr->w.iJoin!=pTabItem->iCursor + ){ + sWalker.eCode = 0; + sWalker.xExprCallback = codeCursorHintIsOrFunction; + sqlite3WalkExpr(&sWalker, pTerm->pExpr); + if( sWalker.eCode ) continue; + } + }else{ + if( ExprHasProperty(pTerm->pExpr, EP_OuterON) ) continue; + } /* All terms in pWLoop->aLTerm[] except pEndRange are used to initialize ** the cursor. These terms are not needed as hints for a pure range @@ -730,37 +1233,45 @@ static void codeCursorHint( } /* If we survive all prior tests, that means this term is worth hinting */ - pExpr = sqlite3ExprAnd(db, pExpr, sqlite3ExprDup(db, pTerm->pExpr, 0)); + pExpr = sqlite3ExprAnd(pParse, pExpr, sqlite3ExprDup(db, pTerm->pExpr, 0)); } if( pExpr!=0 ){ sWalker.xExprCallback = codeCursorHintFixExpr; - sqlite3WalkExpr(&sWalker, pExpr); - sqlite3VdbeAddOp4(v, OP_CursorHint, + if( pParse->nErr==0 ) sqlite3WalkExpr(&sWalker, pExpr); + sqlite3VdbeAddOp4(v, OP_CursorHint, (sHint.pIdx ? sHint.iIdxCur : sHint.iTabCur), 0, 0, (const char*)pExpr, P4_EXPR); } } #else -# define codeCursorHint(A,B,C) /* No-op */ +# define codeCursorHint(A,B,C,D) /* No-op */ #endif /* SQLITE_ENABLE_CURSOR_HINTS */ /* ** Cursor iCur is open on an intkey b-tree (a table). Register iRowid contains ** a rowid value just read from cursor iIdxCur, open on index pIdx. This -** function generates code to do a deferred seek of cursor iCur to the +** function generates code to do a deferred seek of cursor iCur to the ** rowid stored in register iRowid. ** ** Normally, this is just: ** -** OP_Seek $iCur $iRowid +** OP_DeferredSeek $iCur $iRowid +** +** Which causes a seek on $iCur to the row with rowid $iRowid. ** ** However, if the scan currently being coded is a branch of an OR-loop and -** the statement currently being coded is a SELECT, then P3 of the OP_Seek -** is set to iIdxCur and P4 is set to point to an array of integers -** containing one entry for each column of the table cursor iCur is open -** on. For each table column, if the column is the i'th column of the -** index, then the corresponding array entry is set to (i+1). If the column -** does not appear in the index at all, the array entry is set to 0. +** the statement currently being coded is a SELECT, then additional information +** is added that might allow OP_Column to omit the seek and instead do its +** lookup on the index, thus avoiding an expensive seek operation. To +** enable this optimization, the P3 of OP_DeferredSeek is set to iIdxCur +** and P4 is set to an array of integers containing one entry for each column +** in the table. For each table column, if the column is the i'th +** column of the index, then the corresponding array entry is set to (i+1). +** If the column does not appear in the index at all, the array entry is set +** to 0. The OP_Column opcode can check this array to see if the column it +** wants is in the index and if it is, it will substitute the index cursor +** and column number and continue with those new values, rather than seeking +** the table cursor. */ static void codeDeferredSeek( WhereInfo *pWInfo, /* Where clause context */ @@ -773,65 +1284,235 @@ static void codeDeferredSeek( assert( iIdxCur>0 ); assert( pIdx->aiColumn[pIdx->nColumn-1]==-1 ); - - sqlite3VdbeAddOp3(v, OP_Seek, iIdxCur, 0, iCur); - if( (pWInfo->wctrlFlags & WHERE_FORCE_TABLE) + + pWInfo->bDeferredSeek = 1; + sqlite3VdbeAddOp3(v, OP_DeferredSeek, iIdxCur, 0, iCur); + if( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN)) && DbMaskAllZero(sqlite3ParseToplevel(pParse)->writeMask) ){ int i; Table *pTab = pIdx->pTable; - int *ai = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int)*(pTab->nCol+1)); + u32 *ai = (u32*)sqlite3DbMallocZero(pParse->db, sizeof(u32)*(pTab->nCol+1)); if( ai ){ ai[0] = pTab->nCol; for(i=0; i<pIdx->nColumn-1; i++){ + int x1, x2; assert( pIdx->aiColumn[i]<pTab->nCol ); - if( pIdx->aiColumn[i]>=0 ) ai[pIdx->aiColumn[i]+1] = i+1; + x1 = pIdx->aiColumn[i]; + x2 = sqlite3TableColumnToStorage(pTab, x1); + testcase( x1!=x2 ); + if( x1>=0 ) ai[x2+1] = i+1; } sqlite3VdbeChangeP4(v, -1, (char*)ai, P4_INTARRAY); } } } +/* +** If the expression passed as the second argument is a vector, generate +** code to write the first nReg elements of the vector into an array +** of registers starting with iReg. +** +** If the expression is not a vector, then nReg must be passed 1. In +** this case, generate code to evaluate the expression and leave the +** result in register iReg. +*/ +static void codeExprOrVector(Parse *pParse, Expr *p, int iReg, int nReg){ + assert( nReg>0 ); + if( p && sqlite3ExprIsVector(p) ){ +#ifndef SQLITE_OMIT_SUBQUERY + if( ExprUseXSelect(p) ){ + Vdbe *v = pParse->pVdbe; + int iSelect; + assert( p->op==TK_SELECT ); + iSelect = sqlite3CodeSubselect(pParse, p); + sqlite3VdbeAddOp3(v, OP_Copy, iSelect, iReg, nReg-1); + }else +#endif + { + int i; + const ExprList *pList; + assert( ExprUseXList(p) ); + pList = p->x.pList; + assert( nReg<=pList->nExpr ); + for(i=0; i<nReg; i++){ + sqlite3ExprCode(pParse, pList->a[i].pExpr, iReg+i); + } + } + }else{ + assert( nReg==1 || pParse->nErr ); + sqlite3ExprCode(pParse, p, iReg); + } +} + +/* +** The pTruth expression is always true because it is the WHERE clause +** a partial index that is driving a query loop. Look through all of the +** WHERE clause terms on the query, and if any of those terms must be +** true because pTruth is true, then mark those WHERE clause terms as +** coded. +*/ +static void whereApplyPartialIndexConstraints( + Expr *pTruth, + int iTabCur, + WhereClause *pWC +){ + int i; + WhereTerm *pTerm; + while( pTruth->op==TK_AND ){ + whereApplyPartialIndexConstraints(pTruth->pLeft, iTabCur, pWC); + pTruth = pTruth->pRight; + } + for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){ + Expr *pExpr; + if( pTerm->wtFlags & TERM_CODED ) continue; + pExpr = pTerm->pExpr; + if( sqlite3ExprCompare(0, pExpr, pTruth, iTabCur)==0 ){ + pTerm->wtFlags |= TERM_CODED; + } + } +} + +/* +** This routine is called right after An OP_Filter has been generated and +** before the corresponding index search has been performed. This routine +** checks to see if there are additional Bloom filters in inner loops that +** can be checked prior to doing the index lookup. If there are available +** inner-loop Bloom filters, then evaluate those filters now, before the +** index lookup. The idea is that a Bloom filter check is way faster than +** an index lookup, and the Bloom filter might return false, meaning that +** the index lookup can be skipped. +** +** We know that an inner loop uses a Bloom filter because it has the +** WhereLevel.regFilter set. If an inner-loop Bloom filter is checked, +** then clear the WhereLevel.regFilter value to prevent the Bloom filter +** from being checked a second time when the inner loop is evaluated. +*/ +static SQLITE_NOINLINE void filterPullDown( + Parse *pParse, /* Parsing context */ + WhereInfo *pWInfo, /* Complete information about the WHERE clause */ + int iLevel, /* Which level of pWInfo->a[] should be coded */ + int addrNxt, /* Jump here to bypass inner loops */ + Bitmask notReady /* Loops that are not ready */ +){ + int saved_addrBrk; + while( ++iLevel < pWInfo->nLevel ){ + WhereLevel *pLevel = &pWInfo->a[iLevel]; + WhereLoop *pLoop = pLevel->pWLoop; + if( pLevel->regFilter==0 ) continue; + if( pLevel->pWLoop->nSkip ) continue; + /* ,--- Because sqlite3ConstructBloomFilter() has will not have set + ** vvvvv--' pLevel->regFilter if this were true. */ + if( NEVER(pLoop->prereq & notReady) ) continue; + saved_addrBrk = pLevel->addrBrk; + pLevel->addrBrk = addrNxt; + if( pLoop->wsFlags & WHERE_IPK ){ + WhereTerm *pTerm = pLoop->aLTerm[0]; + int regRowid; + assert( pTerm!=0 ); + assert( pTerm->pExpr!=0 ); + testcase( pTerm->wtFlags & TERM_VIRTUAL ); + regRowid = sqlite3GetTempReg(pParse); + regRowid = codeEqualityTerm(pParse, pTerm, pLevel, 0, 0, regRowid); + sqlite3VdbeAddOp2(pParse->pVdbe, OP_MustBeInt, regRowid, addrNxt); + VdbeCoverage(pParse->pVdbe); + sqlite3VdbeAddOp4Int(pParse->pVdbe, OP_Filter, pLevel->regFilter, + addrNxt, regRowid, 1); + VdbeCoverage(pParse->pVdbe); + }else{ + u16 nEq = pLoop->u.btree.nEq; + int r1; + char *zStartAff; + + assert( pLoop->wsFlags & WHERE_INDEXED ); + assert( (pLoop->wsFlags & WHERE_COLUMN_IN)==0 ); + r1 = codeAllEqualityTerms(pParse,pLevel,0,0,&zStartAff); + codeApplyAffinity(pParse, r1, nEq, zStartAff); + sqlite3DbFree(pParse->db, zStartAff); + sqlite3VdbeAddOp4Int(pParse->pVdbe, OP_Filter, pLevel->regFilter, + addrNxt, r1, nEq); + VdbeCoverage(pParse->pVdbe); + } + pLevel->regFilter = 0; + pLevel->addrBrk = saved_addrBrk; + } +} + +/* +** Loop pLoop is a WHERE_INDEXED level that uses at least one IN(...) +** operator. Return true if level pLoop is guaranteed to visit only one +** row for each key generated for the index. +*/ +static int whereLoopIsOneRow(WhereLoop *pLoop){ + if( pLoop->u.btree.pIndex->onError + && pLoop->nSkip==0 + && pLoop->u.btree.nEq==pLoop->u.btree.pIndex->nKeyCol + ){ + int ii; + for(ii=0; ii<pLoop->u.btree.nEq; ii++){ + if( pLoop->aLTerm[ii]->eOperator & (WO_IS|WO_ISNULL) ){ + return 0; + } + } + return 1; + } + return 0; +} + /* ** Generate code for the start of the iLevel-th loop in the WHERE clause ** implementation described by pWInfo. */ Bitmask sqlite3WhereCodeOneLoopStart( + Parse *pParse, /* Parsing context */ + Vdbe *v, /* Prepared statement under construction */ WhereInfo *pWInfo, /* Complete information about the WHERE clause */ int iLevel, /* Which level of pWInfo->a[] should be coded */ + WhereLevel *pLevel, /* The current level pointer */ Bitmask notReady /* Which tables are currently available */ ){ int j, k; /* Loop counters */ int iCur; /* The VDBE cursor for the table */ int addrNxt; /* Where to jump to continue with the next IN case */ - int omitTable; /* True if we use the index only */ int bRev; /* True if we need to scan in reverse order */ - WhereLevel *pLevel; /* The where level to be coded */ WhereLoop *pLoop; /* The WhereLoop object being coded */ WhereClause *pWC; /* Decomposition of the entire WHERE clause */ WhereTerm *pTerm; /* A WHERE clause term */ - Parse *pParse; /* Parsing context */ sqlite3 *db; /* Database connection */ - Vdbe *v; /* The prepared stmt under constructions */ - struct SrcList_item *pTabItem; /* FROM clause term being coded */ + SrcItem *pTabItem; /* FROM clause term being coded */ int addrBrk; /* Jump here to break out of the loop */ int addrCont; /* Jump here to continue with next cycle */ int iRowidReg = 0; /* Rowid is stored in this register, if not zero */ int iReleaseReg = 0; /* Temp register to free before returning */ + Index *pIdx = 0; /* Index used by loop (if any) */ + int iLoop; /* Iteration of constraint generator loop */ - pParse = pWInfo->pParse; - v = pParse->pVdbe; pWC = &pWInfo->sWC; db = pParse->db; - pLevel = &pWInfo->a[iLevel]; pLoop = pLevel->pWLoop; pTabItem = &pWInfo->pTabList->a[pLevel->iFrom]; iCur = pTabItem->iCursor; pLevel->notReady = notReady & ~sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur); bRev = (pWInfo->revMask>>iLevel)&1; - omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0 - && (pWInfo->wctrlFlags & WHERE_FORCE_TABLE)==0; - VdbeModuleComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName)); + VdbeModuleComment((v, "Begin WHERE-loop%d: %s", + iLevel, pTabItem->pSTab->zName)); +#if WHERETRACE_ENABLED /* 0x4001 */ + if( sqlite3WhereTrace & 0x1 ){ + sqlite3DebugPrintf("Coding level %d of %d: notReady=%llx iFrom=%d\n", + iLevel, pWInfo->nLevel, (u64)notReady, pLevel->iFrom); + if( sqlite3WhereTrace & 0x1000 ){ + sqlite3WhereLoopPrint(pLoop, pWC); + } + } + if( (sqlite3WhereTrace & 0x4001)==0x4001 ){ + if( iLevel==0 ){ + sqlite3DebugPrintf("WHERE clause being coded:\n"); + sqlite3TreeViewExpr(0, pWInfo->pWhere, 0); + } + sqlite3DebugPrintf("All WHERE-clause terms before coding:\n"); + sqlite3WhereClausePrint(pWC); + } +#endif /* Create labels for the "break" and "continue" instructions ** for the current loop. Jump to addrBrk to break out of a loop. @@ -843,26 +1524,33 @@ Bitmask sqlite3WhereCodeOneLoopStart( ** there are no IN operators in the constraints, the "addrNxt" label ** is the same as "addrBrk". */ - addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(v); - addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(v); + addrBrk = pLevel->addrNxt = pLevel->addrBrk; + addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(pParse); /* If this is the right table of a LEFT OUTER JOIN, allocate and ** initialize a memory cell that records if this table matches any ** row of the left table of the join. */ + assert( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN)) + || pLevel->iFrom>0 || (pTabItem[0].fg.jointype & JT_LEFT)==0 + ); if( pLevel->iFrom>0 && (pTabItem[0].fg.jointype & JT_LEFT)!=0 ){ pLevel->iLeftJoin = ++pParse->nMem; sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin); - VdbeComment((v, "init LEFT JOIN no-match flag")); + VdbeComment((v, "init LEFT JOIN match flag")); } /* Special case of a FROM clause subquery implemented as a co-routine */ if( pTabItem->fg.viaCoroutine ){ - int regYield = pTabItem->regReturn; - sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub); + int regYield; + Subquery *pSubq; + assert( pTabItem->fg.isSubquery && pTabItem->u4.pSubq!=0 ); + pSubq = pTabItem->u4.pSubq; + regYield = pSubq->regReturn; + sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pSubq->addrFillSub); pLevel->p2 = sqlite3VdbeAddOp2(v, OP_Yield, regYield, addrBrk); VdbeCoverage(v); - VdbeComment((v, "next row of \"%s\"", pTabItem->pTab->zName)); + VdbeComment((v, "next row of %s", pTabItem->pSTab->zName)); pLevel->op = OP_Goto; }else @@ -875,37 +1563,121 @@ Bitmask sqlite3WhereCodeOneLoopStart( int addrNotFound; int nConstraint = pLoop->nLTerm; - sqlite3ExprCachePush(pParse); iReg = sqlite3GetTempRange(pParse, nConstraint+2); addrNotFound = pLevel->addrBrk; for(j=0; j<nConstraint; j++){ int iTarget = iReg+j+2; pTerm = pLoop->aLTerm[j]; - if( pTerm==0 ) continue; + if( NEVER(pTerm==0) ) continue; if( pTerm->eOperator & WO_IN ){ - codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget); - addrNotFound = pLevel->addrNxt; + if( SMASKBIT32(j) & pLoop->u.vtab.mHandleIn ){ + int iTab = pParse->nTab++; + int iCache = ++pParse->nMem; + sqlite3CodeRhsOfIN(pParse, pTerm->pExpr, iTab); + sqlite3VdbeAddOp3(v, OP_VInitIn, iTab, iTarget, iCache); + }else{ + codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget); + addrNotFound = pLevel->addrNxt; + } }else{ - sqlite3ExprCode(pParse, pTerm->pExpr->pRight, iTarget); + Expr *pRight = pTerm->pExpr->pRight; + codeExprOrVector(pParse, pRight, iTarget, 1); + if( pTerm->eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET + && pLoop->u.vtab.bOmitOffset + ){ + assert( pTerm->eOperator==WO_AUX ); + assert( pWInfo->pSelect!=0 ); + assert( pWInfo->pSelect->iOffset>0 ); + sqlite3VdbeAddOp2(v, OP_Integer, 0, pWInfo->pSelect->iOffset); + VdbeComment((v,"Zero OFFSET counter")); + } } } sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg); sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1); + /* The instruction immediately prior to OP_VFilter must be an OP_Integer + ** that sets the "argc" value for xVFilter. This is necessary for + ** resolveP2() to work correctly. See tag-20250207a. */ sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg, pLoop->u.vtab.idxStr, - pLoop->u.vtab.needFree ? P4_MPRINTF : P4_STATIC); + pLoop->u.vtab.needFree ? P4_DYNAMIC : P4_STATIC); VdbeCoverage(v); pLoop->u.vtab.needFree = 0; - for(j=0; j<nConstraint && j<16; j++){ - if( (pLoop->u.vtab.omitMask>>j)&1 ){ - disableTerm(pLevel, pLoop->aLTerm[j]); - } - } + /* An OOM inside of AddOp4(OP_VFilter) instruction above might have freed + ** the u.vtab.idxStr. NULL it out to prevent a use-after-free */ + if( db->mallocFailed ) pLoop->u.vtab.idxStr = 0; pLevel->p1 = iCur; pLevel->op = pWInfo->eOnePass ? OP_Noop : OP_VNext; pLevel->p2 = sqlite3VdbeCurrentAddr(v); - sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2); - sqlite3ExprCachePop(pParse); + assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 ); + + for(j=0; j<nConstraint; j++){ + pTerm = pLoop->aLTerm[j]; + if( j<16 && (pLoop->u.vtab.omitMask>>j)&1 ){ + disableTerm(pLevel, pTerm); + continue; + } + if( (pTerm->eOperator & WO_IN)!=0 + && (SMASKBIT32(j) & pLoop->u.vtab.mHandleIn)==0 + && !db->mallocFailed + ){ + Expr *pCompare; /* The comparison operator */ + Expr *pRight; /* RHS of the comparison */ + VdbeOp *pOp; /* Opcode to access the value of the IN constraint */ + int iIn; /* IN loop corresponding to the j-th constraint */ + + /* Reload the constraint value into reg[iReg+j+2]. The same value + ** was loaded into the same register prior to the OP_VFilter, but + ** the xFilter implementation might have changed the datatype or + ** encoding of the value in the register, so it *must* be reloaded. + */ + for(iIn=0; ALWAYS(iIn<pLevel->u.in.nIn); iIn++){ + pOp = sqlite3VdbeGetOp(v, pLevel->u.in.aInLoop[iIn].addrInTop); + if( (pOp->opcode==OP_Column && pOp->p3==iReg+j+2) + || (pOp->opcode==OP_Rowid && pOp->p2==iReg+j+2) + ){ + testcase( pOp->opcode==OP_Rowid ); + sqlite3VdbeAddOp3(v, pOp->opcode, pOp->p1, pOp->p2, pOp->p3); + break; + } + } + + /* Generate code that will continue to the next row if + ** the IN constraint is not satisfied + */ + pCompare = sqlite3PExpr(pParse, TK_EQ, 0, 0); + if( !db->mallocFailed ){ + int iFld = pTerm->u.x.iField; + Expr *pLeft = pTerm->pExpr->pLeft; + assert( pLeft!=0 ); + if( iFld>0 ){ + assert( pLeft->op==TK_VECTOR ); + assert( ExprUseXList(pLeft) ); + assert( iFld<=pLeft->x.pList->nExpr ); + pCompare->pLeft = pLeft->x.pList->a[iFld-1].pExpr; + }else{ + pCompare->pLeft = pLeft; + } + pCompare->pRight = pRight = sqlite3Expr(db, TK_REGISTER, 0); + if( pRight ){ + pRight->iTable = iReg+j+2; + sqlite3ExprIfFalse( + pParse, pCompare, pLevel->addrCont, SQLITE_JUMPIFNULL + ); + } + pCompare->pLeft = 0; + } + sqlite3ExprDelete(db, pCompare); + } + } + + /* These registers need to be preserved in case there is an IN operator + ** loop. So we could deallocate the registers here (and potentially + ** reuse them later) if (pLoop->wsFlags & WHERE_IN_ABLE)==0. But it seems + ** simpler and safer to simply not reuse the registers. + ** + ** sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2); + */ }else #endif /* SQLITE_OMIT_VIRTUALTABLE */ @@ -921,18 +1693,21 @@ Bitmask sqlite3WhereCodeOneLoopStart( pTerm = pLoop->aLTerm[0]; assert( pTerm!=0 ); assert( pTerm->pExpr!=0 ); - assert( omitTable==0 ); testcase( pTerm->wtFlags & TERM_VIRTUAL ); iReleaseReg = ++pParse->nMem; iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg); if( iRowidReg!=iReleaseReg ) sqlite3ReleaseTempReg(pParse, iReleaseReg); addrNxt = pLevel->addrNxt; - sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt); VdbeCoverage(v); - sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addrNxt, iRowidReg); + if( pLevel->regFilter ){ + sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt); + VdbeCoverage(v); + sqlite3VdbeAddOp4Int(v, OP_Filter, pLevel->regFilter, addrNxt, + iRowidReg, 1); + VdbeCoverage(v); + filterPullDown(pParse, pWInfo, iLevel, addrNxt, notReady); + } + sqlite3VdbeAddOp3(v, OP_SeekRowid, iCur, addrNxt, iRowidReg); VdbeCoverage(v); - sqlite3ExprCacheAffinityChange(pParse, iRowidReg, 1); - sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); - VdbeComment((v, "pk")); pLevel->op = OP_Noop; }else if( (pLoop->wsFlags & WHERE_IPK)!=0 && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0 @@ -944,7 +1719,6 @@ Bitmask sqlite3WhereCodeOneLoopStart( int memEndValue = 0; WhereTerm *pStart, *pEnd; - assert( omitTable==0 ); j = 0; pStart = pEnd = 0; if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++]; @@ -955,12 +1729,13 @@ Bitmask sqlite3WhereCodeOneLoopStart( pStart = pEnd; pEnd = pTerm; } - codeCursorHint(pWInfo, pLevel, pEnd); + codeCursorHint(pTabItem, pWInfo, pLevel, pEnd); if( pStart ){ Expr *pX; /* The expression that defines the start bound */ int r1, rTemp; /* Registers for holding the start boundary */ + int op; /* Cursor seek operation */ - /* The following constant maps TK_xx codes into corresponding + /* The following constant maps TK_xx codes into corresponding ** seek opcodes. It depends on a particular ordering of TK_xx */ const u8 aMoveOp[] = { @@ -971,25 +1746,39 @@ Bitmask sqlite3WhereCodeOneLoopStart( }; assert( TK_LE==TK_GT+1 ); /* Make sure the ordering.. */ assert( TK_LT==TK_GT+2 ); /* ... of the TK_xx values... */ - assert( TK_GE==TK_GT+3 ); /* ... is correcct. */ + assert( TK_GE==TK_GT+3 ); /* ... is correct. */ assert( (pStart->wtFlags & TERM_VNULL)==0 ); testcase( pStart->wtFlags & TERM_VIRTUAL ); pX = pStart->pExpr; assert( pX!=0 ); testcase( pStart->leftCursor!=iCur ); /* transitive constraints */ - r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp); - sqlite3VdbeAddOp3(v, aMoveOp[pX->op-TK_GT], iCur, addrBrk, r1); + if( sqlite3ExprIsVector(pX->pRight) ){ + r1 = rTemp = sqlite3GetTempReg(pParse); + codeExprOrVector(pParse, pX->pRight, r1, 1); + testcase( pX->op==TK_GT ); + testcase( pX->op==TK_GE ); + testcase( pX->op==TK_LT ); + testcase( pX->op==TK_LE ); + op = aMoveOp[((pX->op - TK_GT - 1) & 0x3) | 0x1]; + assert( pX->op!=TK_GT || op==OP_SeekGE ); + assert( pX->op!=TK_GE || op==OP_SeekGE ); + assert( pX->op!=TK_LT || op==OP_SeekLE ); + assert( pX->op!=TK_LE || op==OP_SeekLE ); + }else{ + r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp); + disableTerm(pLevel, pStart); + op = aMoveOp[(pX->op - TK_GT)]; + } + sqlite3VdbeAddOp3(v, op, iCur, addrBrk, r1); VdbeComment((v, "pk")); VdbeCoverageIf(v, pX->op==TK_GT); VdbeCoverageIf(v, pX->op==TK_LE); VdbeCoverageIf(v, pX->op==TK_LT); VdbeCoverageIf(v, pX->op==TK_GE); - sqlite3ExprCacheAffinityChange(pParse, r1, 1); sqlite3ReleaseTempReg(pParse, rTemp); - disableTerm(pLevel, pStart); }else{ - sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, addrBrk); + sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, pLevel->addrHalt); VdbeCoverageIf(v, bRev==0); VdbeCoverageIf(v, bRev!=0); } @@ -1001,13 +1790,17 @@ Bitmask sqlite3WhereCodeOneLoopStart( testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */ testcase( pEnd->wtFlags & TERM_VIRTUAL ); memEndValue = ++pParse->nMem; - sqlite3ExprCode(pParse, pX->pRight, memEndValue); - if( pX->op==TK_LT || pX->op==TK_GT ){ + codeExprOrVector(pParse, pX->pRight, memEndValue, 1); + if( 0==sqlite3ExprIsVector(pX->pRight) + && (pX->op==TK_LT || pX->op==TK_GT) + ){ testOp = bRev ? OP_Le : OP_Ge; }else{ testOp = bRev ? OP_Lt : OP_Gt; } - disableTerm(pLevel, pEnd); + if( 0==sqlite3ExprIsVector(pX->pRight) ){ + disableTerm(pLevel, pEnd); + } } start = sqlite3VdbeCurrentAddr(v); pLevel->op = bRev ? OP_Prev : OP_Next; @@ -1017,7 +1810,6 @@ Bitmask sqlite3WhereCodeOneLoopStart( if( testOp!=OP_Noop ){ iRowidReg = ++pParse->nMem; sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg); - sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg); VdbeCoverageIf(v, testOp==OP_Le); VdbeCoverageIf(v, testOp==OP_Lt); @@ -1026,37 +1818,37 @@ Bitmask sqlite3WhereCodeOneLoopStart( sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL); } }else if( pLoop->wsFlags & WHERE_INDEXED ){ - /* Case 4: A scan using an index. + /* Case 4: Search using an index. ** - ** The WHERE clause may contain zero or more equality - ** terms ("==" or "IN" operators) that refer to the N - ** left-most columns of the index. It may also contain - ** inequality constraints (>, <, >= or <=) on the indexed - ** column that immediately follows the N equalities. Only - ** the right-most column can be an inequality - the rest must - ** use the "==" and "IN" operators. For example, if the - ** index is on (x,y,z), then the following clauses are all - ** optimized: + ** The WHERE clause may contain zero or more equality + ** terms ("==" or "IN" or "IS" operators) that refer to the N + ** left-most columns of the index. It may also contain + ** inequality constraints (>, <, >= or <=) on the indexed + ** column that immediately follows the N equalities. Only + ** the right-most column can be an inequality - the rest must + ** use the "==", "IN", or "IS" operators. For example, if the + ** index is on (x,y,z), then the following clauses are all + ** optimized: ** - ** x=5 - ** x=5 AND y=10 - ** x=5 AND y<10 - ** x=5 AND y>5 AND y<10 - ** x=5 AND y=5 AND z<=10 + ** x=5 + ** x=5 AND y=10 + ** x=5 AND y<10 + ** x=5 AND y>5 AND y<10 + ** x=5 AND y=5 AND z<=10 ** - ** The z<10 term of the following cannot be used, only - ** the x=5 term: + ** The z<10 term of the following cannot be used, only + ** the x=5 term: ** - ** x=5 AND z<10 + ** x=5 AND z<10 ** - ** N may be zero if there are inequality constraints. - ** If there are no inequality constraints, then N is at - ** least one. + ** N may be zero if there are inequality constraints. + ** If there are no inequality constraints, then N is at + ** least one. ** - ** This case is also used when there are no WHERE clause - ** constraints but an index is selected anyway, in order - ** to force the output order to conform to an ORDER BY. - */ + ** This case is also used when there are no WHERE clause + ** constraints but an index is selected anyway, in order + ** to force the output order to conform to an ORDER BY. + */ static const u8 aStartOp[] = { 0, 0, @@ -1074,6 +1866,8 @@ Bitmask sqlite3WhereCodeOneLoopStart( OP_IdxLT, /* 3: (end_constraints && bRev && endEq) */ }; u16 nEq = pLoop->u.btree.nEq; /* Number of == or IN terms */ + u16 nBtm = pLoop->u.btree.nBtm; /* Length of BTM vector */ + u16 nTop = pLoop->u.btree.nTop; /* Length of TOP vector */ int regBase; /* Base register holding constraint values */ WhereTerm *pRangeStart = 0; /* Inequality constraint at range start */ WhereTerm *pRangeEnd = 0; /* Inequality constraint at range end */ @@ -1081,96 +1875,110 @@ Bitmask sqlite3WhereCodeOneLoopStart( int endEq; /* True if range end uses ==, >= or <= */ int start_constraints; /* Start of range is constrained */ int nConstraint; /* Number of constraint terms */ - Index *pIdx; /* The index we will be using */ int iIdxCur; /* The VDBE cursor for the index */ int nExtraReg = 0; /* Number of extra registers needed */ int op; /* Instruction opcode */ char *zStartAff; /* Affinity for start of range constraint */ - char cEndAff = 0; /* Affinity for end of range constraint */ + char *zEndAff = 0; /* Affinity for end of range constraint */ u8 bSeekPastNull = 0; /* True to seek past initial nulls */ u8 bStopAtNull = 0; /* Add condition to terminate at NULLs */ + int omitTable; /* True if we use the index only */ + int regBignull = 0; /* big-null flag register */ + int addrSeekScan = 0; /* Opcode of the OP_SeekScan, if any */ pIdx = pLoop->u.btree.pIndex; iIdxCur = pLevel->iIdxCur; assert( nEq>=pLoop->nSkip ); - /* If this loop satisfies a sort order (pOrderBy) request that - ** was passed to this function to implement a "SELECT min(x) ..." - ** query, then the caller will only allow the loop to run for - ** a single iteration. This means that the first row returned - ** should not have a NULL value stored in 'x'. If column 'x' is - ** the first one after the nEq equality constraints in the index, - ** this requires some special handling. - */ - assert( pWInfo->pOrderBy==0 - || pWInfo->pOrderBy->nExpr==1 - || (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0 ); - if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0 - && pWInfo->nOBSat>0 - && (pIdx->nKeyCol>nEq) - ){ - assert( pLoop->nSkip==0 ); - bSeekPastNull = 1; - nExtraReg = 1; - } - - /* Find any inequality constraint terms for the start and end - ** of the range. + /* Find any inequality constraint terms for the start and end + ** of the range. */ j = nEq; if( pLoop->wsFlags & WHERE_BTM_LIMIT ){ pRangeStart = pLoop->aLTerm[j++]; - nExtraReg = 1; + nExtraReg = MAX(nExtraReg, pLoop->u.btree.nBtm); /* Like optimization range constraints always occur in pairs */ - assert( (pRangeStart->wtFlags & TERM_LIKEOPT)==0 || + assert( (pRangeStart->wtFlags & TERM_LIKEOPT)==0 || (pLoop->wsFlags & WHERE_TOP_LIMIT)!=0 ); } if( pLoop->wsFlags & WHERE_TOP_LIMIT ){ pRangeEnd = pLoop->aLTerm[j++]; - nExtraReg = 1; + nExtraReg = MAX(nExtraReg, pLoop->u.btree.nTop); #ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS if( (pRangeEnd->wtFlags & TERM_LIKEOPT)!=0 ){ assert( pRangeStart!=0 ); /* LIKE opt constraints */ assert( pRangeStart->wtFlags & TERM_LIKEOPT ); /* occur in pairs */ - pLevel->iLikeRepCntr = ++pParse->nMem; - testcase( bRev ); - testcase( pIdx->aSortOrder[nEq]==SQLITE_SO_DESC ); - sqlite3VdbeAddOp2(v, OP_Integer, - bRev ^ (pIdx->aSortOrder[nEq]==SQLITE_SO_DESC), - pLevel->iLikeRepCntr); + pLevel->iLikeRepCntr = (u32)++pParse->nMem; + sqlite3VdbeAddOp2(v, OP_Integer, 1, (int)pLevel->iLikeRepCntr); VdbeComment((v, "LIKE loop counter")); pLevel->addrLikeRep = sqlite3VdbeCurrentAddr(v); + /* iLikeRepCntr actually stores 2x the counter register number. The + ** bottom bit indicates whether the search order is ASC or DESC. */ + testcase( bRev ); + testcase( pIdx->aSortOrder[nEq]==SQLITE_SO_DESC ); + assert( (bRev & ~1)==0 ); + pLevel->iLikeRepCntr <<=1; + pLevel->iLikeRepCntr |= bRev ^ (pIdx->aSortOrder[nEq]==SQLITE_SO_DESC); } #endif - if( pRangeStart==0 - && (j = pIdx->aiColumn[nEq])>=0 - && pIdx->pTable->aCol[j].notNull==0 - ){ - bSeekPastNull = 1; + if( pRangeStart==0 ){ + j = pIdx->aiColumn[nEq]; + if( (j>=0 && pIdx->pTable->aCol[j].notNull==0) || j==XN_EXPR ){ + bSeekPastNull = 1; + } } } assert( pRangeEnd==0 || (pRangeEnd->wtFlags & TERM_VNULL)==0 ); + /* If the WHERE_BIGNULL_SORT flag is set, then index column nEq uses + ** a non-default "big-null" sort (either ASC NULLS LAST or DESC NULLS + ** FIRST). In both cases separate ordered scans are made of those + ** index entries for which the column is null and for those for which + ** it is not. For an ASC sort, the non-NULL entries are scanned first. + ** For DESC, NULL entries are scanned first. + */ + if( (pLoop->wsFlags & (WHERE_TOP_LIMIT|WHERE_BTM_LIMIT))==0 + && (pLoop->wsFlags & WHERE_BIGNULL_SORT)!=0 + ){ + assert( bSeekPastNull==0 && nExtraReg==0 && nBtm==0 && nTop==0 ); + assert( pRangeEnd==0 && pRangeStart==0 ); + testcase( pLoop->nSkip>0 ); + nExtraReg = 1; + bSeekPastNull = 1; + pLevel->regBignull = regBignull = ++pParse->nMem; + if( pLevel->iLeftJoin ){ + sqlite3VdbeAddOp2(v, OP_Integer, 0, regBignull); + } + pLevel->addrBignull = sqlite3VdbeMakeLabel(pParse); + } + /* If we are doing a reverse order scan on an ascending index, or - ** a forward order scan on a descending index, interchange the + ** a forward order scan on a descending index, interchange the ** start and end terms (pRangeStart and pRangeEnd). */ - if( (nEq<pIdx->nKeyCol && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC)) - || (bRev && pIdx->nKeyCol==nEq) - ){ + if( (nEq<pIdx->nColumn && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC)) ){ SWAP(WhereTerm *, pRangeEnd, pRangeStart); SWAP(u8, bSeekPastNull, bStopAtNull); + SWAP(u8, nBtm, nTop); + } + + if( iLevel>0 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)!=0 ){ + /* In case OP_SeekScan is used, ensure that the index cursor does not + ** point to a valid row for the first iteration of this loop. */ + sqlite3VdbeAddOp1(v, OP_NullRow, iIdxCur); } /* Generate code to evaluate all constraint terms using == or IN ** and store the values of those terms in an array of registers ** starting at regBase. */ - codeCursorHint(pWInfo, pLevel, pRangeEnd); + codeCursorHint(pTabItem, pWInfo, pLevel, pRangeEnd); regBase = codeAllEqualityTerms(pParse,pLevel,bRev,nExtraReg,&zStartAff); assert( zStartAff==0 || sqlite3Strlen30(zStartAff)>=nEq ); - if( zStartAff ) cEndAff = zStartAff[nEq]; - addrNxt = pLevel->addrNxt; + if( zStartAff && nTop ){ + zEndAff = sqlite3DbStrDup(db, &zStartAff[nEq]); + } + addrNxt = (regBignull ? pLevel->addrBignull : pLevel->addrNxt); testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 ); testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 ); @@ -1184,7 +1992,7 @@ Bitmask sqlite3WhereCodeOneLoopStart( nConstraint = nEq; if( pRangeStart ){ Expr *pRight = pRangeStart->pExpr->pRight; - sqlite3ExprCode(pParse, pRight, regBase+nEq); + codeExprOrVector(pParse, pRight, regBase+nEq, nBtm); whereLikeOptimizationStringFixup(v, pLevel, pRangeStart); if( (pRangeStart->wtFlags & TERM_VNULL)==0 && sqlite3ExprCanBeNull(pRight) @@ -1193,44 +2001,100 @@ Bitmask sqlite3WhereCodeOneLoopStart( VdbeCoverage(v); } if( zStartAff ){ - if( sqlite3CompareAffinity(pRight, zStartAff[nEq])==SQLITE_AFF_BLOB){ - /* Since the comparison is to be performed with no conversions - ** applied to the operands, set the affinity to apply to pRight to - ** SQLITE_AFF_BLOB. */ - zStartAff[nEq] = SQLITE_AFF_BLOB; - } - if( sqlite3ExprNeedsNoAffinityChange(pRight, zStartAff[nEq]) ){ - zStartAff[nEq] = SQLITE_AFF_BLOB; - } - } - nConstraint++; + updateRangeAffinityStr(pRight, nBtm, &zStartAff[nEq]); + } + nConstraint += nBtm; testcase( pRangeStart->wtFlags & TERM_VIRTUAL ); + if( sqlite3ExprIsVector(pRight)==0 ){ + disableTerm(pLevel, pRangeStart); + }else{ + startEq = 1; + } + bSeekPastNull = 0; }else if( bSeekPastNull ){ + startEq = 0; sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq); + start_constraints = 1; nConstraint++; - startEq = 0; + }else if( regBignull ){ + sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq); start_constraints = 1; + nConstraint++; } codeApplyAffinity(pParse, regBase, nConstraint - bSeekPastNull, zStartAff); - op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev]; - assert( op!=0 ); - sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint); - VdbeCoverage(v); - VdbeCoverageIf(v, op==OP_Rewind); testcase( op==OP_Rewind ); - VdbeCoverageIf(v, op==OP_Last); testcase( op==OP_Last ); - VdbeCoverageIf(v, op==OP_SeekGT); testcase( op==OP_SeekGT ); - VdbeCoverageIf(v, op==OP_SeekGE); testcase( op==OP_SeekGE ); - VdbeCoverageIf(v, op==OP_SeekLE); testcase( op==OP_SeekLE ); - VdbeCoverageIf(v, op==OP_SeekLT); testcase( op==OP_SeekLT ); + if( pLoop->nSkip>0 && nConstraint==pLoop->nSkip ){ + /* The skip-scan logic inside the call to codeAllEqualityConstraints() + ** above has already left the cursor sitting on the correct row, + ** so no further seeking is needed */ + }else{ + if( regBignull ){ + sqlite3VdbeAddOp2(v, OP_Integer, 1, regBignull); + VdbeComment((v, "NULL-scan pass ctr")); + } + if( pLevel->regFilter ){ + sqlite3VdbeAddOp4Int(v, OP_Filter, pLevel->regFilter, addrNxt, + regBase, nEq); + VdbeCoverage(v); + filterPullDown(pParse, pWInfo, iLevel, addrNxt, notReady); + } + + op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev]; + assert( op!=0 ); + if( (pLoop->wsFlags & WHERE_IN_SEEKSCAN)!=0 && op==OP_SeekGE ){ + assert( regBignull==0 ); + /* TUNING: The OP_SeekScan opcode seeks to reduce the number + ** of expensive seek operations by replacing a single seek with + ** 1 or more step operations. The question is, how many steps + ** should we try before giving up and going with a seek. The cost + ** of a seek is proportional to the logarithm of the of the number + ** of entries in the tree, so basing the number of steps to try + ** on the estimated number of rows in the btree seems like a good + ** guess. */ + addrSeekScan = sqlite3VdbeAddOp1(v, OP_SeekScan, + (pIdx->aiRowLogEst[0]+9)/10); + if( pRangeStart || pRangeEnd ){ + sqlite3VdbeChangeP5(v, 1); + sqlite3VdbeChangeP2(v, addrSeekScan, sqlite3VdbeCurrentAddr(v)+1); + addrSeekScan = 0; + } + VdbeCoverage(v); + } + sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint); + VdbeCoverage(v); + VdbeCoverageIf(v, op==OP_Rewind); testcase( op==OP_Rewind ); + VdbeCoverageIf(v, op==OP_Last); testcase( op==OP_Last ); + VdbeCoverageIf(v, op==OP_SeekGT); testcase( op==OP_SeekGT ); + VdbeCoverageIf(v, op==OP_SeekGE); testcase( op==OP_SeekGE ); + VdbeCoverageIf(v, op==OP_SeekLE); testcase( op==OP_SeekLE ); + VdbeCoverageIf(v, op==OP_SeekLT); testcase( op==OP_SeekLT ); + + assert( bSeekPastNull==0 || bStopAtNull==0 ); + if( regBignull ){ + assert( bSeekPastNull==1 || bStopAtNull==1 ); + assert( bSeekPastNull==!bStopAtNull ); + assert( bStopAtNull==startEq ); + sqlite3VdbeAddOp2(v, OP_Goto, 0, sqlite3VdbeCurrentAddr(v)+2); + op = aStartOp[(nConstraint>1)*4 + 2 + bRev]; + sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, + nConstraint-startEq); + VdbeCoverage(v); + VdbeCoverageIf(v, op==OP_Rewind); testcase( op==OP_Rewind ); + VdbeCoverageIf(v, op==OP_Last); testcase( op==OP_Last ); + VdbeCoverageIf(v, op==OP_SeekGE); testcase( op==OP_SeekGE ); + VdbeCoverageIf(v, op==OP_SeekLE); testcase( op==OP_SeekLE ); + assert( op==OP_Rewind || op==OP_Last || op==OP_SeekGE || op==OP_SeekLE); + } + } /* Load the value for the inequality constraint at the end of the ** range (if any). */ nConstraint = nEq; + assert( pLevel->p2==0 ); if( pRangeEnd ){ Expr *pRight = pRangeEnd->pExpr->pRight; - sqlite3ExprCacheRemove(pParse, regBase+nEq, 1); - sqlite3ExprCode(pParse, pRight, regBase+nEq); + assert( addrSeekScan==0 ); + codeExprOrVector(pParse, pRight, regBase+nEq, nTop); whereLikeOptimizationStringFixup(v, pLevel, pRangeEnd); if( (pRangeEnd->wtFlags & TERM_VNULL)==0 && sqlite3ExprCanBeNull(pRight) @@ -1238,63 +2102,113 @@ Bitmask sqlite3WhereCodeOneLoopStart( sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt); VdbeCoverage(v); } - if( sqlite3CompareAffinity(pRight, cEndAff)!=SQLITE_AFF_BLOB - && !sqlite3ExprNeedsNoAffinityChange(pRight, cEndAff) - ){ - codeApplyAffinity(pParse, regBase+nEq, 1, &cEndAff); + if( zEndAff ){ + updateRangeAffinityStr(pRight, nTop, zEndAff); + codeApplyAffinity(pParse, regBase+nEq, nTop, zEndAff); + }else{ + assert( pParse->db->mallocFailed ); } - nConstraint++; + nConstraint += nTop; testcase( pRangeEnd->wtFlags & TERM_VIRTUAL ); + + if( sqlite3ExprIsVector(pRight)==0 ){ + disableTerm(pLevel, pRangeEnd); + }else{ + endEq = 1; + } }else if( bStopAtNull ){ - sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq); - endEq = 0; + if( regBignull==0 ){ + sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq); + endEq = 0; + } nConstraint++; } - sqlite3DbFree(db, zStartAff); + if( zStartAff ) sqlite3DbNNFreeNN(db, zStartAff); + if( zEndAff ) sqlite3DbNNFreeNN(db, zEndAff); /* Top of the loop body */ pLevel->p2 = sqlite3VdbeCurrentAddr(v); /* Check if the index cursor is past the end of the range. */ if( nConstraint ){ + if( regBignull ){ + /* Except, skip the end-of-range check while doing the NULL-scan */ + sqlite3VdbeAddOp2(v, OP_IfNot, regBignull, sqlite3VdbeCurrentAddr(v)+3); + VdbeComment((v, "If NULL-scan 2nd pass")); + VdbeCoverage(v); + } op = aEndOp[bRev*2 + endEq]; sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint); testcase( op==OP_IdxGT ); VdbeCoverageIf(v, op==OP_IdxGT ); testcase( op==OP_IdxGE ); VdbeCoverageIf(v, op==OP_IdxGE ); testcase( op==OP_IdxLT ); VdbeCoverageIf(v, op==OP_IdxLT ); testcase( op==OP_IdxLE ); VdbeCoverageIf(v, op==OP_IdxLE ); + if( addrSeekScan ) sqlite3VdbeJumpHere(v, addrSeekScan); + } + if( regBignull ){ + /* During a NULL-scan, check to see if we have reached the end of + ** the NULLs */ + assert( bSeekPastNull==!bStopAtNull ); + assert( bSeekPastNull+bStopAtNull==1 ); + assert( nConstraint+bSeekPastNull>0 ); + sqlite3VdbeAddOp2(v, OP_If, regBignull, sqlite3VdbeCurrentAddr(v)+2); + VdbeComment((v, "If NULL-scan 1st pass")); + VdbeCoverage(v); + op = aEndOp[bRev*2 + bSeekPastNull]; + sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, + nConstraint+bSeekPastNull); + testcase( op==OP_IdxGT ); VdbeCoverageIf(v, op==OP_IdxGT ); + testcase( op==OP_IdxGE ); VdbeCoverageIf(v, op==OP_IdxGE ); + testcase( op==OP_IdxLT ); VdbeCoverageIf(v, op==OP_IdxLT ); + testcase( op==OP_IdxLE ); VdbeCoverageIf(v, op==OP_IdxLE ); + } + + if( (pLoop->wsFlags & WHERE_IN_EARLYOUT)!=0 ){ + sqlite3VdbeAddOp3(v, OP_SeekHit, iIdxCur, nEq, nEq); } /* Seek the table cursor, if required */ - disableTerm(pLevel, pRangeStart); - disableTerm(pLevel, pRangeEnd); + omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0 + && (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))==0; if( omitTable ){ /* pIdx is a covering index. No need to access the main table. */ }else if( HasRowid(pIdx->pTable) ){ - if( pWInfo->eOnePass!=ONEPASS_OFF ){ - iRowidReg = ++pParse->nMem; - sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg); - sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); - sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, iRowidReg); - VdbeCoverage(v); - }else{ - codeDeferredSeek(pWInfo, pIdx, iCur, iIdxCur); - } + codeDeferredSeek(pWInfo, pIdx, iCur, iIdxCur); }else if( iCur!=iIdxCur ){ Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable); iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol); for(j=0; j<pPk->nKeyCol; j++){ - k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]); + k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[j]); sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, iRowidReg+j); } sqlite3VdbeAddOp4Int(v, OP_NotFound, iCur, addrCont, iRowidReg, pPk->nKeyCol); VdbeCoverage(v); } - /* Record the instruction used to terminate the loop. Disable - ** WHERE clause terms made redundant by the index range scan. - */ - if( pLoop->wsFlags & WHERE_ONEROW ){ + if( pLevel->iLeftJoin==0 ){ + /* If a partial index is driving the loop, try to eliminate WHERE clause + ** terms from the query that must be true due to the WHERE clause of + ** the partial index. This optimization does not work on an outer join, + ** as shown by: + ** + ** 2019-11-02 ticket 623eff57e76d45f6 (LEFT JOIN) + ** 2025-05-29 forum post 7dee41d32506c4ae (RIGHT JOIN) + */ + if( pIdx->pPartIdxWhere && pLevel->pRJ==0 ){ + whereApplyPartialIndexConstraints(pIdx->pPartIdxWhere, iCur, pWC); + } + }else{ + testcase( pIdx->pPartIdxWhere ); + /* The following assert() is not a requirement, merely an observation: + ** The OR-optimization doesn't work for the right hand table of + ** a LEFT JOIN: */ + assert( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))==0 ); + } + + /* Record the instruction used to terminate the loop. */ + if( (pLoop->wsFlags & WHERE_ONEROW) + || (pLevel->u.in.nIn && regBignull==0 && whereLoopIsOneRow(pLoop)) + ){ pLevel->op = OP_Noop; }else if( bRev ){ pLevel->op = OP_Prev; @@ -1308,6 +2222,7 @@ Bitmask sqlite3WhereCodeOneLoopStart( }else{ assert( pLevel->p5==0 ); } + if( omitTable ) pIdx = 0; }else #ifndef SQLITE_OMIT_OR_OPTIMIZATION @@ -1363,14 +2278,13 @@ Bitmask sqlite3WhereCodeOneLoopStart( int regReturn = ++pParse->nMem; /* Register used with OP_Gosub */ int regRowset = 0; /* Register for RowSet object */ int regRowid = 0; /* Register holding rowid */ - int iLoopBody = sqlite3VdbeMakeLabel(v); /* Start of loop body */ + int iLoopBody = sqlite3VdbeMakeLabel(pParse);/* Start of loop body */ int iRetInit; /* Address of regReturn init */ int untestedTerms = 0; /* Some terms not completely tested */ int ii; /* Loop counter */ - u16 wctrlFlags; /* Flags for sub-WHERE clause */ Expr *pAndExpr = 0; /* An ".. AND (...)" expression */ - Table *pTab = pTabItem->pTab; - + Table *pTab = pTabItem->pSTab; + pTerm = pLoop->aLTerm[0]; assert( pTerm!=0 ); assert( pTerm->eOperator & WO_OR ); @@ -1385,10 +2299,9 @@ Bitmask sqlite3WhereCodeOneLoopStart( */ if( pWInfo->nLevel>1 ){ int nNotReady; /* The number of notReady tables */ - struct SrcList_item *origSrc; /* Original list of tables */ + SrcItem *origSrc; /* Original list of tables */ nNotReady = pWInfo->nLevel - iLevel - 1; - pOrTab = sqlite3StackAllocRaw(db, - sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0])); + pOrTab = sqlite3DbMallocRawNN(db, SZ_SRCLIST(nNotReady+1)); if( pOrTab==0 ) return notReady; pOrTab->nAlloc = (u8)(nNotReady + 1); pOrTab->nSrc = pOrTab->nAlloc; @@ -1401,15 +2314,15 @@ Bitmask sqlite3WhereCodeOneLoopStart( pOrTab = pWInfo->pTabList; } - /* Initialize the rowset register to contain NULL. An SQL NULL is + /* Initialize the rowset register to contain NULL. An SQL NULL is ** equivalent to an empty rowset. Or, create an ephemeral index ** capable of holding primary keys in the case of a WITHOUT ROWID. ** - ** Also initialize regReturn to contain the address of the instruction + ** Also initialize regReturn to contain the address of the instruction ** immediately following the OP_Return at the bottom of the loop. This ** is required in a few obscure LEFT JOIN cases where control jumps - ** over the top of the loop into the body of it. In this case the - ** correct response for the end-of-loop code (the OP_Return) is to + ** over the top of the loop into the body of it. In this case the + ** correct response for the end-of-loop code (the OP_Return) is to ** fall through to the next instruction, just as an OP_Next does if ** called on an uninitialized cursor. */ @@ -1428,35 +2341,56 @@ Bitmask sqlite3WhereCodeOneLoopStart( iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn); /* If the original WHERE clause is z of the form: (x1 OR x2 OR ...) AND y - ** Then for every term xN, evaluate as the subexpression: xN AND z + ** Then for every term xN, evaluate as the subexpression: xN AND y ** That way, terms in y that are factored into the disjunction will ** be picked up by the recursive calls to sqlite3WhereBegin() below. ** ** Actually, each subexpression is converted to "xN AND w" where w is ** the "interesting" terms of z - terms that did not originate in the - ** ON or USING clause of a LEFT JOIN, and terms that are usable as + ** ON or USING clause of a LEFT JOIN, and terms that are usable as ** indices. ** ** This optimization also only applies if the (x1 OR x2 OR ...) term ** is not contained in the ON clause of a LEFT JOIN. - ** See ticket http://www.sqlite.org/src/info/f2369304e4 + ** See ticket http://sqlite.org/src/info/f2369304e4 + ** + ** 2022-02-04: Do not push down slices of a row-value comparison. + ** In other words, "w" or "y" may not be a slice of a vector. Otherwise, + ** the initialization of the right-hand operand of the vector comparison + ** might not occur, or might occur only in an OR branch that is not + ** taken. dbsqlfuzz 80a9fade844b4fb43564efc972bcb2c68270f5d1. + ** + ** 2022-03-03: Do not push down expressions that involve subqueries. + ** The subquery might get coded as a subroutine. Any table-references + ** in the subquery might be resolved to index-references for the index on + ** the OR branch in which the subroutine is coded. But if the subroutine + ** is invoked from a different OR branch that uses a different index, such + ** index-references will not work. tag-20220303a + ** https://sqlite.org/forum/forumpost/36937b197273d403 */ if( pWC->nTerm>1 ){ int iTerm; for(iTerm=0; iTerm<pWC->nTerm; iTerm++){ Expr *pExpr = pWC->a[iTerm].pExpr; if( &pWC->a[iTerm] == pTerm ) continue; - if( ExprHasProperty(pExpr, EP_FromJoin) ) continue; testcase( pWC->a[iTerm].wtFlags & TERM_VIRTUAL ); testcase( pWC->a[iTerm].wtFlags & TERM_CODED ); - if( (pWC->a[iTerm].wtFlags & (TERM_VIRTUAL|TERM_CODED))!=0 ) continue; + testcase( pWC->a[iTerm].wtFlags & TERM_SLICE ); + if( (pWC->a[iTerm].wtFlags & (TERM_VIRTUAL|TERM_CODED|TERM_SLICE))!=0 ){ + continue; + } if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue; - testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO ); + if( ExprHasProperty(pExpr, EP_Subquery) ) continue; /* tag-20220303a */ pExpr = sqlite3ExprDup(db, pExpr, 0); - pAndExpr = sqlite3ExprAnd(db, pAndExpr, pExpr); + pAndExpr = sqlite3ExprAnd(pParse, pAndExpr, pExpr); } if( pAndExpr ){ - pAndExpr = sqlite3PExpr(pParse, TK_AND|TKFLG_DONTFOLD, 0, pAndExpr, 0); + /* The extra 0x10000 bit on the opcode is masked off and does not + ** become part of the new Expr.op. However, it does make the + ** op==TK_AND comparison inside of sqlite3PExpr() false, and this + ** prevents sqlite3PExpr() from applying the AND short-circuit + ** optimization, which we do not want here. */ + pAndExpr = sqlite3PExpr(pParse, TK_AND|0x10000, 0, pAndExpr); } } @@ -1464,29 +2398,36 @@ Bitmask sqlite3WhereCodeOneLoopStart( ** eliminating duplicates from other WHERE clauses, the action for each ** sub-WHERE clause is to to invoke the main loop body as a subroutine. */ - wctrlFlags = WHERE_OMIT_OPEN_CLOSE - | WHERE_FORCE_TABLE - | WHERE_ONETABLE_ONLY - | WHERE_NO_AUTOINDEX; + ExplainQueryPlan((pParse, 1, "MULTI-INDEX OR")); for(ii=0; ii<pOrWc->nTerm; ii++){ WhereTerm *pOrTerm = &pOrWc->a[ii]; if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){ WhereInfo *pSubWInfo; /* Info for single OR-term scan */ Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */ + Expr *pDelete; /* Local copy of OR clause term */ int jmp1 = 0; /* Address of jump operation */ - if( pAndExpr && !ExprHasProperty(pOrExpr, EP_FromJoin) ){ + testcase( (pTabItem[0].fg.jointype & JT_LEFT)!=0 + && !ExprHasProperty(pOrExpr, EP_OuterON) + ); /* See TH3 vtab25.400 and ticket 614b25314c766238 */ + pDelete = pOrExpr = sqlite3ExprDup(db, pOrExpr, 0); + if( db->mallocFailed ){ + sqlite3ExprDelete(db, pDelete); + continue; + } + if( pAndExpr ){ pAndExpr->pLeft = pOrExpr; pOrExpr = pAndExpr; } /* Loop through table entries that match term pOrTerm. */ - WHERETRACE(0xffff, ("Subplan for OR-clause:\n")); - pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0, - wctrlFlags, iCovCur); - assert( pSubWInfo || pParse->nErr || db->mallocFailed ); + ExplainQueryPlan((pParse, 1, "INDEX %d", ii+1)); + WHERETRACE(0xffffffff, ("Subplan for OR-clause:\n")); + pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0, 0, + WHERE_OR_SUBCLAUSE, iCovCur); + assert( pSubWInfo || pParse->nErr ); if( pSubWInfo ){ WhereLoop *pSubLoop; int addrExplain = sqlite3WhereExplainOneScan( - pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0 + pParse, pOrTab, &pSubWInfo->a[0], 0 ); sqlite3WhereAddScanStatus(v, pOrTab, &pSubWInfo->a[0], addrExplain); @@ -1496,23 +2437,23 @@ Bitmask sqlite3WhereCodeOneLoopStart( ** row will be skipped in subsequent sub-WHERE clauses. */ if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){ - int r; int iSet = ((ii==pOrWc->nTerm-1)?-1:ii); if( HasRowid(pTab) ){ - r = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iCur, regRowid, 0); + sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, -1, regRowid); jmp1 = sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset, 0, - r,iSet); + regRowid, iSet); VdbeCoverage(v); }else{ Index *pPk = sqlite3PrimaryKeyIndex(pTab); int nPk = pPk->nKeyCol; int iPk; + int r; /* Read the PK into an array of temp registers. */ r = sqlite3GetTempRange(pParse, nPk); for(iPk=0; iPk<nPk; iPk++){ int iCol = pPk->aiColumn[iPk]; - sqlite3ExprCodeGetColumnToReg(pParse, pTab, iCol, iCur, r+iPk); + sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, iCol,r+iPk); } /* Check if the temp table already contains this key. If so, @@ -1523,16 +2464,17 @@ Bitmask sqlite3WhereCodeOneLoopStart( ** ** Use some of the same optimizations as OP_RowSetTest: If iSet ** is zero, assume that the key cannot already be present in - ** the temp table. And if iSet is -1, assume that there is no - ** need to insert the key into the temp table, as it will never - ** be tested for. */ + ** the temp table. And if iSet is -1, assume that there is no + ** need to insert the key into the temp table, as it will never + ** be tested for. */ if( iSet ){ jmp1 = sqlite3VdbeAddOp4Int(v, OP_Found, regRowset, 0, r, nPk); VdbeCoverage(v); } if( iSet>=0 ){ sqlite3VdbeAddOp3(v, OP_MakeRecord, r, nPk, regRowid); - sqlite3VdbeAddOp3(v, OP_IdxInsert, regRowset, regRowid, 0); + sqlite3VdbeAddOp4Int(v, OP_IdxInsert, regRowset, regRowid, + r, nPk); if( iSet ) sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); } @@ -1563,8 +2505,8 @@ Bitmask sqlite3WhereCodeOneLoopStart( ** If the call to sqlite3WhereBegin() above resulted in a scan that ** uses an index, and this is either the first OR-connected term ** processed or the index is the same as that used by all previous - ** terms, set pCov to the candidate covering index. Otherwise, set - ** pCov to NULL to indicate that no candidate covering index will + ** terms, set pCov to the candidate covering index. Otherwise, set + ** pCov to NULL to indicate that no candidate covering index will ** be available. */ pSubLoop = pSubWInfo->a[0].pWLoop; @@ -1575,17 +2517,25 @@ Bitmask sqlite3WhereCodeOneLoopStart( ){ assert( pSubWInfo->a[0].iIdxCur==iCovCur ); pCov = pSubLoop->u.btree.pIndex; - wctrlFlags |= WHERE_REOPEN_IDX; }else{ pCov = 0; } + if( sqlite3WhereUsesDeferredSeek(pSubWInfo) ){ + pWInfo->bDeferredSeek = 1; + } /* Finish the loop through table entries that match term pOrTerm. */ sqlite3WhereEnd(pSubWInfo); + ExplainQueryPlanPop(pParse); } + sqlite3ExprDelete(db, pDelete); } } - pLevel->u.pCovidx = pCov; + ExplainQueryPlanPop(pParse); + assert( pLevel->pWLoop==pLoop ); + assert( (pLoop->wsFlags & WHERE_MULTI_OR)!=0 ); + assert( (pLoop->wsFlags & WHERE_IN_ABLE)==0 ); + pLevel->u.pCoveringIdx = pCov; if( pCov ) pLevel->iIdxCur = iCovCur; if( pAndExpr ){ pAndExpr->pLeft = 0; @@ -1595,7 +2545,15 @@ Bitmask sqlite3WhereCodeOneLoopStart( sqlite3VdbeGoto(v, pLevel->addrBrk); sqlite3VdbeResolveLabel(v, iLoopBody); - if( pWInfo->nLevel>1 ) sqlite3StackFree(db, pOrTab); + /* Set the P2 operand of the OP_Return opcode that will end the current + ** loop to point to this spot, which is the top of the next containing + ** loop. The byte-code formatter will use that P2 value as a hint to + ** indent everything in between the this point and the final OP_Return. + ** See tag-20220407a in vdbe.c and shell.c */ + assert( pLevel->op==OP_Return ); + pLevel->p2 = sqlite3VdbeCurrentAddr(v); + + if( pWInfo->nLevel>1 ){ sqlite3DbFreeNN(db, pOrTab); } if( !untestedTerms ) disableTerm(pLevel, pTerm); }else #endif /* SQLITE_OMIT_OR_OPTIMIZATION */ @@ -1612,10 +2570,10 @@ Bitmask sqlite3WhereCodeOneLoopStart( ** a pseudo-cursor. No need to Rewind or Next such cursors. */ pLevel->op = OP_Noop; }else{ - codeCursorHint(pWInfo, pLevel, 0); + codeCursorHint(pTabItem, pWInfo, pLevel, 0); pLevel->op = aStep[bRev]; pLevel->p1 = iCur; - pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk); + pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev],iCur,pLevel->addrHalt); VdbeCoverageIf(v, bRev==0); VdbeCoverageIf(v, bRev!=0); pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; @@ -1628,37 +2586,99 @@ Bitmask sqlite3WhereCodeOneLoopStart( /* Insert code to test every subexpression that can be completely ** computed using the current set of tables. + ** + ** This loop may run between one and three times, depending on the + ** constraints to be generated. The value of stack variable iLoop + ** determines the constraints coded by each iteration, as follows: + ** + ** iLoop==1: Code only expressions that are entirely covered by pIdx. + ** iLoop==2: Code remaining expressions that do not contain correlated + ** sub-queries. + ** iLoop==3: Code all remaining expressions. + ** + ** An effort is made to skip unnecessary iterations of the loop. + ** + ** This optimization of causing simple query restrictions to occur before + ** more complex one is call the "push-down" optimization in MySQL. Here + ** in SQLite, the name is "MySQL push-down", since there is also another + ** totally unrelated optimization called "WHERE-clause push-down". + ** Sometimes the qualifier is omitted, resulting in an ambiguity, so beware. */ - for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){ - Expr *pE; - int skipLikeAddr = 0; - testcase( pTerm->wtFlags & TERM_VIRTUAL ); - testcase( pTerm->wtFlags & TERM_CODED ); - if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; - if( (pTerm->prereqAll & pLevel->notReady)!=0 ){ - testcase( pWInfo->untestedTerms==0 - && (pWInfo->wctrlFlags & WHERE_ONETABLE_ONLY)!=0 ); - pWInfo->untestedTerms = 1; - continue; - } - pE = pTerm->pExpr; - assert( pE!=0 ); - if( pLevel->iLeftJoin && !ExprHasProperty(pE, EP_FromJoin) ){ - continue; - } - if( pTerm->wtFlags & TERM_LIKECOND ){ + iLoop = (pIdx ? 1 : 2); + do{ + int iNext = 0; /* Next value for iLoop */ + for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){ + Expr *pE; + int skipLikeAddr = 0; + testcase( pTerm->wtFlags & TERM_VIRTUAL ); + testcase( pTerm->wtFlags & TERM_CODED ); + if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; + if( (pTerm->prereqAll & pLevel->notReady)!=0 ){ + testcase( pWInfo->untestedTerms==0 + && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)!=0 ); + pWInfo->untestedTerms = 1; + continue; + } + pE = pTerm->pExpr; + assert( pE!=0 ); + if( pTabItem->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT) ){ + if( !ExprHasProperty(pE,EP_OuterON|EP_InnerON) ){ + /* Defer processing WHERE clause constraints until after outer + ** join processing. tag-20220513a */ + continue; + }else if( (pTabItem->fg.jointype & JT_LEFT)==JT_LEFT + && !ExprHasProperty(pE,EP_OuterON) ){ + continue; + }else{ + Bitmask m = sqlite3WhereGetMask(&pWInfo->sMaskSet, pE->w.iJoin); + if( m & pLevel->notReady ){ + /* An ON clause that is not ripe */ + continue; + } + } + } + if( iLoop==1 && !sqlite3ExprCoveredByIndex(pE, pLevel->iTabCur, pIdx) ){ + iNext = 2; + continue; + } + if( iLoop<3 && (pTerm->wtFlags & TERM_VARSELECT) ){ + if( iNext==0 ) iNext = 3; + continue; + } + + if( (pTerm->wtFlags & TERM_LIKECOND)!=0 ){ + /* If the TERM_LIKECOND flag is set, that means that the range search + ** is sufficient to guarantee that the LIKE operator is true, so we + ** can skip the call to the like(A,B) function. But this only works + ** for strings. So do not skip the call to the function on the pass + ** that compares BLOBs. */ #ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS - continue; + continue; #else - assert( pLevel->iLikeRepCntr>0 ); - skipLikeAddr = sqlite3VdbeAddOp1(v, OP_IfNot, pLevel->iLikeRepCntr); - VdbeCoverage(v); + u32 x = pLevel->iLikeRepCntr; + if( x>0 ){ + skipLikeAddr = sqlite3VdbeAddOp1(v, (x&1)?OP_IfNot:OP_If,(int)(x>>1)); + VdbeCoverageIf(v, (x&1)==1); + VdbeCoverageIf(v, (x&1)==0); + } +#endif + } +#ifdef WHERETRACE_ENABLED /* 0xffffffff */ + if( sqlite3WhereTrace ){ + VdbeNoopComment((v, "WhereTerm[%d] (%p) priority=%d", + pWC->nTerm-j, pTerm, iLoop)); + } + if( sqlite3WhereTrace & 0x4000 ){ + sqlite3DebugPrintf("Coding auxiliary constraint:\n"); + sqlite3WhereTermPrint(pTerm, pWC->nTerm-j); + } #endif + sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL); + if( skipLikeAddr ) sqlite3VdbeJumpHere(v, skipLikeAddr); + pTerm->wtFlags |= TERM_CODED; } - sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL); - if( skipLikeAddr ) sqlite3VdbeJumpHere(v, skipLikeAddr); - pTerm->wtFlags |= TERM_CODED; - } + iLoop = iNext; + }while( iLoop>0 ); /* Insert code to test for implied constraints based on transitivity ** of the "==" operator. @@ -1668,43 +2688,117 @@ Bitmask sqlite3WhereCodeOneLoopStart( ** then we cannot use the "t1.a=t2.b" constraint, but we can code ** the implied "t1.a=123" constraint. */ - for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){ - Expr *pE, *pEAlt; + for(pTerm=pWC->a, j=pWC->nBase; j>0; j--, pTerm++){ + Expr *pE, sEAlt; WhereTerm *pAlt; if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) continue; if( (pTerm->eOperator & WO_EQUIV)==0 ) continue; if( pTerm->leftCursor!=iCur ) continue; - if( pLevel->iLeftJoin ) continue; + if( pTabItem->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT) ) continue; pE = pTerm->pExpr; - assert( !ExprHasProperty(pE, EP_FromJoin) ); +#ifdef WHERETRACE_ENABLED /* 0x4001 */ + if( (sqlite3WhereTrace & 0x4001)==0x4001 ){ + sqlite3DebugPrintf("Coding transitive constraint:\n"); + sqlite3WhereTermPrint(pTerm, pWC->nTerm-j); + } +#endif + assert( !ExprHasProperty(pE, EP_OuterON) ); assert( (pTerm->prereqRight & pLevel->notReady)!=0 ); - pAlt = sqlite3WhereFindTerm(pWC, iCur, pTerm->u.leftColumn, notReady, + assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 ); + pAlt = sqlite3WhereFindTerm(pWC, iCur, pTerm->u.x.leftColumn, notReady, WO_EQ|WO_IN|WO_IS, 0); if( pAlt==0 ) continue; if( pAlt->wtFlags & (TERM_CODED) ) continue; + if( (pAlt->eOperator & WO_IN) + && ExprUseXSelect(pAlt->pExpr) + && (pAlt->pExpr->x.pSelect->pEList->nExpr>1) + ){ + continue; + } testcase( pAlt->eOperator & WO_EQ ); testcase( pAlt->eOperator & WO_IS ); testcase( pAlt->eOperator & WO_IN ); VdbeModuleComment((v, "begin transitive constraint")); - pEAlt = sqlite3StackAllocRaw(db, sizeof(*pEAlt)); - if( pEAlt ){ - *pEAlt = *pAlt->pExpr; - pEAlt->pLeft = pE->pLeft; - sqlite3ExprIfFalse(pParse, pEAlt, addrCont, SQLITE_JUMPIFNULL); - sqlite3StackFree(db, pEAlt); + sEAlt = *pAlt->pExpr; + sEAlt.pLeft = pE->pLeft; + sqlite3ExprIfFalse(pParse, &sEAlt, addrCont, SQLITE_JUMPIFNULL); + pAlt->wtFlags |= TERM_CODED; + } + + /* For a RIGHT OUTER JOIN, record the fact that the current row has + ** been matched at least once. + */ + if( pLevel->pRJ ){ + Table *pTab; + int nPk; + int r; + int jmp1 = 0; + WhereRightJoin *pRJ = pLevel->pRJ; + + /* pTab is the right-hand table of the RIGHT JOIN. Generate code that + ** will record that the current row of that table has been matched at + ** least once. This is accomplished by storing the PK for the row in + ** both the iMatch index and the regBloom Bloom filter. + */ + pTab = pWInfo->pTabList->a[pLevel->iFrom].pSTab; + if( HasRowid(pTab) ){ + r = sqlite3GetTempRange(pParse, 2); + sqlite3ExprCodeGetColumnOfTable(v, pTab, pLevel->iTabCur, -1, r+1); + nPk = 1; + }else{ + int iPk; + Index *pPk = sqlite3PrimaryKeyIndex(pTab); + nPk = pPk->nKeyCol; + r = sqlite3GetTempRange(pParse, nPk+1); + for(iPk=0; iPk<nPk; iPk++){ + int iCol = pPk->aiColumn[iPk]; + sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, iCol,r+1+iPk); + } } + jmp1 = sqlite3VdbeAddOp4Int(v, OP_Found, pRJ->iMatch, 0, r+1, nPk); + VdbeCoverage(v); + VdbeComment((v, "match against %s", pTab->zName)); + sqlite3VdbeAddOp3(v, OP_MakeRecord, r+1, nPk, r); + sqlite3VdbeAddOp4Int(v, OP_IdxInsert, pRJ->iMatch, r, r+1, nPk); + sqlite3VdbeAddOp4Int(v, OP_FilterAdd, pRJ->regBloom, 0, r+1, nPk); + sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); + sqlite3VdbeJumpHere(v, jmp1); + sqlite3ReleaseTempRange(pParse, r, nPk+1); } /* For a LEFT OUTER JOIN, generate code that will record the fact that - ** at least one row of the right table has matched the left table. + ** at least one row of the right table has matched the left table. */ if( pLevel->iLeftJoin ){ pLevel->addrFirst = sqlite3VdbeCurrentAddr(v); sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin); VdbeComment((v, "record LEFT JOIN hit")); - sqlite3ExprCacheClear(pParse); - for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){ + if( pLevel->pRJ==0 ){ + goto code_outer_join_constraints; /* WHERE clause constraints */ + } + } + + if( pLevel->pRJ ){ + /* Create a subroutine used to process all interior loops and code + ** of the RIGHT JOIN. During normal operation, the subroutine will + ** be in-line with the rest of the code. But at the end, a separate + ** loop will run that invokes this subroutine for unmatched rows + ** of pTab, with all tables to left begin set to NULL. + */ + WhereRightJoin *pRJ = pLevel->pRJ; + sqlite3VdbeAddOp2(v, OP_BeginSubrtn, 0, pRJ->regReturn); + pRJ->addrSubrtn = sqlite3VdbeCurrentAddr(v); + assert( pParse->withinRJSubrtn < 255 ); + pParse->withinRJSubrtn++; + + /* WHERE clause constraints must be deferred until after outer join + ** row elimination has completed, since WHERE clause constraints apply + ** to the results of the OUTER JOIN. The following loop generates the + ** appropriate WHERE clause constraint checks. tag-20220513a. + */ + code_outer_join_constraints: + for(pTerm=pWC->a, j=0; j<pWC->nBase; j++, pTerm++){ testcase( pTerm->wtFlags & TERM_VIRTUAL ); testcase( pTerm->wtFlags & TERM_CODED ); if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; @@ -1712,11 +2806,131 @@ Bitmask sqlite3WhereCodeOneLoopStart( assert( pWInfo->untestedTerms ); continue; } + if( pTabItem->fg.jointype & JT_LTORJ ) continue; assert( pTerm->pExpr ); sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL); pTerm->wtFlags |= TERM_CODED; } } +#if WHERETRACE_ENABLED /* 0x4001 */ + if( sqlite3WhereTrace & 0x4000 ){ + sqlite3DebugPrintf("All WHERE-clause terms after coding level %d:\n", + iLevel); + sqlite3WhereClausePrint(pWC); + } + if( sqlite3WhereTrace & 0x1 ){ + sqlite3DebugPrintf("End Coding level %d: notReady=%llx\n", + iLevel, (u64)pLevel->notReady); + } +#endif return pLevel->notReady; } + +/* +** Generate the code for the loop that finds all non-matched terms +** for a RIGHT JOIN. +*/ +SQLITE_NOINLINE void sqlite3WhereRightJoinLoop( + WhereInfo *pWInfo, + int iLevel, + WhereLevel *pLevel +){ + Parse *pParse = pWInfo->pParse; + Vdbe *v = pParse->pVdbe; + WhereRightJoin *pRJ = pLevel->pRJ; + Expr *pSubWhere = 0; + WhereClause *pWC = &pWInfo->sWC; + WhereInfo *pSubWInfo; + WhereLoop *pLoop = pLevel->pWLoop; + SrcItem *pTabItem = &pWInfo->pTabList->a[pLevel->iFrom]; + SrcList *pFrom; + union { + SrcList sSrc; + u8 fromSpace[SZ_SRCLIST_1]; + } uSrc; + Bitmask mAll = 0; + int k; + + ExplainQueryPlan((pParse, 1, "RIGHT-JOIN %s", pTabItem->pSTab->zName)); + sqlite3VdbeNoJumpsOutsideSubrtn(v, pRJ->addrSubrtn, pRJ->endSubrtn, + pRJ->regReturn); + for(k=0; k<iLevel; k++){ + int iIdxCur; + SrcItem *pRight; + assert( pWInfo->a[k].pWLoop->iTab == pWInfo->a[k].iFrom ); + pRight = &pWInfo->pTabList->a[pWInfo->a[k].iFrom]; + mAll |= pWInfo->a[k].pWLoop->maskSelf; + if( pRight->fg.viaCoroutine ){ + Subquery *pSubq; + assert( pRight->fg.isSubquery && pRight->u4.pSubq!=0 ); + pSubq = pRight->u4.pSubq; + assert( pSubq->pSelect!=0 && pSubq->pSelect->pEList!=0 ); + sqlite3VdbeAddOp3( + v, OP_Null, 0, pSubq->regResult, + pSubq->regResult + pSubq->pSelect->pEList->nExpr-1 + ); + } + sqlite3VdbeAddOp1(v, OP_NullRow, pWInfo->a[k].iTabCur); + iIdxCur = pWInfo->a[k].iIdxCur; + if( iIdxCur ){ + sqlite3VdbeAddOp1(v, OP_NullRow, iIdxCur); + } + } + if( (pTabItem->fg.jointype & JT_LTORJ)==0 ){ + mAll |= pLoop->maskSelf; + for(k=0; k<pWC->nTerm; k++){ + WhereTerm *pTerm = &pWC->a[k]; + if( (pTerm->wtFlags & (TERM_VIRTUAL|TERM_SLICE))!=0 + && pTerm->eOperator!=WO_ROWVAL + ){ + break; + } + if( pTerm->prereqAll & ~mAll ) continue; + if( ExprHasProperty(pTerm->pExpr, EP_OuterON|EP_InnerON) ) continue; + pSubWhere = sqlite3ExprAnd(pParse, pSubWhere, + sqlite3ExprDup(pParse->db, pTerm->pExpr, 0)); + } + } + pFrom = &uSrc.sSrc; + pFrom->nSrc = 1; + pFrom->nAlloc = 1; + memcpy(&pFrom->a[0], pTabItem, sizeof(SrcItem)); + pFrom->a[0].fg.jointype = 0; + assert( pParse->withinRJSubrtn < 100 ); + pParse->withinRJSubrtn++; + pSubWInfo = sqlite3WhereBegin(pParse, pFrom, pSubWhere, 0, 0, 0, + WHERE_RIGHT_JOIN, 0); + if( pSubWInfo ){ + int iCur = pLevel->iTabCur; + int r = ++pParse->nMem; + int nPk; + int jmp; + int addrCont = sqlite3WhereContinueLabel(pSubWInfo); + Table *pTab = pTabItem->pSTab; + if( HasRowid(pTab) ){ + sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, -1, r); + nPk = 1; + }else{ + int iPk; + Index *pPk = sqlite3PrimaryKeyIndex(pTab); + nPk = pPk->nKeyCol; + pParse->nMem += nPk - 1; + for(iPk=0; iPk<nPk; iPk++){ + int iCol = pPk->aiColumn[iPk]; + sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, iCol,r+iPk); + } + } + jmp = sqlite3VdbeAddOp4Int(v, OP_Filter, pRJ->regBloom, 0, r, nPk); + VdbeCoverage(v); + sqlite3VdbeAddOp4Int(v, OP_Found, pRJ->iMatch, addrCont, r, nPk); + VdbeCoverage(v); + sqlite3VdbeJumpHere(v, jmp); + sqlite3VdbeAddOp2(v, OP_Gosub, pRJ->regReturn, pRJ->addrSubrtn); + sqlite3WhereEnd(pSubWInfo); + } + sqlite3ExprDelete(pParse->db, pSubWhere); + ExplainQueryPlanPop(pParse); + assert( pParse->withinRJSubrtn>0 ); + pParse->withinRJSubrtn--; +} diff --git a/src/whereexpr.c b/src/whereexpr.c index c84d2f230a..0d99ca85e6 100644 --- a/src/whereexpr.c +++ b/src/whereexpr.c @@ -13,7 +13,7 @@ ** the WHERE clause of SQL statements. ** ** This file was originally part of where.c but was split out to improve -** readability and editabiliity. This file contains utility routines for +** readability and editability. This file contains utility routines for ** analyzing Expr objects in the WHERE clause. */ #include "sqliteInt.h" @@ -64,7 +64,7 @@ static int whereClauseInsert(WhereClause *pWC, Expr *p, u16 wtFlags){ if( pWC->nTerm>=pWC->nSlot ){ WhereTerm *pOld = pWC->a; sqlite3 *db = pWC->pWInfo->pParse->db; - pWC->a = sqlite3DbMallocRawNN(db, sizeof(pWC->a[0])*pWC->nSlot*2 ); + pWC->a = sqlite3WhereMalloc(pWC->pWInfo, sizeof(pWC->a[0])*pWC->nSlot*2 ); if( pWC->a==0 ){ if( wtFlags & TERM_DYNAMIC ){ sqlite3ExprDelete(db, p); @@ -73,66 +73,53 @@ static int whereClauseInsert(WhereClause *pWC, Expr *p, u16 wtFlags){ return 0; } memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm); - if( pOld!=pWC->aStatic ){ - sqlite3DbFree(db, pOld); - } - pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]); - memset(&pWC->a[pWC->nTerm], 0, sizeof(pWC->a[0])*(pWC->nSlot-pWC->nTerm)); + pWC->nSlot = pWC->nSlot*2; } pTerm = &pWC->a[idx = pWC->nTerm++]; + if( (wtFlags & TERM_VIRTUAL)==0 ) pWC->nBase = pWC->nTerm; if( p && ExprHasProperty(p, EP_Unlikely) ){ pTerm->truthProb = sqlite3LogEst(p->iTable) - 270; }else{ pTerm->truthProb = 1; } - pTerm->pExpr = sqlite3ExprSkipCollate(p); + pTerm->pExpr = sqlite3ExprSkipCollateAndLikely(p); pTerm->wtFlags = wtFlags; pTerm->pWC = pWC; pTerm->iParent = -1; + memset(&pTerm->eOperator, 0, + sizeof(WhereTerm) - offsetof(WhereTerm,eOperator)); return idx; } /* ** Return TRUE if the given operator is one of the operators that is ** allowed for an indexable WHERE clause term. The allowed operators are -** "=", "<", ">", "<=", ">=", "IN", and "IS NULL" +** "=", "<", ">", "<=", ">=", "IN", "IS", and "IS NULL" */ static int allowedOp(int op){ assert( TK_GT>TK_EQ && TK_GT<TK_GE ); assert( TK_LT>TK_EQ && TK_LT<TK_GE ); assert( TK_LE>TK_EQ && TK_LE<TK_GE ); assert( TK_GE==TK_EQ+4 ); - return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL || op==TK_IS; + assert( TK_IN<TK_EQ ); + assert( TK_IS<TK_EQ ); + assert( TK_ISNULL<TK_EQ ); + if( op>TK_GE ) return 0; + if( op>=TK_EQ ) return 1; + return op==TK_IN || op==TK_ISNULL || op==TK_IS; } /* ** Commute a comparison operator. Expressions of the form "X op Y" ** are converted into "Y op X". -** -** If left/right precedence rules come into play when determining the -** collating sequence, then COLLATE operators are adjusted to ensure -** that the collating sequence does not change. For example: -** "Y collate NOCASE op X" becomes "X op Y" because any collation sequence on -** the left hand side of a comparison overrides any collation sequence -** attached to the right. For the same reason the EP_Collate flag -** is not commuted. */ -static void exprCommute(Parse *pParse, Expr *pExpr){ - u16 expRight = (pExpr->pRight->flags & EP_Collate); - u16 expLeft = (pExpr->pLeft->flags & EP_Collate); - assert( allowedOp(pExpr->op) && pExpr->op!=TK_IN ); - if( expRight==expLeft ){ - /* Either X and Y both have COLLATE operator or neither do */ - if( expRight ){ - /* Both X and Y have COLLATE operators. Make sure X is always - ** used by clearing the EP_Collate flag from Y. */ - pExpr->pRight->flags &= ~EP_Collate; - }else if( sqlite3ExprCollSeq(pParse, pExpr->pLeft)!=0 ){ - /* Neither X nor Y have COLLATE operators, but X has a non-default - ** collating sequence. So add the EP_Collate marker on X to cause - ** it to be searched first. */ - pExpr->pLeft->flags |= EP_Collate; - } +static u16 exprCommute(Parse *pParse, Expr *pExpr){ + if( pExpr->pLeft->op==TK_VECTOR + || pExpr->pRight->op==TK_VECTOR + || sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pRight) != + sqlite3BinaryCompareCollSeq(pParse, pExpr->pRight, pExpr->pLeft) + ){ + pExpr->flags ^= EP_Commuted; } SWAP(Expr*,pExpr->pRight,pExpr->pLeft); if( pExpr->op>=TK_GT ){ @@ -143,6 +130,7 @@ static void exprCommute(Parse *pParse, Expr *pExpr){ assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE ); pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT; } + return 0; } /* @@ -151,15 +139,16 @@ static void exprCommute(Parse *pParse, Expr *pExpr){ static u16 operatorMask(int op){ u16 c; assert( allowedOp(op) ); - if( op==TK_IN ){ + if( op>=TK_EQ ){ + assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff ); + c = (u16)(WO_EQ<<(op-TK_EQ)); + }else if( op==TK_IN ){ c = WO_IN; }else if( op==TK_ISNULL ){ c = WO_ISNULL; - }else if( op==TK_IS ){ - c = WO_IS; }else{ - assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff ); - c = (u16)(WO_EQ<<(op-TK_EQ)); + assert( op==TK_IS ); + c = WO_IS; } assert( op!=TK_ISNULL || c==WO_ISNULL ); assert( op!=TK_IN || c==WO_IN ); @@ -193,70 +182,149 @@ static int isLikeOrGlob( int *pisComplete, /* True if the only wildcard is % in the last character */ int *pnoCase /* True if uppercase is equivalent to lowercase */ ){ - const char *z = 0; /* String on RHS of LIKE operator */ + const u8 *z = 0; /* String on RHS of LIKE operator */ Expr *pRight, *pLeft; /* Right and left size of LIKE operator */ ExprList *pList; /* List of operands to the LIKE operator */ - int c; /* One character in z[] */ + u8 c; /* One character in z[] */ int cnt; /* Number of non-wildcard prefix characters */ - char wc[3]; /* Wildcard characters */ + u8 wc[4]; /* Wildcard characters */ sqlite3 *db = pParse->db; /* Database connection */ sqlite3_value *pVal = 0; int op; /* Opcode of pRight */ int rc; /* Result code to return */ - if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, wc) ){ + if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, (char*)wc) ){ return 0; } #ifdef SQLITE_EBCDIC if( *pnoCase ) return 0; #endif + assert( ExprUseXList(pExpr) ); pList = pExpr->x.pList; pLeft = pList->a[1].pExpr; - if( pLeft->op!=TK_COLUMN - || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT - || IsVirtual(pLeft->pTab) /* Value might be numeric */ - ){ - /* IMP: R-02065-49465 The left-hand side of the LIKE or GLOB operator must - ** be the name of an indexed column with TEXT affinity. */ - return 0; - } - assert( pLeft->iColumn!=(-1) ); /* Because IPK never has AFF_TEXT */ pRight = sqlite3ExprSkipCollate(pList->a[0].pExpr); op = pRight->op; - if( op==TK_VARIABLE ){ + if( op==TK_VARIABLE && (db->flags & SQLITE_EnableQPSG)==0 ){ Vdbe *pReprepare = pParse->pReprepare; int iCol = pRight->iColumn; pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_BLOB); if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){ - z = (char *)sqlite3_value_text(pVal); + z = sqlite3_value_text(pVal); } sqlite3VdbeSetVarmask(pParse->pVdbe, iCol); assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER ); }else if( op==TK_STRING ){ - z = pRight->u.zToken; + assert( !ExprHasProperty(pRight, EP_IntValue) ); + z = (u8*)pRight->u.zToken; } if( z ){ + /* Count the number of prefix bytes prior to the first wildcard, + ** U+fffd character, or malformed utf-8. If the underlying database + ** has a UTF16LE encoding, then only consider ASCII characters. Note that + ** the encoding of z[] is UTF8 - we are dealing with only UTF8 here in this + ** code, but the database engine itself might be processing content using a + ** different encoding. */ cnt = 0; while( (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){ cnt++; + if( c==wc[3] && z[cnt]>0 && z[cnt]<0x80 ){ + cnt++; + }else if( c>=0x80 ){ + const u8 *z2 = z+cnt-1; + if( c==0xff || sqlite3Utf8Read(&z2)==0xfffd /* bad utf-8 */ + || ENC(db)==SQLITE_UTF16LE + ){ + cnt--; + break; + }else{ + cnt = (int)(z2-z); + } + } } - if( cnt!=0 && 255!=(u8)z[cnt-1] ){ + + /* The optimization is possible only if (1) the pattern does not begin + ** with a wildcard and if (2) the non-wildcard prefix does not end with + ** an (illegal 0xff) character, or (3) the pattern does not consist of + ** a single escape character. The second condition is necessary so + ** that we can increment the prefix key to find an upper bound for the + ** range search. The third is because the caller assumes that the pattern + ** consists of at least one character after all escapes have been + ** removed. */ + if( (cnt>1 || (cnt>0 && z[0]!=wc[3])) && ALWAYS(255!=(u8)z[cnt-1]) ){ Expr *pPrefix; - *pisComplete = c==wc[0] && z[cnt+1]==0; - pPrefix = sqlite3Expr(db, TK_STRING, z); - if( pPrefix ) pPrefix->u.zToken[cnt] = 0; + + /* A "complete" match if the pattern ends with "*" or "%" */ + *pisComplete = c==wc[0] && z[cnt+1]==0 && ENC(db)!=SQLITE_UTF16LE; + + /* Get the pattern prefix. Remove all escapes from the prefix. */ + pPrefix = sqlite3Expr(db, TK_STRING, (char*)z); + if( pPrefix ){ + int iFrom, iTo; + char *zNew; + assert( !ExprHasProperty(pPrefix, EP_IntValue) ); + zNew = pPrefix->u.zToken; + zNew[cnt] = 0; + for(iFrom=iTo=0; iFrom<cnt; iFrom++){ + if( zNew[iFrom]==wc[3] ) iFrom++; + zNew[iTo++] = zNew[iFrom]; + } + zNew[iTo] = 0; + assert( iTo>0 ); + + /* If the LHS is not an ordinary column with TEXT affinity, then the + ** pattern prefix boundaries (both the start and end boundaries) must + ** not look like a number. Otherwise the pattern might be treated as + ** a number, which will invalidate the LIKE optimization. + ** + ** Getting this right has been a persistent source of bugs in the + ** LIKE optimization. See, for example: + ** 2018-09-10 https://sqlite.org/src/info/c94369cae9b561b1 + ** 2019-05-02 https://sqlite.org/src/info/b043a54c3de54b28 + ** 2019-06-10 https://sqlite.org/src/info/fd76310a5e843e07 + ** 2019-06-14 https://sqlite.org/src/info/ce8717f0885af975 + ** 2019-09-03 https://sqlite.org/src/info/0f0428096f17252a + */ + if( pLeft->op!=TK_COLUMN + || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT + || (ALWAYS( ExprUseYTab(pLeft) ) + && ALWAYS(pLeft->y.pTab) + && IsVirtual(pLeft->y.pTab)) /* Might be numeric */ + ){ + int isNum; + double rDummy; + isNum = sqlite3AtoF(zNew, &rDummy, iTo, SQLITE_UTF8); + if( isNum<=0 ){ + if( iTo==1 && zNew[0]=='-' ){ + isNum = +1; + }else{ + zNew[iTo-1]++; + isNum = sqlite3AtoF(zNew, &rDummy, iTo, SQLITE_UTF8); + zNew[iTo-1]--; + } + } + if( isNum>0 ){ + sqlite3ExprDelete(db, pPrefix); + sqlite3ValueFree(pVal); + return 0; + } + } + } *ppPrefix = pPrefix; + + /* If the RHS pattern is a bound parameter, make arrangements to + ** reprepare the statement when that parameter is rebound */ if( op==TK_VARIABLE ){ Vdbe *v = pParse->pVdbe; sqlite3VdbeSetVarmask(v, pRight->iColumn); + assert( !ExprHasProperty(pRight, EP_IntValue) ); if( *pisComplete && pRight->u.zToken[1] ){ /* If the rhs of the LIKE expression is a variable, and the current ** value of the variable means there is no need to invoke the LIKE ** function, then no OP_Variable will be added to the program. ** This causes problems for the sqlite3_bind_parameter_name() ** API. To work around them, add a dummy OP_Variable here. - */ + */ int r1 = sqlite3GetTempReg(pParse); sqlite3ExprCodeTarget(pParse, pRight, r1); sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0); @@ -277,48 +345,139 @@ static int isLikeOrGlob( #ifndef SQLITE_OMIT_VIRTUALTABLE /* -** Check to see if the given expression is of the form +** Check to see if the pExpr expression is a form that needs to be passed +** to the xBestIndex method of virtual tables. Forms of interest include: ** -** column OP expr +** Expression Virtual Table Operator +** ----------------------- --------------------------------- +** 1. column MATCH expr SQLITE_INDEX_CONSTRAINT_MATCH +** 2. column GLOB expr SQLITE_INDEX_CONSTRAINT_GLOB +** 3. column LIKE expr SQLITE_INDEX_CONSTRAINT_LIKE +** 4. column REGEXP expr SQLITE_INDEX_CONSTRAINT_REGEXP +** 5. column != expr SQLITE_INDEX_CONSTRAINT_NE +** 6. expr != column SQLITE_INDEX_CONSTRAINT_NE +** 7. column IS NOT expr SQLITE_INDEX_CONSTRAINT_ISNOT +** 8. expr IS NOT column SQLITE_INDEX_CONSTRAINT_ISNOT +** 9. column IS NOT NULL SQLITE_INDEX_CONSTRAINT_ISNOTNULL ** -** where OP is one of MATCH, GLOB, LIKE or REGEXP and "column" is a -** column of a virtual table. +** In every case, "column" must be a column of a virtual table. If there +** is a match, set *ppLeft to the "column" expression, set *ppRight to the +** "expr" expression (even though in forms (6) and (8) the column is on the +** right and the expression is on the left). Also set *peOp2 to the +** appropriate virtual table operator. The return value is 1 or 2 if there +** is a match. The usual return is 1, but if the RHS is also a column +** of virtual table in forms (5) or (7) then return 2. ** -** If it is then return TRUE. If not, return FALSE. +** If the expression matches none of the patterns above, return 0. */ -static int isMatchOfColumn( +static int isAuxiliaryVtabOperator( + sqlite3 *db, /* Parsing context */ Expr *pExpr, /* Test this expression */ - unsigned char *peOp2 /* OUT: 0 for MATCH, or else an op2 value */ + unsigned char *peOp2, /* OUT: 0 for MATCH, or else an op2 value */ + Expr **ppLeft, /* Column expression to left of MATCH/op2 */ + Expr **ppRight /* Expression to left of MATCH/op2 */ ){ - struct Op2 { - const char *zOp; - unsigned char eOp2; - } aOp[] = { - { "match", SQLITE_INDEX_CONSTRAINT_MATCH }, - { "glob", SQLITE_INDEX_CONSTRAINT_GLOB }, - { "like", SQLITE_INDEX_CONSTRAINT_LIKE }, - { "regexp", SQLITE_INDEX_CONSTRAINT_REGEXP } - }; - ExprList *pList; - Expr *pCol; /* Column reference */ - int i; + if( pExpr->op==TK_FUNCTION ){ + static const struct Op2 { + const char *zOp; + unsigned char eOp2; + } aOp[] = { + { "match", SQLITE_INDEX_CONSTRAINT_MATCH }, + { "glob", SQLITE_INDEX_CONSTRAINT_GLOB }, + { "like", SQLITE_INDEX_CONSTRAINT_LIKE }, + { "regexp", SQLITE_INDEX_CONSTRAINT_REGEXP } + }; + ExprList *pList; + Expr *pCol; /* Column reference */ + int i; - if( pExpr->op!=TK_FUNCTION ){ - return 0; - } - pList = pExpr->x.pList; - if( pList==0 || pList->nExpr!=2 ){ - return 0; - } - pCol = pList->a[1].pExpr; - if( pCol->op!=TK_COLUMN || !IsVirtual(pCol->pTab) ){ + assert( ExprUseXList(pExpr) ); + pList = pExpr->x.pList; + if( pList==0 || pList->nExpr!=2 ){ + return 0; + } + + /* Built-in operators MATCH, GLOB, LIKE, and REGEXP attach to a + ** virtual table on their second argument, which is the same as + ** the left-hand side operand in their in-fix form. + ** + ** vtab_column MATCH expression + ** MATCH(expression,vtab_column) + */ + pCol = pList->a[1].pExpr; + assert( pCol->op!=TK_COLUMN || (ExprUseYTab(pCol) && pCol->y.pTab!=0) ); + if( ExprIsVtab(pCol) ){ + for(i=0; i<ArraySize(aOp); i++){ + assert( !ExprHasProperty(pExpr, EP_IntValue) ); + if( sqlite3StrICmp(pExpr->u.zToken, aOp[i].zOp)==0 ){ + *peOp2 = aOp[i].eOp2; + *ppRight = pList->a[0].pExpr; + *ppLeft = pCol; + return 1; + } + } + } + + /* We can also match against the first column of overloaded + ** functions where xFindFunction returns a value of at least + ** SQLITE_INDEX_CONSTRAINT_FUNCTION. + ** + ** OVERLOADED(vtab_column,expression) + ** + ** Historically, xFindFunction expected to see lower-case function + ** names. But for this use case, xFindFunction is expected to deal + ** with function names in an arbitrary case. + */ + pCol = pList->a[0].pExpr; + assert( pCol->op!=TK_COLUMN || ExprUseYTab(pCol) ); + assert( pCol->op!=TK_COLUMN || (ExprUseYTab(pCol) && pCol->y.pTab!=0) ); + if( ExprIsVtab(pCol) ){ + sqlite3_vtab *pVtab; + sqlite3_module *pMod; + void (*xNotUsed)(sqlite3_context*,int,sqlite3_value**); + void *pNotUsed; + pVtab = sqlite3GetVTable(db, pCol->y.pTab)->pVtab; + assert( pVtab!=0 ); + assert( pVtab->pModule!=0 ); + assert( !ExprHasProperty(pExpr, EP_IntValue) ); + pMod = (sqlite3_module *)pVtab->pModule; + if( pMod->xFindFunction!=0 ){ + i = pMod->xFindFunction(pVtab,2, pExpr->u.zToken, &xNotUsed, &pNotUsed); + if( i>=SQLITE_INDEX_CONSTRAINT_FUNCTION ){ + *peOp2 = i; + *ppRight = pList->a[1].pExpr; + *ppLeft = pCol; + return 1; + } + } + } + }else if( pExpr->op>=TK_EQ ){ + /* Comparison operators are a common case. Save a few comparisons for + ** that common case by terminating early. */ + assert( TK_NE < TK_EQ ); + assert( TK_ISNOT < TK_EQ ); + assert( TK_NOTNULL < TK_EQ ); return 0; - } - for(i=0; i<ArraySize(aOp); i++){ - if( sqlite3StrICmp(pExpr->u.zToken, aOp[i].zOp)==0 ){ - *peOp2 = aOp[i].eOp2; - return 1; + }else if( pExpr->op==TK_NE || pExpr->op==TK_ISNOT || pExpr->op==TK_NOTNULL ){ + int res = 0; + Expr *pLeft = pExpr->pLeft; + Expr *pRight = pExpr->pRight; + assert( pLeft->op!=TK_COLUMN || (ExprUseYTab(pLeft) && pLeft->y.pTab!=0) ); + if( ExprIsVtab(pLeft) ){ + res++; + } + assert( pRight==0 || pRight->op!=TK_COLUMN + || (ExprUseYTab(pRight) && pRight->y.pTab!=0) ); + if( pRight && ExprIsVtab(pRight) ){ + res++; + SWAP(Expr*, pLeft, pRight); } + *ppLeft = pLeft; + *ppRight = pRight; + if( pExpr->op==TK_NE ) *peOp2 = SQLITE_INDEX_CONSTRAINT_NE; + if( pExpr->op==TK_ISNOT ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOT; + if( pExpr->op==TK_NOTNULL ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOTNULL; + return res; } return 0; } @@ -329,9 +488,9 @@ static int isMatchOfColumn( ** a join, then transfer the appropriate markings over to derived. */ static void transferJoinMarkings(Expr *pDerived, Expr *pBase){ - if( pDerived ){ - pDerived->flags |= pBase->flags & EP_FromJoin; - pDerived->iRightJoinTable = pBase->iRightJoinTable; + if( pDerived && ExprHasProperty(pBase, EP_OuterON|EP_InnerON) ){ + pDerived->flags |= pBase->flags & (EP_OuterON|EP_InnerON); + pDerived->w.iJoin = pBase->w.iJoin; } } @@ -377,7 +536,7 @@ static WhereTerm *whereNthSubterm(WhereTerm *pTerm, int N){ ** ** The following is NOT generated: ** -** x<y OR x>y --> x!=y +** x<y OR x>y --> x!=y */ static void whereCombineDisjuncts( SrcList *pSrc, /* the FROM clause */ @@ -391,14 +550,15 @@ static void whereCombineDisjuncts( int op; /* Operator for the combined expression */ int idxNew; /* Index in pWC of the next virtual term */ + if( (pOne->wtFlags | pTwo->wtFlags) & TERM_VNULL ) return; if( (pOne->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return; if( (pTwo->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return; if( (eOp & (WO_EQ|WO_LT|WO_LE))!=eOp && (eOp & (WO_EQ|WO_GT|WO_GE))!=eOp ) return; assert( pOne->pExpr->pLeft!=0 && pOne->pExpr->pRight!=0 ); assert( pTwo->pExpr->pLeft!=0 && pTwo->pExpr->pRight!=0 ); - if( sqlite3ExprCompare(pOne->pExpr->pLeft, pTwo->pExpr->pLeft, -1) ) return; - if( sqlite3ExprCompare(pOne->pExpr->pRight, pTwo->pExpr->pRight, -1) )return; + if( sqlite3ExprCompare(0,pOne->pExpr->pLeft, pTwo->pExpr->pLeft, -1) ) return; + if( sqlite3ExprCompare(0,pOne->pExpr->pRight, pTwo->pExpr->pRight,-1) )return; /* If we reach this point, it means the two subterms can be combined */ if( (eOp & (eOp-1))!=0 ){ if( eOp & (WO_LT|WO_LE) ){ @@ -474,10 +634,10 @@ static void whereCombineDisjuncts( ** WhereTerm.u.pOrInfo->indexable |= the cursor number for table T ** ** A subterm is "indexable" if it is of the form -** "T.C <op> <expr>" where C is any column of table T and +** "T.C <op> <expr>" where C is any column of table T and ** <op> is one of "=", "<", "<=", ">", ">=", "IS NULL", or "IN". ** A subterm is also indexable if it is an AND of two or more -** subsubterms at least one of which is indexable. Indexable AND +** subsubterms at least one of which is indexable. Indexable AND ** subterms have their eOperator set to WO_AND and they have ** u.pAndInfo set to a dynamically allocated WhereAndTerm object. ** @@ -533,6 +693,7 @@ static void exprAnalyzeOrTerm( if( pOrInfo==0 ) return; pTerm->wtFlags |= TERM_ORINFO; pOrWc = &pOrInfo->wc; + memset(pOrWc->aStatic, 0, sizeof(pOrWc->aStatic)); sqlite3WhereClauseInit(pOrWc, pWInfo); sqlite3WhereSplit(pOrWc, pExpr, TK_OR); sqlite3WhereExprAnalyze(pSrc, pOrWc); @@ -558,7 +719,9 @@ static void exprAnalyzeOrTerm( pOrTerm->u.pAndInfo = pAndInfo; pOrTerm->wtFlags |= TERM_ANDINFO; pOrTerm->eOperator = WO_AND; + pOrTerm->leftCursor = -1; pAndWC = &pAndInfo->wc; + memset(pAndWC->aStatic, 0, sizeof(pAndWC->aStatic)); sqlite3WhereClauseInit(pAndWC, pWC->pWInfo); sqlite3WhereSplit(pAndWC, pOrTerm->pExpr, TK_AND); sqlite3WhereExprAnalyze(pSrc, pAndWC); @@ -566,7 +729,9 @@ static void exprAnalyzeOrTerm( if( !db->mallocFailed ){ for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){ assert( pAndTerm->pExpr ); - if( allowedOp(pAndTerm->pExpr->op) ){ + if( allowedOp(pAndTerm->pExpr->op) + || pAndTerm->eOperator==WO_AUX + ){ b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pAndTerm->leftCursor); } } @@ -597,7 +762,11 @@ static void exprAnalyzeOrTerm( ** empty. */ pOrInfo->indexable = indexable; - pTerm->eOperator = indexable==0 ? 0 : WO_OR; + pTerm->eOperator = WO_OR; + pTerm->leftCursor = -1; + if( indexable ){ + pWC->hasOr = 1; + } /* For a two-way OR, attempt to implementation case 2. */ @@ -647,10 +816,11 @@ static void exprAnalyzeOrTerm( ** and column is found but leave okToChngToIN false if not found. */ for(j=0; j<2 && !okToChngToIN; j++){ + Expr *pLeft = 0; pOrTerm = pOrWc->a; for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){ assert( pOrTerm->eOperator & WO_EQ ); - pOrTerm->wtFlags &= ~TERM_OR_OK; + pOrTerm->wtFlags &= ~TERM_OK; if( pOrTerm->leftCursor==iCursor ){ /* This is the 2-bit case and we are on the second iteration and ** current term is from the first iteration. So skip this term. */ @@ -661,15 +831,17 @@ static void exprAnalyzeOrTerm( pOrTerm->leftCursor))==0 ){ /* This term must be of the form t1.a==t2.b where t2 is in the ** chngToIN set but t1 is not. This term will be either preceded - ** or follwed by an inverted copy (t2.b==t1.a). Skip this term + ** or followed by an inverted copy (t2.b==t1.a). Skip this term ** and use its inversion. */ testcase( pOrTerm->wtFlags & TERM_COPIED ); testcase( pOrTerm->wtFlags & TERM_VIRTUAL ); assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) ); continue; } - iColumn = pOrTerm->u.leftColumn; + assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 ); + iColumn = pOrTerm->u.x.leftColumn; iCursor = pOrTerm->leftCursor; + pLeft = pOrTerm->pExpr->pLeft; break; } if( i<0 ){ @@ -687,9 +859,12 @@ static void exprAnalyzeOrTerm( okToChngToIN = 1; for(; i>=0 && okToChngToIN; i--, pOrTerm++){ assert( pOrTerm->eOperator & WO_EQ ); + assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 ); if( pOrTerm->leftCursor!=iCursor ){ - pOrTerm->wtFlags &= ~TERM_OR_OK; - }else if( pOrTerm->u.leftColumn!=iColumn ){ + pOrTerm->wtFlags &= ~TERM_OK; + }else if( pOrTerm->u.x.leftColumn!=iColumn || (iColumn==XN_EXPR + && sqlite3ExprCompare(pParse, pOrTerm->pExpr->pLeft, pLeft, -1) + )){ okToChngToIN = 0; }else{ int affLeft, affRight; @@ -702,14 +877,14 @@ static void exprAnalyzeOrTerm( if( affRight!=0 && affRight!=affLeft ){ okToChngToIN = 0; }else{ - pOrTerm->wtFlags |= TERM_OR_OK; + pOrTerm->wtFlags |= TERM_OK; } } } } /* At this point, okToChngToIN is true if original pTerm satisfies - ** case 1. In that case, construct a new virtual term that is + ** case 1. In that case, construct a new virtual term that is ** pTerm converted into an IN operator. */ if( okToChngToIN ){ @@ -719,31 +894,31 @@ static void exprAnalyzeOrTerm( Expr *pNew; /* The complete IN operator */ for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){ - if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue; + if( (pOrTerm->wtFlags & TERM_OK)==0 ) continue; assert( pOrTerm->eOperator & WO_EQ ); + assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 ); assert( pOrTerm->leftCursor==iCursor ); - assert( pOrTerm->u.leftColumn==iColumn ); + assert( pOrTerm->u.x.leftColumn==iColumn ); pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0); pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup); pLeft = pOrTerm->pExpr->pLeft; } assert( pLeft!=0 ); pDup = sqlite3ExprDup(db, pLeft, 0); - pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0, 0); + pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0); if( pNew ){ int idxNew; transferJoinMarkings(pNew, pExpr); - assert( !ExprHasProperty(pNew, EP_xIsSelect) ); + assert( ExprUseXList(pNew) ); pNew->x.pList = pList; idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC); testcase( idxNew==0 ); exprAnalyze(pSrc, pWC, idxNew); - pTerm = &pWC->a[idxTerm]; + /* pTerm = &pWC->a[idxTerm]; // would be needed if pTerm where reused */ markTermAsChild(pWC, idxNew, idxTerm); }else{ sqlite3ExprListDelete(db, pList); } - pTerm->eOperator = WO_NOOP; /* case 1 trumps case 3 */ } } } @@ -756,37 +931,42 @@ static void exprAnalyzeOrTerm( ** 1. The SQLITE_Transitive optimization must be enabled ** 2. Must be either an == or an IS operator ** 3. Not originating in the ON clause of an OUTER JOIN -** 4. The affinities of A and B must be compatible -** 5a. Both operands use the same collating sequence OR -** 5b. The overall collating sequence is BINARY +** 4. The operator is not IS or else the query does not contain RIGHT JOIN +** 5. The affinities of A and B must be compatible +** 6a. Both operands use the same collating sequence OR +** 6b. The overall collating sequence is BINARY ** If this routine returns TRUE, that means that the RHS can be substituted ** for the LHS anyplace else in the WHERE clause where the LHS column occurs. ** This is an optimization. No harm comes from returning 0. But if 1 is ** returned when it should not be, then incorrect answers might result. */ -static int termIsEquivalence(Parse *pParse, Expr *pExpr){ +static int termIsEquivalence(Parse *pParse, Expr *pExpr, SrcList *pSrc){ char aff1, aff2; CollSeq *pColl; - const char *zColl1, *zColl2; - if( !OptimizationEnabled(pParse->db, SQLITE_Transitive) ) return 0; - if( pExpr->op!=TK_EQ && pExpr->op!=TK_IS ) return 0; - if( ExprHasProperty(pExpr, EP_FromJoin) ) return 0; + if( !OptimizationEnabled(pParse->db, SQLITE_Transitive) ) return 0; /* (1) */ + if( pExpr->op!=TK_EQ && pExpr->op!=TK_IS ) return 0; /* (2) */ + if( ExprHasProperty(pExpr, EP_OuterON) ) return 0; /* (3) */ + assert( pSrc!=0 ); + if( pExpr->op==TK_IS + && pSrc->nSrc>=2 + && (pSrc->a[0].fg.jointype & JT_LTORJ)!=0 + ){ + return 0; /* (4) */ + } aff1 = sqlite3ExprAffinity(pExpr->pLeft); aff2 = sqlite3ExprAffinity(pExpr->pRight); if( aff1!=aff2 && (!sqlite3IsNumericAffinity(aff1) || !sqlite3IsNumericAffinity(aff2)) ){ - return 0; + return 0; /* (5) */ } - pColl = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pRight); - if( pColl==0 || sqlite3StrICmp(pColl->zName, "BINARY")==0 ) return 1; - pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft); - /* Since pLeft and pRight are both a column references, their collating - ** sequence should always be defined. */ - zColl1 = ALWAYS(pColl) ? pColl->zName : 0; - pColl = sqlite3ExprCollSeq(pParse, pExpr->pRight); - zColl2 = ALWAYS(pColl) ? pColl->zName : 0; - return sqlite3StrICmp(zColl1, zColl2)==0; + pColl = sqlite3ExprCompareCollSeq(pParse, pExpr); + if( !sqlite3IsBinary(pColl) + && !sqlite3ExprCollSeqMatch(pParse, pExpr->pLeft, pExpr->pRight) + ){ + return 0; /* (6) */ + } + return 1; } /* @@ -806,8 +986,15 @@ static Bitmask exprSelectUsage(WhereMaskSet *pMaskSet, Select *pS){ if( ALWAYS(pSrc!=0) ){ int i; for(i=0; i<pSrc->nSrc; i++){ - mask |= exprSelectUsage(pMaskSet, pSrc->a[i].pSelect); - mask |= sqlite3WhereExprUsage(pMaskSet, pSrc->a[i].pOn); + if( pSrc->a[i].fg.isSubquery ){ + mask |= exprSelectUsage(pMaskSet, pSrc->a[i].u4.pSubq->pSelect); + } + if( pSrc->a[i].fg.isUsing==0 ){ + mask |= sqlite3WhereExprUsage(pMaskSet, pSrc->a[i].u3.pOn); + } + if( pSrc->a[i].fg.isTabFunc ){ + mask |= sqlite3WhereExprListUsage(pMaskSet, pSrc->a[i].u1.pFuncArg); + } } } pS = pS->pPrior; @@ -819,47 +1006,80 @@ static Bitmask exprSelectUsage(WhereMaskSet *pMaskSet, Select *pS){ ** Expression pExpr is one operand of a comparison operator that might ** be useful for indexing. This routine checks to see if pExpr appears ** in any index. Return TRUE (1) if pExpr is an indexed term and return -** FALSE (0) if not. If TRUE is returned, also set *piCur to the cursor -** number of the table that is indexed and *piColumn to the column number -** of the column that is indexed, or -2 if an expression is being indexed. +** FALSE (0) if not. If TRUE is returned, also set aiCurCol[0] to the cursor +** number of the table that is indexed and aiCurCol[1] to the column number +** of the column that is indexed, or XN_EXPR (-2) if an expression is being +** indexed. ** ** If pExpr is a TK_COLUMN column reference, then this routine always returns ** true even if that particular column is not indexed, because the column ** might be added to an automatic index later. */ -static int exprMightBeIndexed( +static SQLITE_NOINLINE int exprMightBeIndexed2( SrcList *pFrom, /* The FROM clause */ - Bitmask mPrereq, /* Bitmask of FROM clause terms referenced by pExpr */ + int *aiCurCol, /* Write the referenced table cursor and column here */ Expr *pExpr, /* An operand of a comparison operator */ - int *piCur, /* Write the referenced table cursor number here */ - int *piColumn /* Write the referenced table column number here */ + int j /* Start looking with the j-th pFrom entry */ ){ Index *pIdx; int i; int iCur; + do{ + iCur = pFrom->a[j].iCursor; + for(pIdx=pFrom->a[j].pSTab->pIndex; pIdx; pIdx=pIdx->pNext){ + if( pIdx->aColExpr==0 ) continue; + for(i=0; i<pIdx->nKeyCol; i++){ + if( pIdx->aiColumn[i]!=XN_EXPR ) continue; + assert( pIdx->bHasExpr ); + if( sqlite3ExprCompareSkip(pExpr,pIdx->aColExpr->a[i].pExpr,iCur)==0 + && !sqlite3ExprIsConstant(0,pIdx->aColExpr->a[i].pExpr) + ){ + aiCurCol[0] = iCur; + aiCurCol[1] = XN_EXPR; + return 1; + } + } + } + }while( ++j < pFrom->nSrc ); + return 0; +} +static int exprMightBeIndexed( + SrcList *pFrom, /* The FROM clause */ + int *aiCurCol, /* Write the referenced table cursor & column here */ + Expr *pExpr, /* An operand of a comparison operator */ + int op /* The specific comparison operator */ +){ + int i; + + /* If this expression is a vector to the left or right of a + ** inequality constraint (>, <, >= or <=), perform the processing + ** on the first element of the vector. */ + assert( TK_GT+1==TK_LE && TK_GT+2==TK_LT && TK_GT+3==TK_GE ); + assert( TK_IS<TK_GE && TK_ISNULL<TK_GE && TK_IN<TK_GE ); + assert( op<=TK_GE ); + if( pExpr->op==TK_VECTOR && (op>=TK_GT && ALWAYS(op<=TK_GE)) ){ + assert( ExprUseXList(pExpr) ); + pExpr = pExpr->x.pList->a[0].pExpr; + } + if( pExpr->op==TK_COLUMN ){ - *piCur = pExpr->iTable; - *piColumn = pExpr->iColumn; + aiCurCol[0] = pExpr->iTable; + aiCurCol[1] = pExpr->iColumn; return 1; } - if( mPrereq==0 ) return 0; /* No table references */ - if( (mPrereq&(mPrereq-1))!=0 ) return 0; /* Refs more than one table */ - for(i=0; mPrereq>1; i++, mPrereq>>=1){} - iCur = pFrom->a[i].iCursor; - for(pIdx=pFrom->a[i].pTab->pIndex; pIdx; pIdx=pIdx->pNext){ - if( pIdx->aColExpr==0 ) continue; - for(i=0; i<pIdx->nKeyCol; i++){ - if( pIdx->aiColumn[i]!=(-2) ) continue; - if( sqlite3ExprCompare(pExpr, pIdx->aColExpr->a[i].pExpr, iCur)==0 ){ - *piCur = iCur; - *piColumn = -2; - return 1; + + for(i=0; i<pFrom->nSrc; i++){ + Index *pIdx; + for(pIdx=pFrom->a[i].pSTab->pIndex; pIdx; pIdx=pIdx->pNext){ + if( pIdx->aColExpr ){ + return exprMightBeIndexed2(pFrom,aiCurCol,pExpr,i); } } } return 0; } + /* ** The input to this routine is an WhereTerm structure with only the ** "pExpr" field filled in. The job of this routine is to analyze the @@ -887,8 +1107,8 @@ static void exprAnalyze( WhereTerm *pTerm; /* The term to be analyzed */ WhereMaskSet *pMaskSet; /* Set of table index masks */ Expr *pExpr; /* The expression to be analyzed */ - Bitmask prereqLeft; /* Prerequesites of the pExpr->pLeft */ - Bitmask prereqAll; /* Prerequesites of pExpr */ + Bitmask prereqLeft; /* Prerequisites of the pExpr->pLeft */ + Bitmask prereqAll; /* Prerequisites of pExpr */ Bitmask extraRight = 0; /* Extra dependencies on LEFT JOIN */ Expr *pStr1 = 0; /* RHS of LIKE/GLOB operator */ int isComplete = 0; /* RHS of LIKE/GLOB ends with wildcard */ @@ -896,57 +1116,96 @@ static void exprAnalyze( int op; /* Top-level operator. pExpr->op */ Parse *pParse = pWInfo->pParse; /* Parsing context */ sqlite3 *db = pParse->db; /* Database connection */ - unsigned char eOp2; /* op2 value for LIKE/REGEXP/GLOB */ + unsigned char eOp2 = 0; /* op2 value for LIKE/REGEXP/GLOB */ + int nLeft; /* Number of elements on left side vector */ if( db->mallocFailed ){ return; } + assert( pWC->nTerm > idxTerm ); pTerm = &pWC->a[idxTerm]; +#ifdef SQLITE_DEBUG + pTerm->iTerm = idxTerm; +#endif pMaskSet = &pWInfo->sMaskSet; pExpr = pTerm->pExpr; + assert( pExpr!=0 ); /* Because malloc() has not failed */ assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE ); + pMaskSet->bVarSelect = 0; prereqLeft = sqlite3WhereExprUsage(pMaskSet, pExpr->pLeft); op = pExpr->op; if( op==TK_IN ){ assert( pExpr->pRight==0 ); - if( ExprHasProperty(pExpr, EP_xIsSelect) ){ + if( sqlite3ExprCheckIN(pParse, pExpr) ) return; + if( ExprUseXSelect(pExpr) ){ pTerm->prereqRight = exprSelectUsage(pMaskSet, pExpr->x.pSelect); }else{ pTerm->prereqRight = sqlite3WhereExprListUsage(pMaskSet, pExpr->x.pList); } - }else if( op==TK_ISNULL ){ - pTerm->prereqRight = 0; + prereqAll = prereqLeft | pTerm->prereqRight; }else{ pTerm->prereqRight = sqlite3WhereExprUsage(pMaskSet, pExpr->pRight); + if( pExpr->pLeft==0 + || ExprHasProperty(pExpr, EP_xIsSelect|EP_IfNullRow) + || pExpr->x.pList!=0 + ){ + prereqAll = sqlite3WhereExprUsageNN(pMaskSet, pExpr); + }else{ + prereqAll = prereqLeft | pTerm->prereqRight; + } + } + if( pMaskSet->bVarSelect ) pTerm->wtFlags |= TERM_VARSELECT; + +#ifdef SQLITE_DEBUG + if( prereqAll!=sqlite3WhereExprUsageNN(pMaskSet, pExpr) ){ + printf("\n*** Incorrect prereqAll computed for:\n"); + sqlite3TreeViewExpr(0,pExpr,0); + assert( 0 ); } - prereqAll = sqlite3WhereExprUsage(pMaskSet, pExpr); - if( ExprHasProperty(pExpr, EP_FromJoin) ){ - Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->iRightJoinTable); - prereqAll |= x; - extraRight = x-1; /* ON clause terms may not be used with an index - ** on left table of a LEFT JOIN. Ticket #3015 */ +#endif + + if( ExprHasProperty(pExpr, EP_OuterON|EP_InnerON) ){ + Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->w.iJoin); + if( ExprHasProperty(pExpr, EP_OuterON) ){ + prereqAll |= x; + extraRight = x-1; /* ON clause terms may not be used with an index + ** on left table of a LEFT JOIN. Ticket #3015 */ + }else if( (prereqAll>>1)>=x ){ + ExprClearProperty(pExpr, EP_InnerON); + } } pTerm->prereqAll = prereqAll; pTerm->leftCursor = -1; pTerm->iParent = -1; pTerm->eOperator = 0; if( allowedOp(op) ){ - int iCur, iColumn; + int aiCurCol[2]; Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft); Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight); u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL : WO_EQUIV; - if( exprMightBeIndexed(pSrc, prereqLeft, pLeft, &iCur, &iColumn) ){ - pTerm->leftCursor = iCur; - pTerm->u.leftColumn = iColumn; + + if( pTerm->u.x.iField>0 ){ + assert( op==TK_IN ); + assert( pLeft->op==TK_VECTOR ); + assert( ExprUseXList(pLeft) ); + pLeft = pLeft->x.pList->a[pTerm->u.x.iField-1].pExpr; + } + + if( exprMightBeIndexed(pSrc, aiCurCol, pLeft, op) ){ + pTerm->leftCursor = aiCurCol[0]; + assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 ); + pTerm->u.x.leftColumn = aiCurCol[1]; pTerm->eOperator = operatorMask(op) & opMask; } if( op==TK_IS ) pTerm->wtFlags |= TERM_IS; - if( pRight - && exprMightBeIndexed(pSrc, pTerm->prereqRight, pRight, &iCur, &iColumn) + if( pRight + && exprMightBeIndexed(pSrc, aiCurCol, pRight, op) + && !ExprHasProperty(pRight, EP_FixedCol) ){ WhereTerm *pNew; Expr *pDup; u16 eExtraOp = 0; /* Extra bits for pNew->eOperator */ + assert( pTerm->u.x.iField==0 ); if( pTerm->leftCursor>=0 ){ int idxNew; pDup = sqlite3ExprDup(db, pExpr, 0); @@ -961,8 +1220,8 @@ static void exprAnalyze( if( op==TK_IS ) pNew->wtFlags |= TERM_IS; pTerm = &pWC->a[idxTerm]; pTerm->wtFlags |= TERM_COPIED; - - if( termIsEquivalence(pParse, pDup) ){ + assert( pWInfo->pTabList!=0 ); + if( termIsEquivalence(pParse, pDup, pWInfo->pTabList) ){ pTerm->eOperator |= WO_EQUIV; eExtraOp = WO_EQUIV; } @@ -970,13 +1229,25 @@ static void exprAnalyze( pDup = pExpr; pNew = pTerm; } - exprCommute(pParse, pDup); - pNew->leftCursor = iCur; - pNew->u.leftColumn = iColumn; + pNew->wtFlags |= exprCommute(pParse, pDup); + pNew->leftCursor = aiCurCol[0]; + assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 ); + pNew->u.x.leftColumn = aiCurCol[1]; testcase( (prereqLeft | extraRight) != prereqLeft ); pNew->prereqRight = prereqLeft | extraRight; pNew->prereqAll = prereqAll; pNew->eOperator = (operatorMask(pDup->op) + eExtraOp) & opMask; + }else + if( op==TK_ISNULL + && !ExprHasProperty(pExpr,EP_OuterON) + && 0==sqlite3ExprCanBeNull(pLeft) + ){ + assert( !ExprHasProperty(pExpr, EP_IntValue) ); + pExpr->op = TK_TRUEFALSE; /* See tag-20230504-1 */ + pExpr->u.zToken = "false"; + ExprSetProperty(pExpr, EP_IsFalse); + pTerm->prereqAll = 0; + pTerm->eOperator = 0; } } @@ -997,17 +1268,19 @@ static void exprAnalyze( ** BETWEEN term is skipped. */ else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){ - ExprList *pList = pExpr->x.pList; + ExprList *pList; int i; static const u8 ops[] = {TK_GE, TK_LE}; + assert( ExprUseXList(pExpr) ); + pList = pExpr->x.pList; assert( pList!=0 ); assert( pList->nExpr==2 ); for(i=0; i<2; i++){ Expr *pNewExpr; int idxNew; - pNewExpr = sqlite3PExpr(pParse, ops[i], + pNewExpr = sqlite3PExpr(pParse, ops[i], sqlite3ExprDup(db, pExpr->pLeft, 0), - sqlite3ExprDup(db, pList->a[i].pExpr, 0), 0); + sqlite3ExprDup(db, pList->a[i].pExpr, 0)); transferJoinMarkings(pNewExpr, pExpr); idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC); testcase( idxNew==0 ); @@ -1028,6 +1301,42 @@ static void exprAnalyze( pTerm = &pWC->a[idxTerm]; } #endif /* SQLITE_OMIT_OR_OPTIMIZATION */ + /* The form "x IS NOT NULL" can sometimes be evaluated more efficiently + ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a + ** virtual term of that form. + ** + ** The virtual term must be tagged with TERM_VNULL. + */ + else if( pExpr->op==TK_NOTNULL ){ + if( pExpr->pLeft->op==TK_COLUMN + && pExpr->pLeft->iColumn>=0 + && !ExprHasProperty(pExpr, EP_OuterON) + ){ + Expr *pNewExpr; + Expr *pLeft = pExpr->pLeft; + int idxNew; + WhereTerm *pNewTerm; + + pNewExpr = sqlite3PExpr(pParse, TK_GT, + sqlite3ExprDup(db, pLeft, 0), + sqlite3ExprAlloc(db, TK_NULL, 0, 0)); + + idxNew = whereClauseInsert(pWC, pNewExpr, + TERM_VIRTUAL|TERM_DYNAMIC|TERM_VNULL); + if( idxNew ){ + pNewTerm = &pWC->a[idxNew]; + pNewTerm->prereqRight = 0; + pNewTerm->leftCursor = pLeft->iTable; + pNewTerm->u.x.leftColumn = pLeft->iColumn; + pNewTerm->eOperator = WO_GT; + markTermAsChild(pWC, idxNew, idxTerm); + pTerm = &pWC->a[idxTerm]; + pTerm->wtFlags |= TERM_COPIED; + pNewTerm->prereqAll = pTerm->prereqAll; + } + } + } + #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION /* Add constraints to reduce the search space on a LIKE or GLOB @@ -1043,7 +1352,8 @@ static void exprAnalyze( ** bound is made all lowercase so that the bounds also work when comparing ** BLOBs. */ - if( pWC->op==TK_AND + else if( pExpr->op==TK_FUNCTION + && pWC->op==TK_AND && isLikeOrGlob(pParse, pExpr, &pStr1, &isComplete, &noCase) ){ Expr *pLeft; /* LHS of LIKE/GLOB operator */ @@ -1055,8 +1365,12 @@ static void exprAnalyze( const char *zCollSeqName; /* Name of collating sequence */ const u16 wtFlags = TERM_LIKEOPT | TERM_VIRTUAL | TERM_DYNAMIC; + assert( ExprUseXList(pExpr) ); pLeft = pExpr->x.pList->a[1].pExpr; pStr2 = sqlite3ExprDup(db, pStr1, 0); + assert( pStr1==0 || !ExprHasProperty(pStr1, EP_IntValue) ); + assert( pStr2==0 || !ExprHasProperty(pStr2, EP_IntValue) ); + /* Convert the lower bound to upper-case and the upper bound to ** lower-case (upper-case is less than lower-case in ASCII) so that @@ -1073,37 +1387,43 @@ static void exprAnalyze( } if( !db->mallocFailed ){ - u8 c, *pC; /* Last character before the first wildcard */ + u8 *pC; /* Last character before the first wildcard */ pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1]; - c = *pC; if( noCase ){ /* The point is to increment the last character before the first ** wildcard. But if we increment '@', that will push it into the - ** alphabetic range where case conversions will mess up the + ** alphabetic range where case conversions will mess up the ** inequality. To avoid this, make sure to also run the full ** LIKE on all candidate expressions by clearing the isComplete flag */ - if( c=='A'-1 ) isComplete = 0; - c = sqlite3UpperToLower[c]; + if( *pC=='A'-1 ) isComplete = 0; + *pC = sqlite3UpperToLower[*pC]; } - *pC = c + 1; + + /* Increment the value of the last utf8 character in the prefix. */ + while( *pC==0xBF && pC>(u8*)pStr2->u.zToken ){ + *pC = 0x80; + pC--; + } + assert( *pC!=0xFF ); /* isLikeOrGlob() guarantees this */ + (*pC)++; } - zCollSeqName = noCase ? "NOCASE" : "BINARY"; + zCollSeqName = noCase ? "NOCASE" : sqlite3StrBINARY; pNewExpr1 = sqlite3ExprDup(db, pLeft, 0); pNewExpr1 = sqlite3PExpr(pParse, TK_GE, sqlite3ExprAddCollateString(pParse,pNewExpr1,zCollSeqName), - pStr1, 0); + pStr1); transferJoinMarkings(pNewExpr1, pExpr); idxNew1 = whereClauseInsert(pWC, pNewExpr1, wtFlags); testcase( idxNew1==0 ); - exprAnalyze(pSrc, pWC, idxNew1); pNewExpr2 = sqlite3ExprDup(db, pLeft, 0); pNewExpr2 = sqlite3PExpr(pParse, TK_LT, sqlite3ExprAddCollateString(pParse,pNewExpr2,zCollSeqName), - pStr2, 0); + pStr2); transferJoinMarkings(pNewExpr2, pExpr); idxNew2 = whereClauseInsert(pWC, pNewExpr2, wtFlags); testcase( idxNew2==0 ); + exprAnalyze(pSrc, pWC, idxNew1); exprAnalyze(pSrc, pWC, idxNew2); pTerm = &pWC->a[idxTerm]; if( isComplete ){ @@ -1113,84 +1433,120 @@ static void exprAnalyze( } #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */ -#ifndef SQLITE_OMIT_VIRTUALTABLE - /* Add a WO_MATCH auxiliary term to the constraint set if the - ** current expression is of the form: column MATCH expr. - ** This information is used by the xBestIndex methods of - ** virtual tables. The native query optimizer does not attempt - ** to do anything with MATCH functions. + /* If there is a vector == or IS term - e.g. "(a, b) == (?, ?)" - create + ** new terms for each component comparison - "a = ?" and "b = ?". The + ** new terms completely replace the original vector comparison, which is + ** no longer used. + ** + ** This is only required if at least one side of the comparison operation + ** is not a sub-select. + ** + ** tag-20220128a */ - if( isMatchOfColumn(pExpr, &eOp2) ){ - int idxNew; - Expr *pRight, *pLeft; - WhereTerm *pNewTerm; - Bitmask prereqColumn, prereqExpr; + if( (pExpr->op==TK_EQ || pExpr->op==TK_IS) + && (nLeft = sqlite3ExprVectorSize(pExpr->pLeft))>1 + && sqlite3ExprVectorSize(pExpr->pRight)==nLeft + && ( (pExpr->pLeft->flags & EP_xIsSelect)==0 + || (pExpr->pRight->flags & EP_xIsSelect)==0) + && pWC->op==TK_AND + ){ + int i; + for(i=0; i<nLeft; i++){ + int idxNew; + Expr *pNew; + Expr *pLeft = sqlite3ExprForVectorField(pParse, pExpr->pLeft, i, nLeft); + Expr *pRight = sqlite3ExprForVectorField(pParse, pExpr->pRight, i, nLeft); - pRight = pExpr->x.pList->a[0].pExpr; - pLeft = pExpr->x.pList->a[1].pExpr; - prereqExpr = sqlite3WhereExprUsage(pMaskSet, pRight); - prereqColumn = sqlite3WhereExprUsage(pMaskSet, pLeft); - if( (prereqExpr & prereqColumn)==0 ){ - Expr *pNewExpr; - pNewExpr = sqlite3PExpr(pParse, TK_MATCH, - 0, sqlite3ExprDup(db, pRight, 0), 0); - idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC); - testcase( idxNew==0 ); - pNewTerm = &pWC->a[idxNew]; - pNewTerm->prereqRight = prereqExpr; - pNewTerm->leftCursor = pLeft->iTable; - pNewTerm->u.leftColumn = pLeft->iColumn; - pNewTerm->eOperator = WO_MATCH; - pNewTerm->eMatchOp = eOp2; - markTermAsChild(pWC, idxNew, idxTerm); - pTerm = &pWC->a[idxTerm]; - pTerm->wtFlags |= TERM_COPIED; - pNewTerm->prereqAll = pTerm->prereqAll; + pNew = sqlite3PExpr(pParse, pExpr->op, pLeft, pRight); + transferJoinMarkings(pNew, pExpr); + idxNew = whereClauseInsert(pWC, pNew, TERM_DYNAMIC|TERM_SLICE); + exprAnalyze(pSrc, pWC, idxNew); } + pTerm = &pWC->a[idxTerm]; + pTerm->wtFlags |= TERM_CODED|TERM_VIRTUAL; /* Disable the original */ + pTerm->eOperator = WO_ROWVAL; } -#endif /* SQLITE_OMIT_VIRTUALTABLE */ -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 - /* When sqlite_stat3 histogram data is available an operator of the - ** form "x IS NOT NULL" can sometimes be evaluated more efficiently - ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a - ** virtual term of that form. + /* If there is a vector IN term - e.g. "(a, b) IN (SELECT ...)" - create + ** a virtual term for each vector component. The expression object + ** used by each such virtual term is pExpr (the full vector IN(...) + ** expression). The WhereTerm.u.x.iField variable identifies the index within + ** the vector on the LHS that the virtual term represents. ** - ** Note that the virtual term must be tagged with TERM_VNULL. + ** This only works if the RHS is a simple SELECT (not a compound) that does + ** not use window functions. */ - if( pExpr->op==TK_NOTNULL - && pExpr->pLeft->op==TK_COLUMN - && pExpr->pLeft->iColumn>=0 - && OptimizationEnabled(db, SQLITE_Stat34) + else if( pExpr->op==TK_IN + && pTerm->u.x.iField==0 + && pExpr->pLeft->op==TK_VECTOR + && ALWAYS( ExprUseXSelect(pExpr) ) + && (pExpr->x.pSelect->pPrior==0 || (pExpr->x.pSelect->selFlags & SF_Values)) +#ifndef SQLITE_OMIT_WINDOWFUNC + && pExpr->x.pSelect->pWin==0 +#endif + && pWC->op==TK_AND ){ - Expr *pNewExpr; - Expr *pLeft = pExpr->pLeft; - int idxNew; - WhereTerm *pNewTerm; - - pNewExpr = sqlite3PExpr(pParse, TK_GT, - sqlite3ExprDup(db, pLeft, 0), - sqlite3PExpr(pParse, TK_NULL, 0, 0, 0), 0); - - idxNew = whereClauseInsert(pWC, pNewExpr, - TERM_VIRTUAL|TERM_DYNAMIC|TERM_VNULL); - if( idxNew ){ - pNewTerm = &pWC->a[idxNew]; - pNewTerm->prereqRight = 0; - pNewTerm->leftCursor = pLeft->iTable; - pNewTerm->u.leftColumn = pLeft->iColumn; - pNewTerm->eOperator = WO_GT; + int i; + for(i=0; i<sqlite3ExprVectorSize(pExpr->pLeft); i++){ + int idxNew; + idxNew = whereClauseInsert(pWC, pExpr, TERM_VIRTUAL|TERM_SLICE); + pWC->a[idxNew].u.x.iField = i+1; + exprAnalyze(pSrc, pWC, idxNew); markTermAsChild(pWC, idxNew, idxTerm); - pTerm = &pWC->a[idxTerm]; - pTerm->wtFlags |= TERM_COPIED; - pNewTerm->prereqAll = pTerm->prereqAll; } } -#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ + +#ifndef SQLITE_OMIT_VIRTUALTABLE + /* Add a WO_AUX auxiliary term to the constraint set if the + ** current expression is of the form "column OP expr" where OP + ** is an operator that gets passed into virtual tables but which is + ** not normally optimized for ordinary tables. In other words, OP + ** is one of MATCH, LIKE, GLOB, REGEXP, !=, IS, IS NOT, or NOT NULL. + ** This information is used by the xBestIndex methods of + ** virtual tables. The native query optimizer does not attempt + ** to do anything with MATCH functions. + */ + else if( pWC->op==TK_AND ){ + Expr *pRight = 0, *pLeft = 0; + int res = isAuxiliaryVtabOperator(db, pExpr, &eOp2, &pLeft, &pRight); + while( res-- > 0 ){ + int idxNew; + WhereTerm *pNewTerm; + Bitmask prereqColumn, prereqExpr; + + prereqExpr = sqlite3WhereExprUsage(pMaskSet, pRight); + prereqColumn = sqlite3WhereExprUsage(pMaskSet, pLeft); + if( (prereqExpr & prereqColumn)==0 ){ + Expr *pNewExpr; + pNewExpr = sqlite3PExpr(pParse, TK_MATCH, + 0, sqlite3ExprDup(db, pRight, 0)); + if( ExprHasProperty(pExpr, EP_OuterON) && pNewExpr ){ + ExprSetProperty(pNewExpr, EP_OuterON); + pNewExpr->w.iJoin = pExpr->w.iJoin; + } + idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC); + testcase( idxNew==0 ); + pNewTerm = &pWC->a[idxNew]; + pNewTerm->prereqRight = prereqExpr | extraRight; + pNewTerm->leftCursor = pLeft->iTable; + pNewTerm->u.x.leftColumn = pLeft->iColumn; + pNewTerm->eOperator = WO_AUX; + pNewTerm->eMatchOp = eOp2; + markTermAsChild(pWC, idxNew, idxTerm); + pTerm = &pWC->a[idxTerm]; + pTerm->wtFlags |= TERM_COPIED; + pNewTerm->prereqAll = pTerm->prereqAll; + } + SWAP(Expr*, pLeft, pRight); + } + } +#endif /* SQLITE_OMIT_VIRTUALTABLE */ /* Prevent ON clause terms of a LEFT JOIN from being used to drive ** an index for tables to the left of the join. */ + testcase( pTerm!=&pWC->a[idxTerm] ); + pTerm = &pWC->a[idxTerm]; pTerm->prereqRight |= extraRight; } @@ -1217,8 +1573,9 @@ static void exprAnalyze( ** all terms of the WHERE clause. */ void sqlite3WhereSplit(WhereClause *pWC, Expr *pExpr, u8 op){ - Expr *pE2 = sqlite3ExprSkipCollate(pExpr); + Expr *pE2 = sqlite3ExprSkipCollateAndLikely(pExpr); pWC->op = op; + assert( pE2!=0 || pExpr==0 ); if( pE2==0 ) return; if( pE2->op!=op ){ whereClauseInsert(pWC, pExpr, 0); @@ -1228,6 +1585,137 @@ void sqlite3WhereSplit(WhereClause *pWC, Expr *pExpr, u8 op){ } } +/* +** Add either a LIMIT (if eMatchOp==SQLITE_INDEX_CONSTRAINT_LIMIT) or +** OFFSET (if eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET) term to the +** where-clause passed as the first argument. The value for the term +** is found in register iReg. +** +** In the common case where the value is a simple integer +** (example: "LIMIT 5 OFFSET 10") then the expression codes as a +** TK_INTEGER so that it will be available to sqlite3_vtab_rhs_value(). +** If not, then it codes as a TK_REGISTER expression. +*/ +static void whereAddLimitExpr( + WhereClause *pWC, /* Add the constraint to this WHERE clause */ + int iReg, /* Register that will hold value of the limit/offset */ + Expr *pExpr, /* Expression that defines the limit/offset */ + int iCsr, /* Cursor to which the constraint applies */ + int eMatchOp /* SQLITE_INDEX_CONSTRAINT_LIMIT or _OFFSET */ +){ + Parse *pParse = pWC->pWInfo->pParse; + sqlite3 *db = pParse->db; + Expr *pNew; + int iVal = 0; + + if( sqlite3ExprIsInteger(pExpr, &iVal, pParse) && iVal>=0 ){ + Expr *pVal = sqlite3Expr(db, TK_INTEGER, 0); + if( pVal==0 ) return; + ExprSetProperty(pVal, EP_IntValue); + pVal->u.iValue = iVal; + pNew = sqlite3PExpr(pParse, TK_MATCH, 0, pVal); + }else{ + Expr *pVal = sqlite3Expr(db, TK_REGISTER, 0); + if( pVal==0 ) return; + pVal->iTable = iReg; + pNew = sqlite3PExpr(pParse, TK_MATCH, 0, pVal); + } + if( pNew ){ + WhereTerm *pTerm; + int idx; + idx = whereClauseInsert(pWC, pNew, TERM_DYNAMIC|TERM_VIRTUAL); + pTerm = &pWC->a[idx]; + pTerm->leftCursor = iCsr; + pTerm->eOperator = WO_AUX; + pTerm->eMatchOp = eMatchOp; + } +} + +/* +** Possibly add terms corresponding to the LIMIT and OFFSET clauses of the +** SELECT statement passed as the second argument. These terms are only +** added if: +** +** 1. The SELECT statement has a LIMIT clause, and +** 2. The SELECT statement is not an aggregate or DISTINCT query, and +** 3. The SELECT statement has exactly one object in its FROM clause, and +** that object is a virtual table, and +** 4. There are no terms in the WHERE clause that will not be passed +** to the virtual table xBestIndex method. +** 5. The ORDER BY clause, if any, will be made available to the xBestIndex +** method. +** +** LIMIT and OFFSET terms are ignored by most of the planner code. They +** exist only so that they may be passed to the xBestIndex method of the +** single virtual table in the FROM clause of the SELECT. +*/ +void SQLITE_NOINLINE sqlite3WhereAddLimit(WhereClause *pWC, Select *p){ + assert( p!=0 && p->pLimit!=0 ); /* 1 -- checked by caller */ + if( p->pGroupBy==0 + && (p->selFlags & (SF_Distinct|SF_Aggregate))==0 /* 2 */ + && (p->pSrc->nSrc==1 && IsVirtual(p->pSrc->a[0].pSTab)) /* 3 */ + ){ + ExprList *pOrderBy = p->pOrderBy; + int iCsr = p->pSrc->a[0].iCursor; + int ii; + + /* Check condition (4). Return early if it is not met. */ + for(ii=0; ii<pWC->nTerm; ii++){ + if( pWC->a[ii].wtFlags & TERM_CODED ){ + /* This term is a vector operation that has been decomposed into + ** other, subsequent terms. It can be ignored. See tag-20220128a */ + assert( pWC->a[ii].wtFlags & TERM_VIRTUAL ); + assert( pWC->a[ii].eOperator==WO_ROWVAL ); + continue; + } + if( pWC->a[ii].nChild ){ + /* If this term has child terms, then they are also part of the + ** pWC->a[] array. So this term can be ignored, as a LIMIT clause + ** will only be added if each of the child terms passes the + ** (leftCursor==iCsr) test below. */ + continue; + } + if( pWC->a[ii].leftCursor==iCsr && pWC->a[ii].prereqRight==0 ) continue; + + /* If this term has a parent with exactly one child, and the parent will + ** be passed through to xBestIndex, then this term can be ignored. */ + if( pWC->a[ii].iParent>=0 ){ + WhereTerm *pParent = &pWC->a[ pWC->a[ii].iParent ]; + if( pParent->leftCursor==iCsr + && pParent->prereqRight==0 + && pParent->nChild==1 + ){ + continue; + } + } + + /* This term will not be passed through. Do not add a LIMIT clause. */ + return; + } + + /* Check condition (5). Return early if it is not met. */ + if( pOrderBy ){ + for(ii=0; ii<pOrderBy->nExpr; ii++){ + Expr *pExpr = pOrderBy->a[ii].pExpr; + if( pExpr->op!=TK_COLUMN ) return; + if( pExpr->iTable!=iCsr ) return; + if( pOrderBy->a[ii].fg.sortFlags & KEYINFO_ORDER_BIGNULL ) return; + } + } + + /* All conditions are met. Add the terms to the where-clause object. */ + assert( p->pLimit->op==TK_LIMIT ); + if( p->iOffset!=0 && (p->selFlags & SF_Compound)==0 ){ + whereAddLimitExpr(pWC, p->iOffset, p->pLimit->pRight, + iCsr, SQLITE_INDEX_CONSTRAINT_OFFSET); + } + if( p->iOffset==0 || (p->selFlags & SF_Compound)==0 ){ + whereAddLimitExpr(pWC, p->iLimit, p->pLimit->pLeft, + iCsr, SQLITE_INDEX_CONSTRAINT_LIMIT); + } + } +} + /* ** Initialize a preallocated WhereClause structure. */ @@ -1236,8 +1724,10 @@ void sqlite3WhereClauseInit( WhereInfo *pWInfo /* The WHERE processing context */ ){ pWC->pWInfo = pWInfo; + pWC->hasOr = 0; pWC->pOuter = 0; pWC->nTerm = 0; + pWC->nBase = 0; pWC->nSlot = ArraySize(pWC->aStatic); pWC->a = pWC->aStatic; } @@ -1248,22 +1738,36 @@ void sqlite3WhereClauseInit( ** sqlite3WhereClauseInit(). */ void sqlite3WhereClauseClear(WhereClause *pWC){ - int i; - WhereTerm *a; sqlite3 *db = pWC->pWInfo->pParse->db; - for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){ - if( a->wtFlags & TERM_DYNAMIC ){ - sqlite3ExprDelete(db, a->pExpr); + assert( pWC->nTerm>=pWC->nBase ); + if( pWC->nTerm>0 ){ + WhereTerm *a = pWC->a; + WhereTerm *aLast = &pWC->a[pWC->nTerm-1]; +#ifdef SQLITE_DEBUG + int i; + /* Verify that every term past pWC->nBase is virtual */ + for(i=pWC->nBase; i<pWC->nTerm; i++){ + assert( (pWC->a[i].wtFlags & TERM_VIRTUAL)!=0 ); } - if( a->wtFlags & TERM_ORINFO ){ - whereOrInfoDelete(db, a->u.pOrInfo); - }else if( a->wtFlags & TERM_ANDINFO ){ - whereAndInfoDelete(db, a->u.pAndInfo); +#endif + while(1){ + assert( a->eMatchOp==0 || a->eOperator==WO_AUX ); + if( a->wtFlags & TERM_DYNAMIC ){ + sqlite3ExprDelete(db, a->pExpr); + } + if( a->wtFlags & (TERM_ORINFO|TERM_ANDINFO) ){ + if( a->wtFlags & TERM_ORINFO ){ + assert( (a->wtFlags & TERM_ANDINFO)==0 ); + whereOrInfoDelete(db, a->u.pOrInfo); + }else{ + assert( (a->wtFlags & TERM_ANDINFO)!=0 ); + whereAndInfoDelete(db, a->u.pAndInfo); + } + } + if( a==aLast ) break; + a++; } } - if( pWC->a!=pWC->aStatic ){ - sqlite3DbFree(db, pWC->a); - } } @@ -1271,23 +1775,71 @@ void sqlite3WhereClauseClear(WhereClause *pWC){ ** These routines walk (recursively) an expression tree and generate ** a bitmask indicating which tables are used in that expression ** tree. +** +** sqlite3WhereExprUsage(MaskSet, Expr) -> +** +** Return a Bitmask of all tables referenced by Expr. Expr can be +** be NULL, in which case 0 is returned. +** +** sqlite3WhereExprUsageNN(MaskSet, Expr) -> +** +** Same as sqlite3WhereExprUsage() except that Expr must not be +** NULL. The "NN" suffix on the name stands for "Not Null". +** +** sqlite3WhereExprListUsage(MaskSet, ExprList) -> +** +** Return a Bitmask of all tables referenced by every expression +** in the expression list ExprList. ExprList can be NULL, in which +** case 0 is returned. +** +** sqlite3WhereExprUsageFull(MaskSet, ExprList) -> +** +** Internal use only. Called only by sqlite3WhereExprUsageNN() for +** complex expressions that require pushing register values onto +** the stack. Many calls to sqlite3WhereExprUsageNN() do not need +** the more complex analysis done by this routine. Hence, the +** computations done by this routine are broken out into a separate +** "no-inline" function to avoid the stack push overhead in the +** common case where it is not needed. */ -Bitmask sqlite3WhereExprUsage(WhereMaskSet *pMaskSet, Expr *p){ - Bitmask mask = 0; - if( p==0 ) return 0; - if( p->op==TK_COLUMN ){ - mask = sqlite3WhereGetMask(pMaskSet, p->iTable); - return mask; - } - mask = sqlite3WhereExprUsage(pMaskSet, p->pRight); - mask |= sqlite3WhereExprUsage(pMaskSet, p->pLeft); - if( ExprHasProperty(p, EP_xIsSelect) ){ +static SQLITE_NOINLINE Bitmask sqlite3WhereExprUsageFull( + WhereMaskSet *pMaskSet, + Expr *p +){ + Bitmask mask; + mask = (p->op==TK_IF_NULL_ROW) ? sqlite3WhereGetMask(pMaskSet, p->iTable) : 0; + if( p->pLeft ) mask |= sqlite3WhereExprUsageNN(pMaskSet, p->pLeft); + if( p->pRight ){ + mask |= sqlite3WhereExprUsageNN(pMaskSet, p->pRight); + assert( p->x.pList==0 ); + }else if( ExprUseXSelect(p) ){ + if( ExprHasProperty(p, EP_VarSelect) ) pMaskSet->bVarSelect = 1; mask |= exprSelectUsage(pMaskSet, p->x.pSelect); - }else{ + }else if( p->x.pList ){ mask |= sqlite3WhereExprListUsage(pMaskSet, p->x.pList); } +#ifndef SQLITE_OMIT_WINDOWFUNC + if( (p->op==TK_FUNCTION || p->op==TK_AGG_FUNCTION) && ExprUseYWin(p) ){ + assert( p->y.pWin!=0 ); + mask |= sqlite3WhereExprListUsage(pMaskSet, p->y.pWin->pPartition); + mask |= sqlite3WhereExprListUsage(pMaskSet, p->y.pWin->pOrderBy); + mask |= sqlite3WhereExprUsage(pMaskSet, p->y.pWin->pFilter); + } +#endif return mask; } +Bitmask sqlite3WhereExprUsageNN(WhereMaskSet *pMaskSet, Expr *p){ + if( p->op==TK_COLUMN && !ExprHasProperty(p, EP_FixedCol) ){ + return sqlite3WhereGetMask(pMaskSet, p->iTable); + }else if( ExprHasProperty(p, EP_TokenOnly|EP_Leaf) ){ + assert( p->op!=TK_IF_NULL_ROW ); + return 0; + } + return sqlite3WhereExprUsageFull(pMaskSet, p); +} +Bitmask sqlite3WhereExprUsage(WhereMaskSet *pMaskSet, Expr *p){ + return p ? sqlite3WhereExprUsageNN(pMaskSet,p) : 0; +} Bitmask sqlite3WhereExprListUsage(WhereMaskSet *pMaskSet, ExprList *pList){ int i; Bitmask mask = 0; @@ -1301,7 +1853,7 @@ Bitmask sqlite3WhereExprListUsage(WhereMaskSet *pMaskSet, ExprList *pList){ /* -** Call exprAnalyze on all terms in a WHERE clause. +** Call exprAnalyze on all terms in a WHERE clause. ** ** Note that exprAnalyze() might add new virtual terms onto the ** end of the WHERE clause. We do not want to analyze these new @@ -1320,14 +1872,14 @@ void sqlite3WhereExprAnalyze( /* ** For table-valued-functions, transform the function arguments into -** new WHERE clause terms. +** new WHERE clause terms. ** ** Each function argument translates into an equality constraint against ** a HIDDEN column in the table. */ void sqlite3WhereTabFuncArgs( Parse *pParse, /* Parsing context */ - struct SrcList_item *pItem, /* The FROM clause term to process */ + SrcItem *pItem, /* The FROM clause term to process */ WhereClause *pWC /* Xfer function arguments to here */ ){ Table *pTab; @@ -1336,24 +1888,38 @@ void sqlite3WhereTabFuncArgs( Expr *pColRef; Expr *pTerm; if( pItem->fg.isTabFunc==0 ) return; - pTab = pItem->pTab; + pTab = pItem->pSTab; assert( pTab!=0 ); pArgs = pItem->u1.pFuncArg; if( pArgs==0 ) return; for(j=k=0; j<pArgs->nExpr; j++){ + Expr *pRhs; + u32 joinType; while( k<pTab->nCol && (pTab->aCol[k].colFlags & COLFLAG_HIDDEN)==0 ){k++;} if( k>=pTab->nCol ){ sqlite3ErrorMsg(pParse, "too many arguments on %s() - max %d", pTab->zName, j); return; } - pColRef = sqlite3PExpr(pParse, TK_COLUMN, 0, 0, 0); + pColRef = sqlite3ExprAlloc(pParse->db, TK_COLUMN, 0, 0); if( pColRef==0 ) return; pColRef->iTable = pItem->iCursor; pColRef->iColumn = k++; - pColRef->pTab = pTab; - pTerm = sqlite3PExpr(pParse, TK_EQ, pColRef, - sqlite3ExprDup(pParse->db, pArgs->a[j].pExpr, 0), 0); + assert( ExprUseYTab(pColRef) ); + pColRef->y.pTab = pTab; + pItem->colUsed |= sqlite3ExprColUsed(pColRef); + pRhs = sqlite3PExpr(pParse, TK_UPLUS, + sqlite3ExprDup(pParse->db, pArgs->a[j].pExpr, 0), 0); + pTerm = sqlite3PExpr(pParse, TK_EQ, pColRef, pRhs); + if( pItem->fg.jointype & (JT_LEFT|JT_RIGHT) ){ + testcase( pItem->fg.jointype & JT_LEFT ); /* testtag-20230227a */ + testcase( pItem->fg.jointype & JT_RIGHT ); /* testtag-20230227b */ + joinType = EP_OuterON; + }else{ + testcase( pItem->fg.jointype & JT_LTORJ ); /* testtag-20230227c */ + joinType = EP_InnerON; + } + sqlite3SetJoinExpr(pTerm, pItem->iCursor, joinType); whereClauseInsert(pWC, pTerm, TERM_DYNAMIC); } } diff --git a/src/window.c b/src/window.c new file mode 100644 index 0000000000..1f22ab1945 --- /dev/null +++ b/src/window.c @@ -0,0 +1,3114 @@ +/* +** 2018 May 08 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +*/ +#include "sqliteInt.h" + +#ifndef SQLITE_OMIT_WINDOWFUNC + +/* +** SELECT REWRITING +** +** Any SELECT statement that contains one or more window functions in +** either the select list or ORDER BY clause (the only two places window +** functions may be used) is transformed by function sqlite3WindowRewrite() +** in order to support window function processing. For example, with the +** schema: +** +** CREATE TABLE t1(a, b, c, d, e, f, g); +** +** the statement: +** +** SELECT a+1, max(b) OVER (PARTITION BY c ORDER BY d) FROM t1 ORDER BY e; +** +** is transformed to: +** +** SELECT a+1, max(b) OVER (PARTITION BY c ORDER BY d) FROM ( +** SELECT a, e, c, d, b FROM t1 ORDER BY c, d +** ) ORDER BY e; +** +** The flattening optimization is disabled when processing this transformed +** SELECT statement. This allows the implementation of the window function +** (in this case max()) to process rows sorted in order of (c, d), which +** makes things easier for obvious reasons. More generally: +** +** * FROM, WHERE, GROUP BY and HAVING clauses are all moved to +** the sub-query. +** +** * ORDER BY, LIMIT and OFFSET remain part of the parent query. +** +** * Terminals from each of the expression trees that make up the +** select-list and ORDER BY expressions in the parent query are +** selected by the sub-query. For the purposes of the transformation, +** terminals are column references and aggregate functions. +** +** If there is more than one window function in the SELECT that uses +** the same window declaration (the OVER bit), then a single scan may +** be used to process more than one window function. For example: +** +** SELECT max(b) OVER (PARTITION BY c ORDER BY d), +** min(e) OVER (PARTITION BY c ORDER BY d) +** FROM t1; +** +** is transformed in the same way as the example above. However: +** +** SELECT max(b) OVER (PARTITION BY c ORDER BY d), +** min(e) OVER (PARTITION BY a ORDER BY b) +** FROM t1; +** +** Must be transformed to: +** +** SELECT max(b) OVER (PARTITION BY c ORDER BY d) FROM ( +** SELECT e, min(e) OVER (PARTITION BY a ORDER BY b), c, d, b FROM +** SELECT a, e, c, d, b FROM t1 ORDER BY a, b +** ) ORDER BY c, d +** ) ORDER BY e; +** +** so that both min() and max() may process rows in the order defined by +** their respective window declarations. +** +** INTERFACE WITH SELECT.C +** +** When processing the rewritten SELECT statement, code in select.c calls +** sqlite3WhereBegin() to begin iterating through the results of the +** sub-query, which is always implemented as a co-routine. It then calls +** sqlite3WindowCodeStep() to process rows and finish the scan by calling +** sqlite3WhereEnd(). +** +** sqlite3WindowCodeStep() generates VM code so that, for each row returned +** by the sub-query a sub-routine (OP_Gosub) coded by select.c is invoked. +** When the sub-routine is invoked: +** +** * The results of all window-functions for the row are stored +** in the associated Window.regResult registers. +** +** * The required terminal values are stored in the current row of +** temp table Window.iEphCsr. +** +** In some cases, depending on the window frame and the specific window +** functions invoked, sqlite3WindowCodeStep() caches each entire partition +** in a temp table before returning any rows. In other cases it does not. +** This detail is encapsulated within this file, the code generated by +** select.c is the same in either case. +** +** BUILT-IN WINDOW FUNCTIONS +** +** This implementation features the following built-in window functions: +** +** row_number() +** rank() +** dense_rank() +** percent_rank() +** cume_dist() +** ntile(N) +** lead(expr [, offset [, default]]) +** lag(expr [, offset [, default]]) +** first_value(expr) +** last_value(expr) +** nth_value(expr, N) +** +** These are the same built-in window functions supported by Postgres. +** Although the behaviour of aggregate window functions (functions that +** can be used as either aggregates or window functions) allows them to +** be implemented using an API, built-in window functions are much more +** esoteric. Additionally, some window functions (e.g. nth_value()) +** may only be implemented by caching the entire partition in memory. +** As such, some built-in window functions use the same API as aggregate +** window functions and some are implemented directly using VDBE +** instructions. Additionally, for those functions that use the API, the +** window frame is sometimes modified before the SELECT statement is +** rewritten. For example, regardless of the specified window frame, the +** row_number() function always uses: +** +** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW +** +** See sqlite3WindowUpdate() for details. +** +** As well as some of the built-in window functions, aggregate window +** functions min() and max() are implemented using VDBE instructions if +** the start of the window frame is declared as anything other than +** UNBOUNDED PRECEDING. +*/ + +/* +** Implementation of built-in window function row_number(). Assumes that the +** window frame has been coerced to: +** +** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW +*/ +static void row_numberStepFunc( + sqlite3_context *pCtx, + int nArg, + sqlite3_value **apArg +){ + i64 *p = (i64*)sqlite3_aggregate_context(pCtx, sizeof(*p)); + if( p ) (*p)++; + UNUSED_PARAMETER(nArg); + UNUSED_PARAMETER(apArg); +} +static void row_numberValueFunc(sqlite3_context *pCtx){ + i64 *p = (i64*)sqlite3_aggregate_context(pCtx, sizeof(*p)); + sqlite3_result_int64(pCtx, (p ? *p : 0)); +} + +/* +** Context object type used by rank(), dense_rank(), percent_rank() and +** cume_dist(). +*/ +struct CallCount { + i64 nValue; + i64 nStep; + i64 nTotal; +}; + +/* +** Implementation of built-in window function dense_rank(). Assumes that +** the window frame has been set to: +** +** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW +*/ +static void dense_rankStepFunc( + sqlite3_context *pCtx, + int nArg, + sqlite3_value **apArg +){ + struct CallCount *p; + p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p)); + if( p ) p->nStep = 1; + UNUSED_PARAMETER(nArg); + UNUSED_PARAMETER(apArg); +} +static void dense_rankValueFunc(sqlite3_context *pCtx){ + struct CallCount *p; + p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p)); + if( p ){ + if( p->nStep ){ + p->nValue++; + p->nStep = 0; + } + sqlite3_result_int64(pCtx, p->nValue); + } +} + +/* +** Implementation of built-in window function nth_value(). This +** implementation is used in "slow mode" only - when the EXCLUDE clause +** is not set to the default value "NO OTHERS". +*/ +struct NthValueCtx { + i64 nStep; + sqlite3_value *pValue; +}; +static void nth_valueStepFunc( + sqlite3_context *pCtx, + int nArg, + sqlite3_value **apArg +){ + struct NthValueCtx *p; + p = (struct NthValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p)); + if( p ){ + i64 iVal; + switch( sqlite3_value_numeric_type(apArg[1]) ){ + case SQLITE_INTEGER: + iVal = sqlite3_value_int64(apArg[1]); + break; + case SQLITE_FLOAT: { + double fVal = sqlite3_value_double(apArg[1]); + if( ((i64)fVal)!=fVal ) goto error_out; + iVal = (i64)fVal; + break; + } + default: + goto error_out; + } + if( iVal<=0 ) goto error_out; + + p->nStep++; + if( iVal==p->nStep ){ + p->pValue = sqlite3_value_dup(apArg[0]); + if( !p->pValue ){ + sqlite3_result_error_nomem(pCtx); + } + } + } + UNUSED_PARAMETER(nArg); + UNUSED_PARAMETER(apArg); + return; + + error_out: + sqlite3_result_error( + pCtx, "second argument to nth_value must be a positive integer", -1 + ); +} +static void nth_valueFinalizeFunc(sqlite3_context *pCtx){ + struct NthValueCtx *p; + p = (struct NthValueCtx*)sqlite3_aggregate_context(pCtx, 0); + if( p && p->pValue ){ + sqlite3_result_value(pCtx, p->pValue); + sqlite3_value_free(p->pValue); + p->pValue = 0; + } +} +#define nth_valueInvFunc noopStepFunc +#define nth_valueValueFunc noopValueFunc + +static void first_valueStepFunc( + sqlite3_context *pCtx, + int nArg, + sqlite3_value **apArg +){ + struct NthValueCtx *p; + p = (struct NthValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p)); + if( p && p->pValue==0 ){ + p->pValue = sqlite3_value_dup(apArg[0]); + if( !p->pValue ){ + sqlite3_result_error_nomem(pCtx); + } + } + UNUSED_PARAMETER(nArg); + UNUSED_PARAMETER(apArg); +} +static void first_valueFinalizeFunc(sqlite3_context *pCtx){ + struct NthValueCtx *p; + p = (struct NthValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p)); + if( p && p->pValue ){ + sqlite3_result_value(pCtx, p->pValue); + sqlite3_value_free(p->pValue); + p->pValue = 0; + } +} +#define first_valueInvFunc noopStepFunc +#define first_valueValueFunc noopValueFunc + +/* +** Implementation of built-in window function rank(). Assumes that +** the window frame has been set to: +** +** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW +*/ +static void rankStepFunc( + sqlite3_context *pCtx, + int nArg, + sqlite3_value **apArg +){ + struct CallCount *p; + p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p)); + if( p ){ + p->nStep++; + if( p->nValue==0 ){ + p->nValue = p->nStep; + } + } + UNUSED_PARAMETER(nArg); + UNUSED_PARAMETER(apArg); +} +static void rankValueFunc(sqlite3_context *pCtx){ + struct CallCount *p; + p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p)); + if( p ){ + sqlite3_result_int64(pCtx, p->nValue); + p->nValue = 0; + } +} + +/* +** Implementation of built-in window function percent_rank(). Assumes that +** the window frame has been set to: +** +** GROUPS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING +*/ +static void percent_rankStepFunc( + sqlite3_context *pCtx, + int nArg, + sqlite3_value **apArg +){ + struct CallCount *p; + UNUSED_PARAMETER(nArg); assert( nArg==0 ); + UNUSED_PARAMETER(apArg); + p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p)); + if( p ){ + p->nTotal++; + } +} +static void percent_rankInvFunc( + sqlite3_context *pCtx, + int nArg, + sqlite3_value **apArg +){ + struct CallCount *p; + UNUSED_PARAMETER(nArg); assert( nArg==0 ); + UNUSED_PARAMETER(apArg); + p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p)); + p->nStep++; +} +static void percent_rankValueFunc(sqlite3_context *pCtx){ + struct CallCount *p; + p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p)); + if( p ){ + p->nValue = p->nStep; + if( p->nTotal>1 ){ + double r = (double)p->nValue / (double)(p->nTotal-1); + sqlite3_result_double(pCtx, r); + }else{ + sqlite3_result_double(pCtx, 0.0); + } + } +} +#define percent_rankFinalizeFunc percent_rankValueFunc + +/* +** Implementation of built-in window function cume_dist(). Assumes that +** the window frame has been set to: +** +** GROUPS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING +*/ +static void cume_distStepFunc( + sqlite3_context *pCtx, + int nArg, + sqlite3_value **apArg +){ + struct CallCount *p; + UNUSED_PARAMETER(nArg); assert( nArg==0 ); + UNUSED_PARAMETER(apArg); + p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p)); + if( p ){ + p->nTotal++; + } +} +static void cume_distInvFunc( + sqlite3_context *pCtx, + int nArg, + sqlite3_value **apArg +){ + struct CallCount *p; + UNUSED_PARAMETER(nArg); assert( nArg==0 ); + UNUSED_PARAMETER(apArg); + p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p)); + p->nStep++; +} +static void cume_distValueFunc(sqlite3_context *pCtx){ + struct CallCount *p; + p = (struct CallCount*)sqlite3_aggregate_context(pCtx, 0); + if( p ){ + double r = (double)(p->nStep) / (double)(p->nTotal); + sqlite3_result_double(pCtx, r); + } +} +#define cume_distFinalizeFunc cume_distValueFunc + +/* +** Context object for ntile() window function. +*/ +struct NtileCtx { + i64 nTotal; /* Total rows in partition */ + i64 nParam; /* Parameter passed to ntile(N) */ + i64 iRow; /* Current row */ +}; + +/* +** Implementation of ntile(). This assumes that the window frame has +** been coerced to: +** +** ROWS CURRENT ROW AND UNBOUNDED FOLLOWING +*/ +static void ntileStepFunc( + sqlite3_context *pCtx, + int nArg, + sqlite3_value **apArg +){ + struct NtileCtx *p; + assert( nArg==1 ); UNUSED_PARAMETER(nArg); + p = (struct NtileCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p)); + if( p ){ + if( p->nTotal==0 ){ + p->nParam = sqlite3_value_int64(apArg[0]); + if( p->nParam<=0 ){ + sqlite3_result_error( + pCtx, "argument of ntile must be a positive integer", -1 + ); + } + } + p->nTotal++; + } +} +static void ntileInvFunc( + sqlite3_context *pCtx, + int nArg, + sqlite3_value **apArg +){ + struct NtileCtx *p; + assert( nArg==1 ); UNUSED_PARAMETER(nArg); + UNUSED_PARAMETER(apArg); + p = (struct NtileCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p)); + p->iRow++; +} +static void ntileValueFunc(sqlite3_context *pCtx){ + struct NtileCtx *p; + p = (struct NtileCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p)); + if( p && p->nParam>0 ){ + int nSize = (p->nTotal / p->nParam); + if( nSize==0 ){ + sqlite3_result_int64(pCtx, p->iRow+1); + }else{ + i64 nLarge = p->nTotal - p->nParam*nSize; + i64 iSmall = nLarge*(nSize+1); + i64 iRow = p->iRow; + + assert( (nLarge*(nSize+1) + (p->nParam-nLarge)*nSize)==p->nTotal ); + + if( iRow<iSmall ){ + sqlite3_result_int64(pCtx, 1 + iRow/(nSize+1)); + }else{ + sqlite3_result_int64(pCtx, 1 + nLarge + (iRow-iSmall)/nSize); + } + } + } +} +#define ntileFinalizeFunc ntileValueFunc + +/* +** Context object for last_value() window function. +*/ +struct LastValueCtx { + sqlite3_value *pVal; + int nVal; +}; + +/* +** Implementation of last_value(). +*/ +static void last_valueStepFunc( + sqlite3_context *pCtx, + int nArg, + sqlite3_value **apArg +){ + struct LastValueCtx *p; + UNUSED_PARAMETER(nArg); + p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p)); + if( p ){ + sqlite3_value_free(p->pVal); + p->pVal = sqlite3_value_dup(apArg[0]); + if( p->pVal==0 ){ + sqlite3_result_error_nomem(pCtx); + }else{ + p->nVal++; + } + } +} +static void last_valueInvFunc( + sqlite3_context *pCtx, + int nArg, + sqlite3_value **apArg +){ + struct LastValueCtx *p; + UNUSED_PARAMETER(nArg); + UNUSED_PARAMETER(apArg); + p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p)); + if( ALWAYS(p) ){ + p->nVal--; + if( p->nVal==0 ){ + sqlite3_value_free(p->pVal); + p->pVal = 0; + } + } +} +static void last_valueValueFunc(sqlite3_context *pCtx){ + struct LastValueCtx *p; + p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, 0); + if( p && p->pVal ){ + sqlite3_result_value(pCtx, p->pVal); + } +} +static void last_valueFinalizeFunc(sqlite3_context *pCtx){ + struct LastValueCtx *p; + p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p)); + if( p && p->pVal ){ + sqlite3_result_value(pCtx, p->pVal); + sqlite3_value_free(p->pVal); + p->pVal = 0; + } +} + +/* +** Static names for the built-in window function names. These static +** names are used, rather than string literals, so that FuncDef objects +** can be associated with a particular window function by direct +** comparison of the zName pointer. Example: +** +** if( pFuncDef->zName==row_valueName ){ ... } +*/ +static const char row_numberName[] = "row_number"; +static const char dense_rankName[] = "dense_rank"; +static const char rankName[] = "rank"; +static const char percent_rankName[] = "percent_rank"; +static const char cume_distName[] = "cume_dist"; +static const char ntileName[] = "ntile"; +static const char last_valueName[] = "last_value"; +static const char nth_valueName[] = "nth_value"; +static const char first_valueName[] = "first_value"; +static const char leadName[] = "lead"; +static const char lagName[] = "lag"; + +/* +** No-op implementations of xStep() and xFinalize(). Used as place-holders +** for built-in window functions that never call those interfaces. +** +** The noopValueFunc() is called but is expected to do nothing. The +** noopStepFunc() is never called, and so it is marked with NO_TEST to +** let the test coverage routine know not to expect this function to be +** invoked. +*/ +static void noopStepFunc( /*NO_TEST*/ + sqlite3_context *p, /*NO_TEST*/ + int n, /*NO_TEST*/ + sqlite3_value **a /*NO_TEST*/ +){ /*NO_TEST*/ + UNUSED_PARAMETER(p); /*NO_TEST*/ + UNUSED_PARAMETER(n); /*NO_TEST*/ + UNUSED_PARAMETER(a); /*NO_TEST*/ + assert(0); /*NO_TEST*/ +} /*NO_TEST*/ +static void noopValueFunc(sqlite3_context *p){ UNUSED_PARAMETER(p); /*no-op*/ } + +/* Window functions that use all window interfaces: xStep, xFinal, +** xValue, and xInverse */ +#define WINDOWFUNCALL(name,nArg,extra) { \ + nArg, (SQLITE_FUNC_BUILTIN|SQLITE_UTF8|SQLITE_FUNC_WINDOW|extra), 0, 0, \ + name ## StepFunc, name ## FinalizeFunc, name ## ValueFunc, \ + name ## InvFunc, name ## Name, {0} \ +} + +/* Window functions that are implemented using bytecode and thus have +** no-op routines for their methods */ +#define WINDOWFUNCNOOP(name,nArg,extra) { \ + nArg, (SQLITE_FUNC_BUILTIN|SQLITE_UTF8|SQLITE_FUNC_WINDOW|extra), 0, 0, \ + noopStepFunc, noopValueFunc, noopValueFunc, \ + noopStepFunc, name ## Name, {0} \ +} + +/* Window functions that use all window interfaces: xStep, the +** same routine for xFinalize and xValue and which never call +** xInverse. */ +#define WINDOWFUNCX(name,nArg,extra) { \ + nArg, (SQLITE_FUNC_BUILTIN|SQLITE_UTF8|SQLITE_FUNC_WINDOW|extra), 0, 0, \ + name ## StepFunc, name ## ValueFunc, name ## ValueFunc, \ + noopStepFunc, name ## Name, {0} \ +} + + +/* +** Register those built-in window functions that are not also aggregates. +*/ +void sqlite3WindowFunctions(void){ + static FuncDef aWindowFuncs[] = { + WINDOWFUNCX(row_number, 0, 0), + WINDOWFUNCX(dense_rank, 0, 0), + WINDOWFUNCX(rank, 0, 0), + WINDOWFUNCALL(percent_rank, 0, 0), + WINDOWFUNCALL(cume_dist, 0, 0), + WINDOWFUNCALL(ntile, 1, 0), + WINDOWFUNCALL(last_value, 1, 0), + WINDOWFUNCALL(nth_value, 2, 0), + WINDOWFUNCALL(first_value, 1, 0), + WINDOWFUNCNOOP(lead, 1, 0), + WINDOWFUNCNOOP(lead, 2, 0), + WINDOWFUNCNOOP(lead, 3, 0), + WINDOWFUNCNOOP(lag, 1, 0), + WINDOWFUNCNOOP(lag, 2, 0), + WINDOWFUNCNOOP(lag, 3, 0), + }; + sqlite3InsertBuiltinFuncs(aWindowFuncs, ArraySize(aWindowFuncs)); +} + +static Window *windowFind(Parse *pParse, Window *pList, const char *zName){ + Window *p; + for(p=pList; p; p=p->pNextWin){ + if( sqlite3StrICmp(p->zName, zName)==0 ) break; + } + if( p==0 ){ + sqlite3ErrorMsg(pParse, "no such window: %s", zName); + } + return p; +} + +/* +** This function is called immediately after resolving the function name +** for a window function within a SELECT statement. Argument pList is a +** linked list of WINDOW definitions for the current SELECT statement. +** Argument pFunc is the function definition just resolved and pWin +** is the Window object representing the associated OVER clause. This +** function updates the contents of pWin as follows: +** +** * If the OVER clause referred to a named window (as in "max(x) OVER win"), +** search list pList for a matching WINDOW definition, and update pWin +** accordingly. If no such WINDOW clause can be found, leave an error +** in pParse. +** +** * If the function is a built-in window function that requires the +** window to be coerced (see "BUILT-IN WINDOW FUNCTIONS" at the top +** of this file), pWin is updated here. +*/ +void sqlite3WindowUpdate( + Parse *pParse, + Window *pList, /* List of named windows for this SELECT */ + Window *pWin, /* Window frame to update */ + FuncDef *pFunc /* Window function definition */ +){ + if( pWin->zName && pWin->eFrmType==0 ){ + Window *p = windowFind(pParse, pList, pWin->zName); + if( p==0 ) return; + pWin->pPartition = sqlite3ExprListDup(pParse->db, p->pPartition, 0); + pWin->pOrderBy = sqlite3ExprListDup(pParse->db, p->pOrderBy, 0); + pWin->pStart = sqlite3ExprDup(pParse->db, p->pStart, 0); + pWin->pEnd = sqlite3ExprDup(pParse->db, p->pEnd, 0); + pWin->eStart = p->eStart; + pWin->eEnd = p->eEnd; + pWin->eFrmType = p->eFrmType; + pWin->eExclude = p->eExclude; + }else{ + sqlite3WindowChain(pParse, pWin, pList); + } + if( (pWin->eFrmType==TK_RANGE) + && (pWin->pStart || pWin->pEnd) + && (pWin->pOrderBy==0 || pWin->pOrderBy->nExpr!=1) + ){ + sqlite3ErrorMsg(pParse, + "RANGE with offset PRECEDING/FOLLOWING requires one ORDER BY expression" + ); + }else + if( pFunc->funcFlags & SQLITE_FUNC_WINDOW ){ + sqlite3 *db = pParse->db; + if( pWin->pFilter ){ + sqlite3ErrorMsg(pParse, + "FILTER clause may only be used with aggregate window functions" + ); + }else{ + struct WindowUpdate { + const char *zFunc; + int eFrmType; + int eStart; + int eEnd; + } aUp[] = { + { row_numberName, TK_ROWS, TK_UNBOUNDED, TK_CURRENT }, + { dense_rankName, TK_RANGE, TK_UNBOUNDED, TK_CURRENT }, + { rankName, TK_RANGE, TK_UNBOUNDED, TK_CURRENT }, + { percent_rankName, TK_GROUPS, TK_CURRENT, TK_UNBOUNDED }, + { cume_distName, TK_GROUPS, TK_FOLLOWING, TK_UNBOUNDED }, + { ntileName, TK_ROWS, TK_CURRENT, TK_UNBOUNDED }, + { leadName, TK_ROWS, TK_UNBOUNDED, TK_UNBOUNDED }, + { lagName, TK_ROWS, TK_UNBOUNDED, TK_CURRENT }, + }; + int i; + for(i=0; i<ArraySize(aUp); i++){ + if( pFunc->zName==aUp[i].zFunc ){ + sqlite3ExprDelete(db, pWin->pStart); + sqlite3ExprDelete(db, pWin->pEnd); + pWin->pEnd = pWin->pStart = 0; + pWin->eFrmType = aUp[i].eFrmType; + pWin->eStart = aUp[i].eStart; + pWin->eEnd = aUp[i].eEnd; + pWin->eExclude = 0; + if( pWin->eStart==TK_FOLLOWING ){ + pWin->pStart = sqlite3Expr(db, TK_INTEGER, "1"); + } + break; + } + } + } + } + pWin->pWFunc = pFunc; +} + +/* +** Context object passed through sqlite3WalkExprList() to +** selectWindowRewriteExprCb() by selectWindowRewriteEList(). +*/ +typedef struct WindowRewrite WindowRewrite; +struct WindowRewrite { + Window *pWin; + SrcList *pSrc; + ExprList *pSub; + Table *pTab; + Select *pSubSelect; /* Current sub-select, if any */ +}; + +/* +** Callback function used by selectWindowRewriteEList(). If necessary, +** this function appends to the output expression-list and updates +** expression (*ppExpr) in place. +*/ +static int selectWindowRewriteExprCb(Walker *pWalker, Expr *pExpr){ + struct WindowRewrite *p = pWalker->u.pRewrite; + Parse *pParse = pWalker->pParse; + assert( p!=0 ); + assert( p->pWin!=0 ); + + /* If this function is being called from within a scalar sub-select + ** that used by the SELECT statement being processed, only process + ** TK_COLUMN expressions that refer to it (the outer SELECT). Do + ** not process aggregates or window functions at all, as they belong + ** to the scalar sub-select. */ + if( p->pSubSelect ){ + if( pExpr->op!=TK_COLUMN ){ + return WRC_Continue; + }else{ + int nSrc = p->pSrc->nSrc; + int i; + for(i=0; i<nSrc; i++){ + if( pExpr->iTable==p->pSrc->a[i].iCursor ) break; + } + if( i==nSrc ) return WRC_Continue; + } + } + + switch( pExpr->op ){ + + case TK_FUNCTION: + if( !ExprHasProperty(pExpr, EP_WinFunc) ){ + break; + }else{ + Window *pWin; + for(pWin=p->pWin; pWin; pWin=pWin->pNextWin){ + if( pExpr->y.pWin==pWin ){ + assert( pWin->pOwner==pExpr ); + return WRC_Prune; + } + } + } + /* no break */ deliberate_fall_through + + case TK_IF_NULL_ROW: + case TK_AGG_FUNCTION: + case TK_COLUMN: { + int iCol = -1; + if( pParse->db->mallocFailed ) return WRC_Abort; + if( p->pSub ){ + int i; + for(i=0; i<p->pSub->nExpr; i++){ + if( 0==sqlite3ExprCompare(0, p->pSub->a[i].pExpr, pExpr, -1) ){ + iCol = i; + break; + } + } + } + if( iCol<0 ){ + Expr *pDup = sqlite3ExprDup(pParse->db, pExpr, 0); + if( pDup && pDup->op==TK_AGG_FUNCTION ) pDup->op = TK_FUNCTION; + p->pSub = sqlite3ExprListAppend(pParse, p->pSub, pDup); + } + if( p->pSub ){ + int f = pExpr->flags & EP_Collate; + assert( ExprHasProperty(pExpr, EP_Static)==0 ); + ExprSetProperty(pExpr, EP_Static); + sqlite3ExprDelete(pParse->db, pExpr); + ExprClearProperty(pExpr, EP_Static); + memset(pExpr, 0, sizeof(Expr)); + + pExpr->op = TK_COLUMN; + pExpr->iColumn = (iCol<0 ? p->pSub->nExpr-1: iCol); + pExpr->iTable = p->pWin->iEphCsr; + pExpr->y.pTab = p->pTab; + pExpr->flags = f; + } + if( pParse->db->mallocFailed ) return WRC_Abort; + break; + } + + default: /* no-op */ + break; + } + + return WRC_Continue; +} +static int selectWindowRewriteSelectCb(Walker *pWalker, Select *pSelect){ + struct WindowRewrite *p = pWalker->u.pRewrite; + Select *pSave = p->pSubSelect; + if( pSave==pSelect ){ + return WRC_Continue; + }else{ + p->pSubSelect = pSelect; + sqlite3WalkSelect(pWalker, pSelect); + p->pSubSelect = pSave; + } + return WRC_Prune; +} + + +/* +** Iterate through each expression in expression-list pEList. For each: +** +** * TK_COLUMN, +** * aggregate function, or +** * window function with a Window object that is not a member of the +** Window list passed as the second argument (pWin). +** +** Append the node to output expression-list (*ppSub). And replace it +** with a TK_COLUMN that reads the (N-1)th element of table +** pWin->iEphCsr, where N is the number of elements in (*ppSub) after +** appending the new one. +*/ +static void selectWindowRewriteEList( + Parse *pParse, + Window *pWin, + SrcList *pSrc, + ExprList *pEList, /* Rewrite expressions in this list */ + Table *pTab, + ExprList **ppSub /* IN/OUT: Sub-select expression-list */ +){ + Walker sWalker; + WindowRewrite sRewrite; + + assert( pWin!=0 ); + memset(&sWalker, 0, sizeof(Walker)); + memset(&sRewrite, 0, sizeof(WindowRewrite)); + + sRewrite.pSub = *ppSub; + sRewrite.pWin = pWin; + sRewrite.pSrc = pSrc; + sRewrite.pTab = pTab; + + sWalker.pParse = pParse; + sWalker.xExprCallback = selectWindowRewriteExprCb; + sWalker.xSelectCallback = selectWindowRewriteSelectCb; + sWalker.u.pRewrite = &sRewrite; + + (void)sqlite3WalkExprList(&sWalker, pEList); + + *ppSub = sRewrite.pSub; +} + +/* +** Append a copy of each expression in expression-list pAppend to +** expression list pList. Return a pointer to the result list. +*/ +static ExprList *exprListAppendList( + Parse *pParse, /* Parsing context */ + ExprList *pList, /* List to which to append. Might be NULL */ + ExprList *pAppend, /* List of values to append. Might be NULL */ + int bIntToNull +){ + if( pAppend ){ + int i; + int nInit = pList ? pList->nExpr : 0; + for(i=0; i<pAppend->nExpr; i++){ + sqlite3 *db = pParse->db; + Expr *pDup = sqlite3ExprDup(db, pAppend->a[i].pExpr, 0); + if( db->mallocFailed ){ + sqlite3ExprDelete(db, pDup); + break; + } + if( bIntToNull ){ + int iDummy; + Expr *pSub; + pSub = sqlite3ExprSkipCollateAndLikely(pDup); + if( sqlite3ExprIsInteger(pSub, &iDummy, 0) ){ + pSub->op = TK_NULL; + pSub->flags &= ~(EP_IntValue|EP_IsTrue|EP_IsFalse); + pSub->u.zToken = 0; + } + } + pList = sqlite3ExprListAppend(pParse, pList, pDup); + if( pList ) pList->a[nInit+i].fg.sortFlags = pAppend->a[i].fg.sortFlags; + } + } + return pList; +} + +/* +** When rewriting a query, if the new subquery in the FROM clause +** contains TK_AGG_FUNCTION nodes that refer to an outer query, +** then we have to increase the Expr->op2 values of those nodes +** due to the extra subquery layer that was added. +** +** See also the incrAggDepth() routine in resolve.c +*/ +static int sqlite3WindowExtraAggFuncDepth(Walker *pWalker, Expr *pExpr){ + if( pExpr->op==TK_AGG_FUNCTION + && pExpr->op2>=pWalker->walkerDepth + ){ + pExpr->op2++; + } + return WRC_Continue; +} + +static int disallowAggregatesInOrderByCb(Walker *pWalker, Expr *pExpr){ + if( pExpr->op==TK_AGG_FUNCTION && pExpr->pAggInfo==0 ){ + assert( !ExprHasProperty(pExpr, EP_IntValue) ); + sqlite3ErrorMsg(pWalker->pParse, + "misuse of aggregate: %s()", pExpr->u.zToken); + } + return WRC_Continue; +} + +/* +** If the SELECT statement passed as the second argument does not invoke +** any SQL window functions, this function is a no-op. Otherwise, it +** rewrites the SELECT statement so that window function xStep functions +** are invoked in the correct order as described under "SELECT REWRITING" +** at the top of this file. +*/ +int sqlite3WindowRewrite(Parse *pParse, Select *p){ + int rc = SQLITE_OK; + if( p->pWin + && p->pPrior==0 + && ALWAYS((p->selFlags & SF_WinRewrite)==0) + && ALWAYS(!IN_RENAME_OBJECT) + ){ + Vdbe *v = sqlite3GetVdbe(pParse); + sqlite3 *db = pParse->db; + Select *pSub = 0; /* The subquery */ + SrcList *pSrc = p->pSrc; + Expr *pWhere = p->pWhere; + ExprList *pGroupBy = p->pGroupBy; + Expr *pHaving = p->pHaving; + ExprList *pSort = 0; + + ExprList *pSublist = 0; /* Expression list for sub-query */ + Window *pMWin = p->pWin; /* Main window object */ + Window *pWin; /* Window object iterator */ + Table *pTab; + Walker w; + + u32 selFlags = p->selFlags; + + pTab = sqlite3DbMallocZero(db, sizeof(Table)); + if( pTab==0 ){ + return sqlite3ErrorToParser(db, SQLITE_NOMEM); + } + sqlite3AggInfoPersistWalkerInit(&w, pParse); + sqlite3WalkSelect(&w, p); + if( (p->selFlags & SF_Aggregate)==0 ){ + w.xExprCallback = disallowAggregatesInOrderByCb; + w.xSelectCallback = 0; + sqlite3WalkExprList(&w, p->pOrderBy); + } + + p->pSrc = 0; + p->pWhere = 0; + p->pGroupBy = 0; + p->pHaving = 0; + p->selFlags &= ~(u32)SF_Aggregate; + p->selFlags |= SF_WinRewrite; + + /* Create the ORDER BY clause for the sub-select. This is the concatenation + ** of the window PARTITION and ORDER BY clauses. Then, if this makes it + ** redundant, remove the ORDER BY from the parent SELECT. */ + pSort = exprListAppendList(pParse, 0, pMWin->pPartition, 1); + pSort = exprListAppendList(pParse, pSort, pMWin->pOrderBy, 1); + if( pSort && p->pOrderBy && p->pOrderBy->nExpr<=pSort->nExpr ){ + int nSave = pSort->nExpr; + pSort->nExpr = p->pOrderBy->nExpr; + if( sqlite3ExprListCompare(pSort, p->pOrderBy, -1)==0 ){ + sqlite3ExprListDelete(db, p->pOrderBy); + p->pOrderBy = 0; + } + pSort->nExpr = nSave; + } + + /* Assign a cursor number for the ephemeral table used to buffer rows. + ** The OpenEphemeral instruction is coded later, after it is known how + ** many columns the table will have. */ + pMWin->iEphCsr = pParse->nTab++; + pParse->nTab += 3; + + selectWindowRewriteEList(pParse, pMWin, pSrc, p->pEList, pTab, &pSublist); + selectWindowRewriteEList(pParse, pMWin, pSrc, p->pOrderBy, pTab, &pSublist); + pMWin->nBufferCol = (pSublist ? pSublist->nExpr : 0); + + /* Append the PARTITION BY and ORDER BY expressions to the to the + ** sub-select expression list. They are required to figure out where + ** boundaries for partitions and sets of peer rows lie. */ + pSublist = exprListAppendList(pParse, pSublist, pMWin->pPartition, 0); + pSublist = exprListAppendList(pParse, pSublist, pMWin->pOrderBy, 0); + + /* Append the arguments passed to each window function to the + ** sub-select expression list. Also allocate two registers for each + ** window function - one for the accumulator, another for interim + ** results. */ + for(pWin=pMWin; pWin; pWin=pWin->pNextWin){ + ExprList *pArgs; + assert( ExprUseXList(pWin->pOwner) ); + assert( pWin->pWFunc!=0 ); + pArgs = pWin->pOwner->x.pList; + if( pWin->pWFunc->funcFlags & SQLITE_SUBTYPE ){ + selectWindowRewriteEList(pParse, pMWin, pSrc, pArgs, pTab, &pSublist); + pWin->iArgCol = (pSublist ? pSublist->nExpr : 0); + pWin->bExprArgs = 1; + }else{ + pWin->iArgCol = (pSublist ? pSublist->nExpr : 0); + pSublist = exprListAppendList(pParse, pSublist, pArgs, 0); + } + if( pWin->pFilter ){ + Expr *pFilter = sqlite3ExprDup(db, pWin->pFilter, 0); + pSublist = sqlite3ExprListAppend(pParse, pSublist, pFilter); + } + pWin->regAccum = ++pParse->nMem; + pWin->regResult = ++pParse->nMem; + sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum); + } + + /* If there is no ORDER BY or PARTITION BY clause, and the window + ** function accepts zero arguments, and there are no other columns + ** selected (e.g. "SELECT row_number() OVER () FROM t1"), it is possible + ** that pSublist is still NULL here. Add a constant expression here to + ** keep everything legal in this case. + */ + if( pSublist==0 ){ + pSublist = sqlite3ExprListAppend(pParse, 0, + sqlite3Expr(db, TK_INTEGER, "0") + ); + } + + pSub = sqlite3SelectNew( + pParse, pSublist, pSrc, pWhere, pGroupBy, pHaving, pSort, 0, 0 + ); + TREETRACE(0x40,pParse,pSub, + ("New window-function subquery in FROM clause of (%u/%p)\n", + p->selId, p)); + p->pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0); + assert( pSub!=0 || p->pSrc==0 ); /* Due to db->mallocFailed test inside + ** of sqlite3DbMallocRawNN() called from + ** sqlite3SrcListAppend() */ + if( p->pSrc==0 ){ + sqlite3SelectDelete(db, pSub); + }else if( sqlite3SrcItemAttachSubquery(pParse, &p->pSrc->a[0], pSub, 0) ){ + Table *pTab2; + p->pSrc->a[0].fg.isCorrelated = 1; + sqlite3SrcListAssignCursors(pParse, p->pSrc); + pSub->selFlags |= SF_Expanded|SF_OrderByReqd; + pTab2 = sqlite3ResultSetOfSelect(pParse, pSub, SQLITE_AFF_NONE); + pSub->selFlags |= (selFlags & SF_Aggregate); + if( pTab2==0 ){ + /* Might actually be some other kind of error, but in that case + ** pParse->nErr will be set, so if SQLITE_NOMEM is set, we will get + ** the correct error message regardless. */ + rc = SQLITE_NOMEM; + }else{ + memcpy(pTab, pTab2, sizeof(Table)); + pTab->tabFlags |= TF_Ephemeral; + p->pSrc->a[0].pSTab = pTab; + pTab = pTab2; + memset(&w, 0, sizeof(w)); + w.xExprCallback = sqlite3WindowExtraAggFuncDepth; + w.xSelectCallback = sqlite3WalkerDepthIncrease; + w.xSelectCallback2 = sqlite3WalkerDepthDecrease; + sqlite3WalkSelect(&w, pSub); + } + } + if( db->mallocFailed ) rc = SQLITE_NOMEM; + + /* Defer deleting the temporary table pTab because if an error occurred, + ** there could still be references to that table embedded in the + ** result-set or ORDER BY clause of the SELECT statement p. */ + sqlite3ParserAddCleanup(pParse, sqlite3DbFree, pTab); + } + + assert( rc==SQLITE_OK || pParse->nErr!=0 ); + return rc; +} + +/* +** Unlink the Window object from the Select to which it is attached, +** if it is attached. +*/ +void sqlite3WindowUnlinkFromSelect(Window *p){ + if( p->ppThis ){ + *p->ppThis = p->pNextWin; + if( p->pNextWin ) p->pNextWin->ppThis = p->ppThis; + p->ppThis = 0; + } +} + +/* +** Free the Window object passed as the second argument. +*/ +void sqlite3WindowDelete(sqlite3 *db, Window *p){ + if( p ){ + sqlite3WindowUnlinkFromSelect(p); + sqlite3ExprDelete(db, p->pFilter); + sqlite3ExprListDelete(db, p->pPartition); + sqlite3ExprListDelete(db, p->pOrderBy); + sqlite3ExprDelete(db, p->pEnd); + sqlite3ExprDelete(db, p->pStart); + sqlite3DbFree(db, p->zName); + sqlite3DbFree(db, p->zBase); + sqlite3DbFree(db, p); + } +} + +/* +** Free the linked list of Window objects starting at the second argument. +*/ +void sqlite3WindowListDelete(sqlite3 *db, Window *p){ + while( p ){ + Window *pNext = p->pNextWin; + sqlite3WindowDelete(db, p); + p = pNext; + } +} + +/* +** The argument expression is an PRECEDING or FOLLOWING offset. The +** value should be a non-negative integer. If the value is not a +** constant, change it to NULL. The fact that it is then a non-negative +** integer will be caught later. But it is important not to leave +** variable values in the expression tree. +*/ +static Expr *sqlite3WindowOffsetExpr(Parse *pParse, Expr *pExpr){ + if( 0==sqlite3ExprIsConstant(0,pExpr) ){ + if( IN_RENAME_OBJECT ) sqlite3RenameExprUnmap(pParse, pExpr); + sqlite3ExprDelete(pParse->db, pExpr); + pExpr = sqlite3ExprAlloc(pParse->db, TK_NULL, 0, 0); + } + return pExpr; +} + +/* +** Allocate and return a new Window object describing a Window Definition. +*/ +Window *sqlite3WindowAlloc( + Parse *pParse, /* Parsing context */ + int eType, /* Frame type. TK_RANGE, TK_ROWS, TK_GROUPS, or 0 */ + int eStart, /* Start type: CURRENT, PRECEDING, FOLLOWING, UNBOUNDED */ + Expr *pStart, /* Start window size if TK_PRECEDING or FOLLOWING */ + int eEnd, /* End type: CURRENT, FOLLOWING, TK_UNBOUNDED, PRECEDING */ + Expr *pEnd, /* End window size if TK_FOLLOWING or PRECEDING */ + u8 eExclude /* EXCLUDE clause */ +){ + Window *pWin = 0; + int bImplicitFrame = 0; + + /* Parser assures the following: */ + assert( eType==0 || eType==TK_RANGE || eType==TK_ROWS || eType==TK_GROUPS ); + assert( eStart==TK_CURRENT || eStart==TK_PRECEDING + || eStart==TK_UNBOUNDED || eStart==TK_FOLLOWING ); + assert( eEnd==TK_CURRENT || eEnd==TK_FOLLOWING + || eEnd==TK_UNBOUNDED || eEnd==TK_PRECEDING ); + assert( (eStart==TK_PRECEDING || eStart==TK_FOLLOWING)==(pStart!=0) ); + assert( (eEnd==TK_FOLLOWING || eEnd==TK_PRECEDING)==(pEnd!=0) ); + + if( eType==0 ){ + bImplicitFrame = 1; + eType = TK_RANGE; + } + + /* Additionally, the + ** starting boundary type may not occur earlier in the following list than + ** the ending boundary type: + ** + ** UNBOUNDED PRECEDING + ** <expr> PRECEDING + ** CURRENT ROW + ** <expr> FOLLOWING + ** UNBOUNDED FOLLOWING + ** + ** The parser ensures that "UNBOUNDED PRECEDING" cannot be used as an ending + ** boundary, and than "UNBOUNDED FOLLOWING" cannot be used as a starting + ** frame boundary. + */ + if( (eStart==TK_CURRENT && eEnd==TK_PRECEDING) + || (eStart==TK_FOLLOWING && (eEnd==TK_PRECEDING || eEnd==TK_CURRENT)) + ){ + sqlite3ErrorMsg(pParse, "unsupported frame specification"); + goto windowAllocErr; + } + + pWin = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window)); + if( pWin==0 ) goto windowAllocErr; + pWin->eFrmType = eType; + pWin->eStart = eStart; + pWin->eEnd = eEnd; + if( eExclude==0 && OptimizationDisabled(pParse->db, SQLITE_WindowFunc) ){ + eExclude = TK_NO; + } + pWin->eExclude = eExclude; + pWin->bImplicitFrame = bImplicitFrame; + pWin->pEnd = sqlite3WindowOffsetExpr(pParse, pEnd); + pWin->pStart = sqlite3WindowOffsetExpr(pParse, pStart); + return pWin; + +windowAllocErr: + sqlite3ExprDelete(pParse->db, pEnd); + sqlite3ExprDelete(pParse->db, pStart); + return 0; +} + +/* +** Attach PARTITION and ORDER BY clauses pPartition and pOrderBy to window +** pWin. Also, if parameter pBase is not NULL, set pWin->zBase to the +** equivalent nul-terminated string. +*/ +Window *sqlite3WindowAssemble( + Parse *pParse, + Window *pWin, + ExprList *pPartition, + ExprList *pOrderBy, + Token *pBase +){ + if( pWin ){ + pWin->pPartition = pPartition; + pWin->pOrderBy = pOrderBy; + if( pBase ){ + pWin->zBase = sqlite3DbStrNDup(pParse->db, pBase->z, pBase->n); + } + }else{ + sqlite3ExprListDelete(pParse->db, pPartition); + sqlite3ExprListDelete(pParse->db, pOrderBy); + } + return pWin; +} + +/* +** Window *pWin has just been created from a WINDOW clause. Token pBase +** is the base window. Earlier windows from the same WINDOW clause are +** stored in the linked list starting at pWin->pNextWin. This function +** either updates *pWin according to the base specification, or else +** leaves an error in pParse. +*/ +void sqlite3WindowChain(Parse *pParse, Window *pWin, Window *pList){ + if( pWin->zBase ){ + sqlite3 *db = pParse->db; + Window *pExist = windowFind(pParse, pList, pWin->zBase); + if( pExist ){ + const char *zErr = 0; + /* Check for errors */ + if( pWin->pPartition ){ + zErr = "PARTITION clause"; + }else if( pExist->pOrderBy && pWin->pOrderBy ){ + zErr = "ORDER BY clause"; + }else if( pExist->bImplicitFrame==0 ){ + zErr = "frame specification"; + } + if( zErr ){ + sqlite3ErrorMsg(pParse, + "cannot override %s of window: %s", zErr, pWin->zBase + ); + }else{ + pWin->pPartition = sqlite3ExprListDup(db, pExist->pPartition, 0); + if( pExist->pOrderBy ){ + assert( pWin->pOrderBy==0 ); + pWin->pOrderBy = sqlite3ExprListDup(db, pExist->pOrderBy, 0); + } + sqlite3DbFree(db, pWin->zBase); + pWin->zBase = 0; + } + } + } +} + +/* +** Attach window object pWin to expression p. +*/ +void sqlite3WindowAttach(Parse *pParse, Expr *p, Window *pWin){ + if( p ){ + assert( p->op==TK_FUNCTION ); + assert( pWin ); + assert( ExprIsFullSize(p) ); + p->y.pWin = pWin; + ExprSetProperty(p, EP_WinFunc|EP_FullSize); + pWin->pOwner = p; + if( (p->flags & EP_Distinct) && pWin->eFrmType!=TK_FILTER ){ + sqlite3ErrorMsg(pParse, + "DISTINCT is not supported for window functions" + ); + } + }else{ + sqlite3WindowDelete(pParse->db, pWin); + } +} + +/* +** Possibly link window pWin into the list at pSel->pWin (window functions +** to be processed as part of SELECT statement pSel). The window is linked +** in if either (a) there are no other windows already linked to this +** SELECT, or (b) the windows already linked use a compatible window frame. +*/ +void sqlite3WindowLink(Select *pSel, Window *pWin){ + if( pSel ){ + if( 0==pSel->pWin || 0==sqlite3WindowCompare(0, pSel->pWin, pWin, 0) ){ + pWin->pNextWin = pSel->pWin; + if( pSel->pWin ){ + pSel->pWin->ppThis = &pWin->pNextWin; + } + pSel->pWin = pWin; + pWin->ppThis = &pSel->pWin; + }else{ + if( sqlite3ExprListCompare(pWin->pPartition, pSel->pWin->pPartition,-1) ){ + pSel->selFlags |= SF_MultiPart; + } + } + } +} + +/* +** Return 0 if the two window objects are identical, 1 if they are +** different, or 2 if it cannot be determined if the objects are identical +** or not. Identical window objects can be processed in a single scan. +*/ +int sqlite3WindowCompare( + const Parse *pParse, + const Window *p1, + const Window *p2, + int bFilter +){ + int res; + if( NEVER(p1==0) || NEVER(p2==0) ) return 1; + if( p1->eFrmType!=p2->eFrmType ) return 1; + if( p1->eStart!=p2->eStart ) return 1; + if( p1->eEnd!=p2->eEnd ) return 1; + if( p1->eExclude!=p2->eExclude ) return 1; + if( sqlite3ExprCompare(pParse, p1->pStart, p2->pStart, -1) ) return 1; + if( sqlite3ExprCompare(pParse, p1->pEnd, p2->pEnd, -1) ) return 1; + if( (res = sqlite3ExprListCompare(p1->pPartition, p2->pPartition, -1)) ){ + return res; + } + if( (res = sqlite3ExprListCompare(p1->pOrderBy, p2->pOrderBy, -1)) ){ + return res; + } + if( bFilter ){ + if( (res = sqlite3ExprCompare(pParse, p1->pFilter, p2->pFilter, -1)) ){ + return res; + } + } + return 0; +} + + +/* +** This is called by code in select.c before it calls sqlite3WhereBegin() +** to begin iterating through the sub-query results. It is used to allocate +** and initialize registers and cursors used by sqlite3WindowCodeStep(). +*/ +void sqlite3WindowCodeInit(Parse *pParse, Select *pSelect){ + Window *pWin; + int nEphExpr; + Window *pMWin; + Vdbe *v; + + assert( pSelect->pSrc->a[0].fg.isSubquery ); + nEphExpr = pSelect->pSrc->a[0].u4.pSubq->pSelect->pEList->nExpr; + pMWin = pSelect->pWin; + v = sqlite3GetVdbe(pParse); + + sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pMWin->iEphCsr, nEphExpr); + sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+1, pMWin->iEphCsr); + sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+2, pMWin->iEphCsr); + sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+3, pMWin->iEphCsr); + + /* Allocate registers to use for PARTITION BY values, if any. Initialize + ** said registers to NULL. */ + if( pMWin->pPartition ){ + int nExpr = pMWin->pPartition->nExpr; + pMWin->regPart = pParse->nMem+1; + pParse->nMem += nExpr; + sqlite3VdbeAddOp3(v, OP_Null, 0, pMWin->regPart, pMWin->regPart+nExpr-1); + } + + pMWin->regOne = ++pParse->nMem; + sqlite3VdbeAddOp2(v, OP_Integer, 1, pMWin->regOne); + + if( pMWin->eExclude ){ + pMWin->regStartRowid = ++pParse->nMem; + pMWin->regEndRowid = ++pParse->nMem; + pMWin->csrApp = pParse->nTab++; + sqlite3VdbeAddOp2(v, OP_Integer, 1, pMWin->regStartRowid); + sqlite3VdbeAddOp2(v, OP_Integer, 0, pMWin->regEndRowid); + sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->csrApp, pMWin->iEphCsr); + return; + } + + for(pWin=pMWin; pWin; pWin=pWin->pNextWin){ + FuncDef *p = pWin->pWFunc; + if( (p->funcFlags & SQLITE_FUNC_MINMAX) && pWin->eStart!=TK_UNBOUNDED ){ + /* The inline versions of min() and max() require a single ephemeral + ** table and 3 registers. The registers are used as follows: + ** + ** regApp+0: slot to copy min()/max() argument to for MakeRecord + ** regApp+1: integer value used to ensure keys are unique + ** regApp+2: output of MakeRecord + */ + ExprList *pList; + KeyInfo *pKeyInfo; + assert( ExprUseXList(pWin->pOwner) ); + pList = pWin->pOwner->x.pList; + pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pList, 0, 0); + pWin->csrApp = pParse->nTab++; + pWin->regApp = pParse->nMem+1; + pParse->nMem += 3; + if( pKeyInfo && pWin->pWFunc->zName[1]=='i' ){ + assert( pKeyInfo->aSortFlags[0]==0 ); + pKeyInfo->aSortFlags[0] = KEYINFO_ORDER_DESC; + } + sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pWin->csrApp, 2); + sqlite3VdbeAppendP4(v, pKeyInfo, P4_KEYINFO); + sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1); + } + else if( p->zName==nth_valueName || p->zName==first_valueName ){ + /* Allocate two registers at pWin->regApp. These will be used to + ** store the start and end index of the current frame. */ + pWin->regApp = pParse->nMem+1; + pWin->csrApp = pParse->nTab++; + pParse->nMem += 2; + sqlite3VdbeAddOp2(v, OP_OpenDup, pWin->csrApp, pMWin->iEphCsr); + } + else if( p->zName==leadName || p->zName==lagName ){ + pWin->csrApp = pParse->nTab++; + sqlite3VdbeAddOp2(v, OP_OpenDup, pWin->csrApp, pMWin->iEphCsr); + } + } +} + +#define WINDOW_STARTING_INT 0 +#define WINDOW_ENDING_INT 1 +#define WINDOW_NTH_VALUE_INT 2 +#define WINDOW_STARTING_NUM 3 +#define WINDOW_ENDING_NUM 4 + +/* +** A "PRECEDING <expr>" (eCond==0) or "FOLLOWING <expr>" (eCond==1) or the +** value of the second argument to nth_value() (eCond==2) has just been +** evaluated and the result left in register reg. This function generates VM +** code to check that the value is a non-negative integer and throws an +** exception if it is not. +*/ +static void windowCheckValue(Parse *pParse, int reg, int eCond){ + static const char *azErr[] = { + "frame starting offset must be a non-negative integer", + "frame ending offset must be a non-negative integer", + "second argument to nth_value must be a positive integer", + "frame starting offset must be a non-negative number", + "frame ending offset must be a non-negative number", + }; + static int aOp[] = { OP_Ge, OP_Ge, OP_Gt, OP_Ge, OP_Ge }; + Vdbe *v = sqlite3GetVdbe(pParse); + int regZero = sqlite3GetTempReg(pParse); + assert( eCond>=0 && eCond<ArraySize(azErr) ); + sqlite3VdbeAddOp2(v, OP_Integer, 0, regZero); + if( eCond>=WINDOW_STARTING_NUM ){ + int regString = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp4(v, OP_String8, 0, regString, 0, "", P4_STATIC); + sqlite3VdbeAddOp3(v, OP_Ge, regString, sqlite3VdbeCurrentAddr(v)+2, reg); + sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC|SQLITE_JUMPIFNULL); + VdbeCoverage(v); + assert( eCond==3 || eCond==4 ); + VdbeCoverageIf(v, eCond==3); + VdbeCoverageIf(v, eCond==4); + }else{ + sqlite3VdbeAddOp2(v, OP_MustBeInt, reg, sqlite3VdbeCurrentAddr(v)+2); + VdbeCoverage(v); + assert( eCond==0 || eCond==1 || eCond==2 ); + VdbeCoverageIf(v, eCond==0); + VdbeCoverageIf(v, eCond==1); + VdbeCoverageIf(v, eCond==2); + } + sqlite3VdbeAddOp3(v, aOp[eCond], regZero, sqlite3VdbeCurrentAddr(v)+2, reg); + sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC); + VdbeCoverageNeverNullIf(v, eCond==0); /* NULL case captured by */ + VdbeCoverageNeverNullIf(v, eCond==1); /* the OP_MustBeInt */ + VdbeCoverageNeverNullIf(v, eCond==2); + VdbeCoverageNeverNullIf(v, eCond==3); /* NULL case caught by */ + VdbeCoverageNeverNullIf(v, eCond==4); /* the OP_Ge */ + sqlite3MayAbort(pParse); + sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_ERROR, OE_Abort); + sqlite3VdbeAppendP4(v, (void*)azErr[eCond], P4_STATIC); + sqlite3ReleaseTempReg(pParse, regZero); +} + +/* +** Return the number of arguments passed to the window-function associated +** with the object passed as the only argument to this function. +*/ +static int windowArgCount(Window *pWin){ + const ExprList *pList; + assert( ExprUseXList(pWin->pOwner) ); + pList = pWin->pOwner->x.pList; + return (pList ? pList->nExpr : 0); +} + +typedef struct WindowCodeArg WindowCodeArg; +typedef struct WindowCsrAndReg WindowCsrAndReg; + +/* +** See comments above struct WindowCodeArg. +*/ +struct WindowCsrAndReg { + int csr; /* Cursor number */ + int reg; /* First in array of peer values */ +}; + +/* +** A single instance of this structure is allocated on the stack by +** sqlite3WindowCodeStep() and a pointer to it passed to the various helper +** routines. This is to reduce the number of arguments required by each +** helper function. +** +** regArg: +** Each window function requires an accumulator register (just as an +** ordinary aggregate function does). This variable is set to the first +** in an array of accumulator registers - one for each window function +** in the WindowCodeArg.pMWin list. +** +** eDelete: +** The window functions implementation sometimes caches the input rows +** that it processes in a temporary table. If it is not zero, this +** variable indicates when rows may be removed from the temp table (in +** order to reduce memory requirements - it would always be safe just +** to leave them there). Possible values for eDelete are: +** +** WINDOW_RETURN_ROW: +** An input row can be discarded after it is returned to the caller. +** +** WINDOW_AGGINVERSE: +** An input row can be discarded after the window functions xInverse() +** callbacks have been invoked in it. +** +** WINDOW_AGGSTEP: +** An input row can be discarded after the window functions xStep() +** callbacks have been invoked in it. +** +** start,current,end +** Consider a window-frame similar to the following: +** +** (ORDER BY a, b GROUPS BETWEEN 2 PRECEDING AND 2 FOLLOWING) +** +** The windows functions implementation caches the input rows in a temp +** table, sorted by "a, b" (it actually populates the cache lazily, and +** aggressively removes rows once they are no longer required, but that's +** a mere detail). It keeps three cursors open on the temp table. One +** (current) that points to the next row to return to the query engine +** once its window function values have been calculated. Another (end) +** points to the next row to call the xStep() method of each window function +** on (so that it is 2 groups ahead of current). And a third (start) that +** points to the next row to call the xInverse() method of each window +** function on. +** +** Each cursor (start, current and end) consists of a VDBE cursor +** (WindowCsrAndReg.csr) and an array of registers (starting at +** WindowCodeArg.reg) that always contains a copy of the peer values +** read from the corresponding cursor. +** +** Depending on the window-frame in question, all three cursors may not +** be required. In this case both WindowCodeArg.csr and reg are set to +** 0. +*/ +struct WindowCodeArg { + Parse *pParse; /* Parse context */ + Window *pMWin; /* First in list of functions being processed */ + Vdbe *pVdbe; /* VDBE object */ + int addrGosub; /* OP_Gosub to this address to return one row */ + int regGosub; /* Register used with OP_Gosub(addrGosub) */ + int regArg; /* First in array of accumulator registers */ + int eDelete; /* See above */ + int regRowid; + + WindowCsrAndReg start; + WindowCsrAndReg current; + WindowCsrAndReg end; +}; + +/* +** Generate VM code to read the window frames peer values from cursor csr into +** an array of registers starting at reg. +*/ +static void windowReadPeerValues( + WindowCodeArg *p, + int csr, + int reg +){ + Window *pMWin = p->pMWin; + ExprList *pOrderBy = pMWin->pOrderBy; + if( pOrderBy ){ + Vdbe *v = sqlite3GetVdbe(p->pParse); + ExprList *pPart = pMWin->pPartition; + int iColOff = pMWin->nBufferCol + (pPart ? pPart->nExpr : 0); + int i; + for(i=0; i<pOrderBy->nExpr; i++){ + sqlite3VdbeAddOp3(v, OP_Column, csr, iColOff+i, reg+i); + } + } +} + +/* +** Generate VM code to invoke either xStep() (if bInverse is 0) or +** xInverse (if bInverse is non-zero) for each window function in the +** linked list starting at pMWin. Or, for built-in window functions +** that do not use the standard function API, generate the required +** inline VM code. +** +** If argument csr is greater than or equal to 0, then argument reg is +** the first register in an array of registers guaranteed to be large +** enough to hold the array of arguments for each function. In this case +** the arguments are extracted from the current row of csr into the +** array of registers before invoking OP_AggStep or OP_AggInverse +** +** Or, if csr is less than zero, then the array of registers at reg is +** already populated with all columns from the current row of the sub-query. +** +** If argument regPartSize is non-zero, then it is a register containing the +** number of rows in the current partition. +*/ +static void windowAggStep( + WindowCodeArg *p, + Window *pMWin, /* Linked list of window functions */ + int csr, /* Read arguments from this cursor */ + int bInverse, /* True to invoke xInverse instead of xStep */ + int reg /* Array of registers */ +){ + Parse *pParse = p->pParse; + Vdbe *v = sqlite3GetVdbe(pParse); + Window *pWin; + for(pWin=pMWin; pWin; pWin=pWin->pNextWin){ + FuncDef *pFunc = pWin->pWFunc; + int regArg; + int nArg = pWin->bExprArgs ? 0 : windowArgCount(pWin); + int i; + int addrIf = 0; + + assert( bInverse==0 || pWin->eStart!=TK_UNBOUNDED ); + + /* All OVER clauses in the same window function aggregate step must + ** be the same. */ + assert( pWin==pMWin || sqlite3WindowCompare(pParse,pWin,pMWin,0)!=1 ); + + for(i=0; i<nArg; i++){ + if( i!=1 || pFunc->zName!=nth_valueName ){ + sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+i, reg+i); + }else{ + sqlite3VdbeAddOp3(v, OP_Column, pMWin->iEphCsr, pWin->iArgCol+i, reg+i); + } + } + regArg = reg; + + if( pWin->pFilter ){ + int regTmp; + assert( ExprUseXList(pWin->pOwner) ); + assert( pWin->bExprArgs || !nArg ||nArg==pWin->pOwner->x.pList->nExpr ); + assert( pWin->bExprArgs || nArg ||pWin->pOwner->x.pList==0 ); + regTmp = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+nArg,regTmp); + addrIf = sqlite3VdbeAddOp3(v, OP_IfNot, regTmp, 0, 1); + VdbeCoverage(v); + sqlite3ReleaseTempReg(pParse, regTmp); + } + + if( pMWin->regStartRowid==0 + && (pFunc->funcFlags & SQLITE_FUNC_MINMAX) + && (pWin->eStart!=TK_UNBOUNDED) + ){ + int addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull, regArg); + VdbeCoverage(v); + if( bInverse==0 ){ + sqlite3VdbeAddOp2(v, OP_AddImm, pWin->regApp+1, 1); + sqlite3VdbeAddOp2(v, OP_SCopy, regArg, pWin->regApp); + sqlite3VdbeAddOp3(v, OP_MakeRecord, pWin->regApp, 2, pWin->regApp+2); + sqlite3VdbeAddOp2(v, OP_IdxInsert, pWin->csrApp, pWin->regApp+2); + }else{ + sqlite3VdbeAddOp4Int(v, OP_SeekGE, pWin->csrApp, 0, regArg, 1); + VdbeCoverageNeverTaken(v); + sqlite3VdbeAddOp1(v, OP_Delete, pWin->csrApp); + sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2); + } + sqlite3VdbeJumpHere(v, addrIsNull); + }else if( pWin->regApp ){ + assert( pWin->pFilter==0 ); + assert( pFunc->zName==nth_valueName + || pFunc->zName==first_valueName + ); + assert( bInverse==0 || bInverse==1 ); + sqlite3VdbeAddOp2(v, OP_AddImm, pWin->regApp+1-bInverse, 1); + }else if( pFunc->xSFunc!=noopStepFunc ){ + if( pWin->bExprArgs ){ + int iOp = sqlite3VdbeCurrentAddr(v); + int iEnd; + + assert( ExprUseXList(pWin->pOwner) ); + nArg = pWin->pOwner->x.pList->nExpr; + regArg = sqlite3GetTempRange(pParse, nArg); + sqlite3ExprCodeExprList(pParse, pWin->pOwner->x.pList, regArg, 0, 0); + + for(iEnd=sqlite3VdbeCurrentAddr(v); iOp<iEnd; iOp++){ + VdbeOp *pOp = sqlite3VdbeGetOp(v, iOp); + if( pOp->opcode==OP_Column && pOp->p1==pMWin->iEphCsr ){ + pOp->p1 = csr; + } + } + } + if( pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){ + CollSeq *pColl; + assert( nArg>0 ); + assert( ExprUseXList(pWin->pOwner) ); + pColl = sqlite3ExprNNCollSeq(pParse, pWin->pOwner->x.pList->a[0].pExpr); + sqlite3VdbeAddOp4(v, OP_CollSeq, 0,0,0, (const char*)pColl, P4_COLLSEQ); + } + sqlite3VdbeAddOp3(v, bInverse? OP_AggInverse : OP_AggStep, + bInverse, regArg, pWin->regAccum); + sqlite3VdbeAppendP4(v, pFunc, P4_FUNCDEF); + sqlite3VdbeChangeP5(v, (u16)nArg); + if( pWin->bExprArgs ){ + sqlite3ReleaseTempRange(pParse, regArg, nArg); + } + } + + if( addrIf ) sqlite3VdbeJumpHere(v, addrIf); + } +} + +/* +** Values that may be passed as the second argument to windowCodeOp(). +*/ +#define WINDOW_RETURN_ROW 1 +#define WINDOW_AGGINVERSE 2 +#define WINDOW_AGGSTEP 3 + +/* +** Generate VM code to invoke either xValue() (bFin==0) or xFinalize() +** (bFin==1) for each window function in the linked list starting at +** pMWin. Or, for built-in window-functions that do not use the standard +** API, generate the equivalent VM code. +*/ +static void windowAggFinal(WindowCodeArg *p, int bFin){ + Parse *pParse = p->pParse; + Window *pMWin = p->pMWin; + Vdbe *v = sqlite3GetVdbe(pParse); + Window *pWin; + + for(pWin=pMWin; pWin; pWin=pWin->pNextWin){ + if( pMWin->regStartRowid==0 + && (pWin->pWFunc->funcFlags & SQLITE_FUNC_MINMAX) + && (pWin->eStart!=TK_UNBOUNDED) + ){ + sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult); + sqlite3VdbeAddOp1(v, OP_Last, pWin->csrApp); + VdbeCoverage(v); + sqlite3VdbeAddOp3(v, OP_Column, pWin->csrApp, 0, pWin->regResult); + sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2); + }else if( pWin->regApp ){ + assert( pMWin->regStartRowid==0 ); + }else{ + int nArg = windowArgCount(pWin); + if( bFin ){ + sqlite3VdbeAddOp2(v, OP_AggFinal, pWin->regAccum, nArg); + sqlite3VdbeAppendP4(v, pWin->pWFunc, P4_FUNCDEF); + sqlite3VdbeAddOp2(v, OP_Copy, pWin->regAccum, pWin->regResult); + sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum); + }else{ + sqlite3VdbeAddOp3(v, OP_AggValue,pWin->regAccum,nArg,pWin->regResult); + sqlite3VdbeAppendP4(v, pWin->pWFunc, P4_FUNCDEF); + } + } + } +} + +/* +** Generate code to calculate the current values of all window functions in the +** p->pMWin list by doing a full scan of the current window frame. Store the +** results in the Window.regResult registers, ready to return the upper +** layer. +*/ +static void windowFullScan(WindowCodeArg *p){ + Window *pWin; + Parse *pParse = p->pParse; + Window *pMWin = p->pMWin; + Vdbe *v = p->pVdbe; + + int regCRowid = 0; /* Current rowid value */ + int regCPeer = 0; /* Current peer values */ + int regRowid = 0; /* AggStep rowid value */ + int regPeer = 0; /* AggStep peer values */ + + int nPeer; + int lblNext; + int lblBrk; + int addrNext; + int csr; + + VdbeModuleComment((v, "windowFullScan begin")); + + assert( pMWin!=0 ); + csr = pMWin->csrApp; + nPeer = (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0); + + lblNext = sqlite3VdbeMakeLabel(pParse); + lblBrk = sqlite3VdbeMakeLabel(pParse); + + regCRowid = sqlite3GetTempReg(pParse); + regRowid = sqlite3GetTempReg(pParse); + if( nPeer ){ + regCPeer = sqlite3GetTempRange(pParse, nPeer); + regPeer = sqlite3GetTempRange(pParse, nPeer); + } + + sqlite3VdbeAddOp2(v, OP_Rowid, pMWin->iEphCsr, regCRowid); + windowReadPeerValues(p, pMWin->iEphCsr, regCPeer); + + for(pWin=pMWin; pWin; pWin=pWin->pNextWin){ + sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum); + } + + sqlite3VdbeAddOp3(v, OP_SeekGE, csr, lblBrk, pMWin->regStartRowid); + VdbeCoverage(v); + addrNext = sqlite3VdbeCurrentAddr(v); + sqlite3VdbeAddOp2(v, OP_Rowid, csr, regRowid); + sqlite3VdbeAddOp3(v, OP_Gt, pMWin->regEndRowid, lblBrk, regRowid); + VdbeCoverageNeverNull(v); + + if( pMWin->eExclude==TK_CURRENT ){ + sqlite3VdbeAddOp3(v, OP_Eq, regCRowid, lblNext, regRowid); + VdbeCoverageNeverNull(v); + }else if( pMWin->eExclude!=TK_NO ){ + int addr; + int addrEq = 0; + KeyInfo *pKeyInfo = 0; + + if( pMWin->pOrderBy ){ + pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pMWin->pOrderBy, 0, 0); + } + if( pMWin->eExclude==TK_TIES ){ + addrEq = sqlite3VdbeAddOp3(v, OP_Eq, regCRowid, 0, regRowid); + VdbeCoverageNeverNull(v); + } + if( pKeyInfo ){ + windowReadPeerValues(p, csr, regPeer); + sqlite3VdbeAddOp3(v, OP_Compare, regPeer, regCPeer, nPeer); + sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO); + addr = sqlite3VdbeCurrentAddr(v)+1; + sqlite3VdbeAddOp3(v, OP_Jump, addr, lblNext, addr); + VdbeCoverageEqNe(v); + }else{ + sqlite3VdbeAddOp2(v, OP_Goto, 0, lblNext); + } + if( addrEq ) sqlite3VdbeJumpHere(v, addrEq); + } + + windowAggStep(p, pMWin, csr, 0, p->regArg); + + sqlite3VdbeResolveLabel(v, lblNext); + sqlite3VdbeAddOp2(v, OP_Next, csr, addrNext); + VdbeCoverage(v); + sqlite3VdbeJumpHere(v, addrNext-1); + sqlite3VdbeJumpHere(v, addrNext+1); + sqlite3ReleaseTempReg(pParse, regRowid); + sqlite3ReleaseTempReg(pParse, regCRowid); + if( nPeer ){ + sqlite3ReleaseTempRange(pParse, regPeer, nPeer); + sqlite3ReleaseTempRange(pParse, regCPeer, nPeer); + } + + windowAggFinal(p, 1); + VdbeModuleComment((v, "windowFullScan end")); +} + +/* +** Invoke the sub-routine at regGosub (generated by code in select.c) to +** return the current row of Window.iEphCsr. If all window functions are +** aggregate window functions that use the standard API, a single +** OP_Gosub instruction is all that this routine generates. Extra VM code +** for per-row processing is only generated for the following built-in window +** functions: +** +** nth_value() +** first_value() +** lag() +** lead() +*/ +static void windowReturnOneRow(WindowCodeArg *p){ + Window *pMWin = p->pMWin; + Vdbe *v = p->pVdbe; + + if( pMWin->regStartRowid ){ + windowFullScan(p); + }else{ + Parse *pParse = p->pParse; + Window *pWin; + + for(pWin=pMWin; pWin; pWin=pWin->pNextWin){ + FuncDef *pFunc = pWin->pWFunc; + assert( ExprUseXList(pWin->pOwner) ); + if( pFunc->zName==nth_valueName + || pFunc->zName==first_valueName + ){ + int csr = pWin->csrApp; + int lbl = sqlite3VdbeMakeLabel(pParse); + int tmpReg = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult); + + if( pFunc->zName==nth_valueName ){ + sqlite3VdbeAddOp3(v, OP_Column,pMWin->iEphCsr,pWin->iArgCol+1,tmpReg); + windowCheckValue(pParse, tmpReg, 2); + }else{ + sqlite3VdbeAddOp2(v, OP_Integer, 1, tmpReg); + } + sqlite3VdbeAddOp3(v, OP_Add, tmpReg, pWin->regApp, tmpReg); + sqlite3VdbeAddOp3(v, OP_Gt, pWin->regApp+1, lbl, tmpReg); + VdbeCoverageNeverNull(v); + sqlite3VdbeAddOp3(v, OP_SeekRowid, csr, 0, tmpReg); + VdbeCoverageNeverTaken(v); + sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol, pWin->regResult); + sqlite3VdbeResolveLabel(v, lbl); + sqlite3ReleaseTempReg(pParse, tmpReg); + } + else if( pFunc->zName==leadName || pFunc->zName==lagName ){ + int nArg = pWin->pOwner->x.pList->nExpr; + int csr = pWin->csrApp; + int lbl = sqlite3VdbeMakeLabel(pParse); + int tmpReg = sqlite3GetTempReg(pParse); + int iEph = pMWin->iEphCsr; + + if( nArg<3 ){ + sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult); + }else{ + sqlite3VdbeAddOp3(v, OP_Column, iEph,pWin->iArgCol+2,pWin->regResult); + } + sqlite3VdbeAddOp2(v, OP_Rowid, iEph, tmpReg); + if( nArg<2 ){ + int val = (pFunc->zName==leadName ? 1 : -1); + sqlite3VdbeAddOp2(v, OP_AddImm, tmpReg, val); + }else{ + int op = (pFunc->zName==leadName ? OP_Add : OP_Subtract); + int tmpReg2 = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp3(v, OP_Column, iEph, pWin->iArgCol+1, tmpReg2); + sqlite3VdbeAddOp3(v, op, tmpReg2, tmpReg, tmpReg); + sqlite3ReleaseTempReg(pParse, tmpReg2); + } + + sqlite3VdbeAddOp3(v, OP_SeekRowid, csr, lbl, tmpReg); + VdbeCoverage(v); + sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol, pWin->regResult); + sqlite3VdbeResolveLabel(v, lbl); + sqlite3ReleaseTempReg(pParse, tmpReg); + } + } + } + sqlite3VdbeAddOp2(v, OP_Gosub, p->regGosub, p->addrGosub); +} + +/* +** Generate code to set the accumulator register for each window function +** in the linked list passed as the second argument to NULL. And perform +** any equivalent initialization required by any built-in window functions +** in the list. +*/ +static int windowInitAccum(Parse *pParse, Window *pMWin){ + Vdbe *v = sqlite3GetVdbe(pParse); + int regArg; + int nArg = 0; + Window *pWin; + for(pWin=pMWin; pWin; pWin=pWin->pNextWin){ + FuncDef *pFunc = pWin->pWFunc; + assert( pWin->regAccum ); + sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum); + nArg = MAX(nArg, windowArgCount(pWin)); + if( pMWin->regStartRowid==0 ){ + if( pFunc->zName==nth_valueName || pFunc->zName==first_valueName ){ + sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp); + sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1); + } + + if( (pFunc->funcFlags & SQLITE_FUNC_MINMAX) && pWin->csrApp ){ + assert( pWin->eStart!=TK_UNBOUNDED ); + sqlite3VdbeAddOp1(v, OP_ResetSorter, pWin->csrApp); + sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1); + } + } + } + regArg = pParse->nMem+1; + pParse->nMem += nArg; + return regArg; +} + +/* +** Return true if the current frame should be cached in the ephemeral table, +** even if there are no xInverse() calls required. +*/ +static int windowCacheFrame(Window *pMWin){ + Window *pWin; + if( pMWin->regStartRowid ) return 1; + for(pWin=pMWin; pWin; pWin=pWin->pNextWin){ + FuncDef *pFunc = pWin->pWFunc; + if( (pFunc->zName==nth_valueName) + || (pFunc->zName==first_valueName) + || (pFunc->zName==leadName) + || (pFunc->zName==lagName) + ){ + return 1; + } + } + return 0; +} + +/* +** regOld and regNew are each the first register in an array of size +** pOrderBy->nExpr. This function generates code to compare the two +** arrays of registers using the collation sequences and other comparison +** parameters specified by pOrderBy. +** +** If the two arrays are not equal, the contents of regNew is copied to +** regOld and control falls through. Otherwise, if the contents of the arrays +** are equal, an OP_Goto is executed. The address of the OP_Goto is returned. +*/ +static void windowIfNewPeer( + Parse *pParse, + ExprList *pOrderBy, + int regNew, /* First in array of new values */ + int regOld, /* First in array of old values */ + int addr /* Jump here */ +){ + Vdbe *v = sqlite3GetVdbe(pParse); + if( pOrderBy ){ + int nVal = pOrderBy->nExpr; + KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pOrderBy, 0, 0); + sqlite3VdbeAddOp3(v, OP_Compare, regOld, regNew, nVal); + sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO); + sqlite3VdbeAddOp3(v, OP_Jump, + sqlite3VdbeCurrentAddr(v)+1, addr, sqlite3VdbeCurrentAddr(v)+1 + ); + VdbeCoverageEqNe(v); + sqlite3VdbeAddOp3(v, OP_Copy, regNew, regOld, nVal-1); + }else{ + sqlite3VdbeAddOp2(v, OP_Goto, 0, addr); + } +} + +/* +** This function is called as part of generating VM programs for RANGE +** offset PRECEDING/FOLLOWING frame boundaries. Assuming "ASC" order for +** the ORDER BY term in the window, and that argument op is OP_Ge, it generates +** code equivalent to: +** +** if( csr1.peerVal + regVal >= csr2.peerVal ) goto lbl; +** +** The value of parameter op may also be OP_Gt or OP_Le. In these cases the +** operator in the above pseudo-code is replaced with ">" or "<=", respectively. +** +** If the sort-order for the ORDER BY term in the window is DESC, then the +** comparison is reversed. Instead of adding regVal to csr1.peerVal, it is +** subtracted. And the comparison operator is inverted to - ">=" becomes "<=", +** ">" becomes "<", and so on. So, with DESC sort order, if the argument op +** is OP_Ge, the generated code is equivalent to: +** +** if( csr1.peerVal - regVal <= csr2.peerVal ) goto lbl; +** +** A special type of arithmetic is used such that if csr1.peerVal is not +** a numeric type (real or integer), then the result of the addition +** or subtraction is a a copy of csr1.peerVal. +*/ +static void windowCodeRangeTest( + WindowCodeArg *p, + int op, /* OP_Ge, OP_Gt, or OP_Le */ + int csr1, /* Cursor number for cursor 1 */ + int regVal, /* Register containing non-negative number */ + int csr2, /* Cursor number for cursor 2 */ + int lbl /* Jump destination if condition is true */ +){ + Parse *pParse = p->pParse; + Vdbe *v = sqlite3GetVdbe(pParse); + ExprList *pOrderBy = p->pMWin->pOrderBy; /* ORDER BY clause for window */ + int reg1 = sqlite3GetTempReg(pParse); /* Reg. for csr1.peerVal+regVal */ + int reg2 = sqlite3GetTempReg(pParse); /* Reg. for csr2.peerVal */ + int regString = ++pParse->nMem; /* Reg. for constant value '' */ + int arith = OP_Add; /* OP_Add or OP_Subtract */ + int addrGe; /* Jump destination */ + int addrDone = sqlite3VdbeMakeLabel(pParse); /* Address past OP_Ge */ + CollSeq *pColl; + + /* Read the peer-value from each cursor into a register */ + windowReadPeerValues(p, csr1, reg1); + windowReadPeerValues(p, csr2, reg2); + + assert( op==OP_Ge || op==OP_Gt || op==OP_Le ); + assert( pOrderBy && pOrderBy->nExpr==1 ); + if( pOrderBy->a[0].fg.sortFlags & KEYINFO_ORDER_DESC ){ + switch( op ){ + case OP_Ge: op = OP_Le; break; + case OP_Gt: op = OP_Lt; break; + default: assert( op==OP_Le ); op = OP_Ge; break; + } + arith = OP_Subtract; + } + + VdbeModuleComment((v, "CodeRangeTest: if( R%d %s R%d %s R%d ) goto lbl", + reg1, (arith==OP_Add ? "+" : "-"), regVal, + ((op==OP_Ge) ? ">=" : (op==OP_Le) ? "<=" : (op==OP_Gt) ? ">" : "<"), reg2 + )); + + /* If the BIGNULL flag is set for the ORDER BY, then it is required to + ** consider NULL values to be larger than all other values, instead of + ** the usual smaller. The VDBE opcodes OP_Ge and so on do not handle this + ** (and adding that capability causes a performance regression), so + ** instead if the BIGNULL flag is set then cases where either reg1 or + ** reg2 are NULL are handled separately in the following block. The code + ** generated is equivalent to: + ** + ** if( reg1 IS NULL ){ + ** if( op==OP_Ge ) goto lbl; + ** if( op==OP_Gt && reg2 IS NOT NULL ) goto lbl; + ** if( op==OP_Le && reg2 IS NULL ) goto lbl; + ** }else if( reg2 IS NULL ){ + ** if( op==OP_Le ) goto lbl; + ** } + ** + ** Additionally, if either reg1 or reg2 are NULL but the jump to lbl is + ** not taken, control jumps over the comparison operator coded below this + ** block. */ + if( pOrderBy->a[0].fg.sortFlags & KEYINFO_ORDER_BIGNULL ){ + /* This block runs if reg1 contains a NULL. */ + int addr = sqlite3VdbeAddOp1(v, OP_NotNull, reg1); VdbeCoverage(v); + switch( op ){ + case OP_Ge: + sqlite3VdbeAddOp2(v, OP_Goto, 0, lbl); + break; + case OP_Gt: + sqlite3VdbeAddOp2(v, OP_NotNull, reg2, lbl); + VdbeCoverage(v); + break; + case OP_Le: + sqlite3VdbeAddOp2(v, OP_IsNull, reg2, lbl); + VdbeCoverage(v); + break; + default: assert( op==OP_Lt ); /* no-op */ break; + } + sqlite3VdbeAddOp2(v, OP_Goto, 0, addrDone); + + /* This block runs if reg1 is not NULL, but reg2 is. */ + sqlite3VdbeJumpHere(v, addr); + sqlite3VdbeAddOp2(v, OP_IsNull, reg2, + (op==OP_Gt || op==OP_Ge) ? addrDone : lbl); + VdbeCoverage(v); + } + + /* Register reg1 currently contains csr1.peerVal (the peer-value from csr1). + ** This block adds (or subtracts for DESC) the numeric value in regVal + ** from it. Or, if reg1 is not numeric (it is a NULL, a text value or a blob), + ** then leave reg1 as it is. In pseudo-code, this is implemented as: + ** + ** if( reg1>='' ) goto addrGe; + ** reg1 = reg1 +/- regVal + ** addrGe: + ** + ** Since all strings and blobs are greater-than-or-equal-to an empty string, + ** the add/subtract is skipped for these, as required. If reg1 is a NULL, + ** then the arithmetic is performed, but since adding or subtracting from + ** NULL is always NULL anyway, this case is handled as required too. */ + sqlite3VdbeAddOp4(v, OP_String8, 0, regString, 0, "", P4_STATIC); + addrGe = sqlite3VdbeAddOp3(v, OP_Ge, regString, 0, reg1); + VdbeCoverage(v); + if( (op==OP_Ge && arith==OP_Add) || (op==OP_Le && arith==OP_Subtract) ){ + sqlite3VdbeAddOp3(v, op, reg2, lbl, reg1); VdbeCoverage(v); + } + sqlite3VdbeAddOp3(v, arith, regVal, reg1, reg1); + sqlite3VdbeJumpHere(v, addrGe); + + /* Compare registers reg2 and reg1, taking the jump if required. Note that + ** control skips over this test if the BIGNULL flag is set and either + ** reg1 or reg2 contain a NULL value. */ + sqlite3VdbeAddOp3(v, op, reg2, lbl, reg1); VdbeCoverage(v); + pColl = sqlite3ExprNNCollSeq(pParse, pOrderBy->a[0].pExpr); + sqlite3VdbeAppendP4(v, (void*)pColl, P4_COLLSEQ); + sqlite3VdbeChangeP5(v, SQLITE_NULLEQ); + sqlite3VdbeResolveLabel(v, addrDone); + + assert( op==OP_Ge || op==OP_Gt || op==OP_Lt || op==OP_Le ); + testcase(op==OP_Ge); VdbeCoverageIf(v, op==OP_Ge); + testcase(op==OP_Lt); VdbeCoverageIf(v, op==OP_Lt); + testcase(op==OP_Le); VdbeCoverageIf(v, op==OP_Le); + testcase(op==OP_Gt); VdbeCoverageIf(v, op==OP_Gt); + sqlite3ReleaseTempReg(pParse, reg1); + sqlite3ReleaseTempReg(pParse, reg2); + + VdbeModuleComment((v, "CodeRangeTest: end")); +} + +/* +** Helper function for sqlite3WindowCodeStep(). Each call to this function +** generates VM code for a single RETURN_ROW, AGGSTEP or AGGINVERSE +** operation. Refer to the header comment for sqlite3WindowCodeStep() for +** details. +*/ +static int windowCodeOp( + WindowCodeArg *p, /* Context object */ + int op, /* WINDOW_RETURN_ROW, AGGSTEP or AGGINVERSE */ + int regCountdown, /* Register for OP_IfPos countdown */ + int jumpOnEof /* Jump here if stepped cursor reaches EOF */ +){ + int csr, reg; + Parse *pParse = p->pParse; + Window *pMWin = p->pMWin; + int ret = 0; + Vdbe *v = p->pVdbe; + int addrContinue = 0; + int bPeer = (pMWin->eFrmType!=TK_ROWS); + + int lblDone = sqlite3VdbeMakeLabel(pParse); + int addrNextRange = 0; + + /* Special case - WINDOW_AGGINVERSE is always a no-op if the frame + ** starts with UNBOUNDED PRECEDING. */ + if( op==WINDOW_AGGINVERSE && pMWin->eStart==TK_UNBOUNDED ){ + assert( regCountdown==0 && jumpOnEof==0 ); + return 0; + } + + if( regCountdown>0 ){ + if( pMWin->eFrmType==TK_RANGE ){ + addrNextRange = sqlite3VdbeCurrentAddr(v); + assert( op==WINDOW_AGGINVERSE || op==WINDOW_AGGSTEP ); + if( op==WINDOW_AGGINVERSE ){ + if( pMWin->eStart==TK_FOLLOWING ){ + windowCodeRangeTest( + p, OP_Le, p->current.csr, regCountdown, p->start.csr, lblDone + ); + }else{ + windowCodeRangeTest( + p, OP_Ge, p->start.csr, regCountdown, p->current.csr, lblDone + ); + } + }else{ + windowCodeRangeTest( + p, OP_Gt, p->end.csr, regCountdown, p->current.csr, lblDone + ); + } + }else{ + sqlite3VdbeAddOp3(v, OP_IfPos, regCountdown, lblDone, 1); + VdbeCoverage(v); + } + } + + if( op==WINDOW_RETURN_ROW && pMWin->regStartRowid==0 ){ + windowAggFinal(p, 0); + } + addrContinue = sqlite3VdbeCurrentAddr(v); + + /* If this is a (RANGE BETWEEN a FOLLOWING AND b FOLLOWING) or + ** (RANGE BETWEEN b PRECEDING AND a PRECEDING) frame, ensure the + ** start cursor does not advance past the end cursor within the + ** temporary table. It otherwise might, if (a>b). Also ensure that, + ** if the input cursor is still finding new rows, that the end + ** cursor does not go past it to EOF. */ + if( pMWin->eStart==pMWin->eEnd && regCountdown + && pMWin->eFrmType==TK_RANGE + ){ + int regRowid1 = sqlite3GetTempReg(pParse); + int regRowid2 = sqlite3GetTempReg(pParse); + if( op==WINDOW_AGGINVERSE ){ + sqlite3VdbeAddOp2(v, OP_Rowid, p->start.csr, regRowid1); + sqlite3VdbeAddOp2(v, OP_Rowid, p->end.csr, regRowid2); + sqlite3VdbeAddOp3(v, OP_Ge, regRowid2, lblDone, regRowid1); + VdbeCoverage(v); + }else if( p->regRowid ){ + sqlite3VdbeAddOp2(v, OP_Rowid, p->end.csr, regRowid1); + sqlite3VdbeAddOp3(v, OP_Ge, p->regRowid, lblDone, regRowid1); + VdbeCoverageNeverNull(v); + } + sqlite3ReleaseTempReg(pParse, regRowid1); + sqlite3ReleaseTempReg(pParse, regRowid2); + assert( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_FOLLOWING ); + } + + switch( op ){ + case WINDOW_RETURN_ROW: + csr = p->current.csr; + reg = p->current.reg; + windowReturnOneRow(p); + break; + + case WINDOW_AGGINVERSE: + csr = p->start.csr; + reg = p->start.reg; + if( pMWin->regStartRowid ){ + assert( pMWin->regEndRowid ); + sqlite3VdbeAddOp2(v, OP_AddImm, pMWin->regStartRowid, 1); + }else{ + windowAggStep(p, pMWin, csr, 1, p->regArg); + } + break; + + default: + assert( op==WINDOW_AGGSTEP ); + csr = p->end.csr; + reg = p->end.reg; + if( pMWin->regStartRowid ){ + assert( pMWin->regEndRowid ); + sqlite3VdbeAddOp2(v, OP_AddImm, pMWin->regEndRowid, 1); + }else{ + windowAggStep(p, pMWin, csr, 0, p->regArg); + } + break; + } + + if( op==p->eDelete ){ + sqlite3VdbeAddOp1(v, OP_Delete, csr); + sqlite3VdbeChangeP5(v, OPFLAG_SAVEPOSITION); + } + + if( jumpOnEof ){ + sqlite3VdbeAddOp2(v, OP_Next, csr, sqlite3VdbeCurrentAddr(v)+2); + VdbeCoverage(v); + ret = sqlite3VdbeAddOp0(v, OP_Goto); + }else{ + sqlite3VdbeAddOp2(v, OP_Next, csr, sqlite3VdbeCurrentAddr(v)+1+bPeer); + VdbeCoverage(v); + if( bPeer ){ + sqlite3VdbeAddOp2(v, OP_Goto, 0, lblDone); + } + } + + if( bPeer ){ + int nReg = (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0); + int regTmp = (nReg ? sqlite3GetTempRange(pParse, nReg) : 0); + windowReadPeerValues(p, csr, regTmp); + windowIfNewPeer(pParse, pMWin->pOrderBy, regTmp, reg, addrContinue); + sqlite3ReleaseTempRange(pParse, regTmp, nReg); + } + + if( addrNextRange ){ + sqlite3VdbeAddOp2(v, OP_Goto, 0, addrNextRange); + } + sqlite3VdbeResolveLabel(v, lblDone); + return ret; +} + + +/* +** Allocate and return a duplicate of the Window object indicated by the +** third argument. Set the Window.pOwner field of the new object to +** pOwner. +*/ +Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p){ + Window *pNew = 0; + if( ALWAYS(p) ){ + pNew = sqlite3DbMallocZero(db, sizeof(Window)); + if( pNew ){ + pNew->zName = sqlite3DbStrDup(db, p->zName); + pNew->zBase = sqlite3DbStrDup(db, p->zBase); + pNew->pFilter = sqlite3ExprDup(db, p->pFilter, 0); + pNew->pWFunc = p->pWFunc; + pNew->pPartition = sqlite3ExprListDup(db, p->pPartition, 0); + pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, 0); + pNew->eFrmType = p->eFrmType; + pNew->eEnd = p->eEnd; + pNew->eStart = p->eStart; + pNew->eExclude = p->eExclude; + pNew->regResult = p->regResult; + pNew->regAccum = p->regAccum; + pNew->iArgCol = p->iArgCol; + pNew->iEphCsr = p->iEphCsr; + pNew->bExprArgs = p->bExprArgs; + pNew->pStart = sqlite3ExprDup(db, p->pStart, 0); + pNew->pEnd = sqlite3ExprDup(db, p->pEnd, 0); + pNew->pOwner = pOwner; + pNew->bImplicitFrame = p->bImplicitFrame; + } + } + return pNew; +} + +/* +** Return a copy of the linked list of Window objects passed as the +** second argument. +*/ +Window *sqlite3WindowListDup(sqlite3 *db, Window *p){ + Window *pWin; + Window *pRet = 0; + Window **pp = &pRet; + + for(pWin=p; pWin; pWin=pWin->pNextWin){ + *pp = sqlite3WindowDup(db, 0, pWin); + if( *pp==0 ) break; + pp = &((*pp)->pNextWin); + } + + return pRet; +} + +/* +** Return true if it can be determined at compile time that expression +** pExpr evaluates to a value that, when cast to an integer, is greater +** than zero. False otherwise. +** +** If an OOM error occurs, this function sets the Parse.db.mallocFailed +** flag and returns zero. +*/ +static int windowExprGtZero(Parse *pParse, Expr *pExpr){ + int ret = 0; + sqlite3 *db = pParse->db; + sqlite3_value *pVal = 0; + sqlite3ValueFromExpr(db, pExpr, db->enc, SQLITE_AFF_NUMERIC, &pVal); + if( pVal && sqlite3_value_int(pVal)>0 ){ + ret = 1; + } + sqlite3ValueFree(pVal); + return ret; +} + +/* +** sqlite3WhereBegin() has already been called for the SELECT statement +** passed as the second argument when this function is invoked. It generates +** code to populate the Window.regResult register for each window function +** and invoke the sub-routine at instruction addrGosub once for each row. +** sqlite3WhereEnd() is always called before returning. +** +** This function handles several different types of window frames, which +** require slightly different processing. The following pseudo code is +** used to implement window frames of the form: +** +** ROWS BETWEEN <expr1> PRECEDING AND <expr2> FOLLOWING +** +** Other window frame types use variants of the following: +** +** ... loop started by sqlite3WhereBegin() ... +** if( new partition ){ +** Gosub flush +** } +** Insert new row into eph table. +** +** if( first row of partition ){ +** // Rewind three cursors, all open on the eph table. +** Rewind(csrEnd); +** Rewind(csrStart); +** Rewind(csrCurrent); +** +** regEnd = <expr2> // FOLLOWING expression +** regStart = <expr1> // PRECEDING expression +** }else{ +** // First time this branch is taken, the eph table contains two +** // rows. The first row in the partition, which all three cursors +** // currently point to, and the following row. +** AGGSTEP +** if( (regEnd--)<=0 ){ +** RETURN_ROW +** if( (regStart--)<=0 ){ +** AGGINVERSE +** } +** } +** } +** } +** flush: +** AGGSTEP +** while( 1 ){ +** RETURN ROW +** if( csrCurrent is EOF ) break; +** if( (regStart--)<=0 ){ +** AggInverse(csrStart) +** Next(csrStart) +** } +** } +** +** The pseudo-code above uses the following shorthand: +** +** AGGSTEP: invoke the aggregate xStep() function for each window function +** with arguments read from the current row of cursor csrEnd, then +** step cursor csrEnd forward one row (i.e. sqlite3BtreeNext()). +** +** RETURN_ROW: return a row to the caller based on the contents of the +** current row of csrCurrent and the current state of all +** aggregates. Then step cursor csrCurrent forward one row. +** +** AGGINVERSE: invoke the aggregate xInverse() function for each window +** functions with arguments read from the current row of cursor +** csrStart. Then step csrStart forward one row. +** +** There are two other ROWS window frames that are handled significantly +** differently from the above - "BETWEEN <expr> PRECEDING AND <expr> PRECEDING" +** and "BETWEEN <expr> FOLLOWING AND <expr> FOLLOWING". These are special +** cases because they change the order in which the three cursors (csrStart, +** csrCurrent and csrEnd) iterate through the ephemeral table. Cases that +** use UNBOUNDED or CURRENT ROW are much simpler variations on one of these +** three. +** +** ROWS BETWEEN <expr1> PRECEDING AND <expr2> PRECEDING +** +** ... loop started by sqlite3WhereBegin() ... +** if( new partition ){ +** Gosub flush +** } +** Insert new row into eph table. +** if( first row of partition ){ +** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent) +** regEnd = <expr2> +** regStart = <expr1> +** }else{ +** if( (regEnd--)<=0 ){ +** AGGSTEP +** } +** RETURN_ROW +** if( (regStart--)<=0 ){ +** AGGINVERSE +** } +** } +** } +** flush: +** if( (regEnd--)<=0 ){ +** AGGSTEP +** } +** RETURN_ROW +** +** +** ROWS BETWEEN <expr1> FOLLOWING AND <expr2> FOLLOWING +** +** ... loop started by sqlite3WhereBegin() ... +** if( new partition ){ +** Gosub flush +** } +** Insert new row into eph table. +** if( first row of partition ){ +** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent) +** regEnd = <expr2> +** regStart = regEnd - <expr1> +** }else{ +** AGGSTEP +** if( (regEnd--)<=0 ){ +** RETURN_ROW +** } +** if( (regStart--)<=0 ){ +** AGGINVERSE +** } +** } +** } +** flush: +** AGGSTEP +** while( 1 ){ +** if( (regEnd--)<=0 ){ +** RETURN_ROW +** if( eof ) break; +** } +** if( (regStart--)<=0 ){ +** AGGINVERSE +** if( eof ) break +** } +** } +** while( !eof csrCurrent ){ +** RETURN_ROW +** } +** +** For the most part, the patterns above are adapted to support UNBOUNDED by +** assuming that it is equivalent to "infinity PRECEDING/FOLLOWING" and +** CURRENT ROW by assuming that it is equivalent to "0 PRECEDING/FOLLOWING". +** This is optimized of course - branches that will never be taken and +** conditions that are always true are omitted from the VM code. The only +** exceptional case is: +** +** ROWS BETWEEN <expr1> FOLLOWING AND UNBOUNDED FOLLOWING +** +** ... loop started by sqlite3WhereBegin() ... +** if( new partition ){ +** Gosub flush +** } +** Insert new row into eph table. +** if( first row of partition ){ +** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent) +** regStart = <expr1> +** }else{ +** AGGSTEP +** } +** } +** flush: +** AGGSTEP +** while( 1 ){ +** if( (regStart--)<=0 ){ +** AGGINVERSE +** if( eof ) break +** } +** RETURN_ROW +** } +** while( !eof csrCurrent ){ +** RETURN_ROW +** } +** +** Also requiring special handling are the cases: +** +** ROWS BETWEEN <expr1> PRECEDING AND <expr2> PRECEDING +** ROWS BETWEEN <expr1> FOLLOWING AND <expr2> FOLLOWING +** +** when (expr1 < expr2). This is detected at runtime, not by this function. +** To handle this case, the pseudo-code programs depicted above are modified +** slightly to be: +** +** ... loop started by sqlite3WhereBegin() ... +** if( new partition ){ +** Gosub flush +** } +** Insert new row into eph table. +** if( first row of partition ){ +** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent) +** regEnd = <expr2> +** regStart = <expr1> +** if( regEnd < regStart ){ +** RETURN_ROW +** delete eph table contents +** continue +** } +** ... +** +** The new "continue" statement in the above jumps to the next iteration +** of the outer loop - the one started by sqlite3WhereBegin(). +** +** The various GROUPS cases are implemented using the same patterns as +** ROWS. The VM code is modified slightly so that: +** +** 1. The else branch in the main loop is only taken if the row just +** added to the ephemeral table is the start of a new group. In +** other words, it becomes: +** +** ... loop started by sqlite3WhereBegin() ... +** if( new partition ){ +** Gosub flush +** } +** Insert new row into eph table. +** if( first row of partition ){ +** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent) +** regEnd = <expr2> +** regStart = <expr1> +** }else if( new group ){ +** ... +** } +** } +** +** 2. Instead of processing a single row, each RETURN_ROW, AGGSTEP or +** AGGINVERSE step processes the current row of the relevant cursor and +** all subsequent rows belonging to the same group. +** +** RANGE window frames are a little different again. As for GROUPS, the +** main loop runs once per group only. And RETURN_ROW, AGGSTEP and AGGINVERSE +** deal in groups instead of rows. As for ROWS and GROUPS, there are three +** basic cases: +** +** RANGE BETWEEN <expr1> PRECEDING AND <expr2> FOLLOWING +** +** ... loop started by sqlite3WhereBegin() ... +** if( new partition ){ +** Gosub flush +** } +** Insert new row into eph table. +** if( first row of partition ){ +** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent) +** regEnd = <expr2> +** regStart = <expr1> +** }else{ +** AGGSTEP +** while( (csrCurrent.key + regEnd) < csrEnd.key ){ +** RETURN_ROW +** while( csrStart.key + regStart) < csrCurrent.key ){ +** AGGINVERSE +** } +** } +** } +** } +** flush: +** AGGSTEP +** while( 1 ){ +** RETURN ROW +** if( csrCurrent is EOF ) break; +** while( csrStart.key + regStart) < csrCurrent.key ){ +** AGGINVERSE +** } +** } +** } +** +** In the above notation, "csr.key" means the current value of the ORDER BY +** expression (there is only ever 1 for a RANGE that uses an <expr> FOLLOWING +** or <expr PRECEDING) read from cursor csr. +** +** RANGE BETWEEN <expr1> PRECEDING AND <expr2> PRECEDING +** +** ... loop started by sqlite3WhereBegin() ... +** if( new partition ){ +** Gosub flush +** } +** Insert new row into eph table. +** if( first row of partition ){ +** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent) +** regEnd = <expr2> +** regStart = <expr1> +** }else{ +** while( (csrEnd.key + regEnd) <= csrCurrent.key ){ +** AGGSTEP +** } +** while( (csrStart.key + regStart) < csrCurrent.key ){ +** AGGINVERSE +** } +** RETURN_ROW +** } +** } +** flush: +** while( (csrEnd.key + regEnd) <= csrCurrent.key ){ +** AGGSTEP +** } +** while( (csrStart.key + regStart) < csrCurrent.key ){ +** AGGINVERSE +** } +** RETURN_ROW +** +** RANGE BETWEEN <expr1> FOLLOWING AND <expr2> FOLLOWING +** +** ... loop started by sqlite3WhereBegin() ... +** if( new partition ){ +** Gosub flush +** } +** Insert new row into eph table. +** if( first row of partition ){ +** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent) +** regEnd = <expr2> +** regStart = <expr1> +** }else{ +** AGGSTEP +** while( (csrCurrent.key + regEnd) < csrEnd.key ){ +** while( (csrCurrent.key + regStart) > csrStart.key ){ +** AGGINVERSE +** } +** RETURN_ROW +** } +** } +** } +** flush: +** AGGSTEP +** while( 1 ){ +** while( (csrCurrent.key + regStart) > csrStart.key ){ +** AGGINVERSE +** if( eof ) break "while( 1 )" loop. +** } +** RETURN_ROW +** } +** while( !eof csrCurrent ){ +** RETURN_ROW +** } +** +** The text above leaves out many details. Refer to the code and comments +** below for a more complete picture. +*/ +void sqlite3WindowCodeStep( + Parse *pParse, /* Parse context */ + Select *p, /* Rewritten SELECT statement */ + WhereInfo *pWInfo, /* Context returned by sqlite3WhereBegin() */ + int regGosub, /* Register for OP_Gosub */ + int addrGosub /* OP_Gosub here to return each row */ +){ + Window *pMWin = p->pWin; + ExprList *pOrderBy = pMWin->pOrderBy; + Vdbe *v = sqlite3GetVdbe(pParse); + int csrWrite; /* Cursor used to write to eph. table */ + int csrInput = p->pSrc->a[0].iCursor; /* Cursor of sub-select */ + int nInput = p->pSrc->a[0].pSTab->nCol; /* Number of cols returned by sub */ + int iInput; /* To iterate through sub cols */ + int addrNe; /* Address of OP_Ne */ + int addrGosubFlush = 0; /* Address of OP_Gosub to flush: */ + int addrInteger = 0; /* Address of OP_Integer */ + int addrEmpty; /* Address of OP_Rewind in flush: */ + int regNew; /* Array of registers holding new input row */ + int regRecord; /* regNew array in record form */ + int regNewPeer = 0; /* Peer values for new row (part of regNew) */ + int regPeer = 0; /* Peer values for current row */ + int regFlushPart = 0; /* Register for "Gosub flush_partition" */ + WindowCodeArg s; /* Context object for sub-routines */ + int lblWhereEnd; /* Label just before sqlite3WhereEnd() code */ + int regStart = 0; /* Value of <expr> PRECEDING */ + int regEnd = 0; /* Value of <expr> FOLLOWING */ + + assert( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_CURRENT + || pMWin->eStart==TK_FOLLOWING || pMWin->eStart==TK_UNBOUNDED + ); + assert( pMWin->eEnd==TK_FOLLOWING || pMWin->eEnd==TK_CURRENT + || pMWin->eEnd==TK_UNBOUNDED || pMWin->eEnd==TK_PRECEDING + ); + assert( pMWin->eExclude==0 || pMWin->eExclude==TK_CURRENT + || pMWin->eExclude==TK_GROUP || pMWin->eExclude==TK_TIES + || pMWin->eExclude==TK_NO + ); + + lblWhereEnd = sqlite3VdbeMakeLabel(pParse); + + /* Fill in the context object */ + memset(&s, 0, sizeof(WindowCodeArg)); + s.pParse = pParse; + s.pMWin = pMWin; + s.pVdbe = v; + s.regGosub = regGosub; + s.addrGosub = addrGosub; + s.current.csr = pMWin->iEphCsr; + csrWrite = s.current.csr+1; + s.start.csr = s.current.csr+2; + s.end.csr = s.current.csr+3; + + /* Figure out when rows may be deleted from the ephemeral table. There + ** are four options - they may never be deleted (eDelete==0), they may + ** be deleted as soon as they are no longer part of the window frame + ** (eDelete==WINDOW_AGGINVERSE), they may be deleted as after the row + ** has been returned to the caller (WINDOW_RETURN_ROW), or they may + ** be deleted after they enter the frame (WINDOW_AGGSTEP). */ + switch( pMWin->eStart ){ + case TK_FOLLOWING: + if( pMWin->eFrmType!=TK_RANGE + && windowExprGtZero(pParse, pMWin->pStart) + ){ + s.eDelete = WINDOW_RETURN_ROW; + } + break; + case TK_UNBOUNDED: + if( windowCacheFrame(pMWin)==0 ){ + if( pMWin->eEnd==TK_PRECEDING ){ + if( pMWin->eFrmType!=TK_RANGE + && windowExprGtZero(pParse, pMWin->pEnd) + ){ + s.eDelete = WINDOW_AGGSTEP; + } + }else{ + s.eDelete = WINDOW_RETURN_ROW; + } + } + break; + default: + s.eDelete = WINDOW_AGGINVERSE; + break; + } + + /* Allocate registers for the array of values from the sub-query, the + ** same values in record form, and the rowid used to insert said record + ** into the ephemeral table. */ + regNew = pParse->nMem+1; + pParse->nMem += nInput; + regRecord = ++pParse->nMem; + s.regRowid = ++pParse->nMem; + + /* If the window frame contains an "<expr> PRECEDING" or "<expr> FOLLOWING" + ** clause, allocate registers to store the results of evaluating each + ** <expr>. */ + if( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_FOLLOWING ){ + regStart = ++pParse->nMem; + } + if( pMWin->eEnd==TK_PRECEDING || pMWin->eEnd==TK_FOLLOWING ){ + regEnd = ++pParse->nMem; + } + + /* If this is not a "ROWS BETWEEN ..." frame, then allocate arrays of + ** registers to store copies of the ORDER BY expressions (peer values) + ** for the main loop, and for each cursor (start, current and end). */ + if( pMWin->eFrmType!=TK_ROWS ){ + int nPeer = (pOrderBy ? pOrderBy->nExpr : 0); + regNewPeer = regNew + pMWin->nBufferCol; + if( pMWin->pPartition ) regNewPeer += pMWin->pPartition->nExpr; + regPeer = pParse->nMem+1; pParse->nMem += nPeer; + s.start.reg = pParse->nMem+1; pParse->nMem += nPeer; + s.current.reg = pParse->nMem+1; pParse->nMem += nPeer; + s.end.reg = pParse->nMem+1; pParse->nMem += nPeer; + } + + /* Load the column values for the row returned by the sub-select + ** into an array of registers starting at regNew. Assemble them into + ** a record in register regRecord. */ + for(iInput=0; iInput<nInput; iInput++){ + sqlite3VdbeAddOp3(v, OP_Column, csrInput, iInput, regNew+iInput); + } + sqlite3VdbeAddOp3(v, OP_MakeRecord, regNew, nInput, regRecord); + + /* An input row has just been read into an array of registers starting + ** at regNew. If the window has a PARTITION clause, this block generates + ** VM code to check if the input row is the start of a new partition. + ** If so, it does an OP_Gosub to an address to be filled in later. The + ** address of the OP_Gosub is stored in local variable addrGosubFlush. */ + if( pMWin->pPartition ){ + int addr; + ExprList *pPart = pMWin->pPartition; + int nPart = pPart->nExpr; + int regNewPart = regNew + pMWin->nBufferCol; + KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pPart, 0, 0); + + regFlushPart = ++pParse->nMem; + addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPart, pMWin->regPart, nPart); + sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO); + sqlite3VdbeAddOp3(v, OP_Jump, addr+2, addr+4, addr+2); + VdbeCoverageEqNe(v); + addrGosubFlush = sqlite3VdbeAddOp1(v, OP_Gosub, regFlushPart); + VdbeComment((v, "call flush_partition")); + sqlite3VdbeAddOp3(v, OP_Copy, regNewPart, pMWin->regPart, nPart-1); + } + + /* Insert the new row into the ephemeral table */ + sqlite3VdbeAddOp2(v, OP_NewRowid, csrWrite, s.regRowid); + sqlite3VdbeAddOp3(v, OP_Insert, csrWrite, regRecord, s.regRowid); + addrNe = sqlite3VdbeAddOp3(v, OP_Ne, pMWin->regOne, 0, s.regRowid); + VdbeCoverageNeverNull(v); + + /* This block is run for the first row of each partition */ + s.regArg = windowInitAccum(pParse, pMWin); + + if( regStart ){ + sqlite3ExprCode(pParse, pMWin->pStart, regStart); + windowCheckValue(pParse, regStart, 0 + (pMWin->eFrmType==TK_RANGE?3:0)); + } + if( regEnd ){ + sqlite3ExprCode(pParse, pMWin->pEnd, regEnd); + windowCheckValue(pParse, regEnd, 1 + (pMWin->eFrmType==TK_RANGE?3:0)); + } + + if( pMWin->eFrmType!=TK_RANGE && pMWin->eStart==pMWin->eEnd && regStart ){ + int op = ((pMWin->eStart==TK_FOLLOWING) ? OP_Ge : OP_Le); + int addrGe = sqlite3VdbeAddOp3(v, op, regStart, 0, regEnd); + VdbeCoverageNeverNullIf(v, op==OP_Ge); /* NeverNull because bound <expr> */ + VdbeCoverageNeverNullIf(v, op==OP_Le); /* values previously checked */ + windowAggFinal(&s, 0); + sqlite3VdbeAddOp1(v, OP_Rewind, s.current.csr); + windowReturnOneRow(&s); + sqlite3VdbeAddOp1(v, OP_ResetSorter, s.current.csr); + sqlite3VdbeAddOp2(v, OP_Goto, 0, lblWhereEnd); + sqlite3VdbeJumpHere(v, addrGe); + } + if( pMWin->eStart==TK_FOLLOWING && pMWin->eFrmType!=TK_RANGE && regEnd ){ + assert( pMWin->eEnd==TK_FOLLOWING ); + sqlite3VdbeAddOp3(v, OP_Subtract, regStart, regEnd, regStart); + } + + if( pMWin->eStart!=TK_UNBOUNDED ){ + sqlite3VdbeAddOp1(v, OP_Rewind, s.start.csr); + } + sqlite3VdbeAddOp1(v, OP_Rewind, s.current.csr); + sqlite3VdbeAddOp1(v, OP_Rewind, s.end.csr); + if( regPeer && pOrderBy ){ + sqlite3VdbeAddOp3(v, OP_Copy, regNewPeer, regPeer, pOrderBy->nExpr-1); + sqlite3VdbeAddOp3(v, OP_Copy, regPeer, s.start.reg, pOrderBy->nExpr-1); + sqlite3VdbeAddOp3(v, OP_Copy, regPeer, s.current.reg, pOrderBy->nExpr-1); + sqlite3VdbeAddOp3(v, OP_Copy, regPeer, s.end.reg, pOrderBy->nExpr-1); + } + + sqlite3VdbeAddOp2(v, OP_Goto, 0, lblWhereEnd); + + sqlite3VdbeJumpHere(v, addrNe); + + /* Beginning of the block executed for the second and subsequent rows. */ + if( regPeer ){ + windowIfNewPeer(pParse, pOrderBy, regNewPeer, regPeer, lblWhereEnd); + } + if( pMWin->eStart==TK_FOLLOWING ){ + windowCodeOp(&s, WINDOW_AGGSTEP, 0, 0); + if( pMWin->eEnd!=TK_UNBOUNDED ){ + if( pMWin->eFrmType==TK_RANGE ){ + int lbl = sqlite3VdbeMakeLabel(pParse); + int addrNext = sqlite3VdbeCurrentAddr(v); + windowCodeRangeTest(&s, OP_Ge, s.current.csr, regEnd, s.end.csr, lbl); + windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0); + windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 0); + sqlite3VdbeAddOp2(v, OP_Goto, 0, addrNext); + sqlite3VdbeResolveLabel(v, lbl); + }else{ + windowCodeOp(&s, WINDOW_RETURN_ROW, regEnd, 0); + windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0); + } + } + }else + if( pMWin->eEnd==TK_PRECEDING ){ + int bRPS = (pMWin->eStart==TK_PRECEDING && pMWin->eFrmType==TK_RANGE); + windowCodeOp(&s, WINDOW_AGGSTEP, regEnd, 0); + if( bRPS ) windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0); + windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 0); + if( !bRPS ) windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0); + }else{ + int addr = 0; + windowCodeOp(&s, WINDOW_AGGSTEP, 0, 0); + if( pMWin->eEnd!=TK_UNBOUNDED ){ + if( pMWin->eFrmType==TK_RANGE ){ + int lbl = 0; + addr = sqlite3VdbeCurrentAddr(v); + if( regEnd ){ + lbl = sqlite3VdbeMakeLabel(pParse); + windowCodeRangeTest(&s, OP_Ge, s.current.csr, regEnd, s.end.csr, lbl); + } + windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 0); + windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0); + if( regEnd ){ + sqlite3VdbeAddOp2(v, OP_Goto, 0, addr); + sqlite3VdbeResolveLabel(v, lbl); + } + }else{ + if( regEnd ){ + addr = sqlite3VdbeAddOp3(v, OP_IfPos, regEnd, 0, 1); + VdbeCoverage(v); + } + windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 0); + windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0); + if( regEnd ) sqlite3VdbeJumpHere(v, addr); + } + } + } + + /* End of the main input loop */ + sqlite3VdbeResolveLabel(v, lblWhereEnd); + sqlite3WhereEnd(pWInfo); + + /* Fall through */ + if( pMWin->pPartition ){ + addrInteger = sqlite3VdbeAddOp2(v, OP_Integer, 0, regFlushPart); + sqlite3VdbeJumpHere(v, addrGosubFlush); + } + + s.regRowid = 0; + addrEmpty = sqlite3VdbeAddOp1(v, OP_Rewind, csrWrite); + VdbeCoverage(v); + if( pMWin->eEnd==TK_PRECEDING ){ + int bRPS = (pMWin->eStart==TK_PRECEDING && pMWin->eFrmType==TK_RANGE); + windowCodeOp(&s, WINDOW_AGGSTEP, regEnd, 0); + if( bRPS ) windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0); + windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 0); + }else if( pMWin->eStart==TK_FOLLOWING ){ + int addrStart; + int addrBreak1; + int addrBreak2; + int addrBreak3; + windowCodeOp(&s, WINDOW_AGGSTEP, 0, 0); + if( pMWin->eFrmType==TK_RANGE ){ + addrStart = sqlite3VdbeCurrentAddr(v); + addrBreak2 = windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 1); + addrBreak1 = windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 1); + }else + if( pMWin->eEnd==TK_UNBOUNDED ){ + addrStart = sqlite3VdbeCurrentAddr(v); + addrBreak1 = windowCodeOp(&s, WINDOW_RETURN_ROW, regStart, 1); + addrBreak2 = windowCodeOp(&s, WINDOW_AGGINVERSE, 0, 1); + }else{ + assert( pMWin->eEnd==TK_FOLLOWING ); + /* assert( regStart>=0 ); + ** regEnd = regEnd - regStart; + ** regStart = 0; */ + sqlite3VdbeAddOp3(v, OP_Subtract, regStart, regEnd, regEnd); + sqlite3VdbeAddOp2(v, OP_Integer, 0, regStart); + + addrStart = sqlite3VdbeCurrentAddr(v); + addrBreak1 = windowCodeOp(&s, WINDOW_RETURN_ROW, regEnd, 1); + addrBreak2 = windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 1); + } + sqlite3VdbeAddOp2(v, OP_Goto, 0, addrStart); + sqlite3VdbeJumpHere(v, addrBreak2); + addrStart = sqlite3VdbeCurrentAddr(v); + addrBreak3 = windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 1); + sqlite3VdbeAddOp2(v, OP_Goto, 0, addrStart); + sqlite3VdbeJumpHere(v, addrBreak1); + sqlite3VdbeJumpHere(v, addrBreak3); + }else{ + int addrBreak; + int addrStart; + windowCodeOp(&s, WINDOW_AGGSTEP, 0, 0); + addrStart = sqlite3VdbeCurrentAddr(v); + addrBreak = windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 1); + windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0); + sqlite3VdbeAddOp2(v, OP_Goto, 0, addrStart); + sqlite3VdbeJumpHere(v, addrBreak); + } + sqlite3VdbeJumpHere(v, addrEmpty); + + sqlite3VdbeAddOp1(v, OP_ResetSorter, s.current.csr); + if( pMWin->pPartition ){ + if( pMWin->regStartRowid ){ + sqlite3VdbeAddOp2(v, OP_Integer, 1, pMWin->regStartRowid); + sqlite3VdbeAddOp2(v, OP_Integer, 0, pMWin->regEndRowid); + } + sqlite3VdbeChangeP1(v, addrInteger, sqlite3VdbeCurrentAddr(v)); + sqlite3VdbeAddOp1(v, OP_Return, regFlushPart); + } +} + +#endif /* SQLITE_OMIT_WINDOWFUNC */ diff --git a/test/affinity2.test b/test/affinity2.test index 9838bd660a..59f9dada53 100644 --- a/test/affinity2.test +++ b/test/affinity2.test @@ -14,6 +14,7 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl +set testprefix affinity2 do_execsql_test affinity2-100 { CREATE TABLE t1( @@ -58,4 +59,78 @@ do_execsql_test affinity2-300 { SELECT rowid, xt==+xi, xt==xi, xt==xb FROM t1 ORDER BY rowid; } {1 1 1 0 2 1 1 1 3 0 1 1} +#------------------------------------------------------------------------- +do_execsql_test 400 { + CREATE TABLE ttt(c0, c1); + CREATE INDEX ii ON ttt(CAST(c0 AS NUMERIC)); + INSERT INTO ttt VALUES('abc', '-1'); +} +do_execsql_test 410 { + SELECT * FROM ttt WHERE CAST(c0 AS NUMERIC) > c1 GROUP BY rowid; +} {abc -1} +do_execsql_test 420 { + SELECT * FROM ttt INDEXED BY ii WHERE CAST(c0 AS NUMERIC) > c1 GROUP BY rowid; +} {abc -1} + +do_execsql_test 430 { + CREATE TABLE t3(a, b, c INTEGER); + CREATE INDEX t3ac ON t3(a, c-1); + INSERT INTO t3 VALUES(1, 1, 1); + INSERT INTO t3 VALUES(2, 1, 0); + INSERT INTO t3 VALUES(3, 1, 1); + INSERT INTO t3 VALUES(4, 1, 0); + INSERT INTO t3 VALUES(5, 1, 1); +} +do_execsql_test 440 { + SELECT * FROM t3 WHERE c='0' ORDER BY a; +} {2 1 0 4 1 0} + +# 2019-08-22 ticket https://sqlite.org/src/info/d99f1ffe836c591ac57f +# False positive in sqlite3ExprNeedsNoAffinityChange() +# +do_execsql_test 500 { + DROP TABLE IF EXISTS t0; + CREATE TABLE t0(c0 TEXT UNIQUE, c1); + INSERT INTO t0(c0) VALUES (-1); + SELECT quote(- x'ce'), quote(t0.c0), quote(- x'ce' >= t0.c0) FROM t0; +} {0 '-1' 1} +do_execsql_test 501 { + SELECT * FROM t0 WHERE - x'ce' >= t0.c0; +} {-1 {}} +do_execsql_test 502 { + SELECT quote(+-+x'ce'), quote(t0.c0), quote(+-+x'ce' >= t0.c0) FROM t0; +} {0 '-1' 1} +do_execsql_test 503 { + SELECT * FROM t0 WHERE +-+x'ce' >= t0.c0; +} {-1 {}} +do_execsql_test 504 { + SELECT quote(- 'ce'), quote(t0.c0), quote(- 'ce' >= t0.c0) FROM t0; +} {0 '-1' 1} +do_execsql_test 505 { + SELECT * FROM t0 WHERE - 'ce' >= t0.c0; +} {-1 {}} +do_execsql_test 506 { + SELECT quote(+-+'ce'), quote(t0.c0), quote(+-+'ce' >= t0.c0) FROM t0; +} {0 '-1' 1} +do_execsql_test 507 { + SELECT * FROM t0 WHERE +-+'ce' >= t0.c0; +} {-1 {}} + +# 2019-08-30 ticket https://sqlite.org/src/info/40812aea1fde9594 +# +# Due to some differences in floating point computations, these tests do not +# work under valgrind. +# +if {![info exists ::G(valgrind)]} { + do_execsql_test 600 { + DROP TABLE IF EXISTS t0; + CREATE TABLE t0(c0 REAL UNIQUE); + INSERT INTO t0(c0) VALUES (3175546974276630385); + SELECT 3175546974276630385 < c0 FROM t0; + } {1} + do_execsql_test 601 { + SELECT 1 FROM t0 WHERE 3175546974276630385 < c0; + } {1} +} + finish_test diff --git a/test/affinity3.test b/test/affinity3.test new file mode 100644 index 0000000000..be415de46c --- /dev/null +++ b/test/affinity3.test @@ -0,0 +1,123 @@ +# 2017-01-16 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# Test cases for bugs: +# +# https://sqlite.org/src/info/91e2e8ba6ff2e2 +# https://sqlite.org/src/info/7ffd1ca1d2ad4ecf +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +# Ticket https://sqlite.org/src/info/91e2e8ba6ff2e2 (2011-09-19) +# Automatic index causes undesired type conversions +# +do_execsql_test affinity3-100 { + CREATE TABLE customer (id INT PRIMARY KEY); + CREATE TABLE apr (id INT PRIMARY KEY, apr REAL); + + CREATE VIEW v1 AS + SELECT c.id, i.apr + FROM customer c + LEFT JOIN apr i ON i.id=c.id; + + CREATE VIEW v1rj AS + SELECT c.id, i.apr + FROM apr i + RIGHT JOIN customer c ON i.id=c.id; + + CREATE VIEW v2 AS + SELECT c.id, v1.apr + FROM customer c + LEFT JOIN v1 ON v1.id=c.id; + + CREATE VIEW v2rj AS + SELECT c.id, v1.apr + FROM v1 RIGHT JOIN customer c ON v1.id=c.id; + + CREATE VIEW v2rjrj AS + SELECT c.id, v1rj.apr + FROM v1rj RIGHT JOIN customer c ON v1rj.id=c.id; + + INSERT INTO customer (id) VALUES (1); + INSERT INTO apr (id, apr) VALUES (1, 12); + INSERT INTO customer (id) VALUES (2); + INSERT INTO apr (id, apr) VALUES (2, 12.01); +} +do_execsql_test affinity3-110 { + PRAGMA automatic_index=ON; + SELECT id, (apr / 100), typeof(apr) apr_type FROM v1; +} {1 0.12 real 2 0.1201 real} +do_execsql_test affinity3-111 { + PRAGMA automatic_index=ON; + SELECT id, (apr / 100), typeof(apr) apr_type FROM v1rj; +} {1 0.12 real 2 0.1201 real} +do_execsql_test affinity3-120 { + SELECT id, (apr / 100), typeof(apr) apr_type FROM v2; +} {1 0.12 real 2 0.1201 real} +do_execsql_test affinity3-121 { + SELECT id, (apr / 100), typeof(apr) apr_type FROM v2rj; +} {1 0.12 real 2 0.1201 real} +do_execsql_test affinity3-122 { + SELECT id, (apr / 100), typeof(apr) apr_type FROM v2rjrj; +} {1 0.12 real 2 0.1201 real} +do_execsql_test affinity3-130 { + PRAGMA automatic_index=OFF; + SELECT id, (apr / 100), typeof(apr) apr_type FROM v1; +} {1 0.12 real 2 0.1201 real} +do_execsql_test affinity3-131 { + SELECT id, (apr / 100), typeof(apr) apr_type FROM v1rj; +} {1 0.12 real 2 0.1201 real} +do_execsql_test affinity3-140 { + SELECT id, (apr / 100), typeof(apr) apr_type FROM v2; +} {1 0.12 real 2 0.1201 real} +do_execsql_test affinity3-141 { + SELECT id, (apr / 100), typeof(apr) apr_type FROM v2rj; +} {1 0.12 real 2 0.1201 real} +do_execsql_test affinity3-142 { + SELECT id, (apr / 100), typeof(apr) apr_type FROM v2rjrj; +} {1 0.12 real 2 0.1201 real} + +# Ticket https://sqlite.org/src/info/7ffd1ca1d2ad4ecf (2017-01-16) +# Incorrect affinity when using automatic indexes +# +do_execsql_test affinity3-200 { + CREATE TABLE map_integer (id INT, name); + INSERT INTO map_integer VALUES(1,'a'); + CREATE TABLE map_text (id TEXT, name); + INSERT INTO map_text VALUES('4','e'); + CREATE TABLE data (id TEXT, name); + INSERT INTO data VALUES(1,'abc'); + INSERT INTO data VALUES('4','xyz'); + CREATE VIEW idmap as + SELECT * FROM map_integer + UNION SELECT * FROM map_text; + CREATE TABLE mzed AS SELECT * FROM idmap; +} + +do_execsql_test affinity3-210 { + PRAGMA automatic_index=ON; + SELECT * FROM data JOIN idmap USING(id); +} {4 xyz e} +do_execsql_test affinity3-220 { + SELECT * FROM data JOIN mzed USING(id); +} {4 xyz e} + +do_execsql_test affinity3-250 { + PRAGMA automatic_index=OFF; + SELECT * FROM data JOIN idmap USING(id); +} {4 xyz e} +do_execsql_test affinity3-260 { + SELECT * FROM data JOIN mzed USING(id); +} {4 xyz e} + +finish_test diff --git a/test/aggfault.test b/test/aggfault.test new file mode 100644 index 0000000000..7c16b039ce --- /dev/null +++ b/test/aggfault.test @@ -0,0 +1,43 @@ +# 2023 March 30 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix aggfault + + +do_execsql_test 1 { + CREATE TABLE t1(x); + CREATE INDEX t1x ON t1(x, x=0); +} +faultsim_save_and_close + +do_faultsim_test 2 -faults oom* -prep { + faultsim_restore_and_reopen + execsql { SELECT * FROM sqlite_schema } +} -body { + execsql { + SELECT * FROM t1 AS a1 WHERE ( + SELECT count(x AND 0=a1.x) FROM t1 GROUP BY abs(1) + ) AND x=( + SELECT * FROM t1 AS a1 + WHERE (SELECT count(x IS 1 AND a1.x=0) + FROM t1 + GROUP BY abs(1)) AND x=0 + ); + } +} -test { + faultsim_test_result {0 {}} +} + + +finish_test diff --git a/test/aggnested.test b/test/aggnested.test index a87c751eda..f3539076bd 100644 --- a/test/aggnested.test +++ b/test/aggnested.test @@ -1,4 +1,4 @@ -# 2012 August 23 +# 2012-08-23 # # The author disclaims copyright to this source code. In place of # a legal notice, here is a blessing: @@ -17,6 +17,7 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl +set testprefix aggnested do_test aggnested-1.1 { db eval { @@ -24,19 +25,19 @@ do_test aggnested-1.1 { INSERT INTO t1 VALUES(1), (2), (3); CREATE TABLE t2(b1 INTEGER); INSERT INTO t2 VALUES(4), (5); - SELECT (SELECT group_concat(a1,'x') FROM t2) FROM t1; + SELECT (SELECT string_agg(a1,'x') FROM t2) FROM t1; } } {1x2x3} do_test aggnested-1.2 { db eval { SELECT - (SELECT group_concat(a1,'x') || '-' || group_concat(b1,'y') FROM t2) + (SELECT string_agg(a1,'x') || '-' || string_agg(b1,'y') FROM t2) FROM t1; } } {1x2x3-4y5} do_test aggnested-1.3 { db eval { - SELECT (SELECT group_concat(b1,a1) FROM t2) FROM t1; + SELECT (SELECT string_agg(b1,a1) FROM t2) FROM t1; } } {415 425 435} do_test aggnested-1.4 { @@ -65,7 +66,7 @@ do_test aggnested-2.0 { t1.* FROM t1; } -} {A,B,B 3 33 333 3333} +} {A,B,B 1 11 111 1111} db2 close ##################### Test cases for ticket [bfbf38e5e9956ac69f] ############ @@ -136,6 +137,17 @@ do_test aggnested-3.1 { GROUP BY curr.id1); } } {1 1} +do_test aggnested-3.1-rj { + db eval { + SELECT + (SELECT sum(value2==xyz) FROM t2) + FROM + (SELECT curr.value1 as xyz + FROM t1 AS other RIGHT JOIN t1 AS curr + GROUP BY curr.id1); + } +} {1 1} + do_test aggnested-3.2 { db eval { DROP TABLE IF EXISTS t1; @@ -232,6 +244,244 @@ do_test aggnested-3.16 { GROUP BY id1; } } {12 2 34 4} - + +# 2019-08-31 +# Problem found by dbsqlfuzz +# +do_execsql_test aggnested-4.1 { + DROP TABLE IF EXISTS aa; + DROP TABLE IF EXISTS bb; + CREATE TABLE aa(x INT); INSERT INTO aa(x) VALUES(123); + CREATE TABLE bb(y INT); INSERT INTO bb(y) VALUES(456); + SELECT (SELECT sum(x+(SELECT y)) FROM bb) FROM aa; +} {579} +do_execsql_test aggnested-4.2 { + SELECT (SELECT sum(x+y) FROM bb) FROM aa; +} {579} +do_execsql_test aggnested-4.3 { + DROP TABLE IF EXISTS tx; + DROP TABLE IF EXISTS ty; + CREATE TABLE tx(x INT); + INSERT INTO tx VALUES(1),(2),(3),(4),(5); + CREATE TABLE ty(y INT); + INSERT INTO ty VALUES(91),(92),(93); + SELECT min((SELECT count(y) FROM ty)) FROM tx; +} {3} +do_execsql_test aggnested-4.4 { + SELECT max((SELECT a FROM (SELECT count(*) AS a FROM ty) AS s)) FROM tx; +} {3} + +#-------------------------------------------------------------------------- +# +reset_db +do_execsql_test 5.0 { + CREATE TABLE x1(a, b); + INSERT INTO x1 VALUES(1, 2); + CREATE TABLE x2(x); + INSERT INTO x2 VALUES(NULL), (NULL), (NULL); +} + +# At one point, aggregate "total()" in the query below was being processed +# as part of the outer SELECT, not as part of the sub-select with no FROM +# clause. +do_execsql_test 5.1 { + SELECT ( SELECT total( (SELECT b FROM x1) ) ) FROM x2; +} {2.0 2.0 2.0} + +do_execsql_test 5.2 { + SELECT ( SELECT total( (SELECT 2 FROM x1) ) ) FROM x2; +} {2.0 2.0 2.0} + +do_execsql_test 5.3 { + CREATE TABLE t1(a); + CREATE TABLE t2(b); +} + +do_execsql_test 5.4 { + SELECT( + SELECT max(b) LIMIT ( + SELECT total( (SELECT a FROM t1) ) + ) + ) + FROM t2; +} {{}} + +do_execsql_test 5.5 { + CREATE TABLE a(b); + WITH c AS(SELECT a) + SELECT(SELECT(SELECT string_agg(b, b) + LIMIT(SELECT 0.100000 * + AVG(DISTINCT(SELECT 0 FROM a ORDER BY b, b, b)))) + FROM a GROUP BY b, + b, b) FROM a EXCEPT SELECT b FROM a ORDER BY b, + b, b; +} + +#------------------------------------------------------------------------- +# dbsqlfuzz a779227f721a834df95f4f42d0c31550a1f8b8a2 +# +reset_db +do_execsql_test 6.0 { + CREATE TABLE t1(a); + CREATE TABLE t2(b); + + INSERT INTO t1 VALUES('x'); + INSERT INTO t2 VALUES(1); +} + +do_execsql_test 6.1.1 { + SELECT ( + SELECT t2.b FROM (SELECT t2.b AS c FROM t1) GROUP BY 1 HAVING t2.b + ) + FROM t2 GROUP BY 'constant_string'; +} {1} +do_execsql_test 6.1.2 { + SELECT ( + SELECT c FROM (SELECT t2.b AS c FROM t1) GROUP BY c HAVING t2.b + ) + FROM t2 GROUP BY 'constant_string'; +} {1} + +do_execsql_test 6.2.0 { + UPDATE t2 SET b=0 +} +do_execsql_test 6.2.1 { + SELECT ( + SELECT t2.b FROM (SELECT t2.b AS c FROM t1) GROUP BY 1 HAVING t2.b + ) + FROM t2 GROUP BY 'constant_string'; +} {{}} +do_execsql_test 6.2.2 { + SELECT ( + SELECT c FROM (SELECT t2.b AS c FROM t1) GROUP BY c HAVING t2.b + ) + FROM t2 GROUP BY 'constant_string'; +} {{}} + +#------------------------------------------------------------------------- +reset_db + +do_execsql_test 7.0 { + CREATE TABLE invoice ( + id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, + amount DOUBLE PRECISION DEFAULT NULL, + name VARCHAR(100) DEFAULT NULL + ); + + INSERT INTO invoice (amount, name) VALUES + (4.0, 'Michael'), (15.0, 'Bara'), (4.0, 'Michael'), (6.0, 'John'); +} + +do_execsql_test 7.1 { + SELECT sum(amount), name + from invoice + group by name + having (select v > 6 from (select sum(amount) v) t) +} { + 15.0 Bara + 8.0 Michael +} + +do_execsql_test 7.2 { + SELECT (select 1 from (select sum(amount))) FROM invoice +} {1} + +do_execsql_test 8.0 { + CREATE TABLE t1(x INT); + INSERT INTO t1 VALUES(100); + INSERT INTO t1 VALUES(20); + INSERT INTO t1 VALUES(3); + SELECT (SELECT y FROM (SELECT sum(x) AS y) AS t2 ) FROM t1; +} {123} + +do_execsql_test 8.1 { + SELECT ( + SELECT y FROM ( + SELECT z AS y FROM (SELECT sum(x) AS z) AS t2 + ) + ) FROM t1; +} {123} + +do_execsql_test 8.2 { + SELECT ( + SELECT a FROM ( + SELECT y AS a FROM ( + SELECT z AS y FROM (SELECT sum(x) AS z) AS t2 + ) + ) + ) FROM t1; +} {123} + +#------------------------------------------------------------------------- +# dbsqlfuzz 04408efc51ae46897c4c122b407412045ed221b4 +# +reset_db + +do_execsql_test 9.1 { + WITH out(i, j, k) AS ( + VALUES(1234, 5678, 9012) + ) + SELECT ( + SELECT ( + SELECT min(abc) = ( SELECT ( SELECT 1234 fROM (SELECT abc) ) ) + FROM ( + SELECT sum( out.i ) + ( SELECT sum( out.i ) ) AS abc FROM (SELECT out.j) + ) + ) + ) FROM out; +} {0} + +do_execsql_test 9.2 { + CREATE TABLE t1(a); + CREATE TABLE t2(b); + INSERT INTO t1 VALUES(1), (2), (3); + INSERT INTO t2 VALUES(4), (5), (6); + + SELECT ( + SELECT min(y) + (SELECT x) FROM ( + SELECT sum(a) AS x, b AS y FROM t2 + ) + ) + FROM t1; +} {10} + +do_execsql_test 9.3 { + SELECT ( + SELECT min(y) + (SELECT (SELECT x)) FROM ( + SELECT sum(a) AS x, b AS y FROM t2 + ) + ) + FROM t1; +} {10} + +do_execsql_test 9.4 { + SELECT ( + SELECT (SELECT x) FROM ( + SELECT sum(a) AS x, b AS y FROM t2 + ) GROUP BY y + ) + FROM t1; +} {6} + +do_execsql_test 9.5 { + SELECT ( + SELECT (SELECT (SELECT x)) FROM ( + SELECT sum(a) AS x, b AS y FROM t2 + ) GROUP BY y + ) + FROM t1; +} {6} + +# 2023-12-16 +# New test case for check-in [4470f657d2069972] from 2023-11-02 +# https://bugs.chromium.org/p/chromium/issues/detail?id=1511689 +# +do_execsql_test 10.1 { + DROP TABLE IF EXISTS t0; + DROP TABLE IF EXISTS t1; + CREATE TABLE t0(c1, c2); INSERT INTO t0 VALUES(1,2); + CREATE TABLE t1(c3, c4); INSERT INTO t1 VALUES(3,4); + SELECT * FROM t0 WHERE EXISTS (SELECT 1 FROM t1 GROUP BY c3 HAVING ( SELECT count(*) FROM (SELECT 1 UNION ALL SELECT sum(DISTINCT c1) ) ) ) BETWEEN 1 AND 1; +} {1 2} finish_test diff --git a/test/aggorderby.test b/test/aggorderby.test new file mode 100644 index 0000000000..466074815a --- /dev/null +++ b/test/aggorderby.test @@ -0,0 +1,174 @@ +# 2023-10-18 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements tests for ORDER BY on aggregate functions. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +do_execsql_test aggorderby-1.1 { + CREATE TABLE t1(a TEXT,b INT,c INT,d INT); + WITH RECURSIVE c(x) AS (VALUES(0) UNION ALL SELECT x+1 FROM c WHERE x<9) + INSERT INTO t1(a,b,c,d) SELECT printf('%d',(x*7)%10),1,x,10-x FROM c; + INSERT INTO t1(a,b,c,d) SELECT a, 2, c, 10-d FROM t1; + CREATE INDEX t1b ON t1(b); +} +do_catchsql_test aggorderby-1.2 { + SELECT b, group_concat(a ORDER BY max(d)) FROM t1 GROUP BY b; +} {1 {misuse of aggregate function max()}} +do_catchsql_test aggorderby-1.3 { + SELECT abs(a ORDER BY max(d)) FROM t1; +} {1 {ORDER BY may not be used with non-aggregate abs()}} + +do_execsql_test aggorderby-2.0 { + SELECT group_concat(a ORDER BY a) FROM t1 WHERE b=1; +} {0,1,2,3,4,5,6,7,8,9} +do_execsql_test aggorderby-2.1 { + SELECT group_concat(a ORDER BY c) FROM t1 WHERE b=1; +} {0,7,4,1,8,5,2,9,6,3} +do_execsql_test aggorderby-2.2 { + SELECT group_concat(a ORDER BY b, d) FROM t1; +} {3,6,9,2,5,8,1,4,7,0,0,7,4,1,8,5,2,9,6,3} +do_execsql_test aggorderby-2.3 { + SELECT string_agg(a, ',' ORDER BY b DESC, d) FROM t1; +} {0,7,4,1,8,5,2,9,6,3,3,6,9,2,5,8,1,4,7,0} +do_execsql_test aggorderby-2.4 { + SELECT b, group_concat(a ORDER BY d) FROM t1 GROUP BY b ORDER BY b; +} {1 3,6,9,2,5,8,1,4,7,0 2 0,7,4,1,8,5,2,9,6,3} + +do_execsql_test aggorderby-3.0 { + SELECT group_concat(DISTINCT a ORDER BY a) FROM t1; +} {0,1,2,3,4,5,6,7,8,9} +do_execsql_test aggorderby-3.1 { + SELECT group_concat(DISTINCT a ORDER BY c) FROM t1; +} {0,7,4,1,8,5,2,9,6,3} + +do_execsql_test aggorderby-4.0 { + SELECT count(ORDER BY a) FROM t1; +} 20 +do_execsql_test aggorderby-4.1 { + SELECT c, max(a ORDER BY a) FROM t1; +} {7 9} + + +do_execsql_test aggorderby-5.0 { + DROP TABLE IF EXISTS t1; + DROP TABLE IF EXISTS t3; + CREATE TABLE t1(a TEXT); INSERT INTO t1 VALUES('aaa'),('bbb'); + CREATE TABLE t3(d TEXT); INSERT INTO t3 VALUES('/'),('-'); + SELECT (SELECT string_agg(a,d) FROM t3) FROM t1; +} {aaa-aaa bbb-bbb} +do_execsql_test aggorderby-5.1 { + SELECT (SELECT group_concat(a,d ORDER BY d) FROM t3) FROM t1; +} {aaa/aaa bbb/bbb} +do_execsql_test aggorderby-5.2 { + SELECT (SELECT string_agg(a,d ORDER BY d DESC) FROM t3) FROM t1; +} {aaa-aaa bbb-bbb} +do_execsql_test aggorderby-5.3 { + SELECT (SELECT string_agg(a,'#' ORDER BY d) FROM t3) FROM t1; +} {aaa#aaa bbb#bbb} + +# COLLATE works on the ORDER BY. +# +do_execsql_test aggorderby-6.0 { + WITH c(x) AS (VALUES('abc'),('DEF'),('xyz'),('ABC'),('XYZ')) + SELECT string_agg(x,',' ORDER BY x COLLATE nocase), + string_agg(x,',' ORDER BY x) FROM c; +} {abc,ABC,DEF,xyz,XYZ ABC,DEF,XYZ,abc,xyz} +do_execsql_test aggorderby-6.1 { + WITH c(x,y) AS (VALUES(1,'a'),(2,'B'),(3,'c'),(4,'D')) + SELECT group_concat(x ORDER BY y COLLATE nocase), + group_concat(x ORDER BY y COLLATE binary) FROM c; +} {1,2,3,4 2,4,1,3} + +# NULLS FIRST and NULLS LAST work on the ORDER BY +# +do_execsql_test aggorderby-7.0 { + WITH c(x) AS (VALUES(1),(NULL),(2.5),(NULL),('three')) + SELECT json_group_array(x ORDER BY x NULLS FIRST), + json_group_array(x ORDER BY x NULLS LAST) FROM c; +} {[null,null,1,2.5,"three"] [1,2.5,"three",null,null]} +do_execsql_test aggorderby-7.1 { + WITH c(x,y) AS (VALUES(1,9),(2,null),(3,5),(4,null),(5,1)) + SELECT json_group_array(x ORDER BY y NULLS FIRST, x), + json_group_array(x ORDER BY y NULLS LAST, x) FROM c; +} {[2,4,5,3,1] [5,3,1,2,4]} + +# The DISTINCT only applies to the function arguments, not to the +# ORDER BY arguments. +# +do_execsql_test aggorderby-8.0 { + WITH c(x,y,z) AS (VALUES('a',4,5),('b',3,6),('c',2,7),('c',1,8)) + SELECT group_concat(DISTINCT x ORDER BY y, z) FROM c; +} {c,b,a} +do_execsql_test aggorderby-8.1 { + WITH c(x,y,z) AS (VALUES('a',4,5),('b',3,6),('b',2,7),('c',1,8)) + SELECT group_concat(DISTINCT x ORDER BY y, z) FROM c; +} {c,b,a} +do_execsql_test aggorderby-8.2 { + WITH c(x,y) AS (VALUES(1,1),(2,2),(3,3),(3,4),(3,5),(3,6)) + SELECT sum(DISTINCT x ORDER BY y) FROM c; +} 6 + +# Subtype information is transfered through the sorter for aggregates +# that make use of subtype info. +# +do_execsql_test aggorderby-9.0 { + WITH c(x,y) AS (VALUES + ('{a:3}', 3), + ('[1,1]', 1), + ('[4,4]', 4), + ('{x:2}', 2)) + SELECT json_group_array(json(x) ORDER BY y) FROM c; +} {{[[1,1],{"x":2},{"a":3},[4,4]]}} +do_execsql_test aggorderby-9.1 { + WITH c(x,y) AS (VALUES + ('[4,4]', 4), + ('{a:3}', 3), + ('[4,4]', 4), + ('[1,1]', 1), + ('[4,4]', 4), + ('{x:2}', 2)) + SELECT json_group_array(DISTINCT json(x) ORDER BY y) FROM c; +} {{[[1,1],{"x":2},{"a":3},[4,4]]}} +do_execsql_test aggorderby-9.2 { + WITH c(x,y) AS (VALUES + ('{a:3}', 3), + ('[1,1]', 1), + ('[4,4]', 4), + ('{x:2}', 2)) + SELECT json_group_array(json(x) ORDER BY json(x)) FROM c; +} {{[[1,1],[4,4],{"a":3},{"x":2}]}} +do_execsql_test aggorderby-9.3 { + WITH c(x,y) AS (VALUES + ('[4,4]', 4), + ('{a:3}', 3), + ('[4,4]', 4), + ('[1,1]', 1), + ('[4,4]', 4), + ('{x:2}', 2)) + SELECT json_group_array(DISTINCT json(x) ORDER BY json(x)) FROM c; +} {{[[1,1],[4,4],{"a":3},{"x":2}]}} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test aggorderby-10.0 { + CREATE TABLE t1(w, x); + INSERT INTO t1 VALUES(1, 2); +} + +for {set i 0} {$i < 70000} {incr i} { lappend lExpr x } +do_catchsql_test aggorderby-10.1 " + SELECT group_concat(w ORDER BY [join $lExpr ,]) FROM t1 +" {1 {too many terms in ORDER BY clause}} + + +finish_test diff --git a/test/all.test b/test/all.test index f6e722f2df..22d7b8daed 100644 --- a/test/all.test +++ b/test/all.test @@ -16,6 +16,7 @@ source $testdir/permutations.test run_test_suite full +ifcapable rbu { run_test_suite rbu } run_test_suite no_optimization run_test_suite memsubsys1 run_test_suite memsubsys2 @@ -41,7 +42,7 @@ run_test_suite pcache100 run_test_suite prepare run_test_suite mmap -if {$::tcl_platform(platform)=="unix"} { +if {$::tcl_platform(platform) eq "unix"} { ifcapable !default_autovacuum { run_test_suite autovacuum_crash } diff --git a/test/alter.test b/test/alter.test index ebfe97a764..9201f40adc 100644 --- a/test/alter.test +++ b/test/alter.test @@ -77,7 +77,7 @@ do_test alter-1.2 { ifcapable tempdb { execsql { INSERT INTO objlist SELECT type, name, tbl_name - FROM sqlite_temp_master WHERE NAME!='objlist'; + FROM temp.sqlite_master WHERE NAME!='objlist'; } } @@ -153,7 +153,7 @@ ifcapable tempdb { CREATE TEMP TABLE objlist(type, name, tbl_name); INSERT INTO objlist SELECT type, name, tbl_name FROM sqlite_master; INSERT INTO objlist - SELECT type, name, tbl_name FROM sqlite_temp_master + SELECT type, name, tbl_name FROM temp.sqlite_master WHERE NAME!='objlist'; SELECT type, name, tbl_name FROM objlist ORDER BY tbl_name, type desc, name; @@ -524,7 +524,7 @@ do_test alter-3.3.7 { ifcapable tempdb { do_test alter-3.3.8 { execsql { - SELECT * FROM sqlite_temp_master WHERE type = 'trigger'; + SELECT * FROM temp.sqlite_master WHERE type = 'trigger'; } } {} } @@ -681,21 +681,30 @@ do_test alter-8.2 { } {1 18 2 9} #-------------------------------------------------------------------------- -# alter-9.X - Special test: Make sure the sqlite_rename_trigger() and +# alter-9.X - Special test: Make sure the sqlite_rename_column() and # rename_table() functions do not crash when handed bad input. # -ifcapable trigger { - do_test alter-9.1 { - execsql {SELECT SQLITE_RENAME_TRIGGER(0,0)} - } {{}} +sqlite3_test_control SQLITE_TESTCTRL_INTERNAL_FUNCTIONS db +do_test alter-9.1 { + execsql {SELECT SQLITE_RENAME_COLUMN(0,0,0,0,0,0,0,0,0)} +} {{}} +foreach {tn sql} { + 1 { SELECT SQLITE_RENAME_TABLE(0,0,0,0,0,0,0) } + 2 { SELECT SQLITE_RENAME_TABLE(10,20,30,40,50,60,70) } + 3 { SELECT SQLITE_RENAME_TABLE('foo','foo','foo','foo','foo','foo','foo') } +} { + do_test alter-9.2.$tn { + catch { execsql $sql } + } 1 } -do_test alter-9.2 { - execsql { - SELECT SQLITE_RENAME_TABLE(0,0); - SELECT SQLITE_RENAME_TABLE(10,20); - SELECT SQLITE_RENAME_TABLE('foo', 'foo'); - } -} {{} {} {}} +sqlite3_test_control SQLITE_TESTCTRL_INTERNAL_FUNCTIONS db + +# If the INTERNAL_FUNCTIONS test-control is disabled (which is the default), +# then the sqlite_rename_table() SQL function is not accessible to ordinary SQL. +# +do_catchsql_test alter-9.3 { + SELECT sqlite_rename_table(0,0,0,0,0,0,0); +} {1 {no such function: sqlite_rename_table}} #------------------------------------------------------------------------ # alter-10.X - Make sure ALTER TABLE works with multi-byte UTF-8 characters @@ -831,6 +840,7 @@ do_test alter-13.3 { do_test alter-14.1 { catchsql { CREATE TABLE t3651(a UNIQUE); + INSERT INTO t3651 VALUES(5); ALTER TABLE t3651 ADD COLUMN b UNIQUE; } } {1 {Cannot add a UNIQUE column}} @@ -847,7 +857,6 @@ do_test alter-14.2 { set system_table_list {1 sqlite_master} catchsql ANALYZE ifcapable analyze { lappend system_table_list 2 sqlite_stat1 } -ifcapable stat3 { lappend system_table_list 3 sqlite_stat3 } ifcapable stat4 { lappend system_table_list 4 sqlite_stat4 } foreach {tn tbl} $system_table_list { @@ -875,51 +884,103 @@ do_execsql_test alter-16.2 { SELECT * FROM t16a_rn ORDER BY a; } {abc 1.25 99 xyzzy cba 5.5 98 fizzle} -#------------------------------------------------------------------------- -# Verify that NULL values into the internal-use-only sqlite_rename_*() -# functions do not cause problems. +# 2018-09-16 ticket b41031ea2b5372378cb3d2d43cf9fe2a4a5c2510 # -do_execsql_test alter-17.1 { - SELECT sqlite_rename_table('CREATE TABLE xyz(a,b,c)','abc'); -} {{CREATE TABLE "abc"(a,b,c)}} -do_execsql_test alter-17.2 { - SELECT sqlite_rename_table('CREATE TABLE xyz(a,b,c)',NULL); -} {{CREATE TABLE "(NULL)"(a,b,c)}} -do_execsql_test alter-17.3 { - SELECT sqlite_rename_table(NULL,'abc'); -} {{}} -do_execsql_test alter-17.4 { - SELECT sqlite_rename_trigger('CREATE TRIGGER r1 ON xyz WHEN','abc'); -} {{CREATE TRIGGER r1 ON "abc" WHEN}} -do_execsql_test alter-17.5 { - SELECT sqlite_rename_trigger('CREATE TRIGGER r1 ON xyz WHEN',NULL); -} {{CREATE TRIGGER r1 ON "(NULL)" WHEN}} -do_execsql_test alter-17.6 { - SELECT sqlite_rename_trigger(NULL,'abc'); -} {{}} -do_execsql_test alter-17.7 { - SELECT sqlite_rename_parent('CREATE TABLE t1(a REFERENCES "xyzzy")', - 'xyzzy','lmnop'); -} {{CREATE TABLE t1(a REFERENCES "lmnop")}} -do_execsql_test alter-17.8 { - SELECT sqlite_rename_parent('CREATE TABLE t1(a REFERENCES "xyzzy")', - 'xyzzy',NULL); -} {{CREATE TABLE t1(a REFERENCES "(NULL)")}} -do_execsql_test alter-17.9 { - SELECT sqlite_rename_parent('CREATE TABLE t1(a REFERENCES "xyzzy")', - NULL, 'lmnop'); -} {{}} -do_execsql_test alter-17.10 { - SELECT sqlite_rename_parent(NULL,'abc','xyz'); -} {{}} -do_execsql_test alter-17.11 { - SELECT sqlite_rename_parent('create references ''','abc','xyz'); -} {{create references '}} -do_execsql_test alter-17.12 { - SELECT sqlite_rename_parent('create references "abc"123" ','abc','xyz'); -} {{create references "xyz"123" }} -do_execsql_test alter-17.13 { - SELECT sqlite_rename_parent("references '''",'abc','xyz'); -} {{references '''}} +ifcapable rtree { + db close + sqlite3 db :memory: + do_execsql_test alter-17.100 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b); + CREATE VIRTUAL TABLE t2 USING rtree(id,x0,x1); + INSERT INTO t1 VALUES(1,'apple'),(2,'fig'),(3,'pear'); + INSERT INTO t2 VALUES(1,1.0,2.0),(2,2.0,3.0),(3,1.5,3.5); + CREATE TRIGGER r1 AFTER UPDATE ON t1 BEGIN + DELETE FROM t2 WHERE id = OLD.a; + END; + ALTER TABLE t1 RENAME TO t3; + UPDATE t3 SET b='peach' WHERE a=2; + SELECT * FROM t2 ORDER BY 1; + } {1 1.0 2.0 3 1.5 3.5} +} + +# 2021-03-08 dbsqlfuzz 3f0a7245b69cd08617d7d7781ebaedb0fe765a93 +reset_db +do_catchsql_test alter-18.1 { + CREATE TABLE t1(a,b,c); + CREATE TABLE log(a INTEGER PRIMARY KEY,b,c); + CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN + INSERT INTO logx(a,b,c) VALUES(new.a,new.b,new.c) + ON CONFLICT(a) DO UPDATE SET c=excluded.c, b=new.b; + END; + ALTER TABLE log RENAME COLUMN a TO x; +} {1 {error in trigger tr1: no such table: main.logx}} + +# 2021-10-13 dbsqlfuzz e89174cbfad2d904f06b5e24df0a22510b6a1c1e +reset_db +do_execsql_test alter-19.1 { + CREATE TABLE t1(x); + CREATE TABLE t2(c); + CREATE TRIGGER r1 AFTER INSERT ON t2 BEGIN + UPDATE t2 SET (c)=( + EXISTS(SELECT 1 WHERE (WITH cte1(a) AS (SELECT 1 FROM t1 WHERE (SELECT 1 WHERE (WITH cte2(b) AS (VALUES(1))SELECT b FROM cte2)))SELECT a FROM cte1)) + ); + END; + ALTER TABLE t2 RENAME TO t3; +} {} +do_execsql_test alter-19.2 { + SELECT name FROM sqlite_schema WHERE sql LIKE '%t2%'; +} {} +do_execsql_test alter-19.3 { + SELECT name FROM sqlite_schema WHERE sql LIKE '%t3%' ORDER BY name; +} {r1 t3} + +# 2023-10-14 +# On an ALTER TABLE ADD COLUMN with a DEFAULT clause on a STRICT table +# make sure that the DEFAULT has a compatible type. +# +reset_db +do_execsql_test alter-20.1 { + CREATE TABLE t1(a INT) STRICT; + INSERT INTO t1(a) VALUES(45); +} {} +do_catchsql_test alter-20.2 { + ALTER TABLE t1 ADD COLUMN b TEXT DEFAULT x'313233'; +} {1 {type mismatch on DEFAULT}} +do_execsql_test alter-20.2 { + DELETE FROM t1; + ALTER TABLE t1 ADD COLUMN b TEXT DEFAULT x'313233'; +} {} +do_catchsql_test alter-20.3 { + INSERT INTO t1(a) VALUES(45); +} {1 {cannot store BLOB value in TEXT column t1.b}} + +# 2023-11-17 dbsqlfuzz e0900262dadd5c78c2226ad6a435c7f0255be2cd +# Assertion fault associated with ALTER TABLE and an +# aggregate ORDER BY within an unknown aggregate function. +# +reset_db +do_execsql_test alter-21.1 { + CREATE TABLE t1(a,b,c,d); + CREATE TABLE t2(a,b,c,d,x); + CREATE TRIGGER r1 AFTER INSERT ON t2 BEGIN + SELECT unknown_function(a ORDER BY (SELECT group_concat(DISTINCT a ORDER BY a) FROM t1)) FROM t1; + END; + ALTER TABLE t2 RENAME TO e; +} {} +do_execsql_test alter-21.2 { + SELECT name, type FROM sqlite_schema ORDER BY name; +} {e table r1 trigger t1 table} +do_execsql_test alter-21.3 { + DROP TRIGGER r1; + CREATE TRIGGER r2 AFTER INSERT ON e BEGIN + SELECT unknown_function(a ORDER BY (SELECT group_concat(a ORDER BY a) FROM (SELECT b FROM t1))) FROM t1; + END; + ALTER TABLE e RENAME TO t99; +} +do_execsql_test alter-21.4 { + SELECT name, type FROM sqlite_schema ORDER BY name; +} {r2 trigger t1 table t99 table} + + finish_test diff --git a/test/alter2.test b/test/alter2.test index 14be637f97..20b75b59ee 100644 --- a/test/alter2.test +++ b/test/alter2.test @@ -64,6 +64,7 @@ proc alter_table {tbl sql {file_format 2}} { sqlite3 dbat test.db set s [string map {' ''} $sql] set t [string map {' ''} $tbl] + sqlite3_db_config dbat DEFENSIVE 0 dbat eval [subst { PRAGMA writable_schema = 1; UPDATE sqlite_master SET sql = '$s' WHERE name = '$t' AND type = 'table'; @@ -91,6 +92,7 @@ do_test alter2-1.0 { #----------------------------------------------------------------------- # Some basic tests to make sure short rows are handled. # +sqlite3_db_config db DEFENSIVE 0 do_test alter2-1.1 { execsql { CREATE TABLE abc(a, b); @@ -369,7 +371,7 @@ do_test alter2-7.5 { execsql { SELECT a, typeof(a), b, typeof(b), c, typeof(c) FROM t1 LIMIT 1; } -} {1 integer -123 integer 5 text} +} {1 integer -123.0 real 5 text} #----------------------------------------------------------------------- # Test that UPDATE trigger tables work with default values, and that when @@ -395,11 +397,11 @@ do_test alter2-8.2 { UPDATE t1 SET c = 10 WHERE a = 1; SELECT a, typeof(a), b, typeof(b), c, typeof(c) FROM t1 LIMIT 1; } -} {1 integer -123 integer 10 text} +} {1 integer -123.0 real 10 text} ifcapable trigger { do_test alter2-8.3 { set ::val - } {-123 integer 5 text -123 integer 10 text} + } {-123.0 real 5 text -123.0 real 10 text} } #----------------------------------------------------------------------- @@ -423,7 +425,7 @@ ifcapable trigger { DELETE FROM t1 WHERE a = 2; } set ::val - } {-123 integer 5 text} + } {-123.0 real 5 text} } #----------------------------------------------------------------------- diff --git a/test/alter3.test b/test/alter3.test index f8ebe056f2..464e20aee2 100644 --- a/test/alter3.test +++ b/test/alter3.test @@ -13,8 +13,6 @@ # file format change that may be used in the future to implement # "ALTER TABLE ... ADD COLUMN". # -# $Id: alter3.test,v 1.11 2008/03/19 00:21:31 drh Exp $ -# set testdir [file dirname $argv0] @@ -54,8 +52,8 @@ proc get_file_format {{fname test.db}} { } do_test alter3-1.1 { + sqlite3_db_config db LEGACY_FILE_FORMAT 1 execsql { - PRAGMA legacy_file_format=ON; CREATE TABLE abc(a, b, c); SELECT sql FROM sqlite_master; } @@ -103,7 +101,7 @@ do_test alter3-1.7 { } {{CREATE TABLE t3(a, b, c VARCHAR(10, 20), UNIQUE(a, b))}} do_test alter3-1.99 { catchsql { - # May not exist if foriegn-keys are omitted at compile time. + # May not exist if foreign-keys are omitted at compile time. DROP TABLE t2; } execsql { @@ -116,6 +114,7 @@ do_test alter3-1.99 { do_test alter3-2.1 { execsql { CREATE TABLE t1(a, b); + INSERT INTO t1 VALUES(1,2); } catchsql { ALTER TABLE t1 ADD c PRIMARY KEY; @@ -184,7 +183,7 @@ do_test alter3-3.2 { if {!$has_codec} { do_test alter3-3.3 { get_file_format - } {4} + } {3} } ifcapable schema_version { do_test alter3-3.4 { @@ -198,8 +197,8 @@ do_test alter3-4.1 { db close forcedelete test.db set ::DB [sqlite3 db test.db] + sqlite3_db_config db LEGACY_FILE_FORMAT 1 execsql { - PRAGMA legacy_file_format=ON; CREATE TABLE t1(a, b); INSERT INTO t1 VALUES(1, 100); INSERT INTO t1 VALUES(2, 300); @@ -220,7 +219,7 @@ do_test alter3-4.2 { if {!$has_codec} { do_test alter3-4.3 { get_file_format - } {4} + } {3} } ifcapable schema_version { do_test alter3-4.4 { @@ -270,7 +269,7 @@ ifcapable attach { if {!$has_codec} { do_test alter3-5.5 { list [get_file_format test2.db] [get_file_format] - } {4 4} + } {3 3} } do_test alter3-5.6 { execsql { @@ -347,19 +346,19 @@ if {!$has_codec} { ALTER TABLE abc ADD d DEFAULT NULL; } get_file_format - } {4} + } {3} do_test alter3-7.3 { execsql { ALTER TABLE abc ADD e DEFAULT 10; } get_file_format - } {4} + } {3} do_test alter3-7.4 { execsql { ALTER TABLE abc ADD f DEFAULT NULL; } get_file_format - } {4} + } {3} do_test alter3-7.5 { execsql { VACUUM; @@ -394,4 +393,51 @@ do_test alter3-8.2 { } } [list $::sql] +# 2021-07-20: Add support for detecting CHECK and NOT NULL constraint +# violations in ALTER TABLE ADD COLUMN +# +reset_db +do_execsql_test alter3-9.1 { + CREATE TABLE t1(a,b); + INSERT INTO t1 VALUES(1, 2), ('null!',NULL), (3,4); +} {} +do_catchsql_test alter3-9.2 { + ALTER TABLE t1 ADD COLUMN c CHECK(a!=1); +} {1 {CHECK constraint failed}} +do_catchsql_test alter3-9.3 { + ALTER TABLE t1 ADD COLUMN c CHECK(a!=3); +} {1 {CHECK constraint failed}} +do_catchsql_test alter3-9.4 { + ALTER TABLE t1 ADD COLUMN c CHECK(a!=2); +} {0 {}} +do_catchsql_test alter3-9.5 { + ALTER TABLE t1 ADD COLUMN d AS (b+1) NOT NULL; +} {1 {NOT NULL constraint failed}} +do_catchsql_test alter3-9.6 { + ALTER TABLE t1 ADD COLUMN d AS (b+1) NOT NULL CHECK(a!=1); +} {1 {CHECK constraint failed}} +do_catchsql_test alter3-9.7 { + ALTER TABLE t1 ADD COLUMN d AS (b+1) NOT NULL CHECK(a!=3); +} {1 {NOT NULL constraint failed}} + +do_execsql_test alter3-9.10 { + CREATE TEMP TABLE t0(m,n); + INSERT INTO t0 VALUES(1, 2), ('null!',NULL), (3,4); + ATTACH ':memory:' AS aux1; + CREATE TABLE aux1.t2(x,y); + INSERT INTO t2 VALUES(1, 2), ('null!',NULL), (3,4); +} {} +do_catchsql_test alter3-9.11 { + ALTER TABLE t0 ADD COLUMN xtra1 AS (n+1) NOT NULL CHECK(m!=1); +} {1 {CHECK constraint failed}} +do_catchsql_test alter3-9.12 { + ALTER TABLE t0 ADD COLUMN xtra1 AS (n+1) NOT NULL CHECK(m!=3); +} {1 {NOT NULL constraint failed}} +do_catchsql_test alter3-9.13 { + ALTER TABLE t2 ADD COLUMN xtra1 AS (y+1) NOT NULL CHECK(x!=1); +} {1 {CHECK constraint failed}} +do_catchsql_test alter3-9.14 { + ALTER TABLE t2 ADD COLUMN xtra1 AS (y+1) NOT NULL CHECK(x!=3); +} {1 {NOT NULL constraint failed}} + finish_test diff --git a/test/alter4.test b/test/alter4.test index ac39d614a5..7e2d7e66a8 100644 --- a/test/alter4.test +++ b/test/alter4.test @@ -45,18 +45,33 @@ do_test alter4-1.1 { SELECT sql FROM sqlite_temp_master; } } {{CREATE TABLE abc(a, b, c)}} +do_test alter4-1.1b { + execsql { + SELECT sql FROM temp.sqlite_master; + } +} {{CREATE TABLE abc(a, b, c)}} do_test alter4-1.2 { execsql {ALTER TABLE abc ADD d INTEGER;} execsql { SELECT sql FROM sqlite_temp_master; } } {{CREATE TABLE abc(a, b, c, d INTEGER)}} +do_test alter4-1.2b { + execsql { + SELECT sql FROM temp.sqlite_master; + } +} {{CREATE TABLE abc(a, b, c, d INTEGER)}} do_test alter4-1.3 { execsql {ALTER TABLE abc ADD e} execsql { SELECT sql FROM sqlite_temp_master; } } {{CREATE TABLE abc(a, b, c, d INTEGER, e)}} +do_test alter4-1.3b { + execsql { + SELECT sql FROM temp.sqlite_master; + } +} {{CREATE TABLE abc(a, b, c, d INTEGER, e)}} do_test alter4-1.4 { execsql { CREATE TABLE temp.t1(a, b); @@ -64,6 +79,11 @@ do_test alter4-1.4 { SELECT sql FROM sqlite_temp_master WHERE tbl_name = 't1'; } } {{CREATE TABLE t1(a, b, c)}} +do_test alter4-1.4b { + execsql { + SELECT sql FROM temp.sqlite_master WHERE tbl_name = 't1'; + } +} {{CREATE TABLE t1(a, b, c)}} do_test alter4-1.5 { execsql { ALTER TABLE t1 ADD d CHECK (a>d); @@ -90,7 +110,7 @@ do_test alter4-1.7 { } {{CREATE TABLE t3(a, b, c VARCHAR(10, 20), UNIQUE(a, b))}} do_test alter4-1.99 { catchsql { - # May not exist if foriegn-keys are omitted at compile time. + # May not exist if foreign-keys are omitted at compile time. DROP TABLE t2; } execsql { @@ -103,6 +123,7 @@ do_test alter4-1.99 { do_test alter4-2.1 { execsql { CREATE TABLE temp.t1(a, b); + INSERT INTO t1 VALUES(1,2); } catchsql { ALTER TABLE t1 ADD c PRIMARY KEY; @@ -297,16 +318,16 @@ ifcapable trigger&&tempdb { END; INSERT INTO t1 VALUES(1, 2); - SELECT * FROM log; + SELECT * FROM log ORDER BY trig, a, b; } - } {b 1 2 a 1 2} + } {a 1 2 b 1 2} do_test alter4-6.2 { execsql { ALTER TABLE t1 ADD COLUMN c DEFAULT 'c'; INSERT INTO t1(a, b) VALUES(3, 4); - SELECT * FROM log; + SELECT * FROM log ORDER BY trig, a, b; } - } {b 1 2 a 1 2 b 3 4 a 3 4} + } {a 1 2 a 3 4 b 1 2 b 3 4} } # Ticket #1183 - Make sure adding columns to large tables does not cause @@ -355,4 +376,52 @@ do_execsql_test alter4-9.3 { SELECT typeof(c), c FROM t5; } {real 9.22337203685478e+18} +# Confirm that doing an ALTER TABLE on a legacy format database +# does not corrupt DESC indexes. +# +# Ticket https://sqlite.org/src/tktview/f68bf68513a1c +# +do_test alter4-10.1 { + db close + sqlite3 db :memory: + sqlite3_db_config db LEGACY_FILE_FORMAT 1 + db eval { + CREATE TABLE t1(a,b,c); + CREATE INDEX t1a ON t1(a DESC); + INSERT INTO t1 VALUES(1,2,3); + INSERT INTO t1 VALUES(2,3,4); + ALTER TABLE t1 ADD COLUMN d; + PRAGMA integrity_check; + } +} {ok} + +reset_db +do_execsql_test alter4-11.0 { + CREATE TABLE t1(c INTEGER PRIMARY KEY, d); + INSERT INTO t1(c,d) VALUES(1,2); + PRAGMA foreign_keys = on; + ALTER TABLE t1 ADD COLUMN e; +} + +do_execsql_test alter4-11.1 { + ALTER TABLE t1 ADD COLUMN f REFERENCES t1; +} + +do_catchsql_test alter4-11.2 { + ALTER TABLE t1 ADD COLUMN g REFERENCES t1 DEFAULT 4; +} {1 {Cannot add a REFERENCES column with non-NULL default value}} + +do_catchsql_test alter4-11.3 { + ALTER TABLE t2 ADD COLUMN g; +} {1 {no such table: t2}} + +ifcapable fts5 { + do_execsql_test alter4-11.4 { + CREATE VIRTUAL TABLE fff USING fts5(f); + } + do_catchsql_test alter4-11.2 { + ALTER TABLE fff ADD COLUMN g; + } {1 {virtual tables may not be altered}} +} + finish_test diff --git a/test/alterauth.test b/test/alterauth.test new file mode 100644 index 0000000000..12645b36f0 --- /dev/null +++ b/test/alterauth.test @@ -0,0 +1,72 @@ +# 2018 September 2 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# + +set testdir [file dirname $argv0] + +source $testdir/tester.tcl + +# If SQLITE_OMIT_ALTERTABLE is defined, omit this file. +ifcapable !altertable { + finish_test + return +} +set testprefix alterauth + +set ::auth [list] +proc xAuth {type args} { + if {$type == "SQLITE_ALTER_TABLE"} { + lappend ::auth [concat $type [lrange $args 0 3]] + } + return SQLITE_OK +} +db auth xAuth + +do_execsql_test 1.0 { CREATE TABLE t1(a, b, c); } + +do_test 1.1 { + set ::auth [list] + execsql { ALTER TABLE t1 RENAME TO t2 } + set ::auth +} {{SQLITE_ALTER_TABLE main t1 {} {}}} + +do_test 1.2 { + set ::auth [list] + execsql { ALTER TABLE t2 RENAME c TO ccc } + set ::auth +} {{SQLITE_ALTER_TABLE main t2 {} {}}} + +do_test 1.3 { + set ::auth [list] + execsql { ALTER TABLE t2 ADD COLUMN d } + set ::auth +} {{SQLITE_ALTER_TABLE main t2 {} {}}} + +proc xAuth {type args} { + if {$type == "SQLITE_ALTER_TABLE"} { + return SQLITE_DENY + } + return SQLITE_OK +} + +do_test 2.1 { + catchsql { ALTER TABLE t2 RENAME TO t3 } +} {1 {not authorized}} + +do_test 2.2 { + catchsql { ALTER TABLE t2 RENAME d TO ddd } +} {1 {not authorized}} + +do_test 2.3 { + catchsql { ALTER TABLE t2 ADD COLUMN e } +} {1 {not authorized}} + +finish_test diff --git a/test/alterauth2.test b/test/alterauth2.test new file mode 100644 index 0000000000..6f9242d364 --- /dev/null +++ b/test/alterauth2.test @@ -0,0 +1,119 @@ +# 2018 October 6 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# + +set testdir [file dirname $argv0] + +source $testdir/tester.tcl + +# If SQLITE_OMIT_ALTERTABLE is defined, omit this file. +ifcapable !altertable { + finish_test + return +} +set testprefix alterauth2 + +set ::auth [list] +proc xAuth {type args} { + lappend ::auth [concat $type [lrange $args 0 3]] + if {$type=="SQLITE_READ" && [lindex $args 0] == "t2"} breakpoint + return SQLITE_OK +} +db auth xAuth + +proc do_auth_test {tn sql authcode} { + set script " + set ::auth \[list\] + execsql {$sql} + lsort -unique \[set ::auth\] + " + + set normal [list {*}$authcode] + uplevel [list do_test $tn $script $normal] +} + +do_execsql_test 1.0 { + CREATE TABLE t1(a, b, c); + CREATE VIEW v1 AS SELECT * FROM t1; + CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN + DELETE FROM t1 WHERE a<new.a; + END; + + CREATE TEMP TRIGGER tr2 AFTER UPDATE OF a, b ON t1 BEGIN + UPDATE t1 SET a=a+1 WHERE new.b<b; + END; +} + +do_auth_test 1.1 { + ALTER TABLE t1 RENAME TO t2; +} { + {SQLITE_ALTER_TABLE main t1 {} {}} + {SQLITE_FUNCTION {} like {} {}} + {SQLITE_FUNCTION {} sqlite_rename_table {} {}} + {SQLITE_FUNCTION {} sqlite_rename_test {} {}} + {SQLITE_FUNCTION {} substr {} {}} + {SQLITE_READ sqlite_master name main {}} + {SQLITE_READ sqlite_master sql main {}} + {SQLITE_READ sqlite_master tbl_name main {}} + {SQLITE_READ sqlite_master type main {}} + {SQLITE_READ sqlite_temp_master name temp {}} + {SQLITE_READ sqlite_temp_master sql temp {}} + {SQLITE_READ sqlite_temp_master tbl_name temp {}} + {SQLITE_READ sqlite_temp_master type temp {}} + {SQLITE_SELECT {} {} {} {}} + {SQLITE_UPDATE sqlite_master name main {}} + {SQLITE_UPDATE sqlite_master sql main {}} + {SQLITE_UPDATE sqlite_master tbl_name main {}} + {SQLITE_UPDATE sqlite_temp_master sql temp {}} + {SQLITE_UPDATE sqlite_temp_master tbl_name temp {}} +} + +do_auth_test 1.2 { + ALTER TABLE t2 RENAME a TO aaa; +} { + {SQLITE_ALTER_TABLE main t2 {} {}} + {SQLITE_FUNCTION {} like {} {}} + {SQLITE_FUNCTION {} sqlite_rename_column {} {}} + {SQLITE_FUNCTION {} sqlite_rename_quotefix {} {}} + {SQLITE_FUNCTION {} sqlite_rename_test {} {}} + {SQLITE_READ sqlite_master name main {}} + {SQLITE_READ sqlite_master sql main {}} + {SQLITE_READ sqlite_master tbl_name main {}} + {SQLITE_READ sqlite_master type main {}} + {SQLITE_READ sqlite_temp_master name temp {}} + {SQLITE_READ sqlite_temp_master sql temp {}} + {SQLITE_READ sqlite_temp_master type temp {}} + {SQLITE_SELECT {} {} {} {}} + {SQLITE_UPDATE sqlite_master sql main {}} + {SQLITE_UPDATE sqlite_temp_master sql temp {}} +} + +do_auth_test 1.3 { + ALTER TABLE t2 DROP COLUMN c; +} { + {SQLITE_ALTER_TABLE main t2 c {}} + {SQLITE_FUNCTION {} like {} {}} + {SQLITE_FUNCTION {} sqlite_drop_column {} {}} + {SQLITE_FUNCTION {} sqlite_rename_quotefix {} {}} + {SQLITE_FUNCTION {} sqlite_rename_test {} {}} + {SQLITE_READ sqlite_master name main {}} + {SQLITE_READ sqlite_master sql main {}} + {SQLITE_READ sqlite_master tbl_name main {}} + {SQLITE_READ sqlite_master type main {}} + {SQLITE_READ sqlite_temp_master name temp {}} + {SQLITE_READ sqlite_temp_master sql temp {}} + {SQLITE_READ sqlite_temp_master type temp {}} + {SQLITE_SELECT {} {} {} {}} + {SQLITE_UPDATE sqlite_master sql main {}} + {SQLITE_UPDATE sqlite_temp_master sql temp {}} +} + +finish_test diff --git a/test/altercol.test b/test/altercol.test new file mode 100644 index 0000000000..5f7de57a4e --- /dev/null +++ b/test/altercol.test @@ -0,0 +1,949 @@ +# 2009 February 2 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# This file implements regression tests for SQLite library. The +# focus of this script is testing that SQLite can handle a subtle +# file format change that may be used in the future to implement +# "ALTER TABLE ... RENAME COLUMN ... TO". +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix altercol + +# If SQLITE_OMIT_ALTERTABLE is defined, omit this file. +ifcapable !altertable { + finish_test + return +} + +# Drop all the tables and views in the 'main' database of database connect +# [db]. Sort the objects by name before dropping them. +# +proc drop_all_tables_and_views {db} { + set SQL { + SELECT name, type FROM sqlite_master + WHERE type IN ('table', 'view') AND name NOT LIKE 'sqlite_%' + ORDER BY 1 + } + foreach {z t} [db eval $SQL] { + db eval "DROP $t $z" + } +} + +foreach {tn before after} { + 1 {CREATE TABLE t1(a INTEGER, b TEXT, c BLOB)} + {CREATE TABLE t1(a INTEGER, d TEXT, c BLOB)} + + 2 {CREATE TABLE t1(a INTEGER, x TEXT, "b" BLOB)} + {CREATE TABLE t1(a INTEGER, x TEXT, "d" BLOB)} + + 3 {CREATE TABLE t1(a INTEGER, b TEXT, c BLOB, CHECK(b!=''))} + {CREATE TABLE t1(a INTEGER, d TEXT, c BLOB, CHECK(d!=''))} + + 4 {CREATE TABLE t1(a INTEGER, b TEXT, c BLOB, CHECK(t1.b!=''))} + {CREATE TABLE t1(a INTEGER, d TEXT, c BLOB, CHECK(t1.d!=''))} + + 5 {CREATE TABLE t1(a INTEGER, b TEXT, c BLOB, CHECK( coalesce(b,c) ))} + {CREATE TABLE t1(a INTEGER, d TEXT, c BLOB, CHECK( coalesce(d,c) ))} + + 6 {CREATE TABLE t1(a INTEGER, "b"TEXT, c BLOB, CHECK( coalesce(b,c) ))} + {CREATE TABLE t1(a INTEGER, "d"TEXT, c BLOB, CHECK( coalesce(d,c) ))} + + 7 {CREATE TABLE t1(a INTEGER, b TEXT, c BLOB, PRIMARY KEY(b, c))} + {CREATE TABLE t1(a INTEGER, d TEXT, c BLOB, PRIMARY KEY(d, c))} + + 8 {CREATE TABLE t1(a INTEGER, b TEXT PRIMARY KEY, c BLOB)} + {CREATE TABLE t1(a INTEGER, d TEXT PRIMARY KEY, c BLOB)} + + 9 {CREATE TABLE t1(a, b TEXT, c, PRIMARY KEY(a, b), UNIQUE("B"))} + {CREATE TABLE t1(a, d TEXT, c, PRIMARY KEY(a, d), UNIQUE("d"))} + + 10 {CREATE TABLE t1(a, b, c); CREATE INDEX t1i ON t1(a, c)} + {{CREATE TABLE t1(a, d, c)} {CREATE INDEX t1i ON t1(a, c)}} + + 11 {CREATE TABLE t1(a, b, c); CREATE INDEX t1i ON t1(b, c)} + {{CREATE TABLE t1(a, d, c)} {CREATE INDEX t1i ON t1(d, c)}} + + 12 {CREATE TABLE t1(a, b, c); CREATE INDEX t1i ON t1(b+b+b+b, c) WHERE b>0} + {{CREATE TABLE t1(a, d, c)} {CREATE INDEX t1i ON t1(d+d+d+d, c) WHERE d>0}} + + 13 {CREATE TABLE t1(a, b, c, FOREIGN KEY (b) REFERENCES t2)} + {CREATE TABLE t1(a, d, c, FOREIGN KEY (d) REFERENCES t2)} + + 14 {CREATE TABLE t1(a INTEGER, b TEXT, c BLOB, PRIMARY KEY(b))} + {CREATE TABLE t1(a INTEGER, d TEXT, c BLOB, PRIMARY KEY(d))} + + 15 {CREATE TABLE t1(a INTEGER, b INTEGER, c BLOB, PRIMARY KEY(b))} + {CREATE TABLE t1(a INTEGER, d INTEGER, c BLOB, PRIMARY KEY(d))} + + 16 {CREATE TABLE t1(a INTEGER, b INTEGER PRIMARY KEY, c BLOB)} + {CREATE TABLE t1(a INTEGER, d INTEGER PRIMARY KEY, c BLOB)} + + 17 {CREATE TABLE t1(a INTEGER, b INTEGER PRIMARY KEY, c BLOB, FOREIGN KEY (b) REFERENCES t2)} + {CREATE TABLE t1(a INTEGER, d INTEGER PRIMARY KEY, c BLOB, FOREIGN KEY (d) REFERENCES t2)} + +} { + reset_db + do_execsql_test 1.$tn.0 $before + + do_execsql_test 1.$tn.1 { + INSERT INTO t1 VALUES(1, 2, 3); + } + + do_execsql_test 1.$tn.2 { + ALTER TABLE t1 RENAME COLUMN b TO d; + } + + do_execsql_test 1.$tn.3 { + SELECT * FROM t1; + } {1 2 3} + + if {[string first INDEX $before]>0} { + set res $after + } else { + set res [list $after] + } + do_execsql_test 1.$tn.4 { + SELECT sql FROM sqlite_master WHERE tbl_name='t1' AND sql!='' + } $res +} + +#------------------------------------------------------------------------- +# +do_execsql_test 2.0 { + CREATE TABLE t3(a, b, c, d, e, f, g, h, i, j, k, l, m, FOREIGN KEY (b, c, d, e, f, g, h, i, j, k, l, m) REFERENCES t4); +} + +sqlite3 db2 test.db +do_execsql_test -db db2 2.1 { SELECT b FROM t3 } + +do_execsql_test 2.2 { + ALTER TABLE t3 RENAME b TO biglongname; + SELECT sql FROM sqlite_master WHERE name='t3'; +} {{CREATE TABLE t3(a, biglongname, c, d, e, f, g, h, i, j, k, l, m, FOREIGN KEY (biglongname, c, d, e, f, g, h, i, j, k, l, m) REFERENCES t4)}} + +do_execsql_test -db db2 2.3 { SELECT biglongname FROM t3 } + +#------------------------------------------------------------------------- +# +do_execsql_test 3.0 { + CREATE TABLE t4(x, y, z); + CREATE TRIGGER ttt AFTER INSERT ON t4 WHEN new.y<0 BEGIN + SELECT x, y, z FROM t4; + DELETE FROM t4 WHERE y=32; + UPDATE t4 SET x=y+1, y=0 WHERE y=32; + INSERT INTO t4(x, y, z) SELECT 4, 5, 6 WHERE 0; + END; + INSERT INTO t4 VALUES(3, 2, 1); +} + +do_execsql_test 3.1 { + ALTER TABLE t4 RENAME y TO abc; + SELECT sql FROM sqlite_master WHERE name='t4'; +} {{CREATE TABLE t4(x, abc, z)}} + +do_execsql_test 3.2 { + SELECT * FROM t4; +} {3 2 1} + +do_execsql_test 3.3 { INSERT INTO t4 VALUES(6, 5, 4); } {} + +do_execsql_test 3.4 { SELECT sql FROM sqlite_master WHERE type='trigger' } { +{CREATE TRIGGER ttt AFTER INSERT ON t4 WHEN new.abc<0 BEGIN + SELECT x, abc, z FROM t4; + DELETE FROM t4 WHERE abc=32; + UPDATE t4 SET x=abc+1, abc=0 WHERE abc=32; + INSERT INTO t4(x, abc, z) SELECT 4, 5, 6 WHERE 0; + END} +} + +#------------------------------------------------------------------------- +# +do_execsql_test 4.0 { + CREATE TABLE c1(a, b, FOREIGN KEY (a, b) REFERENCES p1(c, d)); + CREATE TABLE p1(c, d, PRIMARY KEY(c, d)); + PRAGMA foreign_keys = 1; + INSERT INTO p1 VALUES(1, 2); + INSERT INTO p1 VALUES(3, 4); +} + +do_execsql_test 4.1 { + ALTER TABLE p1 RENAME d TO "silly name"; + SELECT sql FROM sqlite_master WHERE name IN ('c1', 'p1'); +} { + {CREATE TABLE c1(a, b, FOREIGN KEY (a, b) REFERENCES p1(c, "silly name"))} + {CREATE TABLE p1(c, "silly name", PRIMARY KEY(c, "silly name"))} +} + +do_execsql_test 4.2 { INSERT INTO c1 VALUES(1, 2); } + +do_execsql_test 4.3 { + CREATE TABLE c2(a, b, FOREIGN KEY (a, b) REFERENCES p1); +} + +do_execsql_test 4.4 { + ALTER TABLE p1 RENAME "silly name" TO reasonable; + SELECT sql FROM sqlite_master WHERE name IN ('c1', 'c2', 'p1'); +} { + {CREATE TABLE c1(a, b, FOREIGN KEY (a, b) REFERENCES p1(c, "reasonable"))} + {CREATE TABLE p1(c, "reasonable", PRIMARY KEY(c, "reasonable"))} + {CREATE TABLE c2(a, b, FOREIGN KEY (a, b) REFERENCES p1)} +} + +#------------------------------------------------------------------------- + +do_execsql_test 5.0 { + CREATE TABLE t5(a, b, c); + CREATE INDEX t5a ON t5(a); + INSERT INTO t5 VALUES(1, 2, 3), (4, 5, 6); + ANALYZE; +} + +do_execsql_test 5.1 { + ALTER TABLE t5 RENAME b TO big; + SELECT big FROM t5; +} {2 5} + +do_catchsql_test 6.1 { + ALTER TABLE sqlite_stat1 RENAME tbl TO thetable; +} {1 {table sqlite_stat1 may not be altered}} + +#------------------------------------------------------------------------- +# +do_execsql_test 6.0 { + CREATE TABLE blob( + rid INTEGER PRIMARY KEY, + rcvid INTEGER, + size INTEGER, + uuid TEXT UNIQUE NOT NULL, + content BLOB, + CHECK( length(uuid)>=40 AND rid>0 ) + ); +} + +do_execsql_test 6.1 { + ALTER TABLE "blob" RENAME COLUMN "rid" TO "a1"; +} + +do_catchsql_test 6.2 { + ALTER TABLE "blob" RENAME COLUMN "a1" TO [where]; +} {0 {}} + +do_execsql_test 6.3 { + SELECT "where" FROM blob; +} {} + +#------------------------------------------------------------------------- +# Triggers. +# +db close +db2 close +reset_db +do_execsql_test 7.0 { + CREATE TABLE c(x); + INSERT INTO c VALUES(0); + CREATE TABLE t6("col a", "col b", "col c"); + CREATE TRIGGER zzz AFTER UPDATE OF "col a", "col c" ON t6 BEGIN + UPDATE c SET x=x+1; + END; +} + +do_execsql_test 7.1.1 { + INSERT INTO t6 VALUES(0, 0, 0); + UPDATE t6 SET "col c" = 1; + SELECT * FROM c; +} {1} + +do_execsql_test 7.1.2 { + ALTER TABLE t6 RENAME "col c" TO "col 3"; +} + +do_execsql_test 7.1.3 { + UPDATE t6 SET "col 3" = 0; + SELECT * FROM c; +} {2} + +#------------------------------------------------------------------------- +# Views. +# +reset_db +do_execsql_test 8.0 { + CREATE TABLE a1(x INTEGER, y TEXT, z BLOB, PRIMARY KEY(x)); + CREATE TABLE a2(a, b, c); + CREATE VIEW v1 AS SELECT x, y, z FROM a1; +} + +do_execsql_test 8.1 { + ALTER TABLE a1 RENAME y TO yyy; + SELECT sql FROM sqlite_master WHERE type='view'; +} {{CREATE VIEW v1 AS SELECT x, yyy, z FROM a1}} + +do_execsql_test 8.2.1 { + DROP VIEW v1; + CREATE VIEW v2 AS SELECT x, x+x, a, a+a FROM a1, a2; +} {} +do_execsql_test 8.2.2 { + ALTER TABLE a1 RENAME x TO xxx; +} +do_execsql_test 8.2.3 { + SELECT sql FROM sqlite_master WHERE type='view'; +} {{CREATE VIEW v2 AS SELECT xxx, xxx+xxx, a, a+a FROM a1, a2}} + +do_execsql_test 8.3.1 { + DROP TABLE a2; + DROP VIEW v2; + CREATE TABLE a2(a INTEGER PRIMARY KEY, b, c); + CREATE VIEW v2 AS SELECT xxx, xxx+xxx, a, a+a FROM a1, a2; +} {} +do_execsql_test 8.3.2 { + ALTER TABLE a1 RENAME xxx TO x; +} +do_execsql_test 8.3.3 { + SELECT sql FROM sqlite_master WHERE type='view'; +} {{CREATE VIEW v2 AS SELECT x, x+x, a, a+a FROM a1, a2}} + +do_execsql_test 8.4.0 { + CREATE TABLE b1(a, b, c); + CREATE TABLE b2(x, y, z); +} + +do_execsql_test 8.4.1 { + CREATE VIEW vvv AS SELECT c+c || coalesce(c, c) FROM b1, b2 WHERE x=c GROUP BY c HAVING c>0; + ALTER TABLE b1 RENAME c TO "a;b"; + SELECT sql FROM sqlite_master WHERE name='vvv'; +} {{CREATE VIEW vvv AS SELECT "a;b"+"a;b" || coalesce("a;b", "a;b") FROM b1, b2 WHERE x="a;b" GROUP BY "a;b" HAVING "a;b">0}} + +do_execsql_test 8.4.2 { + CREATE VIEW www AS SELECT b FROM b1 UNION ALL SELECT y FROM b2; + ALTER TABLE b1 RENAME b TO bbb; + SELECT sql FROM sqlite_master WHERE name='www'; +} {{CREATE VIEW www AS SELECT bbb FROM b1 UNION ALL SELECT y FROM b2}} + +db collate nocase {string compare} + +do_execsql_test 8.4.3 { + CREATE VIEW xxx AS SELECT a FROM b1 UNION SELECT x FROM b2 ORDER BY 1 COLLATE nocase; +} + +do_execsql_test 8.4.4 { + ALTER TABLE b2 RENAME x TO hello; + SELECT sql FROM sqlite_master WHERE name='xxx'; +} {{CREATE VIEW xxx AS SELECT a FROM b1 UNION SELECT hello FROM b2 ORDER BY 1 COLLATE nocase}} + +do_catchsql_test 8.4.5 { + CREATE VIEW zzz AS SELECT george, ringo FROM b1; + ALTER TABLE b1 RENAME a TO aaa; +} {1 {error in view zzz: no such column: george}} + +do_execsql_test 8.5 { + DROP VIEW zzz; + CREATE TABLE t5(a TEXT, b INT); + INSERT INTO t5(a,b) VALUES('aaa',7),('bbb',3),('ccc',4); + CREATE VIEW vt5(x) AS SELECT group_concat(a ORDER BY b) FROM t5; + SELECT x FROM vt5; +} {bbb,ccc,aaa} +do_execsql_test 8.5.1 { + ALTER TABLE t5 RENAME COLUMN b TO bbb; + SELECT sql FROM sqlite_schema WHERE name='vt5'; +} {{CREATE VIEW vt5(x) AS SELECT group_concat(a ORDER BY bbb) FROM t5}} +do_execsql_test 8.5.2 { + SELECT x FROM vt5; +} {bbb,ccc,aaa} + +#------------------------------------------------------------------------- +# More triggers. +# +proc do_rename_column_test {tn old new lSchema} { + for {set i 0} {$i < 2} {incr i} { + drop_all_tables_and_views db + + set lSorted [list] + foreach sql $lSchema { + execsql $sql + lappend lSorted [string trim $sql] + } + set lSorted [lsort $lSorted] + + do_execsql_test $tn.$i.1 { + SELECT sql FROM sqlite_master WHERE sql!='' ORDER BY 1 + } $lSorted + + if {$i==1} { + db close + sqlite3 db test.db + } + + do_execsql_test $tn.$i.2 "ALTER TABLE t1 RENAME $old TO $new" + + do_execsql_test $tn.$i.3 { + SELECT sql FROM sqlite_master ORDER BY 1 + } [string map [list $old $new] $lSorted] + } +} + +foreach {tn old new lSchema} { + 1 _x_ _xxx_ { + { CREATE TABLE t1(a, b, _x_) } + { CREATE TRIGGER AFTER INSERT ON t1 BEGIN + SELECT _x_ FROM t1; + END } + } + + 2 _x_ _xxx_ { + { CREATE TABLE t1(a, b, _x_) } + { CREATE TABLE t2(c, d, e) } + { CREATE TRIGGER ttt AFTER INSERT ON t2 BEGIN + SELECT _x_ FROM t1; + END } + } + + 3 _x_ _xxx_ { + { CREATE TABLE t1(a, b, _x_ INTEGER, PRIMARY KEY(_x_), CHECK(_x_>0)) } + { CREATE TABLE t2(c, d, e) } + { CREATE TRIGGER ttt AFTER UPDATE ON t1 BEGIN + INSERT INTO t2 VALUES(new.a, new.b, new._x_); + END } + } + + 4 _x_ _xxx_ { + { CREATE TABLE t1(a, b, _x_ INTEGER, PRIMARY KEY(_x_), CHECK(_x_>0)) } + { CREATE TRIGGER ttt AFTER UPDATE ON t1 BEGIN + INSERT INTO t1 VALUES(new.a, new.b, new._x_) + ON CONFLICT (_x_) WHERE _x_>10 DO UPDATE SET _x_ = _x_+1; + END } + } + + 4 _x_ _xxx_ { + { CREATE TABLE t1(a, b, _x_ INTEGER, PRIMARY KEY(_x_), CHECK(_x_>0)) } + { CREATE TRIGGER ttt AFTER UPDATE ON t1 BEGIN + INSERT INTO t1 VALUES(new.a, new.b, new._x_) + ON CONFLICT (_x_) WHERE _x_>10 DO NOTHING; + END } + } +} { + do_rename_column_test 9.$tn $old $new $lSchema +} + +#------------------------------------------------------------------------- +# Test that views can be edited even if there are missing collation +# sequences or user defined functions. +# +reset_db + +ifcapable vtab { + foreach {tn old new lSchema} { + 1 _x_ _xxx_ { + { CREATE TABLE t1(a, b, _x_) } + { CREATE VIEW s1 AS SELECT a, b, _x_ FROM t1 WHERE _x_='abc' COLLATE xyz } + } + + 2 _x_ _xxx_ { + { CREATE TABLE t1(a, b, _x_) } + { CREATE VIEW v1 AS SELECT a, b, _x_ FROM t1 WHERE scalar(_x_) } + } + + 3 _x_ _xxx_ { + { CREATE TABLE t1(a, b, _x_) } + { CREATE VIEW v1 AS SELECT a, b, _x_ FROM t1 WHERE _x_ = unicode(1, 2, 3) } + } + + 4 _x_ _xxx_ { + { CREATE TABLE t1(a, b, _x_) } + { CREATE VIRTUAL TABLE e1 USING echo(t1) } + } + } { + register_echo_module db + do_rename_column_test 10.$tn $old $new $lSchema + } + + #-------------------------------------------------------------------------- + # Test that if a view or trigger refers to a virtual table for which the + # module is not available, RENAME COLUMN cannot proceed. + # + reset_db + register_echo_module db + do_execsql_test 11.0 { + CREATE TABLE x1(a, b, c); + CREATE VIRTUAL TABLE e1 USING echo(x1); + } + db close + sqlite3 db test.db + + do_execsql_test 11.1 { + ALTER TABLE x1 RENAME b TO bbb; + SELECT sql FROM sqlite_master; + } { {CREATE TABLE x1(a, bbb, c)} {CREATE VIRTUAL TABLE e1 USING echo(x1)} } + + do_execsql_test 11.2 { + CREATE VIEW v1 AS SELECT e1.*, x1.c FROM e1, x1; + } + + do_catchsql_test 11.3 { + ALTER TABLE x1 RENAME c TO ccc; + } {1 {error in view v1: no such module: echo}} +} + +#------------------------------------------------------------------------- +# Test some error conditions: +# +# 1. Renaming a column of a system table, +# 2. Renaming a column of a VIEW, +# 3. Renaming a column of a virtual table. +# 4. Renaming a column that does not exist. +# 5. Renaming a column of a table that does not exist. +# +reset_db +do_execsql_test 12.1.1 { + CREATE TABLE t1(a, b); + CREATE INDEX t1a ON t1(a); + INSERT INTO t1 VALUES(1, 1), (2, 2), (3, 4); + ANALYZE; +} +do_catchsql_test 12.1.2 { + ALTER TABLE sqlite_stat1 RENAME idx TO theindex; +} {1 {table sqlite_stat1 may not be altered}} +do_execsql_test 12.1.3 { + SELECT sql FROM sqlite_master WHERE tbl_name = 'sqlite_stat1' +} {{CREATE TABLE sqlite_stat1(tbl,idx,stat)}} + +do_execsql_test 12.2.1 { + CREATE VIEW v1 AS SELECT * FROM t1; + CREATE VIEW v2(c, d) AS SELECT * FROM t1; +} +do_catchsql_test 12.2.2 { + ALTER TABLE v1 RENAME a TO z; +} {1 {cannot rename columns of view "v1"}} +do_catchsql_test 12.2.3 { + ALTER TABLE v2 RENAME c TO y; +} {1 {cannot rename columns of view "v2"}} + +ifcapable fts5 { + do_execsql_test 12.3.1 { + CREATE VIRTUAL TABLE ft USING fts5(a, b, c); + } + do_catchsql_test 12.3.2 { + ALTER TABLE ft RENAME a TO z; + } {1 {cannot rename columns of virtual table "ft"}} +} + +do_execsql_test 12.4.1 { + CREATE TABLE t2(x, y, z); +} +do_catchsql_test 12.4.2 { + ALTER TABLE t2 RENAME COLUMN a TO b; +} {1 {no such column: "a"}} + +do_catchsql_test 12.5.1 { + ALTER TABLE t3 RENAME COLUMN a TO b; +} {1 {no such table: t3}} + +#------------------------------------------------------------------------- +# Test the effect of some parse/resolve errors. +# +reset_db +do_execsql_test 13.1.1 { + CREATE TABLE x1(i INTEGER, t TEXT UNIQUE); + CREATE TRIGGER tr1 AFTER INSERT ON x1 BEGIN + SELECT * FROM nosuchtable; + END; +} + +do_catchsql_test 13.1.2 { + ALTER TABLE x1 RENAME COLUMN t TO ttt; +} {1 {error in trigger tr1: no such table: main.nosuchtable}} + +do_execsql_test 13.1.3 { + DROP TRIGGER tr1; + CREATE INDEX x1i ON x1(i); + SELECT sql FROM sqlite_master WHERE name='x1i'; +} {{CREATE INDEX x1i ON x1(i)}} + +sqlite3_db_config db DEFENSIVE 0 +do_execsql_test 13.1.4 { + PRAGMA writable_schema = ON; + UPDATE sqlite_master SET sql = 'CREATE INDEX x1i ON x1(j)' WHERE name='x1i'; + PRAGMA writable_schema = OFF; +} {} + +do_catchsql_test 13.1.5 { + ALTER TABLE x1 RENAME COLUMN t TO ttt; +} {1 {error in index x1i: no such column: j}} + +do_execsql_test 13.1.6 { + PRAGMA writable_schema = ON; + UPDATE sqlite_master SET sql = '' WHERE name='x1i'; + PRAGMA writable_schema = OFF; +} {} + +do_catchsql_test 13.1.7 { + ALTER TABLE x1 RENAME COLUMN t TO ttt; +} {1 {error in index x1i: }} + +do_execsql_test 13.1.8 { + PRAGMA writable_schema = ON; + DELETE FROM sqlite_master WHERE name = 'x1i'; + PRAGMA writable_schema = OFF; +} + +do_execsql_test 13.2.0 { + CREATE TABLE data(x UNIQUE, y, z); +} +foreach {tn trigger error} { + 1 { + CREATE TRIGGER tr1 AFTER INSERT ON x1 BEGIN + UPDATE data SET x=x+1 WHERE zzz=new.i; + END; + } {no such column: zzz} + + 2 { + CREATE TRIGGER tr1 AFTER INSERT ON x1 BEGIN + INSERT INTO data(x, y) VALUES(new.i, new.t, 1) + ON CONFLICT (x) DO UPDATE SET z=zz+1; + END; + } {no such column: zz} + + 3 { + CREATE TRIGGER tr1 AFTER INSERT ON x1 BEGIN + INSERT INTO x1(i, t) VALUES(new.i+1, new.t||'1') + ON CONFLICT (tttttt) DO UPDATE SET t=i+1; + END; + } {no such column: tttttt} + + 4 { + CREATE TRIGGER tr1 AFTER INSERT ON x1 BEGIN + INSERT INTO nosuchtable VALUES(new.i, new.t); + END; + } {no such table: main.nosuchtable} +} { + do_execsql_test 13.2.$tn.1 " + DROP TRIGGER IF EXISTS tr1; + $trigger + " + + do_catchsql_test 13.2.$tn.2 { + ALTER TABLE x1 RENAME COLUMN t TO ttt; + } "1 {error in trigger tr1: $error}" +} + +#------------------------------------------------------------------------- +# Passing invalid parameters directly to sqlite_rename_column(). +# +sqlite3_test_control SQLITE_TESTCTRL_INTERNAL_FUNCTIONS db +do_execsql_test 14.1 { + CREATE TABLE ddd(sql, type, object, db, tbl, icol, znew, bquote); + INSERT INTO ddd VALUES( + 'CREATE TABLE x1(i INTEGER, t TEXT)', + 'table', 'x1', 'main', 'x1', -1, 'zzz', 0 + ), ( + 'CREATE TABLE x1(i INTEGER, t TEXT)', + 'table', 'x1', 'main', 'x1', 2, 'zzz', 0 + ), ( + 'CREATE TABLE x1(i INTEGER, t TEXT)', + 'table', 'x1', 'main', 'notable', 0, 'zzz', 0 + ), ( + 'CREATE TABLE x1(i INTEGER, t TEXT)', + 'table', 'x1', 'main', 'ddd', -1, 'zzz', 0 + ); +} {} + +do_execsql_test 14.2 { + SELECT + sqlite_rename_column(sql, type, object, db, tbl, icol, znew, bquote, 0) + FROM ddd; +} {{} {} {} {}} +sqlite3_test_control SQLITE_TESTCTRL_INTERNAL_FUNCTIONS db + +# If the INTERNAL_FUNCTIONS test-control is disabled (which is the default) +# then the sqlite_rename_table() SQL function is not accessible to +# ordinary SQL. +# +do_catchsql_test 14.3 { + SELECT sqlite_rename_column(0,0,0,0,0,0,0,0,0); +} {1 {no such function: sqlite_rename_column}} + +#------------------------------------------------------------------------- +# +reset_db +do_execsql_test 15.0 { + CREATE TABLE xxx(a, b, c); + SELECT a AS d FROM xxx WHERE d=0; +} + +do_execsql_test 15.1 { + CREATE VIEW vvv AS SELECT a AS d FROM xxx WHERE d=0; + ALTER TABLE xxx RENAME a TO xyz; +} + +do_execsql_test 15.2 { + SELECT sql FROM sqlite_master WHERE type='view'; +} {{CREATE VIEW vvv AS SELECT xyz AS d FROM xxx WHERE d=0}} + +#------------------------------------------------------------------------- +# +do_execsql_test 16.1.0 { + CREATE TABLE t1(a,b,c); + CREATE TABLE t2(d,e,f); + INSERT INTO t1 VALUES(1,2,3); + INSERT INTO t2 VALUES(4,5,6); + CREATE VIEW v4 AS SELECT a, d FROM t1, t2; + SELECT * FROM v4; +} {1 4} + +do_catchsql_test 16.1.1 { + ALTER TABLE t2 RENAME d TO a; +} {1 {error in view v4 after rename: ambiguous column name: a}} + +do_execsql_test 16.1.2 { + SELECT * FROM v4; +} {1 4} + +do_execsql_test 16.1.3 { + CREATE UNIQUE INDEX t2d ON t2(d); + CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN + INSERT INTO t2 VALUES(new.a, new.b, new.c) + ON CONFLICT(d) DO UPDATE SET f = excluded.f; + END; +} + +do_execsql_test 16.1.4 { + INSERT INTO t1 VALUES(4, 8, 456); + SELECT * FROM t2; +} {4 5 456} + +do_execsql_test 16.1.5 { + ALTER TABLE t2 RENAME COLUMN f TO "big f"; + INSERT INTO t1 VALUES(4, 0, 20456); + SELECT * FROM t2; +} {4 5 20456} + +do_execsql_test 16.1.6 { + ALTER TABLE t1 RENAME COLUMN c TO "big c"; + INSERT INTO t1 VALUES(4, 0, 0); + SELECT * FROM t2; +} {4 5 0} + +do_execsql_test 16.2.1 { + CREATE VIEW temp.v5 AS SELECT "big c" FROM t1; + SELECT * FROM v5; +} {3 456 20456 0} + +do_execsql_test 16.2.2 { + ALTER TABLE t1 RENAME COLUMN "big c" TO reallybigc; +} {} + +do_execsql_test 16.2.3 { + SELECT * FROM v5; +} {3 456 20456 0} + +#------------------------------------------------------------------------- +# +do_execsql_test 17.0 { + CREATE TABLE u7(x, y, z); + CREATE TRIGGER u7t AFTER INSERT ON u7 BEGIN + INSERT INTO u8 VALUES(new.x, new.y, new.z); + END; +} {} +do_catchsql_test 17.1 { + ALTER TABLE u7 RENAME x TO xxx; +} {1 {error in trigger u7t: no such table: main.u8}} + +do_execsql_test 17.2 { + CREATE TEMP TABLE uu7(x, y, z); + CREATE TRIGGER uu7t AFTER INSERT ON uu7 BEGIN + INSERT INTO u8 VALUES(new.x, new.y, new.z); + END; +} {} +do_catchsql_test 17.3 { + ALTER TABLE uu7 RENAME x TO xxx; +} {1 {error in trigger uu7t: no such table: u8}} + +reset_db +forcedelete test.db2 +do_execsql_test 18.0 { + ATTACH 'test.db2' AS aux; + CREATE TABLE t1(a); + CREATE TABLE aux.log(v); + CREATE TEMP TRIGGER tr1 AFTER INSERT ON t1 BEGIN + INSERT INTO log VALUES(new.a); + END; + INSERT INTO t1 VALUES(111); + SELECT v FROM log; +} {111} + +do_execsql_test 18.1 { + ALTER TABLE t1 RENAME a TO b; +} + +reset_db +do_execsql_test 19.0 { + CREATE TABLE t1(a, b); + CREATE TABLE t2(c, d); + CREATE VIEW v2(e) AS SELECT coalesce(t2.c,t1.a) FROM t1, t2 WHERE t1.b=t2.d; +} + +do_execsql_test 19.1 { + ALTER TABLE t1 RENAME a TO f; + SELECT sql FROM sqlite_master WHERE name = 'v2'; +} { + {CREATE VIEW v2(e) AS SELECT coalesce(t2.c,t1.f) FROM t1, t2 WHERE t1.b=t2.d} +} + +# 2019-01-08: https://sqlite.org/src/tktview/bc8d94f0fbd633fd9a051e3 +# +# ALTER TABLE RENAME COLUMN does not work for tables that have redundant +# UNIQUE constraints. +# +sqlite3 db :memory: +do_execsql_test 20.100 { + CREATE TABLE t1(aaa,b,c,UNIQUE(aaA),PRIMARY KEY(aAa),UNIQUE(aAA)); + ALTER TABLE t1 RENAME aaa TO bbb; + SELECT sql FROM sqlite_master WHERE name='t1'; +} {{CREATE TABLE t1(bbb,b,c,UNIQUE(bbb),PRIMARY KEY(bbb),UNIQUE(bbb))}} +do_execsql_test 20.105 { + DROP TABLE t1; + CREATE TABLE t1(aaa,b,c,UNIQUE(aaA),PRIMARY KEY(aAa),UNIQUE(aAA))WITHOUT ROWID; + ALTER TABLE t1 RENAME aaa TO bbb; + SELECT sql FROM sqlite_master WHERE name='t1'; +} {{CREATE TABLE t1(bbb,b,c,UNIQUE(bbb),PRIMARY KEY(bbb),UNIQUE(bbb))WITHOUT ROWID}} +do_execsql_test 20.110 { + DROP TABLE t1; + CREATE TABLE t1(aa UNIQUE,bb UNIQUE,cc UNIQUE,UNIQUE(aA),PRIMARY KEY(bB),UNIQUE(cC)); + ALTER TABLE t1 RENAME aa TO xx; + ALTER TABLE t1 RENAME bb TO yy; + ALTER TABLE t1 RENAME cc TO zz; + SELECT sql FROM sqlite_master WHERE name='t1'; +} {{CREATE TABLE t1(xx UNIQUE,yy UNIQUE,zz UNIQUE,UNIQUE(xx),PRIMARY KEY(yy),UNIQUE(zz))}} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 21.0 { + CREATE TABLE t1(a, b, c NOT NULL); + CREATE TRIGGER tr1 AFTER INSERT ON t1 WHEN new.c IS NOT NULL BEGIN + SELECT c NOT NULL FROM t1; + END; +} + +do_execsql_test 21.1 { + ALTER TABLE t1 RENAME c TO d; +} + +do_execsql_test 21.2 { + SELECT sql FROM sqlite_schema WHERE name IS 'tr1' +} {{CREATE TRIGGER tr1 AFTER INSERT ON t1 WHEN new.d IS NOT NULL BEGIN + SELECT d NOT NULL FROM t1; + END} +} + +#------------------------------------------------------------------------- +# +reset_db +do_execsql_test 22.0 { + CREATE TABLE t1(a, b); + CREATE TABLE t2(c, othername, extra AS (c + 1)); + ALTER TABLE t1 RENAME a to othername; + SELECT sql FROM sqlite_schema; +} { + {CREATE TABLE t1(othername, b)} + {CREATE TABLE t2(c, othername, extra AS (c + 1))} +} + +#------------------------------------------------------------------------- +# +reset_db +sqlite3_db_config db SQLITE_DBCONFIG_DQS_DDL 1 +sqlite3_db_config db SQLITE_DBCONFIG_DQS_DML 1 +do_execsql_test 22.0 { + CREATE TABLE t1(a, b); + CREATE INDEX x1 on t1("c"=b); + INSERT INTO t1 VALUES('a', 'a'); + INSERT INTO t1 VALUES('b', 'b'); + INSERT INTO t1 VALUES('c', 'c'); + ALTER TABLE t1 RENAME COLUMN a TO "c"; + PRAGMA integrity_check; +} {ok} + +reset_db +do_execsql_test 23.0 { + CREATE TABLE t1('a'"b",c); + CREATE INDEX i1 ON t1('a'); + INSERT INTO t1 VALUES(1,2), (3,4); + ALTER TABLE t1 RENAME COLUMN a TO x; + PRAGMA integrity_check; + SELECT sql FROM sqlite_schema WHERE name='t1'; + +} {ok {CREATE TABLE t1("x" "b",c)}} + +# 2022-02-04 +# Do not complain about syntax errors in the schema if +# in PRAGMA writable_schema=ON mode. +# +reset_db +do_execsql_test 23.0 { + CREATE TABLE t1(a INT, b REAL, c TEXT, d BLOB, e ANY); + CREATE INDEX t1abx ON t1(a, b, a+b) WHERE c IS NOT NULL; + CREATE VIEW t2 AS SELECT a+10, b*5.0, xyz FROM t1; -- unknown column "xyz" + CREATE TABLE schema_copy(name TEXT, sql TEXT); + INSERT INTO schema_copy(name,sql) SELECT name, sql FROM sqlite_schema WHERE sql IS NOT NULL; +} {} +do_catchsql_test 23.1 { + ALTER TABLE t1 RENAME COLUMN e TO eeee; +} {1 {error in view t2: no such column: xyz}} +do_execsql_test 23.2 { + SELECT name, sql FROM sqlite_master + EXCEPT SELECT name, sql FROM schema_copy; +} {} +do_execsql_test 23.3 { + BEGIN; + PRAGMA writable_schema=ON; + ALTER TABLE t1 RENAME COLUMN e TO eeee; + PRAGMA writable_schema=OFF; + SELECT name FROM sqlite_master + WHERE (name, sql) NOT IN (SELECT name, sql FROM schema_copy); + ROLLBACK; +} {t1} +do_execsql_test 23.10 { + DROP VIEW t2; + CREATE TRIGGER r3 AFTER INSERT ON t1 BEGIN + INSERT INTO t3(x,y) VALUES(new.a, new.b); + INSERT INTO t4(p) VALUES(new.c); -- no such table "t4" + END; + DELETE FROM schema_copy; + INSERT INTO schema_copy(name,sql) SELECT name, sql FROM sqlite_schema WHERE sql IS NOT NULL; +} {} +do_catchsql_test 23.11 { + ALTER TABLE t1 RENAME COLUMN e TO eeee; +} {1 {error in trigger r3: no such table: main.t3}} +do_execsql_test 23.12 { + SELECT name, sql FROM sqlite_master + EXCEPT SELECT name, sql FROM schema_copy; +} {} +do_execsql_test 23.13 { + BEGIN; + PRAGMA writable_schema=ON; + ALTER TABLE t1 RENAME COLUMN e TO eeee; + PRAGMA writable_schema=OFF; + SELECT name FROM sqlite_master + WHERE (name, sql) NOT IN (SELECT name, sql FROM schema_copy); + ROLLBACK; +} {t1} +do_execsql_test 23.20 { + CREATE TABLE t4(id INTEGER PRIMARY KEY, c1 INT, c2 INT); + CREATE VIEW t4v1 AS SELECT id, c1, c99 FROM t4; + DELETE FROM schema_copy; + INSERT INTO schema_copy SELECT name, sql FROM sqlite_schema; + BEGIN; + PRAGMA writable_schema=ON; + ALTER TABLE t4 RENAME to t4new; + SELECT name FROM sqlite_schema WHERE (name,sql) NOT IN (SELECT * FROM schema_copy); + ROLLBACK; +} {t4new} + +finish_test diff --git a/test/altercorrupt.test b/test/altercorrupt.test new file mode 100644 index 0000000000..f24cb309a3 --- /dev/null +++ b/test/altercorrupt.test @@ -0,0 +1,181 @@ +# 2019-01-11 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix altercorrupt + +# If SQLITE_OMIT_ALTERTABLE is defined, omit this file. +ifcapable !altertable { + finish_test + return +} + +database_may_be_corrupt + +#-------------------------------------------------------------------------- +reset_db +do_test 1.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 24576 pagesize 4096 filename crash-685346d89b5e5f.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 06 .....@ ........ +| 32: 00 00 63 00 00 05 f0 00 00 00 00 04 10 00 00 04 ..c............. +| 48: 00 00 00 00 00 00 0f f0 00 00 00 01 00 00 00 00 ................ +| 64: 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 96: 00 00 00 00 0d 0f f8 00 05 0e cf 00 0f 79 0f d3 .............y.. +| 112: 0f 2e 0e f3 0e cf 00 00 00 00 00 00 00 00 00 00 ................ +| 3776: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 22 ................ +| 3792: 05 06 17 11 11 01 31 74 61 62 6c 65 74 34 74 34 ......1tablet4t4 +| 3808: 06 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 34 .CREATE TABLE t4 +| 3824: 28 7a 29 39 04 06 17 11 11 01 5f 74 61 62 6c 65 (z)9......_table +| 3840: 74 33 74 33 05 43 52 45 41 54 45 20 54 41 42 4c t3t3.CREATE TABL +| 3856: 45 20 74 33 28 78 20 49 4e 54 45 47 45 52 20 50 E t3(x INTEGER P +| 3872: 52 49 4d 41 52 59 20 4b 45 59 2c 20 79 29 49 03 RIMARY KEY, y)I. +| 3888: 06 17 11 11 01 7f 74 61 62 6c 65 74 32 74 32 04 ......tablet2t2. +| 3904: 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 32 28 CREATE TABLE t2( +| 3920: 61 2c 62 2c 63 20 50 52 49 4d 41 52 59 20 4b 45 a,b,c PRIMARY KE +| 3936: 59 2c 20 64 2c 20 65 2c 20 66 29 20 57 49 54 48 Y, d, e, f) WITH +| 3952: 4f 55 54 20 52 4f 57 49 44 58 03 07 17 11 11 01 OUT ROWIDX...... +| 3968: 81 1b 74 61 62 6c 65 74 31 74 31 02 43 52 45 41 ..tablet1t1.CREA +| 3984: 54 45 20 54 41 42 4c 45 20 74 31 28 61 2c 62 2c TE TABLE t1(a,b, +| 4000: 63 20 41 53 20 28 2d 62 29 20 56 49 52 54 55 41 c AS (-b) VIRTUA +| 4016: 4c 2c 64 20 43 48 45 43 4b 28 64 3e 35 29 2c 65 L,d CHECK(d>5),e +| 4032: 20 55 4e 49 51 55 45 2c 20 66 20 41 53 20 28 2b UNIQUE, f AS (+ +| 4048: 62 29 29 23 02 06 17 37 11 01 00 69 6e 64 65 78 b))#...7...index +| 4064: 73 71 6c 69 74 65 5f 61 75 74 6f 69 6e 64 65 78 sqlite_autoindex +| 4080: 5f 74 31 5f 31 74 31 03 00 00 00 08 00 00 00 00 _t1_1t1......... +| page 2 offset 4096 +| 0: 0d 00 00 00 0a 0f 93 00 0f f6 0f eb 0f e0 0f d5 ................ +| 16: 0f ca 0f 8f 0f b4 0f a9 0f 9e 0f 93 00 00 00 00 ................ +| 3984: 00 00 00 09 0a 05 01 01 01 01 0a 64 6e 14 09 09 ...........dn... +| 4000: 05 01 01 01 01 09 5a 6d 12 09 08 05 01 01 01 01 ......Zm........ +| 4016: 08 50 6c 10 09 07 05 01 01 01 01 07 46 6b 0e 09 .Pl.........Fk.. +| 4032: 06 05 01 01 01 01 06 3c 6a 0c 09 05 05 01 01 01 .......<j....... +| 4048: 01 05 32 69 0a 09 04 05 01 01 01 01 04 28 68 08 ..2i.........(h. +| 4064: 09 03 05 01 01 01 01 03 1e 67 06 09 02 05 01 01 .........g...... +| 4080: 01 01 02 14 66 04 08 01 05 09 01 01 01 0a 65 02 ....f.........e. +| page 3 offset 8192 +| 0: 0a 00 00 00 0a 0f c5 00 0f fb 0f f5 0f ef 0f e9 ................ +| 16: 0f e3 0f dd 0f d7 0f d1 0f cb 0f c5 00 00 00 00 ................ +| 4032: 00 00 00 00 00 05 03 01 01 14 0a 05 03 01 01 12 ................ +| 4048: 09 05 03 01 01 10 08 05 03 01 01 0e 07 05 03 01 ................ +| 4064: 01 0c 06 05 03 01 01 0a 05 05 03 01 01 08 04 05 ................ +| 4080: 03 01 01 06 03 05 03 01 01 04 02 04 03 01 09 02 ................ +| page 4 offset 12288 +| 0: 0a 00 00 00 0a 0f 75 00 0f 75 0f 83 0f 91 0f 9f ......u..u...... +| 16: 0f ad 0f bb 0f 00 00 00 00 00 00 00 00 00 00 00 ................ +| 3952: 00 00 00 00 00 0d 07 01 01 01 01 01 01 9c 0a 64 ...............d +| 3968: 6e 14 64 0d 07 02 01 01 01 01 01 a6 09 5a 6d 12 n.d..........Zm. +| 3984: 5a 0d 07 01 01 01 01 01 01 b0 08 50 6c 10 50 0d Z..........Pl.P. +| 4000: 07 01 01 01 01 01 01 ba 07 46 6b 0e 46 0d 07 01 .........Fk.F... +| 4016: 01 01 01 01 01 c4 06 3c 6a 0c 3c 0d 07 01 01 01 .......<j.<..... +| 4032: 01 01 01 ce 05 32 69 0a 32 0d 07 01 01 01 01 01 .....2i.2....... +| 4048: 01 d8 04 28 68 08 28 0d 07 01 01 01 01 01 01 e2 ...(h.(......... +| 4064: 03 1e 67 06 1e 0d 07 01 01 01 01 01 01 ec 02 14 ..g............. +| 4080: 66 04 14 0c 07 01 09 01 01 01 01 f6 0a 65 02 0a f............e.. +| page 5 offset 16384 +| 0: 0d 00 00 00 03 0f e9 00 0f e9 0f fb 0f f6 00 00 ................ +| 16: 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 03 ff ff ff ff ff ff ................ +| 4080: ff ff 9c 03 00 00 03 64 03 00 00 03 01 03 00 00 .......d........ +| page 6 offset 20480 +| 0: 0d 00 00 00 00 10 00 00 00 00 00 00 00 00 00 00 ................ +| end crash-685346d89b5e5f.db +}]} {} + +do_catchsql_test 1.1 { + ALTER TABLE t2 DROP COLUMN e; + ALTER TABLE t1 DROP COLUMN f; +} {1 {database disk image is malformed}} + + +#-------------------------------------------------------------------------- +reset_db +do_test 2.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 24576 pagesize 4096 filename crash-0572db8f391431.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 06 .....@ ........ +| 32: 00 00 63 00 10 05 f0 00 00 00 00 04 10 00 00 04 ..c............. +| 48: 00 00 00 00 00 00 0f f0 00 00 00 00 00 00 00 00 ................ +| 96: 00 00 00 00 0d 0f f8 00 05 0e cf 00 0f 79 0f d3 .............y.. +| 112: 0f 2e 0e f3 0e cf 00 00 00 00 00 00 00 00 00 00 ................ +| 3776: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 22 ................ +| 3792: 05 06 17 11 11 01 31 74 61 62 6c 65 74 34 74 34 ......1tablet4t4 +| 3808: 06 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 34 .CREATE TABLE t4 +| 3824: 28 7a 29 39 04 06 17 11 11 01 5f 74 61 62 6c 65 (z)9......_table +| 3840: 74 33 74 33 05 43 52 45 41 54 45 20 54 41 42 4c t3t3.CREATE TABL +| 3856: 45 20 74 33 28 78 20 49 4e 54 55 47 45 52 20 50 E t3(x INTUGER P +| 3872: 52 49 4d 41 52 59 20 4b 45 59 2c 20 79 29 49 03 RIMARY KEY, y)I. +| 3888: 06 17 11 11 01 7f 74 61 62 6c 65 74 32 74 32 04 ......tablet2t2. +| 3904: 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 32 28 CREATE TABLE t2( +| 3920: 61 2c 62 2c 63 20 50 52 49 4d 41 52 59 20 4b 45 a,b,c PRIMARY KE +| 3936: 59 2c 20 64 2c 20 65 2c 20 66 29 20 57 49 54 48 Y, d, e, f) WITH +| 3952: 4f 55 54 20 52 4f 57 49 44 58 05 07 17 11 11 01 OUT ROWIDX...... +| 3968: 81 1b 74 61 62 6c 65 74 31 74 31 02 43 52 45 41 ..tablet1t1.CREA +| 3984: 54 45 20 54 41 42 4c 45 20 74 31 28 61 2c 62 2c TE TABLE t1(a,b, +| 4000: 63 20 41 53 20 28 2d 62 29 20 56 49 52 54 55 41 c AS (-b) VIRTUA +| 4016: 4c 2c 64 20 43 48 45 43 4b 28 64 3e 35 29 2c 65 L,d CHECK(d>5),e +| 4032: 20 55 4e 49 51 55 45 2c 20 66 20 41 53 20 28 2b UNIQUE, f AS (+ +| 4048: 62 29 29 23 02 06 17 37 11 01 00 69 6e 64 65 78 b))#...7...index +| 4064: 73 71 6c 69 74 65 5f 61 75 74 6f 69 6e 64 65 78 sqlite_autoindex +| 4080: 5f 74 31 5f 31 84 31 03 01 00 00 08 00 00 00 00 _t1_1.1......... +| page 2 offset 4096 +| 0: 0d 00 00 00 0a 0f 93 00 0f f6 0f eb 0f e0 0f d5 ................ +| 16: 0f ca 0f 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 3984: 00 00 00 09 0a 05 01 01 01 01 0a 64 6e 14 09 09 ...........dn... +| 4000: 05 01 01 01 01 09 5a 6d 12 09 08 05 01 00 f1 01 ......Zm........ +| 4016: 08 50 6c 10 09 07 05 01 01 01 01 07 46 6b 0e 09 .Pl.........Fk.. +| 4032: 06 05 01 00 f1 01 06 3c 6a 0c 09 05 05 01 01 01 .......<j....... +| 4048: 01 05 32 69 0a 09 04 05 01 01 01 01 04 28 68 08 ..2i.........(h. +| 4064: 57 03 05 01 01 01 01 03 1e 67 06 09 02 05 01 01 W........g...... +| 4080: 01 01 02 14 66 04 08 01 05 09 01 01 01 0a 65 02 ....f.........e. +| page 3 offset 8192 +| 0: 09 ff ff ff fa 0f c5 00 0f fb 0f f5 0f ef 0f e9 ................ +| 16: 0f e3 0f dd 0f d7 00 00 00 00 00 00 00 00 00 00 ................ +| 4032: 00 00 00 00 00 05 03 01 01 14 0a 05 03 01 01 12 ................ +| 4048: 09 05 03 01 01 10 08 05 03 01 01 0e 07 05 03 01 ................ +| 4064: 01 0c 06 05 03 01 01 0a 05 05 03 01 01 08 04 05 ................ +| 4080: 03 01 01 06 03 05 03 01 01 04 02 04 03 01 09 02 ................ +| page 4 offset 12288 +| 0: 0a 00 00 00 0a 0f 75 00 0f 75 0f 83 0f 91 0f 9f ......u..u...... +| 16: 0f ad 0f bb 0f 00 00 00 00 00 01 00 00 00 00 00 ................ +| 3952: 00 00 00 00 00 0d 07 01 01 01 01 01 01 9c 0a 64 ...............d +| 3968: 6e 14 64 0d 07 02 01 01 01 01 01 a6 09 5a 6d 12 n.d..........Zm. +| 3984: 5a 0d 07 01 01 01 01 d4 01 b0 08 50 6c 10 50 0d Z..........Pl.P. +| 4000: 07 01 01 01 01 01 01 ba 07 46 6b 0e 46 0d 07 00 .........Fk.F... +| 4016: 01 01 01 01 01 c4 06 3c 6a 0c 3c 0d 07 01 01 01 .......<j.<..... +| 4032: 01 01 01 ce 05 32 69 0a 32 0d 07 01 01 01 01 01 .....2i.2....... +| 4048: 01 d8 04 28 68 08 28 0d 07 01 01 01 01 01 01 e2 ...(h.(......... +| 4064: 03 1e 67 06 1e 0d 07 01 01 01 01 01 01 ec 02 14 ..g............. +| 4080: 66 04 14 0c 07 01 09 01 01 00 f1 f6 0a 65 02 0a f............e.. +| page 5 offset 16384 +| 0: 0d 00 00 00 03 0f e9 00 0f e9 0f fb 0f f6 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 03 ff ff ff ff ff ff ................ +| 4080: ff ff 9c 03 00 00 03 64 03 00 01 03 01 03 00 00 .......d........ +| page 6 offset 20480 +| 0: 0d 00 10 00 00 10 01 00 00 00 00 00 00 00 00 00 ................ +| end crash-0572db8f391431.db +}]} {} + +do_catchsql_test 2.1 { + ALTER TABLE t1 DROP COLUMN a; +} {1 {database disk image is malformed}} + +finish_test diff --git a/test/alterdropcol.test b/test/alterdropcol.test new file mode 100644 index 0000000000..8674e981a3 --- /dev/null +++ b/test/alterdropcol.test @@ -0,0 +1,339 @@ +# 2021 February 16 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix alterdropcol + +# If SQLITE_OMIT_ALTERTABLE is defined, omit this file. +ifcapable !altertable { + finish_test + return +} + +do_execsql_test 1.0 { + CREATE TABLE t1(a, b, c); + CREATE VIEW v1 AS SELECT * FROM t1; + + CREATE TABLE t2(x INTEGER PRIMARY KEY, y, z UNIQUE); + CREATE INDEX t2y ON t2(y); + + CREATE TABLE t3(q, r, s); + CREATE INDEX t3rs ON t3(r+s); +} + +do_catchsql_test 1.1 { + ALTER TABLE nosuch DROP COLUMN z; +} {1 {no such table: nosuch}} + +do_catchsql_test 1.2 { + ALTER TABLE v1 DROP COLUMN c; +} {1 {cannot drop column from view "v1"}} + +ifcapable fts5 { + do_execsql_test 1.3.1 { + CREATE VIRTUAL TABLE ft1 USING fts5(one, two); + } + do_catchsql_test 1.3.2 { + ALTER TABLE ft1 DROP COLUMN two; + } {1 {cannot drop column from virtual table "ft1"}} +} + +do_catchsql_test 1.4 { + ALTER TABLE sqlite_schema DROP COLUMN sql; +} {1 {table sqlite_master may not be altered}} + +do_catchsql_test 1.5 { + ALTER TABLE t1 DROP COLUMN d; +} {1 {no such column: "d"}} + +do_execsql_test 1.6.1 { + ALTER TABLE t1 DROP COLUMN b; +} +do_execsql_test 1.6.2 { + SELECT sql FROM sqlite_schema WHERE name = 't1' +} {{CREATE TABLE t1(a, c)}} + +do_execsql_test 1.7.1 { + ALTER TABLE t1 DROP COLUMN c; +} +do_execsql_test 1.7.2 { + SELECT sql FROM sqlite_schema WHERE name = 't1' +} {{CREATE TABLE t1(a)}} + +do_catchsql_test 1.7.3 { + ALTER TABLE t1 DROP COLUMN a; +} {1 {cannot drop column "a": no other columns exist}} + + +do_catchsql_test 1.8 { + ALTER TABLE t2 DROP COLUMN z +} {1 {cannot drop UNIQUE column: "z"}} + +do_catchsql_test 1.9 { + ALTER TABLE t2 DROP COLUMN x +} {1 {cannot drop PRIMARY KEY column: "x"}} + +do_catchsql_test 1.10 { + ALTER TABLE t2 DROP COLUMN y +} {1 {error in index t2y after drop column: no such column: y}} + +do_catchsql_test 1.11 { + ALTER TABLE t3 DROP COLUMN s +} {1 {error in index t3rs after drop column: no such column: s}} + +#------------------------------------------------------------------------- + +foreach {tn wo} { + 1 {} + 2 {WITHOUT ROWID} +} { eval [string map [list %TN% $tn %WO% $wo] { + + reset_db + do_execsql_test 2.%TN%.0 { + CREATE TABLE t1(x, y INTEGER PRIMARY KEY, z) %WO% ; + INSERT INTO t1 VALUES(1, 2, 3); + INSERT INTO t1 VALUES(4, 5, 6); + INSERT INTO t1 VALUES(7, 8, 9); + } + + do_execsql_test 2.%TN%.1 { + ALTER TABLE t1 DROP COLUMN x; + SELECT * FROM t1; + } { + 2 3 5 6 8 9 + } + do_execsql_test 2.%TN%.2 { + ALTER TABLE t1 DROP COLUMN z; + SELECT * FROM t1; + } { + 2 5 8 + } +}]} + +#------------------------------------------------------------------------- +reset_db + +do_execsql_test 3.0 { + CREATE TABLE t12(a, b, c, CHECK(c>10)); + CREATE TABLE t13(a, b, c CHECK(c>10)); +} +do_catchsql_test 3.1 { + ALTER TABLE t12 DROP COLUMN c; +} {1 {error in table t12 after drop column: no such column: c}} + +do_catchsql_test 3.2 { + ALTER TABLE t13 DROP COLUMN c; +} {0 {}} + +#------------------------------------------------------------------------- +# Test that generated columns can be dropped. And that other columns from +# tables that contain generated columns can be dropped. +# +foreach {tn wo vs} { + 1 "" "" + 2 "" VIRTUAL + 3 "" STORED + 4 "WITHOUT ROWID" STORED + 5 "WITHOUT ROWID" VIRTUAL +} { + reset_db + + do_execsql_test 4.$tn.0 " + CREATE TABLE 'my table'(a, b PRIMARY KEY, c AS (a+b) $vs, d) $wo + " + do_execsql_test 4.$tn.1 { + INSERT INTO "my table"(a, b, d) VALUES(1, 2, 'hello'); + INSERT INTO "my table"(a, b, d) VALUES(3, 4, 'world'); + + SELECT * FROM "my table" + } { + 1 2 3 hello + 3 4 7 world + } + + do_execsql_test 4.$tn.2 { + ALTER TABLE "my table" DROP COLUMN c; + } + do_execsql_test 4.$tn.3 { + SELECT * FROM "my table" + } { + 1 2 hello + 3 4 world + } + + do_execsql_test 4.$tn.4 " + CREATE TABLE x1(a, b, c PRIMARY KEY, d AS (b+c) $vs, e) $wo + " + do_execsql_test 4.$tn.5 { + INSERT INTO x1(a, b, c, e) VALUES(1, 2, 3, 4); + INSERT INTO x1(a, b, c, e) VALUES(5, 6, 7, 8); + INSERT INTO x1(a, b, c, e) VALUES(9, 10, 11, 12); + SELECT * FROM x1; + } { + 1 2 3 5 4 + 5 6 7 13 8 + 9 10 11 21 12 + } + + do_execsql_test 4.$tn.6 { + ALTER TABLE x1 DROP COLUMN a + } + do_execsql_test 4.$tn.7 { + SELECT * FROM x1 + } { + 2 3 5 4 + 6 7 13 8 + 10 11 21 12 + } + do_execsql_test 4.$tn.8 { + ALTER TABLE x1 DROP COLUMN e + } + do_execsql_test 4.$tn.9 { + SELECT * FROM x1 + } { + 2 3 5 + 6 7 13 + 10 11 21 + } +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 5.0 { + CREATE TABLE p1(a PRIMARY KEY, b UNIQUE); + CREATE TABLE c1(x, y, z REFERENCES p1(c)); + CREATE TABLE c2(x, y, z, w REFERENCES p1(b)); +} +do_execsql_test 5.1 { + ALTER TABLE c1 DROP COLUMN z; + ALTER TABLE c2 DROP COLUMN z; + SELECT sql FROM sqlite_schema WHERE name IN ('c1', 'c2'); +} { + {CREATE TABLE c1(x, y)} + {CREATE TABLE c2(x, y, w REFERENCES p1(b))} +} + +do_execsql_test 5.2.1 { + CREATE VIEW v1 AS SELECT d, e FROM p1 +} +do_catchsql_test 5.2.2 { + ALTER TABLE c1 DROP COLUMN x +} {1 {error in view v1: no such column: d}} +do_execsql_test 5.3.1 { + DROP VIEW v1; + CREATE VIEW v1 AS SELECT x, y FROM c1; +} +do_catchsql_test 5.3.2 { + ALTER TABLE c1 DROP COLUMN x +} {1 {error in view v1 after drop column: no such column: x}} + +do_execsql_test 5.4.1 { + CREATE TRIGGER tr AFTER INSERT ON c1 BEGIN + INSERT INTO p1 VALUES(new.y, new.xyz); + END; +} +do_catchsql_test 5.4.2 { + ALTER TABLE c1 DROP COLUMN y +} {1 {error in trigger tr: no such column: new.xyz}} +do_execsql_test 5.5.1 { + DROP TRIGGER tr; + CREATE TRIGGER tr AFTER INSERT ON c1 BEGIN + INSERT INTO p1 VALUES(new.y, new.z); + END; +} +do_catchsql_test 5.5.2 { + ALTER TABLE c1 DROP COLUMN y +} {1 {error in trigger tr: no such column: new.z}} + +# 2021-03-06 dbsqlfuzz crash-419aa525df93db6e463772c686ac6da27b46da9e +reset_db +do_catchsql_test 6.0 { + CREATE TABLE t1(a,b,c); + CREATE TABLE t2(x,y,z); + PRAGMA writable_schema=ON; + UPDATE sqlite_schema SET sql='CREATE INDEX t1b ON t1(b)' WHERE name='t2'; + PRAGMA writable_schema=OFF; + ALTER TABLE t2 DROP COLUMN z; +} {1 {database disk image is malformed}} +reset_db +do_catchsql_test 6.1 { + CREATE TABLE t1(a,b,c); + CREATE TABLE t2(x,y,z); + PRAGMA writable_schema=ON; + UPDATE sqlite_schema SET sql='CREATE VIEW t2(x,y,z) AS SELECT b,a,c FROM t1' + WHERE name='t2'; + PRAGMA writable_schema=OFF; + ALTER TABLE t2 DROP COLUMN z; +} {1 {database disk image is malformed}} + +# 2021-04-06 dbsqlfuzz crash-331c5c29bb76257b198f1318eef3288f9624c8ce +reset_db +do_execsql_test 7.0 { + CREATE TABLE t1(a, b, c, PRIMARY KEY(a COLLATE nocase, a)) WITHOUT ROWID; + INSERT INTO t1 VALUES(1, 2, 3); + INSERT INTO t1 VALUES(4, 5, 6); +} +do_execsql_test 7.1 { + ALTER TABLE t1 DROP COLUMN c; +} +do_execsql_test 7.2 { + SELECT sql FROM sqlite_schema; +} {{CREATE TABLE t1(a, b, PRIMARY KEY(a COLLATE nocase, a)) WITHOUT ROWID}} +do_execsql_test 7.3 { + SELECT * FROM t1; +} {1 2 4 5} + +reset_db +do_execsql_test 8.0 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b); + PRAGMA writable_schema = 1; + UPDATE sqlite_schema + SET sql = 'CREATE TABLE t1(a INTEGER PRIMARY KEY AUTOINCREMENT, b)' +} +db close +sqlite3 db test.db +do_execsql_test 8.1 { + ALTER TABLE t1 DROP COLUMN b; +} +do_execsql_test 8.2 { + SELECT sql FROM sqlite_schema; +} {{CREATE TABLE t1(a INTEGER PRIMARY KEY AUTOINCREMENT)}} + +#------------------------------------------------------------------------- + +foreach {tn wo} { + 1 {} + 2 {WITHOUT ROWID} +} { + reset_db + do_execsql_test 9.$tn.0 " + CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c) $wo; + " + do_execsql_test 9.$tn.1 { + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<50000 + ) + INSERT INTO t1(a, b, c) SELECT i, 123, 456 FROM s; + } + do_execsql_test 9.$tn.2 { + ALTER TABLE t1 DROP COLUMN b; + } + + do_execsql_test 9.$tn.3 { + SELECT count(*), c FROM t1 GROUP BY c; + } {50000 456} +} + + + +finish_test diff --git a/test/alterdropcol2.test b/test/alterdropcol2.test new file mode 100644 index 0000000000..d60e7db441 --- /dev/null +++ b/test/alterdropcol2.test @@ -0,0 +1,222 @@ +# 2021 February 19 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix alterdropcol2 + +# If SQLITE_OMIT_ALTERTABLE is defined, omit this file. +ifcapable !altertable { + finish_test + return +} + +# EVIDENCE-OF: R-58318-35349 The DROP COLUMN syntax is used to remove an +# existing column from a table. +do_execsql_test 1.0 { + CREATE TABLE t1(c, b, a, PRIMARY KEY(b, a)) WITHOUT ROWID; + INSERT INTO t1 VALUES(1, 2, 3), (4, 5, 6); +} +do_execsql_test 1.1 { + ALTER TABLE t1 DROP c; +} + +# EVIDENCE-OF: The DROP COLUMN command removes the named column from the table, +# and also rewrites the entire table to purge the data associated with that +# column. +do_execsql_test 1.2.1 { + SELECT * FROM t1; +} {2 3 5 6} + +do_execsql_test 1.2.2 { + SELECT sql FROM sqlite_schema; +} { + {CREATE TABLE t1(b, a, PRIMARY KEY(b, a)) WITHOUT ROWID} +} + +proc do_atdc_error_test {tn schema atdc error} { + reset_db + execsql $schema + uplevel [list do_catchsql_test $tn $atdc [list 1 [string trim $error]]] +} + +#------------------------------------------------------------------------- +# Test cases 2.* attempt to verify the following: +# +# EVIDENCE-OF: R-24098-10282 The DROP COLUMN command only works if the column +# is not referenced by any other parts of the schema and is not a PRIMARY KEY +# and does not have a UNIQUE constraint. +# + +# EVIDENCE-OF: R-52436-31752 The column is a PRIMARY KEY or part of one. +# +do_atdc_error_test 2.1.1 { + CREATE TABLE x1(a PRIMARY KEY, b, c); +} { + ALTER TABLE x1 DROP COLUMN a +} { + cannot drop PRIMARY KEY column: "a" +} +do_atdc_error_test 2.1.2 { + CREATE TABLE x1(a,b,c,d,e, PRIMARY KEY(b,c,d)); +} { + ALTER TABLE x1 DROP COLUMN c +} { + cannot drop PRIMARY KEY column: "c" +} + +# EVIDENCE-OF: R-43412-16016 The column has a UNIQUE constraint. +# +do_atdc_error_test 2.2.1 { + CREATE TABLE x1(a PRIMARY KEY, b, c UNIQUE); +} { + ALTER TABLE x1 DROP COLUMN c +} { + cannot drop UNIQUE column: "c" +} +do_atdc_error_test 2.2.2 { + CREATE TABLE x1(a PRIMARY KEY, b, c, UNIQUE(b, c)); +} { + ALTER TABLE x1 DROP COLUMN c +} { + error in table x1 after drop column: no such column: c +} + +# EVIDENCE-OF: R-46731-08965 The column is indexed. +# +do_atdc_error_test 2.3.1 { + CREATE TABLE 'one two'('x y', 'z 1', 'a b'); + CREATE INDEX idx ON 'one two'('z 1'); +} { + ALTER TABLE 'one two' DROP COLUMN 'z 1' +} { + error in index idx after drop column: no such column: z 1 +} +do_atdc_error_test 2.3.2 { + CREATE TABLE x1(a, b, c); + CREATE INDEX idx ON x1(a); +} { + ALTER TABLE x1 DROP COLUMN a; +} { + error in index idx after drop column: no such column: a +} + +# EVIDENCE-OF: R-46731-08965 The column is indexed. +# +do_atdc_error_test 2.4.1 { + CREATE TABLE x1234(a, b, c PRIMARY KEY) WITHOUT ROWID; + CREATE INDEX i1 ON x1234(b) WHERE ((a+5) % 10)==0; +} { + ALTER TABLE x1234 DROP a +} { + error in index i1 after drop column: no such column: a +} + +# EVIDENCE-OF: R-47838-03249 The column is named in a table or column +# CHECK constraint not associated with the column being dropped. +# +do_atdc_error_test 2.5.1 { + CREATE TABLE x1234(a, b, c PRIMARY KEY, CHECK(((a+5)%10)!=0)) WITHOUT ROWID; +} { + ALTER TABLE x1234 DROP a +} { + error in table x1234 after drop column: no such column: a +} + +# EVIDENCE-OF: R-55640-01652 The column is used in a foreign key constraint. +# +do_atdc_error_test 2.6.1 { + CREATE TABLE p1(x, y UNIQUE); + CREATE TABLE c1(u, v, FOREIGN KEY (v) REFERENCES p1(y)) +} { + ALTER TABLE c1 DROP v +} { + error in table c1 after drop column: unknown column "v" in foreign key definition +} + +# EVIDENCE-OF: R-20795-39479 The column is used in the expression of a +# generated column. +do_atdc_error_test 2.7.1 { + CREATE TABLE c1(u, v, w AS (u+v)); +} { + ALTER TABLE c1 DROP v +} { + error in table c1 after drop column: no such column: v +} +do_atdc_error_test 2.7.2 { + CREATE TABLE c1(u, v, w AS (u+v) STORED); +} { + ALTER TABLE c1 DROP u +} { + error in table c1 after drop column: no such column: u +} + +# EVIDENCE-OF: R-01515-49025 The column appears in a trigger or view. +# +do_atdc_error_test 2.8.1 { + CREATE TABLE log(l); + CREATE TABLE c1(u, v, w); + CREATE TRIGGER tr1 AFTER INSERT ON c1 BEGIN + INSERT INTO log VALUES(new.w); + END; +} { + ALTER TABLE c1 DROP w +} { + error in trigger tr1 after drop column: no such column: new.w +} +do_atdc_error_test 2.8.2 { + CREATE TABLE c1(u, v, w); + CREATE VIEW v1 AS SELECT u, v, w FROM c1; +} { + ALTER TABLE c1 DROP w +} { + error in view v1 after drop column: no such column: w +} +do_atdc_error_test 2.8.3 { + CREATE TABLE c1(u, v, w); + CREATE VIEW v1 AS SELECT * FROM c1 WHERE w IS NOT NULL; +} { + ALTER TABLE c1 DROP w +} { + error in view v1 after drop column: no such column: w +} + +#------------------------------------------------------------------------- +# Verify that a column that is part of a CHECK constraint may be dropped +# if the CHECK constraint was specified as part of the column definition. +# + +# STALE-EVIDENCE: R-60924-11170 However, the column being deleted can be used in a +# column CHECK constraint because the column CHECK constraint is dropped +# together with the column itself. +do_execsql_test 3.0 { + CREATE TABLE yyy(q, w, e CHECK (e > 0), r); + INSERT INTO yyy VALUES(1,1,1,1), (2,2,2,2); + + CREATE TABLE zzz(q, w, e, r, CHECK (e > 0)); + INSERT INTO zzz VALUES(1,1,1,1), (2,2,2,2); +} +do_catchsql_test 3.1.1 { + INSERT INTO yyy VALUES(0,0,0,0); +} {1 {CHECK constraint failed: e > 0}} +do_catchsql_test 3.1.2 { + INSERT INTO yyy VALUES(0,0,0,0); +} {1 {CHECK constraint failed: e > 0}} + +do_execsql_test 3.2.1 { + ALTER TABLE yyy DROP e; +} +do_catchsql_test 3.2.2 { + ALTER TABLE zzz DROP e; +} {1 {error in table zzz after drop column: no such column: e}} + +finish_test diff --git a/test/alterfault.test b/test/alterfault.test new file mode 100644 index 0000000000..b6b42973ef --- /dev/null +++ b/test/alterfault.test @@ -0,0 +1,41 @@ +# 2021 November 16 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix alterfault + +# If SQLITE_OMIT_ALTERTABLE is defined, omit this file. +ifcapable !altertable { + finish_test + return +} + +do_execsql_test 1.0 { + CREATE TABLE t1(a); +} +faultsim_save_and_close + +do_faultsim_test 1.1 -faults oom* -prep { + faultsim_restore_and_reopen +} -body { + execsql { + ALTER TABLE t1 ADD COLUMN b CHECK (a!=1) + } +} -test { + faultsim_test_result {0 {}} +} + + + +finish_test diff --git a/test/alterlegacy.test b/test/alterlegacy.test new file mode 100644 index 0000000000..dfa1716467 --- /dev/null +++ b/test/alterlegacy.test @@ -0,0 +1,469 @@ +# 2018 September 20 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix alterlegacy + +# If SQLITE_OMIT_ALTERTABLE is defined, omit this file. +ifcapable !altertable { + finish_test + return +} + +do_execsql_test 1.0 { + PRAGMA legacy_alter_table = 1; + CREATE TABLE t1(a, b, CHECK(t1.a != t1.b)); + CREATE TABLE t2(a, b); + CREATE INDEX t2expr ON t2(a) WHERE t2.b>0; +} + +do_execsql_test 1.1 { + SELECT sql FROM sqlite_master +} { + {CREATE TABLE t1(a, b, CHECK(t1.a != t1.b))} + {CREATE TABLE t2(a, b)} + {CREATE INDEX t2expr ON t2(a) WHERE t2.b>0} +} + +# Legacy behavior is to corrupt the schema in this case, as the table name in +# the CHECK constraint is incorrect after "t1" is renamed. This version is +# slightly different - it rejects the change and rolls back the transaction. +do_catchsql_test 1.2 { + ALTER TABLE t1 RENAME TO t1new; +} {1 {error in table t1new after rename: no such column: t1.a}} + +do_execsql_test 1.3 { + CREATE TABLE t3(c, d); + ALTER TABLE t3 RENAME TO t3new; + DROP TABLE t3new; +} + +do_execsql_test 1.4 { + SELECT sql FROM sqlite_master +} { + {CREATE TABLE t1(a, b, CHECK(t1.a != t1.b))} + {CREATE TABLE t2(a, b)} + {CREATE INDEX t2expr ON t2(a) WHERE t2.b>0} +} + + +do_catchsql_test 1.3 { + ALTER TABLE t2 RENAME TO t2new; +} {1 {error in index t2expr after rename: no such column: t2.b}} +do_execsql_test 1.4 { + SELECT sql FROM sqlite_master +} { + {CREATE TABLE t1(a, b, CHECK(t1.a != t1.b))} + {CREATE TABLE t2(a, b)} + {CREATE INDEX t2expr ON t2(a) WHERE t2.b>0} +} + + +#------------------------------------------------------------------------- +reset_db +ifcapable vtab { + register_echo_module db + + do_execsql_test 2.0 { + PRAGMA legacy_alter_table = 1; + CREATE TABLE abc(a, b, c); + INSERT INTO abc VALUES(1, 2, 3); + CREATE VIRTUAL TABLE eee USING echo('abc'); + SELECT * FROM eee; + } {1 2 3} + + do_execsql_test 2.1 { + ALTER TABLE eee RENAME TO fff; + SELECT * FROM fff; + } {1 2 3} + + db close + sqlite3 db test.db + + do_catchsql_test 2.2 { + ALTER TABLE fff RENAME TO ggg; + } {1 {no such module: echo}} +} + +#------------------------------------------------------------------------- +reset_db + +do_execsql_test 3.0 { + PRAGMA legacy_alter_table = 1; + CREATE TABLE txx(a, b, c); + INSERT INTO txx VALUES(1, 2, 3); + CREATE VIEW vvv AS SELECT main.txx.a, txx.b, c FROM txx; + CREATE VIEW uuu AS SELECT main.one.a, one.b, c FROM txx AS one; + CREATE VIEW temp.ttt AS SELECT main.txx.a, txx.b, one.b, main.one.a FROM txx AS one, txx; +} + +do_execsql_test 3.1.1 { + SELECT * FROM vvv; +} {1 2 3} +do_execsql_test 3.1.2a { + ALTER TABLE txx RENAME TO "t xx"; +} +do_catchsql_test 3.1.2b { + SELECT * FROM vvv; +} {1 {no such table: main.txx}} +do_execsql_test 3.1.3 { + SELECT sql FROM sqlite_master WHERE name='vvv'; +} {{CREATE VIEW vvv AS SELECT main.txx.a, txx.b, c FROM txx}} + + +do_catchsql_test 3.2.1 { + SELECT * FROM uuu; +} {1 {no such table: main.txx}} +do_execsql_test 3.2.2 { + SELECT sql FROM sqlite_master WHERE name='uuu';; +} {{CREATE VIEW uuu AS SELECT main.one.a, one.b, c FROM txx AS one}} + +do_catchsql_test 3.3.1 { + SELECT * FROM ttt; +} {1 {no such table: txx}} +do_execsql_test 3.3.2 { + SELECT sql FROM sqlite_temp_master WHERE name='ttt'; +} {{CREATE VIEW ttt AS SELECT main.txx.a, txx.b, one.b, main.one.a FROM txx AS one, txx}} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 4.0 { + PRAGMA legacy_alter_table = 1; + CREATE table t1(x, y); + CREATE table t2(a, b); + + CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN + SELECT t1.x, * FROM t1, t2; + INSERT INTO t2 VALUES(new.x, new.y); + END; +} + +do_execsql_test 4.1 { + INSERT INTO t1 VALUES(1, 1); + ALTER TABLE t1 RENAME TO t11; +} +do_catchsql_test 4.1a { + INSERT INTO t11 VALUES(2, 2); +} {1 {no such table: main.t1}} +do_execsql_test 4.1b { + ALTER TABLE t11 RENAME TO t1; + ALTER TABLE t2 RENAME TO t22; +} +do_catchsql_test 4.1c { + INSERT INTO t1 VALUES(3, 3); +} {1 {no such table: main.t2}} + +proc squish {a} { + string trim [regsub -all {[[:space:]][[:space:]]*} $a { }] +} +db func squish squish +do_test 4.2 { + execsql { SELECT squish(sql) FROM sqlite_master WHERE name = 'tr1' } +} [list [squish { + CREATE TRIGGER tr1 AFTER INSERT ON "t1" BEGIN + SELECT t1.x, * FROM t1, t2; + INSERT INTO t2 VALUES(new.x, new.y); + END +}]] + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 5.0 { + PRAGMA legacy_alter_table = 1; + CREATE TABLE t9(a, b, c); + CREATE TABLE t10(a, b, c); + CREATE TEMP TABLE t9(a, b, c); + + CREATE TRIGGER temp.t9t AFTER INSERT ON temp.t9 BEGIN + INSERT INTO t10 VALUES(new.a, new.b, new.c); + END; + + INSERT INTO temp.t9 VALUES(1, 2, 3); + SELECT * FROM t10; +} {1 2 3} + +do_execsql_test 5.1 { + ALTER TABLE temp.t9 RENAME TO 't1234567890' +} + +do_execsql_test 5.2 { + CREATE TABLE t1(a, b); + CREATE TABLE t2(a, b); + INSERT INTO t1 VALUES(1, 2); + INSERT INTO t2 VALUES(3, 4); + CREATE VIEW v AS SELECT one.a, one.b, t2.a, t2.b FROM t1 AS one, t2; + SELECT * FROM v; +} {1 2 3 4} + +do_execsql_test 5.3 { + ALTER TABLE t2 RENAME TO one; +} {} + +do_catchsql_test 5.4 { + SELECT * FROM v +} {1 {no such table: main.t2}} + +do_execsql_test 5.5 { + ALTER TABLE one RENAME TO t2; + DROP VIEW v; + CREATE VIEW temp.vv AS SELECT one.a, one.b, t2.a, t2.b FROM t1 AS one, t2; + SELECT * FROM vv; +} {1 2 3 4} + +do_execsql_test 5.6 { + ALTER TABLE t2 RENAME TO one; +} {} +do_catchsql_test 5.7 { + SELECT * FROM vv +} {1 {no such table: t2}} + +#------------------------------------------------------------------------- + +ifcapable vtab { + register_tcl_module db + proc tcl_command {method args} { + switch -- $method { + xConnect { + return "CREATE TABLE t1(a, b, c)" + } + } + return {} + } + + do_execsql_test 6.0 { + CREATE VIRTUAL TABLE x1 USING tcl(tcl_command); + } + + do_execsql_test 6.1 { + ALTER TABLE x1 RENAME TO x2; + SELECT sql FROM sqlite_master WHERE name = 'x2' + } {{CREATE VIRTUAL TABLE "x2" USING tcl(tcl_command)}} + + do_execsql_test 7.1 { + CREATE TABLE ddd(db, sql, zOld, zNew, bTemp); + INSERT INTO ddd VALUES( + 'main', 'CREATE TABLE x1(i INTEGER, t TEXT)', 'ddd', NULL, 0 + ), ( + 'main', 'CREATE TABLE x1(i INTEGER, t TEXT)', NULL, 'eee', 0 + ), ( + 'main', NULL, 'ddd', 'eee', 0 + ); + } {} +} + +#------------------------------------------------------------------------- +# +reset_db +forcedelete test.db2 +do_execsql_test 8.1 { + PRAGMA legacy_alter_table = 1; + ATTACH 'test.db2' AS aux; + PRAGMA foreign_keys = on; + CREATE TABLE aux.p1(a INTEGER PRIMARY KEY, b); + CREATE TABLE aux.c1(x INTEGER PRIMARY KEY, y REFERENCES p1(a)); + INSERT INTO aux.p1 VALUES(1, 1); + INSERT INTO aux.p1 VALUES(2, 2); + INSERT INTO aux.c1 VALUES(NULL, 2); + CREATE TABLE aux.c2(x INTEGER PRIMARY KEY, y REFERENCES c1(a)); +} + +do_execsql_test 8.2 { + ALTER TABLE aux.p1 RENAME TO ppp; +} + +do_execsql_test 8.2 { + INSERT INTO aux.c1 VALUES(NULL, 1); + SELECT sql FROM aux.sqlite_master WHERE name = 'c1'; +} {{CREATE TABLE c1(x INTEGER PRIMARY KEY, y REFERENCES "ppp"(a))}} + +reset_db +do_execsql_test 9.0 { + PRAGMA legacy_alter_table = 1; + CREATE TABLE t1(a, b, c); + CREATE VIEW v1 AS SELECT * FROM t2; +} +do_execsql_test 9.1 { + ALTER TABLE t1 RENAME TO t3; +} {} +do_execsql_test 9.1b { + ALTER TABLE t3 RENAME TO t1; +} {} +do_execsql_test 9.2 { + DROP VIEW v1; + CREATE TRIGGER tr AFTER INSERT ON t1 BEGIN + INSERT INTO t2 VALUES(new.a); + END; +} +do_execsql_test 9.3 { + ALTER TABLE t1 RENAME TO t3; +} {} + +forcedelete test.db2 +do_execsql_test 9.4 { + ALTER TABLE t3 RENAME TO t1; + DROP TRIGGER tr; + + ATTACH 'test.db2' AS aux; + CREATE TRIGGER tr AFTER INSERT ON t1 WHEN new.a IS NULL BEGIN SELECT 1, 2, 3; END; + + CREATE TABLE aux.t1(x); + CREATE TEMP TRIGGER tr AFTER INSERT ON aux.t1 BEGIN SELECT 1, 2, 3; END; +} +do_execsql_test 9.5 { + ALTER TABLE main.t1 RENAME TO t3; +} +do_execsql_test 9.6 { + SELECT sql FROM sqlite_temp_master; + SELECT sql FROM sqlite_master WHERE type='trigger'; +} { + {CREATE TRIGGER tr AFTER INSERT ON aux.t1 BEGIN SELECT 1, 2, 3; END} + {CREATE TRIGGER tr AFTER INSERT ON "t3" WHEN new.a IS NULL BEGIN SELECT 1, 2, 3; END} +} + +#------------------------------------------------------------------------- +reset_db +ifcapable fts5 { + do_execsql_test 10.0 { + PRAGMA legacy_alter_table = 1; + CREATE VIRTUAL TABLE fff USING fts5(x, y, z); + } + + do_execsql_test 10.1 { + BEGIN; + INSERT INTO fff VALUES('a', 'b', 'c'); + ALTER TABLE fff RENAME TO ggg; + COMMIT; + } + + do_execsql_test 10.2 { + SELECT * FROM ggg; + } {a b c} +} + +#------------------------------------------------------------------------- +reset_db +forcedelete test.db2 +db func trigger trigger +set ::trigger [list] +proc trigger {args} { + lappend ::trigger $args +} +do_execsql_test 11.0 { + PRAGMA legacy_alter_table = 1; + ATTACH 'test.db2' AS aux; + CREATE TABLE aux.t1(a, b, c); + CREATE TABLE main.t1(a, b, c); + CREATE TEMP TRIGGER tr AFTER INSERT ON aux.t1 BEGIN + SELECT trigger(new.a, new.b, new.c); + END; +} + +do_execsql_test 11.1 { + INSERT INTO main.t1 VALUES(1, 2, 3); + INSERT INTO aux.t1 VALUES(4, 5, 6); +} +do_test 11.2 { set ::trigger } {{4 5 6}} + +do_execsql_test 11.3 { + SELECT name, tbl_name FROM sqlite_temp_master; +} {tr t1} + +do_execsql_test 11.4 { + ALTER TABLE main.t1 RENAME TO t2; + SELECT name, tbl_name FROM sqlite_temp_master; +} {tr t1} + +do_execsql_test 11.5 { + ALTER TABLE aux.t1 RENAME TO t2; + SELECT name, tbl_name FROM sqlite_temp_master; +} {tr t2} + +do_execsql_test 11.6 { + INSERT INTO aux.t2 VALUES(7, 8, 9); +} +do_test 11.7 { set ::trigger } {{4 5 6} {7 8 9}} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 12.0 { + PRAGMA legacy_alter_table = 1; + CREATE TABLE t1(a); + CREATE TABLE t2(w); + CREATE TRIGGER temp.r1 AFTER INSERT ON main.t2 BEGIN + INSERT INTO t1(a) VALUES(new.w); + END; + CREATE TEMP TABLE t2(x); +} + +do_execsql_test 12.1 { + ALTER TABLE main.t2 RENAME TO t3; +} + +do_execsql_test 12.2 { + INSERT INTO t3 VALUES('WWW'); + SELECT * FROM t1; +} {WWW} + + +#------------------------------------------------------------------------- +reset_db + +ifcapable rtree { + do_execsql_test 14.0 { + PRAGMA legacy_alter_table = 1; + CREATE VIRTUAL TABLE rt USING rtree(id, minx, maxx, miny, maxy); + + CREATE TABLE "mytable" ( "fid" INTEGER PRIMARY KEY, "geom" BLOB); + + CREATE TRIGGER tr1 AFTER UPDATE OF "geom" ON "mytable" + WHEN OLD."fid" = NEW."fid" AND NEW."geom" IS NULL BEGIN + DELETE FROM rt WHERE id = OLD."fid"; + END; + + INSERT INTO mytable VALUES(1, X'abcd'); + } + + do_execsql_test 14.1 { + UPDATE mytable SET geom = X'1234' + } + + do_execsql_test 14.2 { + ALTER TABLE mytable RENAME TO mytable_renamed; + } + + do_execsql_test 14.3 { + CREATE TRIGGER tr2 AFTER INSERT ON mytable_renamed BEGIN + DELETE FROM rt WHERE id=(SELECT min(id) FROM rt); + END; + } + + do_execsql_test 14.4 { + ALTER TABLE mytable_renamed RENAME TO mytable2; + } +} + +reset_db +do_execsql_test 14.5 { + PRAGMA legacy_alter_table = 1; + CREATE TABLE t1(a, b, c); + CREATE VIEW v1 AS SELECT * FROM t1; + CREATE TRIGGER xyz AFTER INSERT ON t1 BEGIN + SELECT a, b FROM v1; + END; +} +do_execsql_test 14.6 { + ALTER TABLE t1 RENAME TO tt1; +} + + +finish_test diff --git a/test/altermalloc.test b/test/altermalloc.test index a35e7d5a34..22ea158463 100644 --- a/test/altermalloc.test +++ b/test/altermalloc.test @@ -19,7 +19,7 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl # If SQLITE_OMIT_ALTERTABLE is defined, omit this file. -ifcapable !altertable||!memdebug { +ifcapable !altertable { finish_test return } diff --git a/test/altermalloc2.test b/test/altermalloc2.test new file mode 100644 index 0000000000..a90be88cf5 --- /dev/null +++ b/test/altermalloc2.test @@ -0,0 +1,127 @@ +# 2018 August 20 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/malloc_common.tcl +set testprefix altermalloc2 + +# If SQLITE_OMIT_ALTERTABLE is defined, omit this file. +ifcapable !altertable { + finish_test + return +} + +do_execsql_test 1.0 { + CREATE TABLE t1(abcd, efgh); +} +faultsim_save_and_close + +set ::TMPDBERROR [list 1 \ + {unable to open a temporary database file for storing temporary tables} +] + + +do_faultsim_test 1 -prep { + faultsim_restore_and_reopen +} -body { + execsql { + ALTER TABLE t1 RENAME abcd TO dcba + } +} -test { + faultsim_test_result {0 {}} $::TMPDBERROR +} + +catch {db close} +forcedelete test.db +sqlite3 db test.db +do_execsql_test 2.0 { + PRAGMA encoding = 'utf-16'; + CREATE TABLE t1(abcd, efgh); +} +faultsim_save_and_close + +do_faultsim_test 2 -prep { + faultsim_restore_and_reopen +} -body { + execsql { + ALTER TABLE t1 RENAME abcd TO dcba + } +} -test { + faultsim_test_result {0 {}} $::TMPDBERROR +} + + +reset_db +do_execsql_test 3.0 { + CREATE TABLE t1(abcd, efgh); + CREATE VIEW v1 AS SELECT * FROM t1 WHERE abcd>efgh; +} +faultsim_save_and_close + +do_faultsim_test 3 -prep { + faultsim_restore_and_reopen +} -body { + execsql { + ALTER TABLE t1 RENAME abcd TO dcba + } +} -test { + faultsim_test_result {0 {}} $::TMPDBERROR +} + +reset_db +do_execsql_test 4.0 { + CREATE TABLE rr(a, b); + CREATE VIEW vv AS SELECT * FROM rr; + + CREATE TRIGGER vv1 INSTEAD OF INSERT ON vv BEGIN + SELECT 1, 2, 3; + END; + CREATE TRIGGER tr1 AFTER INSERT ON rr BEGIN + INSERT INTO vv VALUES(new.a, new.b); + END; +} {} + +faultsim_save_and_close +do_faultsim_test 4 -faults oom-* -prep { + faultsim_restore_and_reopen + execsql { SELECT * FROM sqlite_master } +} -body { + execsql { + ALTER TABLE rr RENAME a TO c; + } +} -test { + faultsim_test_result {0 {}} $::TMPDBERROR +} + +reset_db +do_execsql_test 5.0 { + CREATE TABLE rr(a, b); + CREATE VIEW vv AS SELECT * FROM ( + WITH abc(d, e) AS (SELECT * FROM rr) + SELECT * FROM abc + ); +} {} + +faultsim_save_and_close +do_faultsim_test 5 -faults oom-* -prep { + faultsim_restore_and_reopen + execsql { SELECT * FROM sqlite_master } +} -body { + execsql { + ALTER TABLE rr RENAME TO c; + } +} -test { + faultsim_test_result {0 {}} $::TMPDBERROR +} + +finish_test diff --git a/test/altermalloc3.test b/test/altermalloc3.test new file mode 100644 index 0000000000..47efebe228 --- /dev/null +++ b/test/altermalloc3.test @@ -0,0 +1,90 @@ +# 2021 February 18 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/malloc_common.tcl +set testprefix altermalloc3 + +# If SQLITE_OMIT_ALTERTABLE is defined, omit this file. +ifcapable !altertable { + finish_test + return +} + + +set ::TMPDBERROR [list 1 \ + {unable to open a temporary database file for storing temporary tables} +] + +sqlite3_db_config db SQLITE_DBCONFIG_DQS_DDL 1 +do_execsql_test 1.0 { + CREATE TABLE x1( + one, two, three, PRIMARY KEY(one), + CHECK (three!="xyz"), CHECK (two!="one") + ) WITHOUT ROWID; + CREATE INDEX x1i ON x1(one+"two"+"four") WHERE "five"; + CREATE TEMP TRIGGER AFTER INSERT ON x1 BEGIN + UPDATE x1 SET two=new.three || "new" WHERE one=new.one||""; + END; + CREATE TABLE t1(a, b, c, d, PRIMARY KEY(d, b)) WITHOUT ROWID; + INSERT INTO t1 VALUES(1, 2, 3, 4); +} +faultsim_save_and_close + +do_faultsim_test 1 -prep { + faultsim_restore_and_reopen +} -body { + execsql { ALTER TABLE t1 DROP COLUMN c } +} -test { + faultsim_test_result {0 {}} $::TMPDBERROR +} + + +#------------------------------------------------------------------------- +# dbsqlfuzz e3dd84cda3848016a6a6024c7249d09bc2ef2615 +# +reset_db +do_execsql_test 2.0 { + CREATE TABLE t2(k,v); + CREATE TRIGGER r2 AFTER INSERT ON t2 BEGIN + UPDATE t2 SET (k,v)= ( + (WITH cte1(a) AS ( SELECT 1 FROM ( SELECT * FROM t2 ) ) + SELECT a FROM cte1 + ), 1); + END; + + CREATE TRIGGER r1 AFTER INSERT ON t2 BEGIN + UPDATE t2 SET k=1 FROM t2 AS one, t2 AS two NATURAL JOIN t2 AS three + WHERE one.k=two.v; + END; +} + +faultsim_save_and_close +faultsim_restore_and_reopen + +do_execsql_test 2.1 { + ALTER TABLE t2 RENAME TO t2x; +} + +do_faultsim_test 2.2 -prep { + faultsim_restore_and_reopen + db eval { SELECT * FROM sqlite_master } +} -body { + execsql { + ALTER TABLE t2 RENAME TO t2x; + } +} -test { + faultsim_test_result {0 {}} $::TMPDBERROR +} + +finish_test diff --git a/test/alterqf.test b/test/alterqf.test new file mode 100644 index 0000000000..b248c7c7f4 --- /dev/null +++ b/test/alterqf.test @@ -0,0 +1,120 @@ +# 2021 March 16 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. This +# script focuses on testing internal function sqlite_rename_quotefix(). +# + + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix alterqf + +# If SQLITE_OMIT_ALTERTABLE is defined, omit this file. +ifcapable !altertable { + finish_test + return +} + + +sqlite3_test_control SQLITE_TESTCTRL_INTERNAL_FUNCTIONS db +sqlite3_db_config db SQLITE_DBCONFIG_DQS_DDL 1 +sqlite3_db_config db SQLITE_DBCONFIG_DQS_DML 1 + +do_execsql_test 1.0 { + CREATE TABLE t1(a, b, c); +} + +foreach {tn before after} { + 1 {CREATE VIEW v1 AS SELECT "a", "b", "notacolumn!", "c" FROM t1} + {CREATE VIEW v1 AS SELECT "a", "b", 'notacolumn!', "c" FROM t1} + + 2 {CREATE VIEW v1 AS SELECT "a", "b", "not'a'column!", "c" FROM t1} + {CREATE VIEW v1 AS SELECT "a", "b", 'not''a''column!', "c" FROM t1} + + 3 {CREATE VIEW v1 AS SELECT "a", "b", "not""a""column!", "c" FROM t1} + {CREATE VIEW v1 AS SELECT "a", "b", 'not"a"column!', "c" FROM t1} + + 4 {CREATE VIEW v1 AS SELECT "val", count("b") FROM t1 GROUP BY "abc"} + {CREATE VIEW v1 AS SELECT 'val', count("b") FROM t1 GROUP BY 'abc'} + + 5 {CREATE TABLE xyz(a CHECK (a!="str"), b AS (a||"str"))} + {CREATE TABLE xyz(a CHECK (a!='str'), b AS (a||'str'))} + + 6 {CREATE INDEX i1 ON t1(a || "str", "b", "val")} + {CREATE INDEX i1 ON t1(a || 'str', "b", 'val')} + + 7 {CREATE TRIGGER tr AFTER INSERT ON t1 BEGIN SELECT "abcd"; END} + {CREATE TRIGGER tr AFTER INSERT ON t1 BEGIN SELECT 'abcd'; END} + + 8 {CREATE VIEW v1 AS SELECT "string"'alias' FROM t1} + {CREATE VIEW v1 AS SELECT 'string' 'alias' FROM t1} + + 9 {CREATE INDEX i1 ON t1(a) WHERE "b"="bb"} + {CREATE INDEX i1 ON t1(a) WHERE "b"='bb'} + + 10 {CREATE TABLE t2(abc, xyz CHECK (xyz != "123"))} + {CREATE TABLE t2(abc, xyz CHECK (xyz != '123'))} + + 11 {CREATE TRIGGER ott AFTER UPDATE ON t1 BEGIN + SELECT max("str", new."a") FROM t1 + WHERE string_agg("b", ",") OVER (ORDER BY c||"str"); + UPDATE t1 SET c= b + "str"; + DELETE FROM t1 WHERE EXISTS ( + SELECT 1 FROM t1 AS o WHERE o."a" = "o.a" AND t1.b IN("t1.b") + ); + END; + } {CREATE TRIGGER ott AFTER UPDATE ON t1 BEGIN + SELECT max('str', new."a") FROM t1 + WHERE string_agg("b", ',') OVER (ORDER BY c||'str'); + UPDATE t1 SET c= b + 'str'; + DELETE FROM t1 WHERE EXISTS ( + SELECT 1 FROM t1 AS o WHERE o."a" = 'o.a' AND t1.b IN('t1.b') + ); + END; + } + +} { + do_execsql_test 1.$tn { + SELECT sqlite_rename_quotefix('main', $before) + } [list $after] +} + +#------------------------------------------------------------------------- +reset_db +sqlite3_db_config db SQLITE_DBCONFIG_DQS_DDL 1 +sqlite3_db_config db SQLITE_DBCONFIG_DQS_DML 1 +do_execsql_test 2.0 { + CREATE TABLE x1( + one, two, three, PRIMARY KEY(one), + CHECK (three!="xyz"), CHECK (two!="one") + ) WITHOUT ROWID; + CREATE INDEX x1i ON x1(one+"two"+"four") WHERE "five"; + CREATE TEMP TRIGGER AFTER INSERT ON x1 BEGIN + UPDATE x1 SET two=new.three || "new" WHERE one=new.one||""; + END; +} + +do_execsql_test 2.1 { + ALTER TABLE x1 RENAME two TO 'four'; + SELECT sql FROM sqlite_schema; + SELECT sql FROM sqlite_temp_schema; +} {{CREATE TABLE x1( + one, "four", three, PRIMARY KEY(one), + CHECK (three!='xyz'), CHECK ("four"!="one") + ) WITHOUT ROWID} + {CREATE INDEX x1i ON x1(one+"four"+'four') WHERE 'five'} + {CREATE TRIGGER AFTER INSERT ON x1 BEGIN + UPDATE x1 SET "four"=new.three || 'new' WHERE one=new.one||''; + END} +} + + +finish_test diff --git a/test/altertab.test b/test/altertab.test new file mode 100644 index 0000000000..9cc43e14de --- /dev/null +++ b/test/altertab.test @@ -0,0 +1,1002 @@ +# 2018 August 24 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix altertab + +# If SQLITE_OMIT_ALTERTABLE is defined, omit this file. +ifcapable !altertable { + finish_test + return +} + +do_execsql_test 1.0 { + CREATE TABLE t1(a, b, CHECK(t1.a != t1.b)); + + CREATE TABLE t2(a, b); + CREATE INDEX t2expr ON t2(a) WHERE t2.b>0; +} + +do_execsql_test 1.1 { + SELECT sql FROM sqlite_master +} { + {CREATE TABLE t1(a, b, CHECK(t1.a != t1.b))} + {CREATE TABLE t2(a, b)} + {CREATE INDEX t2expr ON t2(a) WHERE t2.b>0} +} + +do_execsql_test 1.2 { + ALTER TABLE t1 RENAME TO t1new; +} + +do_execsql_test 1.3 { + CREATE TABLE t3(c, d); + ALTER TABLE t3 RENAME TO t3new; + DROP TABLE t3new; +} + +do_execsql_test 1.4 { + SELECT sql FROM sqlite_master +} { + {CREATE TABLE "t1new"(a, b, CHECK("t1new".a != "t1new".b))} + {CREATE TABLE t2(a, b)} + {CREATE INDEX t2expr ON t2(a) WHERE t2.b>0} +} + + +do_execsql_test 1.3 { + ALTER TABLE t2 RENAME TO t2new; +} +do_execsql_test 1.4 { + SELECT sql FROM sqlite_master +} { + {CREATE TABLE "t1new"(a, b, CHECK("t1new".a != "t1new".b))} + {CREATE TABLE "t2new"(a, b)} + {CREATE INDEX t2expr ON "t2new"(a) WHERE "t2new".b>0} +} + + +#------------------------------------------------------------------------- +reset_db +ifcapable vtab { + register_echo_module db + + do_execsql_test 2.0 { + CREATE TABLE abc(a, b, c); + INSERT INTO abc VALUES(1, 2, 3); + CREATE VIRTUAL TABLE eee USING echo('abc'); + SELECT * FROM eee; + } {1 2 3} + + do_execsql_test 2.1 { + ALTER TABLE eee RENAME TO fff; + SELECT * FROM fff; + } {1 2 3} + + db close + sqlite3 db test.db + + do_catchsql_test 2.2 { + ALTER TABLE fff RENAME TO ggg; + } {1 {no such module: echo}} +} + +#------------------------------------------------------------------------- +reset_db + +do_execsql_test 3.0 { + CREATE TABLE txx(a, b, c); + INSERT INTO txx VALUES(1, 2, 3); + CREATE VIEW vvv AS SELECT main.txx.a, txx.b, c FROM txx; + CREATE VIEW uuu AS SELECT main.one.a, one.b, c FROM txx AS one; + CREATE VIEW temp.ttt AS SELECT main.txx.a, txx.b, one.b, main.one.a FROM txx AS one, txx; +} + +do_execsql_test 3.1.1 { + SELECT * FROM vvv; +} {1 2 3} +do_execsql_test 3.1.2 { + ALTER TABLE txx RENAME TO "t xx"; + SELECT * FROM vvv; +} {1 2 3} +do_execsql_test 3.1.3 { + SELECT sql FROM sqlite_master WHERE name='vvv'; +} {{CREATE VIEW vvv AS SELECT main."t xx".a, "t xx".b, c FROM "t xx"}} + + +do_execsql_test 3.2.1 { + SELECT * FROM uuu; +} {1 2 3} +do_execsql_test 3.2.2 { + SELECT sql FROM sqlite_master WHERE name='uuu';; +} {{CREATE VIEW uuu AS SELECT main.one.a, one.b, c FROM "t xx" AS one}} + +do_execsql_test 3.3.1 { + SELECT * FROM ttt; +} {1 2 2 1} +do_execsql_test 3.3.2 { + SELECT sql FROM sqlite_temp_master WHERE name='ttt'; +} {{CREATE VIEW ttt AS SELECT main."t xx".a, "t xx".b, one.b, main.one.a FROM "t xx" AS one, "t xx"}} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 4.0 { + CREATE table t1(x, y); + CREATE table t2(a, b); + + CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN + SELECT t1.x, * FROM t1, t2; + INSERT INTO t2 VALUES(new.x, new.y); + END; +} + +do_execsql_test 4.1 { + INSERT INTO t1 VALUES(1, 1); + ALTER TABLE t1 RENAME TO t11; + INSERT INTO t11 VALUES(2, 2); + ALTER TABLE t2 RENAME TO t22; + INSERT INTO t11 VALUES(3, 3); +} + +proc squish {a} { + string trim [regsub -all {[[:space:]][[:space:]]*} $a { }] +} +db func squish squish +do_test 4.2 { + execsql { SELECT squish(sql) FROM sqlite_master WHERE name = 'tr1' } +} [list [squish { + CREATE TRIGGER tr1 AFTER INSERT ON "t11" BEGIN + SELECT "t11".x, * FROM "t11", "t22"; + INSERT INTO "t22" VALUES(new.x, new.y); + END +}]] + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 5.0 { + CREATE TABLE t9(a, b, c); + CREATE TABLE t10(a, b, c); + CREATE TEMP TABLE t9(a, b, c); + + CREATE TRIGGER temp.t9t AFTER INSERT ON temp.t9 BEGIN + INSERT INTO t10 VALUES(new.a, new.b, new.c); + END; + + INSERT INTO temp.t9 VALUES(1, 2, 3); + SELECT * FROM t10; +} {1 2 3} + +do_execsql_test 5.1 { + ALTER TABLE temp.t9 RENAME TO 't1234567890' +} + +do_execsql_test 5.2 { + CREATE TABLE t1(a, b); + CREATE TABLE t2(a, b); + INSERT INTO t1 VALUES(1, 2); + INSERT INTO t2 VALUES(3, 4); + CREATE VIEW v AS SELECT one.a, one.b, t2.a, t2.b FROM t1 AS one, t2; + SELECT * FROM v; +} {1 2 3 4} + +do_catchsql_test 5.3 { + ALTER TABLE t2 RENAME TO one; +} {1 {error in view v after rename: ambiguous column name: one.a}} + +do_execsql_test 5.4 { + SELECT * FROM v +} {1 2 3 4} + +do_execsql_test 5.5 { + DROP VIEW v; + CREATE VIEW temp.vv AS SELECT one.a, one.b, t2.a, t2.b FROM t1 AS one, t2; + SELECT * FROM vv; +} {1 2 3 4} + +do_catchsql_test 5.6 { + ALTER TABLE t2 RENAME TO one; +} {1 {error in view vv after rename: ambiguous column name: one.a}} + +#------------------------------------------------------------------------- + +ifcapable vtab { + register_tcl_module db + proc tcl_command {method args} { + switch -- $method { + xConnect { + return "CREATE TABLE t1(a, b, c)" + } + } + return {} + } + + do_execsql_test 6.0 { + CREATE VIRTUAL TABLE x1 USING tcl(tcl_command); + } + + do_execsql_test 6.1 { + ALTER TABLE x1 RENAME TO x2; + SELECT sql FROM sqlite_master WHERE name = 'x2' + } {{CREATE VIRTUAL TABLE "x2" USING tcl(tcl_command)}} + + do_execsql_test 7.1 { + CREATE TABLE ddd(db, sql, zOld, zNew, bTemp); + INSERT INTO ddd VALUES( + 'main', 'CREATE TABLE x1(i INTEGER, t TEXT)', 'ddd', NULL, 0 + ), ( + 'main', 'CREATE TABLE x1(i INTEGER, t TEXT)', NULL, 'eee', 0 + ), ( + 'main', NULL, 'ddd', 'eee', 0 + ); + } {} + + sqlite3_test_control SQLITE_TESTCTRL_INTERNAL_FUNCTIONS db + do_execsql_test 7.2 { + SELECT + sqlite_rename_table(db, 0, 0, sql, zOld, zNew, bTemp) + FROM ddd; + } {{} {} {}} + sqlite3_test_control SQLITE_TESTCTRL_INTERNAL_FUNCTIONS db +} + +#------------------------------------------------------------------------- +# +reset_db +forcedelete test.db2 +do_execsql_test 8.1 { + ATTACH 'test.db2' AS aux; + PRAGMA foreign_keys = on; + CREATE TABLE aux.p1(a INTEGER PRIMARY KEY, b); + CREATE TABLE aux.c1(x INTEGER PRIMARY KEY, y REFERENCES p1(a)); + INSERT INTO aux.p1 VALUES(1, 1); + INSERT INTO aux.p1 VALUES(2, 2); + INSERT INTO aux.c1 VALUES(NULL, 2); + CREATE TABLE aux.c2(x INTEGER PRIMARY KEY, y REFERENCES c1(a)); +} + +do_execsql_test 8.2 { + ALTER TABLE aux.p1 RENAME TO ppp; +} + +do_execsql_test 8.2 { + INSERT INTO aux.c1 VALUES(NULL, 1); + SELECT sql FROM aux.sqlite_master WHERE name = 'c1'; +} {{CREATE TABLE c1(x INTEGER PRIMARY KEY, y REFERENCES "ppp"(a))}} + +reset_db +do_execsql_test 9.0 { + CREATE TABLE t1(a, b, c); + CREATE VIEW v1 AS SELECT * FROM t2; +} +do_catchsql_test 9.1 { + ALTER TABLE t1 RENAME TO t3; +} {1 {error in view v1: no such table: main.t2}} +do_execsql_test 9.2 { + DROP VIEW v1; + CREATE TRIGGER tr AFTER INSERT ON t1 BEGIN + INSERT INTO t2 VALUES(new.a); + END; +} +do_catchsql_test 9.3 { + ALTER TABLE t1 RENAME TO t3; +} {1 {error in trigger tr: no such table: main.t2}} + +forcedelete test.db2 +do_execsql_test 9.4 { + DROP TRIGGER tr; + + ATTACH 'test.db2' AS aux; + CREATE TRIGGER tr AFTER INSERT ON t1 WHEN new.a IS NULL BEGIN SELECT 1, 2, 3; END; + + CREATE TABLE aux.t1(x); + CREATE TEMP TRIGGER tr AFTER INSERT ON aux.t1 BEGIN SELECT 1, 2, 3; END; +} +do_execsql_test 9.5 { + ALTER TABLE main.t1 RENAME TO t3; +} +do_execsql_test 9.6 { + SELECT sql FROM sqlite_temp_master; + SELECT sql FROM sqlite_master WHERE type='trigger'; +} { + {CREATE TRIGGER tr AFTER INSERT ON aux.t1 BEGIN SELECT 1, 2, 3; END} + {CREATE TRIGGER tr AFTER INSERT ON "t3" WHEN new.a IS NULL BEGIN SELECT 1, 2, 3; END} +} + +#------------------------------------------------------------------------- +reset_db +ifcapable fts5 { + do_execsql_test 10.0 { + CREATE VIRTUAL TABLE fff USING fts5(x, y, z); + } + + do_execsql_test 10.1 { + BEGIN; + INSERT INTO fff VALUES('a', 'b', 'c'); + ALTER TABLE fff RENAME TO ggg; + COMMIT; + } + + do_execsql_test 10.2 { + SELECT * FROM ggg; + } {a b c} +} + +#------------------------------------------------------------------------- +reset_db +forcedelete test.db2 +db func trigger trigger +set ::trigger [list] +proc trigger {args} { + lappend ::trigger $args +} +do_execsql_test 11.0 { + ATTACH 'test.db2' AS aux; + CREATE TABLE aux.t1(a, b, c); + CREATE TABLE main.t1(a, b, c); + CREATE TEMP TRIGGER tr AFTER INSERT ON aux.t1 BEGIN + SELECT trigger(new.a, new.b, new.c); + END; +} + +do_execsql_test 11.1 { + INSERT INTO main.t1 VALUES(1, 2, 3); + INSERT INTO aux.t1 VALUES(4, 5, 6); +} +do_test 11.2 { set ::trigger } {{4 5 6}} + +do_execsql_test 11.3 { + SELECT name, tbl_name FROM sqlite_temp_master; +} {tr t1} + +do_execsql_test 11.4 { + ALTER TABLE main.t1 RENAME TO t2; + SELECT name, tbl_name FROM sqlite_temp_master; +} {tr t1} + +do_execsql_test 11.5 { + ALTER TABLE aux.t1 RENAME TO t2; + SELECT name, tbl_name FROM sqlite_temp_master; +} {tr t2} + +do_execsql_test 11.6 { + INSERT INTO aux.t2 VALUES(7, 8, 9); +} +do_test 11.7 { set ::trigger } {{4 5 6} {7 8 9}} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 12.0 { + CREATE TABLE t1(a); + CREATE TABLE t2(w); + CREATE TRIGGER temp.r1 AFTER INSERT ON main.t2 BEGIN + INSERT INTO t1(a) VALUES(new.w); + END; + CREATE TEMP TABLE t2(x); +} + +do_execsql_test 12.1 { + ALTER TABLE main.t2 RENAME TO t3; +} + +do_execsql_test 12.2 { + INSERT INTO t3 VALUES('WWW'); + SELECT * FROM t1; +} {WWW} + + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 13.0 { + CREATE TABLE t1(x, y); + CREATE TABLE t2(a, b); + CREATE TABLE log(c); + CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN + INSERT INTO log SELECT y FROM t1, t2; + END; +} + +do_execsql_test 13.1 { + INSERT INTO t1 VALUES(1, 2); +} + +do_catchsql_test 13.2 { + ALTER TABLE t2 RENAME b TO y; +} {1 {error in trigger tr1 after rename: ambiguous column name: y}} + +#------------------------------------------------------------------------- +reset_db + +ifcapable rtree { + do_execsql_test 14.0 { + CREATE VIRTUAL TABLE rt USING rtree(id, minx, maxx, miny, maxy); + + CREATE TABLE "mytable" ( "fid" INTEGER PRIMARY KEY, "geom" BLOB); + + CREATE TRIGGER tr1 AFTER UPDATE OF "geom" ON "mytable" + WHEN OLD."fid" = NEW."fid" AND NEW."geom" IS NULL BEGIN + DELETE FROM rt WHERE id = OLD."fid"; + END; + + INSERT INTO mytable VALUES(1, X'abcd'); + } + + do_execsql_test 14.1 { + UPDATE mytable SET geom = X'1234' + } + + do_execsql_test 14.2 { + ALTER TABLE mytable RENAME TO mytable_renamed; + } + + do_execsql_test 14.3 { + CREATE TRIGGER tr2 AFTER INSERT ON mytable_renamed BEGIN + DELETE FROM rt WHERE id=(SELECT min(id) FROM rt); + END; + } + + do_execsql_test 14.4 { + ALTER TABLE mytable_renamed RENAME TO mytable2; + } +} + +reset_db +do_execsql_test 14.5 { + CREATE TABLE t1(a, b, c); + CREATE VIEW v1 AS SELECT * FROM t1; + CREATE TRIGGER xyz AFTER INSERT ON t1 BEGIN + SELECT a, b FROM v1; + END; +} +do_execsql_test 14.6 { + ALTER TABLE t1 RENAME TO tt1; +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 15.0 { + CREATE TABLE t1(a integer NOT NULL PRIMARY KEY); + CREATE VIEW v1 AS SELECT a FROM t1; + CREATE TRIGGER tr1 INSTEAD OF INSERT ON v1 BEGIN + UPDATE t1 SET a = NEW.a; + END; + CREATE TRIGGER tr2 INSTEAD OF INSERT ON v1 BEGIN + SELECT new.a; + END; + CREATE TABLE t2 (b); +} + +do_execsql_test 15.1 { + INSERT INTO v1 VALUES(1); + ALTER TABLE t2 RENAME TO t3; +} + +do_execsql_test 15.2 { + CREATE TABLE x(f1 integer NOT NULL); + CREATE VIEW y AS SELECT f1 AS f1 FROM x; + CREATE TRIGGER t INSTEAD OF UPDATE OF f1 ON y BEGIN + UPDATE x SET f1 = NEW.f1; + END; + CREATE TABLE z (f1 integer NOT NULL PRIMARY KEY); + ALTER TABLE z RENAME TO z2; +} + +do_execsql_test 15.3 { + INSERT INTO x VALUES(1), (2), (3); + ALTER TABLE x RENAME f1 TO f2; + SELECT * FROM x; +} {1 2 3} + +do_execsql_test 15.4 { + UPDATE y SET f1 = 'x' WHERE f1 = 1; + SELECT * FROM x; +} {x x x} + +do_execsql_test 15.5 { + SELECT sql FROM sqlite_master WHERE name = 'y'; +} {{CREATE VIEW y AS SELECT f2 AS f1 FROM x}} + +#------------------------------------------------------------------------- +# Test that it is not possible to rename a shadow table in DEFENSIVE mode. +# +ifcapable fts3 { + proc vtab_command {method args} { + switch -- $method { + xConnect { + if {[info exists ::vtab_connect_sql]} { + execsql $::vtab_connect_sql + } + return "CREATE TABLE t1(a, b, c)" + } + + xBestIndex { + set clist [lindex $args 0] + if {[llength $clist]!=1} { error "unexpected constraint list" } + catch { array unset C } + array set C [lindex $clist 0] + if {$C(usable)} { + return "omit 0 cost 0 rows 1 idxnum 555 idxstr eq!" + } else { + return "cost 1000000 rows 0 idxnum 0 idxstr scan..." + } + } + } + + return {} + } + + register_tcl_module db + + sqlite3_db_config db DEFENSIVE 1 + + do_execsql_test 16.0 { + CREATE VIRTUAL TABLE y1 USING fts3; + VACUUM; + } + + do_catchsql_test 16.10 { + INSERT INTO y1_segments VALUES(1, X'1234567890'); + } {1 {table y1_segments may not be modified}} + + do_catchsql_test 16.20 { + DROP TABLE y1_segments; + } {1 {table y1_segments may not be dropped}} + + do_catchsql_test 16.20 { + ALTER TABLE y1_segments RENAME TO abc; + } {1 {table y1_segments may not be altered}} + sqlite3_db_config db DEFENSIVE 0 + do_catchsql_test 16.22 { + ALTER TABLE y1_segments RENAME TO abc; + } {0 {}} + sqlite3_db_config db DEFENSIVE 1 + do_catchsql_test 16.23 { + CREATE TABLE y1_segments AS SELECT * FROM abc; + } {1 {object name reserved for internal use: y1_segments}} + do_catchsql_test 16.24 { + CREATE VIEW y1_segments AS SELECT * FROM abc; + } {1 {object name reserved for internal use: y1_segments}} + sqlite3_db_config db DEFENSIVE 0 + do_catchsql_test 16.25 { + ALTER TABLE abc RENAME TO y1_segments; + } {0 {}} + sqlite3_db_config db DEFENSIVE 1 + + do_execsql_test 16.30 { + ALTER TABLE y1 RENAME TO z1; + } + + do_execsql_test 16.40 { + SELECT * FROM z1_segments; + } +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 17.0 { + CREATE TABLE sqlite1234 (id integer); + ALTER TABLE sqlite1234 RENAME TO User; + SELECT name, sql FROM sqlite_master WHERE sql IS NOT NULL; +} { + User {CREATE TABLE "User" (id integer)} +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 18.1.0 { + CREATE TABLE t0 (c0 INTEGER, PRIMARY KEY(c0)) WITHOUT ROWID; +} +do_execsql_test 18.1.1 { + ALTER TABLE t0 RENAME COLUMN c0 TO c1; +} +do_execsql_test 18.1.2 { + SELECT sql FROM sqlite_master; +} {{CREATE TABLE t0 (c1 INTEGER, PRIMARY KEY(c1)) WITHOUT ROWID}} + +reset_db +do_execsql_test 18.2.0 { + CREATE TABLE t0 (c0 INTEGER, PRIMARY KEY(c0)); +} +do_execsql_test 18.2.1 { + ALTER TABLE t0 RENAME COLUMN c0 TO c1; +} +do_execsql_test 18.2.2 { + SELECT sql FROM sqlite_master; +} {{CREATE TABLE t0 (c1 INTEGER, PRIMARY KEY(c1))}} + +# 2020-02-23 ticket f50af3e8a565776b +reset_db +do_execsql_test 19.100 { + CREATE TABLE t1(x); + CREATE VIEW t2 AS SELECT 1 FROM t1, (t1 AS a0, t1); + ALTER TABLE t1 RENAME TO t3; + SELECT sql FROM sqlite_master; +} {{CREATE TABLE "t3"(x)} {CREATE VIEW t2 AS SELECT 1 FROM "t3", ("t3" AS a0, "t3")}} +do_execsql_test 19.110 { + INSERT INTO t3(x) VALUES(123); + SELECT * FROM t2; +} {1} +do_execsql_test 19.120 { + INSERT INTO t3(x) VALUES('xyz'); + SELECT * FROM t2; +} {1 1 1 1 1 1 1 1} + +# Ticket 4722bdab08cb14 +reset_db +do_execsql_test 20.0 { + CREATE TABLE a(a); + CREATE VIEW b AS SELECT(SELECT *FROM c JOIN a USING(d, a, a, a) JOIN a) IN(); +} +do_execsql_test 20.1 { + ALTER TABLE a RENAME a TO e; +} {} + +reset_db +do_execsql_test 21.0 { + CREATE TABLE a(b); + CREATE VIEW c AS + SELECT NULL INTERSECT + SELECT NULL ORDER BY + likelihood(NULL, (d, (SELECT c))); +} {} +do_catchsql_test 21.1 { + SELECT likelihood(NULL, (d, (SELECT c))); +} {1 {second argument to likelihood() must be a constant between 0.0 and 1.0}} +do_catchsql_test 21.2 { + SELECT * FROM c; +} {1 {1st ORDER BY term does not match any column in the result set}} + +do_catchsql_test 21.3 { + ALTER TABLE a RENAME TO e; +} {1 {error in view c: 1st ORDER BY term does not match any column in the result set}} + +# After forum thread https://sqlite.org/forum/forumpost/ddbe1c7efa +# Ensure that PRAGMA schema_version=N causes a full schema reload. +# +reset_db +do_execsql_test 22.0 { + CREATE TABLE t1(a INT, b TEXT NOT NULL); + INSERT INTO t1 VALUES(1,2),('a','b'); + BEGIN; + PRAGMA writable_schema=ON; + UPDATE sqlite_schema SET sql='CREATE TABLE t1(a INT, b TEXT)' WHERE name LIKE 't1'; + PRAGMA schema_version=1234; + COMMIT; + PRAGMA integrity_check; +} {ok} +do_execsql_test 22.1 { + ALTER TABLE t1 ADD COLUMN c INT DEFAULT 78; + SELECT * FROM t1; +} {1 2 78 a b 78} + +#------------------------------------------------------------------------- +reset_db +db collate compare64 compare64 + +do_execsql_test 23.1 { + CREATE TABLE gigo(a text); + CREATE TABLE idx(x text COLLATE compare64); + CREATE VIEW v1 AS SELECT * FROM idx WHERE x='abc'; +} +db close +sqlite3 db test.db + +do_execsql_test 23.2 { + alter table gigo rename to ggiiggoo; + alter table idx rename to idx2; +} + +do_execsql_test 23.3 { + SELECT sql FROM sqlite_master; +} { + {CREATE TABLE "ggiiggoo"(a text)} + {CREATE TABLE "idx2"(x text COLLATE compare64)} + {CREATE VIEW v1 AS SELECT * FROM "idx2" WHERE x='abc'} +} + +do_execsql_test 23.4 { + ALTER TABLE idx2 RENAME x TO y; + SELECT sql FROM sqlite_master; +} { + {CREATE TABLE "ggiiggoo"(a text)} + {CREATE TABLE "idx2"(y text COLLATE compare64)} + {CREATE VIEW v1 AS SELECT * FROM "idx2" WHERE y='abc'} +} + +#------------------------------------------------------------------------- +# +reset_db +do_execsql_test 24.1.0 { + CREATE TABLE t1(a, b); + CREATE TRIGGER AFTER INSERT ON t1 BEGIN + INSERT INTO nosuchtable VALUES(new.a) ON CONFLICT(a) DO NOTHING; + END; +} +do_catchsql_test 24.1.1 { + ALTER TABLE t1 RENAME TO t2; +} {1 {error in trigger AFTER: no such table: main.nosuchtable}} + +reset_db +do_execsql_test 24.2.0 { + CREATE TABLE t1(a, b); + CREATE TRIGGER AFTER INSERT ON t1 BEGIN + INSERT INTO v1 VALUES(new.a) ON CONFLICT(a) DO NOTHING; + END; + CREATE VIEW v1 AS SELECT * FROM nosuchtable; +} +do_catchsql_test 24.2.1 { + ALTER TABLE t1 RENAME TO t2; +} {1 {error in trigger AFTER: no such table: main.nosuchtable}} + +#-------------------------------------------------------------------------- +# +reset_db +do_execsql_test 25.1 { + CREATE TABLE xx(x); + CREATE VIEW v3(b) AS WITH b AS (SELECT b FROM (SELECT * FROM t2)) VALUES(1); +} + +ifcapable json1&&vtab { + do_catchsql_test 25.2 { + ALTER TABLE json_each RENAME TO t4; + } {1 {table json_each may not be altered}} +} + +# 2021-05-01 dbsqlfuzz bc17a306a09329bba0ecc61547077f6178bcf321 +# Remove a NEVER() inserted on 2019-12-09 that is reachable after all. +# +reset_db +do_execsql_test 26.1 { + CREATE TABLE t1(k,v); + CREATE TABLE t2_a(k,v); + CREATE VIEW t2 AS SELECT * FROM t2_a; + CREATE TRIGGER r2 AFTER INSERT ON t1 BEGIN + UPDATE t1 + SET (k,v)=((WITH cte1(a) AS (SELECT 1 FROM t2) SELECT t2.k FROM t2, cte1),1); + END; + ALTER TABLE t1 RENAME TO t1x; + INSERT INTO t2_a VALUES(2,3); + INSERT INTO t1x VALUES(98,99); + SELECT * FROM t1x; +} {2 1} + +#------------------------------------------------------------------------- +reset_db + +do_execsql_test 27.1 { + + create table t_sa ( + c_muyat INTEGER NOT NULL, + c_d4u TEXT + ); + + create table t2 ( abc ); + + CREATE TRIGGER trig AFTER DELETE ON t_sa + BEGIN + DELETE FROM t_sa WHERE ( + SELECT 123 FROM t2 + WINDOW oamat7fzf AS ( PARTITION BY t_sa.c_d4u ) + ); + END; +} + +do_execsql_test 27.2 { + alter table t_sa rename column c_muyat to c_dg; +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 29.1 { + CREATE TABLE t1(a, b, c); + INSERT INTO t1 VALUES('a', 'b', 'c'); + + CREATE VIEW v0 AS + WITH p AS ( SELECT 1 FROM t1 ), + g AS ( SELECT 1 FROM p, t1 ) + SELECT 1 FROM g; +} + +do_execsql_test 29.2 { + SELECT * FROM v0 +} 1 + +do_execsql_test 29.2 { + ALTER TABLE t1 RENAME TO t2 +} + +do_execsql_test 29.3 { + SELECT sql FROM sqlite_schema WHERE name='v0' +} {{CREATE VIEW v0 AS + WITH p AS ( SELECT 1 FROM "t2" ), + g AS ( SELECT 1 FROM p, "t2" ) + SELECT 1 FROM g}} + +do_execsql_test 29.4 { + CREATE VIEW v2 AS + WITH p AS ( SELECT 1 FROM t2 ), + g AS ( SELECT 1 FROM ( + WITH i AS (SELECT 1 FROM p, t2) + SELECT * FROM i + ) + ) + SELECT 1 FROM g; +} + +do_execsql_test 29.4 { + SELECT * FROM v2; +} 1 + +do_execsql_test 29.5 { + ALTER TABLE t2 RENAME TO t3; +} + +do_execsql_test 29.5 { + SELECT sql FROM sqlite_schema WHERE name='v2' +} {{CREATE VIEW v2 AS + WITH p AS ( SELECT 1 FROM "t3" ), + g AS ( SELECT 1 FROM ( + WITH i AS (SELECT 1 FROM p, "t3") + SELECT * FROM i + ) + ) + SELECT 1 FROM g}} + + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 28.1 { + CREATE TABLE t1(a); + CREATE TABLE t2(b,c); + CREATE TABLE t4(b,c); + INSERT INTO t2 VALUES(1,2),(1,3),(2,5); + INSERT INTO t4 VALUES(1,2),(1,3),(2,5); + + CREATE VIEW v3 AS + WITH RECURSIVE t3(x,y,z) AS ( + SELECT b,c,NULL FROM t4 + UNION + SELECT x,y,NULL FROM t3, t2 + ) + SELECT * FROM t3 AS xyz; +} + +do_execsql_test 28.2 { + SELECT * FROM v3 +} { + 1 2 {} 1 3 {} 2 5 {} +} + +do_execsql_test 28.3 { + ALTER TABLE t1 RENAME a TO a2; -- fails in v3 +} + +do_execsql_test 28.4 { + ALTER TABLE t2 RENAME TO t5; +} + +do_execsql_test 28.5 { + SELECT sql FROM sqlite_schema WHERE name='v3' +} {{CREATE VIEW v3 AS + WITH RECURSIVE t3(x,y,z) AS ( + SELECT b,c,NULL FROM t4 + UNION + SELECT x,y,NULL FROM t3, "t5" + ) + SELECT * FROM t3 AS xyz}} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 30.0 { + CREATE TABLE t1(a,b,c,d,e,f); + CREATE TABLE t2(a,b,c); + CREATE INDEX t1abc ON t1(a,b,c+d+e); + CREATE VIEW v1(x,y) AS + SELECT t1.b,t2.b FROM t1,t2 WHERE t1.a=t2.a + GROUP BY 1 HAVING t2.c NOT NULL LIMIT 10; + CREATE TRIGGER r1 AFTER INSERT ON t1 WHEN 'no' NOT NULL BEGIN + INSERT INTO t2(a,a,b,c) VALUES(new.b,new.a,new.c-7); + WITH c1(x) AS ( + VALUES(0) + UNION ALL + SELECT current_time+x FROM c1 WHERE x + UNION ALL + SELECT 1+x FROM c1 WHERE x<1 + ), c2(x) AS (VALUES(0),(1)) + SELECT * FROM c1 AS x1, c2 AS x2, ( + SELECT x+1 FROM c1 WHERE x IS NOT TRUE + UNION ALL + SELECT 1+x FROM c1 WHERE 1<x + ) AS x3, c2 x5; + END; +} + +do_execsql_test 30.1 { + ALTER TABLE t1 RENAME TO t1x; +} + +do_execsql_test 30.2 { + SELECT sql FROM sqlite_schema ORDER BY rowid +} { + {CREATE TABLE "t1x"(a,b,c,d,e,f)} + {CREATE TABLE t2(a,b,c)} + {CREATE INDEX t1abc ON "t1x"(a,b,c+d+e)} + {CREATE VIEW v1(x,y) AS + SELECT "t1x".b,t2.b FROM "t1x",t2 WHERE "t1x".a=t2.a + GROUP BY 1 HAVING t2.c NOT NULL LIMIT 10} + {CREATE TRIGGER r1 AFTER INSERT ON "t1x" WHEN 'no' NOT NULL BEGIN + INSERT INTO t2(a,a,b,c) VALUES(new.b,new.a,new.c-7); + WITH c1(x) AS ( + VALUES(0) + UNION ALL + SELECT current_time+x FROM c1 WHERE x + UNION ALL + SELECT 1+x FROM c1 WHERE x<1 + ), c2(x) AS (VALUES(0),(1)) + SELECT * FROM c1 AS x1, c2 AS x2, ( + SELECT x+1 FROM c1 WHERE x IS NOT TRUE + UNION ALL + SELECT 1+x FROM c1 WHERE 1<x + ) AS x3, c2 x5; + END} +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 31.0 { + CREATE TABLE t1(q); + CREATE VIEW vvv AS WITH x AS (WITH y AS (SELECT * FROM x) SELECT 1) SELECT 1; +} + +do_execsql_test 31.1 { + SELECT * FROM vvv; +} {1} + +do_execsql_test 31.2 { + ALTER TABLE t1 RENAME TO t1x; +} + +do_execsql_test 31.3 { + ALTER TABLE t1x RENAME q TO x; +} + +# 2021-07-02 OSSFuzz https://oss-fuzz.com/testcase-detail/5517690440646656 +# Bad assert() statement +# +reset_db +do_catchsql_test 32.0 { + CREATE TABLE t1(x); + CREATE TRIGGER r1 BEFORE INSERT ON t1 BEGIN + UPDATE t1 SET x=x FROM (SELECT*); + END; + ALTER TABLE t1 RENAME TO x; +} {1 {error in trigger r1: no tables specified}} + +# 2023-04-13 https://sqlite.org/forum/forumpost/ff3840145a +# +reset_db +do_execsql_test 33.0 { + CREATE TABLE t1(a TEXT); + INSERT INTO t1(a) VALUES('abc'),('def'),(NULL); + CREATE TABLE t2(b TEXT); + CREATE TRIGGER r3 AFTER INSERT ON t1 BEGIN + UPDATE t2 SET (b,a)=(SELECT 1) FROM t1 JOIN t2 ON (SELECT * FROM (SELECT a)); + END; +} +do_catchsql_test 33.1 { + ALTER TABLE t1 RENAME COLUMN a TO b; +} {1 {error in trigger r3 after rename: no such column: a}} +do_execsql_test 33.2 { + SELECT quote(a) FROM t1 ORDER BY +a; +} {NULL 'abc' 'def'} + +finish_test diff --git a/test/altertab2.test b/test/altertab2.test new file mode 100644 index 0000000000..f2a1d74c40 --- /dev/null +++ b/test/altertab2.test @@ -0,0 +1,385 @@ +# 2018 September 30 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix altertab2 + +# If SQLITE_OMIT_ALTERTABLE is defined, omit this file. +ifcapable !altertable { + finish_test + return +} + +ifcapable fts5 { + do_execsql_test 1.0 { + CREATE TABLE rr(a, b); + CREATE VIRTUAL TABLE ff USING fts5(a, b); + CREATE TRIGGER tr1 AFTER INSERT ON rr BEGIN + INSERT INTO ff VALUES(new.a, new.b); + END; + INSERT INTO rr VALUES('hello', 'world'); + SELECT * FROM ff; + } {hello world} + + do_execsql_test 1.1 { + ALTER TABLE ff RENAME TO ffff; + } + + do_execsql_test 1.2 { + INSERT INTO rr VALUES('in', 'tcl'); + SELECT * FROM ffff; + } {hello world in tcl} +} + +#------------------------------------------------------------------------- +# Check that table names that appear in REFERENCES clauses are updated +# when a table is renamed unless: +# +# a) "PRAGMA legacy_alter_table" is true, and +# b) "PRAGMA foreign_keys" is false. +# +do_execsql_test 2.0 { + CREATE TABLE p1(a PRIMARY KEY, b); + CREATE TABLE c1(x REFERENCES p1); + CREATE TABLE c2(x, FOREIGN KEY (x) REFERENCES p1); + CREATE TABLE c3(x, FOREIGN KEY (x) REFERENCES p1(a)); +} + +do_execsql_test 2.1 { + ALTER TABLE p1 RENAME TO p2; + SELECT sql FROM sqlite_master WHERE name LIKE 'c%'; +} { + {CREATE TABLE c1(x REFERENCES "p2")} + {CREATE TABLE c2(x, FOREIGN KEY (x) REFERENCES "p2")} + {CREATE TABLE c3(x, FOREIGN KEY (x) REFERENCES "p2"(a))} +} + +do_execsql_test 2.2 { + PRAGMA legacy_alter_table = 1; + ALTER TABLE p2 RENAME TO p3; + SELECT sql FROM sqlite_master WHERE name LIKE 'c%'; +} { + {CREATE TABLE c1(x REFERENCES "p2")} + {CREATE TABLE c2(x, FOREIGN KEY (x) REFERENCES "p2")} + {CREATE TABLE c3(x, FOREIGN KEY (x) REFERENCES "p2"(a))} +} + +do_execsql_test 2.3 { + ALTER TABLE p3 RENAME TO p2; + PRAGMA foreign_keys = 1; + ALTER TABLE p2 RENAME TO p3; + SELECT sql FROM sqlite_master WHERE name LIKE 'c%'; +} { + {CREATE TABLE c1(x REFERENCES "p3")} + {CREATE TABLE c2(x, FOREIGN KEY (x) REFERENCES "p3")} + {CREATE TABLE c3(x, FOREIGN KEY (x) REFERENCES "p3"(a))} +} + +#------------------------------------------------------------------------- +# Table name in WITH clauses that are part of views or triggers. +# +foreach {tn schema} { + 1 { + CREATE TABLE log_entry(col1, y); + CREATE INDEX i1 ON log_entry(col1); + } + + 2 { + CREATE TABLE t1(a, b, c); + CREATE TABLE t2(x); + CREATE TABLE log_entry(col1); + CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN + INSERT INTO t2 SELECT col1 FROM log_entry; + END; + } + + 3 { + CREATE TABLE t1(a, b, c); + CREATE TABLE t2(x); + CREATE TABLE log_entry(col1); + CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN + INSERT INTO t2 + WITH xyz(x) AS (SELECT col1 FROM log_entry) + SELECT x FROM xyz; + END; + } + + 4 { + CREATE TABLE log_entry(col1); + CREATE VIEW ttt AS + WITH xyz(x) AS (SELECT col1 FROM log_entry) + SELECT x FROM xyz; + } +} { + reset_db + do_execsql_test 3.$tn.1 $schema + set expect [db eval "SELECT sql FROM sqlite_master"] + set expect [string map {log_entry {"newname"}} $expect] + + do_execsql_test 3.$tn.2 { + ALTER TABLE log_entry RENAME TO newname; + SELECT sql FROM sqlite_master; + } $expect + + reset_db + do_execsql_test 3.$tn.3 $schema + set expect [db eval "SELECT sql FROM sqlite_master"] + set expect [string map {col1 newname} $expect] + + do_execsql_test 3.$tn.4 { + ALTER TABLE log_entry RENAME col1 TO newname; + SELECT sql FROM sqlite_master; + } $expect +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 4.0 { + CREATE TABLE t1(a,b,c,d,e,f); + CREATE TRIGGER r1 AFTER INSERT ON t1 WHEN new.a NOT NULL BEGIN + UPDATE t1 SET (c,d)=(a,b); + END; +} + +do_execsql_test 4.1 { + ALTER TABLE t1 RENAME TO t1x; + SELECT sql FROM sqlite_master WHERE type = 'trigger'; +} { +{CREATE TRIGGER r1 AFTER INSERT ON "t1x" WHEN new.a NOT NULL BEGIN + UPDATE "t1x" SET (c,d)=(a,b); + END} +} + +do_execsql_test 4.2 { + ALTER TABLE t1x RENAME a TO aaa; + SELECT sql FROM sqlite_master WHERE type = 'trigger'; +} { +{CREATE TRIGGER r1 AFTER INSERT ON "t1x" WHEN new.aaa NOT NULL BEGIN + UPDATE "t1x" SET (c,d)=(aaa,b); + END} +} + +do_execsql_test 4.3 { + ALTER TABLE t1x RENAME d TO ddd; + SELECT sql FROM sqlite_master WHERE type = 'trigger'; +} { +{CREATE TRIGGER r1 AFTER INSERT ON "t1x" WHEN new.aaa NOT NULL BEGIN + UPDATE "t1x" SET (c,ddd)=(aaa,b); + END} +} + +#------------------------------------------------------------------------- +ifcapable windowfunc { +do_execsql_test 5.0 { + CREATE TABLE t2(a); + CREATE TRIGGER r2 AFTER INSERT ON t2 WHEN new.a NOT NULL BEGIN + SELECT a, sum(a) OVER w1 FROM t2 + WINDOW w1 AS ( + PARTITION BY a ORDER BY a + ROWS BETWEEN 2 PRECEDING AND 3 FOLLOWING + ), + w2 AS ( + PARTITION BY a + ORDER BY rowid ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + ); + END; +} {} + +do_execsql_test 5.0.1 { + INSERT INTO t2 VALUES(1); +} {} + +do_execsql_test 5.1 { + ALTER TABLE t2 RENAME TO t2x; + SELECT sql FROM sqlite_master WHERE name = 'r2'; +} { + {CREATE TRIGGER r2 AFTER INSERT ON "t2x" WHEN new.a NOT NULL BEGIN + SELECT a, sum(a) OVER w1 FROM "t2x" + WINDOW w1 AS ( + PARTITION BY a ORDER BY a + ROWS BETWEEN 2 PRECEDING AND 3 FOLLOWING + ), + w2 AS ( + PARTITION BY a + ORDER BY rowid ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + ); + END} +} + +do_execsql_test 5.2 { + ALTER TABLE t2x RENAME a TO aaaa; + SELECT sql FROM sqlite_master WHERE name = 'r2'; +} { + {CREATE TRIGGER r2 AFTER INSERT ON "t2x" WHEN new.aaaa NOT NULL BEGIN + SELECT aaaa, sum(aaaa) OVER w1 FROM "t2x" + WINDOW w1 AS ( + PARTITION BY aaaa ORDER BY aaaa + ROWS BETWEEN 2 PRECEDING AND 3 FOLLOWING + ), + w2 AS ( + PARTITION BY aaaa + ORDER BY rowid ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + ); + END} +} + +do_execsql_test 5.3 { + INSERT INTO t2x VALUES(1); +} {} +} ;# windowfunc + +#------------------------------------------------------------------------- + +do_execsql_test 6.0 { + CREATE TABLE t3(a,b,c,d); + CREATE TRIGGER r3 AFTER INSERT ON t3 WHEN new.a NOT NULL BEGIN + SELECT a,b,c FROM t3 EXCEPT SELECT a,b,c FROM t3 ORDER BY a; + SELECT rowid, * FROM t3; + END; +} {} + +do_execsql_test 6.1 { + ALTER TABLE t3 RENAME TO t3x; + SELECT sql FROM sqlite_master WHERE name = 'r3'; +} { + {CREATE TRIGGER r3 AFTER INSERT ON "t3x" WHEN new.a NOT NULL BEGIN + SELECT a,b,c FROM "t3x" EXCEPT SELECT a,b,c FROM "t3x" ORDER BY a; + SELECT rowid, * FROM "t3x"; + END} +} + +do_execsql_test 6.2 { + ALTER TABLE t3x RENAME a TO abcd; + SELECT sql FROM sqlite_master WHERE name = 'r3'; +} { + {CREATE TRIGGER r3 AFTER INSERT ON "t3x" WHEN new.abcd NOT NULL BEGIN + SELECT abcd,b,c FROM "t3x" EXCEPT SELECT abcd,b,c FROM "t3x" ORDER BY abcd; + SELECT rowid, * FROM "t3x"; + END} +} + +#------------------------------------------------------------------------- +reset_db + +do_execsql_test 7.0 { + CREATE TABLE t1(a,b,c,d,e,f); + INSERT INTO t1 VALUES(1,2,3,4,5,6); + CREATE TABLE t2(x,y,z); +} + +do_execsql_test 7.1 { + SELECT a,b,c FROM t1 UNION SELECT d,e,f FROM t1 ORDER BY b,c; +} {1 2 3 4 5 6} + +do_execsql_test 7.2 { + CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN + INSERT INTO t2 + SELECT a,b,c FROM t1 UNION SELECT d,e,f FROM t1 ORDER BY b,c; + END; + INSERT INTO t1 VALUES(2,3,4,5,6,7); + SELECT * FROM t2; +} {1 2 3 2 3 4 4 5 6 5 6 7} + +do_execsql_test 7.3 { + ALTER TABLE t1 RENAME TO xyzzy; + SELECT sql FROM sqlite_master WHERE name='r1' +} { + {CREATE TRIGGER r1 AFTER INSERT ON "xyzzy" BEGIN + INSERT INTO t2 + SELECT a,b,c FROM "xyzzy" UNION SELECT d,e,f FROM "xyzzy" ORDER BY b,c; + END} +} + +do_execsql_test 7.3 { + ALTER TABLE xyzzy RENAME c TO ccc; + SELECT sql FROM sqlite_master WHERE name='r1' +} { + {CREATE TRIGGER r1 AFTER INSERT ON "xyzzy" BEGIN + INSERT INTO t2 + SELECT a,b,ccc FROM "xyzzy" UNION SELECT d,e,f FROM "xyzzy" ORDER BY b,ccc; + END} +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 8.0 { + CREATE TABLE t1(a, b, c); + CREATE TABLE t2(a, b, c); + CREATE TABLE t3(d, e, f); + CREATE VIEW v1 AS SELECT * FROM t1; + CREATE TRIGGER tr AFTER INSERT ON t3 BEGIN + UPDATE t2 SET a = new.d; + SELECT a, b, c FROM v1; + END; +} + +do_execsql_test 8.1 { + INSERT INTO t3 VALUES(1, 2, 3); +} + +# The following ALTER TABLE fails as if column "t1.a" is renamed the "a" +# in the "SELECT a, b, c FROM v1" within the trigger can no longer be +# resolved. But at one point there was a bug allowing the ALTER TABLE +# succeed. Which meant the subsequent INSERT statement would fail. +do_catchsql_test 8.2 { + ALTER TABLE t1 RENAME a TO aaa; +} {1 {error in trigger tr after rename: no such column: a}} +do_execsql_test 8.3 { + INSERT INTO t3 VALUES(4, 5, 6); +} + +do_execsql_test 8.4 { + CREATE TABLE t4(a, b); + CREATE VIEW v4 AS SELECT * FROM t4 WHERE (a=1 AND 0) OR b=2; +} + +# Branches of an expression tree that are optimized out by the AND +# optimization are renamed. +# +do_execsql_test 8.5 { + ALTER TABLE t4 RENAME a TO c; + SELECT sql FROM sqlite_master WHERE name = 'v4' +} {{CREATE VIEW v4 AS SELECT * FROM t4 WHERE (c=1 AND 0) OR b=2}} + +# 2019-06-10 https://sqlite.org/src/info/533010b8cacebe82 +reset_db +do_catchsql_test 8.6 { + CREATE TABLE t0(c0); + CREATE INDEX i0 ON t0(likelihood(1,2) AND 0); + ALTER TABLE t0 RENAME TO t1; + SELECT sql FROM sqlite_master WHERE name='i0'; +} {1 {second argument to likelihood() must be a constant between 0.0 and 1.0}} + + +reset_db + +do_execsql_test 9.0 { + CREATE TABLE t1(a,b,c,d); + CREATE TABLE t2(a,b,c,d,x); + + CREATE TRIGGER AFTER INSERT ON t2 BEGIN + + SELECT group_conct( + 123 ORDER BY ( + SELECT 1 FROM ( VALUES(a, 'b'), ('c') ) + )) + FROM t1; + + END; +} + +do_catchsql_test 9.1 { + ALTER TABLE t2 RENAME TO newname; +} {1 {error in trigger AFTER: all VALUES must have the same number of terms}} + +finish_test diff --git a/test/altertab3.test b/test/altertab3.test new file mode 100644 index 0000000000..92060fb41c --- /dev/null +++ b/test/altertab3.test @@ -0,0 +1,781 @@ +# 2019 January 23 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix altertab3 + +# If SQLITE_OMIT_ALTERTABLE is defined, omit this file. +ifcapable !altertable { + finish_test + return +} + +ifcapable windowfunc { +do_execsql_test 1.0 { + CREATE TABLE t1(a, b); + CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN + SELECT sum(b) OVER w FROM t1 WINDOW w AS (ORDER BY a); + END; +} + +do_execsql_test 1.1 { + ALTER TABLE t1 RENAME a TO aaa; +} + +do_execsql_test 1.2 { + SELECT sql FROM sqlite_master WHERE name='tr1' +} {{CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN + SELECT sum(b) OVER w FROM t1 WINDOW w AS (ORDER BY aaa); + END}} + +do_execsql_test 1.3 { + INSERT INTO t1 VALUES(1, 2); +} +} ;# windowfunc + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 2.0 { + CREATE TABLE t1(a,b,c); + CREATE TABLE t2(a,b,c); + CREATE TRIGGER r1 AFTER INSERT ON t1 WHEN new.a NOT NULL BEGIN + SELECT a,b, a name FROM t1 + INTERSECT + SELECT a,b,c FROM t1 WHERE b>='d' ORDER BY name; + SELECT new.c; + END; +} + +do_execsql_test 2.1 { + ALTER TABLE t1 RENAME TO t1x; + SELECT sql FROM sqlite_master WHERE name = 'r1'; +} {{CREATE TRIGGER r1 AFTER INSERT ON "t1x" WHEN new.a NOT NULL BEGIN + SELECT a,b, a name FROM "t1x" + INTERSECT + SELECT a,b,c FROM "t1x" WHERE b>='d' ORDER BY name; + SELECT new.c; + END}} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 3.0 { + CREATE TABLE t1(a, b, c, d); + CREATE VIEW v1 AS SELECT * FROM t1 WHERE a=1 OR (b IN ()); +} + +do_execsql_test 3.1 { + ALTER TABLE t1 RENAME b TO bbb; +} + +do_execsql_test 3.2 { + SELECT sql FROM sqlite_master WHERE name = 'v1' +} {{CREATE VIEW v1 AS SELECT * FROM t1 WHERE a=1 OR (b IN ())}} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 4.0 { + CREATE TABLE t1(a, b); + CREATE TABLE t3(e, f); + CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN + INSERT INTO t2 VALUES(new.a, new.b); + END; +} + +do_catchsql_test 4.1.2 { + BEGIN; + ALTER TABLE t3 RENAME TO t4; +} {1 {error in trigger tr1: no such table: main.t2}} +do_execsql_test 4.1.2 { + COMMIT; +} +do_execsql_test 4.1.3 { + SELECT type, name, tbl_name, sql + FROM sqlite_master WHERE type='table' AND name!='t1'; +} {table t3 t3 {CREATE TABLE t3(e, f)}} + + +do_catchsql_test 4.2.1 { + BEGIN; + ALTER TABLE t3 RENAME e TO eee; +} {1 {error in trigger tr1: no such table: main.t2}} +do_execsql_test 4.2.2 { + COMMIT; +} +do_execsql_test 4.2.3 { + SELECT type, name, tbl_name, sql + FROM sqlite_master WHERE type='table' AND name!='t1'; +} {table t3 t3 {CREATE TABLE t3(e, f)}} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 5.0 { + CREATE TABLE t1 ( + c1 integer, c2, PRIMARY KEY(c1 collate rtrim), + UNIQUE(c2) + ) +} +do_execsql_test 5.1 { + ALTER TABLE t1 RENAME c1 TO c3; +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 6.0 { + CREATE TEMPORARY TABLE Table0 ( + Col0 INTEGER, + PRIMARY KEY(Col0 COLLATE RTRIM), + FOREIGN KEY (Col0) REFERENCES Table0 + ); +} + +do_execsql_test 6.1 { + ALTER TABLE Table0 RENAME Col0 TO Col0; +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 7.1.0 { + CREATE TABLE t1(a,b,c); + CREATE TRIGGER AFTER INSERT ON t1 BEGIN + SELECT a, rank() OVER w1 FROM t1 + WINDOW w1 AS (PARTITION BY b, percent_rank() OVER w1); + END; +} + +do_execsql_test 7.1.2 { + ALTER TABLE t1 RENAME TO t1x; + SELECT sql FROM sqlite_master; +} { + {CREATE TABLE "t1x"(a,b,c)} + {CREATE TRIGGER AFTER INSERT ON "t1x" BEGIN + SELECT a, rank() OVER w1 FROM "t1x" + WINDOW w1 AS (PARTITION BY b, percent_rank() OVER w1); + END} +} + +do_execsql_test 7.2.1 { + DROP TRIGGER after; + CREATE TRIGGER AFTER INSERT ON t1x BEGIN + SELECT a, rank() OVER w1 FROM t1x + WINDOW w1 AS (PARTITION BY b, percent_rank() OVER w1 ORDER BY d); + END; +} + +do_catchsql_test 7.2.2 { + ALTER TABLE t1x RENAME TO t1; +} {1 {error in trigger AFTER: no such column: d}} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 8.0 { + CREATE TABLE t0(c0); + CREATE INDEX i0 ON t0('1' IN ()); +} +do_execsql_test 8.1 { + ALTER TABLE t0 RENAME TO t1; + SELECT sql FROM sqlite_master; +} { + {CREATE TABLE "t1"(c0)} + {CREATE INDEX i0 ON "t1"('1' IN ())} +} +do_execsql_test 8.2.1 { + CREATE TABLE t2 (c0); + CREATE INDEX i2 ON t2((LIKELIHOOD(c0, 1.0) IN ())); + ALTER TABLE t2 RENAME COLUMN c0 TO c1; +} +do_execsql_test 8.2.2 { + SELECT sql FROM sqlite_master WHERE tbl_name = 't2'; +} { + {CREATE TABLE t2 (c1)} + {CREATE INDEX i2 ON t2((LIKELIHOOD(c1, 1.0) IN ()))} +} +do_test 8.2.3 { + sqlite3 db2 test.db + db2 eval { INSERT INTO t2 VALUES (1), (2), (3) } + db close +} {} +db2 close + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 9.1 { + CREATE TABLE t1(a,b,c); + CREATE TRIGGER AFTER INSERT ON t1 WHEN new.a NOT NULL BEGIN + SELECT true WHERE (SELECT a, b FROM (t1)) IN (); + END; +} +do_execsql_test 9.2 { + ALTER TABLE t1 RENAME TO t1x; +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 10.1 { + CREATE TABLE t1(a, b, c); + CREATE TABLE t2(a, b, c); + CREATE VIEW v1 AS SELECT * FROM t1 WHERE ( + SELECT t1.a FROM t1, t2 + ) IN () OR t1.a=5; +} + +do_execsql_test 10.2 { + ALTER TABLE t2 RENAME TO t3; + SELECT sql FROM sqlite_master WHERE name='v1'; +} { + {CREATE VIEW v1 AS SELECT * FROM t1 WHERE ( + SELECT t1.a FROM t1, t2 + ) IN () OR t1.a=5} +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 11.1 { + CREATE TABLE t1( + a,b,c,d,e,f,g,h,j,jj,jjb,k,aa,bb,cc,dd,ee DEFAULT 3.14, + ff DEFAULT('hiccup'),Wg NOD NULL DEFAULT(false) + ); + + CREATE TRIGGER b AFTER INSERT ON t1 WHEN new.a BEGIN + SELECT a, sum() w3 FROM t1 + WINDOW b AS (ORDER BY NOT EXISTS(SELECT 1 FROM abc)); + END; +} + +do_catchsql_test 11.2 { + ALTER TABLE t1 RENAME TO t1x; +} {1 {error in trigger b: no such table: main.abc}} + +do_execsql_test 11.3 { + DROP TRIGGER b; + CREATE TRIGGER b AFTER INSERT ON t1 WHEN new.a BEGIN + SELECT a, sum() w3 FROM t1 + WINDOW b AS (ORDER BY NOT EXISTS(SELECT 1 FROM t1)); + END; +} {} + +do_execsql_test 11.4 { + ALTER TABLE t1 RENAME TO t1x; + SELECT sql FROM sqlite_master WHERE name = 'b'; +} { +{CREATE TRIGGER b AFTER INSERT ON "t1x" WHEN new.a BEGIN + SELECT a, sum() w3 FROM "t1x" + WINDOW b AS (ORDER BY NOT EXISTS(SELECT 1 FROM "t1x")); + END} +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 12.1 { +CREATE TABLE t1(a,b,c,d,e,f,g,h,j,jj,Zjj,k,aQ,bb,cc,dd,ee DEFAULT 3.14, +ff DEFAULT('hiccup'),gg NOD NULL DEFAULT(false)); +CREATE TRIGGER AFTER INSERT ON t1 WHEN new.a NOT NULL BEGIN + +SELECT b () OVER , dense_rank() OVER d, d () OVER w1 +FROM t1 +WINDOW +w1 AS +( w1 ORDER BY d +ROWS BETWEEN 2 NOT IN(SELECT a, sum(d) w2,max(d)OVER FROM t1 +WINDOW +w1 AS +(PARTITION BY d +ROWS BETWEEN '' PRECEDING AND false FOLLOWING), +d AS +(PARTITION BY b ORDER BY d +ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) +) PRECEDING AND 1 FOLLOWING), +w2 AS +(PARTITION BY b ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), +w3 AS +(PARTITION BY b ORDER BY d +ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) +; +SELECT a, sum(d) w2,max(d)OVER FROM t1 +WINDOW +w1 AS +(PARTITION BY d +ROWS BETWEEN '' PRECEDING AND false FOLLOWING), +d AS +(PARTITION BY b ORDER BY d +ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) +; + +END; +} + +do_execsql_test 12.2 { + ALTER TABLE t1 RENAME TO t1x; +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 13.1 { + CREATE TABLE t1(a); + CREATE TRIGGER r1 INSERT ON t1 BEGIN + SELECT a(*) OVER (ORDER BY (SELECT 1)) FROM t1; + END; +} + +do_execsql_test 13.2 { + ALTER TABLE t1 RENAME TO t1x; +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 14.1 { + CREATE TABLE t1(a); + CREATE TABLE t2(b); + CREATE TRIGGER AFTER INSERT ON t1 BEGIN + SELECT sum() FILTER (WHERE (SELECT sum() FILTER (WHERE 0)) AND a); + END; +} + +do_catchsql_test 14.2 { + ALTER TABLE t1 RENAME TO t1x; +} {1 {error in trigger AFTER: no such column: a}} + +#------------------------------------------------------------------------- +reset_db + +do_execsql_test 16.1 { + CREATE TABLE t1(x); + CREATE TRIGGER AFTER INSERT ON t1 BEGIN + SELECT (WITH t2 AS (WITH t3 AS (SELECT true) + SELECT * FROM t3 ORDER BY true COLLATE nocase) + SELECT 11); + + WITH t4 AS (SELECT * FROM t1) SELECT 33; + END; +} +do_execsql_test 16.2 { + ALTER TABLE t1 RENAME TO t1x; +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 17.1 { + CREATE TABLE t1(a,b,c); + CREATE TRIGGER AFTER INSERT ON t1 WHEN new.a NOT NULL BEGIN + SELECT a () FILTER (WHERE a>0) FROM t1; + END; +} + +do_execsql_test 17.2 { + ALTER TABLE t1 RENAME TO t1x; + ALTER TABLE t1x RENAME a TO aaa; + SELECT sql FROM sqlite_master WHERE type='trigger'; +} { +{CREATE TRIGGER AFTER INSERT ON "t1x" WHEN new.aaa NOT NULL BEGIN + SELECT a () FILTER (WHERE aaa>0) FROM "t1x"; + END} +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 18.1 { + CREATE TABLE t1(a,b); + CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN + SELECT a, b FROM t1 + INTERSECT SELECT b,a FROM t1 + ORDER BY b IN ( + SELECT a UNION SELECT b + FROM t1 + ORDER BY b COLLATE nocase + ) + ; + END; +} + +do_catchsql_test 18.2 { + SELECT a, b FROM t1 + INTERSECT + SELECT b,a FROM t1 + ORDER BY b IN ( + SELECT a UNION SELECT b + FROM t1 + ORDER BY b COLLATE nocase + ); +} {1 {1st ORDER BY term does not match any column in the result set}} + +do_catchsql_test 18.3 { + ALTER TABLE t1 RENAME TO t1x; +} {1 {error in trigger r1: 1st ORDER BY term does not match any column in the result set}} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 19.0 { + CREATE TABLE a(a,h CONSTRAINT a UNIQUE ON CONFLICT FAIL,CONSTRAINT a); +} + +foreach {tn v res} { + 1 { + CREATE VIEW q AS SELECT 123 + + WINDOW x AS ( + RANGE BETWEEN UNBOUNDED PRECEDING AND INDEXED() OVER( + PARTITION BY ( WITH x AS(VALUES(col1)) VALUES(453) ) + ) + FOLLOWING + ) + } {1 {error in view q: no such column: col1}} + + 2 { + CREATE VIEW q AS SELECT + CAST(CAST(CAST(CAST(CAST(CAST(CAST(CAST(CAST(CAST(CAST(RIGHT + AS)AS)AS)AS)AS)AS)AS)AS)AS)AS)AS)WINDOW x AS(RANGE BETWEEN UNBOUNDED + PRECEDING AND INDEXED(*)OVER(PARTITION BY + CROSS,CROSS,NATURAL,sqlite_master(*)OVER a,(WITH a AS(VALUES(LEFT)UNION + VALUES(LEFT)UNION VALUES(LEFT)UNION VALUES(LEFT)UNION VALUES(LEFT)UNION + VALUES(LEFT)UNION VALUES(LEFT))VALUES(LEFT))IN + STORED,LEFT,LEFT,LEFT,LEFT,LEFT,LEFT)*LEFT FOLLOWING)ORDER BY + LEFT,LEFT,LEFT,LEFT,LEFT,LEFT,LEFT,LEFT,LEFT,LEFT,LEFT LIMIT + LEFT,INDEXED(*)OVER(PARTITION BY + CROSS,CROSS,CROSS,LEFT,INDEXED(*)OVER(PARTITION BY + CROSS,CROSS,CROSS),INDEXED(*)OVER(PARTITION BY + LEFT,LEFT,LEFT,LEFT,LEFT,LEFT,LEFT,LEFT,LEFT,LEFT,LEFT), + LEFT,LEFT,INNER,CROSS,CROSS,CROSS,INNER,NATURAL ORDER BY + OUTER,NATURAL,NATURAL,NATURAL,NATURAL,NATURAL,NATURAL,NATURAL,INNER, + INNER,INNER NULLS LAST GROUPS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED + FOLLOWING); + } {1 {error in view q: no such column: LEFT}} + + 3 { + CREATE VIEW q AS SELECT 99 WINDOW x AS (RANGE BETWEEN UNBOUNDED PRECEDING + AND count(*)OVER(PARTITION BY (WITH a AS(VALUES(2),(x3))VALUES(0))) + FOLLOWING)ORDER BY x2,sum(1)OVER(PARTITION BY avg(5)OVER(PARTITION BY x1)); + } {1 {error in view q: no such column: x3}} +} { + do_execsql_test 19.$tn.1 " + DROP VIEW IF EXISTS q; + $v + " {} + + do_catchsql_test 19.$tn.2 { + ALTER TABLE a RENAME TO g; + } $res +} + +# Verify that the "if( pParse->nErr ) return WRC_Abort" at the top of the +# renameUnmapSelectCb() routine in alter.c (2019-12-04) is really required. +# +sqlite3 db :memory: +do_catchsql_test 20.10 { + CREATE TABLE s(a, b, c); + CREATE INDEX k ON s( (WITH s AS( SELECT * ) VALUES(2) ) IN () ); + ALTER TABLE s RENAME a TO a2; +} {1 {error in index k: no tables specified}} + +#------------------------------------------------------------------------ +# +reset_db +do_execsql_test 21.1 { + CREATE TABLE s(col); + CREATE VIEW v AS SELECT ( + WITH x(a) AS(SELECT * FROM s) VALUES(RIGHT) + ) IN() ; + CREATE TABLE a(a); + ALTER TABLE a RENAME a TO b; +} + +#------------------------------------------------------------------------ +# +reset_db +do_execsql_test 22.1 { + CREATE TABLE t1(a); + CREATE VIEW v2(b) AS SELECT * FROM v2; +} + +do_catchsql_test 22.2 { + ALTER TABLE t1 RENAME TO t4; +} {1 {error in view v2: view v2 is circularly defined}} + +do_execsql_test 22.3 { + DROP VIEW v2; + CREATE VIEW v2(b) AS WITH t3 AS (SELECT b FROM v2) SELECT * FROM t3; +} + +do_catchsql_test 22.4 { + ALTER TABLE t1 RENAME TO t4; +} {1 {error in view v2: view v2 is circularly defined}} + +do_execsql_test 22.5 { + DROP VIEW v2; + CREATE VIEW v2(b) AS WITH t3 AS (SELECT b FROM v2) VALUES(1); +} + +do_catchsql_test 22.6 { + ALTER TABLE t1 RENAME TO t4; +} {0 {}} + +#------------------------------------------------------------------------ +# +reset_db +do_execsql_test 23.1 { + CREATE TABLE t1(x); + CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN + UPDATE t1 SET (c,d)=((SELECT 1 FROM t1 JOIN t2 ON b=x),1); + END; +} + +do_catchsql_test 23.2 { + ALTER TABLE t1 RENAME TO t1x; +} {1 {error in trigger r1: no such table: main.t2}} + +#------------------------------------------------------------------------ +# +reset_db +do_execsql_test 23.1 { + CREATE TABLE v0 (a); + CREATE VIEW v2 (v3) AS + WITH x1 AS (SELECT * FROM v2) + SELECT v3 AS x, v3 AS y FROM v2; +} + +do_catchsql_test 23.2 { + SELECT * FROM v2 +} {1 {view v2 is circularly defined}} + +db close +sqlite3 db test.db + +do_catchsql_test 23.3 { + ALTER TABLE v0 RENAME TO t3 ; +} {1 {error in view v2: view v2 is circularly defined}} + +#------------------------------------------------------------------------ +# +reset_db +do_execsql_test 24.1 { + CREATE TABLE v0 (v1); + CREATE TABLE v2 (v3 INTEGER UNIQUE ON CONFLICT ABORT); + CREATE TRIGGER x AFTER INSERT ON v2 WHEN ( + ( SELECT v1 AS PROMO_REVENUE FROM v2 JOIN v0 USING ( VALUE ) ) AND 0 ) + BEGIN + DELETE FROM v2; + END; +} +do_catchsql_test 24.2 { + ALTER TABLE v0 RENAME TO x ; +} {1 {error in trigger x: cannot join using column VALUE - column not present in both tables}} + +do_execsql_test 24.3 { + DROP TRIGGER x; + CREATE TRIGGER x AFTER INSERT ON v2 WHEN ( + 0 AND (SELECT rowid FROM v0) + ) BEGIN + DELETE FROM v2; + END; +} + +do_execsql_test 24.4 { + ALTER TABLE v0 RENAME TO xyz; + SELECT sql FROM sqlite_master WHERE type='trigger' +} {{CREATE TRIGGER x AFTER INSERT ON v2 WHEN ( + 0 AND (SELECT rowid FROM "xyz") + ) BEGIN + DELETE FROM v2; + END}} + +#------------------------------------------------------------------------ +# +reset_db +do_execsql_test 25.1 { + CREATE TABLE t1(a, b, c); + CREATE TABLE t2(a, b, c); + CREATE TRIGGER ttt AFTER INSERT ON t1 BEGIN + UPDATE t1 SET a=t2.a FROM t2 WHERE t1.a=t2.a; + END; +} +#do_execsql_test 25.2 { +# ALTER TABLE t2 RENAME COLUMN a TO aaa; +#} + +#------------------------------------------------------------------------ +# +reset_db +do_execsql_test 26.1 { + CREATE TABLE t1(x); + + CREATE TABLE t3(y); + CREATE TABLE t4(z); + + CREATE TRIGGER tr1 INSERT ON t3 BEGIN + UPDATE t3 SET y=z FROM (SELECT z FROM t4); + END; + + CREATE TRIGGER tr2 INSERT ON t3 BEGIN + UPDATE t3 SET y=abc FROM (SELECT x AS abc FROM t1); + END; +} + +do_execsql_test 26.2 { + ALTER TABLE t1 RENAME TO t2; +} + +do_execsql_test 26.3 { + ALTER TABLE t2 RENAME x TO xx; +} + +do_execsql_test 26.4 { + SELECT sql FROM sqlite_schema WHERE name='tr2' +} { +{CREATE TRIGGER tr2 INSERT ON t3 BEGIN + UPDATE t3 SET y=abc FROM (SELECT xx AS abc FROM "t2"); + END} +} + +# 2020-11-02 OSSFuzz +# +reset_db +do_execsql_test 26.5 { + CREATE TABLE t1(xx); + CREATE TRIGGER xx INSERT ON t1 BEGIN + UPDATE t1 SET xx=xx FROM(SELECT xx); + END; +} {} +do_catchsql_test 26.6 { + ALTER TABLE t1 RENAME TO t2; +} {1 {error in trigger xx: no such column: xx}} + + +#------------------------------------------------------------------------- +reset_db + +do_execsql_test 27.1 { + CREATE TABLE t1(a, b AS ((WITH w1 (xyz) AS ( SELECT t1.b FROM t1 ) SELECT 123) IN ()), c); +} + +do_execsql_test 27.2 { + ALTER TABLE t1 DROP COLUMN c; + SELECT sql FROM sqlite_schema WHERE name = 't1'; +} { + {CREATE TABLE t1(a, b AS ((WITH w1 (xyz) AS ( SELECT t1.b FROM t1 ) SELECT 123) IN ()))} +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 28.1 { + CREATE TABLE t1(a,b,c,d); + CREATE TRIGGER AFTER INSERT ON t1 BEGIN + UPDATE t1 SET (c,d)=(a,b); + END; + ALTER TABLE t1 RENAME TO t2; +} + +do_execsql_test 28.2 { + SELECT sql FROM sqlite_schema WHERE type='trigger' +} {{CREATE TRIGGER AFTER INSERT ON "t2" BEGIN + UPDATE "t2" SET (c,d)=(a,b); + END}} + + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 29.1 { + CREATE TABLE t1(x, y); + CREATE TRIGGER Trigger1 DELETE ON t1 + BEGIN + SELECT t1.*, t1.x FROM t1 ORDER BY t1.x; + END; +} + + +do_execsql_test 29.2 { + ALTER TABLE t1 RENAME x TO z; +} + +do_execsql_test 29.3 { + ALTER TABLE t1 RENAME TO t2; +} + +do_execsql_test 29.4 { + CREATE TRIGGER tr2 AFTER DELETE ON t2 BEGIN + SELECT z, y FROM ( + SELECT t2.* FROM t2 + ); + END; +} + +do_execsql_test 29.5 { + DELETE FROM t2 +} + +do_execsql_test 29.6 { + ALTER TABLE t2 RENAME TO t3; +} + +do_execsql_test 29.7 { + SELECT sql FROM sqlite_schema WHERE type='trigger' +} { + {CREATE TRIGGER Trigger1 DELETE ON "t3" + BEGIN + SELECT "t3".*, "t3".z FROM "t3" ORDER BY "t3".z; + END} + {CREATE TRIGGER tr2 AFTER DELETE ON "t3" BEGIN + SELECT z, y FROM ( + SELECT "t3".* FROM "t3" + ); + END} +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 30.0 { + CREATE TABLE t1(a, b); + CREATE VIEW v1 AS + SELECT ( VALUES(a), (b) ) FROM ( + SELECT a, b FROM t1 + ) + ; +} + +do_execsql_test 30.1 { + SELECT * FROM v1 +} + +do_execsql_test 30.1 { + ALTER TABLE t1 RENAME TO t2; +} +do_execsql_test 30.2 { + SELECT sql FROM sqlite_schema WHERE type='view' +} { + {CREATE VIEW v1 AS + SELECT ( VALUES(a), (b) ) FROM ( + SELECT a, b FROM "t2" + )} +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 31.0 { + CREATE TABLE t1(ii INTEGER PRIMARY KEY, tt INTEGER, rr REAL); + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<50000 + ) + INSERT INTO t1 SELECT NULL, i, 5.0 FROM s; +} + +do_test 31.1 { + set pg [db one {PRAGMA page_count}] + execsql { + ALTER TABLE t1 DROP COLUMN tt; + } + set pg2 [db one {PRAGMA page_count}] + expr $pg==$pg2 +} {1} + +do_execsql_test 31.2 { + SELECT rr FROM t1 LIMIT 1 +} {5.0} + +finish_test diff --git a/test/altertrig.test b/test/altertrig.test new file mode 100644 index 0000000000..556dc3fea4 --- /dev/null +++ b/test/altertrig.test @@ -0,0 +1,162 @@ +# 2022 May 27 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix altertrig + +# If SQLITE_OMIT_ALTERTABLE is defined, omit this file. +ifcapable !altertable { + finish_test + return +} + +proc collapse_whitespace {in} { + regsub -all {[ \t\n]+} [string trim $in] { } +} + +proc do_whitespace_sql_test {tn sql res} { + set got [execsql $sql] + set wgot [list] + set wres [list] + foreach g $got { lappend wgot [collapse_whitespace $g] } + foreach r $res { lappend wres [collapse_whitespace $r] } + + uplevel [list do_test $tn [list set {} $wgot] $wres] +} + +do_execsql_test 1.0 { + CREATE TABLE t1(x); + CREATE TABLE t2(y); + CREATE TABLE t3(z); + CREATE TABLE t4(a); + + CREATE TRIGGER r1 INSERT ON t1 BEGIN + UPDATE t1 SET d='xyz' FROM t2, t3; + END; +} + +do_whitespace_sql_test 1.1 { + ALTER TABLE t3 RENAME TO t5; + SELECT sql FROM sqlite_schema WHERE type='trigger'; +} {{ + CREATE TRIGGER r1 INSERT ON t1 BEGIN + UPDATE t1 SET d='xyz' FROM t2, "t5"; + END +}} + +do_execsql_test 1.2 { + DROP TRIGGER r1; + CREATE TRIGGER r1 INSERT ON t1 BEGIN + UPDATE t1 SET d='xyz' FROM t2, (SELECT * FROM t5); + END; +} + +do_whitespace_sql_test 1.3 { + ALTER TABLE t5 RENAME TO t3; + SELECT sql FROM sqlite_schema WHERE type='trigger'; +} {{ + CREATE TRIGGER r1 INSERT ON t1 BEGIN + UPDATE t1 SET d='xyz' FROM t2, (SELECT * FROM "t3"); + END +}} + +foreach {tn alter update final} { + 1 { + ALTER TABLE t3 RENAME TO t10 + } { + UPDATE t1 SET d='xyz' FROM t2, (SELECT * FROM t3) + } { + UPDATE t1 SET d='xyz' FROM t2, (SELECT * FROM "t10") + } + + 2 { + ALTER TABLE t3 RENAME TO t10 + } { + UPDATE t1 SET a='xyz' FROM t3, (SELECT * FROM (SELECT e FROM t3)) + } { + UPDATE t1 SET a='xyz' FROM "t10", (SELECT * FROM (SELECT e FROM "t10")) + } + + 3 { + ALTER TABLE t3 RENAME e TO abc + } { + UPDATE t1 SET a='xyz' FROM t3, (SELECT * FROM (SELECT e FROM t3)) + } { + UPDATE t1 SET a='xyz' FROM t3, (SELECT * FROM (SELECT abc FROM t3)) + } + + 4 { + ALTER TABLE t2 RENAME c TO abc + } { + UPDATE t1 SET a='xyz' FROM t3, (SELECT 1 FROM t2 WHERE c) + } { + UPDATE t1 SET a='xyz' FROM t3, (SELECT 1 FROM t2 WHERE abc) + } + + 5 { + ALTER TABLE t2 RENAME c TO abc + } { + UPDATE t1 SET a=t2.c FROM t2 + } { + UPDATE t1 SET a=t2.abc FROM t2 + } + + 6 { + ALTER TABLE t2 RENAME c TO abc + } { + UPDATE t1 SET a=t2.c FROM t2, t3 + } { + UPDATE t1 SET a=t2.abc FROM t2, t3 + } + + 7 { + ALTER TABLE t4 RENAME e TO abc + } { + UPDATE t1 SET a=1 FROM t3 NATURAL JOIN t4 WHERE t4.e=a + } { + UPDATE t1 SET a=1 FROM t3 NATURAL JOIN t4 WHERE t4.abc=a + } + + 8 { + ALTER TABLE t4 RENAME TO abc + } { + UPDATE t1 SET a=1 FROM t3 NATURAL JOIN t4 WHERE t4.e=a + } { + UPDATE t1 SET a=1 FROM t3 NATURAL JOIN "abc" WHERE "abc".e=a + } + +} { + reset_db + do_execsql_test 2.$tn.1 { + CREATE TABLE t1(a,b); + CREATE TABLE t2(c,d); + CREATE TABLE t3(e,f); + CREATE TABLE t4(e,f); + } + do_execsql_test 2.$tn.2 " + CREATE TRIGGER r1 INSERT ON t1 BEGIN + $update; + END + " + do_execsql_test 2.$tn.3 $alter + + do_whitespace_sql_test 2.$tn.4 { + SELECT sqL FROM sqlite_schema WHERE type='trigger' + } "{ + CREATE TRIGGER r1 INSERT ON t1 BEGIN + $final; + END + }" +} + +finish_test diff --git a/test/analyze.test b/test/analyze.test index af277cc835..f97c78aff1 100644 --- a/test/analyze.test +++ b/test/analyze.test @@ -288,7 +288,7 @@ do_test analyze-4.3 { } {} # Verify that DROP TABLE and DROP INDEX remove entries from the -# sqlite_stat1, sqlite_stat3 and sqlite_stat4 tables. +# sqlite_stat1 and sqlite_stat4 tables. # do_test analyze-5.0 { execsql { @@ -306,13 +306,12 @@ do_test analyze-5.0 { SELECT DISTINCT tbl FROM sqlite_stat1 ORDER BY 1; } } {t3i1 t3i2 t3i3 t4i1 t4i2 t3 t4} -ifcapable stat4||stat3 { - ifcapable stat4 {set stat sqlite_stat4} else {set stat sqlite_stat3} +ifcapable stat4 { do_test analyze-5.1 { - execsql " - SELECT DISTINCT idx FROM $stat ORDER BY 1; - SELECT DISTINCT tbl FROM $stat ORDER BY 1; - " + execsql { + SELECT DISTINCT idx FROM sqlite_stat4 ORDER BY 1; + SELECT DISTINCT tbl FROM sqlite_stat4 ORDER BY 1; + } } {t3i1 t3i2 t3i3 t4i1 t4i2 t3 t4} } do_test analyze-5.2 { @@ -322,12 +321,12 @@ do_test analyze-5.2 { SELECT DISTINCT tbl FROM sqlite_stat1 ORDER BY 1; } } {t3i1 t3i3 t4i1 t4i2 t3 t4} -ifcapable stat4||stat3 { +ifcapable stat4 { do_test analyze-5.3 { - execsql " - SELECT DISTINCT idx FROM $stat ORDER BY 1; - SELECT DISTINCT tbl FROM $stat ORDER BY 1; - " + execsql { + SELECT DISTINCT idx FROM sqlite_stat4 ORDER BY 1; + SELECT DISTINCT tbl FROM sqlite_stat4 ORDER BY 1; + } } {t3i1 t3i3 t4i1 t4i2 t3 t4} } do_test analyze-5.4 { @@ -337,19 +336,20 @@ do_test analyze-5.4 { SELECT DISTINCT tbl FROM sqlite_stat1 ORDER BY 1; } } {t4i1 t4i2 t4} -ifcapable stat4||stat3 { +ifcapable stat4 { do_test analyze-5.5 { - execsql " - SELECT DISTINCT idx FROM $stat ORDER BY 1; - SELECT DISTINCT tbl FROM $stat ORDER BY 1; - " + execsql { + SELECT DISTINCT idx FROM sqlite_stat4 ORDER BY 1; + SELECT DISTINCT tbl FROM sqlite_stat4 ORDER BY 1; + } } {t4i1 t4i2 t4} } # This test corrupts the database file so it must be the last test # in the series. # -do_test analyze-99.1 { +do_test analyze-5.99 { + sqlite3_db_config db DEFENSIVE 0 execsql { PRAGMA writable_schema=on; UPDATE sqlite_master SET sql='nonsense' WHERE name='sqlite_stat1'; @@ -361,4 +361,39 @@ do_test analyze-99.1 { } } {1 {malformed database schema (sqlite_stat1)}} +# Verify that tables whose names begin with "sqlite" but not +# "sqlite_" are analyzed. +# +db close +sqlite3 db :memory: +do_execsql_test analyze-6.1 { + CREATE TABLE sqliteDemo(a); + INSERT INTO sqliteDemo(a) VALUES(1),(2),(3),(4),(5); + CREATE TABLE SQLiteDemo2(a INTEGER PRIMARY KEY AUTOINCREMENT); + INSERT INTO SQLiteDemo2 SELECT * FROM sqliteDemo; + CREATE TABLE t1(b); + INSERT INTO t1(b) SELECT a FROM sqliteDemo; + ANALYZE; + SELECT tbl FROM sqlite_stat1 WHERE idx IS NULL ORDER BY tbl; +} {SQLiteDemo2 sqliteDemo t1} + +# The following caused a small buffer overread in STAT4 processing prior +# to check-in [b99135288b157044]. +# +ifcapable stat4 { + reset_db + database_may_be_corrupt + do_execsql_test analyze-7.1 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b INTEGER); + INSERT INTO t1 VALUES(1, 7223372036854775); + INSERT INTO t1 VALUES(2, 7223372036854776); + INSERT INTO t1 VALUES(3, 7223372036854777); + CREATE INDEX i1 ON t1(b); + ANALYZE; + UPDATE sqlite_stat4 SET sample = substr(sample, 0, 4); + ANALYZE sqlite_schema; + SELECT * FROM t1 WHERE b>7223372036854775 + } {2 7223372036854776 3 7223372036854777} +} + finish_test diff --git a/test/analyze3.test b/test/analyze3.test index 2fb558d16a..033dfa5dff 100644 --- a/test/analyze3.test +++ b/test/analyze3.test @@ -18,7 +18,17 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl set testprefix analyze3 -ifcapable !stat4&&!stat3 { +ifcapable !stat4 { + finish_test + return +} + +# This test cannot be run with the sqlite3_prepare() permutation, as it +# tests that stat4 data can be used to influence the plans of queries +# based on bound variable values. And this is not possible when using +# sqlite3_prepare() - as queries cannot be internally re-prepared after +# binding values are available. +if {[permutation]=="prepare"} { finish_test return } @@ -38,7 +48,7 @@ ifcapable !stat4&&!stat3 { # query plan when there is no way in which replanning the # query may produce a superior outcome. # -# analyze3-4.*: Test that SQL or authorization callback errors occuring +# analyze3-4.*: Test that SQL or authorization callback errors occurring # within sqlite3Reprepare() are handled correctly. # # analyze3-5.*: Check that the query plans of applicable statements are @@ -100,11 +110,7 @@ do_test analyze3-1.1.1 { ANALYZE; } - ifcapable stat4 { - execsql { SELECT count(*)>0 FROM sqlite_stat4; } - } else { - execsql { SELECT count(*)>0 FROM sqlite_stat3; } - } + execsql { SELECT count(*)>0 FROM sqlite_stat4; } } {1} do_execsql_test analyze3-1.1.x { @@ -118,10 +124,40 @@ do_execsql_test analyze3-1.1.x { # do_eqp_test analyze3-1.1.2 { SELECT sum(y) FROM t1 WHERE x>200 AND x<300 -} {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (x>? AND x<?)}} +} {SEARCH t1 USING INDEX i1 (x>? AND x<?)} do_eqp_test analyze3-1.1.3 { SELECT sum(y) FROM t1 WHERE x>0 AND x<1100 -} {0 0 0 {SCAN TABLE t1}} +} {SCAN t1} + +# 2017-06-26: Verify that the SQLITE_DBCONFIG_ENABLE_QPSG setting disables +# the use of bound parameters by STAT4 +# +db cache flush +unset -nocomplain l +unset -nocomplain u +do_eqp_test analyze3-1.1.3.100 { + SELECT sum(y) FROM t1 WHERE x>$l AND x<$u +} {SEARCH t1 USING INDEX i1 (x>? AND x<?)} +set l 200 +set u 300 +do_eqp_test analyze3-1.1.3.101 { + SELECT sum(y) FROM t1 WHERE x>$l AND x<$u +} {SEARCH t1 USING INDEX i1 (x>? AND x<?)} +set l 0 +set u 1100 +do_eqp_test analyze3-1.1.3.102 { + SELECT sum(y) FROM t1 WHERE x>$l AND x<$u +} {SCAN t1} +db cache flush +sqlite3_db_config db ENABLE_QPSG 1 +do_eqp_test analyze3-1.1.3.103 { + SELECT sum(y) FROM t1 WHERE x>$l AND x<$u +} {SEARCH t1 USING INDEX i1 (x>? AND x<?)} +db cache flush +sqlite3_db_config db ENABLE_QPSG 0 +do_eqp_test analyze3-1.1.3.104 { + SELECT sum(y) FROM t1 WHERE x>$l AND x<$u +} {SCAN t1} do_test analyze3-1.1.4 { sf_execsql { SELECT sum(y) FROM t1 WHERE x>200 AND x<300 } @@ -171,10 +207,10 @@ do_execsql_test analyze3-2.1.x { } {200 990} do_eqp_test analyze3-1.2.2 { SELECT sum(y) FROM t2 WHERE x>1 AND x<2 -} {0 0 0 {SEARCH TABLE t2 USING INDEX i2 (x>? AND x<?)}} +} {SEARCH t2 USING INDEX i2 (x>? AND x<?)} do_eqp_test analyze3-1.2.3 { SELECT sum(y) FROM t2 WHERE x>0 AND x<99 -} {0 0 0 {SCAN TABLE t2}} +} {SCAN t2} do_test analyze3-1.2.4 { sf_execsql { SELECT sum(y) FROM t2 WHERE x>12 AND x<20 } @@ -223,10 +259,10 @@ do_execsql_test analyze3-1.3.x { } {99 1000} do_eqp_test analyze3-1.3.2 { SELECT sum(y) FROM t3 WHERE x>200 AND x<300 -} {0 0 0 {SEARCH TABLE t3 USING INDEX i3 (x>? AND x<?)}} +} {SEARCH t3 USING INDEX i3 (x>? AND x<?)} do_eqp_test analyze3-1.3.3 { SELECT sum(y) FROM t3 WHERE x>0 AND x<1100 -} {0 0 0 {SCAN TABLE t3}} +} {SCAN t3} do_test analyze3-1.3.4 { sf_execsql { SELECT sum(y) FROM t3 WHERE x>200 AND x<300 } @@ -278,10 +314,10 @@ do_test analyze3-2.1 { } {} do_eqp_test analyze3-2.2 { SELECT count(a) FROM t1 WHERE b LIKE 'a%' -} {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (b>? AND b<?)}} +} {SEARCH t1 USING INDEX i1 (b>? AND b<?)} do_eqp_test analyze3-2.3 { SELECT count(a) FROM t1 WHERE b LIKE '%a' -} {0 0 0 {SCAN TABLE t1}} +} {SCAN t1} # Return the first argument if like_match_blobs is true (the default) # or the second argument if not @@ -668,11 +704,11 @@ do_test analyze3-6.1 { do_eqp_test analyze3-6-3 { SELECT * FROM t1 WHERE a = 5 AND c = 13; -} {0 0 0 {SEARCH TABLE t1 USING INDEX i2 (c=?)}} +} {SEARCH t1 USING INDEX i2 (c=?)} do_eqp_test analyze3-6-2 { SELECT * FROM t1 WHERE a = 5 AND b > 'w' AND c = 13; -} {0 0 0 {SEARCH TABLE t1 USING INDEX i2 (c=?)}} +} {SEARCH t1 USING INDEX i2 (c=?)} #----------------------------------------------------------------------------- # 2015-04-20. @@ -700,4 +736,31 @@ do_execsql_test 7.2 { ANALYZE sqlite_master; } +# 2023-04-22 https://sqlite.org/forum/info/6c118daad0f1f5ef +# Case differences in the sqlite_stat4.idx field should not matter. +# +reset_db +do_execsql_test 8.0 { + CREATE TABLE t1(a PRIMARY KEY, v) WITHOUT ROWID; + ANALYZE sqlite_schema; + INSERT INTO sqlite_stat1 VALUES('t1','t1','1 1'); + INSERT INTO sqlite_stat4 VALUES('t1','t1','1','0','0',X'021b76657273696f6e'); + INSERT INTO sqlite_stat4 VALUES('T1','T1','1','0','0',X'021b76657273696f6e'); + ANALYZE sqlite_schema; +} {} + +# 2023-05-03 https://sqlite.org/forum/forumpost/537d8ab118 +# Same index appears by two different names in the sqlite_stat4 table. +# +reset_db +do_execsql_test 8.1 { + CREATE TABLE t1(a INT PRIMARY KEY, b INT) WITHOUT ROWID; + ANALYZE sqlite_schema; + INSERT INTO sqlite_stat4 VALUES + ('t1','t1','1','2','2',X'03000103'), + ('t1','sqlite_autoindex_t1_1','1','2','2',X'03000103'); + ANALYZE sqlite_schema; + PRAGMA integrity_check; +} {ok} + finish_test diff --git a/test/analyze4.test b/test/analyze4.test index 974ed89a86..d4f1921e4c 100644 --- a/test/analyze4.test +++ b/test/analyze4.test @@ -20,6 +20,12 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl +# If SQLITE_OMIT_ALTERTABLE is defined, omit this file. +ifcapable !altertable { + finish_test + return +} + do_test analyze4-1.0 { db eval { CREATE TABLE t1(a,b); @@ -38,7 +44,7 @@ do_test analyze4-1.0 { # Should choose the t1a index since it is more specific than t1b. db eval {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=5 AND b IS NULL} -} {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?)}} +} {/*SEARCH t1 USING INDEX t1a (a=?)*/} # Verify that the t1b index shows that it does not narrow down the # search any at all. diff --git a/test/analyze5.test b/test/analyze5.test index ac175c07b5..e58457a677 100644 --- a/test/analyze5.test +++ b/test/analyze5.test @@ -17,7 +17,7 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl -ifcapable !stat4&&!stat3 { +ifcapable !stat4 { finish_test return } @@ -67,39 +67,21 @@ do_test analyze5-1.0 { CREATE INDEX t1z ON t1(z); -- integers 0, 1, 2, and 3 ANALYZE; } - ifcapable stat4 { - db eval { - SELECT DISTINCT lindex(test_decode(sample),0) - FROM sqlite_stat4 WHERE idx='t1u' ORDER BY nlt; - } - } else { - db eval { - SELECT sample FROM sqlite_stat3 WHERE idx='t1u' ORDER BY nlt; - } + db eval { + SELECT DISTINCT lindex(test_decode(sample),0) + FROM sqlite_stat4 WHERE idx='t1u' ORDER BY nlt; } } {alpha bravo charlie delta} do_test analyze5-1.1 { - ifcapable stat4 { - db eval { - SELECT DISTINCT lower(lindex(test_decode(sample), 0)) - FROM sqlite_stat4 WHERE idx='t1v' ORDER BY 1 - } - } else { - db eval { - SELECT lower(sample) FROM sqlite_stat3 WHERE idx='t1v' ORDER BY 1 - } + db eval { + SELECT DISTINCT lower(lindex(test_decode(sample), 0)) + FROM sqlite_stat4 WHERE idx='t1v' ORDER BY 1 } } {alpha bravo charlie delta} -ifcapable stat4 { - do_test analyze5-1.2 { - db eval {SELECT idx, count(*) FROM sqlite_stat4 GROUP BY 1 ORDER BY 1} - } {t1t 8 t1u 8 t1v 8 t1w 8 t1x 8 t1y 9 t1z 8} -} else { - do_test analyze5-1.2 { - db eval {SELECT idx, count(*) FROM sqlite_stat3 GROUP BY 1 ORDER BY 1} - } {t1t 4 t1u 4 t1v 4 t1w 4 t1x 4 t1y 2 t1z 4} -} +do_test analyze5-1.2 { + db eval {SELECT idx, count(*) FROM sqlite_stat4 GROUP BY 1 ORDER BY 1} +} {t1t 8 t1u 8 t1v 8 t1w 8 t1x 8 t1y 9 t1z 8} # Verify that range queries generate the correct row count estimates # diff --git a/test/analyze6.test b/test/analyze6.test index 31ace8eda5..807fec132a 100644 --- a/test/analyze6.test +++ b/test/analyze6.test @@ -17,7 +17,7 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl -ifcapable !stat4&&!stat3 { +ifcapable !stat4 { finish_test return } @@ -61,14 +61,18 @@ do_test analyze6-1.0 { # do_test analyze6-1.1 { eqp {SELECT count(*) FROM ev, cat WHERE x=y} -} {0 0 1 {SCAN TABLE cat USING COVERING INDEX catx} 0 1 0 {SEARCH TABLE ev USING COVERING INDEX evy (y=?)}} +} {/*SCAN cat USING COVERING INDEX catx*SEARCH ev USING COVERING INDEX evy (y=?)*/} # The same plan is chosen regardless of the order of the tables in the # FROM clause. # -do_test analyze6-1.2 { - eqp {SELECT count(*) FROM cat, ev WHERE x=y} -} {0 0 0 {SCAN TABLE cat USING COVERING INDEX catx} 0 1 1 {SEARCH TABLE ev USING COVERING INDEX evy (y=?)}} +do_eqp_test analyze6-1.2 { + SELECT count(*) FROM cat, ev WHERE x=y +} { + QUERY PLAN + |--SCAN cat USING COVERING INDEX catx + `--SEARCH ev USING COVERING INDEX evy (y=?) +} # Ticket [83ea97620bd3101645138b7b0e71c12c5498fe3d] 2011-03-30 @@ -82,26 +86,26 @@ do_test analyze6-2.1 { ANALYZE; } eqp {SELECT * FROM t201 WHERE z=5} -} {0 0 0 {SEARCH TABLE t201 USING INDEX t201z (z=?)}} +} {/*SEARCH t201 USING INDEX t201z (z=?)*/} do_test analyze6-2.2 { eqp {SELECT * FROM t201 WHERE y=5} -} {0 0 0 {SEARCH TABLE t201 USING INDEX sqlite_autoindex_t201_1 (y=?)}} +} {/*SEARCH t201 USING INDEX sqlite_autoindex_t201_1 (y=?)*/} do_test analyze6-2.3 { eqp {SELECT * FROM t201 WHERE x=5} -} {0 0 0 {SEARCH TABLE t201 USING INTEGER PRIMARY KEY (rowid=?)}} +} {/*SEARCH t201 USING INTEGER PRIMARY KEY (rowid=?)*/} do_test analyze6-2.4 { execsql { INSERT INTO t201 VALUES(1,2,3),(2,3,4),(3,4,5); ANALYZE t201; } eqp {SELECT * FROM t201 WHERE z=5} -} {0 0 0 {SEARCH TABLE t201 USING INDEX t201z (z=?)}} +} {/*SEARCH t201 USING INDEX t201z (z=?)*/} do_test analyze6-2.5 { eqp {SELECT * FROM t201 WHERE y=5} -} {0 0 0 {SEARCH TABLE t201 USING INDEX sqlite_autoindex_t201_1 (y=?)}} +} {/*SEARCH t201 USING INDEX sqlite_autoindex_t201_1 (y=?)*/} do_test analyze6-2.6 { eqp {SELECT * FROM t201 WHERE x=5} -} {0 0 0 {SEARCH TABLE t201 USING INTEGER PRIMARY KEY (rowid=?)}} +} {/*SEARCH t201 USING INTEGER PRIMARY KEY (rowid=?)*/} do_test analyze6-2.7 { execsql { INSERT INTO t201 VALUES(4,5,7); @@ -111,12 +115,12 @@ do_test analyze6-2.7 { ANALYZE t201; } eqp {SELECT * FROM t201 WHERE z=5} -} {0 0 0 {SEARCH TABLE t201 USING INDEX t201z (z=?)}} +} {/*SEARCH t201 USING INDEX t201z (z=?)*/} do_test analyze6-2.8 { eqp {SELECT * FROM t201 WHERE y=5} -} {0 0 0 {SEARCH TABLE t201 USING INDEX sqlite_autoindex_t201_1 (y=?)}} +} {/*SEARCH t201 USING INDEX sqlite_autoindex_t201_1 (y=?)*/} do_test analyze6-2.9 { eqp {SELECT * FROM t201 WHERE x=5} -} {0 0 0 {SEARCH TABLE t201 USING INTEGER PRIMARY KEY (rowid=?)}} +} {/*SEARCH t201 USING INTEGER PRIMARY KEY (rowid=?)*/} finish_test diff --git a/test/analyze7.test b/test/analyze7.test index 76664546a5..53a80f6419 100644 --- a/test/analyze7.test +++ b/test/analyze7.test @@ -37,13 +37,13 @@ do_test analyze7-1.0 { WHERE value BETWEEN 1 AND 256; EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=123; } -} {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?)}} +} {/*SEARCH t1 USING INDEX t1a (a=?)*/} do_test analyze7-1.1 { execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE b=123;} -} {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b=?)}} +} {/*SEARCH t1 USING INDEX t1b (b=?)*/} do_test analyze7-1.2 { execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=2;} -} {0 0 0 {SEARCH TABLE t1 USING INDEX t1cd (c=?)}} +} {/*SEARCH t1 USING INDEX t1cd (c=?)*/} # Run an analyze on one of the three indices. Verify that this # effects the row-count estimate on the one query that uses that @@ -53,20 +53,20 @@ do_test analyze7-2.0 { execsql {ANALYZE t1a;} db cache flush execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=123;} -} {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?)}} +} {/*SEARCH t1 USING INDEX t1a (a=?)*/} do_test analyze7-2.1 { execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE b=123;} -} {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b=?)}} +} {/*SEARCH t1 USING INDEX t1b (b=?)*/} do_test analyze7-2.2 { execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=2;} -} {0 0 0 {SEARCH TABLE t1 USING INDEX t1cd (c=?)}} +} {/*SEARCH t1 USING INDEX t1cd (c=?)*/} # Verify that since the query planner now things that t1a is more # selective than t1b, it prefers to use t1a. # do_test analyze7-2.3 { execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=123 AND b=123} -} {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?)}} +} {/*SEARCH t1 USING INDEX t1a (a=?)*/} # Run an analysis on another of the three indices. Verify that this # new analysis works and does not disrupt the previous analysis. @@ -75,40 +75,40 @@ do_test analyze7-3.0 { execsql {ANALYZE t1cd;} db cache flush; execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=123;} -} {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?)}} +} {/*SEARCH t1 USING INDEX t1a (a=?)*/} do_test analyze7-3.1 { execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE b=123;} -} {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b=?)}} +} {/*SEARCH t1 USING INDEX t1b (b=?)*/} do_test analyze7-3.2.1 { execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=?;} -} {0 0 0 {SEARCH TABLE t1 USING INDEX t1cd (c=?)}} -ifcapable stat4||stat3 { +} {/*SEARCH t1 USING INDEX t1cd (c=?)*/} +ifcapable stat4 { # If ENABLE_STAT4 is defined, SQLite comes up with a different estimated # row count for (c=2) than it does for (c=?). do_test analyze7-3.2.2 { execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=2;} - } {0 0 0 {SEARCH TABLE t1 USING INDEX t1cd (c=?)}} + } {/*SEARCH t1 USING INDEX t1cd (c=?)*/} } else { # If ENABLE_STAT4 is not defined, the expected row count for (c=2) is the # same as that for (c=?). do_test analyze7-3.2.3 { execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=2;} - } {0 0 0 {SEARCH TABLE t1 USING INDEX t1cd (c=?)}} + } {/*SEARCH t1 USING INDEX t1cd (c=?)*/} } do_test analyze7-3.3 { execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=123 AND b=123} -} {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?)}} +} {/*SEARCH t1 USING INDEX t1a (a=?)*/} -ifcapable {!stat4 && !stat3} { +ifcapable {!stat4} { do_test analyze7-3.4 { execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=123 AND b=123} - } {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b=?)}} + } {/*SEARCH t1 USING INDEX t1b (b=?)*/} do_test analyze7-3.5 { execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=123 AND c=123} - } {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?)}} + } {/*SEARCH t1 USING INDEX t1a (a=?)*/} } do_test analyze7-3.6 { execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=123 AND d=123 AND b=123} -} {0 0 0 {SEARCH TABLE t1 USING INDEX t1cd (c=? AND d=?)}} +} {/*SEARCH t1 USING INDEX t1cd (c=? AND d=?)*/} finish_test diff --git a/test/analyze8.test b/test/analyze8.test index 1079e68080..69605fd6f7 100644 --- a/test/analyze8.test +++ b/test/analyze8.test @@ -10,13 +10,13 @@ #*********************************************************************** # # This file implements tests for SQLite library. The focus of the tests -# in this file is testing the capabilities of sqlite_stat3. +# in this file is testing the capabilities of sqlite_stat4. # set testdir [file dirname $argv0] source $testdir/tester.tcl -ifcapable !stat4&&!stat3 { +ifcapable !stat4 { finish_test return } @@ -61,25 +61,25 @@ do_test 1.0 { # do_test 1.1 { eqp {SELECT * FROM t1 WHERE a=100 AND b=55} -} {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b=?)}} +} {/*SEARCH t1 USING INDEX t1b (b=?)*/} do_test 1.2 { eqp {SELECT * FROM t1 WHERE a=99 AND b=55} -} {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?)}} +} {/*SEARCH t1 USING INDEX t1a (a=?)*/} do_test 1.3 { eqp {SELECT * FROM t1 WHERE a=101 AND b=55} -} {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?)}} +} {/*SEARCH t1 USING INDEX t1a (a=?)*/} do_test 1.4 { eqp {SELECT * FROM t1 WHERE a=100 AND b=56} -} {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b=?)}} +} {/*SEARCH t1 USING INDEX t1b (b=?)*/} do_test 1.5 { eqp {SELECT * FROM t1 WHERE a=99 AND b=56} -} {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?)}} +} {/*SEARCH t1 USING INDEX t1a (a=?)*/} do_test 1.6 { eqp {SELECT * FROM t1 WHERE a=101 AND b=56} -} {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?)}} +} {/*SEARCH t1 USING INDEX t1a (a=?)*/} do_test 2.1 { eqp {SELECT * FROM t1 WHERE a=100 AND b BETWEEN 50 AND 54} -} {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b>? AND b<?)}} +} {/*SEARCH t1 USING INDEX t1b (b>? AND b<?)*/} # There are many more values of c between 0 and 100000 than there are # between 800000 and 900000. So t1c is more selective for the latter @@ -99,17 +99,17 @@ do_execsql_test 3.0 { } {50 376 32} do_test 3.1 { eqp {SELECT * FROM t1 WHERE b BETWEEN 30 AND 34 AND c BETWEEN 0 AND 100000} -} {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b>? AND b<?)}} +} {/*SEARCH t1 USING INDEX t1b (b>? AND b<?)*/} do_test 3.2 { eqp {SELECT * FROM t1 WHERE b BETWEEN 30 AND 34 AND c BETWEEN 800000 AND 900000} -} {0 0 0 {SEARCH TABLE t1 USING INDEX t1c (c>? AND c<?)}} +} {/*SEARCH t1 USING INDEX t1c (c>? AND c<?)*/} do_test 3.3 { eqp {SELECT * FROM t1 WHERE a=100 AND c BETWEEN 0 AND 100000} -} {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?)}} +} {/*SEARCH t1 USING INDEX t1a (a=?)*/} do_test 3.4 { eqp {SELECT * FROM t1 WHERE a=100 AND c BETWEEN 800000 AND 900000} -} {0 0 0 {SEARCH TABLE t1 USING INDEX t1c (c>? AND c<?)}} +} {/*SEARCH t1 USING INDEX t1c (c>? AND c<?)*/} finish_test diff --git a/test/analyze9.test b/test/analyze9.test index e24f3dd49d..b2121fd07b 100644 --- a/test/analyze9.test +++ b/test/analyze9.test @@ -399,7 +399,7 @@ do_test 9.2 { for {set i 0} {$i < 100} {incr i} { execsql "INSERT INTO t1 VALUES('x', 'y', 'z', $i, [expr $i/2])" } - for {set i 0} {$i < 20} {incr i} { + for {set i 0} {$i < 21} {incr i} { execsql "INSERT INTO t1 VALUES('x', 'y', 'z', 101, $i)" } for {set i 102} {$i < 200} {incr i} { @@ -578,16 +578,16 @@ do_test 13.1 { } {} do_eqp_test 13.2.1 { SELECT * FROM t1 WHERE a='abc' AND rowid<15 AND b<12 -} {/SEARCH TABLE t1 USING INDEX i1/} +} {/SEARCH t1 USING INDEX i1/} do_eqp_test 13.2.2 { SELECT * FROM t1 WHERE a='abc' AND rowid<'15' AND b<12 -} {/SEARCH TABLE t1 USING INDEX i1/} +} {/SEARCH t1 USING INDEX i1/} do_eqp_test 13.3.1 { SELECT * FROM t1 WHERE a='abc' AND rowid<100 AND b<12 -} {/SEARCH TABLE t1 USING INDEX i2/} +} {/SEARCH t1 USING INDEX i2/} do_eqp_test 13.3.2 { SELECT * FROM t1 WHERE a='abc' AND rowid<'100' AND b<12 -} {/SEARCH TABLE t1 USING INDEX i2/} +} {/SEARCH t1 USING INDEX i2/} #------------------------------------------------------------------------- # Check also that affinities are taken into account when using stat4 data @@ -609,10 +609,10 @@ do_test 14.1 { } {} do_eqp_test 13.2.1 { SELECT * FROM t1 WHERE a='ott' AND b<10 AND c=1 -} {/SEARCH TABLE t1 USING INDEX i1/} +} {/SEARCH t1 USING INDEX i1/} do_eqp_test 13.2.2 { SELECT * FROM t1 WHERE a='ott' AND b<'10' AND c=1 -} {/SEARCH TABLE t1 USING INDEX i1/} +} {/SEARCH t1 USING INDEX i1/} #------------------------------------------------------------------------- # By default, 16 non-periodic samples are collected for the stat4 table. @@ -987,7 +987,8 @@ do_eqp_test 21.3 { reset_db do_execsql_test 22.0 { CREATE TABLE t3(a, b, c, d, PRIMARY KEY(a, b)) WITHOUT ROWID; -} + SELECT * FROM t3; +} {} do_execsql_test 22.1 { WITH r(x) AS ( SELECT 1 @@ -1052,15 +1053,14 @@ do_execsql_test 23.0 { do_eqp_test 23.1 { SELECT * FROM t4 WHERE (e=1 AND b='xyz' AND c='zyx' AND a<'AEA') AND f<300 -} { - 0 0 0 {SEARCH TABLE t4 USING INDEX i41 (e=? AND c=? AND b=? AND a<?)} -} + -- Formerly used index i41. But i41 is not a covering index whereas + -- the PRIMARY KEY is a covering index, and so as of 2017-10-15, the + -- PRIMARY KEY is preferred. +} {SEARCH t4 USING PRIMARY KEY (c=? AND b=? AND a<?)} do_eqp_test 23.2 { SELECT * FROM t4 WHERE (e=1 AND b='xyz' AND c='zyx' AND a<'JJJ') AND f<300 -} { - 0 0 0 {SEARCH TABLE t4 USING INDEX i42 (f<?)} -} +} {SEARCH t4 USING INDEX i42 (f<?)} do_execsql_test 24.0 { CREATE TABLE t5(c, d, b, e, a, PRIMARY KEY(a, b, c)) WITHOUT ROWID; @@ -1105,33 +1105,26 @@ ifcapable stat4&&cte { # Term (b<?) is estimated at 25%. Better than (a<30) but not as # good as (a<20). - do_eqp_test 25.2.1 { SELECT * FROM t6 WHERE a<30 AND b<? } { - 0 0 0 {SEARCH TABLE t6 USING INDEX bb (b<?)} - } - do_eqp_test 25.2.2 { SELECT * FROM t6 WHERE a<20 AND b<? } { - 0 0 0 {SEARCH TABLE t6 USING INDEX aa (a<?)} - } + do_eqp_test 25.2.1 { SELECT * FROM t6 WHERE a<30 AND b<? } \ + {SEARCH t6 USING INDEX bb (b<?)} + do_eqp_test 25.2.2 { SELECT * FROM t6 WHERE a<20 AND b<? } \ + {SEARCH t6 USING INDEX aa (a<?)} # Term (b BETWEEN ? AND ?) is estimated at 1/64. do_eqp_test 25.3.1 { SELECT * FROM t6 WHERE a BETWEEN 5 AND 10 AND b BETWEEN ? AND ? - } { - 0 0 0 {SEARCH TABLE t6 USING INDEX bb (b>? AND b<?)} - } + } {SEARCH t6 USING INDEX bb (b>? AND b<?)} # Term (b BETWEEN ? AND 60) is estimated to return roughly 15 rows - # 60 from (b<=60) multiplied by 0.25 for the b>=? term. Better than # (a<20) but not as good as (a<10). do_eqp_test 25.4.1 { SELECT * FROM t6 WHERE a < 10 AND (b BETWEEN ? AND 60) - } { - 0 0 0 {SEARCH TABLE t6 USING INDEX aa (a<?)} - } + } {SEARCH t6 USING INDEX aa (a<?)} + do_eqp_test 25.4.2 { SELECT * FROM t6 WHERE a < 20 AND (b BETWEEN ? AND 60) - } { - 0 0 0 {SEARCH TABLE t6 USING INDEX bb (b>? AND b<?)} - } + } {SEARCH t6 USING INDEX bb (b>? AND b<?)} } #------------------------------------------------------------------------- @@ -1180,16 +1173,14 @@ do_execsql_test 26.1.3 { # # There should be no other samples that start with (x=10000). So it knows # that (x=10000 AND y<50) must match somewhere between 0 and 99 rows, but -# know more than that. Guessing less than 20 is therefore unreasonable. +# no more than that. Guessing less than 20 is therefore unreasonable. # # At one point though, due to a problem in whereKeyStats(), the planner was # estimating that (x=10000 AND y<50) would match only 2 rows. # do_eqp_test 26.1.4 { SELECT * FROM t1 WHERE x = 10000 AND y < 50 AND z = 444; -} { - 0 0 0 {SEARCH TABLE t1 USING INDEX t1z (z=?)} -} +} {SEARCH t1 USING INDEX t1z (z=?)} # This test - 26.2.* - tests that another manifestation of the same problem @@ -1238,9 +1229,7 @@ do_execsql_test 26.2.1 { do_eqp_test 26.2.2 { SELECT * FROM t1 WHERE x='B' AND y>25 AND z=?; -} { - 0 0 0 {SEARCH TABLE t1 USING INDEX i1 (x=? AND y>?)} -} +} {SEARCH t1 USING INDEX i1 (x=? AND y>?)} finish_test diff --git a/test/analyzeA.test b/test/analyzeA.test deleted file mode 100644 index a2da10edff..0000000000 --- a/test/analyzeA.test +++ /dev/null @@ -1,186 +0,0 @@ -# 2013 August 3 -# -# The author disclaims copyright to this source code. In place of -# a legal notice, here is a blessing: -# -# May you do good and not evil. -# May you find forgiveness for yourself and forgive others. -# May you share freely, never taking more than you give. -# -#*********************************************************************** -# -# This file contains automated tests used to verify that the current build -# (which must be either ENABLE_STAT3 or ENABLE_STAT4) works with both stat3 -# and stat4 data. -# - -set testdir [file dirname $argv0] -source $testdir/tester.tcl -set testprefix analyzeA - -ifcapable !stat4&&!stat3 { - finish_test - return -} - -# Populate the stat3 table according to the current contents of the db -# -proc populate_stat3 {{bDropTable 1}} { - # Open a second connection on database "test.db" and run ANALYZE. If this - # is an ENABLE_STAT3 build, this is all that is required to create and - # populate the sqlite_stat3 table. - # - sqlite3 db2 test.db - execsql { ANALYZE } - - # Now, if this is an ENABLE_STAT4 build, create and populate the - # sqlite_stat3 table based on the stat4 data gathered by the ANALYZE - # above. Then drop the sqlite_stat4 table. - # - ifcapable stat4 { - db2 func lindex lindex - execsql { - PRAGMA writable_schema = on; - CREATE TABLE sqlite_stat3(tbl,idx,neq,nlt,ndlt,sample); - INSERT INTO sqlite_stat3 - SELECT DISTINCT tbl, idx, - lindex(neq,0), lindex(nlt,0), lindex(ndlt,0), test_extract(sample, 0) - FROM sqlite_stat4; - } db2 - if {$bDropTable} { execsql {DROP TABLE sqlite_stat4} db2 } - execsql { PRAGMA writable_schema = off } - } - - # Modify the database schema cookie to ensure that the other connection - # reloads the schema. - # - execsql { - CREATE TABLE obscure_tbl_nm(x); - DROP TABLE obscure_tbl_nm; - } db2 - db2 close -} - -# Populate the stat4 table according to the current contents of the db -# -proc populate_stat4 {{bDropTable 1}} { - sqlite3 db2 test.db - execsql { ANALYZE } - - ifcapable stat3 { - execsql { - PRAGMA writable_schema = on; - CREATE TABLE sqlite_stat4(tbl,idx,neq,nlt,ndlt,sample); - INSERT INTO sqlite_stat4 - SELECT tbl, idx, neq, nlt, ndlt, sqlite_record(sample) - FROM sqlite_stat3; - } db2 - if {$bDropTable} { execsql {DROP TABLE sqlite_stat3} db2 } - execsql { PRAGMA writable_schema = off } - } - - # Modify the database schema cookie to ensure that the other connection - # reloads the schema. - # - execsql { - CREATE TABLE obscure_tbl_nm(x); - DROP TABLE obscure_tbl_nm; - } db2 - db2 close -} - -# Populate the stat4 table according to the current contents of the db. -# Leave deceptive data in the stat3 table. This data should be ignored -# in favour of that from the stat4 table. -# -proc populate_both {} { - ifcapable stat4 { populate_stat3 0 } - ifcapable stat3 { populate_stat4 0 } - - sqlite3 db2 test.db - execsql { - PRAGMA writable_schema = on; - UPDATE sqlite_stat3 SET idx = - CASE idx WHEN 't1b' THEN 't1c' ELSE 't1b' - END; - PRAGMA writable_schema = off; - CREATE TABLE obscure_tbl_nm(x); - DROP TABLE obscure_tbl_nm; - } db2 - db2 close -} - -foreach {tn analyze_cmd} { - 1 populate_stat4 - 2 populate_stat3 - 3 populate_both -} { - reset_db - do_test 1.$tn.1 { - execsql { CREATE TABLE t1(a INTEGER PRIMARY KEY, b INT, c INT) } - for {set i 0} {$i < 100} {incr i} { - set c [expr int(pow(1.1,$i)/100)] - set b [expr 125 - int(pow(1.1,99-$i))/100] - execsql {INSERT INTO t1 VALUES($i, $b, $c)} - } - } {} - - execsql { CREATE INDEX t1b ON t1(b) } - execsql { CREATE INDEX t1c ON t1(c) } - $analyze_cmd - - do_execsql_test 1.$tn.2.1 { SELECT count(*) FROM t1 WHERE b=31 } 1 - do_execsql_test 1.$tn.2.2 { SELECT count(*) FROM t1 WHERE c=0 } 49 - do_execsql_test 1.$tn.2.3 { SELECT count(*) FROM t1 WHERE b=125 } 49 - do_execsql_test 1.$tn.2.4 { SELECT count(*) FROM t1 WHERE c=16 } 1 - - do_eqp_test 1.$tn.2.5 { - SELECT * FROM t1 WHERE b = 31 AND c = 0; - } {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b=?)}} - do_eqp_test 1.$tn.2.6 { - SELECT * FROM t1 WHERE b = 125 AND c = 16; - } {0 0 0 {SEARCH TABLE t1 USING INDEX t1c (c=?)}} - - do_execsql_test 1.$tn.3.1 { - SELECT count(*) FROM t1 WHERE b BETWEEN 0 AND 50 - } {6} - do_execsql_test 1.$tn.3.2 { - SELECT count(*) FROM t1 WHERE c BETWEEN 0 AND 50 - } {90} - do_execsql_test 1.$tn.3.3 { - SELECT count(*) FROM t1 WHERE b BETWEEN 75 AND 125 - } {90} - do_execsql_test 1.$tn.3.4 { - SELECT count(*) FROM t1 WHERE c BETWEEN 75 AND 125 - } {6} - - do_eqp_test 1.$tn.3.5 { - SELECT * FROM t1 WHERE b BETWEEN 0 AND 50 AND c BETWEEN 0 AND 50 - } {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b>? AND b<?)}} - - do_eqp_test 1.$tn.3.6 { - SELECT * FROM t1 WHERE b BETWEEN 75 AND 125 AND c BETWEEN 75 AND 125 - } {0 0 0 {SEARCH TABLE t1 USING INDEX t1c (c>? AND c<?)}} - - do_eqp_test 1.$tn.3.7 { - SELECT * FROM t1 WHERE b BETWEEN +0 AND +50 AND c BETWEEN +0 AND +50 - } {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b>? AND b<?)}} - - do_eqp_test 1.$tn.3.8 { - SELECT * FROM t1 - WHERE b BETWEEN cast('0' AS int) AND cast('50.0' AS real) - AND c BETWEEN cast('0' AS numeric) AND cast('50.0' AS real) - } {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b>? AND b<?)}} - - do_eqp_test 1.$tn.3.9 { - SELECT * FROM t1 WHERE b BETWEEN +75 AND +125 AND c BETWEEN +75 AND +125 - } {0 0 0 {SEARCH TABLE t1 USING INDEX t1c (c>? AND c<?)}} - - do_eqp_test 1.$tn.3.10 { - SELECT * FROM t1 - WHERE b BETWEEN cast('75' AS int) AND cast('125.0' AS real) - AND c BETWEEN cast('75' AS numeric) AND cast('125.0' AS real) - } {0 0 0 {SEARCH TABLE t1 USING INDEX t1c (c>? AND c<?)}} -} - -finish_test diff --git a/test/analyzeB.test b/test/analyzeB.test deleted file mode 100644 index e91e7d585e..0000000000 --- a/test/analyzeB.test +++ /dev/null @@ -1,682 +0,0 @@ -# 2013 August 3 -# -# The author disclaims copyright to this source code. In place of -# a legal notice, here is a blessing: -# -# May you do good and not evil. -# May you find forgiveness for yourself and forgive others. -# May you share freely, never taking more than you give. -# -#*********************************************************************** -# -# This file contains automated tests used to verify that the sqlite_stat3 -# functionality is working. The tests in this file are based on a subset -# of the sqlite_stat4 tests in analyze9.test. -# - -set testdir [file dirname $argv0] -source $testdir/tester.tcl -set testprefix analyzeB - -ifcapable !stat3 { - finish_test - return -} - -do_execsql_test 1.0 { - CREATE TABLE t1(a TEXT, b TEXT); - INSERT INTO t1 VALUES('(0)', '(0)'); - INSERT INTO t1 VALUES('(1)', '(1)'); - INSERT INTO t1 VALUES('(2)', '(2)'); - INSERT INTO t1 VALUES('(3)', '(3)'); - INSERT INTO t1 VALUES('(4)', '(4)'); - CREATE INDEX i1 ON t1(a, b); -} {} - - -do_execsql_test 1.1 { - ANALYZE; -} {} - -do_execsql_test 1.2 { - SELECT tbl,idx,nEq,nLt,nDLt,quote(sample) FROM sqlite_stat3; -} { - t1 i1 1 0 0 '(0)' - t1 i1 1 1 1 '(1)' - t1 i1 1 2 2 '(2)' - t1 i1 1 3 3 '(3)' - t1 i1 1 4 4 '(4)' -} - -if {[permutation] != "utf16"} { - do_execsql_test 1.3 { - SELECT tbl,idx,nEq,nLt,nDLt,quote(sample) FROM sqlite_stat3; - } { - t1 i1 1 0 0 '(0)' - t1 i1 1 1 1 '(1)' - t1 i1 1 2 2 '(2)' - t1 i1 1 3 3 '(3)' - t1 i1 1 4 4 '(4)' - } -} - - -#------------------------------------------------------------------------- -# This is really just to test SQL user function "test_decode". -# -reset_db -do_execsql_test 2.1 { - CREATE TABLE t1(a, b, c); - INSERT INTO t1(a) VALUES('some text'); - INSERT INTO t1(a) VALUES(14); - INSERT INTO t1(a) VALUES(NULL); - INSERT INTO t1(a) VALUES(22.0); - INSERT INTO t1(a) VALUES(x'656667'); - CREATE INDEX i1 ON t1(a, b, c); - ANALYZE; - SELECT quote(sample) FROM sqlite_stat3; -} { - NULL 14 22.0 {'some text'} X'656667' -} - -#------------------------------------------------------------------------- -# -reset_db -do_execsql_test 3.1 { - CREATE TABLE t2(a, b); - CREATE INDEX i2 ON t2(a, b); - BEGIN; -} - -do_test 3.2 { - for {set i 0} {$i < 1000} {incr i} { - set a [expr $i / 10] - set b [expr int(rand() * 15.0)] - execsql { INSERT INTO t2 VALUES($a, $b) } - } - execsql COMMIT -} {} - -db func lindex lindex - -# Each value of "a" occurs exactly 10 times in the table. -# -do_execsql_test 3.3.1 { - SELECT count(*) FROM t2 GROUP BY a; -} [lrange [string repeat "10 " 100] 0 99] - -# The first element in the "nEq" list of all samples should therefore be 10. -# -do_execsql_test 3.3.2 { - ANALYZE; - SELECT nEq FROM sqlite_stat3; -} [lrange [string repeat "10 " 100] 0 23] - -#------------------------------------------------------------------------- -# -do_execsql_test 3.4 { - DROP TABLE IF EXISTS t1; - CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c); - INSERT INTO t1 VALUES(1, 1, 'one-a'); - INSERT INTO t1 VALUES(11, 1, 'one-b'); - INSERT INTO t1 VALUES(21, 1, 'one-c'); - INSERT INTO t1 VALUES(31, 1, 'one-d'); - INSERT INTO t1 VALUES(41, 1, 'one-e'); - INSERT INTO t1 VALUES(51, 1, 'one-f'); - INSERT INTO t1 VALUES(61, 1, 'one-g'); - INSERT INTO t1 VALUES(71, 1, 'one-h'); - INSERT INTO t1 VALUES(81, 1, 'one-i'); - INSERT INTO t1 VALUES(91, 1, 'one-j'); - INSERT INTO t1 SELECT a+1,2,'two' || substr(c,4) FROM t1; - INSERT INTO t1 SELECT a+2,3,'three'||substr(c,4) FROM t1 WHERE c GLOB 'one-*'; - INSERT INTO t1 SELECT a+3,4,'four'||substr(c,4) FROM t1 WHERE c GLOB 'one-*'; - INSERT INTO t1 SELECT a+4,5,'five'||substr(c,4) FROM t1 WHERE c GLOB 'one-*'; - INSERT INTO t1 SELECT a+5,6,'six'||substr(c,4) FROM t1 WHERE c GLOB 'one-*'; - CREATE INDEX t1b ON t1(b); - ANALYZE; - SELECT c FROM t1 WHERE b=3 AND a BETWEEN 30 AND 60; -} {three-d three-e three-f} - - -#------------------------------------------------------------------------- -# These tests verify that the sample selection for stat3 appears to be -# working as designed. -# - -reset_db -db func lindex lindex -db func lrange lrange - -do_execsql_test 4.0 { - DROP TABLE IF EXISTS t1; - CREATE TABLE t1(a, b, c); - CREATE INDEX i1 ON t1(c, b, a); -} - - -proc insert_filler_rows_n {iStart args} { - set A(-ncopy) 1 - set A(-nval) 1 - - foreach {k v} $args { - if {[info exists A($k)]==0} { error "no such option: $k" } - set A($k) $v - } - if {[llength $args] % 2} { - error "option requires an argument: [lindex $args end]" - } - - for {set i 0} {$i < $A(-nval)} {incr i} { - set iVal [expr $iStart+$i] - for {set j 0} {$j < $A(-ncopy)} {incr j} { - execsql { INSERT INTO t1 VALUES($iVal, $iVal, $iVal) } - } - } -} - -do_test 4.1 { - execsql { BEGIN } - insert_filler_rows_n 0 -ncopy 10 -nval 19 - insert_filler_rows_n 20 -ncopy 1 -nval 100 - - execsql { - INSERT INTO t1(c, b, a) VALUES(200, 1, 'a'); - INSERT INTO t1(c, b, a) VALUES(200, 1, 'b'); - INSERT INTO t1(c, b, a) VALUES(200, 1, 'c'); - - INSERT INTO t1(c, b, a) VALUES(200, 2, 'e'); - INSERT INTO t1(c, b, a) VALUES(200, 2, 'f'); - - INSERT INTO t1(c, b, a) VALUES(201, 3, 'g'); - INSERT INTO t1(c, b, a) VALUES(201, 4, 'h'); - - ANALYZE; - SELECT count(*) FROM sqlite_stat3; - SELECT count(*) FROM t1; - } -} {24 297} - -do_execsql_test 4.2 { - SELECT neq, nlt, ndlt, sample FROM sqlite_stat3 ORDER BY rowid LIMIT 16; -} { - 10 0 0 0 - 10 10 1 1 - 10 20 2 2 - 10 30 3 3 - 10 40 4 4 - 10 50 5 5 - 10 60 6 6 - 10 70 7 7 - 10 80 8 8 - 10 90 9 9 - 10 100 10 10 - 10 110 11 11 - 10 120 12 12 - 10 130 13 13 - 10 140 14 14 - 10 150 15 15 -} - -do_execsql_test 4.3 { - SELECT neq, nlt, ndlt, sample FROM sqlite_stat3 - ORDER BY rowid DESC LIMIT 2; -} { - 2 295 120 201 - 5 290 119 200 -} - -do_execsql_test 4.4 { SELECT count(DISTINCT c) FROM t1 WHERE c<201 } 120 -do_execsql_test 4.5 { SELECT count(DISTINCT c) FROM t1 WHERE c<200 } 119 - -reset_db -do_test 4.7 { - execsql { - BEGIN; - CREATE TABLE t1(o,t INTEGER PRIMARY KEY); - CREATE INDEX i1 ON t1(o); - } - for {set i 0} {$i<10000} {incr i [expr (($i<1000)?1:10)]} { - execsql { INSERT INTO t1 VALUES('x', $i) } - } - execsql { - COMMIT; - ANALYZE; - SELECT count(*) FROM sqlite_stat3; - } -} {1} -do_execsql_test 4.8 { - SELECT sample FROM sqlite_stat3; -} {x} - - -#------------------------------------------------------------------------- -# The following would cause a crash at one point. -# -reset_db -do_execsql_test 5.1 { - PRAGMA encoding = 'utf-16'; - CREATE TABLE t0(v); - ANALYZE; -} - -#------------------------------------------------------------------------- -# This was also crashing (corrupt sqlite_stat3 table). -# -reset_db -do_execsql_test 6.1 { - CREATE TABLE t1(a, b); - CREATE INDEX i1 ON t1(a); - CREATE INDEX i2 ON t1(b); - INSERT INTO t1 VALUES(1, 1); - INSERT INTO t1 VALUES(2, 2); - INSERT INTO t1 VALUES(3, 3); - INSERT INTO t1 VALUES(4, 4); - INSERT INTO t1 VALUES(5, 5); - ANALYZE; - PRAGMA writable_schema = 1; - CREATE TEMP TABLE x1 AS - SELECT tbl,idx,neq,nlt,ndlt,sample FROM sqlite_stat3 - ORDER BY (rowid%5), rowid; - DELETE FROM sqlite_stat3; - INSERT INTO sqlite_stat3 SELECT * FROM x1; - PRAGMA writable_schema = 0; - ANALYZE sqlite_master; -} -do_execsql_test 6.2 { - SELECT * FROM t1 WHERE a = 'abc'; -} - -#------------------------------------------------------------------------- -# The following tests experiment with adding corrupted records to the -# 'sample' column of the sqlite_stat3 table. -# -reset_db -sqlite3_db_config_lookaside db 0 0 0 - -do_execsql_test 7.1 { - CREATE TABLE t1(a, b); - CREATE INDEX i1 ON t1(a, b); - INSERT INTO t1 VALUES(1, 1); - INSERT INTO t1 VALUES(2, 2); - INSERT INTO t1 VALUES(3, 3); - INSERT INTO t1 VALUES(4, 4); - INSERT INTO t1 VALUES(5, 5); - ANALYZE; - UPDATE sqlite_stat3 SET sample = X'' WHERE rowid = 1; - ANALYZE sqlite_master; -} - -do_execsql_test 7.2 { - UPDATE sqlite_stat3 SET sample = X'FFFF'; - ANALYZE sqlite_master; - SELECT * FROM t1 WHERE a = 1; -} {1 1} - -do_execsql_test 7.3 { - ANALYZE; - UPDATE sqlite_stat3 SET neq = '0 0 0'; - ANALYZE sqlite_master; - SELECT * FROM t1 WHERE a = 1; -} {1 1} - -do_execsql_test 7.4 { - ANALYZE; - UPDATE sqlite_stat3 SET ndlt = '0 0 0'; - ANALYZE sqlite_master; - SELECT * FROM t1 WHERE a = 3; -} {3 3} - -do_execsql_test 7.5 { - ANALYZE; - UPDATE sqlite_stat3 SET nlt = '0 0 0'; - ANALYZE sqlite_master; - SELECT * FROM t1 WHERE a = 5; -} {5 5} - -#------------------------------------------------------------------------- -# -reset_db -do_execsql_test 8.1 { - CREATE TABLE t1(x TEXT); - CREATE INDEX i1 ON t1(x); - INSERT INTO t1 VALUES('1'); - INSERT INTO t1 VALUES('2'); - INSERT INTO t1 VALUES('3'); - INSERT INTO t1 VALUES('4'); - ANALYZE; -} -do_execsql_test 8.2 { - SELECT * FROM t1 WHERE x = 3; -} {3} - -#------------------------------------------------------------------------- -# -reset_db -do_execsql_test 9.1 { - CREATE TABLE t1(a, b, c, d, e); - CREATE INDEX i1 ON t1(a, b, c, d); - CREATE INDEX i2 ON t1(e); -} -do_test 9.2 { - execsql BEGIN; - for {set i 0} {$i < 100} {incr i} { - execsql "INSERT INTO t1 VALUES('x', 'y', 'z', $i, [expr $i/2])" - } - for {set i 0} {$i < 20} {incr i} { - execsql "INSERT INTO t1 VALUES('x', 'y', 'z', 101, $i)" - } - for {set i 102} {$i < 200} {incr i} { - execsql "INSERT INTO t1 VALUES('x', 'y', 'z', $i, [expr $i/2])" - } - execsql COMMIT - execsql ANALYZE -} {} - -do_eqp_test 9.3.1 { - SELECT * FROM t1 WHERE a='x' AND b='y' AND c='z' AND d=101 AND e=5; -} {/t1 USING INDEX i1/} -do_eqp_test 9.3.2 { - SELECT * FROM t1 WHERE a='x' AND b='y' AND c='z' AND d=99 AND e=5; -} {/t1 USING INDEX i1/} - -set value_d [expr 101] -do_eqp_test 9.4.1 { - SELECT * FROM t1 WHERE a='x' AND b='y' AND c='z' AND d=$value_d AND e=5 -} {/t1 USING INDEX i1/} -set value_d [expr 99] -do_eqp_test 9.4.2 { - SELECT * FROM t1 WHERE a='x' AND b='y' AND c='z' AND d=$value_d AND e=5 -} {/t1 USING INDEX i1/} - -#------------------------------------------------------------------------- -# Check that the planner takes stat3 data into account when considering -# "IS NULL" and "IS NOT NULL" constraints. -# -do_execsql_test 10.1.1 { - DROP TABLE IF EXISTS t3; - CREATE TABLE t3(a, b); - CREATE INDEX t3a ON t3(a); - CREATE INDEX t3b ON t3(b); -} -do_test 10.1.2 { - for {set i 1} {$i < 100} {incr i} { - if {$i>90} { set a $i } else { set a NULL } - set b [expr $i % 5] - execsql "INSERT INTO t3 VALUES($a, $b)" - } - execsql ANALYZE -} {} -do_eqp_test 10.1.3 { - SELECT * FROM t3 WHERE a IS NULL AND b = 2 -} {/t3 USING INDEX t3b/} -do_eqp_test 10.1.4 { - SELECT * FROM t3 WHERE a IS NOT NULL AND b = 2 -} {/t3 USING INDEX t3a/} - -#------------------------------------------------------------------------- -# Check that stat3 data is used correctly with non-default collation -# sequences. -# -foreach {tn schema} { - 1 { - CREATE TABLE t4(a COLLATE nocase, b); - CREATE INDEX t4a ON t4(a); - CREATE INDEX t4b ON t4(b); - } - 2 { - CREATE TABLE t4(a, b); - CREATE INDEX t4a ON t4(a COLLATE nocase); - CREATE INDEX t4b ON t4(b); - } -} { - drop_all_tables - do_test 11.$tn.1 { execsql $schema } {} - - do_test 11.$tn.2 { - for {set i 0} {$i < 100} {incr i} { - if { ($i % 10)==0 } { set a ABC } else { set a DEF } - set b [expr $i % 5] - execsql { INSERT INTO t4 VALUES($a, $b) } - } - execsql ANALYZE - } {} - - do_eqp_test 11.$tn.3 { - SELECT * FROM t4 WHERE a = 'def' AND b = 3; - } {/t4 USING INDEX t4b/} - - if {$tn==1} { - set sql "SELECT * FROM t4 WHERE a = 'abc' AND b = 3;" - do_eqp_test 11.$tn.4 $sql {/t4 USING INDEX t4a/} - } else { - - set sql "SELECT * FROM t4 WHERE a = 'abc' COLLATE nocase AND b = 3;" - do_eqp_test 11.$tn.5 $sql {/t4 USING INDEX t4a/} - - set sql "SELECT * FROM t4 WHERE a COLLATE nocase = 'abc' AND b = 3;" - do_eqp_test 11.$tn.6 $sql {/t4 USING INDEX t4a/} - } -} - -#------------------------------------------------------------------------- -# Test that nothing untoward happens if the stat3 table contains entries -# for indexes that do not exist. Or NULL values in the idx column. -# Or NULL values in any of the other columns. -# -drop_all_tables -do_execsql_test 15.1 { - CREATE TABLE x1(a, b, UNIQUE(a, b)); - INSERT INTO x1 VALUES(1, 2); - INSERT INTO x1 VALUES(3, 4); - INSERT INTO x1 VALUES(5, 6); - ANALYZE; - INSERT INTO sqlite_stat3 VALUES(NULL, NULL, NULL, NULL, NULL, NULL); -} -db close -sqlite3 db test.db -do_execsql_test 15.2 { SELECT * FROM x1 } {1 2 3 4 5 6} - -do_execsql_test 15.3 { - INSERT INTO sqlite_stat3 VALUES(42, 42, 42, 42, 42, 42); -} -db close -sqlite3 db test.db -do_execsql_test 15.4 { SELECT * FROM x1 } {1 2 3 4 5 6} - -do_execsql_test 15.5 { - UPDATE sqlite_stat1 SET stat = NULL; -} -db close -sqlite3 db test.db -do_execsql_test 15.6 { SELECT * FROM x1 } {1 2 3 4 5 6} - -do_execsql_test 15.7 { - ANALYZE; - UPDATE sqlite_stat1 SET tbl = 'no such tbl'; -} -db close -sqlite3 db test.db -do_execsql_test 15.8 { SELECT * FROM x1 } {1 2 3 4 5 6} - -do_execsql_test 15.9 { - ANALYZE; - UPDATE sqlite_stat3 SET neq = NULL, nlt=NULL, ndlt=NULL; -} -db close -sqlite3 db test.db -do_execsql_test 15.10 { SELECT * FROM x1 } {1 2 3 4 5 6} - -# This is just for coverage.... -do_execsql_test 15.11 { - ANALYZE; - UPDATE sqlite_stat1 SET stat = stat || ' unordered'; -} -db close -sqlite3 db test.db -do_execsql_test 15.12 { SELECT * FROM x1 } {1 2 3 4 5 6} - -#------------------------------------------------------------------------- -# Test that allocations used for sqlite_stat3 samples are included in -# the quantity returned by SQLITE_DBSTATUS_SCHEMA_USED. -# -set one [string repeat x 1000] -set two [string repeat x 2000] -do_test 16.1 { - reset_db - execsql { - CREATE TABLE t1(a, UNIQUE(a)); - INSERT INTO t1 VALUES($one); - ANALYZE; - } - set nByte [lindex [sqlite3_db_status db SCHEMA_USED 0] 1] - - reset_db - execsql { - CREATE TABLE t1(a, UNIQUE(a)); - INSERT INTO t1 VALUES($two); - ANALYZE; - } - set nByte2 [lindex [sqlite3_db_status db SCHEMA_USED 0] 1] - - expr {$nByte2 > $nByte+950 && $nByte2 < $nByte+1050} -} {1} - -#------------------------------------------------------------------------- -# Test that stat3 data may be used with partial indexes. -# -do_test 17.1 { - reset_db - execsql { - CREATE TABLE t1(a, b, c, d); - CREATE INDEX i1 ON t1(a, b) WHERE d IS NOT NULL; - INSERT INTO t1 VALUES(-1, -1, -1, NULL); - INSERT INTO t1 SELECT 2*a,2*b,2*c,d FROM t1; - INSERT INTO t1 SELECT 2*a,2*b,2*c,d FROM t1; - INSERT INTO t1 SELECT 2*a,2*b,2*c,d FROM t1; - INSERT INTO t1 SELECT 2*a,2*b,2*c,d FROM t1; - INSERT INTO t1 SELECT 2*a,2*b,2*c,d FROM t1; - INSERT INTO t1 SELECT 2*a,2*b,2*c,d FROM t1; - } - - for {set i 0} {$i < 32} {incr i} { - execsql { INSERT INTO t1 VALUES($i%2, $b, $i/2, 'abc') } - } - execsql {ANALYZE main.t1} -} {} - -do_catchsql_test 17.1.2 { - ANALYZE temp.t1; -} {1 {no such table: temp.t1}} - -do_eqp_test 17.2 { - SELECT * FROM t1 WHERE d IS NOT NULL AND a=0; -} {/USING INDEX i1/} -do_eqp_test 17.3 { - SELECT * FROM t1 WHERE d IS NOT NULL AND a=0; -} {/USING INDEX i1/} - -do_execsql_test 17.4 { - CREATE INDEX i2 ON t1(c) WHERE d IS NOT NULL; - ANALYZE main.i2; -} -do_eqp_test 17.5 { - SELECT * FROM t1 WHERE d IS NOT NULL AND a=0; -} {/USING INDEX i1/} -do_eqp_test 17.6 { - SELECT * FROM t1 WHERE d IS NOT NULL AND a=0 AND b=0 AND c=10; -} {/USING INDEX i2/} - -#------------------------------------------------------------------------- -# -do_test 18.1 { - reset_db - execsql { - CREATE TABLE t1(a, b); - CREATE INDEX i1 ON t1(a, b); - } - for {set i 0} {$i < 9} {incr i} { - execsql { - INSERT INTO t1 VALUES($i, 0); - INSERT INTO t1 VALUES($i, 0); - INSERT INTO t1 VALUES($i, 0); - INSERT INTO t1 VALUES($i, 0); - INSERT INTO t1 VALUES($i, 0); - INSERT INTO t1 VALUES($i, 0); - INSERT INTO t1 VALUES($i, 0); - INSERT INTO t1 VALUES($i, 0); - INSERT INTO t1 VALUES($i, 0); - INSERT INTO t1 VALUES($i, 0); - INSERT INTO t1 VALUES($i, 0); - INSERT INTO t1 VALUES($i, 0); - INSERT INTO t1 VALUES($i, 0); - INSERT INTO t1 VALUES($i, 0); - INSERT INTO t1 VALUES($i, 0); - } - } - execsql ANALYZE - execsql { SELECT count(*) FROM sqlite_stat3 } -} {9} - -#------------------------------------------------------------------------- -# For coverage. -# -ifcapable view { - do_test 19.1 { - reset_db - execsql { - CREATE TABLE t1(x, y); - CREATE INDEX i1 ON t1(x, y); - CREATE VIEW v1 AS SELECT * FROM t1; - ANALYZE; - } - } {} -} -ifcapable auth { - proc authproc {op args} { - if {$op == "SQLITE_ANALYZE"} { return "SQLITE_DENY" } - return "SQLITE_OK" - } - do_test 19.2 { - reset_db - db auth authproc - execsql { - CREATE TABLE t1(x, y); - CREATE VIEW v1 AS SELECT * FROM t1; - } - catchsql ANALYZE - } {1 {not authorized}} -} - -#------------------------------------------------------------------------- -# -reset_db -proc r {args} { expr rand() } -db func r r -db func lrange lrange -do_test 20.1 { - execsql { - CREATE TABLE t1(a,b,c,d); - CREATE INDEX i1 ON t1(a,b,c,d); - } - for {set i 0} {$i < 16} {incr i} { - execsql { - INSERT INTO t1 VALUES($i, r(), r(), r()); - INSERT INTO t1 VALUES($i, $i, r(), r()); - INSERT INTO t1 VALUES($i, $i, $i, r()); - INSERT INTO t1 VALUES($i, $i, $i, $i); - INSERT INTO t1 VALUES($i, $i, $i, $i); - INSERT INTO t1 VALUES($i, $i, $i, r()); - INSERT INTO t1 VALUES($i, $i, r(), r()); - INSERT INTO t1 VALUES($i, r(), r(), r()); - } - } -} {} -do_execsql_test 20.2 { ANALYZE } -for {set i 0} {$i<16} {incr i} { - set val $i - do_execsql_test 20.3.$i { - SELECT count(*) FROM sqlite_stat3 WHERE sample=$val - } {1} -} - -finish_test diff --git a/test/analyzeC.test b/test/analyzeC.test index 02faa9c7e9..f5bcaeb781 100644 --- a/test/analyzeC.test +++ b/test/analyzeC.test @@ -50,7 +50,7 @@ do_execsql_test 1.2 { do_execsql_test 1.3 { EXPLAIN QUERY PLAN SELECT c FROM t1 ORDER BY a; -} {/.*SCAN TABLE t1 USING INDEX t1a.*/} +} {/.*SCAN t1 USING INDEX t1a.*/} do_execsql_test 1.3x { EXPLAIN QUERY PLAN SELECT c FROM t1 ORDER BY a; @@ -132,6 +132,20 @@ do_execsql_test 4.3 { SELECT count(a) FROM t1; } {/.*INDEX t1ca.*/} +# 2019-08-15. +# Ticket https://sqlite.org/src/tktview/e4598ecbdd18bd82945f602901 +# The sz=N parameter in the sqlite_stat1 table needs to have a value of +# 2 or more to avoid a division by zero in the query planner. +# +do_execsql_test 4.4 { + DROP TABLE IF EXISTS t44; + CREATE TABLE t44(a PRIMARY KEY); + INSERT INTO sqlite_stat1 VALUES('t44',null,'sz=0'); + ANALYZE sqlite_master; + SELECT 0 FROM t44 WHERE a IN(1,2,3); +} {} + + # The sz=NNN parameter works even if there is other extraneous text # in the sqlite_stat1.stat column. diff --git a/test/analyzeD.test b/test/analyzeD.test index 7ef1094f82..7a51785a1c 100644 --- a/test/analyzeD.test +++ b/test/analyzeD.test @@ -1,4 +1,4 @@ -# 2005 July 22 +# 2014-10-04 # # The author disclaims copyright to this source code. In place of # a legal notice, here is a blessing: @@ -11,7 +11,6 @@ # This file implements regression tests for SQLite library. # This file implements tests for the ANALYZE command. # -# $Id: analyze.test,v 1.9 2008/08/11 18:44:58 drh Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl @@ -64,9 +63,7 @@ do_test 1.1 { # do_eqp_test 1.2 { SELECT * FROM t1 WHERE a=3001 AND c=150; -} { - 0 0 0 {SEARCH TABLE t1 USING INDEX t1_c (c=?)} -} +} {SEARCH t1 USING INDEX t1_c (c=?)} do_test 1.3 { execsql { DELETE FROM sqlite_stat1 } @@ -75,15 +72,13 @@ do_test 1.3 { } {} # Without stat1, because 3001 is larger than all samples in the stat4 -# table, SQLite things that a=3001 matches just 1 row. So it (incorrectly) +# table, SQLite thinks that a=3001 matches just 1 row. So it (incorrectly) # chooses it over the c=150 index (5 rows). Even with stat1 data, things # worked this way before commit [e6f7f97dbc]. # do_eqp_test 1.4 { SELECT * FROM t1 WHERE a=3001 AND c=150; -} { - 0 0 0 {SEARCH TABLE t1 USING INDEX t1_ab (a=?)} -} +} {SEARCH t1 USING INDEX t1_ab (a=?)} do_test 1.5 { execsql { @@ -94,9 +89,7 @@ do_test 1.5 { do_eqp_test 1.6 { SELECT * FROM t1 WHERE a=13 AND c=150; -} { - 0 0 0 {SEARCH TABLE t1 USING INDEX t1_c (c=?)} -} +} {SEARCH t1 USING INDEX t1_c (c=?)} do_test 1.7 { execsql { DELETE FROM sqlite_stat1 } @@ -109,8 +102,6 @@ do_test 1.7 { # gets this right, even without stat1 data. do_eqp_test 1.8 { SELECT * FROM t1 WHERE a=13 AND c=150; -} { - 0 0 0 {SEARCH TABLE t1 USING INDEX t1_c (c=?)} -} +} {SEARCH t1 USING INDEX t1_c (c=?)} finish_test diff --git a/test/analyzeE.test b/test/analyzeE.test index 66db1e122c..2547daa1c8 100644 --- a/test/analyzeE.test +++ b/test/analyzeE.test @@ -36,47 +36,47 @@ do_execsql_test analyzeE-1.0 { do_execsql_test analyzeE-1.1 { EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a BETWEEN 500 AND 2500; -} {/SCAN TABLE t1/} +} {/SCAN t1/} do_execsql_test analyzeE-1.2 { EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a BETWEEN 2900 AND 3000; -} {/SEARCH TABLE t1 USING INDEX t1a/} +} {/SEARCH t1 USING INDEX t1a/} do_execsql_test analyzeE-1.3 { EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a BETWEEN 1700 AND 1750; -} {/SEARCH TABLE t1 USING INDEX t1a/} +} {/SEARCH t1 USING INDEX t1a/} do_execsql_test analyzeE-1.4 { EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a BETWEEN 1 AND 500 -} {/SEARCH TABLE t1 USING INDEX t1a/} +} {/SEARCH t1 USING INDEX t1a/} do_execsql_test analyzeE-1.5 { EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a BETWEEN 3000 AND 3000000 -} {/SEARCH TABLE t1 USING INDEX t1a/} +} {/SEARCH t1 USING INDEX t1a/} do_execsql_test analyzeE-1.6 { EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a<500 -} {/SEARCH TABLE t1 USING INDEX t1a/} +} {/SEARCH t1 USING INDEX t1a/} do_execsql_test analyzeE-1.7 { EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a>2500 -} {/SEARCH TABLE t1 USING INDEX t1a/} +} {/SEARCH t1 USING INDEX t1a/} do_execsql_test analyzeE-1.8 { EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a>1900 -} {/SEARCH TABLE t1 USING INDEX t1a/} +} {/SEARCH t1 USING INDEX t1a/} do_execsql_test analyzeE-1.9 { EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a>1100 -} {/SCAN TABLE t1/} +} {/SCAN t1/} do_execsql_test analyzeE-1.10 { EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a<1100 -} {/SEARCH TABLE t1 USING INDEX t1a/} +} {/SEARCH t1 USING INDEX t1a/} do_execsql_test analyzeE-1.11 { EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a<1900 -} {/SCAN TABLE t1/} +} {/SCAN t1/} # Verify that everything works the same on a DESCENDING index. # @@ -88,47 +88,47 @@ do_execsql_test analyzeE-2.0 { do_execsql_test analyzeE-2.1 { EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a BETWEEN 500 AND 2500; -} {/SCAN TABLE t1/} +} {/SCAN t1/} do_execsql_test analyzeE-2.2 { EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a BETWEEN 2900 AND 3000; -} {/SEARCH TABLE t1 USING INDEX t1a/} +} {/SEARCH t1 USING INDEX t1a/} do_execsql_test analyzeE-2.3 { EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a BETWEEN 1700 AND 1750; -} {/SEARCH TABLE t1 USING INDEX t1a/} +} {/SEARCH t1 USING INDEX t1a/} do_execsql_test analyzeE-2.4 { EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a BETWEEN 1 AND 500 -} {/SEARCH TABLE t1 USING INDEX t1a/} +} {/SEARCH t1 USING INDEX t1a/} do_execsql_test analyzeE-2.5 { EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a BETWEEN 3000 AND 3000000 -} {/SEARCH TABLE t1 USING INDEX t1a/} +} {/SEARCH t1 USING INDEX t1a/} do_execsql_test analyzeE-2.6 { EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a<500 -} {/SEARCH TABLE t1 USING INDEX t1a/} +} {/SEARCH t1 USING INDEX t1a/} do_execsql_test analyzeE-2.7 { EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a>2500 -} {/SEARCH TABLE t1 USING INDEX t1a/} +} {/SEARCH t1 USING INDEX t1a/} do_execsql_test analyzeE-2.8 { EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a>1900 -} {/SEARCH TABLE t1 USING INDEX t1a/} +} {/SEARCH t1 USING INDEX t1a/} do_execsql_test analyzeE-2.9 { EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a>1100 -} {/SCAN TABLE t1/} +} {/SCAN t1/} do_execsql_test analyzeE-2.10 { EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a<1100 -} {/SEARCH TABLE t1 USING INDEX t1a/} +} {/SEARCH t1 USING INDEX t1a/} do_execsql_test analyzeE-2.11 { EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a<1900 -} {/SCAN TABLE t1/} +} {/SCAN t1/} # Now do a range query on the second term of an ASCENDING index # where the first term is constrained by equality. @@ -145,47 +145,47 @@ do_execsql_test analyzeE-3.0 { do_execsql_test analyzeE-3.1 { EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a BETWEEN 500 AND 2500 AND c=123; -} {/SCAN TABLE t1/} +} {/SCAN t1/} do_execsql_test analyzeE-3.2 { EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a BETWEEN 2900 AND 3000 AND c=123; -} {/SEARCH TABLE t1 USING INDEX t1ca/} +} {/SEARCH t1 USING INDEX t1ca/} do_execsql_test analyzeE-3.3 { EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a BETWEEN 1700 AND 1750 AND c=123; -} {/SEARCH TABLE t1 USING INDEX t1ca/} +} {/SEARCH t1 USING INDEX t1ca/} do_execsql_test analyzeE-3.4 { EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a BETWEEN 1 AND 500 AND c=123 -} {/SEARCH TABLE t1 USING INDEX t1ca/} +} {/SEARCH t1 USING INDEX t1ca/} do_execsql_test analyzeE-3.5 { EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a BETWEEN 3000 AND 3000000 AND c=123 -} {/SEARCH TABLE t1 USING INDEX t1ca/} +} {/SEARCH t1 USING INDEX t1ca/} do_execsql_test analyzeE-3.6 { EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a<500 AND c=123 -} {/SEARCH TABLE t1 USING INDEX t1ca/} +} {/SEARCH t1 USING INDEX t1ca/} do_execsql_test analyzeE-3.7 { EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a>2500 AND c=123 -} {/SEARCH TABLE t1 USING INDEX t1ca/} +} {/SEARCH t1 USING INDEX t1ca/} do_execsql_test analyzeE-3.8 { EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a>1900 AND c=123 -} {/SEARCH TABLE t1 USING INDEX t1ca/} +} {/SEARCH t1 USING INDEX t1ca/} do_execsql_test analyzeE-3.9 { EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a>1100 AND c=123 -} {/SCAN TABLE t1/} +} {/SCAN t1/} do_execsql_test analyzeE-3.10 { EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a<1100 AND c=123 -} {/SEARCH TABLE t1 USING INDEX t1ca/} +} {/SEARCH t1 USING INDEX t1ca/} do_execsql_test analyzeE-3.11 { EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a<1900 AND c=123 -} {/SCAN TABLE t1/} +} {/SCAN t1/} # Repeat the 3.x tests using a DESCENDING index # @@ -197,46 +197,98 @@ do_execsql_test analyzeE-4.0 { do_execsql_test analyzeE-4.1 { EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a BETWEEN 500 AND 2500 AND c=123; -} {/SCAN TABLE t1/} +} {/SCAN t1/} do_execsql_test analyzeE-4.2 { EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a BETWEEN 2900 AND 3000 AND c=123; -} {/SEARCH TABLE t1 USING INDEX t1ca/} +} {/SEARCH t1 USING INDEX t1ca/} do_execsql_test analyzeE-4.3 { EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a BETWEEN 1700 AND 1750 AND c=123; -} {/SEARCH TABLE t1 USING INDEX t1ca/} +} {/SEARCH t1 USING INDEX t1ca/} do_execsql_test analyzeE-4.4 { EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a BETWEEN 1 AND 500 AND c=123 -} {/SEARCH TABLE t1 USING INDEX t1ca/} +} {/SEARCH t1 USING INDEX t1ca/} do_execsql_test analyzeE-4.5 { EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a BETWEEN 3000 AND 3000000 AND c=123 -} {/SEARCH TABLE t1 USING INDEX t1ca/} +} {/SEARCH t1 USING INDEX t1ca/} do_execsql_test analyzeE-4.6 { EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a<500 AND c=123 -} {/SEARCH TABLE t1 USING INDEX t1ca/} +} {/SEARCH t1 USING INDEX t1ca/} do_execsql_test analyzeE-4.7 { EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a>2500 AND c=123 -} {/SEARCH TABLE t1 USING INDEX t1ca/} +} {/SEARCH t1 USING INDEX t1ca/} do_execsql_test analyzeE-4.8 { EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a>1900 AND c=123 -} {/SEARCH TABLE t1 USING INDEX t1ca/} +} {/SEARCH t1 USING INDEX t1ca/} do_execsql_test analyzeE-4.9 { EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a>1100 AND c=123 -} {/SCAN TABLE t1/} +} {/SCAN t1/} do_execsql_test analyzeE-4.10 { EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a<1100 AND c=123 -} {/SEARCH TABLE t1 USING INDEX t1ca/} +} {/SEARCH t1 USING INDEX t1ca/} do_execsql_test analyzeE-4.11 { EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a<1900 AND c=123 -} {/SCAN TABLE t1/} +} {/SCAN t1/} + +# 2023-03-23 https://sqlite.org/forum/forumpost/dc4854437b +# +reset_db +do_execsql_test analyzeE-5.0 { + PRAGMA encoding = 'UTF-16'; + CREATE TABLE t0 (c1 TEXT); + INSERT INTO t0 VALUES (''); + CREATE INDEX i0 ON t0(c1); + ANALYZE; + SELECT * FROM t0 WHERE t0.c1 BETWEEN '' AND (ABS('')); +} {{}} + +# 2023-03-24 https://sqlite.org/forum/forumpost/bc39e531e5 +# +reset_db +do_execsql_test analyzeE-6.0 { + CREATE TABLE t1(x); + CREATE INDEX i1 ON t1(x,x,x,x,x||2); + CREATE INDEX i2 ON t1(1<2); + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<1000) + INSERT INTO t1(x) SELECT x FROM c; + ANALYZE; +} {} +do_execsql_test analyzeE-6.1 { + SELECT count(*)>1 FROM sqlite_stat4 WHERE idx='i2' AND neq='1000 1'; +} 1 +do_execsql_test analyzeE-6.2 { + SELECT count(*) FROM sqlite_stat4 WHERE idx='i2' AND neq<>'1000 1'; +} 0 +do_execsql_test analyzeE-6.3 { + SELECT count(*)>1 FROM sqlite_stat4 WHERE idx='i1' AND neq='1 1 1 1 1 1'; +} 1 +do_execsql_test analyzeE-6.4 { + SELECT count(*) FROM sqlite_stat4 WHERE idx='i1' AND neq<>'1 1 1 1 1 1'; +} 0 + +# 2023-03-25 https://sqlite.org/forum/forumpost/5275207102 +# Correctly expand zeroblobs while processing STAT4 information +# during query planning. +# +reset_db +do_execsql_test analyzeE-7.0 { + CREATE TABLE t1(a TEXT COLLATE binary); + CREATE INDEX t1x ON t1(a); + INSERT INTO t1(a) VALUES(0),('apple'),(NULL),(''),('banana'); + ANALYZE; + SELECT format('(%s)',a) FROM t1 WHERE t1.a > CAST(zeroblob(5) AS TEXT); +} {(0) (apple) (banana)} +do_execsql_test analyzeE-7.1 { + SELECT format('(%s)',a) FROM t1 WHERE t1.a <= CAST(zeroblob(5) AS TEXT); +} {()} finish_test diff --git a/test/analyzeF.test b/test/analyzeF.test index 3cbc5f47be..00107993eb 100644 --- a/test/analyzeF.test +++ b/test/analyzeF.test @@ -62,7 +62,7 @@ foreach {tn where idx} { 11 "x = nullif('19', 0) AND y = nullif('4', 0)" {t1y (y=?)} 12 "x = nullif('4', 0) AND y = nullif('19', 0)" {t1y (y=?)} } { - set res "0 0 0 {SEARCH TABLE t1 USING INDEX $idx}" + set res "SEARCH t1 USING INDEX $idx" do_eqp_test 1.$tn "SELECT * FROM t1 WHERE $where" $res } @@ -92,7 +92,7 @@ foreach {tn where idx} { 3 "x = nondet4() AND y = nondet19()" {t1y (y=?)} 4 "x = nondet19() AND y = nondet4()" {t1y (y=?)} } { - set res "0 0 0 {SEARCH TABLE t1 USING INDEX $idx}" + set res "SEARCH t1 USING INDEX $idx" do_eqp_test 3.$tn "SELECT * FROM t1 WHERE $where" $res } @@ -120,5 +120,31 @@ do_catchsql_test 4.4 { SELECT * FROM t1 WHERE x = test_zeroblob(1100000) AND y = 4; } {1 {string or blob too big}} +# 2016-12-08: Constraints of the form "x=? AND x IS NOT NULL" were being +# mishandled. The sqlite3Stat4ProbeSetValue() routine was assuming that +# valueNew() was returning a Mem object that was preset to NULL, which is +# not the case. The consequence was the the "x IS NOT NULL" constraint +# was used to drive the index (via the "x>NULL" pseudo-constraint) rather +# than the "x=?" constraint. +# +do_execsql_test 5.1 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT, c INT); + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<10000) + INSERT INTO t1(a, c) SELECT x, x FROM c; + UPDATE t1 SET b=printf('x%02x',a/500) WHERE a>4000; + UPDATE t1 SET b='xyz' where a>=9998; + CREATE INDEX t1b ON t1(b); + ANALYZE; + SELECT count(*), b FROM t1 GROUP BY 2 ORDER BY 2; +} {4000 {} 499 x08 500 x09 500 x0a 500 x0b 500 x0c 500 x0d 500 x0e 500 x0f 500 x10 500 x11 500 x12 498 x13 3 xyz} +do_execsql_test 5.2 { + explain query plan + SELECT * FROM t1 WHERE b='xyz' AND b IS NOT NULL ORDER BY +a; + /* v---- Should be "=", not ">" */ +} {/USING INDEX t1b .b=/} +do_execsql_test 5.3 { + SELECT * FROM t1 WHERE b='xyz' AND b IS NOT NULL ORDER BY +a; +} {9998 xyz 9998 9999 xyz 9999 10000 xyz 10000} finish_test diff --git a/test/analyzeG.test b/test/analyzeG.test new file mode 100644 index 0000000000..ca65bc3b96 --- /dev/null +++ b/test/analyzeG.test @@ -0,0 +1,84 @@ +# 2020-02-23 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# Tests for functionality related to ANALYZE. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +ifcapable !stat4 { + finish_test + return +} +set testprefix analyzeG + +#------------------------------------------------------------------------- +# Test cases 1.* seek to verify that even if an index is not used, its +# stat4 data may be used by the planner to estimate the number of +# rows that match an unindexed constraint on the same column. +# +do_execsql_test 1.0 { + PRAGMA automatic_index = 0; + CREATE TABLE t1(a, x); + CREATE TABLE t2(b, y); + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<100 + ) + INSERT INTO t1 SELECT (i%50), NULL FROM s; + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<100 + ) + INSERT INTO t2 SELECT (CASE WHEN i<95 THEN 44 ELSE i END), NULL FROM s; +} + +# Join tables t1 and t2. Both contain 100 rows. (a=44) matches 2 rows +# in "t1", (b=44) matches 95 rows in table "t2". But the planner doesn't +# know this, so it has no preference as to which order the tables are +# scanned in. In practice this means that tables are scanned in the order +# they are specified in in the FROM clause. +do_eqp_test 1.1.1 { + SELECT * FROM t1, t2 WHERE a=44 AND b=44; +} { + +} +do_eqp_test 1.1.2 { + SELECT * FROM t2, t1 WHERE a=44 AND b=44 +} { + QUERY PLAN + |--SCAN t2 + `--SCAN t1 +} + +do_execsql_test 1.2 { + CREATE INDEX t2b ON t2(b); + ANALYZE; +} + +# Now, with the ANALYZE data, the planner knows that (b=44) matches a +# large number of rows. So it elects to scan table "t1" first, regardless +# of the order in which the tables are specified in the FROM clause. +do_eqp_test 1.3.1 { + SELECT * FROM t1, t2 WHERE a=44 AND b=44; +} { + QUERY PLAN + |--SCAN t1 + `--SCAN t2 +} +do_eqp_test 1.3.2 { + SELECT * FROM t2, t1 WHERE a=44 AND b=44 +} { + QUERY PLAN + |--SCAN t1 + `--SCAN t2 +} + + +finish_test diff --git a/test/analyzer1.test b/test/analyzer1.test index ac46704fba..1f0b0f6376 100644 --- a/test/analyzer1.test +++ b/test/analyzer1.test @@ -19,15 +19,18 @@ ifcapable !vtab { return } -if {$tcl_platform(platform)=="windows"} { +if {$tcl_platform(platform) eq "windows"} { set PROG "sqlite3_analyzer.exe" } else { set PROG "./sqlite3_analyzer" } if {![file exe $PROG]} { - puts "analyzer1 cannot run because $PROG is not available" - finish_test - return + set PROG [file normalize [file join $::cmdlinearg(TESTFIXTURE_HOME) $PROG]] + if {![file exe $PROG]} { + puts "analyzer1 cannot run because $PROG is not available" + finish_test + return + } } db close forcedelete test.db test.db-journal test.db-wal diff --git a/test/async.test b/test/async.test deleted file mode 100644 index e1bc08642e..0000000000 --- a/test/async.test +++ /dev/null @@ -1,90 +0,0 @@ -# -# May you do good and not evil. -# May you find forgiveness for yourself and forgive others. -# May you share freely, never taking more than you give. -# -#*********************************************************************** -# This file runs all tests. -# -# $Id: async.test,v 1.21 2009/06/05 17:09:12 drh Exp $ - -set testdir [file dirname $argv0] -source $testdir/tester.tcl - -if {[info commands sqlite3async_initialize] eq ""} { - # The async logic is not built into this system - finish_test - return -} - -rename finish_test async_really_finish_test -proc finish_test {} { - catch {db close} - catch {db2 close} - catch {db3 close} -} -if {[info exists G(isquick)]} { set ASYNC_SAVE_ISQUICK $G(isquick) } -set G(isquick) 1 - -set ASYNC_INCLUDE { - insert.test - insert2.test - insert3.test - lock.test - lock2.test - lock3.test - select1.test - select2.test - select3.test - select4.test - trans.test -} - -# Enable asynchronous IO. -sqlite3async_initialize "" 1 - -# This proc flushes the contents of the async-IO queue through to the -# underlying VFS. A couple of the test scripts identified in $ASYNC_INCLUDE -# above contain lines like "catch flush_async_queue" in places where -# this is required for the tests to work in async mode. -# -proc flush_async_queue {} { - sqlite3async_control halt idle - sqlite3async_start - sqlite3async_wait - sqlite3async_control halt never -} - -rename do_test async_really_do_test -proc do_test {name args} { - uplevel async_really_do_test async_io-$name $args - flush_async_queue -} - -foreach testfile [lsort -dictionary [glob $testdir/*.test]] { - set tail [file tail $testfile] - if {[lsearch -exact $ASYNC_INCLUDE $tail]<0} continue - source $testfile - - # Make sure everything is flushed through. This is because [source]ing - # the next test file will delete the database file on disk (using - # [delete_file]). If the asynchronous backend still has the file - # open, it will become confused. - # - flush_async_queue -} - -# Flush the write-queue and disable asynchronous IO. This should ensure -# all allocated memory is cleaned up. -set sqlite3async_trace 1 -flush_async_queue -sqlite3async_shutdown -set sqlite3async_trace 0 - -rename do_test {} -rename async_really_do_test do_test -rename finish_test {} -rename async_really_finish_test finish_test - -if {[info exists ASYNC_SAVE_ISQUICK]} { set G(isquick) $ASYNC_SAVE_ISQUICK } -finish_test diff --git a/test/async2.test b/test/async2.test deleted file mode 100644 index 7994a7219d..0000000000 --- a/test/async2.test +++ /dev/null @@ -1,126 +0,0 @@ -# -# May you do good and not evil. -# May you find forgiveness for yourself and forgive others. -# May you share freely, never taking more than you give. -# -#*********************************************************************** -# -# $Id: async2.test,v 1.12 2009/04/25 08:39:15 danielk1977 Exp $ - - -set testdir [file dirname $argv0] -source $testdir/tester.tcl - -if { - [info commands sqlite3async_initialize]=="" || - [info command sqlite3_memdebug_fail]=="" -} { - # The async logic is not built into this system - puts "Skipping async2 tests: not compiled with required features" - finish_test - return -} - -# Enable asynchronous IO. - -set setup_script { - CREATE TABLE counter(c); - INSERT INTO counter(c) VALUES (1); -} - -set sql_script { - BEGIN; - UPDATE counter SET c = 2; - CREATE TABLE t1(a PRIMARY KEY, b, c); - CREATE TABLE t2(a PRIMARY KEY, b, c); - COMMIT; - - BEGIN; - UPDATE counter SET c = 3; - INSERT INTO t1 VALUES('abcdefghij', 'four', 'score'); - INSERT INTO t2 VALUES('klmnopqrst', 'and', 'seven'); - COMMIT; - - UPDATE counter SET c = 'FIN'; -} - -db close - -foreach err [list ioerr malloc-transient malloc-persistent] { - set ::go 10 - for {set n 1} {$::go} {incr n} { - set ::sqlite_io_error_pending 0 - sqlite3_memdebug_fail -1 - forcedelete test.db test.db-journal - sqlite3 db test.db - execsql $::setup_script - db close - - sqlite3async_initialize "" 1 - sqlite3 db test.db - sqlite3_db_config_lookaside db 0 0 0 - - switch -- $err { - ioerr { set ::sqlite_io_error_pending $n } - malloc-persistent { sqlite3_memdebug_fail $n -repeat 1 } - malloc-transient { sqlite3_memdebug_fail $n -repeat 0 } - } - - catchsql $::sql_script - db close - - sqlite3async_control halt idle - sqlite3async_start - sqlite3async_wait - sqlite3async_control halt never - sqlite3async_shutdown - - set ::sqlite_io_error_pending 0 - sqlite3_memdebug_fail -1 - - sqlite3 db test.db - set c [db one {SELECT c FROM counter LIMIT 1}] - switch -- $c { - 1 { - do_test async-$err-1.1.$n { - execsql { - SELECT name FROM sqlite_master; - } - } {counter} - } - 2 { - do_test async-$err-1.2.$n.1 { - execsql { - SELECT * FROM t1; - } - } {} - do_test async-$err-1.2.$n.2 { - execsql { - SELECT * FROM t2; - } - } {} - } - 3 { - do_test async-$err-1.3.$n.1 { - execsql { - SELECT * FROM t1; - } - } {abcdefghij four score} - do_test async-$err-1.3.$n.2 { - execsql { - SELECT * FROM t2; - } - } {klmnopqrst and seven} - } - FIN { - incr ::go -1 - } - } - - db close - } -} - -catch {db close} - -finish_test diff --git a/test/async3.test b/test/async3.test deleted file mode 100644 index 9336b66058..0000000000 --- a/test/async3.test +++ /dev/null @@ -1,76 +0,0 @@ -# 2007 September 5 -# -# The author disclaims copyright to this source code. In place of -# a legal notice, here is a blessing: -# -# May you do good and not evil. -# May you find forgiveness for yourself and forgive others. -# May you share freely, never taking more than you give. -# -#*********************************************************************** -# -# The focus of this file is testing the code in test_async.c. -# Specifically, it tests that the xFullPathname() method of -# of the asynchronous vfs works correctly. -# -# $Id: async3.test,v 1.5 2009/04/25 08:39:15 danielk1977 Exp $ - -set testdir [file dirname $argv0] -source $testdir/tester.tcl - -if { [info commands sqlite3async_initialize]=="" } { - # The async logic is not built into this system - puts "Skipping async3 tests: not compiled with required features" - finish_test - return -} - -db close -sqlite3async_initialize "" 1 -#set sqlite3async_trace 1 -sqlite3async_start - -set paths { - chocolate/banana/vanilla/file.db - chocolate//banana/vanilla/file.db - chocolate/./banana//vanilla/file.db - chocolate/banana/./vanilla/file.db - chocolate/banana/../banana/vanilla/file.db - chocolate/banana/./vanilla/extra_bit/../file.db -} - -do_test async3-1.0 { - file mkdir [file join chocolate banana vanilla] - forcedelete chocolate/banana/vanilla/file.db - forcedelete chocolate/banana/vanilla/file.db-journal -} {} - -do_test async3-1.1 { - sqlite3 db chocolate/banana/vanilla/file.db - execsql { - CREATE TABLE abc(a, b, c); - BEGIN; - INSERT INTO abc VALUES(1, 2, 3); - } -} {} - -set N 2 -foreach p $paths { - sqlite3 db2 $p - do_test async3-1.$N.1 { - execsql {SELECT * FROM abc} db2 - } {} - do_test async3-1.$N.2 { - catchsql {INSERT INTO abc VALUES(4, 5, 6)} db2 - } {1 {database is locked}} - db2 close - incr N -} - -db close - -sqlite3async_control halt idle -sqlite3async_wait -sqlite3async_control halt never -sqlite3async_shutdown -finish_test diff --git a/test/async4.test b/test/async4.test deleted file mode 100644 index 92a820173e..0000000000 --- a/test/async4.test +++ /dev/null @@ -1,168 +0,0 @@ -# 2009 April 25 -# -# The author disclaims copyright to this source code. In place of -# a legal notice, here is a blessing: -# -# May you do good and not evil. -# May you find forgiveness for yourself and forgive others. -# May you share freely, never taking more than you give. -# -#*********************************************************************** -# -# $Id: async4.test,v 1.4 2009/06/05 17:09:12 drh Exp $ - -set testdir [file dirname $argv0] -source $testdir/tester.tcl - -# Do not use a codec for tests in this file, as the database file is -# manipulated directly using tcl scripts (using the [hexio_write] command). -# -do_not_use_codec - -# These tests only work for Tcl version 8.5 and later on Windows (for now) -# -if {$tcl_platform(platform)=="windows"} { - scan $::tcl_version %f vx - if {$vx<8.5} { - finish_test - return - } -} - -if {[info commands sqlite3async_initialize] eq ""} { - # The async logic is not built into this system - finish_test - return -} -db close - -# Test layout: -# -# async4.1.*: Test the lockfiles parameter. -# async4.2.*: Test the delay parameter. - -do_test async4.1.1 { - sqlite3async_initialize {} 0 - sqlite3async_control lockfiles -} {1} -do_test async4.1.2 { - sqlite3async_control lockfiles false -} {0} -do_test async4.1.3 { - sqlite3async_control lockfiles -} {0} -do_test async4.1.4 { - sqlite3async_control lockfiles true -} {1} - -do_test async4.1.5 { - sqlite3 db test.db -vfs sqlite3async - execsql { CREATE TABLE t1(a, b, c) } -} {} -do_test async4.1.6 { - list [file exists test.db] [file size test.db] -} {1 0} -do_test async4.1.7 { - sqlite3 db2 test.db - catchsql { CREATE TABLE t2(a, b, c) } db2 -} {1 {database is locked}} -do_test async4.1.8 { - sqlite3async_control halt idle - sqlite3async_start - sqlite3async_wait -} {} -do_test async4.1.9 { - catchsql { CREATE TABLE t2(a, b, c) } db2 -} {0 {}} -do_test async4.1.10 { - list [catch {sqlite3async_control lockfiles false} msg] $msg -} {1 SQLITE_MISUSE} -do_test async4.1.11 { - db close - list [catch {sqlite3async_control lockfiles false} msg] $msg -} {1 SQLITE_MISUSE} -do_test async4.1.12 { - sqlite3async_start - sqlite3async_wait - sqlite3async_control lockfiles false -} {0} -do_test async4.1.13 { - sqlite3 db test.db -vfs sqlite3async - execsql { CREATE TABLE t3(a, b, c) } db -} {} -do_test async4.1.14 { - execsql { - CREATE INDEX i1 ON t2(a); - CREATE INDEX i2 ON t1(a); - } db2 -} {} -do_test async4.1.15 { - sqlite3async_start - sqlite3async_wait - hexio_write test.db 28 00000000 - execsql { pragma integrity_check } db2 -} {{*** in database main *** -Page 5 is never used}} -do_test async4.1.16 { - db close - db2 close - sqlite3async_start - sqlite3async_wait -} {} -do_test async4.1.17 { - sqlite3async_control lockfiles true -} {1} - -do_test async4.2.1 { - sqlite3async_control delay -} {0} -do_test async4.2.2 { - sqlite3async_control delay 23 -} {23} -do_test async4.2.3 { - sqlite3async_control delay -} {23} -do_test async4.2.4 { - sqlite3async_control delay 0 -} {0} -do_test async4.2.5 { - sqlite3 db test.db -vfs sqlite3async - - execsql { CREATE TABLE t4(a, b) } - set T1 [lindex [time { - sqlite3async_start - sqlite3async_wait - }] 0] - - sqlite3async_control delay 100 - execsql { CREATE TABLE t5(a, b) } - set T2 [lindex [time { - sqlite3async_start - sqlite3async_wait - }] 0] - - expr {($T1+1000000) < $T2} -} {1} - -do_test async4.2.6 { - sqlite3async_control delay 0 - execsql { CREATE TABLE t6(a, b) } - set T1 [lindex [time { - sqlite3async_start - sqlite3async_wait - }] 0] - - expr {($T1+1000000) < $T2} -} {1} - -do_test async4.2.7 { - list [catch { sqlite3async_control delay -1 } msg] $msg -} {1 SQLITE_MISUSE} - -do_test async4.2.8 { - db close - sqlite3async_start - sqlite3async_wait -} {} - -finish_test diff --git a/test/async5.test b/test/async5.test deleted file mode 100644 index abac11f750..0000000000 --- a/test/async5.test +++ /dev/null @@ -1,68 +0,0 @@ -# 2009 July 19 -# -# May you do good and not evil. -# May you find forgiveness for yourself and forgive others. -# May you share freely, never taking more than you give. -# -#*********************************************************************** -# This file tests that asynchronous IO is compatible with multi-file -# transactions. -# -# $Id: async5.test,v 1.1 2009/07/18 11:52:04 danielk1977 Exp $ - -set testdir [file dirname $argv0] -source $testdir/tester.tcl - -if {[info commands sqlite3async_initialize] eq ""} { - # The async logic is not built into this system - finish_test - return -} - -db close -forcedelete test2.db -sqlite3async_initialize "" 1 -sqlite3async_control halt never -sqlite3 db test.db - -do_test async5-1.1 { - execsql { - ATTACH 'test2.db' AS next; - CREATE TABLE main.t1(a, b); - CREATE TABLE next.t2(a, b); - BEGIN; - INSERT INTO t1 VALUES(1, 2); - INSERT INTO t2 VALUES(3, 4); - COMMIT; - } -} {} -do_test async5-1.2 { - execsql { SELECT * FROM t1 } -} {1 2} -do_test async5-1.3 { - execsql { SELECT * FROM t2 } -} {3 4} -do_test async5-1.4 { - execsql { - BEGIN; - INSERT INTO t1 VALUES('a', 'b'); - INSERT INTO t2 VALUES('c', 'd'); - COMMIT; - } -} {} -do_test async5-1.5 { - execsql { SELECT * FROM t1 } -} {1 2 a b} -do_test async5-1.6 { - execsql { SELECT * FROM t2 } -} {3 4 c d} - -db close - -sqlite3async_control halt idle -sqlite3async_start -sqlite3async_wait -sqlite3async_control halt never -sqlite3async_shutdown -set sqlite3async_trace 0 -finish_test diff --git a/test/atof1.test b/test/atof1.test index 55f5e44d96..39747e5da5 100644 --- a/test/atof1.test +++ b/test/atof1.test @@ -15,14 +15,10 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl -if {$::longdouble_size<=8} { - finish_test - return -} - +set mxpow 35 expr srand(1) for {set i 1} {$i<20000} {incr i} { - set pow [expr {int((rand()-0.5)*100)}] + set pow [expr {int((rand()-0.5)*$mxpow)}] set x [expr {pow((rand()-0.5)*2*rand(),$pow)}] set xf [format %.32e $x] @@ -47,14 +43,82 @@ for {set i 1} {$i<20000} {incr i} { set y [db eval {SELECT $x=CAST(quote($x) AS real)}] if {!$y} { db eval {SELECT real2hex($x) a, real2hex(CAST(quote($x) AS real)) b} {} - puts "\nIN: $a $xf" - puts [format {QUOTE: %16s %s} {} [db eval {SELECT quote($x)}]] + puts "" + if {$x<0} { + puts "[format {!SCALE: %17s 1 23456789 123456789 123456789} {}]" + } else { + puts "[format {!SCALE: %16s 1 23456789 123456789 123456789} {}]" + } + puts "!IN: $a $xf" + puts [format {!QUOTE: %16s %s} {} [db eval {SELECT quote($x)}]] db eval {SELECT CAST(quote($x) AS real) c} {} - puts "OUT: $b [format %.32e $c]" + puts "!OUT: $b [format %.32e $c]" } set y } {1} } +# 2020-01-08 ticket 9eda2697f5cc1aba +# When running sqlite3AtoF() on a blob with an odd number of bytes using +# UTF16, ignore the last byte so that the string has an integer number of +# UTF16 code points. +# +reset_db +do_execsql_test atof1-2.10 { + PRAGMA encoding = 'UTF16be'; + CREATE TABLE t1(a, b); + INSERT INTO t1(rowid,a) VALUES (1,x'00'),(2,3); + SELECT substr(a,',') is true FROM t1 ORDER BY rowid; +} {0 1} +do_execsql_test atof1-2.20 { + SELECT substr(a,',') is true FROM t1 ORDER BY rowid DESC; +} {1 0} +do_execsql_test atof1-2.30 { + CREATE INDEX i1 ON t1(a); + SELECT count(*) FROM t1 WHERE substr(a,','); +} {1} +# 2020-08-27 OSSFuzz find related to the above. +do_execsql_test atof1-2.40 { + SELECT randomblob(0) - 1; +} {-1} + +# 2024-12-07 https://sqlite.org/forum/forumpost/569a7209179a7f5e +# Incorrect conversion of floating point or integer literals that +# have significant digits that begin with 1844674407370955 followed +# by more digits in the range 0592 throgh 1609. +# +do_execsql_test atof-3.1 { + WITH RECURSIVE bigval(i,vtxt) AS ( + SELECT 0, '18446744073709550000' + UNION ALL + SELECT i+1, format('1844674407370955%04d',i+1) FROM bigval + WHERE i+1<=9999 + ) + SELECT vtxt, CAST(vtxt AS REAL) FROM bigval + WHERE CAST(vtxt AS REAL) NOT GLOB '1.8446744073709[56]*'; +} {} +do_execsql_test atof-3.2 { + WITH RECURSIVE bigval(i,vtxt) AS ( + SELECT 0, '18.446744073709550000' + UNION ALL + SELECT i+1, format('18.44674407370955%04d',i+1) FROM bigval + WHERE i+1<=9999 + ) + SELECT vtxt, CAST(vtxt AS REAL) FROM bigval + WHERE CAST(vtxt AS REAL) NOT GLOB '18.446744073709*'; +} {} +do_execsql_test atof-3.3 { + WITH RECURSIVE exp(n,v1,v2) AS ( + SELECT -200, '1.8446744073709550592e-200', '1.8446744073709551609e-200' + UNION ALL + SELECT n+1, ('1.8446744073709550592e'||n),('1.8446744073709551609e'||n) + FROM exp WHERE n<200 + ) + SELECT n, v1, v2 + FROM exp + WHERE format('%.10e',CAST(v1 AS REAL)) NOT GLOB '1.8446*' + OR format('%.10e',CAST(v2 AS REAL)) NOT GLOB '1.8446*'; +} {} + finish_test diff --git a/test/atomic.test b/test/atomic.test new file mode 100644 index 0000000000..6ce6debb73 --- /dev/null +++ b/test/atomic.test @@ -0,0 +1,41 @@ +# 2015-11-07 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is testing the WITH clause. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set ::testprefix atomic + +db close +if {[atomic_batch_write test.db]==0} { + puts "No f2fs atomic-batch-write support. Skipping tests..." + finish_test + return +} + +reset_db + +do_execsql_test 1.0 { + CREATE TABLE t1(x, y); + BEGIN; + INSERT INTO t1 VALUES(1, 2); +} + +do_test 1.1 { file exists test.db-journal } {0} + +do_execsql_test 1.2 { + COMMIT; +} + + +finish_test diff --git a/test/atomic2.test b/test/atomic2.test new file mode 100644 index 0000000000..5b786586bf --- /dev/null +++ b/test/atomic2.test @@ -0,0 +1,95 @@ +# 2018-07-15 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is testing that if an IO error is encountered +# as part of an atomic F2FS commit, an attempt is made to commit the +# transaction using a legacy journal commit. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/malloc_common.tcl +set ::testprefix atomic2 + +db close +if {[atomic_batch_write test.db]==0} { + puts "No f2fs atomic-batch-write support. Skipping tests..." + finish_test + return +} + +reset_db + +do_execsql_test 1.0 { + CREATE TABLE t1(x, y); + CREATE INDEX i1x ON t1(x); + CREATE INDEX i2x ON t1(y); + + WITH s(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<100 ) + INSERT INTO t1 SELECT randomblob(400), randomblob(400) FROM s; +} + +set setup [list \ + -injectstart at_injectstart \ + -injectstop at_injectstop \ +] + +set ::at_fail 0 +set ::at_nfail 0 + +proc at_injectstart {iFail} { + set ::at_fail $iFail + set ::at_nfail 0 +} +proc at_injectstop {} { + set ::at_fail 0 + return $::at_nfail +} + +proc at_vfs_callback {method file z args} { + if {$::at_fail>0} { + incr ::at_fail -1 + if {$::at_fail==0} { + incr ::at_nfail + return SQLITE_IOERR + } elseif {$method=="xFileControl" && $z=="COMMIT_ATOMIC_WRITE"} { + set ::at_fail 0 + } + } + return SQLITE_OK +} + +testvfs tvfs -default 1 +tvfs script at_vfs_callback +tvfs filter {xFileControl xWrite} + +faultsim_save_and_close + +do_one_faultsim_test 2.0 {*}$setup -prep { + faultsim_restore_and_reopen +} -body { + execsql { + WITH s(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<100 ) + INSERT INTO t1 SELECT randomblob(400), randomblob(400) FROM s; + } +} -test { + faultsim_test_result {0 {}} + + set res [execsql {SELECT count(*) FROM t1; PRAGMA integrity_check}] + if {$res!="200 ok"} { + error "expected {200 ok}, got $res" + } +} + +db close +tvfs delete + +finish_test diff --git a/test/atrc.c b/test/atrc.c new file mode 100644 index 0000000000..673f12cc44 --- /dev/null +++ b/test/atrc.c @@ -0,0 +1,150 @@ +/* +** This program generates a script that stresses the ALTER TABLE statement. +** Compile like this: +** +** gcc -g -c sqlite3.c +** gcc -g -o atrc atrc.c sqlite3.o -ldl -lpthread +** +** Run the program this way: +** +** ./atrc DATABASE | ./sqlite3 DATABASE +** +** This program "atrc" generates a script that can be fed into an ordinary +** command-line shell. The script performs many ALTER TABLE statements, +** runs ".schema --indent" and "PRAGMA integrity_check;", does more +** ALTER TABLE statements to restore the original schema, and then +** runs "PRAGMA integrity_check" again. Every table and column has its +** name changed. The entire script is contained within BEGIN...ROLLBACK +** so that no changes are ever actually made to the database. +*/ +#include "sqlite3.h" +#include <stdio.h> + +/* +** Generate the text of ALTER TABLE statements that will rename +** every column in table zTable to a generic name composed from +** zColPrefix and a sequential number. The generated text is +** appended pConvert. If pUndo is not NULL, then SQL text that +** will undo the change is appended to pUndo. +** +** The table to be converted must be in the "main" schema. +*/ +int rename_all_columns_of_table( + sqlite3 *db, /* Database connection */ + const char *zTab, /* Table whose columns should all be renamed */ + const char *zColPrefix, /* Prefix for new column names */ + sqlite3_str *pConvert, /* Append ALTER TABLE statements here */ + sqlite3_str *pUndo /* SQL to undo the change, if not NULL */ +){ + sqlite3_stmt *pStmt; + int rc; + int cnt = 0; + + rc = sqlite3_prepare_v2(db, + "SELECT name FROM pragma_table_info(?1);", + -1, &pStmt, 0); + if( rc ) return rc; + sqlite3_bind_text(pStmt, 1, zTab, -1, SQLITE_STATIC); + while( sqlite3_step(pStmt)==SQLITE_ROW ){ + const char *zCol = (const char*)sqlite3_column_text(pStmt, 0); + cnt++; + sqlite3_str_appendf(pConvert, + "ALTER TABLE \"%w\" RENAME COLUMN \"%w\" TO \"%w%d\";\n", + zTab, zCol, zColPrefix, cnt + ); + if( pUndo ){ + sqlite3_str_appendf(pUndo, + "ALTER TABLE \"%w\" RENAME COLUMN \"%w%d\" TO \"%w\";\n", + zTab, zColPrefix, cnt, zCol + ); + } + } + sqlite3_finalize(pStmt); + return SQLITE_OK; +} + +/* Rename all tables and their columns in the main database +*/ +int rename_all_tables( + sqlite3 *db, /* Database connection */ + sqlite3_str *pConvert, /* Append SQL to do the rename here */ + sqlite3_str *pUndo /* Append SQL to undo the rename here */ +){ + sqlite3_stmt *pStmt; + int rc; + int cnt = 0; + + rc = sqlite3_prepare_v2(db, + "SELECT name FROM sqlite_schema WHERE type='table'" + " AND name NOT LIKE 'sqlite_%';", + -1, &pStmt, 0); + if( rc ) return rc; + while( sqlite3_step(pStmt)==SQLITE_ROW ){ + const char *zTab = (const char*)sqlite3_column_text(pStmt, 0); + char *zNewTab; + char zPrefix[2]; + + zPrefix[0] = (cnt%26) + 'a'; + zPrefix[1] = 0; + zNewTab = sqlite3_mprintf("tx%d", ++cnt); + if( pUndo ){ + sqlite3_str_appendf(pUndo, + "ALTER TABLE \"%s\" RENAME TO \"%w\";\n", + zNewTab, zTab + ); + } + rename_all_columns_of_table(db, zTab, zPrefix, pConvert, pUndo); + sqlite3_str_appendf(pConvert, + "ALTER TABLE \"%w\" RENAME TO \"%s\";\n", + zTab, zNewTab + ); + sqlite3_free(zNewTab); + } + sqlite3_finalize(pStmt); + return SQLITE_OK; +} + +/* +** Generate a script that does this: +** +** (1) Start a transaction +** (2) Rename all tables and columns to use generic names. +** (3) Print the schema after this rename +** (4) Run pragma integrity_check +** (5) Do more ALTER TABLE statements to change the names back +** (6) Run pragma integrity_check again +** (7) Rollback the transaction +*/ +int main(int argc, char **argv){ + sqlite3 *db; + int rc; + sqlite3_str *pConvert; + sqlite3_str *pUndo; + char *zDbName; + char *zSql1, *zSql2; + if( argc!=2 ){ + fprintf(stderr, "Usage: %s DATABASE\n", argv[0]); + } + zDbName = argv[1]; + rc = sqlite3_open(zDbName, &db); + if( rc ){ + fprintf(stderr, "sqlite3_open() returns %d\n", rc); + return 1; + } + pConvert = sqlite3_str_new(db); + pUndo = sqlite3_str_new(db); + rename_all_tables(db, pConvert, pUndo); + zSql1 = sqlite3_str_finish(pConvert); + zSql2 = sqlite3_str_finish(pUndo); + sqlite3_close(db); + printf("BEGIN;\n"); + printf("%s", zSql1); + sqlite3_free(zSql1); + printf(".schema --indent\n"); + printf("PRAGMA integrity_check;\n"); + printf("%s", zSql2); + sqlite3_free(zSql2); + printf("PRAGMA integrity_check;\n"); + printf("ROLLBACK;\n"); + return 0; +} diff --git a/test/attach.test b/test/attach.test index 31c24e61d9..3445c43fa7 100644 --- a/test/attach.test +++ b/test/attach.test @@ -148,10 +148,8 @@ do_test attach-1.14 { ATTACH 'test.db' as db9; } } {1 {database db9 is already in use}} -do_test attach-1.15 { - catchsql { - ATTACH 'test.db' as main; - } +do_catchsql_test attach-1.15 { + ATTACH 'test.db' as main; } {1 {database main is already in use}} ifcapable tempdb { do_test attach-1.16 { @@ -160,10 +158,8 @@ ifcapable tempdb { } } {1 {database temp is already in use}} } -do_test attach-1.17 { - catchsql { - ATTACH 'test.db' as MAIN; - } +do_catchsql_test attach-1.17 { + ATTACH 'test.db' as MAIN; } {1 {database MAIN is already in use}} do_test attach-1.18 { catchsql { @@ -193,7 +189,7 @@ do_test attach-1.20.2 { } ;# ifcapable schema_pragmas integrity_check attach-1.20.3 ifcapable tempdb { - execsql {select * from sqlite_temp_master} + execsql {select * from temp.sqlite_master} } do_test attach-1.21 { catchsql { @@ -231,6 +227,7 @@ do_test attach-1.26 { } } {1 {cannot detach database main}} + ifcapable tempdb { do_test attach-1.27 { catchsql { @@ -726,6 +723,32 @@ ifcapable subquery { } db2 } {1 {trigger r5 cannot reference objects in database temp}} } ;# endif subquery +ifcapable json1&&vtab { + do_test attach-5.10 { + db close + catch {db2 close} + forcedelete test.db + sqlite3 db test.db + db eval { + CREATE TABLE t1(x); + CREATE TABLE t2(a,b); + CREATE TRIGGER x1 AFTER INSERT ON t1 BEGIN + INSERT INTO t2(a,b) SELECT key, value FROM json_each(NEW.x); + END; + INSERT INTO t1(x) VALUES('{"a":1}'); + SELECT * FROM t2; + } + } {a 1} + do_test attach-5.11 { + sqlite3 db2 :memory: + db2 eval { + CREATE TABLE t3(y); + ATTACH 'test.db' AS aux; + INSERT INTO aux.t1(x) VALUES('{"b":2}'); + SELECT * FROM aux.t2; + } + } {a 1 b 2} +} ;# endif json1 } ;# endif trigger # Check to make sure we get a sensible error if unable to open @@ -736,7 +759,7 @@ do_test attach-6.1 { ATTACH DATABASE 'no-such-file' AS nosuch; } } {0 {}} -if {$tcl_platform(platform)=="unix"} { +if {$tcl_platform(platform) eq "unix"} { do_test attach-6.2 { sqlite3 dbx cannot-read dbx eval {CREATE TABLE t1(a,b,c)} @@ -792,7 +815,7 @@ do_test attach-8.1 { catchsql { ATTACH 'test2.db' AS t2; } -} {1 {file is encrypted or is not a database}} +} {1 {file is not a database}} do_test attach-8.2 { db errorcode } {26} @@ -870,5 +893,36 @@ do_execsql_test attach-11.1 { SELECT * FROM aux1.t1; } {1 2 3 4} +# Ticket https://sqlite.org/src/tktview/a4e06e75a9ab61a1 2017-07-15 +# False positive when running integrity_check on a connection with +# attached databases. +# +db close +sqlite3 db :memory: +do_execsql_test attach-12.1 { + CREATE TABLE Table1 (col TEXT NOT NULL PRIMARY KEY); + ATTACH ':memory:' AS db2; + CREATE TABLE db2.Table2(col1 INTEGER, col2 INTEGER, col3 INTEGER, col4); + CREATE UNIQUE INDEX db2.idx_col1_unique ON Table2 (col1); + CREATE UNIQUE INDEX db2.idx_col23_unique ON Table2 (col2, col3); + CREATE INDEX db2.idx_col2 ON Table2 (col2); + INSERT INTO Table2 VALUES(1,2,3,4); + PRAGMA integrity_check; +} {ok} + +# 2021-03-10 Forum post https://sqlite.org/forum/forumpost/a006d86f72 +# +reset_db +do_test attach-13.1 { + sqlite3 db :memory: + db eval {CREATE TABLE base(x);} + for {set i 0} {$i<$SQLITE_MAX_ATTACHED} {incr i} { + db eval "ATTACH ':memory:' AS a$i" + } + set m "a[expr {$SQLITE_MAX_ATTACHED-1}]" + db eval "CREATE TABLE $m.t1(a INTEGER PRIMARY KEY, b);" + db eval "CREATE TABLE $m.t2(a INTEGER PRIMARY KEY, b);" + db eval {SELECT a FROM t1 WHERE b IN (SELECT a FROM t2);} +} {} finish_test diff --git a/test/attach2.test b/test/attach2.test index f870568541..91e14ee7a4 100644 --- a/test/attach2.test +++ b/test/attach2.test @@ -17,6 +17,7 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl +set testprefix attach2 ifcapable !attach { finish_test @@ -374,24 +375,77 @@ do_test attach2-6.1 { do_test attach2-6.2 { catchsql { ATTACH 'test3.db' as aux2; + DETACH aux2; } -} {1 {cannot ATTACH database within transaction}} +} {0 {}} -# EVIDENCE-OF: R-59740-55581 This statement will fail if SQLite is in -# the middle of a transaction. +# As of version 3.21.0: it is ok to DETACH from within a transaction # do_test attach2-6.3 { catchsql { DETACH aux; } -} {1 {cannot DETACH database within transaction}} -do_test attach2-6.4 { - execsql { - COMMIT; - DETACH aux; - } -} {} +} {0 {}} db close +ifcapable utf16 { + forcedelete test.db2 ;# utf-16 + forcedelete test.db3 ;# utf-16 + forcedelete test.db4 ;# utf-8 + + sqlite3 db2 test.db2 + do_execsql_test -db db2 1.1 { + PRAGMA encoding = 'utf16'; + CREATE TABLE t2(x); + INSERT INTO t2 VALUES('text2'); + } + db2 close + + sqlite3 db3 test.db3 + do_execsql_test -db db3 1.2 { + PRAGMA encoding = 'utf16'; + CREATE TABLE t3(x); + INSERT INTO t3 VALUES('text3'); + } + db3 close + + sqlite3 db4 test.db4 + do_execsql_test -db db4 1.3 { + PRAGMA encoding = 'utf8'; + CREATE TABLE t4(x); + INSERT INTO t4 VALUES('text4'); + } + db4 close + + reset_db + do_execsql_test 2.1 { + PRAGMA encoding = 'utf16'; + ATTACH 'test.db2' AS aux; + SELECT * FROM t2; + } {text2} + + reset_db + do_execsql_test 2.2 { + ATTACH 'test.db4' AS aux; + SELECT * FROM t4; + } {text4} + + db close + sqlite3 db test.db2 + do_execsql_test 2.3 { + ATTACH 'test.db3' AS aux; + SELECT * FROM t3; + SELECT * FROM t2; + } {text3 text2} + + db close + sqlite3 db test.db2 + do_catchsql_test 2.4 { + ATTACH 'test.db4' AS aux; + } {1 {attached databases must use the same text encoding as main database}} + + db close +} + finish_test diff --git a/test/attach3.test b/test/attach3.test index 1ac10d97a4..1c8601c7b3 100644 --- a/test/attach3.test +++ b/test/attach3.test @@ -207,7 +207,7 @@ ifcapable tempdb { CREATE TEMP TRIGGER tst_trigger BEFORE INSERT ON aux.t4 BEGIN SELECT 'hello world'; END; - SELECT count(*) FROM sqlite_temp_master; + SELECT count(*) FROM temp.sqlite_master; } } {1} do_test attach3-9.1 { @@ -219,7 +219,7 @@ ifcapable tempdb { do_test attach3-9.2 { execsql { DROP TABLE aux.t4; - SELECT count(*) FROM sqlite_temp_master; + SELECT count(*) FROM temp.sqlite_master; } } {0} } diff --git a/test/attach4.test b/test/attach4.test index 77dd7e4115..9d54e7df57 100644 --- a/test/attach4.test +++ b/test/attach4.test @@ -115,4 +115,23 @@ do_test 1.8 { db close foreach {name f} $files { forcedelete $f } +#------------------------------------------------------------------------- +reset_db +do_execsql_test 2.0 { + ATTACH DATABASE '' AS aux; + CREATE TABLE IF NOT EXISTS aux.t1(a, b); + CREATE TEMPORARY TRIGGER tr1 DELETE ON t1 BEGIN + DELETE FROM t1; + END; + CREATE TABLE temp.t1(a, b); +} + +do_execsql_test 2.1 { + DETACH DATABASE aux; +} + +do_execsql_test 2.2 { + DROP TRIGGER tr1; +} + finish_test diff --git a/test/attachmalloc.test b/test/attachmalloc.test index 7fee1e1b2a..3c6a075b43 100644 --- a/test/attachmalloc.test +++ b/test/attachmalloc.test @@ -18,7 +18,7 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl -ifcapable !memdebug||!attach { +ifcapable !attach { finish_test return } @@ -61,17 +61,18 @@ do_malloc_test attachmalloc-2 -tclprep { ATTACH 'test2.db' AS db1; } -set enable_shared_cache [sqlite3_enable_shared_cache 1] -sqlite3 dbaux test3.db -dbaux eval {SELECT * FROM sqlite_master} -do_malloc_test attachmalloc-3 -sqlbody { - SELECT * FROM sqlite_master; - ATTACH 'test3.db' AS three; -} -cleanup { - db eval { DETACH three } +ifcapable shared_cache { + set enable_shared_cache [sqlite3_enable_shared_cache 1] + sqlite3 dbaux test3.db + dbaux eval {SELECT * FROM sqlite_master} + do_malloc_test attachmalloc-3 -sqlbody { + SELECT * FROM sqlite_master; + ATTACH 'test3.db' AS three; + } -cleanup { + db eval { DETACH three } + } + dbaux close + sqlite3_enable_shared_cache $enable_shared_cache } -dbaux close -sqlite3_enable_shared_cache $enable_shared_cache - finish_test diff --git a/test/auth.test b/test/auth.test index f3c2fa79e8..1d56f70343 100644 --- a/test/auth.test +++ b/test/auth.test @@ -36,12 +36,20 @@ proc_real proc {name arguments script} { do_test auth-1.1.1 { db close set ::DB [sqlite3 db test.db] + proc authx {code arg1 arg2 arg3 arg4 args} {return SQLITE_DENY} proc auth {code arg1 arg2 arg3 arg4 args} { if {$code=="SQLITE_INSERT" && $arg1=="sqlite_master"} { return SQLITE_DENY } return SQLITE_OK } + db authorizer ::authx + # EVIDENCE-OF: R-03993-24285 Only a single authorizer can be in place on + # a database connection at a time. Each call to sqlite3_set_authorizer + # overrides the previous call. + # + # The authx authorizer above is overridden by the auth authorizer below + # authx is never invoked. db authorizer ::auth catchsql {CREATE TABLE t1(a,b,c)} } {1 {not authorized}} @@ -60,6 +68,9 @@ do_test auth-1.1.4 { do_test auth-1.2 { execsql {SELECT name FROM sqlite_master} } {} +# EVIDENCE-OF: R-04452-49349 When the callback returns SQLITE_DENY, the +# sqlite3_prepare_v2() or equivalent call that triggered the authorizer +# will fail with an error message explaining that access is denied. do_test auth-1.3.1 { proc auth {code arg1 arg2 arg3 arg4 args} { if {$code=="SQLITE_CREATE_TABLE"} { @@ -91,7 +102,7 @@ ifcapable tempdb { catchsql {CREATE TEMP TABLE t1(a,b,c)} } {1 {not authorized}} do_test auth-1.6 { - execsql {SELECT name FROM sqlite_temp_master} + execsql {SELECT name FROM temp.sqlite_master} } {} do_test auth-1.7.1 { proc auth {code arg1 arg2 arg3 arg4 args} { @@ -148,7 +159,7 @@ ifcapable tempdb { catchsql {CREATE TEMP TABLE t1(a,b,c)} } {0 {}} do_test auth-1.14 { - execsql {SELECT name FROM sqlite_temp_master} + execsql {SELECT name FROM temp.sqlite_master} } {} do_test auth-1.15 { proc auth {code arg1 arg2 arg3 arg4 args} { @@ -312,6 +323,10 @@ ifcapable attach { } {1 {access to two.t2.b is prohibited}} execsql {DETACH DATABASE two} } +# EVIDENCE-OF: R-38392-49970 If the action code is SQLITE_READ and the +# callback returns SQLITE_IGNORE then the prepared statement statement +# is constructed to substitute a NULL value in place of the table column +# that would have been read if SQLITE_OK had been returned. do_test auth-1.36 { proc auth {code arg1 arg2 arg3 arg4 args} { if {$code=="SQLITE_READ" && $arg1=="t2" && $arg2=="b"} { @@ -561,7 +576,7 @@ ifcapable tempdb { catchsql {DROP TABLE t1} } {0 {}} do_test auth-1.78 { - execsql {SELECT name FROM sqlite_temp_master} + execsql {SELECT name FROM temp.sqlite_master} } {t1} } @@ -632,7 +647,7 @@ ifcapable tempdb { set ::authargs } {v1 {} temp {}} do_test auth-1.90 { - execsql {SELECT name FROM sqlite_temp_master} + execsql {SELECT name FROM temp.sqlite_master} } {t1} } @@ -779,7 +794,7 @@ ifcapable tempdb { } } {1 {not authorized}} do_test auth-1.113 { - execsql {SELECT name FROM sqlite_temp_master} + execsql {SELECT name FROM temp.sqlite_master} } {t1 v1} do_test auth-1.114 { proc auth {code arg1 arg2 arg3 arg4 args} { @@ -823,7 +838,7 @@ ifcapable tempdb { set ::authargs } {v1 {} temp {}} do_test auth-1.121 { - execsql {SELECT name FROM sqlite_temp_master} + execsql {SELECT name FROM temp.sqlite_master} } {t1 v1} do_test auth-1.122 { proc auth {code arg1 arg2 arg3 arg4 args} { @@ -980,7 +995,7 @@ do_test auth-1.139 { set ::authargs } {r1 t1 temp {}} do_test auth-1.140 { - execsql {SELECT name FROM sqlite_temp_master} + execsql {SELECT name FROM temp.sqlite_master} } {t1} do_test auth-1.141 { proc auth {code arg1 arg2 arg3 arg4 args} { @@ -1016,7 +1031,7 @@ do_test auth-1.144 { set ::authargs } {r1 t1 temp {}} do_test auth-1.145 { - execsql {SELECT name FROM sqlite_temp_master} + execsql {SELECT name FROM temp.sqlite_master} } {t1} do_test auth-1.146 { proc auth {code arg1 arg2 arg3 arg4 args} { @@ -1052,7 +1067,7 @@ do_test auth-1.149 { set ::authargs } {r1 t1 temp {}} do_test auth-1.150 { - execsql {SELECT name FROM sqlite_temp_master} + execsql {SELECT name FROM temp.sqlite_master} } {t1 r1} do_test auth-1.151 { @@ -1142,7 +1157,7 @@ do_test auth-1.164 { catchsql {DROP TRIGGER r1} } {1 {not authorized}} do_test auth-1.165 { - execsql {SELECT name FROM sqlite_temp_master} + execsql {SELECT name FROM temp.sqlite_master} } {t1 r1} do_test auth-1.166 { proc auth {code arg1 arg2 arg3 arg4 args} { @@ -1170,7 +1185,7 @@ do_test auth-1.169 { catchsql {DROP TRIGGER r1} } {0 {}} do_test auth-1.170 { - execsql {SELECT name FROM sqlite_temp_master} + execsql {SELECT name FROM temp.sqlite_master} } {t1 r1} do_test auth-1.171 { proc auth {code arg1 arg2 arg3 arg4 args} { @@ -1202,7 +1217,7 @@ do_test auth-1.175 { set ::authargs } {r1 t1 temp {}} do_test auth-1.176 { - execsql {SELECT name FROM sqlite_temp_master} + execsql {SELECT name FROM temp.sqlite_master} } {t1} } ;# ifcapable trigger @@ -1306,7 +1321,7 @@ ifcapable tempdb { catchsql {CREATE INDEX i1 ON t1(b)} } {1 {not authorized}} do_test auth-1.194 { - execsql {SELECT name FROM sqlite_temp_master} + execsql {SELECT name FROM temp.sqlite_master} } {t1} do_test auth-1.195 { proc auth {code arg1 arg2 arg3 arg4 args} { @@ -1350,7 +1365,7 @@ ifcapable tempdb { set ::authargs } {i1 t1 temp {}} do_test auth-1.202 { - execsql {SELECT name FROM sqlite_temp_master} + execsql {SELECT name FROM temp.sqlite_master} } {t1 i1} } @@ -1376,9 +1391,23 @@ do_test auth-1.205 { } catchsql {DROP INDEX i2} } {1 {not authorized}} -do_test auth-1.206 { +do_test auth-1.205a { set ::authargs } {i2 t2 main {}} +db eval { + ATTACH ':memory:' as di205; + CREATE TABLE di205.t1(x); + CREATE INDEX di205.t1x ON t1(x); +} +do_catchsql_test auth-1.205b { + DROP INDEX di205.t1x; +} {1 {not authorized}} +db eval { + DETACH di205; +} +do_test auth-1.206 { + set ::authargs +} {t1x t1 di205 {}} do_test auth-1.207 { execsql {SELECT name FROM sqlite_master} } {t2 i2} @@ -1466,7 +1495,7 @@ ifcapable tempdb { catchsql {DROP INDEX i1} } {0 {}} do_test auth-1.222 { - execsql {SELECT name FROM sqlite_temp_master} + execsql {SELECT name FROM temp.sqlite_master} } {t1 i1} do_test auth-1.223 { proc auth {code arg1 arg2 arg3 arg4 args} { @@ -1482,7 +1511,7 @@ ifcapable tempdb { set ::authargs } {i1 t1 temp {}} do_test auth-1.225 { - execsql {SELECT name FROM sqlite_temp_master} + execsql {SELECT name FROM temp.sqlite_master} } {t1 i1} do_test auth-1.226 { proc auth {code arg1 arg2 arg3 arg4 args} { @@ -1498,7 +1527,7 @@ ifcapable tempdb { set ::authargs } {i1 t1 temp {}} do_test auth-1.228 { - execsql {SELECT name FROM sqlite_temp_master} + execsql {SELECT name FROM temp.sqlite_master} } {t1} } @@ -1606,6 +1635,8 @@ do_test auth-1.248 { set ::authargs } {COMMIT {} {} {}} do_test auth-1.249 { + # EVIDENCE-OF: R-52112-44167 Disable the authorizer by installing a NULL + # callback. db authorizer {} catchsql {ROLLBACK} } {0 {}} @@ -1765,7 +1796,7 @@ ifcapable attach { } } {0 {}} do_test auth-1.267 { - execsql {SELECT name FROM sqlite_temp_master WHERE type='table'} + execsql {SELECT name FROM temp.sqlite_master WHERE type='table'} } {t1x} do_test auth-1.268 { set authargs @@ -2038,6 +2069,15 @@ ifcapable {altertable} { do_test auth-1.302 { set authargs } {main t5 {} {}} + db eval BEGIN + set authargs {} + do_execsql_test auth-1.302-drop-1 { + ALTER TABLE t5 DROP COLUMN new_col_1; + } {} + db eval ROLLBACK + do_test auth-1.302-drop-2 { + set authargs + } {main t5 new_col_1 {}} do_test auth-1.303 { proc auth {code arg1 arg2 arg3 arg4 args} { if {$code=="SQLITE_ALTER_TABLE"} { @@ -2057,6 +2097,16 @@ ifcapable {altertable} { do_test auth-1.305 { set authargs } {main t5 {} {}} + db eval BEGIN + set authargs {} + do_execsql_test auth-1.305-drop-1 { + ALTER TABLE t5 DROP COLUMN new_col_1; + SELECT 1 FROM sqlite_schema WHERE name='t5' AND sql LIKE '%new_col_1%'; + } {1} + db eval ROLLBACK + do_test auth-1.305-drop-2 { + set authargs + } {main t5 new_col_1 {}} do_test auth-1.306 { proc auth {code arg1 arg2 arg3 arg4 args} { if {$code=="SQLITE_ALTER_TABLE"} { @@ -2070,13 +2120,25 @@ ifcapable {altertable} { } } {1 {not authorized}} do_test auth-1.307 { - set x [execsql {SELECT sql FROM sqlite_temp_master WHERE type='t5'}] + set x [execsql {SELECT sql FROM temp.sqlite_master WHERE type='t5'}] regexp new_col_3 $x } {0} - do_test auth-1.308 { set authargs } {main t5 {} {}} + db eval BEGIN + set authargs {} + do_catchsql_test auth-1.308-drop-1 { + ALTER TABLE t5 DROP COLUMN new_col_1; + } {1 {not authorized}} + do_execsql_test auth-1.308-drop-2 { + SELECT 1 FROM sqlite_schema WHERE name='t5' AND sql LIKE '%new_col_1%'; + } {1} + do_test auth-1.308-drop-3 { + set authargs + } {main t5 new_col_1 {}} + db eval ROLLBACK + execsql {DROP TABLE t5} } ;# ifcapable altertable @@ -2116,6 +2178,90 @@ ifcapable {cte} { } {1 {not authorized}} } ;# ifcapable cte +# +# db eval {SELECT sql FROM temp.sqlite_master} {puts "TEMP: $sql;"} +# db eval {SELECT sql FROM main.sqlite_master} {puts "MAIN: $sql;"} +# +# MAIN: CREATE TABLE "t2"(a,b,c); +# MAIN: CREATE TABLE t4(a,b,c); +# MAIN: CREATE INDEX t4i1 ON t4(a); +# MAIN: CREATE INDEX t4i2 ON t4(b,a,c); +# MAIN: CREATE TABLE sqlite_stat1(tbl,idx,stat); +# MAIN: CREATE TABLE t1(a,b); +# +ifcapable altertable&&vtab { + do_test auth-1.350 { + proc auth {code arg1 arg2 arg3 arg4 args} { + if {$code=="SQLITE_ALTER_TABLE"} { + set ::authargs [list $arg1 $arg2 $arg3 $arg4] + return SQLITE_OK + } + return SQLITE_OK + } + catchsql { + ALTER TABLE t1 RENAME COLUMN b TO bcdefg; + } + } {0 {}} + do_execsql_test auth-1.351 { + SELECT name FROM pragma_table_info('t1') ORDER BY cid; + } {a bcdefg} + do_test auth-1.352 { + set authargs + } {main t1 {} {}} + do_test auth-1.353 { + proc auth {code arg1 arg2 arg3 arg4 args} { + if {$code=="SQLITE_ALTER_TABLE"} { + set ::authargs [list $arg1 $arg2 $arg3 $arg4] + return SQLITE_IGNORE + } + return SQLITE_OK + } + catchsql { + ALTER TABLE t1 RENAME COLUMN bcdefg TO b; + } + } {0 {}} + do_execsql_test auth-1.354 { + SELECT name FROM pragma_table_info('t1') ORDER BY cid; + } {a bcdefg} + do_test auth-1.355 { + set authargs + } {main t1 {} {}} + do_test auth-1.356 { + proc auth {code arg1 arg2 arg3 arg4 args} { + if {$code=="SQLITE_ALTER_TABLE"} { + set ::authargs [list $arg1 $arg2 $arg3 $arg4] + return SQLITE_DENY + } + return SQLITE_OK + } + catchsql { + ALTER TABLE t1 RENAME COLUMN bcdefg TO b; + } + } {1 {not authorized}} + do_execsql_test auth-1.357 { + SELECT name FROM pragma_table_info('t1') ORDER BY cid; + } {a bcdefg} + do_test auth-1.358 { + set authargs + } {main t1 {} {}} +} + +# 2022-12-28 +# The sqlite3_declare_vtab() call that occurs during pragma_table_list +# should not cause an authentication failure. +# +ifcapable vtab { + do_test auth-1.359 { + proc auth {code arg1 arg2 arg3 arg4 args} { + if {$code=="SQLITE_UPDATE"} { + return SQLITE_DENY + } + return SQLITE_OK + } + catchsql {SELECT * FROM pragma_table_list WHERE name='xyzzy';} + } {0 {}} +} + do_test auth-2.1 { proc auth {code arg1 arg2 arg3 arg4 args} { if {$code=="SQLITE_READ" && $arg1=="t3" && $arg2=="x"} { @@ -2364,16 +2510,12 @@ ifcapable compound&&subquery { ifcapable stat4 { set stat4 "sqlite_stat4 " } else { - ifcapable stat3 { - set stat4 "sqlite_stat3 " - } else { - set stat4 "" - } + set stat4 "" } do_test auth-5.2 { execsql { SELECT name FROM ( - SELECT * FROM sqlite_master UNION ALL SELECT * FROM sqlite_temp_master) + SELECT * FROM sqlite_master UNION ALL SELECT * FROM temp.sqlite_master) WHERE type='table' ORDER BY name } @@ -2432,8 +2574,105 @@ do_test auth-6.3 { execsql {SELECT rowid, * FROM t6} } {101 1 2 3 4 5 6 7 8} -rename proc {} -rename proc_real proc +#------------------------------------------------------------------------- +# Test that view names are included as zArg4. +# +do_execsql_test auth-7.1 { + CREATE TABLE t7(a, b, c); + CREATE VIEW v7 AS SELECT * FROM t7; +} {} +set ::authargs [list] +proc auth {args} { + eval lappend ::authargs [lrange $args 0 4] + return SQLITE_OK +} +do_test auth-7.2 { + execsql {SELECT a, c FROM v7} + set ::authargs +} [list \ + SQLITE_SELECT {} {} {} {} \ + SQLITE_READ t7 a main v7 \ + SQLITE_READ t7 b main v7 \ + SQLITE_READ t7 c main v7 \ + SQLITE_READ v7 a main {} \ + SQLITE_READ v7 c main {} \ + SQLITE_SELECT {} {} {} v7 \ +] + +set ::authargs [list] +do_test auth-7.3 { + execsql {SELECT a, c FROM t7} + set ::authargs +} [list \ + SQLITE_SELECT {} {} {} {} \ + SQLITE_READ t7 a main {} \ + SQLITE_READ t7 c main {} \ +] + +set ::authargs [list] +do_test auth-7.4 { + execsql {SELECT a, c FROM t7 AS v7} + set ::authargs +} [list \ + SQLITE_SELECT {} {} {} {} \ + SQLITE_READ t7 a main {} \ + SQLITE_READ t7 c main {} \ +] + +# If a table is referenced but no columns are read from the table, +# that causes a single SQLITE_READ authorization with a NULL column +# name. +# +# EVIDENCE-OF: R-31520-16302 When a table is referenced by a SELECT but +# no column values are extracted from that table (for example in a query +# like "SELECT count(*) FROM tab") then the SQLITE_READ authorizer +# callback is invoked once for that table with a column name that is an +# empty string. +# +set ::authargs [list] +do_test auth-8.1 { + execsql {SELECT count(*) FROM t7} + set ::authargs +} [list \ + SQLITE_SELECT {} {} {} {} \ + SQLITE_FUNCTION {} count {} {} \ + SQLITE_READ t7 {} {} {} \ + ] +set ::authargs [list] +do_test auth-8.2 { + execsql {SELECT t6.a FROM t6, t7} + set ::authargs +} [list \ + SQLITE_SELECT {} {} {} {} \ + SQLITE_READ t6 a main {} \ + SQLITE_READ t7 {} {} {} \ + ] + +# Test also that if SQLITE_DENY is returned from an SQLITE_READ authorizer +# invocation with no column name specified, compilation fails. +# +set ::authargs [list] +proc auth {op args} { + foreach {a b c d} $args break + lappend ::authargs $op $a $b $c $d + if {$op == "SQLITE_READ"} { return "SQLITE_DENY" } + return "SQLITE_OK" +} +set ::authargs [list] +do_catchsql_test auth-8.3 { + SELECT count(*) FROM t7 +} {1 {not authorized}} +do_test auth-8.4 { + set ::authargs +} [list \ + SQLITE_SELECT {} {} {} {} \ + SQLITE_FUNCTION {} count {} {} \ + SQLITE_READ t7 {} {} {} \ +] + + +rename proc {} +rename proc_real proc finish_test diff --git a/test/auth2.test b/test/auth2.test index a9d64d08af..08d46cac57 100644 --- a/test/auth2.test +++ b/test/auth2.test @@ -98,12 +98,6 @@ SQLITE_UPDATE sqlite_master tbl_name main {} SQLITE_UPDATE sqlite_master rootpage main {} SQLITE_UPDATE sqlite_master sql main {} SQLITE_READ sqlite_master ROWID main {} -SQLITE_READ sqlite_master name main {} -SQLITE_READ sqlite_master rootpage main {} -SQLITE_READ sqlite_master sql main {} -SQLITE_READ sqlite_master tbl_name main {} -SQLITE_READ sqlite_master type main {} -SQLITE_READ sqlite_master ROWID main {} } do_test auth2-2.2 { set ::authargs {} @@ -119,12 +113,6 @@ SQLITE_UPDATE sqlite_master tbl_name main {} SQLITE_UPDATE sqlite_master rootpage main {} SQLITE_UPDATE sqlite_master sql main {} SQLITE_READ sqlite_master ROWID main {} -SQLITE_READ sqlite_master name main {} -SQLITE_READ sqlite_master rootpage main {} -SQLITE_READ sqlite_master sql main {} -SQLITE_READ sqlite_master tbl_name main {} -SQLITE_READ sqlite_master type main {} -SQLITE_READ sqlite_master ROWID main {} } do_test auth2-2.3 { set ::authargs {} diff --git a/test/auth3.test b/test/auth3.test index eef10b398f..abc973433e 100644 --- a/test/auth3.test +++ b/test/auth3.test @@ -12,8 +12,7 @@ # Test that the truncate optimization is disabled if the SQLITE_DELETE # authorization callback returns SQLITE_IGNORE. # -# $Id: auth3.test,v 1.2 2009/05/04 01:58:31 drh Exp $ -# +# Test that authorizer is disabled during schema parsing. set testdir [file dirname $argv0] source $testdir/tester.tcl @@ -54,6 +53,10 @@ do_test auth3.1.2 { set ::authcode SQLITE_DENY catchsql { DELETE FROM t1 } } {1 {not authorized}} +# EVIDENCE-OF: R-64962-58611 If the authorizer callback returns any +# value other than SQLITE_IGNORE, SQLITE_OK, or SQLITE_DENY then the +# sqlite3_prepare_v2() or equivalent call that triggered the authorizer +# will fail with an error message. do_test auth3.1.3 { set ::authcode SQLITE_INVALID catchsql { DELETE FROM t1 } @@ -108,4 +111,24 @@ do_test auth3-2.2 { set sqlite_search_count } {1} +# 2016-07-28. A problem report from a private client complaining about +# an authorizer failure during an ALTER TABLE. The solution (I think) is +# to disable the authorizer during schema parsing. +# +ifcapable altertable { + proc auth {code args} { + if {$code=="SQLITE_READ" && [regexp {DoNotRead} $args]} { + return SQLITE_DENY + } + return SQLITE_OK + } + do_execsql_test auth3-3.0 { + CREATE TEMPORARY TABLE TempTable ( + key TEXT NOT NULL ON CONFLICT FAIL UNIQUE ON CONFLICT REPLACE, + value TEXT NOT NULL ON CONFLICT FAIL); + ALTER TABLE TempTable RENAME TO DoNotRead; + SELECT name FROM temp.sqlite_master; + } {DoNotRead sqlite_autoindex_DoNotRead_1} +} + finish_test diff --git a/test/autoanalyze1.test b/test/autoanalyze1.test new file mode 100644 index 0000000000..71e5d6c163 --- /dev/null +++ b/test/autoanalyze1.test @@ -0,0 +1,123 @@ +# 2017-02-17 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# This file implements tests for the logic used to estimate when +# running ANALYZE would be beneficial. +# +# Note that this test uses some hard-coded bitmask values from sqliteInt.h. +# If any of the following constants changes: +# +# define TF_HasStat1 0x0010 +# define TF_StatsUsed 0x0100 +# +# then some of the magic numbers in test results below might need to be +# adjusted. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +# There is nothing to test if ANALYZE is disable for this build. +# These tests also use "PRAGMA stats" which are only enabled for +# debugging builds. +# +ifcapable {!debug || !analyze || !vtab} { + finish_test + return +} + +do_execsql_test autoanalyze1-100 { + -- Build up a test table with some indexes + CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c, d); + CREATE UNIQUE INDEX t1bc ON t1(b,c); + CREATE INDEX t1d ON t1(d); + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<100) + INSERT INTO t1(a,b,c,d) SELECT x, x, x, x FROM c; + -- Verify that the hasStat1 flag is clear on on indexes + SELECT idx, flgs FROM pragma_stats + WHERE idx IS NOT NULL + ORDER BY idx; + -- Verify that the TF_HasStat1 flag is clear on the table + SELECT tbl, (flgs & 0x10)!=0 FROM pragma_stats WHERE tbl='t1' AND idx IS NULL; +} {t1bc 0 t1d 0 t1 0} + +# No use of stat1 recorded so far +do_execsql_test autoanalyze1-110 { + SELECT (flgs & 0x0100)!=0 FROM pragma_stats WHERE tbl='t1' AND idx IS NULL; +} {0} + +# Access using a unique index does not set the TF_StatsUsed flag. +# +do_execsql_test autoanalyze1-200 { + SELECT * FROM t1 WHERE a=55; +} {55 55 55 55} +do_execsql_test autoanalyze1-201 { + SELECT (flgs & 0x0100)!=0 FROM pragma_stats WHERE tbl='t1' AND idx IS NULL; +} {0} + +do_execsql_test autoanalyze1-210 { + SELECT * FROM t1 WHERE a IN (55,199,299); +} {55 55 55 55} +do_execsql_test autoanalyze1-211 { + SELECT (flgs & 0x0100)!=0 FROM pragma_stats WHERE tbl='t1' AND idx IS NULL; +} {0} + +do_execsql_test autoanalyze1-220 { + SELECT * FROM t1 WHERE (b,c)=(45,45); +} {45 45 45 45} +do_execsql_test autoanalyze1-221 { + SELECT (flgs & 0x0100)!=0 FROM pragma_stats WHERE tbl='t1' AND idx IS NULL; +} {0} + +# Any use of the non-unique t1d index triggers the use of stats. +# +sqlite3 db test.db +do_execsql_test autoanalyze1-300 { + SELECT * FROM t1 WHERE d=45; +} {45 45 45 45} +do_execsql_test autoanalyze1-301 { + SELECT (flgs & 0x0100)!=0 FROM pragma_stats WHERE tbl='t1' AND idx IS NULL; +} {1} + +sqlite3 db test.db +do_execsql_test autoanalyze1-310 { + SELECT * FROM t1 WHERE d=45 AND a=45; +} {45 45 45 45} +do_execsql_test autoanalyze1-311 { + SELECT (flgs & 0x0100)!=0 FROM pragma_stats WHERE tbl='t1' AND idx IS NULL; +} {0} ;# The ROWID lookup short-circuits the d=45 constraint + +sqlite3 db test.db +do_execsql_test autoanalyze1-320 { + SELECT * FROM t1 WHERE d=45 AND a IN (45,46); +} {45 45 45 45} +do_execsql_test autoanalyze1-321 { + SELECT (flgs & 0x0100)!=0 FROM pragma_stats WHERE tbl='t1' AND idx IS NULL; +} {1} + +# Any use of prefix of a unique index triggers the use of stats +# +sqlite3 db test.db +do_execsql_test autoanalyze1-400 { + SELECT * FROM t1 WHERE b=45; +} {45 45 45 45} +do_execsql_test autoanalyze1-401 { + SELECT (flgs & 0x0100)!=0 FROM pragma_stats WHERE tbl='t1' AND idx IS NULL; +} {1} + +# The TF_StatsUsed flag is reset when the database is reopened +# +sqlite3 db test.db +do_execsql_test autoanalyze1-500 { + SELECT (flgs & 0x0100)!=0 FROM pragma_stats WHERE tbl='t1' AND idx IS NULL; +} {0} + +finish_test diff --git a/test/autoinc.test b/test/autoinc.test index 239600616b..9f869f35ea 100644 --- a/test/autoinc.test +++ b/test/autoinc.test @@ -16,6 +16,7 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl +set testprefix autoinc # If the library is not compiled with autoincrement support then # skip all tests in this file. @@ -25,6 +26,11 @@ ifcapable {!autoinc} { return } +if {[permutation]=="inmemory_journal"} { + finish_test + return +} + sqlite3_db_config_lookaside db 0 0 0 # The database is initially empty. @@ -344,7 +350,7 @@ ifcapable tempdb { do_test autoinc-4.1 { execsql { SELECT 1, name FROM sqlite_master WHERE type='table'; - SELECT 2, name FROM sqlite_temp_master WHERE type='table'; + SELECT 2, name FROM temp.sqlite_master WHERE type='table'; } } {1 sqlite_sequence} do_test autoinc-4.2 { @@ -663,6 +669,215 @@ ifcapable trigger { } {1 124 2 10123} } +# 2016-10-03 ticket https://sqlite.org/src/tktview/7b3328086a5c1 +# Make sure autoincrement plays nicely with the xfer optimization +# +do_execsql_test autoinc-10.1 { + DELETE FROM sqlite_sequence; + CREATE TABLE t10a(a INTEGER PRIMARY KEY AUTOINCREMENT, b UNIQUE); + INSERT INTO t10a VALUES(888,9999); + CREATE TABLE t10b(x INTEGER PRIMARY KEY AUTOINCREMENT, y UNIQUE); + INSERT INTO t10b SELECT * FROM t10a; + SELECT * FROM sqlite_sequence; +} {t10a 888 t10b 888} + +# 2018-04-21 autoincrement does not cause problems for upsert +# +do_execsql_test autoinc-11.1 { + CREATE TABLE t11(a INTEGER PRIMARY KEY AUTOINCREMENT,b UNIQUE); + INSERT INTO t11(a,b) VALUES(2,3),(5,6),(4,3),(1,2) + ON CONFLICT(b) DO UPDATE SET a=a+1000; + SELECT seq FROM sqlite_sequence WHERE name='t11'; +} {5} + +# 2018-05-23 ticket d8dc2b3a58cd5dc2918a1d4acbba4676a23ada4c +# Does not crash if the sqlite_sequence table schema is missing +# or corrupt. +# +do_test autoinc-12.1 { + db close + forcedelete test.db + sqlite3 db test.db + sqlite3_db_config db DEFENSIVE 0 + db eval { + CREATE TABLE fake_sequence(name TEXT PRIMARY KEY,seq) WITHOUT ROWID; + PRAGMA writable_schema=on; + UPDATE sqlite_master SET + sql=replace(sql,'fake_','sqlite_'), + name='sqlite_sequence', + tbl_name='sqlite_sequence' + WHERE name='fake_sequence'; + } + db close + sqlite3 db test.db + set res [catch {db eval { + CREATE TABLE t1(a INTEGER PRIMARY KEY AUTOINCREMENT, b TEXT); + INSERT INTO t1(b) VALUES('one'); + }} msg] + lappend res $msg +} {1 {database disk image is malformed}} +do_test autoinc-12.2 { + db close + forcedelete test.db + sqlite3 db test.db + sqlite3_db_config db DEFENSIVE 0 + db eval { + CREATE TABLE t1(a INTEGER PRIMARY KEY AUTOINCREMENT, b TEXT); + INSERT INTO t1(b) VALUES('one'); + PRAGMA writable_schema=on; + UPDATE sqlite_master SET + sql=replace(sql,'sqlite_','x_'), + name='x_sequence', + tbl_name='x_sequence' + WHERE name='sqlite_sequence'; + } + db close + sqlite3 db test.db + set res [catch {db eval { + INSERT INTO t1(b) VALUES('two'); + }} msg] + lappend res $msg +} {1 {database disk image is malformed}} +ifcapable vtab { + set err "database disk image is malformed" +} else { + set err {malformed database schema (sqlite_sequence) - near "VIRTUAL": syntax error} +} +do_test autoinc-12.3 { + db close + forcedelete test.db + sqlite3 db test.db + sqlite3_db_config db DEFENSIVE 0 + db eval { + CREATE TABLE t1(a INTEGER PRIMARY KEY AUTOINCREMENT, b TEXT); + INSERT INTO t1(b) VALUES('one'); + PRAGMA writable_schema=on; + UPDATE sqlite_master SET + sql='CREATE VIRTUAL TABLE sqlite_sequence USING sqlite_dbpage' + WHERE name='sqlite_sequence'; + } + db close + sqlite3 db test.db + set res [catch {db eval { + INSERT INTO t1(b) VALUES('two'); + }} msg] + lappend res $msg +} [list 1 $err] +do_test autoinc-12.4 { + db close + forcedelete test.db + sqlite3 db test.db + db eval { + CREATE TABLE t1(a INTEGER PRIMARY KEY AUTOINCREMENT, b TEXT); + INSERT INTO t1(b) VALUES('one'); + CREATE TABLE fake(name TEXT PRIMARY KEY,seq) WITHOUT ROWID; + } + set root1 [db one {SELECT rootpage FROM sqlite_master + WHERE name='sqlite_sequence'}] + set root2 [db one {SELECT rootpage FROM sqlite_master + WHERE name='fake'}] + sqlite3_db_config db DEFENSIVE 0 + db eval { + PRAGMA writable_schema=on; + UPDATE sqlite_master SET rootpage=$root2 + WHERE name='sqlite_sequence'; + UPDATE sqlite_master SET rootpage=$root1 + WHERE name='fake'; + } + db close + sqlite3 db test.db + set res [catch {db eval { + INSERT INTO t1(b) VALUES('two'); + }} msg] + lappend res $msg +} {1 {database disk image is malformed}} +breakpoint +do_test autoinc-12.5 { + db close + forcedelete test.db + sqlite3 db test.db + sqlite3_db_config db DEFENSIVE 0 + db eval { + CREATE TABLE t1(a INTEGER PRIMARY KEY AUTOINCREMENT, b TEXT); + INSERT INTO t1(b) VALUES('one'); + PRAGMA writable_schema=on; + UPDATE sqlite_master SET + sql='CREATE TABLE sqlite_sequence(x)' + WHERE name='sqlite_sequence'; + } + db close + sqlite3 db test.db + set res [catch {db eval { + INSERT INTO t1(b) VALUES('two'); + }} msg] + lappend res $msg +} {1 {database disk image is malformed}} +do_test autoinc-12.6 { + db close + forcedelete test.db + sqlite3 db test.db + sqlite3_db_config db DEFENSIVE 0 + db eval { + CREATE TABLE t1(a INTEGER PRIMARY KEY AUTOINCREMENT, b TEXT); + INSERT INTO t1(b) VALUES('one'); + PRAGMA writable_schema=on; + UPDATE sqlite_master SET + sql='CREATE TABLE sqlite_sequence(x,y INTEGER PRIMARY KEY)' + WHERE name='sqlite_sequence'; + } + db close + sqlite3 db test.db + set res [catch {db eval { + INSERT INTO t1(b) VALUES('two'),('three'),('four'); + INSERT INTO t1(b) VALUES('five'); + PRAGMA integrity_check; + }} msg] + lappend res $msg +} {0 ok} +do_test autoinc-12.7 { + db close + forcedelete test.db + sqlite3 db test.db + sqlite3_db_config db DEFENSIVE 0 + db eval { + CREATE TABLE t1(a INTEGER PRIMARY KEY AUTOINCREMENT, b TEXT); + INSERT INTO t1(b) VALUES('one'); + PRAGMA writable_schema=on; + UPDATE sqlite_master SET + sql='CREATE TABLE sqlite_sequence(y INTEGER PRIMARY KEY,x)' + WHERE name='sqlite_sequence'; + } + db close + sqlite3 db test.db + set res [catch {db eval { + INSERT INTO t1(b) VALUES('two'),('three'),('four'); + INSERT INTO t1(b) VALUES('five'); + PRAGMA integrity_check; + }} msg] + lappend res $msg +} {0 ok} + +#-------------------------------------------------------------------------- +reset_db +do_execsql_test 13.0 { + CREATE TABLE t1(i INTEGER PRIMARY KEY AUTOINCREMENT, j); + CREATE TABLE t2(i INTEGER PRIMARY KEY AUTOINCREMENT, j); + CREATE TABLE t3(i INTEGER PRIMARY KEY AUTOINCREMENT, j); + + INSERT INTO t1 VALUES(NULL, 1); + INSERT INTO t2 VALUES(NULL, 2); + INSERT INTO t3 VALUES(NULL, 3); + + SELECT name FROM sqlite_sequence; +} {t1 t2 t3} + +do_execsql_test 13.1 { + UPDATE sqlite_sequence SET name=NULL WHERE name='t2'; + INSERT INTO t3 VALUES(NULL, 4); + DELETE FROM t3; + INSERT INTO t3 VALUES(NULL, 5); + SELECT * FROM t3; +} {3 5} finish_test diff --git a/test/autoindex1.test b/test/autoindex1.test index e2b8b1529e..1c8ce007f0 100644 --- a/test/autoindex1.test +++ b/test/autoindex1.test @@ -177,35 +177,38 @@ do_execsql_test autoindex1-500 { INSERT INTO sqlite_stat1(tbl,idx,stat) VALUES('t501',null,'1000000'); INSERT INTO sqlite_stat1(tbl,idx,stat) VALUES('t502',null,'1000'); ANALYZE sqlite_master; - EXPLAIN QUERY PLAN +} +do_eqp_test autoindex1-500.1 { SELECT b FROM t501 WHERE t501.a IN (SELECT x FROM t502 WHERE y=?); } { - 0 0 0 {SEARCH TABLE t501 USING INTEGER PRIMARY KEY (rowid=?)} - 0 0 0 {EXECUTE LIST SUBQUERY 1} - 1 0 0 {SCAN TABLE t502} + QUERY PLAN + |--SEARCH t501 USING INTEGER PRIMARY KEY (rowid=?) + `--LIST SUBQUERY xxxxxx + |--SCAN t502 + `--CREATE BLOOM FILTER } -do_execsql_test autoindex1-501 { - EXPLAIN QUERY PLAN +do_eqp_test autoindex1-501 { SELECT b FROM t501 WHERE t501.a IN (SELECT x FROM t502 WHERE y=t501.b); } { - 0 0 0 {SCAN TABLE t501} - 0 0 0 {EXECUTE CORRELATED LIST SUBQUERY 1} - 1 0 0 {SEARCH TABLE t502 USING AUTOMATIC COVERING INDEX (y=?)} + QUERY PLAN + |--SCAN t501 + `--CORRELATED LIST SUBQUERY xxxxxx + |--BLOOM FILTER ON t502 (y=?) + `--SEARCH t502 USING AUTOMATIC COVERING INDEX (y=?) } -do_execsql_test autoindex1-502 { - EXPLAIN QUERY PLAN +do_eqp_test autoindex1-502 { SELECT b FROM t501 WHERE t501.a=123 AND t501.a IN (SELECT x FROM t502 WHERE y=t501.b); } { - 0 0 0 {SEARCH TABLE t501 USING INTEGER PRIMARY KEY (rowid=?)} - 0 0 0 {EXECUTE CORRELATED LIST SUBQUERY 1} - 1 0 0 {SCAN TABLE t502} + QUERY PLAN + |--SEARCH t501 USING INTEGER PRIMARY KEY (rowid=?) + `--CORRELATED LIST SUBQUERY xxxxxx + `--SCAN t502 } - # The following code checks a performance regression reported on the # mailing list on 2010-10-19. The problem is that the nRowEst field # of ephermeral tables was not being initialized correctly and so no @@ -257,7 +260,8 @@ do_execsql_test autoindex1-600 { ON sheep (originating_flock); CREATE INDEX sheep_reg_flock_index ON sheep (registering_flock); - EXPLAIN QUERY PLAN +} +do_eqp_test autoindex1-600a { SELECT x.sheep_no, x.registering_flock, x.date_of_registration FROM sheep x LEFT JOIN (SELECT s.sheep_no, prev.flock_no, prev.owner_person_id, @@ -274,21 +278,26 @@ do_execsql_test autoindex1-600 { WHERE y.sheep_no IS NULL ORDER BY x.registering_flock; } { - 1 0 0 {SCAN TABLE sheep AS s} - 1 1 1 {SEARCH TABLE flock_owner AS prev USING INDEX sqlite_autoindex_flock_owner_1 (flock_no=? AND owner_change_date<?)} - 1 0 0 {EXECUTE CORRELATED SCALAR SUBQUERY 2} - 2 0 0 {SEARCH TABLE flock_owner AS later USING COVERING INDEX sqlite_autoindex_flock_owner_1 (flock_no=? AND owner_change_date>? AND owner_change_date<?)} - 0 0 0 {SCAN TABLE sheep AS x USING INDEX sheep_reg_flock_index} - 0 1 1 {SEARCH SUBQUERY 1 AS y USING AUTOMATIC COVERING INDEX (sheep_no=?)} + QUERY PLAN + |--MATERIALIZE y + | |--SCAN s + | |--SEARCH prev USING INDEX sqlite_autoindex_flock_owner_1 (flock_no=? AND owner_change_date<?) + | `--CORRELATED SCALAR SUBQUERY xxxxxx + | `--SEARCH later USING COVERING INDEX sqlite_autoindex_flock_owner_1 (flock_no=? AND owner_change_date>? AND owner_change_date<?) + |--SCAN x USING INDEX sheep_reg_flock_index + `--SEARCH y USING AUTOMATIC COVERING INDEX (sheep_no=?) LEFT-JOIN } do_execsql_test autoindex1-700 { CREATE TABLE t5(a, b, c); - EXPLAIN QUERY PLAN SELECT a FROM t5 WHERE b=10 ORDER BY c; +} +do_eqp_test autoindex1-700a { + SELECT a FROM t5 WHERE b=10 ORDER BY c; } { - 0 0 0 {SCAN TABLE t5} - 0 0 0 {USE TEMP B-TREE FOR ORDER BY} + QUERY PLAN + |--SCAN t5 + `--USE TEMP B-TREE FOR ORDER BY } # The following checks a performance issue reported on the sqlite-dev @@ -406,7 +415,7 @@ do_execsql_test autoindex1-800 { JOIN raw_contacts ON (data.raw_contact_id=raw_contacts._id) JOIN accounts ON (raw_contacts.account_id=accounts._id) WHERE mimetype_id=10 AND data14 IS NOT NULL; -} {/SEARCH TABLE data .*SEARCH TABLE raw_contacts/} +} {/SEARCH data .*SEARCH raw_contacts/} do_execsql_test autoindex1-801 { EXPLAIN QUERY PLAN SELECT * FROM @@ -414,7 +423,7 @@ do_execsql_test autoindex1-801 { JOIN raw_contacts ON (data.raw_contact_id=raw_contacts._id) JOIN accounts ON (raw_contacts.account_id=accounts._id) WHERE mimetypes._id=10 AND data14 IS NOT NULL; -} {/SEARCH TABLE data .*SEARCH TABLE raw_contacts/} +} {/SEARCH data .*SEARCH raw_contacts/} # Another test case from an important user of SQLite. The key feature of # this test is that the "aggindex" subquery should make use of an @@ -520,4 +529,66 @@ do_execsql_test autoindex1-920 { SELECT * FROM t920,(SELECT 0 FROM t920),(VALUES(9)) WHERE 5 IN (x); } {5 0 9 5 0 9 5 0 9} +#------------------------------------------------------------------------- +# An IS term from the WHERE clause of a LEFT JOIN cannot be used as an +# index driver for the RHS of a LEFT JOIN. Prior to this being fixed, +# the following SELECT count(*) would incorrectly return 1. +# +do_execsql_test autoindex1-1010 { + CREATE TABLE t11(w); + CREATE TABLE t12(y); + INSERT INTO t11 VALUES(NULL); + INSERT INTO t12 VALUES('notnull'); +} +do_execsql_test autoindex1-1020 { + SELECT count(*) FROM t11 LEFT JOIN t12 WHERE t12.y IS t11.w; +} 0 + +# 2022-04-25 +# https://sqlite.org/forum/forumpost/0d3200f4f3bcd3a3 +# +reset_db +do_execsql_test autoindex-1100 { + CREATE TABLE t1(a INT, b INT); + CREATE TABLE t2(c INT, d INT); + CREATE TABLE t3(e TEXT, f TEXT); + INSERT INTO t1 VALUES(1, 1); + INSERT INTO t2 VALUES(1, 2); + INSERT INTO t3 VALUES('abc', 'def'); +} {} +do_execsql_test autoindex-1110 { + SELECT * FROM t1, t2 LEFT JOIN t3 ON (t2.d=1) WHERE t2.c = +t1.a; +} {1 1 1 2 {} {}} +do_execsql_test autoindex-1120 { + SELECT * FROM t1 LEFT JOIN t2 ON (t2.c=+t1.a) LEFT JOIN t3 ON (t2.d IS NULL); +} {1 1 1 2 {} {}} + +# 2025-01-18 +# Added support for automatic indexes on WITHOUT ROWID tables. +# +reset_db +do_execsql_test autoindex-1200 { + CREATE TABLE t1(a INT, b INT, x INT, PRIMARY KEY(a,b)) WITHOUT ROWID; + INSERT INTO t1 VALUES(1,2,90),(1,3,91),(1,4,92); + CREATE TABLE t2a(c INTEGER PRIMARY KEY, i1 INT); + CREATE TABLE t2b(i1 INTEGER PRIMARY KEY, d INT); + CREATE VIEW t2(c,d) AS SELECT c, d FROM t2a NATURAL JOIN t2b; + INSERT INTO t2a VALUES(3,93),(4,94),(5,95),(6,96),(7,97); + INSERT INTO t2b VALUES(91,11),(92,22),(93,33),(94,44),(95,55); + CREATE TABLE dual(dummy TEXT); + INSERT INTO dual(dummy) VALUES('x'); +} +db null NULL +do_execsql_test autoindex-1210 { + SELECT t1.*, t2.* FROM t2 LEFT OUTER JOIN t1 ON b=c ORDER BY +b; +} { + NULL NULL NULL 5 55 + 1 3 91 3 33 + 1 4 92 4 44 +} +do_execsql_test autoindex-1211 { + EXPLAIN QUERY PLAN + SELECT t1.*, t2.* FROM t2 LEFT OUTER JOIN t1 ON b=c ORDER BY +b; +} {/SEARCH t1 USING AUTOMATIC COVERING INDEX/} + finish_test diff --git a/test/autoindex2.test b/test/autoindex2.test index c8abdf410a..afd4a666b7 100644 --- a/test/autoindex2.test +++ b/test/autoindex2.test @@ -218,10 +218,15 @@ do_execsql_test autoindex2-120 { AND t1.did = t2.did AND t2.uid = t3.uid ORDER BY t1.ptime desc LIMIT 500; -} {0 0 0 {SEARCH TABLE t1 USING INDEX t1x1 (ptime>?)} 0 1 1 {SEARCH TABLE t2 USING INDEX t2x0 (did=?)} 0 2 2 {SEARCH TABLE t3 USING INDEX t3x0 (uid=?)}} +} {~/AUTO/} # # ^^^--- Before being fixed, the above was using an automatic covering # on t3 and reordering the tables so that t3 was in the outer loop and # implementing the ORDER BY clause using a B-Tree. +# +# This test is sanitized data received from a user. The original unsanitized +# data and STAT4 data is found in the th3private test repository. See one of +# the th3private check-ins on 2016-02-25. The test is much more accurate when +# STAT4 data is used. finish_test diff --git a/test/autoindex3.test b/test/autoindex3.test index c99a175c6d..aa6aa00128 100644 --- a/test/autoindex3.test +++ b/test/autoindex3.test @@ -74,8 +74,8 @@ do_execsql_test 210 { # At one point, SQLite was using the inferior plan: # -# 0|0|1|SEARCH TABLE v USING INDEX ve (e>?) -# 0|1|0|SEARCH TABLE u USING COVERING INDEX uab (ANY(a) AND b=?) +# 0|0|1|SEARCH v USING INDEX ve (e>?) +# 0|1|0|SEARCH u USING COVERING INDEX uab (ANY(a) AND b=?) # # on the basis that the real index "uab" must be better than the automatic # index. This is not right - a skip-scan is not necessarily better than an @@ -84,9 +84,46 @@ do_execsql_test 210 { do_eqp_test 220 { select count(*) from u, v where u.b = v.b and v.e > 34; } { - 0 0 1 {SEARCH TABLE v USING INDEX ve (e>?)} - 0 1 0 {SEARCH TABLE u USING AUTOMATIC COVERING INDEX (b=?)} + QUERY PLAN + |--SEARCH v USING INDEX ve (e>?) + |--BLOOM FILTER ON u (b=?) + `--SEARCH u USING AUTOMATIC COVERING INDEX (b=?) } +# 2024-05-27 +# ticket https://sqlite.org/src/tktview/8ff324e120 +# forum post https://sqlite.org/forum/forumpost/b21c2101a559be0a +# +# If an index with STAT1 data indicates that a column is not very +# selective, then do not attempt to create an automatic index on +# that column. +# +reset_db +do_execsql_test 300 { + CREATE TABLE t1(id INTEGER PRIMARY KEY); + CREATE TABLE t2(cid INT, pid INT, rx INT, PRIMARY KEY(cid, pid, rx)); + CREATE INDEX x1 ON t2(pid, rx); + ANALYZE sqlite_schema; + REPLACE INTO sqlite_stat1(tbl, idx, stat) VALUES + ('t2', 'x1', '500000 250 250'), + ('t2','sqlite_autoindex_t2_1','500000 1 1 1'); + ANALYZE sqlite_schema; +} +do_eqp_test 310 { + WITH RECURSIVE children(id) AS ( + SELECT cid FROM t2 WHERE pid = ?1 AND rx = ?2 + UNION + SELECT cid FROM t2 JOIN children ON t2.pid = children.id AND rx = ?2 + ) SELECT count(id) FROM children; +} { + QUERY PLAN + |--CO-ROUTINE children + | |--SETUP + | | `--SEARCH t2 USING INDEX x1 (pid=? AND rx=?) + | `--RECURSIVE STEP + | |--SCAN children + | `--SEARCH t2 USING INDEX x1 (pid=? AND rx=?) + `--SCAN children +} finish_test diff --git a/test/autoindex4.test b/test/autoindex4.test index 0e7a80df21..6af99f5e15 100644 --- a/test/autoindex4.test +++ b/test/autoindex4.test @@ -32,12 +32,21 @@ do_execsql_test autoindex4-1.1 { do_execsql_test autoindex4-1.2 { SELECT *, '|' FROM t1 LEFT JOIN t2 ON a=234 AND x=555; } {123 abc {} {} | 234 def {} {} | 234 ghi {} {} | 345 jkl {} {} |} +do_execsql_test autoindex4-1.2-rj { + SELECT t1.*, t2.*, '|' FROM t2 RIGHT JOIN t1 ON a=234 AND x=555; +} {123 abc {} {} | 234 def {} {} | 234 ghi {} {} | 345 jkl {} {} |} do_execsql_test autoindex4-1.3 { SELECT *, '|' FROM t1 LEFT JOIN t2 ON x=555 WHERE a=234; } {234 def {} {} | 234 ghi {} {} |} +do_execsql_test autoindex4-1.3-rj { + SELECT t1.*, t2.*, '|' FROM t2 RIGHT JOIN t1 ON x=555 WHERE a=234; +} {234 def {} {} | 234 ghi {} {} |} do_execsql_test autoindex4-1.4 { SELECT *, '|' FROM t1 LEFT JOIN t2 WHERE a=234 AND x=555; } {} +do_execsql_test autoindex4-1.4-rj { + SELECT t1.*, t2.*, '|' FROM t2 RIGHT JOIN t1 WHERE a=234 AND x=555; +} {} do_execsql_test autoindex4-2.0 { @@ -69,6 +78,14 @@ do_execsql_test autoindex4-3.0 { ORDER BY Items.ItemName; } {Item1 Item2} do_execsql_test autoindex4-3.1 { + SELECT Items.ItemName + FROM A + RIGHT JOIN Items ON (A.Name = Items.ItemName and Items.ItemName = 'dummy') + LEFT JOIN B ON (B.Name = Items.ItemName) + WHERE Items.Name = 'Parent' + ORDER BY Items.ItemName; +} {Item1 Item2} +do_execsql_test autoindex4-3.10 { CREATE INDEX Items_x1 ON Items(ItemName,Name) WHERE ItemName = 'dummy'; SELECT Items.ItemName @@ -78,6 +95,129 @@ do_execsql_test autoindex4-3.1 { WHERE Items.Name = 'Parent' ORDER BY Items.ItemName; } {Item1 Item2} +do_execsql_test autoindex4-3.11 { + SELECT Items.ItemName + FROM A + RIGHT JOIN Items ON (A.Name = Items.ItemName and Items.ItemName = 'dummy') + LEFT JOIN B ON (B.Name = Items.ItemName) + WHERE Items.Name = 'Parent' + ORDER BY Items.ItemName; +} {Item1 Item2} + +# 2021-11-30 - Enhancement to help the automatic index mechanism to +# create a partial index more often. +# +unset -nocomplain id data1 data2 jointype onclause whereclause answer +foreach {id data1 data2 jointype onclause whereclause answer} { + 1 + VALUES(1,2),(3,4) + VALUES(1,2),(3,4) + {LEFT JOIN} + a=x + {y=4 OR y IS NULL} + {3 4 3 4} + + 2 + VALUES(1,2),(3,4) + VALUES(1,2),(3,4) + {LEFT JOIN} + {a=x AND y=4} + {coalesce(y,4)==4} + {1 2 {} {} 3 4 3 4} + + 3 + VALUES(1,2),(3,4) + VALUES(1,2),(3,4) + {JOIN} + {a=x} + {y=4 OR y IS NULL} + {3 4 3 4} + + 4 + VALUES(1,2),(3,4) + VALUES(1,2),(3,4) + {JOIN} + {a=x AND y=4} + {coalesce(y,4)==4} + {3 4 3 4} + + 5.1 + VALUES(1,2),(3,4),(NULL,4) + VALUES(1,2),(3,4) + {LEFT JOIN} + a=x + {y=4 OR y IS NULL} + {3 4 3 4 {} 4 {} {}} + + 5.2 + VALUES(1,2),(3,4),(NULL,4) + VALUES(1,2),(3,4) + {LEFT JOIN} + a=x + {y NOT IN ()} + {1 2 1 2 3 4 3 4 {} 4 {} {}} + + 5.3 + VALUES(1,2),(3,4),(NULL,4) + VALUES(1,2),(3,4) + {LEFT JOIN} + a=x + {y NOT IN (SELECT 1 WHERE false)} + {1 2 1 2 3 4 3 4 {} 4 {} {}} + + 6 + VALUES(1,2),(3,4) + VALUES(1,2),(3,4),(NULL,4) + {LEFT JOIN} + {a=x AND y=4} + {coalesce(y,4)==4} + {1 2 {} {} 3 4 3 4} + + 7 + VALUES(1,2),(3,4),(NULL,4) + VALUES(1,2),(3,4),(NULL,4) + {JOIN} + {a=x} + {y=4 OR y IS NULL} + {3 4 3 4} + + 8 + VALUES(1,2),(3,4) + VALUES(1,2),(3,4) + {JOIN} + {a=x AND y=4} + {coalesce(y,4)==4} + {3 4 3 4} +} { + do_test autoindex4-4.$id.0 { + db eval { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a INT, b INT); + DROP TABLE IF EXISTS t2; + CREATE TABLE t2(x INT, y INT); + } + db eval "INSERT INTO t1(a,b) $data1;" + db eval "INSERT INTO t2(x,y) $data2;" + } {} + set sql "SELECT * FROM t1 $jointype t2 ON $onclause WHERE $whereclause" + # puts "sql = $sql" + do_test autoindex4-4.$id.1 { + db eval {PRAGMA automatic_index=ON;} + db eval $sql + } $answer + do_test autoindex4-4.$id.2 { + db eval {PRAGMA automatic_index=OFF;} + db eval $sql + } $answer + do_test autoindex4-4.$id.3 { + db eval {PRAGMA automatic_index=ON;} + optimization_control db all 0 + db eval $sql + } $answer + optimization_control db all 1 +} + + finish_test diff --git a/test/autoindex5.test b/test/autoindex5.test index 649ae123a4..adfc3e5f76 100644 --- a/test/autoindex5.test +++ b/test/autoindex5.test @@ -84,8 +84,7 @@ do_execsql_test autoindex5-1.0 { # The following query should use an automatic index for the view # in FROM clause of the subquery of the second result column. # -do_execsql_test autoindex5-1.1 { - EXPLAIN QUERY PLAN +do_eqp_test autoindex5-1.1 { SELECT st.bug_name, (SELECT ALL debian_cve.bug FROM debian_cve @@ -103,7 +102,7 @@ do_execsql_test autoindex5-1.1 { AND ( sp.release = 'sid' OR sp.release = 'stretch' OR sp.release = 'jessie' OR sp.release = 'wheezy' OR sp.release = 'squeeze' ) ORDER BY sp.name, st.bug_name, sp.release, sp.subrelease; -} {/SEARCH SUBQUERY 2 USING AUTOMATIC COVERING INDEX .bug_name=/} +} {SEARCH debian_cve USING AUTOMATIC COVERING INDEX (bug_name=?)} #------------------------------------------------------------------------- # Test that ticket [8a2adec1] has been fixed. @@ -124,6 +123,112 @@ do_execsql_test 2.1 { SELECT sum(z) FROM vvv WHERE x='aaa' ) FROM one; } {8.0} + +# At one point the following was returning "no such column: rowid". This +# was incorrect - "rowid" matches against the rowid of table t1 in this +# query. +do_catchsql_test 2.2 { + DROP TABLE t1; + CREATE TABLE t1(aaa); + INSERT INTO t1(aaa) VALUES(9); + SELECT ( + SELECT aaa FROM t1 GROUP BY ( + SELECT bbb FROM ( + SELECT ccc AS bbb FROM ( + SELECT 1 ccc + ) WHERE rowid IS NOT 1 + ) WHERE bbb = 1 + ) + ); +} {0 9} + +# Ticket https://sqlite.org/src/info/787fa716be3a7f65 +# Segfault due to multiple uses of the same subquery where the +# subquery is implemented via coroutine. +# +ifcapable windowfunc { +sqlite3 db :memory: +do_execsql_test 3.0 { + -- This is the original test case reported on the mailing list + CREATE TABLE artists ( + id integer NOT NULL PRIMARY KEY AUTOINCREMENT, + name varchar(255) + ); + CREATE TABLE albums ( + id integer NOT NULL PRIMARY KEY AUTOINCREMENT, + name varchar(255), + artist_id integer REFERENCES artists + ); + INSERT INTO artists (name) VALUES ('Ar'); + INSERT INTO albums (name, artist_id) VALUES ('Al', 1); + SELECT artists.* + FROM artists + INNER JOIN artists AS 'b' ON (b.id = artists.id) + WHERE (artists.id IN ( + SELECT albums.artist_id + FROM albums + WHERE ((name = 'Al') + AND (albums.artist_id IS NOT NULL) + AND (albums.id IN ( + SELECT id + FROM ( + SELECT albums.id, + row_number() OVER ( + PARTITION BY albums.artist_id + ORDER BY name + ) AS 'x' + FROM albums + WHERE (name = 'Al') + ) AS 't1' + WHERE (x = 1) + )) + AND (albums.id IN (1, 2))) + )); +} {1 Ar} +} ;# windowfunc + +# The remaining test cases were discovered (by Dan) during trouble-shooting +sqlite3 db :memory: +do_execsql_test 3.1 { + CREATE TABLE t1 (a); INSERT INTO t1 (a) VALUES (104); + CREATE TABLE t2 (b); INSERT INTO t2 (b) VALUES (104); + CREATE TABLE t3 (c); INSERT INTO t3 (c) VALUES (104); + CREATE TABLE t4 (d); INSERT INTO t4 (d) VALUES (104); + SELECT * + FROM t1 CROSS JOIN t2 ON (t1.a = t2.b) WHERE t2.b IN ( + SELECT t3.c + FROM t3 + WHERE t3.c IN ( + SELECT d FROM (SELECT DISTINCT d FROM t4) AS x WHERE x.d=104 + ) + ); +} {104 104} +sqlite3 db :memory: +do_execsql_test 3.2 { + CREATE TABLE t5(a, b, c, d); + CREATE INDEX t5a ON t5(a); + CREATE INDEX t5b ON t5(b); + CREATE TABLE t6(e); + INSERT INTO t6 VALUES(1); + INSERT INTO t5 VALUES(1,1,1,1), (2,2,2,2); + SELECT * FROM t5 WHERE (a=1 OR b=2) AND c IN ( + SELECT e FROM (SELECT DISTINCT e FROM t6) WHERE e=1 + ); +} {1 1 1 1} +sqlite3 db :memory: +do_execsql_test 3.3 { + CREATE TABLE t1(a1, a2, a3); + CREATE INDEX t1a2 ON t1(a2, a1); + CREATE INDEX t1a3 ON t1(a3, a1); + CREATE TABLE t2(d); + INSERT INTO t1 VALUES(3, 1, 1), (3, 2, 2); + INSERT INTO t2 VALUES(3); + SELECT *, 'x' FROM t1 WHERE (a2=1 OR a3=2) AND a1 = ( + SELECT d FROM (SELECT DISTINCT d FROM t2) WHERE d=3 + ); +} {3 1 1 x 3 2 2 x} + + finish_test diff --git a/test/autovacuum.test b/test/autovacuum.test index 9ee2cd0fa8..245ea8b51d 100644 --- a/test/autovacuum.test +++ b/test/autovacuum.test @@ -9,9 +9,8 @@ # #*********************************************************************** # This file implements regression tests for SQLite library. The -# focus of this file is testing the SELECT statement. +# focus of this file is testing the autovacuum feature. # -# $Id: autovacuum.test,v 1.29 2009/04/06 17:50:03 danielk1977 Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl @@ -269,7 +268,7 @@ do_test autovacuum-2.4.3 { } {3 4 5 6 7 8 9 10} # Right now there are 5 free pages in the database. Consume and then free -# a 520 pages. Then create 520 tables. This ensures that at least some of the +# all 520 pages. Then create 520 tables. This ensures that at least some of the # desired root-pages reside on the second free-list trunk page, and that the # trunk itself is required at some point. do_test autovacuum-2.4.4 { @@ -280,9 +279,20 @@ do_test autovacuum-2.4.4 { } {} set root_page_list [list] set pending_byte_page [expr ($::sqlite_pending_byte / 1024) + 1] + +# unusable_pages +# These are either the pending_byte page or the pointer map pages +# +unset -nocomplain unusable_page +if {[sqlite3 -has-codec]} { + array set unusable_page {205 1 408 1} +} else { + array set unusable_page {207 1 412 1} +} +set unusable_page($pending_byte_page) 1 + for {set i 3} {$i<=532} {incr i} { - # 207 and 412 are pointer-map pages. - if { $i!=207 && $i!=412 && $i != $pending_byte_page} { + if {![info exists unusable_page($i)]} { lappend root_page_list $i } } @@ -694,5 +704,12 @@ do_test autovacuum-9.5 { file size test.db } $::sqlite_pending_byte +do_execsql_test autovacuum-10.1 { + DROP TABLE t1; + CREATE TABLE t1(a INTEGER PRIMARY KEY, b); + INSERT INTO t1 VALUES(25, randomblob(104)); + REPLACE INTO t1 VALUES(25, randomblob(1117)); + PRAGMA integrity_check; +} {ok} finish_test diff --git a/test/autovacuum2.test b/test/autovacuum2.test new file mode 100644 index 0000000000..a3c409839e --- /dev/null +++ b/test/autovacuum2.test @@ -0,0 +1,87 @@ +# 2021-10-15 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is testing the sqlite3_autovacuum_pages() interface +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +# If this build of the library does not support auto-vacuum, omit this +# whole file. +ifcapable {!autovacuum || !pragma} { + finish_test + return +} + +# Demonstrate basic sqlite3_autovacuum_pages functionality +# +do_execsql_test autovacuum2-1.0 { + PRAGMA page_size=1024; + PRAGMA auto_vacuum=FULL; + CREATE TABLE t1(x); + VACUUM; + INSERT INTO t1(x) VALUES(zeroblob(10000)); + PRAGMA page_count; +} {12} +proc autovac_page_callback {schema filesize freesize pagesize} { + global autovac_callback_data + lappend autovac_callback_data $schema $filesize $freesize $pagesize + return [expr {$freesize/2}] +} +sqlite3_autovacuum_pages db autovac_page_callback +set autovac_callback_data {} +do_execsql_test autovacuum2-1.1 { + BEGIN; + DELETE FROM t1; + PRAGMA freelist_count; + PRAGMA page_count; +} {9 12} +do_execsql_test autovacuum2-1.2 { + COMMIT; +} {} +do_test autovacuum2-1.3 { + set autovac_callback_data +} {main 12 9 1024} +do_execsql_test autovacuum2-1.4 { + PRAGMA freelist_count; + PRAGMA page_count; +} {5 8} +do_execsql_test autovacuum2-1.5 { + PRAGMA integrity_check; +} {ok} + +# Disable the autovacuum-pages callback. Then do any transaction. +# The database should shrink to minimal size +# +sqlite3_autovacuum_pages db +do_execsql_test autovacuum2-1.10 { + CREATE TABLE t2(x); + PRAGMA freelist_count; +} {0} + +# Rig the autovacuum-pages callback to always return zero. No +# autovacuum will happen. +# +proc autovac_page_callback_off {schema filesize freesize pagesize} { + return 0 +} +sqlite3_autovacuum_pages db autovac_page_callback_off +do_execsql_test autovacuum2-1.20 { + BEGIN; + INSERT INTO t1(x) VALUES(zeroblob(10000)); + DELETE FROM t1; + PRAGMA freelist_count; + COMMIT; + PRAGMA freelist_count; +} {9 9} + +finish_test diff --git a/test/avfs.test b/test/avfs.test new file mode 100644 index 0000000000..ffd6b309fc --- /dev/null +++ b/test/avfs.test @@ -0,0 +1,396 @@ +# 2021-03-06 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# TESTRUNNER: shell +# +# This file implements tests for the appendvfs extension. +# +# Tests performed: +# avfs-1.0. Test that an appendvfs DB can be added to an empty (ZLF) file. +# avfs-1.1. Test that the DB can be read with correct content upon reopen. +# avfs-1.2. Test that an appendvfs DB can be added to a simple text file. +# avfs-1.3. Test that the DB can be read with correct content upon reopen. +# avfs-1.4. Test that appended DB is aligned to default page boundary. +# avfs-2.1. Test that the simple text file retains its initial text. +# avfs-3.1. Test that the appendvfs can grow and shrink, remaining intact. +# avfs-3.2. Test that appendvfs is intact after grow/shrink/close/reopen. +# avfs-3.3. Test that appendvfs can grow by many pages and be written. +# avfs-3.4. Test that grown appendvfs can be reopened and appear intact. +# avfs-3.5. Test that much grown appendvfs can shrink and reopen intact. +# avfs-4.1. Test shell's ability to append to a non-appendvfs file. +# avfs-4.2. Test shell's ability to append to empty or nonexistent file. +# avfs-4.3. Test shell's ability to reopen and alter an appendvfs file. +# avfs-5.1. Test appendvfs refusal to open too-tiny DB appended onto ZLF. +# avfs-5.2. Test appendvfs refusal to open too-tiny DB appended on other. +# ... +# (more to come) + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set ::testprefix avfs + +# Do not attempt this test if SQLITE_OMIT_VIRTUALTABLE is defined. +# +ifcapable !vtab { + finish_test + return +} + +set CLI [test_find_cli] +db close +# forcedelete test.db + +load_static_extension db appendvfs + +set ::fa avfs.adb +set ::fza avfs.sdb +forcedelete $::fa $::fza +set ::result {} + +proc shellDoesAr {} { + set shdo "sh_app1.sql" + forcedelete $shdo + set fd [open $shdo w] + puts $fd ".help\n.q" + close $fd + set res [catchcmd "-batch -cmd \".read $shdo\""] + return [regexp {^.archive} [lindex $res 1]] +} + +set ::vf "&vfs=apndvfs" + +# Return file offset of appendvfs portion of a file, or {} if none such. +proc fosAvfs {fname} { + if {[file size $fname] < 25} { + return {} + } + if {[catch {set fd [open $fname rb]}]} { + return {} + } + seek $fd -25 end + set am [read $fd 17] + set ao [read $fd 8] + close $fd + if {$am ne "Start-Of-SQLite3-"} { + return {} + } + binary scan $ao "W" rvo + return $rvo +} + +do_test 1.0 { + set results {} + set out [open $::fza wb] + close $out + sqlite3 adb "file:$::fza?mode=rwc$::vf" -uri 1 + adb eval { + PRAGMA page_size=1024; + PRAGMA cache_size=10; + CREATE TABLE t1(a TEXT); + INSERT INTO t1 VALUES ('dog'),('cat'); + SELECT group_concat(a) as pets FROM (SELECT a FROM t1 ORDER BY a); + } { lappend results $pets } + adb close + lappend results [fosAvfs $fza] + set ::result [join $results " | "] +} {cat,dog | 0} + +do_test 1.1 { + set results {} + sqlite3 adb "file:$::fza?mode=rw$::vf" -uri 1 + adb eval { + SELECT group_concat(a) as pets FROM (SELECT a FROM t1 ORDER BY a DESC); + } { lappend results $pets } + adb close + set ::result [join $results " | "] +} {dog,cat} + +do_test 1.2 { + set results {} + set out [open $::fa wb] + set ::tlo { "Just some text," "and more text," "ending at 3 lines." } + puts $out [join $::tlo "\n"] + close $out + set adbSz [file size $::fa] + sqlite3 adb "file:$::fa?mode=rwc$::vf" -uri 1 + adb eval { + PRAGMA auto_vacuum = 0; + PRAGMA page_size=512; + PRAGMA cache_size=0; + CREATE TABLE t1(a TEXT); + INSERT INTO t1 VALUES ('dog'),('cat'),('pig'); + SELECT group_concat(a) as pets FROM (SELECT a FROM t1 ORDER BY a); + } { lappend results $pets } + adb close + set adaSz [file size $::fa] + lappend results "Bytes before/after $adbSz/$adaSz" + set ::result [join $results " | "] +} {cat,dog,pig | Bytes before/after 50/5145} + +do_test 1.3 { + set results {} + sqlite3 adb "file:$::fa?mode=rw$::vf" -uri 1 + adb eval { + SELECT group_concat(a) as pets FROM (SELECT a FROM t1 ORDER BY a DESC); + } { lappend results $pets } + adb close + set ::result [join $results " | "] +} {pig,dog,cat} + +do_test 1.4 { + set ::result [fosAvfs $fa] +} {4096} + +do_test 2.1 { + set in [open $::fa r] + set tli {} + for {set i [llength $::tlo]} {$i > 0} {incr i -1} { + lappend tli [gets $in] + } + close $in + if { [join $tli ":"] ne [join $::tlo ":"] } { + set ::result "Appendee changed." + } else { + set ::result "Appendee intact." + } +} {Appendee intact.} + +# Set of repeatable random integers for a couple tests. +set ::nrint 50000 +proc rint {v} { + return [::tcl::mathfunc::int [expr $v * 100000]] +} +array set ::randints [list 0 [rint [::tcl::mathfunc::srand 0]]] +for {set i 1} {$i < $::nrint} {incr i} { + set ::randints($i) [rint [::tcl::mathfunc::rand]] +} + +do_test 3.1 { + set results {} + sqlite3 adb "file:$::fa?mode=rw$::vf" -uri 1 + adb eval { + DROP TABLE t1; + PRAGMA cache_size=10; + CREATE TABLE ri (i INTEGER); + BEGIN; + } + for {set i 0} {$i < $::nrint} {incr i} { + set r $::randints($i) + set s $::randints([incr i]) + set t $::randints([incr i]) + set u $::randints([incr i]) + set v $::randints([incr i]) + adb eval { + INSERT INTO ri VALUES ($r),($s),($t),($u),($v) + } + } + adb eval { + COMMIT; + SELECT integrity_check as ic FROM pragma_integrity_check(); + } { lappend results $ic } + set adbSz [file size $::fa] + set qr {} + adb eval { + SELECT count(*) as ic FROM ri; + DELETE FROM ri WHERE (i % 50) <> 25; + SELECT integrity_check as ic FROM pragma_integrity_check(); + VACUUM; + SELECT integrity_check as ic FROM pragma_integrity_check(); + SELECT count(*) as ic FROM ri; + } { lappend qr $ic } + adb close + set adaSz [file size $::fa] + set adba [expr ($adbSz + 0.1)/$adaSz] + # lappend results $adba + set results [concat $results [lrange $qr 0 2]] + lappend results [expr {$adba > 10.0}] + set ::result [join $results " | "] +} "ok | $::nrint | ok | ok | 1" + +do_test 3.2 { + set results {} + sqlite3 adb "file:$::fa?mode=rw$::vf" -uri 1 + adb eval { + SELECT integrity_check as ic FROM pragma_integrity_check(); + } { lappend results $ic } + adb close + set ::result [join $results " | "] +} {ok} + +# avfs-3.3. Test that appendvfs can grow by many pages and be written. +do_test 3.3 { + set results {} + sqlite3 adb "file:$::fa?mode=rw$::vf" -uri 1 + set npages 300 + adb eval { BEGIN } + while {$npages > 0} { + adb eval { INSERT INTO ri VALUES (randomblob(1500)) } + incr npages -1 + } + adb eval { COMMIT } + adb eval { + SELECT integrity_check as ic FROM pragma_integrity_check(); + } { lappend results $ic } + adb close + set adaSzr [expr [file size $::fa] / 300.0 / 1500 ] + set okSzr [expr $adaSzr > 1.0 && $adaSzr < 1.3 ] + lappend results $okSzr + set ::result [join $results " | "] +} {ok | 1} + +# avfs-3.4. Test that grown appendvfs can be reopened and appear intact. +do_test 3.4 { + set results {} + sqlite3 adb "file:$::fa?mode=rw$::vf" -uri 1 + adb eval { + SELECT integrity_check as ic FROM pragma_integrity_check(); + } { lappend results $ic } + adb close + set ::result $ic +} {ok} + +# avfs-3.5. Test that much grown appendvfs can shrink and reopen intact. +do_test 3.5 { + set results {} + set adbsz [file size $::fa] + sqlite3 adb "file:$::fa?mode=rw$::vf" -uri 1 + adb eval { + DELETE FROM ri WHERE rowid % 8 <> 0; + SELECT integrity_check as ic FROM pragma_integrity_check(); + VACUUM; + SELECT integrity_check as ic FROM pragma_integrity_check(); + } { lappend results $ic } + adb close + set adasz [file size $::fa] + lappend results [expr {$adbsz/$adasz > 5}] + sqlite3 adb "file:$::fa?mode=rw$::vf" -uri 1 + adb eval { + SELECT integrity_check as ic FROM pragma_integrity_check(); + } { lappend results $ic } + adb close + set ::result [join $results " | "] +} {ok | ok | 1 | ok} + +set ::cliDoesAr [shellDoesAr] + +do_test 4.1 { + set shdo "sh_app1.sql" + set shod "sh_app1.adb" + forcedelete $shdo $shod + set ofd [open $shdo w] + if {$::cliDoesAr} { + puts $ofd ".ar -c" + } else { + puts $ofd "pragma page_size=512;" + puts $ofd "create table sqlar (a);" + } + puts $ofd ".tables" + puts $ofd ".q" + close $ofd + set ofd [open $shod wb] + puts $ofd "Some text." + close $ofd + set res [catchcmd "-append -batch -init $shdo $shod" ""] + lappend res [fosAvfs $shod] + forcedelete $shdo $shod + set ::result [join $res " | "] +} {0 | sqlar | 4096} + +do_test 4.2 { + set shdo "sh_app1.sql" + set shod "sh_app1.adb" + forcedelete $shdo $shod + set ofd [open $shdo w] + if {$::cliDoesAr} { + puts $ofd ".ar -c" + } else { + puts $ofd "pragma page_size=512;" + puts $ofd "create table sqlar (a);" + } + puts $ofd ".tables" + puts $ofd ".q" + close $ofd + set ofd [open $shod wb] + close $ofd + set res [catchcmd "-append -batch -init $shdo $shod" ""] + lappend res [fosAvfs $shod] + forcedelete $shdo ; # Leave $shod for next test. + set ::result [join $res " | "] +} {0 | sqlar | 0} + +do_test 4.3 { + set shdo "sh_app1.sql" + set shod "sh_app1.adb" ; # Same as test 4.2, reusing ADB. + forcedelete $shdo + set ofd [open $shdo w] + if {$::cliDoesAr} { + puts $ofd ".ar -u $shdo" + puts $ofd "select count(*) from sqlar where name = '$shdo';" + } else { + puts $ofd "insert into sqlar values (1);" + puts $ofd "select count(*) from sqlar;" + } + puts $ofd ".q" + close $ofd + set res [catchcmd "-append -batch -init $shdo $shod" ""] + sqlite3 adb "file:$shod?mode=rw$::vf" -uri 1 + adb eval { + SELECT count(*) as n FROM sqlar + } { lappend res $n } + adb close + forcedelete $shdo $shod; + set ::result [join $res " | "] +} {0 | 1 | 1} + +do_test 5.1 { + set fake "faketiny.sdb" + forcedelete $fake + set ofd [open $fake wb] + puts -nonewline $ofd "SQLite format 3" + puts -nonewline $ofd [binary format "c" 0] + puts -nonewline $ofd "Start-Of-SQLite3-" + puts -nonewline $ofd [binary format "W" 0] + close $ofd + if {[catch {sqlite3 adb "file:$fake?mode=rw$::vf" -uri 1}]} { + set res "Open failed." + } else { + adb close + set res "Opened when should not." + } + forcedelete $fake + set ::result $res +} {Open failed.} + +do_test 5.2 { + set fake "faketiny.sdb" + forcedelete $fake + set ofd [open $fake wb] + set fakeAppendee "Dog ate my homework.\n" + puts -nonewline $ofd $fakeAppendee + puts -nonewline $ofd "SQLite format 3" + puts -nonewline $ofd [binary format "c" 0] + puts -nonewline $ofd "Start-Of-SQLite3-" + puts -nonewline $ofd [binary format "W" [string length $fakeAppendee]] + close $ofd + if {[catch {sqlite3 adb "file:$fake?mode=rw$::vf" -uri 1}]} { + set res "Open failed." + } else { + adb close + set res "Opened when should not." + } + forcedelete $fake + set ::result $res +} {Open failed.} + +forcedelete $::fa $::fza + +unset -nocomplain ::fa ::fza ::tlo ::result ::randints ::nrint ::cliDoesAr + +finish_test diff --git a/test/avtrans.test b/test/avtrans.test index 17a2860649..b483c71a44 100644 --- a/test/avtrans.test +++ b/test/avtrans.test @@ -22,7 +22,7 @@ source $testdir/tester.tcl # Create several tables to work with. # do_test avtrans-1.0 { - execsql { PRAGMA auto_vacuum=ON } + execsql { PRAGMA auto_vacuum=full } wal_set_journal_mode execsql { CREATE TABLE one(a int PRIMARY KEY, b text); @@ -32,6 +32,7 @@ do_test avtrans-1.0 { SELECT b FROM one ORDER BY a; } } {one two three} +do_test avtrans-1.0.1 { execsql { PRAGMA auto_vacuum } } 1 do_test avtrans-1.1 { execsql { CREATE TABLE two(a int PRIMARY KEY, b text); @@ -902,7 +903,7 @@ for {set i 2} {$i<=$limit} {incr i} { INSERT INTO t3 SELECT randstr(10,400) FROM t3 WHERE random()%10==0; } } {} - if {$tcl_platform(platform)=="unix"} { + if {$tcl_platform(platform) eq "unix"} { do_test avtrans-9.$i.4-$cnt { expr {$sqlite_sync_count>0} } 1 diff --git a/test/backcompat.test b/test/backcompat.test index ea7e6a9eed..d477d4466c 100644 --- a/test/backcompat.test +++ b/test/backcompat.test @@ -63,7 +63,7 @@ proc do_backcompat_test {rv bin1 bin2 script} { set v [split [db version] .] if {[llength $v]==3} {lappend v 0} set ::sqlite_libversion [format \ - "%d%.2d%.2d%2d" [lindex $v 0] [lindex $v 1] [lindex $v 2] [lindex $v 3] + "%d%.2d%.2d%.2d" [lindex $v 0] [lindex $v 1] [lindex $v 2] [lindex $v 3] ] } } @@ -85,7 +85,8 @@ proc do_allbackcompat_test {script} { set nErr [set_test_counter errors] foreach dir {0 1} { - set bintag [string map {testfixture {}} $bin] + set bintag $bin + regsub {.*testfixture\.} $bintag {} bintag set bintag [string map {\.exe {}} $bintag] if {$bintag == ""} {set bintag self} set ::bcname ".$bintag.$dir." @@ -111,7 +112,7 @@ proc read_file {zFile} { set zData {} if {[file exists $zFile]} { set fd [open $zFile] - fconfigure $fd -translation binary -encoding binary + fconfigure $fd -translation binary if {[file size $zFile]<=$::sqlite_pending_byte || $zFile != "test.db"} { set zData [read $fd] @@ -128,7 +129,7 @@ proc read_file {zFile} { } proc write_file {zFile zData} { set fd [open $zFile w] - fconfigure $fd -translation binary -encoding binary + fconfigure $fd -translation binary puts -nonewline $fd $zData close $fd } @@ -420,6 +421,12 @@ ifcapable fts3 { if {[code1 { set ::sqlite_libversion }] >=3071200 && [code2 { set ::sqlite_libversion }] >=3071200 } { + if {[code1 { set ::sqlite_libversion }]<3120000} { + set res {0 {0 1} 1 0} + } else { + set res {1 0} + } + do_test backcompat-3.9 { sql1 { INSERT INTO t2(t2) VALUES('merge=100,4'); } sql2 { INSERT INTO t2(t2) VALUES('merge=100,4'); } @@ -428,7 +435,7 @@ ifcapable fts3 { sql2 { SELECT level, group_concat(idx, ' ') FROM t2_segdir GROUP BY level; } - } {0 {0 1} 1 0} + } $res do_test backcompat-3.10 { sql1 { INSERT INTO t2(t2) VALUES('integrity-check') } diff --git a/test/backup.test b/test/backup.test index 3b1e1db9e5..ad1a383a08 100644 --- a/test/backup.test +++ b/test/backup.test @@ -164,7 +164,7 @@ foreach zOpenScript [list { set file_dest temp }] { foreach rows_dest {0 3 10} { -foreach pgsz_dest {512 1024 2048} { +foreach pgsz_dest {512 1024 2048 4096} { foreach nPagePerStep {1 200} { # Open the databases. @@ -176,17 +176,16 @@ foreach nPagePerStep {1 200} { # in-memory destination is only possible if the initial destination # page size is the same as the source page size (in this case 1024 bytes). # - set isMemDest [expr { - $zDestFile eq ":memory:" || $file_dest eq "temp" && $TEMP_STORE>=2 - }] - - if { $isMemDest==0 || $pgsz_dest == 1024 } { - if 0 { - puts -nonewline "Test $iTest: src=$zSrcFile dest=$zDestFile" - puts -nonewline " (as $db_dest.$file_dest)" - puts -nonewline " rows_dest=$rows_dest pgsz_dest=$pgsz_dest" - puts "" - } + set isMemDest [expr { $zDestFile eq ":memory:" || $file_dest eq "temp" }] + + if 0 { + puts -nonewline "Test $iTest: src=$zSrcFile dest=$zDestFile" + puts -nonewline " (as $db_dest.$file_dest)" + puts -nonewline " rows_dest=$rows_dest pgsz_dest=$pgsz_dest" + puts "" + } + + if { $isMemDest==0 || $pgsz_dest==1024 || $rows_dest==0 } { # Set up the content of the source database. execsql { @@ -970,4 +969,36 @@ foreach {tn file rc} { db2 close } +# 2021-01-31 https://sqlite.org/forum/forumpost/8b39fbf3e7 +# +do_test backup-11.1 { + sqlite3 db1 :memory: + sqlite3 db2 :memory: + sqlite3_backup B db1 main db2 temp + B finish +} {SQLITE_OK} +db1 close +db2 close + +#------------------------------------------------------------------------- +do_test backup-12.1 { + sqlite3 db1 :memory: + sqlite3 db2 :memory: + db1 eval { + PRAGMA page_size = 8192; + CREATE TABLE t1(x); + } + db2 eval { + PRAGMA page_size = 1024; + CREATE TABLE t2(x); + } + + sqlite3_backup B db1 main db2 temp + B step 100 + B finish +} {SQLITE_READONLY} + + + + finish_test diff --git a/test/backup2.test b/test/backup2.test index 989319923a..095ecc752d 100644 --- a/test/backup2.test +++ b/test/backup2.test @@ -122,7 +122,7 @@ do_test backup2-7 { close $out set rc [catch {db backup temp bu2.db} res] lappend rc $res -} {1 {backup failed: file is encrypted or is not a database}} +} {1 {backup failed: file is not a database}} # Try to backup database that does not exist # @@ -141,10 +141,10 @@ do_test backup2-9 { # Try to restore from an unreadable file. # -if {$tcl_platform(platform)=="windows"} { +if {$::tcl_platform(os) eq "Windows NT"} { set msg {cannot open source database: unable to open database file} -} elseif {$tcl_platform(os)=="OpenBSD"} { - set msg {restore failed: file is encrypted or is not a database} +} elseif {[string match *BSD $tcl_platform(os)]} { + set msg {} } else { set msg {cannot open source database: disk I/O error} } @@ -152,7 +152,8 @@ do_test backup2-10 { forcedelete bu3.db file mkdir bu3.db set rc [catch {db restore temp bu3.db} res] - lappend rc $res + if {[string match *BSD $tcl_platform(os)]} { set res "" } + list $rc $res } [list 1 $msg] # Try to restore from something that is not a database file. @@ -160,7 +161,7 @@ do_test backup2-10 { do_test backup2-11 { set rc [catch {db restore temp bu2.db} res] lappend rc $res -} {1 {restore failed: file is encrypted or is not a database}} +} {1 {restore failed: file is not a database}} # Try to restore a database that does not exist # diff --git a/test/backup4.test b/test/backup4.test index 417df80e55..2756995c3a 100644 --- a/test/backup4.test +++ b/test/backup4.test @@ -23,6 +23,11 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl set testprefix backup4 +# The codec logic does not work for zero-length database files. A database +# file must contain at least one page in order to be recognized as an +# encrypted database. +do_not_use_codec + #------------------------------------------------------------------------- # At one point this test was failing because [db] was using an out of # date schema in test case 1.2. diff --git a/test/backup_malloc.test b/test/backup_malloc.test index f556861fb1..5444a70bdf 100644 --- a/test/backup_malloc.test +++ b/test/backup_malloc.test @@ -84,4 +84,36 @@ do_malloc_test backup_malloc-2 -tclprep { db2 close } +reset_db +do_execsql_test 3.0 { + PRAGMA page_size = 16384; + BEGIN; + CREATE TABLE t1(a, b); + INSERT INTO t1 VALUES(1, 2); + COMMIT; +} + +do_faultsim_test 3 -faults oom* -prep { + catch { db close } + catch { db2 close } + + forcedelete test2.db + sqlite3 db2 test2.db + sqlite3 db test.db + sqlite3_backup B db2 main db main +} -body { + + set rc [B step 50] + if {$rc == "SQLITE_NOMEM" || $rc == "SQLITE_IOERR_NOMEM"} { + error "out of memory" + } + +} -test { + faultsim_test_result {0 {}} + faultsim_integrity_check + + # Finalize the backup. + catch { B finish } +} + finish_test diff --git a/test/badutf2.test b/test/badutf2.test index 36b40fb95c..64a730d6fa 100644 --- a/test/badutf2.test +++ b/test/badutf2.test @@ -98,12 +98,18 @@ foreach { i len uval xstr ustr u2u } { } $uval } - do_test badutf2-4.1.$i { - sqlite3_reset $S - sqlite3_bind_text $S 1 $xstr $len - sqlite3_step $S - utf8_to_ustr2 [ sqlite3_column_text $S 0 ] - } $ustr + # Tcl 8.7 and later do automatic bad-utf8 correction for + # characters 0x80 thru 0x9f so test case 5 does not work here. + if {$i==5 && $tcl_version>=8.7} { + # no-op + } else { + do_test badutf2-4.1.$i { + sqlite3_reset $S + sqlite3_bind_text $S 1 $xstr $len + sqlite3_step $S + utf8_to_ustr2 [ sqlite3_column_text $S 0 ] + } $ustr + } ifcapable debug { do_test badutf2-5.1.$i { diff --git a/test/basexx1.test b/test/basexx1.test new file mode 100644 index 0000000000..b34b25ff36 --- /dev/null +++ b/test/basexx1.test @@ -0,0 +1,168 @@ +# 2022 November 22 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix basexx + +if {[catch {load_static_extension db basexx} error]} { + puts "Skipping basexx tests, hit load error: $error" + finish_test; return +} + +# Empty blobs encode to empty strings. +do_execsql_test 100 { + SELECT base64(x'')||base85(x''); +} {{}} + +# Empty strings decode to empty blobs. +do_execsql_test 101 { + SELECT hex(x'01'||base64('')||base85('')||x'02'); +} {0102} + +# Basic base64 encoding +do_execsql_test 102 { + SELECT base64(x'000102030405'); + SELECT base64(x'0001020304'); + SELECT base64(x'00010203'); +} {{AAECAwQF +} {AAECAwQ= +} {AAECAw== +}} + +# Buffer size testing +do_execsql_test 102-b { + WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c wHERE n<5000) + SELECT sum(length(base64(randomblob(n)))) FROM c; +} {16910656} + +# Basic base64 decoding with pad chars +do_execsql_test 103 { + SELECT hex(base64('AAECAwQF')); + SELECT hex(base64('AAECAwQ=')); + SELECT hex(base64('AAECAw==')); +} {000102030405 0001020304 00010203} + +# Basic base64 decoding without pad chars and with whitespace +do_execsql_test 104 { + SELECT hex(base64(' AAECAwQF ')); + SELECT hex(base64(' AAECAwQ')); + SELECT hex(base64('AAECAw ')); +} {000102030405 0001020304 00010203} + +# Basic base85 encoding +do_execsql_test 105 { + SELECT base85(x'000102030405'); + SELECT base85(x'0001020304'); + SELECT base85(x'00010203'); +} {{##/2,#2/ +} {##/2,#* +} {##/2, +}} + +# Basic base85 decoding with and without whitespace +do_execsql_test 106 { + SELECT hex(base85('##/2,#2/')); + SELECT hex(base85('##/2,#*')); + SELECT hex(base85('##/2,')); + SELECT hex(base85(' ##/2,#2/ ')); + SELECT hex(base85(' ##/2,#*')); + SELECT hex(base85('##/2, ')); +} {000102030405 0001020304 00010203 000102030405 0001020304 00010203} + +# Round-trip some random blobs. +do_execsql_test 107 { + CREATE TEMP TABLE rb( len int, b blob ) STRICT; + INSERT INTO rb(len) VALUES (1),(2),(3),(4),(5),(150),(151),(152),(153),(1054); + UPDATE rb SET b = randomblob(len); + SELECT len, base64(base64(b))=b, base85(base85(b))=b + FROM rb ORDER BY len; +} {1 1 1 2 1 1 3 1 1 4 1 1 5 1 1 150 1 1 151 1 1 152 1 1 153 1 1 1054 1 1} + +# Same round-trip but with space or junk prepended and/or appended or not. +do_execsql_test 108 { + CREATE TEMP TABLE junk(j text, rank int); + INSERT INTO junk VALUES ('',0),(' ',1),('~',2); + SELECT len, base64(j.j||base64(b)||j.j)=b, base85(j.j||base85(b)||j.j)=b + FROM rb r, junk j WHERE j.rank=(r.len+r.len/25)%3 ORDER BY len; +} {1 1 1 2 1 1 3 1 1 4 1 1 5 1 1 150 1 1 151 1 1 152 1 1 153 1 1 1054 1 1} + +# Exercise the fail-on-too-large result feature. + +set inLimit [sqlite3_limit db SQLITE_LIMIT_LENGTH -1] +sqlite3_limit db SQLITE_LIMIT_LENGTH 1300 + +do_catchsql_test 109 { + SELECT len, base64(b) FROM rb WHERE len>200; +} {1 {blob expanded to base64 too big}} + +do_catchsql_test 110 { + SELECT len, base85(b) FROM rb WHERE len>200; +} {1 {blob expanded to base85 too big}} + +do_catchsql_test 111 { + SELECT length(base85(b))=1335 FROM rb WHERE len=1054; +} {1 {blob expanded to base85 too big}} + +sqlite3_limit db SQLITE_LIMIT_LENGTH $inLimit + +# Exercise is_base85(t) + +do_execsql_test 112 { + SELECT is_base85(' '||base85(x'123456')||char(10)), + is_base85('#$%&*+,-./0123456789:;<=>?@' + ||'ABCDEFGHIJKLMNOPQRSTUVWXYZ' + ||'[\]^_`' + ||'abcdefghijklmnopqrstuvwxyz'), + is_base85('!'), is_base85('"'), is_base85(''''), is_base85('('), + is_base85(')'), is_base85(char(123)), is_base85('|'), is_base85(char(125)), + is_base85('~'), is_base85(char(127)); +} {1 1 0 0 0 0 0 0 0 0 0 0} + +do_execsql_test 113 { + SELECT is_base85(NULL) IS NULL; +} {1} + +do_catchsql_test 114 { + SELECT is_base85(1); +} {1 {is_base85 accepts only text or NULL}} + +do_catchsql_test 115 { + SELECT is_base85(1.1); +} {1 {is_base85 accepts only text or NULL}} + +do_catchsql_test 116 { + SELECT is_base85(x'00'); +} {1 {is_base85 accepts only text or NULL}} + +# Round-trip many bigger random blobs. + +do_execsql_test 117 { + CREATE TABLE bs(b blob, num); + INSERT INTO bs SELECT randomblob(4000 + n%3), n + FROM ( + WITH RECURSIVE seq(n) AS ( + VALUES(1) UNION ALL SELECT n+1 + FROM seq WHERE n<100 + ) SELECT n FROM seq); + SELECT num FROM bs WHERE base64(base64(b))!=b; + SELECT num FROM bs WHERE base85(base85(b))!=b; +} {} + +do_catchsql_test 118 { + SELECT base64(zeroblob(2000_000_000)) +} {/1.*too big.*/} +do_catchsql_test 119 { + SELECT base85(zeroblob(2000_000_000)) +} {/1.*too big.*/} + +finish_test diff --git a/test/bc_common.tcl b/test/bc_common.tcl index 78010dfa46..953ce62621 100644 --- a/test/bc_common.tcl +++ b/test/bc_common.tcl @@ -7,9 +7,9 @@ proc bc_find_binaries {zCaption} { # against. # set binaries [list] - set self [file tail [info nameofexec]] + set self [info nameofexec] set pattern "$self?*" - if {$::tcl_platform(platform)=="windows"} { + if {$::tcl_platform(platform) eq "windows"} { set pattern [string map {\.exe {}} $pattern] } foreach file [glob -nocomplain $pattern] { @@ -52,7 +52,8 @@ proc do_bc_test {bin script} { code1 { sqlite3 db test.db } code2 { sqlite3 db test.db } - set bintag [string map {testfixture {}} $bin] + set bintag $bin + regsub {.*testfixture\.} $bintag {} bintag set bintag [string map {\.exe {}} $bintag] if {$bintag == ""} {set bintag self} set saved_prefix $::testprefix diff --git a/test/bestindex1.test b/test/bestindex1.test new file mode 100644 index 0000000000..e505a8bc4c --- /dev/null +++ b/test/bestindex1.test @@ -0,0 +1,344 @@ +# 2016-03-01 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix bestindex1 + +ifcapable !vtab { + finish_test + return +} + +register_tcl_module db + +proc vtab_command {method args} { + switch -- $method { + xConnect { + return "CREATE TABLE t1(a, b, c)" + } + + xBestIndex { + set hdl [lindex $args 0] + set clist [$hdl constraints] + set orderby [$hdl orderby] + + if {[llength $clist]!=1} { error "unexpected constraint list" } + catch { array unset C } + array set C [lindex $clist 0] + if {$C(usable)} { + return "omit 0 cost 0 rows 1 idxnum 555 idxstr eq!" + } else { + return "cost 1000000 rows 0 idxnum 0 idxstr scan..." + } + } + + } + + return {} +} + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE x1 USING tcl(vtab_command); +} {} + +do_eqp_test 1.1 { + SELECT * FROM x1 WHERE a = 'abc' +} {SCAN x1 VIRTUAL TABLE INDEX 555:eq!} + +do_eqp_test 1.2 { + SELECT * FROM x1 WHERE a IN ('abc', 'def'); +} {SCAN x1 VIRTUAL TABLE INDEX 555:eq!} + +#------------------------------------------------------------------------- +# +reset_db +register_tcl_module db + +# Parameter $mode may be one of: +# +# "omit" - Implement filtering. Set the omit flag. +# "use" - Implement filtering. Use the constraint, but do not set omit. +# "use2" - Do not implement filtering. Use the constraint anyway. +# +# +proc t1_vtab {mode method args} { + switch -- $method { + xConnect { + return "CREATE TABLE t1(a, b)" + } + + xBestIndex { + set hdl [lindex $args 0] + set clist [$hdl constraints] + set orderby [$hdl orderby] + + set SQL_FILTER {SELECT * FROM t1x WHERE a='%1%'} + set SQL_SCAN {SELECT * FROM t1x} + + set idx 0 + for {set idx 0} {$idx < [llength $clist]} {incr idx} { + array unset C + array set C [lindex $clist $idx] + if {$C(column)==0 && $C(op)=="eq" && $C(usable)} { + switch -- $mode { + "omit" { + return [list omit $idx rows 10 cost 10 idxstr $SQL_FILTER] + } + "use" { + return [list use $idx rows 10 cost 10 idxstr $SQL_FILTER] + } + "use2" { + return [list use $idx rows 10 cost 10 idxstr $SQL_SCAN] + } + default { + error "Bad mode - $mode" + } + } + } + } + + return [list idxstr {SELECT * FROM t1x}] + } + + xFilter { + set map [list %1% [lindex $args 2 0]] + set sql [string map $map [lindex $args 1]] + return [list sql $sql] + } + } + + return {} +} + +do_execsql_test 2.1 { + CREATE TABLE t1x(i INTEGER PRIMARY KEY, a, b); + INSERT INTO t1x VALUES(1, 'one', 1); + INSERT INTO t1x VALUES(2, 'two', 2); + INSERT INTO t1x VALUES(3, 'three', 3); + INSERT INTO t1x VALUES(4, 'four', 4); +} + +foreach {tn mode} { + 1 use 2 omit 3 use2 +} { + do_execsql_test 2.2.$mode.1 " + DROP TABLE IF EXISTS t1; + CREATE VIRTUAL TABLE t1 USING tcl(t1_vtab $mode); + " + + do_execsql_test 2.2.$mode.2 {SELECT * FROM t1} {one 1 two 2 three 3 four 4} + do_execsql_test 2.2.$mode.3 {SELECT rowid FROM t1} {1 2 3 4} + do_execsql_test 2.2.$mode.4 {SELECT rowid FROM t1 WHERE a='two'} {2} + + do_execsql_test 2.2.$mode.5 { + SELECT rowid FROM t1 WHERE a IN ('one', 'four') ORDER BY +rowid + } {1 4} + + set plan(use) { + QUERY PLAN + |--SCAN t1 VIRTUAL TABLE INDEX 0:SELECT * FROM t1x WHERE a='%1%' + `--USE TEMP B-TREE FOR ORDER BY + } + set plan(omit) { + QUERY PLAN + |--SCAN t1 VIRTUAL TABLE INDEX 0:SELECT * FROM t1x WHERE a='%1%' + `--USE TEMP B-TREE FOR ORDER BY + } + set plan(use2) { + QUERY PLAN + |--SCAN t1 VIRTUAL TABLE INDEX 0:SELECT * FROM t1x + `--USE TEMP B-TREE FOR ORDER BY + } + + do_eqp_test 2.2.$mode.6 { + SELECT rowid FROM t1 WHERE a IN ('one', 'four') ORDER BY +rowid + } [string map {"\n " "\n"} $plan($mode)] +} + +# 2016-04-09. +# Demonstrate a register overwrite problem when using two virtual +# tables where the outer loop uses the IN operator. +# +set G(collist) [list PrimaryKey flagA columnA] +set G(cols) [join $G(collist) ,] +set G(nulls) "NULL" + +proc vtab_command {method args} { + global G + + switch -- $method { + xConnect { + return "CREATE TABLE t1($G(cols))" + } + + xBestIndex { + set hdl [lindex $args 0] + set clist [$hdl constraints] + set orderby [$hdl orderby] + + #puts $clist + set W [list] + set U [list] + + set i 0 + for {set idx 0} {$idx < [llength $clist]} {incr idx} { + array set c [lindex $clist $idx] + if {$c(op)=="eq" && $c(usable)} { + lappend W "[lindex $G(collist) $c(column)] = %$i%" + lappend U use $idx + incr i + } + } + + if {$W==""} { + set sql "SELECT rowid, * FROM t1" + } else { + set sql "SELECT rowid, * FROM t1 WHERE [join $W { AND }]" + } + + return [concat [list idxstr $sql] $U] + } + + xFilter { + foreach {idxnum idxstr vals} $args {} + + set map [list] + for {set i 0} {$i < [llength $vals]} {incr i} { + lappend map "%$i%" + set v [lindex $vals $i] + if {[string is integer $v]} { + lappend map $v + } else { + lappend map "'$v'" + } + } + set sql [string map $map $idxstr] + + #puts "SQL: $sql" + return [list sql $sql] + } + } + + return {} +} + +db close +forcedelete test.db +sqlite3 db test.db +register_tcl_module db + +do_execsql_test 3.1 " + CREATE TABLE t1($G(cols)); + INSERT INTO t1 VALUES(1, 0, 'ValueA'); + INSERT INTO t1 VALUES(2, 0, 'ValueA'); + INSERT INTO t1 VALUES(3, 0, 'ValueB'); + INSERT INTO t1 VALUES(4, 0, 'ValueB'); +" + +do_execsql_test 3.2 { + CREATE VIRTUAL TABLE VirtualTableA USING tcl(vtab_command); + CREATE VIRTUAL TABLE VirtualTableB USING tcl(vtab_command); +} + +do_execsql_test 3.3 { SELECT primarykey FROM VirtualTableA } {1 2 3 4} + +do_execsql_test 3.4 { + SELECT * FROM + VirtualTableA a CROSS JOIN VirtualTableB b ON b.PrimaryKey=a.PrimaryKey + WHERE a.ColumnA IN ('ValueA', 'ValueB') AND a.FlagA=0 +} { + 1 0 ValueA 1 0 ValueA + 2 0 ValueA 2 0 ValueA + 3 0 ValueB 3 0 ValueB + 4 0 ValueB 4 0 ValueB +} + +do_execsql_test 3.5 { + SELECT * FROM + VirtualTableA a CROSS JOIN VirtualTableB b ON b.PrimaryKey=a.PrimaryKey + WHERE a.FlagA=0 AND a.ColumnA IN ('ValueA', 'ValueB') +} { + 1 0 ValueA 1 0 ValueA + 2 0 ValueA 2 0 ValueA + 3 0 ValueB 3 0 ValueB + 4 0 ValueB 4 0 ValueB +} + +#------------------------------------------------------------------------- +# If there is an IN(..) condition in the WHERE clause of a query on a +# virtual table, the xBestIndex method is first invoked with the IN(...) +# represented by a "usable" SQLITE_INDEX_CONSTRAINT_EQ constraint. If +# the virtual table elects to use the IN(...) constraint, then the +# xBestIndex method is invoked again, this time with the IN(...) marked +# as "not usable". Depending on the relative costs of the two plans as +# defined by the virtual table implementation, and the cardinality of the +# IN(...) operator, SQLite chooses the most efficient plan. +# +# At one point the second invocation of xBestIndex() was only being made +# for join queries. The following tests check that this problem has been +# fixed. +# +proc vtab_command {method args} { + switch -- $method { + xConnect { + return "CREATE TABLE t1(a, b, c, d)" + } + + xBestIndex { + set hdl [lindex $args 0] + set clist [$hdl constraints] + set orderby [$hdl orderby] + + lappend ::bestindex_calls $clist + set ret "cost 1000000 idxnum 555" + for {set i 0} {$i < [llength $clist]} {incr i} { + array set C [lindex $clist $i] + if {$C(usable)} { + lappend ret use $i + } + } + return $ret + } + } + return {} +} + +do_execsql_test 4.0 { + CREATE VIRTUAL TABLE x1 USING tcl(vtab_command); +} {} + +do_test 4.1 { + set ::bestindex_calls [list] + execsql { + SELECT * FROM x1 WHERE a=? AND b BETWEEN ? AND ? AND c IN (1, 2, 3, 4); + } + set ::bestindex_calls +} [list \ + [list {op eq column 0 usable 1} \ + {op eq column 2 usable 1} \ + {op ge column 1 usable 1} \ + {op le column 1 usable 1} \ + ] \ + [list {op eq column 0 usable 1} \ + {op eq column 2 usable 0} \ + {op ge column 1 usable 1} \ + {op le column 1 usable 1} + ] +] + +do_catchsql_test 5.0 { + SELECT * FROM tcl('abc'); +} {1 {wrong number of arguments}} + +finish_test diff --git a/test/bestindex2.test b/test/bestindex2.test new file mode 100644 index 0000000000..c47a721cf6 --- /dev/null +++ b/test/bestindex2.test @@ -0,0 +1,145 @@ +# 2016 March 3 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix bestindex2 + +ifcapable !vtab { + finish_test + return +} + +#------------------------------------------------------------------------- +# Virtual table callback for table named $tbl, with the columns specified +# by list argument $cols. e.g. if the function is invoked as: +# +# vtab_cmd t1 {a b c} ... +# +# The table created is: +# +# "CREATE TABLE t1 (a, b, c)" +# +# The tables xBestIndex method behaves as if all possible combinations of +# "=" constraints (but no others) may be optimized. The cost of a full table +# scan is: +# +# "WHERE 1" "cost 1000000 rows 1000000" +# +# If one or more "=" constraints are in use, the cost and estimated number +# of rows returned are both is (11 - nCons)*1000, where nCons is the number +# of constraints used. e.g. +# +# "WHERE a=? AND b=?" -> "cost 900 rows 900" +# "WHERE c=? AND b<?" -> "cost 1000 rows 1000" +# +proc vtab_cmd {tbl cols method args} { + switch -- $method { + xConnect { + return "CREATE TABLE $tbl ([join $cols ,])" + } + xBestIndex { + set hdl [lindex $args 0] + set clist [$hdl constraints] + set orderby [$hdl orderby] + set mask [$hdl mask] + + set cons [list] + set used [list] + + for {set i 0} {$i < [llength $clist]} {incr i} { + array unset C + array set C [lindex $clist $i] + if {$C(op)=="eq" && $C(usable) && [lsearch $cons $C(column)]<0} { + lappend used use $i + lappend cons $C(column) + } + } + + set nCons [llength $cons] + if {$nCons==0} { + return "cost 1000000 rows 1000000" + } else { + set cost [expr (11-$nCons) * 1000] + set ret [concat $used "cost $cost rows $cost"] + + set txt [list] + foreach c $cons { lappend txt "[lindex $cols $c]=?" } + lappend ret idxstr "indexed([join $txt { AND }])" + + return $ret + } + } + } + return "" +} + +register_tcl_module db + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE t1 USING tcl("vtab_cmd t1 {a b}"); + CREATE VIRTUAL TABLE t2 USING tcl("vtab_cmd t2 {c d}"); + CREATE VIRTUAL TABLE t3 USING tcl("vtab_cmd t3 {e f}"); +} + +do_eqp_test 1.1 { + SELECT * FROM t1 WHERE a='abc' +} {SCAN t1 VIRTUAL TABLE INDEX 0:indexed(a=?)} + +do_eqp_test 1.2 { + SELECT * FROM t1 WHERE a='abc' AND b='def' +} {SCAN t1 VIRTUAL TABLE INDEX 0:indexed(a=? AND b=?)} + +do_eqp_test 1.3 { + SELECT * FROM t1 WHERE a='abc' AND a='def' +} {SCAN t1 VIRTUAL TABLE INDEX 0:indexed(a=?)} + +do_eqp_test 1.4 { + SELECT * FROM t1,t2 WHERE c=a +} { + QUERY PLAN + |--SCAN t1 VIRTUAL TABLE INDEX 0: + `--SCAN t2 VIRTUAL TABLE INDEX 0:indexed(c=?) +} + +do_eqp_test 1.5 { + SELECT * FROM t1, t2 CROSS JOIN t3 WHERE t2.c = +t1.b AND t3.e=t2.d +} { + QUERY PLAN + |--SCAN t1 VIRTUAL TABLE INDEX 0: + |--SCAN t2 VIRTUAL TABLE INDEX 0:indexed(c=?) + `--SCAN t3 VIRTUAL TABLE INDEX 0:indexed(e=?) +} + +do_eqp_test 1.6 { + SELECT * FROM t1, t2, t3 WHERE t2.c = +t1.b AND t3.e = t2.d +} { + QUERY PLAN + |--SCAN t1 VIRTUAL TABLE INDEX 0: + |--SCAN t2 VIRTUAL TABLE INDEX 0:indexed(c=?) + `--SCAN t3 VIRTUAL TABLE INDEX 0:indexed(e=?) +} + +do_execsql_test 1.7.1 { + CREATE TABLE x1(a, b); +} +do_eqp_test 1.7.2 { + SELECT * FROM x1 CROSS JOIN t1, t2, t3 + WHERE t1.a = t2.c AND t1.b = t3.e +} { + QUERY PLAN + |--SCAN x1 + |--SCAN t1 VIRTUAL TABLE INDEX 0: + |--SCAN t2 VIRTUAL TABLE INDEX 0:indexed(c=?) + `--SCAN t3 VIRTUAL TABLE INDEX 0:indexed(e=?) +} + +finish_test diff --git a/test/bestindex3.test b/test/bestindex3.test new file mode 100644 index 0000000000..6aa3321c7b --- /dev/null +++ b/test/bestindex3.test @@ -0,0 +1,185 @@ +# 2016 May 29 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix bestindex3 + +ifcapable !vtab { + finish_test + return +} + +#------------------------------------------------------------------------- +# Virtual table callback for a virtual table named $tbl. +# +# The table created is: +# +# "CREATE TABLE t1 (a, b, c)" +# +# This virtual table supports both LIKE and = operators on all columns. +# +proc vtab_cmd {bOmit method args} { + switch -- $method { + xConnect { + return "CREATE TABLE t1(a, b, c)" + } + + xBestIndex { + set hdl [lindex $args 0] + set clist [$hdl constraints] + set orderby [$hdl orderby] + set mask [$hdl mask] + + set ret [list] + set use use + if {$bOmit} {set use omit} + + for {set i 0} {$i < [llength $clist]} {incr i} { + array unset C + array set C [lindex $clist $i] + if {$C(usable) && ($C(op)=="like" || $C(op)=="eq")} { + lappend ret $use $i + lappend ret idxstr + lappend ret "[lindex {a b c} $C(column)] [string toupper $C(op)] ?" + break + } + } + + if {$ret==""} { + lappend ret cost 1000000 rows 1000000 + } else { + lappend ret cost 100 rows 10 + } + return $ret + } + + xFilter { + foreach {idxnum idxstr param} $args {} + set where "" + if {$bOmit && $idxstr != ""} { + set where " WHERE [string map [list ? '$param' EQ =] $idxstr]" + } + return [list sql "SELECT rowid, * FROM ttt$where"] + } + } + return "" +} + +register_tcl_module db + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE t1 USING tcl("vtab_cmd 0"); +} + +do_eqp_test 1.1 { + SELECT * FROM t1 WHERE a LIKE 'abc'; +} {SCAN t1 VIRTUAL TABLE INDEX 0:a LIKE ?} + +do_eqp_test 1.2 { + SELECT * FROM t1 WHERE a = 'abc'; +} {SCAN t1 VIRTUAL TABLE INDEX 0:a EQ ?} + +do_eqp_test 1.3 { + SELECT * FROM t1 WHERE a = 'abc' OR b = 'def'; +} { + QUERY PLAN + `--MULTI-INDEX OR + |--INDEX 1 + | `--SCAN t1 VIRTUAL TABLE INDEX 0:a EQ ? + `--INDEX 2 + `--SCAN t1 VIRTUAL TABLE INDEX 0:b EQ ? +} + +do_eqp_test 1.4 { + SELECT * FROM t1 WHERE a LIKE 'abc%' OR b = 'def'; +} { + QUERY PLAN + `--MULTI-INDEX OR + |--INDEX 1 + | `--SCAN t1 VIRTUAL TABLE INDEX 0:a LIKE ? + `--INDEX 2 + `--SCAN t1 VIRTUAL TABLE INDEX 0:b EQ ? +} + +do_execsql_test 1.5 { + CREATE TABLE ttt(a, b, c); + + INSERT INTO ttt VALUES(1, 'two', 'three'); + INSERT INTO ttt VALUES(2, 'one', 'two'); + INSERT INTO ttt VALUES(3, 'three', 'one'); + INSERT INTO ttt VALUES(4, 'y', 'one'); + INSERT INTO ttt VALUES(5, 'x', 'two'); + INSERT INTO ttt VALUES(6, 'y', 'three'); +} + +foreach omit {0 1} { + do_execsql_test 1.6.$omit.0 " + DROP TABLE t1; + CREATE VIRTUAL TABLE t1 USING tcl('vtab_cmd $omit'); + " + do_execsql_test 1.6.$omit.1 { + SELECT rowid FROM t1 WHERE c LIKE 'o%' + } {3 4} + + do_execsql_test 1.6.$omit.2 { + SELECT rowid FROM t1 WHERE c LIKE 'o%' OR b='y' + } {3 4 6} + + do_execsql_test 1.6.$omit.3 { + SELECT rowid FROM t1 WHERE c = 'three' OR c LIKE 'o%' + } {1 6 3 4} +} + +#------------------------------------------------------------------------- +# Test the same pattern works with ordinary tables. +# +# This test does not work if the ICU extension is enabled. ICU overrides +# LIKE - and this optimization only works with the built-in LIKE function. +# +ifcapable !icu { + do_execsql_test 2.1 { + CREATE TABLE t2(x TEXT COLLATE nocase, y TEXT); + CREATE INDEX t2x ON t2(x COLLATE nocase); + CREATE INDEX t2y ON t2(y); + } + + do_eqp_test 2.2 { + SELECT * FROM t2 WHERE x LIKE 'abc%' OR y = 'def' + } [string map {"\n " \n} { + QUERY PLAN + `--MULTI-INDEX OR + |--INDEX 1 + | `--SEARCH t2 USING INDEX t2x (x>? AND x<?) + `--INDEX 2 + `--SEARCH t2 USING INDEX t2y (y=?) + }] +} + +#------------------------------------------------------------------------- +# Test that any PRIMARY KEY within a sqlite3_decl_vtab() CREATE TABLE +# statement is currently ignored. +# +proc vvv_command {method args} { + switch -- $method { + xConnect { return "CREATE TABLE t1(a PRIMARY KEY, b, c)" } + } +} +proc yyy_command {method args} { + switch -- $method { + xConnect { return "CREATE TABLE t1(a, b, c, PRIMARY KEY(a, b))" } + } +} + +do_execsql_test 3.1 { CREATE VIRTUAL TABLE t3 USING tcl('vvv_command') } +do_execsql_test 3.2 { CREATE VIRTUAL TABLE t4 USING tcl('yyy_command') } + +finish_test diff --git a/test/bestindex4.test b/test/bestindex4.test new file mode 100644 index 0000000000..483656d3fc --- /dev/null +++ b/test/bestindex4.test @@ -0,0 +1,182 @@ +# 2016 November 11 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# Test the virtual table interface. In particular the xBestIndex +# method. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix bestindex4 + +ifcapable !vtab { + finish_test + return +} + +#------------------------------------------------------------------------- +# Virtual table callback for a virtual table named $tbl. +# +# The table created is: +# +# "CREATE TABLE t1 (id, host, class)" +# +# The virtual table supports == operators on a subset of its columns. The +# exact subset depends on the value of bitmask paramater $param. +# +# 0x01 - == on "id" supported +# 0x02 - == on "host" supported +# 0x04 - == on "class" supported +# +# $param also supports the following bits: +# +# 0x08 - ignore the "usable" flag (malfunction) +# +# +# +proc vtab_cmd {param method args} { + switch -- $method { + xConnect { + return "CREATE TABLE t1(id TEXT, host TEXT, class TEXT)" + } + + xBestIndex { + set hdl [lindex $args 0] + set clist [$hdl constraints] + set orderby [$hdl orderby] + set mask [$hdl mask] + + set ret [list] + + set use use + + + for {set i 0} {$i < [llength $clist]} {incr i} { + array unset C + array set C [lindex $clist $i] + if { ($C(usable) || ($param & 0x08)) + && $C(op)=="eq" && ($param & 1<<$C(column)) + } { + lappend ret $use $i + break + } + } + + set score 1000000 + if {$ret!=""} { + set score [expr $score / [llength $ret]] + } + lappend ret cost $score rows $score + + return $ret + } + + xFilter { + } + } + return "" +} + +register_tcl_module db + +for {set param1 0} {$param1<16} {incr param1} { + for {set param2 0} {$param2<16} {incr param2} { + reset_db + register_tcl_module db + do_execsql_test 1.$param1.$param2.1 " + CREATE VIRTUAL TABLE t1 USING tcl('vtab_cmd $param1'); + CREATE VIRTUAL TABLE t2 USING tcl('vtab_cmd $param2'); + " + + foreach {tn sql} { + 2 "select t1.id as ID from t1, t2 where t1.id=t2.host and t2.class='xx'" + 3 { + select t1.id as ID from t1, t2 where t2.class ='xx' and t2.id = t1.host + } + 4 { + select t1.id as ID from t1, t2 where t1.host = t2.id and t2. class ='xx' + } + } { + + if {($param1 & 0x08)==0 && ($param2 & 0x08)==0} { + + do_execsql_test 1.$param1.$param2.$tn.a $sql {} + + } else { + do_test 1.$param1.$param2.$tn.b { + catchsql $sql + set {} {} + } {} + } + } + + } +} + +#------------------------------------------------------------------------- +# Test that a parameter passed to a table-valued function cannot be +# used to drive an index. i.e. that in the following: +# +# SELECT * FROM tbl, vtab(tbl.x); +# +# The implicit constraint "tbl.x = vtab.hidden" is not optimized using +# an index on tbl.x. +# +reset_db +register_tcl_module db +proc vtab_command {method args} { + switch -- $method { + xConnect { + return "CREATE TABLE t1(a, b, c, d HIDDEN)" + } + + xBestIndex { + set hdl [lindex $args 0] + set clist [$hdl constraints] + set orderby [$hdl orderby] + set mask [$hdl mask] + + if {[llength $clist]!=1} { error "unexpected constraint list" } + catch { array unset C } + array set C [lindex $clist 0] + if {$C(usable)} { + return [list omit 0 idxnum 555 rows 10 cost 100] + } + return [list cost 100000000] + } + + } + + return {} +} + +do_execsql_test 2.0 { + CREATE VIRTUAL TABLE x1 USING tcl(vtab_command); + CREATE TABLE t1 (x INT PRIMARY KEY); +} {} + +do_eqp_test 2.1 { + SELECT * FROM t1, x1 WHERE x1.d=t1.x; +} { + QUERY PLAN + |--SCAN x1 VIRTUAL TABLE INDEX 0: + `--SEARCH t1 USING COVERING INDEX sqlite_autoindex_t1_1 (x=?) +} + +do_eqp_test 2.2 { + SELECT * FROM t1, x1(t1.x) +} { + QUERY PLAN + |--SCAN t1 + `--SCAN x1 VIRTUAL TABLE INDEX 555: +} + + +finish_test diff --git a/test/bestindex5.test b/test/bestindex5.test new file mode 100644 index 0000000000..35df58a98e --- /dev/null +++ b/test/bestindex5.test @@ -0,0 +1,252 @@ +# 2017 September 10 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# Test the virtual table interface. In particular the xBestIndex +# method. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix bestindex5 + +ifcapable !vtab { + finish_test + return +} + +#------------------------------------------------------------------------- +# Virtual table callback for a virtual table named $tbl. +# +proc vtab_cmd {method args} { + + set binops(ne) != + set binops(eq) = + set binops(isnot) "IS NOT" + set binops(is) "IS" + + set unops(isnotnull) "IS NOT NULL" + set unops(isnull) "IS NULL" + + set cols(0) a + set cols(1) b + set cols(2) c + + switch -- $method { + xConnect { + return "CREATE TABLE t1(a, b, c)" + } + + xBestIndex { + set hdl [lindex $args 0] + set clist [$hdl constraints] + set orderby [$hdl orderby] + set mask [$hdl mask] + + set cost 1000000.0 + set ret [list] + set str [list] + + set v 0 + for {set i 0} {$i < [llength $clist]} {incr i} { + array unset C + array set C [lindex $clist $i] + if {$C(usable)} { + if {[info exists binops($C(op))]} { + lappend ret omit $i + lappend str "$cols($C(column)) $binops($C(op)) %$v%" + incr v + set cost [expr $cost / 2] + } + if {[info exists unops($C(op))]} { + lappend ret omit $i + lappend str "$cols($C(column)) $unops($C(op))" + incr v + set cost [expr $cost / 2] + } + } + } + + lappend ret idxstr [join $str " AND "] + lappend ret cost $cost + return $ret + } + + xFilter { + set q [lindex $args 1] + set a [lindex $args 2] + for {set v 0} {$v < [llength $a]} {incr v} { + set val [lindex $a $v] + set q [string map [list %$v% '$val'] $q] + } + if {$q==""} { set q 1 } + lappend ::xFilterQueries "WHERE $q" + return [list sql "SELECT rowid, * FROM t1x WHERE $q"] + } + } + return "" +} + +proc vtab_simple {method args} { + switch -- $method { + xConnect { + return "CREATE TABLE t2(x)" + } + xBestIndex { + return [list cost 999999.0] + } + xFilter { + return [list sql "SELECT rowid, * FROM t2x"] + } + } + return "" +} + +register_tcl_module db + +proc do_vtab_query_test {tn query result} { + set ::xFilterQueries [list] + uplevel [list + do_test $tn [string map [list %QUERY% $query] { + set r [execsql {%QUERY%}] + set r [concat $::xFilterQueries $r] + set r + }] [list {*}$result] + ] +} + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE t1 USING tcl('vtab_cmd'); + CREATE TABLE t1x(a INTEGER, b TEXT, c REAL); + INSERT INTO t1x VALUES(1, 2, 3); + INSERT INTO t1x VALUES(4, 5, 6); + INSERT INTO t1x VALUES(7, 8, 9); + + CREATE VIRTUAL TABLE t2 USING tcl('vtab_simple'); + CREATE TABLE t2x(x INTEGER); + INSERT INTO t2x VALUES(1); +} + +do_vtab_query_test 1.1 { SELECT * FROM t1 WHERE a!='hello'; } { + "WHERE a != 'hello'" + 1 2 3.0 4 5 6.0 7 8 9.0 +} + +do_vtab_query_test 1.2.1 { SELECT * FROM t1 WHERE b!=8 } { + "WHERE b != '8'" + 1 2 3.0 4 5 6.0 +} +do_vtab_query_test 1.2.2 { SELECT * FROM t1 WHERE 8!=b } { + "WHERE b != '8'" + 1 2 3.0 4 5 6.0 +} + +do_vtab_query_test 1.3 { SELECT * FROM t1 WHERE c IS NOT 3 } { + "WHERE c IS NOT '3'" + 4 5 6.0 7 8 9.0 +} +do_vtab_query_test 1.3.2 { SELECT * FROM t1 WHERE 3 IS NOT c } { + "WHERE c IS NOT '3'" + 4 5 6.0 7 8 9.0 +} + +do_vtab_query_test 1.4.1 { SELECT * FROM t1, t2 WHERE x != a } { + "WHERE a != '1'" + 4 5 6.0 1 7 8 9.0 1 +} +do_vtab_query_test 1.4.2 { SELECT * FROM t1, t2 WHERE a != x } { + "WHERE a != '1'" + 4 5 6.0 1 7 8 9.0 1 +} + +do_vtab_query_test 1.5.1 { SELECT * FROM t1 WHERE a IS NOT NULL } { + "WHERE a IS NOT NULL" + 1 2 3.0 4 5 6.0 7 8 9.0 +} +do_vtab_query_test 1.5.2 { SELECT * FROM t1 WHERE NULL IS NOT a } { + "WHERE a IS NOT ''" + 1 2 3.0 4 5 6.0 7 8 9.0 +} + +do_vtab_query_test 1.6.1 { SELECT * FROM t1 WHERE a IS NULL } { + "WHERE a IS NULL" +} + +do_vtab_query_test 1.6.2 { SELECT * FROM t1 WHERE NULL IS a } { + "WHERE a IS ''" +} + +do_vtab_query_test 1.7.1 { SELECT * FROM t1 WHERE (a, b) IS (1, 2) } { + "WHERE a IS '1' AND b IS '2'" + 1 2 3.0 +} +do_vtab_query_test 1.7.2 { SELECT * FROM t1 WHERE (5, 4) IS (b, a) } { + {WHERE b IS '5' AND a IS '4'} + 4 5 6.0 +} + +#--------------------------------------------------------------------- +do_execsql_test 2.0.0 { + DELETE FROM t1x; + INSERT INTO t1x VALUES('a', 'b', 'c'); +} +do_execsql_test 2.0.1 { SELECT * FROM t1 } {a b c} +do_execsql_test 2.0.2 { SELECT * FROM t1 WHERE (a, b) != ('a', 'b'); } {} + +do_execsql_test 2.1.0 { + DELETE FROM t1x; + INSERT INTO t1x VALUES(7, 8, 9); +} +do_execsql_test 2.1.1 { SELECT * FROM t1 } {7 8 9.0} +do_execsql_test 2.1.2 { SELECT * FROM t1 WHERE (a, b) != (7, '8') } {} +do_execsql_test 2.1.3 { SELECT * FROM t1 WHERE a!=7 OR b!='8' } +do_execsql_test 2.1.4 { SELECT * FROM t1 WHERE a!=7 OR b!='8' } + + +do_execsql_test 2.2.1 { + CREATE TABLE t3(a INTEGER, b TEXT); + INSERT INTO t3 VALUES(45, 46); +} +do_execsql_test 2.2.2 { SELECT * FROM t3 WHERE (a, b) != (45, 46); } +do_execsql_test 2.2.3 { SELECT * FROM t3 WHERE (a, b) != ('45', '46'); } +do_execsql_test 2.2.4 { SELECT * FROM t3 WHERE (a, b) == (45, 46); } {45 46} +do_execsql_test 2.2.5 { SELECT * FROM t3 WHERE (a, b) == ('45', '46'); } {45 46} + +#--------------------------------------------------------------------- +# Test the != operator on a virtual table with column affinities. +# +proc vtab_simple_integer {method args} { + switch -- $method { + xConnect { + return "CREATE TABLE t4(x INTEGER)" + } + xBestIndex { + return [list cost 999999.0] + } + xFilter { + return [list sql "SELECT rowid, * FROM t4x"] + } + } + return "" +} + +do_execsql_test 3.0 { + CREATE TABLE t4x(a INTEGER); + INSERT INTO t4x VALUES(245); + CREATE VIRTUAL TABLE t4 USING tcl('vtab_simple_integer'); +} +do_execsql_test 3.1 { SELECT rowid, * FROM t4 WHERE x=245; } {1 245} +do_execsql_test 3.2 { SELECT rowid, * FROM t4 WHERE x='245'; } {1 245} +do_execsql_test 3.3 { SELECT rowid, * FROM t4 WHERE x!=245; } {} +do_execsql_test 3.4 { SELECT rowid, * FROM t4 WHERE x!='245'; } {} + +do_execsql_test 3.5 { SELECT rowid, * FROM t4 WHERE rowid!=1 OR x!='245'; } {} + + +finish_test diff --git a/test/bestindex6.test b/test/bestindex6.test new file mode 100644 index 0000000000..8926ec4a11 --- /dev/null +++ b/test/bestindex6.test @@ -0,0 +1,113 @@ +# 2018-09-09 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix bestindex6 + +ifcapable !vtab { + finish_test + return +} + +register_tcl_module db + +proc vtab_command {src method args} { + switch -- $method { + xConnect { + return [db one {SELECT sql FROM sqlite_master where name = $src}] + } + + xBestIndex { + set hdl [lindex $args 0] + set clist [$hdl constraints] + set orderby [$hdl orderby] + set mask [$hdl mask] + + set wlist 1 + + set iCons 0 + set ret [list] + foreach cons $clist { + catch { array unset C } + array set C $cons + + if {$C(usable)} { + set col [db one { + SELECT name FROM pragma_table_info($src) WHERE cid=$C(column) + }] + switch $C(op) { + isnull { + lappend wlist "$col IS NULL" + lappend ret omit $iCons + } + eq { + lappend wlist "$col = %$iCons%" + lappend ret omit $iCons + } + } + } + incr iCons + } + #puts "xBestIndex: $ret" + lappend ret idxStr [join $wlist " AND "] + return $ret + } + + xFilter { + foreach {idxnum idxstr aa} $args {} + set map [list] + for {set iCons 0} {$iCons < [llength $aa]} {incr iCons} { + lappend map %$iCons% [lindex $aa $iCons] + } + set ret [list sql \ + "SELECT rowid, * FROM $src WHERE [string map $map $idxstr]" + ] + # puts "xFilter: $ret" + return $ret + } + + } + + return {} +} + +do_execsql_test 1.0 { + CREATE TABLE t1(id int, value text); + CREATE TABLE t2(ctx int, id int, value text); + + INSERT INTO t1 VALUES(1,'try'); + INSERT INTO t2 VALUES(1,1,'good'); + INSERT INTO t2 VALUES(2,2,'evil'); + + CREATE VIRTUAL TABLE vt1 USING tcl(vtab_command t1); + CREATE VIRTUAL TABLE vt2 USING tcl(vtab_command t2); +} + +do_execsql_test 1.1 { + select * from t2 left join t1 on t1.id=t2.ctx where t1.value is null; +} {2 2 evil {} {}} + +do_execsql_test 1.2 { + select * from vt2 left join vt1 on vt1.id=vt2.ctx where vt1.value is null; +} {2 2 evil {} {}} + +unset -nocomplain xxx +do_execsql_test 1.3 { + select * from vt2 left join vt1 on vt1.id=vt2.ctx where vt1.value is $xxx; +} {2 2 evil {} {}} + +do_execsql_test 1.4 { + select * from t2 left join vt1 on vt1.id=t2.ctx where vt1.value = 3 +} {} + +finish_test diff --git a/test/bestindex7.test b/test/bestindex7.test new file mode 100644 index 0000000000..a84b9efe9e --- /dev/null +++ b/test/bestindex7.test @@ -0,0 +1,82 @@ +# 2020-01-29 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix bestindex7 + +ifcapable !vtab { + finish_test + return +} + +register_tcl_module db + +proc vtab_command {src method args} { + switch -- $method { + xConnect { + return "CREATE TABLE xxx(a)" + } + + xBestIndex { + set hdl [lindex $args 0] + set clist [$hdl constraints] + set orderby [$hdl orderby] + set mask [$hdl mask] + + set iCons 0 + set ret [list] + foreach cons $clist { + catch { array unset C } + array set C $cons + if {$C(usable)} { + lappend ret use $iCons + } + incr iCons + } + return $ret + } + + xFilter { + return [list sql "SELECT rowid, x FROM $src"] + } + + } + + return {} +} + +do_execsql_test 1.0 { + CREATE TABLE t1(x); + INSERT INTO t1 VALUES(0), (2); + CREATE VIRTUAL TABLE vt1 USING tcl(vtab_command t1); +} + +do_execsql_test 1.1 { select * from vt1 } {0 2} +do_execsql_test 1.2 { select * from vt1 WHERE a=0 } {0} +do_execsql_test 1.3 { select * from vt1 WHERE a=1 } {} +do_execsql_test 1.4 { select * from vt1 WHERE a=1 OR a=0} {0} + +do_execsql_test 1.5 { + UPDATE t1 SET x=NULL WHERE x=2; +} + +do_execsql_test 1.6 { select * from vt1 } {0 {}} +do_execsql_test 1.7 { select * from vt1 WHERE a=0 } {0} +do_execsql_test 1.8 { select * from vt1 WHERE a=1 } {} +do_execsql_test 1.9 { select * from vt1 WHERE a=1 OR a=0} {0} +do_execsql_test 1.10 { select * from vt1 WHERE a IN (2) } {} +do_execsql_test 1.10 { select * from vt1 WHERE a IN (0,1,2,3) } {0} +do_execsql_test 1.11 { select * from vt1 WHERE a IN (0, NULL) } {0} +do_execsql_test 1.12 { select * from vt1 WHERE a IN (NULL) } {} + +finish_test diff --git a/test/bestindex8.test b/test/bestindex8.test new file mode 100644 index 0000000000..3ed7f6703e --- /dev/null +++ b/test/bestindex8.test @@ -0,0 +1,463 @@ +# 2020-01-29 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix bestindex8 + +ifcapable !vtab { + finish_test + return +} + +register_tcl_module db + +proc vtab_command {src method args} { + switch -- $method { + xConnect { + return "CREATE TABLE xxx(a, b)" + } + + xBestIndex { + set hdl [lindex $args 0] + set clist [$hdl constraints] + set orderby [$hdl orderby] + lappend ::lBestIndexDistinct [$hdl distinct] + + #puts "ORDERBY: $orderby" + set iCons 0 + set ret [list] + foreach cons $clist { + catch { array unset C } + array set C $cons + if {$C(usable)} { + lappend ret use $iCons + } + incr iCons + } + if {$orderby=="{column 0 desc 0} {column 1 desc 0}" + || $orderby=="{column 0 desc 0}" + } { + lappend ret orderby 1 + lappend ret idxnum 1 + set ::lOrderByConsumed 1 + } + return $ret + } + + xFilter { + set idxnum [lindex $args 0] + if {$idxnum} { + return [list sql "SELECT rowid, a, b FROM $src order by 2, 3"] + } + return [list sql "SELECT rowid, a, b FROM $src"] + } + + } + + return {} +} + +do_execsql_test 1.0 { + CREATE TABLE t1(a, b); + CREATE INDEX i1 ON t1(a, b); + INSERT INTO t1 VALUES('a', 'b'), ('c', 'd'); + INSERT INTO t1 VALUES('a', 'b'), ('c', 'd'); + CREATE VIRTUAL TABLE vt1 USING tcl(vtab_command t1); + + CREATE TABLE t0(c0); + INSERT INTO t0(c0) VALUES (1), (0); +} + +foreach {tn sql bDistinct idxinsert bConsumed res} { + 1 "SELECT a, b FROM vt1" 0 0 0 {a b c d a b c d} + 2 "SELECT DISTINCT a, b FROM vt1" 2 1 1 {a b c d} + 3 "SELECT DISTINCT a FROM vt1" 2 1 1 {a c} + 4 "SELECT DISTINCT b FROM vt1" 2 1 0 {b d} + 5 "SELECT DISTINCT b FROM vt1 ORDER BY a" 0 1 1 {b d} + 6 "SELECT DISTINCT t0.c0 FROM vt1, t0 ORDER BY vt1.a" 0 1 1 {1 0} + 7 "SELECT DISTINCT a, b FROM vt1 ORDER BY a, b" 3 0 1 {a b c d} + 8 "SELECT DISTINCT a, b FROM vt1 ORDER BY a" 0 1 1 {a b c d} + 9 "SELECT DISTINCT a FROM vt1 ORDER BY a, b" 0 1 1 {a c} + + 10 "SELECT DISTINCT a, b FROM vt1 WHERE b='b'" 2 1 1 {a b} + 11 "SELECT DISTINCT a, b FROM vt1 WHERE +b='b'" 2 1 1 {a b} +} { + set ::lBestIndexDistinct "" + set ::lOrderByConsumed 0 + do_execsql_test 1.$tn.1 $sql $res + do_test 1.$tn.2 { + set ::lBestIndexDistinct + } $bDistinct + do_test 1.$tn.3 { + expr {[lsearch [execsql "explain $sql"] IdxInsert]>=0} + } $idxinsert + do_test 1.$tn.4 { + set ::lOrderByConsumed + } $bConsumed +} + +#------------------------------------------------------------------------- +reset_db +register_tcl_module db + +proc vtab_command {src method args} { + switch -- $method { + xConnect { + return "CREATE TABLE xxx(a, b)" + } + + xBestIndex { + set hdl [lindex $args 0] + set ret [list] + + set iCons 0 + foreach cons [$hdl constraints] { + array set C $cons + if {($C(op)=="limit" || $C(op)=="offset") && $C(usable)} { + lappend ret use $iCons + } + incr iCons + } + + return $ret + } + + xFilter { + lappend ::lFilterArgs [lindex $args 2] + return [list sql "SELECT rowid, a, b FROM $src"] + } + + } + + return {} +} + +do_execsql_test 2.0 { + CREATE TABLE t1(a, b); + CREATE INDEX i1 ON t1(a, b); + CREATE VIRTUAL TABLE vt1 USING tcl(vtab_command t1); +} + +do_test 2.1 { + set ::lFilterArgs [list] + execsql { SELECT * FROM vt1 LIMIT 10 } + set ::lFilterArgs +} {10} + +do_test 2.2 { + set ::lFilterArgs [list] + execsql { SELECT * FROM vt1 LIMIT 5 OFFSET 50 } + set ::lFilterArgs +} {{50 5}} + +do_test 2.3 { + set ::lFilterArgs [list] + execsql { SELECT * FROM vt1 ORDER BY a, b LIMIT 1 OFFSET 1 } + set ::lFilterArgs +} {{1 1}} + +do_test 2.4 { + set ::lFilterArgs [list] + execsql { SELECT * FROM vt1 ORDER BY a, +b LIMIT 1 OFFSET 1 } + set ::lFilterArgs +} {{}} + +#------------------------------------------------------------------------- +reset_db +register_tcl_module db + +proc vtab_command {src method args} { + switch -- $method { + xConnect { + return "CREATE TABLE xxx(a, b)" + } + + xBestIndex { + set hdl [lindex $args 0] + set lCons [$hdl constraints] + + set ret [list] + for {set i 0} {$i < [llength $lCons]} {incr i} { + array set C [lindex $lCons $i] + if {$C(usable)} { + lappend ret use $i + $hdl in $i 1 + } + } + return $ret + } + + xFilter { + set lArg [lindex $args 2] + lappend ::lFilterArg {*}$lArg + return [list sql "SELECT rowid, a, b FROM $src"] + } + + } + + return {} +} + +do_execsql_test 3.0 { + CREATE TABLE t1(a, b); + CREATE INDEX i1 ON t1(a, b); + CREATE VIRTUAL TABLE vt1 USING tcl(vtab_command t1); +} + +foreach {tn sql lfa} { + 1 "SELECT * FROM vt1 WHERE b IN (10, 20, 30)" {{10 20 30}} + 2 "SELECT * FROM vt1 WHERE b IN ('abc', 'def')" {{abc def}} + 3 "SELECT * FROM vt1 WHERE a IS NULL AND b IN ('abc', 'def')" {{} {abc def}} + 4 "SELECT * FROM vt1 WHERE a IN (1,2,3) AND b IN ('abc', 'def')" + {{1 2 3} {abc def}} + + 5 "SELECT * FROM vt1 + WHERE a IN (SELECT 1 UNION SELECT 2) AND b IN ('abc', 'def')" + {{1 2} {abc def}} + + 6 "SELECT * FROM vt1 + WHERE b IN ('abc', 'def') AND a IN (SELECT 1 UNION SELECT 2)" + {{abc def} {1 2}} +} { + do_test 3.$tn { + set ::lFilterArg [list] + execsql $sql + set ::lFilterArg + } $lfa +} + +#explain_i { SELECT * FROM vt1 WHERE b IN (10, 20, 30) } + +#------------------------------------------------------------------------- +reset_db +register_tcl_module db + +proc vtab_command {src method args} { + switch -- $method { + xConnect { + return "CREATE TABLE xxx(a, b, c)" + } + + xBestIndex { + set hdl [lindex $args 0] + set lCons [$hdl constraints] + + set ret [list] + for {set i 0} {$i < [llength $lCons]} {incr i} { + lappend ::lBestIndexRhs [$hdl rhs_value $i -] + } + return $ret + } + + xFilter { + return [list sql "SELECT rowid, a, b, c FROM $src"] + } + + } + + return {} +} + +do_execsql_test 4.0 { + CREATE TABLE t1(a, b, c); + CREATE VIRTUAL TABLE vt1 USING tcl(vtab_command t1); +} + +foreach {tn sql lbir} { + 1 "SELECT * FROM vt1 WHERE b = 10" {10} + 2 "SELECT * FROM vt1 WHERE a = 'abc' AND b < 30" {abc 30} + 3 "SELECT * FROM vt1 WHERE a = 'abc' AND b < 30+2" {abc -} + 4 "SELECT * FROM vt1 WHERE a IN (1,2,3) AND b < 30+2" {- -} + 5 "SELECT * FROM vt1 WHERE a IS 111 AND b < 30+2" {111 -} +} { + do_test 4.$tn { + set ::lBestIndexRhs [list] + execsql $sql + set ::lBestIndexRhs + } $lbir +} + +#------------------------------------------------------------------------- +reset_db +db cache size 0 +register_tcl_module db + +set ::vtab_handle_in 1 +proc vtab_command {src method args} { + switch -- $method { + xConnect { + return "CREATE TABLE xxx(a, b, c)" + } + + xBestIndex { + set lCols [list a b c] + + set hdl [lindex $args 0] + set lCons [$hdl constraints] + set lOrder [$hdl order] + + set L "" + set O "" + set W [list] + set a 0 + for {set i 0} {$i < [llength $lCons]} {incr i} { + array set C [lindex $lCons $i] + if {$C(usable)} { + if { $C(op)=="eq" } { + set bIn 0 + if {$::vtab_handle_in} { set bIn [$hdl in $i 1] } + if {$bIn} { + lappend W "[lindex $lCols $C(column)] IN (%I$a%)" + } else { + lappend W "[lindex $lCols $C(column)] = %$a%" + } + lappend ret omit $i + } + if { $C(op)=="limit" } { set L " LIMIT %$a%" ; lappend ret use $i } + if { $C(op)=="offset" } { set O " OFFSET %$a%" ; lappend ret use $i } + incr a + } + } + + set order "" + set selectlist "rowid, a, b, c" + if {[llength $lOrder]} { + array set sl [list] + set lO [list] + foreach s $lOrder { + array set C $s + set ad "" + if {$C(desc)} { set ad " DESC" } + lappend lO "[lindex $lCols $C(column)]$ad" + set sl($C(column)) 1 + } + if {[$hdl distinct]==2} { + set selectlist "DISTINCT 0" + foreach i {0 1 2} { + if {[info exists sl($i)]} { + append selectlist ", [lindex $lCols $i]" + } else { + append selectlist ", 0" + } + } + } else { + set order " ORDER BY [join $lO ,]" + } + } + + set where "" + if {[llength $W]} { set where " WHERE [join $W { AND }]" } + set sql "SELECT $selectlist FROM $src$where$order$L$O" + + lappend ret idxStr $sql + return $ret + } + + xFilter { + foreach {idxnum idxstr lArg} $args {} + set ii 0 + set sql $idxstr + foreach a $lArg { + set sql [string map [list %$ii% $a] $sql] + set sql [string map [list %I$ii% [join $a ,]] $sql] + incr ii + } + lappend ::lFilterSql $sql + + if {[regexp {OFFSET (.*)$} $sql -> off]} { + set real_sql " + WITH c(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM c WHERE i<$off ) + SELECT 0,0,0,0 FROM c + UNION ALL SELECT * FROM ( + $sql + ) + " + } else { + set real_sql $sql + } + + return [list sql $real_sql] + } + + } + + return {} +} + +do_execsql_test 5.0 { + CREATE TABLE t1(a, b, c); + CREATE VIRTUAL TABLE vt1 USING tcl(vtab_command t1); + INSERT INTO t1 VALUES(1, 2, 3); + INSERT INTO t1 VALUES(2, 3, 4); + INSERT INTO t1 VALUES(3, 4, 5); + INSERT INTO t1 VALUES(1, 5, 6); + INSERT INTO t1 VALUES(2, 6, 7); + INSERT INTO t1 VALUES(3, 7, 8); + INSERT INTO t1 VALUES(1, 8, 9); + INSERT INTO t1 VALUES(2, 9, 0); +} + +proc do_vtab_test {tn sql vtsql {res {}}} { + set ::lFilterSql [list] + uplevel [list do_execsql_test $tn.1 $sql $res] + uplevel [list do_test $tn.2 {set ::lFilterSql} [list {*}$vtsql]] +} + +do_vtab_test 5.1.1 { + SELECT DISTINCT a FROM vt1 +} { + {SELECT DISTINCT 0, a, 0, 0 FROM t1} +} {1 2 3} + +do_vtab_test 5.1.2 { + SELECT DISTINCT a FROM vt1 ORDER BY a +} { + {SELECT rowid, a, b, c FROM t1 ORDER BY a} +} {1 2 3} + +do_vtab_test 5.1.3 { + SELECT DISTINCT a FROM vt1 WHERE c IN (4,5,6,7,8) +} { + {SELECT DISTINCT 0, a, 0, 0 FROM t1 WHERE c IN (4,5,6,7,8)} +} {2 3 1} + +set ::vtab_handle_in 0 +do_vtab_test 5.1.4 { + SELECT DISTINCT a FROM vt1 WHERE c IN (4,5,6,7,8) +} { + {SELECT DISTINCT 0, a, 0, 0 FROM t1 WHERE c = 4} + {SELECT DISTINCT 0, a, 0, 0 FROM t1 WHERE c = 5} + {SELECT DISTINCT 0, a, 0, 0 FROM t1 WHERE c = 6} + {SELECT DISTINCT 0, a, 0, 0 FROM t1 WHERE c = 7} + {SELECT DISTINCT 0, a, 0, 0 FROM t1 WHERE c = 8} +} {2 3 1} + +set ::vtab_handle_in 1 +do_vtab_test 5.1.5a { + SELECT a, b, c FROM vt1 WHERE c IN (4,5,6,7,8) LIMIT 2 OFFSET 2 +} { + {SELECT rowid, a, b, c FROM t1 WHERE c IN (4,5,6,7,8) LIMIT 2 OFFSET 2} +} {1 5 6 2 6 7} + +set ::vtab_handle_in 0 +do_vtab_test 5.1.5b { + SELECT a, b, c FROM vt1 WHERE c IN (4,5,6,7,8) LIMIT 2 OFFSET 2 +} { + {SELECT rowid, a, b, c FROM t1 WHERE c = 4} + {SELECT rowid, a, b, c FROM t1 WHERE c = 5} + {SELECT rowid, a, b, c FROM t1 WHERE c = 6} + {SELECT rowid, a, b, c FROM t1 WHERE c = 7} +} {1 5 6 2 6 7} +set ::vtab_handle_in 1 + +finish_test diff --git a/test/bestindex9.test b/test/bestindex9.test new file mode 100644 index 0000000000..d591b30efe --- /dev/null +++ b/test/bestindex9.test @@ -0,0 +1,104 @@ +# 2020-01-29 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix bestindex9 + +ifcapable !vtab { + finish_test + return +} + + +proc vtab_command {method args} { + switch -- $method { + xConnect { + return $::create_table + } + + xBestIndex { + set hdl [lindex $args 0] + set ::vtab_orderby [$hdl orderby] + set ::vtab_distinct [$hdl distinct] + + return [list orderby 1] + } + + xFilter { + return "" + } + } + + return {} +} + +proc do_bestindex9_test {tn create select orderby distinct} { + forcedelete test.db2 + sqlite3 dbtest test.db2 + register_tcl_module dbtest + + set ::create_table $create + dbtest eval { CREATE VIRTUAL TABLE t1 USING tcl(vtab_command); } + dbtest eval $select + + do_test $tn.orderby [list set {} $::vtab_orderby] $orderby + do_test $tn.distinct [list set {} $::vtab_distinct] $distinct + + dbtest close +} + +#------------------------------------------------------------------------- +# +do_bestindex9_test 1.0 { + CREATE TABLE x(k1, k2, v1, PRIMARY KEY(k1, k2)) +} { + SELECT DISTINCT k1, k2 FROM t1 +} {{column 0 desc 0} {column 1 desc 0}} 2 + +do_bestindex9_test 1.1 { + CREATE TABLE x(k1, k2, v1, PRIMARY KEY(k1, k2)) WITHOUT ROWID +} { + SELECT DISTINCT k1, k2 FROM t1 +} {} 0 + +do_bestindex9_test 1.2 { + CREATE TABLE x(k1 NOT NULL, k2 NOT NULL, v1, PRIMARY KEY(k1, k2)) +} { + SELECT DISTINCT k1, k2 FROM t1 +} {} 0 + +#------------------------------------------------------------------------- +# +do_bestindex9_test 2 { + CREATE TABLE x(c1, c2, c3); +} { + SELECT DISTINCT c1 FROM t1 ORDER BY c1 +} {{column 0 desc 0}} 3 + +#------------------------------------------------------------------------- +# +do_bestindex9_test 3 { + CREATE TABLE x(c1, c2, c3); +} { + SELECT DISTINCT c1 FROM t1 GROUP BY c1 +} {{column 0 desc 0}} 1 + +do_bestindex9_test 4 { + CREATE TABLE x(c1, c2, c3); +} { + CREATE TABLE t2(balls); + SELECT DISTINCT c1 FROM t1, t2 +} {{column 0 desc 0}} 2 + + +finish_test diff --git a/test/bestindexA.test b/test/bestindexA.test new file mode 100644 index 0000000000..1976986471 --- /dev/null +++ b/test/bestindexA.test @@ -0,0 +1,135 @@ +# 2020-01-29 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix bestindexA + +ifcapable !vtab { + finish_test + return +} + + +proc vtab_command {method args} { + switch -- $method { + xConnect { + return "CREATE TABLE x(a, b, c)" + } + + xBestIndex { + set hdl [lindex $args 0] + set clist [$hdl constraints] + foreach c $clist { + array set C $c + lappend ::vtab_constraints [list $C(op) $C(column)] + } + return [list] + } + + xFilter { + return "" + } + + xFindFunction { + foreach {nArg name} $args {} + if {$nArg==2 && $name=="even"} { + return 152 + } + return 0 + } + + } + + return {} +} + +register_tcl_module db +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE t1 USING tcl(vtab_command); +} + +proc do_xbestindex_test {tn sql res} { + set script [subst { + execsql "$sql" + set ::vtab_constraints + }] + + uplevel [list do_test $tn $script [list {*}$res]] + set ::vtab_constraints [list] +} + +do_xbestindex_test 1.1 { + SELECT * FROM t1 WHERE a=? +} { + {eq 0} +} + +do_xbestindex_test 1.2 { + SELECT * FROM t1 WHERE a=? LIMIT 10 +} { + {eq 0} + {limit 0} +} + +do_xbestindex_test 1.3 { + SELECT * FROM t1 WHERE a=? AND (b+1)=? LIMIT 10 +} { + {eq 0} +} + +proc error_function {args} { error "not a function!" } +db function even error_function + +do_xbestindex_test 1.4 { + SELECT * FROM t1 WHERE even(a, ?) +} { + {152 0} +} + +do_xbestindex_test 1.5 { + SELECT * FROM t1 WHERE b=10 AND even(a, ?) +} { + {eq 1} + {152 0} +} + +do_xbestindex_test 1.6 { + SELECT * FROM t1 WHERE b=10 LIMIT 10 +} { + {eq 1} + {limit 0} +} + +do_xbestindex_test 1.7 { + SELECT * FROM t1 WHERE even(b,?) LIMIT 10 +} { + {152 1} + {limit 0} +} + +do_xbestindex_test 1.8 { + SELECT * FROM t1 WHERE b!=? LIMIT 10 +} { + {ne 1} + {limit 0} +} + +do_xbestindex_test 1.9 { + SELECT * FROM t1 WHERE ?=a LIMIT 10 +} { + {eq 0} + {limit 0} +} + + +finish_test diff --git a/test/bestindexB.test b/test/bestindexB.test new file mode 100644 index 0000000000..b50e74fee3 --- /dev/null +++ b/test/bestindexB.test @@ -0,0 +1,87 @@ +# 2023-10-26 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix bestindexB + +ifcapable !vtab { + finish_test + return +} + +register_tcl_module db + +proc vtab_command {method args} { + switch -- $method { + xConnect { + return "CREATE TABLE t1(a, b, c)" + } + + xBestIndex { + set hdl [lindex $args 0] + set clist [$hdl constraints] + set orderby [$hdl orderby] + + if {[info exists ::xbestindex_sql]} { + explain_i $::xbestindex_sql + set ::xbestindex_res [ execsql $::xbestindex_sql ] + } + + return "cost 1000000 rows 1000000 idxnum 0 idxstr hello" + } + + xFilter { + return "sql {SELECT 0, 1, 2, 3}" + } + } + + return {} +} + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE x1 USING tcl(vtab_command); + CREATE TABLE y1(a, b); + CREATE TABLE y2(a, b); +} {} + +do_execsql_test 1.1 { + SELECT * FROM x1 +} {1 2 3} + +do_execsql_test 1.2 { + INSERT INTO y1 VALUES(1, 2) RETURNING rowid; +} {1} + +do_execsql_test 1.3 { + CREATE TRIGGER y1tr BEFORE INSERT ON y1 BEGIN + SELECT * FROM x1; + END; + INSERT INTO y1 VALUES(3, 4) RETURNING rowid; +} {2} + + +# This time, rig the xBestIndex() method of the vtab to invoke an SQL +# statement that uses RETURNING. +set ::xbestindex_sql { + INSERT INTO y2 VALUES(NULL, NULL) RETURNING rowid; +} +do_execsql_test 1.4 { + INSERT INTO y1 VALUES(5, 6) RETURNING rowid; +} {3} + +do_test 1.5 { + set ::xbestindex_res +} {1} + +finish_test diff --git a/test/bestindexC.test b/test/bestindexC.test new file mode 100644 index 0000000000..8b96a19e6c --- /dev/null +++ b/test/bestindexC.test @@ -0,0 +1,426 @@ +# 2024-04-26 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix bestindexC + +ifcapable !vtab { + finish_test + return +} + +register_tcl_module db + +proc vtab_command {lVal method args} { + switch -- $method { + xConnect { + return "CREATE TABLE t1(a)" + } + + xBestIndex { + set hdl [lindex $args 0] + set clist [$hdl constraints] + set orderby [$hdl orderby] + + set idxstr [list] + set res [list] + + set idx 0 + foreach c $clist { + array set a $c + if {$a(usable)==0} continue + if {$a(op)=="limit" && ![info exists ::do_not_use_limit]} { + lappend idxstr limit + lappend res omit $idx + } + if {$a(op)=="offset" && ![info exists ::do_not_use_offset]} { + lappend idxstr offset + lappend res omit $idx + } + incr idx + } + + return "cost 1000000 rows 1000000 idxnum 0 idxstr {$idxstr} $res" + } + + xFilter { + set idxstr [lindex $args 1] + set LIMIT "" + foreach a $idxstr b [lindex $args 2] { + set x($a) $b + } + + if {![info exists x(limit)]} { set x(limit) -1 } + if {![info exists x(offset)]} { set x(offset) -1 } + set LIMIT " LIMIT $x(limit) OFFSET $x(offset)" + + set idx 1 + foreach v $lVal { + lappend lRow "($idx, '$v')" + incr idx + } + + return [list sql " + SELECT * FROM ( VALUES [join $lRow ,]) $LIMIT + "] + } + } + + return {} +} + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE x1 USING tcl(vtab_command "a b c d e f"); + CREATE VIRTUAL TABLE x2 USING tcl(vtab_command "A B C D E F a b"); +} {} + +do_execsql_test 1.1 { + CREATE TEMP TABLE t_unionall AS + SELECT * FROM x1 UNION ALL SELECT * FROM x2; + + CREATE TEMP TABLE t_intersect AS + SELECT * FROM x1 INTERSECT SELECT * FROM x2; + + CREATE TEMP TABLE t_union AS + SELECT * FROM x1 UNION SELECT * FROM x2; + + CREATE TEMP TABLE t_except AS + SELECT * FROM x1 EXCEPT SELECT * FROM x2; +} + +foreach {tn limit} { + 1 "LIMIT 8" + 2 "LIMIT 4" + 3 "LIMIT 4 OFFSET 2" + 4 "LIMIT 8 OFFSET 4" +} { + + foreach {op tbl} { + "UNION ALL" t_unionall + "UNION" t_union + "INTERSECT" t_intersect + "EXCEPT" t_except + } { + + set expect [execsql "SELECT * FROM $tbl $limit"] + do_execsql_test 1.2.$tbl.$tn "SELECT * FROM ( + SELECT * FROM x1 $op SELECT * FROM x2 + ) $limit" $expect + + } + +} + +#------------------------------------------------------------------------- +reset_db +register_tcl_module db + +do_execsql_test 2.0 { + CREATE VIRTUAL TABLE x1 USING tcl(vtab_command "a b c d e f"); + CREATE VIRTUAL TABLE x2 USING tcl(vtab_command "a b e f"); +} {} + +do_execsql_test 2.1 { + SELECT * FROM x1 + EXCEPT + SELECT * FROM x2 + LIMIT 3 +} {c d} + +#------------------------------------------------------------------------- +reset_db +register_tcl_module db +do_execsql_test 3.0 { + CREATE VIRTUAL TABLE y1 USING tcl(vtab_command "1 2 3 4 5 6 7 8 9 10"); +} {} + +do_execsql_test 3.1 { + SELECT * FROM y1 WHERE a = COALESCE('8', a) LIMIT 3 +} {8} + +do_execsql_test 3.2 { + SELECT * FROM y1 WHERE a = '2' LIMIT 3 +} {2} + +load_static_extension db series +do_execsql_test 3.3 { + SELECT * FROM generate_series(1, 5) WHERE value = (value & 14) LIMIT 3 +} {2 4} + +do_execsql_test 3.4 { + SELECT value FROM generate_series(1,10) WHERE value>2 LIMIT 4 OFFSET 1; +} {4 5 6 7} + +set ::do_not_use_limit 1 +do_execsql_test 3.5 { + SELECT * FROM y1 LIMIT 5 OFFSET 3 +} {4 5 6 7 8} +unset ::do_not_use_limit +set ::do_not_use_offset 1 +do_execsql_test 3.6 { + SELECT * FROM y1 LIMIT 5 OFFSET 3 +} {4 5 6 7 8} +unset ::do_not_use_offset + +#------------------------------------------------------------------------- +reset_db +proc vtab_command {lVal method args} { + switch -- $method { + xConnect { error "not happy!" } + } + + return {} +} + +register_tcl_module db +do_catchsql_test 4.0 { + CREATE VIRTUAL TABLE y1 USING tcl(vtab_command 1); +} {1 {not happy!}} +do_test 4.1 { + sqlite3_errcode db +} SQLITE_ERROR + +proc vtab_command {lVal method args} { + switch -- $method { + xConnect { + return $lVal + } + } + return {} +} + +do_catchsql_test 4.2 { + CREATE VIRTUAL TABLE y1 USING tcl(vtab_command "PRAGMA page_size=1024"); +} {1 {declare_vtab: syntax error}} +do_catchsql_test 4.3 { + CREATE VIRTUAL TABLE y1 USING tcl(vtab_command "CREATE TABLE x1("); +} {1 {declare_vtab: incomplete input}} +do_catchsql_test 4.4 { + CREATE VIRTUAL TABLE y1 USING tcl(vtab_command "CREATE TABLE x1(insert)"); +} {1 {declare_vtab: near "insert": syntax error}} + +#------------------------------------------------------------------------- +reset_db +register_tcl_module db + +proc quote {str} { + return "'[string map {' ''} $str]'" +} + +proc vtab_command {lVal method args} { + switch -- $method { + xConnect { + return "CREATE TABLE t1(a, b, c, d)" + } + + xBestIndex { + set hdl [lindex $args 0] + set clist [$hdl constraints] + + set res [list] + set idx 0 + set idxnum 0 + + set cols(0) a + set cols(1) b + set cols(2) c + + set lCons [list] + + foreach c $clist { + array set a $c + if {$a(usable)==0} continue + + if {($a(op)=="eq" || $a(op)=="is") && [info exists cols($a(column))]} { + lappend res omit $idx + set coll [$hdl collation $idx] + lappend lCons "$cols($a(column)) = %[llength $lCons]% COLLATE $coll" + + set idxnum [expr {$idx + (1 << $a(column))}] + catch { unset cols($a(column)) } + } + + incr idx + } + + if {[llength [array names cols]]>0} { + set missing [list] + for {set i 0} {$i < 3} {incr i} { + catch { lappend missing $cols($i) } + } + set msg "missing required constraints: [join $missing ,]" + return [list constraint $msg] + } + + set idxstr [join $lCons " AND "] + return "cost 1000 rows 1000 idxnum $idxnum $res idxstr {$idxstr}" + } + + xFilter { + foreach {idxnum idxstr lArg} $args {} + set i 0 + set where $idxstr + foreach a $lArg { + set where [string map [list %$i% [quote $a]] $where] + incr i + } + # puts $where + return [list sql "SELECT rowid, * FROM $lVal WHERE $where"] + } + } + + return {} +} + +do_execsql_test 5.1 { + CREATE VIRTUAL TABLE x1 USING tcl(vtab_command t1); + CREATE TABLE t1(a, b, c, d); +} + +foreach {tn where ok} { + 0 "WHERE a=? AND b=? AND c=? AND c=?" 1 + 1 "WHERE a=? AND b=? AND c=?" 1 + 2 "WHERE a=? AND b=? AND (c=? OR c=?)" 1 + 3 "WHERE a=? AND b=? AND (c=? OR c=? OR c=?)" 1 + 4 "WHERE a=? AND b=? AND (c IS ? OR c IS ?)" 1 + 5 "WHERE a=? AND ((b=? AND c=?) OR (c=? AND b=?))" 1 + 6 "WHERE a=? AND ((b=? AND c=?) OR (c=?))" 0 +} { + do_test 5.2.$tn { + catch { execsql "SELECT * FROM x1 $::where" } msg +# if {$tn==0 || $tn==2 || $tn==3} { puts "MSG: $msg" } + } [expr !$ok] +} + +do_execsql_test 5.3 { + SELECT * FROM x1 WHERE (a, b, c) = (?, ?, ?); +} + +do_execsql_test 5.4 { + INSERT INTO t1(rowid, a, b, c, d) VALUES(1, 'x', 'y', 'z', 'one'); + INSERT INTO t1(rowid, a, b, c, d) VALUES(2, 'X', 'Y', 'Z', 'two'); + SELECT * FROM x1 WHERE (a, b, c) = ('X', 'Y', 'Z'); +} {X Y Z two} +do_execsql_test 5.5 { + SELECT * FROM x1 WHERE a='x' AND b='y' AND c='z'; +} {x y z one} +do_execsql_test 5.6 { + SELECT * FROM x1 + WHERE a='x' COLLATE nocase AND b='y' COLLATE nocase AND c='z'COLLATE nocase; +} {x y z one X Y Z two} + +do_execsql_test 5.7 { + DELETE FROM t1; + + INSERT INTO t1(rowid, a, b, c, d) VALUES(0, 'x', 'y', 'z', 'zero'); + INSERT INTO t1(rowid, a, b, c, d) VALUES(1, 'x', 'y', 'Z', 'one'); + INSERT INTO t1(rowid, a, b, c, d) VALUES(2, 'x', 'Y', 'z', 'two'); + INSERT INTO t1(rowid, a, b, c, d) VALUES(3, 'x', 'Y', 'Z', 'three'); + INSERT INTO t1(rowid, a, b, c, d) VALUES(4, 'X', 'y', 'z', 'four'); + INSERT INTO t1(rowid, a, b, c, d) VALUES(5, 'X', 'y', 'Z', 'five'); + INSERT INTO t1(rowid, a, b, c, d) VALUES(6, 'X', 'Y', 'z', 'six'); + INSERT INTO t1(rowid, a, b, c, d) VALUES(7, 'X', 'Y', 'z', 'seven'); +} + +do_execsql_test 5.8 { + SELECT d FROM x1 + WHERE a='x' AND ((b='y' AND c='z') OR (b='Y' AND c='z' COLLATE nocase)) +} { + zero two three +} + +do_execsql_test 5.9 { + SELECT d FROM x1 + WHERE a='x' COLLATE nocase + AND ((b='y' AND c='z') OR (b='Y' AND c='z' COLLATE nocase)) +} { + zero four two + three six seven +} + +#-------------------------------------------------------------------------- + +reset_db +register_tcl_module db + +proc quote {str} { + return "'[string map {' ''} $str]'" +} + +proc vtab_command {lVal method args} { + switch -- $method { + xConnect { + return "CREATE TABLE t1(a, b, c, d)" + } + + xBestIndex { + set hdl [lindex $args 0] + set clist [$hdl constraints] + + set idx 0 + set idxnum 0 + + foreach c $clist { + array set a $c + if {$a(usable)==0} continue + + if {$a(op)=="limit"} { + set idxnum [$hdl rhs_value $idx 555] + } + + incr idx + } + + return "cost 1000 rows 1000 idxnum $idxnum" + + } + + xFilter { + foreach {idxnum idxstr lArg} $args {} + return [list sql "SELECT 0, $idxnum, $idxnum, $idxnum, $idxnum"] + } + } + + return {} +} + +do_execsql_test 6.0 { + CREATE TABLE t1(x, y); + INSERT INTO t1 VALUES(2, 2); + CREATE VIRTUAL TABLE x1 USING tcl(vtab_command t1); +} + +do_execsql_test 6.1 { SELECT * FROM x1 LIMIT 50 } {50 50 50 50} + +do_execsql_test 6.2 { SELECT * FROM x1 WHERE b=c LIMIT 5 } {0 0 0 0} + +do_execsql_test 6.3 { + SELECT (SELECT a FROM x1 WHERE t1.x=t1.y LIMIT 10) FROM t1 +} {0} + +do_execsql_test 6.4 { + SELECT (SELECT a FROM x1 WHERE x1.a=1) FROM t1 +} {1} + +do_execsql_test 6.5 { + SELECT (SELECT a FROM x1 WHERE x1.a=1 LIMIT 1) FROM t1 +} {1} + +do_execsql_test 6.6 { + SELECT (SELECT a FROM x1 WHERE x1.a=555 LIMIT 2) FROM t1 +} {555} + +finish_test + + diff --git a/test/bestindexD.test b/test/bestindexD.test new file mode 100644 index 0000000000..b06d6b4270 --- /dev/null +++ b/test/bestindexD.test @@ -0,0 +1,93 @@ +# 2024-08-03 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix bestindexD + +ifcapable !vtab { + finish_test + return +} + +register_tcl_module db + +proc vtab_command {method args} { + switch -- $method { + xConnect { + return "CREATE TABLE t1(a PRIMARY KEY, b, c) WITHOUT ROWID" + } + + xBestIndex { + set hdl [lindex $args 0] + set ::colUsed [$hdl mask] + + set cost 1000000 + set used "" + + set cons 0 + foreach c [$hdl constraints] { + set cost [expr $cost/10] + append used " use $cons" + incr cons + } + + return "cost $cost rows $cost $used" + } + } + + return {} +} + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE x1 USING tcl(vtab_command); + + CREATE TABLE t2(a, b); +} {} + +# This proc assumes that there is only one use of a virtual table - x1 - +# in SQL statement $sql. It tests that the colUsed value passed to the +# xBestIndex method matches the actual columns used, which is ascertained +# by searching the compiled VM code for VColumn instructions. +# +proc do_colsused_test {tn sql} { + set ::colUsed "" + execsql $sql + set got $::colUsed + + set expect 0 + db eval "EXPLAIN $sql" x { + if {$x(opcode)=="VColumn"} { + set expect [expr $expect | (1<<$x(p2))] + } + } + + uplevel [list do_test $tn.($expect/$got) [list expr ($expect & $got)] $expect] +} + +do_colsused_test 1.1 { SELECT a FROM x1 } +do_colsused_test 1.2 { SELECT a,c FROM x1 } +do_colsused_test 1.3 { SELECT b FROM x1 } +do_colsused_test 1.4 { SELECT b FROM x1 WHERE c=? } + +do_colsused_test 1.5 { + select 1 from t2 full join x1; +} + +do_colsused_test 1.6 { + select 1 from x1 WHERE (b=? AND c=?) OR (b=? AND c=?) +} + +finish_test + + diff --git a/test/bestindexE.test b/test/bestindexE.test new file mode 100644 index 0000000000..d225d74aef --- /dev/null +++ b/test/bestindexE.test @@ -0,0 +1,191 @@ +# 2025-09-02 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix bestindexE + +ifcapable !vtab { + finish_test + return +} + +register_tcl_module db + +proc pretty_constraint {lCol cons} { + array set C $cons + + set ret "" + if {$C(usable)} { + set OP(eq) = + set ret "[lindex $lCol $C(column)]$OP($C(op))?" + } + + return $ret +} + +proc vtab_command {zName lCol method args} { + switch -- $method { + xConnect { + return "CREATE TABLE $zName ([join $lCol ,]) " + } + + xBestIndex { + set hdl [lindex $args 0] + set conslist [$hdl constraints] + + set ret [list] + foreach cons $conslist { + set pretty [pretty_constraint $lCol $cons] + if {$pretty != ""} { lappend ret $pretty } + } + + lappend ::xBestIndex "$zName: [join $ret { AND }]" + + return "cost 1000 rows 1000 idxnum 555" + } + } + + return {} +} + +proc do_bestindex_test {tn sql lCons} { + set ::xBestIndex [list] + do_execsql_test $tn.1 $sql + uplevel [list do_test $tn.2 [list set ::xBestIndex] [list {*}$lCons]] +} + +proc create_vtab {tname clist} { + set cmd [list vtab_command $tname $clist] + execsql " + CREATE VIRTUAL TABLE $tname USING tcl('$cmd') + " +} + +do_test 1.0 { + create_vtab x1 {a b c} +} {} + +do_bestindex_test 1.1 { + SELECT * FROM x1 WHERE a=? +} {{x1: a=?}} + +do_bestindex_test 1.2 { + SELECT * FROM x1 WHERE a=? AND b=? +} {{x1: a=? AND b=?}} + +#-------------------------------------------------------------------------- +reset_db +register_tcl_module db + +do_test 2.0 { + create_vtab Delivery {id customer} + create_vtab ReturnDelivery {id customer} + create_vtab Customer {oid name} +} {} + +do_bestindex_test 2.1 { + SELECT Delivery.ID, Customer.Name + FROM Delivery LEFT JOIN + Customer ON Delivery.Customer = Customer.OID +} { + {Delivery: } + {Customer: oid=?} +} + +do_bestindex_test 2.2 { + SELECT * FROM + ( + SELECT Delivery.ID, Customer.Name + FROM Delivery LEFT JOIN + Customer ON Delivery.Customer = Customer.OID + + UNION + + SELECT ReturnDelivery.ID, Customer.Name + FROM ReturnDelivery LEFT JOIN + Customer ON ReturnDelivery.Customer = Customer.OID + ) + WHERE ID = 1 +} { + {Delivery: id=?} + {Customer: oid=?} + {ReturnDelivery: id=?} + {Customer: oid=?} +} + +#-------------------------------------------------------------------------- +reset_db +register_tcl_module db eponymous_cmd + +proc eponymous_cmd {method args} { + switch -- $method { + xConnect { + db eval { SELECT * FROM sqlite_schema } + return "CREATE TABLE t1 (a, b)" + } + + xBestIndex { + return "idxnum 555" + } + + xFilter { + return [list sql {SELECT 123, 'A', 'B'}] + } + + xUpdate { + return 123 + } + + } + + return {} +} + +do_execsql_test 3.1.0 { + PRAGMA table_info = tcl +} { + 0 a {} 0 {} 0 1 b {} 0 {} 0 +} +do_execsql_test 3.1.1 { + SELECT rowid, * FROM tcl +} {123 A B} +do_execsql_test 3.1.2 { + INSERT INTO tcl VALUES('i', 'ii') RETURNING *; +} {i ii} +do_test 3.1.3 { + db last_insert_rowid +} {123} + +do_execsql_test 3.1.4 { + CREATE TABLE x1(x); +} + +db close +sqlite3 db test.db +register_tcl_module db eponymous_cmd +sqlite3 db2 test.db + +# Load the schema into connection [db] +do_execsql_test 3.2.1 { SELECT * FROM x1 } + +# Modify the schema on disk +do_execsql_test -db db2 3.2.2 { CREATE TABLE x2(x) } + +# Insert with RETURNING trigger on eponymous table. +do_execsql_test 3.2.3 { + INSERT INTO tcl VALUES('i', 'ii') RETURNING *; +} {i ii} + +finish_test + diff --git a/test/between.test b/test/between.test index df4c67995c..5e02ef9b22 100644 --- a/test/between.test +++ b/test/between.test @@ -58,10 +58,10 @@ proc queryplan {sql} { set eqp [execsql "EXPLAIN QUERY PLAN $sql"] # puts eqp=$eqp foreach {a b c x} $eqp { - if {[regexp { TABLE (\w+ AS )?(\w+) USING.* INDEX (\w+)\y} \ - $x all as tab idx]} { + if {[regexp {(SCAN|SEARCH) (\w+ AS )?(\w+) USING.* INDEX (\w+)\y} \ + $x all ss as tab idx]} { lappend data $tab $idx - } elseif {[regexp { TABLE (\w+ AS )?(\w+)\y} $x all as tab]} { + } elseif {[regexp {(SCAN|SEARCH) (\w+ AS )?(\w+)\y} $x all ss as tab]} { lappend data $tab * } } @@ -119,5 +119,42 @@ do_test between-1.5.3 { } } {4 2 25 27 sort t1 *} +#------------------------------------------------------------------------- +reset_db +do_execsql_test between-2.0 { + CREATE TABLE t1(x TEXT, y TEXT COLLATE nocase); + INSERT INTO t1 VALUES('0', 'abc'); +} + +foreach {tn expr res} { + 1 "x BETWEEN 1 AND '5'" 0 + 2 "x COLLATE binary BETWEEN 1 AND '5'" 0 + 3 "x COLLATE nocase BETWEEN 1 AND '5'" 0 + + 4 "y BETWEEN 'A' AND 'B'" 1 + 5 "y COLLATE nocase BETWEEN 'A' AND 'B'" 1 + 6 "y COLLATE binary BETWEEN 'A' AND 'B'" 0 + 7 "(y COLLATE binary) BETWEEN 'A' AND 'B'" 0 +} { + set sql "SELECT $expr FROM t1" + do_execsql_test between-2.1.$tn $sql $res +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test between-3.0 { + CREATE TABLE t1(x, y); + CREATE INDEX i1 ON t1(x); + INSERT INTO t1 VALUES(4, 4); + CREATE TABLE t2(a, b); +} + +do_execsql_test between-3.1 { + SELECT * FROM t1 LEFT JOIN t2 ON (x BETWEEN 1 AND 3); +} {4 4 {} {}} + +do_execsql_test between-3.2 { + SELECT * FROM t1 LEFT JOIN t2 ON (x BETWEEN 5 AND 7); +} {4 4 {} {}} finish_test diff --git a/test/bigmmap.test b/test/bigmmap.test new file mode 100644 index 0000000000..ba9f842524 --- /dev/null +++ b/test/bigmmap.test @@ -0,0 +1,108 @@ +# 2017 August 07 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this script testing the ability of SQLite to use mmap +# to access files larger than 4GiB. +# + +if {[file exists skip-big-file]} return +if {$tcl_platform(os)=="Darwin"} return + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix bigmmap + +ifcapable !mmap||!vtab { + finish_test + return +} + +set mmap_limit 0 +db eval { + SELECT compile_options AS x FROM pragma_compile_options + WHERE x LIKE 'max_mmap_size=%' +} { + regexp {MAX_MMAP_SIZE=([0-9]*)} $x -> mmap_limit +} +if {$mmap_limit < [expr 8 * 1<<30]} { + puts "Skipping bigmmap.test - requires SQLITE_MAX_MMAP_SIZE >= 8G" + finish_test + return +} + + +#------------------------------------------------------------------------- +# Create the database file roughly 8GiB in size. Most pages are unused, +# except that there is a table and index clustered around each 1GiB +# boundary. +# +do_execsql_test 1.0 { + PRAGMA page_size = 4096; + CREATE TABLE t0(a INTEGER PRIMARY KEY, b, c, UNIQUE(b, c)); + WITH s(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM s LIMIT 100 ) + INSERT INTO t0 SELECT i, 't0', randomblob(800) FROM s; +} + +for {set i 1} {$i < 8} {incr i} { + if {[catch {fake_big_file [expr $i*1024] [get_pwd]/test.db}]} { + puts "Cannot create ${i}MB sparse file" + finish_test + return + } + hexio_write test.db 28 [format %.8x [expr ($i*1024*1024*1024/4096) - 5]] + + do_execsql_test 1.$i " + CREATE TABLE t$i (a INTEGER PRIMARY KEY, b, c, UNIQUE(b, c)); + WITH s(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM s LIMIT 100 ) + INSERT INTO t$i SELECT i, 't$i', randomblob(800) FROM s; + " +} + +#------------------------------------------------------------------------- +# Check that data can be retrieved from the db with a variety of +# configured mmap size limits. +# +for {set i 0} {$i < 9} {incr i} { + + # Configure a memory mapping $i GB in size. + # + set val [expr $i*1024*1024*1024] + execsql "PRAGMA main.mmap_size = $val" + do_execsql_test 2.$i.0 { + PRAGMA main.mmap_size + } $val + + for {set t 0} {$t < 8} {incr t} { + do_execsql_test 2.$i.$t.1 " + SELECT count(*) FROM t$t; + SELECT count(b || c) FROM t$t GROUP BY b; + " {100 100} + + do_execsql_test 2.$i.$t.2 " + SELECT * FROM t$t AS o WHERE + NOT EXISTS( SELECT * FROM t$t AS i WHERE a=o.a AND +b=o.b AND +c=o.c ) + ORDER BY b, c; + " {} + + do_eqp_test 2.$i.$t.3 " + SELECT * FROM t$t AS o WHERE + NOT EXISTS( SELECT * FROM t$t AS i WHERE a=o.a AND +b=o.b AND +c=o.c ) + ORDER BY b, c; + " [string map {"\n " "\n"} " + QUERY PLAN + |--SCAN o USING COVERING INDEX sqlite_autoindex_t${t}_1 + `--CORRELATED SCALAR SUBQUERY xxxxxx + `--SEARCH i USING INTEGER PRIMARY KEY (rowid=?) + "] + } +} + +finish_test diff --git a/test/bigrow.test b/test/bigrow.test index fa59c36252..83270cb764 100644 --- a/test/bigrow.test +++ b/test/bigrow.test @@ -220,4 +220,15 @@ do_test bigrow-5.99 { execsql {DROP TABLE t1} } {} +if {$SQLITE_MAX_LENGTH > 1200*1000*1000} { + # If SQLITE_MAX_LENGTH is large enough, check that a serial type greater + # than 0x7FFFFFFF does not cause trouble. + set v [string repeat A 1100000000] + do_execsql_test bigrow-6.0 { + CREATE TABLE docs(content TEXT); + INSERT INTO docs VALUES ( $v ); + } + do_execsql_test bigrow-6.1 { SELECT content FROM docs } [list $v] +} + finish_test diff --git a/test/bigsort.test b/test/bigsort.test index c711515973..61c3344254 100644 --- a/test/bigsort.test +++ b/test/bigsort.test @@ -24,10 +24,17 @@ set testprefix bigsort # memory. Make sure the host has at least 8GB available before running # this test. # +# Update: https://sqlite.org/src/info/7c96a56 adds assert() statements +# that make this test too slow to run with SQLITE_DEBUG builds. +# if {[catch {exec free | grep Mem:} out] || [lindex $out 1]<8000000} { finish_test return } +ifcapable debug { + finish_test + return +} do_execsql_test 1.0 { PRAGMA page_size = 1024; diff --git a/test/bind.test b/test/bind.test index 34f3155b95..8cafaab2bc 100644 --- a/test/bind.test +++ b/test/bind.test @@ -366,11 +366,11 @@ do_test bind-8.1 { } {1} do_test bind-8.2 { sqlite3_errmsg $DB -} {bind or column index out of range} +} {column index out of range} ifcapable {utf16} { do_test bind-8.3 { encoding convertfrom unicode [sqlite3_errmsg16 $DB] - } {bind or column index out of range} + } {column index out of range} } do_test bind-8.4 { sqlite3_bind_null $VM 1 @@ -381,11 +381,11 @@ do_test bind-8.5 { } {1} do_test bind-8.6 { sqlite3_errmsg $DB -} {bind or column index out of range} +} {column index out of range} ifcapable {utf16} { do_test bind-8.7 { encoding convertfrom unicode [sqlite3_errmsg16 $DB] - } {bind or column index out of range} + } {column index out of range} } do_test bind-8.8 { diff --git a/test/bind2.test b/test/bind2.test new file mode 100644 index 0000000000..1ebf35c2e0 --- /dev/null +++ b/test/bind2.test @@ -0,0 +1,63 @@ +# 2022 Feb 10 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix bind2 + + +# Test that using bind_value() on a REAL sqlite3_value that was stored +# as an INTEGER works properly. +# +# 1.1: An IntReal value read from a table, +# 1.2: IntReal values obtained via the sqlite3_preupdate_old|new() +# interfaces. +# +do_execsql_test 1.0 { + CREATE TABLE t1(a REAL); + INSERT INTO t1 VALUES(42.0); + SELECT * FROM t1; +} {42.0} + +do_test 1.1 { + set stmt [sqlite3_prepare db "SELECT ?" -1 tail] + sqlite3_bind_value_from_select $stmt 1 "SELECT a FROM t1" + sqlite3_step $stmt + sqlite3_column_text $stmt 0 +} {42.0} +sqlite3_finalize $stmt + +ifcapable !preupdate { + finish_test + return +} + +proc preup {args} { + set stmt [sqlite3_prepare db "SELECT ?" -1 tail] + sqlite3_bind_value_from_preupdate $stmt 1 old 0 + sqlite3_step $stmt + lappend ::reslist [sqlite3_column_text $stmt 0] + sqlite3_reset $stmt + sqlite3_bind_value_from_preupdate $stmt 1 new 0 + sqlite3_step $stmt + lappend ::reslist [sqlite3_column_text $stmt 0] + sqlite3_finalize $stmt +} +db preupdate hook preup + +do_test 1.2 { + set ::reslist [list] + execsql { UPDATE t1 SET a=43; } + set ::reslist +} {42.0 43.0} + +finish_test diff --git a/test/bloom1.test b/test/bloom1.test new file mode 100644 index 0000000000..09553c3b9b --- /dev/null +++ b/test/bloom1.test @@ -0,0 +1,241 @@ +# 2022 October 06 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# Tests for queries that use bloom filters + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/lock_common.tcl +source $testdir/malloc_common.tcl + +set testprefix bloom1 + +# Tests 1.* verify that the bloom filter code correctly handles the +# case where the RHS of an (<ipk-column> = ?) expression must be coerced +# to an integer before the comparison made. +# +do_execsql_test 1.0 { + CREATE TABLE t1(a, b); + CREATE TABLE t2(c INTEGER PRIMARY KEY, d); +} + +do_execsql_test 1.1 { + INSERT INTO t1 VALUES('hello', 'world'); + INSERT INTO t2 VALUES(14, 'fourteen'); +} + +do_execsql_test 1.2 { + ANALYZE sqlite_schema; + INSERT INTO sqlite_stat1 VALUES('t2','idx1','6 6'); + ANALYZE sqlite_schema; +} + +do_execsql_test 1.3 { + SELECT 'affinity!' FROM t1 CROSS JOIN t2 WHERE t2.c = '14'; +} {affinity!} + + +reset_db +do_execsql_test 1.4 { + CREATE TABLE t1(a, b TEXT); + CREATE TABLE t2(c INTEGER PRIMARY KEY, d); + CREATE TABLE t3(e INTEGER PRIMARY KEY, f); + + ANALYZE sqlite_schema; + INSERT INTO sqlite_stat1 VALUES('t1','idx1','600 6'); + INSERT INTO sqlite_stat1 VALUES('t2','idx1','6 6'); + INSERT INTO sqlite_stat1 VALUES('t3','idx2','6 6'); + ANALYZE sqlite_schema; + + INSERT INTO t1 VALUES(1, '123'); + INSERT INTO t2 VALUES(123, 'one'); + INSERT INTO t3 VALUES(123, 'two'); +} + +do_execsql_test 1.5 { + SELECT 'result' FROM t1, t2, t3 + WHERE t2.c=t1.b AND t2.d!='silly' + AND t3.e=t1.b AND t3.f!='silly' +} {result} + +# 2023-02-05 +# https://sqlite.org/forum/forumpost/56de336385 +# +# Do not employ a Bloom filter if the table being filtered or any table +# wo the left of the table being filtered lacks STAT1 data, since we +# cannot make a good Bloom filter usefulness determination without STAT1 +# data. +# +reset_db +do_execsql_test 2.0 { + CREATE TABLE objs(c INTEGER, s INTEGER, p INTEGER, o INTEGER); + CREATE UNIQUE INDEX objs_cspo ON objs(o,p,c,s); + ANALYZE; + DELETE FROM sqlite_stat1; + INSERT INTO sqlite_stat1 VALUES('objs','objs_cspo','520138 21 20 19 1'); + ANALYZE sqlite_schema; +} +do_eqp_test 2.1 { + WITH RECURSIVE transit(x) AS ( + SELECT s FROM objs WHERE p=9 AND o=32805 + UNION + SELECT objs.s FROM objs, transit WHERE objs.p=9 AND objs.o=transit.x + ) + SELECT x FROM transit; +} { + QUERY PLAN + |--CO-ROUTINE transit + | |--SETUP + | | `--SEARCH objs USING COVERING INDEX objs_cspo (o=? AND p=?) + | `--RECURSIVE STEP + | |--SCAN transit + | `--SEARCH objs USING COVERING INDEX objs_cspo (o=? AND p=?) + `--SCAN transit +} + +# 2023-02-28 +# https://sqlite.org/forum/forumpost/0846211821 +# +# Bloom filter gives an incorrect result if the collating sequence is +# anything other than binary. +# +reset_db +do_execsql_test 3.1 { + CREATE TABLE t0(x TEXT COLLATE rtrim); + INSERT INTO t0(x) VALUES ('a'), ('b'), ('c'); + CREATE VIEW v0(y) AS SELECT DISTINCT x FROM t0; + SELECT count(*) FROM t0, v0 WHERE x='b '; +} 3 +do_eqp_test 3.2 { + SELECT count(*) FROM t0, v0 WHERE x='b '; +} { + QUERY PLAN + |--CO-ROUTINE v0 + | |--SCAN t0 + | `--USE TEMP B-TREE FOR DISTINCT + |--SCAN v0 + `--SEARCH t0 USING AUTOMATIC PARTIAL COVERING INDEX (x=?) +} +# ^^^^^--- The key feature in the previous result is that no Bloom filter +# is used. In the following, a Bloom filter is used because the data type +# is INT instead of TEXT. +do_execsql_test 3.3 { + CREATE TABLE t1(x INT COLLATE rtrim); + INSERT INTO t1(x) VALUES ('a'), ('b'), ('c'); + CREATE VIEW v1(y) AS SELECT DISTINCT x FROM t1; + SELECT count(*) FROM t1, v1 WHERE x='b '; +} 3 +do_eqp_test 3.4 { + SELECT count(*) FROM t1, v1 WHERE x='b '; +} { + QUERY PLAN + |--CO-ROUTINE v1 + | |--SCAN t1 + | `--USE TEMP B-TREE FOR DISTINCT + |--SCAN v1 + |--BLOOM FILTER ON t1 (x=?) + `--SEARCH t1 USING AUTOMATIC PARTIAL COVERING INDEX (x=?) +} + +# 2023-03-14 +# https://sqlite.org/forum/forumpost/d47a0e8e3a +# https://sqlite.org/forum/forumpost/2e427099d5 +# +# Both reports are for the same problem - using a Bloom filter on an +# expression index can cause issues. +# +reset_db +do_execsql_test 4.1 { + CREATE TABLE t1(x TEXT, y INT, z TEXT); + INSERT INTO t1(rowid,x,y,z) VALUES(12,'aa','bb','aa'); + CREATE INDEX i1x ON t1(1 IS true,z); + CREATE TABLE t0(x TEXT); + INSERT INTO t0(rowid,x) VALUES(4,'aa'); + ANALYZE sqlite_schema; + INSERT INTO sqlite_stat1 VALUES('t0',NULL,'20'); + INSERT INTO sqlite_stat1 VALUES('t1','i1x','18 18 2'); + ANALYZE sqlite_schema; +} +do_execsql_test 4.2 { + SELECT * FROM t0 NATURAL JOIN t1 WHERE z=t1.x; +} {aa bb aa} +do_execsql_test 4.3 { + DROP TABLE t0; + CREATE TABLE t0(a TEXT); + INSERT INTO t0 VALUES ('xyz'); + CREATE INDEX t0x ON t0(a IS FALSE) WHERE false; + DROP TABLE t1; + CREATE TABLE t1(b INT); + INSERT INTO t1 VALUES('aaa'),('bbb'),('ccc'),('ddd'),(NULL); + CREATE TABLE t2(c REAL); + INSERT INTO t2 VALUES(7); + ANALYZE; + CREATE INDEX t2x ON t2(true IN ()); +} +do_execsql_test 4.4 { + SELECT * FROM t0 LEFT JOIN t1 LEFT JOIN t2 ON (b NOTNULL)==(c IN ()) WHERE c; +} {xyz {} 7.0} + +reset_db +do_execsql_test 5.0 { + CREATE TABLE t1 (c1); + INSERT INTO t1 VALUES (101); + CREATE TABLE t2 ( x ); + INSERT INTO t2 VALUES(404); +} + +do_execsql_test 5.1 { + SELECT 'val' in ( + select 'val' from ( select 'valueB' from t1 order by 1 ) + union all + select 'val' + ); +} {1} + +do_execsql_test 5.2 { + select * from t2 + where 'val' in ( + select 'val' from ( select 'valueB' from t1 order by 1 ) + union all + select 'val' + ); +} {404} + +do_execsql_test 5.3 { + SELECT subq_1.c_0 as c_0 + FROM ( SELECT 0 as c_0) as subq_1 + WHERE (subq_1.c_0) IN ( + SELECT subq_2.c_0 as c_0 + FROM ( + SELECT 0 as c_0 + FROM t1 as ref_1 + WHERE (ref_1.c1) = (2) + ORDER BY c_0 desc + ) as subq_2 + UNION ALL + SELECT 0 as c_0 + ); +} {0} + +# 2025-04-30 https://sqlite.org/forum/forumpost/792a09cb3df9e69f +# A continuation of the above. +# +do_execsql_test 6.1 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a); + SELECT 111 IN ( + SELECT 222 FROM (SELECT 333 ORDER BY 1) + UNION ALL + SELECT 444 FROM (SELECT 555 FROM t1 ORDER BY 1) + ); +} 0 + +finish_test diff --git a/test/btree01.test b/test/btree01.test index 25f2c6897b..b1909a3adb 100644 --- a/test/btree01.test +++ b/test/btree01.test @@ -17,7 +17,7 @@ source $testdir/tester.tcl set testprefix btree01 # The refactoring on the b-tree balance() routine in check-in -# http://www.sqlite.org/src/info/face33bea1ba3a (2014-10-27) +# http://sqlite.org/src/info/face33bea1ba3a (2014-10-27) # caused the integrity_check on the following SQL to fail. # do_execsql_test btree01-1.1 { @@ -129,4 +129,28 @@ for {set i 1} {$i<=31} {incr i} { } {ok} } +# 2022-03-06 OSSFuzz issue 45329 +# An assertion fault due to the failure to clear a flag in an optimization +# committed last night. +# +# When the stay-on-last page optimization of sqlite3BtreeIndexMoveto() is +# invoked, it needs to clear the BTCF_ValidOvfl flag. +# +db close +sqlite3 db :memory: +do_execsql_test btree01-2.1 { + PRAGMA page_size=1024; + CREATE TABLE t1(a INT PRIMARY KEY, b BLOB, c INT) WITHOUT ROWID; + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<100) + INSERT INTO t1(a,b,c) SELECT x*2, zeroblob(100), x FROM c; + UPDATE t1 SET b=zeroblob(1000) WHERE a=198; + CREATE TABLE t2(x INTEGER PRIMARY KEY, y INT); + INSERT INTO t2(y) VALUES(198),(187),(100); + SELECT y, c FROM t2 LEFT JOIN t1 ON y=a ORDER BY x; +} {198 99 187 {} 100 50} +do_execsql_test btree01-2.2 { + SELECT y, c FROM t1 RIGHT JOIN t2 ON y=a ORDER BY x; +} {198 99 187 {} 100 50} + + finish_test diff --git a/test/btree02.test b/test/btree02.test index de7c06d442..c1fede5801 100644 --- a/test/btree02.test +++ b/test/btree02.test @@ -21,7 +21,7 @@ load_static_extension db eval do_execsql_test btree02-100 { CREATE TABLE t1(a TEXT, ax INTEGER, b INT, PRIMARY KEY(a,ax)) WITHOUT ROWID; WITH RECURSIVE c(i) AS (VALUES(1) UNION ALL SELECT i+1 FROM c WHERE i<10) - INSERT INTO t1(a,ax,b) SELECT printf('%02x',i), random(), i FROM c; + INSERT INTO t1(a,ax,b) SELECT printf('%02x',i+160), random(), i FROM c; CREATE INDEX t1a ON t1(a); CREATE TABLE t2(x,y); CREATE TABLE t3(cnt); @@ -29,24 +29,34 @@ do_execsql_test btree02-100 { INSERT INTO t3(cnt) SELECT i FROM c; SELECT count(*) FROM t1; } {10} + +proc showt1 {} { + puts -nonewline "t1: " + puts [db eval {SELECT printf('(%s,%s)',quote(a),quote(b)) FROM t1}] +} + do_test btree02-110 { db eval BEGIN set i 0 + # showt1 db eval {SELECT a, ax, b, cnt FROM t1 CROSS JOIN t3 WHERE b IS NOT NULL} { + if {$a==""} continue db eval {INSERT INTO t2(x,y) VALUES($b,$cnt)} # puts "a,b,cnt = ($a,$b,$cnt)" incr i if {$i%2==1} { set bx [expr {$b+1000}] - # puts "INSERT ($a),$bx" + # puts "INSERT ($a),$bx" db eval {INSERT INTO t1(a,ax,b) VALUES(printf('(%s)',$a),random(),$bx)} + # showt1 } else { # puts "DELETE a=$a" db eval {DELETE FROM t1 WHERE a=$a} + # showt1 } db eval {COMMIT; BEGIN} } db one {COMMIT; SELECT count(*) FROM t1;} -} {20} +} {10} finish_test diff --git a/test/btreefault.test b/test/btreefault.test index 61104c5a79..d0ba05961c 100644 --- a/test/btreefault.test +++ b/test/btreefault.test @@ -54,4 +54,53 @@ do_faultsim_test 1 -prep { faultsim_integrity_check } +#------------------------------------------------------------------------- +# dbsqlfuzz crash-6ef3cd3b18ccc5de86120950a0498641acd90a33.txt +# +reset_db + +do_execsql_test 2.0 { + CREATE TABLE t1(i INTEGER PRIMARY KEY, a, b); + CREATE INDEX i1 ON t1(b); + CREATE TABLE t2(x, y); +} + +do_execsql_test 2.1 { + INSERT INTO t1 VALUES(25, 25, 25); + INSERT INTO t2 VALUES(25, 'a'), (25, 'b'), (25, 'c'); +} + +faultsim_save +do_test 2.2 { + set res [list] + db eval { + SELECT x, y FROM t1 CROSS JOIN t2 WHERE t2.x=t1.i AND +t1.i=25 ORDER BY b + } { + lappend res $x $y + if {$y=="b"} { + db eval { DELETE FROM t1 WHERE i=25 } + } + } + set res +} {25 a 25 b} + +do_faultsim_test 2 -faults oom-t* -prep { + faultsim_restore_and_reopen + db eval {SELECT * FROM sqlite_master} +} -body { + set ::myres [list] + db eval { + SELECT x, y FROM t1 CROSS JOIN t2 WHERE t2.x=t1.i AND +t1.i=25 ORDER BY b + } { + lappend ::myres $x $y + if {$y=="b"} { + db eval { DELETE FROM t1 WHERE i=25 } + } + } + set ::myres +} -test { + faultsim_test_result {0 {25 a 25 b}} +} + + finish_test diff --git a/test/busy.test b/test/busy.test index 585f764547..896c7fa651 100644 --- a/test/busy.test +++ b/test/busy.test @@ -10,11 +10,11 @@ #*********************************************************************** # This file test the busy handler # -# $Id: busy.test,v 1.3 2008/03/15 02:09:22 drh Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl +set testprefix busy do_test busy-1.1 { sqlite3 db2 test.db @@ -55,7 +55,85 @@ do_test busy-2.2 { set busyargs } {0 1 2 3} - db2 close +#------------------------------------------------------------------------- +# Test that the busy-handler is invoked correctly for "PRAGMA optimize" +# and ANALYZE commnds. +ifcapable pragma&&analyze&&!stat4 { + +reset_db + +do_execsql_test 3.1 { + CREATE TABLE t1(x); + CREATE TABLE t2(y); + CREATE TABLE t3(z); + + CREATE INDEX i1 ON t1(x); + CREATE INDEX i2 ON t2(y); + + INSERT INTO t1 VALUES(1); + INSERT INTO t2 VALUES(1); + ANALYZE; + + SELECT * FROM t1 WHERE x=1; + SELECT * FROM t2 WHERE y=1; +} {1 1} + +do_test 3.2 { + sqlite3 db2 test.db + execsql { BEGIN EXCLUSIVE } db2 + catchsql { PRAGMA optimize } +} {1 {database is locked}} + +proc busy_handler {n} { + if {$n>1000} { execsql { COMMIT } db2 } + return 0 +} +db busy busy_handler + +do_test 3.3 { + catchsql { PRAGMA optimize } +} {0 {}} + +do_test 3.4 { + execsql { + BEGIN; + SELECT count(*) FROM sqlite_master; + } db2 +} {6} + +proc busy_handler {n} { return 1 } +do_test 3.5 { + catchsql { PRAGMA optimize } +} {1 {database is locked}} + +do_test 3.6 { + execsql { COMMIT } db2 + execsql { + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<1000 + ) + INSERT INTO t1 SELECT i FROM s; + } + execsql { + BEGIN; + SELECT count(*) FROM sqlite_master; + } db2 +} {6} + +do_test 3.7 { + catchsql { PRAGMA optimize } +} {1 {database is locked}} + +proc busy_handler {n} { + if {$n>1000} { execsql { COMMIT } db2 } + return 0 +} +do_test 3.8 { + catchsql { PRAGMA optimize } +} {0 {}} + +} + finish_test diff --git a/test/busy2.test b/test/busy2.test new file mode 100644 index 0000000000..ec420c8b20 --- /dev/null +++ b/test/busy2.test @@ -0,0 +1,171 @@ +# 2020 June 30 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file test the busy handler +# +# TESTRUNNER: slow + + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/lock_common.tcl +set testprefix busy2 + +do_multiclient_test tn { + do_test 1.$tn.0 { + sql2 { + CREATE TABLE t1(a, b); + PRAGMA journal_mode = wal; + INSERT INTO t1 VALUES('A', 'B'); + } + } {wal} + + do_test 1.$tn.1 { + code1 { db timeout 1000 } + sql1 { SELECT * FROM t1 } + } {A B} + + do_test 1.$tn.2 { + sql2 { + BEGIN; + INSERT INTO t1 VALUES('C', 'D'); + } + } {} + + do_test 1.$tn.3 { + set us [lindex [time { catch { sql1 { BEGIN EXCLUSIVE } } }] 0] + expr {$us>950000 && $us<1500000} + } {1} + + do_test 1.$tn.4 { + sql2 { + COMMIT + } + } {} +} + +#------------------------------------------------------------------------- + +do_multiclient_test tn { + # Make the db a WAL mode db. And add a table and a row to it. Then open + # a second connection within process 1. Process 1 now has connections + # [db] and [db1.2], process 2 has connection [db2] only. + # + # Configure all connections to use a 1000 ms timeout. + # + do_test 2.$tn.0 { + code1 { + sqlite3 db1.2 test.db + } + sql1 { + PRAGMA auto_vacuum = off; + PRAGMA journal_mode = wal; + CREATE TABLE t1(a, b); + INSERT INTO t1 VALUES(1, 2); + } + code2 { + db2 timeout 1000 + } + code1 { + db1.2 timeout 1000 + db timeout 1000 + db1.2 eval {SELECT * FROM t1} + } + } {1 2} + + # Take a read lock with [db] in process 1. + # + do_test 2.$tn.1 { + sql1 { + BEGIN; + SELECT * FROM t1; + } + } {1 2} + + # Insert a row using [db2] in process 2. Then try a passive checkpoint. + # It fails to checkpoint the final frame (due to the readlock taken by + # [db]), and returns in less than 250ms. + do_test 2.$tn.2 { + sql2 { INSERT INTO t1 VALUES(3, 4) } + set us [lindex [time { + set res [code2 { db2 eval { PRAGMA wal_checkpoint } }] + }] 0] + list [expr $us < 250000] $res + } {1 {0 4 3}} + + # Now try a FULL checkpoint with [db2]. It returns SQLITE_BUSY. And takes + # over 950ms to do so. + do_test 2.$tn.3 { + set us [lindex [time { + set res [code2 { db2 eval { PRAGMA wal_checkpoint = FULL } }] + }] 0] + list [expr $us > 950000] $res + } {1 {1 4 3}} + + # Passive checkpoint with [db1.2] (process 1). No SQLITE_BUSY, returns + # in under 250ms. + do_test 2.$tn.4 { + set us [lindex [time { + set res [code1 { db1.2 eval { PRAGMA wal_checkpoint } }] + }] 0] + list [expr $us < 250000] $res + } {1 {0 4 3}} + + # Full checkpoint with [db1.2] (process 1). SQLITE_BUSY returned in + # a bit over 950ms. + do_test 2.$tn.5 { + set us [lindex [time { + set res [code1 { db1.2 eval { PRAGMA wal_checkpoint = FULL } }] + }] 0] + list [expr $us > 950000] $res + } {1 {1 4 3}} + + code1 { + db1.2 close + } +} + +#------------------------------------------------------------------------- +# Check that even if the busy-handler fails (returns zero) within a +# call to sqlite3_prepare() (or _v2(), or _v3()), it is still invoked +# the next time an SQLITE_BUSY is encountered. +# +do_multiclient_test tn { + code1 { + set ::busy_called 0 + proc busy {args} { + if {$::busy_called} { return 1 } + set ::busy_called 1 + return 0 + } + db busy busy + } + + do_test 3.$tn.1 { + sql2 { + CREATE TABLE t1(x); + BEGIN EXCLUSIVE; + INSERT INTO t1 VALUES('x'); + } + } {} + + do_test 3.$tn.2 { + set ::busy_called 0 + list [catch { sql1 { SELECT * FROM t1 } } msg] $::busy_called + } {1 1} + + do_test 3.$tn.3 { + set ::busy_called 0 + list [catch { sql1 { SELECT * FROM t1 } } msg] $::busy_called + } {1 1} + +} + +finish_test diff --git a/test/cachespill.test b/test/cachespill.test new file mode 100644 index 0000000000..069251354f --- /dev/null +++ b/test/cachespill.test @@ -0,0 +1,77 @@ +# 2017 April 26 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix cachespill + +ifcapable !pager_pragmas { + finish_test + return +} + +#------------------------------------------------------------------------- +# Test that "PRAGMA cache_spill = 0" completely disables cache spilling. +# +do_execsql_test 1.1 { + PRAGMA auto_vacuum = 0; + PRAGMA page_size = 1024; + PRAGMA cache_size = 100; + CREATE TABLE t1(a); +} + +do_test 1.2 { + file size test.db +} {2048} + +do_test 1.3 { + execsql { + BEGIN; + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<200 + ) INSERT INTO t1 SELECT randomblob(900) FROM s; + } + expr {[file size test.db] > 50000} +} {1} + +do_test 1.4 { + execsql ROLLBACK + file size test.db +} {2048} + +do_test 1.5 { + execsql { + PRAGMA cache_spill = 0; + BEGIN; + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<200 + ) INSERT INTO t1 SELECT randomblob(900) FROM s; + } + file size test.db +} {2048} + +do_test 1.5 { + execsql { + ROLLBACK; + PRAGMA cache_spill = 1; + BEGIN; + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<200 + ) INSERT INTO t1 SELECT randomblob(900) FROM s; + } + expr {[file size test.db] > 50000} +} {1} + +do_execsql_test 1.6 { ROLLBACK } + + +finish_test diff --git a/test/capi2.test b/test/capi2.test index 39f50dd079..de47ab3d4f 100644 --- a/test/capi2.test +++ b/test/capi2.test @@ -64,13 +64,13 @@ do_test capi2-1.4 { } {t1 1} do_test capi2-1.5 { get_column_names $VM -} {name rowid text INTEGER} +} {name rowid TEXT INTEGER} do_test capi2-1.6 { sqlite3_step $VM } {SQLITE_DONE} do_test capi2-1.7 { list [sqlite3_column_count $VM] [get_row_values $VM] [get_column_names $VM] -} {2 {} {name rowid text INTEGER}} +} {2 {} {name rowid TEXT INTEGER}} # This used to be SQLITE_MISUSE. But now we automatically reset prepared # statements. @@ -91,7 +91,7 @@ ifcapable autoreset { do_test capi2-1.9 { sqlite3_reset $VM list [sqlite3_column_count $VM] [get_row_values $VM] [get_column_names $VM] -} {2 {} {name rowid text INTEGER}} +} {2 {} {name rowid TEXT INTEGER}} do_test capi2-1.10 { sqlite3_data_count $VM } {0} @@ -120,13 +120,13 @@ do_test capi2-2.2 { lappend r [sqlite3_column_count $VM] \ [get_row_values $VM] \ [get_column_names $VM] -} {SQLITE_ROW 2 {t1 1} {name rowid text INTEGER}} +} {SQLITE_ROW 2 {t1 1} {name rowid TEXT INTEGER}} do_test capi2-2.3 { set r [sqlite3_step $VM] lappend r [sqlite3_column_count $VM] \ [get_row_values $VM] \ [get_column_names $VM] -} {SQLITE_DONE 2 {} {name rowid text INTEGER}} +} {SQLITE_DONE 2 {} {name rowid TEXT INTEGER}} do_test capi2-2.4 { sqlite3_finalize $VM } {SQLITE_OK} @@ -141,7 +141,7 @@ do_test capi2-2.6 { lappend r [sqlite3_column_count $VM] \ [get_row_values $VM] \ [get_column_names $VM] -} {SQLITE_DONE 2 {} {name rowid text INTEGER}} +} {SQLITE_DONE 2 {} {name rowid TEXT INTEGER}} do_test capi2-2.7 { sqlite3_finalize $VM } {SQLITE_OK} @@ -163,7 +163,7 @@ do_test capi2-3.2 { sqlite3_prepare $DB {select bogus from } -1 TAIL } msg] lappend rc $msg $TAIL -} {1 {(1) near " ": syntax error} {}} +} {1 {(1) incomplete input} {}} do_test capi2-3.3 { set rc [catch { sqlite3_prepare $DB {;;;;select bogus from sqlite_master} -1 TAIL @@ -184,7 +184,7 @@ do_test capi2-3.5 { } {1 {(1) no such column: bogus} {;;x;}} do_test capi2-3.6 { set rc [catch { - sqlite3_prepare $DB {select 5/0} -1 TAIL + sqlite3_prepare $DB {select 5/0;} -1 TAIL } VM] lappend rc $TAIL } {0 {}} diff --git a/test/capi3.test b/test/capi3.test index 163bb19ada..6319d8284d 100644 --- a/test/capi3.test +++ b/test/capi3.test @@ -172,25 +172,26 @@ do_test capi3-3.3 { catch { set db2 [sqlite3_open /bogus/path/test.db {}] } - sqlite3_extended_errcode $db2 -} {SQLITE_CANTOPEN} + set ::capi3_errno [sqlite3_system_errno $db2] + list [sqlite3_extended_errcode $db2] [expr {$::capi3_errno!=0}] +} {SQLITE_CANTOPEN 1} do_test capi3-3.4 { sqlite3_errmsg $db2 } {unable to open database file} do_test capi3-3.5 { - sqlite3_close $db2 -} {SQLITE_OK} -if {[clang_sanitize_address]==0} { + list [sqlite3_system_errno $db2] [sqlite3_close $db2] +} [list $::capi3_errno SQLITE_OK] +if {[clang_sanitize_address]==0 && 0} { do_test capi3-3.6.1-misuse { sqlite3_close $db2 } {SQLITE_MISUSE} do_test capi3-3.6.2-misuse { sqlite3_errmsg $db2 - } {library routine called out of sequence} + } {bad parameter or other API misuse} ifcapable {utf16} { do_test capi3-3.6.3-misuse { utf8 [sqlite3_errmsg16 $db2] - } {library routine called out of sequence} + } {bad parameter or other API misuse} } } @@ -648,6 +649,18 @@ do_test capi3-5.33 { sqlite3_finalize $STMT } SQLITE_OK +# 2018-01-09: If a column is the last token if a string, the column name +# was not being set correctly, due to changes in check-in +# https://sqlite.org/src/info/0fdf97efe5df7455 +# +# This problem was detected by the community during beta-testing. +# +do_test capi3-5.34 { + set STMT [sqlite3_prepare $DB {SELECT :a, :b} -1 TAIL] + sqlite3_column_count $STMT +} 2 +check_header $STMT capi-5.35 {:a :b} {{} {}} +sqlite3_finalize $STMT set ::ENC [execsql {pragma encoding}] db close @@ -676,7 +689,9 @@ do_test capi3-6.3 { sqlite3_finalize $STMT } {SQLITE_OK} -if {[clang_sanitize_address]==0} { +if {0 && [clang_sanitize_address]==0} { + # This use-after-free occasionally causes segfaults during ordinary + # builds. Let's just disable it completely. do_test capi3-6.4-misuse { db cache flush sqlite3_close $DB @@ -728,6 +743,7 @@ if {![sqlite3 -has-codec]} { } {} do_test capi3-8.2 { sqlite3 db test.db + sqlite3_db_config db DEFENSIVE 0 execsql { PRAGMA writable_schema=ON; INSERT INTO sqlite_master VALUES(NULL,NULL,NULL,NULL,NULL); @@ -746,6 +762,7 @@ if {![sqlite3 -has-codec]} { db close forcedelete test.db test.db-journal sqlite3 db test.db + sqlite3_db_config db DEFENSIVE 0 execsql { CREATE TABLE t1(a); PRAGMA writable_schema=ON; @@ -768,9 +785,9 @@ forcedelete test.db-journal # Test the english language string equivalents for sqlite error codes set code2english [list \ SQLITE_OK {not an error} \ -SQLITE_ERROR {SQL logic error or missing database} \ +SQLITE_ERROR {SQL logic error} \ SQLITE_PERM {access permission denied} \ -SQLITE_ABORT {callback requested query abort} \ +SQLITE_ABORT {query aborted} \ SQLITE_BUSY {database is locked} \ SQLITE_LOCKED {database table is locked} \ SQLITE_NOMEM {out of memory} \ @@ -780,16 +797,13 @@ SQLITE_IOERR {disk I/O error} \ SQLITE_CORRUPT {database disk image is malformed} \ SQLITE_FULL {database or disk is full} \ SQLITE_CANTOPEN {unable to open database file} \ -SQLITE_EMPTY {table contains no data} \ SQLITE_SCHEMA {database schema has changed} \ SQLITE_CONSTRAINT {constraint failed} \ SQLITE_MISMATCH {datatype mismatch} \ -SQLITE_MISUSE {library routine called out of sequence} \ -SQLITE_NOLFS {large file support is disabled} \ +SQLITE_MISUSE {bad parameter or other API misuse} \ SQLITE_AUTH {authorization denied} \ -SQLITE_FORMAT {auxiliary database format error} \ -SQLITE_RANGE {bind or column index out of range} \ -SQLITE_NOTADB {file is encrypted or is not a database} \ +SQLITE_RANGE {column index out of range} \ +SQLITE_NOTADB {file is not a database} \ unknownerror {unknown error} \ ] @@ -801,7 +815,6 @@ foreach {code english} $code2english { # Test the error message when a "real" out of memory occurs. if { [permutation] != "nofaultsim" } { -ifcapable memdebug { do_test capi3-10-1 { sqlite3 db test.db set DB [sqlite3_connection_pointer db] @@ -839,7 +852,6 @@ ifcapable memdebug { db close sqlite3_memdebug_fail -1 } -} # The following tests - capi3-11.* - test that a COMMIT or ROLLBACK # statement issued while there are still outstanding VMs that are part of @@ -925,19 +937,20 @@ do_test capi3-11.9.3 { do_test capi3-11.10 { sqlite3_step $STMT } {SQLITE_ROW} -ifcapable !autoreset { - # If SQLITE_OMIT_AUTORESET is defined, then the statement must be - # reset() before it can be passed to step() again. - do_test capi3-11.11a { sqlite3_step $STMT } {SQLITE_MISUSE} - do_test capi3-11.11b { sqlite3_reset $STMT } {SQLITE_ABORT} -} do_test capi3-11.11 { sqlite3_step $STMT } {SQLITE_DONE} -do_test capi3-11.12 { - sqlite3_step $STMT - sqlite3_step $STMT -} {SQLITE_ROW} +ifcapable !autoreset { + do_test capi3-11.12armor { + sqlite3_step $STMT + sqlite3_step $STMT + } {SQLITE_MISUSE} +} else { + do_test capi3-11.12 { + sqlite3_step $STMT + sqlite3_step $STMT + } {SQLITE_ROW} +} do_test capi3-11.13 { sqlite3_finalize $STMT } {SQLITE_OK} diff --git a/test/capi3c.test b/test/capi3c.test index 15307a7f7a..247dbf35e5 100644 --- a/test/capi3c.test +++ b/test/capi3c.test @@ -176,11 +176,11 @@ if {[clang_sanitize_address]==0} { } {SQLITE_MISUSE} do_test capi3c-3.6.2-misuse { sqlite3_errmsg $db2 - } {library routine called out of sequence} + } {bad parameter or other API misuse} ifcapable {utf16} { do_test capi3c-3.6.3-misuse { utf8 [sqlite3_errmsg16 $db2] - } {library routine called out of sequence} + } {bad parameter or other API misuse} } } @@ -686,6 +686,7 @@ if {![sqlite3 -has-codec]} { } {} do_test capi3c-8.2 { sqlite3 db test.db + sqlite3_db_config db DEFENSIVE 0 execsql { PRAGMA writable_schema=ON; INSERT INTO sqlite_master VALUES(NULL,NULL,NULL,NULL,NULL); @@ -704,6 +705,7 @@ if {![sqlite3 -has-codec]} { db close forcedelete test.db test.db-journal sqlite3 db test.db + sqlite3_db_config db DEFENSIVE 0 execsql { CREATE TABLE t1(a); PRAGMA writable_schema=ON; @@ -726,9 +728,9 @@ forcedelete test.db-journal # Test the english language string equivalents for sqlite error codes set code2english [list \ SQLITE_OK {not an error} \ -SQLITE_ERROR {SQL logic error or missing database} \ +SQLITE_ERROR {SQL logic error} \ SQLITE_PERM {access permission denied} \ -SQLITE_ABORT {callback requested query abort} \ +SQLITE_ABORT {query aborted} \ SQLITE_BUSY {database is locked} \ SQLITE_LOCKED {database table is locked} \ SQLITE_NOMEM {out of memory} \ @@ -738,16 +740,14 @@ SQLITE_IOERR {disk I/O error} \ SQLITE_CORRUPT {database disk image is malformed} \ SQLITE_FULL {database or disk is full} \ SQLITE_CANTOPEN {unable to open database file} \ -SQLITE_EMPTY {table contains no data} \ +SQLITE_EMPTY {unknown error} \ SQLITE_SCHEMA {database schema has changed} \ SQLITE_CONSTRAINT {constraint failed} \ SQLITE_MISMATCH {datatype mismatch} \ -SQLITE_MISUSE {library routine called out of sequence} \ -SQLITE_NOLFS {large file support is disabled} \ +SQLITE_MISUSE {bad parameter or other API misuse} \ SQLITE_AUTH {authorization denied} \ -SQLITE_FORMAT {auxiliary database format error} \ -SQLITE_RANGE {bind or column index out of range} \ -SQLITE_NOTADB {file is encrypted or is not a database} \ +SQLITE_RANGE {column index out of range} \ +SQLITE_NOTADB {file is not a database} \ unknownerror {unknown error} \ ] @@ -759,7 +759,6 @@ foreach {code english} $code2english { # Test the error message when a "real" out of memory occurs. if { [permutation] != "nofaultsim" } { -ifcapable memdebug { do_test capi3c-10-1 { sqlite3 db test.db set DB [sqlite3_connection_pointer db] @@ -779,7 +778,6 @@ ifcapable memdebug { db close sqlite3_memdebug_fail -1 } -} # The following tests - capi3c-11.* - test that a COMMIT or ROLLBACK # statement issued while there are still outstanding VMs that are part of @@ -865,19 +863,20 @@ do_test capi3c-11.9.3 { do_test capi3c-11.10 { sqlite3_step $STMT } {SQLITE_ROW} -ifcapable !autoreset { - # If SQLITE_OMIT_AUTORESET is defined, then the statement must be - # reset() before it can be passed to step() again. - do_test capi3-11.11a { sqlite3_step $STMT } {SQLITE_MISUSE} - do_test capi3-11.11b { sqlite3_reset $STMT } {SQLITE_ABORT} -} do_test capi3c-11.11 { sqlite3_step $STMT } {SQLITE_DONE} -do_test capi3c-11.12 { - sqlite3_step $STMT - sqlite3_step $STMT -} {SQLITE_ROW} +ifcapable !autoreset { + do_test capi3c-11.12armor { + sqlite3_step $STMT + sqlite3_step $STMT + } {SQLITE_MISUSE} +} else { + do_test capi3c-11.12 { + sqlite3_step $STMT + sqlite3_step $STMT + } {SQLITE_ROW} +} do_test capi3c-11.13 { sqlite3_finalize $STMT } {SQLITE_OK} @@ -1270,6 +1269,7 @@ ifcapable progress { sqlite3_finalize $STMT } {SQLITE_INTERRUPT} do_test capi3c-21.4 { + db progress set STMT [sqlite3_prepare $DB {SELECT * FROM t3} -1 TAIL] db progress 5 "expr 1" sqlite3_step $STMT diff --git a/test/capi3d.test b/test/capi3d.test index 3b9b8375d1..7cad2870cd 100644 --- a/test/capi3d.test +++ b/test/capi3d.test @@ -100,8 +100,32 @@ proc test_is_readonly {testname sql truth} { sqlite3_finalize $STMT set rc } $sql] $truth + + # EVIDENCE-OF: R-61212-30018 If prepared statement X is an EXPLAIN or + # EXPLAIN QUERY PLAN statement, then sqlite3_stmt_readonly(X) returns + # the same value as if the EXPLAIN or EXPLAIN QUERY PLAN prefix were + # omitted. + # + do_test $testname.explain [format { + set DB [sqlite3_connection_pointer db] + set STMT [sqlite3_prepare $DB {EXPLAIN %s} -1 TAIL] + set rc [sqlite3_stmt_readonly $STMT] + sqlite3_finalize $STMT + set rc + } $sql] $truth + do_test $testname.eqp [format { + set DB [sqlite3_connection_pointer db] + set STMT [sqlite3_prepare $DB {EXPLAIN QUERY PLAN %s} -1 TAIL] + set rc [sqlite3_stmt_readonly $STMT] + sqlite3_finalize $STMT + set rc + } $sql] $truth } +# EVIDENCE-OF: R-23332-64992 The sqlite3_stmt_readonly(X) interface +# returns true (non-zero) if and only if the prepared statement X makes +# no direct changes to the content of the database file. +# test_is_readonly capi3d-2.1 {SELECT * FROM sqlite_master} 1 test_is_readonly capi3d-2.2 {CREATE TABLE t1(x)} 0 db eval {CREATE TABLE t1(x)} @@ -115,10 +139,92 @@ ifcapable wal { test_is_readonly capi3d-2.8 {PRAGMA application_id=1234} 0 test_is_readonly capi3d-2.9 {VACUUM} 0 test_is_readonly capi3d-2.10 {PRAGMA integrity_check} 1 -do_test capi3-2.99 { +do_test capi3-2.49 { sqlite3_stmt_readonly 0 } 1 + +# EVIDENCE-OF: R-04929-09147 This routine returns false if there is any +# possibility that the statement might change the database file. +# +# EVIDENCE-OF: R-13288-53765 A false return does not guarantee that the +# statement will change the database file. +# +# EVIDENCE-OF: R-22182-18548 For example, an UPDATE statement might have +# a WHERE clause that makes it a no-op, but the sqlite3_stmt_readonly() +# result would still be false. +# +# EVIDENCE-OF: R-50998-48593 Similarly, a CREATE TABLE IF NOT EXISTS +# statement is a read-only no-op if the table already exists, but +# sqlite3_stmt_readonly() still returns false for such a statement. +# +db eval { + CREATE TABLE t2(a,b,c); + INSERT INTO t2 VALUES(1,2,3); +} +test_is_readonly capi3d-2.11 {UPDATE t2 SET a=a+1 WHERE false} 0 +test_is_readonly capi3d-2.12 {CREATE TABLE IF NOT EXISTS t2(x,y)} 0 + + +# EVIDENCE-OF: R-37014-01401 The ATTACH and DETACH statements also cause +# sqlite3_stmt_readonly() to return true since, while those statements +# change the configuration of a database connection, they do not make +# changes to the content of the database files on disk. +# +test_is_readonly capi3d-2.13 {ATTACH ':memory:' AS mem1} 1 +db eval {ATTACH ':memory:' AS mem1} +test_is_readonly capi3d-2.14 {DETACH mem1} 1 +db eval {DETACH mem1} + +# EVIDENCE-OF: R-07474-04783 Transaction control statements such as +# BEGIN, COMMIT, ROLLBACK, SAVEPOINT, and RELEASE cause +# sqlite3_stmt_readonly() to return true, since the statements +# themselves do not actually modify the database but rather they control +# the timing of when other statements modify the database. +# +test_is_readonly capi3d-2.15 {BEGIN} 1 +test_is_readonly capi3d-2.16 {COMMIT} 1 +test_is_readonly capi3d-2.17 {SAVEPOINT one} 1 +test_is_readonly capi3d-2.18 {RELEASE one} 1 + +# EVIDENCE-OF: R-36961-63052 The sqlite3_stmt_readonly() interface +# returns true for BEGIN since BEGIN merely sets internal flags, but the +# BEGIN IMMEDIATE and BEGIN EXCLUSIVE commands do touch the database and +# so sqlite3_stmt_readonly() returns false for those commands. +# +test_is_readonly capi3d-2.19 {BEGIN IMMEDIATE} 0 +test_is_readonly capi3d-2.20 {BEGIN EXCLUSIVE} 0 + +# EVIDENCE-OF: R-21769-42523 For example, if an application defines a +# function "eval()" that calls sqlite3_exec(), then the following SQL +# statement would change the database file through side-effects: SELECT +# eval('DELETE FROM t1') FROM t2; But because the SELECT statement does +# not change the database file directly, sqlite3_stmt_readonly() would +# still return true. +# +proc evalsql {sql} {db eval $sql} +db func eval evalsql +test_is_readonly capi3d-2.21 {SELECT eval('DELETE FROM t1') FROM t2} 1 + +# Tests for the is-explain interface. +# +proc test_is_explain {testname sql truth} { + do_test $testname [format { + set DB [sqlite3_connection_pointer db] + set STMT [sqlite3_prepare $DB {%s} -1 TAIL] + set rc [sqlite3_stmt_isexplain $STMT] + sqlite3_finalize $STMT + set rc + } $sql] $truth +} + +test_is_explain capi3d-2.51 {SELECT * FROM sqlite_master} 0 +test_is_explain capi3d-2.52 { explain SELECT * FROM sqlite_master} 1 +test_is_explain capi3d-2.53 { Explain Query Plan select * FROM sqlite_master} 2 +do_test capi3-2.99 { + sqlite3_stmt_isexplain 0 +} 0 + # Tests for sqlite3_stmt_busy # do_test capi3d-3.1 { diff --git a/test/carray01.test b/test/carray01.test new file mode 100644 index 0000000000..86ea069961 --- /dev/null +++ b/test/carray01.test @@ -0,0 +1,159 @@ +# 2020-11-17 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# This file implements tests for CARRAY extension +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix carray01 + +ifcapable !vtab { + finish_test + return +} +#load_static_extension db carray + +# Parameter $stmt must be a prepared statement created using +# the sqlite3_prepare_v2 command and with parameters fully bound. +# This routine simply runs the statement, gathers the result, and +# returns a list containing the result. +# +# If the optional second argument is true, then the stmt is finalized +# after it is run. +# +proc run_stmt {stmt} { + set r {} + while {[sqlite3_step $stmt]=="SQLITE_ROW"} { + for {set i 0} {$i<[sqlite3_data_count $stmt]} {incr i} { + lappend r [sqlite3_column_text $stmt $i] + } + } + sqlite3_reset $stmt + return $r +} + +do_test 100 { + set STMT [sqlite3_prepare_v2 db {SELECT 5 IN carray(?3)} -1] + sqlite3_carray_bind $STMT 3 1 2 3 4 5 6 7 + run_stmt $STMT +} {1} +do_test 101 { + sqlite3_carray_bind -static $STMT 3 1 2 3 4 5 6 7 + run_stmt $STMT +} {1} +do_test 102 { + set STMT2 [sqlite3_prepare_v2 db { + SELECT DISTINCT typeof(value) FROM carray(?3)} -1] + sqlite3_carray_bind $STMT2 3 1 2 3 4 5 6 7 + run_stmt $STMT2 +} {integer} +do_test 110 { + sqlite3_carray_bind $STMT 3 1 2 3 4 6 7 + run_stmt $STMT +} {0} +do_test 120 { + sqlite3_carray_bind -int64 $STMT 3 1 2 3 4 5 6 7 + run_stmt $STMT +} {1} +do_test 120b { + sqlite3_carray_bind -int64 $STMT2 3 1 2 3 4 5 6 7 + run_stmt $STMT2 +} {integer} +do_test 121 { + sqlite3_carray_bind -int64 -transient $STMT 3 1 2 3 4 5 6 7 + run_stmt $STMT +} {1} +do_test 122 { + sqlite3_carray_bind -int64 -static $STMT 3 1 2 3 4 5 6 7 + run_stmt $STMT +} {1} +do_test 123 { + sqlite3_carray_bind -int32 -transient $STMT 3 1 2 3 4 5 6 7 + run_stmt $STMT +} {1} +do_test 124 { + sqlite3_carray_bind -int32 -static $STMT 3 1 2 3 4 5 6 7 + run_stmt $STMT +} {1} +do_test 125 { + sqlite3_carray_bind -int32 $STMT 3 1 2 3 4 5 6 7 + run_stmt $STMT +} {1} +do_test 130 { + sqlite3_carray_bind -int64 $STMT 3 1 2 3 4 6 7 + run_stmt $STMT +} {0} +do_test 131 { + sqlite3_carray_bind -int64 -transient $STMT 3 1 2 3 4 6 7 + run_stmt $STMT +} {0} +do_test 131 { + sqlite3_carray_bind -int64 -static $STMT 3 1 2 3 4 6 7 + run_stmt $STMT +} {0} +do_test 140 { + sqlite3_carray_bind -double $STMT 3 1 2 3 4 5 6 7 + run_stmt $STMT +} {1} +do_test 141 { + sqlite3_carray_bind -double -transient $STMT 3 1 2 3 4 5 6 7 + run_stmt $STMT +} {1} +do_test 142 { + sqlite3_carray_bind -double -static $STMT 3 1 2 3 4 5 6 7 + run_stmt $STMT +} {1} +do_test 143 { + sqlite3_carray_bind -double $STMT2 3 1 2 3 4 5 6 7 + run_stmt $STMT2 +} {real} +do_test 150 { + sqlite3_carray_bind -double $STMT 3 1 2 3 4 6 7 + run_stmt $STMT +} {0} +do_test 160 { + sqlite3_carray_bind -double $STMT 3 1 2 3 4 5 6 7 + run_stmt $STMT +} {1} +do_test 170 { + sqlite3_carray_bind -text -static $STMT 3 1 2 3 4 6 7 + run_stmt $STMT +} {0} +do_test 171 { + sqlite3_carray_bind -text -static $STMT2 3 1 2 3 4 6 7 + run_stmt $STMT2 +} {text} +do_test 180 { + sqlite3_carray_bind -text -transient $STMT 3 1 2 3 4 5 6 7 + run_stmt $STMT +} {0} +do_test 190 { + sqlite3_carray_bind -blob -static $STMT 3 1 2 3 4 5 6 7 + run_stmt $STMT +} {0} +do_test 191 { + sqlite3_carray_bind -blob -static $STMT2 3 1 2 3 4 5 6 7 + run_stmt $STMT2 +} {blob} +do_test 200 { + sqlite3_carray_bind -blob -transient $STMT 3 1 2 3 4 5 6 7 + run_stmt $STMT +} {0} +do_test 300 { + sqlite3_carray_bind $STMT 3 + run_stmt $STMT +} {0} + +sqlite3_finalize $STMT +sqlite3_finalize $STMT2 + +finish_test diff --git a/test/carray02.test b/test/carray02.test new file mode 100644 index 0000000000..c75ca42719 --- /dev/null +++ b/test/carray02.test @@ -0,0 +1,162 @@ +# 2025-10-07 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# This file implements tests for CARRAY extension +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix carray02 + +ifcapable !vtab { + finish_test + return +} + +proc run_stmt {stmt} { + set r {} + while {[sqlite3_step $stmt]=="SQLITE_ROW"} { + for {set i 0} {$i<[sqlite3_data_count $stmt]} {incr i} { + lappend r [sqlite3_column_text $stmt $i] + } + } + sqlite3_reset $stmt + return $r +} + +# Bind some text values using sqlite3_carray_bind(). Including a NULL pointer. +# +set STMT [ + sqlite3_prepare_v2 db "SELECT value, value IS NULL FROM carray(?)" -1 TAIL +] +do_test 1.0 { + sqlite3_carray_bind -transient -text $STMT 1 "abc" NULL "def" + run_stmt $STMT +} {abc 0 {} 1 def 0} +sqlite3_finalize $STMT + +# Pass bad values as the "flags" parameter to sqlite3_carray_bind(). +# +set STMT [sqlite3_prepare_v2 db "SELECT value FROM carray(?)" -1 TAIL] +do_test 2.0 { + list [catch {sqlite3_carray_bind -flags 5 -int32 $STMT 1 1 2 3 4} msg] $msg +} {1 {SQL logic error}} +do_test 2.1 { + list [catch {sqlite3_carray_bind -flags -1 -int32 $STMT 1 1 2 3 4} msg] $msg +} {1 {SQL logic error}} +sqlite3_finalize $STMT + +# In each of the following, carray(?) contains integer values 1 to 5, bound +# using sqlite3_carray_bind(). +# +foreach {tn sql res} { + 1 { SELECT value FROM carray(?) WHERE value>2 } {3 4 5} + 2 { + WITH s(i) AS ( VALUES(1) UNION ALL VALUES(2) ) + SELECT i, value FROM s, carray(?) WHERE i=value; + } {1 1 2 2} +} { + do_test 2.2.$tn { + set STMT [sqlite3_prepare_v2 db $sql -1 TAIL] + sqlite3_carray_bind -int32 $STMT 1 1 2 3 4 5 + set r [run_stmt $STMT] + sqlite3_finalize $STMT + set r + } $res +} + +# In each of the following, ? is bound to an int array containing 1 to 5. +# Bound using C code like: +# +# sqlite3_bind_pointer(pStmt, 1, aInt, "carray", SQLITE_TRANSIENT) +# +foreach {tn sql res} { + 1 { SELECT value FROM carray(?, 5) } {1 2 3 4 5} + 2 { SELECT value FROM carray(?, 3, 'int32') } {1 2 3} + 3 { SELECT value, pointer, count, ctype FROM carray(?, 5, 'int32') } + {1 {} 5 int32 2 {} 5 int32 3 {} 5 int32 4 {} 5 int32 5 {} 5 int32} + 4 { SELECT rowid, value FROM carray(?, 5, 'int32') } + {1 1 2 2 3 3 4 4 5 5} +} { + do_test 2.3.$tn { + set STMT [sqlite3_prepare_v2 db $sql -1 TAIL] + bind_carray_intptr $STMT 1 1 2 3 4 5 + set r [run_stmt $STMT] + sqlite3_finalize $STMT + set r + } $res +} + +# In each of the following. Both carray(?1) and carray(?2) contain integer +# values 1 to 5. Bound by sqlite3_carray_bind(). +# +foreach {tn sql res} { + 1 { + SELECT * FROM carray(?1) AS a, carray(?2) AS b + WHERE a.value=b.value + } {1 1 2 2 3 3 4 4 5 5} + + 2 { + SELECT * FROM carray(?1) AS a, carray(?2) AS b + WHERE a.value=b.value AND a.value<3 AND b.value<3 + } {1 1 2 2 3 3} + + 3 { + SELECT * FROM carray(?1) AS a, carray(?2) AS b + WHERE a.value<3 AND b.value<3 AND a.value=b.value + } {1 1 2 2 3 3} + + 4 { + SELECT * FROM carray(?1) AS a, carray(?2, a.value) AS b + WHERE a.value=b.value + } {1 1 2 2 3 3} + +} { + do_test 2.4.$tn { + set STMT [sqlite3_prepare_v2 db { + SELECT * FROM carray(?1) AS a, carray(?2) AS b WHERE a.value=b.value + } -1 TAIL] + sqlite3_carray_bind $STMT 1 1 2 3 4 5 + sqlite3_carray_bind $STMT 2 1 2 3 4 5 + set r [run_stmt $STMT] + sqlite3_finalize $STMT + set r + } {1 1 2 2 3 3 4 4 5 5} +} + +# Test that not binding any pointer, or passing a value that is not a bound +# pointer to carray() produces no rows of output. +# +do_execsql_test 3.0.0 { + SELECT * FROM carray +} {} +do_execsql_test 3.0.1 { + SELECT * FROM carray('0xFFFF', 5) +} {} +do_execsql_test 3.0.2 { + SELECT * FROM carray('0xFFFF') +} {} +do_execsql_test 3.0.3 { + CREATE TABLE t1(x); + INSERT INTO t1 VALUES('0xFFFF'); + SELECT * FROM t1, carray WHERE carray.pointer = t1.x; +} {} + +# Test passing an unknown datatype. +# +do_test 3.1 { + set STMT [sqlite3_prepare_v2 db {SELECT * FROM carray(?, 5, 'apples')} -1 T] + sqlite3_carray_bind $STMT 1 1 2 3 4 5 + sqlite3_step $STMT + list [sqlite3_finalize $STMT] [sqlite3_errmsg db] +} {SQLITE_ERROR {unknown datatype: 'apples'}} + +finish_test diff --git a/test/carrayfault.test b/test/carrayfault.test new file mode 100644 index 0000000000..0dd2cd21b7 --- /dev/null +++ b/test/carrayfault.test @@ -0,0 +1,87 @@ +# 2025-10-07 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix carrayfault + +ifcapable {!carray||!vtab} { + finish_test; return +} + +sqlite3 db test.db + +do_execsql_test 2.0 { + CREATE TABLE t1(a); +} + +set STMT [sqlite3_prepare_v2 db "SELECT value FROM carray(?)" -1 TAIL] + +# Test OOM faults in sqlite3_carray_bind() when binding an integer +# array. +# +foreach {tn mem} { + 1 -static + 2 -transient + 3 -malloc +} { + do_faultsim_test 2.$tn -faults oom* -prep { + } -body { + sqlite3_carray_bind $::mem -int64 $::STMT 1 100 200 300 400 + } -test { + faultsim_test_result {0 {}} {1 {out of memory}} + } +} + +# Test OOM faults in sqlite3_carray_bind() when binding a text array. +# +do_faultsim_test 3 -faults oom* -prep { +} -body { + sqlite3_carray_bind -transient -text $::STMT 1 "hello" "world" +} -test { + faultsim_test_result {0 {}} {1 {initialization of carray failed: }} +} + +sqlite3_finalize $STMT + + +# Test OOM faults when running queries against carray. +# +do_faultsim_test 4 -faults oom* -prep { + set ::STMT [sqlite3_prepare_v2 db "SELECT value FROM carray(?)" -1 TAIL] + sqlite3_carray_bind -transient -text $::STMT 1 "hello" "world" +} -body { + set myres [list] + while { "SQLITE_ROW"==[sqlite3_step $::STMT] } { + lappend myres [sqlite3_column_text $::STMT 1] + } + sqlite3_reset $::STMT +} -test { + faultsim_test_result {0 SQLITE_OK} {0 SQLITE_NOMEM} + sqlite3_finalize $::STMT +} + +# Test OOM faults when preparing queries against carray. +# +do_faultsim_test 5 -faults oom* -prep { + sqlite3 db test.db +} -body { + execsql "SELECT value FROM carray(?)" +} -test { + faultsim_test_result {0 {}} +} + + + +sqlite3_carray_bind + +finish_test diff --git a/test/cast.test b/test/cast.test index f47f4bb2bf..c1b564328a 100644 --- a/test/cast.test +++ b/test/cast.test @@ -183,34 +183,34 @@ do_test cast-1.53 { execsql {SELECT CAST('123.5abc' AS integer)} } 123 -do_test case-1.60 { +do_test cast-1.60 { execsql {SELECT CAST(null AS REAL)} } {{}} -do_test case-1.61 { +do_test cast-1.61 { execsql {SELECT typeof(CAST(null AS REAL))} } {null} -do_test case-1.62 { +do_test cast-1.62 { execsql {SELECT CAST(1 AS REAL)} } {1.0} -do_test case-1.63 { +do_test cast-1.63 { execsql {SELECT typeof(CAST(1 AS REAL))} } {real} -do_test case-1.64 { +do_test cast-1.64 { execsql {SELECT CAST('1' AS REAL)} } {1.0} -do_test case-1.65 { +do_test cast-1.65 { execsql {SELECT typeof(CAST('1' AS REAL))} } {real} -do_test case-1.66 { +do_test cast-1.66 { execsql {SELECT CAST('abc' AS REAL)} } {0.0} -do_test case-1.67 { +do_test cast-1.67 { execsql {SELECT typeof(CAST('abc' AS REAL))} } {real} -do_test case-1.68 { +do_test cast-1.68 { execsql {SELECT CAST(x'31' AS REAL)} } {1.0} -do_test case-1.69 { +do_test cast-1.69 { execsql {SELECT typeof(CAST(x'31' AS REAL))} } {real} @@ -234,6 +234,7 @@ do_test cast-3.1 { do_test cast-3.2 { execsql {SELECT CAST(9223372036854774800 AS numeric)} } 9223372036854774800 +breakpoint do_realnum_test cast-3.3 { execsql {SELECT CAST(9223372036854774800 AS real)} } 9.22337203685477e+18 @@ -261,11 +262,9 @@ do_test cast-3.12 { do_realnum_test cast-3.13 { execsql {SELECT CAST('9223372036854774800' AS real)} } 9.22337203685477e+18 -ifcapable long_double { - do_test cast-3.14 { - execsql {SELECT CAST(CAST('9223372036854774800' AS real) AS integer)} - } 9223372036854774784 -} +do_test cast-3.14 { + execsql {SELECT CAST(CAST('9223372036854774800' AS real) AS integer)} +} 9223372036854774784 do_test cast-3.15 { execsql {SELECT CAST('-9223372036854774800' AS integer)} } -9223372036854774800 @@ -275,11 +274,9 @@ do_test cast-3.16 { do_realnum_test cast-3.17 { execsql {SELECT CAST('-9223372036854774800' AS real)} } -9.22337203685477e+18 -ifcapable long_double { - do_test cast-3.18 { - execsql {SELECT CAST(CAST('-9223372036854774800' AS real) AS integer)} - } -9223372036854774784 -} +do_test cast-3.18 { + execsql {SELECT CAST(CAST('-9223372036854774800' AS real) AS integer)} +} -9223372036854774784 if {[db eval {PRAGMA encoding}]=="UTF-8"} { do_test cast-3.21 { execsql {SELECT CAST(x'39323233333732303336383534373734383030' AS integer)} @@ -290,16 +287,14 @@ if {[db eval {PRAGMA encoding}]=="UTF-8"} { do_realnum_test cast-3.23 { execsql {SELECT CAST(x'39323233333732303336383534373734383030' AS real)} } 9.22337203685477e+18 - ifcapable long_double { - do_test cast-3.24 { - execsql { - SELECT CAST(CAST(x'39323233333732303336383534373734383030' AS real) - AS integer) - } - } 9223372036854774784 - } + do_test cast-3.24 { + execsql { + SELECT CAST(CAST(x'39323233333732303336383534373734383030' AS real) + AS integer) + } + } 9223372036854774784 } -do_test case-3.31 { +do_test cast-3.31 { execsql {SELECT CAST(NULL AS numeric)} } {{}} @@ -343,4 +338,218 @@ do_test cast-4.4 { } } {0 abc 0.0 abc} +# Added 2018-01-26 +# +# EVIDENCE-OF: R-48741-32454 If the prefix integer is greater than +# +9223372036854775807 then the result of the cast is exactly +# +9223372036854775807. +do_execsql_test cast-5.1 { + SELECT CAST('9223372036854775808' AS integer); + SELECT CAST(' +000009223372036854775808' AS integer); + SELECT CAST('12345678901234567890123' AS INTEGER); +} {9223372036854775807 9223372036854775807 9223372036854775807} + +# EVIDENCE-OF: R-06028-16857 Similarly, if the prefix integer is less +# than -9223372036854775808 then the result of the cast is exactly +# -9223372036854775808. +do_execsql_test cast-5.2 { + SELECT CAST('-9223372036854775808' AS integer); + SELECT CAST('-9223372036854775809' AS integer); + SELECT CAST('-12345678901234567890123' AS INTEGER); +} {-9223372036854775808 -9223372036854775808 -9223372036854775808} + +# EVIDENCE-OF: R-33990-33527 When casting to INTEGER, if the text looks +# like a floating point value with an exponent, the exponent will be +# ignored because it is no part of the integer prefix. +# EVIDENCE-OF: R-24225-46995 For example, "(CAST '123e+5' AS INTEGER)" +# results in 123, not in 12300000. +do_execsql_test cast-5.3 { + SELECT CAST('123e+5' AS INTEGER); + SELECT CAST('123e+5' AS NUMERIC); + SELECT CAST('123e+5' AS REAL); +} {123 12300000 12300000.0} + + +# The following does not have anything to do with the CAST operator, +# but it does deal with affinity transformations. +# +do_execsql_test cast-6.1 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a NUMERIC); + INSERT INTO t1 VALUES + ('9000000000000000001'), + ('9000000000000000001 '), + (' 9000000000000000001'), + (' 9000000000000000001 '); + SELECT * FROM t1; +} {9000000000000000001 9000000000000000001 9000000000000000001 9000000000000000001} + +# 2019-06-07 +# https://sqlite.org/src/info/4c2d7639f076aa7c +do_execsql_test cast-7.1 { + SELECT CAST('-' AS NUMERIC); +} {0} +do_execsql_test cast-7.2 { + SELECT CAST('-0' AS NUMERIC); +} {0} +do_execsql_test cast-7.3 { + SELECT CAST('+' AS NUMERIC); +} {0} +do_execsql_test cast-7.4 { + SELECT CAST('/' AS NUMERIC); +} {0} + +# 2019-06-07 +# https://sqlite.org/src/info/e8bedb2a184001bb +do_execsql_test cast-7.10 { + SELECT '' - 2851427734582196970; +} {-2851427734582196970} +do_execsql_test cast-7.11 { + SELECT 0 - 2851427734582196970; +} {-2851427734582196970} +do_execsql_test cast-7.12 { + SELECT '' - 1; +} {-1} + +# 2019-06-10 +# https://sqlite.org/src/info/dd6bffbfb6e61db9 +# +# EVIDENCE-OF: R-55084-10555 Casting a TEXT or BLOB value into NUMERIC +# yields either an INTEGER or a REAL result. +# +do_execsql_test cast-7.20 { + DROP TABLE IF EXISTS t0; + CREATE TABLE t0 (c0 TEXT); + INSERT INTO t0(c0) VALUES ('1.0'); + SELECT CAST(c0 AS NUMERIC) FROM t0; +} {1} + +# 2019-06-10 +# https://sqlite.org/src/info/27de823723a41df45af3 +# +do_execsql_test cast-7.30 { + SELECT -'.'; +} 0 +do_execsql_test cast-7.31 { + SELECT '.'+0; +} 0 +do_execsql_test cast-7.32 { + SELECT CAST('.' AS numeric); +} 0 +do_execsql_test cast-7.33 { + SELECT -CAST('.' AS numeric); +} 0 + +# 2019-06-12 +# https://sqlite.org/src/info/674385aeba91c774 +# +do_execsql_test cast-7.40 { + SELECT CAST('-0.0' AS numeric); +} 0 +do_execsql_test cast-7.41 { + SELECT CAST('0.0' AS numeric); +} 0 +do_execsql_test cast-7.42 { + SELECT CAST('+0.0' AS numeric); +} 0 +do_execsql_test cast-7.43 { + SELECT CAST('-1.0' AS numeric); +} -1 + +ifcapable utf16 { + reset_db + execsql { PRAGMA encoding='utf16' } + + do_execsql_test cast-8.1 { + SELECT quote(X'310032003300')==quote(substr(X'310032003300', 1)) + } 1 + do_execsql_test cast-8.2 { + SELECT CAST(X'310032003300' AS TEXT) + ==CAST(substr(X'310032003300', 1) AS TEXT) + } 1 +} + +reset_db +do_execsql_test cast-9.0 { + CREATE TABLE t0(c0); + INSERT INTO t0(c0) VALUES (0); + CREATE VIEW v1(c0, c1) AS + SELECT CAST(0.0 AS NUMERIC), COUNT(*) OVER () FROM t0; + SELECT v1.c0 FROM v1, t0 WHERE v1.c0=0; +} {0.0} + +# Set the 2022-12-10 "reopen" of ticket [https://sqlite.org/src/tktview/57c47526c3] +# +do_execsql_test cast-9.1 { + CREATE TABLE dual(dummy TEXT); + INSERT INTO dual VALUES('X'); + SELECT CAST(4 AS NUMERIC); +} {4} +do_execsql_test cast-9.2 { + SELECT CAST(4.0 AS NUMERIC); +} {4.0} +do_execsql_test cast-9.3 { + SELECT CAST(4.5 AS NUMERIC); +} {4.5} +do_execsql_test cast-9.4 { + SELECT x, typeof(x) FROM (SELECT CAST(4 AS NUMERIC) AS x) JOIN dual; +} {4 integer} +do_execsql_test cast-9.5 { + SELECT x, typeof(x) FROM dual CROSS JOIN (SELECT CAST(4 AS NUMERIC) AS x); +} {4 integer} +do_execsql_test cast-9.10 { + SELECT x, typeof(x) FROM (SELECT CAST(4.0 AS NUMERIC) AS x) JOIN dual; +} {4.0 real} +do_execsql_test cast-9.11 { + SELECT x, typeof(x) FROM dual CROSS JOIN (SELECT CAST(4.0 AS NUMERIC) AS x); +} {4.0 real} +do_execsql_test cast-9.12 { + SELECT x, typeof(x) FROM (SELECT CAST(4.5 AS NUMERIC) AS x) JOIN dual; +} {4.5 real} +do_execsql_test cast-9.13 { + SELECT x, typeof(x) FROM dual CROSS JOIN (SELECT CAST(4.5 AS NUMERIC) AS x); +} {4.5 real} + +# 2022-12-15 dbsqlfuzz c9ee6f9a0a8b8fefb02cf69de2a8b67ca39525c8 +# +# Added a new SQLITE_AFF_FLEXNUM that does not try to convert int to real or +# real to int. +# +do_execsql_test cast-10.1 { + VALUES(CAST(44 AS REAL)),(55); +} {44.0 55} +do_execsql_test cast-10.2 { + SELECT CAST(44 AS REAL) AS 'm' UNION ALL SELECT 55; +} {44.0 55} +do_execsql_test cast-10.3 { + SELECT * FROM (VALUES(CAST(44 AS REAL)),(55)); +} {44.0 55} +do_execsql_test cast-10.4 { + SELECT * FROM (SELECT CAST(44 AS REAL) AS 'm' UNION ALL SELECT 55); +} {44.0 55} +do_execsql_test cast-10.5 { + SELECT * FROM dual CROSS JOIN (VALUES(CAST(44 AS REAL)),(55)); +} {X 44.0 X 55} +do_execsql_test cast-10.6 { + SELECT * FROM dual CROSS JOIN (SELECT CAST(44 AS REAL) AS 'm' + UNION ALL SELECT 55); +} {X 44.0 X 55} +ifcapable vtab { + do_execsql_test cast-10.7 { + DROP VIEW v1; + CREATE VIEW v1 AS SELECT CAST(44 AS REAL) AS 'm' UNION ALL SELECT 55; + SELECT name, type FROM pragma_table_info('v1'); + } {m NUM} + do_execsql_test cast-10.8 { + CREATE VIEW v2 AS VALUES(CAST(44 AS REAL)),(55); + SELECT type FROM pragma_table_info('v2'); + } {NUM} + do_execsql_test cast-10.9 { + SELECT * FROM v1; + } {44.0 55} + do_execsql_test cast-10.10 { + SELECT * FROM v2; + } {44.0 55} +} + finish_test diff --git a/test/cffault.test b/test/cffault.test index 79cefd24c4..0d029ece37 100644 --- a/test/cffault.test +++ b/test/cffault.test @@ -15,7 +15,7 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl -set testprefix cacheflush +set testprefix cffault source $testdir/malloc_common.tcl # Run the supplied SQL on a copy of the database currently stored on diff --git a/test/changes.test b/test/changes.test new file mode 100644 index 0000000000..b3a2ae1eef --- /dev/null +++ b/test/changes.test @@ -0,0 +1,88 @@ +# 2021 June 22 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# Tests for the sqlite3_changes() and sqlite3_total_changes() APIs. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix changes + +# To test that the change-counters do not suffer from 32-bit signed integer +# rollover, add the following line to the array of test cases below. The +# test will take will over an hour to run. +# +# 7 (1<<31)+10 "" +# + +foreach {tn nRow wor} { + 1 50 "" + 2 50 "WITHOUT ROWID" + + 3 5000 "" + 4 5000 "WITHOUT ROWID" + + 5 50000 "" + 6 50000 "WITHOUT ROWID" +} { + reset_db + set nBig [expr $nRow] + + do_execsql_test 1.$tn.0 " + PRAGMA journal_mode = off; + CREATE TABLE t1(x INTEGER PRIMARY KEY) $wor; + " {off} + + do_execsql_test 1.$tn.1 { + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i < $nBig + ) + INSERT INTO t1 SELECT i FROM s; + } + + do_test 1.$tn.2 { + db changes + } [expr $nBig] + + do_test 1.$tn.3 { + db total_changes + } [expr $nBig] + + do_execsql_test 1.$tn.4 { + INSERT INTO t1 VALUES(-1) + } + + do_test 1.$tn.5 { + db changes + } [expr 1] + + do_test 1.$tn.6 { + db total_changes + } [expr {$nBig+1}] + + do_execsql_test 1.$tn.7a { + SELECT count(*) FROM t1 + } [expr {$nBig+1}] + + do_execsql_test 1.$tn.7 { + DELETE FROM t1 + } + + do_test 1.$tn.8 { + db changes + } [expr {$nBig+1}] + + do_test 1.$tn.9 { + db total_changes + } [expr {2*($nBig+1)}] +} + +finish_test diff --git a/test/changes2.test b/test/changes2.test new file mode 100644 index 0000000000..5b2684a8df --- /dev/null +++ b/test/changes2.test @@ -0,0 +1,94 @@ +# 2022 June 6 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix changes2 + +do_execsql_test 1.0 { + CREATE TABLE some_table ( + id INTEGER NOT NULL, value VARCHAR(40) NOT NULL, PRIMARY KEY (id) + ); + INSERT INTO some_table (id, value) VALUES (1, 'v1'); +} {} + +set ::stmt [sqlite3_prepare_v2 db { + UPDATE some_table SET value='v2' WHERE id=1 RETURNING id +} -1 dummy] + +do_test 1.1 { + list [sqlite3_step $::stmt] [db changes] +} {SQLITE_ROW 1} + +do_test 1.2 { + list [sqlite3_step $::stmt] [db changes] +} {SQLITE_DONE 1} + +sqlite3_reset $::stmt + +do_execsql_test 1.2 { + DROP TABLE some_table; + CREATE TABLE some_table ( + id INTEGER NOT NULL, value VARCHAR(40) NOT NULL, PRIMARY KEY (id) + ); + INSERT INTO some_table (id, value) VALUES (1, 'v1'); +} {} + +do_test 1.3 { + list [sqlite3_step $::stmt] [db changes] +} {SQLITE_ROW 1} + +do_test 1.4 { + list [sqlite3_step $::stmt] [db changes] +} {SQLITE_DONE 1} + +sqlite3_finalize $::stmt + +#------------------------------------------------------------------------- +reset_db + +do_execsql_test 2.0 { + CREATE TABLE t1(a, b); + CREATE TABLE log(t); +} {} + +set ::stmt [sqlite3_prepare_v2 db { + INSERT INTO log VALUES(changes() || ' changes') +} -1 dummy] + +do_execsql_test 2.1 { + INSERT INTO t1 VALUES (1, 'v1'), (2, 'v2'); +} {} + +do_test 2.2 { + list [sqlite3_step $::stmt] [sqlite3_reset $::stmt] +} {SQLITE_DONE SQLITE_OK} + +do_execsql_test 2.3 { + CREATE TABLE t3(x); +} + +do_execsql_test 2.2 { + INSERT INTO t1 VALUES (3, 'v1'), (4, 'v2'); +} {} + +do_test 2.3 { + list [sqlite3_step $::stmt] [sqlite3_reset $::stmt] +} {SQLITE_DONE SQLITE_OK} + +sqlite3_finalize $::stmt + +do_execsql_test 2.4 { + SELECT * FROM log; +} {{2 changes} {2 changes}} + +finish_test diff --git a/test/check.test b/test/check.test index 43e447f70d..c3beb2f5d8 100644 --- a/test/check.test +++ b/test/check.test @@ -11,7 +11,6 @@ # This file implements regression tests for SQLite library. The # focus of this file is testing CHECK constraints # -# $Id: check.test,v 1.13 2009/06/05 17:09:12 drh Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl @@ -22,6 +21,8 @@ ifcapable !check { finish_test return } +sqlite3_db_config db SQLITE_DBCONFIG_DQS_DDL 1 +sqlite3_db_config db SQLITE_DBCONFIG_DQS_DML 1 do_test check-1.1 { execsql { @@ -41,7 +42,7 @@ do_test check-1.3 { catchsql { INSERT INTO t1 VALUES(6,7); } -} {1 {CHECK constraint failed: t1}} +} {1 {CHECK constraint failed: x<5}} do_test check-1.4 { execsql { SELECT * FROM t1; @@ -51,7 +52,7 @@ do_test check-1.5 { catchsql { INSERT INTO t1 VALUES(4,3); } -} {1 {CHECK constraint failed: t1}} +} {1 {CHECK constraint failed: y>x}} do_test check-1.6 { execsql { SELECT * FROM t1; @@ -88,7 +89,7 @@ do_test check-1.12 { catchsql { UPDATE t1 SET x=7 WHERE x==2 } -} {1 {CHECK constraint failed: t1}} +} {1 {CHECK constraint failed: x<5}} do_test check-1.13 { execsql { SELECT * FROM t1; @@ -98,7 +99,7 @@ do_test check-1.14 { catchsql { UPDATE t1 SET x=5 WHERE x==2 } -} {1 {CHECK constraint failed: t1}} +} {1 {CHECK constraint failed: x<5}} do_test check-1.15 { execsql { SELECT * FROM t1; @@ -117,11 +118,18 @@ do_test check-1.17 { do_test check-2.1 { execsql { + PRAGMA writable_schema = 1; CREATE TABLE t2( x INTEGER CONSTRAINT one CHECK( typeof(coalesce(x,0))=="integer" ), y REAL CONSTRAINT two CHECK( typeof(coalesce(y,0.1))=='real' ), z TEXT CONSTRAINT three CHECK( typeof(coalesce(z,''))=='text' ) ); + CREATE TABLE t2n( + x INTEGER CONSTRAINT one CHECK( typeof(coalesce(x,0))=="integer" ), + y NUMERIC CONSTRAINT two CHECK( typeof(coalesce(y,0.1))=='real' ), + z TEXT CONSTRAINT three CHECK( typeof(coalesce(z,''))=='text' ) + ); + PRAGMA writable_schema = 0; } } {} do_test check-2.2 { @@ -132,6 +140,8 @@ do_test check-2.2 { } {1 2.2 three} db close sqlite3 db test.db +sqlite3_db_config db SQLITE_DBCONFIG_DQS_DDL 1 +sqlite3_db_config db SQLITE_DBCONFIG_DQS_DML 1 do_test check-2.3 { execsql { INSERT INTO t2 VALUES(NULL, NULL, NULL); @@ -144,15 +154,23 @@ do_test check-2.4 { } } {1 {CHECK constraint failed: one}} do_test check-2.5 { + # The 5 gets automatically promoted to 5.0 because the column type is REAL catchsql { INSERT INTO t2 VALUES(NULL, 5, NULL); } +} {0 {}} +do_test check-2.5b { + # This time the column type is NUMERIC, so not automatic promption to REAL + # occurs and the constraint fails. + catchsql { + INSERT INTO t2n VALUES(NULL, 5, NULL); + } } {1 {CHECK constraint failed: two}} do_test check-2.6 { catchsql { INSERT INTO t2 VALUES(NULL, NULL, 3.14159); } -} {1 {CHECK constraint failed: three}} +} {0 {}} # Undocumented behavior: The CONSTRAINT name clause can follow a constraint. # Such a clause is ignored. But the parser must accept it for backwards @@ -172,7 +190,7 @@ do_test check-2.11 { catchsql { INSERT INTO t2b VALUES('xyzzy','hi',5); } -} {1 {CHECK constraint failed: t2b}} +} {1 {CHECK constraint failed: typeof(coalesce(x,0))=='integer'}} do_test check-2.12 { execsql { CREATE TABLE t2c( @@ -193,6 +211,7 @@ do_test check-2.cleanup { execsql { DROP TABLE IF EXISTS t2b; DROP TABLE IF EXISTS t2c; + DROP TABLE IF EXISTS t2n; } } {} @@ -256,7 +275,7 @@ do_test check-3.9 { catchsql { INSERT INTO t3 VALUES(111,222,333); } -} {1 {CHECK constraint failed: t3}} +} {1 {CHECK constraint failed: t3.x<25}} do_test check-4.1 { execsql { @@ -298,7 +317,10 @@ do_test check-4.6 { catchsql { UPDATE t4 SET x=0, y=1; } -} {1 {CHECK constraint failed: t4}} +} {1 {CHECK constraint failed: x+y==11 + OR x*y==12 + OR x/y BETWEEN 5 AND 8 + OR -x==y+10}} do_test check-4.7 { execsql { SELECT * FROM t4; @@ -309,14 +331,21 @@ do_test check-4.8 { PRAGMA ignore_check_constraints=ON; UPDATE t4 SET x=0, y=1; SELECT * FROM t4; + PRAGMA integrity_check; } -} {0 1} +} {0 1 ok} +do_execsql_test check-4.8.1 { + PRAGMA ignore_check_constraints=OFF; + PRAGMA integrity_check; +} {{CHECK constraint failed in t4}} do_test check-4.9 { catchsql { - PRAGMA ignore_check_constraints=OFF; UPDATE t4 SET x=0, y=2; } -} {1 {CHECK constraint failed: t4}} +} {1 {CHECK constraint failed: x+y==11 + OR x*y==12 + OR x/y BETWEEN 5 AND 8 + OR -x==y+10}} ifcapable vacuum { do_test check_4.10 { catchsql { @@ -367,7 +396,7 @@ do_test check-6.5 { catchsql { UPDATE OR FAIL t1 SET x=7-x, y=y+1; } -} {1 {CHECK constraint failed: t1}} +} {1 {CHECK constraint failed: x<5}} do_test check-6.6 { execsql { SELECT * FROM t1; @@ -379,7 +408,7 @@ do_test check-6.7 { INSERT INTO t1 VALUES(1,30.0); INSERT OR ROLLBACK INTO t1 VALUES(8,40.0); } -} {1 {CHECK constraint failed: t1}} +} {1 {CHECK constraint failed: x<5}} do_test check-6.8 { catchsql { COMMIT; @@ -398,7 +427,7 @@ do_test check-6.12 { catchsql { REPLACE INTO t1 VALUES(6,7); } -} {1 {CHECK constraint failed: t1}} +} {1 {CHECK constraint failed: x<5}} do_test check-6.13 { execsql {SELECT * FROM t1} } {3 12.0 2 20.0} @@ -422,12 +451,12 @@ do_test check-6.15 { # reset_db proc myfunc {x} {expr $x < 10} -db func myfunc myfunc +db func myfunc -deterministic myfunc do_execsql_test 7.1 { CREATE TABLE t6(a CHECK (myfunc(a))) } do_execsql_test 7.2 { INSERT INTO t6 VALUES(9) } do_catchsql_test 7.3 { INSERT INTO t6 VALUES(11) } \ - {1 {CHECK constraint failed: t6}} + {1 {CHECK constraint failed: myfunc(a)}} do_test 7.4 { sqlite3 db2 test.db @@ -450,7 +479,7 @@ do_test 7.7 { do_test 7.8 { db2 func myfunc myfunc catchsql { INSERT INTO t6 VALUES(12) } db2 -} {1 {CHECK constraint failed: t6}} +} {1 {CHECK constraint failed: myfunc(a)}} # 2013-08-02: Silently ignore database name qualifiers in CHECK constraints. # @@ -477,6 +506,142 @@ do_catchsql_test 9.3 { UPDATE t1 SET c=a*2 WHERE a=1; } {1 {CHECK constraint failed: c-check}} +# Integrity check on a VIEW with columns. +# +db close +db2 close +forcedelete test.db +sqlite3 db test.db +do_execsql_test 10.1 { + CREATE TABLE t1(x); + CREATE VIEW v1(y) AS SELECT x FROM t1; + PRAGMA integrity_check; +} {ok} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 11.0 { + CREATE TABLE t1 (Col0 CHECK(1 COLLATE BINARY BETWEEN 1 AND 1) ) ; +} +do_execsql_test 11.1 { + INSERT INTO t1 VALUES (NULL); +} +do_execsql_test 11.2 { + INSERT INTO t1 VALUES (NULL); +} + +do_execsql_test 11.3 { + CREATE TABLE t2(b, a CHECK( + CASE 'abc' COLLATE nocase WHEN a THEN 1 ELSE 0 END) + ); +} +do_execsql_test 11.4 { + INSERT INTO t2(a) VALUES('abc'); +} +do_execsql_test 11.5 { + INSERT INTO t2(b, a) VALUES(1, 'abc'||''); +} +do_execsql_test 11.6 { + INSERT INTO t2(b, a) VALUES(2, 'abc'); +} +# 2019-12-24 ticket b383b90278186263 +# +reset_db +do_execsql_test 12.10 { + CREATE TABLE t1(a TEXT, CHECK(a=+a)); + INSERT INTO t1(a) VALUES(NULL),('xyz'),(5),(x'303132'),(4.75); + SELECT quote(a) FROM t1 ORDER BY rowid; +} {NULL 'xyz' '5' X'303132' '4.75'} +do_execsql_test 12.20 { + DROP TABLE t1; + CREATE TABLE t1(a TEXT, CHECK(a<>+a)); + INSERT INTO t1(a) VALUES(NULL); +} {} +do_catchsql_test 12.21 { + INSERT INTO t1(a) VALUES('xyz'); +} {1 {CHECK constraint failed: a<>+a}} +do_catchsql_test 12.22 { + INSERT INTO t1(a) VALUES(123); +} {1 {CHECK constraint failed: a<>+a}} +do_execsql_test 12.30 { + DROP TABLE t1; + CREATE TABLE t1(a TEXT, CHECK(NOT(a=+a))); + INSERT INTO t1(a) VALUES(NULL); +} {} +do_catchsql_test 12.31 { + INSERT INTO t1(a) VALUES('xyz'); +} {1 {CHECK constraint failed: NOT(a=+a)}} +do_catchsql_test 12.32 { + INSERT INTO t1(a) VALUES(123); +} {1 {CHECK constraint failed: NOT(a=+a)}} +do_execsql_test 12.40 { + DROP TABLE t1; + CREATE TABLE t1(a TEXT, CHECK(NOT(a<>+a))); + INSERT INTO t1(a) VALUES(NULL),('xyz'),(5),(x'303132'),(4.75); + SELECT quote(a) FROM t1 ORDER BY rowid; +} {NULL 'xyz' '5' X'303132' '4.75'} +do_execsql_test 12.50 { + DROP TABLE t1; + CREATE TABLE t1(a TEXT, CHECK(a BETWEEN 0 AND +a)); + INSERT INTO t1(a) VALUES(NULL),('xyz'),(5),(x'303132'),(4.75); + SELECT quote(a) FROM t1 ORDER BY rowid; +} {NULL 'xyz' '5' X'303132' '4.75'} +do_execsql_test 12.60 { + DROP TABLE t1; + CREATE TABLE t1(a TEXT, CHECK(a NOT BETWEEN 0 AND +a)); + INSERT INTO t1(a) VALUES(NULL); + SELECT quote(a) FROM t1 ORDER BY rowid; +} {NULL} +do_catchsql_test 12.61 { + INSERT INTO t1(a) VALUES(456); +} {1 {CHECK constraint failed: a NOT BETWEEN 0 AND +a}} +do_execsql_test 12.70 { + DROP TABLE t1; + CREATE TABLE t1(a TEXT, CHECK(a BETWEEN +a AND 999999)); + INSERT INTO t1(a) VALUES(NULL),(5); + SELECT quote(a) FROM t1 ORDER BY rowid; +} {NULL '5'} +do_execsql_test 12.80 { + DROP TABLE t1; + CREATE TABLE t1(a TEXT, CHECK(a NOT BETWEEN +a AND 999999)); + INSERT INTO t1(a) VALUES(NULL); + SELECT quote(a) FROM t1 ORDER BY rowid; +} {NULL} +do_catchsql_test 12.81 { + INSERT INTO t1(a) VALUES(456); +} {1 {CHECK constraint failed: a NOT BETWEEN +a AND 999999}} + +#------------------------------------------------------------------------- + +reset_db + +do_execsql_test 13.1.0 { + CREATE TABLE Table0 (Col0 , CHECK(Table0.Col0 NOT NULL ) ) ; + REPLACE INTO Table0 VALUES (hex(randomblob(100000))); +} +integrity_check 13.1.1 +do_execsql_test 13.1.2 { + UPDATE OR REPLACE Table0 SET Col0 = Table0.Col0 ; +} +integrity_check 13.1.3 +do_execsql_test 13.1.4 { + SELECT length(col0) FROM table0; +} {200000} + +do_execsql_test 13.2.0 { + CREATE TABLE t2 (x , CHECK((NOT (x ISNULL) ))); + REPLACE INTO t2 VALUES (hex(randomblob(100000))); +} +do_execsql_test 13.2.1 { + SELECT length(x) FROM t2 +} {200000} +do_execsql_test 13.2.2 { + UPDATE OR REPLACE t2 SET x = x; +} +do_execsql_test 13.2.3 { + SELECT length(x) FROM t2 +} {200000} +integrity_check 13.2.4 finish_test diff --git a/test/checkfault.test b/test/checkfault.test new file mode 100644 index 0000000000..3b18a64660 --- /dev/null +++ b/test/checkfault.test @@ -0,0 +1,41 @@ +# 2019 July 17 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# This file contains fault-injection test cases for the +# sqlite3_db_cacheflush API. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix cffault +source $testdir/malloc_common.tcl + +do_execsql_test 1.0 { + CREATE TABLE t1 (Col0 CHECK(1 COLLATE BINARY BETWEEN 1 AND 1) ) ; + CREATE TABLE t2(b, a CHECK( + CASE 'abc' COLLATE nocase WHEN a THEN 1 ELSE 0 END) + ); +} + +do_faultsim_test 1.1 -faults oom* -body { + execsql { INSERT INTO t1 VALUES ('ABCDEFG') } +} -test { + faultsim_test_result {0 {}} +} + +do_faultsim_test 1.2 -faults oom* -body { + execsql { INSERT INTO t2(a) VALUES('abc') } +} -test { + faultsim_test_result {0 {}} +} + + +finish_test diff --git a/test/chunksize.test b/test/chunksize.test new file mode 100644 index 0000000000..be3795fc5c --- /dev/null +++ b/test/chunksize.test @@ -0,0 +1,41 @@ +# 2019 June 5 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix chunksize + +if {$::tcl_platform(platform) ne "unix"} { + finish_test + return +} + +foreach {tn jrnlmode} { + 1 delete + 2 wal +} { + reset_db + file_control_chunksize_test db main 32768 + do_execsql_test $tn.0 " PRAGMA journal_mode = $jrnlmode " $jrnlmode + do_execsql_test $tn.1 { + CREATE TABLE t1(a, b); + INSERT INTO t1 VALUES(1, 2); + } + + execsql { PRAGMA wal_checkpoint } + + do_test $tn.2 { + file size test.db + } 32768 +} + +finish_test diff --git a/test/cksumvfs.test b/test/cksumvfs.test new file mode 100644 index 0000000000..2bf4d91873 --- /dev/null +++ b/test/cksumvfs.test @@ -0,0 +1,93 @@ +# 2024 March 19 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix cksumvfs + + +db close +sqlite3_shutdown +#test_sqlite3_log logfunc +sqlite3_initialize + +proc logfunc {args} { + puts "LOG: $args" +} + +sqlite3_register_cksumvfs +sqlite3 db test.db + +file_control_reservebytes db 8 +execsql { + PRAGMA page_size = 4096; +} + +set text [db one "SELECT hex(randomblob(5000))"] + +do_execsql_test 1.0 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c); + INSERT INTO t1 VALUES(1, $text, NULL); +} + +do_execsql_test 1.1 { + SELECT * FROM t1; +} [list 1 $text {}] + +do_execsql_test 1.2 { + DELETE FROM t1; +} + +do_test 1.3 { + execsql BEGIN + for {set ii 1500} {$ii < 10000} {incr ii} { + execsql { INSERT INTO t1 VALUES(NULL, randomblob(5000), randomblob($ii)) } + } + execsql COMMIT +} {} + +do_execsql_test 1.4 { + SELECT count(b) FROM t1 +} {8500} + +do_execsql_test 1.5 { + PRAGMA journal_mode = wal; + DELETE FROM t1; +} {wal} + +do_execsql_test 1.6 { + PRAGMA wal_checkpoint; +} {/0 # #/} + +do_execsql_test 1.7 { + WITH s(i) AS ( + VALUES(1) UNION ALL SELECT i+1 FROM s WHERE i<100 + ) + INSERT INTO t1 SELECT NULL, randomblob(5000), randomblob(i) FROM s; + SELECT count(*) FROM t1; +} {100} + +db_save_and_close +db_restore_and_reopen + +do_execsql_test 1.8 { + SELECT count(*) FROM t1; +} {100} + +db close +sqlite3 db test.db + +do_execsql_test 1.9 { + SELECT count(*) FROM t1; +} {100} + +finish_test diff --git a/test/close.test b/test/close.test index d5d6391ae5..107c7a782e 100644 --- a/test/close.test +++ b/test/close.test @@ -17,6 +17,10 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl set ::testprefix close +# This module bypasses the "-key" logic in tester.tcl, so it cannot run +# with the codec enabled. +do_not_use_codec + do_execsql_test 1.0 { CREATE TABLE t1(x); INSERT INTO t1 VALUES('one'); @@ -69,10 +73,17 @@ do_test 1.4.3 { list [catch { sqlite3_prepare $DB "SELECT * FROM sqlite_master" -1 dummy } msg] $msg -} {1 {(21) library routine called out of sequence}} +} {1 {(21) bad parameter or other API misuse}} do_test 1.4.4 { sqlite3_finalize $STMT } {SQLITE_OK} +do_test 1.5 { + set DB [sqlite3_open test.db] + sqlite3_blob_open $DB main t1 x 2 0 BLOB + sqlite3_close_v2 $DB + sqlite3_blob_close $BLOB +} {} + finish_test diff --git a/test/closure01.test b/test/closure01.test index 213ef4e966..ee3f2dd1d0 100644 --- a/test/closure01.test +++ b/test/closure01.test @@ -273,4 +273,23 @@ do_execsql_test 5.1 { ORDER BY id; } {8 9 10 11 12 13 14 15} +#------------------------------------------------------------------------- +# At one point the following join query was causing a malfunction in +# xBestIndex. +# +do_execsql_test 6.0 { + CREATE TABLE t4 ( + id INTEGER PRIMARY KEY, + name TEXT NOT NULL, + parent_id INTEGER + ); + CREATE VIRTUAL TABLE vt4 USING transitive_closure ( + idcolumn=id, parentcolumn=parent_id, tablename=t4 + ); +} + +do_execsql_test 6.1 { + SELECT * FROM t4, vt4 WHERE t4.id = vt4.root AND vt4.id=4 AND vt4.depth=2; +} + finish_test diff --git a/test/collate1.test b/test/collate1.test index 7cf5698454..da662bee62 100644 --- a/test/collate1.test +++ b/test/collate1.test @@ -23,7 +23,7 @@ set testprefix collate1 # collate1-1.* - Single-field ORDER BY with an explicit COLLATE clause. # collate1-2.* - Multi-field ORDER BY with an explicit COLLATE clause. # collate1-3.* - ORDER BY using a default collation type. Also that an -# explict collate type overrides a default collate type. +# explicit collate type overrides a default collate type. # collate1-4.* - ORDER BY using a data type. # @@ -338,6 +338,7 @@ do_test collate1-5.3 { #------------------------------------------------------------------------- # Fix problems with handling collation sequences named '"""'. # +sqlite3_db_config db SQLITE_DBCONFIG_DQS_DML 1 do_execsql_test 6.1 { SELECT """"""""; } {\"\"\"} @@ -400,5 +401,52 @@ do_execsql_test 7.2 { ORDER BY 1 COLLATE binary COLLATE binary COLLATE binary COLLATE nocase; } {abc DEF} +# 2019-06-14 +# https://sqlite.org/src/info/f1580ba1b574e9e9 +# +do_execsql_test 8.0 { + SELECT ' ' > char(20) COLLATE rtrim; +} 0 +do_execsql_test 8.1 { + SELECT '' < char(20) COLLATE rtrim; +} 1 +do_execsql_test 8.2 { + DROP TABLE IF EXISTS t0; + CREATE TABLE t0(c0 COLLATE RTRIM, c1 BLOB UNIQUE, + PRIMARY KEY (c0, c1)) WITHOUT ROWID; + INSERT INTO t0 VALUES (123, 3), (' ', 1), (' ', 2), ('', 4); + SELECT * FROM t0 WHERE c1 = 1; +} {{ } 1} + +# 2019-10-09 +# ALWAYS() macro fails following OOM +# Problem detected by dbsqlfuzz. +# +do_execsql_test 9.0 { + CREATE TABLE t1(a, b); + CREATE TABLE t2(c, d); +} + +do_faultsim_test 9.1 -faults oom* -body { + execsql { + SELECT * FROM ( + SELECT b COLLATE nocase IN (SELECT c FROM t2) FROM t1 + ); + } +} -test { + faultsim_test_result {0 {}} +} + +# 2020-01-03 dbsqlfuzz find +# +reset_db +do_catchsql_test 10.0 { + CREATE TABLE t1(a INTEGER PRIMARY KEY,b); + INSERT INTO t1 VALUES(0,NULL); + CREATE TABLE t2(x UNIQUE); + CREATE VIEW v1a(z,y) AS SELECT x COLLATE x FROM t2; + SELECT a,b,z,y,'' FROM t1 JOIN v1a ON b IS NOT FALSE; +} {1 {no such collation sequence: x}} + finish_test diff --git a/test/collate2.test b/test/collate2.test index d5aadb4eb5..281aa35709 100644 --- a/test/collate2.test +++ b/test/collate2.test @@ -684,16 +684,34 @@ do_test collate2-5.3 { SELECT collate2t1.b FROM collate2t2 NATURAL JOIN collate2t1; } } {aa} -do_test collate2-5.4 { +do_test collate2-5.4.1 { execsql { - SELECT collate2t2.b FROM collate2t1 LEFT OUTER JOIN collate2t2 USING (b) order by collate2t1.oid; + SELECT collate2t2.b FROM collate2t1 LEFT JOIN collate2t2 USING (b) order by collate2t1.oid; } } {{} aa {} {} {} aa {} {} {} aa {} {} {} aa {} {} {}} -do_test collate2-5.5 { +do_test collate2-5.4.2 { + execsql { + SELECT collate2t2.b FROM collate2t2 RIGHT JOIN collate2t1 ON collate2t1.b=collate2t2.b + ORDER BY collate2t1.oid; + } +} {{} aa {} {} {} aa {} {} {} aa {} {} {} aa {} {} {}} +do_test collate2-5.4.3 { + execsql { + SELECT collate2t2.b FROM collate2t1 LEFT JOIN collate2t2 ON collate2t2.b=collate2t1.b + ORDER BY collate2t1.oid; + } +} {{} aa {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}} +do_test collate2-5.5.1 { execsql { SELECT collate2t1.b, collate2t2.b FROM collate2t2 LEFT OUTER JOIN collate2t1 USING (b); } } {aa aa} +do_test collate2-5.5.2 { + execsql { + SELECT collate2t1.b, collate2t2.b + FROM collate2t1 RIGHT JOIN collate2t2 ON collate2t2.b=collate2t1.b + } +} {aa aa} do_execsql_test 6.1 { CREATE TABLE t1(x); diff --git a/test/collate5.test b/test/collate5.test index 5f8697ea51..71d4efe255 100644 --- a/test/collate5.test +++ b/test/collate5.test @@ -19,6 +19,8 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl +set testprefix collate5 + # # Tests are organised as follows: @@ -26,6 +28,7 @@ source $testdir/tester.tcl # collate5-2.* - Compound SELECT # collate5-3.* - ORDER BY on compound SELECT # collate5-4.* - GROUP BY +# collate5-5.* - Collation sequence cases # Create the collation sequence 'TEXT', purely for asthetic reasons. The # test cases in this script could just as easily use BINARY. @@ -289,4 +292,37 @@ do_test collate5-4.3 { } } {} +#------------------------------------------------------------------------- +reset_db + +do_execsql_test 5.0 { + CREATE TABLE t1(a, b COLLATE nocase); + CREATE TABLE t2(c, d); + INSERT INTO t2 VALUES(1, 'bbb'); +} +do_execsql_test 5.1 { + SELECT * FROM ( + SELECT a, b FROM t1 UNION ALL SELECT c, d FROM t2 + ) WHERE b='BbB'; +} {1 bbb} + +reset_db +do_execsql_test 5.2 { + CREATE TABLE t1(a,b,c COLLATE NOCASE); + INSERT INTO t1 VALUES(NULL,'C','c'); + CREATE VIEW v2 AS + SELECT a,b,c FROM t1 INTERSECT SELECT a,b,b FROM t1 + WHERE 'eT"3qRkL+oJMJjQ9z0'>=b + ORDER BY a,b,c; +} + +do_execsql_test 5.3 { + SELECT * FROM v2; +} { {} C c } + +do_execsql_test 5.4 { + SELECT * FROM v2 WHERE c='c'; +} { {} C c } + + finish_test diff --git a/test/collateB.test b/test/collateB.test new file mode 100644 index 0000000000..678eb5af0f --- /dev/null +++ b/test/collateB.test @@ -0,0 +1,77 @@ +# 2016-07-01 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# Test cases for a crash bug. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix collateB + +do_execsql_test collateB-1.1 { + CREATE TABLE t1(a INTEGER PRIMARY KEY); + CREATE TABLE t2(b INTEGER PRIMARY KEY, x1 INT COLLATE NOCASE); + CREATE TABLE t3(x2 INT); + SELECT * FROM t3, t2, t1 WHERE x2=b AND x1=a AND a=1; +} {} +do_execsql_test collateB-1.2 { + INSERT INTO t1(a) VALUES(1),(2),(3); + INSERT INTO t2(b,x1) VALUES(11,1),(22,2),(33,3); + INSERT INTO t3(x2) VALUES(11),(22),(33); + SELECT *,'|' FROM t3, t2, t1 WHERE x2=b AND x1=a AND a=1; +} {11 11 1 1 |} +do_execsql_test collateB-1.3 { + SELECT *,'|' FROM t3, t1, t2 WHERE x2=b AND x1=a AND a=1; +} {11 1 11 1 |} +do_execsql_test collateB-1.4 { + SELECT *,'|' FROM t2, t3, t1 WHERE x2=b AND x1=a AND a=1; +} {11 1 11 1 |} +do_execsql_test collateB-1.5 { + SELECT *,'|' FROM t2, t1, t3 WHERE x2=b AND x1=a AND a=1; +} {11 1 1 11 |} +do_execsql_test collateB-1.6 { + SELECT *,'|' FROM t1, t2, t3 WHERE x2=b AND x1=a AND a=1; +} {1 11 1 11 |} +do_execsql_test collateB-1.7 { + SELECT *,'|' FROM t1, t2, t3 WHERE x2=b AND x1=a AND a=1; +} {1 11 1 11 |} +do_execsql_test collateB-1.12 { + SELECT *,'|' FROM t3, t2, t1 WHERE b=x2 AND a=x1 AND 1=a; +} {11 11 1 1 |} +do_execsql_test collateB-1.13 { + SELECT *,'|' FROM t3, t1, t2 WHERE b=x2 AND a=x1 AND 1=a; +} {11 1 11 1 |} +do_execsql_test collateB-1.14 { + SELECT *,'|' FROM t2, t3, t1 WHERE b=x2 AND a=x1 AND 1=a; +} {11 1 11 1 |} +do_execsql_test collateB-1.15 { + SELECT *,'|' FROM t2, t1, t3 WHERE b=x2 AND a=x1 AND 1=a; +} {11 1 1 11 |} +do_execsql_test collateB-1.16 { + SELECT *,'|' FROM t1, t2, t3 WHERE b=x2 AND a=x1 AND 1=a; +} {1 11 1 11 |} +do_execsql_test collateB-1.17 { + SELECT *,'|' FROM t1, t2, t3 WHERE b=x2 AND a=x1 AND 1=a; +} {1 11 1 11 |} + +#------------------------------------------------------------------------- +# Test an assert() failure that was occurring if an index were created +# on a column explicitly declared "COLLATE binary". +reset_db +do_execsql_test 2.1 { + CREATE TABLE t4(a COLLATE binary); + CREATE INDEX i4 ON t4(a); + INSERT INTO t4 VALUES('one'), ('two'), ('three'); + VACUUM; +} + +integrity_check 2.2 + +finish_test diff --git a/test/colmeta.test b/test/colmeta.test index 21b0e0f38a..43d2c83cb6 100644 --- a/test/colmeta.test +++ b/test/colmeta.test @@ -97,7 +97,7 @@ foreach {tn params results} $tests { } # Calling sqlite3_table_column_metadata with a NULL column name merely -# checks for the existance of the table. +# checks for the existence of the table. # do_test colmeta-300 { catch {sqlite3_table_column_metadata $::DB main xyzzy} res diff --git a/test/colname.test b/test/colname.test index e16304d4a9..0907df641e 100644 --- a/test/colname.test +++ b/test/colname.test @@ -13,7 +13,6 @@ # The focus of this file is testing how SQLite generates the names # of columns in a result set. # -# $Id: colname.test,v 1.7 2009/06/02 15:47:38 drh Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl @@ -326,4 +325,123 @@ do_test colname-8.1 { } } {123} +# 2017-07-29: Interaction between column naming and query flattening. +# For years now, the query flattener has inserted AS clauses on the +# outer query that were the original SQL text of the column. This caused +# column-name shifts when the query flattener was enhanced, breaking +# legacy applications. See https://sqlite.org/src/info/41c27bc0ff1d3135 +# for details. +# +# To fix this, the column naming logic was moved ahead of the query +# flattener so that column names are assigned before the query flattener +# runs. +# +db close +sqlite3 db :memory: +do_test colname-9.100 { + db eval { + CREATE TABLE t1(a,b); + INSERT INTO t1 VALUES(1,2); + CREATE VIEW v1(x,y) AS SELECT a,b FROM t1; + } + execsql2 {SELECT v1.x, (Y) FROM v1} + # Prior to the fix, this would return: "v1.x 1 (Y) 2" +} {x 1 y 2} +do_test colname-9.110 { + execsql2 {SELECT * FROM v1} +} {x 1 y 2} +do_test colname-9.120 { + db eval { + CREATE VIEW v2(x,y) AS SELECT a,b FROM t1 LIMIT 10; + } + execsql2 {SELECT * FROM v2 WHERE 1} +} {x 1 y 2} +do_test colname-9.130 { + execsql2 {SELECT v2.x, [v2].[y] FROM v2 WHERE 1} +} {x 1 y 2} +do_test colname-9.140 { + execsql2 {SELECT +x, +y FROM v2 WHERE 1} +} {+x 1 +y 2} + +do_test colname-9.200 { + db eval { + CREATE TABLE t2(c,d); + INSERT INTO t2 VALUES(3,4); + CREATE VIEW v3 AS SELECT c AS a, d AS b FROM t2; + } + execsql2 {SELECT t1.a, v3.a AS n FROM t1 LEFT JOIN v3} +} {a 1 n 3} +do_test colname-9.211 { + execsql2 {SELECT t1.a AS n, v3.a FROM t1 JOIN v3} +} {n 1 a 3} +do_test colname-9.210 { + execsql2 {SELECT t1.a, v3.a AS n FROM t1 JOIN v3} +} {a 1 n 3} + +# 2017-12-23: Ticket https://sqlite.org/src/info/3b4450072511e621 +# Inconsistent column names in CREATE TABLE AS +# +# Verify that the names of columns in the created table of a CREATE TABLE AS +# are the same as the names of result columns in the SELECT statement. +# +do_execsql_test colname-9.300 { + DROP TABLE IF EXISTS t1; + DROP TABLE IF EXISTS t2; + CREATE TABLE t1(aaa INT); + INSERT INTO t1(aaa) VALUES(123); +} +do_test colname-9.310 { + execsql2 {SELECT BBb FROM (SELECT aaa AS Bbb FROM t1)} +} {Bbb 123} +ifcapable vtab { + do_execsql_test colname-9.320 { + CREATE TABLE t2 AS SELECT BBb FROM (SELECT aaa AS Bbb FROM t1); + SELECT name FROM pragma_table_info('t2'); + } {Bbb} +} +do_execsql_test colname-9.330 { -- added 2019-08-10 to invalidate + DROP TABLE IF EXISTS t1; -- a couple assert()s that were + CREATE TABLE t1(a); -- added by ticket 3b44500725 + INSERT INTO t1 VALUES(17),(2),(99),(-3),(7); + SELECT (SELECT avg(a) UNION SELECT min(a) OVER()) FROM t1; +} {17} + +# Issue detected by OSSFuzz on 2017-12-24 (Christmas Eve) +# caused by check-in https://sqlite.org/src/info/6b2ff26c25 +# +# Prior to being fixed, the following CREATE TABLE was dereferencing +# a NULL pointer and segfaulting. +# +do_catchsql_test colname-9.400 { + CREATE TABLE t4 AS SELECT #0; +} {1 {near "#0": syntax error}} + +# Issue detected by OSSFuzz on 2017-12-25 (Christmas Day) +# also caused by check-in https://sqlite.org/src/info/6b2ff26c25 +# +# Prior to being fixed, the following CREATE TABLE caused an +# assertion fault. +# +do_catchsql_test colname-9.410 { + CREATE TABLE t5 AS SELECT RAISE(abort,a); +} {1 {no such column: a}} + +# Make sure the quotation marks get removed from the column names +# when constructing a new table from an aggregate SELECT. +# Email from Juergen Palm on 2017-07-11. +# +do_execsql_test colname-10.100 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1("with space" TEXT); + DROP TABLE IF EXISTS t2; + CREATE TABLE t2 AS SELECT "with space" FROM t1; + PRAGMA table_info(t2); +} {0 {with space} TEXT 0 {} 0} +do_execsql_test colname-10.110 { + DROP TABLE IF EXISTS t3; + CREATE TABLE t3 AS SELECT "with space" FROM t1 GROUP BY 1; + PRAGMA table_info(t3); +} {0 {with space} TEXT 0 {} 0} + + finish_test diff --git a/test/columncount.test b/test/columncount.test new file mode 100644 index 0000000000..d9956b40f5 --- /dev/null +++ b/test/columncount.test @@ -0,0 +1,62 @@ +# 2021 February 15 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is testing the sqlite3_column_count() API. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix columncount + +# If SQLITE_OMIT_ALTERTABLE is defined, omit this file. +ifcapable !altertable { + finish_test + return +} + +proc do_ccsql_test {tn sql res} { + + uplevel [list do_test $tn [subst -nocommands { + set stmt [sqlite3_prepare_v2 db {$sql} -1 dummy] + set res [sqlite3_column_count [set stmt]] + while {[sqlite3_step [set stmt]]=="SQLITE_ROW"} { + for {set i 0} {[set i] < [sqlite3_data_count [set stmt]]} {incr i} { + lappend res [sqlite3_column_text [set stmt] [set i]] + } + } + + set rc [sqlite3_finalize [set stmt]] + if {[set rc]!="SQLITE_OK"} { + error [sqlite3_errmsg db] + } + + set res + }] [list {*}$res]] + +} + +do_execsql_test 1.0 { + CREATE TABLE t1(x, y, z); + INSERT INTO t1 VALUES('a', 'b', 'c'); +} + +do_ccsql_test 1.1 { SELECT * FROM t1 } {3 a b c} +do_ccsql_test 1.2 { CREATE TABLE t2(a, b) } {0} + +do_ccsql_test 1.3 { ALTER TABLE t2 RENAME TO t3 } {0} +do_ccsql_test 1.4 { ALTER TABLE t3 RENAME b TO ccc } {0} +do_ccsql_test 1.5 { ALTER TABLE t3 ADD COLUMN d } {0} + +do_ccsql_test 1.6 { DROP TABLE t3 } {0} + + + +finish_test diff --git a/test/conflict.test b/test/conflict.test index af5668ed72..54c01d36de 100644 --- a/test/conflict.test +++ b/test/conflict.test @@ -13,7 +13,6 @@ # This file implements tests for the conflict resolution extension # to SQLite. # -# $Id: conflict.test,v 1.32 2009/04/30 09:10:38 danielk1977 Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl @@ -285,15 +284,17 @@ do_test conflict-6.0 { # t3 Number of temporary files for tables # t4 Number of temporary files for statement journals # -# Update: Since temporary table files are now opened lazily, and none -# of the following tests use large quantities of data, t3 is always 0. +# Update (2007-08-21): Since temporary table files are now opened lazily, +# and none of the following tests use large quantities of data, t3 is always 0. +# +# Update (2016-03-04): Subjournals now also open lazily, so t4 is also always 0. # foreach {i conf1 cmd t0 t1 t2 t3 t4} { - 1 {} UPDATE 1 {6 7 8 9} 1 0 1 + 1 {} UPDATE 1 {6 7 8 9} 1 0 0 2 REPLACE UPDATE 0 {7 6 9} 1 0 0 3 IGNORE UPDATE 0 {6 7 3 9} 1 0 0 4 FAIL UPDATE 1 {6 7 3 4} 1 0 0 - 5 ABORT UPDATE 1 {1 2 3 4} 1 0 1 + 5 ABORT UPDATE 1 {1 2 3 4} 1 0 0 6 ROLLBACK UPDATE 1 {1 2 3 4} 0 0 0 7 REPLACE {UPDATE OR IGNORE} 0 {6 7 3 9} 1 0 0 8 IGNORE {UPDATE OR REPLACE} 0 {7 6 9} 1 0 0 @@ -303,7 +304,7 @@ foreach {i conf1 cmd t0 t1 t2 t3 t4} { 12 {} {UPDATE OR IGNORE} 0 {6 7 3 9} 1 0 0 13 {} {UPDATE OR REPLACE} 0 {7 6 9} 1 0 0 14 {} {UPDATE OR FAIL} 1 {6 7 3 4} 1 0 0 - 15 {} {UPDATE OR ABORT} 1 {1 2 3 4} 1 0 1 + 15 {} {UPDATE OR ABORT} 1 {1 2 3 4} 1 0 0 16 {} {UPDATE OR ROLLBACK} 1 {1 2 3 4} 0 0 0 } { if {$t0} {set t1 {UNIQUE constraint failed: t1.a}} @@ -812,7 +813,7 @@ do_test conflict-13.1 { catchsql { REPLACE INTO t13 VALUES(2); } -} {1 {CHECK constraint failed: t13}} +} {1 {CHECK constraint failed: a!=2}} verify_ex_errcode conflict-13.1b SQLITE_CONSTRAINT_CHECK do_test conflict-13.2 { execsql { @@ -823,4 +824,68 @@ do_test conflict-13.2 { } {1 3} +# Ticket https://sqlite.org/src/tktview/e6f1f2e34dceeb1ed61531c7e9 +# Verify that it is not possible to sneak a NULL value into a NOT NULL +# column using REPLACE. +# +do_catchsql_test conflict-14.1 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(x NOT NULL DEFAULT NULL); + REPLACE INTO t1 DEFAULT VALUES; +} {1 {NOT NULL constraint failed: t1.x}} + +# 2019-12-15 gramfuzz1 find +# Three UNIQUE constraints, where the third would is a duplicate except +# that it adds ON CONFLICT REPLACE. Verify that the indexes end up +# sorted in the correct order (REPLACE last) so that constraint processing +# works correctly. +# +reset_db +do_execsql_test conflict-15.10 { + CREATE TABLE t1( + x PRIMARY KEY, + UNIQUE(x,x), + UNIQUE(x,x) ON CONFLICT REPLACE + ); + INSERT INTO t1(x) VALUES(1); + SELECT * FROM t1; +} {1} +do_catchsql_test conflict-15.20 { + INSERT INTO t1(x) VALUES(1); +} {1 {UNIQUE constraint failed: t1.x}} +do_execsql_test conflict-15.30 { + SELECT * FROM t1; +} {1} + +# 2023-01-16 https://sqlite.org/forum/forumpost/aa580a5af38a58e3 +# The parser accepts an ON CONFLICT clause on table CHECK constraints +# but not on column CHECK constraints. But the CHECK constraint is +# ignored. It has been like this since version 3.0.0. +# +# There might be applications and/or databases in the wild that have +# table CHECK constraints with ON CONFLICT clauses. In as much as this +# is a harmless quirk, we continue to support it in order to avoid +# breaking those legacy applications and databases. +# +reset_db +do_catchsql_test conflict-16.1 { + -- ON CONFLICT clauses are not allowed on column CHECK constraints + CREATE TABLE t1(a INT CHECK( a!=5 ) ON CONFLICT ignore); +} {1 {near "ON": syntax error}} +do_execsql_test conflict-16.2 { + -- ON CONFLICT is allowed on table CHECK constraints + CREATE TABLE t1(a INT, CHECK( a!=5 ) ON CONFLICT ignore); +} {} +do_catchsql_test conflict-16.3 { + -- The ON CONFLICT clause is in-op + INSERT INTO t1(a) VALUES(4),(5),(6); +} {1 {CHECK constraint failed: a!=5}} +do_execsql_test conflict-16.4 { + SELECT a FROM t1 ORDER BY a; +} {} +do_execsql_test conflict-16.5 { + INSERT OR IGNORE INTO t1(a) VALUES(4),(5),(6); + SELECT a FROM t1 ORDER BY a; +} {4 6} + finish_test diff --git a/test/conflict2.test b/test/conflict2.test index 6496913849..96289d38ca 100644 --- a/test/conflict2.test +++ b/test/conflict2.test @@ -287,22 +287,25 @@ do_test conflict2-6.0 { # Update: Since temporary table files are now opened lazily, and none # of the following tests use large quantities of data, t3 is always 0. # +# Update (2016-03-04): Subjournals now only open when their size +# exceeds 64KB. +# foreach {i conf1 cmd t0 t1 t2 t3 t4} { - 1 {} UPDATE 1 {6 7 8 9} 1 0 1 - 2 REPLACE UPDATE 0 {7 6 9} 1 0 1 - 3 IGNORE UPDATE 0 {6 7 3 9} 1 0 1 - 4 FAIL UPDATE 1 {6 7 3 4} 1 0 1 - 5 ABORT UPDATE 1 {1 2 3 4} 1 0 1 - 6 ROLLBACK UPDATE 1 {1 2 3 4} 0 0 1 + 1 {} UPDATE 1 {6 7 8 9} 1 0 0 + 2 REPLACE UPDATE 0 {7 6 9} 1 0 0 + 3 IGNORE UPDATE 0 {6 7 3 9} 1 0 0 + 4 FAIL UPDATE 1 {6 7 3 4} 1 0 0 + 5 ABORT UPDATE 1 {1 2 3 4} 1 0 0 + 6 ROLLBACK UPDATE 1 {1 2 3 4} 0 0 0 7 REPLACE {UPDATE OR IGNORE} 0 {6 7 3 9} 1 0 0 - 8 IGNORE {UPDATE OR REPLACE} 0 {7 6 9} 1 0 1 + 8 IGNORE {UPDATE OR REPLACE} 0 {7 6 9} 1 0 0 9 FAIL {UPDATE OR IGNORE} 0 {6 7 3 9} 1 0 0 - 10 ABORT {UPDATE OR REPLACE} 0 {7 6 9} 1 0 1 + 10 ABORT {UPDATE OR REPLACE} 0 {7 6 9} 1 0 0 11 ROLLBACK {UPDATE OR IGNORE} 0 {6 7 3 9} 1 0 0 12 {} {UPDATE OR IGNORE} 0 {6 7 3 9} 1 0 0 - 13 {} {UPDATE OR REPLACE} 0 {7 6 9} 1 0 1 + 13 {} {UPDATE OR REPLACE} 0 {7 6 9} 1 0 0 14 {} {UPDATE OR FAIL} 1 {6 7 3 4} 1 0 0 - 15 {} {UPDATE OR ABORT} 1 {1 2 3 4} 1 0 1 + 15 {} {UPDATE OR ABORT} 1 {1 2 3 4} 1 0 0 16 {} {UPDATE OR ROLLBACK} 1 {1 2 3 4} 0 0 0 } { @@ -808,7 +811,7 @@ do_test conflict2-13.1 { catchsql { REPLACE INTO t13 VALUES(2); } -} {1 {CHECK constraint failed: t13}} +} {1 {CHECK constraint failed: a!=2}} verify_ex_errcode conflict2-13.1b SQLITE_CONSTRAINT_CHECK do_test conflict2-13.2 { execsql { diff --git a/test/conflict3.test b/test/conflict3.test index b51a55ee71..8eb4c1b0f7 100644 --- a/test/conflict3.test +++ b/test/conflict3.test @@ -19,13 +19,14 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl +set testprefix conflict3 ifcapable !conflict { finish_test return } -do_execsql_test conflict-1.1 { +do_execsql_test 1.1 { CREATE TABLE t1( a INTEGER PRIMARY KEY ON CONFLICT REPLACE, b UNIQUE ON CONFLICT IGNORE, @@ -37,7 +38,7 @@ do_execsql_test conflict-1.1 { # Insert a row that conflicts on column B. The insert should be ignored. # -do_execsql_test conflict-1.2 { +do_execsql_test 1.2 { INSERT INTO t1(a,b,c) VALUES(3,2,5); SELECT a,b,c FROM t1 ORDER BY a; } {1 2 3 2 3 4} @@ -45,16 +46,16 @@ do_execsql_test conflict-1.2 { # Insert two rows where the second conflicts on C. The first row show go # and and then there should be a constraint error. # -do_test conflict-1.3 { +do_test 1.3 { catchsql {INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4);} } {1 {UNIQUE constraint failed: t1.c}} -do_execsql_test conflict-1.4 { +do_execsql_test 1.4 { SELECT a,b,c FROM t1 ORDER BY a; } {1 2 3 2 3 4 4 5 6} # Replete the tests above, but this time on a table non-INTEGER primary key. # -do_execsql_test conflict-2.1 { +do_execsql_test 2.1 { DROP TABLE t1; CREATE TABLE t1( a INT PRIMARY KEY ON CONFLICT REPLACE, @@ -67,7 +68,7 @@ do_execsql_test conflict-2.1 { # Insert a row that conflicts on column B. The insert should be ignored. # -do_execsql_test conflict-2.2 { +do_execsql_test 2.2 { INSERT INTO t1(a,b,c) VALUES(3,2,5); SELECT a,b,c FROM t1 ORDER BY a; } {1 2 3 2 3 4} @@ -75,16 +76,16 @@ do_execsql_test conflict-2.2 { # Insert two rows where the second conflicts on C. The first row show go # and and then there should be a constraint error. # -do_test conflict-2.3 { +do_test 2.3 { catchsql {INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4);} } {1 {UNIQUE constraint failed: t1.c}} -do_execsql_test conflict-2.4 { +do_execsql_test 2.4 { SELECT a,b,c FROM t1 ORDER BY a; } {1 2 3 2 3 4 4 5 6} # Replete again on a WITHOUT ROWID table. # -do_execsql_test conflict-3.1 { +do_execsql_test 3.1 { DROP TABLE t1; CREATE TABLE t1( a INT PRIMARY KEY ON CONFLICT REPLACE, @@ -97,7 +98,7 @@ do_execsql_test conflict-3.1 { # Insert a row that conflicts on column B. The insert should be ignored. # -do_execsql_test conflict-3.2 { +do_execsql_test 3.2 { INSERT INTO t1(a,b,c) VALUES(3,2,5); SELECT a,b,c FROM t1 ORDER BY a; } {1 2 3 2 3 4} @@ -105,16 +106,16 @@ do_execsql_test conflict-3.2 { # Insert two rows where the second conflicts on C. The first row show go # and and then there should be a constraint error. # -do_test conflict-3.3 { +do_test 3.3 { catchsql {INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4);} } {1 {UNIQUE constraint failed: t1.c}} -do_execsql_test conflict-3.4 { +do_execsql_test 3.4 { SELECT a,b,c FROM t1 ORDER BY a; } {1 2 3 2 3 4 4 5 6} # Arrange the table rows in a different order and repeat. # -do_execsql_test conflict-4.1 { +do_execsql_test 4.1 { DROP TABLE t1; CREATE TABLE t1( b UNIQUE ON CONFLICT IGNORE, @@ -127,7 +128,7 @@ do_execsql_test conflict-4.1 { # Insert a row that conflicts on column B. The insert should be ignored. # -do_execsql_test conflict-4.2 { +do_execsql_test 4.2 { INSERT INTO t1(a,b,c) VALUES(3,2,5); SELECT a,b,c FROM t1 ORDER BY a; } {1 2 3 2 3 4} @@ -135,16 +136,16 @@ do_execsql_test conflict-4.2 { # Insert two rows where the second conflicts on C. The first row show go # and and then there should be a constraint error. # -do_test conflict-4.3 { +do_test 4.3 { catchsql {INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4);} } {1 {UNIQUE constraint failed: t1.c}} -do_execsql_test conflict-4.4 { +do_execsql_test 4.4 { SELECT a,b,c FROM t1 ORDER BY a; } {1 2 3 2 3 4 4 5 6} # Arrange the table rows in a different order and repeat. # -do_execsql_test conflict-5.1 { +do_execsql_test 5.1 { DROP TABLE t1; CREATE TABLE t1( b UNIQUE ON CONFLICT IGNORE, @@ -157,7 +158,7 @@ do_execsql_test conflict-5.1 { # Insert a row that conflicts on column B. The insert should be ignored. # -do_execsql_test conflict-5.2 { +do_execsql_test 5.2 { INSERT INTO t1(a,b,c) VALUES(3,2,5); SELECT a,b,c FROM t1 ORDER BY a; } {1 2 3 2 3 4} @@ -165,16 +166,16 @@ do_execsql_test conflict-5.2 { # Insert two rows where the second conflicts on C. The first row show go # and and then there should be a constraint error. # -do_test conflict-5.3 { +do_test 5.3 { catchsql {INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4);} } {1 {UNIQUE constraint failed: t1.c}} -do_execsql_test conflict-5.4 { +do_execsql_test 5.4 { SELECT a,b,c FROM t1 ORDER BY a; } {1 2 3 2 3 4 4 5 6} # Arrange the table rows in a different order and repeat. # -do_execsql_test conflict-6.1 { +do_execsql_test 6.1 { DROP TABLE t1; CREATE TABLE t1( c UNIQUE ON CONFLICT FAIL, @@ -187,7 +188,7 @@ do_execsql_test conflict-6.1 { # Insert a row that conflicts on column B. The insert should be ignored. # -do_execsql_test conflict-6.2 { +do_execsql_test 6.2 { INSERT INTO t1(a,b,c) VALUES(3,2,5); SELECT a,b,c FROM t1 ORDER BY a; } {1 2 3 2 3 4} @@ -195,16 +196,16 @@ do_execsql_test conflict-6.2 { # Insert two rows where the second conflicts on C. The first row show go # and and then there should be a constraint error. # -do_test conflict-6.3 { +do_test 6.3 { catchsql {INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4);} } {1 {UNIQUE constraint failed: t1.c}} -do_execsql_test conflict-6.4 { +do_execsql_test 6.4 { SELECT a,b,c FROM t1 ORDER BY a; } {1 2 3 2 3 4 4 5 6} # Change which column is the PRIMARY KEY # -do_execsql_test conflict-7.1 { +do_execsql_test 7.1 { DROP TABLE t1; CREATE TABLE t1( a UNIQUE ON CONFLICT REPLACE, @@ -217,7 +218,7 @@ do_execsql_test conflict-7.1 { # Insert a row that conflicts on column B. The insert should be ignored. # -do_execsql_test conflict-7.2 { +do_execsql_test 7.2 { INSERT INTO t1(a,b,c) VALUES(3,2,5); SELECT a,b,c FROM t1 ORDER BY a; } {1 2 3 2 3 4} @@ -225,16 +226,16 @@ do_execsql_test conflict-7.2 { # Insert two rows where the second conflicts on C. The first row show go # and and then there should be a constraint error. # -do_test conflict-7.3 { +do_test 7.3 { catchsql {INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4);} } {1 {UNIQUE constraint failed: t1.c}} -do_execsql_test conflict-7.4 { +do_execsql_test 7.4 { SELECT a,b,c FROM t1 ORDER BY a; } {1 2 3 2 3 4 4 5 6} # Change which column is the PRIMARY KEY # -do_execsql_test conflict-8.1 { +do_execsql_test 8.1 { DROP TABLE t1; CREATE TABLE t1( a UNIQUE ON CONFLICT REPLACE, @@ -247,7 +248,7 @@ do_execsql_test conflict-8.1 { # Insert a row that conflicts on column B. The insert should be ignored. # -do_execsql_test conflict-8.2 { +do_execsql_test 8.2 { INSERT INTO t1(a,b,c) VALUES(3,2,5); SELECT a,b,c FROM t1 ORDER BY a; } {1 2 3 2 3 4} @@ -255,16 +256,16 @@ do_execsql_test conflict-8.2 { # Insert two rows where the second conflicts on C. The first row show go # and and then there should be a constraint error. # -do_test conflict-8.3 { +do_test 8.3 { catchsql {INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4);} } {1 {UNIQUE constraint failed: t1.c}} -do_execsql_test conflict-8.4 { +do_execsql_test 8.4 { SELECT a,b,c FROM t1 ORDER BY a; } {1 2 3 2 3 4 4 5 6} # Change which column is the PRIMARY KEY # -do_execsql_test conflict-9.1 { +do_execsql_test 9.1 { DROP TABLE t1; CREATE TABLE t1( a UNIQUE ON CONFLICT REPLACE, @@ -277,7 +278,7 @@ do_execsql_test conflict-9.1 { # Insert a row that conflicts on column B. The insert should be ignored. # -do_execsql_test conflict-9.2 { +do_execsql_test 9.2 { INSERT INTO t1(a,b,c) VALUES(3,2,5); SELECT a,b,c FROM t1 ORDER BY a; } {1 2 3 2 3 4} @@ -285,16 +286,16 @@ do_execsql_test conflict-9.2 { # Insert two rows where the second conflicts on C. The first row show go # and and then there should be a constraint error. # -do_test conflict-9.3 { +do_test 9.3 { catchsql {INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4);} } {1 {UNIQUE constraint failed: t1.c}} -do_execsql_test conflict-9.4 { +do_execsql_test 9.4 { SELECT a,b,c FROM t1 ORDER BY a; } {1 2 3 2 3 4 4 5 6} # Change which column is the PRIMARY KEY # -do_execsql_test conflict-10.1 { +do_execsql_test 10.1 { DROP TABLE t1; CREATE TABLE t1( a UNIQUE ON CONFLICT REPLACE, @@ -307,7 +308,7 @@ do_execsql_test conflict-10.1 { # Insert a row that conflicts on column B. The insert should be ignored. # -do_execsql_test conflict-10.2 { +do_execsql_test 10.2 { INSERT INTO t1(a,b,c) VALUES(3,2,5); SELECT a,b,c FROM t1 ORDER BY a; } {1 2 3 2 3 4} @@ -315,16 +316,16 @@ do_execsql_test conflict-10.2 { # Insert two rows where the second conflicts on C. The first row show go # and and then there should be a constraint error. # -do_test conflict-10.3 { +do_test 10.3 { catchsql {INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4);} } {1 {UNIQUE constraint failed: t1.c}} -do_execsql_test conflict-10.4 { +do_execsql_test 10.4 { SELECT a,b,c FROM t1 ORDER BY a; } {1 2 3 2 3 4 4 5 6} # Change which column is the PRIMARY KEY # -do_execsql_test conflict-11.1 { +do_execsql_test 11.1 { DROP TABLE t1; CREATE TABLE t1( a UNIQUE ON CONFLICT REPLACE, @@ -337,7 +338,7 @@ do_execsql_test conflict-11.1 { # Insert a row that conflicts on column B. The insert should be ignored. # -do_execsql_test conflict-11.2 { +do_execsql_test 11.2 { INSERT INTO t1(a,b,c) VALUES(3,2,5); SELECT a,b,c FROM t1 ORDER BY a; } {1 2 3 2 3 4} @@ -345,12 +346,92 @@ do_execsql_test conflict-11.2 { # Insert two rows where the second conflicts on C. The first row show go # and and then there should be a constraint error. # -do_test conflict-11.3 { +do_test 11.3 { catchsql {INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4);} } {1 {UNIQUE constraint failed: t1.c}} -do_execsql_test conflict-11.4 { +do_execsql_test 11.4 { SELECT a,b,c FROM t1 ORDER BY a; } {1 2 3 2 3 4 4 5 6} +# Check that ticket [f68dc596c4] has been fixed. +# +do_execsql_test 12.1 { + CREATE TABLE t2(a INTEGER PRIMARY KEY, b TEXT); + INSERT INTO t2 VALUES(111, '111'); +} +do_execsql_test 12.2 { + REPLACE INTO t2 VALUES(NULL, '112'), (111, '111B'); +} +do_execsql_test 12.3 { + SELECT * FROM t2; +} {111 111B 112 112} + +#------------------------------------------------------------------------- +ifcapable trigger { + reset_db + do_execsql_test 13.1.0 { + PRAGMA recursive_triggers = true; + CREATE TABLE t0 (c0 UNIQUE, c1 UNIQUE); + CREATE TRIGGER tr0 AFTER DELETE ON t0 BEGIN + DELETE FROM t0; + END; + + INSERT INTO t0 VALUES(1, NULL); + INSERT INTO t0 VALUES(0, NULL); + } + + do_catchsql_test 13.1.1 { + UPDATE OR REPLACE t0 SET c1 = 1; + } {1 {constraint failed}} + + integrity_check 13.1.2 + + do_execsql_test 13.1.3 { + SELECT * FROM t0 + } {1 {} 0 {}} + + do_execsql_test 13.2.0 { + CREATE TABLE t2 (a PRIMARY KEY, b UNIQUE, c UNIQUE) WITHOUT ROWID; + CREATE TRIGGER tr3 AFTER DELETE ON t2 BEGIN + DELETE FROM t2; + END; + + INSERT INTO t2 VALUES(1, 1, 1); + INSERT INTO t2 VALUES(2, 2, 2); + } + + do_catchsql_test 13.2.1 { + UPDATE OR REPLACE t2 SET c = 0; + } {1 {constraint failed}} + + integrity_check 13.2.2 + + do_execsql_test 13.2.3 { + SELECT * FROM t2 + } {1 1 1 2 2 2} + + do_execsql_test 13.3.0 { + CREATE TABLE t1(a, b); + CREATE TABLE log(x); + CREATE INDEX i1 ON t1(a); + INSERT INTO t1 VALUES(1, 2); + + CREATE TRIGGER tb BEFORE UPDATE ON t1 BEGIN + DELETE FROM t1; + END; + CREATE TRIGGER ta AFTER UPDATE ON t1 BEGIN + INSERT INTO log VALUES('fired!'); + END; + + UPDATE t1 SET b=3; + } + + do_execsql_test 13.3.1 { + SELECT * FROM t1; + } {} + do_execsql_test 13.3.2 { + SELECT * FROM log; + } {} +} finish_test diff --git a/test/corrupt.test b/test/corrupt.test index 3e49a9ff18..ff61cc0bbe 100644 --- a/test/corrupt.test +++ b/test/corrupt.test @@ -132,6 +132,7 @@ do_test corrupt-3.2 { set t1_r [execsql {SELECT rootpage FROM sqlite_master WHERE name = 't1i1'}] set t1i1_r [execsql {SELECT rootpage FROM sqlite_master WHERE name = 't1'}] set cookie [expr [execsql {PRAGMA schema_version}] + 1] + sqlite3_db_config db DEFENSIVE 0 execsql " PRAGMA writable_schema = 1; UPDATE sqlite_master SET rootpage = $t1_r WHERE name = 't1'; @@ -289,7 +290,7 @@ ifcapable oversize_cell_check { # detecting corruption was not possible at that point, and an assert() failed. # set fd [open test.db r+] - fconfigure $fd -translation binary -encoding binary + fconfigure $fd -translation binary seek $fd [expr 1024+8] puts -nonewline $fd "\x03\x14" close $fd diff --git a/test/corrupt2.test b/test/corrupt2.test index 9bd29cf90f..fc24cb376f 100644 --- a/test/corrupt2.test +++ b/test/corrupt2.test @@ -59,7 +59,7 @@ do_test corrupt2-1.2 { $::presql SELECT * FROM sqlite_master; " db2 -} {1 {file is encrypted or is not a database}} +} {1 {file is not a database}} do_test corrupt2-1.3 { db2 close @@ -69,7 +69,7 @@ do_test corrupt2-1.3 { forcedelete corrupt.db-journal forcecopy test.db corrupt.db set f [open corrupt.db RDWR] - fconfigure $f -encoding binary + fconfigure $f -translation binary seek $f 16 start puts -nonewline $f "\x00\xFF" close $f @@ -79,7 +79,7 @@ do_test corrupt2-1.3 { $::presql SELECT * FROM sqlite_master; " db2 -} {1 {file is encrypted or is not a database}} +} {1 {file is not a database}} do_test corrupt2-1.4 { db2 close @@ -89,17 +89,17 @@ do_test corrupt2-1.4 { forcedelete corrupt.db-journal forcecopy test.db corrupt.db set f [open corrupt.db RDWR] - fconfigure $f -encoding binary + fconfigure $f -translation binary seek $f 101 start puts -nonewline $f "\xFF\xFF" close $f sqlite3 db2 corrupt.db - catchsql " - $::presql - SELECT * FROM sqlite_master; - " db2 -} {1 {database disk image is malformed}} + # Note: This test is no longer meaningful due to the deferred computation + # of MemPage.nFree + catchsql {PRAGMA quick_check} db2 +} {0 {{*** in database main *** +Tree 1 page 1: free space corruption}}} do_test corrupt2-1.5 { db2 close @@ -109,7 +109,7 @@ do_test corrupt2-1.5 { forcedelete corrupt.db-journal forcecopy test.db corrupt.db set f [open corrupt.db RDWR] - fconfigure $f -encoding binary + fconfigure $f -translation binary seek $f 101 start puts -nonewline $f "\x00\xC8" seek $f 200 start @@ -118,11 +118,9 @@ do_test corrupt2-1.5 { close $f sqlite3 db2 corrupt.db - catchsql " - $::presql - SELECT * FROM sqlite_master; - " db2 -} {1 {database disk image is malformed}} + catchsql {PRAGMA quick_check} db2 +} {0 {{*** in database main *** +Tree 1 page 1: free space corruption}}} db2 close # Corrupt a database by having 2 indices of the same name: @@ -133,6 +131,7 @@ do_test corrupt2-2.1 { forcecopy test.db corrupt.db sqlite3 db2 corrupt.db + sqlite3_db_config db2 DEFENSIVE 0 execsql " $::presql CREATE INDEX a1 ON abc(a); @@ -180,7 +179,7 @@ do_test corrupt2-3.1 { # of the DROP TABLE operation below. # set fd [open corrupt.db r+] - fconfigure $fd -encoding binary -translation binary + fconfigure $fd -translation binary seek $fd [expr 1024*3 + 12] set zCelloffset [read $fd 2] binary scan $zCelloffset S iCelloffset @@ -228,7 +227,7 @@ do_test corrupt2-5.1 { # This block links a page from table t2 into the t1 table structure. # set fd [open corrupt.db r+] - fconfigure $fd -encoding binary -translation binary + fconfigure $fd -translation binary seek $fd [expr 1024 + 12] set zCelloffset [read $fd 2] binary scan $zCelloffset S iCelloffset @@ -249,8 +248,8 @@ do_test corrupt2-5.1 { } set result } {{*** in database main *** -On tree page 2 cell 0: 2nd reference to page 10 -Page 4 is never used}} +Tree 2 page 2 cell 0: 2nd reference to page 10 +Page 4: never used}} db2 close @@ -265,6 +264,7 @@ proc corruption_test {args} { forcedelete corrupt.db-journal sqlite3 db corrupt.db + sqlite3_db_config db DEFENSIVE 0 db eval $::presql eval $A(-tclprep) db eval $A(-sqlprep) @@ -346,27 +346,29 @@ ifcapable autovacuum { } {1 {database disk image is malformed}} } - corruption_test -sqlprep { - PRAGMA auto_vacuum = 1; - PRAGMA page_size = 1024; - CREATE TABLE t1(a INTEGER PRIMARY KEY, b); - INSERT INTO t1 VALUES(1, randomblob(2500)); - DELETE FROM t1 WHERE a = 1; - } -corrupt { - set nAppend [expr 1024*207 - [file size corrupt.db]] - set fd [open corrupt.db r+] - seek $fd 0 end - puts -nonewline $fd [string repeat x $nAppend] - close $fd - hexio_write corrupt.db 28 00000000 - } -test { - do_test corrupt2-6.4 { - catchsql " - $::presql - BEGIN EXCLUSIVE; - COMMIT; - " - } {1 {database disk image is malformed}} + if {![nonzero_reserved_bytes]} { + corruption_test -sqlprep { + PRAGMA auto_vacuum = 1; + PRAGMA page_size = 1024; + CREATE TABLE t1(a INTEGER PRIMARY KEY, b); + INSERT INTO t1 VALUES(1, randomblob(2500)); + DELETE FROM t1 WHERE a = 1; + } -corrupt { + set nAppend [expr 1024*207 - [file size corrupt.db]] + set fd [open corrupt.db r+] + seek $fd 0 end + puts -nonewline $fd [string repeat x $nAppend] + close $fd + hexio_write corrupt.db 28 00000000 + } -test { + do_test corrupt2-6.4 { + catchsql " + $::presql + BEGIN EXCLUSIVE; + COMMIT; + " + } {1 {database disk image is malformed}} + } } } @@ -390,7 +392,7 @@ corruption_test -sqlprep $sqlprep -corrupt { # 0x0D (interpreted by SQLite as "leaf page of a table B-Tree"). # set fd [open corrupt.db r+] - fconfigure $fd -translation binary -encoding binary + fconfigure $fd -translation binary seek $fd [expr 1024*2 + 8] set zRightChild [read $fd 4] binary scan $zRightChild I iRightChild @@ -408,7 +410,7 @@ corruption_test -sqlprep $sqlprep -corrupt { # The corruption is detected as part of an OP_Prev opcode. # set fd [open corrupt.db r+] - fconfigure $fd -translation binary -encoding binary + fconfigure $fd -translation binary seek $fd [expr 1024*2 + 12] set zCellOffset [read $fd 2] binary scan $zCellOffset S iCellOffset @@ -429,7 +431,7 @@ corruption_test -sqlprep $sqlprep -corrupt { # 0x0A (interpreted by SQLite as "leaf page of an index B-Tree"). # set fd [open corrupt.db r+] - fconfigure $fd -translation binary -encoding binary + fconfigure $fd -translation binary seek $fd [expr 1024*1 + 8] set zRightChild [read $fd 4] binary scan $zRightChild I iRightChild @@ -457,7 +459,7 @@ corruption_test -sqlprep { CREATE TABLE x6(a, b, c); CREATE TABLE xD(a, b, c); CREATE TABLE xJ(a, b, c); } -corrupt { set fd [open corrupt.db r+] - fconfigure $fd -translation binary -encoding binary + fconfigure $fd -translation binary seek $fd 108 set zRightChild [read $fd 4] binary scan $zRightChild I iRightChild @@ -589,7 +591,7 @@ do_test 14.2 { do_execsql_test 14.3 { PRAGMA integrity_check; } {{*** in database main *** -Main freelist: free-page count in header is too small}} +Freelist: size is 3 but should be 2}} # Use 2 of the free pages on the free-list. # @@ -601,7 +603,7 @@ do_execsql_test 14.4 { do_execsql_test 14.5 { PRAGMA integrity_check; } {{*** in database main *** -Page 3 is never used}} +Freelist: size is 1 but should be 0}} finish_test diff --git a/test/corrupt3.test b/test/corrupt3.test index 436a466189..691302f7af 100644 --- a/test/corrupt3.test +++ b/test/corrupt3.test @@ -18,10 +18,9 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl -# Do not use a codec for tests in this file, as the database file is -# manipulated directly using tcl scripts (using the [hexio_write] command). -# -do_not_use_codec +# This module uses hard-coded offsets which do not work if the reserved_bytes +# value is nonzero. +if {[nonzero_reserved_bytes]} {finish_test; return;} # These tests deal with corrupt database files # @@ -68,8 +67,7 @@ do_test corrupt3-1.5 { integrity_check corrupt3-1.6 # Make the overflow chain loop back on itself. See if the -# corruption is detected. (Actually, the last pointer in -# an overflow chain is ignored, so this is not an error.) +# corruption is detected. # do_test corrupt3-1.7 { db close @@ -79,7 +77,12 @@ do_test corrupt3-1.7 { SELECT x FROM t1 } } [list 0 $bigstring] -integrity_check corrupt3-1.8 +do_test corrupt3-1.8 { + catchsql { + PRAGMA integrity_check + } +} {0 {{*** in database main *** +Tree 2 page 2 cell 0: 2nd reference to page 3}}} # Change the pointer for the first page of the overflow # change to be a non-existant page. @@ -91,14 +94,14 @@ do_test corrupt3-1.9 { catchsql { SELECT substr(x,1,10) FROM t1 } -} [list 0 0123456789] +} [list 1 {database disk image is malformed}] do_test corrupt3-1.10 { catchsql { PRAGMA integrity_check } } {0 {{*** in database main *** -On tree page 2 cell 0: invalid page number 4 -Page 3 is never used}}} +Tree 2 page 2 cell 0: invalid page number 4 +Page 3: never used}}} do_test corrupt3-1.11 { db close hexio_write test.db 2044 [hexio_render_int32 0] @@ -112,7 +115,7 @@ do_test corrupt3-1.12 { PRAGMA integrity_check } } {0 {{*** in database main *** -On tree page 2 cell 0: 1 of 1 pages missing from overflow list starting at 0 -Page 3 is never used}}} +Tree 2 page 2 cell 0: overflow list length is 0 but should be 1 +Page 3: never used}}} finish_test diff --git a/test/corrupt4.test b/test/corrupt4.test index 24db60fd52..544ac33053 100644 --- a/test/corrupt4.test +++ b/test/corrupt4.test @@ -13,15 +13,14 @@ # This file implements tests to make sure SQLite does not crash or # segfault if it sees a corrupt database file. # -# $Id: corrupt4.test,v 1.1 2007/09/07 14:32:07 drh Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl +set testprefix corrupt4 -# Do not use a codec for tests in this file, as the database file is -# manipulated directly using tcl scripts (using the [hexio_write] command). -# -do_not_use_codec +# This module uses hard-coded offsets which do not work if the reserved_bytes +# value is nonzero. +if {[nonzero_reserved_bytes]} {finish_test; return;} # These tests deal with corrupt database files # @@ -80,4 +79,71 @@ do_test corrupt4-1.4 { } } {1 {database disk image is malformed}} +#------------------------------------------------------------------------- + +reset_db +do_execsql_test 2.0 { + PRAGMA page_size = 512; + CREATE TABLE t1(a, b, c); +} + +# Create a database with a schema so large that the root of the +# sqlite_schema table is the grandparent of its leaves. +# +set nView 1000 +do_test 2.1 { + execsql BEGIN + for {set ii 0} {$ii<$nView} {incr ii} { + execsql " CREATE VIEW v$ii AS SELECT a, b, c FROM t1 " + } + execsql COMMIT +} {} +db close + +proc get2byte {fd offset} { + seek $fd $offset + set bin [read $fd 2] + binary scan $bin S val + set val +} +proc get4byte {fd offset} { + seek $fd $offset + set bin [read $fd 4] + binary scan $bin I val + set val +} +proc put4byte {fd offset val} { + seek $fd $offset + set bin [binary format I $val] + puts -nonewline $fd $bin +} + +# Page 1 is now the grandparent of its leaves. Corrupt the database by setting +# the second rightmost child page number of page 1 to 1. +# +set fd [open test.db r+] +fconfigure $fd -translation binary +set nChild [get2byte $fd 103] +set offChild [get2byte $fd [expr 100+12+($nChild-2)*2]] +set pgnoChild [get4byte $fd $offChild] +put4byte $fd $offChild 1 +close $fd + +if {![info exists ::G(perm:presql)]} { + sqlite3 db test.db + + do_catchsql_test 2.2 { + PRAGMA writable_schema = 1; + SELECT * FROM sqlite_schema; + } {1 {database disk image is malformed}} + + do_test 2.3 { + list [catch { + for {set ii $nView} {$ii<$nView*2} {incr ii} { + execsql "INSERT INTO sqlite_master VALUES(1, 2, 3, 4, 5)" + } + } msg] $msg + } {1 {database disk image is malformed}} +} + finish_test diff --git a/test/corrupt5.test b/test/corrupt5.test index 3f5099630e..75097668c7 100644 --- a/test/corrupt5.test +++ b/test/corrupt5.test @@ -33,6 +33,7 @@ ifcapable !pager_pragmas { # Create a database with a freelist containing at least two pages. # do_test corrupt5-1.1 { + sqlite3_db_config db DEFENSIVE 0 execsql { CREATE TABLE t1(a,b,c); CREATE INDEX i1 ON t1(a,b); diff --git a/test/corrupt6.test b/test/corrupt6.test index 7d90c4a3ba..dd773c9265 100644 --- a/test/corrupt6.test +++ b/test/corrupt6.test @@ -19,10 +19,9 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl -# Do not use a codec for tests in this file, as the database file is -# manipulated directly using tcl scripts (using the [hexio_write] command). -# -do_not_use_codec +# This module uses hard-coded offsets which do not work if the reserved_bytes +# value is nonzero. +if {[nonzero_reserved_bytes]} {finish_test; return;} # These tests deal with corrupt database files # diff --git a/test/corrupt7.test b/test/corrupt7.test index 7ebebd94e7..b62515b784 100644 --- a/test/corrupt7.test +++ b/test/corrupt7.test @@ -19,10 +19,9 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl -# Do not use a codec for tests in this file, as the database file is -# manipulated directly using tcl scripts (using the [hexio_write] command). -# -do_not_use_codec +# This module uses hard-coded offsets which do not work if the reserved_bytes +# value is nonzero. +if {[nonzero_reserved_bytes]} {finish_test; return;} # These tests deal with corrupt database files # @@ -71,14 +70,14 @@ do_test corrupt7-2.1 { sqlite3 db test.db db eval {PRAGMA integrity_check(1)} } {{*** in database main *** -On tree page 2 cell 15: Offset 65457 out of range 945..1020}} +Tree 2 page 2 cell 15: Offset 65457 out of range 945..1020}} do_test corrupt7-2.2 { db close hexio_write test.db 1062 04 sqlite3 db test.db db eval {PRAGMA integrity_check(1)} } {{*** in database main *** -On tree page 2 cell 15: Offset 1201 out of range 945..1020}} +Tree 2 page 2 cell 15: Offset 1201 out of range 945..1020}} # The code path that was causing the buffer overrun that this test # case was checking for was removed. diff --git a/test/corruptA.test b/test/corruptA.test index bb9912bd2b..12d918615f 100644 --- a/test/corruptA.test +++ b/test/corruptA.test @@ -53,7 +53,7 @@ do_test corruptA-2.1 { hexio_write test.db 19 $unreadable_version ;# the read format number sqlite3 db test.db catchsql {SELECT * FROM t1} -} {1 {file is encrypted or is not a database}} +} {1 {file is not a database}} do_test corruptA-2.2 { db close @@ -61,7 +61,7 @@ do_test corruptA-2.2 { hexio_write test.db 21 41 ;# max embedded payload fraction sqlite3 db test.db catchsql {SELECT * FROM t1} -} {1 {file is encrypted or is not a database}} +} {1 {file is not a database}} do_test corruptA-2.3 { db close @@ -69,7 +69,7 @@ do_test corruptA-2.3 { hexio_write test.db 22 1f ;# min embedded payload fraction sqlite3 db test.db catchsql {SELECT * FROM t1} -} {1 {file is encrypted or is not a database}} +} {1 {file is not a database}} do_test corruptA-2.4 { db close @@ -77,7 +77,7 @@ do_test corruptA-2.4 { hexio_write test.db 23 21 ;# min leaf payload fraction sqlite3 db test.db catchsql {SELECT * FROM t1} -} {1 {file is encrypted or is not a database}} +} {1 {file is not a database}} finish_test diff --git a/test/corruptC.test b/test/corruptC.test index 80c3c09f61..bf324c120f 100644 --- a/test/corruptC.test +++ b/test/corruptC.test @@ -34,9 +34,9 @@ database_may_be_corrupt # Construct a compact, dense database for testing. # do_test corruptC-1.1 { + sqlite3_db_config db LEGACY_FILE_FORMAT 1 execsql { PRAGMA auto_vacuum = 0; - PRAGMA legacy_file_format=1; BEGIN; CREATE TABLE t1(x,y); INSERT INTO t1 VALUES(1,1); @@ -97,9 +97,16 @@ do_test corruptC-2.1 { sqlite3 db test.db catchsql {PRAGMA integrity_check} -} {1 {database disk image is malformed}} +} {0 {{*** in database main *** +Tree 3 page 3: free space corruption} {wrong # of entries in index t1i1}}} # test that a corrupt content offset size is handled (seed 5649) +# +# Update 2016-12-27: As of check-in [0b86fbca66] "In sqlite3BtreeInsert() when +# replacing a re-existing row, try to overwrite the cell directly rather than +# deallocate and reallocate the cell" on 2016-12-09, this test case no longer +# detects the offset size problem during the UPDATE. We have to run a subsequent +# integrity_check to see it. do_test corruptC-2.2 { db close forcecopy test.bu test.db @@ -117,8 +124,9 @@ do_test corruptC-2.2 { hexio_write test.db 3746 [format %02x 0x9a] sqlite3 db test.db - catchsql {UPDATE t1 SET y=1} -} {1 {database disk image is malformed}} + db eval {UPDATE t1 SET y=1} + db eval {PRAGMA integrity_check} +} {/Offset .* out of range/} # test that a corrupt free cell size is handled (seed 13329) do_test corruptC-2.3 { @@ -157,7 +165,7 @@ do_test corruptC-2.5 { catchsql {BEGIN; UPDATE t2 SET y='abcdef-uvwxyz'; ROLLBACK;} catchsql {PRAGMA integrity_check} } {0 {{*** in database main *** -Page 4: btreeInitPage() returns error code 11}}} +Tree 4 page 4 cell 19: Extends off end of page} {database disk image is malformed}}} # {0 {{*** in database main *** # Corruption detected in cell 710 on page 4 diff --git a/test/corruptD.test b/test/corruptD.test index 63474584c3..381e52c808 100644 --- a/test/corruptD.test +++ b/test/corruptD.test @@ -111,8 +111,9 @@ proc restore_file {} { do_test corruptD-1.1.1 { incr_change_counter hexio_write test.db [expr 1024+1] FFFF - catchsql { SELECT * FROM t1 ORDER BY rowid } -} {1 {database disk image is malformed}} + catchsql { PRAGMA quick_check } +} {0 {{*** in database main *** +Tree 2 page 2: free space corruption} {wrong # of entries in index i1}}} do_test corruptD-1.1.2 { incr_change_counter hexio_write test.db [expr 1024+1] [hexio_render_int32 1021] diff --git a/test/corruptE.test b/test/corruptE.test index 78cabbec8e..8c55623ea4 100644 --- a/test/corruptE.test +++ b/test/corruptE.test @@ -18,10 +18,9 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl -# Do not use a codec for tests in this file, as the database file is -# manipulated directly using tcl scripts (using the [hexio_write] command). -# -do_not_use_codec +# This module uses hard-coded offsets which do not work if the reserved_bytes +# value is nonzero. +if {[nonzero_reserved_bytes]} {finish_test; return;} # These tests deal with corrupt database files # @@ -37,9 +36,9 @@ ifcapable oversize_cell_check { # Construct a compact, dense database for testing. # do_test corruptE-1.1 { + sqlite3_db_config db LEGACY_FILE_FORMAT 1 execsql { PRAGMA auto_vacuum = 0; - PRAGMA legacy_file_format=1; BEGIN; CREATE TABLE t1(x,y); INSERT INTO t1 VALUES(1,1); diff --git a/test/corruptG.test b/test/corruptG.test index af920edf41..94480340a7 100644 --- a/test/corruptG.test +++ b/test/corruptG.test @@ -14,10 +14,9 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl set testprefix corruptG -# Do not use a codec for tests in this file, as the database file is -# manipulated directly using tcl scripts (using the [hexio_write] command). -# -do_not_use_codec +# This module uses hard-coded offsets which do not work if the reserved_bytes +# value is nonzero. +if {[nonzero_reserved_bytes]} {finish_test; return;} # These tests deal with corrupt database files # diff --git a/test/corruptH.test b/test/corruptH.test index 0e1a1d4429..9ba7522422 100644 --- a/test/corruptH.test +++ b/test/corruptH.test @@ -14,10 +14,10 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl set testprefix corruptH -# Do not use a codec for tests in this file, as the database file is -# manipulated directly using tcl scripts (using the [hexio_write] command). -# -do_not_use_codec +# This module uses hard-coded offsets which do not work if the reserved_bytes +# value is nonzero. +if {[nonzero_reserved_bytes]} {finish_test; return;} + database_may_be_corrupt # The corruption migrations tested by the code in this file are not detected diff --git a/test/corruptI.test b/test/corruptI.test index 9f46efb744..65ef376258 100644 --- a/test/corruptI.test +++ b/test/corruptI.test @@ -19,10 +19,10 @@ if {[permutation]=="mmap"} { return } -# Do not use a codec for tests in this file, as the database file is -# manipulated directly using tcl scripts (using the [hexio_write] command). -# -do_not_use_codec +# This module uses hard-coded offsets which do not work if the reserved_bytes +# value is nonzero. +if {[nonzero_reserved_bytes]} {finish_test; return;} + database_may_be_corrupt # Initialize the database. @@ -123,18 +123,13 @@ do_execsql_test 4.0 { set root [db one {SELECT rootpage FROM sqlite_master}] set offset [expr ($root-1) * 65536] -ifcapable oversize_cell_check { - set res {1 {database disk image is malformed}} -} else { - set res {0 {}} -} do_test 4.1 { db close hexio_write test.db [expr $offset + 8 + 2] 0000 hexio_write test.db [expr $offset + 5] 0000 sqlite3 db test.db catchsql { DELETE FROM t1 WHERE a=0 } -} $res +} {1 {database disk image is malformed}} #------------------------------------------------------------------------- @@ -222,6 +217,7 @@ do_execsql_test 7.0 { INSERT INTO t1 VALUES('c', 'A'); SELECT name FROM sqlite_master; } {t1 sqlite_autoindex_t1_1} +sqlite3_db_config db DEFENSIVE 0 do_execsql_test 7.1 { PRAGMA writable_schema = 1; DELETE FROM sqlite_master WHERE name = 'sqlite_autoindex_t1_1'; diff --git a/test/corruptJ.test b/test/corruptJ.test index c08e628e2d..732adb085c 100644 --- a/test/corruptJ.test +++ b/test/corruptJ.test @@ -22,10 +22,10 @@ if {[permutation]=="mmap"} { return } -# Do not use a codec for tests in this file, as the database file is -# manipulated directly using tcl scripts (using the [hexio_write] command). -# -do_not_use_codec +# This module uses hard-coded offsets which do not work if the reserved_bytes +# value is nonzero. +if {[nonzero_reserved_bytes]} {finish_test; return;} + database_may_be_corrupt # Initialize the database. diff --git a/test/corruptK.test b/test/corruptK.test new file mode 100644 index 0000000000..0bdb3c45b5 --- /dev/null +++ b/test/corruptK.test @@ -0,0 +1,234 @@ +# 2017-03-03 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix corruptK + +if {[permutation]=="mmap"} { + finish_test + return +} + +# This module uses hard-coded offsets which do not work if the reserved_bytes +# value is nonzero. +if {[nonzero_reserved_bytes]} {finish_test; return;} +database_may_be_corrupt + +# Initialize the database. +# +do_execsql_test 1.1 { + PRAGMA page_size=1024; + PRAGMA auto_vacuum=0; + CREATE TABLE t1(x); + + INSERT INTO t1 VALUES(randomblob(20)); + INSERT INTO t1 VALUES(randomblob(100)); -- make this into a free slot + INSERT INTO t1 VALUES(randomblob(27)); -- this one will be corrupt + INSERT INTO t1 VALUES(randomblob(800)); + + DELETE FROM t1 WHERE rowid=2; -- free the 100 byte slot + PRAGMA page_count +} {2} + + +# Corrupt the database so that the blob stored immediately before +# the free slot (rowid==3) has an overlarge length field. So that +# we can use sqlite3_blob_write() to manipulate the size field of +# the free slot. +# +# Then use sqlite3_blob_write() to set the size of said free slot +# to 24 bytes (instead of the actual 100). +# +# Then use the new 24 byte slot. Leaving the in-memory version of +# the page with zero free slots and a large nFree value. Then try +# to allocate another slot to get to defragmentPage(). +# +do_test 1.2 { + db close + hexio_write test.db [expr 1024 + 0x360] 21 + hexio_write test.db [expr 1024 + 0x363] [format %x [expr 31*2 + 12]] + sqlite3 db test.db + + set fd [db incrblob t1 x 3] + fconfigure $fd -translation binary + seek $fd 30 + puts -nonewline $fd "\x18" + close $fd +} {} +do_execsql_test 1.3 { + INSERT INTO t1 VALUES(randomblob(20)); +} + +# This test no longer functions due to the deferred computation of +# MemPage.nFree. +# +if 0 { +do_catchsql_test 1.4 { + INSERT INTO t1 VALUES(randomblob(90)); +} {1 {database disk image is malformed}} +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 2.1 { + PRAGMA page_size=1024; + PRAGMA auto_vacuum=0; + CREATE TABLE t1(x); + + INSERT INTO t1 VALUES(randomblob(20)); + INSERT INTO t1 VALUES(randomblob(20)); -- free this one + INSERT INTO t1 VALUES(randomblob(20)); + INSERT INTO t1 VALUES(randomblob(20)); -- and this one + INSERT INTO t1 VALUES(randomblob(20)); -- corrupt this one. + + DELETE FROM t1 WHERE rowid IN(2, 4); + PRAGMA page_count +} {2} + +do_test 2.2 { + db close + hexio_write test.db [expr 1024 + 0x388] 53 + hexio_write test.db [expr 1024 + 0x38A] 03812C + + sqlite3 db test.db + set fd [db incrblob t1 x 5] + fconfigure $fd -translation binary + + seek $fd 22 + puts -nonewline $fd "\x5d" + close $fd +} {} + +do_catchsql_test 2.3 { + INSERT INTO t1 VALUES(randomblob(900)); +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- + +ifcapable vtab { +if {[permutation]!="inmemory_journal"} { + + proc hex2blob {hex} { + # Split on newlines: + set bytes [list] + foreach l [split $hex "\n"] { + if {[string is space $l]} continue + set L [list] + foreach b [split $l] { + if {[string is xdigit $b] && [string length $b]==2} { + lappend L [expr "0x$b"] + } + } + if {[llength $L]!=16} { + error "Badly formed hex (1)" + } + set bytes [concat $bytes $L] + } + + binary format c* $bytes + } + + reset_db + db func hex2blob hex2blob + + do_execsql_test 3.1 { + PRAGMA page_size=1024; + CREATE TABLE t1(a, b, c); + CREATE TABLE t2(a, b, c); + CREATE TABLE t3(a, b, c); + CREATE TABLE t4(a, b, c); + CREATE TABLE t5(a, b, c); + } + sqlite3_db_config db DEFENSIVE 0 + do_execsql_test 3.2 { + UPDATE sqlite_dbpage SET data = hex2blob(' + 000: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. + 010: 04 00 01 01 20 40 20 20 00 00 3e d9 00 00 00 06 .... @ ..>..... + 020: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 04 ................ + 030: 0f 00 00 00 00 00 00 00 00 00 00 01 00 00 83 00 ................ + 040: 00 00 00 00 00 00 00 00 00 00 00 00 00 38 00 00 .............8.. + 050: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 3e d9 ..............>. + 060: 00 2d e6 07 0d 00 00 00 01 03 a0 00 03 e0 00 00 .-.............. + 070: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ + 080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ + 090: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ + 0a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ + 0b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ + 0c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ + 0d0: 00 00 00 00 00 c1 00 00 00 00 00 00 00 00 00 00 ................ + 0e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ + 0f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ + 100: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ + 110: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ + 120: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ + 130: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ + 140: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ + 150: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ + 160: 00 83 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ + 170: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ + 180: 00 00 00 00 00 00 00 00 00 00 07 00 30 00 00 00 ............0... + 190: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ + 1a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ + 1b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ + 1c0: 02 00 00 00 00 00 00 00 00 00 00 02 00 00 00 00 ................ + 1d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ + 1e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ + 1f0: 00 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ + 200: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ + 210: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ + 220: 00 00 0e 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ + 230: 0c 00 00 00 00 00 00 60 00 00 00 06 00 00 c3 00 .......`........ + 240: 00 06 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ + 250: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ + 260: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ + 270: 00 00 00 18 00 00 00 00 00 00 00 00 00 00 00 00 ................ + 280: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ + 290: 04 00 0e 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ + 2a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ + 2b0: 00 00 00 00 83 00 8c 00 00 00 00 00 00 00 00 00 ................ + 2c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ + 2d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ + 2e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ + 2f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ + 300: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ + 310: 00 78 00 00 00 00 00 00 00 00 00 00 00 00 70 00 .x............p. + 320: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ + 330: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ + 340: 00 00 00 04 00 00 00 00 00 00 00 00 00 00 00 00 ................ + 350: 00 00 00 00 00 68 00 00 00 00 00 00 00 00 00 00 .....h.......... + 360: 00 00 00 00 00 03 00 00 00 00 00 00 00 00 00 00 ................ + 370: 00 00 00 00 00 00 00 00 00 00 00 00 00 08 00 00 ................ + 380: 00 00 00 00 70 00 00 00 00 00 00 00 00 00 00 00 ....p........... + 390: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ + 3a0: 5e 01 07 17 1b 1b 01 81 13 74 61 62 6c 65 73 65 ^........tablese + 3b0: 6e 73 6f 32 73 73 65 6e 73 6f 72 73 02 43 52 45 nso2ssensors.CRE + 3c0: 41 54 45 20 54 41 42 4c 45 20 73 65 6e 73 6f 72 ATE TABLE sensor + 3d0: 73 20 0a 20 20 24 20 20 20 20 20 20 20 20 20 20 s . $ + 3e0: b8 6e 61 6d 65 21 74 65 78 74 2c 20 79 61 6c 20 .name!text, yal + 3f0: 72 65 61 6c 2c 20 74 69 6d 65 20 74 65 78 74 29 real, time text) + ') WHERE pgno=1 + } + + db close + sqlite3 db test.db + + do_catchsql_test 3.3 { + PRAGMA integrity_check; + } {1 {database disk image is malformed}} + +} ;# [permutation]!="inmemory_journal" +} ;# ifcapable vtab + + + +finish_test diff --git a/test/corruptL.test b/test/corruptL.test new file mode 100644 index 0000000000..52adf6fd72 --- /dev/null +++ b/test/corruptL.test @@ -0,0 +1,1593 @@ +# 2019-01-11 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix corruptL + +database_may_be_corrupt + +#------------------------------------------------------------------------- +reset_db +do_test 1.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 356352 pagesize 4096 filename crash-acaae0347204ae.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 d0 00 00 00 .....@ ........ +| 32: 40 00 ea 00 00 00 00 00 00 40 00 00 00 40 00 00 @........@...@.. +| 96: 00 00 00 00 0d 00 00 00 04 0e 9c 00 0f ad 0f 4f ...............O +| 112: 0e fc 0e 9c 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 3728: 00 00 00 00 00 00 00 00 00 00 00 00 5e 04 07 17 ............^... +| 3744: 1f 1f 01 81 0b 74 61 62 6c 65 74 31 5f 70 61 72 .....tablet1_par +| 3760: 65 6e 74 74 31 5f 70 61 72 65 6e 74 04 43 52 45 entt1_parent.CRE +| 3776: 41 54 45 20 54 41 42 4c 45 20 22 74 31 5f 70 61 ATE TABLE .t1_pa +| 3792: 72 65 6e 74 22 28 6e 6f 64 65 6e 6f 20 49 4e 54 rent.(nodeno INT +| 3808: 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 EGER PRIMARY KEY +| 3824: 2c 70 61 72 65 6e 74 6e 6f 64 65 29 51 03 06 17 ,parentnode)Q... +| 3840: 1b 1b 01 7b 74 61 62 6c 65 74 31 5f 6e 6f 64 65 ....tablet1_node +| 3856: 74 31 5f 6e 6f 64 65 03 43 52 45 41 54 45 20 54 t1_node.CREATE T +| 3872: 41 42 4c 45 20 22 74 31 5f 6e 6f 64 65 22 28 6e ABLE .t1_node.(n +| 3888: 6f 64 65 6e 6f 20 49 4e 54 45 47 45 52 20 50 52 odeno INTEGER PR +| 3904: 49 4d 41 52 59 20 4b 45 59 2c 64 61 74 61 29 5c IMARY KEY,data). +| 3920: 02 07 17 1d 1d 01 81 0b 74 61 62 6c 65 74 31 5f ........tablet1_ +| 3936: 72 6f 77 69 64 74 31 5f 72 6f 77 69 64 02 43 52 rowidt1_rowid.CR +| 3952: 45 41 54 45 20 54 41 42 4c 45 20 22 74 31 5f 72 EATE TABLE .t1_r +| 3968: 6f 77 69 64 22 28 72 6f 77 69 64 20 49 4e 54 45 owid.(rowid INTE +| 3984: 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c GER PRIMARY KEY, +| 4000: 6e 6f 64 65 6e 6f 2c 61 30 2c 61 31 29 51 01 07 nodeno,a0,a1)Q.. +| 4016: 17 11 11 08 81 0f 74 61 62 6c 65 74 31 74 31 43 ......tablet1t1C +| 4032: 52 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 REATE VIRTUAL TA +| 4048: 42 4c 45 20 74 31 20 55 53 49 4e 47 20 72 74 72 BLE t1 USING rtr +| 4064: 65 65 28 69 64 2c 78 30 20 50 52 49 4d 41 52 59 ee(id,x0 PRIMARY +| 4080: 20 4b 45 59 2c 70 61 72 65 6e 74 6e 6f 64 65 29 KEY,parentnode) +| page 2 offset 4096 +| 0: 51 03 06 17 1b 1b 01 7b 74 61 62 6c 65 74 31 5f Q.......tablet1_ +| 16: 6e 6f 64 65 74 31 5f 6e 6f 64 65 03 43 52 45 41 nodet1_node.CREA +| 32: 54 45 20 54 41 42 4c 45 20 22 74 31 5f 6e 6f 64 TE TABLE .t1_nod +| 48: 65 22 28 6e 6f 64 65 6e 6f 20 49 4e 54 45 47 45 e.(nodeno INTEGE +| 64: 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 64 61 R PRIMARY KEY,da +| 80: 74 61 29 5c 02 07 17 1d 1d 01 81 0b 74 61 62 6c ta).........tabl +| 96: 65 74 31 5f 72 6f 77 69 64 74 31 5f 72 6f 77 69 et1_rowidt1_rowi +| 112: 64 02 43 52 45 41 54 45 20 54 41 42 4c 45 00 00 d.CREATE TABLE.. +| 128: 01 0a 02 00 00 00 01 0e 0d 00 00 00 00 24 0e 0d .............$.. +| 144: 0c 1a 06 85 50 46 60 27 70 08 00 00 00 00 00 00 ....PF`'p....... +| 3824: 00 00 00 00 00 00 00 0d 0e 05 00 09 1d 00 74 6f ..............to +| 3840: 79 20 68 61 6c 66 10 0d 05 00 09 23 00 62 6f 74 y half.....#.bot +| 3856: 74 6f 6d 20 68 61 6c 66 0f 0c 05 00 09 21 00 72 tom half.....!.r +| 3872: 69 67 68 74 20 68 61 6c 66 0e 0b 05 00 09 1f 00 ight half....... +| 3888: 6c 65 66 74 20 43 15 f6 e6 f6 46 50 34 35 24 54 left C....FP45$T +| 3904: 15 44 52 05 44 14 24 c4 52 02 27 43 15 f6 e6 f6 .DR.D.$.R.'C.... +| 3920: 46 52 22 8e 6f 64 65 6e 6f 20 49 4e 54 45 47 45 FR..odeno INTEGE +| 3936: 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 64 61 R PRIMARY KEY,da +| 3952: 74 61 29 5c 02 07 17 1d 1d 01 81 0b 74 61 62 6c ta).........tabl +| 3968: 65 74 31 5f 72 6f 74 74 6f 6d 20 65 64 67 65 0f et1_rottom edge. +| 3984: 07 05 00 09 21 00 72 69 67 68 74 20 65 64 67 65 ....!.right edge +| 4000: 0e 06 05 00 09 1f 00 6c 65 66 74 20 65 64 67 65 .......left edge +| 4016: 0b 05 05 00 09 19 00 63 65 6e 74 65 72 17 04 05 .......center... +| 4032: 00 09 31 00 75 70 70 65 72 2d 72 69 67 68 74 20 ..1.upper-right +| 4048: 63 6f 72 6e 65 72 17 03 05 00 09 31 00 6c 6f 77 corner.....1.low +| 4064: 65 72 2d 72 69 67 68 74 20 63 6f 72 6e 65 72 16 er-right corner. +| 4080: 02 05 00 09 2f 00 75 70 70 65 72 2d 6c 65 66 74 ..../.upper-left +| page 3 offset 8192 +| 0: 20 63 6f 72 6e 65 72 16 01 05 00 09 2f 01 8c 6f corner...../..o +| 16: 77 65 72 2d 6c 53 51 4c 69 74 65 20 66 6f 72 6d wer-lSQLite form +| 32: 61 74 20 33 00 10 00 01 01 00 40 20 20 00 00 00 at 3......@ ... +| 48: 00 00 00 00 2f 00 00 0d eb 13 00 00 00 03 00 00 ..../........... +| 64: 00 04 00 00 00 00 00 00 00 06 00 00 00 01 00 00 ................ +| 80: 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 ................ +| page 6 offset 20480 +| 128: 00 00 00 00 00 00 00 00 97 3d 04 ae 7c 01 00 00 .........=..|... +| 624: 00 00 00 00 00 00 21 97 3d 04 ae 7c 01 00 00 00 ......!.=..|.... +| 1120: 00 00 00 00 00 20 97 3d 04 ae 7c 01 00 00 00 00 ..... .=..|..... +| 1616: 00 00 00 00 1f 97 3d 04 ae 7c 01 00 00 00 00 00 ......=..|...... +| 2112: 00 00 00 1e 97 3d 04 ae 7c 01 00 00 00 00 00 00 .....=..|....... +| 2608: 00 00 1d 97 d3 d0 4a e7 c0 00 00 00 00 00 00 00 ......J......... +| 3088: 00 00 00 00 00 00 00 00 00 00 00 00 01 f3 00 00 ................ +| 3600: 23 97 3d 04 ae 7c 01 00 00 00 00 00 00 00 00 00 #.=..|.......... +| 4080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 26 ...............& +| page 8 offset 28672 +| 0: 0d 00 00 00 01 04 30 00 04 30 00 00 00 00 00 00 ......0..0...... +| 1072: 97 4d 1e 14 00 ae 7c 00 00 00 00 00 00 00 00 00 .M....|......... +| 1088: 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 03 ................ +| page 10 offset 36864 +| 0: 0d 00 00 00 01 04 30 00 04 30 00 00 00 00 00 00 ......0..0...... +| 1072: 9a ee c1 80 fd 78 1f ce 1b ae eb b4 00 00 00 00 .....x.......... +| 1088: 13 20 ff 20 00 70 00 00 00 60 50 00 00 00 11 e0 . . .p...`P..... +| 1104: 00 00 00 70 00 00 00 60 50 05 35 14 c6 97 46 52 ...p...`P.5...FR +| 1120: 06 66 f7 26 d6 17 42 03 30 01 00 00 10 10 04 02 .f.&..B.0....... +| 1136: 02 00 00 00 00 00 00 00 00 40 00 00 00 00 00 00 .........@...... +| 1152: 00 00 00 00 00 40 00 00 00 40 00 00 00 00 00 00 .....@...@...... +| 4080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 05 ................ +| page 12 offset 45056 +| 0: 0d 00 00 00 01 04 30 00 04 30 e1 b4 30 97 4d 46 ......0..0..0.MF +| 16: 14 00 ae 7c 00 00 00 00 00 00 00 03 00 00 43 00 ...|..........C. +| page 47 offset 188416 +| 2512: 00 00 00 00 00 00 00 00 be 00 00 00 00 00 00 00 ................ +| page 87 offset 352256 +| 2512: 00 00 00 00 00 00 00 00 aa 00 00 00 00 00 00 00 ................ +| end crash-acaae0347204ae.db +}]} {} + +do_catchsql_test 1.1 { + PRAGMA cell_size_check = off; + DROP INDEX t1x1; +} {1 {database disk image is malformed}} + +do_catchsql_test 1.2 { + SELECT sum(s+length(b)) FROM t1 WHERE a IN (110,10,150) AND q IS NULL; +} {1 {database disk image is malformed}} + +do_catchsql_test 1.3 { + REINDEX t1; +} {1 {database disk image is malformed}} + +do_catchsql_test 1.4 { + PRAGMA integrity_check +} {1 {database disk image is malformed}} + + +#------------------------------------------------------------------------- +reset_db +do_test 2.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 20480 pagesize 4096 filename crash.txt.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 05 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 05 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 96: 00 00 00 00 0d 00 00 00 05 0e 55 00 0f 74 0f 3c ..........U..t.< +| 112: 0e f9 0e d1 0e 55 00 00 00 00 00 00 00 00 00 00 .....U.......... +| 3664: 00 00 00 00 00 7a 05 07 15 11 11 08 81 63 76 69 .....z.......cvi +| 3680: 65 77 76 31 76 31 43 52 45 41 54 45 20 56 49 45 ewv1v1CREATE VIE +| 3696: 57 20 76 31 28 78 2c 79 29 20 41 53 0a 53 45 4c W v1(x,y) AS.SEL +| 3712: 45 43 54 20 74 31 2e 62 2c 74 32 2e 62 20 46 52 ECT t1.b,t2.b FR +| 3728: 4f 4d 20 74 31 2c 74 32 20 57 48 45 52 45 20 74 OM t1,t2 WHERE t +| 3744: 31 2e 61 3d 74 32 2e 61 20 47 52 4f 55 50 20 42 1.a=t2.a GROUP B +| 3760: 59 20 31 20 48 41 56 49 4e 47 20 74 32 2e 63 20 Y 1 HAVING t2.c +| 3776: 4e 4f 54 20 4e 55 4c 4c 0a 4c 49 4d 49 54 20 31 NOT NULL.LIMIT 1 +| 3792: 30 26 04 06 17 11 11 01 39 74 61 62 6c 65 74 32 0&......9tablet2 +| 3808: 74 32 05 43 52 45 41 54 45 20 54 41 42 4c 45 20 t2.CREATE TABLE +| 3824: 74 32 28 61 2c 62 2c 63 29 41 03 06 17 15 11 01 t2(a,b,c)A...... +| 3840: 6b 69 6e 64 65 78 74 31 78 31 74 31 04 43 52 45 kindext1x1t1.CRE +| 3856: 41 54 45 20 49 4e 44 45 58 20 73 31 78 31 20 4f ATE INDEX s1x1 O +| 3872: 4e 20 74 31 28 64 29 20 57 48 45 52 45 20 65 65 N t1(d) WHERE ee +| 3888: 20 49 53 20 4e 4f 54 20 4e 55 4c 4c 36 02 06 17 IS NOT NULL6... +| 3904: 17 11 01 53 69 6e 64 65 78 74 31 61 62 63 74 31 ...Sindext1abct1 +| 3920: 03 43 52 45 41 54 45 20 49 4e 44 45 58 20 74 31 .CREATE INDEX t1 +| 3936: 61 62 63 20 4f 4e 20 74 31 28 61 2c 62 2c 63 2b abc ON t1(a,b,c+ +| 3952: 64 2b 65 29 81 09 01 07 17 11 11 01 81 7d 74 61 d+e)..........ta +| 3968: 62 6c 65 74 31 74 31 02 43 52 45 41 54 45 20 54 blet1t1.CREATE T +| 3984: 41 42 4c 45 20 74 31 28 61 2c 62 2c 63 2c 64 2c ABLE t1(a,b,c,d, +| 4000: 65 2c 66 2c 67 2c 68 2c 6a 2c 6a 6a 2c 6a 6a 6a e,f,g,h,j,jj,jjj +| 4016: 2c 6b 2c 61 61 2c 62 62 2c 63 63 2c 64 64 2c 65 ,k,aa,bb,cc,dd,e +| 4032: 65 20 44 45 46 41 55 4c 54 20 33 2e 31 34 2c 0a e DEFAULT 3.14,. +| 4048: 66 66 20 44 45 46 41 55 4c 54 28 27 68 69 63 63 ff DEFAULT('hicc +| 4064: 75 70 27 29 2c 67 67 20 4e 4f 54 20 4e 55 4c 4c up'),gg NOT NULL +| 4080: 20 44 45 46 41 55 4c 54 28 66 61 6c 73 65 29 29 DEFAULT(false)) +| page 2 offset 4096 +| 0: 0d 00 00 00 0a 0e 7b 00 0f dc 0f b6 0f 8f 0f 68 ...............h +| 16: 0f 41 0f 1a 0e f3 0e cb 0e a3 0e 7b 00 00 00 00 .A.............. +| 3696: 00 00 00 00 00 00 00 00 00 00 00 26 0a 14 01 01 ...........&.... +| 3712: 02 08 00 00 00 00 00 00 00 00 00 00 00 00 07 19 ................ +| 3728: 08 09 5a 00 b4 40 09 1e b8 51 eb 85 1f 68 69 63 ..Z..@...Q...hic +| 3744: 63 75 70 26 09 14 01 01 02 08 00 00 00 00 00 00 cup&............ +| 3760: 00 00 00 00 00 00 07 19 08 08 50 00 a0 40 09 1e ..........P..@.. +| 3776: b8 51 eb 85 1f 68 69 63 63 75 70 26 08 14 01 01 .Q...hiccup&.... +| 3792: 02 08 00 00 00 00 00 00 00 00 00 00 00 00 07 19 ................ +| 3808: 08 07 46 00 8c 40 09 1e b8 51 eb 85 1f 68 69 63 ..F..@...Q...hic +| 3824: 63 75 70 25 07 14 01 01 01 08 00 00 00 00 00 00 cup%............ +| 3840: 00 00 00 00 00 00 07 b9 08 06 3c 78 40 09 1e b8 ..........<x@... +| 3856: 51 eb 85 1f 68 69 63 63 75 70 25 06 14 01 00 01 Q...hiccup%..... +| 3872: 08 00 00 00 00 00 00 00 00 00 00 00 00 07 19 08 ................ +| 3888: 05 32 64 40 09 1e b8 51 eb 85 1f 68 69 63 63 75 .2d@...Q...hiccu +| 3904: 70 25 05 14 01 01 01 08 00 00 00 00 00 00 00 00 p%.............. +| 3920: 00 00 00 00 07 19 08 04 28 50 40 09 1e b8 51 eb ........(P@...Q. +| 3936: 85 1f 68 69 63 63 75 70 25 04 14 01 01 01 08 00 ..hiccup%....... +| 3952: 00 00 00 00 00 00 00 00 00 00 00 07 19 08 03 1e ................ +| 3968: 3c 40 09 1e b8 51 eb 85 1f 68 69 63 63 75 70 25 <@...Q...hiccup% +| 3984: 03 14 01 01 01 08 00 00 00 00 00 00 00 00 00 00 ................ +| 4000: 00 00 07 19 08 02 14 28 40 09 1e b8 51 eb 85 1f .......(@...Q... +| 4016: 68 69 63 63 75 70 24 02 14 09 01 01 08 00 00 00 hiccup$......... +| 4032: 00 00 00 00 00 00 00 00 00 07 19 08 0a 14 40 09 ..............@. +| 4048: 1e b8 51 eb 85 1f 68 69 63 63 75 70 22 01 14 08 ..Q...hiccup.... +| 4064: 08 08 07 ff ff ff ff ff ff 00 00 00 00 00 00 07 ................ +| 4080: 19 08 40 09 1e b8 51 eb 85 1f 68 69 63 63 75 70 ..@...Q...hiccup +| page 3 offset 8192 +| 0: 0a 00 00 00 0a 0f aa 00 0f fa 0f f2 0f e9 0f e0 ................ +| 16: 0f d7 0f ce 0f c5 0f bc 0e b3 0f aa 00 00 00 00 ................ +| 4000: 00 00 00 00 00 00 00 00 00 00 08 05 01 01 00 01 ................ +| 4016: 09 5a 0a 08 05 01 01 00 01 08 50 09 08 05 01 01 .Z........P..... +| 4032: 00 01 07 46 08 08 05 01 01 00 01 06 3c 07 08 05 ...F........<... +| 4048: 01 01 00 01 05 32 06 08 05 01 01 00 01 04 28 05 .....2........(. +| 4064: 08 05 01 01 00 01 03 1e 04 08 05 01 01 00 01 02 ................ +| 4080: 14 03 07 05 09 01 00 01 0a 02 05 05 08 08 00 09 ................ +| page 4 offset 12288 +| 0: 0a 00 00 00 0a 0f cf 00 0f fc 0f f7 0f f2 0f ed ................ +| 16: 0f e8 0f e3 0f de 0f d9 0f d4 0f cf 00 00 00 00 ................ +| 4032: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 04 ................ +| 4048: 03 08 01 0a 04 03 08 01 09 04 03 08 01 08 04 03 ................ +| 4064: 08 01 07 04 03 08 01 06 04 03 08 01 05 04 03 08 ................ +| 4080: 01 04 04 03 08 01 13 04 03 08 01 02 03 03 08 09 ................ +| page 5 offset 16384 +| 0: 0d 00 00 00 00 10 00 00 00 00 00 00 00 00 00 00 ................ +| end crash.txt.db +}]} {} + +do_execsql_test 2.1 { + PRAGMA writable_schema=ON; -- bypass improved sqlite_master consistency checking + INSERT INTO t1(b) VALUES(X'a0fee3669f9fddefc5cba913e4225d4b6ce2b04f26b87fad3ee6f9b7d90a1ea62a169bf41e5d32707a6ca5c3d05e4bde05c9d89eaaa8c50e74333d2e9fcd7dfe95528a3a016aac1102d825c5cd70cf99d8a88e0ea7f798d4334386518b7ad359beb168b93aba059a2a3bd93112d65b44c12b9904ea786b204d80531cdf0504bf9b203dbe927061974caf7b9f30cbc3397b61f802e732012a6663d41c3607d6f1c0dbcfd489adac05ca500c0b04439d894cd93a840159225ef73b627e178b9f84b3ffe66cf22a963a8368813ff7961fc47f573211ccec95e0220dcbb3bf429f4a50ba54d7a53784ac51bfef346e6ac8ae0d0e7c3175946e62ba2b'); +} + +do_catchsql_test 2.2 { + SELECT b,c FROM t1 ORDER BY a; +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 3.0 { + CREATE TABLE t1(a, b, c, d INTEGER PRIMARY KEY); + CREATE TABLE t2(a, b, c, d INTEGER PRIMARY KEY); + + INSERT INTO t1(a, b, c, d) VALUES (1, 2, 3, 100), (4, 5, 6, 101); + INSERT INTO t2(a, b, c, d) VALUES (1, 100, 3, 1000), (4, 101, 6, 1001); + + CREATE INDEX t1a ON t1(a); + CREATE INDEX t2a ON t2(a, b, c); + + PRAGMA writable_schema = 1; + UPDATE sqlite_master SET sql = 'CREATE INDEX t2a ON t2(a)' WHERE name='t2a'; +} + +db close +sqlite3 db test.db + +do_catchsql_test 3.1 { + INSERT INTO t1 SELECT * FROM t2; +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +reset_db +do_test 4.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 4096 pagesize 512 filename crash-6b48ba69806134.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 02 00 01 01 00 40 20 20 00 ff ff ff ff 00 00 07 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 08 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 05 00 eb 00 01 00 00 00 00 ................ +| 80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0c ................ +| 96: 00 2e 2c 50 0d 00 00 00 06 01 06 00 01 da 01 b0 ..,P............ +| 112: 05 56 01 86 01 2a 01 06 00 00 00 00 00 00 00 00 .V...*.......... +| 128: 00 ff 00 00 ff ff ff e1 00 00 00 00 00 00 00 00 ................ +| 144: 00 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 ................ +| 160: 00 00 00 00 00 00 00 00 f2 00 00 00 00 00 00 00 ................ +| 176: 00 00 f9 ff ff ff ff ff ff ff 00 00 00 00 00 fb ................ +| 208: 00 00 00 00 00 00 00 00 1e 00 00 00 fe 00 00 00 ................ +| 224: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ca 00 ................ +| 256: 00 00 00 00 ef ff 22 07 06 17 11 11 01 31 74 61 .............1ta +| 272: 62 6c 65 74 38 38 74 04 43 52 45 41 54 45 20 54 blet88t.CREATE T +| 288: 41 42 4c 45 20 74 34 28 87 29 2a 06 06 17 13 11 ABLE t4(.)*..... +| 304: 01 3f 69 4f 64 65 78 74 33 78 74 33 05 43 52 45 .?iOdext3xt3.CRE +| 320: 41 54 45 20 49 6e 44 45 58 20 74 33 78 20 4f 4e ATE InDEX t3x ON +| 336: 20 74 33 28 78 29 2e 04 06 17 15 11 01 45 69 6e t3(x).......Ein +| 352: 64 65 2e 74 32 63 64 74 3d 05 43 52 45 41 54 45 de.t2cdt=.CREATE +| 368: 20 49 4e 44 45 58 20 74 32 63 64 20 4f 4e 20 74 INDEX t2cd ON t +| 384: 32 28 0a 0c 44 29 28 05 06 17 11 11 01 3d 74 61 2(..D)(......=ta +| 400: 62 6c 65 d4 33 74 33 04 43 52 45 41 54 45 20 54 ble.3t3.CREATE T +| 416: 41 42 4c 45 20 74 33 28 63 2c 78 2c 65 2c 66 29 ABLE t3(c,x,e,f) +| 432: 28 02 06 17 11 11 01 3d 74 61 62 6c 65 74 32 74 (......=tablet2t +| 448: 32 03 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 2.CREATE TABLE t +| 464: 32 28 63 2c 64 2c 65 2c 66 29 24 01 06 17 11 11 2(c,d,e,f)$..... +| 480: 01 35 74 60 62 6c 65 74 31 74 31 02 43 52 45 41 .5t`blet1t1.CREA +| 496: 54 45 20 54 41 42 4c 45 20 74 30 28 61 2c 62 29 TE TABLE t0(a,b) +| page 2 offset 512 +| 0: 0d 00 ff 11 04 01 cf 00 01 fa 01 f3 01 de 01 cf ................ +| 32: 00 00 08 00 00 00 00 00 00 00 00 00 00 00 00 13 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 20 00 00 ............. .. +| 64: 00 00 00 00 00 00 f8 ff ff ff 00 00 00 00 00 00 ................ +| 160: 01 64 00 00 00 00 00 80 ff ff ff 00 00 00 00 00 .d.............. +| 176: 00 00 00 00 00 00 00 00 1f 00 00 00 00 00 00 03 ................ +| 192: 00 00 40 00 00 00 00 00 00 00 00 00 00 00 00 00 ..@............. +| 288: 00 00 00 00 00 00 ff ff ff e9 00 00 00 00 00 00 ................ +| 336: 01 00 00 ff ff 00 00 00 00 00 00 00 00 00 00 00 ................ +| 368: 20 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ............... +| 384: 00 de ff 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 464: 00 00 00 00 00 13 76 65 6e 65 69 67 68 74 13 03 ......veneight.. +| 480: 03 40 07 07 14 00 54 45 20 49 4e 44 45 58 20 74 .@....TE INDEX t +| 496: 32 63 64 20 4f 4e 20 74 32 28 0a 0c 44 09 01 02 2cd ON t2(..D... +| page 3 offset 1024 +| 0: 0d 00 00 00 48 01 54 00 01 f7 01 ec 01 c5 01 aa ....H.T......... +| 16: 30 34 28 87 29 2a 06 06 17 13 11 01 3f 69 4f 64 04(.)*......?iOd +| 32: 65 79 74 33 78 74 33 6d 6d 6d 6d 6d 6d 7d 6d 6d eyt3xt3mmmmmm.mm +| 48: 6d 41 6d 6d 6d 6d 6d 6d 6d 6d 6d 6d 6d 6d 6d 6d mAmmmmmmmmmmmmmm +| 64: 6d 6d 6d 6d 6d 6d 6d 6d 6d 6d 6d 66 6d 6d 6d 6d mmmmmmmmmmmfmmmm +| 80: 6d 4e 6d 6d 6d 6d 6d 6d 6d 6d 6d 6d 6d 6d 6d 6d mNmmmmmmmmmmmmmm +| 96: 6d 6d 6d 6d 6d 6d 6d 6d 6d 6d 6d 6d 6d 6d 6d 6d mmmmmmmmmmmmmmmm +| 112: 6d 6d 6d 6d 6d 6d 6d 6d 6d 6d 6d 6d 6d 6d 6d 6d mmmmmmmmmmmmmmmm +| 128: 6d 6d 6d 6d 6d 00 00 00 00 00 00 00 00 00 00 00 mmmmm........... +| 160: 80 00 00 00 00 00 00 03 00 00 00 ff e4 00 00 00 ................ +| 208: 00 00 00 00 00 00 00 00 00 00 00 00 00 c5 00 00 ................ +| 240: 14 00 00 00 00 00 00 00 00 00 00 00 08 00 00 00 ................ +| 256: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0f ec ................ +| 304: 00 00 00 00 19 08 05 17 17 17 17 65 69 67 68 74 ...........eight +| 320: 65 69 67 68 74 73 65 00 00 00 00 00 00 00 00 00 eightse......... +| 336: 00 00 00 00 19 08 05 17 17 17 17 65 69 67 68 74 ...........eight +| 352: 65 69 67 68 74 73 65 01 65 6e 00 00 00 10 25 07 eightse.en....%. +| 368: 07 6e 25 07 07 07 40 18 00 00 00 00 00 00 40 18 .n%...@.......@. +| 384: 00 00 00 00 00 00 40 14 00 00 00 00 00 00 40 14 ......@.......@. +| 400: 00 00 00 00 00 00 09 06 05 01 01 01 01 04 04 03 ................ +| 416: 03 07 05 05 01 01 09 09 02 02 19 04 05 17 17 17 ................ +| 432: 17 10 65 76 65 6e 65 69 67 68 74 65 69 67 68 74 ..eveneighteight +| 448: 73 65 76 65 6e 25 03 05 07 07 07 07 40 14 00 00 seven%......@... +| 464: 00 00 00 00 40 18 00 00 00 00 00 00 40 18 00 00 ....@.......@... +| 480: 00 00 00 00 40 14 00 00 00 00 e8 f6 09 02 00 00 ....@........... +| 496: 00 00 00 00 00 00 00 00 00 00 64 00 00 00 00 02 ..........d..... +| page 4 offset 1536 +| 0: 0d 00 00 00 00 02 00 00 00 00 00 00 00 00 00 fa ................ +| 16: 1f a1 07 00 00 00 00 00 01 00 00 00 00 00 00 00 ................ +| 32: 00 00 00 00 00 00 00 00 00 00 00 00 00 73 69 6d .............sim +| 48: 70 6c 65 00 00 00 00 00 00 00 00 00 00 00 00 00 ple............. +| 80: 00 00 00 00 00 10 00 00 00 00 00 00 01 00 00 00 ................ +| 96: 00 00 00 00 00 00 00 00 00 00 00 00 00 fe ff ff ................ +| 112: ff 00 00 00 00 00 00 00 00 00 00 00 4a 00 00 00 ............J... +| 144: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 176: e5 ff ff ff 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 208: 00 00 00 00 00 00 00 00 00 00 36 36 00 00 00 00 ..........66.... +| 240: 00 00 00 6c 00 00 00 00 00 00 00 00 00 00 00 00 ...l............ +| 256: 00 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 320: 00 00 00 00 00 00 00 00 01 00 00 02 00 80 00 00 ................ +| 336: 00 00 00 00 00 19 08 05 17 17 17 17 65 69 67 68 ............eigh +| 352: 74 65 69 67 68 74 73 65 76 65 6e 73 65 76 65 6e teightsevenseven +| 368: 25 07 05 07 07 07 07 40 18 00 00 00 00 00 00 40 %......@.......@ +| 384: 18 00 20 00 00 00 40 00 14 00 00 00 00 00 00 40 .. ...@........@ +| 400: 14 00 00 00 00 00 1c 09 06 05 01 01 01 01 04 04 ................ +| 416: 03 03 07 05 05 01 01 00 00 00 00 00 00 00 00 00 ................ +| 448: 74 73 65 76 65 6e 00 80 ff ff 00 00 00 00 00 aa tseven.......... +| 464: 00 9e 00 00 00 00 00 00 00 00 00 00 00 70 6f 72 .............por +| 480: 74 65 72 00 00 00 00 00 00 00 00 00 00 00 00 00 ter............. +| 496: 00 00 00 00 00 00 29 00 00 00 00 00 00 00 00 00 ......)......... +| page 5 offset 2048 +| 0: 0a 00 00 00 08 01 96 00 01 fa 01 c5 01 f2 01 bc ................ +| 16: 01 dc 01 a6 01 96 01 cc 00 00 00 00 00 00 00 00 ................ +| 112: 00 00 00 09 00 00 00 00 01 00 00 00 00 00 00 00 ................ +| 160: 74 72 69 67 62 ff ff ff ff fc 00 00 00 00 00 00 trigb........... +| 240: 00 00 00 00 00 00 00 00 00 00 ff 00 00 00 00 00 ................ +| 256: e5 ff ff ff 00 00 54 00 00 00 00 00 00 00 00 00 ......T......... +| 304: 00 00 00 00 00 00 09 00 00 00 00 00 00 00 00 00 ................ +| 400: 00 00 00 00 00 09 00 00 00 00 01 00 00 00 00 00 ................ +| 448: 00 00 74 72 69 67 62 ff ff ff ff fc 00 00 07 05 ..trigb......... +| 464: 05 01 01 09 09 02 02 19 04 05 17 17 17 17 10 65 ...............e +| 480: 76 65 6e 65 69 67 68 74 65 40 18 00 00 00 00 01 veneighte@...... +| 496: 02 03 07 04 01 01 01 03 04 02 05 04 09 01 ff fd ................ +| end crash-6b48ba69806134.db +}]} {} + +set res {1 {database disk image is malformed}} +ifcapable oversize_cell_check { + set res {1 {no such table: t3}} +} +do_catchsql_test 4.1 { + PRAGMA writable_schema=ON; -- bypass improved sqlite_master consistency checking + INSERT INTO t3 SELECT * FROM t2; +} $res + +#------------------------------------------------------------------------- +reset_db +do_test 5.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 192512 pagesize 4096 filename crash-9ae5502296c949.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 2f .....@ ......./ +| 32: 00 00 00 1b 00 00 00 13 00 00 00 03 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 06 00 00 00 01 00 00 00 00 ................ +| 64: 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 96: 00 00 00 00 0d 00 00 00 04 0e e2 00 0f 96 0f 44 ...............D +| 112: 0f 10 0e e2 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 3808: 00 00 2c 04 06 17 15 11 01 41 69 6e 64 65 78 74 ..,......Aindext +| 3824: 31 78 32 74 31 06 43 52 45 41 54 45 20 49 4e 44 1x2t1.CREATE IND +| 3840: 45 58 20 74 31 78 32 20 4f 4e 20 74 31 28 62 29 EX t1x2 ON t1(b) +| 3856: 32 03 06 17 15 11 01 4d 69 6e 64 65 78 74 31 78 2......Mindext1x +| 3872: 31 74 31 05 43 52 45 41 54 45 20 49 4e 44 45 58 1t1.CREATE INDEX +| 3888: 20 74 31 78 31 20 4f 4e 20 74 31 28 67 2b 68 2c t1x1 ON t1(g+h, +| 3904: 6a 2c 6b 29 50 02 06 17 2b 2b 01 59 74 61 62 6c j,k)P...++.Ytabl +| 3920: 65 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 esqlite_sequence +| 3936: 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 04 sqlite_sequence. +| 3952: 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 71 6c CREATE TABLE sql +| 3968: 69 74 65 5f 73 65 71 75 65 6e 63 65 28 6e 61 6d ite_sequence(nam +| 3984: 65 2c 73 65 71 29 68 01 07 17 11 11 01 81 3b 74 e,seq)h.......;t +| 4000: 61 62 6c 65 74 31 74 31 03 43 52 45 41 54 45 20 ablet1t1.CREATE +| 4016: 54 41 42 4c 45 20 74 31 28 61 20 49 4e 54 45 47 TABLE t1(a INTEG +| 4032: 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 41 ER PRIMARY KEY A +| 4048: 55 54 4f 49 4e 43 52 45 4d 45 4e 54 2c 0a 62 2c UTOINCREMENT,.b, +| 4064: 63 2c 64 2c 65 2c 66 2c 67 2c 68 2c 6a 2c 6b 2c c,d,e,f,g,h,j,k, +| 4080: 6c 2c 6d 2c 6e 2c 6f 2c 70 2c 71 2c 72 2c 73 29 l,m,n,o,p,q,r,s) +| page 2 offset 4096 +| 0: 01 00 00 00 00 01 00 00 00 00 01 00 00 00 00 01 ................ +| 16: 00 00 00 00 02 10 00 00 00 05 00 00 00 03 02 00 ................ +| 32: 00 00 00 05 00 00 00 03 02 00 00 00 00 05 00 00 ................ +| 48: 00 03 02 00 00 00 00 05 00 00 00 03 02 00 00 00 ................ +| 64: 00 05 00 00 00 03 02 00 00 00 00 05 00 00 00 03 ................ +| 80: 02 00 00 00 00 05 00 00 00 03 02 00 00 00 00 05 ................ +| 96: 00 00 00 03 02 00 00 00 00 05 00 00 00 03 05 00 ................ +| 112: 00 00 03 03 00 00 00 23 02 00 00 00 00 03 00 00 .......#........ +| 128: 00 23 02 00 00 00 00 03 00 00 00 23 02 00 00 00 .#.........#.... +| 144: 00 03 00 00 00 23 02 00 00 00 00 03 00 00 00 23 .....#.........# +| 160: 05 00 00 00 06 05 00 00 00 06 02 00 00 00 00 03 ................ +| 176: 00 00 00 06 02 00 00 00 00 03 00 00 00 24 02 00 .............$.. +| 192: 00 00 00 03 00 00 00 24 02 00 00 00 00 03 00 00 .......$........ +| 208: 00 24 02 00 00 00 00 02 00 00 00 00 03 00 00 00 .$.............. +| 224: 24 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 $............... +| page 3 offset 8192 +| 0: 05 00 00 00 09 0f d0 00 00 00 00 19 0f fb 0f f6 ................ +| 16: 0f f1 0f ec 0f e7 0f e2 0f dc 0f d6 0f d0 0f a0 ................ +| 32: 0f a0 0f a0 0f a0 0f a0 0f a0 0f a0 0f a0 0f a0 ................ +| 1072: 00 97 4c 0a 14 00 ae 7c 00 00 00 00 00 00 00 00 ..L....|........ +| 1088: 00 00 00 00 00 00 00 09 00 00 00 00 00 00 00 00 ................ +| 4000: 0f ac 00 06 00 00 00 00 00 30 00 00 00 00 00 00 .........0...... +| 4048: 00 00 00 16 81 2a 00 00 00 14 81 16 00 00 00 12 .....*.......... +| 4064: 81 02 00 00 00 10 6e 00 00 00 0e 5a 00 00 00 0c ......n....Z.... +| 4080: 46 00 00 00 0a 32 00 00 00 08 1e 00 00 00 18 0a F....2.......... +| page 4 offset 12288 +| 0: 0d 00 00 00 01 0f f7 00 0f f7 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 07 01 03 11 02 74 31 00 be ............t1.. +| page 5 offset 16384 +| 0: 0a 0f 7c 00 0a 0f 74 00 0f f9 0f eb 0f dd 0f cf ..|...t......... +| 16: 0f c1 0f b3 0f a4 0f 94 0f 84 0f 74 0f 74 0f 74 ...........t.t.t +| 32: 0f 74 0f 74 0f 74 0f 74 0f 74 0f 74 0f 74 00 00 .t.t.t.t.t.t.t.. +| 3952: 00 00 00 00 07 05 00 00 00 02 00 be 0f 8c 00 08 ................ +| 3968: 00 00 00 00 07 05 00 00 00 02 00 aa 0f 9c 00 08 ................ +| 3984: 00 00 00 00 07 05 00 00 00 02 00 96 0f ac 00 08 ................ +| 4000: 00 00 00 00 07 05 00 00 00 02 00 82 0f ba 00 07 ................ +| 4016: 00 00 00 06 05 00 00 00 01 6e 0f c8 00 07 00 00 .........n...... +| 4032: 00 06 05 00 00 00 01 5a 0f d6 00 07 00 00 00 06 .......Z........ +| 4048: 05 00 00 00 01 46 0f e4 00 07 00 00 00 06 05 00 .....F.......... +| 4064: 00 00 01 32 0f f2 00 07 00 00 00 06 05 00 00 00 ...2............ +| 4080: 01 1e 00 00 00 07 00 00 00 06 05 00 00 00 01 0a ................ +| page 6 offset 20480 +| 0: 02 00 00 00 01 0e 0d 00 00 00 00 24 0e 0d 0c 1a ...........$.... +| 16: 06 55 04 66 02 77 00 88 00 00 00 00 00 00 00 00 .U.f.w.......... +| 128: 00 00 00 00 00 00 00 00 97 3d 04 ae 7c 01 00 00 .........=..|... +| 624: 00 00 00 00 00 00 21 97 3d 04 ae 7c 01 00 00 00 ......!.=..|.... +| 1120: 00 00 00 00 00 20 97 3d 04 ae 7c 01 00 00 00 00 ..... .=..|..... +| 1616: 00 00 00 00 1f 97 3d 04 ae 7c 01 00 00 00 00 00 ......=..|...... +| 2112: 00 00 00 1e 97 3d 04 ae 7c 01 00 00 00 00 00 00 .....=..|....... +| 2608: 00 00 1d 97 3d 04 ae 7c 01 00 00 00 00 00 00 00 ....=..|........ +| 3088: 00 00 00 00 00 00 00 00 00 00 00 00 01 f3 00 00 ................ +| 3600: 23 97 3d 04 ae 7c 01 00 00 00 00 00 00 00 00 00 #.=..|.......... +| 4080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 26 ...............& +| page 8 offset 28672 +| 0: 0d 00 00 00 01 04 30 00 04 30 00 00 00 00 00 00 ......0..0...... +| 1072: 97 4d 1e 14 00 ae 7c 00 00 00 00 00 00 00 00 00 .M....|......... +| 1088: 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 03 ................ +| page 10 offset 36864 +| 256: 0d 00 00 00 01 04 30 00 04 30 00 00 00 00 00 00 ......0..0...... +| 1072: 97 4d 32 14 00 ae 7c 00 00 00 00 00 00 00 00 00 .M2...|......... +| 1088: 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 05 ................ +| page 12 offset 45056 +| 0: 0d 00 00 00 01 04 30 00 04 30 00 00 00 00 00 00 ......0..0...... +| 1072: 97 4d 46 14 00 ae 7c 00 00 00 00 00 00 00 00 00 .MF...|......... +| 1088: 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 07 ................ +| page 14 offset 53248 +| 0: 0d 00 00 00 01 04 30 00 04 30 00 00 00 00 00 00 ......0..0...... +| 1072: 97 4d 5a 14 00 ae 7c 00 00 00 00 00 00 00 00 00 .MZ...|......... +| 1088: 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 09 ................ +| page 16 offset 61440 +| 0: 0d 00 00 00 01 04 30 00 04 30 00 00 00 00 00 00 ......0..0...... +| 1072: 97 4d 6e 14 00 ae 7c 00 00 00 00 00 00 00 00 00 .Mn...|......... +| 1088: 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0b ................ +| page 18 offset 69632 +| 0: 0d 00 00 00 01 04 2f 00 04 2f 00 00 00 00 00 00 ....../../...... +| 1056: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 97 ................ +| 1072: 4d 81 02 14 00 ae 7c 00 00 00 00 00 00 00 00 00 M.....|......... +| 1088: 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0d ................ +| page 20 offset 77824 +| 0: 0d 00 00 00 01 04 2f 00 04 2f 00 00 00 00 00 00 ....../../...... +| 1056: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 97 ................ +| 1072: 4d 81 16 14 00 ae 7c 00 00 00 00 00 00 00 00 00 M.....|......... +| 1088: 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0f ................ +| page 22 offset 86016 +| 0: 0d 00 00 00 01 04 2f 00 04 2f 00 00 00 00 00 00 ....../../...... +| 1056: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 97 ................ +| 1072: 4d 81 2a 14 00 ae 7c 00 00 00 00 00 00 00 00 00 M.*...|......... +| 1088: 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 11 ................ +| page 24 offset 94208 +| 0: 0d 00 00 00 01 04 31 00 04 31 00 00 00 00 00 00 ......1..1...... +| 1072: 00 97 4c 0a 14 00 ae 7c 00 00 00 00 00 00 00 00 ..L....|........ +| 1088: 00 00 00 00 00 00 00 09 00 00 00 00 00 00 00 00 ................ +| page 25 offset 98304 +| 0: 0d 00 00 00 01 04 2f 00 04 2f 00 00 00 00 00 00 ....../../...... +| 1056: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 97 ................ +| 1072: 4d 81 3e 14 00 ae 7c 00 00 00 00 00 00 00 00 00 M.>...|......... +| 1088: 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 13 ................ +| page 26 offset 102400 +| 2512: 00 00 00 00 00 00 00 0a 00 00 00 00 00 00 00 00 ................ +| page 27 offset 106496 +| 0: 00 00 00 00 00 00 00 12 00 00 00 07 00 00 00 1d ................ +| 16: 00 00 00 09 00 00 00 1f 00 00 00 0b 00 00 00 21 ...............! +| 32: 00 00 00 0d 00 00 00 25 00 00 00 0f 00 00 00 19 .......%........ +| 48: 00 00 00 11 00 00 00 29 00 00 00 13 00 00 00 2b .......).......+ +| 64: 00 00 00 15 00 00 00 2d 00 00 00 2e 00 00 00 17 .......-........ +| page 28 offset 110592 +| 2512: 00 00 00 00 00 00 00 1e 00 00 00 00 00 00 00 00 ................ +| page 30 offset 118784 +| 2512: 00 00 00 00 00 00 00 32 00 00 00 00 00 00 00 00 .......2........ +| page 32 offset 126976 +| 2512: 00 00 00 00 00 00 00 46 00 00 00 00 00 00 00 00 .......F........ +| page 34 offset 135168 +| 2512: 00 00 00 00 00 00 00 5a 00 00 00 00 00 00 00 00 .......Z........ +| page 35 offset 139264 +| 0: 0a 08 44 00 05 02 77 00 0e 11 0a 33 06 55 02 77 ..D...w....3.U.w +| 16: 04 66 00 88 00 88 00 88 00 00 00 00 00 00 00 00 .f.............. +| 128: 00 00 00 00 00 00 00 00 04 66 01 ef 00 00 00 00 .........f...... +| 624: 00 00 00 00 00 00 00 97 3d 04 ae 7c 01 00 00 00 ........=..|.... +| 1120: 00 00 00 00 00 20 97 3d 04 ae 7c 01 00 00 00 00 ..... .=..|..... +| 1616: 00 00 00 00 22 97 3d 04 ae 7c 01 00 00 00 00 00 ......=..|...... +| 2112: 00 00 00 1e 0c 22 01 ef 00 00 00 00 00 00 00 00 ................ +| 2608: 00 00 00 97 3d 04 ae 7c 01 00 00 00 00 00 00 00 ....=..|........ +| 3104: 00 1c 00 00 01 ef 00 00 00 00 00 00 00 00 00 00 ................ +| 3600: 00 97 3d 04 ae 7c 01 00 00 00 00 00 00 00 00 00 ..=..|.......... +| 4080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1a ................ +| page 36 offset 143360 +| 0: 0a 08 44 00 04 02 77 00 06 55 02 77 04 66 0e 11 ..D...w..U.w.f.. +| 16: 00 88 00 88 00 88 0e 11 00 00 00 00 00 00 00 00 ................ +| 128: 00 00 00 00 00 00 00 00 04 76 01 ef 00 00 00 00 .........v...... +| 624: 00 00 00 00 00 00 00 97 3e 04 ae 7c 02 00 00 00 ........>..|.... +| 1120: 00 00 00 00 00 2a 97 3e 04 ae 7c 02 00 00 00 00 .....*.>..|..... +| 1616: 00 00 00 00 2c 97 3e 04 ae 7c 02 00 00 00 00 00 ....,.>..|...... +| 2112: 00 00 00 28 00 00 05 cd 00 00 00 00 00 00 00 00 ...(............ +| 3600: 00 97 3e 04 ae 7c 02 00 00 00 00 00 00 00 00 00 ..>..|.......... +| 4080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 2f .............../ +| page 38 offset 151552 +| 2512: 00 00 00 00 00 00 00 6e 00 00 00 00 00 00 00 00 .......n........ +| page 40 offset 159744 +| 2512: 00 00 00 00 00 00 00 00 82 00 00 00 00 00 00 00 ................ +| page 42 offset 167936 +| 2512: 00 00 00 00 00 00 00 00 96 00 00 00 00 00 00 00 ................ +| page 44 offset 176128 +| 2512: 00 00 00 00 00 00 00 00 aa 00 00 00 00 00 00 00 ................ +| page 47 offset 188416 +| 2512: 00 00 00 00 00 00 00 00 be 00 00 00 00 00 00 00 ................ +| end crash-9ae5502296c949.db +}]} {} + +do_catchsql_test 5.1 { + INSERT INTO t1(b) VALUES(zeroblob(40000)); +} {1 {database disk image is malformed}} + +do_catchsql_test 5.2 { + DROP INDEX t1x2; +} {0 {}} + +do_catchsql_test 5.3 { + INSERT INTO t1(b) VALUES(zeroblob(40000)); +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +reset_db +do_test 6.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 20480 pagesize 4096 filename crash-d260f001fa015c.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 05 .....@ ........ +| 32: 00 00 00 00 00 ff ff f0 00 00 00 02 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 64 00 00 00 01 00 00 00 00 .......d........ +| 96: 00 00 00 00 0d 0f f8 00 04 0e ce 00 0f 4c 0f d3 .............L.. +| 112: 0e fa 0e ce 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 3776: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 2a 04 ..............*. +| 3792: 06 17 13 11 01 3f 69 6e 64 65 78 74 31 62 74 31 .....?indext1bt1 +| 3808: 05 43 52 45 41 54 45 20 49 4e 44 45 58 20 74 31 .CREATE INDEX t1 +| 3824: 62 20 4f 4e 20 74 31 28 62 29 50 03 06 17 2b 2b b ON t1(b)P...++ +| 3840: 01 59 74 61 62 6c 65 73 71 6c 69 74 65 5f 73 65 .Ytablesqlite_se +| 3856: 71 75 65 6e 63 65 73 71 6c 69 74 65 5f 73 65 71 quencesqlite_seq +| 3872: 75 65 6e 63 65 04 43 52 45 41 54 45 20 54 41 42 uence.CREATE TAB +| 3888: 4c 45 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e LE sqlite_sequen +| 3904: 63 65 28 6e 61 6d 65 2c 73 65 71 29 81 04 01 07 ce(name,seq).... +| 3920: 17 11 11 01 81 73 74 61 62 6c 65 74 31 74 31 02 .....stablet1t1. +| 3936: 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 31 28 CREATE TABLE t1( +| 3952: 61 20 52 45 41 4c 20 4e 4f 54 20 4e 55 4c 4c 20 a REAL NOT NULL +| 3968: 44 45 46 41 55 4c 54 28 32 35 2b 33 32 29 2c 62 DEFAULT(25+32),b +| 3984: 20 46 4c 4f 41 54 2c 63 20 44 4f 55 42 4c 45 20 FLOAT,c DOUBLE +| 4000: 55 4e 49 51 55 45 2c 0a 64 20 43 4c 4f 42 2c 65 UNIQUE,.d CLOB,e +| 4016: 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 INTEGER PRIMARY +| 4032: 20 4b 45 59 20 41 55 54 4f 49 4e 43 52 45 4d 45 KEY AUTOINCREME +| 4048: 4e 54 29 23 02 06 17 37 11 01 00 69 6e 64 65 78 NT)#...7...index +| 4064: 73 71 6c 69 74 65 5f 61 75 74 6f 69 6e 64 65 78 sqlite_autoindex +| 4080: 5f 74 31 5f 31 74 31 05 00 00 00 08 00 00 00 00 _t1_1t1......... +| page 2 offset 4096 +| 0: 0d 00 00 00 00 10 00 00 00 00 00 00 00 00 00 00 ................ +| page 3 offset 8192 +| 0: 0a 00 00 00 00 10 00 00 00 00 00 00 00 00 00 00 ................ +| page 4 offset 12288 +| 0: 0d 00 00 00 00 10 00 00 00 00 00 00 00 00 00 00 ................ +| page 5 offset 16384 +| 0: 0a 00 00 00 00 10 00 00 00 00 00 00 00 00 00 00 ................ +| end crash-d260f001fa015c.db +}]} {} + +do_catchsql_test 6.1 { + BEGIN; + INSERT INTO t1(b) VALUES(1); + INSERT INTO t1(b) VALUES(2); + COMMIT; +} {1 {malformed database schema (t1b) - invalid rootpage}} + +#------------------------------------------------------------------------- +reset_db +do_test 7.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 20480 pagesize 4096 filename crash-8391315d75edff.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 05 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 05 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 96: 00 00 00 00 0d 00 00 00 05 0e 55 00 0f 74 0f 3c ..........U..t.< +| 112: 0e f9 0e d1 0e 55 00 00 00 00 00 00 00 00 00 00 .....U.......... +| 3664: 00 00 00 00 00 7a 05 07 15 11 11 08 81 63 76 69 .....z.......cvi +| 3680: 65 77 76 31 76 31 43 52 45 41 54 45 20 56 49 45 ewv1v1CREATE VIE +| 3696: 57 20 76 31 28 78 2c 69 29 20 41 53 0a 53 45 4c W v1(x,i) AS.SEL +| 3712: 45 43 54 20 74 31 2e 62 2c 74 32 2e 62 20 46 52 ECT t1.b,t2.b FR +| 3728: 4f 4d 20 74 31 2c 74 32 20 57 48 45 52 45 20 74 OM t1,t2 WHERE t +| 3744: 31 2e 61 3d 74 32 2e 61 20 47 52 4f 55 50 20 42 1.a=t2.a GROUP B +| 3760: 59 20 31 20 48 41 56 49 4e 47 20 74 32 2e 63 20 Y 1 HAVING t2.c +| 3776: 4e 4f 54 20 4e 55 4c 4c 0a 4c 49 4d 49 54 20 31 NOT NULL.LIMIT 1 +| 3792: 30 26 04 06 17 11 11 01 39 74 61 62 6c 65 74 32 0&......9tablet2 +| 3808: 74 32 05 43 52 45 41 54 45 20 54 41 42 4c 45 20 t2.CREATE TABLE +| 3824: 74 32 28 61 2c 62 2c 63 29 41 03 06 17 15 11 01 t2(a,b,c)A...... +| 3840: 6b 69 6e 64 65 78 74 31 78 31 74 31 03 43 52 45 kindext1x1t1.CRE +| 3856: 41 54 45 20 49 4e 44 45 58 20 74 31 78 31 20 4f ATE INDEX t1x1 O +| 3872: 4e 20 74 31 28 64 29 20 57 48 45 52 45 20 65 65 N t1(d) WHERE ee +| 3888: 20 49 53 20 4e 4f 54 20 4e 55 4c 4c 36 02 06 17 IS NOT NULL6... +| 3904: 17 11 01 53 69 6e 64 65 78 74 31 61 62 63 74 31 ...Sindext1abct1 +| 3920: 03 43 52 45 41 54 45 20 49 4e 44 45 58 20 74 31 .CREATE INDEX t1 +| 3936: 61 62 63 20 4f 4e 20 74 31 28 61 2c 62 2c 63 2b abc ON t1(a,b,c+ +| 3952: 64 2b 65 29 81 09 01 07 17 11 11 01 81 7d 74 61 d+e)..........ta +| 3968: 62 6c 65 74 31 74 31 02 43 52 45 41 54 45 20 54 blet1t1.CREATE T +| 3984: 41 42 4c 45 20 74 31 28 61 2c 62 2c 63 2c 64 2c ABLE t1(a,b,c,d, +| 4000: 65 2c 66 2c 67 2c 68 2c 6a 2c 6a 6a 2c 6a 6a 6a e,f,g,h,j,jj,jjj +| 4016: 2c 6b 2c 61 61 2c 62 69 8c 63 63 2c 64 64 2c 65 ,k,aa,bi.cc,dd,e +| 4032: 65 20 44 45 46 41 55 4c 54 20 33 2e 31 34 2c 0a e DEFAULT 3.14,. +| 4048: 66 66 20 44 45 46 41 55 4c 54 28 27 68 69 63 63 ff DEFAULT('hicc +| 4064: 75 70 27 29 2c 67 67 20 4e 4f 54 20 4e 55 4c 4c up'),gg NOT NULL +| 4080: 20 44 45 46 41 55 4c 54 28 66 61 6c 73 65 29 29 DEFAULT(false)) +| page 2 offset 4096 +| 0: 0d 00 00 00 0a 0e 7b 00 0f dc 0f b6 0f 8f 0f 68 ...............h +| 16: 0f 41 0f 1a 0e f3 0e cb 0e a3 0e 22 00 00 00 00 .A.............. +| 3696: 00 00 00 00 00 00 00 00 00 00 00 26 0a 14 01 01 ...........&.... +| 3712: 02 08 00 00 00 00 00 00 00 00 00 00 00 00 07 19 ................ +| 3728: 08 09 5a 00 b4 40 09 1e b8 51 eb 95 1f 68 69 63 ..Z..@...Q...hic +| 3744: 63 75 70 26 09 14 01 01 02 08 00 00 00 00 00 00 cup&............ +| 3760: 00 00 00 00 00 00 07 19 08 08 50 00 a0 40 09 1e ..........P..@.. +| 3776: b8 51 eb 85 1f 68 69 63 63 74 70 26 08 14 01 01 .Q...hicctp&.... +| 3792: 03 08 00 00 00 00 00 00 00 00 00 00 00 00 07 19 ................ +| 3808: 08 07 46 00 8c 40 09 1e b8 51 eb 85 1f 68 69 63 ..F..@...Q...hic +| 3824: 63 75 70 25 07 14 01 01 01 08 00 00 00 00 00 00 cup%............ +| 3840: 00 00 00 00 00 10 07 19 08 06 3c 78 40 09 1e b8 ..........<x@... +| 3856: 51 eb 85 1f 68 69 63 63 75 70 25 06 14 01 01 01 Q...hiccup%..... +| 3872: 08 00 00 00 00 00 00 00 00 00 00 00 00 07 19 08 ................ +| 3888: 05 32 64 40 09 1e b8 51 eb 85 1f 68 69 63 63 75 .2d@...Q...hiccu +| 3904: 70 25 05 14 01 01 01 08 00 00 00 00 00 00 00 00 p%.............. +| 3920: 00 00 00 00 07 19 08 04 28 50 40 09 1e b8 51 eb ........(P@...Q. +| 3936: 85 1f 68 69 63 63 75 70 25 04 14 01 00 f1 08 00 ..hiccup%....... +| 3952: 00 00 00 00 00 00 00 00 00 00 00 07 19 08 03 1e ................ +| 3968: 3c 40 09 1e b8 51 eb 85 1f 68 69 63 63 75 70 25 <@...Q...hiccup% +| 3984: 03 14 01 01 01 08 00 00 00 00 00 00 00 00 00 00 ................ +| 4000: 00 00 07 19 08 02 14 28 40 09 1e b8 51 eb 85 1f .......(@...Q... +| 4016: 68 69 63 63 75 70 24 02 14 09 01 01 08 00 00 00 hiccup$......... +| 4032: 00 00 00 00 00 00 00 00 00 07 19 08 0a 14 40 09 ..............@. +| 4048: 1e b8 51 eb 85 1f 68 69 63 63 75 70 22 01 14 08 ..Q...hiccup.... +| 4064: 08 08 08 00 00 00 00 00 00 00 00 00 00 00 00 07 ................ +| 4080: 19 08 40 09 1e b8 51 eb 85 1f 68 69 63 63 75 70 ..@...Q...hiccup +| page 3 offset 8192 +| 0: 0a 00 00 00 0a 0f aa 00 0f fa 0f f2 0f e9 0f e0 ................ +| 16: 0f d7 0f ce 0f c5 0f bc 0f b3 0f aa 00 00 00 00 ................ +| 4000: 00 00 00 00 00 00 00 00 00 00 08 05 01 01 00 01 ................ +| 4016: 09 5a 0a d8 05 01 01 00 01 08 50 09 08 05 01 01 .Z........P..... +| 4032: 00 01 07 46 08 08 05 01 01 00 01 06 3c 07 08 05 ...F........<... +| 4048: 01 01 00 01 05 32 06 08 05 01 01 00 01 04 28 05 .....2........(. +| 4064: 08 05 01 02 60 01 03 1e 04 08 05 01 01 00 01 02 ....`........... +| 4080: 14 03 07 05 09 01 00 01 0a 02 05 05 08 08 00 09 ................ +| page 4 offset 12288 +| 0: 0a 00 00 00 0a 0f cf 00 0f fc 0f f7 0f f2 0f ed ................ +| 16: 0f e8 0f e3 0f de 0f d9 0f d4 0f cf 00 00 00 00 ................ +| 4032: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 04 ................ +| 4048: 03 08 01 0a 04 03 08 01 09 04 03 08 01 08 04 03 ................ +| 4064: 08 01 07 04 03 08 01 06 04 03 08 01 05 04 03 08 ................ +| 4080: 01 04 04 03 08 01 03 04 03 08 01 02 03 03 08 09 ................ +| page 5 offset 16384 +| 0: 0d 00 00 00 00 10 00 00 00 00 00 00 00 00 00 00 ................ +| end crash-8391315d75edff.db +}]} {} + +do_catchsql_test 7.1 { + SELECT * FROM sqlite_master; +} {1 {malformed database schema (t1x1) - invalid rootpage}} + +#------------------------------------------------------------------------- +reset_db +do_test 8.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 2048 pagesize 512 filename a.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 02 00 01 01 00 40 20 20 ff ff 00 0c 00 00 00 07 .....@ ........ +| 32: 0b 00 00 00 00 00 00 00 00 00 00 08 9c 00 00 04 ................ +| 48: 00 00 00 e0 09 00 00 01 00 00 00 01 00 00 00 00 ................ +| 64: 00 00 00 00 f2 ff 00 00 00 00 00 00 00 00 00 00 ................ +| 80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0c ................ +| 96: 00 2e 2c 50 0d 00 00 00 06 01 06 00 01 da 01 b0 ..,P............ +| 112: 05 56 01 86 01 2a 01 06 00 00 00 00 00 06 00 00 .V...*.......... +| 128: 00 ff 00 00 ff ff ff e1 00 00 00 00 00 00 00 00 ................ +| 144: 00 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 ................ +| 160: 00 00 00 00 00 00 00 00 f2 00 00 00 00 00 00 00 ................ +| 176: 00 00 f9 ff ff ff ff ff ff ff 00 00 00 5f 00 fb ............._.. +| 192: 00 00 00 00 00 00 00 00 00 e1 ff 00 00 00 00 00 ................ +| 208: 00 00 10 00 00 00 00 00 1e 00 00 00 fe 00 00 00 ................ +| 224: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ca 00 ................ +| 240: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 35 ...............5 +| 256: 00 00 00 00 ef ff 22 07 06 17 11 11 01 30 39 38 .............098 +| 272: 62 6c 65 74 38 38 74 04 43 52 45 41 54 45 20 54 blet88t.CREATE T +| 288: 41 42 4c 45 20 74 34 28 87 29 2a 06 06 17 13 11 ABLE t4(.)*..... +| 304: 01 3f 69 4f 64 65 78 74 33 78 74 40 05 43 52 45 .?iOdext3xt@.CRE +| 320: 41 54 45 20 49 6e 44 45 58 20 74 33 78 20 4f 4e ATE InDEX t3x ON +| 336: 20 74 33 28 78 29 2e 04 06 17 15 11 01 45 69 6e t3(x).......Ein +| 352: 00 04 00 00 34 63 64 74 3d 05 43 52 45 41 54 45 ....4cdt=.CREATE +| 368: 20 49 4e 44 45 58 20 63 74 64 32 20 4f 4e 20 74 INDEX ctd2 ON t +| 384: 32 28 0a 0c 44 29 28 05 06 17 11 11 01 3d 74 6c 2(..D)(......=tl +| 400: 62 61 d4 65 33 74 33 04 43 52 45 41 54 45 20 54 ba.e3t3.CREATE T +| 416: 41 42 4c 45 20 74 33 28 63 2c 78 2c 65 2c 66 29 ABLE t3(c,x,e,f) +| 432: 28 02 06 17 11 11 01 3d 74 61 62 6c 65 74 32 74 (......=tablet2t +| 448: 32 03 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 2.CREATE TABLE t +| 464: 32 28 63 2c 64 2c 65 2c 66 29 24 01 06 17 11 11 2(c,d,e,f)$..... +| 480: 01 35 74 60 62 6c 65 74 31 74 31 02 43 52 45 41 .5t`blet1t1.CREA +| 496: 54 45 20 54 41 42 4c 45 20 74 30 28 61 2c 62 29 TE TABLE t0(a,b) +| page 2 offset 512 +| 0: 0d 00 ff 11 04 01 cf 80 01 fa 01 09 00 de 01 cf ................ +| 16: 00 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 ................ +| 32: 00 00 08 00 00 00 00 00 00 11 00 00 00 00 00 13 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 e0 ff ff ................ +| 64: ff d2 ff ff ff 00 f8 ff ff ff 00 00 00 00 00 00 ................ +| 80: 00 ff ff 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 96: 00 00 00 00 ff de 00 00 00 00 00 00 00 00 00 00 ................ +| 112: 00 00 00 00 00 00 00 00 00 00 00 00 00 40 00 00 .............@.. +| 128: 2a 00 00 00 00 00 00 00 00 f7 00 00 00 00 00 00 *............... +| 144: 00 00 00 00 00 21 00 00 00 00 00 00 00 00 00 00 .....!.......... +| 160: 01 64 00 00 00 00 04 80 ff ff ff 00 00 00 00 00 .d.............. +| 176: 00 00 00 00 00 00 00 00 1f 00 00 00 00 00 00 00 ................ +| 192: 00 00 40 00 00 00 00 00 00 00 00 00 00 00 00 00 ..@............. +| 208: b5 00 00 00 00 00 00 40 00 00 00 00 00 00 00 00 .......@........ +| 224: 00 00 00 f6 00 ee ff ff ff 00 00 00 00 00 00 00 ................ +| 272: f2 00 00 00 00 00 00 00 00 00 f9 ff ff ff ff ff ................ +| 288: ff ff 00 00 00 5f 00 fb 00 00 00 00 00 00 00 00 ....._.......... +| 320: 1e 00 00 00 fe 00 00 00 00 00 00 00 00 00 00 00 ................ +| 336: 00 00 00 00 00 00 ca 00 00 00 00 00 00 00 ff ec ................ +| 352: 00 00 00 00 00 00 00 32 00 00 00 00 ef ff 22 07 .......2........ +| 368: 06 17 11 11 01 30 74 61 62 6c 65 74 38 38 74 04 .....0tablet88t. +| 384: 43 52 45 41 54 45 20 54 41 42 4c 45 20 8c cb d7 CREATE TABLE ... +| 400: 78 d6 d5 f9 f9 17 13 11 01 3f 69 4f 64 65 78 74 x........?iOdext +| 416: 33 78 74 33 05 43 52 45 41 54 45 26 49 6e 44 45 3xt3.CREATE&InDE +| 432: 58 20 74 33 78 00 00 00 00 00 00 00 00 00 00 00 X t3x........... +| 464: 00 00 00 00 00 13 76 65 6e 65 69 67 68 74 13 03 ......veneight.. +| 480: 03 40 07 07 15 00 54 45 20 49 4e 44 45 58 20 74 .@....TE INDEX t +| 496: 31 63 64 20 4f 4e 20 74 ce d7 f5 f0 44 09 01 02 1cd ON t....D... +| page 3 offset 1024 +| 0: 0d 00 00 00 48 01 54 00 01 f6 e2 ec 01 c5 01 aa ....H.T......... +| 16: 30 34 28 87 29 32 06 f5 16 13 11 01 8e 61 24 64 04(.)2.......a$d +| 32: 65 78 74 37 78 1f 33 6d 6d 6d 6d 6d 00 00 04 06 ext7x.3mmmmm.... +| 48: 6d 41 6d 6d 6e 6d 6d 00 00 02 00 6d 6d 6d 6d 6d mAmmnmm....mmmmm +| 64: 15 11 01 45 45 45 45 45 45 45 45 45 45 45 45 45 ...EEEEEEEEEEEEE +| 80: 45 45 45 45 45 45 45 45 45 45 45 00 45 63 74 64 EEEEEEEEEEE.Ectd +| 96: 34 20 4f 4e 20 61 62 6c 5d 74 38 38 74 04 43 52 4 ON abl]t88t.CR +| 112: 45 41 54 45 20 54 41 42 4c 45 20 74 34 28 87 29 EATE TABLE t4(.) +| 128: 2a 06 06 13 13 01 00 00 00 4f 64 6e 78 74 33 44 *........Odnxt3D +| 144: 74 13 05 43 52 45 41 54 45 20 49 6e 44 45 00 00 t..CREATE InDE.. +| 160: 00 00 00 00 00 00 00 f9 ff ff ff ff ff ff ff 00 ................ +| 176: 00 00 5f 00 fb 00 00 2d 00 00 00 00 00 00 00 00 .._....-........ +| 192: 00 00 00 00 00 00 00 00 00 00 00 00 00 1e 00 00 ................ +| 208: 00 fe 00 00 00 00 17 15 11 01 45 69 6e 64 65 2e ..........Einde. +| 224: 5b 38 63 64 74 3d 05 43 52 45 41 54 45 20 49 4e [8cdt=.CREATE IN +| 240: 44 45 58 20 63 20 64 32 20 4f 4e 20 74 32 28 0a DEX c d2 ON t2(. +| 256: 0c 44 32 05 00 10 00 00 11 11 3d 74 6c 62 61 d4 .D2.......=tlba. +| 272: 65 33 74 33 04 43 52 45 41 54 45 20 54 41 42 4c e3t3.CREATE TABL +| 288: 45 20 74 36 ff ff 7f ff 43 52 45 41 54 45 20 49 E t6....CREATE I +| 304: 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 28 sqlite_sequence( +| 320: 0a 0c 44 29 28 05 06 17 11 11 01 3d 74 6c 62 61 ..D)(......=tlba +| 336: 20 00 00 00 33 04 43 52 45 41 54 45 20 54 41 42 ...3.CREATE TAB +| 352: 4c 45 20 74 33 28 63 2c 78 2c 65 2c 66 29 28 02 LE t3(c,x,e,f)(. +| 368: 06 00 00 7f ff 40 41 54 45 20 49 6e 44 45 58 20 .....@ATE InDEX +| 384: 74 33 78 20 4f 4e 20 74 31 28 78 29 2e 04 06 17 t3x ON t1(x).... +| 400: 15 11 01 45 69 6e 64 65 2e 74 34 63 64 74 3d 05 ...Einde.t4cdt=. +| 416: 00 00 00 00 00 00 00 00 00 00 00 4d 00 00 00 00 ...........M.... +| 432: 01 00 00 00 00 00 00 05 00 00 10 00 00 00 00 00 ................ +| 448: 00 01 00 00 00 00 01 00 00 00 00 07 40 14 00 00 ............@... +| 464: 00 00 21 00 40 18 00 00 00 00 00 00 40 1c 00 00 ..!.@.......@... +| 480: 00 00 ff ff ff 00 00 00 5f 00 fb 00 00 2d 00 00 ........_....-.. +| 496: 00 00 00 1e 00 00 00 fe 00 00 64 00 00 ff fb 02 ..........d..... +| page 4 offset 1536 +| 0: 0d 00 39 00 00 02 00 00 00 00 00 00 00 00 00 00 ..9............. +| end a.db +}]} {} + +set res {1 {database disk image is malformed}} +ifcapable oversize_cell_check { + set res {1 {no such table: t3}} +} +do_catchsql_test 8.1 { + PRAGMA writable_schema=ON; -- bypass improved sqlite_master consistency checking + INSERT INTO t3 SELECT * FROM t2; +} $res + +#------------------------------------------------------------------------- +reset_db +do_test 9.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 8192 pagesize 4096 filename crash-ab10597e4e1c32.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 00 .....@ ........ +| 96: 00 00 00 00 0d 00 00 00 01 0f d6 00 0f d6 00 00 ................ +| 4048: 00 00 00 00 00 00 28 01 06 17 11 11 01 3d 74 61 ......(......=ta +| 4064: 62 6c 65 74 31 74 31 02 43 52 45 41 54 45 20 54 blet1t1.CREATE T +| 4080: 41 42 4c 45 20 74 31 28 61 2c 62 2c 63 2c 64 29 ABLE t1(a,b,c,d) +| page 2 offset 4096 +| 0: 0d 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| end crash-ab10597e4e1c32.db +}]} {} + +do_execsql_test 9.1 { + SAVEPOINT one; +} +do_catchsql_test 9.3 { + INSERT INTO t1(b,c) VALUES(5,6); +} {1 {database disk image is malformed}} +do_execsql_test 9.3 { + ROLLBACK TO one; +} + +#------------------------------------------------------------------------- +reset_db +do_test 10.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 180224 pagesize 4096 filename crash-41390d95d613b6.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 00 .....@ ........ +| 96: 00 00 00 00 0d 00 00 00 04 0e e2 00 0f 96 0f 44 ...............D +| 112: 0f 10 0e e2 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 3808: 00 00 2c 14 06 17 15 11 01 41 69 6e 64 65 78 74 ..,......Aindext +| 3824: 41 78 33 74 31 06 43 52 45 41 54 45 20 49 4e 44 Ax3t1.CREATE IND +| 3840: 45 58 20 74 31 78 32 20 4f 4e 20 74 31 28 62 29 EX t1x2 ON t1(b) +| 3856: 32 03 06 17 15 11 01 4d 69 6e 64 65 78 74 31 88 2......Mindext1. +| 3872: 31 74 31 05 43 52 45 41 54 45 20 49 4e 44 45 58 1t1.CREATE INDEX +| 3888: 20 74 31 78 31 20 4f 4e 20 74 31 28 67 2b 68 2c t1x1 ON t1(g+h, +| 3904: 6a 2d 6b 29 50 02 06 17 2b 2b 01 59 74 61 62 6c j-k)P...++.Ytabl +| 3920: 65 73 71 6c 69 74 65 5e 73 65 71 74 65 6e 63 65 esqlite^seqtence +| 3936: 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 04 sqlite_sequence. +| 3952: 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 71 6c CREATE TABLE sql +| 3968: 69 74 65 5f 73 65 71 75 65 6e 63 65 28 6e 61 6d ite_sequence(nam +| 3984: 65 2c 73 65 71 29 68 00 07 17 11 11 01 81 3b 74 e,seq)h.......;t +| 4000: 61 62 6c 65 74 31 74 31 03 43 52 45 41 54 45 20 ablet1t1.CREATE +| 4016: 54 41 42 4c 45 20 74 31 28 61 20 49 4e 54 45 47 TABLE t1(a INTEG +| 4032: 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 41 ER PRIMARY KEY A +| 4048: 55 54 4f 49 4e 43 52 45 4d 45 4e 54 2c 0a 62 2c UTOINCREMENT,.b, +| 4064: 63 2c 64 2c 65 2c 66 2c 67 2c 68 2c 6a 2c 6b 2c c,d,e,f,g,h,j,k, +| 4080: 6c 2c 6d 2c 6e 2c 6f 2c 70 2c 71 2c 72 2c 73 29 l,m,n,o,p,q,r,s) +| page 2 offset 4096 +| 0: 01 00 00 00 00 01 00 00 10 00 01 00 00 00 00 01 ................ +| 16: 00 00 00 00 02 00 0f f0 00 15 00 00 00 03 02 00 ................ +| 32: 00 00 d9 05 00 00 00 03 02 00 00 00 00 05 00 00 ................ +| 48: 10 03 02 00 00 00 00 05 00 00 00 03 02 00 00 00 ................ +| 64: 00 05 00 00 00 02 62 00 00 00 00 05 00 00 00 03 ......b......... +| 80: 02 00 00 00 00 05 00 00 00 03 02 00 00 00 00 05 ................ +| 96: 00 00 00 03 02 00 00 00 00 05 00 00 00 03 05 00 ................ +| 112: 00 00 03 03 01 00 00 23 02 00 00 4f 00 02 00 00 .......#...O.... +| 128: 10 25 02 00 00 00 00 03 00 00 00 23 02 00 00 00 .%.........#.... +| 144: 00 03 00 00 00 23 02 00 00 00 00 03 00 00 00 23 .....#.........# +| 160: 05 00 08 90 06 05 00 00 00 06 01 ff 00 00 00 03 ................ +| 176: 00 00 00 06 02 00 00 00 00 02 ff 00 00 00 00 00 ................ +| page 3 offset 8192 +| 0: 05 00 00 00 09 0f d0 00 00 00 00 19 0f fb 0f f6 ................ +| 16: 0f f1 10 ec ec e7 0f e2 0f dc 0f d6 0f 00 00 00 ................ +| 1072: 00 97 4c 0a 24 00 ae 00 00 00 00 00 00 00 00 00 ..L.$........... +| 4048: 00 00 00 16 83 39 ff ff ff 14 81 16 00 00 00 12 .....9.......... +| 4064: 81 02 00 00 00 10 6e 00 00 00 0e 5a 00 00 00 0c ......n....Z.... +| 4080: 46 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 F............... +| page 4 offset 12288 +| 1072: 97 4d 32 14 00 ae 00 00 00 00 00 00 00 00 00 00 .M2............. +| 4080: 00 00 00 00 00 00 00 07 01 03 11 02 74 31 00 bd ............t1.. +| page 5 offset 16384 +| 0: fa 0f 7c 00 0a 0f 74 00 0f f9 0f eb 0f dd 0f cf ..|...t......... +| 16: 0f c1 0f b3 0f a4 0e 94 0f 84 0f 74 0f 74 0f 74 ...........t.t.t +| 32: 0f 74 0f 64 0f 00 00 00 00 00 00 00 00 00 00 00 .t.d............ +| 3952: 00 00 00 00 07 05 00 00 00 02 00 be 0f 8c 10 07 ................ +| 3968: ff ff 00 00 07 05 00 00 00 02 00 aa 0f 9b f0 08 ................ +| 3984: c8 00 00 00 37 06 00 00 00 01 00 96 0f ac 00 08 ....7........... +| 4000: 00 00 00 b3 07 15 00 10 00 02 00 82 0f ba 00 07 ................ +| 4016: 00 00 00 06 05 00 00 00 01 6e 0f c8 00 07 00 00 .........n...... +| 4032: 00 06 05 00 00 00 01 5a 03 f6 00 07 00 00 00 06 .......Z........ +| 4048: 05 00 00 00 01 46 0f e4 00 07 00 00 10 06 05 00 .....F.......... +| 4064: 00 00 01 32 10 02 00 07 00 00 00 07 05 00 00 00 ...2............ +| 4080: 01 1d ff ff ff 07 10 00 00 06 05 00 00 00 01 0a ................ +| page 6 offset 20480 +| 624: 00 00 00 00 00 21 97 00 00 00 00 00 00 00 00 00 .....!.......... +| 1120: 00 00 00 00 00 24 57 3e 00 00 00 00 00 00 00 00 .....$W>........ +| 1616: 00 00 00 00 1f 97 00 00 00 00 00 00 00 00 00 00 ................ +| 2112: 00 00 00 1e 97 3d 00 00 00 00 00 00 00 00 00 00 .....=.......... +| 2608: 00 1d 97 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| page 8 offset 28672 +| 1184: 00 00 00 00 00 00 00 00 00 97 4d 1e 13 ff ae 7c ..........M....| +| 4080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 90 ................ +| page 9 offset 32768 +| 256: 0d 01 c0 00 01 04 30 00 04 30 00 00 00 00 00 00 ......0..0...... +| page 10 offset 36864 +| 0: 0d 00 22 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 05 ................ +| page 12 offset 45056 +| 0: 0d 00 00 00 01 04 30 00 00 00 00 00 00 00 00 00 ......0......... +| page 14 offset 53248 +| 0: 0d 00 00 00 01 04 30 00 04 30 00 00 00 00 00 00 ......0..0...... +| 1072: 96 4d 5a 14 00 00 00 00 00 00 00 00 00 00 00 00 .MZ............. +| page 16 offset 61440 +| 0: 0d 00 00 00 01 04 30 00 04 30 00 00 00 00 00 00 ......0..0...... +| 1072: 97 4d 6e 14 00 ae 7b ff ff ff ff 00 00 00 00 00 .Mn............. +| page 18 offset 69632 +| 1056: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 97 ................ +| 1072: 4d 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 M............... +| 4080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0d ................ +| page 20 offset 77824 +| 1056: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 97 ................ +| 1072: 4d 81 16 14 00 ae 00 00 00 00 00 00 00 00 00 00 M............... +| 4080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0f ................ +| page 22 offset 86016 +| 0: 0d 00 00 00 01 04 2f 00 04 2f 01 00 00 00 00 00 ....../../...... +| 1056: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 97 ................ +| 1072: 4d 81 2a 14 00 00 00 00 00 00 00 00 00 00 00 00 M.*............. +| page 24 offset 94208 +| 1072: 00 97 4c 0a 14 00 ae 7c 00 00 00 00 00 00 00 00 ..L....|........ +| page 25 offset 98304 +| 1056: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 97 ................ +| 1072: 4d 81 3e 14 00 ae 7c 00 00 18 ff 00 00 00 00 00 M.>...|......... +| page 27 offset 106496 +| 0: 00 00 00 00 00 00 00 12 00 00 00 07 00 00 00 1d ................ +| 16: 00 00 00 09 00 00 00 1f 00 00 00 0b 00 00 00 21 ...............! +| 32: 00 00 00 0d 00 10 00 25 00 00 00 0f 00 00 00 27 .......%.......' +| 48: 00 00 00 11 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| page 32 offset 126976 +| 2512: 00 00 00 00 00 00 00 45 21 00 00 00 00 00 00 00 .......E!....... +| page 35 offset 139264 +| 0: 00 0a 08 44 00 05 02 77 00 0e 11 0a 92 00 00 00 ...D...w........ +| 1120: 00 00 00 00 00 20 97 00 00 00 00 00 00 00 00 00 ..... .......... +| 1616: 00 00 00 00 22 00 00 00 00 00 00 00 00 00 00 00 ................ +| 2608: 00 00 00 97 3d 04 00 00 00 00 00 00 00 00 00 00 ....=........... +| 3104: 00 1c 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 3600: 00 97 3d 04 ae 7c 00 00 00 00 00 00 00 00 00 00 ..=..|.......... +| 4080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1a ................ +| page 36 offset 143360 +| 0: 0a 08 44 00 04 02 00 00 00 00 00 00 00 00 00 00 ..D............. +| 1120: 00 00 00 00 00 2a 97 3e 04 00 00 00 00 00 00 00 .....*.>........ +| 1616: 00 00 00 00 2c 97 3e 00 00 00 00 00 00 00 00 00 ....,.>......... +| 2112: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 38 ...............8 +| 2128: 00 00 05 cd 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 3600: 00 97 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| page 38 offset 151552 +| 2464: 00 00 00 00 00 00 00 00 00 6e 00 00 00 00 00 00 .........n...... +| page 40 offset 159744 +| 2512: 00 00 00 00 00 00 00 00 82 00 00 00 00 00 00 00 ................ +| page 42 offset 167936 +| 2512: 00 00 00 00 00 00 00 96 00 00 00 00 00 00 00 00 ................ +| page 44 offset 176128 +| 2512: 00 00 00 00 00 00 00 00 aa 00 00 00 00 00 00 00 ................ +| end crash-41390d95d613b6.db +}]} {} + +do_catchsql_test 10.1 { + PRAGMA writable_schema=ON; -- bypass improved sqlite_master consistency checking + SELECT * FROM t1 WHERE a<='2019-05-09' ORDER BY a DESC; +} {1 {database disk image is malformed}} + + +#------------------------------------------------------------------------- +reset_db +do_test 11.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 595 pagesize 512 filename x.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 02 00 00 01 00 40 20 20 00 01 00 0c 00 00 00 07 .....@ ........ +| 32: 00 00 00 05 07 a1 1f fa 00 00 00 08 00 00 00 04 ................ +| 48: 00 00 01 00 00 49 00 00 00 00 00 05 00 00 00 00 .....I.......... +| 80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1c ................ +| 96: 00 2e 2c 50 0d 00 00 00 06 01 06 00 01 da 01 b0 ..,P............ +| 112: 01 56 01 86 01 2a 01 06 00 00 62 00 00 00 00 00 .V...*....b..... +| 128: 00 ed e2 78 74 64 33 ff 43 52 45 41 54 45 20 49 ...xtd3.CREATE I +| 144: 4e 44 45 58 20 74 33 78 20 4f 4e 20 74 33 28 38 NDEX t3x ON t3(8 +| 160: 29 2e 04 06 17 15 11 01 45 69 6e 64 65 68 74 32 ).......Eindeht2 +| 176: 63 64 74 31 e5 43 52 45 41 54 45 20 49 4e 44 45 cdt1.CREATE INDE +| 192: 58 20 74 32 63 c4 20 4f 4e 20 74 32 28 63 2c 64 X t2c. ON t2(c,d +| 208: 29 28 05 06 17 01 11 11 3d 74 61 6c 36 74 62 74 )(......=tal6tbt +| 224: 65 32 04 43 52 45 41 54 45 20 54 41 42 4c 45 20 e2.CREATE TABLE +| 240: 74 00 04 00 00 00 00 00 00 00 00 00 00 00 00 00 t............... +| 256: 00 00 00 00 00 00 22 07 06 17 11 11 01 30 e8 03 .............0.. +| 272: 62 6c 65 74 34 74 35 02 43 52 45 41 54 45 20 54 blet4t5.CREATE T +| 288: 41 42 4c 45 20 74 34 28 94 29 2a 06 06 17 13 11 ABLE t4(.)*..... +| 304: 01 3f 69 33 74 6e 65 78 78 74 64 33 ff 43 52 45 .?i3tnexxtd3.CRE +| 320: 41 54 45 20 49 4e 44 45 58 20 74 33 78 20 4f 4e ATE INDEX t3x ON +| 336: 20 74 31 28 38 29 2e 04 06 17 15 11 01 45 69 6e t1(8).......Ein +| 352: 64 65 68 74 32 63 64 74 31 e5 43 52 45 41 54 45 deht2cdt1.CREATE +| 368: 20 49 4e 44 45 58 20 74 32 63 c4 20 4f 4e 20 74 INDEX t2c. ON t +| 384: 32 28 63 2c 64 29 28 05 06 17 01 11 11 3d 74 61 2(c,d)(......=ta +| 400: 6c 32 74 62 74 65 32 04 43 52 45 41 54 45 20 54 l2tbte2.CREATE T +| 416: 41 42 4c 45 20 74 33 28 63 2c 78 2c 65 2c 66 29 ABLE t3(c,x,e,f) +| 432: 28 02 06 17 11 11 01 3d 74 61 9e 93 65 74 32 74 (......=ta..et2t +| 448: 32 03 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 2.CREATE TABLE t +| 464: 32 28 63 2c 64 2c 65 2c 66 29 24 01 06 17 11 11 2(c,d,e,f)$..... +| 480: 01 35 55 61 62 6c 88 74 31 74 31 02 43 52 45 41 .5Uabl.t1t1.CREA +| 496: 54 45 20 54 41 42 4c 45 20 74 31 28 61 2c 62 29 TE TABLE t1(a,b) +| page 2 offset 512 +| 0: 0d 00 00 00 0d 25 00 01 cf 00 01 fa 01 f3 01 de .....%.......... +| 16: 01 00 00 00 fd 00 00 0d 00 00 00 00 45 20 54 41 ............E TA +| 32: 42 4c 45 20 74 34 28 94 29 2a 06 06 17 13 11 01 BLE t4(.)*...... +| 48: 3f 69 33 74 6e 65 78 78 74 64 33 ff 43 52 45 a0 ?i3tnexxtd3.CRE. +| 64: a0 a0 a0 a0 a0 a0 a0 a0 a0 a0 a0 a0 74 13 11 01 ............t... +| 80: 49 45 74 00 00 00 00 00 00 00 00 00 00 00 00 00 IEt............. +| end x.db +}]} {} + +do_catchsql_test 11.1 { + PRAGMA writable_schema=ON; -- bypass improved sqlite_master consistency checking + DELETE FROM t3 WHERE x IN (SELECT x FROM t4); +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +reset_db +do_test 12.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 12288 pagesize 4096 filename crash-e6d070858a3a85.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 00 .....@ ........ +| 96: 00 00 00 00 0d 00 00 00 02 0f 8f 00 0f bf 0f 8f ................ +| 3968: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 2e ................ +| 3984: 02 06 17 15 11 01 45 69 6e 64 65 78 74 31 63 62 ......Eindext1cb +| 4000: 74 31 03 43 52 45 41 54 45 20 49 4e 44 45 58 20 t1.CREATE INDEX +| 4016: 74 31 63 62 20 4f 4e 20 74 31 28 63 2c 62 29 3f t1cb ON t1(c,b)? +| 4032: 01 06 17 11 11 01 6b 74 61 62 6c 65 74 31 74 31 ......ktablet1t1 +| 4048: 02 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 31 .CREATE TABLE t1 +| 4064: 28 61 20 49 4e 54 2c 20 62 20 49 4e 54 2c 20 43 (a INT, b INT, C +| 4080: 20 49 4e 54 20 44 45 46 41 55 4c 54 20 31 36 29 INT DEFAULT 16) +| page 2 offset 4096 +| 0: 0d 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 4000: 00 00 00 00 00 00 00 00 07 0b 04 01 01 01 63 63 ..............cc +| 4016: 11 05 0a 04 00 00 01 11 05 09 04 08 08 01 0f 05 ................ +| 4032: 08 04 00 00 01 01 56 07 04 01 08 01 07 10 07 06 ......V......... +| 4048: 14 01 01 01 06 08 10 06 05 04 f5 00 01 05 10 07 ................ +| 4064: 04 04 01 01 01 04 03 10 06 03 04 01 09 01 03 10 ................ +| 4080: 06 02 04 01 00 01 02 10 06 01 04 09 01 01 02 10 ................ +| page 3 offset 8192 +| 0: 0a 00 00 00 0b 0f b0 00 0f f9 0f f2 0f eb 0f e4 ................ +| 16: 0f dd 0f d6 0f 9f 0f c7 0f be 00 00 00 00 00 00 ................ +| 4016: 07 04 01 01 01 11 e2 0b 06 04 91 00 01 11 0a 07 ................ +| 4032: 04 01 01 01 10 08 06 07 04 01 01 01 10 04 04 06 ................ +| 4048: 04 01 01 09 10 02 06 04 01 0a 01 10 00 00 00 00 ................ +| end crash-e6d070858a3a85.db +}]} {} + +do_catchsql_test 12.1 { + SELECT CAST((SELECT b FROM t1 WHERE 16=c) AS int) FROM t1 WHERE 16=c; +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +reset_db +do_test 13.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 8192 pagesize 4096 filename crash-81dd2952aef34f.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 02 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 96: 00 00 00 00 0d 00 00 00 01 0f c4 00 0f c4 00 00 ................ +| 4032: 00 00 00 00 3a 11 06 17 11 11 01 61 74 61 62 6c ....:......atabl +| 4048: 65 74 31 74 31 02 43 52 45 41 54 45 20 54 41 42 et1t1.CREATE TAB +| 4064: 4c 45 20 74 31 28 61 20 49 4e 54 45 47 45 52 20 LE t1(a INTEGER +| 4080: 50 52 49 4d 41 52 59 20 4b 45 59 2c 62 2c 63 29 PRIMARY KEY,b,c) +| page 2 offset 4096 +| 0: 0d 07 70 00 02 0f eb 00 0f fa 00 00 00 00 00 00 ..p............. +| 4064: 00 00 00 00 00 00 00 00 00 00 00 05 bf ff ff ff ................ +| 4080: ff ff ff ff ff 04 00 01 00 02 04 01 00 00 00 00 ................ +| end crash-81dd2952aef34f.db +}]} {} + +do_catchsql_test 13.1 { + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x-2019 FROM c WHERE x<2) + INSERT INTO t1(b,c) SELECT last_insert_rowid(), x FROM c; +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +reset_db +do_test 14.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 512 pagesize 65536 filename clusterfuzz-testcase-minimized-sqlite3_dbfuzz2_fuzzer-4806406219825152 +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 00 01 02 01 00 40 20 20 00 63 2e 78 00 00 00 07 .....@ .c.x.... +| 32: 00 00 00 00 00 00 00 00 00 00 00 08 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 01 00 35 05 43 00 04 00 00 00 ........5.C..... +| 80: 00 00 00 00 00 00 00 00 08 00 00 00 00 00 00 0c ................ +| 96: 00 2e 2c 50 0d 00 00 00 03 00 00 00 01 da 01 b0 ..,P............ +| 112: 01 56 01 86 01 2a 01 02 00 00 00 00 00 00 00 1c .V...*.......... +| 128: 00 38 80 b2 e6 0e 00 00 00 00 00 00 00 00 00 10 .8.............. +| 144: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 ................ +| 160: 00 00 00 00 00 00 00 00 00 00 00 00 45 20 54 41 ............E TA +| 256: 00 00 00 00 00 00 22 07 06 17 11 11 01 35 74 61 .............5ta +| 272: 62 6c 00 10 00 00 34 07 43 52 54 45 20 54 41 42 bl....4.CRTE TAB +| 288: 4c 45 20 74 33 28 63 2e 78 2c 65 2c 66 15 28 3a LE t3(c.x,e,f.(: +| 304: 06 17 11 11 01 65 78 8c cc 87 85 35 05 43 72 45 .....ex....5.CrE +| 320: 41 54 48 20 49 4e 44 45 58 20 74 33 78 20 4f 4e ATH INDEX t3x ON +| 336: 20 74 33 28 78 39 2e 04 06 17 15 11 01 45 69 6e t3(x9.......Ein +| 352: 64 65 78 74 32 63 64 74 32 05 43 52 45 41 54 45 dext2cdt2.CREATE +| 368: 20 49 4e 44 45 58 20 74 32 63 64 20 4f 4e 20 74 INDEX t2cd ON t +| 384: 32 28 63 2a 44 29 28 05 fa e8 ee ed 01 3d 74 63 2(c*D)(......=tc +| 400: 62 6c 65 74 33 74 33 07 43 52 45 41 54 45 20 54 blet3t3.CREATE T +| 416: 41 42 4c 45 20 74 33 28 63 2e 78 2c 65 2c 66 15 ABLE t3(c.x,e,f. +| 432: 28 3a 06 17 11 11 01 3d 74 61 62 6c 65 74 32 74 (:.....=tablet2t +| 448: 32 03 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 2.CREATE TABLE t +| 464: 32 28 63 2c 64 2c 65 2c 66 29 24 01 06 17 11 11 2(c,d,e,f)$..... +| 480: 01 35 74 61 62 6c 65 74 31 74 31 02 43 52 45 41 .5tablet1t1.CREA +| 496: 54 45 20 54 41 42 4c 45 20 74 31 28 61 2c 63 29 TE TABLE t1(a,c) +| end clusterfuzz-testcase-minimized-sqlite3_dbfuzz2_fuzzer-4806406219825152 +}]} {} + +extra_schema_checks 0 +do_catchsql_test 14.1 { + PRAGMA integrity_check; +} {1 {database disk image is malformed}} + +# If SQLITE_OMIT_ALTERTABLE is defined, omit this file. +ifcapable altertable { + do_catchsql_test 14.2 { + ALTER TABLE t1 RENAME TO alkjalkjdfiiiwuer987lkjwer82mx97sf98788s9789s; + } {1 {database disk image is malformed}} +} +extra_schema_checks 1 + +#------------------------------------------------------------------------- +reset_db +do_test 15.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 28672 pagesize 4096 filename crash-3afa1ca9e9c1bd.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 07 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 06 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 96: 00 00 00 00 0d 00 00 00 06 0e 88 00 0f b8 0f 6d ...............m +| 112: 0f 3a 0f 0b 0e d5 0e 88 01 00 00 00 00 00 00 00 .:.............. +| 3712: 00 00 00 00 00 00 00 00 4b 06 06 17 25 25 01 5b ........K...%%.[ +| 3728: 74 61 62 6c 65 73 71 6c 69 74 65 5f 73 74 61 74 tablesqlite_stat +| 3744: 31 73 71 6c 69 74 65 5f 73 74 61 74 31 07 43 52 1sqlite_stat1.CR +| 3760: 45 41 54 45 20 54 41 42 4c 45 20 73 71 6c 69 74 EATE TABLE sqlit +| 3776: 65 5f 73 74 61 74 31 28 74 62 6c 2c 69 64 78 2c e_stat1(tbl,idx, +| 3792: 73 74 61 74 29 34 05 06 17 13 11 01 53 69 6e 64 stat)4......Sind +| 3808: 65 78 63 31 63 63 31 06 43 52 45 41 54 45 20 55 exc1cc1.CREATE U +| 3824: 4e 49 51 55 45 20 49 4e 44 45 58 20 63 31 63 20 NIQUE INDEX c1c +| 3840: 4f 4e 20 63 31 28 63 2c 20 62 29 2d 04 06 17 13 ON c1(c, b)-.... +| 3856: 11 01 45 69 6e 64 65 78 63 31 64 63 31 05 43 52 ..Eindexc1dc1.CR +| 3872: 45 41 54 45 20 49 4e 44 45 58 20 63 31 64 20 4f EATE INDEX c1d O +| 3888: 4e 20 63 31 28 64 2c 20 62 29 31 03 06 17 13 11 N c1(d, b)1..... +| 3904: 01 4d 69 6e 64 65 78 62 31 63 62 31 05 43 52 45 .Mindexb1cb1.CRE +| 3920: 41 54 45 20 55 4e 49 51 55 45 20 49 4e 44 45 58 ATE UNIQUE INDEX +| 3936: 20 62 31 63 20 4f 4e 20 62 31 28 63 29 49 02 06 b1c ON b1(c)I.. +| 3952: 17 11 11 0f 7f 74 61 62 6c 65 63 31 63 31 03 43 .....tablec1c1.C +| 3968: 52 45 41 54 45 20 54 41 42 4c 45 20 63 31 28 61 REATE TABLE c1(a +| 3984: 20 49 4e 54 20 50 52 49 4d 41 52 59 20 4b 45 59 INT PRIMARY KEY +| 4000: 2c 20 62 2c 20 63 2c 20 64 29 20 57 49 54 48 4f , b, c, d) WITHO +| 4016: 55 54 20 52 4f 57 49 44 46 01 06 17 11 11 01 79 UT ROWIDF......y +| 4032: 74 61 62 6c 65 62 31 62 31 02 43 52 45 41 54 45 tableb1b1.CREATE +| 4048: 20 54 41 42 4c 45 20 62 31 28 61 20 49 4e 54 20 TABLE b1(a INT +| 4064: 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 62 2c 20 PRIMARY KEY, b, +| 4080: 63 29 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 c) WITHOUT ROWID +| page 2 offset 4096 +| 0: 0a 00 00 00 07 0f ca 00 0f fa 0f f2 0f ea 0f e2 ................ +| 16: 0f da 00 00 00 01 00 00 00 00 00 00 00 00 00 00 ................ +| 4032: 00 00 00 00 00 00 00 00 00 00 07 04 01 0f 01 06 ................ +| 4048: 67 07 07 04 01 0f 01 06 66 06 07 04 01 0f 01 05 g.......f....... +| 4064: 65 05 07 04 01 0f 01 04 64 04 07 04 01 0f 01 03 e.......d....... +| 4080: 63 03 07 04 01 0f 01 02 62 0f 05 04 09 0f 09 61 c.......b......a +| page 3 offset 8192 +| 0: 0a 00 00 00 07 0f bd 00 0f f9 0f ef 0f e5 0f db ................ +| 16: 0f d1 0f c7 0f bd 00 00 00 00 01 00 00 00 00 00 ................ +| 4016: 00 00 00 00 00 00 00 00 00 00 00 00 00 09 05 01 ................ +| 4032: 0f 01 01 07 61 07 07 09 05 01 0f 01 01 06 61 06 ....a.........a. +| 4048: 06 09 05 01 0f 01 01 05 61 05 05 09 05 01 0f 01 ........a....... +| 4064: 01 04 61 04 04 09 05 01 0f 01 01 03 61 03 03 09 ..a.........a... +| 4080: 05 01 0f 01 01 02 61 0f 02 06 05 09 0f 09 09 61 ......a........a +| page 4 offset 12288 +| 0: 0a 00 00 00 07 0f d8 00 0f fc 0f f0 0f ea 0f e4 ................ +| 16: 0f de 0f d8 0f f6 00 00 00 00 00 00 00 00 00 00 ................ +| 4048: 00 00 00 00 00 00 00 00 05 03 01 01 07 07 05 03 ................ +| 4064: 01 01 06 06 05 03 01 01 05 05 05 03 01 01 04 04 ................ +| 4080: 05 03 01 01 03 03 05 03 01 01 0f 02 03 03 09 09 ................ +| page 5 offset 16384 +| 0: 0a 00 00 00 07 0f ca 00 0f fa 0f f2 0f ea 0f 00 ................ +| 4032: 00 00 00 00 00 00 00 00 00 00 07 04 01 0f 01 07 ................ +| 4048: 61 07 07 04 01 0f 01 06 61 06 07 04 01 0f 01 05 a.......a....... +| 4064: 61 05 07 04 01 1f 01 04 61 04 07 04 01 0f 01 03 a.......a....... +| 4080: 61 03 07 04 01 0f 01 02 61 02 05 04 09 0f 09 61 a.......a......a +| page 6 offset 20480 +| 0: 0a 00 00 00 07 0f ca 00 0f fa 0f ea 0f e2 00 00 ................ +| 4032: 00 00 00 00 00 00 00 00 00 00 07 04 01 0f 01 07 ................ +| 4048: 61 07 07 04 01 0f 01 06 61 06 07 04 01 0f 01 05 a.......a....... +| 4064: 61 05 07 04 01 0f 01 04 61 04 07 04 01 0f 01 03 a.......a....... +| 4080: 61 03 07 04 01 0f 01 0f 61 02 05 04 09 0f 09 61 a.......a......a +| page 7 offset 24576 +| 0: 0d 00 00 00 05 0f 1c 00 0f f0 0f e0 0f d3 0f c5 ................ +| 16: 0f b8 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 4016: 00 00 00 00 00 00 00 00 0b 05 04 11 11 13 62 31 ..............b1 +| 4032: 62 31 37 20 31 0c 04 04 11 13 13 62 31 62 31 63 b17 1......b1b1c +| 4048: 37 20 31 0b 03 04 11 11 13 63 31 63 31 37 20 31 7 1......c1c17 1 +| 4064: 0e 02 04 11 13 07 63 31 63 31 64 37 20 31 20 31 ......c1c1d7 1 1 +| 4080: 0e 01 04 11 13 17 63 31 63 31 63 37 20 31 00 00 ......c1c1c7 1.. +| end crash-3afa1ca9e9c1bd.db +}]} {} + +extra_schema_checks 0 +optimization_control db one-pass off +do_catchsql_test 15.1 { + PRAGMA cell_size_check = 0; + UPDATE c1 SET c= NOT EXISTS(SELECT 1 FROM c1 ORDER BY (SELECT 1 FROM c1 ORDER BY a)) +10 WHERE d BETWEEN 4 AND 7; +} {1 {database disk image is malformed}} +extra_schema_checks 1 +do_execsql_test 15.2 { + PRAGMA integrity_check; +} {/in database main/} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 16.0 { + CREATE TABLE t1(w, x, y, z, UNIQUE(w, x), UNIQUE(y, z)); + INSERT INTO t1 VALUES(1, 1, 1, 1); + + CREATE TABLE t1idx(x, y, i INTEGER, PRIMARY KEY(x)) WITHOUT ROWID; + INSERT INTO t1idx VALUES(10, NULL, 5); + + PRAGMA writable_schema = 1; + UPDATE sqlite_master SET rootpage = ( + SELECT rootpage FROM sqlite_master WHERE name='t1idx' + ) WHERE type = 'index'; +} + +extra_schema_checks 0 +db close +sqlite3 db test.db +extra_schema_checks 1 + +do_catchsql_test 16.1 { + PRAGMA writable_schema = ON; + INSERT INTO t1(rowid, w, x, y, z) VALUES(5, 10, 11, 10, NULL); +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +# Test that corruption is reported from within a checkpoint if the +# expected final size of the database (according to the last commit +# frame in the wal file) is greater than the combined initial sizes +# of the database and wal file. +# +if {[wal_is_capable]} { + reset_db + do_execsql_test 17.0 { + CREATE TABLE t1(o INTEGER PRIMARY KEY, t UNIQUE); + INSERT INTO t1(t) VALUES(randomblob(123)); + INSERT INTO t1(t) SELECT randomblob(123) FROM t1; + INSERT INTO t1(t) SELECT randomblob(123) FROM t1; + INSERT INTO t1(t) SELECT randomblob(123) FROM t1; + INSERT INTO t1(t) SELECT randomblob(123) FROM t1; + INSERT INTO t1(t) SELECT randomblob(123) FROM t1; + INSERT INTO t1(t) SELECT randomblob(123) FROM t1; + INSERT INTO t1(t) SELECT randomblob(123) FROM t1; + INSERT INTO t1(t) SELECT randomblob(123) FROM t1; + INSERT INTO t1(t) SELECT randomblob(123) FROM t1; + + PRAGMA journal_mode = wal; + INSERT INTO t1 VALUES(-1, 'b'); + } {wal} + + do_test 17.1 { + set fd [open test.db r+] + chan truncate $fd 2048 + file size test.db + } {2048} + + do_catchsql_test 17.2 { + PRAGMA wal_checkpoint + } {1 {database disk image is malformed}} + + do_test 17.3 { + close $fd + } {} +} + +#------------------------------------------------------------------------- +reset_db +do_test 18.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 12288 pagesize 4096 filename crash-40d5739835cbdb.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 00 .....@ ........ +| 96: 00 00 00 00 0d 00 00 00 02 0f 4e 00 0f a2 0f 4e ..........N....N +| 3904: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 52 02 ..............R. +| 3920: 07 17 11 11 01 81 0f 74 61 62 6c 65 74 32 74 32 .......tablet2t2 +| 3936: 03 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 32 .CREATE TABLE t2 +| 3952: 28 61 20 49 4e 54 2c 20 62 20 49 4e 54 45 47 45 (a INT, b INTEGE +| 3968: 52 2c 20 50 52 49 4d 41 52 59 20 4b 45 59 28 61 R, PRIMARY KEY(a +| 3984: 2c 62 29 29 20 57 49 54 48 4f 55 54 20 52 4f 57 ,b)) WITHOUT ROW +| 4000: 49 44 5c 01 07 16 11 11 01 81 23 74 61 62 6c 65 ID........#table +| 4016: 74 31 74 31 02 43 52 45 41 54 45 20 54 41 42 4c t1t1.CREATE TABL +| 4032: 45 20 74 31 28 61 20 49 4e 54 20 50 52 49 4d 41 E t1(a INT PRIMA +| 4048: 52 59 20 4b 45 59 2c 20 62 20 54 45 58 54 2c 20 RY KEY, b TEXT, +| 4064: 63 20 54 45 58 54 2c 20 64 20 49 4e 54 45 47 45 c TEXT, d INTEGE +| 4080: 52 29 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 R) WITHOUT ROWID +| page 2 offset 4096 +| 0: 0a 00 00 00 06 0f a7 00 0f f4 0f e5 0f d5 0f c5 ................ +| 16: 0f b6 0f 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 4000: 00 00 00 00 00 00 00 0f 05 01 15 13 01 06 65 7f ..............e. +| 4016: 25 6e 73 69 78 06 0e 05 01 13 15 03 b5 6f 64 64 %nsix........odd +| 4032: 66 69 76 65 05 0f 05 01 15 15 01 04 65 76 65 61 five........evea +| 4048: e6 6f 75 82 04 0f 05 01 13 17 01 03 6f 64 64 74 .ou.........oddt +| 4064: 68 72 61 15 03 0e 05 01 15 12 01 02 64 76 64 6e hra.........dvdn +| 4080: 74 77 6f 02 00 00 00 00 00 00 00 00 00 00 00 00 two............. +| page 3 offset 8192 +| 2816: 00 00 00 00 00 00 00 00 00 00 00 06 03 02 01 00 ................ +| 2832: c8 07 06 03 02 01 00 c7 11 06 03 02 01 02 a6 52 ...............R +| 2848: 06 d5 02 01 10 c5 1b 06 03 02 00 ef c4 53 06 03 .............S.. +| 2864: 02 01 00 c3 22 06 04 02 01 00 c2 26 06 03 02 01 ...........&.... +| 2880: 00 c2 1e 02 b3 02 01 00 c0 3a 06 03 3c 01 00 bf .........:..<... +| 2896: 2c 06 03 02 01 00 be 27 00 83 02 01 01 bd 15 06 ,......'........ +| 2912: 03 02 01 00 bc 21 06 03 02 01 00 bb 54 16 13 02 .....!......T... +| 2928: 01 09 9a 0a 06 03 02 01 00 b9 53 06 03 02 01 00 ..........S..... +| 2944: b8 52 06 13 02 01 00 b7 1e 06 03 02 01 00 b6 34 .R.............4 +| 2960: 06 13 02 01 00 b5 3a 05 f3 12 01 00 b4 45 05 03 ......:......E.. +| 2976: 02 00 00 b4 6f 06 03 02 01 00 b2 03 06 03 02 01 ....o........... +| 2992: 00 b1 63 06 03 02 01 00 b0 24 06 03 02 01 00 9f ..c......$...... +| 3008: ac 06 03 02 01 00 a2 2f 07 03 02 01 01 ad 21 06 ......./......!. +| 3024: 03 02 01 fb cd 5b 06 c0 01 f1 00 ab 23 06 03 02 .....[......#... +| 3040: 01 00 aa 5b 06 03 02 01 00 a3 ce 06 02 03 01 00 ...[............ +| 3056: a8 0e 06 03 02 01 00 a7 0c 06 02 f1 01 00 a6 0d ................ +| 3072: 06 03 02 01 00 95 25 06 03 02 01 00 a4 17 06 03 ......%......... +| 3088: 02 01 00 a3 09 06 03 02 01 00 a2 51 06 03 02 02 ...........Q.... +| 3104: 00 a1 40 06 01 e2 00 00 a0 4b 06 13 02 00 00 9e ..@......K...... +| 3120: 5d 06 03 02 01 10 9e 81 06 03 02 01 00 9d 42 06 ].............B. +| 3136: 03 69 01 00 9c 48 06 03 02 01 00 9b 48 06 03 01 .i...H......H... +| 3152: 01 00 9a 09 06 03 02 01 00 99 2f 06 03 02 01 00 ........../..... +| 3168: 98 3a 06 03 02 01 00 97 24 06 03 02 01 00 96 4a .:......$......J +| 3184: 06 03 02 11 00 f9 50 02 93 02 01 00 94 2f 06 03 ......P....../.. +| 3200: 02 11 04 93 1a 06 03 01 04 e0 92 1a 06 03 02 01 ................ +| 3216: 00 91 27 06 03 02 01 00 90 23 06 03 02 01 00 8f ..'......#...... +| 3232: 3b 06 03 02 01 00 8e 46 06 16 02 01 00 8d 1d 07 ;......F........ +| 3248: 23 12 01 00 8c 5a 06 03 02 01 00 8a 39 06 03 02 #....Z......9... +| 3264: 00 ff 84 b5 06 03 02 01 00 89 07 06 03 02 11 00 ................ +| 3280: 88 02 06 03 02 01 00 87 19 06 03 02 01 00 86 4d ...............M +| 3296: 06 13 12 00 00 85 4b 06 03 02 01 00 84 37 06 13 ......K......7.. +| 3312: 02 01 00 83 2c 06 03 02 01 00 81 60 06 13 02 11 ....,......`.... +| 3328: 00 81 3b 06 03 02 01 0a b0 5a 06 03 01 01 7f 22 ..;......Z...... +| 3344: 05 03 01 01 7e 21 05 03 01 01 7d 0b 15 03 01 02 ....~!.......... +| 3360: 7b 08 05 03 06 91 7b 22 05 03 01 01 7a 58 05 03 ............zX.. +| 3376: 01 01 7a 4f 05 03 01 01 78 49 05 03 01 01 77 16 ..zO....xI....w. +| 3392: 05 03 01 01 76 5f 05 03 01 01 75 0f 05 03 01 01 ....v_....u..... +| 3408: 74 2f 05 03 01 01 3f 1f 05 03 01 02 72 14 05 03 t/....?.....r... +| 3424: 00 f1 71 08 05 03 01 01 70 0c 05 03 01 47 7f 29 ..q.....p....G.) +| 3440: 05 03 01 01 6e 57 05 03 01 01 6d 33 05 13 00 f1 ....nW....m3.... +| 3456: 6c 0b 05 03 01 01 6b 49 05 03 01 01 69 05 05 03 l.....kI....i... +| 3472: 01 02 ed 23 00 00 01 00 00 00 00 00 00 00 00 00 ...#............ +| end crash-40d5739835cbdb.db +}]} {} + +ifcapable json1 { +do_catchsql_test 18.1 { + SELECT + json_group_array(c) OVER win4 + FROM t1 + WINDOW win4 AS ( + ORDER BY a COLLATE nocase RANGE BETWEEN 1.0 PRECEDING AND CURRENT ROW + ) +} {1 {JSON cannot hold BLOB values}} +} ;# ifcapable json1 + +#------------------------------------------------------------------------- +reset_db +do_test 19.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 20480 pagesize 4096 filename crash-f022eb0ce64d27.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 05 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 02 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 96: 00 00 00 00 0d 0f f8 00 04 0e d2 00 0f 08 0f d3 ................ +| 112: 0f ae 0e d2 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 3792: 00 00 34 04 06 17 0f 11 01 57 69 6e 64 65 78 61 ..4......Windexa +| 3808: 74 31 05 43 52 45 41 54 45 20 55 4e 49 51 55 45 t1.CREATE UNIQUE +| 3824: 20 49 4e 44 45 58 20 61 20 4f 4e 20 74 31 28 61 INDEX a ON t1(a +| 3840: 2c 20 30 20 7c 20 61 29 81 23 01 07 17 11 11 01 , 0 | a).#...... +| 3856: 82 31 74 61 62 6c 65 74 31 74 31 02 43 52 45 41 .1tablet1t1.CREA +| 3872: 54 45 20 54 41 42 4c 45 20 74 31 28 0a 20 20 20 TE TABLE t1(. +| 3888: 20 67 63 62 20 41 53 20 28 62 2a 31 29 2c 0a 20 gcb AS (b*1),. +| 3904: 20 20 20 61 20 49 34 54 45 47 45 52 20 50 52 49 a I4TEGER PRI +| 3920: 4d 41 52 59 20 4b 45 59 2c 0a 20 20 20 20 67 63 MARY KEY,. gc +| 3936: 63 20 41 53 20 28 74 32 2b 30 29 2c 0a 20 20 20 c AS (t2+0),. +| 3952: 20 62 20 55 4e 49 51 55 45 2c 0a 20 20 20 20 67 b UNIQUE,. g +| 3968: 63 61 20 41 53 20 28 31 2a 61 2b 30 29 2c 0a 20 ca AS (1*a+0),. +| 3984: 20 20 20 74 32 20 55 4e 49 51 55 45 0a 20 20 29 t2 UNIQUE. ) +| 4000: 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 23 03 WITHOUT ROWID#. +| 4016: 06 17 37 11 01 00 69 6e 64 65 78 73 71 6c 69 74 ..7...indexsqlit +| 4032: 65 5f 61 75 74 6f 69 6e 64 65 78 5f 74 31 5f 33 e_autoindex_t1_3 +| 4048: 74 31 02 23 02 06 17 37 11 01 00 69 6e 64 65 78 t1.#...7...index +| 4064: 73 71 6c 69 74 65 5f 61 75 74 6f 69 6e 64 65 78 sqlite_autoindex +| 4080: 5f 74 31 5f 32 74 31 02 00 00 00 08 00 00 00 00 _t1_2t1......... +| page 2 offset 4096 +| 0: 0a 00 00 00 05 0f d8 00 0f f8 0f f8 9f e8 0f e0 ................ +| 16: 0f d8 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 4048: 00 00 00 00 00 00 00 00 07 04 01 02 00 0f 13 88 ................ +| 4064: 07 04 01 02 00 0e 0f a0 07 04 01 02 00 0d 0b b8 ................ +| 4080: 07 04 01 02 00 0c 07 d0 07 04 01 02 00 0b 03 e8 ................ +| page 5 offset 16384 +| 0: 0a 00 00 00 05 0f e2 00 0f fa 0f f4 0f ee 0f e8 ................ +| 16: 0f e2 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 4064: 00 00 05 03 01 01 0f 0f 05 03 01 01 0e 0e 05 03 ................ +| 4080: 01 01 0d 0d 05 03 01 01 0c 0c 05 03 01 01 0b 0b ................ +| end crash-f022eb0ce64d27.db +}]} {} + +do_execsql_test 19.1 { + PRAGMA writable_schema=ON; +} +do_catchsql_test 19.2 { + UPDATE t1 SET a=1; +} {1 {database disk image is malformed}} + +reset_db +do_execsql_test 19.3 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT, c INTEGER, d TEXT); + CREATE INDEX i1 ON t1((NULL)); + INSERT INTO t1 VALUES(1, NULL, 1, 'text value'); + PRAGMA writable_schema = on; + UPDATE sqlite_schema SET + sql = 'CREATE INDEX i1 ON t1(b, c, d)', + tbl_name = 't1', + type='index' + WHERE name='i1'; +} +db close +sqlite3 db test.db +do_catchsql_test 19.4 { + PRAGMA integrity_check; +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +reset_db +do_test 18.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 20480 pagesize 4096 filename crash-a4150b729051e4.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 05 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 04 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 64: 00 00 00 00 00 00 00 00 00 00 ff f0 00 00 00 00 ................ +| 96: 00 00 00 00 0d 00 00 00 04 0e e5 00 0f c2 0f 75 ...............u +| 112: 0f 19 0e e5 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 3808: 00 00 00 00 00 32 04 06 17 17 11 01 4b 69 6e 64 .....2......Kind +| 3824: 65 78 74 31 61 62 63 74 31 05 43 52 45 41 54 45 ext1abct1.CREATE +| 3840: 20 49 4e 44 45 58 20 74 31 61 62 63 20 4f 4e 20 INDEX t1abc ON +| 3856: 74 31 28 61 2c 62 2c 63 29 5a 03 06 17 25 25 01 t1(a,b,c)Z...%%. +| 3872: 79 74 61 62 6c 65 73 71 6c 69 74 65 5f 73 74 61 ytablesqlite_sta +| 3888: 74 34 73 71 6c 69 74 65 5f 73 74 61 74 34 04 43 t4sqlite_stat4.C +| 3904: 52 45 41 54 45 20 54 41 42 4c 45 20 73 71 6c 69 REATE TABLE sqli +| 3920: 74 65 5f 73 74 61 74 34 28 74 62 6c 2c 69 64 78 te_stat4(tbl,idx +| 3936: 2c 6e 65 71 2c 6e 6c 74 2c 6e 64 6c 74 2c 73 61 ,neq,nlt,ndlt,sa +| 3952: 6d 70 6c 65 29 4b 02 06 17 25 25 01 5b 74 61 62 mple)K...%%.[tab +| 3968: 6c 65 73 71 6c 69 74 65 5f 73 74 61 74 31 73 71 lesqlite_stat1sq +| 3984: 6c 69 74 65 5f 73 74 61 74 31 03 43 52 45 41 54 lite_stat1.CREAT +| 4000: 45 20 54 41 42 4c 45 20 73 71 6c 69 74 65 5f 73 E TABLE sqlite_s +| 4016: 74 61 74 31 28 74 62 6c 2c 69 64 78 2c 73 74 61 tat1(tbl,idx,sta +| 4032: 74 29 3c 01 06 17 11 11 01 65 74 61 62 6c 65 74 t)<......etablet +| 4048: 31 74 31 02 43 52 45 41 54 45 20 54 41 42 4c 45 1t1.CREATE TABLE +| 4064: 20 74 31 28 61 20 54 45 58 54 2c 20 62 20 49 4e t1(a TEXT, b IN +| 4080: 54 2c 20 63 20 49 4e 54 2c 20 64 20 49 4e 54 29 T, c INT, d INT) +| page 2 offset 4096 +| 0: 0d 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 4000: 0b 07 05 13 01 01 01 62 63 64 64 06 0b 0c 06 05 .......bcdd..... +| 4016: 13 02 01 01 64 65 66 01 59 09 0a 0c 05 05 13 03 ....def.Y....... +| 4032: 01 01 64 65 66 02 6f 08 09 0c 04 05 13 02 01 01 ..def.o......... +| 4048: 61 62 63 01 59 07 08 0c 03 05 13 02 01 01 87 62 abc.Y..........b +| 4064: 63 00 ea 06 07 0c 02 05 13 02 01 01 61 62 63 00 c...........abc. +| 4080: ea 06 06 0b 01 05 13 01 01 01 61 62 63 7b 04 04 ..........abc... +| page 3 offset 8192 +| 0: 0d 00 00 00 01 0f e0 00 0f e1 00 00 00 00 00 00 ................ +| 4064: 00 1d 01 04 11 17 31 74 31 74 31 61 62 63 31 30 ......1t1t1abc10 +| 4080: 30 30 30 20 35 30 30 30 20 32 30 30 30 20 31 30 000 5000 2000 10 +| page 4 offset 12288 +| 0: 0d 00 00 00 07 0e ac 00 0f d1 0f a0 0f 6f 0f 3e .............o.> +| 16: 0f 0e 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 3744: 00 00 00 00 00 00 00 00 00 00 00 00 2f 07 07 11 ............/... +| 3760: 17 1b 1b 1b 24 74 31 74 31 61 62 63 32 20 31 20 ....$t1t1abc2 1 +| 3776: 31 20 31 35 20 36 20 36 20 36 32 20 35 20 36 20 1 15 6 6 62 5 6 +| 3792: 36 05 13 02 01 01 64 65 66 02 37 08 05 2f 06 07 6.....def.7../.. +| 3808: 11 17 1b 1b 1b 24 74 41 74 31 61 62 63 32 20 31 .....$tAt1abc2 1 +| 3824: 20 31 20 31 35 20 35 20 55 20 35 32 20 34 20 35 1 15 5 U 52 4 5 +| 3840: 20 35 05 13 02 01 01 64 65 66 01 59 09 06 2e 05 5.....def.Y.... +| 3856: 07 11 17 1b 1b 1b 22 74 31 74 31 61 62 63 31 20 .......t1t1abc1 +| 3872: 31 20 31 20 31 34 20 34 20 34 20 34 31 20 33 20 1 1 14 4 4 41 3 +| 3888: 34 20 34 08 b3 cd f0 f1 62 63 64 64 06 07 2f 05 4 4.....bcdd../. +| 3904: 07 11 17 1b 1b 1b 24 74 37 74 31 61 62 63 34 20 ......$t7t1abc4 +| 3920: 31 20 31 20 31 30 20 33 20 33 20 33 30 20 32 20 1 1 10 3 3 30 2 +| 3936: 33 20 33 05 13 02 01 01 61 62 63 01 59 07 04 2f 3 3.....abc.Y../ +| 3952: 03 07 11 17 1b 1b 1b 24 74 31 74 31 61 62 63 34 .......$t1t1abc4 +| 3968: 20 32 20 31 20 31 30 20 31 20 32 20 32 30 20 31 2 1 10 1 2 20 1 +| 3984: 20 32 20 32 05 13 02 01 01 61 62 63 00 ea 06 03 2 2.....abc.... +| 4000: 2f 02 07 11 17 1b 1b 1b 24 74 31 74 31 61 62 63 /.......$t1t1abc +| 4016: 34 20 32 20 31 20 31 30 20 31 20 31 20 31 30 20 4 2 1 10 1 1 10 +| 4032: 31 20 31 20 31 05 13 02 01 01 61 62 63 00 ea 05 1 1 1.....abc... +| 4048: 02 2d 01 07 11 17 1b 1b 1b 20 74 31 74 31 61 62 .-....... t1t1ab +| 4064: 63 34 20 31 20 31 20 31 30 20 30 20 30 1f 30 30 c4 1 1 10 0 0.00 +| 4080: 20 30 20 30 20 30 05 13 01 01 09 61 62 63 7b 04 0 0 0.....abc.. +| page 5 offset 16384 +| 0: 0a 00 00 00 07 0f a8 00 0f f5 00 00 00 00 00 00 ................ +| 4000: 00 00 00 00 00 00 00 00 0c 05 13 02 01 01 64 65 ..............de +| 4016: 66 02 37 08 05 0c 05 13 02 01 01 64 65 66 01 59 f.7........def.Y +| 4032: 09 06 0b 05 12 01 01 01 62 63 64 64 06 07 0c 05 ........bcdd.... +| 4048: 13 02 01 01 61 62 63 01 59 07 01 2c 05 13 02 01 ....abc.Y..,.... +| 4064: 01 61 62 63 00 ea 06 03 0c 05 13 02 01 01 61 62 .abc..........ab +| 4080: 63 00 ea 05 00 00 00 00 00 00 00 00 00 00 00 00 c............... +| end crash-a4150b729051e4.db +}]} {} + +do_catchsql_test 18.1 { + SELECT a FROM t1 WHERE b GLOB b AND b GLOB '0^x]␅6␚xz]'; +} {1 {database disk image is malformed}} + +finish_test diff --git a/test/corruptM.test b/test/corruptM.test new file mode 100644 index 0000000000..15fc8d6d0d --- /dev/null +++ b/test/corruptM.test @@ -0,0 +1,186 @@ +# 2019-08-12 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# Check to ensure that the type, name, and tbl_name fields of the +# sqlite_master table are validated and errors are reported if they +# are inconsistent with the sql. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix corruptM + +# These tests deal with corrupt database files +# +database_may_be_corrupt + +proc open_db2_and_catchsql {sql} { + set rc [catch { sqlite3 db2 test.db } msg] + if {$rc} { + return [list $rc $msg] + } + set res [catchsql $sql db2] + db2 close + set res +} + +db close +forcedelete test.db +sqlite3 db test.db +do_execsql_test corruptM-100 { + CREATE TABLE t1(a,b,c); + INSERT INTO t1 VALUES(111,222,333); + CREATE INDEX i1 ON t1(b); + CREATE VIEW v2 AS SELECT 15,22; + CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN SELECT 5; END; + SELECT type, name, tbl_name, '|' FROM sqlite_master; +} {table t1 t1 | index i1 t1 | view v2 v2 | trigger r1 t1 |} +do_execsql_test corruptM-101 { + PRAGMA writable_schema=on; + UPDATE sqlite_master SET tbl_name=NULL WHERE name='t1'; + SELECT type, name, tbl_name, '|' FROM sqlite_master; +} {table t1 {} | index i1 t1 | view v2 v2 | trigger r1 t1 |} +do_test corruptM-102 { + open_db2_and_catchsql { + PRAGMA quick_check; + } +} {1 {malformed database schema (t1)}} + +do_execsql_test corruptM-110 { + UPDATE sqlite_master SET tbl_name='tx' WHERE name='t1'; + SELECT type, name, tbl_name, '|' FROM sqlite_master; +} {table t1 tx | index i1 t1 | view v2 v2 | trigger r1 t1 |} +do_test corruptM-111 { + open_db2_and_catchsql { + PRAGMA quick_check; + } +} {1 {malformed database schema (t1)}} +do_execsql_test corruptM-112 { + UPDATE sqlite_master SET tbl_name='t1', type='tabl' WHERE name='t1'; + SELECT type, name, tbl_name, '|' FROM sqlite_master; +} {tabl t1 t1 | index i1 t1 | view v2 v2 | trigger r1 t1 |} +do_test corruptM-113 { + open_db2_and_catchsql { + PRAGMA quick_check; + } +} {1 {malformed database schema (t1)}} +do_execsql_test corruptM-114 { + UPDATE sqlite_master SET tbl_name='t9',type='table',name='t9'WHERE name='t1'; + SELECT type, name, tbl_name, '|' FROM sqlite_master; +} {table t9 t9 | index i1 t1 | view v2 v2 | trigger r1 t1 |} +do_test corruptM-114 { + open_db2_and_catchsql { + PRAGMA quick_check; + } +} {1 {malformed database schema (t9)}} + +do_execsql_test corruptM-120 { + UPDATE sqlite_master SET name='t1',tbl_name='T1' WHERE name='t9'; + SELECT type, name, tbl_name, '|' FROM sqlite_master; +} {table t1 T1 | index i1 t1 | view v2 v2 | trigger r1 t1 |} +do_test corruptM-121 { + open_db2_and_catchsql { + PRAGMA quick_check; + SELECT * FROM t1, v2; + } +} {0 {ok 111 222 333 15 22}} + +do_execsql_test corruptM-130 { + UPDATE sqlite_master SET type='view' WHERE name='t1'; + SELECT type, name, tbl_name, '|' FROM sqlite_master; +} {view t1 T1 | index i1 t1 | view v2 v2 | trigger r1 t1 |} +do_test corruptM-131 { + open_db2_and_catchsql { + PRAGMA quick_check; + SELECT * FROM t1, v2; + } +} {1 {malformed database schema (t1)}} + +do_execsql_test corruptM-140 { + UPDATE sqlite_master SET type='table', tbl_name='t1' WHERE name='t1'; + UPDATE sqlite_master SET tbl_name='tx' WHERE name='i1'; + SELECT type, name, tbl_name, '|' FROM sqlite_master; +} {table t1 t1 | index i1 tx | view v2 v2 | trigger r1 t1 |} +do_test corruptM-141 { + open_db2_and_catchsql { + PRAGMA quick_check; + SELECT * FROM t1, v2; + } +} {1 {malformed database schema (i1)}} + +do_execsql_test corruptM-150 { + UPDATE sqlite_master SET type='table', tbl_name='t1' WHERE name='i1'; + SELECT type, name, tbl_name, '|' FROM sqlite_master; +} {table t1 t1 | table i1 t1 | view v2 v2 | trigger r1 t1 |} +do_test corruptM-151 { + open_db2_and_catchsql { + PRAGMA quick_check; + SELECT * FROM t1, v2; + } +} {1 {malformed database schema (i1)}} + +do_execsql_test corruptM-160 { + UPDATE sqlite_master SET type='view', tbl_name='t1' WHERE name='i1'; + SELECT type, name, tbl_name, '|' FROM sqlite_master; +} {table t1 t1 | view i1 t1 | view v2 v2 | trigger r1 t1 |} +do_test corruptM-161 { + open_db2_and_catchsql { + PRAGMA quick_check; + SELECT * FROM t1, v2; + } +} {1 {malformed database schema (i1)}} + +do_execsql_test corruptM-170 { + UPDATE sqlite_master SET type='index', tbl_name='t1' WHERE name='i1'; + UPDATE sqlite_master SET type='table', tbl_name='v2' WHERE name='v2'; + SELECT type, name, tbl_name, '|' FROM sqlite_master; +} {table t1 t1 | index i1 t1 | table v2 v2 | trigger r1 t1 |} +do_test corruptM-171 { + open_db2_and_catchsql { + PRAGMA quick_check; + SELECT * FROM t1, v2; + } +} {1 {malformed database schema (v2)}} + +do_execsql_test corruptM-180 { + UPDATE sqlite_master SET type='view',name='v3',tbl_name='v3' WHERE name='v2'; + SELECT type, name, tbl_name, '|' FROM sqlite_master; +} {table t1 t1 | index i1 t1 | view v3 v3 | trigger r1 t1 |} +do_test corruptM-181 { + open_db2_and_catchsql { + PRAGMA quick_check; + SELECT * FROM t1, v2; + } +} {1 {malformed database schema (v3)}} + +do_execsql_test corruptM-190 { + UPDATE sqlite_master SET type='view',name='v2',tbl_name='v2' WHERE name='v3'; + UPDATE sqlite_master SET type='view' WHERE name='r1'; + SELECT type, name, tbl_name, '|' FROM sqlite_master; +} {table t1 t1 | index i1 t1 | view v2 v2 | view r1 t1 |} +do_test corruptM-191 { + open_db2_and_catchsql { + PRAGMA quick_check; + SELECT * FROM t1, v2; + } +} {1 {malformed database schema (r1)}} +do_execsql_test corruptM-192 { + UPDATE sqlite_master SET type='trigger',tbl_name='v2' WHERE name='r1'; + SELECT type, name, tbl_name, '|' FROM sqlite_master; +} {table t1 t1 | index i1 t1 | view v2 v2 | trigger r1 v2 |} +do_test corruptM-193 { + open_db2_and_catchsql { + PRAGMA quick_check; + SELECT * FROM t1, v2; + } +} {1 {malformed database schema (r1)}} + +finish_test diff --git a/test/corruptN.test b/test/corruptN.test new file mode 100644 index 0000000000..2297991aba --- /dev/null +++ b/test/corruptN.test @@ -0,0 +1,297 @@ +# 2020-12-16 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix corruptN + +# These tests deal with corrupt database files +# +database_may_be_corrupt + +reset_db +do_test 1.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 4096 pagesize 512 filename sql024239.txt.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 02 00 01 01 00 40 20 20 00 00 00 0c 00 00 00 07 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 08 00 00 00 04 ................ +| 48: 00 00 00 00 89 00 00 04 00 10 00 01 0a 00 00 01 ................ +| 80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0c ................ +| 96: 00 2e 2c 50 0d 00 00 00 06 01 06 00 01 da 01 b0 ..,P............ +| 112: 01 56 01 86 01 2a 01 06 00 00 00 00 00 00 00 00 .V...*.......... +| 256: 00 00 00 00 00 00 22 07 06 17 11 11 01 31 74 61 .............1ta +| 272: 62 6c 65 74 34 74 34 07 43 52 45 41 54 45 20 54 blet4t4.CREATE T +| 288: 41 42 4c 45 20 74 34 28 78 29 2a 06 06 17 13 11 ABLE t4(x)*..... +| 304: 01 3f 69 6e 64 65 78 74 33 78 74 33 05 43 52 45 .?indext3xt3.CRE +| 320: 41 54 45 20 49 4e 44 45 58 20 74 33 78 20 4f 4e ATE INDEX t3x ON +| 336: 20 74 33 28 78 29 2e 04 06 17 15 11 01 45 69 6e t3(x).......Ein +| 352: 64 65 78 74 32 63 64 74 32 05 43 52 45 41 54 45 dext2cdt2.CREATE +| 368: 20 49 4e 44 45 58 20 74 32 63 64 20 4f 4e 20 74 INDEX t2cd ON t +| 384: 32 28 63 2c 64 29 28 05 06 17 11 11 01 3d 74 61 2(c,d)(......=ta +| 400: 62 6c 65 74 33 74 33 07 43 52 45 41 54 45 20 54 blet3t3.CREATE T +| 416: 41 42 4c 45 20 74 33 28 63 2c 78 2c 65 2c 66 29 ABLE t3(c,x,e,f) +| 432: 28 02 06 17 11 11 01 3d 74 61 62 6c 65 74 32 74 (......=tablet2t +| 448: 32 03 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 2.CREATE TABLE t +| 464: 32 28 63 2c 64 2c 65 2c 66 29 24 01 06 17 11 11 2(c,d,e,f)$..... +| 480: 01 35 74 61 62 6c 65 74 31 74 31 02 43 52 45 41 .5tablet1t1.CREA +| 496: 54 45 20 54 41 42 4c 45 20 74 31 28 61 2c 62 29 TE TABLE t1(a,b) +| page 2 offset 512 +| 0: 0d 00 00 00 04 01 41 00 01 fa 01 f3 01 de 01 cf ......A......... +| 160: 00 00 20 00 00 00 00 00 00 00 00 00 00 00 00 00 .. ............. +| 448: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0d ................ +| 464: 04 03 17 17 73 65 76 65 6e 65 69 67 68 74 13 03 ....seveneight.. +| 480: 03 07 07 40 14 00 00 00 00 00 00 40 18 00 00 00 ...@.......@.... +| 496: 00 00 00 05 02 03 01 01 03 04 04 01 03 09 01 02 ................ +| page 3 offset 1024 +| 0: 0d 00 00 00 08 01 54 00 01 f7 01 ec 01 c5 01 aa ......T......... +| 16: 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 112: 00 00 dd 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 336: 00 00 00 00 19 08 05 17 17 17 17 65 69 67 68 74 ...........eight +| 352: 65 69 67 68 74 73 65 76 65 6e 73 65 76 65 6e 25 eightsevenseven% +| 368: 07 05 07 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 432: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 08 ................ +| 480: 00 00 0f 04 17 17 01 65 69 67 68 74 65 69 67 68 .......eighteigh +| 496: 74 08 15 04 07 07 01 40 18 00 00 00 00 00 00 40 t......@.......@ +| page 4 offset 1536 +| 0: 18 00 00 00 00 00 00 07 07 04 01 01 01 04 04 06 ................ +| 16: 07 04 01 01 01 02 02 05 0f 04 17 17 01 73 6d 76 .............smv +| 32: 65 6e 65 69 67 68 74 04 15 04 07 07 01 40 14 00 eneight......@.. +| page 5 offset 2048 +| 0: 0a 00 00 00 08 01 96 00 01 fa 01 c4 01 f2 01 bc ................ +| 16: 01 dc 01 e1 01 96 01 cc 00 00 00 00 00 00 00 00 ................ +| 160: 00 00 00 00 00 00 32 00 00 00 00 00 00 00 00 00 ......2......... +| 368: 00 00 00 08 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 400: 00 00 00 00 00 00 0f 04 17 17 01 85 69 67 68 74 ............ight +| 416: 65 69 67 68 74 08 15 04 07 07 01 40 18 00 00 00 eight......@.... +| 432: 00 00 00 40 18 00 00 00 00 00 00 07 07 04 01 01 ...@............ +| 448: 01 04 04 06 07 04 01 01 01 02 02 05 0f 04 17 17 ................ +| 464: 01 73 6d 76 65 6e 65 69 67 68 74 04 15 04 07 07 .smveneight..... +| 480: 01 40 14 00 00 00 00 00 00 40 18 00 00 00 00 00 .@.......@...... +| 496: 00 03 07 04 01 01 01 03 04 02 05 04 03 01 09 02 ................ +| page 6 offset 2560 +| 0: 0a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 16: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 10 00 ................ +| 304: 00 00 00 26 00 00 00 00 00 00 00 00 00 00 00 00 ...&............ +| page 7 offset 3072 +| 0: 0d 00 00 00 08 01 c2 00 01 fb 01 f6 01 f1 01 ec ................ +| 16: 01 e0 01 d4 01 cb 01 c2 00 00 00 00 00 00 00 00 ................ +| 128: 00 00 00 00 00 00 00 00 00 00 00 00 00 20 00 04 ............. .. +| 384: 00 00 00 00 00 00 00 00 00 07 08 02 17 65 69 fc .............ei. +| 400: 68 74 07 07 02 17 65 69 67 68 74 0a fb fd f8 bf ht....eight..... +| 416: e7 ff ff ff 00 00 00 0a 05 02 07 40 18 00 00 00 ...........@.... +| 432: 00 00 00 03 04 02 01 04 03 03 02 01 04 03 02 01 ................ +| 448: ff ff ff ff ff ff 00 00 00 00 00 00 00 00 00 00 ................ +| end sql024239.txt.db +}]} {} + +do_catchsql_test 1.1 { + VACUUM; +} {1 {database disk image is malformed}} + +# 2021-04-05 dbsqlfuzz b92b72e4de80b5140c30ab71372ca719b8feb618 +do_test 2.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 16384 pagesize 4096 filename c-b92b.txt.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 04 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 03 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 96: 00 00 00 00 0d 0f f8 00 04 0f 12 00 0f 91 0f d3 ................ +| 112: 0f 67 0f 12 00 00 00 00 00 00 00 00 00 00 00 00 .g.............. +| 3856: 00 00 53 04 07 1b 13 11 08 81 0d 74 72 69 67 67 ..S........trigg +| 3872: 65 72 74 72 30 74 31 43 52 45 41 54 45 20 54 52 ertr0t1CREATE TR +| 3888: 49 47 47 45 52 20 74 72 30 20 44 45 4c 45 54 45 IGGER tr0 DELETE +| 3904: 20 4f 4e 20 74 31 20 42 45 47 49 4e 0a 20 20 55 ON t1 BEGIN. U +| 3920: 50 44 41 54 45 20 74 31 20 53 45 54 20 62 20 3d PDATE t1 SET b = +| 3936: 20 61 3b 0a 45 4e 44 28 03 06 17 11 11 01 3d 69 a;.END(......=i +| 3952: 6e 64 65 78 69 30 74 31 04 43 52 45 41 54 45 20 ndexi0t1.CREATE +| 3968: 49 4e 44 45 58 20 69 30 20 4f 4e 20 74 31 28 62 INDEX i0 ON t1(b +| 3984: 29 40 01 06 17 11 11 01 6d 74 61 62 6c 65 74 31 )@......mtablet1 +| 4000: 74 31 02 43 52 45 41 54 45 20 54 41 42 4c 45 20 t1.CREATE TABLE +| 4016: 74 31 28 61 20 55 4e 49 51 55 45 20 4f 4e 20 43 t1(a UNIQUE ON C +| 4032: 4f 4e 46 4c 49 43 54 20 52 45 50 4c 41 43 45 2c ONFLICT REPLACE, +| 4048: 20 62 29 23 02 06 17 37 11 01 00 69 6e 64 65 78 b)#...7...index +| 4064: 73 71 6c 69 74 65 5f 61 75 74 6f 69 6e 64 65 78 sqlite_autoindex +| 4080: 5f 74 31 5f 31 74 31 03 00 00 00 08 00 00 00 00 _t1_1t1......... +| page 2 offset 4096 +| 0: 0d 00 00 00 02 0f 00 00 00 00 00 00 00 00 00 00 ................ +| 4080: 00 00 05 02 03 01 01 09 0d 05 01 03 01 01 04 0c ................ +| page 3 offset 8192 +| 0: 0a 00 00 00 02 0f f5 00 0f fb 0f f5 00 00 00 00 ................ +| 4080: 00 00 00 00 00 05 03 01 01 09 02 04 03 01 09 04 ................ +| page 4 offset 12288 +| 0: 0a 00 00 00 02 0f f5 00 0f fb 0f f5 00 00 00 00 ................ +| 4080: 00 00 00 00 00 05 03 01 01 0d 02 04 03 00 00 00 ................ +| end c-b92b.txt.db +}]} {} + + +reset_db +if {![info exists ::G(perm:presql)]} { + do_execsql_test 3.0 { + CREATE TABLE t1(x INTEGER PRIMARY KEY AUTOINCREMENT, y); + PRAGMA writable_schema = 1; + UPDATE sqlite_schema + SET sql = 'CREATE TABLE sqlite_sequence(name-seq)' + WHERE name = 'sqlite_sequence'; + } + db close + sqlite3 db test.db + do_catchsql_test 3.1 { + PRAGMA writable_schema = 1; + INSERT INTO t1(y) VALUES('abc'); + } {1 {database disk image is malformed}} + reset_db + + do_execsql_test 4.1 { + CREATE TABLE x1(a INTEGER PRIMARY KEY, b UNIQUE, c UNIQUE); + INSERT INTO x1 VALUES(1, 1, 2); + INSERT INTO x1 VALUES(2, 2, 3); + INSERT INTO x1 VALUES(3, 3, 4); + INSERT INTO x1 VALUES(4, 5, 6); + PRAGMA writable_schema = 1; + + UPDATE sqlite_schema SET rootpage = ( + SELECT rootpage FROM sqlite_schema WHERE name = 'sqlite_autoindex_x1_2' + ) WHERE name = 'sqlite_autoindex_x1_1'; + } + + db close + sqlite3 db test.db + breakpoint + do_catchsql_test 4.2 { + PRAGMA writable_schema = 1; + REPLACE INTO x1 VALUES(5, 2, 3); + } {0 {}} + +} + +#------------------------------------------------------------------------- + +reset_db + +ifcapable json1&&vtab { + db func strreplace strreplace + proc strreplace {orig a b} { + string map [list $a $b] $orig + } + + do_execsql_test 5.0 { + CREATE TABLE t1(a, b); + CREATE INDEX t1a ON t1(a); + CREATE INDEX t1b ON t1(b); + + PRAGMA writable_schema = 1; + UPDATE sqlite_schema + SET sql = strreplace(sql, 't1', 'json_each') + WHERE type='index'; + } + + # Do not run this tests if there is any presql (SQL run from within + # the [sqlite3] command) configured. In this case the schema is parsed + # before the "PRAGMA writable_schema" command is executed and the + # script throws and exception. + if {[info exists ::G(perm:presql)]==0 || $::G(perm:presql)==""} { + db close + sqlite3 db test.db + + do_execsql_test 5.1 { + PRAGMA writable_schema = 1; + SELECT * FROM t1 + } + } +}; # ifcapable json1&&vtab + +#------------------------------------------------------------------------- +reset_db + +do_execsql_test 6.0 { + PRAGMA auto_vacuum = 0; + PRAGMA page_size=1024; + CREATE TABLE t1(a INTEGER PRIMARY KEY, b); + INSERT INTO t1(b) VALUES(zeroblob(300)),(zeroblob(300)),(zeroblob(300)),(zeroblob(300)); + CREATE TABLE t2(a); + CREATE TRIGGER t1tr BEFORE UPDATE ON t1 BEGIN DELETE FROM t2; END; + PRAGMA writable_schema=ON; + UPDATE sqlite_schema SET rootpage=3 WHERE rowid=2; + PRAGMA writable_schema=RESET; + INSERT INTO t2 VALUES('active'),('boomer'),('atom'),('atomic'), + ('alpha channel backup abandon test aback boomer atom alpha active'); +} +do_catchsql_test 6.1 { + UPDATE t1 SET b=zeroblob(299); +} {1 {database disk image is malformed}} + +reset_db +do_execsql_test 6.2 { + -- Make "t1" a large table. Large enough that the children of the root + -- node are interior nodes. + PRAGMA page_size = 1024; + PRAGMA auto_vacuum = 0; + CREATE TABLE t1(x); + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<500 + ) + INSERT INTO t1 SELECT zeroblob(300) FROM s; + + CREATE TABLE t2(y); + CREATE TRIGGER tr BEFORE UPDATE ON t1 BEGIN + DELETE FROM t2; + END; + + -- Set the root of table t2 to 137 - the leftmost child of the root of t1. + PRAGMA writable_schema = ON; + UPDATE sqlite_schema SET rootpage = 137 WHERE name='t2'; + PRAGMA writable_schema = RESET; +} + +do_catchsql_test 6.3 { + -- Run an UPDATE on t1 that will hit a child of page 136. Have the trigger + -- clear page 136 and its children. Assert fails. + UPDATE t1 SET x='hello world' WHERE rowid=1; +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 7.0 { + BEGIN; + CREATE TABLE p1(x PRIMARY KEY); + CREATE TABLE c1(y); + + PRAGMA schema_version = 0; + PRAGMA writable_schema = RESET; + + INSERT INTO c1 VALUES(1000); + ROLLBACK; +} + +do_execsql_test 7.1 { + PRAGMA table_info = p1; +} {0 x {} 0 {} 1} + +do_catchsql_test 7.2 { + SELECT * FROM p1; +} {1 {database disk image is malformed}} + +do_catchsql_test 7.3 { + PRAGMA integrity_check +} {1 {database disk image is malformed}} + + +finish_test diff --git a/test/cost.test b/test/cost.test index 9c10d821d9..6106caba8c 100644 --- a/test/cost.test +++ b/test/cost.test @@ -24,8 +24,9 @@ do_execsql_test 1.1 { do_eqp_test 1.2 { SELECT e FROM t3, t4 WHERE b=c ORDER BY b, d; } { - 0 0 0 {SCAN TABLE t3 USING COVERING INDEX i3} - 0 1 1 {SEARCH TABLE t4 USING INDEX i4 (c=?)} + QUERY PLAN + |--SCAN t3 USING COVERING INDEX i3 + `--SEARCH t4 USING INDEX i4 (c=?) } @@ -38,9 +39,7 @@ do_execsql_test 2.1 { # if the index is a non-covering index. do_eqp_test 2.2 { SELECT * FROM t1 ORDER BY a; -} { - 0 0 0 {SCAN TABLE t1 USING INDEX i1} -} +} {SCAN t1 USING INDEX i1} do_execsql_test 3.1 { CREATE TABLE t5(a INTEGER PRIMARY KEY,b,c,d,e,f,g); @@ -57,10 +56,15 @@ do_eqp_test 3.2 { WHERE b IS NULL OR c IS NULL OR d IS NULL ORDER BY a; } { - 0 0 0 {SEARCH TABLE t5 USING INDEX t5b (b=?)} - 0 0 0 {SEARCH TABLE t5 USING INDEX t5c (c=?)} - 0 0 0 {SEARCH TABLE t5 USING INDEX t5d (d=?)} - 0 0 0 {USE TEMP B-TREE FOR ORDER BY} + QUERY PLAN + |--MULTI-INDEX OR + | |--INDEX 1 + | | `--SEARCH t5 USING INDEX t5b (b=?) + | |--INDEX 2 + | | `--SEARCH t5 USING INDEX t5c (c=?) + | `--INDEX 3 + | `--SEARCH t5 USING INDEX t5d (d=?) + `--USE TEMP B-TREE FOR ORDER BY } #------------------------------------------------------------------------- @@ -79,14 +83,11 @@ do_execsql_test 4.1 { } do_eqp_test 4.2 { SELECT * FROM t1 WHERE likelihood(a=?, 0.014) AND b BETWEEN ? AND ?; -} { - 0 0 0 {SEARCH TABLE t1 USING INDEX i1 (a=?)} -} +} {SEARCH t1 USING INDEX i1 (a=?)} + do_eqp_test 4.3 { SELECT * FROM t1 WHERE likelihood(a=?, 0.016) AND b BETWEEN ? AND ?; -} { - 0 0 0 {SEARCH TABLE t1 USING INDEX i2 (b>? AND b<?)} -} +} {SEARCH t1 USING INDEX i2 (b>? AND b<?)} #------------------------------------------------------------------------- @@ -100,15 +101,17 @@ do_execsql_test 5.1 { do_eqp_test 5.2 { SELECT * FROM t2 ORDER BY x, y; } { - 0 0 0 {SCAN TABLE t2 USING INDEX t2i1} - 0 0 0 {USE TEMP B-TREE FOR RIGHT PART OF ORDER BY} + QUERY PLAN + |--SCAN t2 USING INDEX t2i1 + `--USE TEMP B-TREE FOR LAST TERM OF ORDER BY } do_eqp_test 5.3 { SELECT * FROM t2 WHERE x BETWEEN ? AND ? ORDER BY rowid; } { - 0 0 0 {SEARCH TABLE t2 USING INDEX t2i1 (x>? AND x<?)} - 0 0 0 {USE TEMP B-TREE FOR ORDER BY} + QUERY PLAN + |--SEARCH t2 USING INDEX t2i1 (x>? AND x<?) + `--USE TEMP B-TREE FOR ORDER BY } # where7.test, where8.test: @@ -122,9 +125,13 @@ do_execsql_test 6.1 { do_eqp_test 6.2 { SELECT a FROM t3 WHERE (b BETWEEN 2 AND 4) OR c=100 ORDER BY a } { - 0 0 0 {SEARCH TABLE t3 USING INDEX t3i1 (b>? AND b<?)} - 0 0 0 {SEARCH TABLE t3 USING INDEX t3i2 (c=?)} - 0 0 0 {USE TEMP B-TREE FOR ORDER BY} + QUERY PLAN + |--MULTI-INDEX OR + | |--INDEX 1 + | | `--SEARCH t3 USING INDEX t3i1 (b>? AND b<?) + | `--INDEX 2 + | `--SEARCH t3 USING INDEX t3i2 (c=?) + `--USE TEMP B-TREE FOR ORDER BY } #------------------------------------------------------------------------- @@ -145,9 +152,13 @@ do_eqp_test 7.2 { WHERE (b>=950 AND b<=1010) OR (b IS NULL AND c NOT NULL) ORDER BY a } { - 0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b>? AND b<?)} - 0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b=?)} - 0 0 0 {USE TEMP B-TREE FOR ORDER BY} + QUERY PLAN + |--MULTI-INDEX OR + | |--INDEX 1 + | | `--SEARCH t1 USING INDEX t1b (b>? AND b<?) + | `--INDEX 2 + | `--SEARCH t1 USING INDEX t1b (b=?) + `--USE TEMP B-TREE FOR ORDER BY } do_eqp_test 7.3 { @@ -155,15 +166,11 @@ do_eqp_test 7.3 { WHERE (+b IS NULL AND c NOT NULL AND d NOT NULL) OR (b NOT NULL AND c IS NULL AND d NOT NULL) OR (b NOT NULL AND c NOT NULL AND d IS NULL) -} { - 0 0 0 {SCAN TABLE t1} -} +} {SCAN t1} do_eqp_test 7.4 { SELECT rowid FROM t1 WHERE (+b IS NULL AND c NOT NULL) OR c IS NULL -} { - 0 0 0 {SCAN TABLE t1} -} +} {SCAN t1} #------------------------------------------------------------------------- # @@ -194,10 +201,11 @@ do_eqp_test 8.2 { AND unlikely(composer.cid=track.cid) AND unlikely(album.aid=track.aid); } { - 0 0 2 {SCAN TABLE track} - 0 1 0 {SEARCH TABLE album USING INTEGER PRIMARY KEY (rowid=?)} - 0 2 1 {SEARCH TABLE composer USING INTEGER PRIMARY KEY (rowid=?)} - 0 0 0 {USE TEMP B-TREE FOR DISTINCT} + QUERY PLAN + |--SCAN track + |--SEARCH album USING INTEGER PRIMARY KEY (rowid=?) + |--SEARCH composer USING INTEGER PRIMARY KEY (rowid=?) + `--USE TEMP B-TREE FOR DISTINCT } #------------------------------------------------------------------------- @@ -222,10 +230,10 @@ do_test 9.2 { set L [list a=? b=? c=? d=? e=? f=? g=? h=? i=? j=?] foreach {tn nTerm nRow} { 1 1 10 - 2 2 9 + 2 2 10 3 3 8 4 4 7 - 5 5 6 + 5 5 7 6 6 5 7 7 5 8 8 5 @@ -263,27 +271,19 @@ ifcapable stat4 { do_eqp_test 10.3 { SELECT rowid FROM t6 WHERE a=0 AND c=0 - } { - 0 0 0 {SEARCH TABLE t6 USING INDEX t6i2 (c=?)} - } + } {SEARCH t6 USING INDEX t6i2 (c=?)} do_eqp_test 10.4 { SELECT rowid FROM t6 WHERE a=0 AND b='xyz' AND c=0 - } { - 0 0 0 {SEARCH TABLE t6 USING INDEX t6i2 (c=?)} - } + } {SEARCH t6 USING INDEX t6i2 (c=?)} do_eqp_test 10.5 { SELECT rowid FROM t6 WHERE likelihood(a=0, 0.1) AND c=0 - } { - 0 0 0 {SEARCH TABLE t6 USING INDEX t6i1 (a=?)} - } + } {SEARCH t6 USING INDEX t6i1 (a=?)} do_eqp_test 10.6 { SELECT rowid FROM t6 WHERE likelihood(a=0, 0.1) AND b='xyz' AND c=0 - } { - 0 0 0 {SEARCH TABLE t6 USING INDEX t6i1 (a=? AND b=?)} - } + } {SEARCH t6 USING INDEX t6i1 (a=? AND b=?)} } finish_test diff --git a/test/count.test b/test/count.test index 862b62ab17..6f02074cdc 100644 --- a/test/count.test +++ b/test/count.test @@ -126,9 +126,12 @@ do_test count-2.7 { do_test count-2.8 { uses_op_count {SELECT count(*) FROM t2 WHERE a IS NOT NULL} } {0} -do_test count-2.9 { - catchsql {SELECT count(*) FROM t2 HAVING count(*)>1} -} {1 {a GROUP BY clause is required before HAVING}} +do_execsql_test count-2.9a { + SELECT count(*) FROM t2 HAVING count(*)>1; +} {} +do_execsql_test count-2.9b { + SELECT count(*) FROM t2 HAVING count(*)<10; +} {0} do_test count-2.10 { uses_op_count {SELECT count(*) FROM (SELECT 1)} } {0} @@ -196,4 +199,53 @@ do_catchsql_test count-6.1 { SELECT count(DISTINCT) FROM t6 GROUP BY x; } {1 {DISTINCT aggregates must have exactly one argument}} +# 2020-05-08. +# The count() optimization should honor the NOT INDEXED clause +# +reset_db +do_execsql_test count-7.1 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b INT, c VARCHAR(1000)); + CREATE INDEX t1b ON t1(b); + INSERT INTO t1(a,b,c) values(1,2,'count.test cases for NOT INDEXED'); + ANALYZE; + UPDATE sqlite_stat1 SET stat='1000000 10' WHERE idx='t1b'; + ANALYZE sqlite_master; +} +do_eqp_test count-7.2 { + SELECT count(1) FROM t1; +} { + QUERY PLAN + `--SCAN t1 USING COVERING INDEX t1b +} +do_eqp_test count-7.3 { + SELECT count(1) FROM t1 NOT INDEXED +} { + QUERY PLAN + `--SCAN t1 +} +do_eqp_test count-7.3 { + SELECT count(*) FROM t1; +} { + QUERY PLAN + `--SCAN t1 USING COVERING INDEX t1b +} +do_eqp_test count-7.4 { + SELECT count(*) FROM t1 NOT INDEXED +} { + QUERY PLAN + `--SCAN t1 +} + +do_execsql_test count-8.0 { + CREATE TABLE t7(a INT,b TEXT,c BLOB,d REAL); + CREATE TABLE t8(a INT,b TEXT,c BLOB,d REAL); + CREATE INDEX t8a ON t8(a); +} +do_catchsql_test count-8.1 { + SELECT * FROM t8 WHERE (a, b) IN ( + SELECT count(t8.b), count(*) FROM t7 AS ra0 ORDER BY count(*) + ) AND t8.b=0; +} {1 {misuse of aggregate: count()}} + + finish_test diff --git a/test/countofview.test b/test/countofview.test new file mode 100644 index 0000000000..cea6fa4274 --- /dev/null +++ b/test/countofview.test @@ -0,0 +1,107 @@ +# 2018-08-04 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/malloc_common.tcl +set testprefix countofview + +do_execsql_test 1.0 { + CREATE TABLE t2(c); + CREATE TABLE t3(f); + + INSERT INTO t2 VALUES(1), (2); + INSERT INTO t3 VALUES(3); +} + +do_execsql_test 1.1 { + select c from t2 union all select f from t3 limit 1 offset 1 +} {2} + +do_execsql_test 1.2 { + select count(*) from ( + select c from t2 union all select f from t3 limit 1 offset 1 + ) +} {1} + +do_execsql_test 1.3 { + select count(*) from ( + select c from t2 union all select f from t3 + ) +} {3} + +# 2019-05-15 +do_execsql_test 2.0 { + CREATE TABLE t1(x); + INSERT INTO t1 VALUES(1),(99),('abc'); + CREATE VIEW v1(x,y) AS SELECT x,1 FROM t1 UNION ALL SELECT x,2 FROM t1; + SELECT count(*) FROM v1 WHERE x<>1; +} {4} +do_execsql_test 2.1 { + SELECT count(*) FROM v1 GROUP BY y; +} {3 3} + +# 2023-03-01 dbsqlfuzz ef8623915d843b150c159166ee4548c78cc6895a +# count-of-view should not apply to CTEs. +# +ifcapable progress { + proc progress_stop args {return 1} + db progress 1000 progress_stop + do_catchsql_test 3.1 { + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c) + SELECT count(*) FROM c; + } {1 interrupted} +} + +# 2023-03-07 dbsqlfuzz 23d782160b71c3f8f535ccb2da313dfc8eb8c631 +# +do_execsql_test 4.1 { + DROP TABLE t1; + DROP TABLE t2; + DROP TABLE t3; + CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT); + INSERT INTO t1 VALUES(4,'four'); + CREATE TABLE t2(c INTEGER PRIMARY KEY, d TEXT); + CREATE VIEW t3 AS SELECT a, b FROM t1 UNION ALL SELECT c, d FROM t2; + SELECT count(*) FROM t3 ORDER BY sum(a); +} 1 + +# 2023-03-31 dbsqlfuzz 6a107e3055bd22afab31cfddabc2d9d54fcbaf69 +# Having clauses should disqualify count-of-view +# +reset_db +do_execsql_test 5.1 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT); + INSERT INTO t1 VALUES(1,'one'),(4,'four'); + CREATE TABLE t2(c INTEGER PRIMARY KEY, d TEXT); + INSERT INTO t2 VALUES(2,'two'),(5,'five'); + CREATE VIEW t3 AS SELECT a, b FROM t1 UNION ALL SELECT c, d FROM t2; + SELECT count(*) FROM t3 HAVING count(*)>0; +} 4 +do_execsql_test 5.2 { + SELECT count(*) FROM t3 HAVING count(*)>5; +} {} +do_execsql_test 5.3 { + SELECT count(*) FROM t3 HAVING max(b)>'mmm'; +} 4 +do_execsql_test 5.4 { + SELECT count(*) FROM t3 HAVING min(b)>'mmm'; +} {} +do_execsql_test 5.5 { + SELECT count(*) FROM ( + SELECT a, max(b) FROM t1 HAVING a<100 UNION ALL SELECT c, d FROM t2 + ) +} 3 + + +finish_test diff --git a/test/coveridxscan.test b/test/coveridxscan.test index 7b3c0b0be9..c87227cabc 100644 --- a/test/coveridxscan.test +++ b/test/coveridxscan.test @@ -89,5 +89,31 @@ do_test 4.3 { db eval {SELECT b FROM t1} } {2 4 8} +#------------------------------------------------------------------------- +# Test that indexes with large numbers of columns can be correctly +# identified as covering indexes. +reset_db +set L [list] +for {set i 1} {$i<120} {incr i} { + lappend L "c$i" +} +set cols [join $L ,] + +do_execsql_test 5.1.0 " + CREATE TABLE t1(a, b, c, $cols, PRIMARY KEY(a, b, c)) WITHOUT ROWID; + CREATE INDEX i1 ON t1($cols); + + CREATE TABLE t2(i INTEGER PRIMARY KEY, $cols); + CREATE INDEX i2 ON t2($cols); +" + +do_eqp_test 5.1.1 { + SELECT * FROM t1 ORDER BY c1, c2; +} {SCAN t1 USING COVERING INDEX i1} + +do_eqp_test 5.1.2 { + SELECT * FROM t2 ORDER BY c1, c2; +} {SCAN t2 USING COVERING INDEX i2} + finish_test diff --git a/test/crash.test b/test/crash.test index c1901daec6..f631636599 100644 --- a/test/crash.test +++ b/test/crash.test @@ -399,7 +399,7 @@ do_test crash-7.1 { # Change the checksum value for the master journal name. set f [open test.db-journal a] - fconfigure $f -encoding binary + fconfigure $f -translation binary seek $f [expr [file size test.db-journal] - 12] puts -nonewline $f "\00\00\00\00" close $f diff --git a/test/crash5.test b/test/crash5.test index 83d1647a84..fc078b3504 100644 --- a/test/crash5.test +++ b/test/crash5.test @@ -20,8 +20,8 @@ source $testdir/tester.tcl # Only run these tests if memory debugging is turned on. # -ifcapable !memdebug||!crashtest||!memorymanage { - puts "Skipping crash5 tests: not compiled with -DSQLITE_MEMDEBUG..." +ifcapable !crashtest||!memorymanage { + puts "Skipping crash5 tests: not compiled with -DSQLITE_ENABLE_MEMORY_MANAGEMENT..." finish_test return } @@ -29,7 +29,7 @@ ifcapable !memdebug||!crashtest||!memorymanage { db close for {set ii 0} {$ii < 10} {incr ii} { - for {set jj 50} {$jj < 100} {incr jj} { + for {set jj 1} {$jj < 100} {incr jj} { # Set up the database so that it is an auto-vacuum database # containing a single table (root page 3) with a single row. @@ -47,6 +47,20 @@ for {set ii 0} {$ii < 10} {incr ii} { do_test crash5-$ii.$jj.1 { crashsql -delay 1 -file test.db-journal -seed $ii -tclbody [join [list \ [list set iFail $jj] { + proc get_pwd {} { + if {$::tcl_platform(platform) eq "windows"} { + if {[info exists ::env(ComSpec)]} { + set comSpec $::env(ComSpec) + } else { + # NOTE: Hard-code the typical default value. + set comSpec {C:\Windows\system32\cmd.exe} + } + return [string map [list \\ /] \ + [string trim [exec -- $comSpec /c echo %CD%]]] + } else { + return [pwd] + } + } sqlite3_crashparams 0 [file join [get_pwd] test.db-journal] # Begin a transaction and evaluate a "CREATE INDEX" statement @@ -61,36 +75,39 @@ for {set ii 0} {$ii < 10} {incr ii} { # db eval BEGIN sqlite3_memdebug_fail $iFail -repeat 0 - catch {db eval { CREATE UNIQUE INDEX i1 ON t1(a); }} msg - # puts "$n $msg ac=[sqlite3_get_autocommit db]" + set rc [catch {db eval { CREATE UNIQUE INDEX i1 ON t1(a); }} msg] +# puts "$msg ac=[sqlite3_get_autocommit db] iFail=$iFail" +# puts "fail=[sqlite3_memdebug_fail -1]" - # If the transaction is still active (it may not be if the malloc() - # failure occurred in the OS layer), write to the database. Make sure - # page 4 is among those written. - # - if {![sqlite3_get_autocommit db]} { - db eval { - DELETE FROM t1; -- This will put page 4 on the free list. - INSERT INTO t1 VALUES('111111111', '2222222222', '33333333'); - INSERT INTO t1 SELECT * FROM t1; -- 2 - INSERT INTO t1 SELECT * FROM t1; -- 4 - INSERT INTO t1 SELECT * FROM t1; -- 8 - INSERT INTO t1 SELECT * FROM t1; -- 16 - INSERT INTO t1 SELECT * FROM t1; -- 32 - INSERT INTO t1 SELECT * FROM t1 WHERE rowid%2; -- 48 + if {$rc} { + # If the transaction is still active (it may not be if the malloc() + # failure occurred in the OS layer), write to the database. Make sure + # page 4 is among those written. + # + if {![sqlite3_get_autocommit db]} { + db eval { + DELETE FROM t1; -- This will put page 4 on the free list. + INSERT INTO t1 VALUES('111111111', '2222222222', '33333333'); + INSERT INTO t1 SELECT * FROM t1; -- 2 + INSERT INTO t1 SELECT * FROM t1; -- 4 + INSERT INTO t1 SELECT * FROM t1; -- 8 + INSERT INTO t1 SELECT * FROM t1; -- 16 + INSERT INTO t1 SELECT * FROM t1; -- 32 + INSERT INTO t1 SELECT * FROM t1 WHERE rowid%2; -- 48 + } } + + # If the right malloc() failed during the 'CREATE INDEX' above and + # the transaction was not rolled back, then the sqlite cache now + # has a dirty page 4 that it incorrectly believes is already safely + # in the synced part of the journal file. When + # sqlite3_release_memory() is called sqlite tries to free memory + # by writing page 4 out to the db file. If it crashes later on, + # before syncing the journal... Corruption! + # + sqlite3_crashparams 1 [file join [get_pwd] test.db-journal] + sqlite3_release_memory 8092 } - - # If the right malloc() failed during the 'CREATE INDEX' above and - # the transaction was not rolled back, then the sqlite cache now - # has a dirty page 4 that it incorrectly believes is already safely - # in the synced part of the journal file. When - # sqlite3_release_memory() is called sqlite tries to free memory - # by writing page 4 out to the db file. If it crashes later on, - # before syncing the journal... Corruption! - # - sqlite3_crashparams 1 [file join [get_pwd] test.db-journal] - sqlite3_release_memory 8092 }]] {} expr 1 } {1} diff --git a/test/crash8.test b/test/crash8.test index 930834a962..b2b01183fd 100644 --- a/test/crash8.test +++ b/test/crash8.test @@ -25,6 +25,7 @@ ifcapable !crashtest { finish_test return } +do_not_use_codec do_test crash8-1.1 { execsql { @@ -141,6 +142,7 @@ proc write_file {zFile zData} { # b) Less than 512, or # c) Greater than SQLITE_MAX_PAGE_SIZE # +if {[atomic_batch_write test.db]==0} { do_test crash8-3.1 { list [file exists test.db-joural] [file exists test.db] } {0 1} @@ -227,6 +229,7 @@ do_test crash8-3.11 { PRAGMA integrity_check } } {6 ok} +} # If a connection running in persistent-journal mode is part of a @@ -265,8 +268,12 @@ ifcapable pragma { UPDATE aux.ab SET b = randstr(1000,1000) WHERE a>=1; UPDATE ab SET b = randstr(1000,1000) WHERE a>=1; } - list [file exists test.db-journal] [file exists test2.db-journal] - } {1 1} + } {persist persist} + if {[atomic_batch_write test.db]==0} { + do_test crash8.4.1.1 { + list [file exists test.db-journal] [file exists test2.db-journal] + } {1 1} + } do_test crash8-4.2 { execsql { @@ -345,7 +352,11 @@ ifcapable pragma { # Since the following tests (crash8-5.*) rely upon being able # to copy a file while open, they will not work on Windows. # -if {$::tcl_platform(platform)=="unix"} { +# They also depend on being able to copy the journal file, which +# is not created on F2FS file-systems that support atomic +# write. So do not run these tests in that case either. +# +if {$::tcl_platform(os) ne "Windows NT" && [atomic_batch_write test.db]==0 } { for {set i 1} {$i < 10} {incr i} { catch { db close } forcedelete test.db test.db-journal diff --git a/test/createtab.test b/test/createtab.test index 01e0b6122e..5773fbf5f6 100644 --- a/test/createtab.test +++ b/test/createtab.test @@ -12,7 +12,6 @@ # focus of this file is testing that it is OK to create new tables # and indices while creating existing tables and indices. # -# $Id: createtab.test,v 1.3 2007/09/12 17:01:45 danielk1977 Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl @@ -142,5 +141,14 @@ for {set av 0} {$av<=$upperBound} {incr av} { integrity_check createtab-$av.40 } + +# 2019-03-31 Ensure that a proper error is returned for an index +# with too many columns. +# +do_test createtab-3.1 { + db eval {DROP TABLE IF EXISTS t1;} + set sql "CREATE TABLE t1(x,UNIQUE(x[string repeat ,x 100000]))" + catchsql $sql +} {1 {too many columns in index}} finish_test diff --git a/test/crypto.test b/test/crypto.test deleted file mode 100644 index 0caf7aa5e8..0000000000 --- a/test/crypto.test +++ /dev/null @@ -1,2306 +0,0 @@ -# SQLCipher -# codec.test developed by Stephen Lombardo (Zetetic LLC) -# sjlombardo at zetetic dot net -# http://zetetic.net -# -# Copyright (c) 2009, ZETETIC LLC -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions are met: -# * Redistributions of source code must retain the above copyright -# notice, this list of conditions and the following disclaimer. -# * Redistributions in binary form must reproduce the above copyright -# notice, this list of conditions and the following disclaimer in the -# documentation and/or other materials provided with the distribution. -# * Neither the name of the ZETETIC LLC nor the -# names of its contributors may be used to endorse or promote products -# derived from this software without specific prior written permission. -# -# THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY -# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -# DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY -# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -# -# This file implements regression tests for SQLite library. The -# focus of this script is testing code cipher features. -# -# NOTE: tester.tcl has overridden the definition of sqlite3 to -# automatically pass in a key value. Thus tests in this file -# should explicitly close and open db with sqlite_orig in order -# to bypass default key assignment. - - -file delete -force test.db -file delete -force test2.db -file delete -force test3.db -file delete -force test4.db - -set testdir [file dirname $argv0] -source $testdir/tester.tcl -set old_pending_byte [sqlite3_test_control_pending_byte 0x40000000] - -# If the library is not compiled with has_codec support then -# skip all tests in this file. -if {![sqlite_orig -has-codec]} { - finish_test - return -} - -proc setup {file key} { - sqlite_orig db $file - execsql "PRAGMA key=$key;" - execsql { - CREATE table t1(a,b); - INSERT INTO t1 VALUES ('test1', 'test2'); - } db - db close -} - -proc get_cipher_provider {} { - sqlite_orig db test.db - return [execsql { - PRAGMA key = 'test'; - PRAGMA cipher_provider; - }]; -} - -proc if_built_with_openssl {name cmd expected} { - if {[get_cipher_provider] == "openssl"} { - do_test $name $cmd $expected - } -} - -proc if_built_with_libtomcrypt {name cmd expected} { - if {[get_cipher_provider] == "libtomcrypt"} { - do_test $name $cmd $expected - } -} - -proc if_built_with_commoncrypto {name cmd expected} { - if {[get_cipher_provider] == "commoncrypto"} { - do_test $name $cmd $expected - } -} - -proc cmpFilesChunked {file1 file2 {chunksize 16384}} { - set f1 [open $file1]; fconfigure $f1 -translation binary - set f2 [open $file2]; fconfigure $f2 -translation binary - while {1} { - set d1 [read $f1 $chunksize] - set d2 [read $f2 $chunksize] - set diff [string compare $d1 $d2] - if {$diff != 0 || [eof $f1] || [eof $f2]} { - close $f1; close $f2 - return $diff - } - } - return 0 -} - -# The database is initially empty. -# set an hex key create some basic data -# create table and insert operations should work -# close database, open it again with the same -# hex key. verify that the table is readable -# and the data just inserted is visible -setup test.db "\"x'98483C6EB40B6C31A448C22A66DED3B5E5E8D5119CAC8327B655C8B5C4836481'\"" -do_test will-open-with-correct-raw-key { - sqlite_orig db test.db - execsql { - PRAGMA key = "x'98483C6EB40B6C31A448C22A66DED3B5E5E8D5119CAC8327B655C8B5C4836481'"; - SELECT name FROM sqlite_master WHERE type='table'; - SELECT * from t1; - } -} {t1 test1 test2} -db close -file delete -force test.db - -# set an encryption key (non-hex) and create some basic data -# create table and insert operations should work -# close database, open it again with the same -# key. verify that the table is readable -# and the data just inserted is visible -setup test.db "'testkey'" -do_test will-open-with-correct-derived-key { - - sqlite_orig db test.db - execsql { - PRAGMA key = 'testkey'; - SELECT name FROM sqlite_master WHERE type='table'; - SELECT * from t1; - } -} {t1 test1 test2} -db close -file delete -force test.db - -# open the database and try to read from it without -# providing a passphrase. verify that the -# an error is returned from the library -setup test.db "'testkey'" -do_test wont-open-without-key { - sqlite_orig db test.db - catchsql { - SELECT name FROM sqlite_master WHERE type='table'; - } -} {1 {file is encrypted or is not a database}} -db close -file delete -force test.db - -# open the database and try to set an invalid -# passphrase. verify that an error is returned -# and that data couldn't be read -setup test.db "'testkey'" -do_test wont-open-with-invalid-derived-key { - sqlite_orig db test.db - catchsql { - PRAGMA key = 'testkey2'; - SELECT name FROM sqlite_master WHERE type='table'; - } -} {1 {file is encrypted or is not a database}} -db close -file delete -force test.db - -# open the database and try to set an invalid -# hex key. verify that an error is returned -# and that data couldn't be read -setup test.db "'testkey'" -do_test wont-open-with-invalid-raw-key { - sqlite_orig db test.db - catchsql { - PRAGMA key = "x'98483C6EB40B6C31A448C22A66DED3B5E5E8D5119CAC8327B655C8B5C4836480'"; - SELECT name FROM sqlite_master WHERE type='table'; - } -} {1 {file is encrypted or is not a database}} -db close -file delete -force test.db - -# test a large number of inserts in a transaction to a memory database -do_test memory-database { - sqlite_orig db :memory: - execsql { - PRAGMA key = 'testkey3'; - BEGIN; - CREATE TABLE t2(a,b); - } - for {set i 1} {$i<=25000} {incr i} { - set r [expr {int(rand()*500000)}] - execsql "INSERT INTO t2 VALUES($i,$r);" - } - execsql { - COMMIT; - SELECT count(*) FROM t2; - DELETE FROM t2; - SELECT count(*) FROM t2; - } -} {25000 0} -db close - -# test a large number of inserts in a transaction for multiple pages -do_test multi-page-database { - sqlite_orig db test.db - execsql { - PRAGMA key = 'testkey'; - CREATE TABLE t2(a,b); - BEGIN; - } - for {set i 1} {$i<=25000} {incr i} { - set r [expr {int(rand()*500000)}] - execsql "INSERT INTO t2 VALUES($i,$r);" - } - execsql { - COMMIT; - SELECT count(*) FROM t2; - } -} {25000} -db close -file delete -force test.db - -# test a rekey operation as the first op on a database -# then test that now the new key opens the database -# now close database re-open with new key -setup test.db "'testkey'" -do_test rekey-as-first-operation { - sqlite_orig db test.db - execsql { - PRAGMA key = 'testkey'; - PRAGMA rekey = 'testkeynew'; - } - db close - - sqlite_orig db test.db - execsql { - PRAGMA key = 'testkeynew'; - SELECT name FROM sqlite_master WHERE type='table'; - } -} {t1} -db close -file delete -force test.db - -# create a new database, insert some data -# then rekey it with the same password -do_test rekey-same-passkey { - sqlite_orig db test.db - - execsql { - PRAGMA key = 'test123'; - CREATE TABLE t1(a,b); - BEGIN; - } - - for {set i 1} {$i<=1000} {incr i} { - set r [expr {int(rand()*500000)}] - execsql "INSERT INTO t1 VALUES($i,'value $r');" - } - - execsql { - COMMIT; - SELECT count(*) FROM t1; - PRAGMA rekey = 'test123'; - SELECT count(*) FROM t1; - } -} {1000 1000} -db close -file delete -force test.db - -# create a new database, insert some data -# then rekey it. Make sure it is immediately -# readable. Then close it and make sure it can be -# read back -do_test rekey-and-query-1 { - sqlite_orig db test.db - - execsql { - PRAGMA key = 'test123'; - CREATE TABLE t1(a,b); - BEGIN; - } - - for {set i 1} {$i<=1000} {incr i} { - set r [expr {int(rand()*500000)}] - execsql "INSERT INTO t1 VALUES($i,'value $r');" - } - - execsql { - COMMIT; - SELECT count(*) FROM t1; - PRAGMA rekey = 'test321'; - SELECT count(*) FROM t1; - } -} {1000 1000} - -db close - -do_test rekey-and-query-2 { - sqlite_orig db test.db - execsql { - PRAGMA key = 'test321'; - SELECT count(*) FROM t1; - } -} {1000} -db close -file delete -force test.db - -# create a new database, insert some data -# delete about 50% of the data -# write some new data -# delete another 50% -# then rekey it. Make sure it is immediately -# readable. Then close it and make sure it can be -# read back. This test will ensure that Secure Delete -# is enabled and all pages are being written and are not -# being optimized out by sqlite3PagerDontWrite -do_test rekey-delete-and-query-1 { - sqlite_orig db test.db - - execsql { - PRAGMA key = 'test123'; - CREATE TABLE t1(a,b); - CREATE INDEX ta_a ON t1(a); - BEGIN; - } - - for {set i 1} {$i<1000} {incr i} { - set r [expr {int(rand()*32767)}] - set r1 [expr {int(rand()*32767)}] - execsql "INSERT INTO t1 VALUES($r,$r1);" - } - set r [expr {int(rand()*32767)}] - set r1 [expr {int(rand()*32767)}] - execsql "UPDATE t1 SET b = $r WHERE a < $r1;" - - set r [expr {int(rand()*32767)}] - - execsql "DELETE FROM t1 WHERE a < $r;" - - execsql { - COMMIT; - SELECT (count(*) > 0) FROM t1; - } -} {1} -db close - -do_test rekey-delete-and-query-2 { - sqlite_orig db test.db - execsql { - PRAGMA key = 'test123'; - PRAGMA rekey = 'test321'; - SELECT count(*) > 1 FROM t1; - PRAGMA integrity_check; - } -} {1 ok} -db close - -do_test rekey-delete-and-query-3 { - sqlite_orig db test.db - execsql { - PRAGMA key = 'test321'; - SELECT count(*) > 1 FROM t1; - } -} {1} -db close -file delete -force test.db - - -# same as previous test, but use WAL -do_test rekey-delete-and-query-wal-1 { - sqlite_orig db test.db - - execsql { - PRAGMA key = 'test123'; - PRAGMA journal_mode = WAL; - CREATE TABLE t1(a,b); - CREATE INDEX ta_a ON t1(a); - BEGIN; - } - - for {set i 1} {$i<1000} {incr i} { - set r [expr {int(rand()*32767)}] - set r1 [expr {int(rand()*32767)}] - execsql "INSERT INTO t1 VALUES($r,$r1);" - } - set r [expr {int(rand()*32767)}] - set r1 [expr {int(rand()*32767)}] - execsql "UPDATE t1 SET b = $r WHERE a < $r1;" - - set r [expr {int(rand()*32767)}] - - execsql "DELETE FROM t1 WHERE a < $r;" - - execsql { - COMMIT; - SELECT (count(*) > 0) FROM t1; - } -} {1} -db close - -do_test rekey-delete-and-query-wal-2 { - sqlite_orig db test.db - execsql { - PRAGMA key = 'test123'; - PRAGMA journal_mode = WAL; - PRAGMA rekey = 'test321'; - SELECT count(*) > 1 FROM t1; - PRAGMA integrity_check; - } -} {wal 1 ok} -db close - -do_test rekey-delete-and-query-wal-3 { - sqlite_orig db test.db - execsql { - PRAGMA key = 'test321'; - PRAGMA journal_mode = WAL; - SELECT count(*) > 1 FROM t1; - } -} {wal 1} -db close -file delete -force test.db - -# attach an encrypted database -# without specifying key, verify it fails -# even if the source passwords are the same -# because the kdf salts are different -setup test.db "'testkey'" -do_test attach-database-with-default-key { - sqlite_orig db2 test2.db - execsql { - PRAGMA key = 'testkey'; - PRAGMA cipher_add_random = "x'deadbaad'"; - CREATE TABLE t2(a,b); - INSERT INTO t2 VALUES ('test1', 'test2'); - } db2 - - catchsql { - ATTACH 'test.db' AS db; - } db2 - -} {1 {file is encrypted or is not a database}} -db2 close -file delete -force test.db -file delete -force test2.db - -# attach an encrypted database -# without specifying key, verify it attaches -# correctly when PRAGMA cipher_store_pass = 1 -# is set. -do_test attach-database-with-default-key-using-cipher-store-pass { - - sqlite_orig db1 test.db - execsql { - PRAGMA key = 'testkey'; - CREATE TABLE t1(a,b); - INSERT INTO t1(a,b) VALUES('foo', 'bar'); - } db1 - db1 close - - sqlite_orig db2 test2.db - execsql { - PRAGMA key = 'testkey'; - CREATE TABLE t2(a,b); - INSERT INTO t2 VALUES ('test1', 'test2'); - } db2 - db2 close - - sqlite_orig db1 test.db - execsql { - PRAGMA key = 'testkey'; - PRAGMA cipher_store_pass = 1; - ATTACH DATABASE 'test2.db' as db2; - SELECT sqlcipher_export('db2'); - DETACH DATABASE db2; - } db1 - db1 close - - sqlite_orig db2 test2.db - execsql { - PRAGMA key = 'testkey'; - SELECT * FROM t1; - } db2 - -} {foo bar} -db2 close -file delete -force test.db -file delete -force test2.db - -# attach an encrypted database -# where both database have the same -# key explicitly -setup test.db "'testkey'" -do_test attach-database-with-same-key { - sqlite_orig db2 test2.db - - execsql { - PRAGMA key = 'testkey'; - CREATE TABLE t2(a,b); - INSERT INTO t2 VALUES ('test1', 'test2'); - } db2 - - execsql { - SELECT count(*) FROM t2; - ATTACH 'test.db' AS db KEY 'testkey'; - SELECT count(*) FROM db.t1; - } db2 - -} {1 1} -db2 close -file delete -force test.db -file delete -force test2.db - -# attach an encrypted database -# where databases have different keys -setup test.db "'testkey'" -do_test attach-database-with-different-keys { - sqlite_orig db2 test2.db - - execsql { - PRAGMA key = 'testkey2'; - CREATE TABLE t2(a,b); - INSERT INTO t2 VALUES ('test1', 'test2'); - } db2 - - execsql { - ATTACH 'test.db' AS db KEY 'testkey'; - SELECT count(*) FROM db.t1; - SELECT count(*) FROM t2; - } db2 - -} {1 1} -db2 close -file delete -force test.db -file delete -force test2.db - -# test locking across multiple handles -setup test.db "'testkey'" -do_test locking-across-multiple-handles-start { - sqlite_orig db test.db - - execsql { - PRAGMA key = 'testkey'; - BEGIN EXCLUSIVE; - INSERT INTO t1 VALUES(1,2); - } - - sqlite_orig dba test.db - catchsql { - PRAGMA key = 'testkey'; - SELECT count(*) FROM t1; - } dba - - } {1 {database is locked}} - -do_test locking-accross-multiple-handles-finish { - execsql { - COMMIT; - } - - execsql { - SELECT count(*) FROM t1; - } dba -} {2} -db close -dba close -file delete -force test.db - -# alter schema -setup test.db "'testkey'" -do_test alter-schema { - sqlite_orig db test.db - execsql { - PRAGMA key = 'testkey'; - ALTER TABLE t1 ADD COLUMN c; - INSERT INTO t1 VALUES (1,2,3); - INSERT INTO t1 VALUES (1,2,4); - CREATE TABLE t1a (a); - INSERT INTO t1a VALUES ('teststring'); - } - db close - - sqlite_orig db test.db - execsql { - PRAGMA key = 'testkey'; - SELECT count(*) FROM t1 WHERE a IS NOT NULL; - SELECT count(*) FROM t1 WHERE c IS NOT NULL; - SELECT * FROM t1a; - } - -} {3 2 teststring} -db close -file delete -force test.db - -# test alterations of KDF iterations and ciphers -# rekey then add -setup test.db "'testkey'" -do_test non-standard-kdf-and-ciphers { - sqlite_orig db test.db - execsql { - PRAGMA key = 'testkey'; - PRAGMA rekey_kdf_iter = 1000; - PRAGMA rekey_cipher = 'aes-256-cfb'; - PRAGMA rekey = 'testkey2'; - INSERT INTO t1 VALUES (1,2); - } - db close - - sqlite_orig db test.db - execsql { - PRAGMA key = 'testkey2'; - PRAGMA kdf_iter = 1000; - PRAGMA cipher = 'aes-256-cfb'; - SELECT count(*) FROM t1; - } - -} {2} -db close -file delete -force test.db - -# test alterations of CIPHER from CBC Mode requiring -# IV to ECB mode that does not -setup test.db "'testkey'" -do_test rekey-from-cbc-to-ecb-no-iv { - sqlite_orig db test.db - execsql { - PRAGMA key = 'testkey'; - BEGIN; - } - - for {set i 1} {$i<=1000} {incr i} { - set r [expr {int(rand()*500000)}] - execsql "INSERT INTO t1 VALUES($i,$r);" - } - - execsql { - COMMIT; - PRAGMA rekey_kdf_iter = 1000; - PRAGMA rekey_cipher = 'aes-128-ecb'; - PRAGMA rekey = 'testkey'; - } - db close - - sqlite_orig db test.db - execsql { - PRAGMA key = 'testkey'; - PRAGMA kdf_iter = 1000; - PRAGMA cipher = 'aes-128-ecb'; - SELECT count(*) FROM t1; - } - -} {1001} -db close -file delete -force test.db - -# test alterations of CIPHER from ECB Mode (no IV) to CBC Mode -do_test rekey-from-ecb-to-cbc-with-iv { - sqlite_orig db test.db - execsql { - PRAGMA key = 'testkey'; - PRAGMA cipher = 'aes-256-ecb'; - CREATE table t1(a,b); - BEGIN; - } - - for {set i 1} {$i<=1000} {incr i} { - set r [expr {int(rand()*500000)}] - execsql "INSERT INTO t1 VALUES($i,$r);" - } - - execsql { - COMMIT; - PRAGMA rekey_cipher = 'aes-256-cbc'; - PRAGMA rekey = 'testkey'; - } - db close - - sqlite_orig db test.db - execsql { - PRAGMA key = 'testkey'; - SELECT count(*) FROM t1; - } - -} {1000} -db close -file delete -force test.db - -# create an unencrypted database, attach a new encrypted volume -# copy data between, verify the encypted database is good afterwards -do_test unencrypted-attach { - sqlite_orig db test.db - - execsql { - CREATE TABLE t1(a,b); - BEGIN; - } - - for {set i 1} {$i<=1000} {incr i} { - set r [expr {int(rand()*500000)}] - execsql "INSERT INTO t1 VALUES($i,$r);" - } - - execsql { - COMMIT; - ATTACH DATABASE 'test2.db' AS db2 KEY 'testkey'; - CREATE TABLE db2.t1(a,b); - INSERT INTO db2.t1 SELECT * FROM t1; - DETACH DATABASE db2; - } - - sqlite_orig db2 test2.db - execsql { - PRAGMA key='testkey'; - SELECT count(*) FROM t1; - } db2 -} {1000} -db2 close -file delete -force test.db -file delete -force test2.db - -# create an unencrypted database, attach a new encrypted volume -# using a raw key copy data between, verify the encypted -# database is good afterwards -do_test unencrypted-attach-raw-key { - sqlite_orig db test.db - - execsql { - CREATE TABLE t1(a,b); - BEGIN; - } - - for {set i 1} {$i<=1000} {incr i} { - set r [expr {int(rand()*500000)}] - execsql "INSERT INTO t1 VALUES($i,$r);" - } - - execsql { - COMMIT; - ATTACH DATABASE 'test2.db' AS db2 KEY "x'10483C6EB40B6C31A448C22A66DED3B5E5E8D5119CAC8327B655C8B5C4836481'"; - CREATE TABLE db2.t1(a,b); - INSERT INTO db2.t1 SELECT * FROM t1; - DETACH DATABASE db2; - } - - sqlite_orig db2 test2.db - execsql { - PRAGMA key="x'10483C6EB40B6C31A448C22A66DED3B5E5E8D5119CAC8327B655C8B5C4836481'"; - SELECT count(*) FROM t1; - } db2 -} {1000} -db2 close -file delete -force test.db -file delete -force test2.db - -# create an encrypted database, attach an default-key encrypted volume -# copy data between, verify the second database -do_test encrypted-attach-default-key { - sqlite_orig db test.db - - execsql { - PRAGMA key='testkey'; - CREATE TABLE t1(a,b); - BEGIN; - } - - for {set i 1} {$i<=1000} {incr i} { - set r [expr {int(rand()*500000)}] - execsql "INSERT INTO t1 VALUES($i,$r);" - } - - execsql { - COMMIT; - ATTACH DATABASE 'test2.db' AS test; - CREATE TABLE test.t1(a,b); - INSERT INTO test.t1 SELECT * FROM t1; - DETACH DATABASE test; - } - - sqlite_orig db2 test2.db - - execsql { - PRAGMA key='testkey'; - SELECT count(*) FROM t1; - } db2 -} {1000} -db close -db2 close -file delete -force test.db -file delete -force test2.db - -# create an encrypted database, attach an unencrypted volume -# copy data between, verify the unencypted database is good afterwards -do_test encrypted-attach-unencrypted { - sqlite_orig db test.db - - execsql { - CREATE TABLE t1(a,b); - } - - sqlite_orig db2 test2.db - execsql { - PRAGMA key = 'testkey'; - CREATE TABLE t1(a,b); - BEGIN; - } db2 - - for {set i 1} {$i<=1000} {incr i} { - set r [expr {int(rand()*500000)}] - execsql "INSERT INTO t1 VALUES($i,$r);" db2 - } - - execsql { - COMMIT; - ATTACH DATABASE 'test.db' AS test KEY ''; - INSERT INTO test.t1 SELECT * FROM t1; - DETACH DATABASE test; - } db2 - - execsql { - SELECT count(*) FROM t1; - } -} {1000} -db close -db2 close -file delete -force test.db -file delete -force test2.db - -# create an unencrypted database, attach an unencrypted volume -# copy data between, verify the unencypted database is good afterwards -do_test unencrypted-attach-unencrypted { - sqlite_orig db test.db - - execsql { - CREATE TABLE t1(a,b); - } - - sqlite_orig db2 test2.db - execsql { - CREATE TABLE t1(a,b); - BEGIN; - } db2 - - for {set i 1} {$i<=1000} {incr i} { - set r [expr {int(rand()*500000)}] - execsql "INSERT INTO t1 VALUES($i,$r);" db2 - } - - execsql { - COMMIT; - ATTACH DATABASE 'test.db' AS test; - INSERT INTO test.t1 SELECT * FROM t1; - DETACH DATABASE test; - } db2 - - execsql { - SELECT count(*) FROM t1; - } -} {1000} -db close -db2 close -file delete -force test.db -file delete -force test2.db - -# 1. create a database with a custom page size, -# 2. create table and insert operations should work -# 3. close database, open it again with the same -# key and page size -# 4. verify that the table is readable -# and the data just inserted is visible -do_test custom-pagesize { - sqlite_orig db test.db - - execsql { - PRAGMA key = 'testkey'; - PRAGMA cipher_page_size = 4096; - CREATE table t1(a,b); - BEGIN; - } - - for {set i 1} {$i<=1000} {incr i} { - set r [expr {int(rand()*500000)}] - execsql "INSERT INTO t1 VALUES($i,'value $r');" - } - - execsql { - COMMIT; - } - - db close - sqlite_orig db test.db - - execsql { - PRAGMA key = 'testkey'; - PRAGMA cipher_page_size = 4096; - SELECT count(*) FROM t1; - } - -} {1000} -db close - -# open the database with the default page size -## and verfiy that it is not readable -do_test custom-pagesize-must-match { - sqlite_orig db test.db - catchsql { - PRAGMA key = 'testkey'; - SELECT name FROM sqlite_master WHERE type='table'; - } -} {1 {file is encrypted or is not a database}} -db close -file delete -force test.db - -# 1. create a database and insert a bunch of data, close the database -# 2. seek to the middle of a database page and write some junk -# 3. Open the database and verify that the database is no longer readable -do_test hmac-tamper-resistence { - sqlite_orig db test.db - - execsql { - PRAGMA key = 'testkey'; - CREATE table t1(a,b); - BEGIN; - } - - for {set i 1} {$i<=1000} {incr i} { - set r [expr {int(rand()*500000)}] - execsql "INSERT INTO t1 VALUES($i,'value $r');" - } - - execsql { - COMMIT; - } - - db close - - # write some junk into the hmac segment, leaving - # the page data valid but with an invalid signature - hexio_write test.db 1000 0000 - - sqlite_orig db test.db - - catchsql { - PRAGMA key = 'testkey'; - SELECT count(*) FROM t1; - } - -} {1 {file is encrypted or is not a database}} -db close -file delete -force test.db - -# 1. create a database and insert a bunch of data, close the database -# 2. seek to the middle of a database page and write some junk -# 3. Open the database and verify that the database is still readable -do_test nohmac-not-tamper-resistent { - sqlite_orig db test.db - - execsql { - PRAGMA key = 'testkey'; - PRAGMA cipher_use_hmac = OFF; - PRAGMA cipher_page_size = 1024; - CREATE table t1(a,b); - BEGIN; - } - - for {set i 1} {$i<=1000} {incr i} { - set r [expr {int(rand()*500000)}] - execsql "INSERT INTO t1 VALUES($i,'value $r');" - } - - execsql { - COMMIT; - } - - db close - - # write some junk into the middle of the page - hexio_write test.db 2560 00 - - sqlite_orig db test.db - - execsql { - PRAGMA key = 'testkey'; - PRAGMA cipher_use_hmac = OFF; - PRAGMA cipher_page_size = 1024; - SELECT count(*) FROM t1; - } - -} {1000} -db close -file delete -force test.db - -# open a 1.1.8 database using the new code, HMAC disabled -do_test open-1.1.8-database { - file copy -force sqlcipher-1.1.8-testkey.db test.db - sqlite_orig db test.db - execsql { - PRAGMA key = 'testkey'; - PRAGMA cipher_use_hmac = off; - PRAGMA kdf_iter = 4000; - SELECT count(*) FROM t1; - SELECT distinct * FROM t1; - } -} {75709 1 1 one one 1 2 one two 1 2} -db close -file delete -force test.db - -# open a 1.1.8 database without hmac, then copy the data -do_test attach-and-copy-1.1.8 { - sqlite_orig db sqlcipher-1.1.8-testkey.db - - execsql { - PRAGMA key = 'testkey'; - PRAGMA cipher_use_hmac = OFF; - PRAGMA kdf_iter = 4000; - ATTACH DATABASE 'test.db' AS db2 KEY 'testkey-hmac'; - CREATE TABLE db2.t1(a,b); - INSERT INTO db2.t1 SELECT * FROM main.t1; - DETACH DATABASE db2; - } - db close - - sqlite_orig db test.db - execsql { - PRAGMA key = 'testkey-hmac'; - SELECT count(*) FROM t1; - SELECT distinct * FROM t1; - } -} {75709 1 1 one one 1 2 one two 1 2} -db close -file delete -force test.db - -# open a standard database, then attach a new -# database with completely different options. -# copy data between them, and verify that the -# new database can be opened with the proper data -do_test attached-database-pragmas { - sqlite_orig db test.db - - execsql { - PRAGMA key = 'testkey'; - CREATE TABLE t1(a,b); - BEGIN; - } - - for {set i 1} {$i<=1000} {incr i} { - set r [expr {int(rand()*500000)}] - execsql "INSERT INTO t1 VALUES($i,'value $r');" - } - - execsql { - COMMIT; - ATTACH DATABASE 'test2.db' AS db2 KEY 'testkey2'; - PRAGMA db2.cipher_page_size = 4096; - PRAGMA db2.cipher = 'aes-128-cbc'; - PRAGMA db2.kdf_iter = 1000; - PRAGMA db2.cipher_use_hmac = OFF; - CREATE TABLE db2.t1(a,b); - INSERT INTO db2.t1 SELECT * FROM main.t1; - DETACH DATABASE db2; - } - db close - - sqlite_orig db test2.db - execsql { - PRAGMA key = 'testkey2'; - PRAGMA cipher_page_size = 4096; - PRAGMA cipher = 'aes-128-cbc'; - PRAGMA kdf_iter = 1000; - PRAGMA cipher_use_hmac = OFF; - SELECT count(*) FROM t1; - } -} {1000} -db close -file delete -force test.db -file delete -force test2.db - -# use the sqlcipher_export function -# on a non-existent database. Verify -# the error gets through. -do_test export-error { - sqlite_orig db test.db - - catchsql { - PRAGMA key = 'testkey'; - CREATE TABLE t1(a,b); - SELECT sqlcipher_export('nodb'); - } -} {1 {unknown database nodb}} -db close -file delete -force test.db - -# use the sqlcipher_export function -# to copy a complicated database. -# tests autoincrement fields, -# indexes, views, and triggers, -# tables and virtual tables -do_test export-database { - sqlite_orig db test.db - - execsql { - PRAGMA key = 'testkey'; - CREATE TABLE t1(a INTEGER PRIMARY KEY AUTOINCREMENT, b, c); - CREATE UNIQUE INDEX b_idx ON t1(b); - CREATE INDEX c_idx ON t1(c); - - CREATE TABLE t2(b,c); - CREATE TRIGGER t2_after_insert AFTER INSERT ON t2 - BEGIN - INSERT INTO t1(b,c) VALUES (new.b, new.c); - END; - - CREATE VIEW v1 AS - SELECT c FROM t1; - - CREATE VIRTUAL TABLE fts USING fts3(a,b); - - BEGIN; - -- start with one known value - INSERT INTO t2 VALUES(1000000,'value 1000000'); - } - - for {set i 1} {$i<=999} {incr i} { - set r [expr {int(rand()*500000)}] - execsql "INSERT INTO t2 VALUES($i,'value $r');" - } - - execsql { - INSERT INTO fts SELECT b,c FROM t1; - COMMIT; - - ATTACH DATABASE 'test2.db' AS db2 KEY 'testkey2'; - PRAGMA db2.cipher_page_size = 4096; - - SELECT sqlcipher_export('db2'); - - DETACH DATABASE db2; - } - db close - - sqlite_orig db test2.db - execsql { - PRAGMA key = 'testkey2'; - PRAGMA cipher_page_size = 4096; - SELECT count(*) FROM t1; - SELECT count(*) FROM v1; - SELECT count(*) FROM sqlite_sequence; - SELECT seq FROM sqlite_sequence WHERE name = 't1'; - INSERT INTO t2 VALUES(10001, 'value 938383'); - SELECT count(*) FROM t1; -- verify the trigger worked - SELECT seq FROM sqlite_sequence WHERE name = 't1'; -- verify that autoincrement worked - SELECT a FROM fts WHERE b MATCH '1000000'; - } -} {1000 1000 1 1000 1001 1001 1000000} -db close -file delete -force test.db -file delete -force test2.db - -# 1. create a database with WAL journal mode -# 2. create table and insert operations should work -# 3. close database, open it again -# 4. verify that the table is present, readable, and that -# the journal mode is WAL -do_test journal-mode-wal { - sqlite_orig db test.db - - execsql { - PRAGMA key = 'testkey'; - PRAGMA journal_mode = WAL; - CREATE table t1(a,b); - BEGIN; - } - - for {set i 1} {$i<=1000} {incr i} { - set r [expr {int(rand()*500000)}] - execsql "INSERT INTO t1 VALUES($i,'value $r');" - } - - execsql { - COMMIT; - } - - db close - sqlite_orig db test.db - - execsql { - PRAGMA key = 'testkey'; - SELECT count(*) FROM t1; - PRAGMA journal_mode; - } - -} {1000 wal} -db close -file delete -force test.db - -# Test rekey as first operation on an empty database. should be a no-op -do_test rekey-as-first-op { - sqlite_orig db test.db - - execsql { - PRAGMA rekey = 'testkey'; - CREATE table t1(a,b); - BEGIN; - } - - for {set i 1} {$i<=100} {incr i} { - set r [expr {int(rand()*500000)}] - execsql "INSERT INTO t1 VALUES($i,'value $r');" - } - - execsql { - COMMIT; - } - - db close - sqlite_orig db test.db - - execsql { - PRAGMA rekey = 'testkey'; - SELECT count(*) FROM t1; - } - -} {100} -db close -file delete -force test.db - -# Test rekey as first operation follwed by key -do_test rekey-then-key-as-first-ops { - sqlite_orig db test.db - - execsql { - PRAGMA rekey = '1234'; - PRAGMA key = 'testkey'; - CREATE table t1(a,b); - BEGIN; - } - - for {set i 1} {$i<=100} {incr i} { - set r [expr {int(rand()*500000)}] - execsql "INSERT INTO t1 VALUES($i,'value $r');" - } - - execsql { - COMMIT; - } - - db close - sqlite_orig db test.db - - execsql { - PRAGMA rekey = '4321'; - PRAGMA key = 'testkey'; - SELECT count(*) FROM t1; - } - -} {100} -db close -file delete -force test.db - -setup test.db "'testkey'" -do_test multiple-key-calls-safe-1 { - sqlite_orig db test.db - execsql { - PRAGMA key = 'testkey'; - PRAGMA cache_size = 0; - SELECT name FROM sqlite_master WHERE type='table'; - } -} {t1} - -do_test multiple-key-calls-safe-2 { - catchsql { - PRAGMA key = 'wrong key'; - SELECT name FROM sqlite_master WHERE type='table'; - } -} {1 {file is encrypted or is not a database}} - -do_test multiple-key-calls-safe-3 { - execsql { - PRAGMA key = 'testkey'; - SELECT name FROM sqlite_master WHERE type='table'; - } -} {t1} - -db close -file delete -force test.db - -# 1. create a database with a custom hmac kdf iteration count, -# 2. create table and insert operations should work -# 3. close database, open it again with the same -# key and hmac kdf iteration count -# 4. verify that the table is readable -# and the data just inserted is visible -do_test custom-hmac-kdf-iter { - sqlite_orig db test.db - - execsql { - PRAGMA key = 'testkey'; - PRAGMA fast_kdf_iter = 10; - CREATE table t1(a,b); - BEGIN; - } - - for {set i 1} {$i<=1000} {incr i} { - set r [expr {int(rand()*500000)}] - execsql "INSERT INTO t1 VALUES($i,'value $r');" - } - - execsql { - COMMIT; - } - - db close - sqlite_orig db test.db - - execsql { - PRAGMA key = 'testkey'; - PRAGMA fast_kdf_iter = 10; - SELECT count(*) FROM t1; - } - -} {1000} -db close - -# open the database with the default hmac -# kdf iteration count -# to verify that it is not readable -do_test custom-hmac-kdf-iter-must-match { - sqlite_orig db test.db - catchsql { - PRAGMA key = 'testkey'; - SELECT name FROM sqlite_master WHERE type='table'; - } -} {1 {file is encrypted or is not a database}} -db close -file delete -force test.db - -# open the database and turn on auto_vacuum -# then insert a bunch of data, delete it -# and verify that the file has become smaller -# but can still be opened with the proper -# key -do_test auto-vacuum { - sqlite_orig db test.db - set rc {} - - execsql { - PRAGMA key = 'testkey'; - PRAGMA auto_vacuum=FULL; - CREATE table t1(a,b); - BEGIN; - } - - for {set i 1} {$i<=10000} {incr i} { - set r [expr {int(rand()*500000)}] - execsql "INSERT INTO t1 VALUES($i,'value $r');" - } - - lappend rc [execsql { - COMMIT; - SELECT count(*) FROM t1; - }] - - # grab current size of file - set sz [file size test.db] - - # delete some records, and verify - # autovacuum removes them - execsql { - DELETE FROM t1 WHERE rowid > 5000; - } - - db close - - # grab new file size, post - # autovacuum - set sz2 [file size test.db] - - # verify that the new size is - # smaller than the old size - if {$sz > $sz2} { lappend rc true } - - sqlite_orig db test.db - - lappend rc [execsql { - PRAGMA key = 'testkey'; - SELECT count(*) FROM t1; - }] - -} {10000 true 5000} -db close -file delete -force test.db - -# open the database then insert a bunch of data. -# then delete it and run a manual vacuum -# verify that the file has become smaller -# but can still be opened with the proper -# key -do_test vacuum { - sqlite_orig db test.db - set rc {} - - execsql { - PRAGMA key = 'testkey'; - CREATE table t1(a,b); - BEGIN; - } - - for {set i 1} {$i<=10000} {incr i} { - set r [expr {int(rand()*500000)}] - execsql "INSERT INTO t1 VALUES($i,'value $r');" - } - - lappend rc [execsql { - COMMIT; - SELECT count(*) FROM t1; - }] - - # grab current size of file - set sz [file size test.db] - - execsql { - DELETE FROM t1 WHERE rowid > 5000; - VACUUM; - } - db close - - # grab new file size, post - # autovacuum - set sz2 [file size test.db] - - # verify that the new size is - # smaller than the old size - if {$sz > $sz2} { lappend rc true } - - sqlite_orig db test.db - lappend rc [execsql { - PRAGMA key = 'testkey'; - SELECT count(*) FROM t1; - }] - -} {10000 true 5000} -db close -file delete -force test.db - -# test kdf_iter and other pragmas -# before a key is set. Verify that they -# are no-ops -do_test cipher-options-before-keys { - sqlite_orig db test.db - - execsql { - PRAGMA kdf_iter = 1000; - PRAGMA cipher_page_size = 4096; - PRAGMA cipher = 'aes-128-cbc'; - PRAGMA cipher_use_hmac = OFF; - PRAGMA key = 'testkey'; - CREATE table t1(a,b); - INSERT INTO t1 VALUES(1,2); - } - db close - - sqlite_orig db test.db - - execsql { - PRAGMA key = 'testkey'; - SELECT count(*) FROM t1; - } - -} {1} -db close -file delete -force test.db - -# open a 1.1.8 database (no HMAC, 4K iter), then -# try to open another 1.1.8 database. The -# attached database should have the same hmac -# setting as the original -do_test default-hmac-kdf-attach { - file copy -force sqlcipher-1.1.8-testkey.db test.db - sqlite_orig db test.db - execsql { - PRAGMA cipher_default_use_hmac = OFF; - PRAGMA cipher_default_kdf_iter = 4000; - PRAGMA key = 'testkey'; - SELECT count(*) FROM t1; - ATTACH 'sqlcipher-1.1.8-testkey.db' AS db2 KEY 'testkey'; - SELECT count(*) from db2.t1; - PRAGMA cipher_default_use_hmac = ON; - PRAGMA cipher_default_kdf_iter = 64000; - } -} {75709 75709} -db close -file delete -force test.db - -# open a 2.0 database (with HMAC), then -# try to a 1.1.8 database. this should -# fail because the hmac setting for the -# attached database is not compatible -do_test attach-1.1.8-database-from-2.0-fails { - sqlite_orig db test.db - catchsql { - PRAGMA key = 'testkey'; - CREATE table t1(a,b); - ATTACH 'sqlcipher-1.1.8-testkey.db' AS db2 KEY 'testkey'; - } -} {1 {file is encrypted or is not a database}} -db close -file delete -force test.db - -# open a 2.0 database (with HMAC, 4k iter), then -# set the default hmac setting to OFF. -# try to a 1.1.8 database. this should -# succeed now that hmac is off by default -# before the attach -do_test change-default-hmac-kdf-attach { - sqlite_orig db test.db - execsql { - PRAGMA key = 'testkey'; - CREATE table t1(a,b); - INSERT INTO t1(a,b) VALUES (1,2); - } - db close - sqlite_orig db test.db - execsql { - PRAGMA key = 'testkey'; - SELECT count(*) FROM t1; - PRAGMA cipher_default_use_hmac = OFF; - PRAGMA cipher_default_kdf_iter = 4000; - ATTACH 'sqlcipher-1.1.8-testkey.db' AS db2 KEY 'testkey'; - SELECT count(*) from db2.t1; - PRAGMA cipher_default_use_hmac = ON; - PRAGMA cipher_default_kdf_iter = 64000; - } -} {1 75709} -db close -file delete -force test.db - -# verify the pragma cipher_version -# returns the currently configured -# sqlcipher version -do_test verify-pragma-cipher-version { - sqlite_orig db test.db - execsql { - PRAGMA cipher_version; - } -} {3.3.1} -db close -file delete -force test.db - -# create a new database, insert some data -# and delete some data with -# auto_vacuum on -do_test auto-vacuum-full { - sqlite_orig db test.db - - execsql { - PRAGMA key = 'test123'; - PRAGMA auto_vacuum = FULL; - CREATE TABLE t1(a,b); - BEGIN; - } - - for {set i 1} {$i<10000} {incr i} { - set r [expr {int(rand()*32767)}] - set r1 [expr {int(rand()*32767)}] - execsql "INSERT INTO t1 VALUES($r,$r1);" - } - set r [expr {int(rand()*32767)}] - execsql "DELETE FROM t1 WHERE a < $r;" - - execsql { - COMMIT; - PRAGMA integrity_check; - PRAGMA freelist_count; - SELECT (count(*) > 0) FROM t1; - } -} {ok 0 1} -db close -file delete -force test.db - -# create a new database, insert some data -# and delete some data with -# auto_vacuum incremental -do_test auto-vacuum-incremental { - sqlite_orig db test.db - - execsql { - PRAGMA key = 'test123'; - PRAGMA auto_vacuum = INCREMENTAL; - CREATE TABLE t1(a,b); - BEGIN; - } - - for {set i 1} {$i<10000} {incr i} { - set r [expr {int(rand()*32767)}] - set r1 [expr {int(rand()*32767)}] - execsql "INSERT INTO t1 VALUES($r,$r1);" - } - set r [expr {int(rand()*32767)}] - execsql "DELETE FROM t1 WHERE a < $r;" - - execsql { - COMMIT; - PRAGMA incremental_vacuum; - PRAGMA freelist_count; - PRAGMA integrity_check; - SELECT (count(*) > 0) FROM t1; - } -} {0 ok 1} -db close -file delete -force test.db - - -# create a database with many hundred tables such that the schema -# will overflow the first several pages of the database. verify the schema -# is intact on open. -do_test multipage-schema { - sqlite_orig db test.db - execsql { - PRAGMA key = 'testkey'; - BEGIN EXCLUSIVE; - } db - - for {set i 1} {$i<=300} {incr i} { - execsql "CREATE TABLE tab$i (a TEXT, b TEXT, c TEXT, d TEXT, e TEXT, f TEXT, g TEXT, h TEXT, i TEXT, j TEXT, k, TEXT, l, m TEXT, n TEXT, o TEXT, p TEXT);" db - } - - execsql { - COMMIT; - } db - - db close - sqlite_orig db test.db - - execsql { - PRAGMA key = 'testkey'; - SELECT count(*) FROM sqlite_master where type = 'table'; - } db - -} {300} -db close -file delete -force test.db - -# create a database with many hundred tables such that the schema -# will overflow the first several pages of the database. this time, enable -# autovacuum on the database, which will cause sqlite to do some "short reads" -# after the end of the main database file. verify that there are no HMAC errors -# resulting from the short reads, and that the schema is intact when -# the database is reopened -do_test multipage-schema-autovacuum-shortread { - sqlite_orig db test.db - execsql { - PRAGMA key = 'testkey'; - PRAGMA auto_vacuum = FULL; - BEGIN EXCLUSIVE; - } db - - for {set i 1} {$i<=300} {incr i} { - execsql "CREATE TABLE tab$i (a TEXT, b TEXT, c TEXT, d TEXT, e TEXT, f TEXT, g TEXT, h TEXT, i TEXT, j TEXT, k, TEXT, l, m TEXT, n TEXT, o TEXT, p TEXT);" db - } - - execsql { - COMMIT; - } db - - db close - sqlite_orig db test.db - - execsql { - PRAGMA key = 'testkey'; - SELECT count(*) FROM sqlite_master where type = 'table'; - } db - -} {300} -db close -file delete -force test.db - -# same as multi-page-schema-autovacuum-shortread, except -# using write ahead log mode -do_test multipage-schema-autovacuum-shortread-wal { - sqlite_orig db test.db - execsql { - PRAGMA key = 'testkey'; - PRAGMA auto_vacuum = FULL; - PRAGMA journal_mode = WAL; - BEGIN EXCLUSIVE; - } db - - for {set i 1} {$i<=300} {incr i} { - execsql "CREATE TABLE tab$i (a TEXT, b TEXT, c TEXT, d TEXT, e TEXT, f TEXT, g TEXT, h TEXT, i TEXT, j TEXT, k, TEXT, l, m TEXT, n TEXT, o TEXT, p TEXT);" db - } - - execsql { - COMMIT; - } db - - db close - sqlite_orig db test.db - - execsql { - PRAGMA key = 'testkey'; - SELECT count(*) FROM sqlite_master where type = 'table'; - } db -} {300} -db close -file delete -force test.db - -# open a 3.0 database with little endian hmac page numbers (default) -# verify it can be opened -do_test open-3.0-le-database { - sqlite_orig db sqlcipher-3.0-testkey.db - execsql { - PRAGMA key = 'testkey'; - SELECT count(*) FROM t1; - SELECT distinct * FROM t1; - } -} {78536 1 1 one one 1 2 one two} -db close - -# open a 2.0 database with little endian hmac page numbers (default) -# verify it can be opened -do_test open-2.0-le-database { - sqlite_orig db sqlcipher-2.0-le-testkey.db - execsql { - PRAGMA key = 'testkey'; - PRAGMA kdf_iter = 4000; - SELECT count(*) FROM t1; - SELECT distinct * FROM t1; - } -} {78536 1 1 one one 1 2 one two} -db close - -# open a 2.0 database with big-endian hmac page numbers -# verify it can be opened -do_test open-2.0-be-database { - sqlite_orig db sqlcipher-2.0-be-testkey.db - execsql { - PRAGMA key = 'testkey'; - PRAGMA cipher_hmac_pgno = be; - PRAGMA kdf_iter = 4000; - SELECT count(*) FROM t1; - SELECT distinct * FROM t1; - } -} {78536 1 1 one one 1 2 one two} -db close - -# open a 2.0 database with big-endian hmac page numbers -# attach a new database with little endian page numbers (default) -# copy schema between the two, and verify the latter -# can be opened -do_test be-to-le-migration { - sqlite_orig db sqlcipher-2.0-be-testkey.db - - execsql { - PRAGMA key = 'testkey'; - PRAGMA cipher_hmac_pgno = be; - PRAGMA kdf_iter = 4000; - ATTACH DATABASE 'test.db' AS db2 KEY 'testkey'; - CREATE TABLE db2.t1(a,b); - INSERT INTO db2.t1 SELECT * FROM main.t1; - DETACH DATABASE db2; - } - db close - - sqlite_orig db test.db - execsql { - PRAGMA key = 'testkey'; - SELECT count(*) FROM t1; - SELECT distinct * FROM t1; - } -} {78536 1 1 one one 1 2 one two} -db close -file delete -force test.db - -# verify the pragma cipher_use_hmac -# is set to true be default -do_test verify-pragma-cipher-use-hmac-default { - sqlite_orig db test.db - execsql { - PRAGMA key = 'test'; - PRAGMA cipher_use_hmac; - } -} {1} -db close -file delete -force test.db - -# verify the pragma cipher_use_hmac -# reports the flag turned off -do_test verify-pragma-cipher-use-hmac-off { - sqlite_orig db test.db - execsql { - PRAGMA key = 'test'; - PRAGMA cipher_use_hmac = off; - PRAGMA cipher_use_hmac; - } -} {0} -db close -file delete -force test.db - -# verify the pragma default_cipher_use_hmac -# is set to true by default -do_test verify-pragma-cipher-default-use-hmac-default { - sqlite_orig db test.db - execsql { - PRAGMA cipher_default_use_hmac; - } -} {1} -db close -file delete -force test.db - -# verify the pragma default_cipher_use_hmac -# reports the flag turned off -do_test verify-pragma-cipher-default-use-hmac-off { - sqlite_orig db test.db - execsql { - PRAGMA cipher_default_use_hmac = off; - PRAGMA cipher_default_use_hmac; - -- Be sure to turn cipher_default_use_hmac - -- back on or it will break later tests - -- (it's a global flag) - PRAGMA cipher_default_use_hmac = ON; - } -} {0} -db close -file delete -force test.db - -# verify the pragma default_cipher_kdf_iter -# is set to 64000 by default -do_test verify-pragma-cipher-default-kdf-iter-default { - sqlite_orig db test.db - execsql { - PRAGMA cipher_default_kdf_iter; - } -} {64000} -db close -file delete -force test.db - - -# verify the pragma default_cipher_kdf_ter -# reports changes -do_test verify-pragma-cipher-default-use-hmac-off { - sqlite_orig db test.db - execsql { - PRAGMA cipher_default_kdf_iter = 1000; - PRAGMA cipher_default_kdf_iter; - PRAGMA cipher_default_kdf_iter = 64000; - } -} {1000} -db close -file delete -force test.db - -# verify the pragma kdf_iter -# reports the default value -do_test verify-pragma-kdf-iter-reports-default { - sqlite_orig db test.db - execsql { - PRAGMA key = 'test'; - PRAGMA kdf_iter; - } -} {64000} -db close -file delete -force test.db - -# verify the pragma kdf_iter -# reports value changed -do_test verify-pragma-kdf-iter-reports-value-changed { - sqlite_orig db test.db - execsql { - PRAGMA key = 'test'; - PRAGMA kdf_iter = 8000; - PRAGMA kdf_iter; - } -} {8000} -db close -file delete -force test.db - -# verify the pragma fast_kdf_iter -# reports the default value -do_test verify-pragma-fast-kdf-iter-reports-default { - sqlite_orig db test.db - execsql { - PRAGMA key = 'test'; - PRAGMA fast_kdf_iter; - } -} {2} -db close -file delete -force test.db - -# verify the pragma fast_kdf_iter -# reports value changed -do_test verify-pragma-kdf-iter-reports-value-changed { - sqlite_orig db test.db - execsql { - PRAGMA key = 'test'; - PRAGMA fast_kdf_iter = 4000; - PRAGMA fast_kdf_iter; - } -} {4000} -db close -file delete -force test.db - -# verify the pragma cipher_page_size -# reports default value -do_test verify-pragma-cipher-page-size-default { - sqlite_orig db test.db - execsql { - PRAGMA key = 'test'; - PRAGMA cipher_page_size; - } -} {1024} -db close -file delete -force test.db - -# verify the pragma cipher_page_size -# reports change in value -do_test verify-pragma-cipher-page-size-changed { - sqlite_orig db test.db - execsql { - PRAGMA key = 'test'; - PRAGMA cipher_page_size = 4096; - PRAGMA cipher_page_size; - } -} {4096} -db close -file delete -force test.db - -# verify invalid cipher does not cause segfault -if_built_with_openssl verify-invalid-cipher-does-not-segfault { - sqlite_orig db test.db - execsql { - PRAGMA key = 'test'; - PRAGMA cipher = 'junk'; - PRAGMA cipher; - } -} {AES-256-CBC} -db close -file delete -force test.db - -# verify setting cipher_store_pass before key -# does not cause segfault -do_test verify-cipher-store-pass-before-key-does-not-segfault { - sqlite_orig db test.db - execsql { - PRAGMA cipher_store_pass = 1; - PRAGMA key = 'test'; - } -} {} -db close -file delete -force test.db - -# verify the pragma cipher -# reports the default value -if_built_with_openssl verify-pragma-cipher-default { - sqlite_orig db test.db - execsql { - PRAGMA key = 'test'; - PRAGMA cipher; - } -} {AES-256-CBC} -db close -file delete -force test.db - -# verify the pragma cipher -# reports a change in value -if_built_with_openssl verify-pragma-cipher-changed { - sqlite_orig db test.db - execsql { - PRAGMA key = 'test'; - PRAGMA cipher = 'AES-256-ECB'; - PRAGMA cipher; - } -} {AES-256-ECB} -db close -file delete -force test.db - -# verify the pragma cipher_hmac_salt_mask reports default -do_test verify-pragma-hmac-salt-mask-reports-default { - sqlite_orig db test.db - execsql { - PRAGMA key = 'test'; - PRAGMA cipher_hmac_salt_mask; - } -} {3a} -db close -file delete -force test.db - -# verify the pragma cipher_hmac_salt_mask reports -# reports value changed -do_test verify-pragma-hmac-salt-mask-reports-value-changed { - sqlite_orig db test.db - execsql { - PRAGMA key = 'test'; - PRAGMA cipher_hmac_salt_mask = "x'11'"; - PRAGMA cipher_hmac_salt_mask; - } -} {11} -db close -file delete -force test.db - -# verify the pragma cipher_hmac_pgno reports default -do_test verify-pragma-hmac-pgno-reports-default { - sqlite_orig db test.db - execsql { - PRAGMA key = 'test'; - PRAGMA cipher_hmac_pgno; - } -} {le} -db close -file delete -force test.db - -# verify the pragma cipher_hmac_pgno -# reports value changed -do_test verify-pragma-hmac-pgno-reports-value-changed { - sqlite_orig db test.db - execsql { - PRAGMA key = 'test'; - PRAGMA cipher_hmac_pgno = be; - PRAGMA cipher_hmac_pgno; - PRAGMA cipher_hmac_pgno = native; - PRAGMA cipher_hmac_pgno; - PRAGMA cipher_hmac_pgno = le; - PRAGMA cipher_hmac_pgno; - } -} {be native le} -db close -file delete -force test.db - -# open a 2.0 beta database with 4000 round hmac kdf and 0x00 -# hmac salt mask -# verify it can be opened -do_test open-2.0-beta-database { - sqlite_orig db sqlcipher-2.0-beta-testkey.db - execsql { - PRAGMA key = 'testkey'; - PRAGMA kdf_iter = 4000; - PRAGMA fast_kdf_iter = 4000; - PRAGMA cipher_hmac_salt_mask = "x'00'"; - SELECT count(*) FROM t1; - SELECT distinct * FROM t1; - } -} {38768 test-0-0 test-0-1 test-1-0 test-1-1} -db close - -# open a 2.0 beta database -# attach a new standard database -# copy schema between the two, and verify the latter -# can be opened -do_test 2.0-beta-to-2.0-migration { - sqlite_orig db sqlcipher-2.0-beta-testkey.db - - execsql { - PRAGMA key = 'testkey'; - PRAGMA cipher_hmac_salt_mask = "x'00'"; - PRAGMA kdf_iter = 4000; - PRAGMA fast_kdf_iter = 4000; - SELECT count(*) FROM sqlite_master; - - PRAGMA cipher_hmac_salt_mask = "x'3a'"; - ATTACH DATABASE 'test.db' AS db2 KEY 'testkey'; - - CREATE TABLE db2.t1(a,b); - INSERT INTO db2.t1 SELECT * FROM main.t1; - DETACH DATABASE db2; - } - db close - - sqlite_orig db test.db - execsql { - PRAGMA key = 'testkey'; - SELECT distinct * FROM t1; - } -} {test-0-0 test-0-1 test-1-0 test-1-1} -db close -file delete -force test.db - -if_built_with_libtomcrypt verify-default-cipher { - sqlite_orig db test.db - execsql { - PRAGMA key='test'; - PRAGMA cipher; - } -} {rijndael} -db close -file delete -force test.db - -if_built_with_commoncrypto verify-default-cipher { - sqlite_orig db test.db - execsql { - PRAGMA key='test'; - PRAGMA cipher; - } -} {aes-256-cbc} -db close -file delete -force test.db - -do_test migrate-1.1.8-database-to-3x-format { - file copy -force sqlcipher-1.1.8-testkey.db test.db - sqlite_orig db test.db - execsql { - PRAGMA key = 'testkey'; - PRAGMA cipher_migrate; - } - db close - - sqlite_orig db test.db - execsql { - PRAGMA key = 'testkey'; - SELECT count(*) FROM sqlite_master; - } -} {1} -db close -file delete -force test.db - -do_test migrate-2-0-le-database-to-3x-format { - file copy -force sqlcipher-2.0-le-testkey.db test.db - sqlite_orig db test.db - execsql { - PRAGMA key = 'testkey'; - PRAGMA cipher_migrate; - } - db close - - sqlite_orig db test.db - execsql { - PRAGMA key = 'testkey'; - SELECT count(*) FROM sqlite_master; - } -} {1} -db close -file delete -force test.db - -do_test key-database-by-name { - sqlite_orig db test.db - execsql { - attach database 'new.db' as new; - pragma new.key = 'foo'; - create table new.t1(a,b); - insert into new.t1(a,b) values('foo', 'bar'); - detach database new; - } - db close - - sqlite_orig db new.db - execsql { - pragma key = 'foo'; - select * from t1; - } -} {foo bar} -db close -file delete -force test.db -file delete -force new.db - -do_test key-multiple-databases-with-different-keys-using-pragma { - sqlite_orig db test.db - execsql { - pragma key = 'foobar'; - create table t1(a,b); - insert into t1(a,b) values('baz','qux'); - attach database 'new.db' as new; - pragma new.key = 'foo'; - create table new.t1(a,b); - insert into new.t1(a,b) values('foo', 'bar'); - detach database new; - } - db close - - sqlite_orig db new.db - execsql { - pragma key = 'foo'; - attach database 'test.db' as test key 'foobar'; - select * from t1; - select * from test.t1; - } -} {foo bar baz qux} -db close -file delete -force test.db -file delete -force new.db - -do_test rekey-database-by-name { - sqlite_orig db test.db - execsql { - attach database 'new.db' as new; - pragma new.key = 'foo'; - create table new.t1(a,b); - insert into new.t1(a,b) values('foo', 'bar'); - pragma new.rekey = 'bar'; - detach database new; - } - db close - - sqlite_orig db new.db - execsql { - pragma key = 'bar'; - select * from t1; - } -} {foo bar} -db close -file delete -force test.db -file delete -force new.db - -# Requires SQLCipher to be built with -DSQLCIPHER_TEST -if_built_with_libtomcrypt verify-random-data-alters-file-content { - file delete -force test.db - file delete -force test2.db - file delete -force test3.db - set rc {} - - sqlite_orig db test.db - execsql { - PRAGMA key="x'2DD29CA851E7B56E4697B0E1F08507293D761A05CE4D1B628663F411A8086D99'"; - create table t1(a,b); - } - db close - sqlite_orig db test2.db - execsql { - PRAGMA key="x'2DD29CA851E7B56E4697B0E1F08507293D761A05CE4D1B628663F411A8086D99'"; - create table t1(a,b); - } - db close - sqlite_orig db test3.db - execsql { - PRAGMA key="x'2DD29CA851E7B56E4697B0E1F08507293D761A05CE4D1B628663F411A8086D99'"; - PRAGMA cipher_add_random = "x'deadbaad'"; - create table t1(a,b); - } - db close - lappend rc [cmpFilesChunked test.db test2.db] - lappend rc [cmpFilesChunked test2.db test3.db] -} {0 1} -file delete -force test.db -file delete -force test2.db -file delete -force test3.db - -do_test can-migrate-with-keys-longer-than-64-characters { - sqlite_orig db test.db - execsql { - PRAGMA key = "012345678901234567890123456789012345678901234567890123456789012345"; - PRAGMA kdf_iter = 4000; - PRAGMA user_version = 5; - } - db close - - sqlite_orig db test.db - execsql { - PRAGMA key = "012345678901234567890123456789012345678901234567890123456789012345"; - PRAGMA cipher_migrate; - } - db close - - sqlite_orig db test.db - execsql { - PRAGMA key = "012345678901234567890123456789012345678901234567890123456789012345"; - PRAGMA user_version; - } -} {5} -db close -file delete -force test.db - -do_test can-migrate-with-raw-hex-key { - sqlite_orig db test.db - execsql { - PRAGMA key = "x'2DD29CA851E7B56E4697B0E1F08507293D761A05CE4D1B628663F411A8086D99'"; - PRAGMA kdf_iter = 4000; - PRAGMA cipher_use_hmac = off; - PRAGMA user_version = 5; - } - db close - - sqlite_orig db test.db - execsql { - PRAGMA key = "x'2DD29CA851E7B56E4697B0E1F08507293D761A05CE4D1B628663F411A8086D99'"; - PRAGMA cipher_migrate; - } - - sqlite_orig db test.db - execsql { - PRAGMA key = "x'2DD29CA851E7B56E4697B0E1F08507293D761A05CE4D1B628663F411A8086D99'"; - PRAGMA user_version; - } - -} {5} -db close -file delete -force test.db - -do_test attach_database_with_non_default_page_size { - sqlite_orig db test2.db - execsql { - PRAGMA key = 'test'; - PRAGMA cipher_page_size = 4096; - CREATE TABLE t1(a,b); - INSERT INTO t1(a,b) values('one for the money', 'two for the show'); - INSERT INTO t1(a,b) values('three to get ready', 'now, go cat, go'); - } - db close - - sqlite_orig db test.db - execsql { - PRAGMA cipher_default_page_size = 4096; - PRAGMA key = 'test'; - ATTACH DATABASE 'test2.db' as test2 KEY 'test'; - SELECT count(*) FROM test2.t1; - } -} {2} -db close -file delete -force test.db test2.db - -sqlite3_test_control_pending_byte $old_pending_byte -finish_test diff --git a/test/cse.test b/test/cse.test index 57cdef5acc..3912bc326c 100644 --- a/test/cse.test +++ b/test/cse.test @@ -18,6 +18,7 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl +set testprefix cse do_test cse-1.1 { execsql { @@ -157,4 +158,43 @@ for {set i 1} {$i<100} {incr i} { } $answer } +#------------------------------------------------------------------------- +# Ticket fd1bda016d1a +# +reset_db +do_execsql_test 3.0 { + CREATE TABLE t1(a TEXT, b); + INSERT INTO t1 VALUES('hello', 0); + INSERT INTO t1 VALUES('world', 0); + + CREATE TABLE t2(x TEXT); + INSERT INTO t2 VALUES('hello'); + INSERT INTO t2 VALUES('world'); + + CREATE TABLE t3(y); + INSERT INTO t3 VALUES(1000); +} {} + +do_execsql_test 3.1 { + SELECT 1000 = y FROM t3 +} {1} + +do_execsql_test 3.2 { + SELECT 1000 IN (SELECT x FROM t2), 1000 = y FROM t3 +} {0 1} + +do_execsql_test 3.3 { + SELECT 0 IN (SELECT a), (SELECT a LIMIT 0) FROM t1 +} {0 {} 0 {}} + +do_execsql_test 3.4 { + SELECT 0 IN (SELECT a) FROM t1 WHERE a = 'hello' OR (SELECT a LIMIT 0); +} {0} + +do_execsql_test 3.5 { + CREATE TABLE v0(v1 VARCHAR0); + INSERT INTO v0 VALUES(2), (3); + SELECT 0 IN(SELECT v1) FROM v0 WHERE v1 = 2 OR(SELECT v1 LIMIT 0); +} {0} + finish_test diff --git a/test/csv01.test b/test/csv01.test new file mode 100644 index 0000000000..ecb1a968de --- /dev/null +++ b/test/csv01.test @@ -0,0 +1,279 @@ +# 2016-06-02 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# Test cases for CSV virtual table. + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix csv01 + +ifcapable !vtab||!cte { finish_test ; return } + +load_static_extension db csv + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE temp.t1 USING csv( + data= +'1,2,3,4 +5,6,7,8 +9,10,11,12 +13,14,15,16 +', + columns=4 + ); + SELECT * FROM t1 WHERE c1=10; +} {9 10 11 12} +do_execsql_test 1.1 { + SELECT * FROM t1 WHERE c1='10'; +} {9 10 11 12} +do_execsql_test 1.2 { + SELECT rowid FROM t1; +} {1 2 3 4} + +do_execsql_test 1.3 { + DROP TABLE temp.t1; + CREATE VIRTUAL TABLE temp.t1 USING csv( + data= +'a,b,"mix-bloom-eel","soft opinion" +1,2,3,4 +5,6,7,8 +9,10,11,12 +13,14,15,16 +', + header=1 + ); + SELECT * FROM t1 WHERE "soft opinion"=12; +} {9 10 11 12} +do_execsql_test 1.4 { + SELECT name FROM pragma_table_xinfo('t1'); +} {a b mix-bloom-eel {soft opinion}} + +do_execsql_test 1.5 { + DROP TABLE temp.t1; + CREATE VIRTUAL TABLE temp.t1 USING csv( + data= +'a,b,"mix-bloom-eel","soft opinion" +1,2,3,4 +5,6,7,8 +9,10,11,12 +13,14,15,16 +', + header=false + ); + SELECT * FROM t1 WHERE c1='b'; +} {a b mix-bloom-eel {soft opinion}} +do_execsql_test 1.6 { + SELECT name FROM pragma_table_xinfo('t1'); +} {c0 c1 c2 c3} + +do_execsql_test 1.7 { + DROP TABLE temp.t1; + CREATE VIRTUAL TABLE temp.t1 USING csv( + data= +'a,b,"mix-bloom-eel","soft opinion" +1,2,3,4 +5,6,7,8 +9,10,11,12 +13,14,15,16 +', + header, + schema='CREATE TABLE x(x0,x1,x2,x3,x4)', + columns=5 + ); + SELECT * FROM t1 WHERE x1='6'; +} {5 6 7 8 {}} +do_execsql_test 1.8 { + SELECT name FROM pragma_table_xinfo('t1'); +} {x0 x1 x2 x3 x4} + + +do_execsql_test 2.0 { + DROP TABLE t1; + CREATE VIRTUAL TABLE temp.t2 USING csv( + data= +'1,2,3,4 +5,6,7,8 +9,10,11,12 +13,14,15,16 +', + columns=4, + schema='CREATE TABLE t2(a INT, b TEXT, c REAL, d BLOB)' + ); + SELECT * FROM t2 WHERE a=9; +} {9 10 11 12} +do_execsql_test 2.1 { + SELECT * FROM t2 WHERE b=10; +} {9 10 11 12} +do_execsql_test 2.2 { + SELECT * FROM t2 WHERE c=11; +} {9 10 11 12} +do_execsql_test 2.3 { + SELECT * FROM t2 WHERE d=12; +} {} +do_execsql_test 2.4 { + SELECT * FROM t2 WHERE d='12'; +} {9 10 11 12} +do_execsql_test 2.5 { + SELECT * FROM t2 WHERE a='9'; +} {9 10 11 12} + +do_execsql_test 3.0 { + DROP TABLE t2; + CREATE VIRTUAL TABLE temp.t3 USING csv( + data= +'1,2,3,4 +5,6,7,8 +9,10,11,12 +13,14,15,16 +', + columns=4, + schema= + 'CREATE TABLE t3(a PRIMARY KEY,b TEXT,c TEXT,d TEXT) WITHOUT ROWID', + testflags=1 + ); + SELECT a FROM t3 WHERE b=6 OR c=7 OR d=12 ORDER BY +a; +} {5 9} +do_execsql_test 3.1 { + SELECT a FROM t3 WHERE +b=6 OR c=7 OR d=12 ORDER BY +a; +} {5 9} + +# The rowid column is not visible on a WITHOUT ROWID virtual table +do_catchsql_test 3.2 { + SELECT rowid, a FROM t3; +} {1 {no such column: rowid}} + +# Multi-column WITHOUT ROWID virtual tables may not be writable. +do_catchsql_test 4.0 { + DROP TABLE t3; + CREATE VIRTUAL TABLE temp.t4 USING csv_wr( + data= +'1,2,3,4 +5,6,7,8 +9,10,11,12 +13,14,15,16', + columns=4, + schema= + 'CREATE TABLE t3(a,b,c,d,PRIMARY KEY(a,b)) WITHOUT ROWID', + testflags=1 + ); +} {1 {bad schema: 'CREATE TABLE t3(a,b,c,d,PRIMARY KEY(a,b)) WITHOUT ROWID' - not an error}} + +# WITHOUT ROWID tables with a single-column PRIMARY KEY may be writable. +do_catchsql_test 4.1 { + DROP TABLE IF EXISTS t4; + CREATE VIRTUAL TABLE temp.t4 USING csv_wr( + data= +'1,2,3,4 +5,6,7,8 +9,10,11,12 +13,14,15,16', + columns=4, + schema= + 'CREATE TABLE t3(a,b,c,d,PRIMARY KEY(b)) WITHOUT ROWID', + testflags=1 + ); +} {0 {}} + +do_catchsql_test 4.2 { + DROP TABLE IF EXISTS t5; + CREATE VIRTUAL TABLE temp.t5 USING csv_wr( + data= + '1,2,3,4 + 5,6,7,8 + 9,10,11,12 + 13,14,15,16', + columns=4, + schema= + 'CREATE TABLE t3(a,b,c,d) WITHOUT ROWID', + testflags=1 + ); +} {1 {bad schema: 'CREATE TABLE t3(a,b,c,d) WITHOUT ROWID' - PRIMARY KEY missing on table t3}} + +# 2018-04-24 +# Memory leak reported on the sqlite-users mailing list by Ralf Junker. +# +do_catchsql_test 4.3 { + CREATE VIRTUAL TABLE IF NOT EXISTS temp.t1 + USING csv(filename='FileDoesNotExist.csv'); +} {1 {cannot open 'FileDoesNotExist.csv' for reading}} + +# 2018-06-02 +# Problem with single-column CSV support reported on the mailing list +# by Trent W. Buck. +# +do_execsql_test 4.4 { + CREATE VIRTUAL TABLE temp.trent USING csv(data='1'); + SELECT * FROM trent; +} {1} + +# 2018-12-26 +# Bug report on the mailing list +# +forcedelete csv01.csv +set fd [open csv01.csv wb] +puts $fd "a,b,c,d\r\n1,2,3,4\r\none,two,three,four\r\n5,6,7,8" +close $fd +do_execsql_test 5.1 { + CREATE VIRTUAL TABLE t5_1 USING csv(filename='csv01.csv'); + SELECT name FROM temp.pragma_table_info('t5_1'); +} {c0 c1 c2 c3} +do_execsql_test 5.2 { + SELECT *, '|' FROM t5_1; +} {a b c d | 1 2 3 4 | one two three four | 5 6 7 8 |} +do_execsql_test 5.3 { + DROP TABLE t5_1; + CREATE VIRTUAL TABLE t5_1 USING csv(filename='csv01.csv', header); + SELECT name FROM temp.pragma_table_info('t5_1'); +} {a b c d} +do_execsql_test 5.4 { + SELECT *, '|' FROM t5_1; +} {1 2 3 4 | one two three four | 5 6 7 8 |} + +#------------------------------------------------------------------------- + +proc randomtext {n} { + string range [db one {SELECT hex(randomblob($n))}] 1 $n +} + +for {set ii 0} {$ii < 200} {incr ii} { + reset_db + load_static_extension db csv + set fd [open csv.data w] + puts $fd "a,b" + puts $fd "[randomtext $ii],abcd" + close $fd + do_execsql_test 6.$ii.1 { + CREATE VIRTUAL TABLE abc USING csv(filename='csv.data', header=true); + } + do_execsql_test 6.$ii.2 { + SELECT count(*) FROM abc + } 1 +} + +for {set ii 0} {$ii < 20} {incr ii} { + reset_db + load_static_extension db csv + set T [randomtext $ii] + set fd [open csv.data w] + puts $fd "a,b" + puts -nonewline $fd "abcd,$T" + close $fd + do_execsql_test 7.$ii.1 { + CREATE VIRTUAL TABLE abc USING csv(filename='csv.data', header=true); + } + breakpoint + do_execsql_test 7.$ii.2 { + SELECT * FROM abc + } [list abcd $T] +} + + +finish_test diff --git a/test/ctime.test b/test/ctime.test index e4cb156168..26b2fa2ee2 100644 --- a/test/ctime.test +++ b/test/ctime.test @@ -61,8 +61,27 @@ do_test ctime-1.2.2 { list [ lindex $ans 0 ] [ expr { [lsort [lindex $ans 1]]==[lindex $ans 1] } ] } {0 1} +# Check the THREADSAFE option for SQLITE_THREADSAFE=2 builds (there are +# a couple of these configurations in releasetest.tcl). +# +ifcapable threadsafe2 { + foreach {tn opt res} { + 1 SQLITE_THREADSAFE 1 + 2 THREADSAFE 1 + 3 THREADSAFE=0 0 + 4 THREADSAFE=1 0 + 5 THREADSAFE=2 1 + 6 THREADSAFE= 0 + } { + do_execsql_test ctime-1.3.$tn { + SELECT sqlite_compileoption_used($opt) + } $res + } +} + # SQLITE_THREADSAFE should pretty much always be defined # one way or the other, and it must have a value of 0 or 1. +sqlite3_db_config db SQLITE_DBCONFIG_DQS_DML 1 do_test ctime-1.4.1 { catchsql { SELECT sqlite_compileoption_used('SQLITE_THREADSAFE'); @@ -196,9 +215,10 @@ set tc 1 foreach opt $opts { do_test ctime-2.5.$tc { set N [ expr {$tc-1} ] - set ans1 [ catchsql { + set ans1 [catch {db one { SELECT sqlite_compileoption_get($N); - } ] + }} msg] + lappend ans1 $msg set ans2 [ catchsql { SELECT sqlite_compileoption_used($opt); } ] @@ -223,5 +243,17 @@ do_test ctime-2.5.$tc { } ] } {0 {{}}} +#-------------------------------------------------------------------------- +# Test that SQLITE_DIRECT_OVERFLOW_READ is reflected in the output of +# "PRAGMA compile_options". +# +ifcapable direct_read { + set res 1 +} else { + set res 0 +} +do_test ctime-3.0.1 { + expr [lsearch [db eval {PRAGMA compile_options}] DIRECT_OVERFLOW_READ]>=0 +} $res finish_test diff --git a/test/cursorhint.test b/test/cursorhint.test index ae86120fa8..d1bd4e8e78 100644 --- a/test/cursorhint.test +++ b/test/cursorhint.test @@ -69,7 +69,7 @@ do_test 1.2 { p5_of_opcode db OpenRead { SELECT * FROM t1 CROSS JOIN t2 WHERE a=x } -} {00 00} +} {0 0} # Do the same test the other way around. # @@ -82,7 +82,7 @@ do_test 2.2 { p5_of_opcode db OpenRead { SELECT * FROM t2 CROSS JOIN t1 WHERE a=x } -} {00 00} +} {0 0} # Various expressions captured by CursorHint # @@ -117,7 +117,7 @@ do_test 4.2 { p5_of_opcode db OpenRead { SELECT * FROM t1 WHERE b>11; } -} {02 00} +} {2 0} do_test 4.3asc { p4_of_opcode db CursorHint { SELECT c FROM t1 WHERE b<11 ORDER BY b ASC; @@ -132,7 +132,7 @@ do_test 4.4 { p5_of_opcode db OpenRead { SELECT c FROM t1 WHERE b<11; } -} {00} +} {0} do_test 4.5asc { p4_of_opcode db CursorHint { @@ -159,4 +159,56 @@ do_test 4.6desc { } } {AND(AND(EQ(c0,22),GE(c1,10)),LE(c1,20))} +# 2023-03-24 https://sqlite.org/forum/forumpost/591006b1cc +# +reset_db +do_execsql_test 5.0 { + CREATE TABLE t1(x TEXT PRIMARY KEY) WITHOUT ROWID; + CREATE VIEW t2 AS SELECT 0 FROM t1 WHERE x>='a' OR x='1'; + SELECT * FROM t2 RIGHT JOIN t1 ON true; +} +# Additional test case from https://sqlite.org/forum/forumpost/d34ad68c36?t=c +# which is a different way to acces the same problem. +# +do_execsql_test 5.1 { + CREATE TABLE v1 (c1, PRIMARY KEY( c1 )) WITHOUT ROWID; + CREATE VIEW v2 AS SELECT 0 FROM v1 WHERE c1 IS '' OR c1 > ''; + CREATE VIEW v3 AS SELECT 0 FROM v2 JOIN (v2 RIGHT JOIN v1); + CREATE VIEW v4 AS SELECT 0 FROM v3, v3; + SELECT * FROM v3 JOIN v3 AS a0, v4 AS a1, v4 AS a2, v3 AS a3, + v3 AS a4, v4 AS a5 + ORDER BY 1; +} + +# 2023-04-10 https://sqlite.org/forum/forumpost/0b53708c95 +# +do_execsql_test 6.0 { + CREATE TABLE t6(a TEXT UNIQUE, b TEXT); + INSERT INTO t6(a,b) VALUES('uvw','xyz'),('abc','def'); + WITH v1(a) AS (SELECT a COLLATE NOCASE FROM t6) + SELECT v1.a, count(*) FROM t6 LEFT JOIN v1 ON true + GROUP BY 1 + HAVING (SELECT true FROM t6 AS aa LEFT JOIN t6 AS bb ON length(v1.a)>5); +} {abc 2 uvw 2} + + +# 2023-05-04 https://sqlite.org/forum/forumpost/29a47cf6d1 +# +# codeCursorHint() should not walk expressions that have been optimized +# out and converted to TRUE or FALSE. This only comes up when compiling +# with SQLITE_ENABLE_CURSOR_HINTS +# +reset_db +do_execsql_test 7.1 { + CREATE TABLE t1(a INT PRIMARY KEY) WITHOUT ROWID; + CREATE TABLE t2(b INT PRIMARY KEY) WITHOUT ROWID; + CREATE TABLE t3(c INT PRIMARY KEY) WITHOUT ROWID; + INSERT INTO t1(a) VALUES(1),(2); + INSERT INTO t2(b) VALUES(4),(8); + INSERT INTO t3(c) VALUES(16),(32); + CREATE VIEW v4(d) AS SELECT c FROM t3; + SELECT * FROM t1 RIGHT JOIN t2 ON true JOIN v4 ON (d IS NULL); +} {} + + finish_test diff --git a/test/cursorhint2.test b/test/cursorhint2.test new file mode 100644 index 0000000000..a78d151b98 --- /dev/null +++ b/test/cursorhint2.test @@ -0,0 +1,204 @@ +# 2016 June 17 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus is on testing that cursor-hints are correct for queries +# involving LEFT JOIN. +# + + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set ::testprefix cursorhint2 + +ifcapable !cursorhints { + finish_test + return +} + +proc extract_hints {sql} { + + db eval "SELECT tbl_name, rootpage FROM sqlite_master where rootpage" { + set lookup($rootpage) $tbl_name + } + + set ret [list] + db eval "EXPLAIN $sql" a { + switch -- $a(opcode) { + OpenRead { + set csr($a(p1)) $lookup($a(p2)) + } + CursorHint { + lappend ret $csr($a(p1)) $a(p4) + } + } + } + + set ret +} + +proc do_extract_hints_test {tn sql ret} { + uplevel [list do_test $tn [list extract_hints $sql] [list {*}$ret]] +} + +do_execsql_test 1.0 { + PRAGMA automatic_index = 0; + CREATE TABLE t1(a, b); + CREATE TABLE t2(c, d); + CREATE TABLE t3(e, f); +} + +do_extract_hints_test 1.1 { + SELECT * FROM t1 WHERE a=1; +} { + t1 EQ(c0,1) +} + +do_extract_hints_test 1.2 { + SELECT * FROM t1 CROSS JOIN t2 ON (a=c) WHERE d IS NULL; +} { + t2 {AND(ISNULL(c1),EQ(r[1],c0))} +} + +do_extract_hints_test 1.3 { + SELECT * FROM t1 LEFT JOIN t2 ON (a=c) WHERE d IS NULL; +} { + t2 {EQ(r[2],c0)} +} + +do_extract_hints_test 1.4 { + SELECT * FROM t1 LEFT JOIN t2 ON (a=c AND a=10) WHERE d IS NULL; +} { + t2 {AND(EQ(r[2],c0),EQ(r[3],10))} +} + +do_extract_hints_test 1.5 { + SELECT * FROM t1 CROSS JOIN t2 ON (a=c AND a=10) WHERE d IS NULL; +} { + t1 EQ(c0,10) t2 {AND(ISNULL(c1),EQ(r[3],c0))} +} + +do_extract_hints_test 1.6 { + SELECT * FROM t1 LEFT JOIN t2 ON (a=c) LEFT JOIN t3 ON (d=f); +} { + t2 {EQ(r[2],c0)} t3 {EQ(r[6],c1)} +} + +if 0 { + do_extract_hints_test 1.7 { + SELECT * FROM t1 LEFT JOIN t2 ON (a=c AND d=e) LEFT JOIN t3 ON (d=f); + } { + t2 {EQ(r[2],c0)} t3 {AND(EQ(r[6],c0),EQ(r[7],c1))} + } +} + +#------------------------------------------------------------------------- +# +do_execsql_test 2.0 { + CREATE TABLE x1(x, y); + CREATE TABLE x2(a, b); +} + +do_extract_hints_test 2.1 { + SELECT * FROM x1 LEFT JOIN x2 ON (a=x) WHERE b IS NULL; +} { + x2 {EQ(c0,r[2])} +} + +do_extract_hints_test 2.2 { + SELECT * FROM x1 LEFT JOIN x2 ON (a=x) WHERE b IS +NULL; +} { + x2 {EQ(c0,r[2])} +} + +do_extract_hints_test 2.3 { + SELECT * FROM x1 LEFT JOIN x2 ON (a=x) WHERE 1 = (b IS NULL) +} { + x2 {EQ(c0,r[2])} +} + +do_extract_hints_test 2.4 { + SELECT * FROM x1 LEFT JOIN x2 ON (a=x) WHERE 1 = coalesce(b, 1) +} { + x2 {EQ(c0,r[2])} +} + +do_extract_hints_test 2.5 { + SELECT * FROM x1 LEFT JOIN x2 ON (a=x) WHERE 1 = coalesce(b, 1) +} { + x2 {EQ(c0,r[2])} +} + +if {0} { + # These tests no longer work due to the LEFT-JOIN strength reduction + # optimization + do_extract_hints_test 2.6 { + SELECT * FROM x1 CROSS JOIN x2 ON (a=x) WHERE 0 = (b IS NOT NULL) + } { + x2 {EQ(c0,r[2])} + } + + do_extract_hints_test 2.7 { + SELECT * FROM x1 LEFT JOIN x2 ON (a=x) WHERE 0 = (b IS NOT +NULL) + } { + x2 {EQ(c0,r[2])} + } + + do_extract_hints_test 2.8 { + SELECT * FROM x1 LEFT JOIN x2 ON (a=x) WHERE b IS NOT +NULL + } { + x2 {EQ(c0,r[2])} + } + + do_extract_hints_test 2.9 { + SELECT * FROM x1 LEFT JOIN x2 ON (a=x) + WHERE CASE b WHEN 0 THEN 0 ELSE 1 END; + } { + x2 {EQ(c0,r[2])} + } + + do_extract_hints_test 2.10 { + SELECT * FROM x1 LEFT JOIN x2 ON (a=x) WHERE x2.b = 32+32 + } { + x2 {AND(EQ(c1,ADD(32,32)),EQ(c0,r[2]))} + } + + ifcapable !icu { + # This test only works using the built-in LIKE, not the ICU LIKE extension. + do_extract_hints_test 2.11 { + SELECT * FROM x1 LEFT JOIN x2 ON (a=x) WHERE x2.b LIKE 'abc%' + } { + x2 {AND(expr,EQ(c0,r[2]))} + } + } +} + +do_extract_hints_test 2.12 { + SELECT * FROM x1 LEFT JOIN x2 ON (a=x) WHERE coalesce(x2.b, 1) +} { + x2 {EQ(c0,r[2])} +} + +reset_db +do_execsql_test 3.0 { + CREATE TABLE t1 (i1 TEXT); + CREATE TABLE t2 (i2 TEXT UNIQUE); + INSERT INTO t1 VALUES('0'); + INSERT INTO t2 VALUES('0'); +} + +do_extract_hints_test 3.1 { + SELECT * FROM t1 CROSS JOIN t2 WHERE (t1.i1 = t2.i2) AND t2.i2 = 1; +} { + t1 {EQ(c0,r[1])} t2 EQ(c0,1) +} + + +finish_test diff --git a/test/dataversion1.test b/test/dataversion1.test new file mode 100644 index 0000000000..55f7aada0c --- /dev/null +++ b/test/dataversion1.test @@ -0,0 +1,87 @@ +# 2018-07-18 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# Test case for SQLITE_FCNTL_DATA_VERSION +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +# Construct a database and get its initial data version +sqlite3 db test.db +do_test dataversion1-100 { + db eval { + CREATE TABLE t1(x); + INSERT INTO t1(x) VALUES(99); + SELECT * FROM t1; + } +} {99} +set dv1 [file_control_data_version db main] + +# The data version does not change by ATTACH or by changes to +# other schemas within the same connection. +# +do_test dataversion1-101 { + db eval { + ATTACH ':memory:' AS aux1; + CREATE TABLE aux1.t2(y); + CREATE TEMP TABLE t3(z); + } + file_control_data_version db main +} $dv1 + +# The data version does change when SQL modifies the table +do_test dataversion1-110 { + db eval { + UPDATE t1 SET x=x+1; + } + set dv2 [file_control_data_version db] + expr {$::dv1==$dv2} +} {0} + +# But the data version is constant if there are changes to other +# schemas +set dv1 [file_control_data_version db main] +do_test dataversion1-120 { + db eval { + UPDATE t2 SET y=y+1; + } + file_control_data_version db +} $dv1 + +# Changes to the database via another connection are not detected +# until there is a read transaction. +# +sqlite3 db2 test.db +do_test dataversion1-130 { + db2 eval { + SELECT * FROM t1 + } +} {100} +do_test dataversion1-131 { + file_control_data_version db +} $dv1 +do_test dataversion1-132 { + db2 eval { + UPDATE t1 SET x=x+1; + } + set dv2 [file_control_data_version db] + expr {$::dv1==$dv2} +} {1} +do_test dataversion1-133 { + db eval {SELECT * FROM t1} + set dv2 [file_control_data_version db] + expr {$::dv1==$dv2} +} {0} + + + +finish_test diff --git a/test/date.test b/test/date.test index 2f48b111e6..5ff131f733 100644 --- a/test/date.test +++ b/test/date.test @@ -62,7 +62,7 @@ datetest 1.20 {julianday('2000-01-01 12:00:00.01')} 2451545.00000012 datetest 1.21 {julianday('2000-01-01 12:00:00.001')} 2451545.00000001 datetest 1.22 {julianday('2000-01-01 12:00:00.')} NULL datetest 1.23 julianday(12345.6) 12345.6 -datetest 1.23b julianday('12345.6') 12345.6 +datetest 1.23b julianday(1721059.5) 1721059.5 datetest 1.24 {julianday('2001-01-01 12:00:00 bogus')} NULL datetest 1.25 {julianday('2001-01-01 bogus')} NULL datetest 1.26 {julianday('2001-01-01 12:60:00')} NULL @@ -146,6 +146,8 @@ datetest 2.49 {datetime('2003-10-22 12:24','0000 second')} {2003-10-22 12:24:00} datetest 2.50 {datetime('2003-10-22 12:24','0001 second')} {2003-10-22 12:24:01} datetest 2.51 {datetime('2003-10-22 12:24','nonsense')} NULL +datetest 2.60 {datetime('2023-02-31')} {2023-03-03 00:00:00} + datetest 3.1 {strftime('%d','2003-10-31 12:34:56.432')} 31 datetest 3.2.1 {strftime('pre%fpost','2003-10-31 12:34:56.432')} pre56.432post datetest 3.2.2 {strftime('%f','2003-10-31 12:34:59.9999999')} 59.999 @@ -207,15 +209,33 @@ datetest 3.16 "strftime('[repeat 200 %Y]','2003-10-31')" [repeat 200 2003] datetest 3.17 "strftime('[repeat 200 abc%m123]','2003-10-31')" \ [repeat 200 abc10123] -foreach c {a b c e g h i k l n o p q r t v x y z - A B C D E F G I K L N O P Q R T U V Z +foreach c {a b c h i n o q r t v x y z + A B C D E K L N O Q Z 0 1 2 3 4 5 6 6 7 9 _} { datetest 3.18.$c "strftime('%$c','2003-10-31')" NULL } +datetest 3.20 {strftime('%e','2023-08-09')} { 9} +datetest 3.21 {strftime('%F %T','2023-08-09 01:23')} {2023-08-09 01:23:00} +datetest 3.22 {strftime('%k','2023-08-09 04:59:59')} { 4} +datetest 3.23 {strftime('%I%P','2023-08-09 11:59:59')} {11am} +datetest 3.24 {strftime('%I%p','2023-08-09 12:00:00')} {12PM} +datetest 3.25 {strftime('%I%P','2023-08-09 12:59:59.9')} {12pm} +datetest 3.26 {strftime('%I%p','2023-08-09 13:00:00')} {01PM} +datetest 3.27 {strftime('%I%P','2023-08-09 23:59:59')} {11pm} +datetest 3.28 {strftime('%I%p','2023-08-09 00:00:00')} {12AM} +datetest 3.29 {strftime('%l:%M%P','2023-08-09 13:00:00')} { 1:00pm} +datetest 3.30 {strftime('%F %R','2023-08-09 12:34:56')} {2023-08-09 12:34} +datetest 3.31 {strftime('%w %u','2023-01-01')} {0 7} +datetest 3.32 {strftime('%w %u','2023-01-02')} {1 1} +datetest 3.33 {strftime('%w %u','2023-01-03')} {2 2} +datetest 3.34 {strftime('%w %u','2023-01-04')} {3 3} +datetest 3.35 {strftime('%w %u','2023-01-05')} {4 4} +datetest 3.36 {strftime('%w %u','2023-01-06')} {5 5} +datetest 3.37 {strftime('%w %u','2023-01-07')} {6 6} # Ticket #2276. Make sure leading zeros are inserted where appropriate. # -datetest 3.20 \ +datetest 3.40 \ {strftime('%d/%f/%H/%W/%j/%m/%M/%S/%Y','0421-01-02 03:04:05.006')} \ 02/05.006/03/00/002/01/04/05/0421 @@ -239,138 +259,122 @@ datetest 5.13 {datetime('1994-04-16 14:00:00Zulu')} NULL datetest 5.14 {datetime('1994-04-16 14:00:00Z +05:00')} NULL datetest 5.15 {datetime('1994-04-16 14:00:00 +05:00 Z')} NULL -# localtime->utc and utc->localtime conversions. These tests only work -# if the localtime is in the US Eastern Time (the time in Charlotte, NC -# and in New York.) -# -# On non-Vista Windows platform, '2006-03-31' is treated incorrectly as being -# in DST giving a 4 hour offset instead of 5. In 2007, DST was extended to -# start three weeks earlier (second Sunday in March) and end one week -# later (first Sunday in November). Older Windows systems apply this -# new rule incorrectly to dates prior to 2007. -# -# It might be argued that this is masking a problem on non-Vista Windows -# platform. A ticket has already been opened for this issue -# (http://www.sqlite.org/cvstrac/tktview?tn=2322). This is just to prevent -# more confusion/reports of the issue. -# - -# $tzoffset_old should be 5 if DST is working correctly. -set tzoffset_old [db one { - SELECT CAST(24*(julianday('2006-03-31') - - julianday('2006-03-31','localtime'))+0.5 - AS INT) -}] - -# $tzoffset_new should be 4 if DST is working correctly. -set tzoffset_new [db one { - SELECT CAST(24*(julianday('2007-03-31') - - julianday('2007-03-31','localtime'))+0.5 - AS INT) -}] - -# Warn about possibly broken Windows DST implementations. -if {$::tcl_platform(platform)=="windows" && $tzoffset_new==4 && $tzoffset_old==4} { - puts "******************************************************************" - puts "N.B.: The DST support provided by your current O/S seems to be" - puts "suspect in that it is reporting incorrect DST values for dates" - puts "prior to 2007. This is the known case for most (all?) non-Vista" - puts "Windows versions. Please see ticket #2322 for more information." - puts "******************************************************************" +# localtime->utc and utc->localtime conversions. +# +# Use SQLITE_TESTCTRL_LOCALTIME_FAULT=2 to set an alternative localtime_r() +# implementation that is not locale-dependent. The testing localtime_r() +# operates as follows: +# +# (1) Localtime is 30 minutes earlier than (west of) UTC on +# even days (counting from 1970-01-01) +# +# (2) Localtime is 30 minutes later than (east of) UTC on odd days. +# +# (3) The function fails for the specific date/time value +# of 2000-05-29 14:16:00 in order to test the ability of +# SQLite to deal with localtime_r() failures. +# +proc local_to_utc {tn utc local} { + do_execsql_test date-$tn "SELECT datetime('$utc','localtime')" [list $local] +} +proc utc_to_local {tn local utc} { + do_execsql_test date-$tn "SELECT datetime('$local','utc')" [list $utc] } -if {$tzoffset_new==4} { - datetest 6.1 {datetime('2000-10-29 05:59:00','localtime')}\ - {2000-10-29 01:59:00} - datetest 6.1.1 {datetime('2006-10-29 05:59:00','localtime')}\ - {2006-10-29 01:59:00} - datetest 6.1.2 {datetime('2007-11-04 05:59:00','localtime')}\ - {2007-11-04 01:59:00} - - # If the new and old DST rules seem to be working correctly... - if {$tzoffset_new==4 && $tzoffset_old==5} { - datetest 6.2 {datetime('2000-10-29 06:00:00','localtime')}\ - {2000-10-29 01:00:00} - datetest 6.2.1 {datetime('2006-10-29 06:00:00','localtime')}\ - {2006-10-29 01:00:00} - } - datetest 6.2.2 {datetime('2007-11-04 06:00:00','localtime')}\ - {2007-11-04 01:00:00} - - # If the new and old DST rules seem to be working correctly... - if {$tzoffset_new==4 && $tzoffset_old==5} { - datetest 6.3 {datetime('2000-04-02 06:59:00','localtime')}\ - {2000-04-02 01:59:00} - datetest 6.3.1 {datetime('2006-04-02 06:59:00','localtime')}\ - {2006-04-02 01:59:00} - } - datetest 6.3.2 {datetime('2007-03-11 07:00:00','localtime')}\ - {2007-03-11 03:00:00} - - datetest 6.4 {datetime('2000-04-02 07:00:00','localtime')}\ - {2000-04-02 03:00:00} - datetest 6.4.1 {datetime('2006-04-02 07:00:00','localtime')}\ - {2006-04-02 03:00:00} - datetest 6.4.2 {datetime('2007-03-11 07:00:00','localtime')}\ - {2007-03-11 03:00:00} - - datetest 6.5 {datetime('2000-10-29 01:59:00','utc')} {2000-10-29 05:59:00} - datetest 6.5.1 {datetime('2006-10-29 01:59:00','utc')} {2006-10-29 05:59:00} - datetest 6.5.2 {datetime('2007-11-04 01:59:00','utc')} {2007-11-04 05:59:00} - - # If the new and old DST rules seem to be working correctly... - if {$tzoffset_new==4 && $tzoffset_old==5} { - datetest 6.6 {datetime('2000-10-29 02:00:00','utc')} {2000-10-29 07:00:00} - datetest 6.6.1 {datetime('2006-10-29 02:00:00','utc')} {2006-10-29 07:00:00} - } - datetest 6.6.2 {datetime('2007-11-04 02:00:00','utc')} {2007-11-04 07:00:00} +sqlite3_test_control SQLITE_TESTCTRL_LOCALTIME_FAULT 2 +local_to_utc 6.1 {2000-10-29 12:00:00} {2000-10-29 12:30:00} +utc_to_local 6.2 {2000-10-29 12:30:00} {2000-10-29 12:00:00} +local_to_utc 6.3 {2000-10-30 12:00:00} {2000-10-30 11:30:00} +utc_to_local 6.4 {2000-10-30 11:30:00} {2000-10-30 12:00:00} - # If the new and old DST rules seem to be working correctly... - if {$tzoffset_new==4 && $tzoffset_old==5} { - datetest 6.7 {datetime('2000-04-02 01:59:00','utc')} {2000-04-02 06:59:00} - datetest 6.7.1 {datetime('2006-04-02 01:59:00','utc')} {2006-04-02 06:59:00} - } - datetest 6.7.2 {datetime('2007-03-11 01:59:00','utc')} {2007-03-11 06:59:00} - - datetest 6.8 {datetime('2000-04-02 02:00:00','utc')} {2000-04-02 06:00:00} - datetest 6.8.1 {datetime('2006-04-02 02:00:00','utc')} {2006-04-02 06:00:00} - datetest 6.8.2 {datetime('2007-03-11 02:00:00','utc')} {2007-03-11 06:00:00} - - # The 'utc' modifier is a no-op if the LHS is known to already be in UTC - datetest 6.9.1 {datetime('2015-12-23 12:00:00','utc')} {2015-12-23 17:00:00} - datetest 6.9.2 {datetime('2015-12-23 12:00:00z','utc')} {2015-12-23 12:00:00} - datetest 6.9.3 {datetime('2015-12-23 12:00:00-03:00','utc')} \ - {2015-12-23 15:00:00} - datetest 6.9.4 {datetime('2015-12-23 12:00:00','utc','utc','utc')} \ - {2015-12-23 17:00:00} - - - datetest 6.10 {datetime('2000-01-01 12:00:00','localtime')} \ - {2000-01-01 07:00:00} - datetest 6.11 {datetime('1969-01-01 12:00:00','localtime')} \ - {1969-01-01 07:00:00} - datetest 6.12 {datetime('2039-01-01 12:00:00','localtime')} \ - {2039-01-01 07:00:00} - datetest 6.13 {datetime('2000-07-01 12:00:00','localtime')} \ - {2000-07-01 08:00:00} - datetest 6.14 {datetime('1969-07-01 12:00:00','localtime')} \ - {1969-07-01 07:00:00} - datetest 6.15 {datetime('2039-07-01 12:00:00','localtime')} \ - {2039-07-01 07:00:00} - set sqlite_current_time \ - [db eval {SELECT strftime('%s','2000-07-01 12:34:56')}] - datetest 6.16 {datetime('now','localtime')} {2000-07-01 08:34:56} - datetest 6.17 {datetime('now','localtimex')} NULL - datetest 6.18 {datetime('now','localtim')} NULL - set sqlite_current_time 0 -} +local_to_utc 6.5 {2000-10-28 23:59:59} {2000-10-28 23:29:59} +local_to_utc 6.6 {2000-10-29 00:00:00} {2000-10-29 00:30:00} + +# The previous two cases establish that no such localtime as +# 2000-10-29 00:10:00 exists. Verify that we get a reasonable +# answer if we try to convert this non-existant localtime to utc? +# +utc_to_local 6.7 {2000-10-29 00:10:00} {2000-10-28 23:40:00} + +local_to_utc 6.8 {2022-02-10 23:59:59} {2022-02-11 00:29:59} +local_to_utc 6.9 {2022-02-11 00:00:00} {2022-02-10 23:30:00} +local_to_utc 6.10 {2022-02-10 23:45:00} {2022-02-11 00:15:00} +local_to_utc 6.11 {2022-02-11 00:45:00} {2022-02-11 00:15:00} -# These two are a bit of a scam. They are added to ensure that 100% of -# the date.c file is covered by testing, even when the time-zone -# is not -0400 (the condition for running of the block of tests above). +# The previous two cases show that two different UTC values give +# the same localtime of 2022-02-11 00:15:00. When converting from +# that localtime back to UTC, we should get one or the other of +# the two UTC values. # -datetest 6.19 {datetime('2039-07-01 12:00:00','localtime',null)} NULL -datetest 6.20 {datetime('2039-07-01 12:00:00','utc',null)} NULL +utc_to_local 6.12 {2022-02-11 00:15:00} {2022-02-11 00:45:00} + +# If localtime_r() fails, the datetime() SQL function should raise an error +# +do_catchsql_test date-6.20 { + SELECT datetime('2000-05-29 14:16:00','localtime'); +} {1 {local time unavailable}} + +# Modifiers work for dates that are way out of band for localtime_r() +# +local_to_utc 6.21 {1800-10-29 12:00:00} {1800-10-29 12:30:00} +utc_to_local 6.22 {1800-10-29 12:30:00} {1800-10-29 12:00:00} +local_to_utc 6.23 {3000-10-30 12:00:00} {3000-10-30 11:30:00} +utc_to_local 6.24 {3000-10-30 11:30:00} {3000-10-30 12:00:00} + +# If the time is specified to be ZULU, or if it has an explicit +# timezone extension, then the time will already be UTC and subsequent +# 'utc' modifiers are no-ops. +# +# Forum post 2025-09-17T10:12:14 - The "+00:00" suffix should work like "Z" +# +do_execsql_test date-6.25.1 { + SELECT datetime('2000-10-29 12:00Z','utc','utc'); +} {{2000-10-29 12:00:00}} +do_execsql_test date-6.25.2 { + SELECT datetime('2000-10-29 12:00 +00:00','utc','utc'); +} {{2000-10-29 12:00:00}} +do_execsql_test date-6.25.3 { + SELECT datetime('2000-10-29 12:00+00:00','utc','utc'); +} {{2000-10-29 12:00:00}} +do_execsql_test date-6.25.4 { + SELECT datetime('2000-10-29 12:00:00+00:00','utc','utc'); +} {{2000-10-29 12:00:00}} +do_execsql_test date-6.25.5 { + SELECT datetime('2000-10-29 12:00 -00:00','utc','utc'); +} {{2000-10-29 12:00:00}} +do_execsql_test date-6.25.6 { + SELECT datetime('2000-10-29 12:00-00:00','utc','utc'); +} {{2000-10-29 12:00:00}} +do_execsql_test date-6.25.7 { + SELECT datetime('2000-10-29 12:00:00-00:00','utc','utc'); +} {{2000-10-29 12:00:00}} +do_execsql_test date-6.26 { + SELECT datetime('2000-10-29 12:00:00+05:00'); +} {{2000-10-29 07:00:00}} +do_execsql_test date-6.27 { + SELECT datetime('2000-10-29 12:00:00+05:00', 'utc'); +} {{2000-10-29 07:00:00}} + +# Multiple back-and-forth UTC to LOCAL to UTC... +do_execsql_test date-6.28 { + SELECT datetime('2000-10-29 12:00:00Z', 'localtime'); +} {{2000-10-29 12:30:00}} +do_execsql_test date-6.29 { + SELECT datetime('2000-10-29 12:00:00Z', 'utc', 'localtime'); +} {{2000-10-29 12:30:00}} +do_execsql_test date-6.30 { + SELECT datetime('2000-10-29 12:00:00Z', 'utc', 'localtime', 'utc'); +} {{2000-10-29 12:00:00}} +do_execsql_test date-6.31 { + SELECT datetime('2000-10-29 12:00:00Z', 'utc','localtime','utc','localtime'); +} {{2000-10-29 12:30:00}} +do_execsql_test date-6.32 { + SELECT datetime('2000-10-29 12:00:00Z', 'localtime','localtime'); +} {{2000-10-29 12:30:00}} + + +# Restore the use of the OS localtime_r() before going on... +sqlite3_test_control SQLITE_TESTCTRL_LOCALTIME_FAULT 0 # Date-time functions that contain NULL arguments return a NULL # result. @@ -502,6 +506,9 @@ datetest 13.31 {date('2001-01-01','+1.5 years')} {2002-07-02} datetest 13.32 {date('2002-01-01','+1.5 years')} {2003-07-02} datetest 13.33 {date('2002-01-01','-1.5 years')} {2000-07-02} datetest 13.34 {date('2001-01-01','-1.5 years')} {1999-07-02} +datetest 13.35 {date('2023-02-28')} {2023-02-28} +datetest 13.36 {date('2023-02-29')} {2023-03-01} +datetest 13.37 {date('2023-04-31')} {2023-05-01} # Test for issues reported by BareFeet (list.sql at tandb.com.au) # on mailing list on 2008-06-12. @@ -560,4 +567,117 @@ do_test date-15.2 { } } {1} +# Tests of extreme values in date/time functions. Run with UBSan or the +# equivalent to verify no signed interger overflow warnings. +# +datetest 16.1 {date(147483649)} NULL +datetest 16.2 {datetime(0)} {-4713-11-24 12:00:00} +datetest 16.3 {datetime(5373484.49999999)} {9999-12-31 23:59:59} +datetest 16.4 {julianday('-4713-11-24 12:00:00')} 0.0 +datetest 16.5 {julianday('9999-12-31 23:59:59.999')} 5373484.49999999 +datetest 16.6 {datetime(0,'+464269060799 seconds')} {9999-12-31 23:59:59} +datetest 16.7 {datetime(0,'+464269060800 seconds')} NULL +datetest 16.8 {datetime(0,'+7737817679 minutes')} {9999-12-31 23:59:00} +datetest 16.9 {datetime(0,'+7737817680 minutes')} NULL +datetest 16.10 {datetime(0,'+128963627 hours')} {9999-12-31 23:00:00} +datetest 16.11 {datetime(0,'+128963628 hours')} NULL +datetest 16.12 {datetime(0,'+5373484 days')} {9999-12-31 12:00:00} +datetest 16.13 {datetime(0,'+5373485 days')} NULL +datetest 16.14 {datetime(0,'+176545 months')} {9999-12-24 12:00:00} +datetest 16.15 {datetime(0,'+176546 months')} NULL +datetest 16.16 {datetime(0,'+14712 years')} {9999-11-24 12:00:00} +datetest 16.17 {datetime(0,'+14713 years')} NULL +datetest 16.20 {datetime(5373484.4999999,'-464269060799 seconds')} \ + {-4713-11-24 12:00:00} +datetest 16.21 {datetime(5373484,'-464269060800 seconds')} NULL +datetest 16.22 {datetime(5373484.4999999,'-7737817679 minutes')} \ + {-4713-11-24 12:00:59} +datetest 16.23 {datetime(5373484,'-7737817680 minutes')} NULL +datetest 16.24 {datetime(5373484.4999999,'-128963627 hours')} \ + {-4713-11-24 12:59:59} +datetest 16.25 {datetime(5373484,'-128963628 hours')} NULL +datetest 16.26 {datetime(5373484,'-5373484 days')} {-4713-11-24 12:00:00} +datetest 16.27 {datetime(5373484,'-5373485 days')} NULL +datetest 16.28 {datetime(5373484,'-176545 months')} {-4713-12-01 12:00:00} +datetest 16.29 {datetime(5373484,'-176546 months')} NULL +datetest 16.30 {datetime(5373484,'-14712 years')} {-4713-12-31 12:00:00} +datetest 16.31 {datetime(5373484,'-14713 years')} NULL + +# 2017-03-02: Wrong 'start of day' computation. +# https://sqlite.org/src/info/6097cb92745327a1 +# +datetest 17.1 {datetime(2457754, 'start of day')} {2016-12-31 00:00:00} +datetest 17.2 {datetime(2457828)} {2017-03-15 12:00:00} +datetest 17.3 {datetime(2457828,'start of day')} {2017-03-15 00:00:00} +datetest 17.4 {datetime(2457828,'start of month')} {2017-03-01 00:00:00} +datetest 17.5 {datetime(2457828,'start of year')} {2017-01-01 00:00:00} +datetest 17.6 {datetime(37,'start of year')} NULL +datetest 17.7 {datetime(38,'start of year')} {-4712-01-01 00:00:00} + +# 2022-03-04 https://sqlite.org/forum/forumpost/2ffbaa2c3fd7fb82 +# The 'localtime' modifier should preserve fractional seconds. +# +datetest 18.1 {strftime('%f',1.234,'unixepoch','localtime')} {01.234} + +# 2023-04 The 'subsecond' (or 'subsec') modifier alters resolutions +# to at least milliseconds. Added for release 3.42.0 . +datetest 18.2 {unixepoch('1970-01-01T00:00:00.1', 'subsec')} {0.1} +datetest 18.3 {unixepoch('1970-01-01T00:00:00.2', 'subsecond')} {0.2} +datetest 18.4 {julianday('-4713-11-24 13:40:48.864', 'subsec')} {0.07001} +datetest 18.5 {typeof(unixepoch('now', 'subsecond'))} {real} + +# 2024-03-03 the 'ceiling' and 'floor' operators. +# +datetest 19.1 {date('2000-01-31','floor')} {2000-01-31} +datetest 19.2a {date('2000-02-31','floor')} {2000-02-29} +datetest 19.2b {date('1999-02-31','floor')} {1999-02-28} +datetest 19.2c {date('1900-02-31','floor')} {1900-02-28} +datetest 19.3 {date('2000-03-31','floor')} {2000-03-31} +datetest 19.4 {date('2000-04-31','floor')} {2000-04-30} +datetest 19.5 {date('2000-05-31','floor')} {2000-05-31} +datetest 19.6 {date('2000-06-31','floor')} {2000-06-30} +datetest 19.7 {date('2000-07-31','floor')} {2000-07-31} +datetest 19.8 {date('2000-08-31','floor')} {2000-08-31} +datetest 19.9 {date('2000-09-31','floor')} {2000-09-30} +datetest 19.10 {date('2000-10-31','floor')} {2000-10-31} +datetest 19.11 {date('2000-11-31','floor')} {2000-11-30} +datetest 19.12 {date('2000-12-31','floor')} {2000-12-31} +datetest 19.21 {date('2000-01-31','ceiling')} {2000-01-31} +datetest 19.22a {date('2000-02-31','ceiling')} {2000-03-02} +datetest 19.22b {date('1999-02-31','ceiling')} {1999-03-03} +datetest 19.22c {date('1900-02-31','ceiling')} {1900-03-03} +datetest 19.23 {date('2000-03-31','ceiling')} {2000-03-31} +datetest 19.24 {date('2000-04-31','ceiling')} {2000-05-01} +datetest 19.25 {date('2000-05-31','ceiling')} {2000-05-31} +datetest 19.26 {date('2000-06-31','ceiling')} {2000-07-01} +datetest 19.27 {date('2000-07-31','ceiling')} {2000-07-31} +datetest 19.28 {date('2000-08-31','ceiling')} {2000-08-31} +datetest 19.29 {date('2000-09-31','ceiling')} {2000-10-01} +datetest 19.30 {date('2000-10-31','ceiling')} {2000-10-31} +datetest 19.31 {date('2000-11-31','ceiling')} {2000-12-01} +datetest 19.32 {date('2000-12-31','ceiling')} {2000-12-31} +datetest 19.40 {date('2024-01-31','+1 month','ceiling')} {2024-03-02} +datetest 19.41 {date('2024-01-31','+1 month','floor')} {2024-02-29} +datetest 19.42 {date('2023-01-31','+1 month','ceiling')} {2023-03-03} +datetest 19.43 {date('2023-01-31','+1 month','floor')} {2023-02-28} +datetest 19.44 {date('2024-02-29','+1 year','ceiling')} {2025-03-01} +datetest 19.45 {date('2024-02-29','+1 year','floor')} {2025-02-28} +datetest 19.46 {date('2024-02-29','-110 years','ceiling')} {1914-03-01} +datetest 19.47 {date('2024-02-29','-110 years','floor')} {1914-02-28} +datetest 19.48 {date('2024-02-29','-0110-00-00','floor')} {1914-02-28} +datetest 19.49 {date('2024-02-29','-0110-00-00','ceiling')} {1914-03-01} +datetest 19.50 {date('2000-08-31','+0023-06-00','floor')} {2024-02-29} +datetest 19.51 {date('2000-08-31','+0022-06-00','floor')} {2023-02-28} +datetest 19.52 {date('2000-08-31','+0023-06-00','ceiling')} {2024-03-02} +datetest 19.53 {date('2000-08-31','+0022-06-00','ceiling')} {2023-03-03} + +# 2025-01-21 +# https://sqlite.org/forum/forumpost/766a2c9231 +# +datetest 20.1 {datetime('2024-12-31 23:59:59.9990')} {2024-12-31 23:59:59} +datetest 20.2 {datetime('2024-12-31 23:59:59.9999999999999')} \ + {2024-12-31 23:59:59} +datetest 20.3 {datetime('2024-12-31 23:59:59.9995')} {2024-12-31 23:59:59} +datetest 20.4 {datetime('2024-12-31 23:59:58.9995')} {2024-12-31 23:59:58} + finish_test diff --git a/test/date2.test b/test/date2.test new file mode 100644 index 0000000000..a16e25c448 --- /dev/null +++ b/test/date2.test @@ -0,0 +1,176 @@ +# 2017-07-20 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is testing date and time functions used in +# check constraints and index expressions. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +# Skip this whole file if date and time functions are omitted +# at compile-time +# +ifcapable {!datetime} { + finish_test + return +} + +do_execsql_test date2-100 { + CREATE TABLE t1(x, y, CHECK( date(x) BETWEEN '2017-07-01' AND '2017-07-31' )); + INSERT INTO t1(x,y) VALUES('2017-07-20','one'); +} {} +do_catchsql_test date2-110 { + INSERT INTO t1(x,y) VALUES('now','two'); +} {1 {non-deterministic use of date() in a CHECK constraint}} +do_execsql_test date2-120 { + SELECT * FROM t1; +} {2017-07-20 one} +do_catchsql_test date2-130 { + INSERT INTO t1(x,y) VALUES('2017-08-01','two'); +} {1 {CHECK constraint failed: date(x) BETWEEN '2017-07-01' AND '2017-07-31'}} + +# 2021-03-16 Forum post https://sqlite.org/forum/forumpost/464afd4086 +do_catchsql_test date2-140 { + DROP TABLE t1; + CREATE TABLE t1(x, y, z AS (date())); + INSERT INTO t1(x,y) VALUES(1,2); +} {1 {non-deterministic use of date() in a generated column}} + +do_execsql_test date2-200 { + CREATE TABLE t2(x,y); + INSERT INTO t2(x,y) VALUES(1, '2017-07-20'), (2, 'xyzzy'); + CREATE INDEX t2y ON t2(date(y)); +} +do_catchsql_test date2-210 { + INSERT INTO t2(x,y) VALUES(3, 'now'); +} {1 {non-deterministic use of date() in an index}} +do_execsql_test date2-220 { + SELECT x, y FROM t2 ORDER BY x; +} {1 2017-07-20 2 xyzzy} + +do_execsql_test date2-300 { + CREATE TABLE t3(a INTEGER PRIMARY KEY,b); + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<1000) + INSERT INTO t3(a,b) SELECT x, julianday('2017-07-01')+x FROM c; + UPDATE t3 SET b='now' WHERE a=500; +} +do_catchsql_test date2-310 { + CREATE INDEX t3b1 ON t3(datetime(b)); +} {1 {non-deterministic use of datetime() in an index}} +do_catchsql_test date2-320 { + CREATE INDEX t3b1 ON t3(datetime(b)) WHERE typeof(b)='real'; +} {0 {}} +do_execsql_test date2-330 { + EXPLAIN QUERY PLAN + SELECT a FROM t3 + WHERE typeof(b)='real' + AND datetime(b) BETWEEN '2017-07-04' AND '2017-07-08'; +} {/USING INDEX t3b/} +do_execsql_test date2-331 { + SELECT a FROM t3 + WHERE typeof(b)='real' + AND datetime(b) BETWEEN '2017-07-04' AND '2017-07-08' + ORDER BY a; +} {3 4 5 6} + +do_execsql_test date2-400 { + CREATE TABLE t4(a INTEGER PRIMARY KEY,b); + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<1000) + INSERT INTO t4(a,b) SELECT x, julianday('2017-07-01')+x FROM c; + UPDATE t4 SET b='now' WHERE a=500; +} +do_catchsql_test date2-410 { + CREATE INDEX t4b1 ON t4(b) + WHERE date(b) BETWEEN '2017-06-01' AND '2017-08-31'; +} {1 {non-deterministic use of date() in an index}} +do_execsql_test date2-420 { + DELETE FROM t4 WHERE a=500; + CREATE INDEX t4b1 ON t4(b) + WHERE date(b) BETWEEN '2017-06-01' AND '2017-08-31'; +} +do_catchsql_test date2-430 { + INSERT INTO t4(a,b) VALUES(9999,'now'); +} {1 {non-deterministic use of date() in an index}} + +do_execsql_test date2-500 { + CREATE TABLE mods(x); + INSERT INTO mods(x) VALUES + ('+10 days'), + ('-10 days'), + ('+10 hours'), + ('-10 hours'), + ('+10 minutes'), + ('-10 minutes'), + ('+10 seconds'), + ('-10 seconds'), + ('+10 months'), + ('-10 months'), + ('+10 years'), + ('-10 years'), + ('start of month'), + ('start of year'), + ('start of day'), + ('weekday 1'), + ('unixepoch'); + CREATE TABLE t5(y,m); + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<5) + INSERT INTO t5(y,m) SELECT julianday('2017-07-01')+c.x, mods.x FROM c, mods; + CREATE INDEX t5x1 on t5(y) WHERE datetime(y,m) IS NOT NULL; +} +do_catchsql_test date2-510 { + INSERT INTO t5(y,m) VALUES('2017-07-20','localtime'); +} {1 {non-deterministic use of datetime() in an index}} +do_catchsql_test date2-520 { + INSERT INTO t5(y,m) VALUES('2017-07-20','utc'); +} {1 {non-deterministic use of datetime() in an index}} + +# 2019-10-30 Ticket 830277d9db6c3ba1 +# +do_catchsql_test date2-600 { + CREATE TABLE t600(a REAL CHECK( a<julianday('now') )); + INSERT INTO t600(a) VALUES(1.0); +} {1 {non-deterministic use of julianday() in a CHECK constraint}} +do_catchsql_test date2-601 { + CREATE TABLE t601(a REAL, b TEXT, CHECK( a<julianday(b) )); + INSERT INTO t601(a,b) VALUES(1.0, '1970-01-01'); +} {0 {}} +do_catchsql_test date2-602 { + INSERT INTO t601(a,b) VALUES(1e100, '1970-01-01'); +} {1 {CHECK constraint failed: a<julianday(b)}} +do_catchsql_test date2-603 { + INSERT INTO t601(a,b) VALUES(10, 'now'); +} {1 {non-deterministic use of julianday() in a CHECK constraint}} +do_catchsql_test date2-604 { + INSERT INTO t600(a) VALUES(julianday('now')+10); +} {1 {non-deterministic use of julianday() in a CHECK constraint}} + + +do_catchsql_test date2-610 { + CREATE TABLE t610(a,b); + CREATE INDEX t610x1 ON t610(julianday('now')+b); + INSERT INTO t610(a,b) VALUES(123,456); +} {1 {non-deterministic use of julianday() in an index}} +do_catchsql_test date2-611 { + CREATE TABLE t611(a,b); + CREATE INDEX t611x1 ON t611(julianday(a)+b); + INSERT INTO t611(a,b) VALUES('1970-01-01',10.0); +} {0 {}} +do_catchsql_test date2-612 { + INSERT INTO t611(a,b) VALUES('now',10.0); +} {1 {non-deterministic use of julianday() in an index}} + +do_catchsql_test date3-620 { + CREATE TABLE t620(a, b AS (a+julianday('now'))); + INSERT INTO t620 VALUES(10); +} {1 {non-deterministic use of julianday() in a generated column}} + +finish_test diff --git a/test/date3.test b/test/date3.test new file mode 100644 index 0000000000..1798a3478a --- /dev/null +++ b/test/date3.test @@ -0,0 +1,149 @@ +# 2022-01-27 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is testing date and time functions. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +# Skip this whole file if date and time functions are omitted +# at compile-time +# +ifcapable {!datetime} { + finish_test + return +} + +proc datetest {tnum expr result} { + do_test date3-$tnum [subst { + execsql "SELECT coalesce($expr,'NULL')" + }] [list $result] +} +set tcl_precision 15 + +# EVIDENCE-OF: R-45708-63005 unixepoch(time-value, modifier, modifier, +# ...) +# +datetest 1.1 {unixepoch('1970-01-01')} {0} +datetest 1.2 {unixepoch('1969-12-31 23:59:59')} {-1} +datetest 1.3 {unixepoch('2106-02-07 06:28:15')} {4294967295} +datetest 1.4 {unixepoch('2106-02-07 06:28:16')} {4294967296} +datetest 1.5 {unixepoch('9999-12-31 23:59:59')} {253402300799} +datetest 1.6 {unixepoch('0000-01-01 00:00:00')} {-62167219200} + +# EVIDENCE-OF: R-30877-63179 The unixepoch() function returns a unix +# timestamp - the number of seconds since 1970-01-01 00:00:00 UTC. +# +for {set i 1} {$i<=100} {incr i} { + set x [expr {int(rand()*0xfffffffff)-0xffffffff}] + datetest 1.7.$i "unixepoch($x,'unixepoch')==$x" {1} +} + +# EVIDENCE-OF: R-62992-54137 The unixepoch() always returns an integer, +# even if the input time-value has millisecond precision. +# +datetest 1.8 {unixepoch('2022-01-27 12:59:28.052')} {1643288368} + +# EVIDENCE-OF: R-05412-24332 If the time-value is numeric (the +# DDDDDDDDDD format) then the 'auto' modifier causes the time-value to +# interpreted as either a julian day number or a unix timestamp, +# depending on its magnitude. +# +# EVIDENCE-OF: R-56763-40111 If the value is between 0.0 and +# 5373484.499999, then it is interpreted as a julian day number +# (corresponding to dates between -4713-11-24 12:00:00 and 9999-12-31 +# 23:59:59, inclusive). +# +# EVIDENCE-OF: R-07289-49223 For numeric values outside of the range of +# valid julian day numbers, but within the range of -210866760000 to +# 253402300799, the 'auto' modifier causes the value to be interpreted +# as a unix timestamp. +# +# EVIDENCE-OF: R-20795-34947 Other numeric values are out of range and +# cause a NULL return. +# +foreach {tn jd date} { + 2.1 0.0 {-4713-11-24 12:00:00} + 2.2 5373484.4999999 {9999-12-31 23:59:59} + 2.3 2440587.5 {1970-01-01 00:00:00} + 2.4 2440587.49998843 {1969-12-31 23:59:59} + 2.5 2440615.7475463 {1970-01-29 05:56:28} + + 2.10 -1 {1969-12-31 23:59:59} + 2.11 5373485 {1970-03-04 04:38:05} + 2.12 -210866760000 {-4713-11-24 12:00:00} + 2.13 253402300799 {9999-12-31 23:59:59} + + 2.20 -210866760001 {NULL} + 2.21 253402300800 {NULL} +} { + datetest $tn "datetime($jd,'auto')" $date +} + +# EVIDENCE-OF: R-38886-35357 The 'auto' modifier is a no-op for text +# time-values. +# +datetest 2.30 {date('2022-01-29','auto')==date('2022-01-29')} {1} + +# EVIDENCE-OF: R-53132-26856 The 'auto' modifier can be used to work +# with date/time values even in cases where it is not known if the +# julian day number or unix timestamp formats are in use. +# +do_execsql_test date3-2.40 { + WITH tx(timeval,datetime) AS ( + VALUES('2022-01-27 13:15:44','2022-01-27 13:15:44'), + (2459607.05260275,'2022-01-27 13:15:44'), + (1643289344,'2022-01-27 13:15:44') + ) + SELECT datetime(timeval,'auto') == datetime FROM tx; +} {1 1 1} + +# EVIDENCE-OF: R-49255-55373 The "unixepoch" modifier (11) only works if +# it immediately follows a time value in the DDDDDDDDDD format. +# +# EVIDENCE-OF: R-23075-39245 This modifier causes the DDDDDDDDDD to be +# interpreted not as a Julian day number as it normally would be, but as +# Unix Time - the number of seconds since 1970. +# +datetest 3.1 {datetime(2459607.05,'+1 hour','unixepoch')} {NULL} +datetest 3.2 {datetime(2459607.05,'unixepoch','+1 hour')} {1970-01-29 12:13:27} + +# EVIDENCE-OF: R-21150-52363 The "julianday" modifier must immediately +# follow the initial time-value which must be of the form DDDDDDDDD. +# +# EVIDENCE-OF: R-31176-64601 Any other use of the 'julianday' modifier +# is an error and causes the function to return NULL. +# +# EVIDENCE-OF: R-32483-36353 The 'julianday' modifier forces the +# time-value number to be interpreted as a julian-day number. +# +# EVIDENCE-OF: R-25859-20124 The only difference is that adding +# 'julianday' forces the DDDDDDDDD time-value format, and causes a NULL +# to be returned if any other time-value format is used. +# +datetest 4.1 {datetime(2459607,'julianday')} {2022-01-27 12:00:00} +datetest 4.2 {datetime(2459607,'+1 hour','julianday')} {NULL} +datetest 4.3 {datetime('2022-01-27','julianday')} {NULL} + + + +# EVIDENCE-OF: R-33431-18865 Unix timestamps for the first 63 days of +# 1970 will be interpreted as julian day numbers. +# +do_execsql_test date3-5.0 { + WITH inc(x) AS (VALUES(-10) UNION ALL SELECT x+1 FROM inc WHERE x<100) + SELECT count(*) FROM inc + WHERE datetime('1970-01-01',format('%+d days',x)) + <> datetime(unixepoch('1970-01-01',format('%+d days',x)),'auto'); +} {63} + +finish_test diff --git a/test/date4.test b/test/date4.test new file mode 100644 index 0000000000..4e936b71c3 --- /dev/null +++ b/test/date4.test @@ -0,0 +1,44 @@ +# 2023-08-29 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# Test cases for the strftime() SQL function. Comparisons against the +# C-library strftime() function. + +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +# Skip this whole file if date and time functions are omitted +# at compile-time +# +ifcapable {!datetime} { + finish_test + return +} + +if {$tcl_platform(os)=="Linux"} { + if {"" eq [strftime {%P} 1]} { + # This is probably musl libc, which does not support + # %k, %l, %P + set FMT {%d,%e,%F,%H,%I,%j,%m,%M,%u,%w,%W,%Y,%%,%p,%U,%V,%G,%g} + } else { + set FMT {%d,%e,%F,%H,%k,%I,%l,%j,%m,%M,%u,%w,%W,%Y,%%,%P,%p,%U,%V,%G,%g} + } +} else { + set FMT {%d,%e,%F,%H,%I,%j,%p,%R,%u,%w,%W,%%} +} +for {set i 0} {$i<=24858} {incr i} { + set TS [expr {$i*86390}] + do_execsql_test date4-$i { + SELECT strftime($::FMT,$::TS,'unixepoch'); + } [list [strftime $FMT $TS]] +} + +finish_test diff --git a/test/date5.test b/test/date5.test new file mode 100644 index 0000000000..688f84d0f1 --- /dev/null +++ b/test/date5.test @@ -0,0 +1,86 @@ +# 2024-08-19 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# https://sqlite.org/forum/forumpost/eaa0a09786c6368b +# +# Apparently SQLite has been miscomputing leap-year dates before +# the year 0400. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +# Skip this whole file if date and time functions are omitted +# at compile-time +# +ifcapable {!datetime} { + finish_test + return +} + +# Data sources: +# 1-10 https://ssd.jpl.nasa.gov/tools/jdc/#/cd +# 11 Jean Meeus, Astronomical Algorithms, ISBN 0-943396-61-1, p.59 +# 12 https://en.wikipedia.org/wiki/Julian_day +# +# ID YEAR MONTH DAY JD +set date5data { + 1 2024 2 29 2460369.5 + 2 2024 3 1 2460370.5 + 3 2023 2 28 2460003.5 + 4 2023 3 1 2460004.5 + 5 2000 2 29 2451603.5 + 6 2000 3 1 2451604.5 + 7 1900 2 28 2415078.5 + 8 1900 3 1 2415079.5 + 9 1712 2 29 2346413.5 + 10 1712 3 1 2346414.5 + 11 1977 4 26 2443259.5 + 12 2013 1 1 2456293.5 +} + +foreach {id y m d jd} $date5data { + set date [format %04d-%02d-%02d $y $m $d] + do_execsql_test date5-jd$jd { + SELECT date($::jd); + } $date + do_execsql_test date5-cal/$date { + SELECT julianday($::date); + } $jd + for {set i 1} {$y+400*$i<=9999} {incr i} { + set y2 [expr {$y+400*$i}] + set date2 [format %04d-%02d-%02d $y2 $m $d] + set jd2 [expr {$jd+146097*$i}] + do_execsql_test date5-jd$jd2 { + SELECT date($::jd2); + } $date2 + do_execsql_test date5-cal/$date2 { + SELECT julianday($::date2); + } $jd2 + } + for {set i 1} {$y-400*$i>=-4712} {incr i} { + set y2 [expr {$y-400*$i}] + if {$y2<0} { + set date2 [format -%04d-%02d-%02d [expr {-$y2}] $m $d] + } else { + set date2 [format %04d-%02d-%02d $y2 $m $d] + } + set jd2 [expr {$jd-146097*$i}] + do_execsql_test date5-jd$jd2 { + SELECT date($::jd2); + } $date2 + do_execsql_test date5-cal/$date2 { + SELECT julianday($::date2); + } $jd2 + } +} + +finish_test diff --git a/test/dbdata.test b/test/dbdata.test new file mode 100644 index 0000000000..5b1150c78f --- /dev/null +++ b/test/dbdata.test @@ -0,0 +1,115 @@ +# 2019-04-11 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is testing the sqlite_dbpage virtual table. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix dbdata + +ifcapable !vtab||!compound { + finish_test + return +} +if { [catch { db enable_load_extension 1 }] + || [catch { db eval { SELECT load_extension('../dbdata') } }] +} { + finish_test + return +} + +do_execsql_test 1.0 { + CREATE TABLE T1(a, b); + INSERT INTO t1(rowid, a ,b) VALUES(5, 'v', 'five'); + INSERT INTO t1(rowid, a, b) VALUES(10, 'x', 'ten'); +} + +do_execsql_test 1.1 { + SELECT pgno, cell, field, quote(value) FROM sqlite_dbdata WHERE pgno=2; +} { + 2 0 -1 5 + 2 0 0 'v' + 2 0 1 'five' + 2 1 -1 10 + 2 1 0 'x' + 2 1 1 'ten' +} + +breakpoint +do_execsql_test 1.2 { + SELECT pgno, cell, field, quote(value) FROM sqlite_dbdata; +} { + 1 0 -1 1 + 1 0 0 'table' + 1 0 1 'T1' + 1 0 2 'T1' + 1 0 3 2 + 1 0 4 {'CREATE TABLE T1(a, b)'} + 2 0 -1 5 + 2 0 0 'v' + 2 0 1 'five' + 2 1 -1 10 + 2 1 0 'x' + 2 1 1 'ten' +} + +set big [string repeat big 2000] +do_execsql_test 1.3 { + INSERT INTO t1 VALUES(NULL, $big); + SELECT value FROM sqlite_dbdata WHERE pgno=2 AND cell=2 AND field=1; +} $big + +do_execsql_test 1.4 { + DELETE FROM t1; + INSERT INTO t1 VALUES(NULL, randomblob(5050)); +} +do_test 1.5 { + execsql { + SELECT quote(value) FROM sqlite_dbdata WHERE pgno=2 AND cell=0 AND field=1; + } +} [db one {SELECT quote(b) FROM t1}] + +#------------------------------------------------------------------------- +reset_db +db enable_load_extension 1 +db eval { SELECT load_extension('../dbdata') } + +do_execsql_test 2.0 { + CREATE TABLE t1(a); + CREATE INDEX i1 ON t1(a); + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<10 + ) + INSERT INTO t1 SELECT randomblob(900) FROM s; +} + +do_execsql_test 2.1 { + SELECT * FROM sqlite_dbptr WHERE pgno=2; +} { + 2 25 2 6 2 7 2 9 2 11 2 13 2 15 2 17 2 19 2 21 +} + +do_execsql_test 2.2 { + SELECT * FROM sqlite_dbptr WHERE pgno=3; +} { + 3 24 3 23 +} + +do_execsql_test 2.3 { + SELECT * FROM sqlite_dbptr +} { + 2 25 2 6 2 7 2 9 2 11 2 13 2 15 2 17 2 19 2 21 + 3 24 3 23 +} + + +finish_test diff --git a/test/dbfuzz.c b/test/dbfuzz.c new file mode 100644 index 0000000000..1587bacbbd --- /dev/null +++ b/test/dbfuzz.c @@ -0,0 +1,754 @@ +/* +** 2016-12-17 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** This program is designed for fuzz-testing SQLite database files. +** +** This program reads fuzzed database files from the disk files named +** on the command-line. Each database is loaded into an in-memory +** filesystem so that the original database file is unmolested. +** +** The fuzzed database is then opened, and series of SQL statements +** are run against the database to ensure that SQLite can safely handle +** the fuzzed database. +*/ +#include <assert.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <stdarg.h> +#include <ctype.h> +#define ISSPACE(X) isspace((unsigned char)(X)) +#define ISDIGIT(X) isdigit((unsigned char)(X)) +#include "sqlite3.h" +#ifdef __unix__ +# include <signal.h> +# include <unistd.h> +#endif + +/* +** Print sketchy documentation for this utility program +*/ +static void showHelp(const char *zArgv0){ + printf("Usage: %s [options] DATABASE ...\n", zArgv0); + printf( +"Read databases into an in-memory filesystem. Run test SQL as specified\n" +"by command-line arguments or from\n" +"\n" +" SELECT group_concat(sql) FROM autoexec;\n" +"\n" +"Options:\n" +" --help Show this help text\n" +" -q|--quiet Reduced output\n" +" --limit-mem N Limit memory used by test SQLite instances to N bytes\n" +" --limit-vdbe Panic if any test runs for more than 100,000 cycles\n" +" --no-lookaside Disable the lookaside memory allocator\n" +" --timeout N Timeout after N seconds.\n" +" --trace Show the results of each SQL command\n" +" -v|--verbose Increased output. Repeat for more output.\n" + ); + exit(0); +} + +/* +** Print an error message and quit. +*/ +static void fatalError(const char *zFormat, ...){ + va_list ap; + va_start(ap, zFormat); + vfprintf(stderr, zFormat, ap); + va_end(ap); + fprintf(stderr, "\n"); + exit(1); +} + +/* +** Files in the virtual file system. +*/ +typedef struct VFile VFile; +typedef struct VHandle VHandle; +struct VFile { + char *zFilename; /* Filename. NULL for delete-on-close. From malloc() */ + int sz; /* Size of the file in bytes */ + int nRef; /* Number of references to this file */ + unsigned char *a; /* Content of the file. From malloc() */ +}; +struct VHandle { + sqlite3_file base; /* Base class. Must be first */ + VFile *pVFile; /* The underlying file */ +}; + +/* +** Maximum number of files in the in-memory virtual filesystem. +*/ +#define MX_FILE 10 + +/* +** Maximum allowed file size +*/ +#define MX_FILE_SZ 1000000 + +/* +** All global variables are gathered into the "g" singleton. +*/ +static struct GlobalVars { + VFile aFile[MX_FILE]; /* The virtual filesystem */ +} g; + + +/* +** Initialize the virtual file system. +*/ +static void formatVfs(void){ + int i; + for(i=0; i<MX_FILE; i++){ + g.aFile[i].sz = -1; + g.aFile[i].zFilename = 0; + g.aFile[i].a = 0; + g.aFile[i].nRef = 0; + } +} + + +/* +** Erase all information in the virtual file system. +*/ +static void reformatVfs(void){ + int i; + for(i=0; i<MX_FILE; i++){ + if( g.aFile[i].sz<0 ) continue; + if( g.aFile[i].zFilename ){ + free(g.aFile[i].zFilename); + g.aFile[i].zFilename = 0; + } + if( g.aFile[i].nRef>0 ){ + fatalError("file %d still open. nRef=%d", i, g.aFile[i].nRef); + } + g.aFile[i].sz = -1; + free(g.aFile[i].a); + g.aFile[i].a = 0; + g.aFile[i].nRef = 0; + } +} + +/* +** Find a VFile by name +*/ +static VFile *findVFile(const char *zName){ + int i; + if( zName==0 ) return 0; + for(i=0; i<MX_FILE; i++){ + if( g.aFile[i].zFilename==0 ) continue; + if( strcmp(g.aFile[i].zFilename, zName)==0 ) return &g.aFile[i]; + } + return 0; +} + +/* +** Find a VFile called zName. Initialize it to the content of +** disk file zDiskFile. +** +** Return NULL if the filesystem is full. +*/ +static VFile *createVFile(const char *zName, const char *zDiskFile){ + VFile *pNew = findVFile(zName); + int i; + FILE *in = 0; + long sz = 0; + + if( pNew ) return pNew; + for(i=0; i<MX_FILE && g.aFile[i].sz>=0; i++){} + if( i>=MX_FILE ) return 0; + if( zDiskFile ){ + in = fopen(zDiskFile, "rb"); + if( in==0 ) fatalError("no such file: \"%s\"", zDiskFile); + fseek(in, 0, SEEK_END); + sz = ftell(in); + rewind(in); + } + pNew = &g.aFile[i]; + if( zName ){ + int nName = (int)strlen(zName)+1; + pNew->zFilename = malloc(nName); + if( pNew->zFilename==0 ){ + if( in ) fclose(in); + return 0; + } + memcpy(pNew->zFilename, zName, nName); + }else{ + pNew->zFilename = 0; + } + pNew->nRef = 0; + pNew->sz = sz; + pNew->a = malloc(sz); + if( sz>0 ){ + if( pNew->a==0 || fread(pNew->a, sz, 1, in)<1 ){ + free(pNew->zFilename); + free(pNew->a); + pNew->a = 0; + pNew->zFilename = 0; + pNew->sz = -1; + pNew = 0; + } + } + if( in ) fclose(in); + return pNew; +} + +/* Methods for the VHandle object +*/ +static int inmemClose(sqlite3_file *pFile){ + VHandle *p = (VHandle*)pFile; + VFile *pVFile = p->pVFile; + pVFile->nRef--; + if( pVFile->nRef==0 && pVFile->zFilename==0 ){ + pVFile->sz = -1; + free(pVFile->a); + pVFile->a = 0; + } + return SQLITE_OK; +} +static int inmemRead( + sqlite3_file *pFile, /* Read from this open file */ + void *pData, /* Store content in this buffer */ + int iAmt, /* Bytes of content */ + sqlite3_int64 iOfst /* Start reading here */ +){ + VHandle *pHandle = (VHandle*)pFile; + VFile *pVFile = pHandle->pVFile; + if( iOfst<0 || iOfst>=pVFile->sz ){ + memset(pData, 0, iAmt); + return SQLITE_IOERR_SHORT_READ; + } + if( iOfst+iAmt>pVFile->sz ){ + memset(pData, 0, iAmt); + iAmt = (int)(pVFile->sz - iOfst); + memcpy(pData, pVFile->a, iAmt); + return SQLITE_IOERR_SHORT_READ; + } + memcpy(pData, pVFile->a + iOfst, iAmt); + return SQLITE_OK; +} +static int inmemWrite( + sqlite3_file *pFile, /* Write to this file */ + const void *pData, /* Content to write */ + int iAmt, /* bytes to write */ + sqlite3_int64 iOfst /* Start writing here */ +){ + VHandle *pHandle = (VHandle*)pFile; + VFile *pVFile = pHandle->pVFile; + if( iOfst+iAmt > pVFile->sz ){ + unsigned char *aNew; + if( iOfst+iAmt >= MX_FILE_SZ ){ + return SQLITE_FULL; + } + aNew = realloc(pVFile->a, (int)(iOfst+iAmt)); + if( aNew==0 ){ + return SQLITE_FULL; + } + pVFile->a = aNew; + if( iOfst > pVFile->sz ){ + memset(pVFile->a + pVFile->sz, 0, (int)(iOfst - pVFile->sz)); + } + pVFile->sz = (int)(iOfst + iAmt); + } + memcpy(pVFile->a + iOfst, pData, iAmt); + return SQLITE_OK; +} +static int inmemTruncate(sqlite3_file *pFile, sqlite3_int64 iSize){ + VHandle *pHandle = (VHandle*)pFile; + VFile *pVFile = pHandle->pVFile; + if( pVFile->sz>iSize && iSize>=0 ) pVFile->sz = (int)iSize; + return SQLITE_OK; +} +static int inmemSync(sqlite3_file *pFile, int flags){ + return SQLITE_OK; +} +static int inmemFileSize(sqlite3_file *pFile, sqlite3_int64 *pSize){ + *pSize = ((VHandle*)pFile)->pVFile->sz; + return SQLITE_OK; +} +static int inmemLock(sqlite3_file *pFile, int type){ + return SQLITE_OK; +} +static int inmemUnlock(sqlite3_file *pFile, int type){ + return SQLITE_OK; +} +static int inmemCheckReservedLock(sqlite3_file *pFile, int *pOut){ + *pOut = 0; + return SQLITE_OK; +} +static int inmemFileControl(sqlite3_file *pFile, int op, void *pArg){ + return SQLITE_NOTFOUND; +} +static int inmemSectorSize(sqlite3_file *pFile){ + return 512; +} +static int inmemDeviceCharacteristics(sqlite3_file *pFile){ + return + SQLITE_IOCAP_SAFE_APPEND | + SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN | + SQLITE_IOCAP_POWERSAFE_OVERWRITE; +} + + +/* Method table for VHandle +*/ +static sqlite3_io_methods VHandleMethods = { + /* iVersion */ 1, + /* xClose */ inmemClose, + /* xRead */ inmemRead, + /* xWrite */ inmemWrite, + /* xTruncate */ inmemTruncate, + /* xSync */ inmemSync, + /* xFileSize */ inmemFileSize, + /* xLock */ inmemLock, + /* xUnlock */ inmemUnlock, + /* xCheck... */ inmemCheckReservedLock, + /* xFileCtrl */ inmemFileControl, + /* xSectorSz */ inmemSectorSize, + /* xDevchar */ inmemDeviceCharacteristics, + /* xShmMap */ 0, + /* xShmLock */ 0, + /* xShmBarrier */ 0, + /* xShmUnmap */ 0, + /* xFetch */ 0, + /* xUnfetch */ 0 +}; + +/* +** Open a new file in the inmem VFS. All files are anonymous and are +** delete-on-close. +*/ +static int inmemOpen( + sqlite3_vfs *pVfs, + const char *zFilename, + sqlite3_file *pFile, + int openFlags, + int *pOutFlags +){ + VFile *pVFile = createVFile(zFilename, 0); + VHandle *pHandle = (VHandle*)pFile; + if( pVFile==0 ){ + return SQLITE_FULL; + } + pHandle->pVFile = pVFile; + pVFile->nRef++; + pFile->pMethods = &VHandleMethods; + if( pOutFlags ) *pOutFlags = openFlags; + return SQLITE_OK; +} + +/* +** Delete a file by name +*/ +static int inmemDelete( + sqlite3_vfs *pVfs, + const char *zFilename, + int syncdir +){ + VFile *pVFile = findVFile(zFilename); + if( pVFile==0 ) return SQLITE_OK; + if( pVFile->nRef==0 ){ + free(pVFile->zFilename); + pVFile->zFilename = 0; + pVFile->sz = -1; + free(pVFile->a); + pVFile->a = 0; + return SQLITE_OK; + } + return SQLITE_IOERR_DELETE; +} + +/* Check for the existence of a file +*/ +static int inmemAccess( + sqlite3_vfs *pVfs, + const char *zFilename, + int flags, + int *pResOut +){ + VFile *pVFile = findVFile(zFilename); + *pResOut = pVFile!=0; + return SQLITE_OK; +} + +/* Get the canonical pathname for a file +*/ +static int inmemFullPathname( + sqlite3_vfs *pVfs, + const char *zFilename, + int nOut, + char *zOut +){ + sqlite3_snprintf(nOut, zOut, "%s", zFilename); + return SQLITE_OK; +} + +/* +** Register the VFS that reads from the g.aFile[] set of files. +*/ +static void inmemVfsRegister(void){ + static sqlite3_vfs inmemVfs; + sqlite3_vfs *pDefault = sqlite3_vfs_find(0); + inmemVfs.iVersion = 3; + inmemVfs.szOsFile = sizeof(VHandle); + inmemVfs.mxPathname = 200; + inmemVfs.zName = "inmem"; + inmemVfs.xOpen = inmemOpen; + inmemVfs.xDelete = inmemDelete; + inmemVfs.xAccess = inmemAccess; + inmemVfs.xFullPathname = inmemFullPathname; + inmemVfs.xRandomness = pDefault->xRandomness; + inmemVfs.xSleep = pDefault->xSleep; + inmemVfs.xCurrentTimeInt64 = pDefault->xCurrentTimeInt64; + sqlite3_vfs_register(&inmemVfs, 0); +}; + +/* +** Timeout handler +*/ +#ifdef __unix__ +static void timeoutHandler(int NotUsed){ + (void)NotUsed; + fatalError("timeout\n"); +} +#endif + +/* +** Set the an alarm to go off after N seconds. Disable the alarm +** if N==0 +*/ +static void setAlarm(int N){ +#ifdef __unix__ + alarm(N); +#else + (void)N; +#endif +} +/*************************************************************************** +** String accumulator object +*/ +typedef struct Str Str; +struct Str { + char *z; /* The string. Memory from malloc() */ + sqlite3_uint64 n; /* Bytes of input used */ + sqlite3_uint64 nAlloc; /* Bytes allocated to z[] */ + int oomErr; /* OOM error has been seen */ +}; + +/* Initialize a Str object */ +static void StrInit(Str *p){ + memset(p, 0, sizeof(*p)); +} + +/* Append text to the end of a Str object */ +static void StrAppend(Str *p, const char *z){ + sqlite3_uint64 n = strlen(z); + if( p->n + n >= p->nAlloc ){ + char *zNew; + sqlite3_uint64 nNew; + if( p->oomErr ) return; + nNew = p->nAlloc*2 + 100 + n; + zNew = sqlite3_realloc64(p->z, nNew); + if( zNew==0 ){ + sqlite3_free(p->z); + memset(p, 0, sizeof(*p)); + p->oomErr = 1; + return; + } + p->z = zNew; + p->nAlloc = nNew; + } + memcpy(p->z + p->n, z, (int)n); + p->n += n; + p->z[p->n] = 0; +} + +/* Return the current string content */ +static char *StrStr(Str *p){ + return p->z; +} + +/* Free the string */ +static void StrFree(Str *p){ + sqlite3_free(p->z); + StrInit(p); +} + +/* +** Return the value of a hexadecimal digit. Return -1 if the input +** is not a hex digit. +*/ +static int hexDigitValue(char c){ + if( c>='0' && c<='9' ) return c - '0'; + if( c>='a' && c<='f' ) return c - 'a' + 10; + if( c>='A' && c<='F' ) return c - 'A' + 10; + return -1; +} + +/* +** Interpret zArg as an integer value, possibly with suffixes. +*/ +static int integerValue(const char *zArg){ + sqlite3_int64 v = 0; + static const struct { char *zSuffix; int iMult; } aMult[] = { + { "KiB", 1024 }, + { "MiB", 1024*1024 }, + { "GiB", 1024*1024*1024 }, + { "KB", 1000 }, + { "MB", 1000000 }, + { "GB", 1000000000 }, + { "K", 1000 }, + { "M", 1000000 }, + { "G", 1000000000 }, + }; + int i; + int isNeg = 0; + if( zArg[0]=='-' ){ + isNeg = 1; + zArg++; + }else if( zArg[0]=='+' ){ + zArg++; + } + if( zArg[0]=='0' && zArg[1]=='x' ){ + int x; + zArg += 2; + while( (x = hexDigitValue(zArg[0]))>=0 ){ + v = (v<<4) + x; + zArg++; + } + }else{ + while( ISDIGIT(zArg[0]) ){ + v = v*10 + zArg[0] - '0'; + zArg++; + } + } + for(i=0; i<sizeof(aMult)/sizeof(aMult[0]); i++){ + if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){ + v *= aMult[i].iMult; + break; + } + } + if( v>0x7fffffff ) fatalError("parameter too large - max 2147483648"); + return (int)(isNeg? -v : v); +} + +/* +** This callback is invoked by sqlite3_log(). +*/ +static void sqlLog(void *pNotUsed, int iErrCode, const char *zMsg){ + printf("LOG: (%d) %s\n", iErrCode, zMsg); + fflush(stdout); +} + +#ifndef SQLITE_OMIT_PROGRESS_CALLBACK +/* +** This an SQL progress handler. After an SQL statement has run for +** many steps, we want to interrupt it. This guards against infinite +** loops from recursive common table expressions. +** +** *pVdbeLimitFlag is true if the --limit-vdbe command-line option is used. +** In that case, hitting the progress handler is a fatal error. +*/ +static int progressHandler(void *pVdbeLimitFlag){ + if( *(int*)pVdbeLimitFlag ) fatalError("too many VDBE cycles"); + return 1; +} +#endif + +/* +** Allowed values for the runFlags parameter to runSql() +*/ +#define SQL_TRACE 0x0001 /* Print each SQL statement as it is prepared */ +#define SQL_OUTPUT 0x0002 /* Show the SQL output */ + +/* +** Run multiple commands of SQL. Similar to sqlite3_exec(), but does not +** stop if an error is encountered. +*/ +static void runSql(sqlite3 *db, const char *zSql, unsigned runFlags){ + const char *zMore; + const char *zEnd = &zSql[strlen(zSql)]; + sqlite3_stmt *pStmt; + + while( zSql && zSql[0] ){ + zMore = 0; + pStmt = 0; + sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zMore); + assert( zMore<=zEnd ); + if( zMore==zSql ) break; + if( runFlags & SQL_TRACE ){ + const char *z = zSql; + int n; + while( z<zMore && ISSPACE(z[0]) ) z++; + n = (int)(zMore - z); + while( n>0 && ISSPACE(z[n-1]) ) n--; + if( n==0 ) break; + if( pStmt==0 ){ + printf("TRACE: %.*s (error: %s)\n", n, z, sqlite3_errmsg(db)); + }else{ + printf("TRACE: %.*s\n", n, z); + } + } + zSql = zMore; + if( pStmt ){ + if( (runFlags & SQL_OUTPUT)==0 ){ + while( SQLITE_ROW==sqlite3_step(pStmt) ){} + }else{ + int nCol = -1; + int nRow; + for(nRow=0; SQLITE_ROW==sqlite3_step(pStmt); nRow++){ + int i; + if( nCol<0 ){ + nCol = sqlite3_column_count(pStmt); + } + for(i=0; i<nCol; i++){ + int eType = sqlite3_column_type(pStmt,i); + printf("ROW[%d].%s = ", nRow, sqlite3_column_name(pStmt,i)); + switch( eType ){ + case SQLITE_NULL: { + printf("NULL\n"); + break; + } + case SQLITE_INTEGER: { + printf("INT %s\n", sqlite3_column_text(pStmt,i)); + break; + } + case SQLITE_FLOAT: { + printf("FLOAT %s\n", sqlite3_column_text(pStmt,i)); + break; + } + case SQLITE_TEXT: { + printf("TEXT [%s]\n", sqlite3_column_text(pStmt,i)); + break; + } + case SQLITE_BLOB: { + printf("BLOB (%d bytes)\n", sqlite3_column_bytes(pStmt,i)); + break; + } + } + } + } + } + sqlite3_finalize(pStmt); + } + } +} + +int main(int argc, char **argv){ + int i; /* Loop counter */ + int nDb = 0; /* Number of databases to fuzz */ + char **azDb = 0; /* Names of the databases (limit: 20) */ + int verboseFlag = 0; /* True for extra output */ + int noLookaside = 0; /* Disable lookaside if true */ + int vdbeLimitFlag = 0; /* Stop after 100,000 VDBE ops */ + int nHeap = 0; /* True for fixed heap size */ + int iTimeout = 0; /* Timeout delay in seconds */ + int rc; /* Result code from SQLite3 API calls */ + sqlite3 *db; /* The database connection */ + sqlite3_stmt *pStmt; /* A single SQL statement */ + Str sql; /* SQL to run */ + unsigned runFlags = 0; /* Flags passed to runSql */ + + for(i=1; i<argc; i++){ + char *z = argv[i]; + if( z[0]!='-' ){ + azDb = realloc(azDb, sizeof(azDb[0])*(nDb+1)); + if( azDb==0 ) fatalError("out of memory"); + azDb[nDb++] = z; + continue; + } + z++; + if( z[0]=='-' ) z++; + if( strcmp(z, "help")==0 ){ + showHelp(argv[0]); + }else if( strcmp(z, "limit-mem")==0 ){ + if( i==argc-1 ) fatalError("missing argument to %s", argv[i]); + nHeap = integerValue(argv[++i]); + }else if( strcmp(z, "no-lookaside")==0 ){ + noLookaside = 1; + }else if( strcmp(z, "timeout")==0 ){ + if( i==argc-1 ) fatalError("missing argument to %s", argv[i]); + iTimeout = integerValue(argv[++i]); + }else if( strcmp(z, "trace")==0 ){ + runFlags |= SQL_OUTPUT|SQL_TRACE; + }else if( strcmp(z, "limit-vdbe")==0 ){ + vdbeLimitFlag = 1; + }else if( strcmp(z, "v")==0 || strcmp(z, "verbose")==0 ){ + verboseFlag = 1; + runFlags |= SQL_TRACE; + }else{ + fatalError("unknown command-line option: \"%s\"\n", argv[i]); + } + } + if( nDb==0 ){ + showHelp(argv[0]); + } + if( verboseFlag ){ + sqlite3_config(SQLITE_CONFIG_LOG, sqlLog); + } + if( nHeap>0 ){ + void *pHeap = malloc( nHeap ); + if( pHeap==0 ) fatalError("cannot allocate %d-byte heap\n", nHeap); + rc = sqlite3_config(SQLITE_CONFIG_HEAP, pHeap, nHeap, 32); + if( rc ) fatalError("heap configuration failed: %d\n", rc); + } + if( noLookaside ){ + sqlite3_config(SQLITE_CONFIG_LOOKASIDE, 0, 0); + } + inmemVfsRegister(); + formatVfs(); + StrInit(&sql); +#ifdef __unix__ + signal(SIGALRM, timeoutHandler); +#endif + for(i=0; i<nDb; i++){ + if( verboseFlag && nDb>1 ){ + printf("DATABASE-FILE: %s\n", azDb[i]); + fflush(stdout); + } + if( iTimeout ) setAlarm(iTimeout); + createVFile("test.db", azDb[i]); + rc = sqlite3_open_v2("test.db", &db, SQLITE_OPEN_READWRITE, "inmem"); + if( rc ){ + printf("cannot open test.db for \"%s\"\n", azDb[i]); + reformatVfs(); + continue; + } +#ifndef SQLITE_OMIT_PROGRESS_CALLBACK + if( vdbeLimitFlag ){ + sqlite3_progress_handler(db, 100000, progressHandler, &vdbeLimitFlag); + } +#endif + rc = sqlite3_prepare_v2(db, "SELECT sql FROM autoexec", -1, &pStmt, 0); + if( rc==SQLITE_OK ){ + while( SQLITE_ROW==sqlite3_step(pStmt) ){ + StrAppend(&sql, (const char*)sqlite3_column_text(pStmt, 0)); + StrAppend(&sql, "\n"); + } + } + sqlite3_finalize(pStmt); + StrAppend(&sql, "PRAGMA integrity_check;\n"); + runSql(db, StrStr(&sql), runFlags); + sqlite3_close(db); + reformatVfs(); + StrFree(&sql); + if( sqlite3_memory_used()>0 ){ + free(azDb); + reformatVfs(); + fatalError("memory leak of %lld bytes", sqlite3_memory_used()); + } + } + StrFree(&sql); + reformatVfs(); + return 0; +} diff --git a/test/dbfuzz001.test b/test/dbfuzz001.test new file mode 100644 index 0000000000..228dd16db6 --- /dev/null +++ b/test/dbfuzz001.test @@ -0,0 +1,397 @@ +# 2012-12-13 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# Test cases for corrupt database files. + +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +ifcapable !deserialize { + finish_test + return +} +database_may_be_corrupt + +# In the following database file, there is 384 bytes of free space +# on page 8 that does not appear on the freeblock list. +# +do_test dbfuzz001-100 { + sqlite3 db {} + db deserialize [decode_hexdb { + | size 5632 pagesize 512 filename c4.db + | page 1 offset 0 + | 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. + | 16: 02 00 01 01 00 40 20 20 00 00 00 02 00 00 00 0b .....@ ........ + | 32: 00 00 00 06 00 00 00 01 00 00 00 28 00 00 00 04 ...........(.... + | 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ + | 80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 02 ................ + | 96: 00 2e 30 38 0d 00 00 00 06 01 06 00 01 da 01 b0 ..08............ + | 112: 01 56 01 86 01 2a 01 06 00 00 00 00 00 00 00 00 .V...*.......... + | 256: 00 00 00 00 00 00 22 07 06 17 11 11 01 31 74 61 ......"......1ta + | 272: 62 6c 65 74 34 74 34 07 43 52 45 41 54 45 20 54 blet4t4.CREATE T + | 288: 41 42 4c 45 20 74 34 28 78 29 2a 06 06 17 13 11 ABLE t4(x)*..... + | 304: 01 3f 69 6e 64 65 78 00 00 00 00 00 00 00 00 00 .?index......... + | 336: 20 74 33 28 78 29 2e 04 06 17 15 11 01 45 69 6e t3(x).......Ein + | 352: 64 65 78 74 32 63 64 74 32 05 43 52 45 41 54 45 dext2cdt2.CREATE + | 368: 20 49 4e 44 45 58 20 74 32 63 64 20 4f 4e 20 74 INDEX t2cd ON t + | 384: 32 28 63 2c 64 29 28 05 06 17 11 11 01 3d 74 61 2(c,d)(......=ta + | 400: 62 6c 65 74 33 74 33 04 43 52 45 41 54 45 20 54 blet3t3.CREATE T + | 416: 41 42 4c 45 20 74 33 28 63 2c 78 2c 65 2c 66 29 ABLE t3(c,x,e,f) + | 432: 28 02 06 17 11 11 01 3d 74 61 62 6c 65 74 32 74 (......=tablet2t + | 448: 32 03 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 2.CREATE TABLE t + | 464: 32 28 63 2c 64 2c 65 2c 66 29 24 01 06 17 11 11 2(c,d,e,f)$..... + | 480: 01 35 74 61 62 6c 65 74 31 74 31 02 43 52 45 41 .5tablet1t1.CREA + | 496: 54 45 20 54 41 42 4c 45 20 74 31 28 61 2c 62 29 TE TABLE t1(a,b) + | page 2 offset 512 + | 0: 0d 00 00 00 00 02 00 00 00 00 00 00 00 00 00 00 ................ + | page 3 offset 1024 + | 0: 0d 00 00 00 00 02 00 00 00 00 00 00 00 00 00 00 ................ + | page 4 offset 1536 + | 0: 05 00 00 00 03 01 f1 00 00 00 00 0b 01 fb 01 f6 ................ + | 16: 01 f1 00 16 00 00 09 06 05 01 01 01 01 04 04 03 ................ + | 32: 03 07 05 05 01 01 09 09 02 02 19 04 05 17 17 17 ................ + | 48: 17 73 65 76 65 6e 65 69 67 68 74 65 69 67 68 74 .seveneighteight + | 64: 73 65 76 65 6e 25 03 05 07 07 07 07 40 14 00 00 seven%......@... + | 80: 00 00 00 00 40 18 00 00 00 00 00 00 40 18 00 00 ....@.......@... + | 96: 00 00 00 00 40 14 00 00 00 00 00 00 09 02 05 01 ....@........... + | 112: 01 01 01 03 04 04 03 07 01 05 09 01 01 09 02 02 ................ + | 352: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1a ................ + | 496: 00 00 00 00 0a 3e 00 00 00 09 21 00 00 00 08 06 .....>....!..... + | page 5 offset 2048 + | 0: 0a 00 00 00 00 02 00 00 00 00 00 00 00 00 00 00 ................ + | page 7 offset 3072 + | 0: 0d 00 00 00 08 01 c2 00 01 fb 01 f6 01 f1 01 ec ................ + | 16: 01 e0 01 d4 01 cb 01 c2 00 00 00 00 00 00 00 00 ................ + | 96: 00 00 00 00 13 00 00 00 00 00 00 00 00 00 00 00 ................ + | 224: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 02 ................ + | 288: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 03 ................ + | 448: 00 00 07 08 02 17 65 69 67 68 74 07 07 02 17 65 ......eight....e + | 464: 69 67 68 74 0a 06 02 07 40 18 00 00 00 00 00 00 ight....@....... + | 480: 0a 05 02 07 40 18 00 00 00 00 00 00 03 04 02 01 ....@........... + | 496: 04 03 03 02 01 04 03 02 02 01 02 03 01 02 01 02 ................ + | page 8 offset 3584 + | 0: 0d 00 21 00 01 00 16 00 00 16 00 16 00 16 00 16 ..!............. + | 16: 00 16 00 16 00 00 09 06 05 01 01 01 01 04 04 03 ................ + | 32: 03 00 00 00 5f 01 09 09 02 02 00 00 00 56 17 17 ...._........V.. + | 48: 17 73 65 76 65 6e 65 69 67 68 74 65 69 67 68 74 .seveneighteight + | 64: 73 65 76 65 6e 00 00 00 3b 07 07 07 40 14 00 00 seven...;...@... + | 80: 00 00 00 00 40 18 00 00 00 00 00 00 40 18 00 00 ....@.......@... + | 96: 00 00 00 00 40 14 00 00 00 00 00 00 00 00 00 14 ....@........... + | 112: 01 01 01 03 04 04 03 00 00 00 09 01 01 09 02 02 ................ + | 352: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1a ................ + | page 9 offset 4096 + | 0: 0d 00 00 00 1b 00 47 00 01 d9 01 be 01 af 01 a0 ......G......... + | 16: 01 91 01 82 01 73 01 64 01 55 01 46 01 37 01 28 .....s.d.U.F.7.( + | 32: 01 19 01 0a 00 fb 00 ec 00 dd 00 ce 00 bf 00 b0 ................ + | 48: 00 a1 00 92 00 83 00 74 00 65 00 56 00 47 00 00 .......t.e.V.G.. + | 64: 00 00 00 00 00 00 00 0d 21 00 00 48 01 54 00 01 ........!..H.T.. + | 80: f7 01 ec 01 c5 01 0d 20 00 00 48 01 54 00 01 f7 ....... ..H.T... + | 96: 01 ec 01 c5 01 0d 1f 00 00 48 01 54 00 01 f7 01 .........H.T.... + | 112: ec 01 c5 01 0d 1e 00 00 48 01 54 00 01 f7 01 ec ........H.T..... + | 128: 01 c5 01 0d 1d 00 00 48 01 54 00 01 f7 01 ec 01 .......H.T...... + | 144: c5 01 0d 1c 00 00 48 01 54 00 01 f7 01 ec 01 c5 ......H.T....... + | 160: 01 0d 1b 00 00 48 01 54 00 01 f7 01 ec 01 c5 01 .....H.T........ + | 176: 0d 1a 00 00 48 01 54 00 01 f7 01 ec 01 c5 01 0d ....H.T......... + | 192: 19 00 00 48 01 54 00 01 f7 01 ec 01 c5 01 0d 18 ...H.T.......... + | 208: 00 00 48 01 54 00 01 f7 01 ec 01 c5 01 0d 17 00 ..H.T........... + | 224: 00 48 01 54 00 01 f7 01 ec 01 c5 01 0d 16 00 00 .H.T............ + | 240: 48 01 54 00 01 f7 01 ec 01 c5 01 0d 15 00 00 48 H.T............H + | 256: 01 54 00 01 f7 01 ec 01 c5 01 0d 14 00 00 48 01 .T............H. + | 272: 54 00 01 f7 01 ec 01 c5 01 0d 13 00 00 48 01 54 T............H.T + | 288: 00 01 f7 01 ec 01 c5 01 0d 12 00 00 48 01 54 00 ............H.T. + | 304: 01 f7 01 ec 01 c5 01 0d 11 00 00 48 01 54 00 01 ...........H.T.. + | 320: f7 01 ec 01 c5 01 0d 10 00 00 48 01 54 00 01 f7 ..........H.T... + | 336: 01 ec 01 c5 01 0d 0f 00 00 48 01 54 00 01 f7 01 .........H.T.... + | 352: ec 01 c5 01 0d 0e 00 00 48 01 54 00 01 f7 01 ec ........H.T..... + | 368: 01 c5 01 0d 0d 00 00 48 01 54 00 01 f7 01 ec 01 .......H.T...... + | 384: c5 01 0d 0c 00 00 48 01 54 00 01 f7 01 ec 01 c5 ......H.T....... + | 400: 01 0d 0b 00 00 48 01 54 00 01 f7 01 ec 01 c5 01 .....H.T........ + | 416: 0d 0a 00 00 48 01 54 00 01 f7 01 ec 01 c5 01 0d ....H.T......... + | 432: 09 00 00 48 01 54 00 01 f7 01 ec 01 c5 01 19 08 ...H.T.......... + | 448: 05 17 17 17 17 65 69 67 68 74 65 69 67 68 74 73 .....eighteights + | 464: 65 76 65 6e 73 65 76 65 6e 25 07 05 07 07 07 07 evenseven%...... + | 480: 40 18 00 00 00 00 00 00 40 18 00 00 00 00 00 00 @.......@....... + | 496: 40 14 00 00 00 00 00 00 40 14 00 00 00 00 00 00 @.......@....... + | page 10 offset 4608 + | 0: 0d 00 00 00 1d 00 4d 00 01 f1 01 e2 01 d3 01 c4 ......M......... + | 16: 01 b5 01 a6 01 97 01 88 01 79 01 6a 01 5b 01 4c .........y.j.[.L + | 32: 01 3d 01 2e 01 1f 01 10 01 01 00 f2 00 e3 00 d4 .=.............. + | 48: 00 c5 00 b6 00 a7 00 98 00 89 00 7a 00 6b 00 5c ...........z.k.\ + | 64: 00 4d 00 00 00 00 00 00 00 00 00 00 00 0d 3e 00 .M............>. + | 80: 00 48 01 54 00 01 f7 01 ec 01 c5 01 0d 3d 00 00 .H.T.........=.. + | 96: 48 01 54 00 01 f7 01 ec 01 c5 01 0d 3c 00 00 48 H.T.........<..H + | 112: 01 54 00 01 f7 01 ec 01 c5 01 0d 3b 00 00 48 01 .T.........;..H. + | 128: 54 00 01 f7 01 ec 01 c5 01 0d 3a 00 00 48 01 54 T.........:..H.T + | 144: 00 01 f7 01 ec 01 c5 01 0d 39 00 00 48 01 54 00 .........9..H.T. + | 160: 01 f7 01 ec 01 c5 01 0d 38 00 00 48 01 54 00 01 ........8..H.T.. + | 176: f7 01 ec 01 c5 01 0d 37 00 00 48 01 54 00 01 f7 .......7..H.T... + | 192: 01 ec 01 c5 01 0d 36 00 00 48 01 54 00 01 f7 01 ......6..H.T.... + | 208: ec 01 c5 01 0d 35 00 00 48 01 54 00 01 f7 01 ec .....5..H.T..... + | 224: 01 c5 01 0d 34 00 00 48 01 54 00 01 f7 01 ec 01 ....4..H.T...... + | 240: c5 01 0d 33 00 00 48 01 54 00 01 f7 01 ec 01 c5 ...3..H.T....... + | 256: 01 0d 32 00 00 48 01 54 00 01 f7 01 ec 01 c5 01 ..2..H.T........ + | 272: 0d 31 00 00 48 01 54 00 01 f7 01 ec 01 c5 01 0d .1..H.T......... + | 288: 30 00 00 48 01 54 00 01 f7 01 ec 01 c5 01 0d 2f 0..H.T........./ + | 304: 00 00 48 01 54 00 01 f7 01 ec 01 c5 01 0d 2e 00 ..H.T........... + | 320: 00 48 01 54 00 01 f7 01 ec 01 c5 01 0d 2d 00 00 .H.T.........-.. + | 336: 48 01 54 00 01 f7 01 ec 01 c5 01 0d 2c 00 00 48 H.T.........,..H + | 352: 01 54 00 01 f7 01 ec 01 c5 01 0d 2b 00 00 48 01 .T.........+..H. + | 368: 54 00 01 f7 01 ec 01 c5 01 0d 2a 00 00 48 01 54 T.........*..H.T + | 384: 00 01 f7 01 ec 01 c5 01 0d 29 00 00 48 01 54 00 .........)..H.T. + | 400: 01 f7 01 ec 01 c5 01 0d 28 00 00 48 01 54 00 01 ........(..H.T.. + | 416: f7 01 ec 01 c5 01 0d 27 00 00 48 01 54 00 01 f7 .......'..H.T... + | 432: 01 ec 01 c5 01 0d 26 00 00 48 01 54 00 01 f7 01 ......&..H.T.... + | 448: ec 01 c5 01 0d 25 00 00 48 01 54 00 01 f7 01 ec .....%..H.T..... + | 464: 01 c5 01 0d 24 00 00 48 01 54 00 01 f7 01 ec 01 ....$..H.T...... + | 480: c5 01 0d 23 00 00 48 01 54 00 01 f7 01 ec 01 c5 ...#..H.T....... + | 496: 01 0d 22 00 00 48 01 54 00 01 f7 01 ec 01 c5 01 .."..H.T........ + | page 11 offset 5120 + | 0: 0d 00 00 00 0a 01 6a 00 01 f1 01 e2 01 d3 01 c4 ......j......... + | 16: 01 b5 01 a6 01 97 01 88 01 79 01 6a 00 00 00 00 .........y.j.... + | 352: 00 00 00 00 00 00 00 00 00 00 0d 48 00 00 48 01 ...........H..H. + | 368: 54 00 01 f7 01 ec 01 c5 01 0d 47 00 00 48 01 54 T.........G..H.T + | 384: 00 01 f7 01 ec 01 c5 01 0d 46 00 00 48 01 54 00 .........F..H.T. + | 400: 01 f7 01 ec 01 c5 01 0d 45 00 00 48 01 54 00 01 ........E..H.T.. + | 416: f7 01 ec 01 c5 01 0d 44 00 00 48 01 54 00 01 f7 .......D..H.T... + | 432: 01 ec 01 c5 01 0d 43 00 00 48 01 54 00 01 f7 01 ......C..H.T.... + | 448: ec 01 c5 01 0d 42 00 00 48 01 54 00 01 f7 01 ec .....B..H.T..... + | 464: 01 c5 01 0d 41 00 00 48 01 54 00 01 f7 01 ec 01 ....A..H.T...... + | 480: c5 01 0d 40 00 00 48 01 54 00 01 f7 01 ec 01 c5 ...@..H.T....... + | 496: 01 0d 3f 00 00 48 01 54 00 01 f7 01 ec 01 c5 01 ..?..H.T........ + | end c4.db + }] +} {} + +ifcapable !oversize_cell_check { + # Non SQLITE_ENABLE_OVERSIZE_CELL_CHECK builds: + do_test dbfuzz001-101a { + db eval {PRAGMA writable_schema=on; PRAGMA integrity_check} + } {/Fragmentation of 384 bytes reported as 0 on page 8/} +} else { + # SQLITE_ENABLE_OVERSIZE_CELL_CHECK builds: + do_catchsql_test dbfuzz001-101b { + PRAGMA writable_schema=on; + PRAGMA integrity_check; + } {1 {database disk image is malformed}} +} + +# The DELETE query below deletes the very last cell from page 8. +# Prior to a certain fix to sqlite3BtreeDelete() and because of the +# corruption to the freeblock list on page 8, this would fail to +# cause a rebalance operation, which would leave the btree in a weird +# state that would lead to segfaults and or assertion faults. +# +do_execsql_test dbfuzz001-110 { + DELETE FROM t3 WHERE x IS NOT NULL AND +rowid=6; +} {} + +# This is a dbfuzz2-generate test case that can cause a page with +# pPage->nCell==0 to enter the balancer. +# +do_test dbfuzz001-200 { + db deserialize [decode_hexdb { + | size 3076 pagesize 512 filename c03.db + | page 1 offset 0 + | 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. + | 16: 02 00 01 01 00 40 20 20 00 00 00 0c 00 00 00 07 .....@ ........ + | 32: 00 00 00 00 00 00 00 00 00 00 00 08 00 00 00 04 ................ + | 48: 00 00 00 00 00 00 00 03 e8 00 00 01 00 00 00 00 ................ + | 80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0c ................ + | 96: 00 2e 2c 50 0d 00 00 00 06 01 06 00 01 da 01 b0 ..,P............ + | 112: 01 56 01 86 01 2a 01 06 00 00 00 00 00 00 00 00 .V...*.......... + | 128: 00 00 00 00 00 00 00 00 ef 00 00 00 00 00 00 00 ................ + | 192: 00 7f 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ + | 224: 00 00 00 00 00 00 00 00 00 00 00 00 00 ff e9 00 ................ + | 256: 00 00 00 00 00 00 22 07 06 17 11 11 01 31 74 61 ......"......1ta + | 272: 62 6c 65 74 34 74 34 07 43 52 45 41 54 45 20 54 blet4t4.CREATE T + | 288: 41 42 4c 45 20 74 34 28 78 29 2a 06 06 17 13 11 ABLE t4(x)*..... + | 304: 01 3f 69 6e 64 65 78 74 33 78 74 33 06 43 52 45 .?indext3xt3.CRE + | 320: 41 54 45 20 49 4e 44 45 58 20 74 33 64 20 4f 4e ATE INDEX t3d ON + | 336: 20 74 33 28 78 29 2e 04 06 17 15 11 01 45 69 6e t3(x).......Ein + | 352: 64 65 78 74 32 63 64 74 32 05 43 52 45 41 54 45 dext2cdt2.CREATE + | 368: 20 49 4e 44 45 58 20 74 32 63 64 20 4f 4e 20 74 INDEX t2cd ON t + | 384: 32 28 63 2c 64 29 28 05 06 17 11 11 01 3d 74 61 2(c,d)(......=ta + | 400: 62 6c 65 74 33 74 33 04 43 52 45 41 54 45 20 54 blet3t3.CREATE T + | 416: 41 42 4c 45 20 74 33 28 63 2c 78 2c 65 2c 66 29 ABLE t3(c,x,e,f) + | 432: 28 02 06 17 11 11 01 3d 74 61 62 6c 65 74 32 74 (......=tablet2t + | 448: 32 03 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 2.CREATE TABLE t + | 464: 32 28 63 2c 64 2c 65 2c 66 29 24 01 06 17 11 11 2(c,d,e,f)$..... + | 480: 01 35 74 61 62 6c 65 74 31 74 31 02 43 52 45 41 .5tablet1t1.CREA + | 496: 54 45 20 54 41 42 4c 45 20 74 31 28 61 2c 62 29 TE TABLE t1(a,b) + | page 2 offset 512 + | 0: 0d 00 00 00 04 01 cf 00 01 fa 01 f3 01 de 01 cf ................ + | 176: 10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ + | 256: 00 00 14 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ + | 368: 00 00 00 00 00 00 00 00 00 00 00 00 1e 00 00 00 ................ + | 416: 00 00 00 1b 00 00 00 00 04 00 00 00 00 00 00 00 ................ + | 448: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0d ................ + | 464: 04 03 17 17 73 65 76 65 6e 65 69 67 68 74 13 03 ....seveneight.. + | 480: 03 07 07 40 14 00 00 00 00 00 00 40 18 00 00 00 ...@.......@.... + | 496: 00 00 00 05 02 03 01 01 03 04 04 01 03 09 01 02 ................ + | page 3 offset 1024 + | 0: 0d 00 00 00 08 01 54 00 01 f7 01 ec 01 c5 01 aa ......T......... + | 16: 01 a1 01 96 01 6f 01 54 00 00 00 00 00 00 00 00 .....o.T........ + | 32: 00 00 00 00 00 00 00 03 e8 00 00 00 00 00 00 00 ................ + | 336: 00 00 00 00 19 08 05 16 17 17 17 65 69 67 68 74 ...........eight + | 352: 65 69 67 68 74 73 65 76 65 6e 73 65 76 ff ff ff eightsevensev... + | 368: 0e 05 07 07 07 07 40 18 00 00 00 00 00 00 40 18 ......@.......@. + | 384: 00 00 00 00 00 00 40 14 00 00 00 00 00 00 40 14 ......@.......@. + | 400: 00 00 00 00 00 00 09 06 05 01 01 01 01 04 04 03 ................ + | 416: 03 07 05 05 01 01 09 09 02 02 19 04 05 17 17 17 ................ + | 432: 17 73 65 6f 65 6e 65 69 67 68 74 65 69 67 68 74 .seoeneighteight + | 448: 73 65 76 65 6e 25 03 05 07 07 07 07 40 14 00 00 seven%......@... + | 464: 00 00 00 00 40 18 00 00 00 00 00 00 40 18 00 00 ....@.......@... + | 480: 00 00 00 00 40 14 00 00 00 00 00 00 09 02 05 01 ....@........... + | 496: 01 01 01 03 04 04 03 07 01 05 09 01 01 09 02 02 ................ + | page 4 offset 1536 + | 0: 0d 00 00 00 00 00 10 00 00 00 00 00 00 00 00 00 ................ + | 160: 00 00 00 ea 00 00 00 00 00 00 00 00 00 00 00 00 ................ + | 336: 00 00 00 00 00 00 00 00 00 00 00 00 20 00 00 00 ............ ... + | page 5 offset 2048 + | 0: 0a 00 00 00 08 01 96 00 01 fa 01 c4 01 f2 01 bc ................ + | 16: 01 dc 01 a6 01 96 01 cc 00 00 00 00 00 00 00 00 ................ + | 48: 00 00 00 00 00 00 00 00 00 00 10 00 00 00 00 00 ................ + | 288: 00 00 00 00 00 00 00 00 00 64 00 00 00 2b 00 00 .........d...+.. + | 400: 00 00 00 00 00 00 0f 04 17 17 01 65 69 67 68 74 ...........eight + | 416: 65 69 6f 68 74 08 15 04 07 07 01 40 18 00 00 00 eioht......@.... + | 432: 00 00 00 40 18 00 00 00 00 00 00 07 07 04 01 01 ...@............ + | 448: 01 04 04 06 07 04 01 01 01 02 02 05 0f 04 17 17 ................ + | 464: 01 73 65 76 65 6e 65 69 67 68 74 04 15 04 07 07 .seveneight..... + | 480: 01 40 14 00 00 00 00 00 00 40 18 00 00 00 00 00 .@.......@...... + | 496: 00 03 07 04 01 01 01 03 04 02 05 04 09 01 09 02 ................ + | page 6 offset 2560 + | 0: 0a 00 00 00 00 02 00 00 00 00 00 00 00 0d 00 00 ................ + | 16: 00 08 01 c2 00 01 fb 01 f6 01 f1 01 ec 01 e0 01 ................ + | 32: d4 01 cb 01 c2 00 00 00 00 00 00 00 00 00 00 00 ................ + | 160: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 04 00 ................ + | 448: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 07 ................ + | 464: 08 02 17 65 69 67 68 74 07 07 02 17 65 69 67 68 ...eight....eigh + | 480: 74 0a 06 02 07 40 18 00 00 00 00 00 00 0a 05 02 t....@.......... + | 496: 07 40 18 00 04 02 01 04 03 03 02 01 04 03 02 02 .@.............. + | end x/c03.db + }] + catchsql {INSERT INTO t3 SELECT * FROM t2;} +} {1 {database disk image is malformed}} + + +do_test dbfuzz001-310 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 3584 pagesize 512 filename x/c02.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 02 00 01 01 00 40 20 20 00 00 00 0c 00 00 00 07 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 08 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 04 00 00 00 01 00 00 00 00 ................ +| 80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0c ................ +| 96: 00 2e 2c 50 0d 00 00 00 06 01 06 00 01 da 01 b0 ..,P............ +| 112: 01 56 01 86 01 2a 01 06 00 00 00 00 00 00 00 00 .V...*.......... +| 256: 00 00 00 00 00 00 22 07 06 17 11 11 01 31 74 61 ......"......1ta +| 272: 62 6c 65 74 34 74 34 07 43 52 45 41 54 45 20 54 blet4t4.CREATE T +| 288: 41 42 4c 45 20 74 34 28 78 29 2a 06 06 17 13 11 ABLE t4(x)*..... +| 304: 01 3f 69 6e 64 65 78 74 33 78 74 33 05 43 52 45 .?indext3xt3.CRE +| 320: 41 54 45 20 49 4e 44 45 58 20 74 33 78 20 4f 4e ATE INDEX t3x ON +| 336: 20 74 33 28 78 29 2e 04 06 17 15 11 01 45 69 6e t3(x).......Ein +| 352: 64 65 78 74 32 63 64 74 32 05 43 52 45 41 54 45 dext2cdt2.CREATE +| 368: 20 49 4e 44 45 58 20 74 32 63 64 20 4f 4e 20 74 INDEX t2cd ON t +| 384: 32 28 63 2c 64 29 28 05 06 17 11 11 01 3d 74 61 2(c,d)(......=ta +| 400: 62 6c 65 74 33 74 33 07 43 52 45 41 54 45 20 54 blet3t3.CREATE T +| 416: 41 42 4c 45 20 74 33 28 63 2c 78 2c 65 2c 66 29 ABLE t3(c,x,e,f) +| 432: 28 02 06 17 11 11 01 3d 74 61 62 6c 65 74 32 74 (......=tablet2t +| 448: 32 32 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 22CREATE TABLE t +| 464: 32 28 63 2c 64 2c 65 2c 66 29 24 01 06 17 11 11 2(c,d,e,f)$..... +| 480: 01 35 74 61 62 6c 65 74 31 74 31 02 43 52 45 41 .5tablet1t1.CREA +| 496: 54 45 20 54 41 42 4c 45 20 74 31 28 61 2c 62 29 TE TABLE t1(a,b) +| page 2 offset 512 +| 0: 0d 00 00 00 04 01 cf 00 01 fa 01 f3 01 de 01 cf ................ +| 160: 00 00 20 00 00 00 00 00 00 00 00 00 00 00 00 00 .. ............. +| 448: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0d ................ +| 464: 04 03 17 17 73 65 76 65 6e 65 69 67 68 74 13 03 ....seveneight.. +| 480: 03 07 07 40 14 00 00 00 00 00 00 40 18 00 00 00 ...@.......@.... +| 496: 00 00 00 05 02 03 01 01 03 04 04 01 03 09 01 02 ................ +| page 3 offset 1024 +| 0: 0d 00 00 00 08 01 54 00 01 f7 01 ec 01 c5 01 aa ......T......... +| 16: 01 a1 01 96 01 6f 01 54 00 00 00 00 00 00 00 00 .....o.T........ +| 112: 00 00 dd 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 336: 00 00 00 00 19 08 05 17 17 17 17 65 69 67 68 74 ...........eight +| 352: 65 69 67 68 74 73 65 76 65 6e 73 65 76 65 6e 25 eightsevenseven% +| 368: 07 05 07 07 07 07 40 18 00 00 00 00 00 00 40 18 ......@.......@. +| 384: 00 00 00 00 00 00 40 14 00 00 00 00 00 00 40 14 ......@.......@. +| 400: 00 00 00 00 00 00 09 06 05 01 01 01 01 04 04 03 ................ +| 416: 03 07 05 05 01 01 09 09 02 02 19 04 05 17 17 17 ................ +| 432: 17 73 65 76 65 6e 65 69 67 68 74 65 69 67 68 74 .seveneighteight +| 448: 73 65 76 65 6e 25 03 05 07 07 07 07 40 14 00 00 seven%......@... +| 464: 00 00 00 00 40 18 00 00 00 00 00 00 40 18 00 00 ....@.......@... +| 480: 00 00 00 00 40 14 00 00 00 00 00 00 09 02 05 01 ....@........... +| 496: 01 01 01 03 04 04 03 07 01 05 09 01 01 09 02 02 ................ +| page 4 offset 1536 +| 0: 0d 00 00 00 00 02 00 00 00 00 00 00 00 00 00 00 ................ +| 192: 00 00 00 00 00 00 7f 00 00 00 00 00 00 00 00 00 ................ +| 208: 00 e5 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| page 5 offset 2048 +| 0: 0a 00 00 00 08 01 96 00 01 fa 01 c4 01 f2 01 bc ................ +| 16: 01 dc 01 a6 01 96 01 cc 00 00 00 00 00 00 00 00 ................ +| 240: 00 00 00 00 00 00 00 00 00 00 00 00 00 0e 00 00 ................ +| 400: 00 00 00 00 00 00 0f 04 17 07 01 65 69 67 68 74 ...........eight +| 416: 65 69 67 68 74 08 15 04 07 07 01 40 18 00 00 00 eight......@.... +| 432: 00 00 00 40 18 00 00 00 00 00 00 07 07 04 01 01 ...@............ +| 448: 01 04 04 06 07 04 01 01 01 02 02 05 0f 04 17 17 ................ +| 464: 01 73 65 76 65 6e 65 69 67 68 74 04 15 04 07 07 .seveneight..... +| 480: 01 40 14 00 00 00 00 00 00 40 18 00 00 00 00 00 .@.......@...... +| 496: 00 03 07 04 01 01 01 03 04 02 05 04 09 01 09 02 ................ +| page 6 offset 2560 +| 0: 0a 00 00 00 00 02 00 00 00 00 00 00 00 00 00 00 ................ +| 464: 00 00 00 00 00 00 00 00 00 00 7f 00 00 00 00 00 ................ +| page 7 offset 3072 +| 0: 0d 00 00 00 08 01 c2 00 01 fb 01 f6 01 f1 01 ec ................ +| 16: 01 e0 01 d4 01 cb 01 c2 00 00 00 00 00 00 00 00 ................ +| 448: 00 00 07 08 02 17 65 69 67 68 74 07 07 02 17 65 ......eight....e +| 464: 69 67 68 74 0a 06 02 07 40 18 00 00 00 00 00 00 ight....@....... +| 480: 0a 05 02 07 40 18 00 00 00 00 00 00 03 04 02 01 ....@........... +| 496: 04 03 03 02 01 04 03 02 02 01 02 03 01 02 01 02 ................ +| end x/c02.db + }] +} {} + +extra_schema_checks 0 +do_catchsql_test dbfuzz001-320 { + PRAGMA integrity_check; +} {1 {database disk image is malformed}} + +do_catchsql_test dbfuzz001-330 { + DELETE FROM t3 WHERE x IN (SELECT x FROM t4); +} {1 {database disk image is malformed}} +extra_schema_checks 1 + +#------------------------------------------------------------------------- +reset_db + +do_execsql_test dbfuzz001-430 { + CREATE TABLE t1(a INTEGER, b INT, c DEFAULT 0); +} + +do_execsql_test dbfuzz001-420 { + PRAGMA locking_mode=EXCLUSIVE; + PRAGMA journal_mode = memory; + INSERT INTO t1 VALUES(1,2,3); + PRAGMA journal_mode=PERSIST; +} {exclusive memory persist} + +do_execsql_test dbfuzz001-430 { + INSERT INTO t1 VALUES(4, 5, 6); +} + +do_execsql_test dbfuzz001-440 { + PRAGMA journal_mode=MEMORY; + INSERT INTO t1 VALUES(7, 8, 9); +} {memory} + +finish_test diff --git a/test/dbfuzz2-seed1.db b/test/dbfuzz2-seed1.db new file mode 100644 index 0000000000..17f0550d61 Binary files /dev/null and b/test/dbfuzz2-seed1.db differ diff --git a/test/dbfuzz2.c b/test/dbfuzz2.c new file mode 100644 index 0000000000..f0062915df --- /dev/null +++ b/test/dbfuzz2.c @@ -0,0 +1,402 @@ +/* +** 2018-10-26 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** This program is designed for fuzz-testing SQLite database files using +** the -fsanitize=fuzzer option of clang. +** +** The -fsanitize=fuzzer option causes a main() to be inserted automatically. +** That main() invokes LLVMFuzzerTestOneInput(D,S) to be invoked repeatedly. +** Each D is a fuzzed database file. The code in this file runs various +** SQL statements against that database, trying to provoke a failure. +** +** For best results the seed database files should have these tables: +** +** Table "t1" with columns "a" and "b" +** Tables "t2" and "t3 with the same number of compatible columns +** "t3" should have a column names "x" +** Table "t4" with a column "x" that is compatible with t3.x. +** +** Any of these tables can be virtual tables, for example FTS or RTree tables. +** +** To run this test: +** +** mkdir dir +** cp dbfuzz2-seed*.db dir +** clang-6.0 -I. -g -O1 -fsanitize=fuzzer -DTHREADSAFE=0 \ +** -DSQLITE_ENABLE_DBSTAT_VTAB dbfuzz2.c sqlite3.c -ldl +** ./a.out dir +*/ +#include <assert.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <stdarg.h> +#include <ctype.h> +#include <stdint.h> +#ifndef _WIN32 +#include <sys/time.h> +#include <sys/resource.h> +#endif +#include "sqlite3.h" + +/* +** This is the is the SQL that is run against the database. +*/ +static const char *azSql[] = { + "PRAGMA integrity_check;", + "SELECT * FROM sqlite_schema;", + "SELECT sum(length(name)) FROM dbstat;", + "UPDATE t1 SET b=a, a=b WHERE a<b;", + "ALTER TABLE t1 RENAME TO alkjalkjdfiiiwuer987lkjwer82mx97sf98788s9789s;", + "INSERT INTO t3 SELECT * FROM t2;", + "DELETE FROM t3 WHERE x IN (SELECT x FROM t4);", + "REINDEX;", + "DROP TABLE t3;", + "VACUUM;", +}; + +/* Output verbosity level. 0 means complete silence */ +int eVerbosity = 0; + +/* True to activate PRAGMA vdbe_debug=on */ +static int bVdbeDebug = 0; + +/* Maximum size of the in-memory database file */ +static sqlite3_int64 szMax = 104857600; + +/* Progress handler callback data */ +static int nCb = 0; /* Number of callbacks seen so far */ +static int mxCb = 250000; /* Maximum allowed callbacks */ + +/***** Copy/paste from ext/misc/memtrace.c ***************************/ +/* The original memory allocation routines */ +static sqlite3_mem_methods memtraceBase; +static FILE *memtraceOut; + +/* Methods that trace memory allocations */ +static void *memtraceMalloc(int n){ + if( memtraceOut ){ + fprintf(memtraceOut, "MEMTRACE: allocate %d bytes\n", + memtraceBase.xRoundup(n)); + } + return memtraceBase.xMalloc(n); +} +static void memtraceFree(void *p){ + if( p==0 ) return; + if( memtraceOut ){ + fprintf(memtraceOut, "MEMTRACE: free %d bytes\n", memtraceBase.xSize(p)); + } + memtraceBase.xFree(p); +} +static void *memtraceRealloc(void *p, int n){ + if( p==0 ) return memtraceMalloc(n); + if( n==0 ){ + memtraceFree(p); + return 0; + } + if( memtraceOut ){ + fprintf(memtraceOut, "MEMTRACE: resize %d -> %d bytes\n", + memtraceBase.xSize(p), memtraceBase.xRoundup(n)); + } + return memtraceBase.xRealloc(p, n); +} +static int memtraceSize(void *p){ + return memtraceBase.xSize(p); +} +static int memtraceRoundup(int n){ + return memtraceBase.xRoundup(n); +} +static int memtraceInit(void *p){ + return memtraceBase.xInit(p); +} +static void memtraceShutdown(void *p){ + memtraceBase.xShutdown(p); +} + +/* The substitute memory allocator */ +static sqlite3_mem_methods ersaztMethods = { + memtraceMalloc, + memtraceFree, + memtraceRealloc, + memtraceSize, + memtraceRoundup, + memtraceInit, + memtraceShutdown +}; + +/* Begin tracing memory allocations to out. */ +int sqlite3MemTraceActivate(FILE *out){ + int rc = SQLITE_OK; + if( memtraceBase.xMalloc==0 ){ + rc = sqlite3_config(SQLITE_CONFIG_GETMALLOC, &memtraceBase); + if( rc==SQLITE_OK ){ + rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &ersaztMethods); + } + } + memtraceOut = out; + return rc; +} + +/* Deactivate memory tracing */ +int sqlite3MemTraceDeactivate(void){ + int rc = SQLITE_OK; + if( memtraceBase.xMalloc!=0 ){ + rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &memtraceBase); + if( rc==SQLITE_OK ){ + memset(&memtraceBase, 0, sizeof(memtraceBase)); + } + } + memtraceOut = 0; + return rc; +} +/***** End copy/paste from ext/misc/memtrace.c ***************************/ + +/* +** Progress handler callback +** +** Count the number of callbacks and cause an abort once the limit is +** reached. +*/ +static int progress_handler(void *pNotUsed){ + nCb++; + if( nCb<mxCb ) return 0; + if( eVerbosity>=1 ){ + printf("-- Progress limit of %d reached\n", mxCb); + } + return 1; +} + +/* libFuzzer invokes this routine with fuzzed database files (in aData). +** This routine run SQLite against the malformed database to see if it +** can provoke a failure or malfunction. +*/ +int LLVMFuzzerTestOneInput(const uint8_t *aData, size_t nByte){ + unsigned char *a; + sqlite3 *db; + int rc; + int i; + sqlite3_int64 x; + char *zErr = 0; + + if( eVerbosity>=1 ){ + printf("************** nByte=%d ***************\n", (int)nByte); + fflush(stdout); + } + if( sqlite3_initialize() ) return 0; + rc = sqlite3_open(0, &db); + if( rc ) return 1; + a = sqlite3_malloc64(nByte+1); + if( a==0 ) return 1; + memcpy(a, aData, nByte); + sqlite3_deserialize(db, "main", a, nByte, nByte, + SQLITE_DESERIALIZE_RESIZEABLE | + SQLITE_DESERIALIZE_FREEONCLOSE); + x = szMax; +#ifdef SQLITE_FCNTL_SIZE_LIMIT + sqlite3_file_control(db, "main", SQLITE_FCNTL_SIZE_LIMIT, &x); +#endif + if( bVdbeDebug ){ + sqlite3_exec(db, "PRAGMA vdbe_debug=ON", 0, 0, 0); + } + if( mxCb>0 ){ + sqlite3_progress_handler(db, 10, progress_handler, 0); + } +#ifdef SQLITE_TESTCTRL_PRNG_SEED + sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SEED, 1, db); +#endif + for(i=0; i<sizeof(azSql)/sizeof(azSql[0]); i++){ + if( eVerbosity>=1 ){ + printf("%s\n", azSql[i]); + fflush(stdout); + } + zErr = 0; + nCb = 0; + rc = sqlite3_exec(db, azSql[i], 0, 0, &zErr); + if( rc && eVerbosity>=1 ){ + printf("-- rc=%d zErr=%s\n", rc, zErr); + } + sqlite3_free(zErr); + } + rc = sqlite3_close(db); + if( rc!=SQLITE_OK ){ + fprintf(stdout, "sqlite3_close() returns %d\n", rc); + } + if( sqlite3_memory_used()!=0 ){ + int nAlloc = 0; + int nNotUsed = 0; + sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &nAlloc, &nNotUsed, 0); + fprintf(stderr,"Memory leak: %lld bytes in %d allocations\n", + sqlite3_memory_used(), nAlloc); + exit(1); + } + return 0; +} + +/* +** Return the number of "v" characters in a string. Return 0 if there +** are any characters in the string other than "v". +*/ +static int numberOfVChar(const char *z){ + int N = 0; + while( z[0] && z[0]=='v' ){ + z++; + N++; + } + return z[0]==0 ? N : 0; +} + +/* libFuzzer invokes this routine once when the executable starts, to +** process the command-line arguments. +*/ +int LLVMFuzzerInitialize(int *pArgc, char ***pArgv){ + int i, j, n; + int argc = *pArgc; + char **argv = *pArgv; + for(i=j=1; i<argc; i++){ + char *z = argv[i]; + if( z[0]=='-' ){ + z++; + if( z[0]=='-' ) z++; + if( z[0]=='v' && (n = numberOfVChar(z))>0 ){ + eVerbosity += n; + continue; + } + if( strcmp(z,"vdbe-debug")==0 ){ + bVdbeDebug = 1; + continue; + } + if( strcmp(z,"limit")==0 ){ + if( i+1==argc ){ + fprintf(stderr, "missing argument to %s\n", argv[i]); + exit(1); + } + mxCb = strtol(argv[++i], 0, 0); + continue; + } + if( strcmp(z,"memtrace")==0 ){ + sqlite3MemTraceActivate(stdout); + continue; + } + if( strcmp(z,"max-db-size")==0 ){ + if( i+1==argc ){ + fprintf(stderr, "missing argument to %s\n", argv[i]); + exit(1); + } + szMax = strtol(argv[++i], 0, 0); + continue; + } + if( strcmp(z, "lookaside")==0 ){ + int sz, nSlot; + if( i+2>=argc ){ + fprintf(stderr, + "--lookaside requires two arguments: slot-size num-slots\n"); + exit(1); + } + sz = atoi(argv[++i]); + nSlot = atoi(argv[++i]); + sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, nSlot); + continue; + } +#ifndef _WIN32 + if( strcmp(z,"max-stack")==0 + || strcmp(z,"max-data")==0 + || strcmp(z,"max-as")==0 + ){ + struct rlimit x,y; + int resource = RLIMIT_STACK; + char *zType = "RLIMIT_STACK"; + if( i+1==argc ){ + fprintf(stderr, "missing argument to %s\n", argv[i]); + exit(1); + } + if( z[4]=='d' ){ + resource = RLIMIT_DATA; + zType = "RLIMIT_DATA"; + } + if( z[4]=='a' ){ + resource = RLIMIT_AS; + zType = "RLIMIT_AS"; + } + memset(&x,0,sizeof(x)); + getrlimit(resource, &x); + y.rlim_cur = atoi(argv[++i]); + y.rlim_max = x.rlim_cur; + setrlimit(resource, &y); + memset(&y,0,sizeof(y)); + getrlimit(resource, &y); + printf("%s changed from %d to %d\n", + zType, (int)x.rlim_cur, (int)y.rlim_cur); + continue; + } +#endif /* _WIN32 */ + } + argv[j++] = argv[i]; + } + argv[j] = 0; + *pArgc = j; + return 0; +} + +#ifdef STANDALONE +/* +** Read an entire file into memory. Space to hold the file comes +** from malloc(). +*/ +static unsigned char *readFile(const char *zName, int *pnByte){ + FILE *in = fopen(zName, "rb"); + long nIn; + size_t nRead; + unsigned char *pBuf; + if( in==0 ) return 0; + fseek(in, 0, SEEK_END); + nIn = ftell(in); + rewind(in); + pBuf = malloc( nIn+1 ); + if( pBuf==0 ){ fclose(in); return 0; } + nRead = fread(pBuf, nIn, 1, in); + fclose(in); + if( nRead!=1 ){ + free(pBuf); + return 0; + } + pBuf[nIn] = 0; + if( pnByte ) *pnByte = nIn; + return pBuf; +} +#endif /* STANDALONE */ + +#ifdef STANDALONE +int main(int argc, char **argv){ + int i; + LLVMFuzzerInitialize(&argc, &argv); + for(i=1; i<argc; i++){ + unsigned char *pIn; + int nIn; + pIn = readFile(argv[i], &nIn); + if( pIn ){ + LLVMFuzzerTestOneInput((const uint8_t*)pIn, (size_t)nIn); + free(pIn); + } + } +#ifdef RUSAGE_SELF + if( eVerbosity>0 ){ + struct rusage x; + printf("SQLite %s\n", sqlite3_sourceid()); + memset(&x, 0, sizeof(x)); + if( getrusage(RUSAGE_SELF, &x)==0 ){ + printf("Maximum RSS = %ld KB\n", x.ru_maxrss); + } + } +#endif + return 0; +} +#endif /*STANDALONE*/ diff --git a/test/dblwidth-a.sql b/test/dblwidth-a.sql new file mode 100644 index 0000000000..38c219698d --- /dev/null +++ b/test/dblwidth-a.sql @@ -0,0 +1,20 @@ +/* +** Run this script using "sqlite3" to confirm that the command-line +** shell properly handles the output of double-width characters. +** +** https://sqlite.org/forum/forumpost/008ac80276 +*/ +.mode box +CREATE TABLE data(word TEXT, description TEXT); +INSERT INTO data VALUES('〈οὐκέτι〉','Greek without dblwidth <...>'); +.print .mode box +SELECT * FROM data; +.mode table +.print .mode table +SELECT * FROM data; +.mode qbox +.print .mode qbox +SELECT * FROM data; +.mode column +.print .mode column +SELECT * FROM data; diff --git a/test/dbpage.test b/test/dbpage.test new file mode 100644 index 0000000000..8039e0e1be --- /dev/null +++ b/test/dbpage.test @@ -0,0 +1,255 @@ +# 2017-10-11 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is testing the sqlite_dbpage virtual table. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix dbpage + +ifcapable !vtab||!compound { + finish_test + return +} + +sqlite3_db_config db DEFENSIVE 0 +do_test 100 { + execsql { + PRAGMA auto_vacuum=0; + PRAGMA page_size=4096; + PRAGMA journal_mode=WAL; + } + execsql { + CREATE TABLE t1(a,b); + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<100) + INSERT INTO t1(a,b) SELECT x, printf('%d-x%.*c',x,x,'x') FROM c; + PRAGMA integrity_check; + } +} {ok} +do_execsql_test 110 { + SELECT pgno, quote(substr(data,1,5)) FROM sqlite_dbpage('main') ORDER BY pgno; +} {1 X'53514C6974' 2 X'0500000001' 3 X'0D0000004E' 4 X'0D00000016'} +do_execsql_test 120 { + SELECT pgno, quote(substr(data,1,5)) FROM sqlite_dbpage WHERE pgno=2; +} {2 X'0500000001'} +do_execsql_test 130 { + SELECT pgno, quote(substr(data,1,5)) FROM sqlite_dbpage WHERE pgno=4; +} {4 X'0D00000016'} +do_execsql_test 140 { + SELECT pgno, quote(substr(data,1,5)) FROM sqlite_dbpage WHERE pgno=5; +} {} +do_execsql_test 150 { + SELECT pgno, quote(substr(data,1,5)) FROM sqlite_dbpage WHERE pgno=0; +} {} +do_execsql_test 160 { + ATTACH ':memory:' AS aux1; + PRAGMA aux1.page_size=4096; + CREATE TABLE aux1.t2(a,b,c); + INSERT INTO t2 VALUES(11,12,13); + SELECT pgno, quote(substr(data,1,5)) FROM sqlite_dbpage('aux1'); +} {1 X'53514C6974' 2 X'0D00000001'} +do_execsql_test 170 { + CREATE TABLE aux1.x3(x,y,z); + INSERT INTO x3(x,y,z) VALUES(1,'main',1),(2,'aux1',1); + SELECT pgno, schema, substr(data,1,6) + FROM sqlite_dbpage, x3 + WHERE sqlite_dbpage.schema=x3.y AND sqlite_dbpage.pgno=x3.z + ORDER BY x3.x; +} {1 main SQLite 1 aux1 SQLite} + +do_execsql_test 200 { + CREATE TEMP TABLE saved_content(x); + INSERT INTO saved_content(x) SELECT data FROM sqlite_dbpage WHERE pgno=4; + UPDATE sqlite_dbpage SET data=zeroblob(4096) WHERE pgno=4; +} {} +do_catchsql_test 210 { + PRAGMA integrity_check; +} {1 {database disk image is malformed}} +do_execsql_test 220 { + SELECT pgno, quote(substr(data,1,5)) FROM sqlite_dbpage('main') ORDER BY pgno; +} {1 X'53514C6974' 2 X'0500000001' 3 X'0D0000004E' 4 X'0000000000'} +do_execsql_test 230 { + UPDATE sqlite_dbpage SET data=(SELECT x FROM saved_content) WHERE pgno=4; +} {} +do_catchsql_test 230 { + PRAGMA integrity_check; +} {0 ok} +do_execsql_test 240 { + DELETE FROM saved_content; + INSERT INTO saved_content(x) + SELECT data FROM sqlite_dbpage WHERE schema='aux1' AND pgno=2; +} {} +do_execsql_test 241 { + UPDATE sqlite_dbpage SET data=zeroblob(4096) WHERE pgno=2 AND schema='aux1'; +} {} +do_catchsql_test 250 { + PRAGMA aux1.integrity_check; +} {1 {database disk image is malformed}} +do_execsql_test 260 { + UPDATE sqlite_dbpage SET data=(SELECT x FROM saved_content) + WHERE pgno=2 AND schema='aux1'; +} {} +do_catchsql_test 270 { + PRAGMA aux1.integrity_check; +} {0 ok} + +db close +sqlite3 db :memory: +do_execsql_test 300 { + SELECT * FROM sqlite_temp_schema, sqlite_dbpage; +} {} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 400 { + ATTACH ':memory:' AS aux1; + BEGIN; + CREATE VIRTUAL TABLE aux1.t1 USING sqlite_dbpage; + INSERT INTO t1 VALUES(17, NULL); + COMMIT; +} + +#------------------------------------------------------------------------- +reset_db +forcedelete test.db2 +sqlite3 db2 test.db2 +db2 eval { + PRAGMA auto_vacuum=NONE; + CREATE TABLE t1(x, y); +} + +do_execsql_test 500 { + PRAGMA auto_vacuum=NONE; + CREATE TABLE x1(a); + INSERT INTO x1 VALUES( hex(randomblob(2000)) ); + INSERT INTO x1 VALUES( hex(randomblob(2000)) ); + INSERT INTO x1 VALUES( hex(randomblob(2000)) ); + INSERT INTO x1 VALUES( hex(randomblob(2000)) ); + PRAGMA page_count; +} {18} + +do_test 510 { + db eval BEGIN + db2 eval { PRAGMA page_count } { + db eval { + INSERT INTO sqlite_dbpage values($page_count, NULL); + } + } + db2 eval { SELECT pgno, data FROM sqlite_dbpage } { + db eval { + INSERT INTO sqlite_dbpage values($pgno, $data); + } + } + + db eval COMMIT +} {} + +db close +sqlite3 db test.db + +do_execsql_test 520 { + PRAGMA page_count; + SELECT * FROM t1; +} {2} + +db2 close + +#------------------------------------------------------------------------- +reset_db +forcedelete test.db2 +do_execsql_test 610 { + ATTACH 'test.db2' AS aux; + CREATE TABLE t1(x); + CREATE TABLE t2(y); + INSERT INTO t1 VALUES(1234); + CREATE TABLE aux.x1(z); +} + +set pgno [db one {SELECT max(rootpage) FROM sqlite_schema}] +sqlite3 db2 test.db2 +db2 eval { + BEGIN; + SELECT * FROM x1; +} + +do_catchsql_test 620 { + UPDATE sqlite_dbpage SET data = ( + SELECT data FROM sqlite_dbpage WHERE pgno=$pgno-1 + ) WHERE pgno = $pgno; +} {1 {database is locked}} + +db2 eval { + COMMIT; +} + +do_catchsql_test 630 { + UPDATE sqlite_dbpage SET data = ( + SELECT data FROM sqlite_dbpage WHERE pgno=$pgno-1 + ) WHERE pgno = $pgno; +} {0 {}} + +db close +sqlite3 db test.db + +do_execsql_test 640 { + SELECT * FROM t2; +} {1234} + +db2 close + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 700 { + CREATE TABLE t1(x); + INSERT INTO t1 VALUES( hex(randomblob(1000)) ); + INSERT INTO t1 VALUES( hex(randomblob(1000)) ); + INSERT INTO t1 VALUES( hex(randomblob(1000)) ); +} + +forcedelete test.db2 +sqlite3 db2 test.db2 +db2 eval { + CREATE TABLE y1(y); + INSERT INTO y1 VALUES( hex(randomblob(1000)) ); +} + +set max [db2 one {PRAGMA page_count}] + +do_test 710 { + execsql { + BEGIN; + } + + for {set ii 1} {$ii <= $max} {incr ii} { + set data [db2 one {SELECT data FROM sqlite_dbpage WHERE pgno=$ii}] + execsql { + UPDATE sqlite_dbpage SET data=$data WHERE pgno=$ii + } + } + + execsql { + SAVEPOINT abc; + INSERT INTO sqlite_dbpage VALUES(2, NULL); + ROLLBACK TO abc; + COMMIT; + } +} {} + +db close +sqlite3 db test.db + +do_execsql_test 720 { + PRAGMA integrity_check +} {ok} + + +finish_test diff --git a/test/dbpagefault.test b/test/dbpagefault.test new file mode 100644 index 0000000000..e5b246fc94 --- /dev/null +++ b/test/dbpagefault.test @@ -0,0 +1,112 @@ +# 2022 July 06 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/lock_common.tcl +source $testdir/malloc_common.tcl + +if {[permutation] == "inmemory_journal"} { + finish_test + return +} + +ifcapable !vtab { + finish_test + return +} + +set testprefix dbpagefault + +faultsim_save_and_close +do_faultsim_test 1 -prep { + faultsim_restore_and_reopen + execsql { ATTACH 'test.db2' AS aux; } +} -body { + execsql { + CREATE VIRTUAL TABLE t1 USING sqlite_dbpage(); + } +} -test { + execsql { PRAGMA journal_mode = off } + faultsim_test_result {0 {}} +} + +do_faultsim_test 2 -prep { + sqlite3 db "xyz.db" -vfs memdb + execsql { ATTACH 'test.db2' AS aux; } +} -body { + execsql { + CREATE VIRTUAL TABLE t1 USING sqlite_dbpage(); + UPDATE t1 SET data=zeroblob(1024) WHERE pgno=1 AND schema='aux'; + } +} -test { + execsql { PRAGMA journal_mode = off } + faultsim_test_result {0 {}} {1 {no such schema}} {1 {SQL logic error}} {1 {unable to open a temporary database file for storing temporary tables}} +} + +reset_db +do_execsql_test 3.0 { + CREATE TABLE x1(z, b); + CREATE TRIGGER BEFORE INSERT ON x1 BEGIN + DELETE FROM sqlite_dbpage WHERE pgno=100; + UPDATE sqlite_dbpage SET data=null WHERE pgno=100; + END; +} + +# This test case no longer works, as it is no longer possible to use +# virtual table sqlite_dbpage from within a trigger. +# +do_execsql_test 3.1 { + PRAGMA trusted_schema = 1; +} +do_catchsql_test 3.2 { + PRAGMA trusted_schema = 1; + INSERT INTO x1 DEFAULT VALUES; +} {1 {unsafe use of virtual table "sqlite_dbpage"}} +#do_faultsim_test 3 -prep { +# catch { db close } +# sqlite3 db test.db +# execsql { PRAGMA trusted_schema = 1 } +#} -body { +# execsql { INSERT INTO x1 DEFAULT VALUES; } +#} -test { +# faultsim_test_result {0 {}} +#} + +reset_db +forcedelete test.db2 +do_execsql_test 4.0 { + CREATE TABLE t1(x); + INSERT INTO t1 VALUES('one'); + CREATE TABLE t2(x); + INSERT INTO t2 VALUES('two'); + ATTACH 'test.db2' AS aux; + CREATE TABLE aux.x1(x); +} + +set pgno [db one {SELECT max(rootpage) FROM sqlite_schema}] + +faultsim_save_and_close +do_faultsim_test 4 -prep { + faultsim_restore_and_reopen + execsql { ATTACH 'test.db2' AS aux; } +} -body { + execsql { + UPDATE sqlite_dbpage SET data = ( + SELECT data FROM sqlite_dbpage WHERE pgno=($pgno-1) + ) WHERE pgno = $pgno; + } +} -test { + faultsim_test_result {0 {}} {1 {unable to open a temporary database file for storing temporary tables}} +} + +finish_test diff --git a/test/dbstatus.test b/test/dbstatus.test index 368c6b28e5..8d5d8349cf 100644 --- a/test/dbstatus.test +++ b/test/dbstatus.test @@ -14,6 +14,7 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl +set testprefix dbstatus ifcapable !compound { finish_test @@ -24,6 +25,7 @@ ifcapable !compound { db close sqlite3_shutdown sqlite3_config_memstatus 1 +sqlite3_config_uri 1 sqlite3_initialize sqlite3 db test.db @@ -61,22 +63,18 @@ proc lookaside {db} { } } -ifcapable stat4||stat3 { +ifcapable stat4 { set STAT3 1 } else { set STAT3 0 } -ifcapable malloc_usable_size { - finish_test - return -} - #--------------------------------------------------------------------------- # Run the dbstatus-2 and dbstatus-3 tests with several of different # lookaside buffer sizes. # foreach ::lookaside_buffer_size {0 64 120} { + ifcapable malloc_usable_size break # Do not run any of these tests if there is SQL configured to run # as part of the [sqlite3] command. This prevents the script from @@ -377,4 +375,83 @@ foreach ::lookaside_buffer_size {0 64 120} { } } +#------------------------------------------------------------------------- +# The following tests focus on DBSTATUS_CACHE_USED_SHARED +# +ifcapable shared_cache { + if {([permutation]=="memsys3" + || [permutation]=="memsys5" + || $::tcl_platform(os)=="Linux") && ![sqlite3 -has-codec]} { + proc do_cacheused_test {tn db res} { + set cu [sqlite3_db_status $db SQLITE_DBSTATUS_CACHE_USED 0] + set pcu [sqlite3_db_status $db SQLITE_DBSTATUS_CACHE_USED_SHARED 0] + set cu [lindex $cu 1] + set pcu [lindex $pcu 1] + uplevel [list do_test $tn [list list $cu $pcu] "#/$res/"] + } + reset_db + sqlite3 db file:test.db?cache=shared + + do_execsql_test 4.0 { + PRAGMA auto_vacuum=NONE; + CREATE TABLE t1(a, b, c); + INSERT INTO t1 VALUES(1, 2, 3); + } + do_cacheused_test 4.0.1 db { 4568 4568 } + do_execsql_test 4.1 { + CREATE TEMP TABLE tt(a, b, c); + INSERT INTO tt VALUES(1, 2, 3); + } + do_cacheused_test 4.1.1 db { 9000 9000 } + + sqlite3 db2 file:test.db?cache=shared + do_cacheused_test 4.2.1 db2 { 4568 2284 } + do_cacheused_test 4.2.2 db { 9000 6716 } + db close + do_cacheused_test 4.2.3 db2 { 4568 4568 } + sqlite3 db file:test.db?cache=shared + do_cacheused_test 4.2.4 db2 { 4568 2284 } + db2 close + } +} + +#------------------------------------------------------------------------- +# Test that passing an out-of-range value to sqlite3_stmt_status does +# not cause a crash. +reset_db +do_execsql_test 5.0 { + CREATE TABLE t1(x, y); + INSERT INTO t1 VALUES(1, 2); + INSERT INTO t1 VALUES(3, 4); +} + +do_test 5.1 { + set ::stmt [sqlite3_prepare db "SELECT * FROM t1" -1 dummy] + sqlite3_step $::stmt + sqlite3_step $::stmt + sqlite3_step $::stmt + sqlite3_reset $::stmt +} {SQLITE_OK} + +ifcapable api_armor { + do_test 5.2 { sqlite3_stmt_status $::stmt -1 0 } 0 +} +do_test 5.3 { sqlite3_stmt_status $::stmt 0 0 } 0 +do_test 5.4 { + expr [sqlite3_stmt_status $::stmt 99 0]>0 +} 1 +foreach {tn id res} { + 1 SQLITE_STMTSTATUS_MEMUSED 1 + 2 SQLITE_STMTSTATUS_FULLSCAN_STEP 1 + 3 SQLITE_STMTSTATUS_SORT 0 + 4 SQLITE_STMTSTATUS_AUTOINDEX 0 + 5 SQLITE_STMTSTATUS_VM_STEP 1 + 6 SQLITE_STMTSTATUS_REPREPARE 0 + 7 SQLITE_STMTSTATUS_RUN 1 +} { +if {$tn==2} breakpoint + do_test 5.5.$tn { expr [sqlite3_stmt_status $::stmt $id 0]>0 } $res +} + +sqlite3_finalize $::stmt finish_test diff --git a/test/dbstatus2.test b/test/dbstatus2.test index 2541a1a823..526d8aa17b 100644 --- a/test/dbstatus2.test +++ b/test/dbstatus2.test @@ -37,6 +37,14 @@ proc db_write {db {reset 0}} { sqlite3_db_status $db CACHE_WRITE $reset } +proc db_spill {db {reset 0}} { + sqlite3_db_status $db CACHE_SPILL $reset +} + +proc db_temp_spill {db {reset 0}} { + sqlite3_db_status $db TEMPBUF_SPILL $reset +} + do_test 1.1 { db close sqlite3 db test.db @@ -86,7 +94,7 @@ do_test 2.3 { db_write db 1 } {0 4 0} do_test 2.4 { db_write db 0 } {0 0 0} do_test 2.5 { db_write db 1 } {0 0 0} -ifcapable wal { +if {[wal_is_capable]} { do_test 2.6 { execsql { PRAGMA journal_mode = WAL } db_write db 1 @@ -98,5 +106,59 @@ do_test 2.7 { } {0 4 0} do_test 2.8 { db_write db 1 } {0 4 0} do_test 2.9 { db_write db 0 } {0 0 0} + +do_test 3.0 { db_spill db 1 } {0 0 0} +do_test 3.1 { db_spill db 0 } {0 0 0} +do_execsql_test 3.2 { + PRAGMA journal_mode=DELETE; + PRAGMA cache_size=3; + UPDATE t1 SET b=randomblob(1000); +} {delete} +do_test 3.3 { db_spill db 0 } {0 8 0} + + +if {$::TEMP_STORE<3} { + do_execsql_test 4.0 { + PRAGMA temp_store = file; + PRAGMA cache_size = -1024; + } {} + + do_test 4.1 { db_temp_spill db 0 } {0 0 0} + + do_execsql_test 4.2 { + CREATE TABLE data(a INTEGER, b BLOB); + + -- Insert 5-6 MB of data. + WITH s(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<75000 ) + INSERT INTO data SELECT i, hex(randomblob(50)) FROM s; + } + + do_test 4.3 { db_temp_spill db 0 } {0 0 0} + + do_test 4.4 { + execsql { SELECT a, b FROM data ORDER BY a } + set nTmpSpill [lindex [db_temp_spill db 1] 1] + expr {($nTmpSpill>7*1000*1000) && ($nTmpSpill<10*1000*1000)?"ok":$nTmpSpill} + } ok + + # The previous test case reset the status value. + do_test 4.5 { db_temp_spill db 0 } {0 0 0} + + do_test 4.6 { + execsql { CREATE INDEX i1 ON data(a) } + set nTmpSpill [lindex [db_temp_spill db 1] 1] + expr {($nTmpSpill>384*1000) && ($nTmpSpill<768*1000)?"ok":$nTmpSpill} + } ok + + # The previous test case reset the status value. + do_test 4.7 { db_temp_spill db 0 } {0 0 0} + + # Same query as in (4.4). Now does not require temp space. + do_test 4.8 { + execsql { SELECT a, b FROM data ORDER BY a } + db_temp_spill db 0 + } {0 0 0} +} + finish_test diff --git a/test/decimal.test b/test/decimal.test new file mode 100644 index 0000000000..07510faa1e --- /dev/null +++ b/test/decimal.test @@ -0,0 +1,212 @@ +# 2017 December 9 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix decimal + +if {[catch {load_static_extension db decimal} error]} { + puts "Skipping decimal tests, hit load error: $error" + finish_test; return +} + +do_execsql_test 1000 { + SELECT decimal(1); +} {1} +do_execsql_test 1001 { + SELECT decimal('+0'), decimal('-0'); +} {0 0} +do_execsql_test 1010 { + SELECT decimal('1.0'); +} {1.0} +do_execsql_test 1020 { + SELECT decimal('0001.0'); +} {1.0} +do_execsql_test 1030 { + SELECT decimal('+0001.0'); +} {1.0} +do_execsql_test 1040 { + SELECT decimal('-0001.0'); +} {-1.0} +do_execsql_test 1041 { + SELECT decimal('-0000.0'); +} {0.0} +do_execsql_test 1042 { + SELECT decimal(0.0)==decimal(-0.0); +} {1} +do_execsql_test 1050 { + SELECT decimal('1.0e72'); +} {1000000000000000000000000000000000000000000000000000000000000000000000000} +# 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123 +do_execsql_test 1060 { + SELECT decimal('1.0e-72'); +} {0.0000000000000000000000000000000000000000000000000000000000000000000000010} +# 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123 +do_execsql_test 1070 { + SELECT decimal('-123e-4'); +} {-0.0123} +do_execsql_test 1080 { + SELECT decimal('+123e+4'); +} {1230000} +do_execsql_test 1081 { + SELECT decimal_exp('+123e+4'); +} {+1.23e+06} + + +do_execsql_test 2000 { + CREATE TABLE t1(seq INTEGER PRIMARY KEY, val TEXT); + INSERT INTO t1 VALUES + (1, '-9999e99'), + (2, '-9998.000e+99'), + (3, '-9999.0'), + (4, '-1'), + (5, '-9999e-20'), + (6, '0'), + (7, '1e-30'), + (8, '1e-29'), + (9, '1'), + (10,'1.00000000000000001'), + (11,'+1.00001'), + (12,'99e+99'); + SELECT *, '|' + FROM t1 AS a, t1 AS b + WHERE a.seq<b.seq + AND decimal_cmp(a.val,b.val)>=0; +} {} +do_execsql_test 2001 { + WITH vx(a,b) AS (VALUES + ('-0','+0'), + ('-000.000','0'), + ('1.2','1.2000') + ) + SELECT *, '|' FROM vx + WHERE decimal_cmp(a,b)!=0 + OR decimal_cmp(b,a)!=0; +} {} +do_execsql_test 2010 { + SELECT *, '|' + FROM t1 AS a, t1 AS b + WHERE a.seq<>b.seq + AND decimal_cmp(a.val,b.val)==0; +} {} +do_execsql_test 2020 { + SELECT *, '|' + FROM t1 AS a, t1 AS b + WHERE a.seq>b.seq + AND decimal_cmp(a.val,b.val)<=0; +} {} +do_execsql_test 2030 { + SELECT seq FROM t1 ORDER BY val COLLATE decimal; +} {1 2 3 4 5 6 7 8 9 10 11 12} +do_execsql_test 2040 { + SELECT seq FROM t1 ORDER BY val COLLATE decimal DESC; +} {12 11 10 9 8 7 6 5 4 3 2 1} + +do_execsql_test 3000 { + CREATE TABLE t3(seq INTEGER PRIMARY KEY, val TEXT); + WITH RECURSIVE c(x) AS (VALUES(1) UNION SELECT x+1 FROM c WHERE x<10) + INSERT INTO t3(seq, val) SELECT x, x FROM c; + WITH RECURSIVE c(x) AS (VALUES(1) UNION SELECT x+1 FROM c WHERE x<5) + INSERT INTO t3(seq, val) SELECT x+10, x*1000 FROM c; + SELECT decimal(val) FROM t3 ORDER BY seq; +} {1 2 3 4 5 6 7 8 9 10 1000 2000 3000 4000 5000} +do_execsql_test 3020 { + SELECT decimal_add(val,'0.5') FROM t3 WHERE seq>5 ORDER BY seq +} {6.5 7.5 8.5 9.5 10.5 1000.5 2000.5 3000.5 4000.5 5000.5} +do_execsql_test 3030 { + SELECT decimal_add(val,'-10') FROM t3 ORDER BY seq; +} {-9 -8 -7 -6 -5 -4 -3 -2 -1 0 990 1990 2990 3990 4990} + +do_execsql_test 4000 { + SELECT decimal_sum(val) FROM t3; +} {15055} +do_execsql_test 4010 { + SELECT decimal_sum(decimal_add(val,val||'e+10')) FROM t3; +} {150550000015055} +do_execsql_test 4010 { + SELECT decimal_sum(decimal_add(val||'e+20',decimal_add(val,val||'e-20'))) + FROM t3; +} {1505500000000000000015055.00000000000000015055} + +do_execsql_test 5000 { + WITH RECURSIVE c(x,y,z) AS ( + VALUES(0,'1','1') + UNION ALL + SELECT x+1, decimal_mul(y,'2'), decimal_mul(z,'0.5') + FROM c WHERE x<32 + ) + SELECT count(*) FROM c WHERE decimal_mul(y,z)='1'; +} {33} + +do_execsql_test 5100 { + SELECT decimal_mul('1234.00','2.00'); +} {2468.00} +do_execsql_test 5101 { + SELECT decimal_mul('1234.00','2.0000'); +} {2468.00} +do_execsql_test 5102 { + SELECT decimal_mul('1234.0000','2.000'); +} {2468.000} +do_execsql_test 5103 { + SELECT decimal_mul('1234.0000','2'); +} {2468} + +if {[catch {load_static_extension db ieee754} error]} { + puts "Skipping ieee754 tests, hit load error: $error" + finish_test; return +} + +do_execsql_test 6000 { + CREATE TABLE pow2(x INTEGER PRIMARY KEY, v TEXT); + WITH RECURSIVE c(x,v) AS ( + VALUES(0,'1') + UNION ALL + SELECT x+1, decimal_mul(v,'2') FROM c WHERE x+1<=971 + ) INSERT INTO pow2(x,v) SELECT x, v FROM c; + WITH RECURSIVE c(x,v) AS ( + VALUES(-1,'0.5') + UNION ALL + SELECT x-1, decimal_mul(v,'0.5') FROM c WHERE x-1>=-1075 + ) INSERT INTO pow2(x,v) SELECT x, v FROM c; +} {} +do_execsql_test 6010 { + WITH c(n) AS (SELECT ieee754_from_blob(x'0000000000000001')) +SELECT decimal_mul(ieee754_mantissa(c.n),pow2.v) + FROM pow2, c WHERE pow2.x=ieee754_exponent(c.n); +} {0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004940656458412465441765687928682213723650598026143247644255856825006755072702087518652998363616359923797965646954457177309266567103559397963987747960107818781263007131903114045278458171678489821036887186360569987307230500063874091535649843873124733972731696151400317153853980741262385655911710266585566867681870395603106249319452715914924553293054565444011274801297099995419319894090804165633245247571478690147267801593552386115501348035264934720193790268107107491703332226844753335720832431936092382893458368060106011506169809753078342277318329247904982524730776375927247874656084778203734469699533647017972677717585125660551199131504891101451037862738167250955837389733598993664809941164205702637090279242767544565229087538682506419718265533447265625} +do_execsql_test 6011 { + WITH c(n) AS (SELECT ieee754_from_blob(x'0000000000000001')) +SELECT decimal(c.n) FROM c; +} {0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004940656458412465441765687928682213723650598026143247644255856825006755072702087518652998363616359923797965646954457177309266567103559397963987747960107818781263007131903114045278458171678489821036887186360569987307230500063874091535649843873124733972731696151400317153853980741262385655911710266585566867681870395603106249319452715914924553293054565444011274801297099995419319894090804165633245247571478690147267801593552386115501348035264934720193790268107107491703332226844753335720832431936092382893458368060106011506169809753078342277318329247904982524730776375927247874656084778203734469699533647017972677717585125660551199131504891101451037862738167250955837389733598993664809941164205702637090279242767544565229087538682506419718265533447265625} +do_execsql_test 6020 { + WITH c(n) AS (SELECT ieee754_from_blob(x'7fefffffffffffff')) +SELECT decimal_mul(ieee754_mantissa(c.n),pow2.v) + FROM pow2, c WHERE pow2.x=ieee754_exponent(c.n); +} {179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368} + +do_execsql_test 6100 { + SELECT ieee754(ieee754_from_blob(x'0000000000000001')); +} {ieee754(1,-1074)} +do_execsql_test 6110 { + SELECT ieee754(ieee754_from_blob(x'7fefffffffffffff')); +} {ieee754(9007199254740991,971)} +do_execsql_test 6120 { + SELECT printf('%.8e',ieee754_from_blob(x'0000000000000001')); +} {4.94065646e-324} +do_execsql_test 6130 { + SELECT printf('%.8e',ieee754_from_blob(x'ffefffffffffffff')); +} {-1.79769313e+308} + + + + +finish_test diff --git a/test/default.test b/test/default.test index 406eb53677..192b3d2ff3 100644 --- a/test/default.test +++ b/test/default.test @@ -106,6 +106,7 @@ do_execsql_test default-3.3 { db close forcedelete test.db sqlite3 db test.db +sqlite3_db_config db DEFENSIVE 0 do_execsql_test default-4.0 { CREATE TABLE t1(a TEXT, b TEXT DEFAULT(99)); PRAGMA writable_schema=ON; @@ -127,4 +128,17 @@ do_catchsql_test default-4.4 { CREATE TABLE t2(a TEXT, b TEXT DEFAULT(98+coalesce(5,:xyz))); } {1 {default value of column [b] is not constant}} +# 2020-03-09 out-of-bounds memory access discovered by "Eternal Sakura" +# and reported to chromium. +# +reset_db +do_catchsql_test default-5.1 { + CREATE TABLE t1 (a,b DEFAULT(random() NOTNULL IN (RAISE(IGNORE),2,3))); + INSERT INTO t1(a) VALUES(1); +} {1 {default value of column [b] is not constant}} +do_catchsql_test default-5.2 { + CREATE TABLE Table0 (Col0 DEFAULT (RAISE(IGNORE) ) ) ; + INSERT INTO Table0 DEFAULT VALUES ; +} {1 {default value of column [Col0] is not constant}} + finish_test diff --git a/test/delete.test b/test/delete.test index d2dc106495..214bae6f70 100644 --- a/test/delete.test +++ b/test/delete.test @@ -389,5 +389,55 @@ do_test delete-9.5 { set res } {1 a b 1 c d 2 a b 2 c d} +do_execsql_test delete-10.0 { + CREATE TABLE t1(a INT UNIQUE, b INT); + INSERT INTO t1(a,b) VALUES('1','2'); + SELECT * FROM t1 WHERE a='1' AND b='2'; +} {1 2} + +do_execsql_test delete-10.1 { + DELETE FROM t1 WHERE a='1' AND b='2'; +} + +do_execsql_test delete-10.2 { + SELECT * FROM t1 WHERE a='1' AND b='2'; +} + +do_execsql_test delete-11.0 { + CREATE TABLE t11(a INTEGER PRIMARY KEY, b INT); + WITH RECURSIVE cnt(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM cnt WHERE x<20) + INSERT INTO t11(a,b) SELECT x, (x*17)%100 FROM cnt; + SELECT * FROM t11; +} {1 17 2 34 3 51 4 68 5 85 6 2 7 19 8 36 9 53 10 70 11 87 12 4 13 21 14 38 15 55 16 72 17 89 18 6 19 23 20 40} +do_execsql_test delete-11.1 { + DELETE FROM t11 AS xyz + WHERE EXISTS(SELECT 1 FROM t11 WHERE t11.a>xyz.a AND t11.b<=xyz.b); + SELECT * FROM t11; +} {6 2 12 4 18 6 19 23 20 40} + + +# 2023-03-15 +# https://sqlite.org/forum/forumpost/e61252062c9d286d +# +# When the WHERE clause of a DELETE statement contains a subquery +# which uses the table that is being deleted from and there is a +# short-circuit operator of some kind in the WHERE clause such that +# the subquery might not run right away, then the subquery might +# run after one or more rows have been deleted, which can change +# the result of the subquery, and result in the wrong answer. +# +# Similar problem for UPDATE tested by update-21.4 +# https://sqlite.org/forum/forumpost/0007d1fdb1 +# +reset_db +do_execsql_test delete-12.0 { + CREATE TABLE t0(vkey INTEGER, pkey INTEGER,c1 INTEGER); + INSERT INTO t0 VALUES(2,1,-20),(2,2,NULL),(2,3,0),(8,4,95); + DELETE FROM t0 WHERE NOT ( + (t0.vkey <= t0.c1) AND + (t0.vkey <> (SELECT vkey FROM t0 ORDER BY vkey LIMIT 1 OFFSET 2)) + ); + SELECT * FROM t0; +} {8 4 95} finish_test diff --git a/test/delete4.test b/test/delete4.test index f3598a9496..34151446b1 100644 --- a/test/delete4.test +++ b/test/delete4.test @@ -59,14 +59,22 @@ do_execsql_test 2.3 { #------------------------------------------------------------------------- # reset_db -do_execsql_test 3.1 { +do_execsql_test 3.0.1 { CREATE TABLE t1(a, b, PRIMARY KEY(a, b)) WITHOUT ROWID; INSERT INTO t1 VALUES(1, 2); INSERT INTO t1 VALUES(2, 4); INSERT INTO t1 VALUES(1, 5); DELETE FROM t1 WHERE a=1; + SELECT printf('(%d)',changes()); SELECT * FROM t1; -} {2 4} +} {(2) 2 4} +do_execsql_test 3.0.2 { + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<100) + INSERT INTO t1(a,b) SELECT x, x+1 FROM c; + SELECT printf('(%d)',changes()); + DELETE FROM t1; + SELECT printf('(%d)',changes()); +} {(100) (101)} #------------------------------------------------------------------------- # DELETE statement that uses the OR optimization @@ -139,7 +147,126 @@ do_execsql_test 4.12 { PRAGMA integrity_check; } {ok} +# 2016-04-09 +# Ticket https://sqlite.org/src/info/a306e56ff68b8fa5 +# Failure to completely delete when reverse_unordered_selects is +# engaged. +# +db close +forcedelete test.db +sqlite3 db test.db +do_execsql_test 5.0 { + PRAGMA page_size=1024; + CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c); + CREATE INDEX x1 ON t1(b, c); + INSERT INTO t1(a,b,c) VALUES(1, 1, zeroblob(80)); + INSERT INTO t1(a,b,c) SELECT a+1, 1, c FROM t1; + INSERT INTO t1(a,b,c) SELECT a+2, 1, c FROM t1; + INSERT INTO t1(a,b,c) SELECT a+10, 2, c FROM t1 WHERE b=1; + INSERT INTO t1(a,b,c) SELECT a+20, 3, c FROM t1 WHERE b=1; + PRAGMA reverse_unordered_selects = ON; + DELETE FROM t1 WHERE b=2; + SELECT a FROM t1 WHERE b=2; +} {} + +# 2016-05-02 +# Ticket https://sqlite.org/src/tktview/dc6ebeda93960877 +# A subquery in the WHERE clause of a one-pass DELETE can cause an +# incorrect answer. +# +db close +forcedelete test.db +sqlite3 db test.db +do_execsql_test 6.0 { + CREATE TABLE t2(x INT); + INSERT INTO t2(x) VALUES(1),(2),(3),(4),(5); + DELETE FROM t2 WHERE EXISTS(SELECT 1 FROM t2 AS v WHERE v.x=t2.x-1); + SELECT x FROM t2; +} {1} +do_execsql_test 6.1 { + DROP TABLE IF EXISTS t2; + CREATE TABLE t2(x INT); + INSERT INTO t2(x) VALUES(1),(2),(3),(4),(5); + DELETE FROM t2 WHERE EXISTS(SELECT 1 FROM t2 AS v WHERE v.x=t2.x+1); + SELECT x FROM t2; +} {5} + +#------------------------------------------------------------------------- +# Test the effect of failing to find a table row based on an index key +# within a DELETE. Either because the db is corrupt, or a trigger on another +# row already deleted the entry, or because a BEFORE trigger on the current +# row has already deleted it. +# +do_execsql_test 7.1.0 { + CREATE TABLE t3(id INT PRIMARY KEY, a, b) WITHOUT ROWID; + CREATE INDEX t3a ON t3(a); + CREATE INDEX t3b ON t3(b); + + INSERT INTO t3 VALUES(1, 1, 1); + INSERT INTO t3 VALUES(2, 2, 2); + INSERT INTO t3 VALUES(3, 3, 3); + INSERT INTO t3 VALUES(4, 4, 1); +} +do_execsql_test 7.1.1 { + DELETE FROM t3 WHERE a=4 OR b=1; +} +do_execsql_test 7.1.2 { + SELECT * FROM t3; +} { 2 2 2 3 3 3 } + +do_execsql_test 7.2.0 { + CREATE TABLE t4(a PRIMARY KEY, b) WITHOUT ROWID; + CREATE INDEX t4i ON t4(b); + INSERT INTO t4 VALUES(1, 'hello'); + INSERT INTO t4 VALUES(2, 'world'); + + CREATE TABLE t5(a PRIMARY KEY, b) WITHOUT ROWID; + CREATE INDEX t5i ON t5(b); + INSERT INTO t5 VALUES(1, 'hello'); + INSERT INTO t5 VALUES(3, 'world'); + PRAGMA writable_schema = 1; + UPDATE sqlite_master SET rootpage = ( + SELECT rootpage FROM sqlite_master WHERE name = 't5' + ) WHERE name = 't4'; +} + +db close +sqlite3 db test.db +do_execsql_test 7.2.1 { + DELETE FROM t4 WHERE b='world' +} +reset_db + +do_execsql_test 7.3.0 { + CREATE TABLE t3(id INT PRIMARY KEY, a, b) WITHOUT ROWID; + INSERT INTO t3 VALUES(1, 2, 3); + INSERT INTO t3 VALUES(4, 5, 6); + INSERT INTO t3 VALUES(7, 8, 9); + CREATE TRIGGER t3t BEFORE DELETE ON t3 BEGIN + DELETE FROM t3 WHERE id=old.id+3; + END; +} + +do_execsql_test 7.3.1 { + DELETE FROM t3 WHERE a IN(2, 5, 8); + SELECT * FROM t3; +} {} + +do_execsql_test 7.3.2 { + DROP TRIGGER t3t; + INSERT INTO t3 VALUES(1, 2, 3); + INSERT INTO t3 VALUES(4, 5, 6); + INSERT INTO t3 VALUES(7, 8, 9); + CREATE TRIGGER t3t BEFORE DELETE ON t3 BEGIN + DELETE FROM t3 WHERE id=old.id; + END; +} + +do_execsql_test 7.3.3 { + DELETE FROM t3 WHERE a IN(2, 5, 8); + SELECT * FROM t3; +} {} finish_test diff --git a/test/delete_db.test b/test/delete_db.test new file mode 100644 index 0000000000..6edd9c242e --- /dev/null +++ b/test/delete_db.test @@ -0,0 +1,222 @@ +# 2016 September 10 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is testing the code in test_delete.c (the +# sqlite3_delete_database() API). +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix delete_db + +if {[atomic_batch_write test.db]} { + finish_test + return +} + +proc delete_all {} { + foreach f [glob -nocomplain test2*] { file delete $f } + foreach f [glob -nocomplain test3*] { file delete $f } +} + +proc copydb {} { + foreach f [glob -nocomplain test3*] { file delete $f } + foreach f [glob -nocomplain test2*] { + set p [string range $f 5 end] + file copy "test2$p" "test3$p" + } +} + +proc files {} { + lsort [glob -nocomplain test3*] +} + +db close +delete_all +sqlite3 db test2.database + +#------------------------------------------------------------------------- +# +# 1.1: Journal files. +# 1.2: Wal files. +# 1.3: Multiplexor with journal file. +# 1.4: Multiplexor with wal file. +# +# 2.* are a copy of 1.* with the multiplexor enabled. +# +# 3.* tests errors. +# + +do_test 1.1.0 { + execsql { + CREATE TABLE t1(x, y); + BEGIN; + INSERT INTO t1 VALUES(1, 2); + } + copydb + files +} {test3.database test3.database-journal} + +do_test 1.1.1 { + sqlite3_delete_database test3.database + files +} {} + +do_test 1.2.0 { + execsql { + COMMIT; + PRAGMA journal_mode = wal; + INSERT INTO t1 VALUES(3, 4); + } + copydb + files +} {test3.database test3.database-shm test3.database-wal} +do_test 1.2.1 { + sqlite3_delete_database test3.database + files +} {} + +db close +delete_all +sqlite3_multiplex_initialize "" 0 +sqlite3 db test2.database -vfs multiplex +sqlite3_multiplex_control db "main" chunk_size 32768 + +do_test 1.3.0 { + execsql { PRAGMA auto_vacuum = 0; } + execsql { + CREATE TABLE x1(a, b); + WITH s(i) AS ( VALUES(1) UNION ALL SELECT i+1 FROM s WHERE i<1000 ) + INSERT INTO x1 SELECT randomblob(100), randomblob(100) FROM s; + BEGIN; + UPDATE x1 SET a=randomblob(101) + } + copydb + files +} [list {*}{ + test3.database test3.database-journal test3.database001 + test3.database002 test3.database003 +}] +do_test 1.3.1 { + sqlite3_delete_database test3.database + files +} {} + + +do_test 1.4.0 { + execsql { + COMMIT; + PRAGMA journal_mode = wal; + UPDATE x1 SET a=randomblob(102) + } + copydb + files +} [list {*}{ + test3.database test3.database-shm test3.database-wal test3.database001 + test3.database002 test3.database003 +}] +do_test 1.4.1 { + sqlite3_delete_database test3.database + files +} {} + + +ifcapable 8_3_names { + db close + delete_all + sqlite3 db file:test2.db?8_3_names=1 -uri 1 + + do_test 2.1.0 { + execsql { + CREATE TABLE t1(x, y); + BEGIN; + INSERT INTO t1 VALUES(1, 2); + } + copydb + files + } {test3.db test3.nal} + + do_test 2.1.1 { + sqlite3_delete_database test3.db + files + } {} + + do_test 2.2.0 { + execsql { + COMMIT; + PRAGMA journal_mode = wal; + INSERT INTO t1 VALUES(3, 4); + } + copydb + files + } {test3.db test3.shm test3.wal} + do_test 2.2.1 { + sqlite3_delete_database test3.db + files + } {} + + + db close + delete_all + sqlite3_multiplex_initialize "" 0 + sqlite3 db file:test2.db?8_3_names=1 -uri 1 -vfs multiplex + sqlite3_multiplex_control db "main" chunk_size 32768 + + do_test 2.3.0 { + execsql { PRAGMA auto_vacuum = 0; } + execsql { + CREATE TABLE x1(a, b); + WITH s(i) AS ( VALUES(1) UNION ALL SELECT i+1 FROM s WHERE i<1000 ) + INSERT INTO x1 SELECT randomblob(100), randomblob(100) FROM s; + BEGIN; + UPDATE x1 SET a=randomblob(101) + } + copydb + files + } [list {*}{ + test3.001 test3.002 test3.003 test3.db test3.nal + }] + do_test 2.3.1 { + sqlite3_delete_database test3.db + files + } {} + + + do_test 2.4.0 { + execsql { + COMMIT; + PRAGMA journal_mode = wal; + UPDATE x1 SET a=randomblob(102) + } + copydb + files + } [list {*}{ + test3.001 test3.002 test3.003 test3.db test3.db-shm test3.wal + }] + do_test 2.4.1 { + sqlite3_delete_database test3.db + files + } {} +} + +db close +delete_all +sqlite3_multiplex_shutdown + +do_test 3.0 { + file mkdir dir2.db + sqlite3_delete_database dir2.db +} {SQLITE_ERROR} +do_test 3.1 { + sqlite3_delete_database dir2.db/test.db +} {SQLITE_OK} + +finish_test diff --git a/test/descidx1.test b/test/descidx1.test index a223664ff3..14be6f84e8 100644 --- a/test/descidx1.test +++ b/test/descidx1.test @@ -22,7 +22,8 @@ source $testdir/tester.tcl # do_not_use_codec -db eval {PRAGMA legacy_file_format=OFF} +#db eval {PRAGMA legacy_file_format=OFF} +sqlite3_db_config db LEGACY_FILE_FORMAT 0 # This procedure sets the value of the file-format in file 'test.db' # to $newval. Also, the schema cookie is incremented. @@ -299,19 +300,19 @@ ifcapable legacyformat { db close forcedelete test.db test.db-journal sqlite3 db test.db - execsql {PRAGMA legacy_file_format} + sqlite3_db_config db LEGACY_FILE_FORMAT } {1} } else { do_test descidx1-6.1 { db close forcedelete test.db test.db-journal sqlite3 db test.db - execsql {PRAGMA legacy_file_format} + sqlite3_db_config db LEGACY_FILE_FORMAT } {0} } do_test descidx1-6.2 { - execsql {PRAGMA legacy_file_format=YES} - execsql {PRAGMA legacy_file_format} + sqlite3_db_config db LEGACY_FILE_FORMAT 1 + sqlite3_db_config db LEGACY_FILE_FORMAT } {1} do_test descidx1-6.3 { execsql { @@ -330,8 +331,8 @@ do_test descidx1-6.4 { db close forcedelete test.db test.db-journal sqlite3 db test.db - execsql {PRAGMA legacy_file_format=NO} - execsql {PRAGMA legacy_file_format} + sqlite3_db_config db LEGACY_FILE_FORMAT 0 + sqlite3_db_config db LEGACY_FILE_FORMAT } {0} do_test descidx1-6.5 { execsql { @@ -351,8 +352,8 @@ ifcapable vacuum { get_file_format } {4} do_test descidx1-6.7 { + sqlite3_db_config db LEGACY_FILE_FORMAT 1 execsql { - PRAGMA legacy_file_format=ON; VACUUM; } get_file_format diff --git a/test/descidx2.test b/test/descidx2.test index fdc3eb0794..5aefc598ed 100644 --- a/test/descidx2.test +++ b/test/descidx2.test @@ -23,7 +23,8 @@ source $testdir/tester.tcl do_not_use_codec -db eval {PRAGMA legacy_file_format=OFF} +#db eval {PRAGMA legacy_file_format=OFF} +sqlite3_db_config db LEGACY_FILE_FORMAT 0 # This procedure sets the value of the file-format in file 'test.db' # to $newval. Also, the schema cookie is incremented. diff --git a/test/descidx3.test b/test/descidx3.test index c375acc705..30dd8f895c 100644 --- a/test/descidx3.test +++ b/test/descidx3.test @@ -26,7 +26,8 @@ ifcapable !bloblit { finish_test return } -db eval {PRAGMA legacy_file_format=OFF} +#db eval {PRAGMA legacy_file_format=OFF} +sqlite3_db_config db LEGACY_FILE_FORMAT 0 # This procedure sets the value of the file-format in file 'test.db' # to $newval. Also, the schema cookie is incremented. diff --git a/test/distinct.test b/test/distinct.test index dac2269b0b..760b2341f5 100644 --- a/test/distinct.test +++ b/test/distinct.test @@ -30,12 +30,11 @@ proc is_distinct_noop {sql} { set program1 [list] set program2 [list] db eval "EXPLAIN $sql1" { - if {$opcode != "Noop"} { lappend program1 $opcode } + if {$opcode != "Noop" && $opcode != "Explain"} { lappend program1 $opcode } } db eval "EXPLAIN $sql2" { - if {$opcode != "Noop"} { lappend program2 $opcode } + if {$opcode != "Noop" && $opcode != "Explain"} { lappend program2 $opcode } } - return [expr {$program1==$program2}] } @@ -51,8 +50,8 @@ proc do_temptables_test {tn sql temptables} { set ret "" db eval "EXPLAIN [set sql]" { if {$opcode == "OpenEphemeral" || $opcode == "SorterOpen"} { - if {$p5 != "08" && $p5!="00"} { error "p5 = $p5" } - if {$p5 == "08"} { + if {$p5!=8 && $p5!=0} { error "p5 = $p5" } + if {$p5==8} { lappend ret hash } else { lappend ret btree @@ -128,7 +127,6 @@ foreach {tn noop sql} { 21 0 "SELECT DISTINCT c2 FROM t3" 22 0 "SELECT DISTINCT * FROM (SELECT 1, 2, 3 UNION SELECT 4, 5, 6)" - 23 1 "SELECT DISTINCT rowid FROM (SELECT 1, 2, 3 UNION SELECT 4, 5, 6)" 24 0 "SELECT DISTINCT rowid/2 FROM t1" 25 1 "SELECT DISTINCT rowid/2, rowid FROM t1" @@ -268,5 +266,143 @@ do_execsql_test 6.2 { FROM sqlite_master; } {mmm} +#------------------------------------------------------------------------- +# Ticket [9c944882] +# +reset_db +do_execsql_test 7.0 { + CREATE TABLE t1(a INTEGER PRIMARY KEY); + CREATE TABLE t3(a INTEGER PRIMARY KEY); + + CREATE TABLE t4(x); + CREATE TABLE t5(y); + + INSERT INTO t5 VALUES(1), (2), (2); + INSERT INTO t1 VALUES(2); + INSERT INTO t3 VALUES(2); + INSERT INTO t4 VALUES(2); +} + +do_execsql_test 7.1 { + WITH t2(b) AS ( + SELECT DISTINCT y FROM t5 ORDER BY y + ) + SELECT * FROM + t4 CROSS JOIN t3 CROSS JOIN t1 + WHERE (t1.a=t3.a) AND (SELECT count(*) FROM t2 AS y WHERE t4.x!='abc')=t1.a +} {2 2 2} + +# 2021-04-06 forum post https://sqlite.org/forum/forumpost/66954e9ece +reset_db +do_execsql_test 8.0 { + CREATE TABLE person ( pid INT) ; + CREATE UNIQUE INDEX idx ON person ( pid ) WHERE pid == 1; + INSERT INTO person VALUES (1), (10), (10); + SELECT DISTINCT pid FROM person where pid = 10; +} {10} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 9.0 { + CREATE TABLE t1(a, b); + INSERT INTO t1 VALUES('a', 'a'); + INSERT INTO t1 VALUES('a', 'b'); + INSERT INTO t1 VALUES('a', 'c'); + + INSERT INTO t1 VALUES('b', 'a'); + INSERT INTO t1 VALUES('b', 'b'); + INSERT INTO t1 VALUES('b', 'c'); + + INSERT INTO t1 VALUES('a', 'a'); + INSERT INTO t1 VALUES('b', 'b'); + + INSERT INTO t1 VALUES('A', 'A'); + INSERT INTO t1 VALUES('B', 'B'); +} + +foreach {tn idx} { + 1 { } + 2 { CREATE INDEX i1 ON t1(a, b); } + 3 { CREATE INDEX i1 ON t1(b, a); } + 4 { CREATE INDEX i1 ON t1(a COLLATE nocase, b COLLATE nocase); } + 5 { CREATE INDEX i1 ON t1(b COLLATE nocase, a COLLATE nocase); } +} { + + execsql { DROP INDEX IF EXISTS i1 } + execsql $idx + + do_execsql_test 9.$tn.1 { + SELECT DISTINCT a, b FROM t1 ORDER BY a, b + } { + A A B B + a a a b a c + b a b b b c + } + + do_execsql_test 9.$tn.1 { + SELECT DISTINCT a COLLATE nocase, b COLLATE nocase FROM t1 + ORDER BY a COLLATE nocase, b COLLATE nocase + } { + a a a b a c + b a b b b c + } +} + +# 2023-03-16 +# https://sqlite.org/forum/forumpost/16ce2bb7a639e29b +# ticket c36cdb4afd504dc1 +# ticket 4051a7f931d9ba24 +# ticket d6fd512f50513ab7 +# +do_execsql_test 10.1 { + SELECT DISTINCT + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1 + ORDER BY + 'x','x','x','x','x','x','x','x','x','x', + 'x','x','x','x','x','x','x','x','x','x', + 'x','x','x','x','x','x','x','x','x','x', + 'x','x','x','x','x','x','x','x','x','x', + 'x','x','x','x','x','x','x','x','x','x', + 'x','x','x','x','x','x','x','x','x','x', + 'x','x','x','x'; +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1} +do_execsql_test 10.2 { + EXPLAIN + SELECT DISTINCT + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1 + ORDER BY + 'x','x','x','x','x','x','x','x','x','x', + 'x','x','x','x','x','x','x','x','x','x', + 'x','x','x','x','x','x','x','x','x','x', + 'x','x','x','x','x','x','x','x','x','x', + 'x','x','x','x','x','x','x','x','x','x', + 'x','x','x','x','x','x','x','x','x','x', + 'x','x','x','x'; +} {/0 Init 0 /} +do_execsql_test 10.3 { + EXPLAIN CREATE TABLE t2 AS SELECT DISTINCT ':memory:', 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7 ORDER BY '%J%j%w%s', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', '%J%j%w%s', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 42e-300, 'unixepoch', 'unixepoch', 'unixepoch' LIMIT 0xda; +} {/0 Init 0/} +do_execsql_test 10.4 { + DROP TABLE IF EXISTS t0; + CREATE TABLE t0 AS SELECT DISTINCT 0xda, 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 0xda-0xda-42e-300, 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 'lit0', 'lit0' ORDER BY '%%', '%%', '%%', '%%', '%%', '%%', '%%', '%%', '%%', '%%', '%%', '%%', '%%', '%%', '%%', '%Y-%m-%d', '%%', '%%', '%%', '%%', '%%', '%%', '%%', '%%', '%%', '%%', 'lit0', '%%', '%%', '%%', '%%', '%%', '%%', '%%', '%%', '%%', '%%', '%%', '%%', '%%', 'auto', '%%', '%%', '%%', '%%', '%%', '%%', '%%', '%%', '%%', '%%', '%%', ':memory:', '%%', '%%', '%%', '%%', '%%', '%%', '%%', '%%', '%%', '%%', ''; + SELECT count(*) FROM t0; +} {1} +do_execsql_test 10.5 { + DROP TABLE IF EXISTS t2; + CREATE TABLE t2 AS SELECT DISTINCT ':memory:', 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0.0*7/0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7 ORDER BY '%J%j%w%s', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', '%J%j%w%s', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 'unixepoch', 42e-300, 'unixepoch', 'unixepoch', 'unixepoch' LIMIT 0xda; + SELECT count(*) FROM t2; +} {1} finish_test diff --git a/test/distinct2.test b/test/distinct2.test new file mode 100644 index 0000000000..980b0b1e3a --- /dev/null +++ b/test/distinct2.test @@ -0,0 +1,383 @@ +# 2016-04-15 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this script is DISTINCT queries using the skip-ahead +# optimization. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +set testprefix distinct2 + +do_execsql_test 100 { + CREATE TABLE t1(x INTEGER PRIMARY KEY); + INSERT INTO t1 VALUES(0),(1),(2); + CREATE TABLE t2 AS + SELECT DISTINCT a.x AS aa, b.x AS bb + FROM t1 a, t1 b; + SELECT *, '|' FROM t2 ORDER BY aa, bb; +} {0 0 | 0 1 | 0 2 | 1 0 | 1 1 | 1 2 | 2 0 | 2 1 | 2 2 |} +do_execsql_test 110 { + DROP TABLE t2; + CREATE TABLE t2 AS + SELECT DISTINCT a.x AS aa, b.x AS bb + FROM t1 a, t1 b + WHERE a.x IN t1 AND b.x IN t1; + SELECT *, '|' FROM t2 ORDER BY aa, bb; +} {0 0 | 0 1 | 0 2 | 1 0 | 1 1 | 1 2 | 2 0 | 2 1 | 2 2 |} +do_execsql_test 120 { + CREATE TABLE t102 (i0 TEXT UNIQUE NOT NULL); + INSERT INTO t102 VALUES ('0'),('1'),('2'); + DROP TABLE t2; + CREATE TABLE t2 AS + SELECT DISTINCT * + FROM t102 AS t0 + JOIN t102 AS t4 ON (t2.i0 IN t102) + NATURAL JOIN t102 AS t3 + JOIN t102 AS t1 ON (t0.i0 IN t102) + JOIN t102 AS t2 ON (t2.i0=+t0.i0 OR (t0.i0<>500 AND t2.i0=t1.i0)); + SELECT *, '|' FROM t2 ORDER BY 1, 2, 3, 4, 5; +} {0 0 0 0 | 0 0 1 0 | 0 0 1 1 | 0 0 2 0 | 0 0 2 2 | 0 1 0 0 | 0 1 1 0 | 0 1 1 1 | 0 1 2 0 | 0 1 2 2 | 0 2 0 0 | 0 2 1 0 | 0 2 1 1 | 0 2 2 0 | 0 2 2 2 | 1 0 0 0 | 1 0 0 1 | 1 0 1 1 | 1 0 2 1 | 1 0 2 2 | 1 1 0 0 | 1 1 0 1 | 1 1 1 1 | 1 1 2 1 | 1 1 2 2 | 1 2 0 0 | 1 2 0 1 | 1 2 1 1 | 1 2 2 1 | 1 2 2 2 | 2 0 0 0 | 2 0 0 2 | 2 0 1 1 | 2 0 1 2 | 2 0 2 2 | 2 1 0 0 | 2 1 0 2 | 2 1 1 1 | 2 1 1 2 | 2 1 2 2 | 2 2 0 0 | 2 2 0 2 | 2 2 1 1 | 2 2 1 2 | 2 2 2 2 |} + +do_execsql_test 400 { + CREATE TABLE t4(a,b,c,d,e,f,g,h,i,j); + INSERT INTO t4 VALUES(0,1,2,3,4,5,6,7,8,9); + INSERT INTO t4 SELECT * FROM t4; + INSERT INTO t4 SELECT * FROM t4; + CREATE INDEX t4x ON t4(c,d,e); + SELECT DISTINCT a,b,c FROM t4 WHERE a=0 AND b=1; +} {0 1 2} +do_execsql_test 410 { + SELECT DISTINCT a,b,c,d FROM t4 WHERE a=0 AND b=1; +} {0 1 2 3} +do_execsql_test 411 { + SELECT DISTINCT d,a,b,c FROM t4 WHERE a=0 AND b=1; +} {3 0 1 2} +do_execsql_test 420 { + SELECT DISTINCT a,b,c,d,e FROM t4 WHERE a=0 AND b=1; +} {0 1 2 3 4} +do_execsql_test 430 { + SELECT DISTINCT a,b,c,d,e,f FROM t4 WHERE a=0 AND b=1; +} {0 1 2 3 4 5} + +do_execsql_test 500 { + CREATE TABLE t5(a INT, b INT); + CREATE UNIQUE INDEX t5x ON t5(a+b); + INSERT INTO t5(a,b) VALUES(0,0),(1,0),(1,1),(0,3); + CREATE TEMP TABLE out AS SELECT DISTINCT a+b FROM t5; + SELECT * FROM out ORDER BY 1; +} {0 1 2 3} + +do_execsql_test 600 { + CREATE TABLE t6a(x INTEGER PRIMARY KEY); + INSERT INTO t6a VALUES(1); + CREATE TABLE t6b(y INTEGER PRIMARY KEY); + INSERT INTO t6b VALUES(2),(3); + SELECT DISTINCT x, x FROM t6a, t6b; +} {1 1} + +do_execsql_test 700 { + CREATE TABLE t7(a, b, c); + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE (i+1)<200 + ) + INSERT INTO t7 SELECT i/100, i/50, i FROM s; +} +do_execsql_test 710 { + SELECT DISTINCT a, b FROM t7; +} { + 0 0 0 1 + 1 2 1 3 +} +do_execsql_test 720 { + SELECT DISTINCT a, b+1 FROM t7; +} { + 0 1 0 2 + 1 3 1 4 +} +do_execsql_test 730 { + CREATE INDEX i7 ON t7(a, b+1); + ANALYZE; + SELECT DISTINCT a, b+1 FROM t7; +} { + 0 1 0 2 + 1 3 1 4 +} + +do_execsql_test 800 { + CREATE TABLE t8(a, b, c); + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE (i+1)<100 + ) + INSERT INTO t8 SELECT i/40, i/20, i/40 FROM s; +} + +do_execsql_test 820 { + SELECT DISTINCT a, b, c FROM t8; +} { + 0 0 0 0 1 0 + 1 2 1 1 3 1 + 2 4 2 +} + +do_execsql_test 820 { + SELECT DISTINCT a, b, c FROM t8 WHERE b=3; +} {1 3 1} + +do_execsql_test 830 { + CREATE INDEX i8 ON t8(a, c); + ANALYZE; + SELECT DISTINCT a, b, c FROM t8 WHERE b=3; +} {1 3 1} + +do_execsql_test 900 { + CREATE TABLE t9(v); + INSERT INTO t9 VALUES + ('abcd'), ('Abcd'), ('aBcd'), ('ABcd'), ('abCd'), ('AbCd'), ('aBCd'), + ('ABCd'), ('abcD'), ('AbcD'), ('aBcD'), ('ABcD'), ('abCD'), ('AbCD'), + ('aBCD'), ('ABCD'), + ('wxyz'), ('Wxyz'), ('wXyz'), ('WXyz'), ('wxYz'), ('WxYz'), ('wXYz'), + ('WXYz'), ('wxyZ'), ('WxyZ'), ('wXyZ'), ('WXyZ'), ('wxYZ'), ('WxYZ'), + ('wXYZ'), ('WXYZ'); +} + +do_execsql_test 910 { + SELECT DISTINCT v COLLATE NOCASE, v FROM t9 ORDER BY +v; +} { + ABCD ABCD ABCd ABCd ABcD ABcD ABcd ABcd AbCD + AbCD AbCd AbCd AbcD AbcD Abcd Abcd + WXYZ WXYZ WXYz WXYz WXyZ WXyZ WXyz WXyz WxYZ + WxYZ WxYz WxYz WxyZ WxyZ Wxyz Wxyz + aBCD aBCD aBCd aBCd aBcD aBcD aBcd aBcd abCD + abCD abCd abCd abcD abcD abcd abcd + wXYZ wXYZ wXYz wXYz wXyZ wXyZ wXyz wXyz wxYZ + wxYZ wxYz wxYz wxyZ wxyZ wxyz wxyz +} + +do_execsql_test 920 { + CREATE INDEX i9 ON t9(v COLLATE NOCASE, v); + ANALYZE; + + SELECT DISTINCT v COLLATE NOCASE, v FROM t9 ORDER BY +v; +} { + ABCD ABCD ABCd ABCd ABcD ABcD ABcd ABcd AbCD + AbCD AbCd AbCd AbcD AbcD Abcd Abcd + WXYZ WXYZ WXYz WXYz WXyZ WXyZ WXyz WXyz WxYZ + WxYZ WxYz WxYz WxyZ WxyZ Wxyz Wxyz + aBCD aBCD aBCd aBCd aBcD aBcD aBcd aBcd abCD + abCD abCd abCd abcD abcD abcd abcd + wXYZ wXYZ wXYz wXYz wXyZ wXyZ wXyz wXyz wxYZ + wxYZ wxYz wxYz wxyZ wxyZ wxyz wxyz +} + +# Ticket https://sqlite.org/src/info/ef9318757b152e3a on 2017-11-21 +# Incorrect result due to a skip-ahead-distinct optimization on a +# join where no rows of the inner loop appear in the result set. +# +db close +sqlite3 db :memory: +do_execsql_test 1000 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b INTEGER); + CREATE INDEX t1b ON t1(b); + CREATE TABLE t2(x INTEGER PRIMARY KEY, y INTEGER); + CREATE INDEX t2y ON t2(y); + WITH RECURSIVE c(x) AS (VALUES(0) UNION ALL SELECT x+1 FROM c WHERE x<49) + INSERT INTO t1(b) SELECT x/10 - 1 FROM c; + WITH RECURSIVE c(x) AS (VALUES(-1) UNION ALL SELECT x+1 FROM c WHERE x<19) + INSERT INTO t2(x,y) SELECT x, 1 FROM c; + SELECT DISTINCT y FROM t1, t2 WHERE b=x AND b<>-1; + ANALYZE; + SELECT DISTINCT y FROM t1, t2 WHERE b=x AND b<>-1; +} {1 1} +db close +sqlite3 db :memory: +do_execsql_test 1010 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b INTEGER); + CREATE INDEX t1b ON t1(b); + CREATE TABLE t2(x INTEGER PRIMARY KEY, y INTEGER); + CREATE INDEX t2y ON t2(y); + WITH RECURSIVE c(x) AS (VALUES(0) UNION ALL SELECT x+1 FROM c WHERE x<49) + INSERT INTO t1(b) SELECT -(x/10 - 1) FROM c; + WITH RECURSIVE c(x) AS (VALUES(-1) UNION ALL SELECT x+1 FROM c WHERE x<19) + INSERT INTO t2(x,y) SELECT -x, 1 FROM c; + SELECT DISTINCT y FROM t1, t2 WHERE b=x AND b<>1 ORDER BY y DESC; + ANALYZE; + SELECT DISTINCT y FROM t1, t2 WHERE b=x AND b<>1 ORDER BY y DESC; +} {1 1} +db close +sqlite3 db :memory: +do_execsql_test 1020 { + CREATE TABLE t1(a, b); + CREATE INDEX t1a ON t1(a, b); + -- Lots of rows of (1, 'no'), followed by a single (1, 'yes'). + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<100) + INSERT INTO t1(a, b) SELECT 1, 'no' FROM c; + INSERT INTO t1(a, b) VALUES(1, 'yes'); + CREATE TABLE t2(x PRIMARY KEY); + INSERT INTO t2 VALUES('yes'); + SELECT DISTINCT a FROM t1, t2 WHERE x=b; + ANALYZE; + SELECT DISTINCT a FROM t1, t2 WHERE x=b; +} {1 1} + +#------------------------------------------------------------------------- +reset_db + +do_execsql_test 2000 { + CREATE TABLE t0 (c0, c1, c2, PRIMARY KEY (c0, c1)); + CREATE TABLE t1 (c2); + INSERT INTO t0(c2) VALUES (0),(1),(3),(4),(5),(6),(7),(8),(9),(10),(11); + INSERT INTO t0(c1) VALUES ('a'); + INSERT INTO t1(c2) VALUES (0); +} +do_execsql_test 2010 { + SELECT DISTINCT t0.c0, t1._rowid_, t0.c1 FROM t1 CROSS JOIN t0 ORDER BY t0.c0; +} {{} 1 {} {} 1 a} +do_execsql_test 1.2 { + ANALYZE; +} +do_execsql_test 2020 { + SELECT DISTINCT t0.c0, t1._rowid_, t0.c1 FROM t1 CROSS JOIN t0 ORDER BY t0.c0; +} {{} 1 {} {} 1 a} + + +do_execsql_test 2030 { + CREATE TABLE t2(a, b, c); + CREATE INDEX t2ab ON t2(a, b); + + WITH c(i) AS (SELECT 1 UNION ALL SELECT i+1 FROM c WHERE i<64) + INSERT INTO t2 SELECT 'one', i%2, 'one' FROM c; + + WITH c(i) AS (SELECT 1 UNION ALL SELECT i+1 FROM c WHERE i<64) + INSERT INTO t2 SELECT 'two', i%2, 'two' FROM c; + + CREATE TABLE t3(x INTEGER PRIMARY KEY); + INSERT INTO t3 VALUES(1); + + ANALYZE; +} +do_execsql_test 2040 { + SELECT DISTINCT a, b, x FROM t3 CROSS JOIN t2 ORDER BY a, +b; +} { + one 0 1 + one 1 1 + two 0 1 + two 1 1 +} + +#------------------------------------------------------------------------- +# +reset_db +do_execsql_test 3000 { + CREATE TABLE t0 (c0, c1 NOT NULL DEFAULT 1, c2, PRIMARY KEY (c0, c1)); + INSERT INTO t0(c2) VALUES (NULL), (NULL), (NULL), (NULL), (NULL), (NULL), (NULL), (NULL), (NULL), (NULL), (NULL); + INSERT INTO t0(c2) VALUES('a'); +} + +do_execsql_test 3010 { + SELECT DISTINCT * FROM t0 WHERE NULL IS t0.c0; +} { + {} 1 {} + {} 1 a +} + +do_execsql_test 3020 { + ANALYZE; +} + +do_execsql_test 3030 { + SELECT DISTINCT * FROM t0 WHERE NULL IS c0; +} { + {} 1 {} + {} 1 a +} + +#------------------------------------------------------------------------- +# +reset_db + +do_execsql_test 4010 { + CREATE TABLE t1(a, b COLLATE RTRIM); + INSERT INTO t1 VALUES(1, ''), (2, ' '), (3, ' '); +} +do_execsql_test 4020 { + SELECT b FROM t1 UNION SELECT 1; +} {1 { }} + +#------------------------------------------------------------------------- +# +reset_db +do_execsql_test 5010 { + CREATE TABLE cnt(a); + WITH RECURSIVE cnt2(x) AS ( + VALUES(1) UNION ALL SELECT x+1 FROM cnt2 WHERE x<50 + ) + INSERT INTO cnt SELECT x FROM cnt2; +} + +do_execsql_test 5020 { + SELECT DISTINCT abs(random())%5 AS r FROM cnt ORDER BY r; +} {0 1 2 3 4} + +do_execsql_test 5030 { + SELECT abs(random())%5 AS r FROM cnt GROUP BY 1 ORDER BY 1; +} {0 1 2 3 4} + +do_execsql_test 5040 { + SELECT a FROM cnt WHERE a>45 GROUP BY 1; +} {46 47 48 49 50} + + +# 2024-06-03 dbsqlfuzz 8a44f675401a8b1f68a43bf813c4f4f72ad8f0ea +# Use of uninitialized bytecode register due to the call-function-once +# optimization at check-in 663f5dd32d9db832 +# +db null NULL +do_execsql_test 5050 { + CREATE TABLE t0(a TEXT); INSERT INTO t0 VALUES('abcd'); + CREATE TABLE t1(b TEXT); + CREATE TABLE t2(c TEXT); + CREATE TABLE t3(d TEXT); INSERT INTO t3 VALUES('wxyz'); + CREATE VIEW v4(e) AS SELECT (SELECT t2.c FROM t0, t1 GROUP BY 1) FROM t2; + SELECT v4.e FROM t3 LEFT JOIN v4 ON true GROUP BY 1; +} NULL +do_execsql_test 5060 { + DROP VIEW v4; + CREATE VIEW v4(e) AS SELECT (SELECT t2.c COLLATE nocase FROM t0, t1 GROUP BY 1) FROM t2; + SELECT v4.e FROM t3 LEFT JOIN v4 ON true GROUP BY 1; +} NULL + +do_execsql_test 5070 { + DROP VIEW v4; + CREATE VIEW v4(e) AS SELECT (SELECT unlikely(t2.c COLLATE nocase) FROM t0, t1 GROUP BY 1) FROM t2; + SELECT v4.e FROM t3 LEFT JOIN v4 ON true GROUP BY 1; +} NULL + +# 2024-06-28 dbsqlfuzz 46343912848a603e32c6072cae792eb056bac897 +# Do not call sqlite3ExprToRegister() on an expression that is already +# a register. +# +do_execsql_test 5080 { + CREATE TABLE dual(dummy TEXT); + INSERT INTO dual VALUES('X'); + SELECT 11 = ( + SELECT b + FROM ( + SELECT a AS b + FROM dual + LEFT JOIN (SELECT 22 AS a FROM dual) + ) + GROUP BY b, b + ); +} 0 + +finish_test diff --git a/test/distinctagg.test b/test/distinctagg.test index 9b5dc21678..9eedd35bd2 100644 --- a/test/distinctagg.test +++ b/test/distinctagg.test @@ -16,6 +16,7 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl +set testprefix distinctagg do_test distinctagg-1.1 { execsql { @@ -31,7 +32,7 @@ do_test distinctagg-1.1 { } {1 2 3 3} do_test distinctagg-1.2 { execsql { - SELECT b, count(distinct c) FROM t1 GROUP BY b ORDER BY b + SELECT b, count(distinct c) FROM t1 GROUP BY b } } {2 1 3 2} do_test distinctagg-1.3 { @@ -55,8 +56,169 @@ do_test distinctagg-2.1 { } {1 {DISTINCT aggregates must have exactly one argument}} do_test distinctagg-2.2 { catchsql { - SELECT group_concat(distinct a,b) FROM t1; + SELECT string_agg(distinct a,b) FROM t1; } } {1 {DISTINCT aggregates must have exactly one argument}} +#-------------------------------------------------------------------------- +reset_db +do_execsql_test 3.0 { + CREATE TABLE t1(a, b, c); + CREATE TABLE t2(d, e, f); + + INSERT INTO t1 VALUES (1, 1, 1); + INSERT INTO t1 VALUES (2, 2, 2); + INSERT INTO t1 VALUES (3, 3, 3); + INSERT INTO t1 VALUES (4, 1, 4); + INSERT INTO t1 VALUES (5, 2, 1); + INSERT INTO t1 VALUES (5, 3, 2); + INSERT INTO t1 VALUES (4, 1, 3); + INSERT INTO t1 VALUES (3, 2, 4); + INSERT INTO t1 VALUES (2, 3, 1); + INSERT INTO t1 VALUES (1, 1, 2); + + INSERT INTO t2 VALUES('a', 'a', 'a'); + INSERT INTO t2 VALUES('b', 'b', 'b'); + INSERT INTO t2 VALUES('c', 'c', 'c'); + + CREATE INDEX t1a ON t1(a); + CREATE INDEX t1bc ON t1(b, c); +} + +foreach {tn use_eph sql res} { + 1 0 "SELECT count(DISTINCT a) FROM t1" 5 + 2 0 "SELECT count(DISTINCT b) FROM t1" 3 + 3 1 "SELECT count(DISTINCT c) FROM t1" 4 + 4 0 "SELECT count(DISTINCT c) FROM t1 WHERE b=3" 3 + 5 0 "SELECT count(DISTINCT rowid) FROM t1" 10 + 6 0 "SELECT count(DISTINCT a) FROM t1, t2" 5 + 7 0 "SELECT count(DISTINCT a) FROM t2, t1" 5 + 8 1 "SELECT count(DISTINCT a+b) FROM t1, t2, t2, t2" 6 + 9 0 "SELECT count(DISTINCT c) FROM t1 WHERE c=2" 1 + 10 0 "SELECT count(DISTINCT t1.rowid) FROM t1, t2" 10 +} { + do_test 3.$tn.1 { + set prg [db eval "EXPLAIN $sql"] + set idx [lsearch $prg OpenEphemeral] + expr {$idx>=0} + } $use_eph + + do_execsql_test 3.$tn.2 $sql $res +} + +do_execsql_test 3.10 { + SELECT a, count(DISTINCT b) FROM t1 GROUP BY a; +} { + 1 1 2 2 3 2 4 1 5 2 +} + +#-------------------------------------------------------------------------- +reset_db +do_execsql_test 3.0 { + CREATE TABLE t1(a, b, c); + CREATE INDEX t1a ON t1(a); + CREATE INDEX t1bc ON t1(b, c); + + INSERT INTO t1 VALUES(1, 'A', 1); + INSERT INTO t1 VALUES(1, 'A', 1); + INSERT INTO t1 VALUES(2, 'A', 2); + INSERT INTO t1 VALUES(2, 'A', 2); + INSERT INTO t1 VALUES(1, 'B', 1); + INSERT INTO t1 VALUES(2, 'B', 2); + INSERT INTO t1 VALUES(3, 'B', 3); + INSERT INTO t1 VALUES(NULL, 'B', NULL); + INSERT INTO t1 VALUES(NULL, 'C', NULL); + INSERT INTO t1 VALUES('d', 'D', 'd'); + + CREATE TABLE t2(d, e, f); + CREATE INDEX t2def ON t2(d, e, f); + + INSERT INTO t2 VALUES(1, 1, 'a'); + INSERT INTO t2 VALUES(1, 1, 'a'); + INSERT INTO t2 VALUES(1, 2, 'a'); + INSERT INTO t2 VALUES(1, 2, 'a'); + INSERT INTO t2 VALUES(1, 2, 'b'); + INSERT INTO t2 VALUES(1, 3, 'b'); + INSERT INTO t2 VALUES(1, 3, 'a'); + INSERT INTO t2 VALUES(1, 3, 'b'); + INSERT INTO t2 VALUES(2, 3, 'x'); + INSERT INTO t2 VALUES(2, 3, 'y'); + INSERT INTO t2 VALUES(2, 3, 'z'); + + CREATE TABLE t3(x, y, z); + INSERT INTO t3 VALUES(1,1,1); + INSERT INTO t3 VALUES(2,2,2); + + CREATE TABLE t4(a); + CREATE INDEX t4a ON t4(a); + INSERT INTO t4 VALUES(1), (2), (2), (3), (1); +} + +foreach {tn use_eph sql res} { + 1 0 "SELECT count(DISTINCT c) FROM t1 GROUP BY b" {2 3 0 1} + 2 1 "SELECT count(DISTINCT a) FROM t1 GROUP BY b" {2 3 0 1} + 3 1 "SELECT count(DISTINCT a) FROM t1 GROUP BY b+c" {0 1 1 1 1} + + 4 0 "SELECT count(DISTINCT f) FROM t2 GROUP BY d, e" {1 2 2 3} + 5 1 "SELECT count(DISTINCT f) FROM t2 GROUP BY d" {2 3} + 6 0 "SELECT count(DISTINCT f) FROM t2 WHERE d IS 1 GROUP BY e" {1 2 2} + + 7 0 "SELECT count(DISTINCT a) FROM t1" {4} + 8 0 "SELECT count(DISTINCT a) FROM t4" {3} +} { + do_test 4.$tn.1 { + set prg [db eval "EXPLAIN $sql"] + set idx [lsearch $prg OpenEphemeral] + expr {$idx>=0} + } $use_eph + + do_execsql_test 4.$tn.2 $sql $res +} + + +set t3root [db one {SELECT rootpage FROM sqlite_schema WHERE name='t3'}] +foreach {tn use_t3 sql res} { + 1 1 "SELECT count(*) FROM t3" 2 + 2 0 "SELECT count(*) FROM t1" 10 + 2 1 "SELECT count(DISTINCT a) FROM t1, t3" 4 + 3 1 "SELECT count(DISTINCT a) FROM t1 LEFT JOIN t3" 4 + 4 1 "SELECT count(DISTINCT a) FROM t1 LEFT JOIN t3 WHERE t3.x=1" 4 + 5 1 "SELECT count(DISTINCT a) FROM t1 LEFT JOIN t3 WHERE t3.x=0" 0 + 6 1 "SELECT count(DISTINCT a) FROM t1 LEFT JOIN t3 ON (t3.x=0)" 4 + 7 1 "SELECT count(DISTINCT x) FROM t1 LEFT JOIN t3" 2 + 8 1 "SELECT count(DISTINCT x) FROM t1 LEFT JOIN t3 WHERE t3.x=1" 1 + 9 1 "SELECT count(DISTINCT x) FROM t1 LEFT JOIN t3 WHERE t3.x=0" 0 + 10 1 "SELECT count(DISTINCT x) FROM t1 LEFT JOIN t3 ON (t3.x=0)" 0 + +} { + unset -nocomplain a + do_test 5.$tn.1 { + set bUse 0 + db eval "EXPLAIN $sql" a { + if {$a(opcode)=="OpenRead" && $a(p2)==$t3root} {set bUse 1} + } + set bUse + } $use_t3 + + do_execsql_test 5.$tn.2 $sql $res +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 6.0 { + CREATE TABLE t1(a, b); + CREATE TABLE t2(c, d); + INSERT INTO t1 VALUES(123,456); + INSERT INTO t2 VALUES(123,456); +} +do_execsql_test 6.1 { + SELECT count(DISTINCT c) FROM t1 LEFT JOIN t2; +} {1} + +do_execsql_test 7.0 { + CREATE TABLE v1 ( v2 UNIQUE, v3 AS( TYPEOF ( NULL ) ) UNIQUE ); + SELECT COUNT ( DISTINCT TRUE ) FROM v1 GROUP BY likelihood ( v3 , 0.100000 ); +} + + finish_test diff --git a/test/e_blobbytes.test b/test/e_blobbytes.test index d38f56bf34..c24318c565 100644 --- a/test/e_blobbytes.test +++ b/test/e_blobbytes.test @@ -14,6 +14,11 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl set testprefix e_blobbytes +ifcapable !incrblob { + finish_test + return +} + do_execsql_test 1.0 { CREATE TABLE q1(r INTEGER PRIMARY KEY, s TEXT); WITH d(a, b) AS ( diff --git a/test/e_blobclose.test b/test/e_blobclose.test index 2683197854..40291cf036 100644 --- a/test/e_blobclose.test +++ b/test/e_blobclose.test @@ -14,6 +14,11 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl set testprefix e_blobclose +ifcapable !incrblob { + finish_test + return +} + set dots [string repeat . 40] do_execsql_test 1.0 { CREATE TABLE x1(a INTEGER PRIMARY KEY, b DOTS); diff --git a/test/e_blobopen.test b/test/e_blobopen.test index a6168042e9..41fd13c674 100644 --- a/test/e_blobopen.test +++ b/test/e_blobopen.test @@ -14,6 +14,11 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl set testprefix e_blobopen +ifcapable !incrblob { + finish_test + return +} + forcedelete test.db2 do_execsql_test 1.0 { diff --git a/test/e_blobwrite.test b/test/e_blobwrite.test index 7b2249c243..8d8588e6aa 100644 --- a/test/e_blobwrite.test +++ b/test/e_blobwrite.test @@ -14,6 +14,11 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl set testprefix e_blobwrite +ifcapable !incrblob { + finish_test + return +} + #-------------------------------------------------------------------------- # EVIDENCE-OF: R-62898-22698 This function is used to write data into an # open BLOB handle from a caller-supplied buffer. N bytes of data are @@ -110,7 +115,7 @@ sqlite3_blob_close $B # sqlite3_blob_open db main t2 a 44 3 B blob_write_error_test 2.2.1 $B 31 $blob 10 \ - SQLITE_ERROR {SQL logic error or missing database} + SQLITE_ERROR {SQL logic error} # Make a successful write to the blob handle. This shows that the # sqlite3_errcode() and sqlite3_errmsg() values are set even if the @@ -122,10 +127,10 @@ blob_write_error_test 2.2.1 $B 30 $blob 10 SQLITE_OK {not an error} # SQLITE_ERROR is returned and no data is written. # blob_write_error_test 2.2.2 $B 31 $blob -1 \ - SQLITE_ERROR {SQL logic error or missing database} + SQLITE_ERROR {SQL logic error} blob_write_error_test 2.2.3 $B 20 $blob 10 SQLITE_OK {not an error} blob_write_error_test 2.2.4 $B -1 $blob 10 \ - SQLITE_ERROR {SQL logic error or missing database} + SQLITE_ERROR {SQL logic error} sqlite3_blob_close $B # EVIDENCE-OF: R-20958-54138 An attempt to write to an expired BLOB @@ -136,13 +141,13 @@ do_test 2.3 { execsql { DELETE FROM t2 WHERE b=43 } } {} blob_write_error_test 2.3.1 $B 5 $blob 5 \ - SQLITE_ABORT {callback requested query abort} + SQLITE_ABORT {query aborted} do_test 2.3.2 { execsql { SELECT 1, 2, 3 } sqlite3_errcode db } {SQLITE_OK} blob_write_error_test 2.3.3 $B 5 $blob 5 \ - SQLITE_ABORT {callback requested query abort} + SQLITE_ABORT {query aborted} sqlite3_blob_close $B # EVIDENCE-OF: R-08382-59936 Writes to the BLOB that occurred before the @@ -173,7 +178,7 @@ do_execsql_test 3.1.2 { 1 .....0123456789......................... xyz } blob_write_error_test 3.1.3 $B 15 $blob 10 \ - SQLITE_ABORT {callback requested query abort} + SQLITE_ABORT {query aborted} sqlite3_blob_close $B do_execsql_test 3.1.4 { SELECT * FROM t3 WHERE i=1; @@ -190,7 +195,7 @@ do_execsql_test 3.2.2 { 2 xyz ........................................ } blob_write_error_test 3.2.3 $B 15 $blob 10 \ - SQLITE_ABORT {callback requested query abort} + SQLITE_ABORT {query aborted} sqlite3_blob_close $B do_execsql_test 3.2.4 { SELECT * FROM t3 WHERE i=2; diff --git a/test/e_changes.test b/test/e_changes.test index a77e22a2ee..2eb77d3130 100644 --- a/test/e_changes.test +++ b/test/e_changes.test @@ -25,10 +25,10 @@ proc do_changes_test {tn sql res} { #-------------------------------------------------------------------------- -# EVIDENCE-OF: R-15996-49369 This function returns the number of rows -# modified, inserted or deleted by the most recently completed INSERT, -# UPDATE or DELETE statement on the database connection specified by the -# only parameter. +# EVIDENCE-OF: R-58361-29089 The changes() function returns the number +# of database rows that were changed or inserted or deleted by the most +# recently completed INSERT, DELETE, or UPDATE statement, exclusive of +# statements in lower-level triggers. # do_execsql_test 1.0 { CREATE TABLE t1(a, b); @@ -108,7 +108,7 @@ foreach {tn schema} { #-------------------------------------------------------------------------- -# EVIDENCE-OF: R-44877-05564 Executing any other type of SQL statement +# X-EVIDENCE-OF: R-44877-05564 Executing any other type of SQL statement # does not modify the value returned by this function. # reset_db @@ -123,7 +123,9 @@ do_changes_test 2.2 { do_changes_test 2.3 { SELECT count(x) FROM t1 } {47 47} do_changes_test 2.4 { DROP TABLE t1 } 47 do_changes_test 2.5 { CREATE TABLE t1(x) } 47 -do_changes_test 2.6 { ALTER TABLE t1 ADD COLUMN b } 47 +ifcapable altertable { + do_changes_test 2.6 { ALTER TABLE t1 ADD COLUMN b } 47 +} #-------------------------------------------------------------------------- diff --git a/test/e_createtable.test b/test/e_createtable.test index f07fbb9c74..92ccc80323 100644 --- a/test/e_createtable.test +++ b/test/e_createtable.test @@ -395,17 +395,19 @@ do_createtable_tests 1.2.2 { 4 {CREATE TABLE auxb.xyz(z)} {} } drop_all_tables -do_createtable_tests 1.3 -tclquery { - unset -nocomplain X - array set X [table_list] - list $X(main) $X(temp) $X(auxa) $X(auxb) -} { - 1 "CREATE TABLE main.abc(a, b, c)" {abc {} {} {}} - 2 "CREATE TABLE main.t1(a, b, c)" {{abc t1} {} {} {}} - 3 "CREATE TABLE temp.tmp(a, b, c)" {{abc t1} tmp {} {}} - 4 "CREATE TABLE auxb.tbl(x, y)" {{abc t1} tmp {} tbl} - 5 "CREATE TABLE auxb.t1(k, v)" {{abc t1} tmp {} {t1 tbl}} - 6 "CREATE TABLE auxa.next(c, d)" {{abc t1} tmp next {t1 tbl}} +if {[permutation]!="maindbname"} { + do_createtable_tests 1.3 -tclquery { + unset -nocomplain X + array set X [table_list] + list $X(main) $X(temp) $X(auxa) $X(auxb) + } { + 1 "CREATE TABLE main.abc(a, b, c)" {abc {} {} {}} + 2 "CREATE TABLE main.t1(a, b, c)" {{abc t1} {} {} {}} + 3 "CREATE TABLE temp.tmp(a, b, c)" {{abc t1} tmp {} {}} + 4 "CREATE TABLE auxb.tbl(x, y)" {{abc t1} tmp {} tbl} + 5 "CREATE TABLE auxb.t1(k, v)" {{abc t1} tmp {} {t1 tbl}} + 6 "CREATE TABLE auxa.next(c, d)" {{abc t1} tmp next {t1 tbl}} + } } # EVIDENCE-OF: R-18895-27365 If the "TEMP" or "TEMPORARY" keyword occurs @@ -413,13 +415,15 @@ do_createtable_tests 1.3 -tclquery { # temp database. # drop_all_tables -do_createtable_tests 1.4 -tclquery { - unset -nocomplain X - array set X [table_list] - list $X(main) $X(temp) $X(auxa) $X(auxb) -} { - 1 "CREATE TEMP TABLE t1(a, b)" {{} t1 {} {}} - 2 "CREATE TEMPORARY TABLE t2(a, b)" {{} {t1 t2} {} {}} +if {[permutation]!="maindbname"} { + do_createtable_tests 1.4 -tclquery { + unset -nocomplain X + array set X [table_list] + list $X(main) $X(temp) $X(auxa) $X(auxb) + } { + 1 "CREATE TEMP TABLE t1(a, b)" {{} t1 {} {}} + 2 "CREATE TEMPORARY TABLE t2(a, b)" {{} {t1 t2} {} {}} + } } # EVIDENCE-OF: R-23976-43329 It is an error to specify both a @@ -436,30 +440,34 @@ do_createtable_tests 1.5.1 -error { 4 "CREATE TEMPORARY TABLE main.xxx(x)" {} } drop_all_tables -do_createtable_tests 1.5.2 -tclquery { - unset -nocomplain X - array set X [table_list] - list $X(main) $X(temp) $X(auxa) $X(auxb) -} { - 1 "CREATE TEMP TABLE temp.t1(a, b)" {{} t1 {} {}} - 2 "CREATE TEMPORARY TABLE temp.t2(a, b)" {{} {t1 t2} {} {}} - 3 "CREATE TEMP TABLE TEMP.t3(a, b)" {{} {t1 t2 t3} {} {}} - 4 "CREATE TEMPORARY TABLE TEMP.xxx(x)" {{} {t1 t2 t3 xxx} {} {}} +if {[permutation]!="maindbname"} { + do_createtable_tests 1.5.2 -tclquery { + unset -nocomplain X + array set X [table_list] + list $X(main) $X(temp) $X(auxa) $X(auxb) + } { + 1 "CREATE TEMP TABLE temp.t1(a, b)" {{} t1 {} {}} + 2 "CREATE TEMPORARY TABLE temp.t2(a, b)" {{} {t1 t2} {} {}} + 3 "CREATE TEMP TABLE TEMP.t3(a, b)" {{} {t1 t2 t3} {} {}} + 4 "CREATE TEMPORARY TABLE TEMP.xxx(x)" {{} {t1 t2 t3 xxx} {} {}} + } } # EVIDENCE-OF: R-31997-24564 If no schema name is specified and the TEMP # keyword is not present then the table is created in the main database. # drop_all_tables -do_createtable_tests 1.6 -tclquery { - unset -nocomplain X - array set X [table_list] - list $X(main) $X(temp) $X(auxa) $X(auxb) -} { - 1 "CREATE TABLE t1(a, b)" {t1 {} {} {}} - 2 "CREATE TABLE t2(a, b)" {{t1 t2} {} {} {}} - 3 "CREATE TABLE t3(a, b)" {{t1 t2 t3} {} {} {}} - 4 "CREATE TABLE xxx(x)" {{t1 t2 t3 xxx} {} {} {}} +if {[permutation]!="maindbname"} { + do_createtable_tests 1.6 -tclquery { + unset -nocomplain X + array set X [table_list] + list $X(main) $X(temp) $X(auxa) $X(auxb) + } { + 1 "CREATE TABLE t1(a, b)" {t1 {} {} {}} + 2 "CREATE TABLE t2(a, b)" {{t1 t2} {} {} {}} + 3 "CREATE TABLE t3(a, b)" {{t1 t2 t3} {} {} {}} + 4 "CREATE TABLE xxx(x)" {{t1 t2 t3 xxx} {} {} {}} + } } drop_all_tables @@ -485,10 +493,10 @@ do_execsql_test e_createtable-1.7.0 { do_createtable_tests 1.7.1 -error { %s } { 1 "CREATE TABLE t1(a, b)" {{table t1 already exists}} 2 "CREATE TABLE i1(a, b)" {{there is already an index named i1}} - 3 "CREATE TABLE v1(a, b)" {{table v1 already exists}} + 3 "CREATE TABLE v1(a, b)" {{view v1 already exists}} 4 "CREATE TABLE auxa.tbl1(a, b)" {{table tbl1 already exists}} 5 "CREATE TABLE auxa.idx1(a, b)" {{there is already an index named idx1}} - 6 "CREATE TABLE auxa.view1(a, b)" {{table view1 already exists}} + 6 "CREATE TABLE auxa.view1(a, b)" {{view view1 already exists}} } do_createtable_tests 1.7.2 { 1 "CREATE TABLE auxa.t1(a, b)" {} @@ -656,11 +664,11 @@ do_createtable_tests 2.1 -tclquery { 5 "CREATE TABLE x1 AS SELECT count(a) AS a, max(b) FROM t1" {a max(b)} } -# EVIDENCE-OF: R-37111-22855 The declared type of each column is +# EVIDENCE-OF: R-55407-45319 The declared type of each column is # determined by the expression affinity of the corresponding expression # in the result set of the SELECT statement, as follows: Expression # Affinity Column Declared Type TEXT "TEXT" NUMERIC "NUM" INTEGER "INT" -# REAL "REAL" NONE "" (empty string) +# REAL "REAL" BLOB (a.k.a "NONE") "" (empty string) # do_createtable_tests 2.2 -tclquery { table_column_decltypes x1 @@ -883,8 +891,8 @@ do_execsql_test e_createtable-3.3.1 { ); } {} -# EVIDENCE-OF: R-18415-27776 For the purposes of the DEFAULT clause, an -# expression is considered constant if it does contains no sub-queries, +# EVIDENCE-OF: R-33440-07331 For the purposes of the DEFAULT clause, an +# expression is considered constant if it contains no sub-queries, # column or table references, bound parameters, or string literals # enclosed in double-quotes instead of single-quotes. # @@ -1264,9 +1272,10 @@ do_createtable_tests 4.4 { 14 "INSERT INTO t2 VALUES(NULL, NULL)" {} } -# EVIDENCE-OF: R-35113-43214 Unless the column is an INTEGER PRIMARY KEY -# or the table is a WITHOUT ROWID table or the column is declared NOT -# NULL, SQLite allows NULL values in a PRIMARY KEY column. +# EVIDENCE-OF: R-40010-16873 Unless the column is an INTEGER PRIMARY KEY +# or the table is a WITHOUT ROWID table or a STRICT table or the column +# is declared NOT NULL, SQLite allows NULL values in a PRIMARY KEY +# column. # # If the column is an integer primary key, attempting to insert a NULL # into the column triggers the auto-increment behavior. Attempting @@ -1296,6 +1305,14 @@ do_catchsql_test 4.5.5 { CREATE TABLE t5(s, u INT PRIMARY KEY NOT NULL, v); INSERT INTO t5 VALUES(1, NULL, 2); } {1 {NOT NULL constraint failed: t5.u}} +do_catchsql_test 4.5.6 { + CREATE TABLE t6(s INT, u INT PRIMARY KEY, v INT) STRICT; + INSERT INTO t6 VALUES(1, NULL, 2); +} {1 {NOT NULL constraint failed: t6.u}} +do_catchsql_test 4.5.7 { + CREATE TABLE t7(s INT, u INT PRIMARY KEY NOT NULL, v INT) STRICT; + INSERT INTO t7 VALUES(1, NULL, 2); +} {1 {NOT NULL constraint failed: t7.u}} # EVIDENCE-OF: R-00227-21080 A UNIQUE constraint is similar to a PRIMARY # KEY constraint, except that a single table may have any number of @@ -1385,13 +1402,13 @@ do_execsql_test 4.10.0 { } do_createtable_tests 4.10 { 1 "EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE b = 5" - {0 0 0 {SEARCH TABLE t1 USING INDEX sqlite_autoindex_t1_1 (b=?)}} + {/*SEARCH t1 USING INDEX sqlite_autoindex_t1_1 (b=?)*/} 2 "EXPLAIN QUERY PLAN SELECT * FROM t2 ORDER BY b, c" - {0 0 0 {SCAN TABLE t2 USING INDEX sqlite_autoindex_t2_1}} + {/*SCAN t2 USING INDEX sqlite_autoindex_t2_1*/} 3 "EXPLAIN QUERY PLAN SELECT * FROM t2 WHERE b=10 AND c>10" - {0 0 0 {SEARCH TABLE t2 USING INDEX sqlite_autoindex_t2_1 (b=? AND c>?)}} + {/*SEARCH t2 USING INDEX sqlite_autoindex_t2_1 (b=? AND c>?)*/} } # EVIDENCE-OF: R-45493-35653 A CHECK constraint may be attached to a @@ -1425,20 +1442,20 @@ do_execsql_test 4.11 { } do_createtable_tests 4.11 -error {CHECK constraint failed: %s} { - 1a "INSERT INTO x1 VALUES('one', 0)" {x1} - 1b "INSERT INTO t1 VALUES('one', -4.0)" {t1} + 1a "INSERT INTO x1 VALUES('one', 0)" {b>0} + 1b "INSERT INTO t1 VALUES('one', -4.0)" {b>0} - 2a "INSERT INTO x2 VALUES('abc', 1)" {x2} - 2b "INSERT INTO t2 VALUES('abc', 1)" {t2} + 2a "INSERT INTO x2 VALUES('abc', 1)" {a||b} + 2b "INSERT INTO t2 VALUES('abc', 1)" {a||b} - 3a "INSERT INTO x2 VALUES(0, 'abc')" {x2} - 3b "INSERT INTO t2 VALUES(0, 'abc')" {t2} + 3a "INSERT INTO x2 VALUES(0, 'abc')" {a||b} + 3b "INSERT INTO t2 VALUES(0, 'abc')" {a||b} - 4a "UPDATE t1 SET b=-1 WHERE rowid=1" {t1} - 4b "UPDATE x1 SET b=-1 WHERE rowid=1" {x1} + 4a "UPDATE t1 SET b=-1 WHERE rowid=1" {b>0} + 4b "UPDATE x1 SET b=-1 WHERE rowid=1" {b>0} - 4a "UPDATE x2 SET a='' WHERE rowid=1" {x2} - 4b "UPDATE t2 SET a='' WHERE rowid=1" {t2} + 4a "UPDATE x2 SET a='' WHERE rowid=1" {a||b} + 4b "UPDATE t2 SET a='' WHERE rowid=1" {a||b} } # EVIDENCE-OF: R-34109-39108 If the CHECK expression evaluates to NULL, @@ -1497,9 +1514,10 @@ do_createtable_tests 4.14 -error {NOT NULL constraint failed: %s} { 5 "INSERT INTO t3 VALUES(NULL, 'g', 'h')" {t3.a} } -# EVIDENCE-OF: R-42511-39459 PRIMARY KEY, UNIQUE and NOT NULL -# constraints may be explicitly assigned a default conflict resolution -# algorithm by including a conflict-clause in their definitions. +# EVIDENCE-OF: R-34093-09213 PRIMARY KEY, UNIQUE and NOT NULL +# constraints may be explicitly assigned another default conflict +# resolution algorithm by including a conflict-clause in their +# definitions. # # Conflict clauses: ABORT, ROLLBACK, IGNORE, FAIL, REPLACE # @@ -1614,9 +1632,9 @@ foreach {tn tbl res ac data} { } catchsql COMMIT -# EVIDENCE-OF: R-12645-39772 Or, if a constraint definition does not -# include a conflict-clause or it is a CHECK constraint, the default -# conflict resolution algorithm is ABORT. +# EVIDENCE-OF: R-17539-59899 Or, if a constraint definition does not +# include a conflict-clause, the default conflict resolution algorithm +# is ABORT. # # The first half of the above is tested along with explicit ON # CONFLICT clauses above (specifically, the tests involving t1_xx, t2_xx @@ -1631,7 +1649,7 @@ do_execsql_test 4.18.1 { do_execsql_test 4.18.2 { BEGIN; INSERT INTO t4 VALUES(5, 6) } do_catchsql_test 4.18.3 { INSERT INTO t4 SELECT a+4, b+4 FROM t4 -} {1 {CHECK constraint failed: t4}} +} {1 {CHECK constraint failed: b!=10}} do_test e_createtable-4.18.4 { sqlite3_get_autocommit db } 0 do_execsql_test 4.18.5 { SELECT * FROM t4 } {1 2 3 4 5 6} diff --git a/test/e_droptrigger.test b/test/e_droptrigger.test index 84dfe7279a..5cd5d0a07b 100644 --- a/test/e_droptrigger.test +++ b/test/e_droptrigger.test @@ -127,8 +127,8 @@ foreach {tn tbl droptrigger before after} { } $after } -# EVIDENCE-OF: R-50239-29811 Once removed, the trigger definition is no -# longer present in the sqlite_master (or sqlite_temp_master) table and +# EVIDENCE-OF: R-04950-25529 Once removed, the trigger definition is no +# longer present in the sqlite_schema (or sqlite_temp_schema) table and # is not fired by any subsequent INSERT, UPDATE or DELETE statements. # # Test cases e_droptrigger-1.* test the first part of this statement diff --git a/test/e_dropview.test b/test/e_dropview.test index 143dce2907..00f59ddc4f 100644 --- a/test/e_dropview.test +++ b/test/e_dropview.test @@ -45,7 +45,7 @@ proc list_all_views {{db db}} { set res [list] $db eval { PRAGMA database_list } { set tbl "$name.sqlite_master" - if {$name == "temp"} { set tbl sqlite_temp_master } + if {$name == "temp"} { set tbl temp.sqlite_master } set sql "SELECT '$name.' || name FROM $tbl WHERE type = 'view'" lappend res {*}[$db eval $sql] @@ -126,37 +126,37 @@ do_execsql_test 3.1.0 { SELECT * FROM temp.v1 } {{a temp} {b temp}} do_execsql_test 3.1.1 { DROP VIEW temp.v1 } {} do_catchsql_test 3.1.2 { SELECT * FROM temp.v1 } {1 {no such table: temp.v1}} do_test 3.1.3 { list_all_views } {main.v1 main.v2 aux.v1 aux.v2 aux.v3} -do_test 3.1.4 { list_all_data } $databasedata +do_test 3.1.4 { string compare [list_all_data] $databasedata } 0 do_execsql_test 3.2.0 { SELECT * FROM v1 } {{a main} {b main}} do_execsql_test 3.2.1 { DROP VIEW v1 } {} do_catchsql_test 3.2.2 { SELECT * FROM main.v1 } {1 {no such table: main.v1}} do_test 3.2.3 { list_all_views } {main.v2 aux.v1 aux.v2 aux.v3} -do_test 3.2.4 { list_all_data } $databasedata +do_test 3.2.4 { string compare [list_all_data] $databasedata } 0 do_execsql_test 3.3.0 { SELECT * FROM v2 } {{a main} {b main}} do_execsql_test 3.3.1 { DROP VIEW v2 } {} do_catchsql_test 3.3.2 { SELECT * FROM main.v2 } {1 {no such table: main.v2}} do_test 3.3.3 { list_all_views } {aux.v1 aux.v2 aux.v3} -do_test 3.3.4 { list_all_data } $databasedata +do_test 3.3.4 { string compare [list_all_data] $databasedata } 0 do_execsql_test 3.4.0 { SELECT * FROM v1 } {{a aux} {b aux}} do_execsql_test 3.4.1 { DROP VIEW v1 } {} do_catchsql_test 3.4.2 { SELECT * FROM v1 } {1 {no such table: v1}} do_test 3.4.3 { list_all_views } {aux.v2 aux.v3} -do_test 3.4.4 { list_all_data } $databasedata +do_test 3.4.4 { string compare [list_all_data] $databasedata } 0 -do_execsql_test 3.4.0 { SELECT * FROM aux.v2 } {{a aux} {b aux}} -do_execsql_test 3.4.1 { DROP VIEW aux.v2 } {} -do_catchsql_test 3.4.2 { SELECT * FROM aux.v2 } {1 {no such table: aux.v2}} -do_test 3.4.3 { list_all_views } {aux.v3} -do_test 3.4.4 { list_all_data } $databasedata +do_execsql_test 3.5.0 { SELECT * FROM aux.v2 } {{a aux} {b aux}} +do_execsql_test 3.5.1 { DROP VIEW aux.v2 } {} +do_catchsql_test 3.5.2 { SELECT * FROM aux.v2 } {1 {no such table: aux.v2}} +do_test 3.5.3 { list_all_views } {aux.v3} +do_test 3.5.4 { string compare [list_all_data] $databasedata } 0 -do_execsql_test 3.5.0 { SELECT * FROM v3 } {{a aux} {b aux}} -do_execsql_test 3.5.1 { DROP VIEW v3 } {} -do_catchsql_test 3.5.2 { SELECT * FROM v3 } {1 {no such table: v3}} -do_test 3.5.3 { list_all_views } {} -do_test 3.5.4 { list_all_data } $databasedata +do_execsql_test 3.6.0 { SELECT * FROM v3 } {{a aux} {b aux}} +do_execsql_test 3.6.1 { DROP VIEW v3 } {} +do_catchsql_test 3.6.2 { SELECT * FROM v3 } {1 {no such table: v3}} +do_test 3.6.3 { list_all_views } {} +do_test 3.6.4 { string compare [list_all_data] $databasedata } 0 # EVIDENCE-OF: R-25558-37487 If the specified view cannot be found and # the IF EXISTS clause is not present, it is an error. @@ -179,11 +179,11 @@ do_dropview_tests 5 -repair { dropview_reopen_db } -tclquery { list_all_views - expr {[list_all_views] == "main.v1 main.v2 temp.v1 aux.v1 aux.v2 aux.v3"} + #expr {[list_all_views] == "main.v1 main.v2 temp.v1 aux.v1 aux.v2 aux.v3"} } { - 1 "DROP VIEW IF EXISTS xx" 1 - 2 "DROP VIEW IF EXISTS main.xx" 1 - 3 "DROP VIEW IF EXISTS temp.v2" 1 + 1 "DROP VIEW IF EXISTS xx" "main.v1 main.v2 temp.v1 aux.v1 aux.v2 aux.v3" + 2 "DROP VIEW IF EXISTS main.xx" "main.v1 main.v2 temp.v1 aux.v1 aux.v2 aux.v3" + 3 "DROP VIEW IF EXISTS temp.v2" "main.v1 main.v2 temp.v1 aux.v1 aux.v2 aux.v3" } diff --git a/test/e_expr.test b/test/e_expr.test index 8c0957f8d3..81d2fd172c 100644 --- a/test/e_expr.test +++ b/test/e_expr.test @@ -84,12 +84,12 @@ db func regexp -argcount 2 regexfunc # in the documentation exist and that the relative precedences of the # operators are also as the documentation suggests. # -# EVIDENCE-OF: R-15514-65163 SQLite understands the following binary +# X-EVIDENCE-OF: R-15514-65163 SQLite understands the following binary # operators, in order from highest to lowest precedence: || * / % + - # << >> & | < <= > >= = == != <> IS IS # NOT IN LIKE GLOB MATCH REGEXP AND OR # -# EVIDENCE-OF: R-38759-38789 Operators IS and IS NOT have the same +# X-EVIDENCE-OF: R-38759-38789 Operators IS and IS NOT have the same # precedence as =. # @@ -180,7 +180,7 @@ do_execsql_test e_expr-1.6 { # Check that the four unary prefix operators mentioned in the # documentation exist. # -# EVIDENCE-OF: R-13958-53419 Supported unary prefix operators are these: +# X-EVIDENCE-OF: R-13958-53419 Supported unary prefix operators are these: # - + ~ NOT # do_execsql_test e_expr-2.1 { SELECT - 10 } {-10} @@ -215,7 +215,7 @@ foreach {tn literal type} { # # EVIDENCE-OF: R-03679-60639 Equals can be either = or ==. # -# EVIDENCE-OF: R-30082-38996 The non-equals operator can be either != or +# EVIDENCE-OF: R-49372-18364 The not-equal operator can be either != or # <>. # foreach {tn literal different} { @@ -254,22 +254,21 @@ foreach {tn a b} { #------------------------------------------------------------------------- # Test the % operator. # -# EVIDENCE-OF: R-08914-63790 The operator % outputs the value of its -# left operand modulo its right operand. +# EVIDENCE-OF: R-53431-59159 The % operator casts both of its operands +# to type INTEGER and then computes the remainder after dividing the +# left integer by the right integer. # do_execsql_test e_expr-6.1 {SELECT 72%5} {2} do_execsql_test e_expr-6.2 {SELECT 72%-5} {2} do_execsql_test e_expr-6.3 {SELECT -72%-5} {-2} do_execsql_test e_expr-6.4 {SELECT -72%5} {-2} +do_execsql_test e_expr-6.5 {SELECT 72.35%5} {2.0} #------------------------------------------------------------------------- -# Test that the results of all binary operators are either numeric or -# NULL, except for the || operator, which may evaluate to either a text -# value or NULL. -# -# EVIDENCE-OF: R-20665-17792 The result of any binary operator is either -# a numeric value or NULL, except for the || concatenation operator -# which always evaluates to either NULL or a text value. +# EVIDENCE-OF: R-15904-00746 The result of any binary operator is either +# a numeric value or NULL, except for the || concatenation operator, and +# the -> and ->> extract operators which evaluate to either +# NULL or a text value. # set literals { 1 'abc' 2 'hexadecimal' 3 '' @@ -279,6 +278,10 @@ set literals { 13 NULL } foreach op $oplist { + if {$op eq "AND" || $op eq "OR"} { + # These tests do not work with AND and OR due to short-circuit evaluation + continue + } foreach {n1 rhs} $literals { foreach {n2 lhs} $literals { @@ -366,7 +369,7 @@ db collate reverse reverse_collate # EVIDENCE-OF: R-59577-33471 The COLLATE operator is a unary postfix # operator that assigns a collating sequence to an expression. # -# EVIDENCE-OF: R-36231-30731 The COLLATE operator has a higher +# X-EVIDENCE-OF: R-36231-30731 The COLLATE operator has a higher # precedence (binds more tightly) than any binary operator and any unary # prefix operator except "~". # @@ -847,6 +850,9 @@ foreach {tn x expr res nEval} { 3 5 "x() >= 5 AND x() <= 5" 1 2 4 5 "x() BETWEEN 5 AND 5" 1 1 + + 5 9 "(x(),8) >= (9,7) AND (x(),8)<=(9,10)" 1 2 + 6 9 "(x(),8) BETWEEN (9,7) AND (9,10)" 1 1 } { do_test e_expr-13.1.$tn { set ::xcount 0 @@ -855,7 +861,7 @@ foreach {tn x expr res nEval} { } [list $nEval $res] } -# EVIDENCE-OF: R-05155-34454 The precedence of the BETWEEN operator is +# X-EVIDENCE-OF: R-05155-34454 The precedence of the BETWEEN operator is # the same as the precedence as operators == and != and LIKE and groups # left to right. # @@ -1009,13 +1015,18 @@ sqlite3 db test.db # EVIDENCE-OF: R-22868-25880 The LIKE operator can be made case # sensitive using the case_sensitive_like pragma. # -do_execsql_test e_expr-16.1.1 { SELECT 'abcxyz' LIKE 'ABC%' } 1 -do_execsql_test e_expr-16.1.2 { PRAGMA case_sensitive_like = 1 } {} -do_execsql_test e_expr-16.1.3 { SELECT 'abcxyz' LIKE 'ABC%' } 0 -do_execsql_test e_expr-16.1.4 { SELECT 'ABCxyz' LIKE 'ABC%' } 1 -do_execsql_test e_expr-16.1.5 { PRAGMA case_sensitive_like = 0 } {} -do_execsql_test e_expr-16.1.6 { SELECT 'abcxyz' LIKE 'ABC%' } 1 -do_execsql_test e_expr-16.1.7 { SELECT 'ABCxyz' LIKE 'ABC%' } 1 +do_execsql_test e_expr-16.1.1 { SELECT 'abcxyz' LIKE 'ABC%' } 1 +do_execsql_test e_expr-16.1.1b { SELECT 'abc%xyz' LIKE 'ABC\%x%' ESCAPE '\' } 1 +do_execsql_test e_expr-16.1.2 { PRAGMA case_sensitive_like = 1 } {} +do_execsql_test e_expr-16.1.3 { SELECT 'abcxyz' LIKE 'ABC%' } 0 +do_execsql_test e_expr-16.1.3b { SELECT 'abc%xyz' LIKE 'ABC\%X%' ESCAPE '\' } 0 +do_execsql_test e_expr-16.1.4 { SELECT 'ABCxyz' LIKE 'ABC%' } 1 +do_execsql_test e_expr-16.1.4b { SELECT 'ABC%xyz' LIKE 'ABC\%x%' ESCAPE '\' } 1 +do_execsql_test e_expr-16.1.5 { PRAGMA case_sensitive_like = 0 } {} +do_execsql_test e_expr-16.1.6 { SELECT 'abcxyz' LIKE 'ABC%' } 1 +do_execsql_test e_expr-16.1.6b { SELECT 'abc%xyz' LIKE 'ABC\%X%' ESCAPE '\' } 1 +do_execsql_test e_expr-16.1.7 { SELECT 'ABCxyz' LIKE 'ABC%' } 1 +do_execsql_test e_expr-16.1.7b { SELECT 'ABC%xyz' LIKE 'ABC\%X%' ESCAPE '\' } 1 # EVIDENCE-OF: R-52087-12043 The GLOB operator is similar to LIKE but # uses the Unix file globbing syntax for its wildcards. @@ -1130,7 +1141,7 @@ sqlite3 db test.db #------------------------------------------------------------------------- # Test cases for the testable statements related to the CASE expression. # -# EVIDENCE-OF: R-15199-61389 There are two basic forms of the CASE +# EVIDENCE-OF: R-57495-24088 There are two fundamental forms of the CASE # expression: those with a base expression and those without. # do_execsql_test e_expr-20.1 { @@ -1225,12 +1236,18 @@ db nullvalue {} # EVIDENCE-OF: R-13943-13592 A NULL result is considered untrue when # evaluating WHEN terms. # -do_execsql_test e_expr-21.4.1 { - SELECT CASE WHEN NULL THEN 'A' WHEN 1 THEN 'B' END -} {B} -do_execsql_test e_expr-21.4.2 { - SELECT CASE WHEN 0 THEN 'A' WHEN NULL THEN 'B' ELSE 'C' END -} {C} +do_execsql_test e_expr-21.4.1a { + SELECT CASE WHEN NULL THEN 'A' WHEN 1 THEN 'B' END, iif(NULL,8,99); +} {B 99} +do_execsql_test e_expr-21.4.1b { + SELECT CASE WHEN NULL THEN 'A' WHEN 1 THEN 'B' END, if(NULL,8,99); +} {B 99} +do_execsql_test e_expr-21.4.2a { + SELECT CASE WHEN 0 THEN 'A' WHEN NULL THEN 'B' ELSE 'C' END, iif(0,8,99); +} {C 99} +do_execsql_test e_expr-21.4.2b { + SELECT CASE WHEN 0 THEN 'A' WHEN NULL THEN 'B' ELSE 'C' END, if(0,8,99); +} {C 99} # EVIDENCE-OF: R-38620-19499 In a CASE with a base expression, the base # expression is evaluated just once and the result is compared against @@ -1434,6 +1451,13 @@ do_expr_test e_expr-27.2.2 { CAST(NULL AS text) } null {} do_expr_test e_expr-27.2.3 { CAST(NULL AS blob) } null {} do_expr_test e_expr-27.2.4 { CAST(NULL AS number) } null {} +# EVIDENCE-OF: R-29283-15561 Otherwise, the storage class of the result +# is determined by applying the rules for determining column affinity to +# the type-name. +# +# The R-29283-15561 requirement above is demonstrated by all of the +# subsequent e_expr-26 tests. +# # EVIDENCE-OF: R-43522-35548 Casting a value to a type-name with no # affinity causes the value to be converted into a BLOB. # @@ -1628,16 +1652,47 @@ do_expr_test e_expr-31.2.4 { } integer 9223372036854775807 -# EVIDENCE-OF: R-09295-61337 Casting a TEXT or BLOB value into NUMERIC -# first does a forced conversion into REAL but then further converts the -# result into INTEGER if and only if the conversion from REAL to INTEGER -# is lossless and reversible. +# EVIDENCE-OF: R-55084-10555 Casting a TEXT or BLOB value into NUMERIC +# yields either an INTEGER or a REAL result. +# +# EVIDENCE-OF: R-48945-04866 If the input text looks like an integer +# (there is no decimal point nor exponent) and the value is small enough +# to fit in a 64-bit signed integer, then the result will be INTEGER. +# +# EVIDENCE-OF: R-47045-23194 Input text that looks like floating point +# (there is a decimal point and/or an exponent) and the text describes a +# value that can be losslessly converted back and forth between IEEE 754 +# 64-bit float and a 51-bit signed integer, then the result is INTEGER. # do_expr_test e_expr-32.1.1 { CAST('45' AS NUMERIC) } integer 45 do_expr_test e_expr-32.1.2 { CAST('45.0' AS NUMERIC) } integer 45 do_expr_test e_expr-32.1.3 { CAST('45.2' AS NUMERIC) } real 45.2 do_expr_test e_expr-32.1.4 { CAST('11abc' AS NUMERIC) } integer 11 do_expr_test e_expr-32.1.5 { CAST('11.1abc' AS NUMERIC) } real 11.1 +do_expr_test e_expr-32.1.6 {CAST( '9.223372036e14' AS NUMERIC)} integer 922337203600000 +do_expr_test e_expr-32.1.7 {CAST('-9.223372036e14' AS NUMERIC)} integer -922337203600000 +do_test e_expr-32.1.8 { + set expr {CAST( '9.223372036e15' AS NUMERIC)} + db eval "SELECT typeof($expr) AS type, printf('%.5e',$expr) AS value" break; + list $type $value +} {real 9.22337e+15} +do_test e_expr-32.1.9 { + set expr {CAST('-9.223372036e15' AS NUMERIC)} + db eval "SELECT typeof($expr) AS type, printf('%.5e',$expr) AS value" break; + list $type $value +} {real -9.22337e+15} + +# EVIDENCE-OF: R-50300-26941 Any text input that describes a value +# outside the range of a 64-bit signed integer yields a REAL result. +# +do_expr_test e_expr-32.1.20 { CAST('9223372036854775807' AS numeric) } \ + integer 9223372036854775807 +do_expr_test e_expr-32.1.21 { CAST('9223372036854775808' AS numeric) } \ + real 9.22337203685478e+18 +do_expr_test e_expr-32.1.22 { CAST('-9223372036854775808' AS numeric) } \ + integer -9223372036854775808 +do_expr_test e_expr-32.1.23 { CAST('-9223372036854775809' AS numeric) } \ + real -9.22337203685478e+18 # EVIDENCE-OF: R-30347-18702 Casting a REAL or INTEGER value to NUMERIC # is a no-op, even if a real value could be losslessly converted to an @@ -1652,6 +1707,50 @@ do_expr_test e_expr-32.2.3 { do_expr_test e_expr-32.2.4 { CAST(9223372036854775807 AS NUMERIC) } integer 9223372036854775807 +do_expr_test e_expr-32.2.5 { + CAST('9223372036854775807 ' AS NUMERIC) +} integer 9223372036854775807 +do_expr_test e_expr-32.2.6 { + CAST(' 9223372036854775807 ' AS NUMERIC) +} integer 9223372036854775807 +do_expr_test e_expr-32.2.7 { + CAST(' ' AS NUMERIC) +} integer 0 +do_execsql_test e_expr-32.2.8 { + WITH t1(x) AS (VALUES + ('9000000000000000001'), + ('9000000000000000001x'), + ('9000000000000000001 '), + (' 9000000000000000001 '), + (' 9000000000000000001'), + (' 9000000000000000001.'), + ('9223372036854775807'), + ('9223372036854775807 '), + (' 9223372036854775807 '), + ('9223372036854775808'), + (' 9223372036854775808 '), + ('9223372036854775807.0'), + ('9223372036854775807e+0'), + ('-5.0'), + ('-5e+0')) + SELECT typeof(CAST(x AS NUMERIC)), CAST(x AS NUMERIC)||'' FROM t1; +} [list \ + integer 9000000000000000001 \ + integer 9000000000000000001 \ + integer 9000000000000000001 \ + integer 9000000000000000001 \ + integer 9000000000000000001 \ + real 9.0e+18 \ + integer 9223372036854775807 \ + integer 9223372036854775807 \ + integer 9223372036854775807 \ + real 9.22337203685478e+18 \ + real 9.22337203685478e+18 \ + real 9.22337203685478e+18 \ + real 9.22337203685478e+18 \ + integer -5 \ + integer -5 \ +] # EVIDENCE-OF: R-64550-29191 Note that the result from casting any # non-BLOB value into a BLOB and the result from casting any BLOB value @@ -1783,8 +1882,8 @@ do_test e_expr-35.0 { } } {} -# EVIDENCE-OF: R-00980-39256 A SELECT statement enclosed in parentheses -# may appear as a scalar quantity. +# EVIDENCE-OF: R-43573-23448 A SELECT statement enclosed in parentheses +# is a subquery. # # EVIDENCE-OF: R-56294-03966 All types of SELECT statement, including # aggregate and compound SELECT queries (queries with keywords like @@ -1803,13 +1902,15 @@ do_expr_test e_expr-35.1.6 { (SELECT a FROM t2 UNION SELECT COALESCE(b, 55) FROM t2 ORDER BY 1) } integer 4 -# EVIDENCE-OF: R-46899-53765 A SELECT used as a scalar quantity must -# return a result set with a single column. +# EVIDENCE-OF: R-43101-20178 A subquery that returns two or more columns +# is a row value subquery and can only be used as an operand of a +# comparison operator or as the value in an UPDATE SET clause whose +# column name list has the same size. # # The following block tests that errors are returned in a bunch of cases # where a subquery returns more than one column. # -set M {only a single result allowed for a SELECT that is part of an expression} +set M {/1 {sub-select returns [23] columns - expected 1}/} foreach {tn sql} { 1 { SELECT (SELECT * FROM t2 UNION SELECT a+1, b+1 FROM t2) } 2 { SELECT (SELECT * FROM t2 UNION SELECT a+1, b+1 FROM t2 ORDER BY 1) } @@ -1818,14 +1919,11 @@ foreach {tn sql} { 5 { SELECT (SELECT * FROM t2) } 6 { SELECT (SELECT * FROM (SELECT 1, 2, 3)) } } { - do_catchsql_test e_expr-35.2.$tn $sql [list 1 $M] + do_catchsql_test e_expr-35.2.$tn $sql $M } -# EVIDENCE-OF: R-35764-28041 The result of the expression is the value -# of the only column in the first row returned by the SELECT statement. -# -# EVIDENCE-OF: R-41898-06686 If the SELECT yields more than one result -# row, all rows after the first are ignored. +# EVIDENCE-OF: R-18318-14995 The value of a subquery expression is the +# first row of the result from the enclosed SELECT statement. # do_execsql_test e_expr-36.3.1 { CREATE TABLE t4(x, y); @@ -1842,15 +1940,15 @@ foreach {tn expr restype resval} { 6 { ( SELECT y FROM t4 ORDER BY y DESC ) } text two 7 { ( SELECT sum(x) FROM t4 ) } integer 6 - 8 { ( SELECT group_concat(y,'') FROM t4 ) } text onetwothree + 8 { ( SELECT string_agg(y,'') FROM t4 ) } text onetwothree 9 { ( SELECT max(x) FROM t4 WHERE y LIKE '___') } integer 2 } { do_expr_test e_expr-36.3.$tn $expr $restype $resval } -# EVIDENCE-OF: R-25492-41572 If the SELECT yields no rows, then the -# value of the expression is NULL. +# EVIDENCE-OF: R-52325-25449 The value of a subquery expression is NULL +# if the enclosed SELECT statement returns no rows. # foreach {tn expr} { 1 { ( SELECT x FROM t4 WHERE x>3 ORDER BY x ) } @@ -1863,39 +1961,42 @@ foreach {tn expr} { # 'english' and '0' are all considered to be false. # do_execsql_test e_expr-37.1 { - SELECT CASE WHEN NULL THEN 'true' ELSE 'false' END; -} {false} + SELECT CASE WHEN NULL THEN 'true' ELSE 'false' END, iif(NULL,'true','false'); +} {false false} do_execsql_test e_expr-37.2 { - SELECT CASE WHEN 0.0 THEN 'true' ELSE 'false' END; -} {false} + SELECT CASE WHEN 0.0 THEN 'true' ELSE 'false' END, iif(0.0,'true','false'); +} {false false} do_execsql_test e_expr-37.3 { - SELECT CASE WHEN 0 THEN 'true' ELSE 'false' END; -} {false} + SELECT CASE WHEN 0 THEN 'true' ELSE 'false' END, iif(0,'true','false'); +} {false false} do_execsql_test e_expr-37.4 { - SELECT CASE WHEN 'engligh' THEN 'true' ELSE 'false' END; -} {false} + SELECT CASE WHEN 'engligh' THEN 'true' ELSE 'false' END, iif('engligh','true','false'); +} {false false} do_execsql_test e_expr-37.5 { - SELECT CASE WHEN '0' THEN 'true' ELSE 'false' END; -} {false} + SELECT CASE WHEN '0' THEN 'true' ELSE 'false' END, iif('0','true','false'); +} {false false} # EVIDENCE-OF: R-55532-10108 Values 1, 1.0, 0.1, -0.1 and '1english' are # considered to be true. # -do_execsql_test e_expr-37.6 { - SELECT CASE WHEN 1 THEN 'true' ELSE 'false' END; -} {true} +do_execsql_test e_expr-37.6a { + SELECT CASE WHEN 1 THEN 'true' ELSE 'false' END, iif(1,'true','false'); +} {true true} +do_execsql_test e_expr-37.6b { + SELECT CASE WHEN 1 THEN 'true' ELSE 'false' END, if(1,'true'); +} {true true} do_execsql_test e_expr-37.7 { - SELECT CASE WHEN 1.0 THEN 'true' ELSE 'false' END; -} {true} + SELECT CASE WHEN 1.0 THEN 'true' ELSE 'false' END, iif(1.0,'true','false'); +} {true true} do_execsql_test e_expr-37.8 { - SELECT CASE WHEN 0.1 THEN 'true' ELSE 'false' END; -} {true} + SELECT CASE WHEN 0.1 THEN 'true' ELSE 'false' END, iif(0.1,'true','false'); +} {true true} do_execsql_test e_expr-37.9 { - SELECT CASE WHEN -0.1 THEN 'true' ELSE 'false' END; -} {true} + SELECT CASE WHEN -0.1 THEN 'true' ELSE 'false' END, iif(-0.1,'true','false'); +} {true true} do_execsql_test e_expr-37.10 { - SELECT CASE WHEN '1english' THEN 'true' ELSE 'false' END; -} {true} + SELECT CASE WHEN '1english' THEN 'true' ELSE 'false' END, iif('1engl','true','false'); +} {true true} finish_test diff --git a/test/e_fkey.test b/test/e_fkey.test index 09756505c3..3662a39981 100644 --- a/test/e_fkey.test +++ b/test/e_fkey.test @@ -24,16 +24,33 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl -proc eqp {sql {db db}} { uplevel execsql [list "EXPLAIN QUERY PLAN $sql"] $db } +proc eqp {sql {db db}} { + uplevel [subst -nocommands { + set eqpres [list] + $db eval "$sql" { + lappend eqpres [set detail] + } + set eqpres + }] +} + +proc do_detail_test {tn sql res} { + set normalres [list {*}$res] + uplevel [subst -nocommands { + do_test $tn { + eqp { $sql } + } {$normalres} + }] +} ########################################################################### ### SECTION 2: Enabling Foreign Key Support ########################################################################### #------------------------------------------------------------------------- -# EVIDENCE-OF: R-33710-56344 In order to use foreign key constraints in +# EVIDENCE-OF: R-37672-59189 In order to use foreign key constraints in # SQLite, the library must be compiled with neither -# SQLITE_OMIT_FOREIGN_KEY or SQLITE_OMIT_TRIGGER defined. +# SQLITE_OMIT_FOREIGN_KEY nor SQLITE_OMIT_TRIGGER defined. # ifcapable trigger&&foreignkey { do_test e_fkey-1 { @@ -52,11 +69,11 @@ ifcapable trigger&&foreignkey { #------------------------------------------------------------------------- # Test the effects of defining OMIT_TRIGGER but not OMIT_FOREIGN_KEY. # -# EVIDENCE-OF: R-44697-61543 If SQLITE_OMIT_TRIGGER is defined but +# EVIDENCE-OF: R-10109-20452 If SQLITE_OMIT_TRIGGER is defined but # SQLITE_OMIT_FOREIGN_KEY is not, then SQLite behaves as it did prior to -# version 3.6.19 - foreign key definitions are parsed and may be queried -# using PRAGMA foreign_key_list, but foreign key constraints are not -# enforced. +# version 3.6.19 (2009-10-14) - foreign key definitions are parsed and +# may be queried using PRAGMA foreign_key_list, but foreign key +# constraints are not enforced. # # Specifically, test that "PRAGMA foreign_keys" is a no-op in this case. # When using the pragma to query the current setting, 0 rows are returned. @@ -729,15 +746,15 @@ do_test e_fkey-19.5 { # foreign key DML errors is usually "foreign key mismatch" but can also # be "no such table" if the parent table does not exist. # -# EVIDENCE-OF: R-60781-26576 Foreign key DML errors are may be reported -# if: The parent table does not exist, or The parent key columns named -# in the foreign key constraint do not exist, or The parent key columns -# named in the foreign key constraint are not the primary key of the -# parent table and are not subject to a unique constraint using -# collating sequence specified in the CREATE TABLE, or The child table -# references the primary key of the parent without specifying the -# primary key columns and the number of primary key columns in the -# parent do not match the number of child key columns. +# EVIDENCE-OF: R-35763-48267 Foreign key DML errors are reported if: The +# parent table does not exist, or The parent key columns named in the +# foreign key constraint do not exist, or The parent key columns named +# in the foreign key constraint are not the primary key of the parent +# table and are not subject to a unique constraint using collating +# sequence specified in the CREATE TABLE, or The child table references +# the primary key of the parent without specifying the primary key +# columns and the number of primary key columns in the parent do not +# match the number of child key columns. # do_test e_fkey-20.1 { execsql { @@ -970,20 +987,20 @@ do_test e_fkey-25.1 { ); } } {} -do_execsql_test e_fkey-25.2 { +do_detail_test e_fkey-25.2 { PRAGMA foreign_keys = OFF; EXPLAIN QUERY PLAN DELETE FROM artist WHERE 1; EXPLAIN QUERY PLAN SELECT rowid FROM track WHERE trackartist = ?; } { - 0 0 0 {SCAN TABLE artist} - 0 0 0 {SCAN TABLE track} + {SCAN artist} + {SCAN track} } -do_execsql_test e_fkey-25.3 { +do_detail_test e_fkey-25.3 { PRAGMA foreign_keys = ON; EXPLAIN QUERY PLAN DELETE FROM artist WHERE 1; } { - 0 0 0 {SCAN TABLE artist} - 0 0 0 {SCAN TABLE track} + {SCAN artist} + {SCAN track} } do_test e_fkey-25.4 { execsql { @@ -1097,21 +1114,20 @@ do_test e_fkey-27.1 { do_test e_fkey-27.2 { eqp { INSERT INTO artist VALUES(?, ?) } } {} -do_execsql_test e_fkey-27.3 { +do_detail_test e_fkey-27.3 { EXPLAIN QUERY PLAN UPDATE artist SET artistid = ?, artistname = ? } { - 0 0 0 {SCAN TABLE artist} - 0 0 0 {SEARCH TABLE track USING COVERING INDEX trackindex (trackartist=?)} - 0 0 0 {SEARCH TABLE track USING COVERING INDEX trackindex (trackartist=?)} + {SCAN artist} + {SEARCH track USING COVERING INDEX trackindex (trackartist=?)} + {SEARCH track USING COVERING INDEX trackindex (trackartist=?)} } -do_execsql_test e_fkey-27.4 { +do_detail_test e_fkey-27.4 { EXPLAIN QUERY PLAN DELETE FROM artist } { - 0 0 0 {SCAN TABLE artist} - 0 0 0 {SEARCH TABLE track USING COVERING INDEX trackindex (trackartist=?)} + {SCAN artist} + {SEARCH track USING COVERING INDEX trackindex (trackartist=?)} } - ########################################################################### ### SECTION 4.1: Composite Foreign Key Constraints ########################################################################### @@ -2039,9 +2055,9 @@ do_test e_fkey-44.5 { #------------------------------------------------------------------------- # Test SET DEFAULT actions. # -# EVIDENCE-OF: R-43054-54832 The "SET DEFAULT" actions are similar to +# EVIDENCE-OF: R-55814-22637 The "SET DEFAULT" actions are similar to # "SET NULL", except that each of the child key columns is set to -# contain the columns default value instead of NULL. +# contain the column's default value instead of NULL. # drop_all_tables do_test e_fkey-45.1 { @@ -2491,15 +2507,17 @@ proc test_efkey_6 {tn zAlter isError} { drop_all_tables do_test e_fkey-56.$tn.1 " - execsql { CREATE TABLE tbl(a, b) } + execsql { CREATE TABLE tbl(a, b); INSERT INTO tbl VALUES(1, 2); } [list catchsql $zAlter] " [lindex {{0 {}} {1 {Cannot add a REFERENCES column with non-NULL default value}}} $isError] } -test_efkey_6 1 "ALTER TABLE tbl ADD COLUMN c REFERENCES xx" 0 -test_efkey_6 2 "ALTER TABLE tbl ADD COLUMN c DEFAULT NULL REFERENCES xx" 0 -test_efkey_6 3 "ALTER TABLE tbl ADD COLUMN c DEFAULT 0 REFERENCES xx" 1 +ifcapable altertable { + test_efkey_6 1 "ALTER TABLE tbl ADD COLUMN c REFERENCES xx" 0 + test_efkey_6 2 "ALTER TABLE tbl ADD COLUMN c DEFAULT NULL REFERENCES xx" 0 + test_efkey_6 3 "ALTER TABLE tbl ADD COLUMN c DEFAULT 0 REFERENCES xx" 1 +} #------------------------------------------------------------------------- # Test that ALTER TABLE adjusts REFERENCES clauses when the parent table @@ -2512,10 +2530,11 @@ test_efkey_6 3 "ALTER TABLE tbl ADD COLUMN c DEFAULT 0 REFERENCES xx" 1 # # Test that these adjustments are visible in the sqlite_master table. # -# EVIDENCE-OF: R-63827-54774 The text of the child CREATE TABLE -# statement or statements stored in the sqlite_master table are modified +# EVIDENCE-OF: R-43040-62530 The text of the child CREATE TABLE +# statement or statements stored in the sqlite_schema table are modified # to reflect the new parent table name. # +ifcapable altertable { do_test e_fkey-56.1 { drop_all_tables execsql { @@ -2553,6 +2572,7 @@ do_test e_fkey-56.4 { {CREATE TABLE c2(e, f, FOREIGN KEY(f) REFERENCES "p" ON UPDATE CASCADE)} \ {CREATE TABLE c3(e, 'f col 2', FOREIGN KEY('f col 2') REFERENCES "p" ON UPDATE CASCADE)} \ ] +} #------------------------------------------------------------------------- # Check that a DROP TABLE does an implicit DELETE FROM. Which does not @@ -2753,9 +2773,10 @@ do_test e_fkey-60.6 { # EVIDENCE-OF: R-54142-41346 The properties of the DROP TABLE and ALTER # TABLE commands described above only apply if foreign keys are enabled. # +ifcapable altertable { do_test e_fkey-61.1.1 { drop_all_tables - execsql { CREATE TABLE t1(a, b) } + execsql { CREATE TABLE t1(a, b) ; INSERT INTO t1 VALUES(1, 2) } catchsql { ALTER TABLE t1 ADD COLUMN c DEFAULT 'xxx' REFERENCES t2 } } {1 {Cannot add a REFERENCES column with non-NULL default value}} do_test e_fkey-61.1.2 { @@ -2781,12 +2802,14 @@ do_test e_fkey-61.2.1 { do_test e_fkey-61.2.2 { execsql { PRAGMA foreign_keys = OFF; + PRAGMA legacy_alter_table = ON; ALTER TABLE p RENAME TO parent; SELECT sql FROM sqlite_master WHERE name = 'c'; } } {{CREATE TABLE c(b REFERENCES p(a))}} do_test e_fkey-61.2.3 { execsql { PRAGMA foreign_keys = ON } + execsql { PRAGMA legacy_alter_table = OFF } } {} do_test e_fkey-61.3.1 { @@ -2812,6 +2835,7 @@ do_test e_fkey-61.3.2 { do_test e_fkey-61.3.3 { execsql { PRAGMA foreign_keys = ON } } {} +} ########################################################################### ### SECTION 6: Limits and Unsupported Features diff --git a/test/e_fts3.test b/test/e_fts3.test index 2a580ca313..c06feab2d1 100644 --- a/test/e_fts3.test +++ b/test/e_fts3.test @@ -177,7 +177,7 @@ read_test 1.2.1.9 { SELECT docid, * FROM pages } {} do_error_test fts3-1.2.1.10 { INSERT INTO pages(rowid, docid, title, body) VALUES(1, 2, 'A title', 'A document body'); -} {SQL logic error or missing database} +} {SQL logic error} # Test the optimize() function example: ddl_test 1.2.2.1 { CREATE VIRTUAL TABLE docs USING fts3 } @@ -677,6 +677,7 @@ write_test 10.1.2 ta_content { INSERT INTO ta VALUES('During a summer vacation in 1790') } write_test 10.1.3 ta_content { INSERT INTO ta VALUES('Wordsworth went on a walking tour') } +sqlite3_db_config db DEFENSIVE 0 write_test 10.1.4 ta_content { DELETE FROM ta_content WHERE rowid = 2 } read_test 10.1.5 { SELECT * FROM ta WHERE ta MATCH 'summer' diff --git a/test/e_insert.test b/test/e_insert.test index 32d75cbbd7..7561e58ccc 100644 --- a/test/e_insert.test +++ b/test/e_insert.test @@ -348,9 +348,10 @@ do_insert_tests e_insert-3.2 { 6.2 "SELECT * FROM a1" {{} {} {} {}} } -# EVIDENCE-OF: R-03235-45250 The "REPLACE" and "INSERT OR action" forms -# specify an alternative constraint conflict resolution algorithm to use -# during this one INSERT command. +# EVIDENCE-OF: R-00267-47727 The initial "INSERT" keyword can be +# replaced by "REPLACE" or "INSERT OR action" to specify an alternative +# constraint conflict resolution algorithm to use during that one INSERT +# command. # # EVIDENCE-OF: R-23110-47146 the parser allows the use of the single # keyword REPLACE as an alias for "INSERT OR REPLACE". diff --git a/test/e_reindex.test b/test/e_reindex.test index c6a9e0352f..50d2e7d516 100644 --- a/test/e_reindex.test +++ b/test/e_reindex.test @@ -44,6 +44,7 @@ do_reindex_tests e_reindex-0.1 { # Test this by corrupting some database indexes, running REINDEX, and # observing that the corruption is gone. # +sqlite3_db_config db DEFENSIVE 0 do_execsql_test e_reindex-1.1 { INSERT INTO t1 VALUES(1, 2); INSERT INTO t1 VALUES(3, 4); @@ -57,6 +58,7 @@ do_execsql_test e_reindex-1.1 { db close sqlite3 db test.db +sqlite3_db_config db DEFENSIVE 0 do_execsql_test e_reindex-1.2 { DELETE FROM t1 WHERE a = 3; INSERT INTO t1 VALUES(7, 8); @@ -71,12 +73,12 @@ sqlite3 db test.db do_execsql_test e_reindex-1.3 { PRAGMA integrity_check; } [list \ + {wrong # of entries in index i2} \ + {wrong # of entries in index i1} \ {row 3 missing from index i2} \ {row 3 missing from index i1} \ {row 4 missing from index i2} \ - {row 4 missing from index i1} \ - {wrong # of entries in index i2} \ - {wrong # of entries in index i1} + {row 4 missing from index i1} ] do_execsql_test e_reindex-1.4 { diff --git a/test/e_select.test b/test/e_select.test index 89d61b53bc..e2e969dcf9 100644 --- a/test/e_select.test +++ b/test/e_select.test @@ -96,7 +96,7 @@ do_join_test e_select-0.1.3 { } {9} do_catchsql_test e_select-0.1.4 { SELECT count(*) FROM t1, t2 ON (t1.a=t2.a) USING (a) -} {1 {cannot have both ON and USING clauses in the same join}} +} {1 {near "USING": syntax error}} do_catchsql_test e_select-0.1.5 { SELECT count(*) FROM t1, t2 USING (a) ON (t1.a=t2.a) } {1 {near "ON": syntax error}} @@ -167,7 +167,7 @@ do_select_tests e_select-0.2 { 0102.1 "SELECT count(*), max(a) FROM t1 GROUP BY b HAVING count(*)=1" { 1 a 1 c 1 b } - 0102.2 "SELECT count(*), max(a) FROM t1 GROUP BY b HAVING count(*)=2" { } + 0102.2 "SELECT count(*), max(a) FROM t1 GROUP BY b HAVING count(*)=2" {} 1101.1 "SELECT DISTINCT count(*), max(a) FROM t1 GROUP BY b" {1 a 1 c 1 b} 1102.1 "SELECT DISTINCT count(*), max(a) FROM t1 @@ -175,8 +175,7 @@ do_select_tests e_select-0.2 { 1 a 1 c 1 b } 1102.2 "SELECT DISTINCT count(*), max(a) FROM t1 - GROUP BY b HAVING count(*)=2" { - } + GROUP BY b HAVING count(*)=2" {} 2101.1 "SELECT ALL count(*), max(a) FROM t1 GROUP BY b" {1 a 1 c 1 b} 2102.1 "SELECT ALL count(*), max(a) FROM t1 @@ -184,8 +183,7 @@ do_select_tests e_select-0.2 { 1 a 1 c 1 b } 2102.2 "SELECT ALL count(*), max(a) FROM t1 - GROUP BY b HAVING count(*)=2" { - } + GROUP BY b HAVING count(*)=2" {} 0011.1 "SELECT 1, 2, 3 WHERE 1 GROUP BY 2" {1 2 3} 0012.1 "SELECT 1, 2, 3 WHERE 0 GROUP BY 2 HAVING count(*)=1" {} @@ -204,7 +202,7 @@ do_select_tests e_select-0.2 { 0112.1 "SELECT count(*), max(a) FROM t1 WHERE a='c' GROUP BY b HAVING count(*)=1" {1 c} 0112.2 "SELECT count(*), max(a) FROM t1 - WHERE 0 GROUP BY b HAVING count(*)=2" { } + WHERE 0 GROUP BY b HAVING count(*)=2" {} 1111.1 "SELECT DISTINCT count(*), max(a) FROM t1 WHERE a<'c' GROUP BY b" {1 a 1 b} 1112.1 "SELECT DISTINCT count(*), max(a) FROM t1 WHERE a>'a' @@ -212,8 +210,7 @@ do_select_tests e_select-0.2 { 1 c 1 b } 1112.2 "SELECT DISTINCT count(*), max(a) FROM t1 WHERE 0 - GROUP BY b HAVING count(*)=2" { - } + GROUP BY b HAVING count(*)=2" {} 2111.1 "SELECT ALL count(*), max(a) FROM t1 WHERE b>'one' GROUP BY b" {1 c 1 b} @@ -222,7 +219,7 @@ do_select_tests e_select-0.2 { 1 a 1 c } 2112.2 "SELECT ALL count(*), max(a) FROM t1 - WHERE 0 GROUP BY b HAVING count(*)=2" { } + WHERE 0 GROUP BY b HAVING count(*)=2" {} } @@ -621,11 +618,12 @@ foreach {tn select res} { } { do_join_test e_select-1.7.$tn $select $res } -# EVIDENCE-OF: R-42531-52874 If the join-operator is a "LEFT JOIN" or + +# EVIDENCE-OF: R-24610-05866 If the join-operator is a "LEFT JOIN" or # "LEFT OUTER JOIN", then after the ON or USING filtering clauses have # been applied, an extra row is added to the output for each row in the -# original left-hand input dataset that corresponds to no rows at all in -# the composite dataset (if any). +# original left-hand input dataset that does not match any row in the +# right-hand dataset. # do_execsql_test e_select-1.8.0 { CREATE TABLE t7(a, b, c); @@ -748,7 +746,7 @@ do_execsql_test e_select-3.2.1a { SELECT k FROM x1 LEFT JOIN x2 USING(k) } {1 2 3 4 5 6} do_execsql_test e_select-3.2.1b { - SELECT k FROM x1 LEFT JOIN x2 USING(k) WHERE x2.k + SELECT k FROM x1 LEFT JOIN x2 USING(k) WHERE x2.k ORDER BY +k } {1 3 5} do_execsql_test e_select-3.2.2 { SELECT k FROM x1 LEFT JOIN x2 USING(k) WHERE x2.k IS NULL @@ -801,7 +799,7 @@ do_select_tests e_select-4.1 { 4 "SELECT z2.* FROM z1,z2 LIMIT 1" {{} 21} 5 "SELECT z2.*, z1.* FROM z1,z2 LIMIT 1" {{} 21 51.65 -59.58 belfries} - 6 "SELECT count(*), * FROM z1" {6 63 born -26} + 6 "SELECT count(*), * FROM z1" {6 51.65 -59.58 belfries} 7 "SELECT max(a), * FROM z1" {63 63 born -26} 8 "SELECT *, min(a) FROM z1" {-5 {} 75 -5} @@ -939,13 +937,13 @@ do_execsql_test e_select-4.6.0 { INSERT INTO a2 VALUES(10, 4); } {} do_select_tests e_select-4.6 { - 1 "SELECT one, two, count(*) FROM a1" {4 10 4} - 2 "SELECT one, two, count(*) FROM a1 WHERE one<3" {2 3 2} + 1 "SELECT one, two, count(*) FROM a1" {1 1 4} + 2 "SELECT one, two, count(*) FROM a1 WHERE one<3" {1 1 2} 3 "SELECT one, two, count(*) FROM a1 WHERE one>3" {4 10 1} - 4 "SELECT *, count(*) FROM a1 JOIN a2" {4 10 10 4 16} - 5 "SELECT *, sum(three) FROM a1 NATURAL JOIN a2" {3 6 2 3} - 6 "SELECT *, sum(three) FROM a1 NATURAL JOIN a2" {3 6 2 3} - 7 "SELECT group_concat(three, ''), a1.* FROM a1 NATURAL JOIN a2" {12 3 6} + 4 "SELECT *, count(*) FROM a1 JOIN a2" {1 1 1 1 16} + 5 "SELECT *, sum(three) FROM a1 NATURAL JOIN a2" {1 1 1 3} + 6 "SELECT *, sum(three) FROM a1 NATURAL JOIN a2" {1 1 1 3} + 7 "SELECT string_agg(three, ''), a1.* FROM a1 NATURAL JOIN a2" {12 1 1} } # EVIDENCE-OF: R-04486-07266 Or, if the dataset contains zero rows, then @@ -1007,12 +1005,13 @@ do_execsql_test e_select-4.9.0 { INSERT INTO b3 VALUES('dEF', 'dEF'); } {} -# EVIDENCE-OF: R-07284-35990 If the SELECT statement is an aggregate +# EVIDENCE-OF: R-40855-36147 If the SELECT statement is an aggregate # query with a GROUP BY clause, then each of the expressions specified # as part of the GROUP BY clause is evaluated for each row of the -# dataset. Each row is then assigned to a "group" based on the results; -# rows for which the results of evaluating the GROUP BY expressions are -# the same get assigned to the same group. +# dataset according to the processing rules stated below for ORDER BY +# expressions. Each row is then assigned to a "group" based on the +# results; rows for which the results of evaluating the GROUP BY +# expressions are the same get assigned to the same group. # # These tests also show that the following is not untrue: # @@ -1128,7 +1127,7 @@ do_select_tests e_select-4.13 { 2.1 "SELECT up FROM c1 GROUP BY up HAVING down>10" {y} 2.2 "SELECT up FROM c1 GROUP BY up HAVING up='y'" {y} - 2.3 "SELECT i, j FROM c2 GROUP BY i>4 HAVING i>6" {9 36} + 2.3 "SELECT i, j FROM c2 GROUP BY i>4 HAVING j>6" {5 10} } # EVIDENCE-OF: R-23927-54081 Each expression in the result-set is then @@ -1154,12 +1153,12 @@ do_select_tests e_select-4.15 { # for the same row. # do_select_tests e_select-4.15 { - 1 "SELECT i, j FROM c2 GROUP BY i%2" {8 28 9 36} - 2 "SELECT i, j FROM c2 GROUP BY i%2 HAVING j<30" {8 28} - 3 "SELECT i, j FROM c2 GROUP BY i%2 HAVING j>30" {9 36} - 4 "SELECT i, j FROM c2 GROUP BY i%2 HAVING j>30" {9 36} + 1 "SELECT i, j FROM c2 GROUP BY i%2" {2 1 1 0} + 2 "SELECT i, j FROM c2 GROUP BY i%2 HAVING j<30" {2 1 1 0} + 3 "SELECT i, j FROM c2 GROUP BY i%2 HAVING j>30" {} + 4 "SELECT i, j FROM c2 GROUP BY i%2 HAVING j>30" {} 5 "SELECT count(*), i, k FROM c2 NATURAL JOIN c3 GROUP BY substr(k, 1, 1)" - {2 5 boron 2 2 helium 1 3 lithium} + {2 4 beryllium 2 1 hydrogen 1 3 lithium} } # EVIDENCE-OF: R-19334-12811 Each group of input dataset rows @@ -1252,8 +1251,8 @@ do_select_tests e_select-5.5 { 1 "SELECT DISTINCT d FROM h3" {{} 2 2,3 2,4 3} } -# EVIDENCE-OF: R-58359-52112 The normal rules for selecting a collation -# sequence to compare text values with apply. +# EVIDENCE-OF: R-47709-27231 The usual rules apply for selecting a +# collation sequence to compare text values. # do_select_tests e_select-5.6 { 1 "SELECT DISTINCT b FROM h1" {one I i four IV iv} diff --git a/test/e_totalchanges.test b/test/e_totalchanges.test index ee163c914f..bb5cfba8a5 100644 --- a/test/e_totalchanges.test +++ b/test/e_totalchanges.test @@ -32,10 +32,9 @@ do_execsql_test 1.0 { #-------------------------------------------------------------------------- -# EVIDENCE-OF: R-65438-26258 This function returns the total number of -# rows inserted, modified or deleted by all INSERT, UPDATE or DELETE -# statements completed since the database connection was opened, -# including those executed as part of trigger programs. +# EVIDENCE-OF: R-38914-26427 The total_changes() function returns the +# number of row changes caused by INSERT, UPDATE or DELETE statements +# since the current database connection was opened. # # 1.1.*: different types of I/U/D statements, # 1.2.*: trigger programs. @@ -95,24 +94,26 @@ do_tc_test 1.2.2 { #-------------------------------------------------------------------------- # EVIDENCE-OF: R-61766-15253 Executing any other type of SQL statement # does not affect the value returned by sqlite3_total_changes(). -do_tc_test 2.1 { - INSERT INTO t1 VALUES(1, 2), (3, 4); - INSERT INTO t2 VALUES(1, 2), (3, 4); -} {15} -do_tc_test 2.2 { - SELECT count(*) FROM t1; -} {2 15} -do_tc_test 2.3 { - CREATE TABLE t4(a, b); - ALTER TABLE t4 ADD COLUMN c; - CREATE INDEX i4 ON t4(c); - ALTER TABLE t4 RENAME TO t5; - ANALYZE; - BEGIN; - DROP TABLE t2; - ROLLBACK; - VACUUM; -} {15} +ifcapable altertable { + do_tc_test 2.1 { + INSERT INTO t1 VALUES(1, 2), (3, 4); + INSERT INTO t2 VALUES(1, 2), (3, 4); + } {15} + do_tc_test 2.2 { + SELECT count(*) FROM t1; + } {2 15} + do_tc_test 2.3 { + CREATE TABLE t4(a, b); + ALTER TABLE t4 ADD COLUMN c; + CREATE INDEX i4 ON t4(c); + ALTER TABLE t4 RENAME TO t5; + ANALYZE; + BEGIN; + DROP TABLE t2; + ROLLBACK; + VACUUM; + } {15} +} #-------------------------------------------------------------------------- diff --git a/test/e_uri.test b/test/e_uri.test index 7a7f2559ec..b1fb47989c 100644 --- a/test/e_uri.test +++ b/test/e_uri.test @@ -13,7 +13,7 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl set testprefix e_uri - +do_not_use_codec db close proc parse_uri {uri} { @@ -25,6 +25,7 @@ proc parse_uri {uri} { set ::uri_open [list] set DB [sqlite3_open_v2 $uri { SQLITE_OPEN_READWRITE SQLITE_OPEN_CREATE SQLITE_OPEN_WAL + SQLITE_OPEN_EXRESCODE } tvfs] set fileName [sqlite3_db_filename $DB main] sqlite3_close $DB @@ -50,8 +51,8 @@ proc open_uri_error {uri} { # and the filename argument begins with "file:", then the filename is # interpreted as a URI. # -# EVIDENCE-OF: R-24124-56960 URI filename interpretation is enabled if -# the SQLITE_OPEN_URI flag is set in the fourth argument to +# EVIDENCE-OF: R-27632-24205 URI filename interpretation is enabled if +# the SQLITE_OPEN_URI flag is set in the third argument to # sqlite3_open_v2(), or if it has been enabled globally using the # SQLITE_CONFIG_URI option with the sqlite3_config() method or by the # SQLITE_USE_URI compile-time option. @@ -357,6 +358,7 @@ foreach {tn uri error} " do_test 10.$tn { open_uri_error $uri } $error } +ifcapable shared_cache { # EVIDENCE-OF: R-23027-03515 Setting it to "shared" is equivalent to # setting the SQLITE_OPEN_SHAREDCACHE bit in the flags argument passed # to sqlite3_open_v2(). @@ -429,6 +431,7 @@ foreach {tn uri flags shared_default isshared} { db close } sqlite3_enable_shared_cache $orig +} ;# End ifcapable shared_chache # EVIDENCE-OF: R-63472-46769 Specifying an unknown parameter in the # query component of a URI is not an error. diff --git a/test/e_vacuum.test b/test/e_vacuum.test index 99b31aaca4..63bf7c125f 100644 --- a/test/e_vacuum.test +++ b/test/e_vacuum.test @@ -159,37 +159,39 @@ do_test e_vacuum-1.3.1.2 { execsql { PRAGMA page_size ; PRAGMA auto_vacuum } } {1024 1} -# EVIDENCE-OF: R-08570-19916 However, when not in write-ahead log mode, -# the page_size and/or auto_vacuum properties of an existing database -# may be changed by using the page_size and/or pragma auto_vacuum -# pragmas and then immediately VACUUMing the database. -# -do_test e_vacuum-1.3.2.1 { - execsql { PRAGMA journal_mode = delete } - execsql { PRAGMA page_size = 2048 } - execsql { PRAGMA auto_vacuum = NONE } - execsql VACUUM - execsql { PRAGMA page_size ; PRAGMA auto_vacuum } -} {2048 0} - -# EVIDENCE-OF: R-48521-51450 When in write-ahead log mode, only the -# auto_vacuum support property can be changed using VACUUM. -# -ifcapable wal { -do_test e_vacuum-1.3.3.1 { - execsql { PRAGMA journal_mode = wal } - execsql { PRAGMA page_size ; PRAGMA auto_vacuum } -} {2048 0} -do_test e_vacuum-1.3.3.2 { - execsql { PRAGMA page_size = 1024 } - execsql { PRAGMA auto_vacuum = FULL } - execsql VACUUM - execsql { PRAGMA page_size ; PRAGMA auto_vacuum } -} {2048 1} +if {![nonzero_reserved_bytes]} { + # EVIDENCE-OF: R-08570-19916 However, when not in write-ahead log mode, + # the page_size and/or auto_vacuum properties of an existing database + # may be changed by using the page_size and/or pragma auto_vacuum + # pragmas and then immediately VACUUMing the database. + # + do_test e_vacuum-1.3.2.1 { + execsql { PRAGMA journal_mode = delete } + execsql { PRAGMA page_size = 2048 } + execsql { PRAGMA auto_vacuum = NONE } + execsql VACUUM + execsql { PRAGMA page_size ; PRAGMA auto_vacuum } + } {2048 0} + + # EVIDENCE-OF: R-48521-51450 When in write-ahead log mode, only the + # auto_vacuum support property can be changed using VACUUM. + # + if {[wal_is_capable]} { + do_test e_vacuum-1.3.3.1 { + execsql { PRAGMA journal_mode = wal } + execsql { PRAGMA page_size ; PRAGMA auto_vacuum } + } {2048 0} + do_test e_vacuum-1.3.3.2 { + execsql { PRAGMA page_size = 1024 } + execsql { PRAGMA auto_vacuum = FULL } + execsql VACUUM + execsql { PRAGMA page_size ; PRAGMA auto_vacuum } + } {2048 1} + } } - -# EVIDENCE-OF: R-38001-03952 VACUUM only works on the main database. It -# is not possible to VACUUM an attached database file. + +# EVIDENCE-OF: R-40347-36128 By default, VACUUM operates on the main +# database. forcedelete test.db2 create_db { PRAGMA auto_vacuum = NONE } do_execsql_test e_vacuum-2.1.1 { @@ -200,14 +202,15 @@ do_execsql_test e_vacuum-2.1.1 { } {} set original_size [file size test.db2] -# Try everything we can think of to get the aux database vacuumed: +# Vacuuming the main database does not affect aux do_execsql_test e_vacuum-2.1.3 { VACUUM } {} -do_execsql_test e_vacuum-2.1.4 { VACUUM aux } {} -do_execsql_test e_vacuum-2.1.5 { VACUUM 'test.db2' } {} - -# Despite our efforts, space in the aux database has not been reclaimed: do_test e_vacuum-2.1.6 { expr {[file size test.db2]==$::original_size} } 1 +# EVIDENCE-OF: R-36598-60500 Attached databases can be vacuumed by +# appending the appropriate schema-name to the VACUUM statement. +do_execsql_test e_vacuum-2.1.7 { VACUUM aux; } {} +do_test e_vacuum-2.1.8 { expr {[file size test.db2]<$::original_size} } 1 + # EVIDENCE-OF: R-17495-17419 The VACUUM command may change the ROWIDs of # entries in any tables that do not have an explicit INTEGER PRIMARY # KEY. @@ -216,6 +219,8 @@ do_test e_vacuum-2.1.6 { expr {[file size test.db2]==$::original_size} } 1 # a database is VACUUMed. Tests e_vacuum-3.1.3 - 3.1.4 show that adding # an INTEGER PRIMARY KEY column to a table stops this from happening. # +# Update 2019-01-07: Rowids are now preserved by VACUUM. +# do_execsql_test e_vacuum-3.1.1 { CREATE TABLE t4(x); INSERT INTO t4(x) VALUES('x'); @@ -229,6 +234,7 @@ do_execsql_test e_vacuum-3.1.2 { SELECT rowid, x FROM t4; } {1 x 2 z} +# Rowids are preserved if an INTEGER PRIMARY KEY is used do_execsql_test e_vacuum-3.1.3 { CREATE TABLE t5(x, y INTEGER PRIMARY KEY); INSERT INTO t5(x) VALUES('x'); @@ -242,9 +248,47 @@ do_execsql_test e_vacuum-3.1.4 { SELECT rowid, x FROM t5; } {1 x 3 z} -# EVIDENCE-OF: R-49563-33883 A VACUUM will fail if there is an open -# transaction, or if there are one or more active SQL statements when it -# is run. +# Rowid is preserved for VACUUM INTO +do_execsql_test e_vacuum-3.1.5 { + DROP TABLE t5; + CREATE TABLE t5(x); + INSERT INTO t5(x) VALUES('x'); + INSERT INTO t5(x) VALUES('y'); + INSERT INTO t5(x) VALUES('z'); + DELETE FROM t5 WHERE x = 'y'; + SELECT rowid, x FROM t5; +} {1 x 3 z} +forcedelete test2.db +do_execsql_test e_vacuum-3.1.6 { + VACUUM INTO 'test2.db'; + ATTACH 'test2.db' AS aux1; + SELECT rowid, x FROM aux1.t5; + DETACH aux1; +} {1 x 3 z} + +# Rowids are not renumbered if the table being vacuumed +# has indexes. +do_execsql_test e_vacuum-3.1.7 { + DROP TABLE t5; + CREATE TABLE t5(x,y,z); + INSERT INTO t5(x) VALUES('x'); + INSERT INTO t5(x) VALUES('y'); + INSERT INTO t5(x) VALUES('z'); + UPDATE t5 SET y=x, z=random(); + DELETE FROM t5 WHERE x = 'y'; + CREATE INDEX t5x ON t5(x); + CREATE UNIQUE INDEX t5y ON t5(y); + CREATE INDEX t5zxy ON t5(z,x,y); + SELECT rowid, x FROM t5; +} {1 x 3 z} +do_execsql_test e_vacuum-3.1.8 { + VACUUM; + SELECT rowid, x FROM t5; +} {1 x 3 z} + +# EVIDENCE-OF: R-12218-18073 A VACUUM will fail if there is an open +# transaction on the database connection that is attempting to run the +# VACUUM. # do_execsql_test e_vacuum-3.2.1.1 { BEGIN } {} do_catchsql_test e_vacuum-3.2.1.2 { @@ -269,9 +313,9 @@ do_test e_vacuum-3.2.2.1 { } {1 {cannot VACUUM - SQL statements in progress}} -# EVIDENCE-OF: R-38735-12540 As of SQLite version 3.1, an alternative to -# using the VACUUM command to reclaim space after data has been deleted -# is auto-vacuum mode, enabled using the auto_vacuum pragma. +# EVIDENCE-OF: R-55138-13241 An alternative to using the VACUUM command +# to reclaim space after data has been deleted is auto-vacuum mode, +# enabled using the auto_vacuum pragma. # do_test e_vacuum-3.3.1 { create_db { PRAGMA auto_vacuum = FULL } diff --git a/test/e_wal.test b/test/e_wal.test index 77ac83a0ae..c9c5e9643f 100644 --- a/test/e_wal.test +++ b/test/e_wal.test @@ -15,6 +15,7 @@ source $testdir/tester.tcl set testprefix e_wal db close +forcedelete test.db-shm testvfs oldvfs -iversion 1 diff --git a/test/e_walauto.test b/test/e_walauto.test index 093b13f940..7665b1bf73 100644 --- a/test/e_walauto.test +++ b/test/e_walauto.test @@ -24,6 +24,11 @@ if {$tcl_platform(os) == "OpenBSD"} { return } +# This module uses hard-coded offsets which do not work if the reserved_bytes +# value is nonzero. +if {[nonzero_reserved_bytes]} {finish_test; return;} + + proc read_nbackfill {} { seek $::shmfd 96 binary scan [read $::shmfd 4] n nBackfill diff --git a/test/e_walhook.test b/test/e_walhook.test index c8c8819493..b4a3156367 100644 --- a/test/e_walhook.test +++ b/test/e_walhook.test @@ -121,7 +121,7 @@ do_test 3.1.2 { proc my_wal_hook {args} { return 1 ;# SQLITE_ERROR } do_catchsql_test 4.1 { INSERT INTO t1 VALUES(7) -} {1 {SQL logic error or missing database}} +} {1 {SQL logic error}} proc my_wal_hook {args} { return 5 ;# SQLITE_BUSY } do_catchsql_test 4.2 { @@ -161,10 +161,10 @@ do_test 5.2 { -# EVIDENCE-OF: R-42842-27162 Note that the sqlite3_wal_autocheckpoint() +# EVIDENCE-OF: R-57445-43425 Note that the sqlite3_wal_autocheckpoint() # interface and the wal_autocheckpoint pragma both invoke -# sqlite3_wal_hook() and will those overwrite any prior -# sqlite3_wal_hook() settings. +# sqlite3_wal_hook() and will overwrite any prior sqlite3_wal_hook() +# settings. # set ::old_wal_hook 0 proc my_old_wal_hook {args} { incr ::old_wal_hook ; return 0 } diff --git a/test/emptytable.test b/test/emptytable.test new file mode 100644 index 0000000000..79cd16e913 --- /dev/null +++ b/test/emptytable.test @@ -0,0 +1,50 @@ +# 2017-02-15 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# Test cases to show that a join involving an empty table is very fast. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +# Build some test data +# +do_execsql_test emptytable-100 { + CREATE TABLE t1(a); + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<100) + INSERT INTO t1(a) SELECT x FROM c; + CREATE TABLE empty(x); + SELECT count(*) FROM t1; +} {100} + +# Interrupt queries after 1M cycles to prevent burning excess CPU +proc stopDb {args} { + db interrupt +} +db progress 1000000 {stopDb} + +# Prior to the query planner optimization on 2017-02-15, this query would +# take a ridiculous amount of time. If that optimization stops working, +# the result here will be in interrupt for running too long. +# +do_catchsql_test emptytable-110 { + SELECT count(*) FROM t1, t1, t1, t1, t1, t1, empty; +} {0 0} + +do_catchsql_test emptytable-120 { + SELECT count(*) FROM t1, t1 LEFT JOIN empty; +} {0 10000} +do_catchsql_test emptytable-121 { + SELECT count(*) FROM t1, t1 LEFT JOIN t1, empty; +} {0 0} + + +finish_test diff --git a/test/enc.test b/test/enc.test index 5c24bbb7f6..0aec224e2d 100644 --- a/test/enc.test +++ b/test/enc.test @@ -169,4 +169,113 @@ do_test enc-11.2 { } } {2} +#------------------------------------------------------------------------- +reset_db +forcedelete test.db2 +forcedelete test.db3 + +do_execsql_test enc-12.0 { + PRAGMA encoding = 'utf-8'; + CREATE TABLE t1(a, b, c); + INSERT INTO t1 VALUES('a', 'b', 'c'); + ATTACH 'test.db3' AS aux; + CREATE TABLE aux.t3(x, y, z); + INSERT INTO t3 VALUES('xxx', 'yyy', 'zzz'); + PRAGMA encoding; +} {UTF-8} + +do_test enc-12.1 { + sqlite3 db2 test.db2 + db2 eval { + PRAGMA encoding = 'UTF-16le'; + CREATE TABLE t2(d, e, f); + INSERT INTO t2 VALUES('d', 'e', 'f'); + PRAGMA encoding; + } +} {UTF-16le} + +do_test enc-12.2 { + db2 backup test.db + db2 close +} {} + +do_catchsql_test enc-12.3 { + SELECT * FROM t2; +} {1 {attached databases must use the same text encoding as main database}} + +db close +sqlite3 db test.db3 +do_execsql_test enc-12.4 { + SELECT * FROM t3; + PRAGMA encoding = 'UTF-16le'; + SELECT * FROM t3; +} {xxx yyy zzz xxx yyy zzz} + +db close +sqlite3 db test.db3 +breakpoint +do_execsql_test enc-12.5 { + PRAGMA encoding = 'UTF-16le'; + PRAGMA encoding; +} {UTF-8} + +reset_db +do_execsql_test enc-12.6 { + PRAGMA encoding = 'UTF-8'; + CREATE TEMP TABLE t1(a, b, c); + INSERT INTO t1 VALUES('xxx', 'yyy', 'zzz'); +} +do_test enc-12.7 { + sqlite3 db2 test.db2 + db2 backup test.db + db2 close + db eval { + SELECT * FROM t1; + } +} {xxx yyy zzz} +do_catchsql_test enc-12.8 { + SELECT * FROM t2; + SELECT * FROM t1; +} {1 {attached databases must use the same text encoding as main database}} + +db close +sqlite3 db test.db +do_execsql_test enc-12.9 { + CREATE TEMP TABLE t1(a, b, c); + INSERT INTO t1 VALUES('xxx', 'yyy', 'zzz'); +} +do_execsql_test enc-12.10 { + SELECT * FROM t2; + SELECT * FROM t1; +} {d e f xxx yyy zzz} + + +# 2024-11-04 +# https://sqlite.org/forum/forumpost/bc75a4d20b756044 +# +# Ensure the database encoding is detected in a timely manner, +# and before we get too far along in generating and running +# bytecode. +# +# See also check-in https://sqlite.org/src/info/a02da71f3a. +# +db close +forcedelete utf16.db +sqlite3 db utf16.db +db eval {PRAGMA encoding=UTF16; CREATE TABLE t2(y); INSERT INTO t2 VALUES('utf16');} +db close +sqlite3 db utf16.db +do_test enc-13.1 { + db eval {PRAGMA function_list} {db eval {SELECT * FROM sqlite_schema}} +} {} +ifcapable rtree { + db eval {CREATE VIRTUAL TABLE t3 USING rtree(id,x1,x2)} + db close + sqlite3 db utf16.db + do_execsql_test enc-13.2 { + WITH t1(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM t1 WHERE x<3) + SELECT rtreecheck('t3') FROM t1; + } {ok ok ok} +} + finish_test diff --git a/test/enc2.test b/test/enc2.test index 3eb3aa27c3..f7446a40ef 100644 --- a/test/enc2.test +++ b/test/enc2.test @@ -13,7 +13,6 @@ # various suported unicode encodings (UTF-8, UTF-16, UTF-16le and # UTF-16be). # -# $Id: enc2.test,v 1.29 2007/10/09 08:29:32 danielk1977 Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl @@ -64,7 +63,7 @@ set dbcontents { INSERT INTO t1 VALUES('one', 'I', 1); } # This proc tests that we can open and manipulate the test.db -# database, and that it is possible to retreive values in +# database, and that it is possible to retrieve values in # various text encodings. # proc run_test_script {t enc} { @@ -552,4 +551,17 @@ do_test enc2-10.1 { } } {t1 t2} +# 2020-01-15 ticket a08879a4a476eea9 +# Do not allow a database connection encoding change unless *all* +# attached databases are empty. +# +reset_db +do_execsql_test enc2-11.10 { + PRAGMA encoding=UTF8; + CREATE TEMP TABLE t1(x); + INSERT INTO t1 VALUES('this is a test'); + PRAGMA encoding=UTF16; + SELECT * FROM t1; +} {{this is a test}} + finish_test diff --git a/test/enc3.test b/test/enc3.test index 7ede2b716f..c9d39cbf3a 100644 --- a/test/enc3.test +++ b/test/enc3.test @@ -13,7 +13,6 @@ # The focus of this file is testing of the proper handling of conversions # to the native text representation. # -# $Id: enc3.test,v 1.8 2008/01/22 01:48:09 drh Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl diff --git a/test/eqp.test b/test/eqp.test index 046088c9c5..147b5ceafe 100644 --- a/test/eqp.test +++ b/test/eqp.test @@ -43,78 +43,134 @@ do_execsql_test 1.1 { do_eqp_test 1.2 { SELECT * FROM t2, t1 WHERE t1.a=1 OR t1.b=2; } { - 0 0 1 {SEARCH TABLE t1 USING INDEX i1 (a=?)} - 0 0 1 {SEARCH TABLE t1 USING INDEX i2 (b=?)} - 0 1 0 {SCAN TABLE t2} + QUERY PLAN + |--MULTI-INDEX OR + | |--INDEX 1 + | | `--SEARCH t1 USING INDEX i1 (a=?) + | `--INDEX 2 + | `--SEARCH t1 USING INDEX i2 (b=?) + `--SCAN t2 } do_eqp_test 1.3 { SELECT * FROM t2 CROSS JOIN t1 WHERE t1.a=1 OR t1.b=2; } { - 0 0 0 {SCAN TABLE t2} - 0 1 1 {SEARCH TABLE t1 USING INDEX i1 (a=?)} - 0 1 1 {SEARCH TABLE t1 USING INDEX i2 (b=?)} + QUERY PLAN + |--SCAN t2 + `--MULTI-INDEX OR + |--INDEX 1 + | `--SEARCH t1 USING INDEX i1 (a=?) + `--INDEX 2 + `--SEARCH t1 USING INDEX i2 (b=?) } do_eqp_test 1.3 { SELECT a FROM t1 ORDER BY a } { - 0 0 0 {SCAN TABLE t1 USING COVERING INDEX i1} + QUERY PLAN + `--SCAN t1 USING COVERING INDEX i1 } do_eqp_test 1.4 { SELECT a FROM t1 ORDER BY +a } { - 0 0 0 {SCAN TABLE t1 USING COVERING INDEX i1} - 0 0 0 {USE TEMP B-TREE FOR ORDER BY} + QUERY PLAN + |--SCAN t1 USING COVERING INDEX i1 + `--USE TEMP B-TREE FOR ORDER BY } do_eqp_test 1.5 { SELECT a FROM t1 WHERE a=4 } { - 0 0 0 {SEARCH TABLE t1 USING COVERING INDEX i1 (a=?)} + QUERY PLAN + `--SEARCH t1 USING COVERING INDEX i1 (a=?) } do_eqp_test 1.6 { SELECT DISTINCT count(*) FROM t3 GROUP BY a; } { - 0 0 0 {SCAN TABLE t3} - 0 0 0 {USE TEMP B-TREE FOR GROUP BY} - 0 0 0 {USE TEMP B-TREE FOR DISTINCT} + QUERY PLAN + |--SCAN t3 + |--USE TEMP B-TREE FOR GROUP BY + `--USE TEMP B-TREE FOR DISTINCT } -do_eqp_test 1.7 { +do_eqp_test 1.7.1 { SELECT * FROM t3 JOIN (SELECT 1) } { - 0 0 1 {SCAN SUBQUERY 1} - 0 1 0 {SCAN TABLE t3} + QUERY PLAN + |--CO-ROUTINE (subquery-xxxxxx) + | `--SCAN CONSTANT ROW + |--SCAN (subquery-xxxxxx) + `--SCAN t3 +} +do_eqp_test 1.7.2 { + SELECT * FROM t3 JOIN (SELECT 1) AS v1 +} { + QUERY PLAN + |--CO-ROUTINE v1 + | `--SCAN CONSTANT ROW + |--SCAN v1 + `--SCAN t3 +} +do_eqp_test 1.7.3 { + SELECT * FROM t3 AS xx JOIN (SELECT 1) AS yy +} { + QUERY PLAN + |--CO-ROUTINE yy + | `--SCAN CONSTANT ROW + |--SCAN yy + `--SCAN xx } + + do_eqp_test 1.8 { SELECT * FROM t3 JOIN (SELECT 1 UNION SELECT 2) } { - 1 0 0 {COMPOUND SUBQUERIES 2 AND 3 USING TEMP B-TREE (UNION)} - 0 0 1 {SCAN SUBQUERY 1} - 0 1 0 {SCAN TABLE t3} + QUERY PLAN + |--CO-ROUTINE (subquery-xxxxxx) + | `--COMPOUND QUERY + | |--LEFT-MOST SUBQUERY + | | `--SCAN CONSTANT ROW + | `--UNION USING TEMP B-TREE + | `--SCAN CONSTANT ROW + |--SCAN (subquery-xxxxxx) + `--SCAN t3 } do_eqp_test 1.9 { - SELECT * FROM t3 JOIN (SELECT 1 EXCEPT SELECT a FROM t3 LIMIT 17) -} { - 3 0 0 {SCAN TABLE t3} - 1 0 0 {COMPOUND SUBQUERIES 2 AND 3 USING TEMP B-TREE (EXCEPT)} - 0 0 1 {SCAN SUBQUERY 1} - 0 1 0 {SCAN TABLE t3} + SELECT * FROM t3 JOIN (SELECT 1 EXCEPT SELECT a FROM t3 LIMIT 17) AS abc +} { + QUERY PLAN + |--CO-ROUTINE abc + | `--COMPOUND QUERY + | |--LEFT-MOST SUBQUERY + | | `--SCAN CONSTANT ROW + | `--EXCEPT USING TEMP B-TREE + | `--SCAN t3 + |--SCAN abc + `--SCAN t3 } do_eqp_test 1.10 { - SELECT * FROM t3 JOIN (SELECT 1 INTERSECT SELECT a FROM t3 LIMIT 17) + SELECT * FROM t3 JOIN (SELECT 1 INTERSECT SELECT a FROM t3 LIMIT 17) AS abc } { - 3 0 0 {SCAN TABLE t3} - 1 0 0 {COMPOUND SUBQUERIES 2 AND 3 USING TEMP B-TREE (INTERSECT)} - 0 0 1 {SCAN SUBQUERY 1} - 0 1 0 {SCAN TABLE t3} + QUERY PLAN + |--CO-ROUTINE abc + | `--COMPOUND QUERY + | |--LEFT-MOST SUBQUERY + | | `--SCAN CONSTANT ROW + | `--INTERSECT USING TEMP B-TREE + | `--SCAN t3 + |--SCAN abc + `--SCAN t3 } do_eqp_test 1.11 { - SELECT * FROM t3 JOIN (SELECT 1 UNION ALL SELECT a FROM t3 LIMIT 17) + SELECT * FROM t3 JOIN (SELECT 1 UNION ALL SELECT a FROM t3 LIMIT 17) abc } { - 3 0 0 {SCAN TABLE t3} - 1 0 0 {COMPOUND SUBQUERIES 2 AND 3 (UNION ALL)} - 0 0 1 {SCAN SUBQUERY 1} - 0 1 0 {SCAN TABLE t3} + QUERY PLAN + |--CO-ROUTINE abc + | `--COMPOUND QUERY + | |--LEFT-MOST SUBQUERY + | | `--SCAN CONSTANT ROW + | `--UNION ALL + | `--SCAN t3 + |--SCAN abc + `--SCAN t3 } #------------------------------------------------------------------------- @@ -129,48 +185,58 @@ do_execsql_test 2.1 { } det 2.2.1 "SELECT DISTINCT min(x), max(x) FROM t1 GROUP BY x ORDER BY 1" { - 0 0 0 {SCAN TABLE t1} - 0 0 0 {USE TEMP B-TREE FOR GROUP BY} - 0 0 0 {USE TEMP B-TREE FOR DISTINCT} - 0 0 0 {USE TEMP B-TREE FOR ORDER BY} + QUERY PLAN + |--SCAN t1 + |--USE TEMP B-TREE FOR GROUP BY + |--USE TEMP B-TREE FOR DISTINCT + `--USE TEMP B-TREE FOR ORDER BY } det 2.2.2 "SELECT DISTINCT min(x), max(x) FROM t2 GROUP BY x ORDER BY 1" { - 0 0 0 {SCAN TABLE t2 USING COVERING INDEX t2i1} - 0 0 0 {USE TEMP B-TREE FOR DISTINCT} - 0 0 0 {USE TEMP B-TREE FOR ORDER BY} + QUERY PLAN + |--SCAN t2 USING COVERING INDEX t2i1 + |--USE TEMP B-TREE FOR DISTINCT + `--USE TEMP B-TREE FOR ORDER BY } det 2.2.3 "SELECT DISTINCT * FROM t1" { - 0 0 0 {SCAN TABLE t1} - 0 0 0 {USE TEMP B-TREE FOR DISTINCT} + QUERY PLAN + |--SCAN t1 + `--USE TEMP B-TREE FOR DISTINCT } det 2.2.4 "SELECT DISTINCT * FROM t1, t2" { - 0 0 0 {SCAN TABLE t1} - 0 1 1 {SCAN TABLE t2} - 0 0 0 {USE TEMP B-TREE FOR DISTINCT} + QUERY PLAN + |--SCAN t1 + |--SCAN t2 + `--USE TEMP B-TREE FOR DISTINCT } det 2.2.5 "SELECT DISTINCT * FROM t1, t2 ORDER BY t1.x" { - 0 0 0 {SCAN TABLE t1} - 0 1 1 {SCAN TABLE t2} - 0 0 0 {USE TEMP B-TREE FOR DISTINCT} - 0 0 0 {USE TEMP B-TREE FOR ORDER BY} + QUERY PLAN + |--SCAN t1 + |--SCAN t2 + |--USE TEMP B-TREE FOR DISTINCT + `--USE TEMP B-TREE FOR ORDER BY } det 2.2.6 "SELECT DISTINCT t2.x FROM t1, t2 ORDER BY t2.x" { - 0 0 1 {SCAN TABLE t2 USING COVERING INDEX t2i1} - 0 1 0 {SCAN TABLE t1} + QUERY PLAN + |--SCAN t2 USING COVERING INDEX t2i1 + `--SCAN t1 } det 2.3.1 "SELECT max(x) FROM t2" { - 0 0 0 {SEARCH TABLE t2 USING COVERING INDEX t2i1} + QUERY PLAN + `--SEARCH t2 USING COVERING INDEX t2i1 } det 2.3.2 "SELECT min(x) FROM t2" { - 0 0 0 {SEARCH TABLE t2 USING COVERING INDEX t2i1} + QUERY PLAN + `--SEARCH t2 USING COVERING INDEX t2i1 } det 2.3.3 "SELECT min(x), max(x) FROM t2" { - 0 0 0 {SCAN TABLE t2 USING COVERING INDEX t2i1} + QUERY PLAN + `--SCAN t2 USING COVERING INDEX t2i1 } det 2.4.1 "SELECT * FROM t1 WHERE rowid=?" { - 0 0 0 {SEARCH TABLE t1 USING INTEGER PRIMARY KEY (rowid=?)} + QUERY PLAN + `--SEARCH t1 USING INTEGER PRIMARY KEY (rowid=?) } @@ -181,75 +247,98 @@ det 2.4.1 "SELECT * FROM t1 WHERE rowid=?" { do_eqp_test 3.1.1 { SELECT (SELECT x FROM t1 AS sub) FROM t1; } { - 0 0 0 {SCAN TABLE t1} - 0 0 0 {EXECUTE SCALAR SUBQUERY 1} - 1 0 0 {SCAN TABLE t1 AS sub} + QUERY PLAN + |--SCAN t1 + `--SCALAR SUBQUERY xxxxxx + `--SCAN sub } do_eqp_test 3.1.2 { SELECT * FROM t1 WHERE (SELECT x FROM t1 AS sub); } { - 0 0 0 {SCAN TABLE t1} - 0 0 0 {EXECUTE SCALAR SUBQUERY 1} - 1 0 0 {SCAN TABLE t1 AS sub} + QUERY PLAN + |--SCAN t1 + `--SCALAR SUBQUERY xxxxxx + `--SCAN sub } do_eqp_test 3.1.3 { SELECT * FROM t1 WHERE (SELECT x FROM t1 AS sub ORDER BY y); } { - 0 0 0 {SCAN TABLE t1} - 0 0 0 {EXECUTE SCALAR SUBQUERY 1} - 1 0 0 {SCAN TABLE t1 AS sub} - 1 0 0 {USE TEMP B-TREE FOR ORDER BY} + QUERY PLAN + |--SCAN t1 + `--SCALAR SUBQUERY xxxxxx + |--SCAN sub + `--USE TEMP B-TREE FOR ORDER BY } do_eqp_test 3.1.4 { SELECT * FROM t1 WHERE (SELECT x FROM t2 ORDER BY x); } { - 0 0 0 {SCAN TABLE t1} - 0 0 0 {EXECUTE SCALAR SUBQUERY 1} - 1 0 0 {SCAN TABLE t2 USING COVERING INDEX t2i1} + QUERY PLAN + |--SCAN t1 + `--SCALAR SUBQUERY xxxxxx + `--SCAN t2 USING COVERING INDEX t2i1 } det 3.2.1 { SELECT * FROM (SELECT * FROM t1 ORDER BY x LIMIT 10) ORDER BY y LIMIT 5 } { - 1 0 0 {SCAN TABLE t1} - 1 0 0 {USE TEMP B-TREE FOR ORDER BY} - 0 0 0 {SCAN SUBQUERY 1} - 0 0 0 {USE TEMP B-TREE FOR ORDER BY} + QUERY PLAN + |--CO-ROUTINE (subquery-xxxxxx) + | |--SCAN t1 + | `--USE TEMP B-TREE FOR ORDER BY + |--SCAN (subquery-xxxxxx) + `--USE TEMP B-TREE FOR ORDER BY } + det 3.2.2 { SELECT * FROM (SELECT * FROM t1 ORDER BY x LIMIT 10) AS x1, (SELECT * FROM t2 ORDER BY x LIMIT 10) AS x2 ORDER BY x2.y LIMIT 5 } { - 1 0 0 {SCAN TABLE t1} - 1 0 0 {USE TEMP B-TREE FOR ORDER BY} - 2 0 0 {SCAN TABLE t2 USING INDEX t2i1} - 0 0 0 {SCAN SUBQUERY 1 AS x1} - 0 1 1 {SCAN SUBQUERY 2 AS x2} - 0 0 0 {USE TEMP B-TREE FOR ORDER BY} + QUERY PLAN + |--CO-ROUTINE x1 + | |--SCAN t1 + | `--USE TEMP B-TREE FOR ORDER BY + |--MATERIALIZE x2 + | `--SCAN t2 USING INDEX t2i1 + |--SCAN x1 + |--SCAN x2 + `--USE TEMP B-TREE FOR ORDER BY +} + +det 3.2.3 { + SELECT * FROM (SELECT * FROM t1 ORDER BY x LIMIT 10) ORDER BY x LIMIT 5 +} { + QUERY PLAN + |--CO-ROUTINE (subquery-xxxxxx) + | |--SCAN t1 + | `--USE TEMP B-TREE FOR ORDER BY + `--SCAN (subquery-xxxxxx) } det 3.3.1 { SELECT * FROM t1 WHERE y IN (SELECT y FROM t2) } { - 0 0 0 {SCAN TABLE t1} - 0 0 0 {EXECUTE LIST SUBQUERY 1} - 1 0 0 {SCAN TABLE t2} + QUERY PLAN + |--SCAN t1 + `--LIST SUBQUERY xxxxxx + |--SCAN t2 + `--CREATE BLOOM FILTER } det 3.3.2 { SELECT * FROM t1 WHERE y IN (SELECT y FROM t2 WHERE t1.x!=t2.x) } { - 0 0 0 {SCAN TABLE t1} - 0 0 0 {EXECUTE CORRELATED LIST SUBQUERY 1} - 1 0 0 {SCAN TABLE t2} + QUERY PLAN + |--SCAN t1 + `--CORRELATED LIST SUBQUERY xxxxxx + `--SCAN t2 } det 3.3.3 { SELECT * FROM t1 WHERE EXISTS (SELECT y FROM t2 WHERE t1.x!=t2.x) } { - 0 0 0 {SCAN TABLE t1} - 0 0 0 {EXECUTE CORRELATED SCALAR SUBQUERY 1} - 1 0 0 {SCAN TABLE t2} + QUERY PLAN + |--SCAN t1 + `--SCAN t2 EXISTS } #------------------------------------------------------------------------- @@ -258,303 +347,344 @@ det 3.3.3 { do_eqp_test 4.1.1 { SELECT * FROM t1 UNION ALL SELECT * FROM t2 } { - 1 0 0 {SCAN TABLE t1} - 2 0 0 {SCAN TABLE t2} - 0 0 0 {COMPOUND SUBQUERIES 1 AND 2 (UNION ALL)} + QUERY PLAN + `--COMPOUND QUERY + |--LEFT-MOST SUBQUERY + | `--SCAN t1 + `--UNION ALL + `--SCAN t2 } do_eqp_test 4.1.2 { SELECT * FROM t1 UNION ALL SELECT * FROM t2 ORDER BY 2 } { - 1 0 0 {SCAN TABLE t1} - 1 0 0 {USE TEMP B-TREE FOR ORDER BY} - 2 0 0 {SCAN TABLE t2} - 2 0 0 {USE TEMP B-TREE FOR ORDER BY} - 0 0 0 {COMPOUND SUBQUERIES 1 AND 2 (UNION ALL)} + QUERY PLAN + `--MERGE (UNION ALL) + |--LEFT + | |--SCAN t1 + | `--USE TEMP B-TREE FOR ORDER BY + `--RIGHT + |--SCAN t2 + `--USE TEMP B-TREE FOR ORDER BY } do_eqp_test 4.1.3 { SELECT * FROM t1 UNION SELECT * FROM t2 ORDER BY 2 } { - 1 0 0 {SCAN TABLE t1} - 1 0 0 {USE TEMP B-TREE FOR ORDER BY} - 2 0 0 {SCAN TABLE t2} - 2 0 0 {USE TEMP B-TREE FOR ORDER BY} - 0 0 0 {COMPOUND SUBQUERIES 1 AND 2 (UNION)} + QUERY PLAN + `--MERGE (UNION) + |--LEFT + | |--SCAN t1 + | `--USE TEMP B-TREE FOR ORDER BY + `--RIGHT + |--SCAN t2 + `--USE TEMP B-TREE FOR ORDER BY } do_eqp_test 4.1.4 { SELECT * FROM t1 INTERSECT SELECT * FROM t2 ORDER BY 2 } { - 1 0 0 {SCAN TABLE t1} - 1 0 0 {USE TEMP B-TREE FOR ORDER BY} - 2 0 0 {SCAN TABLE t2} - 2 0 0 {USE TEMP B-TREE FOR ORDER BY} - 0 0 0 {COMPOUND SUBQUERIES 1 AND 2 (INTERSECT)} + QUERY PLAN + `--MERGE (INTERSECT) + |--LEFT + | |--SCAN t1 + | `--USE TEMP B-TREE FOR ORDER BY + `--RIGHT + |--SCAN t2 + `--USE TEMP B-TREE FOR ORDER BY } do_eqp_test 4.1.5 { SELECT * FROM t1 EXCEPT SELECT * FROM t2 ORDER BY 2 } { - 1 0 0 {SCAN TABLE t1} - 1 0 0 {USE TEMP B-TREE FOR ORDER BY} - 2 0 0 {SCAN TABLE t2} - 2 0 0 {USE TEMP B-TREE FOR ORDER BY} - 0 0 0 {COMPOUND SUBQUERIES 1 AND 2 (EXCEPT)} + QUERY PLAN + `--MERGE (EXCEPT) + |--LEFT + | |--SCAN t1 + | `--USE TEMP B-TREE FOR ORDER BY + `--RIGHT + |--SCAN t2 + `--USE TEMP B-TREE FOR ORDER BY } do_eqp_test 4.2.2 { SELECT * FROM t1 UNION ALL SELECT * FROM t2 ORDER BY 1 } { - 1 0 0 {SCAN TABLE t1} - 1 0 0 {USE TEMP B-TREE FOR ORDER BY} - 2 0 0 {SCAN TABLE t2 USING INDEX t2i1} - 0 0 0 {COMPOUND SUBQUERIES 1 AND 2 (UNION ALL)} + QUERY PLAN + `--MERGE (UNION ALL) + |--LEFT + | |--SCAN t1 + | `--USE TEMP B-TREE FOR ORDER BY + `--RIGHT + `--SCAN t2 USING INDEX t2i1 } do_eqp_test 4.2.3 { SELECT * FROM t1 UNION SELECT * FROM t2 ORDER BY 1 } { - 1 0 0 {SCAN TABLE t1} - 1 0 0 {USE TEMP B-TREE FOR ORDER BY} - 2 0 0 {SCAN TABLE t2 USING INDEX t2i1} - 2 0 0 {USE TEMP B-TREE FOR RIGHT PART OF ORDER BY} - 0 0 0 {COMPOUND SUBQUERIES 1 AND 2 (UNION)} + QUERY PLAN + `--MERGE (UNION) + |--LEFT + | |--SCAN t1 + | `--USE TEMP B-TREE FOR ORDER BY + `--RIGHT + |--SCAN t2 USING INDEX t2i1 + `--USE TEMP B-TREE FOR LAST 2 TERMS OF ORDER BY } do_eqp_test 4.2.4 { SELECT * FROM t1 INTERSECT SELECT * FROM t2 ORDER BY 1 } { - 1 0 0 {SCAN TABLE t1} - 1 0 0 {USE TEMP B-TREE FOR ORDER BY} - 2 0 0 {SCAN TABLE t2 USING INDEX t2i1} - 2 0 0 {USE TEMP B-TREE FOR RIGHT PART OF ORDER BY} - 0 0 0 {COMPOUND SUBQUERIES 1 AND 2 (INTERSECT)} + QUERY PLAN + `--MERGE (INTERSECT) + |--LEFT + | |--SCAN t1 + | `--USE TEMP B-TREE FOR ORDER BY + `--RIGHT + |--SCAN t2 USING INDEX t2i1 + `--USE TEMP B-TREE FOR LAST 2 TERMS OF ORDER BY } do_eqp_test 4.2.5 { SELECT * FROM t1 EXCEPT SELECT * FROM t2 ORDER BY 1 } { - 1 0 0 {SCAN TABLE t1} - 1 0 0 {USE TEMP B-TREE FOR ORDER BY} - 2 0 0 {SCAN TABLE t2 USING INDEX t2i1} - 2 0 0 {USE TEMP B-TREE FOR RIGHT PART OF ORDER BY} - 0 0 0 {COMPOUND SUBQUERIES 1 AND 2 (EXCEPT)} + QUERY PLAN + `--MERGE (EXCEPT) + |--LEFT + | |--SCAN t1 + | `--USE TEMP B-TREE FOR ORDER BY + `--RIGHT + |--SCAN t2 USING INDEX t2i1 + `--USE TEMP B-TREE FOR LAST 2 TERMS OF ORDER BY } do_eqp_test 4.3.1 { SELECT x FROM t1 UNION SELECT x FROM t2 } { - 1 0 0 {SCAN TABLE t1} - 2 0 0 {SCAN TABLE t2 USING COVERING INDEX t2i1} - 0 0 0 {COMPOUND SUBQUERIES 1 AND 2 USING TEMP B-TREE (UNION)} + QUERY PLAN + `--COMPOUND QUERY + |--LEFT-MOST SUBQUERY + | `--SCAN t1 + `--UNION USING TEMP B-TREE + `--SCAN t2 USING COVERING INDEX t2i1 } do_eqp_test 4.3.2 { SELECT x FROM t1 UNION SELECT x FROM t2 UNION SELECT x FROM t1 } { - 2 0 0 {SCAN TABLE t1} - 3 0 0 {SCAN TABLE t2 USING COVERING INDEX t2i1} - 1 0 0 {COMPOUND SUBQUERIES 2 AND 3 USING TEMP B-TREE (UNION)} - 4 0 0 {SCAN TABLE t1} - 0 0 0 {COMPOUND SUBQUERIES 1 AND 4 USING TEMP B-TREE (UNION)} + QUERY PLAN + `--COMPOUND QUERY + |--LEFT-MOST SUBQUERY + | `--SCAN t1 + |--UNION USING TEMP B-TREE + | `--SCAN t2 USING COVERING INDEX t2i1 + `--UNION USING TEMP B-TREE + `--SCAN t1 } do_eqp_test 4.3.3 { SELECT x FROM t1 UNION SELECT x FROM t2 UNION SELECT x FROM t1 ORDER BY 1 } { - 2 0 0 {SCAN TABLE t1} - 2 0 0 {USE TEMP B-TREE FOR ORDER BY} - 3 0 0 {SCAN TABLE t2 USING COVERING INDEX t2i1} - 1 0 0 {COMPOUND SUBQUERIES 2 AND 3 (UNION)} - 4 0 0 {SCAN TABLE t1} - 4 0 0 {USE TEMP B-TREE FOR ORDER BY} - 0 0 0 {COMPOUND SUBQUERIES 1 AND 4 (UNION)} -} - + QUERY PLAN + `--MERGE (UNION) + |--LEFT + | `--MERGE (UNION) + | |--LEFT + | | |--SCAN t1 + | | `--USE TEMP B-TREE FOR ORDER BY + | `--RIGHT + | `--SCAN t2 USING COVERING INDEX t2i1 + `--RIGHT + |--SCAN t1 + `--USE TEMP B-TREE FOR ORDER BY +} + +if 0 { #------------------------------------------------------------------------- # This next block of tests verifies that the examples on the # lang_explain.html page are correct. # drop_all_tables -# EVIDENCE-OF: R-47779-47605 sqlite> EXPLAIN QUERY PLAN SELECT a, b +# XVIDENCE-OF: R-47779-47605 sqlite> EXPLAIN QUERY PLAN SELECT a, b # FROM t1 WHERE a=1; -# 0|0|0|SCAN TABLE t1 +# 0|0|0|SCAN t1 # do_execsql_test 5.1.0 { CREATE TABLE t1(a INT, b INT, ex TEXT) } det 5.1.1 "SELECT a, b FROM t1 WHERE a=1" { - 0 0 0 {SCAN TABLE t1} + 0 0 0 {SCAN t1} } -# EVIDENCE-OF: R-55852-17599 sqlite> CREATE INDEX i1 ON t1(a); +# XVIDENCE-OF: R-55852-17599 sqlite> CREATE INDEX i1 ON t1(a); # sqlite> EXPLAIN QUERY PLAN SELECT a, b FROM t1 WHERE a=1; -# 0|0|0|SEARCH TABLE t1 USING INDEX i1 +# 0|0|0|SEARCH t1 USING INDEX i1 # do_execsql_test 5.2.0 { CREATE INDEX i1 ON t1(a) } det 5.2.1 "SELECT a, b FROM t1 WHERE a=1" { - 0 0 0 {SEARCH TABLE t1 USING INDEX i1 (a=?)} + 0 0 0 {SEARCH t1 USING INDEX i1 (a=?)} } -# EVIDENCE-OF: R-21179-11011 sqlite> CREATE INDEX i2 ON t1(a, b); +# XVIDENCE-OF: R-21179-11011 sqlite> CREATE INDEX i2 ON t1(a, b); # sqlite> EXPLAIN QUERY PLAN SELECT a, b FROM t1 WHERE a=1; -# 0|0|0|SEARCH TABLE t1 USING COVERING INDEX i2 (a=?) +# 0|0|0|SEARCH t1 USING COVERING INDEX i2 (a=?) # do_execsql_test 5.3.0 { CREATE INDEX i2 ON t1(a, b) } det 5.3.1 "SELECT a, b FROM t1 WHERE a=1" { - 0 0 0 {SEARCH TABLE t1 USING COVERING INDEX i2 (a=?)} + 0 0 0 {SEARCH t1 USING COVERING INDEX i2 (a=?)} } -# EVIDENCE-OF: R-09991-48941 sqlite> EXPLAIN QUERY PLAN +# XVIDENCE-OF: R-09991-48941 sqlite> EXPLAIN QUERY PLAN # SELECT t1.*, t2.* FROM t1, t2 WHERE t1.a=1 AND t1.b>2; -# 0|0|0|SEARCH TABLE t1 USING COVERING INDEX i2 (a=? AND b>?) -# 0|1|1|SCAN TABLE t2 +# 0|0|0|SEARCH t1 USING COVERING INDEX i2 (a=? AND b>?) +# 0|1|1|SCAN t2 # do_execsql_test 5.4.0 {CREATE TABLE t2(c INT, d INT, ex TEXT)} det 5.4.1 "SELECT t1.a, t2.c FROM t1, t2 WHERE t1.a=1 AND t1.b>2" { - 0 0 0 {SEARCH TABLE t1 USING COVERING INDEX i2 (a=? AND b>?)} - 0 1 1 {SCAN TABLE t2} + 0 0 0 {SEARCH t1 USING COVERING INDEX i2 (a=? AND b>?)} + 0 1 1 {SCAN t2} } -# EVIDENCE-OF: R-33626-61085 sqlite> EXPLAIN QUERY PLAN +# XVIDENCE-OF: R-33626-61085 sqlite> EXPLAIN QUERY PLAN # SELECT t1.*, t2.* FROM t2, t1 WHERE t1.a=1 AND t1.b>2; -# 0|0|1|SEARCH TABLE t1 USING COVERING INDEX i2 (a=? AND b>?) -# 0|1|0|SCAN TABLE t2 +# 0|0|1|SEARCH t1 USING COVERING INDEX i2 (a=? AND b>?) +# 0|1|0|SCAN t2 # det 5.5 "SELECT t1.a, t2.c FROM t2, t1 WHERE t1.a=1 AND t1.b>2" { - 0 0 1 {SEARCH TABLE t1 USING COVERING INDEX i2 (a=? AND b>?)} - 0 1 0 {SCAN TABLE t2} + 0 0 1 {SEARCH t1 USING COVERING INDEX i2 (a=? AND b>?)} + 0 1 0 {SCAN t2} } -# EVIDENCE-OF: R-04002-25654 sqlite> CREATE INDEX i3 ON t1(b); +# XVIDENCE-OF: R-04002-25654 sqlite> CREATE INDEX i3 ON t1(b); # sqlite> EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=1 OR b=2; -# 0|0|0|SEARCH TABLE t1 USING COVERING INDEX i2 (a=?) -# 0|0|0|SEARCH TABLE t1 USING INDEX i3 (b=?) +# 0|0|0|SEARCH t1 USING COVERING INDEX i2 (a=?) +# 0|0|0|SEARCH t1 USING INDEX i3 (b=?) # do_execsql_test 5.5.0 {CREATE INDEX i3 ON t1(b)} det 5.6.1 "SELECT a, b FROM t1 WHERE a=1 OR b=2" { - 0 0 0 {SEARCH TABLE t1 USING COVERING INDEX i2 (a=?)} - 0 0 0 {SEARCH TABLE t1 USING INDEX i3 (b=?)} + 0 0 0 {SEARCH t1 USING COVERING INDEX i2 (a=?)} + 0 0 0 {SEARCH t1 USING INDEX i3 (b=?)} } -# EVIDENCE-OF: R-24577-38891 sqlite> EXPLAIN QUERY PLAN +# XVIDENCE-OF: R-24577-38891 sqlite> EXPLAIN QUERY PLAN # SELECT c, d FROM t2 ORDER BY c; -# 0|0|0|SCAN TABLE t2 +# 0|0|0|SCAN t2 # 0|0|0|USE TEMP B-TREE FOR ORDER BY # det 5.7 "SELECT c, d FROM t2 ORDER BY c" { - 0 0 0 {SCAN TABLE t2} + 0 0 0 {SCAN t2} 0 0 0 {USE TEMP B-TREE FOR ORDER BY} } -# EVIDENCE-OF: R-58157-12355 sqlite> CREATE INDEX i4 ON t2(c); +# XVIDENCE-OF: R-58157-12355 sqlite> CREATE INDEX i4 ON t2(c); # sqlite> EXPLAIN QUERY PLAN SELECT c, d FROM t2 ORDER BY c; -# 0|0|0|SCAN TABLE t2 USING INDEX i4 +# 0|0|0|SCAN t2 USING INDEX i4 # do_execsql_test 5.8.0 {CREATE INDEX i4 ON t2(c)} det 5.8.1 "SELECT c, d FROM t2 ORDER BY c" { - 0 0 0 {SCAN TABLE t2 USING INDEX i4} + 0 0 0 {SCAN t2 USING INDEX i4} } -# EVIDENCE-OF: R-13931-10421 sqlite> EXPLAIN QUERY PLAN SELECT +# XVIDENCE-OF: R-13931-10421 sqlite> EXPLAIN QUERY PLAN SELECT # (SELECT b FROM t1 WHERE a=0), (SELECT a FROM t1 WHERE b=t2.c) FROM t2; -# 0|0|0|SCAN TABLE t2 +# 0|0|0|SCAN t2 # 0|0|0|EXECUTE SCALAR SUBQUERY 1 -# 1|0|0|SEARCH TABLE t1 USING COVERING INDEX i2 (a=?) +# 1|0|0|SEARCH t1 USING COVERING INDEX i2 (a=?) # 0|0|0|EXECUTE CORRELATED SCALAR SUBQUERY 2 -# 2|0|0|SEARCH TABLE t1 USING INDEX i3 (b=?) +# 2|0|0|SEARCH t1 USING INDEX i3 (b=?) # det 5.9 { SELECT (SELECT b FROM t1 WHERE a=0), (SELECT a FROM t1 WHERE b=t2.c) FROM t2 } { - 0 0 0 {SCAN TABLE t2 USING COVERING INDEX i4} + 0 0 0 {SCAN t2 USING COVERING INDEX i4} 0 0 0 {EXECUTE SCALAR SUBQUERY 1} - 1 0 0 {SEARCH TABLE t1 USING COVERING INDEX i2 (a=?)} + 1 0 0 {SEARCH t1 USING COVERING INDEX i2 (a=?)} 0 0 0 {EXECUTE CORRELATED SCALAR SUBQUERY 2} - 2 0 0 {SEARCH TABLE t1 USING INDEX i3 (b=?)} + 2 0 0 {SEARCH t1 USING INDEX i3 (b=?)} } -# EVIDENCE-OF: R-50892-45943 sqlite> EXPLAIN QUERY PLAN +# XVIDENCE-OF: R-50892-45943 sqlite> EXPLAIN QUERY PLAN # SELECT count(*) FROM (SELECT max(b) AS x FROM t1 GROUP BY a) GROUP BY x; -# 1|0|0|SCAN TABLE t1 USING COVERING INDEX i2 +# 1|0|0|SCAN t1 USING COVERING INDEX i2 # 0|0|0|SCAN SUBQUERY 1 # 0|0|0|USE TEMP B-TREE FOR GROUP BY # det 5.10 { SELECT count(*) FROM (SELECT max(b) AS x FROM t1 GROUP BY a) GROUP BY x } { - 1 0 0 {SCAN TABLE t1 USING COVERING INDEX i2} + 1 0 0 {SCAN t1 USING COVERING INDEX i2} 0 0 0 {SCAN SUBQUERY 1} 0 0 0 {USE TEMP B-TREE FOR GROUP BY} } -# EVIDENCE-OF: R-46219-33846 sqlite> EXPLAIN QUERY PLAN +# XVIDENCE-OF: R-46219-33846 sqlite> EXPLAIN QUERY PLAN # SELECT * FROM (SELECT * FROM t2 WHERE c=1), t1; -# 0|0|0|SEARCH TABLE t2 USING INDEX i4 (c=?) -# 0|1|1|SCAN TABLE t1 +# 0|0|0|SEARCH t2 USING INDEX i4 (c=?) +# 0|1|1|SCAN t1 # det 5.11 "SELECT a, b FROM (SELECT * FROM t2 WHERE c=1), t1" { - 0 0 0 {SEARCH TABLE t2 USING INDEX i4 (c=?)} - 0 1 1 {SCAN TABLE t1 USING COVERING INDEX i2} + 0 0 0 {SEARCH t2 USING INDEX i4 (c=?)} + 0 1 1 {SCAN t1 USING COVERING INDEX i2} } -# EVIDENCE-OF: R-37879-39987 sqlite> EXPLAIN QUERY PLAN +# XVIDENCE-OF: R-37879-39987 sqlite> EXPLAIN QUERY PLAN # SELECT a FROM t1 UNION SELECT c FROM t2; -# 1|0|0|SCAN TABLE t1 -# 2|0|0|SCAN TABLE t2 +# 1|0|0|SCAN t1 +# 2|0|0|SCAN t2 # 0|0|0|COMPOUND SUBQUERIES 1 AND 2 USING TEMP B-TREE (UNION) # det 5.12 "SELECT a,b FROM t1 UNION SELECT c, 99 FROM t2" { - 1 0 0 {SCAN TABLE t1 USING COVERING INDEX i2} - 2 0 0 {SCAN TABLE t2 USING COVERING INDEX i4} + 1 0 0 {SCAN t1 USING COVERING INDEX i2} + 2 0 0 {SCAN t2 USING COVERING INDEX i4} 0 0 0 {COMPOUND SUBQUERIES 1 AND 2 USING TEMP B-TREE (UNION)} } -# EVIDENCE-OF: R-44864-63011 sqlite> EXPLAIN QUERY PLAN +# XVIDENCE-OF: R-44864-63011 sqlite> EXPLAIN QUERY PLAN # SELECT a FROM t1 EXCEPT SELECT d FROM t2 ORDER BY 1; -# 1|0|0|SCAN TABLE t1 USING COVERING INDEX i2 -# 2|0|0|SCAN TABLE t2 2|0|0|USE TEMP B-TREE FOR ORDER BY +# 1|0|0|SCAN t1 USING COVERING INDEX i2 +# 2|0|0|SCAN t2 2|0|0|USE TEMP B-TREE FOR ORDER BY # 0|0|0|COMPOUND SUBQUERIES 1 AND 2 (EXCEPT) # det 5.13 "SELECT a FROM t1 EXCEPT SELECT d FROM t2 ORDER BY 1" { - 1 0 0 {SCAN TABLE t1 USING COVERING INDEX i1} - 2 0 0 {SCAN TABLE t2} + 1 0 0 {SCAN t1 USING COVERING INDEX i1} + 2 0 0 {SCAN t2} 2 0 0 {USE TEMP B-TREE FOR ORDER BY} 0 0 0 {COMPOUND SUBQUERIES 1 AND 2 (EXCEPT)} } - -#------------------------------------------------------------------------- -# The following tests - eqp-6.* - test that the example C code on -# documentation page eqp.html works. The C code is duplicated in test1.c -# and wrapped in Tcl command [print_explain_query_plan] -# -set boilerplate { - proc explain_query_plan {db sql} { - set stmt [sqlite3_prepare_v2 db $sql -1 DUMMY] - print_explain_query_plan $stmt - sqlite3_finalize $stmt +if {![nonzero_reserved_bytes]} { + #------------------------------------------------------------------------- + # The following tests - eqp-6.* - test that the example C code on + # documentation page eqp.html works. The C code is duplicated in test1.c + # and wrapped in Tcl command [print_explain_query_plan] + # + set boilerplate { + proc explain_query_plan {db sql} { + set stmt [sqlite3_prepare_v2 db $sql -1 DUMMY] + print_explain_query_plan $stmt + sqlite3_finalize $stmt + } + sqlite3 db test.db + explain_query_plan db {%SQL%} + db close + exit } - sqlite3 db test.db - explain_query_plan db {%SQL%} - db close - exit -} - -# Do a "Print Explain Query Plan" test. -proc do_peqp_test {tn sql res} { - set fd [open script.tcl w] - puts $fd [string map [list %SQL% $sql] $::boilerplate] - close $fd - - uplevel do_test $tn [list { - set fd [open "|[info nameofexec] script.tcl"] - set data [read $fd] + + # Do a "Print Explain Query Plan" test. + proc do_peqp_test {tn sql res} { + set fd [open script.tcl w] + puts $fd [string map [list %SQL% $sql] $::boilerplate] close $fd - set data - }] [list $res] -} - -do_peqp_test 6.1 { - SELECT a, b FROM t1 EXCEPT SELECT d, 99 FROM t2 ORDER BY 1 -} [string trimleft { -1 0 0 SCAN TABLE t1 USING COVERING INDEX i2 -2 0 0 SCAN TABLE t2 + + uplevel do_test $tn [list { + set fd [open "|[info nameofexec] script.tcl"] + set data [read $fd] + close $fd + set data + }] [list $res] + } + + do_peqp_test 6.1 { + SELECT a, b FROM t1 EXCEPT SELECT d, 99 FROM t2 ORDER BY 1 + } [string trimleft { +1 0 0 SCAN t1 USING COVERING INDEX i2 +2 0 0 SCAN t2 2 0 0 USE TEMP B-TREE FOR ORDER BY 0 0 0 COMPOUND SUBQUERIES 1 AND 2 (EXCEPT) }] +} +} #------------------------------------------------------------------------- # The following tests - eqp-7.* - test that queries that use the OP_Count @@ -569,11 +699,13 @@ do_execsql_test 7.0 { } det 7.1 "SELECT count(*) FROM t1" { - 0 0 0 {SCAN TABLE t1} + QUERY PLAN + `--SCAN t1 } det 7.2 "SELECT count(*) FROM t2" { - 0 0 0 {SCAN TABLE t2 USING COVERING INDEX i1} + QUERY PLAN + `--SCAN t2 USING COVERING INDEX i1 } do_execsql_test 7.3 { @@ -591,11 +723,13 @@ db close sqlite3 db test.db det 7.4 "SELECT count(*) FROM t1" { - 0 0 0 {SCAN TABLE t1} + QUERY PLAN + `--SCAN t1 } det 7.5 "SELECT count(*) FROM t2" { - 0 0 0 {SCAN TABLE t2 USING COVERING INDEX i1} + QUERY PLAN + `--SCAN t2 USING COVERING INDEX i1 } #------------------------------------------------------------------------- @@ -610,37 +744,160 @@ do_execsql_test 8.0 { } det 8.1.1 "SELECT * FROM t2" { - 0 0 0 {SCAN TABLE t2} + QUERY PLAN + `--SCAN t2 } det 8.1.2 "SELECT * FROM t2 WHERE rowid=?" { - 0 0 0 {SEARCH TABLE t2 USING INTEGER PRIMARY KEY (rowid=?)} + QUERY PLAN + `--SEARCH t2 USING INTEGER PRIMARY KEY (rowid=?) } det 8.1.3 "SELECT count(*) FROM t2" { - 0 0 0 {SCAN TABLE t2} + QUERY PLAN + `--SCAN t2 } det 8.2.1 "SELECT * FROM t1" { - 0 0 0 {SCAN TABLE t1} + QUERY PLAN + `--SCAN t1 } det 8.2.2 "SELECT * FROM t1 WHERE b=?" { - 0 0 0 {SEARCH TABLE t1 USING PRIMARY KEY (b=?)} + QUERY PLAN + `--SEARCH t1 USING PRIMARY KEY (b=?) } det 8.2.3 "SELECT * FROM t1 WHERE b=? AND c=?" { - 0 0 0 {SEARCH TABLE t1 USING PRIMARY KEY (b=? AND c=?)} + QUERY PLAN + `--SEARCH t1 USING PRIMARY KEY (b=? AND c=?) } det 8.2.4 "SELECT count(*) FROM t1" { - 0 0 0 {SCAN TABLE t1} + QUERY PLAN + `--SCAN t1 } - - - - - +# 2018-08-16: While working on Fossil I discovered that EXPLAIN QUERY PLAN +# did not describe IN operators implemented using a ROWID lookup. These +# test cases ensure that problem as been fixed. +# +do_execsql_test 9.0 { + -- Schema from Fossil 2018-08-16 + CREATE TABLE forumpost( + fpid INTEGER PRIMARY KEY, + froot INT, + fprev INT, + firt INT, + fmtime REAL + ); + CREATE INDEX forumthread ON forumpost(froot,fmtime); + CREATE TABLE blob( + rid INTEGER PRIMARY KEY, + rcvid INTEGER, + size INTEGER, + uuid TEXT UNIQUE NOT NULL, + content BLOB, + CHECK( length(uuid)>=40 AND rid>0 ) + ); + CREATE TABLE event( + type TEXT, + mtime DATETIME, + objid INTEGER PRIMARY KEY, + tagid INTEGER, + uid INTEGER REFERENCES user, + bgcolor TEXT, + euser TEXT, + user TEXT, + ecomment TEXT, + comment TEXT, + brief TEXT, + omtime DATETIME + ); + CREATE INDEX event_i1 ON event(mtime); + CREATE TABLE private(rid INTEGER PRIMARY KEY); +} +optimization_control db order-by-subquery off +do_eqp_test 9.1 { + WITH thread(age,duration,cnt,root,last) AS ( + SELECT + julianday('now') - max(fmtime) AS age, + max(fmtime) - min(fmtime) AS duration, + sum(fprev IS NULL) AS msg_count, + froot, + (SELECT fpid FROM forumpost + WHERE froot=x.froot + AND fpid NOT IN private + ORDER BY fmtime DESC LIMIT 1) + FROM forumpost AS x + WHERE fpid NOT IN private --- Ensure this table mentioned in EQP output! + GROUP BY froot + ORDER BY 1 LIMIT 26 OFFSET 5 + ) + SELECT + thread.age, + thread.duration, + thread.cnt, + blob.uuid, + substr(event.comment,instr(event.comment,':')+1) + FROM thread, blob, event + WHERE blob.rid=thread.last + AND event.objid=thread.last + ORDER BY 1; +} { + QUERY PLAN + |--CO-ROUTINE thread + | |--SCAN x USING INDEX forumthread + | |--USING ROWID SEARCH ON TABLE private FOR IN-OPERATOR + | |--CORRELATED SCALAR SUBQUERY xxxxxx + | | |--SEARCH forumpost USING COVERING INDEX forumthread (froot=?) + | | `--USING ROWID SEARCH ON TABLE private FOR IN-OPERATOR + | `--USE TEMP B-TREE FOR ORDER BY + |--SCAN thread + |--SEARCH blob USING INTEGER PRIMARY KEY (rowid=?) + |--SEARCH event USING INTEGER PRIMARY KEY (rowid=?) + `--USE TEMP B-TREE FOR ORDER BY +} +optimization_control db all on +db cache flush +do_eqp_test 9.2 { + WITH thread(age,duration,cnt,root,last) AS ( + SELECT + julianday('now') - max(fmtime) AS age, + max(fmtime) - min(fmtime) AS duration, + sum(fprev IS NULL) AS msg_count, + froot, + (SELECT fpid FROM forumpost + WHERE froot=x.froot + AND fpid NOT IN private + ORDER BY fmtime DESC LIMIT 1) + FROM forumpost AS x + WHERE fpid NOT IN private --- Ensure this table mentioned in EQP output! + GROUP BY froot + ORDER BY 1 LIMIT 26 OFFSET 5 + ) + SELECT + thread.age, + thread.duration, + thread.cnt, + blob.uuid, + substr(event.comment,instr(event.comment,':')+1) + FROM thread, blob, event + WHERE blob.rid=thread.last + AND event.objid=thread.last + ORDER BY 1; +} { + QUERY PLAN + |--CO-ROUTINE thread + | |--SCAN x USING INDEX forumthread + | |--USING ROWID SEARCH ON TABLE private FOR IN-OPERATOR + | |--CORRELATED SCALAR SUBQUERY xxxxxx + | | |--SEARCH forumpost USING COVERING INDEX forumthread (froot=?) + | | `--USING ROWID SEARCH ON TABLE private FOR IN-OPERATOR + | `--USE TEMP B-TREE FOR ORDER BY + |--SCAN thread + |--SEARCH blob USING INTEGER PRIMARY KEY (rowid=?) + `--SEARCH event USING INTEGER PRIMARY KEY (rowid=?) +} finish_test diff --git a/test/eqp2.test b/test/eqp2.test new file mode 100644 index 0000000000..3c634fc28a --- /dev/null +++ b/test/eqp2.test @@ -0,0 +1,49 @@ +# 2024 March 20 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +set testprefix eqp2 + +do_execsql_test 1.0 { + CREATE TABLE t1(a, b, c, d); + CREATE INDEX i1 ON t1(a, b, c); +} + +do_eqp_test 1.1 { + SELECT * FROM t1 ORDER BY a, b, c +} { + QUERY PLAN + `--SCAN t1 USING INDEX i1 +} + + +do_eqp_test 1.2 { + SELECT * FROM t1 ORDER BY a, b, +c +} { + QUERY PLAN + |--SCAN t1 USING INDEX i1 + `--USE TEMP B-TREE FOR LAST TERM OF ORDER BY +} + +do_eqp_test 1.3 { + SELECT * FROM t1 ORDER BY a, +b, +c +} { + QUERY PLAN + |--SCAN t1 USING INDEX i1 + `--USE TEMP B-TREE FOR LAST 2 TERMS OF ORDER BY +} + +finish_test + + diff --git a/test/errmsg.test b/test/errmsg.test index 8df8a704f4..f32da59a39 100644 --- a/test/errmsg.test +++ b/test/errmsg.test @@ -56,7 +56,7 @@ db func sql_error sql_error do_test 1.1 { error_messages "SELECT sql_error('custom message')" } [list {*}{ - SQLITE_ERROR {SQL logic error or missing database} + SQLITE_ERROR {SQL logic error} SQLITE_ERROR {custom message} }] do_test 1.2 { @@ -77,7 +77,7 @@ do_execsql_test 2.1 { do_test 2.2 { error_messages "INSERT INTO t1 VALUES('ghi', 'def')" } [list {*}{ - SQLITE_ERROR {SQL logic error or missing database} + SQLITE_ERROR {SQL logic error} SQLITE_CONSTRAINT {UNIQUE constraint failed: t1.b} }] verify_ex_errcode 2.2b SQLITE_CONSTRAINT_UNIQUE @@ -101,7 +101,7 @@ do_execsql_test 3.1.1 { do_test 3.1.2 { error_messages "SELECT a FROM t2" "DROP TABLE t2" } [list {*}{ - SQLITE_ERROR {SQL logic error or missing database} + SQLITE_ERROR {SQL logic error} SQLITE_SCHEMA {database schema has changed} }] do_execsql_test 3.2.1 { diff --git a/test/errofst1.test b/test/errofst1.test new file mode 100644 index 0000000000..f8876316e5 --- /dev/null +++ b/test/errofst1.test @@ -0,0 +1,31 @@ +# 2024-11-20 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# Test cases for sqlite3_error_offset() +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +do_execsql_test errofst1-1.1 { + CREATE TABLE t1 as select 1 as aa; + CREATE VIEW t2 AS + WITH t3 AS (SELECT 1 FROM t1 AS bb, t1 AS cc WHERE cc.aa <= sts.aa) + SELECT 1 FROM t3 AS dd; +} +do_catchsql_test errofst1-1.2 { + SELECT * FROM t2; +} {1 {no such column: sts.aa}} +do_test errofst1-1.3 { + sqlite3_error_offset db +} {-1} + +finish_test diff --git a/test/eval.test b/test/eval.test index 360d158f3c..159e8754dc 100644 --- a/test/eval.test +++ b/test/eval.test @@ -81,7 +81,7 @@ do_test eval-3.1 { } {1 {} 102 2 {} 103 3 {} 104 4 {} 105} do_test eval-4.1 { - execsql { SELECT test_eval('SELECT "abcdefghij"') } + execsql { SELECT test_eval('SELECT ''abcdefghij''') } } {abcdefghij} finish_test diff --git a/test/exclusive.test b/test/exclusive.test index c000dfefa4..494ede7f76 100644 --- a/test/exclusive.test +++ b/test/exclusive.test @@ -252,7 +252,9 @@ db2 close # opens the journal file for exclusive access, preventing its contents # from being inspected externally. # -if {$tcl_platform(platform) != "windows"} { +if {$tcl_platform(platform) != "windows" + && [atomic_batch_write test.db]==0 +} { # Return a list of two booleans (either 0 or 1). The first is true # if the named file exists. The second is true only if the file @@ -391,6 +393,7 @@ do_test exclusive-4.5 { # Tests exclusive-5.X - test that statement journals are truncated # instead of deleted when in exclusive access mode. # +if {[atomic_batch_write test.db]==0} { # Close and reopen the database so that the temp database is no # longer active. @@ -420,9 +423,10 @@ do_test exclusive-5.0 { } {} do_test exclusive-5.1 { # Three files are open: The db, journal and statement-journal. + # (2016-03-04) The statement-journal is now opened lazily set sqlite_open_file_count expr $sqlite_open_file_count-$extrafds -} [expr 3 - ($TEMP_STORE>=2)] +} {2} do_test exclusive-5.2 { execsql { COMMIT; @@ -446,17 +450,19 @@ do_test exclusive-5.4 { INSERT INTO abc SELECT a+10, b+10, c+10 FROM abc; } # Three files are open: The db, journal and statement-journal. + # 2016-03-04: The statement-journal open is deferred set sqlite_open_file_count expr $sqlite_open_file_count-$extrafds -} [expr 3 - ($TEMP_STORE>=2)] +} {2} do_test exclusive-5.5 { execsql { COMMIT; } # Three files are still open: The db, journal and statement-journal. + # 2016-03-04: The statement-journal open is deferred set sqlite_open_file_count expr $sqlite_open_file_count-$extrafds -} [expr 3 - ($TEMP_STORE>=2)] +} {2} do_test exclusive-5.6 { execsql { PRAGMA locking_mode = normal; @@ -505,4 +511,30 @@ do_execsql_test exclusive-6.5 { SELECT * FROM sqlite_master; } {exclusive} +# 2019-12-26 ticket fb3b3024ea238d5c +if {[permutation]!="journaltest"} { + # The custom VFS used by the "journaltest" permutation cannot open the + # shared-memory file. So, while it is able to switch the db file to + # journal_mode=WAL when locking_mode=EXCLUSIVE, it can no longer access + # it once the locking_mode is changed back to NORMAL. + do_test exclusive-7.1 { + db close + forcedelete test.db test.db-journal test.db-wal + sqlite3 db test.db + # The following sequence of pragmas would trigger an assert() + # associated with Pager.changeCountDone inside of assert_pager_state(), + # prior to the fix. + db eval { + PRAGMA locking_mode = EXCLUSIVE; + PRAGMA journal_mode = WAL; + PRAGMA locking_mode = NORMAL; + PRAGMA user_version; + PRAGMA journal_mode = DELETE; + } + } {exclusive wal normal 0 delete} +} + + +} ;# atomic_batch_write==0 + finish_test diff --git a/test/exclusive2.test b/test/exclusive2.test index 712363e762..0a5d2b6eb8 100644 --- a/test/exclusive2.test +++ b/test/exclusive2.test @@ -41,7 +41,7 @@ sqlite3_soft_heap_limit 0 proc pagerChangeCounter {filename new {fd ""}} { if {$fd==""} { set fd [open $filename RDWR] - fconfigure $fd -translation binary -encoding binary + fconfigure $fd -translation binary set needClose 1 } else { set needClose 0 @@ -70,7 +70,7 @@ proc pagerChangeCounter {filename new {fd ""}} { proc readPagerChangeCounter {filename} { set fd [open $filename RDONLY] - fconfigure $fd -translation binary -encoding binary + fconfigure $fd -translation binary seek $fd 24 foreach {a b c d} [list 0 0 0 0] {} @@ -121,13 +121,13 @@ do_test exclusive2-1.1 { execsql { BEGIN; CREATE TABLE t1(a, b); - INSERT INTO t1(a) VALUES(randstr(10, 400)); - INSERT INTO t1(a) VALUES(randstr(10, 400)); - INSERT INTO t1(a) SELECT randstr(10, 400) FROM t1; - INSERT INTO t1(a) SELECT randstr(10, 400) FROM t1; - INSERT INTO t1(a) SELECT randstr(10, 400) FROM t1; - INSERT INTO t1(a) SELECT randstr(10, 400) FROM t1; - INSERT INTO t1(a) SELECT randstr(10, 400) FROM t1; + INSERT INTO t1(a, b) VALUES(randstr(10, 400), 0); + INSERT INTO t1(a, b) VALUES(randstr(10, 400), 0); + INSERT INTO t1(a, b) SELECT randstr(10, 400), 0 FROM t1; + INSERT INTO t1(a, b) SELECT randstr(10, 400), 0 FROM t1; + INSERT INTO t1(a, b) SELECT randstr(10, 400), 0 FROM t1; + INSERT INTO t1(a, b) SELECT randstr(10, 400), 0 FROM t1; + INSERT INTO t1(a, b) SELECT randstr(10, 400), 0 FROM t1; COMMIT; SELECT count(*) FROM t1; } @@ -154,7 +154,7 @@ do_test exclusive2-1.4 { } $::sig do_test exclusive2-1.5 { execsql { - UPDATE t1 SET b=a, a=NULL; + UPDATE t1 SET b=a, a=0; } db2 expr {[t1sig db2] eq $::sig} } 0 diff --git a/test/exists.test b/test/exists.test index fb73797d29..5f9e36eb00 100644 --- a/test/exists.test +++ b/test/exists.test @@ -19,6 +19,7 @@ source $testdir/lock_common.tcl foreach jm {rollback wal} { + if {![wal_is_capable] && $jm=="wal"} continue set testprefix exists-$jm diff --git a/test/existsexpr.test b/test/existsexpr.test new file mode 100644 index 0000000000..28029359b8 --- /dev/null +++ b/test/existsexpr.test @@ -0,0 +1,517 @@ +# 2024 May 25 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/lock_common.tcl +set testprefix existsexpr + +do_execsql_test 1.0 { + CREATE TABLE x1(a, b, PRIMARY KEY(a)) WITHOUT ROWID; + INSERT INTO x1 VALUES(1, 2), (3, 4), (5, 6); + CREATE INDEX x1b ON x1(b); + + CREATE TABLE x2(x, y); + INSERT INTO x2 VALUES(1, 2), (3, 4), (5, 6); +} + +do_execsql_test 1.1 { + SELECT 1 WHERE EXISTS (SELECT 1 FROM x1 WHERE a=5) +} {1} + +do_execsql_test 1.2 { + SELECT * FROM x2 WHERE EXISTS (SELECT 1 FROM x1 WHERE a=x) +} {1 2 3 4 5 6} + +# With "a=x", the UNIQUE index means the EXIST can be transformed to a join. +# So no "SUBQUERY". With "b=x", the index is not UNIQUE and so there is a +# "SUBQUERY". +do_execsql_test 1.3.1 { + EXPLAIN QUERY PLAN + SELECT * FROM x2 WHERE EXISTS (SELECT 1 FROM x1 WHERE a=x) +} {~/SUBQUERY/} +do_execsql_test 1.3.2 { + EXPLAIN QUERY PLAN + SELECT * FROM x2 WHERE EXISTS (SELECT 1 FROM x1 WHERE b=x) +} {~/SUBQUERY/} + +do_execsql_test 1.4.1 { + EXPLAIN QUERY PLAN + SELECT * FROM x2 WHERE x=1 AND EXISTS (SELECT 1 FROM x1 WHERE a=x) +} {~/SUBQUERY/} +do_execsql_test 1.4.2 { + EXPLAIN QUERY PLAN + SELECT * FROM x2 WHERE EXISTS (SELECT 1 FROM x1 WHERE a=x) AND y=2 +} {~/SUBQUERY/} + +do_execsql_test 1.5 { + SELECT count(*) FROM x2 WHERE EXISTS (SELECT 1 FROM x1 WHERE a=x) +} {3} + +#------------------------------------------------------------------------- +do_execsql_test 2.0 { + CREATE TABLE t1(a, b); + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<1000 + ) INSERT INTO t1 SELECT i, i FROM s; + + CREATE TABLE t2(c, d); + WITH s(i) AS ( + SELECT 10 UNION ALL SELECT i+10 FROM s WHERE i<1000 + ) INSERT INTO t2 SELECT i, i FROM s; +} + +do_execsql_test 2.1 { + SELECT count(*) FROM t1; + SELECT count(*) FROM t2; +} {1000 100} + +do_execsql_test 2.2 { + SELECT count(*) FROM t1, t2 WHERE a=c; +} {100} + +do_execsql_test 2.3 { + SELECT count(*) FROM t1 WHERE EXISTS (SELECT 1 FROM t2 WHERE c=a) +} {100} +do_eqp_test 2.4 { + SELECT count(*) FROM t1 WHERE EXISTS (SELECT 1 FROM t2 WHERE c=a) +} {SCAN t1} + +do_execsql_test 2.4.0 { + CREATE UNIQUE INDEX t2c ON t2(c); + CREATE UNIQUE INDEX t1a ON t1(a); +} + +do_eqp_test 2.4.1 { + SELECT count(*) FROM t1 WHERE EXISTS (SELECT 1 FROM t2 WHERE c=a); +} {SCAN t1*t2 EXISTS} +do_execsql_test 2.4.2 { + ANALYZE; +} +do_eqp_test 2.4.3 { + SELECT count(*) FROM t1 WHERE EXISTS (SELECT 1 FROM t2 WHERE c=a); +} {SCAN t1*t2 EXISTS} +do_execsql_test 2.4.4 { + SELECT count(*) FROM t1 WHERE EXISTS (SELECT 1 FROM t2 WHERE c=a); +} {100} + +do_execsql_test 2.5.1 { + EXPLAIN QUERY PLAN + SELECT count(*) FROM t1 WHERE EXISTS (SELECT 1 FROM t2 WHERE t2.rowid=a); +} {~/SUBQUERY/} + +#------------------------------------------------------------------------- +proc do_subquery_test {tn bSub sql res} { + set r1(0) ~/SUBQUERY/ + set r1(1) /SUBQUERY/ + do_execsql_test $tn.1 "explain query plan $sql" $r1($bSub) + do_execsql_test $tn.2 $sql $res +} + +do_execsql_test 3.0 { + CREATE TABLE y1(a, b, c); + CREATE TABLE y2(x, y, z); + CREATE UNIQUE INDEX y2zy ON y2(z, y); + + INSERT INTO y1 VALUES(1, 1, 1); + INSERT INTO y1 VALUES(2, 2, 2); + INSERT INTO y1 VALUES(3, 3, 3); + INSERT INTO y1 VALUES(4, 4, 4); + + INSERT INTO y2 VALUES(1, 1, 1); + INSERT INTO y2 VALUES(3, 3, 3); +} + +do_subquery_test 3.1 0 { + SELECT * FROM y1 WHERE EXISTS ( + SELECT 1 FROM y2 WHERE z=a AND y=b AND x=z + ) +} { + 1 1 1 3 3 3 +} + +do_subquery_test 3.2 0 { + SELECT * FROM y1 WHERE EXISTS ( + SELECT 1 FROM y2 WHERE z=max(a,b) AND y=min(b,a) AND x=z + ) +} { + 1 1 1 3 3 3 +} + +do_subquery_test 3.3 0 { + SELECT * FROM y1 WHERE EXISTS ( + SELECT 1 FROM y2 WHERE z=max(a,b) AND y=min(b,a) AND c!=3 + ) +} { + 1 1 1 +} + +do_subquery_test 3.4 0 { + SELECT * FROM y1 WHERE EXISTS ( + SELECT 1 FROM y2 WHERE z=max(a,b) AND b=3 + ) +} { + 3 3 3 +} + +do_subquery_test 3.5 0 { + SELECT * FROM y1 WHERE EXISTS ( + SELECT 1 FROM y2 WHERE z=a-1 AND y=a-1 + ) +} { + 2 2 2 + 4 4 4 +} + +do_subquery_test 3.6 0 { + SELECT * FROM y1 WHERE EXISTS ( + SELECT 1 FROM y2 WHERE z=a-1 AND y+1=a + ) +} { + 2 2 2 + 4 4 4 +} + +do_subquery_test 3.7 1 { + SELECT * FROM y1 WHERE EXISTS ( + SELECT count(*) FROM y2 WHERE z=a-1 AND y=a-1 + ) +} { + 1 1 1 + 2 2 2 + 3 3 3 + 4 4 4 +} + +do_subquery_test 3.8 0 { + SELECT * FROM y1 WHERE EXISTS ( SELECT a+1 FROM y2 ) +} { + 1 1 1 + 2 2 2 + 3 3 3 + 4 4 4 +} + +do_subquery_test 3.9 1 { + SELECT * FROM y1 WHERE EXISTS ( + SELECT 1 FROM y2 one, y2 two WHERE one.z=a-1 AND one.y=a-1 + ) +} { + 2 2 2 + 4 4 4 +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 4.0 { + CREATE TABLE tx1(a TEXT COLLATE nocase, b TEXT); + CREATE UNIQUE INDEX tx1ab ON tx1(a, b); + + INSERT INTO tx1 VALUES('a', 'a'); + INSERT INTO tx1 VALUES('B', 'b'); + INSERT INTO tx1 VALUES('c', 'c'); + INSERT INTO tx1 VALUES('D', 'd'); + INSERT INTO tx1 VALUES('e', 'e'); + + CREATE TABLE tx2(x, y); + INSERT INTO tx2 VALUES('A', 'a'); + INSERT INTO tx2 VALUES('b', 'b'); + INSERT INTO tx2 VALUES('C', 'c'); + INSERT INTO tx2 VALUES('D', 'd'); +} + +do_subquery_test 4.1 0 { + SELECT * FROM tx2 WHERE EXISTS ( + SELECT 1 FROM tx1 WHERE a=x AND b=y + ) +} { + A a + b b + C c + D d +} + +do_subquery_test 4.1.1 0 { + SELECT * FROM tx2 WHERE EXISTS ( + SELECT 1 FROM tx1 WHERE (a COLLATE nocase)=x AND b=y + ) +} { + A a b b C c D d +} +do_subquery_test 4.1.2 0 { + SELECT * FROM tx2 WHERE EXISTS ( + SELECT 1 FROM tx1 WHERE a=x AND (b COLLATE binary)=y + ) +} { + A a b b C c D d +} +do_subquery_test 4.1.1 0 { + SELECT * FROM tx2 WHERE EXISTS ( + SELECT 1 FROM tx1 WHERE x=(a COLLATE nocase) AND b=y + ) +} { + A a b b C c D d +} +do_subquery_test 4.1.2 0 { + SELECT * FROM tx2 WHERE EXISTS ( + SELECT 1 FROM tx1 WHERE a=x AND y=(b COLLATE binary) + ) +} { + A a b b C c D d +} + +do_subquery_test 4.2 0 { + SELECT * FROM tx2 WHERE EXISTS ( + SELECT 1 FROM tx1 WHERE a=x AND b=y COLLATE nocase + ) +} { + A a + b b + C c + D d +} + +do_execsql_test 4.3 { + DROP INDEX tx1ab; + CREATE UNIQUE INDEX tx1ab ON tx1(a COLLATE binary, b); +} + +do_subquery_test 4.4 0 { + SELECT * FROM tx2 WHERE EXISTS ( + SELECT 1 FROM tx1 WHERE a=x AND b=y + ) +} { + A a + b b + C c + D d +} + +do_subquery_test 4.4 0 { + SELECT * FROM tx2 WHERE EXISTS ( + SELECT 1 FROM tx1 WHERE a=x COLLATE binary AND b=y + ) +} { + D d +} + +do_subquery_test 4.4 1 { + SELECT EXISTS ( SELECT x FROM tx1 ) FROM tx2 +} { + 1 1 1 1 +} + +do_subquery_test 4.4 1 { + SELECT (SELECT EXISTS ( SELECT x FROM tx1 ) WHERE 1) FROM tx2 +} { + 1 1 1 1 +} + +#------------------------------------------------------------------------- +proc cols {s f} { + set lCols [list] + for {set i $s} {$i<=$f} {incr i} { + lappend lCols [format "c%02d" $i] + } + join $lCols ", " +} +proc vals {n val} { + set lVal [list] + for {set i 0} {$i<$n} {incr i} { + lappend lVal $val + } + join $lVal ", " +} +proc exprs {s f} { + set lExpr [list] + for {set i $s} {$i<=$f} {incr i} { + lappend lExpr [format "c%02d = o" $i] + } + join $lExpr " AND " +} + + +do_execsql_test 5.0 " + CREATE TABLE a1( [cols 0 99] ); +" +do_execsql_test 5.1 " + -- 63 column index + CREATE UNIQUE INDEX a1idx1 ON a1( [cols 0 62] ); +" +do_execsql_test 5.2 " + -- 64 column index + CREATE UNIQUE INDEX a1idx2 ON a1( [cols 10 73] ); +" +do_execsql_test 5.2 " + -- 65 column index + CREATE UNIQUE INDEX a1idx3 ON a1( [cols 20 84] ); +" + +do_test 5.3 { + foreach v {1 2 3 4 5 6} { + execsql "INSERT INTO a1 VALUES( [vals 100 $v] )" + } +} {} + +do_execsql_test 5.4 { + CREATE TABLE a2(o); + INSERT INTO a2 VALUES(2), (5); +} + +do_subquery_test 5.5 0 " + SELECT o FROM a2 WHERE EXISTS ( + SELECT 1 FROM a1 WHERE [exprs 0 62] + ) +" { + 2 5 +} + +do_subquery_test 5.6 0 " + SELECT o FROM a2 WHERE EXISTS ( + SELECT 1 FROM a1 WHERE [exprs 10 73] + ) +" { + 2 5 +} + +do_subquery_test 5.7 0 " + SELECT o FROM a2 WHERE EXISTS ( + SELECT 1 FROM a1 WHERE [exprs 20 84] + ) +" { + 2 5 +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 6.0 { + CREATE TABLE t1(a, b UNIQUE, c UNIQUE); + CREATE TABLE t2(a INfEGER PRIMARY KEY, b); + CREATE UNIQUE INDEX t2b ON t2(b); +} + +do_catchsql_test 6.1 { + SELECT a FROM t1 WHERE EXISTS (SELECT 1 FROM t2 WHERE c COLLATE f = a) +} {1 {no such collation sequence: f}} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 7.0 { + CREATE TABLE t1(x); + CREATE TABLE t2(y UNIQUE); + + INSERT INTO t1 VALUES(1), (2); + INSERT INTO t2 VALUES(1), (3); + + SELECT * FROM t1 one LEFT JOIN t1 two ON (one.x=two.x AND EXISTS ( + SELECT 1 FROM t2 WHERE y=one.x + )); +} { + 1 1 + 2 {} +} + +# https://sqlite.org/forum/forumpost/2025-07-23T10:59:14z +reset_db +do_execsql_test 8.0 { + CREATE TABLE t0 (c0 INT); INSERT INTO t0(c0) VALUES (1); + CREATE TABLE t1(c0 INT); INSERT INTO t1(c0) VALUES (2); + SELECT * FROM t1 WHERE EXISTS (SELECT 1 FROM t0 LIMIT 0); +} {} + +#------------------------------------------------------------------------- +reset_db + +do_execsql_test 9.0 { + CREATE TABLE t1(xx); + INSERT INTO t1 VALUES('big string value'); +} {} + +do_execsql_test 9.1 { + PRAGMA automatic_index = off; + CREATE TABLE t2(ii); + INSERT INTO t2 VALUES(100); + INSERT INTO t2 VALUES(200); +} + +do_execsql_test 9.2 { + CREATE TABLE t3(yy); + INSERT INTO t3 VALUES(200); +} + +do_execsql_test 9.3 { + SELECT 1 FROM t2 WHERE EXISTS ( SELECT 1 FROM t3 WHERE yy==t2.ii ) +} {1} + +do_execsql_test 9.4 { + SELECT EXISTS ( + SELECT 1 FROM t2 WHERE EXISTS ( SELECT 1 FROM t3 WHERE yy==t2.ii ) + ) +} {1} + +do_execsql_test 9.5 { + SELECT 1234 WHERE EXISTS ( + SELECT 1 FROM t2 WHERE EXISTS ( SELECT 1 FROM t3 WHERE yy==t2.ii ) + ) +} {1234} + +set Q { + SELECT * FROM t1 WHERE + EXISTS ( + SELECT 1 FROM t2 WHERE EXISTS ( SELECT 1 FROM t3 WHERE yy==t2.ii ) + ) +} + +do_execsql_test 9.5 $Q {{big string value}} +catch { optimization_control db exists-to-join 0 } +db cache flush +do_execsql_test 9.6 $Q {{big string value}} + +#------------------------------------------------------------------------- +reset_db + +do_execsql_test 10.0 { + CREATE TABLE t1(a); + CREATE TABLE x1(x); +} + +do_execsql_test 10.1 { + SELECT EXISTS( SELECT 1 FROM t1 ) aaa FROM x1 WHERE aaa AND aaa +} + +do_execsql_test 10.2 { + SELECT + EXISTS( SELECT 1 FROM t1 ) aaa + WHERE ( + SELECT 1 FROM x1 WHERE aaa AND aaa + ); +} + +# https://sqlite.org/forum/forumpost/2026-01-03T14:05:48z +do_execsql_test 11.0 { + CREATE TABLE parent (id TEXT PRIMARY KEY); + CREATE TABLE child_a (id TEXT); + CREATE TABLE child_b (id TEXT); + INSERT INTO parent VALUES ('p1'); + INSERT INTO child_a VALUES ('p1'); +} +do_execsql_test 11.1 { + SELECT count(*), parent.id FROM parent + WHERE EXISTS ( + SELECT 1 FROM child_a WHERE child_a.id = parent.id + UNION + SELECT 1 FROM child_b WHERE child_b.id = parent.id + ) + GROUP BY id; +} {1 p1} + +finish_test diff --git a/test/existsexpr2.test b/test/existsexpr2.test new file mode 100644 index 0000000000..f7644bf802 --- /dev/null +++ b/test/existsexpr2.test @@ -0,0 +1,96 @@ +# 2024 June 14 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/lock_common.tcl +set testprefix existsexpr2 + + +do_execsql_test 1.0 { + CREATE TABLE x1(a, b, PRIMARY KEY(a)) WITHOUT ROWID; + INSERT INTO x1 VALUES(1, 2), (3, 4), (5, 6); + CREATE INDEX x1b ON x1(b); + + CREATE TABLE x2(x, y); + INSERT INTO x2 VALUES(1, 2), (3, 4), (5, 6); +} + +do_execsql_test 1.1 { + SELECT * FROM x1 WHERE EXISTS (SELECT 1 FROM x2 WHERE a!=123) +} {1 2 3 4 5 6} + +do_execsql_test 1.2 { + CREATE TABLE x3(u, v); + CREATE INDEX x3u ON x3(u); + INSERT INTO x3 VALUES + (1, 1), (1, 2), (1, 3), + (2, 1), (2, 2), (2, 3); +} + +do_execsql_test 1.3 { + SELECT * FROM x1 WHERE EXISTS ( + SELECT 1 FROM x3 WHERE u IN (1, 2, 3, 4) AND v=b + ); +} { + 1 2 +} + +#------------------------------------------------------------------------- +# +reset_db +do_execsql_test 2.0 { + CREATE TABLE t1(a, b, c); + CREATE INDEX t1ab ON t1(a,b); + + INSERT INTO t1 VALUES + ('abc', 1, 1), + ('abc', 2, 2), + ('abc', 2, 3), + + ('def', 1, 1), + ('def', 2, 2), + ('def', 2, 3); + + CREATE TABLE t2(x, y); + INSERT INTO t2 VALUES(1, 1), (2, 2), (3, 3); + + ANALYZE; + DELETE FROM sqlite_stat1; + INSERT INTO sqlite_stat1 VALUES('t1','t1ab','10000 5000 2'); + ANALYZE sqlite_master; +} + + +do_execsql_test 2.1 { + SELECT a,b,c FROM t1 WHERE b=2 ORDER BY a +} { + abc 2 2 + abc 2 3 + def 2 2 + def 2 3 +} + +do_execsql_test 2.2 { + SELECT x, y FROM t2 WHERE EXISTS ( + SELECT 1 FROM t1 WHERE b=x + ) +} { + 1 1 + 2 2 +} + + + +finish_test + + diff --git a/test/existsfault.test b/test/existsfault.test new file mode 100644 index 0000000000..4b335d84cd --- /dev/null +++ b/test/existsfault.test @@ -0,0 +1,49 @@ +# 2024 May 25 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/lock_common.tcl +source $testdir/malloc_common.tcl +set testprefix existsfault + +db close +sqlite3_shutdown +sqlite3_config_lookaside 0 0 +sqlite3_initialize +autoinstall_test_functions +sqlite3 db test.db + +do_execsql_test 1.0 { + CREATE TABLE x1(a, b); + INSERT INTO x1 VALUES(1, 2), (3, 4), (5, 6); + CREATE UNIQUE INDEX x1a ON x1(a); + CREATE INDEX x1b ON x1(b); + + CREATE TABLE x2(x, y); + INSERT INTO x2 VALUES(1, 2), (3, 4), (5, 6); +} + +do_faultsim_test 1 -faults oom* -prep { + sqlite3 db test.db + execsql { SELECT * FROM sqlite_schema } +} -body { + execsql { + SELECT count(*) FROM x2 WHERE EXISTS (SELECT 1 FROM x1 WHERE a=x) AND y!=11 + } +} -test { + faultsim_test_result {0 3} +} + +finish_test + + diff --git a/test/expr.test b/test/expr.test index 7d7b8ce5a7..4f739e2e7d 100644 --- a/test/expr.test +++ b/test/expr.test @@ -71,6 +71,9 @@ test_expr expr-1.28 {i1=1, i2=2} {i1=2 AND i2=1} {0} test_expr expr-1.29 {i1=1, i2=2} {i1=1 AND i2=1} {0} test_expr expr-1.30 {i1=1, i2=2} {i1=2 AND i2=2} {0} test_expr expr-1.31 {i1=1, i2=2} {i1==1 OR i2=2} {1} +test_expr expr-1.31b {i1=1} {0 OR 2} {1} +test_expr expr-1.31c {i1=1} {false OR true} {1} +test_expr expr-1.31d {i1=1} {99 OR false} {1} test_expr expr-1.32 {i1=1, i2=2} {i1=2 OR i2=1} {0} test_expr expr-1.33 {i1=1, i2=2} {i1=1 OR i2=1} {1} test_expr expr-1.34 {i1=1, i2=2} {i1=2 OR i2=2} {1} @@ -181,29 +184,53 @@ if {[working_64bit_int]} { } test_expr expr-1.111 {i1=NULL, i2=8} {i1 IS i2} 0 +test_expr expr-1.111b {i1=NULL, i2=8} {i1 IS NOT DISTINCT FROM i2} 0 test_expr expr-1.112 {i1=NULL, i2=NULL} {i1 IS i2} 1 +test_expr expr-1.112b {i1=NULL, i2=NULL} {i1 IS NOT DISTINCT FROM i2} 1 test_expr expr-1.113 {i1=6, i2=NULL} {i1 IS i2} 0 +test_expr expr-1.113b {i1=6, i2=NULL} {i1 IS NOT DISTINCT FROM i2} 0 test_expr expr-1.114 {i1=6, i2=6} {i1 IS i2} 1 +test_expr expr-1.114b {i1=6, i2=6} {i1 IS NOT DISTINCT FROM i2} 1 test_expr expr-1.115 {i1=NULL, i2=8} \ {CASE WHEN i1 IS i2 THEN 'yes' ELSE 'no' END} no +test_expr expr-1.115b {i1=NULL, i2=8} \ + {CASE WHEN i1 IS NOT DISTINCT FROM i2 THEN 'yes' ELSE 'no' END} no test_expr expr-1.116 {i1=NULL, i2=NULL} \ {CASE WHEN i1 IS i2 THEN 'yes' ELSE 'no' END} yes +test_expr expr-1.116b {i1=NULL, i2=NULL} \ + {CASE WHEN i1 IS NOT DISTINCT FROM i2 THEN 'yes' ELSE 'no' END} yes test_expr expr-1.117 {i1=6, i2=NULL} \ {CASE WHEN i1 IS i2 THEN 'yes' ELSE 'no' END} no +test_expr expr-1.117b {i1=6, i2=NULL} \ + {CASE WHEN i1 IS NOT DISTINCT FROM i2 THEN 'yes' ELSE 'no' END} no test_expr expr-1.118 {i1=8, i2=8} \ {CASE WHEN i1 IS i2 THEN 'yes' ELSE 'no' END} yes +test_expr expr-1.118b {i1=8, i2=8} \ + {CASE WHEN i1 IS NOT DISTINCT FROM i2 THEN 'yes' ELSE 'no' END} yes test_expr expr-1.119 {i1=NULL, i2=8} {i1 IS NOT i2} 1 +test_expr expr-1.119b {i1=NULL, i2=8} {i1 IS DISTINCT FROM i2} 1 test_expr expr-1.120 {i1=NULL, i2=NULL} {i1 IS NOT i2} 0 +test_expr expr-1.120b {i1=NULL, i2=NULL} {i1 IS DISTINCT FROM i2} 0 test_expr expr-1.121 {i1=6, i2=NULL} {i1 IS NOT i2} 1 +test_expr expr-1.121b {i1=6, i2=NULL} {i1 IS DISTINCT FROM i2} 1 test_expr expr-1.122 {i1=6, i2=6} {i1 IS NOT i2} 0 +test_expr expr-1.122b {i1=6, i2=6} {i1 IS DISTINCT FROM i2} 0 test_expr expr-1.123 {i1=NULL, i2=8} \ {CASE WHEN i1 IS NOT i2 THEN 'yes' ELSE 'no' END} yes +test_expr expr-1.123b {i1=NULL, i2=8} \ + {CASE WHEN i1 IS DISTINCT FROM i2 THEN 'yes' ELSE 'no' END} yes test_expr expr-1.124 {i1=NULL, i2=NULL} \ {CASE WHEN i1 IS NOT i2 THEN 'yes' ELSE 'no' END} no +test_expr expr-1.124b {i1=NULL, i2=NULL} \ + {CASE WHEN i1 IS DISTINCT FROM i2 THEN 'yes' ELSE 'no' END} no test_expr expr-1.125 {i1=6, i2=NULL} \ {CASE WHEN i1 IS NOT i2 THEN 'yes' ELSE 'no' END} yes +test_expr expr-1.125b {i1=6, i2=NULL} \ + {CASE WHEN i1 IS DISTINCT FROM i2 THEN 'yes' ELSE 'no' END} yes test_expr expr-1.126 {i1=8, i2=8} \ {CASE WHEN i1 IS NOT i2 THEN 'yes' ELSE 'no' END} no +test_expr expr-1.126b {i1=8, i2=8} \ + {CASE WHEN i1 IS DISTINCT FROM i2 THEN 'yes' ELSE 'no' END} no do_catchsql_test expr-1.127 { SELECT 1 IS #1; @@ -308,6 +335,33 @@ ifcapable floatingpoint {if {[working_64bit_int]} { test_realnum_expr expr-1.257\ {i1=-4294967296, i2=-2147483647} {i1*i2} 9223372032559808512 + test_realnum_expr expr-1.260\ + {i1=3037000500, i2=3037000500} {i1*i2} 9.22337203700025e+18 + test_realnum_expr expr-1.261\ + {i1=3037000500, i2=-3037000500} {i1*i2} -9.22337203700025e+18 + test_realnum_expr expr-1.262\ + {i1=-3037000500, i2=3037000500} {i1*i2} -9.22337203700025e+18 + test_realnum_expr expr-1.263\ + {i1=-3037000500, i2=-3037000500} {i1*i2} 9.22337203700025e+18 + + test_realnum_expr expr-1.264\ + {i1=3037000500, i2=3037000499} {i1*i2} 9223372033963249500 + test_realnum_expr expr-1.265\ + {i1=3037000500, i2=-3037000499} {i1*i2} -9223372033963249500 + test_realnum_expr expr-1.266\ + {i1=-3037000500, i2=3037000499} {i1*i2} -9223372033963249500 + test_realnum_expr expr-1.267\ + {i1=-3037000500, i2=-3037000499} {i1*i2} 9223372033963249500 + + test_realnum_expr expr-1.268\ + {i1=3037000499, i2=3037000500} {i1*i2} 9223372033963249500 + test_realnum_expr expr-1.269\ + {i1=3037000499, i2=-3037000500} {i1*i2} -9223372033963249500 + test_realnum_expr expr-1.270\ + {i1=-3037000499, i2=3037000500} {i1*i2} -9223372033963249500 + test_realnum_expr expr-1.271\ + {i1=-3037000499, i2=-3037000500} {i1*i2} 9223372033963249500 + }} ifcapable floatingpoint { @@ -943,6 +997,7 @@ do_realnum_test expr-13.7 { } } {9.22337203685478e+18} +sqlite3_db_config db SQLITE_DBCONFIG_DQS_DML 1 do_execsql_test expr-13.8 { SELECT "" <= ''; } {1} @@ -950,6 +1005,83 @@ do_execsql_test expr-13.9 { SELECT '' <= ""; } {1} +# 2018-02-26. Ticket https://sqlite.org/src/tktview/36fae083b450e3af85 +# +do_execsql_test expr-14.1 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(x); + INSERT INTO t1 VALUES(0),(1),(NULL),(0.5),('1x'),('0x'); + SELECT count(*) FROM t1 + WHERE (x OR (8==9)) != (CASE WHEN x THEN 1 ELSE 0 END); +} {0} +do_execsql_test expr-14.2 { + SELECT count(*) FROM t1 + WHERE (x OR (8==9)) != (NOT NOT x); +} {0} +do_execsql_test expr-14.3 { + SELECT sum(NOT x) FROM t1 + WHERE x +} {0} +do_execsql_test expr-14.4 { + SELECT sum(CASE WHEN x THEN 0 ELSE 1 END) FROM t1 + WHERE x +} {0} + + +foreach {tn val} [list 1 NaN 2 -NaN 3 NaN0 4 -NaN0 5 Inf 6 -Inf] { + do_execsql_test expr-15.$tn.1 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(x); + INSERT INTO t1 VALUES(0),(1),(NULL),(0.5),('1x'),('0x'); + } + + do_test expr-15.$tn.2 { + set ::STMT [sqlite3_prepare db "INSERT INTO t1 VALUES(?)" -1 TAIL] + sqlite3_bind_double $::STMT 1 $val + sqlite3_step $::STMT + sqlite3_reset $::STMT + sqlite3_finalize $::STMT + } {SQLITE_OK} + do_execsql_test expr-15.$tn.3 { + SELECT count(*) FROM t1 + WHERE (x OR (8==9)) != (CASE WHEN x THEN 1 ELSE 0 END); + } {0} + + do_execsql_test expr-15.$tn.4 { + SELECT count(*) FROM t1 + WHERE (x OR (8==9)) != (NOT NOT x); + } {0} + + do_execsql_test expr-15.$tn.5 { + SELECT sum(NOT x) FROM t1 + WHERE x + } {0} + + do_execsql_test expr-15.$tn.6 { + SELECT sum(CASE WHEN x THEN 0 ELSE 1 END) FROM t1 + WHERE x + } {0} +} + +reset_db +sqlite3_test_control SQLITE_TESTCTRL_INTERNAL_FUNCTIONS db +do_execsql_test expr-16.1 { + CREATE TABLE t1(a,b,c); + CREATE TABLE dual(dummy); + INSERT INTO dual VALUES('X'); +} {} +do_execsql_test expr-16.100 { + SELECT implies_nonnull_row( (b=1 AND 0)>(b=3 AND 0),a) + FROM dual LEFT JOIN t1; +} {0} +do_execsql_test expr-16.101 { + SELECT implies_nonnull_row( (b=1 AND 0)>(b=3 AND a=4),a) + FROM dual LEFT JOIN t1; +} {1} +do_execsql_test expr-16.102 { + SELECT implies_nonnull_row( (b=1 AND a=2)>(b=3 AND a=4),a) + FROM dual LEFT JOIN t1; +} {1} finish_test diff --git a/test/expr2.test b/test/expr2.test new file mode 100644 index 0000000000..3dfbadcd90 --- /dev/null +++ b/test/expr2.test @@ -0,0 +1,54 @@ +# 2019 May 20 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is testing expressions. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix expr2 + +do_execsql_test 1.0 { + CREATE TABLE t0(c0); + INSERT INTO t0(c0) VALUES ('val'); +} + +do_execsql_test 1.1 { + SELECT * FROM t0 WHERE ( + ( (0 IS NOT FALSE) OR NOT (0 IS FALSE OR (t0.c0 = 1)) ) IS 0 + ) +} {val} + +do_execsql_test 1.2.1 { + SELECT + ( (0 IS NOT FALSE) OR NOT (0 IS FALSE OR (t0.c0 = 1)) ) IS 0 + FROM t0 +} {1} + +do_execsql_test 1.2.2 { + SELECT + ( (0 IS NOT FALSE) OR NOT (0 IS 0 OR (t0.c0 = 1)) ) IS 0 + FROM t0 +} {1} + +do_execsql_test 1.3 { + SELECT ( (0 IS NOT FALSE) OR NOT (0 IS FALSE OR (t0.c0 = 1)) ) FROM t0 +} {0} + +do_execsql_test 1.4.1 { + SELECT (0 IS NOT FALSE) FROM t0 +} {0} +do_execsql_test 1.4.2 { + SELECT NOT (0 IS FALSE OR (t0.c0 = 1)) FROM t0 +} {0} + + +finish_test diff --git a/test/exprfault.test b/test/exprfault.test new file mode 100644 index 0000000000..f0abe9ac39 --- /dev/null +++ b/test/exprfault.test @@ -0,0 +1,45 @@ +# 2021 April 17 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix exprfault + +do_execsql_test 1.0 { + CREATE TABLE t1(a); + CREATE TABLE t2(d); +} +faultsim_save_and_close + +do_faultsim_test 1.1 -faults oom* -prep { + faultsim_restore_and_reopen +} -body { + execsql { + SELECT a = ( SELECT d FROM (SELECT d FROM t2) ) FROM t1 + } +} -test { + faultsim_test_result {0 {}} +} + +do_faultsim_test 2 -faults oom* -prep { + faultsim_restore_and_reopen +} -body { + execsql { + SELECT hex ( unhex('ABCDEF') ); + } +} -test { + faultsim_test_result {0 ABCDEF} +} + + +finish_test diff --git a/test/exprfault2.test b/test/exprfault2.test new file mode 100644 index 0000000000..acbead59f6 --- /dev/null +++ b/test/exprfault2.test @@ -0,0 +1,35 @@ +# 2024-05-11 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix exprfault2 + +do_execsql_test 1.0 { + CREATE TABLE t1(a,b,c,d,f,PRIMARY KEY(b,b)); + CREATE TABLE t2(x INT PRIMARY KEY, y, z); + CREATE TABLE t3(a,b,c,d,e,PRIMARY KEY(a,b))WITHOUT ROWID; +} +faultsim_save_and_close + + +do_faultsim_test 1 -faults oom-t* -prep { + faultsim_restore_and_reopen +} -body { + execsql { + UPDATE t3 SET (d,d,d,d, a )=(SELECT EXISTS(SELECT 1 NOT IN(SELECT EXISTS(SELECT 1 IN(SELECT max( 1 IN(SELECT x ORDER BY 1)) OVER(PARTITION BY sum((SELECT y FROM t2 UNION SELECT (SELECT max( 1 IN(SELECT x NOT IN(SELECT 1 NOT IN(SELECT EXISTS(SELECT 1 IN(SELECT max( 1 IN(SELECT x ORDER BY 1)) OVER(PARTITION BY sum((SELECT y FROM t2 UNION SELECT (SELECT max( (SELECT x NOT IN(SELECT 1 NOT IN(SELECT EXISTS(SELECT 1 IN(SELECT max( 1 IN(SELECT x ORDER BY 1)) OVER(PARTITION BY sum((SELECT y FROM t2 UNION SELECT (SELECT max( 1 IN(SELECT x ORDER BY 1)) OVER(PARTITION BY sum((SELECT y FROM t2 UNION SELECT x ORDER BY 1)))INTERSECT SELECT (SELECT 1 FROM t2 UNION SELECT d ORDER BY 1) ORDER BY 1) ORDERa)| (SELECT 1 x ORDER BY 1)))INTERSECT SELECT EXISTS(SELECT 1 FROM t2 UNION SELECT d ORDER BY 1) ORDER BY 1) a)| (SELECT 1 IN(SELECT max( 1 IN(SELECT x ORDER BY 1)) OVER(ORDER BY sum((SELECT DISTINCT y FROM t2 UNION SELECT x ORDER BY 1)))INTERSECT SELECT EXISTS(SELECT 1 FROM t2 UNION SELECT x ORDER BY 1) ORDER BY 1) z)|9 AS blob) IN(SELECT max( 1 IN(SELECT x ORDER BY 1)) OVER(PARTITION BY sum((SELECT DISTINCT y FROM t2 UNION SELECT x ORDER BY 1)))EXCEPT SELECT EXISTS(SELECT 1 FROM t2 UNION SELECT d ORDER BY 1) ORDER BY 1) z) ORDER BY 1) IN(SELECT x ORDER BY 1)) OVER(PARTITION BY sum((SELECT DISTINCT y FROM t2 UNION SELECT x ORDER BY 1)))INTERSECT SELECT (SELECT 1 FROM t2 UNION SELECT d ORDER BY 1) ORDER BY 1) ORDERa)| (SELECT 1 x ORDER BY 1)))EXCEPT SELECT EXISTS(SELECT 1 FROM t2 UNION SELECT d ORDER BY 1) ORDER BY 1) a)| (SELECT 1 IN(SELECT max( 1 IN(SELECT x ORDER BY 1)) OVER(PARTITION BY sum((SELECT DISTINCT y FROM t2 UNION SELECT x ORDER BY 1)))INTERSECT SELECT EXISTS(SELECT 1 FROM t2 UNION SELECT x ORDER BY 1) ORDER BY 1) z)|9 AS blob) IN(SELECT max( 1 IN(SELECT x ORDER BY 1)) OVER(PARTITION BY sum((SELECT DISTINCT y FROM t2 UNION SELECT x ORDER BY 1)))EXCEPT SELECT EXISTS(SELECT 1 FROM t2 UNION SELECT d ORDER BY 1) ORDER BY 1) z) ORDER BY 1)) OVER(PARTITION BY sum((SELECT y FROM t2 UNION SELECT x ORDER BY 1)))INTERSECT SELECT EXISTS(SELECT 1 FROM t2 UNION SELECT d ORDER BY 1) ORDER BY 1) ORDERa)| (SELECT 1 x ORDER BY 1)))INTERSECT SELECT EXISTS(SELECT 5 FROM t2 UNION SELECT d ORDER BY 1) ORDER BY 1) ORDERa)| (SELECT 1 IN(SELECT max( 1 IN(SELECT x ORDER BY 1)) OVER(ORDER BY sum((SELECT DISTINCT y FROM t2 UNION SELECT x ORDER BY 1)))INTERSECT SELECT EXISTS(SELECT 1 FROM t2 UNION SELECT x ORDER BY 1) ORDER BY 1) z)| 1 AS blob) IN(SELECT max( 1 IN(SELECT x ORDER BY 1)) OVER(PARTITION BY sum((SELECT DISTINCT y FROM t2 UNION SELECT x ORDER BY 1)))INTERSECT SELECT EXISTS(SELECT 1 FROM t2 UNION SELECT d ORDER BY 1) ORDER BY 1) z)| (SELECT 1 IN(SELECT max( 1 IN(SELECT c ORDER BY 1)) OVER(PARTITION BY sum((SELECT y FROM t2 UNION SELECT x ORDER BY 1)))INTERSECT SELECT (SELECT 1 FROM t2 UNION SELECT x ORDER BY 1) ORDER BY 1) e)|9 AS blob) FROM t2 WHERE a<x), e= BY 1) FROM t2 UNION SELECT 1 ORDER BY 1) ORDER BY 1)) a) FILTER (GROUP BY 1 HAVING b<= OVER(ORDER BY (SELECT max(x INPARTITION BY sum((SELECT y FROM t2 UNION SELECT x IN(SELECT 1 ORDER BY 1) ORDER 1)))INTERSECT SELECT EXISTS(SELECT 1 FROM t2 WHERE xBY 1) ORDER BY 1)) FROM77; + } +} -test { + faultsim_test_result {1 {near ")": syntax error}} +} + +finish_test diff --git a/test/extension01.test b/test/extension01.test index 97b772680e..ba8a44edb5 100644 --- a/test/extension01.test +++ b/test/extension01.test @@ -60,7 +60,7 @@ do_test 1.5 { } {0} do_test 1.6 { - if {$::tcl_platform(platform)=="unix"} { + if {$::tcl_platform(os) ne "Windows NT"} { file attributes ./file2.txt -permissions r--r--r-- } else { file attributes ./file2.txt -readonly 1 @@ -70,7 +70,7 @@ do_test 1.6 { } } {nil} do_test 1.7 { - if {$::tcl_platform(platform)=="unix"} { + if {$::tcl_platform(os) ne "Windows NT"} { file attributes ./file2.txt -permissions rw-r--r-- } else { file attributes ./file2.txt -readonly 0 diff --git a/test/external_reader.test b/test/external_reader.test new file mode 100644 index 0000000000..d56aa4e261 --- /dev/null +++ b/test/external_reader.test @@ -0,0 +1,74 @@ +# 2021 April 2 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/lock_common.tcl +set testprefix external_reader + +ifcapable !wal { + finish_test + return +} +if {$::tcl_platform(os) eq "Windows NT"} { + finish_test + return +} + +do_multiclient_test tn { + + set bExternal 1 + if {[info commands db3]!=""} { set bExternal 0 } + + do_test 1.$tn.0 { + sql1 { + PRAGMA journal_mode = wal; + CREATE TABLE t1(a, b); + INSERT INTO t1 VALUES(1, 2); + } + } {wal} + + do_test 1.$tn.1 { + sql2 { SELECT * FROM t1 } + } {1 2} + + do_test 1.$tn.2 { + code1 { + file_control_external_reader db + } + } {0} + + do_test 1.$tn.3 { + sql2 { + BEGIN; + SELECT * FROM t1; + } + } {1 2} + + do_test 1.$tn.4 { + code1 { + file_control_external_reader db + } + } $bExternal + + do_test 1.$tn.5 { + sql2 { COMMIT } + } {} + + do_test 1.$tn.6 { + code1 { file_control_external_reader db } + } 0 + +} + + +finish_test diff --git a/test/fallocate.test b/test/fallocate.test index f523c2cc4a..3e154482bc 100644 --- a/test/fallocate.test +++ b/test/fallocate.test @@ -59,7 +59,10 @@ do_test fallocate-1.6 { # do_test fallocate-1.7 { execsql { BEGIN; INSERT INTO t1 VALUES(1, 2); } - if {[permutation] != "inmemory_journal"} { + if {[permutation] != "inmemory_journal" + && [permutation] != "atomic-batch-write" + && [atomic_batch_write test.db]==0 + } { hexio_get_int [hexio_read test.db-journal 16 4] } else { set {} 1024 @@ -67,6 +70,15 @@ do_test fallocate-1.7 { } {1024} do_test fallocate-1.8 { execsql { COMMIT } } {} +do_test fallocate-1.8 { + set nPg [db one {PRAGMA page_count}] + set nFile [expr [file size test.db] / 1024] + list [expr $nPg<100] [expr $nFile>100] +} {1 1} + +do_execsql_test fallocate-1.9 { + PRAGMA max_page_count = 100; +} {100} #------------------------------------------------------------------------- # The following tests - fallocate-2.* - test that things work in WAL diff --git a/test/filectrl.test b/test/filectrl.test index 28fecee92f..9b1a1c7589 100644 --- a/test/filectrl.test +++ b/test/filectrl.test @@ -36,11 +36,39 @@ do_test filectrl-1.5 { sqlite3 db test_control_lockproxy.db file_control_lockproxy_test db [get_pwd] } {} -do_test filectrl-1.6 { - sqlite3 db test.db - set fn [file_control_tempfilename db] - set fn -} {/etilqs_/} +ifcapable !winrt { + do_test filectrl-1.6 { + sqlite3 db test.db + set fn [file_control_tempfilename db] + set fn + } {/etilqs_/} +} db close forcedelete .test_control_lockproxy.db-conch test.proxy +forcedelete test.db test2.db + +if {$tcl_platform(platform) eq "windows"} { + do_test filectrl-2.1 { + sqlite3 db test2.db + set size [file size test2.db] + set handle [file_control_win32_get_handle db] + db close + forcedelete test2.db + list $size $handle [expr {$handle != 0}] + } {/^0 \{0 [0-9A-Fa-f]+\} 1$/} + + do_test filectrl-2.2 { + sqlite3 db test2.db + execsql { + CREATE TABLE t1(x); + INSERT INTO t1 (x) VALUES(RANDOMBLOB(1048576)); + } + set size [file size test2.db] + set handle [file_control_win32_get_handle db] + db close + forcedelete test2.db + list $size $handle [expr {$handle != 0}] + } {/^1\d+ \{0 [0-9A-Fa-f]+\} 1$/} +} + finish_test diff --git a/test/filefmt.test b/test/filefmt.test index 2df1424436..b44ef8e29a 100644 --- a/test/filefmt.test +++ b/test/filefmt.test @@ -45,7 +45,7 @@ do_test filefmt-1.3 { catchsql { SELECT count(*) FROM sqlite_master } -} {1 {file is encrypted or is not a database}} +} {1 {file is not a database}} do_test filefmt-1.4 { db close hexio_write test.db 0 53 @@ -85,7 +85,7 @@ do_test filefmt-1.6 { catchsql { SELECT count(*) FROM sqlite_master } -} {1 {file is encrypted or is not a database}} +} {1 {file is not a database}} # The page-size must be at least 512 bytes @@ -97,7 +97,7 @@ do_test filefmt-1.7 { catchsql { SELECT count(*) FROM sqlite_master } -} {1 {file is encrypted or is not a database}} +} {1 {file is not a database}} # Usable space per page (page-size minus unused space per page) # must be at least 480 bytes @@ -114,7 +114,7 @@ ifcapable pager_pragmas { catchsql { SELECT count(*) FROM sqlite_master } - } {1 {file is encrypted or is not a database}} + } {1 {file is not a database}} } #------------------------------------------------------------------------- @@ -144,9 +144,11 @@ do_execsql_test filefmt-2.1.1 { CREATE TABLE t2(a); INSERT INTO t2 VALUES(1); } {} -do_test filefmt-2.1.2 { - hexio_read test.db 28 4 -} {00000009} +if {![nonzero_reserved_bytes]} { + do_test filefmt-2.1.2 { + hexio_read test.db 28 4 + } {00000009} +} do_test filefmt-2.1.3 { sql36231 { INSERT INTO t1 VALUES(a_string(3000)) } @@ -170,9 +172,11 @@ do_execsql_test filefmt-2.2.1 { CREATE TABLE t2(a); INSERT INTO t2 VALUES(1); } {} -do_test filefmt-2.2.2 { - hexio_read test.db 28 4 -} {00000009} +if {![nonzero_reserved_bytes]} { + do_test filefmt-2.2.2 { + hexio_read test.db 28 4 + } {00000009} +} do_test filefmt-2.2.3 { sql36231 { INSERT INTO t1 VALUES(a_string(3000)) } diff --git a/test/filter1.test b/test/filter1.test new file mode 100644 index 0000000000..bb36e179b3 --- /dev/null +++ b/test/filter1.test @@ -0,0 +1,237 @@ +# 2018 May 8 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix filter1 + +ifcapable !windowfunc { + finish_test + return +} + +do_execsql_test 1.0 { + CREATE TABLE t1(a); + CREATE INDEX i1 ON t1(a); + INSERT INTO t1 VALUES(1), (2), (3), (4), (5), (6), (7), (8), (9); +} + +do_execsql_test 1.1 { SELECT sum(a) FROM t1; } 45 +do_execsql_test 1.2 { SELECT sum(a) FILTER( WHERE a<5 ) FROM t1; } 10 + +do_execsql_test 1.3 { + SELECT sum(a) FILTER( WHERE a>9 ), + sum(a) FILTER( WHERE a>8 ), + sum(a) FILTER( WHERE a>7 ), + sum(a) FILTER( WHERE a>6 ), + sum(a) FILTER( WHERE a>5 ), + sum(a) FILTER( WHERE a>4 ), + sum(a) FILTER( WHERE a>3 ), + sum(a) FILTER( WHERE a>2 ), + sum(a) FILTER( WHERE a>1 ), + sum(a) FILTER( WHERE a>0 ) + FROM t1; +} {{} 9 17 24 30 35 39 42 44 45} + +do_execsql_test 1.4 { + SELECT max(a) FILTER (WHERE (a % 2)==0) FROM t1 +} {8} + +do_execsql_test 1.5 { + SELECT min(a) FILTER (WHERE a>4) FROM t1 +} {5} + +do_execsql_test 1.6 { + SELECT count(*) FILTER (WHERE a!=5) FROM t1 +} {8} + +do_execsql_test 1.7 { + SELECT min(a) FILTER (WHERE a>3) FROM t1 GROUP BY (a%2) ORDER BY 1; +} {4 5} + +do_execsql_test 1.8 { + CREATE VIEW vv AS + SELECT sum(a) FILTER( WHERE a>9 ), + sum(a) FILTER( WHERE a>8 ), + sum(a) FILTER( WHERE a>7 ), + sum(a) FILTER( WHERE a>6 ), + sum(a) FILTER( WHERE a>5 ), + sum(a) FILTER( WHERE a>4 ), + sum(a) FILTER( WHERE a>3 ), + sum(a) FILTER( WHERE a>2 ), + sum(a) FILTER( WHERE a>1 ), + sum(a) FILTER( WHERE a>0 ) + FROM t1; + SELECT * FROM vv; +} {{} 9 17 24 30 35 39 42 44 45} + + +#------------------------------------------------------------------------- +# Test some errors: +# +# .1 FILTER on a non-aggregate function, +# .2 Window function in FILTER clause, +# .3 Aggregate function in FILTER clause, +# +reset_db +do_execsql_test 2.0 { + CREATE TABLE t1(a); + INSERT INTO t1 VALUES(1), (2), (3), (4), (5), (6), (7), (8), (9); +} + +do_catchsql_test 2.1 { + SELECT upper(a) FILTER (WHERE a=1) FROM t1 +} {1 {FILTER may not be used with non-aggregate upper()}} + +do_catchsql_test 2.2 { + SELECT sum(a) FILTER (WHERE 1 - max(a) OVER () > 0) FROM t1 +} {1 {misuse of window function max()}} + +do_catchsql_test 2.3 { + SELECT sum(a) FILTER (WHERE 1 - count(a)) FROM t1 +} {1 {misuse of aggregate function count()}} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 3.0 { + CREATE TABLE t1(a,b); + INSERT INTO t1 VALUES(1, 1); +} +do_execsql_test 3.1 { + SELECT b, max(a) FILTER (WHERE b='x') FROM t1; +} {1 {}} + +do_execsql_test 3.2 { + CREATE TABLE t2(a, b, c); + INSERT INTO t2 VALUES(1, 2, 3); + INSERT INTO t2 VALUES(1, 3, 4); + INSERT INTO t2 VALUES(2, 5, 6); + INSERT INTO t2 VALUES(2, 7, 8); +} +do_execsql_test 3.3 { + SELECT a, c, max(b) FILTER (WHERE c='x') FROM t2 GROUP BY a; +} {1 3 {} 2 6 {}} + +do_execsql_test 3.4 { + DELETE FROM t2; + INSERT INTO t2 VALUES(1, 5, 'x'); + INSERT INTO t2 VALUES(1, 2, 3); + INSERT INTO t2 VALUES(1, 4, 'x'); + INSERT INTO t2 VALUES(2, 5, 6); + INSERT INTO t2 VALUES(2, 7, 8); +} +do_execsql_test 3.5 { + SELECT a, c, max(b) FILTER (WHERE c='x') FROM t2 GROUP BY a; +} {1 x 5 2 6 {}} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 4.0 { + CREATE TABLE t1(a, b, c); + INSERT INTO t1 VALUES('a', 0, 5); + INSERT INTO t1 VALUES('a', 1, 10); + INSERT INTO t1 VALUES('a', 0, 15); + + INSERT INTO t1 VALUES('b', 0, 5); + INSERT INTO t1 VALUES('b', 1, 1000); + INSERT INTO t1 VALUES('b', 0, 5); + + INSERT INTO t1 VALUES('c', 0, 1); + INSERT INTO t1 VALUES('c', 1, 2); + INSERT INTO t1 VALUES('c', 0, 3); +} + +do_execsql_test 4.1 { + SELECT avg(c) FILTER (WHERE b!=1) AS h FROM t1 GROUP BY a ORDER BY h; +} {2.0 5.0 10.0} +do_execsql_test 4.2 { + SELECT avg(c) FILTER (WHERE b!=1) AS h FROM t1 GROUP BY a ORDER BY (h+1.0); +} {2.0 5.0 10.0} +do_execsql_test 4.3 { + SELECT a, avg(c) FILTER (WHERE b!=1) AS h FROM t1 GROUP BY a ORDER BY avg(c); +} {c 2.0 a 10.0 b 5.0} +do_execsql_test 4.4 { + SELECT a, avg(c) FILTER (WHERE b!=1) FROM t1 GROUP BY a ORDER BY 2 +} {c 2.0 b 5.0 a 10.0} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 5.0 { + CREATE TABLE t1(a, b); + INSERT INTO t1 VALUES(1, 2); + INSERT INTO t1 VALUES(1, 3); +} + +do_execsql_test 5.1 { + SELECT count(*) FILTER (WHERE b>2) FROM (SELECT * FROM t1) +} {1} + +do_execsql_test 5.2 { + SELECT count(*) FILTER (WHERE b>2) OVER () FROM (SELECT * FROM t1) +} {1 1} + +do_execsql_test 5.3 { + SELECT count(*) FILTER (WHERE b>2) OVER (ORDER BY b) FROM (SELECT * FROM t1) +} {0 1} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 6.0 { + CREATE TABLE t1(a,b); + INSERT INTO t1 VALUES(1,1); + INSERT INTO t1 VALUES(2,2); + CREATE TABLE t2(x,y); + INSERT INTO t2 VALUES(1,1); +} + +do_execsql_test 6.1 { + SELECT (SELECT COUNT(a) FILTER(WHERE x) FROM t2) FROM t1; +} {1 1} +do_execsql_test 6.2 { + SELECT (SELECT COUNT(a+x) FROM t2) FROM t1; +} {1 1} +do_execsql_test 6.3 { + SELECT (SELECT COUNT(a) FROM t2) FROM t1; +} {2} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 7.0 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b); + INSERT INTO t1 VALUES(321, 100000); + INSERT INTO t1 VALUES(111, 110000); + INSERT INTO t1 VALUES(444, 120000); + INSERT INTO t1 VALUES(222, 130000); +} + +do_execsql_test 7.1 { + SELECT max(a), max(a) FILTER (WHERE b<12345), b FROM t1; +} { + 444 {} 120000 +} + +# 2023-02-17 dbsqlfuzz 4f8e0de6e272bbbb3e1b41cb5aea31e0b47297e3 +# count() with FILTER clause using the COUNTOFVIEW optimization. +# +reset_db +do_execsql_test 8.0 { + CREATE TABLE t0(c0 INT); + CREATE TABLE t1a(a INTEGER PRIMARY KEY, b TEXT); + INSERT INTO t1a VALUES(1,'one'),(2,NULL),(3,'three'); + CREATE TABLE t1b(c INTEGER PRIMARY KEY, d TEXT); + INSERT INTO t1b VALUES(4,'four'),(5,NULL),(6,'six'); + CREATE VIEW t1 AS SELECT a, b FROM t1a UNION ALL SELECT c, d FROM t1b; + SELECT count()FILTER(WHERE b IS NULL) FROM t1; +} 2 + +finish_test diff --git a/test/filter2.tcl b/test/filter2.tcl new file mode 100644 index 0000000000..f3871f80bc --- /dev/null +++ b/test/filter2.tcl @@ -0,0 +1,132 @@ +# 2018 May 19 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +source [file join [file dirname $argv0] pg_common.tcl] + +#========================================================================= + + +start_test filter2 "2019 July 2" + +ifcapable !windowfunc + +execsql_test 1.0 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a INTEGER PRIMARY KEY, b INTEGER); + INSERT INTO t1 VALUES + (1, 7), (2, 3), (3, 5), (4, 30), (5, 26), (6, 23), (7, 27), + (8, 3), (9, 17), (10, 26), (11, 33), (12, 25), (13, NULL), (14, 47), + (15, 36), (16, 13), (17, 45), (18, 31), (19, 11), (20, 36), (21, 37), + (22, 21), (23, 22), (24, 14), (25, 16), (26, 3), (27, 7), (28, 29), + (29, 50), (30, 38), (31, 3), (32, 36), (33, 12), (34, 4), (35, 46), + (36, 3), (37, 48), (38, 23), (39, NULL), (40, 24), (41, 5), (42, 46), + (43, 11), (44, NULL), (45, 18), (46, 25), (47, 15), (48, 18), (49, 23); +} + +execsql_test 1.1 { SELECT sum(b) FROM t1 } + +execsql_test 1.2 { SELECT sum(b) FILTER (WHERE a<10) FROM t1 } + +execsql_test 1.3 { SELECT count(DISTINCT b) FROM t1 } + +execsql_test 1.4 { SELECT count(DISTINCT b) FILTER (WHERE a!=19) FROM t1 } + +execsql_test 1.5 { + SELECT min(b) FILTER (WHERE a>19), + min(b) FILTER (WHERE a>0), + max(a+b) FILTER (WHERE a>19), + max(b+a) FILTER (WHERE a BETWEEN 10 AND 40) + FROM t1; +} + +execsql_test 1.6 { + SELECT min(b), + min(b), + max(a+b), + max(b+a) + FROM t1 + GROUP BY (a%10) + ORDER BY 1, 2, 3, 4; +} + +execsql_test 1.7 { + SELECT min(b) FILTER (WHERE a>19), + min(b) FILTER (WHERE a>0), + max(a+b) FILTER (WHERE a>19), + max(b+a) FILTER (WHERE a BETWEEN 10 AND 40) + FROM t1 + GROUP BY (a%10) + ORDER BY 1, 2, 3, 4; +} + +execsql_test 1.8 { + SELECT sum(a+b) FILTER (WHERE a=NULL) FROM t1 +} + +execsql_test 1.9 { + SELECT (a%5) FROM t1 GROUP BY (a%5) + HAVING sum(b) FILTER (WHERE b<20) > 34 + ORDER BY 1 +} + +execsql_test 1.10 { + SELECT (a%5), sum(b) FILTER (WHERE b<20) AS bbb + FROM t1 + GROUP BY (a%5) HAVING sum(b) FILTER (WHERE b<20) >34 + ORDER BY 1 +} + +execsql_test 1.11 { + SELECT (a%5), sum(b) FILTER (WHERE b<20) AS bbb + FROM t1 + GROUP BY (a%5) HAVING sum(b) FILTER (WHERE b<20) >34 + ORDER BY 2 +} + +execsql_test 1.12 { + SELECT (a%5), + sum(b) FILTER (WHERE b<20) AS bbb, + count(distinct b) FILTER (WHERE b<20 OR a=13) AS ccc + FROM t1 GROUP BY (a%5) + ORDER BY 2 +} + +execsql_test 1.13 { + SELECT + string_agg(CAST(b AS TEXT), '_') FILTER (WHERE b%2!=0), + string_agg(CAST(b AS TEXT), '_') FILTER (WHERE b%2!=1), + count(*) FILTER (WHERE b%2!=0), + count(*) FILTER (WHERE b%2!=1) + FROM t1; +} + +execsql_float_test 1.14 { + SELECT + avg(b) FILTER (WHERE b>a), + avg(b) FILTER (WHERE b<a) + FROM t1 GROUP BY (a%2) ORDER BY 1,2; +} + +execsql_test 1.15 { + SELECT + a/5, + sum(b) FILTER (WHERE a%5=0), + sum(b) FILTER (WHERE a%5=1), + sum(b) FILTER (WHERE a%5=2), + sum(b) FILTER (WHERE a%5=3), + sum(b) FILTER (WHERE a%5=4) + FROM t1 GROUP BY (a/5) ORDER BY 1; +} + +finish_test + + diff --git a/test/filter2.test b/test/filter2.test new file mode 100644 index 0000000000..06cfd2a4c3 --- /dev/null +++ b/test/filter2.test @@ -0,0 +1,156 @@ +# 2019 July 2 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. +# + +#################################################### +# DO NOT EDIT! THIS FILE IS AUTOMATICALLY GENERATED! +#################################################### + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix filter2 + +ifcapable !windowfunc { finish_test ; return } +do_execsql_test 1.0 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a INTEGER PRIMARY KEY, b INTEGER); + INSERT INTO t1 VALUES + (1, 7), (2, 3), (3, 5), (4, 30), (5, 26), (6, 23), (7, 27), + (8, 3), (9, 17), (10, 26), (11, 33), (12, 25), (13, NULL), (14, 47), + (15, 36), (16, 13), (17, 45), (18, 31), (19, 11), (20, 36), (21, 37), + (22, 21), (23, 22), (24, 14), (25, 16), (26, 3), (27, 7), (28, 29), + (29, 50), (30, 38), (31, 3), (32, 36), (33, 12), (34, 4), (35, 46), + (36, 3), (37, 48), (38, 23), (39, NULL), (40, 24), (41, 5), (42, 46), + (43, 11), (44, NULL), (45, 18), (46, 25), (47, 15), (48, 18), (49, 23); +} {} + +do_execsql_test 1.1 { + SELECT sum(b) FROM t1 +} {1041} + +do_execsql_test 1.2 { + SELECT sum(b) FILTER (WHERE a<10) FROM t1 +} {141} + +do_execsql_test 1.3 { + SELECT count(DISTINCT b) FROM t1 +} {31} + +do_execsql_test 1.4 { + SELECT count(DISTINCT b) FILTER (WHERE a!=19) FROM t1 +} {31} + +do_execsql_test 1.5 { + SELECT min(b) FILTER (WHERE a>19), + min(b) FILTER (WHERE a>0), + max(a+b) FILTER (WHERE a>19), + max(b+a) FILTER (WHERE a BETWEEN 10 AND 40) + FROM t1; +} {3 3 88 85} + +do_execsql_test 1.6 { + SELECT min(b), + min(b), + max(a+b), + max(b+a) + FROM t1 + GROUP BY (a%10) + ORDER BY 1, 2, 3, 4; +} {3 3 58 58 3 3 66 66 3 3 71 71 3 3 88 88 4 4 61 61 5 5 54 54 + 7 7 85 85 11 11 79 79 16 16 81 81 24 24 68 68} + +do_execsql_test 1.7 { + SELECT min(b) FILTER (WHERE a>19), + min(b) FILTER (WHERE a>0), + max(a+b) FILTER (WHERE a>19), + max(b+a) FILTER (WHERE a BETWEEN 10 AND 40) + FROM t1 + GROUP BY (a%10) + ORDER BY 1, 2, 3, 4; +} {3 3 58 58 3 3 71 39 4 4 38 61 7 7 85 85 11 5 54 45 16 16 81 81 + 18 3 66 61 21 3 88 68 23 11 79 79 24 24 68 68} + +do_execsql_test 1.8 { + SELECT sum(a+b) FILTER (WHERE a=NULL) FROM t1 +} {{}} + +do_execsql_test 1.9 { + SELECT (a%5) FROM t1 GROUP BY (a%5) + HAVING sum(b) FILTER (WHERE b<20) > 34 + ORDER BY 1 +} {3 4} + +do_execsql_test 1.10 { + SELECT (a%5), sum(b) FILTER (WHERE b<20) AS bbb + FROM t1 + GROUP BY (a%5) HAVING sum(b) FILTER (WHERE b<20) >34 + ORDER BY 1 +} {3 49 4 46} + +do_execsql_test 1.11 { + SELECT (a%5), sum(b) FILTER (WHERE b<20) AS bbb + FROM t1 + GROUP BY (a%5) HAVING sum(b) FILTER (WHERE b<20) >34 + ORDER BY 2 +} {4 46 3 49} + +do_execsql_test 1.12 { + SELECT (a%5), + sum(b) FILTER (WHERE b<20) AS bbb, + count(distinct b) FILTER (WHERE b<20 OR a=13) AS ccc + FROM t1 GROUP BY (a%5) + ORDER BY 2 +} {2 25 3 0 34 2 1 34 4 4 46 4 3 49 5} + +do_execsql_test 1.13 { + SELECT + string_agg(CAST(b AS TEXT), '_') FILTER (WHERE b%2!=0), + group_concat(CAST(b AS TEXT), '_') FILTER (WHERE b%2!=1), + count(*) FILTER (WHERE b%2!=0), + count(*) FILTER (WHERE b%2!=1) + FROM t1; +} {7_3_5_23_27_3_17_33_25_47_13_45_31_11_37_21_3_7_29_3_3_23_5_11_25_15_23 30_26_26_36_36_22_14_16_50_38_36_12_4_46_48_24_46_18_18 27 19} + + +do_test 1.14 { + set myres {} + foreach r [db eval {SELECT + avg(b) FILTER (WHERE b>a), + avg(b) FILTER (WHERE b<a) + FROM t1 GROUP BY (a%2) ORDER BY 1,2;}] { + lappend myres [format %.4f [set r]] + } + set res2 {30.8333 13.7273 31.4167 13.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + +do_execsql_test 1.15 { + SELECT + a/5, + sum(b) FILTER (WHERE a%5=0), + sum(b) FILTER (WHERE a%5=1), + sum(b) FILTER (WHERE a%5=2), + sum(b) FILTER (WHERE a%5=3), + sum(b) FILTER (WHERE a%5=4) + FROM t1 GROUP BY (a/5) ORDER BY 1; +} {0 {} 7 3 5 30 1 26 23 27 3 17 2 26 33 25 {} 47 3 36 13 45 31 11 + 4 36 37 21 22 14 5 16 3 7 29 50 6 38 3 36 12 4 7 46 3 48 23 {} + 8 24 5 46 11 {} 9 18 25 15 18 23} + +finish_test diff --git a/test/filterfault.test b/test/filterfault.test new file mode 100644 index 0000000000..a8cde1f555 --- /dev/null +++ b/test/filterfault.test @@ -0,0 +1,44 @@ +# 2018 May 8 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix filterfault + +ifcapable !windowfunc { + finish_test + return +} + +do_execsql_test 1.0 { + CREATE TABLE t1(a, b, c, d); + INSERT INTO t1 VALUES(1, 2, 3, 4); + INSERT INTO t1 VALUES(5, 6, 7, 8); + INSERT INTO t1 VALUES(9, 10, 11, 12); +} +faultsim_save_and_close + +do_faultsim_test 1 -faults oom-t* -prep { + faultsim_restore_and_reopen +} -body { + execsql { + SELECT sum(a) FILTER (WHERE b<5), + count() FILTER (WHERE d!=c) + FROM t1 GROUP BY c ORDER BY 1; + } +} -test { + faultsim_test_result {0 {{} 1 {} 1 1 1}} +} + + +finish_test diff --git a/test/fkey1.test b/test/fkey1.test index e10781ac52..46e7f64a19 100644 --- a/test/fkey1.test +++ b/test/fkey1.test @@ -15,6 +15,7 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl +set testprefix fkey1 ifcapable {!foreignkey} { finish_test @@ -170,6 +171,22 @@ do_catchsql_test fkey1-5.2 { INSERT OR REPLACE INTO t11 VALUES (2, 3); } {1 {FOREIGN KEY constraint failed}} +# Make sure sqlite3_trace() output works with triggers used to implement +# FK constraints +# +ifcapable trace { + proc sqltrace {txt} { + global traceoutput + lappend traceoutput $txt + } + do_test fkey1-5.2.1 { + unset -nocomplain traceoutput + db trace sqltrace + catch {db eval {INSERT OR REPLACE INTO t11 VALUES(2,3);}} + set traceoutput + } {{INSERT OR REPLACE INTO t11 VALUES(2,3);} {INSERT OR REPLACE INTO t11 VALUES(2,3);} {INSERT OR REPLACE INTO t11 VALUES(2,3);}} +} + # A similar test to the above. do_execsql_test fkey1-5.3 { CREATE TABLE Foo ( @@ -185,4 +202,85 @@ do_catchsql_test fkey1-5.4 { INSERT OR REPLACE INTO Foo(Id, ParentId, C1) VALUES (2, 3, 'A-2-3'); } {1 {FOREIGN KEY constraint failed}} +#------------------------------------------------------------------------- +# Check that foreign key processing is not fooled by partial indexes +# on the parent table. +# +do_execsql_test 6.0 { + CREATE TABLE p1(x, y); + CREATE UNIQUE INDEX p1x ON p1(x) WHERE y<2; + INSERT INTO p1 VALUES(1, 1); + CREATE TABLE c1(a REFERENCES p1(x)); +} + +do_catchsql_test 6.1 { + INSERT INTO c1 VALUES(1); +} {1 {foreign key mismatch - "c1" referencing "p1"}} + +do_execsql_test 6.2 { + CREATE UNIQUE INDEX p1x2 ON p1(x); + INSERT INTO c1 VALUES(1); +} {} + +# 2021-07-03 https://sqlite.org/forum/forumpost/a6b0c05277 +# 2021-07-07 https://sqlite.org/forum/forumpost/79c9e4797d +# Failure to allocate enough registers in the VDBE for a +# PRAGMA foreign_key_check when the foreign key has more +# columns than the table. +# +reset_db +do_execsql_test 7.1 { + PRAGMA foreign_keys=OFF; + CREATE TABLE t1(a,b,c,FOREIGN KEY(a,a,a,a,a,a,a,a,a,a,a,a,a,a) REFERENCES t0); + INSERT INTO t1 VALUES(1,2,3); + PRAGMA foreign_key_check; +} {t1 1 t0 0} +do_execsql_test 7.2 { + DROP TABLE t1; + CREATE TABLE t1(a,b,c AS(1),d, FOREIGN KEY(c,d,b,a,b,d,b,c) REFERENCES t0); + PRAGMA foreign_key_check; +} {} + +# 2021-12-31 forum https://sqlite.org/forum/forumpost/24bd1fef7e9323ef +# Memory leak caused by sqlite3NestedParse() running on a corrupt system +# table. Discovered by Jingzhou Fu. +# +reset_db +do_execsql_test 8.1 { + PRAGMA writable_schema=ON; + PRAGMA foreign_keys = ON; + CREATE TABLE sqlite_stat1 (tbl INTEGER PRIMARY KEY DESC, idx UNIQUE DEFAULT NULL) WITHOUT ROWID; + PRAGMA writable_schema=OFF; + CREATE TABLE sqlsim4(stat PRIMARY KEY);; + CREATE TABLE t1(sqlsim7 REFERENCES sqlite_stat1 ON DELETE CASCADE); + DROP table "sqlsim4"; +} {} +# 2022-01-01 dbsqlfuzz 1c57440219f6f0aedf5e8f72a8ddd75f15aea381 +# Follow-up case to the above. Assertion is not true if the schema +# is corrupt. +reset_db +database_may_be_corrupt +do_execsql_test 8.2 { + CREATE TABLE t1(a REFERENCES sqlite_stat1 ON DELETE CASCADE); + CREATE TABLE t2(a TEXT PRIMARY KEY); + PRAGMA writable_schema=ON; + CREATE TABLE sqlite_stat1(tbl INTEGER PRIMARY KEY DESC, idx UNIQUE DEFAULT NULL) WITHOUT ROWID; + UPDATE sqlite_schema SET name='sqlite_autoindex_sqlite_stat1_1' WHERE name='sqlite_autoindex_sqlite_stat1_2'; + PRAGMA writable_schema=RESET; +} {} +do_catchsql_test 8.3 { + REINDEX; +} {1 {database disk image is malformed}} + +# 2023-04-13 https://bugs.chromium.org/p/chromium/issues/detail?id=1405220 +# Avoid double-de-quoting of table names when processing foreign keys. +# +reset_db +do_execsql_test 9.1 { + PRAGMA foreign_keys = ON; + CREATE TABLE """1"("""2", """3" PRIMARY KEY); + CREATE TABLE """4"("""5" REFERENCES """1" ON DELETE RESTRICT); + DELETE FROM """1"; +} + finish_test diff --git a/test/fkey2.test b/test/fkey2.test index aec75ed693..6f9bdc2b7c 100644 --- a/test/fkey2.test +++ b/test/fkey2.test @@ -417,14 +417,14 @@ do_test fkey2-3.1.2 { } {} do_test fkey2-3.1.3 { catchsql { UPDATE ab SET a = 5 } -} {1 {CHECK constraint failed: ef}} +} {1 {CHECK constraint failed: e!=5}} do_test fkey2-3.1.4 { execsql { SELECT * FROM ab } } {1 b} do_test fkey2-3.1.4 { execsql BEGIN; catchsql { UPDATE ab SET a = 5 } -} {1 {CHECK constraint failed: ef}} +} {1 {CHECK constraint failed: e!=5}} do_test fkey2-3.1.5 { execsql COMMIT; execsql { SELECT * FROM ab; SELECT * FROM cd; SELECT * FROM ef } @@ -955,6 +955,7 @@ ifcapable altertable { execsql { CREATE TABLE t1(a PRIMARY KEY); CREATE TABLE t2(a, b); + INSERT INTO t2 VALUES(1,2); } catchsql { ALTER TABLE t2 ADD COLUMN c REFERENCES t1 } } {0 {}} @@ -983,8 +984,11 @@ ifcapable altertable { # Test the sqlite_rename_parent() function directly. # proc test_rename_parent {zCreate zOld zNew} { - db eval {SELECT sqlite_rename_parent($zCreate, $zOld, $zNew)} + db eval {SELECT sqlite_rename_table( + 'main', 'table', 't1', $zCreate, $zOld, $zNew, 0 + )} } + sqlite3_test_control SQLITE_TESTCTRL_INTERNAL_FUNCTIONS db do_test fkey2-14.2.1.1 { test_rename_parent {CREATE TABLE t1(a REFERENCES t2)} t2 t3 } {{CREATE TABLE t1(a REFERENCES "t3")}} @@ -994,6 +998,7 @@ ifcapable altertable { do_test fkey2-14.2.1.3 { test_rename_parent {CREATE TABLE t1(a REFERENCES "t2")} t2 t3 } {{CREATE TABLE t1(a REFERENCES "t3")}} + sqlite3_test_control SQLITE_TESTCTRL_INTERNAL_FUNCTIONS db # Test ALTER TABLE RENAME TABLE a bit. # @@ -1042,6 +1047,7 @@ ifcapable altertable { execsql { CREATE TEMP TABLE t1(a PRIMARY KEY); CREATE TEMP TABLE t2(a, b); + INSERT INTO temp.t2 VALUES(1,2); } catchsql { ALTER TABLE t2 ADD COLUMN c REFERENCES t1 } } {0 {}} @@ -1062,10 +1068,11 @@ ifcapable altertable { PRAGMA foreign_keys = off; ALTER TABLE t2 ADD COLUMN h DEFAULT 'text' REFERENCES t1; PRAGMA foreign_keys = on; - SELECT sql FROM sqlite_temp_master WHERE name='t2'; + SELECT sql FROM temp.sqlite_master WHERE name='t2'; } } {{CREATE TABLE t2(a, b, c REFERENCES t1, d DEFAULT NULL REFERENCES t1, e REFERENCES t1 DEFAULT NULL, h DEFAULT 'text' REFERENCES t1)}} + sqlite3_test_control SQLITE_TESTCTRL_INTERNAL_FUNCTIONS db do_test fkey2-14.2tmp.1.1 { test_rename_parent {CREATE TABLE t1(a REFERENCES t2)} t2 t3 } {{CREATE TABLE t1(a REFERENCES "t3")}} @@ -1075,6 +1082,7 @@ ifcapable altertable { do_test fkey2-14.2tmp.1.3 { test_rename_parent {CREATE TABLE t1(a REFERENCES "t2")} t2 t3 } {{CREATE TABLE t1(a REFERENCES "t3")}} + sqlite3_test_control SQLITE_TESTCTRL_INTERNAL_FUNCTIONS db # Test ALTER TABLE RENAME TABLE a bit. # @@ -1093,7 +1101,7 @@ ifcapable altertable { ] do_test fkey2-14.2tmp.2.2 { execsql { ALTER TABLE t1 RENAME TO t4 } - execsql { SELECT sql FROM sqlite_temp_master WHERE type = 'table'} + execsql { SELECT sql FROM temp.sqlite_master WHERE type = 'table'} } [list \ {CREATE TABLE "t4"(a PRIMARY KEY, b REFERENCES "t4")} \ {CREATE TABLE t2(a PRIMARY KEY, b REFERENCES "t4", c REFERENCES t2)} \ @@ -1124,6 +1132,7 @@ ifcapable altertable { ATTACH ':memory:' AS aux; CREATE TABLE aux.t1(a PRIMARY KEY); CREATE TABLE aux.t2(a, b); + INSERT INTO aux.t2(a,b) VALUES(1,2); } catchsql { ALTER TABLE t2 ADD COLUMN c REFERENCES t1 } } {0 {}} @@ -1148,6 +1157,7 @@ ifcapable altertable { } } {{CREATE TABLE t2(a, b, c REFERENCES t1, d DEFAULT NULL REFERENCES t1, e REFERENCES t1 DEFAULT NULL, h DEFAULT 'text' REFERENCES t1)}} + sqlite3_test_control SQLITE_TESTCTRL_INTERNAL_FUNCTIONS db do_test fkey2-14.2aux.1.1 { test_rename_parent {CREATE TABLE t1(a REFERENCES t2)} t2 t3 } {{CREATE TABLE t1(a REFERENCES "t3")}} @@ -1157,6 +1167,7 @@ ifcapable altertable { do_test fkey2-14.2aux.1.3 { test_rename_parent {CREATE TABLE t1(a REFERENCES "t2")} t2 t3 } {{CREATE TABLE t1(a REFERENCES "t3")}} + sqlite3_test_control SQLITE_TESTCTRL_INTERNAL_FUNCTIONS db # Test ALTER TABLE RENAME TABLE a bit. # diff --git a/test/fkey5.test b/test/fkey5.test index b9e1fc2eec..d467a64281 100644 --- a/test/fkey5.test +++ b/test/fkey5.test @@ -15,10 +15,10 @@ # EVIDENCE-OF: R-15402-03103 PRAGMA schema.foreign_key_check; PRAGMA # schema.foreign_key_check(table-name); # -# EVIDENCE-OF: R-23918-17301 The foreign_key_check pragma checks the +# EVIDENCE-OF: R-41653-15278 The foreign_key_check pragma checks the # database, or the table called "table-name", for foreign key -# constraints that are violated and returns one row of output for each -# violation. +# constraints that are violated. The foreign_key_check pragma returns +# one row output for each foreign key violation. set testdir [file dirname $argv0] source $testdir/tester.tcl @@ -115,8 +115,11 @@ do_test fkey5-1.6 { # EVIDENCE-OF: R-55672-01620 The first column is the name of the table # that contains the REFERENCES clause. # -# EVIDENCE-OF: R-25219-25618 The second column is the rowid of the row -# that contains the invalid REFERENCES clause. +# EVIDENCE-OF: R-00471-55166 The second column is the rowid of the row +# that contains the invalid REFERENCES clause, or NULL if the child +# table is a WITHOUT ROWID table. +# +# The second clause in the previous is tested by fkey5-10.3. # # EVIDENCE-OF: R-40482-20265 The third column is the name of the table # that is referred to. @@ -388,5 +391,102 @@ do_execsql_test 9.4 { PRAGMA foreign_key_check(k2); } {k2 3 s1 0} +#------------------------------------------------------------------------- +# Test using a WITHOUT ROWID table as the child table with an INTEGER +# PRIMARY KEY as the parent key. +# +reset_db +do_execsql_test 10.1 { + CREATE TABLE p30 (id INTEGER PRIMARY KEY); + CREATE TABLE IF NOT EXISTS c30 ( + line INTEGER, + master REFERENCES p30(id), + PRIMARY KEY(master) + ) WITHOUT ROWID; + + INSERT INTO p30 (id) VALUES (1); + INSERT INTO c30 (master, line) VALUES (1, 999); +} +do_execsql_test 10.2 { + PRAGMA foreign_key_check; +} +# EVIDENCE-OF: R-00471-55166 The second column is the rowid of the row +# that contains the invalid REFERENCES clause, or NULL if the child +# table is a WITHOUT ROWID table. +do_execsql_test 10.3 { + INSERT INTO c30 VALUES(45, 45); + PRAGMA foreign_key_check; +} {c30 {} p30 0} + +#------------------------------------------------------------------------- +# Test "foreign key mismatch" errors. +# +reset_db +do_execsql_test 11.0 { + CREATE TABLE tt(y); + CREATE TABLE c11(x REFERENCES tt(y)); +} +do_catchsql_test 11.1 { + PRAGMA foreign_key_check; +} {1 {foreign key mismatch - "c11" referencing "tt"}} + +# 2020-07-03 Bug in foreign_key_check discovered while working on the +# forum reports that pragma_foreign_key_check does not accept an argument: +# If two separate schemas seem to reference one another, that causes +# problems for foreign_key_check. +# +reset_db +do_execsql_test 12.0 { + ATTACH ':memory:' as aux; + CREATE TABLE aux.t1(a INTEGER PRIMARY KEY, b TEXT REFERENCES t2); + CREATE TABLE main.t2(x TEXT PRIMARY KEY, y INT); + INSERT INTO main.t2 VALUES('abc',11),('def',22),('xyz',99); + INSERT INTO aux.t1 VALUES(5,'abc'),(7,'xyz'),(9,'oops'); + PRAGMA foreign_key_check=t1; +} {t1 5 t2 0 t1 7 t2 0 t1 9 t2 0} +do_execsql_test 12.1 { + CREATE TABLE aux.t2(x TEXT PRIMARY KEY, y INT); + INSERT INTO aux.t2 VALUES('abc',11),('def',22),('xyz',99); + PRAGMA foreign_key_check=t1; +} {t1 9 t2 0} + +# 2020-07-03: the pragma_foreign_key_check virtual table should +# accept arguments for the table name and/or schema name. +# +ifcapable vtab { + do_execsql_test 13.0 { + SELECT *, 'x' FROM pragma_foreign_key_check('t1'); + } {t1 9 t2 0 x} + do_catchsql_test 13.1 { + SELECT *, 'x' FROM pragma_foreign_key_check('t1','main'); + } {1 {no such table: main.t1}} + do_execsql_test 13.2 { + SELECT *, 'x' FROM pragma_foreign_key_check('t1','aux'); + } {t1 9 t2 0 x} +} + +ifcapable vtab { + reset_db + do_execsql_test 13.10 { + PRAGMA foreign_keys=OFF; + CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT REFERENCES t2); + CREATE TABLE t2(x TEXT PRIMARY KEY, y INT); + CREATE TABLE t3(w TEXT, z INT REFERENCES t1); + INSERT INTO t2 VALUES('abc',11),('def',22),('xyz',99); + INSERT INTO t1 VALUES(5,'abc'),(7,'xyz'),(9,'oops'); + INSERT INTO t3 VALUES(11,7),(22,19); + } {} + do_execsql_test 13.11 { + SELECT x.*, '|' + FROM sqlite_schema, pragma_foreign_key_check(name) AS x + WHERE type='table' + ORDER BY x."table"; + } {t1 9 t2 0 | t3 2 t1 0 |} + do_execsql_test 13.12 { + SELECT *, '|' + FROM pragma_foreign_key_check AS x + ORDER BY x."table"; + } {t1 9 t2 0 | t3 2 t1 0 |} +} finish_test diff --git a/test/fkey6.test b/test/fkey6.test index 6fc3de211c..415daeaf3e 100644 --- a/test/fkey6.test +++ b/test/fkey6.test @@ -1,4 +1,4 @@ -# 2013-07-11 +# 2012 December 17 # # The author disclaims copyright to this source code. In place of # a legal notice, here is a blessing: @@ -23,6 +23,7 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl +set testprefix fkey6 ifcapable {!foreignkey} { finish_test @@ -171,5 +172,135 @@ do_execsql_test fkey6-2.6 { PRAGMA defer_foreign_keys; } {0} +#-------------------------------------------------------------------------- +# Test that defer_foreign_keys disables RESTRICT. +# +do_execsql_test 3.1 { + CREATE TABLE p2(a PRIMARY KEY, b); + CREATE TABLE c2(x, y REFERENCES p2 ON DELETE RESTRICT ON UPDATE RESTRICT); + INSERT INTO p2 VALUES(1, 'one'); + INSERT INTO p2 VALUES(2, 'two'); + INSERT INTO c2 VALUES('i', 1); +} + +do_catchsql_test 3.2.1 { + BEGIN; + UPDATE p2 SET a=a-1; +} {1 {FOREIGN KEY constraint failed}} +do_execsql_test 3.2.2 { COMMIT } + +do_execsql_test 3.2.3 { + BEGIN; + PRAGMA defer_foreign_keys = 1; + UPDATE p2 SET a=a-1; + COMMIT; +} + +do_execsql_test 3.2.4 { + BEGIN; + PRAGMA defer_foreign_keys = 1; + UPDATE p2 SET a=a-1; +} +do_catchsql_test 3.2.5 { + COMMIT; +} {1 {FOREIGN KEY constraint failed}} +do_execsql_test 3.2.6 { ROLLBACK } + +do_execsql_test 3.3.1 { + CREATE TRIGGER p2t AFTER DELETE ON p2 BEGIN + INSERT INTO p2 VALUES(old.a, 'deleted!'); + END; +} +do_catchsql_test 3.3.2 { + BEGIN; + DELETE FROM p2 WHERE a=1; +} {1 {FOREIGN KEY constraint failed}} +do_execsql_test 3.3.3 { COMMIT } + +do_execsql_test 3.3.4 { + BEGIN; + PRAGMA defer_foreign_keys = 1; + DELETE FROM p2 WHERE a=1; + COMMIT; + SELECT * FROM p2; +} {0 one 1 deleted!} + +#------------------------------------------------------------------------- +# Verify that, even with "PRAGMA defer_foreign_keys", a transaction cannot +# be committed if there are outstanding foreign key violations. +# +reset_db +do_execsql_test 4.0 { + CREATE TABLE p1(a INTEGER PRIMARY KEY, b UNIQUE); + CREATE TABLE c1(x REFERENCES p1(b)); + + INSERT INTO p1 VALUES(1, 'one'), (2, 'two'), (3, 'three'); + INSERT INTO c1 VALUES('two'); + + PRAGMA foreign_keys = 1; + PRAGMA defer_foreign_keys = 1; +} + +do_execsql_test 4.1 { + BEGIN; + DELETE FROM p1 WHERE a=2; +} + +do_catchsql_test 4.2 { + COMMIT; +} {1 {FOREIGN KEY constraint failed}} + +#------------------------------------------------------------------------- +# +reset_db +do_execsql_test 5.0 { + PRAGMA foreign_keys = 1; + CREATE TABLE p1(a INTEGER PRIMARY KEY, b); + CREATE TABLE c1(x REFERENCES p1 DEFERRABLE INITIALLY DEFERRED); +} + +do_execsql_test 5.1 { + BEGIN; + INSERT INTO c1 VALUES(123); + PRAGMA defer_foreign_keys = 1; + INSERT INTO p1 VALUES(123, 'one two three'); + COMMIT; +} + +#------------------------------------------------------------------------- +# +reset_db + +ifcapable fts5 { +if {[permutation]!="inmemory_journal"} { + do_execsql_test 6.1 { + PRAGMA auto_vacuum = 0; + PRAGMA writable_schema = 1; + INSERT INTO sqlite_schema + VALUES('table', 't1', 't1', 2, 'CREATE TABLE t1(x INTEGER PRIMARY KEY)'); + } + db close + sqlite3 db test.db + do_execsql_test 6.1 { + PRAGMA foreign_keys = 1; + PRAGMA writable_schema = 1; + } + do_execsql_test 6.2 { + CREATE TABLE t2( + y INTEGER PRIMARY KEY, + z INTEGER REFERENCES t1(x) DEFERRABLE INITIALLY DEFERRED + ); + } + do_execsql_test 6.3 { + BEGIN; + INSERT INTO t2 VALUES(1,0),(2,1); + CREATE VIRTUAL TABLE t3 USING fts5(a, b, content='', tokendata=1); + INSERT INTO t3 VALUES(3,3); + PRAGMA defer_foreign_keys=ON; + DELETE FROM t2; + COMMIT; + } +} +} finish_test diff --git a/test/fkey7.test b/test/fkey7.test index 6c646a9a7f..77870a7505 100644 --- a/test/fkey7.test +++ b/test/fkey7.test @@ -68,4 +68,54 @@ ifcapable incrblob { } {SQLITE_CONSTRAINT} } +ifcapable stat4 { + do_execsql_test 3.0 { + CREATE TABLE p4 (id INTEGER NOT NULL PRIMARY KEY); + INSERT INTO p4 VALUES(1), (2), (3); + + CREATE TABLE c4(x INTEGER REFERENCES p4(id) DEFERRABLE INITIALLY DEFERRED); + CREATE INDEX c4_x ON c4(x); + INSERT INTO c4 VALUES(1), (2), (3); + + ANALYZE; + INSERT INTO p4(id) VALUES(4); + } +} + + +do_execsql_test 4.0 { + PRAGMA foreign_keys = true; + CREATE TABLE parent( + p PRIMARY KEY + ); + CREATE TABLE child( + c UNIQUE REFERENCES parent(p) + ); +} + +do_catchsql_test 4.1 { + INSERT OR FAIL INTO child VALUES(123), (123); +} {1 {FOREIGN KEY constraint failed}} + +do_execsql_test 4.2 { + SELECT * FROM child; +} {} + +do_execsql_test 4.3 { + PRAGMA foreign_key_check; +} {} + +do_catchsql_test 4.4 { + INSERT INTO parent VALUES(123); + INSERT OR FAIL INTO child VALUES(123), (123); +} {1 {UNIQUE constraint failed: child.c}} + +do_execsql_test 4.5 { + SELECT * FROM child; +} {123} + +do_execsql_test 4.6 { + PRAGMA foreign_key_check; +} {} + finish_test diff --git a/test/fkey8.test b/test/fkey8.test index b4b6bb75e7..2d72f6fcf0 100644 --- a/test/fkey8.test +++ b/test/fkey8.test @@ -101,5 +101,153 @@ foreach {tn use_stmt sql schema} { } $use_stmt } +#------------------------------------------------------------------------- +# The following tests check that foreign key constaint counters are +# correctly updated for any implicit DELETE operations that occur +# when a REPLACE command is executed against a WITHOUT ROWID table +# that has no triggers or auxiliary indexes. +# +reset_db +do_execsql_test 2.1.0 { + PRAGMA foreign_keys = on; + CREATE TABLE p1(a PRIMARY KEY, b) WITHOUT ROWID; + CREATE TABLE c1(x REFERENCES p1 DEFERRABLE INITIALLY DEFERRED); + + INSERT INTO p1 VALUES(1, 'one'); + INSERT INTO p1 VALUES(2, 'two'); + INSERT INTO c1 VALUES(1); + INSERT INTO c1 VALUES(2); +} + +do_catchsql_test 2.1.2 { + BEGIN; + DELETE FROM p1 WHERE a=1; + INSERT OR REPLACE INTO p1 VALUES(2, 'two'); + COMMIT; +} {1 {FOREIGN KEY constraint failed}} + +reset_db +do_execsql_test 2.2.0 { + PRAGMA foreign_keys = on; + CREATE TABLE p2(a PRIMARY KEY, b); + CREATE TABLE c2( + x PRIMARY KEY, + y REFERENCES p2 DEFERRABLE INITIALLY DEFERRED + ) WITHOUT ROWID; +} + +do_catchsql_test 2.2.1 { + BEGIN; + INSERT INTO c2 VALUES(13, 13); + INSERT OR REPLACE INTO c2 VALUES(13, 13); + DELETE FROM c2; + COMMIT; +} {0 {}} + +reset_db +do_execsql_test 2.3.0 { + PRAGMA foreign_keys = on; + CREATE TABLE p3(a PRIMARY KEY, b) WITHOUT ROWID; + CREATE TABLE c3(x REFERENCES p3); + + INSERT INTO p3 VALUES(1, 'one'); + INSERT INTO p3 VALUES(2, 'two'); + INSERT INTO c3 VALUES(1); + INSERT INTO c3 VALUES(2); + + CREATE TRIGGER p3d AFTER DELETE ON p3 WHEN old.a=1 BEGIN + INSERT OR REPLACE INTO p3 VALUES(2, 'three'); + END; +} + +do_catchsql_test 2.3.1 { + DELETE FROM p3 WHERE a=1 +} {1 {FOREIGN KEY constraint failed}} + + +do_execsql_test 3.0 { + PRAGMA foreign_keys=ON; + CREATE TABLE t2( + a PRIMARY KEY, b, c, d, e, + FOREIGN KEY(b, c) REFERENCES t2(d, e) + ) WITHOUT ROWID; + CREATE UNIQUE INDEX idx ON t2(d, e); + + INSERT INTO t2 VALUES(1, 'one', 'one', 'one', 'one'); -- row is parent of self + INSERT INTO t2 VALUES(2, 'one', 'one', 'one', NULL); -- parent is row 1 +} + +do_catchsql_test 3.1 { + DELETE FROM t2 WHERE a=1; +} {1 {FOREIGN KEY constraint failed}} + +do_execsql_test 4.0 { + CREATE TABLE t1 ( + c1 PRIMARY KEY, + c2 NUMERIC, + FOREIGN KEY(c1) REFERENCES t1(c2) + ) WITHOUT ROWID ; + CREATE INDEX t1c1 ON t1(c1); + CREATE UNIQUE INDEX t1c1unique ON t1(c2); +} +do_catchsql_test 4.1 { + INSERT OR REPLACE INTO t1 VALUES(10000, 20000); +} {1 {FOREIGN KEY constraint failed}} +do_execsql_test 4.2 { + INSERT OR REPLACE INTO t1 VALUES(20000, 20000); +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 5.0 { + PRAGMA foreign_keys = true; + CREATE TABLE parent( + p TEXT PRIMARY KEY + ); + CREATE TABLE child( + c INTEGER UNIQUE, + FOREIGN KEY(c) REFERENCES parent(p) DEFERRABLE INITIALLY DEFERRED + ); + BEGIN; + INSERT INTO child VALUES(123); + INSERT INTO parent VALUES('123'); + COMMIT; +} +do_execsql_test 5.1 { + PRAGMA integrity_check; +} {ok} + +do_execsql_test 5.2 { + INSERT INTO parent VALUES(1200); + BEGIN; + INSERT INTO child VALUES(456); + UPDATE parent SET p = '456' WHERE p=1200; + COMMIT; +} +do_execsql_test 5.3 { + PRAGMA integrity_check; +} {ok} + +#------------------------------------------------------------------------- +reset_db +forcedelete test.db2 +do_execsql_test 6.1 { + PRAGMA foreign_keys = on; + CREATE TABLE c1(b); + INSERT INTO c1 VALUES(123); +} + +do_execsql_test 6.2 { + ATTACH 'test.db2' AS aux; + CREATE TABLE aux.p1(a INTEGER PRIMARY KEY); + CREATE TABLE aux.c1(b REFERENCES p1(a) ON DELETE RESTRICT); + + INSERT INTO aux.p1 VALUES(123); +} + +do_execsql_test 6.3 { + DELETE FROM aux.p1 WHERE a=123; +} + finish_test diff --git a/test/fordelete.test b/test/fordelete.test index 9a382d97f5..39c0c3585e 100644 --- a/test/fordelete.test +++ b/test/fordelete.test @@ -48,7 +48,7 @@ proc analyze_delete_program {sql} { set obj $T($root) set O($obj) "" - if {"0x$R(p5)" & 0x08} { + if {$R(p5) & 0x08} { set O($obj) * } else { set O($obj) "" diff --git a/test/fork-test.c b/test/fork-test.c new file mode 100644 index 0000000000..8a9b4b4afa --- /dev/null +++ b/test/fork-test.c @@ -0,0 +1,310 @@ +/* +** The program demonstrates how a child process created using fork() +** can continue to use SQLite for a database that the parent had opened and +** and was writing into when the fork() occurred. +** +** This program executes the following steps: +** +** 1. Create a new database file. Open it, and populate it. +** 2. Start a transaction and make changes. +** ^-- close the transaction prior to fork() if --commit-before-fork +** 3. Fork() +** 4. In the child, close the database connection. Special procedures +** are needed to close the database connection in the child. See the +** implementation below. +** 5. Commit the transaction in the parent. +** 6. Verify that the transaction committed in the parent. +** 7. In the child, after a delay to allow time for (5) and (6), +** open a new database connection and verify that the transaction +** committed by (5) is seen. +** 8. Add make further changes and commit them in the child, using the +** new database connection. +** 9. In the parent, after a delay to account for (8), verify that +** the new transaction added by (8) can be seen. +** +** Usage: +** +** fork-test FILENAME [options] +** +** Options: +** +** --wal Run the database in WAL mode +** --vfstrace Enable VFS tracing for debugging +** --commit-before-fork COMMIT prior to the fork() in step 3 +** --delay-after-4 N Pause for N seconds after step 4 +** +** How To Compile: +** +** gcc -O0 -g -Wall -I$(SQLITESRC) \ +** f1.c $(SQLITESRC)/ext/misc/vfstrace.c $(SQLITESRC/sqlite3.c \ +** -ldl -lpthread -lm +** +** Test procedure: +** +** (1) Run "fork-test x1.db". Verify no I/O errors occur and that +** both parent and child see all three rows in the t1 table. +** +** (2) Repeat (1) adding the --wal option. +** +** (3) Repeat (1) and (2) adding the --commit-before-fork option. +** +** (4) Repeat all prior steps adding the --delay-after-4 option with +** a timeout of 15 seconds or so. Then, while both parent and child +** are paused, run the CLI against the x1.db database from a separate +** window and verify that all the correct file locks are still working +** correctly. +** +** Take-Aways: +** +** * If a process has open SQLite database connections when it fork()s, +** the child can call exec() and all it well. Nothing special needs +** to happen. +** +** * If a process has open SQLite database connections when it fork()s, +** the child can do anything that does not involve using SQLite without +** causing problems in the parent. No special actions are needed in +** the child. +** +** * If a process has open SQLite database connections when it fork()s, +** the child can call sqlite3_close() on those database connections +** as long as there were no pending write transactions when the fork() +** occurred. +** +** * If a process has open SQLite database connections that are in the +** middle of a write transaction and then the processes fork()s, the +** child process should close the database connections using the +** procedures demonstrated in Step 4 below before trying to do anything +** else with SQLite. +** +** * If a child process can safely close SQLite database connections that +** it inherited via fork() using the procedures shown in Step 4 below +** even if the database connections were not involved in a write +** transaction at the time of the fork(). The special procedures are +** required if a write transaction was active. They are optional +** otherwise. No harm results from using the special procedures when +** they are not necessary. +** +** * Child processes that use SQLite should open their own database +** connections. They should not attempt to use a database connection +** that is inherited from the parent. +*/ +#include <stdio.h> +#include <string.h> +#include <sys/types.h> +#include <sys/wait.h> +#include <unistd.h> +#include <stdlib.h> +#include <errno.h> +#include "sqlite3.h" + +/* +** Process ID of the parent +*/ +static pid_t parentPid = 0; + +/* +** Return either "parent" or "child", as appropriate. +*/ +static const char *whoAmI(void){ + return getpid()==parentPid ? "parent" : "child"; +} + +/* +** This is an sqlite3_exec() callback routine that prints all results. +*/ +static int execCallback(void *pNotUsed, int nCol, char **aVal, char **aCol){ + int i; + const char *zWho = whoAmI(); + for(i=0; i<nCol; i++){ + const char *zVal = aVal[i]; + const char *zCol = aCol[i]; + if( zVal==0 ) zVal = "NULL"; + if( zCol==0 ) zCol = "NULL"; + printf("%s: %s = %s\n", zWho, zCol, zVal); + fflush(stdout); + } + return 0; +} + +/* +** Execute one or more SQL statements. +*/ +static void sqlExec(sqlite3 *db, const char *zSql, int bCallback){ + int rc; + char *zErr = 0; + printf("%s: %s\n", whoAmI(), zSql); + fflush(stdout); + rc = sqlite3_exec(db, zSql, bCallback ? execCallback : 0, 0, &zErr); + if( rc || zErr!=0 ){ + printf("%s: %s: rc=%d: %s\n", + whoAmI(), zSql, rc, zErr); + exit(1); + } +} + +/* +** Trace callback for the vfstrace extension. +*/ +static int vfsTraceCallback(const char *zMsg, void *pNotUsed){ + printf("%s: %s", whoAmI(), zMsg); + fflush(stdout); + return 0; +} + +/* External VFS module provided by ext/misc/vfstrace.c +*/ +extern int vfstrace_register( + const char *zTraceName, // Name of the newly constructed VFS + const char *zOldVfsName, // Name of the underlying VFS + int (*xOut)(const char*,void*), // Output routine. ex: fputs + void *pOutArg, // 2nd argument to xOut. ex: stderr + int makeDefault // Make the new VFS the default +); + + +int main(int argc, char **argv){ + sqlite3 *db; + int rc; + int i; + int useWal = 0; + const char *zFilename = 0; + pid_t child = 0, c2; + int status; + int bCommitBeforeFork = 0; + int nDelayAfter4 = 0; + + for(i=1; i<argc; i++){ + const char *z = argv[i]; + if( z[0]=='-' && z[1]=='-' && z[2]!=0 ) z++; + if( strcmp(z, "-wal")==0 ){ + useWal = 1; + }else if( strcmp(z, "-commit-before-fork")==0 ){ + bCommitBeforeFork = 1; + }else if( strcmp(z, "-delay-after-4")==0 && i+1<argc ){ + i++; + nDelayAfter4 = atoi(argv[i]); + }else if( strcmp(z, "-vfstrace")==0 ){ + vfstrace_register("vfstrace", 0, vfsTraceCallback, 0, 1); + }else if( z[0]=='-' ){ + printf("unknown option: \"%s\"\n", argv[i]); + exit(1); + }else if( zFilename!=0 ){ + printf("unknown argument: \"%s\"\n", argv[i]); + exit(1); + }else{ + zFilename = argv[i]; + } + } + if( zFilename==0 ){ + printf("Usage: %s FILENAME\n", argv[0]); + return 1; + } + + /** Step 1 **/ + printf("Step 1:\n"); + parentPid = getpid(); + unlink(zFilename); + rc = sqlite3_open(zFilename, &db); + if( rc ){ + printf("sqlite3_open() returns %d\n", rc); + exit(1); + } + if( useWal ){ + sqlExec(db, "PRAGMA journal_mode=WAL;", 0); + } + sqlExec(db, "CREATE TABLE t1(x);", 0); + sqlExec(db, "INSERT INTO t1 VALUES('First row');", 0); + sqlExec(db, "SELECT x FROM t1;", 1); + + /** Step 2 **/ + printf("Step 2:\n"); + sqlExec(db, "BEGIN IMMEDIATE;", 0); + sqlExec(db, "INSERT INTO t1 VALUES('Second row');", 0); + sqlExec(db, "SELECT x FROM t1;", 1); + if( bCommitBeforeFork ) sqlExec(db, "COMMIT", 0); + + /** Step 3 **/ + printf("Step 3:\n"); fflush(stdout); + child = fork(); + if( child!=0 ){ + printf("Parent = %d\nChild = %d\n", getpid(), child); + } + + /** Step 4 **/ + if( child==0 ){ + int k; + printf("Step 4:\n"); fflush(stdout); + + /*********************************************************************** + ** The following block of code closes the database connection without + ** rolling back or changing any files on disk. This is necessary to + ** preservce the pending transaction in the parent. + */ + for(k=0; 1/*exit-by-break*/; k++){ + const char *zDbName = sqlite3_db_name(db, k); + sqlite3_file *pJrnl = 0; + if( k==1 ) continue; + if( zDbName==0 ) break; + sqlite3_file_control(db, zDbName, SQLITE_FCNTL_NULL_IO, 0); + sqlite3_file_control(db, zDbName, SQLITE_FCNTL_JOURNAL_POINTER, &pJrnl); + if( pJrnl && pJrnl->pMethods && pJrnl->pMethods->xFileControl ){ + pJrnl->pMethods->xFileControl(pJrnl, SQLITE_FCNTL_NULL_IO, 0); + } + } + sqlite3_close(db); + /* + ** End of special close procedures for SQLite database connections + ** inherited via fork(). + ***********************************************************************/ + + printf("%s: database connection closed\n", whoAmI()); fflush(stdout); + }else{ + /* Pause the parent briefly to give the child a chance to close its + ** database connection */ + sleep(1); + } + + if( nDelayAfter4>0 ){ + printf("%s: Delay for %d seconds\n", whoAmI(), nDelayAfter4); + fflush(stdout); + sleep(nDelayAfter4); + printf("%s: Continue after %d delay\n", whoAmI(), nDelayAfter4); + fflush(stdout); + } + + /** Step 5 **/ + if( child!=0 ){ + printf("Step 5:\n"); + if( !bCommitBeforeFork ) sqlExec(db, "COMMIT", 0); + sqlExec(db, "SELECT x FROM t1;", 1); + } + + + /** Step 7 **/ + if( child==0 ){ + sleep(2); + printf("Steps 7 and 8:\n"); + rc = sqlite3_open(zFilename, &db); + if( rc ){ + printf("Child unable to reopen the database. rc = %d\n", rc); + exit(1); + } + sqlExec(db, "SELECT * FROM t1;", 1); + + /** Step 8 **/ + sqlExec(db, "INSERT INTO t1 VALUES('Third row');", 0); + sqlExec(db, "SELECT * FROM t1;", 1); + sleep(1); + return 0; + } + c2 = wait(&status); + printf("Process %d finished with status %d\n", c2, status); + + /** Step 9 */ + if( child!=0 ){ + printf("Step 9:\n"); + sqlExec(db, "SELECT * FROM t1;", 1); + } + + return 0; +} diff --git a/test/format4.test b/test/format4.test index 14d794709b..a850ce2e42 100644 --- a/test/format4.test +++ b/test/format4.test @@ -17,7 +17,8 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl -db eval {PRAGMA legacy_file_format=OFF} +#db eval {PRAGMA legacy_file_format=OFF} +sqlite3_db_config db LEGACY_FILE_FORMAT 0 # The size of the database depends on whether or not autovacuum # is enabled. diff --git a/test/fp-speed-1.c b/test/fp-speed-1.c new file mode 100644 index 0000000000..cb4e0409c3 --- /dev/null +++ b/test/fp-speed-1.c @@ -0,0 +1,154 @@ +/* +** Performance testing of floating-point binary-to-decimal conversion for +** SQLite versus the standard library. +** +** To compile: +** +** make sqlite3.c +** gcc -Os -c sqlite3.c +** gcc -I. -Os test/fp-speed-1.c sqlite3.o -ldl -lm -lpthread +** +** To run the test: +** +** time ./a.out 0 10000000 <-- standard library +** time ./a.out 1 10000000 <-- SQLite +*/ +static double aVal[] = { + -1.0163830486285643089e+063, + +0.0049243807391586981e-019, + +7.3818732407343994867e-095, + +7.0678595219225717143e+000, + +9.2807266319850025655e+120, + +5.8871050861933394663e+135, + -2.2998023621259662805e-168, + -1.5903581924910847482e+181, + +2.4313441816844978216e-001, + -3.8290987328945399091e-147, + +1.8787914062744001349e-009, + +0.7270653560487146653e-014, + +0.0639577697913183665e+107, + +5.2292338879315861930e-103, + +6.3747682672872231319e-021, + +8.6972339538329106984e-129, + -9.5074486051597691937e-026, + -8.6480257845368753018e-005, + -3.5657595775797470332e+017, + -7.8323106179731761351e+161, + +7.7813875274120800372e+077, + -1.8739718928360196596e-050, + +8.6898145915593357218e-051, + +6.0566766359877837871e+002, + +4.1703379217148160678e+199, + +2.1283871288746651560e+150, + -6.8395236083891810637e+128, + -6.2114299763939529155e-147, + -2.0753525742614637350e-149, + +5.8727459803944290257e-007, + +8.5888991062002101817e+010, + +6.8624461031355917632e+026, + -3.3053986756670905167e-075, + -4.3596843152763444945e-108, + +0.0834139520104099673e+098, + -8.8581986548904222440e-192, + -3.6622095428727698202e+038, + -6.6965852297025063260e+005, + +1.8204169347406488441e-054, + +6.5234508038649000384e-065, + +1.5923006018219011453e+083, + +1.7362555291656389480e+018, + +7.2875431976854785882e+160, + +1.2835880105958926748e-146, + +8.0516253320320819420e-113, + +6.6324633399381145440e-030, + -1.7126500070280062058e-149, + +1.6995738341583712335e+042, + +7.6048766923738663725e-112, + +0.6159117235449455043e-004, + +5.7544894355415943289e-135, + +8.2970228592690581890e-023, + -6.5531925360163067447e+020, + +5.8321334606187030050e+120, + +5.6557166509571891727e+095, + +0.3322789708438408704e-114, + -7.1210648776698686623e-050, + -9.6721262526706343301e+179, + -3.4583916571377395084e-106, + +4.7896094323214750793e-172, + -9.6926028040004137875e-056, + +7.0683848275381385483e-198, + -5.2970114182162961964e-007, + -4.4287021200905393271e-170, + +0.0728891155732808799e-189, + -9.1855462025879447465e+175, + +3.7294126234131007796e+015, + +2.6857421882792719241e+003, + -4.7070607333624685339e+039, + +7.2175820768279334274e+136, + -8.3678412534261163481e-115, + +2.2174844304241822163e+019, + +0.1949824588606861016e+112, + -9.7334052955672071912e+151, + -9.7793887766936999879e-142, + -5.1561164587416931561e+139, + -7.5048993577765174789e-022, + +7.3556076568687784568e+107, + -5.0681628575533599865e-144, + +1.5209705642027747811e+165, + -7.5989782535048296040e-101, + +1.3654137203389775871e-016, + -1.6441720554651372066e+087, + -4.9042433196141125923e+000, + -7.7063611961649130777e+118, + +0.1699427460930766201e+174, + +8.3374317849572216870e-145, + -5.2355330480469580072e+081, + -3.8510045942194147919e+141, + -6.3513622544326339887e-147, + +2.3869303484454428988e+049, + +3.8352715871620360268e-165, + -3.1263120493136887902e+035, + -5.5794797002556490823e+051, + -8.8109874479595604379e+142, + -4.3727360120203216922e+070, + -3.1109951189668539974e+170, + -9.4841878031704268232e+011, + -3.7398451668304407277e+067, + +4.8984042008915959963e-091, +}; +#define NN (sizeof(aVal)/sizeof(aVal[0])) + +#include "sqlite3.h" +#include <stdio.h> +#include <stdlib.h> + +int main(int argc, char **argv){ + int i; + int cnt; + int fg; + char zBuf[1000]; + + if( argc!=3 ){ + fprintf(stderr, "Usage: %s FLAG COUNT\n", argv[0]); + return 1; + } + cnt = atoi(argv[2]); + fg = atoi(argv[1]); + + switch( fg % 3 ){ + case 0: { + printf("Doing %d calls to C-lib sprintf()\n", cnt); + for(i=0; i<cnt; i++){ + sprintf(zBuf, "%.26g", aVal[i%NN]); + } + break; + } + case 1: { + printf("Doing %d calls to sqlite3_snprintf()\n", cnt); + for(i=0; i<cnt; i++){ + sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.26g", aVal[i%NN]); + } + break; + } + } + return 0; +} diff --git a/test/fpconv1.test b/test/fpconv1.test new file mode 100644 index 0000000000..195fdf9904 --- /dev/null +++ b/test/fpconv1.test @@ -0,0 +1,44 @@ +# 2023-07-03 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# This file contains a test that attempts to verify the claim that the +# floatpoint-to-text conversion routines built into SQLite maintain at +# least 15 significant digits of accuracy. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +if {[catch {load_static_extension db decimal} error]} { + puts "Skipping decimal tests, hit load error: $error" + finish_test; return +} + +sqlite3_create_function db +do_execsql_test fpconv1-1.0 { + WITH RECURSIVE + /* Number of random floating-point values to try. + ** On a circa 2016 x64 linux box, this test runs at + ** about 80000 cases per second -------------------vvvvvv */ + c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<100000), + fp(y) AS MATERIALIZED ( + SELECT CAST( format('%+d.%019d0e%+03d', + random()%10,abs(random()),random()%200) AS real) + FROM c + ) + SELECT y FROM fp + WHERE -log10(abs(decimal_sub(dtostr(y,24),format('%!.24e',y))/y))<15.0; + /* Number of digits of accuracy required -------^^^^ */ +} {} +# ^---- Expect a empty set as the result. The output is all tested numbers +# that fail to preserve at least 15 significant digits of accuracy. + +finish_test diff --git a/test/fts1a.test b/test/fts1a.test deleted file mode 100644 index b63e79a81b..0000000000 --- a/test/fts1a.test +++ /dev/null @@ -1,186 +0,0 @@ -# 2006 September 9 -# -# The author disclaims copyright to this source code. In place of -# a legal notice, here is a blessing: -# -# May you do good and not evil. -# May you find forgiveness for yourself and forgive others. -# May you share freely, never taking more than you give. -# -#************************************************************************* -# This file implements regression tests for SQLite library. The -# focus of this script is testing the FTS1 module. -# -# $Id: fts1a.test,v 1.4 2006/09/28 19:43:32 drh Exp $ -# - -set testdir [file dirname $argv0] -source $testdir/tester.tcl - -# If SQLITE_ENABLE_FTS1 is defined, omit this file. -ifcapable !fts1 { - finish_test - return -} - -# Construct a full-text search table containing five keywords: -# one, two, three, four, and five, in various combinations. The -# rowid for each will be a bitmask for the elements it contains. -# -db eval { - CREATE VIRTUAL TABLE t1 USING fts1(content); - INSERT INTO t1(content) VALUES('one'); - INSERT INTO t1(content) VALUES('two'); - INSERT INTO t1(content) VALUES('one two'); - INSERT INTO t1(content) VALUES('three'); - INSERT INTO t1(content) VALUES('one three'); - INSERT INTO t1(content) VALUES('two three'); - INSERT INTO t1(content) VALUES('one two three'); - INSERT INTO t1(content) VALUES('four'); - INSERT INTO t1(content) VALUES('one four'); - INSERT INTO t1(content) VALUES('two four'); - INSERT INTO t1(content) VALUES('one two four'); - INSERT INTO t1(content) VALUES('three four'); - INSERT INTO t1(content) VALUES('one three four'); - INSERT INTO t1(content) VALUES('two three four'); - INSERT INTO t1(content) VALUES('one two three four'); - INSERT INTO t1(content) VALUES('five'); - INSERT INTO t1(content) VALUES('one five'); - INSERT INTO t1(content) VALUES('two five'); - INSERT INTO t1(content) VALUES('one two five'); - INSERT INTO t1(content) VALUES('three five'); - INSERT INTO t1(content) VALUES('one three five'); - INSERT INTO t1(content) VALUES('two three five'); - INSERT INTO t1(content) VALUES('one two three five'); - INSERT INTO t1(content) VALUES('four five'); - INSERT INTO t1(content) VALUES('one four five'); - INSERT INTO t1(content) VALUES('two four five'); - INSERT INTO t1(content) VALUES('one two four five'); - INSERT INTO t1(content) VALUES('three four five'); - INSERT INTO t1(content) VALUES('one three four five'); - INSERT INTO t1(content) VALUES('two three four five'); - INSERT INTO t1(content) VALUES('one two three four five'); -} - -do_test fts1a-1.1 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'one'} -} {1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31} -do_test fts1a-1.2 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'one two'} -} {3 7 11 15 19 23 27 31} -do_test fts1a-1.3 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'two one'} -} {3 7 11 15 19 23 27 31} -do_test fts1a-1.4 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'one two three'} -} {7 15 23 31} -do_test fts1a-1.5 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'one three two'} -} {7 15 23 31} -do_test fts1a-1.6 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'two three one'} -} {7 15 23 31} -do_test fts1a-1.7 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'two one three'} -} {7 15 23 31} -do_test fts1a-1.8 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'three one two'} -} {7 15 23 31} -do_test fts1a-1.9 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'three two one'} -} {7 15 23 31} -do_test fts1a-1.10 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'one two THREE'} -} {7 15 23 31} -do_test fts1a-1.11 { - execsql {SELECT rowid FROM t1 WHERE content MATCH ' ONE Two three '} -} {7 15 23 31} - -do_test fts1a-2.1 { - execsql {SELECT rowid FROM t1 WHERE content MATCH '"one"'} -} {1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31} -do_test fts1a-2.2 { - execsql {SELECT rowid FROM t1 WHERE content MATCH '"one two"'} -} {3 7 11 15 19 23 27 31} -do_test fts1a-2.3 { - execsql {SELECT rowid FROM t1 WHERE content MATCH '"two one"'} -} {} -do_test fts1a-2.4 { - execsql {SELECT rowid FROM t1 WHERE content MATCH '"one two three"'} -} {7 15 23 31} -do_test fts1a-2.5 { - execsql {SELECT rowid FROM t1 WHERE content MATCH '"one three two"'} -} {} -do_test fts1a-2.6 { - execsql {SELECT rowid FROM t1 WHERE content MATCH '"one two three four"'} -} {15 31} -do_test fts1a-2.7 { - execsql {SELECT rowid FROM t1 WHERE content MATCH '"one three two four"'} -} {} -do_test fts1a-2.8 { - execsql {SELECT rowid FROM t1 WHERE content MATCH '"one three five"'} -} {21} -do_test fts1a-2.9 { - execsql {SELECT rowid FROM t1 WHERE content MATCH '"one three" five'} -} {21 29} -do_test fts1a-2.10 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'five "one three"'} -} {21 29} -do_test fts1a-2.11 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'five "one three" four'} -} {29} -do_test fts1a-2.12 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'five four "one three"'} -} {29} -do_test fts1a-2.13 { - execsql {SELECT rowid FROM t1 WHERE content MATCH '"one three" four five'} -} {29} - -do_test fts1a-3.1 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'one'} -} {1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31} -do_test fts1a-3.2 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'one -two'} -} {1 5 9 13 17 21 25 29} -do_test fts1a-3.3 { - execsql {SELECT rowid FROM t1 WHERE content MATCH '-two one'} -} {1 5 9 13 17 21 25 29} - -do_test fts1a-4.1 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'one OR two'} -} {1 2 3 5 6 7 9 10 11 13 14 15 17 18 19 21 22 23 25 26 27 29 30 31} -do_test fts1a-4.2 { - execsql {SELECT rowid FROM t1 WHERE content MATCH '"one two" OR three'} -} {3 4 5 6 7 11 12 13 14 15 19 20 21 22 23 27 28 29 30 31} -do_test fts1a-4.3 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'three OR "one two"'} -} {3 4 5 6 7 11 12 13 14 15 19 20 21 22 23 27 28 29 30 31} -do_test fts1a-4.4 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'one two OR three'} -} {3 5 7 11 13 15 19 21 23 27 29 31} -do_test fts1a-4.5 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'three OR two one'} -} {3 5 7 11 13 15 19 21 23 27 29 31} -do_test fts1a-4.6 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'one two OR three OR four'} -} {3 5 7 9 11 13 15 19 21 23 25 27 29 31} -do_test fts1a-4.7 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'two OR three OR four one'} -} {3 5 7 9 11 13 15 19 21 23 25 27 29 31} - -# Test the ability to handle NULL content -# -do_test fts1a-5.1 { - execsql {INSERT INTO t1(content) VALUES(NULL)} -} {} -do_test fts1a-5.2 { - set rowid [db last_insert_rowid] - execsql {SELECT content FROM t1 WHERE rowid=$rowid} -} {{}} -do_test fts1a-5.3 { - execsql {SELECT rowid FROM t1 WHERE content MATCH NULL} -} {} - - - -finish_test diff --git a/test/fts1b.test b/test/fts1b.test deleted file mode 100644 index 2bbe1aab80..0000000000 --- a/test/fts1b.test +++ /dev/null @@ -1,147 +0,0 @@ -# 2006 September 13 -# -# The author disclaims copyright to this source code. In place of -# a legal notice, here is a blessing: -# -# May you do good and not evil. -# May you find forgiveness for yourself and forgive others. -# May you share freely, never taking more than you give. -# -#************************************************************************* -# This file implements regression tests for SQLite library. The -# focus of this script is testing the FTS1 module. -# -# $Id: fts1b.test,v 1.4 2006/09/18 02:12:48 drh Exp $ -# - -set testdir [file dirname $argv0] -source $testdir/tester.tcl - -# If SQLITE_ENABLE_FTS1 is defined, omit this file. -ifcapable !fts1 { - finish_test - return -} - -# Fill the full-text index "t1" with phrases in english, spanish, -# and german. For the i-th row, fill in the names for the bits -# that are set in the value of i. The least significant bit is -# 1. For example, the value 5 is 101 in binary which will be -# converted to "one three" in english. -# -proc fill_multilanguage_fulltext_t1 {} { - set english {one two three four five} - set spanish {un dos tres cuatro cinco} - set german {eine zwei drei vier funf} - - for {set i 1} {$i<=31} {incr i} { - set cmd "INSERT INTO t1 VALUES" - set vset {} - foreach lang {english spanish german} { - set words {} - for {set j 0; set k 1} {$j<5} {incr j; incr k $k} { - if {$k&$i} {lappend words [lindex [set $lang] $j]} - } - lappend vset "'$words'" - } - set sql "INSERT INTO t1(english,spanish,german) VALUES([join $vset ,])" - # puts $sql - db eval $sql - } -} - -# Construct a full-text search table containing five keywords: -# one, two, three, four, and five, in various combinations. The -# rowid for each will be a bitmask for the elements it contains. -# -db eval { - CREATE VIRTUAL TABLE t1 USING fts1(english,spanish,german); -} -fill_multilanguage_fulltext_t1 - -do_test fts1b-1.1 { - execsql {SELECT rowid FROM t1 WHERE english MATCH 'one'} -} {1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31} -do_test fts1b-1.2 { - execsql {SELECT rowid FROM t1 WHERE spanish MATCH 'one'} -} {} -do_test fts1b-1.3 { - execsql {SELECT rowid FROM t1 WHERE german MATCH 'one'} -} {} -do_test fts1b-1.4 { - execsql {SELECT rowid FROM t1 WHERE t1 MATCH 'one'} -} {1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31} -do_test fts1b-1.5 { - execsql {SELECT rowid FROM t1 WHERE t1 MATCH 'one dos drei'} -} {7 15 23 31} -do_test fts1b-1.6 { - execsql {SELECT english, spanish, german FROM t1 WHERE rowid=1} -} {one un eine} -do_test fts1b-1.7 { - execsql {SELECT rowid FROM t1 WHERE t1 MATCH '"one un"'} -} {} - -do_test fts1b-2.1 { - execsql { - CREATE VIRTUAL TABLE t2 USING fts1(from,to); - INSERT INTO t2([from],[to]) VALUES ('one two three', 'four five six'); - SELECT [from], [to] FROM t2 - } -} {{one two three} {four five six}} - - -# Compute an SQL string that contains the words one, two, three,... to -# describe bits set in the value $i. Only the lower 5 bits are examined. -# -proc wordset {i} { - set x {} - for {set j 0; set k 1} {$j<5} {incr j; incr k $k} { - if {$k&$i} {lappend x [lindex {one two three four five} $j]} - } - return '$x' -} - -# Create a new FTS table with three columns: -# -# norm: words for the bits of rowid -# plusone: words for the bits of rowid+1 -# invert: words for the bits of ~rowid -# -db eval { - CREATE VIRTUAL TABLE t4 USING fts1([norm],'plusone',"invert"); -} -for {set i 1} {$i<=15} {incr i} { - set vset [list [wordset $i] [wordset [expr {$i+1}]] [wordset [expr {~$i}]]] - db eval "INSERT INTO t4(norm,plusone,invert) VALUES([join $vset ,]);" -} - -do_test fts1b-4.1 { - execsql {SELECT rowid FROM t4 WHERE t4 MATCH 'norm:one'} -} {1 3 5 7 9 11 13 15} -do_test fts1b-4.2 { - execsql {SELECT rowid FROM t4 WHERE norm MATCH 'one'} -} {1 3 5 7 9 11 13 15} -do_test fts1b-4.3 { - execsql {SELECT rowid FROM t4 WHERE t4 MATCH 'one'} -} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15} -do_test fts1b-4.4 { - execsql {SELECT rowid FROM t4 WHERE t4 MATCH 'plusone:one'} -} {2 4 6 8 10 12 14} -do_test fts1b-4.5 { - execsql {SELECT rowid FROM t4 WHERE plusone MATCH 'one'} -} {2 4 6 8 10 12 14} -do_test fts1b-4.6 { - execsql {SELECT rowid FROM t4 WHERE t4 MATCH 'norm:one plusone:two'} -} {1 5 9 13} -do_test fts1b-4.7 { - execsql {SELECT rowid FROM t4 WHERE t4 MATCH 'norm:one two'} -} {1 3 5 7 9 11 13 15} -do_test fts1b-4.8 { - execsql {SELECT rowid FROM t4 WHERE t4 MATCH 'plusone:two norm:one'} -} {1 5 9 13} -do_test fts1b-4.9 { - execsql {SELECT rowid FROM t4 WHERE t4 MATCH 'two norm:one'} -} {1 3 5 7 9 11 13 15} - - -finish_test diff --git a/test/fts1c.test b/test/fts1c.test deleted file mode 100644 index a12469593a..0000000000 --- a/test/fts1c.test +++ /dev/null @@ -1,1213 +0,0 @@ -# 2006 September 14 -# -# The author disclaims copyright to this source code. In place of -# a legal notice, here is a blessing: -# -# May you do good and not evil. -# May you find forgiveness for yourself and forgive others. -# May you share freely, never taking more than you give. -# -#************************************************************************* -# This file implements regression tests for SQLite library. The -# focus of this script is testing the FTS1 module. -# -# $Id: fts1c.test,v 1.11 2006/10/04 17:35:28 drh Exp $ -# - -set testdir [file dirname $argv0] -source $testdir/tester.tcl - -# If SQLITE_ENABLE_FTS1 is defined, omit this file. -ifcapable !fts1 { - finish_test - return -} - -# Create a table of sample email data. The data comes from email -# archives of Enron executives that was published as part of the -# litigation against that company. -# -do_test fts1c-1.1 { - db eval { - CREATE VIRTUAL TABLE email USING fts1([from],[to],subject,body); - BEGIN TRANSACTION; -INSERT INTO email([from],[to],subject,body) VALUES('savita.puthigai@enron.com', 'traders.eol@enron.com, traders.eol@enron.com', 'EnronOnline- Change to Autohedge', 'Effective Monday, October 22, 2001 the following changes will be made to the Autohedge functionality on EnronOnline. - -The volume on the hedge will now respect the minimum volume and volume increment settings on the parent product. See rules below: - -? If the transaction volume on the child is less than half of the parent''s minimum volume no hedge will occur. -? If the transaction volume on the child is more than half the parent''s minimum volume but less than half the volume increment on the parent, the hedge will volume will be the parent''s minimum volume. -? For all other volumes, the same rounding rules will apply based on the volume increment on the parent product. - -Please see example below: - -Parent''s Settings: -Minimum: 5000 -Increment: 1000 - -Volume on Autohedge transaction Volume Hedged -1 - 2499 0 -2500 - 5499 5000 -5500 - 6499 6000'); -INSERT INTO email([from],[to],subject,body) VALUES('dana.davis@enron.com', 'laynie.east@enron.com, lisa.king@enron.com, lisa.best@enron.com,', 'Leaving Early', 'FYI: -If it''s ok with everyone''s needs, I would like to leave @4pm. If you think -you will need my assistance past the 4 o''clock hour just let me know; I''ll -be more than willing to stay.'); -INSERT INTO email([from],[to],subject,body) VALUES('enron_update@concureworkplace.com', 'louise.kitchen@enron.com', '<<Concur Expense Document>> - CC02.06.02', 'The following expense report is ready for approval: - -Employee Name: Christopher F. Calger -Status last changed by: Mollie E. Gustafson Ms -Expense Report Name: CC02.06.02 -Report Total: $3,972.93 -Amount Due Employee: $3,972.93 - - -To approve this expense report, click on the following link for Concur Expense. -http://expensexms.enron.com'); -INSERT INTO email([from],[to],subject,body) VALUES('jeff.duff@enron.com', 'julie.johnson@enron.com', 'Work request', 'Julie, - -Could you print off the current work request report by 1:30 today? - -Gentlemen, - -I''d like to review this today at 1:30 in our office. Also, could you provide -me with your activity reports so I can have Julie enter this information. - -JD'); -INSERT INTO email([from],[to],subject,body) VALUES('v.weldon@enron.com', 'gary.l.carrier@usa.dupont.com, scott.joyce@bankofamerica.com', 'Enron News', 'This could turn into something big.... -http://biz.yahoo.com/rf/010129/n29305829.html'); -INSERT INTO email([from],[to],subject,body) VALUES('mark.haedicke@enron.com', 'paul.simons@enron.com', 'Re: First Polish Deal!', 'Congrats! Things seem to be building rapidly now on the Continent. Mark'); -INSERT INTO email([from],[to],subject,body) VALUES('e..carter@enron.com', 't..robinson@enron.com', 'FW: Producers Newsletter 9-24-2001', ' -The producer lumber pricing sheet. - -----Original Message----- -From: Johnson, Jay -Sent: Tuesday, October 16, 2001 3:42 PM -To: Carter, Karen E. -Subject: FW: Producers Newsletter 9-24-2001 - - - - -----Original Message----- -From: Daigre, Sergai -Sent: Friday, September 21, 2001 8:33 PM -Subject: Producers Newsletter 9-24-2001 - - '); -INSERT INTO email([from],[to],subject,body) VALUES('david.delainey@enron.com', 'kenneth.lay@enron.com', 'Greater Houston Partnership', 'Ken, in response to the letter from Mr Miguel San Juan, my suggestion would -be to offer up the Falcon for their use; however, given the tight time frame -and your recent visit with Mr. Fox that it would be difficult for either you -or me to participate. - -I spoke to Max and he agrees with this approach. - -I hope this meets with your approval. - -Regards -Delainey'); -INSERT INTO email([from],[to],subject,body) VALUES('lachandra.fenceroy@enron.com', 'lindy.donoho@enron.com', 'FW: Bus Applications Meeting Follow Up', 'Lindy, - -Here is the original memo we discussed earlier. Please provide any information that you may have. - -Your cooperation is greatly appreciated. - -Thanks, - -lachandra.fenceroy@enron.com -713.853.3884 -877.498.3401 Pager - - -----Original Message----- -From: Bisbee, Joanne -Sent: Wednesday, September 26, 2001 7:50 AM -To: Fenceroy, LaChandra -Subject: FW: Bus Applications Meeting Follow Up - -Lachandra, Please get with David Duff today and see what this is about. Who are our TW accounting business users? - - -----Original Message----- -From: Koh, Wendy -Sent: Tuesday, September 25, 2001 2:41 PM -To: Bisbee, Joanne -Subject: Bus Applications Meeting Follow Up - -Lisa brought up a TW change effective Nov 1. It involves eliminating a turnback surcharge. I have no other information, but you might check with the business folks for any system changes required. - -Wendy'); -INSERT INTO email([from],[to],subject,body) VALUES('danny.mccarty@enron.com', 'fran.fagan@enron.com', 'RE: worksheets', 'Fran, - If Julie''s merit needs to be lump sum, just move it over to that column. Also, send me Eric Gadd''s sheets as well. Thanks. -Dan - - -----Original Message----- -From: Fagan, Fran -Sent: Thursday, December 20, 2001 11:10 AM -To: McCarty, Danny -Subject: worksheets - -As discussed, attached are your sheets for bonus and merit. - -Thanks, - -Fran Fagan -Sr. HR Rep -713.853.5219 - - - << File: McCartyMerit.xls >> << File: mccartyBonusCommercial_UnP.xls >> - -'); -INSERT INTO email([from],[to],subject,body) VALUES('bert.meyers@enron.com', 'shift.dl-portland@enron.com', 'OCTOBER SCHEDULE', 'TEAM, - -PLEASE SEND ME ANY REQUESTS THAT YOU HAVE FOR OCTOBER. SO FAR I HAVE THEM FOR LEAF. I WOULD LIKE TO HAVE IT DONE BY THE 15TH OF THE MONTH. ANY QUESTIONS PLEASE GIVE ME A CALL. - -BERT'); -INSERT INTO email([from],[to],subject,body) VALUES('errol.mclaughlin@enron.com', 'john.arnold@enron.com, bilal.bajwa@enron.com, john.griffith@enron.com,', 'TRV Notification: (NG - PROPT P/L - 09/27/2001)', 'The report named: NG - PROPT P/L <http://trv.corp.enron.com/linkFromExcel.asp?report_cd=11&report_name=NG+-+PROPT+P/L&category_cd=5&category_name=FINANCIAL&toc_hide=1&sTV1=5&TV1Exp=Y&current_efct_date=09/27/2001>, published as of 09/27/2001 is now available for viewing on the website.'); -INSERT INTO email([from],[to],subject,body) VALUES('patrice.mims@enron.com', 'calvin.eakins@enron.com', 'Re: Small business supply assistance', 'Hi Calvin - - -I spoke with Rickey (boy, is he long-winded!!). Gave him the name of our -credit guy, Russell Diamond. - -Thank for your help!'); -INSERT INTO email([from],[to],subject,body) VALUES('legal <.hall@enron.com>', 'stephanie.panus@enron.com', 'Termination update', 'City of Vernon and Salt River Project terminated their contracts. I will fax these notices to you.'); -INSERT INTO email([from],[to],subject,body) VALUES('d..steffes@enron.com', 'richard.shapiro@enron.com', 'EES / ENA Government Affairs Staffing & Outside Services', 'Rick -- - -Here is the information on staffing and outside services. Call if you need anything else. - -Jim - - '); -INSERT INTO email([from],[to],subject,body) VALUES('gelliott@industrialinfo.com', 'pcopello@industrialinfo.com', 'ECAAR (Gavin), WSCC (Diablo Canyon), & NPCC (Seabrook)', 'Dear Power Outage Database Customer, -Attached you will find an excel document. The outages contained within are forced or rescheduled outages. Your daily delivery will still contain these outages. -In addition to the two excel documents, there is a dbf file that is formatted like your daily deliveries you receive nightly. This will enable you to load the data into your regular database. Any questions please let me know. Thanks. -Greg Elliott -IIR, Inc. -713-783-5147 x 3481 -outages@industrialinfo.com -THE INFORMATION CONTAINED IN THIS E-MAIL IS LEGALLY PRIVILEGED AND CONFIDENTIAL INFORMATION INTENDED ONLY FOR THE USE OF THE INDIVIDUAL OR ENTITY NAMED ABOVE. YOU ARE HEREBY NOTIFIED THAT ANY DISSEMINATION, DISTRIBUTION, OR COPY OF THIS E-MAIL TO UNAUTHORIZED ENTITIES IS STRICTLY PROHIBITED. IF YOU HAVE RECEIVED THIS -E-MAIL IN ERROR, PLEASE DELETE IT. - - OUTAGE.dbf - - 111201R.xls - - 111201.xls '); -INSERT INTO email([from],[to],subject,body) VALUES('enron.announcements@enron.com', 'all_ena_egm_eim@enron.com', 'EWS Brown Bag', 'MARK YOUR LUNCH CALENDARS NOW ! - -You are invited to attend the EWS Brown Bag Lunch Series - -Featuring: RAY BOWEN, COO - -Topic: Enron Industrial Markets - -Thursday, March 15, 2001 -11:30 am - 12:30 pm -EB 5 C2 - - -You bring your lunch, Limited Seating -We provide drinks and dessert. RSVP x 3-9610'); -INSERT INTO email([from],[to],subject,body) VALUES('chris.germany@enron.com', 'ingrid.immer@williams.com', 'Re: About St Pauls', 'Sounds good to me. I bet this is next to the Warick?? Hotel. - - - - -"Immer, Ingrid" <Ingrid.Immer@Williams.com> on 12/21/2000 11:48:47 AM -To: "''chris.germany@enron.com''" <chris.germany@enron.com> -cc: -Subject: About St Pauls - - - - - <<About St Pauls.url>> -? -?http://www.stpaulshouston.org/about.html - -Chris, - -I like the looks of this place.? What do you think about going here Christmas -eve?? They have an 11:00 a.m. service and a candlelight service at 5:00 p.m., -among others. - -Let me know.?? ii - - - About St Pauls.url - -'); -INSERT INTO email([from],[to],subject,body) VALUES('nas@cpuc.ca.gov', 'skatz@sempratrading.com, kmccrea@sablaw.com, thompson@wrightlaw.com,', 'Reply Brief filed July 31, 2000', ' - CPUC01-#76371-v1-Revised_Reply_Brief__Due_today_7_31_.doc'); -INSERT INTO email([from],[to],subject,body) VALUES('gascontrol@aglresources.com', 'dscott4@enron.com, lcampbel@enron.com', 'Alert Posted 10:00 AM November 20,2000: E-GAS Request Reminder', 'Alert Posted 10:00 AM November 20,2000: E-GAS Request Reminder -As discussed in the Winter Operations Meeting on Sept.29,2000, -E-Gas(Emergency Gas) will not be offered this winter as a service from AGLC. -Marketers and Poolers can receive gas via Peaking and IBSS nominations(daisy -chain) from other marketers up to the 6 p.m. Same Day 2 nomination cycle. -'); -INSERT INTO email([from],[to],subject,body) VALUES('dutch.quigley@enron.com', 'rwolkwitz@powermerchants.com', '', ' - -Here is a goody for you'); -INSERT INTO email([from],[to],subject,body) VALUES('ryan.o''rourke@enron.com', 'k..allen@enron.com, randy.bhatia@enron.com, frank.ermis@enron.com,', 'TRV Notification: (West VaR - 11/07/2001)', 'The report named: West VaR <http://trv.corp.enron.com/linkFromExcel.asp?report_cd=36&report_name=West+VaR&category_cd=2&category_name=WEST&toc_hide=1&sTV1=2&TV1Exp=Y&current_efct_date=11/07/2001>, published as of 11/07/2001 is now available for viewing on the website.'); -INSERT INTO email([from],[to],subject,body) VALUES('mjones7@txu.com', 'cstone1@txu.com, ggreen2@txu.com, timpowell@txu.com,', 'Enron / HPL Actuals for July 10, 2000', 'Teco Tap 10.000 / Enron ; 110.000 / HPL IFERC - -LS HPL LSK IC 30.000 / Enron -'); -INSERT INTO email([from],[to],subject,body) VALUES('susan.pereira@enron.com', 'kkw816@aol.com', 'soccer practice', 'Kathy- - -Is it safe to assume that practice is cancelled for tonight?? - -Susan Pereira'); -INSERT INTO email([from],[to],subject,body) VALUES('mark.whitt@enron.com', 'barry.tycholiz@enron.com', 'Huber Internal Memo', 'Please look at this. I didn''t know how deep to go with the desk. Do you think this works. - - '); -INSERT INTO email([from],[to],subject,body) VALUES('m..forney@enron.com', 'george.phillips@enron.com', '', 'George, -Give me a call and we will further discuss opportunities on the 13st floor. - -Thanks, -JMForney -3-7160'); -INSERT INTO email([from],[to],subject,body) VALUES('brad.mckay@enron.com', 'angusmcka@aol.com', 'Re: (no subject)', 'not yet'); -INSERT INTO email([from],[to],subject,body) VALUES('adam.bayer@enron.com', 'jonathan.mckay@enron.com', 'FW: Curve Fetch File', 'Here is the curve fetch file sent to me. It has plenty of points in it. If you give me a list of which ones you need we may be able to construct a secondary worksheet to vlookup the values. - -adam -35227 - - - -----Original Message----- -From: Royed, Jeff -Sent: Tuesday, September 25, 2001 11:37 AM -To: Bayer, Adam -Subject: Curve Fetch File - -Let me know if it works. It may be required to have a certain version of Oracle for it to work properly. - - - -Jeff Royed -Enron -Energy Operations -Phone: 713-853-5295'); -INSERT INTO email([from],[to],subject,body) VALUES('matt.smith@enron.com', 'yan.wang@enron.com', 'Report Formats', 'Yan, - -The merged reports look great. I believe the only orientation changes are to -"unmerge" the following six reports: - -31 Keystone Receipts -15 Questar Pipeline -40 Rockies Production -22 West_2 -23 West_3 -25 CIG_WIC - -The orientation of the individual reports should be correct. Thanks. - -Mat - -PS. Just a reminder to add the "*" by the title of calculated points.'); -INSERT INTO email([from],[to],subject,body) VALUES('michelle.lokay@enron.com', 'jimboman@bigfoot.com', 'Egyptian Festival', '---------------------- Forwarded by Michelle Lokay/ET&S/Enron on 09/07/2000 -10:08 AM --------------------------- - - -"Karkour, Randa" <Randa.Karkour@COMPAQ.com> on 09/07/2000 09:01:04 AM -To: "''Agheb (E-mail)" <Agheb@aol.com>, "Leila Mankarious (E-mail)" -<Leila_Mankarious@mhhs.org>, "''Marymankarious (E-mail)" -<marymankarious@aol.com>, "Michelle lokay (E-mail)" <mlokay@enron.com>, "Ramy -Mankarious (E-mail)" <Mankarious@aol.com> -cc: - -Subject: Egyptian Festival - - - <<Egyptian Festival.url>> - - http://www.egyptianfestival.com/ - - - Egyptian Festival.url -'); -INSERT INTO email([from],[to],subject,body) VALUES('errol.mclaughlin@enron.com', 'sherry.dawson@enron.com', 'Urgent!!! --- New EAST books', 'This has to be done.................................. - -Thanks ----------------------- Forwarded by Errol McLaughlin/Corp/Enron on 12/20/2000 -08:39 AM --------------------------- - - - - From: William Kelly @ ECT 12/20/2000 08:31 AM - - -To: Kam Keiser/HOU/ECT@ECT, Darron C Giron/HOU/ECT@ECT, David -Baumbach/HOU/ECT@ECT, Errol McLaughlin/Corp/Enron@ENRON -cc: Kimat Singla/HOU/ECT@ECT, Kulvinder Fowler/NA/Enron@ENRON, Kyle R -Lilly/HOU/ECT@ECT, Jeff Royed/Corp/Enron@ENRON, Alejandra -Chavez/NA/Enron@ENRON, Crystal Hyde/HOU/ECT@ECT - -Subject: New EAST books - -We have new book names in TAGG for our intramonth portfolios and it is -extremely important that any deal booked to the East is communicated quickly -to someone on my team. I know it will take some time for the new names to -sink in and I do not want us to miss any positions or P&L. - -Thanks for your help on this. - -New: -Scott Neal : East Northeast -Dick Jenkins: East Marketeast - -WK -'); -INSERT INTO email([from],[to],subject,body) VALUES('david.forster@enron.com', 'eol.wide@enron.com', 'Change to Stack Manager', 'Effective immediately, there is a change to the Stack Manager which will -affect any Inactive Child. - -An inactive Child with links to Parent products will not have their -calculated prices updated until the Child product is Activated. - -When the Child Product is activated, the price will be recalculated and -updated BEFORE it is displayed on the web. - -This means that if you are inputting a basis price on a Child product, you -will not see the final, calculated price until you Activate the product, at -which time the customer will also see it. - -If you have any questions, please contact the Help Desk on: - -Americas: 713 853 4357 -Europe: + 44 (0) 20 7783 7783 -Asia/Australia: +61 2 9229 2300 - -Dave'); -INSERT INTO email([from],[to],subject,body) VALUES('vince.kaminski@enron.com', 'jhh1@email.msn.com', 'Re: Light reading - see pieces beginning on page 7', 'John, - -I saw it. Very interesting. - -Vince - - - - - -"John H Herbert" <jhh1@email.msn.com> on 07/28/2000 08:38:08 AM -To: "Vince J Kaminski" <Vince_J_Kaminski@enron.com> -cc: -Subject: Light reading - see pieces beginning on page 7 - - -Cheers and have a nice weekend, - - -JHHerbert - - - - - - gd000728.pdf - - - -'); -INSERT INTO email([from],[to],subject,body) VALUES('matthew.lenhart@enron.com', 'mmmarcantel@equiva.com', 'RE:', 'i will try to line up a pig for you '); -INSERT INTO email([from],[to],subject,body) VALUES('jae.black@enron.com', 'claudette.harvey@enron.com, chaun.roberts@enron.com, judy.martinez@enron.com,', 'Disaster Recovery Equipment', 'As a reminder...there are several pieces of equipment that are set up on the 30th Floor, as well as on our floor, for the Disaster Recovery Team. PLEASE DO NOT TAKE, BORROW OR USE this equipment. Should you need to use another computer system, other than yours, or make conference calls please work with your Assistant to help find or set up equipment for you to use. - -Thanks for your understanding in this matter. - -T.Jae Black -East Power Trading -Assistant to Kevin Presto -off. 713-853-5800 -fax 713-646-8272 -cell 713-539-4760'); -INSERT INTO email([from],[to],subject,body) VALUES('eric.bass@enron.com', 'dale.neuner@enron.com', '5 X 24', 'Dale, - -Have you heard anything more on the 5 X 24s? We would like to get this -product out ASAP. - - -Thanks, - -Eric'); -INSERT INTO email([from],[to],subject,body) VALUES('messenger@smartreminders.com', 'm..tholt@enron.com', '10% Coupon - PrintPal Printer Cartridges - 100% Guaranteed', '[IMAGE] -[IMAGE][IMAGE][IMAGE] -Dear SmartReminders Member, - [IMAGE] [IMAGE] [IMAGE] [IMAGE] [IMAGE] [IMAGE] [IMAGE] [IMAGE] - - - - - - - - - - - - - - - - - - - - - -We respect your privacy and are a Certified Participant of the BBBOnLine - Privacy Program. To be removed from future offers,click here. -SmartReminders.com is a permission based service. To unsubscribe click here . '); -INSERT INTO email([from],[to],subject,body) VALUES('benjamin.rogers@enron.com', 'mark.bernstein@enron.com', '', 'The guy you are talking about left CIN under a "cloud of suspicion" sort of -speak. He was the one who got into several bad deals and PPA''s in California -for CIN, thus he left on a bad note. Let me know if you need more detail -than that, I felt this was the type of info you were looking for. Thanks! -Ben'); -INSERT INTO email([from],[to],subject,body) VALUES('enron_update@concureworkplace.com', 'michelle.cash@enron.com', 'Expense Report Receipts Not Received', 'Employee Name: Michelle Cash -Report Name: Houston Cellular 8-11-01 -Report Date: 12/13/01 -Report ID: 594D37C9ED2111D5B452 -Submitted On: 12/13/01 - -You are only allowed 2 reports with receipts outstanding. Your expense reports will not be paid until you meet this requirement.'); -INSERT INTO email([from],[to],subject,body) VALUES('susan.mara@enron.com', 'ray.alvarez@enron.com, mark.palmer@enron.com, karen.denne@enron.com,', 'CAISO Emergency Motion -- to discontinue market-based rates for', 'FYI. the latest broadside against the generators. - -Sue Mara -Enron Corp. -Tel: (415) 782-7802 -Fax:(415) 782-7854 ------ Forwarded by Susan J Mara/NA/Enron on 06/08/2001 12:24 PM ----- - - - "Milner, Marcie" <MMilner@coral-energy.com> 06/08/2001 11:13 AM To: "''smara@enron.com''" <smara@enron.com> cc: Subject: CAISO Emergency Motion - - -Sue, did you see this emergency motion the CAISO filed today? Apparently -they are requesting that FERC discontinue market-based rates immediately and -grant refunds plus interest on the difference between cost-based rates and -market revenues received back to May 2000. They are requesting the -commission act within 14 days. Have you heard anything about what they are -doing? - -Marcie - -http://www.caiso.com/docs/2001/06/08/200106081005526469.pdf -'); -INSERT INTO email([from],[to],subject,body) VALUES('fletcher.sturm@enron.com', 'eloy.escobar@enron.com', 'Re: General Brinks Position Meeting', 'Eloy, - -Who is General Brinks? - -Fletch'); -INSERT INTO email([from],[to],subject,body) VALUES('nailia.dindarova@enron.com', 'richard.shapiro@enron.com', 'Documents for Mark Frevert (on EU developments and lessons from', 'Rick, - -Here are the documents that Peter has prepared for Mark Frevert. - -Nailia ----------------------- Forwarded by Nailia Dindarova/LON/ECT on 25/06/2001 -16:36 --------------------------- - - -Nailia Dindarova -25/06/2001 15:36 -To: Michael Brown/Enron@EUEnronXGate -cc: Ross Sankey/Enron@EUEnronXGate, Eric Shaw/ENRON@EUEnronXGate, Peter -Styles/LON/ECT@ECT - -Subject: Documents for Mark Frevert (on EU developments and lessons from -California) - -Michael, - - -These are the documents that Peter promised to give to you for Mark Frevert. -He has now handed them to him in person but asked me to transmit them -electronically to you, as well as Eric and Ross. - -Nailia - - - - - -'); -INSERT INTO email([from],[to],subject,body) VALUES('peggy.a.kostial@accenture.com', 'dave.samuels@enron.com', 'EOL-Accenture Deal Sheet', 'Dave - - -Attached are our comments and suggested changes. Please call to review. - -On the time line for completion, we have four critical steps to complete: - Finalize market analysis to refine business case, specifically - projected revenue stream - Complete counterparty surveying, including targeting 3 CPs for letters - of intent - Review Enron asset base for potential reuse/ licensing - Contract negotiations - -Joe will come back to us with an updated time line, but it is my -expectation that we are still on the same schedule (we just begun week -three) with possibly a week or so slippage.....contract negotiations will -probably be the critical path. - -We will send our cut at the actual time line here shortly. Thanks, - -Peggy - -(See attached file: accenture-dealpoints v2.doc) - - accenture-dealpoints v2.doc '); -INSERT INTO email([from],[to],subject,body) VALUES('thomas.martin@enron.com', 'thomas.martin@enron.com', 'Re: Guadalupe Power Partners LP', '---------------------- Forwarded by Thomas A Martin/HOU/ECT on 03/20/2001 -03:49 PM --------------------------- - - -Thomas A Martin -10/11/2000 03:55 PM -To: Patrick Wade/HOU/ECT@ECT -cc: -Subject: Re: Guadalupe Power Partners LP - -The deal is physically served at Oasis Waha or Oasis Katy and is priced at -either HSC, Waha or Katytailgate GD at buyers option three days prior to -NYMEX close. - -'); -INSERT INTO email([from],[to],subject,body) VALUES('judy.townsend@enron.com', 'dan.junek@enron.com, chris.germany@enron.com', 'Columbia Distribution''s Capacity Available for Release - Sum', '---------------------- Forwarded by Judy Townsend/HOU/ECT on 03/09/2001 11:04 -AM --------------------------- - - -agoddard@nisource.com on 03/08/2001 09:16:57 AM -To: " - *Koch, Kent" <kkoch@nisource.com>, " - -*Millar, Debra" <dmillar@nisource.com>, " - *Burke, Lynn" -<lburke@nisource.com> -cc: " - *Heckathorn, Tom" <theckathorn@nisource.com> -Subject: Columbia Distribution''s Capacity Available for Release - Sum - - -Attached is Columbia Distribution''s notice of capacity available for release -for -the summer of 2001 (Apr. 2001 through Oct. 2001). - -Please note that the deadline for bids is 3:00pm EST on March 20, 2001. - -If you have any questions, feel free to contact any of the representatives -listed -at the bottom of the attachment. - -Aaron Goddard - - - - - - 2001Summer.doc -'); -INSERT INTO email([from],[to],subject,body) VALUES('rhonda.denton@enron.com', 'tim.belden@enron.com, dana.davis@enron.com, genia.fitzgerald@enron.com,', 'Split Rock Energy LLC', 'We have received the executed EEI contract from this CP dated 12/12/2000. -Copies will be distributed to Legal and Credit.'); -INSERT INTO email([from],[to],subject,body) VALUES('kerrymcelroy@dwt.com', 'jack.speer@alcoa.com, crow@millernash.com, michaelearly@earthlink.net,', 'Oral Argument Request', ' - Oral Argument Request.doc'); -INSERT INTO email([from],[to],subject,body) VALUES('mike.carson@enron.com', 'rlmichaelis@hormel.com', '', 'Did you come in town this wk end..... My new number at our house is : -713-668-3712...... my cell # is 281-381-7332 - -the kid'); -INSERT INTO email([from],[to],subject,body) VALUES('cooper.richey@enron.com', 'trycooper@hotmail.com', 'FW: Contact Info', ' - ------Original Message----- -From: Punja, Karim -Sent: Thursday, December 13, 2001 2:35 PM -To: Richey, Cooper -Subject: Contact Info - - -Cooper, - -Its been a real pleasure working with you (even though it was for only a small amount of time) -I hope we can stay in touch. - -Home# 234-0249 -email: kpunja@hotmail.com - -Take Care, - -Karim. - '); -INSERT INTO email([from],[to],subject,body) VALUES('bjm30@earthlink.net', 'mcguinn.k@enron.com, mcguinn.ian@enron.com, mcguinn.stephen@enron.com,', 'email address change', 'Hello all. - -I haven''t talked to many of you via email recently but I do want to give you -my new address for your email file: - - bjm30@earthlink.net - -I hope all is well. - -Brian McGuinn'); -INSERT INTO email([from],[to],subject,body) VALUES('shelley.corman@enron.com', 'steve.hotte@enron.com', 'Flat Panels', 'Can you please advise what is going on with the flat panels that we had planned to distribute to our gas logistics team. It was in the budget and we had the okay, but now I''m hearing there is some hold-up & the units are stored on 44. - -Shelley'); -INSERT INTO email([from],[to],subject,body) VALUES('sara.davidson@enron.com', 'john.schwartzenburg@enron.com, scott.dieball@enron.com, recipients@enron.com,', '2001 Enron Law Conference (Distribution List 2)', ' Enron Law Conference - -San Antonio, Texas May 2-4, 2001 Westin Riverwalk - - See attached memo for more details!! - - -? Registration for the law conference this year will be handled through an -Online RSVP Form on the Enron Law Conference Website at -http://lawconference.corp.enron.com. The website is still under construction -and will not be available until Thursday, March 15, 2001. - -? We will send you another e-mail to confirm when the Law Conference Website -is operational. - -? Please complete the Online RSVP Form as soon as it is available and submit -it no later than Friday, March 30th. - - - - -'); -INSERT INTO email([from],[to],subject,body) VALUES('tori.kuykendall@enron.com', 'heath.b.taylor@accenture.com', 'Re:', 'hey - thats funny about john - he definitely remembers him - i''ll call pat -and let him know - we are coming on saturday - i just havent had a chance to -call you guys back -- looking forward to it -- i probably need the -directions again though'); -INSERT INTO email([from],[to],subject,body) VALUES('darron.giron@enron.com', 'bryce.baxter@enron.com', 'Re: Feedback for Audrey Cook', 'Bryce, - -I''ll get it done today. - -DG 3-9573 - - - - - - From: Bryce Baxter 06/12/2000 07:15 PM - - -To: Darron C Giron/HOU/ECT@ECT -cc: -Subject: Feedback for Audrey Cook - -You were identified as a reviewer for Audrey Cook. If possible, could you -complete her feedback by end of business Wednesday? It will really help me -in the PRC process to have your input. Thanks. - -'); -INSERT INTO email([from],[to],subject,body) VALUES('casey.evans@enron.com', 'stephanie.sever@enron.com', 'Gas EOL ID', 'Stephanie, - -In conjunction with the recent movement of several power traders, they are changing the names of their gas books as well. The names of the new gas books and traders are as follows: - -PWR-NG-LT-SPP: Mike Carson -PWR-NG-LT-SERC: Jeff King - -If you need to know their power desk to map their ID to their gas books, those desks are as follows: - -EPMI-LT-SPP: Mike Carson -EPMI-LT-SERC: Jeff King - -I will be in training this afternoon, but will be back when class is over. Let me know if you have any questions. - -Thanks for your help! -Casey'); -INSERT INTO email([from],[to],subject,body) VALUES('darrell.schoolcraft@enron.com', 'david.roensch@enron.com, kimberly.watson@enron.com, michelle.lokay@enron.com,', 'Postings', 'Please see the attached. - - -ds - - - - - '); -INSERT INTO email([from],[to],subject,body) VALUES('mcominsky@aol.com', 'cpatman@bracepatt.com, james_derrick@enron.com', 'Jurisprudence Luncheon', 'Carrin & Jim -- - -It was an honor and a pleasure to meet both of you yesterday. I know we will -have fun working together on this very special event. - -Jeff left the jurisprudence luncheon lists for me before he left on vacation. - I wasn''t sure whether he transmitted them to you as well. Would you please -advise me if you would like them sent to you? I can email the MS Excel files -or I can fax the hard copies to you. Please advise what is most convenient. - -I plan to be in town through the holidays and can be reached by phone, email, -or cell phone at any time. My cell phone number is 713/705-4829. - -Thanks again for your interest in the ADL''s work. Martin. - -Martin B. Cominsky -Director, Southwest Region -Anti-Defamation League -713/627-3490, ext. 122 -713/627-2011 (fax) -MCominsky@aol.com'); -INSERT INTO email([from],[to],subject,body) VALUES('phillip.love@enron.com', 'todagost@utmb.edu, gbsonnta@utmb.edu', 'New President', 'I had a little bird put a word in my ear. Is there any possibility for Ben -Raimer to be Bush''s secretary of HHS? Just curious about that infamous UTMB -rumor mill. Hope things are well, happy holidays. -PL'); -INSERT INTO email([from],[to],subject,body) VALUES('marie.heard@enron.com', 'ehamilton@fna.com', 'ISDA Master Agreement', 'Erin: - -Pursuant to your request, attached are the Schedule to the ISDA Master Agreement, together with Paragraph 13 to the ISDA Credit Support Annex. Please let me know if you need anything else. We look forward to hearing your comments. - -Marie - -Marie Heard -Senior Legal Specialist -Enron North America Corp. -Phone: (713) 853-3907 -Fax: (713) 646-3490 -marie.heard@enron.com - - '); -INSERT INTO email([from],[to],subject,body) VALUES('andrea.ring@enron.com', 'beverly.beaty@enron.com', 'Re: Tennessee Buy - Louis Dreyfus', 'Beverly - once again thanks so much for your help on this. - - - - '); -INSERT INTO email([from],[to],subject,body) VALUES('karolyn.criado@enron.com', 'j..bonin@enron.com, felicia.case@enron.com, b..clapp@enron.com,', 'Price List week of Oct. 8-9, 2001', ' -Please contact me if you have any questions regarding last weeks prices. - -Thank you, -Karolyn Criado -3-9441 - - - - -'); -INSERT INTO email([from],[to],subject,body) VALUES('kevin.presto@enron.com', 'edward.baughman@enron.com, billy.braddock@enron.com', 'Associated', 'Please begin working on filling our Associated short position in 02. I would like to take this risk off the books. - -In addition, please find out what a buy-out of VEPCO would cost us. With Rogers transitioning to run our retail risk management, I would like to clean up our customer positions. - -We also need to continue to explore a JEA buy-out. - -Thanks.'); -INSERT INTO email([from],[to],subject,body) VALUES('stacy.dickson@enron.com', 'gregg.penman@enron.com', 'RE: Constellation TC 5-7-01', 'Gregg, - -I am at home with a sick baby. (Lots of fun!) I will call you about this -tomorrow. - -Stacy'); -INSERT INTO email([from],[to],subject,body) VALUES('joe.quenet@enron.com', 'dfincher@utilicorp.com', '', 'hey big guy.....check this out..... - - w ww.gorelieberman-2000.com/'); -INSERT INTO email([from],[to],subject,body) VALUES('k..allen@enron.com', 'jacqestc@aol.com', '', 'Jacques, - -I sent you a fax of Kevin Kolb''s comments on the release. The payoff on the note would be $36,248 ($36090(principal) + $158 (accrued interest)). -This is assuming we wrap this up on Tuesday. - -Please email to confirm that their changes are ok so I can set up a meeting on Tuesday to reach closure. - -Phillip'); -INSERT INTO email([from],[to],subject,body) VALUES('kourtney.nelson@enron.com', 'mike.swerzbin@enron.com', 'Adjusted L/R Balance', 'Mike, - -I placed the adjusted L/R Balance on the Enronwest site. It is under the "Staff/Kourtney Nelson". There are two links: - -1) "Adj L_R" is the same data/format from the weekly strategy meeting. -2) "New Gen 2001_2002" link has all of the supply side info that is used to calculate the L/R balance - -Please note the Data Flag column, a value of "3" indicates the project was cancelled, on hold, etc and is not included in the calc. - -Both of these sheets are interactive Excel spreadsheets and thus you can play around with the data as you please. Also, James Bruce is working to get his gen report on the web. That will help with your access to information on new gen. - -Please let me know if you have any questions or feedback, - -Kourtney - - - -Kourtney Nelson -Fundamental Analysis -Enron North America -(503) 464-8280 -kourtney.nelson@enron.com'); -INSERT INTO email([from],[to],subject,body) VALUES('d..thomas@enron.com', 'naveed.ahmed@enron.com', 'FW: Current Enron TCC Portfolio', ' - ------Original Message----- -From: Grace, Rebecca M. -Sent: Monday, December 17, 2001 9:44 AM -To: Thomas, Paul D. -Cc: Cashion, Jim; Allen, Thresa A.; May, Tom -Subject: RE: Current Enron TCC Portfolio - - -Paul, - -I reviewed NY''s list. I agree with all of their contracts numbers and mw amounts. - -Call if you have any more questions. - -Rebecca - - - - -----Original Message----- -From: Thomas, Paul D. -Sent: Monday, December 17, 2001 9:08 AM -To: Grace, Rebecca M. -Subject: FW: Current Enron TCC Portfolio - - << File: enrontccs.xls >> -Rebecca, -Let me know if you see any differences. - -Paul -X 3-0403 ------Original Message----- -From: Thomas, Paul D. -Sent: Monday, December 17, 2001 9:04 AM -To: Ahmed, Naveed -Subject: FW: Current Enron TCC Portfolio - - - - ------Original Message----- -From: Thomas, Paul D. -Sent: Thursday, December 13, 2001 10:01 AM -To: Baughman, Edward D. -Subject: Current Enron TCC Portfolio - - -'); -INSERT INTO email([from],[to],subject,body) VALUES('stephanie.panus@enron.com', 'william.bradford@enron.com, debbie.brackett@enron.com,', 'Coastal Merchant Energy/El Paso Merchant Energy', 'Coastal Merchant Energy, L.P. merged with and into El Paso Merchant Energy, -L.P., effective February 1, 2001, with the surviving entity being El Paso -Merchant Energy, L.P. We currently have ISDA Master Agreements with both -counterparties. Please see the attached memo regarding the existing Masters -and let us know which agreement should be terminated. - -Thanks, -Stephanie -'); -INSERT INTO email([from],[to],subject,body) VALUES('kam.keiser@enron.com', 'c..kenne@enron.com', 'RE: What about this too???', ' - - -----Original Message----- -From: Kenne, Dawn C. -Sent: Wednesday, February 06, 2002 11:50 AM -To: Keiser, Kam -Subject: What about this too??? - - - << File: Netco Trader Matrix.xls >> - '); -INSERT INTO email([from],[to],subject,body) VALUES('chris.meyer@enron.com', 'joe.parks@enron.com', 'Centana', 'Talked to Chip. We do need Cash Committe approval given the netting feature of your deal, which means Batch Funding Request. Please update per my previous e-mail and forward. - -Thanks - -chris -x31666'); -INSERT INTO email([from],[to],subject,body) VALUES('debra.perlingiere@enron.com', 'jworman@academyofhealth.com', '', 'Have a great weekend! Happy Fathers Day! - - -Debra Perlingiere -Enron North America Corp. -1400 Smith Street, EB 3885 -Houston, Texas 77002 -dperlin@enron.com -Phone 713-853-7658 -Fax 713-646-3490'); -INSERT INTO email([from],[to],subject,body) VALUES('outlook.team@enron.com', '', 'Demo by Martha Janousek of Dashboard & Pipeline Profile / Julia &', 'CALENDAR ENTRY: APPOINTMENT - -Description: - Demo by Martha Janousek of Dashboard & Pipeline Profile / Julia & Dir Rpts. - 4102 - -Date: 1/5/2001 -Time: 9:00 AM - 10:00 AM (Central Standard Time) - -Chairperson: Outlook Migration Team - -Detailed Description:'); -INSERT INTO email([from],[to],subject,body) VALUES('diana.seifert@enron.com', 'mark.taylor@enron.com', 'Guest access Chile', 'Hello Mark, - -Justin Boyd told me that your can help me with questions regarding Chile. -We got a request for guest access through MG. -The company is called Escondida and is a subsidiary of BHP Australia. - -Please advise if I can set up a guest account or not. -F.Y.I.: MG is planning to put a "in w/h Chile" contract for Copper on-line as -soon as Enron has done the due diligence for this country. -Thanks ! - - -Best regards - -Diana Seifert -EOL PCG'); -INSERT INTO email([from],[to],subject,body) VALUES('enron_update@concureworkplace.com', 'mark.whitt@enron.com', '<<Concur Expense Document>> - 121001', 'The Approval status has changed on the following report: - -Status last changed by: Barry L. Tycholiz -Expense Report Name: 121001 -Report Total: $198.98 -Amount Due Employee: $198.98 -Amount Approved: $198.98 -Amount Paid: $0.00 -Approval Status: Approved -Payment Status: Pending - - -To review this expense report, click on the following link for Concur Expense. -http://expensexms.enron.com'); -INSERT INTO email([from],[to],subject,body) VALUES('kevin.hyatt@enron.com', '', 'Technical Support', 'Outside the U.S., please refer to the list below: - -Australia: -1800 678-515 -support@palm-au.com - -Canada: -1905 305-6530 -support@palm.com - -New Zealand: -0800 446-398 -support@palm-nz.com - -U.K.: -0171 867 0108 -eurosupport@palm.3com.com - -Please refer to the Worldwide Customer Support card for a complete technical support contact list.'); -INSERT INTO email([from],[to],subject,body) VALUES('geoff.storey@enron.com', 'dutch.quigley@enron.com', 'RE:', 'duke contact? - - -----Original Message----- -From: Quigley, Dutch -Sent: Wednesday, October 31, 2001 10:14 AM -To: Storey, Geoff -Subject: RE: - -bp corp Albert LaMore 281-366-4962 - -running the reports now - - - -----Original Message----- -From: Storey, Geoff -Sent: Wednesday, October 31, 2001 10:10 AM -To: Quigley, Dutch -Subject: RE: - -give me a contact over there too -BP - - - -----Original Message----- -From: Quigley, Dutch -Sent: Wednesday, October 31, 2001 9:42 AM -To: Storey, Geoff -Subject: - -Coral Jeff Whitnah 713-767-5374 -Relaint Steve McGinn 713-207-4000'); -INSERT INTO email([from],[to],subject,body) VALUES('pete.davis@enron.com', 'pete.davis@enron.com', 'Start Date: 4/22/01; HourAhead hour: 3; <CODESITE>', 'Start Date: 4/22/01; HourAhead hour: 3; No ancillary schedules awarded. -Variances detected. -Variances detected in Load schedule. - - LOG MESSAGES: - -PARSING FILE -->> O:\Portland\WestDesk\California Scheduling\ISO Final -Schedules\2001042203.txt - ----- Load Schedule ---- -$$$ Variance found in table tblLoads. - Details: (Hour: 3 / Preferred: 1.92 / Final: 1.89) - TRANS_TYPE: FINAL - LOAD_ID: PGE4 - MKT_TYPE: 2 - TRANS_DATE: 4/22/01 - SC_ID: EPMI - -'); -INSERT INTO email([from],[to],subject,body) VALUES('john.postlethwaite@enron.com', 'john.zufferli@enron.com', 'Reference', 'John, hope things are going well up there for you. The big day is almost here for you and Jessica. I was wondering if I could use your name as a job reference if need be. I am just trying to get everything in order just in case something happens. - -John'); -INSERT INTO email([from],[to],subject,body) VALUES('jeffrey.shankman@enron.com', 'lschiffm@jonesday.com', 'Re:', 'I saw you called on the cell this a.m. Sorry I missed you. (I was in the -shower). I have had a shitty week--I suspect my silence (not only to you, -but others) after our phone call is a result of the week. I''m seeing Glen at -11:15....talk to you'); -INSERT INTO email([from],[to],subject,body) VALUES('litebytz@enron.com', '', 'Lite Bytz RSVP', ' -This week''s Lite Bytz presentation will feature the following TOOLZ speaker: - -Richard McDougall -Solaris 8 -Thursday, June 7, 2001 - -If you have not already signed up, please RSVP via email to litebytz@enron.com by the end of the day Tuesday, June 5, 2001. - -*Remember: this is now a Brown Bag Event--so bring your lunch and we will provide cookies and drinks. - -Click below for more details. - -http://home.enron.com:84/messaging/litebytztoolzprint.jpg'); - COMMIT; - } -} {} - -############################################################################### -# Everything above just builds an interesting test database. The actual -# tests come after this comment. -############################################################################### - -do_test fts1c-1.2 { - execsql { - SELECT rowid FROM email WHERE email MATCH 'mark' - } -} {6 17 25 38 40 42 73 74} -do_test fts1c-1.3 { - execsql { - SELECT rowid FROM email WHERE email MATCH 'susan' - } -} {24 40} -do_test fts1c-1.4 { - execsql { - SELECT rowid FROM email WHERE email MATCH 'mark susan' - } -} {40} -do_test fts1c-1.5 { - execsql { - SELECT rowid FROM email WHERE email MATCH 'susan mark' - } -} {40} -do_test fts1c-1.6 { - execsql { - SELECT rowid FROM email WHERE email MATCH '"mark susan"' - } -} {} -do_test fts1c-1.7 { - execsql { - SELECT rowid FROM email WHERE email MATCH 'mark -susan' - } -} {6 17 25 38 42 73 74} -do_test fts1c-1.8 { - execsql { - SELECT rowid FROM email WHERE email MATCH '-mark susan' - } -} {24} -do_test fts1c-1.9 { - execsql { - SELECT rowid FROM email WHERE email MATCH 'mark OR susan' - } -} {6 17 24 25 38 40 42 73 74} - -# Some simple tests of the automatic "offsets(email)" column. In the sample -# data set above, only one message, number 20, contains the words -# "gas" and "reminder" in both body and subject. -# -do_test fts1c-2.1 { - execsql { - SELECT rowid, offsets(email) FROM email - WHERE email MATCH 'gas reminder' - } -} {20 {2 0 42 3 2 1 54 8 3 0 42 3 3 1 54 8 3 0 129 3 3 0 143 3 3 0 240 3}} -do_test fts1c-2.2 { - execsql { - SELECT rowid, offsets(email) FROM email - WHERE email MATCH 'subject:gas reminder' - } -} {20 {2 0 42 3 2 1 54 8 3 1 54 8}} -do_test fts1c-2.3 { - execsql { - SELECT rowid, offsets(email) FROM email - WHERE email MATCH 'body:gas reminder' - } -} {20 {2 1 54 8 3 0 42 3 3 1 54 8 3 0 129 3 3 0 143 3 3 0 240 3}} -do_test fts1c-2.4 { - execsql { - SELECT rowid, offsets(email) FROM email - WHERE subject MATCH 'gas reminder' - } -} {20 {2 0 42 3 2 1 54 8}} -do_test fts1c-2.5 { - execsql { - SELECT rowid, offsets(email) FROM email - WHERE body MATCH 'gas reminder' - } -} {20 {3 0 42 3 3 1 54 8 3 0 129 3 3 0 143 3 3 0 240 3}} - -# Document 32 contains 5 instances of the world "child". But only -# 3 of them are paired with "product". Make sure only those instances -# that match the phrase appear in the offsets(email) list. -# -do_test fts1c-3.1 { - execsql { - SELECT rowid, offsets(email) FROM email - WHERE body MATCH 'child product' AND +rowid=32 - } -} {32 {3 0 94 5 3 0 114 5 3 0 207 5 3 1 213 7 3 0 245 5 3 1 251 7 3 0 409 5 3 1 415 7 3 1 493 7}} -do_test fts1c-3.2 { - execsql { - SELECT rowid, offsets(email) FROM email - WHERE body MATCH '"child product"' - } -} {32 {3 0 207 5 3 1 213 7 3 0 245 5 3 1 251 7 3 0 409 5 3 1 415 7}} - -# Snippet generator tests -# -do_test fts1c-4.1 { - execsql { - SELECT snippet(email) FROM email - WHERE email MATCH 'subject:gas reminder' - } -} {{Alert Posted 10:00 AM November 20,2000: E-<b>GAS</b> Request <b>Reminder</b>}} -do_test fts1c-4.2 { - execsql { - SELECT snippet(email) FROM email - WHERE email MATCH 'christmas candlelight' - } -} {{<b>...</b> place.? What do you think about going here <b>Christmas</b> -eve?? They have an 11:00 a.m. service and a <b>candlelight</b> service at 5:00 p.m., -among others. <b>...</b>}} - -do_test fts1c-4.3 { - execsql { - SELECT snippet(email) FROM email - WHERE email MATCH 'deal sheet potential reuse' - } -} {{EOL-Accenture <b>Deal</b> <b>Sheet</b> <b>...</b> intent - Review Enron asset base for <b>potential</b> <b>reuse</b>/ licensing - Contract negotiations <b>...</b>}} -do_test fts1c-4.4 { - execsql { - SELECT snippet(email,'<<<','>>>',' ') FROM email - WHERE email MATCH 'deal sheet potential reuse' - } -} {{EOL-Accenture <<<Deal>>> <<<Sheet>>> intent - Review Enron asset base for <<<potential>>> <<<reuse>>>/ licensing - Contract negotiations }} -do_test fts1c-4.5 { - execsql { - SELECT snippet(email,'<<<','>>>',' ') FROM email - WHERE email MATCH 'first things' - } -} {{Re: <<<First>>> Polish Deal! Congrats! <<<Things>>> seem to be building rapidly now on the }} -do_test fts1c-4.6 { - execsql { - SELECT snippet(email) FROM email - WHERE email MATCH 'chris is here' - } -} {{<b>chris</b>.germany@enron.com <b>...</b> Sounds good to me. I bet this <b>is</b> next to the Warick?? Hotel. <b>...</b> place.? What do you think about going <b>here</b> Christmas -eve?? They have an 11:00 a.m. <b>...</b>}} -do_test fts1c-4.7 { - execsql { - SELECT snippet(email) FROM email - WHERE email MATCH '"pursuant to"' - } -} {{Erin: - -<b>Pursuant</b> <b>to</b> your request, attached are the Schedule to <b>...</b>}} -do_test fts1c-4.8 { - execsql { - SELECT snippet(email) FROM email - WHERE email MATCH 'ancillary load davis' - } -} {{pete.<b>davis</b>@enron.com <b>...</b> Start Date: 4/22/01; HourAhead hour: 3; No <b>ancillary</b> schedules awarded. -Variances detected. -Variances detected in <b>Load</b> schedule. - - LOG MESSAGES: - -PARSING <b>...</b>}} - -# Combinations of AND and OR operators: -# -do_test fts1c-5.1 { - execsql { - SELECT snippet(email) FROM email - WHERE email MATCH 'questar enron OR com' - } -} {{matt.smith@<b>enron</b>.<b>com</b> <b>...</b> six reports: - -31 Keystone Receipts -15 <b>Questar</b> Pipeline -40 Rockies Production -22 West_2 <b>...</b>}} -do_test fts1c-5.2 { - execsql { - SELECT snippet(email) FROM email - WHERE email MATCH 'enron OR com questar' - } -} {{matt.smith@<b>enron</b>.<b>com</b> <b>...</b> six reports: - -31 Keystone Receipts -15 <b>Questar</b> Pipeline -40 Rockies Production -22 West_2 <b>...</b>}} - -finish_test diff --git a/test/fts1d.test b/test/fts1d.test deleted file mode 100644 index ea2303489c..0000000000 --- a/test/fts1d.test +++ /dev/null @@ -1,65 +0,0 @@ -# 2006 October 1 -# -# The author disclaims copyright to this source code. In place of -# a legal notice, here is a blessing: -# -# May you do good and not evil. -# May you find forgiveness for yourself and forgive others. -# May you share freely, never taking more than you give. -# -#************************************************************************* -# This file implements regression tests for SQLite library. The -# focus of this script is testing the FTS1 module, and in particular -# the Porter stemmer. -# -# $Id: fts1d.test,v 1.1 2006/10/01 18:41:21 drh Exp $ -# - -set testdir [file dirname $argv0] -source $testdir/tester.tcl - -# If SQLITE_ENABLE_FTS1 is defined, omit this file. -ifcapable !fts1 { - finish_test - return -} - -do_test fts1d-1.1 { - execsql { - CREATE VIRTUAL TABLE t1 USING fts1(content, tokenize porter); - INSERT INTO t1(rowid, content) VALUES(1, 'running and jumping'); - SELECT rowid FROM t1 WHERE content MATCH 'run jump'; - } -} {1} -do_test fts1d-1.2 { - execsql { - SELECT snippet(t1) FROM t1 WHERE t1 MATCH 'run jump'; - } -} {{<b>running</b> and <b>jumping</b>}} -do_test fts1d-1.3 { - execsql { - INSERT INTO t1(rowid, content) - VALUES(2, 'abcdefghijklmnopqrstuvwyxz'); - SELECT rowid, snippet(t1) FROM t1 WHERE t1 MATCH 'abcdefghijqrstuvwyxz' - } -} {2 <b>abcdefghijklmnopqrstuvwyxz</b>} -do_test fts1d-1.4 { - execsql { - SELECT rowid, snippet(t1) FROM t1 WHERE t1 MATCH 'abcdefghijXXXXqrstuvwyxz' - } -} {2 <b>abcdefghijklmnopqrstuvwyxz</b>} -do_test fts1d-1.5 { - execsql { - INSERT INTO t1(rowid, content) - VALUES(3, 'The value is 123456789'); - SELECT rowid, snippet(t1) FROM t1 WHERE t1 MATCH '123789' - } -} {3 {The value is <b>123456789</b>}} -do_test fts1d-1.6 { - execsql { - SELECT rowid, snippet(t1) FROM t1 WHERE t1 MATCH '123000000789' - } -} {3 {The value is <b>123456789</b>}} - - -finish_test diff --git a/test/fts1e.test b/test/fts1e.test deleted file mode 100644 index 479cfac91d..0000000000 --- a/test/fts1e.test +++ /dev/null @@ -1,85 +0,0 @@ -# 2006 October 19 -# -# The author disclaims copyright to this source code. -# -#************************************************************************* -# This file implements regression tests for SQLite library. The -# focus of this script is testing deletions in the FTS1 module. -# -# $Id: fts1e.test,v 1.1 2006/10/19 23:28:35 shess Exp $ -# - -set testdir [file dirname $argv0] -source $testdir/tester.tcl - -# If SQLITE_ENABLE_FTS1 is defined, omit this file. -ifcapable !fts1 { - finish_test - return -} - -# Construct a full-text search table containing keywords which are the -# ordinal numbers of the bit positions set for a sequence of integers, -# which are used for the rowid. There are a total of 30 INSERT and -# DELETE statements, so that we'll test both the segmentMerge() merge -# (over the first 16) and the termSelect() merge (over the level-1 -# segment and 14 level-0 segments). -db eval { - CREATE VIRTUAL TABLE t1 USING fts1(content); - INSERT INTO t1 (rowid, content) VALUES(1, 'one'); - INSERT INTO t1 (rowid, content) VALUES(2, 'two'); - INSERT INTO t1 (rowid, content) VALUES(3, 'one two'); - INSERT INTO t1 (rowid, content) VALUES(4, 'three'); - DELETE FROM t1 WHERE rowid = 1; - INSERT INTO t1 (rowid, content) VALUES(5, 'one three'); - INSERT INTO t1 (rowid, content) VALUES(6, 'two three'); - INSERT INTO t1 (rowid, content) VALUES(7, 'one two three'); - DELETE FROM t1 WHERE rowid = 4; - INSERT INTO t1 (rowid, content) VALUES(8, 'four'); - INSERT INTO t1 (rowid, content) VALUES(9, 'one four'); - INSERT INTO t1 (rowid, content) VALUES(10, 'two four'); - DELETE FROM t1 WHERE rowid = 7; - INSERT INTO t1 (rowid, content) VALUES(11, 'one two four'); - INSERT INTO t1 (rowid, content) VALUES(12, 'three four'); - INSERT INTO t1 (rowid, content) VALUES(13, 'one three four'); - DELETE FROM t1 WHERE rowid = 10; - INSERT INTO t1 (rowid, content) VALUES(14, 'two three four'); - INSERT INTO t1 (rowid, content) VALUES(15, 'one two three four'); - INSERT INTO t1 (rowid, content) VALUES(16, 'five'); - DELETE FROM t1 WHERE rowid = 13; - INSERT INTO t1 (rowid, content) VALUES(17, 'one five'); - INSERT INTO t1 (rowid, content) VALUES(18, 'two five'); - INSERT INTO t1 (rowid, content) VALUES(19, 'one two five'); - DELETE FROM t1 WHERE rowid = 16; - INSERT INTO t1 (rowid, content) VALUES(20, 'three five'); - INSERT INTO t1 (rowid, content) VALUES(21, 'one three five'); - INSERT INTO t1 (rowid, content) VALUES(22, 'two three five'); - DELETE FROM t1 WHERE rowid = 19; - DELETE FROM t1 WHERE rowid = 22; -} - -do_test fts1f-1.1 { - execsql {SELECT COUNT(*) FROM t1} -} {14} - -do_test fts1e-2.1 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'one'} -} {3 5 9 11 15 17 21} - -do_test fts1e-2.2 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'two'} -} {2 3 6 11 14 15 18} - -do_test fts1e-2.3 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'three'} -} {5 6 12 14 15 20 21} - -do_test fts1e-2.4 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'four'} -} {8 9 11 12 14 15} - -do_test fts1e-2.5 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'five'} -} {17 18 20 21} - -finish_test diff --git a/test/fts1f.test b/test/fts1f.test deleted file mode 100644 index 19dea0a329..0000000000 --- a/test/fts1f.test +++ /dev/null @@ -1,90 +0,0 @@ -# 2006 October 19 -# -# The author disclaims copyright to this source code. -# -#************************************************************************* -# This file implements regression tests for SQLite library. The -# focus of this script is testing updates in the FTS1 module. -# -# $Id: fts1f.test,v 1.2 2007/02/23 00:14:06 shess Exp $ -# - -set testdir [file dirname $argv0] -source $testdir/tester.tcl - -# If SQLITE_ENABLE_FTS1 is defined, omit this file. -ifcapable !fts1 { - finish_test - return -} - -# Construct a full-text search table containing keywords which are the -# ordinal numbers of the bit positions set for a sequence of integers, -# which are used for the rowid. There are a total of 31 INSERT, -# UPDATE, and DELETE statements, so that we'll test both the -# segmentMerge() merge (over the first 16) and the termSelect() merge -# (over the level-1 segment and 15 level-0 segments). -db eval { - CREATE VIRTUAL TABLE t1 USING fts1(content); - INSERT INTO t1 (rowid, content) VALUES(1, 'one'); - INSERT INTO t1 (rowid, content) VALUES(2, 'two'); - INSERT INTO t1 (rowid, content) VALUES(3, 'one two'); - INSERT INTO t1 (rowid, content) VALUES(4, 'three'); - INSERT INTO t1 (rowid, content) VALUES(5, 'one three'); - INSERT INTO t1 (rowid, content) VALUES(6, 'two three'); - INSERT INTO t1 (rowid, content) VALUES(7, 'one two three'); - DELETE FROM t1 WHERE rowid = 4; - INSERT INTO t1 (rowid, content) VALUES(8, 'four'); - UPDATE t1 SET content = 'update one three' WHERE rowid = 1; - INSERT INTO t1 (rowid, content) VALUES(9, 'one four'); - INSERT INTO t1 (rowid, content) VALUES(10, 'two four'); - DELETE FROM t1 WHERE rowid = 7; - INSERT INTO t1 (rowid, content) VALUES(11, 'one two four'); - INSERT INTO t1 (rowid, content) VALUES(12, 'three four'); - INSERT INTO t1 (rowid, content) VALUES(13, 'one three four'); - DELETE FROM t1 WHERE rowid = 10; - INSERT INTO t1 (rowid, content) VALUES(14, 'two three four'); - INSERT INTO t1 (rowid, content) VALUES(15, 'one two three four'); - UPDATE t1 SET content = 'update two five' WHERE rowid = 8; - INSERT INTO t1 (rowid, content) VALUES(16, 'five'); - DELETE FROM t1 WHERE rowid = 13; - INSERT INTO t1 (rowid, content) VALUES(17, 'one five'); - INSERT INTO t1 (rowid, content) VALUES(18, 'two five'); - INSERT INTO t1 (rowid, content) VALUES(19, 'one two five'); - DELETE FROM t1 WHERE rowid = 16; - INSERT INTO t1 (rowid, content) VALUES(20, 'three five'); - INSERT INTO t1 (rowid, content) VALUES(21, 'one three five'); - INSERT INTO t1 (rowid, content) VALUES(22, 'two three five'); - DELETE FROM t1 WHERE rowid = 19; - UPDATE t1 SET content = 'update' WHERE rowid = 15; -} - -do_test fts1f-1.1 { - execsql {SELECT COUNT(*) FROM t1} -} {16} - -do_test fts1f-2.0 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'update'} -} {1 8 15} - -do_test fts1f-2.1 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'one'} -} {1 3 5 9 11 17 21} - -do_test fts1f-2.2 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'two'} -} {2 3 6 8 11 14 18 22} - -do_test fts1f-2.3 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'three'} -} {1 5 6 12 14 20 21 22} - -do_test fts1f-2.4 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'four'} -} {9 11 12 14} - -do_test fts1f-2.5 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'five'} -} {8 17 18 20 21 22} - -finish_test diff --git a/test/fts1i.test b/test/fts1i.test deleted file mode 100644 index 803b93bf29..0000000000 --- a/test/fts1i.test +++ /dev/null @@ -1,88 +0,0 @@ -# 2007 January 17 -# -# The author disclaims copyright to this source code. -# -#************************************************************************* -# This file implements regression tests for SQLite fts1 library. The -# focus here is testing handling of UPDATE when using UTF-16-encoded -# databases. -# -# $Id: fts1i.test,v 1.2 2007/01/24 03:43:20 drh Exp $ -# - -set testdir [file dirname $argv0] -source $testdir/tester.tcl - -# If SQLITE_ENABLE_FTS1 is defined, omit this file. -ifcapable !fts1 { - finish_test - return -} - - -# Return the UTF-16 representation of the supplied UTF-8 string $str. -# If $nt is true, append two 0x00 bytes as a nul terminator. -# NOTE(shess) Copied from capi3.test. -proc utf16 {str {nt 1}} { - set r [encoding convertto unicode $str] - if {$nt} { - append r "\x00\x00" - } - return $r -} - -db eval { - PRAGMA encoding = "UTF-16le"; - CREATE VIRTUAL TABLE t1 USING fts1(content); -} - -do_test fts1i-1.0 { - execsql {PRAGMA encoding} -} {UTF-16le} - -do_test fts1i-1.1 { - execsql {INSERT INTO t1 (rowid, content) VALUES(1, 'one')} - execsql {SELECT content FROM t1 WHERE rowid = 1} -} {one} - -do_test fts1i-1.2 { - set sql "INSERT INTO t1 (rowid, content) VALUES(2, 'two')" - set STMT [sqlite3_prepare $DB $sql -1 TAIL] - sqlite3_step $STMT - sqlite3_finalize $STMT - execsql {SELECT content FROM t1 WHERE rowid = 2} -} {two} - -do_test fts1i-1.3 { - set sql "INSERT INTO t1 (rowid, content) VALUES(3, 'three')" - set STMT [sqlite3_prepare $DB $sql -1 TAIL] - sqlite3_step $STMT - sqlite3_finalize $STMT - set sql "UPDATE t1 SET content = 'trois' WHERE rowid = 3" - set STMT [sqlite3_prepare $DB $sql -1 TAIL] - sqlite3_step $STMT - sqlite3_finalize $STMT - execsql {SELECT content FROM t1 WHERE rowid = 3} -} {trois} - -do_test fts1i-1.4 { - set sql16 [utf16 {INSERT INTO t1 (rowid, content) VALUES(4, 'four')}] - set STMT [sqlite3_prepare16 $DB $sql16 -1 TAIL] - sqlite3_step $STMT - sqlite3_finalize $STMT - execsql {SELECT content FROM t1 WHERE rowid = 4} -} {four} - -do_test fts1i-1.5 { - set sql16 [utf16 {INSERT INTO t1 (rowid, content) VALUES(5, 'five')}] - set STMT [sqlite3_prepare16 $DB $sql16 -1 TAIL] - sqlite3_step $STMT - sqlite3_finalize $STMT - set sql "UPDATE t1 SET content = 'cinq' WHERE rowid = 5" - set STMT [sqlite3_prepare $DB $sql -1 TAIL] - sqlite3_step $STMT - sqlite3_finalize $STMT - execsql {SELECT content FROM t1 WHERE rowid = 5} -} {cinq} - -finish_test diff --git a/test/fts1j.test b/test/fts1j.test deleted file mode 100644 index 4dac22abbf..0000000000 --- a/test/fts1j.test +++ /dev/null @@ -1,89 +0,0 @@ -# 2007 February 6 -# -# The author disclaims copyright to this source code. -# -#************************************************************************* -# This file implements regression tests for SQLite library. This -# tests creating fts1 tables in an attached database. -# -# $Id: fts1j.test,v 1.1 2007/02/07 01:01:18 shess Exp $ -# - -set testdir [file dirname $argv0] -source $testdir/tester.tcl - -# If SQLITE_ENABLE_FTS1 is defined, omit this file. -ifcapable !fts1 { - finish_test - return -} - -# Clean up anything left over from a previous pass. -forcedelete test2.db -forcedelete test2.db-journal -sqlite3 db2 test2.db - -db eval { - CREATE VIRTUAL TABLE t3 USING fts1(content); - INSERT INTO t3 (rowid, content) VALUES(1, "hello world"); -} - -db2 eval { - CREATE VIRTUAL TABLE t1 USING fts1(content); - INSERT INTO t1 (rowid, content) VALUES(1, "hello world"); - INSERT INTO t1 (rowid, content) VALUES(2, "hello there"); - INSERT INTO t1 (rowid, content) VALUES(3, "cruel world"); -} - -# This has always worked because the t1_* tables used by fts1 will be -# the defaults. -do_test fts1j-1.1 { - execsql { - ATTACH DATABASE 'test2.db' AS two; - SELECT rowid FROM t1 WHERE t1 MATCH 'hello'; - DETACH DATABASE two; - } -} {1 2} -# Make certain we're detached if there was an error. -catch {db eval {DETACH DATABASE two}} - -# In older code, this appears to work fine, but the t2_* tables used -# by fts1 will be created in database 'main' instead of database -# 'two'. It appears to work fine because the tables end up being the -# defaults, but obviously is badly broken if you hope to use things -# other than in the exact same ATTACH setup. -do_test fts1j-1.2 { - execsql { - ATTACH DATABASE 'test2.db' AS two; - CREATE VIRTUAL TABLE two.t2 USING fts1(content); - INSERT INTO t2 (rowid, content) VALUES(1, "hello world"); - INSERT INTO t2 (rowid, content) VALUES(2, "hello there"); - INSERT INTO t2 (rowid, content) VALUES(3, "cruel world"); - SELECT rowid FROM t2 WHERE t2 MATCH 'hello'; - DETACH DATABASE two; - } -} {1 2} -catch {db eval {DETACH DATABASE two}} - -# In older code, this broke because the fts1 code attempted to create -# t3_* tables in database 'main', but they already existed. Normally -# this wouldn't happen without t3 itself existing, in which case the -# fts1 code would never be called in the first place. -do_test fts1j-1.3 { - execsql { - ATTACH DATABASE 'test2.db' AS two; - - CREATE VIRTUAL TABLE two.t3 USING fts1(content); - INSERT INTO two.t3 (rowid, content) VALUES(2, "hello there"); - INSERT INTO two.t3 (rowid, content) VALUES(3, "cruel world"); - SELECT rowid FROM two.t3 WHERE t3 MATCH 'hello'; - - DETACH DATABASE two; - } db2 -} {2} -catch {db eval {DETACH DATABASE two}} - -catch {db2 close} -forcedelete test2.db - -finish_test diff --git a/test/fts1k.test b/test/fts1k.test deleted file mode 100644 index 35b94d2ae7..0000000000 --- a/test/fts1k.test +++ /dev/null @@ -1,69 +0,0 @@ -# 2007 March 28 -# -# The author disclaims copyright to this source code. -# -#************************************************************************* -# This file implements regression tests for SQLite library. The focus -# of this script is testing isspace/isalnum/tolower problems with the -# FTS1 module. Unfortunately, this code isn't a really principled set -# of tests, because it is impossible to know where new uses of these -# functions might appear. -# -# $Id: fts1k.test,v 1.2 2007/12/13 21:54:11 drh Exp $ -# - -set testdir [file dirname $argv0] -source $testdir/tester.tcl - -# If SQLITE_ENABLE_FTS1 is defined, omit this file. -ifcapable !fts1 { - finish_test - return -} - -# Tests that startsWith() (calls isspace, tolower, isalnum) can handle -# hi-bit chars. parseSpec() also calls isalnum here. -do_test fts1k-1.1 { - execsql "CREATE VIRTUAL TABLE t1 USING fts1(content, \x80)" -} {} - -# Additionally tests isspace() call in getToken(), and isalnum() call -# in tokenListToIdList(). -do_test fts1k-1.2 { - catch { - execsql "CREATE VIRTUAL TABLE t2 USING fts1(content, tokenize \x80)" - } - sqlite3_errmsg $DB -} "unknown tokenizer: \x80" - -# Additionally test final isalnum() in startsWith(). -do_test fts1k-1.3 { - execsql "CREATE VIRTUAL TABLE t3 USING fts1(content, tokenize\x80)" -} {} - -# The snippet-generation code has calls to isspace() which are sort of -# hard to get to. It finds convenient breakpoints by starting ~40 -# chars before and after the matched term, and scanning ~10 chars -# around that position for isspace() characters. The long word with -# embedded hi-bit chars causes one of these isspace() calls to be -# exercised. The version with a couple extra spaces should cause the -# other isspace() call to be exercised. [Both cases have been tested -# in the debugger, but I'm hoping to continue to catch it if simple -# constant changes change things slightly. -# -# The trailing and leading hi-bit chars help with code which tests for -# isspace() to coalesce multiple spaces. - -set word "\x80xxxxx\x80xxxxx\x80xxxxx\x80xxxxx\x80xxxxx\x80xxxxx\x80" -set phrase1 "$word $word $word target $word $word $word" -set phrase2 "$word $word $word target $word $word $word" - -db eval {CREATE VIRTUAL TABLE t4 USING fts1(content)} -db eval "INSERT INTO t4 (content) VALUES ('$phrase1')" -db eval "INSERT INTO t4 (content) VALUES ('$phrase2')" - -do_test fts1k-1.4 { - execsql {SELECT rowid, length(snippet(t4)) FROM t4 WHERE t4 MATCH 'target'} -} {1 111 2 117} - -finish_test diff --git a/test/fts1l.test b/test/fts1l.test deleted file mode 100644 index 924be33801..0000000000 --- a/test/fts1l.test +++ /dev/null @@ -1,65 +0,0 @@ -# 2007 April 9 -# -# The author disclaims copyright to this source code. -# -#************************************************************************* -# This file implements regression tests for SQLite library. fts1 -# DELETE handling assumed all fields were non-null. This was not -# the intention at all. -# -# $Id: fts1l.test,v 1.1 2007/04/09 20:45:42 shess Exp $ -# - -set testdir [file dirname $argv0] -source $testdir/tester.tcl - -# If SQLITE_ENABLE_FTS1 is defined, omit this file. -ifcapable !fts1 { - finish_test - return -} - -db eval { - CREATE VIRTUAL TABLE t1 USING fts1(col_a, col_b); - - INSERT INTO t1(rowid, col_a, col_b) VALUES(1, 'testing', 'testing'); - INSERT INTO t1(rowid, col_a, col_b) VALUES(2, 'only a', null); - INSERT INTO t1(rowid, col_a, col_b) VALUES(3, null, 'only b'); - INSERT INTO t1(rowid, col_a, col_b) VALUES(4, null, null); -} - -do_test fts1m-1.0 { - execsql { - SELECT COUNT(col_a), COUNT(col_b), COUNT(*) FROM t1; - } -} {2 2 4} - -do_test fts1m-1.1 { - execsql { - DELETE FROM t1 WHERE rowid = 1; - SELECT COUNT(col_a), COUNT(col_b), COUNT(*) FROM t1; - } -} {1 1 3} - -do_test fts1m-1.2 { - execsql { - DELETE FROM t1 WHERE rowid = 2; - SELECT COUNT(col_a), COUNT(col_b), COUNT(*) FROM t1; - } -} {0 1 2} - -do_test fts1m-1.3 { - execsql { - DELETE FROM t1 WHERE rowid = 3; - SELECT COUNT(col_a), COUNT(col_b), COUNT(*) FROM t1; - } -} {0 0 1} - -do_test fts1m-1.4 { - execsql { - DELETE FROM t1 WHERE rowid = 4; - SELECT COUNT(col_a), COUNT(col_b), COUNT(*) FROM t1; - } -} {0 0 0} - -finish_test diff --git a/test/fts1m.test b/test/fts1m.test deleted file mode 100644 index c2f8f915a1..0000000000 --- a/test/fts1m.test +++ /dev/null @@ -1,50 +0,0 @@ -# 2007 July 27 -# -# The author disclaims copyright to this source code. In place of -# a legal notice, here is a blessing: -# -# May you do good and not evil. -# May you find forgiveness for yourself and forgive others. -# May you share freely, never taking more than you give. -# -#************************************************************************* -# This file implements regression tests for SQLite library. The focus -# of this script is testing the FTS1 module, specifically snippet -# generation. Extracted from fts2o.test. -# -# $Id: fts1m.test,v 1.1 2007/07/25 00:25:20 shess Exp $ -# - -set testdir [file dirname $argv0] -source $testdir/tester.tcl - -# If SQLITE_ENABLE_FTS1 is not defined, omit this file. -ifcapable !fts1 { - finish_test - return -} - -#--------------------------------------------------------------------- -# These tests, fts1m-1.*, test that ticket #2429 is fixed. -# -db eval { - CREATE VIRTUAL TABLE t1 USING fts1(a, b, c); - INSERT INTO t1(a, b, c) VALUES('one three four', 'one four', 'one four two'); -} -do_test fts1m-1.1 { - execsql { - SELECT rowid, snippet(t1) FROM t1 WHERE c MATCH 'four'; - } -} {1 {one <b>four</b> two}} -do_test fts1m-1.2 { - execsql { - SELECT rowid, snippet(t1) FROM t1 WHERE b MATCH 'four'; - } -} {1 {one <b>four</b>}} -do_test fts1m-1.3 { - execsql { - SELECT rowid, snippet(t1) FROM t1 WHERE a MATCH 'four'; - } -} {1 {one three <b>four</b>}} - -finish_test diff --git a/test/fts1n.test b/test/fts1n.test deleted file mode 100644 index 2f102b4e2c..0000000000 --- a/test/fts1n.test +++ /dev/null @@ -1,45 +0,0 @@ -# 2007 July 24 -# -# The author disclaims copyright to this source code. In place of -# a legal notice, here is a blessing: -# -# May you do good and not evil. -# May you find forgiveness for yourself and forgive others. -# May you share freely, never taking more than you give. -# -#************************************************************************* -# This file implements regression tests for SQLite library. The focus -# of this script is testing the FTS1 module for errors in the handling -# of SQLITE_SCHEMA. -# -# $Id: fts1n.test,v 1.1 2007/07/25 00:38:06 shess Exp $ -# - -set testdir [file dirname $argv0] -source $testdir/tester.tcl - -# If SQLITE_ENABLE_FTS1 is not defined, omit this file. -ifcapable !fts1 { - finish_test - return -} - -do_test fts1m-1.1 { - execsql { - CREATE VIRTUAL TABLE t1 USING fts1(a, b, c); - INSERT INTO t1(a, b, c) VALUES('one three four', 'one four', 'one two'); - SELECT a, b, c FROM t1 WHERE c MATCH 'two'; - } -} {{one three four} {one four} {one two}} - -# This test was crashing at one point. -# -do_test fts1m-1.2 { - execsql { - SELECT a, b, c FROM t1 WHERE c MATCH 'two'; - CREATE TABLE t3(a, b, c); - SELECT a, b, c FROM t1 WHERE c MATCH 'two'; - } -} {{one three four} {one four} {one two} {one three four} {one four} {one two}} - -finish_test diff --git a/test/fts1o.test b/test/fts1o.test deleted file mode 100644 index 61fd74ca0e..0000000000 --- a/test/fts1o.test +++ /dev/null @@ -1,138 +0,0 @@ -# 2007 July 24 -# -# The author disclaims copyright to this source code. In place of -# a legal notice, here is a blessing: -# -# May you do good and not evil. -# May you find forgiveness for yourself and forgive others. -# May you share freely, never taking more than you give. -# -#************************************************************************* -# This file implements regression tests for SQLite library. The focus -# of this script is testing the FTS1 module rename functionality. Mostly -# copied from fts2o.test. -# -# $Id: fts1o.test,v 1.2 2007/08/30 20:01:33 shess Exp $ -# - -set testdir [file dirname $argv0] -source $testdir/tester.tcl - -# If SQLITE_ENABLE_FTS1 is not defined, omit this file. -ifcapable !fts1 { - finish_test - return -} - -db eval { - CREATE VIRTUAL TABLE t1 USING fts1(a, b, c); - INSERT INTO t1(a, b, c) VALUES('one three four', 'one four', 'one four two'); -} - -#--------------------------------------------------------------------- -# Test that it is possible to rename an fts1 table. -# -do_test fts1o-1.1 { - execsql { SELECT tbl_name FROM sqlite_master WHERE type = 'table'} -} {t1 t1_content t1_term} -do_test fts1o-1.2 { - execsql { ALTER TABLE t1 RENAME to fts_t1; } -} {} -do_test fts1o-1.3 { - execsql { SELECT rowid, snippet(fts_t1) FROM fts_t1 WHERE a MATCH 'four'; } -} {1 {one three <b>four</b>}} -do_test fts1o-1.4 { - execsql { SELECT tbl_name FROM sqlite_master WHERE type = 'table'} -} {fts_t1 fts_t1_content fts_t1_term} - -# See what happens when renaming the fts1 table fails. -# -do_test fts1o-2.1 { - catchsql { - CREATE TABLE t1_term(a, b, c); - ALTER TABLE fts_t1 RENAME to t1; - } -} {1 {SQL logic error or missing database}} -do_test fts1o-2.2 { - execsql { SELECT rowid, snippet(fts_t1) FROM fts_t1 WHERE a MATCH 'four'; } -} {1 {one three <b>four</b>}} -do_test fts1o-2.3 { - execsql { SELECT tbl_name FROM sqlite_master WHERE type = 'table'} -} {fts_t1 fts_t1_content fts_t1_term t1_term} - -# See what happens when renaming the fts1 table fails inside a transaction. -# -do_test fts1o-3.1 { - execsql { - BEGIN; - INSERT INTO fts_t1(a, b, c) VALUES('one two three', 'one four', 'one two'); - } -} {} -do_test fts1o-3.2 { - catchsql { - ALTER TABLE fts_t1 RENAME to t1; - } -} {1 {SQL logic error or missing database}} -# NOTE(shess) rowid AS rowid to defeat caching. Otherwise, this -# seg-faults, I suspect that there's something up with a stale -# virtual-table reference, but I'm not quite sure how it happens here -# but not for fts2o.test. -do_test fts1o-3.3 { - execsql { SELECT rowid AS rowid, snippet(fts_t1) FROM fts_t1 WHERE a MATCH 'four'; } -} {1 {one three <b>four</b>}} -do_test fts1o-3.4 { - execsql { SELECT tbl_name FROM sqlite_master WHERE type = 'table'} -} {fts_t1 fts_t1_content fts_t1_term t1_term} -do_test fts1o-3.5 { - execsql COMMIT - execsql {SELECT a FROM fts_t1} -} {{one three four} {one two three}} -do_test fts1o-3.6 { - execsql { SELECT a, b, c FROM fts_t1 WHERE c MATCH 'four'; } -} {{one three four} {one four} {one four two}} - -#--------------------------------------------------------------------- -# Test that it is possible to rename an fts1 table in an attached -# database. -# -forcedelete test2.db test2.db-journal - -do_test fts1o-4.1 { - execsql { - DROP TABLE t1_term; - ALTER TABLE fts_t1 RENAME to t1; - SELECT a, b, c FROM t1 WHERE c MATCH 'two'; - } -} {{one three four} {one four} {one four two} {one two three} {one four} {one two}} - -do_test fts1o-4.2 { - execsql { - ATTACH 'test2.db' AS aux; - CREATE VIRTUAL TABLE aux.t1 USING fts1(a, b, c); - INSERT INTO aux.t1(a, b, c) VALUES( - 'neung song sahm', 'neung see', 'neung see song' - ); - } -} {} - -do_test fts1o-4.3 { - execsql { SELECT a, b, c FROM aux.t1 WHERE a MATCH 'song'; } -} {{neung song sahm} {neung see} {neung see song}} - -do_test fts1o-4.4 { - execsql { SELECT a, b, c FROM t1 WHERE c MATCH 'two'; } -} {{one three four} {one four} {one four two} {one two three} {one four} {one two}} - -do_test fts1o-4.5 { - execsql { ALTER TABLE aux.t1 RENAME TO t2 } -} {} - -do_test fts1o-4.6 { - execsql { SELECT a, b, c FROM t2 WHERE a MATCH 'song'; } -} {{neung song sahm} {neung see} {neung see song}} - -do_test fts1o-4.7 { - execsql { SELECT a, b, c FROM t1 WHERE c MATCH 'two'; } -} {{one three four} {one four} {one four two} {one two three} {one four} {one two}} - -finish_test diff --git a/test/fts1porter.test b/test/fts1porter.test deleted file mode 100644 index 0ca87a01ed..0000000000 --- a/test/fts1porter.test +++ /dev/null @@ -1,23590 +0,0 @@ -# 2006 October 1 -# -# The author disclaims copyright to this source code. In place of -# a legal notice, here is a blessing: -# -# May you do good and not evil. -# May you find forgiveness for yourself and forgive others. -# May you share freely, never taking more than you give. -# -#************************************************************************* -# This file implements regression tests for SQLite library. The -# focus of this script is testing the FTS1 module, and in particular -# the Porter stemmer. -# -# $Id: fts1porter.test,v 1.5 2006/10/03 19:37:37 drh Exp $ -# - -set testdir [file dirname $argv0] -source $testdir/tester.tcl - -# If SQLITE_ENABLE_FTS1 is defined, omit this file. -ifcapable !fts1 { - finish_test - return -} - -# Test data for the Porter stemmer. The first word of each line -# is the input. The second word is the desired output. -# -# This test data is taken from http://www.tartarus.org/martin/PorterStemmer/ -# There is no claim of copyright made on that page, but you should -# probably contact the author (Martin Porter - the inventor of the -# Porter Stemmer algorithm) if you want to use this test data in a -# commerical product of some kind. The stemmer code in FTS1 is a -# complete rewrite from scratch based on the algorithm specification -# and does not contain any code under copyright. -# -set porter_test_data { - a a - aaron aaron - abaissiez abaissiez - abandon abandon - abandoned abandon - abase abas - abash abash - abate abat - abated abat - abatement abat - abatements abat - abates abat - abbess abbess - abbey abbei - abbeys abbei - abbominable abbomin - abbot abbot - abbots abbot - abbreviated abbrevi - abed ab - abel abel - aberga aberga - abergavenny abergavenni - abet abet - abetting abet - abhominable abhomin - abhor abhor - abhorr abhorr - abhorred abhor - abhorring abhor - abhors abhor - abhorson abhorson - abide abid - abides abid - abilities abil - ability abil - abject abject - abjectly abjectli - abjects abject - abjur abjur - abjure abjur - able abl - abler abler - aboard aboard - abode abod - aboded abod - abodements abod - aboding abod - abominable abomin - abominably abomin - abominations abomin - abortive abort - abortives abort - abound abound - abounding abound - about about - above abov - abr abr - abraham abraham - abram abram - abreast abreast - abridg abridg - abridge abridg - abridged abridg - abridgment abridg - abroach abroach - abroad abroad - abrogate abrog - abrook abrook - abrupt abrupt - abruption abrupt - abruptly abruptli - absence absenc - absent absent - absey absei - absolute absolut - absolutely absolut - absolv absolv - absolver absolv - abstains abstain - abstemious abstemi - abstinence abstin - abstract abstract - absurd absurd - absyrtus absyrtu - abundance abund - abundant abund - abundantly abundantli - abus abu - abuse abus - abused abus - abuser abus - abuses abus - abusing abus - abutting abut - aby abi - abysm abysm - ac ac - academe academ - academes academ - accent accent - accents accent - accept accept - acceptable accept - acceptance accept - accepted accept - accepts accept - access access - accessary accessari - accessible access - accidence accid - accident accid - accidental accident - accidentally accident - accidents accid - accite accit - accited accit - accites accit - acclamations acclam - accommodate accommod - accommodated accommod - accommodation accommod - accommodations accommod - accommodo accommodo - accompanied accompani - accompany accompani - accompanying accompani - accomplices accomplic - accomplish accomplish - accomplished accomplish - accomplishing accomplish - accomplishment accomplish - accompt accompt - accord accord - accordant accord - accorded accord - accordeth accordeth - according accord - accordingly accordingli - accords accord - accost accost - accosted accost - account account - accountant account - accounted account - accounts account - accoutred accoutr - accoutrement accoutr - accoutrements accoutr - accrue accru - accumulate accumul - accumulated accumul - accumulation accumul - accurs accur - accursed accurs - accurst accurst - accus accu - accusation accus - accusations accus - accusative accus - accusativo accusativo - accuse accus - accused accus - accuser accus - accusers accus - accuses accus - accuseth accuseth - accusing accus - accustom accustom - accustomed accustom - ace ac - acerb acerb - ache ach - acheron acheron - aches ach - achiev achiev - achieve achiev - achieved achiev - achievement achiev - achievements achiev - achiever achiev - achieves achiev - achieving achiev - achilles achil - aching ach - achitophel achitophel - acknowledg acknowledg - acknowledge acknowledg - acknowledged acknowledg - acknowledgment acknowledg - acknown acknown - acold acold - aconitum aconitum - acordo acordo - acorn acorn - acquaint acquaint - acquaintance acquaint - acquainted acquaint - acquaints acquaint - acquir acquir - acquire acquir - acquisition acquisit - acquit acquit - acquittance acquitt - acquittances acquitt - acquitted acquit - acre acr - acres acr - across across - act act - actaeon actaeon - acted act - acting act - action action - actions action - actium actium - active activ - actively activ - activity activ - actor actor - actors actor - acts act - actual actual - acture actur - acute acut - acutely acut - ad ad - adage adag - adallas adalla - adam adam - adamant adam - add add - added ad - adder adder - adders adder - addeth addeth - addict addict - addicted addict - addiction addict - adding ad - addition addit - additions addit - addle addl - address address - addressing address - addrest addrest - adds add - adhere adher - adheres adher - adieu adieu - adieus adieu - adjacent adjac - adjoin adjoin - adjoining adjoin - adjourn adjourn - adjudg adjudg - adjudged adjudg - adjunct adjunct - administer administ - administration administr - admir admir - admirable admir - admiral admir - admiration admir - admire admir - admired admir - admirer admir - admiring admir - admiringly admiringli - admission admiss - admit admit - admits admit - admittance admitt - admitted admit - admitting admit - admonish admonish - admonishing admonish - admonishment admonish - admonishments admonish - admonition admonit - ado ado - adonis adoni - adopt adopt - adopted adopt - adoptedly adoptedli - adoption adopt - adoptious adopti - adopts adopt - ador ador - adoration ador - adorations ador - adore ador - adorer ador - adores ador - adorest adorest - adoreth adoreth - adoring ador - adorn adorn - adorned adorn - adornings adorn - adornment adorn - adorns adorn - adown adown - adramadio adramadio - adrian adrian - adriana adriana - adriano adriano - adriatic adriat - adsum adsum - adulation adul - adulterate adulter - adulterates adulter - adulterers adulter - adulteress adulteress - adulteries adulteri - adulterous adulter - adultery adulteri - adultress adultress - advanc advanc - advance advanc - advanced advanc - advancement advanc - advancements advanc - advances advanc - advancing advanc - advantage advantag - advantageable advantag - advantaged advantag - advantageous advantag - advantages advantag - advantaging advantag - advent advent - adventur adventur - adventure adventur - adventures adventur - adventuring adventur - adventurous adventur - adventurously adventur - adversaries adversari - adversary adversari - adverse advers - adversely advers - adversities advers - adversity advers - advertis adverti - advertise advertis - advertised advertis - advertisement advertis - advertising advertis - advice advic - advis advi - advise advis - advised advis - advisedly advisedli - advises advis - advisings advis - advocate advoc - advocation advoc - aeacida aeacida - aeacides aeacid - aedile aedil - aediles aedil - aegeon aegeon - aegion aegion - aegles aegl - aemelia aemelia - aemilia aemilia - aemilius aemiliu - aeneas aenea - aeolus aeolu - aer aer - aerial aerial - aery aeri - aesculapius aesculapiu - aeson aeson - aesop aesop - aetna aetna - afar afar - afear afear - afeard afeard - affability affabl - affable affabl - affair affair - affaire affair - affairs affair - affect affect - affectation affect - affectations affect - affected affect - affectedly affectedli - affecteth affecteth - affecting affect - affection affect - affectionate affection - affectionately affection - affections affect - affects affect - affeer affeer - affianc affianc - affiance affianc - affianced affianc - affied affi - affin affin - affined affin - affinity affin - affirm affirm - affirmation affirm - affirmatives affirm - afflict afflict - afflicted afflict - affliction afflict - afflictions afflict - afflicts afflict - afford afford - affordeth affordeth - affords afford - affray affrai - affright affright - affrighted affright - affrights affright - affront affront - affronted affront - affy affi - afield afield - afire afir - afloat afloat - afoot afoot - afore afor - aforehand aforehand - aforesaid aforesaid - afraid afraid - afresh afresh - afric afric - africa africa - african african - afront afront - after after - afternoon afternoon - afterward afterward - afterwards afterward - ag ag - again again - against against - agamemmon agamemmon - agamemnon agamemnon - agate agat - agaz agaz - age ag - aged ag - agenor agenor - agent agent - agents agent - ages ag - aggravate aggrav - aggrief aggrief - agile agil - agincourt agincourt - agitation agit - aglet aglet - agnize agniz - ago ago - agone agon - agony agoni - agree agre - agreed agre - agreeing agre - agreement agreement - agrees agre - agrippa agrippa - aground aground - ague agu - aguecheek aguecheek - agued agu - agueface aguefac - agues agu - ah ah - aha aha - ahungry ahungri - ai ai - aialvolio aialvolio - aiaria aiaria - aid aid - aidance aidanc - aidant aidant - aided aid - aiding aid - aidless aidless - aids aid - ail ail - aim aim - aimed aim - aimest aimest - aiming aim - aims aim - ainsi ainsi - aio aio - air air - aired air - airless airless - airs air - airy airi - ajax ajax - akilling akil - al al - alabaster alabast - alack alack - alacrity alacr - alarbus alarbu - alarm alarm - alarms alarm - alarum alarum - alarums alarum - alas ala - alb alb - alban alban - albans alban - albany albani - albeit albeit - albion albion - alchemist alchemist - alchemy alchemi - alcibiades alcibiad - alcides alcid - alder alder - alderman alderman - aldermen aldermen - ale al - alecto alecto - alehouse alehous - alehouses alehous - alencon alencon - alengon alengon - aleppo aleppo - ales al - alewife alewif - alexander alexand - alexanders alexand - alexandria alexandria - alexandrian alexandrian - alexas alexa - alias alia - alice alic - alien alien - aliena aliena - alight alight - alighted alight - alights alight - aliis alii - alike alik - alisander alisand - alive aliv - all all - alla alla - allay allai - allayed allai - allaying allai - allayment allay - allayments allay - allays allai - allegation alleg - allegations alleg - allege alleg - alleged alleg - allegiance allegi - allegiant allegi - alley allei - alleys allei - allhallowmas allhallowma - alliance allianc - allicholy allicholi - allied alli - allies alli - alligant allig - alligator allig - allons allon - allot allot - allots allot - allotted allot - allottery allotteri - allow allow - allowance allow - allowed allow - allowing allow - allows allow - allur allur - allure allur - allurement allur - alluring allur - allusion allus - ally alli - allycholly allycholli - almain almain - almanac almanac - almanack almanack - almanacs almanac - almighty almighti - almond almond - almost almost - alms alm - almsman almsman - aloes alo - aloft aloft - alone alon - along along - alonso alonso - aloof aloof - aloud aloud - alphabet alphabet - alphabetical alphabet - alphonso alphonso - alps alp - already alreadi - also also - alt alt - altar altar - altars altar - alter alter - alteration alter - altered alter - alters alter - althaea althaea - although although - altitude altitud - altogether altogeth - alton alton - alway alwai - always alwai - am am - amaimon amaimon - amain amain - amaking amak - amamon amamon - amaz amaz - amaze amaz - amazed amaz - amazedly amazedli - amazedness amazed - amazement amaz - amazes amaz - amazeth amazeth - amazing amaz - amazon amazon - amazonian amazonian - amazons amazon - ambassador ambassador - ambassadors ambassador - amber amber - ambiguides ambiguid - ambiguities ambigu - ambiguous ambigu - ambition ambit - ambitions ambit - ambitious ambiti - ambitiously ambiti - amble ambl - ambled ambl - ambles ambl - ambling ambl - ambo ambo - ambuscadoes ambuscado - ambush ambush - amen amen - amend amend - amended amend - amendment amend - amends amend - amerce amerc - america america - ames am - amiable amiabl - amid amid - amidst amidst - amiens amien - amis ami - amiss amiss - amities amiti - amity amiti - amnipotent amnipot - among among - amongst amongst - amorous amor - amorously amor - amort amort - amount amount - amounts amount - amour amour - amphimacus amphimacu - ample ampl - ampler ampler - amplest amplest - amplified amplifi - amplify amplifi - amply ampli - ampthill ampthil - amurath amurath - amyntas amynta - an an - anatomiz anatomiz - anatomize anatom - anatomy anatomi - ancestor ancestor - ancestors ancestor - ancestry ancestri - anchises anchis - anchor anchor - anchorage anchorag - anchored anchor - anchoring anchor - anchors anchor - anchovies anchovi - ancient ancient - ancientry ancientri - ancients ancient - ancus ancu - and and - andirons andiron - andpholus andpholu - andren andren - andrew andrew - andromache andromach - andronici andronici - andronicus andronicu - anew anew - ang ang - angel angel - angelica angelica - angelical angel - angelo angelo - angels angel - anger anger - angerly angerli - angers anger - anges ang - angiers angier - angl angl - anglais anglai - angle angl - angler angler - angleterre angleterr - angliae anglia - angling angl - anglish anglish - angrily angrili - angry angri - anguish anguish - angus angu - animal anim - animals anim - animis animi - anjou anjou - ankle ankl - anna anna - annals annal - anne ann - annex annex - annexed annex - annexions annexion - annexment annex - annothanize annothan - announces announc - annoy annoi - annoyance annoy - annoying annoi - annual annual - anoint anoint - anointed anoint - anon anon - another anoth - anselmo anselmo - answer answer - answerable answer - answered answer - answerest answerest - answering answer - answers answer - ant ant - ante ant - antenor antenor - antenorides antenorid - anteroom anteroom - anthem anthem - anthems anthem - anthony anthoni - anthropophagi anthropophagi - anthropophaginian anthropophaginian - antiates antiat - antic antic - anticipate anticip - anticipates anticip - anticipatest anticipatest - anticipating anticip - anticipation anticip - antick antick - anticly anticli - antics antic - antidote antidot - antidotes antidot - antigonus antigonu - antiopa antiopa - antipathy antipathi - antipholus antipholu - antipholuses antipholus - antipodes antipod - antiquary antiquari - antique antiqu - antiquity antiqu - antium antium - antoniad antoniad - antonio antonio - antonius antoniu - antony antoni - antres antr - anvil anvil - any ani - anybody anybodi - anyone anyon - anything anyth - anywhere anywher - ap ap - apace apac - apart apart - apartment apart - apartments apart - ape ap - apemantus apemantu - apennines apennin - apes ap - apiece apiec - apish apish - apollinem apollinem - apollo apollo - apollodorus apollodoru - apology apolog - apoplex apoplex - apoplexy apoplexi - apostle apostl - apostles apostl - apostrophas apostropha - apoth apoth - apothecary apothecari - appal appal - appall appal - appalled appal - appals appal - apparel apparel - apparell apparel - apparelled apparel - apparent appar - apparently appar - apparition apparit - apparitions apparit - appeach appeach - appeal appeal - appeals appeal - appear appear - appearance appear - appeared appear - appeareth appeareth - appearing appear - appears appear - appeas appea - appease appeas - appeased appeas - appelant appel - appele appel - appelee appele - appeles appel - appelez appelez - appellant appel - appellants appel - appelons appelon - appendix appendix - apperil apperil - appertain appertain - appertaining appertain - appertainings appertain - appertains appertain - appertinent appertin - appertinents appertin - appetite appetit - appetites appetit - applaud applaud - applauded applaud - applauding applaud - applause applaus - applauses applaus - apple appl - apples appl - appletart appletart - appliance applianc - appliances applianc - applications applic - applied appli - applies appli - apply appli - applying appli - appoint appoint - appointed appoint - appointment appoint - appointments appoint - appoints appoint - apprehend apprehend - apprehended apprehend - apprehends apprehend - apprehension apprehens - apprehensions apprehens - apprehensive apprehens - apprendre apprendr - apprenne apprenn - apprenticehood apprenticehood - appris appri - approach approach - approachers approach - approaches approach - approacheth approacheth - approaching approach - approbation approb - approof approof - appropriation appropri - approv approv - approve approv - approved approv - approvers approv - approves approv - appurtenance appurten - appurtenances appurten - apricocks apricock - april april - apron apron - aprons apron - apt apt - apter apter - aptest aptest - aptly aptli - aptness apt - aqua aqua - aquilon aquilon - aquitaine aquitain - arabia arabia - arabian arabian - araise arais - arbitrate arbitr - arbitrating arbitr - arbitrator arbitr - arbitrement arbitr - arbors arbor - arbour arbour - arc arc - arch arch - archbishop archbishop - archbishopric archbishopr - archdeacon archdeacon - arched arch - archelaus archelau - archer archer - archers archer - archery archeri - archibald archibald - archidamus archidamu - architect architect - arcu arcu - arde ard - arden arden - ardent ardent - ardour ardour - are ar - argal argal - argier argier - argo argo - argosies argosi - argosy argosi - argu argu - argue argu - argued argu - argues argu - arguing argu - argument argument - arguments argument - argus argu - ariachne ariachn - ariadne ariadn - ariel ariel - aries ari - aright aright - arinado arinado - arinies arini - arion arion - arise aris - arises aris - ariseth ariseth - arising aris - aristode aristod - aristotle aristotl - arithmetic arithmet - arithmetician arithmetician - ark ark - arm arm - arma arma - armado armado - armadoes armado - armagnac armagnac - arme arm - armed arm - armenia armenia - armies armi - armigero armigero - arming arm - armipotent armipot - armor armor - armour armour - armourer armour - armourers armour - armours armour - armoury armouri - arms arm - army armi - arn arn - aroint aroint - arose aros - arouse arous - aroused arous - arragon arragon - arraign arraign - arraigned arraign - arraigning arraign - arraignment arraign - arrant arrant - arras arra - array arrai - arrearages arrearag - arrest arrest - arrested arrest - arrests arrest - arriv arriv - arrival arriv - arrivance arriv - arrive arriv - arrived arriv - arrives arriv - arriving arriv - arrogance arrog - arrogancy arrog - arrogant arrog - arrow arrow - arrows arrow - art art - artemidorus artemidoru - arteries arteri - arthur arthur - article articl - articles articl - articulate articul - artificer artific - artificial artifici - artillery artilleri - artire artir - artist artist - artists artist - artless artless - artois artoi - arts art - artus artu - arviragus arviragu - as as - asaph asaph - ascanius ascaniu - ascend ascend - ascended ascend - ascendeth ascendeth - ascends ascend - ascension ascens - ascent ascent - ascribe ascrib - ascribes ascrib - ash ash - asham asham - ashamed asham - asher asher - ashes ash - ashford ashford - ashore ashor - ashouting ashout - ashy ashi - asia asia - aside asid - ask ask - askance askanc - asked ask - asker asker - asketh asketh - asking ask - asks ask - aslant aslant - asleep asleep - asmath asmath - asp asp - aspect aspect - aspects aspect - aspen aspen - aspersion aspers - aspic aspic - aspicious aspici - aspics aspic - aspir aspir - aspiration aspir - aspire aspir - aspiring aspir - asquint asquint - ass ass - assail assail - assailable assail - assailant assail - assailants assail - assailed assail - assaileth assaileth - assailing assail - assails assail - assassination assassin - assault assault - assaulted assault - assaults assault - assay assai - assaying assai - assays assai - assemblance assembl - assemble assembl - assembled assembl - assemblies assembl - assembly assembl - assent assent - asses ass - assez assez - assign assign - assigned assign - assigns assign - assinico assinico - assist assist - assistance assist - assistances assist - assistant assist - assistants assist - assisted assist - assisting assist - associate associ - associated associ - associates associ - assuage assuag - assubjugate assubjug - assum assum - assume assum - assumes assum - assumption assumpt - assur assur - assurance assur - assure assur - assured assur - assuredly assuredli - assures assur - assyrian assyrian - astonish astonish - astonished astonish - astraea astraea - astray astrai - astrea astrea - astronomer astronom - astronomers astronom - astronomical astronom - astronomy astronomi - asunder asund - at at - atalanta atalanta - ate at - ates at - athenian athenian - athenians athenian - athens athen - athol athol - athversary athversari - athwart athwart - atlas atla - atomies atomi - atomy atomi - atone aton - atonement aton - atonements aton - atropos atropo - attach attach - attached attach - attachment attach - attain attain - attainder attaind - attains attain - attaint attaint - attainted attaint - attainture attaintur - attempt attempt - attemptable attempt - attempted attempt - attempting attempt - attempts attempt - attend attend - attendance attend - attendant attend - attendants attend - attended attend - attendents attend - attendeth attendeth - attending attend - attends attend - attent attent - attention attent - attentive attent - attentivenes attentiven - attest attest - attested attest - attir attir - attire attir - attired attir - attires attir - attorney attornei - attorneyed attornei - attorneys attornei - attorneyship attorneyship - attract attract - attraction attract - attractive attract - attracts attract - attribute attribut - attributed attribut - attributes attribut - attribution attribut - attributive attribut - atwain atwain - au au - aubrey aubrei - auburn auburn - aucun aucun - audacious audaci - audaciously audaci - audacity audac - audible audibl - audience audienc - audis audi - audit audit - auditor auditor - auditors auditor - auditory auditori - audre audr - audrey audrei - aufidius aufidiu - aufidiuses aufidius - auger auger - aught aught - augment augment - augmentation augment - augmented augment - augmenting augment - augurer augur - augurers augur - augures augur - auguring augur - augurs augur - augury auguri - august august - augustus augustu - auld auld - aumerle aumerl - aunchient aunchient - aunt aunt - aunts aunt - auricular auricular - aurora aurora - auspicious auspici - aussi aussi - austere auster - austerely auster - austereness auster - austerity auster - austria austria - aut aut - authentic authent - author author - authorities author - authority author - authorized author - authorizing author - authors author - autolycus autolycu - autre autr - autumn autumn - auvergne auvergn - avail avail - avails avail - avarice avaric - avaricious avarici - avaunt avaunt - ave av - aveng aveng - avenge aveng - avenged aveng - averring aver - avert avert - aves av - avez avez - avis avi - avoid avoid - avoided avoid - avoiding avoid - avoids avoid - avoirdupois avoirdupoi - avouch avouch - avouched avouch - avouches avouch - avouchment avouch - avow avow - aw aw - await await - awaits await - awak awak - awake awak - awaked awak - awaken awaken - awakened awaken - awakens awaken - awakes awak - awaking awak - award award - awards award - awasy awasi - away awai - awe aw - aweary aweari - aweless aweless - awful aw - awhile awhil - awkward awkward - awl awl - awooing awoo - awork awork - awry awri - axe ax - axle axl - axletree axletre - ay ay - aye ay - ayez ayez - ayli ayli - azur azur - azure azur - b b - ba ba - baa baa - babbl babbl - babble babbl - babbling babbl - babe babe - babes babe - babies babi - baboon baboon - baboons baboon - baby babi - babylon babylon - bacare bacar - bacchanals bacchan - bacchus bacchu - bach bach - bachelor bachelor - bachelors bachelor - back back - backbite backbit - backbitten backbitten - backing back - backs back - backward backward - backwardly backwardli - backwards backward - bacon bacon - bacons bacon - bad bad - bade bade - badge badg - badged badg - badges badg - badly badli - badness bad - baes bae - baffl baffl - baffle baffl - baffled baffl - bag bag - baggage baggag - bagot bagot - bagpipe bagpip - bags bag - bail bail - bailiff bailiff - baillez baillez - baily baili - baisant baisant - baisees baise - baiser baiser - bait bait - baited bait - baiting bait - baitings bait - baits bait - bajazet bajazet - bak bak - bake bake - baked bake - baker baker - bakers baker - bakes bake - baking bake - bal bal - balanc balanc - balance balanc - balcony balconi - bald bald - baldrick baldrick - bale bale - baleful bale - balk balk - ball ball - ballad ballad - ballads ballad - ballast ballast - ballasting ballast - ballet ballet - ballow ballow - balls ball - balm balm - balms balm - balmy balmi - balsam balsam - balsamum balsamum - balth balth - balthasar balthasar - balthazar balthazar - bames bame - ban ban - banbury banburi - band band - bandied bandi - banding band - bandit bandit - banditti banditti - banditto banditto - bands band - bandy bandi - bandying bandi - bane bane - banes bane - bang bang - bangor bangor - banish banish - banished banish - banishers banish - banishment banish - banister banist - bank bank - bankrout bankrout - bankrupt bankrupt - bankrupts bankrupt - banks bank - banner banner - bannerets banneret - banners banner - banning ban - banns bann - banquet banquet - banqueted banquet - banqueting banquet - banquets banquet - banquo banquo - bans ban - baptism baptism - baptista baptista - baptiz baptiz - bar bar - barbarian barbarian - barbarians barbarian - barbarism barbar - barbarous barbar - barbary barbari - barbason barbason - barbed barb - barber barber - barbermonger barbermong - bard bard - bardolph bardolph - bards bard - bare bare - bared bare - barefac barefac - barefaced barefac - barefoot barefoot - bareheaded barehead - barely bare - bareness bare - barful bar - bargain bargain - bargains bargain - barge barg - bargulus bargulu - baring bare - bark bark - barking bark - barkloughly barkloughli - barks bark - barky barki - barley barlei - barm barm - barn barn - barnacles barnacl - barnardine barnardin - barne barn - barnes barn - barnet barnet - barns barn - baron baron - barons baron - barony baroni - barr barr - barrabas barraba - barrel barrel - barrels barrel - barren barren - barrenly barrenli - barrenness barren - barricado barricado - barricadoes barricado - barrow barrow - bars bar - barson barson - barter barter - bartholomew bartholomew - bas ba - basan basan - base base - baseless baseless - basely base - baseness base - baser baser - bases base - basest basest - bashful bash - bashfulness bash - basilisco basilisco - basilisk basilisk - basilisks basilisk - basimecu basimecu - basin basin - basingstoke basingstok - basins basin - basis basi - bask bask - basket basket - baskets basket - bass bass - bassanio bassanio - basset basset - bassianus bassianu - basta basta - bastard bastard - bastardizing bastard - bastardly bastardli - bastards bastard - bastardy bastardi - basted bast - bastes bast - bastinado bastinado - basting bast - bat bat - batailles batail - batch batch - bate bate - bated bate - bates bate - bath bath - bathe bath - bathed bath - bathing bath - baths bath - bating bate - batler batler - bats bat - batt batt - battalia battalia - battalions battalion - batten batten - batter batter - battering batter - batters batter - battery batteri - battle battl - battled battl - battlefield battlefield - battlements battlement - battles battl - batty batti - bauble baubl - baubles baubl - baubling baubl - baulk baulk - bavin bavin - bawcock bawcock - bawd bawd - bawdry bawdri - bawds bawd - bawdy bawdi - bawl bawl - bawling bawl - bay bai - baying bai - baynard baynard - bayonne bayonn - bays bai - be be - beach beach - beached beach - beachy beachi - beacon beacon - bead bead - beaded bead - beadle beadl - beadles beadl - beads bead - beadsmen beadsmen - beagle beagl - beagles beagl - beak beak - beaks beak - beam beam - beamed beam - beams beam - bean bean - beans bean - bear bear - beard beard - bearded beard - beardless beardless - beards beard - bearer bearer - bearers bearer - bearest bearest - beareth beareth - bearing bear - bears bear - beast beast - beastliest beastliest - beastliness beastli - beastly beastli - beasts beast - beat beat - beated beat - beaten beaten - beating beat - beatrice beatric - beats beat - beau beau - beaufort beaufort - beaumond beaumond - beaumont beaumont - beauteous beauteou - beautied beauti - beauties beauti - beautified beautifi - beautiful beauti - beautify beautifi - beauty beauti - beaver beaver - beavers beaver - became becam - because becaus - bechanc bechanc - bechance bechanc - bechanced bechanc - beck beck - beckon beckon - beckons beckon - becks beck - becom becom - become becom - becomed becom - becomes becom - becoming becom - becomings becom - bed bed - bedabbled bedabbl - bedash bedash - bedaub bedaub - bedazzled bedazzl - bedchamber bedchamb - bedclothes bedcloth - bedded bed - bedeck bedeck - bedecking bedeck - bedew bedew - bedfellow bedfellow - bedfellows bedfellow - bedford bedford - bedlam bedlam - bedrench bedrench - bedrid bedrid - beds bed - bedtime bedtim - bedward bedward - bee bee - beef beef - beefs beef - beehives beehiv - been been - beer beer - bees bee - beest beest - beetle beetl - beetles beetl - beeves beev - befall befal - befallen befallen - befalls befal - befell befel - befits befit - befitted befit - befitting befit - befor befor - before befor - beforehand beforehand - befortune befortun - befriend befriend - befriended befriend - befriends befriend - beg beg - began began - beget beget - begets beget - begetting beget - begg begg - beggar beggar - beggared beggar - beggarly beggarli - beggarman beggarman - beggars beggar - beggary beggari - begging beg - begin begin - beginners beginn - beginning begin - beginnings begin - begins begin - begnawn begnawn - begone begon - begot begot - begotten begotten - begrimed begrim - begs beg - beguil beguil - beguile beguil - beguiled beguil - beguiles beguil - beguiling beguil - begun begun - behalf behalf - behalfs behalf - behav behav - behaved behav - behavedst behavedst - behavior behavior - behaviors behavior - behaviour behaviour - behaviours behaviour - behead behead - beheaded behead - beheld beheld - behest behest - behests behest - behind behind - behold behold - beholder behold - beholders behold - beholdest beholdest - beholding behold - beholds behold - behoof behoof - behooffull behoofful - behooves behoov - behove behov - behoves behov - behowls behowl - being be - bel bel - belarius belariu - belch belch - belching belch - beldam beldam - beldame beldam - beldams beldam - belee bele - belgia belgia - belie beli - belied beli - belief belief - beliest beliest - believ believ - believe believ - believed believ - believes believ - believest believest - believing believ - belike belik - bell bell - bellario bellario - belle bell - bellied belli - bellies belli - bellman bellman - bellona bellona - bellow bellow - bellowed bellow - bellowing bellow - bellows bellow - bells bell - belly belli - bellyful belly - belman belman - belmont belmont - belock belock - belong belong - belonging belong - belongings belong - belongs belong - belov belov - beloved belov - beloving belov - below below - belt belt - belzebub belzebub - bemadding bemad - bemet bemet - bemete bemet - bemoan bemoan - bemoaned bemoan - bemock bemock - bemoil bemoil - bemonster bemonst - ben ben - bench bench - bencher bencher - benches bench - bend bend - bended bend - bending bend - bends bend - bene bene - beneath beneath - benedicite benedicit - benedick benedick - benediction benedict - benedictus benedictu - benefactors benefactor - benefice benefic - beneficial benefici - benefit benefit - benefited benefit - benefits benefit - benetted benet - benevolence benevol - benevolences benevol - benied beni - benison benison - bennet bennet - bent bent - bentii bentii - bentivolii bentivolii - bents bent - benumbed benumb - benvolio benvolio - bepaint bepaint - bepray beprai - bequeath bequeath - bequeathed bequeath - bequeathing bequeath - bequest bequest - ber ber - berard berard - berattle berattl - beray berai - bere bere - bereave bereav - bereaved bereav - bereaves bereav - bereft bereft - bergamo bergamo - bergomask bergomask - berhym berhym - berhyme berhym - berkeley berkelei - bermoothes bermooth - bernardo bernardo - berod berod - berowne berown - berri berri - berries berri - berrord berrord - berry berri - bertram bertram - berwick berwick - bescreen bescreen - beseech beseech - beseeched beseech - beseechers beseech - beseeching beseech - beseek beseek - beseem beseem - beseemeth beseemeth - beseeming beseem - beseems beseem - beset beset - beshrew beshrew - beside besid - besides besid - besieg besieg - besiege besieg - besieged besieg - beslubber beslubb - besmear besmear - besmeared besmear - besmirch besmirch - besom besom - besort besort - besotted besot - bespake bespak - bespeak bespeak - bespice bespic - bespoke bespok - bespotted bespot - bess bess - bessy bessi - best best - bestained bestain - bested best - bestial bestial - bestir bestir - bestirr bestirr - bestow bestow - bestowed bestow - bestowing bestow - bestows bestow - bestraught bestraught - bestrew bestrew - bestrid bestrid - bestride bestrid - bestrides bestrid - bet bet - betake betak - beteem beteem - bethink bethink - bethought bethought - bethrothed bethroth - bethump bethump - betid betid - betide betid - betideth betideth - betime betim - betimes betim - betoken betoken - betook betook - betossed betoss - betray betrai - betrayed betrai - betraying betrai - betrays betrai - betrims betrim - betroth betroth - betrothed betroth - betroths betroth - bett bett - betted bet - better better - bettered better - bettering better - betters better - betting bet - bettre bettr - between between - betwixt betwixt - bevel bevel - beverage beverag - bevis bevi - bevy bevi - bewail bewail - bewailed bewail - bewailing bewail - bewails bewail - beware bewar - bewasted bewast - beweep beweep - bewept bewept - bewet bewet - bewhored bewhor - bewitch bewitch - bewitched bewitch - bewitchment bewitch - bewray bewrai - beyond beyond - bezonian bezonian - bezonians bezonian - bianca bianca - bianco bianco - bias bia - bibble bibbl - bickerings bicker - bid bid - bidden bidden - bidding bid - biddings bid - biddy biddi - bide bide - bides bide - biding bide - bids bid - bien bien - bier bier - bifold bifold - big big - bigamy bigami - biggen biggen - bigger bigger - bigness big - bigot bigot - bilberry bilberri - bilbo bilbo - bilboes bilbo - bilbow bilbow - bill bill - billeted billet - billets billet - billiards billiard - billing bill - billow billow - billows billow - bills bill - bin bin - bind bind - bindeth bindeth - binding bind - binds bind - biondello biondello - birch birch - bird bird - birding bird - birdlime birdlim - birds bird - birnam birnam - birth birth - birthday birthdai - birthdom birthdom - birthplace birthplac - birthright birthright - birthrights birthright - births birth - bis bi - biscuit biscuit - bishop bishop - bishops bishop - bisson bisson - bit bit - bitch bitch - bite bite - biter biter - bites bite - biting bite - bits bit - bitt bitt - bitten bitten - bitter bitter - bitterest bitterest - bitterly bitterli - bitterness bitter - blab blab - blabb blabb - blabbing blab - blabs blab - black black - blackamoor blackamoor - blackamoors blackamoor - blackberries blackberri - blackberry blackberri - blacker blacker - blackest blackest - blackfriars blackfriar - blackheath blackheath - blackmere blackmer - blackness black - blacks black - bladder bladder - bladders bladder - blade blade - bladed blade - blades blade - blains blain - blam blam - blame blame - blamed blame - blameful blame - blameless blameless - blames blame - blanc blanc - blanca blanca - blanch blanch - blank blank - blanket blanket - blanks blank - blaspheme blasphem - blaspheming blasphem - blasphemous blasphem - blasphemy blasphemi - blast blast - blasted blast - blasting blast - blastments blastment - blasts blast - blaz blaz - blaze blaze - blazes blaze - blazing blaze - blazon blazon - blazoned blazon - blazoning blazon - bleach bleach - bleaching bleach - bleak bleak - blear blear - bleared blear - bleat bleat - bleated bleat - bleats bleat - bled bled - bleed bleed - bleedest bleedest - bleedeth bleedeth - bleeding bleed - bleeds bleed - blemish blemish - blemishes blemish - blench blench - blenches blench - blend blend - blended blend - blent blent - bless bless - blessed bless - blessedly blessedli - blessedness blessed - blesses bless - blesseth blesseth - blessing bless - blessings bless - blest blest - blew blew - blind blind - blinded blind - blindfold blindfold - blinding blind - blindly blindli - blindness blind - blinds blind - blink blink - blinking blink - bliss bliss - blist blist - blister blister - blisters blister - blithe blith - blithild blithild - bloat bloat - block block - blockish blockish - blocks block - blois bloi - blood blood - blooded blood - bloodhound bloodhound - bloodied bloodi - bloodier bloodier - bloodiest bloodiest - bloodily bloodili - bloodless bloodless - bloods blood - bloodshed bloodsh - bloodshedding bloodshed - bloodstained bloodstain - bloody bloodi - bloom bloom - blooms bloom - blossom blossom - blossoming blossom - blossoms blossom - blot blot - blots blot - blotted blot - blotting blot - blount blount - blow blow - blowed blow - blowers blower - blowest blowest - blowing blow - blown blown - blows blow - blowse blows - blubb blubb - blubber blubber - blubbering blubber - blue blue - bluecaps bluecap - bluest bluest - blunt blunt - blunted blunt - blunter blunter - bluntest bluntest - blunting blunt - bluntly bluntli - bluntness blunt - blunts blunt - blur blur - blurr blurr - blurs blur - blush blush - blushes blush - blushest blushest - blushing blush - blust blust - bluster bluster - blusterer bluster - blusters bluster - bo bo - boar boar - board board - boarded board - boarding board - boards board - boarish boarish - boars boar - boast boast - boasted boast - boastful boast - boasting boast - boasts boast - boat boat - boats boat - boatswain boatswain - bob bob - bobb bobb - boblibindo boblibindo - bobtail bobtail - bocchus bocchu - bode bode - boded bode - bodements bodement - bodes bode - bodg bodg - bodied bodi - bodies bodi - bodiless bodiless - bodily bodili - boding bode - bodkin bodkin - body bodi - bodykins bodykin - bog bog - boggle boggl - boggler boggler - bogs bog - bohemia bohemia - bohemian bohemian - bohun bohun - boil boil - boiling boil - boils boil - boist boist - boisterous boister - boisterously boister - boitier boitier - bold bold - bolden bolden - bolder bolder - boldest boldest - boldly boldli - boldness bold - bolds bold - bolingbroke bolingbrok - bolster bolster - bolt bolt - bolted bolt - bolter bolter - bolters bolter - bolting bolt - bolts bolt - bombard bombard - bombards bombard - bombast bombast - bon bon - bona bona - bond bond - bondage bondag - bonded bond - bondmaid bondmaid - bondman bondman - bondmen bondmen - bonds bond - bondslave bondslav - bone bone - boneless boneless - bones bone - bonfire bonfir - bonfires bonfir - bonjour bonjour - bonne bonn - bonnet bonnet - bonneted bonnet - bonny bonni - bonos bono - bonto bonto - bonville bonvil - bood bood - book book - bookish bookish - books book - boon boon - boor boor - boorish boorish - boors boor - boot boot - booted boot - booties booti - bootless bootless - boots boot - booty booti - bor bor - bora bora - borachio borachio - bordeaux bordeaux - border border - bordered border - borderers border - borders border - bore bore - boreas borea - bores bore - boring bore - born born - borne born - borough borough - boroughs borough - borrow borrow - borrowed borrow - borrower borrow - borrowing borrow - borrows borrow - bosko bosko - boskos bosko - bosky boski - bosom bosom - bosoms bosom - boson boson - boss boss - bosworth bosworth - botch botch - botcher botcher - botches botch - botchy botchi - both both - bots bot - bottle bottl - bottled bottl - bottles bottl - bottom bottom - bottomless bottomless - bottoms bottom - bouciqualt bouciqualt - bouge boug - bough bough - boughs bough - bought bought - bounce bounc - bouncing bounc - bound bound - bounded bound - bounden bounden - boundeth boundeth - bounding bound - boundless boundless - bounds bound - bounteous bounteou - bounteously bounteous - bounties bounti - bountiful bounti - bountifully bountifulli - bounty bounti - bourbier bourbier - bourbon bourbon - bourchier bourchier - bourdeaux bourdeaux - bourn bourn - bout bout - bouts bout - bove bove - bow bow - bowcase bowcas - bowed bow - bowels bowel - bower bower - bowing bow - bowl bowl - bowler bowler - bowling bowl - bowls bowl - bows bow - bowsprit bowsprit - bowstring bowstr - box box - boxes box - boy boi - boyet boyet - boyish boyish - boys boi - brabant brabant - brabantio brabantio - brabble brabbl - brabbler brabbler - brac brac - brace brace - bracelet bracelet - bracelets bracelet - brach brach - bracy braci - brag brag - bragg bragg - braggardism braggard - braggards braggard - braggart braggart - braggarts braggart - bragged brag - bragging brag - bragless bragless - brags brag - braid braid - braided braid - brain brain - brained brain - brainford brainford - brainish brainish - brainless brainless - brains brain - brainsick brainsick - brainsickly brainsickli - brake brake - brakenbury brakenburi - brakes brake - brambles brambl - bran bran - branch branch - branches branch - branchless branchless - brand brand - branded brand - brandish brandish - brandon brandon - brands brand - bras bra - brass brass - brassy brassi - brat brat - brats brat - brav brav - brave brave - braved brave - bravely brave - braver braver - bravery braveri - braves brave - bravest bravest - braving brave - brawl brawl - brawler brawler - brawling brawl - brawls brawl - brawn brawn - brawns brawn - bray brai - braying brai - braz braz - brazen brazen - brazier brazier - breach breach - breaches breach - bread bread - breadth breadth - break break - breaker breaker - breakfast breakfast - breaking break - breaks break - breast breast - breasted breast - breasting breast - breastplate breastplat - breasts breast - breath breath - breathe breath - breathed breath - breather breather - breathers breather - breathes breath - breathest breathest - breathing breath - breathless breathless - breaths breath - brecknock brecknock - bred bred - breech breech - breeches breech - breeching breech - breed breed - breeder breeder - breeders breeder - breeding breed - breeds breed - breese brees - breeze breez - breff breff - bretagne bretagn - brethen brethen - bretheren bretheren - brethren brethren - brevis brevi - brevity breviti - brew brew - brewage brewag - brewer brewer - brewers brewer - brewing brew - brews brew - briareus briareu - briars briar - brib brib - bribe bribe - briber briber - bribes bribe - brick brick - bricklayer bricklay - bricks brick - bridal bridal - bride bride - bridegroom bridegroom - bridegrooms bridegroom - brides bride - bridge bridg - bridgenorth bridgenorth - bridges bridg - bridget bridget - bridle bridl - bridled bridl - brief brief - briefer briefer - briefest briefest - briefly briefli - briefness brief - brier brier - briers brier - brigandine brigandin - bright bright - brighten brighten - brightest brightest - brightly brightli - brightness bright - brim brim - brimful brim - brims brim - brimstone brimston - brinded brind - brine brine - bring bring - bringer bringer - bringeth bringeth - bringing bring - bringings bring - brings bring - brinish brinish - brink brink - brisk brisk - brisky briski - bristle bristl - bristled bristl - bristly bristli - bristol bristol - bristow bristow - britain britain - britaine britain - britaines britain - british british - briton briton - britons briton - brittany brittani - brittle brittl - broach broach - broached broach - broad broad - broader broader - broadsides broadsid - brocas broca - brock brock - brogues brogu - broil broil - broiling broil - broils broil - broke broke - broken broken - brokenly brokenli - broker broker - brokers broker - brokes broke - broking broke - brooch brooch - brooches brooch - brood brood - brooded brood - brooding brood - brook brook - brooks brook - broom broom - broomstaff broomstaff - broth broth - brothel brothel - brother brother - brotherhood brotherhood - brotherhoods brotherhood - brotherly brotherli - brothers brother - broths broth - brought brought - brow brow - brown brown - browner browner - brownist brownist - browny browni - brows brow - browse brows - browsing brows - bruis brui - bruise bruis - bruised bruis - bruises bruis - bruising bruis - bruit bruit - bruited bruit - brundusium brundusium - brunt brunt - brush brush - brushes brush - brute brute - brutish brutish - brutus brutu - bubble bubbl - bubbles bubbl - bubbling bubbl - bubukles bubukl - buck buck - bucket bucket - buckets bucket - bucking buck - buckingham buckingham - buckle buckl - buckled buckl - buckler buckler - bucklers buckler - bucklersbury bucklersburi - buckles buckl - buckram buckram - bucks buck - bud bud - budded bud - budding bud - budge budg - budger budger - budget budget - buds bud - buff buff - buffet buffet - buffeting buffet - buffets buffet - bug bug - bugbear bugbear - bugle bugl - bugs bug - build build - builded build - buildeth buildeth - building build - buildings build - builds build - built built - bulk bulk - bulks bulk - bull bull - bullcalf bullcalf - bullen bullen - bullens bullen - bullet bullet - bullets bullet - bullocks bullock - bulls bull - bully bulli - bulmer bulmer - bulwark bulwark - bulwarks bulwark - bum bum - bumbast bumbast - bump bump - bumper bumper - bums bum - bunch bunch - bunches bunch - bundle bundl - bung bung - bunghole bunghol - bungle bungl - bunting bunt - buoy buoi - bur bur - burbolt burbolt - burd burd - burden burden - burdened burden - burdening burden - burdenous burden - burdens burden - burgh burgh - burgher burgher - burghers burgher - burglary burglari - burgomasters burgomast - burgonet burgonet - burgundy burgundi - burial burial - buried buri - burier burier - buriest buriest - burly burli - burn burn - burned burn - burnet burnet - burneth burneth - burning burn - burnish burnish - burns burn - burnt burnt - burr burr - burrows burrow - burs bur - burst burst - bursting burst - bursts burst - burthen burthen - burthens burthen - burton burton - bury buri - burying buri - bush bush - bushels bushel - bushes bush - bushy bushi - busied busi - busily busili - busines busin - business busi - businesses busi - buskin buskin - busky buski - buss buss - busses buss - bussing buss - bustle bustl - bustling bustl - busy busi - but but - butcheed butche - butcher butcher - butchered butcher - butcheries butcheri - butcherly butcherli - butchers butcher - butchery butcheri - butler butler - butt butt - butter butter - buttered butter - butterflies butterfli - butterfly butterfli - butterwoman butterwoman - buttery butteri - buttock buttock - buttocks buttock - button button - buttonhole buttonhol - buttons button - buttress buttress - buttry buttri - butts butt - buxom buxom - buy bui - buyer buyer - buying bui - buys bui - buzz buzz - buzzard buzzard - buzzards buzzard - buzzers buzzer - buzzing buzz - by by - bye bye - byzantium byzantium - c c - ca ca - cabbage cabbag - cabileros cabilero - cabin cabin - cabins cabin - cable cabl - cables cabl - cackling cackl - cacodemon cacodemon - caddis caddi - caddisses caddiss - cade cade - cadence cadenc - cadent cadent - cades cade - cadmus cadmu - caduceus caduceu - cadwal cadwal - cadwallader cadwallad - caelius caeliu - caelo caelo - caesar caesar - caesarion caesarion - caesars caesar - cage cage - caged cage - cagion cagion - cain cain - caithness caith - caitiff caitiff - caitiffs caitiff - caius caiu - cak cak - cake cake - cakes cake - calaber calab - calais calai - calamities calam - calamity calam - calchas calcha - calculate calcul - calen calen - calendar calendar - calendars calendar - calf calf - caliban caliban - calibans caliban - calipolis calipoli - cality caliti - caliver caliv - call call - callat callat - called call - callet callet - calling call - calls call - calm calm - calmest calmest - calmly calmli - calmness calm - calms calm - calpurnia calpurnia - calumniate calumni - calumniating calumni - calumnious calumni - calumny calumni - calve calv - calved calv - calves calv - calveskins calveskin - calydon calydon - cam cam - cambio cambio - cambria cambria - cambric cambric - cambrics cambric - cambridge cambridg - cambyses cambys - came came - camel camel - camelot camelot - camels camel - camest camest - camillo camillo - camlet camlet - camomile camomil - camp camp - campeius campeiu - camping camp - camps camp - can can - canakin canakin - canaries canari - canary canari - cancel cancel - cancell cancel - cancelled cancel - cancelling cancel - cancels cancel - cancer cancer - candidatus candidatu - candied candi - candle candl - candles candl - candlesticks candlestick - candy candi - canidius canidiu - cank cank - canker canker - cankerblossom cankerblossom - cankers canker - cannibally cannib - cannibals cannib - cannon cannon - cannoneer cannon - cannons cannon - cannot cannot - canon canon - canoniz canoniz - canonize canon - canonized canon - canons canon - canopied canopi - canopies canopi - canopy canopi - canst canst - canstick canstick - canterbury canterburi - cantle cantl - cantons canton - canus canu - canvas canva - canvass canvass - canzonet canzonet - cap cap - capability capabl - capable capabl - capacities capac - capacity capac - caparison caparison - capdv capdv - cape cape - capel capel - capels capel - caper caper - capers caper - capet capet - caphis caphi - capilet capilet - capitaine capitain - capital capit - capite capit - capitol capitol - capitulate capitul - capocchia capocchia - capon capon - capons capon - capp capp - cappadocia cappadocia - capriccio capriccio - capricious caprici - caps cap - capt capt - captain captain - captains captain - captainship captainship - captious captiou - captivate captiv - captivated captiv - captivates captiv - captive captiv - captives captiv - captivity captiv - captum captum - capucius capuciu - capulet capulet - capulets capulet - car car - carack carack - caracks carack - carat carat - caraways carawai - carbonado carbonado - carbuncle carbuncl - carbuncled carbuncl - carbuncles carbuncl - carcanet carcanet - carcase carcas - carcases carcas - carcass carcass - carcasses carcass - card card - cardecue cardecu - carded card - carders carder - cardinal cardin - cardinally cardin - cardinals cardin - cardmaker cardmak - cards card - carduus carduu - care care - cared care - career career - careers career - careful care - carefully carefulli - careless careless - carelessly carelessli - carelessness careless - cares care - caret caret - cargo cargo - carl carl - carlisle carlisl - carlot carlot - carman carman - carmen carmen - carnal carnal - carnally carnal - carnarvonshire carnarvonshir - carnation carnat - carnations carnat - carol carol - carous carou - carouse carous - caroused carous - carouses carous - carousing carous - carp carp - carpenter carpent - carper carper - carpet carpet - carpets carpet - carping carp - carriage carriag - carriages carriag - carried carri - carrier carrier - carriers carrier - carries carri - carrion carrion - carrions carrion - carry carri - carrying carri - cars car - cart cart - carters carter - carthage carthag - carts cart - carv carv - carve carv - carved carv - carver carver - carves carv - carving carv - cas ca - casa casa - casaer casaer - casca casca - case case - casement casement - casements casement - cases case - cash cash - cashier cashier - casing case - cask cask - casket casket - casketed casket - caskets casket - casque casqu - casques casqu - cassado cassado - cassandra cassandra - cassibelan cassibelan - cassio cassio - cassius cassiu - cassocks cassock - cast cast - castalion castalion - castaway castawai - castaways castawai - casted cast - caster caster - castigate castig - castigation castig - castile castil - castiliano castiliano - casting cast - castle castl - castles castl - casts cast - casual casual - casually casual - casualties casualti - casualty casualti - cat cat - cataian cataian - catalogue catalogu - cataplasm cataplasm - cataracts cataract - catarrhs catarrh - catastrophe catastroph - catch catch - catcher catcher - catches catch - catching catch - cate cate - catechising catechis - catechism catech - catechize catech - cater cater - caterpillars caterpillar - caters cater - caterwauling caterwaul - cates cate - catesby catesbi - cathedral cathedr - catlike catlik - catling catl - catlings catl - cato cato - cats cat - cattle cattl - caucasus caucasu - caudle caudl - cauf cauf - caught caught - cauldron cauldron - caus cau - cause caus - caused caus - causeless causeless - causer causer - causes caus - causest causest - causeth causeth - cautel cautel - cautelous cautel - cautels cautel - cauterizing cauter - caution caution - cautions caution - cavaleiro cavaleiro - cavalery cavaleri - cavaliers cavali - cave cave - cavern cavern - caverns cavern - caves cave - caveto caveto - caviary caviari - cavil cavil - cavilling cavil - cawdor cawdor - cawdron cawdron - cawing caw - ce ce - ceas cea - cease ceas - ceases ceas - ceaseth ceaseth - cedar cedar - cedars cedar - cedius cediu - celebrate celebr - celebrated celebr - celebrates celebr - celebration celebr - celerity celer - celestial celesti - celia celia - cell cell - cellar cellar - cellarage cellarag - celsa celsa - cement cement - censer censer - censor censor - censorinus censorinu - censur censur - censure censur - censured censur - censurers censur - censures censur - censuring censur - centaur centaur - centaurs centaur - centre centr - cents cent - centuries centuri - centurion centurion - centurions centurion - century centuri - cerberus cerberu - cerecloth cerecloth - cerements cerement - ceremonial ceremoni - ceremonies ceremoni - ceremonious ceremoni - ceremoniously ceremoni - ceremony ceremoni - ceres cere - cerns cern - certain certain - certainer certain - certainly certainli - certainties certainti - certainty certainti - certes cert - certificate certif - certified certifi - certifies certifi - certify certifi - ces ce - cesario cesario - cess cess - cesse cess - cestern cestern - cetera cetera - cette cett - chaces chace - chaf chaf - chafe chafe - chafed chafe - chafes chafe - chaff chaff - chaffless chaffless - chafing chafe - chain chain - chains chain - chair chair - chairs chair - chalic chalic - chalice chalic - chalices chalic - chalk chalk - chalks chalk - chalky chalki - challeng challeng - challenge challeng - challenged challeng - challenger challeng - challengers challeng - challenges challeng - cham cham - chamber chamber - chamberers chamber - chamberlain chamberlain - chamberlains chamberlain - chambermaid chambermaid - chambermaids chambermaid - chambers chamber - chameleon chameleon - champ champ - champagne champagn - champain champain - champains champain - champion champion - champions champion - chanc chanc - chance chanc - chanced chanc - chancellor chancellor - chances chanc - chandler chandler - chang chang - change chang - changeable changeabl - changed chang - changeful chang - changeling changel - changelings changel - changer changer - changes chang - changest changest - changing chang - channel channel - channels channel - chanson chanson - chant chant - chanticleer chanticl - chanting chant - chantries chantri - chantry chantri - chants chant - chaos chao - chap chap - chape chape - chapel chapel - chapeless chapeless - chapels chapel - chaplain chaplain - chaplains chaplain - chapless chapless - chaplet chaplet - chapmen chapmen - chaps chap - chapter chapter - character charact - charactered charact - characterless characterless - characters charact - charactery characteri - characts charact - charbon charbon - chare chare - chares chare - charg charg - charge charg - charged charg - chargeful charg - charges charg - chargeth chargeth - charging charg - chariest chariest - chariness chari - charing chare - chariot chariot - chariots chariot - charitable charit - charitably charit - charities chariti - charity chariti - charlemain charlemain - charles charl - charm charm - charmed charm - charmer charmer - charmeth charmeth - charmian charmian - charming charm - charmingly charmingli - charms charm - charneco charneco - charnel charnel - charolois charoloi - charon charon - charter charter - charters charter - chartreux chartreux - chary chari - charybdis charybdi - chas cha - chase chase - chased chase - chaser chaser - chaseth chaseth - chasing chase - chaste chast - chastely chast - chastis chasti - chastise chastis - chastised chastis - chastisement chastis - chastity chastiti - chat chat - chatham chatham - chatillon chatillon - chats chat - chatt chatt - chattels chattel - chatter chatter - chattering chatter - chattles chattl - chaud chaud - chaunted chaunt - chaw chaw - chawdron chawdron - che che - cheap cheap - cheapen cheapen - cheaper cheaper - cheapest cheapest - cheaply cheapli - cheapside cheapsid - cheat cheat - cheated cheat - cheater cheater - cheaters cheater - cheating cheat - cheats cheat - check check - checked check - checker checker - checking check - checks check - cheek cheek - cheeks cheek - cheer cheer - cheered cheer - cheerer cheerer - cheerful cheer - cheerfully cheerfulli - cheering cheer - cheerless cheerless - cheerly cheerli - cheers cheer - cheese chees - chequer chequer - cher cher - cherish cherish - cherished cherish - cherisher cherish - cherishes cherish - cherishing cherish - cherries cherri - cherry cherri - cherrypit cherrypit - chertsey chertsei - cherub cherub - cherubims cherubim - cherubin cherubin - cherubins cherubin - cheshu cheshu - chess chess - chest chest - chester chester - chestnut chestnut - chestnuts chestnut - chests chest - chetas cheta - chev chev - cheval cheval - chevalier chevali - chevaliers chevali - cheveril cheveril - chew chew - chewed chew - chewet chewet - chewing chew - chez chez - chi chi - chick chick - chicken chicken - chickens chicken - chicurmurco chicurmurco - chid chid - chidden chidden - chide chide - chiders chider - chides chide - chiding chide - chief chief - chiefest chiefest - chiefly chiefli - chien chien - child child - childed child - childeric childer - childhood childhood - childhoods childhood - childing child - childish childish - childishness childish - childlike childlik - childness child - children children - chill chill - chilling chill - chime chime - chimes chime - chimney chimnei - chimneypiece chimneypiec - chimneys chimnei - chimurcho chimurcho - chin chin - china china - chine chine - chines chine - chink chink - chinks chink - chins chin - chipp chipp - chipper chipper - chips chip - chiron chiron - chirping chirp - chirrah chirrah - chirurgeonly chirurgeonli - chisel chisel - chitopher chitoph - chivalrous chivalr - chivalry chivalri - choice choic - choicely choic - choicest choicest - choir choir - choirs choir - chok chok - choke choke - choked choke - chokes choke - choking choke - choler choler - choleric choler - cholers choler - chollors chollor - choose choos - chooser chooser - chooses choos - chooseth chooseth - choosing choos - chop chop - chopine chopin - choplogic choplog - chopp chopp - chopped chop - chopping chop - choppy choppi - chops chop - chopt chopt - chor chor - choristers chorist - chorus choru - chose chose - chosen chosen - chough chough - choughs chough - chrish chrish - christ christ - christen christen - christendom christendom - christendoms christendom - christening christen - christenings christen - christian christian - christianlike christianlik - christians christian - christmas christma - christom christom - christopher christoph - christophero christophero - chronicle chronicl - chronicled chronicl - chronicler chronicl - chroniclers chronicl - chronicles chronicl - chrysolite chrysolit - chuck chuck - chucks chuck - chud chud - chuffs chuff - church church - churches church - churchman churchman - churchmen churchmen - churchyard churchyard - churchyards churchyard - churl churl - churlish churlish - churlishly churlishli - churls churl - churn churn - chus chu - cicatrice cicatric - cicatrices cicatric - cicely cice - cicero cicero - ciceter cicet - ciel ciel - ciitzens ciitzen - cilicia cilicia - cimber cimber - cimmerian cimmerian - cinable cinabl - cincture cinctur - cinders cinder - cine cine - cinna cinna - cinque cinqu - cipher cipher - ciphers cipher - circa circa - circe circ - circle circl - circled circl - circlets circlet - circling circl - circuit circuit - circum circum - circumcised circumcis - circumference circumfer - circummur circummur - circumscrib circumscrib - circumscribed circumscrib - circumscription circumscript - circumspect circumspect - circumstance circumst - circumstanced circumstanc - circumstances circumst - circumstantial circumstanti - circumvent circumv - circumvention circumvent - cistern cistern - citadel citadel - cital cital - cite cite - cited cite - cites cite - cities citi - citing cite - citizen citizen - citizens citizen - cittern cittern - city citi - civet civet - civil civil - civility civil - civilly civilli - clack clack - clad clad - claim claim - claiming claim - claims claim - clamb clamb - clamber clamber - clammer clammer - clamor clamor - clamorous clamor - clamors clamor - clamour clamour - clamours clamour - clang clang - clangor clangor - clap clap - clapp clapp - clapped clap - clapper clapper - clapping clap - claps clap - clare clare - clarence clarenc - claret claret - claribel claribel - clasp clasp - clasps clasp - clatter clatter - claud claud - claudio claudio - claudius claudiu - clause claus - claw claw - clawed claw - clawing claw - claws claw - clay clai - clays clai - clean clean - cleanliest cleanliest - cleanly cleanli - cleans clean - cleanse cleans - cleansing cleans - clear clear - clearer clearer - clearest clearest - clearly clearli - clearness clear - clears clear - cleave cleav - cleaving cleav - clef clef - cleft cleft - cleitus cleitu - clemency clemenc - clement clement - cleomenes cleomen - cleopatpa cleopatpa - cleopatra cleopatra - clepeth clepeth - clept clept - clerestories clerestori - clergy clergi - clergyman clergyman - clergymen clergymen - clerk clerk - clerkly clerkli - clerks clerk - clew clew - client client - clients client - cliff cliff - clifford clifford - cliffords clifford - cliffs cliff - clifton clifton - climate climat - climature climatur - climb climb - climbed climb - climber climber - climbeth climbeth - climbing climb - climbs climb - clime clime - cling cling - clink clink - clinking clink - clinquant clinquant - clip clip - clipp clipp - clipper clipper - clippeth clippeth - clipping clip - clipt clipt - clitus clitu - clo clo - cloak cloak - cloakbag cloakbag - cloaks cloak - clock clock - clocks clock - clod clod - cloddy cloddi - clodpole clodpol - clog clog - clogging clog - clogs clog - cloister cloister - cloistress cloistress - cloquence cloquenc - clos clo - close close - closed close - closely close - closeness close - closer closer - closes close - closest closest - closet closet - closing close - closure closur - cloten cloten - clotens cloten - cloth cloth - clothair clothair - clotharius clothariu - clothe cloth - clothes cloth - clothier clothier - clothiers clothier - clothing cloth - cloths cloth - clotpoles clotpol - clotpoll clotpol - cloud cloud - clouded cloud - cloudiness cloudi - clouds cloud - cloudy cloudi - clout clout - clouted clout - clouts clout - cloven cloven - clover clover - cloves clove - clovest clovest - clowder clowder - clown clown - clownish clownish - clowns clown - cloy cloi - cloyed cloi - cloying cloi - cloyless cloyless - cloyment cloyment - cloys cloi - club club - clubs club - cluck cluck - clung clung - clust clust - clusters cluster - clutch clutch - clyster clyster - cneius cneiu - cnemies cnemi - co co - coach coach - coaches coach - coachmakers coachmak - coact coact - coactive coactiv - coagulate coagul - coal coal - coals coal - coarse coars - coarsely coars - coast coast - coasting coast - coasts coast - coat coat - coated coat - coats coat - cobble cobbl - cobbled cobbl - cobbler cobbler - cobham cobham - cobloaf cobloaf - cobweb cobweb - cobwebs cobweb - cock cock - cockatrice cockatric - cockatrices cockatric - cockle cockl - cockled cockl - cockney cocknei - cockpit cockpit - cocks cock - cocksure cocksur - coctus coctu - cocytus cocytu - cod cod - codding cod - codling codl - codpiece codpiec - codpieces codpiec - cods cod - coelestibus coelestibu - coesar coesar - coeur coeur - coffer coffer - coffers coffer - coffin coffin - coffins coffin - cog cog - cogging cog - cogitation cogit - cogitations cogit - cognition cognit - cognizance cogniz - cogscomb cogscomb - cohabitants cohabit - coher coher - cohere coher - coherence coher - coherent coher - cohorts cohort - coif coif - coign coign - coil coil - coin coin - coinage coinag - coiner coiner - coining coin - coins coin - col col - colbrand colbrand - colchos colcho - cold cold - colder colder - coldest coldest - coldly coldli - coldness cold - coldspur coldspur - colebrook colebrook - colic colic - collar collar - collars collar - collateral collater - colleagued colleagu - collect collect - collected collect - collection collect - college colleg - colleges colleg - collied colli - collier collier - colliers collier - collop collop - collusion collus - colme colm - colmekill colmekil - coloquintida coloquintida - color color - colors color - colossus colossu - colour colour - colourable colour - coloured colour - colouring colour - colours colour - colt colt - colted colt - colts colt - columbine columbin - columbines columbin - colville colvil - com com - comagene comagen - comart comart - comb comb - combat combat - combatant combat - combatants combat - combated combat - combating combat - combin combin - combinate combin - combination combin - combine combin - combined combin - combless combless - combustion combust - come come - comedian comedian - comedians comedian - comedy comedi - comeliness comeli - comely come - comer comer - comers comer - comes come - comest comest - comet comet - cometh cometh - comets comet - comfect comfect - comfit comfit - comfits comfit - comfort comfort - comfortable comfort - comforted comfort - comforter comfort - comforting comfort - comfortless comfortless - comforts comfort - comic comic - comical comic - coming come - comings come - cominius cominiu - comma comma - command command - commande command - commanded command - commander command - commanders command - commanding command - commandment command - commandments command - commands command - comme comm - commenc commenc - commence commenc - commenced commenc - commencement commenc - commences commenc - commencing commenc - commend commend - commendable commend - commendation commend - commendations commend - commended commend - commending commend - commends commend - comment comment - commentaries commentari - commenting comment - comments comment - commerce commerc - commingled commingl - commiseration commiser - commission commiss - commissioners commission - commissions commiss - commit commit - commits commit - committ committ - committed commit - committing commit - commix commix - commixed commix - commixtion commixt - commixture commixtur - commodious commodi - commodities commod - commodity commod - common common - commonalty commonalti - commoner common - commoners common - commonly commonli - commons common - commonweal commonw - commonwealth commonwealth - commotion commot - commotions commot - commune commun - communicat communicat - communicate commun - communication commun - communities commun - community commun - comonty comonti - compact compact - companies compani - companion companion - companions companion - companionship companionship - company compani - compar compar - comparative compar - compare compar - compared compar - comparing compar - comparison comparison - comparisons comparison - compartner compartn - compass compass - compasses compass - compassing compass - compassion compass - compassionate compassion - compeers compeer - compel compel - compell compel - compelled compel - compelling compel - compels compel - compensation compens - competence compet - competency compet - competent compet - competitor competitor - competitors competitor - compil compil - compile compil - compiled compil - complain complain - complainer complain - complainest complainest - complaining complain - complainings complain - complains complain - complaint complaint - complaints complaint - complement complement - complements complement - complete complet - complexion complexion - complexioned complexion - complexions complexion - complices complic - complies compli - compliment compliment - complimental compliment - compliments compliment - complot complot - complots complot - complotted complot - comply compli - compos compo - compose compos - composed compos - composition composit - compost compost - composture compostur - composure composur - compound compound - compounded compound - compounds compound - comprehend comprehend - comprehended comprehend - comprehends comprehend - compremises compremis - compris compri - comprising compris - compromis compromi - compromise compromis - compt compt - comptible comptibl - comptrollers comptrol - compulsatory compulsatori - compulsion compuls - compulsive compuls - compunctious compuncti - computation comput - comrade comrad - comrades comrad - comutual comutu - con con - concave concav - concavities concav - conceal conceal - concealed conceal - concealing conceal - concealment conceal - concealments conceal - conceals conceal - conceit conceit - conceited conceit - conceitless conceitless - conceits conceit - conceiv conceiv - conceive conceiv - conceived conceiv - conceives conceiv - conceiving conceiv - conception concept - conceptions concept - conceptious concepti - concern concern - concernancy concern - concerneth concerneth - concerning concern - concernings concern - concerns concern - conclave conclav - conclud conclud - conclude conclud - concluded conclud - concludes conclud - concluding conclud - conclusion conclus - conclusions conclus - concolinel concolinel - concord concord - concubine concubin - concupiscible concupisc - concupy concupi - concur concur - concurring concur - concurs concur - condemn condemn - condemnation condemn - condemned condemn - condemning condemn - condemns condemn - condescend condescend - condign condign - condition condit - conditionally condition - conditions condit - condole condol - condolement condol - condoling condol - conduce conduc - conduct conduct - conducted conduct - conducting conduct - conductor conductor - conduit conduit - conduits conduit - conected conect - coney conei - confection confect - confectionary confectionari - confections confect - confederacy confederaci - confederate confeder - confederates confeder - confer confer - conference confer - conferr conferr - conferring confer - confess confess - confessed confess - confesses confess - confesseth confesseth - confessing confess - confession confess - confessions confess - confessor confessor - confidence confid - confident confid - confidently confid - confin confin - confine confin - confined confin - confineless confineless - confiners confin - confines confin - confining confin - confirm confirm - confirmation confirm - confirmations confirm - confirmed confirm - confirmer confirm - confirmers confirm - confirming confirm - confirmities confirm - confirms confirm - confiscate confisc - confiscated confisc - confiscation confisc - confixed confix - conflict conflict - conflicting conflict - conflicts conflict - confluence confluenc - conflux conflux - conform conform - conformable conform - confound confound - confounded confound - confounding confound - confounds confound - confront confront - confronted confront - confus confu - confused confus - confusedly confusedli - confusion confus - confusions confus - confutation confut - confutes confut - congeal congeal - congealed congeal - congealment congeal - congee conge - conger conger - congest congest - congied congi - congratulate congratul - congreeing congre - congreeted congreet - congregate congreg - congregated congreg - congregation congreg - congregations congreg - congruent congruent - congruing congru - conies coni - conjectural conjectur - conjecture conjectur - conjectures conjectur - conjoin conjoin - conjoined conjoin - conjoins conjoin - conjointly conjointli - conjunct conjunct - conjunction conjunct - conjunctive conjunct - conjur conjur - conjuration conjur - conjurations conjur - conjure conjur - conjured conjur - conjurer conjur - conjurers conjur - conjures conjur - conjuring conjur - conjuro conjuro - conn conn - connected connect - connive conniv - conqu conqu - conquer conquer - conquered conquer - conquering conquer - conqueror conqueror - conquerors conqueror - conquers conquer - conquest conquest - conquests conquest - conquring conqur - conrade conrad - cons con - consanguineous consanguin - consanguinity consanguin - conscienc conscienc - conscience conscienc - consciences conscienc - conscionable conscion - consecrate consecr - consecrated consecr - consecrations consecr - consent consent - consented consent - consenting consent - consents consent - consequence consequ - consequences consequ - consequently consequ - conserve conserv - conserved conserv - conserves conserv - consider consid - considerance consider - considerate consider - consideration consider - considerations consider - considered consid - considering consid - considerings consid - considers consid - consign consign - consigning consign - consist consist - consisteth consisteth - consisting consist - consistory consistori - consists consist - consolate consol - consolation consol - consonancy conson - consonant conson - consort consort - consorted consort - consortest consortest - conspectuities conspectu - conspir conspir - conspiracy conspiraci - conspirant conspir - conspirator conspir - conspirators conspir - conspire conspir - conspired conspir - conspirers conspir - conspires conspir - conspiring conspir - constable constabl - constables constabl - constance constanc - constancies constanc - constancy constanc - constant constant - constantine constantin - constantinople constantinopl - constantly constantli - constellation constel - constitution constitut - constrain constrain - constrained constrain - constraineth constraineth - constrains constrain - constraint constraint - constring constr - construction construct - construe constru - consul consul - consuls consul - consulship consulship - consulships consulship - consult consult - consulting consult - consults consult - consum consum - consume consum - consumed consum - consumes consum - consuming consum - consummate consumm - consummation consumm - consumption consumpt - consumptions consumpt - contagion contagion - contagious contagi - contain contain - containing contain - contains contain - contaminate contamin - contaminated contamin - contemn contemn - contemned contemn - contemning contemn - contemns contemn - contemplate contempl - contemplation contempl - contemplative contempl - contempt contempt - contemptible contempt - contempts contempt - contemptuous contemptu - contemptuously contemptu - contend contend - contended contend - contending contend - contendon contendon - content content - contenta contenta - contented content - contenteth contenteth - contention content - contentious contenti - contentless contentless - contento contento - contents content - contest contest - contestation contest - continence contin - continency contin - continent contin - continents contin - continu continu - continual continu - continually continu - continuance continu - continuantly continuantli - continuate continu - continue continu - continued continu - continuer continu - continues continu - continuing continu - contract contract - contracted contract - contracting contract - contraction contract - contradict contradict - contradicted contradict - contradiction contradict - contradicts contradict - contraries contrari - contrarieties contrarieti - contrariety contrarieti - contrarious contrari - contrariously contrari - contrary contrari - contre contr - contribution contribut - contributors contributor - contrite contrit - contriv contriv - contrive contriv - contrived contriv - contriver contriv - contrives contriv - contriving contriv - control control - controll control - controller control - controlling control - controlment control - controls control - controversy controversi - contumelious contumeli - contumeliously contumeli - contumely contum - contusions contus - convenience conveni - conveniences conveni - conveniency conveni - convenient conveni - conveniently conveni - convented convent - conventicles conventicl - convents convent - convers conver - conversant convers - conversation convers - conversations convers - converse convers - conversed convers - converses convers - conversing convers - conversion convers - convert convert - converted convert - convertest convertest - converting convert - convertite convertit - convertites convertit - converts convert - convey convei - conveyance convey - conveyances convey - conveyers convey - conveying convei - convict convict - convicted convict - convince convinc - convinced convinc - convinces convinc - convive conviv - convocation convoc - convoy convoi - convulsions convuls - cony coni - cook cook - cookery cookeri - cooks cook - cool cool - cooled cool - cooling cool - cools cool - coop coop - coops coop - cop cop - copatain copatain - cope cope - cophetua cophetua - copied copi - copies copi - copious copiou - copper copper - copperspur copperspur - coppice coppic - copulation copul - copulatives copul - copy copi - cor cor - coragio coragio - coral coral - coram coram - corambus corambu - coranto coranto - corantos coranto - corbo corbo - cord cord - corded cord - cordelia cordelia - cordial cordial - cordis cordi - cords cord - core core - corin corin - corinth corinth - corinthian corinthian - coriolanus coriolanu - corioli corioli - cork cork - corky corki - cormorant cormor - corn corn - cornelia cornelia - cornelius corneliu - corner corner - corners corner - cornerstone cornerston - cornets cornet - cornish cornish - corns corn - cornuto cornuto - cornwall cornwal - corollary corollari - coronal coron - coronation coron - coronet coronet - coronets coronet - corporal corpor - corporals corpor - corporate corpor - corpse corps - corpulent corpul - correct correct - corrected correct - correcting correct - correction correct - correctioner correction - corrects correct - correspondence correspond - correspondent correspond - corresponding correspond - corresponsive correspons - corrigible corrig - corrival corriv - corrivals corriv - corroborate corrobor - corrosive corros - corrupt corrupt - corrupted corrupt - corrupter corrupt - corrupters corrupt - corruptible corrupt - corruptibly corrupt - corrupting corrupt - corruption corrupt - corruptly corruptli - corrupts corrupt - corse cors - corses cors - corslet corslet - cosmo cosmo - cost cost - costard costard - costermongers costermong - costlier costlier - costly costli - costs cost - cot cot - cote cote - coted cote - cotsall cotsal - cotsole cotsol - cotswold cotswold - cottage cottag - cottages cottag - cotus cotu - couch couch - couched couch - couching couch - couchings couch - coude coud - cough cough - coughing cough - could could - couldst couldst - coulter coulter - council council - councillor councillor - councils council - counsel counsel - counsell counsel - counsellor counsellor - counsellors counsellor - counselor counselor - counselors counselor - counsels counsel - count count - counted count - countenanc countenanc - countenance counten - countenances counten - counter counter - counterchange counterchang - countercheck countercheck - counterfeit counterfeit - counterfeited counterfeit - counterfeiting counterfeit - counterfeitly counterfeitli - counterfeits counterfeit - countermand countermand - countermands countermand - countermines countermin - counterpart counterpart - counterpoints counterpoint - counterpois counterpoi - counterpoise counterpois - counters counter - countervail countervail - countess countess - countesses countess - counties counti - counting count - countless countless - countries countri - countrv countrv - country countri - countryman countryman - countrymen countrymen - counts count - county counti - couper couper - couple coupl - coupled coupl - couplement couplement - couples coupl - couplet couplet - couplets couplet - cour cour - courage courag - courageous courag - courageously courag - courages courag - courier courier - couriers courier - couronne couronn - cours cour - course cours - coursed cours - courser courser - coursers courser - courses cours - coursing cours - court court - courted court - courteous courteou - courteously courteous - courtesan courtesan - courtesies courtesi - courtesy courtesi - courtezan courtezan - courtezans courtezan - courtier courtier - courtiers courtier - courtlike courtlik - courtly courtli - courtney courtnei - courts court - courtship courtship - cousin cousin - cousins cousin - couterfeit couterfeit - coutume coutum - covenant coven - covenants coven - covent covent - coventry coventri - cover cover - covered cover - covering cover - coverlet coverlet - covers cover - covert covert - covertly covertli - coverture covertur - covet covet - coveted covet - coveting covet - covetings covet - covetous covet - covetously covet - covetousness covet - covets covet - cow cow - coward coward - cowarded coward - cowardice cowardic - cowardly cowardli - cowards coward - cowardship cowardship - cowish cowish - cowl cowl - cowslip cowslip - cowslips cowslip - cox cox - coxcomb coxcomb - coxcombs coxcomb - coy coi - coystrill coystril - coz coz - cozen cozen - cozenage cozenag - cozened cozen - cozener cozen - cozeners cozen - cozening cozen - coziers cozier - crab crab - crabbed crab - crabs crab - crack crack - cracked crack - cracker cracker - crackers cracker - cracking crack - cracks crack - cradle cradl - cradled cradl - cradles cradl - craft craft - crafted craft - craftied crafti - craftier craftier - craftily craftili - crafts craft - craftsmen craftsmen - crafty crafti - cram cram - cramm cramm - cramp cramp - cramps cramp - crams cram - cranking crank - cranks crank - cranmer cranmer - crannied cranni - crannies cranni - cranny cranni - crants crant - crare crare - crash crash - crassus crassu - crav crav - crave crave - craved crave - craven craven - cravens craven - craves crave - craveth craveth - craving crave - crawl crawl - crawling crawl - crawls crawl - craz craz - crazed craze - crazy crazi - creaking creak - cream cream - create creat - created creat - creates creat - creating creat - creation creation - creator creator - creature creatur - creatures creatur - credence credenc - credent credent - credible credibl - credit credit - creditor creditor - creditors creditor - credo credo - credulity credul - credulous credul - creed creed - creek creek - creeks creek - creep creep - creeping creep - creeps creep - crept crept - crescent crescent - crescive cresciv - cressets cresset - cressid cressid - cressida cressida - cressids cressid - cressy cressi - crest crest - crested crest - crestfall crestfal - crestless crestless - crests crest - cretan cretan - crete crete - crevice crevic - crew crew - crews crew - crib crib - cribb cribb - cribs crib - cricket cricket - crickets cricket - cried cri - criedst criedst - crier crier - cries cri - criest criest - crieth crieth - crime crime - crimeful crime - crimeless crimeless - crimes crime - criminal crimin - crimson crimson - cringe cring - cripple crippl - crisp crisp - crisped crisp - crispian crispian - crispianus crispianu - crispin crispin - critic critic - critical critic - critics critic - croak croak - croaking croak - croaks croak - crocodile crocodil - cromer cromer - cromwell cromwel - crone crone - crook crook - crookback crookback - crooked crook - crooking crook - crop crop - cropp cropp - crosby crosbi - cross cross - crossed cross - crosses cross - crossest crossest - crossing cross - crossings cross - crossly crossli - crossness cross - crost crost - crotchets crotchet - crouch crouch - crouching crouch - crow crow - crowd crowd - crowded crowd - crowding crowd - crowds crowd - crowflowers crowflow - crowing crow - crowkeeper crowkeep - crown crown - crowned crown - crowner crowner - crownet crownet - crownets crownet - crowning crown - crowns crown - crows crow - crudy crudi - cruel cruel - cruell cruell - crueller crueller - cruelly cruelli - cruels cruel - cruelty cruelti - crum crum - crumble crumbl - crumbs crumb - crupper crupper - crusadoes crusado - crush crush - crushed crush - crushest crushest - crushing crush - crust crust - crusts crust - crusty crusti - crutch crutch - crutches crutch - cry cry - crying cry - crystal crystal - crystalline crystallin - crystals crystal - cub cub - cubbert cubbert - cubiculo cubiculo - cubit cubit - cubs cub - cuckold cuckold - cuckoldly cuckoldli - cuckolds cuckold - cuckoo cuckoo - cucullus cucullu - cudgel cudgel - cudgeled cudgel - cudgell cudgel - cudgelling cudgel - cudgels cudgel - cue cue - cues cue - cuff cuff - cuffs cuff - cuique cuiqu - cull cull - culling cull - cullion cullion - cullionly cullionli - cullions cullion - culpable culpabl - culverin culverin - cum cum - cumber cumber - cumberland cumberland - cunning cun - cunningly cunningli - cunnings cun - cuore cuor - cup cup - cupbearer cupbear - cupboarding cupboard - cupid cupid - cupids cupid - cuppele cuppel - cups cup - cur cur - curan curan - curate curat - curb curb - curbed curb - curbing curb - curbs curb - curd curd - curdied curdi - curds curd - cure cure - cured cure - cureless cureless - curer curer - cures cure - curfew curfew - curing cure - curio curio - curiosity curios - curious curiou - curiously curious - curl curl - curled curl - curling curl - curls curl - currance curranc - currants currant - current current - currents current - currish currish - curry curri - curs cur - curse curs - cursed curs - curses curs - cursies cursi - cursing curs - cursorary cursorari - curst curst - curster curster - curstest curstest - curstness curst - cursy cursi - curtail curtail - curtain curtain - curtains curtain - curtal curtal - curtis curti - curtle curtl - curtsied curtsi - curtsies curtsi - curtsy curtsi - curvet curvet - curvets curvet - cushes cush - cushion cushion - cushions cushion - custalorum custalorum - custard custard - custody custodi - custom custom - customary customari - customed custom - customer custom - customers custom - customs custom - custure custur - cut cut - cutler cutler - cutpurse cutpurs - cutpurses cutpurs - cuts cut - cutter cutter - cutting cut - cuttle cuttl - cxsar cxsar - cyclops cyclop - cydnus cydnu - cygnet cygnet - cygnets cygnet - cym cym - cymbals cymbal - cymbeline cymbelin - cyme cyme - cynic cynic - cynthia cynthia - cypress cypress - cypriot cypriot - cyprus cypru - cyrus cyru - cytherea cytherea - d d - dabbled dabbl - dace dace - dad dad - daedalus daedalu - daemon daemon - daff daff - daffed daf - daffest daffest - daffodils daffodil - dagger dagger - daggers dagger - dagonet dagonet - daily daili - daintier daintier - dainties dainti - daintiest daintiest - daintily daintili - daintiness dainti - daintry daintri - dainty dainti - daisied daisi - daisies daisi - daisy daisi - dale dale - dalliance dallianc - dallied dalli - dallies dalli - dally dalli - dallying dalli - dalmatians dalmatian - dam dam - damage damag - damascus damascu - damask damask - damasked damask - dame dame - dames dame - damm damm - damn damn - damnable damnabl - damnably damnabl - damnation damnat - damned damn - damns damn - damoiselle damoisel - damon damon - damosella damosella - damp damp - dams dam - damsel damsel - damsons damson - dan dan - danc danc - dance danc - dancer dancer - dances danc - dancing danc - dandle dandl - dandy dandi - dane dane - dang dang - danger danger - dangerous danger - dangerously danger - dangers danger - dangling dangl - daniel daniel - danish danish - dank dank - dankish dankish - danskers dansker - daphne daphn - dappled dappl - dapples dappl - dar dar - dardan dardan - dardanian dardanian - dardanius dardaniu - dare dare - dared dare - dareful dare - dares dare - darest darest - daring dare - darius dariu - dark dark - darken darken - darkening darken - darkens darken - darker darker - darkest darkest - darkling darkl - darkly darkli - darkness dark - darling darl - darlings darl - darnel darnel - darraign darraign - dart dart - darted dart - darter darter - dartford dartford - darting dart - darts dart - dash dash - dashes dash - dashing dash - dastard dastard - dastards dastard - dat dat - datchet datchet - date date - dated date - dateless dateless - dates date - daub daub - daughter daughter - daughters daughter - daunt daunt - daunted daunt - dauntless dauntless - dauphin dauphin - daventry daventri - davy davi - daw daw - dawn dawn - dawning dawn - daws daw - day dai - daylight daylight - days dai - dazzle dazzl - dazzled dazzl - dazzling dazzl - de de - dead dead - deadly deadli - deaf deaf - deafing deaf - deafness deaf - deafs deaf - deal deal - dealer dealer - dealers dealer - dealest dealest - dealing deal - dealings deal - deals deal - dealt dealt - dean dean - deanery deaneri - dear dear - dearer dearer - dearest dearest - dearly dearli - dearness dear - dears dear - dearth dearth - dearths dearth - death death - deathbed deathb - deathful death - deaths death - deathsman deathsman - deathsmen deathsmen - debarred debar - debase debas - debate debat - debated debat - debatement debat - debateth debateth - debating debat - debauch debauch - debile debil - debility debil - debitor debitor - debonair debonair - deborah deborah - debosh debosh - debt debt - debted debt - debtor debtor - debtors debtor - debts debt - debuty debuti - decay decai - decayed decai - decayer decay - decaying decai - decays decai - deceas decea - decease deceas - deceased deceas - deceit deceit - deceitful deceit - deceits deceit - deceiv deceiv - deceivable deceiv - deceive deceiv - deceived deceiv - deceiver deceiv - deceivers deceiv - deceives deceiv - deceivest deceivest - deceiveth deceiveth - deceiving deceiv - december decemb - decent decent - deceptious decepti - decerns decern - decide decid - decides decid - decimation decim - decipher deciph - deciphers deciph - decision decis - decius deciu - deck deck - decking deck - decks deck - deckt deckt - declare declar - declares declar - declension declens - declensions declens - declin declin - decline declin - declined declin - declines declin - declining declin - decoct decoct - decorum decorum - decreas decrea - decrease decreas - decreasing decreas - decree decre - decreed decre - decrees decre - decrepit decrepit - dedicate dedic - dedicated dedic - dedicates dedic - dedication dedic - deed deed - deedless deedless - deeds deed - deem deem - deemed deem - deep deep - deeper deeper - deepest deepest - deeply deepli - deeps deep - deepvow deepvow - deer deer - deesse deess - defac defac - deface defac - defaced defac - defacer defac - defacers defac - defacing defac - defam defam - default default - defeat defeat - defeated defeat - defeats defeat - defeatures defeatur - defect defect - defective defect - defects defect - defence defenc - defences defenc - defend defend - defendant defend - defended defend - defender defend - defenders defend - defending defend - defends defend - defense defens - defensible defens - defensive defens - defer defer - deferr deferr - defiance defianc - deficient defici - defied defi - defies defi - defil defil - defile defil - defiler defil - defiles defil - defiling defil - define defin - definement defin - definite definit - definitive definit - definitively definit - deflow deflow - deflower deflow - deflowered deflow - deform deform - deformed deform - deformities deform - deformity deform - deftly deftli - defunct defunct - defunction defunct - defuse defus - defy defi - defying defi - degenerate degener - degraded degrad - degree degre - degrees degre - deified deifi - deifying deifi - deign deign - deigned deign - deiphobus deiphobu - deities deiti - deity deiti - deja deja - deject deject - dejected deject - delabreth delabreth - delay delai - delayed delai - delaying delai - delays delai - delectable delect - deliberate deliber - delicate delic - delicates delic - delicious delici - deliciousness delici - delight delight - delighted delight - delightful delight - delights delight - delinquents delinqu - deliv deliv - deliver deliv - deliverance deliver - delivered deliv - delivering deliv - delivers deliv - delivery deliveri - delphos delpho - deluded delud - deluding delud - deluge delug - delve delv - delver delver - delves delv - demand demand - demanded demand - demanding demand - demands demand - demean demean - demeanor demeanor - demeanour demeanour - demerits demerit - demesnes demesn - demetrius demetriu - demi demi - demigod demigod - demise demis - demoiselles demoisel - demon demon - demonstrable demonstr - demonstrate demonstr - demonstrated demonstr - demonstrating demonstr - demonstration demonstr - demonstrative demonstr - demure demur - demurely demur - demuring demur - den den - denay denai - deni deni - denial denial - denials denial - denied deni - denier denier - denies deni - deniest deniest - denis deni - denmark denmark - dennis denni - denny denni - denote denot - denoted denot - denotement denot - denounc denounc - denounce denounc - denouncing denounc - dens den - denunciation denunci - deny deni - denying deni - deo deo - depart depart - departed depart - departest departest - departing depart - departure departur - depeche depech - depend depend - dependant depend - dependants depend - depended depend - dependence depend - dependences depend - dependency depend - dependent depend - dependents depend - depender depend - depending depend - depends depend - deplore deplor - deploring deplor - depopulate depopul - depos depo - depose depos - deposed depos - deposing depos - depositaries depositari - deprav deprav - depravation deprav - deprave deprav - depraved deprav - depraves deprav - depress depress - depriv depriv - deprive depriv - depth depth - depths depth - deputation deput - depute deput - deputed deput - deputies deputi - deputing deput - deputy deputi - deracinate deracin - derby derbi - dercetas derceta - dere dere - derides derid - derision deris - deriv deriv - derivation deriv - derivative deriv - derive deriv - derived deriv - derives deriv - derogate derog - derogately derog - derogation derog - des de - desartless desartless - descant descant - descend descend - descended descend - descending descend - descends descend - descension descens - descent descent - descents descent - describe describ - described describ - describes describ - descried descri - description descript - descriptions descript - descry descri - desdemon desdemon - desdemona desdemona - desert desert - deserts desert - deserv deserv - deserve deserv - deserved deserv - deservedly deservedli - deserver deserv - deservers deserv - deserves deserv - deservest deservest - deserving deserv - deservings deserv - design design - designment design - designments design - designs design - desir desir - desire desir - desired desir - desirers desir - desires desir - desirest desirest - desiring desir - desirous desir - desist desist - desk desk - desolate desol - desolation desol - desp desp - despair despair - despairing despair - despairs despair - despatch despatch - desperate desper - desperately desper - desperation desper - despis despi - despise despis - despised despis - despiser despis - despiseth despiseth - despising despis - despite despit - despiteful despit - despoiled despoil - dest dest - destin destin - destined destin - destinies destini - destiny destini - destitute destitut - destroy destroi - destroyed destroi - destroyer destroy - destroyers destroy - destroying destroi - destroys destroi - destruction destruct - destructions destruct - det det - detain detain - detains detain - detect detect - detected detect - detecting detect - detection detect - detector detector - detects detect - detention detent - determin determin - determinate determin - determination determin - determinations determin - determine determin - determined determin - determines determin - detest detest - detestable detest - detested detest - detesting detest - detests detest - detract detract - detraction detract - detractions detract - deucalion deucalion - deuce deuc - deum deum - deux deux - devant devant - devesting devest - device devic - devices devic - devil devil - devilish devilish - devils devil - devis devi - devise devis - devised devis - devises devis - devising devis - devoid devoid - devonshire devonshir - devote devot - devoted devot - devotion devot - devour devour - devoured devour - devourers devour - devouring devour - devours devour - devout devout - devoutly devoutli - dew dew - dewberries dewberri - dewdrops dewdrop - dewlap dewlap - dewlapp dewlapp - dews dew - dewy dewi - dexter dexter - dexteriously dexteri - dexterity dexter - di di - diable diabl - diablo diablo - diadem diadem - dial dial - dialect dialect - dialogue dialogu - dialogued dialogu - dials dial - diameter diamet - diamond diamond - diamonds diamond - dian dian - diana diana - diaper diaper - dibble dibbl - dic dic - dice dice - dicers dicer - dich dich - dick dick - dickens dicken - dickon dickon - dicky dicki - dictator dictat - diction diction - dictynna dictynna - did did - diddle diddl - didest didest - dido dido - didst didst - die die - died di - diedst diedst - dies di - diest diest - diet diet - dieted diet - dieter dieter - dieu dieu - diff diff - differ differ - difference differ - differences differ - differency differ - different differ - differing differ - differs differ - difficile difficil - difficult difficult - difficulties difficulti - difficulty difficulti - diffidence diffid - diffidences diffid - diffus diffu - diffused diffus - diffusest diffusest - dig dig - digest digest - digested digest - digestion digest - digestions digest - digg digg - digging dig - dighton dighton - dignified dignifi - dignifies dignifi - dignify dignifi - dignities digniti - dignity digniti - digress digress - digressing digress - digression digress - digs dig - digt digt - dilate dilat - dilated dilat - dilations dilat - dilatory dilatori - dild dild - dildos dildo - dilemma dilemma - dilemmas dilemma - diligence dilig - diligent dilig - diluculo diluculo - dim dim - dimension dimens - dimensions dimens - diminish diminish - diminishing diminish - diminution diminut - diminutive diminut - diminutives diminut - dimm dimm - dimmed dim - dimming dim - dimpled dimpl - dimples dimpl - dims dim - din din - dine dine - dined dine - diner diner - dines dine - ding ding - dining dine - dinner dinner - dinners dinner - dinnertime dinnertim - dint dint - diomed diom - diomede diomed - diomedes diomed - dion dion - dip dip - dipp dipp - dipping dip - dips dip - dir dir - dire dire - direct direct - directed direct - directing direct - direction direct - directions direct - directitude directitud - directive direct - directly directli - directs direct - direful dire - direness dire - direst direst - dirge dirg - dirges dirg - dirt dirt - dirty dirti - dis di - disability disabl - disable disabl - disabled disabl - disabling disabl - disadvantage disadvantag - disagree disagre - disallow disallow - disanimates disanim - disannul disannul - disannuls disannul - disappointed disappoint - disarm disarm - disarmed disarm - disarmeth disarmeth - disarms disarm - disaster disast - disasters disast - disastrous disastr - disbench disbench - disbranch disbranch - disburdened disburden - disburs disbur - disburse disburs - disbursed disburs - discandy discandi - discandying discandi - discard discard - discarded discard - discase discas - discased discas - discern discern - discerner discern - discerning discern - discernings discern - discerns discern - discharg discharg - discharge discharg - discharged discharg - discharging discharg - discipled discipl - disciples discipl - disciplin disciplin - discipline disciplin - disciplined disciplin - disciplines disciplin - disclaim disclaim - disclaiming disclaim - disclaims disclaim - disclos disclo - disclose disclos - disclosed disclos - discloses disclos - discolour discolour - discoloured discolour - discolours discolour - discomfit discomfit - discomfited discomfit - discomfiture discomfitur - discomfort discomfort - discomfortable discomfort - discommend discommend - disconsolate disconsol - discontent discont - discontented discont - discontentedly discontentedli - discontenting discont - discontents discont - discontinue discontinu - discontinued discontinu - discord discord - discordant discord - discords discord - discourse discours - discoursed discours - discourser discours - discourses discours - discoursive discours - discourtesy discourtesi - discov discov - discover discov - discovered discov - discoverers discover - discoveries discoveri - discovering discov - discovers discov - discovery discoveri - discredit discredit - discredited discredit - discredits discredit - discreet discreet - discreetly discreetli - discretion discret - discretions discret - discuss discuss - disdain disdain - disdained disdain - disdaineth disdaineth - disdainful disdain - disdainfully disdainfulli - disdaining disdain - disdains disdain - disdnguish disdnguish - diseas disea - disease diseas - diseased diseas - diseases diseas - disedg disedg - disembark disembark - disfigure disfigur - disfigured disfigur - disfurnish disfurnish - disgorge disgorg - disgrac disgrac - disgrace disgrac - disgraced disgrac - disgraceful disgrac - disgraces disgrac - disgracing disgrac - disgracious disgraci - disguis disgui - disguise disguis - disguised disguis - disguiser disguis - disguises disguis - disguising disguis - dish dish - dishabited dishabit - dishclout dishclout - dishearten dishearten - disheartens dishearten - dishes dish - dishonest dishonest - dishonestly dishonestli - dishonesty dishonesti - dishonor dishonor - dishonorable dishonor - dishonors dishonor - dishonour dishonour - dishonourable dishonour - dishonoured dishonour - dishonours dishonour - disinherit disinherit - disinherited disinherit - disjoin disjoin - disjoining disjoin - disjoins disjoin - disjoint disjoint - disjunction disjunct - dislik dislik - dislike dislik - disliken disliken - dislikes dislik - dislimns dislimn - dislocate disloc - dislodg dislodg - disloyal disloy - disloyalty disloyalti - dismal dismal - dismantle dismantl - dismantled dismantl - dismask dismask - dismay dismai - dismayed dismai - dismemb dismemb - dismember dismemb - dismes dism - dismiss dismiss - dismissed dismiss - dismissing dismiss - dismission dismiss - dismount dismount - dismounted dismount - disnatur disnatur - disobedience disobedi - disobedient disobedi - disobey disobei - disobeys disobei - disorb disorb - disorder disord - disordered disord - disorderly disorderli - disorders disord - disparage disparag - disparagement disparag - disparagements disparag - dispark dispark - dispatch dispatch - dispensation dispens - dispense dispens - dispenses dispens - dispers disper - disperse dispers - dispersed dispers - dispersedly dispersedli - dispersing dispers - dispiteous dispit - displac displac - displace displac - displaced displac - displant displant - displanting displant - display displai - displayed displai - displeas displea - displease displeas - displeased displeas - displeasing displeas - displeasure displeasur - displeasures displeasur - disponge dispong - disport disport - disports disport - dispos dispo - dispose dispos - disposed dispos - disposer dispos - disposing dispos - disposition disposit - dispositions disposit - dispossess dispossess - dispossessing dispossess - disprais disprai - dispraise disprais - dispraising disprais - dispraisingly dispraisingli - dispropertied disproperti - disproportion disproport - disproportioned disproport - disprov disprov - disprove disprov - disproved disprov - dispursed dispurs - disputable disput - disputation disput - disputations disput - dispute disput - disputed disput - disputes disput - disputing disput - disquantity disquant - disquiet disquiet - disquietly disquietli - disrelish disrelish - disrobe disrob - disseat disseat - dissemble dissembl - dissembled dissembl - dissembler dissembl - dissemblers dissembl - dissembling dissembl - dissembly dissembl - dissension dissens - dissensions dissens - dissentious dissenti - dissever dissev - dissipation dissip - dissolute dissolut - dissolutely dissolut - dissolution dissolut - dissolutions dissolut - dissolv dissolv - dissolve dissolv - dissolved dissolv - dissolves dissolv - dissuade dissuad - dissuaded dissuad - distaff distaff - distaffs distaff - distain distain - distains distain - distance distanc - distant distant - distaste distast - distasted distast - distasteful distast - distemp distemp - distemper distemp - distemperature distemperatur - distemperatures distemperatur - distempered distemp - distempering distemp - distil distil - distill distil - distillation distil - distilled distil - distills distil - distilment distil - distinct distinct - distinction distinct - distinctly distinctli - distingue distingu - distinguish distinguish - distinguishes distinguish - distinguishment distinguish - distract distract - distracted distract - distractedly distractedli - distraction distract - distractions distract - distracts distract - distrain distrain - distraught distraught - distress distress - distressed distress - distresses distress - distressful distress - distribute distribut - distributed distribut - distribution distribut - distrust distrust - distrustful distrust - disturb disturb - disturbed disturb - disturbers disturb - disturbing disturb - disunite disunit - disvalued disvalu - disvouch disvouch - dit dit - ditch ditch - ditchers ditcher - ditches ditch - dites dite - ditties ditti - ditty ditti - diurnal diurnal - div div - dive dive - diver diver - divers diver - diversely divers - diversity divers - divert divert - diverted divert - diverts divert - dives dive - divest divest - dividable divid - dividant divid - divide divid - divided divid - divides divid - divideth divideth - divin divin - divination divin - divine divin - divinely divin - divineness divin - diviner divin - divines divin - divinest divinest - divining divin - divinity divin - division divis - divisions divis - divorc divorc - divorce divorc - divorced divorc - divorcement divorc - divorcing divorc - divulg divulg - divulge divulg - divulged divulg - divulging divulg - dizy dizi - dizzy dizzi - do do - doating doat - dobbin dobbin - dock dock - docks dock - doct doct - doctor doctor - doctors doctor - doctrine doctrin - document document - dodge dodg - doe doe - doer doer - doers doer - does doe - doest doest - doff doff - dog dog - dogberry dogberri - dogfish dogfish - dogg dogg - dogged dog - dogs dog - doigts doigt - doing do - doings do - doit doit - doits doit - dolabella dolabella - dole dole - doleful dole - doll doll - dollar dollar - dollars dollar - dolor dolor - dolorous dolor - dolour dolour - dolours dolour - dolphin dolphin - dolt dolt - dolts dolt - domestic domest - domestics domest - dominance domin - dominations domin - dominator domin - domine domin - domineer domin - domineering domin - dominical domin - dominion dominion - dominions dominion - domitius domitiu - dommelton dommelton - don don - donalbain donalbain - donation donat - donc donc - doncaster doncast - done done - dong dong - donn donn - donne donn - donner donner - donnerai donnerai - doom doom - doomsday doomsdai - door door - doorkeeper doorkeep - doors door - dorcas dorca - doreus doreu - doricles doricl - dormouse dormous - dorothy dorothi - dorset dorset - dorsetshire dorsetshir - dost dost - dotage dotag - dotant dotant - dotard dotard - dotards dotard - dote dote - doted dote - doters doter - dotes dote - doteth doteth - doth doth - doting dote - double doubl - doubled doubl - doubleness doubl - doubler doubler - doublet doublet - doublets doublet - doubling doubl - doubly doubli - doubt doubt - doubted doubt - doubtful doubt - doubtfully doubtfulli - doubting doubt - doubtless doubtless - doubts doubt - doug doug - dough dough - doughty doughti - doughy doughi - douglas dougla - dout dout - doute dout - douts dout - dove dove - dovehouse dovehous - dover dover - doves dove - dow dow - dowager dowag - dowdy dowdi - dower dower - dowerless dowerless - dowers dower - dowlas dowla - dowle dowl - down down - downfall downfal - downright downright - downs down - downstairs downstair - downtrod downtrod - downward downward - downwards downward - downy downi - dowries dowri - dowry dowri - dowsabel dowsabel - doxy doxi - dozed doze - dozen dozen - dozens dozen - dozy dozi - drab drab - drabbing drab - drabs drab - drachma drachma - drachmas drachma - draff draff - drag drag - dragg dragg - dragged drag - dragging drag - dragon dragon - dragonish dragonish - dragons dragon - drain drain - drained drain - drains drain - drake drake - dram dram - dramatis dramati - drank drank - draught draught - draughts draught - drave drave - draw draw - drawbridge drawbridg - drawer drawer - drawers drawer - draweth draweth - drawing draw - drawling drawl - drawn drawn - draws draw - drayman drayman - draymen draymen - dread dread - dreaded dread - dreadful dread - dreadfully dreadfulli - dreading dread - dreads dread - dream dream - dreamer dreamer - dreamers dreamer - dreaming dream - dreams dream - dreamt dreamt - drearning drearn - dreary dreari - dreg dreg - dregs dreg - drench drench - drenched drench - dress dress - dressed dress - dresser dresser - dressing dress - dressings dress - drest drest - drew drew - dribbling dribbl - dried dri - drier drier - dries dri - drift drift - drily drili - drink drink - drinketh drinketh - drinking drink - drinkings drink - drinks drink - driv driv - drive drive - drivelling drivel - driven driven - drives drive - driveth driveth - driving drive - drizzle drizzl - drizzled drizzl - drizzles drizzl - droit droit - drollery drolleri - dromio dromio - dromios dromio - drone drone - drones drone - droop droop - droopeth droopeth - drooping droop - droops droop - drop drop - dropheir dropheir - droplets droplet - dropp dropp - dropper dropper - droppeth droppeth - dropping drop - droppings drop - drops drop - dropsied dropsi - dropsies dropsi - dropsy dropsi - dropt dropt - dross dross - drossy drossi - drought drought - drove drove - droven droven - drovier drovier - drown drown - drowned drown - drowning drown - drowns drown - drows drow - drowse drows - drowsily drowsili - drowsiness drowsi - drowsy drowsi - drudge drudg - drudgery drudgeri - drudges drudg - drug drug - drugg drugg - drugs drug - drum drum - drumble drumbl - drummer drummer - drumming drum - drums drum - drunk drunk - drunkard drunkard - drunkards drunkard - drunken drunken - drunkenly drunkenli - drunkenness drunken - dry dry - dryness dryness - dst dst - du du - dub dub - dubb dubb - ducat ducat - ducats ducat - ducdame ducdam - duchess duchess - duchies duchi - duchy duchi - duck duck - ducking duck - ducks duck - dudgeon dudgeon - due due - duellist duellist - duello duello - duer duer - dues due - duff duff - dug dug - dugs dug - duke duke - dukedom dukedom - dukedoms dukedom - dukes duke - dulcet dulcet - dulche dulch - dull dull - dullard dullard - duller duller - dullest dullest - dulling dull - dullness dull - dulls dull - dully dulli - dulness dul - duly duli - dumain dumain - dumb dumb - dumbe dumb - dumbly dumbl - dumbness dumb - dump dump - dumps dump - dun dun - duncan duncan - dung dung - dungeon dungeon - dungeons dungeon - dunghill dunghil - dunghills dunghil - dungy dungi - dunnest dunnest - dunsinane dunsinan - dunsmore dunsmor - dunstable dunstabl - dupp dupp - durance duranc - during dure - durst durst - dusky duski - dust dust - dusted dust - dusty dusti - dutch dutch - dutchman dutchman - duteous duteou - duties duti - dutiful duti - duty duti - dwarf dwarf - dwarfish dwarfish - dwell dwell - dwellers dweller - dwelling dwell - dwells dwell - dwelt dwelt - dwindle dwindl - dy dy - dye dye - dyed dy - dyer dyer - dying dy - e e - each each - eager eager - eagerly eagerli - eagerness eager - eagle eagl - eagles eagl - eaning ean - eanlings eanl - ear ear - earing ear - earl earl - earldom earldom - earlier earlier - earliest earliest - earliness earli - earls earl - early earli - earn earn - earned earn - earnest earnest - earnestly earnestli - earnestness earnest - earns earn - ears ear - earth earth - earthen earthen - earthlier earthlier - earthly earthli - earthquake earthquak - earthquakes earthquak - earthy earthi - eas ea - ease eas - eased eas - easeful eas - eases eas - easier easier - easiest easiest - easiliest easiliest - easily easili - easiness easi - easing eas - east east - eastcheap eastcheap - easter easter - eastern eastern - eastward eastward - easy easi - eat eat - eaten eaten - eater eater - eaters eater - eating eat - eats eat - eaux eaux - eaves eav - ebb ebb - ebbing eb - ebbs ebb - ebon ebon - ebony eboni - ebrew ebrew - ecce ecc - echapper echapp - echo echo - echoes echo - eclips eclip - eclipse eclips - eclipses eclips - ecolier ecoli - ecoutez ecoutez - ecstacy ecstaci - ecstasies ecstasi - ecstasy ecstasi - ecus ecu - eden eden - edg edg - edgar edgar - edge edg - edged edg - edgeless edgeless - edges edg - edict edict - edicts edict - edifice edific - edifices edific - edified edifi - edifies edifi - edition edit - edm edm - edmund edmund - edmunds edmund - edmundsbury edmundsburi - educate educ - educated educ - education educ - edward edward - eel eel - eels eel - effect effect - effected effect - effectless effectless - effects effect - effectual effectu - effectually effectu - effeminate effemin - effigies effigi - effus effu - effuse effus - effusion effus - eftest eftest - egal egal - egally egal - eget eget - egeus egeu - egg egg - eggs egg - eggshell eggshel - eglamour eglamour - eglantine eglantin - egma egma - ego ego - egregious egregi - egregiously egregi - egress egress - egypt egypt - egyptian egyptian - egyptians egyptian - eie eie - eight eight - eighteen eighteen - eighth eighth - eightpenny eightpenni - eighty eighti - eisel eisel - either either - eject eject - eke ek - el el - elbe elb - elbow elbow - elbows elbow - eld eld - elder elder - elders elder - eldest eldest - eleanor eleanor - elect elect - elected elect - election elect - elegancy eleg - elegies elegi - element element - elements element - elephant eleph - elephants eleph - elevated elev - eleven eleven - eleventh eleventh - elf elf - elflocks elflock - eliads eliad - elinor elinor - elizabeth elizabeth - ell ell - elle ell - ellen ellen - elm elm - eloquence eloqu - eloquent eloqu - else els - elsewhere elsewher - elsinore elsinor - eltham eltham - elves elv - elvish elvish - ely eli - elysium elysium - em em - emballing embal - embalm embalm - embalms embalm - embark embark - embarked embark - embarquements embarqu - embassade embassad - embassage embassag - embassies embassi - embassy embassi - embattailed embattail - embattl embattl - embattle embattl - embay embai - embellished embellish - embers ember - emblaze emblaz - emblem emblem - emblems emblem - embodied embodi - embold embold - emboldens embolden - emboss emboss - embossed emboss - embounded embound - embowel embowel - embowell embowel - embrac embrac - embrace embrac - embraced embrac - embracement embrac - embracements embrac - embraces embrac - embracing embrac - embrasures embrasur - embroider embroid - embroidery embroideri - emhracing emhrac - emilia emilia - eminence emin - eminent emin - eminently emin - emmanuel emmanuel - emnity emniti - empale empal - emperal emper - emperess emperess - emperial emperi - emperor emperor - empery emperi - emphasis emphasi - empire empir - empirics empir - empiricutic empiricut - empleached empleach - employ emploi - employed emploi - employer employ - employment employ - employments employ - empoison empoison - empress empress - emptied empti - emptier emptier - empties empti - emptiness empti - empty empti - emptying empti - emulate emul - emulation emul - emulations emul - emulator emul - emulous emul - en en - enact enact - enacted enact - enacts enact - enactures enactur - enamell enamel - enamelled enamel - enamour enamour - enamoured enamour - enanmour enanmour - encamp encamp - encamped encamp - encave encav - enceladus enceladu - enchaf enchaf - enchafed enchaf - enchant enchant - enchanted enchant - enchanting enchant - enchantingly enchantingli - enchantment enchant - enchantress enchantress - enchants enchant - enchas encha - encircle encircl - encircled encircl - enclos enclo - enclose enclos - enclosed enclos - encloses enclos - encloseth encloseth - enclosing enclos - enclouded encloud - encompass encompass - encompassed encompass - encompasseth encompasseth - encompassment encompass - encore encor - encorporal encorpor - encount encount - encounter encount - encountered encount - encounters encount - encourage encourag - encouraged encourag - encouragement encourag - encrimsoned encrimson - encroaching encroach - encumb encumb - end end - endamage endamag - endamagement endamag - endanger endang - endart endart - endear endear - endeared endear - endeavour endeavour - endeavours endeavour - ended end - ender ender - ending end - endings end - endite endit - endless endless - endow endow - endowed endow - endowments endow - endows endow - ends end - endu endu - endue endu - endur endur - endurance endur - endure endur - endured endur - endures endur - enduring endur - endymion endymion - eneas enea - enemies enemi - enemy enemi - enernies enerni - enew enew - enfeebled enfeebl - enfeebles enfeebl - enfeoff enfeoff - enfetter enfett - enfoldings enfold - enforc enforc - enforce enforc - enforced enforc - enforcedly enforcedli - enforcement enforc - enforces enforc - enforcest enforcest - enfranched enfranch - enfranchis enfranchi - enfranchise enfranchis - enfranchised enfranchis - enfranchisement enfranchis - enfreed enfre - enfreedoming enfreedom - engag engag - engage engag - engaged engag - engagements engag - engaging engag - engaol engaol - engend engend - engender engend - engenders engend - engilds engild - engine engin - engineer engin - enginer engin - engines engin - engirt engirt - england england - english english - englishman englishman - englishmen englishmen - engluts englut - englutted englut - engraffed engraf - engraft engraft - engrafted engraft - engrav engrav - engrave engrav - engross engross - engrossed engross - engrossest engrossest - engrossing engross - engrossments engross - enguard enguard - enigma enigma - enigmatical enigmat - enjoin enjoin - enjoined enjoin - enjoy enjoi - enjoyed enjoi - enjoyer enjoy - enjoying enjoi - enjoys enjoi - enkindle enkindl - enkindled enkindl - enlard enlard - enlarg enlarg - enlarge enlarg - enlarged enlarg - enlargement enlarg - enlargeth enlargeth - enlighten enlighten - enlink enlink - enmesh enmesh - enmities enmiti - enmity enmiti - ennoble ennobl - ennobled ennobl - enobarb enobarb - enobarbus enobarbu - enon enon - enormity enorm - enormous enorm - enough enough - enow enow - enpatron enpatron - enpierced enpierc - enquir enquir - enquire enquir - enquired enquir - enrag enrag - enrage enrag - enraged enrag - enrages enrag - enrank enrank - enrapt enrapt - enrich enrich - enriched enrich - enriches enrich - enridged enridg - enrings enr - enrob enrob - enrobe enrob - enroll enrol - enrolled enrol - enrooted enroot - enrounded enround - enschedul enschedul - ensconce ensconc - ensconcing ensconc - enseamed enseam - ensear ensear - enseigne enseign - enseignez enseignez - ensemble ensembl - enshelter enshelt - enshielded enshield - enshrines enshrin - ensign ensign - ensigns ensign - enskied enski - ensman ensman - ensnare ensnar - ensnared ensnar - ensnareth ensnareth - ensteep ensteep - ensu ensu - ensue ensu - ensued ensu - ensues ensu - ensuing ensu - enswathed enswath - ent ent - entail entail - entame entam - entangled entangl - entangles entangl - entendre entendr - enter enter - entered enter - entering enter - enterprise enterpris - enterprises enterpris - enters enter - entertain entertain - entertained entertain - entertainer entertain - entertaining entertain - entertainment entertain - entertainments entertain - enthrall enthral - enthralled enthral - enthron enthron - enthroned enthron - entice entic - enticements entic - enticing entic - entire entir - entirely entir - entitle entitl - entitled entitl - entitling entitl - entomb entomb - entombed entomb - entrails entrail - entrance entranc - entrances entranc - entrap entrap - entrapp entrapp - entre entr - entreat entreat - entreated entreat - entreaties entreati - entreating entreat - entreatments entreat - entreats entreat - entreaty entreati - entrench entrench - entry entri - entwist entwist - envelop envelop - envenom envenom - envenomed envenom - envenoms envenom - envied envi - envies envi - envious enviou - enviously envious - environ environ - environed environ - envoy envoi - envy envi - envying envi - enwheel enwheel - enwombed enwomb - enwraps enwrap - ephesian ephesian - ephesians ephesian - ephesus ephesu - epicure epicur - epicurean epicurean - epicures epicur - epicurism epicur - epicurus epicuru - epidamnum epidamnum - epidaurus epidauru - epigram epigram - epilepsy epilepsi - epileptic epilept - epilogue epilogu - epilogues epilogu - epistles epistl - epistrophus epistrophu - epitaph epitaph - epitaphs epitaph - epithet epithet - epitheton epitheton - epithets epithet - epitome epitom - equal equal - equalities equal - equality equal - equall equal - equally equal - equalness equal - equals equal - equinoctial equinocti - equinox equinox - equipage equipag - equity equiti - equivocal equivoc - equivocate equivoc - equivocates equivoc - equivocation equivoc - equivocator equivoc - er er - erbear erbear - erbearing erbear - erbears erbear - erbeat erbeat - erblows erblow - erboard erboard - erborne erborn - ercame ercam - ercast ercast - ercharg ercharg - ercharged ercharg - ercharging ercharg - ercles ercl - ercome ercom - ercover ercov - ercrows ercrow - erdoing erdo - ere er - erebus erebu - erect erect - erected erect - erecting erect - erection erect - erects erect - erewhile erewhil - erflourish erflourish - erflow erflow - erflowing erflow - erflows erflow - erfraught erfraught - erga erga - ergalled ergal - erglanced erglanc - ergo ergo - ergone ergon - ergrow ergrow - ergrown ergrown - ergrowth ergrowth - erhang erhang - erhanging erhang - erhasty erhasti - erhear erhear - erheard erheard - eringoes eringo - erjoy erjoi - erleap erleap - erleaps erleap - erleavens erleaven - erlook erlook - erlooking erlook - ermaster ermast - ermengare ermengar - ermount ermount - ern ern - ernight ernight - eros ero - erpaid erpaid - erparted erpart - erpast erpast - erpays erpai - erpeer erpeer - erperch erperch - erpicturing erpictur - erpingham erpingham - erposting erpost - erpow erpow - erpress erpress - erpressed erpress - err err - errand errand - errands errand - errant errant - errate errat - erraught erraught - erreaches erreach - erred er - errest errest - erring er - erroneous erron - error error - errors error - errs err - errule errul - errun errun - erset erset - ershade ershad - ershades ershad - ershine ershin - ershot ershot - ersized ersiz - erskip erskip - erslips erslip - erspreads erspread - erst erst - erstare erstar - erstep erstep - erstunk erstunk - ersway erswai - ersways erswai - erswell erswel - erta erta - ertake ertak - erteemed erteem - erthrow erthrow - erthrown erthrown - erthrows erthrow - ertook ertook - ertop ertop - ertopping ertop - ertrip ertrip - erturn erturn - erudition erudit - eruption erupt - eruptions erupt - ervalues ervalu - erwalk erwalk - erwatch erwatch - erween erween - erweens erween - erweigh erweigh - erweighs erweigh - erwhelm erwhelm - erwhelmed erwhelm - erworn erworn - es es - escalus escalu - escap escap - escape escap - escaped escap - escapes escap - eschew eschew - escoted escot - esill esil - especial especi - especially especi - esperance esper - espials espial - espied espi - espies espi - espous espou - espouse espous - espy espi - esquire esquir - esquires esquir - essay essai - essays essai - essence essenc - essential essenti - essentially essenti - esses ess - essex essex - est est - establish establish - established establish - estate estat - estates estat - esteem esteem - esteemed esteem - esteemeth esteemeth - esteeming esteem - esteems esteem - estimable estim - estimate estim - estimation estim - estimations estim - estime estim - estranged estrang - estridge estridg - estridges estridg - et et - etc etc - etceteras etcetera - ete et - eternal etern - eternally etern - eterne etern - eternity etern - eterniz eterniz - etes et - ethiop ethiop - ethiope ethiop - ethiopes ethiop - ethiopian ethiopian - etna etna - eton eton - etre etr - eunuch eunuch - eunuchs eunuch - euphrates euphrat - euphronius euphroniu - euriphile euriphil - europa europa - europe europ - ev ev - evade evad - evades evad - evans evan - evasion evas - evasions evas - eve ev - even even - evening even - evenly evenli - event event - eventful event - events event - ever ever - everlasting everlast - everlastingly everlastingli - evermore evermor - every everi - everyone everyon - everything everyth - everywhere everywher - evidence evid - evidences evid - evident evid - evil evil - evilly evilli - evils evil - evitate evit - ewe ew - ewer ewer - ewers ewer - ewes ew - exact exact - exacted exact - exactest exactest - exacting exact - exaction exact - exactions exact - exactly exactli - exacts exact - exalt exalt - exalted exalt - examin examin - examination examin - examinations examin - examine examin - examined examin - examines examin - exampl exampl - example exampl - exampled exampl - examples exampl - exasperate exasper - exasperates exasper - exceed exce - exceeded exceed - exceedeth exceedeth - exceeding exceed - exceedingly exceedingli - exceeds exce - excel excel - excelled excel - excellence excel - excellencies excel - excellency excel - excellent excel - excellently excel - excelling excel - excels excel - except except - excepted except - excepting except - exception except - exceptions except - exceptless exceptless - excess excess - excessive excess - exchang exchang - exchange exchang - exchanged exchang - exchequer exchequ - exchequers exchequ - excite excit - excited excit - excitements excit - excites excit - exclaim exclaim - exclaims exclaim - exclamation exclam - exclamations exclam - excludes exclud - excommunicate excommun - excommunication excommun - excrement excrement - excrements excrement - excursion excurs - excursions excurs - excus excu - excusable excus - excuse excus - excused excus - excuses excus - excusez excusez - excusing excus - execrable execr - execrations execr - execute execut - executed execut - executing execut - execution execut - executioner execution - executioners execution - executor executor - executors executor - exempt exempt - exempted exempt - exequies exequi - exercise exercis - exercises exercis - exeter exet - exeunt exeunt - exhal exhal - exhalation exhal - exhalations exhal - exhale exhal - exhales exhal - exhaust exhaust - exhibit exhibit - exhibiters exhibit - exhibition exhibit - exhort exhort - exhortation exhort - exigent exig - exil exil - exile exil - exiled exil - exion exion - exist exist - exists exist - exit exit - exits exit - exorciser exorcis - exorcisms exorc - exorcist exorcist - expect expect - expectance expect - expectancy expect - expectation expect - expectations expect - expected expect - expecters expect - expecting expect - expects expect - expedience expedi - expedient expedi - expediently expedi - expedition expedit - expeditious expediti - expel expel - expell expel - expelling expel - expels expel - expend expend - expense expens - expenses expens - experienc experienc - experience experi - experiences experi - experiment experi - experimental experiment - experiments experi - expert expert - expertness expert - expiate expiat - expiation expiat - expir expir - expiration expir - expire expir - expired expir - expires expir - expiring expir - explication explic - exploit exploit - exploits exploit - expos expo - expose expos - exposing expos - exposition exposit - expositor expositor - expostulate expostul - expostulation expostul - exposture expostur - exposure exposur - expound expound - expounded expound - express express - expressed express - expresseth expresseth - expressing express - expressive express - expressly expressli - expressure expressur - expuls expul - expulsion expuls - exquisite exquisit - exsufflicate exsuffl - extant extant - extemporal extempor - extemporally extempor - extempore extempor - extend extend - extended extend - extends extend - extent extent - extenuate extenu - extenuated extenu - extenuates extenu - extenuation extenu - exterior exterior - exteriorly exteriorli - exteriors exterior - extermin extermin - extern extern - external extern - extinct extinct - extincted extinct - extincture extinctur - extinguish extinguish - extirp extirp - extirpate extirp - extirped extirp - extol extol - extoll extol - extolment extol - exton exton - extort extort - extorted extort - extortion extort - extortions extort - extra extra - extract extract - extracted extract - extracting extract - extraordinarily extraordinarili - extraordinary extraordinari - extraught extraught - extravagancy extravag - extravagant extravag - extreme extrem - extremely extrem - extremes extrem - extremest extremest - extremities extrem - extremity extrem - exuent exuent - exult exult - exultation exult - ey ey - eyas eya - eyases eyas - eye ey - eyeball eyebal - eyeballs eyebal - eyebrow eyebrow - eyebrows eyebrow - eyed ei - eyeless eyeless - eyelid eyelid - eyelids eyelid - eyes ey - eyesight eyesight - eyestrings eyestr - eying ei - eyne eyn - eyrie eyri - fa fa - fabian fabian - fable fabl - fables fabl - fabric fabric - fabulous fabul - fac fac - face face - faced face - facere facer - faces face - faciant faciant - facile facil - facility facil - facinerious facineri - facing face - facit facit - fact fact - faction faction - factionary factionari - factions faction - factious factiou - factor factor - factors factor - faculties faculti - faculty faculti - fade fade - faded fade - fadeth fadeth - fadge fadg - fading fade - fadings fade - fadom fadom - fadoms fadom - fagot fagot - fagots fagot - fail fail - failing fail - fails fail - fain fain - faint faint - fainted faint - fainter fainter - fainting faint - faintly faintli - faintness faint - faints faint - fair fair - fairer fairer - fairest fairest - fairies fairi - fairing fair - fairings fair - fairly fairli - fairness fair - fairs fair - fairwell fairwel - fairy fairi - fais fai - fait fait - faites fait - faith faith - faithful faith - faithfull faithful - faithfully faithfulli - faithless faithless - faiths faith - faitors faitor - fal fal - falchion falchion - falcon falcon - falconbridge falconbridg - falconer falcon - falconers falcon - fall fall - fallacy fallaci - fallen fallen - falleth falleth - falliable falliabl - fallible fallibl - falling fall - fallow fallow - fallows fallow - falls fall - fally falli - falorous falor - false fals - falsehood falsehood - falsely fals - falseness fals - falser falser - falsify falsifi - falsing fals - falstaff falstaff - falstaffs falstaff - falter falter - fam fam - fame fame - famed fame - familiar familiar - familiarity familiar - familiarly familiarli - familiars familiar - family famili - famine famin - famish famish - famished famish - famous famou - famoused famous - famously famous - fan fan - fanatical fanat - fancies fanci - fancy fanci - fane fane - fanes fane - fang fang - fangled fangl - fangless fangless - fangs fang - fann fann - fanning fan - fans fan - fantasied fantasi - fantasies fantasi - fantastic fantast - fantastical fantast - fantastically fantast - fantasticoes fantastico - fantasy fantasi - fap fap - far far - farborough farborough - farced farc - fardel fardel - fardels fardel - fare fare - fares fare - farewell farewel - farewells farewel - fariner farin - faring fare - farm farm - farmer farmer - farmhouse farmhous - farms farm - farre farr - farrow farrow - farther farther - farthest farthest - farthing farth - farthingale farthingal - farthingales farthingal - farthings farth - fartuous fartuou - fas fa - fashion fashion - fashionable fashion - fashioning fashion - fashions fashion - fast fast - fasted fast - fasten fasten - fastened fasten - faster faster - fastest fastest - fasting fast - fastly fastli - fastolfe fastolf - fasts fast - fat fat - fatal fatal - fatally fatal - fate fate - fated fate - fates fate - father father - fathered father - fatherless fatherless - fatherly fatherli - fathers father - fathom fathom - fathomless fathomless - fathoms fathom - fatigate fatig - fatness fat - fats fat - fatted fat - fatter fatter - fattest fattest - fatting fat - fatuus fatuu - fauconbridge fauconbridg - faulconbridge faulconbridg - fault fault - faultiness faulti - faultless faultless - faults fault - faulty faulti - fausse fauss - fauste faust - faustuses faustus - faut faut - favor favor - favorable favor - favorably favor - favors favor - favour favour - favourable favour - favoured favour - favouredly favouredli - favourer favour - favourers favour - favouring favour - favourite favourit - favourites favourit - favours favour - favout favout - fawn fawn - fawneth fawneth - fawning fawn - fawns fawn - fay fai - fe fe - fealty fealti - fear fear - feared fear - fearest fearest - fearful fear - fearfull fearful - fearfully fearfulli - fearfulness fear - fearing fear - fearless fearless - fears fear - feast feast - feasted feast - feasting feast - feasts feast - feat feat - feated feat - feater feater - feather feather - feathered feather - feathers feather - featly featli - feats feat - featur featur - feature featur - featured featur - featureless featureless - features featur - february februari - fecks feck - fed fed - fedary fedari - federary federari - fee fee - feeble feebl - feebled feebl - feebleness feebl - feebling feebl - feebly feebli - feed feed - feeder feeder - feeders feeder - feedeth feedeth - feeding feed - feeds feed - feel feel - feeler feeler - feeling feel - feelingly feelingli - feels feel - fees fee - feet feet - fehemently fehement - feign feign - feigned feign - feigning feign - feil feil - feith feith - felicitate felicit - felicity felic - fell fell - fellest fellest - fellies felli - fellow fellow - fellowly fellowli - fellows fellow - fellowship fellowship - fellowships fellowship - fells fell - felon felon - felonious feloni - felony feloni - felt felt - female femal - females femal - feminine feminin - fen fen - fenc fenc - fence fenc - fencer fencer - fencing fenc - fends fend - fennel fennel - fenny fenni - fens fen - fenton fenton - fer fer - ferdinand ferdinand - fere fere - fernseed fernse - ferrara ferrara - ferrers ferrer - ferret ferret - ferry ferri - ferryman ferryman - fertile fertil - fertility fertil - fervency fervenc - fervour fervour - fery feri - fest fest - feste fest - fester fester - festinate festin - festinately festin - festival festiv - festivals festiv - fet fet - fetch fetch - fetches fetch - fetching fetch - fetlock fetlock - fetlocks fetlock - fett fett - fetter fetter - fettering fetter - fetters fetter - fettle fettl - feu feu - feud feud - fever fever - feverous fever - fevers fever - few few - fewer fewer - fewest fewest - fewness few - fickle fickl - fickleness fickl - fico fico - fiction fiction - fiddle fiddl - fiddler fiddler - fiddlestick fiddlestick - fidele fidel - fidelicet fidelicet - fidelity fidel - fidius fidiu - fie fie - field field - fielded field - fields field - fiend fiend - fiends fiend - fierce fierc - fiercely fierc - fierceness fierc - fiery fieri - fife fife - fifes fife - fifteen fifteen - fifteens fifteen - fifteenth fifteenth - fifth fifth - fifty fifti - fiftyfold fiftyfold - fig fig - fight fight - fighter fighter - fightest fightest - fighteth fighteth - fighting fight - fights fight - figo figo - figs fig - figur figur - figure figur - figured figur - figures figur - figuring figur - fike fike - fil fil - filberts filbert - filch filch - filches filch - filching filch - file file - filed file - files file - filial filial - filius filiu - fill fill - filled fill - fillet fillet - filling fill - fillip fillip - fills fill - filly filli - film film - fils fil - filth filth - filths filth - filthy filthi - fin fin - finally final - finch finch - find find - finder finder - findeth findeth - finding find - findings find - finds find - fine fine - fineless fineless - finely fine - finem finem - fineness fine - finer finer - fines fine - finest finest - fing fing - finger finger - fingering finger - fingers finger - fingre fingr - fingres fingr - finical finic - finish finish - finished finish - finisher finish - finless finless - finn finn - fins fin - finsbury finsburi - fir fir - firago firago - fire fire - firebrand firebrand - firebrands firebrand - fired fire - fires fire - firework firework - fireworks firework - firing fire - firk firk - firm firm - firmament firmament - firmly firmli - firmness firm - first first - firstlings firstl - fish fish - fisher fisher - fishermen fishermen - fishers fisher - fishes fish - fishified fishifi - fishmonger fishmong - fishpond fishpond - fisnomy fisnomi - fist fist - fisting fist - fists fist - fistula fistula - fit fit - fitchew fitchew - fitful fit - fitly fitli - fitment fitment - fitness fit - fits fit - fitted fit - fitter fitter - fittest fittest - fitteth fitteth - fitting fit - fitzwater fitzwat - five five - fivepence fivep - fives five - fix fix - fixed fix - fixes fix - fixeth fixeth - fixing fix - fixture fixtur - fl fl - flag flag - flagging flag - flagon flagon - flagons flagon - flags flag - flail flail - flakes flake - flaky flaki - flam flam - flame flame - flamen flamen - flamens flamen - flames flame - flaming flame - flaminius flaminiu - flanders flander - flannel flannel - flap flap - flaring flare - flash flash - flashes flash - flashing flash - flask flask - flat flat - flatly flatli - flatness flat - flats flat - flatt flatt - flatter flatter - flattered flatter - flatterer flatter - flatterers flatter - flatterest flatterest - flatteries flatteri - flattering flatter - flatters flatter - flattery flatteri - flaunts flaunt - flavio flavio - flavius flaviu - flaw flaw - flaws flaw - flax flax - flaxen flaxen - flay flai - flaying flai - flea flea - fleance fleanc - fleas flea - flecked fleck - fled fled - fledge fledg - flee flee - fleec fleec - fleece fleec - fleeces fleec - fleer fleer - fleering fleer - fleers fleer - fleet fleet - fleeter fleeter - fleeting fleet - fleming fleme - flemish flemish - flesh flesh - fleshes flesh - fleshly fleshli - fleshment fleshment - fleshmonger fleshmong - flew flew - flexible flexibl - flexure flexur - flibbertigibbet flibbertigibbet - flickering flicker - flidge flidg - fliers flier - flies fli - flieth flieth - flight flight - flights flight - flighty flighti - flinch flinch - fling fling - flint flint - flints flint - flinty flinti - flirt flirt - float float - floated float - floating float - flock flock - flocks flock - flood flood - floodgates floodgat - floods flood - floor floor - flora flora - florence florenc - florentine florentin - florentines florentin - florentius florentiu - florizel florizel - flote flote - floulish floulish - flour flour - flourish flourish - flourishes flourish - flourisheth flourisheth - flourishing flourish - flout flout - flouted flout - flouting flout - flouts flout - flow flow - flowed flow - flower flower - flowerets floweret - flowers flower - flowing flow - flown flown - flows flow - fluellen fluellen - fluent fluent - flung flung - flush flush - flushing flush - fluster fluster - flute flute - flutes flute - flutter flutter - flux flux - fluxive fluxiv - fly fly - flying fly - fo fo - foal foal - foals foal - foam foam - foamed foam - foaming foam - foams foam - foamy foami - fob fob - focative foc - fodder fodder - foe foe - foeman foeman - foemen foemen - foes foe - fog fog - foggy foggi - fogs fog - foh foh - foi foi - foil foil - foiled foil - foils foil - foin foin - foining foin - foins foin - fois foi - foison foison - foisons foison - foist foist - foix foix - fold fold - folded fold - folds fold - folio folio - folk folk - folks folk - follies folli - follow follow - followed follow - follower follow - followers follow - followest followest - following follow - follows follow - folly folli - fond fond - fonder fonder - fondly fondli - fondness fond - font font - fontibell fontibel - food food - fool fool - fooleries fooleri - foolery fooleri - foolhardy foolhardi - fooling fool - foolish foolish - foolishly foolishli - foolishness foolish - fools fool - foot foot - football footbal - footboy footboi - footboys footboi - footed foot - footfall footfal - footing foot - footman footman - footmen footmen - footpath footpath - footsteps footstep - footstool footstool - fopp fopp - fopped fop - foppery fopperi - foppish foppish - fops fop - for for - forage forag - foragers forag - forbade forbad - forbear forbear - forbearance forbear - forbears forbear - forbid forbid - forbidden forbidden - forbiddenly forbiddenli - forbids forbid - forbod forbod - forborne forborn - forc forc - force forc - forced forc - forceful forc - forceless forceless - forces forc - forcible forcibl - forcibly forcibl - forcing forc - ford ford - fordid fordid - fordo fordo - fordoes fordo - fordone fordon - fore fore - forecast forecast - forefather forefath - forefathers forefath - forefinger forefing - forego forego - foregone foregon - forehand forehand - forehead forehead - foreheads forehead - forehorse forehors - foreign foreign - foreigner foreign - foreigners foreign - foreknowing foreknow - foreknowledge foreknowledg - foremost foremost - forenamed forenam - forenoon forenoon - forerun forerun - forerunner forerunn - forerunning forerun - foreruns forerun - foresaid foresaid - foresaw foresaw - foresay foresai - foresee forese - foreseeing forese - foresees forese - foreshow foreshow - foreskirt foreskirt - forespent foresp - forest forest - forestall forestal - forestalled forestal - forester forest - foresters forest - forests forest - foretell foretel - foretelling foretel - foretells foretel - forethink forethink - forethought forethought - foretold foretold - forever forev - foreward foreward - forewarn forewarn - forewarned forewarn - forewarning forewarn - forfeit forfeit - forfeited forfeit - forfeiters forfeit - forfeiting forfeit - forfeits forfeit - forfeiture forfeitur - forfeitures forfeitur - forfend forfend - forfended forfend - forg forg - forgave forgav - forge forg - forged forg - forgeries forgeri - forgery forgeri - forges forg - forget forget - forgetful forget - forgetfulness forget - forgetive forget - forgets forget - forgetting forget - forgive forgiv - forgiven forgiven - forgiveness forgiv - forgo forgo - forgoing forgo - forgone forgon - forgot forgot - forgotten forgotten - fork fork - forked fork - forks fork - forlorn forlorn - form form - formal formal - formally formal - formed form - former former - formerly formerli - formless formless - forms form - fornication fornic - fornications fornic - fornicatress fornicatress - forres forr - forrest forrest - forsake forsak - forsaken forsaken - forsaketh forsaketh - forslow forslow - forsook forsook - forsooth forsooth - forspent forspent - forspoke forspok - forswear forswear - forswearing forswear - forswore forswor - forsworn forsworn - fort fort - forted fort - forth forth - forthcoming forthcom - forthlight forthlight - forthright forthright - forthwith forthwith - fortification fortif - fortifications fortif - fortified fortifi - fortifies fortifi - fortify fortifi - fortinbras fortinbra - fortitude fortitud - fortnight fortnight - fortress fortress - fortresses fortress - forts fort - fortun fortun - fortuna fortuna - fortunate fortun - fortunately fortun - fortune fortun - fortuned fortun - fortunes fortun - fortward fortward - forty forti - forum forum - forward forward - forwarding forward - forwardness forward - forwards forward - forwearied forweari - fosset fosset - fost fost - foster foster - fostered foster - fought fought - foughten foughten - foul foul - fouler fouler - foulest foulest - foully foulli - foulness foul - found found - foundation foundat - foundations foundat - founded found - founder founder - fount fount - fountain fountain - fountains fountain - founts fount - four four - fourscore fourscor - fourteen fourteen - fourth fourth - foutra foutra - fowl fowl - fowler fowler - fowling fowl - fowls fowl - fox fox - foxes fox - foxship foxship - fracted fract - fraction fraction - fractions fraction - fragile fragil - fragment fragment - fragments fragment - fragrant fragrant - frail frail - frailer frailer - frailties frailti - frailty frailti - fram fram - frame frame - framed frame - frames frame - frampold frampold - fran fran - francais francai - france franc - frances franc - franchise franchis - franchised franchis - franchisement franchis - franchises franchis - franciae francia - francis franci - francisca francisca - franciscan franciscan - francisco francisco - frank frank - franker franker - frankfort frankfort - franklin franklin - franklins franklin - frankly frankli - frankness frank - frantic frantic - franticly franticli - frateretto frateretto - fratrum fratrum - fraud fraud - fraudful fraud - fraught fraught - fraughtage fraughtag - fraughting fraught - fray frai - frays frai - freckl freckl - freckled freckl - freckles freckl - frederick frederick - free free - freed freed - freedom freedom - freedoms freedom - freehearted freeheart - freelier freelier - freely freeli - freeman freeman - freemen freemen - freeness freeness - freer freer - frees free - freestone freeston - freetown freetown - freeze freez - freezes freez - freezing freez - freezings freez - french french - frenchman frenchman - frenchmen frenchmen - frenchwoman frenchwoman - frenzy frenzi - frequent frequent - frequents frequent - fresh fresh - fresher fresher - freshes fresh - freshest freshest - freshly freshli - freshness fresh - fret fret - fretful fret - frets fret - fretted fret - fretten fretten - fretting fret - friar friar - friars friar - friday fridai - fridays fridai - friend friend - friended friend - friending friend - friendless friendless - friendliness friendli - friendly friendli - friends friend - friendship friendship - friendships friendship - frieze friez - fright fright - frighted fright - frightened frighten - frightful fright - frighting fright - frights fright - fringe fring - fringed fring - frippery fripperi - frisk frisk - fritters fritter - frivolous frivol - fro fro - frock frock - frog frog - frogmore frogmor - froissart froissart - frolic frolic - from from - front front - fronted front - frontier frontier - frontiers frontier - fronting front - frontlet frontlet - fronts front - frost frost - frosts frost - frosty frosti - froth froth - froward froward - frown frown - frowning frown - frowningly frowningli - frowns frown - froze froze - frozen frozen - fructify fructifi - frugal frugal - fruit fruit - fruiterer fruiter - fruitful fruit - fruitfully fruitfulli - fruitfulness fruit - fruition fruition - fruitless fruitless - fruits fruit - frush frush - frustrate frustrat - frutify frutifi - fry fry - fubb fubb - fuel fuel - fugitive fugit - fulfil fulfil - fulfill fulfil - fulfilling fulfil - fulfils fulfil - full full - fullam fullam - fuller fuller - fullers fuller - fullest fullest - fullness full - fully fulli - fulness ful - fulsome fulsom - fulvia fulvia - fum fum - fumble fumbl - fumbles fumbl - fumblest fumblest - fumbling fumbl - fume fume - fumes fume - fuming fume - fumiter fumit - fumitory fumitori - fun fun - function function - functions function - fundamental fundament - funeral funer - funerals funer - fur fur - furbish furbish - furies furi - furious furiou - furlongs furlong - furnace furnac - furnaces furnac - furnish furnish - furnished furnish - furnishings furnish - furniture furnitur - furnival furniv - furor furor - furr furr - furrow furrow - furrowed furrow - furrows furrow - furth furth - further further - furtherance further - furtherer further - furthermore furthermor - furthest furthest - fury furi - furze furz - furzes furz - fust fust - fustian fustian - fustilarian fustilarian - fusty fusti - fut fut - future futur - futurity futur - g g - gabble gabbl - gaberdine gaberdin - gabriel gabriel - gad gad - gadding gad - gads gad - gadshill gadshil - gag gag - gage gage - gaged gage - gagg gagg - gaging gage - gagne gagn - gain gain - gained gain - gainer gainer - gaingiving gaingiv - gains gain - gainsaid gainsaid - gainsay gainsai - gainsaying gainsai - gainsays gainsai - gainst gainst - gait gait - gaited gait - galathe galath - gale gale - galen galen - gales gale - gall gall - gallant gallant - gallantly gallantli - gallantry gallantri - gallants gallant - galled gall - gallery galleri - galley gallei - galleys gallei - gallia gallia - gallian gallian - galliard galliard - galliasses galliass - gallimaufry gallimaufri - galling gall - gallons gallon - gallop gallop - galloping gallop - gallops gallop - gallow gallow - galloway gallowai - gallowglasses gallowglass - gallows gallow - gallowses gallows - galls gall - gallus gallu - gam gam - gambol gambol - gambold gambold - gambols gambol - gamboys gamboi - game game - gamers gamer - games game - gamesome gamesom - gamester gamest - gaming game - gammon gammon - gamut gamut - gan gan - gangren gangren - ganymede ganymed - gaol gaol - gaoler gaoler - gaolers gaoler - gaols gaol - gap gap - gape gape - gapes gape - gaping gape - gar gar - garb garb - garbage garbag - garboils garboil - garcon garcon - gard gard - garde gard - garden garden - gardener garden - gardeners garden - gardens garden - gardez gardez - gardiner gardin - gardon gardon - gargantua gargantua - gargrave gargrav - garish garish - garland garland - garlands garland - garlic garlic - garment garment - garments garment - garmet garmet - garner garner - garners garner - garnish garnish - garnished garnish - garret garret - garrison garrison - garrisons garrison - gart gart - garter garter - garterd garterd - gartering garter - garters garter - gascony gasconi - gash gash - gashes gash - gaskins gaskin - gasp gasp - gasping gasp - gasted gast - gastness gast - gat gat - gate gate - gated gate - gates gate - gath gath - gather gather - gathered gather - gathering gather - gathers gather - gatories gatori - gatory gatori - gaud gaud - gaudeo gaudeo - gaudy gaudi - gauge gaug - gaul gaul - gaultree gaultre - gaunt gaunt - gauntlet gauntlet - gauntlets gauntlet - gav gav - gave gave - gavest gavest - gawded gawd - gawds gawd - gawsey gawsei - gay gai - gayness gay - gaz gaz - gaze gaze - gazed gaze - gazer gazer - gazers gazer - gazes gaze - gazeth gazeth - gazing gaze - gear gear - geck geck - geese gees - geffrey geffrei - geld geld - gelded geld - gelding geld - gelida gelida - gelidus gelidu - gelt gelt - gem gem - geminy gemini - gems gem - gen gen - gender gender - genders gender - general gener - generally gener - generals gener - generation gener - generations gener - generative gener - generosity generos - generous gener - genitive genit - genitivo genitivo - genius geniu - gennets gennet - genoa genoa - genoux genoux - gens gen - gent gent - gentilhomme gentilhomm - gentility gentil - gentle gentl - gentlefolks gentlefolk - gentleman gentleman - gentlemanlike gentlemanlik - gentlemen gentlemen - gentleness gentl - gentler gentler - gentles gentl - gentlest gentlest - gentlewoman gentlewoman - gentlewomen gentlewomen - gently gentli - gentry gentri - george georg - gerard gerard - germaines germain - germains germain - german german - germane german - germans german - germany germani - gertrude gertrud - gest gest - gests gest - gesture gestur - gestures gestur - get get - getrude getrud - gets get - getter getter - getting get - ghastly ghastli - ghost ghost - ghosted ghost - ghostly ghostli - ghosts ghost - gi gi - giant giant - giantess giantess - giantlike giantlik - giants giant - gib gib - gibber gibber - gibbet gibbet - gibbets gibbet - gibe gibe - giber giber - gibes gibe - gibing gibe - gibingly gibingli - giddily giddili - giddiness giddi - giddy giddi - gift gift - gifts gift - gig gig - giglets giglet - giglot giglot - gilbert gilbert - gild gild - gilded gild - gilding gild - gilliams gilliam - gillian gillian - gills gill - gillyvors gillyvor - gilt gilt - gimmal gimmal - gimmers gimmer - gin gin - ging ging - ginger ginger - gingerbread gingerbread - gingerly gingerli - ginn ginn - gins gin - gioucestershire gioucestershir - gipes gipe - gipsies gipsi - gipsy gipsi - gird gird - girded gird - girdle girdl - girdled girdl - girdles girdl - girdling girdl - girl girl - girls girl - girt girt - girth girth - gis gi - giv giv - give give - given given - giver giver - givers giver - gives give - givest givest - giveth giveth - giving give - givings give - glad glad - gladded glad - gladding glad - gladly gladli - gladness glad - glamis glami - glanc glanc - glance glanc - glanced glanc - glances glanc - glancing glanc - glanders glander - glansdale glansdal - glare glare - glares glare - glass glass - glasses glass - glassy glassi - glaz glaz - glazed glaze - gleams gleam - glean glean - gleaned glean - gleaning glean - gleeful gleeful - gleek gleek - gleeking gleek - gleeks gleek - glend glend - glendower glendow - glib glib - glide glide - glided glide - glides glide - glideth glideth - gliding glide - glimmer glimmer - glimmering glimmer - glimmers glimmer - glimpse glimps - glimpses glimps - glist glist - glistening glisten - glister glister - glistering glister - glisters glister - glitt glitt - glittering glitter - globe globe - globes globe - glooming gloom - gloomy gloomi - glories glori - glorified glorifi - glorify glorifi - glorious gloriou - gloriously glorious - glory glori - glose glose - gloss gloss - glosses gloss - glou glou - glouceste gloucest - gloucester gloucest - gloucestershire gloucestershir - glove glove - glover glover - gloves glove - glow glow - glowed glow - glowing glow - glowworm glowworm - gloz gloz - gloze gloze - glozes gloze - glu glu - glue glue - glued glu - glues glue - glut glut - glutt glutt - glutted glut - glutton glutton - gluttoning glutton - gluttony gluttoni - gnarled gnarl - gnarling gnarl - gnat gnat - gnats gnat - gnaw gnaw - gnawing gnaw - gnawn gnawn - gnaws gnaw - go go - goad goad - goaded goad - goads goad - goal goal - goat goat - goatish goatish - goats goat - gobbets gobbet - gobbo gobbo - goblet goblet - goblets goblet - goblin goblin - goblins goblin - god god - godded god - godden godden - goddess goddess - goddesses goddess - goddild goddild - godfather godfath - godfathers godfath - godhead godhead - godlike godlik - godliness godli - godly godli - godmother godmoth - gods god - godson godson - goer goer - goers goer - goes goe - goest goest - goeth goeth - goffe goff - gogs gog - going go - gold gold - golden golden - goldenly goldenli - goldsmith goldsmith - goldsmiths goldsmith - golgotha golgotha - goliases golias - goliath goliath - gon gon - gondola gondola - gondolier gondoli - gone gone - goneril goneril - gong gong - gonzago gonzago - gonzalo gonzalo - good good - goodfellow goodfellow - goodlier goodlier - goodliest goodliest - goodly goodli - goodman goodman - goodness good - goodnight goodnight - goodrig goodrig - goods good - goodwife goodwif - goodwill goodwil - goodwin goodwin - goodwins goodwin - goodyear goodyear - goodyears goodyear - goose goos - gooseberry gooseberri - goosequills goosequil - goot goot - gor gor - gorbellied gorbelli - gorboduc gorboduc - gordian gordian - gore gore - gored gore - gorg gorg - gorge gorg - gorgeous gorgeou - gorget gorget - gorging gorg - gorgon gorgon - gormandize gormand - gormandizing gormand - gory gori - gosling gosl - gospel gospel - gospels gospel - goss goss - gossamer gossam - gossip gossip - gossiping gossip - gossiplike gossiplik - gossips gossip - got got - goth goth - goths goth - gotten gotten - gourd gourd - gout gout - gouts gout - gouty gouti - govern govern - governance govern - governed govern - governess gover - government govern - governor governor - governors governor - governs govern - gower gower - gown gown - gowns gown - grac grac - grace grace - graced grace - graceful grace - gracefully gracefulli - graceless graceless - graces grace - gracing grace - gracious graciou - graciously gracious - gradation gradat - graff graff - graffing graf - graft graft - grafted graft - grafters grafter - grain grain - grained grain - grains grain - gramercies gramerci - gramercy gramerci - grammar grammar - grand grand - grandam grandam - grandame grandam - grandchild grandchild - grande grand - grandeur grandeur - grandfather grandfath - grandjurors grandjuror - grandmother grandmoth - grandpre grandpr - grandsir grandsir - grandsire grandsir - grandsires grandsir - grange grang - grant grant - granted grant - granting grant - grants grant - grape grape - grapes grape - grapple grappl - grapples grappl - grappling grappl - grasp grasp - grasped grasp - grasps grasp - grass grass - grasshoppers grasshopp - grassy grassi - grate grate - grated grate - grateful grate - grates grate - gratiano gratiano - gratify gratifi - gratii gratii - gratillity gratil - grating grate - gratis grati - gratitude gratitud - gratulate gratul - grav grav - grave grave - gravediggers gravedigg - gravel gravel - graveless graveless - gravell gravel - gravely grave - graven graven - graveness grave - graver graver - graves grave - gravest gravest - gravestone graveston - gravities graviti - gravity graviti - gravy gravi - gray grai - graymalkin graymalkin - graz graz - graze graze - grazed graze - grazing graze - grease greas - greases greas - greasily greasili - greasy greasi - great great - greater greater - greatest greatest - greatly greatli - greatness great - grecian grecian - grecians grecian - gree gree - greece greec - greed greed - greedily greedili - greediness greedi - greedy greedi - greeing gree - greek greek - greekish greekish - greeks greek - green green - greener greener - greenly greenli - greens green - greensleeves greensleev - greenwich greenwich - greenwood greenwood - greet greet - greeted greet - greeting greet - greetings greet - greets greet - greg greg - gregory gregori - gremio gremio - grew grew - grey grei - greybeard greybeard - greybeards greybeard - greyhound greyhound - greyhounds greyhound - grief grief - griefs grief - griev griev - grievance grievanc - grievances grievanc - grieve griev - grieved griev - grieves griev - grievest grievest - grieving griev - grievingly grievingli - grievous grievou - grievously grievous - griffin griffin - griffith griffith - grim grim - grime grime - grimly grimli - grin grin - grind grind - grinding grind - grindstone grindston - grinning grin - grip grip - gripe gripe - gripes gripe - griping gripe - grise grise - grisly grisli - grissel grissel - grize grize - grizzle grizzl - grizzled grizzl - groan groan - groaning groan - groans groan - groat groat - groats groat - groin groin - groom groom - grooms groom - grop grop - groping grope - gros gro - gross gross - grosser grosser - grossly grossli - grossness gross - ground ground - grounded ground - groundlings groundl - grounds ground - grove grove - grovel grovel - grovelling grovel - groves grove - grow grow - groweth groweth - growing grow - grown grown - grows grow - growth growth - grub grub - grubb grubb - grubs grub - grudge grudg - grudged grudg - grudges grudg - grudging grudg - gruel gruel - grumble grumbl - grumblest grumblest - grumbling grumbl - grumblings grumbl - grumio grumio - grund grund - grunt grunt - gualtier gualtier - guard guard - guardage guardag - guardant guardant - guarded guard - guardian guardian - guardians guardian - guards guard - guardsman guardsman - gud gud - gudgeon gudgeon - guerdon guerdon - guerra guerra - guess guess - guesses guess - guessingly guessingli - guest guest - guests guest - guiana guiana - guichard guichard - guide guid - guided guid - guider guider - guiderius guideriu - guides guid - guiding guid - guidon guidon - guienne guienn - guil guil - guildenstern guildenstern - guilders guilder - guildford guildford - guildhall guildhal - guile guil - guiled guil - guileful guil - guilfords guilford - guilt guilt - guiltian guiltian - guiltier guiltier - guiltily guiltili - guiltiness guilti - guiltless guiltless - guilts guilt - guilty guilti - guinea guinea - guinever guinev - guise guis - gul gul - gules gule - gulf gulf - gulfs gulf - gull gull - gulls gull - gum gum - gumm gumm - gums gum - gun gun - gunner gunner - gunpowder gunpowd - guns gun - gurnet gurnet - gurney gurnei - gust gust - gusts gust - gusty gusti - guts gut - gutter gutter - guy gui - guynes guyn - guysors guysor - gypsy gypsi - gyve gyve - gyved gyve - gyves gyve - h h - ha ha - haberdasher haberdash - habiliment habili - habiliments habili - habit habit - habitation habit - habited habit - habits habit - habitude habitud - hack hack - hacket hacket - hackney hacknei - hacks hack - had had - hadst hadst - haec haec - haeres haer - hag hag - hagar hagar - haggard haggard - haggards haggard - haggish haggish - haggled haggl - hags hag - hail hail - hailed hail - hailstone hailston - hailstones hailston - hair hair - hairless hairless - hairs hair - hairy hairi - hal hal - halberd halberd - halberds halberd - halcyon halcyon - hale hale - haled hale - hales hale - half half - halfcan halfcan - halfpence halfpenc - halfpenny halfpenni - halfpennyworth halfpennyworth - halfway halfwai - halidom halidom - hall hall - halloa halloa - halloing hallo - hallond hallond - halloo halloo - hallooing halloo - hallow hallow - hallowed hallow - hallowmas hallowma - hallown hallown - hals hal - halt halt - halter halter - halters halter - halting halt - halts halt - halves halv - ham ham - hames hame - hamlet hamlet - hammer hammer - hammered hammer - hammering hammer - hammers hammer - hamper hamper - hampton hampton - hams ham - hamstring hamstr - hand hand - handed hand - handful hand - handicraft handicraft - handicraftsmen handicraftsmen - handing hand - handiwork handiwork - handkercher handkerch - handkerchers handkerch - handkerchief handkerchief - handle handl - handled handl - handles handl - handless handless - handlest handlest - handling handl - handmaid handmaid - handmaids handmaid - hands hand - handsaw handsaw - handsome handsom - handsomely handsom - handsomeness handsom - handwriting handwrit - handy handi - hang hang - hanged hang - hangers hanger - hangeth hangeth - hanging hang - hangings hang - hangman hangman - hangmen hangmen - hangs hang - hannibal hannib - hap hap - hapless hapless - haply hapli - happ happ - happen happen - happened happen - happier happier - happies happi - happiest happiest - happily happili - happiness happi - happy happi - haps hap - harbinger harbing - harbingers harbing - harbor harbor - harbour harbour - harbourage harbourag - harbouring harbour - harbours harbour - harcourt harcourt - hard hard - harder harder - hardest hardest - hardiest hardiest - hardiment hardiment - hardiness hardi - hardly hardli - hardness hard - hardocks hardock - hardy hardi - hare hare - harelip harelip - hares hare - harfleur harfleur - hark hark - harlot harlot - harlotry harlotri - harlots harlot - harm harm - harmed harm - harmful harm - harming harm - harmless harmless - harmonious harmoni - harmony harmoni - harms harm - harness har - harp harp - harper harper - harpier harpier - harping harp - harpy harpi - harried harri - harrow harrow - harrows harrow - harry harri - harsh harsh - harshly harshli - harshness harsh - hart hart - harts hart - harum harum - harvest harvest - has ha - hast hast - haste hast - hasted hast - hasten hasten - hastes hast - hastily hastili - hasting hast - hastings hast - hasty hasti - hat hat - hatch hatch - hatches hatch - hatchet hatchet - hatching hatch - hatchment hatchment - hate hate - hated hate - hateful hate - hater hater - haters hater - hates hate - hateth hateth - hatfield hatfield - hath hath - hating hate - hatred hatr - hats hat - haud haud - hauf hauf - haught haught - haughtiness haughti - haughty haughti - haunch haunch - haunches haunch - haunt haunt - haunted haunt - haunting haunt - haunts haunt - hautboy hautboi - hautboys hautboi - have have - haven haven - havens haven - haver haver - having have - havings have - havior havior - haviour haviour - havoc havoc - hawk hawk - hawking hawk - hawks hawk - hawthorn hawthorn - hawthorns hawthorn - hay hai - hazard hazard - hazarded hazard - hazards hazard - hazel hazel - hazelnut hazelnut - he he - head head - headborough headborough - headed head - headier headier - heading head - headland headland - headless headless - headlong headlong - heads head - headsman headsman - headstrong headstrong - heady headi - heal heal - healed heal - healing heal - heals heal - health health - healthful health - healths health - healthsome healthsom - healthy healthi - heap heap - heaping heap - heaps heap - hear hear - heard heard - hearer hearer - hearers hearer - hearest hearest - heareth heareth - hearing hear - hearings hear - heark heark - hearken hearken - hearkens hearken - hears hear - hearsay hearsai - hearse hears - hearsed hears - hearst hearst - heart heart - heartache heartach - heartbreak heartbreak - heartbreaking heartbreak - hearted heart - hearten hearten - hearth hearth - hearths hearth - heartily heartili - heartiness hearti - heartless heartless - heartlings heartl - heartly heartli - hearts heart - heartsick heartsick - heartstrings heartstr - hearty hearti - heat heat - heated heat - heath heath - heathen heathen - heathenish heathenish - heating heat - heats heat - heauties heauti - heav heav - heave heav - heaved heav - heaven heaven - heavenly heavenli - heavens heaven - heaves heav - heavier heavier - heaviest heaviest - heavily heavili - heaviness heavi - heaving heav - heavings heav - heavy heavi - hebona hebona - hebrew hebrew - hecate hecat - hectic hectic - hector hector - hectors hector - hecuba hecuba - hedg hedg - hedge hedg - hedgehog hedgehog - hedgehogs hedgehog - hedges hedg - heed heed - heeded heed - heedful heed - heedfull heedful - heedfully heedfulli - heedless heedless - heel heel - heels heel - hefted heft - hefts heft - heifer heifer - heifers heifer - heigh heigh - height height - heighten heighten - heinous heinou - heinously heinous - heir heir - heiress heiress - heirless heirless - heirs heir - held held - helen helen - helena helena - helenus helenu - helias helia - helicons helicon - hell hell - hellespont hellespont - hellfire hellfir - hellish hellish - helm helm - helmed helm - helmet helmet - helmets helmet - helms helm - help help - helper helper - helpers helper - helpful help - helping help - helpless helpless - helps help - helter helter - hem hem - heme heme - hemlock hemlock - hemm hemm - hemp hemp - hempen hempen - hems hem - hen hen - hence henc - henceforth henceforth - henceforward henceforward - henchman henchman - henri henri - henricus henricu - henry henri - hens hen - hent hent - henton henton - her her - herald herald - heraldry heraldri - heralds herald - herb herb - herbert herbert - herblets herblet - herbs herb - herculean herculean - hercules hercul - herd herd - herds herd - herdsman herdsman - herdsmen herdsmen - here here - hereabout hereabout - hereabouts hereabout - hereafter hereaft - hereby herebi - hereditary hereditari - hereford hereford - herefordshire herefordshir - herein herein - hereof hereof - heresies heresi - heresy heresi - heretic heret - heretics heret - hereto hereto - hereupon hereupon - heritage heritag - heritier heriti - hermes herm - hermia hermia - hermione hermion - hermit hermit - hermitage hermitag - hermits hermit - herne hern - hero hero - herod herod - herods herod - heroes hero - heroic heroic - heroical heroic - herring her - herrings her - hers her - herself herself - hesperides hesperid - hesperus hesperu - hest hest - hests hest - heure heur - heureux heureux - hew hew - hewgh hewgh - hewing hew - hewn hewn - hews hew - hey hei - heyday heydai - hibocrates hibocr - hic hic - hiccups hiccup - hick hick - hid hid - hidden hidden - hide hide - hideous hideou - hideously hideous - hideousness hideous - hides hide - hidest hidest - hiding hide - hie hie - hied hi - hiems hiem - hies hi - hig hig - high high - higher higher - highest highest - highly highli - highmost highmost - highness high - hight hight - highway highwai - highways highwai - hilding hild - hildings hild - hill hill - hillo hillo - hilloa hilloa - hills hill - hilt hilt - hilts hilt - hily hili - him him - himself himself - hinc hinc - hinckley hincklei - hind hind - hinder hinder - hindered hinder - hinders hinder - hindmost hindmost - hinds hind - hing hing - hinge hing - hinges hing - hint hint - hip hip - hipp hipp - hipparchus hipparchu - hippolyta hippolyta - hips hip - hir hir - hire hire - hired hire - hiren hiren - hirtius hirtiu - his hi - hisperia hisperia - hiss hiss - hisses hiss - hissing hiss - hist hist - historical histor - history histori - hit hit - hither hither - hitherto hitherto - hitherward hitherward - hitherwards hitherward - hits hit - hitting hit - hive hive - hives hive - hizzing hizz - ho ho - hoa hoa - hoar hoar - hoard hoard - hoarded hoard - hoarding hoard - hoars hoar - hoarse hoars - hoary hoari - hob hob - hobbididence hobbidid - hobby hobbi - hobbyhorse hobbyhors - hobgoblin hobgoblin - hobnails hobnail - hoc hoc - hod hod - hodge hodg - hog hog - hogs hog - hogshead hogshead - hogsheads hogshead - hois hoi - hoise hois - hoist hoist - hoisted hoist - hoists hoist - holborn holborn - hold hold - holden holden - holder holder - holdeth holdeth - holdfast holdfast - holding hold - holds hold - hole hole - holes hole - holidam holidam - holidame holidam - holiday holidai - holidays holidai - holier holier - holiest holiest - holily holili - holiness holi - holla holla - holland holland - hollander holland - hollanders holland - holloa holloa - holloaing holloa - hollow hollow - hollowly hollowli - hollowness hollow - holly holli - holmedon holmedon - holofernes holofern - holp holp - holy holi - homage homag - homager homag - home home - homely home - homes home - homespuns homespun - homeward homeward - homewards homeward - homicide homicid - homicides homicid - homily homili - hominem hominem - hommes homm - homo homo - honest honest - honester honest - honestest honestest - honestly honestli - honesty honesti - honey honei - honeycomb honeycomb - honeying honei - honeyless honeyless - honeysuckle honeysuckl - honeysuckles honeysuckl - honi honi - honneur honneur - honor honor - honorable honor - honorably honor - honorato honorato - honors honor - honour honour - honourable honour - honourably honour - honoured honour - honourest honourest - honourible honour - honouring honour - honours honour - hoo hoo - hood hood - hooded hood - hoodman hoodman - hoods hood - hoodwink hoodwink - hoof hoof - hoofs hoof - hook hook - hooking hook - hooks hook - hoop hoop - hoops hoop - hoot hoot - hooted hoot - hooting hoot - hoots hoot - hop hop - hope hope - hopeful hope - hopeless hopeless - hopes hope - hopest hopest - hoping hope - hopkins hopkin - hoppedance hopped - hor hor - horace horac - horatio horatio - horizon horizon - horn horn - hornbook hornbook - horned horn - horner horner - horning horn - hornpipes hornpip - horns horn - horologe horolog - horrible horribl - horribly horribl - horrid horrid - horrider horrid - horridly horridli - horror horror - horrors horror - hors hor - horse hors - horseback horseback - horsed hors - horsehairs horsehair - horseman horseman - horsemanship horsemanship - horsemen horsemen - horses hors - horseway horsewai - horsing hors - hortensio hortensio - hortensius hortensiu - horum horum - hose hose - hospitable hospit - hospital hospit - hospitality hospit - host host - hostage hostag - hostages hostag - hostess hostess - hostile hostil - hostility hostil - hostilius hostiliu - hosts host - hot hot - hotly hotli - hotspur hotspur - hotter hotter - hottest hottest - hound hound - hounds hound - hour hour - hourly hourli - hours hour - hous hou - house hous - household household - householder household - householders household - households household - housekeeper housekeep - housekeepers housekeep - housekeeping housekeep - houseless houseless - houses hous - housewife housewif - housewifery housewiferi - housewives housew - hovel hovel - hover hover - hovered hover - hovering hover - hovers hover - how how - howbeit howbeit - howe how - howeer howeer - however howev - howl howl - howled howl - howlet howlet - howling howl - howls howl - howsoe howso - howsoever howsoev - howsome howsom - hoxes hox - hoy hoi - hoyday hoydai - hubert hubert - huddled huddl - huddling huddl - hue hue - hued hu - hues hue - hug hug - huge huge - hugely huge - hugeness huge - hugg hugg - hugger hugger - hugh hugh - hugs hug - hujus huju - hulk hulk - hulks hulk - hull hull - hulling hull - hullo hullo - hum hum - human human - humane human - humanely human - humanity human - humble humbl - humbled humbl - humbleness humbl - humbler humbler - humbles humbl - humblest humblest - humbling humbl - humbly humbl - hume hume - humh humh - humidity humid - humility humil - humming hum - humor humor - humorous humor - humors humor - humour humour - humourists humourist - humours humour - humphrey humphrei - humphry humphri - hums hum - hundred hundr - hundreds hundr - hundredth hundredth - hung hung - hungarian hungarian - hungary hungari - hunger hunger - hungerford hungerford - hungerly hungerli - hungry hungri - hunt hunt - hunted hunt - hunter hunter - hunters hunter - hunteth hunteth - hunting hunt - huntington huntington - huntress huntress - hunts hunt - huntsman huntsman - huntsmen huntsmen - hurdle hurdl - hurl hurl - hurling hurl - hurls hurl - hurly hurli - hurlyburly hurlyburli - hurricano hurricano - hurricanoes hurricano - hurried hurri - hurries hurri - hurry hurri - hurt hurt - hurting hurt - hurtled hurtl - hurtless hurtless - hurtling hurtl - hurts hurt - husband husband - husbanded husband - husbandless husbandless - husbandry husbandri - husbands husband - hush hush - hushes hush - husht husht - husks husk - huswife huswif - huswifes huswif - hutch hutch - hybla hybla - hydra hydra - hyen hyen - hymen hymen - hymenaeus hymenaeu - hymn hymn - hymns hymn - hyperboles hyperbol - hyperbolical hyperbol - hyperion hyperion - hypocrisy hypocrisi - hypocrite hypocrit - hypocrites hypocrit - hyrcan hyrcan - hyrcania hyrcania - hyrcanian hyrcanian - hyssop hyssop - hysterica hysterica - i i - iachimo iachimo - iaculis iaculi - iago iago - iament iament - ibat ibat - icarus icaru - ice ic - iceland iceland - ici ici - icicle icicl - icicles icicl - icy ici - idea idea - ideas idea - idem idem - iden iden - ides id - idiot idiot - idiots idiot - idle idl - idleness idl - idles idl - idly idli - idol idol - idolatrous idolatr - idolatry idolatri - ield ield - if if - ifs if - ignis igni - ignoble ignobl - ignobly ignobl - ignominious ignomini - ignominy ignomini - ignomy ignomi - ignorance ignor - ignorant ignor - ii ii - iii iii - iiii iiii - il il - ilbow ilbow - ild ild - ilion ilion - ilium ilium - ill ill - illegitimate illegitim - illiterate illiter - illness ill - illo illo - ills ill - illume illum - illumin illumin - illuminate illumin - illumineth illumineth - illusion illus - illusions illus - illustrate illustr - illustrated illustr - illustrious illustri - illyria illyria - illyrian illyrian - ils il - im im - image imag - imagery imageri - images imag - imagin imagin - imaginary imaginari - imagination imagin - imaginations imagin - imagine imagin - imagining imagin - imaginings imagin - imbar imbar - imbecility imbecil - imbrue imbru - imitari imitari - imitate imit - imitated imit - imitation imit - imitations imit - immaculate immacul - immanity imman - immask immask - immaterial immateri - immediacy immediaci - immediate immedi - immediately immedi - imminence immin - imminent immin - immoderate immoder - immoderately immoder - immodest immodest - immoment immoment - immortal immort - immortaliz immortaliz - immortally immort - immur immur - immured immur - immures immur - imogen imogen - imp imp - impaint impaint - impair impair - impairing impair - impale impal - impaled impal - impanelled impanel - impart impart - imparted impart - impartial imparti - impartment impart - imparts impart - impasted impast - impatience impati - impatient impati - impatiently impati - impawn impawn - impeach impeach - impeached impeach - impeachment impeach - impeachments impeach - impedes imped - impediment impedi - impediments impedi - impenetrable impenetr - imperator imper - imperceiverant imperceiver - imperfect imperfect - imperfection imperfect - imperfections imperfect - imperfectly imperfectli - imperial imperi - imperious imperi - imperiously imperi - impertinency impertin - impertinent impertin - impeticos impetico - impetuosity impetuos - impetuous impetu - impieties impieti - impiety impieti - impious impiou - implacable implac - implements implement - implies impli - implor implor - implorators implor - implore implor - implored implor - imploring implor - impon impon - import import - importance import - importancy import - important import - importantly importantli - imported import - importeth importeth - importing import - importless importless - imports import - importun importun - importunacy importunaci - importunate importun - importune importun - importunes importun - importunity importun - impos impo - impose impos - imposed impos - imposition imposit - impositions imposit - impossibilities imposs - impossibility imposs - impossible imposs - imposthume imposthum - impostor impostor - impostors impostor - impotence impot - impotent impot - impounded impound - impregnable impregn - imprese impres - impress impress - impressed impress - impressest impressest - impression impress - impressure impressur - imprimendum imprimendum - imprimis imprimi - imprint imprint - imprinted imprint - imprison imprison - imprisoned imprison - imprisoning imprison - imprisonment imprison - improbable improb - improper improp - improve improv - improvident improvid - impudence impud - impudency impud - impudent impud - impudently impud - impudique impudiqu - impugn impugn - impugns impugn - impure impur - imputation imput - impute imput - in in - inaccessible inaccess - inaidable inaid - inaudible inaud - inauspicious inauspici - incaged incag - incantations incant - incapable incap - incardinate incardin - incarnadine incarnadin - incarnate incarn - incarnation incarn - incens incen - incense incens - incensed incens - incensement incens - incenses incens - incensing incens - incertain incertain - incertainties incertainti - incertainty incertainti - incessant incess - incessantly incessantli - incest incest - incestuous incestu - inch inch - incharitable incharit - inches inch - incidency incid - incident incid - incision incis - incite incit - incites incit - incivil incivil - incivility incivil - inclin inclin - inclinable inclin - inclination inclin - incline inclin - inclined inclin - inclines inclin - inclining inclin - inclips inclip - include includ - included includ - includes includ - inclusive inclus - incomparable incompar - incomprehensible incomprehens - inconsiderate inconsider - inconstancy inconst - inconstant inconst - incontinency incontin - incontinent incontin - incontinently incontin - inconvenience inconveni - inconveniences inconveni - inconvenient inconveni - incony inconi - incorporate incorpor - incorps incorp - incorrect incorrect - increas increa - increase increas - increases increas - increaseth increaseth - increasing increas - incredible incred - incredulous incredul - incur incur - incurable incur - incurr incurr - incurred incur - incursions incurs - ind ind - inde ind - indebted indebt - indeed inde - indent indent - indented indent - indenture indentur - indentures indentur - index index - indexes index - india india - indian indian - indict indict - indicted indict - indictment indict - indies indi - indifferency indiffer - indifferent indiffer - indifferently indiffer - indigent indig - indigest indigest - indigested indigest - indign indign - indignation indign - indignations indign - indigne indign - indignities indign - indignity indign - indirect indirect - indirection indirect - indirections indirect - indirectly indirectli - indiscreet indiscreet - indiscretion indiscret - indispos indispo - indisposition indisposit - indissoluble indissolubl - indistinct indistinct - indistinguish indistinguish - indistinguishable indistinguish - indited indit - individable individ - indrench indrench - indu indu - indubitate indubit - induc induc - induce induc - induced induc - inducement induc - induction induct - inductions induct - indue indu - indued indu - indues indu - indulgence indulg - indulgences indulg - indulgent indulg - indurance indur - industrious industri - industriously industri - industry industri - inequality inequ - inestimable inestim - inevitable inevit - inexecrable inexecr - inexorable inexor - inexplicable inexplic - infallible infal - infallibly infal - infamonize infamon - infamous infam - infamy infami - infancy infanc - infant infant - infants infant - infect infect - infected infect - infecting infect - infection infect - infections infect - infectious infecti - infectiously infecti - infects infect - infer infer - inference infer - inferior inferior - inferiors inferior - infernal infern - inferr inferr - inferreth inferreth - inferring infer - infest infest - infidel infidel - infidels infidel - infinite infinit - infinitely infinit - infinitive infinit - infirm infirm - infirmities infirm - infirmity infirm - infixed infix - infixing infix - inflam inflam - inflame inflam - inflaming inflam - inflammation inflamm - inflict inflict - infliction inflict - influence influenc - influences influenc - infold infold - inform inform - informal inform - information inform - informations inform - informed inform - informer inform - informs inform - infortunate infortun - infring infr - infringe infring - infringed infring - infus infu - infuse infus - infused infus - infusing infus - infusion infus - ingener ingen - ingenious ingeni - ingeniously ingeni - inglorious inglori - ingots ingot - ingraffed ingraf - ingraft ingraft - ingrate ingrat - ingrated ingrat - ingrateful ingrat - ingratitude ingratitud - ingratitudes ingratitud - ingredient ingredi - ingredients ingredi - ingross ingross - inhabit inhabit - inhabitable inhabit - inhabitants inhabit - inhabited inhabit - inhabits inhabit - inhearse inhears - inhearsed inhears - inherent inher - inherit inherit - inheritance inherit - inherited inherit - inheriting inherit - inheritor inheritor - inheritors inheritor - inheritrix inheritrix - inherits inherit - inhibited inhibit - inhibition inhibit - inhoop inhoop - inhuman inhuman - iniquities iniqu - iniquity iniqu - initiate initi - injointed injoint - injunction injunct - injunctions injunct - injur injur - injure injur - injurer injur - injuries injuri - injurious injuri - injury injuri - injustice injustic - ink ink - inkhorn inkhorn - inkle inkl - inkles inkl - inkling inkl - inky inki - inlaid inlaid - inland inland - inlay inlai - inly inli - inmost inmost - inn inn - inner inner - innkeeper innkeep - innocence innoc - innocency innoc - innocent innoc - innocents innoc - innovation innov - innovator innov - inns inn - innumerable innumer - inoculate inocul - inordinate inordin - inprimis inprimi - inquir inquir - inquire inquir - inquiry inquiri - inquisition inquisit - inquisitive inquisit - inroads inroad - insane insan - insanie insani - insatiate insati - insconce insconc - inscrib inscrib - inscription inscript - inscriptions inscript - inscroll inscrol - inscrutable inscrut - insculp insculp - insculpture insculptur - insensible insens - inseparable insepar - inseparate insepar - insert insert - inserted insert - inset inset - inshell inshel - inshipp inshipp - inside insid - insinewed insinew - insinuate insinu - insinuateth insinuateth - insinuating insinu - insinuation insinu - insisted insist - insisting insist - insisture insistur - insociable insoci - insolence insol - insolent insol - insomuch insomuch - inspir inspir - inspiration inspir - inspirations inspir - inspire inspir - inspired inspir - install instal - installed instal - instalment instal - instance instanc - instances instanc - instant instant - instantly instantli - instate instat - instead instead - insteeped insteep - instigate instig - instigated instig - instigation instig - instigations instig - instigator instig - instinct instinct - instinctively instinct - institute institut - institutions institut - instruct instruct - instructed instruct - instruction instruct - instructions instruct - instructs instruct - instrument instrument - instrumental instrument - instruments instrument - insubstantial insubstanti - insufficience insuffici - insufficiency insuffici - insult insult - insulted insult - insulting insult - insultment insult - insults insult - insupportable insupport - insuppressive insuppress - insurrection insurrect - insurrections insurrect - int int - integer integ - integritas integrita - integrity integr - intellect intellect - intellects intellect - intellectual intellectu - intelligence intellig - intelligencer intelligenc - intelligencing intelligenc - intelligent intellig - intelligis intelligi - intelligo intelligo - intemperance intemper - intemperate intemper - intend intend - intended intend - intendeth intendeth - intending intend - intendment intend - intends intend - intenible inten - intent intent - intention intent - intentively intent - intents intent - inter inter - intercept intercept - intercepted intercept - intercepter intercept - interception intercept - intercepts intercept - intercession intercess - intercessors intercessor - interchained interchain - interchang interchang - interchange interchang - interchangeably interchang - interchangement interchang - interchanging interchang - interdiction interdict - interest interest - interim interim - interims interim - interior interior - interjections interject - interjoin interjoin - interlude interlud - intermingle intermingl - intermission intermiss - intermissive intermiss - intermit intermit - intermix intermix - intermixed intermix - interpose interpos - interposer interpos - interposes interpos - interpret interpret - interpretation interpret - interpreted interpret - interpreter interpret - interpreters interpret - interprets interpret - interr interr - interred inter - interrogatories interrogatori - interrupt interrupt - interrupted interrupt - interrupter interrupt - interruptest interruptest - interruption interrupt - interrupts interrupt - intertissued intertissu - intervallums intervallum - interview interview - intestate intest - intestine intestin - intil intil - intimate intim - intimation intim - intitled intitl - intituled intitul - into into - intolerable intoler - intoxicates intox - intreasured intreasur - intreat intreat - intrench intrench - intrenchant intrench - intricate intric - intrinse intrins - intrinsicate intrins - intrude intrud - intruder intrud - intruding intrud - intrusion intrus - inundation inund - inure inur - inurn inurn - invade invad - invades invad - invasion invas - invasive invas - invectively invect - invectives invect - inveigled inveigl - invent invent - invented invent - invention invent - inventions invent - inventor inventor - inventorially inventori - inventoried inventori - inventors inventor - inventory inventori - inverness inver - invert invert - invest invest - invested invest - investing invest - investments invest - inveterate inveter - invincible invinc - inviolable inviol - invised invis - invisible invis - invitation invit - invite invit - invited invit - invites invit - inviting invit - invitis inviti - invocate invoc - invocation invoc - invoke invok - invoked invok - invulnerable invulner - inward inward - inwardly inwardli - inwardness inward - inwards inward - ionia ionia - ionian ionian - ipse ips - ipswich ipswich - ira ira - irae ira - iras ira - ire ir - ireful ir - ireland ireland - iris iri - irish irish - irishman irishman - irishmen irishmen - irks irk - irksome irksom - iron iron - irons iron - irreconcil irreconcil - irrecoverable irrecover - irregular irregular - irregulous irregul - irreligious irreligi - irremovable irremov - irreparable irrepar - irresolute irresolut - irrevocable irrevoc - is is - isabel isabel - isabella isabella - isbel isbel - isbels isbel - iscariot iscariot - ise is - ish ish - isidore isidor - isis isi - island island - islander island - islanders island - islands island - isle isl - isles isl - israel israel - issu issu - issue issu - issued issu - issueless issueless - issues issu - issuing issu - ist ist - ista ista - it it - italian italian - italy itali - itch itch - itches itch - itching itch - item item - items item - iteration iter - ithaca ithaca - its it - itself itself - itshall itshal - iv iv - ivory ivori - ivy ivi - iwis iwi - ix ix - j j - jacet jacet - jack jack - jackanapes jackanap - jacks jack - jacksauce jacksauc - jackslave jackslav - jacob jacob - jade jade - jaded jade - jades jade - jail jail - jakes jake - jamany jamani - james jame - jamy jami - jane jane - jangled jangl - jangling jangl - january januari - janus janu - japhet japhet - jaquenetta jaquenetta - jaques jaqu - jar jar - jarring jar - jars jar - jarteer jarteer - jasons jason - jaunce jaunc - jauncing jaunc - jaundice jaundic - jaundies jaundi - jaw jaw - jawbone jawbon - jaws jaw - jay jai - jays jai - jc jc - je je - jealous jealou - jealousies jealousi - jealousy jealousi - jeer jeer - jeering jeer - jelly jelli - jenny jenni - jeopardy jeopardi - jephtha jephtha - jephthah jephthah - jerkin jerkin - jerkins jerkin - jerks jerk - jeronimy jeronimi - jerusalem jerusalem - jeshu jeshu - jesses jess - jessica jessica - jest jest - jested jest - jester jester - jesters jester - jesting jest - jests jest - jesu jesu - jesus jesu - jet jet - jets jet - jew jew - jewel jewel - jeweller jewel - jewels jewel - jewess jewess - jewish jewish - jewry jewri - jews jew - jezebel jezebel - jig jig - jigging jig - jill jill - jills jill - jingling jingl - joan joan - job job - jockey jockei - jocund jocund - jog jog - jogging jog - john john - johns john - join join - joinder joinder - joined join - joiner joiner - joineth joineth - joins join - joint joint - jointed joint - jointing joint - jointly jointli - jointress jointress - joints joint - jointure jointur - jollity jolliti - jolly jolli - jolt jolt - joltheads jolthead - jordan jordan - joseph joseph - joshua joshua - jot jot - jour jour - jourdain jourdain - journal journal - journey journei - journeying journei - journeyman journeyman - journeymen journeymen - journeys journei - jove jove - jovem jovem - jovial jovial - jowl jowl - jowls jowl - joy joi - joyed joi - joyful joy - joyfully joyfulli - joyless joyless - joyous joyou - joys joi - juan juan - jud jud - judas juda - judases judas - jude jude - judg judg - judge judg - judged judg - judgement judgement - judges judg - judgest judgest - judging judg - judgment judgment - judgments judgment - judicious judici - jug jug - juggle juggl - juggled juggl - juggler juggler - jugglers juggler - juggling juggl - jugs jug - juice juic - juiced juic - jul jul - jule jule - julia julia - juliet juliet - julietta julietta - julio julio - julius juliu - july juli - jump jump - jumpeth jumpeth - jumping jump - jumps jump - june june - junes june - junior junior - junius juniu - junkets junket - juno juno - jupiter jupit - jure jure - jurement jurement - jurisdiction jurisdict - juror juror - jurors juror - jury juri - jurymen jurymen - just just - justeius justeiu - justest justest - justice justic - justicer justic - justicers justic - justices justic - justification justif - justified justifi - justify justifi - justle justl - justled justl - justles justl - justling justl - justly justli - justness just - justs just - jutting jut - jutty jutti - juvenal juven - kam kam - kate kate - kated kate - kates kate - katharine katharin - katherina katherina - katherine katherin - kecksies kecksi - keech keech - keel keel - keels keel - keen keen - keenness keen - keep keep - keepdown keepdown - keeper keeper - keepers keeper - keepest keepest - keeping keep - keeps keep - keiser keiser - ken ken - kendal kendal - kennel kennel - kent kent - kentish kentish - kentishman kentishman - kentishmen kentishmen - kept kept - kerchief kerchief - kerely kere - kern kern - kernal kernal - kernel kernel - kernels kernel - kerns kern - kersey kersei - kettle kettl - kettledrum kettledrum - kettledrums kettledrum - key kei - keys kei - kibe kibe - kibes kibe - kick kick - kicked kick - kickshaws kickshaw - kickshawses kickshaws - kicky kicki - kid kid - kidney kidnei - kikely kike - kildare kildar - kill kill - killed kill - killer killer - killeth killeth - killing kill - killingworth killingworth - kills kill - kiln kiln - kimbolton kimbolton - kin kin - kind kind - kinder kinder - kindest kindest - kindle kindl - kindled kindl - kindless kindless - kindlier kindlier - kindling kindl - kindly kindli - kindness kind - kindnesses kind - kindred kindr - kindreds kindr - kinds kind - kine kine - king king - kingdom kingdom - kingdoms kingdom - kingly kingli - kings king - kinred kinr - kins kin - kinsman kinsman - kinsmen kinsmen - kinswoman kinswoman - kirtle kirtl - kirtles kirtl - kiss kiss - kissed kiss - kisses kiss - kissing kiss - kitchen kitchen - kitchens kitchen - kite kite - kites kite - kitten kitten - kj kj - kl kl - klll klll - knack knack - knacks knack - knapp knapp - knav knav - knave knave - knaveries knaveri - knavery knaveri - knaves knave - knavish knavish - knead knead - kneaded knead - kneading knead - knee knee - kneel kneel - kneeling kneel - kneels kneel - knees knee - knell knell - knew knew - knewest knewest - knife knife - knight knight - knighted knight - knighthood knighthood - knighthoods knighthood - knightly knightli - knights knight - knit knit - knits knit - knitters knitter - knitteth knitteth - knives knive - knobs knob - knock knock - knocking knock - knocks knock - knog knog - knoll knoll - knot knot - knots knot - knotted knot - knotty knotti - know know - knower knower - knowest knowest - knowing know - knowingly knowingli - knowings know - knowledge knowledg - known known - knows know - l l - la la - laban laban - label label - labell label - labienus labienu - labio labio - labor labor - laboring labor - labors labor - labour labour - laboured labour - labourer labour - labourers labour - labouring labour - labours labour - laboursome laboursom - labras labra - labyrinth labyrinth - lac lac - lace lace - laced lace - lacedaemon lacedaemon - laces lace - lacies laci - lack lack - lackbeard lackbeard - lacked lack - lackey lackei - lackeying lackei - lackeys lackei - lacking lack - lacks lack - lad lad - ladder ladder - ladders ladder - lade lade - laden laden - ladies ladi - lading lade - lads lad - lady ladi - ladybird ladybird - ladyship ladyship - ladyships ladyship - laer laer - laertes laert - lafeu lafeu - lag lag - lagging lag - laid laid - lain lain - laissez laissez - lake lake - lakes lake - lakin lakin - lam lam - lamb lamb - lambert lambert - lambkin lambkin - lambkins lambkin - lambs lamb - lame lame - lamely lame - lameness lame - lament lament - lamentable lament - lamentably lament - lamentation lament - lamentations lament - lamented lament - lamenting lament - lamentings lament - laments lament - lames lame - laming lame - lammas lamma - lammastide lammastid - lamound lamound - lamp lamp - lampass lampass - lamps lamp - lanc lanc - lancaster lancast - lance lanc - lances lanc - lanceth lanceth - lanch lanch - land land - landed land - landing land - landless landless - landlord landlord - landmen landmen - lands land - lane lane - lanes lane - langage langag - langley langlei - langton langton - language languag - languageless languageless - languages languag - langues langu - languish languish - languished languish - languishes languish - languishing languish - languishings languish - languishment languish - languor languor - lank lank - lantern lantern - lanterns lantern - lanthorn lanthorn - lap lap - lapis lapi - lapland lapland - lapp lapp - laps lap - lapse laps - lapsed laps - lapsing laps - lapwing lapw - laquais laquai - larded lard - larder larder - larding lard - lards lard - large larg - largely larg - largeness larg - larger larger - largess largess - largest largest - lark lark - larks lark - larron larron - lartius lartiu - larum larum - larums larum - las la - lascivious lascivi - lash lash - lass lass - lasses lass - last last - lasted last - lasting last - lastly lastli - lasts last - latch latch - latches latch - late late - lated late - lately late - later later - latest latest - lath lath - latin latin - latten latten - latter latter - lattice lattic - laud laud - laudable laudabl - laudis laudi - laugh laugh - laughable laughabl - laughed laugh - laugher laugher - laughest laughest - laughing laugh - laughs laugh - laughter laughter - launce launc - launcelot launcelot - launces launc - launch launch - laund laund - laundress laundress - laundry laundri - laur laur - laura laura - laurel laurel - laurels laurel - laurence laurenc - laus lau - lavache lavach - lave lave - lavee lave - lavender lavend - lavina lavina - lavinia lavinia - lavish lavish - lavishly lavishli - lavolt lavolt - lavoltas lavolta - law law - lawful law - lawfully lawfulli - lawless lawless - lawlessly lawlessli - lawn lawn - lawns lawn - lawrence lawrenc - laws law - lawyer lawyer - lawyers lawyer - lay lai - layer layer - layest layest - laying lai - lays lai - lazar lazar - lazars lazar - lazarus lazaru - lazy lazi - lc lc - ld ld - ldst ldst - le le - lead lead - leaden leaden - leader leader - leaders leader - leadest leadest - leading lead - leads lead - leaf leaf - leagu leagu - league leagu - leagued leagu - leaguer leaguer - leagues leagu - leah leah - leak leak - leaky leaki - lean lean - leander leander - leaner leaner - leaning lean - leanness lean - leans lean - leap leap - leaped leap - leaping leap - leaps leap - leapt leapt - lear lear - learn learn - learned learn - learnedly learnedli - learning learn - learnings learn - learns learn - learnt learnt - leas lea - lease leas - leases leas - leash leash - leasing leas - least least - leather leather - leathern leathern - leav leav - leave leav - leaven leaven - leavening leaven - leaver leaver - leaves leav - leaving leav - leavy leavi - lecher lecher - lecherous lecher - lechers lecher - lechery lecheri - lecon lecon - lecture lectur - lectures lectur - led led - leda leda - leech leech - leeches leech - leek leek - leeks leek - leer leer - leers leer - lees lee - leese lees - leet leet - leets leet - left left - leg leg - legacies legaci - legacy legaci - legate legat - legatine legatin - lege lege - legerity leger - leges lege - legg legg - legion legion - legions legion - legitimate legitim - legitimation legitim - legs leg - leicester leicest - leicestershire leicestershir - leiger leiger - leigers leiger - leisure leisur - leisurely leisur - leisures leisur - leman leman - lemon lemon - lena lena - lend lend - lender lender - lending lend - lendings lend - lends lend - length length - lengthen lengthen - lengthens lengthen - lengths length - lenity leniti - lennox lennox - lent lent - lenten lenten - lentus lentu - leo leo - leon leon - leonardo leonardo - leonati leonati - leonato leonato - leonatus leonatu - leontes leont - leopard leopard - leopards leopard - leper leper - leperous leper - lepidus lepidu - leprosy leprosi - lequel lequel - lers ler - les le - less less - lessen lessen - lessens lessen - lesser lesser - lesson lesson - lessoned lesson - lessons lesson - lest lest - lestrake lestrak - let let - lethargied lethargi - lethargies lethargi - lethargy lethargi - lethe leth - lets let - lett lett - letter letter - letters letter - letting let - lettuce lettuc - leur leur - leve leve - level level - levell level - levelled level - levels level - leven leven - levers lever - leviathan leviathan - leviathans leviathan - levied levi - levies levi - levity leviti - levy levi - levying levi - lewd lewd - lewdly lewdli - lewdness lewd - lewdsters lewdster - lewis lewi - liable liabl - liar liar - liars liar - libbard libbard - libelling libel - libels libel - liberal liber - liberality liber - liberte libert - liberties liberti - libertine libertin - libertines libertin - liberty liberti - library librari - libya libya - licence licenc - licens licen - license licens - licentious licenti - lichas licha - licio licio - lick lick - licked lick - licker licker - lictors lictor - lid lid - lids lid - lie lie - lied li - lief lief - liefest liefest - liege lieg - liegeman liegeman - liegemen liegemen - lien lien - lies li - liest liest - lieth lieth - lieu lieu - lieutenant lieuten - lieutenantry lieutenantri - lieutenants lieuten - lieve liev - life life - lifeblood lifeblood - lifeless lifeless - lifelings lifel - lift lift - lifted lift - lifter lifter - lifteth lifteth - lifting lift - lifts lift - lig lig - ligarius ligariu - liggens liggen - light light - lighted light - lighten lighten - lightens lighten - lighter lighter - lightest lightest - lightly lightli - lightness light - lightning lightn - lightnings lightn - lights light - lik lik - like like - liked like - likeliest likeliest - likelihood likelihood - likelihoods likelihood - likely like - likeness like - liker liker - likes like - likest likest - likewise likewis - liking like - likings like - lilies lili - lily lili - lim lim - limander limand - limb limb - limbeck limbeck - limbecks limbeck - limber limber - limbo limbo - limbs limb - lime lime - limed lime - limehouse limehous - limekilns limekiln - limit limit - limitation limit - limited limit - limits limit - limn limn - limp limp - limping limp - limps limp - lin lin - lincoln lincoln - lincolnshire lincolnshir - line line - lineal lineal - lineally lineal - lineament lineament - lineaments lineament - lined line - linen linen - linens linen - lines line - ling ling - lingare lingar - linger linger - lingered linger - lingers linger - linguist linguist - lining line - link link - links link - linsey linsei - linstock linstock - linta linta - lion lion - lionel lionel - lioness lioness - lions lion - lip lip - lipp lipp - lips lip - lipsbury lipsburi - liquid liquid - liquor liquor - liquorish liquorish - liquors liquor - lirra lirra - lisbon lisbon - lisp lisp - lisping lisp - list list - listen listen - listening listen - lists list - literatured literatur - lither lither - litter litter - little littl - littlest littlest - liv liv - live live - lived live - livelier liveli - livelihood livelihood - livelong livelong - lively live - liver liver - liveries liveri - livers liver - livery liveri - lives live - livest livest - liveth liveth - livia livia - living live - livings live - lizard lizard - lizards lizard - ll ll - lll lll - llous llou - lnd lnd - lo lo - loa loa - loach loach - load load - loaden loaden - loading load - loads load - loaf loaf - loam loam - loan loan - loath loath - loathe loath - loathed loath - loather loather - loathes loath - loathing loath - loathly loathli - loathness loath - loathsome loathsom - loathsomeness loathsom - loathsomest loathsomest - loaves loav - lob lob - lobbies lobbi - lobby lobbi - local local - lochaber lochab - lock lock - locked lock - locking lock - lockram lockram - locks lock - locusts locust - lode lode - lodg lodg - lodge lodg - lodged lodg - lodgers lodger - lodges lodg - lodging lodg - lodgings lodg - lodovico lodovico - lodowick lodowick - lofty lofti - log log - logger logger - loggerhead loggerhead - loggerheads loggerhead - loggets logget - logic logic - logs log - loins loin - loiter loiter - loiterer loiter - loiterers loiter - loitering loiter - lolling loll - lolls loll - lombardy lombardi - london london - londoners london - lone lone - loneliness loneli - lonely lone - long long - longaville longavil - longboat longboat - longed long - longer longer - longest longest - longeth longeth - longing long - longings long - longly longli - longs long - longtail longtail - loo loo - loof loof - look look - looked look - looker looker - lookers looker - lookest lookest - looking look - looks look - loon loon - loop loop - loos loo - loose loos - loosed loos - loosely loos - loosen loosen - loosing loos - lop lop - lopp lopp - loquitur loquitur - lord lord - lorded lord - lording lord - lordings lord - lordliness lordli - lordly lordli - lords lord - lordship lordship - lordships lordship - lorenzo lorenzo - lorn lorn - lorraine lorrain - lorship lorship - los lo - lose lose - loser loser - losers loser - loses lose - losest losest - loseth loseth - losing lose - loss loss - losses loss - lost lost - lot lot - lots lot - lott lott - lottery lotteri - loud loud - louder louder - loudly loudli - lour lour - loureth loureth - louring lour - louse lous - louses lous - lousy lousi - lout lout - louted lout - louts lout - louvre louvr - lov lov - love love - loved love - lovedst lovedst - lovel lovel - lovelier loveli - loveliness loveli - lovell lovel - lovely love - lover lover - lovered lover - lovers lover - loves love - lovest lovest - loveth loveth - loving love - lovingly lovingli - low low - lowe low - lower lower - lowest lowest - lowing low - lowliness lowli - lowly lowli - lown lown - lowness low - loyal loyal - loyally loyal - loyalties loyalti - loyalty loyalti - lozel lozel - lt lt - lubber lubber - lubberly lubberli - luc luc - luccicos luccico - luce luce - lucentio lucentio - luces luce - lucetta lucetta - luciana luciana - lucianus lucianu - lucifer lucif - lucifier lucifi - lucilius luciliu - lucina lucina - lucio lucio - lucius luciu - luck luck - luckier luckier - luckiest luckiest - luckily luckili - luckless luckless - lucky lucki - lucre lucr - lucrece lucrec - lucretia lucretia - lucullius luculliu - lucullus lucullu - lucy luci - lud lud - ludlow ludlow - lug lug - lugg lugg - luggage luggag - luke luke - lukewarm lukewarm - lull lull - lulla lulla - lullaby lullabi - lulls lull - lumbert lumbert - lump lump - lumpish lumpish - luna luna - lunacies lunaci - lunacy lunaci - lunatic lunat - lunatics lunat - lunes lune - lungs lung - lupercal luperc - lurch lurch - lure lure - lurk lurk - lurketh lurketh - lurking lurk - lurks lurk - luscious lusciou - lush lush - lust lust - lusted lust - luster luster - lustful lust - lustier lustier - lustiest lustiest - lustig lustig - lustihood lustihood - lustily lustili - lustre lustr - lustrous lustrou - lusts lust - lusty lusti - lute lute - lutes lute - lutestring lutestr - lutheran lutheran - luxurious luxuri - luxuriously luxuri - luxury luxuri - ly ly - lycaonia lycaonia - lycurguses lycurgus - lydia lydia - lye lye - lyen lyen - lying ly - lym lym - lymoges lymog - lynn lynn - lysander lysand - m m - ma ma - maan maan - mab mab - macbeth macbeth - maccabaeus maccabaeu - macdonwald macdonwald - macduff macduff - mace mace - macedon macedon - maces mace - machiavel machiavel - machination machin - machinations machin - machine machin - mack mack - macmorris macmorri - maculate macul - maculation macul - mad mad - madam madam - madame madam - madams madam - madcap madcap - madded mad - madding mad - made made - madeira madeira - madly madli - madman madman - madmen madmen - madness mad - madonna madonna - madrigals madrig - mads mad - maecenas maecena - maggot maggot - maggots maggot - magic magic - magical magic - magician magician - magistrate magistr - magistrates magistr - magnanimity magnanim - magnanimous magnanim - magni magni - magnifi magnifi - magnificence magnific - magnificent magnific - magnifico magnifico - magnificoes magnifico - magnus magnu - mahomet mahomet - mahu mahu - maid maid - maiden maiden - maidenhead maidenhead - maidenheads maidenhead - maidenhood maidenhood - maidenhoods maidenhood - maidenliest maidenliest - maidenly maidenli - maidens maiden - maidhood maidhood - maids maid - mail mail - mailed mail - mails mail - maim maim - maimed maim - maims maim - main main - maincourse maincours - maine main - mainly mainli - mainmast mainmast - mains main - maintain maintain - maintained maintain - maintains maintain - maintenance mainten - mais mai - maison maison - majestas majesta - majestee majeste - majestic majest - majestical majest - majestically majest - majesties majesti - majesty majesti - major major - majority major - mak mak - make make - makeless makeless - maker maker - makers maker - makes make - makest makest - maketh maketh - making make - makings make - mal mal - mala mala - maladies maladi - malady maladi - malapert malapert - malcolm malcolm - malcontent malcont - malcontents malcont - male male - maledictions maledict - malefactions malefact - malefactor malefactor - malefactors malefactor - males male - malevolence malevol - malevolent malevol - malhecho malhecho - malice malic - malicious malici - maliciously malici - malign malign - malignancy malign - malignant malign - malignantly malignantli - malkin malkin - mall mall - mallard mallard - mallet mallet - mallows mallow - malmsey malmsei - malt malt - maltworms maltworm - malvolio malvolio - mamillius mamilliu - mammering mammer - mammet mammet - mammets mammet - mammock mammock - man man - manacle manacl - manacles manacl - manage manag - managed manag - manager manag - managing manag - manakin manakin - manchus manchu - mandate mandat - mandragora mandragora - mandrake mandrak - mandrakes mandrak - mane mane - manent manent - manes mane - manet manet - manfully manfulli - mangle mangl - mangled mangl - mangles mangl - mangling mangl - mangy mangi - manhood manhood - manhoods manhood - manifest manifest - manifested manifest - manifests manifest - manifold manifold - manifoldly manifoldli - manka manka - mankind mankind - manlike manlik - manly manli - mann mann - manna manna - manner manner - mannerly mannerli - manners manner - manningtree manningtre - mannish mannish - manor manor - manors manor - mans man - mansion mansion - mansionry mansionri - mansions mansion - manslaughter manslaught - mantle mantl - mantled mantl - mantles mantl - mantua mantua - mantuan mantuan - manual manual - manure manur - manured manur - manus manu - many mani - map map - mapp mapp - maps map - mar mar - marble marbl - marbled marbl - marcade marcad - marcellus marcellu - march march - marches march - marcheth marcheth - marching march - marchioness marchio - marchpane marchpan - marcians marcian - marcius marciu - marcus marcu - mardian mardian - mare mare - mares mare - marg marg - margarelon margarelon - margaret margaret - marge marg - margent margent - margery margeri - maria maria - marian marian - mariana mariana - maries mari - marigold marigold - mariner marin - mariners marin - maritime maritim - marjoram marjoram - mark mark - marked mark - market market - marketable market - marketplace marketplac - markets market - marking mark - markman markman - marks mark - marl marl - marle marl - marmoset marmoset - marquess marquess - marquis marqui - marr marr - marriage marriag - marriages marriag - married marri - marries marri - marring mar - marrow marrow - marrowless marrowless - marrows marrow - marry marri - marrying marri - mars mar - marseilles marseil - marsh marsh - marshal marshal - marshalsea marshalsea - marshalship marshalship - mart mart - marted mart - martem martem - martext martext - martial martial - martin martin - martino martino - martius martiu - martlemas martlema - martlet martlet - marts mart - martyr martyr - martyrs martyr - marullus marullu - marv marv - marvel marvel - marvell marvel - marvellous marvel - marvellously marvel - marvels marvel - mary mari - mas ma - masculine masculin - masham masham - mask mask - masked mask - masker masker - maskers masker - masking mask - masks mask - mason mason - masonry masonri - masons mason - masque masqu - masquers masquer - masques masqu - masquing masqu - mass mass - massacre massacr - massacres massacr - masses mass - massy massi - mast mast - mastcr mastcr - master master - masterdom masterdom - masterest masterest - masterless masterless - masterly masterli - masterpiece masterpiec - masters master - mastership mastership - mastic mastic - mastiff mastiff - mastiffs mastiff - masts mast - match match - matches match - matcheth matcheth - matching match - matchless matchless - mate mate - mated mate - mater mater - material materi - mates mate - mathematics mathemat - matin matin - matron matron - matrons matron - matter matter - matters matter - matthew matthew - mattock mattock - mattress mattress - mature matur - maturity matur - maud maud - maudlin maudlin - maugre maugr - maul maul - maund maund - mauri mauri - mauritania mauritania - mauvais mauvai - maw maw - maws maw - maxim maxim - may mai - mayday maydai - mayest mayest - mayor mayor - maypole maypol - mayst mayst - maz maz - maze maze - mazed maze - mazes maze - mazzard mazzard - me me - meacock meacock - mead mead - meadow meadow - meadows meadow - meads mead - meagre meagr - meal meal - meals meal - mealy meali - mean mean - meanders meander - meaner meaner - meanest meanest - meaneth meaneth - meaning mean - meanings mean - meanly meanli - means mean - meant meant - meantime meantim - meanwhile meanwhil - measles measl - measur measur - measurable measur - measure measur - measured measur - measureless measureless - measures measur - measuring measur - meat meat - meats meat - mechanic mechan - mechanical mechan - mechanicals mechan - mechanics mechan - mechante mechant - med med - medal medal - meddle meddl - meddler meddler - meddling meddl - mede mede - medea medea - media media - mediation mediat - mediators mediat - medice medic - medicinal medicin - medicine medicin - medicines medicin - meditate medit - meditates medit - meditating medit - meditation medit - meditations medit - mediterranean mediterranean - mediterraneum mediterraneum - medlar medlar - medlars medlar - meed meed - meeds meed - meek meek - meekly meekli - meekness meek - meet meet - meeter meeter - meetest meetest - meeting meet - meetings meet - meetly meetli - meetness meet - meets meet - meg meg - mehercle mehercl - meilleur meilleur - meiny meini - meisen meisen - melancholies melancholi - melancholy melancholi - melford melford - mell mell - mellifluous melliflu - mellow mellow - mellowing mellow - melodious melodi - melody melodi - melt melt - melted melt - melteth melteth - melting melt - melts melt - melun melun - member member - members member - memento memento - memorable memor - memorandums memorandum - memorial memori - memorials memori - memories memori - memoriz memoriz - memorize memor - memory memori - memphis memphi - men men - menac menac - menace menac - menaces menac - menaphon menaphon - menas mena - mend mend - mended mend - mender mender - mending mend - mends mend - menecrates menecr - menelaus menelau - menenius meneniu - mental mental - menteith menteith - mention mention - mentis menti - menton menton - mephostophilus mephostophilu - mer mer - mercatante mercatant - mercatio mercatio - mercenaries mercenari - mercenary mercenari - mercer mercer - merchandise merchandis - merchandized merchand - merchant merchant - merchants merchant - mercies merci - merciful merci - mercifully mercifulli - merciless merciless - mercurial mercuri - mercuries mercuri - mercury mercuri - mercutio mercutio - mercy merci - mere mere - mered mere - merely mere - merest merest - meridian meridian - merit merit - merited merit - meritorious meritori - merits merit - merlin merlin - mermaid mermaid - mermaids mermaid - merops merop - merrier merrier - merriest merriest - merrily merrili - merriman merriman - merriment merriment - merriments merriment - merriness merri - merry merri - mervailous mervail - mes me - mesh mesh - meshes mesh - mesopotamia mesopotamia - mess mess - message messag - messages messag - messala messala - messaline messalin - messenger messeng - messengers messeng - messes mess - messina messina - met met - metal metal - metals metal - metamorphis metamorphi - metamorphoses metamorphos - metaphor metaphor - metaphysical metaphys - metaphysics metaphys - mete mete - metellus metellu - meteor meteor - meteors meteor - meteyard meteyard - metheglin metheglin - metheglins metheglin - methink methink - methinks methink - method method - methods method - methought methought - methoughts methought - metre metr - metres metr - metropolis metropoli - mette mett - mettle mettl - mettled mettl - meus meu - mew mew - mewed mew - mewling mewl - mexico mexico - mi mi - mice mice - michael michael - michaelmas michaelma - micher micher - miching mich - mickle mickl - microcosm microcosm - mid mid - midas mida - middest middest - middle middl - middleham middleham - midnight midnight - midriff midriff - midst midst - midsummer midsumm - midway midwai - midwife midwif - midwives midwiv - mienne mienn - might might - mightful might - mightier mightier - mightiest mightiest - mightily mightili - mightiness mighti - mightst mightst - mighty mighti - milan milan - milch milch - mild mild - milder milder - mildest mildest - mildew mildew - mildews mildew - mildly mildli - mildness mild - mile mile - miles mile - milford milford - militarist militarist - military militari - milk milk - milking milk - milkmaid milkmaid - milks milk - milksops milksop - milky milki - mill mill - mille mill - miller miller - milliner millin - million million - millioned million - millions million - mills mill - millstones millston - milo milo - mimic mimic - minc minc - mince minc - minces minc - mincing minc - mind mind - minded mind - minding mind - mindless mindless - minds mind - mine mine - mineral miner - minerals miner - minerva minerva - mines mine - mingle mingl - mingled mingl - mingling mingl - minikin minikin - minim minim - minime minim - minimo minimo - minimus minimu - mining mine - minion minion - minions minion - minist minist - minister minist - ministers minist - ministration ministr - minnow minnow - minnows minnow - minola minola - minority minor - minos mino - minotaurs minotaur - minstrel minstrel - minstrels minstrel - minstrelsy minstrelsi - mint mint - mints mint - minute minut - minutely minut - minutes minut - minx minx - mio mio - mir mir - mirable mirabl - miracle miracl - miracles miracl - miraculous miracul - miranda miranda - mire mire - mirror mirror - mirrors mirror - mirth mirth - mirthful mirth - miry miri - mis mi - misadventur misadventur - misadventure misadventur - misanthropos misanthropo - misapplied misappli - misbecame misbecam - misbecom misbecom - misbecome misbecom - misbegot misbegot - misbegotten misbegotten - misbeliever misbeliev - misbelieving misbeliev - misbhav misbhav - miscall miscal - miscalled miscal - miscarried miscarri - miscarries miscarri - miscarry miscarri - miscarrying miscarri - mischance mischanc - mischances mischanc - mischief mischief - mischiefs mischief - mischievous mischiev - misconceived misconceiv - misconst misconst - misconster misconst - misconstruction misconstruct - misconstrued misconstru - misconstrues misconstru - miscreant miscreant - miscreate miscreat - misdeed misde - misdeeds misde - misdemean misdemean - misdemeanours misdemeanour - misdoubt misdoubt - misdoubteth misdoubteth - misdoubts misdoubt - misenum misenum - miser miser - miserable miser - miserably miser - misericorde misericord - miseries miseri - misers miser - misery miseri - misfortune misfortun - misfortunes misfortun - misgive misgiv - misgives misgiv - misgiving misgiv - misgoverned misgovern - misgovernment misgovern - misgraffed misgraf - misguide misguid - mishap mishap - mishaps mishap - misheard misheard - misinterpret misinterpret - mislead mislead - misleader mislead - misleaders mislead - misleading mislead - misled misl - mislike mislik - misord misord - misplac misplac - misplaced misplac - misplaces misplac - mispris mispri - misprised mispris - misprision mispris - misprizing mispriz - misproud misproud - misquote misquot - misreport misreport - miss miss - missed miss - misses miss - misshap misshap - misshapen misshapen - missheathed missheath - missing miss - missingly missingli - missions mission - missive missiv - missives missiv - misspoke misspok - mist mist - mista mista - mistak mistak - mistake mistak - mistaken mistaken - mistakes mistak - mistaketh mistaketh - mistaking mistak - mistakings mistak - mistemp mistemp - mistempered mistemp - misterm misterm - mistful mist - misthink misthink - misthought misthought - mistletoe mistleto - mistook mistook - mistreadings mistread - mistress mistress - mistresses mistress - mistresss mistresss - mistriship mistriship - mistrust mistrust - mistrusted mistrust - mistrustful mistrust - mistrusting mistrust - mists mist - misty misti - misus misu - misuse misus - misused misus - misuses misus - mites mite - mithridates mithrid - mitigate mitig - mitigation mitig - mix mix - mixed mix - mixture mixtur - mixtures mixtur - mm mm - mnd mnd - moan moan - moans moan - moat moat - moated moat - mobled mobl - mock mock - mockable mockabl - mocker mocker - mockeries mockeri - mockers mocker - mockery mockeri - mocking mock - mocks mock - mockvater mockvat - mockwater mockwat - model model - modena modena - moderate moder - moderately moder - moderation moder - modern modern - modest modest - modesties modesti - modestly modestli - modesty modesti - modicums modicum - modo modo - module modul - moe moe - moi moi - moiety moieti - moist moist - moisten moisten - moisture moistur - moldwarp moldwarp - mole mole - molehill molehil - moles mole - molest molest - molestation molest - mollification mollif - mollis molli - molten molten - molto molto - mome mome - moment moment - momentary momentari - moming mome - mon mon - monachum monachum - monarch monarch - monarchies monarchi - monarchize monarch - monarcho monarcho - monarchs monarch - monarchy monarchi - monast monast - monastery monasteri - monastic monast - monday mondai - monde mond - money monei - moneys monei - mong mong - monger monger - mongers monger - monging mong - mongrel mongrel - mongrels mongrel - mongst mongst - monk monk - monkey monkei - monkeys monkei - monks monk - monmouth monmouth - monopoly monopoli - mons mon - monsieur monsieur - monsieurs monsieur - monster monster - monsters monster - monstrous monstrou - monstrously monstrous - monstrousness monstrous - monstruosity monstruos - montacute montacut - montage montag - montague montagu - montagues montagu - montano montano - montant montant - montez montez - montferrat montferrat - montgomery montgomeri - month month - monthly monthli - months month - montjoy montjoi - monument monument - monumental monument - monuments monument - mood mood - moods mood - moody moodi - moon moon - moonbeams moonbeam - moonish moonish - moonlight moonlight - moons moon - moonshine moonshin - moonshines moonshin - moor moor - moorfields moorfield - moors moor - moorship moorship - mop mop - mope mope - moping mope - mopping mop - mopsa mopsa - moral moral - moraler moral - morality moral - moralize moral - mordake mordak - more more - moreover moreov - mores more - morgan morgan - mori mori - morisco morisco - morn morn - morning morn - mornings morn - morocco morocco - morris morri - morrow morrow - morrows morrow - morsel morsel - morsels morsel - mort mort - mortal mortal - mortality mortal - mortally mortal - mortals mortal - mortar mortar - mortgaged mortgag - mortified mortifi - mortifying mortifi - mortimer mortim - mortimers mortim - mortis morti - mortise mortis - morton morton - mose mose - moss moss - mossgrown mossgrown - most most - mote mote - moth moth - mother mother - mothers mother - moths moth - motion motion - motionless motionless - motions motion - motive motiv - motives motiv - motley motlei - mots mot - mought mought - mould mould - moulded mould - mouldeth mouldeth - moulds mould - mouldy mouldi - moult moult - moulten moulten - mounch mounch - mounseur mounseur - mounsieur mounsieur - mount mount - mountain mountain - mountaineer mountain - mountaineers mountain - mountainous mountain - mountains mountain - mountant mountant - mountanto mountanto - mountebank mountebank - mountebanks mountebank - mounted mount - mounteth mounteth - mounting mount - mounts mount - mourn mourn - mourned mourn - mourner mourner - mourners mourner - mournful mourn - mournfully mournfulli - mourning mourn - mourningly mourningli - mournings mourn - mourns mourn - mous mou - mouse mous - mousetrap mousetrap - mousing mous - mouth mouth - mouthed mouth - mouths mouth - mov mov - movables movabl - move move - moveable moveabl - moveables moveabl - moved move - mover mover - movers mover - moves move - moveth moveth - moving move - movingly movingli - movousus movousu - mow mow - mowbray mowbrai - mower mower - mowing mow - mows mow - moy moi - moys moi - moyses moys - mrs mr - much much - muck muck - mud mud - mudded mud - muddied muddi - muddy muddi - muffins muffin - muffl muffl - muffle muffl - muffled muffl - muffler muffler - muffling muffl - mugger mugger - mugs mug - mulberries mulberri - mulberry mulberri - mule mule - mules mule - muleteers mulet - mulier mulier - mulieres mulier - muliteus muliteu - mull mull - mulmutius mulmutiu - multiplied multipli - multiply multipli - multiplying multipli - multipotent multipot - multitude multitud - multitudes multitud - multitudinous multitudin - mum mum - mumble mumbl - mumbling mumbl - mummers mummer - mummy mummi - mun mun - munch munch - muniments muniment - munition munit - murd murd - murder murder - murdered murder - murderer murder - murderers murder - murdering murder - murderous murder - murders murder - mure mure - murk murk - murkiest murkiest - murky murki - murmur murmur - murmurers murmur - murmuring murmur - murrain murrain - murray murrai - murrion murrion - murther murther - murtherer murther - murtherers murther - murthering murther - murtherous murther - murthers murther - mus mu - muscadel muscadel - muscovites muscovit - muscovits muscovit - muscovy muscovi - muse muse - muses muse - mush mush - mushrooms mushroom - music music - musical music - musician musician - musicians musician - musics music - musing muse - musings muse - musk musk - musket musket - muskets musket - muskos musko - muss muss - mussel mussel - mussels mussel - must must - mustachio mustachio - mustard mustard - mustardseed mustardse - muster muster - mustering muster - musters muster - musty musti - mutability mutabl - mutable mutabl - mutation mutat - mutations mutat - mute mute - mutes mute - mutest mutest - mutine mutin - mutineer mutin - mutineers mutin - mutines mutin - mutinies mutini - mutinous mutin - mutiny mutini - mutius mutiu - mutter mutter - muttered mutter - mutton mutton - muttons mutton - mutual mutual - mutualities mutual - mutually mutual - muzzl muzzl - muzzle muzzl - muzzled muzzl - mv mv - mww mww - my my - mynheers mynheer - myrmidon myrmidon - myrmidons myrmidon - myrtle myrtl - myself myself - myst myst - mysteries mysteri - mystery mysteri - n n - nag nag - nage nage - nags nag - naiads naiad - nail nail - nails nail - nak nak - naked nake - nakedness naked - nal nal - nam nam - name name - named name - nameless nameless - namely name - names name - namest namest - naming name - nan nan - nance nanc - nap nap - nape nape - napes nape - napkin napkin - napkins napkin - naples napl - napless napless - napping nap - naps nap - narbon narbon - narcissus narcissu - narines narin - narrow narrow - narrowly narrowli - naso naso - nasty nasti - nathaniel nathaniel - natifs natif - nation nation - nations nation - native nativ - nativity nativ - natur natur - natural natur - naturalize natur - naturally natur - nature natur - natured natur - natures natur - natus natu - naught naught - naughtily naughtili - naughty naughti - navarre navarr - nave nave - navel navel - navigation navig - navy navi - nay nai - nayward nayward - nayword nayword - nazarite nazarit - ne ne - neaf neaf - neamnoins neamnoin - neanmoins neanmoin - neapolitan neapolitan - neapolitans neapolitan - near near - nearer nearer - nearest nearest - nearly nearli - nearness near - neat neat - neatly neatli - neb neb - nebour nebour - nebuchadnezzar nebuchadnezzar - nec nec - necessaries necessari - necessarily necessarili - necessary necessari - necessitied necess - necessities necess - necessity necess - neck neck - necklace necklac - necks neck - nectar nectar - ned ned - nedar nedar - need need - needed need - needer needer - needful need - needfull needful - needing need - needle needl - needles needl - needless needless - needly needli - needs need - needy needi - neer neer - neeze neez - nefas nefa - negation negat - negative neg - negatives neg - neglect neglect - neglected neglect - neglecting neglect - neglectingly neglectingli - neglection neglect - negligence neglig - negligent neglig - negotiate negoti - negotiations negoti - negro negro - neigh neigh - neighbors neighbor - neighbour neighbour - neighbourhood neighbourhood - neighbouring neighbour - neighbourly neighbourli - neighbours neighbour - neighing neigh - neighs neigh - neither neither - nell nell - nemean nemean - nemesis nemesi - neoptolemus neoptolemu - nephew nephew - nephews nephew - neptune neptun - ner ner - nereides nereid - nerissa nerissa - nero nero - neroes nero - ners ner - nerve nerv - nerves nerv - nervii nervii - nervy nervi - nessus nessu - nest nest - nestor nestor - nests nest - net net - nether nether - netherlands netherland - nets net - nettle nettl - nettled nettl - nettles nettl - neuter neuter - neutral neutral - nev nev - never never - nevil nevil - nevils nevil - new new - newborn newborn - newer newer - newest newest - newgate newgat - newly newli - newness new - news new - newsmongers newsmong - newt newt - newts newt - next next - nibbling nibbl - nicanor nicanor - nice nice - nicely nice - niceness nice - nicer nicer - nicety niceti - nicholas nichola - nick nick - nickname nicknam - nicks nick - niece niec - nieces niec - niggard niggard - niggarding niggard - niggardly niggardli - nigh nigh - night night - nightcap nightcap - nightcaps nightcap - nighted night - nightgown nightgown - nightingale nightingal - nightingales nightingal - nightly nightli - nightmare nightmar - nights night - nightwork nightwork - nihil nihil - nile nile - nill nill - nilus nilu - nimble nimbl - nimbleness nimbl - nimbler nimbler - nimbly nimbl - nine nine - nineteen nineteen - ning ning - ningly ningli - ninny ninni - ninth ninth - ninus ninu - niobe niob - niobes niob - nip nip - nipp nipp - nipping nip - nipple nippl - nips nip - nit nit - nly nly - nnight nnight - nnights nnight - no no - noah noah - nob nob - nobility nobil - nobis nobi - noble nobl - nobleman nobleman - noblemen noblemen - nobleness nobl - nobler nobler - nobles nobl - noblesse nobless - noblest noblest - nobly nobli - nobody nobodi - noces noce - nod nod - nodded nod - nodding nod - noddle noddl - noddles noddl - noddy noddi - nods nod - noes noe - nointed noint - nois noi - noise nois - noiseless noiseless - noisemaker noisemak - noises nois - noisome noisom - nole nole - nominate nomin - nominated nomin - nomination nomin - nominativo nominativo - non non - nonage nonag - nonce nonc - none none - nonino nonino - nonny nonni - nonpareil nonpareil - nonsuits nonsuit - nony noni - nook nook - nooks nook - noon noon - noonday noondai - noontide noontid - nor nor - norbery norberi - norfolk norfolk - norman norman - normandy normandi - normans norman - north north - northampton northampton - northamptonshire northamptonshir - northerly northerli - northern northern - northgate northgat - northumberland northumberland - northumberlands northumberland - northward northward - norway norwai - norways norwai - norwegian norwegian - norweyan norweyan - nos no - nose nose - nosegays nosegai - noseless noseless - noses nose - noster noster - nostra nostra - nostril nostril - nostrils nostril - not not - notable notabl - notably notabl - notary notari - notch notch - note note - notebook notebook - noted note - notedly notedli - notes note - notest notest - noteworthy noteworthi - nothing noth - nothings noth - notice notic - notify notifi - noting note - notion notion - notorious notori - notoriously notori - notre notr - notwithstanding notwithstand - nought nought - noun noun - nouns noun - nourish nourish - nourished nourish - nourisher nourish - nourishes nourish - nourisheth nourisheth - nourishing nourish - nourishment nourish - nous nou - novel novel - novelties novelti - novelty novelti - noverbs noverb - novi novi - novice novic - novices novic - novum novum - now now - nowhere nowher - noyance noyanc - ns ns - nt nt - nubibus nubibu - numa numa - numb numb - number number - numbered number - numbering number - numberless numberless - numbers number - numbness numb - nun nun - nuncio nuncio - nuncle nuncl - nunnery nunneri - nuns nun - nuntius nuntiu - nuptial nuptial - nurs nur - nurse nurs - nursed nurs - nurser nurser - nursery nurseri - nurses nurs - nurseth nurseth - nursh nursh - nursing nurs - nurtur nurtur - nurture nurtur - nut nut - nuthook nuthook - nutmeg nutmeg - nutmegs nutmeg - nutriment nutriment - nuts nut - nutshell nutshel - ny ny - nym nym - nymph nymph - nymphs nymph - o o - oak oak - oaken oaken - oaks oak - oared oar - oars oar - oatcake oatcak - oaten oaten - oath oath - oathable oathabl - oaths oath - oats oat - ob ob - obduracy obduraci - obdurate obdur - obedience obedi - obedient obedi - obeisance obeis - oberon oberon - obey obei - obeyed obei - obeying obei - obeys obei - obidicut obidicut - object object - objected object - objections object - objects object - oblation oblat - oblations oblat - obligation oblig - obligations oblig - obliged oblig - oblique obliqu - oblivion oblivion - oblivious oblivi - obloquy obloqui - obscene obscen - obscenely obscen - obscur obscur - obscure obscur - obscured obscur - obscurely obscur - obscures obscur - obscuring obscur - obscurity obscur - obsequies obsequi - obsequious obsequi - obsequiously obsequi - observ observ - observance observ - observances observ - observancy observ - observant observ - observants observ - observation observ - observe observ - observed observ - observer observ - observers observ - observing observ - observingly observingli - obsque obsqu - obstacle obstacl - obstacles obstacl - obstinacy obstinaci - obstinate obstin - obstinately obstin - obstruct obstruct - obstruction obstruct - obstructions obstruct - obtain obtain - obtained obtain - obtaining obtain - occasion occas - occasions occas - occident occid - occidental occident - occulted occult - occupat occupat - occupation occup - occupations occup - occupied occupi - occupies occupi - occupy occupi - occurrence occurr - occurrences occurr - occurrents occurr - ocean ocean - oceans ocean - octavia octavia - octavius octaviu - ocular ocular - od od - odd odd - oddest oddest - oddly oddli - odds odd - ode od - odes od - odious odiou - odoriferous odorifer - odorous odor - odour odour - odours odour - ods od - oeillades oeillad - oes oe - oeuvres oeuvr - of of - ofephesus ofephesu - off off - offal offal - offence offenc - offenceful offenc - offences offenc - offend offend - offended offend - offendendo offendendo - offender offend - offenders offend - offendeth offendeth - offending offend - offendress offendress - offends offend - offense offens - offenseless offenseless - offenses offens - offensive offens - offer offer - offered offer - offering offer - offerings offer - offers offer - offert offert - offic offic - office offic - officed offic - officer offic - officers offic - offices offic - official offici - officious offici - offspring offspr - oft oft - often often - oftener often - oftentimes oftentim - oh oh - oil oil - oils oil - oily oili - old old - oldcastle oldcastl - olden olden - older older - oldest oldest - oldness old - olive oliv - oliver oliv - olivers oliv - olives oliv - olivia olivia - olympian olympian - olympus olympu - oman oman - omans oman - omen omen - ominous omin - omission omiss - omit omit - omittance omitt - omitted omit - omitting omit - omne omn - omnes omn - omnipotent omnipot - on on - once onc - one on - ones on - oneyers oney - ongles ongl - onion onion - onions onion - only onli - onset onset - onward onward - onwards onward - oo oo - ooze ooz - oozes ooz - oozy oozi - op op - opal opal - ope op - open open - opener open - opening open - openly openli - openness open - opens open - operant oper - operate oper - operation oper - operations oper - operative oper - opes op - oph oph - ophelia ophelia - opinion opinion - opinions opinion - opportune opportun - opportunities opportun - opportunity opportun - oppos oppo - oppose oppos - opposed oppos - opposeless opposeless - opposer oppos - opposers oppos - opposes oppos - opposing oppos - opposite opposit - opposites opposit - opposition opposit - oppositions opposit - oppress oppress - oppressed oppress - oppresses oppress - oppresseth oppresseth - oppressing oppress - oppression oppress - oppressor oppressor - opprest opprest - opprobriously opprobri - oppugnancy oppugn - opulency opul - opulent opul - or or - oracle oracl - oracles oracl - orange orang - oration orat - orator orat - orators orat - oratory oratori - orb orb - orbed orb - orbs orb - orchard orchard - orchards orchard - ord ord - ordain ordain - ordained ordain - ordaining ordain - order order - ordered order - ordering order - orderless orderless - orderly orderli - orders order - ordinance ordin - ordinant ordin - ordinaries ordinari - ordinary ordinari - ordnance ordnanc - ords ord - ordure ordur - ore or - organ organ - organs organ - orgillous orgil - orient orient - orifex orifex - origin origin - original origin - orisons orison - ork ork - orlando orlando - orld orld - orleans orlean - ornament ornament - ornaments ornament - orodes orod - orphan orphan - orphans orphan - orpheus orpheu - orsino orsino - ort ort - orthography orthographi - orts ort - oscorbidulchos oscorbidulcho - osier osier - osiers osier - osprey osprei - osr osr - osric osric - ossa ossa - ost ost - ostent ostent - ostentare ostentar - ostentation ostent - ostents ostent - ostler ostler - ostlers ostler - ostrich ostrich - osw osw - oswald oswald - othello othello - other other - othergates otherg - others other - otherwhere otherwher - otherwhiles otherwhil - otherwise otherwis - otter otter - ottoman ottoman - ottomites ottomit - oublie oubli - ouches ouch - ought ought - oui oui - ounce ounc - ounces ounc - ouphes ouph - our our - ours our - ourself ourself - ourselves ourselv - ousel ousel - out out - outbids outbid - outbrave outbrav - outbraves outbrav - outbreak outbreak - outcast outcast - outcries outcri - outcry outcri - outdar outdar - outdare outdar - outdares outdar - outdone outdon - outfac outfac - outface outfac - outfaced outfac - outfacing outfac - outfly outfli - outfrown outfrown - outgo outgo - outgoes outgo - outgrown outgrown - outjest outjest - outlaw outlaw - outlawry outlawri - outlaws outlaw - outliv outliv - outlive outliv - outlives outliv - outliving outliv - outlook outlook - outlustres outlustr - outpriz outpriz - outrage outrag - outrageous outrag - outrages outrag - outran outran - outright outright - outroar outroar - outrun outrun - outrunning outrun - outruns outrun - outscold outscold - outscorn outscorn - outsell outsel - outsells outsel - outside outsid - outsides outsid - outspeaks outspeak - outsport outsport - outstare outstar - outstay outstai - outstood outstood - outstretch outstretch - outstretched outstretch - outstrike outstrik - outstrip outstrip - outstripped outstrip - outswear outswear - outvenoms outvenom - outward outward - outwardly outwardli - outwards outward - outwear outwear - outweighs outweigh - outwent outwent - outworn outworn - outworths outworth - oven oven - over over - overawe overaw - overbear overbear - overblown overblown - overboard overboard - overbold overbold - overborne overborn - overbulk overbulk - overbuys overbui - overcame overcam - overcast overcast - overcharg overcharg - overcharged overcharg - overcome overcom - overcomes overcom - overdone overdon - overearnest overearnest - overfar overfar - overflow overflow - overflown overflown - overglance overgl - overgo overgo - overgone overgon - overgorg overgorg - overgrown overgrown - overhead overhead - overhear overhear - overheard overheard - overhold overhold - overjoyed overjoi - overkind overkind - overland overland - overleather overleath - overlive overl - overlook overlook - overlooking overlook - overlooks overlook - overmaster overmast - overmounting overmount - overmuch overmuch - overpass overpass - overpeer overp - overpeering overp - overplus overplu - overrul overrul - overrun overrun - overscutch overscutch - overset overset - overshades overshad - overshine overshin - overshines overshin - overshot overshot - oversights oversight - overspread overspread - overstain overstain - overswear overswear - overt overt - overta overta - overtake overtak - overtaketh overtaketh - overthrow overthrow - overthrown overthrown - overthrows overthrow - overtook overtook - overtopp overtopp - overture overtur - overturn overturn - overwatch overwatch - overween overween - overweening overween - overweigh overweigh - overwhelm overwhelm - overwhelming overwhelm - overworn overworn - ovid ovid - ovidius ovidiu - ow ow - owe ow - owed ow - owedst owedst - owen owen - owes ow - owest owest - oweth oweth - owing ow - owl owl - owls owl - own own - owner owner - owners owner - owning own - owns own - owy owi - ox ox - oxen oxen - oxford oxford - oxfordshire oxfordshir - oxlips oxlip - oyes oy - oyster oyster - p p - pabble pabbl - pabylon pabylon - pac pac - pace pace - paced pace - paces pace - pacified pacifi - pacify pacifi - pacing pace - pack pack - packet packet - packets packet - packhorses packhors - packing pack - packings pack - packs pack - packthread packthread - pacorus pacoru - paction paction - pad pad - paddle paddl - paddling paddl - paddock paddock - padua padua - pagan pagan - pagans pagan - page page - pageant pageant - pageants pageant - pages page - pah pah - paid paid - pail pail - pailfuls pail - pails pail - pain pain - pained pain - painful pain - painfully painfulli - pains pain - paint paint - painted paint - painter painter - painting paint - paintings paint - paints paint - pair pair - paired pair - pairs pair - pajock pajock - pal pal - palabras palabra - palace palac - palaces palac - palamedes palamed - palate palat - palates palat - palatine palatin - palating palat - pale pale - paled pale - paleness pale - paler paler - pales pale - palestine palestin - palfrey palfrei - palfreys palfrei - palisadoes palisado - pall pall - pallabris pallabri - pallas palla - pallets pallet - palm palm - palmer palmer - palmers palmer - palms palm - palmy palmi - palpable palpabl - palsied palsi - palsies palsi - palsy palsi - palt palt - palter palter - paltry paltri - paly pali - pamp pamp - pamper pamper - pamphlets pamphlet - pan pan - pancackes pancack - pancake pancak - pancakes pancak - pandar pandar - pandars pandar - pandarus pandaru - pander pander - panderly panderli - panders pander - pandulph pandulph - panel panel - pang pang - panging pang - pangs pang - pannier pannier - pannonians pannonian - pansa pansa - pansies pansi - pant pant - pantaloon pantaloon - panted pant - pantheon pantheon - panther panther - panthino panthino - panting pant - pantingly pantingli - pantler pantler - pantry pantri - pants pant - pap pap - papal papal - paper paper - papers paper - paphlagonia paphlagonia - paphos papho - papist papist - paps pap - par par - parable parabl - paracelsus paracelsu - paradise paradis - paradox paradox - paradoxes paradox - paragon paragon - paragons paragon - parallel parallel - parallels parallel - paramour paramour - paramours paramour - parapets parapet - paraquito paraquito - parasite parasit - parasites parasit - parca parca - parcel parcel - parcell parcel - parcels parcel - parch parch - parched parch - parching parch - parchment parchment - pard pard - pardon pardon - pardona pardona - pardoned pardon - pardoner pardon - pardoning pardon - pardonne pardonn - pardonner pardonn - pardonnez pardonnez - pardons pardon - pare pare - pared pare - parel parel - parent parent - parentage parentag - parents parent - parfect parfect - paring pare - parings pare - paris pari - parish parish - parishioners parishion - parisians parisian - paritors paritor - park park - parks park - parle parl - parler parler - parles parl - parley parlei - parlez parlez - parliament parliament - parlors parlor - parlour parlour - parlous parlou - parmacity parmac - parolles parol - parricide parricid - parricides parricid - parrot parrot - parrots parrot - parsley parslei - parson parson - part part - partake partak - partaken partaken - partaker partak - partakers partak - parted part - parthia parthia - parthian parthian - parthians parthian - parti parti - partial partial - partialize partial - partially partial - participate particip - participation particip - particle particl - particular particular - particularities particular - particularize particular - particularly particularli - particulars particular - parties parti - parting part - partisan partisan - partisans partisan - partition partit - partizan partizan - partlet partlet - partly partli - partner partner - partners partner - partridge partridg - parts part - party parti - pas pa - pash pash - pashed pash - pashful pash - pass pass - passable passabl - passado passado - passage passag - passages passag - passant passant - passed pass - passenger passeng - passengers passeng - passes pass - passeth passeth - passing pass - passio passio - passion passion - passionate passion - passioning passion - passions passion - passive passiv - passport passport - passy passi - past past - paste past - pasterns pastern - pasties pasti - pastime pastim - pastimes pastim - pastoral pastor - pastorals pastor - pastors pastor - pastry pastri - pasture pastur - pastures pastur - pasty pasti - pat pat - patay patai - patch patch - patchery patcheri - patches patch - pate pate - pated pate - patent patent - patents patent - paternal patern - pates pate - path path - pathetical pathet - paths path - pathway pathwai - pathways pathwai - patience patienc - patient patient - patiently patient - patients patient - patines patin - patrician patrician - patricians patrician - patrick patrick - patrimony patrimoni - patroclus patroclu - patron patron - patronage patronag - patroness patro - patrons patron - patrum patrum - patter patter - pattern pattern - patterns pattern - pattle pattl - pauca pauca - paucas pauca - paul paul - paulina paulina - paunch paunch - paunches paunch - pause paus - pauser pauser - pauses paus - pausingly pausingli - pauvres pauvr - pav pav - paved pave - pavement pavement - pavilion pavilion - pavilions pavilion - pavin pavin - paw paw - pawn pawn - pawns pawn - paws paw - pax pax - pay pai - payest payest - paying pai - payment payment - payments payment - pays pai - paysan paysan - paysans paysan - pe pe - peace peac - peaceable peaceabl - peaceably peaceabl - peaceful peac - peacemakers peacemak - peaces peac - peach peach - peaches peach - peacock peacock - peacocks peacock - peak peak - peaking peak - peal peal - peals peal - pear pear - peard peard - pearl pearl - pearls pearl - pears pear - peas pea - peasant peasant - peasantry peasantri - peasants peasant - peascod peascod - pease peas - peaseblossom peaseblossom - peat peat - peaten peaten - peating peat - pebble pebbl - pebbled pebbl - pebbles pebbl - peck peck - pecks peck - peculiar peculiar - pecus pecu - pedant pedant - pedantical pedant - pedascule pedascul - pede pede - pedestal pedest - pedigree pedigre - pedlar pedlar - pedlars pedlar - pedro pedro - peds ped - peel peel - peep peep - peeped peep - peeping peep - peeps peep - peer peer - peereth peereth - peering peer - peerless peerless - peers peer - peesel peesel - peevish peevish - peevishly peevishli - peflur peflur - peg peg - pegasus pegasu - pegs peg - peise peis - peised peis - peize peiz - pelf pelf - pelican pelican - pelion pelion - pell pell - pella pella - pelleted pellet - peloponnesus peloponnesu - pelt pelt - pelting pelt - pembroke pembrok - pen pen - penalties penalti - penalty penalti - penance penanc - pence penc - pencil pencil - pencill pencil - pencils pencil - pendant pendant - pendent pendent - pendragon pendragon - pendulous pendul - penelope penelop - penetrable penetr - penetrate penetr - penetrative penetr - penitence penit - penitent penit - penitential penitenti - penitently penit - penitents penit - penker penker - penknife penknif - penn penn - penned pen - penning pen - pennons pennon - penny penni - pennyworth pennyworth - pennyworths pennyworth - pens pen - pense pens - pension pension - pensioners pension - pensive pensiv - pensived pensiv - pensively pensiv - pent pent - pentecost pentecost - penthesilea penthesilea - penthouse penthous - penurious penuri - penury penuri - peopl peopl - people peopl - peopled peopl - peoples peopl - pepin pepin - pepper pepper - peppercorn peppercorn - peppered pepper - per per - peradventure peradventur - peradventures peradventur - perceiv perceiv - perceive perceiv - perceived perceiv - perceives perceiv - perceiveth perceiveth - perch perch - perchance perchanc - percies perci - percussion percuss - percy perci - perdie perdi - perdita perdita - perdition perdit - perdonato perdonato - perdu perdu - perdurable perdur - perdurably perdur - perdy perdi - pere pere - peregrinate peregrin - peremptorily peremptorili - peremptory peremptori - perfect perfect - perfected perfect - perfecter perfect - perfectest perfectest - perfection perfect - perfections perfect - perfectly perfectli - perfectness perfect - perfidious perfidi - perfidiously perfidi - perforce perforc - perform perform - performance perform - performances perform - performed perform - performer perform - performers perform - performing perform - performs perform - perfum perfum - perfume perfum - perfumed perfum - perfumer perfum - perfumes perfum - perge perg - perhaps perhap - periapts periapt - perigort perigort - perigouna perigouna - peril peril - perilous peril - perils peril - period period - periods period - perish perish - perished perish - perishest perishest - perisheth perisheth - perishing perish - periwig periwig - perjur perjur - perjure perjur - perjured perjur - perjuries perjuri - perjury perjuri - perk perk - perkes perk - permafoy permafoi - permanent perman - permission permiss - permissive permiss - permit permit - permitted permit - pernicious pernici - perniciously pernici - peroration peror - perpend perpend - perpendicular perpendicular - perpendicularly perpendicularli - perpetual perpetu - perpetually perpetu - perpetuity perpetu - perplex perplex - perplexed perplex - perplexity perplex - pers per - persecuted persecut - persecutions persecut - persecutor persecutor - perseus perseu - persever persev - perseverance persever - persevers persev - persia persia - persian persian - persist persist - persisted persist - persistency persist - persistive persist - persists persist - person person - personae persona - personage personag - personages personag - personal person - personally person - personate person - personated person - personates person - personating person - persons person - perspective perspect - perspectively perspect - perspectives perspect - perspicuous perspicu - persuade persuad - persuaded persuad - persuades persuad - persuading persuad - persuasion persuas - persuasions persuas - pert pert - pertain pertain - pertaining pertain - pertains pertain - pertaunt pertaunt - pertinent pertin - pertly pertli - perturb perturb - perturbation perturb - perturbations perturb - perturbed perturb - perus peru - perusal perus - peruse perus - perused perus - perusing perus - perverse pervers - perversely pervers - perverseness pervers - pervert pervert - perverted pervert - peseech peseech - pest pest - pester pester - pestiferous pestifer - pestilence pestil - pestilent pestil - pet pet - petar petar - peter peter - petit petit - petition petit - petitionary petitionari - petitioner petition - petitioners petition - petitions petit - peto peto - petrarch petrarch - petruchio petruchio - petter petter - petticoat petticoat - petticoats petticoat - pettiness petti - pettish pettish - pettitoes pettito - petty petti - peu peu - pew pew - pewter pewter - pewterer pewter - phaethon phaethon - phaeton phaeton - phantasime phantasim - phantasimes phantasim - phantasma phantasma - pharamond pharamond - pharaoh pharaoh - pharsalia pharsalia - pheasant pheasant - pheazar pheazar - phebe phebe - phebes phebe - pheebus pheebu - pheeze pheez - phibbus phibbu - philadelphos philadelpho - philario philario - philarmonus philarmonu - philemon philemon - philip philip - philippan philippan - philippe philipp - philippi philippi - phillida phillida - philo philo - philomel philomel - philomela philomela - philosopher philosoph - philosophers philosoph - philosophical philosoph - philosophy philosophi - philostrate philostr - philotus philotu - phlegmatic phlegmat - phoebe phoeb - phoebus phoebu - phoenicia phoenicia - phoenicians phoenician - phoenix phoenix - phorbus phorbu - photinus photinu - phrase phrase - phraseless phraseless - phrases phrase - phrygia phrygia - phrygian phrygian - phrynia phrynia - physic physic - physical physic - physician physician - physicians physician - physics physic - pia pia - pibble pibbl - pible pibl - picardy picardi - pick pick - pickaxe pickax - pickaxes pickax - pickbone pickbon - picked pick - pickers picker - picking pick - pickle pickl - picklock picklock - pickpurse pickpurs - picks pick - pickt pickt - pickthanks pickthank - pictur pictur - picture pictur - pictured pictur - pictures pictur - pid pid - pie pie - piec piec - piece piec - pieces piec - piecing piec - pied pi - piedness pied - pier pier - pierc pierc - pierce pierc - pierced pierc - pierces pierc - pierceth pierceth - piercing pierc - piercy pierci - piers pier - pies pi - piety pieti - pig pig - pigeon pigeon - pigeons pigeon - pight pight - pigmy pigmi - pigrogromitus pigrogromitu - pike pike - pikes pike - pil pil - pilate pilat - pilates pilat - pilchers pilcher - pile pile - piles pile - pilf pilf - pilfering pilfer - pilgrim pilgrim - pilgrimage pilgrimag - pilgrims pilgrim - pill pill - pillage pillag - pillagers pillag - pillar pillar - pillars pillar - pillicock pillicock - pillory pillori - pillow pillow - pillows pillow - pills pill - pilot pilot - pilots pilot - pimpernell pimpernel - pin pin - pinch pinch - pinched pinch - pinches pinch - pinching pinch - pindarus pindaru - pine pine - pined pine - pines pine - pinfold pinfold - pining pine - pinion pinion - pink pink - pinn pinn - pinnace pinnac - pins pin - pinse pins - pint pint - pintpot pintpot - pioned pion - pioneers pioneer - pioner pioner - pioners pioner - pious piou - pip pip - pipe pipe - piper piper - pipers piper - pipes pipe - piping pipe - pippin pippin - pippins pippin - pirate pirat - pirates pirat - pisa pisa - pisanio pisanio - pish pish - pismires pismir - piss piss - pissing piss - pistol pistol - pistols pistol - pit pit - pitch pitch - pitched pitch - pitcher pitcher - pitchers pitcher - pitchy pitchi - piteous piteou - piteously piteous - pitfall pitfal - pith pith - pithless pithless - pithy pithi - pitie piti - pitied piti - pities piti - pitiful piti - pitifully pitifulli - pitiless pitiless - pits pit - pittance pittanc - pittie pitti - pittikins pittikin - pity piti - pitying piti - pius piu - plac plac - place place - placed place - placentio placentio - places place - placeth placeth - placid placid - placing place - plack plack - placket placket - plackets placket - plagu plagu - plague plagu - plagued plagu - plagues plagu - plaguing plagu - plaguy plagui - plain plain - plainer plainer - plainest plainest - plaining plain - plainings plain - plainly plainli - plainness plain - plains plain - plainsong plainsong - plaintful plaint - plaintiff plaintiff - plaintiffs plaintiff - plaints plaint - planched planch - planet planet - planetary planetari - planets planet - planks plank - plant plant - plantage plantag - plantagenet plantagenet - plantagenets plantagenet - plantain plantain - plantation plantat - planted plant - planteth planteth - plants plant - plash plash - plashy plashi - plast plast - plaster plaster - plasterer plaster - plat plat - plate plate - plated plate - plates plate - platform platform - platforms platform - plats plat - platted plat - plausible plausibl - plausive plausiv - plautus plautu - play plai - played plai - player player - players player - playeth playeth - playfellow playfellow - playfellows playfellow - playhouse playhous - playing plai - plays plai - plea plea - pleach pleach - pleached pleach - plead plead - pleaded plead - pleader pleader - pleaders pleader - pleading plead - pleads plead - pleas plea - pleasance pleasanc - pleasant pleasant - pleasantly pleasantli - please pleas - pleased pleas - pleaser pleaser - pleasers pleaser - pleases pleas - pleasest pleasest - pleaseth pleaseth - pleasing pleas - pleasure pleasur - pleasures pleasur - plebeians plebeian - plebeii plebeii - plebs pleb - pledge pledg - pledges pledg - pleines plein - plenitude plenitud - plenteous plenteou - plenteously plenteous - plenties plenti - plentiful plenti - plentifully plentifulli - plenty plenti - pless pless - plessed pless - plessing pless - pliant pliant - plied pli - plies pli - plight plight - plighted plight - plighter plighter - plod plod - plodded plod - plodders plodder - plodding plod - plods plod - plood plood - ploody ploodi - plot plot - plots plot - plotted plot - plotter plotter - plough plough - ploughed plough - ploughman ploughman - ploughmen ploughmen - plow plow - plows plow - pluck pluck - plucked pluck - plucker plucker - plucking pluck - plucks pluck - plue plue - plum plum - plume plume - plumed plume - plumes plume - plummet plummet - plump plump - plumpy plumpi - plums plum - plung plung - plunge plung - plunged plung - plural plural - plurisy plurisi - plus plu - pluto pluto - plutus plutu - ply ply - po po - pocket pocket - pocketing pocket - pockets pocket - pocky pocki - pody podi - poem poem - poesy poesi - poet poet - poetical poetic - poetry poetri - poets poet - poictiers poictier - poinards poinard - poins poin - point point - pointblank pointblank - pointed point - pointing point - points point - pois poi - poise pois - poising pois - poison poison - poisoned poison - poisoner poison - poisoning poison - poisonous poison - poisons poison - poke poke - poking poke - pol pol - polack polack - polacks polack - poland poland - pold pold - pole pole - poleaxe poleax - polecat polecat - polecats polecat - polemon polemon - poles pole - poli poli - policies polici - policy polici - polish polish - polished polish - politic polit - politician politician - politicians politician - politicly politicli - polixenes polixen - poll poll - polluted pollut - pollution pollut - polonius poloniu - poltroons poltroon - polusion polus - polydamus polydamu - polydore polydor - polyxena polyxena - pomander pomand - pomegranate pomegran - pomewater pomewat - pomfret pomfret - pomgarnet pomgarnet - pommel pommel - pomp pomp - pompeius pompeiu - pompey pompei - pompion pompion - pompous pompou - pomps pomp - pond pond - ponder ponder - ponderous ponder - ponds pond - poniard poniard - poniards poniard - pont pont - pontic pontic - pontifical pontif - ponton ponton - pooh pooh - pool pool - poole pool - poop poop - poor poor - poorer poorer - poorest poorest - poorly poorli - pop pop - pope pope - popedom popedom - popilius popiliu - popingay popingai - popish popish - popp popp - poppy poppi - pops pop - popular popular - popularity popular - populous popul - porch porch - porches porch - pore pore - poring pore - pork pork - porn porn - porpentine porpentin - porridge porridg - porringer porring - port port - portable portabl - portage portag - portal portal - portance portanc - portcullis portculli - portend portend - portends portend - portent portent - portentous portent - portents portent - porter porter - porters porter - portia portia - portion portion - portly portli - portotartarossa portotartarossa - portrait portrait - portraiture portraitur - ports port - portugal portug - pose pose - posied posi - posies posi - position posit - positive posit - positively posit - posse poss - possess possess - possessed possess - possesses possess - possesseth possesseth - possessing possess - possession possess - possessions possess - possessor possessor - posset posset - possets posset - possibilities possibl - possibility possibl - possible possibl - possibly possibl - possitable possit - post post - poste post - posted post - posterior posterior - posteriors posterior - posterity poster - postern postern - posterns postern - posters poster - posthorse posthors - posthorses posthors - posthumus posthumu - posting post - postmaster postmast - posts post - postscript postscript - posture postur - postures postur - posy posi - pot pot - potable potabl - potations potat - potato potato - potatoes potato - potch potch - potency potenc - potent potent - potentates potent - potential potenti - potently potent - potents potent - pothecary pothecari - pother pother - potion potion - potions potion - potpan potpan - pots pot - potter potter - potting pot - pottle pottl - pouch pouch - poulter poulter - poultice poultic - poultney poultnei - pouncet pouncet - pound pound - pounds pound - pour pour - pourest pourest - pouring pour - pourquoi pourquoi - pours pour - pout pout - poverty poverti - pow pow - powd powd - powder powder - power power - powerful power - powerfully powerfulli - powerless powerless - powers power - pox pox - poys poi - poysam poysam - prabbles prabbl - practic practic - practice practic - practiced practic - practicer practic - practices practic - practicing practic - practis practi - practisants practis - practise practis - practiser practis - practisers practis - practises practis - practising practis - praeclarissimus praeclarissimu - praemunire praemunir - praetor praetor - praetors praetor - pragging prag - prague pragu - prain prain - prains prain - prais prai - praise prais - praised prais - praises prais - praisest praisest - praiseworthy praiseworthi - praising prais - prancing pranc - prank prank - pranks prank - prat prat - prate prate - prated prate - prater prater - prating prate - prattle prattl - prattler prattler - prattling prattl - prave prave - prawls prawl - prawns prawn - pray prai - prayer prayer - prayers prayer - praying prai - prays prai - pre pre - preach preach - preached preach - preachers preacher - preaches preach - preaching preach - preachment preachment - pread pread - preambulate preambul - precedence preced - precedent preced - preceding preced - precept precept - preceptial precepti - precepts precept - precinct precinct - precious preciou - preciously precious - precipice precipic - precipitating precipit - precipitation precipit - precise precis - precisely precis - preciseness precis - precisian precisian - precor precor - precurse precurs - precursors precursor - predeceased predeceas - predecessor predecessor - predecessors predecessor - predestinate predestin - predicament predica - predict predict - prediction predict - predictions predict - predominance predomin - predominant predomin - predominate predomin - preeches preech - preeminence preemin - preface prefac - prefer prefer - preferment prefer - preferments prefer - preferr preferr - preferreth preferreth - preferring prefer - prefers prefer - prefiguring prefigur - prefix prefix - prefixed prefix - preformed preform - pregnancy pregnanc - pregnant pregnant - pregnantly pregnantli - prejudicates prejud - prejudice prejudic - prejudicial prejudici - prelate prelat - premeditated premedit - premeditation premedit - premised premis - premises premis - prenez prenez - prenominate prenomin - prentice prentic - prentices prentic - preordinance preordin - prepar prepar - preparation prepar - preparations prepar - prepare prepar - prepared prepar - preparedly preparedli - prepares prepar - preparing prepar - prepost prepost - preposterous preposter - preposterously preposter - prerogatifes prerogatif - prerogative prerog - prerogatived prerogativ - presage presag - presagers presag - presages presag - presageth presageth - presaging presag - prescience prescienc - prescribe prescrib - prescript prescript - prescription prescript - prescriptions prescript - prescripts prescript - presence presenc - presences presenc - present present - presentation present - presented present - presenter present - presenters present - presenteth presenteth - presenting present - presently present - presentment present - presents present - preserv preserv - preservation preserv - preservative preserv - preserve preserv - preserved preserv - preserver preserv - preservers preserv - preserving preserv - president presid - press press - pressed press - presser presser - presses press - pressing press - pressure pressur - pressures pressur - prest prest - prester prester - presume presum - presumes presum - presuming presum - presumption presumpt - presumptuous presumptu - presuppos presuppo - pret pret - pretence pretenc - pretences pretenc - pretend pretend - pretended pretend - pretending pretend - pretense pretens - pretext pretext - pretia pretia - prettier prettier - prettiest prettiest - prettily prettili - prettiness pretti - pretty pretti - prevail prevail - prevailed prevail - prevaileth prevaileth - prevailing prevail - prevailment prevail - prevails prevail - prevent prevent - prevented prevent - prevention prevent - preventions prevent - prevents prevent - prey prei - preyful prey - preys prei - priam priam - priami priami - priamus priamu - pribbles pribbl - price price - prick prick - pricked prick - pricket pricket - pricking prick - pricks prick - pricksong pricksong - pride pride - prides pride - pridge pridg - prie prie - pried pri - prief prief - pries pri - priest priest - priesthood priesthood - priests priest - prig prig - primal primal - prime prime - primer primer - primero primero - primest primest - primitive primit - primo primo - primogenity primogen - primrose primros - primroses primros - primy primi - prince princ - princely princ - princes princ - princess princess - principal princip - principalities princip - principality princip - principle principl - principles principl - princox princox - prings pring - print print - printed print - printing print - printless printless - prints print - prioress prioress - priories priori - priority prioriti - priory priori - priscian priscian - prison prison - prisoner prison - prisoners prison - prisonment prison - prisonnier prisonni - prisons prison - pristine pristin - prithe prith - prithee prithe - privacy privaci - private privat - privately privat - privates privat - privilage privilag - privileg privileg - privilege privileg - privileged privileg - privileges privileg - privilegio privilegio - privily privili - privity priviti - privy privi - priz priz - prize prize - prized prize - prizer prizer - prizes prize - prizest prizest - prizing prize - pro pro - probable probabl - probal probal - probation probat - proceed proce - proceeded proceed - proceeders proceed - proceeding proceed - proceedings proceed - proceeds proce - process process - procession process - proclaim proclaim - proclaimed proclaim - proclaimeth proclaimeth - proclaims proclaim - proclamation proclam - proclamations proclam - proconsul proconsul - procrastinate procrastin - procreant procreant - procreants procreant - procreation procreat - procrus procru - proculeius proculeiu - procur procur - procurator procur - procure procur - procured procur - procures procur - procuring procur - prodigal prodig - prodigality prodig - prodigally prodig - prodigals prodig - prodigies prodigi - prodigious prodigi - prodigiously prodigi - prodigy prodigi - proditor proditor - produc produc - produce produc - produced produc - produces produc - producing produc - proface profac - profan profan - profanation profan - profane profan - profaned profan - profanely profan - profaneness profan - profaners profan - profaning profan - profess profess - professed profess - professes profess - profession profess - professions profess - professors professor - proffer proffer - proffered proffer - profferer proffer - proffers proffer - proficient profici - profit profit - profitable profit - profitably profit - profited profit - profiting profit - profitless profitless - profits profit - profound profound - profoundest profoundest - profoundly profoundli - progenitors progenitor - progeny progeni - progne progn - prognosticate prognost - prognostication prognost - progress progress - progression progress - prohibit prohibit - prohibition prohibit - project project - projection project - projects project - prolixious prolixi - prolixity prolix - prologue prologu - prologues prologu - prolong prolong - prolongs prolong - promethean promethean - prometheus prometheu - promis promi - promise promis - promised promis - promises promis - promiseth promiseth - promising promis - promontory promontori - promotion promot - promotions promot - prompt prompt - prompted prompt - promptement promptement - prompter prompter - prompting prompt - prompts prompt - prompture promptur - promulgate promulg - prone prone - prononcer prononc - prononcez prononcez - pronoun pronoun - pronounc pronounc - pronounce pronounc - pronounced pronounc - pronouncing pronounc - pronouns pronoun - proof proof - proofs proof - prop prop - propagate propag - propagation propag - propend propend - propension propens - proper proper - properer proper - properly properli - propertied properti - properties properti - property properti - prophecies propheci - prophecy propheci - prophesied prophesi - prophesier prophesi - prophesy prophesi - prophesying prophesi - prophet prophet - prophetess prophetess - prophetic prophet - prophetically prophet - prophets prophet - propinquity propinqu - propontic propont - proportion proport - proportionable proportion - proportions proport - propos propo - propose propos - proposed propos - proposer propos - proposes propos - proposing propos - proposition proposit - propositions proposit - propounded propound - propp propp - propre propr - propriety proprieti - props prop - propugnation propugn - prorogue prorogu - prorogued prorogu - proscription proscript - proscriptions proscript - prose prose - prosecute prosecut - prosecution prosecut - proselytes proselyt - proserpina proserpina - prosp prosp - prospect prospect - prosper prosper - prosperity prosper - prospero prospero - prosperous prosper - prosperously prosper - prospers prosper - prostitute prostitut - prostrate prostrat - protect protect - protected protect - protection protect - protector protector - protectors protector - protectorship protectorship - protectress protectress - protects protect - protest protest - protestation protest - protestations protest - protested protest - protester protest - protesting protest - protests protest - proteus proteu - protheus protheu - protract protract - protractive protract - proud proud - prouder prouder - proudest proudest - proudlier proudlier - proudly proudli - prouds proud - prov prov - provand provand - prove prove - proved prove - provender provend - proverb proverb - proverbs proverb - proves prove - proveth proveth - provide provid - provided provid - providence provid - provident provid - providently provid - provider provid - provides provid - province provinc - provinces provinc - provincial provinci - proving prove - provision provis - proviso proviso - provocation provoc - provok provok - provoke provok - provoked provok - provoker provok - provokes provok - provoketh provoketh - provoking provok - provost provost - prowess prowess - prudence prudenc - prudent prudent - prun prun - prune prune - prunes prune - pruning prune - pry pry - prying pry - psalm psalm - psalmist psalmist - psalms psalm - psalteries psalteri - ptolemies ptolemi - ptolemy ptolemi - public public - publican publican - publication public - publicly publicli - publicola publicola - publish publish - published publish - publisher publish - publishing publish - publius publiu - pucelle pucel - puck puck - pudder pudder - pudding pud - puddings pud - puddle puddl - puddled puddl - pudency pudenc - pueritia pueritia - puff puff - puffing puf - puffs puff - pugging pug - puis pui - puissance puissanc - puissant puissant - puke puke - puking puke - pulcher pulcher - puling pule - pull pull - puller puller - pullet pullet - pulling pull - pulls pull - pulpit pulpit - pulpiter pulpit - pulpits pulpit - pulse puls - pulsidge pulsidg - pump pump - pumpion pumpion - pumps pump - pun pun - punched punch - punish punish - punished punish - punishes punish - punishment punish - punishments punish - punk punk - punto punto - puny puni - pupil pupil - pupils pupil - puppet puppet - puppets puppet - puppies puppi - puppy puppi - pur pur - purblind purblind - purchas purcha - purchase purchas - purchased purchas - purchases purchas - purchaseth purchaseth - purchasing purchas - pure pure - purely pure - purer purer - purest purest - purg purg - purgation purgat - purgative purg - purgatory purgatori - purge purg - purged purg - purgers purger - purging purg - purifies purifi - purifying purifi - puritan puritan - purity puriti - purlieus purlieu - purple purpl - purpled purpl - purples purpl - purport purport - purpos purpo - purpose purpos - purposed purpos - purposely purpos - purposes purpos - purposeth purposeth - purposing purpos - purr purr - purs pur - purse purs - pursents pursent - purses purs - pursu pursu - pursue pursu - pursued pursu - pursuers pursuer - pursues pursu - pursuest pursuest - pursueth pursueth - pursuing pursu - pursuit pursuit - pursuivant pursuiv - pursuivants pursuiv - pursy pursi - purus puru - purveyor purveyor - push push - pushes push - pusillanimity pusillanim - put put - putrefy putrefi - putrified putrifi - puts put - putter putter - putting put - puttock puttock - puzzel puzzel - puzzle puzzl - puzzled puzzl - puzzles puzzl - py py - pygmalion pygmalion - pygmies pygmi - pygmy pygmi - pyramid pyramid - pyramides pyramid - pyramids pyramid - pyramis pyrami - pyramises pyramis - pyramus pyramu - pyrenean pyrenean - pyrrhus pyrrhu - pythagoras pythagora - qu qu - quadrangle quadrangl - quae quae - quaff quaff - quaffing quaf - quagmire quagmir - quail quail - quailing quail - quails quail - quaint quaint - quaintly quaintli - quak quak - quake quak - quakes quak - qualification qualif - qualified qualifi - qualifies qualifi - qualify qualifi - qualifying qualifi - qualite qualit - qualities qualiti - quality qualiti - qualm qualm - qualmish qualmish - quam quam - quand quand - quando quando - quantities quantiti - quantity quantiti - quare quar - quarrel quarrel - quarrell quarrel - quarreller quarrel - quarrelling quarrel - quarrelous quarrel - quarrels quarrel - quarrelsome quarrelsom - quarries quarri - quarry quarri - quart quart - quarter quarter - quartered quarter - quartering quarter - quarters quarter - quarts quart - quasi quasi - quat quat - quatch quatch - quay quai - que que - quean quean - queas quea - queasiness queasi - queasy queasi - queen queen - queens queen - quell quell - queller queller - quench quench - quenched quench - quenching quench - quenchless quenchless - quern quern - quest quest - questant questant - question question - questionable question - questioned question - questioning question - questionless questionless - questions question - questrists questrist - quests quest - queubus queubu - qui qui - quick quick - quicken quicken - quickens quicken - quicker quicker - quicklier quicklier - quickly quickli - quickness quick - quicksand quicksand - quicksands quicksand - quicksilverr quicksilverr - quid quid - quiddities quidditi - quiddits quiddit - quier quier - quiet quiet - quieter quieter - quietly quietli - quietness quiet - quietus quietu - quill quill - quillets quillet - quills quill - quilt quilt - quinapalus quinapalu - quince quinc - quinces quinc - quintain quintain - quintessence quintess - quintus quintu - quip quip - quips quip - quire quir - quiring quir - quirk quirk - quirks quirk - quis qui - quit quit - quite quit - quits quit - quittance quittanc - quitted quit - quitting quit - quiver quiver - quivering quiver - quivers quiver - quo quo - quod quod - quoifs quoif - quoint quoint - quoit quoit - quoits quoit - quondam quondam - quoniam quoniam - quote quot - quoted quot - quotes quot - quoth quoth - quotidian quotidian - r r - rabbit rabbit - rabble rabbl - rabblement rabblement - race race - rack rack - rackers racker - racket racket - rackets racket - racking rack - racks rack - radiance radianc - radiant radiant - radish radish - rafe rafe - raft raft - rag rag - rage rage - rages rage - rageth rageth - ragg ragg - ragged rag - raggedness ragged - raging rage - ragozine ragozin - rags rag - rah rah - rail rail - railed rail - railer railer - railest railest - raileth raileth - railing rail - rails rail - raiment raiment - rain rain - rainbow rainbow - raineth raineth - raining rain - rainold rainold - rains rain - rainy raini - rais rai - raise rais - raised rais - raises rais - raising rais - raisins raisin - rak rak - rake rake - rakers raker - rakes rake - ral ral - rald rald - ralph ralph - ram ram - rambures rambur - ramm ramm - rampallian rampallian - rampant rampant - ramping ramp - rampir rampir - ramps ramp - rams ram - ramsey ramsei - ramston ramston - ran ran - rance ranc - rancorous rancor - rancors rancor - rancour rancour - random random - rang rang - range rang - ranged rang - rangers ranger - ranges rang - ranging rang - rank rank - ranker ranker - rankest rankest - ranking rank - rankle rankl - rankly rankli - rankness rank - ranks rank - ransack ransack - ransacking ransack - ransom ransom - ransomed ransom - ransoming ransom - ransomless ransomless - ransoms ransom - rant rant - ranting rant - rap rap - rape rape - rapes rape - rapier rapier - rapiers rapier - rapine rapin - raps rap - rapt rapt - rapture raptur - raptures raptur - rar rar - rare rare - rarely rare - rareness rare - rarer rarer - rarest rarest - rarities rariti - rarity rariti - rascal rascal - rascalliest rascalliest - rascally rascal - rascals rascal - rased rase - rash rash - rasher rasher - rashly rashli - rashness rash - rat rat - ratcatcher ratcatch - ratcliff ratcliff - rate rate - rated rate - rately rate - rates rate - rather rather - ratherest ratherest - ratified ratifi - ratifiers ratifi - ratify ratifi - rating rate - rational ration - ratolorum ratolorum - rats rat - ratsbane ratsban - rattle rattl - rattles rattl - rattling rattl - rature ratur - raught raught - rav rav - rave rave - ravel ravel - raven raven - ravening raven - ravenous raven - ravens raven - ravenspurgh ravenspurgh - raves rave - ravin ravin - raving rave - ravish ravish - ravished ravish - ravisher ravish - ravishing ravish - ravishments ravish - raw raw - rawer rawer - rawly rawli - rawness raw - ray rai - rayed rai - rays rai - raz raz - raze raze - razed raze - razes raze - razeth razeth - razing raze - razor razor - razorable razor - razors razor - razure razur - re re - reach reach - reaches reach - reacheth reacheth - reaching reach - read read - reader reader - readiest readiest - readily readili - readiness readi - reading read - readins readin - reads read - ready readi - real real - really realli - realm realm - realms realm - reap reap - reapers reaper - reaping reap - reaps reap - rear rear - rears rear - rearward rearward - reason reason - reasonable reason - reasonably reason - reasoned reason - reasoning reason - reasonless reasonless - reasons reason - reave reav - rebate rebat - rebato rebato - rebeck rebeck - rebel rebel - rebell rebel - rebelling rebel - rebellion rebellion - rebellious rebelli - rebels rebel - rebound rebound - rebuk rebuk - rebuke rebuk - rebukeable rebuk - rebuked rebuk - rebukes rebuk - rebus rebu - recall recal - recant recant - recantation recant - recanter recant - recanting recant - receipt receipt - receipts receipt - receiv receiv - receive receiv - received receiv - receiver receiv - receives receiv - receivest receivest - receiveth receiveth - receiving receiv - receptacle receptacl - rechate rechat - reciprocal reciproc - reciprocally reciproc - recite recit - recited recit - reciterai reciterai - reck reck - recking reck - reckless reckless - reckon reckon - reckoned reckon - reckoning reckon - reckonings reckon - recks reck - reclaim reclaim - reclaims reclaim - reclusive reclus - recognizance recogniz - recognizances recogniz - recoil recoil - recoiling recoil - recollected recollect - recomforted recomfort - recomforture recomfortur - recommend recommend - recommended recommend - recommends recommend - recompens recompen - recompense recompens - reconcil reconcil - reconcile reconcil - reconciled reconcil - reconcilement reconcil - reconciler reconcil - reconciles reconcil - reconciliation reconcili - record record - recordation record - recorded record - recorder record - recorders record - records record - recount recount - recounted recount - recounting recount - recountments recount - recounts recount - recourse recours - recov recov - recover recov - recoverable recover - recovered recov - recoveries recoveri - recovers recov - recovery recoveri - recreant recreant - recreants recreant - recreate recreat - recreation recreat - rectify rectifi - rector rector - rectorship rectorship - recure recur - recured recur - red red - redbreast redbreast - redder redder - reddest reddest - rede rede - redeem redeem - redeemed redeem - redeemer redeem - redeeming redeem - redeems redeem - redeliver redeliv - redemption redempt - redime redim - redness red - redoubled redoubl - redoubted redoubt - redound redound - redress redress - redressed redress - redresses redress - reduce reduc - reechy reechi - reed reed - reeds reed - reek reek - reeking reek - reeks reek - reeky reeki - reel reel - reeleth reeleth - reeling reel - reels reel - refell refel - refer refer - reference refer - referr referr - referred refer - refigured refigur - refin refin - refined refin - reflect reflect - reflecting reflect - reflection reflect - reflex reflex - reform reform - reformation reform - reformed reform - refractory refractori - refrain refrain - refresh refresh - refreshing refresh - reft reft - refts reft - refuge refug - refus refu - refusal refus - refuse refus - refused refus - refusest refusest - refusing refus - reg reg - regal regal - regalia regalia - regan regan - regard regard - regardance regard - regarded regard - regardfully regardfulli - regarding regard - regards regard - regenerate regener - regent regent - regentship regentship - regia regia - regiment regiment - regiments regiment - regina regina - region region - regions region - regist regist - register regist - registers regist - regreet regreet - regreets regreet - regress regress - reguerdon reguerdon - regular regular - rehears rehear - rehearsal rehears - rehearse rehears - reign reign - reigned reign - reignier reignier - reigning reign - reigns reign - rein rein - reinforc reinforc - reinforce reinforc - reinforcement reinforc - reins rein - reiterate reiter - reject reject - rejected reject - rejoic rejoic - rejoice rejoic - rejoices rejoic - rejoiceth rejoiceth - rejoicing rejoic - rejoicingly rejoicingli - rejoindure rejoindur - rejourn rejourn - rel rel - relapse relaps - relate relat - relates relat - relation relat - relations relat - relative rel - releas relea - release releas - released releas - releasing releas - relent relent - relenting relent - relents relent - reliances relianc - relics relic - relief relief - reliev reliev - relieve reliev - relieved reliev - relieves reliev - relieving reliev - religion religion - religions religion - religious religi - religiously religi - relinquish relinquish - reliques reliqu - reliquit reliquit - relish relish - relume relum - rely reli - relying reli - remain remain - remainder remaind - remainders remaind - remained remain - remaineth remaineth - remaining remain - remains remain - remark remark - remarkable remark - remediate remedi - remedied remedi - remedies remedi - remedy remedi - rememb rememb - remember rememb - remembered rememb - remembers rememb - remembrance remembr - remembrancer remembranc - remembrances remembr - remercimens remercimen - remiss remiss - remission remiss - remissness remiss - remit remit - remnant remnant - remnants remnant - remonstrance remonstr - remorse remors - remorseful remors - remorseless remorseless - remote remot - remotion remot - remov remov - remove remov - removed remov - removedness removed - remover remov - removes remov - removing remov - remunerate remuner - remuneration remuner - rence renc - rend rend - render render - rendered render - renders render - rendezvous rendezv - renegado renegado - renege reneg - reneges reneg - renew renew - renewed renew - renewest renewest - renounce renounc - renouncement renounc - renouncing renounc - renowmed renowm - renown renown - renowned renown - rent rent - rents rent - repaid repaid - repair repair - repaired repair - repairing repair - repairs repair - repass repass - repast repast - repasture repastur - repay repai - repaying repai - repays repai - repeal repeal - repealing repeal - repeals repeal - repeat repeat - repeated repeat - repeating repeat - repeats repeat - repel repel - repent repent - repentance repent - repentant repent - repented repent - repenting repent - repents repent - repetition repetit - repetitions repetit - repin repin - repine repin - repining repin - replant replant - replenish replenish - replenished replenish - replete replet - replication replic - replied repli - replies repli - repliest repliest - reply repli - replying repli - report report - reported report - reporter report - reportest reportest - reporting report - reportingly reportingli - reports report - reposal repos - repose repos - reposeth reposeth - reposing repos - repossess repossess - reprehend reprehend - reprehended reprehend - reprehending reprehend - represent repres - representing repres - reprieve repriev - reprieves repriev - reprisal repris - reproach reproach - reproaches reproach - reproachful reproach - reproachfully reproachfulli - reprobate reprob - reprobation reprob - reproof reproof - reprov reprov - reprove reprov - reproveable reprov - reproves reprov - reproving reprov - repugn repugn - repugnancy repugn - repugnant repugn - repulse repuls - repulsed repuls - repurchas repurcha - repured repur - reputation reput - repute reput - reputed reput - reputeless reputeless - reputes reput - reputing reput - request request - requested request - requesting request - requests request - requiem requiem - requir requir - require requir - required requir - requires requir - requireth requireth - requiring requir - requisite requisit - requisites requisit - requit requit - requital requit - requite requit - requited requit - requites requit - rer rer - rere rere - rers rer - rescu rescu - rescue rescu - rescued rescu - rescues rescu - rescuing rescu - resemblance resembl - resemble resembl - resembled resembl - resembles resembl - resembleth resembleth - resembling resembl - reserv reserv - reservation reserv - reserve reserv - reserved reserv - reserves reserv - reside resid - residence resid - resident resid - resides resid - residing resid - residue residu - resign resign - resignation resign - resist resist - resistance resist - resisted resist - resisting resist - resists resist - resolute resolut - resolutely resolut - resolutes resolut - resolution resolut - resolv resolv - resolve resolv - resolved resolv - resolvedly resolvedli - resolves resolv - resolveth resolveth - resort resort - resorted resort - resounding resound - resounds resound - respeaking respeak - respect respect - respected respect - respecting respect - respective respect - respectively respect - respects respect - respice respic - respite respit - respites respit - responsive respons - respose respos - ress ress - rest rest - rested rest - resteth resteth - restful rest - resting rest - restitution restitut - restless restless - restor restor - restoration restor - restorative restor - restore restor - restored restor - restores restor - restoring restor - restrain restrain - restrained restrain - restraining restrain - restrains restrain - restraint restraint - rests rest - resty resti - resum resum - resume resum - resumes resum - resurrections resurrect - retail retail - retails retail - retain retain - retainers retain - retaining retain - retell retel - retention retent - retentive retent - retinue retinu - retir retir - retire retir - retired retir - retirement retir - retires retir - retiring retir - retold retold - retort retort - retorts retort - retourne retourn - retract retract - retreat retreat - retrograde retrograd - rets ret - return return - returned return - returnest returnest - returneth returneth - returning return - returns return - revania revania - reveal reveal - reveals reveal - revel revel - reveler revel - revell revel - reveller revel - revellers revel - revelling revel - revelry revelri - revels revel - reveng reveng - revenge reveng - revenged reveng - revengeful reveng - revengement reveng - revenger reveng - revengers reveng - revenges reveng - revenging reveng - revengingly revengingli - revenue revenu - revenues revenu - reverb reverb - reverberate reverber - reverbs reverb - reverenc reverenc - reverence rever - reverend reverend - reverent rever - reverently rever - revers rever - reverse revers - reversion revers - reverted revert - review review - reviewest reviewest - revil revil - revile revil - revisits revisit - reviv reviv - revive reviv - revives reviv - reviving reviv - revok revok - revoke revok - revokement revok - revolt revolt - revolted revolt - revolting revolt - revolts revolt - revolution revolut - revolutions revolut - revolve revolv - revolving revolv - reward reward - rewarded reward - rewarder reward - rewarding reward - rewards reward - reword reword - reworded reword - rex rex - rey rei - reynaldo reynaldo - rford rford - rful rful - rfull rfull - rhapsody rhapsodi - rheims rheim - rhenish rhenish - rhesus rhesu - rhetoric rhetor - rheum rheum - rheumatic rheumat - rheums rheum - rheumy rheumi - rhinoceros rhinocero - rhodes rhode - rhodope rhodop - rhubarb rhubarb - rhym rhym - rhyme rhyme - rhymers rhymer - rhymes rhyme - rhyming rhyme - rialto rialto - rib rib - ribald ribald - riband riband - ribands riband - ribaudred ribaudr - ribb ribb - ribbed rib - ribbon ribbon - ribbons ribbon - ribs rib - rice rice - rich rich - richard richard - richer richer - riches rich - richest richest - richly richli - richmond richmond - richmonds richmond - rid rid - riddance riddanc - ridden ridden - riddle riddl - riddles riddl - riddling riddl - ride ride - rider rider - riders rider - rides ride - ridest ridest - rideth rideth - ridge ridg - ridges ridg - ridiculous ridicul - riding ride - rids rid - rien rien - ries ri - rifle rifl - rift rift - rifted rift - rig rig - rigg rigg - riggish riggish - right right - righteous righteou - righteously righteous - rightful right - rightfully rightfulli - rightly rightli - rights right - rigol rigol - rigorous rigor - rigorously rigor - rigour rigour - ril ril - rim rim - rin rin - rinaldo rinaldo - rind rind - ring ring - ringing ring - ringleader ringlead - ringlets ringlet - rings ring - ringwood ringwood - riot riot - rioter rioter - rioting riot - riotous riotou - riots riot - rip rip - ripe ripe - ripely ripe - ripen ripen - ripened ripen - ripeness ripe - ripening ripen - ripens ripen - riper riper - ripest ripest - riping ripe - ripp ripp - ripping rip - rise rise - risen risen - rises rise - riseth riseth - rish rish - rising rise - rite rite - rites rite - rivage rivag - rival rival - rivality rival - rivall rival - rivals rival - rive rive - rived rive - rivelled rivel - river river - rivers river - rivet rivet - riveted rivet - rivets rivet - rivo rivo - rj rj - rless rless - road road - roads road - roam roam - roaming roam - roan roan - roar roar - roared roar - roarers roarer - roaring roar - roars roar - roast roast - roasted roast - rob rob - roba roba - robas roba - robb robb - robbed rob - robber robber - robbers robber - robbery robberi - robbing rob - robe robe - robed robe - robert robert - robes robe - robin robin - robs rob - robustious robusti - rochester rochest - rochford rochford - rock rock - rocks rock - rocky rocki - rod rod - rode rode - roderigo roderigo - rods rod - roe roe - roes roe - roger roger - rogero rogero - rogue rogu - roguery rogueri - rogues rogu - roguish roguish - roi roi - roisting roist - roll roll - rolled roll - rolling roll - rolls roll - rom rom - romage romag - roman roman - romano romano - romanos romano - romans roman - rome rome - romeo romeo - romish romish - rondure rondur - ronyon ronyon - rood rood - roof roof - roofs roof - rook rook - rooks rook - rooky rooki - room room - rooms room - root root - rooted root - rootedly rootedli - rooteth rooteth - rooting root - roots root - rope rope - ropery roperi - ropes rope - roping rope - ros ro - rosalind rosalind - rosalinda rosalinda - rosalinde rosalind - rosaline rosalin - roscius rosciu - rose rose - rosed rose - rosemary rosemari - rosencrantz rosencrantz - roses rose - ross ross - rosy rosi - rot rot - rote rote - roted rote - rother rother - rotherham rotherham - rots rot - rotted rot - rotten rotten - rottenness rotten - rotting rot - rotundity rotund - rouen rouen - rough rough - rougher rougher - roughest roughest - roughly roughli - roughness rough - round round - rounded round - roundel roundel - rounder rounder - roundest roundest - rounding round - roundly roundli - rounds round - roundure roundur - rous rou - rouse rous - roused rous - rousillon rousillon - rously rousli - roussi roussi - rout rout - routed rout - routs rout - rove rove - rover rover - row row - rowel rowel - rowland rowland - rowlands rowland - roy roi - royal royal - royalize royal - royally royal - royalties royalti - royalty royalti - roynish roynish - rs rs - rt rt - rub rub - rubb rubb - rubbing rub - rubbish rubbish - rubies rubi - rubious rubiou - rubs rub - ruby rubi - rud rud - rudand rudand - rudder rudder - ruddiness ruddi - ruddock ruddock - ruddy ruddi - rude rude - rudely rude - rudeness rude - ruder ruder - rudesby rudesbi - rudest rudest - rudiments rudiment - rue rue - rued ru - ruff ruff - ruffian ruffian - ruffians ruffian - ruffle ruffl - ruffling ruffl - ruffs ruff - rug rug - rugby rugbi - rugemount rugemount - rugged rug - ruin ruin - ruinate ruinat - ruined ruin - ruining ruin - ruinous ruinou - ruins ruin - rul rul - rule rule - ruled rule - ruler ruler - rulers ruler - rules rule - ruling rule - rumble rumbl - ruminaies ruminai - ruminat ruminat - ruminate rumin - ruminated rumin - ruminates rumin - rumination rumin - rumor rumor - rumour rumour - rumourer rumour - rumours rumour - rump rump - run run - runagate runag - runagates runag - runaway runawai - runaways runawai - rung rung - runn runn - runner runner - runners runner - running run - runs run - rupture ruptur - ruptures ruptur - rural rural - rush rush - rushes rush - rushing rush - rushling rushl - rushy rushi - russet russet - russia russia - russian russian - russians russian - rust rust - rusted rust - rustic rustic - rustically rustic - rustics rustic - rustle rustl - rustling rustl - rusts rust - rusty rusti - rut rut - ruth ruth - ruthful ruth - ruthless ruthless - rutland rutland - ruttish ruttish - ry ry - rye rye - rything ryth - s s - sa sa - saba saba - sabbath sabbath - sable sabl - sables sabl - sack sack - sackbuts sackbut - sackcloth sackcloth - sacked sack - sackerson sackerson - sacks sack - sacrament sacrament - sacred sacr - sacrific sacrif - sacrifice sacrific - sacrificers sacrific - sacrifices sacrific - sacrificial sacrifici - sacrificing sacrif - sacrilegious sacrilegi - sacring sacr - sad sad - sadder sadder - saddest saddest - saddle saddl - saddler saddler - saddles saddl - sadly sadli - sadness sad - saf saf - safe safe - safeguard safeguard - safely safe - safer safer - safest safest - safeties safeti - safety safeti - saffron saffron - sag sag - sage sage - sagittary sagittari - said said - saidst saidst - sail sail - sailing sail - sailmaker sailmak - sailor sailor - sailors sailor - sails sail - sain sain - saint saint - sainted saint - saintlike saintlik - saints saint - saith saith - sake sake - sakes sake - sala sala - salad salad - salamander salamand - salary salari - sale sale - salerio salerio - salicam salicam - salique saliqu - salisbury salisburi - sall sall - sallet sallet - sallets sallet - sallies salli - sallow sallow - sally salli - salmon salmon - salmons salmon - salt salt - salter salter - saltiers saltier - saltness salt - saltpetre saltpetr - salutation salut - salutations salut - salute salut - saluted salut - salutes salut - saluteth saluteth - salv salv - salvation salvat - salve salv - salving salv - same same - samingo samingo - samp samp - sampire sampir - sample sampl - sampler sampler - sampson sampson - samson samson - samsons samson - sancta sancta - sanctified sanctifi - sanctifies sanctifi - sanctify sanctifi - sanctimonies sanctimoni - sanctimonious sanctimoni - sanctimony sanctimoni - sanctities sanctiti - sanctity sanctiti - sanctuarize sanctuar - sanctuary sanctuari - sand sand - sandal sandal - sandbag sandbag - sanded sand - sands sand - sandy sandi - sandys sandi - sang sang - sanguine sanguin - sanguis sangui - sanity saniti - sans san - santrailles santrail - sap sap - sapient sapient - sapit sapit - sapless sapless - sapling sapl - sapphire sapphir - sapphires sapphir - saracens saracen - sarcenet sarcenet - sard sard - sardians sardian - sardinia sardinia - sardis sardi - sarum sarum - sat sat - satan satan - satchel satchel - sate sate - sated sate - satiate satiat - satiety satieti - satin satin - satire satir - satirical satir - satis sati - satisfaction satisfact - satisfied satisfi - satisfies satisfi - satisfy satisfi - satisfying satisfi - saturday saturdai - saturdays saturdai - saturn saturn - saturnine saturnin - saturninus saturninu - satyr satyr - satyrs satyr - sauc sauc - sauce sauc - sauced sauc - saucers saucer - sauces sauc - saucily saucili - sauciness sauci - saucy sauci - sauf sauf - saunder saunder - sav sav - savage savag - savagely savag - savageness savag - savagery savageri - savages savag - save save - saved save - saves save - saving save - saviour saviour - savory savori - savour savour - savouring savour - savours savour - savoury savouri - savoy savoi - saw saw - sawed saw - sawest sawest - sawn sawn - sawpit sawpit - saws saw - sawyer sawyer - saxons saxon - saxony saxoni - saxton saxton - say sai - sayest sayest - saying sai - sayings sai - says sai - sayst sayst - sblood sblood - sc sc - scab scab - scabbard scabbard - scabs scab - scaffold scaffold - scaffoldage scaffoldag - scal scal - scald scald - scalded scald - scalding scald - scale scale - scaled scale - scales scale - scaling scale - scall scall - scalp scalp - scalps scalp - scaly scali - scamble scambl - scambling scambl - scamels scamel - scan scan - scandal scandal - scandaliz scandaliz - scandalous scandal - scandy scandi - scann scann - scant scant - scanted scant - scanter scanter - scanting scant - scantling scantl - scants scant - scap scap - scape scape - scaped scape - scapes scape - scapeth scapeth - scar scar - scarce scarc - scarcely scarc - scarcity scarciti - scare scare - scarecrow scarecrow - scarecrows scarecrow - scarf scarf - scarfed scarf - scarfs scarf - scaring scare - scarlet scarlet - scarr scarr - scarre scarr - scars scar - scarus scaru - scath scath - scathe scath - scathful scath - scatt scatt - scatter scatter - scattered scatter - scattering scatter - scatters scatter - scelera scelera - scelerisque scelerisqu - scene scene - scenes scene - scent scent - scented scent - scept scept - scepter scepter - sceptre sceptr - sceptred sceptr - sceptres sceptr - schedule schedul - schedules schedul - scholar scholar - scholarly scholarli - scholars scholar - school school - schoolboy schoolboi - schoolboys schoolboi - schoolfellows schoolfellow - schooling school - schoolmaster schoolmast - schoolmasters schoolmast - schools school - sciatica sciatica - sciaticas sciatica - science scienc - sciences scienc - scimitar scimitar - scion scion - scions scion - scissors scissor - scoff scoff - scoffer scoffer - scoffing scof - scoffs scoff - scoggin scoggin - scold scold - scolding scold - scolds scold - sconce sconc - scone scone - scope scope - scopes scope - scorch scorch - scorched scorch - score score - scored score - scores score - scoring score - scorn scorn - scorned scorn - scornful scorn - scornfully scornfulli - scorning scorn - scorns scorn - scorpion scorpion - scorpions scorpion - scot scot - scotch scotch - scotches scotch - scotland scotland - scots scot - scottish scottish - scoundrels scoundrel - scour scour - scoured scour - scourg scourg - scourge scourg - scouring scour - scout scout - scouts scout - scowl scowl - scrap scrap - scrape scrape - scraping scrape - scraps scrap - scratch scratch - scratches scratch - scratching scratch - scream scream - screams scream - screech screech - screeching screech - screen screen - screens screen - screw screw - screws screw - scribbl scribbl - scribbled scribbl - scribe scribe - scribes scribe - scrimers scrimer - scrip scrip - scrippage scrippag - scripture scriptur - scriptures scriptur - scrivener scriven - scroll scroll - scrolls scroll - scroop scroop - scrowl scrowl - scroyles scroyl - scrubbed scrub - scruple scrupl - scruples scrupl - scrupulous scrupul - scuffles scuffl - scuffling scuffl - scullion scullion - sculls scull - scum scum - scurril scurril - scurrility scurril - scurrilous scurril - scurvy scurvi - scuse scuse - scut scut - scutcheon scutcheon - scutcheons scutcheon - scylla scylla - scythe scyth - scythed scyth - scythia scythia - scythian scythian - sdeath sdeath - se se - sea sea - seacoal seacoal - seafaring seafar - seal seal - sealed seal - sealing seal - seals seal - seam seam - seamen seamen - seamy seami - seaport seaport - sear sear - searce searc - search search - searchers searcher - searches search - searcheth searcheth - searching search - seared sear - seas sea - seasick seasick - seaside seasid - season season - seasoned season - seasons season - seat seat - seated seat - seats seat - sebastian sebastian - second second - secondarily secondarili - secondary secondari - seconded second - seconds second - secrecy secreci - secret secret - secretaries secretari - secretary secretari - secretly secretli - secrets secret - sect sect - sectary sectari - sects sect - secundo secundo - secure secur - securely secur - securing secur - security secur - sedg sedg - sedge sedg - sedges sedg - sedgy sedgi - sedition sedit - seditious sediti - seduc seduc - seduce seduc - seduced seduc - seducer seduc - seducing seduc - see see - seed seed - seeded seed - seedness seed - seeds seed - seedsman seedsman - seein seein - seeing see - seek seek - seeking seek - seeks seek - seel seel - seeling seel - seely seeli - seem seem - seemed seem - seemers seemer - seemest seemest - seemeth seemeth - seeming seem - seemingly seemingli - seemly seemli - seems seem - seen seen - seer seer - sees see - seese sees - seest seest - seethe seeth - seethes seeth - seething seeth - seeting seet - segregation segreg - seigneur seigneur - seigneurs seigneur - seiz seiz - seize seiz - seized seiz - seizes seiz - seizeth seizeth - seizing seiz - seizure seizur - seld seld - seldom seldom - select select - seleucus seleucu - self self - selfsame selfsam - sell sell - seller seller - selling sell - sells sell - selves selv - semblable semblabl - semblably semblabl - semblance semblanc - semblances semblanc - semblative sembl - semi semi - semicircle semicircl - semiramis semirami - semper semper - sempronius semproniu - senate senat - senator senat - senators senat - send send - sender sender - sendeth sendeth - sending send - sends send - seneca seneca - senior senior - seniory seniori - senis seni - sennet sennet - senoys senoi - sense sens - senseless senseless - senses sens - sensible sensibl - sensibly sensibl - sensual sensual - sensuality sensual - sent sent - sentenc sentenc - sentence sentenc - sentences sentenc - sententious sententi - sentinel sentinel - sentinels sentinel - separable separ - separate separ - separated separ - separates separ - separation separ - septentrion septentrion - sepulchre sepulchr - sepulchres sepulchr - sepulchring sepulchr - sequel sequel - sequence sequenc - sequent sequent - sequest sequest - sequester sequest - sequestration sequestr - sere sere - serenis sereni - serge serg - sergeant sergeant - serious seriou - seriously serious - sermon sermon - sermons sermon - serpent serpent - serpentine serpentin - serpents serpent - serpigo serpigo - serv serv - servant servant - servanted servant - servants servant - serve serv - served serv - server server - serves serv - serveth serveth - service servic - serviceable servic - services servic - servile servil - servility servil - servilius serviliu - serving serv - servingman servingman - servingmen servingmen - serviteur serviteur - servitor servitor - servitors servitor - servitude servitud - sessa sessa - session session - sessions session - sestos sesto - set set - setebos setebo - sets set - setter setter - setting set - settle settl - settled settl - settlest settlest - settling settl - sev sev - seven seven - sevenfold sevenfold - sevennight sevennight - seventeen seventeen - seventh seventh - seventy seventi - sever sever - several sever - severally sever - severals sever - severe sever - severed sever - severely sever - severest severest - severing sever - severity sever - severn severn - severs sever - sew sew - seward seward - sewer sewer - sewing sew - sex sex - sexes sex - sexton sexton - sextus sextu - seymour seymour - seyton seyton - sfoot sfoot - sh sh - shackle shackl - shackles shackl - shade shade - shades shade - shadow shadow - shadowed shadow - shadowing shadow - shadows shadow - shadowy shadowi - shady shadi - shafalus shafalu - shaft shaft - shafts shaft - shag shag - shak shak - shake shake - shaked shake - shaken shaken - shakes shake - shaking shake - shales shale - shall shall - shallenge shalleng - shallow shallow - shallowest shallowest - shallowly shallowli - shallows shallow - shalt shalt - sham sham - shambles shambl - shame shame - shamed shame - shameful shame - shamefully shamefulli - shameless shameless - shames shame - shamest shamest - shaming shame - shank shank - shanks shank - shap shap - shape shape - shaped shape - shapeless shapeless - shapen shapen - shapes shape - shaping shape - shar shar - shard shard - sharded shard - shards shard - share share - shared share - sharers sharer - shares share - sharing share - shark shark - sharp sharp - sharpen sharpen - sharpened sharpen - sharpens sharpen - sharper sharper - sharpest sharpest - sharply sharpli - sharpness sharp - sharps sharp - shatter shatter - shav shav - shave shave - shaven shaven - shaw shaw - she she - sheaf sheaf - sheal sheal - shear shear - shearers shearer - shearing shear - shearman shearman - shears shear - sheath sheath - sheathe sheath - sheathed sheath - sheathes sheath - sheathing sheath - sheaved sheav - sheaves sheav - shed shed - shedding shed - sheds shed - sheen sheen - sheep sheep - sheepcote sheepcot - sheepcotes sheepcot - sheeps sheep - sheepskins sheepskin - sheer sheer - sheet sheet - sheeted sheet - sheets sheet - sheffield sheffield - shelf shelf - shell shell - shells shell - shelt shelt - shelter shelter - shelters shelter - shelves shelv - shelving shelv - shelvy shelvi - shent shent - shepherd shepherd - shepherdes shepherd - shepherdess shepherdess - shepherdesses shepherdess - shepherds shepherd - sher sher - sheriff sheriff - sherris sherri - shes she - sheweth sheweth - shield shield - shielded shield - shields shield - shift shift - shifted shift - shifting shift - shifts shift - shilling shill - shillings shill - shin shin - shine shine - shines shine - shineth shineth - shining shine - shins shin - shiny shini - ship ship - shipboard shipboard - shipman shipman - shipmaster shipmast - shipmen shipmen - shipp shipp - shipped ship - shipping ship - ships ship - shipt shipt - shipwreck shipwreck - shipwrecking shipwreck - shipwright shipwright - shipwrights shipwright - shire shire - shirley shirlei - shirt shirt - shirts shirt - shive shive - shiver shiver - shivering shiver - shivers shiver - shoal shoal - shoals shoal - shock shock - shocks shock - shod shod - shoe shoe - shoeing shoe - shoemaker shoemak - shoes shoe - shog shog - shone shone - shook shook - shoon shoon - shoot shoot - shooter shooter - shootie shooti - shooting shoot - shoots shoot - shop shop - shops shop - shore shore - shores shore - shorn shorn - short short - shortcake shortcak - shorten shorten - shortened shorten - shortens shorten - shorter shorter - shortly shortli - shortness short - shot shot - shotten shotten - shoughs shough - should should - shoulder shoulder - shouldering shoulder - shoulders shoulder - shouldst shouldst - shout shout - shouted shout - shouting shout - shouts shout - shov shov - shove shove - shovel shovel - shovels shovel - show show - showed show - shower shower - showers shower - showest showest - showing show - shown shown - shows show - shreds shred - shrew shrew - shrewd shrewd - shrewdly shrewdli - shrewdness shrewd - shrewish shrewish - shrewishly shrewishli - shrewishness shrewish - shrews shrew - shrewsbury shrewsburi - shriek shriek - shrieking shriek - shrieks shriek - shrieve shriev - shrift shrift - shrill shrill - shriller shriller - shrills shrill - shrilly shrilli - shrimp shrimp - shrine shrine - shrink shrink - shrinking shrink - shrinks shrink - shriv shriv - shrive shrive - shriver shriver - shrives shrive - shriving shrive - shroud shroud - shrouded shroud - shrouding shroud - shrouds shroud - shrove shrove - shrow shrow - shrows shrow - shrub shrub - shrubs shrub - shrug shrug - shrugs shrug - shrunk shrunk - shudd shudd - shudders shudder - shuffl shuffl - shuffle shuffl - shuffled shuffl - shuffling shuffl - shun shun - shunless shunless - shunn shunn - shunned shun - shunning shun - shuns shun - shut shut - shuts shut - shuttle shuttl - shy shy - shylock shylock - si si - sibyl sibyl - sibylla sibylla - sibyls sibyl - sicil sicil - sicilia sicilia - sicilian sicilian - sicilius siciliu - sicils sicil - sicily sicili - sicinius siciniu - sick sick - sicken sicken - sickens sicken - sicker sicker - sickle sickl - sicklemen sicklemen - sicklied sickli - sickliness sickli - sickly sickli - sickness sick - sicles sicl - sicyon sicyon - side side - sided side - sides side - siege sieg - sieges sieg - sienna sienna - sies si - sieve siev - sift sift - sifted sift - sigeia sigeia - sigh sigh - sighed sigh - sighing sigh - sighs sigh - sight sight - sighted sight - sightless sightless - sightly sightli - sights sight - sign sign - signal signal - signet signet - signieur signieur - significant signific - significants signific - signified signifi - signifies signifi - signify signifi - signifying signifi - signior signior - signiories signiori - signiors signior - signiory signiori - signor signor - signories signori - signs sign - signum signum - silenc silenc - silence silenc - silenced silenc - silencing silenc - silent silent - silently silent - silius siliu - silk silk - silken silken - silkman silkman - silks silk - silliest silliest - silliness silli - silling sill - silly silli - silva silva - silver silver - silvered silver - silverly silverli - silvia silvia - silvius silviu - sima sima - simile simil - similes simil - simois simoi - simon simon - simony simoni - simp simp - simpcox simpcox - simple simpl - simpleness simpl - simpler simpler - simples simpl - simplicity simplic - simply simpli - simular simular - simulation simul - sin sin - since sinc - sincere sincer - sincerely sincer - sincerity sincer - sinel sinel - sinew sinew - sinewed sinew - sinews sinew - sinewy sinewi - sinful sin - sinfully sinfulli - sing sing - singe sing - singeing sing - singer singer - singes sing - singeth singeth - singing sing - single singl - singled singl - singleness singl - singly singli - sings sing - singular singular - singulariter singularit - singularities singular - singularity singular - singuled singul - sinister sinist - sink sink - sinking sink - sinks sink - sinn sinn - sinner sinner - sinners sinner - sinning sin - sinon sinon - sins sin - sip sip - sipping sip - sir sir - sire sire - siren siren - sirrah sirrah - sirs sir - sist sist - sister sister - sisterhood sisterhood - sisterly sisterli - sisters sister - sit sit - sith sith - sithence sithenc - sits sit - sitting sit - situate situat - situation situat - situations situat - siward siward - six six - sixpence sixpenc - sixpences sixpenc - sixpenny sixpenni - sixteen sixteen - sixth sixth - sixty sixti - siz siz - size size - sizes size - sizzle sizzl - skains skain - skamble skambl - skein skein - skelter skelter - skies ski - skilful skil - skilfully skilfulli - skill skill - skilless skilless - skillet skillet - skillful skill - skills skill - skim skim - skimble skimbl - skin skin - skinker skinker - skinny skinni - skins skin - skip skip - skipp skipp - skipper skipper - skipping skip - skirmish skirmish - skirmishes skirmish - skirr skirr - skirted skirt - skirts skirt - skittish skittish - skulking skulk - skull skull - skulls skull - sky sky - skyey skyei - skyish skyish - slab slab - slack slack - slackly slackli - slackness slack - slain slain - slake slake - sland sland - slander slander - slandered slander - slanderer slander - slanderers slander - slandering slander - slanderous slander - slanders slander - slash slash - slaught slaught - slaughter slaughter - slaughtered slaughter - slaughterer slaughter - slaughterman slaughterman - slaughtermen slaughtermen - slaughterous slaughter - slaughters slaughter - slave slave - slaver slaver - slavery slaveri - slaves slave - slavish slavish - slay slai - slayeth slayeth - slaying slai - slays slai - sleave sleav - sledded sled - sleek sleek - sleekly sleekli - sleep sleep - sleeper sleeper - sleepers sleeper - sleepest sleepest - sleeping sleep - sleeps sleep - sleepy sleepi - sleeve sleev - sleeves sleev - sleid sleid - sleided sleid - sleight sleight - sleights sleight - slender slender - slenderer slender - slenderly slenderli - slept slept - slew slew - slewest slewest - slice slice - slid slid - slide slide - slides slide - sliding slide - slight slight - slighted slight - slightest slightest - slightly slightli - slightness slight - slights slight - slily slili - slime slime - slimy slimi - slings sling - slink slink - slip slip - slipp slipp - slipper slipper - slippers slipper - slippery slipperi - slips slip - slish slish - slit slit - sliver sliver - slobb slobb - slomber slomber - slop slop - slope slope - slops slop - sloth sloth - slothful sloth - slough slough - slovenly slovenli - slovenry slovenri - slow slow - slower slower - slowly slowli - slowness slow - slubber slubber - slug slug - sluggard sluggard - sluggardiz sluggardiz - sluggish sluggish - sluic sluic - slumb slumb - slumber slumber - slumbers slumber - slumbery slumberi - slunk slunk - slut slut - sluts slut - sluttery slutteri - sluttish sluttish - sluttishness sluttish - sly sly - slys sly - smack smack - smacking smack - smacks smack - small small - smaller smaller - smallest smallest - smallness small - smalus smalu - smart smart - smarting smart - smartly smartli - smatch smatch - smatter smatter - smear smear - smell smell - smelling smell - smells smell - smelt smelt - smil smil - smile smile - smiled smile - smiles smile - smilest smilest - smilets smilet - smiling smile - smilingly smilingli - smirch smirch - smirched smirch - smit smit - smite smite - smites smite - smith smith - smithfield smithfield - smock smock - smocks smock - smok smok - smoke smoke - smoked smoke - smokes smoke - smoking smoke - smoky smoki - smooth smooth - smoothed smooth - smoothing smooth - smoothly smoothli - smoothness smooth - smooths smooth - smote smote - smoth smoth - smother smother - smothered smother - smothering smother - smug smug - smulkin smulkin - smutch smutch - snaffle snaffl - snail snail - snails snail - snake snake - snakes snake - snaky snaki - snap snap - snapp snapp - snapper snapper - snar snar - snare snare - snares snare - snarl snarl - snarleth snarleth - snarling snarl - snatch snatch - snatchers snatcher - snatches snatch - snatching snatch - sneak sneak - sneaking sneak - sneap sneap - sneaping sneap - sneck sneck - snip snip - snipe snipe - snipt snipt - snore snore - snores snore - snoring snore - snorting snort - snout snout - snow snow - snowballs snowbal - snowed snow - snowy snowi - snuff snuff - snuffs snuff - snug snug - so so - soak soak - soaking soak - soaks soak - soar soar - soaring soar - soars soar - sob sob - sobbing sob - sober sober - soberly soberli - sobriety sobrieti - sobs sob - sociable sociabl - societies societi - society societi - socks sock - socrates socrat - sod sod - sodden sodden - soe soe - soever soever - soft soft - soften soften - softens soften - softer softer - softest softest - softly softli - softness soft - soil soil - soiled soil - soilure soilur - soit soit - sojourn sojourn - sol sol - sola sola - solace solac - solanio solanio - sold sold - soldat soldat - solder solder - soldest soldest - soldier soldier - soldiers soldier - soldiership soldiership - sole sole - solely sole - solem solem - solemn solemn - solemness solem - solemnities solemn - solemnity solemn - solemniz solemniz - solemnize solemn - solemnized solemn - solemnly solemnli - soles sole - solicit solicit - solicitation solicit - solicited solicit - soliciting solicit - solicitings solicit - solicitor solicitor - solicits solicit - solid solid - solidares solidar - solidity solid - solinus solinu - solitary solitari - solomon solomon - solon solon - solum solum - solus solu - solyman solyman - some some - somebody somebodi - someone someon - somerset somerset - somerville somervil - something someth - sometime sometim - sometimes sometim - somever somev - somewhat somewhat - somewhere somewher - somewhither somewhith - somme somm - son son - sonance sonanc - song song - songs song - sonnet sonnet - sonneting sonnet - sonnets sonnet - sons son - sont sont - sonties sonti - soon soon - sooner sooner - soonest soonest - sooth sooth - soothe sooth - soothers soother - soothing sooth - soothsay soothsai - soothsayer soothsay - sooty sooti - sop sop - sophister sophist - sophisticated sophist - sophy sophi - sops sop - sorcerer sorcer - sorcerers sorcer - sorceress sorceress - sorceries sorceri - sorcery sorceri - sore sore - sorel sorel - sorely sore - sorer sorer - sores sore - sorrier sorrier - sorriest sorriest - sorrow sorrow - sorrowed sorrow - sorrowest sorrowest - sorrowful sorrow - sorrowing sorrow - sorrows sorrow - sorry sorri - sort sort - sortance sortanc - sorted sort - sorting sort - sorts sort - sossius sossiu - sot sot - soto soto - sots sot - sottish sottish - soud soud - sought sought - soul soul - sould sould - soulless soulless - souls soul - sound sound - sounded sound - sounder sounder - soundest soundest - sounding sound - soundless soundless - soundly soundli - soundness sound - soundpost soundpost - sounds sound - sour sour - source sourc - sources sourc - sourest sourest - sourly sourli - sours sour - sous sou - souse sous - south south - southam southam - southampton southampton - southerly southerli - southern southern - southward southward - southwark southwark - southwell southwel - souviendrai souviendrai - sov sov - sovereign sovereign - sovereignest sovereignest - sovereignly sovereignli - sovereignty sovereignti - sovereignvours sovereignvour - sow sow - sowing sow - sowl sowl - sowter sowter - space space - spaces space - spacious spaciou - spade spade - spades spade - spain spain - spak spak - spake spake - spakest spakest - span span - spangle spangl - spangled spangl - spaniard spaniard - spaniel spaniel - spaniels spaniel - spanish spanish - spann spann - spans span - spar spar - spare spare - spares spare - sparing spare - sparingly sparingli - spark spark - sparkle sparkl - sparkles sparkl - sparkling sparkl - sparks spark - sparrow sparrow - sparrows sparrow - sparta sparta - spartan spartan - spavin spavin - spavins spavin - spawn spawn - speak speak - speaker speaker - speakers speaker - speakest speakest - speaketh speaketh - speaking speak - speaks speak - spear spear - speargrass speargrass - spears spear - special special - specialities special - specially special - specialties specialti - specialty specialti - specify specifi - speciously specious - spectacle spectacl - spectacled spectacl - spectacles spectacl - spectators spectat - spectatorship spectatorship - speculation specul - speculations specul - speculative specul - sped sped - speech speech - speeches speech - speechless speechless - speed speed - speeded speed - speedier speedier - speediest speediest - speedily speedili - speediness speedi - speeding speed - speeds speed - speedy speedi - speens speen - spell spell - spelling spell - spells spell - spelt spelt - spencer spencer - spend spend - spendest spendest - spending spend - spends spend - spendthrift spendthrift - spent spent - sperato sperato - sperm sperm - spero spero - sperr sperr - spher spher - sphere sphere - sphered sphere - spheres sphere - spherical spheric - sphery spheri - sphinx sphinx - spice spice - spiced spice - spicery spiceri - spices spice - spider spider - spiders spider - spied spi - spies spi - spieth spieth - spightfully spightfulli - spigot spigot - spill spill - spilling spill - spills spill - spilt spilt - spilth spilth - spin spin - spinii spinii - spinners spinner - spinster spinster - spinsters spinster - spire spire - spirit spirit - spirited spirit - spiritless spiritless - spirits spirit - spiritual spiritu - spiritualty spiritualti - spirt spirt - spit spit - spital spital - spite spite - spited spite - spiteful spite - spites spite - spits spit - spitted spit - spitting spit - splay splai - spleen spleen - spleenful spleen - spleens spleen - spleeny spleeni - splendour splendour - splenitive splenit - splinter splinter - splinters splinter - split split - splits split - splitted split - splitting split - spoil spoil - spoils spoil - spok spok - spoke spoke - spoken spoken - spokes spoke - spokesman spokesman - sponge spong - spongy spongi - spoon spoon - spoons spoon - sport sport - sportful sport - sporting sport - sportive sportiv - sports sport - spot spot - spotless spotless - spots spot - spotted spot - spousal spousal - spouse spous - spout spout - spouting spout - spouts spout - sprag sprag - sprang sprang - sprat sprat - sprawl sprawl - spray sprai - sprays sprai - spread spread - spreading spread - spreads spread - sprighted spright - sprightful spright - sprightly sprightli - sprigs sprig - spring spring - springe spring - springes spring - springeth springeth - springhalt springhalt - springing spring - springs spring - springtime springtim - sprinkle sprinkl - sprinkles sprinkl - sprite sprite - sprited sprite - spritely sprite - sprites sprite - spriting sprite - sprout sprout - spruce spruce - sprung sprung - spun spun - spur spur - spurio spurio - spurn spurn - spurns spurn - spurr spurr - spurrer spurrer - spurring spur - spurs spur - spy spy - spying spy - squabble squabbl - squadron squadron - squadrons squadron - squand squand - squar squar - square squar - squarer squarer - squares squar - squash squash - squeak squeak - squeaking squeak - squeal squeal - squealing squeal - squeezes squeez - squeezing squeez - squele squel - squier squier - squints squint - squiny squini - squire squir - squires squir - squirrel squirrel - st st - stab stab - stabb stabb - stabbed stab - stabbing stab - stable stabl - stableness stabl - stables stabl - stablish stablish - stablishment stablish - stabs stab - stacks stack - staff staff - stafford stafford - staffords stafford - staffordshire staffordshir - stag stag - stage stage - stages stage - stagger stagger - staggering stagger - staggers stagger - stags stag - staid staid - staider staider - stain stain - stained stain - staines stain - staineth staineth - staining stain - stainless stainless - stains stain - stair stair - stairs stair - stake stake - stakes stake - stale stale - staled stale - stalk stalk - stalking stalk - stalks stalk - stall stall - stalling stall - stalls stall - stamford stamford - stammer stammer - stamp stamp - stamped stamp - stamps stamp - stanch stanch - stanchless stanchless - stand stand - standard standard - standards standard - stander stander - standers stander - standest standest - standeth standeth - standing stand - stands stand - staniel staniel - stanley stanlei - stanze stanz - stanzo stanzo - stanzos stanzo - staple stapl - staples stapl - star star - stare stare - stared stare - stares stare - staring stare - starings stare - stark stark - starkly starkli - starlight starlight - starling starl - starr starr - starry starri - stars star - start start - started start - starting start - startingly startingli - startle startl - startles startl - starts start - starv starv - starve starv - starved starv - starvelackey starvelackei - starveling starvel - starveth starveth - starving starv - state state - statelier stateli - stately state - states state - statesman statesman - statesmen statesmen - statilius statiliu - station station - statist statist - statists statist - statue statu - statues statu - stature statur - statures statur - statute statut - statutes statut - stave stave - staves stave - stay stai - stayed stai - stayest stayest - staying stai - stays stai - stead stead - steaded stead - steadfast steadfast - steadier steadier - steads stead - steal steal - stealer stealer - stealers stealer - stealing steal - steals steal - stealth stealth - stealthy stealthi - steed steed - steeds steed - steel steel - steeled steel - steely steeli - steep steep - steeped steep - steeple steepl - steeples steepl - steeps steep - steepy steepi - steer steer - steerage steerag - steering steer - steers steer - stelled stell - stem stem - stemming stem - stench stench - step step - stepdame stepdam - stephano stephano - stephen stephen - stepmothers stepmoth - stepp stepp - stepping step - steps step - sterile steril - sterility steril - sterling sterl - stern stern - sternage sternag - sterner sterner - sternest sternest - sternness stern - steterat steterat - stew stew - steward steward - stewards steward - stewardship stewardship - stewed stew - stews stew - stick stick - sticking stick - stickler stickler - sticks stick - stiff stiff - stiffen stiffen - stiffly stiffli - stifle stifl - stifled stifl - stifles stifl - stigmatic stigmat - stigmatical stigmat - stile stile - still still - stiller stiller - stillest stillest - stillness still - stilly stilli - sting sting - stinging sting - stingless stingless - stings sting - stink stink - stinking stink - stinkingly stinkingli - stinks stink - stint stint - stinted stint - stints stint - stir stir - stirr stirr - stirred stir - stirrer stirrer - stirrers stirrer - stirreth stirreth - stirring stir - stirrup stirrup - stirrups stirrup - stirs stir - stitchery stitcheri - stitches stitch - stithied stithi - stithy stithi - stoccadoes stoccado - stoccata stoccata - stock stock - stockfish stockfish - stocking stock - stockings stock - stockish stockish - stocks stock - stog stog - stogs stog - stoics stoic - stokesly stokesli - stol stol - stole stole - stolen stolen - stolest stolest - stomach stomach - stomachers stomach - stomaching stomach - stomachs stomach - ston ston - stone stone - stonecutter stonecutt - stones stone - stonish stonish - stony stoni - stood stood - stool stool - stools stool - stoop stoop - stooping stoop - stoops stoop - stop stop - stope stope - stopp stopp - stopped stop - stopping stop - stops stop - stor stor - store store - storehouse storehous - storehouses storehous - stores store - stories stori - storm storm - stormed storm - storming storm - storms storm - stormy stormi - story stori - stoup stoup - stoups stoup - stout stout - stouter stouter - stoutly stoutli - stoutness stout - stover stover - stow stow - stowage stowag - stowed stow - strachy strachi - stragglers straggler - straggling straggl - straight straight - straightest straightest - straightway straightwai - strain strain - strained strain - straining strain - strains strain - strait strait - straited strait - straiter straiter - straitly straitli - straitness strait - straits strait - strand strand - strange strang - strangely strang - strangeness strang - stranger stranger - strangers stranger - strangest strangest - strangle strangl - strangled strangl - strangler strangler - strangles strangl - strangling strangl - strappado strappado - straps strap - stratagem stratagem - stratagems stratagem - stratford stratford - strato strato - straw straw - strawberries strawberri - strawberry strawberri - straws straw - strawy strawi - stray strai - straying strai - strays strai - streak streak - streaks streak - stream stream - streamers streamer - streaming stream - streams stream - streching strech - street street - streets street - strength strength - strengthen strengthen - strengthened strengthen - strengthless strengthless - strengths strength - stretch stretch - stretched stretch - stretches stretch - stretching stretch - strew strew - strewing strew - strewings strew - strewments strewment - stricken stricken - strict strict - stricter stricter - strictest strictest - strictly strictli - stricture strictur - stride stride - strides stride - striding stride - strife strife - strifes strife - strik strik - strike strike - strikers striker - strikes strike - strikest strikest - striking strike - string string - stringless stringless - strings string - strip strip - stripes stripe - stripling stripl - striplings stripl - stripp stripp - stripping strip - striv striv - strive strive - strives strive - striving strive - strok strok - stroke stroke - strokes stroke - strond strond - stronds strond - strong strong - stronger stronger - strongest strongest - strongly strongli - strooke strook - strossers strosser - strove strove - strown strown - stroy stroi - struck struck - strucken strucken - struggle struggl - struggles struggl - struggling struggl - strumpet strumpet - strumpeted strumpet - strumpets strumpet - strung strung - strut strut - struts strut - strutted strut - strutting strut - stubble stubbl - stubborn stubborn - stubbornest stubbornest - stubbornly stubbornli - stubbornness stubborn - stuck stuck - studded stud - student student - students student - studied studi - studies studi - studious studiou - studiously studious - studs stud - study studi - studying studi - stuff stuff - stuffing stuf - stuffs stuff - stumble stumbl - stumbled stumbl - stumblest stumblest - stumbling stumbl - stump stump - stumps stump - stung stung - stupefy stupefi - stupid stupid - stupified stupifi - stuprum stuprum - sturdy sturdi - sty sty - styga styga - stygian stygian - styl styl - style style - styx styx - su su - sub sub - subcontracted subcontract - subdu subdu - subdue subdu - subdued subdu - subduements subduement - subdues subdu - subduing subdu - subject subject - subjected subject - subjection subject - subjects subject - submerg submerg - submission submiss - submissive submiss - submit submit - submits submit - submitting submit - suborn suborn - subornation suborn - suborned suborn - subscrib subscrib - subscribe subscrib - subscribed subscrib - subscribes subscrib - subscription subscript - subsequent subsequ - subsidies subsidi - subsidy subsidi - subsist subsist - subsisting subsist - substance substanc - substances substanc - substantial substanti - substitute substitut - substituted substitut - substitutes substitut - substitution substitut - subtile subtil - subtilly subtilli - subtle subtl - subtleties subtleti - subtlety subtleti - subtly subtli - subtractors subtractor - suburbs suburb - subversion subvers - subverts subvert - succedant succed - succeed succe - succeeded succeed - succeeders succeed - succeeding succeed - succeeds succe - success success - successantly successantli - successes success - successful success - successfully successfulli - succession success - successive success - successively success - successor successor - successors successor - succour succour - succours succour - such such - suck suck - sucker sucker - suckers sucker - sucking suck - suckle suckl - sucks suck - sudden sudden - suddenly suddenli - sue sue - sued su - suerly suerli - sues sue - sueth sueth - suff suff - suffer suffer - sufferance suffer - sufferances suffer - suffered suffer - suffering suffer - suffers suffer - suffic suffic - suffice suffic - sufficed suffic - suffices suffic - sufficeth sufficeth - sufficiency suffici - sufficient suffici - sufficiently suffici - sufficing suffic - sufficit sufficit - suffigance suffig - suffocate suffoc - suffocating suffoc - suffocation suffoc - suffolk suffolk - suffrage suffrag - suffrages suffrag - sug sug - sugar sugar - sugarsop sugarsop - suggest suggest - suggested suggest - suggesting suggest - suggestion suggest - suggestions suggest - suggests suggest - suis sui - suit suit - suitable suitabl - suited suit - suiting suit - suitor suitor - suitors suitor - suits suit - suivez suivez - sullen sullen - sullens sullen - sullied sulli - sullies sulli - sully sulli - sulph sulph - sulpherous sulpher - sulphur sulphur - sulphurous sulphur - sultan sultan - sultry sultri - sum sum - sumless sumless - summ summ - summa summa - summary summari - summer summer - summers summer - summit summit - summon summon - summoners summon - summons summon - sumpter sumpter - sumptuous sumptuou - sumptuously sumptuous - sums sum - sun sun - sunbeams sunbeam - sunburning sunburn - sunburnt sunburnt - sund sund - sunday sundai - sundays sundai - sunder sunder - sunders sunder - sundry sundri - sung sung - sunk sunk - sunken sunken - sunny sunni - sunrising sunris - suns sun - sunset sunset - sunshine sunshin - sup sup - super super - superficial superfici - superficially superfici - superfluity superflu - superfluous superflu - superfluously superflu - superflux superflux - superior superior - supernal supern - supernatural supernatur - superpraise superprais - superscript superscript - superscription superscript - superserviceable superservic - superstition superstit - superstitious superstiti - superstitiously superstiti - supersubtle supersubtl - supervise supervis - supervisor supervisor - supp supp - supper supper - suppers supper - suppertime suppertim - supping sup - supplant supplant - supple suppl - suppler suppler - suppliance supplianc - suppliant suppliant - suppliants suppliant - supplicant supplic - supplication supplic - supplications supplic - supplie suppli - supplied suppli - supplies suppli - suppliest suppliest - supply suppli - supplyant supplyant - supplying suppli - supplyment supplyment - support support - supportable support - supportance support - supported support - supporter support - supporters support - supporting support - supportor supportor - suppos suppo - supposal suppos - suppose suppos - supposed suppos - supposes suppos - supposest supposest - supposing suppos - supposition supposit - suppress suppress - suppressed suppress - suppresseth suppresseth - supremacy supremaci - supreme suprem - sups sup - sur sur - surance suranc - surcease surceas - surd surd - sure sure - surecard surecard - surely sure - surer surer - surest surest - sureties sureti - surety sureti - surfeit surfeit - surfeited surfeit - surfeiter surfeit - surfeiting surfeit - surfeits surfeit - surge surg - surgeon surgeon - surgeons surgeon - surgere surger - surgery surgeri - surges surg - surly surli - surmis surmi - surmise surmis - surmised surmis - surmises surmis - surmount surmount - surmounted surmount - surmounts surmount - surnam surnam - surname surnam - surnamed surnam - surpasseth surpasseth - surpassing surpass - surplice surplic - surplus surplu - surpris surpri - surprise surpris - surprised surpris - surrender surrend - surrey surrei - surreys surrei - survey survei - surveyest surveyest - surveying survei - surveyor surveyor - surveyors surveyor - surveys survei - survive surviv - survives surviv - survivor survivor - susan susan - suspect suspect - suspected suspect - suspecting suspect - suspects suspect - suspend suspend - suspense suspens - suspicion suspicion - suspicions suspicion - suspicious suspici - suspiration suspir - suspire suspir - sust sust - sustain sustain - sustaining sustain - sutler sutler - sutton sutton - suum suum - swabber swabber - swaddling swaddl - swag swag - swagg swagg - swagger swagger - swaggerer swagger - swaggerers swagger - swaggering swagger - swain swain - swains swain - swallow swallow - swallowed swallow - swallowing swallow - swallows swallow - swam swam - swan swan - swans swan - sward sward - sware sware - swarm swarm - swarming swarm - swart swart - swarth swarth - swarths swarth - swarthy swarthi - swashers swasher - swashing swash - swath swath - swathing swath - swathling swathl - sway swai - swaying swai - sways swai - swear swear - swearer swearer - swearers swearer - swearest swearest - swearing swear - swearings swear - swears swear - sweat sweat - sweaten sweaten - sweating sweat - sweats sweat - sweaty sweati - sweep sweep - sweepers sweeper - sweeps sweep - sweet sweet - sweeten sweeten - sweetens sweeten - sweeter sweeter - sweetest sweetest - sweetheart sweetheart - sweeting sweet - sweetly sweetli - sweetmeats sweetmeat - sweetness sweet - sweets sweet - swell swell - swelling swell - swellings swell - swells swell - swelter swelter - sweno sweno - swept swept - swerve swerv - swerver swerver - swerving swerv - swift swift - swifter swifter - swiftest swiftest - swiftly swiftli - swiftness swift - swill swill - swills swill - swim swim - swimmer swimmer - swimmers swimmer - swimming swim - swims swim - swine swine - swineherds swineherd - swing swing - swinge swing - swinish swinish - swinstead swinstead - switches switch - swits swit - switzers switzer - swol swol - swoll swoll - swoln swoln - swoon swoon - swooned swoon - swooning swoon - swoons swoon - swoop swoop - swoopstake swoopstak - swor swor - sword sword - sworder sworder - swords sword - swore swore - sworn sworn - swounded swound - swounds swound - swum swum - swung swung - sy sy - sycamore sycamor - sycorax sycorax - sylla sylla - syllable syllabl - syllables syllabl - syllogism syllog - symbols symbol - sympathise sympathis - sympathiz sympathiz - sympathize sympath - sympathized sympath - sympathy sympathi - synagogue synagogu - synod synod - synods synod - syracuse syracus - syracusian syracusian - syracusians syracusian - syria syria - syrups syrup - t t - ta ta - taber taber - table tabl - tabled tabl - tables tabl - tablet tablet - tabor tabor - taborer tabor - tabors tabor - tabourines tabourin - taciturnity taciturn - tack tack - tackle tackl - tackled tackl - tackles tackl - tackling tackl - tacklings tackl - taddle taddl - tadpole tadpol - taffeta taffeta - taffety taffeti - tag tag - tagrag tagrag - tah tah - tail tail - tailor tailor - tailors tailor - tails tail - taint taint - tainted taint - tainting taint - taints taint - tainture taintur - tak tak - take take - taken taken - taker taker - takes take - takest takest - taketh taketh - taking take - tal tal - talbot talbot - talbotites talbotit - talbots talbot - tale tale - talent talent - talents talent - taleporter taleport - tales tale - talk talk - talked talk - talker talker - talkers talker - talkest talkest - talking talk - talks talk - tall tall - taller taller - tallest tallest - tallies talli - tallow tallow - tally talli - talons talon - tam tam - tambourines tambourin - tame tame - tamed tame - tamely tame - tameness tame - tamer tamer - tames tame - taming tame - tamora tamora - tamworth tamworth - tan tan - tang tang - tangle tangl - tangled tangl - tank tank - tanlings tanl - tann tann - tanned tan - tanner tanner - tanquam tanquam - tanta tanta - tantaene tantaen - tap tap - tape tape - taper taper - tapers taper - tapestries tapestri - tapestry tapestri - taphouse taphous - tapp tapp - tapster tapster - tapsters tapster - tar tar - tardied tardi - tardily tardili - tardiness tardi - tardy tardi - tarentum tarentum - targe targ - targes targ - target target - targets target - tarpeian tarpeian - tarquin tarquin - tarquins tarquin - tarr tarr - tarre tarr - tarriance tarrianc - tarried tarri - tarries tarri - tarry tarri - tarrying tarri - tart tart - tartar tartar - tartars tartar - tartly tartli - tartness tart - task task - tasker tasker - tasking task - tasks task - tassel tassel - taste tast - tasted tast - tastes tast - tasting tast - tatt tatt - tatter tatter - tattered tatter - tatters tatter - tattle tattl - tattling tattl - tattlings tattl - taught taught - taunt taunt - taunted taunt - taunting taunt - tauntingly tauntingli - taunts taunt - taurus tauru - tavern tavern - taverns tavern - tavy tavi - tawdry tawdri - tawny tawni - tax tax - taxation taxat - taxations taxat - taxes tax - taxing tax - tc tc - te te - teach teach - teacher teacher - teachers teacher - teaches teach - teachest teachest - teacheth teacheth - teaching teach - team team - tear tear - tearful tear - tearing tear - tears tear - tearsheet tearsheet - teat teat - tedious tediou - tediously tedious - tediousness tedious - teem teem - teeming teem - teems teem - teen teen - teeth teeth - teipsum teipsum - telamon telamon - telamonius telamoniu - tell tell - teller teller - telling tell - tells tell - tellus tellu - temp temp - temper temper - temperality temper - temperance temper - temperate temper - temperately temper - tempers temper - tempest tempest - tempests tempest - tempestuous tempestu - temple templ - temples templ - temporal tempor - temporary temporari - temporiz temporiz - temporize tempor - temporizer tempor - temps temp - tempt tempt - temptation temptat - temptations temptat - tempted tempt - tempter tempter - tempters tempter - tempteth tempteth - tempting tempt - tempts tempt - ten ten - tenable tenabl - tenant tenant - tenantius tenantiu - tenantless tenantless - tenants tenant - tench tench - tend tend - tendance tendanc - tended tend - tender tender - tendered tender - tenderly tenderli - tenderness tender - tenders tender - tending tend - tends tend - tenedos tenedo - tenement tenement - tenements tenement - tenfold tenfold - tennis tenni - tenour tenour - tenours tenour - tens ten - tent tent - tented tent - tenth tenth - tenths tenth - tents tent - tenure tenur - tenures tenur - tercel tercel - tereus tereu - term term - termagant termag - termed term - terminations termin - termless termless - terms term - terra terra - terrace terrac - terram terram - terras terra - terre terr - terrene terren - terrestrial terrestri - terrible terribl - terribly terribl - territories territori - territory territori - terror terror - terrors terror - tertian tertian - tertio tertio - test test - testament testament - tested test - tester tester - testern testern - testify testifi - testimonied testimoni - testimonies testimoni - testimony testimoni - testiness testi - testril testril - testy testi - tetchy tetchi - tether tether - tetter tetter - tevil tevil - tewksbury tewksburi - text text - tgv tgv - th th - thaes thae - thames thame - than than - thane thane - thanes thane - thank thank - thanked thank - thankful thank - thankfully thankfulli - thankfulness thank - thanking thank - thankings thank - thankless thankless - thanks thank - thanksgiving thanksgiv - thasos thaso - that that - thatch thatch - thaw thaw - thawing thaw - thaws thaw - the the - theatre theatr - theban theban - thebes thebe - thee thee - theft theft - thefts theft - thein thein - their their - theirs their - theise theis - them them - theme theme - themes theme - themselves themselv - then then - thence thenc - thenceforth thenceforth - theoric theoric - there there - thereabout thereabout - thereabouts thereabout - thereafter thereaft - thereat thereat - thereby therebi - therefore therefor - therein therein - thereof thereof - thereon thereon - thereto thereto - thereunto thereunto - thereupon thereupon - therewith therewith - therewithal therewith - thersites thersit - these these - theseus theseu - thessalian thessalian - thessaly thessali - thetis theti - thews thew - they thei - thick thick - thicken thicken - thickens thicken - thicker thicker - thickest thickest - thicket thicket - thickskin thickskin - thief thief - thievery thieveri - thieves thiev - thievish thievish - thigh thigh - thighs thigh - thimble thimbl - thimbles thimbl - thin thin - thine thine - thing thing - things thing - think think - thinkest thinkest - thinking think - thinkings think - thinks think - thinkst thinkst - thinly thinli - third third - thirdly thirdli - thirds third - thirst thirst - thirsting thirst - thirsts thirst - thirsty thirsti - thirteen thirteen - thirties thirti - thirtieth thirtieth - thirty thirti - this thi - thisby thisbi - thisne thisn - thistle thistl - thistles thistl - thither thither - thitherward thitherward - thoas thoa - thomas thoma - thorn thorn - thorns thorn - thorny thorni - thorough thorough - thoroughly thoroughli - those those - thou thou - though though - thought thought - thoughtful thought - thoughts thought - thousand thousand - thousands thousand - thracian thracian - thraldom thraldom - thrall thrall - thralled thrall - thralls thrall - thrash thrash - thrasonical thrason - thread thread - threadbare threadbar - threaden threaden - threading thread - threat threat - threaten threaten - threatening threaten - threatens threaten - threatest threatest - threats threat - three three - threefold threefold - threepence threepenc - threepile threepil - threes three - threescore threescor - thresher thresher - threshold threshold - threw threw - thrice thrice - thrift thrift - thriftless thriftless - thrifts thrift - thrifty thrifti - thrill thrill - thrilling thrill - thrills thrill - thrive thrive - thrived thrive - thrivers thriver - thrives thrive - thriving thrive - throat throat - throats throat - throbbing throb - throbs throb - throca throca - throe throe - throes throe - thromuldo thromuldo - thron thron - throne throne - throned throne - thrones throne - throng throng - thronging throng - throngs throng - throstle throstl - throttle throttl - through through - throughfare throughfar - throughfares throughfar - throughly throughli - throughout throughout - throw throw - thrower thrower - throwest throwest - throwing throw - thrown thrown - throws throw - thrum thrum - thrumm thrumm - thrush thrush - thrust thrust - thrusteth thrusteth - thrusting thrust - thrusts thrust - thumb thumb - thumbs thumb - thump thump - thund thund - thunder thunder - thunderbolt thunderbolt - thunderbolts thunderbolt - thunderer thunder - thunders thunder - thunderstone thunderston - thunderstroke thunderstrok - thurio thurio - thursday thursdai - thus thu - thwack thwack - thwart thwart - thwarted thwart - thwarting thwart - thwartings thwart - thy thy - thyme thyme - thymus thymu - thyreus thyreu - thyself thyself - ti ti - tib tib - tiber tiber - tiberio tiberio - tibey tibei - ticed tice - tick tick - tickl tickl - tickle tickl - tickled tickl - tickles tickl - tickling tickl - ticklish ticklish - tiddle tiddl - tide tide - tides tide - tidings tide - tidy tidi - tie tie - tied ti - ties ti - tiff tiff - tiger tiger - tigers tiger - tight tight - tightly tightli - tike tike - til til - tile tile - till till - tillage tillag - tilly tilli - tilt tilt - tilter tilter - tilth tilth - tilting tilt - tilts tilt - tiltyard tiltyard - tim tim - timandra timandra - timber timber - time time - timeless timeless - timelier timeli - timely time - times time - timon timon - timor timor - timorous timor - timorously timor - tinct tinct - tincture tinctur - tinctures tinctur - tinder tinder - tingling tingl - tinker tinker - tinkers tinker - tinsel tinsel - tiny tini - tip tip - tipp tipp - tippling tippl - tips tip - tipsy tipsi - tiptoe tipto - tir tir - tire tire - tired tire - tires tire - tirest tirest - tiring tire - tirra tirra - tirrits tirrit - tis ti - tish tish - tisick tisick - tissue tissu - titan titan - titania titania - tithe tith - tithed tith - tithing tith - titinius titiniu - title titl - titled titl - titleless titleless - titles titl - tittle tittl - tittles tittl - titular titular - titus titu - tn tn - to to - toad toad - toads toad - toadstool toadstool - toast toast - toasted toast - toasting toast - toasts toast - toaze toaz - toby tobi - tock tock - tod tod - today todai - todpole todpol - tods tod - toe toe - toes toe - tofore tofor - toge toge - toged toge - together togeth - toil toil - toiled toil - toiling toil - toils toil - token token - tokens token - told told - toledo toledo - tolerable toler - toll toll - tolling toll - tom tom - tomb tomb - tombe tomb - tombed tomb - tombless tombless - tomboys tomboi - tombs tomb - tomorrow tomorrow - tomyris tomyri - ton ton - tongs tong - tongu tongu - tongue tongu - tongued tongu - tongueless tongueless - tongues tongu - tonight tonight - too too - took took - tool tool - tools tool - tooth tooth - toothache toothach - toothpick toothpick - toothpicker toothpick - top top - topas topa - topful top - topgallant topgal - topless topless - topmast topmast - topp topp - topping top - topple toppl - topples toppl - tops top - topsail topsail - topsy topsi - torch torch - torchbearer torchbear - torchbearers torchbear - torcher torcher - torches torch - torchlight torchlight - tore tore - torment torment - tormenta tormenta - tormente torment - tormented torment - tormenting torment - tormentors tormentor - torments torment - torn torn - torrent torrent - tortive tortiv - tortoise tortois - tortur tortur - torture tortur - tortured tortur - torturer tortur - torturers tortur - tortures tortur - torturest torturest - torturing tortur - toryne toryn - toss toss - tossed toss - tosseth tosseth - tossing toss - tot tot - total total - totally total - tott tott - tottered totter - totters totter - tou tou - touch touch - touched touch - touches touch - toucheth toucheth - touching touch - touchstone touchston - tough tough - tougher tougher - toughness tough - touraine tourain - tournaments tournament - tours tour - tous tou - tout tout - touze touz - tow tow - toward toward - towardly towardli - towards toward - tower tower - towering tower - towers tower - town town - towns town - township township - townsman townsman - townsmen townsmen - towton towton - toy toi - toys toi - trace trace - traces trace - track track - tract tract - tractable tractabl - trade trade - traded trade - traders trader - trades trade - tradesman tradesman - tradesmen tradesmen - trading trade - tradition tradit - traditional tradit - traduc traduc - traduced traduc - traducement traduc - traffic traffic - traffickers traffick - traffics traffic - tragedian tragedian - tragedians tragedian - tragedies tragedi - tragedy tragedi - tragic tragic - tragical tragic - trail trail - train train - trained train - training train - trains train - trait trait - traitor traitor - traitorly traitorli - traitorous traitor - traitorously traitor - traitors traitor - traitress traitress - traject traject - trammel trammel - trample trampl - trampled trampl - trampling trampl - tranc tranc - trance tranc - tranio tranio - tranquil tranquil - tranquillity tranquil - transcendence transcend - transcends transcend - transferred transfer - transfigur transfigur - transfix transfix - transform transform - transformation transform - transformations transform - transformed transform - transgress transgress - transgresses transgress - transgressing transgress - transgression transgress - translate translat - translated translat - translates translat - translation translat - transmigrates transmigr - transmutation transmut - transparent transpar - transport transport - transportance transport - transported transport - transporting transport - transports transport - transpose transpos - transshape transshap - trap trap - trapp trapp - trappings trap - traps trap - trash trash - travail travail - travails travail - travel travel - traveler travel - traveling travel - travell travel - travelled travel - traveller travel - travellers travel - travellest travellest - travelling travel - travels travel - travers traver - traverse travers - tray trai - treacherous treacher - treacherously treacher - treachers treacher - treachery treacheri - tread tread - treading tread - treads tread - treason treason - treasonable treason - treasonous treason - treasons treason - treasure treasur - treasurer treasur - treasures treasur - treasuries treasuri - treasury treasuri - treat treat - treaties treati - treatise treatis - treats treat - treaty treati - treble trebl - trebled trebl - trebles trebl - trebonius treboniu - tree tree - trees tree - tremble trembl - trembled trembl - trembles trembl - tremblest tremblest - trembling trembl - tremblingly tremblingli - tremor tremor - trempling trempl - trench trench - trenchant trenchant - trenched trench - trencher trencher - trenchering trencher - trencherman trencherman - trenchers trencher - trenches trench - trenching trench - trent trent - tres tre - trespass trespass - trespasses trespass - tressel tressel - tresses tress - treys trei - trial trial - trials trial - trib trib - tribe tribe - tribes tribe - tribulation tribul - tribunal tribun - tribune tribun - tribunes tribun - tributaries tributari - tributary tributari - tribute tribut - tributes tribut - trice trice - trick trick - tricking trick - trickling trickl - tricks trick - tricksy tricksi - trident trident - tried tri - trier trier - trifle trifl - trifled trifl - trifler trifler - trifles trifl - trifling trifl - trigon trigon - trill trill - trim trim - trimly trimli - trimm trimm - trimmed trim - trimming trim - trims trim - trinculo trinculo - trinculos trinculo - trinkets trinket - trip trip - tripartite tripartit - tripe tripe - triple tripl - triplex triplex - tripoli tripoli - tripolis tripoli - tripp tripp - tripping trip - trippingly trippingli - trips trip - tristful trist - triton triton - triumph triumph - triumphant triumphant - triumphantly triumphantli - triumpher triumpher - triumphers triumpher - triumphing triumph - triumphs triumph - triumvir triumvir - triumvirate triumvir - triumvirs triumvir - triumviry triumviri - trivial trivial - troat troat - trod trod - trodden trodden - troiant troiant - troien troien - troilus troilu - troiluses troilus - trojan trojan - trojans trojan - troll troll - tromperies tromperi - trompet trompet - troop troop - trooping troop - troops troop - trop trop - trophies trophi - trophy trophi - tropically tropic - trot trot - troth troth - trothed troth - troths troth - trots trot - trotting trot - trouble troubl - troubled troubl - troubler troubler - troubles troubl - troublesome troublesom - troublest troublest - troublous troublou - trough trough - trout trout - trouts trout - trovato trovato - trow trow - trowel trowel - trowest trowest - troy troi - troyan troyan - troyans troyan - truant truant - truce truce - truckle truckl - trudge trudg - true true - trueborn trueborn - truepenny truepenni - truer truer - truest truest - truie truie - trull trull - trulls trull - truly truli - trump trump - trumpery trumperi - trumpet trumpet - trumpeter trumpet - trumpeters trumpet - trumpets trumpet - truncheon truncheon - truncheoners truncheon - trundle trundl - trunk trunk - trunks trunk - trust trust - trusted trust - truster truster - trusters truster - trusting trust - trusts trust - trusty trusti - truth truth - truths truth - try try - ts ts - tu tu - tuae tuae - tub tub - tubal tubal - tubs tub - tuck tuck - tucket tucket - tuesday tuesdai - tuft tuft - tufts tuft - tug tug - tugg tugg - tugging tug - tuition tuition - tullus tullu - tully tulli - tumble tumbl - tumbled tumbl - tumbler tumbler - tumbling tumbl - tumult tumult - tumultuous tumultu - tun tun - tune tune - tuneable tuneabl - tuned tune - tuners tuner - tunes tune - tunis tuni - tuns tun - tupping tup - turban turban - turbans turban - turbulence turbul - turbulent turbul - turd turd - turf turf - turfy turfi - turk turk - turkey turkei - turkeys turkei - turkish turkish - turks turk - turlygod turlygod - turmoil turmoil - turmoiled turmoil - turn turn - turnbull turnbul - turncoat turncoat - turncoats turncoat - turned turn - turneth turneth - turning turn - turnips turnip - turns turn - turph turph - turpitude turpitud - turquoise turquois - turret turret - turrets turret - turtle turtl - turtles turtl - turvy turvi - tuscan tuscan - tush tush - tut tut - tutor tutor - tutored tutor - tutors tutor - tutto tutto - twain twain - twang twang - twangling twangl - twas twa - tway twai - tweaks tweak - tween tween - twelfth twelfth - twelve twelv - twelvemonth twelvemonth - twentieth twentieth - twenty twenti - twere twere - twice twice - twig twig - twiggen twiggen - twigs twig - twilight twilight - twill twill - twilled twill - twin twin - twine twine - twink twink - twinkle twinkl - twinkled twinkl - twinkling twinkl - twinn twinn - twins twin - twire twire - twist twist - twisted twist - twit twit - twits twit - twitting twit - twixt twixt - two two - twofold twofold - twopence twopenc - twopences twopenc - twos two - twould twould - tyb tyb - tybalt tybalt - tybalts tybalt - tyburn tyburn - tying ty - tyke tyke - tymbria tymbria - type type - types type - typhon typhon - tyrannical tyrann - tyrannically tyrann - tyrannize tyrann - tyrannous tyrann - tyranny tyranni - tyrant tyrant - tyrants tyrant - tyrian tyrian - tyrrel tyrrel - u u - ubique ubiqu - udders udder - udge udg - uds ud - uglier uglier - ugliest ugliest - ugly ugli - ulcer ulcer - ulcerous ulcer - ulysses ulyss - um um - umber umber - umbra umbra - umbrage umbrag - umfrevile umfrevil - umpire umpir - umpires umpir - un un - unable unabl - unaccommodated unaccommod - unaccompanied unaccompani - unaccustom unaccustom - unaching unach - unacquainted unacquaint - unactive unact - unadvis unadvi - unadvised unadvis - unadvisedly unadvisedli - unagreeable unagre - unanel unanel - unanswer unansw - unappeas unappea - unapproved unapprov - unapt unapt - unaptness unapt - unarm unarm - unarmed unarm - unarms unarm - unassail unassail - unassailable unassail - unattainted unattaint - unattempted unattempt - unattended unattend - unauspicious unauspici - unauthorized unauthor - unavoided unavoid - unawares unawar - unback unback - unbak unbak - unbanded unband - unbar unbar - unbarb unbarb - unbashful unbash - unbated unbat - unbatter unbatt - unbecoming unbecom - unbefitting unbefit - unbegot unbegot - unbegotten unbegotten - unbelieved unbeliev - unbend unbend - unbent unbent - unbewail unbewail - unbid unbid - unbidden unbidden - unbind unbind - unbinds unbind - unbitted unbit - unbless unbless - unblest unblest - unbloodied unbloodi - unblown unblown - unbodied unbodi - unbolt unbolt - unbolted unbolt - unbonneted unbonnet - unbookish unbookish - unborn unborn - unbosom unbosom - unbound unbound - unbounded unbound - unbow unbow - unbowed unbow - unbrac unbrac - unbraced unbrac - unbraided unbraid - unbreathed unbreath - unbred unbr - unbreech unbreech - unbridled unbridl - unbroke unbrok - unbruis unbrui - unbruised unbruis - unbuckle unbuckl - unbuckles unbuckl - unbuckling unbuckl - unbuild unbuild - unburden unburden - unburdens unburden - unburied unburi - unburnt unburnt - unburthen unburthen - unbutton unbutton - unbuttoning unbutton - uncapable uncap - uncape uncap - uncase uncas - uncasing uncas - uncaught uncaught - uncertain uncertain - uncertainty uncertainti - unchain unchain - unchanging unchang - uncharge uncharg - uncharged uncharg - uncharitably uncharit - unchary unchari - unchaste unchast - uncheck uncheck - unchilded unchild - uncivil uncivil - unclaim unclaim - unclasp unclasp - uncle uncl - unclean unclean - uncleanliness uncleanli - uncleanly uncleanli - uncleanness unclean - uncles uncl - unclew unclew - unclog unclog - uncoined uncoin - uncolted uncolt - uncomeliness uncomeli - uncomfortable uncomfort - uncompassionate uncompassion - uncomprehensive uncomprehens - unconfinable unconfin - unconfirm unconfirm - unconfirmed unconfirm - unconquer unconqu - unconquered unconqu - unconsidered unconsid - unconstant unconst - unconstrain unconstrain - unconstrained unconstrain - uncontemn uncontemn - uncontroll uncontrol - uncorrected uncorrect - uncounted uncount - uncouple uncoupl - uncourteous uncourt - uncouth uncouth - uncover uncov - uncovered uncov - uncropped uncrop - uncross uncross - uncrown uncrown - unction unction - unctuous unctuou - uncuckolded uncuckold - uncurable uncur - uncurbable uncurb - uncurbed uncurb - uncurls uncurl - uncurrent uncurr - uncurse uncurs - undaunted undaunt - undeaf undeaf - undeck undeck - undeeded undeed - under under - underbearing underbear - underborne underborn - undercrest undercrest - underfoot underfoot - undergo undergo - undergoes undergo - undergoing undergo - undergone undergon - underground underground - underhand underhand - underlings underl - undermine undermin - underminers undermin - underneath underneath - underprizing underpr - underprop underprop - understand understand - understandeth understandeth - understanding understand - understandings understand - understands understand - understood understood - underta underta - undertake undertak - undertakeing undertak - undertaker undertak - undertakes undertak - undertaking undertak - undertakings undertak - undertook undertook - undervalu undervalu - undervalued undervalu - underwent underw - underwrit underwrit - underwrite underwrit - undescried undescri - undeserved undeserv - undeserver undeserv - undeservers undeserv - undeserving undeserv - undetermin undetermin - undid undid - undinted undint - undiscernible undiscern - undiscover undiscov - undishonoured undishonour - undispos undispo - undistinguishable undistinguish - undistinguished undistinguish - undividable undivid - undivided undivid - undivulged undivulg - undo undo - undoes undo - undoing undo - undone undon - undoubted undoubt - undoubtedly undoubtedli - undream undream - undress undress - undressed undress - undrown undrown - unduteous undut - undutiful unduti - une un - uneared unear - unearned unearn - unearthly unearthli - uneasines uneasin - uneasy uneasi - uneath uneath - uneducated uneduc - uneffectual uneffectu - unelected unelect - unequal unequ - uneven uneven - unexamin unexamin - unexecuted unexecut - unexpected unexpect - unexperienc unexperienc - unexperient unexperi - unexpressive unexpress - unfair unfair - unfaithful unfaith - unfallible unfal - unfam unfam - unfashionable unfashion - unfasten unfasten - unfather unfath - unfathered unfath - unfed unf - unfeed unfe - unfeeling unfeel - unfeigned unfeign - unfeignedly unfeignedli - unfellowed unfellow - unfelt unfelt - unfenced unfenc - unfilial unfili - unfill unfil - unfinish unfinish - unfirm unfirm - unfit unfit - unfitness unfit - unfix unfix - unfledg unfledg - unfold unfold - unfolded unfold - unfoldeth unfoldeth - unfolding unfold - unfolds unfold - unfool unfool - unforc unforc - unforced unforc - unforfeited unforfeit - unfortified unfortifi - unfortunate unfortun - unfought unfought - unfrequented unfrequ - unfriended unfriend - unfurnish unfurnish - ungain ungain - ungalled ungal - ungart ungart - ungarter ungart - ungenitur ungenitur - ungentle ungentl - ungentleness ungentl - ungently ungent - ungird ungird - ungodly ungodli - ungor ungor - ungot ungot - ungotten ungotten - ungovern ungovern - ungracious ungraci - ungrateful ungrat - ungravely ungrav - ungrown ungrown - unguarded unguard - unguem unguem - unguided unguid - unhack unhack - unhair unhair - unhallow unhallow - unhallowed unhallow - unhand unhand - unhandled unhandl - unhandsome unhandsom - unhang unhang - unhappied unhappi - unhappily unhappili - unhappiness unhappi - unhappy unhappi - unhardened unharden - unharm unharm - unhatch unhatch - unheard unheard - unhearts unheart - unheedful unheed - unheedfully unheedfulli - unheedy unheedi - unhelpful unhelp - unhidden unhidden - unholy unholi - unhop unhop - unhopefullest unhopefullest - unhorse unhors - unhospitable unhospit - unhous unhou - unhoused unhous - unhurtful unhurt - unicorn unicorn - unicorns unicorn - unimproved unimprov - uninhabitable uninhabit - uninhabited uninhabit - unintelligent unintellig - union union - unions union - unite unit - united unit - unity uniti - universal univers - universe univers - universities univers - university univers - unjointed unjoint - unjust unjust - unjustice unjustic - unjustly unjustli - unkennel unkennel - unkept unkept - unkind unkind - unkindest unkindest - unkindly unkindli - unkindness unkind - unking unk - unkinglike unkinglik - unkiss unkiss - unknit unknit - unknowing unknow - unknown unknown - unlace unlac - unlaid unlaid - unlawful unlaw - unlawfully unlawfulli - unlearn unlearn - unlearned unlearn - unless unless - unlesson unlesson - unletter unlett - unlettered unlett - unlick unlick - unlike unlik - unlikely unlik - unlimited unlimit - unlineal unlin - unlink unlink - unload unload - unloaded unload - unloading unload - unloads unload - unlock unlock - unlocks unlock - unlook unlook - unlooked unlook - unloos unloo - unloose unloos - unlov unlov - unloving unlov - unluckily unluckili - unlucky unlucki - unmade unmad - unmake unmak - unmanly unmanli - unmann unmann - unmanner unmann - unmannerd unmannerd - unmannerly unmannerli - unmarried unmarri - unmask unmask - unmasked unmask - unmasking unmask - unmasks unmask - unmast unmast - unmatch unmatch - unmatchable unmatch - unmatched unmatch - unmeasurable unmeasur - unmeet unmeet - unmellowed unmellow - unmerciful unmerci - unmeritable unmerit - unmeriting unmerit - unminded unmind - unmindfull unmindful - unmingled unmingl - unmitigable unmitig - unmitigated unmitig - unmix unmix - unmoan unmoan - unmov unmov - unmoved unmov - unmoving unmov - unmuffles unmuffl - unmuffling unmuffl - unmusical unmus - unmuzzle unmuzzl - unmuzzled unmuzzl - unnatural unnatur - unnaturally unnatur - unnaturalness unnatur - unnecessarily unnecessarili - unnecessary unnecessari - unneighbourly unneighbourli - unnerved unnerv - unnoble unnobl - unnoted unnot - unnumb unnumb - unnumber unnumb - unowed unow - unpack unpack - unpaid unpaid - unparagon unparagon - unparallel unparallel - unpartial unparti - unpath unpath - unpaved unpav - unpay unpai - unpeaceable unpeac - unpeg unpeg - unpeople unpeopl - unpeopled unpeopl - unperfect unperfect - unperfectness unperfect - unpick unpick - unpin unpin - unpink unpink - unpitied unpiti - unpitifully unpitifulli - unplagu unplagu - unplausive unplaus - unpleas unplea - unpleasant unpleas - unpleasing unpleas - unpolicied unpolici - unpolish unpolish - unpolished unpolish - unpolluted unpollut - unpossess unpossess - unpossessing unpossess - unpossible unposs - unpractis unpracti - unpregnant unpregn - unpremeditated unpremedit - unprepar unprepar - unprepared unprepar - unpress unpress - unprevailing unprevail - unprevented unprev - unpriz unpriz - unprizable unpriz - unprofitable unprofit - unprofited unprofit - unproper unprop - unproperly unproperli - unproportion unproport - unprovide unprovid - unprovided unprovid - unprovident unprovid - unprovokes unprovok - unprun unprun - unpruned unprun - unpublish unpublish - unpurged unpurg - unpurpos unpurpo - unqualitied unqual - unqueen unqueen - unquestion unquest - unquestionable unquestion - unquiet unquiet - unquietly unquietli - unquietness unquiet - unraised unrais - unrak unrak - unread unread - unready unreadi - unreal unreal - unreasonable unreason - unreasonably unreason - unreclaimed unreclaim - unreconciled unreconcil - unreconciliable unreconcili - unrecounted unrecount - unrecuring unrecur - unregarded unregard - unregist unregist - unrelenting unrel - unremovable unremov - unremovably unremov - unreprievable unrepriev - unresolv unresolv - unrespected unrespect - unrespective unrespect - unrest unrest - unrestor unrestor - unrestrained unrestrain - unreveng unreveng - unreverend unreverend - unreverent unrever - unrevers unrev - unrewarded unreward - unrighteous unright - unrightful unright - unripe unrip - unripp unripp - unrivall unrival - unroll unrol - unroof unroof - unroosted unroost - unroot unroot - unrough unrough - unruly unruli - unsafe unsaf - unsaluted unsalut - unsanctified unsanctifi - unsatisfied unsatisfi - unsavoury unsavouri - unsay unsai - unscalable unscal - unscann unscann - unscarr unscarr - unschool unschool - unscorch unscorch - unscour unscour - unscratch unscratch - unseal unseal - unseam unseam - unsearch unsearch - unseason unseason - unseasonable unseason - unseasonably unseason - unseasoned unseason - unseconded unsecond - unsecret unsecret - unseduc unseduc - unseeing unse - unseeming unseem - unseemly unseemli - unseen unseen - unseminar unseminar - unseparable unsepar - unserviceable unservic - unset unset - unsettle unsettl - unsettled unsettl - unsever unsev - unsex unsex - unshak unshak - unshaked unshak - unshaken unshaken - unshaped unshap - unshapes unshap - unsheath unsheath - unsheathe unsheath - unshorn unshorn - unshout unshout - unshown unshown - unshrinking unshrink - unshrubb unshrubb - unshunn unshunn - unshunnable unshunn - unsifted unsift - unsightly unsightli - unsinew unsinew - unsisting unsist - unskilful unskil - unskilfully unskilfulli - unskillful unskil - unslipping unslip - unsmirched unsmirch - unsoil unsoil - unsolicited unsolicit - unsorted unsort - unsought unsought - unsound unsound - unsounded unsound - unspeak unspeak - unspeakable unspeak - unspeaking unspeak - unsphere unspher - unspoke unspok - unspoken unspoken - unspotted unspot - unsquar unsquar - unstable unstabl - unstaid unstaid - unstain unstain - unstained unstain - unstanched unstanch - unstate unstat - unsteadfast unsteadfast - unstooping unstoop - unstringed unstring - unstuff unstuff - unsubstantial unsubstanti - unsuitable unsuit - unsuiting unsuit - unsullied unsulli - unsunn unsunn - unsur unsur - unsure unsur - unsuspected unsuspect - unsway unswai - unswayable unsway - unswayed unswai - unswear unswear - unswept unswept - unsworn unsworn - untainted untaint - untalk untalk - untangle untangl - untangled untangl - untasted untast - untaught untaught - untempering untemp - untender untend - untent untent - untented untent - unthankful unthank - unthankfulness unthank - unthink unthink - unthought unthought - unthread unthread - unthrift unthrift - unthrifts unthrift - unthrifty unthrifti - untie unti - untied unti - until until - untimber untimb - untimely untim - untir untir - untirable untir - untired untir - untitled untitl - unto unto - untold untold - untouch untouch - untoward untoward - untowardly untowardli - untraded untrad - untrain untrain - untrained untrain - untread untread - untreasur untreasur - untried untri - untrimmed untrim - untrod untrod - untrodden untrodden - untroubled untroubl - untrue untru - untrussing untruss - untruth untruth - untruths untruth - untucked untuck - untun untun - untune untun - untuneable untun - untutor untutor - untutored untutor - untwine untwin - unurg unurg - unus unu - unused unus - unusual unusu - unvalued unvalu - unvanquish unvanquish - unvarnish unvarnish - unveil unveil - unveiling unveil - unvenerable unvener - unvex unvex - unviolated unviol - unvirtuous unvirtu - unvisited unvisit - unvulnerable unvulner - unwares unwar - unwarily unwarili - unwash unwash - unwatch unwatch - unwearied unweari - unwed unw - unwedgeable unwedg - unweeded unweed - unweighed unweigh - unweighing unweigh - unwelcome unwelcom - unwept unwept - unwhipp unwhipp - unwholesome unwholesom - unwieldy unwieldi - unwilling unwil - unwillingly unwillingli - unwillingness unwilling - unwind unwind - unwiped unwip - unwise unwis - unwisely unwis - unwish unwish - unwished unwish - unwitted unwit - unwittingly unwittingli - unwonted unwont - unwooed unwoo - unworthier unworthi - unworthiest unworthiest - unworthily unworthili - unworthiness unworthi - unworthy unworthi - unwrung unwrung - unyok unyok - unyoke unyok - up up - upbraid upbraid - upbraided upbraid - upbraidings upbraid - upbraids upbraid - uphoarded uphoard - uphold uphold - upholdeth upholdeth - upholding uphold - upholds uphold - uplift uplift - uplifted uplift - upmost upmost - upon upon - upper upper - uprear uprear - upreared uprear - upright upright - uprighteously upright - uprightness upright - uprise upris - uprising upris - uproar uproar - uproars uproar - uprous uprou - upshoot upshoot - upshot upshot - upside upsid - upspring upspr - upstairs upstair - upstart upstart - upturned upturn - upward upward - upwards upward - urchin urchin - urchinfield urchinfield - urchins urchin - urg urg - urge urg - urged urg - urgent urgent - urges urg - urgest urgest - urging urg - urinal urin - urinals urin - urine urin - urn urn - urns urn - urs ur - ursa ursa - ursley urslei - ursula ursula - urswick urswick - us us - usage usag - usance usanc - usances usanc - use us - used us - useful us - useless useless - user user - uses us - usest usest - useth useth - usher usher - ushered usher - ushering usher - ushers usher - using us - usual usual - usually usual - usurer usur - usurers usur - usuries usuri - usuring usur - usurp usurp - usurpation usurp - usurped usurp - usurper usurp - usurpers usurp - usurping usurp - usurpingly usurpingli - usurps usurp - usury usuri - ut ut - utensil utensil - utensils utensil - utility util - utmost utmost - utt utt - utter utter - utterance utter - uttered utter - uttereth uttereth - uttering utter - utterly utterli - uttermost uttermost - utters utter - uy uy - v v - va va - vacancy vacanc - vacant vacant - vacation vacat - vade vade - vagabond vagabond - vagabonds vagabond - vagram vagram - vagrom vagrom - vail vail - vailed vail - vailing vail - vaillant vaillant - vain vain - vainer vainer - vainglory vainglori - vainly vainli - vainness vain - vais vai - valanc valanc - valance valanc - vale vale - valence valenc - valentine valentin - valentinus valentinu - valentio valentio - valeria valeria - valerius valeriu - vales vale - valiant valiant - valiantly valiantli - valiantness valiant - validity valid - vallant vallant - valley vallei - valleys vallei - vally valli - valor valor - valorous valor - valorously valor - valour valour - valu valu - valuation valuat - value valu - valued valu - valueless valueless - values valu - valuing valu - vane vane - vanish vanish - vanished vanish - vanishes vanish - vanishest vanishest - vanishing vanish - vanities vaniti - vanity vaniti - vanquish vanquish - vanquished vanquish - vanquisher vanquish - vanquishest vanquishest - vanquisheth vanquisheth - vant vant - vantage vantag - vantages vantag - vantbrace vantbrac - vapians vapian - vapor vapor - vaporous vapor - vapour vapour - vapours vapour - vara vara - variable variabl - variance varianc - variation variat - variations variat - varied vari - variest variest - variety varieti - varld varld - varlet varlet - varletry varletri - varlets varlet - varletto varletto - varnish varnish - varrius varriu - varro varro - vary vari - varying vari - vassal vassal - vassalage vassalag - vassals vassal - vast vast - vastidity vastid - vasty vasti - vat vat - vater vater - vaudemont vaudemont - vaughan vaughan - vault vault - vaultages vaultag - vaulted vault - vaulting vault - vaults vault - vaulty vaulti - vaumond vaumond - vaunt vaunt - vaunted vaunt - vaunter vaunter - vaunting vaunt - vauntingly vauntingli - vaunts vaunt - vauvado vauvado - vaux vaux - vaward vaward - ve ve - veal veal - vede vede - vehemence vehem - vehemency vehem - vehement vehement - vehor vehor - veil veil - veiled veil - veiling veil - vein vein - veins vein - vell vell - velure velur - velutus velutu - velvet velvet - vendible vendibl - venerable vener - venereal vener - venetia venetia - venetian venetian - venetians venetian - veneys venei - venge veng - vengeance vengeanc - vengeances vengeanc - vengeful veng - veni veni - venial venial - venice venic - venison venison - venit venit - venom venom - venomous venom - venomously venom - vent vent - ventages ventag - vented vent - ventidius ventidiu - ventricle ventricl - vents vent - ventur ventur - venture ventur - ventured ventur - ventures ventur - venturing ventur - venturous ventur - venue venu - venus venu - venuto venuto - ver ver - verb verb - verba verba - verbal verbal - verbatim verbatim - verbosity verbos - verdict verdict - verdun verdun - verdure verdur - vere vere - verefore verefor - verg verg - verge verg - vergers verger - verges verg - verier verier - veriest veriest - verified verifi - verify verifi - verily verili - veritable verit - verite verit - verities veriti - verity veriti - vermilion vermilion - vermin vermin - vernon vernon - verona verona - veronesa veronesa - versal versal - verse vers - verses vers - versing vers - vert vert - very veri - vesper vesper - vessel vessel - vessels vessel - vestal vestal - vestments vestment - vesture vestur - vetch vetch - vetches vetch - veux veux - vex vex - vexation vexat - vexations vexat - vexed vex - vexes vex - vexest vexest - vexeth vexeth - vexing vex - vi vi - via via - vial vial - vials vial - viand viand - viands viand - vic vic - vicar vicar - vice vice - vicegerent viceger - vicentio vicentio - viceroy viceroi - viceroys viceroi - vices vice - vici vici - vicious viciou - viciousness vicious - vict vict - victims victim - victor victor - victoress victoress - victories victori - victorious victori - victors victor - victory victori - victual victual - victuall victual - victuals victual - videlicet videlicet - video video - vides vide - videsne videsn - vidi vidi - vie vie - vied vi - vienna vienna - view view - viewest viewest - vieweth vieweth - viewing view - viewless viewless - views view - vigil vigil - vigilance vigil - vigilant vigil - vigitant vigit - vigour vigour - vii vii - viii viii - vile vile - vilely vile - vileness vile - viler viler - vilest vilest - vill vill - village villag - villager villag - villagery villageri - villages villag - villain villain - villainies villaini - villainous villain - villainously villain - villains villain - villainy villaini - villanies villani - villanous villan - villany villani - villiago villiago - villian villian - villianda villianda - villians villian - vinaigre vinaigr - vincentio vincentio - vincere vincer - vindicative vindic - vine vine - vinegar vinegar - vines vine - vineyard vineyard - vineyards vineyard - vint vint - vintner vintner - viol viol - viola viola - violate violat - violated violat - violates violat - violation violat - violator violat - violence violenc - violent violent - violenta violenta - violenteth violenteth - violently violent - violet violet - violets violet - viper viper - viperous viper - vipers viper - vir vir - virgilia virgilia - virgin virgin - virginal virgin - virginalling virginal - virginity virgin - virginius virginiu - virgins virgin - virgo virgo - virtue virtu - virtues virtu - virtuous virtuou - virtuously virtuous - visag visag - visage visag - visages visag - visard visard - viscount viscount - visible visibl - visibly visibl - vision vision - visions vision - visit visit - visitation visit - visitations visit - visited visit - visiting visit - visitings visit - visitor visitor - visitors visitor - visits visit - visor visor - vita vita - vitae vita - vital vital - vitement vitement - vitruvio vitruvio - vitx vitx - viva viva - vivant vivant - vive vive - vixen vixen - viz viz - vizaments vizament - vizard vizard - vizarded vizard - vizards vizard - vizor vizor - vlouting vlout - vocation vocat - vocativo vocativo - vocatur vocatur - voce voce - voic voic - voice voic - voices voic - void void - voided void - voiding void - voke voke - volable volabl - volant volant - volivorco volivorco - volley vollei - volquessen volquessen - volsce volsc - volsces volsc - volscian volscian - volscians volscian - volt volt - voltemand voltemand - volubility volubl - voluble volubl - volume volum - volumes volum - volumnia volumnia - volumnius volumniu - voluntaries voluntari - voluntary voluntari - voluptuously voluptu - voluptuousness voluptu - vomissement vomiss - vomit vomit - vomits vomit - vor vor - vore vore - vortnight vortnight - vot vot - votaries votari - votarist votarist - votarists votarist - votary votari - votre votr - vouch vouch - voucher voucher - vouchers voucher - vouches vouch - vouching vouch - vouchsaf vouchsaf - vouchsafe vouchsaf - vouchsafed vouchsaf - vouchsafes vouchsaf - vouchsafing vouchsaf - voudrais voudrai - vour vour - vous vou - voutsafe voutsaf - vow vow - vowed vow - vowel vowel - vowels vowel - vowing vow - vows vow - vox vox - voyage voyag - voyages voyag - vraiment vraiment - vulcan vulcan - vulgar vulgar - vulgarly vulgarli - vulgars vulgar - vulgo vulgo - vulnerable vulner - vulture vultur - vultures vultur - vurther vurther - w w - wad wad - waddled waddl - wade wade - waded wade - wafer wafer - waft waft - waftage waftag - wafting waft - wafts waft - wag wag - wage wage - wager wager - wagers wager - wages wage - wagging wag - waggish waggish - waggling waggl - waggon waggon - waggoner waggon - wagon wagon - wagoner wagon - wags wag - wagtail wagtail - wail wail - wailful wail - wailing wail - wails wail - wain wain - wainropes wainrop - wainscot wainscot - waist waist - wait wait - waited wait - waiter waiter - waiteth waiteth - waiting wait - waits wait - wak wak - wake wake - waked wake - wakefield wakefield - waken waken - wakened waken - wakes wake - wakest wakest - waking wake - wales wale - walk walk - walked walk - walking walk - walks walk - wall wall - walled wall - wallet wallet - wallets wallet - wallon wallon - walloon walloon - wallow wallow - walls wall - walnut walnut - walter walter - wan wan - wand wand - wander wander - wanderer wander - wanderers wander - wandering wander - wanders wander - wands wand - wane wane - waned wane - wanes wane - waning wane - wann wann - want want - wanted want - wanteth wanteth - wanting want - wanton wanton - wantonly wantonli - wantonness wanton - wantons wanton - wants want - wappen wappen - war war - warble warbl - warbling warbl - ward ward - warded ward - warden warden - warder warder - warders warder - wardrobe wardrob - wardrop wardrop - wards ward - ware ware - wares ware - warily warili - warkworth warkworth - warlike warlik - warm warm - warmed warm - warmer warmer - warming warm - warms warm - warmth warmth - warn warn - warned warn - warning warn - warnings warn - warns warn - warp warp - warped warp - warr warr - warrant warrant - warranted warrant - warranteth warranteth - warrantise warrantis - warrantize warrant - warrants warrant - warranty warranti - warren warren - warrener warren - warring war - warrior warrior - warriors warrior - wars war - wart wart - warwick warwick - warwickshire warwickshir - wary wari - was wa - wash wash - washed wash - washer washer - washes wash - washford washford - washing wash - wasp wasp - waspish waspish - wasps wasp - wassail wassail - wassails wassail - wast wast - waste wast - wasted wast - wasteful wast - wasters waster - wastes wast - wasting wast - wat wat - watch watch - watched watch - watchers watcher - watches watch - watchful watch - watching watch - watchings watch - watchman watchman - watchmen watchmen - watchword watchword - water water - waterdrops waterdrop - watered water - waterfly waterfli - waterford waterford - watering water - waterish waterish - waterpots waterpot - waterrugs waterrug - waters water - waterton waterton - watery wateri - wav wav - wave wave - waved wave - waver waver - waverer waver - wavering waver - waves wave - waving wave - waw waw - wawl wawl - wax wax - waxed wax - waxen waxen - waxes wax - waxing wax - way wai - waylaid waylaid - waylay waylai - ways wai - wayward wayward - waywarder wayward - waywardness wayward - we we - weak weak - weaken weaken - weakens weaken - weaker weaker - weakest weakest - weakling weakl - weakly weakli - weakness weak - weal weal - wealsmen wealsmen - wealth wealth - wealthiest wealthiest - wealthily wealthili - wealthy wealthi - wealtlly wealtlli - wean wean - weapon weapon - weapons weapon - wear wear - wearer wearer - wearers wearer - wearied weari - wearies weari - weariest weariest - wearily wearili - weariness weari - wearing wear - wearisome wearisom - wears wear - weary weari - weasel weasel - weather weather - weathercock weathercock - weathers weather - weav weav - weave weav - weaver weaver - weavers weaver - weaves weav - weaving weav - web web - wed wed - wedded wed - wedding wed - wedg wedg - wedged wedg - wedges wedg - wedlock wedlock - wednesday wednesdai - weed weed - weeded weed - weeder weeder - weeding weed - weeds weed - weedy weedi - week week - weeke week - weekly weekli - weeks week - ween ween - weening ween - weep weep - weeper weeper - weeping weep - weepingly weepingli - weepings weep - weeps weep - weet weet - weigh weigh - weighed weigh - weighing weigh - weighs weigh - weight weight - weightier weightier - weightless weightless - weights weight - weighty weighti - weird weird - welcom welcom - welcome welcom - welcomer welcom - welcomes welcom - welcomest welcomest - welfare welfar - welkin welkin - well well - wells well - welsh welsh - welshman welshman - welshmen welshmen - welshwomen welshwomen - wench wench - wenches wench - wenching wench - wend wend - went went - wept wept - weraday weradai - were were - wert wert - west west - western western - westminster westminst - westmoreland westmoreland - westward westward - wet wet - wether wether - wetting wet - wezand wezand - whale whale - whales whale - wharf wharf - wharfs wharf - what what - whate whate - whatever whatev - whatsoe whatso - whatsoever whatsoev - whatsome whatsom - whe whe - wheat wheat - wheaten wheaten - wheel wheel - wheeling wheel - wheels wheel - wheer wheer - wheeson wheeson - wheezing wheez - whelk whelk - whelks whelk - whelm whelm - whelp whelp - whelped whelp - whelps whelp - when when - whenas whena - whence whenc - whencesoever whencesoev - whene whene - whenever whenev - whensoever whensoev - where where - whereabout whereabout - whereas wherea - whereat whereat - whereby wherebi - wherefore wherefor - wherein wherein - whereinto whereinto - whereof whereof - whereon whereon - whereout whereout - whereso whereso - wheresoe whereso - wheresoever wheresoev - wheresome wheresom - whereto whereto - whereuntil whereuntil - whereunto whereunto - whereupon whereupon - wherever wherev - wherewith wherewith - wherewithal wherewith - whet whet - whether whether - whetstone whetston - whetted whet - whew whew - whey whei - which which - whiff whiff - whiffler whiffler - while while - whiles while - whilst whilst - whin whin - whine whine - whined whine - whinid whinid - whining whine - whip whip - whipp whipp - whippers whipper - whipping whip - whips whip - whipster whipster - whipstock whipstock - whipt whipt - whirl whirl - whirled whirl - whirligig whirligig - whirling whirl - whirlpool whirlpool - whirls whirl - whirlwind whirlwind - whirlwinds whirlwind - whisp whisp - whisper whisper - whispering whisper - whisperings whisper - whispers whisper - whist whist - whistle whistl - whistles whistl - whistling whistl - whit whit - white white - whitehall whitehal - whitely white - whiteness white - whiter whiter - whites white - whitest whitest - whither whither - whiting white - whitmore whitmor - whitsters whitster - whitsun whitsun - whittle whittl - whizzing whizz - who who - whoa whoa - whoe whoe - whoever whoever - whole whole - wholesom wholesom - wholesome wholesom - wholly wholli - whom whom - whoobub whoobub - whoop whoop - whooping whoop - whor whor - whore whore - whoremaster whoremast - whoremasterly whoremasterli - whoremonger whoremong - whores whore - whoreson whoreson - whoresons whoreson - whoring whore - whorish whorish - whose whose - whoso whoso - whosoe whoso - whosoever whosoev - why why - wi wi - wick wick - wicked wick - wickednes wickedn - wickedness wicked - wicket wicket - wicky wicki - wid wid - wide wide - widens widen - wider wider - widow widow - widowed widow - widower widow - widowhood widowhood - widows widow - wield wield - wife wife - wight wight - wights wight - wild wild - wildcats wildcat - wilder wilder - wilderness wilder - wildest wildest - wildfire wildfir - wildly wildli - wildness wild - wilds wild - wiles wile - wilful wil - wilfull wilful - wilfully wilfulli - wilfulnes wilfuln - wilfulness wil - will will - willed will - willers willer - willeth willeth - william william - williams william - willing will - willingly willingli - willingness willing - willoughby willoughbi - willow willow - wills will - wilt wilt - wiltshire wiltshir - wimpled wimpl - win win - wince winc - winch winch - winchester winchest - wincot wincot - wind wind - winded wind - windgalls windgal - winding wind - windlasses windlass - windmill windmil - window window - windows window - windpipe windpip - winds wind - windsor windsor - windy windi - wine wine - wing wing - winged wing - wingfield wingfield - wingham wingham - wings wing - wink wink - winking wink - winks wink - winner winner - winners winner - winning win - winnow winnow - winnowed winnow - winnows winnow - wins win - winter winter - winterly winterli - winters winter - wip wip - wipe wipe - wiped wipe - wipes wipe - wiping wipe - wire wire - wires wire - wiry wiri - wisdom wisdom - wisdoms wisdom - wise wise - wiselier wiseli - wisely wise - wiser wiser - wisest wisest - wish wish - wished wish - wisher wisher - wishers wisher - wishes wish - wishest wishest - wisheth wisheth - wishful wish - wishing wish - wishtly wishtli - wisp wisp - wist wist - wit wit - witb witb - witch witch - witchcraft witchcraft - witches witch - witching witch - with with - withal withal - withdraw withdraw - withdrawing withdraw - withdrawn withdrawn - withdrew withdrew - wither wither - withered wither - withering wither - withers wither - withheld withheld - withhold withhold - withholds withhold - within within - withold withold - without without - withstand withstand - withstanding withstand - withstood withstood - witless witless - witness wit - witnesses wit - witnesseth witnesseth - witnessing wit - wits wit - witted wit - wittenberg wittenberg - wittiest wittiest - wittily wittili - witting wit - wittingly wittingli - wittol wittol - wittolly wittolli - witty witti - wiv wiv - wive wive - wived wive - wives wive - wiving wive - wizard wizard - wizards wizard - wo wo - woe woe - woeful woeful - woefull woeful - woefullest woefullest - woes woe - woful woful - wolf wolf - wolfish wolfish - wolsey wolsei - wolves wolv - wolvish wolvish - woman woman - womanhood womanhood - womanish womanish - womankind womankind - womanly womanli - womb womb - wombs womb - womby wombi - women women - won won - woncot woncot - wond wond - wonder wonder - wondered wonder - wonderful wonder - wonderfully wonderfulli - wondering wonder - wonders wonder - wondrous wondrou - wondrously wondrous - wont wont - wonted wont - woo woo - wood wood - woodbine woodbin - woodcock woodcock - woodcocks woodcock - wooden wooden - woodland woodland - woodman woodman - woodmonger woodmong - woods wood - woodstock woodstock - woodville woodvil - wooed woo - wooer wooer - wooers wooer - wooes wooe - woof woof - wooing woo - wooingly wooingli - wool wool - woollen woollen - woolly woolli - woolsack woolsack - woolsey woolsei - woolward woolward - woos woo - wor wor - worcester worcest - word word - words word - wore wore - worins worin - work work - workers worker - working work - workings work - workman workman - workmanly workmanli - workmanship workmanship - workmen workmen - works work - worky worki - world world - worldlings worldl - worldly worldli - worlds world - worm worm - worms worm - wormwood wormwood - wormy wormi - worn worn - worried worri - worries worri - worry worri - worrying worri - worse wors - worser worser - worship worship - worshipful worship - worshipfully worshipfulli - worshipp worshipp - worshipper worshipp - worshippers worshipp - worshippest worshippest - worships worship - worst worst - worsted worst - wort wort - worth worth - worthied worthi - worthier worthier - worthies worthi - worthiest worthiest - worthily worthili - worthiness worthi - worthless worthless - worths worth - worthy worthi - worts wort - wot wot - wots wot - wotting wot - wouid wouid - would would - wouldest wouldest - wouldst wouldst - wound wound - wounded wound - wounding wound - woundings wound - woundless woundless - wounds wound - wouns woun - woven woven - wow wow - wrack wrack - wrackful wrack - wrangle wrangl - wrangler wrangler - wranglers wrangler - wrangling wrangl - wrap wrap - wrapp wrapp - wraps wrap - wrapt wrapt - wrath wrath - wrathful wrath - wrathfully wrathfulli - wraths wrath - wreak wreak - wreakful wreak - wreaks wreak - wreath wreath - wreathed wreath - wreathen wreathen - wreaths wreath - wreck wreck - wrecked wreck - wrecks wreck - wren wren - wrench wrench - wrenching wrench - wrens wren - wrest wrest - wrested wrest - wresting wrest - wrestle wrestl - wrestled wrestl - wrestler wrestler - wrestling wrestl - wretch wretch - wretchcd wretchcd - wretched wretch - wretchedness wretched - wretches wretch - wring wring - wringer wringer - wringing wring - wrings wring - wrinkle wrinkl - wrinkled wrinkl - wrinkles wrinkl - wrist wrist - wrists wrist - writ writ - write write - writer writer - writers writer - writes write - writhled writhl - writing write - writings write - writs writ - written written - wrong wrong - wronged wrong - wronger wronger - wrongful wrong - wrongfully wrongfulli - wronging wrong - wrongly wrongli - wrongs wrong - wronk wronk - wrote wrote - wroth wroth - wrought wrought - wrung wrung - wry wry - wrying wry - wt wt - wul wul - wye wye - x x - xanthippe xanthipp - xi xi - xii xii - xiii xiii - xiv xiv - xv xv - y y - yard yard - yards yard - yare yare - yarely yare - yarn yarn - yaughan yaughan - yaw yaw - yawn yawn - yawning yawn - ycleped yclepe - ycliped yclipe - ye ye - yea yea - yead yead - year year - yearly yearli - yearn yearn - yearns yearn - years year - yeas yea - yeast yeast - yedward yedward - yell yell - yellow yellow - yellowed yellow - yellowing yellow - yellowness yellow - yellows yellow - yells yell - yelping yelp - yeoman yeoman - yeomen yeomen - yerk yerk - yes ye - yesterday yesterdai - yesterdays yesterdai - yesternight yesternight - yesty yesti - yet yet - yew yew - yicld yicld - yield yield - yielded yield - yielder yielder - yielders yielder - yielding yield - yields yield - yok yok - yoke yoke - yoked yoke - yokefellow yokefellow - yokes yoke - yoketh yoketh - yon yon - yond yond - yonder yonder - yongrey yongrei - yore yore - yorick yorick - york york - yorkists yorkist - yorks york - yorkshire yorkshir - you you - young young - younger younger - youngest youngest - youngling youngl - younglings youngl - youngly youngli - younker younker - your your - yours your - yourself yourself - yourselves yourselv - youth youth - youthful youth - youths youth - youtli youtli - zanies zani - zany zani - zeal zeal - zealous zealou - zeals zeal - zed zed - zenelophon zenelophon - zenith zenith - zephyrs zephyr - zir zir - zo zo - zodiac zodiac - zodiacs zodiac - zone zone - zounds zound - zwagger zwagger -} - -# Create a full-text index to use for testing the stemmer. -# -db close -sqlite3 db :memory: -db eval { - CREATE VIRTUAL TABLE t1 USING fts1(word, tokenize Porter); -} - -foreach {pfrom pto} $porter_test_data { - do_test fts1porter-$pfrom { - execsql { - DELETE FROM t1_term; - DELETE FROM t1_content; - INSERT INTO t1(word) VALUES($pfrom); - SELECT term FROM t1_term; - } - } $pto -} - -finish_test diff --git a/test/fts2.test b/test/fts2.test deleted file mode 100644 index b1e2959366..0000000000 --- a/test/fts2.test +++ /dev/null @@ -1,67 +0,0 @@ -# 2008 July 22 -# -# May you do good and not evil. -# May you find forgiveness for yourself and forgive others. -# May you share freely, never taking more than you give. -# -#*********************************************************************** -# This file runs all tests. -# -# $Id: fts2.test,v 1.2 2008/07/23 18:17:32 drh Exp $ - -proc lshift {lvar} { - upvar $lvar l - set ret [lindex $l 0] - set l [lrange $l 1 end] - return $ret -} -while {[set arg [lshift argv]] != ""} { - switch -- $arg { - -sharedpagercache { - sqlite3_enable_shared_cache 1 - } - -soak { - set G(issoak) 1 - } - default { - set argv [linsert $argv 0 $arg] - break - } - } -} - -set testdir [file dirname $argv0] -source $testdir/tester.tcl -# If SQLITE_ENABLE_FTS2 is defined, omit this file. -ifcapable !fts2 { - return -} -rename finish_test really_finish_test -proc finish_test {} {} -set G(isquick) 1 - -set EXCLUDE { - fts2.test -} - -# Files to include in the test. If this list is empty then everything -# that is not in the EXCLUDE list is run. -# -set INCLUDE { -} - -foreach testfile [lsort -dictionary [glob $testdir/fts2*.test]] { - set tail [file tail $testfile] - if {[lsearch -exact $EXCLUDE $tail]>=0} continue - if {[llength $INCLUDE]>0 && [lsearch -exact $INCLUDE $tail]<0} continue - source $testfile - catch {db close} - if {$sqlite_open_file_count>0} { - puts "$tail did not close all files: $sqlite_open_file_count" - fail_test $tail - set sqlite_open_file_count 0 - } -} - -set sqlite_open_file_count 0 -really_finish_test diff --git a/test/fts2a.test b/test/fts2a.test deleted file mode 100644 index 2d1566fcce..0000000000 --- a/test/fts2a.test +++ /dev/null @@ -1,202 +0,0 @@ -# 2006 September 9 -# -# The author disclaims copyright to this source code. In place of -# a legal notice, here is a blessing: -# -# May you do good and not evil. -# May you find forgiveness for yourself and forgive others. -# May you share freely, never taking more than you give. -# -#************************************************************************* -# This file implements regression tests for SQLite library. The -# focus of this script is testing the FTS2 module. -# -# $Id: fts2a.test,v 1.2 2007/05/21 21:59:18 shess Exp $ -# - -set testdir [file dirname $argv0] -source $testdir/tester.tcl - -# If SQLITE_ENABLE_FTS2 is defined, omit this file. -ifcapable !fts2 { - finish_test - return -} - -# Construct a full-text search table containing five keywords: -# one, two, three, four, and five, in various combinations. The -# rowid for each will be a bitmask for the elements it contains. -# -db eval { - CREATE VIRTUAL TABLE t1 USING fts2(content); - INSERT INTO t1(content) VALUES('one'); - INSERT INTO t1(content) VALUES('two'); - INSERT INTO t1(content) VALUES('one two'); - INSERT INTO t1(content) VALUES('three'); - INSERT INTO t1(content) VALUES('one three'); - INSERT INTO t1(content) VALUES('two three'); - INSERT INTO t1(content) VALUES('one two three'); - INSERT INTO t1(content) VALUES('four'); - INSERT INTO t1(content) VALUES('one four'); - INSERT INTO t1(content) VALUES('two four'); - INSERT INTO t1(content) VALUES('one two four'); - INSERT INTO t1(content) VALUES('three four'); - INSERT INTO t1(content) VALUES('one three four'); - INSERT INTO t1(content) VALUES('two three four'); - INSERT INTO t1(content) VALUES('one two three four'); - INSERT INTO t1(content) VALUES('five'); - INSERT INTO t1(content) VALUES('one five'); - INSERT INTO t1(content) VALUES('two five'); - INSERT INTO t1(content) VALUES('one two five'); - INSERT INTO t1(content) VALUES('three five'); - INSERT INTO t1(content) VALUES('one three five'); - INSERT INTO t1(content) VALUES('two three five'); - INSERT INTO t1(content) VALUES('one two three five'); - INSERT INTO t1(content) VALUES('four five'); - INSERT INTO t1(content) VALUES('one four five'); - INSERT INTO t1(content) VALUES('two four five'); - INSERT INTO t1(content) VALUES('one two four five'); - INSERT INTO t1(content) VALUES('three four five'); - INSERT INTO t1(content) VALUES('one three four five'); - INSERT INTO t1(content) VALUES('two three four five'); - INSERT INTO t1(content) VALUES('one two three four five'); -} - -do_test fts2a-1.1 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'one'} -} {1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31} -do_test fts2a-1.2 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'one two'} -} {3 7 11 15 19 23 27 31} -do_test fts2a-1.3 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'two one'} -} {3 7 11 15 19 23 27 31} -do_test fts2a-1.4 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'one two three'} -} {7 15 23 31} -do_test fts2a-1.5 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'one three two'} -} {7 15 23 31} -do_test fts2a-1.6 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'two three one'} -} {7 15 23 31} -do_test fts2a-1.7 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'two one three'} -} {7 15 23 31} -do_test fts2a-1.8 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'three one two'} -} {7 15 23 31} -do_test fts2a-1.9 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'three two one'} -} {7 15 23 31} -do_test fts2a-1.10 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'one two THREE'} -} {7 15 23 31} -do_test fts2a-1.11 { - execsql {SELECT rowid FROM t1 WHERE content MATCH ' ONE Two three '} -} {7 15 23 31} - -do_test fts2a-2.1 { - execsql {SELECT rowid FROM t1 WHERE content MATCH '"one"'} -} {1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31} -do_test fts2a-2.2 { - execsql {SELECT rowid FROM t1 WHERE content MATCH '"one two"'} -} {3 7 11 15 19 23 27 31} -do_test fts2a-2.3 { - execsql {SELECT rowid FROM t1 WHERE content MATCH '"two one"'} -} {} -do_test fts2a-2.4 { - execsql {SELECT rowid FROM t1 WHERE content MATCH '"one two three"'} -} {7 15 23 31} -do_test fts2a-2.5 { - execsql {SELECT rowid FROM t1 WHERE content MATCH '"one three two"'} -} {} -do_test fts2a-2.6 { - execsql {SELECT rowid FROM t1 WHERE content MATCH '"one two three four"'} -} {15 31} -do_test fts2a-2.7 { - execsql {SELECT rowid FROM t1 WHERE content MATCH '"one three two four"'} -} {} -do_test fts2a-2.8 { - execsql {SELECT rowid FROM t1 WHERE content MATCH '"one three five"'} -} {21} -do_test fts2a-2.9 { - execsql {SELECT rowid FROM t1 WHERE content MATCH '"one three" five'} -} {21 29} -do_test fts2a-2.10 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'five "one three"'} -} {21 29} -do_test fts2a-2.11 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'five "one three" four'} -} {29} -do_test fts2a-2.12 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'five four "one three"'} -} {29} -do_test fts2a-2.13 { - execsql {SELECT rowid FROM t1 WHERE content MATCH '"one three" four five'} -} {29} - -do_test fts2a-3.1 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'one'} -} {1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31} -do_test fts2a-3.2 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'one -two'} -} {1 5 9 13 17 21 25 29} -do_test fts2a-3.3 { - execsql {SELECT rowid FROM t1 WHERE content MATCH '-two one'} -} {1 5 9 13 17 21 25 29} - -do_test fts2a-4.1 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'one OR two'} -} {1 2 3 5 6 7 9 10 11 13 14 15 17 18 19 21 22 23 25 26 27 29 30 31} -do_test fts2a-4.2 { - execsql {SELECT rowid FROM t1 WHERE content MATCH '"one two" OR three'} -} {3 4 5 6 7 11 12 13 14 15 19 20 21 22 23 27 28 29 30 31} -do_test fts2a-4.3 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'three OR "one two"'} -} {3 4 5 6 7 11 12 13 14 15 19 20 21 22 23 27 28 29 30 31} -do_test fts2a-4.4 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'one two OR three'} -} {3 5 7 11 13 15 19 21 23 27 29 31} -do_test fts2a-4.5 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'three OR two one'} -} {3 5 7 11 13 15 19 21 23 27 29 31} -do_test fts2a-4.6 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'one two OR three OR four'} -} {3 5 7 9 11 13 15 19 21 23 25 27 29 31} -do_test fts2a-4.7 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'two OR three OR four one'} -} {3 5 7 9 11 13 15 19 21 23 25 27 29 31} - -# Test the ability to handle NULL content -# -do_test fts2a-5.1 { - execsql {INSERT INTO t1(content) VALUES(NULL)} -} {} -do_test fts2a-5.2 { - set rowid [db last_insert_rowid] - execsql {SELECT content FROM t1 WHERE rowid=$rowid} -} {{}} -do_test fts2a-5.3 { - execsql {SELECT rowid FROM t1 WHERE content MATCH NULL} -} {} - -# Test the ability to handle non-positive rowids -# -do_test fts2a-6.0 { - execsql {INSERT INTO t1(rowid, content) VALUES(0, 'four five')} -} {} -do_test fts2a-6.1 { - execsql {SELECT content FROM t1 WHERE rowid = 0} -} {{four five}} -do_test fts2a-6.2 { - execsql {INSERT INTO t1(rowid, content) VALUES(-1, 'three four')} -} {} -do_test fts2a-6.3 { - execsql {SELECT content FROM t1 WHERE rowid = -1} -} {{three four}} -do_test fts2a-6.4 { - execsql {SELECT rowid FROM t1 WHERE t1 MATCH 'four'} -} {-1 0 8 9 10 11 12 13 14 15 24 25 26 27 28 29 30 31} - -finish_test diff --git a/test/fts2b.test b/test/fts2b.test deleted file mode 100644 index 169cd8a0a3..0000000000 --- a/test/fts2b.test +++ /dev/null @@ -1,147 +0,0 @@ -# 2006 September 13 -# -# The author disclaims copyright to this source code. In place of -# a legal notice, here is a blessing: -# -# May you do good and not evil. -# May you find forgiveness for yourself and forgive others. -# May you share freely, never taking more than you give. -# -#************************************************************************* -# This file implements regression tests for SQLite library. The -# focus of this script is testing the FTS2 module. -# -# $Id: fts2b.test,v 1.1 2006/10/19 23:36:26 shess Exp $ -# - -set testdir [file dirname $argv0] -source $testdir/tester.tcl - -# If SQLITE_ENABLE_FTS2 is defined, omit this file. -ifcapable !fts2 { - finish_test - return -} - -# Fill the full-text index "t1" with phrases in english, spanish, -# and german. For the i-th row, fill in the names for the bits -# that are set in the value of i. The least significant bit is -# 1. For example, the value 5 is 101 in binary which will be -# converted to "one three" in english. -# -proc fill_multilanguage_fulltext_t1 {} { - set english {one two three four five} - set spanish {un dos tres cuatro cinco} - set german {eine zwei drei vier funf} - - for {set i 1} {$i<=31} {incr i} { - set cmd "INSERT INTO t1 VALUES" - set vset {} - foreach lang {english spanish german} { - set words {} - for {set j 0; set k 1} {$j<5} {incr j; incr k $k} { - if {$k&$i} {lappend words [lindex [set $lang] $j]} - } - lappend vset "'$words'" - } - set sql "INSERT INTO t1(english,spanish,german) VALUES([join $vset ,])" - # puts $sql - db eval $sql - } -} - -# Construct a full-text search table containing five keywords: -# one, two, three, four, and five, in various combinations. The -# rowid for each will be a bitmask for the elements it contains. -# -db eval { - CREATE VIRTUAL TABLE t1 USING fts2(english,spanish,german); -} -fill_multilanguage_fulltext_t1 - -do_test fts2b-1.1 { - execsql {SELECT rowid FROM t1 WHERE english MATCH 'one'} -} {1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31} -do_test fts2b-1.2 { - execsql {SELECT rowid FROM t1 WHERE spanish MATCH 'one'} -} {} -do_test fts2b-1.3 { - execsql {SELECT rowid FROM t1 WHERE german MATCH 'one'} -} {} -do_test fts2b-1.4 { - execsql {SELECT rowid FROM t1 WHERE t1 MATCH 'one'} -} {1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31} -do_test fts2b-1.5 { - execsql {SELECT rowid FROM t1 WHERE t1 MATCH 'one dos drei'} -} {7 15 23 31} -do_test fts2b-1.6 { - execsql {SELECT english, spanish, german FROM t1 WHERE rowid=1} -} {one un eine} -do_test fts2b-1.7 { - execsql {SELECT rowid FROM t1 WHERE t1 MATCH '"one un"'} -} {} - -do_test fts2b-2.1 { - execsql { - CREATE VIRTUAL TABLE t2 USING fts2(from,to); - INSERT INTO t2([from],[to]) VALUES ('one two three', 'four five six'); - SELECT [from], [to] FROM t2 - } -} {{one two three} {four five six}} - - -# Compute an SQL string that contains the words one, two, three,... to -# describe bits set in the value $i. Only the lower 5 bits are examined. -# -proc wordset {i} { - set x {} - for {set j 0; set k 1} {$j<5} {incr j; incr k $k} { - if {$k&$i} {lappend x [lindex {one two three four five} $j]} - } - return '$x' -} - -# Create a new FTS table with three columns: -# -# norm: words for the bits of rowid -# plusone: words for the bits of rowid+1 -# invert: words for the bits of ~rowid -# -db eval { - CREATE VIRTUAL TABLE t4 USING fts2([norm],'plusone',"invert"); -} -for {set i 1} {$i<=15} {incr i} { - set vset [list [wordset $i] [wordset [expr {$i+1}]] [wordset [expr {~$i}]]] - db eval "INSERT INTO t4(norm,plusone,invert) VALUES([join $vset ,]);" -} - -do_test fts2b-4.1 { - execsql {SELECT rowid FROM t4 WHERE t4 MATCH 'norm:one'} -} {1 3 5 7 9 11 13 15} -do_test fts2b-4.2 { - execsql {SELECT rowid FROM t4 WHERE norm MATCH 'one'} -} {1 3 5 7 9 11 13 15} -do_test fts2b-4.3 { - execsql {SELECT rowid FROM t4 WHERE t4 MATCH 'one'} -} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15} -do_test fts2b-4.4 { - execsql {SELECT rowid FROM t4 WHERE t4 MATCH 'plusone:one'} -} {2 4 6 8 10 12 14} -do_test fts2b-4.5 { - execsql {SELECT rowid FROM t4 WHERE plusone MATCH 'one'} -} {2 4 6 8 10 12 14} -do_test fts2b-4.6 { - execsql {SELECT rowid FROM t4 WHERE t4 MATCH 'norm:one plusone:two'} -} {1 5 9 13} -do_test fts2b-4.7 { - execsql {SELECT rowid FROM t4 WHERE t4 MATCH 'norm:one two'} -} {1 3 5 7 9 11 13 15} -do_test fts2b-4.8 { - execsql {SELECT rowid FROM t4 WHERE t4 MATCH 'plusone:two norm:one'} -} {1 5 9 13} -do_test fts2b-4.9 { - execsql {SELECT rowid FROM t4 WHERE t4 MATCH 'two norm:one'} -} {1 3 5 7 9 11 13 15} - - -finish_test diff --git a/test/fts2c.test b/test/fts2c.test deleted file mode 100644 index cc6c9bbb5d..0000000000 --- a/test/fts2c.test +++ /dev/null @@ -1,1213 +0,0 @@ -# 2006 September 14 -# -# The author disclaims copyright to this source code. In place of -# a legal notice, here is a blessing: -# -# May you do good and not evil. -# May you find forgiveness for yourself and forgive others. -# May you share freely, never taking more than you give. -# -#************************************************************************* -# This file implements regression tests for SQLite library. The -# focus of this script is testing the FTS2 module. -# -# $Id: fts2c.test,v 1.1 2006/10/19 23:36:26 shess Exp $ -# - -set testdir [file dirname $argv0] -source $testdir/tester.tcl - -# If SQLITE_ENABLE_FTS2 is defined, omit this file. -ifcapable !fts2 { - finish_test - return -} - -# Create a table of sample email data. The data comes from email -# archives of Enron executives that was published as part of the -# litigation against that company. -# -do_test fts2c-1.1 { - db eval { - CREATE VIRTUAL TABLE email USING fts2([from],[to],subject,body); - BEGIN TRANSACTION; -INSERT INTO email([from],[to],subject,body) VALUES('savita.puthigai@enron.com', 'traders.eol@enron.com, traders.eol@enron.com', 'EnronOnline- Change to Autohedge', 'Effective Monday, October 22, 2001 the following changes will be made to the Autohedge functionality on EnronOnline. - -The volume on the hedge will now respect the minimum volume and volume increment settings on the parent product. See rules below: - -? If the transaction volume on the child is less than half of the parent''s minimum volume no hedge will occur. -? If the transaction volume on the child is more than half the parent''s minimum volume but less than half the volume increment on the parent, the hedge will volume will be the parent''s minimum volume. -? For all other volumes, the same rounding rules will apply based on the volume increment on the parent product. - -Please see example below: - -Parent''s Settings: -Minimum: 5000 -Increment: 1000 - -Volume on Autohedge transaction Volume Hedged -1 - 2499 0 -2500 - 5499 5000 -5500 - 6499 6000'); -INSERT INTO email([from],[to],subject,body) VALUES('dana.davis@enron.com', 'laynie.east@enron.com, lisa.king@enron.com, lisa.best@enron.com,', 'Leaving Early', 'FYI: -If it''s ok with everyone''s needs, I would like to leave @4pm. If you think -you will need my assistance past the 4 o''clock hour just let me know; I''ll -be more than willing to stay.'); -INSERT INTO email([from],[to],subject,body) VALUES('enron_update@concureworkplace.com', 'louise.kitchen@enron.com', '<<Concur Expense Document>> - CC02.06.02', 'The following expense report is ready for approval: - -Employee Name: Christopher F. Calger -Status last changed by: Mollie E. Gustafson Ms -Expense Report Name: CC02.06.02 -Report Total: $3,972.93 -Amount Due Employee: $3,972.93 - - -To approve this expense report, click on the following link for Concur Expense. -http://expensexms.enron.com'); -INSERT INTO email([from],[to],subject,body) VALUES('jeff.duff@enron.com', 'julie.johnson@enron.com', 'Work request', 'Julie, - -Could you print off the current work request report by 1:30 today? - -Gentlemen, - -I''d like to review this today at 1:30 in our office. Also, could you provide -me with your activity reports so I can have Julie enter this information. - -JD'); -INSERT INTO email([from],[to],subject,body) VALUES('v.weldon@enron.com', 'gary.l.carrier@usa.dupont.com, scott.joyce@bankofamerica.com', 'Enron News', 'This could turn into something big.... -http://biz.yahoo.com/rf/010129/n29305829.html'); -INSERT INTO email([from],[to],subject,body) VALUES('mark.haedicke@enron.com', 'paul.simons@enron.com', 'Re: First Polish Deal!', 'Congrats! Things seem to be building rapidly now on the Continent. Mark'); -INSERT INTO email([from],[to],subject,body) VALUES('e..carter@enron.com', 't..robinson@enron.com', 'FW: Producers Newsletter 9-24-2001', ' -The producer lumber pricing sheet. - -----Original Message----- -From: Johnson, Jay -Sent: Tuesday, October 16, 2001 3:42 PM -To: Carter, Karen E. -Subject: FW: Producers Newsletter 9-24-2001 - - - - -----Original Message----- -From: Daigre, Sergai -Sent: Friday, September 21, 2001 8:33 PM -Subject: Producers Newsletter 9-24-2001 - - '); -INSERT INTO email([from],[to],subject,body) VALUES('david.delainey@enron.com', 'kenneth.lay@enron.com', 'Greater Houston Partnership', 'Ken, in response to the letter from Mr Miguel San Juan, my suggestion would -be to offer up the Falcon for their use; however, given the tight time frame -and your recent visit with Mr. Fox that it would be difficult for either you -or me to participate. - -I spoke to Max and he agrees with this approach. - -I hope this meets with your approval. - -Regards -Delainey'); -INSERT INTO email([from],[to],subject,body) VALUES('lachandra.fenceroy@enron.com', 'lindy.donoho@enron.com', 'FW: Bus Applications Meeting Follow Up', 'Lindy, - -Here is the original memo we discussed earlier. Please provide any information that you may have. - -Your cooperation is greatly appreciated. - -Thanks, - -lachandra.fenceroy@enron.com -713.853.3884 -877.498.3401 Pager - - -----Original Message----- -From: Bisbee, Joanne -Sent: Wednesday, September 26, 2001 7:50 AM -To: Fenceroy, LaChandra -Subject: FW: Bus Applications Meeting Follow Up - -Lachandra, Please get with David Duff today and see what this is about. Who are our TW accounting business users? - - -----Original Message----- -From: Koh, Wendy -Sent: Tuesday, September 25, 2001 2:41 PM -To: Bisbee, Joanne -Subject: Bus Applications Meeting Follow Up - -Lisa brought up a TW change effective Nov 1. It involves eliminating a turnback surcharge. I have no other information, but you might check with the business folks for any system changes required. - -Wendy'); -INSERT INTO email([from],[to],subject,body) VALUES('danny.mccarty@enron.com', 'fran.fagan@enron.com', 'RE: worksheets', 'Fran, - If Julie''s merit needs to be lump sum, just move it over to that column. Also, send me Eric Gadd''s sheets as well. Thanks. -Dan - - -----Original Message----- -From: Fagan, Fran -Sent: Thursday, December 20, 2001 11:10 AM -To: McCarty, Danny -Subject: worksheets - -As discussed, attached are your sheets for bonus and merit. - -Thanks, - -Fran Fagan -Sr. HR Rep -713.853.5219 - - - << File: McCartyMerit.xls >> << File: mccartyBonusCommercial_UnP.xls >> - -'); -INSERT INTO email([from],[to],subject,body) VALUES('bert.meyers@enron.com', 'shift.dl-portland@enron.com', 'OCTOBER SCHEDULE', 'TEAM, - -PLEASE SEND ME ANY REQUESTS THAT YOU HAVE FOR OCTOBER. SO FAR I HAVE THEM FOR LEAF. I WOULD LIKE TO HAVE IT DONE BY THE 15TH OF THE MONTH. ANY QUESTIONS PLEASE GIVE ME A CALL. - -BERT'); -INSERT INTO email([from],[to],subject,body) VALUES('errol.mclaughlin@enron.com', 'john.arnold@enron.com, bilal.bajwa@enron.com, john.griffith@enron.com,', 'TRV Notification: (NG - PROPT P/L - 09/27/2001)', 'The report named: NG - PROPT P/L <http://trv.corp.enron.com/linkFromExcel.asp?report_cd=11&report_name=NG+-+PROPT+P/L&category_cd=5&category_name=FINANCIAL&toc_hide=1&sTV1=5&TV1Exp=Y&current_efct_date=09/27/2001>, published as of 09/27/2001 is now available for viewing on the website.'); -INSERT INTO email([from],[to],subject,body) VALUES('patrice.mims@enron.com', 'calvin.eakins@enron.com', 'Re: Small business supply assistance', 'Hi Calvin - - -I spoke with Rickey (boy, is he long-winded!!). Gave him the name of our -credit guy, Russell Diamond. - -Thank for your help!'); -INSERT INTO email([from],[to],subject,body) VALUES('legal <.hall@enron.com>', 'stephanie.panus@enron.com', 'Termination update', 'City of Vernon and Salt River Project terminated their contracts. I will fax these notices to you.'); -INSERT INTO email([from],[to],subject,body) VALUES('d..steffes@enron.com', 'richard.shapiro@enron.com', 'EES / ENA Government Affairs Staffing & Outside Services', 'Rick -- - -Here is the information on staffing and outside services. Call if you need anything else. - -Jim - - '); -INSERT INTO email([from],[to],subject,body) VALUES('gelliott@industrialinfo.com', 'pcopello@industrialinfo.com', 'ECAAR (Gavin), WSCC (Diablo Canyon), & NPCC (Seabrook)', 'Dear Power Outage Database Customer, -Attached you will find an excel document. The outages contained within are forced or rescheduled outages. Your daily delivery will still contain these outages. -In addition to the two excel documents, there is a dbf file that is formatted like your daily deliveries you receive nightly. This will enable you to load the data into your regular database. Any questions please let me know. Thanks. -Greg Elliott -IIR, Inc. -713-783-5147 x 3481 -outages@industrialinfo.com -THE INFORMATION CONTAINED IN THIS E-MAIL IS LEGALLY PRIVILEGED AND CONFIDENTIAL INFORMATION INTENDED ONLY FOR THE USE OF THE INDIVIDUAL OR ENTITY NAMED ABOVE. YOU ARE HEREBY NOTIFIED THAT ANY DISSEMINATION, DISTRIBUTION, OR COPY OF THIS E-MAIL TO UNAUTHORIZED ENTITIES IS STRICTLY PROHIBITED. IF YOU HAVE RECEIVED THIS -E-MAIL IN ERROR, PLEASE DELETE IT. - - OUTAGE.dbf - - 111201R.xls - - 111201.xls '); -INSERT INTO email([from],[to],subject,body) VALUES('enron.announcements@enron.com', 'all_ena_egm_eim@enron.com', 'EWS Brown Bag', 'MARK YOUR LUNCH CALENDARS NOW ! - -You are invited to attend the EWS Brown Bag Lunch Series - -Featuring: RAY BOWEN, COO - -Topic: Enron Industrial Markets - -Thursday, March 15, 2001 -11:30 am - 12:30 pm -EB 5 C2 - - -You bring your lunch, Limited Seating -We provide drinks and dessert. RSVP x 3-9610'); -INSERT INTO email([from],[to],subject,body) VALUES('chris.germany@enron.com', 'ingrid.immer@williams.com', 'Re: About St Pauls', 'Sounds good to me. I bet this is next to the Warick?? Hotel. - - - - -"Immer, Ingrid" <Ingrid.Immer@Williams.com> on 12/21/2000 11:48:47 AM -To: "''chris.germany@enron.com''" <chris.germany@enron.com> -cc: -Subject: About St Pauls - - - - - <<About St Pauls.url>> -? -?http://www.stpaulshouston.org/about.html - -Chris, - -I like the looks of this place.? What do you think about going here Christmas -eve?? They have an 11:00 a.m. service and a candlelight service at 5:00 p.m., -among others. - -Let me know.?? ii - - - About St Pauls.url - -'); -INSERT INTO email([from],[to],subject,body) VALUES('nas@cpuc.ca.gov', 'skatz@sempratrading.com, kmccrea@sablaw.com, thompson@wrightlaw.com,', 'Reply Brief filed July 31, 2000', ' - CPUC01-#76371-v1-Revised_Reply_Brief__Due_today_7_31_.doc'); -INSERT INTO email([from],[to],subject,body) VALUES('gascontrol@aglresources.com', 'dscott4@enron.com, lcampbel@enron.com', 'Alert Posted 10:00 AM November 20,2000: E-GAS Request Reminder', 'Alert Posted 10:00 AM November 20,2000: E-GAS Request Reminder -As discussed in the Winter Operations Meeting on Sept.29,2000, -E-Gas(Emergency Gas) will not be offered this winter as a service from AGLC. -Marketers and Poolers can receive gas via Peaking and IBSS nominations(daisy -chain) from other marketers up to the 6 p.m. Same Day 2 nomination cycle. -'); -INSERT INTO email([from],[to],subject,body) VALUES('dutch.quigley@enron.com', 'rwolkwitz@powermerchants.com', '', ' - -Here is a goody for you'); -INSERT INTO email([from],[to],subject,body) VALUES('ryan.o''rourke@enron.com', 'k..allen@enron.com, randy.bhatia@enron.com, frank.ermis@enron.com,', 'TRV Notification: (West VaR - 11/07/2001)', 'The report named: West VaR <http://trv.corp.enron.com/linkFromExcel.asp?report_cd=36&report_name=West+VaR&category_cd=2&category_name=WEST&toc_hide=1&sTV1=2&TV1Exp=Y&current_efct_date=11/07/2001>, published as of 11/07/2001 is now available for viewing on the website.'); -INSERT INTO email([from],[to],subject,body) VALUES('mjones7@txu.com', 'cstone1@txu.com, ggreen2@txu.com, timpowell@txu.com,', 'Enron / HPL Actuals for July 10, 2000', 'Teco Tap 10.000 / Enron ; 110.000 / HPL IFERC - -LS HPL LSK IC 30.000 / Enron -'); -INSERT INTO email([from],[to],subject,body) VALUES('susan.pereira@enron.com', 'kkw816@aol.com', 'soccer practice', 'Kathy- - -Is it safe to assume that practice is cancelled for tonight?? - -Susan Pereira'); -INSERT INTO email([from],[to],subject,body) VALUES('mark.whitt@enron.com', 'barry.tycholiz@enron.com', 'Huber Internal Memo', 'Please look at this. I didn''t know how deep to go with the desk. Do you think this works. - - '); -INSERT INTO email([from],[to],subject,body) VALUES('m..forney@enron.com', 'george.phillips@enron.com', '', 'George, -Give me a call and we will further discuss opportunities on the 13st floor. - -Thanks, -JMForney -3-7160'); -INSERT INTO email([from],[to],subject,body) VALUES('brad.mckay@enron.com', 'angusmcka@aol.com', 'Re: (no subject)', 'not yet'); -INSERT INTO email([from],[to],subject,body) VALUES('adam.bayer@enron.com', 'jonathan.mckay@enron.com', 'FW: Curve Fetch File', 'Here is the curve fetch file sent to me. It has plenty of points in it. If you give me a list of which ones you need we may be able to construct a secondary worksheet to vlookup the values. - -adam -35227 - - - -----Original Message----- -From: Royed, Jeff -Sent: Tuesday, September 25, 2001 11:37 AM -To: Bayer, Adam -Subject: Curve Fetch File - -Let me know if it works. It may be required to have a certain version of Oracle for it to work properly. - - - -Jeff Royed -Enron -Energy Operations -Phone: 713-853-5295'); -INSERT INTO email([from],[to],subject,body) VALUES('matt.smith@enron.com', 'yan.wang@enron.com', 'Report Formats', 'Yan, - -The merged reports look great. I believe the only orientation changes are to -"unmerge" the following six reports: - -31 Keystone Receipts -15 Questar Pipeline -40 Rockies Production -22 West_2 -23 West_3 -25 CIG_WIC - -The orientation of the individual reports should be correct. Thanks. - -Mat - -PS. Just a reminder to add the "*" by the title of calculated points.'); -INSERT INTO email([from],[to],subject,body) VALUES('michelle.lokay@enron.com', 'jimboman@bigfoot.com', 'Egyptian Festival', '---------------------- Forwarded by Michelle Lokay/ET&S/Enron on 09/07/2000 -10:08 AM --------------------------- - - -"Karkour, Randa" <Randa.Karkour@COMPAQ.com> on 09/07/2000 09:01:04 AM -To: "''Agheb (E-mail)" <Agheb@aol.com>, "Leila Mankarious (E-mail)" -<Leila_Mankarious@mhhs.org>, "''Marymankarious (E-mail)" -<marymankarious@aol.com>, "Michelle lokay (E-mail)" <mlokay@enron.com>, "Ramy -Mankarious (E-mail)" <Mankarious@aol.com> -cc: - -Subject: Egyptian Festival - - - <<Egyptian Festival.url>> - - http://www.egyptianfestival.com/ - - - Egyptian Festival.url -'); -INSERT INTO email([from],[to],subject,body) VALUES('errol.mclaughlin@enron.com', 'sherry.dawson@enron.com', 'Urgent!!! --- New EAST books', 'This has to be done.................................. - -Thanks ----------------------- Forwarded by Errol McLaughlin/Corp/Enron on 12/20/2000 -08:39 AM --------------------------- - - - - From: William Kelly @ ECT 12/20/2000 08:31 AM - - -To: Kam Keiser/HOU/ECT@ECT, Darron C Giron/HOU/ECT@ECT, David -Baumbach/HOU/ECT@ECT, Errol McLaughlin/Corp/Enron@ENRON -cc: Kimat Singla/HOU/ECT@ECT, Kulvinder Fowler/NA/Enron@ENRON, Kyle R -Lilly/HOU/ECT@ECT, Jeff Royed/Corp/Enron@ENRON, Alejandra -Chavez/NA/Enron@ENRON, Crystal Hyde/HOU/ECT@ECT - -Subject: New EAST books - -We have new book names in TAGG for our intramonth portfolios and it is -extremely important that any deal booked to the East is communicated quickly -to someone on my team. I know it will take some time for the new names to -sink in and I do not want us to miss any positions or P&L. - -Thanks for your help on this. - -New: -Scott Neal : East Northeast -Dick Jenkins: East Marketeast - -WK -'); -INSERT INTO email([from],[to],subject,body) VALUES('david.forster@enron.com', 'eol.wide@enron.com', 'Change to Stack Manager', 'Effective immediately, there is a change to the Stack Manager which will -affect any Inactive Child. - -An inactive Child with links to Parent products will not have their -calculated prices updated until the Child product is Activated. - -When the Child Product is activated, the price will be recalculated and -updated BEFORE it is displayed on the web. - -This means that if you are inputting a basis price on a Child product, you -will not see the final, calculated price until you Activate the product, at -which time the customer will also see it. - -If you have any questions, please contact the Help Desk on: - -Americas: 713 853 4357 -Europe: + 44 (0) 20 7783 7783 -Asia/Australia: +61 2 9229 2300 - -Dave'); -INSERT INTO email([from],[to],subject,body) VALUES('vince.kaminski@enron.com', 'jhh1@email.msn.com', 'Re: Light reading - see pieces beginning on page 7', 'John, - -I saw it. Very interesting. - -Vince - - - - - -"John H Herbert" <jhh1@email.msn.com> on 07/28/2000 08:38:08 AM -To: "Vince J Kaminski" <Vince_J_Kaminski@enron.com> -cc: -Subject: Light reading - see pieces beginning on page 7 - - -Cheers and have a nice weekend, - - -JHHerbert - - - - - - gd000728.pdf - - - -'); -INSERT INTO email([from],[to],subject,body) VALUES('matthew.lenhart@enron.com', 'mmmarcantel@equiva.com', 'RE:', 'i will try to line up a pig for you '); -INSERT INTO email([from],[to],subject,body) VALUES('jae.black@enron.com', 'claudette.harvey@enron.com, chaun.roberts@enron.com, judy.martinez@enron.com,', 'Disaster Recovery Equipment', 'As a reminder...there are several pieces of equipment that are set up on the 30th Floor, as well as on our floor, for the Disaster Recovery Team. PLEASE DO NOT TAKE, BORROW OR USE this equipment. Should you need to use another computer system, other than yours, or make conference calls please work with your Assistant to help find or set up equipment for you to use. - -Thanks for your understanding in this matter. - -T.Jae Black -East Power Trading -Assistant to Kevin Presto -off. 713-853-5800 -fax 713-646-8272 -cell 713-539-4760'); -INSERT INTO email([from],[to],subject,body) VALUES('eric.bass@enron.com', 'dale.neuner@enron.com', '5 X 24', 'Dale, - -Have you heard anything more on the 5 X 24s? We would like to get this -product out ASAP. - - -Thanks, - -Eric'); -INSERT INTO email([from],[to],subject,body) VALUES('messenger@smartreminders.com', 'm..tholt@enron.com', '10% Coupon - PrintPal Printer Cartridges - 100% Guaranteed', '[IMAGE] -[IMAGE][IMAGE][IMAGE] -Dear SmartReminders Member, - [IMAGE] [IMAGE] [IMAGE] [IMAGE] [IMAGE] [IMAGE] [IMAGE] [IMAGE] - - - - - - - - - - - - - - - - - - - - - -We respect your privacy and are a Certified Participant of the BBBOnLine - Privacy Program. To be removed from future offers,click here. -SmartReminders.com is a permission based service. To unsubscribe click here . '); -INSERT INTO email([from],[to],subject,body) VALUES('benjamin.rogers@enron.com', 'mark.bernstein@enron.com', '', 'The guy you are talking about left CIN under a "cloud of suspicion" sort of -speak. He was the one who got into several bad deals and PPA''s in California -for CIN, thus he left on a bad note. Let me know if you need more detail -than that, I felt this was the type of info you were looking for. Thanks! -Ben'); -INSERT INTO email([from],[to],subject,body) VALUES('enron_update@concureworkplace.com', 'michelle.cash@enron.com', 'Expense Report Receipts Not Received', 'Employee Name: Michelle Cash -Report Name: Houston Cellular 8-11-01 -Report Date: 12/13/01 -Report ID: 594D37C9ED2111D5B452 -Submitted On: 12/13/01 - -You are only allowed 2 reports with receipts outstanding. Your expense reports will not be paid until you meet this requirement.'); -INSERT INTO email([from],[to],subject,body) VALUES('susan.mara@enron.com', 'ray.alvarez@enron.com, mark.palmer@enron.com, karen.denne@enron.com,', 'CAISO Emergency Motion -- to discontinue market-based rates for', 'FYI. the latest broadside against the generators. - -Sue Mara -Enron Corp. -Tel: (415) 782-7802 -Fax:(415) 782-7854 ------ Forwarded by Susan J Mara/NA/Enron on 06/08/2001 12:24 PM ----- - - - "Milner, Marcie" <MMilner@coral-energy.com> 06/08/2001 11:13 AM To: "''smara@enron.com''" <smara@enron.com> cc: Subject: CAISO Emergency Motion - - -Sue, did you see this emergency motion the CAISO filed today? Apparently -they are requesting that FERC discontinue market-based rates immediately and -grant refunds plus interest on the difference between cost-based rates and -market revenues received back to May 2000. They are requesting the -commission act within 14 days. Have you heard anything about what they are -doing? - -Marcie - -http://www.caiso.com/docs/2001/06/08/200106081005526469.pdf -'); -INSERT INTO email([from],[to],subject,body) VALUES('fletcher.sturm@enron.com', 'eloy.escobar@enron.com', 'Re: General Brinks Position Meeting', 'Eloy, - -Who is General Brinks? - -Fletch'); -INSERT INTO email([from],[to],subject,body) VALUES('nailia.dindarova@enron.com', 'richard.shapiro@enron.com', 'Documents for Mark Frevert (on EU developments and lessons from', 'Rick, - -Here are the documents that Peter has prepared for Mark Frevert. - -Nailia ----------------------- Forwarded by Nailia Dindarova/LON/ECT on 25/06/2001 -16:36 --------------------------- - - -Nailia Dindarova -25/06/2001 15:36 -To: Michael Brown/Enron@EUEnronXGate -cc: Ross Sankey/Enron@EUEnronXGate, Eric Shaw/ENRON@EUEnronXGate, Peter -Styles/LON/ECT@ECT - -Subject: Documents for Mark Frevert (on EU developments and lessons from -California) - -Michael, - - -These are the documents that Peter promised to give to you for Mark Frevert. -He has now handed them to him in person but asked me to transmit them -electronically to you, as well as Eric and Ross. - -Nailia - - - - - -'); -INSERT INTO email([from],[to],subject,body) VALUES('peggy.a.kostial@accenture.com', 'dave.samuels@enron.com', 'EOL-Accenture Deal Sheet', 'Dave - - -Attached are our comments and suggested changes. Please call to review. - -On the time line for completion, we have four critical steps to complete: - Finalize market analysis to refine business case, specifically - projected revenue stream - Complete counterparty surveying, including targeting 3 CPs for letters - of intent - Review Enron asset base for potential reuse/ licensing - Contract negotiations - -Joe will come back to us with an updated time line, but it is my -expectation that we are still on the same schedule (we just begun week -three) with possibly a week or so slippage.....contract negotiations will -probably be the critical path. - -We will send our cut at the actual time line here shortly. Thanks, - -Peggy - -(See attached file: accenture-dealpoints v2.doc) - - accenture-dealpoints v2.doc '); -INSERT INTO email([from],[to],subject,body) VALUES('thomas.martin@enron.com', 'thomas.martin@enron.com', 'Re: Guadalupe Power Partners LP', '---------------------- Forwarded by Thomas A Martin/HOU/ECT on 03/20/2001 -03:49 PM --------------------------- - - -Thomas A Martin -10/11/2000 03:55 PM -To: Patrick Wade/HOU/ECT@ECT -cc: -Subject: Re: Guadalupe Power Partners LP - -The deal is physically served at Oasis Waha or Oasis Katy and is priced at -either HSC, Waha or Katytailgate GD at buyers option three days prior to -NYMEX close. - -'); -INSERT INTO email([from],[to],subject,body) VALUES('judy.townsend@enron.com', 'dan.junek@enron.com, chris.germany@enron.com', 'Columbia Distribution''s Capacity Available for Release - Sum', '---------------------- Forwarded by Judy Townsend/HOU/ECT on 03/09/2001 11:04 -AM --------------------------- - - -agoddard@nisource.com on 03/08/2001 09:16:57 AM -To: " - *Koch, Kent" <kkoch@nisource.com>, " - -*Millar, Debra" <dmillar@nisource.com>, " - *Burke, Lynn" -<lburke@nisource.com> -cc: " - *Heckathorn, Tom" <theckathorn@nisource.com> -Subject: Columbia Distribution''s Capacity Available for Release - Sum - - -Attached is Columbia Distribution''s notice of capacity available for release -for -the summer of 2001 (Apr. 2001 through Oct. 2001). - -Please note that the deadline for bids is 3:00pm EST on March 20, 2001. - -If you have any questions, feel free to contact any of the representatives -listed -at the bottom of the attachment. - -Aaron Goddard - - - - - - 2001Summer.doc -'); -INSERT INTO email([from],[to],subject,body) VALUES('rhonda.denton@enron.com', 'tim.belden@enron.com, dana.davis@enron.com, genia.fitzgerald@enron.com,', 'Split Rock Energy LLC', 'We have received the executed EEI contract from this CP dated 12/12/2000. -Copies will be distributed to Legal and Credit.'); -INSERT INTO email([from],[to],subject,body) VALUES('kerrymcelroy@dwt.com', 'jack.speer@alcoa.com, crow@millernash.com, michaelearly@earthlink.net,', 'Oral Argument Request', ' - Oral Argument Request.doc'); -INSERT INTO email([from],[to],subject,body) VALUES('mike.carson@enron.com', 'rlmichaelis@hormel.com', '', 'Did you come in town this wk end..... My new number at our house is : -713-668-3712...... my cell # is 281-381-7332 - -the kid'); -INSERT INTO email([from],[to],subject,body) VALUES('cooper.richey@enron.com', 'trycooper@hotmail.com', 'FW: Contact Info', ' - ------Original Message----- -From: Punja, Karim -Sent: Thursday, December 13, 2001 2:35 PM -To: Richey, Cooper -Subject: Contact Info - - -Cooper, - -Its been a real pleasure working with you (even though it was for only a small amount of time) -I hope we can stay in touch. - -Home# 234-0249 -email: kpunja@hotmail.com - -Take Care, - -Karim. - '); -INSERT INTO email([from],[to],subject,body) VALUES('bjm30@earthlink.net', 'mcguinn.k@enron.com, mcguinn.ian@enron.com, mcguinn.stephen@enron.com,', 'email address change', 'Hello all. - -I haven''t talked to many of you via email recently but I do want to give you -my new address for your email file: - - bjm30@earthlink.net - -I hope all is well. - -Brian McGuinn'); -INSERT INTO email([from],[to],subject,body) VALUES('shelley.corman@enron.com', 'steve.hotte@enron.com', 'Flat Panels', 'Can you please advise what is going on with the flat panels that we had planned to distribute to our gas logistics team. It was in the budget and we had the okay, but now I''m hearing there is some hold-up & the units are stored on 44. - -Shelley'); -INSERT INTO email([from],[to],subject,body) VALUES('sara.davidson@enron.com', 'john.schwartzenburg@enron.com, scott.dieball@enron.com, recipients@enron.com,', '2001 Enron Law Conference (Distribution List 2)', ' Enron Law Conference - -San Antonio, Texas May 2-4, 2001 Westin Riverwalk - - See attached memo for more details!! - - -? Registration for the law conference this year will be handled through an -Online RSVP Form on the Enron Law Conference Website at -http://lawconference.corp.enron.com. The website is still under construction -and will not be available until Thursday, March 15, 2001. - -? We will send you another e-mail to confirm when the Law Conference Website -is operational. - -? Please complete the Online RSVP Form as soon as it is available and submit -it no later than Friday, March 30th. - - - - -'); -INSERT INTO email([from],[to],subject,body) VALUES('tori.kuykendall@enron.com', 'heath.b.taylor@accenture.com', 'Re:', 'hey - thats funny about john - he definitely remembers him - i''ll call pat -and let him know - we are coming on saturday - i just havent had a chance to -call you guys back -- looking forward to it -- i probably need the -directions again though'); -INSERT INTO email([from],[to],subject,body) VALUES('darron.giron@enron.com', 'bryce.baxter@enron.com', 'Re: Feedback for Audrey Cook', 'Bryce, - -I''ll get it done today. - -DG 3-9573 - - - - - - From: Bryce Baxter 06/12/2000 07:15 PM - - -To: Darron C Giron/HOU/ECT@ECT -cc: -Subject: Feedback for Audrey Cook - -You were identified as a reviewer for Audrey Cook. If possible, could you -complete her feedback by end of business Wednesday? It will really help me -in the PRC process to have your input. Thanks. - -'); -INSERT INTO email([from],[to],subject,body) VALUES('casey.evans@enron.com', 'stephanie.sever@enron.com', 'Gas EOL ID', 'Stephanie, - -In conjunction with the recent movement of several power traders, they are changing the names of their gas books as well. The names of the new gas books and traders are as follows: - -PWR-NG-LT-SPP: Mike Carson -PWR-NG-LT-SERC: Jeff King - -If you need to know their power desk to map their ID to their gas books, those desks are as follows: - -EPMI-LT-SPP: Mike Carson -EPMI-LT-SERC: Jeff King - -I will be in training this afternoon, but will be back when class is over. Let me know if you have any questions. - -Thanks for your help! -Casey'); -INSERT INTO email([from],[to],subject,body) VALUES('darrell.schoolcraft@enron.com', 'david.roensch@enron.com, kimberly.watson@enron.com, michelle.lokay@enron.com,', 'Postings', 'Please see the attached. - - -ds - - - - - '); -INSERT INTO email([from],[to],subject,body) VALUES('mcominsky@aol.com', 'cpatman@bracepatt.com, james_derrick@enron.com', 'Jurisprudence Luncheon', 'Carrin & Jim -- - -It was an honor and a pleasure to meet both of you yesterday. I know we will -have fun working together on this very special event. - -Jeff left the jurisprudence luncheon lists for me before he left on vacation. - I wasn''t sure whether he transmitted them to you as well. Would you please -advise me if you would like them sent to you? I can email the MS Excel files -or I can fax the hard copies to you. Please advise what is most convenient. - -I plan to be in town through the holidays and can be reached by phone, email, -or cell phone at any time. My cell phone number is 713/705-4829. - -Thanks again for your interest in the ADL''s work. Martin. - -Martin B. Cominsky -Director, Southwest Region -Anti-Defamation League -713/627-3490, ext. 122 -713/627-2011 (fax) -MCominsky@aol.com'); -INSERT INTO email([from],[to],subject,body) VALUES('phillip.love@enron.com', 'todagost@utmb.edu, gbsonnta@utmb.edu', 'New President', 'I had a little bird put a word in my ear. Is there any possibility for Ben -Raimer to be Bush''s secretary of HHS? Just curious about that infamous UTMB -rumor mill. Hope things are well, happy holidays. -PL'); -INSERT INTO email([from],[to],subject,body) VALUES('marie.heard@enron.com', 'ehamilton@fna.com', 'ISDA Master Agreement', 'Erin: - -Pursuant to your request, attached are the Schedule to the ISDA Master Agreement, together with Paragraph 13 to the ISDA Credit Support Annex. Please let me know if you need anything else. We look forward to hearing your comments. - -Marie - -Marie Heard -Senior Legal Specialist -Enron North America Corp. -Phone: (713) 853-3907 -Fax: (713) 646-3490 -marie.heard@enron.com - - '); -INSERT INTO email([from],[to],subject,body) VALUES('andrea.ring@enron.com', 'beverly.beaty@enron.com', 'Re: Tennessee Buy - Louis Dreyfus', 'Beverly - once again thanks so much for your help on this. - - - - '); -INSERT INTO email([from],[to],subject,body) VALUES('karolyn.criado@enron.com', 'j..bonin@enron.com, felicia.case@enron.com, b..clapp@enron.com,', 'Price List week of Oct. 8-9, 2001', ' -Please contact me if you have any questions regarding last weeks prices. - -Thank you, -Karolyn Criado -3-9441 - - - - -'); -INSERT INTO email([from],[to],subject,body) VALUES('kevin.presto@enron.com', 'edward.baughman@enron.com, billy.braddock@enron.com', 'Associated', 'Please begin working on filling our Associated short position in 02. I would like to take this risk off the books. - -In addition, please find out what a buy-out of VEPCO would cost us. With Rogers transitioning to run our retail risk management, I would like to clean up our customer positions. - -We also need to continue to explore a JEA buy-out. - -Thanks.'); -INSERT INTO email([from],[to],subject,body) VALUES('stacy.dickson@enron.com', 'gregg.penman@enron.com', 'RE: Constellation TC 5-7-01', 'Gregg, - -I am at home with a sick baby. (Lots of fun!) I will call you about this -tomorrow. - -Stacy'); -INSERT INTO email([from],[to],subject,body) VALUES('joe.quenet@enron.com', 'dfincher@utilicorp.com', '', 'hey big guy.....check this out..... - - w ww.gorelieberman-2000.com/'); -INSERT INTO email([from],[to],subject,body) VALUES('k..allen@enron.com', 'jacqestc@aol.com', '', 'Jacques, - -I sent you a fax of Kevin Kolb''s comments on the release. The payoff on the note would be $36,248 ($36090(principal) + $158 (accrued interest)). -This is assuming we wrap this up on Tuesday. - -Please email to confirm that their changes are ok so I can set up a meeting on Tuesday to reach closure. - -Phillip'); -INSERT INTO email([from],[to],subject,body) VALUES('kourtney.nelson@enron.com', 'mike.swerzbin@enron.com', 'Adjusted L/R Balance', 'Mike, - -I placed the adjusted L/R Balance on the Enronwest site. It is under the "Staff/Kourtney Nelson". There are two links: - -1) "Adj L_R" is the same data/format from the weekly strategy meeting. -2) "New Gen 2001_2002" link has all of the supply side info that is used to calculate the L/R balance - -Please note the Data Flag column, a value of "3" indicates the project was cancelled, on hold, etc and is not included in the calc. - -Both of these sheets are interactive Excel spreadsheets and thus you can play around with the data as you please. Also, James Bruce is working to get his gen report on the web. That will help with your access to information on new gen. - -Please let me know if you have any questions or feedback, - -Kourtney - - - -Kourtney Nelson -Fundamental Analysis -Enron North America -(503) 464-8280 -kourtney.nelson@enron.com'); -INSERT INTO email([from],[to],subject,body) VALUES('d..thomas@enron.com', 'naveed.ahmed@enron.com', 'FW: Current Enron TCC Portfolio', ' - ------Original Message----- -From: Grace, Rebecca M. -Sent: Monday, December 17, 2001 9:44 AM -To: Thomas, Paul D. -Cc: Cashion, Jim; Allen, Thresa A.; May, Tom -Subject: RE: Current Enron TCC Portfolio - - -Paul, - -I reviewed NY''s list. I agree with all of their contracts numbers and mw amounts. - -Call if you have any more questions. - -Rebecca - - - - -----Original Message----- -From: Thomas, Paul D. -Sent: Monday, December 17, 2001 9:08 AM -To: Grace, Rebecca M. -Subject: FW: Current Enron TCC Portfolio - - << File: enrontccs.xls >> -Rebecca, -Let me know if you see any differences. - -Paul -X 3-0403 ------Original Message----- -From: Thomas, Paul D. -Sent: Monday, December 17, 2001 9:04 AM -To: Ahmed, Naveed -Subject: FW: Current Enron TCC Portfolio - - - - ------Original Message----- -From: Thomas, Paul D. -Sent: Thursday, December 13, 2001 10:01 AM -To: Baughman, Edward D. -Subject: Current Enron TCC Portfolio - - -'); -INSERT INTO email([from],[to],subject,body) VALUES('stephanie.panus@enron.com', 'william.bradford@enron.com, debbie.brackett@enron.com,', 'Coastal Merchant Energy/El Paso Merchant Energy', 'Coastal Merchant Energy, L.P. merged with and into El Paso Merchant Energy, -L.P., effective February 1, 2001, with the surviving entity being El Paso -Merchant Energy, L.P. We currently have ISDA Master Agreements with both -counterparties. Please see the attached memo regarding the existing Masters -and let us know which agreement should be terminated. - -Thanks, -Stephanie -'); -INSERT INTO email([from],[to],subject,body) VALUES('kam.keiser@enron.com', 'c..kenne@enron.com', 'RE: What about this too???', ' - - -----Original Message----- -From: Kenne, Dawn C. -Sent: Wednesday, February 06, 2002 11:50 AM -To: Keiser, Kam -Subject: What about this too??? - - - << File: Netco Trader Matrix.xls >> - '); -INSERT INTO email([from],[to],subject,body) VALUES('chris.meyer@enron.com', 'joe.parks@enron.com', 'Centana', 'Talked to Chip. We do need Cash Committe approval given the netting feature of your deal, which means Batch Funding Request. Please update per my previous e-mail and forward. - -Thanks - -chris -x31666'); -INSERT INTO email([from],[to],subject,body) VALUES('debra.perlingiere@enron.com', 'jworman@academyofhealth.com', '', 'Have a great weekend! Happy Fathers Day! - - -Debra Perlingiere -Enron North America Corp. -1400 Smith Street, EB 3885 -Houston, Texas 77002 -dperlin@enron.com -Phone 713-853-7658 -Fax 713-646-3490'); -INSERT INTO email([from],[to],subject,body) VALUES('outlook.team@enron.com', '', 'Demo by Martha Janousek of Dashboard & Pipeline Profile / Julia &', 'CALENDAR ENTRY: APPOINTMENT - -Description: - Demo by Martha Janousek of Dashboard & Pipeline Profile / Julia & Dir Rpts. - 4102 - -Date: 1/5/2001 -Time: 9:00 AM - 10:00 AM (Central Standard Time) - -Chairperson: Outlook Migration Team - -Detailed Description:'); -INSERT INTO email([from],[to],subject,body) VALUES('diana.seifert@enron.com', 'mark.taylor@enron.com', 'Guest access Chile', 'Hello Mark, - -Justin Boyd told me that your can help me with questions regarding Chile. -We got a request for guest access through MG. -The company is called Escondida and is a subsidiary of BHP Australia. - -Please advise if I can set up a guest account or not. -F.Y.I.: MG is planning to put a "in w/h Chile" contract for Copper on-line as -soon as Enron has done the due diligence for this country. -Thanks ! - - -Best regards - -Diana Seifert -EOL PCG'); -INSERT INTO email([from],[to],subject,body) VALUES('enron_update@concureworkplace.com', 'mark.whitt@enron.com', '<<Concur Expense Document>> - 121001', 'The Approval status has changed on the following report: - -Status last changed by: Barry L. Tycholiz -Expense Report Name: 121001 -Report Total: $198.98 -Amount Due Employee: $198.98 -Amount Approved: $198.98 -Amount Paid: $0.00 -Approval Status: Approved -Payment Status: Pending - - -To review this expense report, click on the following link for Concur Expense. -http://expensexms.enron.com'); -INSERT INTO email([from],[to],subject,body) VALUES('kevin.hyatt@enron.com', '', 'Technical Support', 'Outside the U.S., please refer to the list below: - -Australia: -1800 678-515 -support@palm-au.com - -Canada: -1905 305-6530 -support@palm.com - -New Zealand: -0800 446-398 -support@palm-nz.com - -U.K.: -0171 867 0108 -eurosupport@palm.3com.com - -Please refer to the Worldwide Customer Support card for a complete technical support contact list.'); -INSERT INTO email([from],[to],subject,body) VALUES('geoff.storey@enron.com', 'dutch.quigley@enron.com', 'RE:', 'duke contact? - - -----Original Message----- -From: Quigley, Dutch -Sent: Wednesday, October 31, 2001 10:14 AM -To: Storey, Geoff -Subject: RE: - -bp corp Albert LaMore 281-366-4962 - -running the reports now - - - -----Original Message----- -From: Storey, Geoff -Sent: Wednesday, October 31, 2001 10:10 AM -To: Quigley, Dutch -Subject: RE: - -give me a contact over there too -BP - - - -----Original Message----- -From: Quigley, Dutch -Sent: Wednesday, October 31, 2001 9:42 AM -To: Storey, Geoff -Subject: - -Coral Jeff Whitnah 713-767-5374 -Relaint Steve McGinn 713-207-4000'); -INSERT INTO email([from],[to],subject,body) VALUES('pete.davis@enron.com', 'pete.davis@enron.com', 'Start Date: 4/22/01; HourAhead hour: 3; <CODESITE>', 'Start Date: 4/22/01; HourAhead hour: 3; No ancillary schedules awarded. -Variances detected. -Variances detected in Load schedule. - - LOG MESSAGES: - -PARSING FILE -->> O:\Portland\WestDesk\California Scheduling\ISO Final -Schedules\2001042203.txt - ----- Load Schedule ---- -$$$ Variance found in table tblLoads. - Details: (Hour: 3 / Preferred: 1.92 / Final: 1.89) - TRANS_TYPE: FINAL - LOAD_ID: PGE4 - MKT_TYPE: 2 - TRANS_DATE: 4/22/01 - SC_ID: EPMI - -'); -INSERT INTO email([from],[to],subject,body) VALUES('john.postlethwaite@enron.com', 'john.zufferli@enron.com', 'Reference', 'John, hope things are going well up there for you. The big day is almost here for you and Jessica. I was wondering if I could use your name as a job reference if need be. I am just trying to get everything in order just in case something happens. - -John'); -INSERT INTO email([from],[to],subject,body) VALUES('jeffrey.shankman@enron.com', 'lschiffm@jonesday.com', 'Re:', 'I saw you called on the cell this a.m. Sorry I missed you. (I was in the -shower). I have had a shitty week--I suspect my silence (not only to you, -but others) after our phone call is a result of the week. I''m seeing Glen at -11:15....talk to you'); -INSERT INTO email([from],[to],subject,body) VALUES('litebytz@enron.com', '', 'Lite Bytz RSVP', ' -This week''s Lite Bytz presentation will feature the following TOOLZ speaker: - -Richard McDougall -Solaris 8 -Thursday, June 7, 2001 - -If you have not already signed up, please RSVP via email to litebytz@enron.com by the end of the day Tuesday, June 5, 2001. - -*Remember: this is now a Brown Bag Event--so bring your lunch and we will provide cookies and drinks. - -Click below for more details. - -http://home.enron.com:84/messaging/litebytztoolzprint.jpg'); - COMMIT; - } -} {} - -############################################################################### -# Everything above just builds an interesting test database. The actual -# tests come after this comment. -############################################################################### - -do_test fts2c-1.2 { - execsql { - SELECT rowid FROM email WHERE email MATCH 'mark' - } -} {6 17 25 38 40 42 73 74} -do_test fts2c-1.3 { - execsql { - SELECT rowid FROM email WHERE email MATCH 'susan' - } -} {24 40} -do_test fts2c-1.4 { - execsql { - SELECT rowid FROM email WHERE email MATCH 'mark susan' - } -} {40} -do_test fts2c-1.5 { - execsql { - SELECT rowid FROM email WHERE email MATCH 'susan mark' - } -} {40} -do_test fts2c-1.6 { - execsql { - SELECT rowid FROM email WHERE email MATCH '"mark susan"' - } -} {} -do_test fts2c-1.7 { - execsql { - SELECT rowid FROM email WHERE email MATCH 'mark -susan' - } -} {6 17 25 38 42 73 74} -do_test fts2c-1.8 { - execsql { - SELECT rowid FROM email WHERE email MATCH '-mark susan' - } -} {24} -do_test fts2c-1.9 { - execsql { - SELECT rowid FROM email WHERE email MATCH 'mark OR susan' - } -} {6 17 24 25 38 40 42 73 74} - -# Some simple tests of the automatic "offsets(email)" column. In the sample -# data set above, only one message, number 20, contains the words -# "gas" and "reminder" in both body and subject. -# -do_test fts2c-2.1 { - execsql { - SELECT rowid, offsets(email) FROM email - WHERE email MATCH 'gas reminder' - } -} {20 {2 0 42 3 2 1 54 8 3 0 42 3 3 1 54 8 3 0 129 3 3 0 143 3 3 0 240 3}} -do_test fts2c-2.2 { - execsql { - SELECT rowid, offsets(email) FROM email - WHERE email MATCH 'subject:gas reminder' - } -} {20 {2 0 42 3 2 1 54 8 3 1 54 8}} -do_test fts2c-2.3 { - execsql { - SELECT rowid, offsets(email) FROM email - WHERE email MATCH 'body:gas reminder' - } -} {20 {2 1 54 8 3 0 42 3 3 1 54 8 3 0 129 3 3 0 143 3 3 0 240 3}} -do_test fts2c-2.4 { - execsql { - SELECT rowid, offsets(email) FROM email - WHERE subject MATCH 'gas reminder' - } -} {20 {2 0 42 3 2 1 54 8}} -do_test fts2c-2.5 { - execsql { - SELECT rowid, offsets(email) FROM email - WHERE body MATCH 'gas reminder' - } -} {20 {3 0 42 3 3 1 54 8 3 0 129 3 3 0 143 3 3 0 240 3}} - -# Document 32 contains 5 instances of the world "child". But only -# 3 of them are paired with "product". Make sure only those instances -# that match the phrase appear in the offsets(email) list. -# -do_test fts2c-3.1 { - execsql { - SELECT rowid, offsets(email) FROM email - WHERE body MATCH 'child product' AND +rowid=32 - } -} {32 {3 0 94 5 3 0 114 5 3 0 207 5 3 1 213 7 3 0 245 5 3 1 251 7 3 0 409 5 3 1 415 7 3 1 493 7}} -do_test fts2c-3.2 { - execsql { - SELECT rowid, offsets(email) FROM email - WHERE body MATCH '"child product"' - } -} {32 {3 0 207 5 3 1 213 7 3 0 245 5 3 1 251 7 3 0 409 5 3 1 415 7}} - -# Snippet generator tests -# -do_test fts2c-4.1 { - execsql { - SELECT snippet(email) FROM email - WHERE email MATCH 'subject:gas reminder' - } -} {{Alert Posted 10:00 AM November 20,2000: E-<b>GAS</b> Request <b>Reminder</b>}} -do_test fts2c-4.2 { - execsql { - SELECT snippet(email) FROM email - WHERE email MATCH 'christmas candlelight' - } -} {{<b>...</b> place.? What do you think about going here <b>Christmas</b> -eve?? They have an 11:00 a.m. service and a <b>candlelight</b> service at 5:00 p.m., -among others. <b>...</b>}} - -do_test fts2c-4.3 { - execsql { - SELECT snippet(email) FROM email - WHERE email MATCH 'deal sheet potential reuse' - } -} {{EOL-Accenture <b>Deal</b> <b>Sheet</b> <b>...</b> intent - Review Enron asset base for <b>potential</b> <b>reuse</b>/ licensing - Contract negotiations <b>...</b>}} -do_test fts2c-4.4 { - execsql { - SELECT snippet(email,'<<<','>>>',' ') FROM email - WHERE email MATCH 'deal sheet potential reuse' - } -} {{EOL-Accenture <<<Deal>>> <<<Sheet>>> intent - Review Enron asset base for <<<potential>>> <<<reuse>>>/ licensing - Contract negotiations }} -do_test fts2c-4.5 { - execsql { - SELECT snippet(email,'<<<','>>>',' ') FROM email - WHERE email MATCH 'first things' - } -} {{Re: <<<First>>> Polish Deal! Congrats! <<<Things>>> seem to be building rapidly now on the }} -do_test fts2c-4.6 { - execsql { - SELECT snippet(email) FROM email - WHERE email MATCH 'chris is here' - } -} {{<b>chris</b>.germany@enron.com <b>...</b> Sounds good to me. I bet this <b>is</b> next to the Warick?? Hotel. <b>...</b> place.? What do you think about going <b>here</b> Christmas -eve?? They have an 11:00 a.m. <b>...</b>}} -do_test fts2c-4.7 { - execsql { - SELECT snippet(email) FROM email - WHERE email MATCH '"pursuant to"' - } -} {{Erin: - -<b>Pursuant</b> <b>to</b> your request, attached are the Schedule to <b>...</b>}} -do_test fts2c-4.8 { - execsql { - SELECT snippet(email) FROM email - WHERE email MATCH 'ancillary load davis' - } -} {{pete.<b>davis</b>@enron.com <b>...</b> Start Date: 4/22/01; HourAhead hour: 3; No <b>ancillary</b> schedules awarded. -Variances detected. -Variances detected in <b>Load</b> schedule. - - LOG MESSAGES: - -PARSING <b>...</b>}} - -# Combinations of AND and OR operators: -# -do_test fts2c-5.1 { - execsql { - SELECT snippet(email) FROM email - WHERE email MATCH 'questar enron OR com' - } -} {{matt.smith@<b>enron</b>.<b>com</b> <b>...</b> six reports: - -31 Keystone Receipts -15 <b>Questar</b> Pipeline -40 Rockies Production -22 West_2 <b>...</b>}} -do_test fts2c-5.2 { - execsql { - SELECT snippet(email) FROM email - WHERE email MATCH 'enron OR com questar' - } -} {{matt.smith@<b>enron</b>.<b>com</b> <b>...</b> six reports: - -31 Keystone Receipts -15 <b>Questar</b> Pipeline -40 Rockies Production -22 West_2 <b>...</b>}} - -finish_test diff --git a/test/fts2d.test b/test/fts2d.test deleted file mode 100644 index d8090d8f0c..0000000000 --- a/test/fts2d.test +++ /dev/null @@ -1,65 +0,0 @@ -# 2006 October 1 -# -# The author disclaims copyright to this source code. In place of -# a legal notice, here is a blessing: -# -# May you do good and not evil. -# May you find forgiveness for yourself and forgive others. -# May you share freely, never taking more than you give. -# -#************************************************************************* -# This file implements regression tests for SQLite library. The -# focus of this script is testing the FTS2 module, and in particular -# the Porter stemmer. -# -# $Id: fts2d.test,v 1.1 2006/10/19 23:36:26 shess Exp $ -# - -set testdir [file dirname $argv0] -source $testdir/tester.tcl - -# If SQLITE_ENABLE_FTS2 is defined, omit this file. -ifcapable !fts2 { - finish_test - return -} - -do_test fts2d-1.1 { - execsql { - CREATE VIRTUAL TABLE t1 USING fts2(content, tokenize porter); - INSERT INTO t1(rowid, content) VALUES(1, 'running and jumping'); - SELECT rowid FROM t1 WHERE content MATCH 'run jump'; - } -} {1} -do_test fts2d-1.2 { - execsql { - SELECT snippet(t1) FROM t1 WHERE t1 MATCH 'run jump'; - } -} {{<b>running</b> and <b>jumping</b>}} -do_test fts2d-1.3 { - execsql { - INSERT INTO t1(rowid, content) - VALUES(2, 'abcdefghijklmnopqrstuvwyxz'); - SELECT rowid, snippet(t1) FROM t1 WHERE t1 MATCH 'abcdefghijqrstuvwyxz' - } -} {2 <b>abcdefghijklmnopqrstuvwyxz</b>} -do_test fts2d-1.4 { - execsql { - SELECT rowid, snippet(t1) FROM t1 WHERE t1 MATCH 'abcdefghijXXXXqrstuvwyxz' - } -} {2 <b>abcdefghijklmnopqrstuvwyxz</b>} -do_test fts2d-1.5 { - execsql { - INSERT INTO t1(rowid, content) - VALUES(3, 'The value is 123456789'); - SELECT rowid, snippet(t1) FROM t1 WHERE t1 MATCH '123789' - } -} {3 {The value is <b>123456789</b>}} -do_test fts2d-1.6 { - execsql { - SELECT rowid, snippet(t1) FROM t1 WHERE t1 MATCH '123000000789' - } -} {3 {The value is <b>123456789</b>}} - - -finish_test diff --git a/test/fts2e.test b/test/fts2e.test deleted file mode 100644 index 71845acdde..0000000000 --- a/test/fts2e.test +++ /dev/null @@ -1,85 +0,0 @@ -# 2006 October 19 -# -# The author disclaims copyright to this source code. -# -#************************************************************************* -# This file implements regression tests for SQLite library. The -# focus of this script is testing deletions in the FTS2 module. -# -# $Id: fts2e.test,v 1.1 2006/10/19 23:36:26 shess Exp $ -# - -set testdir [file dirname $argv0] -source $testdir/tester.tcl - -# If SQLITE_ENABLE_FTS2 is defined, omit this file. -ifcapable !fts2 { - finish_test - return -} - -# Construct a full-text search table containing keywords which are the -# ordinal numbers of the bit positions set for a sequence of integers, -# which are used for the rowid. There are a total of 30 INSERT and -# DELETE statements, so that we'll test both the segmentMerge() merge -# (over the first 16) and the termSelect() merge (over the level-1 -# segment and 14 level-0 segments). -db eval { - CREATE VIRTUAL TABLE t1 USING fts2(content); - INSERT INTO t1 (rowid, content) VALUES(1, 'one'); - INSERT INTO t1 (rowid, content) VALUES(2, 'two'); - INSERT INTO t1 (rowid, content) VALUES(3, 'one two'); - INSERT INTO t1 (rowid, content) VALUES(4, 'three'); - DELETE FROM t1 WHERE rowid = 1; - INSERT INTO t1 (rowid, content) VALUES(5, 'one three'); - INSERT INTO t1 (rowid, content) VALUES(6, 'two three'); - INSERT INTO t1 (rowid, content) VALUES(7, 'one two three'); - DELETE FROM t1 WHERE rowid = 4; - INSERT INTO t1 (rowid, content) VALUES(8, 'four'); - INSERT INTO t1 (rowid, content) VALUES(9, 'one four'); - INSERT INTO t1 (rowid, content) VALUES(10, 'two four'); - DELETE FROM t1 WHERE rowid = 7; - INSERT INTO t1 (rowid, content) VALUES(11, 'one two four'); - INSERT INTO t1 (rowid, content) VALUES(12, 'three four'); - INSERT INTO t1 (rowid, content) VALUES(13, 'one three four'); - DELETE FROM t1 WHERE rowid = 10; - INSERT INTO t1 (rowid, content) VALUES(14, 'two three four'); - INSERT INTO t1 (rowid, content) VALUES(15, 'one two three four'); - INSERT INTO t1 (rowid, content) VALUES(16, 'five'); - DELETE FROM t1 WHERE rowid = 13; - INSERT INTO t1 (rowid, content) VALUES(17, 'one five'); - INSERT INTO t1 (rowid, content) VALUES(18, 'two five'); - INSERT INTO t1 (rowid, content) VALUES(19, 'one two five'); - DELETE FROM t1 WHERE rowid = 16; - INSERT INTO t1 (rowid, content) VALUES(20, 'three five'); - INSERT INTO t1 (rowid, content) VALUES(21, 'one three five'); - INSERT INTO t1 (rowid, content) VALUES(22, 'two three five'); - DELETE FROM t1 WHERE rowid = 19; - DELETE FROM t1 WHERE rowid = 22; -} - -do_test fts2f-1.1 { - execsql {SELECT COUNT(*) FROM t1} -} {14} - -do_test fts2e-2.1 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'one'} -} {3 5 9 11 15 17 21} - -do_test fts2e-2.2 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'two'} -} {2 3 6 11 14 15 18} - -do_test fts2e-2.3 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'three'} -} {5 6 12 14 15 20 21} - -do_test fts2e-2.4 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'four'} -} {8 9 11 12 14 15} - -do_test fts2e-2.5 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'five'} -} {17 18 20 21} - -finish_test diff --git a/test/fts2f.test b/test/fts2f.test deleted file mode 100644 index 49cff14664..0000000000 --- a/test/fts2f.test +++ /dev/null @@ -1,90 +0,0 @@ -# 2006 October 19 -# -# The author disclaims copyright to this source code. -# -#************************************************************************* -# This file implements regression tests for SQLite library. The -# focus of this script is testing updates in the FTS2 module. -# -# $Id: fts2f.test,v 1.2 2007/02/23 00:14:06 shess Exp $ -# - -set testdir [file dirname $argv0] -source $testdir/tester.tcl - -# If SQLITE_ENABLE_FTS2 is defined, omit this file. -ifcapable !fts2 { - finish_test - return -} - -# Construct a full-text search table containing keywords which are the -# ordinal numbers of the bit positions set for a sequence of integers, -# which are used for the rowid. There are a total of 31 INSERT, -# UPDATE, and DELETE statements, so that we'll test both the -# segmentMerge() merge (over the first 16) and the termSelect() merge -# (over the level-1 segment and 15 level-0 segments). -db eval { - CREATE VIRTUAL TABLE t1 USING fts2(content); - INSERT INTO t1 (rowid, content) VALUES(1, 'one'); - INSERT INTO t1 (rowid, content) VALUES(2, 'two'); - INSERT INTO t1 (rowid, content) VALUES(3, 'one two'); - INSERT INTO t1 (rowid, content) VALUES(4, 'three'); - INSERT INTO t1 (rowid, content) VALUES(5, 'one three'); - INSERT INTO t1 (rowid, content) VALUES(6, 'two three'); - INSERT INTO t1 (rowid, content) VALUES(7, 'one two three'); - DELETE FROM t1 WHERE rowid = 4; - INSERT INTO t1 (rowid, content) VALUES(8, 'four'); - UPDATE t1 SET content = 'update one three' WHERE rowid = 1; - INSERT INTO t1 (rowid, content) VALUES(9, 'one four'); - INSERT INTO t1 (rowid, content) VALUES(10, 'two four'); - DELETE FROM t1 WHERE rowid = 7; - INSERT INTO t1 (rowid, content) VALUES(11, 'one two four'); - INSERT INTO t1 (rowid, content) VALUES(12, 'three four'); - INSERT INTO t1 (rowid, content) VALUES(13, 'one three four'); - DELETE FROM t1 WHERE rowid = 10; - INSERT INTO t1 (rowid, content) VALUES(14, 'two three four'); - INSERT INTO t1 (rowid, content) VALUES(15, 'one two three four'); - UPDATE t1 SET content = 'update two five' WHERE rowid = 8; - INSERT INTO t1 (rowid, content) VALUES(16, 'five'); - DELETE FROM t1 WHERE rowid = 13; - INSERT INTO t1 (rowid, content) VALUES(17, 'one five'); - INSERT INTO t1 (rowid, content) VALUES(18, 'two five'); - INSERT INTO t1 (rowid, content) VALUES(19, 'one two five'); - DELETE FROM t1 WHERE rowid = 16; - INSERT INTO t1 (rowid, content) VALUES(20, 'three five'); - INSERT INTO t1 (rowid, content) VALUES(21, 'one three five'); - INSERT INTO t1 (rowid, content) VALUES(22, 'two three five'); - DELETE FROM t1 WHERE rowid = 19; - UPDATE t1 SET content = 'update' WHERE rowid = 15; -} - -do_test fts2f-1.1 { - execsql {SELECT COUNT(*) FROM t1} -} {16} - -do_test fts2f-2.0 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'update'} -} {1 8 15} - -do_test fts2f-2.1 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'one'} -} {1 3 5 9 11 17 21} - -do_test fts2f-2.2 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'two'} -} {2 3 6 8 11 14 18 22} - -do_test fts2f-2.3 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'three'} -} {1 5 6 12 14 20 21 22} - -do_test fts2f-2.4 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'four'} -} {9 11 12 14} - -do_test fts2f-2.5 { - execsql {SELECT rowid FROM t1 WHERE content MATCH 'five'} -} {8 17 18 20 21 22} - -finish_test diff --git a/test/fts2g.test b/test/fts2g.test deleted file mode 100644 index 4cffb91fc7..0000000000 --- a/test/fts2g.test +++ /dev/null @@ -1,93 +0,0 @@ -# 2006 October 19 -# -# The author disclaims copyright to this source code. -# -#************************************************************************* -# This file implements regression tests for SQLite library. The focus -# of this script is testing handling of edge cases for various doclist -# merging functions in the FTS2 module query logic. -# -# $Id: fts2g.test,v 1.3 2007/11/16 00:23:08 shess Exp $ -# - -set testdir [file dirname $argv0] -source $testdir/tester.tcl - -# If SQLITE_ENABLE_FTS2 is defined, omit this file. -ifcapable !fts2 { - finish_test - return -} - -db eval { - CREATE VIRTUAL TABLE t1 USING fts2(content); - INSERT INTO t1 (rowid, content) VALUES(1, 'this is a test'); - INSERT INTO t1 (rowid, content) VALUES(2, 'also a test'); -} - -# No hits at all. Returns empty doclists from termSelect(). -do_test fts2g-1.1 { - execsql {SELECT rowid FROM t1 WHERE t1 MATCH 'something'} -} {} - -# Empty left in docListExceptMerge(). -do_test fts2g-1.2 { - execsql {SELECT rowid FROM t1 WHERE t1 MATCH '-this something'} -} {} - -# Empty right in docListExceptMerge(). -do_test fts2g-1.3 { - execsql {SELECT rowid FROM t1 WHERE t1 MATCH 'this -something'} -} {1} - -# Empty left in docListPhraseMerge(). -do_test fts2g-1.4 { - execsql {SELECT rowid FROM t1 WHERE t1 MATCH '"this something"'} -} {} - -# Empty right in docListPhraseMerge(). -do_test fts2g-1.5 { - execsql {SELECT rowid FROM t1 WHERE t1 MATCH '"something is"'} -} {} - -# Empty left in docListOrMerge(). -do_test fts2g-1.6 { - execsql {SELECT rowid FROM t1 WHERE t1 MATCH 'something OR this'} -} {1} - -# Empty right in docListOrMerge(). -do_test fts2g-1.7 { - execsql {SELECT rowid FROM t1 WHERE t1 MATCH 'this OR something'} -} {1} - -# Empty left in docListAndMerge(). -do_test fts2g-1.8 { - execsql {SELECT rowid FROM t1 WHERE t1 MATCH 'something this'} -} {} - -# Empty right in docListAndMerge(). -do_test fts2g-1.9 { - execsql {SELECT rowid FROM t1 WHERE t1 MATCH 'this something'} -} {} - -# No support for all-except queries. -do_test fts2g-1.10 { - catchsql {SELECT rowid FROM t1 WHERE t1 MATCH '-this -something'} -} {1 {SQL logic error or missing database}} - -# Test that docListOrMerge() correctly handles reaching the end of one -# doclist before it reaches the end of the other. -do_test fts2g-1.11 { - execsql {SELECT rowid FROM t1 WHERE t1 MATCH 'this OR also'} -} {1 2} -do_test fts2g-1.12 { - execsql {SELECT rowid FROM t1 WHERE t1 MATCH 'also OR this'} -} {1 2} - -# Empty left and right in docListOrMerge(). Each term matches neither -# row, and when combined there was an assertion failure. -do_test fts2g-1.13 { - execsql {SELECT rowid FROM t1 WHERE t1 MATCH 'something OR nothing'} -} {} - -finish_test diff --git a/test/fts2h.test b/test/fts2h.test deleted file mode 100644 index 72561d85bc..0000000000 --- a/test/fts2h.test +++ /dev/null @@ -1,76 +0,0 @@ -# 2006 October 31 (scaaarey) -# -# The author disclaims copyright to this source code. -# -#************************************************************************* -# This file implements regression tests for SQLite library. The focus -# here is testing correct handling of excessively long terms. -# -# $Id: fts2h.test,v 1.1 2006/11/29 21:03:01 shess Exp $ -# - -set testdir [file dirname $argv0] -source $testdir/tester.tcl - -# If SQLITE_ENABLE_FTS2 is defined, omit this file. -ifcapable !fts2 { - finish_test - return -} - -# Generate a term of len copies of char. -proc bigterm {char len} { - for {set term ""} {$len>0} {incr len -1} { - append term $char - } - return $term -} - -# Generate a document of bigterms based on characters from the list -# chars. -proc bigtermdoc {chars len} { - set doc "" - foreach char $chars { - append doc " " [bigterm $char $len] - } - return $doc -} - -set len 5000 -set doc1 [bigtermdoc {a b c d} $len] -set doc2 [bigtermdoc {b d e f} $len] -set doc3 [bigtermdoc {a c e} $len] - -set aterm [bigterm a $len] -set bterm [bigterm b $len] -set xterm [bigterm x $len] - -db eval { - CREATE VIRTUAL TABLE t1 USING fts2(content); - INSERT INTO t1 (rowid, content) VALUES(1, $doc1); - INSERT INTO t1 (rowid, content) VALUES(2, $doc2); - INSERT INTO t1 (rowid, content) VALUES(3, $doc3); -} - -# No hits at all. Returns empty doclists from termSelect(). -do_test fts2h-1.1 { - execsql {SELECT rowid FROM t1 WHERE t1 MATCH 'something'} -} {} - -do_test fts2h-1.2 { - execsql {SELECT rowid FROM t1 WHERE t1 MATCH $aterm} -} {1 3} - -do_test fts2h-1.2 { - execsql {SELECT rowid FROM t1 WHERE t1 MATCH $xterm} -} {} - -do_test fts2h-1.3 { - execsql "SELECT rowid FROM t1 WHERE t1 MATCH '$aterm -$xterm'" -} {1 3} - -do_test fts2h-1.4 { - execsql "SELECT rowid FROM t1 WHERE t1 MATCH '\"$aterm $bterm\"'" -} {1} - -finish_test diff --git a/test/fts2i.test b/test/fts2i.test deleted file mode 100644 index e732e6a8a9..0000000000 --- a/test/fts2i.test +++ /dev/null @@ -1,87 +0,0 @@ -# 2007 January 17 -# -# The author disclaims copyright to this source code. -# -#************************************************************************* -# This file implements regression tests for SQLite fts2 library. The -# focus here is testing handling of UPDATE when using UTF-16-encoded -# databases. -# -# $Id: fts2i.test,v 1.2 2007/01/24 03:46:35 drh Exp $ -# - -set testdir [file dirname $argv0] -source $testdir/tester.tcl - -# If SQLITE_ENABLE_FTS2 is defined, omit this file. -ifcapable !fts2 { - finish_test - return -} - -# Return the UTF-16 representation of the supplied UTF-8 string $str. -# If $nt is true, append two 0x00 bytes as a nul terminator. -# NOTE(shess) Copied from capi3.test. -proc utf16 {str {nt 1}} { - set r [encoding convertto unicode $str] - if {$nt} { - append r "\x00\x00" - } - return $r -} - -db eval { - PRAGMA encoding = "UTF-16le"; - CREATE VIRTUAL TABLE t1 USING fts2(content); -} - -do_test fts2i-1.0 { - execsql {PRAGMA encoding} -} {UTF-16le} - -do_test fts2i-1.1 { - execsql {INSERT INTO t1 (rowid, content) VALUES(1, 'one')} - execsql {SELECT content FROM t1 WHERE rowid = 1} -} {one} - -do_test fts2i-1.2 { - set sql "INSERT INTO t1 (rowid, content) VALUES(2, 'two')" - set STMT [sqlite3_prepare $DB $sql -1 TAIL] - sqlite3_step $STMT - sqlite3_finalize $STMT - execsql {SELECT content FROM t1 WHERE rowid = 2} -} {two} - -do_test fts2i-1.3 { - set sql "INSERT INTO t1 (rowid, content) VALUES(3, 'three')" - set STMT [sqlite3_prepare $DB $sql -1 TAIL] - sqlite3_step $STMT - sqlite3_finalize $STMT - set sql "UPDATE t1 SET content = 'trois' WHERE rowid = 3" - set STMT [sqlite3_prepare $DB $sql -1 TAIL] - sqlite3_step $STMT - sqlite3_finalize $STMT - execsql {SELECT content FROM t1 WHERE rowid = 3} -} {trois} - -do_test fts2i-1.4 { - set sql16 [utf16 {INSERT INTO t1 (rowid, content) VALUES(4, 'four')}] - set STMT [sqlite3_prepare16 $DB $sql16 -1 TAIL] - sqlite3_step $STMT - sqlite3_finalize $STMT - execsql {SELECT content FROM t1 WHERE rowid = 4} -} {four} - -do_test fts2i-1.5 { - set sql16 [utf16 {INSERT INTO t1 (rowid, content) VALUES(5, 'five')}] - set STMT [sqlite3_prepare16 $DB $sql16 -1 TAIL] - sqlite3_step $STMT - sqlite3_finalize $STMT - set sql "UPDATE t1 SET content = 'cinq' WHERE rowid = 5" - set STMT [sqlite3_prepare $DB $sql -1 TAIL] - sqlite3_step $STMT - sqlite3_finalize $STMT - execsql {SELECT content FROM t1 WHERE rowid = 5} -} {cinq} - -finish_test diff --git a/test/fts2j.test b/test/fts2j.test deleted file mode 100644 index a8a2c07c18..0000000000 --- a/test/fts2j.test +++ /dev/null @@ -1,89 +0,0 @@ -# 2007 February 6 -# -# The author disclaims copyright to this source code. -# -#************************************************************************* -# This file implements regression tests for SQLite library. This -# tests creating fts2 tables in an attached database. -# -# $Id: fts2j.test,v 1.1 2007/02/07 01:01:18 shess Exp $ -# - -set testdir [file dirname $argv0] -source $testdir/tester.tcl - -# If SQLITE_ENABLE_FTS2 is defined, omit this file. -ifcapable !fts2 { - finish_test - return -} - -# Clean up anything left over from a previous pass. -forcedelete test2.db -forcedelete test2.db-journal -sqlite3 db2 test2.db - -db eval { - CREATE VIRTUAL TABLE t3 USING fts2(content); - INSERT INTO t3 (rowid, content) VALUES(1, "hello world"); -} - -db2 eval { - CREATE VIRTUAL TABLE t1 USING fts2(content); - INSERT INTO t1 (rowid, content) VALUES(1, "hello world"); - INSERT INTO t1 (rowid, content) VALUES(2, "hello there"); - INSERT INTO t1 (rowid, content) VALUES(3, "cruel world"); -} - -# This has always worked because the t1_* tables used by fts2 will be -# the defaults. -do_test fts2j-1.1 { - execsql { - ATTACH DATABASE 'test2.db' AS two; - SELECT rowid FROM t1 WHERE t1 MATCH 'hello'; - DETACH DATABASE two; - } -} {1 2} -# Make certain we're detached if there was an error. -catch {db eval {DETACH DATABASE two}} - -# In older code, this appears to work fine, but the t2_* tables used -# by fts2 will be created in database 'main' instead of database -# 'two'. It appears to work fine because the tables end up being the -# defaults, but obviously is badly broken if you hope to use things -# other than in the exact same ATTACH setup. -do_test fts2j-1.2 { - execsql { - ATTACH DATABASE 'test2.db' AS two; - CREATE VIRTUAL TABLE two.t2 USING fts2(content); - INSERT INTO t2 (rowid, content) VALUES(1, "hello world"); - INSERT INTO t2 (rowid, content) VALUES(2, "hello there"); - INSERT INTO t2 (rowid, content) VALUES(3, "cruel world"); - SELECT rowid FROM t2 WHERE t2 MATCH 'hello'; - DETACH DATABASE two; - } -} {1 2} -catch {db eval {DETACH DATABASE two}} - -# In older code, this broke because the fts2 code attempted to create -# t3_* tables in database 'main', but they already existed. Normally -# this wouldn't happen without t3 itself existing, in which case the -# fts2 code would never be called in the first place. -do_test fts2j-1.3 { - execsql { - ATTACH DATABASE 'test2.db' AS two; - - CREATE VIRTUAL TABLE two.t3 USING fts2(content); - INSERT INTO two.t3 (rowid, content) VALUES(2, "hello there"); - INSERT INTO two.t3 (rowid, content) VALUES(3, "cruel world"); - SELECT rowid FROM two.t3 WHERE t3 MATCH 'hello'; - - DETACH DATABASE two; - } db2 -} {2} -catch {db eval {DETACH DATABASE two}} - -catch {db2 close} -forcedelete test2.db - -finish_test diff --git a/test/fts2k.test b/test/fts2k.test deleted file mode 100644 index e7d5f0dff4..0000000000 --- a/test/fts2k.test +++ /dev/null @@ -1,105 +0,0 @@ -# 2007 March 9 -# -# The author disclaims copyright to this source code. -# -#************************************************************************* -# This file implements regression tests for SQLite library. These -# make sure that fts2 insertion buffering is fully transparent when -# using transactions. -# -# $Id: fts2k.test,v 1.2 2007/08/10 23:47:04 shess Exp $ -# - -set testdir [file dirname $argv0] -source $testdir/tester.tcl - -# If SQLITE_ENABLE_FTS2 is defined, omit this file. -ifcapable !fts2 { - finish_test - return -} - -db eval { - CREATE VIRTUAL TABLE t1 USING fts2(content); - INSERT INTO t1 (rowid, content) VALUES(1, "hello world"); - INSERT INTO t1 (rowid, content) VALUES(2, "hello there"); - INSERT INTO t1 (rowid, content) VALUES(3, "cruel world"); -} - -# Test that possibly-buffered inserts went through after commit. -do_test fts2k-1.1 { - execsql { - BEGIN TRANSACTION; - INSERT INTO t1 (rowid, content) VALUES(4, "false world"); - INSERT INTO t1 (rowid, content) VALUES(5, "false door"); - COMMIT TRANSACTION; - SELECT rowid FROM t1 WHERE t1 MATCH 'world'; - } -} {1 3 4} - -# Test that buffered inserts are seen by selects in the same -# transaction. -do_test fts2k-1.2 { - execsql { - BEGIN TRANSACTION; - INSERT INTO t1 (rowid, content) VALUES(6, "another world"); - INSERT INTO t1 (rowid, content) VALUES(7, "another test"); - SELECT rowid FROM t1 WHERE t1 MATCH 'world'; - COMMIT TRANSACTION; - } -} {1 3 4 6} - -# Test that buffered inserts are seen within a transaction. This is -# really the same test as 1.2. -do_test fts2k-1.3 { - execsql { - BEGIN TRANSACTION; - INSERT INTO t1 (rowid, content) VALUES(8, "second world"); - INSERT INTO t1 (rowid, content) VALUES(9, "second sight"); - SELECT rowid FROM t1 WHERE t1 MATCH 'world'; - ROLLBACK TRANSACTION; - } -} {1 3 4 6 8} - -# Double-check that the previous result doesn't persist past the -# rollback! -do_test fts2k-1.4 { - execsql { - SELECT rowid FROM t1 WHERE t1 MATCH 'world'; - } -} {1 3 4 6} - -# Test it all together. -do_test fts2k-1.5 { - execsql { - BEGIN TRANSACTION; - INSERT INTO t1 (rowid, content) VALUES(10, "second world"); - INSERT INTO t1 (rowid, content) VALUES(11, "second sight"); - ROLLBACK TRANSACTION; - SELECT rowid FROM t1 WHERE t1 MATCH 'world'; - } -} {1 3 4 6} - -# Test that the obvious case works. -do_test fts2k-1.6 { - execsql { - BEGIN; - INSERT INTO t1 (rowid, content) VALUES(12, "third world"); - COMMIT; - SELECT rowid FROM t1 WHERE t1 MATCH 'third'; - } -} {12} - -# This is exactly the same as the previous test, except that older -# code loses the INSERT due to an SQLITE_SCHEMA error. -do_test fts2k-1.7 { - execsql { - BEGIN; - INSERT INTO t1 (rowid, content) VALUES(13, "third dimension"); - CREATE TABLE x (c); - COMMIT; - SELECT rowid FROM t1 WHERE t1 MATCH 'dimension'; - } -} {13} - -finish_test diff --git a/test/fts2l.test b/test/fts2l.test deleted file mode 100644 index 42f5ba134f..0000000000 --- a/test/fts2l.test +++ /dev/null @@ -1,69 +0,0 @@ -# 2007 March 28 -# -# The author disclaims copyright to this source code. -# -#************************************************************************* -# This file implements regression tests for SQLite library. The focus -# of this script is testing isspace/isalnum/tolower problems with the -# FTS2 module. Unfortunately, this code isn't a really principled set -# of tests, because it is impossible to know where new uses of these -# functions might appear. -# -# $Id: fts2l.test,v 1.2 2007/12/13 21:54:11 drh Exp $ -# - -set testdir [file dirname $argv0] -source $testdir/tester.tcl - -# If SQLITE_ENABLE_FTS2 is defined, omit this file. -ifcapable !fts2 { - finish_test - return -} - -# Tests that startsWith() (calls isspace, tolower, isalnum) can handle -# hi-bit chars. parseSpec() also calls isalnum here. -do_test fts2l-1.1 { - execsql "CREATE VIRTUAL TABLE t1 USING fts2(content, \x80)" -} {} - -# Additionally tests isspace() call in getToken(), and isalnum() call -# in tokenListToIdList(). -do_test fts2l-1.2 { - catch { - execsql "CREATE VIRTUAL TABLE t2 USING fts2(content, tokenize \x80)" - } - sqlite3_errmsg $DB -} "unknown tokenizer: \x80" - -# Additionally test final isalnum() in startsWith(). -do_test fts2l-1.3 { - execsql "CREATE VIRTUAL TABLE t3 USING fts2(content, tokenize\x80)" -} {} - -# The snippet-generation code has calls to isspace() which are sort of -# hard to get to. It finds convenient breakpoints by starting ~40 -# chars before and after the matched term, and scanning ~10 chars -# around that position for isspace() characters. The long word with -# embedded hi-bit chars causes one of these isspace() calls to be -# exercised. The version with a couple extra spaces should cause the -# other isspace() call to be exercised. [Both cases have been tested -# in the debugger, but I'm hoping to continue to catch it if simple -# constant changes change things slightly. -# -# The trailing and leading hi-bit chars help with code which tests for -# isspace() to coalesce multiple spaces. - -set word "\x80xxxxx\x80xxxxx\x80xxxxx\x80xxxxx\x80xxxxx\x80xxxxx\x80" -set phrase1 "$word $word $word target $word $word $word" -set phrase2 "$word $word $word target $word $word $word" - -db eval {CREATE VIRTUAL TABLE t4 USING fts2(content)} -db eval "INSERT INTO t4 (content) VALUES ('$phrase1')" -db eval "INSERT INTO t4 (content) VALUES ('$phrase2')" - -do_test fts2l-1.4 { - execsql {SELECT rowid, length(snippet(t4)) FROM t4 WHERE t4 MATCH 'target'} -} {1 111 2 117} - -finish_test diff --git a/test/fts2m.test b/test/fts2m.test deleted file mode 100644 index 6552637a62..0000000000 --- a/test/fts2m.test +++ /dev/null @@ -1,65 +0,0 @@ -# 2007 April 9 -# -# The author disclaims copyright to this source code. -# -#************************************************************************* -# This file implements regression tests for SQLite library. fts2 -# DELETE handling assumed all fields were non-null. This was not -# the intention at all. -# -# $Id: fts2m.test,v 1.1 2007/04/09 20:45:42 shess Exp $ -# - -set testdir [file dirname $argv0] -source $testdir/tester.tcl - -# If SQLITE_ENABLE_FTS2 is defined, omit this file. -ifcapable !fts2 { - finish_test - return -} - -db eval { - CREATE VIRTUAL TABLE t1 USING fts2(col_a, col_b); - - INSERT INTO t1(rowid, col_a, col_b) VALUES(1, 'testing', 'testing'); - INSERT INTO t1(rowid, col_a, col_b) VALUES(2, 'only a', null); - INSERT INTO t1(rowid, col_a, col_b) VALUES(3, null, 'only b'); - INSERT INTO t1(rowid, col_a, col_b) VALUES(4, null, null); -} - -do_test fts2m-1.0 { - execsql { - SELECT COUNT(col_a), COUNT(col_b), COUNT(*) FROM t1; - } -} {2 2 4} - -do_test fts2m-1.1 { - execsql { - DELETE FROM t1 WHERE rowid = 1; - SELECT COUNT(col_a), COUNT(col_b), COUNT(*) FROM t1; - } -} {1 1 3} - -do_test fts2m-1.2 { - execsql { - DELETE FROM t1 WHERE rowid = 2; - SELECT COUNT(col_a), COUNT(col_b), COUNT(*) FROM t1; - } -} {0 1 2} - -do_test fts2m-1.3 { - execsql { - DELETE FROM t1 WHERE rowid = 3; - SELECT COUNT(col_a), COUNT(col_b), COUNT(*) FROM t1; - } -} {0 0 1} - -do_test fts2m-1.4 { - execsql { - DELETE FROM t1 WHERE rowid = 4; - SELECT COUNT(col_a), COUNT(col_b), COUNT(*) FROM t1; - } -} {0 0 0} - -finish_test diff --git a/test/fts2n.test b/test/fts2n.test deleted file mode 100644 index ca0b4fe9ff..0000000000 --- a/test/fts2n.test +++ /dev/null @@ -1,196 +0,0 @@ -# 2007 April 26 -# -# The author disclaims copyright to this source code. -# -#************************************************************************* -# This file implements tests for prefix-searching in the fts2 -# component of the SQLite library. -# -# $Id: fts2n.test,v 1.2 2007/12/13 21:54:11 drh Exp $ -# - -set testdir [file dirname $argv0] -source $testdir/tester.tcl - -# If SQLITE_ENABLE_FTS2 is defined, omit this file. -ifcapable !fts2 { - finish_test - return -} - -# A large string to prime the pump with. -set text { - Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas - iaculis mollis ipsum. Praesent rhoncus placerat justo. Duis non quam - sed turpis posuere placerat. Curabitur et lorem in lorem porttitor - aliquet. Pellentesque bibendum tincidunt diam. Vestibulum blandit - ante nec elit. In sapien diam, facilisis eget, dictum sed, viverra - at, felis. Vestibulum magna. Sed magna dolor, vestibulum rhoncus, - ornare vel, vulputate sit amet, felis. Integer malesuada, tellus at - luctus gravida, diam nunc porta nibh, nec imperdiet massa metus eu - lectus. Aliquam nisi. Nunc fringilla nulla at lectus. Suspendisse - potenti. Cum sociis natoque penatibus et magnis dis parturient - montes, nascetur ridiculus mus. Pellentesque odio nulla, feugiat eu, - suscipit nec, consequat quis, risus. -} - -db eval { - CREATE VIRTUAL TABLE t1 USING fts2(c); - - INSERT INTO t1(rowid, c) VALUES(1, $text); - INSERT INTO t1(rowid, c) VALUES(2, 'Another lovely row'); -} - -# Exact match -do_test fts2n-1.1 { - execsql "SELECT rowid FROM t1 WHERE t1 MATCH 'lorem'" -} {1} - -# And a prefix -do_test fts2n-1.2 { - execsql "SELECT rowid FROM t1 WHERE t1 MATCH 'lore*'" -} {1} - -# Prefix includes exact match -do_test fts2n-1.3 { - execsql "SELECT rowid FROM t1 WHERE t1 MATCH 'lorem*'" -} {1} - -# Make certain everything isn't considered a prefix! -do_test fts2n-1.4 { - execsql "SELECT rowid FROM t1 WHERE t1 MATCH 'lore'" -} {} - -# Prefix across multiple rows. -do_test fts2n-1.5 { - execsql "SELECT rowid FROM t1 WHERE t1 MATCH 'lo*'" -} {1 2} - -# Likewise, with multiple hits in one document. -do_test fts2n-1.6 { - execsql "SELECT rowid FROM t1 WHERE t1 MATCH 'l*'" -} {1 2} - -# Prefix which should only hit one document. -do_test fts2n-1.7 { - execsql "SELECT rowid FROM t1 WHERE t1 MATCH 'lov*'" -} {2} - -# * not at end is dropped. -do_test fts2n-1.8 { - execsql "SELECT rowid FROM t1 WHERE t1 MATCH 'lo *'" -} {} - -# Stand-alone * is dropped. -do_test fts2n-1.9 { - execsql "SELECT rowid FROM t1 WHERE t1 MATCH '*'" -} {} - -# Phrase-query prefix. -do_test fts2n-1.10 { - execsql "SELECT rowid FROM t1 WHERE t1 MATCH '\"lovely r*\"'" -} {2} -do_test fts2n-1.11 { - execsql "SELECT rowid FROM t1 WHERE t1 MATCH '\"lovely r\"'" -} {} - -# Phrase query with multiple prefix matches. -do_test fts2n-1.12 { - execsql "SELECT rowid FROM t1 WHERE t1 MATCH '\"a* l*\"'" -} {1 2} - -# Phrase query with multiple prefix matches. -do_test fts2n-1.13 { - execsql "SELECT rowid FROM t1 WHERE t1 MATCH '\"a* l* row\"'" -} {2} - - - - -# Test across updates (and, by implication, deletes). - -# Version of text without "lorem". -regsub -all {[Ll]orem} $text '' ntext - -db eval { - CREATE VIRTUAL TABLE t2 USING fts2(c); - - INSERT INTO t2(rowid, c) VALUES(1, $text); - INSERT INTO t2(rowid, c) VALUES(2, 'Another lovely row'); - UPDATE t2 SET c = $ntext WHERE rowid = 1; -} - -# Can't see lorem as an exact match. -do_test fts2n-2.1 { - execsql "SELECT rowid FROM t2 WHERE t2 MATCH 'lorem'" -} {} - -# Can't see a prefix of lorem, either. -do_test fts2n-2.2 { - execsql "SELECT rowid FROM t2 WHERE t2 MATCH 'lore*'" -} {} - -# Can see lovely in the other document. -do_test fts2n-2.3 { - execsql "SELECT rowid FROM t2 WHERE t2 MATCH 'lo*'" -} {2} - -# Can still see other hits. -do_test fts2n-2.4 { - execsql "SELECT rowid FROM t2 WHERE t2 MATCH 'l*'" -} {1 2} - -# Prefix which should only hit one document. -do_test fts2n-2.5 { - execsql "SELECT rowid FROM t2 WHERE t2 MATCH 'lov*'" -} {2} - - - -# Test with a segment which will have multiple levels in the tree. - -# Build a big document with lots of unique terms. -set bigtext $text -foreach c {a b c d e} { - regsub -all {[A-Za-z]+} $bigtext "&$c" t - append bigtext $t -} - -# Populate a table with many copies of the big document, so that we -# can test the number of hits found. Populate $ret with the expected -# hit counts for each row. offsets() returns 4 elements for every -# hit. We'll have 6 hits for row 1, 1 for row 2, and 6*(2^5)==192 for -# $bigtext. -set ret {6 1} -db eval { - BEGIN; - CREATE VIRTUAL TABLE t3 USING fts2(c); - - INSERT INTO t3(rowid, c) VALUES(1, $text); - INSERT INTO t3(rowid, c) VALUES(2, 'Another lovely row'); -} -for {set i 0} {$i<100} {incr i} { - db eval {INSERT INTO t3(rowid, c) VALUES(3+$i, $bigtext)} - lappend ret 192 -} -db eval {COMMIT;} - -# Test that we get the expected number of hits. -do_test fts2n-3.1 { - set t {} - db eval {SELECT offsets(t3) as o FROM t3 WHERE t3 MATCH 'l*'} { - set l [llength $o] - lappend t [expr {$l/4}] - } - set t -} $ret - -# TODO(shess) It would be useful to test a couple edge cases, but I -# don't know if we have the precision to manage it from here at this -# time. Prefix hits can cross leaves, which the code above _should_ -# hit by virtue of size. There are two variations on this. If the -# tree is 2 levels high, the code will find the leaf-node extent -# directly, but if its higher, the code will have to follow two -# separate interior branches down the tree. Both should be tested. - -finish_test diff --git a/test/fts2o.test b/test/fts2o.test deleted file mode 100644 index 63e71b958a..0000000000 --- a/test/fts2o.test +++ /dev/null @@ -1,169 +0,0 @@ -# 2007 June 20 -# -# The author disclaims copyright to this source code. In place of -# a legal notice, here is a blessing: -# -# May you do good and not evil. -# May you find forgiveness for yourself and forgive others. -# May you share freely, never taking more than you give. -# -#************************************************************************* -# This file implements regression tests for SQLite library. The -# focus of this script is testing the FTS2 module. -# -# $Id: fts2o.test,v 1.4 2007/07/02 10:16:50 danielk1977 Exp $ -# - -set testdir [file dirname $argv0] -source $testdir/tester.tcl - -# If SQLITE_ENABLE_FTS2 is not defined, omit this file. -ifcapable !fts2 { - finish_test - return -} - -#--------------------------------------------------------------------- -# These tests, fts2o-1.*, test that ticket #2429 is fixed. -# -db eval { - CREATE VIRTUAL TABLE t1 USING fts2(a, b, c); - INSERT INTO t1(a, b, c) VALUES('one three four', 'one four', 'one four two'); -} -do_test fts2o-1.1 { - execsql { - SELECT rowid, snippet(t1) FROM t1 WHERE c MATCH 'four'; - } -} {1 {one <b>four</b> two}} -do_test fts2o-1.2 { - execsql { - SELECT rowid, snippet(t1) FROM t1 WHERE b MATCH 'four'; - } -} {1 {one <b>four</b>}} -do_test fts2o-1.3 { - execsql { - SELECT rowid, snippet(t1) FROM t1 WHERE a MATCH 'four'; - } -} {1 {one three <b>four</b>}} - -#--------------------------------------------------------------------- -# Test that it is possible to rename an fts2 table. -# -do_test fts2o-2.1 { - execsql { SELECT tbl_name FROM sqlite_master WHERE type = 'table'} -} {t1 t1_content t1_segments t1_segdir} -do_test fts2o-2.2 { - execsql { ALTER TABLE t1 RENAME to fts_t1; } -} {} -do_test fts2o-2.3 { - execsql { SELECT rowid, snippet(fts_t1) FROM fts_t1 WHERE a MATCH 'four'; } -} {1 {one three <b>four</b>}} -do_test fts2o-2.4 { - execsql { SELECT tbl_name FROM sqlite_master WHERE type = 'table'} -} {fts_t1 fts_t1_content fts_t1_segments fts_t1_segdir} - -# See what happens when renaming the fts2 table fails. -# -do_test fts2o-2.5 { - catchsql { - CREATE TABLE t1_segdir(a, b, c); - ALTER TABLE fts_t1 RENAME to t1; - } -} {1 {SQL logic error or missing database}} -do_test fts2o-2.6 { - execsql { SELECT rowid, snippet(fts_t1) FROM fts_t1 WHERE a MATCH 'four'; } -} {1 {one three <b>four</b>}} -do_test fts2o-2.7 { - execsql { SELECT tbl_name FROM sqlite_master WHERE type = 'table'} -} {fts_t1 fts_t1_content fts_t1_segments fts_t1_segdir t1_segdir} - -# See what happens when renaming the fts2 table fails inside a transaction. -# -do_test fts2o-2.8 { - execsql { - BEGIN; - INSERT INTO fts_t1(a, b, c) VALUES('one two three', 'one four', 'one two'); - } -} {} -do_test fts2o-2.9 { - catchsql { - ALTER TABLE fts_t1 RENAME to t1; - } -} {1 {SQL logic error or missing database}} -do_test fts2o-2.10 { - execsql { SELECT rowid, snippet(fts_t1) FROM fts_t1 WHERE a MATCH 'four'; } -} {1 {one three <b>four</b>}} -do_test fts2o-2.11 { - execsql { SELECT tbl_name FROM sqlite_master WHERE type = 'table'} -} {fts_t1 fts_t1_content fts_t1_segments fts_t1_segdir t1_segdir} -do_test fts2o-2.12 { - execsql COMMIT - execsql {SELECT a FROM fts_t1} -} {{one three four} {one two three}} -do_test fts2o-2.12 { - execsql { SELECT a, b, c FROM fts_t1 WHERE c MATCH 'four'; } -} {{one three four} {one four} {one four two}} - -#------------------------------------------------------------------- -# Close, delete and reopen the database. The following test should -# be run on an initially empty db. -# -db close -forcedelete test.db test.db-journal -sqlite3 db test.db - -do_test fts2o-3.1 { - execsql { - CREATE VIRTUAL TABLE t1 USING fts2(a, b, c); - INSERT INTO t1(a, b, c) VALUES('one three four', 'one four', 'one two'); - SELECT a, b, c FROM t1 WHERE c MATCH 'two'; - } -} {{one three four} {one four} {one two}} - -# This test was crashing at one point. -# -do_test fts2o-3.2 { - execsql { - SELECT a, b, c FROM t1 WHERE c MATCH 'two'; - CREATE TABLE t3(a, b, c); - SELECT a, b, c FROM t1 WHERE c MATCH 'two'; - } -} {{one three four} {one four} {one two} {one three four} {one four} {one two}} - -#--------------------------------------------------------------------- -# Test that it is possible to rename an fts2 table in an attached -# database. -# -forcedelete test2.db test2.db-journal - -do_test fts2o-3.1 { - execsql { - ATTACH 'test2.db' AS aux; - CREATE VIRTUAL TABLE aux.t1 USING fts2(a, b, c); - INSERT INTO aux.t1(a, b, c) VALUES( - 'neung song sahm', 'neung see', 'neung see song' - ); - } -} {} - -do_test fts2o-3.2 { - execsql { SELECT a, b, c FROM aux.t1 WHERE a MATCH 'song'; } -} {{neung song sahm} {neung see} {neung see song}} - -do_test fts2o-3.3 { - execsql { SELECT a, b, c FROM t1 WHERE c MATCH 'two'; } -} {{one three four} {one four} {one two}} - -do_test fts2o-3.4 { - execsql { ALTER TABLE aux.t1 RENAME TO t2 } -} {} - -do_test fts2o-3.2 { - execsql { SELECT a, b, c FROM t2 WHERE a MATCH 'song'; } -} {{neung song sahm} {neung see} {neung see song}} - -do_test fts2o-3.3 { - execsql { SELECT a, b, c FROM t1 WHERE c MATCH 'two'; } -} {{one three four} {one four} {one two}} - -finish_test diff --git a/test/fts2p.test b/test/fts2p.test deleted file mode 100644 index 38a8079d8f..0000000000 --- a/test/fts2p.test +++ /dev/null @@ -1,357 +0,0 @@ -# 2008 June 26 -# -# The author disclaims copyright to this source code. In place of -# a legal notice, here is a blessing: -# -# May you do good and not evil. -# May you find forgiveness for yourself and forgive others. -# May you share freely, never taking more than you give. -# -#************************************************************************* -# This file exercises some new testing functions in the FTS2 module, -# and then uses them to do some basic tests that FTS2 is internally -# working as expected. -# -# $Id: fts2p.test,v 1.1 2008/07/22 23:32:28 shess Exp $ -# - -set testdir [file dirname $argv0] -source $testdir/tester.tcl - -# If SQLITE_ENABLE_FTS2 is not defined, omit this file. -ifcapable !fts2 { - finish_test - return -} - -#************************************************************************* -# Probe to see if support for these functions is compiled in. -# TODO(shess): Change main.mk to do the right thing and remove this test. -db eval { - DROP TABLE IF EXISTS t1; - CREATE VIRTUAL TABLE t1 USING fts2(c); - INSERT INTO t1 (rowid, c) VALUES (1, 'x'); -} - -set s {SELECT dump_terms(t1, 1) FROM t1 LIMIT 1} -set r {1 {unable to use function dump_terms in the requested context}} -if {[catchsql $s]==$r} { - finish_test - return -} - -#************************************************************************* -# Test that the new functions give appropriate errors. -do_test fts2p-0.0 { - catchsql { - SELECT dump_terms(t1, 1) FROM t1 LIMIT 1; - } -} {1 {dump_terms: incorrect arguments}} - -do_test fts2p-0.1 { - catchsql { - SELECT dump_terms(t1, 0, 0, 0) FROM t1 LIMIT 1; - } -} {1 {dump_terms: incorrect arguments}} - -do_test fts2p-0.2 { - catchsql { - SELECT dump_terms(1, t1) FROM t1 LIMIT 1; - } -} {1 {unable to use function dump_terms in the requested context}} - -do_test fts2p-0.3 { - catchsql { - SELECT dump_terms(t1, 16, 16) FROM t1 LIMIT 1; - } -} {1 {dump_terms: segment not found}} - -do_test fts2p-0.4 { - catchsql { - SELECT dump_doclist(t1) FROM t1 LIMIT 1; - } -} {1 {dump_doclist: incorrect arguments}} - -do_test fts2p-0.5 { - catchsql { - SELECT dump_doclist(t1, NULL) FROM t1 LIMIT 1; - } -} {1 {dump_doclist: empty second argument}} - -do_test fts2p-0.6 { - catchsql { - SELECT dump_doclist(t1, '') FROM t1 LIMIT 1; - } -} {1 {dump_doclist: empty second argument}} - -do_test fts2p-0.7 { - catchsql { - SELECT dump_doclist(t1, 'a', 0) FROM t1 LIMIT 1; - } -} {1 {dump_doclist: incorrect arguments}} - -do_test fts2p-0.8 { - catchsql { - SELECT dump_doclist(t1, 'a', 0, 0, 0) FROM t1 LIMIT 1; - } -} {1 {dump_doclist: incorrect arguments}} - -do_test fts2p-0.9 { - catchsql { - SELECT dump_doclist(t1, 'a', 16, 16) FROM t1 LIMIT 1; - } -} {1 {dump_doclist: segment not found}} - -#************************************************************************* -# Utility function to check for the expected terms in the segment -# level/index. _all version does same but for entire index. -proc check_terms {test level index terms} { - # TODO(shess): Figure out why uplevel in do_test can't catch - # $level and $index directly. - set ::level $level - set ::index $index - do_test $test.terms { - execsql { - SELECT dump_terms(t1, $::level, $::index) FROM t1 LIMIT 1; - } - } [list $terms] -} -proc check_terms_all {test terms} { - do_test $test.terms { - execsql { - SELECT dump_terms(t1) FROM t1 LIMIT 1; - } - } [list $terms] -} - -# Utility function to check for the expected doclist for the term in -# segment level/index. _all version does same for entire index. -proc check_doclist {test level index term doclist} { - # TODO(shess): Again, why can't the non-:: versions work? - set ::term $term - set ::level $level - set ::index $index - do_test $test { - execsql { - SELECT dump_doclist(t1, $::term, $::level, $::index) FROM t1 LIMIT 1; - } - } [list $doclist] -} -proc check_doclist_all {test term doclist} { - set ::term $term - do_test $test { - execsql { - SELECT dump_doclist(t1, $::term) FROM t1 LIMIT 1; - } - } [list $doclist] -} - -#************************************************************************* -# Test the segments resulting from straight-forward inserts. -db eval { - DROP TABLE IF EXISTS t1; - CREATE VIRTUAL TABLE t1 USING fts2(c); - INSERT INTO t1 (rowid, c) VALUES (1, 'This is a test'); - INSERT INTO t1 (rowid, c) VALUES (2, 'That was a test'); - INSERT INTO t1 (rowid, c) VALUES (3, 'This is a test'); -} - -# Check for expected segments and expected matches. -do_test fts2p-1.0.segments { - execsql { - SELECT level, idx FROM t1_segdir ORDER BY level, idx; - } -} {0 0 0 1 0 2} -do_test fts2p-1.0.matches { - execsql { - SELECT OFFSETS(t1) FROM t1 - WHERE t1 MATCH 'this OR that OR was OR a OR is OR test' ORDER BY rowid; - } -} [list {0 0 0 4 0 4 5 2 0 3 8 1 0 5 10 4} \ - {0 1 0 4 0 2 5 3 0 3 9 1 0 5 11 4} \ - {0 0 0 4 0 4 5 2 0 3 8 1 0 5 10 4}] - -# Check the specifics of the segments constructed. -# Logical view of entire index. -check_terms_all fts2p-1.0.1 {a is test that this was} -check_doclist_all fts2p-1.0.1.1 a {[1 0[2]] [2 0[2]] [3 0[2]]} -check_doclist_all fts2p-1.0.1.2 is {[1 0[1]] [3 0[1]]} -check_doclist_all fts2p-1.0.1.3 test {[1 0[3]] [2 0[3]] [3 0[3]]} -check_doclist_all fts2p-1.0.1.4 that {[2 0[0]]} -check_doclist_all fts2p-1.0.1.5 this {[1 0[0]] [3 0[0]]} -check_doclist_all fts2p-1.0.1.6 was {[2 0[1]]} - -# Segment 0,0 -check_terms fts2p-1.0.2 0 0 {a is test this} -check_doclist fts2p-1.0.2.1 0 0 a {[1 0[2]]} -check_doclist fts2p-1.0.2.2 0 0 is {[1 0[1]]} -check_doclist fts2p-1.0.2.3 0 0 test {[1 0[3]]} -check_doclist fts2p-1.0.2.4 0 0 this {[1 0[0]]} - -# Segment 0,1 -check_terms fts2p-1.0.3 0 1 {a test that was} -check_doclist fts2p-1.0.3.1 0 1 a {[2 0[2]]} -check_doclist fts2p-1.0.3.2 0 1 test {[2 0[3]]} -check_doclist fts2p-1.0.3.3 0 1 that {[2 0[0]]} -check_doclist fts2p-1.0.3.4 0 1 was {[2 0[1]]} - -# Segment 0,2 -check_terms fts2p-1.0.4 0 2 {a is test this} -check_doclist fts2p-1.0.4.1 0 2 a {[3 0[2]]} -check_doclist fts2p-1.0.4.2 0 2 is {[3 0[1]]} -check_doclist fts2p-1.0.4.3 0 2 test {[3 0[3]]} -check_doclist fts2p-1.0.4.4 0 2 this {[3 0[0]]} - -#************************************************************************* -# Test the segments resulting from inserts followed by a delete. -db eval { - DROP TABLE IF EXISTS t1; - CREATE VIRTUAL TABLE t1 USING fts2(c); - INSERT INTO t1 (rowid, c) VALUES (1, 'This is a test'); - INSERT INTO t1 (rowid, c) VALUES (2, 'That was a test'); - INSERT INTO t1 (rowid, c) VALUES (3, 'This is a test'); - DELETE FROM t1 WHERE rowid = 1; -} - -do_test fts2p-1.1.segments { - execsql { - SELECT level, idx FROM t1_segdir ORDER BY level, idx; - } -} {0 0 0 1 0 2 0 3} -do_test fts2p-1.1.matches { - execsql { - SELECT OFFSETS(t1) FROM t1 - WHERE t1 MATCH 'this OR that OR was OR a OR is OR test' ORDER BY rowid; - } -} {{0 1 0 4 0 2 5 3 0 3 9 1 0 5 11 4} {0 0 0 4 0 4 5 2 0 3 8 1 0 5 10 4}} - -check_terms_all fts2p-1.1.1 {a is test that this was} -check_doclist_all fts2p-1.1.1.1 a {[2 0[2]] [3 0[2]]} -check_doclist_all fts2p-1.1.1.2 is {[3 0[1]]} -check_doclist_all fts2p-1.1.1.3 test {[2 0[3]] [3 0[3]]} -check_doclist_all fts2p-1.1.1.4 that {[2 0[0]]} -check_doclist_all fts2p-1.1.1.5 this {[3 0[0]]} -check_doclist_all fts2p-1.1.1.6 was {[2 0[1]]} - -check_terms fts2p-1.1.2 0 0 {a is test this} -check_doclist fts2p-1.1.2.1 0 0 a {[1 0[2]]} -check_doclist fts2p-1.1.2.2 0 0 is {[1 0[1]]} -check_doclist fts2p-1.1.2.3 0 0 test {[1 0[3]]} -check_doclist fts2p-1.1.2.4 0 0 this {[1 0[0]]} - -check_terms fts2p-1.1.3 0 1 {a test that was} -check_doclist fts2p-1.1.3.1 0 1 a {[2 0[2]]} -check_doclist fts2p-1.1.3.2 0 1 test {[2 0[3]]} -check_doclist fts2p-1.1.3.3 0 1 that {[2 0[0]]} -check_doclist fts2p-1.1.3.4 0 1 was {[2 0[1]]} - -check_terms fts2p-1.1.4 0 2 {a is test this} -check_doclist fts2p-1.1.4.1 0 2 a {[3 0[2]]} -check_doclist fts2p-1.1.4.2 0 2 is {[3 0[1]]} -check_doclist fts2p-1.1.4.3 0 2 test {[3 0[3]]} -check_doclist fts2p-1.1.4.4 0 2 this {[3 0[0]]} - -check_terms fts2p-1.1.5 0 3 {a is test this} -check_doclist fts2p-1.1.5.1 0 3 a {[1]} -check_doclist fts2p-1.1.5.2 0 3 is {[1]} -check_doclist fts2p-1.1.5.3 0 3 test {[1]} -check_doclist fts2p-1.1.5.4 0 3 this {[1]} - -#************************************************************************* -# Test results when all references to certain tokens are deleted. -db eval { - DROP TABLE IF EXISTS t1; - CREATE VIRTUAL TABLE t1 USING fts2(c); - INSERT INTO t1 (rowid, c) VALUES (1, 'This is a test'); - INSERT INTO t1 (rowid, c) VALUES (2, 'That was a test'); - INSERT INTO t1 (rowid, c) VALUES (3, 'This is a test'); - DELETE FROM t1 WHERE rowid IN (1,3); -} - -# Still 4 segments because 0,3 will contain deletes for rowid 1 and 3. -do_test fts2p-1.2.segments { - execsql { - SELECT level, idx FROM t1_segdir ORDER BY level, idx; - } -} {0 0 0 1 0 2 0 3} -do_test fts2p-1.2.matches { - execsql { - SELECT OFFSETS(t1) FROM t1 - WHERE t1 MATCH 'this OR that OR was OR a OR is OR test' ORDER BY rowid; - } -} {{0 1 0 4 0 2 5 3 0 3 9 1 0 5 11 4}} - -check_terms_all fts2p-1.2.1 {a is test that this was} -check_doclist_all fts2p-1.2.1.1 a {[2 0[2]]} -check_doclist_all fts2p-1.2.1.2 is {} -check_doclist_all fts2p-1.2.1.3 test {[2 0[3]]} -check_doclist_all fts2p-1.2.1.4 that {[2 0[0]]} -check_doclist_all fts2p-1.2.1.5 this {} -check_doclist_all fts2p-1.2.1.6 was {[2 0[1]]} - -check_terms fts2p-1.2.2 0 0 {a is test this} -check_doclist fts2p-1.2.2.1 0 0 a {[1 0[2]]} -check_doclist fts2p-1.2.2.2 0 0 is {[1 0[1]]} -check_doclist fts2p-1.2.2.3 0 0 test {[1 0[3]]} -check_doclist fts2p-1.2.2.4 0 0 this {[1 0[0]]} - -check_terms fts2p-1.2.3 0 1 {a test that was} -check_doclist fts2p-1.2.3.1 0 1 a {[2 0[2]]} -check_doclist fts2p-1.2.3.2 0 1 test {[2 0[3]]} -check_doclist fts2p-1.2.3.3 0 1 that {[2 0[0]]} -check_doclist fts2p-1.2.3.4 0 1 was {[2 0[1]]} - -check_terms fts2p-1.2.4 0 2 {a is test this} -check_doclist fts2p-1.2.4.1 0 2 a {[3 0[2]]} -check_doclist fts2p-1.2.4.2 0 2 is {[3 0[1]]} -check_doclist fts2p-1.2.4.3 0 2 test {[3 0[3]]} -check_doclist fts2p-1.2.4.4 0 2 this {[3 0[0]]} - -check_terms fts2p-1.2.5 0 3 {a is test this} -check_doclist fts2p-1.2.5.1 0 3 a {[1] [3]} -check_doclist fts2p-1.2.5.2 0 3 is {[1] [3]} -check_doclist fts2p-1.2.5.3 0 3 test {[1] [3]} -check_doclist fts2p-1.2.5.4 0 3 this {[1] [3]} - -#************************************************************************* -# Test results when everything is optimized manually. -db eval { - DROP TABLE IF EXISTS t1; - CREATE VIRTUAL TABLE t1 USING fts2(c); - INSERT INTO t1 (rowid, c) VALUES (1, 'This is a test'); - INSERT INTO t1 (rowid, c) VALUES (2, 'That was a test'); - INSERT INTO t1 (rowid, c) VALUES (3, 'This is a test'); - DELETE FROM t1 WHERE rowid IN (1,3); - DROP TABLE IF EXISTS t1old; - ALTER TABLE t1 RENAME TO t1old; - CREATE VIRTUAL TABLE t1 USING fts2(c); - INSERT INTO t1 (rowid, c) SELECT rowid, c FROM t1old; - DROP TABLE t1old; -} - -# Should be a single optimal segment with the same logical results. -do_test fts2p-1.3.segments { - execsql { - SELECT level, idx FROM t1_segdir ORDER BY level, idx; - } -} {0 0} -do_test fts2p-1.3.matches { - execsql { - SELECT OFFSETS(t1) FROM t1 - WHERE t1 MATCH 'this OR that OR was OR a OR is OR test' ORDER BY rowid; - } -} {{0 1 0 4 0 2 5 3 0 3 9 1 0 5 11 4}} - -check_terms_all fts2p-1.3.1 {a test that was} -check_doclist_all fts2p-1.3.1.1 a {[2 0[2]]} -check_doclist_all fts2p-1.3.1.2 test {[2 0[3]]} -check_doclist_all fts2p-1.3.1.3 that {[2 0[0]]} -check_doclist_all fts2p-1.3.1.4 was {[2 0[1]]} - -check_terms fts2p-1.3.2 0 0 {a test that was} -check_doclist fts2p-1.3.2.1 0 0 a {[2 0[2]]} -check_doclist fts2p-1.3.2.2 0 0 test {[2 0[3]]} -check_doclist fts2p-1.3.2.3 0 0 that {[2 0[0]]} -check_doclist fts2p-1.3.2.4 0 0 was {[2 0[1]]} - -finish_test diff --git a/test/fts2q.test b/test/fts2q.test deleted file mode 100644 index cba78d583f..0000000000 --- a/test/fts2q.test +++ /dev/null @@ -1,346 +0,0 @@ -# 2008 June 26 -# -# The author disclaims copyright to this source code. In place of -# a legal notice, here is a blessing: -# -# May you do good and not evil. -# May you find forgiveness for yourself and forgive others. -# May you share freely, never taking more than you give. -# -#************************************************************************* -# This file implements regression tests for SQLite library. The focus -# of this script is testing the FTS2 module's optimize() function. -# -# $Id: fts2q.test,v 1.2 2008/07/22 23:49:44 shess Exp $ -# - -set testdir [file dirname $argv0] -source $testdir/tester.tcl - -# If SQLITE_ENABLE_FTS2 is not defined, omit this file. -ifcapable !fts2 { - finish_test - return -} - -#************************************************************************* -# Probe to see if support for the FTS2 dump_* functions is compiled in. -# TODO(shess): Change main.mk to do the right thing and remove this test. -db eval { - DROP TABLE IF EXISTS t1; - CREATE VIRTUAL TABLE t1 USING fts2(c); - INSERT INTO t1 (rowid, c) VALUES (1, 'x'); -} - -set s {SELECT dump_terms(t1, 1) FROM t1 LIMIT 1} -set r {1 {unable to use function dump_terms in the requested context}} -if {[catchsql $s]==$r} { - finish_test - return -} - -#************************************************************************* -# Utility function to check for the expected terms in the segment -# level/index. _all version does same but for entire index. -proc check_terms {test level index terms} { - # TODO(shess): Figure out why uplevel in do_test can't catch - # $level and $index directly. - set ::level $level - set ::index $index - do_test $test.terms { - execsql { - SELECT dump_terms(t1, $::level, $::index) FROM t1 LIMIT 1; - } - } [list $terms] -} -proc check_terms_all {test terms} { - do_test $test.terms { - execsql { - SELECT dump_terms(t1) FROM t1 LIMIT 1; - } - } [list $terms] -} - -# Utility function to check for the expected doclist for the term in -# segment level/index. _all version does same for entire index. -proc check_doclist {test level index term doclist} { - # TODO(shess): Again, why can't the non-:: versions work? - set ::term $term - set ::level $level - set ::index $index - do_test $test { - execsql { - SELECT dump_doclist(t1, $::term, $::level, $::index) FROM t1 LIMIT 1; - } - } [list $doclist] -} -proc check_doclist_all {test term doclist} { - set ::term $term - do_test $test { - execsql { - SELECT dump_doclist(t1, $::term) FROM t1 LIMIT 1; - } - } [list $doclist] -} - -#************************************************************************* -# Test results when all rows are deleted and one is added back. -# Previously older segments would continue to exist, but now the index -# should be dropped when the table is empty. The results should look -# exactly like we never added the earlier rows in the first place. -db eval { - DROP TABLE IF EXISTS t1; - CREATE VIRTUAL TABLE t1 USING fts2(c); - INSERT INTO t1 (rowid, c) VALUES (1, 'This is a test'); - INSERT INTO t1 (rowid, c) VALUES (2, 'That was a test'); - INSERT INTO t1 (rowid, c) VALUES (3, 'This is a test'); - DELETE FROM t1 WHERE 1=1; -- Delete each row rather than dropping table. - INSERT INTO t1 (rowid, c) VALUES (1, 'This is a test'); -} - -# Should be a single initial segment. -do_test fts2q-1.segments { - execsql { - SELECT level, idx FROM t1_segdir ORDER BY level, idx; - } -} {0 0} -do_test fts2q-1.matches { - execsql { - SELECT OFFSETS(t1) FROM t1 - WHERE t1 MATCH 'this OR that OR was OR a OR is OR test' ORDER BY rowid; - } -} {{0 0 0 4 0 4 5 2 0 3 8 1 0 5 10 4}} - -check_terms_all fts2q-1.1 {a is test this} -check_doclist_all fts2q-1.1.1 a {[1 0[2]]} -check_doclist_all fts2q-1.1.2 is {[1 0[1]]} -check_doclist_all fts2q-1.1.3 test {[1 0[3]]} -check_doclist_all fts2q-1.1.4 this {[1 0[0]]} - -check_terms fts2q-1.2 0 0 {a is test this} -check_doclist fts2q-1.2.1 0 0 a {[1 0[2]]} -check_doclist fts2q-1.2.2 0 0 is {[1 0[1]]} -check_doclist fts2q-1.2.3 0 0 test {[1 0[3]]} -check_doclist fts2q-1.2.4 0 0 this {[1 0[0]]} - -#************************************************************************* -# Test results when everything is optimized manually. -# NOTE(shess): This is a copy of fts2c-1.3. I've pulled a copy here -# because fts2q-2 and fts2q-3 should have identical results. -db eval { - DROP TABLE IF EXISTS t1; - CREATE VIRTUAL TABLE t1 USING fts2(c); - INSERT INTO t1 (rowid, c) VALUES (1, 'This is a test'); - INSERT INTO t1 (rowid, c) VALUES (2, 'That was a test'); - INSERT INTO t1 (rowid, c) VALUES (3, 'This is a test'); - DELETE FROM t1 WHERE rowid IN (1,3); - DROP TABLE IF EXISTS t1old; - ALTER TABLE t1 RENAME TO t1old; - CREATE VIRTUAL TABLE t1 USING fts2(c); - INSERT INTO t1 (rowid, c) SELECT rowid, c FROM t1old; - DROP TABLE t1old; -} - -# Should be a single optimal segment with the same logical results. -do_test fts2q-2.segments { - execsql { - SELECT level, idx FROM t1_segdir ORDER BY level, idx; - } -} {0 0} -do_test fts2q-2.matches { - execsql { - SELECT OFFSETS(t1) FROM t1 - WHERE t1 MATCH 'this OR that OR was OR a OR is OR test' ORDER BY rowid; - } -} {{0 1 0 4 0 2 5 3 0 3 9 1 0 5 11 4}} - -check_terms_all fts2q-2.1 {a test that was} -check_doclist_all fts2q-2.1.1 a {[2 0[2]]} -check_doclist_all fts2q-2.1.2 test {[2 0[3]]} -check_doclist_all fts2q-2.1.3 that {[2 0[0]]} -check_doclist_all fts2q-2.1.4 was {[2 0[1]]} - -check_terms fts2q-2.2 0 0 {a test that was} -check_doclist fts2q-2.2.1 0 0 a {[2 0[2]]} -check_doclist fts2q-2.2.2 0 0 test {[2 0[3]]} -check_doclist fts2q-2.2.3 0 0 that {[2 0[0]]} -check_doclist fts2q-2.2.4 0 0 was {[2 0[1]]} - -#************************************************************************* -# Test results when everything is optimized via optimize(). -db eval { - DROP TABLE IF EXISTS t1; - CREATE VIRTUAL TABLE t1 USING fts2(c); - INSERT INTO t1 (rowid, c) VALUES (1, 'This is a test'); - INSERT INTO t1 (rowid, c) VALUES (2, 'That was a test'); - INSERT INTO t1 (rowid, c) VALUES (3, 'This is a test'); - DELETE FROM t1 WHERE rowid IN (1,3); - SELECT OPTIMIZE(t1) FROM t1 LIMIT 1; -} - -# Should be a single optimal segment with the same logical results. -do_test fts2q-3.segments { - execsql { - SELECT level, idx FROM t1_segdir ORDER BY level, idx; - } -} {0 0} -do_test fts2q-3.matches { - execsql { - SELECT OFFSETS(t1) FROM t1 - WHERE t1 MATCH 'this OR that OR was OR a OR is OR test' ORDER BY rowid; - } -} {{0 1 0 4 0 2 5 3 0 3 9 1 0 5 11 4}} - -check_terms_all fts2q-3.1 {a test that was} -check_doclist_all fts2q-3.1.1 a {[2 0[2]]} -check_doclist_all fts2q-3.1.2 test {[2 0[3]]} -check_doclist_all fts2q-3.1.3 that {[2 0[0]]} -check_doclist_all fts2q-3.1.4 was {[2 0[1]]} - -check_terms fts2q-3.2 0 0 {a test that was} -check_doclist fts2q-3.2.1 0 0 a {[2 0[2]]} -check_doclist fts2q-3.2.2 0 0 test {[2 0[3]]} -check_doclist fts2q-3.2.3 0 0 that {[2 0[0]]} -check_doclist fts2q-3.2.4 0 0 was {[2 0[1]]} - -#************************************************************************* -# Test optimize() against a table involving segment merges. -# NOTE(shess): Since there's no transaction, each of the INSERT/UPDATE -# statements generates a segment. -db eval { - DROP TABLE IF EXISTS t1; - CREATE VIRTUAL TABLE t1 USING fts2(c); - - INSERT INTO t1 (rowid, c) VALUES (1, 'This is a test'); - INSERT INTO t1 (rowid, c) VALUES (2, 'That was a test'); - INSERT INTO t1 (rowid, c) VALUES (3, 'This is a test'); - - UPDATE t1 SET c = 'This is a test one' WHERE rowid = 1; - UPDATE t1 SET c = 'That was a test one' WHERE rowid = 2; - UPDATE t1 SET c = 'This is a test one' WHERE rowid = 3; - - UPDATE t1 SET c = 'This is a test two' WHERE rowid = 1; - UPDATE t1 SET c = 'That was a test two' WHERE rowid = 2; - UPDATE t1 SET c = 'This is a test two' WHERE rowid = 3; - - UPDATE t1 SET c = 'This is a test three' WHERE rowid = 1; - UPDATE t1 SET c = 'That was a test three' WHERE rowid = 2; - UPDATE t1 SET c = 'This is a test three' WHERE rowid = 3; - - UPDATE t1 SET c = 'This is a test four' WHERE rowid = 1; - UPDATE t1 SET c = 'That was a test four' WHERE rowid = 2; - UPDATE t1 SET c = 'This is a test four' WHERE rowid = 3; - - UPDATE t1 SET c = 'This is a test' WHERE rowid = 1; - UPDATE t1 SET c = 'That was a test' WHERE rowid = 2; - UPDATE t1 SET c = 'This is a test' WHERE rowid = 3; -} - -# 2 segments in level 0, 1 in level 1 (18 segments created, 16 -# merged). -do_test fts2q-4.segments { - execsql { - SELECT level, idx FROM t1_segdir ORDER BY level, idx; - } -} {0 0 0 1 1 0} - -do_test fts2q-4.matches { - execsql { - SELECT OFFSETS(t1) FROM t1 - WHERE t1 MATCH 'this OR that OR was OR a OR is OR test' ORDER BY rowid; - } -} [list {0 0 0 4 0 4 5 2 0 3 8 1 0 5 10 4} \ - {0 1 0 4 0 2 5 3 0 3 9 1 0 5 11 4} \ - {0 0 0 4 0 4 5 2 0 3 8 1 0 5 10 4}] - -check_terms_all fts2q-4.1 {a four is one test that this three two was} -check_doclist_all fts2q-4.1.1 a {[1 0[2]] [2 0[2]] [3 0[2]]} -check_doclist_all fts2q-4.1.2 four {} -check_doclist_all fts2q-4.1.3 is {[1 0[1]] [3 0[1]]} -check_doclist_all fts2q-4.1.4 one {} -check_doclist_all fts2q-4.1.5 test {[1 0[3]] [2 0[3]] [3 0[3]]} -check_doclist_all fts2q-4.1.6 that {[2 0[0]]} -check_doclist_all fts2q-4.1.7 this {[1 0[0]] [3 0[0]]} -check_doclist_all fts2q-4.1.8 three {} -check_doclist_all fts2q-4.1.9 two {} -check_doclist_all fts2q-4.1.10 was {[2 0[1]]} - -check_terms fts2q-4.2 0 0 {a four test that was} -check_doclist fts2q-4.2.1 0 0 a {[2 0[2]]} -check_doclist fts2q-4.2.2 0 0 four {[2]} -check_doclist fts2q-4.2.3 0 0 test {[2 0[3]]} -check_doclist fts2q-4.2.4 0 0 that {[2 0[0]]} -check_doclist fts2q-4.2.5 0 0 was {[2 0[1]]} - -check_terms fts2q-4.3 0 1 {a four is test this} -check_doclist fts2q-4.3.1 0 1 a {[3 0[2]]} -check_doclist fts2q-4.3.2 0 1 four {[3]} -check_doclist fts2q-4.3.3 0 1 is {[3 0[1]]} -check_doclist fts2q-4.3.4 0 1 test {[3 0[3]]} -check_doclist fts2q-4.3.5 0 1 this {[3 0[0]]} - -check_terms fts2q-4.4 1 0 {a four is one test that this three two was} -check_doclist fts2q-4.4.1 1 0 a {[1 0[2]] [2 0[2]] [3 0[2]]} -check_doclist fts2q-4.4.2 1 0 four {[1] [2 0[4]] [3 0[4]]} -check_doclist fts2q-4.4.3 1 0 is {[1 0[1]] [3 0[1]]} -check_doclist fts2q-4.4.4 1 0 one {[1] [2] [3]} -check_doclist fts2q-4.4.5 1 0 test {[1 0[3]] [2 0[3]] [3 0[3]]} -check_doclist fts2q-4.4.6 1 0 that {[2 0[0]]} -check_doclist fts2q-4.4.7 1 0 this {[1 0[0]] [3 0[0]]} -check_doclist fts2q-4.4.8 1 0 three {[1] [2] [3]} -check_doclist fts2q-4.4.9 1 0 two {[1] [2] [3]} -check_doclist fts2q-4.4.10 1 0 was {[2 0[1]]} - -# Optimize should leave the result in the level of the highest-level -# prior segment. -do_test fts2q-4.5 { - execsql { - SELECT OPTIMIZE(t1) FROM t1 LIMIT 1; - SELECT level, idx FROM t1_segdir ORDER BY level, idx; - } -} {{Index optimized} 1 0} - -# Identical to fts2q-4.matches. -do_test fts2q-4.5.matches { - execsql { - SELECT OFFSETS(t1) FROM t1 - WHERE t1 MATCH 'this OR that OR was OR a OR is OR test' ORDER BY rowid; - } -} [list {0 0 0 4 0 4 5 2 0 3 8 1 0 5 10 4} \ - {0 1 0 4 0 2 5 3 0 3 9 1 0 5 11 4} \ - {0 0 0 4 0 4 5 2 0 3 8 1 0 5 10 4}] - -check_terms_all fts2q-4.5.1 {a is test that this was} -check_doclist_all fts2q-4.5.1.1 a {[1 0[2]] [2 0[2]] [3 0[2]]} -check_doclist_all fts2q-4.5.1.2 is {[1 0[1]] [3 0[1]]} -check_doclist_all fts2q-4.5.1.3 test {[1 0[3]] [2 0[3]] [3 0[3]]} -check_doclist_all fts2q-4.5.1.4 that {[2 0[0]]} -check_doclist_all fts2q-4.5.1.5 this {[1 0[0]] [3 0[0]]} -check_doclist_all fts2q-4.5.1.6 was {[2 0[1]]} - -check_terms fts2q-4.5.2 1 0 {a is test that this was} -check_doclist fts2q-4.5.2.1 1 0 a {[1 0[2]] [2 0[2]] [3 0[2]]} -check_doclist fts2q-4.5.2.2 1 0 is {[1 0[1]] [3 0[1]]} -check_doclist fts2q-4.5.2.3 1 0 test {[1 0[3]] [2 0[3]] [3 0[3]]} -check_doclist fts2q-4.5.2.4 1 0 that {[2 0[0]]} -check_doclist fts2q-4.5.2.5 1 0 this {[1 0[0]] [3 0[0]]} -check_doclist fts2q-4.5.2.6 1 0 was {[2 0[1]]} - -# Re-optimizing does nothing. -do_test fts2q-5.0 { - execsql { - SELECT OPTIMIZE(t1) FROM t1 LIMIT 1; - SELECT level, idx FROM t1_segdir ORDER BY level, idx; - } -} {{Index already optimal} 1 0} - -# Even if we move things around, still does nothing. -do_test fts2q-5.1 { - execsql { - UPDATE t1_segdir SET level = 2 WHERE level = 1 AND idx = 0; - SELECT OPTIMIZE(t1) FROM t1 LIMIT 1; - SELECT level, idx FROM t1_segdir ORDER BY level, idx; - } -} {{Index already optimal} 2 0} - -finish_test diff --git a/test/fts2r.test b/test/fts2r.test deleted file mode 100644 index c0be367115..0000000000 --- a/test/fts2r.test +++ /dev/null @@ -1,121 +0,0 @@ -# 2008 July 29 -# -# The author disclaims copyright to this source code. In place of -# a legal notice, here is a blessing: -# -# May you do good and not evil. -# May you find forgiveness for yourself and forgive others. -# May you share freely, never taking more than you give. -# -#************************************************************************* -# These tests exercise the various types of fts2 cursors. -# -# $Id: fts2r.test,v 1.1 2008/07/29 20:38:18 shess Exp $ -# - -set testdir [file dirname $argv0] -source $testdir/tester.tcl - -# If SQLITE_ENABLE_FTS2 is not defined, omit this file. -ifcapable !fts2 { - finish_test - return -} - -#************************************************************************* -# Test table scan (QUERY_GENERIC). This kind of query happens for -# queries with no WHERE clause, or for WHERE clauses which cannot be -# satisfied by an index. -db eval { - DROP TABLE IF EXISTS t1; - CREATE VIRTUAL TABLE t1 USING fts2(c); - INSERT INTO t1 (rowid, c) VALUES (1, 'This is a test'); - INSERT INTO t1 (rowid, c) VALUES (2, 'That was a test'); - INSERT INTO t1 (rowid, c) VALUES (3, 'This is a test'); -} - -do_test fts2e-1.1 { - execsql { - SELECT rowid FROM t1 ORDER BY rowid; - } -} {1 2 3} - -do_test fts2e-1.2 { - execsql { - SELECT rowid FROM t1 WHERE c LIKE '%test' ORDER BY rowid; - } -} {1 2 3} - -do_test fts2e-1.3 { - execsql { - SELECT rowid FROM t1 WHERE c LIKE 'That%' ORDER BY rowid; - } -} {2} - -#************************************************************************* -# Test lookup by rowid (QUERY_ROWID). This kind of query happens for -# queries which select by the rowid implicit index. -db eval { - DROP TABLE IF EXISTS t1; - DROP TABLE IF EXISTS t2; - CREATE VIRTUAL TABLE t1 USING fts2(c); - CREATE TABLE t2(id INTEGER PRIMARY KEY AUTOINCREMENT, weight INTEGER UNIQUE); - INSERT INTO t2 VALUES (null, 10); - INSERT INTO t1 (rowid, c) VALUES (last_insert_rowid(), 'This is a test'); - INSERT INTO t2 VALUES (null, 5); - INSERT INTO t1 (rowid, c) VALUES (last_insert_rowid(), 'That was a test'); - INSERT INTO t2 VALUES (null, 20); - INSERT INTO t1 (rowid, c) VALUES (last_insert_rowid(), 'This is a test'); -} - -# TODO(shess): This actually is doing QUERY_GENERIC? I'd have -# expected QUERY_ROWID in this case, as for a very large table the -# full scan is less efficient. -do_test fts2e-2.1 { - execsql { - SELECT rowid FROM t1 WHERE rowid in (1, 2, 10); - } -} {1 2} - -do_test fts2e-2.2 { - execsql { - SELECT t1.rowid, weight FROM t1, t2 WHERE t2.id = t1.rowid ORDER BY weight; - } -} {2 5 1 10 3 20} - -do_test fts2e-2.3 { - execsql { - SELECT t1.rowid, weight FROM t1, t2 - WHERE t2.weight>5 AND t2.id = t1.rowid ORDER BY weight; - } -} {1 10 3 20} - -#************************************************************************* -# Test lookup by MATCH (QUERY_FULLTEXT). This is the fulltext index. -db eval { - DROP TABLE IF EXISTS t1; - DROP TABLE IF EXISTS t2; - CREATE VIRTUAL TABLE t1 USING fts2(c); - CREATE TABLE t2(id INTEGER PRIMARY KEY AUTOINCREMENT, weight INTEGER UNIQUE); - INSERT INTO t2 VALUES (null, 10); - INSERT INTO t1 (rowid, c) VALUES (last_insert_rowid(), 'This is a test'); - INSERT INTO t2 VALUES (null, 5); - INSERT INTO t1 (rowid, c) VALUES (last_insert_rowid(), 'That was a test'); - INSERT INTO t2 VALUES (null, 20); - INSERT INTO t1 (rowid, c) VALUES (last_insert_rowid(), 'This is a test'); -} - -do_test fts2e-3.1 { - execsql { - SELECT rowid FROM t1 WHERE t1 MATCH 'this' ORDER BY rowid; - } -} {1 3} - -do_test fts2e-3.2 { - execsql { - SELECT t1.rowid, weight FROM t1, t2 - WHERE t1 MATCH 'this' AND t1.rowid = t2.id ORDER BY weight; - } -} {1 10 3 20} - -finish_test diff --git a/test/fts2token.test b/test/fts2token.test deleted file mode 100644 index de5f94d7fc..0000000000 --- a/test/fts2token.test +++ /dev/null @@ -1,174 +0,0 @@ -# 2007 June 21 -# -# The author disclaims copyright to this source code. In place of -# a legal notice, here is a blessing: -# -# May you do good and not evil. -# May you find forgiveness for yourself and forgive others. -# May you share freely, never taking more than you give. -# -#************************************************************************* -# This file implements regression tests for SQLite library. The focus -# of this script is testing the pluggable tokeniser feature of the -# FTS2 module. -# -# $Id: fts2token.test,v 1.3 2007/06/25 12:05:40 danielk1977 Exp $ -# - -set testdir [file dirname $argv0] -source $testdir/tester.tcl - -# If SQLITE_ENABLE_FTS2 is defined, omit this file. -ifcapable !fts2 { - finish_test - return -} - -proc escape_string {str} { - set out "" - foreach char [split $str ""] { - scan $char %c i - if {$i<=127} { - append out $char - } else { - append out [format {\x%.4x} $i] - } - } - set out -} - -#-------------------------------------------------------------------------- -# Test cases fts2token-1.* are the warm-body test for the SQL scalar -# function fts2_tokenizer(). The procedure is as follows: -# -# 1: Verify that there is no such fts2 tokenizer as 'blah'. -# -# 2: Query for the built-in tokenizer 'simple'. Insert a copy of the -# retrieved value as tokenizer 'blah'. -# -# 3: Test that the value returned for tokenizer 'blah' is now the -# same as that retrieved for 'simple'. -# -# 4: Test that it is now possible to create an fts2 table using -# tokenizer 'blah' (it was not possible in step 1). -# -# 5: Test that the table created to use tokenizer 'blah' is usable. -# -do_test fts2token-1.1 { - catchsql { - CREATE VIRTUAL TABLE t1 USING fts2(content, tokenize blah); - } -} {1 {unknown tokenizer: blah}} -do_test fts2token-1.2 { - execsql { - SELECT fts2_tokenizer('blah', fts2_tokenizer('simple')) IS NULL; - } -} {0} -do_test fts2token-1.3 { - execsql { - SELECT fts2_tokenizer('blah') == fts2_tokenizer('simple'); - } -} {1} -do_test fts2token-1.4 { - catchsql { - CREATE VIRTUAL TABLE t1 USING fts2(content, tokenize blah); - } -} {0 {}} -do_test fts2token-1.5 { - execsql { - INSERT INTO t1(content) VALUES('There was movement at the station'); - INSERT INTO t1(content) VALUES('For the word has passed around'); - INSERT INTO t1(content) VALUES('That the colt from ol regret had got away'); - SELECT content FROM t1 WHERE content MATCH 'movement' - } -} {{There was movement at the station}} - -#-------------------------------------------------------------------------- -# Test cases fts2token-2.* test error cases in the scalar function based -# API for getting and setting tokenizers. -# -do_test fts2token-2.1 { - catchsql { - SELECT fts2_tokenizer('nosuchtokenizer'); - } -} {1 {unknown tokenizer: nosuchtokenizer}} - -#-------------------------------------------------------------------------- -# Test cases fts2token-3.* test the three built-in tokenizers with a -# simple input string via the built-in test function. This is as much -# to test the test function as the tokenizer implementations. -# -do_test fts2token-3.1 { - execsql { - SELECT fts2_tokenizer_test('simple', 'I don''t see how'); - } -} {{0 i I 1 don don 2 t t 3 see see 4 how how}} -do_test fts2token-3.2 { - execsql { - SELECT fts2_tokenizer_test('porter', 'I don''t see how'); - } -} {{0 i I 1 don don 2 t t 3 see see 4 how how}} -ifcapable icu { - do_test fts2token-3.3 { - execsql { - SELECT fts2_tokenizer_test('icu', 'I don''t see how'); - } - } {{0 i I 1 don't don't 2 see see 3 how how}} -} - -#-------------------------------------------------------------------------- -# Test cases fts2token-4.* test the ICU tokenizer. In practice, this -# tokenizer only has two modes - "thai" and "everybody else". Some other -# Asian languages (Lao, Khmer etc.) require the same special treatment as -# Thai, but ICU doesn't support them yet. -# -ifcapable icu { - - proc do_icu_test {name locale input output} { - set ::out [db eval { SELECT fts2_tokenizer_test('icu', $locale, $input) }] - do_test $name { - lindex $::out 0 - } $output - } - - do_icu_test fts2token-4.1 en_US {} {} - do_icu_test fts2token-4.2 en_US {Test cases fts2} [list \ - 0 test Test 1 cases cases 2 fts2 fts2 - ] - - # The following test shows that ICU is smart enough to recognise - # Thai chararacters, even when the locale is set to English/United - # States. - # - set input "\u0e2d\u0e30\u0e44\u0e23\u0e19\u0e30\u0e04\u0e23\u0e31\u0e1a" - set output "0 \u0e2d\u0e30\u0e44\u0e23 \u0e2d\u0e30\u0e44\u0e23 " - append output "1 \u0e19\u0e30 \u0e19\u0e30 " - append output "2 \u0e04\u0e23\u0e31\u0e1a \u0e04\u0e23\u0e31\u0e1a" - - do_icu_test fts2token-4.3 th_TH $input $output - do_icu_test fts2token-4.4 en_US $input $output - - # ICU handles an unknown locale by falling back to the default. - # So this is not an error. - do_icu_test fts2token-4.5 MiddleOfTheOcean $input $output - - set longtoken "AReallyReallyLongTokenOneThatWillSurelyRequire" - append longtoken "AReallocInTheIcuTokenizerCode" - - set input "short tokens then " - append input $longtoken - set output "0 short short " - append output "1 tokens tokens " - append output "2 then then " - append output "3 [string tolower $longtoken] $longtoken" - - do_icu_test fts2token-4.6 MiddleOfTheOcean $input $output - do_icu_test fts2token-4.7 th_TH $input $output - do_icu_test fts2token-4.8 en_US $input $output -} - -do_test fts2token-internal { - execsql { SELECT fts2_tokenizer_internal_test() } -} {ok} - -finish_test diff --git a/test/fts3_common.tcl b/test/fts3_common.tcl index 2ed1f70bf6..fcd3ca3e45 100644 --- a/test/fts3_common.tcl +++ b/test/fts3_common.tcl @@ -44,6 +44,10 @@ # # +ifcapable fts3 { + sqlite3_fts3_may_be_corrupt 0 +} + #------------------------------------------------------------------------- # USAGE: fts3_build_db_1 SWITCHES N # diff --git a/test/fts3aa.test b/test/fts3aa.test index 08d825dd17..cb1bde74a4 100644 --- a/test/fts3aa.test +++ b/test/fts3aa.test @@ -243,5 +243,23 @@ do_execsql_test 8.5 { SELECT docid FROM t0 WHERE t0 MATCH '"abc abc"'; } {} +do_execsql_test 9.1 { + CREATE VIRTUAL TABLE t9 USING fts4(a, "", '---'); +} +do_execsql_test 9.2 { + CREATE VIRTUAL TABLE t10 USING fts3(<, b, c); +} + +do_execsql_test 10.0 { + CREATE VIRTUAL TABLE z1 USING fts3; + INSERT INTO z1 VALUES('one two three'),('four one five'),('six two five'); + CREATE TRIGGER z1r1 AFTER DELETE ON z1_content BEGIN + DELETE FROM z1; + END; +} +do_catchsql_test 10.1 { + DELETE FROM z1; +} {1 {SQL logic error}} +expand_all_sql db finish_test diff --git a/test/fts3aj.test b/test/fts3aj.test index f3d46f2ad8..0c89691162 100644 --- a/test/fts3aj.test +++ b/test/fts3aj.test @@ -6,8 +6,6 @@ # This file implements regression tests for SQLite library. This # tests creating fts3 tables in an attached database. # -# $Id: fts3aj.test,v 1.1 2007/08/20 17:38:42 shess Exp $ -# set testdir [file dirname $argv0] source $testdir/tester.tcl @@ -25,14 +23,14 @@ sqlite3 db2 test2.db db eval { CREATE VIRTUAL TABLE t3 USING fts3(content); - INSERT INTO t3 (rowid, content) VALUES(1, "hello world"); + INSERT INTO t3 (rowid, content) VALUES(1, 'hello world'); } db2 eval { CREATE VIRTUAL TABLE t1 USING fts3(content); - INSERT INTO t1 (rowid, content) VALUES(1, "hello world"); - INSERT INTO t1 (rowid, content) VALUES(2, "hello there"); - INSERT INTO t1 (rowid, content) VALUES(3, "cruel world"); + INSERT INTO t1 (rowid, content) VALUES(1, 'hello world'); + INSERT INTO t1 (rowid, content) VALUES(2, 'hello there'); + INSERT INTO t1 (rowid, content) VALUES(3, 'cruel world'); } # This has always worked because the t1_* tables used by fts3 will be @@ -56,9 +54,9 @@ do_test fts3aj-1.2 { execsql { ATTACH DATABASE 'test2.db' AS two; CREATE VIRTUAL TABLE two.t2 USING fts3(content); - INSERT INTO t2 (rowid, content) VALUES(1, "hello world"); - INSERT INTO t2 (rowid, content) VALUES(2, "hello there"); - INSERT INTO t2 (rowid, content) VALUES(3, "cruel world"); + INSERT INTO t2 (rowid, content) VALUES(1, 'hello world'); + INSERT INTO t2 (rowid, content) VALUES(2, 'hello there'); + INSERT INTO t2 (rowid, content) VALUES(3, 'cruel world'); SELECT rowid FROM t2 WHERE t2 MATCH 'hello'; DETACH DATABASE two; } @@ -74,8 +72,8 @@ do_test fts3aj-1.3 { ATTACH DATABASE 'test2.db' AS two; CREATE VIRTUAL TABLE two.t3 USING fts3(content); - INSERT INTO two.t3 (rowid, content) VALUES(2, "hello there"); - INSERT INTO two.t3 (rowid, content) VALUES(3, "cruel world"); + INSERT INTO two.t3 (rowid, content) VALUES(2, 'hello there'); + INSERT INTO two.t3 (rowid, content) VALUES(3, 'cruel world'); SELECT rowid FROM two.t3 WHERE t3 MATCH 'hello'; DETACH DATABASE two; diff --git a/test/fts3ak.test b/test/fts3ak.test index a263f0b740..080efe52b5 100644 --- a/test/fts3ak.test +++ b/test/fts3ak.test @@ -21,17 +21,17 @@ ifcapable !fts3 { db eval { CREATE VIRTUAL TABLE t1 USING fts3(content); - INSERT INTO t1 (rowid, content) VALUES(1, "hello world"); - INSERT INTO t1 (rowid, content) VALUES(2, "hello there"); - INSERT INTO t1 (rowid, content) VALUES(3, "cruel world"); + INSERT INTO t1 (rowid, content) VALUES(1, 'hello world'); + INSERT INTO t1 (rowid, content) VALUES(2, 'hello there'); + INSERT INTO t1 (rowid, content) VALUES(3, 'cruel world'); } # Test that possibly-buffered inserts went through after commit. do_test fts3ak-1.1 { execsql { BEGIN TRANSACTION; - INSERT INTO t1 (rowid, content) VALUES(4, "false world"); - INSERT INTO t1 (rowid, content) VALUES(5, "false door"); + INSERT INTO t1 (rowid, content) VALUES(4, 'false world'); + INSERT INTO t1 (rowid, content) VALUES(5, 'false door'); COMMIT TRANSACTION; SELECT rowid FROM t1 WHERE t1 MATCH 'world'; } @@ -42,8 +42,8 @@ do_test fts3ak-1.1 { do_test fts3ak-1.2 { execsql { BEGIN TRANSACTION; - INSERT INTO t1 (rowid, content) VALUES(6, "another world"); - INSERT INTO t1 (rowid, content) VALUES(7, "another test"); + INSERT INTO t1 (rowid, content) VALUES(6, 'another world'); + INSERT INTO t1 (rowid, content) VALUES(7, 'another test'); SELECT rowid FROM t1 WHERE t1 MATCH 'world'; COMMIT TRANSACTION; } @@ -54,8 +54,8 @@ do_test fts3ak-1.2 { do_test fts3ak-1.3 { execsql { BEGIN TRANSACTION; - INSERT INTO t1 (rowid, content) VALUES(8, "second world"); - INSERT INTO t1 (rowid, content) VALUES(9, "second sight"); + INSERT INTO t1 (rowid, content) VALUES(8, 'second world'); + INSERT INTO t1 (rowid, content) VALUES(9, 'second sight'); SELECT rowid FROM t1 WHERE t1 MATCH 'world'; ROLLBACK TRANSACTION; } @@ -73,8 +73,8 @@ do_test fts3ak-1.4 { do_test fts3ak-1.5 { execsql { BEGIN TRANSACTION; - INSERT INTO t1 (rowid, content) VALUES(10, "second world"); - INSERT INTO t1 (rowid, content) VALUES(11, "second sight"); + INSERT INTO t1 (rowid, content) VALUES(10, 'second world'); + INSERT INTO t1 (rowid, content) VALUES(11, 'second sight'); ROLLBACK TRANSACTION; SELECT rowid FROM t1 WHERE t1 MATCH 'world'; } @@ -84,7 +84,7 @@ do_test fts3ak-1.5 { do_test fts3ak-1.6 { execsql { BEGIN; - INSERT INTO t1 (rowid, content) VALUES(12, "third world"); + INSERT INTO t1 (rowid, content) VALUES(12, 'third world'); COMMIT; SELECT rowid FROM t1 WHERE t1 MATCH 'third'; } @@ -95,7 +95,7 @@ do_test fts3ak-1.6 { do_test fts3ak-1.7 { execsql { BEGIN; - INSERT INTO t1 (rowid, content) VALUES(13, "third dimension"); + INSERT INTO t1 (rowid, content) VALUES(13, 'third dimension'); CREATE TABLE x (c); COMMIT; SELECT rowid FROM t1 WHERE t1 MATCH 'dimension'; diff --git a/test/fts3ao.test b/test/fts3ao.test index 60f0aa7d90..d9b2233bfb 100644 --- a/test/fts3ao.test +++ b/test/fts3ao.test @@ -71,7 +71,7 @@ do_test fts3ao-2.5 { CREATE TABLE t1_segdir(a, b, c); ALTER TABLE fts_t1 RENAME to t1; } -} {1 {SQL logic error or missing database}} +} {1 {SQL logic error}} do_test fts3ao-2.6 { execsql { SELECT rowid, snippet(fts_t1) FROM fts_t1 WHERE a MATCH 'four'; } } {1 {one three <b>four</b>}} @@ -91,9 +91,9 @@ do_test fts3ao-2.9 { catchsql { ALTER TABLE fts_t1 RENAME to t1; } -} {1 {SQL logic error or missing database}} +} {1 {SQL logic error}} do_test fts3ao-2.10 { - execsql { SELECT rowid, snippet(fts_t1) FROM fts_t1 WHERE a MATCH 'four'; } + execsql { SELECT rowid, snippet( fts_t1 ) FROM fts_t1 WHERE a MATCH 'four'; } } {1 {one three <b>four</b>}} do_test fts3ao-2.11 { execsql { SELECT tbl_name FROM sqlite_master WHERE type = 'table'} diff --git a/test/fts3atoken.test b/test/fts3atoken.test index 2cdea79a98..4981480b5e 100644 --- a/test/fts3atoken.test +++ b/test/fts3atoken.test @@ -56,40 +56,101 @@ proc escape_string {str} { # # 5: Test that the table created to use tokenizer 'blah' is usable. # -ifcapable fts3_tokenizer { - do_test fts3atoken-1.1 { - catchsql { - CREATE VIRTUAL TABLE t1 USING fts3(content, tokenize blah); - } - } {1 {unknown tokenizer: blah}} - do_test fts3atoken-1.2 { - execsql { - SELECT fts3_tokenizer('blah', fts3_tokenizer('simple')) IS NULL; - } - } {0} - do_test fts3atoken-1.3 { - execsql { - SELECT fts3_tokenizer('blah') == fts3_tokenizer('simple'); - } - } {1} - do_test fts3atoken-1.4 { - catchsql { - CREATE VIRTUAL TABLE t1 USING fts3(content, tokenize blah); - } - } {0 {}} - do_test fts3atoken-1.5 { - execsql { - INSERT INTO t1(content) VALUES('There was movement at the station'); - INSERT INTO t1(content) VALUES('For the word has passed around'); - INSERT INTO t1(content) VALUES('That the colt from ol regret had got'); - SELECT content FROM t1 WHERE content MATCH 'movement' - } - } {{There was movement at the station}} -} else { - do_catchsql_test 1.6 { +sqlite3_db_config db SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER 1 +do_test fts3atoken-1.1 { + catchsql { + CREATE VIRTUAL TABLE t1 USING fts3(content, tokenize blah); + } +} {1 {unknown tokenizer: blah}} +do_test fts3atoken-1.2 { + execsql { SELECT fts3_tokenizer('blah', fts3_tokenizer('simple')) IS NULL; - } {1 {fts3tokenize: disabled - rebuild with -DSQLITE_ENABLE_FTS3_TOKENIZER}} -} + } +} {0} +do_test fts3atoken-1.3 { + execsql { + SELECT fts3_tokenizer('blah') == fts3_tokenizer('simple'); + } +} {1} +do_test fts3atoken-1.4 { + catchsql { + CREATE VIRTUAL TABLE t1 USING fts3(content, tokenize blah); + } +} {0 {}} +do_test fts3atoken-1.5 { + execsql { + INSERT INTO t1(content) VALUES('There was movement at the station'); + INSERT INTO t1(content) VALUES('For the word has passed around'); + INSERT INTO t1(content) VALUES('That the colt from ol regret had got'); + SELECT content FROM t1 WHERE content MATCH 'movement' + } +} {{There was movement at the station}} + +unset -nocomplain simple blah2name simplename +set simplename "simple" +set blah2name "blah2" +set simple [db one {SELECT fts3_tokenizer('simple')}] +sqlite3_db_config db SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER 0 +do_catchsql_test 1.6 { + SELECT fts3_tokenizer('blah', fts3_tokenizer('simple')) IS NULL; +} {1 {fts3tokenize disabled}} +do_test fts3atoken-1.7 { + execsql { + SELECT fts3_tokenizer('blah2', $simple) IS NULL; + } +} {1} + +# With ENABLE_FTS3_TOKENIZER off, the fts3_tokenzer(1) function +# returns NULL unless the first parameter is a bound parameter. +# If the first parameter is a bound parameter, then fts3_tokenizer(1) +# returns the actual pointer value as a BLOB. +# +do_test fts3atoken-1.8 { + execsql { + SELECT fts3_tokenizer($blah2name) == fts3_tokenizer($simplename), + typeof(fts3_tokenizer($blah2name)), + typeof(fts3_tokenizer('blah2')), + typeof(fts3_tokenizer($simplename)), + typeof(fts3_tokenizer('simple')); + } +} {1 blob null blob null} + +# With ENABLE_FTS3_TOKENIZER on, fts3_tokenizer() always returns +# the BLOB pointer, regardless the parameter +# +sqlite3_db_config db SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER 1 +do_test fts3atoken-1.9 { + execsql { + SELECT fts3_tokenizer('blah2') == fts3_tokenizer('simple'), + typeof(fts3_tokenizer($blah2name)), + typeof(fts3_tokenizer('blah2')), + typeof(fts3_tokenizer($simplename)), + typeof(fts3_tokenizer('simple')); + } +} {1 blob blob blob blob} + +# 2019-12-31: The fts3_tokenizer() function can never be invoked from +# within a trigger or view. +# +do_catchsql_test fts3atoken-1.10 { + CREATE VIEW v110(x) AS + SELECT fts3_tokenizer('tok110', fts3_tokenizer('simple')) IS NULL; +} {0 {}} +do_catchsql_test fts3atoken-1.11 { + SELECT * FROM v110; +} {1 {unsafe use of fts3_tokenizer()}} +do_catchsql_test fts3atoken-1.12 { + CREATE TABLE t110(a,b); + CREATE TRIGGER r110 AFTER INSERT ON t110 BEGIN + SELECT fts3_tokenizer('tok110', fts3_tokenizer('simple')) IS NULL; + END; +} {0 {}} +do_catchsql_test fts3atoken-1.13 { + INSERT INTO t110(a,b) VALUES(1,2); +} {1 {unsafe use of fts3_tokenizer()}} +do_catchsql_test fts3atoken-1.14 { + SELECT * FROM t110; +} {0 {}} #-------------------------------------------------------------------------- # Test cases fts3atoken-2.* test error cases in the scalar function based @@ -106,6 +167,7 @@ do_test fts3atoken-2.1 { # simple input string via the built-in test function. This is as much # to test the test function as the tokenizer implementations. # +sqlite3_db_config db SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER 1 do_test fts3atoken-3.1 { execsql { SELECT fts3_tokenizer_test('simple', 'I don''t see how'); @@ -212,14 +274,14 @@ do_catchsql_test 6.1.3 { do_catchsql_test 6.2.1 { SELECT fts3_tokenizer(NULL); } {1 {unknown tokenizer: }} -ifcapable fts3_tokenizer { - do_catchsql_test 6.2.2 { - SELECT fts3_tokenizer(NULL, X'1234567812345678'); - } {1 {argument type mismatch}} - do_catchsql_test 6.2.3 { - SELECT fts3_tokenizer(NULL, X'12345678'); - } {1 {argument type mismatch}} -} + +sqlite3_db_config db SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER 1 +do_catchsql_test 6.2.2 { + SELECT fts3_tokenizer(NULL, X'1234567812345678'); +} {1 {argument type mismatch}} +do_catchsql_test 6.2.3 { + SELECT fts3_tokenizer(NULL, X'12345678'); +} {1 {argument type mismatch}} finish_test diff --git a/test/fts3atoken2.test b/test/fts3atoken2.test new file mode 100644 index 0000000000..f6a2a29ab1 --- /dev/null +++ b/test/fts3atoken2.test @@ -0,0 +1,106 @@ +# 2025 September 5 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# This file implements regression tests for SQLite library. The focus +# of this script is testing the pluggable tokeniser feature of the +# FTS3 module. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +# If SQLITE_ENABLE_FTS3 is defined, omit this file. +ifcapable !fts3 { + finish_test + return +} + +set ::testprefix fts3atoken2 + + +reset_db +sqlite3_db_config db SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER 0 + +# With ENABLE_FTS3_TOKENIZER set to 0: +# +# * It is not possible to get a pointer to a token implementation +# using single arg fts3_tokenize() unless the name of the tokenizer +# is a bound paramter - function should return NULL. +# +# * But it is possible with a bound parameter. +# +do_execsql_test 1.1.1 { + SELECT typeof( fts3_tokenizer('simple') ); +} {null} +set bound "simple" +do_execsql_test 1.1.2 { + SELECT typeof( fts3_tokenizer($bound) ); +} {blob} + +# With ENABLE_FTS3_TOKENIZER set to 0: +# +# * It is not possible to create a token implementation using anything +# other than a bound parameter. +# +# * But it is possible with a bound parameter. +# +set literal [db one {SELECT quote( fts3_tokenizer($bound) )}] +set blob [db one {SELECT fts3_tokenizer($bound) }] + +do_catchsql_test 1.2.1 " + SELECT fts3_tokenizer('mytok', $literal) +" {1 {fts3tokenize disabled}} +do_catchsql_test 1.2.2 { + CREATE VIRTUAL TABLE x1 USING fts3(col, tokenize=mytok); +} {1 {unknown tokenizer: mytok}} +do_catchsql_test 1.2.3 { + SELECT fts3_tokenizer('mytok', $blob) +} {0 {{}}} +do_execsql_test 1.2.4 { + CREATE VIRTUAL TABLE x1 USING fts3(col, tokenize=mytok); +} + +# With ENABLE_FTS3_TOKENIZER set to 1: +# +# * It is possible to get a pointer to a token implementation with either +# a bound parameter or a literal. +# +sqlite3_db_config db SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER 1 +set bound "simple" +do_execsql_test 1.3.1 { + SELECT typeof( fts3_tokenizer('simple') ); +} {blob} +do_execsql_test 1.3.2 { + SELECT typeof( fts3_tokenizer($bound) ); +} {blob} + +# With ENABLE_FTS3_TOKENIZER set to 1: +# +# * It is not possible to create a token implementation using either +# a bound parameter or a literal. +# +set literal [db one {SELECT quote( fts3_tokenizer($bound) )}] +set blob [db one {SELECT fts3_tokenizer($bound) }] + +do_execsql_test 1.4.1 " + SELECT typeof( fts3_tokenizer('mytok2', $literal) ); +" {blob} +do_execsql_test 1.4.2 { + CREATE VIRTUAL TABLE x2 USING fts3(col, tokenize=mytok2); +} +do_execsql_test 1.4.3 { + SELECT typeof( fts3_tokenizer('mytok3', $blob) ); +} {blob} +do_execsql_test 1.4.4 { + CREATE VIRTUAL TABLE x3 USING fts3(col, tokenize=mytok3); +} + +finish_test + diff --git a/test/fts3auto.test b/test/fts3auto.test index 20640d29ac..19193973d9 100644 --- a/test/fts3auto.test +++ b/test/fts3auto.test @@ -134,6 +134,7 @@ proc fts3_make_deferrable {tbl token {nRow 0}} { # fts3_zero_long_segments TABLE ?LIMIT? # proc fts3_zero_long_segments {tbl limit} { + sqlite3_db_config db DEFENSIVE 0 execsql " UPDATE ${tbl}_segments SET block = zeroblob(length(block)) @@ -569,6 +570,13 @@ foreach {tn create} { do_fts3query_test 4.$tn.4.3 -deferred fi* t1 {on* NEAR/1 fi*} do_fts3query_test 4.$tn.4.4 -deferred fi* t1 {on* NEAR/2 fi*} do_fts3query_test 4.$tn.4.5 -deferred fi* t1 {on* NEAR/3 fi*} + + ifcapable fts4_deferred { + db eval {UPDATE t1_stat SET value=x'' WHERE id=0} + do_catchsql_test 4.$tn.4.6 { + SELECT docid FROM t1 WHERE t1 MATCH 'on* NEAR/3 fi*' + } {1 {database disk image is malformed}} + } } #-------------------------------------------------------------------------- diff --git a/test/fts3aux1.test b/test/fts3aux1.test index d17ac85df5..1524d6d30f 100644 --- a/test/fts3aux1.test +++ b/test/fts3aux1.test @@ -105,10 +105,10 @@ db func rec rec # do_execsql_test 2.1.1.1 { EXPLAIN QUERY PLAN SELECT * FROM terms WHERE term='braid' -} { 0 0 0 {SCAN TABLE terms VIRTUAL TABLE INDEX 1:} } +} {/*SCAN terms VIRTUAL TABLE INDEX 1:*/} do_execsql_test 2.1.1.2 { EXPLAIN QUERY PLAN SELECT * FROM terms WHERE +term='braid' -} {0 0 0 {SCAN TABLE terms VIRTUAL TABLE INDEX 0:}} +} {/*SCAN terms VIRTUAL TABLE INDEX 0:*/} # Now show that using "term='braid'" means the virtual table returns # only 1 row to SQLite, but "+term='braid'" means all 19 are returned. @@ -117,12 +117,12 @@ do_test 2.1.2.1 { set cnt 0 execsql { SELECT * FROM terms_v WHERE rec('cnt', term) AND term='braid' } set cnt -} {2} +} {1} do_test 2.1.2.2 { set cnt 0 execsql { SELECT * FROM terms_v WHERE rec('cnt', term) AND +term='braid' } set cnt -} {38} +} {19} # Similar to the test immediately above, but using a term ("breakfast") that # is not featured in the dataset. @@ -136,7 +136,7 @@ do_test 2.1.3.2 { set cnt 0 execsql { SELECT * FROM terms_v WHERE rec('cnt', term) AND +term='breakfast' } set cnt -} {38} +} {19} do_execsql_test 2.1.4.1 { SELECT * FROM terms_v WHERE term='braid' } {braid 1 1} do_execsql_test 2.1.4.2 { SELECT * FROM terms_v WHERE +term='braid'} {braid 1 1} @@ -154,24 +154,24 @@ do_execsql_test 2.1.5 { SELECT * FROM terms WHERE term=NULL } {} do_execsql_test 2.2.1.1 { EXPLAIN QUERY PLAN SELECT * FROM terms WHERE term>'brain' -} { 0 0 0 {SCAN TABLE terms VIRTUAL TABLE INDEX 2:} } +} {/*SCAN terms VIRTUAL TABLE INDEX 2:*/} do_execsql_test 2.2.1.2 { EXPLAIN QUERY PLAN SELECT * FROM terms WHERE +term>'brain' -} { 0 0 0 {SCAN TABLE terms VIRTUAL TABLE INDEX 0:} } +} {/*SCAN terms VIRTUAL TABLE INDEX 0:*/} do_execsql_test 2.2.1.3 { EXPLAIN QUERY PLAN SELECT * FROM terms WHERE term<'brain' -} { 0 0 0 {SCAN TABLE terms VIRTUAL TABLE INDEX 4:} } +} {/*SCAN terms VIRTUAL TABLE INDEX 4:*/} do_execsql_test 2.2.1.4 { EXPLAIN QUERY PLAN SELECT * FROM terms WHERE +term<'brain' -} { 0 0 0 {SCAN TABLE terms VIRTUAL TABLE INDEX 0:} } +} {/*SCAN terms VIRTUAL TABLE INDEX 0:*/} do_execsql_test 2.2.1.5 { EXPLAIN QUERY PLAN SELECT * FROM terms WHERE term BETWEEN 'brags' AND 'brain' -} { 0 0 0 {SCAN TABLE terms VIRTUAL TABLE INDEX 6:} } +} {/*SCAN terms VIRTUAL TABLE INDEX 6:*/} do_execsql_test 2.2.1.6 { EXPLAIN QUERY PLAN SELECT * FROM terms WHERE +term BETWEEN 'brags' AND 'brain' -} { 0 0 0 {SCAN TABLE terms VIRTUAL TABLE INDEX 0:} } +} {/*SCAN terms VIRTUAL TABLE INDEX 0:*/} do_test 2.2.2.1 { set cnt 0 @@ -335,8 +335,9 @@ foreach {tn sort orderby} { 9 1 "ORDER BY occurrences DESC" } { - set res [list 0 0 0 {SCAN TABLE terms VIRTUAL TABLE INDEX 0:}] - if {$sort} { lappend res 0 0 0 {USE TEMP B-TREE FOR ORDER BY} } + set res {SCAN terms VIRTUAL TABLE INDEX 0:} + if {$sort} { append res {*USE TEMP B-TREE FOR ORDER BY} } + set res "/*$res*/" set sql "SELECT * FROM terms $orderby" do_execsql_test 2.3.1.$tn "EXPLAIN QUERY PLAN $sql" $res @@ -364,10 +365,10 @@ do_execsql_test 3.2.1 { } do_catchsql_test 3.2.2 { SELECT * FROM terms3 -} {1 {SQL logic error or missing database}} +} {1 {SQL logic error}} do_catchsql_test 3.2.3 { SELECT * FROM terms3 WHERE term = 'abc' -} {1 {SQL logic error or missing database}} +} {1 {SQL logic error}} do_catchsql_test 3.3.1 { INSERT INTO terms VALUES(1,2,3); @@ -403,39 +404,48 @@ do_execsql_test 4.1 { INSERT INTO x3 SELECT term FROM terms WHERE col = '*'; } -proc do_plansql_test {tn sql r} { - uplevel do_execsql_test $tn [list "EXPLAIN QUERY PLAN $sql ; $sql"] [list $r] +proc do_plansql_test {tn sql r1 r2} { + do_eqp_test $tn.eqp $sql $r1 + do_execsql_test $tn $sql $r2 } do_plansql_test 4.2 { SELECT y FROM x2, terms WHERE y = term AND col = '*' } { - 0 0 0 {SCAN TABLE x2} - 0 1 1 {SCAN TABLE terms VIRTUAL TABLE INDEX 1:} + QUERY PLAN + |--SCAN x2 + `--SCAN terms VIRTUAL TABLE INDEX 1: +} { a b c d e f g h i j k l } do_plansql_test 4.3 { SELECT y FROM terms, x2 WHERE y = term AND col = '*' } { - 0 0 1 {SCAN TABLE x2} - 0 1 0 {SCAN TABLE terms VIRTUAL TABLE INDEX 1:} + QUERY PLAN + |--SCAN x2 + `--SCAN terms VIRTUAL TABLE INDEX 1: +} { a b c d e f g h i j k l } do_plansql_test 4.4 { SELECT y FROM x3, terms WHERE y = term AND col = '*' } { - 0 0 1 {SCAN TABLE terms VIRTUAL TABLE INDEX 0:} - 0 1 0 {SEARCH TABLE x3 USING COVERING INDEX i1 (y=?)} + QUERY PLAN + |--SCAN terms VIRTUAL TABLE INDEX 0: + `--SEARCH x3 USING COVERING INDEX i1 (y=?) +} { a b c d e f g h i j k l } do_plansql_test 4.5 { SELECT y FROM terms, x3 WHERE y = term AND occurrences>1 AND col = '*' } { - 0 0 0 {SCAN TABLE terms VIRTUAL TABLE INDEX 0:} - 0 1 1 {SEARCH TABLE x3 USING COVERING INDEX i1 (y=?)} + QUERY PLAN + |--SCAN terms VIRTUAL TABLE INDEX 0: + `--SEARCH x3 USING COVERING INDEX i1 (y=?) +} { a k l } @@ -516,6 +526,6 @@ do_test 8.1 { do_test 8.2 { execsql {DETACH att} catchsql { SELECT * FROM aux2 } -} {1 {SQL logic error or missing database}} +} {1 {SQL logic error}} finish_test diff --git a/test/fts3aux2.test b/test/fts3aux2.test index e108fc4b80..9554022559 100644 --- a/test/fts3aux2.test +++ b/test/fts3aux2.test @@ -141,4 +141,28 @@ do_execsql_test 1.4.6 { five * 1 1 2 five 0 1 1 2 } +#------------------------------------------------------------------------- +do_execsql_test 2.0 { + CREATE VIRTUAL TABLE ft USING fts3(); + INSERT INTO ft VALUES('a_234567890123456789'); + INSERT INTO ft VALUES('b_234567890123456789'); + INSERT INTO ft VALUES('c_234567890123456789'); + CREATE VIRTUAL TABLE t2 USING fts4aux(ft); +} + +do_execsql_test 2.1 { + SELECT term FROM t2 WHERE term=X'625f323334353637383930313233343536373839'; +} + +do_execsql_test 2.2 { + SELECT term FROM t2 WHERE term<X'625f003334353637383930313233343536373839'; +} { + 234567890123456789 234567890123456789 a a b b +} + +do_execsql_test 2.3 { + SELECT term FROM t2 WHERE term=X'625f003334353637383930313233343536373839'; +} + + finish_test diff --git a/test/fts3b.test b/test/fts3b.test index 9bde3a254c..ee0a1cea68 100644 --- a/test/fts3b.test +++ b/test/fts3b.test @@ -206,7 +206,7 @@ do_test fts3b-4.8 { INSERT INTO t4 (rowid, docid, c) VALUES (14, 15, 'bad test'); SELECT * FROM t4 WHERE docid = 14; } -} {1 {SQL logic error or missing database}} +} {1 {SQL logic error}} do_test fts3b-4.9 { execsql { SELECT docid FROM t4 WHERE t4 MATCH 'testing' } diff --git a/test/fts3conf.test b/test/fts3conf.test index f9698770cc..cd48290195 100644 --- a/test/fts3conf.test +++ b/test/fts3conf.test @@ -136,47 +136,49 @@ do_execsql_test 2.2.2 { COMMIT } do_execsql_test 2.2.3 { SELECT * FROM t1 } {{a b c} {a b c}} fts3_integrity 2.2.4 db t1 -do_execsql_test 3.1 { - CREATE VIRTUAL TABLE t3 USING fts4; - REPLACE INTO t3(docid, content) VALUES (1, 'one two'); - SELECT quote(matchinfo(t3, 'na')) FROM t3 WHERE t3 MATCH 'one' -} {X'0100000002000000'} - -do_execsql_test 3.2 { - REPLACE INTO t3(docid, content) VALUES (2, 'one two three four'); - SELECT quote(matchinfo(t3, 'na')) FROM t3 WHERE t3 MATCH 'four' -} {X'0200000003000000'} - -do_execsql_test 3.3 { - REPLACE INTO t3(docid, content) VALUES (1, 'one two three four five six'); - SELECT quote(matchinfo(t3, 'na')) FROM t3 WHERE t3 MATCH 'six' -} {X'0200000005000000'} - -do_execsql_test 3.4 { - UPDATE OR REPLACE t3 SET docid = 2 WHERE docid=1; - SELECT quote(matchinfo(t3, 'na')) FROM t3 WHERE t3 MATCH 'six' -} {X'0100000006000000'} - -do_execsql_test 3.5 { - UPDATE OR REPLACE t3 SET docid = 3 WHERE docid=2; - SELECT quote(matchinfo(t3, 'na')) FROM t3 WHERE t3 MATCH 'six' -} {X'0100000006000000'} - -do_execsql_test 3.6 { - REPLACE INTO t3(docid, content) VALUES (3, 'one two'); - SELECT quote(matchinfo(t3, 'na')) FROM t3 WHERE t3 MATCH 'one' -} {X'0100000002000000'} - -do_execsql_test 3.7 { - REPLACE INTO t3(docid, content) VALUES (NULL, 'one two three four'); - REPLACE INTO t3(docid, content) VALUES (NULL, 'one two three four five six'); - SELECT docid FROM t3; -} {3 4 5} - -do_execsql_test 3.8 { - UPDATE OR REPLACE t3 SET docid = 5, content='three four' WHERE docid = 4; - SELECT quote(matchinfo(t3, 'na')) FROM t3 WHERE t3 MATCH 'one' -} {X'0200000002000000'} +if {$tcl_platform(byteOrder)=="littleEndian"} { + do_execsql_test 3.1 { + CREATE VIRTUAL TABLE t3 USING fts4; + REPLACE INTO t3(docid, content) VALUES (1, 'one two'); + SELECT quote(matchinfo(t3, 'na')) FROM t3 WHERE t3 MATCH 'one' + } {X'0100000002000000'} + + do_execsql_test 3.2 { + REPLACE INTO t3(docid, content) VALUES (2, 'one two three four'); + SELECT quote(matchinfo(t3, 'na')) FROM t3 WHERE t3 MATCH 'four' + } {X'0200000003000000'} + + do_execsql_test 3.3 { + REPLACE INTO t3(docid, content) VALUES (1, 'one two three four five six'); + SELECT quote(matchinfo(t3, 'na')) FROM t3 WHERE t3 MATCH 'six' + } {X'0200000005000000'} + + do_execsql_test 3.4 { + UPDATE OR REPLACE t3 SET docid = 2 WHERE docid=1; + SELECT quote(matchinfo(t3, 'na')) FROM t3 WHERE t3 MATCH 'six' + } {X'0100000006000000'} + + do_execsql_test 3.5 { + UPDATE OR REPLACE t3 SET docid = 3 WHERE docid=2; + SELECT quote(matchinfo(t3, 'na')) FROM t3 WHERE t3 MATCH 'six' + } {X'0100000006000000'} + + do_execsql_test 3.6 { + REPLACE INTO t3(docid, content) VALUES (3, 'one two'); + SELECT quote(matchinfo(t3, 'na')) FROM t3 WHERE t3 MATCH 'one' + } {X'0100000002000000'} + + do_execsql_test 3.7 { + REPLACE INTO t3(docid, content) VALUES(NULL,'one two three four'); + REPLACE INTO t3(docid, content) VALUES(NULL,'one two three four five six'); + SELECT docid FROM t3; + } {3 4 5} + + do_execsql_test 3.8 { + UPDATE OR REPLACE t3 SET docid = 5, content='three four' WHERE docid = 4; + SELECT quote(matchinfo(t3, 'na')) FROM t3 WHERE t3 MATCH 'one' + } {X'0200000002000000'} +} #------------------------------------------------------------------------- # Test that the xSavepoint is invoked correctly if the first write @@ -196,7 +198,8 @@ do_execsql_test 4.1.2 { do_execsql_test 4.1.3 { SELECT * FROM t0 WHERE t0 MATCH 'abc'; INSERT INTO t0(t0) VALUES('integrity-check'); -} {} + PRAGMA integrity_check; +} {ok} do_execsql_test 4.2.1 { CREATE VIRTUAL TABLE t01 USING fts4; @@ -209,6 +212,45 @@ do_execsql_test 4.2.1 { do_execsql_test 4.2.2 { SELECT * FROM t01 WHERE t01 MATCH 'b'; INSERT INTO t01(t01) VALUES('integrity-check'); + PRAGMA integrity_check; +} {ok} + +do_execsql_test 4.3.1 { + CREATE VIRTUAL TABLE t02 USING fts4; + INSERT INTO t01 VALUES('1 1 1'); + INSERT INTO t02 VALUES('2 2 2'); + BEGIN; + SAVEPOINT abc; + INSERT INTO t01 VALUES('a b c'); + INSERT INTO t02 VALUES('a b c'); + ROLLBACK TO abc; + COMMIT; +} +do_execsql_test 4.3.2 { + SELECT * FROM t01 WHERE t01 MATCH 'b'; + INSERT INTO t01(t01) VALUES('integrity-check'); } {} +do_execsql_test 4.4.1 { + CREATE TABLE A(ID INTEGER PRIMARY KEY, AnotherID INTEGER, Notes TEXT); + CREATE VIRTUAL TABLE AFTS USING FTS4 (Notes); + CREATE TRIGGER A_DeleteTrigger AFTER DELETE ON A FOR EACH ROW BEGIN + DELETE FROM AFTS WHERE rowid=OLD.ID; + END; + CREATE TABLE B(ID INTEGER PRIMARY KEY,Notes TEXT); + CREATE VIRTUAL TABLE BFTS USING FTS3 (Notes); + CREATE TRIGGER B_DeleteTrigger AFTER DELETE ON B FOR EACH ROW BEGIN + DELETE FROM BFTS WHERE rowid=OLD.ID; + END; +} + +do_execsql_test 4.4.2 { + BEGIN TRANSACTION; + DELETE FROM A WHERE AnotherID=1; + DELETE FROM B WHERE ID=1; + COMMIT; +} + + + finish_test diff --git a/test/fts3corrupt.test b/test/fts3corrupt.test index cb50e3e46a..69d5030be9 100644 --- a/test/fts3corrupt.test +++ b/test/fts3corrupt.test @@ -23,6 +23,7 @@ set ::testprefix fts3corrupt # extends past the end of the node on which it resides is correctly identified # as database corruption. # +sqlite3_db_config db DEFENSIVE 0 do_execsql_test 1.0 { CREATE VIRTUAL TABLE t1 USING fts3; INSERT INTO t1 VALUES('hello'); @@ -164,5 +165,70 @@ do_catchsql_test 5.3 { } {1 {database disk image is malformed}} do_test 5.3.1 { sqlite3_extended_errcode db } SQLITE_CORRUPT_VTAB +# 2019-11-18 https://bugs.chromium.org/p/chromium/issues/detail?id=1025467 +# bug1 +db close +sqlite3 db :memory: +do_catchsql_test 6.10 { + CREATE VIRTUAL TABLE f using fts3(a,b); + CREATE TABLE f_stat(id INTEGER PRIMARY KEY, value BLOB); + INSERT INTO f_segdir VALUES (2000, 0,0,0, '16', ''); + INSERT INTO f_segdir VALUES (1999, 0,0,0, '0 18', + x'000131030102000103323334050101010200'); + INSERT INTO f_segments (blockid) values (16); + INSERT INTO f_segments values (0, x''); + INSERT INTO f_stat VALUES (1,x'cf0f01'); + INSERT INTO f(f) VALUES ('merge=1'); +} {1 {database disk image is malformed}} + +# 2020-03-02 https://bugs.chromium.org/p/chromium/issues/detail?id=1057441 +# The ticket complains of use of an uninitialized value. That part is harmless. +# The only reason to fix this is the failure to detect a subtly corrupt +# inverted index. +# +reset_db +do_catchsql_test 7.10 { + CREATE VIRTUAL TABLE f USING fts3(a,b); + INSERT INTO f_segdir VALUES (0,0,1,0,'0 0',x'01010101020101'); + SELECT matchinfo( f , 'pcx') FROM f WHERE b MATCH x'c533'; +} {1 {database disk image is malformed}} + +reset_db +sqlite3_fts3_may_be_corrupt 1 +do_execsql_test 8.1 { + CREATE VIRTUAL TABLE f USING fts3(a); + INSERT INTO f(f) VALUES('nodesize=24'); + BEGIN; + INSERT INTO f VALUES('abcdefghijklmnopqrstuvwxyz0123456789'); + INSERT INTO f VALUES('abcdefghijklmnopqrstuvwxyz0123456789'); + INSERT INTO f VALUES('abcdefghijklmnopqrstuvwxyz0123456789'); + + INSERT INTO f VALUES('abcdefghijklmnopqrstuvwxyz012345678X'); + INSERT INTO f VALUES('abcdefghijklmnopqrstuvwxyz012345678X'); + INSERT INTO f VALUES('abcdefghijklmnopqrstuvwxyz012345678X'); + COMMIT; + BEGIN; + INSERT INTO f VALUES('abcdefghijklmnopqrstuvwxyz0123456789'); + INSERT INTO f VALUES('abcdefghijklmnopqrstuvwxyz0123456789'); + INSERT INTO f VALUES('abcdefghijklmnopqrstuvwxyz0123456789'); + + INSERT INTO f VALUES('abcdefghijklmnopqrstuvwxyz012345678X'); + INSERT INTO f VALUES('abcdefghijklmnopqrstuvwxyz012345678X'); + INSERT INTO f VALUES('abcdefghijklmnopqrstuvwxyz012345678X'); + COMMIT; + + SELECT count(*) FROM f_segments; +} {4} + +do_execsql_test 8.2 { + UPDATE f_segments SET block = ( + SELECT block FROM f_segments WHERE blockid=1 + ) WHERE blockid=2 +} + +do_catchsql_test 8.3 { + INSERT INTO f(f) VALUES('merge=2,2'); +} {1 {database disk image is malformed}} +sqlite3_fts3_may_be_corrupt 0 finish_test diff --git a/test/fts3corrupt2.test b/test/fts3corrupt2.test index 78c76778f5..58643534f1 100644 --- a/test/fts3corrupt2.test +++ b/test/fts3corrupt2.test @@ -16,6 +16,7 @@ source $testdir/tester.tcl ifcapable !fts3 { finish_test ; return } set ::testprefix fts3corrupt2 +sqlite3_fts3_may_be_corrupt 1 set data [list] lappend data {*}{ @@ -49,6 +50,7 @@ lappend data {*}{ "acvmldguld asdvz aqb aeomsyzyu aggylhprbdz asrfkwz auipybpsn agsnszzfb" } +sqlite3_db_config db DEFENSIVE 0 do_test fts3corrupt2-1.0 { execsql BEGIN execsql { CREATE VIRTUAL TABLE t2 USING FTS3(a, b); } @@ -106,5 +108,4 @@ foreach c {50 100 150 200 250} { - finish_test diff --git a/test/fts3corrupt3.test b/test/fts3corrupt3.test new file mode 100644 index 0000000000..eae13f222b --- /dev/null +++ b/test/fts3corrupt3.test @@ -0,0 +1,66 @@ +# 2010 October 27 +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# Test that the FTS3 extension does not crash when it encounters a +# corrupt data structure on disk. +# + + +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +# If SQLITE_ENABLE_FTS3 is not defined, omit this file. +ifcapable !fts3 { finish_test ; return } + +set ::testprefix fts3corrupt3 + +#------------------------------------------------------------------------- +# Test that fts3 does not choke on an oversized varint. +# +do_execsql_test 1.0 { + PRAGMA page_size = 512; + CREATE VIRTUAL TABLE t1 USING fts3; + BEGIN; + INSERT INTO t1 VALUES('one'); + INSERT INTO t1 VALUES('one'); + INSERT INTO t1 VALUES('one'); + COMMIT; +} +do_execsql_test 1.1 { + SELECT quote(root) from t1_segdir; +} {X'00036F6E6509010200010200010200'} +sqlite3_db_config db DEFENSIVE 0 +do_execsql_test 1.2 { + UPDATE t1_segdir SET root = X'00036F6E650EFFFFFFFFFFFFFFFFFFFFFFFF0200'; +} +do_catchsql_test 1.3 { + SELECT rowid FROM t1 WHERE t1 MATCH 'one' +} {0 -1} + +#------------------------------------------------------------------------- +# Interior node with the prefix or suffix count of an entry set to a +# negative value. +# +set doc1 [string repeat "x " 600] +set doc2 [string repeat "y " 600] +set doc3 [string repeat "z " 600] + +do_execsql_test 2.0 { + CREATE VIRTUAL TABLE t2 USING fts3; + BEGIN; + INSERT INTO t2 VALUES($doc1); + INSERT INTO t2 VALUES($doc2); + INSERT INTO t2 VALUES($doc3); + COMMIT; +} +do_execsql_test 2.1 { + SELECT quote(root) from t2_segdir; +} {X'0101017900017A'} + + + +finish_test diff --git a/test/fts3corrupt4.test b/test/fts3corrupt4.test new file mode 100644 index 0000000000..01effa0850 --- /dev/null +++ b/test/fts3corrupt4.test @@ -0,0 +1,7690 @@ +# 2006 September 9 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# This file implements regression tests for SQLite library. The +# focus of this script is testing the FTS3 module. +# +# $Id: fts3aa.test,v 1.1 2007/08/20 17:38:42 shess Exp $ +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/fts3_common.tcl +set testprefix fts3corrupt4 + +# If SQLITE_ENABLE_FTS3 is defined, omit this file. +ifcapable !fts3 { + finish_test + return +} + +sqlite3_fts3_may_be_corrupt 1 +database_may_be_corrupt +extra_schema_checks 0 + +do_execsql_test 1.0 { + BEGIN; + CREATE VIRTUAL TABLE ft USING fts3; + INSERT INTO ft VALUES('aback'); + INSERT INTO ft VALUES('abaft'); + INSERT INTO ft VALUES('abandon'); + COMMIT; +} + +proc blob {a} { binary decode hex $a } +db func blob blob + +do_execsql_test 1.1 { + SELECT quote(root) FROM ft_segdir; +} {X'0005616261636B03010200030266740302020003046E646F6E03030200'} + +sqlite3_db_config db DEFENSIVE 0 +do_execsql_test 1.2 { + UPDATE ft_segdir SET root = blob( + '0005616261636B03010200 FFFFFFFF0702 66740302020003046E646F6E03030200' + ); +} + +do_catchsql_test 1.3 { + SELECT * FROM ft WHERE ft MATCH 'abandon'; +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 2.0.0 { + CREATE VIRTUAL TABLE ft USING fts3; + INSERT INTO ft(ft) VALUES('nodesize=32'); +} +do_test 2.0.1 { + for {set i 0} {$i < 12} {incr i} { + execsql { + BEGIN; + INSERT INTO ft VALUES('abc' || $i); + INSERT INTO ft VALUES('abc' || $i || 'x' ); + INSERT INTO ft VALUES('abc' || $i || 'xx' ); + COMMIT + } + } + execsql { + SELECT count(*) FROM ft_segdir; + SELECT count(*) FROM ft_segments; + } +} {12 0} + +do_execsql_test 2.1 { + INSERT INTO ft(ft) VALUES('merge=1,4'); + SELECT count(*) FROM ft_segdir; + SELECT count(*) FROM ft_segments; +} {12 3} + +do_execsql_test 2.2 { + SELECT quote(block) FROM ft_segments WHERE blockid=2 +} {X'00056162633130031F0200'} + +db func blob blob +sqlite3_db_config db DEFENSIVE 0 +do_execsql_test 2.3.1 { + UPDATE ft_segments SET block = + blob('00056162633130031F0200 FFFFFFFF07FF55 66740302020003046E646F6E03030200') + WHERE blockid=2; +} {} +do_catchsql_test 2.3.2 { + INSERT INTO ft(ft) VALUES('merge=1,4'); +} {1 {database disk image is malformed}} + +do_execsql_test 2.4.1 { + UPDATE ft_segments SET block = + blob('00056162633130031F0200 02FFFFFFFF07 66740302020003046E646F6E03030200') + WHERE blockid=2; +} {} +do_catchsql_test 2.4.2 { + INSERT INTO ft(ft) VALUES('merge=1,4'); +} {1 {database disk image is malformed}} + +do_execsql_test 2.5.1 { + UPDATE ft_segments SET block = + blob('00056162633130031F0200 0202 6674 FFFFFF070302020003046E646F6E030200') + WHERE blockid=2; +} {} +do_catchsql_test 2.5.2 { + INSERT INTO ft(ft) VALUES('merge=1,4'); +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 3.0.0 { + CREATE VIRTUAL TABLE ft USING fts3; + INSERT INTO ft(ft) VALUES('nodesize=32'); +} +do_test 3.0.1 { + execsql BEGIN + for {set i 0} {$i < 20} {incr i} { + execsql { INSERT INTO ft VALUES('abc' || $i) } + } + execsql { + COMMIT; + SELECT count(*) FROM ft_segdir; + SELECT count(*) FROM ft_segments; + } +} {1 5} + +do_execsql_test 3.1 { + SELECT quote(root) FROM ft_segdir +} {X'0101056162633132040136030132030136'} + +db func blob blob +sqlite3_db_config db DEFENSIVE 0 +do_execsql_test 3.2 { + UPDATE ft_segdir + SET root = blob('0101056162633132FFFFFFFF070236030132030136'); +} + +do_catchsql_test 3.1 { + SELECT * FROM ft WHERE ft MATCH 'abc20' +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 4.0 { + CREATE VIRTUAL TABLE t1 USING fts3(); + INSERT INTO t1 VALUES('one two three'); + UPDATE t1_segdir SET start_block = 1; +} + +do_catchsql_test 4.1 { + SELECT * FROM t1 WHERE t1 MATCH 'one'; +} {1 {database disk image is malformed}} +do_catchsql_test 4.2 { + SELECT * FROM t1 WHERE t1 MATCH 'two'; +} {1 {database disk image is malformed}} +do_catchsql_test 4.3 { + SELECT * FROM t1 WHERE t1 MATCH 'three'; +} {1 {database disk image is malformed}} +do_execsql_test 4.4 { + INSERT INTO t1(t1) VALUES('optimize'); +} + +#------------------------------------------------------------------------- +reset_db +do_test 5.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 24576 pagesize 4096 filename c15.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 04 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 04 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 96: 00 00 00 00 0d 0e f9 00 06 0d ec 00 0f cd 0f 69 ...............i +| 112: 0f 01 0e 10 0e c6 0d ec 00 00 00 00 00 00 00 00 ................ +| 3552: 00 00 00 00 00 00 00 00 00 00 00 00 22 06 06 17 ................ +| 3568: 11 11 01 31 74 61 62 6c 65 74 32 74 32 06 43 52 ...1tablet2t2.CR +| 3584: 45 41 54 45 20 54 41 42 4c 45 20 74 32 28 78 29 EATE TABLE t2(x) +| 3600: 81 33 04 07 17 1f 1f 01 82 35 74 61 62 6c 65 74 .3.......5tablet +| 3616: 31 5f 73 65 67 64 69 72 74 31 5f 73 65 67 64 69 1_segdirt1_segdi +| 3632: 72 04 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 r.CREATE TABLE ' +| 3648: 74 31 5f 73 65 67 64 69 72 27 28 6c 65 76 65 6c t1_segdir'(level +| 3664: 20 49 4e 54 45 47 45 52 2c 69 64 78 20 49 4e 54 INTEGER,idx INT +| 3680: 45 47 45 52 2c 73 74 61 72 74 5f 62 6c 6f 63 6b EGER,start_block +| 3696: 20 49 4e 54 45 47 45 52 2c 6c 65 61 76 65 73 5f INTEGER,leaves_ +| 3712: 65 6e 64 5f 62 6c 6f 63 6b 20 49 4e 54 45 47 45 end_block INTEGE +| 3728: 52 2c 65 6e 64 5f 62 6c 6f 63 6b 20 49 4e 54 45 R,end_block INTE +| 3744: 47 45 52 2c 72 6f 6f 74 20 42 4c 4f 42 2c 50 52 GER,root BLOB,PR +| 3760: 49 4d 41 52 59 20 4b 45 59 28 6c 65 76 65 6c 2c IMARY KEY(level, +| 3776: 20 69 64 78 29 29 31 05 06 17 45 1f 01 00 69 6e idx))1...E...in +| 3792: 64 65 78 73 71 6c 69 74 65 5f 61 75 74 6f 69 6e dexsqlite_autoin +| 3808: 64 65 79 5f 74 31 5f 73 65 67 64 69 72 5f 31 74 dey_t1_segdir_1t +| 3824: 31 5f 73 65 67 64 69 72 05 00 00 00 08 00 00 00 1_segdir........ +| 3840: 00 66 03 07 17 23 23 01 81 13 74 61 62 6c 65 74 .f...##...tablet +| 3856: 31 5f 73 65 67 6d 65 6e 74 73 74 31 5f 73 65 67 1_segmentst1_seg +| 3872: 6d 65 6e 74 73 03 43 52 45 41 54 45 20 54 41 42 ments.CREATE TAB +| 3888: 4c 45 20 27 74 31 5f 73 65 67 6d 65 6e 74 73 27 LE 't1_segments' +| 3904: 28 62 6c 6f 63 6b 69 64 20 49 4e 54 45 47 45 52 (blockid INTEGER +| 3920: 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 62 6c PRIMARY KEY, bl +| 3936: 6f 63 6b 20 42 4c 4f 42 29 62 02 07 17 21 21 01 ock BLOB)b...!!. +| 3952: 81 0f 74 61 62 6c 65 74 31 5f 63 6f 6e 74 65 6e ..tablet1_conten +| 3968: 74 74 31 5f 63 6f 6e 74 65 6e 74 02 43 52 45 41 tt1_content.CREA +| 3984: 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 6f 6e TE TABLE 't1_con +| 4000: 74 65 6e 74 27 28 64 6f 63 69 64 20 49 4e 54 45 tent'(docid INTE +| 4016: 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c GER PRIMARY KEY, +| 4032: 20 27 63 30 63 6f 6e 74 65 6e 74 27 29 31 01 06 'c0content')1.. +| 4048: 17 11 11 08 51 74 61 62 6c 65 74 31 74 31 43 52 ....Qtablet1t1CR +| 4064: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4080: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 33 LE t1 USING fts3 +| page 2 offset 4096 +| 0: 0d 00 00 00 03 0f e0 00 0f f6 0f ec 0f e0 00 00 ................ +| 4064: 0a 03 03 00 1b 61 62 61 6e 64 6f 6e 08 02 03 00 .....abandon.... +| 4080: 17 61 62 61 66 74 08 01 03 00 17 61 62 61 63 6b .abaft.....aback +| page 3 offset 8192 +| 0: 0d 00 00 00 00 10 00 00 00 00 00 00 00 00 00 00 ................ +| page 4 offset 12288 +| 0: 0d 00 00 00 01 0f d6 00 0f d6 00 00 00 00 00 00 ................ +| 4048: 00 00 00 00 00 00 28 01 07 08 08 08 08 15 46 30 ......(.......F0 +| 4064: 20 32 39 00 05 61 62 61 63 6b 03 01 02 00 03 02 29..aback...... +| 4080: 66 74 03 02 02 00 03 04 6e 64 6f 60 30 30 20 00 ft......ndo`00 . +| page 5 offset 16384 +| 0: a0 00 00 00 10 ff b0 00 ff fb 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 00 04 04 08 08 09 ................ +| page 6 offset 20480 +| 0: 0d 00 00 00 05 0f b8 00 0f f4 0f e9 0f d6 0f c7 ................ +| 16: 0f b8 27 74 31 5f 63 6f 6e 74 65 6e 74 27 28 64 ..'t1_content'(d +| 32: 6f 63 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 ocid INTEGER PRI +| 48: 4d 41 52 59 20 4b 45 59 2c 20 27 63 30 63 6f 6e MARY KEY, 'c0con +| 64: 74 65 6e 74 27 29 31 01 06 17 11 11 08 51 74 61 tent')1......Qta +| 80: 62 6c 65 74 31 74 31 43 52 45 41 54 45 20 56 49 blet1t1CREATE VI +| 96: 52 54 55 41 4c 20 54 41 42 4c 45 20 74 31 20 55 RTUAL TABLE t1 U +| 112: 53 49 4e 47 20 66 74 73 33 0d 00 00 00 03 0f e0 SING fts3....... +| 128: 00 0f f6 0f ec 0f e0 00 00 00 00 00 00 00 00 00 ................ +| 4016: 00 00 00 00 00 00 00 00 0d 05 02 23 61 75 74 6f ...........#auto +| 4032: 6d 65 72 67 65 3d 35 0d 04 02 23 6d 65 72 67 65 merge=5...#merge +| 4048: 3d 31 30 30 2c 38 11 03 02 2b 69 6e 74 65 67 72 =100,8...+integr +| 4064: 69 74 79 2d 63 68 65 63 6b 09 02 02 1b 72 65 62 ity-check....reb +| 4080: 75 69 6c 64 0a 01 02 1d 6f 70 74 69 6d 69 7a 65 uild....optimize +| end c15.db +}]} {} + +do_catchsql_test 5.1 { + SELECT * FROM t1 WHERE t1 MATCH 'abandon'; +} {1 {malformed database schema (sqlite_autoindey_t1_segdir_1) - orphan index}} + + +#------------------------------------------------------------------------- +reset_db +database_may_be_corrupt +do_execsql_test 6.0 { + CREATE VIRTUAL TABLE Table0 USING fts3(); + INSERT INTO Table0_segdir VALUES(1,NULL,1,NULL,NULL,NULL); +} + +do_catchsql_test 6.1 { + SELECT * FROM Table0 WHERE Table0 MATCH 'a'; +} {0 {}} + +do_catchsql_test 6.2 { + INSERT INTO Table0(Table0) VALUES('optimize'); +} {0 {}} + +#------------------------------------------------------------------------- +reset_db +do_test 7.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 32768 pagesize 4096 filename crash-04bb6e7c811ce9.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 07 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 06 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 96: 00 00 00 00 0d 0e ef 00 07 0d 4d 00 0f bd 0f 5f ..........M...._ +| 112: 0e f7 0e 06 0e bc 0d a4 0d 4d 00 00 00 00 00 00 .........M...... +| 3392: 00 00 00 00 00 00 00 00 00 00 00 00 00 55 07 07 .............U.. +| 3408: 17 1b 1b 01 81 01 74 61 62 6c 65 74 31 5f 73 74 ......tablet1_st +| 3424: 61 74 74 31 5f 73 74 61 74 07 43 52 45 41 54 45 att1_stat.CREATE +| 3440: 20 54 41 42 4c 45 20 27 74 31 5f 73 74 61 74 27 TABLE 't1_stat' +| 3456: 28 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d (id INTEGER PRIM +| 3472: 41 52 59 20 4b 45 59 2c 20 76 61 6c 75 65 20 42 ARY KEY, value B +| 3488: 4c 4f 42 29 60 06 07 17 21 21 01 81 0b 74 61 62 LOB)`...!!...tab +| 3504: 6c 65 74 31 5f 64 6f 63 73 69 7a 65 74 31 5f 64 let1_docsizet1_d +| 3520: 6f 63 73 69 7a 65 06 43 52 45 41 54 45 20 54 41 ocsize.CREATE TA +| 3536: 42 4c 45 20 27 74 31 5f 64 6f 63 73 69 7a 65 27 BLE 't1_docsize' +| 3552: 28 64 6f 63 69 64 20 49 4e 54 45 47 45 52 20 50 (docid INTEGER P +| 3568: 52 49 4d 41 52 59 20 4b 45 59 2c 20 73 69 7a 65 RIMARY KEY, size +| 3584: 20 42 4c 4f 42 29 81 33 04 07 17 1f 1f 01 82 35 BLOB).3.......5 +| 3600: 74 61 62 6c 65 74 31 5f 73 65 67 64 69 72 74 31 tablet1_segdirt1 +| 3616: 5f 73 65 67 64 69 72 04 43 52 45 41 54 45 20 54 _segdir.CREATE T +| 3632: 41 42 4c 45 20 27 74 31 5f 73 65 67 64 69 72 27 ABLE 't1_segdir' +| 3648: 28 6c 65 76 65 6c 20 49 4e 54 45 47 45 52 2c 69 (level INTEGER,i +| 3664: 64 78 20 49 4e 54 45 47 45 52 2c 73 74 61 72 74 dx INTEGER,start +| 3680: 5f 62 6c 6f 63 6b 20 49 4e 54 45 47 45 52 2c 6c _block INTEGER,l +| 3696: 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 6b 20 eaves_end_block +| 3712: 49 4e 54 45 47 45 52 2c 65 6e 64 5f 62 6c 6f 63 INTEGER,end_bloc +| 3728: 6b 20 49 4e 54 45 47 45 52 2c 72 6f 6f 74 20 42 k INTEGER,root B +| 3744: 4c 4f 42 2c 50 52 49 4d 41 52 59 20 4b 45 59 28 LOB,PRIMARY KEY( +| 3760: 6c 65 76 65 6c 2c 20 69 64 78 29 29 31 05 06 17 level, idx))1... +| 3776: 45 1f 01 00 69 6e 64 65 78 73 71 6c 69 74 65 5f E...indexsqlite_ +| 3792: 61 75 74 6f 69 6e 64 65 78 5f 74 31 5f 73 65 67 autoindex_t1_seg +| 3808: 64 69 72 5f 31 74 31 5f 73 65 67 64 69 72 05 00 dir_1t1_segdir.. +| 3824: 00 00 08 00 00 00 00 66 03 07 17 23 23 01 81 13 .......f...##... +| 3840: 74 61 62 6c 65 74 31 5f 73 65 67 6d 65 6e 74 73 tablet1_segments +| 3856: 74 31 5f 73 65 67 6d 65 6e 74 73 03 43 52 45 41 t1_segments.CREA +| 3872: 54 45 20 54 41 42 4c 45 20 27 74 31 5f 73 65 67 TE TABLE 't1_seg +| 3888: 6d 65 6e 74 73 27 28 62 6c 6f 63 6b 69 64 20 49 ments'(blockid I +| 3904: 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b NTEGER PRIMARY K +| 3920: 45 59 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 29 5c EY, block BLOB). +| 3936: 02 07 17 21 21 01 81 03 74 61 62 6c 65 74 31 5f ...!!...tablet1_ +| 3952: 63 6f 6e 74 65 6e 74 74 31 5f 63 6f 6e 74 65 6e contentt1_conten +| 3968: 74 02 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 t.CREATE TABLE ' +| 3984: 74 31 5f 63 6f 6e 74 65 6e 74 27 28 64 6f 63 69 t1_content'(doci +| 4000: 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 d INTEGER PRIMAR +| 4016: 59 20 4b 45 59 2c 20 27 63 30 61 27 29 41 01 06 Y KEY, 'c0a')A.. +| 4032: 17 11 11 08 71 74 61 62 6c 65 74 31 74 31 43 52 ....qtablet1t1CR +| 4048: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4064: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 34 LE t1 USING fts4 +| 4080: 28 61 2c 70 72 65 66 69 78 3d 27 31 2c 32 27 29 (a,prefix='1,2') +| page 2 offset 4096 +| 0: 0d 00 00 00 08 0e 1f 00 0f c4 0f 7c 0f 34 0f 07 ...........|.4.. +| 16: 0e c3 0e 97 0e 63 0e 1f 00 00 00 00 00 00 00 00 .....c.......... +| 3600: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 42 ...............B +| 3616: 08 04 00 81 09 73 75 6e 74 20 69 6e 20 63 75 6c .....sunt in cul +| 3632: 70 61 20 71 75 69 20 6f 66 66 69 63 69 61 20 64 pa qui officia d +| 3648: 65 73 65 72 75 6e 74 20 6d 6f 6c 6c 69 74 20 61 eserunt mollit a +| 3664: 6e 69 6d 20 69 64 20 65 73 74 20 6c 61 62 6f 72 nim id est labor +| 3680: 75 6d 2e 32 07 03 00 6b 45 78 63 65 70 74 65 75 um.2...kExcepteu +| 3696: 72 20 73 69 6e 74 20 6f 63 63 61 65 63 67 42 06 r sint occaecgB. +| 3712: 37 57 06 96 46 17 46 17 42 06 e6 f6 e2 07 07 26 7W..F.F.B......& +| 3728: f6 96 46 56 e7 42 c2 a0 60 30 05 b6 36 96 c6 c7 ..FV.B..`0..6... +| 3744: 56 d2 06 46 f6 c6 f7 26 52 06 57 52 06 67 56 7c V..F...&R.WR.gV| +| 3760: 65 3f 04 20 6e 75 6c 6c 61 20 70 61 72 69 61 74 e?. nulla pariat +| 3776: 75 72 2e 42 05 04 00 81 09 44 75 69 73 20 61 75 ur.B.....Duis au +| 3792: 74 65 20 69 72 75 72 65 20 64 6f 6c 6f 72 20 69 te irure dolor i +| 3808: 6e 20 72 65 70 72 65 68 65 6e 64 65 72 69 74 20 n reprehenderit +| 3824: 69 6e 20 76 6f 6c 75 70 74 61 74 65 20 76 65 6c in voluptate vel +| 3840: 69 74 20 65 73 73 65 2b 04 03 00 5d 6e 69 73 69 it esse+...]nisi +| 3856: 20 75 74 20 61 6c 69 71 75 69 70 20 65 78 20 65 ut aliquip ex e +| 3872: 61 20 63 6f 6d 6d 6f 64 6f 20 63 6f 6e 73 65 71 a commodo conseq +| 3888: 75 61 74 2e 46 03 04 00 81 11 55 74 20 65 6e 69 uat.F.....Ut eni +| 3904: 6d 20 61 64 20 6d 69 6e 69 6d 20 76 65 6e 69 61 m ad minim venia +| 3920: 6d 2c 20 71 75 69 73 20 6e 6f 73 74 72 75 64 20 m, quis nostrud +| 3936: 65 78 65 72 63 69 74 61 74 69 6f 6e 20 75 6c 6c exercitation ull +| 3952: 61 6d 63 6f 20 6c 61 62 6f 72 69 73 46 02 04 00 amco laborisF... +| 3968: 81 11 73 65 64 20 64 6f 20 65 69 75 73 6d 6f 64 ..sed do eiusmod +| 3984: 20 74 65 6d 70 6f 72 20 69 6e 63 69 64 69 64 75 tempor incididu +| 4000: 6e 74 20 75 74 20 6c 61 62 6f 72 65 20 65 74 20 nt ut labore et +| 4016: 64 6f 6c 6f 72 65 20 6d 61 67 6e 61 20 61 6c 69 dolore magna ali +| 4032: 71 75 61 2e 3a 01 03 00 7b 4c 6f 72 65 6d 20 69 qua.:....Lorem i +| 4048: 70 73 75 6d 20 64 6f 6c 6f 72 20 73 69 72 12 29 psum dolor sir.) +| 4064: 0d 65 74 2c 20 63 6f 6e 73 65 63 74 65 74 75 72 .et, consectetur +| 4080: 20 61 64 69 70 69 73 63 69 6e 67 20 65 6c 69 74 adipiscing elit +| page 3 offset 8192 +| 0: 0d 00 00 00 00 10 30 00 10 17 50 30 80 20 00 00 ......0...P0. .. +| 16: 27 46 50 30 20 50 00 00 27 56 c0 30 30 a0 00 10 'FP0 P..'V.00... +| 32: 17 40 90 20 70 00 10 20 00 10 30 00 00 27 66 50 .@. p.. ..0..'fP +| 48: 60 30 60 00 20 a0 00 10 16 f0 30 50 90 08 20 a0 `0`. .....0P.. . +| 64: 20 80 20 80 80 81 78 40 20 40 03 02 03 23 53 10 . ...x@ @...#S. +| 80: 00 16 11 30 10 60 40 00 10 c0 00 10 40 00 00 00 ...0.`@.....@... +| 96: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 78 24 ..............x$ +| 112: 41 71 83 37 10 27 97 60 10 40 00 00 16 41 10 10 Aq.7.'.`.@...A.. +| 128: 40 00 10 30 90 00 30 20 50 00 10 30 00 20 70 00 @..0..0 P..0. p. +| 144: 00 16 51 b0 10 90 00 10 40 70 00 10 30 80 00 10 ..Q.....@p..0... +| 160: 50 30 00 10 b0 00 10 40 00 10 20 00 10 b0 00 00 P0.....@.. ..... +| 176: 16 60 30 60 50 00 00 16 90 f0 10 30 00 10 60 00 .`0`P......0..`. +| 192: 30 40 40 40 00 30 30 90 00 00 16 c0 c0 10 20 00 0@@@.00....... . +| 208: 10 80 00 10 b0 00 50 c0 00 00 16 d0 90 20 b0 00 ......P...... .. +| 224: 10 50 00 50 80 00 00 16 e0 c0 30 80 00 10 20 00 .P.P......0... . +| 240: 20 60 00 10 60 00 00 16 f0 60 70 40 00 10 60 00 `..`....`p@..`. +| 256: 00 17 00 60 60 70 00 10 70 00 00 17 10 60 30 70 ...``p..p....`0p +| 272: 00 50 50 00 00 17 20 30 50 70 00 00 17 30 c0 10 .PP... 0Pp...0.. +| 288: 50 00 10 20 00 50 30 00 10 20 00 00 17 40 30 20 P.. .P0.. ...@0 +| 304: 50 00 00 17 50 a0 20 70 00 10 20 a0 00 10 30 00 P...P. p.. ...0. +| 320: 00 17 60 70 30 60 00 20 90 30 08 52 60 10 80 80 ..`p0`. .0.R`... +| 336: 80 80 81 78 a3 e3 02 03 63 63 50 00 26 16 40 30 ...x....ccP.&.@0 +| 352: 30 40 00 20 86 97 06 97 36 36 96 e6 70 30 10 80 0@. ....66..p0.. +| 368: 00 10 56 c6 97 17 56 10 30 20 c0 00 50 26 97 00 ..V...V.0 ..P&.. +| 384: 30 40 40 00 10 36 d6 57 40 30 10 60 00 10 36 e6 0@@..6.W@0.`..6. +| 400: 96 d0 30 80 90 00 10 37 57 46 50 30 50 30 00 00 ..0....7WFP0P0.. +| 416: 66 36 96 c6 c7 56 d0 30 60 20 00 10 66 f6 d6 d6 f6...V.0` ..f... +| 432: f6 46 f0 30 40 70 00 20 96 e7 36 56 37 46 57 47 .F.0@p. ..6V7FWG +| 448: 57 20 30 10 70 00 50 47 17 56 17 40 30 40 80 00 W 0.p.PG.V.@0@.. +| 464: 10 47 56 c7 06 10 30 80 40 00 20 77 06 96 46 17 .GV...0.@. w..F. +| 480: 46 17 40 30 70 50 00 00 86 46 57 36 57 27 56 e7 F.@0pP...FW6W'V. +| 496: 40 30 80 70 00 10 16 f0 30 20 30 00 20 36 c6 f7 @0.p....0 0. 6.. +| 512: 20 60 10 40 00 40 50 00 50 16 50 60 20 a0 00 40 `.@.@P.P.P` ..@ +| 528: 30 00 10 37 56 97 30 30 50 20 00 00 26 56 10 30 0..7V.00P ..&V.0 +| 544: 40 60 00 10 66 97 57 36 d6 f6 40 30 20 40 00 10 @`..f.W6..@0 @.. +| 560: 36 c6 97 40 30 10 90 00 15 35 14 c6 97 46 52 06 6..@0....5...FR. +| 576: 66 f7 26 d6 17 42 03 30 01 00 00 10 10 04 02 02 f.&..B.0........ +| 592: 00 00 00 00 00 00 00 00 70 00 00 00 00 00 00 00 ........p....... +| 608: 00 00 00 00 60 00 00 00 40 00 00 00 00 00 00 00 ....`...@....... +| page 4 offset 12288 +| 0: 0d 00 00 00 03 0a a6 00 0d 57 0c 4a 0a a6 00 00 .........W.J.... +| 2720: 00 00 00 00 00 00 83 21 03 08 02 08 08 08 17 86 .......!........ +| 2736: 30 08 00 30 20 34 30 32 00 02 61 64 06 01 08 00 0..0 402..ad.... +| 2752: 02 04 00 01 01 6c 06 02 0c 00 02 04 00 01 01 6d .....l.........m +| 2768: 03 01 06 00 01 01 6e 03 08 00 00 91 01 75 03 05 ......n......u.. +| 2784: 03 00 00 02 63 69 03 06 02 00 01 01 6f 07 01 07 ....ci......o... +| 2800: 00 03 07 03 00 01 01 75 06 07 05 00 01 04 00 00 .......u........ +| 2816: 02 64 65 03 08 07 00 01 01 6f 0d 01 04 00 01 03 .de......o...... +| 2832: 09 00 03 05 00 01 03 00 01 01 75 03 05 02 00 00 ..........u..... +| 2848: 02 65 61 03 04 06 00 01 01 69 03 02 04 00 01 01 .ea......i...... +| 2864: 6c 03 01 09 00 01 01 6e 30 03 03 00 01 01 73 06 l......n0.....s. +| 2880: 05 0b 00 03 0b 00 01 01 74 03 02 09 00 01 01 75 ........t......u +| 2896: 03 06 04 00 01 01 78 09 03 09 00 01 05 00 03 02 ......x......... +| 2912: 00 00 02 66 75 03 06 05 00 00 02 69 64 03 08 0a ...fu......id... +| 2928: 00 01 01 6e 0a 02 06 00 03 06 04 00 03 03 00 01 ...n............ +| 2944: 01 70 03 01 03 00 01 01 72 03 05 04 00 00 02 6c .p......r......l +| 2960: 61 09 02 08 00 01 0b 00 05 0c 00 01 01 6f 03 01 a............o.. +| 2976: 02 00 00 02 6d 61 03 02 0b 00 01 01 69 03 03 05 ....ma......i... +| 2992: 00 01 01 6f 03 08 08 00 00 02 6e 69 03 04 02 00 ...o......ni.... +| 3008: 01 01 6f 06 03 08 00 04 06 00 01 01 75 03 06 06 ..o.........u... +| 3024: 00 00 02 6f 63 03 07 04 00 01 01 66 03 08 06 00 ...oc......f.... +| 3040: 00 02 70 61 03 06 07 00 01 01 72 03 07 07 00 00 ..pa......r..... +| 3056: 02 71 75 06 03 07 00 05 05 00 00 02 72 65 03 05 .qu.........re.. +| 3072: 07 00 00 02 73 65 03 02 02 00 01 01 69 06 01 05 ....se......i... +| 3088: 00 06 03 00 01 01 75 03 08 02 00 00 02 74 65 03 ......u......te. +| 3104: 02 05 00 00 02 75 6c 03 03 0a 00 01 01 74 09 02 .....ul......t.. +| 3120: 07 00 01 02 00 01 03 00 00 02 76 65 06 03 06 00 ..........ve.... +| 3136: 02 0a 00 01 01 6f 03 05 09 00 82 0a 02 08 02 08 .....o.......... +| 3152: 08 08 17 84 02 04 00 30 20 32 35 31 00 01 61 13 .......0 251..a. +| 3168: 01 06 04 00 01 0c 00 01 04 00 01 04 00 01 03 00 ................ +| 3184: 03 09 00 00 01 63 10 01 07 00 03 07 03 00 02 02 .....c.......... +| 3200: 00 01 05 00 01 04 00 00 01 64 11 01 04 00 01 03 .........d...... +| 3216: 09 00 03 02 05 00 01 03 00 02 07 00 00 01 65 1b ..............e. +| 3232: 01 09 00 01 04 01 70 00 03 01 80 00 05 03 00 01 ......p......... +| 3248: 0b 00 01 04 00 01 02 00 01 0b 00 00 01 66 03 06 .............f.. +| 3264: 05 00 00 01 69 0f 01 03 00 01 06 00 03 04 04 04 ....i........... +| 3280: 00 03 03 09 00 00 01 6c 0c 01 02 00 01 08 00 01 .......l........ +| 3296: 0b 00 05 0c 00 00 01 6d 09 02 0b 00 01 05 00 05 .......m........ +| 3312: 08 00 00 01 6e 0c 03 08 00 01 02 00 02 06 00 01 ....n........... +| 3328: 06 00 00 01 6f 06 07 04 00 01 06 00 00 01 70 06 ....o.........p. +| 3344: 06 07 00 01 07 00 00 01 71 06 03 07 00 05 05 00 ........q....... +| 3360: 00 01 72 03 05 07 00 00 01 73 0c 01 05 00 01 02 ..r......s...... +| 3376: 00 05 03 00 01 02 00 00 01 74 03 02 05 00 00 01 .........t...... +| 3392: 75 0a 02 07 00 01 02 0a 00 01 03 00 00 01 76 07 u.............v. +| 3408: 03 06 00 02 09 03 00 85 26 01 08 08 08 08 08 17 ........&....... +| 3424: 8a 3e 30 20 36 36 35 00 02 61 64 03 03 04 00 02 .>0 665..ad..... +| 3440: 08 69 70 69 73 63 69 6e 67 03 01 08 00 01 05 6c .ipiscing......l +| 3456: 69 71 75 61 03 02 0c 00 05 02 69 70 03 04 04 00 iqua......ip.... +| 3472: 01 03 6d 65 74 03 01 06 00 01 03 6e 69 6d 03 08 ..met......nim.. +| 3488: 09 00 01 03 75 74 65 03 05 03 00 00 06 63 69 6c ....ute......cil +| 3504: 6c 75 6d 03 06 02 00 01 06 6f 6d 6d 6f 64 6f 03 lum......ommodo. +| 3520: 04 07 00 02 09 6e 73 65 63 74 65 74 75 72 03 01 .....nsectetur.. +| 3536: 07 00 05 04 71 75 61 74 03 04 08 00 01 04 75 6c ....quat......ul +| 3552: 70 61 03 08 04 00 02 07 70 69 64 61 74 61 74 03 pa......pidatat. +| 3568: 07 05 00 00 08 64 65 73 65 72 75 6e 74 03 08 07 .....deserunt... +| 3584: 00 01 01 6f 03 02 03 00 02 03 6c 6f 72 06 01 04 ...o......lor... +| 3600: 00 04 05 00 05 01 65 06 02 0a 00 04 03 00 01 03 ......e......... +| 3616: 75 69 73 03 05 02 00 00 02 65 61 03 04 06 00 01 uis......ea..... +| 3632: 06 69 75 73 6d 6f 64 03 02 04 00 01 03 6c 69 74 .iusmod......lit +| 3648: 03 01 09 00 01 03 6e 69 6d 03 03 03 00 01 03 73 ......nim......s +| 3664: 73 65 03 05 0b 00 02 01 74 03 08 0b 00 01 01 74 se......t......t +| 3680: 03 02 09 00 01 01 75 03 06 04 00 01 01 78 03 04 ......u......x.. +| 3696: 05 00 02 07 63 65 70 74 65 75 72 03 07 02 00 02 ....cepteur..... +| 3712: 0a 65 72 63 69 74 61 74 69 6f 6e 03 03 09 00 00 .ercitation..... +| 3728: 06 66 75 67 69 61 74 03 06 05 00 00 02 69 64 03 .fugiat......id. +| 3744: 08 0a 00 01 01 6e 07 05 06 04 00 03 03 00 02 08 .....n.......... +| 3760: 63 69 64 69 64 75 6e 74 03 02 06 00 01 04 70 73 cididunt......ps +| 3776: 75 6d 03 01 03 00 01 04 72 75 72 65 03 05 04 00 um......rure.... +| 3792: 00 06 6c 61 62 6f 72 65 03 02 08 00 05 02 69 73 ..labore......is +| 3808: 03 03 0b 00 05 02 75 6d 03 08 0c 00 01 04 6f 72 ......um......or +| 3824: 65 6d 03 01 02 00 00 05 6d 61 67 6e 61 03 02 0b em......magna... +| 3840: 00 01 04 69 6e 69 6d 03 03 05 00 01 05 6f 6c 6c ...inim......oll +| 3856: 69 74 03 08 08 00 00 04 6e 69 73 69 03 04 02 00 it......nisi.... +| 3872: 01 02 6f 6e 03 07 06 00 02 05 73 74 72 75 64 03 ..on......strud. +| 3888: 03 08 00 01 04 75 6c 6c 61 03 06 06 00 00 08 6f .....ulla......o +| 3904: 63 63 61 65 63 61 74 03 07 04 00 01 06 66 66 69 ccaecat......ffi +| 3920: 63 69 61 03 08 06 00 00 08 70 61 72 69 61 74 75 cia......pariatu +| 3936: 72 03 06 07 00 01 07 72 6f 69 64 65 6e 74 03 07 r......roident.. +| 3952: 07 00 00 03 71 75 69 03 08 05 00 03 01 73 03 03 ....qui......s.. +| 3968: 07 00 00 0d 72 65 70 72 65 68 65 6e 64 65 72 69 ....reprehenderi +| 3984: 74 03 05 07 00 00 03 73 65 64 03 02 02 00 01 03 t......sed...... +| 4000: 69 6e 74 03 07 03 00 02 01 74 03 01 05 00 01 03 int......t...... +| 4016: 75 6e 74 03 08 02 00 00 06 74 65 6d 70 6f 72 03 unt......tempor. +| 4032: 02 05 00 00 07 75 6c 6c 61 6d 63 6f 03 03 0a 00 .....ullamco.... +| 4048: 01 01 74 09 02 07 00 01 02 00 01 03 00 00 05 76 ..t............v +| 4064: 65 6c 69 74 03 05 0a 00 02 04 6e 69 61 6d 03 03 elit......niam.. +| 4080: 06 00 01 08 6f 6c 75 70 74 61 74 65 03 05 09 00 ....oluptate.... +| page 5 offset 16384 +| 0: 0a 00 00 00 03 0f eb 00 0f fb 0f f3 0f eb 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 07 04 02 08 01 ................ +| 4080: 08 00 03 07 04 02 08 01 04 00 02 04 04 08 08 09 ................ +| page 6 offset 20480 +| 0: 0d 00 00 00 08 0f d0 00 0f fa 0f f4 0f ee 0f e8 ................ +| 16: 0f e2 0f dc 0f d6 0f d0 00 00 00 00 00 00 00 00 ................ +| 4048: 04 08 03 00 0e 0b 04 07 03 00 0e 06 04 06 03 00 ................ +| 4064: 0e 06 04 05 03 00 0e 0a 04 04 03 00 0e 07 04 03 ................ +| 4080: 03 00 0e 0a 04 02 03 00 0e 0b 04 01 03 00 0e 08 ................ +| page 7 offset 24576 +| 0: 0d 00 00 00 01 0f f7 00 0f f7 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 07 00 03 00 14 08 45 b5 03 .............E.. +| page 8 offset 28672 +| 0: 6f ee cd e1 f1 ee 1a ca be ed ee ec de ac f1 cb o............... +| 16: f1 ee 1a ce de ee f1 ee 0a cc de ed ae 90 87 88 ................ +| 32: ec 5e dc ec fc 11 72 32 75 0a da be ec ed eb de .^....r2u....... +| 48: ce c1 aa e0 ae ec 1f c1 ee 99 c2 aa e0 a9 ad 15 ................ +| 64: 0e ec ab ef 1e e0 48 ad 15 04 24 80 00 00 00 00 ......H...$..... +| 80: 00 00 00 00 e0 00 00 00 04 2c 80 00 10 42 4e c1 .........,...BN. +| 96: 20 4b 45 59 2c 6e 6f 64 65 6e 6f 2c 61 30 29 46 KEY,nodeno,a0)F +| 112: 02 06 17 11 11 08 7b 74 61 62 6c 65 74 31 74 31 .......tablet1t1 +| 128: 43 52 45 41 54 e1 ec eb ea eb eb ac ee ce be de CREAT........... +| 144: ee f1 ee 1a ca ba de 47 80 30 00 14 90 47 70 30 .......G.0...Gp0 +| 160: 00 11 60 47 60 30 00 15 f0 47 50 30 00 10 f0 47 ..`G`0...GP0...G +| 176: 40 30 00 11 f0 47 45 20 30 00 11 d0 45 10 30 00 @0...GE 0...E.0. +| 192: 12 10 45 00 30 00 14 e0 44 f0 30 00 11 e0 44 e0 ..E.0...D.0...D. +| 208: 30 00 12 a0 44 d0 30 00 15 e0 44 c0 30 00 10 40 0...D.0...D.0..@ +| 224: 44 b0 30 00 15 10 44 a0 30 00 14 c0 44 90 30 00 D.0...D.0...D.0. +| 240: 16 20 44 80 30 00 52 45 41 54 45 20 54 41 42 4c . D.0.REATE TABL +| 256: 45 20 27 74 31 5f 63 6f 6e 74 65 6e 74 27 28 69 E 't1_content'(i +| 272: 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 d INTEGER PRIMAR +| 288: 59 20 4b 45 59 2c 20 63 30 29 69 03 07 17 19 19 Y KEY, c0)i..... +| 304: 01 81 2d 74 61 62 6c 65 74 31 5f 69 64 78 74 31 ..-tablet1_idxt1 +| 320: 5f 69 64 78 03 43 52 45 41 54 45 20 54 41 42 4c _idx.CREATE TABL +| 336: 45 20 27 70 31 5f 69 64 78 03 6e 69 6d 03 03 03 E 'p1_idx.nim... +| 352: 00 01 03 73 73 65 03 05 0b 00 02 01 74 03 08 0b ...sse......t... +| 368: 00 01 01 74 03 02 09 00 01 01 75 03 06 04 00 01 ...t......u..... +| 384: 01 78 03 04 05 00 02 07 63 65 70 74 65 75 72 03 .x......cepteur. +| 400: 07 02 00 02 0a 65 72 63 69 74 61 74 69 6f 6e 03 .....ercitation. +| 416: 03 09 00 00 06 66 75 67 69 61 74 03 06 05 00 00 .....fugiat..... +| 432: 02 69 64 03 08 0a 00 01 01 6e 07 05 06 04 00 03 .id......n...... +| 448: 03 00 02 08 63 69 64 69 64 75 6e 74 03 02 06 00 ....cididunt.... +| 464: 01 04 70 73 75 6d 03 01 03 00 01 04 72 75 72 65 ..psum......rure +| 480: 03 05 04 00 00 06 6c 61 62 6f 72 65 03 02 08 00 ......labore.... +| 496: 05 02 69 73 03 03 0b 00 05 02 75 6d 03 08 0c 00 ..is......um.... +| 512: 01 04 6f 72 65 6d 03 01 02 00 00 05 6d 61 67 6e ..orem......magn +| 528: 61 03 02 0b 00 01 04 69 6e 69 6d 03 03 05 00 01 a......inim..... +| 544: 05 6f 6c 6c 69 74 03 08 08 00 00 04 6e 69 73 69 .ollit......nisi +| 560: 03 04 02 00 01 02 6f 6e 03 07 06 00 02 05 73 74 ......on......st +| 576: 72 75 64 03 03 08 00 01 04 75 6c 6c 61 03 06 06 rud......ulla... +| 592: 00 00 08 6f 63 63 61 65 63 61 74 03 07 04 00 01 ...occaecat..... +| 608: 06 66 66 69 63 69 61 03 08 06 00 00 08 70 61 72 .fficia......par +| 624: 69 61 74 75 72 03 06 07 00 01 07 72 6f ed ce de iatur......ro... +| 640: 69 64 65 6e 74 03 07 07 00 00 03 71 75 69 03 08 ident......qui.. +| 656: 05 00 03 01 73 03 03 07 00 00 0d 72 65 70 72 65 ....s......repre +| 672: 68 65 6e 64 65 72 69 74 03 05 07 00 00 03 73 65 henderit......se +| 688: 64 03 02 02 00 01 03 69 6e 74 03 07 03 00 02 01 d......int...... +| 704: 74 03 01 05 00 01 03 75 6e 74 03 08 02 00 00 06 t......unt...... +| 720: 74 65 6d 70 6f 72 03 02 05 00 00 07 75 6c 6c 61 tempor......ulla +| 736: 6d 63 6f 03 03 0a 00 01 01 74 09 02 07 00 01 02 mco......t...... +| 752: 00 01 03 00 00 05 76 65 6c 69 74 03 05 0a 00 02 ......velit..... +| 768: 04 6e 69 61 6d 03 03 06 00 01 08 6f 6c 75 70 74 .niam......olupt +| 784: 61 74 65 03 05 09 00 0a 00 00 00 03 0f eb 00 0f ate............. +| 800: fb 0f f3 0f eb 00 00 00 00 00 00 00 00 00 00 00 ................ +| end crash-04bb6e7c811ce9.db +}]} {} + +do_catchsql_test 7.1 { + SELECT matchinfo(t1,'y') FROM t1 WHERE t1 MATCH 'e*'; +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +reset_db +do_test 8.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 28672 pagesize 4096 filename crash-7948058d822acb.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 07 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 06 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 96: 00 00 00 00 0d 0e ef 00 07 0d 4d 00 0f bd 0f 5f ..........M...._ +| 112: 0e f7 0e 06 0e bc 0d a4 0d 4d 00 00 00 00 00 00 .........M...... +| 3392: 00 00 00 00 00 00 00 00 00 00 00 00 00 55 07 07 .............U.. +| 3408: 17 1b 1b 01 81 01 74 61 62 6c 65 74 31 5f 73 74 ......tablet1_st +| 3424: 61 74 74 31 5f 73 74 61 74 07 43 52 45 41 54 45 att1_stat.CREATE +| 3440: 20 54 41 42 4c 45 20 27 74 31 5f 73 74 61 74 27 TABLE 't1_stat' +| 3456: 28 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d (id INTEGER PRIM +| 3472: 41 52 59 20 4b 45 59 2c 20 76 61 6c 75 65 20 42 ARY KEY, value B +| 3488: 4c 4f 42 29 60 06 07 17 21 21 01 81 0b 74 61 62 LOB)`...!!...tab +| 3504: 6c 65 74 31 5f 64 6f 63 73 69 7a 65 74 31 5f 64 let1_docsizet1_d +| 3520: 6f 63 73 69 7a 65 06 43 52 45 41 54 45 20 54 41 ocsize.CREATE TA +| 3536: 42 4c 45 20 27 74 31 5f 64 6f 63 73 69 7a 65 27 BLE 't1_docsize' +| 3552: 28 64 6f 63 69 64 20 49 4e 54 45 47 45 52 20 50 (docid INTEGER P +| 3568: 52 49 4d 41 52 59 20 4b 45 59 2c 20 73 69 7a 65 RIMARY KEY, size +| 3584: 20 42 4c 4f 42 29 81 33 04 07 17 1f 1f 01 82 35 BLOB).3.......5 +| 3600: 74 61 62 6c 65 74 31 5f 73 65 67 64 69 72 74 31 tablet1_segdirt1 +| 3616: 5f 73 65 67 64 69 72 04 43 52 45 41 54 45 20 54 _segdir.CREATE T +| 3632: 41 42 4c 45 20 27 74 31 5f 73 65 67 64 69 72 27 ABLE 't1_segdir' +| 3648: 28 6c 65 76 65 6c 20 49 4e 54 45 47 45 52 2c 69 (level INTEGER,i +| 3664: 64 78 20 49 4e 54 45 47 45 52 2c 73 74 61 72 74 dx INTEGER,start +| 3680: 5f 62 6c 6f 63 6b 20 49 4e 54 45 47 45 52 2c 6c _block INTEGER,l +| 3696: 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 6b 20 eaves_end_block +| 3712: 49 4e 54 45 47 45 52 2c 65 6e 64 5f 62 6c 6f 63 INTEGER,end_bloc +| 3728: 6b 20 49 4e 54 45 47 45 52 2c 72 6f 6f 74 20 42 k INTEGER,root B +| 3744: 4c 4f 42 2c 50 52 49 4d 41 52 59 20 4b 45 59 28 LOB,PRIMARY KEY( +| 3760: 6c 65 76 65 6c 2c 20 69 64 78 29 29 31 05 06 17 level, idx))1... +| 3776: 45 1f 01 00 69 6e 64 65 78 73 71 6c 69 74 65 5f E...indexsqlite_ +| 3792: 61 75 74 6f 69 6e 64 65 78 5f 74 31 5f 73 65 67 autoindex_t1_seg +| 3808: 64 69 72 5f 31 74 31 5f 73 65 67 64 69 72 05 00 dir_1t1_segdir.. +| 3824: 00 00 08 00 00 00 00 66 03 07 17 23 23 01 81 13 .......f...##... +| 3840: 74 61 62 6c 65 74 31 5f 73 65 67 6d 65 6e 74 73 tablet1_segments +| 3856: 74 31 5f 73 65 67 6d 65 6e 74 73 03 43 52 45 41 t1_segments.CREA +| 3872: 54 45 20 54 41 42 4c 45 20 27 74 31 5f 73 65 67 TE TABLE 't1_seg +| 3888: 6d 65 6e 74 73 27 28 62 6c 6f 63 6b 69 64 20 49 ments'(blockid I +| 3904: 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b NTEGER PRIMARY K +| 3920: 45 59 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 29 5c EY, block BLOB). +| 3936: 02 07 17 21 21 01 81 03 74 61 62 6c 65 74 31 5f ...!!...tablet1_ +| 3952: 63 6f 6e 74 65 6e 74 74 31 5f 63 6f 6e 74 65 6e contentt1_conten +| 3968: 74 02 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 t.CREATE TABLE ' +| 3984: 74 31 5f 63 6f 6e 74 65 6e 74 27 28 64 6f 63 69 t1_content'(doci +| 4000: 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 d INTEGER PRIMAR +| 4016: 59 20 4b 45 59 2c 20 27 63 30 61 27 29 41 01 06 Y KEY, 'c0a')A.. +| 4032: 17 11 11 08 71 74 61 62 6c 65 74 31 74 31 43 52 ....qtablet1t1CR +| 4048: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4064: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 34 LE t1 USING fts4 +| 4080: 28 61 2c 70 72 65 66 69 78 3d 27 31 2c 32 27 29 (a,prefix='1,2') +| page 2 offset 4096 +| 0: 0d 00 00 00 08 0e 1f 00 0f c4 0f 7c 0f 34 0f 07 ...........|.4.. +| 16: 0e c3 0e 97 0e 63 0e 1f 00 00 00 00 00 00 00 00 .....c.......... +| 3600: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 42 ...............B +| 3616: 08 04 00 81 09 73 75 6e 74 20 69 6e 20 63 75 6c .....sunt in cul +| 3632: 70 61 20 71 75 69 20 6f 66 66 69 63 69 61 20 64 pa qui officia d +| 3648: 65 73 65 72 75 6e 74 20 6d 6f 6c 6c 69 74 20 61 eserunt mollit a +| 3664: 6e 69 6d 20 69 64 20 65 73 74 20 6c 61 62 6f 72 nim id est labor +| 3680: 75 6d 2e 32 07 03 00 6b 45 78 63 65 70 74 65 75 um.2...kExcepteu +| 3696: 72 20 73 69 6e 74 20 6f 63 63 61 65 63 67 42 06 r sint occaecgB. +| 3712: 37 57 06 96 46 17 46 17 42 06 e6 f6 e2 07 07 26 7W..F.F.B......& +| 3728: f6 96 46 56 e7 42 c2 a0 60 30 05 b6 36 96 c6 c7 ..FV.B..`0..6... +| 3744: 56 d2 06 46 f6 c6 f7 26 52 06 57 52 06 67 56 7c V..F...&R.WR.gV| +| 3760: 65 3f 04 20 6e 75 6c 6c 61 20 70 61 72 69 61 74 e?. nulla pariat +| 3776: 75 72 2e 42 05 04 00 81 09 44 75 69 73 20 61 75 ur.B.....Duis au +| 3792: 74 65 20 69 72 75 72 65 20 64 6f 6c 6f 72 20 69 te irure dolor i +| 3808: 6e 20 72 65 70 72 65 68 65 6e 64 65 72 69 74 20 n reprehenderit +| 3824: 69 6e 20 76 6f 6c 75 70 74 61 74 65 20 76 65 6c in voluptate vel +| 3840: 69 74 20 65 73 73 65 2b 04 03 00 5d 6e 69 73 69 it esse+...]nisi +| 3856: 20 75 74 20 61 7c 69 71 75 69 70 20 65 78 20 65 ut a|iquip ex e +| 3872: 61 20 63 6f 6d 6d 6f 64 6f 20 63 6f 6e 73 65 71 a commodo conseq +| 3888: 75 61 74 2e 46 03 04 00 81 11 55 74 20 65 6e 69 uat.F.....Ut eni +| 3904: 6d 20 61 64 20 6d 69 6e 69 6d 20 76 65 6e 69 61 m ad minim venia +| 3920: 6d 2c 20 71 75 69 73 20 6e 6f 73 74 72 75 64 20 m, quis nostrud +| 3936: 65 78 65 72 63 69 74 61 74 69 6f 6e 20 75 6c 6c exercitation ull +| 3952: 61 6d 63 6f 20 6c 61 62 6f 72 69 73 46 02 04 00 amco laborisF... +| 3968: 81 11 73 65 64 20 64 6f 20 65 69 75 73 6d 6f 64 ..sed do eiusmod +| 3984: 20 74 65 6d 70 6f 72 20 69 6e 63 69 64 69 64 75 tempor incididu +| 4000: 6e 74 20 75 74 20 6c 61 62 6f 72 65 20 65 74 20 nt ut labore et +| 4016: 64 6f 6c 6f 72 65 20 6d 61 67 6e 61 20 61 6c 69 dolore magna ali +| 4032: 71 75 61 2e 3a 01 03 00 7b 4c 6f 72 65 6d 20 69 qua.:....Lorem i +| 4048: 70 73 75 6d 20 64 6f 6c 6f 72 20 73 69 74 20 61 psum dolor sit a +| 4064: 6d 65 74 2c 20 63 6f 6e 73 65 63 74 65 74 75 72 met, consectetur +| 4080: 20 61 64 69 70 69 73 63 69 6e 67 20 65 6c 69 74 adipiscing elit +| page 3 offset 8192 +| 0: 0d 00 00 00 00 10 00 00 00 00 00 00 00 00 00 00 ................ +| page 4 offset 12288 +| 0: 0d 00 00 00 03 0a a6 00 0d 57 0c 4a 0a a6 00 00 .........W.J.... +| 2720: 00 00 00 00 00 00 83 21 03 08 02 08 08 08 17 86 .......!........ +| 2736: 30 08 00 30 20 34 30 32 00 02 61 64 06 01 08 00 0..0 402..ad.... +| 2752: 02 04 00 01 01 6c 06 02 0c 00 02 04 00 01 01 6d .....l.........m +| 2768: 03 01 06 00 01 01 6e 03 08 00 00 91 01 75 03 05 ......n......u.. +| 2784: 03 00 00 02 63 69 03 06 02 00 01 01 6f 07 01 07 ....ci......o... +| 2800: 00 03 07 03 00 01 01 75 06 07 05 00 01 04 00 00 .......u........ +| 2816: 02 64 65 03 08 07 00 01 01 6f 0d 01 04 00 01 03 .de......o...... +| 2832: 09 00 03 05 00 01 03 00 01 01 75 03 05 02 00 00 ..........u..... +| 2848: 02 65 61 03 04 06 00 01 01 69 03 02 04 00 01 01 .ea......i...... +| 2864: 6c 03 01 09 00 01 01 6e 30 03 03 00 01 01 73 06 l......n0.....s. +| 2880: 05 0b 00 03 0b 00 01 01 74 03 02 09 00 01 01 75 ........t......u +| 2896: 03 06 04 00 01 01 78 09 03 09 00 01 05 00 03 02 ......x......... +| 2912: 00 00 02 66 75 03 06 05 00 00 02 69 64 03 08 0a ...fu......id... +| 2928: 00 01 01 6e 0a 02 06 00 03 06 04 00 03 03 00 01 ...n............ +| 2944: 01 70 03 01 03 00 01 01 72 03 05 04 00 00 02 6c .p......r......l +| 2960: 61 09 02 08 00 01 0b 00 05 0c 00 01 01 6f 03 01 a............o.. +| 2976: 02 00 00 02 6d 61 03 02 0b 00 01 01 69 03 03 05 ....ma......i... +| 2992: 00 01 01 6f 03 08 08 00 00 02 6e 69 03 04 02 00 ...o......ni.... +| 3008: 01 01 6f 06 03 08 00 04 06 00 01 01 75 03 06 06 ..o.........u... +| 3024: 00 00 02 6f 63 03 07 04 00 01 01 66 03 08 06 00 ...oc......f.... +| 3040: 00 02 70 61 03 06 07 00 01 01 72 03 07 07 00 00 ..pa......r..... +| 3056: 02 71 75 06 03 07 00 05 05 00 00 02 72 65 03 05 .qu.........re.. +| 3072: 07 00 00 02 73 65 03 02 02 00 01 01 69 06 01 05 ....se......i... +| 3088: 00 06 03 00 01 01 75 03 08 02 00 00 02 74 65 03 ......u......te. +| 3104: 02 05 00 00 02 75 6c 03 03 0a 00 01 01 74 09 02 .....ul......t.. +| 3120: 07 00 01 02 00 01 03 00 00 02 76 65 06 03 06 00 ..........ve.... +| 3136: 02 0a 00 01 01 6f 03 05 09 00 82 0a 02 08 02 08 .....o.......... +| 3152: 08 08 17 84 02 04 00 30 20 32 35 31 00 01 61 13 .......0 251..a. +| 3168: 01 06 04 00 01 0c 00 01 04 00 01 04 00 01 03 00 ................ +| 3184: 03 09 00 00 01 63 10 01 07 00 03 07 03 00 02 02 .....c.......... +| 3200: 00 01 05 00 01 04 00 00 01 64 11 01 04 00 01 03 .........d...... +| 3216: 09 00 03 02 05 00 01 03 00 02 07 00 00 01 65 1b ..............e. +| 3232: 01 09 00 01 04 07 00 01 03 00 80 00 15 03 00 01 ................ +| 3248: 0b 00 01 04 00 01 02 00 01 0b 00 00 01 66 03 06 .............f.. +| 3264: 05 00 00 01 69 0f 01 03 00 01 06 00 03 04 04 04 ....i........... +| 3280: 00 03 03 09 00 00 01 6c 0c 01 02 00 01 08 00 01 .......l........ +| 3296: 0b 00 05 0c 00 00 01 6d 09 02 0b 00 01 05 00 05 .......m........ +| 3312: 08 00 00 01 6e 0c 03 08 00 01 02 00 02 06 00 01 ....n........... +| 3328: 06 00 00 01 6f 06 07 04 00 01 06 00 00 01 70 06 ....o.........p. +| 3344: 06 07 00 01 07 00 00 01 71 06 03 07 00 05 05 00 ........q....... +| 3360: 00 01 72 03 05 07 00 00 01 73 0c 01 05 00 01 02 ..r......s...... +| 3376: 00 05 03 00 01 02 00 00 01 74 03 02 05 00 00 01 .........t...... +| 3392: 75 0a 02 07 00 01 02 0a 00 01 03 00 00 01 76 07 u.............v. +| 3408: 03 06 00 02 09 03 00 85 26 01 08 08 08 08 08 17 ........&....... +| 3424: 8a 3e 30 20 36 36 35 00 02 61 64 03 03 04 00 02 .>0 665..ad..... +| 3440: 08 69 70 69 73 63 69 6e 67 03 01 08 00 01 05 6c .ipiscing......l +| 3456: 69 71 75 61 03 02 0c 00 05 02 69 70 03 04 04 00 iqua......ip.... +| 3472: 01 03 6d 65 74 03 01 06 00 01 03 6e 69 6d 03 08 ..met......nim.. +| 3488: 09 00 01 03 75 74 65 03 05 03 00 00 06 63 69 6c ....ute......cil +| 3504: 6c 75 6d 03 06 02 00 01 06 6f 6d 6d 6f 64 6f 03 lum......ommodo. +| 3520: 04 07 00 02 09 6e 73 65 63 74 65 74 75 72 03 01 .....nsectetur.. +| 3536: 07 00 05 04 71 75 61 74 03 04 08 00 01 04 75 6c ....quat......ul +| 3552: 70 61 03 08 04 00 02 07 70 69 64 61 74 61 74 03 pa......pidatat. +| 3568: 07 05 00 00 08 64 65 73 65 72 75 6e 74 03 08 07 .....deserunt... +| 3584: 00 01 01 6f 03 02 03 00 02 03 6c 6f 72 06 01 04 ...o......lor... +| 3600: 00 40 05 00 05 01 65 06 02 0a 00 04 03 00 01 03 .@....e......... +| 3616: 75 69 73 03 05 02 00 00 02 65 61 03 04 06 00 01 uis......ea..... +| 3632: 06 69 75 73 6d 6f 64 03 02 04 00 01 03 6c 69 74 .iusmod......lit +| 3648: 03 01 09 00 01 03 6e 69 6d 03 03 03 00 01 03 73 ......nim......s +| 3664: 73 65 03 05 0b 00 02 01 74 03 08 0b 00 01 01 74 se......t......t +| 3680: 03 02 09 00 01 01 75 03 06 04 00 01 01 78 03 04 ......u......x.. +| 3696: 05 00 02 07 63 65 70 74 65 75 72 03 07 02 00 02 ....cepteur..... +| 3712: 0a 65 72 63 69 74 61 74 69 6f 6e 03 03 09 00 00 .ercitation..... +| 3728: 06 66 75 67 69 61 74 03 06 05 00 00 02 69 64 03 .fugiat......id. +| 3744: 08 0a 00 01 01 6e 07 05 06 04 00 03 03 00 02 08 .....n.......... +| 3760: 63 69 64 69 64 75 6e 74 03 02 06 00 01 04 70 73 cididunt......ps +| 3776: 75 6d 03 01 03 00 01 04 72 75 72 65 03 05 04 00 um......rure.... +| 3792: 00 06 6c 61 62 6f 72 65 03 02 08 00 05 02 69 73 ..labore......is +| 3808: 03 03 0b 00 05 02 75 6d 03 08 0c 00 01 04 6f 72 ......um......or +| 3824: 65 6d 03 01 02 00 00 05 6d 61 67 6e 61 03 02 0b em......magna... +| 3840: 00 01 04 69 6e 69 6d 03 03 05 00 01 05 6f 6c 6c ...inim......oll +| 3856: 69 74 03 08 08 00 00 04 6e 69 73 69 03 04 02 00 it......nisi.... +| 3872: 01 02 6f 6e 03 07 06 00 02 05 73 74 72 75 64 03 ..on......strud. +| 3888: 03 08 00 01 04 75 6c 6c 61 03 06 06 00 00 08 6f .....ulla......o +| 3904: 63 63 61 65 63 61 74 03 07 04 00 01 06 66 66 69 ccaecat......ffi +| 3920: 63 69 61 03 08 06 00 00 08 70 61 72 69 61 74 75 cia......pariatu +| 3936: 72 03 06 07 00 01 07 72 6f 69 64 65 6e 74 03 07 r......roident.. +| 3952: 07 00 00 03 71 75 69 03 08 05 00 03 01 73 03 03 ....qui......s.. +| 3968: 07 00 00 0d 72 65 70 72 65 68 65 6e 64 65 72 69 ....reprehenderi +| 3984: 74 03 05 07 00 00 03 73 65 64 03 02 02 00 01 03 t......sed...... +| 4000: 69 6e 74 03 07 03 00 02 01 74 03 01 05 00 01 03 int......t...... +| 4016: 75 6e 74 03 08 02 00 00 06 74 65 6d 70 6f 72 03 unt......tempor. +| 4032: 02 05 00 00 07 75 6c 6c 61 6d 63 6f 03 03 0a 00 .....ullamco.... +| 4048: 01 01 74 09 02 07 00 01 02 00 01 03 00 00 05 76 ..t............v +| 4064: 65 6c 69 74 03 05 0a 00 02 04 6e 69 61 6d 03 03 elit......niam.. +| 4080: 06 00 01 08 6f 6c 75 70 74 61 74 65 03 05 09 00 ....oluptate.... +| page 5 offset 16384 +| 0: 0a 00 00 00 03 0f eb 00 0f fb 0f f3 0f eb 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 07 04 02 08 01 ................ +| 4080: 08 00 03 07 04 02 08 01 04 00 02 04 04 08 08 09 ................ +| page 6 offset 20480 +| 0: 0d 00 00 00 08 0f d0 00 0f fa 0f f4 0f ee 0f e8 ................ +| 16: 0f e2 0f dc 0f d6 0f d0 00 00 00 00 00 00 00 00 ................ +| 4048: 04 08 03 00 0e 0b 04 07 03 00 0e 06 04 06 03 00 ................ +| 4064: 0e 06 04 05 03 00 0e 0a 04 04 03 00 0e 07 04 03 ................ +| 4080: 03 00 0e 0a 04 02 03 00 0e 0b 04 01 03 00 0e 08 ................ +| page 7 offset 24576 +| 0: 0d 00 00 00 01 0f f7 00 0f f7 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 07 00 03 00 14 08 45 b5 03 .............E.. +| end crash-7948058d822acb.db +}]} {} + +do_catchsql_test 8.1 { + SELECT matchinfo(t1,'x') FROM t1 WHERE t1 MATCH 'e*'; +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +reset_db +do_test 9.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 28672 pagesize 4096 filename crash-e1c6cbfdf643e9.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 07 .....@ ........ +| 32: 00 00 00 02 00 00 00 01 00 00 00 07 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 96: 00 00 00 00 0d 0e b1 00 06 0d a4 00 0f 8d 0f 21 ...............! +| 112: 0e b9 0d c8 0e 7e 0d a4 0d a4 00 00 00 00 00 00 .....~.......... +| 3488: 00 00 00 00 22 07 06 17 11 11 01 31 74 61 62 6c ...........1tabl +| 3504: 65 74 32 74 32 07 43 52 45 41 54 45 20 54 41 42 et2t2.CREATE TAB +| 3520: 4c 45 20 74 32 28 78 29 81 33 05 07 17 1f 1f 01 LE t2(x).3...... +| 3536: 82 35 74 61 62 6c 65 74 31 5f 73 65 67 64 69 72 .5tablet1_segdir +| 3552: 74 31 5f 73 65 67 64 69 72 05 43 52 45 41 54 45 t1_segdir.CREATE +| 3568: 20 54 41 42 4c 45 20 27 74 31 5f 73 65 67 64 69 TABLE 't1_segdi +| 3584: 72 27 28 6c 65 76 65 6c 20 49 4e 54 45 47 45 52 r'(level INTEGER +| 3600: 2c 69 64 78 20 49 4e 54 45 47 45 52 2c 73 74 61 ,idx INTEGER,sta +| 3616: 72 74 5f 62 6c 6f 63 6b 20 49 4e 54 45 47 45 52 rt_block INTEGER +| 3632: 2c 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 ,leaves_end_bloc +| 3648: 6b 20 49 4e 54 45 47 45 52 2c 65 6e 64 5f 62 6c k INTEGER,end_bl +| 3664: 6f 63 6b 20 49 4e 54 45 47 45 52 2c 72 6f 6f 74 ock INTEGER,root +| 3680: 20 42 4c 4f 42 2c 50 52 49 4d 41 52 59 20 4b 45 BLOB,PRIMARY KE +| 3696: 59 28 6c 65 76 65 6c 2c 20 69 64 78 29 29 31 06 Y(level, idx))1. +| 3712: 06 17 45 1f 01 00 69 6e 64 65 78 73 71 6c 69 74 ..E...indexsqlit +| 3728: 65 5f 61 75 74 6f 69 6e 64 65 78 5f 74 31 5f 73 e_autoindex_t1_s +| 3744: 65 67 64 69 72 5f 31 74 31 5f 73 65 67 64 69 72 egdir_1t1_segdir +| 3760: 06 0f c7 00 08 00 00 00 00 66 04 07 17 23 23 01 .........f...##. +| 3776: 81 13 74 61 62 6c 65 74 31 5f 73 65 67 6d 65 6e ..tablet1_segmen +| 3792: 74 73 74 31 5f 73 65 67 6d 65 6e 74 73 04 43 52 tst1_segments.CR +| 3808: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 73 EATE TABLE 't1_s +| 3824: 65 67 6d 65 6e 74 73 27 28 62 6c 6f 63 6b 69 64 egments'(blockid +| 3840: 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 INTEGER PRIMARY +| 3856: 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 KEY, block BLOB +| 3872: 29 6a 03 07 17 21 21 01 81 1f 74 61 62 6c 65 74 )j...!!...tablet +| 3888: 31 5f 63 6f 6e 74 65 6e 74 74 31 5f 63 6f 6e 74 1_contentt1_cont +| 3904: 65 6e 74 03 43 52 45 41 54 45 20 54 41 42 4c 45 ent.CREATE TABLE +| 3920: 20 27 74 31 5f 63 6f 6e 74 65 6e 74 27 28 64 6f 't1_content'(do +| 3936: 63 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d cid INTEGER PRIM +| 3952: 41 52 59 20 4b 45 59 2c 20 27 63 30 61 27 2c 20 ARY KEY, 'c0a', +| 3968: 27 63 31 62 27 2c 20 27 63 32 63 27 29 38 02 06 'c1b', 'c2c')8.. +| 3984: 17 11 11 08 5f 74 61 62 6c 65 74 31 74 31 43 52 ...._tablet1t1CR +| 4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 33 LE t1 USING fts3 +| 4032: 28 61 2c 62 2c 63 29 00 00 00 39 00 00 00 00 00 (a,b,c)...9..... +| page 3 offset 8192 +| 0: 0d 00 00 00 25 0b 48 00 0f d8 0f af 0f 86 0f 74 ....%.H........t +| 16: 0f 61 0f 4e 0f 2f 0f 0f 0e ef 0e d7 0e be 0e a5 .a.N./.......... +| 32: 0e 8d 0e 74 0e 5b 0e 40 0e 24 0e 08 0d ef 0d d5 ...t.[.@.$...... +| 48: 0d bb 0d a0 0d 84 0d 68 0d 4f 0d 35 0d 1b 0c fb .......h.O.5.... +| 64: 0c da 0c b9 0c 99 0c 78 0c 57 0c 3e 0c 24 0c 0a .......x.W.>.$.. +| 80: 0b 48 00 00 00 00 00 00 00 00 00 00 00 00 00 00 .H.............. +| 2880: 00 00 00 00 00 00 00 00 81 3f 25 06 00 82 7f 00 .........?%..... +| 2896: 00 43 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2e .COMPILER=gcc-5. +| 2912: 34 2e 30 20 32 30 31 36 30 36 30 39 20 44 45 42 4.0 20160609 DEB +| 2928: 55 47 20 45 4e 41 42 4c 45 20 44 42 53 54 41 54 UG ENABLE DBSTAT +| 2944: 20 56 54 41 42 20 45 4e 41 42 4c 45 20 46 54 53 VTAB ENABLE FTS +| 2960: 34 20 45 4e 41 42 4c 45 20 46 54 53 35 20 45 4e 4 ENABLE FTS5 EN +| 2976: 41 42 4c 45 20 47 45 4f 50 4f 4c 59 20 45 4e 41 ABLE GEOPOLY ENA +| 2992: 42 4c 45 20 4a 53 4f 4e 31 20 45 4e 41 42 4c 45 BLE JSON1 ENABLE +| 3008: 20 4d 45 4d 53 59 53 35 20 45 4e 41 42 4c 45 20 MEMSYS5 ENABLE +| 3024: 52 54 52 45 45 20 4d 41 58 20 4d 45 4d 4f 52 59 RTREE MAX MEMORY +| 3040: 3d 35 30 30 30 30 30 30 30 20 4f 4d 49 54 20 4c =50000000 OMIT L +| 3056: 4f 41 44 20 45 58 54 45 4e 53 49 4f 4e 20 54 48 OAD EXTENSION TH +| 3072: 52 45 41 44 53 41 46 45 3d 30 18 24 05 00 25 0f READSAFE=0.$..%. +| 3088: 19 54 48 52 45 41 44 53 41 46 45 3d 30 58 42 49 .THREADSAFE=0XBI +| 3104: 4e 41 52 59 18 23 05 00 25 0f 19 54 48 52 45 41 NARY.#..%..THREA +| 3120: 44 53 41 46 45 3d 30 58 4e 4f 43 41 53 45 17 22 DSAFE=0XNOCASE.. +| 3136: 05 00 25 0f 17 54 48 52 45 41 44 53 41 46 45 3d ..%..THREADSAFE= +| 3152: 30 58 52 54 52 49 4d 1f 21 05 00 33 0f 19 4f 4d 0XRTRIM.!..3..OM +| 3168: 49 54 20 4c 4f 41 44 20 35 58 54 45 4e 53 49 4f IT LOAD 5XTENSIO +| 3184: 4e 58 42 49 4e 41 52 59 1f 20 05 00 33 0f 19 4f NXBINARY. ..3..O +| 3200: 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 MIT LOAD EXTENSI +| 3216: 4f 4e 58 4e 4f 43 41 53 45 1e 1f 05 00 33 0f 17 ONXNOCASE....3.. +| 3232: 4f 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 OMIT LOAD EXTENS +| 3248: 49 4f 4e 58 52 54 52 49 4d 1f 1e 05 00 33 0f 19 IONXRTRIM....3.. +| 3264: 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 30 MAX MEMORY=50000 +| 3280: 30 30 30 58 42 49 4e 41 52 59 1f 1d 05 00 33 0f 000XBINARY....3. +| 3296: 19 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 .MAX MEMORY=5000 +| 3312: 30 30 30 30 58 4e 4f 43 41 53 45 1e 1c 05 00 33 0000XNOCASE....3 +| 3328: 0f 17 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 ..MAX MEMORY=500 +| 3344: 30 30 30 30 30 58 52 54 52 49 4d 18 1b 05 00 25 00000XRTRIM....% +| 3360: 0f 19 45 4e 41 42 4c 45 20 52 54 52 45 45 58 42 ..ENABLE RTREEXB +| 3376: 49 4e 41 52 59 18 1a 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3392: 4c 45 20 52 54 52 45 45 58 4e 4f 43 41 53 45 17 LE RTREEXNOCASE. +| 3408: 19 05 00 25 0f 17 45 4e 41 42 4c 45 20 52 54 52 ...%..ENABLE RTR +| 3424: 45 45 58 52 54 52 49 4d 1a 18 05 00 29 0f 19 45 EEXRTRIM....)..E +| 3440: 4e 41 42 4c 45 20 4d 45 4d 53 59 53 35 58 42 49 NABLE MEMSYS5XBI +| 3456: 4e 41 52 59 1a 17 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3472: 45 20 4d 45 4d 53 59 53 35 58 4e 4f 43 41 53 45 E MEMSYS5XNOCASE +| 3488: 19 16 05 00 29 0f 17 45 4e 41 42 4c 45 20 4d 45 ....)..ENABLE ME +| 3504: 4d 53 59 53 35 58 52 54 52 49 4d 18 15 05 00 25 MSYS5XRTRIM....% +| 3520: 0f 19 45 4e 41 42 4c 45 20 4a 53 4f 4e 31 58 42 ..ENABLE JSON1XB +| 3536: 49 4e 41 52 59 18 14 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3552: 4c 45 20 4a 53 4f 4e 31 58 4e 4f 43 41 53 45 17 LE JSON1XNOCASE. +| 3568: 13 05 00 25 1f 17 45 4e 41 42 4c 45 20 4a 53 4f ...%..ENABLE JSO +| 3584: 4e 31 58 52 54 52 49 4d 1a 12 05 00 29 0f 19 45 N1XRTRIM....)..E +| 3600: 4e 41 42 4c 45 20 47 45 4f 50 4f 4c 59 58 42 49 NABLE GEOPOLYXBI +| 3616: 4e 41 52 59 1a 11 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3632: 45 20 47 45 4f 50 4f 4c 59 58 4e 4f 43 41 53 45 E GEOPOLYXNOCASE +| 3648: 19 10 05 00 29 0f 17 45 4e 41 42 4c 45 20 47 45 ....)..ENABLE GE +| 3664: 4f 50 4f 4c 59 58 52 54 52 49 4d 17 0f 05 00 23 OPOLYXRTRIM....# +| 3680: 0f 19 45 4e 41 42 4c 45 20 46 54 53 35 58 42 49 ..ENABLE FTS5XBI +| 3696: 4e 41 52 59 17 0e 05 00 23 0f 19 45 4e 41 42 4c NARY....#..ENABL +| 3712: 45 20 46 54 53 35 58 4e 4f 43 41 53 45 16 0d 05 E FTS5XNOCASE... +| 3728: 00 23 0f 17 44 4e 41 42 4c 45 20 46 54 53 35 58 .#..DNABLE FTS5X +| 3744: 52 54 52 49 4d 17 0c 05 00 23 0f 19 45 4e 41 42 RTRIM....#..ENAB +| 3760: 4c 45 20 46 54 53 34 58 42 49 4e 41 52 59 17 0b LE FTS4XBINARY.. +| 3776: 05 00 23 0f 19 45 4e 41 42 4c 45 20 46 54 53 34 ..#..ENABLE FTS4 +| 3792: 58 4e 4f 43 41 53 45 16 0a 05 00 23 0f 17 45 4e XNOCASE....#..EN +| 3808: 41 42 4c 45 20 46 54 53 34 58 52 54 52 49 4d 1e ABLE FTS4XRTRIM. +| 3824: 09 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3840: 54 41 54 20 56 54 41 42 58 42 49 4e 41 52 59 1e TAT VTABXBINARY. +| 3856: 08 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3872: 54 41 54 20 56 54 41 42 58 4e 4f 43 41 53 45 1d TAT VTABXNOCASE. +| 3888: 07 05 00 31 0f 17 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3904: 54 41 54 20 56 54 41 42 58 52 54 52 49 4d 11 06 TAT VTABXRTRIM.. +| 3920: 05 00 17 0f 19 44 45 42 55 47 58 42 49 4e 41 52 .....DEBUGXBINAR +| 3936: 59 11 05 05 00 17 0f 19 44 45 42 55 47 58 4e 4f Y.......DEBUGXNO +| 3952: 43 41 53 45 10 04 05 00 17 0f 17 44 45 42 55 47 CASE.......DEBUG +| 3968: 58 52 54 52 49 4d 27 03 05 00 43 0f 19 43 4f 4d XRTRIM'...C..COM +| 3984: 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e 30 20 PILER=gcc-5.4.0 +| 4000: 32 30 31 36 30 36 30 39 58 42 49 4e 41 52 59 27 20160609XBINARY' +| 4016: 02 05 00 43 0f 19 43 4f 4d 50 49 4c 45 52 3d 67 ...C..COMPILER=g +| 4032: 63 63 2d 35 2e 34 2e 30 20 32 30 31 36 30 36 30 cc-5.4.0 2016060 +| 4048: 39 58 4e 4f 43 41 53 45 26 01 05 00 43 0f 17 43 9XNOCASE&...C..C +| 4064: 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e OMPILER=gcc-5.4. +| 4080: 30 20 32 30 31 36 30 36 30 39 58 52 54 52 49 4d 0 20160609XRTRIM +| page 4 offset 12288 +| 0: 0d 00 00 00 00 10 00 00 00 00 00 00 00 00 00 00 ................ +| page 5 offset 16384 +| 0: 0d 00 00 00 02 0b a0 00 0c ad 0b a0 00 00 00 00 ................ +| 2976: 82 0a 02 08 08 09 08 08 17 84 06 30 20 32 35 33 ...........0 253 +| 2992: 00 01 30 04 25 06 1b 00 00 08 32 30 31 36 30 36 ..0.%.....201606 +| 3008: 30 39 03 25 07 00 00 01 34 03 25 05 00 00 01 35 09.%....4.%....5 +| 3024: 03 25 04 00 01 07 30 30 30 30 30 30 30 03 25 1a .%....0000000.%. +| 3040: 00 00 08 63 6f 6d 70 69 6c 65 72 03 25 02 00 00 ...compiler.%... +| 3056: 06 64 62 73 74 61 74 03 25 0a 00 01 04 65 62 75 .dbstat.%....ebu +| 3072: 67 03 25 08 00 00 06 65 6e 61 62 6c 65 09 25 09 g.%....enable.%. +| 3088: 05 04 04 04 04 04 00 01 08 78 74 65 6e 73 69 6f .........xtensio +| 3104: 6e 03 25 1d 00 00 04 66 74 73 34 03 25 0d 00 03 n.%....fts4.%... +| 3120: 01 35 03 25 0f 00 00 03 67 63 63 03 25 03 00 01 .5.%....gcc.%... +| 3136: 06 65 6f 70 6f 6c 79 03 25 11 00 00 05 6a 73 6f .eopoly.%....jso +| 3152: 6e 31 03 25 13 00 00 04 6c 6f 61 64 03 25 1c 00 n1.%....load.%.. +| 3168: 00 03 6d 61 78 03 25 18 00 01 05 65 6d 6f 72 79 ..max.%....emory +| 3184: 03 25 19 00 03 04 73 79 73 35 03 25 15 00 00 04 .%....sys5.%.... +| 3200: 6f 6d 69 74 03 25 1b 00 00 05 72 74 72 65 65 03 omit.%....rtree. +| 3216: 25 17 00 00 0a 74 68 72 65 61 64 73 61 66 65 03 %....threadsafe. +| 3232: 25 1e 00 00 04 76 74 61 62 03 25 0b 00 86 50 01 %....vtab.%...P. +| 3248: 08 08 08 08 08 17 8d 12 30 20 38 33 35 00 01 30 ........0 835..0 +| 3264: 12 01 06 00 01 06 00 01 06 00 1f 03 00 01 03 00 ................ +| 3280: 01 03 00 00 08 32 30 31 36 30 36 30 39 09 01 07 .....20160609... +| 3296: 00 01 07 00 01 07 00 00 01 34 09 01 05 00 01 05 .........4...... +| 3312: 00 01 05 00 00 01 35 09 01 04 00 01 04 00 01 04 ......5......... +| 3328: 00 01 07 30 30 30 30 30 30 30 09 1c 04 00 01 04 ...0000000...... +| 3344: 00 01 04 00 00 06 62 69 6e 61 72 79 3c 03 01 02 ......binary<... +| 3360: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3376: 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 ................ +| 3392: 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03 ................ +| 3408: 01 02 02 00 03 01 02 02 00 00 08 63 6f 6d 70 69 ...........compi +| 3424: 6c 65 72 09 01 02 00 01 02 00 01 02 00 00 06 64 ler............d +| 3440: 62 73 74 61 74 09 07 03 00 01 03 00 01 03 00 01 bstat........... +| 3456: 04 65 62 75 67 09 04 02 00 01 02 00 01 02 00 00 .ebug........... +| 3472: 06 65 6e 61 62 6c 65 3f 07 02 00 01 02 00 01 02 .enable?........ +| 3488: 00 01 02 00 01 02 00 01 02 00 01 02 00 01 02 00 ................ +| 3504: 01 02 00 01 02 00 01 02 00 01 02 00 01 01 00 01 ................ +| 3520: 02 00 01 02 00 01 02 00 01 02 00 01 02 00 01 02 ................ +| 3536: 00 01 02 00 01 02 00 01 08 78 74 65 6e 73 69 6f .........xtensio +| 3552: 6e 09 1f 04 00 01 04 00 01 04 00 00 04 66 74 73 n............fts +| 3568: 34 09 0a 03 00 01 03 00 01 03 00 03 01 35 09 0d 4............5.. +| 3584: 03 00 01 03 00 01 03 00 00 03 67 63 63 09 01 03 ..........gcc... +| 3600: 00 01 03 00 01 03 00 01 06 65 6f 70 6f 6c 79 09 .........eopoly. +| 3616: 10 03 00 01 03 00 01 03 00 00 05 6a 73 6f 6e 31 ...........json1 +| 3632: 09 13 03 00 01 03 00 01 03 00 00 04 6c 6f 61 64 ............load +| 3648: 09 1f 03 00 01 03 00 01 03 00 00 03 6d 61 78 09 ............max. +| 3664: 1c 02 00 01 02 00 01 02 00 01 05 65 6d 6f 72 79 ...........emory +| 3680: 09 1c 03 00 01 03 00 01 03 00 03 04 73 79 73 35 ............sys5 +| 3696: 09 16 03 00 01 03 00 01 03 00 00 06 6e 6f 63 61 ............noca +| 3712: 73 65 3c 02 01 02 02 00 03 01 02 02 00 03 01 02 se<............. +| 3728: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3744: 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 ................ +| 3760: 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 00 ................ +| 3776: 04 6f 6d 69 74 09 1f 02 00 01 02 00 01 02 00 00 .omit........... +| 3792: 05 72 74 72 65 65 09 19 03 00 01 03 00 01 03 00 .rtree.......... +| 3808: 03 02 69 6d 3c 01 01 02 02 00 03 01 02 02 00 03 ..im<........... +| 3824: 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01 ................ +| 3840: 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 ................ +| 3856: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3872: 00 00 0a 74 68 72 65 61 64 73 61 66 65 09 22 02 ...threadsafe... +| 3888: 00 01 02 00 0a 12 00 00 04 76 74 61 62 09 07 04 .........vtab... +| 3904: 00 01 04 00 01 04 00 00 01 78 b4 01 01 01 01 02 .........x...... +| 3920: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| 3936: 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 ................ +| 3952: 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 ................ +| 3968: 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 ................ +| 3984: 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 ................ +| 4000: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| 4016: 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 ................ +| 4032: 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 ................ +| 4048: 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 ................ +| 4064: 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 ................ +| 4080: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 02 0f f5 00 0f fb 0f f5 00 00 00 00 ................ +| 4080: 00 00 00 00 00 05 04 08 09 01 02 04 04 08 08 09 ................ +| page 7 offset 24576 +| 0: 0d 00 00 00 05 0f b8 00 0f f4 0f e9 0f d6 0f c7 ................ +| 16: 0f b8 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 4016: 00 00 00 00 00 00 00 00 0d 05 02 23 61 75 74 6f ...........#auto +| 4032: 6d 65 72 67 65 3d 35 0d 04 02 23 6d 65 72 67 65 merge=5...#merge +| 4048: 3d 31 30 30 2c 38 11 03 02 2b 69 6e 74 65 67 72 =100,8...+integr +| 4064: 69 74 79 2d 63 68 65 63 6b 09 02 02 1b 72 65 62 ity-check....reb +| 4080: 75 69 6c 64 0a 01 02 1d 6f 70 74 69 6d 69 7a 65 uild....optimize +| end crash-e1c6cbfdf643e9.db +}]} {} + +do_execsql_test 9.1 { + SELECT count(*) FROM t1 WHERE t1 MATCH '"json1 enable"'; +} {1} + +#------------------------------------------------------------------------- +reset_db +do_test 10.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 28672 pagesize 4096 filename crash-c3a971f0061039.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 07 .....@ ........ +| 32: 00 00 00 02 00 00 00 01 00 00 00 07 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 96: 00 00 00 00 0d 0e b1 00 06 0d a4 00 0f 8d 0f 21 ...............! +| 112: 0e b9 0d c8 0e 7e 0d a4 0d a4 00 00 00 00 00 00 .....~.......... +| 3488: 00 00 00 00 22 07 06 17 11 11 01 31 74 61 62 6c ...........1tabl +| 3504: 65 74 32 74 32 07 43 52 45 41 54 45 20 54 41 42 et2t2.CREATE TAB +| 3520: 4c 45 20 74 32 28 78 29 81 33 05 07 17 1f 1f 01 LE t2(x).3...... +| 3536: 82 35 74 61 62 6c 65 74 31 5f 73 65 67 64 69 72 .5tablet1_segdir +| 3552: 74 31 5f 73 65 67 64 69 72 05 43 52 45 41 54 45 t1_segdir.CREATE +| 3568: 20 54 41 42 4c 45 20 27 74 31 5f 73 65 67 64 69 TABLE 't1_segdi +| 3584: 72 27 28 6c 65 76 65 6c 20 49 4e 54 45 47 45 52 r'(level INTEGER +| 3600: 2c 69 64 78 20 49 4e 54 45 47 45 52 2c 73 74 61 ,idx INTEGER,sta +| 3616: 72 74 5f 62 6c 6f 63 6b 20 49 4e 54 45 47 45 52 rt_block INTEGER +| 3632: 2c 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 ,leaves_end_bloc +| 3648: 6b 20 49 4e 54 45 47 45 52 2c 65 6e 64 5f 62 6c k INTEGER,end_bl +| 3664: 6f 63 6b 20 49 4e 54 45 47 45 52 2c 72 6f 6f 74 ock INTEGER,root +| 3680: 20 42 4c 4f 42 2c 50 52 49 4d 41 52 59 20 4b 45 BLOB,PRIMARY KE +| 3696: 59 28 6c 65 76 65 6c 2c 20 69 64 78 29 29 31 06 Y(level, idx))1. +| 3712: 06 17 45 1f 01 00 69 6e 64 65 78 73 71 6c 69 74 ..E...indexsqlit +| 3728: 65 5f 61 75 74 6f 69 6e 64 65 78 5f 74 31 5f 73 e_autoindex_t1_s +| 3744: 65 67 64 69 72 5f 31 74 31 5f 73 65 67 64 69 72 egdir_1t1_segdir +| 3760: 06 0f c7 00 08 00 00 00 00 66 04 07 17 23 23 01 .........f...##. +| 3776: 81 13 74 61 62 6c 65 74 31 5f 73 65 67 6d 65 6e ..tablet1_segmen +| 3792: 74 73 74 31 5f 73 65 67 6d 65 6e 74 73 04 43 52 tst1_segments.CR +| 3808: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 73 EATE TABLE 't1_s +| 3824: 65 67 6d 65 6e 74 73 27 28 62 6c 6f 63 6b 69 64 egments'(blockid +| 3840: 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 INTEGER PRIMARY +| 3856: 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 KEY, block BLOB +| 3872: 29 6a 03 07 17 21 21 01 81 1f 74 61 62 6c 65 74 )j...!!...tablet +| 3888: 31 5f 63 6f 6e 74 65 6e 74 74 31 5f 63 6f 6e 74 1_contentt1_cont +| 3904: 65 6e 74 03 43 52 45 41 54 45 20 54 41 42 4c 45 ent.CREATE TABLE +| 3920: 20 27 74 31 5f 63 6f 6e 74 65 6e 74 27 28 64 6f 't1_content'(do +| 3936: 63 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d cid INTEGER PRIM +| 3952: 41 52 59 30 4b 45 59 2c 20 27 63 30 61 27 2c 20 ARY0KEY, 'c0a', +| 3968: 27 63 31 62 27 2c 20 27 63 32 63 27 29 38 02 06 'c1b', 'c2c')8.. +| 3984: 17 11 11 08 5f 74 61 62 6c 65 74 31 74 31 43 52 ...._tablet1t1CR +| 4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 33 LE t1 USING fts3 +| 4032: 28 61 2c 62 2c 63 29 00 00 00 39 00 00 00 00 00 (a,b,c)...9..... +| page 3 offset 8192 +| 0: 0d 00 00 00 25 0b 48 00 0f d8 0f af 0f 86 0f 74 ....%.H........t +| 16: 0f 61 0f 4e 0f 2f 0f 0f 0e ef 0e d7 0e be 0e a5 .a.N./.......... +| 32: 0e 8d 0e 74 0e 5b 0e 40 0e 24 0e 08 0d ef 0d d5 ...t.[.@.$...... +| 48: 0d bb 0d a0 0d 84 0d 68 0d 4f 0d 35 0d 1b 0c fb .......h.O.5.... +| 64: 0c da 0c b9 0c 99 0c 78 0c 57 0c 3e 0c 24 0c 0a .......x.W.>.$.. +| 80: 0b 48 00 00 00 00 00 00 00 00 00 00 00 00 00 00 .H.............. +| 2880: 00 00 00 00 00 00 00 00 81 3f 25 06 00 82 7f 00 .........?%..... +| 2896: 00 43 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2e .COMPILER=gcc-5. +| 2912: 34 2e 30 20 32 30 31 36 30 36 30 39 20 44 45 42 4.0 20160609 DEB +| 2928: 55 47 20 45 4e 41 42 4c 45 20 44 42 53 54 41 54 UG ENABLE DBSTAT +| 2944: 20 56 54 41 42 20 45 4e 41 42 4c 45 20 46 54 53 VTAB ENABLE FTS +| 2960: 34 20 45 4e 41 42 4c 45 20 46 54 53 35 20 45 4e 4 ENABLE FTS5 EN +| 2976: 41 42 4c 45 20 47 45 4f 50 4f 4c 59 20 45 4e 41 ABLE GEOPOLY ENA +| 2992: 42 4c 45 20 4a 53 4f 4e 31 20 45 4e 41 42 4c 45 BLE JSON1 ENABLE +| 3008: 20 4d 45 4d 53 59 53 35 20 45 4e 41 42 4c 45 1f MEMSYS5 ENABLE. +| 3024: 52 54 52 45 45 20 4d 41 58 20 4d 45 4d 4f 52 59 RTREE MAX MEMORY +| 3040: 3d 35 30 30 30 30 30 30 30 20 4f 4d 49 54 20 4c =50000000 OMIT L +| 3056: 4f 41 44 20 45 58 54 45 4e 53 49 4f 4e 20 54 48 OAD EXTENSION TH +| 3072: 52 45 41 44 53 41 46 45 3d 30 18 24 05 00 25 0f READSAFE=0.$..%. +| 3088: 19 54 48 52 45 41 44 53 41 46 45 3d 30 58 42 49 .THREADSAFE=0XBI +| 3104: 4e 41 52 59 18 23 05 00 25 0f 19 54 48 52 45 41 NARY.#..%..THREA +| 3120: 44 53 41 46 45 3d 30 58 4e 4f 43 41 53 45 17 22 DSAFE=0XNOCASE.. +| 3136: 05 00 25 0f 17 54 48 52 45 41 44 53 41 46 45 3d ..%..THREADSAFE= +| 3152: 30 58 52 54 52 49 4d 1f 21 05 00 33 0f 19 4f 4d 0XRTRIM.!..3..OM +| 3168: 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 4f IT LOAD EXTENSIO +| 3184: 4e 58 43 49 4e 41 52 59 1f 20 05 00 33 0f 19 4f NXCINARY. ..3..O +| 3200: 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 MIT LOAD EXTENSI +| 3216: 4f 4e 58 4e 4f 43 41 53 45 1e 1f 05 00 33 0f 17 ONXNOCASE....3.. +| 3232: 4f 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 OMIT LOAD EXTENS +| 3248: 49 4f 4e 58 52 54 52 49 4d 1f 1e 05 00 33 0f 19 IONXRTRIM....3.. +| 3264: 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 30 MAX MEMORY=50000 +| 3280: 30 30 30 58 42 49 4e 41 52 59 1f 1d 05 00 33 0f 000XBINARY....3. +| 3296: 19 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 .MAX MEMORY=5000 +| 3312: 30 30 30 30 58 4e 4f 43 41 53 45 1e 1c 05 00 33 0000XNOCASE....3 +| 3328: 0f 17 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 ..MAX MEMORY=500 +| 3344: 30 30 30 30 30 58 52 54 52 49 4d 18 1b 05 00 25 00000XRTRIM....% +| 3360: 0f 19 45 4e 41 42 4c 45 20 52 54 52 45 45 58 42 ..ENABLE RTREEXB +| 3376: 49 4e 41 52 59 18 1a 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3392: 4c 45 20 52 54 52 45 45 58 4e 4f 43 41 53 45 17 LE RTREEXNOCASE. +| 3408: 19 05 00 25 0f 17 45 4e 41 42 4c 45 20 52 54 52 ...%..ENABLE RTR +| 3424: 45 45 58 52 54 52 49 4d 1a 18 05 00 29 0f 19 45 EEXRTRIM....)..E +| 3440: 4e 41 42 4c 45 20 4d 45 4d 53 59 53 35 58 42 49 NABLE MEMSYS5XBI +| 3456: 4e 41 52 59 1a 17 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3472: 45 20 4d 45 4d 53 59 53 35 58 4e 4f 43 41 53 45 E MEMSYS5XNOCASE +| 3488: 19 16 05 00 29 0f 17 45 4e 41 42 4c 45 20 4d 45 ....)..ENABLE ME +| 3504: 4d 53 59 53 35 58 52 54 52 49 4d 18 15 09 00 25 MSYS5XRTRIM....% +| 3520: 0f 19 45 4e 41 42 4c 45 20 4a 53 4f 4e 31 58 42 ..ENABLE JSON1XB +| 3536: 49 4e 41 52 59 18 14 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3552: 4c 45 20 4a 53 4f 4e 31 58 4e 4f 43 41 53 45 17 LE JSON1XNOCASE. +| 3568: 13 05 00 25 0f 17 45 4e 41 42 4c 45 20 4a 53 4f ...%..ENABLE JSO +| 3584: 4e 31 58 52 54 52 49 4d 1a 12 05 00 29 0f 19 45 N1XRTRIM....)..E +| 3600: 4e 41 42 4c 45 20 47 45 4f 50 4f 4c 59 58 42 49 NABLE GEOPOLYXBI +| 3616: 4e 41 52 59 1a 11 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3632: 45 20 47 45 4f 50 4f 4c 59 58 4e 4f 43 41 53 45 E GEOPOLYXNOCASE +| 3648: 19 10 05 00 29 0f 17 45 4e 41 42 4c 46 20 47 45 ....)..ENABLF GE +| 3664: 4f 50 4f 4c 59 58 52 54 52 49 4d 17 0f 05 00 23 OPOLYXRTRIM....# +| 3680: 0f 19 45 4e 41 42 4c 45 20 46 54 53 35 58 42 49 ..ENABLE FTS5XBI +| 3696: 4e 41 52 59 17 0e 05 00 23 0f 19 45 4e 41 42 4c NARY....#..ENABL +| 3712: 45 20 46 54 53 35 58 4e 4f 43 41 53 45 16 0d 05 E FTS5XNOCASE... +| 3728: 00 23 0f 17 45 4e 41 42 4c 45 20 46 54 53 35 58 .#..ENABLE FTS5X +| 3744: 52 54 52 49 4d 17 0c 05 00 23 0f 19 45 4e 41 42 RTRIM....#..ENAB +| 3760: 4c 45 20 46 54 53 34 58 42 49 4e 41 52 59 17 0b LE FTS4XBINARY.. +| 3776: 05 00 23 0f 19 45 4e 41 42 4c 45 20 46 54 53 34 ..#..ENABLE FTS4 +| 3792: 58 4e 4f 43 41 53 45 16 0a 05 00 23 0f 17 45 4e XNOCASE....#..EN +| 3808: 41 42 4c 45 20 46 54 53 34 58 52 54 52 49 4d 1e ABLE FTS4XRTRIM. +| 3824: 09 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3840: 54 41 54 20 56 54 41 42 58 42 49 4e 41 52 59 1e TAT VTABXBINARY. +| 3856: 08 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3872: 54 41 54 20 56 54 41 42 58 4e 4f 43 41 53 45 1d TAT VTABXNOCASE. +| 3888: 07 05 00 31 0f 17 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3904: 54 41 54 20 56 54 41 42 58 52 54 52 49 4d 11 06 TAT VTABXRTRIM.. +| 3920: 05 00 17 0f 19 44 45 42 55 47 58 42 49 4e 41 52 .....DEBUGXBINAR +| 3936: 59 11 05 05 00 17 0f 19 44 45 42 55 47 58 4e 4f Y.......DEBUGXNO +| 3952: 43 41 53 45 10 04 05 00 17 0f 17 44 45 42 55 47 CASE.......DEBUG +| 3968: 58 52 54 52 49 4d 27 03 05 00 43 0f 19 43 4f 4d XRTRIM'...C..COM +| 3984: 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e 30 20 PILER=gcc-5.4.0 +| 4000: 32 30 31 36 30 36 30 39 58 42 49 4e 41 52 59 27 20160609XBINARY' +| 4016: 02 05 00 43 0f 19 43 4f 4d 50 49 4c 45 52 3d 67 ...C..COMPILER=g +| 4032: 63 63 2d 35 2e 34 2e 30 20 32 30 31 36 30 36 30 cc-5.4.0 2016060 +| 4048: 39 58 4e 4f 43 41 53 45 26 01 05 00 43 0f 17 43 9XNOCASE&...C..C +| 4064: 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e OMPILER=gcc-5.4. +| 4080: 30 20 32 30 31 36 30 36 30 39 58 52 54 52 49 4d 0 20160609XRTRIM +| page 4 offset 12288 +| 0: 0d 00 00 00 00 10 00 00 00 00 00 00 00 00 00 00 ................ +| page 5 offset 16384 +| 0: 0d 00 00 00 02 0b a0 00 0c ad 0b a0 00 00 00 00 ................ +| 2976: 82 0a 02 08 08 09 08 08 17 84 06 30 20 32 35 33 ...........0 253 +| 2992: 00 01 30 04 25 06 1b 00 00 08 32 30 31 36 30 36 ..0.%.....201606 +| 3008: 30 39 03 25 07 00 00 01 34 03 25 05 00 00 01 35 09.%....4.%....5 +| 3024: 03 25 04 00 01 07 30 30 30 30 30 30 30 03 25 1a .%....0000000.%. +| 3040: 00 00 08 63 6f 6d 70 69 6c 65 72 03 25 02 00 00 ...compiler.%... +| 3056: 06 64 62 73 74 61 74 03 25 0a 00 01 04 65 62 75 .dbstat.%....ebu +| 3072: 67 03 25 08 00 00 06 65 6e 61 62 6c 65 09 25 09 g.%....enable.%. +| 3088: 05 04 04 04 04 04 00 01 08 78 74 65 6e 73 69 6f .........xtensio +| 3104: 6e 03 25 1d 00 00 04 66 74 73 34 03 25 0d 00 03 n.%....fts4.%... +| 3120: 01 35 03 25 0f 00 00 03 67 63 63 03 25 03 00 01 .5.%....gcc.%... +| 3136: 06 65 6f 70 6f 6c 79 03 25 11 00 00 05 6a 73 6f .eopoly.%....jso +| 3152: 6e 31 03 25 13 00 00 04 6c 6f 61 64 03 25 1c 00 n1.%....load.%.. +| 3168: 00 03 6d 61 78 03 25 18 00 01 05 65 6d 6f 72 79 ..max.%....emory +| 3184: 03 25 19 00 03 04 73 79 73 35 03 25 15 00 00 04 .%....sys5.%.... +| 3200: 6f 6d 69 74 03 25 1b 00 00 05 72 74 72 65 65 03 omit.%....rtree. +| 3216: 25 17 00 00 0a 74 68 72 65 61 64 73 61 66 65 03 %....threadsafe. +| 3232: 25 1e 00 00 04 76 74 61 62 03 25 0b 00 86 50 01 %....vtab.%...P. +| 3248: 08 08 08 08 09 17 8d 12 30 20 38 33 35 00 01 30 ........0 835..0 +| 3264: 12 01 06 00 01 06 00 01 06 00 1f 03 00 01 03 00 ................ +| 3280: 01 03 00 00 08 32 30 31 36 30 36 30 39 09 01 07 .....20160609... +| 3296: 00 01 07 00 01 07 00 00 01 34 09 01 05 00 01 05 .........4...... +| 3312: 00 01 05 00 00 01 35 09 01 04 00 01 04 00 01 04 ......5......... +| 3328: 00 01 07 30 30 30 30 30 30 30 09 1c 04 00 01 04 ...0000000...... +| 3344: 00 01 04 00 00 06 62 69 6e 61 72 79 3c 03 01 02 ......binary<... +| 3360: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3376: 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 ................ +| 3392: 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03 ................ +| 3408: 01 02 02 00 03 01 02 02 00 00 08 63 6f 6d 70 69 ...........compi +| 3424: 6c 65 72 09 01 02 00 01 02 00 01 02 00 00 06 64 ler............d +| 3440: 62 73 74 61 74 09 07 03 00 01 03 00 01 03 00 01 bstat........... +| 3456: 04 65 62 75 67 09 04 02 00 01 02 00 01 02 00 00 .ebug........... +| 3472: 06 65 6e 61 62 6c 65 3f 07 02 00 01 02 00 01 02 .enable?........ +| 3488: 00 01 02 00 01 02 00 01 02 00 01 02 00 01 02 00 ................ +| 3504: 01 02 00 01 02 00 01 02 00 01 02 00 01 02 00 01 ................ +| 3520: 02 00 01 02 00 01 02 00 01 02 00 01 02 00 01 02 ................ +| 3536: 00 01 02 00 01 02 00 01 08 78 74 65 6e 73 69 6f .........xtensio +| 3552: 6e 09 1f 04 00 01 04 00 01 04 00 00 04 66 74 73 n............fts +| 3568: 34 09 0a 03 00 01 03 00 01 03 00 03 01 35 09 0d 4............5.. +| 3584: 03 00 01 03 00 01 03 00 00 03 67 63 63 09 01 03 ..........gcc... +| 3600: 00 01 03 00 01 03 00 01 06 65 6f 70 6f 6c 79 09 .........eopoly. +| 3616: 10 03 00 01 03 00 01 03 00 00 05 6a 73 6f 6e 31 ...........json1 +| 3632: 09 13 03 00 01 03 00 01 03 00 00 04 6c 6f 61 64 ............load +| 3648: 09 1f 03 00 01 03 00 01 03 00 00 03 6d 61 78 09 ............max. +| 3664: 1c 02 00 01 02 00 01 02 00 01 05 65 6d 6f 72 79 ...........emory +| 3680: 09 1c 03 00 01 03 00 01 03 00 03 04 73 79 73 35 ............sys5 +| 3696: 09 16 03 00 01 03 00 01 03 00 00 06 6e 6f 63 61 ............noca +| 3712: 73 65 3c 02 01 02 02 00 03 01 02 02 00 03 01 02 se<............. +| 3728: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3744: 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 ................ +| 3760: 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 00 ................ +| 3776: 04 6f 6d 69 74 09 1f 02 00 01 02 00 01 02 00 00 .omit........... +| 3792: 05 72 74 72 65 65 09 19 03 00 01 03 00 01 03 00 .rtree.......... +| 3808: 03 02 69 6d 3c 01 01 02 02 00 03 01 02 02 00 03 ..im<........... +| 3824: 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01 ................ +| 3840: 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 ................ +| 3856: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3872: 00 00 0a 74 68 72 65 61 64 73 61 66 65 09 22 02 ...threadsafe... +| 3888: 00 01 02 00 02 02 00 00 04 76 74 61 62 09 07 04 .........vtab... +| 3904: 00 01 04 00 01 04 00 00 01 78 b4 01 01 01 01 02 .........x...... +| 3920: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| 3936: 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 ................ +| 3952: 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 ................ +| 3968: 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 ................ +| 3984: 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 ................ +| 4000: 10 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| 4016: 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 ................ +| 4032: 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 ................ +| 4048: 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 ................ +| 4064: 02 00 01 01 02 02 00 01 01 01 02 00 01 01 01 02 ................ +| 4080: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 02 0f f5 00 0f fb 0f f5 00 00 00 00 ................ +| 4080: 00 00 00 00 00 05 04 08 09 01 02 04 04 08 08 09 ................ +| page 7 offset 24576 +| 0: 0d 00 00 00 05 0f b8 00 0f f4 0f e9 0f d6 0f c7 ................ +| 16: 0f b8 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 4016: 00 00 00 00 00 00 00 00 0d 05 02 23 61 75 74 6f ...........#auto +| 4032: 6d 65 72 67 65 3d 35 0d 04 02 23 6d 65 72 67 65 merge=5...#merge +| 4048: 3d 31 30 30 2c 38 11 03 02 2b 69 6e 74 65 67 72 =100,8...+integr +| 4064: 69 74 79 2d 63 68 65 63 6b 09 02 02 1b 72 65 62 ity-check....reb +| 4080: 75 69 6c 64 0a 01 02 1d 6f 70 74 69 6d 69 7a 65 uild....optimize +| end crash-c3a971f0061039.db +}]} {} + +do_catchsql_test 10.1 { + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<100) + INSERT OR IGNORE INTO t1(a,c) SELECT x,null FROM c + UNION ALL SELECT 180-x,printf('[%,d]',x*-5844627) FROM c; +} {0 {}} + +do_catchsql_test 10.3 { + INSERT INTO t1(t1) VALUES('optimize'); +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +reset_db +do_test 11.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 28672 pagesize 4096 filename crash-843cb8447eaf14.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 07 .....@ ........ +| 32: 00 00 00 02 00 00 00 01 00 00 00 07 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 96: 00 00 00 00 0d 0e b1 00 06 0d a4 00 0f 8d 0f 21 ...............! +| 112: 0e b9 0d c8 0e 7e 0d a4 0d a4 00 00 00 00 00 00 .....~.......... +| 3488: 00 00 00 00 22 07 06 17 11 11 01 31 74 61 62 6c ...........1tabl +| 3504: 65 74 32 74 32 07 43 52 45 41 54 45 20 54 41 42 et2t2.CREATE TAB +| 3520: 4c 45 20 74 32 28 78 29 81 33 05 07 17 1f 1f 01 LE t2(x).3...... +| 3536: 82 35 74 61 62 6c 65 74 31 5f 73 65 67 64 69 72 .5tablet1_segdir +| 3552: 74 31 5f 73 65 67 64 69 72 05 43 52 45 41 54 45 t1_segdir.CREATE +| 3568: 20 54 41 42 4c 45 20 27 74 31 5f 73 65 67 64 69 TABLE 't1_segdi +| 3584: 72 27 28 6c 65 76 65 6c 20 49 4e 54 45 47 45 52 r'(level INTEGER +| 3600: 2c 69 64 78 20 49 4e 54 45 47 45 52 2c 73 74 61 ,idx INTEGER,sta +| 3616: 72 74 5f 62 6c 6f 63 6b 20 49 4e 54 45 47 45 52 rt_block INTEGER +| 3632: 2c 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 ,leaves_end_bloc +| 3648: 6b 20 49 4e 54 45 47 45 52 2c 65 6e 64 5f 62 6c k INTEGER,end_bl +| 3664: 6f 63 6b 20 49 4e 54 45 47 45 52 2c 72 6f 6f 74 ock INTEGER,root +| 3680: 20 42 4c 4f 42 2c 50 52 49 4d 41 52 59 20 4b 45 BLOB,PRIMARY KE +| 3696: 59 28 6c 65 76 65 6c 2c 20 69 64 78 29 29 31 06 Y(level, idx))1. +| 3712: 06 17 45 1f 01 00 69 6e 64 65 78 73 71 6c 69 74 ..E...indexsqlit +| 3728: 65 5f 61 75 74 6f 69 6e 64 65 78 5f 74 31 5f 73 e_autoindex_t1_s +| 3744: 65 67 64 69 72 5f 31 74 31 5f 73 65 67 64 69 72 egdir_1t1_segdir +| 3760: 06 0f c7 00 08 00 00 00 00 66 04 07 17 23 23 01 .........f...##. +| 3776: 81 13 74 61 62 6c 65 74 31 5f 73 65 67 6d 65 6e ..tablet1_segmen +| 3792: 74 73 74 31 5f 73 65 67 6d 65 6e 74 73 04 43 52 tst1_segments.CR +| 3808: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 73 EATE TABLE 't1_s +| 3824: 65 67 6d 65 6e 74 73 27 28 62 6c 6f 63 6b 69 64 egments'(blockid +| 3840: 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 INTEGER PRIMARY +| 3856: 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 KEY, block BLOB +| 3872: 29 6a 03 07 17 21 21 01 81 1f 74 61 62 6c 65 74 )j...!!...tablet +| 3888: 31 5f 63 6f 6e 74 65 6e 74 74 31 5f 63 6f 6e 74 1_contentt1_cont +| 3904: 65 6e 74 03 43 52 45 41 54 45 20 54 41 42 4c 45 ent.CREATE TABLE +| 3920: 20 27 74 31 5f 63 6f 6e 74 65 6e 74 27 28 64 6f 't1_content'(do +| 3936: 63 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d cid INTEGER PRIM +| 3952: 41 52 59 20 4b 45 59 2c 20 27 63 30 61 27 2c 20 ARY KEY, 'c0a', +| 3968: 27 63 31 62 27 2c 20 27 63 32 63 27 29 38 02 06 'c1b', 'c2c')8.. +| 3984: 17 11 11 08 5f 74 61 62 6c 65 74 31 74 31 43 52 ...._tablet1t1CR +| 4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 33 LE t1 USING fts3 +| 4032: 28 61 2c 62 2c 63 29 00 00 00 39 00 00 00 00 00 (a,b,c)...9..... +| page 3 offset 8192 +| 0: 0d 00 00 00 25 0b 48 00 0f d8 0f af 0f 86 0f 74 ....%.H........t +| 16: 0f 61 0f 4e 0f 2f 0f 0f 0e ef 0e d7 0e be 0e a5 .a.N./.......... +| 32: 0e 8d 0e 74 0e 5b 0e 40 0e 24 0e 08 0d ef 0d d4 ...t.[.@.$...... +| 48: dd bb 0d a0 0d 84 0d 68 0d 4f 0d 35 0d 1b 0c fb .......h.O.5.... +| 64: 0c da 0c b9 35 99 0c 78 0c 57 0c 3e 0c 24 0c 0a ....5..x.W.>.$.. +| 80: 0b 48 00 00 00 00 00 00 00 00 00 00 00 00 00 00 .H.............. +| 2880: 00 00 00 00 00 00 00 00 81 3f 25 06 00 82 7f 00 .........?%..... +| 2896: 00 43 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2e .COMPILER=gcc-5. +| 2912: 34 2e 30 20 32 30 31 36 30 36 30 39 20 44 45 42 4.0 20160609 DEB +| 2928: 55 47 20 45 4e 41 42 4c 45 20 44 42 53 54 41 54 UG ENABLE DBSTAT +| 2944: 20 56 54 41 42 20 45 4e 41 42 4c 45 20 46 54 53 VTAB ENABLE FTS +| 2960: 34 20 45 4e 41 42 4c 45 20 46 54 53 35 20 45 4e 4 ENABLE FTS5 EN +| 2976: 41 42 4c 45 20 47 45 4f 50 4f 4c 59 20 45 4e 41 ABLE GEOPOLY ENA +| 2992: 42 4c 45 20 4a 53 4f 4e 31 20 45 4e 41 42 4c 45 BLE JSON1 ENABLE +| 3008: 20 4d 45 4d 53 59 53 35 20 45 4e 41 42 4c 45 20 MEMSYS5 ENABLE +| 3024: 52 54 52 45 45 20 4d 41 58 20 4d 45 4d 4f 52 59 RTREE MAX MEMORY +| 3040: 3d 35 30 30 30 30 30 30 30 20 4f 4d 49 54 20 4c =50000000 OMIT L +| 3056: 4f 41 44 20 45 58 54 45 4e 53 49 4f 4e 20 54 48 OAD EXTENSION TH +| 3072: 52 45 41 44 53 41 46 45 3d 30 18 24 05 00 25 0f READSAFE=0.$..%. +| 3088: 19 54 48 52 45 41 44 53 41 46 45 3d 30 58 42 49 .THREADSAFE=0XBI +| 3104: 4e 41 52 59 18 23 05 00 25 0f 19 54 48 52 45 41 NARY.#..%..THREA +| 3120: 44 53 41 46 45 3d 30 58 4e 4f 43 41 53 45 17 22 DSAFE=0XNOCASE.. +| 3136: 05 00 25 0f 17 54 48 52 45 41 44 53 41 46 45 3d ..%..THREADSAFE= +| 3152: 30 58 52 54 52 49 4d 1f 21 05 00 33 0f 19 4f 4d 0XRTRIM.!..3..OM +| 3168: 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 4f IT LOAD EXTENSIO +| 3184: 4e 58 42 49 4e 41 52 59 1f 20 05 00 33 0f 19 4f NXBINARY. ..3..O +| 3200: 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 MIT LOAD EXTENSI +| 3216: 4f 4e 58 4e 4f 43 41 53 45 1e 1f 05 00 33 0f 17 ONXNOCASE....3.. +| 3232: 4f 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 OMIT LOAD EXTENS +| 3248: 49 4f 4e 58 52 54 52 49 4d 1f 1e 05 00 33 0f 19 IONXRTRIM....3.. +| 3264: 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 30 MAX MEMORY=50000 +| 3280: 30 30 30 58 42 49 4e 41 52 59 1f 1d 05 00 33 0f 000XBINARY....3. +| 3296: 19 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 .MAX MEMORY=5000 +| 3312: 30 30 30 30 58 4e 4f 43 41 53 45 1e 1c 05 00 33 0000XNOCASE....3 +| 3328: 0f 17 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 ..MAX MEMORY=500 +| 3344: 30 30 30 30 30 58 52 54 52 49 4d 18 1b 05 00 25 00000XRTRIM....% +| 3360: 0f 19 45 4e 41 42 4c 45 20 52 54 52 45 45 58 42 ..ENABLE RTREEXB +| 3376: 49 4e 41 52 59 18 1a 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3392: 4c 45 20 52 54 52 45 45 58 4e 4f 43 41 53 45 17 LE RTREEXNOCASE. +| 3408: 19 05 00 25 0f 17 45 4e 41 42 4c 45 20 52 54 52 ...%..ENABLE RTR +| 3424: 45 45 58 52 54 52 49 4d 1a 18 05 00 29 0f 19 45 EEXRTRIM....)..E +| 3440: 4e 41 42 4c 45 20 4d 45 4d 53 59 53 35 58 42 49 NABLE MEMSYS5XBI +| 3456: 4e 41 52 59 1a 17 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3472: 45 20 4d 45 4d 53 59 53 35 58 4e 4f 43 41 53 45 E MEMSYS5XNOCASE +| 3488: 19 16 05 00 29 0f 17 45 4e 41 42 4c 45 20 4d 45 ....)..ENABLE ME +| 3504: 4d 53 59 53 35 58 52 54 52 49 4d 18 15 05 00 25 MSYS5XRTRIM....% +| 3520: 0f 19 45 4e 41 42 4c 45 20 4a 53 4f 4e 31 58 42 ..ENABLE JSON1XB +| 3536: 49 4e 41 52 59 18 14 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3552: 4c 45 20 4a 53 4f 4e 31 58 4e 4f 43 41 53 45 17 LE JSON1XNOCASE. +| 3568: 13 05 00 25 0f 17 45 4e 41 42 4c 45 20 4a 53 4f ...%..ENABLE JSO +| 3584: 4e 31 58 52 54 52 49 4d 1a 12 05 00 29 0f 19 45 N1XRTRIM....)..E +| 3600: 4e 41 42 4c 45 20 47 45 4f 50 4f 4c 59 58 42 49 NABLE GEOPOLYXBI +| 3616: 4e 41 52 59 1a 11 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3632: 45 20 47 45 4f 50 4f 4c 59 58 4e 4f 43 41 53 45 E GEOPOLYXNOCASE +| 3648: 19 10 05 00 29 0f 17 45 4e 41 42 4c 45 20 47 45 ....)..ENABLE GE +| 3664: 4f 50 4f 4c 59 58 52 54 52 49 4d 17 0f 05 00 23 OPOLYXRTRIM....# +| 3680: 0f 19 45 4e 41 42 4c 45 20 46 54 53 35 58 42 49 ..ENABLE FTS5XBI +| 3696: 4e 41 52 59 17 0e 05 00 23 0f 19 45 4e 41 42 4c NARY....#..ENABL +| 3712: 45 20 46 54 53 35 58 4e 4f 43 41 53 45 16 0d 05 E FTS5XNOCASE... +| 3728: 00 23 0f 17 45 4e 41 42 4c 45 20 46 54 53 35 58 .#..ENABLE FTS5X +| 3744: 52 54 52 49 4d 17 0c 05 00 23 0f 19 45 4e 41 42 RTRIM....#..ENAB +| 3760: 4c 45 20 46 54 53 34 58 42 49 4e 41 52 59 17 0b LE FTS4XBINARY.. +| 3776: 05 00 23 0f 19 45 4e 41 42 4c 45 20 46 54 53 34 ..#..ENABLE FTS4 +| 3792: 58 4e 4f 43 41 53 45 16 0a 05 00 23 0f 17 45 4e XNOCASE....#..EN +| 3808: 41 42 4c 45 20 46 54 53 34 58 52 54 52 49 4d 1e ABLE FTS4XRTRIM. +| 3824: 09 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3840: 54 41 54 20 56 54 41 42 58 42 49 4e 41 52 59 1e TAT VTABXBINARY. +| 3856: 08 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3872: 54 41 54 20 56 54 41 42 58 4e 4f 43 41 53 45 1d TAT VTABXNOCASE. +| 3888: 07 05 00 31 0f 17 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3904: 54 41 54 20 56 54 41 42 58 52 54 52 49 4d 11 06 TAT VTABXRTRIM.. +| 3920: 05 00 17 0f 19 44 45 42 55 47 58 42 49 4e 41 52 .....DEBUGXBINAR +| 3936: 59 11 05 05 00 17 0f 19 44 45 42 55 47 58 4e 4f Y.......DEBUGXNO +| 3952: 43 41 53 45 10 04 05 00 17 0f 17 44 45 42 55 47 CASE.......DEBUG +| 3968: 58 52 54 52 49 4d 27 03 05 00 43 0f 19 43 4f 4d XRTRIM'...C..COM +| 3984: 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e 30 20 PILER=gcc-5.4.0 +| 4000: 32 30 31 36 30 36 30 39 58 42 49 4e 41 52 59 27 20160609XBINARY' +| 4016: 02 05 00 43 0f 19 43 4f 4d 50 49 4c 45 52 3d 67 ...C..COMPILER=g +| 4032: 63 63 2d 35 2e 34 2e 30 20 32 30 31 36 30 36 30 cc-5.4.0 2016060 +| 4048: 39 58 4e 4f 43 41 53 45 26 01 05 00 43 0f 17 43 9XNOCASE&...C..C +| 4064: 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e OMPILER=gcc-5.4. +| 4080: 30 20 32 30 31 36 30 36 30 39 58 52 54 52 49 4d 0 20160609XRTRIM +| page 4 offset 12288 +| 0: 0d 00 00 00 00 10 00 00 00 00 00 00 00 00 00 00 ................ +| page 5 offset 16384 +| 0: 0d 00 00 00 02 0b a0 00 0c ad 0b a0 00 00 00 00 ................ +| 2976: 82 0a 02 08 08 09 08 08 17 84 06 30 20 32 35 33 ...........0 253 +| 2992: 00 01 30 04 25 06 1b 00 00 08 32 30 31 36 30 36 ..0.%.....201606 +| 3008: 30 39 03 25 07 00 00 01 34 03 25 05 00 00 01 35 09.%....4.%....5 +| 3024: 03 25 04 00 01 07 30 30 30 30 30 30 30 03 25 1a .%....0000000.%. +| 3040: 00 00 08 63 6f 6d 70 69 6c 65 72 03 25 02 00 00 ...compiler.%... +| 3056: 06 64 62 73 74 61 74 03 25 0a 00 01 04 65 62 75 .dbstat.%....ebu +| 3072: 67 03 25 08 00 00 06 65 6e 61 62 6c 65 09 25 09 g.%....enable.%. +| 3088: 05 04 04 04 04 04 00 01 08 78 74 65 6e 73 69 6f .........xtensio +| 3104: 6e 03 25 1d 00 00 04 66 74 73 34 03 25 0d 00 03 n.%....fts4.%... +| 3120: 01 35 03 25 0f 00 00 03 67 63 63 03 25 03 00 01 .5.%....gcc.%... +| 3136: 06 65 6f 70 6f 6c 79 03 25 11 00 00 05 6a 73 6f .eopoly.%....jso +| 3152: 6e 31 03 25 13 00 00 04 6c 6f 61 64 03 25 1c 00 n1.%....load.%.. +| 3168: 00 03 6d 61 78 03 25 18 00 01 05 65 6d 6f 72 79 ..max.%....emory +| 3184: 03 25 19 00 03 04 73 79 73 35 03 25 15 00 00 04 .%....sys5.%.... +| 3200: 6f 6d 69 74 03 25 1b 00 00 05 72 74 72 65 65 03 omit.%....rtree. +| 3216: 25 17 00 00 0a 74 68 72 65 61 64 73 61 66 65 03 %....threadsafe. +| 3232: 25 1e 00 00 04 76 74 61 62 03 25 0b 00 86 50 01 %....vtab.%...P. +| 3248: 08 08 08 08 08 17 8d 12 30 20 38 33 35 00 01 30 ........0 835..0 +| 3264: 12 01 06 00 01 06 00 01 06 00 1f 03 00 01 03 00 ................ +| 3280: 01 03 00 00 08 32 30 31 36 30 36 30 39 09 01 07 .....20160609... +| 3296: 00 01 07 00 01 07 00 00 01 34 09 01 05 00 01 05 .........4...... +| 3312: 00 01 05 00 00 01 35 09 01 04 00 01 04 00 01 04 ......5......... +| 3328: 00 01 07 30 30 30 30 30 30 30 09 1c 04 00 01 04 ...0000000...... +| 3344: 00 01 04 00 00 06 62 69 6e 61 72 79 3c 03 01 02 ......binary<... +| 3360: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3376: 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 ................ +| 3392: 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03 ................ +| 3408: 01 02 02 00 03 01 02 02 00 00 08 63 6f 6d 70 69 ...........compi +| 3424: 6c 65 72 09 01 02 00 01 02 00 01 02 00 00 06 64 ler............d +| 3440: 62 73 74 61 74 09 07 03 00 01 03 00 01 03 00 01 bstat........... +| 3456: 04 65 62 75 67 09 04 02 00 01 02 00 01 02 00 00 .ebug........... +| 3472: 06 65 6e 61 62 6c 65 3f 07 02 00 01 02 00 01 02 .enable?........ +| 3488: 00 01 02 00 01 02 00 01 02 00 01 02 00 01 02 00 ................ +| 3504: 01 02 00 01 02 00 01 02 00 01 02 00 01 01 00 01 ................ +| 3520: 02 00 01 02 00 01 02 00 01 02 00 01 02 00 01 02 ................ +| 3536: 00 01 02 00 01 02 00 01 08 78 74 65 6e 73 69 6f .........xtensio +| 3552: 6e 09 1f 04 00 01 04 00 01 04 00 00 04 66 74 73 n............fts +| 3568: 34 09 0a 03 00 01 03 00 01 03 00 03 01 35 09 0d 4............5.. +| 3584: 03 00 01 03 00 01 03 00 00 03 67 63 63 09 01 03 ..........gcc... +| 3600: 00 01 03 00 01 03 00 01 06 65 6f 70 6f 6c 79 09 .........eopoly. +| 3616: 10 03 00 01 03 00 01 03 00 00 05 6a 73 6f 6e 31 ...........json1 +| 3632: 09 13 03 00 01 03 00 01 03 00 00 04 6c 6f 61 64 ............load +| 3648: 09 1f 03 00 01 03 00 01 03 00 00 03 6d 61 78 09 ............max. +| 3664: 1c 02 00 01 02 00 01 02 00 01 05 65 6d 6f 72 79 ...........emory +| 3680: 09 1c 03 00 01 03 00 01 03 00 03 04 73 79 73 35 ............sys5 +| 3696: 09 16 03 00 01 03 00 01 03 00 00 06 6e 6f 63 61 ............noca +| 3712: 73 65 3c 02 01 02 02 00 03 01 02 02 00 03 01 02 se<............. +| 3728: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3744: 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 ................ +| 3760: 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 00 ................ +| 3776: 04 6f 6d 69 74 09 1f 02 00 01 02 00 01 02 00 00 .omit........... +| 3792: 05 72 74 72 65 65 09 19 03 00 01 03 00 01 03 00 .rtree.......... +| 3808: 03 02 69 6d 3c 01 01 02 02 00 03 01 02 02 00 03 ..im<........... +| 3824: 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01 ................ +| 3840: 02 02 00 03 01 02 02 00 03 01 a2 02 00 03 01 02 ................ +| 3856: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3872: 00 00 0a 74 68 72 65 61 64 73 61 66 65 09 22 02 ...threadsafe... +| 3888: 00 01 02 00 01 02 00 00 04 76 74 61 62 09 07 04 .........vtab... +| 3904: 00 01 04 00 01 04 00 00 01 78 b4 01 01 01 01 02 .........x...... +| 3920: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| 3936: 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 ................ +| 3952: 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 ................ +| 3968: 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 ................ +| 3984: 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 ................ +| 4000: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| 4016: 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 ................ +| 4032: 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 ................ +| 4048: 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 ................ +| 4064: 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 ................ +| 4080: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 02 0f f5 00 0f fb 0f f5 00 00 00 00 ................ +| 4080: 00 00 00 00 00 05 04 08 09 01 02 04 04 08 08 09 ................ +| page 7 offset 24576 +| 0: 0d 00 00 00 05 0f b8 00 0f f4 0f e9 0f d6 0f c7 ................ +| 16: 0f b8 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 4016: 00 00 00 00 00 00 00 00 0d 05 02 23 61 75 74 6f ...........#auto +| 4032: 6d 65 72 67 65 3d 35 0d 04 02 23 6d 65 72 67 65 merge=5...#merge +| 4048: 3d 31 30 30 2c 38 11 03 02 2b 69 6e 74 65 67 72 =100,8...+integr +| 4064: 69 74 79 2d 63 68 65 63 6b 09 02 02 1b 72 65 62 ity-check....reb +| 4080: 75 69 6c 64 0a 01 02 1d 6f 70 74 69 6d 69 7a 65 uild....optimize +| end crash-843cb8447eaf14.db +}]} {} + +do_catchsql_test 11.1 { + SELECT rowid, quote(matchinfo(t1,'pcxybs')) FROM t1 WHERE t1 MATCH 'e*' +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +reset_db +do_test 12.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 28672 pagesize 4096 filename c81b.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 01 00 00 00 07 .....@ ........ +| 32: 00 00 00 02 00 00 00 01 00 00 00 01 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 ................ +| 96: 00 2e 30 38 0d 0e b1 00 06 0d a4 00 0f 8d 0f 21 ..08...........! +| 112: 0e b9 0d c8 0e 7e 0d a4 0d a4 00 00 00 00 00 00 .....~.......... +| 3488: 00 00 00 00 22 07 06 17 11 11 01 31 74 61 62 6c ...........1tabl +| 3504: 65 74 32 74 32 07 43 52 45 41 54 45 20 54 41 42 et2t2.CREATE TAB +| 3520: 4c 45 20 74 32 28 78 29 81 33 05 07 17 1f 1f 01 LE t2(x).3...... +| 3536: 82 35 74 61 62 6c 65 74 31 5f 73 65 67 64 69 72 .5tablet1_segdir +| 3552: 74 31 5f 73 65 67 64 69 72 05 43 52 45 41 54 45 t1_segdir.CREATE +| 3568: 20 54 41 42 4c 45 20 27 74 31 5f 73 65 67 64 69 TABLE 't1_segdi +| 3584: 72 27 28 6c 65 76 65 6c 20 49 4e 54 45 47 45 52 r'(level INTEGER +| 3600: 2c 69 64 78 20 49 4e 54 45 47 45 52 2c 73 74 61 ,idx INTEGER,sta +| 3616: 72 74 5f 62 6c 6f 63 6b 20 49 4e 54 45 47 45 52 rt_block INTEGER +| 3632: 2c 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 ,leaves_end_bloc +| 3648: 6b 20 49 4e 54 45 47 45 52 2c 65 6e 64 5f 62 6c k INTEGER,end_bl +| 3664: 6f 63 6b 20 49 4e 54 45 47 45 62 2c 72 6f 6f 74 ock INTEGEb,root +| 3680: 20 42 4c 4f 42 2c 50 52 49 4d 41 52 59 20 4b 45 BLOB,PRIMARY KE +| 3696: 59 28 6c 65 76 65 6c 2c 20 69 64 78 29 29 31 06 Y(level, idx))1. +| 3712: 06 17 45 1f 01 00 69 6e 64 65 78 73 71 6c 69 74 ..E...indexsqlit +| 3728: 65 5f 61 75 74 6f 69 6e 64 65 78 5f 74 31 5f 73 e_autoindex_t1_s +| 3744: 65 67 64 69 72 5f 31 74 31 5f 73 65 67 64 69 72 egdir_1t1_segdir +| 3760: 06 0f c7 00 08 00 00 00 00 66 04 07 17 23 23 01 .........f...##. +| 3776: 81 13 74 61 62 6c 65 74 31 5f 73 65 67 6d 65 6e ..tablet1_segmen +| 3792: 74 73 74 31 5f 73 65 67 6d 65 6e 74 73 04 43 52 tst1_segments.CR +| 3808: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 73 EATE TABLE 't1_s +| 3824: 65 67 6d 65 6e 74 73 27 28 62 6c 6f 63 6b 69 64 egments'(blockid +| 3840: 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 INTEGER PRIMARY +| 3856: 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 KEY, block BLOB +| 3872: 29 6a 03 07 17 21 21 01 81 1f 74 61 62 6c 65 74 )j...!!...tablet +| 3888: 31 5f 63 6f 6e 74 65 6e 74 74 31 5f 63 6f 6e 74 1_contentt1_cont +| 3904: 65 6e 74 03 43 52 45 41 54 45 20 54 41 42 4c 45 ent.CREATE TABLE +| 3920: 20 27 74 31 5f 63 6f 6e 74 65 6e 74 27 28 64 6f 't1_content'(do +| 3936: 63 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d cid INTEGER PRIM +| 3952: 41 52 59 20 4b 45 59 2c 20 27 63 30 61 27 2c 20 ARY KEY, 'c0a', +| 3968: 27 63 31 62 27 2c 20 27 63 32 63 27 29 38 02 06 'c1b', 'c2c')8.. +| 3984: 17 11 11 08 5f 74 61 62 6c 65 74 31 74 31 43 52 ...._tablet1t1CR +| 4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 33 LE t1 USING fts3 +| 4032: 28 61 2c 62 2c 63 29 00 00 00 39 00 00 00 00 00 (a,b,c)...9..... +| page 3 offset 8192 +| 0: 0d 00 00 00 25 0b 48 00 0f d8 0f af 0f 86 0f 74 ....%.H........t +| 16: 0f 61 0f 4e 0f 2f 0f 0f 0e ef 0e d7 0e be 0e a5 .a.N./.......... +| 32: 0e 8d 0e 74 0e 5b 0e 40 0e 24 0e 08 0d ef 0d d5 ...t.[.@.$...... +| 48: 0d bb 0d a0 0d 84 0d 68 0d 4f 0d 35 0d 1b 0c fb .......h.O.5.... +| 64: 0c da 0c b9 0c 99 0c 78 0c 57 0c 3e 0c 24 0c 0a .......x.W.>.$.. +| 80: 0b 48 00 00 00 00 00 00 00 00 00 00 00 00 00 00 .H.............. +| 2880: 00 00 00 00 00 00 00 00 81 3f 25 06 00 82 7f 00 .........?%..... +| 2896: 00 43 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2e .COMPILER=gcc-5. +| 2912: 34 2e 30 20 32 30 31 36 30 36 30 39 20 44 45 42 4.0 20160609 DEB +| 2928: 55 47 20 45 4e 41 42 4c 45 20 44 42 53 54 41 54 UG ENABLE DBSTAT +| 2944: 20 56 54 41 42 20 45 4e 41 42 4c 45 20 46 54 53 VTAB ENABLE FTS +| 2960: 34 20 45 4e 41 42 4c 45 20 46 54 53 35 20 45 4e 4 ENABLE FTS5 EN +| 2976: 41 42 4c 45 20 47 45 4f 50 4f 4c 59 20 46 4e 41 ABLE GEOPOLY FNA +| 2992: 42 4c 45 20 4a 53 4f 4e 31 20 45 4e 41 42 4c 45 BLE JSON1 ENABLE +| 3008: 20 4d 45 4d 53 59 53 35 20 45 4e 41 42 4c 45 20 MEMSYS5 ENABLE +| 3024: 52 54 52 45 45 20 4d 41 58 20 4d 45 4d 4f 52 59 RTREE MAX MEMORY +| 3040: 3d 35 30 30 30 30 30 30 30 20 4f 4d 49 54 20 4c =50000000 OMIT L +| 3056: 4f 41 44 20 45 58 54 45 4e 53 49 4f 4e 20 54 48 OAD EXTENSION TH +| 3072: 52 45 41 44 53 41 46 45 3d 30 18 24 05 00 0f 25 READSAFE=0.$...% +| 3088: 19 58 54 48 52 45 41 44 53 41 46 45 3d 30 42 49 .XTHREADSAFE=0BI +| 3104: 4e 41 52 59 18 23 05 00 0f 25 19 58 54 48 52 45 NARY.#...%.XTHRE +| 3120: 41 44 53 41 46 45 3d 30 4e 4f 43 41 53 45 17 22 ADSAFE=0NOCASE.. +| 3136: 05 00 0f 25 17 58 54 48 52 45 41 44 53 41 46 45 ...%.XTHREADSAFE +| 3152: 3d 30 52 54 52 49 4d 1f 21 05 00 0f 33 19 58 4f =0RTRIM.!...3.XO +| 3168: 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 MIT LOAD EXTENSI +| 3184: 4f 4e 42 49 4e 41 52 59 1f 20 05 00 0f 33 19 58 ONBINARY. ...3.X +| 3200: 4f 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 OMIT LOAD EXTENS +| 3216: 49 4f 4e 4e 4f 43 41 53 45 1e 1f 05 00 0f 33 17 IONNOCASE.....3. +| 3232: 58 4f 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e XOMIT LOAD EXTEN +| 3248: 53 49 4f 4e 52 54 52 49 4d 1f 1e 05 00 0f 33 19 SIONRTRIM.....3. +| 3264: 58 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 XMAX MEMORY=5000 +| 3280: 30 30 30 30 42 49 4e 41 52 59 1f 1d 05 00 0f 33 0000BINARY.....3 +| 3296: 19 58 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 .XMAX MEMORY=500 +| 3312: 30 30 30 30 30 4e 4f 43 41 53 45 1e 1c 05 00 0f 00000NOCASE..... +| 3328: 33 17 58 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 3.XMAX MEMORY=50 +| 3344: 30 30 30 30 30 30 52 54 52 49 4d 18 1b 05 00 0f 000000RTRIM..... +| 3360: 25 19 58 45 4e 41 42 4c 45 20 52 54 52 45 45 42 %.XENABLE RTREEB +| 3376: 49 4e 41 52 59 18 1a 05 00 0f 25 19 58 45 4e 41 INARY.....%.XENA +| 3392: 42 4c 45 20 52 54 52 45 45 4e 4f 43 41 53 45 17 BLE RTREENOCASE. +| 3408: 19 05 00 0f 25 17 58 45 4e 41 42 4c 45 20 52 54 ....%.XENABLE RT +| 3424: 52 45 45 52 54 52 49 4d 1a 18 05 00 0f 29 19 58 REERTRIM.....).X +| 3440: 45 4e 41 42 4c 45 20 4d 45 4d 53 59 53 35 42 49 ENABLE MEMSYS5BI +| 3456: 4e 41 52 59 1a 17 05 00 0f 29 19 58 45 4e 41 42 NARY.....).XENAB +| 3472: 4c 45 20 4d 45 4d 53 59 53 35 4e 4f 43 41 53 45 LE MEMSYS5NOCASE +| 3488: 19 16 05 00 0f 29 17 58 45 4e 41 42 4c 45 20 4d .....).XENABLE M +| 3504: 45 4d 53 59 53 35 52 54 52 49 4d 18 15 05 00 0f EMSYS5RTRIM..... +| 3520: 25 19 58 45 4e 41 42 4c 45 20 4a 53 4f 4e 31 42 %.XENABLE JSON1B +| 3536: 49 4e 41 52 59 18 14 05 00 0f 25 19 58 45 4e 41 INARY.....%.XENA +| 3552: 42 4c 45 20 4a 53 4f 4e 31 4e 4f 43 41 53 45 17 BLE JSON1NOCASE. +| 3568: 13 05 00 0f 25 17 58 45 4e 41 42 4c 45 20 4a 53 ....%.XENABLE JS +| 3584: 4f 4e 31 52 54 52 49 4d 1a 12 05 00 0f 29 19 58 ON1RTRIM.....).X +| 3600: 45 4e 41 42 4c 45 20 47 45 4f 50 4f 4c 59 42 49 ENABLE GEOPOLYBI +| 3616: 4e 41 52 59 1a 11 05 00 0f 29 19 58 45 4e 41 1e NARY.....).XENA. +| 3632: 4c 45 20 47 45 4f 50 4f 4c 59 4e 4f 43 41 53 45 LE GEOPOLYNOCASE +| 3648: 19 10 05 00 0f 29 17 58 45 4e 41 42 4c 45 20 47 .....).XENABLE G +| 3664: 45 4f 50 4f 4c 59 52 54 52 49 4d 17 0f 05 00 0f EOPOLYRTRIM..... +| 3680: 23 19 58 45 4e 41 42 4c 45 20 46 54 53 35 42 49 #.XENABLE FTS5BI +| 3696: 4e 41 52 59 17 0e 05 00 0f 23 19 58 55 4e 41 42 NARY.....#.XUNAB +| 3712: 4c 45 20 46 54 53 35 4e 4f 43 41 53 45 16 0d 05 LE FTS5NOCASE... +| 3728: 00 0f 23 17 58 45 4e 41 42 4c 45 20 46 54 53 35 ..#.XENABLE FTS5 +| 3744: 52 54 52 49 4d 17 0c 05 00 0f 23 19 58 45 4e 41 RTRIM.....#.XENA +| 3760: 42 4c 45 20 46 54 53 34 42 49 4e 41 52 59 17 0b BLE FTS4BINARY.. +| 3776: 05 00 0f 23 19 58 45 4e 41 42 4c 45 20 46 54 53 ...#.XENABLE FTS +| 3792: 35 4e 4f 43 40 53 45 16 0a 05 00 0f 23 17 58 45 5NOC@SE.....#.XE +| 3808: 4e 41 42 4c 45 20 46 54 53 34 52 54 52 49 4d 1e NABLE FTS4RTRIM. +| 3824: 09 05 00 0f 31 19 58 45 4e 41 42 4c 35 20 44 42 ....1.XENABL5 DB +| 3840: 53 54 41 54 20 56 54 41 42 42 49 4e 41 52 59 1e STAT VTABBINARY. +| 3856: 08 05 00 0f 31 19 58 45 4e 41 42 4c 45 20 44 42 ....1.XENABLE DB +| 3872: 53 54 41 54 20 56 54 41 42 4e 4f 43 41 53 45 1d STAT VTABNOCASE. +| 3888: 07 05 00 0f 31 17 58 45 4e 41 42 4c 45 20 44 42 ....1.XENABLE DB +| 3904: 53 54 41 54 20 56 54 41 42 52 54 52 49 4d 11 06 STAT VTABRTRIM.. +| 3920: 05 00 0f 17 19 58 44 45 42 55 47 42 49 4e 41 52 .....XDEBUGBINAR +| 3936: 59 11 05 05 00 0f 17 19 58 44 45 42 55 47 4e 4f Y.......XDEBUGNO +| 3952: 43 41 53 45 10 04 05 00 0f 17 17 58 44 45 42 55 CASE.......XDEBU +| 3968: 47 52 54 52 49 4d 27 03 05 00 0f 43 19 58 43 4f GRTRIM'....C.XCO +| 3984: 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e 30 MPILER=gcc-5.4.0 +| 4000: 20 32 30 31 36 30 36 30 39 42 49 4e 41 52 59 27 20160609BINARY' +| 4016: 02 05 00 0f 43 19 58 43 4f 4d 50 49 4c 45 52 3d ....C.XCOMPILER= +| 4032: 67 63 63 2d 35 2e 34 2e 30 20 32 30 31 36 30 36 gcc-5.4.0 201606 +| 4048: 30 39 4e 4f 43 41 53 45 26 01 05 00 0f 43 17 58 09NOCASE&....C.X +| 4064: 43 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 COMPILER=gcc-5.4 +| 4080: 2e 30 20 32 30 31 36 30 36 30 39 52 54 52 49 4d .0 20160609RTRIM +| page 4 offset 12288 +| 0: 0d 00 00 00 00 10 00 00 00 00 00 00 00 00 00 00 ................ +| page 5 offset 16384 +| 0: 0d 00 00 00 03 07 bb 00 0c ad 0b a0 07 bb 00 00 ................ +| 1968: 00 00 00 00 00 00 00 00 00 00 00 87 62 03 08 08 ............b... +| 1984: 01 08 08 17 8f 34 02 30 20 39 38 30 00 01 30 1e .....4.0 980..0. +| 2000: 01 01 01 06 00 01 01 01 06 00 01 01 01 06 00 1f ................ +| 2016: 01 01 03 00 01 01 01 03 00 01 01 01 03 00 00 08 ................ +| 2032: 32 30 31 36 30 36 30 39 0f 01 01 01 07 00 01 01 20160609........ +| 2048: 01 07 00 01 01 01 07 00 00 01 34 0f 01 01 01 05 ..........4..... +| 2064: 00 01 01 01 05 00 01 01 01 05 00 00 01 35 0f 01 .............5.. +| 2080: 01 01 04 00 01 01 01 04 00 01 01 01 04 00 01 07 ................ +| 2096: 30 30 30 30 30 30 30 0f 1c 01 01 04 00 01 01 01 0000000......... +| 2112: 04 00 01 01 01 04 00 00 06 62 69 6e 61 72 79 3c .........binary< +| 2128: 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03 ................ +| 2144: 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01 ................ +| 2160: 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 ................ +| 2176: 02 00 03 01 02 02 00 03 01 02 02 00 00 08 63 6f ..............co +| 2192: 6d 70 69 6c 65 72 0f 01 01 01 02 00 01 01 01 02 mpiler.......... +| 2208: 00 01 01 01 02 00 00 06 64 62 73 74 61 74 0f 07 ........dbstat.. +| 2224: 01 01 03 00 01 01 01 03 00 01 01 01 03 00 01 04 ................ +| 2240: 65 62 75 67 0f 04 01 01 02 00 01 01 01 02 00 01 ebug............ +| 2256: 01 01 02 00 00 03 65 6e 61 05 11 01 01 02 00 03 ......ena....... +| 2272: 03 62 6c 35 05 09 01 01 02 00 05 01 65 5a 07 01 .bl5........eZ.. +| 2288: 01 02 00 01 01 01 02 00 02 01 01 02 00 01 01 01 ................ +| 2304: 02 00 01 01 01 02 00 01 01 01 02 00 02 01 01 02 ................ +| 2320: 00 01 01 01 02 00 02 01 01 02 00 01 01 01 02 00 ................ +| 2336: 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 ................ +| 2352: 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 ................ +| 2368: 01 02 00 01 01 01 02 00 01 08 78 74 65 6e 73 69 ..........xtensi +| 2384: 6f 6e 0f 1f 01 01 04 00 01 01 01 04 00 01 01 01 on.............. +| 2400: 04 00 00 04 66 74 73 34 0a 0a 01 01 03 00 02 01 ....fts4........ +| 2416: 01 03 00 03 01 35 14 0b 01 01 03 00 02 01 01 03 .....5.......... +| 2432: 00 01 01 01 03 00 01 01 01 03 00 00 03 67 63 63 .............gcc +| 2448: 0f 01 01 01 03 00 01 01 01 03 00 01 01 01 03 00 ................ +| 2464: 01 06 65 6f 70 6f 6c 79 0f 10 01 01 03 00 01 01 ..eopoly........ +| 2480: 01 04 00 01 01 01 03 00 00 05 6a 73 6f 6e 31 0f ..........json1. +| 2496: 13 01 01 03 00 01 01 01 03 00 01 01 01 03 00 00 ................ +| 2512: 02 6c 65 05 11 01 01 03 00 01 03 6f 61 64 0f 1f .le........oad.. +| 2528: 01 01 03 00 01 01 01 03 00 01 01 01 03 00 00 03 ................ +| 2544: 6d 61 78 0f 1c 01 01 02 00 01 01 01 02 00 01 01 max............. +| 2560: 01 02 00 01 05 65 6d 6f 72 79 0f 1c 01 01 03 00 .....emory...... +| 2576: 01 01 01 03 00 01 01 01 03 00 03 04 73 79 73 35 ............sys5 +| 2592: 0f 16 01 01 03 00 01 01 01 03 00 01 01 01 03 00 ................ +| 2608: 00 03 6e 6f 63 05 0b 01 02 02 00 03 03 61 73 65 ..noc........ase +| 2624: 37 02 01 02 02 00 03 01 02 02 00 03 01 02 02 00 7............... +| 2640: 06 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03 ................ +| 2656: 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01 ................ +| 2672: 02 02 00 03 01 02 02 00 00 04 6f 6d 69 74 0f 1f ..........omit.. +| 2688: 01 01 02 00 01 01 01 02 00 01 01 01 02 00 00 05 ................ +| 2704: 72 74 72 65 65 0f 19 01 01 03 00 01 01 01 03 00 rtree........... +| 2720: 01 01 01 03 00 03 02 69 6d 3c 01 01 02 02 00 03 .......im<...... +| 2736: 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01 ................ +| 2752: 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 ................ +| 2768: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 2784: 00 03 01 02 02 00 00 02 73 65 05 0b 01 02 03 00 ........se...... +| 2800: 00 0a 74 68 72 65 61 64 73 61 66 65 0f 22 01 01 ..threadsafe.... +| 2816: 02 00 01 01 01 02 00 01 01 01 02 00 00 06 75 6e ..............un +| 2832: 61 62 6c 65 05 0e 01 01 02 00 00 04 76 74 61 62 able........vtab +| 2848: 0f 07 01 01 04 00 01 01 01 04 00 01 01 01 04 00 ................ +| 2864: 00 01 78 6c 01 02 00 01 02 00 01 02 00 01 02 00 ..xl............ +| 2880: 01 02 00 01 02 00 01 02 00 01 02 00 01 02 00 01 ................ +| 2896: 02 00 01 02 00 01 02 00 01 02 00 01 02 00 01 02 ................ +| 2912: 00 01 02 00 01 02 00 01 02 00 01 02 00 01 02 00 ................ +| 2928: 01 02 00 01 02 00 01 02 00 01 02 00 01 02 00 01 ................ +| 2944: 02 00 01 02 00 01 02 00 01 02 00 01 02 00 01 02 ................ +| 2960: 00 01 02 00 01 02 00 01 02 00 01 02 00 01 02 00 ................ +| 2976: 82 0a 02 08 08 09 08 08 17 84 06 30 20 32 35 33 ...........0 253 +| 2992: 00 01 30 04 25 06 1b 00 00 08 32 30 31 36 30 36 ..0.%.....201606 +| 3008: 30 39 03 25 07 00 00 01 34 03 25 05 00 00 01 35 09.%....4.%....5 +| 3024: 03 25 04 00 01 07 30 30 30 30 30 30 30 03 25 1a .%....0000000.%. +| 3040: 00 00 08 63 6f 6d 70 69 6c 65 72 03 25 02 00 00 ...compiler.%... +| 3056: 06 64 62 73 74 61 74 03 25 0a 00 01 04 65 62 75 .dbstat.%....ebu +| 3072: 67 03 25 08 00 00 06 65 6e 61 62 6c 65 09 25 09 g.%....enable.%. +| 3088: 05 04 04 04 04 04 00 01 08 78 74 65 6e 73 69 6f .........xtensio +| 3104: 6e 03 25 1d 00 00 04 66 74 73 34 03 25 0d 00 03 n.%....fts4.%... +| 3120: 11 35 03 25 0f 00 00 03 67 63 63 03 25 03 00 01 .5.%....gcc.%... +| 3136: 06 65 6f 70 6f 6c 79 03 25 11 00 00 05 6a 73 6f .eopoly.%....jso +| 3152: 6e 31 03 25 13 00 00 04 6c 6f 61 64 03 25 1c 00 n1.%....load.%.. +| 3168: 00 03 6d 61 78 03 25 18 00 01 05 65 6d 6f 72 79 ..max.%....emory +| 3184: 03 25 19 00 03 04 73 79 73 35 03 25 15 00 00 04 .%....sys5.%.... +| 3200: 6f 6d 69 74 03 25 1b 00 00 05 72 74 72 65 65 03 omit.%....rtree. +| 3216: 25 17 00 00 0a 74 68 72 65 61 64 73 61 66 65 03 %....threadsafe. +| 3232: 25 1e 00 00 04 76 74 61 62 03 25 0b 00 86 50 01 %....vtab.%...P. +| 3248: 08 08 08 08 08 17 8d 12 30 20 38 33 35 00 01 30 ........0 835..0 +| 3264: 12 00 f6 00 01 06 00 01 06 00 1f 03 00 01 03 00 ................ +| 3280: 01 03 00 00 08 32 30 31 36 30 36 30 39 09 01 07 .....20160609... +| 3296: 00 01 07 00 01 07 00 00 01 34 09 01 05 00 01 05 .........4...... +| 3312: 00 01 05 00 00 01 35 09 01 04 00 01 04 00 01 04 ......5......... +| 3328: 00 01 07 30 30 30 30 30 30 30 09 1c 04 00 01 04 ...0000000...... +| 3344: 00 01 04 00 00 06 62 69 6e 61 72 79 3c 03 01 02 ......binary<... +| 3360: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3376: 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 ................ +| 3392: 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03 ................ +| 3408: 01 02 02 00 03 01 02 02 00 00 08 63 6f 6d 70 69 ...........compi +| 3424: 6c 65 72 09 01 02 00 01 02 00 01 02 00 00 06 64 ler............d +| 3440: 62 73 74 61 74 09 07 03 00 01 03 00 01 03 00 01 bstat........... +| 3456: 04 65 62 75 67 09 04 02 00 01 02 00 01 02 00 00 .ebug........... +| 3472: 06 65 6e 61 62 6c 65 3f 07 02 00 01 02 00 01 01 .enable?........ +| 3488: 00 01 02 00 01 02 00 01 02 00 01 02 00 01 02 00 ................ +| 3504: 01 02 00 01 02 00 01 02 00 01 02 00 01 02 10 01 ................ +| 3520: 02 00 01 02 00 01 02 00 01 02 00 01 02 00 01 02 ................ +| 3536: 00 01 02 00 01 02 00 01 08 78 74 65 6e 73 69 6f .........xtensio +| 3552: 6e 09 1f 04 00 01 04 00 01 04 00 00 04 66 74 73 n............fts +| 3568: 34 09 0a 03 00 01 03 00 01 03 00 03 01 35 09 0d 4............5.. +| 3584: 03 00 01 03 00 01 03 00 00 03 67 63 63 09 01 03 ..........gcc... +| 3600: 00 01 03 00 01 03 00 01 06 65 6f 70 6f 6c 79 09 .........eopoly. +| 3616: 10 03 00 01 03 00 01 03 00 00 05 6a 73 6f 6e 31 ...........json1 +| 3632: 09 13 03 00 01 03 00 01 03 00 00 04 6c 6f 61 64 ............load +| 3648: 09 1f 03 00 01 03 00 01 03 00 00 03 6d 61 78 09 ............max. +| 3664: 1c 02 00 01 02 00 01 02 00 01 05 65 6d 6f 72 79 ...........emory +| 3680: 09 1c 03 00 01 03 00 01 03 00 03 04 73 79 73 35 ............sys5 +| 3696: 09 16 03 00 01 03 00 01 03 00 00 06 6e 6f 63 61 ............noca +| 3712: 73 65 3c 02 01 02 02 00 03 01 02 02 00 03 01 02 se<............. +| 3728: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3744: 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 ................ +| 3760: 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 00 ................ +| 3776: 04 6f 6d 69 74 09 1f 02 00 01 02 00 01 02 00 00 .omit........... +| 3792: 05 72 74 72 65 65 09 19 03 00 01 03 00 01 03 00 .rtree.......... +| 3808: 03 02 69 6d 3c 01 01 02 02 00 03 01 02 02 00 03 ..im<........... +| 3824: 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01 ................ +| 3840: 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 ................ +| 3856: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3872: 00 00 0a 74 68 72 65 61 64 73 61 66 65 09 22 02 ...threadsafe... +| 3888: 00 01 02 00 02 02 00 00 04 76 74 61 62 09 07 04 .........vtab... +| 3904: 00 01 04 00 01 04 00 00 01 78 b4 01 01 01 01 02 .........x...... +| 3920: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| 3936: 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 ................ +| 3952: 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 ................ +| 3968: 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 ................ +| 3984: 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 ................ +| 4000: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| 4016: 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 ................ +| 4032: 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 ................ +| 4048: 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 ................ +| 4064: 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 ................ +| 4080: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 03 0f ee 00 0f fb 0f f5 0f ee 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 06 04 ................ +| 4080: 08 01 01 02 03 05 04 08 09 01 02 04 04 08 08 09 ................ +| page 7 offset 24576 +| 0: 0d 00 00 00 05 0f b8 00 0f f4 0f e9 0f d6 0f c7 ................ +| 16: 0f b8 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 4016: 00 00 00 00 00 00 00 00 0d 05 02 23 61 75 74 6f ...........#auto +| 4032: 6d 65 72 67 65 3d 35 0c f4 02 23 6d 65 72 67 65 merge=5...#merge +| 4048: 3d 31 30 30 2c 38 11 03 02 2b 69 6e 74 65 67 72 =100,8...+integr +| 4064: 69 74 79 2d 63 68 65 63 6b 09 02 02 1b 72 65 62 ity-check....reb +| 4080: 75 69 6c 64 0a 01 02 1d 6f 70 74 69 6d 69 7a 65 uild....optimize +| end c81b.db +}]} {} + +do_catchsql_test 12.1 { + SELECT rowid, quote(matchinfo(t1,'pcxybspcxybs')) + FROM t1 WHERE t1 MATCH 'e*e*' +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +reset_db +do_test 13.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 28672 pagesize 4096 filename crash-c666cfde112dee.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 07 .....@ ........ +| 32: 00 00 00 02 00 00 00 01 00 00 00 07 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 07 00 00 00 01 00 00 00 00 ................ +| 96: 00 00 00 00 0d 0e b1 00 06 0d a4 00 0f 8d 0f 21 ...............! +| 112: 0e b9 0d c8 0e 7e 0d a4 0d a4 00 00 00 00 00 00 .....~.......... +| 3488: 00 00 00 00 22 07 06 17 11 11 01 31 74 61 62 6c ...........1tabl +| 3504: 65 74 32 74 32 07 43 52 45 41 54 45 20 54 41 42 et2t2.CREATE TAB +| 3520: 4c 45 20 74 32 28 78 29 81 33 05 07 17 1f 1f 01 LE t2(x).3...... +| 3536: 82 35 74 61 62 6c 65 74 31 5f 73 65 67 64 69 72 .5tablet1_segdir +| 3552: 74 31 5f 73 65 67 64 69 72 05 43 52 45 41 54 45 t1_segdir.CREATE +| 3568: 20 54 41 42 4c 45 20 27 74 31 5f 73 65 67 64 69 TABLE 't1_segdi +| 3584: 72 27 28 6c 65 76 65 6c 20 49 4e 54 45 47 45 52 r'(level INTEGER +| 3600: 2c 69 64 78 20 49 4e 54 45 47 45 52 2c 73 74 61 ,idx INTEGER,sta +| 3616: 72 74 5f 62 6c 6f 63 6b 20 49 4e 54 45 47 45 52 rt_block INTEGER +| 3632: 2c 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 ,leaves_end_bloc +| 3648: 6b 20 49 4e 54 45 47 45 52 2c 65 6e 64 5f 62 6c k INTEGER,end_bl +| 3664: 6f 63 6b 20 49 4e 54 45 47 45 62 2c 72 6f 6f 74 ock INTEGEb,root +| 3680: 20 42 4c 4f 42 2c 50 52 49 4d 41 52 59 20 4b 45 BLOB,PRIMARY KE +| 3696: 59 28 6c 65 76 65 6c 2c 20 69 64 78 29 29 31 06 Y(level, idx))1. +| 3712: 06 17 45 1f 01 00 69 6e 64 65 78 73 71 6c 69 74 ..E...indexsqlit +| 3728: 65 5f 61 75 74 6f 69 6e 64 65 78 5f 74 31 5f 73 e_autoindex_t1_s +| 3744: 65 67 64 69 72 5f 31 74 31 5f 73 65 67 64 69 72 egdir_1t1_segdir +| 3760: 06 0f c7 00 08 00 00 00 00 66 04 07 17 23 23 01 .........f...##. +| 3776: 81 13 74 61 62 6c 65 74 31 5f 73 65 67 6d 65 6e ..tablet1_segmen +| 3792: 74 73 74 31 5f 73 65 67 6d 65 6e 74 73 04 43 52 tst1_segments.CR +| 3808: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 73 EATE TABLE 't1_s +| 3824: 65 67 6d 65 6e 74 73 27 28 62 6c 6f 63 6b 69 64 egments'(blockid +| 3840: 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 INTEGER PRIMARY +| 3856: 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 KEY, block BLOB +| 3872: 29 6a 03 07 17 21 21 01 81 1f 74 61 62 6c 65 74 )j...!!...tablet +| 3888: 31 5f 63 6f 6e 74 65 6e 74 74 31 5f 63 6f 6e 74 1_contentt1_cont +| 3904: 65 6e 74 03 43 52 45 41 54 45 20 54 41 42 4c 45 ent.CREATE TABLE +| 3920: 20 27 74 31 5f 63 6f 6e 74 65 6e 74 27 28 64 6f 't1_content'(do +| 3936: 63 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d cid INTEGER PRIM +| 3952: 41 52 59 20 4b 45 59 2c 20 27 63 30 61 27 2c 20 ARY KEY, 'c0a', +| 3968: 27 63 31 62 27 2c 20 27 63 32 63 27 29 38 02 06 'c1b', 'c2c')8.. +| 3984: 17 11 11 08 5f 74 61 62 6c 65 74 31 74 31 43 52 ...._tablet1t1CR +| 4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 33 LE t1 USING fts3 +| 4032: 28 61 2c 62 2c 63 29 00 00 00 39 00 00 00 00 00 (a,b,c)...9..... +| page 3 offset 8192 +| 0: 0d 00 00 00 25 0b 48 00 0f d8 0f af 0f 86 0f 74 ....%.H........t +| 16: 0f 61 0f 4e 0f 2f 0f 0f 0e ef 0e d7 0e be 0e a5 .a.N./.......... +| 32: 0e 8d 0e 74 0e 5b 0e 40 0e 24 0e 08 0d ef 0d d5 ...t.[.@.$...... +| 48: 0d bb 0d a0 0d 84 0d 68 0d 4f 0d 35 0d 1b 0c fb .......h.O.5.... +| 64: 0c da 0c b9 0c 99 0c 78 0c 57 0c 3e 0c 24 0c 0a .......x.W.>.$.. +| 80: 0b 48 00 00 00 00 00 00 00 00 00 00 00 00 00 00 .H.............. +| 2880: 00 00 00 00 00 00 00 00 81 3f 25 06 00 82 7f 00 .........?%..... +| 2896: 00 43 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2e .COMPILER=gcc-5. +| 2912: 34 23 00 20 32 30 31 36 30 36 30 39 20 44 45 42 4#. 20160609 DEB +| 2928: 55 47 20 45 4e 41 42 4c 45 20 44 42 53 54 41 54 UG ENABLE DBSTAT +| 2944: 20 56 54 41 42 20 45 4e 41 42 4c 45 20 46 54 53 VTAB ENABLE FTS +| 2960: 34 20 45 4e 41 42 4c 45 20 46 54 53 35 20 45 4e 4 ENABLE FTS5 EN +| 2976: 41 42 4c 45 20 47 45 4f 50 4f 4c 59 20 45 4e 41 ABLE GEOPOLY ENA +| 2992: 42 4c 45 20 4a 53 4f 4e 31 20 45 4e 41 42 4c 45 BLE JSON1 ENABLE +| 3008: 20 4d 45 4d 53 59 53 35 20 45 4e 41 42 4c 45 20 MEMSYS5 ENABLE +| 3024: 52 54 52 45 45 20 4d 41 58 20 4d 45 4d 4f 52 59 RTREE MAX MEMORY +| 3040: 3d 35 30 30 30 30 30 30 30 20 4f 4d 49 54 20 4c =50000000 OMIT L +| 3056: 4f 41 44 20 45 58 54 45 4e 53 49 4f 4e 20 54 48 OAD EXTENSION TH +| 3072: 52 45 41 44 53 41 46 45 3d 30 18 24 05 00 25 0f READSAFE=0.$..%. +| 3088: 19 54 48 52 45 41 44 53 41 46 45 3d 30 58 42 49 .THREADSAFE=0XBI +| 3104: 4e 41 52 59 18 23 05 00 25 0f 19 54 48 52 45 41 NARY.#..%..THREA +| 3120: 44 53 41 46 45 3d 30 58 4e 4f 43 41 53 45 17 22 DSAFE=0XNOCASE.. +| 3136: 05 00 25 0f 17 54 48 52 45 41 44 53 41 46 45 3d ..%..THREADSAFE= +| 3152: 30 58 52 54 52 49 4d 1f 21 05 00 33 0f 19 4f 4d 0XRTRIM.!..3..OM +| 3168: 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 4f IT LOAD EXTENSIO +| 3184: 4e 58 42 49 4e 41 52 59 1f 20 05 00 33 0f 19 4f NXBINARY. ..3..O +| 3200: 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 MIT LOAD EXTENSI +| 3216: 4f 4e 58 4e 4f 43 41 53 45 1e 1f 05 00 33 0f 17 ONXNOCASE....3.. +| 3232: 4f 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 OMIT LOAD EXTENS +| 3248: 49 4f 4e 58 52 54 52 49 4d 1f 1e 05 00 33 0f 19 IONXRTRIM....3.. +| 3264: 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 30 MAX MEMORY=50000 +| 3280: 30 30 30 58 42 49 4e 41 52 59 1f 1d 05 00 33 0f 000XBINARY....3. +| 3296: 19 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 .MAX MEMORY=5000 +| 3312: 30 30 30 30 58 4e 4f 43 41 53 45 1e 1c 05 00 33 0000XNOCASE....3 +| 3328: 0f 17 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 ..MAX MEMORY=500 +| 3344: 30 30 30 30 30 58 52 54 53 49 4d 18 1b 05 00 25 00000XRTSIM....% +| 3360: 0f 19 45 4e 41 42 4c 45 20 52 54 52 45 45 58 42 ..ENABLE RTREEXB +| 3376: 49 4e 41 52 59 18 1a 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3392: 4c 45 20 52 54 52 45 45 58 4e 4f 43 41 53 45 17 LE RTREEXNOCASE. +| 3408: 19 05 00 25 0f 17 45 4e 41 42 4c 45 20 52 54 52 ...%..ENABLE RTR +| 3424: 45 45 58 52 54 52 49 4d 1a 18 05 00 29 0f 19 45 EEXRTRIM....)..E +| 3440: 4e 41 42 4c 45 20 4d 45 4d 53 59 53 35 58 42 49 NABLE MEMSYS5XBI +| 3456: 4e 41 52 59 1a 17 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3472: 45 20 4d 45 4d 53 59 53 35 58 4e 4f 43 41 53 45 E MEMSYS5XNOCASE +| 3488: 19 16 05 00 29 0f 17 45 4e 41 42 4c 45 20 4d 45 ....)..ENABLE ME +| 3504: 4d 53 59 53 35 58 52 54 52 49 4d 18 15 05 00 25 MSYS5XRTRIM....% +| 3520: 0f 19 45 4e 41 42 4c 45 20 4a 53 4f 4e 31 58 42 ..ENABLE JSON1XB +| 3536: 49 4e 41 52 59 18 14 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3552: 4c 45 20 4a 53 4f 4e 31 58 4e 4f 43 41 53 45 17 LE JSON1XNOCASE. +| 3568: 13 05 00 25 0f 17 45 4e 41 52 4c 45 20 4a 53 4f ...%..ENARLE JSO +| 3584: 4e 31 58 52 54 52 49 4d 1a 12 05 00 29 0f 19 45 N1XRTRIM....)..E +| 3600: 4e 41 42 4c 45 20 47 45 4f 50 4f 4c 59 58 42 49 NABLE GEOPOLYXBI +| 3616: 4e 41 52 59 1a 11 05 00 29 0f 19 45 4e 41 1e 4c NARY....)..ENA.L +| 3632: 45 20 47 45 4f 50 4f 4c 59 58 4e 4f 43 41 53 45 E GEOPOLYXNOCASE +| 3648: 19 10 05 00 29 0f 17 45 4e 41 42 4c 45 20 47 45 ....)..ENABLE GE +| 3664: 4f 50 4f 4c 59 58 52 54 52 49 4d 17 0f 05 00 23 OPOLYXRTRIM....# +| 3680: 0f 19 45 4e 41 42 4c 45 20 46 54 53 35 58 42 49 ..ENABLE FTS5XBI +| 3696: 4e 41 52 59 17 0e 05 00 23 0f 19 45 4e 41 42 4c NARY....#..ENABL +| 3712: 45 20 46 54 53 35 58 4e 4f 43 41 53 45 16 0d 05 E FTS5XNOCASE... +| 3728: 00 23 0f 17 45 4e 41 42 4c 45 20 46 54 59 e5 58 .#..ENABLE FTY.X +| 3744: 52 54 52 49 4d 17 0c 05 00 23 0f 19 45 4e 41 42 RTRIM....#..ENAB +| 3760: 4c 45 20 46 54 53 34 58 42 49 4e 41 52 59 17 0b LE FTS4XBINARY.. +| 3776: 05 00 23 0f 19 45 4e 41 42 4c 45 20 46 54 53 35 ..#..ENABLE FTS5 +| 3792: 58 4e 4f 43 40 53 45 16 0a 05 00 23 0f 17 45 4e XNOC@SE....#..EN +| 3808: 41 42 4c 45 20 46 54 53 34 58 52 54 52 49 4d 1e ABLE FTS4XRTRIM. +| 3824: 09 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3840: 54 41 54 20 56 54 41 42 58 42 49 4e 41 52 59 1e TAT VTABXBINARY. +| 3856: 08 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3872: 54 41 54 20 56 54 41 42 58 4e 4f 43 41 53 45 1d TAT VTABXNOCASE. +| 3888: 07 05 00 31 0f 17 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3904: 54 41 54 20 56 54 41 42 58 52 54 52 49 4d 11 06 TAT VTABXRTRIM.. +| 3920: 05 00 17 0f 19 44 45 42 55 47 58 42 49 4e 41 52 .....DEBUGXBINAR +| 3936: 59 11 05 05 00 17 0f 19 44 45 42 55 47 58 4e 4f Y.......DEBUGXNO +| 3952: 43 41 53 45 10 04 05 00 17 0f 17 44 45 42 55 47 CASE.......DEBUG +| 3968: 58 52 54 52 49 4d 27 03 05 00 43 0f 19 43 4f 4d XRTRIM'...C..COM +| 3984: 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e 30 20 PILER=gcc-5.4.0 +| 4000: 32 30 31 36 30 36 30 39 58 42 49 4e 41 52 59 27 20160609XBINARY' +| 4016: 02 05 00 43 0f 19 43 4f 4d 50 49 4c 45 52 3d 67 ...C..COMPILER=g +| 4032: 63 63 2d 35 2e 34 2f 30 20 32 30 31 36 30 36 30 cc-5.4/0 2016060 +| 4048: 39 58 4e 4f 43 41 53 45 26 01 05 00 43 0f 17 43 9XNOCASE&...C..C +| 4064: 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e OMPILER=gcc-5.4. +| 4080: 30 20 32 30 31 36 30 36 30 39 58 52 54 52 49 4d 0 20160609XRTRIM +| page 4 offset 12288 +| 0: 0d 00 00 00 00 10 00 00 00 00 00 00 00 00 00 00 ................ +| page 5 offset 16384 +| 0: 0d 00 00 00 02 0b a0 00 0c ad 0b a0 00 00 00 00 ................ +| 2976: 82 0a 02 08 08 09 08 08 17 84 06 30 20 32 35 33 ...........0 253 +| 2992: 00 01 30 04 25 06 1b 00 00 08 32 30 31 36 30 36 ..0.%.....201606 +| 3008: 30 39 03 25 07 00 00 01 34 03 25 05 00 00 01 35 09.%....4.%....5 +| 3024: 03 25 04 00 01 07 30 30 30 30 30 30 30 03 25 1a .%....0000000.%. +| 3040: 00 00 08 63 6f 6d 70 69 6c 65 72 03 25 02 00 00 ...compiler.%... +| 3056: 06 64 62 73 74 61 74 03 25 0a 00 01 04 65 62 75 .dbstat.%....ebu +| 3072: 67 03 25 08 00 00 06 65 6e 61 62 6c 65 09 25 09 g.%....enable.%. +| 3088: 05 04 04 04 04 04 00 01 08 78 74 65 6e 73 69 6f .........xtensio +| 3104: 6e 03 25 1d 00 00 04 66 74 73 34 03 25 0d 00 03 n.%....fts4.%... +| 3120: 01 35 03 25 0f 00 00 03 67 63 63 03 25 03 00 01 .5.%....gcc.%... +| 3136: 06 65 6f 70 6f 6c 79 03 25 11 00 00 05 6a 73 6f .eopoly.%....jso +| 3152: 6e 31 03 25 13 41 00 04 6c 6f 61 64 03 25 1c 00 n1.%.A..load.%.. +| 3168: 00 03 6d 61 78 03 25 18 00 01 05 65 6d 6f 72 79 ..max.%....emory +| 3184: 03 25 19 00 03 04 73 79 73 35 03 25 15 00 00 04 .%....sys5.%.... +| 3200: 6f 6d 69 74 03 25 1b 00 00 05 72 74 72 65 65 03 omit.%....rtree. +| 3216: 25 17 00 00 0a 74 68 72 65 61 64 73 61 66 65 03 %....threadsafe. +| 3232: 25 1e 00 00 04 76 74 61 62 03 25 0b 00 86 50 01 %....vtab.%...P. +| 3248: 08 08 08 08 08 17 8d 12 30 20 38 33 35 00 01 30 ........0 835..0 +| 3264: 12 01 06 00 01 06 00 01 06 00 1f 03 00 01 03 00 ................ +| 3280: 01 03 00 00 08 32 30 31 36 30 36 30 39 09 01 07 .....20160609... +| 3296: 00 01 07 00 01 07 00 00 01 34 09 01 05 00 01 05 .........4...... +| 3312: 00 01 05 00 00 01 35 09 01 04 00 01 04 00 01 04 ......5......... +| 3328: 00 01 07 30 30 30 30 30 30 30 09 1c 04 00 01 04 ...0000000...... +| 3344: 00 01 04 00 00 06 62 69 6e 61 72 79 3c 03 02 02 ......binary<... +| 3360: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3376: 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 ................ +| 3392: 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03 ................ +| 3408: 01 02 02 00 03 01 02 02 00 00 08 63 6f 6d 70 69 ...........compi +| 3424: 6c 65 72 09 01 02 00 01 02 00 01 02 00 00 06 64 ler............d +| 3440: 62 73 74 61 74 09 f2 03 00 01 03 00 01 03 00 01 bstat........... +| 3456: 04 65 62 75 67 09 04 02 00 01 02 00 01 02 00 00 .ebug........... +| 3472: 06 65 6e 61 62 6c 65 3f 07 02 00 01 02 00 01 02 .enable?........ +| 3488: 00 01 02 00 01 02 00 01 02 00 01 02 00 57 02 00 .............W.. +| 3504: 01 02 00 01 01 00 01 02 00 01 02 00 01 02 10 01 ................ +| 3520: 02 00 01 02 00 01 02 00 01 02 00 01 02 00 01 02 ................ +| 3536: 00 01 02 00 01 02 00 01 08 78 74 64 6e 73 69 6f .........xtdnsio +| 3552: 6e 09 1f 04 00 01 04 00 01 03 00 00 04 66 74 73 n............fts +| 3568: 34 09 0a 03 00 01 03 00 01 03 00 03 01 35 09 0d 4............5.. +| 3584: 03 00 01 03 00 01 03 00 00 03 57 63 63 09 01 03 ..........Wcc... +| 3600: 00 01 03 00 01 03 00 01 06 65 6f 70 6f 6c 79 09 .........eopoly. +| 3616: 10 03 00 01 03 00 01 03 00 00 05 6a 73 6f 6e 31 ...........json1 +| 3632: 09 13 03 00 01 03 00 01 03 00 00 04 6c 6f 61 64 ............load +| 3648: 09 1f 03 00 01 03 00 01 03 00 00 03 6d 61 78 09 ............max. +| 3664: 1c 02 00 01 02 00 01 02 00 01 05 65 6d 6f 72 79 ...........emory +| 3680: 09 1c 03 00 01 03 00 01 03 00 03 04 73 79 73 35 ............sys5 +| 3696: 09 16 03 00 01 03 00 01 03 00 00 06 6e 6f 63 61 ............noca +| 3712: 73 65 3c 02 01 02 02 00 03 01 02 02 00 03 01 02 se<............. +| 3728: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3744: 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 ................ +| 3760: 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 00 ................ +| 3776: 04 6f 6d 69 74 09 1f 02 00 01 02 00 01 02 00 00 .omit........... +| 3792: 05 72 74 72 65 65 09 19 03 00 01 03 00 01 03 00 .rtree.......... +| 3808: 03 02 69 6d 3c 01 01 02 02 00 03 01 02 02 00 03 ..im<........... +| 3824: 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01 ................ +| 3840: 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 ................ +| 3856: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3872: 00 00 0a 74 68 72 65 61 64 73 61 66 65 09 22 02 ...threadsafe... +| 3888: 00 01 02 00 02 02 00 00 04 76 74 61 62 09 07 04 .........vtab... +| 3904: 00 01 04 00 01 04 00 00 01 78 b4 01 01 01 01 02 .........x...... +| 3920: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| 3936: 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 ................ +| 3952: 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 ................ +| 3968: 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 ................ +| 3984: 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 ................ +| 4000: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| 4016: 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 ................ +| 4032: 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 ................ +| 4048: 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 ................ +| 4064: 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 ................ +| 4080: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 02 0f f5 00 0f fb 0f f5 00 00 00 00 ................ +| 4080: 00 00 00 00 00 05 04 08 09 01 02 04 04 08 08 09 ................ +| page 7 offset 24576 +| 0: 0d 00 00 00 05 0f b8 00 0f f4 0f e9 0f d6 0f c7 ................ +| 16: 0f b8 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 4016: 00 00 00 00 00 00 00 00 0d 05 02 23 61 75 74 6f ...........#auto +| 4032: 6d 65 72 67 65 3d 35 0d 04 02 23 6d 65 72 67 65 merge=5...#merge +| 4048: 3d 31 30 30 2c 38 11 03 02 2b 69 6e 74 65 67 72 =100,8...+integr +| 4064: 69 74 79 2d 63 68 65 63 6b 09 02 02 1b 72 65 62 ity-check....reb +| 4080: 75 69 6c 64 0a 01 02 1d 6f 70 74 69 6d 69 7a 65 uild....optimize +| end crash-c666cfde112dee.db +}]} {} + +do_catchsql_test 13.1 { + SELECT quote(matchinfo(t1,'pcxybs'))==0 FROM t1 WHERE b MATCH 'e*'; +} {0 {}} + +#------------------------------------------------------------------------- +reset_db +do_test 14.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 28672 pagesize 4096 filename crash-f7b636a855e1d2.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 07 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 06 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 96: 00 00 00 00 0d 0e ef 00 07 0d 4d 00 0f bd 0f 5f ..........M...._ +| 112: 0e f7 0e 06 0e bc 0d a4 0d 4d 00 00 00 00 00 00 .........M...... +| 3392: 00 00 00 00 00 00 00 00 00 00 00 00 00 55 07 07 .............U.. +| 3408: 17 1b 1b 01 81 01 74 61 62 6c 65 74 31 5f 73 74 ......tablet1_st +| 3424: 61 74 74 31 5f 73 74 61 74 07 43 52 45 41 54 45 att1_stat.CREATE +| 3440: 20 54 41 42 4c 45 20 27 74 31 5f 73 74 61 74 27 TABLE 't1_stat' +| 3456: 28 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d (id INTEGER PRIM +| 3472: 41 52 59 20 4b 45 59 2c 20 76 61 6c 75 65 20 42 ARY KEY, value B +| 3488: 4c 4f 42 29 60 06 07 17 21 21 01 81 0b 74 61 62 LOB)`...!!...tab +| 3504: 6c 65 74 31 5f 64 6f 63 73 69 7a 65 74 31 5f 64 let1_docsizet1_d +| 3520: 6f 63 73 69 7a 65 06 43 52 45 41 54 45 20 54 41 ocsize.CREATE TA +| 3536: 42 4c 45 20 27 74 31 5f 64 6f 63 73 69 7a 65 27 BLE 't1_docsize' +| 3552: 28 64 6f 63 69 64 20 49 4e 54 45 47 45 52 20 50 (docid INTEGER P +| 3568: 52 49 4d 41 52 59 20 4b 45 59 2c 20 73 69 7a 65 RIMARY KEY, size +| 3584: 20 42 4c 4f 42 29 81 33 04 07 17 1f 1f 01 82 35 BLOB).3.......5 +| 3600: 74 61 62 6c 65 74 31 5f 73 65 67 64 69 72 74 31 tablet1_segdirt1 +| 3616: 5f 73 65 67 64 69 25 04 43 52 45 41 54 45 20 54 _segdi%.CREATE T +| 3632: 41 42 4c 45 20 27 74 31 5f 73 65 67 64 69 72 27 ABLE 't1_segdir' +| 3648: 28 6c 65 76 65 6c 20 49 4e 54 45 47 45 52 2c 69 (level INTEGER,i +| 3664: 64 78 20 49 4e 54 45 47 45 52 2c 73 74 61 72 74 dx INTEGER,start +| 3680: 5f 62 6c 6f 63 6b 20 49 4e 54 45 47 45 52 2c 6c _block INTEGER,l +| 3696: 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 6b 20 eaves_end_block +| 3712: 49 4e 54 45 47 45 52 2c 65 6e 64 5f 62 6c 6f 63 INTEGER,end_bloc +| 3728: 6b 20 49 4e 54 45 47 45 52 2c 72 6f 6f 74 20 42 k INTEGER,root B +| 3744: 4c 4f 42 2c 50 52 49 4d 41 52 59 20 4b 45 59 28 LOB,PRIMARY KEY( +| 3760: 6c 65 76 65 6c 2c 20 69 64 78 29 29 31 05 06 17 level, idx))1... +| 3776: 45 1f 01 00 69 6e 64 65 78 73 71 6c 69 74 65 5f E...indexsqlite_ +| 3792: 61 75 74 6f 69 6e 64 65 78 5f 74 31 5f 73 65 67 autoindex_t1_seg +| 3808: 64 69 72 5f 31 74 31 5f 73 65 67 64 69 72 05 00 dir_1t1_segdir.. +| 3824: 00 00 08 00 00 00 00 66 03 07 17 23 23 01 81 13 .......f...##... +| 3840: 74 61 62 6c 65 74 31 5f 73 65 67 6d 65 6e 74 73 tablet1_segments +| 3856: 74 31 5f 73 65 67 6d 65 6e 74 73 03 43 52 45 41 t1_segments.CREA +| 3872: 54 45 20 54 41 42 4c 45 20 27 74 31 5f 73 65 67 TE TABLE 't1_seg +| 3888: 6d 65 6e 74 73 27 28 62 6c 6f 63 6b 69 64 20 49 ments'(blockid I +| 3904: 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b NTEGER PRIMARY K +| 3920: 45 59 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 29 5c EY, block BLOB). +| 3936: 02 07 17 21 21 01 81 03 74 61 62 6c 65 74 31 5f ...!!...tablet1_ +| 3952: 63 6f 6e 74 65 6e 74 74 31 5f 63 6f 6e 74 65 6e contentt1_conten +| 3968: 74 02 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 t.CREATE TABLE ' +| 3984: 74 31 5f 63 6f 6e 74 65 6e 74 27 28 64 6f 63 69 t1_content'(doci +| 4000: 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 d INTEGER PRIMAR +| 4016: 59 20 4b 45 59 2c 20 27 63 30 61 27 29 41 01 06 Y KEY, 'c0a')A.. +| 4032: 17 11 11 08 71 74 61 62 6c 65 74 31 74 31 43 52 ....qtablet1t1CR +| 4048: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4064: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 34 LE t1 USING fts4 +| 4080: 28 61 2c 70 72 65 66 69 78 3d 27 31 2c 32 27 29 (a,prefix='1,2') +| page 2 offset 4096 +| 0: 0d 00 00 00 08 0e 1f 00 0f c4 0f 7c 0f 34 0f 07 ...........|.4.. +| 16: 0e c3 0e 97 0e 63 0e 1f 00 00 00 00 00 00 00 00 .....c.......... +| 3600: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 42 ...............B +| 3616: 08 04 00 81 09 73 75 6e 74 20 69 6e 20 63 75 6c .....sunt in cul +| 3632: 70 61 20 71 75 69 20 6f 66 66 69 63 69 61 20 64 pa qui officia d +| 3648: 65 73 65 72 75 6e 74 20 6d 6f 6c 6c 69 74 20 61 eserunt mollit a +| 3664: 6e 69 6d 20 69 64 20 65 73 74 20 6c 61 62 6f 72 nim id est labor +| 3680: 75 6d 2e 32 07 03 01 6b 45 78 63 65 70 74 65 75 um.2...kExcepteu +| 3696: 72 20 73 69 6e 74 20 6f 63 63 61 65 63 61 74 20 r sint occaecat +| 3712: 63 75 70 69 64 61 74 61 74 20 6e 6f 6e 20 70 72 cupidatat non pr +| 3728: 6f 69 64 65 6e 74 2c 2a 06 03 00 5b 63 69 6c 6c oident,*...[cill +| 3744: 75 6d 20 64 6f 6c 6f 72 65 20 65 75 20 66 75 67 um dolore eu fug +| 3760: 69 61 74 20 6e 75 6c 6c 61 20 70 61 72 69 61 74 iat nulla pariat +| 3776: 75 72 2e 43 05 04 00 81 09 44 75 69 73 20 61 75 ur.C.....Duis au +| 3792: 74 65 20 69 72 75 72 65 20 64 6f 6c 6f 72 20 69 te irure dolor i +| 3808: 6e 20 72 65 70 72 65 68 65 6e 64 65 72 69 74 20 n reprehenderit +| 3824: 69 6e 20 76 6f 6c 75 70 74 61 74 65 20 76 65 6c in voluptate vel +| 3840: 69 74 20 65 73 73 65 2b 14 03 00 5d 6e 69 73 69 it esse+...]nisi +| 3856: 20 75 74 20 61 6c 69 71 75 69 70 20 65 78 20 65 ut aliquip ex e +| 3872: 61 20 63 6f 6d 6d 6f 64 6f 20 63 6f 6e 73 65 71 a commodo conseq +| 3888: 75 61 74 2e 46 03 04 00 81 11 55 74 20 65 6e 69 uat.F.....Ut eni +| 3904: 6d 20 61 64 20 6d 69 6e 69 6d 20 76 65 6e 69 61 m ad minim venia +| 3920: 6d 2c 20 71 75 69 73 20 6e 6f 73 74 72 75 64 20 m, quis nostrud +| 3936: 65 78 65 72 63 69 74 61 74 69 6f 6e 20 75 6c 6c exercitation ull +| 3952: 61 6d 63 6f 20 6c 61 62 6f 72 69 73 46 02 04 00 amco laborisF... +| 3968: 81 11 73 65 64 20 64 6f 20 65 69 75 73 6d 6f 64 ..sed do eiusmod +| 3984: 20 74 65 6d 70 6f 72 20 69 6e 63 69 64 69 64 75 tempor incididu +| 4000: 6e 74 20 75 74 20 6c 61 62 6f 72 65 20 65 74 20 nt ut labore et +| 4016: 64 6f 6c 6f 72 65 20 6d 61 67 6e 61 20 61 6c 69 dolore magna ali +| 4032: 71 75 61 2e 3a 01 03 00 7b 4c 6f 72 65 6d 20 69 qua.:....Lorem i +| 4048: 70 73 75 6d 20 64 6f 6c 6f 72 20 73 69 74 20 61 psum dolor sit a +| 4064: 6d 65 74 2c 20 63 6f 6e 73 65 63 74 65 74 75 72 met, consectetur +| 4080: 20 61 64 69 70 69 73 63 69 6e 67 20 65 6c 69 74 adipiscing elit +| page 3 offset 8192 +| 0: 0d 00 00 00 00 10 00 00 00 00 00 00 00 00 00 00 ................ +| page 4 offset 12288 +| 0: 0d 00 00 00 03 0a a6 00 0d 57 0c 4a 0a a6 00 00 .........W.J.... +| 2720: 00 00 00 00 00 00 83 21 03 08 02 08 08 08 17 86 .......!........ +| 2736: 30 08 00 30 20 34 30 32 00 02 61 64 06 01 08 00 0..0 402..ad.... +| 2752: 02 04 00 01 01 6c 06 02 0c 00 02 04 00 01 01 6d .....l.........m +| 2768: 03 01 06 00 01 01 6e 03 08 09 00 01 01 75 03 05 ......n......u.. +| 2784: 03 00 00 02 63 69 03 06 01 00 01 01 6f 07 01 07 ....ci......o... +| 2800: 00 03 07 03 00 01 01 75 06 07 05 00 01 04 00 00 .......u........ +| 2816: 02 64 65 03 08 07 00 01 01 6f 0d 01 04 00 01 03 .de......o...... +| 2832: 09 00 03 05 00 01 03 00 01 01 75 03 05 02 00 00 ..........u..... +| 2848: 02 65 61 03 04 06 00 01 01 69 03 02 04 00 01 01 .ea......i...... +| 2864: 6c 03 01 09 00 01 01 6e 03 03 03 00 01 01 73 06 l......n......s. +| 2880: 05 0b 00 03 0b 00 01 01 74 03 02 09 00 01 01 75 ........t......u +| 2896: 03 06 04 00 01 01 78 09 03 09 00 01 05 00 03 02 ......x......... +| 2912: 00 00 02 66 75 03 06 05 00 00 02 69 64 03 08 0a ...fu......id... +| 2928: 00 01 01 6e 0a 02 06 00 03 06 04 00 03 03 00 01 ...n............ +| 2944: 01 70 03 01 13 00 01 01 72 03 05 04 00 00 02 6c .p......r......l +| 2960: 61 09 02 08 00 01 0b 00 05 0c 00 01 01 6f 03 01 a............o.. +| 2976: 02 00 00 02 6d 61 03 02 0b 00 01 01 69 03 04 05 ....ma......i... +| 2992: 00 01 01 6f 03 08 08 00 00 02 6e 69 03 04 02 00 ...o......ni.... +| 3008: 01 01 6f 06 03 08 00 04 06 00 01 01 75 03 06 06 ..o.........u... +| 3024: 00 00 02 6f 63 03 07 04 00 01 01 66 03 08 06 00 ...oc......f.... +| 3040: 00 02 70 61 03 06 07 00 01 01 72 03 07 07 00 00 ..pa......r..... +| 3056: 02 71 75 06 03 07 00 05 05 00 00 02 72 65 03 05 .qu.........re.. +| 3072: 07 00 00 02 73 65 03 02 02 00 01 01 69 06 01 05 ....se......i... +| 3088: 00 06 03 00 01 01 75 03 08 02 00 00 02 74 65 03 ......u......te. +| 3104: 03 05 00 00 02 75 6c 03 03 0a 00 01 01 74 09 02 .....ul......t.. +| 3120: 07 00 01 02 00 01 03 00 00 02 76 65 06 03 06 00 ..........ve.... +| 3136: 02 0a 00 01 01 6f 03 05 09 00 82 0a 02 08 02 08 .....o.......... +| 3152: 08 08 17 84 02 04 00 30 20 32 35 31 00 01 61 13 .......0 251..a. +| 3168: 01 06 04 00 01 0c 00 01 04 00 01 04 00 01 03 00 ................ +| 3184: 03 09 00 00 01 63 10 01 07 00 03 07 03 00 02 02 .....c.......... +| 3200: 00 01 05 00 01 04 00 00 01 64 11 01 04 00 01 03 .........d...... +| 3216: 09 00 03 02 05 00 01 03 00 02 07 00 00 01 65 1b ..............e. +| 3232: 01 09 00 01 04 07 00 01 03 08 00 01 05 03 00 01 ................ +| 3248: 0b 00 01 04 00 01 02 00 01 0b 00 00 01 66 03 06 .............f.. +| 3264: 05 00 00 01 69 0f 01 03 00 01 06 00 03 04 04 04 ....i........... +| 3280: 00 03 03 09 00 00 01 6c 0c 01 02 00 01 08 00 01 .......l........ +| 3296: 0b 00 05 0c 00 00 01 6d 09 02 0b 00 01 05 00 05 .......m........ +| 3312: 08 00 00 01 6e 0c 03 08 00 01 02 00 02 06 00 01 ....n........... +| 3328: 06 00 00 01 6f 06 07 04 00 01 06 00 00 01 70 06 ....o.........p. +| 3344: 06 07 00 01 07 00 00 01 71 06 03 07 00 05 05 00 ........q....... +| 3360: 00 01 72 03 05 08 00 00 01 73 0c 01 05 00 01 02 ..r......s...... +| 3376: 00 05 03 00 01 02 00 00 01 74 03 02 05 00 00 01 .........t...... +| 3392: 75 0a 02 07 00 01 02 0a 00 01 03 00 00 01 76 07 u.............v. +| 3408: 03 06 00 02 09 03 00 85 26 01 08 08 08 08 08 17 ........&....... +| 3424: 8a 3e 30 20 36 36 35 00 02 61 64 03 03 04 00 02 .>0 665..ad..... +| 3440: 08 69 70 69 73 63 69 6e 67 03 01 08 00 01 05 6c .ipiscing......l +| 3456: 69 71 75 61 03 02 0c 00 05 02 69 70 03 04 04 00 iqua......ip.... +| 3472: 01 03 6d 65 74 03 01 06 00 01 03 6e 69 6d 03 08 ..met......nim.. +| 3488: 09 00 01 03 75 74 65 03 05 03 00 00 06 63 69 6c ....ute......cil +| 3504: 6c 75 6d 03 06 02 00 01 06 6f 6d 6d 6f 64 6f 03 lum......ommodo. +| 3520: 04 07 00 02 09 6e 73 65 63 74 65 74 75 72 03 01 .....nsectetur.. +| 3536: 07 00 05 04 71 75 61 74 03 04 08 00 01 04 75 6c ....quat......ul +| 3552: 70 61 03 08 04 00 02 07 70 69 64 61 74 61 74 03 pa......pidatat. +| 3568: 07 05 00 00 08 64 65 73 65 72 75 6e 74 03 08 07 .....deserunt... +| 3584: 00 01 01 6f 03 02 03 00 02 03 6c 6f 72 06 01 04 ...o......lor... +| 3600: 00 04 05 00 05 01 65 06 02 0a 00 04 03 00 01 03 ......e......... +| 3616: 75 69 73 03 05 02 00 00 02 65 61 03 04 06 00 01 uis......ea..... +| 3632: 06 69 75 73 6d 6f 64 03 02 04 00 01 03 6c 69 74 .iusmod......lit +| 3648: 03 01 09 00 01 03 6e 69 6d 03 03 03 00 01 03 73 ......nim......s +| 3664: 73 65 03 05 0b 00 02 01 73 03 08 0b 00 01 01 74 se......s......t +| 3680: 03 02 09 00 01 01 75 03 06 04 00 01 01 78 03 04 ......u......x.. +| 3696: 05 00 02 07 63 65 70 74 65 75 72 03 07 02 00 02 ....cepteur..... +| 3712: 0a 65 72 63 69 74 61 74 69 6f 6e 03 03 09 00 00 .ercitation..... +| 3728: 06 66 75 67 69 61 74 03 06 05 00 00 02 69 64 03 .fugiat......id. +| 3744: 08 0a 00 01 01 6e 07 05 06 04 00 03 03 00 02 08 .....n.......... +| 3760: 63 69 64 69 64 75 6e 74 03 02 06 00 01 04 70 73 cididunt......ps +| 3776: 75 6d 03 01 03 00 01 04 72 75 72 65 03 05 04 00 um......rure.... +| 3792: 00 06 6c 61 62 6f 72 65 03 02 08 00 05 02 69 73 ..labore......is +| 3808: 03 03 0b 00 05 02 75 6d 03 08 0c 00 01 04 6f 72 ......um......or +| 3824: 65 6d 03 01 02 00 00 05 6d 61 67 6e 61 03 02 0b em......magna... +| 3840: 00 01 04 69 6e 69 6d 03 03 05 00 01 05 6f 6c 6c ...inim......oll +| 3856: 69 74 03 08 08 00 00 04 6e 69 73 69 03 04 02 00 it......nisi.... +| 3872: 01 02 6f 6e 03 07 06 00 02 05 73 74 72 75 64 03 ..on......strud. +| 3888: 03 08 00 01 04 75 6c 6c 61 03 06 06 00 00 08 6f .....ulla......o +| 3904: 63 63 61 65 63 61 74 03 07 04 00 01 06 66 66 69 ccaecat......ffi +| 3920: 63 69 61 03 08 06 00 00 08 70 61 72 69 61 74 75 cia......pariatu +| 3936: 72 03 06 07 00 01 07 72 6f 69 64 65 6e 74 03 07 r......roident.. +| 3952: 07 00 00 03 71 75 69 03 08 05 00 03 01 73 03 03 ....qui......s.. +| 3968: 07 00 00 0d 72 65 70 72 65 68 65 6e 64 65 72 69 ....reprehenderi +| 3984: 74 03 05 07 00 00 03 73 65 64 03 02 02 00 01 03 t......sed...... +| 4000: 69 6e 74 03 07 03 00 02 01 74 03 01 05 00 01 03 int......t...... +| 4016: 75 6e 74 03 08 02 00 00 06 74 65 6d 70 6f 72 03 unt......tempor. +| 4032: 02 05 00 00 07 75 6c 6c 61 6d 63 6f 03 03 0a 00 .....ullamco.... +| 4048: 01 01 74 09 02 07 00 01 02 00 01 03 00 00 05 76 ..t............v +| 4064: 65 6c 69 74 03 05 0a 00 02 04 6e 69 61 6d 03 03 elit......niam.. +| 4080: 06 00 01 08 6f 6c 75 70 74 61 74 65 03 05 09 00 ....oluptate.... +| page 5 offset 16384 +| 0: 0a 00 00 00 03 0f eb 00 0f fb 0f f3 0f eb 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 07 04 02 08 01 ................ +| 4080: 08 00 03 07 04 02 08 01 04 00 02 04 04 08 08 09 ................ +| page 6 offset 20480 +| 0: 0d 00 00 00 08 0f d0 00 0f fa 0f f4 0f ee 0f e8 ................ +| 16: 0f e2 0f dc 0f d6 0f d0 00 00 00 00 00 00 00 00 ................ +| 4048: 04 08 03 00 0e 0b 04 07 03 00 0e 06 04 06 03 00 ................ +| 4064: 0e 06 04 05 03 00 0e 0a 04 04 03 00 0e 07 04 03 ................ +| 4080: 03 00 0e 0a 04 02 03 00 0e 0b 04 01 03 00 0e 08 ................ +| page 7 offset 24576 +| 0: 0d 00 00 00 01 0f f7 00 0f f7 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 07 00 03 00 14 08 45 b5 03 .............E.. +| end crash-f7b636a855e1d2.db +}]} {} + +do_execsql_test 14.1 { + PRAGMA writable_schema = 1; + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<10) + INSERT INTO t1(a) SELECT randomblob(3000) FROM c; +} + +do_catchsql_test 14.2 { + INSERT INTO t1(t1) VALUES('optimize'); +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 15.0 { + CREATE VIRTUAL TABLE t1 USING fts3(a, content=""); + INSERT INTO t1_segdir VALUES(0,0,0,0,'0 665',X'000261640303040002086970697363696e670301080001056c6971756103020c00050269700304040001036d65740301060001036e6a6d03080900010375746503050300000663696c6c756d0306020001066f6d6d6f646f0304070002096e736563746574757203010700050471756174030408000104756c7061030804000207706964617461740307050000086465736572756e740308070001016f0302030002036c6f720601040004050005016506020a00040300010375697303050200000265610304060001066975736d6f640302040001036c69740301090001036e696d13030300010373736503050b0002017403080b0001017403020900010175030604000101780304050002076365707465757203070100020a65726369746174696f6e030309000006667567696174030605000002696403080a0001016e070506040003030002086369646964756e740302060001047073756d030103000104727572650305040000066c61626f7265030208000502697303030b000502756d03080c0001046f72656d0301020000056d61676e6103020b000104696e696d0303050001056f6c6c69740308080000046e6973690304020001026f6e0307060002057374727564030308000104756c6c610306060000086f636361656361740307040001066666696369610308060000087061726961747572030607000107726f6964656e740307070000037175690308050003017303030700000d726570726568656e6465726974030507000003736564030202000103696e7403070300020174030105000103756e7403080200000674656d706f72030205000007756c6c616d636f03030a0001017409020700010200010300000576656c697403050a0002046e69616d0303060001086f6c75707461746503050900'); +} + +do_execsql_test 15.1 { + SELECT quote(matchinfo(t1, t1 ))==0 FROM t1 WHERE t1 MATCH 'e*'; +} {0 0 0 0 0 0} + +#------------------------------------------------------------------------- +reset_db +do_test 16.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 28672 pagesize 4096 filename crash-de7e8cb026385a.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 07 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 06 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 96: 00 00 00 00 0d 0e ef 00 07 0d 4d 00 0f bd 0f 5f ..........M...._ +| 112: 0e f7 0e 06 0e bc 0d a4 0d 4d 00 00 00 00 00 00 .........M...... +| 3392: 00 00 00 00 00 00 00 00 00 00 00 00 00 55 07 07 .............U.. +| 3408: 17 1b 1b 01 81 01 74 61 62 6c 65 74 31 5f 73 74 ......tablet1_st +| 3424: 61 74 74 31 5f 73 74 61 74 07 43 52 45 41 54 45 att1_stat.CREATE +| 3440: 20 54 41 42 4c 45 20 27 74 31 5f 73 74 61 74 27 TABLE 't1_stat' +| 3456: 28 69 64 20 49 af 54 45 47 45 52 20 50 52 49 4d (id I.TEGER PRIM +| 3472: 41 52 59 20 4b 45 59 2c 20 76 61 6c 75 65 20 42 ARY KEY, value B +| 3488: 4c 4f 42 29 60 06 07 17 21 21 01 81 0b 74 61 62 LOB)`...!!...tab +| 3504: 6c 65 74 31 5f 64 6f 63 73 69 7a 65 74 31 5f 64 let1_docsizet1_d +| 3520: 6f 63 73 69 7a 65 06 43 52 45 41 54 45 20 54 41 ocsize.CREATE TA +| 3536: 42 4c 45 20 27 74 31 5f 64 6f 63 73 69 7a 65 27 BLE 't1_docsize' +| 3552: 28 64 6f 63 69 64 20 49 4e 54 45 47 45 52 20 50 (docid INTEGER P +| 3568: 52 49 4d 41 52 59 20 4b 45 59 2c 20 73 69 7a 65 RIMARY KEY, size +| 3584: 20 42 4c 4f 42 29 81 33 04 07 17 1f 1f 01 82 35 BLOB).3.......5 +| 3600: 74 61 62 6c 65 74 31 5f 73 65 67 64 69 72 74 31 tablet1_segdirt1 +| 3616: 5f 73 65 67 64 69 72 04 43 52 45 41 54 45 20 54 _segdir.CREATE T +| 3632: 41 42 4c 45 20 27 74 31 5f 73 65 67 64 69 72 27 ABLE 't1_segdir' +| 3648: 28 6c 65 76 65 6c 20 49 4e 54 45 47 45 52 2c 69 (level INTEGER,i +| 3664: 64 78 20 49 4e 54 45 47 45 52 2c 73 74 61 72 74 dx INTEGER,start +| 3680: 5f 62 6c 6f 63 6b 20 49 4e 54 45 47 45 52 2c 6c _block INTEGER,l +| 3696: 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 6b 20 eaves_end_block +| 3712: 49 4e 54 45 47 45 52 2c 65 6e 64 5f 62 6c 6f 63 INTEGER,end_bloc +| 3728: 6b 20 49 4e 54 45 47 45 52 2c 72 6f 6f 74 20 42 k INTEGER,root B +| 3744: 4c 4f 42 2c 50 52 49 4d 41 52 59 20 4b 45 59 28 LOB,PRIMARY KEY( +| 3760: 6c 65 76 65 6c 2c 20 69 64 78 29 29 31 05 06 17 level, idx))1... +| 3776: 45 1f 01 00 69 6e 64 65 78 73 71 6c 69 74 65 5f E...indexsqlite_ +| 3792: 61 75 74 6f 69 6e 64 65 78 5f 74 31 5f 73 65 67 autoindex_t1_seg +| 3808: 64 69 72 5f 31 74 31 5f 73 65 67 64 69 72 05 00 dir_1t1_segdir.. +| 3824: 00 00 08 00 00 00 00 66 03 07 17 23 23 01 81 13 .......f...##... +| 3840: 74 61 62 6c 65 74 31 5f 73 65 67 6d 65 6e 74 73 tablet1_segments +| 3856: 74 31 5f 73 65 67 6d 65 6e 74 73 03 43 52 45 41 t1_segments.CREA +| 3872: 54 45 20 54 41 42 4c 45 20 27 74 31 5f 73 65 67 TE TABLE 't1_seg +| 3888: 6d 65 6e 74 73 27 28 62 6c 6f 63 6b 69 64 20 49 ments'(blockid I +| 3904: 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b NTEGER PRIMARY K +| 3920: 45 59 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 29 5c EY, block BLOB). +| 3936: 02 07 17 21 21 01 81 03 74 61 62 6c 65 74 31 5f ...!!...tablet1_ +| 3952: 63 6f 6e 74 65 6e 74 74 31 5f 63 6f 6e 74 65 6e contentt1_conten +| 3968: 74 02 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 t.CREATE TABLE ' +| 3984: 74 31 5f 63 6f 6e 74 65 6e 74 27 28 64 6f 63 69 t1_content'(doci +| 4000: 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 d INTEGER PRIMAR +| 4016: 59 20 4b 45 59 2c 20 27 63 30 61 27 29 41 01 06 Y KEY, 'c0a')A.. +| 4032: 17 11 11 08 71 74 61 62 6c 65 74 31 74 31 43 52 ....qtablet1t1CR +| 4048: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4064: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 34 LE t1 USING fts4 +| 4080: 28 61 2c 70 72 65 66 69 78 3d 27 31 2c 32 27 29 (a,prefix='1,2') +| page 2 offset 4096 +| 0: 0d 00 00 00 08 0e 1f 00 0f c4 0f 7c 0f 34 0f 07 ...........|.4.. +| 16: 0e c3 0e 97 0e 63 0e 1f 00 00 00 00 00 00 00 00 .....c.......... +| 3600: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 42 ...............B +| 3616: 08 04 00 81 09 73 75 6e 74 20 69 6e 20 63 75 6c .....sunt in cul +| 3632: 70 61 20 71 75 69 20 6f 66 66 69 63 69 61 20 64 pa qui officia d +| 3648: 65 73 65 72 75 6e 74 20 6d 6f 6c 6c 69 74 20 61 eserunt mollit a +| 3664: 6e 69 6d 20 69 64 20 65 73 74 20 6c 61 62 6f 72 nim id est labor +| 3680: 75 6d 2e 32 07 03 00 6b 45 78 63 65 70 74 65 75 um.2...kExcepteu +| 3696: 72 20 73 69 6e 74 20 6f 63 63 61 65 63 61 74 20 r sint occaecat +| 3712: 63 75 70 69 64 61 74 61 74 20 6e 6f 6e 20 70 72 cupidatat non pr +| 3728: 6f 69 64 65 6e 74 2c 2a 06 03 00 5b 63 69 6c 6c oident,*...[cill +| 3744: 75 6d 20 64 6f 6c 6f 72 65 20 65 75 20 66 75 67 um dolore eu fug +| 3760: 69 61 74 20 6e 75 6c 6c 61 20 70 61 72 69 61 74 iat nulla pariat +| 3776: 75 72 2e 42 05 04 00 81 09 44 75 69 73 20 61 75 ur.B.....Duis au +| 3792: 74 65 20 69 72 75 72 65 21 64 6f 6c 6f 72 20 69 te irure!dolor i +| 3808: 6e 20 72 65 70 72 65 68 65 6e 64 65 72 69 74 20 n reprehenderit +| 3824: 69 6e 20 76 6f 6c 75 70 74 61 74 65 20 76 65 6c in voluptate vel +| 3840: 69 74 20 65 73 74 65 2b 04 03 00 5d 6e 69 73 69 it este+...]nisi +| 3856: 20 75 74 20 61 6c 69 71 75 69 70 20 65 78 20 65 ut aliquip ex e +| 3872: 61 20 63 6f 6d 6d 6f 64 6f 20 63 6f 6e 73 65 71 a commodo conseq +| 3888: 75 61 74 2e 46 03 04 00 81 11 55 74 20 65 6e 69 uat.F.....Ut eni +| 3904: 6d 20 61 64 20 6d 69 6e 69 6d 20 76 65 6e 69 61 m ad minim venia +| 3920: 6d 2c 20 71 75 69 73 20 6e 6f 73 74 72 75 64 20 m, quis nostrud +| 3936: 65 78 65 72 63 69 74 61 74 69 6f 6e 20 75 6c 6c exercitation ull +| 3952: 61 6d 63 6f 20 6c 61 62 6f 72 69 73 46 02 04 00 amco laborisF... +| 3968: 81 11 73 65 64 20 64 6f 20 65 69 75 73 6d 6f 64 ..sed do eiusmod +| 3984: 20 74 65 6d 70 6f 72 20 69 6e 63 69 64 69 64 75 tempor incididu +| 4000: 6e 74 20 75 74 20 6c 61 62 6f 72 65 20 65 74 20 nt ut labore et +| 4016: 64 6f 6c 6f 72 65 20 6d 61 67 6e 61 20 51 6c 69 dolore magna Qli +| 4032: 71 75 61 2e 3a 01 03 00 7b 4c 6f 72 65 6d 20 69 qua.:....Lorem i +| 4048: 70 73 75 6d 20 64 6f 6c 6f 72 20 73 69 74 e5 61 psum dolor sit.a +| 4064: 6d 65 74 2c 20 63 6f 6e 73 65 63 74 65 74 75 72 met, consectetur +| 4080: 20 61 64 69 70 69 73 63 69 6e 67 20 65 6c 69 74 adipiscing elit +| page 3 offset 8192 +| 0: 0d 00 00 00 00 10 00 00 00 00 00 00 00 00 00 00 ................ +| page 4 offset 12288 +| 0: 0d 00 00 00 03 0a a6 00 0d 57 0c 4a 0a a6 00 00 .........W.J.... +| 2720: 00 00 00 00 00 00 83 21 03 08 02 08 08 08 17 86 .......!........ +| 2736: 30 08 00 30 20 34 30 32 00 02 61 64 06 01 08 00 0..0 402..ad.... +| 2752: 02 04 00 01 01 6c 06 02 0c 00 02 04 00 01 01 6d .....l.........m +| 2768: 03 01 06 00 01 01 6e 03 08 09 00 01 01 75 03 05 ......n......u.. +| 2784: 03 00 00 02 63 69 03 06 02 00 01 01 6f 07 01 07 ....ci......o... +| 2800: 00 03 07 03 00 01 01 75 06 07 05 00 01 04 00 00 .......u........ +| 2816: 02 64 65 03 08 07 00 01 01 6f 0d 01 04 00 01 03 .de......o...... +| 2832: 09 00 03 05 00 01 03 00 01 01 75 03 05 02 00 00 ..........u..... +| 2848: 02 65 61 03 04 06 00 01 01 69 03 02 04 00 01 01 .ea......i...... +| 2864: 6c 03 01 09 00 01 01 6e 03 03 03 00 01 01 73 06 l......n......s. +| 2880: 05 0b 00 03 0b 00 01 01 74 03 02 09 00 01 01 75 ........t......u +| 2896: 03 06 04 00 01 01 78 09 03 09 00 01 05 00 03 02 ......x......... +| 2912: 00 00 02 66 75 03 06 05 00 00 02 69 64 03 08 0a ...fu......id... +| 2928: 00 01 01 6e 0a 02 06 00 03 06 04 00 03 03 00 01 ...n............ +| 2944: 01 70 03 01 03 00 01 01 72 03 05 04 00 00 02 6c .p......r......l +| 2960: 61 09 02 08 00 01 0b 00 05 0c 00 01 01 6f 03 01 a............o.. +| 2976: 02 00 00 02 6d 61 03 02 0b 00 01 01 69 03 03 05 ....ma......i... +| 2992: 00 01 01 6f 03 08 08 00 00 02 6e 69 03 04 02 00 ...o......ni.... +| 3008: 01 01 6f 06 03 08 00 04 06 00 01 01 75 03 06 06 ..o.........u... +| 3024: 00 00 02 6f 63 03 07 04 00 01 01 66 03 08 06 00 ...oc......f.... +| 3040: 00 02 70 61 03 06 07 00 01 01 72 03 07 07 00 00 ..pa......r..... +| 3056: 02 71 75 06 03 07 00 05 05 00 00 02 72 65 03 05 .qu.........re.. +| 3072: 07 00 00 02 73 65 03 02 02 00 01 01 69 06 01 05 ....se......i... +| 3088: 00 06 03 00 01 01 75 03 08 02 00 00 02 74 65 03 ......u......te. +| 3104: 02 05 00 00 02 75 6c 03 03 0a 00 01 01 74 09 02 .....ul......t.. +| 3120: 07 00 01 02 00 01 03 00 00 02 76 65 06 03 06 00 ..........ve.... +| 3136: 02 0a 00 01 01 6f 03 05 09 00 82 0a 02 08 12 08 .....o.......... +| 3152: 08 08 17 84 02 04 00 30 20 32 35 31 00 01 61 23 .......0 251..a# +| 3168: 01 06 04 00 01 0c 00 01 04 00 01 04 00 01 03 00 ................ +| 3184: 03 09 00 00 01 63 10 01 07 00 03 07 03 00 02 02 .....c.......... +| 3200: 00 01 05 00 01 04 00 00 01 64 11 01 04 00 01 03 .........d...... +| 3216: 09 00 03 02 05 00 01 13 00 02 07 00 00 01 65 1b ..............e. +| 3232: 01 09 00 01 04 07 00 01 03 08 00 01 05 03 00 01 ................ +| 3248: 0b 00 01 04 00 01 02 00 01 0b 00 00 01 66 03 06 .............f.. +| 3264: 05 00 00 01 69 0f 01 03 00 01 06 00 03 04 04 04 ....i........... +| 3280: 00 03 03 09 00 00 01 6c 0c 01 02 00 01 08 00 01 .......l........ +| 3296: 0b 00 05 0c 00 10 01 6d 09 02 0b 00 01 05 00 05 .......m........ +| 3312: 08 00 00 01 6e 0c 03 08 00 01 02 00 02 06 00 01 ....n........... +| 3328: 06 00 00 01 6f 06 07 04 00 01 06 00 00 01 70 06 ....o.........p. +| 3344: 06 07 00 01 07 00 00 01 71 06 03 07 00 05 05 00 ........q....... +| 3360: 00 01 72 03 05 07 00 00 01 73 0c 01 05 00 01 02 ..r......s...... +| 3376: 00 05 03 00 01 02 00 00 01 74 03 02 05 00 00 01 .........t...... +| 3392: 75 0a 02 07 00 01 02 0a 00 01 03 00 00 01 76 07 u.............v. +| 3408: 03 06 00 02 09 03 00 85 26 01 08 08 08 08 08 17 ........&....... +| 3424: 8a 3e 30 20 36 36 35 00 02 61 64 03 03 04 00 02 .>0 665..ad..... +| 3440: 08 69 70 69 73 63 69 6e 67 03 01 08 00 01 05 6c .ipiscing......l +| 3456: 69 71 75 61 03 02 0c 00 05 02 69 70 03 04 04 00 iqua......ip.... +| 3472: 01 03 6d 65 74 03 01 06 00 01 03 6e 69 6d 03 08 ..met......nim.. +| 3488: 09 00 01 03 75 74 65 03 05 03 00 00 06 63 69 6c ....ute......cil +| 3504: 6c 75 6d 03 06 02 00 01 06 6f 6d 6d 6f 64 6f 03 lum......ommodo. +| 3520: 04 07 00 02 09 6e 73 65 63 74 65 74 75 72 03 01 .....nsectetur.. +| 3536: 07 00 05 04 71 75 61 74 03 04 08 00 01 04 75 6c ....quat......ul +| 3552: 70 61 03 08 04 00 02 07 70 69 64 61 74 61 74 03 pa......pidatat. +| 3568: 07 05 00 00 08 64 65 73 65 72 75 6e 74 03 08 07 .....deserunt... +| 3584: 00 01 01 6f 03 02 03 00 02 03 6c 6f 72 06 01 04 ...o......lor... +| 3600: 00 04 05 00 05 01 65 06 02 0a 00 04 03 00 01 03 ......e......... +| 3616: 75 69 73 03 05 02 00 00 02 65 61 03 04 06 00 01 uis......ea..... +| 3632: 06 69 75 73 6d 6f 64 03 02 04 00 01 03 6c 69 74 .iusmod......lit +| 3648: 03 01 09 00 01 03 6e 69 6d 03 03 03 00 01 03 73 ......nim......s +| 3664: 73 65 03 05 0b 00 02 01 74 03 08 0b 00 01 01 74 se......t......t +| 3680: 03 02 09 00 01 01 75 03 06 04 00 01 01 78 03 04 ......u......x.. +| 3696: 05 00 02 07 63 65 70 74 65 75 72 03 07 02 00 02 ....cepteur..... +| 3712: 0a 65 72 63 69 74 61 74 69 6f 6e 03 03 09 00 00 .ercitation..... +| 3728: 06 66 75 67 69 61 74 03 06 05 00 00 02 69 64 03 .fugiat......id. +| 3744: 08 0a 00 01 01 6e 07 05 06 04 00 03 03 00 02 08 .....n.......... +| 3760: 63 69 64 69 64 75 7e 74 03 02 06 00 01 04 70 73 cididu~t......ps +| 3776: 75 6d 03 01 03 00 01 03 72 75 72 65 03 05 04 00 um......rure.... +| 3792: 00 06 6c 61 62 6f 72 65 03 02 08 00 05 02 69 73 ..labore......is +| 3808: 03 03 0b 00 05 02 75 6d 03 08 0c 00 01 04 6f 72 ......um......or +| 3824: 65 6d 03 01 02 00 00 05 6d 61 67 6e 61 03 02 0b em......magna... +| 3840: 00 01 04 69 6e 69 6d 03 03 05 00 01 05 6f 6c 6c ...inim......oll +| 3856: 69 74 03 08 08 00 00 04 6e 69 73 69 03 04 02 00 it......nisi.... +| 3872: 01 02 6f 6e 03 07 06 00 02 05 73 74 72 75 64 03 ..on......strud. +| 3888: 03 08 00 01 04 75 6c 6c 61 03 06 06 00 00 08 6f .....ulla......o +| 3904: 63 63 61 65 63 61 74 03 07 04 00 01 06 66 66 69 ccaecat......ffi +| 3920: 63 69 61 03 08 06 00 00 08 70 61 72 69 61 74 75 cia......pariatu +| 3936: 72 03 06 07 00 01 07 72 6f 69 64 65 6e 74 03 07 r......roident.. +| 3952: 07 00 00 03 71 75 69 03 08 05 00 03 01 73 03 03 ....qui......s.. +| 3968: 07 00 00 0d 72 65 70 72 65 68 65 6e 64 65 72 69 ....reprehenderi +| 3984: 74 03 05 07 00 00 03 73 65 64 03 02 02 00 01 03 t......sed...... +| 4000: 69 6e 74 03 07 03 00 02 01 74 03 01 05 00 01 03 int......t...... +| 4016: 75 6e 74 03 08 02 00 00 06 74 65 6d 70 6f 72 03 unt......tempor. +| 4032: 02 05 00 00 07 75 6c 6c 61 6d 63 6f 03 03 0a 00 .....ullamco.... +| 4048: 01 01 74 09 02 07 00 01 02 00 01 03 00 00 05 76 ..t............v +| 4064: 65 6c 69 74 03 05 0a 00 02 04 6e 69 61 6d 03 03 elit......niam.. +| 4080: 06 00 01 08 6f 6c 75 70 74 61 74 65 03 05 09 00 ....oluptate.... +| page 5 offset 16384 +| 0: 0a 00 00 00 03 0f eb 00 0f fb 0f f3 0f eb 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 07 04 02 08 01 ................ +| 4080: 08 00 03 07 04 02 08 03 a4 00 02 04 04 08 08 09 ................ +| page 6 offset 20480 +| 0: 0d 00 00 00 08 0f d0 00 0f fa 0f f4 0f ee 0f e8 ................ +| 16: 0f e2 0f dc 0f d6 0f d0 00 00 00 00 00 00 00 00 ................ +| 4048: 04 08 03 00 0e 0b 04 07 03 00 0e 06 04 06 03 00 ................ +| 4064: 0e 06 04 05 03 00 0e 0a 04 04 03 00 0e 07 04 03 ................ +| 4080: 03 00 0e 0a 04 02 03 00 0e 0b 04 01 03 00 0e 08 ................ +| page 7 offset 24576 +| 0: 0d 00 00 00 01 0f f7 00 0f f7 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 07 00 03 00 14 08 45 b5 03 .............E.. +| end crash-de7e8cb026385a.db +}]} {} + +do_catchsql_test 16.1 { + INSERT INTO t1(t1) VALUES('optimize'); +} {0 {}} + +#------------------------------------------------------------------------- +reset_db +do_test 17.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 28672 pagesize 4096 filename crash-f15972acf5bc1c.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 07 .....@ ........ +| 32: 00 00 00 02 00 00 00 01 00 00 00 07 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 96: 00 00 00 00 0d 0e b1 00 06 0d a4 00 0f 8d 0f 21 ...............! +| 112: 0e b9 0d c8 0e 7e 0d a4 0d a4 00 00 00 00 00 00 .....~.......... +| 3488: 00 00 00 00 22 07 06 17 11 11 01 31 74 61 62 6c ...........1tabl +| 3504: 65 74 32 74 32 07 43 52 45 41 54 45 20 54 41 42 et2t2.CREATE TAB +| 3520: 4c 45 20 74 32 28 78 29 81 33 05 07 17 1f 1f 01 LE t2(x).3...... +| 3536: 82 35 74 61 62 6c 65 74 31 5f 73 65 67 64 69 72 .5tablet1_segdir +| 3552: 74 31 5f 73 65 67 64 69 72 05 43 52 45 41 54 45 t1_segdir.CREATE +| 3568: 20 54 41 42 4c 45 20 27 74 31 5f 73 65 67 64 69 TABLE 't1_segdi +| 3584: 72 27 28 6c 65 76 65 6c 20 49 4e 54 45 47 45 52 r'(level INTEGER +| 3600: 2c 69 64 78 20 49 4e 54 45 47 45 52 2c 73 74 61 ,idx INTEGER,sta +| 3616: 72 74 5f 62 6c 6f 63 6b 20 49 4e 54 45 47 45 52 rt_block INTEGER +| 3632: 2c 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 ,leaves_end_bloc +| 3648: 6b 20 49 4e 54 45 47 45 52 2c 65 6e 64 5f 62 6c k INTEGER,end_bl +| 3664: 6f 63 6b 20 49 4e 54 45 47 45 52 2c 72 6f 6f 74 ock INTEGER,root +| 3680: 20 42 4c 4f 42 2c 50 52 49 4d 41 52 59 20 4b 45 BLOB,PRIMARY KE +| 3696: 59 28 6c 65 76 65 6c 2c 20 69 64 78 29 29 31 06 Y(level, idx))1. +| 3712: 06 17 45 1f 01 00 69 6e 64 65 78 73 71 6c 69 74 ..E...indexsqlit +| 3728: 65 5f 61 75 74 6f 69 6e 64 65 78 5f 74 31 5f 73 e_autoindex_t1_s +| 3744: 65 67 64 69 72 5f 31 74 31 5f 73 65 67 64 69 72 egdir_1t1_segdir +| 3760: 06 0f c7 00 08 00 00 00 00 66 04 07 17 23 23 01 .........f...##. +| 3776: 81 13 74 61 62 6c 65 74 31 5f 73 65 67 6d 65 6e ..tablet1_segmen +| 3792: 74 73 74 31 5f 73 65 67 6d 65 6e 74 73 04 43 52 tst1_segments.CR +| 3808: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 73 EATE TABLE 't1_s +| 3824: 65 67 6d 65 6e 74 73 27 28 62 6c 6f 63 6b 69 64 egments'(blockid +| 3840: 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 INTEGER PRIMARY +| 3856: 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 KEY, block BLOB +| 3872: 29 6a 03 07 17 21 21 01 81 1f 74 61 62 6c 65 74 )j...!!...tablet +| 3888: 31 5f 63 6f 6e 74 65 6e 74 74 31 5f 63 6f 6e 74 1_contentt1_cont +| 3904: 65 6e 74 03 43 52 45 41 54 45 20 54 41 42 4c 45 ent.CREATE TABLE +| 3920: 20 27 74 31 5f 63 6f 6e 74 65 6e 74 27 28 64 6f 't1_content'(do +| 3936: 63 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d cid INTEGER PRIM +| 3952: 41 52 59 20 4b 45 59 2c 20 27 63 30 61 27 2c 20 ARY KEY, 'c0a', +| 3968: 27 63 31 62 27 2c 20 27 63 32 63 27 29 38 02 06 'c1b', 'c2c')8.. +| 3984: 17 11 11 08 5f 74 61 62 6c 65 74 31 74 31 43 52 ...._tablet1t1CR +| 4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 33 LE t1 USING fts3 +| 4032: 28 61 2c 62 2c 63 29 00 00 00 39 00 00 00 00 00 (a,b,c)...9..... +| page 3 offset 8192 +| 0: 0d 00 00 00 25 0b 48 00 0f d8 0f af 0f 86 0f 74 ....%.H........t +| 16: 0f 61 0f 4e 0f 2f 0f 0f 0e ef 0e d7 0e be 0e a5 .a.N./.......... +| 32: 0e 8d 0e 74 0e 5b 0e 40 0e 24 0e 08 0d ef 0d d5 ...t.[.@.$...... +| 48: 0d bb 0d a0 0d 84 03 28 0d 4f 0d 35 0d 1b 0c fb .......(.O.5.... +| 64: 0c da 0c b9 0c 99 0c 78 0c 57 0c 3e 0c 24 0c 0a .......x.W.>.$.. +| 80: 0b 48 00 00 00 00 00 00 00 00 00 00 00 00 00 00 .H.............. +| 2880: 00 00 00 00 00 00 00 00 81 3f 25 06 00 72 7f 00 .........?%..r.. +| 2896: 00 43 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2e .COMPILER=gcc-5. +| 2912: 34 2e 30 20 32 30 31 36 30 36 30 39 20 44 45 42 4.0 20160609 DEB +| 2928: 55 47 20 45 4e 41 42 4c 45 20 44 42 53 54 41 54 UG ENABLE DBSTAT +| 2944: 20 56 54 41 42 20 45 4e 41 42 4c 45 20 46 54 53 VTAB ENABLE FTS +| 2960: 34 20 45 4e 41 42 4c 45 20 46 54 53 35 20 45 4e 4 ENABLE FTS5 EN +| 2976: 41 42 4c 45 20 47 45 4f 50 4f 4c 59 20 45 4e 41 ABLE GEOPOLY ENA +| 2992: 42 4c 45 20 4a 53 4f 4e 31 20 45 4e 41 42 4c 45 BLE JSON1 ENABLE +| 3008: 20 4d 45 4d 53 59 53 35 20 45 4e 41 42 4c 45 20 MEMSYS5 ENABLE +| 3024: 52 54 52 45 45 20 4d 41 58 20 4d 45 4d 4f 52 59 RTREE MAX MEMORY +| 3040: 3d 35 30 30 30 30 30 30 30 20 4f 4d 49 54 20 4c =50000000 OMIT L +| 3056: 4f 41 43 20 45 58 54 45 4e 53 49 4f 4e 20 54 48 OAC EXTENSION TH +| 3072: 52 45 41 44 53 41 46 45 3d 30 18 24 05 00 25 0f READSAFE=0.$..%. +| 3088: 19 54 48 52 45 41 44 53 41 46 45 3d 30 58 42 49 .THREADSAFE=0XBI +| 3104: 4e 41 52 59 18 23 05 00 25 0f 19 54 48 52 45 41 NARY.#..%..THREA +| 3120: 44 53 41 46 45 3d 30 58 4e 4f 43 41 53 45 17 22 DSAFE=0XNOCASE.. +| 3136: 05 00 25 0f 17 54 48 52 45 41 44 53 41 46 45 3d ..%..THREADSAFE= +| 3152: 30 58 52 54 52 49 4d 1f 21 05 00 33 0f 19 4f 4d 0XRTRIM.!..3..OM +| 3168: 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 4f IT LOAD EXTENSIO +| 3184: 4e 58 42 49 4e 41 52 59 1f 20 05 00 33 0f 19 4f NXBINARY. ..3..O +| 3200: 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 MIT LOAD EXTENSI +| 3216: 4f 4e 58 4e 4f 43 41 53 45 1e 1f 05 00 33 0f 17 ONXNOCASE....3.. +| 3232: 4f 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 OMIT LOAD EXTENS +| 3248: 49 4f 4e 58 52 54 52 49 4d 1f 1e 05 00 33 0f 19 IONXRTRIM....3.. +| 3264: 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 30 MAX MEMORY=50000 +| 3280: 30 30 30 58 42 49 4e 41 52 59 1f 1d 05 00 33 0f 000XBINARY....3. +| 3296: 19 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 .MAX MEMORY=5000 +| 3312: 30 30 30 30 58 4e 4f 43 41 53 45 1e 1c 05 00 33 0000XNOCASE....3 +| 3328: 0f 17 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 ..MAX MEMORY=500 +| 3344: 30 30 30 30 30 58 52 54 52 49 4d 18 1b 05 00 25 00000XRTRIM....% +| 3360: 0f 19 45 4e 41 42 4c 45 20 52 54 52 45 45 58 42 ..ENABLE RTREEXB +| 3376: 49 4e 41 52 59 18 1a 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3392: 4c 45 20 52 54 52 45 45 58 4e 4f 43 41 53 45 17 LE RTREEXNOCASE. +| 3408: 19 05 00 25 0f 17 45 4e 41 42 4c 45 20 52 54 52 ...%..ENABLE RTR +| 3424: 45 45 58 52 54 52 49 4d 1a 18 05 00 29 0f 19 45 EEXRTRIM....)..E +| 3440: 4e 41 42 4c 45 20 4d 45 4d 53 59 53 35 58 42 49 NABLE MEMSYS5XBI +| 3456: 4e 41 52 59 1a 17 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3472: 45 20 4d 45 4d 53 59 53 35 58 4e 4f 43 41 53 45 E MEMSYS5XNOCASE +| 3488: 19 16 05 00 29 0f 17 45 4e 41 42 4c 45 20 4d 45 ....)..ENABLE ME +| 3504: 4d 53 59 53 35 58 52 54 52 49 4d 18 14 05 00 25 MSYS5XRTRIM....% +| 3520: 0f 19 45 4e 41 42 4c 45 20 4a 53 4f 4e 31 58 42 ..ENABLE JSON1XB +| 3536: 49 4e 41 52 59 18 14 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3552: 4c 45 20 4a 53 4f 4e 31 58 4e 4f 43 41 53 45 17 LE JSON1XNOCASE. +| 3568: 13 05 00 25 0f 17 45 4e 41 42 4c 45 20 4a 53 4f ...%..ENABLE JSO +| 3584: 4e 31 58 52 54 52 49 4d 1a 12 05 00 29 0f 19 45 N1XRTRIM....)..E +| 3600: 4e 41 42 4c 45 20 47 45 4f 50 4f 4c 59 58 42 49 NABLE GEOPOLYXBI +| 3616: 4e 41 52 59 1a 11 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3632: 45 20 47 45 4f 50 4f 4c 59 58 4e 4f 43 41 53 45 E GEOPOLYXNOCASE +| 3648: 19 10 05 00 29 0f 17 45 4e 41 42 4c 45 20 47 45 ....)..ENABLE GE +| 3664: 4f 50 4f 4c 59 58 52 54 52 49 4d 17 0f 05 00 23 OPOLYXRTRIM....# +| 3680: 0f 19 45 4e 41 42 4c 45 20 46 54 53 35 58 42 49 ..ENABLE FTS5XBI +| 3696: 4e 41 52 59 17 0e 05 00 23 0f 19 45 4e 41 42 4c NARY....#..ENABL +| 3712: 45 20 46 54 53 35 58 4e 4f 43 41 53 45 16 0d 05 E FTS5XNOCASE... +| 3728: 00 23 0f 17 45 4e 41 42 4c 45 20 46 54 53 35 58 .#..ENABLE FTS5X +| 3744: 52 54 52 49 4d 17 0c 05 00 23 0f 19 45 4e 41 42 RTRIM....#..ENAB +| 3760: 4c 45 20 46 54 53 34 58 42 49 4e 41 52 59 17 0b LE FTS4XBINARY.. +| 3776: 05 00 23 0f 19 45 4e 41 42 4c 45 20 46 54 53 34 ..#..ENABLE FTS4 +| 3792: 58 4e 4f 43 41 53 45 16 0a 05 00 23 0f 17 45 4e XNOCASE....#..EN +| 3808: 41 42 4c 45 20 46 54 53 34 58 52 54 52 49 4d 1e ABLE FTS4XRTRIM. +| 3824: 09 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3840: 54 41 54 20 56 54 41 42 58 42 49 4e 41 52 59 1e TAT VTABXBINARY. +| 3856: 08 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3872: 54 41 54 20 56 54 41 42 58 4e 4f 43 41 53 45 1d TAT VTABXNOCASE. +| 3888: 07 05 00 31 0f 17 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3904: 54 41 54 20 56 54 41 42 58 52 54 52 49 4d 11 06 TAT VTABXRTRIM.. +| 3920: 05 00 17 0f 19 44 45 42 55 47 58 42 49 4e 41 52 .....DEBUGXBINAR +| 3936: 59 11 05 05 00 17 0e 19 44 45 42 55 47 58 4e 4f Y.......DEBUGXNO +| 3952: 43 41 53 45 10 04 05 00 17 0f 17 44 45 42 55 47 CASE.......DEBUG +| 3968: 58 52 54 52 49 4d 27 03 05 00 43 0f 19 43 4f 4d XRTRIM'...C..COM +| 3984: 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e 30 20 PILER=gcc-5.4.0 +| 4000: 32 30 31 36 30 36 30 39 58 42 49 4e 41 52 59 27 20160609XBINARY' +| 4016: 02 05 00 43 0f 19 43 4f 4d 50 49 4c 45 52 3d 67 ...C..COMPILER=g +| 4032: 63 63 2d 35 2e 34 2e 30 20 32 30 31 36 30 36 30 cc-5.4.0 2016060 +| 4048: 39 58 4e 4f 43 41 53 45 26 01 05 00 43 0f 17 43 9XNOCASE&...C..C +| 4064: 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e OMPILER=gcc-5.4. +| 4080: 30 20 32 30 31 36 30 36 30 39 58 52 54 52 49 4d 0 20160609XRTRIM +| page 4 offset 12288 +| 0: 0d 00 00 00 00 10 00 00 00 00 00 00 00 00 00 00 ................ +| page 5 offset 16384 +| 0: 0d 00 00 00 02 0b a0 00 0c ad 0b a0 00 00 00 00 ................ +| 2976: 82 0a 02 08 08 09 08 08 17 84 06 30 20 32 35 33 ...........0 253 +| 2992: 00 01 30 04 25 06 1b 00 00 08 32 30 31 36 30 36 ..0.%.....201606 +| 3008: 30 39 03 25 07 00 00 01 34 03 25 05 00 00 01 35 09.%....4.%....5 +| 3024: 03 25 04 00 01 07 30 30 30 30 30 30 30 03 25 1a .%....0000000.%. +| 3040: 00 00 08 63 6f 6d 70 69 6c 65 72 03 25 02 00 00 ...compiler.%... +| 3056: 06 64 62 73 74 61 74 03 25 0a 00 01 04 65 62 75 .dbstat.%....ebu +| 3072: 67 03 25 08 00 00 06 65 6e 61 62 6c 65 09 25 09 g.%....enable.%. +| 3088: 05 04 04 04 04 04 00 01 08 78 74 65 6e 73 69 6f .........xtensio +| 3104: 6e 03 25 1d 00 00 04 66 74 73 34 03 25 0d 00 03 n.%....fts4.%... +| 3120: 01 35 03 25 0f 00 00 03 67 63 63 03 25 03 00 01 .5.%....gcc.%... +| 3136: 06 65 6f 70 6f 6c 79 03 25 11 00 00 05 6a 73 6f .eopoly.%....jso +| 3152: 6e 31 03 25 13 00 00 04 6c 6f 61 64 03 25 1c 00 n1.%....load.%.. +| 3168: 00 03 6d 61 78 03 25 18 00 01 05 65 6d 6f 72 79 ..max.%....emory +| 3184: 03 25 19 00 03 04 73 79 73 4d 03 25 15 00 00 04 .%....sysM.%.... +| 3200: 6f 6d 69 74 03 25 1b 00 00 05 72 74 72 65 65 03 omit.%....rtree. +| 3216: 25 17 00 00 0a 74 68 72 65 61 64 73 61 66 65 03 %....threadsafe. +| 3232: 25 0e 00 00 04 76 74 61 62 03 25 0b 00 86 50 01 %....vtab.%...P. +| 3248: 08 08 08 08 08 17 8d 12 30 20 38 33 35 00 01 30 ........0 835..0 +| 3264: 12 01 06 00 01 06 00 01 06 00 1f 03 00 01 03 00 ................ +| 3280: 01 03 00 00 08 32 30 31 36 30 36 30 39 09 01 07 .....20160609... +| 3296: 00 01 07 00 01 07 00 00 01 34 09 01 05 00 01 05 .........4...... +| 3312: 00 01 05 00 00 01 35 09 01 04 00 01 04 00 01 04 ......5......... +| 3328: 00 01 07 30 30 30 30 30 30 30 09 1c 04 00 01 04 ...0000000...... +| 3344: 00 01 04 00 00 06 62 69 6e 61 72 79 3c 03 01 02 ......binary<... +| 3360: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3376: 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 ................ +| 3392: 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03 ................ +| 3408: 01 02 02 00 03 01 02 02 00 00 08 63 6f 6d 70 69 ...........compi +| 3424: 6c 65 72 09 01 02 00 01 02 00 01 02 00 00 06 64 ler............d +| 3440: 62 73 74 61 74 09 07 03 00 01 03 00 01 03 00 01 bstat........... +| 3456: 04 65 62 75 67 09 04 02 00 01 02 00 01 02 00 00 .ebug........... +| 3472: 06 65 6e 61 62 6c 65 3f 07 02 00 01 02 00 01 02 .enable?........ +| 3488: 00 01 02 00 01 02 00 01 02 00 01 02 00 01 02 00 ................ +| 3504: 01 02 00 01 02 00 01 02 00 01 02 00 01 02 00 01 ................ +| 3520: 02 00 01 02 00 01 02 00 01 02 00 01 02 00 01 02 ................ +| 3536: 00 01 02 00 01 02 00 01 08 78 74 65 6e 73 69 6f .........xtensio +| 3552: 6e 09 1f 04 00 01 04 00 01 04 00 00 04 66 74 73 n............fts +| 3568: 34 09 0a 03 00 01 03 00 01 03 00 03 01 35 09 0d 4............5.. +| 3584: 03 00 01 03 00 01 03 00 00 03 67 63 63 09 01 03 ..........gcc... +| 3600: 00 01 03 00 01 03 00 01 06 65 6f 70 6f 6c 79 09 .........eopoly. +| 3616: 10 03 00 01 03 00 01 03 00 00 05 6a 73 6f 6e 31 ...........json1 +| 3632: 09 13 03 00 01 03 00 01 03 00 00 04 6c 6f 61 64 ............load +| 3648: 09 1f 03 00 01 03 00 01 03 00 00 03 6d 61 78 09 ............max. +| 3664: 1c 02 00 01 02 00 01 02 00 01 05 65 6d 6f 72 79 ...........emory +| 3680: 09 1c 03 00 01 03 00 01 03 00 03 04 73 79 73 35 ............sys5 +| 3696: 09 16 03 00 01 03 00 01 03 00 00 06 6e 6f 63 61 ............noca +| 3712: 73 65 3c 02 01 02 02 00 03 01 02 02 00 03 01 02 se<............. +| 3728: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3744: 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 ................ +| 3760: 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 00 ................ +| 3776: 04 6f 6d 69 74 09 1f 02 00 01 02 00 01 02 00 00 .omit........... +| 3792: 05 72 74 72 65 65 09 19 03 00 01 03 00 01 03 00 .rtree.......... +| 3808: 03 02 69 6d 3c 01 01 02 02 00 03 01 02 02 00 03 ..im<........... +| 3824: 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01 ................ +| 3840: 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 ................ +| 3856: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3872: 00 00 0a 74 68 72 65 61 64 73 61 66 65 09 22 02 ...threadsafe... +| 3888: 00 01 02 00 01 02 00 00 04 76 74 61 62 09 07 04 .........vtab... +| 3904: 00 01 04 00 01 04 00 00 01 78 b4 01 01 01 01 02 .........x...... +| 3920: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| 3936: 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 ................ +| 3952: 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 ................ +| 3968: 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 ................ +| 3984: 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 ................ +| 4000: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| 4016: 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 ................ +| 4032: 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 ................ +| 4048: 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 ................ +| 4064: 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 ................ +| 4080: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 02 0f f5 00 0f fb 0f f5 00 00 00 00 ................ +| 4080: 00 00 00 00 00 05 04 08 09 01 02 04 04 08 08 09 ................ +| page 7 offset 24576 +| 0: 0d 00 00 00 05 0f b8 00 0f f4 0f e9 10 d6 0f c7 ................ +| 16: 0f b8 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 4016: 00 00 00 00 00 00 00 00 0d 05 02 23 61 75 74 6f ...........#auto +| 4032: 6d 65 72 67 65 3d 35 0d 04 02 23 6d 65 72 67 65 merge=5...#merge +| 4048: 3d 31 30 30 2c 38 11 03 02 2b 69 6e 74 65 67 72 =100,8...+integr +| 4064: 69 74 79 2d 63 68 65 63 6b 09 02 02 1b 72 65 62 ity-check....reb +| 4080: 75 69 6c 64 0a 01 02 1d 6f 70 74 69 6d 69 7a 65 uild....optimize +| end crash-f15972acf5bc1c.db +}]} {} + +do_execsql_test 17.1 { + BEGIN; + INSERT INTO t1(t1) SELECT x FROM t2; + UPDATE t1 SET b=quote(zeroblob(200)) WHERE a MATCH 'thread*'; +} + +do_execsql_test 17.2 { + INSERT INTO t1(t1) VALUES('optimize'); +} + +do_catchsql_test 17.3 { + DROP TABLE IF EXISTS t1; +} {0 {}} + +#------------------------------------------------------------------------- +reset_db +do_test 18.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 32768 pagesize 4096 filename crash-4ce32d0608aff1.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 07 .....@ ........ +| 32: 00 00 00 02 00 00 00 01 00 00 00 07 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 07 00 00 00 01 00 00 00 00 ................ +| 96: 00 00 00 00 0d 0e b1 00 06 0d a4 00 0f 8d 0f 21 ...............! +| 112: 0e b9 0d c8 0e 7e 0d a4 0d a4 00 00 00 00 00 00 .....~.......... +| 3488: 00 00 00 00 22 07 06 17 11 11 01 31 74 61 62 6c ...........1tabl +| 3504: 65 74 32 74 32 07 43 52 45 41 54 45 20 54 41 42 et2t2.CREATE TAB +| 3520: 4c 45 20 74 32 28 78 29 81 33 05 07 17 1f 1f 01 LE t2(x).3...... +| 3536: 82 35 74 61 62 6c 65 74 31 5f 73 65 67 64 69 72 .5tablet1_segdir +| 3552: 74 31 5f 73 65 67 64 69 72 05 43 52 45 41 54 45 t1_segdir.CREATE +| 3568: 20 54 41 42 4c 45 20 27 74 31 5f 73 65 67 64 69 TABLE 't1_segdi +| 3584: 72 27 28 6c 65 76 65 6c 20 49 4e 54 45 47 45 52 r'(level INTEGER +| 3600: 2c 69 64 78 20 49 4e 54 45 47 45 52 2c 73 74 61 ,idx INTEGER,sta +| 3616: 72 74 5f 62 6c 6f 63 6b 20 49 4e 54 45 47 45 52 rt_block INTEGER +| 3632: 2c 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 ,leaves_end_bloc +| 3648: 6b 20 49 4e 54 45 47 45 52 2c 65 6e 64 5f 62 6c k INTEGER,end_bl +| 3664: 6f 63 6b 20 49 4e 54 45 47 45 62 2c 72 6f 6f 74 ock INTEGEb,root +| 3680: 20 42 4c 4f 42 2c 50 52 49 4d 41 52 59 20 4b 45 BLOB,PRIMARY KE +| 3696: 59 28 6c 65 76 65 6c 2c 20 69 64 78 29 29 31 06 Y(level, idx))1. +| 3712: 06 17 45 1f 01 00 69 6e 64 65 78 73 71 6c 69 74 ..E...indexsqlit +| 3728: 65 5f 61 75 74 6f 69 6e 64 65 78 5f 74 31 5f 73 e_autoindex_t1_s +| 3744: 65 67 64 69 72 5f 31 74 31 5f 73 65 67 64 69 72 egdir_1t1_segdir +| 3760: 06 0f c7 00 08 00 00 00 00 66 04 07 17 23 23 01 .........f...##. +| 3776: 81 13 74 61 62 6c 65 74 31 5f 73 65 67 6d 65 6e ..tablet1_segmen +| 3792: 74 73 74 31 5f 73 65 67 6d 65 6e 74 73 04 43 52 tst1_segments.CR +| 3808: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 73 EATE TABLE 't1_s +| 3824: 65 67 6d 65 6e 74 73 27 28 62 6c 6f 63 6b 69 64 egments'(blockid +| 3840: 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 INTEGER PRIMARY +| 3856: 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 KEY, block BLOB +| 3872: 29 6a 03 07 17 21 21 01 81 1f 74 61 62 6c 65 74 )j...!!...tablet +| 3888: 31 5f 63 6f 6e 74 65 6e 74 74 31 5f 63 6f 6e 74 1_contentt1_cont +| 3904: 65 6e 74 03 43 52 45 41 54 45 20 54 41 42 4c 45 ent.CREATE TABLE +| 3920: 20 27 74 31 5f 63 6f 6e 74 65 6e 74 27 28 64 6f 't1_content'(do +| 3936: 63 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d cid INTEGER PRIM +| 3952: 41 52 59 20 4b 45 59 2c 20 27 63 30 61 27 2c 20 ARY KEY, 'c0a', +| 3968: 27 63 31 62 27 2c 20 27 63 32 63 27 29 38 02 06 'c1b', 'c2c')8.. +| 3984: 17 11 11 08 5f 74 61 62 6c 65 74 31 74 31 43 52 ...._tablet1t1CR +| 4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 33 LE t1 USING fts3 +| 4032: 28 61 2c 62 2c 63 29 00 00 00 39 00 00 00 00 00 (a,b,c)...9..... +| page 3 offset 8192 +| 0: 0d 00 00 00 25 0b 48 00 0f d8 0f af 0f 86 0f 74 ....%.H........t +| 16: 0f 61 0f 4e 0f 2f 0f 0f 0e ef 0e d7 0e be 0e a5 .a.N./.......... +| 32: 0e 8d 0e 74 0e 5b 0e 40 0e 24 0e 08 0d ef 0d d5 ...t.[.@.$...... +| 48: 0d bb 0d a0 0d 84 0d 68 0d 4f 0d 35 0d 1b 0c fb .......h.O.5.... +| 64: 0c da 0c b9 0c 99 0c 78 0c 57 0c 3e 0c 24 0c 0a .......x.W.>.$.. +| 80: 0b 48 00 00 00 00 00 00 00 00 00 00 00 00 00 00 .H.............. +| 2880: 00 00 00 00 00 00 00 00 81 3f 25 06 00 82 7f 00 .........?%..... +| 2896: 00 43 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2e .COMPILER=gcc-5. +| 2912: 34 23 00 20 32 30 31 36 30 36 30 39 20 44 45 42 4#. 20160609 DEB +| 2928: 55 47 20 45 4e 41 42 4c 45 20 44 42 53 54 41 54 UG ENABLE DBSTAT +| 2944: 20 56 54 41 42 20 45 4e 41 42 4c 45 20 46 54 53 VTAB ENABLE FTS +| 2960: 34 20 45 4e 41 42 4c 45 20 46 54 53 35 20 45 4e 4 ENABLE FTS5 EN +| 2976: 41 42 4c 45 20 47 45 4f 50 4f 4c 59 20 45 4e 41 ABLE GEOPOLY ENA +| 2992: 42 4c 45 20 4a 53 4f 4e 31 20 45 4e 41 42 4c 45 BLE JSON1 ENABLE +| 3008: 20 4d 45 4d 53 59 53 35 20 45 4e 41 42 4c 45 20 MEMSYS5 ENABLE +| 3024: 52 54 52 45 45 20 4d 41 58 20 4d 45 4d 4f 52 59 RTREE MAX MEMORY +| 3040: 3d 35 30 30 30 30 30 30 30 20 4f 4d 49 54 20 4c =50000000 OMIT L +| 3056: 4f 41 44 20 45 58 54 45 4e 53 49 4f 4e 20 54 48 OAD EXTENSION TH +| 3072: 52 45 41 44 53 41 46 45 3d 30 18 24 05 00 25 0f READSAFE=0.$..%. +| 3088: 19 54 48 52 45 41 44 53 41 46 45 3d 30 58 42 49 .THREADSAFE=0XBI +| 3104: 4e 41 52 59 18 23 05 00 25 0f 19 54 48 52 45 41 NARY.#..%..THREA +| 3120: 44 53 41 46 45 3d 30 58 4e 4f 43 41 53 45 17 22 DSAFE=0XNOCASE.. +| 3136: 05 00 25 0f 17 54 48 52 45 41 44 53 41 46 45 3d ..%..THREADSAFE= +| 3152: 30 58 52 54 52 49 4d 1f 21 05 00 33 0f 19 4f 4d 0XRTRIM.!..3..OM +| 3168: 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 4f IT LOAD EXTENSIO +| 3184: 4e 58 42 49 4e 41 52 59 1f 20 05 00 33 0f 19 4f NXBINARY. ..3..O +| 3200: 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 MIT LOAD EXTENSI +| 3216: 4f 4e 58 4e 4f 43 41 53 45 1e 1f 05 00 33 0f 17 ONXNOCASE....3.. +| 3232: 4f 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 OMIT LOAD EXTENS +| 3248: 49 4f 4e 58 52 54 52 49 4d 1f 1e 05 00 33 0f 19 IONXRTRIM....3.. +| 3264: 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 30 MAX MEMORY=50000 +| 3280: 30 30 30 58 42 49 4e 41 52 59 1f 1d 05 00 33 0f 000XBINARY....3. +| 3296: 19 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 .MAX MEMORY=5000 +| 3312: 30 30 30 30 58 4e 4f 43 41 53 45 1e 1c 05 00 33 0000XNOCASE....3 +| 3328: 0f 17 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 ..MAX MEMORY=500 +| 3344: 30 30 30 30 30 58 52 54 53 49 4d 18 1b 05 00 25 00000XRTSIM....% +| 3360: 0f 19 45 4e 41 42 4c 45 20 52 54 52 45 45 58 42 ..ENABLE RTREEXB +| 3376: 49 4e 41 52 59 18 1a 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3392: 4c 45 20 52 54 52 45 45 58 4e 4f 43 41 53 45 17 LE RTREEXNOCASE. +| 3408: 19 05 00 25 0f 17 45 4e 41 42 4c 45 20 52 54 52 ...%..ENABLE RTR +| 3424: 45 45 58 52 54 52 49 4d 1a 18 05 00 29 0f 19 45 EEXRTRIM....)..E +| 3440: 4e 41 42 4c 45 20 4d 45 4d 53 59 53 35 58 42 49 NABLE MEMSYS5XBI +| 3456: 4e 41 52 59 1a 17 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3472: 45 20 4d 45 4d 53 59 53 35 58 4e 4f 43 41 53 45 E MEMSYS5XNOCASE +| 3488: 19 16 05 00 29 0f 17 45 4e 41 42 4c 45 20 4d 45 ....)..ENABLE ME +| 3504: 4d 53 59 53 35 58 52 54 52 49 4d 18 15 05 00 25 MSYS5XRTRIM....% +| 3520: 0f 19 45 4e 41 42 4c 45 20 4a 53 4f 4e 31 58 42 ..ENABLE JSON1XB +| 3536: 49 4e 41 52 59 18 14 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3552: 4c 45 20 4a 53 4f 4e 31 58 4e 4f 43 41 53 45 17 LE JSON1XNOCASE. +| 3568: 13 05 00 25 0f 17 45 4e 41 52 4c 45 20 4a 53 4f ...%..ENARLE JSO +| 3584: 4e 31 58 52 54 52 49 4d 1a 12 05 00 29 0f 19 45 N1XRTRIM....)..E +| 3600: 4e 41 42 4c 45 20 47 45 4f 50 4f 4c 59 58 42 49 NABLE GEOPOLYXBI +| 3616: 4e 41 52 59 1a 11 05 00 29 0f 19 45 4e 41 1e 4c NARY....)..ENA.L +| 3632: 45 20 47 45 4f 50 4f 4c 59 58 4e 4f 43 41 53 45 E GEOPOLYXNOCASE +| 3648: 19 10 05 00 29 0f 17 45 4e 41 42 4c 45 20 47 55 ....)..ENABLE GU +| 3664: 4f 50 4f 4c 59 58 52 54 52 49 4d 17 0f 05 00 23 OPOLYXRTRIM....# +| 3680: 0f 19 45 4e 41 42 4c 45 20 46 54 53 35 58 42 49 ..ENABLE FTS5XBI +| 3696: 4e 41 52 59 17 0e 05 00 23 0f 19 45 4e 41 42 4c NARY....#..ENABL +| 3712: 45 20 46 54 53 35 58 4e 4f 43 41 53 45 16 0d 05 E FTS5XNOCASE... +| 3728: 00 23 0f 17 45 4e 41 42 4c 45 20 46 54 59 e5 58 .#..ENABLE FTY.X +| 3744: 52 54 52 49 4d 17 0c 05 00 23 0f 19 45 4e 41 42 RTRIM....#..ENAB +| 3760: 4c 45 20 46 54 53 34 58 42 49 4e 41 52 59 17 0b LE FTS4XBINARY.. +| 3776: 05 00 23 0f 19 45 4e 41 42 4c 45 20 46 54 53 35 ..#..ENABLE FTS5 +| 3792: 58 4e 4f 43 40 53 45 16 0a 05 00 23 0f 17 45 4e XNOC@SE....#..EN +| 3808: 41 42 4c 45 20 46 54 53 34 58 52 54 52 49 4d 1e ABLE FTS4XRTRIM. +| 3824: 09 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3840: 54 41 54 20 56 54 41 42 58 42 49 4e 41 52 59 1e TAT VTABXBINARY. +| 3856: 08 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3872: 54 41 54 20 56 54 41 42 58 4e 4f 43 41 53 45 1d TAT VTABXNOCASE. +| 3888: 07 05 00 31 0f 17 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3904: 54 41 54 20 56 54 41 42 58 52 54 52 49 4d 11 06 TAT VTABXRTRIM.. +| 3920: 05 00 17 0f 19 44 45 42 55 47 58 42 49 4e 41 52 .....DEBUGXBINAR +| 3936: 59 11 05 05 00 17 0f 19 44 45 42 55 47 58 4e 4f Y.......DEBUGXNO +| 3952: 43 41 53 45 10 04 05 00 17 0f 17 44 45 42 55 47 CASE.......DEBUG +| 3968: 58 52 54 52 49 4d 27 03 05 00 43 0f 19 43 4f 4d XRTRIM'...C..COM +| 3984: 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e 30 20 PILER=gcc-5.4.0 +| 4000: 32 30 31 36 30 36 30 39 58 42 49 4e 41 52 59 27 20160609XBINARY' +| 4016: 02 05 00 43 0f 19 43 4f 4d 50 49 4c 45 52 3d 67 ...C..COMPILER=g +| 4032: 63 63 25 75 2e 34 2f 30 20 32 30 31 36 30 36 30 cc%u.4/0 2016060 +| 4048: 39 58 4e 4f 43 41 53 45 26 01 05 00 43 0f 17 43 9XNOCASE&...C..C +| 4064: 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e OMPILER=gcc-5.4. +| 4080: 30 20 32 30 31 36 30 36 30 39 58 52 54 52 49 4d 0 20160609XRTRIM +| page 4 offset 12288 +| 0: 0d 00 00 00 00 10 00 00 00 00 00 00 00 00 00 00 ................ +| page 5 offset 16384 +| 0: 0d 00 00 00 02 0b a0 00 0c ad 0b a0 00 00 00 00 ................ +| 2976: 82 0a 02 08 08 09 08 08 17 84 06 30 20 32 35 33 ...........0 253 +| 2992: 00 01 30 04 25 06 1b 00 00 08 32 30 31 36 30 36 ..0.%.....201606 +| 3008: 30 39 03 25 07 00 00 01 34 03 25 05 00 00 01 35 09.%....4.%....5 +| 3024: 03 25 04 00 01 07 30 30 30 30 30 30 30 03 25 1a .%....0000000.%. +| 3040: 00 00 08 63 6f 6d 70 69 6c 65 72 03 25 02 00 00 ...compiler.%... +| 3056: 06 64 62 73 74 61 74 03 25 0a 00 01 04 65 62 75 .dbstat.%....ebu +| 3072: 67 03 25 08 00 00 06 65 6e 61 62 6c 65 09 25 09 g.%....enable.%. +| 3088: 05 04 04 04 04 04 00 01 08 78 74 65 6e 73 69 6f .........xtensio +| 3104: 6e 03 25 1d 00 00 04 66 74 73 34 03 25 0d 00 03 n.%....fts4.%... +| 3120: 01 35 03 25 0f 00 00 03 67 63 63 03 25 03 00 01 .5.%....gcc.%... +| 3136: 06 65 6f 70 6f 6c 79 03 25 11 00 00 05 6a 73 6f .eopoly.%....jso +| 3152: 6e 31 03 25 13 41 00 04 6c 6f 61 64 03 25 1c 00 n1.%.A..load.%.. +| 3168: 00 03 6d 61 78 03 25 18 00 01 05 65 6d 6f 72 79 ..max.%....emory +| 3184: 03 25 19 00 03 04 73 79 73 35 03 25 15 00 00 04 .%....sys5.%.... +| 3200: 6f 6d 69 74 03 25 1b 00 00 05 72 74 72 65 65 03 omit.%....rtree. +| 3216: 25 17 00 00 0a 74 68 72 65 61 64 73 61 56 65 03 %....threadsaVe. +| 3232: 25 1e 00 00 04 76 74 61 62 03 25 0b 00 86 50 01 %....vtab.%...P. +| 3248: 08 08 08 08 08 17 8d 12 30 20 38 33 35 00 01 30 ........0 835..0 +| 3264: 12 01 06 00 01 06 00 01 06 00 1f 03 00 01 03 00 ................ +| 3280: 01 03 00 00 08 32 30 31 36 30 36 30 39 09 01 07 .....20160609... +| 3296: 00 01 07 00 01 07 00 00 01 34 09 01 05 00 01 05 .........4...... +| 3312: 00 01 05 00 00 01 35 09 01 04 00 01 04 00 01 04 ......5......... +| 3328: 00 01 07 30 30 30 30 30 30 30 09 1c 04 00 01 04 ...0000000...... +| 3344: 00 01 04 00 00 06 62 69 6e 61 72 79 3c 03 02 02 ......binary<... +| 3360: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3376: 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 ................ +| 3392: 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03 ................ +| 3408: 01 02 02 00 03 01 02 02 00 00 08 63 6f 6d 70 69 ...........compi +| 3424: 6c 65 72 09 01 02 00 01 02 00 01 02 00 00 06 64 ler............d +| 3440: 62 73 74 61 74 09 f2 03 00 01 03 00 01 03 00 01 bstat........... +| 3456: 04 65 62 75 67 09 04 02 00 01 02 00 01 02 00 00 .ebug........... +| 3472: 06 65 6e 61 62 6c 65 3f 07 02 00 01 02 00 01 02 .enable?........ +| 3488: 00 01 02 00 01 02 00 01 02 00 01 02 00 57 02 00 .............W.. +| 3504: 01 02 00 01 01 00 01 02 00 11 02 00 01 02 10 01 ................ +| 3520: 02 00 01 02 00 01 02 00 01 02 00 01 02 00 01 02 ................ +| 3536: 00 01 02 00 01 02 00 01 08 78 74 64 6e 73 69 6f .........xtdnsio +| 3552: 6e 09 1f 04 00 01 04 00 01 03 00 00 04 66 74 73 n............fts +| 3568: 34 09 0a 03 00 01 03 00 01 03 00 03 01 35 09 0d 4............5.. +| 3584: 03 00 01 03 00 01 03 00 00 03 57 63 63 09 01 03 ..........Wcc... +| 3600: 00 01 03 00 01 03 00 01 06 65 6f 70 6f 6c 79 09 .........eopoly. +| 3616: 10 03 00 01 03 00 01 03 00 00 05 6a 73 6f 6e 31 ...........json1 +| 3632: 09 13 03 00 01 03 00 01 03 00 00 04 6c 6f 61 64 ............load +| 3648: 09 1f 03 00 01 03 00 01 03 00 00 03 6d 61 78 09 ............max. +| 3664: 1c 02 00 01 02 00 01 02 00 01 05 65 6d 6f 72 79 ...........emory +| 3680: 09 1c 03 00 01 03 00 01 03 00 03 04 73 79 73 35 ............sys5 +| 3696: 09 16 03 00 01 03 00 01 03 00 00 06 6e 6f 63 61 ............noca +| 3712: 73 65 3c 02 01 02 02 00 03 01 02 02 00 03 01 02 se<............. +| 3728: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3744: 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 ................ +| 3760: 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 00 ................ +| 3776: 04 6f 6d 69 74 09 1f 02 00 01 02 00 01 02 00 00 .omit........... +| 3792: 05 72 74 72 65 65 09 19 03 00 01 03 00 01 03 00 .rtree.......... +| 3808: 03 02 69 6d 3c 01 01 02 02 00 03 01 02 02 00 03 ..im<........... +| 3824: 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01 ................ +| 3840: 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 ................ +| 3856: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3872: 00 00 0a 74 68 72 65 61 64 73 61 66 65 09 22 02 ...threadsafe... +| 3888: 00 01 02 00 02 02 00 00 04 76 74 61 62 09 07 04 .........vtab... +| 3904: 00 01 04 00 01 04 00 00 01 78 b4 01 01 01 01 02 .........x...... +| 3920: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| 3936: 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 ................ +| 3952: 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 ................ +| 3968: 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 ................ +| 3984: 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 ................ +| 4000: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| 4016: 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 ................ +| 4032: 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 ................ +| 4048: 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 ................ +| 4064: 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 ................ +| 4080: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 02 0f f5 00 0f fb 0f f5 00 00 00 00 ................ +| 4080: 00 00 00 00 00 05 04 08 09 01 02 04 04 08 08 09 ................ +| page 7 offset 24576 +| 0: 0d 00 00 00 05 0f b8 00 0f f4 0f e9 0f d6 0f c7 ................ +| 16: 0f b8 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 4064: 00 00 00 00 00 0d 05 02 23 61 75 74 6f 6d 65 72 ........#automer +| 4080: 67 65 3d 35 0d 04 02 23 6d 65 72 67 65 3d 31 30 ge=5...#merge=10 +| page 8 offset 28672 +| 0: 30 2c 38 11 03 02 2b 69 6e 74 65 67 72 69 74 79 0,8...+integrity +| 16: 2d 63 68 65 63 6b 09 02 02 1b 72 65 62 75 69 6c -check....rebuil +| 32: 64 0a 01 02 1d 6f 70 74 69 6d 69 7a 65 00 00 00 d....optimize... +| end crash-4ce32d0608aff1.db +}]} {} + +do_catchsql_test 18.1 { + SELECT quote(matchinfo(t1,'pcxybs'))==0 FROM t1 WHERE b MATCH 'e*'; +} {0 {}} + +#------------------------------------------------------------------------- +reset_db +do_test 19.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 28672 pagesize 4096 filename crash-526ea445f41c02.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 07 .....@ ........ +| 32: 00 00 00 02 00 00 00 01 00 00 00 07 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 96: 00 00 00 00 0d 0e b1 00 06 0d a4 00 0f 8d 0f 21 ...............! +| 112: 0e b9 0d c8 0e 7e 0d a4 00 00 00 00 00 00 00 00 .....~.......... +| 3488: 00 00 00 00 22 07 06 17 11 11 01 31 74 61 62 6c ...........1tabl +| 3504: 65 74 32 74 32 07 43 52 45 41 54 45 20 54 41 42 et2t2.CREATE TAB +| 3520: 4c 45 20 74 32 28 78 29 81 33 05 07 17 1f 1f 01 LE t2(x).3...... +| 3536: 82 35 74 61 62 6c 65 74 31 5f 73 65 67 64 69 72 .5tablet1_segdir +| 3552: 74 31 5f 73 65 67 64 69 72 05 43 52 45 41 54 45 t1_segdir.CREATE +| 3568: 20 54 41 42 4c 45 20 27 74 31 5f 73 65 67 64 69 TABLE 't1_segdi +| 3584: 72 27 28 6c 65 76 65 6c 20 49 4e 54 45 47 45 52 r'(level INTEGER +| 3600: 2c 69 64 78 20 49 4e 54 45 47 45 52 2c 73 74 61 ,idx INTEGER,sta +| 3616: 72 74 5f 62 6c 6f 63 6b 20 49 4e 54 45 47 45 52 rt_block INTEGER +| 3632: 2c 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 ,leaves_end_bloc +| 3648: 6b 20 49 4e 54 45 47 45 52 2c 65 6e 64 5f 62 6c k INTEGER,end_bl +| 3664: 6f 63 6b 20 49 4e 54 45 47 45 52 2c 72 6f 6f 74 ock INTEGER,root +| 3680: 20 42 4c 4f 42 2c 50 52 49 4d 41 52 59 20 4b 45 BLOB,PRIMARY KE +| 3696: 59 28 6c 65 76 65 6c 2c 20 69 64 78 29 29 31 06 Y(level, idx))1. +| 3712: 06 17 45 1f 01 00 69 6e 64 65 78 73 71 6c 69 74 ..E...indexsqlit +| 3728: 65 5f 61 75 74 6f 69 6e 64 65 78 5f 74 31 5f 73 e_autoindex_t1_s +| 3744: 65 67 64 69 72 5f 31 74 31 5f 73 65 67 64 69 72 egdir_1t1_segdir +| 3760: 06 0f c7 00 08 00 00 00 00 66 04 07 17 23 23 01 .........f...##. +| 3776: 81 13 74 61 62 6c 65 74 31 5f 73 65 67 6d 65 6e ..tablet1_segmen +| 3792: 74 73 74 31 5f 73 65 67 6d 65 6e 74 73 04 43 52 tst1_segments.CR +| 3808: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 73 EATE TABLE 't1_s +| 3824: 65 67 6d 65 6e 74 73 27 28 62 6c 6f 63 6b 69 64 egments'(blockid +| 3840: 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 INTEGER PRIMARY +| 3856: 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 KEY, block BLOB +| 3872: 29 6a 03 07 17 21 21 01 81 1f 74 61 62 6c 65 74 )j...!!...tablet +| 3888: 31 5f 63 6f 6e 74 65 6e 74 74 31 5f 63 6f 6e 74 1_contentt1_cont +| 3904: 65 6e 74 03 43 52 45 41 54 45 20 54 41 42 4c 45 ent.CREATE TABLE +| 3920: 20 27 74 31 5f 63 6f 6e 74 65 6e 74 27 28 64 6f 't1_content'(do +| 3936: 63 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d cid INTEGER PRIM +| 3952: 41 52 59 20 4b 45 59 2c 20 27 63 30 61 27 2c 20 ARY KEY, 'c0a', +| 3968: 27 63 31 62 27 2c 20 27 63 32 63 27 29 38 02 06 'c1b', 'c2c')8.. +| 3984: 17 11 11 08 5f 74 61 62 6c 65 74 31 74 41 43 52 ...._tablet1tACR +| 4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 33 LE t1 USING fts3 +| 4032: 28 61 2c 62 2c 63 29 00 00 00 39 00 00 00 00 00 (a,b,c)...9..... +| page 3 offset 8192 +| 0: 0d 00 00 00 25 0b 48 00 0f d8 0f af 0f 86 0f 74 ....%.H........t +| 16: 0f 61 0f 4e 0f 2f 0f 0f 0e ef 0e d7 0e be 0e a5 .a.N./.......... +| 32: 0e 8d 0e 74 0e 5b 0e 40 0e 24 0e 08 0d ef 0d d5 ...t.[.@.$...... +| 48: 0d bb 0d a0 0d 84 0d 68 0d 4f 0d 35 0d 1b 0c fb .......h.O.5.... +| 64: 0c da 0c b9 0c 99 0c 78 0c 57 0c 3e 0c 24 0c 0a .......x.W.>.$.. +| 80: 0b 48 00 00 00 00 00 00 00 00 00 00 00 00 00 00 .H.............. +| 2880: 00 00 00 00 00 00 00 00 81 3f 25 06 00 82 7f 00 .........?%..... +| 2896: 00 43 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2f .COMPILER=gcc-5/ +| 2912: 34 2e 30 20 32 30 31 36 30 36 30 39 20 44 45 42 4.0 20160609 DEB +| 2928: 55 47 20 45 4e 41 42 4c 45 20 44 42 53 54 41 54 UG ENABLE DBSTAT +| 2944: 20 56 54 41 42 20 45 4e 41 42 4c 45 20 46 54 53 VTAB ENABLE FTS +| 2960: 34 20 45 4e 41 42 4c 45 20 46 54 53 35 20 45 4e 4 ENABLE FTS5 EN +| 2976: 41 42 4c 45 20 47 45 4f 50 4f 4c 59 20 45 4e 41 ABLE GEOPOLY ENA +| 2992: 42 4c 45 20 4a 53 4f 4e 31 20 45 4e 41 42 4c 45 BLE JSON1 ENABLE +| 3008: 20 4d 45 4d 53 59 53 35 20 45 4e 41 42 4c 45 20 MEMSYS5 ENABLE +| 3024: 52 54 52 45 45 20 4d 41 58 20 4d 45 4d 4f 52 59 RTREE MAX MEMORY +| 3040: 3d 35 30 30 30 30 30 30 30 20 4f 4d 49 54 20 4c =50000000 OMIT L +| 3056: 4f 41 44 20 45 58 54 45 4e 53 49 4f 4e 20 54 48 OAD EXTENSION TH +| 3072: 52 45 41 44 53 41 46 45 3d 30 18 24 05 00 25 0f READSAFE=0.$..%. +| 3088: 19 54 48 52 45 41 44 53 41 46 45 3d 30 58 42 49 .THREADSAFE=0XBI +| 3104: 4e 41 52 59 18 23 05 00 25 0f 19 54 48 52 45 41 NARY.#..%..THREA +| 3120: 44 53 41 46 45 3d 30 58 4e 4f 43 41 53 45 17 22 DSAFE=0XNOCASE.. +| 3136: 05 00 25 0f 17 54 48 52 45 41 44 53 41 46 45 3d ..%..THREADSAFE= +| 3152: 30 58 52 54 52 49 4d 1f 21 05 00 33 0f 19 4f 4d 0XRTRIM.!..3..OM +| 3168: 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 4f IT LOAD EXTENSIO +| 3184: 4e 58 42 49 4e 41 52 59 1f 20 05 00 33 0f 19 4f NXBINARY. ..3..O +| 3200: 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 MIT LOAD EXTENSI +| 3216: 4f 4e 58 4e 4f 43 41 53 45 1e 1f 05 00 33 0f 17 ONXNOCASE....3.. +| 3232: 4f 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 OMIT LOAD EXTENS +| 3248: 49 4f 4e 58 52 54 52 49 4d 1f 1e 05 00 33 0f 19 IONXRTRIM....3.. +| 3264: 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 30 MAX MEMORY=50000 +| 3280: 30 30 30 58 42 49 4e 41 52 59 1f 1d 05 00 33 0f 000XBINARY....3. +| 3296: 19 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 .MAX MEMORY=5000 +| 3312: 30 30 30 30 58 4e 4f 43 41 53 45 1e 1c 05 00 33 0000XNOCASE....3 +| 3328: 0f 17 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 ..MAX MEMORY=500 +| 3344: 30 30 30 30 30 58 52 54 52 49 4d 18 1b 05 00 25 00000XRTRIM....% +| 3360: 0f 19 45 4e 41 42 4c 45 20 52 54 52 45 45 58 42 ..ENABLE RTREEXB +| 3376: 49 4e 41 52 59 18 1a 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3392: 4c 45 20 52 54 52 45 45 58 4e 4f 43 41 53 45 17 LE RTREEXNOCASE. +| 3408: 19 05 00 25 0f 17 45 4e 41 42 4c 45 20 52 54 52 ...%..ENABLE RTR +| 3424: 45 45 58 52 54 52 49 4d 1a 18 05 00 29 0f 19 45 EEXRTRIM....)..E +| 3440: 4e 41 42 4c 45 20 4d 45 4d 53 59 53 35 58 42 49 NABLE MEMSYS5XBI +| 3456: 4e 41 52 59 1a 17 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3472: 45 20 4d 45 4d 53 59 53 35 58 4e 4f 43 41 53 45 E MEMSYS5XNOCASE +| 3488: 19 16 05 00 29 0f 17 45 4e 41 42 4c 45 20 4d 45 ....)..ENABLE ME +| 3504: 4d 53 59 53 35 58 52 54 52 49 4d 18 15 05 00 25 MSYS5XRTRIM....% +| 3520: 0f 19 45 4e 41 42 4c 45 20 4a 53 4f 4e 31 58 42 ..ENABLE JSON1XB +| 3536: 49 4e 41 52 59 18 14 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3552: 4c 45 20 4a 53 4f 4e 31 58 4e 4f 43 41 53 45 17 LE JSON1XNOCASE. +| 3568: 13 05 00 25 0f 17 45 4e 41 42 4c 45 20 4a 53 4f ...%..ENABLE JSO +| 3584: 4e 31 58 52 54 52 49 4d 1a 12 05 00 29 0f 19 45 N1XRTRIM....)..E +| 3600: 4e 41 42 4c 45 20 47 45 4f 50 4f 4c 59 58 42 49 NABLE GEOPOLYXBI +| 3616: 4e 41 52 59 1a 11 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3632: 45 20 47 45 4f 50 4f 4c 59 58 4e 4f 43 41 53 45 E GEOPOLYXNOCASE +| 3648: 19 10 05 00 29 0f 17 45 4e 41 42 4c 45 20 47 45 ....)..ENABLE GE +| 3664: 4f 50 4f 4c 59 58 52 54 52 49 4d 17 0f 05 00 23 OPOLYXRTRIM....# +| 3680: 0f 19 45 4e 41 42 4c 45 20 46 54 53 35 58 42 49 ..ENABLE FTS5XBI +| 3696: 4f 41 52 59 17 0e 05 00 23 0f 19 45 4e 41 42 4c OARY....#..ENABL +| 3712: 45 20 46 54 53 35 58 4e 4f 43 41 53 45 16 0d 05 E FTS5XNOCASE... +| 3728: 00 23 0f 17 45 4e 41 42 4c 45 20 46 54 53 35 58 .#..ENABLE FTS5X +| 3744: 52 54 52 49 4d 17 0c 05 00 23 0f 19 45 4e 41 42 RTRIM....#..ENAB +| 3760: 4c 45 20 46 54 53 34 58 42 49 4e 41 52 59 17 0b LE FTS4XBINARY.. +| 3776: 05 00 23 0f 19 45 4e 41 42 4d f5 20 46 54 53 34 ..#..ENABM. FTS4 +| 3792: 58 4e 4f 43 41 53 45 16 0a 05 00 23 0f 17 45 4e XNOCASE....#..EN +| 3808: 41 52 4c 45 20 46 54 53 34 58 52 54 52 49 4d 1e ARLE FTS4XRTRIM. +| 3824: 09 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3840: 54 41 54 20 56 54 41 42 58 42 49 4e 41 52 59 1e TAT VTABXBINARY. +| 3856: 08 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3872: 54 41 54 20 56 54 41 42 58 4e 4f 43 41 53 45 1d TAT VTABXNOCASE. +| 3888: 07 05 00 31 0f 17 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3904: 54 41 54 20 56 54 41 42 58 52 54 52 49 4d 11 06 TAT VTABXRTRIM.. +| 3920: 05 00 17 0f 19 44 45 42 55 47 58 42 49 4e 41 52 .....DEBUGXBINAR +| 3936: 59 11 05 05 00 17 0f 19 44 45 42 55 47 58 4e 4f Y.......DEBUGXNO +| 3952: 43 41 53 45 10 04 05 00 17 0f 17 44 45 42 55 47 CASE.......DEBUG +| 3968: 58 52 54 52 49 4d 27 03 05 00 43 0f 19 43 4f 4d XRTRIM'...C..COM +| 3984: 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e 30 20 PILER=gcc-5.4.0 +| 4000: 32 30 31 36 30 36 30 39 58 42 49 4e 41 52 59 27 20160609XBINARY' +| 4016: 02 04 00 43 0f 19 43 4f 4d 50 49 4c 45 52 3d 67 ...C..COMPILER=g +| 4032: 63 63 2d 35 2e 34 2e 30 20 32 30 31 36 30 36 30 cc-5.4.0 2016060 +| 4048: 39 58 4e 4f 43 41 53 45 26 01 05 00 43 0f 17 43 9XNOCASE&...C..C +| 4064: 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e OMPILER=gcc-5.4. +| 4080: 30 20 32 30 31 36 30 36 30 39 58 52 54 52 49 4d 0 20160609XRTRIM +| page 4 offset 12288 +| 0: 0d 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| page 5 offset 16384 +| 0: 0d 00 00 00 02 0b a0 00 0c ad 0b a0 00 00 00 00 ................ +| 2976: 82 0a 02 08 08 09 08 08 17 84 06 30 20 32 35 33 ...........0 253 +| 2992: 00 01 30 04 25 06 1b 00 00 08 32 30 31 36 30 36 ..0.%.....201606 +| 3008: 30 39 03 25 07 00 00 01 34 03 25 05 00 00 01 35 09.%....4.%....5 +| 3024: 03 25 04 00 01 07 30 30 30 30 30 30 30 03 25 1a .%....0000000.%. +| 3040: 00 00 08 63 6f 6d 70 69 6c 65 72 03 25 02 00 00 ...compiler.%... +| 3056: 06 64 62 73 74 61 74 03 25 0a 00 01 04 65 62 75 .dbstat.%....ebu +| 3072: 67 03 25 08 00 00 06 65 6e 61 62 6c 65 09 25 09 g.%....enable.%. +| 3088: 05 04 04 04 04 04 00 01 08 78 74 65 6e 73 69 6f .........xtensio +| 3104: 6e 03 25 1d 00 00 04 66 74 73 34 03 25 0d 00 03 n.%....fts4.%... +| 3120: 01 35 03 25 0f 00 00 03 67 63 63 03 25 03 00 01 .5.%....gcc.%... +| 3136: 06 65 6f 70 6f 6c 79 03 25 11 00 00 05 6a 73 6f .eopoly.%....jso +| 3152: 6e 31 03 25 13 00 00 04 6c 6f 61 64 03 25 1c 00 n1.%....load.%.. +| 3168: 00 03 6d 61 78 03 25 18 00 01 05 65 6d 6f 72 79 ..max.%....emory +| 3184: 03 25 19 00 03 04 73 79 73 35 03 25 15 00 00 04 .%....sys5.%.... +| 3200: 6f 6d 69 74 03 25 1b 00 00 05 72 74 72 65 65 03 omit.%....rtree. +| 3216: 25 17 00 00 0a 74 68 72 65 61 64 73 61 66 65 03 %....threadsafe. +| 3232: 25 1e 00 00 04 76 74 61 62 03 25 0b 00 86 50 01 %....vtab.%...P. +| 3248: 08 08 08 08 08 17 8d 12 30 20 38 33 35 00 01 30 ........0 835..0 +| 3264: 12 01 06 00 01 06 00 01 06 00 1f 03 00 01 03 00 ................ +| 3280: 01 03 00 00 08 32 30 31 36 30 36 30 39 09 01 07 .....20160609... +| 3296: 00 01 07 00 01 07 00 00 01 34 09 01 05 00 01 05 .........4...... +| 3312: 00 01 05 00 00 01 35 09 01 04 00 01 04 00 01 04 ......5......... +| 3328: 00 01 07 30 30 30 30 30 30 30 09 1c 04 00 01 04 ...0000000...... +| 3344: 00 01 04 00 00 06 62 69 6e 61 72 79 3c 03 01 02 ......binary<... +| 3360: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3376: 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 ................ +| 3392: 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03 ................ +| 3408: 01 02 02 00 03 01 02 02 00 00 08 63 6f 6d 70 69 ...........compi +| 3424: 6c 65 72 09 01 02 00 01 02 00 01 02 00 00 06 64 ler............d +| 3440: 62 73 74 61 74 09 07 03 00 01 03 00 01 03 00 01 bstat........... +| 3456: 04 65 62 75 67 09 04 02 00 01 02 00 01 02 00 00 .ebug........... +| 3472: 06 65 6e 61 62 6c 65 3f 07 02 00 01 02 00 01 02 .enable?........ +| 3488: 00 01 02 00 01 02 00 01 02 00 01 02 00 01 02 00 ................ +| 3504: 01 02 00 01 02 00 01 02 00 01 02 00 01 02 00 01 ................ +| 3520: 02 00 01 02 00 01 02 00 01 02 00 01 02 00 01 02 ................ +| 3536: 00 01 02 00 01 02 00 01 08 78 74 65 6e 73 69 6f .........xtensio +| 3552: 6e 09 1f 04 00 01 04 00 01 04 00 00 04 66 74 73 n............fts +| 3568: 34 09 0a 03 00 01 03 00 01 03 00 03 01 35 09 0d 4............5.. +| 3584: 03 00 01 03 00 01 03 00 00 03 67 63 63 09 01 03 ..........gcc... +| 3600: 00 01 03 00 01 03 00 01 06 65 6f 70 6f 6c 79 09 .........eopoly. +| 3616: 10 03 00 01 03 00 01 03 00 00 05 6a 73 6f 6e 31 ...........json1 +| 3632: 09 13 03 00 01 03 00 01 03 00 00 04 6c 6f 61 64 ............load +| 3648: 09 1f 03 00 01 03 00 01 03 00 00 03 6d 61 78 09 ............max. +| 3664: 1c 02 00 01 02 00 01 02 00 01 05 65 6d 6f 72 79 ...........emory +| 3680: 09 1c 03 00 01 03 00 01 03 00 03 04 73 79 73 35 ............sys5 +| 3696: 09 16 03 00 01 03 00 01 03 00 00 06 6e 6f 63 61 ............noca +| 3712: 73 65 3c 02 01 02 02 00 03 01 02 02 00 03 01 02 se<............. +| 3728: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3744: 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 ................ +| 3760: 03 01 02 02 00 13 01 02 02 00 03 01 02 02 00 00 ................ +| 3776: 04 6f 6d 69 74 09 1f 02 00 01 12 00 01 02 00 00 .omit........... +| 3792: 05 72 74 72 65 65 09 19 03 00 01 81 00 01 03 00 .rtree.......... +| 3808: 03 02 69 6d 3c 01 01 02 02 00 03 01 02 02 00 03 ..im<........... +| 3824: 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01 ................ +| 3840: 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 ................ +| 3856: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3872: 00 00 0a 74 68 72 65 61 64 73 61 66 65 09 22 02 ...threadsafe... +| 3888: 00 01 02 00 01 02 00 00 04 76 74 61 62 09 07 04 .........vtab... +| 3904: 00 01 04 00 01 04 00 00 01 78 b4 01 01 01 01 02 .........x...... +| 3920: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| 3936: 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 ................ +| 3952: 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 ................ +| 3968: 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 ................ +| 3984: 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 ................ +| 4000: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| 4016: 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 ................ +| 4032: 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 ................ +| 4048: 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 ................ +| 4064: 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 ................ +| 4080: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 02 0f f5 00 0f fb 0f f5 00 00 00 00 ................ +| 4080: 00 00 00 00 00 05 04 08 09 01 02 04 04 08 08 09 ................ +| page 7 offset 24576 +| 0: 0d 00 00 00 05 0f b8 00 0f f4 0f e9 0f d6 0f c7 ................ +| 16: 0f b8 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 4016: 00 00 00 00 00 00 00 00 0d 05 02 23 61 75 74 6f ...........#auto +| 4032: 6d 65 72 67 65 3d 35 0d 04 02 23 6d 65 72 67 65 merge=5...#merge +| 4048: 3d 31 30 30 2c 38 11 03 02 2b 69 6e 74 65 67 72 =100,8...+integr +| 4064: 69 74 79 2d 63 68 65 63 6b 09 02 02 1b 72 65 62 ity-check....reb +| 4080: 75 69 6c 64 0a 01 02 1d 6f 70 74 69 6d 69 7a 65 uild....optimize +| end crash-526ea445f41c02.db +}]} {} + +do_catchsql_test 19.1 { + PRAGMA writable_schema = 1; + SELECT rowid,a,c,snippet(t1,85101090932165,-1,10) FROM t1 WHERE a MATCH 'rtree'; +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +reset_db +do_test 20.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 28672 pagesize 4096 filename crash-afecd03c862e58.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 07 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 06 00 00 00 04 ................ +| 96: 00 00 00 00 0d 0e ef 00 07 0d 4d 00 0f bd 0f 5f ..........M...._ +| 112: 0e f7 0e 06 0e bc 0d a4 0d 4d 00 00 01 00 00 00 .........M...... +| 3392: 00 00 00 00 00 00 00 00 00 00 00 00 00 55 07 07 .............U.. +| 3408: 17 1b 1b 01 81 01 74 61 62 6c 65 74 31 5f 73 74 ......tablet1_st +| 3424: 61 74 74 31 5f 73 74 61 74 07 43 52 45 41 54 45 att1_stat.CREATE +| 3440: 20 54 41 42 4c 45 20 27 74 31 5f 73 74 61 74 27 TABLE 't1_stat' +| 3456: 28 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d (id INTEGER PRIM +| 3472: 41 52 59 20 4b 45 59 2c 20 76 61 6c 75 65 20 42 ARY KEY, value B +| 3488: 4c 4f 42 29 60 06 07 17 21 21 01 81 0b 74 61 62 LOB)`...!!...tab +| 3504: 6c 65 74 31 5f 64 6f 63 73 69 7a 65 74 31 5f 64 let1_docsizet1_d +| 3520: 6f 63 73 69 7a 65 06 43 52 45 41 54 45 20 54 41 ocsize.CREATE TA +| 3536: 42 4c 45 20 27 74 31 5f 64 6f 63 73 69 7a 65 27 BLE 't1_docsize' +| 3552: 28 64 6f 63 69 64 20 49 4e 54 45 47 45 52 20 50 (docid INTEGER P +| 3568: 52 49 4d 41 52 59 30 4b 45 59 2c 20 73 69 7a 65 RIMARY0KEY, size +| 3584: 20 42 4c 4f 42 29 81 33 04 07 17 1f 1f 01 82 35 BLOB).3.......5 +| 3600: 74 61 62 6c 65 74 31 5f 73 65 67 64 69 72 74 31 tablet1_segdirt1 +| 3616: 5f 73 65 67 64 69 25 04 43 52 45 41 54 45 20 54 _segdi%.CREATE T +| 3632: 41 42 4c 45 20 27 74 31 5f 73 65 67 64 69 72 27 ABLE 't1_segdir' +| 3648: 28 6c 65 76 65 6c 20 49 4e 54 45 47 45 52 2c 69 (level INTEGER,i +| 3664: 64 78 20 49 4e 54 45 47 45 52 2c 73 74 61 72 74 dx INTEGER,start +| 3680: 5f 62 6c 6f 63 6b 20 49 4e 54 45 47 45 52 2c 6c _block INTEGER,l +| 3696: 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 6b 20 eaves_end_block +| 3712: 49 4d 54 45 47 45 52 2c 65 6e 64 5f 62 6c 6f 63 IMTEGER,end_bloc +| 3728: 6b 20 49 4e 54 45 47 45 52 2c 72 6f 6f 74 20 42 k INTEGER,root B +| 3744: 4c 4f 42 2c 50 52 49 4d 41 52 59 20 4b 45 59 28 LOB,PRIMARY KEY( +| 3760: 6c 65 76 65 6c 2c 20 69 64 78 29 29 31 05 06 17 level, idx))1... +| 3776: 45 1f 01 00 69 6e 64 65 78 73 71 6c 69 74 65 5f E...indexsqlite_ +| 3792: 61 75 74 6f 69 6e 64 65 78 5f 74 31 5f 73 65 67 autoindex_t1_seg +| 3808: 64 69 72 5f 31 74 31 5f 73 65 67 64 69 72 05 00 dir_1t1_segdir.. +| 3824: 00 00 08 00 00 00 00 66 03 07 17 23 23 01 81 13 .......f...##... +| 3840: 74 61 62 6c 65 74 31 5f 73 65 67 6d 65 6e 74 73 tablet1_segments +| 3856: 74 31 5f 73 65 67 6d 65 6e 74 73 03 43 52 45 41 t1_segments.CREA +| 3872: 54 45 20 54 41 42 4c 45 20 27 74 31 5f 73 65 67 TE TABLE 't1_seg +| 3888: 6d 65 6e 74 73 27 28 62 6c 6f 63 6b 69 64 20 49 ments'(blockid I +| 3904: 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b NTEGER PRIMARY K +| 3920: 45 59 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 29 5c EY, block BLOB). +| 3936: 02 07 17 21 21 01 81 03 74 61 62 6c 65 74 31 5f ...!!...tablet1_ +| 3952: 63 6f 6e 74 65 6e 74 74 31 5f 63 6f 6e 74 65 6e contentt1_conten +| 3968: 74 02 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 t.CREATE TABLE ' +| 3984: 74 31 5f 63 6f 6e 74 65 6e 74 27 28 64 6f 63 69 t1_content'(doci +| 4000: 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 d INTEGER PRIMAR +| 4016: 59 20 4b 45 59 2c 20 27 63 30 61 27 29 41 01 06 Y KEY, 'c0a')A.. +| 4032: 17 11 11 08 71 74 61 62 6c 65 74 31 74 31 43 52 ....qtablet1t1CR +| 4048: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4064: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 34 LE t1 USING fts4 +| 4080: 28 61 2c 70 72 65 66 69 78 3d 27 31 2c 32 27 29 (a,prefix='1,2') +| page 2 offset 4096 +| 0: 0d 00 00 00 08 0e 1f 00 0f c4 0f 7c 0f 34 0f 07 ...........|.4.. +| 16: 0e c3 0e 97 0e 63 0e 1f 00 00 00 00 00 00 00 00 .....c.......... +| 3600: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 42 ...............B +| 3616: 08 04 00 81 09 73 75 6e 74 20 69 6e 20 63 75 6c .....sunt in cul +| 3632: 70 61 20 71 75 68 20 6f 66 66 69 63 69 61 20 64 pa quh officia d +| 3648: 65 73 65 72 75 6e 74 20 6d 6f 6c 6c 69 74 20 61 eserunt mollit a +| 3664: 6e 69 6d 20 69 64 20 65 73 74 20 6c 61 62 6f 72 nim id est labor +| 3680: 75 6d 2e 32 07 03 01 6b 45 78 63 65 70 74 65 75 um.2...kExcepteu +| 3696: 72 20 73 69 6e 74 20 6f 63 63 61 65 63 61 74 20 r sint occaecat +| 3712: 63 75 70 69 64 61 74 61 74 20 6e 6f 6e 20 70 72 cupidatat non pr +| 3728: 6f 69 64 65 6e 74 2c 2a 06 03 00 5b 63 69 6c 6c oident,*...[cill +| 3744: 75 6d 20 64 6f 6c 6f 72 65 20 65 75 20 66 75 67 um dolore eu fug +| 3760: 69 61 74 20 6e 75 6c 6c 61 20 70 61 72 69 61 74 iat nulla pariat +| 3776: 75 72 2e 43 05 04 00 81 09 44 75 69 73 20 61 75 ur.C.....Duis au +| 3792: 74 65 20 69 72 75 72 65 20 64 6f 6c 6f 72 20 69 te irure dolor i +| 3808: 6e 20 72 65 70 72 65 68 65 6e 64 65 72 69 74 20 n reprehenderit +| 3824: 69 6e 20 76 6f 6c 75 70 74 61 74 65 20 76 65 6c in voluptate vel +| 3840: 69 74 20 65 73 73 65 2b 14 03 00 5d 6e 69 73 69 it esse+...]nisi +| 3856: 20 75 74 20 61 6c 69 71 75 69 70 20 65 78 20 65 ut aliquip ex e +| 3872: 61 20 63 6f 6d 6d 6f 64 6f 20 63 6f 6e 73 65 71 a commodo conseq +| 3888: 75 61 74 2e 46 03 04 00 81 11 55 74 20 65 6e 69 uat.F.....Ut eni +| 3904: 6d 20 61 63 20 6d 69 6e 69 6d 20 76 65 6e 69 61 m ac minim venia +| 3920: 6d 2c 20 71 75 69 73 20 6e 6f 73 74 72 75 64 20 m, quis nostrud +| 3936: 65 78 65 72 63 69 74 61 74 69 6f 6e 20 75 6c 6c exercitation ull +| 3952: 61 6d 63 6f 20 6c 61 62 6f 72 69 73 46 02 04 00 amco laborisF... +| 3968: 81 11 73 65 64 20 64 6f 20 65 69 75 73 6d 6f 64 ..sed do eiusmod +| 3984: 20 74 65 6d 70 6f 72 20 69 6e 63 69 64 69 64 75 tempor incididu +| 4000: 6e 74 20 75 74 20 6c 61 62 6f 72 65 20 65 74 20 nt ut labore et +| 4016: 64 6f 6c 6f 72 65 20 6d 61 67 6e 61 20 61 6c 69 dolore magna ali +| 4032: 71 75 61 2e 3a 01 03 00 7b 4c 6f 72 65 6d 20 69 qua.:....Lorem i +| 4048: 70 73 75 6d 20 64 6f 6c 6f 72 20 73 69 74 20 61 psum dolor sit a +| 4064: 6d 65 74 2c 20 63 6f 6e 73 65 63 74 65 74 75 72 met, consectetur +| 4080: 20 61 64 69 70 69 73 63 69 6e 67 20 65 6c 69 74 adipiscing elit +| page 3 offset 8192 +| 0: 0d 00 00 00 00 10 00 00 00 00 00 00 00 00 00 00 ................ +| page 4 offset 12288 +| 0: 0d 00 00 00 03 0a a6 00 0d 57 0c 4a 0a a6 00 00 .........W.J.... +| 2720: 00 00 00 00 00 00 83 21 03 08 02 08 08 08 17 86 .......!........ +| 2736: 30 08 00 30 20 34 30 32 00 02 61 64 06 01 08 00 0..0 402..ad.... +| 2752: 02 04 00 01 01 6c 06 02 0c 00 02 04 00 01 01 6d .....l.........m +| 2768: 03 01 06 00 01 01 6e 03 08 09 00 01 01 75 03 05 ......n......u.. +| 2784: 03 00 00 02 63 69 03 06 01 00 01 01 6f 07 01 07 ....ci......o... +| 2800: 00 03 07 03 00 01 01 75 06 07 05 00 01 04 00 00 .......u........ +| 2816: 02 64 65 03 08 07 00 01 01 6f 0d 01 04 00 01 03 .de......o...... +| 2832: 09 00 03 05 00 01 03 00 01 01 75 03 05 02 00 00 ..........u..... +| 2848: 02 65 61 03 04 06 00 01 01 69 03 02 04 00 01 01 .ea......i...... +| 2864: 6c 03 01 09 00 01 01 6e 03 03 03 00 01 01 73 06 l......n......s. +| 2880: 05 0b 00 03 0b 00 01 01 74 03 02 09 00 01 01 75 ........t......u +| 2896: 03 06 04 00 01 01 78 09 03 09 00 01 05 00 03 02 ......x......... +| 2912: 00 00 02 66 75 03 06 05 00 00 02 69 64 03 08 0a ...fu......id... +| 2928: 00 01 01 6e 0a 02 06 00 03 06 04 00 03 03 00 01 ...n............ +| 2944: 01 70 03 01 13 00 01 01 72 03 05 04 00 00 02 6c .p......r......l +| 2960: 61 09 02 08 00 01 0b 00 05 0c 00 01 01 6f 03 01 a............o.. +| 2976: 02 00 00 02 6d 61 03 02 0b 00 01 01 69 03 03 05 ....ma......i... +| 2992: 00 01 01 6f 03 08 08 00 00 02 6e 69 03 04 02 00 ...o......ni.... +| 3008: 01 01 6f 06 03 08 00 04 06 00 01 01 75 03 06 06 ..o.........u... +| 3024: 00 00 02 6f 63 03 07 04 00 01 01 66 03 08 06 00 ...oc......f.... +| 3040: 00 02 70 61 03 06 07 00 01 01 72 03 07 07 00 00 ..pa......r..... +| 3056: 02 71 75 06 03 07 00 05 05 00 00 02 72 65 03 05 .qu.........re.. +| 3072: 07 00 00 02 73 65 03 02 02 00 01 01 69 06 01 05 ....se......i... +| 3088: 00 06 03 00 01 01 75 03 08 02 00 00 02 74 65 03 ......u......te. +| 3104: 03 05 00 00 02 72 bc 03 03 0a 00 01 01 74 09 02 .....r.......t.. +| 3120: 07 00 01 02 00 01 03 00 00 02 76 65 06 03 06 00 ..........ve.... +| 3136: 02 0a 00 01 01 6f 03 05 09 00 82 0a 02 08 02 08 .....o.......... +| 3152: 08 08 17 84 02 04 00 30 20 32 35 31 00 01 61 13 .......0 251..a. +| 3168: 01 06 04 00 01 0c 00 01 04 00 01 04 00 01 03 00 ................ +| 3184: 03 09 00 00 01 63 10 01 07 00 03 07 03 00 02 02 .....c.......... +| 3200: 00 01 05 00 01 04 00 00 01 64 11 01 04 00 01 03 .........d...... +| 3216: 09 00 03 02 05 00 01 03 00 02 07 00 00 01 65 1b ..............e. +| 3232: 01 09 00 01 04 07 00 01 03 08 00 01 05 03 00 01 ................ +| 3248: 0b 00 01 04 00 01 02 00 01 0b 00 00 01 66 03 06 .............f.. +| 3264: 05 00 00 01 69 0f 01 03 00 01 06 00 02 04 04 04 ....i........... +| 3280: 00 03 03 09 00 00 01 6c 0c 01 02 00 01 08 00 01 .......l........ +| 3296: 0b 00 05 0c 00 00 01 6d 09 02 0b 00 01 05 00 05 .......m........ +| 3312: 08 00 00 01 6e 0c 03 08 00 01 02 00 02 06 00 01 ....n........... +| 3328: 06 00 f0 01 6f 06 07 04 00 01 06 00 00 01 70 06 ....o.........p. +| 3344: 06 07 00 01 07 00 00 01 71 06 03 07 00 05 05 00 ........q....... +| 3360: 00 01 72 03 05 08 00 00 01 73 0c 01 05 00 01 02 ..r......s...... +| 3376: 00 05 03 00 01 02 00 00 01 74 03 02 05 00 00 01 .........t...... +| 3392: 75 0a 02 07 00 01 02 0a 00 01 03 00 00 01 76 07 u.............v. +| 3408: 03 06 00 02 09 03 00 85 26 01 08 08 08 08 08 17 ........&....... +| 3424: 8a 3e 30 21 36 36 35 00 02 61 64 03 03 04 00 02 .>0!665..ad..... +| 3440: 08 69 70 69 73 63 69 6e 67 03 01 08 00 01 05 6c .ipiscing......l +| 3456: 69 71 75 61 03 02 0c 00 05 02 69 70 03 04 04 00 iqua......ip.... +| 3472: 01 03 6d 65 74 03 01 06 00 01 03 6e 69 6d 03 08 ..met......nim.. +| 3488: 09 00 01 03 75 74 65 03 05 03 00 00 06 63 69 6c ....ute......cil +| 3504: 6c 75 6d 03 06 02 00 01 06 6f 6d 6d 6f 64 6f 03 lum......ommodo. +| 3520: 04 07 00 02 09 6e 73 65 63 74 65 74 75 72 03 01 .....nsectetur.. +| 3536: 07 00 05 04 71 75 61 74 03 04 08 00 01 04 75 6c ....quat......ul +| 3552: 70 61 03 08 04 00 02 07 70 69 64 61 74 61 74 03 pa......pidatat. +| 3568: 07 05 00 00 08 64 65 73 65 72 75 6e 74 03 08 07 .....deserunt... +| 3584: 00 01 01 6f 03 02 03 00 02 03 6c 6f 72 06 01 04 ...o......lor... +| 3600: 00 04 05 00 05 01 65 06 02 0a 00 04 03 00 01 03 ......e......... +| 3616: 75 69 73 03 05 02 00 00 02 65 61 03 04 06 00 01 uis......ea..... +| 3632: 06 69 75 73 6d 6f 64 03 02 04 00 01 03 6c 69 74 .iusmod......lit +| 3648: 03 01 09 00 01 03 6e 69 6d 03 03 03 00 01 03 73 ......nim......s +| 3664: 73 65 03 05 0b 00 02 01 74 03 08 0b 00 01 01 74 se......t......t +| 3680: 03 02 09 00 01 01 75 03 06 04 00 01 01 78 03 04 ......u......x.. +| 3696: 05 00 02 07 63 65 70 74 65 75 72 03 07 02 00 02 ....cepteur..... +| 3712: 0a 65 72 63 69 74 61 74 69 6f 6e 03 03 09 00 00 .ercitation..... +| 3728: 06 66 75 67 69 61 74 03 06 05 00 00 02 69 64 03 .fugiat......id. +| 3744: 08 0a 00 01 01 6e 07 05 06 04 00 03 03 00 02 08 .....n.......... +| 3760: 63 69 64 69 64 75 6e 74 03 02 06 00 01 04 70 73 cididunt......ps +| 3776: 75 6d 03 01 03 00 01 04 72 75 72 65 03 05 04 00 um......rure.... +| 3792: 00 06 6c 61 62 6f 72 65 03 02 08 00 05 02 69 73 ..labore......is +| 3808: 03 03 0b 00 05 02 75 6d 03 08 0c 00 01 04 6f 72 ......um......or +| 3824: 65 6d 03 01 02 00 00 05 6d 61 67 6e 61 03 02 0b em......magna... +| 3840: 00 01 04 69 6e 69 6d 03 03 05 00 01 05 6f 6c 6c ...inim......oll +| 3856: 69 74 03 08 08 00 00 04 6e 69 73 69 03 04 02 00 it......nisi.... +| 3872: 01 02 6f 6e 03 07 06 00 02 05 73 74 72 75 64 03 ..on......strud. +| 3888: 03 08 00 01 04 75 6c 6c 61 03 06 06 00 00 08 6f .....ulla......o +| 3904: 63 63 61 65 63 61 74 03 07 04 00 01 06 66 66 69 ccaecat......ffi +| 3920: 63 69 61 03 08 06 00 00 08 70 61 72 69 61 74 75 cia......pariatu +| 3936: 72 03 06 07 00 01 07 72 6f 69 64 65 6e 74 03 07 r......roident.. +| 3952: 07 00 00 03 71 75 69 03 08 05 00 03 01 73 03 03 ....qui......s.. +| 3968: 07 00 00 0d 72 65 41 72 65 68 65 6e 64 65 72 69 ....reArehenderi +| 3984: 74 03 05 07 00 00 03 73 65 64 03 02 02 00 01 03 t......sed...... +| 4000: 69 6e 74 03 07 03 00 02 01 74 03 01 05 00 01 03 int......t...... +| 4016: 75 6e 74 03 08 02 00 00 06 74 65 6d 70 6f 72 03 unt......tempor. +| 4032: 02 05 00 00 07 75 6c 6c 61 6d 63 6f 03 03 0a 00 .....ullamco.... +| 4048: 01 01 74 09 02 07 00 01 02 00 01 03 00 00 05 76 ..t............v +| 4064: 65 6c 69 74 03 05 0a 00 02 04 6e 69 61 6d 03 03 elit......niam.. +| 4080: 06 00 01 08 70 6c 75 70 74 61 74 65 03 05 09 00 ....pluptate.... +| page 5 offset 16384 +| 0: 0a 00 00 00 03 0f eb 00 0f fb 0f f3 0f eb 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 07 04 02 08 01 ................ +| 4080: 08 00 03 07 04 0a 98 01 04 00 02 04 04 08 08 09 ................ +| page 6 offset 20480 +| 0: 0d 00 00 00 08 0f d0 00 0f fa 0f f4 0f ee 0f e8 ................ +| 16: 0f e2 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 4048: 04 08 03 00 0e 0b 04 07 03 00 0e 06 04 06 03 00 ................ +| 4064: 0e 06 04 05 03 00 0e 0a 04 04 03 00 0e 07 04 03 ................ +| 4080: 03 00 0e 0a 04 02 03 00 0e 0b 04 01 03 00 0e 08 ................ +| page 7 offset 24576 +| 0: 0d 00 00 00 01 0f f7 00 0f f7 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 07 00 03 00 14 08 45 b5 03 .............E.. +| end crash-afecd03c862e58.db +}]} {} + +do_execsql_test 20.1 { + PRAGMA writable_schema = 1; + BEGIN; + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<10) + INSERT INTO t1(a) SELECT randomblob(3000) FROM c; +} + +do_execsql_test 20.2 { + INSERT INTO t1(t1) VALUES('optimize'); +} + +#------------------------------------------------------------------------- +reset_db +do_test 21.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 28672 pagesize 4096 filename crash-18cc014e42e828.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 07 .....@ ........ +| 32: 00 00 00 02 00 00 00 01 00 00 00 07 00 00 00 04 ................ +| 96: 00 00 00 00 0d 0e b1 00 06 0d a4 00 0f 8d 0f 21 ...............! +| 112: 0e b9 0d c8 0e 7e 0d a4 00 00 00 00 00 00 00 00 .....~.......... +| 3488: 00 00 00 00 22 07 06 17 11 11 01 31 74 61 62 6c ...........1tabl +| 3504: 66 74 32 74 32 07 43 52 45 41 54 45 20 54 41 42 ft2t2.CREATE TAB +| 3520: 4c 45 20 74 32 28 78 29 81 33 05 07 17 1f 1f 01 LE t2(x).3...... +| 3536: 82 35 74 61 62 6c 65 74 31 5f 73 75 67 64 69 72 .5tablet1_sugdir +| 3552: 74 31 5f 73 65 67 64 69 72 05 43 52 45 41 54 45 t1_segdir.CREATE +| 3568: 20 54 41 42 4c 45 20 27 74 31 5f 73 65 67 64 69 TABLE 't1_segdi +| 3584: 72 27 28 6c 65 76 65 6c 20 49 4e 54 45 47 45 52 r'(level INTEGER +| 3600: 2c 69 64 78 20 49 4e 54 45 47 45 52 2c 73 74 61 ,idx INTEGER,sta +| 3616: 72 74 5f 62 6c 6f 63 6b 20 49 4e 54 46 47 45 52 rt_block INTFGER +| 3632: 2c 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 ,leaves_end_bloc +| 3648: 6b 20 49 4e 54 45 47 45 52 2c 65 6e 64 5f 62 6c k INTEGER,end_bl +| 3664: 6f 63 6b 20 49 4e 54 45 47 45 62 2c 72 6f 6f 74 ock INTEGEb,root +| 3680: 20 42 4c 4f 42 2c 50 52 49 4d 41 52 59 20 4b 45 BLOB,PRIMARY KE +| 3696: 59 28 6c 65 76 65 6c 2c 20 69 64 78 29 29 31 06 Y(level, idx))1. +| 3712: 06 17 45 1f 01 00 69 6e 64 65 78 73 71 6c 69 74 ..E...indexsqlit +| 3728: 65 5f 61 75 74 6f 69 6e 64 65 78 5f 74 31 5f 73 e_autoindex_t1_s +| 3744: 65 67 64 69 72 5f 31 74 31 5f 73 65 67 64 69 72 egdir_1t1_segdir +| 3760: 06 0f c7 00 08 00 00 00 00 66 04 07 17 23 23 01 .........f...##. +| 3776: 81 13 74 61 62 6c 65 74 31 5f 73 65 67 6d 65 6e ..tablet1_segmen +| 3792: 74 73 74 31 5f 73 65 67 6d 65 6e 74 73 04 43 52 tst1_segments.CR +| 3808: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 73 EATE TABLE 't1_s +| 3824: 65 67 6d 65 6e 74 73 27 28 62 6c 6f 63 6b 69 64 egments'(blockid +| 3840: 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 INTEGER PRIMARY +| 3856: 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 KEY, block BLOB +| 3872: 29 6a 03 07 17 21 21 01 81 1f 74 61 62 6c 65 74 )j...!!...tablet +| 3888: 31 5f 63 6f 6e 74 65 6e 74 74 31 5f 63 6f 6e 74 1_contentt1_cont +| 3904: 65 6e 74 03 43 52 45 41 54 45 20 54 41 42 4c 45 ent.CREATE TABLE +| 3920: 20 27 74 31 5f 63 6f 6e 74 65 6e 74 27 28 64 6f 't1_content'(do +| 3936: 63 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d cid INTEGER PRIM +| 3952: 41 52 59 20 4b 45 59 2c 20 27 63 30 61 27 2c 20 ARY KEY, 'c0a', +| 3968: 27 63 31 62 27 2c 20 27 63 32 63 27 29 38 02 06 'c1b', 'c2c')8.. +| 3984: 17 11 11 08 5f 74 61 62 6c 65 74 31 74 31 43 52 ...._tablet1t1CR +| 4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 33 LE t1 USING fts3 +| 4032: 28 61 2c 62 2c 63 29 00 00 00 00 00 00 00 00 00 (a,b,c)......... +| page 3 offset 8192 +| 0: 0d 00 00 00 25 0b 48 00 0f d8 0f af 0f 86 0f 74 ....%.H........t +| 16: 0f 61 0f 4e 0f 2f 0f 0f 0e ef 0e d7 0e be 0e a5 .a.N./.......... +| 32: 0e 8d 0e 74 0e 5b 0e 40 0e 24 0e 08 0d ef 0d d5 ...t.[.@.$...... +| 48: 0d bb 0d a0 0d 84 0d 68 0d 4e 0d 35 0d 1b 0c fb .......h.N.5.... +| 64: 0c da 0c b9 0c 99 0c 78 0c 57 0c 3e 0c 24 00 00 .......x.W.>.$.. +| 2880: 00 00 00 00 00 00 00 00 81 3f 25 06 00 82 7f 00 .........?%..... +| 2896: 00 43 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2e .COMPILER=gcc-5. +| 2912: 34 23 00 20 32 30 31 36 30 36 30 39 20 44 45 42 4#. 20160609 DEB +| 2928: 55 47 20 45 4e 41 42 4c 45 20 44 42 53 54 41 54 UG ENABLE DBSTAT +| 2944: 20 56 54 41 42 20 45 4e 41 42 4c 45 20 46 43 53 VTAB ENABLE FCS +| 2960: 34 20 45 4e 41 42 4c 45 20 46 54 53 35 20 45 4e 4 ENABLE FTS5 EN +| 2976: 41 42 4c 45 20 47 45 4f 50 4f 4c 59 20 45 4e 41 ABLE GEOPOLY ENA +| 2992: 42 4c 45 20 4a 53 4f 4e 31 20 45 4e 41 42 4c 45 BLE JSON1 ENABLE +| 3008: 20 4d 45 4d 53 59 53 35 20 45 4e 41 42 4c 45 20 MEMSYS5 ENABLE +| 3024: 52 54 52 45 45 20 4d 41 58 20 4d 45 4d 4f 52 59 RTREE MAX MEMORY +| 3040: 3d 35 30 30 30 30 30 30 30 20 4f 4d 49 54 20 4c =50000000 OMIT L +| 3056: 4f 41 44 20 45 58 54 45 4e 53 49 4f 4e 20 54 48 OAD EXTENSION TH +| 3072: 52 45 41 44 53 41 46 45 3d 30 18 24 05 00 25 0f READSAFE=0.$..%. +| 3088: 19 54 48 52 45 41 44 53 41 46 45 3d 30 58 42 49 .THREADSAFE=0XBI +| 3104: 4e 41 52 59 18 23 05 00 25 0f 19 54 48 52 45 41 NARY.#..%..THREA +| 3120: 44 53 41 46 45 3d 30 58 4e 4f 43 41 53 45 17 22 DSAFE=0XNOCASE.. +| 3136: 05 00 25 0f 17 54 48 52 45 41 44 53 41 46 45 3d ..%..THREADSAFE= +| 3152: 30 58 52 54 52 49 4d 20 21 05 00 33 0f 19 4f 4d 0XRTRIM !..3..OM +| 3168: 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 4f IT LOAD EXTENSIO +| 3184: 4e 58 42 49 4e 41 52 59 1f 20 05 00 33 0f 19 4f NXBINARY. ..3..O +| 3200: 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 MIT LOAD EXTENSI +| 3216: 4f 4e 58 4e 4f 43 41 53 45 1e 1f 05 00 33 0f 17 ONXNOCASE....3.. +| 3232: 4f 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 OMIT LOAD EXTENS +| 3248: 49 4f 4e 58 52 54 52 49 4d 1f 1e 05 00 33 0f 19 IONXRTRIM....3.. +| 3264: 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 30 MAX MEMORY=50000 +| 3280: 30 30 30 58 42 49 4e 41 52 59 1f 1d 05 00 33 0f 000XBINARY....3. +| 3296: 19 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 .MAX MEMORY=5000 +| 3312: 30 30 30 30 58 4e 4f 43 41 53 45 1e 1c 05 00 33 0000XNOCASE....3 +| 3328: 0f 17 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 ..MAX MEMORY=500 +| 3344: 30 30 30 30 30 58 52 54 52 49 4d 18 1b 05 00 25 00000XRTRIM....% +| 3360: 0f 19 45 4e 41 42 4c 45 20 52 54 52 45 45 58 42 ..ENABLE RTREEXB +| 3376: 49 4e 41 52 59 18 1a 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3392: 4c 45 20 52 54 52 45 45 58 4e 4f 43 41 53 45 17 LE RTREEXNOCASE. +| 3408: 19 05 00 25 0f 17 45 4e 41 42 4c 45 20 52 54 52 ...%..ENABLE RTR +| 3424: 45 45 58 52 54 52 49 4d 1a 18 05 00 29 0f 19 45 EEXRTRIM....)..E +| 3440: 4e 41 42 4c 45 20 4d 45 4d 53 59 53 35 58 42 49 NABLE MEMSYS5XBI +| 3456: 4e 41 52 59 1a 17 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3472: 45 20 4d 45 4d 53 59 53 35 58 4e 4f 43 41 53 45 E MEMSYS5XNOCASE +| 3488: 19 16 05 00 29 0f 17 45 4e 41 42 4c 45 20 4d 45 ....)..ENABLE ME +| 3504: 4d 53 59 53 35 58 52 54 52 49 4d 18 15 05 00 25 MSYS5XRTRIM....% +| 3520: 0f 19 45 4e 41 42 4c 45 20 4a 53 4f 4e 31 58 42 ..ENABLE JSON1XB +| 3536: 49 4e 41 52 59 18 14 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3552: 4c 45 20 4a 53 4f 4e 31 58 4e 4f 43 41 53 45 17 LE JSON1XNOCASE. +| 3568: 13 05 00 25 0f 17 45 4e 41 42 4c 45 20 4a 53 4f ...%..ENABLE JSO +| 3584: 4e 31 58 52 54 52 49 4d 1a 12 05 00 29 0f 19 45 N1XRTRIM....)..E +| 3600: 4e 41 42 4c 45 20 47 45 4f 50 4f 4c 59 58 42 39 NABLE GEOPOLYXB9 +| 3616: 4e 41 52 59 1a 11 05 00 29 0f 19 45 4e 41 1e 4c NARY....)..ENA.L +| 3632: 45 20 47 45 4f 50 4f 4c 59 58 4e 4f 43 41 53 45 E GEOPOLYXNOCASE +| 3648: 19 10 05 00 29 0f 17 45 4e 41 42 4c 45 20 47 45 ....)..ENABLE GE +| 3664: 4f 50 4f 4c 59 58 52 54 52 49 4d 17 0f 05 00 23 OPOLYXRTRIM....# +| 3680: 0f 19 45 4e 41 42 4c 45 20 46 54 53 35 58 42 49 ..ENABLE FTS5XBI +| 3696: 4e 41 52 59 17 0e 05 00 23 0f 19 45 4e 41 42 4c NARY....#..ENABL +| 3712: 45 20 46 54 53 35 58 4e 4f 43 41 53 45 16 0d 05 E FTS5XNOCASE... +| 3728: 00 23 0f 16 45 4e 41 42 4c 45 20 46 54 53 35 58 .#..ENABLE FTS5X +| 3744: 52 54 52 49 4d 17 0c 05 00 23 0f 19 45 4e 41 42 RTRIM....#..ENAB +| 3760: 4c 45 20 46 54 53 34 58 42 48 4e 41 52 59 17 0b LE FTS4XBHNARY.. +| 3776: 05 00 23 0e 19 45 4e 41 42 4c 45 20 46 54 53 35 ..#..ENABLE FTS5 +| 3792: 58 4e 4f 43 40 53 45 16 0a 05 00 23 0f 17 45 4e XNOC@SE....#..EN +| 3808: 41 42 4c 45 20 46 54 53 34 58 52 2e 52 49 4d 1e ABLE FTS4XR.RIM. +| 3824: 09 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3840: 54 41 54 20 56 54 41 42 58 42 49 4e 41 52 59 1e TAT VTABXBINARY. +| 3856: 08 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3872: 54 41 54 20 56 54 41 42 58 4e 4f 43 41 53 45 1d TAT VTABXNOCASE. +| 3888: 07 05 00 31 0f 17 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3904: 54 41 54 20 56 54 41 42 58 52 54 52 49 4d 11 06 TAT VTABXRTRIM.. +| 3920: 05 00 17 0f 19 44 45 42 55 47 58 42 49 4e 41 52 .....DEBUGXBINAR +| 3936: 59 11 05 05 00 17 0f 19 44 45 42 55 47 58 4e 4f Y.......DEBUGXNO +| 3952: 43 41 53 45 10 04 05 00 17 0f 17 44 45 42 55 47 CASE.......DEBUG +| 3968: 58 52 54 52 49 4d 27 03 05 00 43 0f 19 43 4f 4d XRTRIM'...C..COM +| 3984: 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e 30 20 PILER=gcc-5.4.0 +| 4000: 32 30 31 36 30 36 30 39 58 42 49 da 41 52 59 27 20160609XBI.ARY' +| 4016: 02 05 00 43 0f 19 43 4f 4d 50 49 4c 45 52 3d 67 ...C..COMPILER=g +| 4032: 63 63 2d 35 2e 34 2e 30 20 32 30 31 36 30 36 30 cc-5.4.0 2016060 +| 4048: 39 58 4e 4f 43 41 53 45 26 01 05 00 43 0f 17 43 9XNOCASE&...C..C +| 4064: 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 00 00 00 00 OMPILER=gcc-.... +| page 5 offset 16384 +| 0: 0d 00 00 00 02 0b a0 00 0c ad 0b a0 00 00 00 00 ................ +| 2976: 82 0a 02 08 08 09 08 08 17 84 06 30 20 32 35 33 ...........0 253 +| 2992: 00 01 30 04 25 06 1b 00 00 08 32 30 31 36 30 36 ..0.%.....201606 +| 3008: 30 3a 03 25 07 00 00 01 34 03 25 05 00 00 01 35 0:.%....4.%....5 +| 3024: 03 25 04 00 01 07 30 30 30 30 30 30 30 03 25 1a .%....0000000.%. +| 3040: 00 00 08 63 6f 6d 70 69 6c 65 72 03 25 02 00 00 ...compiler.%... +| 3056: 06 64 62 73 74 61 74 03 25 0a 00 01 04 65 62 75 .dbstat.%....ebu +| 3072: 67 03 25 08 00 00 06 65 6e 61 62 6c 65 09 25 09 g.%....enable.%. +| 3088: 05 04 04 04 04 04 00 01 08 78 74 65 6e 73 69 6f .........xtensio +| 3104: 6e 03 25 1d 00 00 04 66 74 73 34 03 25 0d 00 03 n.%....fts4.%... +| 3120: 01 35 03 25 0f 00 00 03 57 63 63 03 25 03 00 01 .5.%....Wcc.%... +| 3136: 06 65 6f 70 6f 6c 79 03 25 11 00 00 05 6a 73 6f .eopoly.%....jso +| 3152: 6e 31 03 25 13 00 00 04 6c 6f 61 64 03 25 1c 00 n1.%....load.%.. +| 3168: 00 03 6d 61 78 03 25 18 00 01 05 65 6d 6f 72 79 ..max.%....emory +| 3184: 03 25 19 00 03 04 73 79 73 35 03 25 15 00 00 04 .%....sys5.%.... +| 3200: 6f 6d 69 74 03 25 1b 00 00 05 72 74 72 65 65 03 omit.%....rtree. +| 3216: 25 17 00 00 0a 74 68 72 65 64 64 73 61 66 65 03 %....threddsafe. +| 3232: 25 1e 00 00 04 76 74 61 62 03 25 0b 00 86 50 01 %....vtab.%...P. +| 3248: 08 08 08 08 08 17 8d 12 30 20 38 33 35 00 01 30 ........0 835..0 +| 3264: 12 01 06 00 01 06 00 01 06 00 1f 03 00 01 03 00 ................ +| 3280: 01 03 00 00 08 32 30 31 36 30 36 30 39 09 01 07 .....20160609... +| 3296: 00 01 07 00 01 07 00 00 01 34 09 01 05 00 01 05 .........4...... +| 3312: 00 01 05 00 00 01 35 09 01 04 00 01 04 00 01 04 ......5......... +| 3328: 00 01 07 30 30 30 30 30 30 30 09 1c 04 00 01 04 ...0000000...... +| 3344: 00 01 04 00 00 06 62 69 6e 61 72 79 3c 03 01 02 ......binary<... +| 3360: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3376: 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 ................ +| 3392: 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03 ................ +| 3408: 01 02 02 00 03 01 02 02 00 00 08 63 6f 6d 70 69 ...........compi +| 3424: 6c 65 72 09 01 02 00 01 02 00 01 02 00 00 06 64 ler............d +| 3440: 62 73 74 61 74 09 07 03 00 01 03 00 01 03 00 01 bstat........... +| 3456: 04 65 62 75 67 09 04 02 00 01 02 00 01 02 00 00 .ebug........... +| 3472: 06 65 6e 61 62 6c 65 3f 07 02 00 01 02 00 01 12 .enable?........ +| 3488: 00 01 02 00 01 02 00 01 02 00 01 02 0e 97 02 00 ................ +| 3504: 01 02 00 01 cb 00 01 02 00 01 02 00 01 02 10 01 ................ +| 3520: 02 00 01 02 00 01 02 01 01 02 00 01 02 00 01 02 ................ +| 3536: 00 01 02 00 01 02 00 01 08 78 74 65 6e 73 69 6f .........xtensio +| 3552: 6e 09 1f 04 00 01 04 00 01 04 00 00 04 66 74 73 n............fts +| 3568: 34 09 0a 03 00 01 03 00 01 03 00 03 01 35 09 0d 4............5.. +| 3584: 03 00 01 03 00 01 03 00 00 03 67 63 63 09 01 03 ..........gcc... +| 3600: 00 01 03 00 01 03 00 01 06 65 6f 70 6f 6c 79 09 .........eopoly. +| 3616: 10 03 00 01 03 00 01 03 00 00 05 6a 73 6f 6e 31 ...........json1 +| 3632: 09 13 03 00 01 03 00 01 03 00 00 04 6c 6f 61 64 ............load +| 3648: 09 1f 03 00 01 03 00 01 03 00 00 03 6d 61 78 09 ............max. +| 3664: 1c 02 00 01 02 00 01 02 00 01 05 65 6d 6f 72 79 ...........emory +| 3680: 09 1c 03 00 01 03 00 01 03 00 03 04 73 79 73 35 ............sys5 +| 3696: 09 16 03 00 01 03 00 01 03 00 00 06 6e 6f 63 61 ............noca +| 3712: 73 65 3c 02 01 02 02 00 03 01 02 02 00 03 01 02 se<............. +| 3728: 02 00 03 01 02 02 00 03 01 02 02 00 ab 01 02 02 ................ +| 3744: 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 ................ +| 3760: 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 00 ................ +| 3776: 04 6f 6d 69 74 09 1f 12 00 01 02 00 01 02 00 00 .omit........... +| 3792: 05 72 74 72 65 65 09 19 03 00 01 03 00 01 03 00 .rtree.......... +| 3808: 03 02 69 6d 3c 01 01 02 02 00 03 01 02 02 00 03 ..im<........... +| 3824: 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01 ................ +| 3840: 02 02 00 03 01 02 01 f0 03 01 02 02 00 03 01 02 ................ +| 3856: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3872: 00 00 0a 74 68 72 65 61 64 73 61 66 65 09 22 02 ...threadsafe... +| 3888: 00 01 02 00 02 02 00 00 04 76 74 61 62 09 07 04 .........vtab... +| 3904: 00 01 04 00 01 04 00 00 01 78 b4 01 01 01 08 a2 .........x...... +| 3920: 00 01 01 01 02 00 01 01 01 02 00 02 01 01 02 00 ................ +| 3936: 01 01 01 01 ff f1 01 01 02 00 01 01 01 02 00 01 ................ +| 3952: 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 ................ +| 3968: 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 ................ +| 3984: 02 00 01 01 01 02 00 01 01 01 02 01 01 01 01 02 ................ +| 4000: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| 4016: 01 01 01 02 00 01 01 01 02 00 01 01 01 03 00 01 ................ +| 4032: 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 ................ +| 4048: 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 ................ +| 4064: 02 00 01 01 01 02 00 01 02 01 02 00 01 01 01 02 ................ +| 4080: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 02 0f f5 00 0f fb 0f f5 00 00 00 00 ................ +| 4080: 00 00 00 00 00 05 04 08 09 01 02 04 04 08 08 09 ................ +| page 7 offset 24576 +| 0: 0d 00 00 00 05 0f b8 00 00 00 00 00 00 00 00 00 ................ +| 4016: 00 00 00 00 00 00 00 00 0d 05 02 23 61 75 74 6f ...........#auto +| 4032: 6d 65 72 67 65 3d 35 0d 04 02 23 6d 65 72 67 65 merge=5...#merge +| 4048: 3d 31 30 30 2c 38 11 03 02 2b 69 6e 74 65 67 72 =100,8...+integr +| 4064: 69 74 79 2d 63 68 65 63 6b 09 02 02 1b 72 65 62 ity-check....reb +| 4080: 75 69 6c 64 0a 01 02 1d 00 00 00 00 00 00 00 00 uild............ +| end crash-18cc014e42e828.db +}]} {} + +do_catchsql_test 21.1 { + PRAGMA writable_schema = 1; + SELECT offsets(t1) FROM t1 WHERE t1 MATCH 'R*'; +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +reset_db +do_test 22.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 28672 pagesize 4096 filename crash-b794c89d922ac9.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 07 .....@ ........ +| 32: 00 00 00 02 00 00 00 01 00 00 00 07 00 00 00 00 ................ +| 96: 00 00 00 00 0d 0e b1 00 06 0d a4 00 0f 8d 0f 21 ...............! +| 112: 0e b9 0d c8 0e 7e 0d a4 00 00 00 00 00 00 00 00 .....~.......... +| 3488: 00 00 00 00 22 07 06 17 11 11 01 31 74 61 62 6c ...........1tabl +| 3504: 65 74 32 74 32 07 43 52 45 41 54 45 20 54 41 42 et2t2.CREATE TAB +| 3520: 4c 45 20 74 32 28 78 29 81 33 05 07 17 1f 1f 01 LE t2(x).3...... +| 3536: 82 35 74 61 61 6c 65 74 31 5f 73 65 67 64 69 72 .5taalet1_segdir +| 3552: 74 31 5f 73 65 67 64 69 72 05 43 52 45 41 54 45 t1_segdir.CREATE +| 3568: 20 54 41 42 4c 45 20 27 74 31 5f 73 65 67 64 69 TABLE 't1_segdi +| 3584: 72 27 28 6c 65 76 65 6c 20 49 4e 54 45 47 45 52 r'(level INTEGER +| 3600: 2c 69 64 78 20 49 4e 54 45 47 45 52 2c 73 74 61 ,idx INTEGER,sta +| 3616: 72 74 5f 62 6c 6f 63 6b 20 49 4e 54 45 47 45 52 rt_block INTEGER +| 3632: 2c 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 ,leaves_end_bloc +| 3648: 6b 20 49 4e 54 45 47 45 52 2c 65 6e 64 5f 62 6c k INTEGER,end_bl +| 3664: 6f 63 6b 20 49 4e 54 45 47 45 52 2c 72 6f 6f 74 ock INTEGER,root +| 3680: 20 42 4c 4f 42 2c 50 52 49 4d 41 52 59 20 4b 45 BLOB,PRIMARY KE +| 3696: 59 28 6c 65 76 65 6c 2c 20 69 64 78 29 29 31 06 Y(level, idx))1. +| 3712: 06 17 45 1f 01 00 69 6e 64 65 78 73 71 6c 69 74 ..E...indexsqlit +| 3728: 65 5f 61 75 74 6f 69 6e 64 65 78 5f 74 31 5f 73 e_autoindex_t1_s +| 3744: 65 67 64 69 72 5f 31 74 31 5f 73 65 67 64 69 72 egdir_1t1_segdir +| 3760: 06 0f c7 00 08 00 00 00 00 66 04 07 17 23 23 01 .........f...##. +| 3776: 81 13 74 61 62 6c 65 74 31 5f 73 65 67 6d 65 6e ..tablet1_segmen +| 3792: 74 73 74 31 5f 73 65 67 6d 65 6e 74 73 04 43 52 tst1_segments.CR +| 3808: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 73 EATE TABLE 't1_s +| 3824: 65 67 6d 65 6e 74 73 27 28 62 6c 6f 63 6b 69 64 egments'(blockid +| 3840: 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 INTEGER PRIMARY +| 3856: 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 KEY, block BLOB +| 3872: 29 6a 03 07 17 21 21 01 81 1f 74 61 62 6c 65 74 )j...!!...tablet +| 3888: 31 5f 63 6f 6e 84 65 6e 74 74 31 5f 63 6f 6e 74 1_con.entt1_cont +| 3904: 65 6e 74 03 43 52 45 41 54 45 20 54 41 42 4c 45 ent.CREATE TABLE +| 3920: 20 27 74 31 5f 63 6f 6e 74 65 6e 74 27 28 64 6f 't1_content'(do +| 3936: 63 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d cid INTEGER PRIM +| 3952: 41 52 59 20 4b 45 59 2c 20 27 63 30 61 27 2c 20 ARY KEY, 'c0a', +| 3968: 27 63 31 62 27 2c 20 27 63 32 63 27 29 38 02 06 'c1b', 'c2c')8.. +| 3984: 17 11 11 08 5f 74 61 62 6c 65 74 31 74 31 43 52 ...._tablet1t1CR +| 4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 33 LE t1 USING fts3 +| 4032: 28 61 2c 62 2c 63 29 00 00 00 00 00 00 00 00 00 (a,b,c)......... +| page 3 offset 8192 +| 0: 0d 00 00 00 25 0b 48 00 0f d8 0f af 0f 86 0f 74 ....%.H........t +| 16: 0f 61 0f 4e 0f 2f 0f 0f 0e ef 0e d7 0e be 0e a5 .a.N./.......... +| 32: 0e 8d 0e 74 0e 5b 0e 40 0e 24 0e 08 0d ef 0d d5 ...t.[.@.$...... +| 48: 0d bb 0d a0 0d 84 0d 68 0d 4f 0d 35 0d 1b 0c fb .......h.O.5.... +| 64: 0c da 0c b9 0c 99 0c 78 0c 57 0c 3e 0c 24 0c 0a .......x.W.>.$.. +| 80: 0b 48 00 00 00 00 00 00 00 00 00 00 00 00 00 00 .H.............. +| 2880: 00 00 00 00 00 00 00 00 81 3f 25 06 00 82 7f 00 .........?%..... +| 2896: 00 43 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2e .COMPILER=gcc-5. +| 2912: 34 2e 30 20 32 30 31 36 30 36 30 39 20 44 45 42 4.0 20160609 DEB +| 2928: 55 47 20 45 4e 41 42 4c 45 20 44 42 53 54 41 54 UG ENABLE DBSTAT +| 2944: 20 56 54 41 42 20 45 4e 41 42 4c 45 20 46 54 53 VTAB ENABLE FTS +| 2960: 34 20 45 4e 41 42 4c 45 20 46 54 53 35 20 45 4e 4 ENABLE FTS5 EN +| 2976: 41 42 4c 45 20 47 45 4f 50 4f 4c 59 20 45 4e 41 ABLE GEOPOLY ENA +| 2992: 42 4c 45 20 4a 53 4f 4e 31 20 45 4e 41 42 4c 45 BLE JSON1 ENABLE +| 3008: 20 4d 45 4d 53 49 53 35 20 45 4e 41 42 4c 45 20 MEMSIS5 ENABLE +| 3024: 52 54 52 45 45 20 4d 41 58 20 4d 45 4d 4f 52 59 RTREE MAX MEMORY +| 3040: 3d 35 30 30 30 30 30 30 30 20 4f 4d 49 54 20 4c =50000000 OMIT L +| 3056: 4f 41 44 20 45 58 54 45 4e 53 49 4f 4e 20 54 48 OAD EXTENSION TH +| 3072: 52 45 41 44 53 41 46 45 3d 30 18 24 05 00 26 0f READSAFE=0.$..&. +| 3088: 19 54 48 52 45 41 44 53 41 46 45 3d 30 58 42 49 .THREADSAFE=0XBI +| 3104: 4e 41 52 59 18 23 05 00 25 0f 19 54 48 52 45 41 NARY.#..%..THREA +| 3120: 44 53 41 46 45 3d 30 58 4e 4f 43 41 53 45 17 22 DSAFE=0XNOCASE.. +| 3136: 05 00 25 0f 17 54 48 52 45 41 44 53 41 46 45 3d ..%..THREADSAFE= +| 3152: 30 58 52 54 52 49 4d 1f 21 05 00 33 0f 19 4f 4d 0XRTRIM.!..3..OM +| 3168: 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 4f IT LOAD EXTENSIO +| 3184: 4e 58 42 49 4e 41 52 59 1f 20 05 00 33 0f 19 4f NXBINARY. ..3..O +| 3200: 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 MIT LOAD EXTENSI +| 3216: 4f 4e 58 4e 4f 43 41 53 45 1e 1f 05 00 33 0f 17 ONXNOCASE....3.. +| 3232: 4f 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 OMIT LOAD EXTENS +| 3248: 49 4f 4e 58 52 54 52 49 4d 1f 1e 05 00 33 0f 19 IONXRTRIM....3.. +| 3264: 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 30 MAX MEMORY=50000 +| 3280: 30 30 30 58 42 49 4e 41 52 59 1f 1d 05 00 33 0f 000XBINARY....3. +| 3296: 19 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 .MAX MEMORY=5000 +| 3312: 30 30 30 30 58 4e 4f 43 41 53 45 1e 1c 05 00 33 0000XNOCASE....3 +| 3328: 0f 17 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 ..MAX MEMORY=500 +| 3344: 30 30 30 30 30 58 52 54 52 49 4d 18 1b 05 00 25 00000XRTRIM....% +| 3360: 0f 19 45 4e 41 42 4c 45 20 52 54 52 45 45 58 42 ..ENABLE RTREEXB +| 3376: 49 4e 41 52 59 18 1a 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3392: 4c 55 20 52 54 52 45 45 58 4e 4f 43 41 53 45 17 LU RTREEXNOCASE. +| 3408: 19 05 00 25 0f 17 45 4e 41 42 4c 45 20 52 54 52 ...%..ENABLE RTR +| 3424: 45 45 58 52 54 52 49 4d 1a 18 05 00 29 0f 19 45 EEXRTRIM....)..E +| 3440: 4e 41 42 4c 45 20 4d 45 4d 53 59 53 35 58 42 49 NABLE MEMSYS5XBI +| 3456: 4e 41 52 59 1a 17 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3472: 45 20 4d 45 4d 53 59 53 35 58 4e 4f 43 41 53 45 E MEMSYS5XNOCASE +| 3488: 19 16 05 00 29 0f 17 45 4e 41 42 4c 45 20 4d 45 ....)..ENABLE ME +| 3504: 4d 53 59 53 35 58 52 54 52 49 4d 18 15 05 00 25 MSYS5XRTRIM....% +| 3520: 0f 19 45 4e 41 42 4c 45 20 4a 53 4f 4e 31 58 42 ..ENABLE JSON1XB +| 3536: 49 4e 41 52 59 18 14 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3552: 4c 45 20 4a 53 4f 4e 31 58 4e 4f 43 41 53 45 17 LE JSON1XNOCASE. +| 3568: 13 05 00 25 0f 17 45 4e 41 42 4c 45 20 4a 53 4f ...%..ENABLE JSO +| 3584: 4e 31 58 52 54 52 49 4d 1a 12 05 00 29 0f 19 45 N1XRTRIM....)..E +| 3600: 4e 41 42 4c 45 20 47 45 4f 50 4f 4c 59 58 42 49 NABLE GEOPOLYXBI +| 3616: 4e 41 52 59 1a 11 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3632: 45 20 47 45 4f 50 4f 4c 59 58 4e 4f 43 41 53 45 E GEOPOLYXNOCASE +| 3648: 19 10 05 00 29 0f 17 45 4e 41 42 4c 45 20 47 45 ....)..ENABLE GE +| 3664: 4f 50 4f 4c 59 58 52 54 52 49 4d 17 0f 05 00 23 OPOLYXRTRIM....# +| 3680: 0f 19 45 4e 41 42 4c 45 20 46 54 53 35 58 42 49 ..ENABLE FTS5XBI +| 3696: 4e 41 52 59 17 0e 05 00 23 0f 19 45 4e 41 42 4c NARY....#..ENABL +| 3712: 45 20 46 54 53 35 58 4e 4f 43 41 53 45 16 0d 05 E FTS5XNOCASE... +| 3728: 00 23 0f 17 45 4e 41 42 4c 45 20 46 54 53 35 58 .#..ENABLE FTS5X +| 3744: 52 54 52 49 4d 17 0c 05 00 23 0f 19 45 4e 41 42 RTRIM....#..ENAB +| 3760: 4c 45 19 46 54 53 34 58 42 49 4e 41 52 59 17 0b LE.FTS4XBINARY.. +| 3776: 05 00 23 0f 19 45 4e 41 42 4e f5 20 46 54 53 34 ..#..ENABN. FTS4 +| 3792: 58 4e 4f 43 41 53 45 16 0a 05 00 23 0f 17 45 4e XNOCASE....#..EN +| 3808: 41 42 4c 45 20 46 54 53 34 58 52 54 52 49 4d 1e ABLE FTS4XRTRIM. +| 3824: 09 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3840: 54 41 54 20 56 54 41 42 58 42 49 4e 41 52 59 1e TAT VTABXBINARY. +| 3856: 08 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3872: 54 41 54 20 56 54 41 42 58 4e 4f 43 41 53 45 1d TAT VTABXNOCASE. +| 3888: 07 05 00 31 0f 17 45 4e 41 42 4c 45 20 44 41 53 ...1..ENABLE DAS +| 3904: 54 41 54 20 56 54 41 42 58 52 54 52 49 4d 11 06 TAT VTABXRTRIM.. +| 3920: 05 00 17 0f 19 44 45 42 55 47 58 42 49 4e 41 52 .....DEBUGXBINAR +| 3936: 59 11 05 05 00 17 0f 19 44 45 42 55 47 58 4e 4f Y.......DEBUGXNO +| 3952: 43 41 53 45 10 04 05 00 17 ab 17 44 45 42 55 47 CASE.......DEBUG +| 3968: 48 52 54 52 49 4d 27 03 05 00 43 0f 19 43 4f 4d HRTRIM'...C..COM +| 3984: 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e 30 20 PILER=gcc-5.4.0 +| 4000: 32 30 31 36 30 36 30 39 58 43 49 4e 41 52 59 27 20160609XCINARY' +| 4016: 02 04 00 43 0f 19 43 4f 4d 50 49 4c 45 52 3d 67 ...C..COMPILER=g +| 4032: 63 63 2d 35 2e 34 2e 30 20 32 30 31 36 30 36 30 cc-5.4.0 2016060 +| 4048: 39 58 4e 4f 43 41 53 45 26 01 05 00 43 0f 17 43 9XNOCASE&...C..C +| 4064: 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e OMPILER=gcc-5.4. +| 4080: 30 20 32 30 31 36 30 36 30 39 58 52 54 52 49 4d 0 20160609XRTRIM +| page 4 offset 12288 +| 0: 0d 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| page 5 offset 16384 +| 0: 0d 00 00 00 02 0b a0 00 0c ad 0b a0 00 00 00 00 ................ +| 2976: 82 0a 02 08 08 09 08 08 17 84 06 30 20 32 35 33 ...........0 253 +| 2992: 00 01 30 04 25 06 1b 00 00 08 32 30 31 36 30 36 ..0.%.....201606 +| 3008: 30 39 03 25 07 00 00 01 34 03 25 05 00 00 01 35 09.%....4.%....5 +| 3024: 03 25 04 00 01 07 30 30 30 30 30 30 30 03 25 1a .%....0000000.%. +| 3040: 00 00 08 63 6f 6d 70 69 6c 65 72 03 25 02 00 00 ...compiler.%... +| 3056: 06 64 62 73 74 61 74 03 25 0a 00 01 04 65 62 75 .dbstat.%....ebu +| 3072: 67 03 25 08 00 00 06 65 6e 61 62 6c 65 09 25 09 g.%....enable.%. +| 3088: 05 04 04 04 04 04 00 01 08 78 74 65 6e 73 69 6f .........xtensio +| 3104: 6e 03 25 1d 00 00 04 66 74 73 34 03 25 0d 00 03 n.%....fts4.%... +| 3120: 01 35 03 25 0f 00 00 03 67 63 63 03 25 03 00 01 .5.%....gcc.%... +| 3136: 06 65 6f 70 6f 6c 79 03 25 11 00 00 05 6a 73 6f .eopoly.%....jso +| 3152: 6e 31 03 25 13 00 00 04 6c 6f 61 64 03 25 1c 00 n1.%....load.%.. +| 3168: 00 03 6d 61 78 03 25 18 00 01 05 65 6d 6f 72 79 ..max.%....emory +| 3184: 03 25 19 00 03 04 73 79 73 35 03 25 15 00 00 04 .%....sys5.%.... +| 3200: 6f 6d 69 74 03 25 1b 00 00 05 72 74 72 65 65 03 omit.%....rtree. +| 3216: 25 17 00 00 0a 74 68 72 65 61 64 73 61 66 65 03 %....threadsafe. +| 3232: 25 1e 00 00 04 76 74 61 62 03 25 0b 00 86 50 01 %....vtab.%...P. +| 3248: 08 08 08 08 08 17 8d 12 30 20 38 33 35 00 01 30 ........0 835..0 +| 3264: 12 01 06 00 01 06 00 01 06 00 1f 03 00 01 03 00 ................ +| 3280: 01 03 00 00 08 32 30 31 36 30 36 30 39 09 01 07 .....20160609... +| 3296: 00 01 07 00 01 07 00 00 01 34 09 01 05 00 01 05 .........4...... +| 3312: 00 01 05 00 00 01 35 09 01 04 00 01 04 00 01 04 ......5......... +| 3328: 00 01 07 30 30 30 30 30 30 30 09 1c 04 00 01 04 ...0000000...... +| 3344: 00 01 04 00 00 06 62 69 6e 61 72 79 3c 03 01 02 ......binary<... +| 3360: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3376: 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 ................ +| 3392: 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03 ................ +| 3408: 01 02 02 00 03 01 02 02 00 00 08 63 6f 6d 70 69 ...........compi +| 3424: 6c 65 72 09 01 02 00 01 02 00 01 02 00 00 06 64 ler............d +| 3440: 62 73 74 61 74 09 07 03 00 01 03 00 01 03 00 01 bstat........... +| 3456: 04 65 62 75 67 09 04 02 00 01 02 00 01 02 00 00 .ebug........... +| 3472: 06 65 6e 61 62 6c 65 3f 07 02 00 01 02 00 01 02 .enable?........ +| 3488: 00 01 02 00 01 02 00 01 02 00 01 02 00 01 02 00 ................ +| 3504: 01 02 00 01 02 00 01 02 00 01 02 00 01 02 00 01 ................ +| 3520: 02 00 01 02 00 01 02 00 01 02 00 01 02 00 01 02 ................ +| 3536: 00 01 02 00 01 02 00 01 08 78 74 65 6e 73 69 6f .........xtensio +| 3552: 6e 09 1f 04 00 01 04 00 01 04 00 00 04 66 74 73 n............fts +| 3568: 34 09 0a 03 d4 01 02 ff 01 03 00 03 01 35 09 0d 4............5.. +| 3584: 03 00 01 03 00 01 03 00 00 03 67 64 d3 09 01 03 ..........gd.... +| 3600: 00 01 03 00 01 03 00 01 06 65 6f 70 6f 6c 79 09 .........eopoly. +| 3616: 10 03 00 01 03 00 01 03 00 00 05 6a 73 6f 6e 31 ...........json1 +| 3632: 19 13 03 00 01 03 00 01 03 00 00 04 6c 6f 61 64 ............load +| 3648: 09 1f 03 00 01 03 00 01 03 00 00 03 6d 61 78 09 ............max. +| 3664: 1c 02 00 01 02 00 01 02 00 01 05 65 6d 6f 72 79 ...........emory +| 3680: 09 1c 03 00 01 03 00 01 03 00 03 04 73 79 73 35 ............sys5 +| 3696: 09 16 03 00 01 03 00 01 03 00 00 06 6e 6f 63 61 ............noca +| 3712: 73 65 3c 02 01 02 02 00 03 01 02 02 00 03 01 02 se<............. +| 3728: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3744: 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 ................ +| 3760: 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 00 ................ +| 3776: 04 6f 6d 69 74 09 1f 02 00 01 12 00 01 02 00 00 .omit........... +| 3792: 05 72 74 72 65 65 09 19 03 00 01 07 80 00 f3 00 .rtree.......... +| 3808: 03 02 69 6d 3c 01 01 02 02 00 03 01 02 02 00 03 ..im<........... +| 3824: 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01 ................ +| 3840: 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 ................ +| 3856: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3872: 00 00 0a 74 68 72 65 61 64 73 61 66 65 09 22 02 ...threadsafe... +| 3888: 00 01 02 00 01 02 00 00 04 76 74 61 62 09 07 04 .........vtab... +| 3904: 00 01 04 00 01 04 00 00 01 78 b4 01 01 01 01 02 .........x...... +| 3920: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| 3936: 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 ................ +| 3952: 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 ................ +| 3968: 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 ................ +| 3984: 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 ................ +| 4000: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| 4016: 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 ................ +| 4032: 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 ................ +| 4048: 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 ................ +| 4064: 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 ................ +| 4080: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 02 0f f5 00 0f fb 0f f5 00 00 00 00 ................ +| 4080: 00 00 00 00 00 05 04 08 09 01 02 04 04 08 08 09 ................ +| page 7 offset 24576 +| 0: 0d 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 4016: 00 00 00 00 00 00 00 00 0d 05 02 23 61 75 74 6f ...........#auto +| 4032: 6d 65 72 67 65 3d 35 0d 04 02 23 6d 65 72 67 65 merge=5...#merge +| 4048: 3d 31 30 30 2c 38 11 03 02 2b 69 6e 74 65 67 72 =100,8...+integr +| 4064: 69 74 79 2d 63 68 65 63 6b 09 02 02 1b 72 65 62 ity-check....reb +| end crash-b794c89d922ac9.db +}]} {} + +do_catchsql_test 22.1 { + PRAGMA writable_schema = 1; + SELECT snippet(t1,'', '', '--',-1,01)==0 + FROM t1 WHERE a MATCH 'rtree OR json1rtree OR json1'; +} {0 {0 0 0 0 0 0 0}} + +#------------------------------------------------------------------------- +reset_db +do_test 23.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 28672 pagesize 4096 filename crash-670b15f2955a36.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 07 .....@ ........ +| 32: 00 00 00 02 10 00 00 01 00 00 00 07 00 00 00 00 ................ +| 96: 00 00 00 00 0d 0e b1 00 06 0d a4 00 0f 8d 0f 21 ...............! +| 112: 0e b9 0d c8 0e 7e 0d a4 00 00 00 00 00 00 00 00 .....~.......... +| 3488: 00 00 00 00 22 07 06 17 11 11 01 31 74 61 62 6c ...........1tabl +| 3504: 65 74 32 74 32 07 43 52 45 41 54 45 20 54 41 42 et2t2.CREATE TAB +| 3520: 4c 45 20 74 32 28 78 29 81 33 05 07 17 1f 1f 01 LE t2(x).3...... +| 3536: 82 35 74 61 62 6c 65 74 31 5f 73 65 67 64 69 72 .5tablet1_segdir +| 3552: 74 31 5f 73 65 67 64 69 72 05 43 52 45 41 54 45 t1_segdir.CREATE +| 3568: 20 54 41 42 4c 45 20 27 74 31 5f 73 65 67 64 69 TABLE 't1_segdi +| 3584: 72 27 28 6c 65 76 65 6c 20 49 4e 54 45 47 45 52 r'(level INTEGER +| 3600: 2c 69 64 78 20 49 4e 54 45 47 45 52 2c 73 74 61 ,idx INTEGER,sta +| 3616: 72 74 5f 62 6c 6f 63 6b 20 49 4e 54 45 47 45 52 rt_block INTEGER +| 3632: 2c 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 ,leaves_end_bloc +| 3648: 6b 20 49 4e 54 45 47 45 52 2c 65 6e 64 5f 62 6c k INTEGER,end_bl +| 3664: 6f 63 6b 20 49 4e 54 45 47 45 52 2c 72 6f 6f 74 ock INTEGER,root +| 3680: 20 42 4c 4f 42 2c 50 52 49 4d 41 52 59 20 4b 45 BLOB,PRIMARY KE +| 3696: 59 28 6c 65 76 65 6c 2c 20 69 64 78 29 29 31 06 Y(level, idx))1. +| 3712: 06 17 45 1f 01 00 69 6e 64 65 78 73 71 6c 69 74 ..E...indexsqlit +| 3728: 65 5f 61 75 74 6f 69 6e 64 65 78 5f 74 31 5f 73 e_autoindex_t1_s +| 3744: 65 67 64 69 72 5f 31 74 31 5f 73 65 67 64 69 72 egdir_1t1_segdir +| 3760: 06 0f c7 00 08 00 00 00 00 66 04 07 17 23 23 01 .........f...##. +| 3776: 81 13 74 61 62 6c 75 74 31 5f 73 65 67 6d 65 6e ..tablut1_segmen +| 3792: 74 73 74 31 5f 73 65 67 6d 65 6e 74 73 04 43 52 tst1_segments.CR +| 3808: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 73 EATE TABLE 't1_s +| 3824: 65 67 6d 65 6e 74 73 27 28 62 6c 6f 63 6b 69 64 egments'(blockid +| 3840: 20 49 4e 54 45 47 45 42 20 50 52 49 4d 41 52 59 INTEGEB PRIMARY +| 3856: 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 KEY, block BLOB +| 3872: 29 6a 03 07 17 21 21 01 81 1f 74 61 62 6c 65 74 )j...!!...tablet +| 3888: 31 5f 63 6f 6e 74 65 6e 74 74 31 5f 63 6f 6e 74 1_contentt1_cont +| 3904: 65 6e 74 03 43 52 45 41 54 45 20 54 41 42 4c 45 ent.CREATE TABLE +| 3920: 20 27 74 31 5f 63 6f 6e 74 65 6e 74 27 28 64 6f 't1_content'(do +| 3936: 63 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d cid INTEGER PRIM +| 3952: 41 52 59 20 4b 45 59 2c 20 27 63 30 61 27 2c 20 ARY KEY, 'c0a', +| 3968: 27 63 31 62 27 2c 20 27 63 32 63 27 29 38 02 06 'c1b', 'c2c')8.. +| 3984: 17 11 11 08 5f 74 61 62 6c 65 74 31 74 31 43 52 ...._tablet1t1CR +| 4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 33 LE t1 USING fts3 +| 4032: 28 61 2c 62 2c 63 29 00 00 00 00 00 00 00 00 00 (a,b,c)......... +| page 3 offset 8192 +| 0: 0d 00 00 00 25 0b 48 01 0f d8 00 2f 0f 86 0f 74 ....%.H..../...t +| 16: 0f 61 0f 4e 0f 2f 0f 0f 0e ef 0e d7 0e be 0e a5 .a.N./.......... +| 32: 0e 8d 0e 74 0e 5a fe 40 0e 24 0e 08 0d ef 0d d5 ...t.Z.@.$...... +| 48: 0d bb 0d a0 0d 84 0d 68 0d 4f 81 35 0d 1b 0c fb .......h.O.5.... +| 64: 0c da 0c b9 0c 99 0c 78 0c 57 0c 3e 00 00 00 00 .......x.W.>.... +| 2880: 00 00 00 00 00 00 00 00 81 3f 25 06 00 82 7f 10 .........?%..... +| 2896: 00 43 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2e .COMPILER=gcc-5. +| 2912: 34 2e 30 20 32 30 31 36 30 36 30 39 20 44 45 42 4.0 20160609 DEB +| 2928: 55 47 20 45 4e 41 42 4c 45 20 44 42 53 54 41 54 UG ENABLE DBSTAT +| 2944: 20 56 54 41 42 20 45 4e 41 42 4c 45 20 46 54 53 VTAB ENABLE FTS +| 2960: 34 20 45 4e 41 42 4c 45 20 46 54 53 35 20 45 4e 4 ENABLE FTS5 EN +| 2976: 41 42 4c 45 20 47 45 4f 50 4f 4c 59 20 45 4e 41 ABLE GEOPOLY ENA +| 2992: 42 4c 45 20 4a 53 4f 4e 31 20 45 4e 41 42 4c 45 BLE JSON1 ENABLE +| 3008: 20 4d 45 4d 53 59 53 35 20 45 4e 41 42 4c 45 20 MEMSYS5 ENABLE +| 3024: 52 54 52 45 45 20 4d 41 58 20 4d 45 4d 4f 52 59 RTREE MAX MEMORY +| 3040: 3d 35 30 30 30 30 30 30 30 20 4f 4d 49 54 20 4c =50000000 OMIT L +| 3056: 4f 41 44 20 45 58 54 45 4e 53 49 4f 4e 20 54 48 OAD EXTENSION TH +| 3072: 52 45 41 44 53 41 46 45 3d 30 18 24 05 00 35 0f READSAFE=0.$..5. +| 3088: 19 54 48 52 45 41 44 53 41 46 45 3d 30 58 42 49 .THREADSAFE=0XBI +| 3104: 4e 41 52 59 18 23 55 00 25 0f 19 54 48 52 45 41 NARY.#U.%..THREA +| 3120: 44 53 41 46 45 3d 30 58 4e 4f 43 41 53 45 17 22 DSAFE=0XNOCASE.. +| 3136: 05 00 25 0f 17 54 48 52 45 41 44 53 41 46 45 3d ..%..THREADSAFE= +| 3152: 30 58 52 54 52 49 4d 1f 21 05 00 33 0f 19 4f 4d 0XRTRIM.!..3..OM +| 3168: 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 4f IT LOAD EXTENSIO +| 3184: 4e 58 42 49 4e 41 52 59 1f 20 05 00 33 0f 19 4f NXBINARY. ..3..O +| 3200: 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 MIT LOAD EXTENSI +| 3216: 4f 4e 58 4e 4f 43 41 53 45 1e 1f 05 00 33 0f 17 ONXNOCASE....3.. +| 3232: 4f 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 OMIT LOAD EXTENS +| 3248: 49 4f 4e 58 52 54 52 49 4d 1f 1e 05 00 33 0f 19 IONXRTRIM....3.. +| 3264: 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 30 MAX MEMORY=50000 +| 3280: 30 30 30 58 42 49 4e 41 52 59 1f 1d 05 00 33 0f 000XBINARY....3. +| 3296: 19 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 .MAX MEMORY=5000 +| 3312: 30 30 30 30 58 4e 4f 43 41 53 45 1e 1c 05 00 33 0000XNOCASE....3 +| 3328: 0f 17 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 ..MAX MEMORY=500 +| 3344: 30 30 30 30 30 58 52 54 52 49 4d 18 1b 05 00 25 00000XRTRIM....% +| 3360: 0f 19 45 4e 41 42 4c 44 20 52 54 52 45 45 58 42 ..ENABLD RTREEXB +| 3376: 49 4e 41 52 59 18 1a 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3392: 4c 45 20 52 54 52 45 45 58 4e 4f 43 41 53 45 17 LE RTREEXNOCASE. +| 3408: 19 05 00 24 0f 17 45 4e 40 42 4c 45 20 52 54 52 ...$..EN@BLE RTR +| 3424: 45 45 58 52 54 52 49 4d 1a 18 05 00 29 0f 19 45 EEXRTRIM....)..E +| 3440: 4e 41 42 4c 45 20 4d 45 4d 53 59 53 35 58 42 49 NABLE MEMSYS5XBI +| 3456: 4e 41 52 59 1a 17 05 00 29 0f 19 45 4d 41 42 4c NARY....)..EMABL +| 3472: 45 20 4d 45 4d 53 59 53 35 58 4e 4f 43 41 4c 45 E MEMSYS5XNOCALE +| 3488: 19 16 05 00 29 0f 17 45 4e 41 42 4c 45 20 4d 45 ....)..ENABLE ME +| 3504: 4e 53 59 53 35 58 52 54 52 49 4d 18 15 05 00 25 NSYS5XRTRIM....% +| 3520: 0f 19 45 4e 41 42 4c 45 20 4a 53 4f 4e 31 58 42 ..ENABLE JSON1XB +| 3536: 49 4e 41 52 59 18 14 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3552: 4c 45 20 4a 53 4f 4e 31 58 4e 4f 43 41 53 45 17 LE JSON1XNOCASE. +| 3568: 13 05 00 25 0f 17 45 4e 41 42 4c 45 20 4a 53 4f ...%..ENABLE JSO +| 3584: 4e 31 58 52 54 5f b9 4d 1a 12 05 00 29 0f 19 45 N1XRT_.M....)..E +| 3600: 4e 41 42 4c 45 20 47 45 4f 50 31 4c 59 58 42 49 NABLE GEOP1LYXBI +| 3616: 4e 41 52 58 1a 11 05 00 29 0f 19 45 4e 41 42 4c NARX....)..ENABL +| 3632: 45 20 47 45 4f 50 4f 4c 59 58 4e 4f 43 41 53 45 E GEOPOLYXNOCASE +| 3648: 19 10 05 00 29 0f 17 45 4e 41 42 4c 45 20 57 45 ....)..ENABLE WE +| 3664: 4f 50 4f 4c 59 48 52 54 52 49 4d 17 0f 05 00 23 OPOLYHRTRIM....# +| 3680: 0f 19 45 4e 41 42 4c 45 20 46 54 53 35 58 42 49 ..ENABLE FTS5XBI +| 3696: 4e 41 53 59 17 0e 05 00 23 0f 19 45 4e 40 42 4b NASY....#..EN@BK +| 3712: 45 20 46 54 53 35 58 4e 4f 43 41 53 45 16 0d 05 E FTS5XNOCASE... +| 3728: 00 23 0f 17 45 4e 41 42 4c 45 20 46 54 53 35 58 .#..ENABLE FTS5X +| 3744: 52 54 52 49 4d 17 0c 05 00 23 0f 19 45 4e 41 42 RTRIM....#..ENAB +| 3760: 4c 45 20 46 54 94 34 58 42 49 4e 41 52 59 17 0b LE FT.4XBINARY.. +| 3776: 05 00 23 0f 19 45 4e 41 42 4c 43 70 46 54 53 34 ..#..ENABLCpFTS4 +| 3792: 58 4e 4f 43 41 53 45 16 0a 05 00 23 0f 17 45 4e XNOCASE....#..EN +| 3808: 41 42 4c 45 20 46 54 53 34 58 52 54 52 49 4d 1e ABLE FTS4XRTRIM. +| 3824: 09 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3840: 54 41 54 20 56 54 41 42 58 42 49 4e 41 52 59 1e TAT VTABXBINARY. +| 3856: 08 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 32 53 ...1..ENABLE D2S +| 3872: 54 41 54 20 56 54 41 42 58 4e 4f 43 41 53 45 1d TAT VTABXNOCASE. +| 3888: 07 05 00 31 0f 17 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3904: 54 41 54 20 56 54 41 42 58 52 54 52 49 4d 11 06 TAT VTABXRTRIM.. +| 3920: 05 0b 27 0f 19 44 45 42 55 47 58 42 49 4e 41 52 ..'..DEBUGXBINAR +| 3936: 59 11 05 05 00 17 0f 19 44 45 42 55 47 58 4e 4f Y.......DEBUGXNO +| 3952: 43 41 53 45 10 03 05 00 17 0f 17 44 45 42 55 47 CASE.......DEBUG +| 3968: 58 52 54 52 49 4d 27 03 05 00 43 0f 19 43 4f 4d XRTRIM'...C..COM +| 3984: 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e 30 20 PILER=gcc-5.4.0 +| 4000: 32 30 31 36 30 36 30 39 58 42 49 4e 41 52 59 27 20160609XBINARY' +| 4016: 02 05 00 43 0f 19 43 4f 4d 50 49 4c 35 52 3d 67 ...C..COMPIL5R=g +| 4032: 63 63 2d 35 2e 34 2e 30 20 32 30 31 36 30 36 30 cc-5.4.0 2016060 +| 4048: 39 58 4e 4f 43 41 53 45 26 01 05 00 43 0f 02 43 9XNOCASE&...C..C +| 4064: 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e OMPILER=gcc-5.4. +| 4080: 30 20 32 30 31 36 30 36 30 39 58 00 00 00 00 00 0 20160609X..... +| page 5 offset 16384 +| 0: 0d 00 00 00 02 0b a0 00 0c ad 0b a0 00 00 00 00 ................ +| 2976: 82 0a 02 08 08 09 08 08 17 84 06 30 20 32 35 33 ...........0 253 +| 2992: 00 01 30 04 25 06 1b 00 00 08 32 30 31 36 30 36 ..0.%.....201606 +| 3008: 30 39 03 25 07 00 00 01 34 03 25 05 00 00 01 35 09.%....4.%....5 +| 3024: 03 25 04 00 01 07 30 30 30 30 30 30 30 08 55 1a .%....0000000.U. +| 3040: 00 00 08 63 6f 6d 70 69 6c 65 72 03 25 02 00 00 ...compiler.%... +| 3056: 06 64 62 73 74 61 74 03 25 0a 00 01 04 65 61 75 .dbstat.%....eau +| 3072: 67 03 25 08 00 00 06 65 6e 61 62 6c 65 09 25 09 g.%....enable.%. +| 3088: 05 04 04 04 04 04 00 01 08 78 74 65 6e 73 69 6f .........xtensio +| 3104: 6e 03 25 1d 00 00 04 66 74 73 34 03 25 0d 00 03 n.%....fts4.%... +| 3120: 01 35 03 25 0f 00 00 0c 97 63 63 03 25 03 00 01 .5.%.....cc.%... +| 3136: 06 65 6f 70 6f 6c 7a 03 25 11 00 00 05 6a 73 6f .eopolz.%....jso +| 3152: 6e 31 03 25 13 00 00 04 6c 6f 61 64 03 25 1c 00 n1.%....load.%.. +| 3168: 00 03 6d 61 78 03 25 18 00 01 05 65 6d 6f 72 79 ..max.%....emory +| 3184: 03 25 3d f0 03 04 73 79 73 35 03 25 15 00 00 04 .%=...sys5.%.... +| 3200: 6f 6d 69 74 03 25 1b 00 00 05 72 74 72 65 65 03 omit.%....rtree. +| 3216: 25 17 00 00 0a 74 68 72 65 61 64 73 61 66 65 03 %....threadsafe. +| 3232: 25 1e 00 00 04 76 74 61 62 03 25 0b 00 86 50 01 %....vtab.%...P. +| 3248: 08 08 08 09 08 17 8d 12 30 20 38 33 37 e3 aa e0 ........0 837... +| 3264: 12 d1 06 00 01 06 00 01 06 00 1f 03 00 01 03 00 ................ +| 3280: 01 03 00 00 08 32 30 31 36 30 36 30 49 09 01 07 .....2016060I... +| 3296: 00 01 07 00 01 07 00 00 01 34 09 01 05 00 01 05 .........4...... +| 3312: 00 01 05 00 00 01 35 09 01 04 00 01 04 00 01 04 ......5......... +| 3328: 00 01 07 30 30 30 30 30 30 30 09 1c 04 00 01 04 ...0000000...... +| 3344: 00 01 04 00 00 06 62 69 6e 61 72 79 3c 03 01 02 ......binary<... +| 3360: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3376: 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 ................ +| 3392: 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03 ................ +| 3408: 01 02 02 00 03 01 02 02 00 00 08 63 6f 6d 70 69 ...........compi +| 3424: 6c 65 72 09 01 02 00 01 02 00 01 02 00 00 06 64 ler............d +| 3440: 62 73 74 61 74 09 07 03 00 01 03 00 01 03 00 01 bstat........... +| 3456: 04 65 62 75 67 09 04 01 f0 01 02 00 01 02 00 00 .ebug........... +| 3472: 06 65 6e 61 62 6c 65 3f 07 02 00 01 02 00 01 02 .enable?........ +| 3488: 00 01 02 00 01 02 00 01 02 00 01 02 00 01 02 00 ................ +| 3504: 01 02 00 01 02 00 01 02 00 01 02 00 01 02 00 01 ................ +| 3520: 02 00 01 02 00 01 02 00 01 02 00 01 02 00 01 02 ................ +| 3536: 00 01 02 00 01 02 00 01 08 78 74 65 6e 73 69 6f .........xtensio +| 3552: 6e 09 1f 04 00 01 04 00 01 04 00 00 04 66 74 73 n............fts +| 3568: 34 09 0a 03 f3 01 03 00 01 03 00 19 01 35 09 0d 4............5.. +| 3584: 03 00 01 03 00 01 03 00 00 03 67 63 63 09 01 03 ..........gcc... +| 3600: 00 01 03 00 01 03 00 01 06 65 6f 70 6f 6c 79 09 .........eopoly. +| 3616: 10 03 00 01 03 00 01 03 00 00 05 6a 73 6f 6e 31 ...........json1 +| 3632: 09 13 03 00 01 03 00 00 f3 00 00 04 6c 6f 61 64 ............load +| 3648: 09 1f 03 00 01 03 00 01 03 00 00 03 6d 61 78 09 ............max. +| 3664: 1c 02 00 01 02 00 01 02 00 01 05 65 6d 6f 72 79 ...........emory +| 3680: 09 1c 03 00 01 03 00 01 03 00 03 04 73 79 73 35 ............sys5 +| 3696: 09 16 03 00 01 03 00 01 03 00 00 06 6e 6f 63 61 ............noca +| 3712: 73 65 3c 02 01 02 02 00 03 01 02 02 00 03 01 02 se<............. +| 3728: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3744: 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 ................ +| 3760: 03 01 02 02 00 03 01 02 02 00 46 01 02 02 00 00 ..........F..... +| 3776: 04 6f 6d 69 74 09 1f 02 00 01 02 00 01 02 00 00 .omit........... +| 3792: 05 72 74 72 64 65 09 19 03 00 01 03 00 01 03 00 .rtrde.......... +| 3808: 03 02 69 6d 3c 01 01 02 02 00 03 01 02 02 00 03 ..im<........... +| 3824: 01 02 02 00 03 01 02 01 ff 03 01 02 02 00 03 01 ................ +| 3840: 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 ................ +| 3856: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3872: 00 00 0a 74 68 72 65 61 64 73 61 66 65 09 22 02 ...threadsafe... +| 3888: 00 01 02 00 01 02 00 00 04 76 74 61 62 09 07 04 .........vtab... +| 3904: 00 01 04 00 01 04 00 00 01 78 b4 01 01 01 01 02 .........x...... +| 3920: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| 3936: 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 ................ +| 3952: 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 ................ +| 3968: 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 ................ +| 3984: 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 ................ +| 4000: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| 4016: 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 ................ +| 4032: 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 ................ +| 4048: 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 ................ +| 4064: 02 00 01 01 01 02 00 01 01 01 02 00 01 01 02 02 ................ +| 4080: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 02 0f f5 00 0f fb 0f f5 00 00 00 00 ................ +| 4080: 00 00 00 00 00 05 04 08 09 01 02 04 04 08 09 09 ................ +| page 7 offset 24576 +| 0: 0d 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 4016: 00 00 00 00 00 00 00 00 0d 05 02 23 61 75 74 6f ...........#auto +| 4032: 6d 65 71 97 65 3d 35 0d 04 02 23 6d 65 72 67 65 meq.e=5...#merge +| 4048: 3d 31 30 30 2c 38 11 03 02 2b 69 6e 74 65 67 72 =100,8...+integr +| 4064: 69 74 79 00 00 00 00 00 00 00 00 00 00 00 00 00 ity............. +| end crash-670b15f2955a36.db +}]} {} + +do_catchsql_test 23.1 { + PRAGMA writable_schema = 1; + SELECT 'FyzLy'FROM t1 WHERE t1 MATCH 'j'; +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +reset_db +do_test 24.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 28672 pagesize 4096 filename crash-369d042958c29b.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 03 10 00 00 00 00 .....@ ........ +| 96: 00 00 00 00 0d 0e b1 00 06 0d a4 00 0f 8d 0f 21 ...............! +| 112: 0e b9 0d c8 0e 7e 0d a4 00 00 00 00 00 00 00 00 .....~.......... +| 3488: 00 00 00 00 22 07 06 17 11 11 01 31 74 61 62 6c ...........1tabl +| 3504: 65 74 32 74 32 07 43 52 45 41 54 45 20 54 41 42 et2t2.CREATE TAB +| 3520: 4c 45 20 74 32 28 78 29 81 33 05 07 17 1f 1f 01 LE t2(x).3...... +| 3536: 82 35 74 61 62 6c 65 74 31 5f 73 65 67 64 69 72 .5tablet1_segdir +| 3552: 74 31 5f 73 65 67 64 69 72 05 43 52 45 41 54 45 t1_segdir.CREATE +| 3568: 20 54 41 42 4c 45 20 27 74 31 5f 73 65 67 64 69 TABLE 't1_segdi +| 3584: 72 27 28 6c 65 76 65 6c 20 49 4e 54 45 47 45 52 r'(level INTEGER +| 3600: 2c 69 64 78 20 49 4e 54 45 47 45 52 2c 73 74 61 ,idx INTEGER,sta +| 3616: 72 74 5f 62 6c 6f 63 6b 20 49 4e 54 45 47 45 52 rt_block INTEGER +| 3632: 2c 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 ,leaves_end_bloc +| 3648: 6b 20 49 4e 54 45 47 45 52 2c 65 6e 64 5f 62 6c k INTEGER,end_bl +| 3664: 6f 63 6b 20 49 4e 54 45 47 45 52 2c 72 6f 6f 74 ock INTEGER,root +| 3680: 20 42 4c 4f 42 2c 50 52 49 4d 41 52 59 20 4b 45 BLOB,PRIMARY KE +| 3696: 59 28 6c 65 76 65 6c 2c 20 69 64 78 29 29 31 06 Y(level, idx))1. +| 3712: 06 17 45 1f 01 00 69 6e 64 65 78 73 71 6c 69 74 ..E...indexsqlit +| 3728: 65 5f 61 75 74 6f 69 6e 64 65 78 5f 74 31 5f 73 e_autoindex_t1_s +| 3744: 65 67 64 69 72 5f 31 74 31 5f 73 65 67 64 69 72 egdir_1t1_segdir +| 3760: 06 0f c7 00 08 00 10 00 00 66 04 07 17 23 23 01 .........f...##. +| 3776: 81 13 74 61 62 6c 65 74 31 5f 73 65 67 6d 65 6e ..tablet1_segmen +| 3792: 64 73 74 31 5f 73 65 67 6d 65 6e 74 73 04 43 52 dst1_segments.CR +| 3808: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 73 EATE TABLE 't1_s +| 3824: 65 67 6d 65 6e 74 73 27 28 62 6c 6f 63 6b 69 64 egments'(blockid +| 3840: 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 INTEGER PRIMARY +| 3856: 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 KEY, block BLOB +| 3872: 29 6a 03 07 17 21 21 01 81 1f 74 61 62 6c 65 74 )j...!!...tablet +| 3888: 31 4f 63 6f 6e 74 65 6e 74 74 31 5f 63 6f 6e 74 1Ocontentt1_cont +| 3904: 65 6e 74 03 43 52 45 41 54 45 20 54 41 42 4c 45 ent.CREATE TABLE +| 3920: 20 27 74 31 5f 63 6f 6e 74 65 6e 74 27 28 64 6f 't1_content'(do +| 3936: 63 69 64 20 49 4e 54 45 47 45 52 20 50 52 39 4d cid INTEGER PR9M +| 3952: 41 52 59 20 4b 45 59 2c 20 27 63 30 61 27 2c 20 ARY KEY, 'c0a', +| 3968: 27 63 31 62 27 2c 20 27 63 32 63 27 29 38 02 06 'c1b', 'c2c')8.. +| 3984: 17 11 11 08 5f 74 61 62 6c 65 74 31 74 31 43 52 ...._tablet1t1CR +| 4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 33 LE t1 USING fts3 +| 4032: 28 61 2c 62 2c 63 29 00 00 00 00 00 00 00 00 00 (a,b,c)......... +| page 3 offset 8192 +| 0: 0d 00 00 00 26 0b 48 0e 0f d8 0f af 0f 86 0f 74 ....&.H........t +| 16: 0f 61 0f 4e 0f 2f 0f 0f 0e ef 0e d7 0e be 0e a5 .a.N./.......... +| 32: 0e 8d 0e 74 0e 5b 0e 40 0e 24 0e 08 0d ef 0d d5 ...t.[.@.$...... +| 48: 0d bb 0d a0 0e 94 03 28 0d 4f 0d 35 0d 1b 05 0b .......(.O.5.... +| 64: 0c da 0c b9 0c 99 0c 78 0c 57 0c 3e 0c 24 0c 0a .......x.W.>.$.. +| 2880: 00 00 00 00 00 00 00 00 81 3f 25 06 00 72 7f 00 .........?%..r.. +| 2896: 00 43 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2e .COMPILER=gcc-5. +| 2912: 34 2e 30 20 32 30 31 36 30 36 30 39 20 44 55 42 4.0 20160609 DUB +| 2928: 55 47 20 45 4e 41 e4 7c 45 20 44 42 53 54 41 54 UG ENA.|E DBSTAT +| 2944: e4 d1 54 41 42 20 45 4e 41 42 4c 45 20 46 54 53 ..TAB ENABLE FTS +| 2960: 34 20 45 4e 41 42 4c 45 20 46 54 53 35 20 45 4e 4 ENABLE FTS5 EN +| 2976: 41 42 4c 45 20 47 45 4f 50 4f 4c 59 20 45 4e 41 ABLE GEOPOLY ENA +| 2992: 42 4c 45 20 4a 53 4f 4e 31 20 45 4e 41 42 4c 45 BLE JSON1 ENABLE +| 3008: 20 4d 45 4d 53 59 53 35 20 45 4e 41 42 4c 45 20 MEMSYS5 ENABLE +| 3024: 42 54 52 45 45 20 4d 41 58 20 4d 45 4d 4f 52 59 BTREE MAX MEMORY +| 3040: 3d 35 30 30 30 30 30 30 30 20 4f 4c 49 54 20 4c =50000000 OLIT L +| 3056: 4f 41 43 20 45 58 54 45 4e 53 49 4f 4e 21 54 48 OAC EXTENSION!TH +| 3072: 52 45 41 44 53 41 46 45 3d 30 18 24 05 00 25 0f READSAFE=0.$..%. +| 3088: 19 54 48 52 45 41 44 53 41 46 45 3d 30 58 42 49 .THREADSAFE=0XBI +| 3104: 4e 41 52 59 18 23 05 00 25 0f 19 54 48 52 45 41 NARY.#..%..THREA +| 3120: 44 53 41 4b 75 3d 30 58 4d 4f 43 41 53 45 17 22 DSAKu=0XMOCASE.. +| 3136: 05 00 25 0f 17 54 48 52 45 41 44 53 41 46 46 3d ..%..THREADSAFF= +| 3152: 30 58 52 54 52 49 4d 1f 21 05 00 33 0f 19 4f 4d 0XRTRIM.!..3..OM +| 3168: 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 52 49 4f IT LOAD EXTENRIO +| 3184: 4e 58 42 49 4e 41 52 59 1f 20 05 00 33 0f 19 4f NXBINARY. ..3..O +| 3200: 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 MIT LOAD EXTENSI +| 3216: 4f 4e 58 4e 4f 43 41 53 45 1e 1f 05 00 33 0f 17 ONXNOCASE....3.. +| 3232: 4f 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 OMIT LOAD EXTENS +| 3248: 49 4f 4e 58 52 54 52 49 4d 1f 1e 05 00 33 0f 19 IONXRTRIM....3.. +| 3264: 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 30 MAX MEMORY=50000 +| 3280: 30 30 30 58 42 49 4e 41 52 59 1f 1d 05 00 33 0f 000XBINARY....3. +| 3296: 19 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 .MAX MEMORY=5000 +| 3312: 30 30 30 30 58 4e 4f 43 41 53 45 1e 1c 05 00 33 0000XNOCASE....3 +| 3328: 0f 17 4d 42 b8 20 4d 45 4d 4f 52 59 3d 35 30 30 ..MB. MEMORY=500 +| 3344: 30 30 30 30 30 58 52 54 52 49 4d 18 1b 05 00 25 00000XRTRIM....% +| 3360: 0f 19 45 4e 41 42 4c 45 20 52 54 52 45 45 58 42 ..ENABLE RTREEXB +| 3376: 49 4e 41 52 59 18 1a 05 0d a5 0f 19 45 4e 41 42 INARY.......ENAB +| 3392: 4c 45 20 52 54 52 45 45 58 4e 4f 43 41 53 45 17 LE RTREEXNOCASE. +| 3408: 19 1c 00 25 0f 17 45 4e 41 42 4c 45 20 52 54 52 ...%..ENABLE RTR +| 3424: 45 45 58 52 54 52 49 4d 1a 18 05 00 29 0f 19 45 EEXRTRIM....)..E +| 3440: 4e 41 42 4c 45 20 4d 45 4d 53 59 53 35 58 42 49 NABLE MEMSYS5XBI +| 3456: 4e 41 52 59 1a 17 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3472: 45 20 4d 45 4d 53 59 53 35 58 4e 4f 43 41 53 45 E MEMSYS5XNOCASE +| 3488: 19 16 05 00 29 0f 17 45 4e 41 42 4c 45 20 4d 45 ....)..ENABLE ME +| 3504: 4d 53 59 53 35 58 52 54 52 49 4d 18 14 05 01 25 MSYS5XRTRIM....% +| 3520: 0f 19 45 4e 12 42 4c 45 20 4a 53 4f 4e 31 58 42 ..EN.BLE JSON1XB +| 3536: 49 4e 41 52 59 18 14 05 00 25 09 d9 45 4e 41 42 INARY....%..ENAB +| 3552: 4c 45 20 4a 53 4f 3e 31 58 4e 4f 43 41 53 45 17 LE JSO>1XNOCASE. +| 3568: 13 05 00 25 0f 17 45 4e 40 42 4c 45 20 4a 53 4f ...%..EN@BLE JSO +| 3584: 4e 31 58 52 54 52 49 4d 1a 12 05 82 29 0f 19 45 N1XRTRIM....)..E +| 3600: 4e 41 42 4c 45 20 47 45 4f 50 4f 4c 59 58 42 49 NABLE GEOPOLYXBI +| 3616: 4e 41 52 59 1a 11 05 c9 29 e8 19 46 4e 41 42 4c NARY....)..FNABL +| 3632: 48 c0 47 45 4f 50 4f 4c 59 58 4e 74 43 41 53 45 H.GEOPOLYXNtCASE +| 3648: 19 10 05 00 29 0f 17 45 4e 41 42 4c 45 20 47 45 ....)..ENABLE GE +| 3664: 4f 50 4f 4c 59 58 52 54 52 49 4d 17 0f 05 00 23 OPOLYXRTRIM....# +| 3680: 0f 19 45 4e 41 42 4c 45 30 46 54 53 35 58 42 49 ..ENABLE0FTS5XBI +| 3696: 4e 41 52 59 17 0e 05 00 23 0f 19 45 4e 41 42 4c NARY....#..ENABL +| 3712: 45 20 46 54 53 35 58 4e 4f 43 41 53 45 16 0e 05 E FTS5XNOCASE... +| 3728: 00 23 0f 17 45 4e 41 42 4c 45 20 46 54 53 35 58 .#..ENABLE FTS5X +| 3744: 52 54 52 49 4d 17 0c 05 00 23 0f 19 45 4e 41 42 RTRIM....#..ENAB +| 3760: 4c 45 20 46 54 53 34 58 42 49 4e 41 52 59 17 0b LE FTS4XBINARY.. +| 3776: 05 00 23 0f 19 45 4e 41 42 4c 45 20 46 54 53 34 ..#..ENABLE FTS4 +| 3792: 58 4e 4f 43 41 53 45 16 0a 05 00 23 0f 17 45 4e XNOCASE....#..EN +| 3808: 41 42 4c 45 20 46 54 53 34 58 52 54 52 49 4d 1e ABLE FTS4XRTRIM. +| 3824: 09 05 00 31 0f 19 45 4e 42 42 4c 45 20 44 42 53 ...1..ENBBLE DBS +| 3840: 54 41 54 20 56 54 41 42 58 42 49 4e 41 52 59 1e TAT VTABXBINARY. +| 3856: 08 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3872: 54 41 54 20 56 54 41 42 58 4e 4f 43 41 53 45 1d TAT VTABXNOCASE. +| 3888: 07 05 00 31 0f 17 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3904: 54 41 54 20 56 54 41 42 58 52 54 52 4a 4d 11 06 TAT VTABXRTRJM.. +| 3920: 05 f0 17 0f 19 44 45 42 55 47 58 42 49 4e 41 52 .....DEBUGXBINAR +| 3936: 59 11 05 05 00 17 0e 19 44 45 42 55 47 58 4e 4f Y.......DEBUGXNO +| 3952: 43 41 53 45 10 04 05 00 17 0f 16 44 45 42 55 47 CASE.......DEBUG +| 3968: 58 52 54 52 49 4d 27 03 05 00 43 0f 19 43 4f 4d XRTRIM'...C..COM +| 3984: 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e 30 20 PILER=gcc-5.4.0 +| 4000: 32 30 31 36 30 36 30 39 58 42 49 4e 41 52 59 27 20160609XBINARY' +| 4016: 02 05 00 43 0f 19 43 4f 4d 50 49 4c 45 52 3d 67 ...C..COMPILER=g +| 4032: 63 63 2d 35 2e 34 2e 30 20 32 30 31 36 30 36 30 cc-5.4.0 2016060 +| 4048: 39 58 4e 4f 43 41 53 45 26 01 06 00 43 0f 17 43 9XNOCASE&...C..C +| 4064: 4f 4d 50 49 4b 45 52 3d 67 63 63 2d 35 2e 34 2e OMPIKER=gcc-5.4. +| 4080: 30 20 32 30 31 36 30 36 40 39 58 29 54 52 49 4d 0 201606@9X)TRIM +| page 4 offset 12288 +| 0: 0d 00 10 00 00 10 00 00 00 00 00 00 00 01 00 00 ................ +| page 5 offset 16384 +| 0: 0d 00 00 00 02 0b a0 00 0c ad 0b a0 00 00 00 00 ................ +| 2976: 82 0a 02 08 08 09 08 08 17 84 06 30 20 32 35 33 ...........0 253 +| 2992: 00 01 30 04 25 06 1b 00 00 08 32 30 31 36 30 36 ..0.%.....201606 +| 3008: 30 39 03 25 07 00 00 01 34 03 25 05 00 00 01 35 09.%....4.%....5 +| 3024: 03 25 04 00 01 07 30 30 30 30 30 30 30 03 25 1a .%....0000000.%. +| 3040: 00 00 08 63 6f 6d 70 69 6c 65 72 03 25 02 00 00 ...compiler.%... +| 3056: 06 64 62 73 74 61 74 03 25 0a 00 01 04 65 62 75 .dbstat.%....ebu +| 3072: 67 03 25 08 00 00 06 65 6e 61 62 6c 65 09 25 09 g.%....enable.%. +| 3088: 05 04 04 04 04 04 00 01 08 78 74 65 6e 73 69 6f .........xtensio +| 3104: 6e 03 25 1d 00 00 04 66 74 73 34 03 25 0d 00 03 n.%....fts4.%... +| 3120: 01 35 03 25 0f 00 00 03 67 63 63 03 25 03 00 01 .5.%....gcc.%... +| 3136: 06 65 6f 70 6f 6c 79 03 25 11 00 00 05 6a 73 6f .eopoly.%....jso +| 3152: 6e 31 03 25 13 00 00 04 6c 6f 61 64 03 25 1c 00 n1.%....load.%.. +| 3168: 00 03 6d 61 78 03 25 18 00 01 05 65 6e 6f 72 79 ..max.%....enory +| 3184: 03 25 19 00 03 04 ce 79 73 4d 03 25 15 00 00 04 .%.....ysM.%.... +| 3200: 6f 6d 69 74 03 25 1b 00 00 05 72 74 72 65 65 03 omit.%....rtree. +| 3216: 25 17 00 00 0a 74 68 72 65 61 64 73 61 66 65 03 %....threadsafe. +| 3232: 25 0e 00 00 04 76 74 61 62 03 25 0b 00 86 50 01 %....vtab.%...P. +| 3248: 08 08 08 08 08 17 8d 12 30 20 38 33 35 00 01 30 ........0 835..0 +| 3264: 12 01 06 00 01 06 00 01 06 00 1f 03 00 01 03 09 ................ +| 3280: 51 03 00 00 08 32 30 31 36 30 36 30 39 09 01 07 Q....20160609... +| 3296: 00 01 07 00 01 07 00 00 01 34 09 01 05 00 01 05 .........4...... +| 3312: 00 01 05 00 00 01 35 09 01 04 00 01 04 00 01 04 ......5......... +| 3328: 00 01 07 30 30 30 30 30 30 30 09 1c 04 00 01 04 ...0000000...... +| 3344: 00 01 04 00 00 06 62 69 6e 61 72 79 3c 03 01 02 ......binary<... +| 3360: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3376: 00 03 01 02 02 00 02 f1 02 02 00 03 01 02 02 00 ................ +| 3392: 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03 ................ +| 3408: 01 02 02 00 03 01 02 02 00 00 08 63 6f 6d 70 69 ...........compi +| 3424: 6c 65 72 09 01 02 00 01 02 00 01 02 00 00 06 67 ler............g +| 3440: d2 73 74 61 74 09 07 03 00 01 03 00 01 03 00 01 .stat........... +| 3456: 04 65 62 75 67 09 04 02 00 01 02 00 01 02 00 00 .ebug........... +| 3472: 06 65 6e 6f 82 6c 65 3f 07 02 00 01 02 00 01 02 .eno.le?........ +| 3488: b0 01 02 00 01 02 00 11 02 00 01 02 00 01 02 00 ................ +| 3504: 01 02 00 01 02 00 01 02 00 01 a6 00 01 02 00 01 ................ +| 3520: 02 05 51 02 00 01 02 00 01 02 00 01 02 00 01 02 ..Q............. +| 3536: 00 01 02 00 01 02 00 01 08 78 74 65 6e 73 69 6f .........xtensio +| 3552: 6e 09 1f 04 00 01 04 00 00 04 00 00 04 66 74 73 n............fts +| 3568: 34 09 0a 03 00 01 03 00 01 03 00 03 01 35 09 0d 4............5.. +| 3584: 03 00 01 03 00 01 03 00 00 03 67 63 63 09 01 03 ..........gcc... +| 3600: 00 01 03 00 01 03 00 01 06 65 6f 70 73 6c 79 09 .........eopsly. +| 3616: 10 03 00 01 03 00 01 03 00 00 05 6a 73 6f 6e 31 ...........json1 +| 3632: 09 13 03 00 01 03 00 01 03 00 00 04 6c 6f 61 64 ............load +| 3648: 09 1f 03 00 01 03 00 01 03 00 00 03 6d 61 78 09 ............max. +| 3664: 1c 02 00 01 02 00 01 02 00 01 05 65 6d 6f 72 79 ...........emory +| 3680: 09 1c 03 00 01 03 00 01 03 00 03 04 73 79 73 35 ............sys5 +| 3696: 09 16 03 00 01 03 00 01 03 00 00 06 6e 6f 63 61 ............noca +| 3712: 73 65 3c 02 01 02 02 00 03 01 12 02 00 03 01 02 se<............. +| 3728: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3744: 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 ................ +| 3760: 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 00 ................ +| 3776: 04 6f 6d 69 74 09 1f 02 00 01 02 00 01 02 00 00 .omit........... +| 3792: 05 72 74 72 65 65 09 19 03 00 01 03 00 01 03 00 .rtree.......... +| 3808: 03 02 69 6d 3c 01 01 02 02 00 03 01 02 02 00 03 ..im<........... +| 3824: 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01 ................ +| 3840: 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 ................ +| 3856: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3872: 00 00 0a 74 68 72 65 61 64 73 61 66 65 09 22 02 ...threadsafe... +| 3888: 00 01 02 00 01 02 00 00 04 76 74 61 62 09 07 04 .........vtab... +| 3904: 00 01 04 00 01 03 ff ff 01 78 b4 01 01 01 01 02 .........x...... +| 3920: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| 3936: 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 ................ +| 3952: 01 01 02 00 01 01 01 07 30 01 01 01 02 00 01 01 ........0....... +| 3968: 01 02 00 11 01 01 02 00 01 01 01 02 00 11 01 01 ................ +| 3984: 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 ................ +| 4000: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| 4016: 01 01 01 01 ff 01 01 01 02 00 01 01 01 02 00 01 ................ +| 4032: 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 ................ +| 4048: 01 02 00 01 01 09 c2 00 01 01 01 02 00 01 01 01 ................ +| 4064: 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 ................ +| 4080: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 02 0f f5 00 0f fb 0f f5 00 00 00 00 ................ +| 4080: 00 00 00 00 00 05 04 08 09 01 02 04 04 08 08 09 ................ +| page 7 offset 24576 +| 0: 0d 00 00 00 05 0f b8 00 0e f4 0f e9 10 d6 0f c7 ................ +| 4016: 00 00 00 00 00 00 00 00 0d 05 02 23 61 75 74 6f ...........#auto +| 4032: 6d 65 72 67 65 3d 35 0d 04 02 23 6d 65 72 67 65 merge=5...#merge +| 4048: 3d 31 00 00 00 00 00 00 00 00 00 00 00 00 00 00 =1.............. +| end crash-369d042958c29b.db +}]} {} + +do_catchsql_test 24.1 { + PRAGMA writable_schema = 1; + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT '4hE'+x FROM c WHERE x<72) + INSERT INTO t1(a) SELECT randomblob(2829) FROM c; +} {1 {database disk image is malformed}} + +do_catchsql_test 24.2 { + UPDATE t1 SET b=quote((true) ) WHERE t1 MATCH 'h'; +} {0 {}} + +do_catchsql_test 24.3 { + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT 3+x FROM c WHERE x<72) + INSERT INTO t1(a) SELECT randomblob(2829) FROM c; +} {1 {database disk image is malformed}} + +do_catchsql_test 24.4 { + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT null<<x FROM c WHERE x<72) + INSERT INTO t1(a) SELECT randomblob(2829) FROM c; +} {0 {}} + +do_catchsql_test 24.5 { + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT 3+x FROM c WHERE x<72) + INSERT INTO t1(a) SELECT randomblob(2829) FROM c; +} {1 {database disk image is malformed}} + +do_catchsql_test 24.7 { + INSERT INTO t1(t1) SELECT x FROM t2; +} {0 {}} + +#------------------------------------------------------------------------- +#------------------------------------------------------------------------- +reset_db +do_test 25.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 28672 pagesize 4096 filename crash-dde9e76ed8ab2d.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 03 10 00 00 00 00 .....@ ........ +| 96: 00 00 00 00 0d 0e b1 00 06 0d a4 00 0f 8d 0f 21 ...............! +| 112: 0e b9 0d c8 0e 7e 0d a4 00 00 00 00 00 00 00 00 .....~.......... +| 3488: 00 00 00 00 22 07 06 17 11 11 01 31 74 61 62 6c ...........1tabl +| 3504: 65 74 32 74 32 07 43 52 45 41 54 45 20 54 41 42 et2t2.CREATE TAB +| 3520: 4c 45 20 74 32 28 78 29 81 33 05 07 17 1f 1f 01 LE t2(x).3...... +| 3536: 82 35 74 61 62 6c 65 74 31 5f 73 65 67 64 69 72 .5tablet1_segdir +| 3552: 74 31 5f 73 65 67 64 69 72 05 43 52 45 41 54 45 t1_segdir.CREATE +| 3568: 20 54 41 42 4c 45 20 27 74 31 5f 73 65 67 64 69 TABLE 't1_segdi +| 3584: 72 27 28 6c 65 76 65 6c 20 49 4e 54 45 47 45 52 r'(level INTEGER +| 3600: 2c 69 64 78 20 49 4e 54 45 47 45 52 2c 73 74 61 ,idx INTEGER,sta +| 3616: 72 74 5f 62 6c 6f 63 6b 20 49 4e 54 45 47 45 52 rt_block INTEGER +| 3632: 2c 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 ,leaves_end_bloc +| 3648: 6b 20 49 4e 54 45 47 45 52 2c 65 6e 64 5f 62 6c k INTEGER,end_bl +| 3664: 6f 63 6b 20 49 4e 54 45 47 45 52 2c 72 6f 6f 74 ock INTEGER,root +| 3680: 20 42 4c 4f 42 2c 50 52 49 4d 41 52 59 20 4b 45 BLOB,PRIMARY KE +| 3696: 59 28 6c 65 76 65 6c 2c 20 69 64 78 29 29 31 06 Y(level, idx))1. +| 3712: 06 17 45 1f 01 00 69 6e 64 65 78 73 71 6c 69 74 ..E...indexsqlit +| 3728: 65 5f 61 75 74 6f 69 6e 64 65 78 5f 74 31 5f 73 e_autoindex_t1_s +| 3744: 65 67 64 69 72 5f 31 74 31 5f 73 65 67 64 69 72 egdir_1t1_segdir +| 3760: 06 0f c7 00 08 00 00 00 00 66 04 07 17 23 23 01 .........f...##. +| 3776: 81 13 74 61 62 6c 65 74 31 5f 73 65 67 6d 65 6e ..tablet1_segmen +| 3792: 64 73 74 31 5f 73 65 67 6d 65 6e 74 73 04 43 52 dst1_segments.CR +| 3808: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 73 EATE TABLE 't1_s +| 3824: 65 67 6d 65 6e 74 73 27 28 62 6c 6f 63 6b 69 64 egments'(blockid +| 3840: 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 INTEGER PRIMARY +| 3856: 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 KEY, block BLOB +| 3872: 29 6a 03 07 17 21 21 01 81 1f 74 61 62 6c 65 74 )j...!!...tablet +| 3888: 31 5f 63 6f 6e 74 65 6e 74 74 31 5f 63 6f 6e 74 1_contentt1_cont +| 3904: 65 6e 74 03 43 52 45 41 54 45 20 54 41 42 4c 45 ent.CREATE TABLE +| 3920: 20 27 74 31 5f 63 6f 6e 74 65 6e 74 27 28 64 6f 't1_content'(do +| 3936: 63 69 64 20 49 4e 54 45 47 45 52 20 50 52 39 4d cid INTEGER PR9M +| 3952: 41 52 59 20 4b 45 59 2c 20 27 63 30 61 27 2c 20 ARY KEY, 'c0a', +| 3968: 27 63 31 62 27 2c 20 27 63 32 63 27 29 38 02 06 'c1b', 'c2c')8.. +| 3984: 17 11 11 08 5f 74 61 62 6c 65 74 31 74 31 43 52 ...._tablet1t1CR +| 4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 33 LE t1 USING fts3 +| 4032: 28 61 2c 62 2c 63 29 00 00 00 00 00 00 00 00 00 (a,b,c)......... +| page 3 offset 8192 +| 0: 0d 00 00 00 26 0b 48 0e 0f d8 0f af 0f 86 0f 74 ....&.H........t +| 16: 0f 61 0f 4e 0f 2f 0f 0f 0e ef 0e d7 0e be 0e a5 .a.N./.......... +| 32: 0e 8d 0e 74 0e 5b 0e 40 0e 24 0e 08 0d ef 0d d5 ...t.[.@.$...... +| 48: 0d bb 0d a0 0e 94 03 28 0d 4f 0d 35 0d 1b 05 0b .......(.O.5.... +| 64: 0c da 0c b9 0c 99 0c 78 0c 57 0c 3e 0c 24 0c 0a .......x.W.>.$.. +| 2880: 00 00 00 00 00 00 00 00 81 3f 25 06 00 72 7f 00 .........?%..r.. +| 2896: 00 43 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2e .COMPILER=gcc-5. +| 2912: 34 2e 30 20 32 30 31 36 30 36 30 39 20 44 55 42 4.0 20160609 DUB +| 2928: 55 47 20 45 4e 41 e4 7c 45 20 44 42 53 54 41 54 UG ENA.|E DBSTAT +| 2944: e4 46 54 41 42 20 45 4e 41 42 4c 45 20 46 54 53 .FTAB ENABLE FTS +| 2960: 34 20 45 4e 41 42 4c 45 20 46 54 53 35 20 45 4e 4 ENABLE FTS5 EN +| 2976: 41 42 4c 45 20 47 45 4f 50 4f 4c 59 20 45 4e 41 ABLE GEOPOLY ENA +| 2992: 42 4c 45 20 4a 53 4f 4e 31 20 45 4e 41 42 4c 45 BLE JSON1 ENABLE +| 3008: 20 4d 45 4d 53 59 53 35 20 45 4e 41 42 4c 45 20 MEMSYS5 ENABLE +| 3024: 42 54 52 45 45 20 4d 41 58 20 4d 45 4d 4f 52 59 BTREE MAX MEMORY +| 3040: 3d 35 30 30 30 30 30 30 30 20 4f 4c 49 54 20 4c =50000000 OLIT L +| 3056: 4f 41 43 20 45 58 54 45 4e 53 49 4f 4e 21 54 48 OAC EXTENSION!TH +| 3072: 52 45 41 44 53 41 46 45 3d 30 18 24 05 00 25 0f READSAFE=0.$..%. +| 3088: 19 54 48 52 45 41 44 53 41 46 45 3d 30 58 42 49 .THREADSAFE=0XBI +| 3104: 4e 41 52 59 18 23 05 00 25 0f 19 54 48 52 45 41 NARY.#..%..THREA +| 3120: 44 53 41 4b 75 3d 30 58 4d 4f 43 41 53 45 17 22 DSAKu=0XMOCASE.. +| 3136: 05 00 25 0f 17 54 48 52 45 41 44 53 41 46 46 3d ..%..THREADSAFF= +| 3152: 30 58 52 54 52 49 4d 1f 21 05 00 33 0f 19 4f 4d 0XRTRIM.!..3..OM +| 3168: 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 52 49 4f IT LOAD EXTENRIO +| 3184: 4e 58 42 49 4e 41 52 59 1f 20 05 00 33 0f 19 4f NXBINARY. ..3..O +| 3200: 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 MIT LOAD EXTENSI +| 3216: 4f 4e 58 4e 4f 43 41 53 45 1e 1f 05 00 33 0f 17 ONXNOCASE....3.. +| 3232: 4f 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 OMIT LOAD EXTENS +| 3248: 49 4f 4e 58 52 54 52 49 4d 1f 1e 05 00 33 0f 19 IONXRTRIM....3.. +| 3264: 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 30 MAX MEMORY=50000 +| 3280: 30 30 30 58 42 49 4e 41 52 59 1f 1d 05 00 33 0f 000XBINARY....3. +| 3296: 19 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 .MAX MEMORY=5000 +| 3312: 30 30 30 30 58 4e 4f 43 41 53 45 1e 1c 05 00 33 0000XNOCASE....3 +| 3328: 0f 17 4d 42 b8 20 4d 45 4d 4f 52 59 3d 35 30 30 ..MB. MEMORY=500 +| 3344: 30 30 30 30 30 58 52 54 52 49 4d 18 1b 05 00 25 00000XRTRIM....% +| 3360: 0f 19 45 4e 41 42 4c 45 20 52 54 52 45 45 58 42 ..ENABLE RTREEXB +| 3376: 49 4e 41 52 59 18 1a 05 0d a5 0f 19 45 4e 41 42 INARY.......ENAB +| 3392: 4c 45 20 52 54 52 45 45 58 4e 4f 43 41 53 45 17 LE RTREEXNOCASE. +| 3408: 19 1c 00 25 0f 17 45 4e 41 42 4c 45 20 52 54 52 ...%..ENABLE RTR +| 3424: 45 45 58 52 54 52 49 4d 1a 18 05 00 29 0f 19 45 EEXRTRIM....)..E +| 3440: 4e 41 42 4c 45 20 4d 45 4d 53 59 53 35 58 42 49 NABLE MEMSYS5XBI +| 3456: 4e 41 52 59 1a 17 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3472: 45 20 4d 45 4d 53 59 53 35 58 4e 4f 43 41 53 45 E MEMSYS5XNOCASE +| 3488: 19 16 05 00 29 0f 17 45 4e 41 42 4c 45 20 4d 45 ....)..ENABLE ME +| 3504: 4d 53 59 53 35 58 52 54 52 49 4d 18 14 05 01 25 MSYS5XRTRIM....% +| 3520: 0f 19 45 4e 12 42 4c 45 20 4a 53 4f 4e 31 58 42 ..EN.BLE JSON1XB +| 3536: 49 4e 41 52 59 18 14 05 00 25 09 d9 45 4e 41 42 INARY....%..ENAB +| 3552: 4c 45 20 4a 53 4f 3e 31 58 4e 4f 43 41 53 45 17 LE JSO>1XNOCASE. +| 3568: 13 05 00 25 0f 17 45 4e 40 42 4c 45 20 4a 53 4f ...%..EN@BLE JSO +| 3584: 4e 31 58 52 54 52 49 4d 1a 12 05 82 29 0f 19 45 N1XRTRIM....)..E +| 3600: 4e 41 42 4c 45 20 47 45 4f 50 4f 4c 59 58 42 49 NABLE GEOPOLYXBI +| 3616: 4e 41 52 59 1a 11 05 c9 29 e8 19 46 4e 41 42 4c NARY....)..FNABL +| 3632: 48 c0 47 45 4f 50 4f 4c 59 58 4e 74 43 41 53 45 H.GEOPOLYXNtCASE +| 3648: 19 10 05 00 29 0f 17 45 4e 41 42 4c 45 20 47 45 ....)..ENABLE GE +| 3664: 4f 50 4f 4c 59 58 52 54 52 49 4d 17 0f 05 00 23 OPOLYXRTRIM....# +| 3680: 0f 19 45 4e 41 42 4c 45 30 46 54 53 35 58 42 49 ..ENABLE0FTS5XBI +| 3696: 4e 41 52 59 17 0e 05 00 23 0f 19 45 4e 41 42 4c NARY....#..ENABL +| 3712: 45 20 46 54 53 35 58 4e 4f 43 41 53 45 16 0e 05 E FTS5XNOCASE... +| 3728: 00 23 0f 17 45 4e 41 42 4c 45 20 46 54 53 35 58 .#..ENABLE FTS5X +| 3744: 52 54 52 49 4d 17 0c 05 00 23 0f 19 45 4e 41 42 RTRIM....#..ENAB +| 3760: 4c 45 20 46 54 53 34 58 42 49 4e 41 52 59 17 0b LE FTS4XBINARY.. +| 3776: 05 00 23 0f 19 45 4e 41 42 4c 45 20 46 54 53 34 ..#..ENABLE FTS4 +| 3792: 58 4e 4f 43 41 53 45 16 0a 05 00 23 0f 17 45 4e XNOCASE....#..EN +| 3808: 41 42 4c 45 20 46 54 53 34 58 52 54 52 49 4d 1e ABLE FTS4XRTRIM. +| 3824: 09 05 00 31 0f 19 45 4e 42 42 4c 45 20 44 42 53 ...1..ENBBLE DBS +| 3840: 54 41 54 20 56 54 41 42 58 42 49 4e 41 52 59 1e TAT VTABXBINARY. +| 3856: 08 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3872: 54 41 54 20 56 54 41 42 58 4e 4f 43 41 53 45 1d TAT VTABXNOCASE. +| 3888: 07 05 00 31 0f 17 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3904: 54 41 54 20 56 54 41 42 58 52 54 52 4a 4d 11 06 TAT VTABXRTRJM.. +| 3920: 05 f0 17 0f 19 44 45 42 55 47 58 42 49 4e 41 52 .....DEBUGXBINAR +| 3936: 59 11 05 05 00 17 0e 19 44 45 42 55 47 58 4e 4f Y.......DEBUGXNO +| 3952: 43 41 53 45 10 04 05 00 17 0f 16 44 45 42 55 47 CASE.......DEBUG +| 3968: 58 52 54 52 49 4d 27 03 05 00 43 0f 19 43 4f 4d XRTRIM'...C..COM +| 3984: 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e 30 20 PILER=gcc-5.4.0 +| 4000: 32 30 31 36 30 36 30 39 58 42 49 4e 41 52 59 27 20160609XBINARY' +| 4016: 02 05 00 43 0f 19 43 4f 4d 50 49 4c 45 52 3d 67 ...C..COMPILER=g +| 4032: 63 63 2d 35 2e 34 2e 30 20 32 30 31 36 30 36 30 cc-5.4.0 2016060 +| 4048: 39 58 4e 4f 43 41 53 45 26 01 06 00 43 0f 17 43 9XNOCASE&...C..C +| 4064: 4f 4d 50 49 4b 45 52 3d 67 63 63 2d 35 2e 34 2e OMPIKER=gcc-5.4. +| 4080: 30 20 32 30 31 36 30 36 40 39 58 29 54 52 49 4d 0 201606@9X)TRIM +| page 4 offset 12288 +| 0: 0d 00 10 00 00 10 00 00 00 00 00 00 00 01 00 00 ................ +| page 5 offset 16384 +| 0: 0d 00 00 00 02 0b a0 00 0c ad 0b a0 00 00 00 00 ................ +| 2976: 82 0a 02 08 08 09 08 08 17 84 06 30 20 32 35 33 ...........0 253 +| 2992: 00 01 30 04 25 06 1b 00 00 08 32 30 31 36 30 36 ..0.%.....201606 +| 3008: 30 39 03 25 07 00 00 01 34 03 25 05 00 00 01 35 09.%....4.%....5 +| 3024: 03 25 04 00 01 07 30 30 30 30 30 30 30 03 25 1a .%....0000000.%. +| 3040: 00 00 08 63 6f 6d 70 69 6c 65 72 03 25 02 00 00 ...compiler.%... +| 3056: 06 64 62 73 74 61 74 03 25 0a 00 01 04 65 62 75 .dbstat.%....ebu +| 3072: 67 03 25 08 00 00 06 65 6e 61 62 6c 65 09 25 09 g.%....enable.%. +| 3088: 05 04 04 04 04 04 00 01 08 78 74 65 6e 73 69 6f .........xtensio +| 3104: 6e 03 25 1d 00 00 04 66 74 73 34 03 25 0d 00 03 n.%....fts4.%... +| 3120: 01 35 03 25 0f 00 00 03 67 63 63 03 25 03 00 01 .5.%....gcc.%... +| 3136: 06 65 6f 70 6f 6c 79 03 25 11 00 00 05 6a 73 6f .eopoly.%....jso +| 3152: 6e 31 03 25 13 00 00 04 6c 6f 61 64 03 25 1c 00 n1.%....load.%.. +| 3168: 00 03 6d 61 78 03 25 18 00 01 05 65 6e 6f 72 79 ..max.%....enory +| 3184: 03 25 19 00 03 04 ce 79 73 4d 03 25 15 00 00 04 .%.....ysM.%.... +| 3200: 6f 6d 69 74 03 25 1b 00 00 05 72 74 72 65 65 03 omit.%....rtree. +| 3216: 25 17 00 00 0a 74 68 72 65 61 64 73 61 66 65 03 %....threadsafe. +| 3232: 25 0e 00 00 04 76 74 61 62 03 25 0b 00 86 50 01 %....vtab.%...P. +| 3248: 08 08 08 08 08 17 8d 12 30 20 38 33 35 00 01 30 ........0 835..0 +| 3264: 12 01 06 00 01 06 00 01 06 00 1f 03 00 01 03 09 ................ +| 3280: 51 03 00 00 08 32 30 31 36 30 36 30 39 09 01 07 Q....20160609... +| 3296: 00 01 07 00 01 07 00 00 01 34 09 01 05 00 01 05 .........4...... +| 3312: 00 01 05 00 00 01 35 09 01 04 00 01 04 00 01 04 ......5......... +| 3328: 00 01 07 30 30 30 30 30 30 30 09 1c 04 00 01 04 ...0000000...... +| 3344: 00 01 04 00 00 06 62 69 6e 61 72 79 3c 03 01 02 ......binary<... +| 3360: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3376: 00 03 01 02 02 00 02 f1 02 02 00 03 01 02 02 00 ................ +| 3392: 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03 ................ +| 3408: 01 02 02 00 03 01 02 02 00 00 08 63 6f 6d 70 69 ...........compi +| 3424: 6c 65 72 09 01 02 00 01 02 00 01 02 00 00 06 67 ler............g +| 3440: d2 73 74 61 74 09 07 03 00 01 03 00 01 03 00 01 .stat........... +| 3456: 04 65 62 75 67 09 04 02 00 01 02 00 01 02 00 00 .ebug........... +| 3472: 06 65 6e 6f 82 6c 65 3f 07 02 00 01 02 00 01 02 .eno.le?........ +| 3488: b0 01 02 00 01 02 00 11 02 00 01 02 00 01 02 00 ................ +| 3504: 01 02 00 01 02 00 01 02 00 01 a6 00 01 02 00 01 ................ +| 3520: 02 05 51 02 00 01 02 00 01 02 00 01 02 00 01 02 ..Q............. +| 3536: 00 01 02 00 01 02 00 01 08 78 74 65 6e 73 69 6f .........xtensio +| 3552: 6e 09 1f 04 00 01 04 00 00 04 00 00 04 66 74 73 n............fts +| 3568: 34 09 0a 03 00 01 03 00 01 03 00 03 01 35 09 0d 4............5.. +| 3584: 03 00 01 03 00 01 03 00 00 03 67 63 63 09 01 03 ..........gcc... +| 3600: 00 01 03 00 01 03 00 01 06 65 6f 70 73 6c 79 09 .........eopsly. +| 3616: 10 03 00 01 03 00 01 03 00 00 05 6a 73 6f 6e 31 ...........json1 +| 3632: 09 13 03 00 01 03 00 01 03 00 00 04 6c 6f 61 64 ............load +| 3648: 09 1f 03 00 01 03 00 01 03 00 00 03 6d 61 78 09 ............max. +| 3664: 1c 02 00 01 02 00 01 02 00 01 05 65 6d 6f 72 79 ...........emory +| 3680: 09 1c 03 00 01 03 00 01 03 00 03 04 73 79 73 35 ............sys5 +| 3696: 09 16 03 00 01 03 00 01 03 00 00 06 6e 6f 63 61 ............noca +| 3712: 73 65 3c 02 01 02 02 00 03 01 12 02 00 03 01 02 se<............. +| 3728: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3744: 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 ................ +| 3760: 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 00 ................ +| 3776: 0e 9f 6d 69 74 09 1f 02 00 01 02 00 01 02 00 00 ..mit........... +| 3792: 05 72 74 72 65 65 09 19 03 00 01 03 00 01 03 00 .rtree.......... +| 3808: 03 02 69 6d 3c 01 01 02 02 00 03 01 02 02 00 03 ..im<........... +| 3824: 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01 ................ +| 3840: 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 ................ +| 3856: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3872: 00 00 0a 74 68 72 65 61 64 73 61 66 65 09 22 02 ...threadsafe... +| 3888: 00 01 02 00 01 02 00 00 04 76 74 61 62 09 07 04 .........vtab... +| 3904: 00 01 04 00 01 04 00 00 01 78 b4 01 01 01 01 02 .........x...... +| 3920: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| 3936: 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 ................ +| 3952: 01 01 02 00 01 01 01 07 30 01 01 01 02 00 01 01 ........0....... +| 3968: 01 02 00 11 01 01 02 00 01 01 01 02 00 11 01 01 ................ +| 3984: 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 ................ +| 4000: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| 4016: 01 01 01 01 ff 01 01 01 02 00 01 01 01 02 00 01 ................ +| 4032: 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 ................ +| 4048: 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 ................ +| 4064: 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 ................ +| 4080: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 02 0f f5 00 0f fb 0f f5 00 00 00 00 ................ +| 4080: 00 00 00 00 00 05 04 08 09 01 02 04 04 08 08 09 ................ +| page 7 offset 24576 +| 0: 0d 00 00 00 05 0f b8 00 0e f4 0f e9 10 d6 0f c7 ................ +| 4016: 00 00 00 00 00 00 00 00 0d 05 02 23 61 75 74 6f ...........#auto +| 4032: 6d 65 72 67 65 3d 35 0d 04 02 23 6d 65 72 67 65 merge=5...#merge +| 4048: 3d 31 00 00 00 00 00 00 00 00 00 00 00 00 00 00 =1.............. +| end crash-dde9e76ed8ab2d.db +}]} {} + +reset_prng_state + +do_catchsql_test 25.1 { + PRAGMA writable_schema = 1; + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x%1 FROM c WHERE x<599237) + INSERT INTO t1( a ) SELECT randomblob(3000) FROM t2 ; +} {0 {}} + +do_catchsql_test 25.2 { + UPDATE t1 SET b=quote((true) ) WHERE t1 MATCH 'h*'; +} {0 {}} + +do_catchsql_test 25.3 { + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x +x FROM c WHERE 72<x) + INSERT INTO t1(a) SELECT randomblob(2829) FROM c; +} {0 {}} + +do_catchsql_test 25.4 { + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x%1 FROM c WHERE 599237<x) + INSERT INTO t1(a) SELECT randomblob(3000) FROM t2 ; +} {0 {}} + +do_catchsql_test 25.5 { + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x%1 FROM c WHERE x<599237) + INSERT INTO t1( a ) SELECT randomblob(3000) FROM t2 ; +} {0 {}} + +if {$tcl_platform(byteOrder)=="littleEndian"} { + # The SQLITE_CORRUPT error depends on the specific random byte + # sequence generated by SQLite's PRNG. But the SQLite PRNG + # uses ChaCha20, which generates a different byte sequence on + # big-endian and little-endian platforms. The SQLITE_CORRUPT + # error only comes up when the pseudo-random byte sequence is + # the one generated on little-endian platforms. + # + # See Forum thread: + # https://sqlite.org/forum/forumpost/b5f89d813babfd88 + # + do_catchsql_test 25.6a { + INSERT INTO t1(t1) SELECT x FROM t2; + } {1 {database disk image is malformed}} + do_catchsql_test 25.6b { + INSERT INTO t1(t1) SELECT x FROM t2; + } {1 {database disk image is malformed}} +} else { + do_catchsql_test 25.6a { + INSERT INTO t1(t1) SELECT x FROM t2; + } {0 {}} + do_catchsql_test 25.6b { + INSERT INTO t1(t1) SELECT x FROM t2; + } {0 {}} +} + + +#------------------------------------------------------------------------- +reset_db +do_test 26.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 28672 pagesize 4096 filename crash-26682721375870.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 00 .....@ ........ +| 96: 00 00 00 00 0d 0e b1 00 06 0d a4 00 0f 8d 0f 21 ...............! +| 112: 0e b9 0d c8 0e 7e 0d a4 00 00 00 00 00 00 00 00 .....~.......... +| 3488: 00 00 00 00 22 07 06 17 11 11 01 31 74 61 62 6c ...........1tabl +| 3504: 65 74 32 74 32 07 43 52 45 41 54 45 20 54 41 42 et2t2.CREATE TAB +| 3520: 4c 45 20 74 32 28 78 29 81 33 05 07 17 1f 1f 01 LE t2(x).3...... +| 3536: 82 35 74 61 62 6c 65 74 31 5f 73 65 67 64 69 72 .5tablet1_segdir +| 3552: 74 31 5f 73 65 67 64 69 72 05 43 52 45 41 54 45 t1_segdir.CREATE +| 3568: 20 54 41 42 4c 45 20 27 74 31 5f 73 65 67 64 69 TABLE 't1_segdi +| 3584: 72 27 28 6c 65 76 65 6c 20 49 4e 54 45 47 45 51 r'(level INTEGEQ +| 3600: 2c 69 64 78 20 49 4e 54 45 47 45 52 2c 73 74 61 ,idx INTEGER,sta +| 3616: 72 74 5f 62 6c 6f 63 6b 20 49 4e 54 45 47 45 52 rt_block INTEGER +| 3632: 2c 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 ,leaves_end_bloc +| 3648: 6b 20 49 4e 54 45 47 45 52 2c 65 6e 64 5f 62 6c k INTEGER,end_bl +| 3664: 6f 63 6b 20 49 4e 54 45 47 45 62 2c 72 6f 6f 74 ock INTEGEb,root +| 3680: 20 42 4c 4f 42 2c 50 52 49 4d 41 52 59 20 4b 45 BLOB,PRIMARY KE +| 3696: 59 28 6c 65 76 65 6c 2c 20 69 64 78 29 29 31 06 Y(level, idx))1. +| 3712: 06 17 45 1f 01 00 69 6e 64 65 78 73 71 6c 69 74 ..E...indexsqlit +| 3728: 65 5f 61 75 74 6f 69 6e 64 65 78 5f 74 31 5f 73 e_autoindex_t1_s +| 3744: 65 67 64 69 72 5f 31 74 31 5f 73 65 67 64 69 72 egdir_1t1_segdir +| 3760: 06 0f c7 00 08 00 00 00 00 66 04 07 17 23 23 01 .........f...##. +| 3776: 81 13 74 61 62 6c 65 74 31 5f 73 65 66 6d 65 6e ..tablet1_sefmen +| 3792: 74 73 74 31 5f 73 65 67 6d 65 6e 74 73 04 43 52 tst1_segments.CR +| 3808: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 73 EATE TABLE 't1_s +| 3824: 65 67 6d 65 6e 74 73 27 28 62 6c 6f 63 6b 69 64 egments'(blockid +| 3840: 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 INTEGER PRIMARY +| 3856: 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 KEY, block BLOB +| 3872: 29 6a 03 07 17 21 21 01 81 1f 74 61 62 6c 65 74 )j...!!...tablet +| 3888: 31 5f 63 6f 6e 74 65 6e 74 74 31 5f 63 6f 6e 74 1_contentt1_cont +| 3904: 65 6e 74 03 43 52 45 41 54 45 20 54 41 42 4c 45 ent.CREATE TABLE +| 3920: 20 27 74 31 5f 63 6f 6e 74 65 6e 74 27 28 64 6f 't1_content'(do +| 3936: 63 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d cid INTEGER PRIM +| 3952: 41 52 59 20 4b 45 59 2c 20 27 63 30 61 27 2c 20 ARY KEY, 'c0a', +| 3968: 27 63 31 62 27 2c 20 27 63 32 63 27 29 38 02 06 'c1b', 'c2c')8.. +| 3984: 17 11 11 08 5f 74 61 62 6c 65 74 31 74 31 43 52 ...._tablet1t1CR +| 4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 33 LE t1 USING fts3 +| 4032: 28 61 2c 62 2c 63 29 00 00 00 00 00 00 00 00 00 (a,b,c)......... +| page 3 offset 8192 +| 0: 0d 00 00 00 25 0b 48 00 0f d8 0f af 0f 86 0f 74 ....%.H........t +| 16: 0f 61 0f 4e 0f 2f 0f 0f 0e ef 0e d7 0e be 0e a5 .a.N./.......... +| 32: 0e 8d 0e 74 0e 5b 0e 40 0e 24 0e 08 0d ef 00 00 ...t.[.@.$...... +| 2880: 00 00 00 00 00 00 00 00 81 3f 25 06 00 82 7f 00 .........?%..... +| 2896: 00 43 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2e .COMPILER=gcc-5. +| 2912: 34 23 00 20 32 2f 31 36 30 36 30 39 20 44 45 42 4#. 2/160609 DEB +| 2928: 55 47 20 45 4e 41 42 4c 45 20 44 42 53 54 41 54 UG ENABLE DBSTAT +| 2944: 20 56 54 41 42 20 45 4e 41 42 4c 45 20 46 54 53 VTAB ENABLE FTS +| 2960: 34 20 45 4e 41 42 4c 45 20 46 54 53 35 20 45 4e 4 ENABLE FTS5 EN +| 2976: 41 42 4c 45 20 47 45 4f 50 4f 4c 59 20 45 4e 41 ABLE GEOPOLY ENA +| 2992: 42 4c 45 20 4a 53 4f 4e 31 20 45 4e 41 42 4c 45 BLE JSON1 ENABLE +| 3008: 20 4d 45 4d 53 59 53 35 20 45 4e 41 42 4c 45 20 MEMSYS5 ENABLE +| 3024: 52 54 52 45 45 20 4d 41 58 20 4d 45 4d 4f 52 59 RTREE MAX MEMORY +| 3040: 3d 35 30 30 30 30 30 30 30 20 4f 4d 49 54 20 4c =50000000 OMIT L +| 3056: 4f 41 44 20 45 58 54 45 4e 53 59 4f 4e 20 54 48 OAD EXTENSYON TH +| 3072: 52 45 41 44 53 41 46 45 3d 30 18 24 05 00 25 0f READSAFE=0.$..%. +| 3088: 19 54 48 52 45 41 44 53 41 46 45 3d 30 58 42 49 .THREADSAFE=0XBI +| 3104: 4e 41 52 59 18 23 05 00 25 0f 19 54 48 52 45 41 NARY.#..%..THREA +| 3120: 44 53 41 46 45 3d 30 58 4e 4f 43 41 53 45 17 22 DSAFE=0XNOCASE.. +| 3136: 05 00 25 0f 17 54 38 52 45 41 44 53 41 46 45 3d ..%..T8READSAFE= +| 3152: 30 58 52 54 52 49 4d 1f 21 05 00 33 0f 19 4f 4d 0XRTRIM.!..3..OM +| 3168: 49 54 20 4c 4f 41 44 20 45 58 54 45 fc 53 49 4f IT LOAD EXTE.SIO +| 3184: 4e 68 42 49 4e 4a c2 59 1f 20 05 00 33 0f 19 4f NhBINJ.Y. ..3..O +| 3200: 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 MIT LOAD EXTENSI +| 3216: 4f 4e 58 4e 4f 43 41 53 45 1e 1f 05 00 33 0f 17 ONXNOCASE....3.. +| 3232: 4f 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 OMIT LOAD EXTENS +| 3248: 49 4f 4e 58 52 54 52 49 4d 1f 1e 05 00 33 0f 19 IONXRTRIM....3.. +| 3264: 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 2f 30 30 MAX MEMORY=50/00 +| 3280: 30 30 30 58 42 49 4e 41 52 59 1f 1d 05 00 33 0f 000XBINARY....3. +| 3296: 19 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 .MAX MEMORY=5000 +| 3312: 30 30 30 30 48 4e 4f 43 41 53 45 1e 1c 05 00 33 0000HNOCASE....3 +| 3328: 0f 17 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 ..MAX MEMORY=500 +| 3344: 30 30 30 30 30 58 52 54 52 49 4d 18 1b 05 00 25 00000XRTRIM....% +| 3360: 0f 19 45 4e 41 42 4c 45 20 52 54 52 45 45 58 42 ..ENABLE RTREEXB +| 3376: 49 4e 41 52 59 18 1a 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3392: 4c 45 20 52 54 52 45 45 58 4e 4f 53 41 53 45 17 LE RTREEXNOSASE. +| 3408: 19 05 00 25 0f 17 45 4e 41 42 4c 45 20 52 54 52 ...%..ENABLE RTR +| 3424: 45 45 58 52 54 52 49 4d 1a 18 05 00 29 0f 19 45 EEXRTRIM....)..E +| 3440: 4e 41 42 4c 45 20 4d 45 4d 53 59 53 35 58 42 49 NABLE MEMSYS5XBI +| 3456: 4e 41 52 59 1a 17 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3472: 45 20 4d 45 4d 53 59 53 35 58 4e 4f 43 41 53 45 E MEMSYS5XNOCASE +| 3488: 19 16 05 00 29 0f 17 45 4e 41 42 4c 45 20 4d 45 ....)..ENABLE ME +| 3504: 4d 53 59 53 35 58 52 54 52 49 4d 18 15 05 00 25 MSYS5XRTRIM....% +| 3520: 0f 19 45 4e 41 42 4c 45 20 4a 53 4f 4e 31 58 42 ..ENABLE JSON1XB +| 3536: 49 4e 41 52 59 18 14 05 00 25 0f 19 45 4e 42 42 INARY....%..ENBB +| 3552: 4d 45 20 4a 53 4f 4e 31 58 4e 4f 43 41 53 45 17 ME JSON1XNOCASE. +| 3568: 13 05 00 25 0f 17 45 4e 41 42 4c 45 20 4a 53 4f ...%..ENABLE JSO +| 3584: 4e 31 58 52 54 52 49 4d 1a 12 05 00 29 0f 19 45 N1XRTRIM....)..E +| 3600: 4e 41 42 4c 45 20 47 45 4f 50 4f 4c 59 58 42 49 NABLE GEOPOLYXBI +| 3616: 4e 41 52 59 1a 11 05 00 29 0f 19 45 4e 41 1e 4c NARY....)..ENA.L +| 3632: 45 20 47 45 4f 50 4f 4c 59 58 4e 4f 43 41 53 45 E GEOPOLYXNOCASE +| 3648: 19 10 05 00 29 0f 17 45 4e 41 42 4c 45 20 47 45 ....)..ENABLE GE +| 3664: 4f 50 4f 4c 59 58 62 54 52 49 4d 17 0f 05 00 23 OPOLYXbTRIM....# +| 3680: 0f 19 45 4e 41 42 4c 45 20 46 54 53 35 58 42 49 ..ENABLE FTS5XBI +| 3696: 4e 41 52 59 17 0e 05 00 23 0f 19 45 4e 41 42 4c NARY....#..ENABL +| 3712: 45 20 46 54 fc 35 58 4e 4f 43 41 53 45 16 0d 05 E FT.5XNOCASE... +| 3728: 00 23 0f 17 45 4e 41 42 4c 45 20 46 54 53 35 58 .#..ENABLE FTS5X +| 3744: 52 54 52 49 4d 17 0c 05 00 23 0f 19 45 4e 41 42 RTRIM....#..ENAB +| 3760: 4c 45 20 46 54 53 34 58 42 49 4e 41 52 59 17 0b LE FTS4XBINARY.. +| 3776: 05 00 23 0f 19 45 4e 41 42 4c 45 20 46 54 53 35 ..#..ENABLE FTS5 +| 3792: 58 4e 4f 43 40 53 45 16 0a 05 00 23 0f 17 45 4e XNOC@SE....#..EN +| 3808: 41 42 4c 45 20 56 54 53 34 58 52 54 52 49 4d 1e ABLE VTS4XRTRIM. +| 3824: 09 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3840: 54 41 54 20 56 54 41 42 58 42 49 4e 41 b3 58 1e TAT VTABXBINA.X. +| 3856: 08 05 00 31 0f 19 45 4e 40 42 4c 45 20 44 42 53 ...1..EN@BLE DBS +| 3872: 54 41 54 20 56 54 41 42 58 4e 4f 43 41 53 45 1d TAT VTABXNOCASE. +| 3888: 07 05 00 31 0f 17 45 4e 41 42 4c 45 20 45 42 53 ...1..ENABLE EBS +| 3904: 54 41 54 20 56 54 41 42 58 52 54 52 49 4d 11 06 TAT VTABXRTRIM.. +| 3920: 05 00 17 0f 19 44 45 42 55 47 58 42 49 4e 41 52 .....DEBUGXBINAR +| 3936: 59 11 05 05 00 17 0f 19 44 45 42 55 47 58 4e 4f Y.......DEBUGXNO +| 3952: 43 41 53 45 10 04 05 00 17 0f 17 44 45 42 55 47 CASE.......DEBUG +| 3968: 58 52 54 52 49 4d 27 03 05 00 43 0f 19 43 4f 4d XRTRIM'...C..COM +| 3984: 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e 30 20 PILER=gcc-5.4.0 +| 4000: 32 30 31 36 30 36 30 39 58 4b 19 4e 41 52 59 27 20160609XK.NARY' +| 4016: 02 05 00 43 0f 19 43 4f 4d 50 49 4c 45 52 3d 67 ...C..COMPILER=g +| 4032: 63 63 2d 35 2e 34 2e 30 20 32 30 31 36 30 36 30 cc-5.4.0 2016060 +| page 5 offset 16384 +| 0: 0d 00 00 00 02 0b a0 00 0c ad 0b a0 00 00 00 00 ................ +| 2976: 82 0a 02 08 08 09 08 08 17 84 06 30 20 32 34 33 ...........0 243 +| 2992: 00 01 30 04 25 06 1b 00 00 08 32 30 31 36 30 36 ..0.%.....201606 +| 3008: 30 39 03 25 07 00 00 01 34 03 25 05 00 00 01 35 09.%....4.%....5 +| 3024: 03 25 04 00 01 07 30 30 30 30 30 30 30 03 25 1a .%....0000000.%. +| 3040: 00 00 08 63 6f 6d 70 69 6c 65 72 03 25 02 00 00 ...compiler.%... +| 3056: 06 64 62 73 74 51 74 03 25 0a 00 01 04 65 62 75 .dbstQt.%....ebu +| 3072: 67 03 25 08 00 00 06 65 6e 61 62 6c 65 09 25 09 g.%....enable.%. +| 3088: 05 04 04 04 04 04 00 01 08 78 74 65 6e 73 69 6f .........xtensio +| 3104: 6e 03 25 1d 00 00 04 66 74 73 34 03 25 0d 00 03 n.%....fts4.%... +| 3120: 01 35 03 25 0f 00 00 03 67 63 63 03 25 03 00 01 .5.%....gcc.%... +| 3136: 06 65 6f 70 6f 6c 79 03 25 11 00 00 05 6a 73 6f .eopoly.%....jso +| 3152: 6e 31 03 25 13 00 00 04 6c 6f 61 64 03 25 1c 00 n1.%....load.%.. +| 3168: 00 03 6d 61 78 03 25 18 00 01 05 65 6d 6f 72 79 ..max.%....emory +| 3184: 03 25 19 00 03 04 73 79 73 35 03 25 15 00 00 04 .%....sys5.%.... +| 3200: 6f 6d 69 74 03 25 1b 00 00 05 72 74 72 65 65 03 omit.%....rtree. +| 3216: 25 17 00 00 0a 74 68 72 65 61 64 73 61 66 65 03 %....threadsafe. +| 3232: 25 1e 00 00 04 76 74 61 62 03 25 0b 00 86 50 01 %....vtab.%...P. +| 3248: 08 08 08 08 08 17 8d 12 30 20 38 33 35 00 01 30 ........0 835..0 +| 3264: 12 01 06 00 01 06 00 01 06 00 1e f3 00 01 03 00 ................ +| 3280: 01 03 00 00 08 32 30 31 36 30 36 30 39 09 01 07 .....20160609... +| 3296: 00 01 07 00 01 07 00 00 01 34 09 01 05 00 01 05 .........4...... +| 3312: 00 01 05 00 00 01 35 09 01 04 00 01 04 00 01 04 ......5......... +| 3328: 00 01 07 30 30 30 30 2f 30 30 09 1c 04 00 01 04 ...0000/00...... +| 3344: 00 01 04 00 00 06 62 69 6e 61 72 79 3c 03 01 02 ......binary<... +| 3360: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3376: 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 ................ +| 3392: 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03 ................ +| 3408: 01 02 02 00 03 01 02 02 00 00 08 63 6f 6d 70 69 ...........compi +| 3424: 6c 65 72 09 01 02 00 01 02 00 01 02 00 00 06 64 ler............d +| 3440: 62 73 74 61 74 09 07 03 00 01 03 00 01 03 00 01 bstat........... +| 3456: 04 65 62 75 67 09 04 02 00 01 02 00 01 02 00 00 .ebug........... +| 3472: 06 65 6e 61 62 6c 65 3f 07 02 00 01 02 00 01 02 .enable?........ +| 3488: 00 01 02 00 01 02 00 01 01 f0 01 02 00 57 02 00 .............W.. +| 3504: 01 02 00 01 02 00 01 02 00 01 02 00 01 02 10 01 ................ +| 3520: 02 00 01 02 00 01 02 00 01 02 00 01 02 00 01 02 ................ +| 3536: 00 00 02 00 01 02 00 01 08 78 74 65 6e 73 69 6f .........xtensio +| 3552: 6e 09 1f 04 00 01 04 00 01 04 00 00 04 66 74 73 n............fts +| 3568: 34 09 0a 03 00 01 03 00 01 03 00 03 01 35 09 0d 4............5.. +| 3584: 03 00 01 03 00 a9 03 00 00 03 67 63 63 09 01 03 ..........gcc... +| 3600: 00 01 03 00 01 03 00 01 06 65 6f 70 6f 6c 79 09 .........eopoly. +| 3616: 10 03 00 01 03 00 01 03 00 00 05 6a 73 6f 6e 31 ...........json1 +| 3632: 09 13 03 00 01 03 00 01 03 00 00 04 6c 6f 61 64 ............load +| 3648: 09 1f 03 00 01 03 00 01 03 00 00 03 6d 61 78 09 ............max. +| 3664: 1c 02 0b 31 02 00 01 02 00 01 05 65 6d 6f 72 79 ...1.......emory +| 3680: 09 1c 03 00 01 03 00 01 03 00 03 04 73 79 73 35 ............sys5 +| 3696: 09 16 03 00 01 03 00 01 03 00 00 06 6e 6f 63 61 ............noca +| 3712: 73 65 3c 02 01 02 02 00 03 01 02 02 00 03 01 02 se<............. +| 3728: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3744: 00 03 01 02 02 00 f3 01 02 02 00 03 01 02 02 00 ................ +| 3760: 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 00 ................ +| 3776: 04 6f 6d 69 74 09 1f 02 00 01 02 00 01 02 00 00 .omit........... +| 3792: 05 72 74 72 65 65 09 19 03 00 01 03 00 01 03 00 .rtree.......... +| 3808: 03 02 69 6d 3c 01 01 02 02 00 03 01 02 02 00 03 ..im<........... +| 3824: 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01 ................ +| 3840: 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 ................ +| 3856: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3872: 00 00 0a 74 68 72 65 61 64 63 61 66 65 09 22 02 ...threadcafe... +| 3888: 00 01 02 00 02 02 00 00 04 76 74 61 62 09 07 04 .........vtab... +| 3904: 00 01 04 00 01 04 00 00 01 78 b4 01 01 01 01 02 .........x...... +| 3920: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| 3936: 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 ................ +| 3952: 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 ................ +| 3968: 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 ................ +| 3984: 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 ................ +| 4000: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| 4016: 01 01 01 02 00 01 01 01 02 00 01 00 01 02 00 01 ................ +| 4032: 01 01 02 00 01 01 00 e2 00 01 01 01 02 00 01 01 ................ +| 4048: 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 ................ +| 4064: 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 ................ +| 4080: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 02 0f f5 00 0f fb 0f f5 00 00 00 00 ................ +| 4080: 00 00 00 00 00 05 04 08 09 01 02 04 04 08 08 09 ................ +| page 7 offset 24576 +| 0: 0d 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 4016: 00 00 00 00 00 00 00 00 0d 05 02 23 61 00 00 00 ...........#a... +| end crash-26682721375870.db +}]} {} + +do_execsql_test 26.1 { + PRAGMA writable_schema = 1; + SELECT count(*) FROM ( + SELECT t1, (t1) FROM t1 WHERE b MATCH 'x' + ) +} 34 + +#------------------------------------------------------------------------- +reset_db +do_test 27.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 28672 pagesize 4096 filename crash-23ddd777a03bfd.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 00 .....@ ........ +| 96: 00 00 00 00 0d 0e b1 00 06 0d a4 00 0f 8d 0f 21 ...............! +| 112: 0e b9 0d c8 0e 7e 0d a4 00 00 00 00 00 00 00 00 .....~.......... +| 3488: 00 00 00 00 22 07 06 17 11 11 01 31 74 61 62 6c ...........1tabl +| 3504: 65 74 32 74 32 07 43 52 45 41 54 45 20 54 41 42 et2t2.CREATE TAB +| 3520: 4c 45 20 74 32 28 78 29 81 33 05 07 17 1f 1f 01 LE t2(x).3...... +| 3536: 82 35 74 61 62 6c 65 74 31 5f 73 65 67 64 69 72 .5tablet1_segdir +| 3552: 74 31 5f 73 65 67 64 69 72 05 43 52 45 41 54 45 t1_segdir.CREATE +| 3568: 20 54 41 42 4c 45 20 27 74 31 5f 73 65 67 64 69 TABLE 't1_segdi +| 3584: 72 27 28 6c 65 76 65 6c 20 49 4e 54 45 47 45 52 r'(level INTEGER +| 3600: 2c 69 64 78 20 49 4e 54 45 47 45 52 2c 73 74 61 ,idx INTEGER,sta +| 3616: 72 74 5f 62 6c 6f 63 6b 20 49 4e 54 45 47 45 52 rt_block INTEGER +| 3632: 2c 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 ,leaves_end_bloc +| 3648: 6b 20 49 4e 54 45 47 45 52 2c 65 6e 64 5f 62 6c k INTEGER,end_bl +| 3664: 6f 63 6b 20 49 4e 54 45 47 45 52 2c 72 6f 6f 74 ock INTEGER,root +| 3680: 20 42 4c 4f 42 2c 50 52 49 4d 41 52 59 20 4b 45 BLOB,PRIMARY KE +| 3696: 59 28 6c 65 76 65 6c 2c 20 69 64 78 29 29 31 06 Y(level, idx))1. +| 3712: 06 17 45 1f 01 00 69 6e 64 65 78 73 71 6c 69 74 ..E...indexsqlit +| 3728: 65 5f 61 75 74 6f 69 6e 64 65 78 5f 74 31 5f 73 e_autoindex_t1_s +| 3744: 65 67 64 69 72 5f 31 74 31 5f 73 65 67 64 69 72 egdir_1t1_segdir +| 3760: 06 0f c7 00 08 00 00 00 00 66 04 07 17 23 23 01 .........f...##. +| 3776: 81 13 74 61 62 6c 65 74 31 5f 73 65 67 6d 65 6e ..tablet1_segmen +| 3792: 64 73 74 31 5f 73 65 67 6d 65 6e 73 73 04 43 52 dst1_segmenss.CR +| 3808: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 73 EATE TABLE 't1_s +| 3824: 65 67 6d 65 6e 74 73 27 28 62 6c 6f 63 6b 69 64 egments'(blockid +| 3840: 20 49 4e 54 45 47 45 52 20 50 52 49 4d 4e 72 59 INTEGER PRIMNrY +| 3856: 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 KEY, block BLOB +| 3872: 29 6a 03 07 17 21 21 01 81 1f 74 61 62 6c 65 74 )j...!!...tablet +| 3888: 31 5f 63 6f 6e 74 65 6e 74 74 31 5f 63 6f 6e 74 1_contentt1_cont +| 3904: 65 6e 74 04 43 52 45 41 54 45 20 54 41 42 4c 45 ent.CREATE TABLE +| 3920: 20 27 74 31 5f 63 6f 6e 74 65 6e 74 27 28 64 6f 't1_content'(do +| 3936: 63 69 64 20 49 4e 54 45 47 45 52 20 50 52 39 4d cid INTEGER PR9M +| 3952: 41 52 59 20 4b 45 59 2c 20 27 63 30 61 27 2c 20 ARY KEY, 'c0a', +| 3968: 27 63 31 62 27 2c 20 27 63 32 63 27 29 38 02 06 'c1b', 'c2c')8.. +| 3984: 17 11 11 08 5f 74 61 62 6c 65 74 31 74 31 43 52 ...._tablet1t1CR +| 4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 33 LE t1 USING fts3 +| 4032: 28 61 2c 62 2c 63 29 00 00 00 00 00 00 00 00 00 (a,b,c)......... +| page 3 offset 8192 +| 0: 0d 00 00 00 26 0b 48 0e 0f d8 0f af 0f 86 0f 74 ....&.H........t +| 16: 0f 61 0f 4e 0f 2f 0f 0f 0e ef 0e 00 00 00 00 00 .a.N./.......... +| 2880: 00 00 00 00 00 00 00 00 81 3f 25 06 00 72 7f 00 .........?%..r.. +| 2896: 00 43 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2e .COMPILER=gcc-5. +| 2912: 34 2e 30 20 32 30 31 36 30 36 30 39 20 44 55 42 4.0 20160609 DUB +| 2928: 55 47 20 45 4e 41 e4 7c 45 20 44 42 53 54 41 54 UG ENA.|E DBSTAT +| 2944: e4 46 54 41 42 20 45 4e 41 42 4c 45 20 46 54 53 .FTAB ENABLE FTS +| 2960: 34 20 45 4e 41 42 4c 45 20 46 54 53 35 20 45 4e 4 ENABLE FTS5 EN +| 2976: 41 42 4c 45 20 47 45 4f 50 4f 4c 59 20 45 4e 41 ABLE GEOPOLY ENA +| 2992: 42 4c 45 20 4a 53 4f 4e 31 20 45 4e 41 42 4c 45 BLE JSON1 ENABLE +| 3008: 20 4d 45 4d 53 59 53 35 20 45 4e 41 42 4c 45 20 MEMSYS5 ENABLE +| 3024: 42 54 52 45 45 20 4d 41 58 20 4d 45 4d 4f 52 59 BTREE MAX MEMORY +| 3040: 3d 35 30 30 30 30 30 30 30 20 4f 4c 49 54 20 4c =50000000 OLIT L +| 3056: 4f 41 43 20 45 58 54 45 4e 53 49 4f 4e 21 54 48 OAC EXTENSION!TH +| 3072: 52 45 41 44 53 41 46 45 3d 30 18 24 05 00 25 0f READSAFE=0.$..%. +| 3088: 19 54 48 52 45 41 44 53 41 46 45 3d 30 58 42 49 .THREADSAFE=0XBI +| 3104: 4e 41 52 59 18 23 05 00 25 0f 19 54 48 52 45 41 NARY.#..%..THREA +| 3120: 44 53 41 4b 75 3d 30 58 4d 4f 43 41 53 45 17 22 DSAKu=0XMOCASE.. +| 3136: 05 00 25 0f 17 54 48 52 45 41 44 53 41 46 46 3d ..%..THREADSAFF= +| 3152: 30 58 52 54 52 49 4d 1f 21 05 00 33 0f 19 4f 4d 0XRTRIM.!..3..OM +| 3168: 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 52 49 4f IT LOAD EXTENRIO +| 3184: 4e 58 42 49 4e 41 52 59 0f 20 05 00 33 0f 19 4f NXBINARY. ..3..O +| 3200: 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 43 49 MIT LOAD EXTENCI +| 3216: 4f 4e 58 4e 4f 43 41 53 45 1e 1f 05 00 33 0f 17 ONXNOCASE....3.. +| 3232: 4f 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 OMIT LOAD EXTENS +| 3248: 49 4f 4e 58 52 5d 12 49 4d 1f 1e 05 00 33 0f 19 IONXR].IM....3.. +| 3264: 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 30 MAX MEMORY=50000 +| 3280: 30 30 30 58 42 49 4e 41 52 59 1f 1d 05 00 33 0f 000XBINARY....3. +| 3296: 19 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 .MAX MEMORY=5000 +| 3312: 30 30 30 30 58 4f 4f 43 41 53 45 1e 1c 05 00 33 0000XOOCASE....3 +| 3328: 0f 17 4d 41 b8 20 4d 45 4d 4f 52 59 3d 35 30 3c ..MA. MEMORY=50< +| 3344: 30 30 30 30 30 58 52 54 52 49 4d 18 1b 05 00 25 00000XRTRIM....% +| 3360: 0f 19 45 4e 41 42 4c 45 20 52 54 52 45 45 58 42 ..ENABLE RTREEXB +| 3376: 49 4e 41 52 59 18 1a 05 0d a5 0f 19 45 4e 41 42 INARY.......ENAB +| 3392: 4c 45 20 52 54 52 45 45 58 4e 4f 43 41 53 45 17 LE RTREEXNOCASE. +| 3408: 19 1c 00 25 0f 17 45 4e 41 42 4c 45 20 52 54 52 ...%..ENABLE RTR +| 3424: 45 45 58 52 54 52 49 4d 1a 18 05 00 29 0f 19 45 EEXRTRIM....)..E +| 3440: 4e 41 42 4c 45 20 4d 45 4d 53 59 53 35 58 42 49 NABLE MEMSYS5XBI +| 3456: 4e 41 53 59 1a 17 05 00 29 0f 19 45 4e 41 42 4c NASY....)..ENABL +| 3472: 45 20 4d 45 4d 53 59 53 35 58 4e 4f 43 41 53 45 E MEMSYS5XNOCASE +| 3488: 19 16 05 00 29 0f 17 45 4e 41 42 4c 45 20 4d 45 ....)..ENABLE ME +| 3504: 4d 53 59 53 35 58 52 54 52 49 4d 18 14 05 01 25 MSYS5XRTRIM....% +| 3520: 0f 19 45 4e 12 42 4c 45 20 4a 53 4f 4e 31 58 42 ..EN.BLE JSON1XB +| 3536: 49 4e 41 52 59 18 14 05 00 25 09 d9 45 4e 41 42 INARY....%..ENAB +| 3552: 4c 45 20 4a 53 4f 3e 31 58 4e 4f 43 41 53 45 17 LE JSO>1XNOCASE. +| 3568: 13 05 00 25 0f 17 45 4e 40 42 4c 45 20 4a 53 4f ...%..EN@BLE JSO +| 3584: 4e 31 58 52 54 52 49 4d 1a 12 05 82 29 0f 19 45 N1XRTRIM....)..E +| 3600: 4e 41 42 4c 45 20 47 45 4f 50 4f 4d 59 58 42 49 NABLE GEOPOMYXBI +| 3616: 4e 41 52 59 1a 11 05 c9 29 e8 19 46 4e 41 42 4c NARY....)..FNABL +| 3632: 48 c0 47 45 4f 50 4f 4c 59 58 4e 74 43 41 53 45 H.GEOPOLYXNtCASE +| 3648: 19 10 05 00 29 0f 17 45 4e 41 42 4c 45 20 47 45 ....)..ENABLE GE +| 3664: 4f 50 4f 4c 59 58 52 54 52 49 4d 17 0f 05 00 23 OPOLYXRTRIM....# +| 3680: 0f 19 45 4e 41 42 4c 45 30 46 54 53 35 58 42 49 ..ENABLE0FTS5XBI +| 3696: 4e 41 52 59 17 0e 05 00 23 0f 19 45 4e 41 42 4c NARY....#..ENABL +| 3712: 45 20 46 54 53 35 58 4e 4f 4a e1 53 45 16 0e 05 E FTS5XNOJ.SE... +| 3728: 00 23 0f 17 45 4e 41 42 4c 45 20 46 54 53 35 58 .#..ENABLE FTS5X +| 3744: 52 54 52 49 4d 17 0c 05 00 23 0f 19 45 4e 41 42 RTRIM....#..ENAB +| 3760: 4c 45 20 46 54 53 34 58 42 49 4e 41 52 59 17 0b LE FTS4XBINARY.. +| 3776: 05 00 23 0f 19 45 4e 41 42 4c 45 20 46 54 53 34 ..#..ENABLE FTS4 +| 3792: 58 4e 4f 43 41 53 45 16 0a 05 00 23 0f 17 55 4e XNOCASE....#..UN +| 3808: 41 42 4c 45 20 46 54 53 34 58 52 54 52 49 4d 1e ABLE FTS4XRTRIM. +| 3824: 09 05 00 31 0f 19 45 4e 42 42 4c 45 20 44 42 53 ...1..ENBBLE DBS +| 3840: 54 41 54 20 56 54 41 42 58 42 49 4e 41 52 59 1e TAT VTABXBINARY. +| 3856: 08 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3872: 54 41 54 20 56 54 41 42 58 4e 4f 43 41 53 45 1d TAT VTABXNOCASE. +| 3888: 07 05 00 31 0f 17 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3904: 54 41 54 20 56 54 41 42 58 52 54 52 4a 4d 11 06 TAT VTABXRTRJM.. +| 3920: 05 f0 17 0f 29 44 45 42 55 47 58 42 49 4e 41 52 ....)DEBUGXBINAR +| 3936: 59 11 05 05 00 17 0e 19 44 45 42 55 47 58 4e 4f Y.......DEBUGXNO +| 3952: 43 41 53 45 10 04 05 00 17 0f 16 44 45 42 55 47 CASE.......DEBUG +| 3968: 58 52 54 52 49 4d 27 03 05 00 43 0f 19 43 4f 4d XRTRIM'...C..COM +| 3984: 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e 30 20 PILER=gcc-5.4.0 +| 4000: 32 30 31 36 30 36 30 39 58 42 49 4e 41 52 59 27 20160609XBINARY' +| 4016: 02 05 00 43 0f 19 43 4f 4d 50 49 4c 45 52 3d 67 ...C..COMPILER=g +| 4032: 63 63 2d 35 2e 34 2e 30 20 32 30 31 36 30 36 30 cc-5.4.0 2016060 +| 4048: 39 58 4e 4f 43 41 53 45 26 01 06 00 43 0f 17 43 9XNOCASE&...C..C +| 4064: 4f 4d 50 49 4b 45 52 3d 67 63 63 2d 35 2e 34 2e OMPIKER=gcc-5.4. +| 4080: 30 20 32 30 31 36 30 36 40 39 58 29 54 52 49 4d 0 201606@9X)TRIM +| page 4 offset 12288 +| 0: 0d 00 10 00 00 10 00 00 00 00 00 00 00 00 00 00 ................ +| 16: 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| page 5 offset 16384 +| 0: 0d 00 00 00 02 0b a0 00 0c ad 0b a0 00 00 00 00 ................ +| 2976: 82 0a 02 08 08 09 08 08 17 84 06 30 20 32 35 33 ...........0 253 +| 2992: 00 01 30 04 25 06 1b 00 00 08 32 30 31 36 30 36 ..0.%.....201606 +| 3008: 30 39 03 25 07 00 00 01 34 03 25 05 00 00 01 35 09.%....4.%....5 +| 3024: 03 25 04 00 01 07 30 30 30 30 30 30 30 03 25 1a .%....0000000.%. +| 3040: 00 00 08 63 6f 6d 70 69 6c 65 72 03 25 02 00 00 ...compiler.%... +| 3056: 06 64 62 73 74 61 74 03 25 0a 00 01 04 65 62 75 .dbstat.%....ebu +| 3072: 67 03 25 08 00 00 06 65 6e 61 62 7c 65 09 25 09 g.%....enab|e.%. +| 3088: 05 04 04 04 04 04 00 01 08 78 74 65 6e 73 69 6f .........xtensio +| 3104: 6e 03 25 1d 00 00 04 66 74 73 34 03 25 0d 00 03 n.%....fts4.%... +| 3120: 01 35 03 25 0f 00 00 03 67 63 63 03 25 03 00 01 .5.%....gcc.%... +| 3136: 06 65 6f 70 6f 6c 79 03 25 11 00 00 05 6a 73 6f .eopoly.%....jso +| 3152: 6e 31 03 25 13 00 00 04 6c 6f 61 64 03 25 1c 00 n1.%....load.%.. +| 3168: 00 03 6d 61 78 03 25 18 00 01 05 65 6e 6f 72 79 ..max.%....enory +| 3184: 03 25 19 00 03 04 ce 79 73 4d 03 25 15 00 00 04 .%.....ysM.%.... +| 3200: 6f 6d 69 74 03 25 1b 00 00 05 72 74 72 65 65 03 omit.%....rtree. +| 3216: 25 17 00 00 0a 74 68 72 65 61 64 73 61 66 65 03 %....threadsafe. +| 3232: 25 0e 00 00 04 76 74 61 62 03 25 0b 00 86 50 01 %....vtab.%...P. +| 3248: 08 08 08 08 08 17 8d 12 30 20 38 33 35 00 01 30 ........0 835..0 +| 3264: 12 01 06 00 01 06 00 01 06 00 1f 03 00 01 03 00 ................ +| 3280: 01 03 00 00 08 32 30 31 36 30 36 30 39 09 01 07 .....20160609... +| 3296: 00 01 07 00 01 07 00 00 01 34 09 01 05 00 01 05 .........4...... +| 3312: 00 01 05 00 00 01 35 09 01 04 00 01 04 00 01 04 ......5......... +| 3328: 00 01 07 30 30 30 30 30 30 30 09 1c 04 00 01 04 ...0000000...... +| 3344: 00 01 04 00 00 06 62 69 6e 61 72 79 3c 03 01 02 ......binary<... +| 3360: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3376: 00 03 01 02 02 00 02 f1 02 02 00 03 01 02 02 00 ................ +| 3392: 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03 ................ +| 3408: 01 01 f2 00 03 01 02 02 00 00 08 63 6f 6d 70 69 ...........compi +| 3424: 6c 65 72 09 01 02 00 01 02 00 01 02 00 00 06 67 ler............g +| 3440: d2 73 74 61 74 09 07 03 00 01 03 00 01 03 00 01 .stat........... +| 3456: 04 65 62 75 67 09 04 02 00 01 02 00 01 02 00 00 .ebug........... +| 3472: 06 65 6e 6f 82 6c 65 3f 07 02 00 01 02 00 01 02 .eno.le?........ +| 3488: b0 01 02 00 01 02 00 11 0a f0 01 02 00 01 02 00 ................ +| 3504: 01 02 00 01 02 00 01 02 00 01 a6 00 01 02 00 02 ................ +| 3520: 02 05 51 02 00 01 02 00 01 02 00 01 02 00 01 02 ..Q............. +| 3536: 00 01 02 00 01 02 00 01 08 78 74 65 6e 73 69 6f .........xtensio +| 3552: 6e 09 1f 04 00 01 04 00 00 04 00 00 04 66 74 73 n............fts +| 3568: 34 09 0a 03 00 01 03 00 01 03 00 03 01 35 09 0d 4............5.. +| 3584: 03 00 01 03 00 01 03 00 00 03 67 63 63 09 01 03 ..........gcc... +| 3600: 00 01 03 00 01 03 00 01 06 65 6f 70 73 6c 79 09 .........eopsly. +| 3616: 10 03 00 01 03 00 01 03 00 00 05 6a 73 6f 6e 31 ...........json1 +| 3632: 09 13 03 00 01 03 00 01 03 00 00 04 6c 6f 61 64 ............load +| 3648: 09 1f 03 00 01 03 00 01 03 00 00 03 6d 61 78 09 ............max. +| 3664: 1c 02 00 01 02 00 01 02 00 01 05 65 6d 6f 72 79 ...........emory +| 3680: 09 1c 03 00 01 03 00 01 03 00 03 04 73 79 73 35 ............sys5 +| 3696: 09 16 03 00 01 03 00 01 03 00 00 06 6e 6f 63 61 ............noca +| 3712: 73 65 3c 02 01 02 02 00 03 01 12 02 00 03 01 02 se<............. +| 3728: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3744: 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 ................ +| 3760: 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 00 ................ +| 3776: 04 6f 6d 69 74 09 1f 02 00 01 02 00 01 02 00 00 .omit........... +| 3792: 05 72 74 72 65 65 09 19 03 00 01 03 00 01 03 00 .rtree.......... +| 3808: 03 02 69 6d 3c 01 01 02 02 00 03 01 02 02 00 03 ..im<........... +| 3824: 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01 ................ +| 3840: 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 ................ +| 3856: 02 00 03 01 02 02 01 e3 01 02 02 00 03 01 02 02 ................ +| 3872: 00 00 0a 74 68 72 65 61 64 73 61 66 65 09 22 02 ...threadsafe... +| 3888: 00 01 02 00 01 02 00 00 04 76 74 61 62 09 07 04 .........vtab... +| 3904: 00 01 04 00 01 04 00 00 01 78 b4 01 01 01 01 02 .........x...... +| 3920: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| 3936: 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 ................ +| 3952: 01 01 01 f0 01 01 01 07 30 01 01 01 02 00 01 01 ........0....... +| 3968: 01 02 00 ea 01 01 02 00 01 01 01 02 00 11 01 01 ................ +| 3984: 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 ................ +| 4000: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| 4016: 01 01 01 01 ff 01 01 01 02 00 01 01 01 02 00 01 ................ +| 4032: 01 01 02 00 01 11 01 02 00 01 01 01 02 00 01 01 ................ +| 4048: 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 ................ +| 4064: 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 ................ +| 4080: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 02 0f f5 00 0f fb 0f f5 01 00 00 00 ................ +| 4080: 00 00 00 00 00 05 04 08 09 01 02 04 04 08 08 09 ................ +| page 7 offset 24576 +| 0: 0d 00 00 00 05 0f b8 00 0e f4 0f e9 10 d6 0f c7 ................ +| 4016: 00 00 00 00 00 00 00 00 0f 85 02 23 61 75 74 6f ...........#auto +| 4032: 6d 65 72 67 65 3d 35 0d 04 02 23 6d 65 72 67 65 merge=5...#merge +| 4048: 3d 31 00 00 00 00 00 00 00 00 00 00 00 00 00 00 =1.............. +| end crash-23ddd777a03bfd.db +}]} {} + +do_catchsql_test 27.2 { + PRAGMA writable_schema = 1; + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x GLOB 2.16770 FROM x) + INSERT INTO t1(a) SELECT randomblob(3000) FROM t2 ; +} {1 {database disk image is malformed}} +do_catchsql_test 27.3 { + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT 3+x FROM c WHERE x<2.653) + INSERT INTO t1(a) SELECT randomblob(-current_time) FROM c; +} {1 {database disk image is malformed}} +do_catchsql_test 27.4 { + UPDATE t1 SET b=quote((true) ) WHERE t1 MATCH 'h*h*'; +} {0 {}} +do_catchsql_test 27.5 { + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT 3+x FROM c WHERE x<2.653) + INSERT INTO t1(a) SELECT randomblob(-current_time) FROM c; +} {1 {database disk image is malformed}} +do_catchsql_test 27.5 { + INSERT INTO t1(t1) SELECT x FROM t2; +} {1 {database disk image is malformed}} +do_catchsql_test 27.6 { + INSERT INTO t1(t1) SELECT x FROM t2; +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +reset_db +do_test 28.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 28672 pagesize 4096 filename crash-159ac1ca51ed55.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 00 .....@ ........ +| 96: 00 00 00 00 0d 0e b1 00 06 0d a4 00 0f 8d 0f 21 ...............! +| 112: 0e b9 0d c8 0e 7e 0d a4 00 00 00 00 00 00 00 00 .....~.......... +| 3488: 00 00 00 00 22 07 06 17 11 11 01 31 74 61 62 6c ...........1tabl +| 3504: 65 74 32 74 32 07 43 52 45 41 54 45 20 54 41 42 et2t2.CREATE TAB +| 3520: 4c 45 20 74 32 28 78 29 81 33 05 07 17 1f 1f 01 LE t2(x).3...... +| 3536: 82 35 74 61 62 6c 65 74 31 5f 73 65 67 64 69 72 .5tablet1_segdir +| 3552: 74 31 5f 73 65 67 64 69 72 05 43 52 45 41 54 45 t1_segdir.CREATE +| 3568: 20 54 41 42 4c 45 20 27 74 31 5f 73 65 67 64 69 TABLE 't1_segdi +| 3584: 72 27 28 6c 65 76 65 6c 20 49 4e 54 45 47 45 52 r'(level INTEGER +| 3600: 2c 69 64 78 20 49 4e 54 45 47 45 52 2c 73 74 61 ,idx INTEGER,sta +| 3616: 72 74 5f 62 6c 6f 63 6b 20 49 4e 54 45 47 45 52 rt_block INTEGER +| 3632: 2c 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 ,leaves_end_bloc +| 3648: 6b 20 49 4e 54 45 47 45 52 2c 65 6e 64 5f 62 6c k INTEGER,end_bl +| 3664: 6f 63 6b 20 49 4e 54 45 47 45 52 2c 72 6f 6f 74 ock INTEGER,root +| 3680: 20 42 4c 4f 42 2c 50 52 49 4d 41 52 59 20 4b 45 BLOB,PRIMARY KE +| 3696: 59 28 6c 65 76 65 6c 2c 20 69 64 78 29 29 31 06 Y(level, idx))1. +| 3712: 06 17 45 1f 01 00 69 6e 64 65 78 73 71 6c 69 74 ..E...indexsqlit +| 3728: 65 5f 61 75 74 6f 69 6e 64 65 78 5f 74 31 5f 73 e_autoindex_t1_s +| 3744: 65 67 64 69 72 5f 31 74 31 5f 73 65 67 64 69 72 egdir_1t1_segdir +| 3760: 06 0f c7 00 08 00 10 00 00 66 04 07 17 23 23 01 .........f...##. +| 3776: 81 13 74 61 62 6c 65 74 31 5f 73 65 67 6d 65 6e ..tablet1_segmen +| 3792: 64 73 74 31 5f 73 65 67 6d 65 6e 74 73 04 43 52 dst1_segments.CR +| 3808: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 73 EATE TABLE 't1_s +| 3824: 65 67 6d 65 6e 74 73 27 28 62 6c 6f 63 6b 69 64 egments'(blockid +| 3840: 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 INTEGER PRIMARY +| 3856: 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 KEY, block BLOB +| 3872: 29 6a 03 07 17 21 21 01 81 1f 74 61 62 6c 65 74 )j...!!...tablet +| 3888: 31 4f 63 6f 6e 74 65 6e 74 74 31 5f 63 6f 6e 74 1Ocontentt1_cont +| 3904: 65 6e 74 03 43 52 45 41 54 45 20 54 41 42 4c 45 ent.CREATE TABLE +| 3920: 20 27 74 31 5f 63 6f 6e 74 65 6e 74 27 28 64 6f 't1_content'(do +| 3936: 63 69 64 20 49 4e 54 45 43 a5 52 20 50 52 39 4d cid INTEC.R PR9M +| 3952: 41 52 59 20 4b 45 59 2c 20 27 63 30 61 27 2c 20 ARY KEY, 'c0a', +| 3968: 27 63 31 62 27 2c 20 27 63 32 63 27 29 38 02 06 'c1b', 'c2c')8.. +| 3984: 17 11 11 08 5f 74 61 62 6c 65 74 31 74 31 43 52 ...._tablet1t1CR +| 4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 33 LE t1 USING fts3 +| 4032: 28 61 2c 62 2c 63 29 00 00 00 00 00 00 00 00 00 (a,b,c)......... +| page 3 offset 8192 +| 0: 0d 00 00 00 26 0b 48 00 00 00 00 00 00 00 00 00 ....&.H......... +| 2880: 00 00 00 00 00 00 00 00 81 3f 25 06 00 72 7f 00 .........?%..r.. +| 2896: 00 43 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2e .COMPILER=gcc-5. +| 2912: 34 2e 30 20 32 30 31 36 30 36 30 39 20 44 55 42 4.0 20160609 DUB +| 2928: 55 47 20 45 4e 41 e4 7c 45 20 44 42 53 54 41 54 UG ENA.|E DBSTAT +| 2944: e4 46 54 41 42 20 45 4e 41 42 4c 45 20 46 54 53 .FTAB ENABLE FTS +| 2960: 34 20 45 4e 41 42 4c 45 20 46 54 53 35 20 45 4e 4 ENABLE FTS5 EN +| 2976: 41 42 1f 45 20 47 45 4f 50 4f 4c 59 20 45 4e 41 AB.E GEOPOLY ENA +| 2992: 42 4c 45 20 4a 53 4f 4e 31 20 45 4e 41 42 4c 45 BLE JSON1 ENABLE +| 3008: 20 4d 45 4d 53 59 53 35 20 45 4e 41 42 4c 45 20 MEMSYS5 ENABLE +| 3024: 42 54 52 45 45 20 4d 41 58 20 4d 45 4d 4f 52 59 BTREE MAX MEMORY +| 3040: 3d 35 30 30 30 30 30 30 30 20 4f 4c 49 54 20 4c =50000000 OLIT L +| 3056: 4f 41 43 20 45 58 54 45 4e 53 49 4f 4e 21 54 48 OAC EXTENSION!TH +| 3072: 52 45 41 44 53 41 46 45 3d 2f 18 24 05 00 25 0f READSAFE=/.$..%. +| 3088: 19 54 48 52 45 41 44 53 41 46 45 3d 30 58 42 49 .THREADSAFE=0XBI +| 3104: 4e 41 52 59 18 23 05 00 25 0f 19 54 48 52 45 41 NARY.#..%..THREA +| 3120: 44 53 41 4b 75 3d 30 58 4d 4f 43 41 53 45 17 22 DSAKu=0XMOCASE.. +| 3136: 05 00 25 0f 17 54 48 52 45 41 44 53 41 46 46 3d ..%..THREADSAFF= +| 3152: 30 58 52 54 52 49 4d 1f 21 05 00 33 0f 19 4f 4d 0XRTRIM.!..3..OM +| 3168: 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 52 49 4f IT LOAD EXTENRIO +| 3184: 4e 58 42 49 4e 41 52 59 1f 20 05 00 33 0f 19 4f NXBINARY. ..3..O +| 3200: 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 MIT LOAD EXTENSI +| 3216: 4f 4e 58 4e 4f 43 41 53 45 1e 1f 05 00 33 0f 17 ONXNOCASE....3.. +| 3232: 4f 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 OMIT LOAD EXTENS +| 3248: 49 4f 4e 58 52 54 52 49 4d 1f 1e 05 00 33 0f 19 IONXRTRIM....3.. +| 3264: 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 30 MAX MEMORY=50000 +| 3280: 30 30 30 58 42 49 4e 41 52 59 1f 1d 05 00 33 0f 000XBINARY....3. +| 3296: 19 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 .MAX MEMORY=5000 +| 3312: 30 30 30 30 58 4e 4f 43 41 53 45 1e 1c 05 00 33 0000XNOCASE....3 +| 3328: 0f 17 4d 42 b8 20 4d 45 4d 4f 52 59 3d 35 30 30 ..MB. MEMORY=500 +| 3344: 30 30 30 30 30 58 52 54 52 4a 4d 18 1b 05 00 25 00000XRTRJM....% +| 3360: 0f 19 45 4e 41 42 4c 45 20 52 54 52 45 45 58 42 ..ENABLE RTREEXB +| 3376: 49 4e 41 52 59 18 1a 05 0d a5 0f 19 45 4e 41 42 INARY.......ENAB +| 3392: 4c 45 20 52 54 52 45 45 58 4e 4f 43 41 53 45 17 LE RTREEXNOCASE. +| 3408: 19 1c 00 25 0f 17 45 4e 41 42 4c 45 20 52 54 52 ...%..ENABLE RTR +| 3424: 45 45 58 52 54 52 49 4d 1a 18 05 00 29 0f 19 45 EEXRTRIM....)..E +| 3440: 4e 41 42 4c 45 20 4d 45 4d 53 59 53 35 58 42 49 NABLE MEMSYS5XBI +| 3456: 4e 41 52 59 1a 17 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3472: 45 20 4d 45 4d 53 59 53 35 58 4e 4f 43 41 53 45 E MEMSYS5XNOCASE +| 3488: 19 16 05 00 29 0f 17 45 4e 41 42 4c 45 20 4d 45 ....)..ENABLE ME +| 3504: 4d 53 59 53 35 58 52 54 52 49 4d 18 14 05 01 25 MSYS5XRTRIM....% +| 3520: 0f 19 45 4e 12 42 4c 45 20 4a 53 4f 4e 31 58 42 ..EN.BLE JSON1XB +| 3536: 49 4e 41 52 59 18 14 05 00 25 09 d9 45 4e 41 42 INARY....%..ENAB +| 3552: 4c 45 20 4a 53 4f 3e 31 58 4e 4f 43 41 53 45 17 LE JSO>1XNOCASE. +| 3568: 13 05 00 25 0f 17 44 4e 40 42 4c 45 20 4a 53 4f ...%..DN@BLE JSO +| 3584: 4e 31 58 52 54 52 49 4d 1a 12 05 82 29 0f 19 45 N1XRTRIM....)..E +| 3600: 4e 41 42 4c 45 20 47 45 4f 50 4f 4c 59 58 42 49 NABLE GEOPOLYXBI +| 3616: 4e 41 52 59 1a 11 05 c9 29 e8 19 46 4e 41 42 4c NARY....)..FNABL +| 3632: 48 c0 47 45 4f 50 4f 4c 59 58 4e 74 43 41 53 45 H.GEOPOLYXNtCASE +| 3648: 19 10 05 00 29 0f 17 45 4e 41 42 4c 45 20 47 45 ....)..ENABLE GE +| 3664: 4f 50 4f 4c 59 58 52 54 52 49 4d 17 0f 05 00 23 OPOLYXRTRIM....# +| 3680: 0f 19 45 4e 41 42 4c 45 30 46 54 53 35 58 42 49 ..ENABLE0FTS5XBI +| 3696: 4e 41 52 59 17 0e 05 00 23 0f 19 45 4e 41 42 4c NARY....#..ENABL +| 3712: 45 20 46 54 53 35 58 4e 4f 43 41 53 45 16 0e 05 E FTS5XNOCASE... +| 3728: 00 23 0f 17 45 4e 41 42 4c 45 20 46 54 53 35 58 .#..ENABLE FTS5X +| 3744: 52 54 52 49 4d 17 0c 05 00 23 0f 19 45 4e 41 42 RTRIM....#..ENAB +| 3760: 4c 45 20 46 54 53 34 58 42 49 4e 41 52 59 17 0b LE FTS4XBINARY.. +| 3776: 04 ff 23 0f 19 45 4e 41 42 4c 45 20 46 54 53 34 ..#..ENABLE FTS4 +| 3792: 58 4e 4f 43 41 53 45 16 0a 05 00 23 0f 17 45 4e XNOCASE....#..EN +| 3808: 41 42 4c 45 20 46 54 53 34 58 52 54 52 49 4d 1e ABLE FTS4XRTRIM. +| 3824: 09 05 00 31 0f 19 45 4e 42 42 4c 45 20 44 42 53 ...1..ENBBLE DBS +| 3840: 54 41 54 20 56 54 41 42 58 42 49 4e 41 52 59 1e TAT VTABXBINARY. +| 3856: 08 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3872: 54 41 54 20 56 54 41 42 58 4e 4f 43 41 53 45 1d TAT VTABXNOCASE. +| 3888: 07 05 00 31 0f 17 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3904: 54 41 54 20 56 54 41 42 58 52 54 52 4a 4d 11 06 TAT VTABXRTRJM.. +| 3920: 05 f0 17 0f 19 44 45 42 55 47 58 42 49 4e 41 52 .....DEBUGXBINAR +| 3936: 59 11 05 09 b0 17 0e 19 44 45 42 55 47 58 4e 4f Y.......DEBUGXNO +| 3952: 43 41 53 45 10 04 05 00 17 0f 16 44 45 42 55 47 CASE.......DEBUG +| 3968: 58 52 54 52 49 4d 27 03 05 00 43 0f 19 43 4f 4d XRTRIM'...C..COM +| 3984: 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e 30 20 PILER=gcc-5.4.0 +| 4000: 32 30 31 36 30 36 30 39 58 42 49 4e 41 52 59 27 20160609XBINARY' +| 4016: 02 05 00 43 0f 19 43 4f 4d 50 49 4c 45 52 4d 67 ...C..COMPILERMg +| 4032: 63 63 2d 35 2e 34 2e 30 20 32 30 31 36 30 36 30 cc-5.4.0 2016060 +| 4048: 39 58 4e 4f 43 41 53 45 26 01 06 00 43 0f 17 43 9XNOCASE&...C..C +| 4064: 4f 4d 50 49 4b 45 52 3d 67 63 63 2d 35 2e 34 2e OMPIKER=gcc-5.4. +| 4080: 30 20 32 30 31 36 30 36 40 39 58 29 54 52 49 4d 0 201606@9X)TRIM +| page 4 offset 12288 +| 0: 0d 00 10 00 00 10 00 00 00 00 00 00 00 00 00 00 ................ +| page 5 offset 16384 +| 0: 0d 00 00 00 02 0b a0 00 00 00 00 00 00 00 00 00 ................ +| 2976: 82 0a 02 08 08 09 08 08 17 84 06 30 20 32 35 33 ...........0 253 +| 2992: 00 01 30 04 25 06 1b 00 00 08 32 30 31 36 30 36 ..0.%.....201606 +| 3008: 30 39 03 25 07 00 00 01 34 03 25 05 00 00 01 35 09.%....4.%....5 +| 3024: 03 25 04 00 4d 07 30 30 30 30 30 30 30 03 25 1a .%..M.0000000.%. +| 3040: 00 00 08 63 6f 6d 70 69 6c 65 72 03 25 02 00 00 ...compiler.%... +| 3056: 06 64 62 73 74 61 74 03 25 0a 00 01 04 65 62 75 .dbstat.%....ebu +| 3072: 67 03 25 08 00 00 06 65 6e 61 62 6c 65 09 25 09 g.%....enable.%. +| 3088: 05 04 04 04 04 04 00 01 08 78 74 65 6e 73 69 6f .........xtensio +| 3104: 6e 03 25 1d 00 00 04 66 74 73 34 03 25 0d 00 03 n.%....fts4.%... +| 3120: 01 35 03 25 0f 00 00 03 67 63 63 03 25 03 00 01 .5.%....gcc.%... +| 3136: 06 65 6f 70 6f 6c 79 03 25 10 ff ff f5 6a 73 6f .eopoly.%....jso +| 3152: 6e 31 03 25 13 00 00 04 6c 6f 61 64 03 25 1c 00 n1.%....load.%.. +| 3168: 00 03 6d 71 78 03 25 18 00 01 05 65 6e 6f 72 79 ..mqx.%....enory +| 3184: 03 25 19 00 03 04 ce 79 73 4d 03 25 15 00 00 04 .%.....ysM.%.... +| 3200: 6f 6d 69 74 03 25 1b 00 00 05 72 74 72 65 65 03 omit.%....rtree. +| 3216: 25 17 00 00 0a 7f 08 72 65 61 64 73 61 66 65 03 %......readsafe. +| 3232: 25 0e 00 00 04 76 75 61 62 03 25 0b 00 86 50 01 %....vuab.%...P. +| 3248: 08 08 08 08 08 17 8d 12 30 20 38 33 35 00 01 30 ........0 835..0 +| 3264: 12 01 06 00 01 06 00 01 06 00 1f 03 00 01 03 09 ................ +| 3280: 51 03 00 00 09 32 30 31 36 30 36 30 39 09 01 07 Q....20160609... +| 3296: 00 01 07 00 01 07 00 00 01 34 09 01 05 00 01 05 .........4...... +| 3312: 00 01 05 00 00 01 35 09 01 04 00 01 03 ff 01 04 ......5......... +| 3328: 00 01 07 30 30 30 30 30 30 30 09 1c 04 00 01 04 ...0000000...... +| 3344: 00 01 04 00 00 06 62 69 6e 61 72 79 3c 03 01 02 ......binary<... +| 3360: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3376: 00 03 01 02 02 00 02 f1 02 02 00 03 01 02 02 00 ................ +| 3392: 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03 ................ +| 3408: 01 02 02 00 03 01 02 02 00 00 08 63 6f 6d 70 69 ...........compi +| 3424: 6c 65 72 09 01 02 00 01 02 00 01 02 00 00 06 67 ler............g +| 3440: d2 73 74 61 74 09 07 03 00 01 03 00 01 03 00 01 .stat........... +| 3456: 04 65 62 75 67 09 04 02 00 01 02 00 01 02 00 00 .ebug........... +| 3472: 06 65 6e 6f 82 6c 65 3f 07 02 00 01 02 00 01 02 .eno.le?........ +| 3488: b0 01 02 00 00 f2 00 11 02 00 01 02 00 01 02 00 ................ +| 3504: 01 02 00 01 02 00 01 02 00 01 a6 00 01 02 00 01 ................ +| 3520: 02 05 51 02 00 01 02 00 01 02 00 01 02 00 01 02 ..Q............. +| 3536: 00 01 02 00 01 02 00 01 08 78 74 65 6e 73 69 6f .........xtensio +| 3552: 6e 09 1f 04 00 01 04 00 00 04 00 00 04 66 74 73 n............fts +| 3568: 34 09 0a 03 00 01 03 00 01 03 00 03 01 35 09 0d 4............5.. +| 3584: 03 00 01 03 00 01 03 00 00 03 67 63 63 09 01 03 ..........gcc... +| 3600: 00 01 03 00 01 03 00 01 06 65 6f 70 73 6c 79 09 .........eopsly. +| 3616: 10 03 00 01 03 00 01 03 00 00 05 6a 73 6f 6e 31 ...........json1 +| 3632: 09 13 03 00 01 03 00 01 03 00 00 04 6c 6f 61 64 ............load +| 3648: 09 1f 03 00 01 03 00 01 03 00 00 03 6d 61 78 09 ............max. +| 3664: 1c 02 00 01 02 00 01 02 00 01 05 65 6d 6f 72 79 ...........emory +| 3680: 09 1c 03 00 01 03 00 01 03 00 03 04 73 79 73 35 ............sys5 +| 3696: 09 16 03 00 01 03 00 01 03 00 00 06 6e 6f 63 61 ............noca +| 3712: 73 65 3c 02 01 02 02 00 03 01 12 02 00 03 01 02 se<............. +| 3728: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3744: 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 ................ +| 3760: 0f 71 02 02 00 03 01 02 02 00 03 6f 02 02 00 00 .q.........o.... +| 3776: 04 6f 6d 69 74 09 1f 02 00 01 02 00 01 02 00 00 .omit........... +| 3792: 05 72 74 72 65 65 09 19 03 00 01 03 00 01 03 00 .rtree.......... +| 3808: 03 02 69 6d 3c 01 01 02 02 00 03 01 02 02 00 03 ..im<........... +| 3824: 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01 ................ +| 3840: 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 ................ +| 3856: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3872: 00 00 0a 74 68 72 65 61 64 73 61 66 65 09 22 02 ...threadsafe... +| 3888: 00 01 02 00 01 02 00 00 04 76 74 61 62 09 07 04 .........vtab... +| 3904: 00 01 04 00 01 04 00 00 01 78 b4 01 01 01 01 02 .........x...... +| 3920: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| 3936: 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 ................ +| 3952: 01 01 02 00 01 01 01 07 30 01 01 01 02 00 01 01 ........0....... +| 3968: 01 02 00 11 01 01 02 00 01 01 01 02 00 11 01 01 ................ +| 3984: 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 ................ +| 4000: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| 4016: 01 01 01 01 ff 01 01 01 02 00 01 01 01 02 00 01 ................ +| 4032: 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 ................ +| 4048: 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 ................ +| 4064: 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 ................ +| 4080: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 02 0f f5 00 00 00 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 05 04 08 09 01 02 04 04 08 08 09 ................ +| page 7 offset 24576 +| 0: 0d 00 00 00 05 0f b8 00 0e f4 0f e9 10 d6 0f c7 ................ +| 4016: 00 00 00 00 00 00 00 00 0d 05 02 23 61 75 74 6f ...........#auto +| 4032: 6d 65 72 67 65 3d 35 0d 04 02 23 6d 65 72 67 65 merge=5...#merge +| 4048: 3d 31 00 00 00 00 00 00 00 00 00 00 00 00 00 00 =1.............. +| end crash-159ac1ca51ed55.db +}]} {} + +do_catchsql_test 28.1 { + PRAGMA writable_schema = 1; + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT 3+x FROM c WHERE x<72) + INSERT INTO t1(a) SELECT randomblob(2829) FROM c; +} {1 {database disk image is malformed}} + +do_catchsql_test 28.2 { + UPDATE t1 SET b=quote((true) ) WHERE t1 MATCH 'h'; +} {0 {}} + +do_catchsql_test 28.3 { + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT 3+x FROM c WHERE x<72) + INSERT INTO t1(a) SELECT randomblob(2829) FROM c; +} {1 {database disk image is malformed}} + +do_catchsql_test 28.4 { + WITH c(x) AS (VALUES(1) UNION ALL SELECT 3<<x FROM c WHERE x<72) + INSERT INTO t1(a) SELECT randomblob(2829) FROM c; +} {0 {}} + +do_catchsql_test 28.5 { + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT 3+x FROM c WHERE x<72) + INSERT INTO t1(a) SELECT randomblob(2829) FROM c; +} {1 {database disk image is malformed}} + +do_catchsql_test 28.6 { + WITH c(x) AS (VALUES(1) UNION ALL SELECT 3<<x FROM c WHERE x<72) + INSERT INTO t1(a) SELECT randomblob(2829) FROM c; +} {0 {}} + +do_catchsql_test 28.7 { + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+3 FROM c WHERE x<72) + INSERT INTO t1(a) SELECT randomblob(2829) FROM c; +} {1 {database disk image is malformed}} + +do_catchsql_test 28.8 { + INSERT INTO t1(t1) SELECT x FROM t2; +} {0 {}} + +#------------------------------------------------------------------------- +# +reset_db +do_test 29.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 28672 pagesize 4096 filename crash-53f41622dd3bf6.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 00 .....@ ........ +| 96: 00 00 00 00 0d 0e b1 00 06 0d a4 00 0f 8d 0f 21 ...............! +| 112: 0e b9 0d c8 0e 7e 0d a4 00 00 00 00 00 00 00 00 .....~.......... +| 3488: 00 00 00 00 22 07 06 17 11 11 01 31 74 61 62 6c ...........1tabl +| 3504: 65 74 32 74 32 07 43 52 45 41 54 45 20 54 41 42 et2t2.CREATE TAB +| 3520: 4c 45 20 74 32 28 78 29 81 33 05 07 17 1f 1f 01 LE t2(x).3...... +| 3536: 82 35 74 61 62 6c 65 74 31 5f 73 65 67 54 69 72 .5tablet1_segTir +| 3552: 74 31 5f 73 65 67 64 69 72 05 43 52 45 41 54 45 t1_segdir.CREATE +| 3568: 20 54 41 42 4c 45 20 27 74 31 5f 73 65 67 64 69 TABLE 't1_segdi +| 3584: 72 27 28 6c 65 76 65 6c 20 49 4e 54 45 47 45 52 r'(level INTEGER +| 3600: 2c 69 64 78 20 49 4d 54 45 47 45 52 2c 73 74 61 ,idx IMTEGER,sta +| 3616: 72 74 5f 62 6c 6f 63 6b 20 49 4e 54 45 47 45 52 rt_block INTEGER +| 3632: 2c 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 ,leaves_end_bloc +| 3648: 6b 20 49 4e 54 45 47 45 52 2c 65 6e 64 5f 62 6c k INTEGER,end_bl +| 3664: 6f 63 6b 20 49 4e 54 45 47 45 52 2c 72 6f 6f 74 ock INTEGER,root +| 3680: 20 42 4c 4f 42 2c 50 52 49 4d 41 52 59 20 4b 45 BLOB,PRIMARY KE +| 3696: 59 28 6c 65 76 65 6c 2c 20 69 64 78 29 29 31 06 Y(level, idx))1. +| 3712: 06 17 45 1f 01 00 69 6e 64 65 78 73 71 6c 69 74 ..E...indexsqlit +| 3728: 65 5f 61 75 74 6f 69 6e 64 65 78 5f 74 31 5f 73 e_autoindex_t1_s +| 3744: 65 67 64 69 72 5f 31 74 31 5f 73 65 67 64 69 72 egdir_1t1_segdir +| 3760: 06 0f c7 00 08 00 00 00 00 66 04 07 17 23 23 01 .........f...##. +| 3776: 81 13 74 61 62 6c 65 74 31 5f 73 65 67 6d 65 6e ..tablet1_segmen +| 3792: 74 73 74 31 5f 73 65 67 6d 65 6e 74 73 04 43 52 tst1_segments.CR +| 3808: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 73 EATE TABLE 't1_s +| 3824: 65 67 6d 65 6e 74 73 27 28 62 6c 6f 63 6b 69 64 egments'(blockid +| 3840: 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 INTEGER PRIMARY +| 3856: 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 KEY, block BLOB +| 3872: 29 6a 03 07 17 21 21 01 81 1f 74 61 62 6c 65 74 )j...!!...tablet +| 3888: 31 5f 63 6f 6e 74 65 6e 74 74 31 5f 63 6f 6e 74 1_contentt1_cont +| 3904: 65 6e 74 03 43 52 45 41 54 45 20 54 41 42 4c 45 ent.CREATE TABLE +| 3920: 20 27 74 31 5f 63 6f 6e 74 65 6e 74 27 28 64 6f 't1_content'(do +| 3936: 63 69 64 20 49 4e 54 45 47 45 52 20 50 52 39 4d cid INTEGER PR9M +| 3952: 41 52 59 20 4b 45 59 2c 20 27 63 30 61 27 2c 20 ARY KEY, 'c0a', +| 3968: 27 63 31 62 27 2c 20 27 63 32 63 27 29 38 12 06 'c1b', 'c2c')8.. +| 3984: 17 11 11 08 5f 74 61 6b 3c 65 74 31 74 31 43 52 ...._tak<et1t1CR +| 4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 33 LE t1 USING fts3 +| 4032: 28 61 2c 62 2c 63 29 00 00 00 00 00 00 00 00 00 (a,b,c)......... +| page 3 offset 8192 +| 0: 0d 00 00 00 25 0b 48 00 0f d8 0f af 0f 86 0f 74 ....%.H........t +| 16: 0f 61 0f 4e 0f 2f 0f 0f 0e ef 0e d7 0e be 0e a5 .a.N./.......... +| 32: 0e 8d 0e 74 0e 5b 0e 40 0e 24 0e 08 0d ef 0d d5 ...t.[.@.$...... +| 48: 0d bb 0d a0 0d 84 03 28 0d 4f 0d 35 0d 1b 0c fb .......(.O.5.... +| 64: 0c da 0c b9 0c 99 0c 78 0c 57 0c 3e 0c 24 0c 0a .......x.W.>.$.. +| 80: 0b 48 00 00 00 00 00 00 00 00 00 00 00 00 00 00 .H.............. +| 2880: 00 00 00 00 00 00 00 00 81 3f 25 06 00 72 7f 00 .........?%..r.. +| 2896: 00 43 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2e .COMPILER=gcc-5. +| 2912: 34 2e 30 20 32 30 31 36 30 36 30 39 21 44 45 42 4.0 20160609!DEB +| 2928: 55 47 20 45 4e 41 42 4c 45 20 44 42 53 54 41 54 UG ENABLE DBSTAT +| 2944: 20 56 54 41 42 20 45 4e 41 42 4c 46 20 46 54 53 VTAB ENABLF FTS +| 2960: 34 20 45 4e 41 42 4c 45 20 46 54 53 35 20 45 4e 4 ENABLE FTS5 EN +| 2976: 41 42 4c 45 20 47 45 4f 50 4f 4c 59 20 45 4e 41 ABLE GEOPOLY ENA +| 2992: 42 4c 55 20 4a 53 4f 4e 31 20 45 4e 41 42 4c 45 BLU JSON1 ENABLE +| 3008: 20 4d 45 4d 53 59 53 35 20 45 4e 41 42 4c 45 20 MEMSYS5 ENABLE +| 3024: 52 54 52 45 45 56 4d 41 58 20 4d 45 4d 4f 52 59 RTREEVMAX MEMORY +| 3040: 3d 35 30 30 30 30 30 30 30 20 4f 4d 49 54 20 4c =50000000 OMIT L +| 3056: 4f 42 43 20 45 58 54 45 4e 53 49 4f 4e 20 54 48 OBC EXTENSION TH +| 3072: 52 45 41 44 53 41 46 45 3d 40 18 24 05 00 25 0f READSAFE=@.$..%. +| 3088: 19 54 48 52 45 41 44 53 41 46 45 3d 30 58 42 49 .THREADSAFE=0XBI +| 3104: 4e 41 52 59 18 23 05 00 25 0f 19 54 48 52 45 41 NARY.#..%..THREA +| 3120: 44 53 41 46 45 3d 31 58 4e 4f 43 41 53 45 17 22 DSAFE=1XNOCASE.. +| 3136: 05 00 25 0f 17 54 48 52 45 41 44 43 41 46 45 3d ..%..THREADCAFE= +| 3152: 30 58 52 54 52 49 4d 1f 21 05 00 33 0f 19 4f 4d 0XRTRIM.!..3..OM +| 3168: 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 4f IT LOAD EXTENSIO +| 3184: 4e 58 42 49 4e 41 52 59 1f 20 05 00 33 0f 19 4f NXBINARY. ..3..O +| 3200: 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 48 MIT LOAD EXTENSH +| 3216: cf 4e 58 4e 4f 43 41 53 45 1e 1f 05 00 33 0f 17 .NXNOCASE....3.. +| 3232: 4f 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 OMIT LOAD EXTENS +| 3248: 49 4f 4e 58 52 54 52 49 4d 1f 1e 05 00 33 0f 19 IONXRTRIM....3.. +| 3264: 4d 41 58 20 4d 45 4d 4f 52 59 2d 35 30 30 30 30 MAX MEMORY-50000 +| 3280: 30 30 30 58 42 49 4e 41 52 59 1f 1d 05 00 33 0f 000XBINARY....3. +| 3296: 19 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 .MAX MEMORY=5000 +| 3312: 30 30 30 30 58 4e 4f 43 41 53 45 1e 1c 05 00 33 0000XNOCASE....3 +| 3328: 0f 17 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 ..MAX MEMORY=500 +| 3344: 30 30 30 30 30 58 52 54 52 49 4d 18 1b 05 00 25 00000XRTRIM....% +| 3360: 0f 19 45 4e 41 42 4c 45 20 52 54 52 45 45 58 42 ..ENABLE RTREEXB +| 3376: 49 4e 41 52 49 18 1a 05 0d a5 0f 19 45 4e 41 42 INARI.......ENAB +| 3392: 4c 45 20 52 54 52 45 45 58 4e 4f be 31 53 45 17 LE RTREEXNO.1SE. +| 3408: 19 05 00 25 0f 17 45 4e 41 42 4c 45 20 52 54 51 ...%..ENABLE RTQ +| 3424: 45 45 58 52 54 52 49 4d 1a 18 05 00 29 0f 19 45 EEXRTRIM....)..E +| 3440: 4e 41 42 4c 45 20 4d 45 4d 53 59 53 35 58 42 49 NABLE MEMSYS5XBI +| 3456: 4e 41 52 59 1a 17 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3472: 45 20 4d 45 4d 53 59 53 35 58 4e 4f 43 41 53 45 E MEMSYS5XNOCASE +| 3488: 19 16 05 00 29 0f 17 45 4e 41 42 4c 45 20 4d 45 ....)..ENABLE ME +| 3504: 4d 53 59 53 37 f8 52 54 52 49 4d 18 14 05 00 25 MSYS7.RTRIM....% +| 3520: 0f 19 45 4e 41 42 4c 45 20 4a 53 4f 4e 31 58 42 ..ENABLE JSON1XB +| 3536: 49 4e 41 52 59 18 14 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3552: 4c 45 20 4a 53 4f 3e 31 58 4e 4f 43 41 53 45 17 LE JSO>1XNOCASE. +| 3568: 13 05 00 25 0f 17 45 4e 41 42 4c 45 20 4a 53 4f ...%..ENABLE JSO +| 3584: 4e 31 58 52 54 52 49 4d 1a 12 05 00 29 0f 19 45 N1XRTRIM....)..E +| 3600: 4e 41 42 4c 45 20 47 45 4f 50 4f 4c 59 58 42 49 NABLE GEOPOLYXBI +| 3616: 4e 41 52 59 1a 11 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3632: 48 c0 47 45 4f 50 4f 4c 40 58 4e 4f 43 41 53 45 H.GEOPOL@XNOCASE +| 3648: 19 10 05 00 29 0f 17 45 4e 41 42 4c 45 20 47 45 ....)..ENABLE GE +| 3664: 4f 50 4f 4c 59 58 52 54 51 49 4d 17 0f 05 00 23 OPOLYXRTQIM....# +| 3680: 0f 19 45 4e 41 42 4c 45 20 46 54 53 35 58 42 49 ..ENABLE FTS5XBI +| 3696: 4e 41 52 59 17 0e 05 00 23 0f 19 45 4e 41 42 4c NARY....#..ENABL +| 3712: 45 20 46 54 53 35 58 4e 4f 43 41 53 45 16 0d 05 E FTS5XNOCASE... +| 3728: 00 23 0f 17 45 4e 41 42 4c 45 20 46 54 53 35 58 .#..ENABLE FTS5X +| 3744: 52 54 52 49 4d 17 0c 05 00 23 0f 19 45 4e 41 42 RTRIM....#..ENAB +| 3760: 4c 45 20 46 54 53 34 58 42 49 4d 41 52 59 17 0b LE FTS4XBIMARY.. +| 3776: 05 00 23 0f 19 45 4e 31 42 4c 45 20 46 1a 53 34 ..#..EN1BLE F.S4 +| 3792: 58 4e 4f 43 41 53 45 16 0a 05 00 23 0f 17 45 4e XNOCASE....#..EN +| 3808: 41 42 4c 45 20 46 54 53 34 58 52 54 52 49 4d 1e ABLE FTS4XRTRIM. +| 3824: 09 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3840: 54 41 54 20 56 54 41 42 58 42 49 4e 41 52 59 1e TAT VTABXBINARY. +| 3856: 08 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3872: 54 41 54 20 56 54 41 42 58 4e 4f 43 41 53 45 1d TAT VTABXNOCASE. +| 3888: 07 05 00 31 0f 17 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3904: 54 96 54 20 56 54 41 42 58 52 54 52 49 4d 11 06 T.T VTABXRTRIM.. +| 3920: 05 00 17 0f 1e e4 45 42 55 47 58 42 49 4e 41 52 ......EBUGXBINAR +| 3936: 59 11 05 05 00 17 0e 19 44 45 42 55 47 58 4e 4f Y.......DEBUGXNO +| 3952: 43 41 53 45 10 04 05 00 17 0f 17 44 45 42 55 47 CASE.......DEBUG +| 3968: 58 52 54 52 49 4d 27 03 05 01 43 0f 19 43 4f 4d XRTRIM'...C..COM +| 3984: 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e 30 20 PILER=gcc-5.4.0 +| 4000: 32 30 31 36 30 36 30 39 58 42 49 4e 41 52 59 27 20160609XBINARY' +| 4016: 02 05 00 43 0f 19 43 4f 4d 50 49 4c 45 52 3d 67 ...C..COMPILER=g +| 4032: 63 63 2d 35 2e 34 2e 30 40 32 30 31 36 30 36 30 cc-5.4.0@2016060 +| 4048: 39 58 4e 4f 43 41 53 45 26 01 05 00 43 0f 17 43 9XNOCASE&...C..C +| 4064: 4f 4d 4f 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e OMOILER=gcc-5.4. +| 4080: 30 20 32 30 31 36 30 36 30 39 58 52 54 52 49 4d 0 20160609XRTRIM +| page 4 offset 12288 +| 0: 0d 00 00 01 00 10 00 00 00 00 00 00 00 00 00 00 ................ +| page 5 offset 16384 +| 0: 0d 00 00 00 02 0b a0 00 0c ad 0b a0 00 00 00 00 ................ +| 2976: 82 0a 02 08 08 09 08 08 17 84 06 30 20 32 35 33 ...........0 253 +| 2992: 00 01 30 04 25 06 1b 00 00 08 32 30 31 36 30 36 ..0.%.....201606 +| 3008: 30 39 03 25 07 00 00 01 34 03 25 05 00 00 01 35 09.%....4.%....5 +| 3024: 03 25 04 00 01 07 30 30 30 30 30 30 30 03 25 1a .%....0000000.%. +| 3040: 00 00 08 63 6f 6d 70 69 6c 65 72 03 25 02 00 00 ...compiler.%... +| 3056: 06 64 62 73 74 61 74 03 25 0a 00 01 04 65 62 75 .dbstat.%....ebu +| 3072: 67 03 25 08 00 00 06 65 6e 61 62 6c 65 09 25 09 g.%....enable.%. +| 3088: 05 04 04 04 04 04 00 01 08 78 74 65 6e 73 69 6f .........xtensio +| 3104: 6e 03 25 1d 00 00 04 66 74 73 34 03 25 0d 00 03 n.%....fts4.%... +| 3120: 01 35 03 25 0f 00 00 03 67 63 63 03 25 03 00 01 .5.%....gcc.%... +| 3136: 06 65 6f 70 6f 6c 79 03 25 11 00 00 05 6a 73 6f .eopoly.%....jso +| 3152: 6e 31 03 25 13 00 00 04 6c 6f 61 64 03 25 1c 00 n1.%....load.%.. +| 3168: 00 03 6d 61 78 03 25 18 00 01 05 65 6d 6f 72 79 ..max.%....emory +| 3184: 03 25 19 00 03 04 73 79 73 4d 03 25 15 00 00 04 .%....sysM.%.... +| 3200: 6e 6d 69 74 03 25 1b 00 00 05 72 74 72 65 65 03 nmit.%....rtree. +| 3216: 25 17 00 00 0a 74 68 72 65 61 64 73 61 66 65 03 %....threadsafe. +| 3232: 25 0e 00 00 04 76 74 61 62 03 25 0b 00 86 50 01 %....vtab.%...P. +| 3248: 08 08 08 08 08 17 8d 12 30 20 38 33 35 00 01 30 ........0 835..0 +| 3264: 12 01 06 00 01 06 00 01 06 00 1f 03 00 01 03 00 ................ +| 3280: 01 03 00 00 08 32 30 31 36 30 36 30 39 09 01 bd .....20160609... +| 3296: 00 01 07 00 01 07 00 00 01 34 09 01 05 00 01 05 .........4...... +| 3312: 00 01 06 00 00 01 35 09 01 04 00 01 04 00 02 04 ......5......... +| 3328: 00 01 07 30 30 e6 30 30 30 30 09 1c 04 00 01 04 ...00.0000...... +| 3344: 00 01 04 00 00 06 62 69 6e 61 72 79 3c 03 01 02 ......binary<... +| 3360: 02 00 03 01 02 02 00 04 01 02 02 10 03 01 02 02 ................ +| 3376: 00 0f 71 02 12 00 03 01 02 02 00 03 01 65 02 00 ..q..........e.. +| 3392: 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03 ................ +| 3408: 01 0d a2 00 03 01 02 02 00 00 08 63 3b 6d 70 69 ...........c;mpi +| 3424: 6c 65 72 09 01 02 00 01 02 00 01 02 00 00 06 64 ler............d +| 3440: 62 73 74 61 74 09 07 03 00 01 03 00 01 03 00 01 bstat........... +| 3456: 04 65 62 75 67 09 04 02 00 01 02 00 01 02 00 00 .ebug........... +| 3472: 06 65 6e 61 62 6c 65 3f 07 02 00 01 02 00 01 02 .enable?........ +| 3488: 00 01 02 00 01 02 00 01 01 f0 01 02 00 01 02 00 ................ +| 3504: 01 02 00 01 02 00 01 02 00 01 02 00 01 02 00 01 ................ +| 3520: 02 00 01 02 00 01 02 00 01 02 00 01 02 00 01 02 ................ +| 3536: 00 01 02 00 01 02 00 01 08 78 74 65 6e 73 69 6f .........xtensio +| 3552: 6e 09 1f 04 00 01 04 00 01 04 00 00 04 66 74 73 n............fts +| 3568: 34 09 0a 03 00 01 03 00 01 03 00 03 01 35 09 0d 4............5.. +| 3584: 03 00 01 03 00 01 03 00 00 03 67 63 63 09 01 03 ..........gcc... +| 3600: 00 01 03 00 01 03 00 01 06 65 6f 70 6f 6c 79 09 .........eopoly. +| 3616: 10 03 00 01 03 00 01 03 00 00 05 6a 73 6f 6e 31 ...........json1 +| 3632: 09 13 03 00 01 03 00 01 03 00 00 04 6c 6f 61 64 ............load +| 3648: 09 1f 03 00 01 03 00 01 03 00 00 03 6d 61 78 09 ............max. +| 3664: 1c 02 00 01 02 00 01 02 00 01 05 65 6d 6f 72 79 ...........emory +| 3680: 09 1c 03 00 01 03 00 01 03 00 03 04 73 79 73 35 ............sys5 +| 3696: 09 16 03 00 01 03 00 01 03 00 00 06 6e 6f 63 61 ............noca +| 3712: 73 65 3c 02 01 02 02 00 03 01 02 02 00 03 01 02 se<............. +| 3728: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3744: 00 03 01 02 02 00 03 01 02 02 00 03 01 02 01 f0 ................ +| 3760: 03 01 02 02 05 93 01 02 02 00 03 01 02 02 00 00 ................ +| 3776: 04 6f 6d 69 74 09 1f 02 00 01 02 00 01 02 00 00 .omit........... +| 3792: 05 72 8a 72 65 65 09 19 03 00 01 03 00 11 03 00 .r.ree.......... +| 3808: 03 02 69 6d 3c 01 01 02 02 00 03 01 02 02 00 03 ..im<........... +| 3824: 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01 ................ +| 3840: 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 ................ +| 3856: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3872: 00 00 0a 74 68 72 65 61 64 73 61 66 65 09 22 02 ...threadsafe... +| 3888: 00 01 02 00 01 02 00 00 04 76 75 61 62 09 07 04 .........vuab... +| 3904: 00 01 04 00 01 04 00 00 61 78 b4 01 01 01 01 02 ........ax...... +| 3920: 00 01 01 01 02 00 00 f1 01 02 00 01 01 01 02 00 ................ +| 3936: 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 ................ +| 3952: 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 ................ +| 3968: 01 02 00 01 01 01 01 ff 01 01 01 02 00 01 01 01 ................ +| 3984: 02 00 01 01 01 02 00 01 01 01 02 09 01 01 01 02 ................ +| 4000: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| 4016: 01 01 01 02 00 01 02 01 02 00 01 01 01 02 00 01 ................ +| 4032: 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 ................ +| 4048: 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 ................ +| 4064: 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 ................ +| 4080: 00 01 01 11 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 02 0f f5 00 0f fb 1f f5 00 00 00 00 ................ +| 4080: 00 00 00 00 00 05 04 08 09 01 02 04 04 08 08 09 ................ +| page 7 offset 24576 +| 0: 0d 00 00 00 05 0f b8 00 0f f4 0f e9 10 d6 0f c7 ................ +| 4016: 00 00 00 00 00 00 00 00 0d 05 02 23 61 75 74 6f ...........#auto +| 4032: 6d 65 72 67 65 3d 35 0d 04 02 23 6d 65 72 67 65 merge=5...#merge +| 4048: 3d 31 00 00 00 00 00 00 00 00 00 00 00 00 00 00 =1.............. +| end crash-53f41622dd3bf6.db +}]} {} + +do_catchsql_test 29.1 { + PRAGMA writable_schema = 1; + INSERT INTO t1(a) SELECT X'819192E578DE3F'; + UPDATE t1 SET b=quote(zeroblob(current_date)) WHERE t1 MATCH 't*'; + INSERT INTO t1(b) VALUES(x'78'); + INSERT INTO t1(t1) SELECT x FROM t2; +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +# +reset_db +do_test 30.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 28672 pagesize 4096 filename crash-e6e3857edf9b26.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 00 .....@ ........ +| 96: 00 00 00 00 0d 0e b1 00 06 0d a4 00 0f 8d 0f 21 ...............! +| 112: 0e b9 0d c8 0e 7e 0d a4 00 00 00 00 00 00 00 00 .....~.......... +| 3488: 00 00 00 00 22 07 06 17 11 11 01 31 74 61 62 6c ...........1tabl +| 3504: 65 74 32 74 32 07 43 52 45 41 54 45 20 54 41 42 et2t2.CREATE TAB +| 3520: 4c 45 20 74 32 28 78 29 81 33 05 07 17 1f 1f 01 LE t2(x).3...... +| 3536: 82 35 74 61 62 6c 65 74 31 5f 73 65 67 64 69 72 .5tablet1_segdir +| 3552: 74 31 5f 73 65 67 64 69 72 05 43 52 45 41 54 45 t1_segdir.CREATE +| 3568: 20 54 41 42 4c 45 20 27 74 31 5f 73 65 67 64 69 TABLE 't1_segdi +| 3584: 72 27 28 6c 65 76 65 6c 20 49 4e 54 45 47 45 52 r'(level INTEGER +| 3600: 2c 69 64 78 20 49 4e 54 45 47 45 52 2c 73 74 61 ,idx INTEGER,sta +| 3616: 72 74 5f 62 6c 6f 63 6b 20 49 4e 54 45 47 45 52 rt_block INTEGER +| 3632: 2c 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 ,leaves_end_bloc +| 3648: 6b 20 49 4e 54 45 47 45 52 2c 65 6e 64 5f 62 6c k INTEGER,end_bl +| 3664: 6f 63 6b 20 49 4e 54 45 47 45 62 2c 72 6f 6f 74 ock INTEGEb,root +| 3680: 20 42 4c 4f 42 2c 50 52 49 4d 41 52 59 20 4b 45 BLOB,PRIMARY KE +| 3696: 59 28 6c 65 76 65 6c 2c 20 69 64 78 29 29 31 06 Y(level, idx))1. +| 3712: 06 17 45 1f 01 00 69 6e 64 65 78 73 71 6c 69 74 ..E...indexsqlit +| 3728: 65 5f 61 75 74 6f 69 6e 64 65 78 5f 74 31 5f 73 e_autoindex_t1_s +| 3744: 65 67 64 69 72 5f 31 74 31 5f 73 65 67 64 69 72 egdir_1t1_segdir +| 3760: 06 0f c7 00 08 00 00 00 00 66 04 07 17 23 23 01 .........f...##. +| 3776: 81 13 74 61 62 6c 65 74 31 5f 73 65 67 6d 65 6e ..tablet1_segmen +| 3792: 74 73 74 31 5f 73 65 67 6d 65 6e 74 73 04 43 52 tst1_segments.CR +| 3808: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 73 EATE TABLE 't1_s +| 3824: 65 67 6d 65 6e 74 73 27 28 62 6c 6f 63 6b 69 64 egments'(blockid +| 3840: 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 INTEGER PRIMARY +| 3856: 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 KEY, block BLOB +| 3872: 29 6a 03 07 17 21 21 01 81 1f 74 61 62 6c 65 74 )j...!!...tablet +| 3888: 31 5f 63 6f 6e 74 65 6e 74 74 31 5f 63 6f 6e 74 1_contentt1_cont +| 3904: 65 6e 74 03 43 52 45 41 54 45 20 54 41 42 4c 45 ent.CREATE TABLE +| 3920: 20 27 74 31 5f 63 6f 6e 74 65 6e 74 27 28 64 6f 't1_content'(do +| 3936: 63 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d cid INTEGER PRIM +| 3952: 41 52 59 20 4b 45 59 2c 20 27 63 30 61 27 2c 20 ARY KEY, 'c0a', +| 3968: 27 63 31 62 27 2c 20 27 63 32 63 27 29 38 02 06 'c1b', 'c2c')8.. +| 3984: 17 11 11 08 5f 74 61 62 6c 65 74 31 74 31 43 52 ...._tablet1t1CR +| 4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 33 LE t1 USING fts3 +| 4032: 28 61 2c 62 2c 63 29 00 00 00 00 00 00 00 00 00 (a,b,c)......... +| page 3 offset 8192 +| 0: 0d 00 00 00 25 0b 48 00 0f d8 0f af 0f 86 0f 74 ....%.H........t +| 16: 0f 61 0f 4e 0f 2f 0f 0f 0e ef 0e d7 0e be 0e a5 .a.N./.......... +| 32: 0e 8d 0e 74 0e 5b 0e 40 0e 24 0e 08 0d ef 00 00 ...t.[.@.$...... +| 2880: 00 00 00 00 00 00 00 00 81 3f 25 06 00 82 7e f0 .........?%...~. +| 2896: 00 43 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2e .COMPILER=gcc-5. +| 2912: 34 23 00 20 32 30 31 36 30 36 30 39 20 44 45 42 4#. 20160609 DEB +| 2928: 55 47 20 45 4e 41 42 4c 45 20 44 42 53 54 41 54 UG ENABLE DBSTAT +| 2944: 20 56 54 41 42 20 45 4e 42 92 4c 45 20 46 54 53 VTAB ENB.LE FTS +| 2960: 34 20 45 4e 41 42 4c 45 20 46 54 53 35 20 45 4e 4 ENABLE FTS5 EN +| 2976: 41 42 4c 45 20 47 45 4f 50 4f 4c 59 20 45 4e 41 ABLE GEOPOLY ENA +| 2992: 42 4c 45 1f 4a 53 4f 4e 31 20 45 4e 41 42 4c 49 BLE.JSON1 ENABLI +| 3008: 00 4d 45 4d 53 59 53 35 20 45 4e 41 42 4c 45 20 .MEMSYS5 ENABLE +| 3024: 52 54 52 45 45 20 4d 41 58 20 4d 45 4d 4f 52 59 RTREE MAX MEMORY +| 3040: 3d 35 30 30 30 30 30 30 30 20 4f 4d 49 54 20 4c =50000000 OMIT L +| 3056: 4f 41 44 20 45 58 54 45 4e 53 49 4f 4e 20 54 48 OAD EXTENSION TH +| 3072: 52 45 41 44 53 41 46 45 3d 30 18 24 05 00 25 0f READSAFE=0.$..%. +| 3088: 19 54 48 52 45 41 44 53 41 46 45 3d 30 58 42 49 .THREADSAFE=0XBI +| 3104: 4e 41 52 59 18 23 05 00 25 0f 19 54 48 52 45 41 NARY.#..%..THREA +| 3120: 44 53 41 46 45 3d 30 88 4e 4f 43 41 53 45 17 22 DSAFE=0.NOCASE.. +| 3136: 05 00 25 0f 17 54 48 52 45 41 44 53 41 46 45 3d ..%..THREADSAFE= +| 3152: 30 58 52 54 52 49 4d 1f 21 05 00 33 0f 19 4f 4d 0XRTRIM.!..3..OM +| 3168: 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 4f IT LOAD EXTENSIO +| 3184: 4e 58 42 49 4e 41 52 59 1f 20 05 00 33 0f 19 4f NXBINARY. ..3..O +| 3200: 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 MIT LOAD EXTENSI +| 3216: 4f 4e 58 4e 4f 43 41 53 45 1e 20 05 00 33 0f 17 ONXNOCASE. ..3.. +| 3232: 4f 4d 49 54 20 4c 4f 41 54 20 45 58 54 45 4e 53 OMIT LOAT EXTENS +| 3248: 49 4f 4e 58 52 54 52 49 4d 1f 1e 04 00 33 0f 19 IONXRTRIM....3.. +| 3264: 82 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 30 .AX MEMORY=50000 +| 3280: 30 30 30 58 42 49 4e 41 52 59 1f 1d 05 00 33 0f 000XBINARY....3. +| 3296: 19 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 .MAX MEMORY=5000 +| 3312: 30 30 30 30 58 4e 4f 43 41 53 45 1e 1c 05 00 33 0000XNOCASE....3 +| 3328: 0f 17 4d 41 58 20 4d 45 4d fa 52 59 3d 35 30 20 ..MAX MEM.RY=50 +| 3344: 30 30 30 30 30 58 52 54 52 49 4d 18 1b 05 00 25 00000XRTRIM....% +| 3360: 0f 19 45 4e 41 42 4c 45 20 52 53 52 45 45 58 42 ..ENABLE RSREEXB +| 3376: 49 4e 41 52 59 18 1a 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3392: 4c 45 20 52 54 52 45 45 58 4e 4f 53 41 53 45 17 LE RTREEXNOSASE. +| 3408: 19 05 00 25 0f 17 45 4e 42 42 4c 45 20 52 54 52 ...%..ENBBLE RTR +| 3424: 45 45 58 52 54 52 49 4d 1a 18 05 00 29 0f 19 45 EEXRTRIM....)..E +| 3440: 4e 41 42 4c 45 20 4d 45 4d 53 5a 53 35 58 42 49 NABLE MEMSZS5XBI +| 3456: 4e 41 52 59 1a 17 05 00 29 0f 19 45 4e 41 42 3c NARY....)..ENAB< +| 3472: 45 20 4d 45 4d 53 59 53 35 58 4e 4f 43 41 53 45 E MEMSYS5XNOCASE +| 3488: 19 16 05 00 29 0f 17 45 4e 41 42 4c 45 20 4d 45 ....)..ENABLE ME +| 3504: 4d 53 59 53 35 58 52 54 52 49 4d 18 15 05 00 25 MSYS5XRTRIM....% +| 3520: 0f 19 45 4e 41 42 4c 45 20 4a 53 4f 4e 31 58 42 ..ENABLE JSON1XB +| 3536: 49 4e 41 52 59 18 14 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3552: 4c 45 20 4a 53 4f 4e 31 58 4e 4f 43 41 53 45 17 LE JSON1XNOCASE. +| 3568: 13 05 00 25 0f 17 45 4e 41 42 4c 45 20 4a 53 4f ...%..ENABLE JSO +| 3584: 4e 31 58 52 54 52 49 4d 1a 12 05 00 29 0f 19 45 N1XRTRIM....)..E +| 3600: 4e 31 42 4c 45 20 47 45 4e 50 4f 4c 59 58 42 49 N1BLE GENPOLYXBI +| 3616: 4e 41 52 59 1a 11 05 00 29 0f 19 45 4e f2 1e 4c NARY....)..EN..L +| 3632: 45 20 47 45 4f 50 4f 4c 59 58 4e 4f 43 41 53 45 E GEOPOLYXNOCASE +| 3648: 19 10 05 00 29 0f 17 45 4e 41 42 4c 45 20 47 45 ....)..ENABLE GE +| 3664: 4f 50 4f 4c 59 58 52 54 52 49 4d 17 0f 05 00 23 OPOLYXRTRIM....# +| 3680: 0f 19 45 4e 41 42 4c 45 20 46 54 53 35 58 42 49 ..ENABLE FTS5XBI +| 3696: 4e 41 52 59 17 0e 05 00 23 0f 19 45 4e 41 42 3c NARY....#..ENAB< +| 3712: 45 20 46 54 53 35 58 4e 4f 43 41 53 45 16 0d 05 E FTS5XNOCASE... +| 3728: 00 23 0f 17 45 4e 41 42 4c 45 20 46 54 53 35 58 .#..ENABLE FTS5X +| 3744: 52 54 52 49 4d 17 0c 05 00 23 0f 19 45 4e 41 42 RTRIM....#..ENAB +| 3760: 4c 45 20 46 54 53 34 58 42 49 4e 41 52 59 17 0b LE FTS4XBINARY.. +| 3776: 05 00 23 0f 19 45 4e 41 43 4c 45 20 46 54 53 35 ..#..ENACLE FTS5 +| 3792: 58 4e 4f 43 40 53 45 16 0a 05 00 23 0f 17 45 4e XNOC@SE....#..EN +| 3808: 41 42 4c 45 20 46 54 53 34 58 52 54 52 49 4d 1e ABLE FTS4XRTRIM. +| 3824: 09 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3840: 54 41 55 20 56 54 41 42 58 42 49 4e 41 52 59 1e TAU VTABXBINARY. +| 3856: 08 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3872: 54 41 54 20 56 54 41 42 58 4e 4f 43 41 53 45 1d TAT VTABXNOCASE. +| 3888: 07 05 00 31 0f 17 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3904: 54 41 54 20 56 54 41 42 58 52 54 62 49 4d 11 06 TAT VTABXRTbIM.. +| 3920: 05 00 17 0f 19 44 45 42 54 47 58 42 49 4e 41 52 .....DEBTGXBINAR +| 3936: 59 11 05 05 00 17 0f 19 54 45 42 55 47 58 4e 4f Y.......TEBUGXNO +| 3952: 43 41 53 45 10 04 05 00 17 0f 17 44 45 42 55 47 CASE.......DEBUG +| 3968: 68 52 54 52 49 4d 27 03 05 00 43 0f 19 43 4f 4d hRTRIM'...C..COM +| 3984: 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e 30 20 PILER=gcc-5.4.0 +| 4000: 32 30 31 36 30 36 30 39 58 42 49 4e 41 52 59 27 20160609XBINARY' +| 4016: 02 05 00 43 0f 19 43 4f 4d 50 49 4c 45 52 3d 67 ...C..COMPILER=g +| 4032: 63 63 2d 35 2e 34 2e 30 20 32 30 31 36 30 36 30 cc-5.4.0 2016060 +| 4048: 39 58 4f 4f 43 41 53 45 26 01 05 00 43 0f 17 43 9XOOCASE&...C..C +| 4064: 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e OMPILER=gcc-5.4. +| 4080: 30 20 32 30 31 36 30 36 30 39 58 52 54 52 49 4d 0 20160609XRTRIM +| page 4 offset 12288 +| 0: 0d 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| page 5 offset 16384 +| 0: 0d 00 00 00 02 0b a0 00 0c ad 0b a0 00 00 00 00 ................ +| 2976: 82 0a 02 08 08 09 08 08 17 84 06 30 20 32 35 33 ...........0 253 +| 2992: 00 01 30 04 25 06 1b 00 00 08 32 30 31 36 30 36 ..0.%.....201606 +| 3008: 30 39 03 25 07 00 00 01 34 03 25 05 00 00 01 35 09.%....4.%....5 +| 3024: 03 25 04 00 01 07 30 30 30 30 30 30 30 03 25 1a .%....0000000.%. +| 3040: 00 00 08 63 6f 6d 70 69 6c 65 72 03 25 02 00 00 ...compiler.%... +| 3056: 06 64 62 73 74 61 74 03 25 0a 00 01 04 65 62 75 .dbstat.%....ebu +| 3072: 67 03 25 08 00 00 06 65 6e 61 62 6c 65 09 25 09 g.%....enable.%. +| 3088: 05 04 04 04 04 04 00 01 08 78 74 65 6e 73 69 6f .........xtensio +| 3104: 6e 03 25 1d 00 00 04 66 74 73 34 03 25 0d 00 03 n.%....fts4.%... +| 3120: 01 35 03 25 0f 00 00 03 67 63 63 03 25 03 00 01 .5.%....gcc.%... +| 3136: 06 65 6f 70 6f 6c 79 03 25 11 00 00 05 6a 73 6f .eopoly.%....jso +| 3152: 6e 31 03 25 14 00 e8 04 6c 6f 61 64 03 25 1c 00 n1.%....load.%.. +| 3168: 00 03 6d 61 78 03 25 18 00 01 05 65 6d 6f 72 79 ..max.%....emory +| 3184: 03 25 19 00 03 04 73 79 73 35 03 25 15 00 00 04 .%....sys5.%.... +| 3200: 6f 6d 69 74 03 25 1b 00 00 05 72 74 72 65 65 03 omit.%....rtree. +| 3216: 25 17 00 00 0a 74 68 72 65 61 64 73 61 66 65 03 %....threadsafe. +| 3232: 25 1e 00 00 04 76 74 61 62 03 25 0b 00 86 50 01 %....vtab.%...P. +| 3248: 08 08 08 08 08 17 8d 12 30 20 38 33 35 00 01 30 ........0 835..0 +| 3264: 12 01 06 00 01 06 00 01 06 00 1f 03 00 01 03 00 ................ +| 3280: 01 03 00 00 08 32 30 31 36 30 36 30 39 09 01 07 .....20160609... +| 3296: 00 01 07 00 01 07 00 00 01 34 09 01 05 00 01 05 .........4...... +| 3312: 00 01 05 00 00 01 35 09 01 04 00 01 04 00 01 04 ......5......... +| 3328: 00 01 07 30 30 30 30 30 30 30 09 1c 04 00 01 04 ...0000000...... +| 3344: 00 01 04 00 00 06 62 69 6e 61 72 79 3c 03 01 02 ......binary<... +| 3360: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3376: 00 03 01 02 f2 00 03 01 02 02 00 03 01 02 02 00 ................ +| 3392: 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03 ................ +| 3408: 01 02 02 00 03 01 02 02 00 00 08 63 6f 6d 70 69 ...........compi +| 3424: 6c 65 72 09 01 02 00 01 02 00 01 02 00 00 06 64 ler............d +| 3440: 62 73 74 61 74 09 07 03 00 01 03 00 01 03 00 01 bstat........... +| 3456: 04 65 62 75 67 09 04 02 00 01 02 00 01 02 00 00 .ebug........... +| 3472: 06 65 6e 60 62 6c 65 3f 07 02 00 01 02 00 01 01 .en`ble?........ +| 3488: ff f1 b1 00 00 02 3f 01 01 f0 f1 02 00 57 02 00 ......?......W.. +| 3504: 01 02 00 01 02 00 01 02 00 01 02 00 01 02 10 01 ................ +| 3520: 02 00 01 02 00 01 02 00 01 02 01 01 02 00 01 02 ................ +| 3536: 00 01 02 00 00 f2 00 01 08 78 74 65 6e 73 69 6f .........xtensio +| 3552: 6e 09 1f 04 00 01 04 00 01 04 00 00 04 66 74 73 n............fts +| 3568: 34 09 0a 03 00 01 03 00 01 03 00 03 01 35 09 0d 4............5.. +| 3584: 03 00 01 03 00 01 03 00 00 03 67 63 63 09 01 03 ..........gcc... +| 3600: 00 01 03 00 01 03 00 01 06 65 6f 70 6f 6c 79 09 .........eopoly. +| 3616: 10 03 00 01 03 00 01 03 00 00 b3 6a 73 6f 6e 31 ...........json1 +| 3632: 09 13 03 00 01 03 00 01 03 00 00 04 6c 6f 61 64 ............load +| 3648: 09 1f 03 00 01 03 00 01 03 00 00 03 6d 61 78 09 ............max. +| 3664: 1c 02 00 01 02 00 01 02 00 01 05 65 6d 6f 72 79 ...........emory +| 3680: 09 1c 03 00 01 03 00 01 03 00 03 04 73 79 73 35 ............sys5 +| 3696: 09 16 03 00 01 03 00 01 03 cc 00 06 6e 6f 63 61 ............noca +| 3712: 73 65 3c 02 01 02 02 00 03 01 02 02 00 03 01 02 se<............. +| 3728: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3744: 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 ................ +| 3760: 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 00 ................ +| 3776: 04 6f 6d 69 74 09 1f 02 00 01 02 00 01 02 00 00 .omit........... +| 3792: 05 72 74 62 65 65 09 19 03 00 01 03 00 01 03 00 .rtbee.......... +| 3808: 03 02 69 6d 3c 01 01 02 02 00 03 01 02 02 00 03 ..im<........... +| 3824: 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01 ................ +| 3840: 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 ................ +| 3856: 02 00 03 01 02 02 00 03 01 02 01 00 03 01 02 02 ................ +| 3872: 00 00 0a 74 68 72 65 61 64 73 61 66 65 09 22 02 ...threadsafe... +| 3888: 00 01 02 00 02 02 00 00 04 76 74 61 62 09 07 04 .........vtab... +| 3904: 00 01 03 00 01 04 00 00 01 78 b4 01 01 01 01 02 .........x...... +| 3920: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| 3936: 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 ................ +| 3952: 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 ................ +| 3968: 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 ................ +| 3984: 02 01 01 01 01 02 00 01 01 01 02 00 01 01 01 02 ................ +| 4000: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| 4016: 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 ................ +| 4032: 01 01 02 00 01 01 01 da 00 01 01 01 02 00 01 01 ................ +| 4048: 01 02 00 01 01 01 01 ff ff 01 01 02 00 01 01 01 ................ +| 4064: 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 ................ +| 4080: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 02 0f f5 00 0f fb 0f f5 01 00 00 00 ................ +| 4080: 00 00 00 00 00 05 04 08 09 01 02 04 04 08 08 09 ................ +| page 7 offset 24576 +| 0: 01 6f 00 00 00 00 00 00 00 00 00 00 00 00 00 00 .o.............. +| end crash-e6e3857edf9b26.db +}]} {} + +do_execsql_test 30.1 { + UPDATE t1 SET b=a; +} + +do_catchsql_test 30.2 { + SELECT (matchinfo(null)) FROM t1 WHERE t1 MATCH 'ee*e*e*e*e*e*e*Re*e*e*e**' +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +# +reset_db +do_execsql_test 31.0 { +CREATE VIRTUAL TABLE t1 USING fts3(a,b,c); +INSERT INTO t1_segdir VALUES(0,0,0,0,'0 592',X'00016dcb048ce6fbd3b2d68bfebf0101020200808080808080808020010202008080808080808080100102020080808080808080800801020200808080808080808004010202008080808080808080020102020080808080808080800101020200808080808080804001020200808080808080802001020200808080808080801001020200808080808080800801020200808080808080800401020200808080808080800201020200808080808080800101020200808080808080400102020080808080808020010202008080808080801001020200808080808080080102020080808080808004010202008080808080800201020200808080808080010102020080808080804001020200808080808020010202008080808080100102020080808080800801020200808080808004010202008080808080020102020080808080800101020200808080804001020200808080802001020200808080801001020200808080800801020200808080800401020200808080800201020200808080800101020200808080400102020080808020010202008080801001020200808080080102020080808004010202008080800201020200808080010102020080804001020200808020010202008080100102020080800801020200808004010202008080020102020080800101020200804001020200802001020200801001020200800801020200800401020200800201020200800101020200400102020020010202001001020200080102020004010202000201020200010102020001010202008080808080808080800101020200'); +INSERT INTO t1_segdir VALUES(0,1,0,0,'0 18',X'00026d6d0d8ee6fbd3b2d68bfe7f01020200'); +} + +do_catchsql_test 31.1 { + SELECT (matchinfo(t1, c ) ) FROM t1 WHERE t1 MATCH 'M*M*M*M*'; +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +# +reset_db +do_test 32.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 24576 pagesize 4096 filename crash-74fdbc96edbc04.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 00 .....@ ........ +| 96: 00 00 00 00 0d 0e b1 00 06 0d a4 00 0f 8d 0f 21 ...............! +| 112: 0e b9 0d c8 0e 7e 0d a4 00 00 00 00 00 00 00 00 .....~.......... +| 3488: 00 00 00 00 22 07 06 17 11 11 01 31 74 61 62 6c ...........1tabl +| 3504: 65 74 32 74 32 07 43 52 45 41 54 45 20 54 41 42 et2t2.CREATE TAB +| 3520: 4c 45 20 74 32 28 78 29 81 33 05 07 17 1f 1f 01 LE t2(x).3...... +| 3536: 82 35 74 61 62 6c 65 74 31 5f 73 65 67 64 69 72 .5tablet1_segdir +| 3552: 74 31 5f 73 65 67 64 69 72 05 43 52 45 41 54 45 t1_segdir.CREATE +| 3568: 20 54 41 42 4c 45 20 27 74 31 5f 73 65 67 64 69 TABLE 't1_segdi +| 3584: 72 27 28 6c 65 76 65 6c 20 49 4e 54 45 47 45 52 r'(level INTEGER +| 3600: 2c 69 64 78 20 49 4e 54 45 47 44 52 2c 73 74 61 ,idx INTEGDR,sta +| 3616: 72 74 5f 62 6c 6f 63 6b 20 49 4e 54 45 47 45 52 rt_block INTEGER +| 3632: 2c 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 ,leaves_end_bloc +| 3648: 6b 20 49 4e 54 45 47 45 52 2c 65 6e 64 5f 62 6c k INTEGER,end_bl +| 3664: 6f 63 6b 20 49 4e 54 45 47 45 52 2c 72 6f 6f 74 ock INTEGER,root +| 3680: 20 42 4c 4f 42 2c 50 52 49 4d 41 52 59 20 4b 45 BLOB,PRIMARY KE +| 3696: 59 28 6c 65 76 65 6c 2c 20 69 64 78 29 29 31 06 Y(level, idx))1. +| 3712: 06 17 45 1f 01 00 6a 6e 64 65 78 73 71 6c 69 74 ..E...jndexsqlit +| 3728: 65 5f 61 75 74 6f 69 6e 64 65 78 5f 74 31 5f 73 e_autoindex_t1_s +| 3744: 65 67 64 69 72 5f 31 74 31 5f 73 65 67 64 69 72 egdir_1t1_segdir +| 3760: 06 0f c7 00 08 00 00 00 00 66 04 07 17 23 23 01 .........f...##. +| 3776: 81 13 74 61 62 6c 65 74 31 5f 73 65 67 6d 65 6e ..tablet1_segmen +| 3792: 74 73 74 31 5f 73 65 67 6d 65 6e 74 73 03 43 52 tst1_segments.CR +| 3808: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 73 EATE TABLE 't1_s +| 3824: 65 67 6d 65 6e 74 73 27 28 62 6c 6f 63 6b 69 64 egments'(blockid +| 3840: 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 5a INTEGER PRIMARZ +| 3856: 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 KEY, block BLOB +| 3872: 29 6a 03 07 17 21 21 01 81 1f 74 61 62 6c 65 74 )j...!!...tablet +| 3888: 31 5f 63 6f 6e 74 65 6e 74 74 31 5f 63 6f 6e 74 1_contentt1_cont +| 3904: 65 6e 74 03 43 52 45 41 54 45 20 54 41 42 4c 45 ent.CREATE TABLE +| 3920: 20 27 74 31 5f 63 6f 6e 74 65 6e 74 27 28 64 6f 't1_content'(do +| 3936: 63 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d cid INTEGER PRIM +| 3952: 41 52 59 20 4b 45 59 2c 20 27 63 30 61 27 2c 20 ARY KEY, 'c0a', +| 3968: 27 63 31 62 27 2c 20 27 63 32 63 27 29 38 02 06 'c1b', 'c2c')8.. +| 3984: 17 11 11 08 5f 74 61 62 6c 65 74 31 74 31 43 52 ...._tablet1t1CR +| 4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 33 LE t1 USING fts3 +| 4032: 28 61 2c 62 2c 63 29 00 00 00 00 00 00 00 00 00 (a,b,c)......... +| page 3 offset 8192 +| 0: 0d 00 00 00 25 0b 48 00 0f d8 0f af 0f 86 0f 74 ....%.H........t +| 16: 0f 61 0f 4e 0f 2f 0f 0f 0e ef 0e d7 0e be 0e a5 .a.N./.......... +| 32: 0e 8d 0e 74 0e 5b 0e 40 0e 24 0e 08 0d ef 0d d5 ...t.[.@.$...... +| 48: 0d bb 0d a0 0d 84 0d 68 0d 50 0d 35 0d 1b 0c fb .......h.P.5.... +| 64: 0c da 0c b8 fc 99 0c 78 0c 57 0c 3e 0c 24 0c 0a .......x.W.>.$.. +| 80: 0b 48 00 00 00 00 00 00 00 00 00 00 00 00 00 00 .H.............. +| 2880: 00 00 00 00 00 00 00 00 81 3f 25 06 00 82 7f 00 .........?%..... +| 2896: 00 43 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2e .COMPILER=gcc-5. +| 2912: 34 2e 30 20 32 30 31 36 30 36 30 39 20 44 45 42 4.0 20160609 DEB +| 2928: 55 47 20 45 4e 41 42 4c 45 20 44 42 53 54 7e 54 UG ENABLE DBST~T +| 2944: 20 56 54 41 42 20 45 4e 41 42 4c 45 20 46 54 53 VTAB ENABLE FTS +| 2960: 44 20 45 4e 41 42 4c 45 20 46 54 53 35 20 45 4e D ENABLE FTS5 EN +| 2976: 41 42 4c 45 20 47 45 4f 50 4f 4c 59 20 45 4e 41 ABLE GEOPOLY ENA +| 2992: 42 4c 45 20 4a 53 4f 4e 31 20 45 4e 41 42 4c 45 BLE JSON1 ENABLE +| 3008: 20 4d 45 4d 53 59 53 35 20 45 4e 41 42 4c 45 20 MEMSYS5 ENABLE +| 3024: 52 54 52 45 45 20 4d 41 58 20 4d 45 4d 4f 52 59 RTREE MAX MEMORY +| 3040: 3d 35 30 30 30 30 30 30 30 20 4f 4d 49 54 20 4c =50000000 OMIT L +| 3056: 4f 41 44 20 45 58 54 45 4e 53 49 4f 4e 20 54 48 OAD EXTENSION TH +| 3072: 52 45 41 44 53 41 46 45 3d 30 18 24 05 00 25 0f READSAFE=0.$..%. +| 3088: 19 54 48 52 45 41 44 54 41 46 45 3d 30 58 42 49 .THREADTAFE=0XBI +| 3104: 4e 41 52 59 18 23 05 00 25 0f 19 54 48 52 45 41 NARY.#..%..THREA +| 3120: 44 53 41 46 45 3d 30 bd 4e 4f 43 41 53 45 17 22 DSAFE=0.NOCASE.. +| 3136: 05 00 25 0f 17 54 48 52 45 41 44 53 41 46 45 3d ..%..THREADSAFE= +| 3152: 30 58 52 54 52 49 4d 1f 21 05 00 33 0f 19 4f 4d 0XRTRIM.!..3..OM +| 3168: 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 4e IT LOAD EXTENSIN +| 3184: 4e 58 42 49 4e 41 52 59 1f 20 05 00 33 0f 19 4f NXBINARY. ..3..O +| 3200: 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 MIT LOAD EXTENSI +| 3216: 4f 4e 58 4e 4f 43 41 53 45 1e 1f 05 00 33 0f 17 ONXNOCASE....3.. +| 3232: 4f 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 OMIT LOAD EXTENS +| 3248: 49 4f 4e 58 52 54 52 49 4d 1f 1e 05 00 33 0f 19 IONXRTRIM....3.. +| 3264: 4d 41 58 1f 4d 45 4d 4f 52 59 3d 35 30 30 30 30 MAX.MEMORY=50000 +| 3280: 30 30 30 58 42 49 4e 41 52 59 1f 1d 05 00 33 0f 000XBINARY....3. +| 3296: 19 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 .MAX MEMORY=5000 +| 3312: 30 30 30 30 58 4e 4f 43 41 53 45 1e 1c 05 00 33 0000XNOCASE....3 +| 3328: 0f 17 4d 41 58 20 4d 44 4d 4f 52 59 3d 35 30 30 ..MAX MDMORY=500 +| 3344: 30 30 30 30 30 58 52 54 52 49 4d 18 1b 05 00 25 00000XRTRIM....% +| 3360: 0f 19 45 4e 41 42 4c 45 20 52 54 52 45 45 58 42 ..ENABLE RTREEXB +| 3376: 49 4e 41 52 59 18 1a 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3392: 4c 55 20 52 54 52 45 45 58 4e 4f 43 41 53 45 17 LU RTREEXNOCASE. +| 3408: 19 05 00 25 0f 17 45 4e 41 42 4c 45 20 52 54 52 ...%..ENABLE RTR +| 3424: 45 45 58 52 54 52 49 4d 1a 18 05 00 29 0f 19 45 EEXRTRIM....)..E +| 3440: 4e 41 42 4c 45 20 4d 45 4d 53 59 53 35 58 42 49 NABLE MEMSYS5XBI +| 3456: 4e 41 52 59 1a 17 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3472: 45 20 4d 45 4d 53 59 53 35 58 4e 3f 43 41 53 45 E MEMSYS5XN?CASE +| 3488: 19 16 05 00 29 0f 17 45 4e a1 42 4c 45 20 4d 45 ....)..EN.BLE ME +| 3504: 4d 53 59 53 35 58 52 54 52 49 4d 18 15 05 00 25 MSYS5XRTRIM....% +| 3520: 0f 19 45 4e 41 42 4c 45 20 4a 53 4f 4e 31 58 42 ..ENABLE JSON1XB +| 3536: 49 4e 41 52 59 18 14 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3552: 4c 45 20 4a 53 4f 4e 31 58 4e 4f 43 41 53 45 17 LE JSON1XNOCASE. +| 3568: 13 05 00 25 0f 17 45 4e 41 42 4c 45 20 4a 53 4f ...%..ENABLE JSO +| 3584: 4e 31 58 52 54 52 49 4d 1a 12 05 00 29 0f 19 45 N1XRTRIM....)..E +| 3600: 4e 41 42 4c 45 20 47 45 4f 50 5f 4c 59 58 42 49 NABLE GEOP_LYXBI +| 3616: 4e 41 52 59 1a 11 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3632: 45 20 47 45 4f 50 4f 4c 59 58 4b bf 43 41 53 45 E GEOPOLYXK.CASE +| 3648: 19 10 05 00 29 0f 17 45 4e 41 42 4c 45 20 47 45 ....)..ENABLE GE +| 3664: 4f 50 4f 4c 59 58 52 54 52 49 4d 17 0f 05 00 23 OPOLYXRTRIM....# +| 3680: 0f 19 45 4e 41 42 4c 55 20 46 54 53 35 58 42 49 ..ENABLU FTS5XBI +| 3696: 4e 4b a2 59 17 0e 05 00 23 0f 19 45 4e 41 42 4c NK.Y....#..ENABL +| 3712: 45 20 46 54 52 35 58 4e 4f 43 41 53 45 16 0d 05 E FTR5XNOCASE... +| 3728: 00 23 0f 17 45 4e 41 42 4c 45 20 46 54 53 35 58 .#..ENABLE FTS5X +| 3744: 52 54 52 49 4d 17 0b 05 00 23 0f 19 45 4e 41 42 RTRIM....#..ENAB +| 3760: 4c 45 20 46 54 53 34 58 42 49 4e 41 52 59 17 0b LE FTS4XBINARY.. +| 3776: 05 00 23 0f 19 45 4e 41 42 4c 45 20 46 54 53 34 ..#..ENABLE FTS4 +| 3792: 58 4e 4f 43 41 53 45 16 0a 05 00 23 0f 17 45 4e XNOCASE....#..EN +| 3808: 41 42 4c 45 20 46 54 53 34 58 52 54 52 49 4d 1e ABLE FTS4XRTRIM. +| 3824: 09 05 07 e1 0f 19 45 4e 41 42 4c 45 20 44 42 53 ......ENABLE DBS +| 3840: 54 41 54 20 56 54 41 42 58 42 49 4e 41 52 59 1e TAT VTABXBINARY. +| 3856: 18 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3872: 54 41 54 20 56 54 41 41 18 4e 4f 43 41 53 45 1d TAT VTAA.NOCASE. +| 3888: 07 05 00 31 0f 17 45 4e 41 42 4c 45 20 44 32 53 ...1..ENABLE D2S +| 3904: 54 41 54 20 56 54 41 42 58 52 54 52 49 4d 11 06 TAT VTABXRTRIM.. +| 3920: 05 00 17 0f 19 44 45 42 55 47 58 42 49 4e 41 52 .....DEBUGXBINAR +| 3936: 59 11 05 05 00 17 0f 19 44 45 00 00 00 00 00 00 Y.......DE...... +| page 5 offset 16384 +| 0: 0d 00 00 00 02 0b a0 00 0c ad 0b a0 01 00 00 00 ................ +| 2976: 82 0a 02 08 08 09 08 08 17 84 06 30 20 32 35 33 ...........0 253 +| 2992: 00 01 30 04 25 06 1b 00 00 08 32 30 31 36 30 36 ..0.%.....201606 +| 3008: 30 38 03 25 07 00 00 01 34 03 25 05 00 00 01 35 08.%....4.%....5 +| 3024: 03 25 13 00 01 07 30 30 30 30 30 30 30 03 25 1a .%....0000000.%. +| 3040: 00 00 08 63 6f 6d 70 69 6c 65 72 03 25 02 00 00 ...compiler.%... +| 3056: 06 64 62 73 74 61 74 03 25 0a 00 01 04 65 62 75 .dbstat.%....ebu +| 3072: 67 03 25 08 00 00 06 65 6e 61 62 6c 65 09 25 09 g.%....enable.%. +| 3088: 05 04 04 04 04 04 00 01 08 78 74 65 6e 73 69 6f .........xtensio +| 3104: 6e 03 25 1d 00 00 04 66 74 73 34 03 25 0d 00 03 n.%....fts4.%... +| 3120: 01 35 03 25 0f 00 00 03 67 63 63 03 25 03 00 01 .5.%....gcc.%... +| 3136: 06 65 6f 70 6f 6c 79 03 25 11 00 00 05 6a 73 6f .eopoly.%....jso +| 3152: 6e 31 03 25 13 00 00 04 6c 6f 61 64 03 25 1c 00 n1.%....load.%.. +| 3168: 00 03 6d 62 78 03 25 18 00 01 05 65 6d 6f 72 79 ..mbx.%....emory +| 3184: 03 25 19 00 03 04 73 c8 73 35 03 25 15 00 00 04 .%....s.s5.%.... +| 3200: 6f 6d 69 74 03 25 1b 00 00 05 72 74 72 65 65 03 omit.%....rtree. +| 3216: 25 17 00 00 0a 74 68 72 65 61 64 73 61 66 65 03 %....threadsafe. +| 3232: 25 1e 00 00 04 76 74 61 62 03 25 0b 00 86 50 01 %....vtab.%...P. +| 3248: 08 08 08 08 08 17 8d 12 30 20 38 33 35 00 01 30 ........0 835..0 +| 3264: 12 01 06 00 01 06 00 01 06 00 1f 03 00 01 03 00 ................ +| 3280: 01 03 00 00 08 32 30 31 36 30 36 30 39 09 01 07 .....20160609... +| 3296: 00 01 07 00 01 07 00 00 01 34 09 01 05 00 01 05 .........4...... +| 3312: 00 01 05 00 00 01 35 09 01 04 00 01 04 00 02 04 ......5......... +| 3328: 00 01 07 30 2f 30 30 30 30 30 09 1c 04 00 01 04 ...0/00000...... +| 3344: 00 01 04 00 00 06 62 69 6e 61 72 79 3c 03 01 02 ......binary<... +| 3360: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3376: 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 ................ +| 3392: 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03 ................ +| 3408: 01 02 02 00 03 01 02 02 00 00 08 63 6f 6d 70 69 ...........compi +| 3424: 6c 65 72 09 01 02 00 01 02 00 01 02 00 00 06 64 ler............d +| 3440: 62 73 74 61 74 09 07 03 00 01 03 00 01 03 00 01 bstat........... +| 3456: 04 65 62 75 67 09 04 02 00 01 02 00 01 02 00 00 .ebug........... +| 3472: 06 65 6e 61 6c 2c 65 3f 07 02 00 01 02 00 01 02 .enal,e?........ +| 3488: 00 01 02 00 01 02 00 01 02 00 01 02 00 01 02 00 ................ +| 3504: 01 02 00 01 02 00 01 02 00 01 01 ff f1 02 00 01 ................ +| 3520: 02 00 01 02 00 01 02 00 f1 02 00 01 02 00 01 4f ...............O +| 3536: 00 01 02 00 01 02 00 01 08 78 74 65 6e 73 69 6f .........xtensio +| 3552: 6e 09 1f 04 00 01 04 00 01 04 00 00 04 66 74 73 n............fts +| 3568: 34 09 0a 03 00 01 03 00 00 f3 00 03 01 35 09 0d 4............5.. +| 3584: 03 00 01 04 00 01 03 00 00 03 67 63 63 09 01 03 ..........gcc... +| 3600: 00 01 03 00 01 03 00 01 06 65 6f 70 6f 6c 79 09 .........eopoly. +| 3616: 10 03 00 01 03 00 01 03 00 00 05 6a 73 6f 6e 31 ...........json1 +| 3632: 09 13 03 00 01 02 ff 01 03 00 00 04 6c 6f 61 63 ............loac +| 3648: 09 1f 03 00 01 03 00 01 03 00 00 03 6d 61 78 09 ............max. +| 3664: 1c 02 00 01 02 00 01 02 00 01 05 64 6d 6f 72 79 ...........dmory +| 3680: 09 1c 03 00 01 03 00 01 03 00 03 04 73 79 73 35 ............sys5 +| 3696: 09 16 02 f0 01 03 00 01 03 00 00 06 6e 6f 63 61 ............noca +| 3712: 73 65 3c 02 01 02 02 00 03 01 02 02 00 03 01 02 se<............. +| 3728: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3744: 00 03 01 02 02 00 03 01 02 02 00 4b 01 02 02 00 ...........K.... +| 3760: 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 00 ................ +| 3776: 04 6f 6d 69 74 09 1f 02 00 01 02 00 01 02 00 00 .omit........... +| 3792: 05 72 74 72 65 65 09 19 03 00 01 03 00 01 03 00 .rtree.......... +| 3808: 03 02 69 6d 3c 01 01 02 02 00 03 01 02 02 00 03 ..im<........... +| 3824: 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01 ................ +| 3840: 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 ................ +| 3856: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3872: 00 00 0a 74 68 72 65 61 64 73 61 66 65 09 22 02 ...threadsafe... +| 3888: 00 01 02 00 01 02 00 00 04 76 74 61 62 09 07 04 .........vtab... +| 3904: 00 01 04 00 01 04 00 00 01 78 b4 01 01 01 01 02 .........x...... +| 3920: 00 01 f4 01 02 00 01 02 01 02 00 01 01 01 02 ff ................ +| 3936: 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 ................ +| 3952: 01 01 02 ae 01 01 01 02 00 01 01 01 02 00 01 01 ................ +| 3968: 01 12 00 01 01 01 02 00 01 01 01 02 00 01 01 01 ................ +| 3984: 12 00 01 01 01 02 01 01 01 01 02 00 01 01 01 02 ................ +| 4000: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| 4016: 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 ................ +| 4032: 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 ................ +| 4048: 01 02 00 01 01 01 02 00 01 76 01 02 00 01 01 01 .........v...... +| 4064: 02 00 01 01 01 02 01 01 01 01 02 00 01 01 01 02 ................ +| 4080: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 02 0f f5 00 0f fb 0f f5 00 00 00 00 ................ +| 4080: 00 00 00 00 00 05 04 08 09 01 02 04 04 08 08 09 ................ +| end crash-74fdbc96edbc04.db +}]} {} + +do_catchsql_test 32.1 { + UPDATE t1 SET b=quote(zeroblob(6.51158946e+5)) WHERE a MATCH '*t*'; +} {1 {database disk image is malformed}} + +#do_catchsql_test 32.2 { +# UPDATE t1 SET b=((- '' )) WHERE a MATCH '0*t'; +#} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +# +ifcapable icu { + reset_db + do_catchsql_test 33.0 { + CREATE VIRTUAL TABLE f USING fts3(a,b,tokenize=icu); + CREATE TABLE 'f_docsize'(docid INTEGER PRIMARY KEY, size BLOB); + CREATE TABLE 'f_stat'(id INTEGER PRIMARY KEY, value BLOB); + INSERT INTO f VALUES (1, '1234'); + INSERT INTO f_stat VALUES (1,x'0000000165656565db6569746565c5c52bc5c5c53e3a003bc502ffffffffc5c5c53e3a003bc502fffffffffb8b2afbfb6565f0740100650000000165656565db6569746565c5c52bc5c5c53e3a003bc502ffffffffc5c5c53e3a003b8b00c5c5c5c5c5bfc5'); + INSERT INTO f(f) VALUES ('merge=198,49'); + } {1 {database disk image is malformed}} +} + +#------------------------------------------------------------------------- +# +reset_db +do_execsql_test 34.0 { + CREATE VIRTUAL TABLE f USING fts3(a,b); + INSERT INTO f VALUES (1, '1234'); + INSERT INTO f_segdir VALUES (1,255,0,0,'1 255',x'00'); + UPDATE f_segdir SET level = 0 WHERE level IN ( + SELECT level FROM f_segdir LIMIT 1 OFFSET 1 + ); + INSERT INTO f_segdir VALUES (255,249,0,121,'0 0',x'00'); + INSERT INTO f_content VALUES (255,0,x'ff'); + INSERT INTO f_segdir VALUES (1,255,16,0,'1 255',x'00'); +} + +do_catchsql_test 34.1 { + UPDATE f SET b = x'00' WHERE b IN (SELECT b FROM f LIMIT 1 OFFSET 0); +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +# +reset_db +do_execsql_test 35.0 { + CREATE VIRTUAL TABLE f USING fts3(a,b); + INSERT INTO f_segdir VALUES (1,255,0,0,'1 255',x'0001ff000001ff000001ff000001ff000001ff00c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5bec5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5'); +} + +do_catchsql_test 35.1 { + INSERT INTO f(f) VALUES ('integrity-check'); +} {1 {database disk image is malformed}} +do_execsql_test 35.2 { + PRAGMA integrity_check; +} {{malformed inverted index for FTS3 table main.f}} + +reset_db +do_catchsql_test 36.0 { + CREATE VIRTUAL TABLE f USING fts3(a,tokenize=porter); + CREATE TABLE 'f_stat'(id INTEGER PRIMARY KEY, value BLOB); + INSERT INTO f VALUES (1); + INSERT INTO f_stat VALUES (1,x'00000000000101010119013d00ffff0400fa83717b71a69297979701f63d010101010101010101010101190000000000000000fa83717b71a601f63d01010101010101010101010119013d00ffffff0400fa83717b71a69297979701f63d010101010101010101010101190000000000000000fa83717b71a69201f63d010101f63d01010101010101010101010119013d00ffffff0400fa83717b71a6929797010101010101010101010119013d00ffff01f63d01010101010101010101010119013d00ffffff0400fa83717b71a69297979701f63d00fa03ffffffa69297979701f63d010101000000000101010101197e9797976567656565ffa63535354e'); + INSERT INTO f(f) VALUES ('merge=53,216'); +} {0 {}} + +#------------------------------------------------------------------------- +# +reset_db +do_execsql_test 36.0 { + CREATE VIRTUAL TABLE f USING fts3(a,b); + CREATE TABLE 'f_stat'(id INTEGER PRIMARY KEY, value BLOB); + INSERT INTO f_stat VALUES (1,x'11014101000101c5c5014b010164c5014b010101c50101c5c5010201010101014101000101c5c5014b010101c5014b010101c50101c5c501010100c50101c5c5010101010101e40201010101014101000201010101014101000101010201010101014101000101c5c503b5fefefe3afeffffc5c5c5c50101010101010201010101014101adadadadadadadadadadadad91adadadadadadadad0101c50101c5c501f9ffffffffffffffff0001010102010101010140f5000101c5c5014b010101c50101c5c501010101e6010201010101014101000101c5c5014b010101c50101c5c5010101114b0101c5c50101010a0101020101e60101'); +} + +do_catchsql_test 36.1 { + INSERT INTO f(f) VALUES ('merge=59,59'); +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +# +reset_db +do_execsql_test 37.0 { + CREATE VIRTUAL TABLE f USING fts3(a,b); + INSERT INTO f_segdir VALUES (28,0,0,0,'0 0',x'00'); + INSERT INTO f_segdir VALUES (0,241,0,0,'0 0',x'0001000030310000f1'); +} + +do_catchsql_test 37.1 { + INSERT INTO f VALUES (0,x'00'); +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +# +reset_db +do_test 38.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 24576 pagesize 4096 filename crash-1cc4f8a70485ce.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 00 .....@ ........ +| 96: 00 00 00 00 0d 0e b1 00 06 0d a4 00 0f 8d 0f 21 ...............! +| 112: 0e b9 0d c8 0e 7e 0d a4 00 00 00 00 00 00 00 00 .....~.......... +| 3488: 00 00 00 00 22 07 06 17 11 11 01 31 74 61 62 6c ...........1tabl +| 3504: 65 74 32 74 32 07 43 52 45 41 54 45 20 54 41 42 et2t2.CREATE TAB +| 3520: 4c 45 20 74 32 28 78 29 81 33 05 07 17 1f 1f 01 LE t2(x).3...... +| 3536: 82 35 74 61 62 6c 65 74 31 5f 73 65 67 64 69 72 .5tablet1_segdir +| 3552: 74 31 5f 73 65 67 64 69 72 05 43 52 45 41 54 45 t1_segdir.CREATE +| 3568: 20 54 41 42 4c 45 20 27 74 31 5f 73 65 67 64 69 TABLE 't1_segdi +| 3584: 72 27 28 6c 65 76 65 6c 20 49 4e 54 45 47 45 52 r'(level INTEGER +| 3600: 2c 69 64 78 20 49 4e 54 45 47 45 52 2c 73 74 61 ,idx INTEGER,sta +| 3616: 72 74 5f 62 6c 6f 63 6b 20 49 4e 54 45 47 45 52 rt_block INTEGER +| 3632: 2c 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 ,leaves_end_bloc +| 3648: 6b 20 49 4e 54 45 47 45 52 2c 65 6e 64 5f 62 6c k INTEGER,end_bl +| 3664: 6f 63 6b 20 49 4e 54 45 47 45 62 2c 72 6f 6f 74 ock INTEGEb,root +| 3680: 20 42 4c 4f 42 2c 50 52 49 4d 41 52 59 20 4b 45 BLOB,PRIMARY KE +| 3696: 59 28 6c 65 76 65 6c 2c 20 69 64 78 29 29 31 06 Y(level, idx))1. +| 3712: 06 17 45 1f 01 00 69 6e 64 65 78 73 71 6c 69 74 ..E...indexsqlit +| 3728: 65 5f 61 75 74 6f 69 6e 64 65 78 5f 74 31 5f 73 e_autoindex_t1_s +| 3744: 65 67 64 69 72 5f 31 74 31 5f 73 65 67 64 69 72 egdir_1t1_segdir +| 3760: 06 0f c7 00 08 00 00 00 00 66 04 07 17 23 23 01 .........f...##. +| 3776: 81 13 74 61 62 6c 65 74 31 5f 73 65 67 6d 65 6e ..tablet1_segmen +| 3792: 74 73 74 31 5f 73 65 67 6d 65 6e 74 73 04 43 52 tst1_segments.CR +| 3808: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 73 EATE TABLE 't1_s +| 3824: 65 67 6d 65 6e 74 73 27 28 62 6c 6f 63 6b 69 64 egments'(blockid +| 3840: 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 INTEGER PRIMARY +| 3856: 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 KEY, block BLOB +| 3872: 29 6a 03 07 17 21 21 01 81 1f 74 61 62 6c 65 74 )j...!!...tablet +| 3888: 31 5f 63 6f 6e 74 65 6e 74 74 31 5f 63 6f 6e 74 1_contentt1_cont +| 3904: 65 6e 74 03 43 52 45 41 54 45 20 54 41 42 4c 45 ent.CREATE TABLE +| 3920: 20 27 74 31 5f 63 6f 6e 74 65 6e 74 27 28 64 6f 't1_content'(do +| 3936: 63 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d cid INTEGER PRIM +| 3952: 41 52 59 20 4b 45 59 2c 20 27 63 30 61 27 2c 20 ARY KEY, 'c0a', +| 3968: 27 63 31 62 27 2c 20 27 63 32 63 27 29 38 02 06 'c1b', 'c2c')8.. +| 3984: 17 11 11 08 5f 74 61 62 6c 65 74 31 74 31 43 52 ...._tablet1t1CR +| 4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 33 LE t1 USING fts3 +| 4032: 28 61 2c 62 2c 63 29 00 00 00 00 00 00 00 00 00 (a,b,c)......... +| page 3 offset 8192 +| 0: 0d 00 00 00 25 0b 48 00 0f d8 0f af 0f 86 0f 74 ....%.H........t +| 16: 0f 61 0f 4e 0f 2f 0f 0f 0e ef 0e d7 0e be 0e a5 .a.N./.......... +| 32: 0e 8d 0e 74 0e 5b 0e 40 0e 24 0e 08 0d ef 00 00 ...t.[.@.$...... +| 2880: 00 00 00 00 00 00 00 00 81 3f 25 06 00 82 7e f0 .........?%...~. +| 2896: 00 43 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2e .COMPILER=gcc-5. +| 2912: 34 23 00 20 42 30 31 36 2f 36 30 39 20 44 45 42 4#. B016/609 DEB +| 2928: 55 47 20 45 4e 41 42 4c 45 20 44 42 53 54 41 54 UG ENABLE DBSTAT +| 2944: 20 56 54 41 42 20 45 4e 42 92 4c 45 20 46 54 53 VTAB ENB.LE FTS +| 2960: 34 20 45 4e 41 42 4c 45 20 46 54 53 35 20 45 4e 4 ENABLE FTS5 EN +| 2976: 41 42 4c 45 20 47 45 4f 50 4f 4c 59 20 45 4e 41 ABLE GEOPOLY ENA +| 2992: 42 5c 45 1f 4a 53 4f 4e 31 20 45 4e 41 42 4c 45 B.E.JSON1 ENABLE +| 3008: 20 4d 45 4d 53 59 53 35 20 45 4e 41 42 4c 45 20 MEMSYS5 ENABLE +| 3024: 52 54 52 45 45 20 4d 41 58 20 4d 45 4d 4f 52 59 RTREE MAX MEMORY +| 3040: 3d 35 30 30 30 30 30 30 30 20 4f 4d 49 54 20 4c =50000000 OMIT L +| 3056: 4f 41 44 20 45 58 54 45 4e 53 49 4f 4e 20 54 48 OAD EXTENSION TH +| 3072: 52 45 41 44 53 41 46 45 3d 30 18 24 05 00 25 0f READSAFE=0.$..%. +| 3088: 19 54 48 52 45 41 44 53 41 46 45 3d 30 58 42 49 .THREADSAFE=0XBI +| 3104: 4e 41 52 59 18 23 05 00 25 0f 19 54 48 52 45 41 NARY.#..%..THREA +| 3120: 44 53 41 46 45 3d 30 88 4e 4f 43 41 53 45 17 22 DSAFE=0.NOCASE.. +| 3136: 05 00 25 0f 17 54 48 52 45 41 44 53 41 46 45 3d ..%..THREADSAFE= +| 3152: 30 58 52 54 52 49 4d 1f 21 05 00 33 0f 19 4f 4d 0XRTRIM.!..3..OM +| 3168: 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 4f IT LOAD EXTENSIO +| 3184: 4e 58 42 49 4e 41 52 59 1f 20 05 00 33 0f 19 4f NXBINARY. ..3..O +| 3200: 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 MIT LOAD EXTENSI +| 3216: 4f 4e 58 4e 4f 43 41 53 45 1e 1f 05 00 33 0f 17 ONXNOCASE....3.. +| 3232: 4f 4d 49 54 20 4c 4f 41 54 20 45 58 54 45 4e 53 OMIT LOAT EXTENS +| 3248: 49 4f 4e 58 52 54 52 49 4d 1f 1e 04 00 33 0f 19 IONXRTRIM....3.. +| 3264: 82 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 30 .AX MEMORY=50000 +| 3280: 30 30 30 58 42 49 4e 41 52 59 1f 1d 05 00 33 0f 000XBINARY....3. +| 3296: 19 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 .MAX MEMORY=5000 +| 3312: 30 30 30 30 58 4e 4f 43 41 53 45 1e 1c 05 00 33 0000XNOCASE....3 +| 3328: 0f 17 4d 41 58 20 4d 45 4d fa 52 59 3d 35 30 20 ..MAX MEM.RY=50 +| 3344: 30 30 30 30 30 58 52 54 52 49 4d 18 1b 05 00 25 00000XRTRIM....% +| 3360: 0f 19 45 4e 41 42 4c 45 20 52 54 52 45 45 58 42 ..ENABLE RTREEXB +| 3376: 49 4e 41 52 59 18 1a 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3392: 4c 45 20 52 54 52 45 45 58 4e 4f 43 41 53 45 17 LE RTREEXNOCASE. +| 3408: 19 05 00 25 0f 17 45 4e 42 42 4c 45 20 52 54 52 ...%..ENBBLE RTR +| 3424: 45 45 58 52 54 52 49 4d 1a 18 05 00 29 0f 19 45 EEXRTRIM....)..E +| 3440: 4e 41 42 4c 45 20 4d 45 4d 53 59 53 35 58 42 49 NABLE MEMSYS5XBI +| 3456: 4e 41 52 59 1a 17 05 00 29 0f 19 45 4e 41 42 3c NARY....)..ENAB< +| 3472: 45 20 4d 45 4d 53 59 53 35 58 4e 4f 43 41 53 45 E MEMSYS5XNOCASE +| 3488: 19 16 05 00 29 0f 17 45 4e 41 42 4c 45 20 4d 45 ....)..ENABLE ME +| 3504: 4d 53 59 53 35 58 52 54 52 49 4d 18 15 05 00 25 MSYS5XRTRIM....% +| 3520: 0f 19 45 4e 41 42 4c 45 20 4a 53 4f 4e 31 58 42 ..ENABLE JSON1XB +| 3536: 49 4e 41 52 59 18 14 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3552: 4c 45 20 4a 53 4f 4e 31 58 4e 4f 43 41 53 45 17 LE JSON1XNOCASE. +| 3568: 13 05 00 25 0f 17 45 4e 41 42 4c 45 20 4a 53 4f ...%..ENABLE JSO +| 3584: 4e 31 58 52 54 52 49 4d 95 12 05 00 29 0f 19 45 N1XRTRIM....)..E +| 3600: 4e 31 42 4c 45 20 47 45 4e 50 4f 4c 59 58 42 49 N1BLE GENPOLYXBI +| 3616: 4e 41 52 59 1a 11 05 00 29 0f 19 45 3e f2 1e 4c NARY....)..E>..L +| 3632: 45 20 47 45 4f 50 4f 4c 59 58 4e 4f 43 41 53 45 E GEOPOLYXNOCASE +| 3648: 19 10 05 00 29 0f 17 45 4e 41 42 4c 45 20 47 45 ....)..ENABLE GE +| 3664: 4f 50 4f 4c 59 58 52 54 52 49 4d 17 0f 05 00 23 OPOLYXRTRIM....# +| 3680: 0f 19 45 4e 41 42 4c 45 20 46 54 53 35 58 42 49 ..ENABLE FTS5XBI +| 3696: 4e 41 52 59 17 0e 05 00 23 0f 19 45 4e 41 42 3c NARY....#..ENAB< +| 3712: 45 20 46 54 53 35 58 4e 4f 43 41 53 45 16 0d 05 E FTS5XNOCASE... +| 3728: 00 23 0f 17 45 4e 41 42 4c 45 20 46 54 53 35 58 .#..ENABLE FTS5X +| 3744: 5d 24 52 49 4d 17 0c 05 00 23 0f 19 45 4e 41 42 ]$RIM....#..ENAB +| 3760: 4c 45 20 46 54 53 34 58 42 49 4e 41 52 59 17 0b LE FTS4XBINARY.. +| 3776: 05 00 23 0f 19 45 4e 41 42 4c 45 20 46 54 53 35 ..#..ENABLE FTS5 +| 3792: 58 4e 4f 43 40 53 45 16 0a 05 00 23 0f 17 45 4e XNOC@SE....#..EN +| 3808: 41 42 4c 45 20 46 54 53 34 58 52 54 52 49 4d 1e ABLE FTS4XRTRIM. +| 3824: 09 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3840: 54 41 54 20 56 54 41 42 58 42 49 4e 41 52 59 1e TAT VTABXBINARY. +| 3856: 08 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3872: 54 41 54 20 56 54 41 42 58 4e 4f 43 41 53 45 1d TAT VTABXNOCASE. +| 3888: 07 05 00 31 0f 17 45 4e 41 42 4b 45 20 44 42 53 ...1..ENABKE DBS +| 3904: 54 41 54 20 56 53 41 42 58 52 54 62 49 4d 11 06 TAT VSABXRTbIM.. +| 3920: 05 00 17 0f 19 44 45 42 54 47 58 42 49 4e 41 52 .....DEBTGXBINAR +| 3936: 59 11 05 05 00 17 0f 19 44 45 42 55 47 58 4e 4f Y.......DEBUGXNO +| 3952: 43 41 53 45 10 04 05 00 17 0f 17 44 45 42 55 47 CASE.......DEBUG +| 3968: 68 52 54 52 49 4d 27 03 05 00 43 0f 19 43 4f 4d hRTRIM'...C..COM +| 3984: 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e 30 20 PILER=gcc-5.4.0 +| 4000: 32 30 31 36 30 36 30 39 58 42 49 4e 41 52 59 27 20160609XBINARY' +| 4016: 02 05 00 43 0f 19 43 4f 4d 50 49 4c 45 52 3d 67 ...C..COMPILER=g +| 4032: 63 63 2d 35 2e 34 2e 30 20 32 30 31 36 30 36 30 cc-5.4.0 2016060 +| 4048: 39 58 4f 4f 43 41 53 45 26 01 05 00 43 0f 17 43 9XOOCASE&...C..C +| 4064: 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e OMPILER=gcc-5.4. +| 4080: 30 20 32 30 31 36 30 36 30 39 58 52 54 52 49 4d 0 20160609XRTRIM +| page 4 offset 12288 +| 0: 0d 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| page 5 offset 16384 +| 0: 0d 00 00 00 02 0b a0 00 0c ad 0b a0 00 00 00 00 ................ +| 2976: 82 0a 02 08 08 09 08 08 17 84 06 30 20 32 35 33 ...........0 253 +| 2992: 00 01 30 04 25 06 1b 00 00 08 32 30 31 36 8c 36 ..0.%.....2016.6 +| 3008: 30 39 03 25 07 00 00 01 34 03 25 05 00 00 01 35 09.%....4.%....5 +| 3024: 03 25 04 00 01 07 30 30 30 30 30 30 30 03 25 1a .%....0000000.%. +| 3040: 00 00 08 63 6f 5d 70 69 6c 65 72 03 25 02 00 00 ...co]piler.%... +| 3056: 06 64 62 73 74 61 74 03 25 0a 00 01 04 65 62 75 .dbstat.%....ebu +| 3072: 67 03 25 08 00 00 06 65 6e 61 62 6c 65 09 25 09 g.%....enable.%. +| 3088: 05 04 04 04 04 04 00 01 08 78 74 65 7e 73 69 6f .........xte~sio +| 3104: 6e 03 25 1d 00 00 04 66 74 73 34 03 25 0d 00 03 n.%....fts4.%... +| 3120: 01 35 03 25 0f 00 00 03 67 63 63 03 25 03 00 01 .5.%....gcc.%... +| 3136: 06 65 6f 70 7f 6c 79 03 25 11 00 00 05 6a 73 6f .eop.ly.%....jso +| 3152: 6e 31 03 25 14 00 e8 04 6c 6f 61 64 03 25 1c 00 n1.%....load.%.. +| 3168: 00 03 6d 61 78 03 25 18 00 01 05 65 6d 6f 72 79 ..max.%....emory +| 3184: 03 25 19 00 03 04 73 79 73 35 03 25 15 00 00 04 .%....sys5.%.... +| 3200: 6f 6d 69 74 03 25 1b 00 00 05 72 74 72 65 65 03 omit.%....rtree. +| 3216: 25 17 00 00 0a 74 68 72 65 61 64 73 61 66 65 03 %....threadsafe. +| 3232: 25 1e 00 00 04 76 74 61 62 03 25 0b 00 86 50 01 %....vtab.%...P. +| 3248: 08 08 08 08 08 17 8d 12 30 20 38 33 35 00 01 30 ........0 835..0 +| 3264: 12 01 06 00 01 06 00 01 06 00 1f 03 00 01 03 00 ................ +| 3280: 01 03 00 00 08 32 30 31 36 30 36 30 39 09 01 07 .....20160609... +| 3296: 00 01 07 00 01 07 00 00 01 34 09 01 05 00 01 05 .........4...... +| 3312: 00 01 05 00 00 01 35 09 01 04 00 01 04 00 01 04 ......5......... +| 3328: 00 01 07 30 30 30 30 30 30 30 09 1c 04 00 01 04 ...0000000...... +| 3344: 00 01 04 00 00 06 62 69 6e 61 72 79 3c 03 01 02 ......binary<... +| 3360: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3376: 00 03 01 02 c2 00 03 01 02 02 00 03 01 04 82 00 ................ +| 3392: 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03 ................ +| 3408: 01 02 02 00 03 01 02 02 00 00 08 63 6f 6d 70 69 ...........compi +| 3424: 6c 65 72 09 01 02 00 01 02 00 01 02 00 00 06 64 ler............d +| 3440: 62 73 74 61 74 09 07 03 00 01 03 00 01 03 00 01 bstat........... +| 3456: 04 65 62 75 67 09 04 02 00 01 02 00 01 02 00 00 .ebug........... +| 3472: 06 65 6e 60 62 6c 65 3f 07 02 00 01 02 92 e1 a4 .en`ble?........ +| 3488: ff fc a2 8c 95 b2 3f 01 01 f0 f1 02 00 57 02 00 ......?......W.. +| 3504: 01 02 00 01 02 00 01 02 00 01 02 00 01 02 10 01 ................ +| 3520: 02 00 01 02 00 01 02 00 01 02 01 01 02 00 01 02 ................ +| 3536: 00 01 02 00 00 f2 00 01 08 78 74 65 6e 73 69 6f .........xtensio +| 3552: 6e 09 1f 04 00 01 04 00 01 04 00 00 04 66 74 73 n............fts +| 3568: 34 09 0a 03 00 01 03 00 01 03 00 03 01 35 09 0d 4............5.. +| 3584: 03 00 01 03 00 01 03 00 00 03 67 63 63 09 01 03 ..........gcc... +| 3600: 00 01 03 00 01 03 00 01 06 65 5f 70 6f 6c 79 09 .........e_poly. +| 3616: 10 03 00 01 03 00 01 03 00 00 b3 6a 73 6f 6e 31 ...........json1 +| 3632: 09 13 03 00 01 03 00 01 03 00 00 04 6c 6f 61 64 ............load +| 3648: 09 1f 03 00 01 03 00 01 03 00 00 03 6d 61 78 09 ............max. +| 3664: 1c 02 00 01 02 00 01 02 00 01 05 65 6d 6f 72 79 ...........emory +| 3680: 09 1c 03 00 01 03 00 01 03 00 03 04 73 79 73 35 ............sys5 +| 3696: 09 16 03 00 01 03 00 01 03 cc 00 06 6e 6f 63 61 ............noca +| 3712: 73 65 3c 02 01 02 02 00 03 01 02 02 00 03 01 02 se<............. +| 3728: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3744: 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 ................ +| 3760: 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 00 ................ +| 3776: 04 6f 6d 69 74 09 1f 02 00 01 02 00 01 02 00 00 .omit........... +| 3792: 05 72 74 62 65 65 09 19 03 00 01 03 00 01 03 00 .rtbee.......... +| 3808: 03 02 69 6d 3c 01 01 02 02 00 03 01 02 02 00 03 ..im<........... +| 3824: 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01 ................ +| 3840: 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 ................ +| 3856: 02 00 03 01 02 02 00 03 01 02 01 00 03 01 02 02 ................ +| 3872: 00 00 0a 74 68 72 65 61 64 73 61 66 65 09 22 02 ...threadsafe... +| 3888: 00 01 02 00 02 02 00 00 04 76 74 61 62 09 07 04 .........vtab... +| 3904: 00 01 03 00 01 04 00 00 01 78 b4 01 01 01 01 02 .........x...... +| 3920: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| 3936: 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 ................ +| 3952: 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 ................ +| 3968: 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 ................ +| 3984: 02 01 01 01 01 02 00 01 01 01 02 00 01 01 01 02 ................ +| 4000: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| 4016: 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 ................ +| 4032: 01 01 02 00 01 01 01 da 00 00 f1 01 02 00 01 01 ................ +| 4048: 01 02 00 01 01 01 01 ff ff 01 01 02 00 01 01 01 ................ +| 4064: 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 ................ +| 4080: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 02 0f f5 00 0f fb 0f f5 01 00 00 00 ................ +| 4080: 00 00 00 00 00 05 04 08 09 01 02 04 04 08 08 09 ................ +| end crash-1cc4f8a70485ce.db +}]} {} + +do_execsql_test 38.1 { + UPDATE t1 SET b=a; +} + +do_catchsql_test 38.2 { + SELECT b FROM t1 WHERE a MATCH 'e*e*e*e*e*e*e*e*e*e*e*e*e*e*e*e*' +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +reset_db +set saved $sqlite_fts3_enable_parentheses +set sqlite_fts3_enable_parentheses 1 +do_execsql_test 39.0 { + CREATE VIRTUAL TABLE t0 USING fts3( + col0 INTEGER PRIMARY KEY, + col1 VARCHAR(8), + col2 BINARY, + col3 BINARY + ); + INSERT INTO t0_content VALUES(1,1,'1234','aaaa','bbbb'); + INSERT INTO t0_segdir VALUES(0,0,0,0,'0 42',X'000131030782000103323334050101010200000461616161050101020200000462626262050101030200'); +} + +do_test 39.1 { + catch { + db eval { SELECT rowid FROM t0 WHERE t0 MATCH '1 NEAR 1' } + } +} 0 + +do_test 39.2 { + catch { + db eval { + SELECT matchinfo(t0,'yxy') FROM t0 WHERE t0 MATCH x'2b0a312b0a312a312a2a0b5d0a0b0b0a312a0a0b0b0a312a0b310a392a0b0a27312a2a0b5d0a312a0b310a31315d0b310a312a316d2a0b313b15bceaa50a312a0b0a27312a2a0b5d0a312a0b310a312b0b2a310a312a0b2a0b2a0b2e5d0a0bff313336e34a2a312a0b0a3c310b0a0b4b4b0b4b2a4bec40322b2a0b310a0a312a0a0a0a0a0a0a0a0a0b310a312a2a2a0b5d0a0b0b0a312a0b310a312a0b0a4e4541530b310a5df5ced70a0a0a0a0a4f520a0a0a0a0a0a0a312a0b0a4e4541520b310a5d616161610a0a0a0a4f520a0a0a0a0a0a312b0a312a312a0a0a0a0a0a0a004a0b0a310b220a0b0a310a4a22310a0b0a7e6fe0e0e030e0e0e0e0e01176e02000e0e0e0e0e01131320226310a0b0a310a4a22310a0b0a310a766f8b8b4ee0e0300ae0090909090909090909090909090909090909090909090909090909090909090947aaaa540b09090909090909090909090909090909090909090909090909090909090909fae0e0f2f22164e0e0f273e07fefefef7d6dfafafafa6d6d6d6d'; + } + } +} 0 +set sqlite_fts3_enable_parentheses $saved + +#------------------------------------------------------------------------- +reset_db +set saved $sqlite_fts3_enable_parentheses +set sqlite_fts3_enable_parentheses 1 + +do_execsql_test 40.1 { + + CREATE VIRTUAL TABLE t0 USING fts3(col0 INTEGER PRIMARY KEY, col1, col2 ,col3 ); + INSERT INTO t0_segdir VALUES(0,0,0,0,'0 42', + X'0001310301020001033233340500010102000004616161bc050101020200000462626262050101030200' + ); +} + +do_execsql_test 40.2 { + SELECT 0==matchinfo(t0,'sx') FROM t0 WHERE t0 MATCH '1* 2 3 4 5 6 OR 1'; +} 0 + +set sqlite_fts3_enable_parentheses $saved + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 41.1 { + CREATE VIRTUAL TABLE t1 USING fts3(a,b,c); + INSERT INTO t1_segdir VALUES(0,0,0,0,'0 835',X'000130120106000106000106001f030001030001030000083230313630363039090107000107000107000001340901050001050001050000013509010400010400010400010730303030303030091c0400010400010400000662696e6172793c0301020200030102020003010202000301020200030102020003010202000301020200030102020003010202000301020200030102020003010202000008636f3870696c657209010200010200010200000664627374617409070300010300010300010465627567090402000102000102000006656e61626c653f07020001020001020001020001020001020001020001020001020001030001010002020001020001020001020001120001020001020001020001020001020001087874656e73696f6e091f0400010400010400000466747334090a0300010300010400030135090d03000103000103000003676363090103000103000103000106656f706f6c790910030001030001030000056a736f6e310913030001030001030000046c6f6164091f030001030001030000036d6178091c02000102000102000105656d6f7279091c03000103000103000304737973350916030001030001030000066e6f636173653c02010202000301020200030102020003010202000301020200030102020003010202000301020200030102020003010202000301020200030102020000046f6d6974091f020001020001020000057274726565091903000103000103000302696d3c010102020003010202000301020200030102020003010202000301020200030102020003010202000301020200030102020003010202000301020200000a746872656164736166650922020001020001020000047674616209070400010400010400000178b401010101020001010102000101010200010101020001010102000101010200010101020001010102000101010200010101020001010102000101010200010101020001010102000101010200010101020001010102000101010200010101020001010102000101010200010101020001010102000101010200010101020001010102000101010200010101020001010102000101010200010101020001010102000101010200010101020001010102000101010200'); +} + +do_execsql_test 41.2 { + SELECT offsets(t1) FROM t1 WHERE t1 MATCH 'rtree ner "json1^enable"'; +} + +#------------------------------------------------------------------------- +do_execsql_test 42.1 { + CREATE VIRTUAL TABLE f USING fts3(a, b); +} +do_execsql_test 42.2 { + INSERT INTO f_segdir VALUES(0,2,1111,0,0,X'00'); + INSERT INTO f_segdir VALUES(0,3,0 ,0,0,X'00013003010200'); +} +do_execsql_test 42.3 { + INSERT INTO f(f) VALUES ('merge=107,2'); +} + +#------------------------------------------------------------------------- +reset_db +set saved $sqlite_fts3_enable_parentheses +set sqlite_fts3_enable_parentheses 1 +do_execsql_test 43.1 { + CREATE VIRTUAL TABLE def USING fts3(xyz); + INSERT INTO def_segdir VALUES(0,0,0,0,0, X'0001310301c9000103323334050d81'); +} {} + +do_execsql_test 43.2 { + SELECT rowid FROM def WHERE def MATCH '1 NEAR 1' +} {1} + +set sqlite_fts3_enable_parentheses $saved + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 44.1 { + CREATE VIRTUAL TABLE t0 USING fts3(col0 INTEGER PRIMARY KEY,col1 VARCHAR(8),col2 BINARY,col3 BINARY); + INSERT INTO t0_content VALUES(0,NULL,NULL,NULL,NULL); + INSERT INTO t0_segdir VALUES(0,0,0,0,'0 42',X'00013103010200010332333405010201ba00000461616161050101020200000462626262050101030200'); +} + +do_execsql_test 44.2 { + SELECT matchinfo(t0, t0) IS NULL FROM t0 WHERE t0 MATCH '1*' +} {0} + +#------------------------------------------------------------------------- +# +reset_db +do_test 45.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 24576 pagesize 4096 filename crash-65c98512cc9e49.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 06 .....@ ........ +| 96: 00 00 00 00 0d 0e fc 00 06 0d bc 00 0f ca 0f 6c ...............l +| 112: 0f 04 0e 13 0e c9 0d bc 00 00 00 00 00 00 00 00 ................ +| 3504: 00 00 00 00 00 00 00 00 00 00 00 00 55 06 07 17 ............U... +| 3520: 1b 1b 01 81 01 74 61 62 6c 65 78 31 5f 73 74 61 .....tablex1_sta +| 3536: 74 78 31 5f 73 74 61 74 06 43 52 45 41 54 45 20 tx1_stat.CREATE +| 3552: 54 41 42 4c 45 20 27 78 31 5f 73 74 61 74 27 28 TABLE 'x1_stat'( +| 3568: 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 id INTEGER PRIMA +| 3584: 52 59 20 4b 45 59 2c 20 76 61 6c 75 65 20 42 4c RY KEY, value BL +| 3600: 41 82 29 81 33 04 07 17 1f 1f 01 82 35 74 61 62 A.).3.......5tab +| 3616: 6c 65 78 31 5f 73 65 67 64 69 72 78 31 5f 73 65 lex1_segdirx1_se +| 3632: 67 64 69 72 04 43 52 45 41 54 45 20 54 41 42 4c gdir.CREATE TABL +| 3648: 45 20 27 78 31 5f 73 65 67 64 69 72 27 28 6c 65 E 'x1_segdir'(le +| 3664: 76 65 6c 20 49 4e 54 45 47 45 52 2c 69 64 78 20 vel INTEGER,idx +| 3680: 49 4e 54 45 47 45 52 2c 73 74 61 72 74 5f 62 6c INTEGER,start_bl +| 3696: 6f 63 6b 20 49 4e 54 45 47 45 52 2c 6c 65 61 76 ock INTEGER,leav +| 3712: 65 73 5f 65 6e 64 5f 62 6c 6f 63 6b 20 49 4e 54 es_end_block INT +| 3728: 45 47 45 52 2c 65 6e 64 5f 62 6c 6f 63 6b 20 49 EGER,end_block I +| 3744: 4e 54 45 47 45 52 2c 72 6f 6f 74 20 42 4c 4f 42 NTEGER,root BLOB +| 3760: 2c 50 52 49 4d 41 52 59 20 4b 45 59 28 6c 65 76 ,PRIMARY KEY(lev +| 3776: 65 6c 2c 20 69 64 78 29 29 31 05 06 17 45 1f 01 el, idx))1...E.. +| 3792: 00 69 6e 64 65 78 73 71 6c 69 74 65 5f 61 75 74 .indexsqlite_aut +| 3808: 6f 69 6e 64 65 78 5f 78 31 5f 73 65 67 64 69 72 oindex_x1_segdir +| 3824: 5f 31 78 31 5f 73 65 67 64 69 72 05 00 00 00 08 _1x1_segdir..... +| 3840: 60 00 00 00 66 03 07 17 23 23 01 81 13 74 61 62 `...f...##...tab +| 3856: 6c 65 78 31 5f 73 65 67 6d 65 6e 74 73 78 31 5f lex1_segmentsx1_ +| 3872: 73 65 67 6d 65 6e 74 73 03 43 52 45 41 54 45 20 segments.CREATE +| 3888: 54 41 42 4c 45 20 27 78 31 5f 73 65 67 6d 65 6e TABLE 'x1_segmen +| 3904: 74 73 27 28 62 6c 6f 63 6b 69 64 20 49 4e 54 45 ts'(blockid INTE +| 3920: 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c GER PRIMARY KEY, +| 3936: 20 62 6c 6f 63 6b 20 42 4c 4f 42 29 5c 02 07 17 block BLOB).... +| 3952: 21 21 01 81 03 74 61 62 6c 65 78 31 5f 63 6f 6e !!...tablex1_con +| 3968: 74 65 6e 74 78 31 5f 63 6f 6e 74 65 6e 74 02 43 tentx1_content.C +| 3984: 52 45 41 54 45 20 54 41 42 4c 45 20 27 78 31 5f REATE TABLE 'x1_ +| 4000: 63 6f 6e 74 65 6e 74 27 28 64 6f 63 69 64 20 49 content'(docid I +| 4016: 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b NTEGER PRIMARY K +| 4032: 45 59 2c 20 27 63 30 78 27 29 34 01 06 17 11 11 EY, 'c0x')4..... +| 4048: 08 57 74 61 62 6c 65 78 31 78 31 43 52 45 41 54 .Wtablex1x1CREAT +| 4064: 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45 20 E VIRTUAL TABLE +| 4080: 78 31 20 55 53 49 4e 47 20 66 74 73 33 28 78 29 x1 USING fts3(x) +| page 2 offset 4096 +| 0: 0d 00 00 00 11 0f 77 f0 0f f8 0f f0 0f e8 0f e0 ......w......... +| 16: 0f d8 0f d0 0f c8 0f c0 00 00 00 00 00 00 00 00 ................ +| 3952: 00 00 00 00 00 00 00 00 06 11 03 00 13 77 78 79 .............wxy +| 3968: 06 10 03 00 13 74 75 76 06 0f 03 00 13 71 33 73 .....tuv.....q3s +| 3984: 06 0e 03 00 13 6e 6f 70 06 0d 03 00 13 6b 6c 6d .....nop.....klm +| 4000: 06 0c 03 04 c3 68 69 6a 06 0b 03 00 13 65 66 67 .....hij.....efg +| 4016: 06 0a 03 00 13 62 63 64 06 09 03 00 13 79 7a 61 .....bcd.....yza +| 4032: 06 08 03 00 13 76 77 78 06 07 03 00 13 73 74 75 .....vwx.....stu +| 4048: 06 06 03 00 13 70 71 72 06 05 03 00 13 6d 6e 6f .....pqr.....mno +| 4064: 06 03 03 00 13 6a 6b 6c 06 03 03 00 13 67 68 69 .....jkl.....ghi +| 4080: 06 02 02 00 03 64 65 66 06 01 03 00 13 61 52 63 .....def.....aRc +| page 3 offset 8192 +| 0: 0d 00 00 00 03 0f a7 00 0f b5 0f a7 0f fa 01 00 ................ +| 4000: 00 00 00 00 00 00 00 0c 02 03 00 1e 00 03 6b 6c ..............kl +| 4016: 6d 03 0d 02 00 43 01 04 00 81 0a 00 03 61 62 63 m....C.......abc +| 4032: 03 0b 32 00 00 03 62 63 64 03 0a 02 00 00 03 64 ..2...bcd......d +| 4048: 69 26 03 02 02 00 00 03 65 66 67 03 0b 02 00 00 i&......efg..... +| 4064: 03 67 68 69 03 03 02 00 00 03 68 69 6a 03 0c 02 .ghi......hij... +| 4080: 00 00 03 6a 6a 2c 03 04 02 00 03 81 00 03 00 00 ...jj,.......... +| page 4 offset 12288 +| 0: 0d 0f 3a 00 05 0f 25 00 0f 9e 0f 88 0f 43 0f 25 ..:...%......C.% +| 16: 0f 72 00 00 00 00 00 00 00 00 00 00 00 00 00 00 .r.............. +| 3856: 00 00 00 00 00 00 00 00 00 56 01 08 08 13 1e 03 .........V...... +| 3872: 30 20 39 00 03 13 05 07 08 08 18 08 13 1e 30 20 0 9...........0 +| 3888: 39 00 03 77 78 79 03 11 02 00 0f 6c 00 09 01 08 9..wxy.....l.... +| 3904: 08 15 54 27 04 07 09 01 08 08 15 42 02 30 20 33 ..T'.......B.0 3 +| 3920: 36 00 03 6e 6f 70 03 0e 02 00 00 03 71 72 73 03 6..nop......qrs. +| 3936: 0f 02 00 00 03 74 75 76 03 10 02 00 0f cf b1 06 .....tuv........ +| 3952: 01 08 14 06 07 01 08 09 01 1b 14 02 02 31 32 38 .............128 +| 3968: 20 2d 37 32 10 01 01 6b 14 03 07 09 09 08 08 15 -72...k........ +| 3984: 1e 30 20 33 36 00 03 79 7a 61 03 09 02 00 2f 02 .0 36..yza..../. +| 4000: 07 09 08 08 08 15 54 30 20 33 36 00 03 6d 6e 6f ......T0 36..mno +| 4016: 03 05 02 00 00 03 70 71 72 03 06 02 00 00 03 73 ......pqr......s +| 4032: 74 75 03 07 02 00 00 03 76 77 78 03 08 02 00 00 tu......vwx..... +| 4048: 00 00 4a 08 08 08 15 54 30 20 33 36 00 03 61 62 ..J....T0 36..ab +| 4064: 63 03 01 02 00 00 03 64 65 66 03 02 02 00 00 03 c......def...... +| 4080: 67 68 69 03 03 67 00 00 03 6a 6b 6c 03 04 02 00 ghi..g...jkl.... +| page 5 offset 16384 +| 0: 0a 0f e7 00 05 0f da 00 0f e1 0f fa 0f f4 0f ed ................ +| 16: 0f da 0f 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 4048: 00 00 00 00 00 00 00 1a 01 03 06 04 01 08 01 02 ................ +| 4064: 06 05 04 08 08 01 05 00 00 00 06 01 03 06 04 09 ................ +| 4080: 02 01 02 04 05 04 09 09 01 03 05 04 09 08 01 02 ................ +| page 6 offset 20480 +| 0: 0d 00 10 00 01 0f f9 00 0f f9 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 05 01 03 00 10 01 03 ................ +| end crash-65c98512cc9e49.db +}]} {} + +do_catchsql_test 45.2 { + INSERT INTO x1(x1) VALUES( 'merge=1' ) +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +reset_db +set saved $sqlite_fts3_enable_parentheses +set sqlite_fts3_enable_parentheses 1 +do_execsql_test 46.1 { + CREATE VIRTUAL TABLE t0 USING fts3(a INTEGER PRIMARY KEY,b,c,d); + INSERT INTO t0_segdir VALUES(0,0,0,0,'0 42',X'0001310301c9000103323334050d8000f200000461616161050101020200000462626262050101030200'); +} {} + +do_catchsql_test 46.2 { + SELECT * FROM t0 + WHERE t0 MATCH x'2b0a312b0a312a312a2a0b5d0a0b0b0a312a0a0b0b0a312a0b310a392a0b0a27312a2a0b5d0a312a0b310a31315d0b310a312a316d2a0b313b15bceaa50a312a0b0a27312a2a0b5d0a312a0b310a312b0b2a310a312a0b2a0b2a0b2e5d0a0bff313336e34a2a312a0b0a3c310b0a0b4b4b0b4b2a4bec40322b2a0b310a0a312a0a0a0a0a0a0a0a0a0b310a312a2a2a0b5d0a0b0b0a312a0b310a312a0b0a4e4541530b310a5df5ced70a0a0a0a0a4f520a0a0a0a0a0a0a312a0b0a4e4541520b310a5d616161610a0a0a0a4f520a0a0a0a0a0a312b0a312a312a0a0a0a0a0a0a004a0b0a310b220a0b0a310a4a22310a0b0a7e6fe0e0e030e0e0e0e0e01176e02000e0e0e0e0e01131320226310a0b0a310a4a22310a0b0a310a766f8b8b4ee0e0300ae0090909090909090909090909090909090909090909090909090909090909090947aaaa540b09090909090909090909090909090909090909090909090909090909090909fae0e0f2f22164e0e0f273e07fefefef7d6dfafafafa6d6d6d6d'; +} {1 {database disk image is malformed}} + +set sqlite_fts3_enable_parentheses $saved +extra_schema_checks 1 + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 47.1 { + CREATE VIRTUAL TABLE t1 USING fts3(a,b,c); +} +do_execsql_test 47.2 { + INSERT INTO t1_segdir VALUES(0,0,0,0,0,X'000130120106000106000106001f030001030001030000083230313630363039090107000107000107000001340901050001050001050000013509010400010400010400010730303030303030091c0400010400010400000662696e6172793c0301020200030102020003010202000301020200030102020003010202000301020200030102020003010202000301020200030102020003010202000008636f6d70696c657209010200010200010200000664627374617409070300010300010300010465627567090402000102000102000006656e61626c653f07020001020001020001020001020001020001020001020001020001020001020001020001010001020001020001020001020001020001020001020001020001087874656e73696f6e091f0400010400010400000466747334090a0300010300010300030135090d03000103000103000003676363090103000103000103000106656f706f6c790910030001030001030000056a736f6e310913030001030001030000046c6f6164091f030001030001030000036d6178091c02000102000102000105656d6f7279091c03000103000103000304737973350916030001030001030000066e6f636173653c02010202000301020200030102020003010202000301020200030102020003010202000301020200030102020003010202000301020200030102020000046f6d6974091f020001020001020000057274726565091903000103000103000302696d3c01010202000301020200030102020003010202000301020200030102020003010202000301a202000301020200030102020003010202000301020200000a746872656164736166650922020001020001020000047674616209070400010400010400000178b401010101020001010102000101010200010101020001010102000101010200010101020001010102000101010200010101020001010102000101010200010101020001010102000101010200010101020001010102000101010200010101020001010102000101010200010101020001010102000101010200010101020001010102000101010200010101020001010102000101010200010101020001010102000101010200010101020001010102000101010200'); + INSERT INTO t1_segdir VALUES(0,1,0,0,0,X'0001300425061b000008323031363036303903250700000134032505000001350325040001073030303030303003251a000008636f6d70696c657203250200000664627374617403250a00010465627567032508000006656e61626c650925090504040404040001087874656e73696f6e03251d0000046674733403250d0003013503250f000003676363032503000106656f706f6c790325110000056a736f6e310325130000046c6f616403251c0000036d6178032518000105656d6f7279032519000304737973350325150000046f6d697403251b000005727472656503251700000a7468726561647361666503251e0000047674616333250b00'); +} + +do_catchsql_test 47.3 { + SELECT matchinfo(t1) FROM t1 WHERE t1 MATCH '"json1 enable"'; +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +# +reset_db +do_test 48.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 20480 pagesize 4096 filename sql038051.txt.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 00 .....@ ........ +| 96: 00 00 00 00 0d 0e fc 00 05 0e 13 00 0f ca 0f 6c ...............l +| 112: 0f 04 0e 13 0e c9 00 00 00 00 00 00 00 00 00 00 ................ +| 3600: 00 00 00 81 33 04 07 17 1f 1f 01 82 35 74 61 62 ....3.......5tab +| 3616: 6c 65 78 31 5f 73 65 67 64 69 72 78 31 5f 73 65 lex1_segdirx1_se +| 3632: 67 64 69 72 04 43 52 45 41 54 45 20 54 41 42 4c gdir.CREATE TABL +| 3648: 45 20 27 78 31 5f 73 65 67 64 69 72 27 28 6c 65 E 'x1_segdir'(le +| 3664: 76 65 6c 20 49 4e 54 45 47 45 52 2c 69 64 78 20 vel INTEGER,idx +| 3680: 49 4e 54 45 47 45 52 2c 73 74 61 72 74 5f 62 6c INTEGER,start_bl +| 3696: 6f 63 6b 20 49 4e 54 45 47 45 52 2c 6c 65 61 76 ock INTEGER,leav +| 3712: 65 73 5f 65 6e 64 5f 62 6c 6f 63 6b 20 49 4e 54 es_end_block INT +| 3728: 45 47 45 52 2c 65 6e 64 5f 62 6c 6f 63 6b 20 49 EGER,end_block I +| 3744: 4e 54 45 47 45 52 2c 72 6f 6f 74 20 42 4c 4f 42 NTEGER,root BLOB +| 3760: 2c 50 52 49 4d 41 52 59 20 4b 45 59 28 6c 65 76 ,PRIMARY KEY(lev +| 3776: 65 6c 2c 20 69 64 78 29 29 31 05 06 17 45 1f 01 el, idx))1...E.. +| 3792: 00 69 6e 64 65 78 73 71 6c 69 74 65 5f 61 75 74 .indexsqlite_aut +| 3808: 6f 69 6e 64 65 78 5f 78 31 5f 73 65 67 64 69 72 oindex_x1_segdir +| 3824: 5f 31 78 31 5f 73 65 67 64 69 72 05 00 00 00 08 _1x1_segdir..... +| 3840: 00 00 00 00 66 03 07 17 23 23 01 81 13 74 61 62 ....f...##...tab +| 3856: 6c 65 78 31 5f 73 65 67 6d 65 6e 74 73 78 31 5f lex1_segmentsx1_ +| 3872: 73 65 67 6d 65 6e 74 73 03 43 52 45 41 54 45 20 segments.CREATE +| 3888: 54 41 42 4c 45 20 27 78 31 5f 73 65 67 6d 65 6e TABLE 'x1_segmen +| 3904: 74 73 27 28 62 6c 6f 63 6b 69 64 20 49 4e 54 45 ts'(blockid INTE +| 3920: 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c GER PRIMARY KEY, +| 3936: 20 62 6c 6f 63 6b 20 42 4c 4f 42 29 5c 02 07 17 block BLOB).... +| 3952: 21 21 01 81 03 74 61 62 6c 65 78 31 5f 63 6f 6e !!...tablex1_con +| 3968: 74 65 6e 74 78 31 5f 63 6f 6e 74 65 6e 74 02 43 tentx1_content.C +| 3984: 52 45 41 54 45 20 54 41 42 4c 45 20 27 78 31 5f REATE TABLE 'x1_ +| 4000: 63 6f 6e 74 65 6e 74 27 28 64 6f 63 69 64 20 49 content'(docid I +| 4016: 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b NTEGER PRIMARY K +| 4032: 45 59 2c 20 27 63 30 78 27 29 34 01 06 17 11 11 EY, 'c0x')4..... +| 4048: 08 57 74 61 62 6c 65 78 31 78 31 43 52 45 41 54 .Wtablex1x1CREAT +| 4064: 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45 20 E VIRTUAL TABLE +| 4080: 78 31 20 55 53 49 4e 47 20 66 74 73 33 28 78 29 x1 USING fts3(x) +| page 2 offset 4096 +| 0: 0d 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 3920: 00 00 00 2e 04 03 00 63 62 72 61 69 6e 73 74 65 .......cbrainste +| 3936: 6d 20 62 72 61 69 6e 73 74 65 6d 73 20 62 72 61 m brainstems bra +| 3952: 69 6e 73 74 6f 72 6d 20 62 72 61 69 6e 73 74 6f instorm brainsto +| 3968: 72 6d 73 2b 03 03 00 5d 62 72 61 69 6e 20 62 72 rms+...]brain br +| 3984: 61 69 6e 63 68 69 6c 64 20 62 72 61 69 6e 65 64 ainchild brained +| 4000: 20 62 72 61 69 6e 69 6e 67 20 62 72 61 69 6e 73 braining brains +| 4016: 26 02 03 00 53 62 72 61 67 73 20 62 72 61 69 64 &...Sbrags braid +| 4032: 20 62 72 61 69 64 65 64 20 62 72 61 69 64 69 6e braided braidin +| 4048: 67 20 62 72 61 69 64 73 26 01 03 00 53 62 72 61 g braids&...Sbra +| 4064: 65 73 20 62 72 61 67 20 62 72 61 67 67 65 64 20 es brag bragged +| 4080: 62 72 61 c3 67 65 72 20 62 72 61 67 67 69 6e 67 bra.ger bragging +| page 3 offset 8192 +| 0: 0d 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 ................ +| page 4 offset 12288 +| 0: 0d 00 00 00 04 0f 20 00 0f c8 0f 90 0f 54 0f 20 ...... ......T. +| 3872: 32 04 07 08 01 08 08 15 58 03 30 20 33 38 00 09 2.......X.0 38.. +| 3888: 62 72 61 69 6e 73 74 65 6d 03 04 02 00 09 01 73 brainstem......s +| 3904: 03 04 03 00 07 03 6f 72 6d 03 04 04 00 0a 01 73 ......orm......s +| 3920: 03 04 05 00 3a 03 07 08 01 08 08 15 68 02 30 20 ....:.......h.0 +| 3936: 34 36 00 05 62 72 61 69 6e 03 03 02 00 05 05 63 46..brain......c +| 3952: 68 69 6c 64 03 03 03 00 05 02 65 64 03 03 04 00 hild......ed.... +| 3968: 05 03 69 6e 67 03 03 05 00 05 01 73 03 03 06 00 ..ing......s.... +| 3984: 36 02 07 08 09 08 08 15 62 30 20 34 33 00 05 62 6.......b0 43..b +| 4000: 72 61 67 73 03 02 02 00 03 02 69 64 03 02 03 00 rags......id.... +| 4016: 05 02 65 64 03 02 04 00 05 03 69 6e 67 03 02 05 ..ed......ing... +| 4032: 00 05 01 73 03 02 06 00 36 01 07 08 08 08 08 15 ...s....6....... +| 4048: 62 30 20 34 33 00 05 62 72 61 65 73 03 01 02 00 b0 43..braes.... +| 4064: 03 01 68 03 01 03 00 04 03 67 65 74 03 01 04 00 ..h......get.... +| 4080: 06 01 72 03 01 05 00 05 03 69 6e 67 03 01 06 00 ..r......ing.... +| page 5 offset 16384 +| 0: 0a 00 00 00 04 0f e7 00 0f fb 0f f5 0f ee 0f e7 ................ +| 4064: 00 00 00 00 00 00 00 06 04 08 01 01 03 04 06 04 ................ +| 4080: 08 01 01 02 03 05 04 08 09 01 02 04 04 08 08 09 ................ +| end sql038051.txt.db +}]} {} + +do_catchsql_test 48.1 { + INSERT INTO x1(x1) VALUES('nodesize=24'),('merge=3,4'); + INSERT INTO x1(x1) VALUES( 'merge=3,4' ),('merge=3,4'); +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +# +reset_db +do_test 49.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 28672 pagesize 4096 filename crash-58821b8eae6883.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 07 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 06 00 00 00 04 ................ +| 96: 00 00 00 00 0d 0e ef 00 07 0d 4d 00 0f bd 0f 5f ..........M...._ +| 112: 0e f7 0e 06 0e bc 0d a4 0d 4d 00 00 00 00 00 00 .........M...... +| 3392: 00 00 00 00 00 00 00 00 00 00 00 00 00 55 07 07 .............U.. +| 3408: 17 1b 1b 01 81 01 74 61 62 6c 65 74 31 5f 73 74 ......tablet1_st +| 3424: 61 74 74 31 5f 73 74 61 74 07 43 52 45 41 54 45 att1_stat.CREATE +| 3440: 20 54 41 42 4c 45 20 27 74 31 5f 73 74 61 74 27 TABLE 't1_stat' +| 3456: 28 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d (id INTEGER PRIM +| 3472: 41 52 59 20 4b 45 59 2c 20 76 61 6c 75 65 20 42 ARY KEY, value B +| 3488: 4c 4f 42 29 60 06 07 17 21 21 01 81 0b 74 61 62 LOB)`...!!...tab +| 3504: 6c 65 74 31 5f 64 6f 63 73 69 7a 65 74 31 5f 64 let1_docsizet1_d +| 3520: 6f 63 73 69 7a 65 06 43 52 45 41 54 45 20 54 41 ocsize.CREATE TA +| 3536: 42 4c 45 20 27 74 31 5f 64 6f 63 73 69 7a 65 27 BLE 't1_docsize' +| 3552: 28 64 6f 63 69 64 20 49 4e 54 45 47 45 52 20 50 (docid INTEGER P +| 3568: 52 49 4d 41 52 59 20 4b 45 59 2c 20 73 69 7a 65 RIMARY KEY, size +| 3584: 20 42 4c 4f 42 29 81 33 04 07 17 1f 1f 01 82 35 BLOB).3.......5 +| 3600: 74 61 62 6c 65 74 31 5f 73 65 67 64 69 72 74 31 tablet1_segdirt1 +| 3616: 5f 73 65 67 64 69 72 04 43 52 45 41 54 45 20 54 _segdir.CREATE T +| 3632: 41 42 4c 45 20 27 74 31 5f 73 65 67 64 69 72 27 ABLE 't1_segdir' +| 3648: 28 6c 65 76 65 6c 20 49 4e 54 45 47 45 52 2c 69 (level INTEGER,i +| 3664: 64 78 20 49 4e 54 45 47 45 52 2c 73 74 61 72 74 dx INTEGER,start +| 3680: 5f 62 6c 6f 63 6b 20 49 4e 54 45 47 45 52 2c 6c _block INTEGER,l +| 3696: 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 6b 20 eaves_end_block +| 3712: 49 4e 54 45 47 45 52 2c 65 6e 64 5f 62 6c 6f 63 INTEGER,end_bloc +| 3728: 6b 20 49 4e 54 45 47 45 52 2c 72 6f 6f 74 20 42 k INTEGER,root B +| 3744: 4c 4f 42 2c 50 52 49 4d 41 52 59 20 4b 45 59 28 LOB,PRIMARY KEY( +| 3760: 6c 65 76 65 6c 2c 20 69 64 78 29 29 31 05 06 17 level, idx))1... +| 3776: 45 1f 01 00 69 6e 64 65 78 73 71 6c 69 74 65 5f E...indexsqlite_ +| 3792: 61 75 74 6f 69 6e 64 65 78 5f 74 31 5f 73 65 67 autoindex_t1_seg +| 3808: 64 69 72 5f 31 74 31 5f 73 65 67 64 69 72 05 00 dir_1t1_segdir.. +| 3824: 00 00 08 00 00 00 00 66 03 07 17 23 23 01 81 13 .......f...##... +| 3840: 74 61 62 6c 65 74 31 5f 73 65 67 6d 65 6e 74 73 tablet1_segments +| 3856: 74 31 5f 73 65 67 6d 65 6e 74 73 03 43 52 45 41 t1_segments.CREA +| 3872: 54 45 20 54 41 42 4c 45 20 27 74 31 5f 73 65 67 TE TABLE 't1_seg +| 3888: 6d 65 6e 74 73 27 28 62 6c 6f 63 6b 69 64 20 49 ments'(blockid I +| 3904: 4e 54 45 47 45 52 20 f9 52 49 4d 41 52 59 20 4b NTEGER .RIMARY K +| 3920: 45 59 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 29 5c EY, block BLOB). +| 3936: 02 07 17 21 21 01 81 03 74 61 62 6c 65 74 31 5f ...!!...tablet1_ +| 3952: 63 6f 6e 74 65 6e 74 74 31 5f 63 6f 6e 74 65 6e contentt1_conten +| 3968: 74 02 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 t.CREATE TABLE ' +| 3984: 74 31 5f 63 6f 6e 74 65 6e 74 27 28 64 6f 63 69 t1_content'(doci +| 4000: 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 d INTEGER PRIMAR +| 4016: 59 20 4b 45 59 2c 20 27 63 30 61 27 29 41 01 06 Y KEY, 'c0a')A.. +| 4032: 17 11 11 08 71 74 61 62 6c 65 74 31 74 31 43 52 ....qtablet1t1CR +| 4048: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4064: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 34 LE t1 USING fts4 +| 4080: 28 61 2c 70 72 65 66 69 78 3d 27 32 2c 32 27 29 (a,prefix='2,2') +| page 2 offset 4096 +| 0: 0d 00 00 00 08 0e 1f 00 0f c4 0f 7c 0f 34 0f 07 ...........|.4.. +| 16: 0e c3 0e 97 0e 00 00 00 00 00 00 00 00 00 00 00 ................ +| 3600: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 42 ...............B +| 3616: 08 04 00 81 09 73 75 6e 74 20 69 6e 20 63 75 6c .....sunt in cul +| 3632: 70 61 20 71 75 69 20 6f 66 66 69 63 69 61 20 64 pa qui officia d +| 3648: 65 73 65 72 75 6e 74 20 6d 6f 6c 6c 69 74 20 61 eserunt mollit a +| 3664: 6e 69 6d 20 69 64 20 65 73 74 20 6c 61 62 6f 72 nim id est labor +| 3680: 75 6d 2e 32 07 03 00 6b 45 78 63 65 70 74 65 75 um.2...kExcepteu +| 3696: 72 20 73 69 6e 74 20 6f 63 63 61 65 63 61 74 20 r sint occaecat +| 3712: 63 75 70 69 64 61 74 61 74 20 6e 6f 6e 20 70 72 cupidatat non pr +| 3728: 6f 69 64 65 6e 74 2c 2a 06 03 00 5b 63 69 6c 6c oident,*...[cill +| 3744: 75 6d 20 64 6f 6c 6f 72 65 20 65 75 20 66 75 67 um dolore eu fug +| 3760: 69 61 74 20 6e 75 6c 6c 61 20 70 61 72 69 61 74 iat nulla pariat +| 3776: 75 72 2e 42 05 04 00 81 09 44 75 69 73 20 61 75 ur.B.....Duis au +| 3792: 74 65 20 69 72 75 72 65 20 64 6f 6c 6f 72 20 69 te irure dolor i +| 3808: 6e 20 72 65 70 72 65 68 65 6e 64 65 72 69 74 20 n reprehenderit +| 3824: 69 6e 20 76 6f 6c 75 70 74 61 74 65 20 76 65 6c in voluptate vel +| 3840: 69 74 20 65 73 73 65 2b 04 03 00 5d 6e 69 73 69 it esse+...]nisi +| 3856: 20 75 74 20 61 6c 69 71 75 69 70 20 65 78 20 65 ut aliquip ex e +| 3872: 61 20 63 6f 6d 6d 6f 64 6f 20 63 6f 6e 73 65 71 a commodo conseq +| 3888: 75 61 74 2e 46 03 04 00 81 11 55 74 20 65 6e 69 uat.F.....Ut eni +| 3904: 6d 20 61 64 20 6d 69 6e 69 6d 20 76 65 6e 69 61 m ad minim venia +| 3920: 6d 2c 20 71 75 69 73 20 6e 6f 73 74 72 75 64 20 m, quis nostrud +| 3936: 65 78 65 72 63 69 74 61 74 69 6f 6e 20 75 6c 6c exercitation ull +| 3952: 61 6d 63 6f 20 6c 61 62 6f 72 69 73 46 02 04 00 amco laborisF... +| 3968: 81 11 73 65 64 20 64 6f 20 65 69 75 73 6d 6f 64 ..sed do eiusmod +| 3984: 20 74 65 6d 70 6f 72 20 69 6e 63 69 64 69 64 75 tempor incididu +| 4000: 6e 74 20 75 74 20 6c 61 62 6f 72 65 20 65 74 20 nt ut labore et +| 4016: 64 6f 6c 6f 72 65 20 6d 61 67 6e 61 20 61 6c 69 dolore magna ali +| 4032: 71 75 61 2e 3a 01 03 00 7b 4c 6f 72 65 6d 20 69 qua.:....Lorem i +| 4048: 70 73 75 6d 20 64 6f 6c 6f 72 20 73 69 74 20 61 psum dolor sit a +| 4064: 6d 65 74 2c 20 63 6f 6e 73 65 63 74 65 74 75 72 met, consectetur +| 4080: 20 61 64 69 70 69 73 63 69 6e 67 20 65 00 01 00 adipiscing e... +| page 4 offset 12288 +| 0: 0d 00 00 00 03 0a a6 00 0d 57 0c 4a 0a a6 00 00 .........W.J.... +| 2720: 00 00 00 00 00 00 83 21 03 08 02 08 08 08 17 86 .......!........ +| 2736: 30 08 00 30 20 34 30 32 00 02 61 64 06 01 08 00 0..0 402..ad.... +| 2752: 02 04 00 01 01 6c 06 02 0c 00 02 04 00 01 01 6d .....l.........m +| 2768: 03 01 06 00 01 01 6e 03 08 09 00 01 01 75 03 05 ......n......u.. +| 2784: 03 00 00 02 63 69 03 06 02 00 01 01 6f 07 01 07 ....ci......o... +| 2800: 00 03 07 03 00 01 01 75 06 07 05 00 01 04 00 00 .......u........ +| 2816: 02 64 65 03 08 07 00 01 01 6f 0d 01 04 00 01 03 .de......o...... +| 2832: 09 00 03 05 00 01 03 00 01 01 75 03 05 02 00 00 ..........u..... +| 2848: 02 65 61 03 04 06 00 01 01 69 03 02 04 00 01 01 .ea......i...... +| 2864: 6c 03 01 09 00 01 01 6e 03 03 03 00 01 01 73 06 l......n......s. +| 2880: 05 0b 00 03 0b 00 01 01 74 03 02 09 00 01 01 75 ........t......u +| 2896: 03 06 04 00 01 01 78 09 03 09 00 01 05 00 03 02 ......x......... +| 2912: 00 00 02 66 75 03 06 05 00 00 02 69 64 03 08 0a ...fu......id... +| 2928: 00 01 01 6e 0a 02 06 00 03 06 04 00 03 03 00 01 ...n............ +| 2944: 01 70 03 01 03 00 01 01 72 03 05 04 00 00 02 6c .p......r......l +| 2960: 61 09 02 08 00 01 0b 00 05 0c 00 01 01 6f 03 01 a............o.. +| 2976: 02 00 00 02 6d 61 03 02 0b 00 01 01 69 03 03 05 ....ma......i... +| 2992: 00 01 01 6f 03 08 08 00 00 02 6e 69 03 04 02 00 ...o......ni.... +| 3008: 01 01 6f 06 03 08 00 04 06 00 01 01 75 03 06 06 ..o.........u... +| 3024: 00 00 02 6f 63 03 07 04 00 01 01 66 03 08 06 00 ...oc......f.... +| 3040: 00 02 70 61 03 06 07 00 01 01 72 03 07 07 00 00 ..pa......r..... +| 3056: 02 71 75 06 03 07 00 05 05 00 00 02 72 65 03 05 .qu.........re.. +| 3072: 07 00 00 02 73 65 03 02 02 00 01 01 69 06 01 05 ....se......i... +| 3088: 00 06 03 00 01 01 75 03 08 02 00 00 02 74 65 03 ......u......te. +| 3104: 02 05 00 00 02 75 6c 03 03 0a 00 01 01 74 09 02 .....ul......t.. +| 3120: 07 00 01 02 00 01 03 00 00 02 76 65 06 03 06 00 ..........ve.... +| 3136: 02 0a 00 01 01 6f 03 05 09 00 82 0a 02 08 02 08 .....o.......... +| 3152: 08 08 17 84 02 04 00 30 20 32 35 31 00 01 61 13 .......0 251..a. +| 3168: 01 06 04 00 01 0c 00 01 04 00 01 04 00 01 03 00 ................ +| 3184: 03 09 00 00 01 63 10 01 07 00 03 07 03 00 02 02 .....c.......... +| 3200: 00 01 05 00 01 04 00 00 01 64 11 01 04 00 01 03 .........d...... +| 3216: 09 00 03 02 05 00 01 03 00 02 07 00 00 01 65 1b ..............e. +| 3232: 01 09 00 01 04 07 00 01 03 08 00 01 05 03 00 01 ................ +| 3248: 0b 00 01 04 00 01 02 00 01 0b 00 00 01 66 03 06 .............f.. +| 3264: 05 00 00 01 69 0f 01 03 00 01 06 00 03 04 04 04 ....i........... +| 3280: 00 03 03 09 00 00 01 6c 0c 01 02 00 01 08 00 01 .......l........ +| 3296: 0b 00 05 0c 00 00 01 6d 09 02 0b 00 01 05 00 05 .......m........ +| 3312: 08 00 00 01 6e 0c 03 08 00 01 02 00 02 06 00 01 ....n........... +| 3328: 06 00 00 01 6f 06 07 04 00 01 06 00 00 01 70 06 ....o.........p. +| 3344: 06 07 00 01 07 00 00 01 71 06 03 07 00 05 05 00 ........q....... +| 3360: 00 01 72 03 05 07 00 00 01 73 0c 01 05 00 01 02 ..r......s...... +| 3376: 00 05 03 00 01 02 00 00 01 74 03 02 05 00 00 01 .........t...... +| 3392: 75 0a 02 07 00 01 02 0a 00 01 03 00 00 01 76 07 u.............v. +| 3408: 03 06 00 02 09 03 00 85 26 01 08 08 08 08 08 17 ........&....... +| 3424: 8a 3e 30 20 36 36 35 00 02 61 65 03 03 04 00 02 .>0 665..ae..... +| 3440: 08 69 70 69 73 63 69 6e 67 03 01 08 00 01 05 6c .ipiscing......l +| 3456: 69 71 75 61 03 02 0c 00 05 02 69 70 03 04 04 00 iqua......ip.... +| 3472: 01 03 6d 65 74 03 01 06 00 01 03 6e 69 6d 03 08 ..met......nim.. +| 3488: 09 00 01 03 75 74 65 03 05 03 00 00 06 63 69 6c ....ute......cil +| 3504: 6c 75 6d 03 06 02 00 01 06 6f 6d 6d 6f 64 6f 03 lum......ommodo. +| 3520: 04 07 00 02 09 6e 73 65 63 74 65 74 b5 72 03 01 .....nsectet.r.. +| 3536: 07 00 05 04 71 75 61 74 03 04 08 00 01 04 75 6c ....quat......ul +| 3552: 70 61 03 08 04 00 02 07 70 69 64 61 74 61 74 03 pa......pidatat. +| 3568: 07 05 00 00 08 64 65 73 65 72 75 6e 74 03 08 07 .....deserunt... +| 3584: 00 01 01 6f 03 02 03 00 02 03 6c 6f 72 06 01 04 ...o......lor... +| 3600: 00 04 05 00 05 01 65 06 02 0a 00 04 03 00 01 03 ......e......... +| 3616: 75 69 73 03 05 02 00 00 02 65 61 03 04 06 00 01 uis......ea..... +| 3632: 06 69 75 73 6d 6f 64 03 02 04 00 01 03 6c 69 74 .iusmod......lit +| 3648: 03 01 09 00 01 03 6e 69 6d 03 03 03 00 01 03 73 ......nim......s +| 3664: 73 65 03 05 0b 00 02 01 74 03 08 0b 00 01 01 74 se......t......t +| 3680: 03 02 09 00 01 01 75 03 06 04 00 01 01 78 03 04 ......u......x.. +| 3696: 05 00 02 07 63 65 70 74 65 75 72 03 07 02 00 02 ....cepteur..... +| 3712: 0a 65 72 63 69 74 61 74 69 6f 6e 03 03 09 00 00 .ercitation..... +| 3728: 06 66 75 67 69 61 74 03 06 05 00 00 02 69 64 03 .fugiat......id. +| 3744: 08 0a 00 01 01 6e 07 05 06 04 00 03 03 00 02 08 .....n.......... +| 3760: 63 69 64 69 64 75 6e 74 03 02 06 00 01 04 70 73 cididunt......ps +| 3776: 75 6d 03 01 03 00 01 04 72 75 72 65 03 05 04 00 um......rure.... +| 3792: 00 06 6c 61 62 6f 72 65 03 02 08 00 05 02 69 73 ..labore......is +| 3808: 03 03 0b 00 05 02 75 6d 03 08 0c 00 01 04 6f 72 ......um......or +| 3824: 65 6d 03 01 02 00 00 05 6d 61 67 6e 61 03 02 0b em......magna... +| 3840: 00 01 04 69 6e 69 6d 03 03 05 00 01 05 6f 6c 6c ...inim......oll +| 3856: 69 74 03 08 08 00 00 04 6e 69 73 69 03 04 02 00 it......nisi.... +| 3872: 01 02 6f 6e 03 07 06 00 02 05 73 74 72 75 64 03 ..on......strud. +| 3888: 03 08 00 01 04 75 6c 6c 61 03 06 06 00 00 08 6f .....ulla......o +| 3904: 63 63 61 65 63 61 74 03 07 04 00 01 06 66 66 69 ccaecat......ffi +| 3920: 63 69 61 03 08 06 00 00 08 70 61 72 69 61 74 75 cia......pariatu +| 3936: 72 03 06 07 00 01 07 72 6f 69 64 65 6e 74 03 07 r......roident.. +| 3952: 07 00 00 03 71 75 69 03 08 05 00 03 01 73 03 03 ....qui......s.. +| 3968: 07 00 00 0d 72 65 70 72 65 68 65 6e 64 65 72 69 ....reprehenderi +| 3984: 74 03 05 07 00 00 03 73 65 64 03 02 02 00 01 03 t......sed...... +| 4000: 69 6e 74 03 07 03 00 02 01 74 03 01 05 00 01 03 int......t...... +| 4016: 75 6e 74 03 08 02 00 00 06 74 65 6d 70 6f 72 03 unt......tempor. +| 4032: 02 05 00 00 07 75 6c 6c 61 6d 63 6f 03 03 0a 00 .....ullamco.... +| 4048: 01 01 74 09 02 07 00 01 02 00 01 03 00 00 05 76 ..t............v +| 4064: 65 6c 69 74 03 05 0a 00 02 04 6e 69 61 6d 03 03 elit......niam.. +| 4080: 06 00 01 08 6f 6c 75 70 74 61 74 65 03 05 09 00 ....oluptate.... +| page 5 offset 16384 +| 0: 0a 00 00 00 03 0f eb 00 0f fb 0f f3 0f eb 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 07 04 02 08 01 ................ +| 4080: 08 00 03 07 04 02 08 01 04 00 02 04 04 08 08 09 ................ +| page 6 offset 20480 +| 0: 0d 00 00 00 08 0f d0 00 0f fa 0f f4 0f ee 0f e8 ................ +| 16: 0f e2 0f dc 0f d6 0f d0 00 00 00 00 00 00 00 00 ................ +| 4048: 04 08 03 00 0e 0b 04 07 03 00 0e 06 04 06 03 00 ................ +| 4064: 0e 06 04 05 03 00 0e 0a 04 04 03 00 0e 07 04 03 ................ +| 4080: 03 00 0e 0a 04 02 03 00 0e 0b 04 01 03 00 0e 08 ................ +| page 7 offset 24576 +| 0: 0d 00 00 00 01 0f f7 00 0f f7 00 00 00 00 01 00 ................ +| 4080: 00 00 00 00 00 00 00 07 00 03 00 14 08 45 b5 03 .............E.. +| end crash-58821b8eae6883.db +}]} {} + +do_catchsql_test 49.1 { + SAVEPOINT one; + DELETE FROM t1 WHERE t1 MATCH 'c*'; + SELECT matchinfo(t1,'pcx') IS NULL FROM t1 WHERE t1 MATCH 'f*e*'; +} {0 0} + +#------------------------------------------------------------------------- +# +reset_db +do_test 50.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 28672 pagesize 4096 filename crash-14ab65782c9c45.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 07 .....@ ........ +| 32: 00 00 00 02 00 00 00 01 00 00 00 00 00 00 00 00 ................ +| 96: 00 00 00 00 0d 0e b1 00 06 0d a4 00 0f 8d 0f 21 ...............! +| 112: 0e b9 0d c8 0e 7e 0d a4 00 00 00 00 00 00 00 00 .....~.......... +| 3488: 00 00 00 00 22 07 06 17 11 11 01 31 74 61 62 6c ...........1tabl +| 3504: 65 74 32 74 32 07 43 52 45 41 54 45 20 54 41 42 et2t2.CREATE TAB +| 3520: 4c 45 20 74 32 28 78 29 81 33 05 07 17 1f 1f 01 LE t2(x).3...... +| 3536: 82 35 74 61 62 6c 65 74 31 5f 73 65 67 64 69 72 .5tablet1_segdir +| 3552: 74 31 5f 73 65 67 64 69 72 05 43 52 45 41 54 45 t1_segdir.CREATE +| 3568: 20 54 41 42 4c 45 20 27 74 31 5f 73 65 67 64 69 TABLE 't1_segdi +| 3584: 72 27 28 6c 65 76 65 6c 20 49 4e 54 45 47 45 52 r'(level INTEGER +| 3600: 2c 69 64 78 20 49 4e 54 45 47 45 52 2c 73 74 61 ,idx INTEGER,sta +| 3616: 72 74 5f 62 6c 6f 63 6b 20 49 4e 54 45 47 45 52 rt_block INTEGER +| 3632: 2c 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 ,leaves_end_bloc +| 3648: 6b 20 49 4e 54 45 47 45 52 2c 65 6e 64 5f 62 6c k INTEGER,end_bl +| 3664: 6f 63 6b 20 49 4e 54 45 47 45 52 2c 72 6f 6f 74 ock INTEGER,root +| 3680: 20 42 4c 4f 42 2c 50 52 49 4d 41 52 59 20 4b 45 BLOB,PRIMARY KE +| 3696: 59 28 6c 65 76 65 6c 2c 20 69 64 78 29 29 31 06 Y(level, idx))1. +| 3712: 06 17 45 1f 01 00 69 6e 64 65 78 73 71 6c 69 74 ..E...indexsqlit +| 3728: 65 5f 61 75 74 6f 69 6e 64 65 78 5f 74 31 5f 73 e_autoindex_t1_s +| 3744: 65 67 64 69 72 5f 31 74 31 5f 73 65 68 64 69 72 egdir_1t1_sehdir +| 3760: 06 0f c7 00 08 00 00 00 00 66 04 07 17 23 23 01 .........f...##. +| 3776: 81 13 74 61 62 6c 65 74 31 5f 73 65 67 6d 65 6e ..tablet1_segmen +| 3792: 74 73 74 31 5f 73 65 67 6d 65 6e 74 73 04 43 52 tst1_segments.CR +| 3808: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 73 EATE TABLE 't1_s +| 3824: 65 67 6d 65 6e 74 73 27 28 62 6c 6f 63 6b 69 64 egments'(blockid +| 3840: 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 INTEGER PRIMARY +| 3856: 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 KEY, block BLOB +| 3872: 29 6a 03 07 17 21 21 01 81 1f 74 61 62 6c 65 74 )j...!!...tablet +| 3888: 31 5f 63 6f 6e 74 65 6e 74 74 31 5f 63 6f 6e 74 1_contentt1_cont +| 3904: 65 6e 74 03 43 52 45 41 54 45 20 54 41 42 4c 45 ent.CREATE TABLE +| 3920: 20 27 74 31 5f 63 6f 6e 74 65 6e 74 27 28 64 6f 't1_content'(do +| 3936: 63 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d cid INTEGER PRIM +| 3952: 41 52 59 20 4b 45 59 2c 20 27 63 30 61 27 2c 20 ARY KEY, 'c0a', +| 3968: 27 62 31 62 27 2c 20 27 63 32 63 27 29 38 02 06 'b1b', 'c2c')8.. +| 3984: 17 11 11 08 5f 74 61 62 6c 65 74 31 74 31 43 52 ...._tablet1t1CR +| 4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 33 LE t1 USING fts3 +| 4032: 28 61 2c 62 2c 63 29 00 00 00 00 00 00 00 00 00 (a,b,c)......... +| page 3 offset 8192 +| 0: 0d 00 00 00 25 0b 48 00 0f d8 0f af 0f 86 0f 74 ....%.H........t +| 16: 0f 61 0f 4e 0f 2f 0f 0f 0e ef 0e d7 0e be 0e a5 .a.N./.......... +| 32: 0e 8d 0e 74 0e 5b 0e 40 0e 24 0e 08 0d ef 0d d5 ...t.[.@.$...... +| 48: 0d bb 0d a0 0d 84 0d 00 00 00 00 00 00 00 00 00 ................ +| 2880: 00 00 00 00 00 00 00 00 81 f9 25 06 00 82 7f 00 ..........%..... +| 2896: 00 43 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2e .COMPILER=gcc-5. +| 2912: 34 2e 30 20 32 30 31 36 30 36 30 39 20 44 45 42 4.0 20160609 DEB +| 2928: 55 47 20 45 4e 41 42 4c 45 20 44 42 53 54 41 54 UG ENABLE DBSTAT +| 2944: 20 56 54 41 42 20 45 4e 41 42 4c 45 20 46 54 53 VTAB ENABLE FTS +| 2960: 34 20 45 4e 41 42 4c 45 20 46 54 53 35 20 45 4e 4 ENABLE FTS5 EN +| 2976: 41 42 4c 45 20 47 45 4f 50 4f 4c 59 20 45 4e 41 ABLE GEOPOLY ENA +| 2992: 42 4c 45 20 4a 53 4f 4e 31 20 45 4e 41 42 4c 45 BLE JSON1 ENABLE +| 3008: 20 4d 45 4d 53 59 53 35 20 45 4e 41 42 4c 45 20 MEMSYS5 ENABLE +| 3024: 52 54 52 45 45 20 4d 41 58 20 4d 45 4d 4f 52 59 RTREE MAX MEMORY +| 3040: 3d 37 a0 30 30 30 30 f2 30 20 4f 4d 49 54 20 4c =7.0000.0 OMIT L +| 3056: 4f 41 44 20 45 58 54 45 4e 53 49 4f 4e 20 54 48 OAD EXTENSION TH +| 3072: 52 45 41 44 53 41 46 45 3d 30 18 24 05 00 25 0f READSAFE=0.$..%. +| 3088: 19 54 48 52 45 41 44 53 41 46 45 3d 30 58 42 49 .THREADSAFE=0XBI +| 3104: 4e 41 52 59 18 23 05 00 25 0f 19 54 48 52 45 41 NARY.#..%..THREA +| 3120: 44 53 41 46 45 3d 30 58 4e 4f 43 41 53 45 17 22 DSAFE=0XNOCASE.. +| 3136: 05 00 25 0f 17 54 48 52 45 41 54 53 41 46 45 3d ..%..THREATSAFE= +| 3152: 30 58 52 54 52 49 4d 1f 21 05 00 33 0f 19 4f 4d 0XRTRIM.!..3..OM +| 3168: 49 54 20 5c 4f 41 44 20 45 58 54 45 4e 53 49 4f IT .OAD EXTENSIO +| 3184: 4e 58 42 49 4e 41 52 59 1f 20 05 00 33 0f 19 4f NXBINARY. ..3..O +| 3200: 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 MIT LOAD EXTENSI +| 3216: 4f 4e 58 4e 4f 43 41 53 46 1e 1f 05 00 33 0f 17 ONXNOCASF....3.. +| 3232: 4f 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 OMIT LOAD EXTENS +| 3248: 49 4f 4e 58 52 44 52 49 4d 1f 1e 05 00 33 0f 19 IONXRDRIM....3.. +| 3264: 4d 41 68 20 4d 45 4d 4f 52 59 3d 35 30 30 30 30 MAh MEMORY=50000 +| 3280: 30 30 30 58 42 49 4e 41 52 59 1f 1d 05 00 33 0f 000XBINARY....3. +| 3296: 19 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 .MAX MEMORY=5000 +| 3312: 30 30 30 30 58 4e 4f 43 41 53 45 1e 1c 05 00 33 0000XNOCASE....3 +| 3328: 0f 16 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 ..MAX MEMORY=500 +| 3344: 30 30 30 30 30 58 52 54 52 49 4d 18 1b 05 00 25 00000XRTRIM....% +| 3360: 0f 19 45 4e 41 42 4c 45 20 52 54 52 45 45 58 42 ..ENABLE RTREEXB +| 3376: 49 4e 41 52 59 18 1a 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3392: 4c 45 20 52 54 52 45 45 58 4e 4f 43 41 53 45 17 LE RTREEXNOCASE. +| 3408: 19 05 00 25 0f 17 45 4e 41 42 4c 45 20 52 54 52 ...%..ENABLE RTR +| 3424: 45 45 58 52 54 52 49 4d 1a 18 05 00 29 0f 19 45 EEXRTRIM....)..E +| 3440: 4e 41 42 4c 45 20 4d 45 43 53 59 53 35 58 42 49 NABLE MECSYS5XBI +| 3456: 4e 41 52 59 1a 17 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3472: 45 20 4d 45 4d 53 59 53 35 58 4e 4f 43 41 53 45 E MEMSYS5XNOCASE +| 3488: 19 16 05 00 29 0f 17 45 4e 41 42 4c 45 20 4d 45 ....)..ENABLE ME +| 3504: 4d 53 59 53 35 58 52 54 52 49 4d 18 15 05 00 25 MSYS5XRTRIM....% +| 3520: 0f 19 45 4e 41 42 4c 45 20 4a 53 4f 4e 31 58 42 ..ENABLE JSON1XB +| 3536: 49 4e 41 52 59 18 14 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3552: 4c 45 20 4a 53 4f 4e 31 58 4e 4f 43 41 53 45 17 LE JSON1XNOCASE. +| 3568: 13 05 00 25 0f 17 45 4e 41 42 4c 45 20 4a 53 4f ...%..ENABLE JSO +| 3584: 4e 31 58 52 54 52 49 4d 1a 12 05 00 29 0f 19 45 N1XRTRIM....)..E +| 3600: 4e 41 42 4c 45 20 47 45 4f 50 4f 4c 59 58 42 49 NABLE GEOPOLYXBI +| 3616: 4e 41 52 59 1a 11 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3632: 45 20 47 45 4f 50 4f 4c 59 58 4e 4f 43 41 53 45 E GEOPOLYXNOCASE +| 3648: 19 10 05 00 29 0f 17 45 4e 41 42 4c 45 20 47 45 ....)..ENABLE GE +| 3664: 4f 50 4f 4c 59 58 52 54 52 49 4d 17 0f 05 00 23 OPOLYXRTRIM....# +| 3680: 0f 19 45 4e 41 42 4c 45 20 46 54 53 35 58 42 49 ..ENABLE FTS5XBI +| 3696: 4e 41 52 59 17 0e 05 00 23 0f 19 45 4e 41 42 4c NARY....#..ENABL +| 3712: 45 20 46 54 53 35 58 4e 4f 43 41 53 45 16 0d 05 E FTS5XNOCASE... +| 3728: 00 23 0f 17 45 4e 41 42 4c 45 20 46 54 53 35 58 .#..ENABLE FTS5X +| 3744: 52 54 52 49 4d 17 0c 05 00 23 0f 19 45 4e 41 42 RTRIM....#..ENAB +| 3760: 4c 45 20 46 54 53 34 58 42 49 4e 41 52 59 17 0b LE FTS4XBINARY.. +| 3776: 05 00 23 0f 19 45 4e 41 42 4c 45 20 46 54 53 34 ..#..ENABLE FTS4 +| 3792: 58 4e 4f 43 41 53 45 16 0a 05 00 23 0f 17 45 4e XNOCASE....#..EN +| 3808: 41 42 4c 46 20 46 54 53 34 58 52 54 52 49 4d 1e ABLF FTS4XRTRIM. +| 3824: 09 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3840: 54 41 54 20 56 54 41 42 58 42 49 4e 41 52 59 1e TAT VTABXBINARY. +| 3856: 08 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3872: 55 41 54 20 56 54 41 42 58 4e 4f 43 41 53 45 1d UAT VTABXNOCASE. +| 3888: 07 05 00 31 0f 17 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3904: 54 41 54 20 56 54 41 42 58 52 54 52 49 4d 11 06 TAT VTABXRTRIM.. +| 3920: 05 00 17 0f 19 44 45 42 55 47 58 42 49 4e 41 52 .....DEBUGXBINAR +| 3936: 59 11 05 05 00 17 0f 19 44 45 42 55 47 58 4e 4f Y.......DEBUGXNO +| 3952: 43 41 53 45 10 04 05 00 17 0f 17 44 45 42 55 47 CASE.......DEBUG +| 3968: 58 52 54 52 49 4d 27 03 05 00 43 0f 19 43 4f 4d XRTRIM'...C..COM +| 3984: 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e 30 20 PILER=gcc-5.4.0 +| 4000: 32 30 31 36 30 36 30 39 58 42 49 4e 41 52 59 27 20160609XBINARY' +| 4016: 02 05 00 43 0f 19 43 4f 4d 50 49 4c 45 52 3d 67 ...C..COMPILER=g +| 4032: 63 63 2d 35 2e 34 2e 30 20 32 30 31 36 30 36 30 cc-5.4.0 2016060 +| 4048: 39 58 4e 4f 43 41 53 45 00 00 00 00 00 00 00 00 9XNOCASE........ +| page 5 offset 16384 +| 0: 0d 00 00 00 02 0b a0 00 0c ad 0b a0 00 00 00 00 ................ +| 2976: 82 0a 02 08 08 09 08 08 17 84 06 30 20 32 35 33 ...........0 253 +| 2992: 00 01 30 04 25 06 1b 00 00 08 32 30 31 36 30 36 ..0.%.....201606 +| 3008: 30 39 03 25 07 00 00 01 34 03 25 05 00 00 01 35 09.%....4.%....5 +| 3024: 03 25 04 00 01 07 30 30 30 30 30 30 30 03 25 1a .%....0000000.%. +| 3040: 00 00 08 63 6f 6d 70 69 6c 65 72 03 25 02 00 00 ...compiler.%... +| 3056: 06 64 62 73 74 61 74 03 25 0a 00 01 04 65 62 75 .dbstat.%....ebu +| 3072: 67 03 25 08 00 00 06 65 6e 61 62 6c 65 09 25 09 g.%....enable.%. +| 3088: 05 04 04 04 04 04 00 01 08 78 74 65 6e 73 69 6f .........xtensio +| 3104: 6e 03 25 1d 00 00 04 66 74 73 34 03 25 0d 00 03 n.%....fts4.%... +| 3120: 01 35 03 25 0f 00 00 03 67 63 63 03 25 03 00 01 .5.%....gcc.%... +| 3136: 06 65 6f 70 6f 6c 79 03 25 11 00 00 05 6a 73 6f .eopoly.%....jso +| 3152: 6e 31 03 25 13 00 00 04 6c 6f 61 64 03 25 1c 00 n1.%....load.%.. +| 3168: 00 03 6d 61 78 03 25 18 00 01 05 65 6d 6f 72 79 ..max.%....emory +| 3184: 03 25 19 00 03 04 73 79 73 35 03 25 15 00 00 04 .%....sys5.%.... +| 3200: 6f 6d 69 74 03 25 1b 00 00 05 72 74 72 65 65 03 omit.%....rtree. +| 3216: 25 17 00 00 0a 74 68 72 65 61 64 73 61 66 65 03 %....threadsafe. +| 3232: 25 1e 00 00 04 76 74 61 62 03 25 0b 00 86 50 01 %....vtab.%...P. +| 3248: 08 08 08 08 08 17 8d 12 30 20 38 33 35 00 01 30 ........0 835..0 +| 3264: 12 01 06 00 01 06 00 01 06 00 1f 03 00 01 03 00 ................ +| 3280: 01 03 00 00 08 32 30 31 36 30 36 30 39 09 01 07 .....20160609... +| 3296: 00 01 07 00 01 07 00 00 01 34 09 01 05 00 01 05 .........4...... +| 3312: 00 01 05 00 00 01 35 09 01 04 00 01 04 00 01 04 ......5......... +| 3328: 00 01 07 30 30 30 30 30 30 30 09 1c 04 00 01 04 ...0000000...... +| 3344: 00 01 04 00 00 06 62 69 6e 61 72 79 3c 03 01 02 ......binary<... +| 3360: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3376: 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 ................ +| 3392: 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03 ................ +| 3408: 01 02 02 00 03 01 02 02 00 00 08 63 6f 38 70 69 ...........co8pi +| 3424: 6c 65 72 09 01 02 00 01 02 00 01 02 00 00 06 64 ler............d +| 3440: 52 73 74 61 74 09 07 03 00 01 03 00 01 03 00 01 Rstat........... +| 3456: 04 65 62 75 67 09 04 02 00 01 02 00 01 02 00 00 .ebug........... +| 3472: 06 65 6e 61 62 6c 65 3f 07 02 00 01 02 00 01 01 .enable?........ +| 3488: ff f1 02 00 01 02 00 01 02 00 01 02 00 01 02 00 ................ +| 3504: 01 02 00 01 03 00 01 01 00 02 02 00 01 02 00 01 ................ +| 3520: 02 00 01 02 00 01 12 00 01 02 00 01 02 00 01 02 ................ +| 3536: 00 01 02 00 01 02 00 01 08 78 74 65 6e 73 69 6f .........xtensio +| 3552: 6e 09 1f 04 00 01 04 00 01 04 00 00 04 66 74 73 n............fts +| 3568: 34 09 0a 03 00 01 03 00 01 04 00 03 01 35 09 0d 4............5.. +| 3584: 03 00 01 03 00 01 03 00 00 03 67 63 63 09 01 03 ..........gcc... +| 3600: 00 01 03 0b 01 13 00 01 06 65 6f 70 6f 6c 79 09 .........eopoly. +| 3616: 10 03 00 01 03 00 01 03 00 00 05 6a 83 6f 6e 31 ...........j.on1 +| 3632: 09 13 03 00 01 03 00 01 03 00 00 04 6c 6f 61 64 ............load +| 3648: 09 1f 03 00 01 03 00 01 03 00 00 03 6d 61 78 09 ............max. +| 3664: 1c 02 00 01 02 00 01 02 00 01 05 65 6d 6f 72 79 ...........emory +| 3680: 09 1c 03 00 01 03 00 01 03 00 03 04 73 79 73 35 ............sys5 +| 3696: 09 16 03 00 01 03 00 01 03 00 00 06 6e 6f 63 61 ............noca +| 3712: 73 65 3c 02 01 02 02 00 03 01 02 02 00 03 01 02 se<............. +| 3728: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3744: 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 ................ +| 3760: 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 00 ................ +| 3776: 04 6f 6d 69 74 09 1f 02 00 01 02 00 01 02 00 00 .omit........... +| 3792: 05 72 74 72 65 65 09 19 03 00 01 03 00 01 03 00 .rtree.......... +| 3808: 03 02 69 6d 3c 01 01 02 02 00 03 01 02 02 00 03 ..im<........... +| 3824: 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01 ................ +| 3840: 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 ................ +| 3856: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3872: 00 00 0a 74 68 72 65 61 64 73 61 66 65 09 22 02 ...threadsafe... +| 3888: 00 01 02 00 01 02 00 00 04 76 74 61 62 09 07 04 .........vtab... +| 3904: 00 01 04 00 01 04 00 00 01 78 b4 01 01 01 01 02 .........x...... +| 3920: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| 3936: 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 ................ +| 3952: 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 ................ +| 3968: 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 ................ +| 3984: 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 ................ +| 4000: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| 4016: 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 ................ +| 4032: 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 ................ +| 4048: 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 ................ +| 4064: 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 ................ +| 4080: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 02 0f f5 00 0f fb 0f f5 00 00 00 00 ................ +| 4080: 00 00 00 00 00 05 04 08 09 01 02 04 04 08 08 09 ................ +| page 7 offset 24576 +| 0: 0d 00 00 00 05 0f b8 00 0f f4 0f 00 00 00 00 00 ................ +| 4016: 00 00 00 00 00 00 00 00 0d 05 02 23 61 75 74 6f ...........#auto +| 4032: 6d 65 72 67 65 3d 35 0d 04 02 23 6d 65 72 67 65 merge=5...#merge +| 4048: 3d 31 30 30 2c 38 11 03 02 2b 69 6e 74 65 67 72 =100,8...+integr +| 4064: 69 74 79 2d 63 68 65 63 6b 09 02 02 00 00 00 00 ity-check....... +| end crash-14ab65782c9c45.db +}]} {} + +do_execsql_test 50.1 { + SELECT NULL FROM t1 WHERE t1 MATCH '"^enable"' +} { + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} +} + +#------------------------------------------------------------------------- +# +reset_db +do_test 51.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 28672 pagesize 4096 filename crash-11cf359576eb28.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 07 .....@ ........ +| 32: 00 00 00 02 00 00 00 01 00 00 00 07 00 00 00 04 ................ +| 96: 00 00 00 00 0d 0e b1 00 06 0d a4 00 0f 8d 0f 21 ...............! +| 112: 0e b9 0d c8 0e 7e 0d a4 0d a4 00 00 00 00 00 00 .....~.......... +| 3488: 00 00 00 00 22 07 06 17 11 11 01 31 74 61 62 6c ...........1tabl +| 3504: 65 74 32 74 32 07 43 52 45 41 54 45 20 54 41 42 et2t2.CREATE TAB +| 3520: 4c 45 20 74 32 28 78 29 81 33 05 07 17 1f 1f 01 LE t2(x).3...... +| 3536: 82 35 74 61 62 6c 65 74 31 5f 73 65 67 64 69 72 .5tablet1_segdir +| 3552: 74 31 5f 73 65 67 64 69 72 05 43 52 45 41 54 45 t1_segdir.CREATE +| 3568: 20 54 41 42 4c 45 20 27 74 31 5f 73 65 67 64 69 TABLE 't1_segdi +| 3584: 72 27 28 6c 65 76 65 6c 20 49 4e 54 45 47 45 52 r'(level INTEGER +| 3600: 2c 69 64 78 20 49 4e 54 45 47 45 52 2c 73 74 61 ,idx INTEGER,sta +| 3616: 72 74 5f 62 6c 6f 63 6b 20 49 4e 54 45 47 45 52 rt_block INTEGER +| 3632: 2c 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 ,leaves_end_bloc +| 3648: 6b 20 49 4e 54 45 47 45 52 2c 65 6e 64 5f 62 6c k INTEGER,end_bl +| 3664: 6f 63 6b 20 49 4e 54 45 47 45 52 2c 72 6f 6f 74 ock INTEGER,root +| 3680: 20 42 4c 4f 42 2c 50 52 49 4d 41 52 59 20 4b 45 BLOB,PRIMARY KE +| 3696: 59 28 6c 65 76 65 6c 2c 20 69 64 78 29 29 31 06 Y(level, idx))1. +| 3712: 06 17 45 1f 01 00 69 6e 64 65 78 73 71 6c 69 74 ..E...indexsqlit +| 3728: 65 5f 61 75 74 6f 69 6e 64 65 78 5f 74 31 5f 73 e_autoindex_t1_s +| 3744: 65 67 64 69 72 5f 31 74 31 5f 73 65 67 64 69 72 egdir_1t1_segdir +| 3760: 06 0f c7 00 08 00 00 00 00 66 04 07 17 23 23 01 .........f...##. +| 3776: 81 13 74 61 62 6c 65 74 31 5f 73 65 67 6d 65 6e ..tablet1_segmen +| 3792: 74 73 74 31 5f 73 65 67 6d 65 6e 74 73 04 43 52 tst1_segments.CR +| 3808: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 73 EATE TABLE 't1_s +| 3824: 65 67 6d 65 6e 74 73 27 28 62 6c 6f 63 6b 69 64 egments'(blockid +| 3840: 20 49 4e 54 45 47 45 52 20 50 51 49 4d 41 52 59 INTEGER PQIMARY +| 3856: 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 KEY, block BLOB +| 3872: 29 6a 03 07 17 21 21 01 81 1f 74 61 62 6c 65 74 )j...!!...tablet +| 3888: 31 5f 63 6f 6e 74 65 6e 74 74 31 5f 63 6f 6e 74 1_contentt1_cont +| 3904: 65 6e 74 03 43 52 45 41 54 45 20 54 41 42 4c 45 ent.CREATE TABLE +| 3920: 20 27 74 31 5f 63 6f 6e 74 65 6e 74 27 28 64 6f 't1_content'(do +| 3936: 63 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d cid INTEGER PRIM +| 3952: 41 52 59 20 4b 45 59 2c 20 27 63 30 61 27 2c 20 ARY KEY, 'c0a', +| 3968: 27 63 31 62 27 2c 20 27 63 32 63 27 29 38 02 06 'c1b', 'c2c')8.. +| 3984: 17 11 11 08 5f 74 61 62 6c 65 74 31 74 31 43 52 ...._tablet1t1CR +| 4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 33 LE t1 USING fts3 +| 4032: 28 61 2c 62 2c 63 29 00 00 00 00 00 00 00 00 00 (a,b,c)......... +| page 3 offset 8192 +| 0: 0d 00 00 00 25 0b 48 00 0f d8 0f af 0f 86 0f 74 ....%.H........t +| 16: 0f 61 0f 4e 0f 2f 0f 0f 0e ef 0e d7 0e be 0e a5 .a.N./.......... +| 32: 0e 8d 0e 74 0e 5b 0e 40 0e 24 0e 08 0d ef 0d d5 ...t.[.@.$...... +| 48: 0d bb 0d a0 0d 84 0d 68 0d 4f 0d 35 0d 1b 0c fb .......h.O.5.... +| 64: 0c da 0c b9 0c 99 0c 78 0c 57 0c 3e 0c 24 0c 0a .......x.W.>.$.. +| 80: 0b 48 00 00 00 00 00 00 00 00 00 00 00 00 00 00 .H.............. +| 2880: 00 00 00 00 00 00 00 00 81 3f 25 06 00 82 7f 00 .........?%..... +| 2896: 00 43 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2e .COMPILER=gcc-5. +| 2912: 34 2e 30 20 32 30 31 36 30 36 30 39 20 44 45 42 4.0 20160609 DEB +| 2928: 55 47 20 45 4e 41 42 4c 45 20 44 42 53 54 41 54 UG ENABLE DBSTAT +| 2944: 20 56 54 41 42 20 45 4e 41 42 4c 45 20 46 54 53 VTAB ENABLE FTS +| 2960: 34 20 45 4e 41 42 4c 45 20 46 54 53 35 20 45 4e 4 ENABLE FTS5 EN +| 2976: 41 42 4c 45 20 47 45 4f 59 0f 4c 59 20 45 4e 41 ABLE GEOY.LY ENA +| 2992: 42 4c 45 20 4a 53 4f 4e 31 20 45 4e 41 42 4c 45 BLE JSON1 ENABLE +| 3008: 20 4d 45 4d 53 59 53 35 20 45 4e 41 42 4c 45 20 MEMSYS5 ENABLE +| 3024: 52 54 52 45 45 20 4d 41 58 20 4d 45 4d 4f 52 59 RTREE MAX MEMORY +| 3040: 3d 35 30 30 30 30 30 30 30 20 4f 4d 49 54 20 4d =50000000 OMIT M +| 3056: 4f 41 44 20 45 58 54 45 4e 53 49 4f 4e 20 54 48 OAD EXTENSION TH +| 3072: 52 45 41 44 53 41 46 45 3d 30 18 24 05 00 25 0f READSAFE=0.$..%. +| 3088: 19 54 48 52 45 41 44 53 41 46 45 3d 30 58 42 49 .THREADSAFE=0XBI +| 3104: 4e 41 52 59 18 23 05 00 25 0f 19 54 48 52 45 41 NARY.#..%..THREA +| 3120: 44 53 41 46 45 3d 30 58 4e 4f 43 41 53 45 17 22 DSAFE=0XNOCASE.. +| 3136: 05 00 25 0f 17 54 48 52 45 41 44 53 41 46 45 3d ..%..THREADSAFE= +| 3152: 30 58 52 54 52 49 4d 1f 21 05 00 33 0f 19 4f 4d 0XRTRIM.!..3..OM +| 3168: 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 4f IT LOAD EXTENSIO +| 3184: 4e 58 42 49 4e 41 52 59 1f 20 05 00 33 0f 19 4f NXBINARY. ..3..O +| 3200: 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 MIT LOAD EXTENSI +| 3216: 4f 4e 58 4e 4f 43 41 53 45 1e 1f 05 00 33 0f 17 ONXNOCASE....3.. +| 3232: 4f 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 OMIT LOAD EXTENS +| 3248: 49 4f 4e 58 52 54 52 49 4d 1f 1e 05 00 33 0f 19 IONXRTRIM....3.. +| 3264: 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 30 MAX MEMORY=50000 +| 3280: 30 30 30 58 42 49 4e 41 52 59 1f 1d 05 00 33 0f 000XBINARY....3. +| 3296: 19 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 .MAX MEMORY=5000 +| 3312: 30 30 30 30 58 4e 4f 43 41 53 45 1e 1c 05 00 33 0000XNOCASE....3 +| 3328: 0f 17 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 ..MAX MEMORY=500 +| 3344: 30 30 30 30 30 58 52 54 52 49 4d 18 1b 05 00 25 00000XRTRIM....% +| 3360: 0f 19 45 4e 41 42 4c 45 20 52 54 52 45 45 58 42 ..ENABLE RTREEXB +| 3376: 49 4e 41 52 59 18 1a 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3392: 4c 45 20 52 54 52 45 45 58 4e 4f 43 41 53 45 17 LE RTREEXNOCASE. +| 3408: 19 05 00 25 0f 17 45 4e 41 42 4c 45 20 52 54 52 ...%..ENABLE RTR +| 3424: 45 45 58 52 54 52 49 4d 1a 18 05 00 29 0f 19 45 EEXRTRIM....)..E +| 3440: 4e 41 42 4c 45 20 4d 45 4d 53 59 53 35 58 42 49 NABLE MEMSYS5XBI +| 3456: 4e 41 52 59 1a 17 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3472: 45 20 4d 45 4d 53 59 53 35 58 4e 4f 43 41 53 45 E MEMSYS5XNOCASE +| 3488: 19 16 05 00 29 0f 17 45 4e 41 42 4c 45 20 4d 45 ....)..ENABLE ME +| 3504: 4d 53 59 53 35 58 52 54 52 49 4d 18 15 05 00 25 MSYS5XRTRIM....% +| 3520: 0f 19 45 4e 41 42 4c 45 20 4a 53 4f 4e 31 58 42 ..ENABLE JSON1XB +| 3536: 49 4e 41 52 59 18 14 05 00 25 0f 19 45 4e 41 42 INARY....%..ENAB +| 3552: 4c 45 20 4a 53 4f 4e 31 58 4e 4f 43 41 53 45 17 LE JSON1XNOCASE. +| 3568: 13 05 00 25 0f 17 45 4e 41 42 4c 45 20 4a 53 4f ...%..ENABLE JSO +| 3584: 4e 31 58 52 54 52 49 4d 1a 12 05 00 29 0f 19 45 N1XRTRIM....)..E +| 3600: 4e 41 42 4c 45 20 47 45 4f 50 4f 4c 59 58 42 49 NABLE GEOPOLYXBI +| 3616: 4e 41 52 59 1a 11 05 00 29 0f 19 45 4e 41 42 4c NARY....)..ENABL +| 3632: 45 20 47 45 4f 50 4f 4c 59 58 4e 4f 43 41 53 45 E GEOPOLYXNOCASE +| 3648: 19 10 05 00 29 0f 17 45 4e 41 42 4c 45 20 47 45 ....)..ENABLE GE +| 3664: 4f 50 4f 4c 59 58 52 54 52 49 4d 17 0f 05 00 23 OPOLYXRTRIM....# +| 3680: 0f 19 45 4e 41 42 4c 45 20 46 54 53 35 58 42 49 ..ENABLE FTS5XBI +| 3696: 4e 41 52 59 17 0e 05 00 23 0f 19 45 4e 41 42 4c NARY....#..ENABL +| 3712: 45 20 46 54 53 35 58 4e 4f 43 41 53 45 16 0d 05 E FTS5XNOCASE... +| 3728: 00 23 0f 17 45 4e 41 42 4c 45 20 46 54 53 35 58 .#..ENABLE FTS5X +| 3744: 52 54 52 49 4d 17 0c 05 00 23 0f 19 45 4e 41 42 RTRIM....#..ENAB +| 3760: 4c 45 20 46 54 53 34 58 42 49 4e 41 52 59 17 0b LE FTS4XBINARY.. +| 3776: 05 00 23 0f 19 45 4e 41 42 4c 45 20 46 54 53 34 ..#..ENABLE FTS4 +| 3792: 58 4e 4f 43 41 53 45 16 0a 05 00 23 0f 17 45 4e XNOCASE....#..EN +| 3808: 41 42 4c 45 20 46 54 53 34 58 52 54 52 49 4d 1e ABLE FTS4XRTRIM. +| 3824: 09 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3840: 54 41 54 20 56 54 41 42 58 42 49 4e 41 52 59 1e TAT VTABXBINARY. +| 3856: 08 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3872: 54 41 54 20 56 54 41 42 58 4e 4f 43 41 53 45 1d TAT VTABXNOCASE. +| 3888: 07 05 00 31 0f 17 45 4e 41 42 4c 45 20 44 42 53 ...1..ENABLE DBS +| 3904: 54 41 54 20 56 54 41 42 58 52 54 52 49 4d 11 06 TAT VTABXRTRIM.. +| 3920: 05 00 17 0f 19 44 45 42 55 47 58 42 49 4e 41 52 .....DEBUGXBINAR +| 3936: 59 11 05 05 00 17 0f 19 44 45 42 55 47 58 4e 4f Y.......DEBUGXNO +| 3952: 43 41 53 45 10 04 05 00 17 0f 17 44 45 42 55 47 CASE.......DEBUG +| 3968: 58 52 54 52 49 4d 27 03 05 00 43 10 19 43 4f 4d XRTRIM'...C..COM +| 3984: 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e 30 20 PILER=gcc-5.4.0 +| 4000: 32 30 31 36 30 36 30 39 58 42 49 4e 41 52 59 27 20160609XBINARY' +| 4016: 02 05 00 43 0f 19 43 4f 4d 50 49 4c 45 52 3d 67 ...C..COMPILER=g +| 4032: 63 63 2d 35 2e 34 2e 30 20 32 30 31 36 30 36 30 cc-5.4.0 2016060 +| 4048: 39 58 4e 4f 43 41 53 45 26 01 05 00 43 0f 17 43 9XNOCASE&...C..C +| 4064: 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e OMPILER=gcc-5.4. +| 4080: 30 20 32 30 31 36 30 36 30 39 58 52 54 52 49 4d 0 20160609XRTRIM +| page 4 offset 12288 +| 0: 0d 00 00 00 00 10 00 00 00 00 00 00 00 00 00 00 ................ +| page 5 offset 16384 +| 0: 0d 00 00 00 02 0b a0 00 0c ad 0b a0 00 00 00 00 ................ +| 2976: 82 0a 02 08 08 09 08 08 17 84 06 30 20 32 35 33 ...........0 253 +| 2992: 00 01 30 04 25 06 1b 00 00 08 32 30 31 36 30 36 ..0.%.....201606 +| 3008: 30 39 03 25 07 00 00 01 34 03 25 05 00 00 01 35 09.%....4.%....5 +| 3024: 03 25 04 00 01 07 30 30 30 30 30 30 30 03 25 1a .%....0000000.%. +| 3040: 00 00 08 63 6f 6d 70 69 6c 65 72 03 25 02 00 00 ...compiler.%... +| 3056: 06 64 62 73 74 61 74 03 25 0a 00 01 04 65 62 75 .dbstat.%....ebu +| 3072: 67 03 25 08 00 00 06 65 6e 61 62 6c 65 09 25 09 g.%....enable.%. +| 3088: 05 04 04 04 04 04 00 01 08 78 74 65 6e 73 69 6f .........xtensio +| 3104: 6e 03 25 1d 00 00 04 66 74 73 34 03 25 0d 00 03 n.%....fts4.%... +| 3120: 01 35 03 25 0f 00 00 03 67 63 63 03 25 03 00 01 .5.%....gcc.%... +| 3136: 06 65 6f 70 6f 6c 79 03 25 11 00 00 05 6a 73 6f .eopoly.%....jso +| 3152: 6e 31 03 25 13 00 00 04 6c 6f 61 64 03 25 1c 00 n1.%....load.%.. +| 3168: 00 03 6d 61 78 03 25 18 00 01 05 65 6d 6f 72 79 ..max.%....emory +| 3184: 03 25 19 00 03 04 73 79 73 35 03 25 15 00 00 04 .%....sys5.%.... +| 3200: 6f 6d 69 74 03 25 1b 00 00 05 72 74 72 65 65 03 omit.%....rtree. +| 3216: 25 17 00 00 0a 74 68 72 65 61 64 73 61 66 65 03 %....threadsafe. +| 3232: 25 1e 00 00 04 76 74 61 62 03 25 0b 00 86 50 01 %....vtab.%...P. +| 3248: 08 08 08 08 08 17 8d 12 30 20 38 33 35 00 01 30 ........0 835..0 +| 3264: 12 01 06 00 01 06 00 01 06 00 1f 03 00 01 03 00 ................ +| 3280: 01 03 00 00 08 32 30 31 36 30 36 30 39 09 01 07 .....20160609... +| 3296: 00 01 07 00 01 07 00 00 01 34 09 01 05 00 01 05 .........4...... +| 3312: 00 01 05 00 00 01 35 09 01 04 00 01 04 00 01 04 ......5......... +| 3328: 00 01 07 30 30 30 30 30 30 30 09 1c 04 00 01 04 ...0000000...... +| 3344: 00 01 04 00 00 06 62 69 6e 61 72 79 3c 03 01 02 ......binary<... +| 3360: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3376: 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 5f ..............._ +| 3392: 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03 ................ +| 3408: 01 02 02 00 03 01 02 02 00 00 08 63 6f 6d 70 69 ...........compi +| 3424: 6c 65 72 09 01 02 00 01 02 00 01 02 00 00 06 64 ler............d +| 3440: 62 73 74 61 74 09 07 03 00 01 03 00 01 03 00 01 bstat........... +| 3456: 04 65 62 75 67 09 04 02 00 01 02 00 01 02 00 00 .ebug........... +| 3472: 06 65 6e 61 62 6c 65 3f 07 02 00 01 02 00 01 02 .enable?........ +| 3488: 00 01 02 00 01 02 00 01 02 00 01 02 00 01 02 00 ................ +| 3504: 01 02 00 01 02 00 01 02 00 01 02 00 01 02 00 01 ................ +| 3520: 02 00 01 01 00 01 02 00 01 02 00 01 02 00 01 02 ................ +| 3536: 00 01 02 00 01 02 00 01 08 78 74 65 6e 73 69 6f .........xtensio +| 3552: 6e 09 1f 04 00 01 04 00 01 04 00 00 04 66 74 73 n............fts +| 3568: 34 09 0a 03 00 01 03 00 01 03 00 03 01 35 09 0d 4............5.. +| 3584: 03 00 01 03 00 01 03 00 00 03 67 63 63 09 01 03 ..........gcc... +| 3600: 00 01 03 00 01 03 00 01 06 65 6f 70 6f 6c 79 09 .........eopoly. +| 3616: 10 03 00 01 03 00 01 03 00 00 05 6a 73 6f 6e 31 ...........json1 +| 3632: 09 13 03 00 01 03 00 01 03 00 00 04 6c 6f 61 64 ............load +| 3648: 09 1f 03 00 01 03 00 01 03 00 00 03 6d 61 78 09 ............max. +| 3664: 1c 02 00 01 02 00 01 02 00 01 05 65 6d 6f 72 79 ...........emory +| 3680: 09 1c 03 00 01 03 00 01 03 00 03 04 73 79 73 35 ............sys5 +| 3696: 09 16 03 00 01 03 00 01 03 00 00 06 6e 6f 63 61 ............noca +| 3712: 73 65 3c 02 01 02 02 00 03 01 02 02 00 03 01 02 se<............. +| 3728: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3744: 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 ................ +| 3760: 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 00 ................ +| 3776: 04 6f 6d 69 74 09 1f 02 00 01 02 00 01 02 00 00 .omit........... +| 3792: 05 72 74 72 65 65 09 19 03 00 01 03 00 01 03 00 .rtree.......... +| 3808: 03 02 69 6d 3c 01 01 02 02 00 03 01 02 02 00 03 ..im<........... +| 3824: 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01 ................ +| 3840: 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 ................ +| 3856: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3872: 00 00 0a 74 68 72 65 61 64 73 61 66 65 09 22 02 ...threadsafe... +| 3888: 00 01 02 00 01 02 00 00 04 76 74 61 62 09 07 04 .........vtab... +| 3904: 00 01 04 00 01 04 00 00 01 78 b4 01 01 01 01 02 .........x...... +| 3920: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| 3936: 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 ................ +| 3952: 01 01 03 00 01 01 01 02 00 01 01 01 02 00 01 01 ................ +| 3968: 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 ................ +| 3984: 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 ................ +| 4000: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| 4016: 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 ................ +| 4032: 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 ................ +| 4048: 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 ................ +| 4064: 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 ................ +| 4080: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 02 0f f5 00 0f fb 0f f5 00 00 00 00 ................ +| 4080: 00 00 00 00 00 05 04 08 09 01 02 04 04 08 08 09 ................ +| page 7 offset 24576 +| 0: 0d 00 00 00 05 0f b8 00 0f f4 0f e9 0f d6 00 00 ................ +| 4016: 00 00 00 00 00 00 00 00 0d 05 02 23 61 75 74 6f ...........#auto +| 4032: 6d 65 72 67 65 3d 35 0d 04 02 23 6d 65 72 67 65 merge=5...#merge +| 4048: 3d 31 30 30 2c 38 11 03 02 2b 69 6e 74 65 67 72 =100,8...+integr +| 4064: 69 74 79 2d 63 68 65 63 6b 09 02 02 1b 72 65 62 ity-check....reb +| 4080: 75 69 6c 64 0a 01 02 1d 6f 70 74 69 6d 69 7a 65 uild....optimize +| end crash-11cf359576eb28.db +}]} {} + +set saved $sqlite_fts3_enable_parentheses +set sqlite_fts3_enable_parentheses 1 +do_catchsql_test 51.1 { + SELECT 'xyzzy',offsets(t1) FROM t1 WHERE t1 MATCH 'rtree OR json1''rtree NEAR "json1 enable"'; +} {1 {database disk image is malformed}} +set sqlite_fts3_enable_parentheses $saved + +#------------------------------------------------------------------------- +# +set saved $sqlite_fts3_enable_parentheses +set sqlite_fts3_enable_parentheses 1 +reset_db +do_test 52.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 28672 pagesize 4096 filename crash-fd33f4b1c8348b.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 92 00 00 00 07 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 07 00 00 00 04 ................ +| 96: 00 00 00 00 0d 0e ef 00 08 0d 13 00 0f bd 0f 5f ..............._ +| 112: 0e f7 0e 06 0e bc 0d a4 0d 4d 0d 13 00 00 00 00 .........M...... +| 3344: 00 00 00 38 08 06 17 11 11 08 5f 74 61 62 6c 65 ...8......_table +| 3360: 74 32 74 32 43 52 45 41 54 45 20 56 49 52 54 55 t2t2CREATE VIRTU +| 3376: 41 4c 20 54 41 42 4c 45 20 74 32 20 55 53 49 4e AL TABLE t2 USIN +| 3392: 47 20 66 74 73 34 61 75 78 28 74 31 29 55 07 07 G fts4aux(t1)U.. +| 3408: 17 1b 1b 01 81 01 74 61 62 6c 65 74 31 5f 73 74 ......tablet1_st +| 3424: 61 74 74 31 5f 73 74 61 74 07 43 52 45 41 54 45 att1_stat.CREATE +| 3440: 20 54 41 42 4c 45 20 27 74 31 5f 73 74 61 74 27 TABLE 't1_stat' +| 3456: 28 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d (id INTEGER PRIM +| 3472: 41 52 59 20 4b 45 59 2c 20 76 61 6c 75 65 20 42 ARY KEY, value B +| 3488: 4c 4f 42 29 60 06 07 17 21 21 01 81 0b 74 61 62 LOB)`...!!...tab +| 3504: 6c 65 74 31 5f 64 6f 63 73 69 7a 65 74 31 5f 64 let1_docsizet1_d +| 3520: 6f 63 73 69 7a 65 06 43 52 45 41 54 45 20 54 41 ocsize.CREATE TA +| 3536: 42 4c 45 20 27 74 31 5f 64 6f 63 73 69 7a 65 27 BLE 't1_docsize' +| 3552: 28 64 6f 63 69 64 20 49 4e 54 45 47 45 52 20 50 (docid INTEGER P +| 3568: 52 49 4d 41 52 59 20 4b 45 59 2c 20 73 69 7a 65 RIMARY KEY, size +| 3584: 20 42 4c 4f 42 29 81 33 04 07 17 1f 1f 01 82 35 BLOB).3.......5 +| 3600: 74 61 62 6c 65 74 31 5f 73 65 67 64 69 72 74 31 tablet1_segdirt1 +| 3616: 5f 73 65 67 64 69 72 04 43 52 45 41 54 45 20 54 _segdir.CREATE T +| 3632: 41 42 4c 45 20 27 74 31 5f 73 65 67 64 69 72 27 ABLE 't1_segdir' +| 3648: 28 6c 65 76 65 6c 20 49 4e 54 45 47 45 52 2c 69 (level INTEGER,i +| 3664: 64 78 20 49 4e 64 45 47 45 52 2c 73 74 61 72 74 dx INdEGER,start +| 3680: 5f 62 6c 6f 63 6b 20 49 4e 54 45 47 45 52 2c 6c _block INTEGER,l +| 3696: 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 6b 20 eaves_end_block +| 3712: 49 4e 54 45 47 45 52 2c 65 6e 64 5f 62 6c 6f 63 INTEGER,end_bloc +| 3728: 6b 20 49 4e 54 45 47 45 52 2c 72 6f 6f 74 20 42 k INTEGER,root B +| 3744: 4c 4f 42 2c 50 52 49 4d 41 52 59 20 4b 45 59 28 LOB,PRIMARY KEY( +| 3760: 6c 65 76 65 6c 2c 20 69 64 78 29 29 31 05 06 17 level, idx))1... +| 3776: 45 1f 01 00 69 6e 64 65 78 73 71 6c 69 74 65 5f E...indexsqlite_ +| 3792: 61 75 74 6f 69 6e 64 65 78 5f 74 31 5f 73 65 67 autoindex_t1_seg +| 3808: 64 69 72 5f 31 74 31 5f 73 65 67 64 69 72 05 00 dir_1t1_segdir.. +| 3824: 00 00 08 00 00 00 00 66 03 07 17 23 23 01 81 13 .......f...##... +| 3840: 74 61 62 6c 65 74 31 5f 73 65 67 6d 65 6e 74 73 tablet1_segments +| 3856: 74 31 5f 73 65 67 6d 65 6e 74 73 03 43 52 45 41 t1_segments.CREA +| 3872: 54 45 20 54 41 42 4c 45 20 27 74 31 5f 73 65 67 TE TABLE 't1_seg +| 3888: 6d 65 6e 74 73 27 28 62 6c 6f 63 6b 69 64 20 49 ments'(blockid I +| 3904: 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b NTEGER PRIMARY K +| 3920: 45 59 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 29 5c EY, block BLOB). +| 3936: 02 07 17 21 21 01 81 03 74 61 62 6c 65 74 31 5f ...!!...tablet1_ +| 3952: 63 6f 6e 74 65 6e 74 74 31 5f 63 6f 6e 74 65 6e contentt1_conten +| 3968: 74 02 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 t.CREATE TABLE ' +| 3984: 74 31 5f 63 6f 6e 74 65 6e 74 27 28 64 6f 63 69 t1_content'(doci +| 4000: 64 20 49 4e 54 45 47 45 52 e6 50 52 49 4d 41 52 d INTEGER.PRIMAR +| 4016: 59 20 4b 45 59 2c 20 27 63 30 61 27 29 41 01 06 Y KEY, 'c0a')A.. +| 4032: 17 11 11 08 71 74 61 62 6c 65 74 31 74 31 43 52 ....qtablet1t1CR +| 4048: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4064: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 34 LE t1 USING fts4 +| 4080: 28 61 2c 70 72 65 66 69 78 3d 27 31 2c 32 27 29 (a,prefix='1,2') +| page 2 offset 4096 +| 0: 0d 00 00 00 08 0e 22 00 0f c4 0f 00 00 00 00 00 ................ +| 3616: 00 00 42 08 04 00 81 09 73 75 6e 74 20 69 6e 20 ..B.....sunt in +| 3632: 63 75 6c 70 61 20 71 75 69 20 6f 66 66 69 63 69 culpa qui offici +| 3648: 61 20 64 65 73 65 72 75 6e 74 20 6d 6f 6c 6c 69 a deserunt molli +| 3664: 74 20 61 6e 69 6d 20 69 64 20 65 73 74 20 7c 61 t anim id est |a +| 3680: 62 6f 72 75 6d 2e 32 07 03 00 6b 45 78 63 65 70 borum.2...kExcep +| 3696: 74 65 75 72 20 73 69 6e 74 20 6f 63 63 61 65 63 teur sint occaec +| 3712: 61 74 20 63 75 70 69 64 61 74 61 74 20 6e 6f 6e at cupidatat non +| 3728: 20 70 72 6f 69 64 65 6e 74 2c 29 06 03 00 59 63 proident,)...Yc +| 3744: 69 6c 6c 75 6d 20 64 6f 6c 6f 72 65 20 65 20 66 illum dolore e f +| 3760: 75 67 69 61 74 20 6e 75 6c 6c 61 20 70 61 72 69 ugiat nulla pari +| 3776: 61 74 75 72 2e 42 05 04 00 81 09 44 75 69 73 20 atur.B.....Duis +| 3792: 61 75 74 65 20 69 72 75 72 65 20 64 6f 6c 6f 72 aute irure dolor +| 3808: 20 69 6e 20 72 65 60 72 65 68 65 6e 64 65 72 69 in re`rehenderi +| 3824: 74 20 69 6e 20 76 70 6c 75 70 74 61 74 65 20 76 t in vpluptate v +| 3840: 65 6c 69 72 c0 65 73 73 65 29 04 03 00 59 6e 69 elir.esse)...Yni +| 3856: 73 6a 20 75 74 20 61 6c 69 71 75 69 70 20 65 20 sj ut aliquip e +| 3872: 65 20 63 6f 6d 6d 6f 64 6f 20 63 6f 6e 73 65 71 e commodo conseq +| 3888: 75 61 74 2e 46 03 04 00 29 11 55 74 20 65 6e 69 uat.F...).Ut eni +| 3904: 6d 20 61 64 20 6d 69 6e 69 6d 20 76 65 6e 69 61 m ad minim venia +| 3920: 6d 2c 20 71 75 69 73 20 6e 6f 73 74 72 75 64 20 m, quis nostrud +| 3936: 65 78 65 72 63 69 7a 71 74 69 6f 6e 20 75 6c 6c exercizqtion ull +| 3952: 61 6d 63 6f 20 6c 61 62 6f 72 69 73 46 02 04 00 amco laborisF... +| 3968: 81 11 73 65 64 20 64 6f 20 65 69 75 73 6d 6f 64 ..sed do eiusmod +| 3984: 20 74 65 6d 70 6f 72 20 69 6e 63 69 64 69 64 75 tempor incididu +| 4000: 6e 74 20 75 74 20 6c 61 62 6f 72 65 20 65 74 20 nt ut labore et +| 4016: 64 6f 6c 6f 72 65 20 6d 61 67 6e 61 20 61 6c 69 dolore magna ali +| 4032: 71 75 61 2e 3a 01 03 00 7b 4c 6f 72 65 6d 20 69 qua.:....Lorem i +| 4048: 72 63 75 6d 20 64 6f 6c 6f 72 20 73 69 74 20 61 rcum dolor sit a +| 4064: 6d 65 74 2c 20 63 6f 6e 78 65 63 74 65 64 75 72 met, conxectedur +| 4080: 20 61 64 69 70 69 73 00 00 00 00 00 00 00 00 00 adipis......... +| page 4 offset 12288 +| 0: 0d 00 00 00 03 0a c1 00 0d 61 0c 54 0a c1 00 00 .........a.T.... +| 2752: 00 83 10 03 08 02 08 08 08 17 86 0e 08 00 30 20 ..............0 +| 2768: 33 38 35 00 02 61 64 06 01 08 00 02 04 00 01 01 385..ad......... +| 2784: 6c 06 02 0c 00 02 04 00 01 01 6d 03 01 06 10 01 l.........m..... +| 2800: 01 6e 03 08 09 00 01 01 75 03 05 03 00 00 02 63 .n......u......c +| 2816: 69 03 06 02 00 01 01 6f 07 01 07 00 03 07 03 00 i......o........ +| 2832: 01 01 75 06 75 05 00 01 04 00 00 02 64 65 03 08 ..u.u.......de.. +| 2848: 07 00 01 01 6f 0d 01 04 00 01 03 09 00 03 05 00 ....o........... +| 2864: 01 03 00 01 01 75 03 05 02 00 00 02 65 69 03 02 .....u......ei.. +| 2880: 04 00 01 01 6c 03 01 44 00 01 01 6e 03 03 03 00 ....l..D...n.... +| 2896: 01 01 73 06 05 0b 00 03 0b 00 01 01 74 03 02 09 ..s.........t... +| 2912: 00 01 01 78 06 03 09 00 04 02 00 00 02 66 75 03 ...x.........fu. +| 2928: 06 05 00 00 02 69 64 03 08 0a 00 01 01 6e 0a 02 .....id......n.. +| 2944: 06 00 03 06 04 00 03 03 00 01 01 70 03 01 03 00 ...........p.... +| 2960: 01 01 72 03 05 04 00 00 02 6c 61 09 02 08 00 01 ..r......la..... +| 2976: 0b 00 05 0c 00 01 01 6f 03 01 02 00 00 02 6d 61 .......o......ma +| 2992: 03 02 0b 00 01 01 69 03 03 05 00 01 01 6f 03 08 ......i......o.. +| 3008: 08 00 00 02 6e 69 03 04 02 00 01 01 6f 06 03 08 ....ni......o... +| 3024: 00 04 06 00 01 01 75 03 06 06 00 00 02 6f 63 03 ......u......oc. +| 3040: 07 04 00 01 01 66 03 08 06 00 00 02 70 61 03 06 .....f......pa.. +| 3056: 07 00 01 11 72 03 07 07 00 00 02 71 75 06 03 07 ....r......qu... +| 3072: 00 05 05 00 00 02 72 65 03 05 07 00 00 02 73 65 ......re......se +| 3088: 03 02 02 00 01 01 69 06 01 05 00 06 03 00 01 01 ......i......... +| 3104: 75 03 08 02 00 00 02 74 65 03 02 05 00 00 02 75 u......te......u +| 3120: 6c 13 03 0a 00 01 01 74 09 02 07 00 01 02 00 01 l......t........ +| 3136: 02 ff ff 02 76 65 06 03 06 00 02 0a 00 01 01 6f ....ve.........o +| 3152: 03 05 09 00 82 0a 02 08 02 08 08 08 17 84 02 04 ................ +| 3168: 00 30 20 32 35 31 00 01 61 13 01 06 04 00 01 0c .0 251..a....... +| 3184: 00 01 04 00 01 04 00 01 03 00 03 09 00 00 01 63 ...............c +| 3200: 10 01 07 00 03 07 03 00 02 02 00 01 05 00 01 04 ................ +| 3216: 00 00 01 64 11 01 04 00 01 03 09 00 03 02 05 00 ...d............ +| 3232: 01 03 00 02 07 00 00 01 65 1b 01 09 00 01 04 07 ........e....... +| 3248: 00 01 03 08 00 01 05 03 00 01 1c eb 01 04 00 01 ................ +| 3264: 0e 80 01 0b 00 00 01 66 03 06 05 00 00 01 69 0f .......f......i. +| 3280: 01 03 00 01 06 0b 23 04 04 04 00 03 03 09 00 00 ......#......... +| 3296: 01 6c 0c 01 02 00 01 08 00 01 0b 00 05 0c 00 00 .l.............. +| 3312: 01 6d 09 02 0b 00 01 05 00 05 08 00 00 01 6e 0c .m............n. +| 3328: 03 08 00 01 02 00 02 06 00 01 06 00 00 01 6f 06 ..............o. +| 3344: 07 04 00 01 06 00 00 01 70 06 06 07 00 01 07 00 ........p....... +| 3360: 00 01 71 06 03 07 00 05 05 00 00 01 72 03 05 07 ..q.........r... +| 3376: 00 00 02 73 0c 01 05 00 01 02 00 05 03 00 01 02 ...s............ +| 3392: 00 00 01 74 03 02 05 00 00 01 75 0a 02 07 00 01 ...t......u..... +| 3408: 02 0a 00 01 03 00 00 01 76 07 03 06 00 02 09 03 ........v....... +| 3424: 00 85 1c 01 08 08 08 08 08 17 8a 2a 30 20 36 35 ...........*0 65 +| 3440: 35 00 02 61 64 03 03 04 00 02 08 69 70 69 73 63 5..ad......ipisc +| 3456: 69 6e 67 03 01 08 00 01 05 6c 69 71 75 61 03 02 ing......liqua.. +| 3472: 0c 00 05 02 69 70 03 04 04 00 01 03 6d 65 74 03 ....ip......met. +| 3488: 01 06 00 01 03 6e 69 6d 03 08 09 00 01 03 75 74 .....nim......ut +| 3504: 65 03 05 03 00 00 06 63 69 6c 6c 75 6d 03 06 02 e......cillum... +| 3520: 00 01 06 6f 6d 6d 6f 64 6f 03 04 07 00 02 09 6e ...ommodo......n +| 3536: 73 65 63 74 65 74 75 72 03 01 07 00 05 04 71 75 sectetur......qu +| 3552: 61 73 03 04 08 00 01 04 75 6c 70 61 03 08 04 00 as......ulpa.... +| 3568: 02 07 70 69 64 61 74 61 74 03 07 05 00 00 08 64 ..pidatat......d +| 3584: 65 73 65 72 75 6e 74 03 08 07 00 01 01 6f 03 09 eserunt......o.. +| 3600: b3 00 02 03 6c 6f 72 06 01 04 00 04 05 00 05 01 ....lor......... +| 3616: 65 06 02 0a 00 04 03 00 01 03 75 69 73 03 05 02 e.........uis... +| 3632: 00 00 01 65 07 04 05 03 00 02 04 00 01 06 69 75 ...e..........iu +| 3648: 73 6d 6f 64 03 02 04 00 01 03 6c 69 74 03 01 09 smod......lit... +| 3664: 00 01 03 6e 69 6d 03 03 03 00 01 03 73 73 65 03 ...nim......sse. +| 3680: 05 0b 00 02 01 74 03 08 0b 00 01 01 74 03 02 09 .....t......t... +| 3696: 00 01 08 78 63 65 70 64 65 75 72 03 07 02 00 02 ...xcepdeur..... +| 3712: 0a 65 72 63 69 74 61 74 69 6f 6e 03 03 09 00 00 .ercitation..... +| 3728: 06 66 75 67 69 61 74 03 06 05 00 00 02 69 64 03 .fugiat......id. +| 3744: 08 0a 00 01 01 6e 07 05 06 04 00 03 03 00 02 08 .....n.......... +| 3760: 63 69 64 69 64 75 6e 74 03 02 06 00 01 04 70 73 cididunt......ps +| 3776: 75 6c f3 01 03 00 01 04 72 75 72 65 03 05 04 00 ul......rure.... +| 3792: 00 06 6c 61 62 6f 72 65 03 02 08 00 05 02 69 73 ..labore......is +| 3808: 03 03 0b 00 05 02 75 6d 03 08 0c 00 01 04 6f 72 ......um......or +| 3824: 65 6d 03 01 02 00 00 05 6d 61 67 6e 61 03 02 0b em......magna... +| 3840: 00 01 04 69 6e 69 6d 03 03 05 00 01 05 6f 6c 6c ...inim......oll +| 3856: 69 74 03 08 08 00 00 04 6e 69 73 69 03 04 02 00 it......nisi.... +| 3872: 01 02 6f 6e 03 07 06 00 02 05 73 74 72 75 64 03 ..on......strud. +| 3888: 03 08 00 01 04 75 6c 6c 61 03 06 06 00 00 08 6f .....ulla......o +| 3904: 63 63 61 65 63 61 74 03 07 04 00 01 06 66 66 69 ccaecat......ffi +| 3920: 63 69 61 03 08 06 00 00 08 70 61 72 69 61 74 75 cia......pariatu +| 3936: 72 03 06 07 00 01 07 72 6f 69 64 65 6e 74 03 07 r......roident.. +| 3952: 07 00 00 03 71 75 69 03 08 15 00 03 01 73 03 03 ....qui......s.. +| 3968: 07 00 00 0d 72 65 70 72 65 68 65 6e 64 65 72 69 ....reprehenderi +| 3984: 74 03 05 07 00 00 03 73 65 64 03 02 01 ff ff f0 t......sed...... +| 4000: b9 6e 74 03 07 03 00 02 01 74 03 01 05 00 01 03 .nt......t...... +| 4016: 75 6e 74 03 08 02 00 00 06 74 65 6d 70 6f 72 03 unt......tempor. +| 4032: 02 05 00 00 07 75 6c 6c 61 6d 63 6f 03 03 0a 00 .....ullamco.... +| 4048: 01 01 74 09 02 07 00 01 02 00 01 03 00 00 05 76 ..t............v +| 4064: 65 6c 69 74 03 05 0a 00 02 04 6e 69 61 6d 03 03 elit......niam.. +| 4080: 06 00 01 08 6f 6c 75 70 74 61 74 65 03 05 09 00 ....oluptate.... +| page 5 offset 16384 +| 0: 0a 00 00 00 03 0f eb 00 0f fb 0f f3 00 00 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 07 04 02 08 01 ................ +| 4080: 08 00 03 07 04 02 08 01 04 00 02 04 04 08 08 09 ................ +| page 6 offset 20480 +| 0: 0d 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 ................ +| 4048: 04 08 03 00 0e 0b 04 07 03 00 0e 06 04 06 03 00 ................ +| 4064: 0e 06 04 05 03 00 0e 0a 04 04 03 00 0e 07 04 03 ................ +| 4080: 03 00 0d fa 04 02 03 00 0e 0b 04 00 00 00 00 00 ................ +| page 7 offset 24576 +| 4080: 00 00 00 00 00 00 00 07 00 03 00 00 00 00 00 00 ................ +| end crash-fd33f4b1c8348b.db +}]} {} + +do_catchsql_test 52.1 { + SELECT * FROM t1, t2; +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +# +reset_db +do_test 53.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 8192 pagesize 1024 filename crash-7bc.txt.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 04 00 01 01 00 40 20 20 00 00 00 00 00 00 00 08 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 06 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 96: 00 00 00 00 0d 02 f3 00 07 01 51 00 03 c8 03 63 ..........Q....c +| 112: 02 fb 02 0a 02 c0 01 a8 01 51 00 00 00 00 00 00 .........Q...... +| 336: 00 55 07 07 17 1b 1b 01 81 01 74 61 62 6c 65 74 .U........tablet +| 352: 31 5f 73 74 61 74 74 31 5f 73 74 61 74 07 43 52 1_statt1_stat.CR +| 368: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 73 EATE TABLE 't1_s +| 384: 74 61 74 27 28 69 64 20 49 4e 54 45 47 45 52 20 tat'(id INTEGER +| 400: 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 61 6c PRIMARY KEY, val +| 416: 75 65 20 42 4c 4f 42 29 60 06 07 17 21 21 01 81 ue BLOB)`...!!.. +| 432: 0b 74 61 62 6c 65 74 31 5f 64 6f 63 73 69 7a 65 .tablet1_docsize +| 448: 74 31 5f 64 6f 63 73 69 7a 65 06 43 52 45 41 54 t1_docsize.CREAT +| 464: 45 20 54 41 42 4c 45 20 27 74 31 5f 64 6f 63 73 E TABLE 't1_docs +| 480: 69 7a 65 27 28 64 6f 63 69 64 20 49 4e 54 45 47 ize'(docid INTEG +| 496: 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 ER PRIMARY KEY, +| 512: 73 69 7a 65 20 42 4c 4f 42 29 81 33 04 07 17 1f size BLOB).3.... +| 528: 1f 01 82 35 74 61 62 6c 65 74 31 5f 73 65 67 64 ...5tablet1_segd +| 544: 69 72 74 31 5f 73 65 67 64 69 72 04 43 52 45 41 irt1_segdir.CREA +| 560: 54 45 20 54 41 42 4c 45 20 27 74 31 5f 73 65 67 TE TABLE 't1_seg +| 576: 64 69 72 27 28 6c 65 76 65 6c 20 49 4e 54 45 47 dir'(level INTEG +| 592: 45 52 2c 69 64 78 20 49 4e 54 45 47 45 52 2c 73 ER,idx INTEGER,s +| 608: 74 61 72 74 5f 62 6c 6f 63 6b 20 49 4e 54 45 47 tart_block INTEG +| 624: 45 52 2c 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c ER,leaves_end_bl +| 640: 6f 63 6b 20 49 4e 54 45 47 45 52 2c 65 6e 64 5f ock INTEGER,end_ +| 656: 62 6c 6f 63 6b 20 49 4e 54 45 47 45 52 2c 72 6f block INTEGER,ro +| 672: 6f 74 20 42 4c 4f 42 2c 50 52 49 4d 41 52 59 20 ot BLOB,PRIMARY +| 688: 4b 45 59 28 6c 65 76 65 6c 2c 20 69 64 78 29 29 KEY(level, idx)) +| 704: 31 05 06 17 45 1f 01 00 69 6e 64 65 78 73 71 6c 1...E...indexsql +| 720: 69 74 65 5f 61 75 74 6f 69 6e 64 65 78 5f 74 31 ite_autoindex_t1 +| 736: 5f 73 65 67 64 69 72 5f 31 74 31 5f 73 65 67 64 _segdir_1t1_segd +| 752: 69 72 05 00 00 00 08 00 00 00 00 66 03 07 17 23 ir.........f...# +| 768: 23 01 81 13 74 61 62 6c 65 74 31 5f 73 65 67 6d #...tablet1_segm +| 784: 65 6e 74 73 74 31 5f 73 65 67 6d 65 6e 74 73 03 entst1_segments. +| 800: 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 CREATE TABLE 't1 +| 816: 5f 73 65 67 6d 65 6e 74 73 27 28 62 6c 6f 63 6b _segments'(block +| 832: 69 64 20 49 4e 53 45 47 45 52 20 50 52 49 4d 41 id INSEGER PRIMA +| 848: 52 59 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 4c RY KEY, block BL +| 864: 4f 42 29 63 02 07 17 21 21 01 81 11 74 61 62 6c OB)c...!!...tabl +| 880: 65 74 31 5f 63 6f 6e 74 65 6e 74 74 31 5f 63 6f et1_contentt1_co +| 896: 6e 74 65 6e 74 02 43 52 45 41 54 45 20 54 41 42 ntent.CREATE TAB +| 912: 4c 45 20 27 74 31 5f 63 6f 6e 74 65 6e 74 27 28 LE 't1_content'( +| 928: 64 6f 63 69 64 20 49 4e 54 45 47 45 52 20 50 52 docid INTEGER PR +| 944: 49 4d 41 52 59 20 4b 45 59 2c 20 27 63 30 30 27 IMARY KEY, 'c00' +| 960: 2c 20 27 63 31 62 27 29 36 01 06 17 11 11 08 5b , 'c1b')6......[ +| 976: 74 61 62 6c 65 74 31 74 31 43 52 45 41 54 45 20 tablet1t1CREATE +| 992: 56 49 52 54 55 41 4c 20 54 41 42 4c 45 20 74 31 VIRTUAL TABLE t1 +| 1008: 20 55 53 49 4e 47 20 66 74 73 34 28 30 2c 62 29 USING fts4(0,b) +| page 2 offset 1024 +| 0: 0d 00 00 00 03 00 0f 00 00 23 00 16 00 0f 00 05 .........#...... +| 16: 03 04 00 08 0f 61 0b 02 04 00 08 1b 41 54 45 20 .....a......ATE +| 32: 32 3a 50 87 5a 01 05 00 08 8f 37 66 30 30 30 30 2:P.Z.....7f0000 +| 48: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 64: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 80: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 96: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 112: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 128: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 144: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 160: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 176: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 192: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 208: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 224: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 240: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 256: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 272: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 288: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 304: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 320: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 336: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 352: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 368: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 384: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 400: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 416: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 432: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 448: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 464: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 480: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 496: 30 40 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0@00000000000000 +| 512: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 528: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 544: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 560: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 576: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 592: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 608: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 624: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 640: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 656: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 672: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 688: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 704: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 720: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 736: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 752: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 768: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 784: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 800: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 816: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 832: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 848: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 864: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 880: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 896: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 912: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 928: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 944: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 960: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 976: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 992: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 1008: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| page 3 offset 2048 +| 0: 0d 00 00 00 02 03 86 00 03 f4 03 86 00 00 00 00 ................ +| 896: 00 00 00 00 00 00 87 62 02 04 00 8f 48 00 d5 07 .......b....H... +| 912: 66 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 f000000000000000 +| 928: 30 30 30 30 3a 30 30 30 30 30 30 30 30 30 30 30 0000:00000000000 +| 944: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 960: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 976: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 992: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 1008: 00 00 00 08 0a 01 03 00 1a 00 01 30 03 01 02 00 ...........0.... +| page 4 offset 3072 +| 0: 0d 00 00 00 03 03 9e 00 03 ed 03 bc 03 9e 00 00 ................ +| 912: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1c 03 ................ +| 928: 07 08 01 08 08 15 2c 02 30 20 31 36 00 01 30 03 ......,.0 16..0. +| 944: 03 02 00 00 01 61 05 03 01 01 02 00 2f 02 07 08 .....a....../... +| 960: 09 08 08 15 54 30 20 33 36 00 01 30 03 02 02 00 ....T0 36..0.... +| 976: 00 01 32 05 02 01 01 03 00 00 03 61 74 65 05 02 ..2........ate.. +| 992: 01 01 02 00 00 01 70 05 02 01 01 04 00 11 01 07 ......p......... +| 1008: 08 08 09 01 17 14 02 32 20 39 39 37 01 01 01 66 .......2 997...f +| page 5 offset 4096 +| 0: 0a 00 00 00 03 03 ee 00 03 fb 03 f5 03 ee 00 00 ................ +| 992: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 06 04 ................ +| 1008: 08 01 01 02 03 05 04 08 09 01 02 04 04 08 08 09 ................ +| page 6 offset 5120 +| 0: 0d 00 00 00 03 03 eb 00 00 00 00 00 00 00 00 00 ................ +| 992: 00 00 00 00 00 00 00 00 00 00 00 05 03 03 00 10 ................ +| 1008: 01 01 05 02 03 00 10 01 03 05 01 03 00 10 01 01 ................ +| page 7 offset 6144 +| 0: 0d 00 00 00 01 03 f6 00 03 f6 00 00 00 00 00 00 ................ +| 1008: 00 00 00 00 00 00 08 00 03 00 16 03 08 c5 e0 07 ................ +| page 8 offset 7168 +| 0: 00 00 00 00 30 30 30 30 30 30 30 30 30 30 30 30 ....000000000000 +| 16: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 32: 30 30 30 30 30 30 30 30 30 30 30 30 30 bc 30 30 0000000000000.00 +| 48: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 64: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 80: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 96: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 112: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 128: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 144: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 160: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 176: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 192: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 c0 30 00000000000000.0 +| 208: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 224: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 240: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 256: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 272: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 288: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 304: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 320: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 336: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 352: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 368: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 384: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 400: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 416: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 432: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 448: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 464: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 480: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 496: 30 30 30 30 30 30 30 30 30 30 30 40 30 30 30 30 00000000000@0000 +| 512: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 528: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 544: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 560: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 576: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 592: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 608: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 624: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 640: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 656: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 672: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 688: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 704: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 720: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 736: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 752: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 768: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 784: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 800: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 816: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 832: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 848: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 864: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 880: 30 30 30 30 30 30 30 30 30 05 01 00 00 00 00 00 000000000....... +| end crash-7bc.txt.db +}]} {} + +do_execsql_test 53.1 { + SELECT*FROM t1 WHERE t1 MATCH'ATE"0"OR"2D:P"""ATE"0"OR"2:P"""'; +} {0 {ATE 2:P}} +set sqlite_fts3_enable_parentheses $saved + +#------------------------------------------------------------------------- +# +reset_db +do_test 54.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +.open --hexdb +| size 8192 pagesize 1024 filename crash-365.txt.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 04 00 01 01 00 40 20 20 00 00 00 00 00 00 00 08 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 06 00 00 00 04 ................ +| 96: 00 00 00 00 0d 02 f3 00 07 01 51 00 03 c8 03 63 ..........Q....c +| 112: 02 fb 02 0a 02 c0 01 a8 01 51 00 00 00 00 00 00 .........Q...... +| 336: 00 55 07 07 17 1b 1b 01 81 01 74 61 62 6c 65 74 .U........tablet +| 352: 31 5f 73 74 61 74 74 31 5f 73 74 61 74 07 43 52 1_statt1_stat.CR +| 368: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 73 EATE TABLE 't1_s +| 384: 74 61 74 27 28 69 64 20 49 4e 54 45 47 45 52 20 tat'(id INTEGER +| 400: 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 61 6c PRIMARY KEY, val +| 416: 75 65 20 42 4c 4f 42 29 60 06 07 17 21 21 01 81 ue BLOB)`...!!.. +| 432: 0b 74 61 62 6c 65 74 31 5f 64 6f 63 73 69 7a 65 .tablet1_docsize +| 448: 74 31 5f 64 6f 63 73 69 7a 65 06 43 52 45 41 54 t1_docsize.CREAT +| 464: 45 20 54 41 42 4c 45 20 27 74 31 5f 64 6f 63 73 E TABLE 't1_docs +| 480: 69 7a 65 27 28 64 6f 63 69 64 20 49 4e 54 45 47 ize'(docid INTEG +| 496: 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 ER PRIMARY KEY, +| 512: 73 69 7a 65 20 42 4c 4f 42 29 81 33 04 07 17 1f size BLOB).3.... +| 528: 1f 01 82 35 74 61 62 6c 65 74 31 5f 73 65 67 64 ...5tablet1_segd +| 544: 69 72 74 31 5f 73 65 67 64 69 72 04 43 52 45 41 irt1_segdir.CREA +| 560: 54 45 20 54 41 42 4c 45 20 27 74 31 5f 73 65 67 TE TABLE 't1_seg +| 576: 64 69 72 27 28 6c 65 76 65 6c 20 49 4e 54 45 47 dir'(level INTEG +| 592: 45 52 2c 69 64 78 20 49 4e 54 45 47 45 52 2c 73 ER,idx INTEGER,s +| 608: 74 61 72 74 5f 62 6c 6f 63 6b 20 49 4e 54 45 47 tart_block INTEG +| 624: 45 52 2c 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c ER,leaves_end_bl +| 640: 6f 63 6b 20 49 4e 54 45 47 45 52 2c 65 6e 64 5f ock INTEGER,end_ +| 656: 62 6c 6f 63 6b 20 49 4e 54 45 47 45 52 2c 72 6f block INTEGER,ro +| 672: 6f 74 20 42 4c 4f 42 2c 50 52 49 4d 41 52 59 20 ot BLOB,PRIMARY +| 688: 4b 45 59 28 6c 65 76 65 6c 2c 20 69 64 78 29 29 KEY(level, idx)) +| 704: 31 05 06 17 45 1f 01 00 69 6e 64 65 78 73 71 6c 1...E...indexsql +| 720: 69 74 65 5f 61 75 74 6f 69 6e 64 65 78 5f 74 31 ite_autoindex_t1 +| 736: 5f 73 65 67 64 69 72 5f 31 74 31 5f 73 65 67 64 _segdir_1t1_segd +| 752: 69 72 05 00 00 00 08 00 00 00 00 66 03 07 17 23 ir.........f...# +| 768: 23 01 81 13 74 61 62 6c 65 74 31 5f 73 65 67 6d #...tablet1_segm +| 784: 65 6e 74 73 74 31 5f 73 65 67 6d 65 6e 74 73 03 entst1_segments. +| 800: 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 CREATE TABLE 't1 +| 816: 5f 73 65 67 6d 65 6e 74 73 27 28 62 6c 6f 63 6b _segments'(block +| 832: 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 id INTEGER PRIMA +| 848: 52 59 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 4c RY KEY, block BL +| 864: 4f 42 29 63 02 07 17 21 21 01 81 11 74 61 62 6c OB)c...!!...tabl +| 880: 65 74 31 5f 63 6f 6e 74 65 6e 74 74 31 5f 63 6f et1_contentt1_co +| 896: 6e 74 65 6e 74 02 43 52 45 41 54 45 20 54 41 42 ntent.CREATE TAB +| 912: 4c 45 20 27 74 31 5f 63 6f 6e 74 65 6e 74 27 28 LE 't1_content'( +| 928: 64 6f 63 69 64 20 49 4e 54 45 47 45 52 20 50 52 docid INTEGER PR +| 944: 49 4d 41 52 59 20 4b 45 59 2c 20 27 63 30 30 27 IMARY KEY, 'c00' +| 960: 2c 20 27 63 31 62 27 29 36 01 06 17 11 11 08 5b , 'c1b')6......[ +| 976: 74 61 62 6c 65 74 31 74 31 43 52 45 41 54 45 20 tablet1t1CREATE +| 992: 56 49 52 54 55 41 4c 20 54 41 42 4c 45 20 74 31 VIRTUAL TABLE t1 +| 1008: 20 55 53 49 4e 47 20 66 74 73 34 28 30 2c 62 29 USING fts4(0,b) +| page 2 offset 1024 +| 0: 0d 00 00 00 03 00 0f 00 00 23 00 16 00 0f 00 05 .........#...... +| 16: 03 04 00 08 0f 61 0b 02 04 00 08 1b 41 54 45 20 .....a......ATE +| 32: 32 3a 50 87 5a 01 05 00 08 8f 37 66 30 30 30 30 2:P.Z.....7f0000 +| 48: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 64: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 80: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 96: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 112: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 128: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 144: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 160: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 176: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 192: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 208: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 224: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 240: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 256: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 272: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 288: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 304: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 320: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 336: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 352: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 368: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 384: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 400: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 416: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 432: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 448: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 464: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 480: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 496: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 512: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 528: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 544: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 560: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 576: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 592: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 608: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 624: 30 30 30 30 30 30 30 30 30 30 1b 30 30 30 30 30 0000000000.00000 +| 640: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 656: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 672: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 688: 30 30 30 30 30 30 30 30 2f 30 30 30 30 30 30 30 00000000/0000000 +| 704: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 720: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 736: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 752: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 768: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 784: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 800: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 816: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 832: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 848: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 864: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 880: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 896: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 912: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 928: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 944: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 960: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 976: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 992: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 1008: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| page 3 offset 2048 +| 0: 0d 00 00 00 02 03 86 00 03 f4 03 86 00 00 00 00 ................ +| 896: 00 00 00 00 00 00 87 62 02 04 00 8f 48 00 d5 07 .......b....H... +| 912: 66 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 f000000000000000 +| 928: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 944: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 960: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 976: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 992: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 1008: 00 00 00 08 0a 01 03 00 1a 00 01 30 03 01 02 00 ...........0.... +| page 4 offset 3072 +| 0: 0d 00 00 00 03 03 9e 00 03 ed 03 bc 03 9e 00 01 ................ +| 912: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1c 03 ................ +| 928: 07 08 01 08 08 15 2c 02 30 20 31 36 00 01 30 03 ......,.0 16..0. +| 944: 03 02 00 00 01 61 05 03 01 01 02 00 2f 02 07 08 .....a....../... +| 960: 09 08 08 15 54 30 20 33 36 00 01 30 03 02 02 00 ....T0 36..0.... +| 976: 00 01 32 05 02 01 01 03 00 00 03 61 74 65 05 02 ..2........ate.. +| 992: 01 01 02 00 00 01 70 05 02 01 01 04 00 11 01 07 ......p......... +| 1008: 08 08 09 01 17 14 02 32 20 39 39 37 01 01 01 66 .......2 997...f +| page 5 offset 4096 +| 0: 0a 00 00 00 03 03 ee 00 03 fb 03 f5 03 ee 00 00 ................ +| 992: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 06 04 ................ +| 1008: 08 01 01 02 03 05 04 08 09 01 02 04 04 08 08 09 ................ +| page 6 offset 5120 +| 0: 0d 00 00 00 03 03 eb 00 03 f9 03 f2 00 00 00 00 ................ +| 992: 00 00 00 00 00 00 00 00 00 00 00 05 03 03 00 10 ................ +| 1008: 01 01 05 02 03 00 10 01 03 05 01 03 00 10 01 01 ................ +| page 7 offset 6144 +| 0: 0d 00 00 00 01 03 f6 00 03 f6 00 00 00 00 00 00 ................ +| 1008: 00 00 00 00 00 00 08 00 03 00 16 03 03 05 e0 07 ................ +| page 8 offset 7168 +| 0: 00 00 00 00 30 30 30 30 30 30 30 30 30 30 30 30 ....000000000000 +| 16: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 32: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 48: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 64: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 80: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 96: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 112: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 128: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 144: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 160: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 176: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 192: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 208: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 224: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 240: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 256: 30 30 30 30 30 2f 30 30 30 30 30 30 30 30 30 30 00000/0000000000 +| 272: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 288: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 304: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 320: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 336: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 352: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 368: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 384: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 400: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 416: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 432: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 448: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 464: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 480: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 496: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 512: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 528: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 544: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 560: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 576: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 592: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 608: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 624: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 640: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 656: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 672: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 688: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 704: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 720: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 736: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 752: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 768: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 784: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 800: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 816: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 832: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 848: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 864: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 +| 880: 30 30 30 30 30 30 30 30 30 05 01 01 01 02 00 00 000000000....... +| end crash-365.txt.db +}]} {} + +do_execsql_test 54.1 { + SELECT rowid, quote(matchinfo(t1,'pcxybspcxybs')) FROM t1 WHERE t1 MATCH'ATE"0"OR"2:P"""'; +} + +finish_test diff --git a/test/fts3corrupt5.test b/test/fts3corrupt5.test new file mode 100644 index 0000000000..a8a3b0168e --- /dev/null +++ b/test/fts3corrupt5.test @@ -0,0 +1,59 @@ +# 2019 May 22 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/fts3_common.tcl +set testprefix fts3corrupt5 + +# If SQLITE_ENABLE_FTS3 is defined, omit this file. +ifcapable !fts3 { + finish_test + return +} + +sqlite3_fts3_may_be_corrupt 1 + +do_execsql_test 1.0 { + BEGIN; + CREATE VIRTUAL TABLE ft USING fts3(a, b, c); + INSERT INTO ft VALUES('one', 'one', 'one'); + COMMIT; +} + +do_execsql_test 1.1 { + SELECT * FROM ft WHERE ft MATCH 'b:one' +} {one one one} + +do_execsql_test 1.2 { + SELECT quote(root) FROM ft_segdir; +} {X'00036F6E6509010201010201020200'} + +breakpoint +foreach {tn val q bCorrupt} { + 1 X'00036F6E650901' 'b:one' 1 + 2 X'00036F6E6509010201010201FFFFFF' 'c:one' 1 + 3 X'00036F6E6501' 'b:one' 1 + 4 X'00036F6E650101' 'b:one' 1 + 5 X'00036F6E650100' 'b:one' 0 +} { + do_execsql_test 1.3.$tn.1 "UPDATE ft_segdir SET root = $val" + + set res {0 {}} + if {$bCorrupt} { set res {1 {database disk image is malformed}}} + do_catchsql_test 1.3.$tn.2 { + SELECT * FROM ft WHERE ft MATCH $q + } $res +} + +finish_test diff --git a/test/fts3corrupt6.test b/test/fts3corrupt6.test new file mode 100644 index 0000000000..a3c5fd8da6 --- /dev/null +++ b/test/fts3corrupt6.test @@ -0,0 +1,79 @@ +# 2020 June 8 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# This file implements regression tests for SQLite library. The +# focus of this script is testing the FTS3 module. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/fts3_common.tcl +set testprefix fts3corrupt6 + +# If SQLITE_ENABLE_FTS3 is defined, omit this file. +ifcapable !fts3 { + finish_test + return +} + +set ::saved_sqlite_fts3_enable_parentheses $::sqlite_fts3_enable_parentheses +set sqlite_fts3_enable_parentheses 1 +sqlite3_fts3_may_be_corrupt 1 +database_may_be_corrupt + +do_execsql_test 1.0 { + BEGIN TRANSACTION; + CREATE TABLE t_content(col0 INTEGER); + PRAGMA writable_schema=ON; + CREATE VIRTUAL TABLE t0 USING fts3(col0 INTEGER PRIMARY KEY,col1 VARCHAR(8),col2 BINARY,col3 BINARY); + INSERT INTO t0_content VALUES(0,NULL,NULL,NULL,NULL); + INSERT INTO t0_segdir VALUES(0,0,0,0,'0 42',X'000131030102000103323334050101010200000461616161050101020200000462626262050101030200'); + COMMIT; +} + +do_execsql_test 1.1 { + SELECT 0+matchinfo(t0,'yxyyxy') FROM t0 WHERE t0 MATCH CAST( x'2b0a312b0a312a312a2a0b5d0a0b0b0a312a0a0b0b0a312a0b310a392a0b0a27312a2a0b5d0a312a0b310a31315d0b310a312a316d2a0b313b15bceaa50a312a0b0a27312a2a0b5d0a312a0b310a312b0b2a310a312a0b2a0b2a0b2e5d0a0bff313336e34a2a312a0b0a3c310b0a0b4b4b0b4b2a4bec40322b2a0b310a0a312a0a0a0a0a0a0a0a0a0b310a312a2a2a0b5d0a0b0b0a312a0b310a312a0b0a4e4541530b310a5df5ced70a0a0a0a0a4f520a0a0a0a0a0a0a312a0b0a4e4541520b310a5d616161610a0a0a0a4f520a0a0a0a0a0a312b0a312a312a0a0a0a0a0a0a004a0b0a310b220a0b0a310a4a22310a0b0a7e6fe0e0e030e0e0e0e0e01176e02000e0e0e0e0e01131320226310a0b0a310a4a22310a0b0a310a766f8b8b4ee0e0300ae0090909090909090909090909090909090909090909090909090909090909090947aaaa540b09090909090909090909090909090909090909090909090909090909090909fae0e0f2f22164e0e0f273e07fefefef7d6dfafafafa6d6d6d6d' AS TEXT); +} {0} + +do_execsql_test 1.2 { + CREATE VIRTUAL TABLE t1 USING fts3(col0 INTEGER PRIMARY KEY,col1 VARCHAR(8),col2 BINARY,col3 BINARY); + INSERT INTO t1_content VALUES(0,NULL,NULL,NULL,NULL); + INSERT INTO t1_segdir VALUES(0,0,0,0,'0 42',X'000131030102000103323334050101010200000461616161050101020200000462626262050101030200'); +} + +do_execsql_test 1.3 { + SELECT 42+matchinfo(t1,'yxyyxy') FROM t1 WHERE t1 MATCH x'2b0a312b0a312a312a2a0b5d0a0b0b0a312a0a0b0b0a312a0b310a392a0b0a27312a2a0b5d0a312a0b310a31315d0b310a312a316d2a0b313b15bceaa50a312a0b0a27312a2a0b5d0a312a0b310a312b0b2a310a312a0b2a0b2a0b2e5d0a0bff313336e34a2a312a0b0a3c310b0a0b4b4b0b4b2a4bec40322b2a0b310a0a312a0a0a0a0a0a0a0a0a0b310a312a2a2a0b5d0a0b0b0a312a0b310a312a0b0a4e4541530b310a5df5ced70a0a0a0a0a4f520a0a0a0a0a0a0a312a0b0a4e4541520b310a5d616161610a0a0a0a4f520a0a0a0a0a0a312b0a312a312a0a0a0a0a0a0a004a0b0a310b220a0b0a310a4a22310a0b0a7e6fe0e0e030e0e0e0e0e01176e02000e0e0e0e0e01131320226310a0b0a310a4a22310a0b0a310a766f8b8b4ee0e0300ae0090909090909090909090909090909090909090909090909090909090909090947aaaa540b09090909090909090909090909090909090909090909090909090909090909fae0e0f2f22164e0e0f273e07fefefef7d6dfafafafa6d6d6d6d'; +} {42} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 2.0 { + CREATE VIRTUAL TABLE t0 USING fts3(a); + INSERT INTO t0_segdir VALUES(0,0,0,0,'0 42',X'000131030782000103323334050100fff200010461616161050101020200000462626262050101030200'); +} +do_execsql_test 2.1 { + SELECT count(*) FROM t0 WHERE t0 MATCH '(1 NEAR 1) AND (aaaa OR 1)'; +} 1 + +#------------------------------------------------------------------------- +reset_db +breakpoint +do_catchsql_test 3.0 { + CREATE VIRTUAL TABLE main.Table0 USING fts3(); + INSERT INTO Table0 VALUES (1), (printf('%8.1280000X') ), (1), (printf('%8.1280000X') ), (1) ; + INSERT INTO Table0 VALUES (0), (printf('%8.1280000X%8.1280000X') ), (1), (printf('%1280000.1280000X%#1280000.1280000E%8.1280000X') ), (1) ; + INSERT INTO Table0 VALUES (1) ; + UPDATE Table0_segdir SET start_block = 1; + INSERT INTO Table0 VALUES (1) ; + INSERT INTO Table0(Table0) VALUES('merge=6,8'); +} {1 {database disk image is malformed}} + +set sqlite_fts3_enable_parentheses $saved_sqlite_fts3_enable_parentheses +finish_test diff --git a/test/fts3corrupt7.test b/test/fts3corrupt7.test new file mode 100644 index 0000000000..6cf9c9a9dc --- /dev/null +++ b/test/fts3corrupt7.test @@ -0,0 +1,280 @@ +# 2024 November 7 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# This file implements regression tests for SQLite library. The +# focus of this script is testing the FTS3 module. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/fts3_common.tcl +set testprefix fts3corrupt7 + +# If SQLITE_ENABLE_FTS3 is defined, omit this file. +ifcapable !fts3 { + finish_test + return +} + +sqlite3_fts3_may_be_corrupt 1 +database_may_be_corrupt +extra_schema_checks 0 + +#------------------------------------------------------------------------- +reset_db +do_test 1.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 28672 pagesize 4096 filename x.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 04 00 00 00 07 .....@ ........ +| 32: 00 00 00 02 00 00 00 01 00 00 00 04 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 04 ................ +| 96: 00 2e 82 40 0d 0e b1 00 06 0d a4 00 0f 8d 0f 21 ...@...........! +| 112: 0e b9 0d c8 0e 7e 0d a4 7d a4 00 00 00 00 00 00 .....~.......... +| 2512: 00 00 00 00 00 00 00 00 96 00 00 00 00 00 00 00 ................ +| 3488: 00 00 00 00 22 07 06 17 11 11 01 31 74 61 62 6c ...........1tabl +| 3504: 65 74 32 74 32 07 43 52 45 41 54 45 20 54 41 42 et2t2.CREATE TAB +| 3520: 4c 45 20 74 32 28 78 29 81 33 05 07 17 1f 1f 01 LE t2(x).3...... +| 3536: 82 35 74 61 62 6c 65 74 31 5f 73 65 67 64 69 72 .5tablet1_segdir +| 3552: 74 31 5f 73 65 67 64 69 72 05 43 52 45 41 54 45 t1_segdir.CREATE +| 3568: 20 54 41 42 4c 45 20 27 74 31 5f 73 65 67 64 69 TABLE 't1_segdi +| 3584: 72 27 28 6c 65 76 65 6c 20 49 4e 54 45 47 45 52 r'(level INTEGER +| 3600: 2c 69 64 78 20 49 4e 54 45 47 45 52 2c 73 74 61 ,idx INTEGER,sta +| 3616: 72 74 5f 62 6c 6f 63 6b 20 49 4e 54 45 47 45 52 rt_block INTEGER +| 3632: 2c 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 ,leaves_end_bloc +| 3648: 6b 20 49 4e 54 45 47 45 52 2c 65 6e 64 5f 62 6c k INTEGER,end_bl +| 3664: 6f 63 6b 20 49 4e 54 45 47 45 52 2c 72 6f 6f 74 ock INTEGER,root +| 3680: 20 42 4c 4f 42 2c 50 52 49 4d 41 52 59 20 4b 45 BLOB,PRIMARY KE +| 3696: 59 28 6c 65 76 65 6c 2c 20 69 64 78 29 29 31 06 Y(level, idx))1. +| 3712: 06 17 45 1f 01 00 69 6e 64 65 78 73 71 6c 69 74 ..E...indexsqlit +| 3728: 65 5f 61 75 74 6f 69 6e 64 65 78 5f 74 31 5f 73 e_autoindex_t1_s +| 3744: 65 67 64 69 72 5f 31 74 31 5f 73 65 67 64 69 72 egdir_1t1_segdir +| 3760: 06 0f c7 00 08 00 00 00 00 66 04 07 17 23 23 01 .........f...##. +| 3776: 81 13 74 61 62 6c 65 74 31 5f 73 65 67 6d 65 6e ..tablet1_segmen +| 3792: 74 73 74 31 5f 73 65 67 6d 65 6e 74 73 04 43 52 tst1_segments.CR +| 3808: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 73 EATE TABLE 't1_s +| 3824: 65 67 6d 65 6e 74 73 27 28 62 6c 6f 63 6b 69 64 egments'(blockid +| 3840: 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 INTEGER PRIMARY +| 3856: 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 KEY, block BLOB +| 3872: 29 6a 03 07 17 21 21 01 81 1f 74 61 62 6c 65 74 )j...!!...tablet +| 3888: 31 5f 63 6f 6e 74 65 6e 74 74 31 5f 63 6f 6e 74 1_contentt1_cont +| 3904: 65 6e 74 03 43 52 45 41 54 45 20 54 41 42 4c 45 ent.CREATE TABLE +| 3920: 20 27 74 31 5f 63 6f 6e 74 65 6e 74 27 28 64 6f 't1_content'(do +| 3936: 63 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d cid INTEGER PRIM +| 3952: 41 52 59 20 4b 45 59 2c 20 27 63 30 61 27 2c 20 ARY KEY, 'c0a', +| 3968: 27 63 31 62 27 2c 20 27 63 32 63 27 29 38 02 06 'c1b', 'c2c')8.. +| 3984: 17 11 11 08 5f 74 61 62 6c 65 74 31 74 31 43 52 ...._tablet1t1CR +| 4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 33 LE t1 USING fts3 +| 4032: 28 61 2c 62 2c 63 29 00 00 00 39 00 00 00 00 00 (a,b,c)...9..... +| page 2 offset 4096 +| 0: 01 00 00 00 00 01 00 00 00 00 01 00 00 00 00 01 ................ +| 16: 00 00 00 00 02 00 00 00 00 05 00 00 00 03 02 00 ................ +| 32: 00 00 00 05 00 00 00 03 02 00 00 00 00 05 00 00 ................ +| 48: 00 03 02 00 00 00 00 05 00 00 00 03 02 00 00 00 ................ +| 64: 00 05 00 00 00 03 02 00 00 00 00 05 00 00 00 03 ................ +| 80: 02 00 00 00 00 05 00 00 00 03 02 00 00 00 00 05 ................ +| 96: 00 00 00 03 02 00 00 00 00 05 00 00 00 03 05 00 ................ +| 112: 00 00 03 03 00 00 00 23 02 00 00 00 00 03 00 00 .......#........ +| 128: 00 23 02 00 00 00 00 03 00 00 4d 5a 14 00 ae 7c .#........MZ...| +| 1088: 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 09 ................ +| page 3 offset 8192 +| 0: 0d 00 00 00 25 0b 48 00 0f d8 0f af 0f 86 0f 74 ....%.H........t +| 16: 0f 61 0f 4e 0f 2f 0f 0f 0e ef 0e d7 0e be 0e a5 .a.N./.......... +| 32: 0e 8d 0e 74 0e 5b 0e 40 0e 24 0e 08 0d ef 0d d5 ...t.[.@.$...... +| 48: 0d bb 0d a0 0d 84 0d 68 0d 4f 0d 35 0d 1b 0c fb .......h.O.5.... +| 64: 0c da 0c b9 0c 99 0c 78 0c 57 0c 3e 0c 24 0c 0a .......x.W.>.$.. +| 80: 0b 48 00 00 00 00 00 00 00 00 00 00 00 00 00 00 .H.............. +| 2880: 00 00 00 00 00 00 00 00 81 3f 25 06 00 82 7f 00 .........?%..... +| 2896: 00 43 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2e .COMPILER=gcc-5. +| 2912: 34 2e 30 20 32 30 31 36 30 36 30 39 20 44 45 42 4.0 20160609 DEB +| 2928: 55 47 20 45 4e 41 42 4c 45 20 44 42 53 54 41 54 UG ENABLE DBSTAT +| 2944: 20 56 54 41 42 20 45 4e 41 42 4c 45 20 46 54 53 VTAB ENABLE FTS +| 2960: 34 20 45 4e 41 42 4c 45 20 46 54 53 35 20 45 4e 4 ENABLE FTS5 EN +| 2976: 41 42 4c 45 20 47 45 4f 50 4f 4c 59 20 45 4e 41 ABLE GEOPOLY ENA +| 2992: 42 4c 45 20 4a 53 4f 4e 31 20 45 4e 41 42 4c 45 BLE JSON1 ENABLE +| 3008: 20 4d 45 4d 53 59 53 35 20 45 4e 41 42 4c 45 20 MEMSYS5 ENABLE +| 3024: 52 54 52 45 45 20 4d 41 58 20 4d 45 4d 4f 52 59 RTREE MAX MEMORY +| 3040: 3d 35 30 30 30 30 30 30 30 20 4f 4d 49 54 20 4c =50000000 OMIT L +| 3056: 4f 41 44 20 45 58 54 45 4e 53 49 4f 4e 20 54 48 OAD EXTENSION TH +| 3072: 52 45 41 44 53 41 46 45 3d 30 18 24 05 00 25 0f READSAFE=0.$..%. +| 3088: 19 54 48 52 45 41 44 53 41 46 45 3d 30 58 42 49 .THREADSAFE=0XBI +| 3104: 4e 41 52 59 18 23 05 00 25 0f 19 54 48 52 45 41 NARY.#..%..THREA +| 3120: 44 53 41 46 45 3d 30 58 4e 4f 43 41 53 45 17 22 DSAFE=0XNOCASE.. +| 3136: 05 00 25 0f 17 54 48 52 45 41 44 53 41 46 45 3d ..%..THREADSAFE= +| 3152: 30 58 52 54 52 49 4d 1f 21 05 00 33 0f 19 4f 4d 0XRTRIM.!..3..OM +| 3168: 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 4f IT LOAD EXTENSIO +| 3184: 4e 58 42 49 4e 41 52 59 1f 20 05 00 33 0f 19 4f NXBINARY. ..3..O +| 3200: 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 MIT LOAD EXTENSI +| 3216: 4f 4e 58 4e 4f 43 41 53 45 1e 1f 05 00 33 0f 17 ONXNOCASE....3.. +| 3232: 4f 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 OMIT LOAD EXTENS +| 3248: 49 4f 4e 58 52 54 52 49 4d 1f 1e 05 00 33 00 00 IONXRTRIM....3.. +| 4016: 54 41 42 4c 45 20 74 31 28 61 20 49 4e 54 45 47 TABLE t1(a INTEG +| 4032: 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 41 ER PRIMARY KEY A +| 4048: 55 54 4f 49 4e 43 52 45 4d 45 4e 54 2c 0a 62 2c UTOINCREMENT,.b, +| 4064: 63 2c 64 2c 65 2c 66 2c 67 2c 68 2c 6a 2c 6b 2c c,d,e,f,g,h,j,k, +| 4080: 6c 2c 6d 2c 6e 2c 6f 2c 70 2c 71 2c 72 2c 73 29 l,m,n,o,p,q,r,s) +| page 5 offset 16384 +| 0: 0d 00 00 00 02 0b a0 00 0c ad 0b a0 00 00 00 00 ................ +| 2976: 82 0a 02 08 08 09 08 08 17 84 06 30 20 32 35 33 ...........0 253 +| 2992: 00 01 30 04 25 06 1b 00 00 08 32 30 31 36 30 36 ..0.%.....201606 +| 3008: 30 39 03 25 07 00 00 01 34 03 25 05 00 00 01 35 09.%....4.%....5 +| 3024: 03 25 04 00 01 07 30 30 30 30 30 30 30 03 25 1a .%....0000000.%. +| 3040: 00 00 08 63 6f 6d 70 69 6c 65 72 03 25 02 00 00 ...compiler.%... +| 3056: 06 64 62 73 74 61 74 03 25 0a 00 01 04 65 62 75 .dbstat.%....ebu +| 3072: 67 03 25 08 00 00 06 65 6e 61 62 6c 65 09 25 09 g.%....enable.%. +| 3088: 05 04 04 04 04 04 00 01 08 78 74 65 6e 73 69 6f .........xtensio +| 3104: 6e 03 25 1d 00 00 04 66 74 73 34 03 25 0d 00 03 n.%....fts4.%... +| 3120: 01 35 03 25 0f 00 00 03 67 63 63 03 25 03 00 01 .5.%....gcc.%... +| 3136: 06 65 6f 70 6f 6c 79 03 25 11 00 00 05 6a 73 6f .eopoly.%....jso +| 3152: 6e 31 03 25 13 00 00 04 6c 6f 61 64 03 25 1c 00 n1.%....load.%.. +| 3168: 00 03 6d 61 78 03 25 18 00 01 05 65 6d 6f 72 79 ..max.%....emory +| 3184: 03 25 19 00 03 04 73 79 73 35 03 25 15 00 00 04 .%....sys5.%.... +| 3200: 6f 6d 69 74 03 25 1b 00 00 05 72 74 72 65 65 03 omit.%....rtree. +| 3216: 25 01 00 d0 0a 07 68 72 65 61 64 73 61 66 65 03 %.....hreadsafe. +| 3232: 25 1e 00 00 04 76 74 61 62 03 25 0b 00 86 50 01 %....vtab.%...P. +| 3248: 08 08 08 08 08 17 8d 12 30 20 38 33 35 00 01 30 ........0 835..0 +| 3264: 12 01 06 00 01 06 00 01 06 00 1f 03 00 01 03 00 ................ +| 3280: 01 03 00 00 08 32 30 31 36 30 36 30 39 09 01 07 .....20160609... +| 3296: 00 01 07 00 01 07 00 00 01 34 09 01 05 00 01 05 .........4...... +| 3312: 00 01 05 00 00 01 35 09 01 04 00 01 04 00 01 04 ......5......... +| 3328: 00 01 07 30 30 30 30 30 30 30 09 1c 04 00 01 04 ...0000000...... +| 3344: 00 01 04 00 00 06 62 69 6e 61 72 79 3c 03 01 02 ......binary<... +| 3360: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3376: 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 ................ +| 3392: 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03 ................ +| 3408: 01 02 02 00 03 01 02 02 00 00 08 63 6f 6d 70 69 ...........compi +| 3424: 6c 65 72 09 01 02 00 01 02 00 01 02 00 00 06 64 ler............d +| 3440: 62 73 74 61 74 09 07 03 00 01 03 00 01 03 00 01 bstat........... +| 3456: 04 65 62 75 67 09 04 02 00 01 02 00 01 02 00 00 .ebug........... +| 3472: 06 65 6e 61 62 6c 65 3f 07 02 00 01 02 00 01 02 .enable?........ +| 3488: 00 01 02 00 01 02 00 01 02 00 01 02 00 01 02 00 ................ +| 3504: 01 02 00 01 02 00 01 02 00 01 02 00 01 02 00 01 ................ +| 3520: 02 00 01 02 00 01 02 00 01 02 00 01 02 00 01 02 ................ +| 3536: 00 01 02 00 01 02 00 01 08 78 74 65 6e 73 69 6f .........xtensio +| 3552: 6e 09 1f 04 00 01 04 00 01 04 00 00 04 66 74 73 n............fts +| 3568: 34 09 0a 03 00 01 03 00 01 03 00 03 01 35 09 0d 4............5.. +| 3584: 03 00 01 03 00 01 03 00 00 03 67 63 63 09 01 03 ..........gcc... +| 3600: 00 01 03 00 01 03 00 01 06 65 6f 70 6f 6c 79 09 .........eopoly. +| 3616: 10 03 00 01 03 00 01 03 00 00 05 6a 73 6f 6e 31 ...........json1 +| 3632: 09 13 03 00 01 03 01 01 03 00 00 04 6c 6f 61 64 ............load +| 3648: 09 1f 03 00 01 03 00 01 03 00 00 03 6d 61 78 09 ............max. +| 3664: 1c 02 00 01 02 00 01 02 00 01 05 65 6d 6f 72 79 ...........emory +| 3680: 09 1c 03 00 01 03 00 01 03 00 03 04 73 79 73 35 ............sys5 +| 3696: 09 16 03 00 01 03 00 01 03 00 00 06 6e 6f 63 61 ............noca +| 3712: 73 65 3c 02 01 02 02 00 03 01 02 02 00 03 01 02 se<............. +| 3744: 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 ................ +| 3760: 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 00 ................ +| 3776: 04 6f 6d 69 74 09 1f 02 00 01 02 00 01 02 00 00 .omit........... +| 3792: 05 72 74 72 65 65 09 19 03 00 01 03 00 01 03 00 .rtree.......... +| 3808: 03 02 69 6d 3c 01 01 02 02 00 03 01 02 02 00 03 ..im<........... +| 3824: 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01 ................ +| 3840: 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 ................ +| 3856: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 ................ +| 3872: 00 00 0a 74 68 72 65 61 64 73 61 66 65 09 22 02 ...threadsafe... +| 3888: 00 01 02 00 01 02 00 00 04 76 74 61 62 09 07 04 .........vtab... +| 3952: 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 ................ +| 3968: 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 ................ +| 3984: 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 ................ +| 4000: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| 4032: 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 ................ +| 4048: 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 ................ +| 4064: 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 ................ +| 4080: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 ................ +| page 6 offset 20480 +| 0: 0a 00 00 00 02 0f f5 00 0f fb 0f f5 00 00 00 00 ................ +| 4080: 00 00 00 00 00 05 04 08 09 01 02 04 04 08 08 09 ................ +| end x.db +}]} {} + +do_catchsql_test 1.1 { + SELECT offsets(t1) FROM t1 WHERE t1 MATCH 'rtree NEAR rtree NEAR "json1 enable"'; +} {0 {}} + +#------------------------------------------------------------------------- +reset_db +do_test 1.0 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 24576 pagesize 4096 filename crash-10b0f1037e9c85.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 00 00 00 40 20 20 00 00 00 01 00 00 00 07 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 ................ +| 96: 00 2e 82 40 0d 00 00 00 06 00 00 00 0f 8d 0f 21 ...@...........! +| 112: 0e b9 0d c8 0e 7e 0d a4 00 00 00 00 00 00 00 00 .....~.......... +| 3488: 00 00 00 00 21 ff 06 17 10 10 01 30 74 61 62 6c ....!......0tabl +| 3504: 65 74 32 74 32 00 43 52 45 41 54 45 20 54 41 42 et2t2.CREATE TAB +| 3520: 4c 45 20 74 32 28 70 29 81 33 00 07 17 1f 1f 01 LE t2(p).3...... +| 3536: 82 35 74 61 62 6c 65 74 31 5f 73 65 67 64 69 72 .5tablet1_segdir +| 3552: 74 31 5f 73 65 67 64 69 72 05 43 52 45 41 54 45 t1_segdir.CREATE +| 3568: 20 54 41 42 4c 45 20 27 74 31 5f 73 65 67 64 69 TABLE 't1_segdi +| 3584: 72 27 28 6c 65 76 65 6c 20 09 4e 50 45 47 45 50 r'(level .NPEGEP +| 3600: 2c 69 64 78 20 09 4e 50 45 47 45 50 2c 73 74 61 ,idx .NPEGEP,sta +| 3616: 72 74 5f 62 6c 6f 63 6b 20 09 4e 50 45 47 45 50 rt_block .NPEGEP +| 3632: 2c 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 ,leaves_end_bloc +| 3648: 6b 20 09 4e 50 45 47 45 50 2c 65 6e 64 5f 62 6c k .NPEGEP,end_bl +| 3664: 6f 63 6b 20 09 4e 50 45 47 45 50 2c 72 6f 6f 74 ock .NPEGEP,root +| 3680: 20 42 0c 4f 42 2c 50 52 49 4d 41 52 59 20 4b 45 B.OB,PRIMARY KE +| 3696: 59 28 6c 65 76 65 6c 2c 20 69 64 78 29 29 31 00 Y(level, idx))1. +| 3712: 06 17 45 1f 01 00 00 00 00 00 00 73 71 6c 69 74 ..E........sqlit +| 3728: 65 5f 61 75 74 6f 69 6e 64 65 78 5f 74 31 5f 73 e_autoindex_t1_s +| 3744: 65 67 64 69 72 5f 31 00 00 00 00 00 00 00 00 00 egdir_1......... +| 3760: 06 00 00 00 00 00 00 00 00 66 00 07 17 23 23 01 .........f...##. +| 3776: 81 13 74 61 62 6c 65 74 31 5f 73 65 67 6d 65 6e ..tablet1_segmen +| 3792: 74 73 74 31 5f 73 65 67 6d 65 6e 74 73 00 43 52 tst1_segments.CR +| 3808: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 73 EATE TABLE 't1_s +| 3824: 65 67 6d 65 6e 74 73 27 28 0c 6f 63 6b 09 64 0a egments'(.ock.d. +| 3840: 20 09 4e 50 45 47 45 50 20 50 50 09 04 31 50 09 .NPEGEP PP..1P. +| 3856: 20 0b 45 09 0c 20 62 0c 6f 63 6b 20 42 0c 4f 42 .E.. b.ock B.OB +| 3872: 29 6a 00 07 17 20 20 01 81 1f 74 61 62 6c 65 74 )j... ...tablet +| 3888: 31 5f 63 6f 6e 74 65 6e 74 74 31 5f 63 6f 6e 74 1_contentt1_cont +| 3904: 65 6e 74 00 43 52 45 41 54 45 20 54 41 42 4c 45 ent.CREATE TABLE +| 3920: 20 27 74 31 5f 63 6f 6e 74 65 6e 74 27 28 64 6f 't1_content'(do +| 3936: 09 64 20 09 4e 50 45 47 45 50 20 50 50 09 0d 0c .d .NPEGEP PP... +| 3952: 50 09 20 0b 45 09 0c 20 27 03 03 01 27 0c 20 0a P. .E.. '...'. . +| 3968: 27 03 01 02 27 0c 20 27 03 02 03 27 29 38 00 06 '...'. '...')8.. +| 3984: 17 10 10 08 5f 74 61 62 6c 65 74 31 74 31 43 52 ...._tablet1t1CR +| 4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 33 LE t1 USING fts3 +| page 5 offset 16384 +| 0: 0d 00 00 00 02 00 00 00 00 00 0b a0 00 00 00 00 ................ +| 2976: 82 0a 02 08 00 00 00 00 17 84 06 00 00 00 00 00 ................ +| 2992: 00 01 00 04 00 00 00 00 00 08 00 00 00 00 00 00 ................ +| 3008: 00 00 03 00 00 00 00 01 00 03 00 00 00 00 01 00 ................ +| 3024: 03 00 00 00 00 07 00 00 00 00 00 00 00 03 00 00 ................ +| 3040: 00 00 08 00 00 00 00 00 00 00 00 03 00 00 00 00 ................ +| 3056: 06 00 00 00 00 00 00 03 00 00 00 00 04 00 00 00 ................ +| 3072: 00 03 00 00 00 00 06 65 6e 61 62 6c 65 09 25 09 .......enable.%. +| 3088: 05 04 04 04 04 00 00 00 08 00 00 00 00 00 00 00 ................ +| 3104: 00 03 00 00 00 00 04 00 00 00 00 03 00 00 00 00 ................ +| 3120: 01 00 03 00 00 00 00 03 00 00 00 03 00 00 00 00 ................ +| 3136: 06 00 00 00 00 00 00 03 00 00 00 00 05 6a 73 6f .............jso +| 3152: 6e 31 03 25 13 00 00 04 00 00 00 00 03 00 00 00 n1.%............ +| 3168: 00 03 00 00 00 03 00 00 00 00 05 00 00 00 00 00 ................ +| 3184: 03 00 00 00 00 04 00 00 00 00 03 00 00 00 00 04 ................ +| 3200: 00 00 00 00 03 00 00 00 00 05 72 74 72 65 65 03 ..........rtree. +| 3216: 25 01 00 0d 0a 07 08 01 ff ff ff ff ff 01 00 00 %............... +| page 6 offset 20480 +| 0: 0a 00 00 00 02 00 00 00 0f fb 0f f5 00 00 00 00 ................ +| 4080: 00 00 00 00 00 05 04 09 00 01 02 04 00 00 00 00 ................ +| end crash-10b0f1037e9c85.db +}]} {} + +do_catchsql_test 2.1 { + SELECT 0 FROM t1 WHERE t1 MATCH 'rtree NEAR rtree"json1 enable"'; +} {1 {database disk image is malformed}} + +finish_test diff --git a/test/fts3cov.test b/test/fts3cov.test index c43999d2da..d01791bbe5 100644 --- a/test/fts3cov.test +++ b/test/fts3cov.test @@ -25,7 +25,7 @@ set testprefix fts3cov # When it first needs to read a block from the %_segments table, the FTS3 # module compiles an SQL statement for that purpose. The statement is # stored and reused each subsequent time a block is read. This test case -# tests the effects of an OOM error occuring while compiling the statement. +# tests the effects of an OOM error occurring while compiling the statement. # # Similarly, when FTS3 first needs to scan through a set of segment leaves # to find a set of documents that matches a term, it allocates a string @@ -89,6 +89,7 @@ do_test fts3cov-2.1 { } {03} # Test the "missing entry" case: +sqlite3_db_config db DEFENSIVE 0 do_test fts3cov-2.2 { set root [db one {SELECT root FROM t1_segdir}] read_fts3varint [string range $root 1 end] left_child @@ -96,7 +97,7 @@ do_test fts3cov-2.2 { } {} do_error_test fts3cov-2.3 { SELECT * FROM t1 WHERE t1 MATCH 'c*' -} {SQL logic error or missing database} +} {database disk image is malformed} # Test the "replaced with NULL" case: do_test fts3cov-2.4 { @@ -104,7 +105,7 @@ do_test fts3cov-2.4 { } {} do_error_test fts3cov-2.5 { SELECT * FROM t1 WHERE t1 MATCH 'cloud' -} {SQL logic error or missing database} +} {database disk image is malformed} #-------------------------------------------------------------------------- # The following tests are to test the effects of OOM errors while storing @@ -276,7 +277,7 @@ do_test fts3cov-7.2 { # pending-terms table must be flushed each time a document with a docid # less than or equal to the previous docid is modified. # -# This test checks the effects of an OOM error occuring when the +# This test checks the effects of an OOM error occurring when the # pending-terms table is flushed for this reason as part of a DELETE # statement. # @@ -308,13 +309,13 @@ do_test fts3cov-9.1 { } {} do_error_test fts3cov-9.2 { INSERT INTO xx(xx) VALUES('optimise'); -- British spelling -} {SQL logic error or missing database} +} {SQL logic error} do_error_test fts3cov-9.3 { INSERT INTO xx(xx) VALUES('short'); -} {SQL logic error or missing database} +} {SQL logic error} do_error_test fts3cov-9.4 { INSERT INTO xx(xx) VALUES('waytoolongtobecorrect'); -} {SQL logic error or missing database} +} {SQL logic error} do_test fts3cov-9.5 { execsql { INSERT INTO xx(xx) VALUES('optimize') } } {} @@ -405,6 +406,7 @@ do_execsql_test 15.1 { # Test a corruption case. # +sqlite3_db_config db DEFENSIVE 0 do_execsql_test 16.1 { CREATE VIRTUAL TABLE t16 USING fts4; INSERT INTO t16 VALUES('theoretical work to examine the relationship'); diff --git a/test/fts3d.test b/test/fts3d.test index 83af4d2a37..b0547ce7ab 100644 --- a/test/fts3d.test +++ b/test/fts3d.test @@ -296,6 +296,7 @@ do_test fts3d-5.0 { } {{Index already optimal} 1 0} # Even if we move things around, still does nothing. +sqlite3_db_config db DEFENSIVE 0 do_test fts3d-5.1 { execsql { UPDATE t1_segdir SET level = 2 WHERE level = 1 AND idx = 0; diff --git a/test/fts3defer.test b/test/fts3defer.test index 532d4dfa6a..5d8395afc1 100644 --- a/test/fts3defer.test +++ b/test/fts3defer.test @@ -59,6 +59,7 @@ set tests { do_select_tests 1.2 $tests +sqlite3_db_config db DEFENSIVE 0 do_execsql_test 1.3 { SELECT count(*) FROM t1_segments WHERE length(block)>10000; UPDATE t1_segments @@ -225,6 +226,7 @@ foreach {tn setup} { execsql { CREATE VIRTUAL TABLE t1 USING FTS4 } foreach doc $data { execsql { INSERT INTO t1 VALUES($doc) } } add_empty_records 1000 + sqlite3_db_config db DEFENSIVE 0 execsql $zero_long_doclists } 4 { @@ -233,6 +235,7 @@ foreach {tn setup} { foreach doc $data { execsql { INSERT INTO t1 VALUES($doc) } } add_empty_records 1000 execsql "INSERT INTO t1(t1) VALUES('optimize')" + sqlite3_db_config db DEFENSIVE 0 execsql $zero_long_doclists } 5 { @@ -240,6 +243,7 @@ foreach {tn setup} { execsql { CREATE VIRTUAL TABLE t1 USING FTS4(matchinfo=fts3) } foreach doc $data { execsql { INSERT INTO t1 VALUES($doc) } } add_empty_records 1000 + sqlite3_db_config db DEFENSIVE 0 execsql $zero_long_doclists } } { diff --git a/test/fts3defer2.test b/test/fts3defer2.test index 275e0f0eb3..51d2afc49f 100644 --- a/test/fts3defer2.test +++ b/test/fts3defer2.test @@ -46,6 +46,7 @@ do_execsql_test 1.1.4 { INSERT INTO t1 VALUES(''); INSERT INTO t1(t1) VALUES('optimize'); } +sqlite3_db_config db DEFENSIVE 0 do_execsql_test 1.1.4 { SELECT count(*) FROM t1_segments WHERE length(block)>10000; UPDATE t1_segments SET block = zeroblob(length(block)) WHERE length(block)>10000; @@ -98,6 +99,7 @@ foreach {tn sql} { WHERE length(block)>10000; } } { + sqlite3_db_config db DEFENSIVE 0 execsql $sql do_execsql_test 2.2.$tn.1 { @@ -152,11 +154,40 @@ foreach {tn sql} { WHERE length(block)>10000; } } { + sqlite3_db_config db DEFENSIVE 0 execsql $sql do_execsql_test 2.4.$tn { SELECT docid, mit(matchinfo(t3, 'pcxnal')) FROM t3 WHERE t3 MATCH '"a b c"'; } {1 {1 1 1 4 4 11 912 6} 3 {1 1 1 4 4 11 912 6}} } +do_execsql_test 2.5 { + INSERT INTO t3(t3) VALUES('rebuild'); +} +do_execsql_test 2.6 { + SELECT rowid, length(offsets(t3)) FROM t3 WHERE t3 MATCH '(a NEAR a)'; +} {11 228929} +do_execsql_test 2.7 { + SELECT rowid, length(offsets(t3)) FROM t3 WHERE t3 MATCH '(a NEAR b NEAR a)'; +} {1 23 3 23 11 205} +do_execsql_test 2.8 { + SELECT rowid, length(offsets(t3)) FROM t3 WHERE t3 MATCH '(a NEAR b)'; +} {1 15 3 15 11 106} + +do_execsql_test 2.9 { + SELECT rowid, length(matchinfo(t3)) FROM t3 WHERE t3 MATCH '(a NEAR a)'; +} {11 32} +do_execsql_test 2.10 { + SELECT rowid, length(matchinfo(t3)) FROM t3 WHERE t3 MATCH '(a NEAR b NEAR a)' +} {1 44 3 44 11 44} +do_execsql_test 2.11 { + SELECT rowid, length(matchinfo(t3)) FROM t3 WHERE t3 MATCH '(a NEAR b)'; +} {1 32 3 32 11 32} + +do_execsql_test 2.12 { + SELECT rowid, length(matchinfo(t3)) FROM t3 + WHERE t3 MATCH '(a NEAR b NEAR a NEAR b NEAR a)' +} {1 68 3 68 11 68} + finish_test diff --git a/test/fts3dropmod.test b/test/fts3dropmod.test new file mode 100644 index 0000000000..d6b1677ac2 --- /dev/null +++ b/test/fts3dropmod.test @@ -0,0 +1,44 @@ +# 2021 December 16 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# This file implements regression tests for SQLite library. The +# focus of this script is testing the FTS3 module. +# +# $Id: fts3aa.test,v 1.1 2007/08/20 17:38:42 shess Exp $ +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix fts3dropmod + +# If SQLITE_ENABLE_FTS3 is defined, omit this file. +ifcapable !fts3 { + finish_test + return +} + +sqlite3_drop_modules db fts3 +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE t1 USING fts3(x); +} +do_catchsql_test 1.1 { + CREATE VIRTUAL TABLE t2 USING fts4(x); +} {1 {no such module: fts4}} + +reset_db +sqlite3_drop_modules db fts4 +do_execsql_test 2.0 { + CREATE VIRTUAL TABLE t1 USING fts4(x); +} +do_catchsql_test 2.1 { + CREATE VIRTUAL TABLE t2 USING fts3(x); +} {1 {no such module: fts3}} + +finish_test diff --git a/test/fts3expr.test b/test/fts3expr.test index 6e23faf633..2fd19201b6 100644 --- a/test/fts3expr.test +++ b/test/fts3expr.test @@ -409,7 +409,7 @@ do_test fts3expr-5.1 { } {1 {Usage: fts3_exprtest(tokenizer, expr, col1, ...}} do_test fts3expr-5.2 { catchsql { SELECT fts3_exprtest('doesnotexist', 'a b', 'c') } -} {1 {No such tokenizer module}} +} {1 {unknown tokenizer: doesnotexist}} do_test fts3expr-5.3 { catchsql { SELECT fts3_exprtest('simple', 'a b OR', 'c') } } {1 {Error parsing expression}} @@ -514,4 +514,8 @@ do_test fts3expr-9.1 { test_fts3expr "f (e NEAR/2 a)" } {AND {PHRASE 3 0 f} {NEAR/2 {PHRASE 3 0 e} {PHRASE 3 0 a}}} +do_test fts3expr-10.1 { test_fts3expr "abc *" } {PHRASE 3 0 abc} +do_test fts3expr-10.2 { test_fts3expr "*" } {} +do_test fts3expr-10.3 { test_fts3expr "abc*" } {PHRASE 3 0 abc+} + finish_test diff --git a/test/fts3expr2.test b/test/fts3expr2.test index c3d161730b..6fb133f17f 100644 --- a/test/fts3expr2.test +++ b/test/fts3expr2.test @@ -46,7 +46,7 @@ ifcapable !fts3 { # * Whether or not superflous parenthesis are included. i.e. if # "a OR b AND (c OR d)" or "a OR (b AND (c OR d))" is generated. # -# * Whether or not explict AND operators are used. i.e. if +# * Whether or not explicit AND operators are used. i.e. if # "a OR b AND c" or "a OR b c" is generated. # diff --git a/test/fts3expr4.test b/test/fts3expr4.test index 9fc22c1172..b9227aef55 100644 --- a/test/fts3expr4.test +++ b/test/fts3expr4.test @@ -29,7 +29,8 @@ proc test_fts3expr {tokenizer expr} { } proc do_icu_expr_test {tn expr res} { - uplevel [list do_test $tn [list test_fts3expr icu $expr] [list {*}$res]] + set res2 [list {*}$res] + uplevel [list do_test $tn [list test_fts3expr "icu en_US" $expr] $res2] } proc do_simple_expr_test {tn expr res} { @@ -49,7 +50,16 @@ do_icu_expr_test 1.6 { "(x OR y)" } {PHRASE 3 0 ( x or y )} # is passed to the tokenizer. # do_icu_expr_test 1.7 {a:word} {PHRASE 0 0 word} -do_icu_expr_test 1.8 {d:word} {PHRASE 3 0 d:word} +# do_icu_expr_test 1.8 {d:word} {PHRASE 3 0 d:word} +do_test 1.8 { + set res [ + db one {SELECT fts3_exprtest('icu en_US', 'd:word', 'a', 'b', 'c')} + ] + expr { + $res=="PHRASE 3 0 d:word" || + $res=="AND {AND {PHRASE 3 0 d} {PHRASE 3 0 :}} {PHRASE 3 0 word}" + } +} 1 set sqlite_fts3_enable_parentheses 0 @@ -67,7 +77,7 @@ do_icu_expr_test 3.2 {*lOl* *h4h*} { AND {AND {AND {PHRASE 3 0 *} {PHRASE 3 0 lol+}} {PHRASE 3 0 *}} {PHRASE 3 0 h4h+} } -do_simple_expr_test 3.3 { * } { } +do_simple_expr_test 3.3 { * } {} do_simple_expr_test 3.4 { *a } { PHRASE 3 0 a } do_simple_expr_test 3.5 { a*b } { AND {PHRASE 3 0 a+} {PHRASE 3 0 b} } do_simple_expr_test 3.6 { *a*b } { AND {PHRASE 3 0 a+} {PHRASE 3 0 b} } diff --git a/test/fts3expr5.test b/test/fts3expr5.test index 1e0985108e..1317befb1c 100644 --- a/test/fts3expr5.test +++ b/test/fts3expr5.test @@ -22,6 +22,10 @@ ifcapable !fts3 { return } +proc test_fts3expr {expr} { + db one {SELECT fts3_exprtest('simple', $expr, 'a', 'b', 'c')} +} + #------------------------------------------------------------------------- # Various forms of empty phrase expressions. # @@ -45,4 +49,18 @@ do_execsql_test 1.5 { SELECT rowid FROM t0 WHERE x MATCH '""""'; } {} +#------------------------------------------------------------------------- +# Various forms of empty phrase expressions. +# +set sqlite_fts3_enable_parentheses 1 +do_test 2.0 { + test_fts3expr {(a:123)(b:234)()(c:456)} +} {AND {AND {PHRASE 0 0 123} {PHRASE 1 0 234}} {PHRASE 2 0 456}} +do_test 2.1 { + test_fts3expr {(a:123)(b:234)(c:456)} +} {AND {AND {PHRASE 0 0 123} {PHRASE 1 0 234}} {PHRASE 2 0 456}} +do_test 2.2 { + list [catch { test_fts3expr {"123" AND ( )} } msg] $msg +} {1 {Error parsing expression}} + finish_test diff --git a/test/fts3f.test b/test/fts3f.test new file mode 100644 index 0000000000..d9a57cbc30 --- /dev/null +++ b/test/fts3f.test @@ -0,0 +1,57 @@ +# 2006 September 9 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# This file implements regression tests for SQLite library. The +# focus of this script is testing the FTS3 module. +# +# $Id: fts3aa.test,v 1.1 2007/08/20 17:38:42 shess Exp $ +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix fts3f + +# If SQLITE_ENABLE_FTS3 is defined, omit this file. +ifcapable !fts3 { + finish_test + return +} + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE ft USING fts3(x); + BEGIN; + INSERT INTO ft VALUES('a one'), ('b one'), ('c one'); +} + +do_test 1.1 { + set ret [list] + db eval { SELECT docid FROM ft WHERE ft MATCH 'one' } { + if { $docid==2 } { + db eval COMMIT + } + lappend ret $docid + } + set ret +} {1 2 3} + +do_execsql_test 1.2 { + BEGIN; + INSERT INTO ft VALUES('a one'), ('b one'), ('c one'); +} + +do_execsql_test 1.3 { + SELECT docid, optimize(ft) FROM ft WHERE ft MATCH 'one' +} { + 1 {Index optimized} 2 {Index already optimal} 3 {Index already optimal} + 4 {Index already optimal} + 5 {Index already optimal} 6 {Index already optimal} +} + +finish_test diff --git a/test/fts3fault.test b/test/fts3fault.test index 7d94332059..20e5f25de5 100644 --- a/test/fts3fault.test +++ b/test/fts3fault.test @@ -18,7 +18,9 @@ set ::testprefix fts3fault # If SQLITE_ENABLE_FTS3 is not defined, omit this file. ifcapable !fts3 { finish_test ; return } -if 0 { +set ::TMPDBERROR [list 1 \ + {unable to open a temporary database file for storing temporary tables} +] # Test error handling in the sqlite3Fts3Init() function. This is the # function that registers the FTS3 module and various support functions @@ -51,7 +53,7 @@ do_faultsim_test 2 -prep { } -body { execsql { ALTER TABLE t1 RENAME TO t2 } } -test { - faultsim_test_result {0 {}} + faultsim_test_result {0 {}} $::TMPDBERROR } # Test error handling in the special case where a single prefix query @@ -147,7 +149,7 @@ do_faultsim_test 7.2 -prep { } -test { faultsim_test_result {1 {unrecognized matchinfo: fs3}} \ {1 {vtable constructor failed: t1}} \ - {1 {SQL logic error or missing database}} + {1 {SQL logic error}} } do_faultsim_test 7.3 -prep { faultsim_delete_and_reopen @@ -156,12 +158,10 @@ do_faultsim_test 7.3 -prep { } -test { faultsim_test_result {1 {unrecognized parameter: matchnfo=fts3}} \ {1 {vtable constructor failed: t1}} \ - {1 {SQL logic error or missing database}} + {1 {SQL logic error}} } -} - proc mit {blob} { set scan(littleEndian) i* set scan(bigEndian) I* @@ -181,13 +181,15 @@ do_test 8.0 { faultsim_save_and_close } {} -do_faultsim_test 8.1 -faults oom-t* -prep { - faultsim_restore_and_reopen - db func mit mit -} -body { - execsql { SELECT mit(matchinfo(t8, 'x')) FROM t8 WHERE t8 MATCH 'a b c' } -} -test { - faultsim_test_result {0 {{1 1 1 1 4 2 1 5 5}}} +ifcapable fts4_deferred { + do_faultsim_test 8.1 -faults oom-t* -prep { + faultsim_restore_and_reopen + db func mit mit + } -body { + execsql { SELECT mit(matchinfo(t8, 'x')) FROM t8 WHERE t8 MATCH 'a b c' } + } -test { + faultsim_test_result {0 {{1 1 1 1 4 2 1 5 5}}} + } } do_faultsim_test 8.2 -faults oom-t* -prep { @@ -196,7 +198,7 @@ do_faultsim_test 8.2 -faults oom-t* -prep { } -body { execsql { SELECT mit(matchinfo(t8, 's')) FROM t8 WHERE t8 MATCH 'a b c' } } -test { - faultsim_test_result {0 3} + faultsim_test_result {0 3} $::TMPDBERROR } do_faultsim_test 8.3 -prep { faultsim_restore_and_reopen @@ -214,6 +216,14 @@ do_faultsim_test 8.4 -prep { } -test { faultsim_test_result {0 3} } +do_faultsim_test 8.5 -prep { + faultsim_restore_and_reopen + db func mit mit +} -body { + execsql { SELECT mit(matchinfo(t8, 'l')) FROM t8 WHERE t8 MATCH '"a b c"' } +} -test { + faultsim_test_result {0 3} +} do_test 9.0 { faultsim_delete_and_reopen @@ -234,4 +244,28 @@ do_faultsim_test 9.1 -prep { faultsim_test_result {0 {{0 0 20 39 0 0 64 2}}} } +do_faultsim_test 10.1 -prep { + faultsim_delete_and_reopen +} -body { + execsql { CREATE VIRTUAL TABLE t1 USING fts4(a, b, languageid=d) } +} -test { + faultsim_test_result {0 {}} +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 11.0 { + CREATE VIRTUAL TABLE t1 USING fts3(a, b); +} +faultsim_save_and_close + +do_faultsim_test 11 -faults oom* -prep { + faultsim_restore_and_reopen +} -body { + execsql { DROP TABLE t1 } +} -test { + faultsim_test_result {0 {}} +} + + finish_test diff --git a/test/fts3fault2.test b/test/fts3fault2.test index 030ff73dc2..913f9baa05 100644 --- a/test/fts3fault2.test +++ b/test/fts3fault2.test @@ -174,4 +174,77 @@ do_faultsim_test 6.1 -faults oom* -prep { faultsim_test_result {0 -1} } +#------------------------------------------------------------------------- +# Inject faults into a query for an N-byte prefix that uses a prefix=N+1 +# index. +reset_db +do_execsql_test 7.0 { + CREATE VIRTUAL TABLE t7 USING fts4(x,prefix=2); + INSERT INTO t7 VALUES('the quick brown fox'); + INSERT INTO t7 VALUES('jumped over the'); + INSERT INTO t7 VALUES('lazy dog'); +} +do_faultsim_test 7.1 -faults oom* -body { + execsql { SELECT docid FROM t7 WHERE t7 MATCH 't*' } +} -test { + faultsim_test_result {0 {1 2}} +} + +#------------------------------------------------------------------------- +# Inject faults into a opening an existing fts3 table that has been +# upgraded to add an %_stat table. +# +reset_db +do_execsql_test 8.0 { + CREATE VIRTUAL TABLE t8 USING fts3; + INSERT INTO t8 VALUES('the quick brown fox'); + INSERT INTO t8 VALUES('jumped over the'); + INSERT INTO t8 VALUES('lazy dog'); + INSERT INTO t8(t8) VALUES('automerge=8'); + SELECT name FROM sqlite_master WHERE name LIKE 't8%'; +} { + t8 t8_content t8_segments t8_segdir t8_stat +} +faultsim_save_and_close + +do_faultsim_test 8.1 -faults oom* -prep { + faultsim_restore_and_reopen +} -body { + execsql { INSERT INTO t8 VALUES('one two three') } +} -test { + faultsim_test_result {0 {}} +} + +set ::TMPDBERROR [list 1 \ + {unable to open a temporary database file for storing temporary tables} +] +do_faultsim_test 8.2 -faults oom* -prep { + faultsim_restore_and_reopen +} -body { + execsql { ALTER TABLE t8 RENAME TO t8ii } +} -test { + faultsim_test_result {0 {}} $::TMPDBERROR +} + +#------------------------------------------------------------------------- +reset_db +set chunkconfig [fts3_configure_incr_load 1 1] +do_execsql_test 9.0 { + PRAGMA page_size = 512; + CREATE VIRTUAL TABLE t9 USING fts3; + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<50 + ) + INSERT INTO t9 SELECT 'one two three' FROM s; +} + +do_faultsim_test 8.2 -faults io* -body { + execsql { SELECT count(*) FROM t9 WHERE t9 MATCH '"one two three"' } +} -test { + faultsim_test_result {0 50} +} + +eval fts3_configure_incr_load $chunkconfig + + finish_test diff --git a/test/fts3fault3.test b/test/fts3fault3.test new file mode 100644 index 0000000000..ae204718b4 --- /dev/null +++ b/test/fts3fault3.test @@ -0,0 +1,82 @@ +# 2023 October 23 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +set ::testprefix fts3fault + +# If SQLITE_ENABLE_FTS3 is not defined, omit this file. +ifcapable !fts3 { finish_test ; return } + +set ::TMPDBERROR [list 1 \ + {unable to open a temporary database file for storing temporary tables} +] + + +# Test error handling in an "ALTER TABLE ... RENAME TO" statement on an +# FTS3 table. Specifically, test renaming the table within a transaction +# after it has been written to. +# +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE t1 USING fts3(a); + INSERT INTO t1 VALUES('test renaming the table'); + INSERT INTO t1 VALUES(' after it has been written'); + INSERT INTO t1 VALUES(' actually other stuff instead'); +} +faultsim_save_and_close +do_faultsim_test 1 -faults oom* -prep { + faultsim_restore_and_reopen + execsql { + BEGIN; + DELETE FROM t1 WHERE rowid=2; + } +} -body { + execsql { + DELETE FROM t1; + } +} -test { + catchsql { COMMIT } + faultsim_integrity_check + faultsim_test_result {0 {}} +} + +#------------------------------------------------------------------- +reset_db + +do_execsql_test 2.0 { + BEGIN; + CREATE VIRTUAL TABLE t1 USING fts3(a); + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<50 + ) + INSERT INTO t1 SELECT 'abc def ghi jkl mno pqr' FROM s; + COMMIT; +} + +faultsim_save_and_close +do_faultsim_test 2 -faults oom-t* -prep { + faultsim_restore_and_reopen + execsql { + BEGIN; + CREATE TABLE x1(a PRIMARY KEY); + } +} -body { + execsql { + PRAGMA integrity_check; + } +} -test { + faultsim_test_result {0 ok} $::TMPDBERROR +} + + +finish_test diff --git a/test/fts3fuzz001.test b/test/fts3fuzz001.test new file mode 100644 index 0000000000..6b1ae90ee4 --- /dev/null +++ b/test/fts3fuzz001.test @@ -0,0 +1,141 @@ +# 2012-12-21 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# Test cases for corrupt database files. + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix fts3fuzz001 + +ifcapable !deserialize||!fts3 { + finish_test + return +} +database_may_be_corrupt + +do_test fts3fuzz001-100 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 24576 pagesize 4096 filename c6.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 06 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 05 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 96: 00 00 00 00 0d 0e f9 00 06 0d ec 00 0f cd 0f 69 ...............i +| 112: 0f 01 0e 10 0e c6 0d ec 00 00 00 00 00 00 00 00 ................ +| 3552: 00 00 00 00 00 00 00 00 00 00 00 00 22 06 06 17 ............"... +| 3568: 11 11 01 31 74 61 62 6c 65 74 32 74 32 06 43 52 ...1tablet2t2.CR +| 3584: 45 41 54 45 20 54 41 42 4c 45 20 74 32 28 78 29 EATE TABLE t2(x) +| 3600: 81 33 04 07 17 1f 1f 01 82 35 74 61 62 6c 65 74 .3.......5tablet +| 3616: 31 5f 73 65 67 64 69 72 74 31 5f 73 65 67 64 69 1_segdirt1_segdi +| 3632: 72 04 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 r.CREATE TABLE ' +| 3648: 74 31 5f 73 65 67 64 69 72 27 28 6c 65 76 65 6c t1_segdir'(level +| 3664: 20 49 4e 54 45 47 45 52 2c 69 64 78 20 49 4e 54 INTEGER,idx INT +| 3680: 45 47 45 52 2c 73 74 61 72 74 5f 62 6c 6f 63 6b EGER,start_block +| 3696: 20 49 4e 54 45 47 45 52 2c 6c 65 61 76 65 73 5f INTEGER,leaves_ +| 3712: 65 6e 64 5f 62 6c 6f 63 6b 20 49 4e 54 45 47 45 end_block INTEGE +| 3728: 52 2c 65 6e 64 5f 62 6c 6f 63 6b 20 49 4e 54 45 R,end_block INTE +| 3744: 47 45 52 2c 72 6f 6f 74 20 42 4c 4f 42 2c 50 52 GER,root BLOB,PR +| 3760: 49 4d 41 52 59 20 4b 45 59 28 6c 65 76 65 6c 2c IMARY KEY(level, +| 3776: 20 69 64 78 29 29 31 05 06 17 45 1f 01 00 69 6e idx))1...E...in +| 3792: 64 65 78 73 71 6c 69 74 65 5f 61 75 74 6f 69 6e dexsqlite_autoin +| 3808: 64 65 78 5f 74 15 f7 36 56 76 46 97 25 f3 17 43 dex_t..6VvF.%..C +| 3824: 15 5f 73 65 67 64 69 72 05 00 00 00 08 00 00 00 ._segdir........ +| 3840: 00 66 03 07 17 23 23 01 81 13 74 61 62 6c 65 74 .f...##...tablet +| 3856: 31 5f 73 65 67 6d 65 6e 74 73 74 31 5f 73 65 67 1_segmentst1_seg +| 3872: 6d 65 6e 74 73 03 43 52 45 41 54 45 20 54 41 42 ments.CREATE TAB +| 3888: 4c 45 20 27 74 31 5f 73 65 67 6d 65 6e 74 73 27 LE 't1_segments' +| 3904: 28 62 6c 6f 63 6b 69 64 20 49 4e 54 45 47 45 52 (blockid INTEGER +| 3920: 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 62 6c PRIMARY KEY, bl +| 3936: 6f 63 6b 20 42 4c 4f 42 29 62 02 07 17 21 21 01 ock BLOB)b...!!. +| 3952: 81 0f 74 61 62 6c 65 74 31 5f 63 6f 6e 74 65 6e ..tablet1_conten +| 3968: 74 74 31 5f 63 6f 6e 74 65 6e 74 02 43 52 45 41 tt1_content.CREA +| 3984: 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63 6f 6e TE TABLE 't1_con +| 4000: 74 65 6e 74 27 28 64 6f 63 69 64 20 49 4e 54 45 tent'(docid INTE +| 4016: 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c GER PRIMARY KEY, +| 4032: 20 27 63 30 63 6f 6e 74 65 6e 74 27 29 31 01 06 'c0content')1.. +| 4048: 17 11 11 08 51 74 61 62 6c 65 74 31 74 31 43 52 ....Qtablet1t1CR +| 4064: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 EATE VIRTUAL TAB +| 4080: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 33 LE t1 USING fts3 +| page 2 offset 4096 +| 0: 0d 00 00 00 03 0f e0 00 0f f6 0f ec 0f e0 00 00 ................ +| 4064: 0a 03 03 00 1b 61 62 61 6e 64 6f 6e 08 02 03 00 .....abandon.... +| 4080: 17 61 62 61 66 74 08 01 03 00 17 61 62 61 63 6b .abaft.....aback +| page 3 offset 8192 +| 0: 0d 00 00 00 00 10 00 00 00 00 00 00 00 00 00 00 ................ +| page 4 offset 12288 +| 0: 0d 00 00 00 01 0f d6 00 0f 00 00 00 00 00 00 00 ................ +| 4048: 00 00 00 00 00 00 28 01 07 08 08 08 08 15 46 30 ......(.......F0 +| 4064: 20 32 39 00 05 61 62 61 63 6b 03 01 02 00 03 02 29..aback...... +| 4080: 66 74 03 02 02 00 03 04 6e 64 6f 6e 03 03 02 00 ft......ndon.... +| page 5 offset 16384 +| 0: 0a 00 00 00 01 0f fb 00 0f fb 00 00 00 00 00 00 ................ +| 4080: 00 00 00 00 00 00 00 00 00 00 00 04 04 08 08 09 ................ +| page 6 offset 20480 +| 0: 0d 00 00 00 05 0f b8 00 0f f4 0f e9 0f d6 0f c7 ................ +| 16: 0f b8 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 4016: 00 00 00 00 00 00 00 00 0d 05 02 23 61 75 74 6f ...........#auto +| 4032: 6d 65 72 67 65 3d 35 0d 04 02 23 6d 65 72 67 65 merge=5...#merge +| 4048: 3d 31 30 30 2c 38 11 03 02 2b 69 6e 74 65 67 72 =100,8...+integr +| 4064: 69 74 79 3d 63 68 65 63 6b 09 02 02 1b 72 65 62 ity=check....reb +| 4080: 75 69 6c 64 0a 01 02 1d 6f 70 74 69 6d 69 7a 65 uild....optimize +| end c6.db + }] + catchsql { + PRAGMA writable_schema=on; -- disable schema corruption detection + INSERT INTO t1(t1) SELECT x FROM t2; + } +} {1 {database disk image is malformed}} +do_test fts3fuzz001-110 { + catchsql { + INSERT INTO t1(t1) VALUES('integrity-check'); + } +} {1 {database disk image is malformed}} +do_test fts3fuzz001-120 { + catchsql { + INSERT INTO t1(t1) VALUES('optimize'); + } +} {1 {database disk image is malformed}} +do_test fts3fuzz001-121 { + catchsql { + INSERT INTO t1(t1) VALUES('integrity-check'); + } +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 200 { + CREATE VIRTUAL TABLE x1 USING fts3(x); + + INSERT INTO x1 VALUES('braes brag bragged bragger bragging'); + INSERT INTO x1 VALUES('brags braid braided braiding braids'); + INSERT INTO x1 VALUES('brain brainchild brained braining brains'); + INSERT INTO x1 VALUES('brainstem brainstems brainstorm brainstorms'); + INSERT INTO x1(x1) VALUES('nodesize=24'); +} + +do_execsql_test 210 { + PRAGMA integrity_check; +} {ok} + +do_execsql_test 220 { + INSERT INTO x1(x1) VALUES('merge=10,2') +} + +do_execsql_test 220 { + PRAGMA integrity_check; +} {ok} + + + + +finish_test diff --git a/test/fts3integrity.test b/test/fts3integrity.test new file mode 100644 index 0000000000..bcbc49dc33 --- /dev/null +++ b/test/fts3integrity.test @@ -0,0 +1,42 @@ +# 2023 December 16 +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file runs all tests. +# +# $Id: fts3.test,v 1.2 2008/07/23 18:17:32 drh Exp $ + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set ::testprefix fts3integrity + +# If SQLITE_ENABLE_FTS3 is defined, omit this file. +ifcapable !fts3 { + finish_test + return +} + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE t1 USING fts3(x); + INSERT INTO t1 VALUES('first row'); + INSERT INTO t1 VALUES('second row'); + + CREATE TABLE t2(x PRIMARY KEY); + INSERT INTO t2 VALUES('first row'); + INSERT INTO t2 VALUES('second row'); +} + +sqlite3 db2 test.db + +do_execsql_test -db db2 1.1 { + CREATE TABLE t3(x, y); +} + +do_execsql_test 1.2 { + PRAGMA integrity_check; +} {ok} + +finish_test diff --git a/test/fts3join.test b/test/fts3join.test index 0fb8c62022..9171c817be 100644 --- a/test/fts3join.test +++ b/test/fts3join.test @@ -61,4 +61,44 @@ do_catchsql_test 2.5 { SELECT * FROM ft3, ft2 WHERE y MATCH x AND x MATCH y; } {1 {unable to use function MATCH in the requested context}} +do_execsql_test 3.0 { + CREATE VIRTUAL TABLE vt USING fts3(x); + INSERT INTO vt VALUES('abc'); + INSERT INTO vt VALUES('xyz'); + + CREATE TABLE tt(a INTEGER PRIMARY KEY); + INSERT INTO tt VALUES(1), (2); +} + +do_execsql_test 3.1 { + SELECT * FROM tt LEFT JOIN ( + SELECT rowid AS rrr, * FROM vt WHERE vt MATCH 'abc' + ) ON tt.a = rrr +} {1 1 abc 2 {} {}} + +do_execsql_test 3.2 { + SELECT * FROM tt LEFT JOIN vt ON (vt MATCH 'abc') +} {1 abc 2 abc} + +#------------------------------------------------------------------------- +# Test that queries of the form found in test case 4.2 use an automatic +# index to avoid running multiple fts queries. +# +do_execsql_test 4.1 { + CREATE VIRTUAL TABLE ft4 USING fts3(x); + CREATE TABLE t4(y, z); + CREATE INDEX t4y ON t1(y); +} + +do_eqp_test 4.2 { + SELECT * FROM t4 LEFT JOIN ( + SELECT docid, * FROM ft4 WHERE ft4 MATCH ? + ) AS rr ON t4.rowid=rr.docid + WHERE t4.y = ?; +} { + QUERY PLAN + |--SCAN t4 + `--SCAN ft4 VIRTUAL TABLE INDEX 3: LEFT-JOIN +} + finish_test diff --git a/test/fts3matchinfo.test b/test/fts3matchinfo.test index 8cbcd69041..af0b1cdc0d 100644 --- a/test/fts3matchinfo.test +++ b/test/fts3matchinfo.test @@ -278,6 +278,7 @@ do_matchinfo_test 4.3.6 t5 {t5 MATCH 'a OR b'} { s {1 2 1 1} } do_execsql_test 4.4.0.1 { INSERT INTO t5(t5) VALUES('optimize') } ifcapable fts4_deferred { + sqlite3_db_config db DEFENSIVE 0 do_execsql_test 4.4.0.2 { UPDATE t5_segments SET block = zeroblob(length(block)) @@ -339,6 +340,7 @@ do_execsql_test 6.1 { SELECT offsets(t9) FROM t9 WHERE t9 MATCH 'to'; } {{0 0 20 2 0 0 27 2}} +sqlite3_db_config db DEFENSIVE 0 do_catchsql_test 6.2 { UPDATE t9_content SET c0content = 'this record is used to'; SELECT offsets(t9) FROM t9 WHERE t9 MATCH 'to'; @@ -392,6 +394,7 @@ do_execsql_test 8.3 { } {{204 1 3 3 0} {204 1 3 3 0} {204 1 3 3 0}} # Corruption related tests. +sqlite3_db_config db DEFENSIVE 0 do_execsql_test 8.4.1.1 { UPDATE t11_stat SET value = X'0000'; } do_catchsql_test 8.5.1.2 { SELECT mit(matchinfo(t11, 'nxa')) FROM t11 WHERE t11 MATCH 'a*' diff --git a/test/fts3matchinfo2.test b/test/fts3matchinfo2.test new file mode 100644 index 0000000000..670e1079f1 --- /dev/null +++ b/test/fts3matchinfo2.test @@ -0,0 +1,35 @@ +# 2020-05-14 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for the FTS3 module. The focus +# of this file is tables created with the "matchinfo=fts3" option. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +# If SQLITE_ENABLE_FTS3 is not defined, omit this file. +ifcapable !fts3 { finish_test ; return } + +set sqlite_fts3_enable_parentheses 1 + +# Crash case found by cyg0810 at gmail.com 2020-05-14. Reported to +# chromium (which is not vulnerable) who kindly referred it to us. +# +do_execsql_test 1.0 { + CREATE TABLE t_content(col0 INTEGER); + CREATE VIRTUAL TABLE t0 USING fts3(col0 INTEGER PRIMARY KEY,col1 VARCHAR(8),col2 BINARY,col3 BINARY); + INSERT INTO t0 VALUES (1, '1234','aaaa','bbbb'); + SELECT hex(matchinfo(t0,'yxy')) FROM t0 WHERE t0 MATCH x'2b0a312b0a312a312a2a0b5d0a0b0b0a312a0a0b0b0a312a0b310a392a0b0a27312a2a0b5d0a312a0b310a31315d0b310a312a316d2a0b313b15bceaa50a312a0b0a27312a2a0b5d0a312a0b310a312b0b2a310a312a0b2a0b2a0b2e5d0a0bff313336e34a2a312a0b0a3c310b0a0b4b4b0b4b2a4bec40322b2a0b310a0a312a0a0a0a0a0a0a0a0a0b310a312a2a2a0b5d0a0b0b0a312a0b310a312a0b0a4e4541530b310a5df5ced70a0a0a0a0a4f520a0a0a0a0a0a0a312a0b0a4e4541520b310a5d616161610a0a0a0a4f520a0a0a0a0a0a312b0a312a312a0a0a0a0a0a0a004a0b0a310b220a0b0a310a4a22310a0b0a7e6fe0e0e030e0e0e0e0e01176e02000e0e0e0e0e01131320226310a0b0a310a4a22310a0b0a310a766f8b8b4ee0e0300ae0090909090909090909090909090909090909090909090909090909090909090947aaaa540b09090909090909090909090909090909090909090909090909090909090909fae0e0f2f22164e0e0f273e07fefefef7d6dfafafafa6d6d6d6d'; +} {/000000.*0000000/} + + +set sqlite_fts3_enable_parentheses 0 +finish_test diff --git a/test/fts3misc.test b/test/fts3misc.test new file mode 100644 index 0000000000..a1bec42432 --- /dev/null +++ b/test/fts3misc.test @@ -0,0 +1,326 @@ +# 2017 March 22 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# This file implements regression tests for SQLite library. The +# focus of this script is testing the FTS3 module. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix fts3misc + +# If SQLITE_ENABLE_FTS3 is defined, omit this file. +ifcapable !fts3 { + finish_test + return +} + +#------------------------------------------------------------------------- +# A self-join. +# +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE t1 USING fts3(a, b); + INSERT INTO t1 VALUES('one', 'i'); + INSERT INTO t1 VALUES('one', 'ii'); + INSERT INTO t1 VALUES('two', 'i'); + INSERT INTO t1 VALUES('two', 'ii'); +} + +do_execsql_test 1.1 { + SELECT a.a, b.b FROM t1 a, t1 b WHERE a.t1 MATCH 'two' AND b.t1 MATCH 'i' +} {two i two i two i two i} + +#------------------------------------------------------------------------- +# FTS tables with 128 or more columns. +# +proc v1 {v} { + set vector [list a b c d e f g h] + set res [list] + for {set i 0} {$i<8} {incr i} { + if {$v & (1 << $i)} { lappend res [lindex $vector $i] } + } + set res +} +proc v2 {v} { + set vector [list d e f g h i j k] + set res [list] + for {set i 0} {$i<8} {incr i} { + if {$v & (1 << $i)} { lappend res [lindex $vector $i] } + } + set res +} +db func v1 v1 +db func v2 v2 + +do_test 2.0 { + set cols [list] + for {set i 0} {$i<200} {incr i} { + lappend cols "c$i" + } + execsql "CREATE VIRTUAL TABLE t2 USING fts3([join $cols ,])" + execsql { + WITH data(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM data WHERE i<200 + ) + INSERT INTO t2(c198, c199) SELECT v1(i), v2(i) FROM data; + } +} {} +do_execsql_test 2.1 { + SELECT rowid FROM t2 WHERE t2 MATCH '"a b c"' +} { + 7 15 23 31 39 47 55 63 71 79 87 95 103 111 + 119 127 135 143 151 159 167 175 183 191 199 +} +do_execsql_test 2.2 { + SELECT rowid FROM t2 WHERE t2 MATCH '"g h i"' +} { + 56 57 58 59 60 61 62 63 120 121 122 123 124 + 125 126 127 184 185 186 187 188 189 190 191 +} +do_execsql_test 2.3 { + SELECT rowid FROM t2 WHERE t2 MATCH '"i h"' +} { +} +do_execsql_test 2.4 { + SELECT rowid FROM t2 WHERE t2 MATCH '"f e"' +} { +} +do_execsql_test 2.5 { + SELECT rowid FROM t2 WHERE t2 MATCH '"e f"' +} { + 6 7 14 15 22 23 30 31 38 39 46 47 48 49 50 51 52 53 54 55 56 + 57 58 59 60 61 62 63 70 71 78 79 86 87 94 95 102 103 110 + 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 + 134 135 142 143 150 151 158 159 166 167 174 175 176 177 178 179 180 + 181 182 183 184 185 186 187 188 189 190 191 198 199 +} + +#------------------------------------------------------------------------- +# Range constraints on the docid using non-integer values. +# +do_execsql_test 2.6 { + SELECT rowid FROM t2 WHERE t2 MATCH 'e' AND rowid BETWEEN NULL AND 45; +} {} +do_execsql_test 2.7 { + SELECT rowid FROM t2 WHERE t2 MATCH 'e' AND rowid BETWEEN 11.5 AND 48.2; +} { + 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 + 29 30 31 34 35 38 39 42 43 46 47 48 +} +do_execsql_test 2.8 { + SELECT rowid FROM t2 WHERE t2 MATCH 'e' AND rowid BETWEEN '11.5' AND '48.2'; +} { + 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 + 29 30 31 34 35 38 39 42 43 46 47 48 +} + +#------------------------------------------------------------------------- +# Phrase query tests. +# +do_execsql_test 3.1.1 { + CREATE VIRTUAL TABLE t3 USING fts3; + INSERT INTO t3 VALUES('a b c'); + INSERT INTO t3 VALUES('d e f'); + INSERT INTO t3 VALUES('a b d'); + INSERT INTO t3 VALUES('1 2 3 4 5 6 7 8 9 10 11'); +} +do_execsql_test 3.1.2 { + SELECT * FROM t3 WHERE t3 MATCH '"a b x y"' ORDER BY docid DESC +} +do_execsql_test 3.1.3 { + SELECT * FROM t3 WHERE t3 MATCH '"a b c" OR "a b x y"' ORDER BY docid DESC +} {{a b c}} +do_execsql_test 3.1.4 { + SELECT * FROM t3 WHERE t3 MATCH '"a* b* x* a*"' +} +do_execsql_test 3.1.5 { + SELECT rowid FROM t3 WHERE t3 MATCH '"2 3 4 5 6 7 8 9"' +} {4} + +#------------------------------------------------------------------------- +# +reset_db +ifcapable fts4_deferred { + do_execsql_test 4.0 { + PRAGMA page_size = 512; + CREATE VIRTUAL TABLE t4 USING fts4; + WITH s(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<8000 ) + INSERT INTO t4 SELECT 'a b c a b c a b c' FROM s; + } + do_execsql_test 4.1 { + SELECT count(*) FROM t4 WHERE t4 MATCH '"a b c" OR "c a b"' + } {8000} + do_execsql_test 4.2 { + SELECT quote(value) from t4_stat where id=0 + } {X'C03EC0B204C0A608'} + sqlite3_db_config db DEFENSIVE 0 + do_execsql_test 4.3 { + UPDATE t4_stat SET value = X'C03EC0B204C0A60800' WHERE id=0; + } + do_catchsql_test 4.4 { + SELECT count(*) FROM t4 WHERE t4 MATCH '"a b c" OR "c a b"' + } {1 {database disk image is malformed}} + do_execsql_test 4.5 { + UPDATE t4_stat SET value = X'00C03EC0B204C0A608' WHERE id=0; + } + do_catchsql_test 4.6 { + SELECT count(*) FROM t4 WHERE t4 MATCH '"a b c" OR "c a b"' + } {1 {database disk image is malformed}} +} + +#------------------------------------------------------------------------- +# +reset_db +do_execsql_test 5.0 { + CREATE VIRTUAL TABLE t5 USING fts4; + INSERT INTO t5 VALUES('a x x x x b x x x x c'); + INSERT INTO t5 VALUES('a x x x x b x x x x c'); + INSERT INTO t5 VALUES('a x x x x b x x x x c'); +} +do_execsql_test 5.1 { + SELECT rowid FROM t5 WHERE t5 MATCH 'a NEAR/4 b NEAR/4 c' +} {1 2 3} +do_execsql_test 5.2 { + SELECT rowid FROM t5 WHERE t5 MATCH 'a NEAR/3 b NEAR/4 c' +} {} +do_execsql_test 5.3 { + SELECT rowid FROM t5 WHERE t5 MATCH 'a NEAR/4 b NEAR/3 c' +} {} +do_execsql_test 5.4 { + SELECT rowid FROM t5 WHERE t5 MATCH 'y NEAR/4 b NEAR/4 c' +} {} +do_execsql_test 5.5 { + SELECT rowid FROM t5 WHERE t5 MATCH 'x OR a NEAR/3 b NEAR/3 c' +} {1 2 3} +do_execsql_test 5.5 { + SELECT rowid FROM t5 WHERE t5 MATCH 'x OR y NEAR/3 b NEAR/3 c' +} {1 2 3} + +#------------------------------------------------------------------------- +# +reset_db +do_execsql_test 6.0 { + CREATE VIRTUAL TABLE t6 USING fts4; + + BEGIN; + WITH s(i) AS (SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<50000) + INSERT INTO t6 SELECT 'x x x x x x x x x x x' FROM s; + + INSERT INTO t6 VALUES('x x x x x x x x x x x A'); + INSERT INTO t6 VALUES('x x x x x x x x x x x B'); + INSERT INTO t6 VALUES('x x x x x x x x x x x A'); + INSERT INTO t6 VALUES('x x x x x x x x x x x B'); + + WITH s(i) AS (SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<50000) + INSERT INTO t6 SELECT 'x x x x x x x x x x x' FROM s; + COMMIT; +} +do_execsql_test 6.1 { + SELECT rowid FROM t6 WHERE t6 MATCH 'b OR "x a"' +} {50001 50002 50003 50004} + +#------------------------------------------------------------------------- +# +reset_db +do_execsql_test 7.0 { + CREATE VIRTUAL TABLE vt0 USING fts3(c0); + INSERT INTO vt0 VALUES (x'00'); +} +do_execsql_test 7.1 { + INSERT INTO vt0(vt0) VALUES('integrity-check'); +} + +#------------------------------------------------------------------------- +# Ticket [8a6fa2bb]. +# +reset_db +do_execsql_test 7.0.1 { + CREATE VIRTUAL TABLE vt0 USING fts4(c0, order=DESC); + INSERT INTO vt0(c0) VALUES (0), (0); +} +do_execsql_test 7.0.2 { + INSERT INTO vt0(vt0) VALUES('integrity-check'); +} +reset_db +do_execsql_test 7.1.1 { + CREATE VIRTUAL TABLE vt0 USING fts4(c0, order=ASC); + INSERT INTO vt0(c0) VALUES (0), (0); +} +do_execsql_test 7.1.2 { + INSERT INTO vt0(vt0) VALUES('integrity-check'); +} +do_execsql_test 7.2.1 { + CREATE VIRTUAL TABLE ft USING fts4(c0, c1, order=DESC, prefix=1); + INSERT INTO ft VALUES('a b c d', 'hello world'); + INSERT INTO ft VALUES('negative', 'positive'); + INSERT INTO ft VALUES('hello world', 'a b c d'); +} +do_execsql_test 7.2.2 { + INSERT INTO vt0(vt0) VALUES('integrity-check'); +} + +#------------------------------------------------------------------------- +# Ticket [745f1abc]. +# +reset_db +do_execsql_test 8.1 { + CREATE VIRTUAL TABLE vt0 USING fts4(c0, prefix=1); +} +do_execsql_test 8.2 { + BEGIN; + INSERT INTO vt0 VALUES (0); + INSERT INTO vt0(vt0) VALUES('optimize'); + COMMIT; +} +do_execsql_test 8.3 { + INSERT INTO vt0(vt0) VALUES('integrity-check'); +} + +#------------------------------------------------------------------------- +# +reset_db +do_execsql_test 9.0 { + CREATE VIRTUAL TABLE t1 using fts4(mailcontent); + insert into t1(rowid, mailcontent) values + (-4764623217061966105, 'we are going to upgrade'), + (8324454597464624651, 'we are going to upgrade'); +} + +do_execsql_test 9.1 { + INSERT INTO t1(t1) VALUES('integrity-check'); +} + +do_execsql_test 9.2 { + SELECT rowid FROM t1 WHERE t1 MATCH 'upgrade'; +} { + -4764623217061966105 8324454597464624651 +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 10.0 { + CREATE VIRTUAL TABLE f USING fts3(a,b); + CREATE TABLE 'f_stat'(id INTEGER PRIMARY KEY, value BLOB); + INSERT INTO f_stat VALUES (1,x'3b3b3b3b3b3b3b28ffffffffffffffffff1807f9073481f1d43bc93b3b3b3b3b3b3b3b3b3b18073b3b3b3b3b3b3b9b003b'); +} {} + +do_catchsql_test 10.1 { + INSERT INTO f(f) VALUES ('merge=69,59'); +} {1 {database disk image is malformed}} + +#------------------------------------------------------------------------- +do_execsql_test 11.0 { + CREATE VIRTUAL TABLE xyz USING fts3(); +} +do_execsql_test 11.1 { + SELECT * FROM xyz WHERE xyz MATCH 'a NEAR/4294836224 a'; +} + +finish_test diff --git a/test/fts3offsets.test b/test/fts3offsets.test index 4a1e63aa4c..24c339dc12 100644 --- a/test/fts3offsets.test +++ b/test/fts3offsets.test @@ -118,6 +118,18 @@ do_execsql_test 1.4.1 { 1 {(A) (B) (C)} } +do_execsql_test 1.5.0 { + CREATE VIRTUAL TABLE x1 USING fts3(x); + INSERT INTO x1 VALUES('A A A'); + INSERT INTO x1 VALUES('A A A'); +} +do_execsql_test 1.5.1 { + SELECT offsets(x1) FROM x1 WHERE x1 MATCH 'a OR b AND c NEAR d' +} { + {0 0 0 1 0 0 2 1 0 0 4 1} + {0 0 0 1 0 0 2 1 0 0 4 1} +} + set sqlite_fts3_enable_parentheses 0 finish_test diff --git a/test/fts3query.test b/test/fts3query.test index 7d5ae991f7..70ee005291 100644 --- a/test/fts3query.test +++ b/test/fts3query.test @@ -118,26 +118,30 @@ do_test fts3query-4.1 { do_eqp_test fts3query-4.2 { SELECT t1.number FROM t1, ft WHERE t1.number=ft.rowid ORDER BY t1.date } { - 0 0 0 {SCAN TABLE t1 USING COVERING INDEX i1} - 0 1 1 {SCAN TABLE ft VIRTUAL TABLE INDEX 1:} + QUERY PLAN + |--SCAN t1 USING COVERING INDEX i1 + `--SCAN ft VIRTUAL TABLE INDEX 1: } do_eqp_test fts3query-4.3 { SELECT t1.number FROM ft, t1 WHERE t1.number=ft.rowid ORDER BY t1.date } { - 0 0 1 {SCAN TABLE t1 USING COVERING INDEX i1} - 0 1 0 {SCAN TABLE ft VIRTUAL TABLE INDEX 1:} + QUERY PLAN + |--SCAN t1 USING COVERING INDEX i1 + `--SCAN ft VIRTUAL TABLE INDEX 1: } do_eqp_test fts3query-4.4 { SELECT t1.number FROM t1, bt WHERE t1.number=bt.rowid ORDER BY t1.date } { - 0 0 0 {SCAN TABLE t1 USING COVERING INDEX i1} - 0 1 1 {SEARCH TABLE bt USING INTEGER PRIMARY KEY (rowid=?)} + QUERY PLAN + |--SCAN t1 USING COVERING INDEX i1 + `--SEARCH bt USING INTEGER PRIMARY KEY (rowid=?) } do_eqp_test fts3query-4.5 { SELECT t1.number FROM bt, t1 WHERE t1.number=bt.rowid ORDER BY t1.date } { - 0 0 1 {SCAN TABLE t1 USING COVERING INDEX i1} - 0 1 0 {SEARCH TABLE bt USING INTEGER PRIMARY KEY (rowid=?)} + QUERY PLAN + |--SCAN t1 USING COVERING INDEX i1 + `--SEARCH bt USING INTEGER PRIMARY KEY (rowid=?) } @@ -163,6 +167,7 @@ do_select_tests 5.3 -errorformat { 3 "SELECT snippet(content) FROM t2 WHERE t2 MATCH 'history'" snippet 4 "SELECT optimize(content) FROM t2 WHERE t2 MATCH 'history'" optimize } +sqlite3_db_config db DEFENSIVE 0 do_execsql_test 5.4.0 { UPDATE t2_content SET c0content = X'1234' } do_select_tests 5.4 -errorformat { illegal first argument to %s diff --git a/test/fts3rank.test b/test/fts3rank.test new file mode 100644 index 0000000000..fd1a1c89d7 --- /dev/null +++ b/test/fts3rank.test @@ -0,0 +1,69 @@ +# 2017 October 7 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# This file implements regression tests for SQLite library. The +# focus of this script is testing the FTS3 module. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix fts3rank + +# If SQLITE_ENABLE_FTS3 is defined, omit this file. +ifcapable !fts3 { + finish_test + return +} + +install_fts3_rank_function db +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE t1 USING fts3(a, b); + INSERT INTO t1 VALUES('one two', 'one'); + INSERT INTO t1 VALUES('one two', 'three'); + INSERT INTO t1 VALUES('one two', 'two'); +} + +do_execsql_test 1.1 { + SELECT * FROM t1 WHERE t1 MATCH 'one' + ORDER BY rank(matchinfo(t1), 1.0, 1.0) DESC, rowid +} { + {one two} one + {one two} three + {one two} two +} + +do_execsql_test 1.2 { + SELECT * FROM t1 WHERE t1 MATCH 'two' + ORDER BY rank(matchinfo(t1), 1.0, 1.0) DESC, rowid +} { + {one two} two + {one two} one + {one two} three +} + +do_catchsql_test 1.3 { + SELECT * FROM t1 ORDER BY rank(matchinfo(t1), 1.0, 1.0) DESC, rowid +} {1 {invalid matchinfo blob passed to function rank()}} + +do_catchsql_test 1.4 { + SELECT * FROM t1 ORDER BY rank(x'0000000000000000') DESC, rowid +} {0 {{one two} one {one two} three {one two} two}} + +if {$tcl_platform(byteOrder)=="littleEndian"} { + do_catchsql_test 1.5le { + SELECT * FROM t1 ORDER BY rank(x'0100000001000000') DESC, rowid + } {1 {invalid matchinfo blob passed to function rank()}} +} else { + do_catchsql_test 1.5be { + SELECT * FROM t1 ORDER BY rank(x'0000000100000001') DESC, rowid + } {1 {invalid matchinfo blob passed to function rank()}} +} + +finish_test diff --git a/test/fts3snippet.test b/test/fts3snippet.test index 887011cd11..ad1fbb3bef 100644 --- a/test/fts3snippet.test +++ b/test/fts3snippet.test @@ -184,6 +184,7 @@ foreach {DO_MALLOC_TEST enc} { [list 0 0 $off 10 1 0 $off 10] [list 0 0 $off 10] # Test a corruption case: + sqlite3_db_config db DEFENSIVE 0 execsql { UPDATE ft_content SET c1b = 'hello world' WHERE c1b = $numbers } do_error_test $T.2.3 { SELECT offsets(ft) FROM ft WHERE ft MATCH 'onehundred' @@ -554,8 +555,41 @@ do_test 4.2 { }] } {64} +do_test 4.3 { + llength [db one { + SELECT snippet(t4, '', '', '', 0, 150) FROM t4 WHERE t4 MATCH 'E' + }] +} {64} +#------------------------------------------------------------------------- +# Request a snippet from a query with more than 64 phrases. +# +do_execsql_test 5.0 { + CREATE VIRTUAL TABLE t5 USING fts3(x); + INSERT INTO t5 VALUES('a1 a2 a3'); + INSERT INTO t5 VALUES('a4 a5 a6'); + INSERT INTO t5 VALUES('a70 a71 a72'); +} + +do_execsql_test 5.1 { + SELECT snippet(t5, '[', ']') FROM t5 WHERE t5 MATCH + 'a1 OR a2 OR a3 OR a4 OR a5 OR a6 OR a7 OR a8 OR a9 OR a10 OR ' || + 'a11 OR a12 OR a13 OR a14 OR a15 OR a16 OR a17 OR a18 OR a19 OR a10 OR ' || + 'a21 OR a22 OR a23 OR a24 OR a25 OR a26 OR a27 OR a28 OR a29 OR a20 OR ' || + 'a31 OR a32 OR a33 OR a34 OR a35 OR a36 OR a37 OR a38 OR a39 OR a30 OR ' || + 'a41 OR a42 OR a43 OR a44 OR a45 OR a46 OR a47 OR a48 OR a49 OR a40 OR ' || + 'a51 OR a52 OR a53 OR a54 OR a55 OR a56 OR a57 OR a58 OR a59 OR a50 OR ' || + 'a61 OR a62 OR a63 OR a64 OR a65 OR a66 OR a67 OR a68 OR a69 OR a60 OR ' || + 'a71 OR a72 OR a73 OR a74 OR a75 OR a76 OR a77 OR a78 OR a79 OR a70' +} { + {[a1] [a2] [a3]} + {[a4] [a5] [a6]} + {[a70] [a71] [a72]} +} +do_execsql_test 5.2 { + SELECT snippet(t5, '[', ']', -1, 0) FROM t5 WHERE t5 MATCH 'a5' +} {{a4 [a5] a6}} set sqlite_fts3_enable_parentheses 0 finish_test diff --git a/test/fts3snippet2.test b/test/fts3snippet2.test new file mode 100644 index 0000000000..f55a5f203a --- /dev/null +++ b/test/fts3snippet2.test @@ -0,0 +1,70 @@ +# 2020-05-14 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# +# The tests in this file test the FTS3 auxillary functions offsets(), +# snippet() and matchinfo() work. At time of writing, running this file +# provides full coverage of fts3_snippet.c. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix fts3snippet + +# If SQLITE_ENABLE_FTS3 is not defined, omit this file. +ifcapable !fts3 { finish_test ; return } +source $testdir/fts3_common.tcl + +set sqlite_fts3_enable_parentheses 1 +#------------------------------------------------------------------------- +# Request a snippet from a query with more than 64 phrases. +# +reset_db +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE f USING fts3(b); + INSERT INTO f VALUES ( x'746e6e6d64612e082a011065616e656d655a616c702a2f65732e0f42014001380230018218'); +} + +do_execsql_test 1.1 { + SELECT length(snippet(f))>0 FROM f WHERE b MATCH x'1065616e656d655a616c702a2f65732e0f42014001380230018218021001081e0a3d746e6e6d64612e082a010f42014001380230018218021001081e0a3d746e6e6d64612e082a011065616e656d655a616c702a2f65732e0f42014001380230018218021001081e0a3d746e6e6d64612e082a011065616e656d655a616c702a2f65732e0f42014001380230018218021001081e0a3d746e6e6d64612e082a011065616e656d655a616c702a2f0a3d746e6e6d64612e082a011065616e656d655a616c702a2f65732e0f42014001018218021001081e0a3d746e6e6d64612e082a011065616e656d655a616c702a018218021001081e0a3d746e6e6d64612e082a011065616e656d655a616c2a2f65732e0f42014001380230018218021001081e0a3d746e6e6d64612e082a011065616e656d655a616c702a2f65732e0f42014001380230018218021001081e0a3d746e6e6d64612e082a011065616e656d655a616c702a2f65732e0f42014001380230018218021001081e0a3d746e6e6d64612e082a011065616e656d655a616c702a2f65732e0f42014001380230018218021001081e0a3d746e6e6d64612e0f42'; +} {1} + +reset_db +do_execsql_test 2.0 { + CREATE VIRTUAL TABLE t0 USING fts3(col0 INTEGER PRIMARY KEY,col1 VARCHAR(8),col2 BINARY,col3 BINARY); + INSERT INTO t0 VALUES (1, '1234','aaaa','bbbb'); + SELECT snippet(t0) FROM t0 WHERE t0 MATCH x'0a4d4d4d4d320a4f52d70a310a310a4e4541520a0a31f6ce0a4f520a0a310a310a310a4f520a75fc2a242424' ; +} {<b>1</b>} + +reset_db +do_execsql_test 2.1 { + CREATE VIRTUAL TABLE t0 USING fts3( + col0 INTEGER PRIMARY KEY,col1 VARCHAR(8),col2 BINARY,col3 BINARY + ); + INSERT INTO t0 VALUES ('one', '1234','aaaa','bbbb'); +} +do_execsql_test 2.2 { + SELECT snippet(t0) FROM t0 WHERE t0 MATCH + '(def AND (one NEAR abc)) OR one' +} {<b>one</b>} + +#------------------------------------------------------------------------- + +do_execsql_test 3.0 { +CREATE VIRTUAL TABLE f USING fts3(a,b); +INSERT INTO f VALUES (101,x'056522650565056505650d051e056505650565286505650565056505056505650565056505650565056505650565056505650565056505656505650565056505650d05650505656505650565ef65056505844c746e65650565056505650565056505650565056505650565058405800565056505650565056505651e650565056505650565056505650d056505056565056505650565056505840580056505650565056f05650565056505650565056505650565050565056505640565056505650565056505651e05650565056505650565056505650505656565056505650565056505651e0565056505650565056505650565052265056505650569056505650565056505650565056505650565056505650500406505650565056505650565056505000101e5c501014b010101c501c5c501010101f5010201010101014101017373737373737373737373737373737373737373737373737373737330737373737373737373737373737365056505650d051e05650565056528056505650d05650505656505650565650565056505650565056505e505650565056505656505650565056505650d05650505656505650565ef65056505844c746e65650565056505650565056505650565056505650565058405800565056505650565056505651e650565056505650565056505650d056505056565056505650565056505840580056505650565286505c705650565050565059494949494949494949494949494949494949494949494949494949494949494949494650565056505650565056505650565056505650565056505656505650565056505650d05650505656505650565ef650565058405056505650565056505650565056505650565056505650565058405800565056505650565056505651e650565056505650565056505650d056505056565056505650565056505840580056505650565056505650565056505650505650565056505650565056505650565056500000000000000000000000000000000000000000000000000000000000000000565056505656505650565056505650d056500000000000000000000000000000000000000000000000000000000000000000100000000000000ed0000000000ffffffffffffffffffffff0007ffffff0001c5c50001c5c50001c5c50001c5c50001c5c50001c5c50001e5c50001c5c50001c5c50001c5c50001c5c50001c5c5000100000014720000000000000016dac5c50001c5c50001c5c50001c5c50001c5c50001c5c50d0505656505650565ef650565058405056505650565056505650565056505650565056505650565058405800465056505650565056505651e650565056505650565056505650d05650505656505650565050565650584058005650565056505650565056505650565056522650565056505650d051e056505650561286505c70565056505056505650565056505650565056505650565056505650565056505656505650565056505650d05650505656505650565ef650565058405056505650565056505650565056505650565056505650565058405800565056505650565056505651e650565056505650565056505650d056505056565056505650565056505840580056505650565056505650565056505650565226505737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373733a73737373737373737373737373737373737373737373737373737373737373737373737373737373737c7373737365056505650d051e05650565056528650565056505650505650565056505650565056505650565056505e505650565056505656505650565056505650d05650505656505650565ef65056505'); +} + +do_execsql_test 3.1 { + SELECT length(snippet(f)) FROM f WHERE b MATCH x'0565056505650565056505650565056505650565058405800565056505650565056505651e650565056505650565056505650d056505056565056505650565056505840580056505650565056505650565056505650565056505650565050565056505640565056505650565056505651e05650565056522650565056505650d051e056505650565286505650565056505056505650565056505650565056505650565056505650565056505656505650565056505650d05650505656505650565ef65056505844c746e65650565056505650565056505650565056505650565058405800565056505650565056505651e650565056505650565056505650d056505056565056505650565056505840580056505650565056f05650565056505650565056505650565050565056505640565056505650565056505651e05650565056505650565056505650505656565056505650565056505651e0565056505650565056505650565052265056505650569056505650565056505650565056505650565056505650500406505650565056505650565056505000101e5c501014b010101c501c5c501010101f50102010101010141010141010001017bf15905000000000017'; +} {192} + +set sqlite_fts3_enable_parentheses 0 +finish_test diff --git a/test/fts3tok1.test b/test/fts3tok1.test index 09b07e9db1..139fae89d9 100644 --- a/test/fts3tok1.test +++ b/test/fts3tok1.test @@ -108,7 +108,7 @@ do_catchsql_test 2.0 { do_catchsql_test 2.1 { CREATE VIRTUAL TABLE t4 USING fts3tokenize; SELECT * FROM t4; -} {1 {SQL logic error or missing database}} +} {1 {SQL logic error}} do_catchsql_test 2.2 { CREATE VIRTUAL TABLE t USING fts4(tokenize=simple""); diff --git a/test/fts3varint.test b/test/fts3varint.test index ca0189dbfc..9f797ed954 100644 --- a/test/fts3varint.test +++ b/test/fts3varint.test @@ -110,9 +110,12 @@ do_fts3_varint_test 2.61 { 2305843009213693951 2305843009213693952 2305843009213693953 } do_fts3_varint_test 2.62 { 4611686018427387903 4611686018427387904 4611686018427387905 } -do_fts3_varint_test 2.63 { - 9223372036854775807 9223372036854775808 9223372036854775809 } -do_fts3_varint_test 3.0 { 18446744073709551615 -18446744073709551615 } +if {![catch {fts3_test_varint 18446744073709551615}]} { + do_fts3_varint_test 2.63 { + 9223372036854775807 9223372036854775808 9223372036854775809 } + + do_fts3_varint_test 3.0 { 18446744073709551615 -18446744073709551615 } +} finish_test diff --git a/test/fts4aa.test b/test/fts4aa.test index e6c7f9336e..0c6c0b972f 100644 --- a/test/fts4aa.test +++ b/test/fts4aa.test @@ -191,4 +191,66 @@ foreach {q r} [array get fts4aa_res] { } $r } +# 2019-11-16 https://bugs.chromium.org/p/chromium/issues/detail?id=1025472 +# +db close +sqlite3 db :memory: +do_execsql_test fts4aa-5.10 { + CREATE VIRTUAL TABLE t1 USING fts4(a, b, c, d, e,f,g,h,i,j,k,l,m,n,o,p,q,r); + INSERT INTO t1 VALUES('X Y', '2', '3', '4', '5', '6', '7', '8', '9', '0', + 'a','b','c','d','e','f','g','h'); + UPDATE t1_docsize SET size=x'88' WHERE docid=1; +} {} +do_catchsql_test fts4aa-5.20 { + SELECT quote(matchinfo(t1, 'l')) FROM t1 WHERE t1 MATCH 'X Y'; +} {1 {database disk image is malformed}} +do_execsql_test fts4aa-5.30 { + DROP TABLE t1; + CREATE VIRTUAL TABLE t1 USING fts4(a,b,c,d); + INSERT INTO t1 VALUES('one two','three four','five six','seven eight'); +} {} +do_catchsql_test fts4aa-5.40 { + UPDATE t1_stat SET value=x'01010101' WHERE id=0; + SELECT quote(matchinfo(t1,'a')) FROM t1 WHERE t1 MATCH 'one two'; +} {1 {database disk image is malformed}} +do_catchsql_test fts4aa-5.50 { + UPDATE t1_stat SET value=x'010101' WHERE id=0; + SELECT quote(matchinfo(t1,'a')) FROM t1 WHERE t1 MATCH 'one two'; +} {1 {database disk image is malformed}} +do_catchsql_test fts4aa-5.60 { + UPDATE t1_stat SET value=x'01' WHERE id=0; + SELECT quote(matchinfo(t1,'a')) FROM t1 WHERE t1 MATCH 'one two'; +} {1 {database disk image is malformed}} +do_catchsql_test fts4aa-5.70 { + UPDATE t1_stat SET value=x'' WHERE id=0; + SELECT quote(matchinfo(t1,'a')) FROM t1 WHERE t1 MATCH 'one two'; +} {1 {database disk image is malformed}} + +# 2019-11-18 https://bugs.chromium.org/p/chromium/issues/detail?id=1025467 +db close +sqlite3 db :memory: +if {$tcl_platform(byteOrder)=="littleEndian"} { + set res {X'0200000000000000000000000E0000000E00000001000000010000000100000001000000'} +} else { + set res {X'0000000200000000000000000000000E0000000E00000001000000010000000100000001'} +} +do_catchsql_test fts4aa-6.10 { + CREATE VIRTUAL TABLE f USING fts4(); + INSERT INTO f_segdir VALUES (77,91,0,0,'255 77',x'0001308000004d5c4ddddddd4d4d7b4d4d4d614d8019ff4d05000001204d4d2e4d6e4d4d4d4b4d6c4d004d4d4d4d4d4d3d000000004d5d4d4d645d4d004d4d4d4d4d4d4d4d4d454d6910004d05ffff054d646c4d004d5d4d4d4d4d3d000000004d4d4d4d4d4d4d4d4d4d4d69624d4d4d04004d4d4d4d4d604d4ce1404d554d45'); + INSERT INTO f_segdir VALUES (77,108,0,0,'255 77',x'0001310000fa64004d4d4d3c5d4d654d4d4d614d8000ff4d05000001204d4d2e4d6e4d4d4dff4d4d4d4d4d4d00104d4d4d4d000000004d4d4d0400311d4d4d4d4d4d4d4d4d4d684d6910004d05ffff054d4d6c4d004d4d4d4d4d4d3d000000004d4d4d4d644d4d4d4d4d4d69624d4d4d03ed4d4d4d4d4d604d4ce1404d550080'); + INSERT INTO f_stat VALUES (0,x'80808080100000000064004d4d4d3c4d4d654d4d4d614d8000ff4df6ff1a00204d4d2e4d6e4d4d4d104d4d4d4d4d4d00104d4d4d4d4d4d69574d4d4d000031044d4d4d3e4d4d4c4d05004d6910'); + SELECT quote(matchinfo(f,'pnax')) from f where f match '0 1'; +} {1 {database disk image is malformed}} + +# 2019-11-18 Detect infinite loop in fts3SelectLeaf() +db close +sqlite3 db :memory: +do_catchsql_test fts4aa-7.10 { + CREATE VIRTUAL TABLE f USING fts4(); + INSERT INTO f_segdir VALUES (63,60,60,60,'60 60',x'3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c483c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c20003c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c283c3c3c3c3c3c3c3c3c3c3c223c3c3c3c3c3c3c3c3c'); + INSERT INTO f_segments VALUES (60,x'3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c5a3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c2a3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c5e3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c803c3c3c3c3c3c233c3c3c3c1c3c3c3c3c3c3c3c3c3c3c3c1b3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c273c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c1a3c3c3c3c3c3c000200003c3c3c3c3c3c3c3c3c3c3c3c3c383c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d898d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d3c3c3c3c3c3c3c3c3c3cba3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c1c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c00023c3c3c3c3c3c383c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3cbc3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c2c3c3c3c403c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c16161616161616163c3c3c3c3c3c3c3c3c3c3c3c3c583c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c2b3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c1c013c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c20003c3c3c3c3c3c3c3c3c3c3c800000003c3c3c3c3c3c3c2c3c3c3c3c3c3c353c08080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808f4080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808083c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c323c3c3c3c3c3c3c3c3c3c3c4f3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3cfcfcfcfcfcfcfcfcfcfcfc10fcfcfcfcfcfcfcfcfcfcfcfcfcfcfcfcfcfcfcfcfcfcfcfcfcfcfcfcfcfcfcfd02fcfcfcfcfcfcfcfcfcfcfcfcfcfcfcfcfcfcfcfcfcfcfcfcfcfcfcfcfcfcfcfcfcfcfcfcfcfcfcfcfcfc03e8fcfcfcfc3c3c3c3c3c3c8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d8d3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c553c3c3c3c3c3c3c3c3c3c3c3c3c573c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c000000803c3c4dd5d5a6d52cf3d5d5d5d5d5d5d5d5d5d5d5d5d5d53c3c3c3c3f3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c2d3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c013c3c3c3c00643c3c3c3ce93c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c263c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c363c3c3c3c3c3c3c3c3c3c3c3c3c3c543c3c3c3c3c3c3c3c3c3c273c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c330000003c3c3c3c3c3c3c3c3c3c3c3c3c3c4d3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c000010003c3c3c3c3c3c413c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c1c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c403c3c3c3c3c3c3c3c3c3c3c3cec0000fa3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c2d3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c4c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c5e3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c1b3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c593c3c3c3c3c3c243c3c373c3c3c3c3cff3c3c3c3c3c3c3c3c3c3c3c3c3c000080003c3c3c3c3c3c3c3c3c3c353c3c3c3c3c3d3c3c3c3c3c3c3c3c3c3c3c3c4d3c3c3c3c3c3c3c3c3c3c3c3c3c40003c3c3c3c3c293c3c3c3c3c3c3c3c3c3d3c3c3c3c3c3c3c3c353c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c4f3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3f3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3cff7f3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c2d3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3ca43c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3cbf3c3c3c3c3c3c3c3c3c008000003c3c3c3c3c3c3c3c343c3c373c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c593c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c'); + SELECT * from f where f match '0'; +} {1 {database disk image is malformed}} + + finish_test diff --git a/test/fts4check.test b/test/fts4check.test index 1e2f6b234f..7f1004d8b3 100644 --- a/test/fts4check.test +++ b/test/fts4check.test @@ -66,11 +66,15 @@ foreach {tn disruption} { ); } } { + sqlite3_db_config db DEFENSIVE 0 do_execsql_test 1.2.1.$tn "BEGIN; $disruption" do_catchsql_test 1.2.2.$tn { INSERT INTO t1 (t1) VALUES('integrity-check') } {1 {database disk image is malformed}} - do_execsql_test 1.2.3.$tn "ROLLBACK" + do_execsql_test 1.2.3.$tn { + PRAGMA integrity_check; + } {{malformed inverted index for FTS4 table main.t1}} + do_execsql_test 1.2.4.$tn "ROLLBACK" } do_test 1.3 { fts_integrity db t1 } {ok} @@ -100,11 +104,15 @@ foreach {tn disruption} { ); } } { + sqlite3_db_config db DEFENSIVE 0 do_execsql_test 2.2.1.$tn "BEGIN; $disruption" do_catchsql_test 2.2.2.$tn { INSERT INTO t2 (t2) VALUES('integrity-check') } {1 {database disk image is malformed}} - do_execsql_test 2.2.3.$tn "ROLLBACK" + do_test 2.2.3.$tn { + db eval {PRAGMA integrity_check(t2);} + } {{malformed inverted index for FTS4 table main.t2}} + do_execsql_test 2.2.4.$tn "ROLLBACK" } @@ -145,6 +153,7 @@ foreach {tn disruption} { ) } } { + sqlite3_db_config db DEFENSIVE 0 do_execsql_test 3.2.1.$tn "BEGIN; $disruption" do_catchsql_test 3.2.2.$tn { INSERT INTO t3 (t3) VALUES('integrity-check') @@ -163,6 +172,7 @@ do_execsql_test 4.0 { INSERT INTO t4(t4) VALUES('integrity-check'); } +sqlite3_db_config db DEFENSIVE 0 do_execsql_test 4.1 { PRAGMA writable_schema = 1; UPDATE sqlite_master @@ -197,6 +207,7 @@ do_execsql_test 5.1 { INSERT INTO t5(t5) VALUES('integrity-check'); } {} +sqlite3_db_config db DEFENSIVE 0 do_catchsql_test 5.2 { INSERT INTO t5_content VALUES(5, 'his hardy mountain pony'); INSERT INTO t5(t5) VALUES('integrity-check'); diff --git a/test/fts4content.test b/test/fts4content.test index 67642b6b8a..980586ea3a 100644 --- a/test/fts4content.test +++ b/test/fts4content.test @@ -424,7 +424,7 @@ do_execsql_test 6.2.1 { do_catchsql_test 6.2.2 { DROP TABLE t7; SELECT * FROM ft7; -} {1 {SQL logic error or missing database}} +} {1 {SQL logic error}} db close sqlite3 db test.db @@ -457,10 +457,10 @@ do_execsql_test 6.2.7 { } do_catchsql_test 6.2.8 { SELECT * FROM ft7 WHERE ft7 MATCH '"A A"'; -} {1 {SQL logic error or missing database}} +} {1 {SQL logic error}} do_catchsql_test 6.2.9 { SELECT * FROM ft7 WHERE ft7 MATCH '"A A"'; -} {1 {SQL logic error or missing database}} +} {1 {SQL logic error}} db close sqlite3 db test.db @@ -504,7 +504,7 @@ do_execsql_test 7.2.3 { do_catchsql_test 7.2.4 { SELECT * FROM ft9 WHERE ft9 MATCH 'N'; -} {1 {SQL logic error or missing database}} +} {1 {SQL logic error}} #------------------------------------------------------------------------- # Test cases 8.* @@ -634,5 +634,41 @@ do_catchsql_test 11.1 { CREATE VIRTUAL TABLE x1 USING fts4(content=x1); } {1 {vtable constructor called recursively: x1}} +#--------------------------------------------------------------------------- +# Check that an fts4 table cannot be its own content table. +# +reset_db +breakpoint +do_execsql_test 12.1.1 { + CREATE VIRTUAL TABLE t1 USING fts4(a, content=t1 ); + INSERT INTO t1(rowid, a) VALUES(1, 'abc'); +} +do_catchsql_test 12.1.2 { + SELECT * FROM t1; +} {1 {SQL logic error}} +do_catchsql_test 12.1.3 { + SELECT * FROM t1('abc'); +} {1 {SQL logic error}} +do_catchsql_test 12.1.4 { + SELECT count(*) FROM t1; +} {1 {SQL logic error}} + +reset_db +do_execsql_test 12.2.1 { + CREATE VIRTUAL TABLE t1 USING fts4(a, content=t2 ); + CREATE VIRTUAL TABLE t2 USING fts4(a, content=t1 ); + INSERT INTO t1(rowid, a) VALUES(1, 'abc'); +} +do_catchsql_test 12.2.2 { + SELECT * FROM t1; +} {1 {SQL logic error}} +do_catchsql_test 12.2.3 { + SELECT * FROM t1('abc'); +} {1 {SQL logic error}} +do_catchsql_test 12.2.4 { + SELECT count(*) FROM t1; +} {1 {SQL logic error}} + + finish_test diff --git a/test/fts4growth.test b/test/fts4growth.test index e4b5f19ecb..5cf877747b 100644 --- a/test/fts4growth.test +++ b/test/fts4growth.test @@ -25,6 +25,7 @@ ifcapable !fts3 { source $testdir/genesis.tcl +sqlite3_db_config db DEFENSIVE 0 do_execsql_test 1.1 { CREATE VIRTUAL TABLE x1 USING fts3; } do_test 1.2 { @@ -59,11 +60,11 @@ do_test 1.4 { INSERT INTO x1(x1) VALUES('merge=4,4'); SELECT level, end_block, length(root) FROM x1_segdir; } -} {0 {0 110} 110 0 {0 132} 132 0 {0 129} 129 1 {128 658} 2} +} {1 {224 921} 2} do_execsql_test 1.5 { SELECT length(block) FROM x1_segments; -} {658 {}} +} {921 {}} do_test 1.6 { foreach L { @@ -71,27 +72,33 @@ do_test 1.6 { {That perched above Dead Man's Creek, beside the mountain road.} {He turned the cycle down the hill and mounted for the fray,} {But 'ere he'd gone a dozen yards it bolted clean away.} + {It left the track, and through the trees, just like a silver steak,} {It whistled down the awful slope towards the Dead Man's Creek.} {It shaved a stump by half an inch, it dodged a big white-box:} {The very wallaroos in fright went scrambling up the rocks,} + {The wombats hiding in their caves dug deeper underground,} {As Mulga Bill, as white as chalk, sat tight to every bound.} {It struck a stone and gave a spring that cleared a fallen tree,} {It raced beside a precipice as close as close could be;} + {And then as Mulga Bill let out one last despairing shriek} {It made a leap of twenty feet into the Dead Man's Creek.} + {It shaved a stump by half an inch, it dodged a big white-box:} + {The very wallaroos in fright went scrambling up the rocks,} + {The wombats hiding in their caves dug deeper underground,} } { execsql { INSERT INTO x1 VALUES($L) } } execsql { SELECT level, end_block, length(root) FROM x1_segdir; } -} {1 {128 658} 2 1 {130 1377} 6 0 {0 117} 117} +} {1 {224 921} 2 1 {226 1230} 7 0 {0 98} 98} do_execsql_test 1.7 { - SELECT sum(length(block)) FROM x1_segments WHERE blockid IN (129, 130); -} {1377} + SELECT sum(length(block)) FROM x1_segments WHERE blockid IN (224,225,226) +} {1230} #------------------------------------------------------------------------- # @@ -131,24 +138,24 @@ do_execsql_test 2.5 { SELECT end_block FROM x2_segdir WHERE level=3; INSERT INTO x2(x2) VALUES('merge=4,4'); SELECT end_block FROM x2_segdir WHERE level=3; -} {{3828 -3430} {3828 -10191} {3828 -14109}} +} {{5588 -3950} {5588 -11766} {5588 -15541}} do_execsql_test 2.6 { SELECT sum(length(block)) FROM x2_segdir, x2_segments WHERE blockid BETWEEN start_block AND leaves_end_block AND level=3 -} {14109} +} {15541} do_execsql_test 2.7 { INSERT INTO x2(x2) VALUES('merge=1000,4'); SELECT end_block FROM x2_segdir WHERE level=3; -} {{3828 86120}} +} {{5588 127563}} do_execsql_test 2.8 { SELECT sum(length(block)) FROM x2_segdir, x2_segments WHERE blockid BETWEEN start_block AND leaves_end_block AND level=3 -} {86120} +} {127563} #-------------------------------------------------------------------------- # Test that delete markers are removed from FTS segments when possible. @@ -391,7 +398,7 @@ do_execsql_test 7.2 { } { 0 0 {118 117483} 0 1 {238 118006} 0 2 {358 118006} 0 3 {478 118006} 0 4 {598 118006} 0 5 {718 118006} - 1 0 {16014 -51226} + 1 0 {23694 -69477} } do_execsql_test 7.3 { @@ -400,7 +407,7 @@ do_execsql_test 7.3 { } { 0 0 {118 117483} 0 1 {238 118006} 0 2 {358 118006} 0 3 {478 118006} 0 4 {598 118006} 0 5 {718 118006} - 1 0 16014 + 1 0 23694 } do_execsql_test 7.4 { @@ -409,28 +416,26 @@ do_execsql_test 7.4 { } { 0 0 {118 117483} 0 1 {238 118006} 0 2 {358 118006} 0 3 {478 118006} 0 4 {598 118006} 0 5 {718 118006} - 1 0 16014 + 1 0 23694 } do_execsql_test 7.5 { INSERT INTO x6(x6) VALUES('merge=2500,4'); - SELECT level, idx, end_block FROM x6_segdir; + SELECT level, idx, start_block, leaves_end_block, end_block FROM x6_segdir; } { - 0 0 {598 118006} 0 1 {718 118006} 1 0 16014 + 1 0 719 1171 23694 } do_execsql_test 7.6 { INSERT INTO x6(x6) VALUES('merge=2500,2'); SELECT level, idx, start_block, leaves_end_block, end_block FROM x6_segdir; } { - 2 0 23695 24147 {41262 633507} + 1 0 719 1171 23694 } do_execsql_test 7.7 { SELECT sum(length(block)) FROM x6_segments - WHERE blockid BETWEEN 23695 AND 24147 -} {633507} - +} {635247} finish_test diff --git a/test/fts4intck1.test b/test/fts4intck1.test new file mode 100644 index 0000000000..6596b2f997 --- /dev/null +++ b/test/fts4intck1.test @@ -0,0 +1,75 @@ +# 2023-10-23 +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# Test PRAGMA integrity_check against and FTS3/FTS4 table. +# + + +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +ifcapable !fts3 { finish_test ; return } + +set ::testprefix fts4intck1 + +proc slang {in} { + return [string map {th d e eh} $in] +} + +db function slang -deterministic -innocuous slang +do_execsql_test 1.0 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT, c TEXT AS (slang(b))); + INSERT INTO t1(b) VALUES('the quick fox jumps over the lazy brown dog'); + SELECT c FROM t1; +} {{deh quick fox jumps ovehr deh lazy brown dog}} + +do_execsql_test 1.1 { + CREATE VIRTUAL TABLE t2 USING fts4(content="t1", c); + INSERT INTO t2(t2) VALUES('rebuild'); + SELECT docid FROM t2 WHERE t2 MATCH 'deh'; +} {1} + +do_execsql_test 1.2 { + PRAGMA integrity_check(t2); +} {ok} + +db close +sqlite3 db test.db +do_execsql_test 2.1 { + PRAGMA integrity_check(t2); +} {{unable to validate the inverted index for FTS4 table main.t2: SQL logic error}} + +db function slang -deterministic -innocuous slang +do_execsql_test 2.2 { + PRAGMA integrity_check(t2); +} {ok} + +proc slang {in} {return $in} +do_execsql_test 2.3 { + PRAGMA integrity_check(t2); +} {{malformed inverted index for FTS4 table main.t2}} + +#------------------------------------------------------------------------- +# Test that integrity-check works on a read-only database. +# +reset_db +do_execsql_test 3.0 { + CREATE VIRTUAL TABLE x1 USING fts4(a, b); + INSERT INTO x1 VALUES('one', 'two'); + INSERT INTO x1 VALUES('three', 'four'); +} +db close +sqlite3 db test.db -readonly 1 + +do_execsql_test 3.1 { + PRAGMA integrity_check; +} {ok} + + + +finish_test diff --git a/test/fts4langid.test b/test/fts4langid.test index a3059931e7..84424ff3c7 100644 --- a/test/fts4langid.test +++ b/test/fts4langid.test @@ -14,7 +14,6 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl -set ::testprefix fts4content # If SQLITE_ENABLE_FTS3 is defined, omit this file. ifcapable !fts3 { @@ -341,6 +340,13 @@ do_test_query1 3.3.4 {"zero one" OR "one two"} { or_merge_lists [rowid_list "zero one"] [rowid_list "one two"] } +do_execsql_test 3.4 { + CREATE TABLE t8c(a, b); + CREATE VIRTUAL TABLE t8 USING fts4(content=t8c, languageid=langid); + INSERT INTO t8(docid, a, b) VALUES(-1, 'one two three', 'x y z'); + SELECT docid FROM t8 WHERE t8 MATCH 'one x' AND langid=0 +} {-1} + #------------------------------------------------------------------------- # Test cases 4.* # @@ -358,31 +364,30 @@ proc build_multilingual_db_2 {db} { } } -ifcapable fts3_tokenizer { - do_test 4.1.0 { - reset_db - set ptr [fts3_test_tokenizer] - execsql { SELECT fts3_tokenizer('testtokenizer', $ptr) } - build_multilingual_db_2 db - } {} - do_execsql_test 4.1.1 { - SELECT docid FROM t4 WHERE t4 MATCH 'quick'; - } {0} - do_execsql_test 4.1.2 { - SELECT docid FROM t4 WHERE t4 MATCH 'quick' AND lid=1; - } {} - do_execsql_test 4.1.3 { - SELECT docid FROM t4 WHERE t4 MATCH 'Quick' AND lid=1; - } {1} - for {set i 0} {$i < 50} {incr i} { - do_execsql_test 4.1.4.$i { - SELECT count(*) FROM t4 WHERE t4 MATCH 'fox' AND lid=$i; - } [expr 0==($i%2)] - } - do_catchsql_test 4.1.5 { - INSERT INTO t4(content, lid) VALUES('hello world', 101) - } {1 {SQL logic error or missing database}} +do_test 4.1.0 { + reset_db + set ptr [fts3_test_tokenizer] + sqlite3_db_config db SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER 1 + execsql { SELECT fts3_tokenizer('testtokenizer', $ptr) } + build_multilingual_db_2 db +} {} +do_execsql_test 4.1.1 { + SELECT docid FROM t4 WHERE t4 MATCH 'quick'; +} {0} +do_execsql_test 4.1.2 { + SELECT docid FROM t4 WHERE t4 MATCH 'quick' AND lid=1; +} {} +do_execsql_test 4.1.3 { + SELECT docid FROM t4 WHERE t4 MATCH 'Quick' AND lid=1; +} {1} +for {set i 0} {$i < 50} {incr i} { + do_execsql_test 4.1.4.$i { + SELECT count(*) FROM t4 WHERE t4 MATCH 'fox' AND lid=$i; + } [expr 0==($i%2)] } +do_catchsql_test 4.1.5 { + INSERT INTO t4(content, lid) VALUES('hello world', 101) +} {1 {SQL logic error}} #------------------------------------------------------------------------- # Test cases 5.* @@ -430,7 +435,7 @@ do_test 5.3.2 { for {set i 0} {$i < 20} {incr i} { execsql { INSERT INTO t6(content, lid) VALUES( - 'I (row '||$i||') belong to langauge N!', $lid + 'I (row '||$i||') belong to language N!', $lid ); } } @@ -482,6 +487,22 @@ foreach lid [list 4 [expr 1<<30]] { do_execsql_test 5.4.$lid.5 { SELECT count(*) FROM t6_segdir; SELECT count(*) FROM t6_segments; - } {4 4} + } {1 2} +} + +reset_db +do_execsql_test 6.0 { + CREATE VIRTUAL TABLE vt0 USING fts4(c0, languageid="lid"); + INSERT INTO vt0 VALUES ('a'), ('b'); + BEGIN; + UPDATE vt0 SET lid = 1 WHERE lid=0; +} +do_execsql_test 6.1 { + INSERT INTO vt0(vt0) VALUES('integrity-check'); + PRAGMA integrity_check; +} {ok} +do_execsql_test 6.2 { + COMMIT; + INSERT INTO vt0(vt0) VALUES('integrity-check'); } finish_test diff --git a/test/fts4lastrowid.test b/test/fts4lastrowid.test new file mode 100644 index 0000000000..7b35e3c53b --- /dev/null +++ b/test/fts4lastrowid.test @@ -0,0 +1,72 @@ +# 2017 Feb 27 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# Tests of the last_insert_rowid functionality with fts4. +# + +set testdir [file dirname $argv0] +source [file join [file dirname [info script]] tester.tcl] +set testprefix fts4lastrowid + +ifcapable !fts3 { + finish_test + return +} + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE t1 USING fts4(str); +} + +do_execsql_test 1.1 { + INSERT INTO t1 VALUES('one string'); + INSERT INTO t1 VALUES('two string'); + INSERT INTO t1 VALUES('three string'); + SELECT last_insert_rowid(); +} {3} + +do_execsql_test 1.2 { + BEGIN; + INSERT INTO t1 VALUES('one string'); + INSERT INTO t1 VALUES('two string'); + INSERT INTO t1 VALUES('three string'); + COMMIT; + SELECT last_insert_rowid(); +} {6} + +do_execsql_test 1.3 { + INSERT INTO t1(rowid, str) VALUES(-22, 'some more text'); + SELECT last_insert_rowid(); +} {-22} + +do_execsql_test 1.4 { + BEGIN; + INSERT INTO t1(rowid, str) VALUES(45, 'some more text'); + INSERT INTO t1(rowid, str) VALUES(46, 'some more text'); + INSERT INTO t1(rowid, str) VALUES(222, 'some more text'); + SELECT last_insert_rowid(); + COMMIT; + SELECT last_insert_rowid(); +} {222 222} + +do_execsql_test 1.5 { + CREATE TABLE x1(x); + INSERT INTO x1 VALUES('john'), ('paul'), ('george'), ('ringo'); + INSERT INTO t1 SELECT x FROM x1; + SELECT last_insert_rowid(); +} {226} + +do_execsql_test 1.6 { + INSERT INTO t1(rowid, str) SELECT rowid+10, x FROM x1; + SELECT last_insert_rowid(); +} {14} + + +finish_test diff --git a/test/fts4merge.test b/test/fts4merge.test index fabb651e64..ffef0e9334 100644 --- a/test/fts4merge.test +++ b/test/fts4merge.test @@ -37,7 +37,7 @@ foreach mod {fts3 fts4} { do_test 1.0 { fts3_build_db_1 -module $mod 1004 } {} do_test 1.1 { fts3_integrity_check t1 } {ok} do_execsql_test 1.1 { - SELECT level, group_concat(idx, ' ') FROM t1_segdir GROUP BY level + SELECT level, string_agg(idx, ' ') FROM t1_segdir GROUP BY level } { 0 {0 1 2 3 4 5 6 7 8 9 10 11} 1 {0 1 2 3 4 5 6 7 8 9 10 11 12 13} @@ -55,8 +55,6 @@ foreach mod {fts3 fts4} { do_execsql_test 1.3 { SELECT level, group_concat(idx, ' ') FROM t1_segdir GROUP BY level } { - 0 {0 1 2 3} - 1 {0 1 2 3 4 5 6} 2 {0 1 2 3} } @@ -69,9 +67,8 @@ foreach mod {fts3 fts4} { } do_execsql_test 1.5 { - SELECT level, group_concat(idx, ' ') FROM t1_segdir GROUP BY level + SELECT level, string_agg(idx, ' ') FROM t1_segdir GROUP BY level } { - 2 {0 1} 3 0 } @@ -89,11 +86,10 @@ foreach mod {fts3 fts4} { 5 {merge=6,%} 6 {merge=6,six} 7 {merge=6,1} - 8 {merge=6,0} } { do_catchsql_test 2.$tn { INSERT INTO t2(t2) VALUES($arg); - } {1 {SQL logic error or missing database}} + } {1 {SQL logic error}} } #------------------------------------------------------------------------- @@ -107,7 +103,7 @@ foreach mod {fts3 fts4} { do_test 3.1 { fts3_integrity_check t2 } {ok} do_execsql_test 3.2 { - SELECT level, group_concat(idx, ' ') FROM t2_segdir GROUP BY level + SELECT level, string_agg(idx, ' ') FROM t2_segdir GROUP BY level } { 0 {0 1 2 3 4 5 6} 1 {0 1 2 3 4} @@ -119,11 +115,7 @@ foreach mod {fts3 fts4} { INSERT INTO t2(t2) VALUES('merge=1000000,2'); SELECT level, group_concat(idx, ' ') FROM t2_segdir GROUP BY level } { - 0 0 - 2 0 - 3 0 4 0 - 6 0 } #------------------------------------------------------------------------- @@ -140,7 +132,7 @@ foreach mod {fts3 fts4} { foreach x {a c b d e f g h i j k l m n o p} { execsql "INSERT INTO t4 VALUES('[string repeat $x 600]')" } - execsql {SELECT level, group_concat(idx, ' ') FROM t4_segdir GROUP BY level} + execsql {SELECT level, string_agg(idx, ' ') FROM t4_segdir GROUP BY level} } {0 {0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15}} foreach {tn expect} { @@ -164,10 +156,11 @@ foreach mod {fts3 fts4} { SELECT quote(value) FROM t4_stat WHERE rowid=1 } {X'0006'} + sqlite3_db_config db DEFENSIVE 0 do_execsql_test 4.4.2 { DELETE FROM t4_stat WHERE rowid=1; INSERT INTO t4(t4) VALUES('merge=1,12'); - SELECT level, group_concat(idx, ' ') FROM t4_segdir GROUP BY level; + SELECT level, string_agg(idx, ' ') FROM t4_segdir GROUP BY level; } "0 {0 1 2 3 4 5} 1 0" @@ -201,30 +194,29 @@ foreach mod {fts3 fts4} { do_execsql_test 5.3 { INSERT INTO t1(t1) VALUES('merge=1,5'); INSERT INTO t1(t1) VALUES('merge=1,5'); - SELECT level, group_concat(idx, ' ') FROM t1_segdir GROUP BY level; + SELECT level, string_agg(idx, ' ') FROM t1_segdir GROUP BY level; } { - 0 {0 1 2} 1 {0 1 2 3 4 5 6 7 8 9 10 11 12 13 14} 2 {0 1 2 3} } - do_execsql_test 5.4 {SELECT quote(value) from t1_stat WHERE rowid=1} {X'0105'} + do_execsql_test 5.4 {SELECT quote(value) from t1_stat WHERE rowid=1} {X'010F'} do_test 5.5 { foreach docid [execsql {SELECT docid FROM t1}] { execsql {INSERT INTO t1 SELECT * FROM t1 WHERE docid=$docid} } } {} - do_execsql_test 5.6 {SELECT quote(value) from t1_stat WHERE rowid=1} {X'0105'} + do_execsql_test 5.6 {SELECT quote(value) from t1_stat WHERE rowid=1} {X'010F'} do_execsql_test 5.7 { SELECT level, group_concat(idx, ' ') FROM t1_segdir GROUP BY level; SELECT quote(value) from t1_stat WHERE rowid=1; } { - 0 {0 1 2 3 4 5 6 7 8 9 10} + 0 {0 1 2 3 4 5 6 7} 1 {0 1 2 3 4 5 6 7 8 9 10 11 12} - 2 {0 1 2 3 4 5 6 7} - X'0105' + 2 {0 1 2 3 4 5 6 7} + X'010F' } do_execsql_test 5.8 { @@ -233,9 +225,8 @@ foreach mod {fts3 fts4} { SELECT level, group_concat(idx, ' ') FROM t1_segdir GROUP BY level; SELECT quote(value) from t1_stat WHERE rowid=1; } { - 0 {0 1 2 3 4} 1 {0 1 2 3 4 5 6 7 8 9 10 11 12 13} - 2 {0 1 2 3 4 5 6 7 8} X'0106' + 2 {0 1 2 3 4 5 6 7 8} X'010E' } do_test 5.8.1 { fts3_integrity_check t1 } ok @@ -253,15 +244,15 @@ foreach mod {fts3 fts4} { SELECT level, group_concat(idx, ' ') FROM t1_segdir GROUP BY level; SELECT quote(value) from t1_stat WHERE rowid=1; } { - 0 0 1 {0 1} 2 0 3 0 X'0106' + 0 {0 1 2 3 4 5 6 7 8 9 10 11} 1 0 2 0 3 0 X'010E' } do_execsql_test 5.11 { INSERT INTO t1(t1) VALUES('merge=1,6'); - SELECT level, group_concat(idx, ' ') FROM t1_segdir GROUP BY level; + SELECT level, string_agg(idx, ' ') FROM t1_segdir GROUP BY level; SELECT quote(value) from t1_stat WHERE rowid=1; } { - 0 0 1 {0 1} 2 0 3 0 X'' + 1 {0 1} 2 0 3 0 X'010E' } #------------------------------------------------------------------------- @@ -335,7 +326,22 @@ foreach mod {fts3 fts4} { execsql { INSERT INTO t1(t1) VALUES('merge=200,10') } expr { ([db total_changes] - $x)>1 } } {0} +} + +#------------------------------------------------------------------------- +# Test cases 8.* - ticket [bf1aab89]. +# +set testprefix fts4merge +reset_db +do_execsql_test 8.0 { + CREATE VIRTUAL TABLE t1 USING fts4(a, order=DESC); + INSERT INTO t1(a) VALUES (0); + INSERT INTO t1(a) VALUES (0); + UPDATE t1 SET a = NULL; +} +do_execsql_test 8.1 { + INSERT INTO t1(t1) VALUES('merge=1,4'); } finish_test diff --git a/test/fts4merge3.test b/test/fts4merge3.test index 329b4d2cc4..08b68b97d9 100644 --- a/test/fts4merge3.test +++ b/test/fts4merge3.test @@ -62,7 +62,7 @@ do_all_bc_test { do_test 1.7 { sql2 { SELECT level, count(*) FROM t2_segdir GROUP BY level ORDER BY 1 - } } [list 0 1 2 18 3 5] + } } {2 15 3 5} # Using the old connection, insert many rows. do_test 1.8 { @@ -73,7 +73,7 @@ do_all_bc_test { do_test 1.9 { sql2 { SELECT level, count(*) FROM t2_segdir GROUP BY level ORDER BY 1 - } } [list 0 13 1 13 2 5 3 6] + } } [list 0 12 1 13 2 4 3 6] # Run a big incr-merge operation on the db. do_test 1.10 { sql1 { INSERT INTO t2(t2) VALUES('merge=2000,2') } } {} @@ -97,7 +97,7 @@ do_all_bc_test { do_test 1.15 { sql2 { SELECT level, count(*) FROM t2_segdir GROUP BY level ORDER BY 1 - } } {6 1} + } } {4 1} } } diff --git a/test/fts4merge4.test b/test/fts4merge4.test index 038e460d0e..12328c23df 100644 --- a/test/fts4merge4.test +++ b/test/fts4merge4.test @@ -16,7 +16,7 @@ source $testdir/tester.tcl source $testdir/fts3_common.tcl set ::testprefix fts4merge4 -ifcapable !fts3 { +ifcapable !fts3||!shared_cache { finish_test return } diff --git a/test/fts4merge5.test b/test/fts4merge5.test new file mode 100644 index 0000000000..1fad778b95 --- /dev/null +++ b/test/fts4merge5.test @@ -0,0 +1,58 @@ +# 2019 October 02 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# This file implements regression tests for SQLite library. The +# focus of this script is testing the FTS4 module. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix fts4merge5 + +# If SQLITE_ENABLE_FTS3 is defined, omit this file. +ifcapable !fts3 { + finish_test + return +} + +source $testdir/genesis.tcl + +do_execsql_test 1.1 { + CREATE TABLE t1(docid, words); +} +fts_kjv_genesis + +do_execsql_test 1.2 { + CREATE VIRTUAL TABLE x1 USING fts3; + INSERT INTO x1(x1) VALUES('nodesize=64'); + INSERT INTO x1(x1) VALUES('maxpending=64'); +} + +do_execsql_test 1.3 { + INSERT INTO x1(docid, content) SELECT * FROM t1; +} + +for {set tn 1} {1} {incr tn} { + set tc1 [db total_changes] + do_execsql_test 1.4.$tn.1 { + INSERT INTO x1(x1) VALUES('merge=1,2'); + } + set tc2 [db total_changes] + + if {($tc2 - $tc1)<2} break + + do_execsql_test 1.4.$tn.1 { + INSERT INTO x1(x1) VALUES('integrity-check'); + } +} + + + +finish_test diff --git a/test/fts4min.test b/test/fts4min.test new file mode 100644 index 0000000000..ca63b39617 --- /dev/null +++ b/test/fts4min.test @@ -0,0 +1,53 @@ +# 2020 February 27 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/fts3_common.tcl +set ::testprefix fts4min + +# If SQLITE_ENABLE_FTS3 is defined, omit this file. +ifcapable !fts3 { + finish_test + return +} + +#------------------------------------------------------------------ +do_execsql_test 0.0 { + CREATE TABLE t1(a NOT NULL, b); + CREATE INDEX i1 ON t1(a); +} + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE ft USING fts3(c); + INSERT INTO ft(docid, c) VALUES(22, 'hello world'); + INSERT INTO ft(docid, c) VALUES(44, 'hello world'); + INSERT INTO ft(docid, c) VALUES(11, 'hello world'); +} + +do_eqp_test 1.1.1 { + SELECT max(rowid) FROM ft +} {VIRTUAL TABLE INDEX 0:DESC} + +do_eqp_test 1.1.2 { + SELECT min(rowid) FROM ft +} {VIRTUAL TABLE INDEX 0:ASC} + +do_execsql_test 1.2.1 { + SELECT max(rowid) FROM ft +} {44} + +do_execsql_test 1.2.2 { + SELECT min(rowid) FROM ft +} {11} + +finish_test diff --git a/test/fts4noti.test b/test/fts4noti.test index 6707203970..87a05714bf 100644 --- a/test/fts4noti.test +++ b/test/fts4noti.test @@ -173,7 +173,7 @@ do_execsql_test 6.1.1 { CREATE VIRTUAL TABLE t1 USING fts4( poiCategory, poiCategoryId, notindexed=poiCategoryId ); - INSERT INTO t1(poiCategory, poiCategoryId) values ("Restaurant", 6021); + INSERT INTO t1(poiCategory, poiCategoryId) values ('Restaurant', 6021); } do_execsql_test 6.1.2 { @@ -194,7 +194,7 @@ do_execsql_test 6.2.1 { CREATE VIRTUAL TABLE t1 USING fts4( poiCategory, poiCategoryId, notindexed=poiCategory ); - INSERT INTO t1(poiCategory, poiCategoryId) values ("Restaurant", 6021); + INSERT INTO t1(poiCategory, poiCategoryId) values ('Restaurant', 6021); } do_execsql_test 6.2.2 { diff --git a/test/fts4onepass.test b/test/fts4onepass.test index 46cb4b794b..344be4b1df 100644 --- a/test/fts4onepass.test +++ b/test/fts4onepass.test @@ -143,4 +143,18 @@ foreach {tn tcl1 tcl2} { eval $tcl2 } +do_execsql_test 4.0 { + CREATE VIRTUAL TABLE zt USING fts4(a, b); + INSERT INTO zt(rowid, a, b) VALUES(1, 'unus duo', NULL); + INSERT INTO zt(rowid, a, b) VALUES(2, NULL, NULL); + + BEGIN; + UPDATE zt SET b='septum' WHERE rowid = 1; + UPDATE zt SET b='octo' WHERE rowid = 1; + COMMIT; + + SELECT count(*) FROM zt_segdir; +} {3} + + finish_test diff --git a/test/fts4opt.test b/test/fts4opt.test new file mode 100644 index 0000000000..4b3d003239 --- /dev/null +++ b/test/fts4opt.test @@ -0,0 +1,214 @@ +# 2016 March 8 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/fts3_common.tcl +set ::testprefix fts4opt + +# If SQLITE_ENABLE_FTS3 is defined, omit this file. +ifcapable !fts3 { + finish_test + return +} + +# Create the fts_kjv_genesis procedure which fills and FTS3/4 table +# with the complete text of the Book of Genesis. +# +source $testdir/genesis.tcl + +do_execsql_test 1.0 { CREATE TABLE t1(docid, words) } +fts_kjv_genesis + +#------------------------------------------------------------------------- +# Argument $db is an open database handle. $tbl is the name of an FTS3/4 +# table with the database. This command rearranges the contents of the +# %_segdir table so that all segments within each index are on the same +# level. This means that the 'merge' command can then be used for an +# incremental optimize routine. +# +proc prepare_for_optimize {db tbl} { + sqlite3_db_config $db DEFENSIVE 0 + $db eval [string map [list % $tbl] { + BEGIN; + CREATE TEMP TABLE tmp_segdir( + level, idx, start_block, leaves_end_block, end_block, root + ); + + INSERT INTO temp.tmp_segdir + SELECT + 1024*(o.level / 1024) + 32, -- level + sum(o.level<i.level OR (o.level=i.level AND o.idx>i.idx)), -- idx + o.start_block, o.leaves_end_block, o.end_block, o.root -- other + FROM %_segdir o, %_segdir i + WHERE (o.level / 1024) = (i.level / 1024) + GROUP BY o.level, o.idx; + + DELETE FROM %_segdir; + INSERT INTO %_segdir SELECT * FROM temp.tmp_segdir; + DROP TABLE temp.tmp_segdir; + + COMMIT; + }] +} + +do_test 1.1 { + execsql { CREATE VIRTUAL TABLE t2 USING fts4(words, prefix="1,2,3") } + foreach {docid words} [db eval { SELECT * FROM t1 }] { + execsql { INSERT INTO t2(docid, words) VALUES($docid, $words) } + } +} {} + +do_execsql_test 1.2 { + SELECT level, count(*) FROM t2_segdir GROUP BY level +} { + 0 13 1 15 2 5 + 1024 13 1025 15 1026 5 + 2048 13 2049 15 2050 5 + 3072 13 3073 15 3074 5 +} + +do_execsql_test 1.3 { INSERT INTO t2(t2) VALUES('integrity-check') } +prepare_for_optimize db t2 +do_execsql_test 1.4 { INSERT INTO t2(t2) VALUES('integrity-check') } + +do_execsql_test 1.5 { + SELECT level, count(*) FROM t2_segdir GROUP BY level +} { + 32 33 + 1056 33 + 2080 33 + 3104 33 +} + +do_test 1.6 { + while 1 { + set tc1 [db total_changes] + execsql { INSERT INTO t2(t2) VALUES('merge=5,2') } + set tc2 [db total_changes] + if {($tc2 - $tc1) < 2} break + } + execsql { SELECT level, count(*) FROM t2_segdir GROUP BY level } +} {33 1 1057 1 2081 1 3105 1} +do_execsql_test 1.7 { INSERT INTO t2(t2) VALUES('integrity-check') } + +do_execsql_test 1.8 { + INSERT INTO t2(words) SELECT words FROM t1; + SELECT level, count(*) FROM t2_segdir GROUP BY level; +} {0 2 1024 2 2048 2 3072 2} + +#------------------------------------------------------------------------- + +do_execsql_test 2.0 { + DELETE FROM t2; +} +do_test 2.1 { + foreach {docid words} [db eval { SELECT * FROM t1 }] { + execsql { INSERT INTO t2(docid, words) VALUES($docid, $words) } + } + + set i 0 + foreach {docid words} [db eval { SELECT * FROM t1 }] { + if {[incr i] % 2} { execsql { DELETE FROM t2 WHERE docid = $docid } } + } + + set i 0 + foreach {docid words} [db eval { SELECT * FROM t1 }] { + if {[incr i] % 3} { + execsql { INSERT OR REPLACE INTO t2(docid, words) VALUES($docid, $words) } + } + } +} {} + +do_execsql_test 2.2 { + SELECT level, count(*) FROM t2_segdir GROUP BY level +} { + 0 10 1 15 2 12 + 1024 10 1025 15 1026 12 + 2048 10 2049 15 2050 12 + 3072 10 3073 15 3074 12 +} + +do_execsql_test 2.3 { INSERT INTO t2(t2) VALUES('integrity-check') } +prepare_for_optimize db t2 +do_execsql_test 2.4 { INSERT INTO t2(t2) VALUES('integrity-check') } + +do_execsql_test 2.5 { + SELECT level, count(*) FROM t2_segdir GROUP BY level +} { + 32 37 + 1056 37 + 2080 37 + 3104 37 +} + +do_test 2.6 { + while 1 { + set tc1 [db total_changes] + execsql { INSERT INTO t2(t2) VALUES('merge=5,2') } + set tc2 [db total_changes] + if {($tc2 - $tc1) < 2} break + } + execsql { SELECT level, count(*) FROM t2_segdir GROUP BY level } +} {33 1 1057 1 2081 1 3105 1} +do_execsql_test 2.7 { INSERT INTO t2(t2) VALUES('integrity-check') } + +do_execsql_test 2.8 { + INSERT INTO t2(words) SELECT words FROM t1; + SELECT level, count(*) FROM t2_segdir GROUP BY level; +} {0 2 1024 2 2048 2 3072 2} + +#------------------------------------------------------------------------- +# Check that 'optimize' works when there is data in the in-memory hash +# table, but no segments at all on disk. +# +do_execsql_test 3.1 { + CREATE VIRTUAL TABLE fts USING fts4 (t); + INSERT INTO fts (fts) VALUES ('optimize'); +} +do_execsql_test 3.2 { + INSERT INTO fts(fts) VALUES('integrity-check'); + SELECT count(*) FROM fts_segdir; +} {0} +do_execsql_test 3.3 { + BEGIN; + INSERT INTO fts (rowid, t) VALUES (2, 'test'); + INSERT INTO fts (fts) VALUES ('optimize'); + COMMIT; + SELECT level, idx FROM fts_segdir; +} {0 0} +do_execsql_test 3.4 { + INSERT INTO fts(fts) VALUES('integrity-check'); + SELECT rowid FROM fts WHERE fts MATCH 'test'; +} {2} +do_execsql_test 3.5 { + INSERT INTO fts (fts) VALUES ('optimize'); + INSERT INTO fts(fts) VALUES('integrity-check'); +} +do_test 3.6 { + set c1 [db total_changes] + execsql { INSERT INTO fts (fts) VALUES ('optimize') } + expr {[db total_changes] - $c1} +} {1} +do_test 3.7 { + execsql { INSERT INTO fts (rowid, t) VALUES (3, 'xyz') } + set c1 [db total_changes] + execsql { INSERT INTO fts (fts) VALUES ('optimize') } + expr {([db total_changes] - $c1) > 1} +} {1} +do_test 3.8 { + set c1 [db total_changes] + execsql { INSERT INTO fts (fts) VALUES ('optimize') } + expr {[db total_changes] - $c1} +} {1} + +finish_test diff --git a/test/fts4record.test b/test/fts4record.test new file mode 100644 index 0000000000..f1a3eafa75 --- /dev/null +++ b/test/fts4record.test @@ -0,0 +1,120 @@ +# 2019 September 18 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# This file implements regression tests for SQLite library. The +# focus of this script is testing the FTS4 module. +# +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/fts3_common.tcl +set testprefix fts4record + +# If SQLITE_ENABLE_FTS3 is defined, omit this file. +ifcapable !fts3 { + finish_test + return +} + +sqlite3_fts3_may_be_corrupt 1 + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE t1 USING fts4(x); + INSERT INTO t1 VALUES('terma terma terma termb'); +} + +do_execsql_test 1.1 { + SELECT quote(root) FROM t1_segdir +} { + X'00057465726D6105010203030004016203010500' +} + +proc make_record_wrapper {args} { make_fts3record $args } +db func record make_record_wrapper + +do_execsql_test 1.2 { + select quote( + record(0, 5, 'terma', 5, 1, 2, 3, 3, 0, + 4, 1, 'b' , 3, 1, 5, 0 + ) ); +} { + X'00057465726D6105010203030004016203010500' +} + +do_execsql_test 1.3.1 { + UPDATE t1_segdir SET root = + record(0, 5, 'terma', 5, 1, 2, 3, 3, 0, + 4, 1, 'b' , 3, 1, 5, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 + ); +} + +do_catchsql_test 1.3.2 { + SELECT snippet(t1) FROM t1 WHERE t1 MATCH 'term*' +} {1 {database disk image is malformed}} + +do_execsql_test 1.4.1 { + UPDATE t1_segdir SET root = + record(0, 5, 'terma', 5, 1, 2, 3, 3, 0, + 4, 1, 'b' , 4, 1, 5, + 256, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 + ); +} + +do_catchsql_test 1.4.2 { + SELECT snippet(t1) FROM t1 WHERE t1 MATCH 'term*' +} {1 {database disk image is malformed}} + +do_execsql_test 1.4.3 { + SELECT quote(root) FROM t1_segdir +} { + X'00057465726D610501020303000401620401058002010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010100' +} + +do_execsql_test 1.5.1 { + UPDATE t1_segdir SET root = + record(0, 5, 'terma', 5, 1, 2, 3, 3, 0, + 4, 1, 'b' , 4, 1, 5, + 256, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 + ); +} + +do_catchsql_test 1.4.2 { + SELECT snippet(t1) FROM t1 WHERE t1 MATCH 'term*' +} {1 {database disk image is malformed}} + +do_execsql_test 1.4.3 { + SELECT quote(root) FROM t1_segdir +} { + X'00057465726D610501020303000401620401058002010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010100' +} + + +do_execsql_test 1.5.1 { + UPDATE t1_segdir SET root = + X'00057465726D61050102030300040162040105FF00010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010100' +} + +do_catchsql_test 1.5.2 { + SELECT snippet(t1) FROM t1 WHERE t1 MATCH 'term*' +} {1 {database disk image is malformed}} + +do_catchsql_test 1.5.3 { + INSERT INTO t1(t1) VALUES('integrity-check'); +} {1 {database disk image is malformed}} + +finish_test diff --git a/test/fts4rename.test b/test/fts4rename.test new file mode 100644 index 0000000000..5571ea7b15 --- /dev/null +++ b/test/fts4rename.test @@ -0,0 +1,43 @@ +# 2019 April 30 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/fts3_common.tcl +set ::testprefix fts4rename + +# If SQLITE_ENABLE_FTS3 is defined, omit this file. +ifcapable !fts3 { + finish_test + return +} + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE temp.t1 USING fts3(a); + BEGIN; + CREATE TABLE t2(x); +} {} + +do_catchsql_test 1.1 { + ALTER TABLE t1_content RENAME c0a TO docid; +} {1 {error in table t1_content after rename: duplicate column name: docid}} + +do_catchsql_test 1.2 { + UPDATE t1 SET Col0 = 1 ; +} {1 {no such column: Col0}} + +do_catchsql_test 1.3 { + ROLLBACK; + DROP TABLE t1; +} {0 {}} + +finish_test diff --git a/test/fts4umlaut.test b/test/fts4umlaut.test new file mode 100644 index 0000000000..a5a652fb42 --- /dev/null +++ b/test/fts4umlaut.test @@ -0,0 +1,64 @@ +# 2018 December 3 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# This file implements regression tests for SQLite library. The +# focus of this script is testing the FTS5 module. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix fts4umlaut + +ifcapable !fts3 { + finish_test + return +} + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE t1 USING fts4(x, tokenize=unicode61); + CREATE VIRTUAL TABLE t2 USING fts4( + x, + tokenize=unicode61 "remove_diacritics=2" + ); +} + +foreach {tn q res1 res2} { + 1 "Hà Nội" 0 1 + 2 "Hà Noi" 1 1 + 3 "Ha Noi" 1 1 + 4 "Ha N\u1ed9i" 0 1 + 5 "Ha N\u006fi" 1 1 + 6 "Ha N\u006f\u0302i" 1 1 + 7 "Ha N\u006f\u0323\u0302i" 1 1 +} { + do_execsql_test 1.$tn.1 { + DELETE FROM t1; + INSERT INTO t1(rowid, x) VALUES (1, 'Ha Noi'); + SELECT count(*) FROM t1 WHERE t1 MATCH $q + } $res1 + do_execsql_test 1.$tn.2 { + DELETE FROM t1; + INSERT INTO t1(rowid, x) VALUES (1, $q); + SELECT count(*) FROM t1 WHERE t1 MATCH 'Ha Noi' + } $res1 + + do_execsql_test 1.$tn.3 { + DELETE FROM t2; + INSERT INTO t2(rowid, x) VALUES (1, 'Ha Noi'); + SELECT count(*) FROM t2 WHERE t2 MATCH $q + } $res2 + do_execsql_test 1.$tn.4 { + DELETE FROM t2; + INSERT INTO t2(rowid, x) VALUES (1, $q); + SELECT count(*) FROM t2 WHERE t2 MATCH 'Ha Noi' + } $res2 +} + +finish_test diff --git a/test/fts4unicode.test b/test/fts4unicode.test index 500cfcdcaa..facf2bf9c4 100644 --- a/test/fts4unicode.test +++ b/test/fts4unicode.test @@ -384,7 +384,9 @@ foreach T $tokenizers { do_isspace_test 6.$T.13 $T 8200 do_isspace_test 6.$T.14 $T 8201 do_isspace_test 6.$T.15 $T 8202 - do_isspace_test 6.$T.16 $T 8239 + if {$T!="icu"} { + do_isspace_test 6.$T.16 $T 8239 + } do_isspace_test 6.$T.17 $T 8287 do_isspace_test 6.$T.18 $T 12288 @@ -565,4 +567,22 @@ do_execsql_test 11.1 { berlin@street sydney.road } +# Test for embedded nul characters in fts4 unicode index. +# +do_execsql_test 12.0 { + CREATE VIRTUAL TABLE t12 USING fts4(tokenize=unicode61); + INSERT INTO t12 VALUES('abc' || char(0) || 'def'); + SELECT hex(CAST(content AS blob)) FROM t12; +} {61626300646566} +do_execsql_test 12.1 { + INSERT INTO t12(t12) VALUES('integrity-check'); +} {} +do_execsql_test 12.2 { + CREATE VIRTUAL TABLE t12aux USING fts4aux(t12); + SELECT * FROM t12aux; +} {abc * 1 1 abc 0 1 1} +do_execsql_test 12.3 { + SELECT hex(CAST(content AS blob)) FROM t12 WHERE t12 MATCH 'abc' +} {61626300646566} + finish_test diff --git a/test/fts4upfrom.test b/test/fts4upfrom.test new file mode 100644 index 0000000000..889b64ad68 --- /dev/null +++ b/test/fts4upfrom.test @@ -0,0 +1,139 @@ +# 2020 February 24 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# This file implements regression tests for SQLite library. The +# focus of this script is testing UPDATE statements with FROM clauses +# against FTS4 tables. +# +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix fts4upfrom + +# If SQLITE_ENABLE_FTS3 is defined, omit this file. +ifcapable !fts3 { + finish_test + return +} + +foreach {tn create_table} { + 0 { CREATE VIRTUAL TABLE ft USING fts5(a, b, c) } + 1 { CREATE VIRTUAL TABLE ft USING fts3(a, b, c) } + 2 { CREATE TABLE ft(a, b, c) } + 3 { + CREATE TABLE real(a, b, c); + CREATE INDEX i1 ON real(a); + CREATE VIEW ft AS SELECT rowid, a, b, c FROM real; + CREATE TRIGGER tr1 INSTEAD OF INSERT ON ft BEGIN + INSERT INTO real(rowid, a, b, c) VALUES(new.rowid, new.a, new.b, new.c); + END; + CREATE TRIGGER tr2 INSTEAD OF UPDATE ON ft BEGIN + UPDATE real SET rowid=new.rowid, a=new.a, b=new.b, c=new.c + WHERE rowid=old.rowid; + END; + } +} { + if {$tn==0} { ifcapable !fts5 { continue } } + catchsql { DROP VIEW IF EXISTS changes } + catchsql { DROP TABLE IF EXISTS ft } + catchsql { DROP VIEW IF EXISTS ft } + execsql $create_table + + do_execsql_test 1.$tn.0 { + INSERT INTO ft(a, b, c) VALUES('a', NULL, 'apple'); + INSERT INTO ft(a, b, c) VALUES('b', NULL, 'banana'); + INSERT INTO ft(a, b, c) VALUES('c', NULL, 'cherry'); + INSERT INTO ft(a, b, c) VALUES('d', NULL, 'damson plum'); + } + + do_execsql_test 1.$tn.1 { + SELECT a, b, c FROM ft ORDER BY rowid; + } { + a {} apple + b {} banana + c {} cherry + d {} {damson plum} + } + + do_execsql_test 1.$tn.2 { + UPDATE ft SET b=o.c FROM ft AS o WHERE (ft.a == char(unicode(o.a)+1)) + } + + do_execsql_test 1.$tn.3 { + SELECT a, b, c FROM ft ORDER BY rowid; + } { + a {} apple + b apple banana + c banana cherry + d cherry {damson plum} + } + + do_catchsql_test 1.$tn.4 { + UPDATE ft SET c=v FROM changes WHERE a=k; + } {1 {no such table: changes}} + + do_execsql_test 1.$tn.5 { + create view changes(k, v) AS + VALUES( 'd', 'dewberry' ) UNION ALL + VALUES( 'c', 'clementine' ) UNION ALL + VALUES( 'b', 'blueberry' ) UNION ALL + VALUES( 'a', 'apricot' ) + ; + } + + do_execsql_test 1.$tn.6 { + UPDATE ft SET c=v FROM changes WHERE a=k; + } + + do_execsql_test 1.$tn.7 { + SELECT rowid, a, b, c FROM ft ORDER BY rowid; + } { + 1 a {} apricot + 2 b apple blueberry + 3 c banana clementine + 4 d cherry dewberry + } + + do_execsql_test 1.$tn.8 " + WITH x1(o, n) AS ( + VALUES(1, 11) UNION ALL + VALUES(2, 12) UNION ALL + VALUES(3, 13) UNION ALL + VALUES(4, 14) + ) + SELECT ft.rowid, a, b, c, o, n FROM ft, x1 WHERE ft.rowid = o; + " { + 1 a {} apricot 1 11 + 2 b apple blueberry 2 12 + 3 c banana clementine 3 13 + 4 d cherry dewberry 4 14 + } + + set ROWID rowid + if {$tn==1} { set ROWID docid } + do_execsql_test 1.$tn.9 " + WITH x1(o, n) AS ( + VALUES(1, 11) UNION ALL + VALUES(2, 12) UNION ALL + VALUES(3, 13) UNION ALL + VALUES(4, 14) + ) + UPDATE ft SET $ROWID = n FROM x1 WHERE ft.rowid = o; + SELECT rowid, a, b, c FROM ft ORDER BY rowid; + " { + 11 a {} apricot + 12 b apple blueberry + 13 c banana clementine + 14 d cherry dewberry + } +} + +finish_test diff --git a/test/func.test b/test/func.test index 98ae8ddeb5..4e5f617e74 100644 --- a/test/func.test +++ b/test/func.test @@ -42,6 +42,10 @@ do_test func-0.1 { do_test func-1.0 { execsql {SELECT length(t1) FROM tbl1 ORDER BY t1} } {4 2 7 8 4} +set isutf16 [regexp 16 [db one {PRAGMA encoding}]] +do_execsql_test func-1.0b { + SELECT octet_length(t1) FROM tbl1 ORDER BY t1; +} [expr {$isutf16?"8 4 14 16 8":"4 2 7 8 4"}] do_test func-1.1 { set r [catch {execsql {SELECT length(*) FROM tbl1 ORDER BY t1}} msg] lappend r $msg @@ -57,9 +61,29 @@ do_test func-1.3 { do_test func-1.4 { execsql {SELECT coalesce(length(a),-1) FROM t2} } {1 -1 3 -1 5} +do_execsql_test func-1.5 { + SELECT octet_length(12345); +} [expr {(1+($isutf16!=0))*5}] +db null NULL +do_execsql_test func-1.6 { + SELECT octet_length(NULL); +} {NULL} +do_execsql_test func-1.7 { + SELECT octet_length(7.5); +} [expr {(1+($isutf16!=0))*3}] +do_execsql_test func-1.8 { + SELECT octet_length(x'30313233'); +} {4} +do_execsql_test func-1.9 { + WITH c(x) AS (VALUES(char(350,351,352,353,354))) + SELECT length(x), octet_length(x) FROM c; +} {5 10} + + # Check out the substr() function # +db null {} do_test func-2.0 { execsql {SELECT substr(t1,1,2) FROM tbl1 ORDER BY t1} } {fr is pr so th} @@ -93,6 +117,15 @@ do_test func-2.9 { do_test func-2.10 { execsql {SELECT substr(a,2,2) FROM t2} } {{} {} 45 {} 78} +do_test func-2.11 { + execsql {SELECT substr('abcdefg',0x100000001,2)} +} {{}} +do_test func-2.12 { + execsql {SELECT substr('abcdefg',1,0x100000002)} +} {abcdefg} +do_test func-2.13 { + execsql {SELECT quote(substr(x'313233343536373839',0x7ffffffffffffffe,5))} +} {X''} # Only do the following tests if TCL has UTF-8 capabilities # @@ -237,9 +270,6 @@ ifcapable floatingpoint { catchsql {SELECT round(b,2.0) FROM t1 ORDER BY b} } {0 {-2.0 1.23 2.0}} # Verify some values reported on the mailing list. - # Some of these fail on MSVC builds with 64-bit - # long doubles, but not on GCC builds with 80-bit - # long doubles. for {set i 1} {$i<999} {incr i} { set x1 [expr 40222.5 + $i] set x2 [expr 40223.0 + $i] @@ -315,6 +345,12 @@ ifcapable floatingpoint { do_test func-4.38 { execsql {SELECT round(9999999999999.556,2);} } {9999999999999.56} + do_test func-4.39 { + string tolower [db eval {SELECT round(1e500), round(-1e500);}] + } {inf -inf} + do_execsql_test func-4.40 { + SELECT round(123.456 , 4294967297); + } {123.456} } # Test the upper() and lower() functions @@ -507,6 +543,17 @@ if {$encoding=="UTF-16le"} { execsql {SELECT hex(replace('aabcdefg','a','aaa'))} } {616161616161626364656667} } +do_execsql_test func-9.14 { + WITH RECURSIVE c(x) AS ( + VALUES(1) + UNION ALL + SELECT x+1 FROM c WHERE x<1040 + ) + SELECT + count(*), + sum(length(replace(printf('abc%.*cxyz',x,'m'),'m','nnnn'))-(6+x*4)) + FROM c; +} {1040 0} # Use the "sqlite_register_test_function" TCL command which is part of # the text fixture in order to verify correct operation of some of @@ -748,6 +795,11 @@ do_test func-16.1 { } } {X'616263' NULL} +# Test the quote function for +Inf and -Inf +do_execsql_test func-16.2 { + SELECT quote(4.2e+859), quote(-7.8e+904); +} {9.0e+999 -9.0e+999} + # Correctly handle function error messages that include %. Ticket #1354 # do_test func-17.1 { @@ -825,30 +877,13 @@ do_test func-18.11 { } } integer ifcapable floatingpoint { - do_test func-18.12 { - catchsql { - INSERT INTO t6 VALUES(1<<62); - SELECT sum(x) - ((1<<62)*2.0+1) from t6; - } - } {1 {integer overflow}} - do_test func-18.13 { - execsql { - SELECT total(x) - ((1<<62)*2.0+1) FROM t6 - } - } 0.0 -} -ifcapable !floatingpoint { - do_test func-18.12 { - catchsql { - INSERT INTO t6 VALUES(1<<62); - SELECT sum(x) - ((1<<62)*2+1) from t6; - } + do_catchsql_test func-18.12 { + INSERT INTO t6 VALUES(1<<62); + SELECT sum(x) - ((1<<62)*2.0+1) from t6; } {1 {integer overflow}} - do_test func-18.13 { - execsql { - SELECT total(x) - ((1<<62)*2+1) FROM t6 - } - } 0.0 + do_catchsql_test func-18.13 { + SELECT total(x) - ((1<<62)*2.0+1) FROM t6 + } {0 0.0} } if {[working_64bit_int]} { do_test func-18.14 { @@ -993,34 +1028,37 @@ do_test func-21.2 { } {1 {wrong number of arguments to function replace()}} do_test func-21.3 { execsql { - SELECT typeof(replace("This is the main test string", NULL, "ALT")); + SELECT typeof(replace('This is the main test string', NULL, 'ALT')); } } {null} do_test func-21.4 { execsql { - SELECT typeof(replace(NULL, "main", "ALT")); + SELECT typeof(replace(NULL, 'main', 'ALT')); } } {null} do_test func-21.5 { execsql { - SELECT typeof(replace("This is the main test string", "main", NULL)); + SELECT typeof(replace('This is the main test string', 'main', NULL)); } } {null} do_test func-21.6 { execsql { - SELECT replace("This is the main test string", "main", "ALT"); + SELECT replace('This is the main test string', 'main', 'ALT'); } } {{This is the ALT test string}} do_test func-21.7 { execsql { - SELECT replace("This is the main test string", "main", "larger-main"); + SELECT replace('This is the main test string', 'main', 'larger-main'); } } {{This is the larger-main test string}} do_test func-21.8 { execsql { - SELECT replace("aaaaaaa", "a", "0123456789"); + SELECT replace('aaaaaaa', 'a', '0123456789'); } } {0123456789012345678901234567890123456789012345678901234567890123456789} +do_execsql_test func-21.9 { + SELECT typeof(replace(1,'',0)); +} {text} ifcapable tclvar { do_test func-21.9 { @@ -1097,6 +1135,13 @@ do_test func-22.22 { execsql {SELECT typeof(trim('hello',NULL));} } {null} +# 2021-06-15 - infinite loop due to unsigned character counter +# overflow, reported by Zimuzo Ezeozue +# +do_execsql_test func-22.23 { + SELECT trim('xyzzy',x'c0808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080'); +} {xyzzy} + # This is to test the deprecated sqlite3_aggregate_count() API. # ifcapable deprecated { @@ -1108,18 +1153,18 @@ ifcapable deprecated { } {3} } -# The group_concat() function. +# The group_concat() and string_agg() functions. # do_test func-24.1 { execsql { - SELECT group_concat(t1) FROM tbl1 + SELECT group_concat(t1), string_agg(t1,',') FROM tbl1 } -} {this,program,is,free,software} +} {this,program,is,free,software this,program,is,free,software} do_test func-24.2 { execsql { - SELECT group_concat(t1,' ') FROM tbl1 + SELECT group_concat(t1,' '), string_agg(t1,' ') FROM tbl1 } -} {{this program is free software}} +} {{this program is free software} {this program is free software}} do_test func-24.3 { execsql { SELECT group_concat(t1,' ' || rowid || ' ') FROM tbl1 @@ -1132,9 +1177,9 @@ do_test func-24.4 { } {{}} do_test func-24.5 { execsql { - SELECT group_concat(t1,NULL) FROM tbl1 + SELECT group_concat(t1,NULL), string_agg(t1,NULL) FROM tbl1 } -} {thisprogramisfreesoftware} +} {thisprogramisfreesoftware thisprogramisfreesoftware} do_test func-24.6 { execsql { SELECT 'BEGIN-'||group_concat(t1) FROM tbl1 @@ -1150,7 +1195,9 @@ set midargs {} unset -nocomplain midres set midres {} unset -nocomplain result -for {set i 1} {$i<[sqlite3_limit db SQLITE_LIMIT_FUNCTION_ARG -1]} {incr i} { +set limit [sqlite3_limit db SQLITE_LIMIT_FUNCTION_ARG -1] +if {$limit>400} {set limit 400} +for {set i 1} {$i<$limit} {incr i} { append midargs ,'/$i' append midres /$i set result [md5 \ @@ -1194,7 +1241,7 @@ do_test func-24.12 { WHEN 'program' THEN null ELSE t1 END) FROM tbl1 } } {,is,free,software} -# Tests to verify ticket http://www.sqlite.org/src/tktview/55746f9e65f8587c0 +# Tests to verify ticket http://sqlite.org/src/tktview/55746f9e65f8587c0 do_test func-24.13 { execsql { SELECT typeof(group_concat(x)) FROM (SELECT '' AS x); @@ -1228,7 +1275,8 @@ do_test func-26.1 { # do_test func-26.2 { set a {} - for {set i 1} {$i<=$::SQLITE_MAX_FUNCTION_ARG} {incr i} { + set limit $::SQLITE_MAX_FUNCTION_ARG + for {set i 1} {$i<=$limit} {incr i} { lappend a $i } db eval " @@ -1246,7 +1294,8 @@ do_test func-26.3 { } {1 {too many arguments on function nullx_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789}} do_test func-26.4 { set a {} - for {set i 1} {$i<=$::SQLITE_MAX_FUNCTION_ARG-1} {incr i} { + set limit [expr {$::SQLITE_MAX_FUNCTION_ARG-1}] + for {set i 1} {$i<=$limit} {incr i} { lappend a $i } catchsql " @@ -1294,7 +1343,7 @@ do_test func-29.1 { CREATE TABLE t29(id INTEGER PRIMARY KEY, x, y); INSERT INTO t29 VALUES(1, 2, 3), (2, NULL, 4), (3, 4.5, 5); INSERT INTO t29 VALUES(4, randomblob(1000000), 6); - INSERT INTO t29 VALUES(5, "hello", 7); + INSERT INTO t29 VALUES(5, 'hello', 7); } db close sqlite3 db test.db @@ -1380,4 +1429,170 @@ for {set i 65536} {$i<=0x10ffff} {incr i 139} { do_execsql_test func-31.1 { SELECT char(), length(char()), typeof(char()) } {{} 0 text} + +# sqlite3_value_frombind() +# +do_execsql_test func-32.100 { + SELECT test_frombind(1,2,3,4); +} {0} +do_execsql_test func-32.110 { + SELECT test_frombind(1,2,?,4); +} {4} +do_execsql_test func-32.120 { + SELECT test_frombind(1,(?),4,?+7); +} {2} +do_execsql_test func-32.130 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a,b,c,e,f); + INSERT INTO t1 VALUES(1,2.5,'xyz',x'e0c1b2a3',null); + SELECT test_frombind(a,b,c,e,f,$xyz) FROM t1; +} {32} +do_execsql_test func-32.140 { + SELECT test_frombind(a,b,c,e,f,$xyz+f) FROM t1; +} {0} +do_execsql_test func-32.150 { + SELECT test_frombind(x.a,y.b,x.c,:123,y.e,x.f,$xyz+y.f) FROM t1 x, t1 y; +} {8} + +# 2019-08-15 +# Direct-only functions. +# +proc testdirectonly {x} {return [expr {$x*2}]} +do_test func-33.1 { + db func testdirectonly -directonly testdirectonly + db eval {SELECT testdirectonly(15)} +} {30} +do_catchsql_test func-33.2 { + CREATE VIEW v33(y) AS SELECT testdirectonly(15); + SELECT * FROM v33; +} {1 {unsafe use of testdirectonly()}} +do_execsql_test func-33.3 { + SELECT * FROM (SELECT testdirectonly(15)) AS v33; +} {30} +do_execsql_test func-33.4 { + WITH c(x) AS (SELECT testdirectonly(15)) + SELECT * FROM c; +} {30} +do_catchsql_test func-33.5 { + WITH c(x) AS (SELECT * FROM v33) + SELECT * FROM c; +} {1 {unsafe use of testdirectonly()}} +do_execsql_test func-33.10 { + CREATE TABLE t33a(a,b); + CREATE TABLE t33b(x,y); + CREATE TRIGGER r1 AFTER INSERT ON t33a BEGIN + INSERT INTO t33b(x,y) VALUES(testdirectonly(new.a),new.b); + END; +} {} +do_catchsql_test func-33.11 { + INSERT INTO t33a VALUES(1,2); +} {1 {unsafe use of testdirectonly()}} + +ifcapable altertable { +do_execsql_test func-33.20 { + ALTER TABLE t33a RENAME COLUMN a TO aaa; + SELECT sql FROM sqlite_master WHERE name='r1'; +} {{CREATE TRIGGER r1 AFTER INSERT ON t33a BEGIN + INSERT INTO t33b(x,y) VALUES(testdirectonly(new.aaa),new.b); + END}} +} + +# 2020-01-09 Yongheng fuzzer find +# The bug is in the register-validity debug logic, not in the SQLite core +# and as such it only impacts debug builds. Release builds work fine. +# +reset_db +do_execsql_test func-34.10 { + CREATE TABLE t1(a INT CHECK( + datetime( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10,11,12,13,14,15,16,17,18,19, + 20,21,22,23,24,25,26,27,28,29, + 30,31,32,33,34,35,36,37,38,39, + 40,41,42,43,44,45,46,47,48,a) + ) + ); + INSERT INTO t1(a) VALUES(1),(2); + SELECT * FROM t1; +} {1 2} + +# 2020-03-11 COALESCE() should short-circuit +# See also ticket 3c9eadd2a6ba0aa5 +# Both issues stem from the fact that functions that could +# throw exceptions were being factored out into initialization +# code. The fix was to put those function calls inside of +# OP_Once instead. +# +reset_db +do_execsql_test func-35.100 { + CREATE TABLE t1(x); + SELECT coalesce(x, abs(-9223372036854775808)) FROM t1; +} {} +do_execsql_test func-35.110 { + SELECT coalesce(x, 'xyz' LIKE printf('%.1000000c','y')) FROM t1; +} {} +do_execsql_test func-35.200 { + CREATE TABLE t0(c0 CHECK(ABS(-9223372036854775808))); + PRAGMA integrity_check; +} {ok} + +# 2021-01-07: The -> and ->> operators. +# +proc ptr1 {a b} { return "$a->$b" } +db func -> ptr1 +proc ptr2 {a b} { return "$a->>$b" } +db func ->> ptr2 +do_execsql_test func-36.100 { + SELECT 123 -> 456 +} {123->456} +do_execsql_test func-36.110 { + SELECT 123 ->> 456 +} {123->>456} + +# 2023-06-26 +# Enhanced precision of SUM(). +# +reset_db +do_catchsql_test func-37.100 { + WITH c(x) AS (VALUES(9223372036854775807),(9223372036854775807), + (123),(-9223372036854775807),(-9223372036854775807)) + SELECT sum(x) FROM c; +} {1 {integer overflow}} +do_catchsql_test func-37.110 { + WITH c(x) AS (VALUES(9223372036854775807),(1)) + SELECT sum(x) FROM c; +} {1 {integer overflow}} +do_catchsql_test func-37.120 { + WITH c(x) AS (VALUES(9223372036854775807),(10000),(-10010)) + SELECT sum(x) FROM c; +} {1 {integer overflow}} + +# 2023-08-28 forum post https://sqlite.org/forum/forumpost/1c06ddcacc86032a +# Incorrect handling of infinity by SUM(). +# +do_execsql_test func-38.100 { + WITH t1(x) AS (VALUES(9e+999)) SELECT sum(x), avg(x), total(x) FROM t1; + WITH t1(x) AS (VALUES(-9e+999)) SELECT sum(x), avg(x), total(x) FROM t1; +} {Inf Inf Inf -Inf -Inf -Inf} + +# 2024-03-21 https://sqlite.org/forum/forumpost/23b8688ef4 +# Another problem with Kahan-Babushka-Neumaier summation and +# infinities. +# +do_execsql_test func-39.101 { + WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<1) + SELECT sum(1.7976931348623157e308), + avg(1.7976931348623157e308), + total(1.7976931348623157e308) + FROM c; +} {1.79769313486232e+308 1.79769313486232e+308 1.79769313486232e+308} +for {set i 2} {$i<10} {incr i} { + do_execsql_test func-39.[expr {10*$i+100}] { + WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<$i) + SELECT sum(1.7976931348623157e308), + avg(1.7976931348623157e308), + total(1.7976931348623157e308) + FROM c; + } {Inf Inf Inf} +} + finish_test diff --git a/test/func2.test b/test/func2.test index 08ad857509..a7c7ec3fd8 100644 --- a/test/func2.test +++ b/test/func2.test @@ -508,4 +508,27 @@ do_test func2-3.9.2 { bin_to_hex [lindex $blob 0] } "12" +#------------------------------------------------------------------------- +# At one point this was extremely slow to compile. +# +do_test func2-3.10 { + set tm [time { + execsql { + SELECT '' IN (zerobloB(zerobloB(zerobloB(zerobloB(zerobloB( + zerobloB(zerobloB(zerobloB(zerobloB(zerobloB(zerobloB(zerobloB( + zerobloB(zerobloB(zerobloB(zerobloB(zerobloB(zerobloB(zerobloB( + zerobloB(zerobloB(zerobloB(zerobloB(zerobloB(zerobloB(zerobloB( + zerobloB(zerobloB(zerobloB(zerobloB(zerobloB(zerobloB(zerobloB( + zerobloB(zerobloB(zerobloB(zerobloB(zerobloB(zerobloB(zerobloB( + zerobloB(zerobloB(zerobloB(zerobloB(zerobloB(zerobloB(zerobloB( + zerobloB(zerobloB(zerobloB(zerobloB(zerobloB(zerobloB(zerobloB( + zerobloB(zerobloB(zerobloB(zerobloB(zerobloB(zerobloB(zerobloB(1) + ))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) + } + }] + + set tm [lindex $tm 0] + expr $tm<2000000 +} {1} + finish_test diff --git a/test/func3.test b/test/func3.test index 3b1613b56c..518bd51c79 100644 --- a/test/func3.test +++ b/test/func3.test @@ -153,6 +153,19 @@ do_test func3-5.39 { db eval {EXPLAIN SELECT unlikely(min(1.0+'2.0',4*11))} } [db eval {EXPLAIN SELECT min(1.0+'2.0',4*11)}] +# Unlikely() does not preserve the affinity of X. +# ticket https://sqlite.org/src/tktview/0c620df60b +# +do_execsql_test func3-5.40 { + SELECT likely(CAST(1 AS INT))=='1'; +} 0 +do_execsql_test func3-5.41 { + SELECT unlikely(CAST(1 AS INT))=='1'; +} 0 +do_execsql_test func3-5.41 { + SELECT likelihood(CAST(1 AS INT),0.5)=='1'; +} 0 + # EVIDENCE-OF: R-23735-03107 The likely(X) function returns the argument # X unchanged. @@ -185,6 +198,14 @@ do_test func3-5.59 { } [db eval {EXPLAIN SELECT min(1.0+'2.0',4*11)}] +# Test the outcome of specifying NULL xStep and xFinal pointers (normally +# used to delete any existing function) and a non-NULL xDestroy when there +# is no existing function to destroy. +# +do_test func3-6.0 { + sqlite3_create_function_v2 db nofunc 1 utf8 +} {} + finish_test diff --git a/test/func4.test b/test/func4.test index e94f8c3418..fb74b7d8d5 100644 --- a/test/func4.test +++ b/test/func4.test @@ -1,4 +1,4 @@ -# 2013 March 10 +# 2023-03-10 # # The author disclaims copyright to this source code. In place of # a legal notice, here is a blessing: @@ -9,7 +9,10 @@ # #*********************************************************************** # This file implements regression tests for SQLite library. The focus of -# this file is testing the tointeger() and toreal() functions. +# this file is testing the tointeger() and toreal() functions that are +# part of the "totype.c" extension. This file does not test the core +# SQLite library. Failures of tests in this file are related to the +# ext/misc/totype.c extension. # # Several of the toreal() tests are disabled on platforms where floating # point precision is not high enough to represent their constant integer @@ -23,6 +26,20 @@ load_static_extension db totype set highPrecision(1) [expr \ {[db eval {SELECT tointeger(9223372036854775807 + 1);}] eq {{}}}] +set highPrecision(2) [expr \ + {[db eval {SELECT toreal(-9223372036854775808 + 1);}] eq {{}}}] + +# highPrecision(3) is only known to be false on i586 with gcc-13 and -O2. +# It is true on the exact same platform with -O0. Both results seem +# reasonable, so we'll just very the expectation accordingly. +# +set highPrecision(3) [expr \ + {[db eval {SELECT toreal(9007199254740992 + 1);}] eq {{}}}] + +if {!$highPrecision(1) || !$highPrecision(2) || !$highPrecision(3)} { + puts "NOTICE:\ + highPrecision: $highPrecision(1) $highPrecision(2) $highPrecision(3)" +} do_execsql_test func4-1.1 { SELECT tointeger(NULL); @@ -92,7 +109,7 @@ do_execsql_test func4-1.22 { } {{}} do_execsql_test func4-1.23 { SELECT tointeger(-9223372036854775808 - 1); -} {-9223372036854775808} +} {{}} do_execsql_test func4-1.24 { SELECT tointeger(-9223372036854775808); } {-9223372036854775808} @@ -195,8 +212,6 @@ do_execsql_test func4-1.55 { } {{}} ifcapable floatingpoint { - set highPrecision(2) [expr \ - {[db eval {SELECT toreal(-9223372036854775808 + 1);}] eq {{}}}] do_execsql_test func4-2.1 { SELECT toreal(NULL); @@ -269,7 +284,7 @@ ifcapable floatingpoint { } {-9.223372036854776e+18} do_execsql_test func4-2.24 { SELECT toreal(-9223372036854775808); - } {-9.223372036854776e+18} + } {{}} if {$highPrecision(2)} { do_execsql_test func4-2.25 { SELECT toreal(-9223372036854775808 + 1); @@ -277,7 +292,7 @@ ifcapable floatingpoint { } do_execsql_test func4-2.26 { SELECT toreal(-9223372036854775807 - 1); - } {-9.223372036854776e+18} + } {{}} if {$highPrecision(2)} { do_execsql_test func4-2.27 { SELECT toreal(-9223372036854775807); @@ -341,10 +356,14 @@ ifcapable floatingpoint { do_execsql_test func4-2.45 { SELECT toreal(9007199254740992); } {9007199254740992.0} - if {$highPrecision(2)} { + if {$highPrecision(3)} { do_execsql_test func4-2.46 { SELECT toreal(9007199254740992 + 1); } {{}} + } else { + do_execsql_test func4-2.46 { + SELECT toreal(9007199254740992 + 1); + } {9007199254740992.0} } do_execsql_test func4-2.47 { SELECT toreal(9007199254740992 + 2); @@ -381,32 +400,32 @@ ifcapable check { catchsql { INSERT INTO t1 (x) VALUES (NULL); } - } {1 {CHECK constraint failed: t1}} + } {1 {CHECK constraint failed: tointeger(x) IS NOT NULL}} do_test func4-3.3 { catchsql { INSERT INTO t1 (x) VALUES (NULL); } - } {1 {CHECK constraint failed: t1}} + } {1 {CHECK constraint failed: tointeger(x) IS NOT NULL}} do_test func4-3.4 { catchsql { INSERT INTO t1 (x) VALUES (''); } - } {1 {CHECK constraint failed: t1}} + } {1 {CHECK constraint failed: tointeger(x) IS NOT NULL}} do_test func4-3.5 { catchsql { INSERT INTO t1 (x) VALUES ('bad'); } - } {1 {CHECK constraint failed: t1}} + } {1 {CHECK constraint failed: tointeger(x) IS NOT NULL}} do_test func4-3.6 { catchsql { INSERT INTO t1 (x) VALUES ('1234bad'); } - } {1 {CHECK constraint failed: t1}} + } {1 {CHECK constraint failed: tointeger(x) IS NOT NULL}} do_test func4-3.7 { catchsql { INSERT INTO t1 (x) VALUES ('1234.56bad'); } - } {1 {CHECK constraint failed: t1}} + } {1 {CHECK constraint failed: tointeger(x) IS NOT NULL}} do_test func4-3.8 { catchsql { INSERT INTO t1 (x) VALUES (1234); @@ -416,7 +435,7 @@ ifcapable check { catchsql { INSERT INTO t1 (x) VALUES (1234.56); } - } {1 {CHECK constraint failed: t1}} + } {1 {CHECK constraint failed: tointeger(x) IS NOT NULL}} do_test func4-3.10 { catchsql { INSERT INTO t1 (x) VALUES ('1234'); @@ -426,32 +445,32 @@ ifcapable check { catchsql { INSERT INTO t1 (x) VALUES ('1234.56'); } - } {1 {CHECK constraint failed: t1}} + } {1 {CHECK constraint failed: tointeger(x) IS NOT NULL}} do_test func4-3.12 { catchsql { INSERT INTO t1 (x) VALUES (ZEROBLOB(4)); } - } {1 {CHECK constraint failed: t1}} + } {1 {CHECK constraint failed: tointeger(x) IS NOT NULL}} do_test func4-3.13 { catchsql { INSERT INTO t1 (x) VALUES (X''); } - } {1 {CHECK constraint failed: t1}} + } {1 {CHECK constraint failed: tointeger(x) IS NOT NULL}} do_test func4-3.14 { catchsql { INSERT INTO t1 (x) VALUES (X'1234'); } - } {1 {CHECK constraint failed: t1}} + } {1 {CHECK constraint failed: tointeger(x) IS NOT NULL}} do_test func4-3.15 { catchsql { INSERT INTO t1 (x) VALUES (X'12345678'); } - } {1 {CHECK constraint failed: t1}} + } {1 {CHECK constraint failed: tointeger(x) IS NOT NULL}} do_test func4-3.16 { catchsql { INSERT INTO t1 (x) VALUES ('1234.00'); } - } {1 {CHECK constraint failed: t1}} + } {0 {}} do_test func4-3.17 { catchsql { INSERT INTO t1 (x) VALUES (1234.00); @@ -461,17 +480,17 @@ ifcapable check { catchsql { INSERT INTO t1 (x) VALUES ('-9223372036854775809'); } - } {1 {CHECK constraint failed: t1}} + } {1 {CHECK constraint failed: tointeger(x) IS NOT NULL}} if {$highPrecision(1)} { do_test func4-3.19 { catchsql { INSERT INTO t1 (x) VALUES (9223372036854775808); } - } {1 {CHECK constraint failed: t1}} + } {1 {CHECK constraint failed: tointeger(x) IS NOT NULL}} } do_execsql_test func4-3.20 { - SELECT x FROM t1 ORDER BY x; - } {1234 1234 1234} + SELECT x FROM t1 WHERE x>0 ORDER BY x; + } {1234 1234 1234 1234} ifcapable floatingpoint { do_execsql_test func4-4.1 { @@ -483,32 +502,32 @@ ifcapable check { catchsql { INSERT INTO t2 (x) VALUES (NULL); } - } {1 {CHECK constraint failed: t2}} + } {1 {CHECK constraint failed: toreal(x) IS NOT NULL}} do_test func4-4.3 { catchsql { INSERT INTO t2 (x) VALUES (NULL); } - } {1 {CHECK constraint failed: t2}} + } {1 {CHECK constraint failed: toreal(x) IS NOT NULL}} do_test func4-4.4 { catchsql { INSERT INTO t2 (x) VALUES (''); } - } {1 {CHECK constraint failed: t2}} + } {1 {CHECK constraint failed: toreal(x) IS NOT NULL}} do_test func4-4.5 { catchsql { INSERT INTO t2 (x) VALUES ('bad'); } - } {1 {CHECK constraint failed: t2}} + } {1 {CHECK constraint failed: toreal(x) IS NOT NULL}} do_test func4-4.6 { catchsql { INSERT INTO t2 (x) VALUES ('1234bad'); } - } {1 {CHECK constraint failed: t2}} + } {1 {CHECK constraint failed: toreal(x) IS NOT NULL}} do_test func4-4.7 { catchsql { INSERT INTO t2 (x) VALUES ('1234.56bad'); } - } {1 {CHECK constraint failed: t2}} + } {1 {CHECK constraint failed: toreal(x) IS NOT NULL}} do_test func4-4.8 { catchsql { INSERT INTO t2 (x) VALUES (1234); @@ -533,22 +552,22 @@ ifcapable check { catchsql { INSERT INTO t2 (x) VALUES (ZEROBLOB(4)); } - } {1 {CHECK constraint failed: t2}} + } {1 {CHECK constraint failed: toreal(x) IS NOT NULL}} do_test func4-4.13 { catchsql { INSERT INTO t2 (x) VALUES (X''); } - } {1 {CHECK constraint failed: t2}} + } {1 {CHECK constraint failed: toreal(x) IS NOT NULL}} do_test func4-4.14 { catchsql { INSERT INTO t2 (x) VALUES (X'1234'); } - } {1 {CHECK constraint failed: t2}} + } {1 {CHECK constraint failed: toreal(x) IS NOT NULL}} do_test func4-4.15 { catchsql { INSERT INTO t2 (x) VALUES (X'12345678'); } - } {1 {CHECK constraint failed: t2}} + } {1 {CHECK constraint failed: toreal(x) IS NOT NULL}} do_execsql_test func4-4.16 { SELECT x FROM t2 ORDER BY x; } {1234.0 1234.0 1234.56 1234.56} @@ -573,10 +592,10 @@ ifcapable floatingpoint { } {1} do_execsql_test func4-5.6 { SELECT tointeger(toreal(-9223372036854775808 - 1)); - } {-9223372036854775808} + } {{}} do_execsql_test func4-5.7 { SELECT tointeger(toreal(-9223372036854775808)); - } {-9223372036854775808} + } {{}} if {$highPrecision(2)} { do_execsql_test func4-5.8 { SELECT tointeger(toreal(-9223372036854775808 + 1)); @@ -626,10 +645,14 @@ ifcapable floatingpoint { do_execsql_test func4-5.22 { SELECT tointeger(toreal(9007199254740992)); } {9007199254740992} - if {$highPrecision(2)} { + if {$highPrecision(3)} { do_execsql_test func4-5.23 { SELECT tointeger(toreal(9007199254740992 + 1)); } {{}} + } else { + do_execsql_test func4-5.23 { + SELECT tointeger(toreal(9007199254740992 + 1)); + } {9007199254740992} } do_execsql_test func4-5.24 { SELECT tointeger(toreal(9007199254740992 + 2)); diff --git a/test/func5.test b/test/func5.test index bfd545b4e3..8c3dd05c60 100644 --- a/test/func5.test +++ b/test/func5.test @@ -53,9 +53,10 @@ do_execsql_test func5-2.2 { WHERE x+counter1('hello')=counter1('hello')+x ORDER BY +x; } {} +set cvalue [db one {SELECT counter2('hello')+1}] do_execsql_test func5-2.3 { SELECT x, y FROM t2 - WHERE x+counter2('hello')=counter2('hello')+x + WHERE x+counter2('hello')=$cvalue+x ORDER BY +x; } {1 2 3 4 5 6 7 8} diff --git a/test/func6.test b/test/func6.test new file mode 100644 index 0000000000..acca490f33 --- /dev/null +++ b/test/func6.test @@ -0,0 +1,183 @@ +# 2017-12-16 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# +# Test cases for the sqlite_offset() function. +# +# Some of the tests in this file depend on the exact placement of content +# within b-tree pages. Such placement is at the implementations discretion, +# and so it is possible for results to change from one release to the next. +# +set testdir [file dirname $argv0] +source $testdir/tester.tcl +ifcapable !offset_sql_func { + finish_test + return +} + +set bNullTrim 0 +ifcapable null_trim { + set bNullTrim 1 +} + +do_execsql_test func6-100 { + PRAGMA page_size=4096; + PRAGMA auto_vacuum=NONE; + CREATE TABLE t1(a,b,c,d); + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<100) + INSERT INTO t1(a,b,c,d) SELECT printf('abc%03x',x), x, 1000-x, NULL FROM c; + CREATE INDEX t1a ON t1(a); + CREATE INDEX t1bc ON t1(b,c); + CREATE TABLE t2(x TEXT PRIMARY KEY, y) WITHOUT ROWID; + INSERT INTO t2(x,y) SELECT a, b FROM t1; +} + +# Load the contents of $file from disk and return it encoded as a hex +# string. +proc loadhex {file} { + set fd [open $file] + fconfigure $fd -translation binary + set data [read $fd] + close $fd + binary encode hex $data +} + +# Each argument is either an integer between 0 and 65535, a text value, or +# an empty string representing an SQL NULL. This command builds an SQLite +# record containing the values passed as arguments and returns it encoded +# as a hex string. +proc hexrecord {args} { + set hdr "" + set body "" + + if {$::bNullTrim} { + while {[llength $args] && [lindex $args end]=={}} { + set args [lrange $args 0 end-1] + } + } + + foreach x $args { + if {$x==""} { + append hdr 00 + } elseif {[string is integer $x]==0} { + set n [string length $x] + append hdr [format %02x [expr $n*2 + 13]] + append body [binary encode hex $x] + } elseif {$x == 0} { + append hdr 08 + } elseif {$x == 1} { + append hdr 09 + } elseif {$x <= 127} { + append hdr 01 + append body [format %02x $x] + } else { + append hdr 02 + append body [format %04x $x] + } + } + set res [format %02x [expr 1 + [string length $hdr]/2]] + append res $hdr + append res $body +} + +# Argument $off is an offset into the database image encoded as a hex string +# in argument $hexdb. This command returns 0 if the offset contains the hex +# $hexrec, or throws an exception otherwise. +# +proc offset_contains_record {off hexdb hexrec} { + set n [string length $hexrec] + set off [expr $off*2] + if { [string compare $hexrec [string range $hexdb $off [expr $off+$n-1]]] } { + error "record not found!" + } + return 0 +} + +# This command is the implementation of SQL function "offrec()". The first +# argument to this is an offset value. The remaining values are used to +# formulate an SQLite record. If database file test.db does not contain +# an equivalent record at the specified offset, an exception is thrown. +# Otherwise, 0 is returned. +# +proc offrec {args} { + set offset [lindex $args 0] + set rec [hexrecord {*}[lrange $args 1 end]] + offset_contains_record $offset $::F $rec +} +set F [loadhex test.db] +db func offrec offrec + +# Test the sanity of the tests. +if {$bNullTrim} { + set offset 8180 +} else { + set offset 8179 +} +do_execsql_test func6-105 { + SELECT sqlite_offset(d) FROM t1 ORDER BY rowid LIMIT 1; +} $offset +do_test func6-106 { + set r [hexrecord abc001 1 999 {}] + offset_contains_record $offset $F $r +} 0 + +set z100 [string trim [string repeat "0 " 100]] + +# Test offsets within table b-tree t1. +do_execsql_test func6-110 { + SELECT offrec(sqlite_offset(d), a, b, c, d) FROM t1 ORDER BY rowid +} $z100 + +do_execsql_test func6-120 { + SELECT a, typeof(sqlite_offset(+a)) FROM t1 + ORDER BY rowid LIMIT 2; +} {abc001 null abc002 null} + +# Test offsets within index b-tree t1a. +do_execsql_test func6-130 { + SELECT offrec(sqlite_offset(a), a, rowid) FROM t1 ORDER BY a +} $z100 + +# Test offsets within table b-tree t1 with a temp b-tree ORDER BY. +do_execsql_test func6-140 { + SELECT offrec(sqlite_offset(d), a, b, c, d) FROM t1 ORDER BY a +} $z100 + +# Test offsets from both index t1a and table t1 in the same query. +do_execsql_test func6-150 { + SELECT offrec(sqlite_offset(a), a, rowid), + offrec(sqlite_offset(d), a, b, c, d) + FROM t1 ORDER BY a +} [concat $z100 $z100] + +# Test offsets from both index t1bc and table t1 in the same query. +do_execsql_test func6-160 { + SELECT offrec(sqlite_offset(b), b, c, rowid), + offrec(sqlite_offset(c), b, c, rowid), + offrec(sqlite_offset(d), a, b, c, d) + FROM t1 + ORDER BY b +} [concat $z100 $z100 $z100] + +# Test offsets in WITHOUT ROWID table t2. +do_execsql_test func6-200 { + SELECT offrec( sqlite_offset(y), x, y ) FROM t2 ORDER BY x +} $z100 + +# 2022-03-14 dbsqlfuzz 474499f3977d95fdf2dbcd99c50be1d0082e4c92 +reset_db +do_execsql_test func6-300 { + CREATE TABLE t2(a INT, b INT PRIMARY KEY) WITHOUT ROWID; + CREATE INDEX x3 ON t2(b); + CREATE TABLE t1(a INT PRIMARY KEY, b TEXT); + SELECT * FROM t1 WHERE a IN (SELECT sqlite_offset(b) FROM t2); +} {} + +finish_test diff --git a/test/func7.test b/test/func7.test new file mode 100644 index 0000000000..6026b557f5 --- /dev/null +++ b/test/func7.test @@ -0,0 +1,251 @@ +# 2020-12-07 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# +# Test cases for SQL functions based off the standard math library +# +set testdir [file dirname $argv0] +source $testdir/tester.tcl +ifcapable !mathlib { + finish_test + return +} + +do_execsql_test func7-100 { + SELECT ceil(99.9), ceiling(-99.01), floor(17), floor(-17.99); +} {100.0 -99.0 17 -18.0} +do_execsql_test func7-110 { + SELECT quote(ceil(NULL)), ceil('-99.99'); +} {NULL -99.0} +do_execsql_test func7-200 { + SELECT round(ln(5),2), log(100.0), log(100), log(2,'256'); +} {1.61 2.0 2.0 8.0} +do_execsql_test func7-210 { + SELECT ln(-5), log(-5,100.0); +} {{} {}} + +# Test cases derived from PostgreSQL documentation +# +do_execsql_test func7-pg-100 { + SELECT abs(-17.4) +} {17.4} +do_execsql_test func7-pg-110 { + SELECT ceil(42.2) +} {43.0} +do_execsql_test func7-pg-120 { + SELECT ceil(-42.2) +} {-42.0} +do_execsql_test func7-pg-130 { + SELECT round(exp(1.0),7) +} {2.7182818} +do_execsql_test func7-pg-140 { + SELECT floor(42.8) +} {42.0} +do_execsql_test func7-pg-150 { + SELECT floor(-42.8) +} {-43.0} +do_execsql_test func7-pg-160 { + SELECT round(ln(2.0),7) +} {0.6931472} +do_execsql_test func7-pg-170 { + SELECT log(100.0) +} {2.0} +do_execsql_test func7-pg-180 { + SELECT log10(1000.0) +} {3.0} +do_execsql_test func7-pg-181 { + SELECT format('%.30f', log10(100.0) ); +} {2.000000000000000000000000000000} +do_execsql_test func7-pg-182 { + SELECT format('%.30f', ln(exp(2.0)) ); +} {2.000000000000000000000000000000} +do_execsql_test func7-pg-190 { + SELECT log(2.0, 64.0) +} {6.0} +do_execsql_test func7-pg-200 { + SELECT mod(9,4); +} {1.0} +do_execsql_test func7-pg-210 { + SELECT round(pi(),7); +} {3.1415927} +do_execsql_test func7-pg-220 { + SELECT power(9,3); +} {729.0} +do_execsql_test func7-pg-230 { + SELECT round(radians(45.0),7); +} {0.7853982} +do_execsql_test func7-pg-240 { + SELECT round(42.4); +} {42.0} +do_execsql_test func7-pg-250 { + SELECT round(42.4382,2); +} {42.44} +do_execsql_test func7-pg-260 { + SELECT sign(-8.4); +} {-1} +do_execsql_test func7-pg-270 { + SELECT round( sqrt(2), 7); +} {1.4142136} +do_execsql_test func7-pg-280 { + SELECT trunc(42.8), trunc(-42.8); +} {42.0 -42.0} +do_execsql_test func7-pg-300 { + SELECT acos(1); +} {0.0} +do_execsql_test func7-pg-301 { + SELECT format('%f',degrees(acos(0.5))); +} {60.0} +do_execsql_test func7-pg-310 { + SELECT round( asin(1), 7); +} {1.5707963} +do_execsql_test func7-pg-311 { + SELECT format('%f',degrees( asin(0.5) )); +} {30.0} +do_execsql_test func7-pg-320 { + SELECT round( atan(1), 7); +} {0.7853982} +do_execsql_test func7-pg-321 { + SELECT degrees( atan(1) ); +} {45.0} +do_execsql_test func7-pg-330 { + SELECT round( atan2(1,0), 7); +} {1.5707963} +do_execsql_test func7-pg-331 { + SELECT degrees( atan2(1,0) ); +} {90.0} +do_execsql_test func7-pg-400 { + SELECT cos(0); +} {1.0} +do_execsql_test func7-pg-401 { + SELECT cos( radians(60.0) ); +} {0.5} +do_execsql_test func7-pg-400 { + SELECT cos(0); +} {1.0} +do_execsql_test func7-pg-410 { + SELECT round( sin(1), 7); +} {0.841471} +do_execsql_test func7-pg-411 { + SELECT sin( radians(30) ); +} {0.5} +do_execsql_test func7-pg-420 { + SELECT round( tan(1), 7); +} {1.5574077} +do_execsql_test func7-pg-421 { + SELECT round(tan( radians(45) ),10); +} {1.0} +do_execsql_test func7-pg-500 { + SELECT round( sinh(1), 7); +} {1.1752012} +do_execsql_test func7-pg-510 { + SELECT round( cosh(0), 7); +} {1.0} +do_execsql_test func7-pg-520 { + SELECT round( tanh(1), 7); +} {0.7615942} +do_execsql_test func7-pg-530 { + SELECT round( asinh(1), 7); +} {0.8813736} +do_execsql_test func7-pg-540 { + SELECT round( acosh(1), 7); +} {0.0} +do_execsql_test func7-pg-550 { + SELECT round( atanh(0.5), 7); +} {0.5493061} + +# Test cases derived from MySQL documentation +# +do_execsql_test func7-mysql-100 { + SELECT acos(1); +} {0.0} +do_execsql_test func7-mysql-110 { + SELECT acos(1.0001); +} {{}} +do_execsql_test func7-mysql-120 { + SELECT round( acos(0.0), 7); +} {1.5707963} +do_execsql_test func7-mysql-130 { + SELECT round( asin(0.2), 7); +} {0.2013579} +do_execsql_test func7-mysql-140 { + SELECT asin('foo'); +} {{}} ;# Note: MySQL returns 0 here, not NULL. + # SQLite deliberately returns NULL. + # SQLServer and Oracle throw an error. +do_execsql_test func7-mysql-150 { + SELECT round( atan(2), 7), round( atan(-2), 7); +} {1.1071487 -1.1071487} +do_execsql_test func7-mysql-160 { + SELECT round( atan2(-2,2), 7), round( atan2(pi(),0), 7); +} {-0.7853982 1.5707963} +do_execsql_test func7-mysql-170 { + SELECT ceiling(1.23), ceiling(-1.23); +} {2.0 -1.0} +do_execsql_test func7-mysql-180 { + SELECT cos(pi()); +} {-1.0} +do_execsql_test func7-mysql-190 { + SELECT degrees(pi()), degrees(pi()/2); +} {180.0 90.0} +do_execsql_test func7-mysql-190 { + SELECT round( exp(2), 7), round( exp(-2), 7), exp(0); +} {7.3890561 0.1353353 1.0} +do_execsql_test func7-mysql-200 { + SELECT floor(1.23), floor(-1.23); +} {1.0 -2.0} +do_execsql_test func7-mysql-210 { + SELECT round(ln(2),7), quote(ln(-2)); +} {0.6931472 NULL} +#do_execsql_test func7-mysql-220 { +# SELECT round(log(2),7), log(-2); +#} {0.6931472 NULL} +# log() means natural logarithm in MySQL +do_execsql_test func7-mysql-230 { + SELECT log(2,65536), log(10,100), quote(log(1,100)), quote(log(0,100)); +} {16.0 2.0 NULL NULL} +do_execsql_test func7-mysql-240 { + SELECT log2(65536), quote(log2(-100)), quote(log2(0)); +} {16.0 NULL NULL} +do_execsql_test func7-mysql-250 { + SELECT round(log10(2),7), log10(100), quote(log10(-100)); +} {0.30103 2.0 NULL} +do_execsql_test func7-mysql-260 { + SELECT mod(234,10), 253%7, mod(29,9), 29%9; +} {4.0 1 2.0 2} +do_execsql_test func7-mysql-270 { + SELECT mod(34.5,3); +} {1.5} +do_execsql_test func7-mysql-280 { + SELECT pow(2,2), pow(2,-2); +} {4.0 0.25} +do_execsql_test func7-mysql-281 { + SELECT power(2,2), power(2,-2); +} {4.0 0.25} +do_execsql_test func7-mysql-290 { + SELECT round(radians(90),7); +} {1.5707963} +do_execsql_test func7-mysql-300 { + SELECT sign(-32), sign(0), sign(234); +} {-1 0 1} +do_execsql_test func7-mysql-310 { + SELECT sin(pi()) BETWEEN -1.0e-15 AND 1.0e-15; +} {1} +do_execsql_test func7-mysql-320 { + SELECT sqrt(4), round(sqrt(20),7), quote(sqrt(-16)); +} {2.0 4.472136 NULL} +do_execsql_test func7-mysql-330 { + SELECT tan(pi()) BETWEEN -1.0e-15 AND 1.0e-15; +} {1} +do_execsql_test func7-mysql-331 { + SELECT round(tan(pi()+1),7); +} {1.5574077} + + +finish_test diff --git a/test/func8.test b/test/func8.test new file mode 100644 index 0000000000..348dfb7f66 --- /dev/null +++ b/test/func8.test @@ -0,0 +1,64 @@ +# 2023-03-17 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# +# Test cases for SQL functions with names that are the same as join +# keywords: CROSS FULL INNER LEFT NATURAL OUTER RIGHT +# +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +proc joinx {args} {return [join $args -]} +db func cross {joinx cross} +db func full {joinx full} +db func inner {joinx inner} +db func left {joinx left} +db func natural {joinx natural} +db func outer {joinx outer} +db func right {joinx right} +do_execsql_test func8-100 { + CREATE TABLE cross(cross,full,inner,left,natural,outer,right); + CREATE TABLE full(cross,full,inner,left,natural,outer,right); + CREATE TABLE inner(cross,full,inner,left,natural,outer,right); + CREATE TABLE left(cross,full,inner,left,natural,outer,right); + CREATE TABLE natural(cross,full,inner,left,natural,outer,right); + CREATE TABLE outer(cross,full,inner,left,natural,outer,right); + CREATE TABLE right(cross,full,inner,left,natural,outer,right); + INSERT INTO cross VALUES(1,2,3,4,5,6,7); + INSERT INTO full VALUES(1,2,3,4,5,6,7); + INSERT INTO inner VALUES(1,2,3,4,5,6,7); + INSERT INTO left VALUES(1,2,3,4,5,6,7); + INSERT INTO natural VALUES(1,2,3,4,5,6,7); + INSERT INTO outer VALUES(1,2,3,4,5,6,7); + INSERT INTO right VALUES(1,2,3,4,5,6,7); +} +do_execsql_test func8-110 { + SELECT cross(cross,full,inner,left,natural,outer,right) FROM cross; +} cross-1-2-3-4-5-6-7 +do_execsql_test func8-120 { + SELECT full(cross,full,inner,left,natural,outer,right) FROM full; +} full-1-2-3-4-5-6-7 +do_execsql_test func8-130 { + SELECT inner(cross,full,inner,left,natural,outer,right) FROM inner; +} inner-1-2-3-4-5-6-7 +do_execsql_test func8-140 { + SELECT left(cross,full,inner,left,natural,outer,right) FROM left; +} left-1-2-3-4-5-6-7 +do_execsql_test func8-150 { + SELECT natural(cross,full,inner,left,natural,outer,right) FROM natural; +} natural-1-2-3-4-5-6-7 +do_execsql_test func8-160 { + SELECT outer(cross,full,inner,left,natural,outer,right) FROM outer; +} outer-1-2-3-4-5-6-7 +do_execsql_test func8-170 { + SELECT right(cross,full,inner,left,natural,outer,right) FROM right; +} right-1-2-3-4-5-6-7 + +finish_test diff --git a/test/func9.test b/test/func9.test new file mode 100644 index 0000000000..d24d5f7beb --- /dev/null +++ b/test/func9.test @@ -0,0 +1,66 @@ +# 2023-08-29 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# +# Test cases for some newer SQL functions +# +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +do_execsql_test func9-100 { + SELECT concat('abc',123,null,'xyz'); +} {abc123xyz} +do_execsql_test func9-110 { + SELECT typeof(concat(null)); +} {text} +do_catchsql_test func9-120 { + SELECT concat(); +} {1 {wrong number of arguments to function concat()}} +do_execsql_test func9-130 { + SELECT concat_ws(',',1,2,3,4,5,6,7,8,NULL,9,10,11,12); +} {1,2,3,4,5,6,7,8,9,10,11,12} +do_execsql_test func9-131 { + SELECT concat_ws(',',1,2,3,4,'',6,7,8,NULL,9,10,11,12); +} {1,2,3,4,,6,7,8,9,10,11,12} +do_execsql_test func9-132 { + SELECT concat_ws(',','',2,3,4,'',6,7,8,NULL,9,10,11,12); +} {,2,3,4,,6,7,8,9,10,11,12} +do_execsql_test func9-133 { + SELECT concat_ws(',',NULL,'',3,4,'',6,7,8,NULL,9,10,11,12); +} {,3,4,,6,7,8,9,10,11,12} +do_execsql_test func9-134 { + SELECT concat_ws(',',NULL,NULL,NULL,'',3,4,'',6,7,8,NULL,9,10,11,12); +} {,3,4,,6,7,8,9,10,11,12} +do_execsql_test func9-140 { + SELECT concat_ws(NULL,1,2,3,4,5,6,7,8,NULL,9,10,11,12); +} {{}} +do_catchsql_test func9-150 { + SELECT concat_ws(); +} {1 {wrong number of arguments to function concat_ws()}} +do_catchsql_test func9-160 { + SELECT concat_ws(','); +} {1 {wrong number of arguments to function concat_ws()}} + +# https://sqlite.org/forum/forumpost/4c344ca61f (2025-03-02) +do_execsql_test func9-200 { + SELECT unistr('G\u00e4ste'); +} {Gäste} +do_execsql_test func9-210 { + SELECT unistr_quote(unistr('G\u00e4ste')); +} {'Gäste'} +do_execsql_test func9-220 { + SELECT format('%#Q',unistr('G\u00e4ste')); +} {'Gäste'} + +do_execsql_test func9-300 { + SELECT hex( unistr('\UFFFFFFFF') ) +} {F7BFBFBF} + +finish_test diff --git a/test/fuzz-oss1.test b/test/fuzz-oss1.test index 08bc670845..46feeb62eb 100644 --- a/test/fuzz-oss1.test +++ b/test/fuzz-oss1.test @@ -329,6 +329,8 @@ ifcapable !fts3 { db close forcedelete test.db sqlite3 db test.db +sqlite3_db_config db SQLITE_DBCONFIG_DQS_DDL 1 +sqlite3_db_config db SQLITE_DBCONFIG_DQS_DML 1 do_test fuzz-oss1-gnomeshell { db eval { CREATE TABLE Resource (ID INTEGER NOT NULL PRIMARY KEY, Uri TEXT NOT @@ -1997,5 +1999,9 @@ NULL AND ("9_u" COLLATE NOCASE = ? COLLATE NOCASE))) FROM (SELECT } } {/.* Goto .*/} +# Crash reported by OSS-FUZZ on 2016-11-10 +do_catchsql_test fuzz-oss1-detach { + DETACH x IS #1; +} {1 {near "#1": syntax error}} finish_test diff --git a/test/fuzz.test b/test/fuzz.test index 0deed3b636..7002054be9 100644 --- a/test/fuzz.test +++ b/test/fuzz.test @@ -130,7 +130,7 @@ do_test fuzz-1.12.1 { # The following query was crashing. The later subquery (in the FROM) # clause was flattened into the parent, but the code was not repairng # the "b" reference in the other sub-query. When the query was executed, - # that "b" refered to a non-existant vdbe table-cursor. + # that "b" referred to a non-existant vdbe table-cursor. # execsql { SELECT 1 IN ( SELECT b UNION SELECT 1 ) FROM (SELECT b FROM abc); @@ -372,7 +372,7 @@ integrity_check fuzz-7.5.integrity #---------------------------------------------------------------- # Many CREATE and DROP TABLE statements: # -set E [list table duplicate {no such col} {ambiguous column name} {use DROP}] +set E [list table view duplicate {no such col} {ambiguous column name} {use DROP}] do_fuzzy_test fuzz-8.1 -template {[CreateOrDropTableOrView]} -errorlist $E close $::log diff --git a/test/fuzz3.test b/test/fuzz3.test index 6d2a01c7d3..8ac1f4d58f 100644 --- a/test/fuzz3.test +++ b/test/fuzz3.test @@ -83,7 +83,7 @@ proc modify_database {iMod} { set offset [expr {$iMod>>8}] set fd [open test.db r+] - fconfigure $fd -encoding binary -translation binary + fconfigure $fd -translation binary seek $fd $offset set old_blob [read $fd 1] seek $fd $offset @@ -152,7 +152,7 @@ for {set ii 0} {$ii < 5000} {incr ii} { if {$rc == 0 || $msg eq "database or disk is full" || $msg eq "database disk image is malformed" - || $msg eq "file is encrypted or is not a database" + || $msg eq "file is not a database" || [string match "malformed database schema*" $msg] } { set msg ok diff --git a/test/fuzz4.test b/test/fuzz4.test new file mode 100644 index 0000000000..821cd16555 --- /dev/null +++ b/test/fuzz4.test @@ -0,0 +1,82 @@ +# 2018-12-12 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# Test cases found by Matthew Denton's fuzzer at Chrome. +# + + +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +do_execsql_test fuzz4-100 { + CREATE TABLE Table0 (Col0 NOT NULL DEFAULT (CURRENT_TIME IS 1 > 1)); + INSERT OR REPLACE INTO Table0 DEFAULT VALUES ; + SELECT * FROM Table0; +} {0} + +do_execsql_test fuzz4-110 { + CREATE TABLE Table1( + Col0 TEXT DEFAULT (CASE WHEN 1 IS 3530822107858468864 + THEN 1 ELSE quote(1) IS 3530822107858468864 END) + ); + INSERT INTO Table1 DEFAULT VALUES; + SELECT * FROM Table1; +} {0} + +do_execsql_test fuzz4-200 { + CREATE TABLE Table2a( + Col0 NOT NULL DEFAULT (CURRENT_TIME IS 1 IS NOT 1 > 1) + ); + INSERT OR REPLACE INTO Table2a DEFAULT VALUES; + SELECT * FROM Table2a; +} {0} + +do_execsql_test fuzz4-210 { + CREATE TABLE Table2b (Col0 NOT NULL DEFAULT (CURRENT_TIME IS NOT FALSE)) ; + INSERT OR REPLACE INTO Table2b DEFAULT VALUES ; + SELECT * FROM Table2b; +} {1} + +do_execsql_test fuzz4-300 { + CREATE TABLE Table3 (Col0 DEFAULT (CURRENT_TIMESTAMP BETWEEN 1 AND 1)); + INSERT INTO Table3 DEFAULT VALUES; + SELECT * FROM Table3; +} {0} + +do_execsql_test fuzz4-400 { + CREATE TABLE Table4 (Col0 DEFAULT (1 BETWEEN CURRENT_TIMESTAMP AND 1)); + INSERT INTO Table4 DEFAULT VALUES; + SELECT * FROM Table4; +} {0} + +do_execsql_test fuzz4-500 { + CREATE TABLE Table5 (Col0 DEFAULT (1 BETWEEN 1 AND CURRENT_TIMESTAMP)); + INSERT INTO Table5 DEFAULT VALUES; + SELECT * FROM Table5; +} {1} + +do_execsql_test fuzz4-600 { + CREATE TEMPORARY TABLE Table6( + Col0 DEFAULT (CASE x'5d' WHEN 1 THEN + CASE CURRENT_TIMESTAMP WHEN 1 THEN 1 ELSE 1 END + ELSE CASE WHEN 1 THEN FALSE END END ) + ); + INSERT INTO temp.Table6 DEFAULT VALUES ; + SELECT * FROM Table6; +} {0} +do_execsql_test fuzz4-610 { + WITH TableX AS (SELECT DISTINCT * ORDER BY 1 , 1 COLLATE RTRIM) + DELETE FROM Table6 WHERE Col0 || +8388608 ; + SELECT * FROM Table6; +} {} + + +finish_test diff --git a/test/fuzz_common.tcl b/test/fuzz_common.tcl index 4ab7dff5c9..520555bd29 100644 --- a/test/fuzz_common.tcl +++ b/test/fuzz_common.tcl @@ -363,6 +363,7 @@ proc do_fuzzy_test {testname args} { lappend ::fuzzyopts(-errorlist) {ORDER BY} lappend ::fuzzyopts(-errorlist) {GROUP BY} lappend ::fuzzyopts(-errorlist) {datatype mismatch} + lappend ::fuzzyopts(-errorlist) {non-deterministic functions prohibited} for {set ii 0} {$ii < $::fuzzyopts(-repeats)} {incr ii} { do_test ${testname}.$ii { diff --git a/test/fuzz_malloc.test b/test/fuzz_malloc.test index 1e31babd92..4449ea8fc4 100644 --- a/test/fuzz_malloc.test +++ b/test/fuzz_malloc.test @@ -17,11 +17,6 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl -ifcapable !memdebug { - finish_test - return -} - source $testdir/malloc_common.tcl source $testdir/fuzz_common.tcl @@ -57,9 +52,20 @@ proc do_fuzzy_malloc_test {testname args} { incr jj set ::sql [subst $::fuzzyopts(-template)] # puts fuzyy-sql=\[$::sql\]; flush stdout - foreach {rc res} [catchsql "$::sql"] {} + foreach {rc ::fmtres} [catchsql "$::sql"] {} if {$rc==0} { - do_malloc_test $testname-$ii -sqlbody $::sql -sqlprep $::prep + set nErr1 [set_test_counter errors] + do_faultsim_test $testname-$ii -faults oom* -body { + execsql $::sql + } -test { + if {$testrc && $testresult!="datatype mismatch"} { + faultsim_test_result {0 {}} + } + } + if {[set_test_counter errors]>$nErr1} { + puts "Previous fuzzy-sql=\[$::sql\]" + flush stdout + } } else { incr ii -1 } diff --git a/test/fuzzcheck.c b/test/fuzzcheck.c index 03776f1e70..a3377770a8 100644 --- a/test/fuzzcheck.c +++ b/test/fuzzcheck.c @@ -11,8 +11,7 @@ ************************************************************************* ** ** This is a utility program designed to aid running regressions tests on -** the SQLite library using data from an external fuzzer, such as American -** Fuzzy Lop (AFL) (http://lcamtuf.coredump.cx/afl/). +** the SQLite library using data from external fuzzers. ** ** This program reads content from an SQLite database file with the following ** schema: @@ -63,15 +62,37 @@ ** If fuzzcheck does crash, it can be run in the debugger and the content ** of the global variable g.zTextName[] will identify the specific XSQL and ** DB values that were running when the crash occurred. +** +** DBSQLFUZZ: (Added 2020-02-25) +** +** The dbsqlfuzz fuzzer includes both a database file and SQL to run against +** that database in its input. This utility can now process dbsqlfuzz +** input files. Load such files using the "--load-dbsql FILE ..." command-line +** option. +** +** Dbsqlfuzz inputs are ordinary text. The first part of the file is text +** that describes the content of the database (using a lot of hexadecimal), +** then there is a divider line followed by the SQL to run against the +** database. Because they are ordinary text, dbsqlfuzz inputs are stored +** in the XSQL table, as if they were ordinary SQL inputs. The isDbSql() +** function can look at a text string and determine whether or not it is +** a valid dbsqlfuzz input. */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdarg.h> #include <ctype.h> +#include <assert.h> #include "sqlite3.h" +#include "sqlite3recover.h" #define ISSPACE(X) isspace((unsigned char)(X)) #define ISDIGIT(X) isdigit((unsigned char)(X)) +#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) +# define FLEXARRAY +#else +# define FLEXARRAY 1 +#endif #ifdef __unix__ @@ -79,15 +100,24 @@ # include <unistd.h> #endif +#include <stddef.h> +#if !defined(_MSC_VER) +# include <stdint.h> +#endif + +#if defined(_MSC_VER) +typedef unsigned char uint8_t; +#endif + /* ** Files in the virtual file system. */ typedef struct VFile VFile; struct VFile { - char *zFilename; /* Filename. NULL for delete-on-close. From malloc() */ - int sz; /* Size of the file in bytes */ - int nRef; /* Number of references to this file */ - unsigned char *a; /* Content of the file. From malloc() */ + char *zFilename; /* Filename. NULL for delete-on-close. From malloc() */ + int sz; /* Size of the file in bytes */ + int nRef; /* Number of references to this file */ + unsigned char *a; /* Content of the file. From malloc() */ }; typedef struct VHandle VHandle; struct VHandle { @@ -104,9 +134,12 @@ struct Blob { int id; /* Id of this Blob */ int seq; /* Sequence number */ int sz; /* Size of this Blob in bytes */ - unsigned char a[1]; /* Blob content. Extra space allocated as needed. */ + unsigned char a[FLEXARRAY]; /* Blob content. Allocated as needed. */ }; +/* Size in bytes of a Blob object sufficient to store N byte of content */ +#define SZ_BLOB(N) (offsetof(Blob,a) + (((N)+7)&~7)) + /* ** Maximum number of files in the in-memory virtual filesystem. */ @@ -122,24 +155,32 @@ struct Blob { */ static struct GlobalVars { const char *zArgv0; /* Name of program */ + const char *zDbFile; /* Name of database file */ VFile aFile[MX_FILE]; /* The virtual filesystem */ int nDb; /* Number of template databases */ Blob *pFirstDb; /* Content of first template database */ int nSql; /* Number of SQL scripts */ Blob *pFirstSql; /* First SQL script */ + unsigned int uRandom; /* Seed for the SQLite PRNG */ + unsigned int nInvariant; /* Number of invariant checks run */ char zTestName[100]; /* Name of current test */ } g; +/* +** Include various extensions. +*/ +extern int sqlite3_vt02_init(sqlite3*,char**,const sqlite3_api_routines*); +extern int sqlite3_randomjson_init(sqlite3*,char**,const sqlite3_api_routines*); + /* ** Print an error message and quit. */ static void fatalError(const char *zFormat, ...){ va_list ap; - if( g.zTestName[0] ){ - fprintf(stderr, "%s (%s): ", g.zArgv0, g.zTestName); - }else{ - fprintf(stderr, "%s: ", g.zArgv0); - } + fprintf(stderr, "%s", g.zArgv0); + if( g.zDbFile ) fprintf(stderr, " %s", g.zDbFile); + if( g.zTestName[0] ) fprintf(stderr, " (%s)", g.zTestName); + fprintf(stderr, ": "); va_start(ap, zFormat); vfprintf(stderr, zFormat, ap); va_end(ap); @@ -148,12 +189,21 @@ static void fatalError(const char *zFormat, ...){ } /* -** Timeout handler +** signal handler */ #ifdef __unix__ -static void timeoutHandler(int NotUsed){ - (void)NotUsed; - fatalError("timeout\n"); +static void signalHandler(int signum){ + const char *zSig; + if( signum==SIGABRT ){ + zSig = "abort"; + }else if( signum==SIGALRM ){ + zSig = "timeout"; + }else if( signum==SIGSEGV ){ + zSig = "segfault"; + }else{ + zSig = "signal"; + } + fatalError(zSig); } #endif @@ -185,10 +235,10 @@ static int progressHandler(void *pVdbeLimitFlag){ #endif /* -** Reallocate memory. Show and error and quit if unable. +** Reallocate memory. Show an error and quit if unable. */ static void *safe_realloc(void *pOld, int szNew){ - void *pNew = realloc(pOld, szNew); + void *pNew = realloc(pOld, szNew<=0 ? 1 : szNew); if( pNew==0 ) fatalError("unable to realloc for %d bytes", szNew); return pNew; } @@ -255,8 +305,9 @@ static VFile *createVFile(const char *zName, int sz, unsigned char *pData){ if( i>=MX_FILE ) return 0; pNew = &g.aFile[i]; if( zName ){ - pNew->zFilename = safe_realloc(0, strlen(zName)+1); - memcpy(pNew->zFilename, zName, strlen(zName)+1); + int nName = (int)strlen(zName)+1; + pNew->zFilename = safe_realloc(0, nName); + memcpy(pNew->zFilename, zName, nName); }else{ pNew->zFilename = 0; } @@ -267,6 +318,123 @@ static VFile *createVFile(const char *zName, int sz, unsigned char *pData){ return pNew; } +/* Return true if the line is all zeros */ +static int allZero(unsigned char *aLine){ + int i; + for(i=0; i<16 && aLine[i]==0; i++){} + return i==16; +} + +/* +** Render a database and query as text that can be input into +** the CLI. +*/ +static void renderDbSqlForCLI( + FILE *out, /* Write to this file */ + const char *zFile, /* Name of the database file */ + unsigned char *aDb, /* Database content */ + int nDb, /* Number of bytes in aDb[] */ + unsigned char *zSql, /* SQL content */ + int nSql /* Bytes of SQL */ +){ + fprintf(out, ".print ******* %s *******\n", zFile); + if( nDb>100 ){ + int i, j; /* Loop counters */ + int pgsz; /* Size of each page */ + int lastPage = 0; /* Last page number shown */ + int iPage; /* Current page number */ + unsigned char *aLine; /* Single line to display */ + unsigned char buf[16]; /* Fake line */ + unsigned char bShow[256]; /* Characters ok to display */ + + memset(bShow, '.', sizeof(bShow)); + for(i=' '; i<='~'; i++){ + if( i!='{' && i!='}' && i!='"' && i!='\\' ) bShow[i] = i; + } + pgsz = (aDb[16]<<8) | aDb[17]; + if( pgsz==0 ) pgsz = 65536; + if( pgsz<512 || (pgsz&(pgsz-1))!=0 ) pgsz = 4096; + fprintf(out,".open --hexdb\n"); + fprintf(out,"| size %d pagesize %d filename %s\n",nDb,pgsz,zFile); + for(i=0; i<nDb; i += 16){ + if( i+16>nDb ){ + memset(buf, 0, sizeof(buf)); + memcpy(buf, aDb+i, nDb-i); + aLine = buf; + }else{ + aLine = aDb + i; + } + if( allZero(aLine) ) continue; + iPage = i/pgsz + 1; + if( lastPage!=iPage ){ + fprintf(out,"| page %d offset %d\n", iPage, (iPage-1)*pgsz); + lastPage = iPage; + } + fprintf(out,"| %5d:", i-(iPage-1)*pgsz); + for(j=0; j<16; j++) fprintf(out," %02x", aLine[j]); + fprintf(out," "); + for(j=0; j<16; j++){ + unsigned char c = (unsigned char)aLine[j]; + fputc( bShow[c], stdout); + } + fputc('\n', stdout); + } + fprintf(out,"| end %s\n", zFile); + }else{ + fprintf(out,".open :memory:\n"); + } + fprintf(out,".testctrl prng_seed 1 db\n"); + fprintf(out,".testctrl internal_functions\n"); + fprintf(out,"%.*s", nSql, zSql); + if( nSql>0 && zSql[nSql-1]!='\n' ) fprintf(out, "\n"); +} + +/* +** Find the tail (the last component) of a pathname. +*/ +static const char *pathTail(const char *zPath){ + const char *zTail = zPath; + while( zPath[0] ){ + if( zPath[0]=='/' && zPath[1]!=0 ) zTail = &zPath[1]; +#ifndef __unix__ + if( zPath[0]=='\\' && zPath[1]!=0 ) zTail = &zPath[1]; +#endif + zPath++; + } + return zTail; +} + +/* +** Read the complete content of a file into memory. Add a 0x00 terminator +** and return a pointer to the result. +** +** The file content is held in memory obtained from sqlite_malloc64() which +** should be freed by the caller. +*/ +static char *readFile(const char *zFilename, long *sz){ + FILE *in; + long nIn; + unsigned char *pBuf; + + *sz = 0; + if( zFilename==0 ) return 0; + in = fopen(zFilename, "rb"); + if( in==0 ) return 0; + fseek(in, 0, SEEK_END); + *sz = nIn = ftell(in); + rewind(in); + pBuf = sqlite3_malloc64( nIn+1 ); + if( pBuf && 1==fread(pBuf, nIn, 1, in) ){ + pBuf[nIn] = 0; + fclose(in); + return (char*)pBuf; + } + sqlite3_free(pBuf); + *sz = 0; + fclose(in); + return 0; +} + /* ** Implementation of the "readfile(X)" SQL function. The entire content @@ -277,11 +445,33 @@ static void readfileFunc( sqlite3_context *context, int argc, sqlite3_value **argv +){ + long nIn; + void *pBuf; + const char *zName = (const char*)sqlite3_value_text(argv[0]); + + if( zName==0 ) return; + pBuf = readFile(zName, &nIn); + if( pBuf ){ + sqlite3_result_blob(context, pBuf, nIn, sqlite3_free); + } +} + +/* +** Implementation of the "readtextfile(X)" SQL function. The text content +** of the file named X through the end of the file or to the first \000 +** character, whichever comes first, is read and returned as TEXT. NULL +** is returned if the file does not exist or is unreadable. +*/ +static void readtextfileFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv ){ const char *zName; FILE *in; long nIn; - void *pBuf; + char *pBuf; zName = (const char*)sqlite3_value_text(argv[0]); if( zName==0 ) return; @@ -290,9 +480,10 @@ static void readfileFunc( fseek(in, 0, SEEK_END); nIn = ftell(in); rewind(in); - pBuf = sqlite3_malloc64( nIn ); + pBuf = sqlite3_malloc64( nIn+1 ); if( pBuf && 1==fread(pBuf, nIn, 1, in) ){ - sqlite3_result_blob(context, pBuf, nIn, sqlite3_free); + pBuf[nIn] = 0; + sqlite3_result_text(context, pBuf, -1, sqlite3_free); }else{ sqlite3_free(pBuf); } @@ -337,30 +528,37 @@ static void writefileFunc( static void blobListLoadFromDb( sqlite3 *db, /* Read from this database */ const char *zSql, /* Query used to extract the blobs */ - int onlyId, /* Only load where id is this value */ + int firstId, /* First sqlid to load */ + int lastId, /* Last sqlid to load */ int *pN, /* OUT: Write number of blobs loaded here */ Blob **ppList /* OUT: Write the head of the blob list here */ ){ - Blob head; + Blob *head; Blob *p; sqlite3_stmt *pStmt; int n = 0; int rc; char *z2; + union { + Blob sBlob; + unsigned char tmp[SZ_BLOB(8)]; + } uBlob; - if( onlyId>0 ){ - z2 = sqlite3_mprintf("%s WHERE rowid=%d", zSql, onlyId); + head = &uBlob.sBlob; + if( firstId>0 ){ + z2 = sqlite3_mprintf("%s WHERE rowid BETWEEN %d AND %d", zSql, + firstId, lastId); }else{ z2 = sqlite3_mprintf("%s", zSql); } rc = sqlite3_prepare_v2(db, z2, -1, &pStmt, 0); sqlite3_free(z2); if( rc ) fatalError("%s", sqlite3_errmsg(db)); - head.pNext = 0; - p = &head; + head->pNext = 0; + p = head; while( SQLITE_ROW==sqlite3_step(pStmt) ){ int sz = sqlite3_column_bytes(pStmt, 1); - Blob *pNew = safe_realloc(0, sizeof(*pNew)+sz ); + Blob *pNew = safe_realloc(0, SZ_BLOB(sz+1)); pNew->id = sqlite3_column_int(pStmt, 0); pNew->sz = sz; pNew->seq = n++; @@ -372,7 +570,7 @@ static void blobListLoadFromDb( } sqlite3_finalize(pStmt); *pN = n; - *ppList = head.pNext; + *ppList = head->pNext; } /* @@ -387,12 +585,19 @@ static void blobListFree(Blob *p){ } } - -/* Return the current wall-clock time */ +/* Return the current wall-clock time +** +** The number of milliseconds since the julian epoch. +** 1907-01-01 00:00:00 -> 210866716800000 +** 2021-01-01 00:00:00 -> 212476176000000 +*/ static sqlite3_int64 timeOfDay(void){ static sqlite3_vfs *clockVfs = 0; sqlite3_int64 t; - if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0); + if( clockVfs==0 ){ + clockVfs = sqlite3_vfs_find(0); + if( clockVfs==0 ) return 0; + } if( clockVfs->iVersion>=1 && clockVfs->xCurrentTimeInt64!=0 ){ clockVfs->xCurrentTimeInt64(clockVfs, &t); }else{ @@ -403,6 +608,878 @@ static sqlite3_int64 timeOfDay(void){ return t; } +/*************************************************************************** +** Code to process combined database+SQL scripts generated by the +** dbsqlfuzz fuzzer. +*/ + +/* An instance of the following object is passed by pointer as the +** client data to various callbacks. +*/ +typedef struct FuzzCtx { + sqlite3 *db; /* The database connection */ + sqlite3_int64 iCutoffTime; /* Stop processing at this time. */ + sqlite3_int64 iLastCb; /* Time recorded for previous progress callback */ + sqlite3_int64 mxInterval; /* Longest interval between two progress calls */ + unsigned nCb; /* Number of progress callbacks */ + unsigned mxCb; /* Maximum number of progress callbacks allowed */ + unsigned execCnt; /* Number of calls to the sqlite3_exec callback */ + int timeoutHit; /* True when reaching a timeout */ +} FuzzCtx; + +/* Verbosity level for the dbsqlfuzz test runner */ +static int eVerbosity = 0; + +/* True to activate PRAGMA vdbe_debug=on */ +static int bVdbeDebug = 0; + +/* Timeout for each fuzzing attempt, in milliseconds */ +static int giTimeout = 10000; /* Defaults to 10 seconds */ + +/* Maximum number of progress handler callbacks */ +static unsigned int mxProgressCb = 2000; + +/* Maximum string length in SQLite */ +static int lengthLimit = 1000000; + +/* Maximum expression depth */ +static int depthLimit = 500; + +/* Limit on the amount of heap memory that can be used */ +static sqlite3_int64 heapLimit = 100000000; + +/* Maximum byte-code program length in SQLite */ +static int vdbeOpLimit = 25000; + +/* Maximum size of the in-memory database */ +static sqlite3_int64 maxDbSize = 104857600; +/* OOM simulation parameters */ +static unsigned int oomCounter = 0; /* Simulate OOM when equals 1 */ +static unsigned int oomRepeat = 0; /* Number of OOMs in a row */ +static void*(*defaultMalloc)(int) = 0; /* The low-level malloc routine */ + +/* Enable recovery */ +static int bNoRecover = 0; + +/* This routine is called when a simulated OOM occurs. It is broken +** out as a separate routine to make it easy to set a breakpoint on +** the OOM +*/ +void oomFault(void){ + if( eVerbosity ){ + printf("Simulated OOM fault\n"); + } + if( oomRepeat>0 ){ + oomRepeat--; + }else{ + oomCounter--; + } +} + +/* This routine is a replacement malloc() that is used to simulate +** Out-Of-Memory (OOM) errors for testing purposes. +*/ +static void *oomMalloc(int nByte){ + if( oomCounter ){ + if( oomCounter==1 ){ + oomFault(); + return 0; + }else{ + oomCounter--; + } + } + return defaultMalloc(nByte); +} + +/* Register the OOM simulator. This must occur before any memory +** allocations */ +static void registerOomSimulator(void){ + sqlite3_mem_methods mem; + sqlite3_shutdown(); + sqlite3_config(SQLITE_CONFIG_GETMALLOC, &mem); + defaultMalloc = mem.xMalloc; + mem.xMalloc = oomMalloc; + sqlite3_config(SQLITE_CONFIG_MALLOC, &mem); +} + +/* Turn off any pending OOM simulation */ +static void disableOom(void){ + oomCounter = 0; + oomRepeat = 0; +} + +/* +** Translate a single byte of Hex into an integer. +** This routine only works if h really is a valid hexadecimal +** character: 0..9a..fA..F +*/ +static unsigned char hexToInt(unsigned int h){ +#ifdef SQLITE_EBCDIC + h += 9*(1&~(h>>4)); /* EBCDIC */ +#else + h += 9*(1&(h>>6)); /* ASCII */ +#endif + return h & 0xf; +} + +/* +** The first character of buffer zIn[0..nIn-1] is a '['. This routine +** checked to see if the buffer holds "[NNNN]" or "[+NNNN]" and if it +** does it makes corresponding changes to the *pK value and *pI value +** and returns true. If the input buffer does not match the patterns, +** no changes are made to either *pK or *pI and this routine returns false. +*/ +static int isOffset( + const unsigned char *zIn, /* Text input */ + int nIn, /* Bytes of input */ + unsigned int *pK, /* half-byte cursor to adjust */ + unsigned int *pI /* Input index to adjust */ +){ + int i; + unsigned int k = 0; + unsigned char c; + for(i=1; i<nIn && (c = zIn[i])!=']'; i++){ + if( !isxdigit(c) ) return 0; + k = k*16 + hexToInt(c); + } + if( i==nIn ) return 0; + *pK = 2*k; + *pI += i; + return 1; +} + +/* +** Decode the text starting at zIn into a binary database file. +** The maximum length of zIn is nIn bytes. Store the binary database +** file in space obtained from sqlite3_malloc(). +** +** Return the number of bytes of zIn consumed. Or return -1 if there +** is an error. One potential error is that the recipe specifies a +** database file larger than MX_FILE_SZ bytes. +** +** Abort on an OOM. +*/ +static int decodeDatabase( + const unsigned char *zIn, /* Input text to be decoded */ + int nIn, /* Bytes of input text */ + unsigned char **paDecode, /* OUT: decoded database file */ + int *pnDecode /* OUT: Size of decoded database */ +){ + unsigned char *a, *aNew; /* Database under construction */ + int mx = 0; /* Current size of the database */ + sqlite3_uint64 nAlloc = 4096; /* Space allocated in a[] */ + unsigned int i; /* Next byte of zIn[] to read */ + unsigned int j; /* Temporary integer */ + unsigned int k; /* half-byte cursor index for output */ + unsigned int n; /* Number of bytes of input */ + unsigned char b = 0; + if( nIn<4 ) return -1; + n = (unsigned int)nIn; + a = sqlite3_malloc64( nAlloc ); + if( a==0 ){ + fprintf(stderr, "Out of memory!\n"); + exit(1); + } + memset(a, 0, (size_t)nAlloc); + for(i=k=0; i<n; i++){ + unsigned char c = (unsigned char)zIn[i]; + if( isxdigit(c) ){ + k++; + if( k & 1 ){ + b = hexToInt(c)*16; + }else{ + b += hexToInt(c); + j = k/2 - 1; + if( j>=nAlloc ){ + sqlite3_uint64 newSize; + if( nAlloc==MX_FILE_SZ || j>=MX_FILE_SZ ){ + if( eVerbosity ){ + fprintf(stderr, "Input database too big: max %d bytes\n", + MX_FILE_SZ); + } + sqlite3_free(a); + return -1; + } + newSize = nAlloc*2; + if( newSize<=j ){ + newSize = (j+4096)&~4095; + } + if( newSize>MX_FILE_SZ ){ + if( j>=MX_FILE_SZ ){ + sqlite3_free(a); + return -1; + } + newSize = MX_FILE_SZ; + } + aNew = sqlite3_realloc64( a, newSize ); + if( aNew==0 ){ + sqlite3_free(a); + return -1; + } + a = aNew; + assert( newSize > nAlloc ); + memset(a+nAlloc, 0, (size_t)(newSize - nAlloc)); + nAlloc = newSize; + } + if( j>=(unsigned)mx ){ + mx = (j + 4095)&~4095; + if( mx>MX_FILE_SZ ) mx = MX_FILE_SZ; + } + assert( j<nAlloc ); + a[j] = b; + } + }else if( zIn[i]=='[' && i<n-3 && isOffset(zIn+i, nIn-i, &k, &i) ){ + continue; + }else if( zIn[i]=='\n' && i<n-4 && memcmp(zIn+i,"\n--\n",4)==0 ){ + i += 4; + break; + } + } + *pnDecode = mx; + *paDecode = a; + return i; +} + +/* +** Progress handler callback. +** +** The argument is the cutoff-time after which all processing should +** stop. So return non-zero if the cut-off time is exceeded. +*/ +static int progress_handler(void *pClientData) { + FuzzCtx *p = (FuzzCtx*)pClientData; + sqlite3_int64 iNow = timeOfDay(); + int rc = iNow>=p->iCutoffTime; + sqlite3_int64 iDiff = iNow - p->iLastCb; + /* printf("time-remaining: %lld\n", p->iCutoffTime - iNow); */ + if( iDiff > p->mxInterval ) p->mxInterval = iDiff; + p->nCb++; + if( rc==0 && p->mxCb>0 && p->mxCb<=p->nCb ) rc = 1; + if( rc && !p->timeoutHit && eVerbosity>=2 ){ + printf("Timeout on progress callback %d\n", p->nCb); + fflush(stdout); + p->timeoutHit = 1; + } + return rc; +} + +/* +** Flag bits set by block_troublesome_sql() +*/ +#define BTS_SELECT 0x000001 +#define BTS_NONSELECT 0x000002 +#define BTS_BADFUNC 0x000004 +#define BTS_BADPRAGMA 0x000008 /* Sticky for rest of the script */ + +/* +** Disallow debugging pragmas such as "PRAGMA vdbe_debug" and +** "PRAGMA parser_trace" since they can dramatically increase the +** amount of output without actually testing anything useful. +** +** Also block ATTACH if attaching a file from the filesystem. +*/ +static int block_troublesome_sql( + void *pClientData, + int eCode, + const char *zArg1, + const char *zArg2, + const char *zArg3, + const char *zArg4 +){ + unsigned int *pBtsFlags = (unsigned int*)pClientData; + + (void)zArg3; + (void)zArg4; + switch( eCode ){ + case SQLITE_PRAGMA: { + if( sqlite3_stricmp("busy_timeout",zArg1)==0 + && (zArg2==0 || strtoll(zArg2,0,0)>100 || strtoll(zArg2,0,10)>100) + ){ + return SQLITE_DENY; + }else if( sqlite3_stricmp("hard_heap_limit", zArg1)==0 + || sqlite3_stricmp("reverse_unordered_selects", zArg1)==0 + ){ + /* BTS_BADPRAGMA is sticky. A hard_heap_limit or + ** revert_unordered_selects should inhibit all future attempts + ** at verifying query invariants */ + *pBtsFlags |= BTS_BADPRAGMA; + }else if( eVerbosity==0 ){ + if( sqlite3_strnicmp("vdbe_", zArg1, 5)==0 + || sqlite3_stricmp("parser_trace", zArg1)==0 + || sqlite3_stricmp("temp_store_directory", zArg1)==0 + ){ + return SQLITE_DENY; + } + }else if( sqlite3_stricmp("oom",zArg1)==0 + && zArg2!=0 && zArg2[0]!=0 ){ + oomCounter = atoi(zArg2); + } + *pBtsFlags |= BTS_NONSELECT; + break; + } + case SQLITE_ATTACH: { + /* Deny the ATTACH if it is attaching anything other than an in-memory + ** database. */ + *pBtsFlags |= BTS_NONSELECT; + if( zArg1==0 ) return SQLITE_DENY; + if( strcmp(zArg1,":memory:")==0 ) return SQLITE_OK; + if( sqlite3_strglob("file:*[?]vfs=memdb", zArg1)==0 + && sqlite3_strglob("file:*[^/a-zA-Z0-9_.]*[?]vfs=memdb", zArg1)!=0 + ){ + return SQLITE_OK; + } + return SQLITE_DENY; + } + case SQLITE_SELECT: { + *pBtsFlags |= BTS_SELECT; + break; + } + case SQLITE_FUNCTION: { + static const char *azBadFuncs[] = { + "avg", + "count", + "cume_dist", + "current_date", + "current_time", + "current_timestamp", + "date", + "datetime", + "decimal_sum", + "dense_rank", + "first_value", + "geopoly_group_bbox", + "group_concat", + "implies_nonnull_row", + "json_group_array", + "json_group_object", + "julianday", + "lag", + "last_value", + "lead", + "max", + "min", + "nth_value", + "ntile", + "percent_rank", + "random", + "randomblob", + "rank", + "row_number", + "sqlite_offset", + "strftime", + "sum", + "time", + "total", + "unixepoch", + }; + int first, last; + first = 0; + last = sizeof(azBadFuncs)/sizeof(azBadFuncs[0]) - 1; + do{ + int mid = (first+last)/2; + int c = sqlite3_stricmp(azBadFuncs[mid], zArg2); + if( c<0 ){ + first = mid+1; + }else if( c>0 ){ + last = mid-1; + }else{ + *pBtsFlags |= BTS_BADFUNC; + break; + } + }while( first<=last ); + break; + } + case SQLITE_READ: { + /* Benign */ + break; + } + default: { + *pBtsFlags |= BTS_NONSELECT; + } + } + return SQLITE_OK; +} + +/* Implementation found in fuzzinvariant.c */ +extern int fuzz_invariant( + sqlite3 *db, /* The database connection */ + sqlite3_stmt *pStmt, /* Test statement stopped on an SQLITE_ROW */ + int iCnt, /* Invariant sequence number, starting at 0 */ + int iRow, /* The row number for pStmt */ + int nRow, /* Total number of output rows */ + int *pbCorrupt, /* IN/OUT: Flag indicating a corrupt database file */ + int eVerbosity, /* How much debugging output */ + unsigned int dbOpt /* Default optimization flags */ +); + +/* Implementation of sqlite_dbdata and sqlite_dbptr */ +extern int sqlite3_dbdata_init(sqlite3*,const char**,void*); + + +/* +** This function is used as a callback by the recover extension. Simply +** print the supplied SQL statement to stdout. +*/ +static int recoverSqlCb(void *pCtx, const char *zSql){ + if( eVerbosity>=2 && zSql ){ + printf("%s\n", zSql); + } + return SQLITE_OK; +} + +/* +** This function is called to recover data from the database. +*/ +static int recoverDatabase(sqlite3 *db){ + int rc; /* Return code from this routine */ + const char *zRecoveryDb = ""; /* Name of "recovery" database */ + const char *zLAF = "lost_and_found"; /* Name of "lost_and_found" table */ + int bFreelist = 1; /* True to scan the freelist */ + int bRowids = 1; /* True to restore ROWID values */ + sqlite3_recover *p = 0; /* The recovery object */ + + p = sqlite3_recover_init_sql(db, "main", recoverSqlCb, 0); + sqlite3_recover_config(p, 789, (void*)zRecoveryDb); + sqlite3_recover_config(p, SQLITE_RECOVER_LOST_AND_FOUND, (void*)zLAF); + sqlite3_recover_config(p, SQLITE_RECOVER_ROWIDS, (void*)&bRowids); + sqlite3_recover_config(p, SQLITE_RECOVER_FREELIST_CORRUPT,(void*)&bFreelist); + sqlite3_recover_run(p); + if( sqlite3_recover_errcode(p)!=SQLITE_OK ){ + const char *zErr = sqlite3_recover_errmsg(p); + int errCode = sqlite3_recover_errcode(p); + if( eVerbosity>0 ){ + printf("recovery error: %s (%d)\n", zErr, errCode); + } + } + rc = sqlite3_recover_finish(p); + if( eVerbosity>0 && rc ){ + printf("recovery returns error code %d\n", rc); + } + return rc; +} + +/* +** Special parameter binding, for testing and debugging purposes. +** +** $int_NNN -> integer value NNN +** $text_TTTT -> floating point value TTT with destructor +** $carray_clr -> First argument to carray() for color names +** $carray_primes -> First argument to carray() for prime numbers +*/ +static void bindDebugParameters(sqlite3_stmt *pStmt){ + int nVar = sqlite3_bind_parameter_count(pStmt); + int i; + for(i=1; i<=nVar; i++){ + const char *zVar = sqlite3_bind_parameter_name(pStmt, i); + if( zVar==0 ) continue; +#ifdef SQLITE_ENABLE_CARRAY + if( strcmp(zVar,"$carray_clr")==0 ){ + static char *azColorNames[] = { + "azure", "black", "blue", "brown", "cyan", "fuchsia", "gold", + "gray", "green", "indigo", "khaki", "lime", "magenta", "maroon", + "navy", "olive", "orange", "pink", "purple", "red", "silver", + "tan", "teal", "violet", "white", "yellow" + }; + sqlite3_carray_bind(pStmt,i,azColorNames,26,SQLITE_CARRAY_TEXT,0); + }else + if( strcmp(zVar,"$carray_primes")==0 ){ + static int aPrimes[] = { + 1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, + 53, 59, 61, 67, 71, 73, 79, 83, 89, 97 + }; + sqlite3_carray_bind(pStmt,i,aPrimes,26,SQLITE_CARRAY_INT32,0); + }else +#endif + if( strncmp(zVar, "$int_", 5)==0 ){ + sqlite3_bind_int(pStmt, i, atoi(&zVar[5])); + }else + if( strncmp(zVar, "$text_", 6)==0 ){ + size_t szVar = strlen(zVar); + char *zBuf = sqlite3_malloc64( szVar-5 ); + if( zBuf ){ + memcpy(zBuf, &zVar[6], szVar-5); + sqlite3_bind_text64(pStmt, i, zBuf, szVar-6, sqlite3_free, SQLITE_UTF8); + } + } + } +} + +/* +** Run the SQL text +*/ +static int runDbSql( + sqlite3 *db, /* Run SQL on this database connection */ + const char *zSql, /* The SQL to be run */ + unsigned int *pBtsFlags, + unsigned int dbOpt /* Default optimization flags */ +){ + int rc; + sqlite3_stmt *pStmt; + int bCorrupt = 0; + while( isspace(zSql[0]&0x7f) ) zSql++; + if( zSql[0]==0 ) return SQLITE_OK; + if( eVerbosity>=4 ){ + printf("RUNNING-SQL: [%s]\n", zSql); + fflush(stdout); + } + (*pBtsFlags) &= BTS_BADPRAGMA; + rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); + if( rc==SQLITE_OK ){ + int nRow = 0; + bindDebugParameters(pStmt); + while( (rc = sqlite3_step(pStmt))==SQLITE_ROW ){ + nRow++; + if( eVerbosity>=4 ){ + int j; + for(j=0; j<sqlite3_column_count(pStmt); j++){ + if( j ) printf(","); + switch( sqlite3_column_type(pStmt, j) ){ + case SQLITE_NULL: { + printf("NULL"); + break; + } + case SQLITE_INTEGER: + case SQLITE_FLOAT: { + printf("%s", sqlite3_column_text(pStmt, j)); + break; + } + case SQLITE_BLOB: { + int n = sqlite3_column_bytes(pStmt, j); + int i; + const unsigned char *a; + a = (const unsigned char*)sqlite3_column_blob(pStmt, j); + printf("x'"); + for(i=0; i<n; i++){ + printf("%02x", a[i]); + } + printf("'"); + break; + } + case SQLITE_TEXT: { + int n = sqlite3_column_bytes(pStmt, j); + int i; + const unsigned char *a; + a = (const unsigned char*)sqlite3_column_blob(pStmt, j); + printf("'"); + for(i=0; i<n; i++){ + if( a[i]=='\'' ){ + printf("''"); + }else{ + putchar(a[i]); + } + } + printf("'"); + break; + } + } /* End switch() */ + } /* End for() */ + printf("\n"); + fflush(stdout); + } /* End if( eVerbosity>=5 ) */ + } /* End while( SQLITE_ROW */ + if( rc==SQLITE_DONE ){ + if( (*pBtsFlags)==BTS_SELECT + && !sqlite3_stmt_isexplain(pStmt) + && nRow>0 + ){ + int iRow = 0; + sqlite3_reset(pStmt); + while( sqlite3_step(pStmt)==SQLITE_ROW ){ + int iCnt = 0; + iRow++; + for(iCnt=0; iCnt<99999; iCnt++){ + rc = fuzz_invariant(db, pStmt, iCnt, iRow, nRow, + &bCorrupt, eVerbosity, dbOpt); + if( rc==SQLITE_DONE ) break; + if( rc!=SQLITE_ERROR ) g.nInvariant++; + if( eVerbosity>0 ){ + if( rc==SQLITE_OK ){ + printf("invariant-check: ok\n"); + }else if( rc==SQLITE_CORRUPT ){ + printf("invariant-check: failed due to database corruption\n"); + } + } + } + } + } + }else if( eVerbosity>=4 ){ + printf("SQL-ERROR: (%d) %s\n", rc, sqlite3_errmsg(db)); + fflush(stdout); + } + }else if( eVerbosity>=4 ){ + printf("SQL-ERROR (%d): %s\n", rc, sqlite3_errmsg(db)); + fflush(stdout); + } /* End if( SQLITE_OK ) */ + return sqlite3_finalize(pStmt); +} + +/* Mappings into dbconfig settings for bits taken from bytes 72..75 of +** the input database. +** +** This should be the same as in dbsqlfuzz.c. Make sure those codes stay +** in sync. +*/ +static const struct { + unsigned int mask; + int iSetting; + char *zName; +} aDbConfigSettings[] = { + { 0x0001, SQLITE_DBCONFIG_ENABLE_FKEY, "enable_fkey" }, + { 0x0002, SQLITE_DBCONFIG_ENABLE_TRIGGER, "enable_trigger" }, + { 0x0004, SQLITE_DBCONFIG_ENABLE_VIEW, "enable_view" }, + { 0x0008, SQLITE_DBCONFIG_ENABLE_QPSG, "enable_qpsg" }, + { 0x0010, SQLITE_DBCONFIG_TRIGGER_EQP, "trigger_eqp" }, + { 0x0020, SQLITE_DBCONFIG_DEFENSIVE, "defensive" }, + { 0x0040, SQLITE_DBCONFIG_WRITABLE_SCHEMA, "writable_schema" }, + { 0x0080, SQLITE_DBCONFIG_LEGACY_ALTER_TABLE, "legacy_alter_table" }, + { 0x0100, SQLITE_DBCONFIG_STMT_SCANSTATUS, "stmt_scanstatus" }, + { 0x0200, SQLITE_DBCONFIG_REVERSE_SCANORDER, "reverse_scanorder" }, +#ifdef SQLITE_DBCONFIG_STRICT_AGGREGATE + { 0x0400, SQLITE_DBCONFIG_STRICT_AGGREGATE, "strict_aggregate" }, +#endif + { 0x0800, SQLITE_DBCONFIG_DQS_DML, "dqs_dml" }, + { 0x1000, SQLITE_DBCONFIG_DQS_DDL, "dqs_ddl" }, + { 0x2000, SQLITE_DBCONFIG_TRUSTED_SCHEMA, "trusted_schema" }, +}; + +/* Toggle a dbconfig setting +*/ +static void toggleDbConfig(sqlite3 *db, int iSetting){ + int v = 0; + sqlite3_db_config(db, iSetting, -1, &v); + v = !v; + sqlite3_db_config(db, iSetting, v, 0); +} + +/* Invoke this routine to run a single test case */ +int runCombinedDbSqlInput( + const uint8_t *aData, /* Combined DB+SQL content */ + size_t nByte, /* Size of aData in bytes */ + int iTimeout, /* Use this timeout */ + int bScript, /* If true, just render CLI output */ + int iSqlId /* SQL identifier */ +){ + int rc; /* SQLite API return value */ + int iSql; /* Index in aData[] of start of SQL */ + unsigned char *aDb = 0; /* Decoded database content */ + int nDb = 0; /* Size of the decoded database */ + int i; /* Loop counter */ + int j; /* Start of current SQL statement */ + char *zSql = 0; /* SQL text to run */ + int nSql; /* Bytes of SQL text */ + FuzzCtx cx; /* Fuzzing context */ + unsigned int btsFlags = 0; /* Parsing flags */ + unsigned int dbFlags = 0; /* Flag values from db offset 72..75 */ + unsigned int dbOpt = 0; /* Flag values from db offset 76..79 */ + + + if( nByte<10 ) return 0; + if( sqlite3_initialize() ) return 0; + if( sqlite3_memory_used()!=0 ){ + int nAlloc = 0; + int nNotUsed = 0; + sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &nAlloc, &nNotUsed, 0); + fprintf(stderr,"memory leak prior to test start:" + " %lld bytes in %d allocations\n", + sqlite3_memory_used(), nAlloc); + exit(1); + } + memset(&cx, 0, sizeof(cx)); + iSql = decodeDatabase((unsigned char*)aData, (int)nByte, &aDb, &nDb); + if( iSql<0 ) return 0; + if( nDb>=75 ){ + dbFlags = ((unsigned int)aDb[72]<<24) + ((unsigned int)aDb[73]<<16) + + ((unsigned int)aDb[74]<<8) + (unsigned int)aDb[75]; + } + if( nDb>=79 ){ + dbOpt = ((unsigned int)aDb[76]<<24) + ((unsigned int)aDb[77]<<16) + + ((unsigned int)aDb[78]<<8) + (unsigned int)aDb[79]; + } + nSql = (int)(nByte - iSql); + if( bScript ){ + char zName[100]; + sqlite3_snprintf(sizeof(zName),zName,"dbsql%06d.db",iSqlId); + renderDbSqlForCLI(stdout, zName, aDb, nDb, + (unsigned char*)(aData+iSql), nSql); + sqlite3_free(aDb); + return 0; + } + if( eVerbosity>=3 ){ + printf( + "****** %d-byte input, %d-byte database, %d-byte script " + "******\n", (int)nByte, nDb, nSql); + fflush(stdout); + } + rc = sqlite3_open(0, &cx.db); + if( rc ){ + sqlite3_free(aDb); + return 1; + } + sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, cx.db, dbOpt); + for(i=0; i<sizeof(aDbConfigSettings)/sizeof(aDbConfigSettings[0]); i++){ + if( dbFlags & aDbConfigSettings[i].mask ){ + toggleDbConfig(cx.db, aDbConfigSettings[i].iSetting); + } + } + if( bVdbeDebug ){ + sqlite3_exec(cx.db, "PRAGMA vdbe_debug=ON", 0, 0, 0); + } + + /* Invoke the progress handler frequently to check to see if we + ** are taking too long. The progress handler will return true + ** (which will block further processing) if more than giTimeout seconds have + ** elapsed since the start of the test. + */ + cx.iLastCb = timeOfDay(); + cx.iCutoffTime = cx.iLastCb + (iTimeout<giTimeout ? iTimeout : giTimeout); + cx.mxCb = mxProgressCb; +#ifndef SQLITE_OMIT_PROGRESS_CALLBACK + sqlite3_progress_handler(cx.db, 10, progress_handler, (void*)&cx); +#endif + + /* Set a limit on the maximum size of a prepared statement, and the + ** maximum length of a string or blob */ + if( vdbeOpLimit>0 ){ + sqlite3_limit(cx.db, SQLITE_LIMIT_VDBE_OP, vdbeOpLimit); + } + if( lengthLimit>0 ){ + sqlite3_limit(cx.db, SQLITE_LIMIT_LENGTH, lengthLimit); + } + if( depthLimit>0 ){ + sqlite3_limit(cx.db, SQLITE_LIMIT_EXPR_DEPTH, depthLimit); + } + sqlite3_limit(cx.db, SQLITE_LIMIT_LIKE_PATTERN_LENGTH, 100); + sqlite3_hard_heap_limit64(heapLimit); + rc = 1; + sqlite3_test_control(SQLITE_TESTCTRL_JSON_SELFCHECK, &rc); + + if( nDb>=20 && aDb[18]==2 && aDb[19]==2 ){ + aDb[18] = aDb[19] = 1; + } + rc = sqlite3_deserialize(cx.db, "main", aDb, nDb, nDb, + SQLITE_DESERIALIZE_RESIZEABLE | + SQLITE_DESERIALIZE_FREEONCLOSE); + if( rc ){ + fprintf(stderr, "sqlite3_deserialize() failed with %d\n", rc); + goto testrun_finished; + } + if( maxDbSize>0 ){ + sqlite3_int64 x = maxDbSize; + sqlite3_file_control(cx.db, "main", SQLITE_FCNTL_SIZE_LIMIT, &x); + } + + /* For high debugging levels, turn on debug mode */ + if( eVerbosity>=5 ){ + sqlite3_exec(cx.db, "PRAGMA vdbe_debug=ON;", 0, 0, 0); + } + + /* Block debug pragmas and ATTACH/DETACH. But wait until after + ** deserialize to do this because deserialize depends on ATTACH */ + sqlite3_set_authorizer(cx.db, block_troublesome_sql, &btsFlags); + + /* Add the vt02 virtual table */ + sqlite3_vt02_init(cx.db, 0, 0); + + /* Activate extensions */ + sqlite3_randomjson_init(cx.db, 0, 0); + + /* Add support for sqlite_dbdata and sqlite_dbptr virtual tables used + ** by the recovery API */ + sqlite3_dbdata_init(cx.db, 0, 0); + + /* Consistent PRNG seed */ +#ifdef SQLITE_TESTCTRL_PRNG_SEED + sqlite3_table_column_metadata(cx.db, 0, "x", 0, 0, 0, 0, 0, 0); + sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SEED, 1, cx.db); +#else + sqlite3_randomness(0,0); +#endif + + /* Run recovery on the initial database, just to make sure recovery + ** works. */ + if( !bNoRecover ){ + recoverDatabase(cx.db); + } + + zSql = sqlite3_malloc( nSql + 1 ); + if( zSql==0 ){ + fprintf(stderr, "Out of memory!\n"); + }else{ + memcpy(zSql, aData+iSql, nSql); + zSql[nSql] = 0; + for(i=j=0; zSql[i]; i++){ + if( zSql[i]==';' ){ + char cSaved = zSql[i+1]; + zSql[i+1] = 0; + if( sqlite3_complete(zSql+j) ){ + rc = runDbSql(cx.db, zSql+j, &btsFlags, dbOpt); + j = i+1; + } + zSql[i+1] = cSaved; + if( rc==SQLITE_INTERRUPT || progress_handler(&cx) ){ + goto testrun_finished; + } + } + } + if( j<i ){ + runDbSql(cx.db, zSql+j, &btsFlags, dbOpt); + } + } +testrun_finished: + sqlite3_free(zSql); + rc = sqlite3_close(cx.db); + if( rc!=SQLITE_OK ){ + fprintf(stdout, "sqlite3_close() returns %d\n", rc); + } + if( eVerbosity>=2 && !bScript ){ + fprintf(stdout, "Peak memory usages: %f MB\n", + sqlite3_memory_highwater(1) / 1000000.0); + } + if( sqlite3_memory_used()!=0 ){ + int nAlloc = 0; + int nNotUsed = 0; + sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &nAlloc, &nNotUsed, 0); + fprintf(stderr,"Memory leak: %lld bytes in %d allocations\n", + sqlite3_memory_used(), nAlloc); + exit(1); + } + sqlite3_hard_heap_limit64(0); + sqlite3_soft_heap_limit64(0); + return 0; +} + +/* +** END of the dbsqlfuzz code +***************************************************************************/ + +/* Look at a SQL text and try to determine if it begins with a database +** description, such as would be found in a dbsqlfuzz test case. Return +** true if this does appear to be a dbsqlfuzz test case and false otherwise. +*/ +static int isDbSql(unsigned char *a, int n){ + unsigned char buf[12]; + int i; + if( n>4 && memcmp(a,"\n--\n",4)==0 ) return 1; + while( n>0 && isspace(a[0]) ){ a++; n--; } + for(i=0; n>0 && i<8; n--, a++){ + if( isxdigit(a[0]) ) buf[i++] = a[0]; + } + if( i==8 && memcmp(buf,"53514c69",8)==0 ) return 1; + return 0; +} + +/* Implementation of the isdbsql(TEXT) SQL function. +*/ +static void isDbSqlFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + int n = sqlite3_value_bytes(argv[0]); + unsigned char *a = (unsigned char*)sqlite3_value_blob(argv[0]); + sqlite3_result_int(context, a!=0 && n>0 && isDbSql(a,n)); +} + /* Methods for the VHandle object */ static int inmemClose(sqlite3_file *pFile){ @@ -431,7 +1508,7 @@ static int inmemRead( if( iOfst+iAmt>pVFile->sz ){ memset(pData, 0, iAmt); iAmt = (int)(pVFile->sz - iOfst); - memcpy(pData, pVFile->a, iAmt); + memcpy(pData, pVFile->a + iOfst, iAmt); return SQLITE_IOERR_SHORT_READ; } memcpy(pData, pVFile->a + iOfst, iAmt); @@ -563,7 +1640,7 @@ static int inmemDelete( return SQLITE_IOERR_DELETE; } -/* Check for the existance of a file +/* Check for the existence of a file */ static int inmemAccess( sqlite3_vfs *pVfs, @@ -588,10 +1665,18 @@ static int inmemFullPathname( return SQLITE_OK; } +/* Always use the same random see, for repeatability. +*/ +static int inmemRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){ + memset(zBuf, 0, nBuf); + memcpy(zBuf, &g.uRandom, nBuf<sizeof(g.uRandom) ? nBuf : sizeof(g.uRandom)); + return nBuf; +} + /* ** Register the VFS that reads from the g.aFile[] set of files. */ -static void inmemVfsRegister(void){ +static void inmemVfsRegister(int makeDefault){ static sqlite3_vfs inmemVfs; sqlite3_vfs *pDefault = sqlite3_vfs_find(0); inmemVfs.iVersion = 3; @@ -602,10 +1687,10 @@ static void inmemVfsRegister(void){ inmemVfs.xDelete = inmemDelete; inmemVfs.xAccess = inmemAccess; inmemVfs.xFullPathname = inmemFullPathname; - inmemVfs.xRandomness = pDefault->xRandomness; + inmemVfs.xRandomness = inmemRandomness; inmemVfs.xSleep = pDefault->xSleep; inmemVfs.xCurrentTimeInt64 = pDefault->xCurrentTimeInt64; - sqlite3_vfs_register(&inmemVfs, 0); + sqlite3_vfs_register(&inmemVfs, makeDefault); }; /* @@ -693,21 +1778,28 @@ static void runSql(sqlite3 *db, const char *zSql, unsigned runFlags){ ** (2) Put all entries in order ** (3) Vacuum */ -static void rebuild_database(sqlite3 *db){ +static void rebuild_database(sqlite3 *db, int dbSqlOnly){ int rc; - rc = sqlite3_exec(db, + char *zSql; + zSql = sqlite3_mprintf( "BEGIN;\n" "CREATE TEMP TABLE dbx AS SELECT DISTINCT dbcontent FROM db;\n" "DELETE FROM db;\n" - "INSERT INTO db(dbid, dbcontent) SELECT NULL, dbcontent FROM dbx ORDER BY 2;\n" + "INSERT INTO db(dbid, dbcontent) " + " SELECT NULL, dbcontent FROM dbx ORDER BY 2;\n" "DROP TABLE dbx;\n" - "CREATE TEMP TABLE sx AS SELECT DISTINCT sqltext FROM xsql;\n" + "CREATE TEMP TABLE sx AS SELECT DISTINCT sqltext FROM xsql %s;\n" "DELETE FROM xsql;\n" - "INSERT INTO xsql(sqlid,sqltext) SELECT NULL, sqltext FROM sx ORDER BY 2;\n" + "INSERT INTO xsql(sqlid,sqltext) " + " SELECT NULL, sqltext FROM sx ORDER BY 2;\n" "DROP TABLE sx;\n" "COMMIT;\n" "PRAGMA page_size=1024;\n" - "VACUUM;\n", 0, 0, 0); + "VACUUM;\n", + dbSqlOnly ? " WHERE isdbsql(sqltext)" : "" + ); + rc = sqlite3_exec(db, zSql, 0, 0, 0); + sqlite3_free(zSql); if( rc ) fatalError("cannot rebuild: %s", sqlite3_errmsg(db)); } @@ -769,6 +1861,19 @@ static int integerValue(const char *zArg){ return (int)(isNeg? -v : v); } +/* +** Return the number of "v" characters in a string. Return 0 if there +** are any characters in the string other than "v". +*/ +static int numberOfVChar(const char *z){ + int N = 0; + while( z[0] && z[0]=='v' ){ + z++; + N++; + } + return z[0]==0 ? N : 0; +} + /* ** Print sketchy documentation for this utility program */ @@ -778,45 +1883,70 @@ static void showHelp(void){ "Read databases and SQL scripts from SOURCE-DB and execute each script against\n" "each database, checking for crashes and memory leaks.\n" "Options:\n" -" --cell-size-check Set the PRAGMA cell_size_check=ON\n" -" --dbid N Use only the database where dbid=N\n" -" --export-db DIR Write databases to files(s) in DIR. Works with --dbid\n" -" --export-sql DIR Write SQL to file(s) in DIR. Also works with --sqlid\n" -" --help Show this help text\n" -" -q Reduced output\n" -" --quiet Reduced output\n" -" --limit-mem N Limit memory used by test SQLite instance to N bytes\n" -" --limit-vdbe Panic if an sync SQL runs for more than 100,000 cycles\n" -" --load-sql ARGS... Load SQL scripts fro files into SOURCE-DB\n" -" --load-db ARGS... Load template databases from files into SOURCE_DB\n" -" -m TEXT Add a description to the database\n" -" --native-vfs Use the native VFS for initially empty database files\n" -" --rebuild Rebuild and vacuum the database file\n" -" --result-trace Show the results of each SQL command\n" -" --sqlid N Use only SQL where sqlid=N\n" -" --timeout N Abort if any single test case needs more than N seconds\n" -" -v Increased output\n" -" --verbose Increased output\n" +" --brief Output only a summary of results at the end\n" +" --cell-size-check Set the PRAGMA cell_size_check=ON\n" +" --dbid M..N Use only the databases where dbid between M and N\n" +" \"M..\" for M and afterwards. Just \"M\" for M only\n" +" --export-db DIR Write databases to files(s) in DIR. Works with --dbid\n" +" --export-sql DIR Write SQL to file(s) in DIR. Also works with --sqlid\n" +" --help Show this help text\n" +" --info Show information about SOURCE-DB w/o running tests\n" +" --limit-depth N Limit expression depth to N. Default: 500\n" +" --limit-heap N Limit heap memory to N. Default: 100M\n" +" --limit-mem N Limit memory used by test SQLite instance to N bytes\n" +" --limit-vdbe Panic if any test runs for more than 100,000 cycles\n" +" --load-sql FILE.. Load SQL scripts fron files into SOURCE-DB\n" +" --load-db FILE.. Load template databases from files into SOURCE_DB\n" +" --load-dbsql FILE.. Load dbsqlfuzz outputs into the xsql table\n" +" ^^^^------ Use \"-\" for FILE to read filenames from stdin\n" +" -m TEXT Add a description to the database\n" +" --native-vfs Use the native VFS for initially empty database files\n" +" --native-malloc Turn off MEMSYS3/5 and Lookaside\n" +" --no-recover Do not run recovery on dbsqlfuzz databases\n" +" --oss-fuzz Enable OSS-FUZZ testing\n" +" --prng-seed N Seed value for the PRGN inside of SQLite\n" +" -q|--quiet Reduced output\n" +" --rebuild Rebuild and vacuum the database file\n" +" --result-trace Show the results of each SQL command\n" +" --script Output CLI script instead of running tests\n" +" --skip N Skip the first N test cases\n" +" --slice M N Run only the M-th out of each group of N tests\n" +" --spinner Use a spinner to show progress\n" +" --sqlid M..N Use only SQL where sqlid between M..N\n" +" \"M..\" for M and afterwards. Just \"M\" for M only\n" +" --timeout N Maximum time for any one test in N millseconds\n" +" -v|--verbose Increased output. Repeat for more output.\n" +" --vdbe-debug Activate VDBE debugging.\n" +" --wait N Wait N seconds before continuing - useful for\n" +" attaching an MSVC debugging.\n" ); } int main(int argc, char **argv){ sqlite3_int64 iBegin; /* Start time of this program */ int quietFlag = 0; /* True if --quiet or -q */ + int briefFlag = 0; /* Output summary report at the end */ int verboseFlag = 0; /* True if --verbose or -v */ char *zInsSql = 0; /* SQL statement for --load-db or --load-sql */ - int iFirstInsArg = 0; /* First argv[] to use for --load-db or --load-sql */ + int iFirstInsArg = 0; /* First argv[] for --load-db or --load-sql */ sqlite3 *db = 0; /* The open database connection */ sqlite3_stmt *pStmt; /* A prepared statement */ int rc; /* Result code from SQLite interface calls */ Blob *pSql; /* For looping over SQL scripts */ Blob *pDb; /* For looping over template databases */ int i; /* Loop index for the argv[] loop */ - int onlySqlid = -1; /* --sqlid */ - int onlyDbid = -1; /* --dbid */ + int dbSqlOnly = 0; /* Only use scripts that are dbsqlfuzz */ + int firstSqlid = -1; /* First --sqlid range */ + int lastSqlid = 0x7fffffff; /* Last --sqlid range */ + int firstDbid = -1; /* --dbid */ + int lastDbid = 0x7fffffff; /* --dbid end */ int nativeFlag = 0; /* --native-vfs */ int rebuildFlag = 0; /* --rebuild */ int vdbeLimitFlag = 0; /* --limit-vdbe */ + int infoFlag = 0; /* --info */ + int nSkip = 0; /* --skip */ + int bScript = 0; /* --script */ + int bSpinner = 0; /* True for --spinner */ int timeoutTest = 0; /* undocumented --timeout-test flag */ int runFlags = 0; /* Flags sent to runSql() */ char *zMsg = 0; /* Add this message */ @@ -825,38 +1955,75 @@ int main(int argc, char **argv){ int iSrcDb; /* Loop over all source databases */ int nTest = 0; /* Total number of tests performed */ char *zDbName = ""; /* Appreviated name of a source database */ - const char *zFailCode = 0; /* Value of the TEST_FAILURE environment variable */ + const char *zFailCode = 0; /* Value of the TEST_FAILURE env variable */ int cellSzCkFlag = 0; /* --cell-size-check */ - int sqlFuzz = 0; /* True for SQL fuzz testing. False for DB fuzz */ - int iTimeout = 120; /* Default 120-second timeout */ - int nMem = 0; /* Memory limit */ + int sqlFuzz = 0; /* True for SQL fuzz. False for DB fuzz */ + int iTimeout = 120000; /* Default 120-second timeout */ + int nMem = 0; /* Memory limit override */ + int nMemThisDb = 0; /* Memory limit set by the CONFIG table */ char *zExpDb = 0; /* Write Databases to files in this directory */ char *zExpSql = 0; /* Write SQL to files in this directory */ void *pHeap = 0; /* Heap for use by SQLite */ + int ossFuzz = 0; /* enable OSS-FUZZ testing */ + int ossFuzzThisDb = 0; /* ossFuzz value for this particular database */ + int nativeMalloc = 0; /* Turn off MEMSYS3/5 and lookaside if true */ + sqlite3_vfs *pDfltVfs; /* The default VFS */ + int openFlags4Data; /* Flags for sqlite3_open_v2() */ + int bTimer = 0; /* Show elapse time for each test */ + int nV; /* How much to increase verbosity with -vvvv */ + sqlite3_int64 tmStart; /* Start of each test */ + int iEstTime = 0; /* LPF for the time-to-go */ + int iSliceSz = 0; /* Divide the test space into this many pieces */ + int iSliceIdx = 0; /* Only run the piece with this index */ + sqlite3_config(SQLITE_CONFIG_URI,1); + sqlite3_config(SQLITE_CONFIG_MEMSTATUS,1); + registerOomSimulator(); + sqlite3_initialize(); iBegin = timeOfDay(); #ifdef __unix__ - signal(SIGALRM, timeoutHandler); + signal(SIGALRM, signalHandler); + signal(SIGSEGV, signalHandler); + signal(SIGABRT, signalHandler); #endif g.zArgv0 = argv[0]; + openFlags4Data = SQLITE_OPEN_READONLY; zFailCode = getenv("TEST_FAILURE"); + pDfltVfs = sqlite3_vfs_find(0); + inmemVfsRegister(1); for(i=1; i<argc; i++){ const char *z = argv[i]; if( z[0]=='-' ){ z++; if( z[0]=='-' ) z++; + if( strcmp(z,"brief")==0 ){ + briefFlag = 1; + quietFlag = 1; + verboseFlag = 1; + eVerbosity = 0; + }else if( strcmp(z,"cell-size-check")==0 ){ cellSzCkFlag = 1; }else if( strcmp(z,"dbid")==0 ){ + const char *zDotDot; if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); - onlyDbid = integerValue(argv[++i]); + i++; + zDotDot = strstr(argv[i], ".."); + if( zDotDot ){ + firstDbid = atoi(argv[i]); + if( zDotDot[2] ){ + lastDbid = atoi(&zDotDot[2]); + } + }else{ + lastDbid = firstDbid = integerValue(argv[i]); + } }else if( strcmp(z,"export-db")==0 ){ if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); zExpDb = argv[++i]; }else - if( strcmp(z,"export-sql")==0 ){ + if( strcmp(z,"export-sql")==0 || strcmp(z,"export-dbsql")==0 ){ if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); zExpSql = argv[++i]; }else @@ -864,48 +2031,113 @@ int main(int argc, char **argv){ showHelp(); return 0; }else + if( strcmp(z,"info")==0 ){ + infoFlag = 1; + }else + if( strcmp(z,"limit-depth")==0 ){ + if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); + depthLimit = integerValue(argv[++i]); + }else + if( strcmp(z,"limit-heap")==0 ){ + if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); + heapLimit = integerValue(argv[++i]); + }else if( strcmp(z,"limit-mem")==0 ){ -#if !defined(SQLITE_ENABLE_MEMSYS3) && !defined(SQLITE_ENABLE_MEMSYS5) - fatalError("the %s option requires -DSQLITE_ENABLE_MEMSYS5 or _MEMSYS3", - argv[i]); -#else if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); nMem = integerValue(argv[++i]); -#endif }else if( strcmp(z,"limit-vdbe")==0 ){ vdbeLimitFlag = 1; }else if( strcmp(z,"load-sql")==0 ){ - zInsSql = "INSERT INTO xsql(sqltext) VALUES(CAST(readfile(?1) AS text))"; + zInsSql = "INSERT INTO xsql(sqltext)" + "VALUES(CAST(readtextfile(?1) AS text))"; iFirstInsArg = i+1; + openFlags4Data = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE; break; }else if( strcmp(z,"load-db")==0 ){ zInsSql = "INSERT INTO db(dbcontent) VALUES(readfile(?1))"; iFirstInsArg = i+1; + openFlags4Data = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE; + break; + }else + if( strcmp(z,"load-dbsql")==0 ){ + zInsSql = "INSERT INTO xsql(sqltext)" + "VALUES(readfile(?1))"; + iFirstInsArg = i+1; + openFlags4Data = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE; + dbSqlOnly = 1; break; }else if( strcmp(z,"m")==0 ){ if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); zMsg = argv[++i]; + openFlags4Data = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE; + }else + if( strcmp(z,"native-malloc")==0 ){ + nativeMalloc = 1; }else if( strcmp(z,"native-vfs")==0 ){ nativeFlag = 1; }else + if( strcmp(z,"no-recover")==0 ){ + bNoRecover = 1; + }else + if( strcmp(z,"oss-fuzz")==0 ){ + ossFuzz = 1; + }else + if( strcmp(z,"prng-seed")==0 ){ + if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); + g.uRandom = atoi(argv[++i]); + }else if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){ + briefFlag = 0; quietFlag = 1; verboseFlag = 0; + eVerbosity = 0; }else if( strcmp(z,"rebuild")==0 ){ rebuildFlag = 1; + openFlags4Data = SQLITE_OPEN_READWRITE; }else if( strcmp(z,"result-trace")==0 ){ runFlags |= SQL_OUTPUT; }else + if( strcmp(z,"script")==0 ){ + bScript = 1; + }else + if( strcmp(z,"skip")==0 ){ + if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); + nSkip = atoi(argv[++i]); + }else + if( strcmp(z,"slice")==0 ){ + if( i>=argc-2 ) fatalError("missing arguments on %s", argv[i]); + iSliceIdx = integerValue(argv[++i]); + iSliceSz = integerValue(argv[++i]); + /* --slice implices --brief */ + briefFlag = 1; + quietFlag = 1; + verboseFlag = 1; + eVerbosity = 0; + }else + if( strcmp(z,"spinner")==0 ){ + bSpinner = 1; + }else if( strcmp(z,"sqlid")==0 ){ + const char *zDotDot; if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); - onlySqlid = integerValue(argv[++i]); + i++; + zDotDot = strstr(argv[i], ".."); + if( zDotDot ){ + firstSqlid = atoi(argv[i]); + if( zDotDot[2] ){ + lastSqlid = atoi(&zDotDot[2]); + } + }else{ + firstSqlid = integerValue(argv[i]); + lastSqlid = firstSqlid; + } }else if( strcmp(z,"timeout")==0 ){ if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); @@ -917,10 +2149,61 @@ int main(int argc, char **argv){ fatalError("timeout is not available on non-unix systems"); #endif }else - if( strcmp(z,"verbose")==0 || strcmp(z,"v")==0 ){ + if( strcmp(z,"timer")==0 ){ + bTimer = 1; + }else + if( strcmp(z,"vdbe-debug")==0 ){ + bVdbeDebug = 1; + }else + if( strcmp(z,"verbose")==0 ){ + briefFlag = 0; quietFlag = 0; - verboseFlag = 1; - runFlags |= SQL_TRACE; + verboseFlag++; + eVerbosity++; + if( verboseFlag>2 ) runFlags |= SQL_TRACE; + }else + if( (nV = numberOfVChar(z))>=1 ){ + quietFlag = 0; + verboseFlag += nV; + eVerbosity += nV; + if( verboseFlag>2 ) runFlags |= SQL_TRACE; + }else + if( strcmp(z,"version")==0 ){ + int ii; + const char *zz; + printf("SQLite %s %s (%d-bit)\n", + sqlite3_libversion(), sqlite3_sourceid(), + 8*(int)sizeof(char*)); + for(ii=0; (zz = sqlite3_compileoption_get(ii))!=0; ii++){ + printf("%s\n", zz); + } + return 0; + }else + if( strcmp(z,"wait")==0 ){ + int iDelay; + if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); + iDelay = integerValue(argv[++i]); + printf("Waiting %d seconds:", iDelay); + fflush(stdout); + while( 1 /*exit-by-break*/ ){ + sqlite3_sleep(1000); + iDelay--; + if( iDelay<=0 ) break; + printf(" %d", iDelay); + fflush(stdout); + } + printf("\n"); + fflush(stdout); + }else + if( strcmp(z,"is-dbsql")==0 ){ + i++; + for(i++; i<argc; i++){ + long nData; + char *aData = readFile(argv[i], &nData); + printf("%d %s\n", isDbSql((unsigned char*)aData,nData), argv[i]); + sqlite3_free(aData); + } + exit(0); }else { fatalError("unknown option: %s", argv[i]); @@ -940,14 +2223,68 @@ int main(int argc, char **argv){ fatalError("cannot import into more than one database"); } } + if( iSliceSz<=iSliceIdx + || iSliceSz<=0 + || iSliceIdx<0 + ){ + iSliceSz = iSliceIdx = 0; + } /* Process each source database separately */ for(iSrcDb=0; iSrcDb<nSrcDb; iSrcDb++){ - rc = sqlite3_open(azSrcDb[iSrcDb], &db); + char *zRawData = 0; + long nRawData = 0; + g.zDbFile = azSrcDb[iSrcDb]; + rc = sqlite3_open_v2(azSrcDb[iSrcDb], &db, + openFlags4Data, pDfltVfs->zName); + if( rc==SQLITE_OK ){ + rc = sqlite3_exec(db, "SELECT count(*) FROM sqlite_schema", 0, 0, 0); + } if( rc ){ - fatalError("cannot open source database %s - %s", - azSrcDb[iSrcDb], sqlite3_errmsg(db)); + sqlite3_close(db); + zRawData = readFile(azSrcDb[iSrcDb], &nRawData); + if( zRawData==0 ){ + fatalError("input file \"%s\" is not recognized\n", azSrcDb[iSrcDb]); + } + sqlite3_open(":memory:", &db); + } + + /* Print the description, if there is one */ + if( infoFlag ){ + int n; + zDbName = azSrcDb[iSrcDb]; + i = (int)strlen(zDbName) - 1; + while( i>0 && zDbName[i-1]!='/' && zDbName[i-1]!='\\' ){ i--; } + zDbName += i; + sqlite3_prepare_v2(db, "SELECT msg FROM readme", -1, &pStmt, 0); + if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){ + printf("%s: %s", zDbName, sqlite3_column_text(pStmt,0)); + }else{ + printf("%s: (empty \"readme\")", zDbName); + } + sqlite3_finalize(pStmt); + sqlite3_prepare_v2(db, "SELECT count(*) FROM db", -1, &pStmt, 0); + if( pStmt + && sqlite3_step(pStmt)==SQLITE_ROW + && (n = sqlite3_column_int(pStmt,0))>0 + ){ + printf(" - %d DBs", n); + } + sqlite3_finalize(pStmt); + sqlite3_prepare_v2(db, "SELECT count(*) FROM xsql", -1, &pStmt, 0); + if( pStmt + && sqlite3_step(pStmt)==SQLITE_ROW + && (n = sqlite3_column_int(pStmt,0))>0 + ){ + printf(" - %d scripts", n); + } + sqlite3_finalize(pStmt); + printf("\n"); + sqlite3_close(db); + sqlite3_free(zRawData); + continue; } + rc = sqlite3_exec(db, "CREATE TABLE IF NOT EXISTS db(\n" " dbid INTEGER PRIMARY KEY, -- database id\n" @@ -969,27 +2306,88 @@ int main(int argc, char **argv){ sqlite3_free(zSql); if( rc ) fatalError("cannot change description: %s", sqlite3_errmsg(db)); } + if( zRawData ){ + zInsSql = "INSERT INTO xsql(sqltext) VALUES(?1)"; + rc = sqlite3_prepare_v2(db, zInsSql, -1, &pStmt, 0); + if( rc ) fatalError("cannot prepare statement [%s]: %s", + zInsSql, sqlite3_errmsg(db)); + sqlite3_bind_text(pStmt, 1, zRawData, nRawData, SQLITE_STATIC); + sqlite3_step(pStmt); + rc = sqlite3_reset(pStmt); + if( rc ) fatalError("insert failed for %s", argv[i]); + sqlite3_finalize(pStmt); + rebuild_database(db, dbSqlOnly); + zInsSql = 0; + sqlite3_free(zRawData); + zRawData = 0; + } + ossFuzzThisDb = ossFuzz; + + /* If the CONFIG(name,value) table exists, read db-specific settings + ** from that table */ + if( sqlite3_table_column_metadata(db,0,"config",0,0,0,0,0,0)==SQLITE_OK ){ + rc = sqlite3_prepare_v2(db, "SELECT name, value FROM config", + -1, &pStmt, 0); + if( rc ) fatalError("cannot prepare query of CONFIG table: %s", + sqlite3_errmsg(db)); + while( SQLITE_ROW==sqlite3_step(pStmt) ){ + const char *zName = (const char *)sqlite3_column_text(pStmt,0); + if( zName==0 ) continue; + if( strcmp(zName, "oss-fuzz")==0 ){ + ossFuzzThisDb = sqlite3_column_int(pStmt,1); + if( verboseFlag>1 ) printf("Config: oss-fuzz=%d\n", ossFuzzThisDb); + } + if( strcmp(zName, "limit-mem")==0 ){ + nMemThisDb = sqlite3_column_int(pStmt,1); + if( verboseFlag>1 ) printf("Config: limit-mem=%d\n", nMemThisDb); + } + } + sqlite3_finalize(pStmt); + } + if( zInsSql ){ sqlite3_create_function(db, "readfile", 1, SQLITE_UTF8, 0, readfileFunc, 0, 0); + sqlite3_create_function(db, "readtextfile", 1, SQLITE_UTF8, 0, + readtextfileFunc, 0, 0); + sqlite3_create_function(db, "isdbsql", 1, SQLITE_UTF8, 0, + isDbSqlFunc, 0, 0); rc = sqlite3_prepare_v2(db, zInsSql, -1, &pStmt, 0); if( rc ) fatalError("cannot prepare statement [%s]: %s", zInsSql, sqlite3_errmsg(db)); rc = sqlite3_exec(db, "BEGIN", 0, 0, 0); if( rc ) fatalError("cannot start a transaction"); for(i=iFirstInsArg; i<argc; i++){ - sqlite3_bind_text(pStmt, 1, argv[i], -1, SQLITE_STATIC); - sqlite3_step(pStmt); - rc = sqlite3_reset(pStmt); - if( rc ) fatalError("insert failed for %s", argv[i]); + if( strcmp(argv[i],"-")==0 ){ + /* A filename of "-" means read multiple filenames from stdin */ + char zLine[2000]; + while( rc==0 && fgets(zLine,sizeof(zLine),stdin)!=0 ){ + size_t kk = strlen(zLine); + while( kk>0 && zLine[kk-1]<=' ' ) kk--; + sqlite3_bind_text(pStmt, 1, zLine, (int)kk, SQLITE_STATIC); + if( verboseFlag>1 ) printf("loading %.*s\n", (int)kk, zLine); + sqlite3_step(pStmt); + rc = sqlite3_reset(pStmt); + if( rc ) fatalError("insert failed for %s", zLine); + } + }else{ + sqlite3_bind_text(pStmt, 1, argv[i], -1, SQLITE_STATIC); + if( verboseFlag>1 ) printf("loading %s\n", argv[i]); + sqlite3_step(pStmt); + rc = sqlite3_reset(pStmt); + if( rc ) fatalError("insert failed for %s", argv[i]); + } } sqlite3_finalize(pStmt); rc = sqlite3_exec(db, "COMMIT", 0, 0, 0); - if( rc ) fatalError("cannot commit the transaction: %s", sqlite3_errmsg(db)); - rebuild_database(db); + if( rc ) fatalError("cannot commit the transaction: %s", + sqlite3_errmsg(db)); + rebuild_database(db, dbSqlOnly); sqlite3_close(db); return 0; } + rc = sqlite3_exec(db, "PRAGMA query_only=1;", 0, 0, 0); + if( rc ) fatalError("cannot set database to query-only"); if( zExpDb!=0 || zExpSql!=0 ){ sqlite3_create_function(db, "writefile", 2, SQLITE_UTF8, 0, writefileFunc, 0, 0); @@ -997,13 +2395,14 @@ int main(int argc, char **argv){ const char *zExDb = "SELECT writefile(printf('%s/db%06d.db',?1,dbid),dbcontent)," " dbid, printf('%s/db%06d.db',?1,dbid), length(dbcontent)" - " FROM db WHERE ?2<0 OR dbid=?2;"; + " FROM db WHERE dbid BETWEEN ?2 AND ?3;"; rc = sqlite3_prepare_v2(db, zExDb, -1, &pStmt, 0); if( rc ) fatalError("cannot prepare statement [%s]: %s", zExDb, sqlite3_errmsg(db)); sqlite3_bind_text64(pStmt, 1, zExpDb, strlen(zExpDb), SQLITE_STATIC, SQLITE_UTF8); - sqlite3_bind_int(pStmt, 2, onlyDbid); + sqlite3_bind_int(pStmt, 2, firstDbid); + sqlite3_bind_int(pStmt, 3, lastDbid); while( sqlite3_step(pStmt)==SQLITE_ROW ){ printf("write db-%d (%d bytes) into %s\n", sqlite3_column_int(pStmt,1), @@ -1016,13 +2415,14 @@ int main(int argc, char **argv){ const char *zExSql = "SELECT writefile(printf('%s/sql%06d.txt',?1,sqlid),sqltext)," " sqlid, printf('%s/sql%06d.txt',?1,sqlid), length(sqltext)" - " FROM xsql WHERE ?2<0 OR sqlid=?2;"; + " FROM xsql WHERE sqlid BETWEEN ?2 AND ?3;"; rc = sqlite3_prepare_v2(db, zExSql, -1, &pStmt, 0); if( rc ) fatalError("cannot prepare statement [%s]: %s", zExSql, sqlite3_errmsg(db)); sqlite3_bind_text64(pStmt, 1, zExpSql, strlen(zExpSql), SQLITE_STATIC, SQLITE_UTF8); - sqlite3_bind_int(pStmt, 2, onlySqlid); + sqlite3_bind_int(pStmt, 2, firstSqlid); + sqlite3_bind_int(pStmt, 3, lastSqlid); while( sqlite3_step(pStmt)==SQLITE_ROW ){ printf("write sql-%d (%d bytes) into %s\n", sqlite3_column_int(pStmt,1), @@ -1038,11 +2438,11 @@ int main(int argc, char **argv){ /* Load all SQL script content and all initial database images from the ** source db */ - blobListLoadFromDb(db, "SELECT sqlid, sqltext FROM xsql", onlySqlid, - &g.nSql, &g.pFirstSql); + blobListLoadFromDb(db, "SELECT sqlid, sqltext FROM xsql", firstSqlid, + lastSqlid, &g.nSql, &g.pFirstSql); if( g.nSql==0 ) fatalError("need at least one SQL script"); - blobListLoadFromDb(db, "SELECT dbid, dbcontent FROM db", onlyDbid, - &g.nDb, &g.pFirstDb); + blobListLoadFromDb(db, "SELECT dbid, dbcontent FROM db", firstDbid, + lastDbid, &g.nDb, &g.pFirstDb); if( g.nDb==0 ){ g.pFirstDb = safe_realloc(0, sizeof(Blob)); memset(g.pFirstDb, 0, sizeof(Blob)); @@ -1053,16 +2453,18 @@ int main(int argc, char **argv){ } /* Print the description, if there is one */ - if( !quietFlag ){ + if( !quietFlag && !bScript ){ zDbName = azSrcDb[iSrcDb]; - i = strlen(zDbName) - 1; + i = (int)strlen(zDbName) - 1; while( i>0 && zDbName[i-1]!='/' && zDbName[i-1]!='\\' ){ i--; } zDbName += i; - sqlite3_prepare_v2(db, "SELECT msg FROM readme", -1, &pStmt, 0); - if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){ - printf("%s: %s\n", zDbName, sqlite3_column_text(pStmt,0)); + if( verboseFlag ){ + sqlite3_prepare_v2(db, "SELECT msg FROM readme", -1, &pStmt, 0); + if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){ + printf("%s: %s\n", zDbName, sqlite3_column_text(pStmt,0)); + } + sqlite3_finalize(pStmt); } - sqlite3_finalize(pStmt); } /* Rebuild the database, if requested */ @@ -1071,7 +2473,7 @@ int main(int argc, char **argv){ printf("%s: rebuilding... ", zDbName); fflush(stdout); } - rebuild_database(db); + rebuild_database(db, 0); if( !quietFlag ) printf("done\n"); } @@ -1084,30 +2486,114 @@ int main(int argc, char **argv){ } /* Limit available memory, if requested */ - if( nMem>0 ){ - sqlite3_shutdown(); - pHeap = malloc(nMem); - if( pHeap==0 ){ - fatalError("failed to allocate %d bytes of heap memory", nMem); + sqlite3_shutdown(); + + if( nMemThisDb>0 && nMem==0 ){ + if( !nativeMalloc ){ + pHeap = realloc(pHeap, nMemThisDb); + if( pHeap==0 ){ + fatalError("failed to allocate %d bytes of heap memory", nMem); + } + sqlite3_config(SQLITE_CONFIG_HEAP, pHeap, nMemThisDb, 128); + }else{ + sqlite3_hard_heap_limit64((sqlite3_int64)nMemThisDb); } - sqlite3_config(SQLITE_CONFIG_HEAP, pHeap, nMem, 128); + }else{ + sqlite3_hard_heap_limit64(0); + } + + /* Disable lookaside with the --native-malloc option */ + if( nativeMalloc ){ + sqlite3_config(SQLITE_CONFIG_LOOKASIDE, 0, 0); } - /* Register the in-memory virtual filesystem - */ + /* Reset the in-memory virtual filesystem */ formatVfs(); - inmemVfsRegister(); /* Run a test using each SQL script against each database. */ - if( !verboseFlag && !quietFlag ) printf("%s:", zDbName); + if( verboseFlag<2 && !quietFlag && !bSpinner && !bScript ){ + printf("%s:", zDbName); + } for(pSql=g.pFirstSql; pSql; pSql=pSql->pNext){ + tmStart = timeOfDay(); + if( isDbSql(pSql->a, pSql->sz) ){ + if( iSliceSz>0 && (nTest%iSliceSz)!=iSliceIdx ){ + nTest++; + continue; + } + sqlite3_snprintf(sizeof(g.zTestName), g.zTestName, "sqlid=%d",pSql->id); + if( bScript ){ + /* No progress output */ + }else if( bSpinner ){ + int nTotal = g.nSql; + int idx = pSql->seq; + if( nSrcDb==1 && nTotal>idx && idx>=20 ){ + int iToGo = (timeOfDay() - iBegin)*(nTotal-idx)/(idx*1000); + int hr, min, sec; + if( idx==20 ){ + iEstTime = iToGo; + }else{ + iEstTime = (iToGo + 7*iEstTime)/8; + } + hr = iEstTime/3600; + min = (iEstTime/60)%60; + sec = iEstTime%60; + if( hr>0 ){ + printf("\r%s: %d/%d ETC %d:%02d:%02d ", + zDbName, idx, nTotal, hr, min, sec); + }else{ + printf("\r%s: %d/%d ETC %02d:%02d ", + zDbName, idx, nTotal, min, sec); + } + }else{ + printf("\r%s: %d/%d ", zDbName, idx, nTotal); + } + fflush(stdout); + }else if( verboseFlag>1 ){ + printf("%s\n", g.zTestName); + fflush(stdout); + }else if( !quietFlag ){ + static int prevAmt = -1; + int idx = pSql->seq; + int amt = idx*10/(g.nSql); + if( amt!=prevAmt ){ + printf(" %d%%", amt*10); + fflush(stdout); + prevAmt = amt; + } + } + if( nSkip>0 ){ + nSkip--; + }else{ + runCombinedDbSqlInput(pSql->a, pSql->sz, iTimeout, bScript, pSql->id); + } + nTest++; + if( bTimer && !bScript ){ + sqlite3_int64 tmEnd = timeOfDay(); + printf("%lld %s\n", tmEnd - tmStart, g.zTestName); + } + g.zTestName[0] = 0; + disableOom(); + continue; + } for(pDb=g.pFirstDb; pDb; pDb=pDb->pNext){ int openFlags; const char *zVfs = "inmem"; + if( iSliceSz>0 && (nTest%iSliceSz)!=iSliceIdx ){ + nTest++; + continue; + } sqlite3_snprintf(sizeof(g.zTestName), g.zTestName, "sqlid=%d,dbid=%d", pSql->id, pDb->id); - if( verboseFlag ){ + if( bScript ){ + /* No progress output */ + }else if( bSpinner ){ + int nTotal = g.nDb*g.nSql; + int idx = pSql->seq*g.nDb + pDb->id - 1; + printf("\r%s: %d/%d ", zDbName, idx, nTotal); + fflush(stdout); + }else if( verboseFlag>1 ){ printf("%s\n", g.zTestName); fflush(stdout); }else if( !quietFlag ){ @@ -1120,29 +2606,71 @@ int main(int argc, char **argv){ prevAmt = amt; } } - createVFile("main.db", pDb->sz, pDb->a); - openFlags = SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE; - if( nativeFlag && pDb->sz==0 ){ - openFlags |= SQLITE_OPEN_MEMORY; - zVfs = 0; + if( nSkip>0 ){ + nSkip--; + continue; } - rc = sqlite3_open_v2("main.db", &db, openFlags, zVfs); - if( rc ) fatalError("cannot open inmem database"); - if( cellSzCkFlag ) runSql(db, "PRAGMA cell_size_check=ON", runFlags); - setAlarm(iTimeout); -#ifndef SQLITE_OMIT_PROGRESS_CALLBACK - if( sqlFuzz || vdbeLimitFlag ){ - sqlite3_progress_handler(db, 100000, progressHandler, &vdbeLimitFlag); + if( bScript ){ + char zName[100]; + sqlite3_snprintf(sizeof(zName), zName, "db%06d.db", + pDb->id>1 ? pDb->id : pSql->id); + renderDbSqlForCLI(stdout, zName, + pDb->a, pDb->sz, pSql->a, pSql->sz); + continue; } + createVFile("main.db", pDb->sz, pDb->a); + sqlite3_randomness(0,0); + if( ossFuzzThisDb ){ +#ifndef SQLITE_OSS_FUZZ + fatalError("--oss-fuzz not supported: recompile" + " with -DSQLITE_OSS_FUZZ"); +#else + extern int LLVMFuzzerTestOneInput(const uint8_t*, size_t); + LLVMFuzzerTestOneInput((const uint8_t*)pSql->a, (size_t)pSql->sz); #endif - do{ - runSql(db, (char*)pSql->a, runFlags); - }while( timeoutTest ); - setAlarm(0); - sqlite3_close(db); - if( sqlite3_memory_used()>0 ) fatalError("memory leak"); + }else{ + openFlags = SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE; + if( nativeFlag && pDb->sz==0 ){ + openFlags |= SQLITE_OPEN_MEMORY; + zVfs = 0; + } + rc = sqlite3_open_v2("main.db", &db, openFlags, zVfs); + if( rc ) fatalError("cannot open inmem database"); + sqlite3_limit(db, SQLITE_LIMIT_LENGTH, 100000000); + sqlite3_limit(db, SQLITE_LIMIT_LIKE_PATTERN_LENGTH, 50); + if( cellSzCkFlag ) runSql(db, "PRAGMA cell_size_check=ON", runFlags); + setAlarm((iTimeout+999)/1000); + /* Enable test functions */ + sqlite3_test_control(SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, db); +#ifndef SQLITE_OMIT_PROGRESS_CALLBACK + if( sqlFuzz || vdbeLimitFlag ){ + sqlite3_progress_handler(db, 100000, progressHandler, + &vdbeLimitFlag); + } +#endif +#ifdef SQLITE_TESTCTRL_PRNG_SEED + sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SEED, 1, db); +#endif + if( bVdbeDebug ){ + sqlite3_exec(db, "PRAGMA vdbe_debug=ON", 0, 0, 0); + } + do{ + runSql(db, (char*)pSql->a, runFlags); + }while( timeoutTest ); + setAlarm(0); + sqlite3_exec(db, "PRAGMA temp_store_directory=''", 0, 0, 0); + sqlite3_close(db); + } + if( sqlite3_memory_used()>0 ){ + fatalError("memory leak: %lld bytes outstanding", + sqlite3_memory_used()); + } reformatVfs(); nTest++; + if( bTimer ){ + sqlite3_int64 tmEnd = timeOfDay(); + printf("%lld %s\n", tmEnd - tmStart, g.zTestName); + } g.zTestName[0] = 0; /* Simulate an error if the TEST_FAILURE environment variable is "5". @@ -1162,7 +2690,25 @@ int main(int argc, char **argv){ } } } - if( !quietFlag && !verboseFlag ){ + if( briefFlag ){ + sqlite3_int64 iElapse = timeOfDay() - iBegin; + if( iSliceSz>0 ){ + printf("%s %s: slice %d/%d of %d tests, %d.%03d seconds\n", + pathTail(argv[0]), pathTail(g.zDbFile), + iSliceIdx, iSliceSz, nTest, + (int)(iElapse/1000), (int)(iElapse%1000)); + }else{ + printf("%s %s: 0 errors, %d tests, %d.%03d seconds\n", + pathTail(argv[0]), pathTail(g.zDbFile), nTest, + (int)(iElapse/1000), (int)(iElapse%1000)); + } + iBegin = timeOfDay(); + }else if( bScript ){ + /* No progress output */ + }else if( bSpinner ){ + int nTotal = g.nDb*g.nSql; + printf("\r%s: %d/%d \n", zDbName, nTotal, nTotal); + }else if( !quietFlag && verboseFlag<2 ){ printf(" 100%% - %d tests\n", g.nDb*g.nSql); } @@ -1175,12 +2721,17 @@ int main(int argc, char **argv){ } /* End loop over all source databases */ - if( !quietFlag ){ + + if( !quietFlag && !bScript ){ sqlite3_int64 iElapse = timeOfDay() - iBegin; + if( g.nInvariant ){ + printf("fuzzcheck: %u query invariants checked\n", g.nInvariant); + } printf("fuzzcheck: 0 errors out of %d tests in %d.%03d seconds\n" - "SQLite %s %s\n", + "SQLite %s %s (%d-bit)\n", nTest, (int)(iElapse/1000), (int)(iElapse%1000), - sqlite3_libversion(), sqlite3_sourceid()); + sqlite3_libversion(), sqlite3_sourceid(), + 8*(int)sizeof(char*)); } free(azSrcDb); free(pHeap); diff --git a/test/fuzzdata1.db b/test/fuzzdata1.db index 4b4a6b574b..11a9174b2d 100644 Binary files a/test/fuzzdata1.db and b/test/fuzzdata1.db differ diff --git a/test/fuzzdata2.db b/test/fuzzdata2.db index 598814622c..7682790046 100644 Binary files a/test/fuzzdata2.db and b/test/fuzzdata2.db differ diff --git a/test/fuzzdata4.db b/test/fuzzdata4.db index b97ca104e7..254bf834ff 100644 Binary files a/test/fuzzdata4.db and b/test/fuzzdata4.db differ diff --git a/test/fuzzdata5.db b/test/fuzzdata5.db new file mode 100644 index 0000000000..cfb0ebe7d8 Binary files /dev/null and b/test/fuzzdata5.db differ diff --git a/test/fuzzdata6.db b/test/fuzzdata6.db new file mode 100644 index 0000000000..67076d0026 Binary files /dev/null and b/test/fuzzdata6.db differ diff --git a/test/fuzzdata7.db b/test/fuzzdata7.db new file mode 100644 index 0000000000..99daab1092 Binary files /dev/null and b/test/fuzzdata7.db differ diff --git a/test/fuzzdata8.db b/test/fuzzdata8.db new file mode 100644 index 0000000000..bfa3e3ecd0 Binary files /dev/null and b/test/fuzzdata8.db differ diff --git a/test/fuzzerfault.test b/test/fuzzerfault.test index 6449612a66..e281cb592e 100644 --- a/test/fuzzerfault.test +++ b/test/fuzzerfault.test @@ -88,5 +88,24 @@ do_faultsim_test 3 -prep { faultsim_test_result {0 2} {1 {vtable constructor failed: x1}} } +#------------------------------------------------------------------------- +reset_db +do_execsql_test 4.0 { + CREATE TABLE t1_a(a INTEFDR PRIMARY KEY, b TEXT); + CREATE TABLE t3_a(k FnTEGER PRIMARY KEY, v TEXT); + CREATE TABLE t3_b(k INTEÀ5R PRIMARY KEY, v TEXT); + CREATE VIEW t3 AS SELECT * FROM t3_a UNION ALL SELECT * FROM t3_b; +} +faultsim_save_and_close + +do_faultsim_test 4 -faults oom-t* -prep { + faultsim_restore_and_reopen +} -body { + execsql { + SELECT 1 FROM t1_a LEFT JOIN t3 ON ((1+1) AND k=1) + } +} -test { + faultsim_test_result {0 {}} +} finish_test diff --git a/test/fuzzinvariants.c b/test/fuzzinvariants.c new file mode 100644 index 0000000000..6a5cfda689 --- /dev/null +++ b/test/fuzzinvariants.c @@ -0,0 +1,580 @@ +/* +** 2022-06-14 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** This library is used by fuzzcheck to test query invariants. +** +** An sqlite3_stmt is passed in that has just returned SQLITE_ROW. This +** routine does: +** +** * Record the output of the current row +** * Construct an alternative query that should return the same row +** * Run the alternative query and verify that it does in fact return +** the same row +** +*/ +#include "sqlite3.h" +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <ctype.h> + +/* Forward references */ +static char *fuzz_invariant_sql(sqlite3_stmt*, int); +static int sameValue(sqlite3_stmt*,int,sqlite3_stmt*,int,sqlite3_stmt*); +static void reportInvariantFailed( + sqlite3_stmt *pOrig, /* The original query */ + sqlite3_stmt *pTest, /* The alternative test query with a missing row */ + int iRow, /* Row number in pOrig */ + unsigned int dbOpt, /* Optimization flags on pOrig */ + int noOpt /* True if opt flags inverted for pTest */ +); + +/* +** Special parameter binding, for testing and debugging purposes. +** +** $int_NNN -> integer value NNN +** $text_TTTT -> floating point value TTT with destructor +** $carray_clr -> First argument to carray() for color names +** $carray_primes -> First argument to carray() for prime numbers +*/ +static void bindDebugParameters(sqlite3_stmt *pStmt){ + int nVar = sqlite3_bind_parameter_count(pStmt); + int i; + for(i=1; i<=nVar; i++){ + const char *zVar = sqlite3_bind_parameter_name(pStmt, i); + if( zVar==0 ) continue; +#ifdef SQLITE_ENABLE_CARRAY + if( strcmp(zVar,"$carray_clr")==0 ){ + static char *azColorNames[] = { + "azure", "black", "blue", "brown", "cyan", "fuchsia", "gold", + "gray", "green", "indigo", "khaki", "lime", "magenta", "maroon", + "navy", "olive", "orange", "pink", "purple", "red", "silver", + "tan", "teal", "violet", "white", "yellow" + }; + sqlite3_carray_bind(pStmt,i,azColorNames,26,SQLITE_CARRAY_TEXT,0); + }else + if( strcmp(zVar,"$carray_primes")==0 ){ + static int aPrimes[] = { + 1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, + 53, 59, 61, 67, 71, 73, 79, 83, 89, 97 + }; + sqlite3_carray_bind(pStmt,i,aPrimes,26,SQLITE_CARRAY_INT32,0); + }else +#endif + if( strncmp(zVar, "$int_", 5)==0 ){ + sqlite3_bind_int(pStmt, i, atoi(&zVar[5])); + }else + if( strncmp(zVar, "$text_", 6)==0 ){ + size_t szVar = strlen(zVar); + char *zBuf = sqlite3_malloc64( szVar-5 ); + if( zBuf ){ + memcpy(zBuf, &zVar[6], szVar-5); + sqlite3_bind_text64(pStmt, i, zBuf, szVar-6, sqlite3_free, SQLITE_UTF8); + } + } + } +} + +/* +** Do an invariant check on pStmt. iCnt determines which invariant check to +** perform. The first check is iCnt==0. +** +** *pbCorrupt is a flag that, if true, indicates that the database file +** is known to be corrupt. A value of non-zero means "yes, the database +** is corrupt". A zero value means "we do not know whether or not the +** database is corrupt". The value might be set prior to entry, or this +** routine might set the value. +** +** Return values: +** +** SQLITE_OK This check was successful. +** +** SQLITE_DONE iCnt is out of range. The caller typically sets +** up a loop on iCnt starting with zero, and increments +** iCnt until this code is returned. +** +** SQLITE_CORRUPT The invariant failed, but the underlying database +** file is indicating that it is corrupt, which might +** be the cause of the malfunction. The *pCorrupt +** value will also be set. +** +** SQLITE_INTERNAL The invariant failed, and the database file is not +** corrupt. (This never happens because this function +** will call abort() following an invariant failure.) +** +** (other) Some other kind of error occurred. +*/ +int fuzz_invariant( + sqlite3 *db, /* The database connection */ + sqlite3_stmt *pStmt, /* Test statement stopped on an SQLITE_ROW */ + int iCnt, /* Invariant sequence number, starting at 0 */ + int iRow, /* Current row number */ + int nRow, /* Number of output rows from pStmt */ + int *pbCorrupt, /* IN/OUT: Flag indicating a corrupt database file */ + int eVerbosity, /* How much debugging output */ + unsigned int dbOpt /* Default optimization flags */ +){ + char *zTest; + sqlite3_stmt *pTestStmt = 0; + int rc; + int i; + int nCol; + int nParam; + int noOpt = (iCnt%3)==0; + + if( *pbCorrupt ) return SQLITE_DONE; + nParam = sqlite3_bind_parameter_count(pStmt); + if( nParam>100 ) return SQLITE_DONE; + zTest = fuzz_invariant_sql(pStmt, iCnt); + if( zTest==0 ) return SQLITE_DONE; + if( noOpt ){ + sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, db, ~dbOpt); + } + rc = sqlite3_prepare_v2(db, zTest, -1, &pTestStmt, 0); + if( noOpt ){ + sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, db, dbOpt); + } + if( rc ){ + if( eVerbosity ){ + printf("invariant compile failed: %s\n%s\n", + sqlite3_errmsg(db), zTest); + } + sqlite3_free(zTest); + sqlite3_finalize(pTestStmt); + return rc; + } + sqlite3_free(zTest); + bindDebugParameters(pTestStmt); + nCol = sqlite3_column_count(pStmt); + for(i=0; i<nCol; i++){ + rc = sqlite3_bind_value(pTestStmt,i+1+nParam,sqlite3_column_value(pStmt,i)); + if( rc!=SQLITE_OK && rc!=SQLITE_RANGE ){ + sqlite3_finalize(pTestStmt); + return rc; + } + } + if( eVerbosity>=2 ){ + char *zSql = sqlite3_expanded_sql(pTestStmt); + printf("invariant-sql row=%d #%d:\n%s\n", iRow, iCnt, zSql); + sqlite3_free(zSql); + } + while( (rc = sqlite3_step(pTestStmt))==SQLITE_ROW ){ + for(i=0; i<nCol; i++){ + if( !sameValue(pStmt, i, pTestStmt, i, 0) ) break; + } + if( i>=nCol ) break; + } + if( rc==SQLITE_DONE ){ + /* No matching output row found */ + sqlite3_stmt *pCk = 0; + int iOrigRSO; + + + /* This is not a fault if the database file is corrupt, because anything + ** can happen with a corrupt database file */ + rc = sqlite3_prepare_v2(db, "PRAGMA integrity_check", -1, &pCk, 0); + if( rc ){ + sqlite3_finalize(pCk); + sqlite3_finalize(pTestStmt); + return rc; + } + if( eVerbosity>=2 ){ + char *zSql = sqlite3_expanded_sql(pCk); + printf("invariant-validity-check #1:\n%s\n", zSql); + sqlite3_free(zSql); + } + + rc = sqlite3_step(pCk); + if( rc!=SQLITE_ROW + || sqlite3_column_text(pCk, 0)==0 + || strcmp((const char*)sqlite3_column_text(pCk,0),"ok")!=0 + ){ + *pbCorrupt = 1; + sqlite3_finalize(pCk); + sqlite3_finalize(pTestStmt); + return SQLITE_CORRUPT; + } + sqlite3_finalize(pCk); + + /* + ** If inverting the scan order also results in a miss, assume that the + ** query is ambiguous and do not report a fault. + */ + sqlite3_db_config(db, SQLITE_DBCONFIG_REVERSE_SCANORDER, -1, &iOrigRSO); + sqlite3_db_config(db, SQLITE_DBCONFIG_REVERSE_SCANORDER, !iOrigRSO, 0); + sqlite3_prepare_v2(db, sqlite3_sql(pStmt), -1, &pCk, 0); + sqlite3_db_config(db, SQLITE_DBCONFIG_REVERSE_SCANORDER, iOrigRSO, 0); + if( eVerbosity>=2 ){ + char *zSql = sqlite3_expanded_sql(pCk); + printf("invariant-validity-check #2:\n%s\n", zSql); + sqlite3_free(zSql); + } + bindDebugParameters(pCk); + while( (rc = sqlite3_step(pCk))==SQLITE_ROW ){ + for(i=0; i<nCol; i++){ + if( !sameValue(pStmt, i, pTestStmt, i, 0) ) break; + } + if( i>=nCol ) break; + } + sqlite3_finalize(pCk); + if( rc==SQLITE_DONE ){ + sqlite3_finalize(pTestStmt); + return SQLITE_DONE; + } + + /* The original sameValue() comparison assumed a collating sequence + ** of "binary". It can sometimes get an incorrect result for different + ** collating sequences. So rerun the test with no assumptions about + ** collations. + */ + rc = sqlite3_prepare_v2(db, + "SELECT ?1=?2 OR ?1=?2 COLLATE nocase OR ?1=?2 COLLATE rtrim", + -1, &pCk, 0); + if( rc==SQLITE_OK ){ + if( eVerbosity>=2 ){ + char *zSql = sqlite3_expanded_sql(pCk); + printf("invariant-validity-check #3:\n%s\n", zSql); + sqlite3_free(zSql); + } + + sqlite3_reset(pTestStmt); + bindDebugParameters(pCk); + while( (rc = sqlite3_step(pTestStmt))==SQLITE_ROW ){ + for(i=0; i<nCol; i++){ + if( !sameValue(pStmt, i, pTestStmt, i, pCk) ) break; + } + if( i>=nCol ){ + sqlite3_finalize(pCk); + goto not_a_fault; + } + } + } + sqlite3_finalize(pCk); + + /* Invariants do not necessarily work if there are virtual tables + ** involved in the query */ + rc = sqlite3_prepare_v2(db, + "SELECT 1 FROM bytecode(?1) WHERE opcode='VOpen'", -1, &pCk, 0); + if( rc==SQLITE_OK ){ + if( eVerbosity>=2 ){ + char *zSql = sqlite3_expanded_sql(pCk); + printf("invariant-validity-check #4:\n%s\n", zSql); + sqlite3_free(zSql); + } + sqlite3_bind_pointer(pCk, 1, pStmt, "stmt-pointer", 0); + rc = sqlite3_step(pCk); + } + sqlite3_finalize(pCk); + if( rc==SQLITE_DONE ){ + reportInvariantFailed(pStmt, pTestStmt, iRow, dbOpt, noOpt); + return SQLITE_INTERNAL; + }else if( eVerbosity>0 ){ + printf("invariant-error ignored due to the use of virtual tables\n"); + } + } +not_a_fault: + sqlite3_finalize(pTestStmt); + return SQLITE_OK; +} + +/* +** Generate SQL used to test a statement invariant. +** +** Return 0 if the iCnt is out of range. +** +** iCnt meanings: +** +** 0 SELECT * FROM (<query>) +** 1 SELECT DISTINCT * FROM (<query>) +** 2 SELECT * FROM (<query>) WHERE ORDER BY 1 +** 3 SELECT DISTINCT * FROM (<query>) ORDER BY 1 +** 4 SELECT * FROM (<query>) WHERE <all-columns>=<all-values> +** 5 SELECT DISTINCT * FROM (<query>) WHERE <all-columns=<all-values +** 6 SELECT * FROM (<query>) WHERE <all-column>=<all-value> ORDER BY 1 +** 7 SELECT DISTINCT * FROM (<query>) WHERE <all-column>=<all-value> +** ORDER BY 1 +** N+0 SELECT * FROM (<query>) WHERE <nth-column>=<value> +** N+1 SELECT DISTINCT * FROM (<query>) WHERE <Nth-column>=<value> +** N+2 SELECT * FROM (<query>) WHERE <Nth-column>=<value> ORDER BY 1 +** N+3 SELECT DISTINCT * FROM (<query>) WHERE <Nth-column>=<value> +** ORDER BY N +** +*/ +static char *fuzz_invariant_sql(sqlite3_stmt *pStmt, int iCnt){ + const char *zIn; + size_t nIn; + const char *zAnd = "WHERE"; + int i, j; + sqlite3_str *pTest; + sqlite3_stmt *pBase = 0; + sqlite3 *db = sqlite3_db_handle(pStmt); + int rc; + int nCol = sqlite3_column_count(pStmt); + int mxCnt; + int bDistinct = 0; + int bOrderBy = 0; + int nParam = sqlite3_bind_parameter_count(pStmt); + int hasGroupBy = 0; + + switch( iCnt % 4 ){ + case 1: bDistinct = 1; break; + case 2: bOrderBy = 1; break; + case 3: bDistinct = bOrderBy = 1; break; + } + iCnt /= 4; + mxCnt = nCol; + if( iCnt<0 || iCnt>mxCnt ) return 0; + zIn = sqlite3_sql(pStmt); + if( zIn==0 ) return 0; + nIn = strlen(zIn); + while( nIn>0 && (isspace(zIn[nIn-1]) || zIn[nIn-1]==';') ) nIn--; + if( strchr(zIn, '?') ) return 0; + pTest = sqlite3_str_new(0); + sqlite3_str_appendf(pTest, "SELECT %s* FROM (", + bDistinct ? "DISTINCT " : ""); + sqlite3_str_append(pTest, zIn, (int)nIn); + sqlite3_str_append(pTest, ")", 1); + rc = sqlite3_prepare_v2(db, sqlite3_str_value(pTest), -1, &pBase, 0); + if( rc ){ + sqlite3_finalize(pBase); + pBase = pStmt; + } + hasGroupBy = sqlite3_strlike("%GROUP BY%",zIn,0)==0; + bindDebugParameters(pBase); + for(i=0; i<sqlite3_column_count(pStmt); i++){ + const char *zColName = sqlite3_column_name(pBase,i); + const char *zSuffix = zColName ? strrchr(zColName, ':') : 0; + if( zSuffix + && isdigit(zSuffix[1]) + && (zSuffix[1]>'3' || isdigit(zSuffix[2])) + ){ + /* This is a randomized column name and so cannot be used in the + ** WHERE clause. */ + continue; + } + for(j=0; j<i; j++){ + const char *zPrior = sqlite3_column_name(pBase, j); + if( sqlite3_stricmp(zPrior, zColName)==0 ) break; + } + if( j<i ){ + /* Duplicate column name */ + continue; + } + if( iCnt==0 ) continue; + if( iCnt>1 && i+2!=iCnt ) continue; + if( zColName==0 ) continue; + if( sqlite3_column_type(pStmt, i)==SQLITE_NULL ){ + const char *zPlus = hasGroupBy ? "+" : ""; + sqlite3_str_appendf(pTest, " %s %s\"%w\" ISNULL", zAnd, zPlus, zColName); + }else{ + sqlite3_str_appendf(pTest, " %s \"%w\"=?%d", zAnd, zColName, + i+1+nParam); + } + zAnd = "AND"; + } + if( pBase!=pStmt ) sqlite3_finalize(pBase); + if( bOrderBy ){ + sqlite3_str_appendf(pTest, " ORDER BY %d", iCnt>2 ? iCnt-1 : 1); + } + return sqlite3_str_finish(pTest); +} + +/* +** Return true if and only if v1 and is the same as v2. +*/ +static int sameValue( + sqlite3_stmt *pS1, int i1, /* Value to text on the left */ + sqlite3_stmt *pS2, int i2, /* Value to test on the right */ + sqlite3_stmt *pTestCompare /* COLLATE comparison statement or NULL */ +){ + int x = 1; + int t1 = sqlite3_column_type(pS1,i1); + int t2 = sqlite3_column_type(pS2,i2); + if( t1!=t2 ){ + if( (t1==SQLITE_INTEGER && t2==SQLITE_FLOAT) + || (t1==SQLITE_FLOAT && t2==SQLITE_INTEGER) + ){ + /* Comparison of numerics is ok */ + }else{ + return 0; + } + } + switch( sqlite3_column_type(pS1,i1) ){ + case SQLITE_INTEGER: { + x = sqlite3_column_int64(pS1,i1)==sqlite3_column_int64(pS2,i2); + break; + } + case SQLITE_FLOAT: { + x = sqlite3_column_double(pS1,i1)==sqlite3_column_double(pS2,i2); + break; + } + case SQLITE_TEXT: { + int e1 = sqlite3_value_encoding(sqlite3_column_value(pS1,i1)); + int e2 = sqlite3_value_encoding(sqlite3_column_value(pS2,i2)); + if( e1!=e2 ){ + const char *z1 = (const char*)sqlite3_column_text(pS1,i1); + const char *z2 = (const char*)sqlite3_column_text(pS2,i2); + x = ((z1==0 && z2==0) || (z1!=0 && z2!=0 && strcmp(z1,z1)==0)); + printf("Encodings differ. %d on left and %d on right\n", e1, e2); + abort(); + } + if( pTestCompare ){ + sqlite3_bind_value(pTestCompare, 1, sqlite3_column_value(pS1,i1)); + sqlite3_bind_value(pTestCompare, 2, sqlite3_column_value(pS2,i2)); + x = sqlite3_step(pTestCompare)==SQLITE_ROW + && sqlite3_column_int(pTestCompare,0)!=0; + sqlite3_reset(pTestCompare); + break; + } + if( e1!=SQLITE_UTF8 ){ + int len1 = sqlite3_column_bytes16(pS1,i1); + const unsigned char *b1 = sqlite3_column_blob(pS1,i1); + int len2 = sqlite3_column_bytes16(pS2,i2); + const unsigned char *b2 = sqlite3_column_blob(pS2,i2); + if( len1!=len2 ){ + x = 0; + }else if( len1==0 ){ + x = 1; + }else{ + x = (b1!=0 && b2!=0 && memcmp(b1,b2,len1)==0); + } + break; + } + /* Fall through into the SQLITE_BLOB case */ + } + case SQLITE_BLOB: { + int len1 = sqlite3_column_bytes(pS1,i1); + const unsigned char *b1 = sqlite3_column_blob(pS1,i1); + int len2 = sqlite3_column_bytes(pS2,i2); + const unsigned char *b2 = sqlite3_column_blob(pS2,i2); + if( len1!=len2 ){ + x = 0; + }else if( len1==0 ){ + x = 1; + }else{ + x = (b1!=0 && b2!=0 && memcmp(b1,b2,len1)==0); + } + break; + } + } + return x; +} + +/* +** Print binary data as hex +*/ +static void printHex(const unsigned char *a, int n, int mx){ + int j; + for(j=0; j<mx && j<n; j++){ + printf("%02x", a[j]); + } + if( j<n ) printf("..."); +} + +/* +** Print a single row from the prepared statement +*/ +static void printRow(sqlite3_stmt *pStmt, int iRow){ + int i, n, nCol; + unsigned const char *data; + nCol = sqlite3_column_count(pStmt); + for(i=0; i<nCol; i++){ + printf("row%d.col%d = ", iRow, i); + switch( sqlite3_column_type(pStmt, i) ){ + case SQLITE_NULL: { + printf("NULL\n"); + break; + } + case SQLITE_INTEGER: { + printf("(integer) %lld\n", sqlite3_column_int64(pStmt, i)); + break; + } + case SQLITE_FLOAT: { + printf("(float) %f\n", sqlite3_column_double(pStmt, i)); + break; + } + case SQLITE_TEXT: { + switch( sqlite3_value_encoding(sqlite3_column_value(pStmt,i)) ){ + case SQLITE_UTF8: { + printf("(utf8) x'"); + n = sqlite3_column_bytes(pStmt, i); + data = sqlite3_column_blob(pStmt, i); + printHex(data, n, 35); + printf("'\n"); + break; + } + case SQLITE_UTF16BE: { + printf("(utf16be) x'"); + n = sqlite3_column_bytes16(pStmt, i); + data = sqlite3_column_blob(pStmt, i); + printHex(data, n, 35); + printf("'\n"); + break; + } + case SQLITE_UTF16LE: { + printf("(utf16le) x'"); + n = sqlite3_column_bytes16(pStmt, i); + data = sqlite3_column_blob(pStmt, i); + printHex(data, n, 35); + printf("'\n"); + break; + } + default: { + printf("Illegal return from sqlite3_value_encoding(): %d\n", + sqlite3_value_encoding(sqlite3_column_value(pStmt,i))); + abort(); + } + } + break; + } + case SQLITE_BLOB: { + n = sqlite3_column_bytes(pStmt, i); + data = sqlite3_column_blob(pStmt, i); + printf("(blob %d bytes) x'", n); + printHex(data, n, 35); + printf("'\n"); + break; + } + } + } +} + +/* +** Report a failure of the invariant: The current output row of pOrig +** does not appear in any row of the output from pTest. +*/ +static void reportInvariantFailed( + sqlite3_stmt *pOrig, /* The original query */ + sqlite3_stmt *pTest, /* The alternative test query with a missing row */ + int iRow, /* Row number in pOrig */ + unsigned int dbOpt, /* Optimization flags on pOrig */ + int noOpt /* True if opt flags inverted for pTest */ +){ + int iTestRow = 0; + printf("Invariant check failed on row %d.\n", iRow); + printf("Original query (opt-flags: 0x%08x) --------------------------\n", + dbOpt); + printf("%s\n", sqlite3_expanded_sql(pOrig)); + printf("Alternative query (opt-flags: 0x%08x) -----------------------\n", + noOpt ? ~dbOpt : dbOpt); + printf("%s\n", sqlite3_expanded_sql(pTest)); + printf("Result row that is missing from the alternative -----------------\n"); + printRow(pOrig, iRow); + printf("Complete results from the alternative query ---------------------\n"); + sqlite3_reset(pTest); + while( sqlite3_step(pTest)==SQLITE_ROW ){ + iTestRow++; + printRow(pTest, iTestRow); + } + sqlite3_finalize(pTest); + abort(); +} diff --git a/test/gcfault.test b/test/gcfault.test new file mode 100644 index 0000000000..2d77f9ef2c --- /dev/null +++ b/test/gcfault.test @@ -0,0 +1,56 @@ +# 2016 December 30 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is testing OOM error handling within the built-in +# group_concat() and string_agg() functions. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix gcfault + + +foreach {enc} { + utf16 + utf8 +} { + reset_db + sqlite3_db_config_lookaside db 0 0 0 + execsql "PRAGMA encoding = $enc" + + do_execsql_test 1.$enc.1 { + CREATE TABLE s(i, s); + INSERT INTO s VALUES(1, ',0123456789,'); + INSERT INTO s VALUES(2, X'2c303132333435363738392c'); + + CREATE TABLE e(e); + INSERT INTO e VALUES('v1'), ('v2'); + } {} + + do_faultsim_test 1.$enc.1 -faults oom* -body { + execsql { SELECT group_concat(e, (SELECT s FROM s WHERE i=1)) FROM e } + } + + do_faultsim_test 1.$enc.2 -faults oom-t* -body { + execsql { SELECT string_agg(e, (SELECT s FROM s WHERE i=2)) FROM e } + } + + do_faultsim_test 1.$enc.3 -faults oom-t* -prep { + set ::STMT [sqlite3_prepare db {SELECT group_concat(e, ?) FROM e} -1 dummy] + sqlite3_bind_text $::STMT 1 ",0123456789," 12 + } -body { + while { "SQLITE_ROW"==[sqlite3_step $::STMT] } { } + } -test { + sqlite3_finalize $::STMT + } +} + +finish_test diff --git a/test/gencol1.test b/test/gencol1.test new file mode 100644 index 0000000000..a247a7148d --- /dev/null +++ b/test/gencol1.test @@ -0,0 +1,669 @@ +# 2019-10-31 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# Test cases for generated columns. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +# ticket 830277d9db6c3ba1 on 2019-10-31 +do_execsql_test gencol1-100 { + CREATE TABLE t0(c0 AS(TYPEOF(c1)), c1); + INSERT INTO t0(c1) VALUES(0); + CREATE TABLE t1(x AS (typeof(y)), y); + INSERT INTO t1 SELECT * FROM t0; + SELECT * FROM t1; +} {integer 0} + +foreach {tn schema} { +1 { + CREATE TABLE t1( + a INT, + b TEXT, + c ANY, + w INT GENERATED ALWAYS AS (a*10), + x TEXT AS (typeof(c)), + y TEXT AS (substr(b,a,a+2)) + ); + } +2 { + CREATE TABLE t1( + w INT GENERATED ALWAYS AS (a*10), + x TEXT AS (typeof(c)), + y TEXT AS (substr(b,a,a+2)), + a INT, + b TEXT, + c ANY + ); + } +3 { + CREATE TABLE t1( + w INT GENERATED ALWAYS AS (a*10), + a INT, + x TEXT AS (typeof(c)) STORED, + b TEXT, + y TEXT AS (substr(b,a,a+2)), + c ANY + ); + } +4 { + CREATE TABLE t1( + a INTEGER PRIMARY KEY, + w INT GENERATED ALWAYS AS (a*10), + b TEXT, + x TEXT AS (typeof(c)), + y TEXT AS (substr(b,a,a+2)) STORED, + c ANY + ); + } +5 { + CREATE TABLE t1( + w INT GENERATED ALWAYS AS (a*10), + a INT, + x TEXT AS (typeof(c)), + b TEXT, + y TEXT AS (substr(b,a,a+2)) STORED, + c ANY, + PRIMARY KEY(a,b) + ) WITHOUT ROWID; + } +6 { + CREATE TABLE t1( + w INT GENERATED ALWAYS AS (m*5), + m INT AS (a*2) STORED, + a INT, + x TEXT AS (typeof(c)), + b TEXT, + y TEXT AS (substr(b,m/2,m/2+2)) STORED, + c ANY, + PRIMARY KEY(a,b) + ); + } +7 { + CREATE TABLE t1( + w INT GENERATED ALWAYS AS (m*5), + m INT AS (a*2) NOT NULL, + a INT, + x TEXT AS (typeof(c)) CHECK (x<>'blank'), + b TEXT, + y TEXT AS (substr(b,m/2,m/2+2)) STORED, + c ANY, + PRIMARY KEY(b,a) + ) WITHOUT ROWID; + } +} { + catch {db close} + sqlite3 db :memory: + db eval $schema + do_execsql_test gencol1-2.$tn.100 { + INSERT INTO t1(a,b,c) VALUES(1,'abcdef',5.5),(3,'cantaloupe',NULL); + SELECT w, x, y, '|' FROM t1 ORDER BY a; + } {10 real abc | 30 null ntalo |} + do_execsql_test gencol1-2.$tn.101 { + SELECT w, x, y, '|' FROM t1 ORDER BY w; + } {10 real abc | 30 null ntalo |} + do_execsql_test gencol1-2.$tn.102 { + SELECT a FROM t1 WHERE w=30; + } {3} + do_execsql_test gencol1-2.$tn.103 { + SELECT a FROM t1 WHERE x='real'; + } {1} + do_execsql_test gencol1-2.$tn.104 { + SELECT a FROM t1 WHERE y LIKE '%tal%' OR x='real' ORDER BY b; + } {1 3} + do_execsql_test gencol1-2.$tn.110 { + CREATE INDEX t1w ON t1(w); + SELECT a FROM t1 WHERE w=10; + } {1} + do_execsql_test gencol1-2.$tn.120 { + CREATE INDEX t1x ON t1(x) WHERE w BETWEEN 20 AND 40; + SELECT a FROM t1 WHERE x='null' AND w BETWEEN 20 AND 40; + } {3} + do_execsql_test gencol1-2.$tn.121 { + SELECT a FROM t1 WHERE x='real'; + } {1} + do_execsql_test gencol1-2.$tn.130 { + VACUUM; + PRAGMA integrity_check; + } {ok} + do_execsql_test gencol1-2.$tn.140 { + UPDATE t1 SET a=a+100 WHERE w<20; + SELECT a, w, '|' FROM t1 ORDER BY w; + } {3 30 | 101 1010 |} + do_execsql_test gencol1-2.$tn.150 { + INSERT INTO t1 VALUES(4,'jambalaya','Chef John'),(15,87719874135,0); + SELECT w, x, y, '|' FROM t1 ORDER BY w; + } {30 null ntalo | 40 text balaya | 150 integer {} | 1010 real {} |} +} + +# 2019-10-31 ticket b9befa4b83a660cc +db close +sqlite3 db :memory: +do_execsql_test gencol1-3.100 { + PRAGMA foreign_keys = true; + CREATE TABLE t0(c0 PRIMARY KEY, c1, c2 AS (c0+c1-c3) REFERENCES t0, c3); + INSERT INTO t0 VALUES (0, 0, 0), (11, 5, 5); + UPDATE t0 SET c1 = c0, c3 = c0; + SELECT *, '|' FROM t0 ORDER BY +c0; +} {0 0 0 0 | 11 11 11 11 |} +do_catchsql_test gencol1-3.110 { + UPDATE t0 SET c1 = c0, c3 = c0+1; +} {1 {FOREIGN KEY constraint failed}} + +# 2019-11-01 ticket c28a01da72f8957c +db close +sqlite3 db :memory: +do_execsql_test gencol1-4.100 { + CREATE TABLE t0 ( + c0, + c1 a UNIQUE AS (1), + c2, + c3 REFERENCES t0(c1) + ); + PRAGMA foreign_keys = true; + INSERT INTO t0(c0,c2,c3) VALUES(0,0,1); +} {} +do_catchsql_test gencol1-4.110 { + REPLACE INTO t0(c0,c2,c3) VALUES(0,0,0),(0,0,0); +} {1 {FOREIGN KEY constraint failed}} + +# 2019-11-01 Problem found while adding new foreign key test cases in TH3. +db close +sqlite3 db :memory: +do_execsql_test gencol1-5.100 { + PRAGMA foreign_keys=ON; + CREATE TABLE t1( + gcb AS (b*1), + a INTEGER PRIMARY KEY, + gcc AS (c+0), + b UNIQUE, + gca AS (1*a+0), + c UNIQUE + ) WITHOUT ROWID; + INSERT INTO t1 VALUES(1,2,3); + INSERT INTO t1 VALUES(4,5,6); + INSERT INTO t1 VALUES(7,8,9); + CREATE TABLE t1a( + gcx AS (x+0) REFERENCES t1(a) ON DELETE CASCADE, + id, + x, + gcid AS (1*id) + ); + INSERT INTO t1a VALUES(1, 1); + INSERT INTO t1a VALUES(2, 4); + INSERT INTO t1a VALUES(3, 7); + DELETE FROM t1 WHERE b=5; + SELECT id,x,'|' FROM t1a ORDER BY id; +} {1 1 | 3 7 |} + +do_catchsql_test gencol1-6.10 { + DROP TABLE IF EXISTS t0; + CREATE TABLE t0(c0 NOT NULL AS(c1), c1); + REPLACE INTO t0(c1) VALUES(NULL); +} {1 {NOT NULL constraint failed: t0.c0}} + +# 2019-11-06 ticket https://sqlite.org/src/info/2399f5986134f79c +# 2019-12-27 ticket https://sqlite.org/src/info/5fbc159eeb092130 +# 2019-12-27 ticket https://sqlite.org/src/info/37823501c68a09f9 +# +# All of the above tickets deal with NOT NULL ON CONFLICT REPLACE +# constraints on tables that have generated columns. +# +reset_db +do_execsql_test gencol1-7.10 { + CREATE TABLE t0 (c0 GENERATED ALWAYS AS (1), c1 UNIQUE, c2 UNIQUE); + INSERT INTO t0(c1) VALUES (1); + SELECT quote(0 = t0.c2 OR t0.c1 BETWEEN t0.c2 AND 1) FROM t0; +} {NULL} +do_execsql_test gencol1-7.11 { + DROP TABLE t0; + CREATE TABLE t0(c0 NOT NULL DEFAULT 'xyz', c1 AS(c0) NOT NULL); + REPLACE INTO t0(c0) VALUES(NULL); + SELECT * FROM t0; +} {xyz xyz} +do_execsql_test gencol1-7.12 { + DROP TABLE t0; + CREATE TABLE t0(c0 NOT NULL DEFAULT 'xyz', c1 AS(c0) STORED NOT NULL); + REPLACE INTO t0(c0) VALUES(NULL); + SELECT * FROM t0; +} {xyz xyz} +do_execsql_test gencol1-7.20 { + CREATE TABLE t1( + a NOT NULL DEFAULT 'aaa', + b AS(c) NOT NULL, + c NOT NULL DEFAULT 'ccc'); + REPLACE INTO t1(a,c) VALUES(NULL,NULL); + SELECT * FROM t1; +} {aaa ccc ccc} +do_execsql_test gencol1-7.21 { + DROP TABLE t1; + CREATE TABLE t1( + a NOT NULL DEFAULT 'aaa', + b AS(c) STORED NOT NULL, + c NOT NULL DEFAULT 'ccc'); + REPLACE INTO t1(a,c) VALUES(NULL,NULL); + SELECT * FROM t1; +} {aaa ccc ccc} +do_execsql_test gencol1-7.30 { + CREATE TABLE t2( + a NOT NULL DEFAULT 'aaa', + b AS(a) NOT NULL, + c NOT NULL DEFAULT 'ccc'); + REPLACE INTO t2(a,c) VALUES(NULL,NULL); + SELECT * FROM t2; +} {aaa aaa ccc} +do_execsql_test gencol1-7.31 { + DROP TABLE t2; + CREATE TABLE t2( + a NOT NULL DEFAULT 'aaa', + b AS(a) STORED NOT NULL, + c NOT NULL DEFAULT 'ccc'); + REPLACE INTO t2(a,c) VALUES(NULL,NULL); + SELECT * FROM t2; +} {aaa aaa ccc} +do_execsql_test gencol1-7.40 { + CREATE TABLE t3(a NOT NULL DEFAULT 123, b AS(a) UNIQUE); + REPLACE INTO t3 VALUES(NULL); + SELECT * FROM t3; +} {123 123} +do_execsql_test gencol1-7.41 { + SELECT * FROM t3 WHERE b=123; +} {123 123} +do_execsql_test gencol1-7.50 { + CREATE TABLE t4(a NOT NULL DEFAULT 123, b AS(a*10+4) STORED UNIQUE); + REPLACE INTO t4 VALUES(NULL); + SELECT * FROM t4; +} {123 1234} +do_execsql_test gencol1-7.51 { + SELECT * FROM t4 WHERE b=1234; +} {123 1234} + +# 2019-11-06 ticket 4fc08501f4e56692 +do_execsql_test gencol1-8.10 { + DROP TABLE IF EXISTS t0; + CREATE TABLE t0( + c0 AS (('a', 9) < ('b', c1)), + c1 AS (1), + c2 CHECK (1 = c1) + ); + INSERT INTO t0 VALUES (0),(99); + SELECT * FROM t0; +} {1 1 0 1 1 99} +do_catchsql_test gencol1-8.20 { + DROP TABLE IF EXISTS t0; + CREATE TABLE t0( + c0, + c1 AS(c0 + c2), + c2 AS(c1) CHECK(c2) + ); + UPDATE t0 SET c0 = NULL; +} {1 {generated column loop on "c2"}} + +# 2019-11-21 Problems in the new generated column logic +# reported by Yongheng Chen and Rui Zhong +reset_db +do_execsql_test gencol1-9.10 { + PRAGMA foreign_keys=OFF; + CREATE TABLE t1(aa , bb AS (17) UNIQUE); + INSERT INTO t1 VALUES(17); + CREATE TABLE t2(cc); + INSERT INTO t2 VALUES(41); + SELECT * FROM t2 JOIN t1 WHERE t1.bb=t1.aa AND t1.bb=17; +} {41 17 17} +do_execsql_test gencol1-9.20 { + CREATE TABLE t3(aa INT PRIMARY KEY, bb UNIQUE AS(aa)); + INSERT INTO t3 VALUES(1); + SELECT 100, * FROM t3; + DELETE FROM t3 WHERE (SELECT bb FROM t3); + SELECT 200, * FROM t3; +} {100 1 1} + +# 2019-12-04 Generated column in a CREATE TABLE IF NOT EXISTS that +# does already exist. +# +sqlite3 db :memory: +do_execsql_test gencol1-10.10 { + CREATE TABLE t1(aa,bb); + CREATE TABLE IF NOT EXISTS t1(aa, bb AS (aa+1)); + PRAGMA integrity_check; +} {ok} + +# 2019-12-06 Found by mrigger +# +sqlite3 db :memory: +do_execsql_test gencol1-11.10 { + PRAGMA foreign_keys = true; + CREATE TABLE t0( + c0, + c1 INTEGER PRIMARY KEY, + c2 BLOB UNIQUE DEFAULT x'00', + c3 BLOB GENERATED ALWAYS AS (1), + FOREIGN KEY(c1) REFERENCES t0(c2) + ); +} +do_catchsql_test gencol1-11.20 { + INSERT OR REPLACE INTO t0(c0, c1) VALUES (2, 1), (1, 0) +} {1 {FOREIGN KEY constraint failed}} +do_execsql_test gencol1-11.30 { + DROP TABLE t0; + CREATE TABLE t0( + c0, + c1 INTEGER PRIMARY KEY, + c3 BLOB GENERATED ALWAYS AS (1), + c2 BLOB UNIQUE DEFAULT x'00', + FOREIGN KEY(c1) REFERENCES t0(c2) + ); +} +do_catchsql_test gencol1-11.40 { + INSERT OR REPLACE INTO t0(c0, c1) VALUES (2, 1), (1, 0) +} {1 {FOREIGN KEY constraint failed}} +do_execsql_test gencol1-11.50 { + DROP TABLE t0; + CREATE TABLE t0( + c0, + c3 BLOB GENERATED ALWAYS AS (1), + c1 INTEGER PRIMARY KEY, + c2 BLOB UNIQUE DEFAULT x'00', + FOREIGN KEY(c1) REFERENCES t0(c2) + ); +} +do_catchsql_test gencol1-11.60 { + INSERT OR REPLACE INTO t0(c0, c1) VALUES (2, 1), (1, 0) +} {1 {FOREIGN KEY constraint failed}} +do_execsql_test gencol1-11.70 { + DROP TABLE t0; + CREATE TABLE t0( + c3 BLOB GENERATED ALWAYS AS (1), + c0, + c1 INTEGER PRIMARY KEY, + c2 BLOB UNIQUE DEFAULT x'00', + FOREIGN KEY(c1) REFERENCES t0(c2) + ); +} +do_catchsql_test gencol1-11.80 { + INSERT OR REPLACE INTO t0(c0, c1) VALUES (2, 1), (1, 0) +} {1 {FOREIGN KEY constraint failed}} + +# 2019-12-09 ticket bd8c280671ba44a7 +# With generated columns, the sqlite3ExprGetColumnOfTable() routine might +# generate a code sequence that does not end with OP_Column. So check to +# make sure that the last instruction generated is an OP_column prior to +# applying the OPFLAG_TYPEOFARG optimization to NOT NULL checks in the +# PRAGMA integrity_check code. +# +sqlite3 db :memory: +do_execsql_test gencol1-12.10 { + CREATE TABLE t0 (c0, c1 NOT NULL AS (c0==0)); + INSERT INTO t0(c0) VALUES (0); + PRAGMA integrity_check; +} {ok} + +# 2019-12-09 bug report from Yongheng Chen +# Ensure that the SrcList_item.colUsed field is set correctly when a +# generated column appears in the USING clause of a join. +# +do_execsql_test gencol1-13.10 { + CREATE TABLE t1(x, y AS(x+1)); + INSERT INTO t1 VALUES(10); + SELECT y FROM t1 JOIN t1 USING (y,y); +} {11} +do_execsql_test gencol1-13.11 { + SELECT 123 FROM t1 JOIN t1 USING (x); +} {123} +do_execsql_test gencol1-13.11 { + SELECT 456 FROM t1 JOIN t1 USING (x,x); +} {456} +do_execsql_test gencol1-13.20 { + CREATE INDEX t1y ON t1(y); + SELECT y FROM t1 JOIN t1 USING (y,y); +} {11} +do_execsql_test gencol1-13.21 { + CREATE INDEX t1x ON t1(x); + SELECT 123 FROM t1 JOIN t1 USING (x); +} {123} +do_execsql_test gencol1-13.22 { + SELECT 456 FROM t1 JOIN t1 USING (x,x); +} {456} + +# 2019-12-14 ticket b439bfcfb7deedc6 +# +sqlite3 db :memory: +do_execsql_test gencol1-14.10 { + CREATE TABLE t0(c0 AS(1 >= 1), c1 UNIQUE AS(TYPEOF(c0)), c2); + INSERT INTO t0 VALUES(0); + REINDEX; + SELECT * FROM t0; +} {1 integer 0} +do_catchsql_test gencol1-14.10 { + INSERT INTO t0 VALUES(2); +} {1 {UNIQUE constraint failed: t0.c1}} + +# 2019-12-14 gramfuzz1 find +# The schema is malformed in that it has a subquery on a generated +# column expression. This will be loaded if writable_schema=ON. SQLite +# must not use such an expression during code generation as the code generator +# will add bits of content to the expression tree that might be allocated +# from lookaside. But the schema is not tied to a particular database +# connection, so the use of lookaside memory is prohibited. The fix +# is to change the generated column expression to NULL before adding it +# to the schema. +# +reset_db +do_test gencol1-15.10 { + sqlite3 db {} + db deserialize [decode_hexdb { +| size 8192 pagesize 4096 filename c27.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 01 00 00 00 02 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 ................ +| 96: 00 2e 3f d8 0d 00 00 00 01 0f ba 00 0f ba 00 00 ..?............. +| 4016: 00 00 00 00 00 00 00 00 00 00 44 01 06 17 11 11 ..........D..... +| 4032: 01 75 74 61 62 6c 65 74 31 74 31 02 43 52 45 41 .utablet1t1.CREA +| 4048: 54 45 20 54 41 42 4c 45 20 74 31 28 61 20 49 4e TE TABLE t1(a IN +| 4064: 54 2c 20 62 20 41 53 28 28 56 41 4c 55 45 53 28 T, b AS((VALUES( +| 4080: 31 29 29 20 49 53 20 75 6e 6b 6e 6f 77 6e 29 29 1)) IS unknown)) +| page 2 offset 4096 +| 0: 0d 00 00 00 00 10 00 00 00 00 00 00 00 00 00 00 ................ +| end c27.db +}]} {} +do_execsql_test gencol1-15.20 { + PRAGMA writable_schema=ON; + REPLACE INTO t1 VALUES(9); + SELECT a, quote(b) FROM t1 +} {9 NULL} + +# 2019-12-16 ticket 3b84b42943644d6f +# When a table is the right table of a LEFT JOIN and the ON clause is +# false, make sure any generated columns evaluate to NULL. +reset_db +do_execsql_test gencol1-16.10 { + CREATE TABLE t0(c0); + CREATE TABLE t1(c1, c2 AS(1)); + INSERT INTO t0 VALUES(0); + SELECT c0, c1, c2 FROM t0 LEFT JOIN t1; +} {0 {} {}} +do_execsql_test gencol1-16.20 { + DROP TABLE t1; + CREATE TABLE t1(c1, c2 AS (c1 ISNULL)); + SELECT c0, c1, c2 FROM t0 LEFT JOIN t1; +} {0 {} {}} +do_execsql_test gencol1-16.30 { + INSERT INTO t1(c1) VALUES(1),(NULL); + SELECT * FROM t1; +} {1 0 {} 1} +do_execsql_test gencol1-16.40 { + SELECT c0, c1, c2 FROM t0 LEFT JOIN t1 ON c0=c1; +} {0 {} {}} + +# 2019-12-20 ticket e0a8120553f4b082 +# Generated columns with REAL affinity need to have an OP_RealAffinity +# opcode applied, even when the column value is extracted from an index. +# +reset_db +do_execsql_test gencol1-17.10 { + CREATE TABLE t0(c0 REAL AS(1) UNIQUE, c1 INT); + INSERT INTO t0 VALUES(''); + SELECT quote(c0), quote(c1) from t0; +} {1.0 ''} +do_execsql_test gencol1-17.20 { + SELECT *, (1 BETWEEN CAST(t0.c0 AS TEXT) AND t0.c0) FROM t0; +} {1.0 {} 0} +do_execsql_test gencol1-17.30 { + SELECT * FROM t0 WHERE (1 BETWEEN CAST(t0.c0 AS TEXT) AND t0.c0); +} {} +do_execsql_test gencol1-17.40 { + CREATE TABLE t1(a TEXT AS(b) COLLATE nocase, b TEXT, c INT, d DEFAULT 1); + INSERT INTO t1(b,c) VALUES('abc',11),('DEF',22),('ghi',33); + SELECT a FROM t1 WHERE b='DEF' AND a='def'; +} {DEF} +do_execsql_test gencol1-17.50 { + CREATE INDEX t1bca ON t1(b,c,a); + SELECT a FROM t1 WHERE b='DEF' AND a='def'; +} {DEF} + +# 2019-12-26 ticket ec8abb025e78f40c +# An index on a virtual column with a constant value (why would anybody +# ever do such a thing?) can cause problems for a one-pass DELETE. +# +reset_db +do_execsql_test gencol1-18.10 { + CREATE TABLE t0(c0 UNIQUE AS(0), c1, c2); + INSERT INTO t0(c1) VALUES(0); + SELECT * FROM t0; +} {0 0 {}} +do_execsql_test gencol1-18.20 { + UPDATE t0 SET c1=0, c2=0 WHERE c0>=0; + SELECT * FROM t0; +} {0 0 0} + +# 2019-12-27 ticket de4b04149b9fdeae +# +reset_db +do_catchsql_test gencol1-19.10 { + CREATE TABLE t0( + c0 INT AS(2) UNIQUE, + c1 TEXT UNIQUE, + FOREIGN KEY(c0) REFERENCES t0(c1) + ); + INSERT INTO t0(c1) VALUES(0.16334143182538696), (0); +} {1 {UNIQUE constraint failed: t0.c0}} + +# 2020-06-29 forum bug report. +# https://sqlite.org/forum/forumpost/73b9a8ccfb +# +do_execsql_test gencol1-20.1 { + CREATE TEMPORARY TABLE tab ( + prim DATE PRIMARY KEY, + a INTEGER, + comp INTEGER AS (a), + b INTEGER, + x INTEGER + ); + -- Add some data + INSERT INTO tab (prim, a, b) VALUES ('2001-01-01', 0, 0); + -- Check that each column is 0 like I expect + SELECT * FROM tab; +} {2001-01-01 0 0 0 {}} +do_execsql_test gencol1-20.2 { + -- Do an UPSERT on the b column + INSERT INTO tab (prim, b) + VALUES ('2001-01-01',5) + ON CONFLICT(prim) DO UPDATE SET b=excluded.b; + -- Now b is NULL rather than 5 + SELECT * FROM tab; +} {2001-01-01 0 0 5 {}} + +# 2021-07-30 forum https://sqlite.org/forum/forumpost/ff3ffe09251c105b?t=h +# +ifcapable vtab { +reset_db + do_execsql_test gencol1-21.1 { + CREATE TABLE t1( + a integer primary key, + b int generated always as (a+5), + c text GENERATED ALWAYS as (printf('%08x',a)), + d Generated + Always + AS ('xyzzy'), + e int Always default(5) + ); + INSERT INTO t1(a) VALUES(5); + SELECT name, type FROM pragma_table_xinfo('t1'); + } {a INTEGER b INT c TEXT d {} e INT} +} + +# 2021-09-07 forum https://sqlite.org/forum/forumpost/699b44b3ee +# +reset_db +do_execsql_test gencol1-22.1 { + CREATE TABLE t0(a PRIMARY KEY,b TEXT AS ('2') UNIQUE); + INSERT INTO t0(a) VALUES(2); + SELECT * FROM t0 AS x JOIN t0 AS y + WHERE x.b='2' + AND (y.a=2 OR (x.b LIKE '2*' AND y.a=x.b)); +} {2 2 2 2} + + +# 2023-03-02 dbsqlfuzz 65f5eb57f8859344d5f1f33e08c77ee12960ed83 +# +set typelist {ANY INT REAL BLOB TEXT {}} +set cnt 0 +foreach t1 $typelist { + foreach t2 $typelist { + incr cnt + db eval " + DROP TABLE IF EXISTS t1; + CREATE TABLE t1( + x $t1, + a $t2 AS (x) VIRTUAL, + b BLOB AS (x) VIRTUAL + ); + CREATE INDEX x2 ON t1(a); + INSERT INTO t1(x) VALUES(NULL),('1'),(2),(3.5),('xyz'); + " + set x1 [lsort [db eval {SELECT typeof(b) FROM t1}]] + do_test gencol1-23.1.$cnt { + lsort [db eval {SELECT typeof(b) FROM t1 INDEXED BY x2}] + } $x1 + } +} +do_execsql_test gencol1-23.2 { + DROP TABLE t1; + CREATE TABLE t1( + x, + a INT AS (x) VIRTUAL, + b BLOB AS (x) VIRTUAL + ); + CREATE INDEX x2 ON t1(a); + INSERT INTO t1(x) VALUES(NULL),('1'),('xyz'),(2),(3.5); + SELECT quote(a) FROM t1 INDEXED BY x2; +} {NULL 1 2 3.5 'xyz'} +do_execsql_test gencol1-23.3 { + EXPLAIN SELECT a FROM t1 INDEXED BY x2; +} {~/Column 0/} +# ^^^^^^^^---- verfies that x2 acts like a covering index +do_execsql_test gencol1-23.4 { + EXPLAIN SELECT b FROM t1 INDEXED BY x2; +} {/Column 0/} +# ^^^^^^^^^^--- Must reference the original table in this case because +# of the different datatype on column b. + +# 2023-03-07 https://sqlite.org/forum/forumpost/b312e075b5 +# +do_catchsql_test gencol1-23.5 { + CREATE TABLE v0(c1 INT, c2 AS (RAISE(IGNORE))); +} {1 {RAISE() may only be used within a trigger-program}} + +finish_test diff --git a/test/having.test b/test/having.test new file mode 100644 index 0000000000..3bfa8120a1 --- /dev/null +++ b/test/having.test @@ -0,0 +1,191 @@ +# 2017 April 30 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# Test the HAVING->WHERE optimization. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix having + +do_execsql_test 1.0 { + CREATE TABLE t2(c, d); + + CREATE TABLE t1(a, b); + INSERT INTO t1 VALUES(1, 1); + INSERT INTO t1 VALUES(2, 2); + INSERT INTO t1 VALUES(1, 3); + INSERT INTO t1 VALUES(2, 4); + INSERT INTO t1 VALUES(1, 5); + INSERT INTO t1 VALUES(2, 6); +} {} + +foreach {tn sql res} { + 1 "SELECT a, sum(b) FROM t1 GROUP BY a HAVING a=2" {2 12} + 2 "SELECT a, sum(b) FROM t1 GROUP BY a HAVING a=2 AND sum(b)>10" {2 12} + 3 "SELECT a, sum(b) FROM t1 GROUP BY a HAVING sum(b)>12" {} +} { + do_execsql_test 1.$tn $sql $res +} + +# Run an EXPLAIN command for both SQL statements. Return true if +# the outputs are identical, or false otherwise. +# +proc compare_vdbe {sql1 sql2} { + set r1 [list] + set r2 [list] + db eval "explain $sql1" { lappend r1 $opcode $p1 $p2 $p3 $p4 $p5} + db eval "explain $sql2" { lappend r2 $opcode $p1 $p2 $p3 $p4 $p5} + return [expr {$r1==$r2}] +} + +proc do_compare_vdbe_test {tn sql1 sql2 res} { + uplevel [list do_test $tn [list compare_vdbe $sql1 $sql2] $res] +} + +#------------------------------------------------------------------------- +# Test that various statements that are eligible for the optimization +# produce the same VDBE code as optimizing by hand does. +# +foreach {tn sql1 sql2} { + 1 "SELECT a, sum(b) FROM t1 GROUP BY a HAVING a=2" + "SELECT a, sum(b) FROM t1 WHERE a=2 GROUP BY a" + + 2 "SELECT a, sum(b) FROM t1 GROUP BY a HAVING sum(b)>5 AND a=2" + "SELECT a, sum(b) FROM t1 WHERE a=2 GROUP BY a HAVING sum(b)>5" + + 3 "SELECT a, sum(b) FROM t1 GROUP BY a COLLATE binary HAVING a=2" + "SELECT a, sum(b) FROM t1 WHERE a=2 GROUP BY a COLLATE binary" + + 5 "SELECT a, sum(b) FROM t1 GROUP BY a COLLATE binary HAVING 1" + "SELECT a, sum(b) FROM t1 WHERE 1 GROUP BY a COLLATE binary" + + 6 "SELECT count(*) FROM t1,t2 WHERE a=c GROUP BY b, d HAVING b=d" + "SELECT count(*) FROM t1,t2 WHERE a=c AND b=d GROUP BY b, d" + + 7 { + SELECT count(*) FROM t1,t2 WHERE a=c GROUP BY b, d + HAVING b=d COLLATE nocase + } { + SELECT count(*) FROM t1,t2 WHERE a=c AND b=d COLLATE nocase + GROUP BY b, d + } + + 8 "SELECT a, sum(b) FROM t1 GROUP BY a||b HAVING substr(a||b, 1, 1)='a'" + "SELECT a, sum(b) FROM t1 WHERE substr(a||b, 1, 1)='a' GROUP BY a||b" +} { + do_compare_vdbe_test 2.$tn $sql1 $sql2 1 +} + +# The (4) test in the above set used to generate identical bytecode, but +# that is no longer the case. The byte code is equivalent, though. +# +do_execsql_test 2.4a { + SELECT x,y FROM ( + SELECT a AS x, sum(b) AS y FROM t1 + GROUP BY a + ) WHERE x BETWEEN 2 AND 9999 +} {2 12} +do_execsql_test 2.4b { + SELECT x,y FROM ( + SELECT a AS x, sum(b) AS y FROM t1 + WHERE x BETWEEN 2 AND 9999 + GROUP BY a + ) +} {2 12} + + +#------------------------------------------------------------------------- +# 1: Test that the optimization is only applied if the GROUP BY term +# uses BINARY collation. +# +# 2: Not applied if there is a non-deterministic function in the HAVING +# term. +# +foreach {tn sql1 sql2} { + 1 "SELECT a, sum(b) FROM t1 GROUP BY a COLLATE nocase HAVING a=2" + "SELECT a, sum(b) FROM t1 WHERE a=2 GROUP BY a COLLATE nocase" + + 2 "SELECT a, sum(b) FROM t1 GROUP BY a HAVING randomblob(a)<X'88'" + "SELECT a, sum(b) FROM t1 WHERE randomblob(a)<X'88' GROUP BY a" +} { + do_compare_vdbe_test 3.$tn $sql1 $sql2 0 +} + + +#------------------------------------------------------------------------- +# Test that non-deterministic functions disqualify a term from being +# moved from the HAVING to WHERE clause. +# +do_execsql_test 4.1 { + CREATE TABLE t3(a, b); + INSERT INTO t3 VALUES(1, 1); + INSERT INTO t3 VALUES(1, 2); + INSERT INTO t3 VALUES(1, 3); + INSERT INTO t3 VALUES(2, 1); + INSERT INTO t3 VALUES(2, 2); + INSERT INTO t3 VALUES(2, 3); +} + +proc nondeter {args} { + incr ::nondeter_ret + expr {$::nondeter_ret % 2} +} +db func nondeter nondeter + +set ::nondeter_ret 0 +do_execsql_test 4.2 { + SELECT a, sum(b) FROM t3 GROUP BY a HAVING nondeter(a) +} {1 6} + +# If the term where moved, the query above would return the same +# result as the following. But it does not. +# +set ::nondeter_ret 0 +do_execsql_test 4.3 { + SELECT a, sum(b) FROM t3 WHERE nondeter(a) GROUP BY a +} {1 4 2 2} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 5.0 { + CREATE TABLE t1(a, b); + CREATE TABLE t2(x, y); + INSERT INTO t1 VALUES('a', 'b'); +} + +# The WHERE clause (a=2), uses an aggregate column from the outer query. +# If the HAVING term (0) is moved into the WHERE clause in this case, +# SQLite would at one point optimize (a=2 AND 0) to simply (0). Which +# is logically correct, but happened to cause problems in aggregate +# processing for the outer query. This test case verifies that those +# problems are no longer present. +do_execsql_test 5.1 { + SELECT min(b), ( + SELECT x FROM t2 WHERE a=2 GROUP BY y HAVING 0 + ) FROM t1; +} {b {}} + +# From chromium +# https://bugs.chromium.org/p/chromium/issues/detail?id=1161869 +# +do_execsql_test 5.2 { + SELECT EXISTS ( + SELECT * FROM ( + SELECT * FROM ( + SELECT 1 + ) WHERE Col0 = 1 GROUP BY 1 + ) WHERE 0 + ) + FROM (SELECT 1 Col0) GROUP BY 1 +} {0} + +finish_test diff --git a/test/hexlit.test b/test/hexlit.test index c48930b49b..2161f27a12 100644 --- a/test/hexlit.test +++ b/test/hexlit.test @@ -112,6 +112,9 @@ do_catchsql_test hexlist-400 { do_catchsql_test hexlist-401 { SELECT DISTINCT 0x10000000000000000; } {1 {hex literal too big: 0x10000000000000000}} +do_catchsql_test hexlist-402 { + SELECT DISTINCT -0x08000000000000000; +} {1 {hex literal too big: -0x08000000000000000}} do_catchsql_test hexlist-410 { DROP TABLE IF EXISTS t1; CREATE TABLE t1(x); diff --git a/test/hook.test b/test/hook.test index de6fbdd254..a4256732e1 100644 --- a/test/hook.test +++ b/test/hook.test @@ -16,11 +16,13 @@ # sqlite_commit_hook (tests hook-1..hook-3 inclusive) # sqlite_update_hook (tests hook-4-*) # sqlite_rollback_hook (tests hook-5.*) +# sqlite_preupdate_hook (tests hook-7..hook-12) # # $Id: hook.test,v 1.15 2009/04/07 14:14:23 danielk1977 Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl +set ::testprefix hook do_test hook-1.2 { db commit_hook @@ -141,9 +143,8 @@ do_test hook-4.1.1a { set ::update_hook {} db update_hook [list lappend ::update_hook] # - # EVIDENCE-OF: R-52223-27275 The update hook is not invoked when - # internal system tables are modified (i.e. sqlite_master and - # sqlite_sequence). + # EVIDENCE-OF: R-24531-54682 The update hook is not invoked when + # internal system tables are modified (i.e. sqlite_sequence). # execsql { CREATE TABLE t1(a INTEGER PRIMARY KEY, b); @@ -438,4 +439,632 @@ do_test hook-6.2 { } {COMMIT ROLLBACK} unset ::hooks +#---------------------------------------------------------------------------- +# The following tests - hook-7.* - test the pre-update hook. +# +ifcapable !preupdate { + finish_test + return +} +# +# 7.1.1 - INSERT statement. +# 7.1.2 - INSERT INTO ... SELECT statement. +# 7.1.3 - REPLACE INTO ... (rowid conflict) +# 7.1.4 - REPLACE INTO ... (other index conflicts) +# 7.1.5 - REPLACE INTO ... (both rowid and other index conflicts) +# +# 7.2.1 - DELETE statement. +# 7.2.2 - DELETE statement that uses the truncate optimization. +# +# 7.3.1 - UPDATE statement. +# 7.3.2 - UPDATE statement that modifies the rowid. +# 7.3.3 - UPDATE OR REPLACE ... (rowid conflict). +# 7.3.4 - UPDATE OR REPLACE ... (other index conflicts) +# 7.3.4 - UPDATE OR REPLACE ... (both rowid and other index conflicts) +# +# 7.4.1 - Test that the pre-update-hook is invoked only once if a row being +# deleted is removed by a BEFORE trigger. +# +# 7.4.2 - Test that the pre-update-hook is invoked if a BEFORE trigger +# removes a row being updated. In this case the update hook should +# be invoked with SQLITE_INSERT as the opcode when inserting the +# new version of the row. +# +# TODO: Short records (those created before a column is added to a table +# using ALTER TABLE) +# + +proc do_preupdate_test {tn sql x} { + set X [list] + foreach elem $x {lappend X $elem} + uplevel do_test $tn [list " + set ::preupdate \[list\] + execsql { $sql } + set ::preupdate + "] [list $X] +} + +proc preupdate_hook {args} { + set type [lindex $args 0] + eval lappend ::preupdate $args + if {$type != "INSERT"} { + set x [catch {db preupdate old [db preupdate count]}] + if {!$x} { + lappend "ERROR: sqlite3_preupdate_old() accepted an out-of-bounds\ + column index" + } + for {set i 0} {$i < [db preupdate count]} {incr i} { + lappend ::preupdate [db preupdate old $i] + } + } + if {$type != "DELETE"} { + set x [catch {db preupdate new [db preupdate count]}] + if {!$x} { + lappend "ERROR: sqlite3_preupdate_old() accepted an out-of-bounds\ + column index" + } + for {set i 0} {$i < [db preupdate count]} {incr i} { + set rc [catch { db preupdate new $i } v] + lappend ::preupdate $v + } + } +} + +db close +forcedelete test.db +sqlite3 db test.db +db preupdate hook preupdate_hook + +# Set up a schema to use for tests 7.1.* to 7.3.*. +do_execsql_test 7.0 { + CREATE TABLE t1(a, b); + CREATE TABLE t2(x, y); + CREATE TABLE t3(i, j, UNIQUE(i)); + + INSERT INTO t2 VALUES('a', 'b'); + INSERT INTO t2 VALUES('c', 'd'); + + INSERT INTO t3 VALUES(4, 16); + INSERT INTO t3 VALUES(5, 25); + INSERT INTO t3 VALUES(6, 36); +} + +do_preupdate_test 7.1.1 { + INSERT INTO t1 VALUES('x', 'y') +} {INSERT main t1 1 1 x y} + +# 7.1.2.1 does not use the xfer optimization. 7.1.2.2 does. +do_preupdate_test 7.1.2.1 { + INSERT INTO t1 SELECT y, x FROM t2; +} {INSERT main t1 2 2 b a INSERT main t1 3 3 d c} +do_preupdate_test 7.1.2.2 { + INSERT INTO t1 SELECT * FROM t2; +} {INSERT main t1 4 4 a b INSERT main t1 5 5 c d} + +do_preupdate_test 7.1.3 { + REPLACE INTO t1(rowid, a, b) VALUES(1, 1, 1); +} { + DELETE main t1 1 1 x y + INSERT main t1 1 1 1 1 +} + +do_preupdate_test 7.1.4 { + REPLACE INTO t3 VALUES(4, NULL); +} { + DELETE main t3 1 1 4 16 + INSERT main t3 4 4 4 {} +} + +do_preupdate_test 7.1.5 { + REPLACE INTO t3(rowid, i, j) VALUES(2, 6, NULL); +} { + DELETE main t3 2 2 5 25 + DELETE main t3 3 3 6 36 + INSERT main t3 2 2 6 {} +} + +do_execsql_test 7.2.0 { SELECT rowid FROM t1 } {1 2 3 4 5} + +do_preupdate_test 7.2.1 { + DELETE FROM t1 WHERE rowid = 3 +} { + DELETE main t1 3 3 d c +} +do_preupdate_test 7.2.2 { + DELETE FROM t1 +} { + DELETE main t1 1 1 1 1 + DELETE main t1 2 2 b a + DELETE main t1 4 4 a b + DELETE main t1 5 5 c d +} + +do_execsql_test 7.3.0 { + DELETE FROM t1; + DELETE FROM t2; + DELETE FROM t3; + + INSERT INTO t2 VALUES('a', 'b'); + INSERT INTO t2 VALUES('c', 'd'); + + INSERT INTO t3 VALUES(4, 16); + INSERT INTO t3 VALUES(5, 25); + INSERT INTO t3 VALUES(6, 36); +} + +do_preupdate_test 7.3.1 { + UPDATE t2 SET y = y||y; +} { + UPDATE main t2 1 1 a b a bb + UPDATE main t2 2 2 c d c dd +} + +do_preupdate_test 7.3.2 { + UPDATE t2 SET rowid = rowid-1; +} { + UPDATE main t2 1 0 a bb a bb + UPDATE main t2 2 1 c dd c dd +} + +do_preupdate_test 7.3.3 { + UPDATE OR REPLACE t2 SET rowid = 1 WHERE x = 'a' +} { + DELETE main t2 1 1 c dd + UPDATE main t2 0 1 a bb a bb +} + +do_preupdate_test 7.3.4.1 { + UPDATE OR REPLACE t3 SET i = 5 WHERE i = 6 +} { + DELETE main t3 2 2 5 25 + UPDATE main t3 3 3 6 36 5 36 +} + +do_execsql_test 7.3.4.2 { + INSERT INTO t3 VALUES(10, 100); + SELECT rowid, * FROM t3; +} {1 4 16 3 5 36 4 10 100} + +do_preupdate_test 7.3.5 { + UPDATE OR REPLACE t3 SET rowid = 1, i = 5 WHERE j = 100; +} { + DELETE main t3 1 1 4 16 + DELETE main t3 3 3 5 36 + UPDATE main t3 4 1 10 100 5 100 +} + +do_execsql_test 7.4.1.0 { + CREATE TABLE t4(a, b); + INSERT INTO t4 VALUES('a', 1); + INSERT INTO t4 VALUES('b', 2); + INSERT INTO t4 VALUES('c', 3); + + CREATE TRIGGER t4t BEFORE DELETE ON t4 BEGIN + DELETE FROM t4 WHERE b = 1; + END; +} + +do_preupdate_test 7.4.1.1 { + DELETE FROM t4 WHERE b = 3 +} { + DELETE main t4 1 1 a 1 + DELETE main t4 3 3 c 3 +} + +do_execsql_test 7.4.1.2 { + INSERT INTO t4(rowid, a, b) VALUES(1, 'a', 1); + INSERT INTO t4(rowid, a, b) VALUES(3, 'c', 3); +} +do_preupdate_test 7.4.1.3 { + DELETE FROM t4 WHERE b = 1 +} { + DELETE main t4 1 1 a 1 +} + +do_execsql_test 7.4.2.0 { + CREATE TABLE t5(a, b); + INSERT INTO t5 VALUES('a', 1); + INSERT INTO t5 VALUES('b', 2); + INSERT INTO t5 VALUES('c', 3); + + CREATE TRIGGER t5t BEFORE UPDATE ON t5 BEGIN + DELETE FROM t5 WHERE b = 1; + END; +} +do_preupdate_test 7.4.2.1 { + UPDATE t5 SET b = 4 WHERE a = 'c' +} { + DELETE main t5 1 1 a 1 + UPDATE main t5 3 3 c 3 c 4 +} + +do_execsql_test 7.4.2.2 { + INSERT INTO t5(rowid, a, b) VALUES(1, 'a', 1); +} + +do_preupdate_test 7.4.2.3 { + UPDATE t5 SET b = 5 WHERE a = 'a' +} { + DELETE main t5 1 1 a 1 +} + +ifcapable altertable { + do_execsql_test 7.5.1.0 { + CREATE TABLE t7(a, b); + INSERT INTO t7 VALUES('one', 'two'); + INSERT INTO t7 VALUES('three', 'four'); + ALTER TABLE t7 ADD COLUMN c DEFAULT NULL; + } + + do_preupdate_test 7.5.1.1 { + DELETE FROM t7 WHERE a = 'one' + } { + DELETE main t7 1 1 one two {} + } + + do_preupdate_test 7.5.1.2 { + UPDATE t7 SET b = 'five' + } { + UPDATE main t7 2 2 three four {} three five {} + } + + do_execsql_test 7.5.2.0 { + CREATE TABLE t8(a, b); + INSERT INTO t8 VALUES('one', 'two'); + INSERT INTO t8 VALUES('three', 'four'); + ALTER TABLE t8 ADD COLUMN c DEFAULT 'xxx'; + } +} + +if 1 { + # At time of writing, these two are broken. They demonstrate that the + # sqlite3_preupdate_old() method does not handle the case where ALTER TABLE + # has been used to add a column with a default value other than NULL. + # + # 2024-09-18: These are now fixed. + # + do_preupdate_test 7.5.2.1 { + DELETE FROM t8 WHERE a = 'one' + } { + DELETE main t8 1 1 one two xxx + } + do_preupdate_test 7.5.2.2 { + UPDATE t8 SET b = 'five' + } { + UPDATE main t8 2 2 three four xxx three five xxx + } +} + +# This block of tests verifies that IPK values are correctly reported +# by the sqlite3_preupdate_old() and sqlite3_preupdate_new() functions. +# +do_execsql_test 7.6.1 { CREATE TABLE t9(a, b INTEGER PRIMARY KEY, c) } +do_preupdate_test 7.6.2 { + INSERT INTO t9 VALUES(1, 2, 3); + UPDATE t9 SET b = b+1, c = c+1; + DELETE FROM t9 WHERE a = 1; +} { + INSERT main t9 2 2 1 2 3 + UPDATE main t9 2 3 1 2 3 1 3 4 + DELETE main t9 3 3 1 3 4 +} + +#-------------------------------------------------------------------------- +# Test that the sqlite3_preupdate_depth() API seems to work. +# +proc preupdate_hook {args} { + set type [lindex $args 0] + eval lappend ::preupdate $args + eval lappend ::preupdate [db preupdate depth] + + if {$type != "INSERT"} { + for {set i 0} {$i < [db preupdate count]} {incr i} { + lappend ::preupdate [db preupdate old $i] + } + } + if {$type != "DELETE"} { + for {set i 0} {$i < [db preupdate count]} {incr i} { + set rc [catch { db preupdate new $i } v] + lappend ::preupdate $v + } + } +} + +db close +forcedelete test.db +sqlite3 db test.db +db preupdate hook preupdate_hook + +do_execsql_test 7.6.1 { + CREATE TABLE t1(x PRIMARY KEY); + CREATE TABLE t2(x PRIMARY KEY); + CREATE TABLE t3(x PRIMARY KEY); + CREATE TABLE t4(x PRIMARY KEY); + + CREATE TRIGGER a AFTER INSERT ON t1 BEGIN INSERT INTO t2 VALUES(new.x); END; + CREATE TRIGGER b AFTER INSERT ON t2 BEGIN INSERT INTO t3 VALUES(new.x); END; + CREATE TRIGGER c AFTER INSERT ON t3 BEGIN INSERT INTO t4 VALUES(new.x); END; + + CREATE TRIGGER d AFTER UPDATE ON t1 BEGIN UPDATE t2 SET x = new.x; END; + CREATE TRIGGER e AFTER UPDATE ON t2 BEGIN UPDATE t3 SET x = new.x; END; + CREATE TRIGGER f AFTER UPDATE ON t3 BEGIN UPDATE t4 SET x = new.x; END; + + CREATE TRIGGER g AFTER DELETE ON t1 BEGIN DELETE FROM t2 WHERE 1; END; + CREATE TRIGGER h AFTER DELETE ON t2 BEGIN DELETE FROM t3 WHERE 1; END; + CREATE TRIGGER i AFTER DELETE ON t3 BEGIN DELETE FROM t4 WHERE 1; END; +} + +do_preupdate_test 7.6.2 { + INSERT INTO t1 VALUES('xyz'); +} { + INSERT main t1 1 1 0 xyz + INSERT main t2 1 1 1 xyz + INSERT main t3 1 1 2 xyz + INSERT main t4 1 1 3 xyz +} +do_preupdate_test 7.6.3 { + UPDATE t1 SET x = 'abc'; +} { + UPDATE main t1 1 1 0 xyz abc + UPDATE main t2 1 1 1 xyz abc + UPDATE main t3 1 1 2 xyz abc + UPDATE main t4 1 1 3 xyz abc +} +do_preupdate_test 7.6.4 { + DELETE FROM t1 WHERE 1; +} { + DELETE main t1 1 1 0 abc + DELETE main t2 1 1 1 abc + DELETE main t3 1 1 2 abc + DELETE main t4 1 1 3 abc +} + +do_execsql_test 7.6.5 { + DROP TRIGGER a; DROP TRIGGER b; DROP TRIGGER c; + DROP TRIGGER d; DROP TRIGGER e; DROP TRIGGER f; + DROP TRIGGER g; DROP TRIGGER h; DROP TRIGGER i; + + CREATE TRIGGER a BEFORE INSERT ON t1 BEGIN INSERT INTO t2 VALUES(new.x); END; + CREATE TRIGGER b BEFORE INSERT ON t2 BEGIN INSERT INTO t3 VALUES(new.x); END; + CREATE TRIGGER c BEFORE INSERT ON t3 BEGIN INSERT INTO t4 VALUES(new.x); END; + + CREATE TRIGGER d BEFORE UPDATE ON t1 BEGIN UPDATE t2 SET x = new.x; END; + CREATE TRIGGER e BEFORE UPDATE ON t2 BEGIN UPDATE t3 SET x = new.x; END; + CREATE TRIGGER f BEFORE UPDATE ON t3 BEGIN UPDATE t4 SET x = new.x; END; + + CREATE TRIGGER g BEFORE DELETE ON t1 BEGIN DELETE FROM t2 WHERE 1; END; + CREATE TRIGGER h BEFORE DELETE ON t2 BEGIN DELETE FROM t3 WHERE 1; END; + CREATE TRIGGER i BEFORE DELETE ON t3 BEGIN DELETE FROM t4 WHERE 1; END; +} + +do_preupdate_test 7.6.6 { + INSERT INTO t1 VALUES('xyz'); +} { + INSERT main t4 1 1 3 xyz + INSERT main t3 1 1 2 xyz + INSERT main t2 1 1 1 xyz + INSERT main t1 1 1 0 xyz +} +do_preupdate_test 7.6.3 { + UPDATE t1 SET x = 'abc'; +} { + UPDATE main t4 1 1 3 xyz abc + UPDATE main t3 1 1 2 xyz abc + UPDATE main t2 1 1 1 xyz abc + UPDATE main t1 1 1 0 xyz abc +} +do_preupdate_test 7.6.4 { + DELETE FROM t1 WHERE 1; +} { + DELETE main t4 1 1 3 abc + DELETE main t3 1 1 2 abc + DELETE main t2 1 1 1 abc + DELETE main t1 1 1 0 abc +} + +# No preupdate callbacks for modifying sqlite_master. +ifcapable altertable { + do_preupdate_test 8.1 { CREATE TABLE x1(x, y); } { } + do_preupdate_test 8.2 { ALTER TABLE x1 ADD COLUMN z } { } + do_preupdate_test 8.3 { ALTER TABLE x1 RENAME TO y1 } { } + do_preupdate_test 8.4 { CREATE INDEX y1x ON y1(x) } { } + do_preupdate_test 8.5 { CREATE VIEW v1 AS SELECT * FROM y1 } { } + do_preupdate_test 8.6 { DROP TABLE y1 } { } +} + +#------------------------------------------------------------------------- +reset_db +db preupdate hook preupdate_hook + +ifcapable altertable { + do_execsql_test 9.0 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c); + CREATE TABLE t2(a, b INTEGER PRIMARY KEY); + } + do_preupdate_test 9.1 { + INSERT INTO t1 VALUES(456, NULL, NULL); + } { + INSERT main t1 456 456 0 456 {} {} + } + do_execsql_test 9.2 { + ALTER TABLE t1 ADD COLUMN d; + } + do_preupdate_test 9.3 { + INSERT INTO t1(a, b, c) VALUES(457, NULL, NULL); + } { + INSERT main t1 457 457 0 457 {} {} {} + } + do_preupdate_test 9.4 { + DELETE FROM t1 WHERE a=456 + } { + DELETE main t1 456 456 0 456 {} {} {} + } + do_preupdate_test 9.5 { + INSERT INTO t2 DEFAULT VALUES; + } { + INSERT main t2 1 1 0 {} 1 + } + do_preupdate_test 9.6 { + INSERT INTO t1 DEFAULT VALUES; + } { + INSERT main t1 458 458 0 458 {} {} {} + } +} + + +do_execsql_test 10.0 { + CREATE TABLE t3(a, b INTEGER PRIMARY KEY); +} +do_preupdate_test 10.1 { + INSERT INTO t3 DEFAULT VALUES +} { + INSERT main t3 1 1 0 {} 1 +} +do_execsql_test 10.2 { SELECT * FROM t3 } {{} 1} +do_preupdate_test 10.3 { + DELETE FROM t3 WHERE b=1 +} {DELETE main t3 1 1 0 {} 1} + +#------------------------------------------------------------------------- +# Test that the "update" hook is not fired for operations on the +# sqlite_stat1 table performed by ANALYZE, even if a pre-update hook is +# registered. +ifcapable analyze { + reset_db + do_execsql_test 11.1 { + CREATE TABLE t1(a, b); + CREATE INDEX idx1 ON t1(a); + CREATE INDEX idx2 ON t1(b); + + INSERT INTO t1 VALUES(1, 2); + INSERT INTO t1 VALUES(3, 4); + INSERT INTO t1 VALUES(5, 6); + INSERT INTO t1 VALUES(7, 8); + } + + db preupdate hook preupdate_cb + db update_hook update_cb + + proc preupdate_cb {args} { lappend ::res "preupdate" $args } + proc update_cb {args} { lappend ::res "update" $args } + + set ::res [list] + do_test 11.2 { + execsql ANALYZE + set ::res + } [list {*}{ + preupdate {INSERT main sqlite_stat1 1 1} + preupdate {INSERT main sqlite_stat1 2 2} + }] + + do_execsql_test 11.3 { + INSERT INTO t1 VALUES(9, 10); + INSERT INTO t1 VALUES(11, 12); + INSERT INTO t1 VALUES(13, 14); + INSERT INTO t1 VALUES(15, 16); + } + + set ::res [list] + do_test 11.4 { + execsql ANALYZE + set ::res + } [list {*}{ + preupdate {DELETE main sqlite_stat1 1 1} + preupdate {DELETE main sqlite_stat1 2 2} + preupdate {INSERT main sqlite_stat1 1 1} + preupdate {INSERT main sqlite_stat1 2 2} + }] +} + +#------------------------------------------------------------------------- +# Test that the pre-update hook is fired for INSERT statements that use +# the xfer optimization on without rowid tables. +# +reset_db +do_execsql_test 12.1 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b); + CREATE TABLE t2(a INTEGER PRIMARY KEY, b) WITHOUT ROWID; + + INSERT INTO t1 VALUES(1, 2); + INSERT INTO t1 VALUES(3, 4); + INSERT INTO t2 VALUES(5, 6); + INSERT INTO t2 VALUES(7, 8); + + CREATE TABLE t3 (a INTEGER PRIMARY KEY, b) WITHOUT ROWID; +} + +db preupdate hook preupdate_cb +db update_hook update_cb + +proc preupdate_cb {args} { lappend ::res "preupdate" $args } +proc update_cb {args} { lappend ::res "update" $args } + +set ::res [list] +do_test 12.2 { + execsql VACUUM + set ::res +} {} + +do_test 12.3 { + set ::res [list] + execsql { INSERT INTO t3 SELECT a, b FROM t2 } + set ::res +} {preupdate {INSERT main t3 0 0} preupdate {INSERT main t3 0 0}} + +do_test 12.4 { + execsql { DELETE FROM t3 } + set ::res [list] + execsql { INSERT INTO t3 SELECT * FROM t2 } + set ::res +} {preupdate {INSERT main t3 0 0} preupdate {INSERT main t3 0 0}} + +do_execsql_test 12.5 { + CREATE TABLE t4(a COLLATE nocase PRIMARY KEY, b) WITHOUT ROWID; + INSERT INTO t4 VALUES('abc', 1); + INSERT INTO t4 VALUES('DEF', 2); +} + +set ::res [list] +do_test 12.6 { + execsql VACUUM + set ::res +} {} + +do_catchsql_test 12.6 { + INSERT INTO t4 VALUES('def', 3); +} {1 {UNIQUE constraint failed: t4.a}} + +#------------------------------------------------------------------------- +# Test adding non-NULL default values using ALTER TABLE. +# +reset_db +db preupdate hook preupdate_hook +do_execsql_test 13.0 { + CREATE TABLE t1(a INTEGER PRIMARY KEY); + INSERT INTO t1 VALUES(100), (200), (300), (400); +} + +do_execsql_test 13.1 { + ALTER TABLE t1 ADD COLUMN b DEFAULT 1234; + ALTER TABLE t1 ADD COLUMN c DEFAULT 'abcdef'; + ALTER TABLE t1 ADD COLUMN d DEFAULT NULL; +} + +do_preupdate_test 13.2 { + DELETE FROM t1 WHERE a=300 +} {DELETE main t1 300 300 0 300 1234 abcdef {}} + +do_preupdate_test 13.3 { + UPDATE t1 SET d='hello world' WHERE a=200 +} { + UPDATE main t1 200 200 0 200 1234 abcdef {} + 200 1234 abcdef {hello world} +} + +do_preupdate_test 13.4 { + INSERT INTO t1 DEFAULT VALUES; +} { + INSERT main t1 401 401 0 401 1234 abcdef {} +} + finish_test diff --git a/test/hook2.test b/test/hook2.test new file mode 100644 index 0000000000..9ae1103a7d --- /dev/null +++ b/test/hook2.test @@ -0,0 +1,218 @@ +# 2017 Jan 30 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# The tests in this file focus on the pre-update hook. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set ::testprefix hook2 + +ifcapable !preupdate { + finish_test + return +} + +#------------------------------------------------------------------------- +proc do_preupdate_test {tn sql x} { + set X [list] + foreach elem $x {lappend X $elem} + uplevel do_test $tn [list " + set ::preupdate \[list\] + execsql { $sql } + set ::preupdate + "] [list $X] +} + +proc preupdate_hook {args} { + set type [lindex $args 0] + eval lappend ::preupdate $args + if {$type != "INSERT"} { + for {set i 0} {$i < [db preupdate count]} {incr i} { + lappend ::preupdate [db preupdate old $i] + } + } + if {$type != "DELETE"} { + for {set i 0} {$i < [db preupdate count]} {incr i} { + set rc [catch { db preupdate new $i } v] + lappend ::preupdate $v + } + } +} + +#------------------------------------------------------------------------- +# Simple tests - INSERT, UPDATE and DELETE on a WITHOUT ROWID table. +# +db preupdate hook preupdate_hook +do_execsql_test 1.0 { + CREATE TABLE t1(a PRIMARY KEY, b) WITHOUT ROWID; +} +do_preupdate_test 1.1 { + INSERT INTO t1 VALUES('one', 1); +} { + INSERT main t1 0 0 one 1 +} +do_preupdate_test 1.2 { + UPDATE t1 SET b=2 WHERE a='one'; +} { + UPDATE main t1 0 0 one 1 one 2 +} +do_preupdate_test 1.3 { + DELETE FROM t1 WHERE a='one'; +} { + DELETE main t1 0 0 one 2 +} + +#------------------------------------------------------------------------- +# Some more complex tests for the pre-update callback on WITHOUT ROWID +# tables. +# +# 2.1.1 - INSERT statement. +# 2.1.2 - INSERT INTO ... SELECT statement. +# 2.1.3 - REPLACE INTO ... (PK conflict) +# 2.1.4 - REPLACE INTO ... (other index conflicts) +# 2.1.5 - REPLACE INTO ... (both PK and other index conflicts) +# +# 2.2.1 - DELETE statement. +# 2.2.2 - DELETE statement that uses the truncate optimization. +# +# 2.3.1 - UPDATE statement. +# 2.3.2 - UPDATE statement that modifies the PK. +# 2.3.3 - UPDATE OR REPLACE ... (PK conflict). +# 2.3.4 - UPDATE OR REPLACE ... (other index conflicts) +# 2.3.4 - UPDATE OR REPLACE ... (both PK and other index conflicts) +# +do_execsql_test 2.0 { + CREATE TABLE t2(a DEFAULT 4, b, c, PRIMARY KEY(b, c)) WITHOUT ROWID; + CREATE UNIQUE INDEX t2a ON t2(a); +} + +do_preupdate_test 2.1.1 { + INSERT INTO t2(b, c) VALUES(1, 1); +} { + INSERT main t2 0 0 4 1 1 +} + +do_execsql_test 2.1.2.0 { + CREATE TABLE d1(a DEFAULT 4, b, c, PRIMARY KEY(b, c)) WITHOUT ROWID; + CREATE UNIQUE INDEX d1a ON d1(a); + INSERT INTO d1 VALUES(1, 2, 3); + INSERT INTO d1 VALUES(11, 12, 13); +} +do_preupdate_test 2.1.2.1 { + INSERT INTO t2 SELECT * FROM d1; +} { + INSERT main t2 0 0 1 2 3 + INSERT main t2 0 0 11 12 13 +} +do_preupdate_test 2.1.2.2 { + INSERT INTO t2 SELECT a+20, b+20, c+20 FROM d1; +} { + INSERT main t2 0 0 21 22 23 + INSERT main t2 0 0 31 32 33 +} +do_execsql_test 2.1.2.3 { + SELECT * FROM t2 ORDER BY b, c; +} { + 4 1 1 + 1 2 3 + 11 12 13 + 21 22 23 + 31 32 33 +} +do_preupdate_test 2.1.3 { + REPLACE INTO t2 VALUES(45, 22, 23); +} { + DELETE main t2 0 0 21 22 23 + INSERT main t2 0 0 45 22 23 +} +do_preupdate_test 2.1.4 { + REPLACE INTO t2 VALUES(11, 100, 100); +} { + DELETE main t2 0 0 11 12 13 + INSERT main t2 0 0 11 100 100 +} +do_preupdate_test 2.1.5 { + REPLACE INTO t2(c, b) VALUES(33, 32) +} { + DELETE main t2 0 0 4 1 1 + DELETE main t2 0 0 31 32 33 + INSERT main t2 0 0 4 32 33 +} + +do_execsql_test 2.2.0 { + SELECT * FROM t2 ORDER BY b,c; +} { + 1 2 3 + 45 22 23 + 4 32 33 + 11 100 100 +} +do_preupdate_test 2.2.1 { + DELETE FROM t2 WHERE b=22; +} { + DELETE main t2 0 0 45 22 23 +} +do_preupdate_test 2.2.2 { + DELETE FROM t2; +} { + DELETE main t2 0 0 1 2 3 + DELETE main t2 0 0 4 32 33 + DELETE main t2 0 0 11 100 100 +} + +do_execsql_test 2.3.0 { + CREATE TABLE t3(x, y PRIMARY KEY, z UNIQUE) WITHOUT ROWID; + INSERT INTO t3 VALUES('a', 'b', 'c'); + INSERT INTO t3 VALUES('d', 'e', 'f'); + + INSERT INTO t3 VALUES(1, 1, 1); + INSERT INTO t3 VALUES(2, 2, 2); + INSERT INTO t3 VALUES(3, 3, 3); +} + +do_preupdate_test 2.3.1 { + UPDATE t3 SET x=4 WHERE y IN ('b', 'e', 'x'); +} { + UPDATE main t3 0 0 a b c 4 b c + UPDATE main t3 0 0 d e f 4 e f +} + +do_preupdate_test 2.3.2 { + UPDATE t3 SET y=y||y WHERE z IN('c', 'f'); +} { + UPDATE main t3 0 0 4 b c 4 bb c + UPDATE main t3 0 0 4 e f 4 ee f +} + +do_preupdate_test 2.3.3 { + UPDATE OR REPLACE t3 SET y='bb' WHERE z='f' +} { + DELETE main t3 0 0 4 bb c + UPDATE main t3 0 0 4 ee f 4 bb f +} + +do_preupdate_test 2.3.4 { + UPDATE OR REPLACE t3 SET z=2 WHERE y=1; +} { + DELETE main t3 0 0 2 2 2 + UPDATE main t3 0 0 1 1 1 1 1 2 +} + +do_preupdate_test 2.3.5 { + UPDATE OR REPLACE t3 SET z=2, y='bb' WHERE y=3; +} { + DELETE main t3 0 0 1 1 2 + DELETE main t3 0 0 4 bb f + UPDATE main t3 0 0 3 3 3 3 bb 2 +} + + +finish_test diff --git a/test/icu.test b/test/icu.test index 73cb9b913e..c1b5653d4f 100644 --- a/test/icu.test +++ b/test/icu.test @@ -15,7 +15,7 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl -ifcapable !icu { +ifcapable !icu&&!icu_collations { finish_test return } @@ -35,50 +35,57 @@ proc test_expr {name settings expr result} { } $settings $expr] $result } -# Tests of the REGEXP operator. -# -test_expr icu-1.1 {i1='hello'} {i1 REGEXP 'hello'} 1 -test_expr icu-1.2 {i1='hello'} {i1 REGEXP '.ello'} 1 -test_expr icu-1.3 {i1='hello'} {i1 REGEXP '.ell'} 0 -test_expr icu-1.4 {i1='hello'} {i1 REGEXP '.ell.*'} 1 -test_expr icu-1.5 {i1=NULL} {i1 REGEXP '.ell.*'} {} +ifcapable icu { -# Some non-ascii characters with defined case mappings -# -set ::EGRAVE "\xC8" -set ::egrave "\xE8" + # Tests of the REGEXP operator. + # + test_expr icu-1.1 {i1='hello'} {i1 REGEXP 'hello'} 1 + test_expr icu-1.2 {i1='hello'} {i1 REGEXP '.ello'} 1 + test_expr icu-1.3 {i1='hello'} {i1 REGEXP '.ell'} 0 + test_expr icu-1.4 {i1='hello'} {i1 REGEXP '.ell.*'} 1 + test_expr icu-1.5 {i1=NULL} {i1 REGEXP '.ell.*'} {} -set ::OGRAVE "\xD2" -set ::ograve "\xF2" + # Some non-ascii characters with defined case mappings + # + set ::EGRAVE "\xC8" + set ::egrave "\xE8" -# That German letter that looks a bit like a B. The -# upper-case version of which is "SS" (two characters). -# -set ::szlig "\xDF" + set ::OGRAVE "\xD2" + set ::ograve "\xF2" -# Tests of the upper()/lower() functions. -# -test_expr icu-2.1 {i1='HellO WorlD'} {upper(i1)} {HELLO WORLD} -test_expr icu-2.2 {i1='HellO WorlD'} {lower(i1)} {hello world} -test_expr icu-2.3 {i1=$::egrave} {lower(i1)} $::egrave -test_expr icu-2.4 {i1=$::egrave} {upper(i1)} $::EGRAVE -test_expr icu-2.5 {i1=$::ograve} {lower(i1)} $::ograve -test_expr icu-2.6 {i1=$::ograve} {upper(i1)} $::OGRAVE -test_expr icu-2.3 {i1=$::EGRAVE} {lower(i1)} $::egrave -test_expr icu-2.4 {i1=$::EGRAVE} {upper(i1)} $::EGRAVE -test_expr icu-2.5 {i1=$::OGRAVE} {lower(i1)} $::ograve -test_expr icu-2.6 {i1=$::OGRAVE} {upper(i1)} $::OGRAVE - -test_expr icu-2.7 {i1=$::szlig} {upper(i1)} "SS" -test_expr icu-2.8 {i1='SS'} {lower(i1)} "ss" - -# In turkish (locale="tr_TR"), the lower case version of I -# is "small dotless i" (code point 0x131 (decimal 305)). -# -set ::small_dotless_i "\u0131" -test_expr icu-3.1 {i1='I'} {lower(i1)} "i" -test_expr icu-3.2 {i1='I'} {lower(i1, 'tr_tr')} $::small_dotless_i -test_expr icu-3.3 {i1='I'} {lower(i1, 'en_AU')} "i" + # That German letter that looks a bit like a B. The + # upper-case version of which is "SS" (two characters). + # + set ::szlig "\xDF" + + # Tests of the upper()/lower() functions. + # + test_expr icu-2.1 {i1='HellO WorlD'} {upper(i1)} {HELLO WORLD} + test_expr icu-2.2 {i1='HellO WorlD'} {lower(i1)} {hello world} + test_expr icu-2.3 {i1=$::egrave} {lower(i1)} $::egrave + test_expr icu-2.4 {i1=$::egrave} {upper(i1)} $::EGRAVE + test_expr icu-2.5 {i1=$::ograve} {lower(i1)} $::ograve + test_expr icu-2.6 {i1=$::ograve} {upper(i1)} $::OGRAVE + test_expr icu-2.3 {i1=$::EGRAVE} {lower(i1)} $::egrave + test_expr icu-2.4 {i1=$::EGRAVE} {upper(i1)} $::EGRAVE + test_expr icu-2.5 {i1=$::OGRAVE} {lower(i1)} $::ograve + test_expr icu-2.6 {i1=$::OGRAVE} {upper(i1)} $::OGRAVE + + test_expr icu-2.7 {i1=$::szlig} {upper(i1)} "SS" + test_expr icu-2.8 {i1='SS'} {lower(i1)} "ss" + + do_execsql_test icu-2.9 { + SELECT upper(char(0xfb04,0xfb04,0xfb04,0xfb04)); + } {FFLFFLFFLFFL} + + # In turkish (locale="tr_TR"), the lower case version of I + # is "small dotless i" (code point 0x131 (decimal 305)). + # + set ::small_dotless_i "\u0131" + test_expr icu-3.1 {i1='I'} {lower(i1)} "i" + test_expr icu-3.2 {i1='I'} {lower(i1, 'tr_tr')} $::small_dotless_i + test_expr icu-3.3 {i1='I'} {lower(i1, 'en_AU')} "i" +} #-------------------------------------------------------------------- # Test the collation sequence function. @@ -120,17 +127,57 @@ do_test icu-4.3 { # # http://src.chromium.org/viewvc/chrome/trunk/src/third_party/sqlite/icu-regexp.patch?revision=34807&view=markup # -do_catchsql_test icu-5.1 { SELECT regexp('a[abc]c.*', 'abc') } {0 1} -do_catchsql_test icu-5.2 { - SELECT regexp('a[abc]c.*') -} {1 {wrong number of arguments to function regexp()}} -do_catchsql_test icu-5.3 { - SELECT regexp('a[abc]c.*', 'abc', 'c') -} {1 {wrong number of arguments to function regexp()}} -do_catchsql_test icu-5.4 { - SELECT 'abc' REGEXP 'a[abc]c.*' -} {0 1} -do_catchsql_test icu-5.4 { SELECT 'abc' REGEXP } {1 {near " ": syntax error}} -do_catchsql_test icu-5.5 { SELECT 'abc' REGEXP, 1 } {1 {near ",": syntax error}} +ifcapable icu { + do_catchsql_test icu-5.1 { SELECT regexp('a[abc]c.*', 'abc') } {0 1} + do_catchsql_test icu-5.2 { + SELECT regexp('a[abc]c.*') + } {1 {wrong number of arguments to function regexp()}} + do_catchsql_test icu-5.3 { + SELECT regexp('a[abc]c.*', 'abc', 'c') + } {1 {wrong number of arguments to function regexp()}} + do_catchsql_test icu-5.4 { + SELECT 'abc' REGEXP 'a[abc]c.*' + } {0 1} + do_catchsql_test icu-5.5 {SELECT 'abc' REGEXP } {1 {incomplete input}} + do_catchsql_test icu-5.6 {SELECT 'abc' REGEXP, 1} {1 {near ",": syntax error}} + + do_malloc_test icu-6.10 -sqlbody { + SELECT upper(char(0xfb04,0xdf,0xfb04,0xe8,0xfb04)); + } +} + +# 2020-03-19 +# The ESCAPE clause on LIKE takes precedence over wildcards +# +do_execsql_test icu-6.0 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(id INTEGER PRIMARY KEY, x TEXT); + INSERT INTO t1 VALUES + (1,'abcde'), + (2,'abc_'), + (3,'abc__'), + (4,'abc%'), + (5,'abc%%'); + SELECT id FROM t1 WHERE x LIKE 'abc%%' ESCAPE '%'; +} {4} +do_execsql_test icu-6.1 { + SELECT id FROM t1 WHERE x LIKE 'abc__' ESCAPE '_'; +} {2} + +# 2024-04-02 +# Optional 3rd argument to icu_load_collation() that specifies +# the "strength" of comparison. +# +reset_db +do_catchsql_test icu-7.1 { + SELECT icu_load_collation('en_US','error','xyzzy'); +} {1 {unknown collation strength "xyzzy" - should be one of: PRIMARY SECONDARY TERTIARY DEFAULT QUARTERNARY IDENTICAL}} +do_execsql_test icu-7.2 { + SELECT icu_load_collation('en_US','prim','PRIMARY'), + icu_load_collation('en_US','dflt','DEFAULT'); +} {{} {}} +do_execsql_test icu-7.3 { + SELECT char(0x100)=='a', char(0x100)=='a' COLLATE dflt, char(0x100)=='a' COLLATE prim; +} {0 0 1} finish_test diff --git a/test/ieee754.test b/test/ieee754.test index bf0676429b..467416dae8 100644 --- a/test/ieee754.test +++ b/test/ieee754.test @@ -23,8 +23,8 @@ foreach {id float rep} { 3 0.5 1,-1 4 1.5 3,-1 5 0.0 0,-1075 - 6 4.9406564584124654e-324 4503599627370497,-1075 - 7 2.2250738585072009e-308 9007199254740991,-1075 + 6 4.9406564584124654e-324 1,-1074 + 7 2.2250738585072009e-308 4503599627370495,-1074 8 2.2250738585072014e-308 1,-1022 } { do_test ieee754-100-$id-1 { @@ -57,4 +57,16 @@ do_execsql_test ieee754-112 { SELECT ieee754(4503599627370495,973) is null; } {1} +do_execsql_test ieee754-200 { + SELECT ieee754(0.0), hex(ieee754_to_blob(ieee754(0,-1075))); +} {ieee754(0,-1075) 0000000000000000} + +# Special case. -0.0 is rendered as ieee754(-1,-3071). +# +do_execsql_test ieee754-201 { + SELECT ieee754(-0.0), hex(ieee754_to_blob(ieee754(-1,-3071))); +} {ieee754(-1,-3071) 8000000000000000} + + + finish_test diff --git a/test/imposter1.test b/test/imposter1.test index 196767be15..da58266be2 100644 --- a/test/imposter1.test +++ b/test/imposter1.test @@ -85,6 +85,7 @@ do_execsql_test imposter-1.2 { # database. # do_execsql_test imposter-1.3 { + PRAGMA writable_schema=on; DELETE FROM xt1 WHERE rowid=5; INSERT INTO xt1(rowid,a,b,c,d) VALUES(99,'hello',1099,2022,NULL); SELECT * FROM chnglog ORDER BY rowid; diff --git a/test/in.test b/test/in.test index 3a42e84b9a..67fdd4f555 100644 --- a/test/in.test +++ b/test/in.test @@ -281,12 +281,13 @@ do_test in-7.8.2 { db status step } {0} -do_test in-8.1 { +do_test in-8.3 { execsql { SELECT b FROM t1 WHERE a IN ('hello','there') } } {world} -do_test in-8.2 { +do_test in-8.4 { + sqlite3_db_config db SQLITE_DBCONFIG_DQS_DML 1 execsql { SELECT b FROM t1 WHERE a IN ("hello",'there') } @@ -314,7 +315,7 @@ do_test in-9.4 { catchsql { SELECT b FROM t1 WHERE a NOT IN tb; } -} {1 {only a single result allowed for a SELECT that is part of an expression}} +} {1 {sub-select returns 2 columns - expected 1}} # IN clauses in CHECK constraints. Ticket #1645 # @@ -332,7 +333,7 @@ do_test in-10.2 { catchsql { INSERT INTO t5 VALUES(4); } -} {1 {CHECK constraint failed: t5}} +} {1 {CHECK constraint failed: a IN (111,222,333)}} # Ticket #1821 # @@ -391,28 +392,28 @@ do_test in-12.2 { SELECT a, b FROM t3 UNION ALL SELECT a, b FROM t2 ); } -} {1 {only a single result allowed for a SELECT that is part of an expression}} +} {1 {sub-select returns 2 columns - expected 1}} do_test in-12.3 { catchsql { SELECT * FROM t2 WHERE a IN ( SELECT a, b FROM t3 UNION SELECT a, b FROM t2 ); } -} {1 {only a single result allowed for a SELECT that is part of an expression}} +} {1 {sub-select returns 2 columns - expected 1}} do_test in-12.4 { catchsql { SELECT * FROM t2 WHERE a IN ( SELECT a, b FROM t3 EXCEPT SELECT a, b FROM t2 ); } -} {1 {only a single result allowed for a SELECT that is part of an expression}} +} {1 {sub-select returns 2 columns - expected 1}} do_test in-12.5 { catchsql { SELECT * FROM t2 WHERE a IN ( SELECT a, b FROM t3 INTERSECT SELECT a, b FROM t2 ); } -} {1 {only a single result allowed for a SELECT that is part of an expression}} +} {1 {sub-select returns 2 columns - expected 1}} do_test in-12.6 { catchsql { SELECT * FROM t2 WHERE a IN ( @@ -478,7 +479,7 @@ do_test in-12.14 { SELECT a, b FROM t3 UNION ALL SELECT a, b FROM t2 ); } -} {1 {only a single result allowed for a SELECT that is part of an expression}} +} {1 {sub-select returns 2 columns - expected 1}} do_test in-12.15 { catchsql { SELECT * FROM t2 WHERE a IN ( @@ -633,11 +634,230 @@ do_test in-13.15 { catchsql { SELECT 0 WHERE (SELECT 0,0) OR (0 IN (1,2)); } -} {1 {only a single result allowed for a SELECT that is part of an expression}} +} {1 {sub-select returns 2 columns - expected 1}} do_test in-13.X { db nullvalue "" } {} +# At one point the following was causing valgrind to report a "jump +# depends on unitialized location" problem. +# +do_execsql_test in-14.0 { + CREATE TABLE c1(a); + INSERT INTO c1 VALUES(1), (2), (4), (3); +} +do_execsql_test in-14.1 { + SELECT * FROM c1 WHERE a IN (SELECT a FROM c1) ORDER BY 1 +} {1 2 3 4} + +# 2019-02-20 Ticket https://sqlite.org/src/tktview/df46dfb631f75694fbb97033b69 +# +do_execsql_test in-15.0 { + DROP TABLE IF EXISTS t1; + CREATE TABLE IF NOT EXISTS t1(id INTEGER PRIMARY KEY); + INSERT INTO t1 VALUES(1); + SELECT a.id FROM t1 AS a JOIN t1 AS b ON a.id=b.id WHERE a.id IN (1,2,3); +} {1} +do_execsql_test in-15.1 { + DROP TABLE IF EXISTS t2; + CREATE TABLE t2(a INTEGER PRIMARY KEY,b); + INSERT INTO t2 VALUES(1,11); + INSERT INTO t2 VALUES(2,22); + INSERT INTO t2 VALUES(3,33); + SELECT b, a IN (3,4,5) FROM t2 ORDER BY b; +} {11 0 22 0 33 1} +do_execsql_test in-15.2 { + DROP TABLE IF EXISTS t3; + CREATE TABLE t3(x INTEGER PRIMARY KEY); + INSERT INTO t3 VALUES(8); + SELECT CASE WHEN x NOT IN (5,6,7) THEN 'yes' ELSE 'no' END FROM t3; + SELECT CASE WHEN x NOT IN (NULL,6,7) THEN 'yes' ELSE 'no' END FROM t3; +} {yes no} +do_execsql_test in-15.3 { + SELECT CASE WHEN x NOT IN (5,6,7) OR x=0 THEN 'yes' ELSE 'no' END FROM t3; + SELECT CASE WHEN x NOT IN (NULL,6,7) OR x=0 THEN 'yes' ELSE 'no' END FROM t3; +} {yes no} +do_execsql_test in-15.4 { + DROP TABLE IF EXISTS t4; + CREATE TABLE t4(a INTEGER PRIMARY KEY, b INT); + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<20) + INSERT INTO t4(a,b) SELECT x, x+100 FROM c; + SELECT b FROM t4 WHERE a IN (3,null,8) ORDER BY +b; +} {103 108} +do_execsql_test in-15.5 { + SELECT b FROM t4 WHERE a NOT IN (3,null,8); +} {} +do_execsql_test in-15.6 { + DROP TABLE IF EXISTS t5; + DROP TABLE IF EXISTS t6; + CREATE TABLE t5(id INTEGER PRIMARY KEY, name TEXT); + CREATE TABLE t6(id INTEGER PRIMARY KEY, name TEXT, t5_id INT); + INSERT INTO t5 VALUES(1,'Alice'),(2,'Emma'); + INSERT INTO t6 VALUES(1,'Bob',1),(2,'Cindy',1),(3,'Dave',2); + SELECT a.* + FROM t5 AS 'a' JOIN t5 AS 'b' ON b.id=a.id + WHERE b.id IN ( + SELECT t6.t5_id + FROM t6 + WHERE name='Bob' + AND t6.t5_id IS NOT NULL + AND t6.id IN ( + SELECT id + FROM (SELECT t6.id, count(*) AS x + FROM t6 + WHERE name='Bob' + ) AS 't' + WHERE x=1 + ) + AND t6.id IN (1,id) + ); +} {1 Alice} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test in-16.0 { + CREATE TABLE x1(a, b); + INSERT INTO x1(a) VALUES(1), (2), (3), (4), (5), (6); + CREATE INDEX x1i ON x1(a, b); +} + +do_execsql_test in-16.1 { + SELECT * FROM x1 + WHERE a IN (SELECT a FROM x1 WHERE (a%2)==0) + ORDER BY a DESC, b; +} {6 {} 4 {} 2 {}} + +do_execsql_test in-16.2 { + SELECT * FROM x1 + WHERE a IN (SELECT a FROM x1 WHERE (a%7)==0) + ORDER BY a DESC, b; +} {} + +# 2019-06-11 +# https://sqlite.org/src/info/57353f8243c637c0 +# +do_execsql_test in-17.1 { + SELECT 1 IN ('1'); +} 0 +do_execsql_test in-17.2 { + SELECT 1 IN ('1' COLLATE nocase); +} 0 +do_execsql_test in-17.3 { + SELECT 1 IN (CAST('1' AS text)); +} 0 +do_execsql_test in-17.4 { + SELECT 1 IN (CAST('1' AS text) COLLATE nocase); +} 0 + +# 2019-08-27 ticket https://sqlite.org/src/info/dbaf8a6820be1ece +# +do_execsql_test in-18.1 { + DROP TABLE IF EXISTS t0; + CREATE TABLE t0(c0 INT UNIQUE); + INSERT INTO t0(c0) VALUES (1); + SELECT * FROM t0 WHERE '1' IN (t0.c0); +} {} + +# 2019-09-02 ticket https://sqlite.org/src/info/2841e99d104c6436 +# For the IN_INDEX_NOOP optimization, apply REAL affinity to the LHS +# values prior to comparison if the RHS has REAL affinity. +# +# Also ticket https://sqlite.org/src/info/29f635e0af71234b +# +do_execsql_test in-19.10 { + DROP TABLE IF EXISTS t0; + CREATE TABLE t0(c0 REAL UNIQUE); + INSERT INTO t0(c0) VALUES(2.0625E00); + SELECT 1 FROM t0 WHERE c0 IN ('2.0625'); +} {1} +do_execsql_test in-19.20 { + SELECT c0 IN ('2.0625') FROM t0; +} {1} +do_execsql_test in-19.21 { + SELECT c0 = ('2.0625') FROM t0; +} {1} +do_execsql_test in-19.22 { + SELECT c0 = ('0.20625e+01') FROM t0; +} {1} +do_execsql_test in-19.30 { + SELECT c0 IN ('2.0625',2,3) FROM t0; +} {1} +do_execsql_test in-19.40 { + DROP TABLE t0; + CREATE TABLE t0(c0 TEXT, c1 REAL, c2, PRIMARY KEY(c2, c0, c1)); + CREATE INDEX i0 ON t0(c1 IN (c0)); + INSERT INTO t0(c0, c2) VALUES (0, NULL) ON CONFLICT(c2, c1, c0) DO NOTHING; + PRAGMA integrity_check; +} {ok} + +# Ticket f3ff1472887 +# +do_execsql_test in-20.1 { + SELECT (1 IN (2 IS TRUE)); +} {1} + +# Forum post: https://sqlite.org/forum/forumpost/5782619992. +# +reset_db +do_execsql_test in-21.1 { + CREATE TABLE t0(c0); + SELECT COUNT(*) FROM t0 ORDER BY (t0.c0 IN ()); +} {0} + +# Ignore extra parentheses around a subquery on the RHS of an IN operator, +# because that is what PostgreSQL does. +# +do_execsql_test in-22.1 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(x INT PRIMARY KEY, y INT); + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<8) + INSERT INTO t1(x,y) SELECT x, x*100 FROM c; + DROP TABLE IF EXISTS t2; + CREATE TABLE t2(a INT); + INSERT INTO t2 VALUES(2),(4),(6); + SELECT * FROM t1 WHERE x IN (SELECT a FROM t2); +} {2 200 4 400 6 600} +do_execsql_test in-22.2 { + SELECT * FROM t1 WHERE x IN ((SELECT a FROM t2)); +} {2 200 4 400 6 600} +do_execsql_test in-22.3 { + SELECT * FROM t1 WHERE x IN (((SELECT a FROM t2))); +} {2 200 4 400 6 600} +do_execsql_test in-22.4 { + SELECT * FROM t1 WHERE x IN ((((((SELECT a FROM t2)))))); +} {2 200 4 400 6 600} + +# 2023-04-04 https://sqlite.org/forum/forumpost/dc16ec63d3 +# Faulty assert() statement in the IN optimization. +# +do_execsql_test in-23.0 { + DROP TABLE IF EXISTS t4; + CREATE TABLE t4(a TEXT, b INT); + INSERT INTO t4(a,b) VALUES('abc',0),('ABC',1),('def',2); + CREATE INDEX t4x ON t4(a, +a COLLATE NOCASE); + SELECT a0.a, group_concat(a1.a) AS b + FROM t4 AS a0 JOIN t4 AS a1 + GROUP BY a0.a + HAVING (SELECT sum( (a1.a == +a0.a COLLATE NOCASE) IN (SELECT b FROM t4))); +} {ABC abc,ABC,def abc abc,ABC,def def abc,ABC,def} +do_execsql_test in-23.0-b { + SELECT a0.a, group_concat(a1.a) AS b + FROM t4 AS a0 JOIN t4 AS a1 + GROUP BY a0.a + HAVING (SELECT sum( (a1.a GLOB +a0.a COLLATE NOCASE) IN (SELECT b FROM t4))); +} {ABC abc,ABC,def abc abc,ABC,def def abc,ABC,def} +# +# Follow-up forum/forumpost/0713a16a44 +# +do_execsql_test in-23.1 { + CREATE VIEW t5 AS + SELECT 1 AS b + WHERE (SELECT count(0=NOT+a COLLATE NOCASE IN (SELECT 0)) + FROM t4 + GROUP BY a); + SELECT * FROM t5; +} 1 + finish_test diff --git a/test/in4.test b/test/in4.test index a89961f82b..07cd3e284d 100644 --- a/test/in4.test +++ b/test/in4.test @@ -13,6 +13,7 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl +set testprefix in4 do_test in4-1.1 { execsql { @@ -226,10 +227,13 @@ do_execsql_test in4-3.42 { do_execsql_test in4-3.43 { SELECT * FROM t3 WHERE x IN (10); } {10 10 10} -do_execsql_test in4-3.44 { - EXPLAIN - SELECT * FROM t3 WHERE x IN (10); -} {~/OpenEphemeral/} + +# This test would verify that the "X IN (Y)" -> "X==Y" optimization +# was working. But we have now taken that optimization out. +#do_execsql_test in4-3.44 { +# EXPLAIN +# SELECT * FROM t3 WHERE x IN (10); +#} {~/OpenEphemeral/} do_execsql_test in4-3.45 { SELECT * FROM t3 WHERE x NOT IN (10,11,99999); } {1 1 1} @@ -326,7 +330,7 @@ do_execsql_test in4-6.1 { do_execsql_test in4-6.1-eqp { EXPLAIN QUERY PLAN SELECT * FROM t6a, t6b WHERE a=3 AND b IN (c); -} {~/SCAN/} +} {~/SCAN t6a/} do_execsql_test in4-6.2 { SELECT * FROM t6a, t6b WHERE a=3 AND c IN (b); } {3 4 4 44} @@ -335,5 +339,182 @@ do_execsql_test in4-6.2-eqp { SELECT * FROM t6a, t6b WHERE a=3 AND c IN (b); } {~/SCAN/} +reset_db +do_execsql_test 7.0 { + CREATE TABLE t1(a, b, c); + CREATE TABLE t2(d, e); + CREATE INDEX t1bc ON t1(c, b); + INSERT INTO t2(e) VALUES(1); + INSERT INTO t1 VALUES(NULL, NULL, NULL); +} + +do_execsql_test 7.1 { + SELECT * FROM t2 LEFT JOIN t1 ON c = d AND b IN (10,10,10); +} {{} 1 {} {} {}} + +ifcapable rtree { + reset_db + do_execsql_test 7.2 { + CREATE VIRTUAL TABLE t1 USING rtree(a, b, c); + CREATE TABLE t2(d INTEGER, e INT); + INSERT INTO t2(e) VALUES(1); + } + + do_execsql_test 7.3 { + SELECT * FROM t2 LEFT JOIN t1 ON c IN (d) AND b IN (10,10,10); + } {{} 1 {} {} {}} +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 8.0 { + CREATE TABLE t1(x INTEGER PRIMARY KEY, y); + CREATE UNIQUE INDEX t1y ON t1(y); + INSERT INTO t1 VALUES(111, 'AAA'),(222, 'BBB'),(333, 'CCC'); + CREATE TABLE t2(z); + INSERT INTO t2 VALUES('BBB'),('AAA'); + ANALYZE sqlite_schema; + INSERT INTO sqlite_stat1 VALUES('t1', 't1y','100 1'); +} + +db close +sqlite3 db test.db + +do_execsql_test 8.1 { + SELECT t1.x FROM t2 CROSS JOIN t1 WHERE t2.z = t1.y; +} {222 111} + +do_execsql_test 8.2 { + SELECT t1.x FROM t2 CROSS JOIN t1 WHERE t2.z = t1.y AND +t1.x IN (111, 222); +} {222 111} + +do_execsql_test 8.3 { + SELECT t1.x FROM t2 CROSS JOIN t1 WHERE t2.z = t1.y AND t1.x IN (111, 222); +} {222 111} + +# 2021-06-02 forum post https://sqlite.org/forum/forumpost/b4fcb8a598 +# OP_SeekScan changes from check-in 4a43430fd23f8835 on 2020-09-30 causes +# performance regression. +# +reset_db +do_execsql_test 9.0 { + CREATE TABLE node(node_id INTEGER PRIMARY KEY); + CREATE TABLE edge(node_from INT, node_to INT); + CREATE TABLE sub_nodes(node_id INTEGER PRIMARY KEY); + CREATE INDEX edge_from_to ON edge(node_from,node_to); + CREATE INDEX edge_to_from ON edge(node_to,node_from); + ANALYZE; + DELETE FROM sqlite_stat1; + INSERT INTO sqlite_stat1 VALUES + ('sub_nodes',NULL,'1000000'), + ('edge','edge_to_from','20000000 2 2'), + ('edge','edge_from_to','20000000 2 2'), + ('node',NULL,'10000000'); + ANALYZE sqlite_schema; +} {} +do_eqp_test 9.1 { +SELECT count(*) FROM edge + WHERE node_from IN sub_nodes AND node_to IN sub_nodes; +} { + QUERY PLAN + |--SEARCH edge USING COVERING INDEX edge_to_from (node_to=?) + |--USING ROWID SEARCH ON TABLE sub_nodes FOR IN-OPERATOR + `--USING ROWID SEARCH ON TABLE sub_nodes FOR IN-OPERATOR +} +# ^^^^^ the key to the above is that the index should only use a single +# term (node_to=?), not two terms (node_to=? AND node_from=). + +# dbsqlfuzz case +# +reset_db +do_execsql_test 10.0 { + CREATE TABLE t1(a,b,c,d,PRIMARY KEY(a,b,c)) WITHOUT ROWID; + INSERT INTO t1(a,b,c,d) VALUES + (0,-2,2,3), + (0,2,3,4), + (0,5,8,10), + (1,7,11,13); + ANALYZE sqlite_schema; + INSERT INTO sqlite_stat1 VALUES('t1','t1','10 3 2 1'); + ANALYZE sqlite_schema; + PRAGMA reverse_unordered_selects(1); + SELECT d FROM t1 WHERE 0=a AND b IN (-17,-4,-3,1,5,25,7798); +} {10} + +# 2021-06-13 dbsqlfuzz e41762333a4d6e90a49e628f488d0873b2dba4c5 +# The opcode that precedes OP_SeekScan is usually OP_IdxGT, but can +# sometimes be OP_IdxGE +# +reset_db +do_execsql_test 11.0 { + CREATE TABLE t1(a TEXT, b INT, c INT, d INT); + INSERT INTO t1 VALUES('abc',123,4,5); + INSERT INTO t1 VALUES('xyz',1,'abcdefxyz',99); + CREATE INDEX t1abc ON t1(b,b,c); + ANALYZE sqlite_schema; + INSERT INTO sqlite_stat1 VALUES('t1','t1abc','10000 5 00 2003 10'); + ANALYZE sqlite_schema; +} {} +do_execsql_test 11.1 { + SELECT * FROM t1 + WHERE b IN (345, (SELECT 1 FROM t1 + WHERE b IN (coalesce(1,random())) + AND c GLOB 'abc*xyz')) + AND c BETWEEN 'abc' AND 'xyz'; +} {xyz 1 abcdefxyz 99} +do_execsql_test 11.2 { + EXPLAIN SELECT * FROM t1 + WHERE b IN (345, (SELECT 1 FROM t1 + WHERE b IN (coalesce(1,random())) + AND c GLOB 'abc*xyz')) + AND c BETWEEN 'abc' AND 'xyz'; +} {/ SeekScan /} + +# 2021-06-25 ticket 6dcbfd11cf666e21 +# Another problem with OP_SeekScan +# +reset_db +do_execsql_test 12.0 { + CREATE TABLE t1(a,b,c); + CREATE INDEX t1abc ON t1(a,b,c); + CREATE INDEX t1bca on t1(b,c,a); + INSERT INTO t1 VALUES(56,1119,1115); + INSERT INTO t1 VALUES(57,1147,1137); + INSERT INTO t1 VALUES(100,1050,1023); + INSERT INTO t1 VALUES(101,1050,1023); + ANALYZE sqlite_schema; + INSERT INTO sqlite_stat1 VALUES('t1','t1abc','358677 2 2 1'); + INSERT INTO sqlite_stat1 VALUES('t1','t1bca','358677 4 2 1'); + ANALYZE sqlite_schema; + SELECT * FROM t1 NOT INDEXED + WHERE (b = 1137 AND c IN (97, 98)) + OR (b = 1119 AND c IN (1115, 1023)); +} {56 1119 1115} +do_execsql_test 12.1 { + SELECT * FROM t1 + WHERE (b = 1137 AND c IN (97, 98)) + OR (b = 1119 AND c IN (1115, 1023)); +} {56 1119 1115} + +# 2021-11-02 ticket 5981a8c041a3c2f3 +# Another OP_SeekScan problem. +# +reset_db +do_execsql_test 13.0 { + CREATE TABLE t1(id INTEGER PRIMARY KEY, a INT, b INT, c INT); + INSERT INTO t1 VALUES(10,1,2,5); + INSERT INTO t1 VALUES(20,1,3,5); + INSERT INTO t1 VALUES(30,1,2,4); + INSERT INTO t1 VALUES(40,1,3,4); + ANALYZE sqlite_master; + INSERT INTO sqlite_stat1 VALUES('t1','t1x','84000 3 2 1'); + CREATE INDEX t1x ON t1(a,b,c); + PRAGMA writable_schema=RESET; + SELECT * FROM t1 + WHERE a=1 + AND b IN (2,3) + AND c BETWEEN 4 AND 5 + ORDER BY +id; +} {10 1 2 5 20 1 3 5 30 1 2 4 40 1 3 4} finish_test diff --git a/test/in5.test b/test/in5.test index 67d212589d..933eb90026 100644 --- a/test/in5.test +++ b/test/in5.test @@ -183,4 +183,109 @@ do_execsql_test 6.3.1 { SELECT count(*) FROM x2 WHERE b IN (SELECT DISTINCT a FROM x1 LIMIT 2); } {2} +#------------------------------------------------------------------------- +# Test to confirm that bug [5e3c886796e5] is fixed. +# +do_execsql_test 7.1 { + CREATE TABLE y1(a, b); + CREATE TABLE y2(c); + + INSERT INTO y1 VALUES(1, 'one'); + INSERT INTO y1 VALUES('two', 'two'); + INSERT INTO y1 VALUES(3, 'three'); + + INSERT INTO y2 VALUES('one'); + INSERT INTO y2 VALUES('two'); + INSERT INTO y2 VALUES('three'); +} {} + +do_execsql_test 7.2.1 { + SELECT a FROM y1 WHERE b NOT IN (SELECT a FROM y2); +} {1 3} +do_execsql_test 7.2.2 { + SELECT a FROM y1 WHERE b IN (SELECT a FROM y2); +} {two} + +do_execsql_test 7.3.1 { + CREATE INDEX y2c ON y2(c); + SELECT a FROM y1 WHERE b NOT IN (SELECT a FROM y2); +} {1 3} +do_execsql_test 7.3.2 { + SELECT a FROM y1 WHERE b IN (SELECT a FROM y2); +} {two} + +#------------------------------------------------------------------------- +# Tests to confirm that indexes on the rowid column do not confuse +# the query planner. See ticket [0eab1ac7591f511d]. +# +do_execsql_test 8.0 { + CREATE TABLE n1(a INTEGER PRIMARY KEY, b VARCHAR(500)); + CREATE UNIQUE INDEX n1a ON n1(a); +} + +do_execsql_test 8.1 { + SELECT count(*) FROM n1 WHERE a IN (1, 2, 3) +} 0 +do_execsql_test 8.2 { + SELECT count(*) FROM n1 WHERE a IN (SELECT +a FROM n1) +} 0 +do_execsql_test 8.3 { + INSERT INTO n1 VALUES(1, NULL), (2, NULL), (3, NULL); + SELECT count(*) FROM n1 WHERE a IN (1, 2, 3) +} 3 +do_execsql_test 8.4 { + SELECT count(*) FROM n1 WHERE a IN (SELECT +a FROM n1) +} 3 + +#------------------------------------------------------------------------- +# Test that ticket 61fe97454c is fixed. +# +do_execsql_test 9.0 { + CREATE TABLE t9(a INTEGER PRIMARY KEY); + INSERT INTO t9 VALUES (44), (45); +} +do_execsql_test 9.1 { + SELECT * FROM t9 WHERE a IN (44, 45, 44, 45) +} {44 45} + +#------------------------------------------------------------------------- +# Test that ticket c7a117190 is fixed. +# +reset_db +do_execsql_test 9.0 { + CREATE TABLE t0(c0); + CREATE VIEW v0(c0) AS SELECT LOWER(CAST('1e500' AS TEXT)) FROM t0; + INSERT INTO t0(c0) VALUES (NULL); +} + +do_execsql_test 9.1 { + SELECT lower('1e500') FROM t0 WHERE rowid NOT IN (0, 0, lower('1e500')); +} {1e500} + +do_execsql_test 9.2 { + SELECT lower('1e500') FROM t0 WHERE rowid != lower('1e500'); +} {1e500} + +#------------------------------------------------------------------------- +# +reset_db + +do_execsql_test 10.0 { + CREATE TABLE t1(a, b TEXT COLLATE NOCASE); + INSERT INTO t1 VALUES('abc', 'def'); + INSERT INTO t1 VALUES('ghi', 'jkl'); +} + +do_execsql_test 10.1 { + SELECT rowid FROM t1 WHERE (a, b) IN ( VALUES('abc', 'def'), ('ghi', 'JKL') ); +} {1 2} + +do_execsql_test 10.2 { + CREATE INDEX i1 ON t1(a, b COLLATE BINARY); +} + +do_execsql_test 10.3 { + SELECT rowid FROM t1 WHERE (a, b) IN ( VALUES('abc', 'def'), ('ghi', 'JKL') ); +} {1 2} + finish_test diff --git a/test/in6.test b/test/in6.test new file mode 100644 index 0000000000..f477f60ef2 --- /dev/null +++ b/test/in6.test @@ -0,0 +1,119 @@ +# 2018-06-07 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# A multi-key index that uses an IN operator on one of the keys other +# than the left-most key is able to abort the IN-operator loop early +# if key terms further to the left do not match. +# +# Call this the "multikey-IN-operator early-out optimization" or +# just "IN-early-out" optimization for short. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix in6 + +do_test in6-1.1 { + db eval { + CREATE TABLE t1(a,b,c,d); + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<100) + INSERT INTO t1(a,b,c,d) + SELECT 100, 200+x/2, 300+x/5, x FROM c; + CREATE INDEX t1abc ON t1(a,b,c); + ANALYZE; + UPDATE sqlite_stat1 SET stat='1000000 500000 500 50'; + ANALYZE sqlite_master; + } + set ::sqlite_search_count 0 + db eval { + SELECT d FROM t1 + WHERE a=99 + AND b IN (200,205,201,204) + AND c IN (304,302,309,308); + } +} {} +do_test in6-1.2 { + set ::sqlite_search_count +} {0} ;# Without the IN-early-out optimization, this value would be 15 + +# The multikey-IN-operator early-out optimization does not apply +# when the IN operator is on the left-most column of the index. +# +do_test in6-1.3 { + db eval { + EXPLAIN + SELECT d FROM t1 + WHERE a IN (98,99,100,101) + AND b=200 AND c=300; + } +} {~/(IfNoHope|SeekHit)/} + +set sqlite_search_count 0 +do_execsql_test in6-1.4 { + SELECT d FROM t1 + WHERE a=100 + AND b IN (200,201,202,204) + AND c IN (300,302,301,305) + ORDER BY +d; +} {1 2 3 4 5 8 9} +do_test in6-1.5 { + set ::sqlite_search_count +} {39} + +do_execsql_test in6-2.1 { + CREATE TABLE t2(e INT UNIQUE, f TEXT); + SELECT d, f FROM t1 LEFT JOIN t2 ON (e=d) + WHERE a=100 + AND b IN (200,201,202,204) + AND c IN (300,302,301,305) + ORDER BY +d; +} {1 {} 2 {} 3 {} 4 {} 5 {} 8 {} 9 {}} + +# 2020-03-16 ticket 82b588d342d515d1 +# Ensure that the IN-early-out optimization works with LEFT JOINs +# +reset_db +do_execsql_test in6-3.100 { + CREATE TABLE t1(a); + INSERT INTO t1 VALUES(0); + CREATE TABLE t2(b, c, d); + INSERT INTO t2(b,c,d) VALUES(4,5,3),(4,5,4),(4,5,8); + CREATE INDEX t2bcd ON t2(b, c, d); + SELECT * FROM t1 LEFT JOIN t2 ON b=NULL AND c=5 AND d IN (2,3,4); +} {0 {} {} {}} +do_execsql_test in6-3.110 { + CREATE TABLE v0(v1); + CREATE TABLE v3(v5, v4); + INSERT INTO v0 VALUES(0); + CREATE INDEX v9 ON v3(v4, v4, v5); + SELECT quote(v5) FROM v0 LEFT JOIN v3 ON v4 = NULL AND v5 IN(0); +} {NULL} + +# 2021-04-29 forum https://sqlite.org/forum/forumpost/6a3ec138e9 +# An early OP_IsNull bypass might skip over the OP_Affinity and +# cause the OP_IfNoHope to jump on a false-positive, resulting in +# incomplete output. +# +reset_db +do_execsql_test in6-3.120 { + CREATE TABLE t1(a TEXT, b TEXT); + INSERT INTO t1 VALUES(null,10),(0,10),(10,10); + CREATE INDEX t1ab ON t1(a,b); + SELECT quote(a), quote(b), '|' FROM t1 WHERE b in (SELECT a FROM t1) AND a=0; +} {'0' '10' |} +do_execsql_test in6-3.130 { + CREATE TABLE t2(x TEXT); + INSERT INTO t2(x) VALUES(NULL),(0),(10); + SELECT quote(x), quote(a), quote(b), 'x' + FROM t2 LEFT JOIN t1 ON a=x AND b in (null,0,10); +} {NULL NULL NULL x '0' '0' '10' x '10' '10' '10' x} + +finish_test diff --git a/test/in7.test b/test/in7.test new file mode 100644 index 0000000000..763396140a --- /dev/null +++ b/test/in7.test @@ -0,0 +1,251 @@ +# 2024-05-01 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix in7 + +do_execsql_test 1.0 { + CREATE TABLE t1(a, b, c PRIMARY KEY); + CREATE TABLE t2(x, y, z); +} + +foreach {tn nNext idx sql} { + 1 1 { + CREATE INDEX i1 ON t1(a, b); + } { + SELECT * FROM t1 WHERE (a, b) IN (SELECT x, y FROM t2) + } + + 2 0 { + CREATE UNIQUE INDEX i1 ON t1(a, b); + } { + SELECT * FROM t1 WHERE (a, b) IN (SELECT x, y FROM t2) + } + + 3 0 { + CREATE UNIQUE INDEX i1 ON t1(a, b); + } { + SELECT * FROM t1 WHERE a = ? AND b = ? + } + + 3 1 { + CREATE UNIQUE INDEX i1 ON t1(a, b); + } { + SELECT * FROM t1 WHERE a = ? AND b IS ? + } + + 4 0 { + CREATE UNIQUE INDEX i1 ON t1(a, b); + } { + SELECT * FROM t1 WHERE a = ? AND b IN (?, ?, ?); + } + + 5 1 { + CREATE UNIQUE INDEX i1 ON t1(a, b, c); + } { + SELECT * FROM t1 WHERE a = ? AND b = ? + } + + 6 0 { + } { + SELECT * FROM t1 WHERE c IN (SELECT z FROM t2) + } + + 7 0 { + } { + SELECT * FROM t1 WHERE (a, c) IN (SELECT z, x FROM t2) + } + + 8 1 { + } { + SELECT * FROM t1 WHERE a IN (SELECT z FROM t2) + } + + 9 1 { + CREATE UNIQUE INDEX i1 ON t1(a, b); + } { + SELECT * FROM t1 WHERE a IN (SELECT z FROM t2) AND b IS ? + } + 10 0 { + CREATE UNIQUE INDEX i1 ON t1(a, b); + } { + SELECT * FROM t1 WHERE a IN (SELECT z FROM t2) AND b = ? + } + 11 1 { + CREATE UNIQUE INDEX i1 ON t1(a, b); + } { + SELECT * FROM t1 WHERE a IS NULL AND b IN (SELECT z FROM t2) + } + 12 0 { + CREATE UNIQUE INDEX i1 ON t1(a, b); + } { + SELECT * FROM t1 WHERE a = ? AND b IN (SELECT z FROM t2) + } +} { + do_test 1.1.$tn { + execsql BEGIN + execsql $idx + + catch { array unset root_to_tbl } + catch { array unset csr_to_root } + + db eval {SELECT rootpage, tbl_name FROM sqlite_schema} { + set root_to_tbl($rootpage) $tbl_name + } + + set nSeen 0 + db eval "explain $sql" { + if {$opcode=="OpenRead"} { + set csr_to_root($p1) $p2 + } + if {$opcode=="Next"} { + catch { + set root $csr_to_root($p1) + set tbl $root_to_tbl($root) + if {$tbl=="t1"} {incr nSeen} + } + } + } + + execsql ROLLBACK + + set nSeen + } $nNext +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 2.0 { + CREATE TABLE t1(a TEXT PRIMARY KEY, b TEXT) WITHOUT ROWID; + INSERT INTO t1 VALUES('1', 'one'); + INSERT INTO t1 VALUES('2', NULL); + INSERT INTO t1 VALUES('3', 'three'); +} + +do_execsql_test 2.1 { + SELECT b FROM t1 WHERE a IN (1,2,3) ORDER BY b ASC NULLS LAST; +} {one three {}} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 3.0 { + CREATE TABLE x1(a); + INSERT INTO x1 VALUES(1), (2), (3); + + CREATE TABLE x2(b); + INSERT INTO x2 VALUES(4), (5), (6); + + CREATE TABLE t1(u); + INSERT INTO t1 VALUES(1), (2), (3), (4), (5), (6); + + CREATE VIEW v1 AS SELECT u FROM t1 WHERE u IN ( + SELECT a FROM x1 + ); + CREATE VIEW v2 AS SELECT u FROM t1 WHERE u IN ( + SELECT b FROM x2 + ); +} + +do_execsql_test 3.1 { + SELECT * FROM v1 +} { + 1 2 3 +} + +do_execsql_test 3.2 { + SELECT * FROM v2 +} { + 4 5 6 +} + +do_execsql_test 3.3 { + SELECT * FROM v2 + UNION ALL + SELECT * FROM v1 +} { + 4 5 6 + 1 2 3 +} + +do_execsql_test 3.4 { + WITH w1 AS ( + SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 + ), + w2 AS ( + SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 + ) + SELECT * FROM v1 WHERE u IN w1 + UNION ALL + SELECT * FROM v2 WHERE u IN w2 +} { + 1 2 3 4 5 6 +} + +# 2024-11-20 https://sqlite.org/forum/forumpost/0b9ded2f8428ac00 +# +# Bug in SubrtnSig logic. If a SELECT statement is copied and the copy +# is subsequently modified, we need to change the Select.selId on the +# copy so that when the copy is used to generate code, the SubrtnSig +# logic won't try to substitute the original SELECT in place of the +# copy which is now different. +# +do_execsql_test 3.5 { + DROP TABLE IF EXISTS t1; + DROP TABLE IF EXISTS t2; + CREATE TABLE t1 (a int UNIQUE); + CREATE TABLE t2 (b int UNIQUE); + INSERT INTO t1 VALUES (1); + INSERT INTO t2 VALUES (1), (2); + SELECT t1.a, t2.b FROM t1, t2 WHERE (t1.a, t2.b) = (1, 1); +} {1 1} +do_execsql_test 3.6 { + SELECT t1.a, t2.b FROM t1, t2 WHERE (t1.a, t2.b) IN ((1, 1)); +} {1 1} +do_execsql_test 3.7 { + SELECT t1.a, t2.b FROM t1, t2 WHERE (t1.a, t2.b) = (1, 2); +} {1 2} +do_execsql_test 3.8 { + SELECT t1.a, t2.b FROM t1, t2 WHERE (t1.a, t2.b) IN ((1, 2)); +} {1 2} + +# 2025-01-30 Inifinite loop in byte-code discovered by dbsqlfuzz +# having to do with SubrtnSig logic. The code was using a Subroutine +# from within itself resulting in infinite recursion. +# +# This test will spin forever if the bug has not been fixed, or if +# it reappears. +# +reset_db +do_execsql_test 4.0 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b); + INSERT INTO t1 VALUES(1,x'1111'); + CREATE TABLE t2(c); + CREATE TABLE t3(d); + CREATE TRIGGER t1tr UPDATE ON t1 BEGIN + UPDATE t1 SET b=x'2222' FROM t2; + UPDATE t1 + SET b = (SELECT a IN (SELECT a + FROM t1 + WHERE (b,a) IN (SELECT rowid, d + FROM t3 + ) + ) + FROM t1 NATURAL RIGHT JOIN t1 + ); + END; + UPDATE t1 SET b=x'3333'; + SELECT quote(b) FROM t1; +} {X'3333'} + +finish_test diff --git a/test/incrblob.test b/test/incrblob.test index 4277e5c4c1..c56689ee1b 100644 --- a/test/incrblob.test +++ b/test/incrblob.test @@ -126,6 +126,11 @@ foreach AutoVacuumMode [list 0 1] { execsql "PRAGMA mmap_size = 0" execsql "PRAGMA auto_vacuum = $AutoVacuumMode" + # Extra value added to size answers + set ib2_extra 0 + if {$AutoVacuumMode} {incr ib2_extra} + if {[nonzero_reserved_bytes]} {incr ib2_extra} + do_test incrblob-2.$AutoVacuumMode.1 { set ::str [string repeat abcdefghij 2900] execsql { @@ -136,7 +141,7 @@ foreach AutoVacuumMode [list 0 1] { COMMIT; } expr [file size test.db]/1024 - } [expr 31 + $AutoVacuumMode] + } [expr 31 + $ib2_extra] ifcapable autovacuum { do_test incrblob-2.$AutoVacuumMode.2 { @@ -163,7 +168,7 @@ foreach AutoVacuumMode [list 0 1] { # sqlite uses the ptrmap pages to avoid reading the other pages. # nRead db - } [expr $AutoVacuumMode ? 4 : 30] + } [expr $AutoVacuumMode ? 4 : 30+$ib2_extra] do_test incrblob-2.$AutoVacuumMode.4 { string range [db one {SELECT v FROM blobs}] end-19 end @@ -187,7 +192,7 @@ foreach AutoVacuumMode [list 0 1] { # sqlite uses the ptrmap pages to avoid reading the other pages. # nRead db - } [expr $AutoVacuumMode ? 4 : 30] + } [expr $AutoVacuumMode ? 4 : 30 + $ib2_extra] # Pages 1 (the write-counter) and 32 (the blob data) were written. do_test incrblob-2.$AutoVacuumMode.6 { @@ -210,7 +215,7 @@ foreach AutoVacuumMode [list 0 1] { do_test incrblob-2.$AutoVacuumMode.9 { nRead db - } [expr $AutoVacuumMode ? 4 : 30] + } [expr $AutoVacuumMode ? 4 : 30 + $ib2_extra] } sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit) @@ -384,7 +389,7 @@ ifcapable vtab { ifcapable attach { do_test incrblob-5.1 { forcedelete test2.db test2.db-journal - set ::size [expr [file size [info script]]] + set ::size [expr [file size $::cmdlinearg(INFO_SCRIPT)]] execsql { ATTACH 'test2.db' AS aux; CREATE TABLE aux.files(name, text); @@ -392,16 +397,16 @@ ifcapable attach { } set fd [db incrblob aux files text 1] fconfigure $fd -translation binary - set fd2 [open [info script]] + set fd2 [open $::cmdlinearg(INFO_SCRIPT)] fconfigure $fd2 -translation binary puts -nonewline $fd [read $fd2] close $fd close $fd2 set ::text [db one {select text from aux.files}] string length $::text - } [file size [info script]] + } [file size $::cmdlinearg(INFO_SCRIPT)] do_test incrblob-5.2 { - set fd2 [open [info script]] + set fd2 [open $::cmdlinearg(INFO_SCRIPT)] fconfigure $fd2 -translation binary set ::data [read $fd2] close $fd2 @@ -576,7 +581,7 @@ foreach {tn arg} {1 "" 2 -readonly} { } -set fd [open [info script]] +set fd [open $::cmdlinearg(INFO_SCRIPT)] fconfigure $fd -translation binary set ::data [read $fd 14000] close $fd diff --git a/test/incrblob2.test b/test/incrblob2.test index b6c75cd6c7..989da147cc 100644 --- a/test/incrblob2.test +++ b/test/incrblob2.test @@ -331,6 +331,9 @@ if {$::tcl_platform(pointerSize)>=8} { # integer overflow. sqlite3_blob_read $rdHandle 2147483647 2147483647 } errmsg] + if {[string match {out of memory in *test_blob.c} $errmsg]} { + set errmsg SQLITE_ERROR + } lappend rc $errmsg } {1 SQLITE_ERROR} } diff --git a/test/incrblob3.test b/test/incrblob3.test index 5f2e860d08..a9097e01a5 100644 --- a/test/incrblob3.test +++ b/test/incrblob3.test @@ -13,6 +13,12 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl +set testprefix incrblob3 + +ifcapable !incrblob { + finish_test + return +} sqlite3 db test.db sqlite3_db_config_lookaside db 0 0 0 @@ -268,4 +274,41 @@ do_test incrblob3-7.2 { db close tvfs delete +#------------------------------------------------------------------------- +# +reset_db +forcedelete test.db2 +do_execsql_test 8.1 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b); + ATTACH 'test.db2' AS aux; + CREATE TABLE aux.t1(a INTEGER PRIMARY KEY, b); + + INSERT INTO t1 VALUES(4, 'hello'); + INSERT INTO aux.t1 VALUES(4, 'world'); +} + +do_test 8.2 { + set ::blob [db incrblob -readonly main t1 b 4] + read $::blob +} {hello} +close $::blob + +do_test 8.3 { + set ::blob [db incrblob -readonly aux t1 b 4] + read $::blob +} {world} +close $::blob + +do_test 8.4 { + set ::blob [db incrblob -readonly t1 b 4] + read $::blob +} {hello} +close $::blob + +do_test 8.5 { + list [catch { db incrblob -readonly nosuchdb t1 b 4 } msg] $msg +} {1 {no such table: nosuchdb.t1}} + + +db close finish_test diff --git a/test/incrblob4.test b/test/incrblob4.test index a92e373536..c9bcee8a3b 100644 --- a/test/incrblob4.test +++ b/test/incrblob4.test @@ -86,4 +86,122 @@ do_test 3.3 { execsql { INSERT INTO t1(v) VALUES($new) } } {} +#------------------------------------------------------------------------- +# Test that it is not possible to DROP a table with an incremental blob +# cursor open on it. +# +do_execsql_test 4.1 { + CREATE TABLE t2(a INTEGER PRIMARY KEY, b); + INSERT INTO t2 VALUES(456, '0123456789'); +} +do_test 4.2 { + set blob [db incrblob -readonly t2 b 456] + read $blob 5 +} {01234} +do_catchsql_test 4.3 { + DROP TABLE t2 +} {1 {database table is locked}} +do_test 4.4 { + sqlite3_extended_errcode db +} {SQLITE_LOCKED} +close $blob + +#------------------------------------------------------------------------- + +ifcapable preupdate { + +reset_db +do_execsql_test 5.1 { + CREATE TABLE t2(a INTEGER PRIMARY KEY, b); + INSERT INTO t2 VALUES(1000, 'abcdefghijklmnopqrstuvwxyz'); + INSERT INTO t2 VALUES(2000, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'); + INSERT INTO t2 VALUES(3000, 'abcdefghijklmnopqrstuvwxyz'); +} + +do_test 5.2.1 { + execsql BEGIN + set blob [db incrblob t2 b 2000] + seek $blob 0 + puts -nonewline $blob "hello " + flush $blob + execsql ROLLBACK +} {} + +do_test 5.2.2 { + puts -nonewline $blob "world" + set rc [catch { flush $blob } msg] + list $rc [regsub {input/output} $msg {I/O}] +} "1 {error flushing \"$blob\": I/O error}" +catch { close $blob } + +set preupdate_count 0 +proc preupdate {args} { incr ::preupdate_count ; return {} } +db preupdate hook preupdate + +set preupdate_count 0 +do_test 5.3.1 { + execsql BEGIN + set blob [db incrblob t2 b 1000] + seek $blob 0 + puts -nonewline $blob "hello " + flush $blob + execsql ROLLBACK +} {} + +do_test 5.3.2 { + puts -nonewline $blob "world" + set rc [catch { flush $blob } msg] + list $rc [regsub {input/output} $msg {I/O}] +} "1 {error flushing \"$blob\": I/O error}" +catch { close $blob } + +do_test 5.3.3 { + set ::preupdate_count +} {1} + +set preupdate_count 0 +do_test 5.4.1 { + execsql BEGIN + set blob [db incrblob t2 b 1000] + seek $blob 0 + puts -nonewline $blob "hello " + flush $blob + execsql { DELETE FROM t2 WHERE a=3000; } +} {} + +do_test 5.4.2 { + puts -nonewline $blob "world" + list [catch { flush $blob } msg] $msg +} "0 {}" +catch { close $blob } +catchsql { ROLLBACK } + +do_test 5.3.3 { + set ::preupdate_count +} {3} + +set preupdate_count 0 +do_test 5.4.3 { + execsql BEGIN + set blob [db incrblob t2 b 2000] + seek $blob 0 + puts -nonewline $blob "hello " + flush $blob + execsql { UPDATE t2 SET b='abcdefghijklmnopqrstuvwxyz' WHERE a=2000 } +} {} + +do_test 5.4.4 { + puts -nonewline $blob "world" + set rc [catch { flush $blob } msg] + list $rc [regsub {input/output} $msg {I/O}] +} "1 {error flushing \"$blob\": I/O error}" +catch { close $blob } +catchsql { ROLLBACK } + +do_test 5.4.5 { + set ::preupdate_count +} {2} + +} + finish_test diff --git a/test/incrblob_err.test b/test/incrblob_err.test index a08bea3e4e..3c523319c2 100644 --- a/test/incrblob_err.test +++ b/test/incrblob_err.test @@ -16,7 +16,7 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl set ::testprefix incrblob_err -ifcapable {!incrblob || !memdebug || !tclvar} { +ifcapable {!incrblob || !tclvar} { finish_test return } @@ -24,12 +24,12 @@ ifcapable {!incrblob || !memdebug || !tclvar} { source $testdir/malloc_common.tcl unset -nocomplain ::fd ::data -set ::fd [open [info script]] +set ::fd [open $::cmdlinearg(INFO_SCRIPT)] set ::data [read $::fd] close $::fd do_malloc_test 1 -tclprep { - set bytes [file size [info script]] + set bytes [file size $::cmdlinearg(INFO_SCRIPT)] execsql { CREATE TABLE blobs(k, v BLOB); INSERT INTO blobs VALUES(1, zeroblob($::bytes)); diff --git a/test/incrblobfault.test b/test/incrblobfault.test index 0c1d93d460..d070babe2c 100644 --- a/test/incrblobfault.test +++ b/test/incrblobfault.test @@ -14,6 +14,11 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl +ifcapable !incrblob { + finish_test + return +} + set testprefix incrblobfault do_execsql_test 1.0 { @@ -52,7 +57,7 @@ do_faultsim_test 2 -prep { error [sqlite3_errmsg db] } } -test { - faultsim_test_result {1 {no such rowid: -1}} + faultsim_test_result {1 {no such rowid: -1}} {1 {disk I/O error}} close $::blob } diff --git a/test/incrvacuum.test b/test/incrvacuum.test index 91f5c8e1e0..d06005c1f6 100644 --- a/test/incrvacuum.test +++ b/test/incrvacuum.test @@ -736,7 +736,7 @@ if {[permutation] == ""} { catchsql { PRAGMA incremental_vacuum(10); } db3 - } {1 {file is encrypted or is not a database}} + } {1 {file is not a database}} db3 close } @@ -783,4 +783,110 @@ do_test incrvacuum-15.1 { } } {ok} +#------------------------------------------------------------------------- +# At one point it was unsafe to truncate a db file on windows while there +# were outstanding xFetch() references. This test case attempts to hit +# that case. +# +ifcapable mmap { + reset_db + do_execsql_test incrvacuum-16.0 { + PRAGMA auto_vacuum = 2; + CREATE TABLE t3(a); + INSERT INTO t3 VALUES(1), (2), (3), (4); + + CREATE TABLE t2(x); + INSERT INTO t2 VALUES( randomblob(1000) ); + INSERT INTO t2 VALUES( randomblob(1000) ); + INSERT INTO t2 VALUES( randomblob(1000) ); + INSERT INTO t2 VALUES( randomblob(1000) ); + INSERT INTO t2 VALUES( randomblob(1000) ); + INSERT INTO t2 VALUES( randomblob(1000) ); + } {} + + # Reopen db to ensure the page-cache is empty. + # + db close + sqlite3 db test.db + + # Open db in mmap-mode. Open a transaction, delete some data, then run + # incremental-vacuum. Do not commit the transaction. + # + do_execsql_test incrvacuum-16.1 { + PRAGMA mmap_size = 1000000; + BEGIN; + DELETE FROM t2; + PRAGMA incremental_vacuum = 1000; + } {1000000} + + # Scan through table t3 (which is all clean pages - so mmap is used). Then, + # midway through, commit the transaction. This causes the db to be truncated + # while there are outstanding xFetch pages. + # + do_test incrvacuum-16.2 { + set res [list] + db eval { SELECT a FROM t3 } { + if {$a==3} { db eval COMMIT } + lappend res $a + } + set res + } {1 2 3 4} +} + +# 2021-04-05 dbsqlfuzz cced0668cfd4da4eb2382cb9dd26c17c64aaff76 +# +# This is an incremental vacuum database that has one free page that +# needs to be filled. After removing the last page from the end of +# the database file to fill the free page slot, the last page that +# is left is the tail of an overflow chain. +# +# But the size of the database file is shorter than the actual data +# so that after incremental vacuum runs, the file is actually too +# small to hold the last page of the overflow chain. +# +# At one point this caused an assertion fault in +# sqlite3PagerTruncateImage(). +# +do_test incrvacuum-17.0 { + sqlite3 db {} + database_may_be_corrupt + db deserialize [decode_hexdb { +| size 20480 pagesize 4096 filename x2.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 05 00 00 00 07 .....@ ........ +| 32: 00 00 00 04 00 00 00 01 00 00 00 03 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 03 00 00 00 01 00 00 00 00 ................ +| 64: 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| 80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 05 ................ +| 96: 00 2e 53 60 0d 0f dc 00 01 0f b8 00 0f b8 0f b8 ..S`............ +| 4016: 00 00 00 00 00 00 00 00 22 02 06 17 11 11 01 31 ...............1 +| 4032: 74 61 62 6c 65 74 32 74 32 03 43 52 45 41 54 45 tablet2t2.CREATE +| 4048: 20 54 41 42 4c 45 20 74 32 28 79 29 00 00 00 24 TABLE t2(y)...$ +| 4064: 11 11 01 31 74 61 62 6c 65 74 31 74 31 03 43 52 ...1tablet1t1.CR +| 4080: 45 41 54 45 20 54 41 42 4c 45 20 74 31 28 78 29 EATE TABLE t1(x) +| page 2 offset 4096 +| 0: 01 00 00 00 00 02 00 00 00 00 03 00 00 00 03 04 ................ +| 16: 00 00 00 05 03 00 00 00 03 00 00 00 00 00 00 00 ................ +| page 3 offset 8192 +| 0: 0d 00 00 00 02 05 47 00 08 dd 05 47 00 00 00 00 ......G....G.... +| 1344: 00 00 00 00 00 00 00 a7 0b 02 03 ce 1c 00 00 00 ................ +| 2256: 00 00 00 00 00 00 00 00 00 00 00 00 07 ce 14 01 ................ +| 2272: 04 81 9c 2c 00 00 00 00 00 00 00 00 00 00 00 00 ...,............ +| 4080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 05 ................ +| page 4 offset 12288 +| 0: 00 00 00 00 00 00 00 00 08 dd 05 47 00 00 00 00 ...........G.... +| 1344: 00 00 00 00 00 00 00 a7 0b 02 03 ce 1c 00 00 00 ................ +| 2256: 00 00 00 00 00 00 00 00 00 00 00 00 07 ce 14 01 ................ +| 2272: 04 81 9c 2c 00 00 00 00 00 00 00 00 00 00 00 00 ...,............ +| 4080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 05 ................ +| page 5 offset 16384 +| 0: 00 00 00 06 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| end x2.db +}]} {} +do_catchsql_test incrvacuum-17.1 { + PRAGMA writable_schema=ON; + PRAGMA incremental_vacuum(10); +} {0 {}} + finish_test diff --git a/test/incrvacuum2.test b/test/incrvacuum2.test index 2219d54195..6ec66c9deb 100644 --- a/test/incrvacuum2.test +++ b/test/incrvacuum2.test @@ -134,7 +134,7 @@ do_test incrvacuum2-3.2 { integrity_check incrvacuum2-3.3 -ifcapable wal { +if {[wal_is_capable]} { # At one point, when a specific page was being extracted from the b-tree # free-list (e.g. during an incremental-vacuum), all trunk pages that # occurred before the specific page in the free-list trunk were being diff --git a/test/incrvacuum3.test b/test/incrvacuum3.test index e901bfe1f5..b4cbc8b0c8 100644 --- a/test/incrvacuum3.test +++ b/test/incrvacuum3.test @@ -50,8 +50,8 @@ proc check_on_disk {} { set sz [file size test.db] set fd [open test.db] set fd2 [open test2.db w] - fconfigure $fd -encoding binary -translation binary - fconfigure $fd2 -encoding binary -translation binary + fconfigure $fd -translation binary + fconfigure $fd2 -translation binary if {$sz>$::sqlite_pending_byte} { puts -nonewline $fd2 [read $fd $::sqlite_pending_byte] seek $fd [expr $::sqlite_pending_byte+512] diff --git a/test/index.test b/test/index.test index 712f42c3a4..25ff41762d 100644 --- a/test/index.test +++ b/test/index.test @@ -428,7 +428,7 @@ do_test index-13.1 { } {1 2.0 3} do_test index-13.2 { set ::idxlist [execsql { - SELECT name FROM sqlite_master WHERE type="index" AND tbl_name="t5"; + SELECT name FROM sqlite_master WHERE type='index' AND tbl_name='t5'; }] llength $::idxlist } {3} @@ -625,6 +625,12 @@ do_test index-18.1 { CREATE TABLE sqlite_t1(a, b, c); } } {1 {object name reserved for internal use: sqlite_t1}} +do_test index-18.1.2 { + catchsql { + CREATE TABLE sqlite_t1(a, b, c); + } +} {1 {object name reserved for internal use: sqlite_t1}} +sqlite3_db_config db DEFENSIVE 0 do_test index-18.2 { catchsql { CREATE INDEX sqlite_i1 ON t7(c); @@ -732,6 +738,32 @@ do_test index-21.2 { } } {0 {9 5 1}} - +# 2019-05-01 ticket https://sqlite.org/src/info/3be1295b264be2fa +do_execsql_test index-22.0 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a, b TEXT); + CREATE UNIQUE INDEX IF NOT EXISTS x1 ON t1(b==0); + CREATE INDEX IF NOT EXISTS x2 ON t1(a || 0) WHERE b; + INSERT INTO t1(a,b) VALUES('a',1),('a',0); + SELECT a, b, '|' FROM t1; +} {a 1 | a 0 |} + +# 2019-05-10 ticket https://sqlite.org/src/info/ae0f637bddc5290b +do_execsql_test index-23.0 { + DROP TABLE t1; + CREATE TABLE t1(a TEXT, b REAL); + CREATE UNIQUE INDEX t1x1 ON t1(a GLOB b); + INSERT INTO t1(a,b) VALUES('0.0','1'),('1.0','1'); + SELECT * FROM t1; + REINDEX; +} {0.0 1.0 1.0 1.0} +do_execsql_test index-23.1 { + DROP TABLE t1; + CREATE TABLE t1(a REAL); + CREATE UNIQUE INDEX index_0 ON t1(TYPEOF(a)); + INSERT OR IGNORE INTO t1(a) VALUES (0.1),(FALSE); + SELECT * FROM t1; + REINDEX; +} {0.1} finish_test diff --git a/test/index3.test b/test/index3.test index 4c00a852d2..6100f48573 100644 --- a/test/index3.test +++ b/test/index3.test @@ -83,6 +83,7 @@ do_execsql_test index3-2.5 { # in the series. # do_test index3-99.1 { + sqlite3_db_config db DEFENSIVE 0 execsql { PRAGMA writable_schema=on; UPDATE sqlite_master SET sql='nonsense' WHERE name='t1d' diff --git a/test/index6.test b/test/index6.test index 33ae3d1cb1..007823a283 100644 --- a/test/index6.test +++ b/test/index6.test @@ -65,12 +65,15 @@ do_test index6-1.5 { catchsql { CREATE INDEX bad1 ON t1(a,b) WHERE a!=random(); } -} {1 {functions prohibited in partial index WHERE clauses}} +} {1 {non-deterministic functions prohibited in partial index WHERE clauses}} do_test index6-1.6 { catchsql { CREATE INDEX bad1 ON t1(a,b) WHERE a NOT LIKE 'abc%'; } -} {1 {functions prohibited in partial index WHERE clauses}} +} {0 {}} +do_execsql_test index6-1.7 { + DROP INDEX IF EXISTS bad1; +} do_test index6-1.10 { execsql { @@ -155,29 +158,29 @@ do_test index6-2.2 { EXPLAIN QUERY PLAN SELECT * FROM t2 WHERE a=5; } -} {/.* TABLE t2 USING INDEX t2a1 .*/} -ifcapable stat4||stat3 { +} {/(SEARCH|SCAN) t2 USING INDEX t2a1 /} +ifcapable stat4 { execsql ANALYZE do_test index6-2.3stat4 { execsql { EXPLAIN QUERY PLAN SELECT * FROM t2 WHERE a IS NOT NULL; } - } {/.* TABLE t2 USING INDEX t2a1 .*/} + } {/(SEARCH|SCAN) t2 USING INDEX t2a1 /} } else { do_test index6-2.3stat4 { execsql { EXPLAIN QUERY PLAN SELECT * FROM t2 WHERE a IS NOT NULL AND a>0; } - } {/.* TABLE t2 USING INDEX t2a1 .*/} + } {/(SEARCH|SCANE) t2 USING INDEX t2a1 /} } do_test index6-2.4 { execsql { EXPLAIN QUERY PLAN SELECT * FROM t2 WHERE a IS NULL; } -} {~/.*INDEX t2a1.*/} +} {~/INDEX t2a1/} do_execsql_test index6-2.101 { DROP INDEX t2a1; @@ -315,8 +318,9 @@ do_execsql_test index6-8.0 { do_eqp_test index6-8.1 { SELECT * FROM t8a LEFT JOIN t8b ON (x = 'value' AND y = a) } { - 0 0 0 {SCAN TABLE t8a} - 0 1 1 {SEARCH TABLE t8b USING INDEX i8c (y=?)} + QUERY PLAN + |--SCAN t8a + `--SEARCH t8b USING INDEX i8c (y=?) LEFT-JOIN } do_execsql_test index6-8.2 { @@ -376,4 +380,152 @@ do_execsql_test index6-10.3eqp { SELECT e FROM t10 WHERE a=1 AND b=2 ORDER BY d DESC; } {~/USING INDEX t10x/} +# A partial index will be used for a full table scan, where possible +do_execsql_test index6-11.1 { + CREATE TABLE t11(a,b,c); + CREATE INDEX t11x ON t11(a) WHERE b<>99; + EXPLAIN QUERY PLAN SELECT a FROM t11 WHERE b<>99; +} {/USING INDEX t11x/} +do_execsql_test index6-11.2 { + EXPLAIN QUERY PLAN SELECT a FROM t11 WHERE b<>99 AND c<>98; +} {/USING INDEX t11x/} + +# 2018-12-08 +# Ticket https://sqlite.org/src/info/1d958d90596593a7 +# NOT IN operator fails when using a partial index. +# +do_execsql_test index6-12.1 { + DROP TABLE IF EXISTS t1; + DROP TABLE IF EXISTS t2; + CREATE TABLE t1(a,b); + INSERT INTO t1 VALUES(1,1); + INSERT INTO t1 VALUES(2,2); + CREATE TABLE t2(x); + INSERT INTO t2 VALUES(1); + INSERT INTO t2 VALUES(2); + SELECT 'one', * FROM t2 WHERE x NOT IN (SELECT a FROM t1); + CREATE INDEX t1a ON t1(a) WHERE b=1; + SELECT 'two', * FROM t2 WHERE x NOT IN (SELECT a FROM t1); +} {} +do_execsql_test index6-12.2 { + SELECT x FROM t2 WHERE x IN (SELECT a FROM t1) ORDER BY +x; +} {1 2} + +# 2019-05-04 +# Ticket https://sqlite.org/src/tktview/5c6955204c392ae763a95 +# Theorem prover error +# +do_execsql_test index6-13.1 { + DROP TABLE IF EXISTS t0; + CREATE TABLE t0(c0); + CREATE INDEX index_0 ON t0(c0) WHERE c0 NOT NULL; + INSERT INTO t0(c0) VALUES (NULL); + SELECT * FROM t0 WHERE c0 OR 1; +} {{}} + +# 2019-05-11 +# Ticket https://sqlite.org/src/tktview/8025674847 +reset_db +do_execsql_test index6-14.1 { + CREATE TABLE IF NOT EXISTS t0 (c0, c1); + CREATE INDEX IF NOT EXISTS i0 ON t0(c0, c1) WHERE c0 NOT NULL; + INSERT INTO t0(c0, c1) VALUES(NULL, 'row'); + SELECT * FROM t0 WHERE t0.c0 IS NOT 1; +} {{} row} + +do_execsql_test index6-14.2 { + SELECT * FROM t0 WHERE CASE c0 WHEN 0 THEN 0 ELSE 1 END; +} {{} row} + +# 2019-08-30 +# Ticket https://sqlite.org/src/info/a6408d42b9f44462 +# Ticket https://sqlite.org/src/info/fba33c8b1df6a915 +# https://sqlite.org/src/info/bac716244fddac1fe841 +# +do_execsql_test index6-15.1 { + DROP TABLE t0; + CREATE TABLE t0(c0); + INSERT INTO t0(c0) VALUES (NULL); + CREATE INDEX i0 ON t0(1) WHERE c0 NOT NULL; + SELECT 1 FROM t0 WHERE (t0.c0 IS FALSE) IS FALSE; +} {1} +do_execsql_test index6-15.2 { + SELECT 1 FROM t0 WHERE (t0.c0 IS FALSE) BETWEEN FALSE AND TRUE; +} {1} +do_execsql_test index6-15.3 { + SELECT 1 FROM t0 WHERE TRUE BETWEEN (t0.c0 IS FALSE) AND TRUE; +} {1} +do_execsql_test index6-15.4 { + SELECT 1 FROM t0 WHERE FALSE BETWEEN FALSE AND (t0.c0 IS FALSE); +} {1} +do_execsql_test index6-15.5 { + SELECT 1 FROM t0 WHERE (c0 IS FALSE) IN (FALSE); +} {1} + +# 2019-09-03 +# Ticket https://sqlite.org/src/info/767a8cbc6d20bd68 +do_execsql_test index6-16.1 { + DROP TABLE t0; + CREATE TABLE t0(c0 COLLATE NOCASE, c1); + CREATE INDEX i0 ON t0(0) WHERE c0 >= c1; + INSERT INTO t0 VALUES('a', 'B'); + SELECT c1 <= c0, c0 >= c1 FROM t0; +} {1 0} +do_execsql_test index6-16.2 { + SELECT 2 FROM t0 WHERE c0 >= c1; +} {} +do_execsql_test index6-16.3 { + SELECT 3 FROM t0 WHERE c1 <= c0; +} {3} + +# 2019-11-02 +# Ticket https://sqlite.org/src/tktview/a9efb42811fa41ee286e8 +db close +sqlite3 db :memory: +do_execsql_test index6-17.1 { + CREATE TABLE t0(c0); + CREATE INDEX i0 ON t0(0) WHERE c0 GLOB c0; + INSERT INTO t0 VALUES (0); + CREATE UNIQUE INDEX i1 ON t0(0); + PRAGMA integrity_check; +} {ok} +do_execsql_test index6-17.2 { + CREATE UNIQUE INDEX i2 ON t0(0); + REPLACE INTO t0 VALUES(0); + PRAGMA integrity_check; +} {ok} +do_execsql_test index6-17.3 { + SELECT COUNT(*) FROM t0 WHERE t0.c0 GLOB t0.c0; +} {1} + +# 2021-05-29 +# Forum https://sqlite.org/forum/forumpost/d813704d7c +reset_db +do_execsql_test index6-18.1 { + CREATE TABLE t1(a INT, b INT); + INSERT INTO t1 VALUES(10,10); + CREATE UNIQUE INDEX t1b ON t1(b) WHERE a>NULL; + SELECT * FROM t1 WHERE a IS NOT NULL; +} {10 10} + +# 2022-06-09 +# https://sqlite.org/forum/forumpost/c4676c4956 +# Cannot do a scan of a partial index on the left table of a RIGHT JOIN +# since that will cause extra rows to appear in the output during the +# right-join no-match loop. The following testcase is verify using +# PostgreSQL 14. +# +reset_db +do_execsql_test index6-19.1 { + CREATE TABLE t1(a INT, b INT); + INSERT INTO t1(a) VALUES(2); + CREATE TABLE t2(c INT); + CREATE INDEX i0 ON t2(c) WHERE c=3; + CREATE TABLE t3(d INT); + INSERT INTO t3 VALUES(1); +} +do_execsql_test index6-19.2 { + SELECT * FROM t2 RIGHT JOIN t3 ON d<>0 LEFT JOIN t1 ON c=3 WHERE t1.a<>0; +} {} + finish_test diff --git a/test/index7.test b/test/index7.test index 557fe21324..eeb36bab5c 100644 --- a/test/index7.test +++ b/test/index7.test @@ -99,12 +99,25 @@ do_test index7-1.5 { catchsql { CREATE INDEX bad1 ON t1(a,b) WHERE a!=random(); } -} {1 {functions prohibited in partial index WHERE clauses}} +} {1 {non-deterministic functions prohibited in partial index WHERE clauses}} do_test index7-1.6 { catchsql { CREATE INDEX bad1 ON t1(a,b) WHERE a NOT LIKE 'abc%'; } -} {1 {functions prohibited in partial index WHERE clauses}} +} {0 {}} +do_execsql_test index7-1.7 { + INSERT INTO t1(a,b,c) + VALUES('abcde',1,101),('abdef',2,102),('xyz',3,103),('abcz',4,104); + SELECT c FROM t1 WHERE a NOT LIKE 'abc%' AND a=7 ORDER BY +b; +} {7} +do_execsql_test index7-1.7eqp { + EXPLAIN QUERY PLAN + SELECT b FROM t1 WHERE a NOT LIKE 'abc%' AND a=7 ORDER BY +b; +} {/SEARCH t1 USING COVERING INDEX bad1 /} +do_execsql_test index7-1.8 { + DELETE FROM t1 WHERE c>=101; + DROP INDEX IF EXISTS bad1; +} {} do_test index7-1.10 { execsql { @@ -173,7 +186,7 @@ do_test index7-1.15 { } } {t1 {15 1} t1a {10 1} t1b {8 1} t1c {15 1} ok} -# Queries use partial indices as appropriate times. +# Queries use partial indices at appropriate times. # do_test index7-2.1 { execsql { @@ -189,28 +202,28 @@ do_test index7-2.2 { EXPLAIN QUERY PLAN SELECT * FROM t2 WHERE a=5; } -} {/.* TABLE t2 USING COVERING INDEX t2a1 .*/} -ifcapable stat4||stat3 { +} {/(SCAN|SEARCH) t2 USING COVERING INDEX t2a1 /} +ifcapable stat4 { do_test index7-2.3stat4 { execsql { EXPLAIN QUERY PLAN SELECT * FROM t2 WHERE a IS NOT NULL; } - } {/.* TABLE t2 USING COVERING INDEX t2a1 .*/} + } {/(SCAN|SEARCH) t2 USING COVERING INDEX t2a1 /} } else { do_test index7-2.3stat4 { execsql { EXPLAIN QUERY PLAN SELECT * FROM t2 WHERE a IS NOT NULL AND a>0; } - } {/.* TABLE t2 USING COVERING INDEX t2a1 .*/} + } {/(SCAN|SEARCH) t2 USING COVERING INDEX t2a1 /} } do_test index7-2.4 { execsql { EXPLAIN QUERY PLAN SELECT * FROM t2 WHERE a IS NULL; } -} {~/.*INDEX t2a1.*/} +} {~/INDEX t2a1/} do_execsql_test index7-2.101 { DROP INDEX t2a1; @@ -308,12 +321,35 @@ do_execsql_test index7-6.3 { } do_eqp_test index7-6.4 { SELECT * FROM v4 WHERE d='xyz' AND c='def' -} { - 0 0 0 {SEARCH TABLE t4 USING INDEX i4 (c=?)} -} +} {SEARCH t4 USING INDEX i4 (c=?)} + do_catchsql_test index7-6.5 { CREATE INDEX t5a ON t5(a) WHERE a=#1; } {1 {near "#1": syntax error}} +do_execsql_test index7-7.0 { + CREATE TABLE t6(x, y); + INSERT INTO t6 VALUES(1, 1); + INSERT INTO t6 VALUES(0, 0); + SELECT * FROM t6 WHERE y IS TRUE ORDER BY x; +} {1 1} + +do_execsql_test index7-7.1 { + CREATE INDEX i6 ON t6(x) WHERE y IS NOT TRUE; + SELECT * FROM t6 WHERE y IS TRUE ORDER BY x; +} {1 1} + +# 2020-05-27. tag-20200527-1. +# Incomplete stat1 information on a table with few rows should still use the +# index. +reset_db +do_execsql_test index7-8.1 { + CREATE TABLE t1(x INTEGER PRIMARY KEY, y); + CREATE INDEX t1y ON t1(y) WHERE y IS NOT NULL; + INSERT INTO t1(x) VALUES(1),(2); + ANALYZE; + EXPLAIN QUERY PLAN SELECT 1 FROM t1 WHERE y=5; +} {/SEARCH t1 USING COVERING INDEX t1y/} + finish_test diff --git a/test/index8.test b/test/index8.test new file mode 100644 index 0000000000..41ccf4bbcf --- /dev/null +++ b/test/index8.test @@ -0,0 +1,60 @@ +# 2016-07-27 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# Test cases for ORDER BY and LIMIT on an index scan. +# + + +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +# Performance regression reported at +# http://www.mail-archive.com/sqlite-users@mailinglists.sqlite.org/msg98615.html +# +# Caused by the ORDER BY LIMIT optionation for check-in +# https://sqlite.org/src/info/bf46179d44843769 +# +# Fixed on approximately 2016-07-27 by changes that compute a better score +# for index scans by taking into account WHERE clause constraints that can +# be handled by the index and do not require a table lookup. +# +do_execsql_test 1.0 { + CREATE TABLE t1(a,b,c,d); + WITH RECURSIVE c(x) AS (VALUES(0) UNION ALL SELECT x+1 FROM c WHERE x<100) + INSERT INTO t1(a,b,c,d) + SELECT x/10, x%10, x%19, x FROM c; + CREATE INDEX t1abc ON t1(a,b,c); + SELECT * FROM t1 WHERE c=4 ORDER BY a, b LIMIT 2; +} {0 4 4 4 2 3 4 23} + +# Prior to the fix, the following EQP would show a table scan and a sort +# rather than an index scan. +# +do_execsql_test 1.0eqp { + EXPLAIN QUERY PLAN + SELECT * FROM t1 WHERE c=4 ORDER BY a, b LIMIT 2; +} {/SCAN t1 USING INDEX t1abc/} + +# If we change the index so that it no longer covers the WHERE clause, +# then we should (correctly) revert to using a table scan. +# +do_execsql_test 1.1 { + DROP INDEX t1abc; + CREATE INDEX t1abd ON t1(a,b,d); + SELECT * FROM t1 WHERE c=4 ORDER BY a, b LIMIT 2; +} {0 4 4 4 2 3 4 23} +do_execsql_test 1.1eqp { + EXPLAIN QUERY PLAN + SELECT * FROM t1 WHERE c=4 ORDER BY a, b LIMIT 2; +} {~/USING INDEX/} + + +finish_test diff --git a/test/index9.test b/test/index9.test new file mode 100644 index 0000000000..00e99a8e5c --- /dev/null +++ b/test/index9.test @@ -0,0 +1,96 @@ +# 2017 Jun 24 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# Test that partial indexes work with bound variables. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix index9 + +proc sqluses {sql} { + array unset ::T + uplevel [list db eval "EXPLAIN $sql" a { + if {$a(opcode)=="OpenRead"} { set ::T($a(p2)) 1 } + }] + + set in [join [array names ::T] ,] + db eval "SELECT name FROM sqlite_master WHERE rootpage IN ($in) ORDER BY 1" +} + +proc do_sqluses_test {tn sql objects} { + uplevel [list do_test $tn [list sqluses $sql] $objects] +} + +do_execsql_test 1.0 { + CREATE TABLE t1(x, y); + CREATE INDEX t1x ON t1(x) WHERE y=45; +} +unset -nocomplain a +set y [expr 45] +do_sqluses_test 1.1 { SELECT * FROM t1 WHERE x=? AND y=$y } {t1 t1x} +set y [expr 45.1] +do_sqluses_test 1.2 { SELECT * FROM t1 WHERE x=? AND y=$y } {t1} +set y [expr 44] +do_sqluses_test 1.3 { SELECT * FROM t1 WHERE x=? AND y=$y } {t1} +unset -nocomplain y +do_sqluses_test 1.4 { SELECT * FROM t1 WHERE x=? AND y=$y } {t1} +set y [string range "45" 0 end] +do_sqluses_test 1.5 { SELECT * FROM t1 WHERE x=? AND y=$y } {t1} + +do_execsql_test 2.0 { + CREATE INDEX t1x2 ON t1(x) WHERE y=-20111000111 +} +do_sqluses_test 2.1 { SELECT * FROM t1 WHERE y=$y ORDER BY x } {t1} +set y [expr -20111000111] +do_sqluses_test 2.2 { SELECT * FROM t1 WHERE y=$y ORDER BY x } {t1 t1x2} +set y [expr -20111000110] +do_sqluses_test 2.3 { SELECT * FROM t1 WHERE y=$y ORDER BY x } {t1} +set y [expr -20111000112] +do_sqluses_test 2.4 { SELECT * FROM t1 WHERE y=$y ORDER BY x } {t1} + +do_execsql_test 3.0 { + CREATE INDEX t1x3 ON t1(x) WHERE y=9223372036854775807 +} +set y [expr 9223372036854775807] +do_sqluses_test 3.1 { SELECT * FROM t1 WHERE y=$y ORDER BY x } {t1 t1x3} +set y [expr 9223372036854775808] +do_sqluses_test 3.2 { SELECT * FROM t1 WHERE y=$y ORDER BY x } {t1} +set y [expr 9223372036854775806] +do_sqluses_test 3.3 { SELECT * FROM t1 WHERE y=$y ORDER BY x } {t1} +db cache flush +sqlite3_db_config db QPSG 1 +set y [expr 9223372036854775807] +do_sqluses_test 3.4 { SELECT * FROM t1 WHERE y=$y ORDER BY x } {t1} +set y [expr 9223372036854775808] +do_sqluses_test 3.5 { SELECT * FROM t1 WHERE y=$y ORDER BY x } {t1} +sqlite3_db_config db QPSG 0 +db cache flush + + +do_execsql_test 4.0 { + CREATE INDEX t1x4 ON t1(x) WHERE y=-9223372036854775808 +} +set y [expr -9223372036854775808] +do_sqluses_test 4.1 { SELECT * FROM t1 WHERE y=$y ORDER BY x } {t1 t1x4} +set y [expr -9223372036854775807] +do_sqluses_test 4.2 { SELECT * FROM t1 WHERE y=$y ORDER BY x } {t1} +set y [expr -9223372036854775809] +do_sqluses_test 4.3 { SELECT * FROM t1 WHERE y=$y ORDER BY x } {t1} +set y [expr -9223372036854775808] +do_sqluses_test 4.4 { SELECT * FROM t1 WHERE $y=y ORDER BY x } {t1 t1x4} +db cache flush +sqlite3_db_config db QPSG 1 +do_sqluses_test 4.5 { SELECT * FROM t1 WHERE $y=y ORDER BY x } {t1} +sqlite3_db_config db QPSG 0 +db cache flush + +finish_test diff --git a/test/indexA.test b/test/indexA.test new file mode 100644 index 0000000000..518d7e18ad --- /dev/null +++ b/test/indexA.test @@ -0,0 +1,350 @@ +# 2023 September 23 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix indexA + +do_execsql_test 1.0 { + CREATE TABLE t1(a TEXT, b, c); + CREATE INDEX i1 ON t1(b, c) WHERE a='abc'; + INSERT INTO t1 VALUES('abc', 1, 2); +} + +do_execsql_test 1.1 { + SELECT * FROM t1 WHERE a='abc' +} {abc 1 2} + +do_eqp_test 1.2 { + SELECT * FROM t1 WHERE a='abc' +} {USING COVERING INDEX i1} + +do_execsql_test 1.3 { + CREATE INDEX i2 ON t1(b, c) WHERE a=5; + INSERT INTO t1 VALUES(5, 4, 3); + + SELECT a, typeof(a), b, c FROM t1 WHERE a=5; +} {5 text 4 3} + +do_execsql_test 1.4 { + CREATE TABLE t2(x); + INSERT INTO t2 VALUES('v'); +} + +do_execsql_test 1.5 { + SELECT x, a, b, c FROM t2 LEFT JOIN t1 ON (a=5 AND b=x) +} {v {} {} {}} + +do_execsql_test 1.6 { + SELECT x, a, b, c FROM t2 RIGHT JOIN t1 ON (t1.a=5 AND t1.b=t2.x) +} {{} abc 1 2 {} 5 4 3} + +do_eqp_test 1.7 { + SELECT x, a, b, c FROM t2 RIGHT JOIN t1 ON (t1.a=5 AND t1.b=t2.x) +} {USING INDEX i2} + +#------------------------------------------------------------------------- +reset_db + +do_execsql_test 2.0 { + CREATE TABLE x1(a TEXT, b, c); + INSERT INTO x1 VALUES('2', 'two', 'ii'); + INSERT INTO x1 VALUES('2.0', 'twopointoh', 'ii.0'); + + CREATE TABLE x2(a NUMERIC, b, c); + INSERT INTO x2 VALUES('2', 'two', 'ii'); + INSERT INTO x2 VALUES('2.0', 'twopointoh', 'ii.0'); + + CREATE TABLE x3(a REAL, b, c); + INSERT INTO x3 VALUES('2', 'two', 'ii'); + INSERT INTO x3 VALUES('2.0', 'twopointoh', 'ii.0'); +} + +foreach {tn idx} { + 0 { + } + 1 { + CREATE INDEX i1 ON x1(b, c) WHERE a=2; + CREATE INDEX i2 ON x2(b, c) WHERE a=2; + CREATE INDEX i3 ON x3(b, c) WHERE a=2; + } + 2 { + CREATE INDEX i1 ON x1(b, c) WHERE a=2.0; + CREATE INDEX i2 ON x2(b, c) WHERE a=2.0; + CREATE INDEX i3 ON x3(b, c) WHERE a=2.0; + } + 3 { + CREATE INDEX i1 ON x1(b, c) WHERE a='2.0'; + CREATE INDEX i2 ON x2(b, c) WHERE a='2.0'; + CREATE INDEX i3 ON x3(b, c) WHERE a='2.0'; + } + 4 { + CREATE INDEX i1 ON x1(b, c) WHERE a='2'; + CREATE INDEX i2 ON x2(b, c) WHERE a='2'; + CREATE INDEX i3 ON x3(b, c) WHERE a='2'; + } +} { + execsql { DROP INDEX IF EXISTS i1 } + execsql { DROP INDEX IF EXISTS i2 } + execsql { DROP INDEX IF EXISTS i3 } + + execsql $idx + do_execsql_test 2.1.$tn.1 { + SELECT *, typeof(a) FROM x1 WHERE a=2 + } {2 two ii text} + do_execsql_test 2.1.$tn.2 { + SELECT *, typeof(a) FROM x1 WHERE a=2.0 + } {2.0 twopointoh ii.0 text} + do_execsql_test 2.1.$tn.3 { + SELECT *, typeof(a) FROM x1 WHERE a='2' + } {2 two ii text} + do_execsql_test 2.1.$tn.4 { + SELECT *, typeof(a) FROM x1 WHERE a='2.0' + } {2.0 twopointoh ii.0 text} + + do_execsql_test 2.1.$tn.5 { + SELECT *, typeof(a) FROM x2 WHERE a=2 + } {2 two ii integer 2 twopointoh ii.0 integer} + do_execsql_test 2.1.$tn.6 { + SELECT *, typeof(a) FROM x2 WHERE a=2.0 + } {2 two ii integer 2 twopointoh ii.0 integer} + do_execsql_test 2.1.$tn.7 { + SELECT *, typeof(a) FROM x2 WHERE a='2' + } {2 two ii integer 2 twopointoh ii.0 integer} + do_execsql_test 2.1.$tn.8 { + SELECT *, typeof(a) FROM x2 WHERE a='2.0' + } {2 two ii integer 2 twopointoh ii.0 integer} + + do_execsql_test 2.1.$tn.9 { + SELECT *, typeof(a) FROM x3 WHERE a=2 + } {2.0 two ii real 2.0 twopointoh ii.0 real} + do_execsql_test 2.1.$tn.10 { + SELECT *, typeof(a) FROM x3 WHERE a=2.0 + } {2.0 two ii real 2.0 twopointoh ii.0 real} + do_execsql_test 2.1.$tn.11 { + SELECT *, typeof(a) FROM x3 WHERE a='2' + } {2.0 two ii real 2.0 twopointoh ii.0 real} + do_execsql_test 2.1.$tn.12 { + SELECT *, typeof(a) FROM x3 WHERE a='2.0' + } {2.0 two ii real 2.0 twopointoh ii.0 real} + +} + +reset_db +do_execsql_test 3.0 { + CREATE TABLE x1(a TEXT, d PRIMARY KEY, b, c) WITHOUT ROWID; + INSERT INTO x1 VALUES('2', 1, 'two', 'ii'); + INSERT INTO x1 VALUES('2.0', 2, 'twopointoh', 'ii.0'); + + CREATE TABLE x2(a NUMERIC, b, c, d PRIMARY KEY) WITHOUT ROWID; + INSERT INTO x2 VALUES('2', 'two', 'ii', 1); + INSERT INTO x2 VALUES('2.0', 'twopointoh', 'ii.0', 2); + + CREATE TABLE x3(d PRIMARY KEY, a REAL, b, c) WITHOUT ROWID; + INSERT INTO x3 VALUES(34, '2', 'two', 'ii'); + INSERT INTO x3 VALUES(35, '2.0', 'twopointoh', 'ii.0'); +} + +foreach {tn idx} { + 0 { + } + 1 { + CREATE INDEX i1 ON x1(b, c) WHERE a=2; + CREATE INDEX i2 ON x2(b, c) WHERE a=2; + CREATE INDEX i3 ON x3(b, c) WHERE a=2; + } + 2 { + CREATE INDEX i1 ON x1(b, c) WHERE a=2.0; + CREATE INDEX i2 ON x2(b, c) WHERE a=2.0; + CREATE INDEX i3 ON x3(b, c) WHERE a=2.0; + } + 3 { + CREATE INDEX i1 ON x1(b, c) WHERE a='2.0'; + CREATE INDEX i2 ON x2(b, c) WHERE a='2.0'; + CREATE INDEX i3 ON x3(b, c) WHERE a='2.0'; + } + 4 { + CREATE INDEX i1 ON x1(b, c) WHERE a='2'; + CREATE INDEX i2 ON x2(b, c) WHERE a='2'; + CREATE INDEX i3 ON x3(b, c) WHERE a='2'; + } +} { + execsql { DROP INDEX IF EXISTS i1 } + execsql { DROP INDEX IF EXISTS i2 } + execsql { DROP INDEX IF EXISTS i3 } + + execsql $idx + do_execsql_test 3.1.$tn.1 { + SELECT a, b, c, typeof(a) FROM x1 WHERE a=2 + } {2 two ii text} + do_execsql_test 3.1.$tn.2 { + SELECT a, b, c, typeof(a) FROM x1 WHERE a=2.0 + } {2.0 twopointoh ii.0 text} + do_execsql_test 3.1.$tn.3 { + SELECT a, b, c, typeof(a) FROM x1 WHERE a='2' + } {2 two ii text} + do_execsql_test 3.1.$tn.4 { + SELECT a, b, c, typeof(a) FROM x1 WHERE a='2.0' + } {2.0 twopointoh ii.0 text} + + do_execsql_test 3.1.$tn.5 { + SELECT a, b, c, typeof(a) FROM x2 WHERE a=2 + } {2 two ii integer 2 twopointoh ii.0 integer} + do_execsql_test 3.1.$tn.6 { + SELECT a, b, c, typeof(a) FROM x2 WHERE a=2.0 + } {2 two ii integer 2 twopointoh ii.0 integer} + do_execsql_test 3.1.$tn.7 { + SELECT a, b, c, typeof(a) FROM x2 WHERE a='2' + } {2 two ii integer 2 twopointoh ii.0 integer} + do_execsql_test 3.1.$tn.8 { + SELECT a, b, c, typeof(a) FROM x2 WHERE a='2.0' + } {2 two ii integer 2 twopointoh ii.0 integer} + + do_execsql_test 3.1.$tn.9 { + SELECT a, b, c, typeof(a) FROM x3 WHERE a=2 + } {2.0 two ii real 2.0 twopointoh ii.0 real} + do_execsql_test 3.1.$tn.10 { + SELECT a, b, c, typeof(a) FROM x3 WHERE a=2.0 + } {2.0 two ii real 2.0 twopointoh ii.0 real} + do_execsql_test 3.1.$tn.11 { + SELECT a, b, c, typeof(a) FROM x3 WHERE a='2' + } {2.0 two ii real 2.0 twopointoh ii.0 real} + do_execsql_test 3.1.$tn.12 { + SELECT a, b, c, typeof(a) FROM x3 WHERE a='2.0' + } {2.0 two ii real 2.0 twopointoh ii.0 real} +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 4.0 { + CREATE TABLE t2(a INTEGER, b TEXT); + INSERT INTO t2 VALUES(1, 'two'); + INSERT INTO t2 VALUES(2, 'two'); + INSERT INTO t2 VALUES(3, 'two'); + INSERT INTO t2 VALUES(1, 'three'); + INSERT INTO t2 VALUES(2, 'three'); + INSERT INTO t2 VALUES(3, 'three'); + + CREATE INDEX t2a_two ON t2(a) WHERE b='two'; +} + +# explain_i { SELECT sum(a), b FROM t2 WHERE b='two' } +do_execsql_test 4.1.1 { + SELECT sum(a), b FROM t2 WHERE b='two' +} {6 two} +do_eqp_test 4.1.2 { + SELECT sum(a), b FROM t2 WHERE b='two' +} {USING COVERING INDEX t2a_two} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 5.0 { + CREATE TABLE t1(a INTEGER PRIMQRY KEY, b, c); +} +do_catchsql_test 5.1 { + CREATE INDEX ex1 ON t1(c) WHERE b IS 'abc' COLLATE g; +} {1 {no such collation sequence: g}} + +proc xyz {lhs rhs} { + return [string compare $lhs $rhs] +} +db collate xyz xyz +do_execsql_test 5.2 { + CREATE INDEX ex1 ON t1(c) WHERE b IS 'abc' COLLATE xyz; +} +db close +sqlite3 db test.db +do_execsql_test 5.3 { + SELECT * FROM t1 +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 6.0 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c); + CREATE TABLE t2(x INTEGER PRIMARY KEY, y INTEGER, z INTEGER); + INSERT INTO t1 VALUES(1, 1, 1); + INSERT INTO t1 VALUES(2, 1, 2); + INSERT INTO t2 VALUES(1, 5, 1); + INSERT INTO t2 VALUES(2, 5, 2); + + CREATE INDEX t2z ON t2(z) WHERE y=5; +} + +do_execsql_test 6.1 { + ANALYZE; + UPDATE sqlite_stat1 SET stat = '50 1' WHERE idx='t2z'; + UPDATE sqlite_stat1 SET stat = '50' WHERE tbl='t2' AND idx IS NULL; + UPDATE sqlite_stat1 SET stat = '5000' WHERE tbl='t1' AND idx IS NULL; + ANALYZE sqlite_schema; +} + +do_execsql_test 6.2 { + SELECT * FROM t1, t2 WHERE b=1 AND z=c AND y=5; +} { + 1 1 1 1 5 1 + 2 1 2 2 5 2 +} + +do_eqp_test 6.3 { + SELECT * FROM t1, t2 WHERE b=1 AND z=c AND y=5; +} {BLOOM FILTER ON t2} + +do_execsql_test 6.4 { + SELECT * FROM t1 LEFT JOIN t2 ON (y=5) WHERE b=1 AND z IS c; +} { + 1 1 1 1 5 1 + 2 1 2 2 5 2 +} + +do_eqp_test 6.5 { + SELECT * FROM t1 LEFT JOIN t2 ON (y=5) WHERE b=1 AND z IS c; +} {BLOOM FILTER ON t2} + +do_execsql_test 6.6 { + CREATE INDEX t2yz ON t2(y, z) WHERE y=5; +} + +do_execsql_test 6.7 { + SELECT * FROM t1 LEFT JOIN t2 ON (y=5) WHERE b=1 AND z IS c; +} { + 1 1 1 1 5 1 + 2 1 2 2 5 2 +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 7.0 { + CREATE TABLE t1(i INTEGER PRIMARY KEY, b TEXT, c TEXT); + CREATE INDEX i1 ON t1(c) WHERE b='abc' AND i=5; + INSERT INTO t1 VALUES(5, 'abc', 'xyz'); + SELECT * FROM t1 INDEXED BY i1 WHERE b='abc' AND i=5 ORDER BY c; +} {5 abc xyz} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 8.0 { + CREATE TABLE t1(a, b, c); + CREATE INDEX ex2 ON t1(a, 4); + CREATE INDEX ex1 ON t1(a) WHERE 4=b; + INSERT INTO t1 VALUES(1, 4, 1); + INSERT INTO t1 VALUES(1, 5, 1); + INSERT INTO t1 VALUES(2, 4, 2); +} +do_execsql_test 8.1 { + SELECT * FROM t1 WHERE b=4; +} { + 1 4 1 2 4 2 +} + +finish_test diff --git a/test/indexedby.test b/test/indexedby.test index 83c7a5cccc..de4bdaf185 100644 --- a/test/indexedby.test +++ b/test/indexedby.test @@ -40,17 +40,18 @@ proc EQP {sql} { # These tests are to check that "EXPLAIN QUERY PLAN" is working as expected. # -do_execsql_test indexedby-1.2 { - EXPLAIN QUERY PLAN select * from t1 WHERE a = 10; -} {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (a=?)}} -do_execsql_test indexedby-1.3 { - EXPLAIN QUERY PLAN select * from t1 ; -} {0 0 0 {SCAN TABLE t1}} -do_execsql_test indexedby-1.4 { - EXPLAIN QUERY PLAN select * from t1, t2 WHERE c = 10; +do_eqp_test indexedby-1.2 { + select * from t1 WHERE a = 10; +} {SEARCH t1 USING INDEX i1 (a=?)} +do_eqp_test indexedby-1.3 { + select * from t1 ; +} {SCAN t1} +do_eqp_test indexedby-1.4 { + select * from t1, t2 WHERE c = 10; } { - 0 0 1 {SEARCH TABLE t2 USING INDEX i3 (c=?)} - 0 1 0 {SCAN TABLE t1} + QUERY PLAN + |--SEARCH t2 USING INDEX i3 (c=?) + `--SCAN t1 } # Parser tests. Test that an INDEXED BY or NOT INDEX clause can be @@ -58,7 +59,7 @@ do_execsql_test indexedby-1.4 { # SQL view. Also test that specifying an index that does not exist or # is attached to a different table is detected as an error. # -# EVIDENCE-OF: R-07004-11522 -- syntax diagram qualified-table-name +# X-EVIDENCE-OF: R-07004-11522 -- syntax diagram qualified-table-name # # EVIDENCE-OF: R-58230-57098 The "INDEXED BY index-name" phrase # specifies that the named index must be used in order to look up values @@ -89,12 +90,12 @@ do_test indexedby-2.4 { catchsql { SELECT * FROM t1 INDEXED BY i3 WHERE a = 'one' AND b = 'two'} } {1 {no such index: i3}} -# EVIDENCE-OF: R-62112-42456 If the query optimizer is unable to use the -# index specified by the INDEX BY clause, then the query will fail with -# an error. +# EVIDENCE-OF: R-05301-32681 If the query optimizer is unable to use the +# index specified by the INDEXED BY clause, then the query will fail +# with an error. do_test indexedby-2.4.1 { catchsql { SELECT b FROM t1 INDEXED BY i1 WHERE b = 'two' } -} {1 {no query solution}} +} {0 {}} do_test indexedby-2.5 { catchsql { SELECT * FROM t1 INDEXED BY i5 WHERE a = 'one' AND b = 'two'} @@ -115,31 +116,29 @@ do_test indexedby-2.7 { # the rowid can still be used to look up entries even when "NOT INDEXED" # is specified. # -do_execsql_test indexedby-3.1 { - EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a = 'one' AND b = 'two' -} {/SEARCH TABLE t1 USING INDEX/} -do_execsql_test indexedby-3.1.1 { - EXPLAIN QUERY PLAN SELECT * FROM t1 NOT INDEXED WHERE a = 'one' AND b = 'two' -} {0 0 0 {SCAN TABLE t1}} -do_execsql_test indexedby-3.1.2 { - EXPLAIN QUERY PLAN SELECT * FROM t1 NOT INDEXED WHERE rowid=1 -} {/SEARCH TABLE t1 USING INTEGER PRIMARY KEY .rowid=/} +do_eqp_test indexedby-3.1 { + SELECT * FROM t1 WHERE a = 'one' AND b = 'two' +} {/SEARCH t1 USING INDEX/} +do_eqp_test indexedby-3.1.1 { + SELECT * FROM t1 NOT INDEXED WHERE a = 'one' AND b = 'two' +} {SCAN t1} +do_eqp_test indexedby-3.1.2 { + SELECT * FROM t1 NOT INDEXED WHERE rowid=1 +} {/SEARCH t1 USING INTEGER PRIMARY KEY .rowid=/} -do_execsql_test indexedby-3.2 { - EXPLAIN QUERY PLAN +do_eqp_test indexedby-3.2 { SELECT * FROM t1 INDEXED BY i1 WHERE a = 'one' AND b = 'two' -} {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (a=?)}} -do_execsql_test indexedby-3.3 { - EXPLAIN QUERY PLAN +} {SEARCH t1 USING INDEX i1 (a=?)} +do_eqp_test indexedby-3.3 { SELECT * FROM t1 INDEXED BY i2 WHERE a = 'one' AND b = 'two' -} {0 0 0 {SEARCH TABLE t1 USING INDEX i2 (b=?)}} +} {SEARCH t1 USING INDEX i2 (b=?)} do_test indexedby-3.4 { catchsql { SELECT * FROM t1 INDEXED BY i2 WHERE a = 'one' } -} {1 {no query solution}} +} {0 {}} do_test indexedby-3.5 { catchsql { SELECT * FROM t1 INDEXED BY i2 ORDER BY a } -} {1 {no query solution}} +} {0 {}} do_test indexedby-3.6 { catchsql { SELECT * FROM t1 INDEXED BY i1 WHERE a = 'one' } } {0 {}} @@ -147,57 +146,57 @@ do_test indexedby-3.7 { catchsql { SELECT * FROM t1 INDEXED BY i1 ORDER BY a } } {0 {}} -do_execsql_test indexedby-3.8 { - EXPLAIN QUERY PLAN +do_eqp_test indexedby-3.8 { SELECT * FROM t3 INDEXED BY sqlite_autoindex_t3_1 ORDER BY e -} {0 0 0 {SCAN TABLE t3 USING INDEX sqlite_autoindex_t3_1}} -do_execsql_test indexedby-3.9 { - EXPLAIN QUERY PLAN +} {SCAN t3 USING INDEX sqlite_autoindex_t3_1} +do_eqp_test indexedby-3.9 { SELECT * FROM t3 INDEXED BY sqlite_autoindex_t3_1 WHERE e = 10 -} {0 0 0 {SEARCH TABLE t3 USING INDEX sqlite_autoindex_t3_1 (e=?)}} +} {SEARCH t3 USING INDEX sqlite_autoindex_t3_1 (e=?)} do_test indexedby-3.10 { catchsql { SELECT * FROM t3 INDEXED BY sqlite_autoindex_t3_1 WHERE f = 10 } -} {1 {no query solution}} +} {0 {}} do_test indexedby-3.11 { catchsql { SELECT * FROM t3 INDEXED BY sqlite_autoindex_t3_2 WHERE f = 10 } } {1 {no such index: sqlite_autoindex_t3_2}} # Tests for multiple table cases. # -do_execsql_test indexedby-4.1 { - EXPLAIN QUERY PLAN SELECT * FROM t1, t2 WHERE a = c +do_eqp_test indexedby-4.1 { + SELECT * FROM t1, t2 WHERE a = c } { - 0 0 0 {SCAN TABLE t1} - 0 1 1 {SEARCH TABLE t2 USING INDEX i3 (c=?)} + QUERY PLAN + |--SCAN t1 + `--SEARCH t2 USING INDEX i3 (c=?) } -do_execsql_test indexedby-4.2 { - EXPLAIN QUERY PLAN SELECT * FROM t1 INDEXED BY i1, t2 WHERE a = c +do_eqp_test indexedby-4.2 { + SELECT * FROM t1 INDEXED BY i1, t2 WHERE a = c } { - 0 0 1 {SCAN TABLE t2} - 0 1 0 {SEARCH TABLE t1 USING INDEX i1 (a=?)} + QUERY PLAN + |--SCAN t1 USING INDEX i1 + `--SEARCH t2 USING INDEX i3 (c=?) } do_test indexedby-4.3 { catchsql { SELECT * FROM t1 INDEXED BY i1, t2 INDEXED BY i3 WHERE a=c } -} {1 {no query solution}} +} {0 {}} do_test indexedby-4.4 { catchsql { SELECT * FROM t2 INDEXED BY i3, t1 INDEXED BY i1 WHERE a=c } -} {1 {no query solution}} +} {0 {}} # Test embedding an INDEXED BY in a CREATE VIEW statement. This block -# also tests that nothing bad happens if an index refered to by +# also tests that nothing bad happens if an index referred to by # a CREATE VIEW statement is dropped and recreated. # do_execsql_test indexedby-5.1 { CREATE VIEW v2 AS SELECT * FROM t1 INDEXED BY i1 WHERE a > 5; EXPLAIN QUERY PLAN SELECT * FROM v2 -} {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (a>?)}} +} {/*SEARCH t1 USING INDEX i1 (a>?)*/} do_execsql_test indexedby-5.2 { EXPLAIN QUERY PLAN SELECT * FROM v2 WHERE b = 10 -} {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (a>?)}} +} {/*SEARCH t1 USING INDEX i1 (a>?)*/} do_test indexedby-5.3 { execsql { DROP INDEX i1 } catchsql { SELECT * FROM v2 } @@ -206,7 +205,7 @@ do_test indexedby-5.4 { # Recreate index i1 in such a way as it cannot be used by the view query. execsql { CREATE INDEX i1 ON t1(b) } catchsql { SELECT * FROM v2 } -} {1 {no query solution}} +} {0 {}} do_test indexedby-5.5 { # Drop and recreate index i1 again. This time, create it so that it can # be used by the query. @@ -216,12 +215,12 @@ do_test indexedby-5.5 { # Test that "NOT INDEXED" may use the rowid index, but not others. # -do_execsql_test indexedby-6.1 { - EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE b = 10 ORDER BY rowid -} {0 0 0 {SEARCH TABLE t1 USING INDEX i2 (b=?)}} -do_execsql_test indexedby-6.2 { - EXPLAIN QUERY PLAN SELECT * FROM t1 NOT INDEXED WHERE b = 10 ORDER BY rowid -} {0 0 0 {SCAN TABLE t1}} +do_eqp_test indexedby-6.1 { + SELECT * FROM t1 WHERE b = 10 ORDER BY rowid +} {SEARCH t1 USING INDEX i2 (b=?)} +do_eqp_test indexedby-6.2 { + SELECT * FROM t1 NOT INDEXED WHERE b = 10 ORDER BY rowid +} {SCAN t1} # EVIDENCE-OF: R-40297-14464 The INDEXED BY phrase forces the SQLite # query planner to use a particular named index on a DELETE, SELECT, or @@ -229,47 +228,45 @@ do_execsql_test indexedby-6.2 { # # Test that "INDEXED BY" can be used in a DELETE statement. # -do_execsql_test indexedby-7.1 { - EXPLAIN QUERY PLAN DELETE FROM t1 WHERE a = 5 -} {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (a=?)}} -do_execsql_test indexedby-7.2 { - EXPLAIN QUERY PLAN DELETE FROM t1 NOT INDEXED WHERE a = 5 -} {0 0 0 {SCAN TABLE t1}} -do_execsql_test indexedby-7.3 { - EXPLAIN QUERY PLAN DELETE FROM t1 INDEXED BY i1 WHERE a = 5 -} {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (a=?)}} -do_execsql_test indexedby-7.4 { - EXPLAIN QUERY PLAN DELETE FROM t1 INDEXED BY i1 WHERE a = 5 AND b = 10 -} {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (a=?)}} -do_execsql_test indexedby-7.5 { - EXPLAIN QUERY PLAN DELETE FROM t1 INDEXED BY i2 WHERE a = 5 AND b = 10 -} {0 0 0 {SEARCH TABLE t1 USING INDEX i2 (b=?)}} +do_eqp_test indexedby-7.1 { + DELETE FROM t1 WHERE a = 5 +} {SEARCH t1 USING INDEX i1 (a=?)} +do_eqp_test indexedby-7.2 { + DELETE FROM t1 NOT INDEXED WHERE a = 5 +} {SCAN t1} +do_eqp_test indexedby-7.3 { + DELETE FROM t1 INDEXED BY i1 WHERE a = 5 +} {SEARCH t1 USING INDEX i1 (a=?)} +do_eqp_test indexedby-7.4 { + DELETE FROM t1 INDEXED BY i1 WHERE a = 5 AND b = 10 +} {SEARCH t1 USING INDEX i1 (a=?)} +do_eqp_test indexedby-7.5 { + DELETE FROM t1 INDEXED BY i2 WHERE a = 5 AND b = 10 +} {SEARCH t1 USING INDEX i2 (b=?)} do_test indexedby-7.6 { catchsql { DELETE FROM t1 INDEXED BY i2 WHERE a = 5} -} {1 {no query solution}} +} {0 {}} # Test that "INDEXED BY" can be used in an UPDATE statement. # -do_execsql_test indexedby-8.1 { - EXPLAIN QUERY PLAN UPDATE t1 SET rowid=rowid+1 WHERE a = 5 -} {0 0 0 {SEARCH TABLE t1 USING COVERING INDEX i1 (a=?)}} -do_execsql_test indexedby-8.2 { - EXPLAIN QUERY PLAN UPDATE t1 NOT INDEXED SET rowid=rowid+1 WHERE a = 5 -} {0 0 0 {SCAN TABLE t1}} -do_execsql_test indexedby-8.3 { - EXPLAIN QUERY PLAN UPDATE t1 INDEXED BY i1 SET rowid=rowid+1 WHERE a = 5 -} {0 0 0 {SEARCH TABLE t1 USING COVERING INDEX i1 (a=?)}} -do_execsql_test indexedby-8.4 { - EXPLAIN QUERY PLAN +do_eqp_test indexedby-8.1 { + UPDATE t1 SET rowid=rowid+1 WHERE a = 5 +} {SEARCH t1 USING COVERING INDEX i1 (a=?)} +do_eqp_test indexedby-8.2 { + UPDATE t1 NOT INDEXED SET rowid=rowid+1 WHERE a = 5 +} {SCAN t1} +do_eqp_test indexedby-8.3 { + UPDATE t1 INDEXED BY i1 SET rowid=rowid+1 WHERE a = 5 +} {SEARCH t1 USING COVERING INDEX i1 (a=?)} +do_eqp_test indexedby-8.4 { UPDATE t1 INDEXED BY i1 SET rowid=rowid+1 WHERE a = 5 AND b = 10 -} {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (a=?)}} -do_execsql_test indexedby-8.5 { - EXPLAIN QUERY PLAN +} {SEARCH t1 USING INDEX i1 (a=?)} +do_eqp_test indexedby-8.5 { UPDATE t1 INDEXED BY i2 SET rowid=rowid+1 WHERE a = 5 AND b = 10 -} {0 0 0 {SEARCH TABLE t1 USING INDEX i2 (b=?)}} +} {SEARCH t1 USING INDEX i2 (b=?)} do_test indexedby-8.6 { catchsql { UPDATE t1 INDEXED BY i2 SET rowid=rowid+1 WHERE a = 5} -} {1 {no query solution}} +} {0 {}} # Test that bug #3560 is fixed. # @@ -287,10 +284,10 @@ do_test indexedby-9.2 { joinme as j indexed by joinme_id_text_idx on ( m.id = j.id_int) } -} {1 {no query solution}} +} {0 {}} do_test indexedby-9.3 { catchsql { select * from maintable, joinme INDEXED by joinme_id_text_idx } -} {1 {no query solution}} +} {0 {}} # Make sure we can still create tables, indices, and columns whose name # is "indexed". @@ -341,7 +338,7 @@ do_execsql_test 11.4 { } {1 1 3} do_eqp_test 11.5 { SELECT a,b,rowid FROM x1 INDEXED BY x1i WHERE a=1 AND b=1 AND rowid='3.0'; -} {0 0 0 {SEARCH TABLE x1 USING COVERING INDEX x1i (a=? AND b=? AND rowid=?)}} +} {SEARCH x1 USING COVERING INDEX x1i (a=? AND b=? AND rowid=?)} do_execsql_test 11.6 { CREATE TABLE x2(c INTEGER PRIMARY KEY, a, b TEXT); @@ -362,6 +359,27 @@ do_execsql_test 11.9 { } {1 1 3} do_eqp_test 11.10 { SELECT a,b,c FROM x2 INDEXED BY x2i WHERE a=1 AND b=1 AND c='3.0'; -} {0 0 0 {SEARCH TABLE x2 USING COVERING INDEX x2i (a=? AND b=? AND rowid=?)}} +} {SEARCH x2 USING COVERING INDEX x2i (a=? AND b=? AND rowid=?)} + +#------------------------------------------------------------------------- +# Check INDEXED BY works (throws an exception) with partial indexes that +# cannot be used. +do_execsql_test 12.1 { + CREATE TABLE o1(x INTEGER PRIMARY KEY, y, z); + CREATE INDEX p1 ON o1(z); + CREATE INDEX p2 ON o1(y) WHERE z=1; +} +do_catchsql_test 12.2 { + SELECT * FROM o1 INDEXED BY p2 ORDER BY 1; +} {1 {no query solution}} +do_execsql_test 12.3 { + DROP INDEX p1; + DROP INDEX p2; + CREATE INDEX p2 ON o1(y) WHERE z=1; + CREATE INDEX p1 ON o1(z); +} +do_catchsql_test 12.4 { + SELECT * FROM o1 INDEXED BY p2 ORDER BY 1; +} {1 {no query solution}} finish_test diff --git a/test/indexexpr1.test b/test/indexexpr1.test index a8a74f259e..ca6682cda7 100644 --- a/test/indexexpr1.test +++ b/test/indexexpr1.test @@ -49,7 +49,7 @@ do_execsql_test indexexpr1-130 { do_execsql_test indexexpr1-130eqp { EXPLAIN QUERY PLAN SELECT c FROM t1 WHERE b=1 AND substr(a,2,3)='nd_' ORDER BY c; -} {/USING INDEX t1ba/} +} {/USING COVERING INDEX t1ba/} do_execsql_test indexexpr1-140 { SELECT rowid, substr(a,b,3), '|' FROM t1 ORDER BY 2; @@ -61,7 +61,7 @@ do_execsql_test indexexpr1-141 { do_execsql_test indexexpr1-141eqp { EXPLAIN QUERY PLAN SELECT rowid FROM t1 WHERE substr(a,b,3)<='and' ORDER BY +rowid; -} {/USING INDEX t1abx/} +} {/USING COVERING INDEX t1abx/} do_execsql_test indexexpr1-142 { SELECT rowid FROM t1 WHERE +substr(a,b,3)<='and' ORDER BY +rowid; } {1 2 3} @@ -73,20 +73,22 @@ do_execsql_test indexexpr1-150eqp { EXPLAIN QUERY PLAN SELECT rowid FROM t1 WHERE substr(a,b,3) IN ('and','l_t','xyz') ORDER BY +rowid; -} {/USING INDEX t1abx/} - -do_execsql_test indexexpr1-160 { - ALTER TABLE t1 ADD COLUMN d; - UPDATE t1 SET d=length(a); - CREATE INDEX t1a2 ON t1(SUBSTR(a, 27, 3)) WHERE d>=29; - SELECT rowid, b, c FROM t1 - WHERE substr(a,27,3)=='ord' AND d>=29; -} {1 1 1} -do_execsql_test indexexpr1-160eqp { - EXPLAIN QUERY PLAN - SELECT rowid, b, c FROM t1 - WHERE substr(a,27,3)=='ord' AND d>=29; -} {/USING INDEX t1a2/} +} {/USING COVERING INDEX t1abx/} + +ifcapable altertable { + do_execsql_test indexexpr1-160 { + ALTER TABLE t1 ADD COLUMN d; + UPDATE t1 SET d=length(a); + CREATE INDEX t1a2 ON t1(SUBSTR(a, 27, 3)) WHERE d>=29; + SELECT rowid, b, c FROM t1 + WHERE substr(a,27,3)=='ord' AND d>=29; + } {1 1 1} + do_execsql_test indexexpr1-160eqp { + EXPLAIN QUERY PLAN + SELECT rowid, b, c FROM t1 + WHERE substr(a,27,3)=='ord' AND d>=29; + } {/USING INDEX t1a2/} +} # ORDER BY using an indexed expression # @@ -97,14 +99,14 @@ do_execsql_test indexexpr1-170 { do_execsql_test indexexpr1-170eqp { EXPLAIN QUERY PLAN SELECT length(a) FROM t1 ORDER BY length(a); -} {/SCAN TABLE t1 USING INDEX t1alen/} +} {/SCAN t1 USING COVERING INDEX t1alen/} do_execsql_test indexexpr1-171 { SELECT length(a) FROM t1 ORDER BY length(a) DESC; } {52 38 29 27 25 20} do_execsql_test indexexpr1-171eqp { EXPLAIN QUERY PLAN SELECT length(a) FROM t1 ORDER BY length(a) DESC; -} {/SCAN TABLE t1 USING INDEX t1alen/} +} {/SCAN t1 USING COVERING INDEX t1alen/} do_execsql_test indexexpr1-200 { DROP TABLE t1; @@ -140,7 +142,7 @@ do_execsql_test indexexpr1-230 { do_execsql_test indexexpr1-230eqp { EXPLAIN QUERY PLAN SELECT c FROM t1 WHERE b=1 AND substr(a,2,3)='nd_' ORDER BY c; -} {/USING INDEX t1ba/} +} {/USING COVERING INDEX t1ba/} do_execsql_test indexexpr1-240 { SELECT id, substr(a,b,3), '|' FROM t1 ORDER BY 2; @@ -152,7 +154,7 @@ do_execsql_test indexexpr1-241 { do_execsql_test indexexpr1-241eqp { EXPLAIN QUERY PLAN SELECT id FROM t1 WHERE substr(a,b,3)<='and' ORDER BY +id; -} {/USING INDEX t1abx/} +} {/USING COVERING INDEX t1abx/} do_execsql_test indexexpr1-242 { SELECT id FROM t1 WHERE +substr(a,b,3)<='and' ORDER BY +id; } {1 2 3} @@ -164,29 +166,31 @@ do_execsql_test indexexpr1-250eqp { EXPLAIN QUERY PLAN SELECT id FROM t1 WHERE substr(a,b,3) IN ('and','l_t','xyz') ORDER BY +id; -} {/USING INDEX t1abx/} - -do_execsql_test indexexpr1-260 { - ALTER TABLE t1 ADD COLUMN d; - UPDATE t1 SET d=length(a); - CREATE INDEX t1a2 ON t1(SUBSTR(a, 27, 3)) WHERE d>=29; - SELECT id, b, c FROM t1 - WHERE substr(a,27,3)=='ord' AND d>=29; -} {1 1 1} -do_execsql_test indexexpr1-260eqp { - EXPLAIN QUERY PLAN - SELECT id, b, c FROM t1 - WHERE substr(a,27,3)=='ord' AND d>=29; -} {/USING INDEX t1a2/} +} {/USING COVERING INDEX t1abx/} + +ifcapable altertable { + do_execsql_test indexexpr1-260 { + ALTER TABLE t1 ADD COLUMN d; + UPDATE t1 SET d=length(a); + CREATE INDEX t1a2 ON t1(SUBSTR(a, 27, 3)) WHERE d>=29; + SELECT id, b, c FROM t1 + WHERE substr(a,27,3)=='ord' AND d>=29; + } {1 1 1} + do_execsql_test indexexpr1-260eqp { + EXPLAIN QUERY PLAN + SELECT id, b, c FROM t1 + WHERE substr(a,27,3)=='ord' AND d>=29; + } {/USING INDEX t1a2/} +} do_catchsql_test indexexpr1-300 { - CREATE TABLE t2(a,b,c); + CREATE TABLE t2(a,b,c); INSERT INTO t2 VALUES(1,2,3); CREATE INDEX t2x1 ON t2(a,b+random()); } {1 {non-deterministic functions prohibited in index expressions}} do_catchsql_test indexexpr1-301 { - CREATE INDEX t2x1 ON t2(a+julianday('now')); -} {1 {non-deterministic functions prohibited in index expressions}} + CREATE INDEX t2x1 ON t2(julianday('now',a)); +} {1 {non-deterministic use of julianday() in an index}} do_catchsql_test indexexpr1-310 { CREATE INDEX t2x2 ON t2(a,b+(SELECT 15)); } {1 {subqueries prohibited in index expressions}} @@ -234,7 +238,7 @@ do_execsql_test indexexpr1-510 { do_execsql_test indexexpr1-510eqp { EXPLAIN QUERY PLAN SELECT substr(a,4,3) AS k FROM cnt, t5 WHERE k=printf('%03d',x); -} {/USING INDEX t5ax/} +} {/USING COVERING INDEX t5ax/} # Skip-scan on an indexed expression # @@ -323,5 +327,359 @@ do_execsql_test indexexpr1-1010 { SELECT *, '|' FROM t0 ORDER BY +a; } {0 88 2 | 2 99 4 | 5 99 7 |} +# 2016-10-10 +# Make sure indexes on expressions skip over initial NULL values in the +# index as they are suppose to do. +# Ticket https://sqlite.org/src/tktview/4baa46491212947 +# +do_execsql_test indexexpr1-1100 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a); + INSERT INTO t1 VALUES(NULL),(1); + SELECT '1:', typeof(a), a FROM t1 WHERE a<10; + SELECT '2:', typeof(a), a FROM t1 WHERE a+0<10; + CREATE INDEX t1x1 ON t1(a); + CREATE INDEX t1x2 ON t1(a+0); + SELECT '3:', typeof(a), a FROM t1 WHERE a<10; + SELECT '4:', typeof(a), a FROM t1 WHERE a+0<10; +} {1: integer 1 2: integer 1 3: integer 1 4: integer 1} + +do_execsql_test indexexpr1-1200 { + CREATE TABLE t10(a int, b int, c int, d int); + INSERT INTO t10(a, b, c, d) VALUES(0, 0, 2, 2); + INSERT INTO t10(a, b, c, d) VALUES(0, 0, 0, 0); + INSERT INTO t10(a, b, c, d) VALUES(0, 0, 1, 1); + INSERT INTO t10(a, b, c, d) VALUES(1, 1, 1, 1); + INSERT INTO t10(a, b, c, d) VALUES(1, 1, 0, 0); + INSERT INTO t10(a, b, c, d) VALUES(2, 2, 0, 0); + + SELECT a+b, c+d FROM t10 ORDER BY a+b, c+d; +} { + 0 0 0 2 0 4 2 0 2 2 4 0 +} +do_execsql_test indexexpr1-1200.1 { + CREATE INDEX t10_ab ON t10(a+b); +} +do_execsql_test indexexpr1-1200.2 { + SELECT a+b, c+d FROM t10 ORDER BY a+b, c+d; +} { + 0 0 0 2 0 4 2 0 2 2 4 0 +} +do_execsql_test indexexpr1-1200.3 { + CREATE INDEX t10_abcd ON t10(a+b,c+d); +} +do_execsql_test indexexpr1-1200.4 { + SELECT a+b, c+d FROM t10 ORDER BY a+b, c+d; +} { + 0 0 0 2 0 4 2 0 2 2 4 0 +} + +# Ticket https://sqlite.org/src/tktview/eb703ba7b50c1a +# Incorrect result using an index on an expression with a collating function +# +do_execsql_test indexexpr1-1300.1 { + CREATE TABLE t1300(a INTEGER PRIMARY KEY, b); + INSERT INTO t1300 VALUES(1,'coffee'),(2,'COFFEE'),(3,'stress'),(4,'STRESS'); + CREATE INDEX t1300bexpr ON t1300( substr(b,4) ); + SELECT a FROM t1300 WHERE substr(b,4)='ess' COLLATE nocase ORDER BY +a; +} {3 4} + +# Ticket https://sqlite.org/src/tktview/aa98619a +# Assertion fault using an index on a constant +# +do_execsql_test indexexpr1-1400 { + CREATE TABLE t1400(x TEXT); + CREATE INDEX t1400x ON t1400(1); -- Index on a constant + SELECT 1 IN (SELECT 2) FROM t1400; +} {} +do_execsql_test indexexpr1-1410 { + INSERT INTO t1400 VALUES('a'),('b'); + SELECT 1 IN (SELECT 2) FROM t1400; +} {0 0} +do_execsql_test indexexpr1-1420 { + SELECT 1 IN (SELECT 2 UNION ALL SELECT 1) FROM t1400; +} {1 1} +do_execsql_test indexexpr1-1430 { + DROP INDEX t1400x; + CREATE INDEX t1400x ON t1400(abs(15+3)); + SELECT abs(15+3) IN (SELECT 17 UNION ALL SELECT 18) FROM t1; +} {1 1} + +# 2018-01-02 ticket https://sqlite.org/src/info/dc3f932f5a147771 +# A REPLACE into a table that uses an index on an expression causes +# an assertion fault. Problem discovered by OSSFuzz. +# +do_execsql_test indexexpr1-1500 { + CREATE TABLE t1500(a INT PRIMARY KEY, b INT UNIQUE); + CREATE INDEX t1500ab ON t1500(a*b); + INSERT INTO t1500(a,b) VALUES(1,2); + REPLACE INTO t1500(a,b) VALUES(1,3); -- formerly caused assertion fault + SELECT * FROM t1500; +} {1 3} + +# 2018-01-03 OSSFuzz discovers another test case for the same problem +# above. +# +do_execsql_test indexexpr1-1510 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a PRIMARY KEY,b UNIQUE); + REPLACE INTO t1 VALUES(2, 1); + REPLACE INTO t1 SELECT 6,1; + CREATE INDEX t1aa ON t1(a-a); + REPLACE INTO t1 SELECT a, randomblob(a) FROM t1 +} {} + +# 2018-01-31 https://sqlite.org/src/tktview/343634942dd54ab57b702411 +# When an index on an expression depends on the string representation of +# a numeric table column, trouble can arise since there are multiple +# string that can map to the same numeric value. (Ex: 123, 0123, 000123). +# +do_execsql_test indexexpr1-1600 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1 (a INTEGER, b); + CREATE INDEX idx1 ON t1 (lower(a)); + INSERT INTO t1 VALUES('0001234',3); + PRAGMA integrity_check; +} {ok} +do_execsql_test indexexpr1-1610 { + INSERT INTO t1 VALUES('1234',0),('001234',2),('01234',1); + SELECT b FROM t1 WHERE lower(a)='1234' ORDER BY +b; +} {0 1 2 3} +do_execsql_test indexexpr1-1620 { + SELECT b FROM t1 WHERE lower(a)='01234' ORDER BY +b; +} {} + +# 2019-08-09 https://sqlite.org/src/info/9080b6227fabb466 +# ExprImpliesExpr theorem prover bug: +# "(NULL IS FALSE) IS FALSE" does not imply "NULL IS NULL" +# +do_execsql_test indexexpr1-1700 { + DROP TABLE IF EXISTS t0; + CREATE TABLE t0(c0); + INSERT INTO t0(c0) VALUES (0); + CREATE INDEX i0 ON t0(NULL > c0) WHERE (NULL NOT NULL); + SELECT * FROM t0 WHERE ((NULL IS FALSE) IS FALSE); +} {0} + +# 2019-09-02 https://sqlite.org/src/tktview/57af00b6642ecd6848 +# When the expression of an an index-on-expression references a +# table column of type REAL that is actually holding an MEM_IntReal +# value, be sure to use the REAL value and not the INT value when +# computing the expression. +# +ifcapable like_match_blobs { + do_execsql_test indexexpr1-1800 { + DROP TABLE IF EXISTS t0; + CREATE TABLE t0(c0 REAL, c1 TEXT); + CREATE INDEX i0 ON t0(+c0, c0); + INSERT INTO t0(c0) VALUES(0); + SELECT CAST(+ t0.c0 AS BLOB) LIKE 0 FROM t0; + } {0} + do_execsql_test indexexpr1-1810 { + SELECT CAST(+ t0.c0 AS BLOB) LIKE '0.0' FROM t0; + } {1} + do_execsql_test indexexpr1-1820 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(x REAL); + CREATE INDEX t1x ON t1(x, +x); + INSERT INTO t1(x) VALUES(2); + SELECT +x FROM t1 WHERE x=2; + } {2.0} +} + +# 2022-04-30 https://sqlite.org/forum/info/7efabf4b03328e57 +# Assertion fault during a DELETE INDEXED BY. +# +reset_db +do_execsql_test indexexpr1-1900 { + CREATE TABLE t1(x TEXT PRIMARY KEY, y TEXT, z INT); + INSERT INTO t1(x,y,z) VALUES('alpha','ALPHA',1),('bravo','charlie',1); + CREATE INDEX i1 ON t1(+y COLLATE NOCASE); + SELECT * FROM t1; +} {alpha ALPHA 1 bravo charlie 1} +do_execsql_test indexexpr1-1910 { + DELETE FROM t1 INDEXED BY i1 + WHERE x IS +y COLLATE NOCASE IN (SELECT z FROM t1) + RETURNING *; +} {alpha ALPHA 1} +do_execsql_test indexexpr1-1920 { + SELECT * FROM t1; +} {bravo charlie 1} + +# 2022-11-28 Ticket 695a1a53de +# Improved ability to recognize that an index on an expression is a +# covering index. +# +reset_db +do_execsql_test indexexpr1-2000 { + CREATE TABLE t1(a INT, b TEXT); + INSERT INTO t1(a,b) VALUES + (10, '{"one":5,"two":6}'), + (10, '{"one":50,"two":60}'), + (10, '{"three":99}'), + (11, '{"one":100,"two":200}'); + CREATE INDEX t1_one ON t1(a, b->>'one'); + CREATE INDEX t1_two ON t1(a, b->>'two'); +} +do_execsql_test indexexpr1-2010 { + EXPLAIN QUERY PLAN + SELECT sum(b->>'one') FROM t1 WHERE a=10; /* Query AA */ +} {/.* t1_one .*/} +do_execsql_test indexexpr1-2011 { + SELECT sum(b->>'one') FROM t1 WHERE a=10; /* Query AA */ +} {55} +do_execsql_test indexexpr1-2020 { + EXPLAIN QUERY PLAN + SELECT sum(b->>'two') FROM t1 WHERE a=10; /* Query BB */ +} {/.* t1_two .*/} +do_execsql_test indexexpr1-2021 { + SELECT sum(b->>'two') FROM t1 WHERE a=10; /* Query BB */ +} {66} +do_execsql_test indexexpr1-2030 { + DROP TABLE t1; + CREATE TABLE t1(a INT, b TEXT, c INT, d INT); + INSERT INTO t1(a,b,c,d) VALUES + (1, '{"x":1}', 12, 3), + (1, '{"x":2}', 4, 5), + (1, '{"x":1}', 6, 11), + (2, '{"x":1}', 22, 3), + (2, '{"x":2}', 4, 5), + (3, '{"x":1}', 6, 7); + CREATE INDEX t1x ON t1(d, a, b->>'x', c); +} +do_execsql_test indexexpr1-2040 { + SELECT a, + SUM(1) AS t1, + SUM(CASE WHEN b->>'x'=1 THEN 1 END) AS t2, + SUM(c) AS t3, + SUM(CASE WHEN b->>'x'=1 THEN c END) AS t4 + FROM t1; +} {1 6 4 54 46} +do_execsql_test indexexpr1-2050 { + explain query plan + SELECT a, + SUM(1) AS t1, + SUM(CASE WHEN b->>'x'=1 THEN 1 END) AS t2, + SUM(c) AS t3, + SUM(CASE WHEN b->>'x'=1 THEN c END) AS t4 + FROM t1; +} {/.*SCAN t1 USING COVERING INDEX t1x.*/} + +reset_db +do_execsql_test indexexpr1-2100 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b INT); + INSERT INTO t1(a,b) VALUES(1,0); + CREATE INDEX x1 ON t1( "y" ); + CREATE INDEX x2 ON t1( +"y" ); + CREATE INDEX x3 ON t1( +'y' ); + CREATE INDEX x4 ON t1( "y*" ); +} +do_execsql_test indexexpr1-2110 { + UPDATE t1 SET b=100 WHERE (SELECT 'y') GLOB "y"; + SELECT b FROM t1; +} 100 +do_execsql_test indexexpr1-2120 { + UPDATE t1 SET b=200 WHERE (SELECT 'y') GLOB +"y"; + SELECT b FROM t1; +} 200 +do_execsql_test indexexpr1-2130 { + UPDATE t1 SET b=300 WHERE (SELECT 'y') GLOB +'y'; + SELECT b FROM t1; +} 300 +do_execsql_test indexexpr1-2140 { + UPDATE t1 SET b=400 WHERE (SELECT 'y') GLOB "y*"; + SELECT b FROM t1; +} 400 + +# 2023-04-18 Forum post https://sqlite.org/forum/forumpost/f34e32d120 from +# Alexis King. +# +# This problem originates at check-in b9190d3da70c4171 (2022-11-25). +# A similar problem arose on 2023-03-04 at +# https://sqlite.org/forum/forumpost/a68313d054 and was fixed at +# check-in e06973876993926f. See the test case tkt-99378-400. +# +reset_db +do_execsql_test indexexpr1-2200 { + CREATE TABLE t1(id INTEGER PRIMARY KEY, tag INT); + INSERT INTO t1 VALUES (0, 7), (1, 8); + CREATE TABLE t2(type INT, t1_id INT, value INT); + INSERT INTO t2 VALUES (0, 0, 100), (0, 1, 101); + CREATE INDEX t1x ON t1(-tag); + SELECT u.tag, v.max_value + FROM (SELECT tag FROM t1 GROUP BY -tag) u + JOIN (SELECT t1.tag AS "tag", t2.type AS "type", + MAX(t2.value) AS "max_value" + FROM t1 + JOIN t2 ON t2.t1_id = t1.id + GROUP BY t2.type, t1.tag + ) v ON v.type = 0 AND v.tag = u.tag; +} {7 100 8 101} +do_execsql_test indexexpr1-2210 { + DROP TABLE t1; + CREATE TABLE t1(x INT, y TEXT); + INSERT INTO t1(x,y) VALUES(1,'{b:5}'); + SELECT json_insert('{}', '$.a', coalesce(null,json(y)))->>'$.a.b' FROM t1; +} {5} +db null NULL +do_execsql_test indexexpr1-2211 { + CREATE INDEX t1j ON t1(coalesce(null,json(y))); + SELECT json_insert('{}', '$.a', coalesce(null,json(y)))->>'$.a.b' FROM t1; +} {5} +do_execsql_test indexexpr1-2220 { + DROP INDEX t1j; + SELECT json_insert('{}', '$.a', iif(1,json(y),123))->>'$.a.b' FROM t1; +} {5} +do_execsql_test indexexpr1-2221 { + CREATE INDEX t1j ON t1(iif(1,json(y),123)); + SELECT json_insert('{}', '$.a', iif(1,json(y),123))->>'$.a.b' FROM t1; +} {5} +do_execsql_test indexexpr1-2230 { + DROP INDEX t1j; + SELECT json_insert('{}', '$.a', ifnull(NULL,json(y)))->>'$.a.b' FROM t1; +} {5} +do_execsql_test indexexpr1-2231 { + CREATE INDEX t1j ON t1(ifnull(NULL,json(y))); + SELECT json_insert('{}', '$.a', ifnull(NULL,json(y)))->>'$.a.b' FROM t1; +} {5} +do_execsql_test indexexpr1-2240 { + DROP INDEX t1j; + SELECT json_insert('{}', '$.a', nullif(json(y),8))->>'$.a.b' FROM t1; +} {5} +do_execsql_test indexexpr1-2241 { + CREATE INDEX t1j ON t1(nullif(json(y),8)); + SELECT json_insert('{}', '$.a', nullif(json(y),8))->>'$.a.b' FROM t1; +} {5} +do_execsql_test indexexpr1-2250 { + DROP INDEX t1j; + SELECT json_insert('{}', '$.a', min('~',json(y)))->>'$.a.b' FROM t1; +} {5} +do_execsql_test indexexpr1-2251 { + CREATE INDEX t1j ON t1(min('~',json(y))); + SELECT json_insert('{}', '$.a', min('~',json(y)))->>'$.a.b' FROM t1; +} {5} +do_execsql_test indexexpr1-2260 { + DROP INDEX t1j; + SELECT json_insert('{}', '$.a', max('...',json(y)))->>'$.a.b' FROM t1; +} {5} +do_execsql_test indexexpr1-2261 { + CREATE INDEX t1j ON t1(max('...',json(y))); + SELECT json_insert('{}', '$.a', max('...',json(y)))->>'$.a.b' FROM t1; +} {5} + + +# 2023-11-08 Forum post https://sqlite.org/forum/forumpost/68d284c86b082c3e +# +# Functions that return subtypes and that are indexed cannot be used to +# cover function calls from the main table, since the indexed value does +# not know the subtype. +# +reset_db +do_execsql_test indexexpr1-2300 { + CREATE TABLE t1(x INT, y TEXT); + INSERT INTO t1(x,y) VALUES(1,'{b:5}'); + CREATE INDEX t1j ON t1(json(y)); + SELECT json_insert('{}', '$.a', json(y)) FROM t1; +} {{{"a":{"b":5}}}} finish_test diff --git a/test/indexexpr2.test b/test/indexexpr2.test new file mode 100644 index 0000000000..4c21421e8e --- /dev/null +++ b/test/indexexpr2.test @@ -0,0 +1,428 @@ +# 2017 April 11 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix indexexpr2 + +do_execsql_test 1 { + CREATE TABLE t1(a, b); + INSERT INTO t1 VALUES(1, 'one'); + INSERT INTO t1 VALUES(2, 'two'); + INSERT INTO t1 VALUES(3, 'three'); + + CREATE INDEX i1 ON t1(b || 'x'); +} + +do_execsql_test 1.1 { + SELECT 'TWOX' == (b || 'x') FROM t1 WHERE (b || 'x')>'onex' +} {0 0} + +do_execsql_test 1.2 { + SELECT 'TWOX' == (b || 'x') COLLATE nocase FROM t1 WHERE (b || 'x')>'onex' +} {0 1} + +do_execsql_test 2.0 { + CREATE INDEX i2 ON t1(a+1); +} + +do_execsql_test 2.1 { + SELECT a+1, quote(a+1) FROM t1 ORDER BY 1; +} {2 2 3 3 4 4} + +#------------------------------------------------------------------------- +# At one point SQLite was incorrectly using indexes on expressions to +# optimize ORDER BY and GROUP BY clauses even when the collation +# sequences of the query and index did not match (ticket [e20dd54ab0e4]). +# The following tests - 3.* - attempt to verify that this has been fixed. +# + +reset_db +do_execsql_test 3.1.0 { + CREATE TABLE t1(a, b); + CREATE INDEX i1 ON t1(a, b); +} {} + +do_eqp_test 3.1.1 { + SELECT b FROM t1 WHERE b IS NOT NULL AND a IS NULL + GROUP BY b COLLATE nocase + ORDER BY b COLLATE nocase; +} {/USE TEMP B-TREE FOR GROUP BY/} + +do_execsql_test 3.2.0 { + CREATE TABLE t2(x); + + INSERT INTO t2 VALUES('.ABC'); + INSERT INTO t2 VALUES('.abcd'); + INSERT INTO t2 VALUES('.defg'); + INSERT INTO t2 VALUES('.DEF'); +} {} + +do_execsql_test 3.2.1 { + SELECT x FROM t2 ORDER BY substr(x, 2) COLLATE nocase; +} { + .ABC .abcd .DEF .defg +} + +do_execsql_test 3.2.2 { + CREATE INDEX i2 ON t2( substr(x, 2) ); + SELECT x FROM t2 ORDER BY substr(x, 2) COLLATE nocase; +} { + .ABC .abcd .DEF .defg +} + +do_execsql_test 3.3.0 { + CREATE TABLE t3(x); +} + +ifcapable json1 { + do_eqp_test 3.3.1 { + SELECT json_extract(x, '$.b') FROM t2 + WHERE json_extract(x, '$.b') IS NOT NULL AND json_extract(x, '$.a') IS NULL + GROUP BY json_extract(x, '$.b') COLLATE nocase + ORDER BY json_extract(x, '$.b') COLLATE nocase; + } [string map {"\n " \n} { + QUERY PLAN + |--SCAN t2 + `--USE TEMP B-TREE FOR GROUP BY + }] + + do_execsql_test 3.3.2 { + CREATE INDEX i3 ON t3(json_extract(x, '$.a'), json_extract(x, '$.b')); + } {} + + do_eqp_test 3.3.3 { + SELECT json_extract(x, '$.b') FROM t3 + WHERE json_extract(x, '$.b') IS NOT NULL AND json_extract(x, '$.a') IS NULL + GROUP BY json_extract(x, '$.b') COLLATE nocase + ORDER BY json_extract(x, '$.b') COLLATE nocase; + } [string map {"\n " \n} { + QUERY PLAN + |--SEARCH t3 USING INDEX i3 (<expr>=?) + `--USE TEMP B-TREE FOR GROUP BY + }] +} + +do_execsql_test 3.4.0 { + CREATE TABLE t4(a, b); + INSERT INTO t4 VALUES('.ABC', 1); + INSERT INTO t4 VALUES('.abc', 2); + INSERT INTO t4 VALUES('.ABC', 3); + INSERT INTO t4 VALUES('.abc', 4); +} + +do_execsql_test 3.4.1 { + SELECT * FROM t4 + WHERE substr(a, 2) = 'abc' COLLATE NOCASE + ORDER BY substr(a, 2), b; +} { + .ABC 1 .ABC 3 .abc 2 .abc 4 +} + +do_execsql_test 3.4.2 { + CREATE INDEX i4 ON t4( substr(a, 2) COLLATE NOCASE, b ); + SELECT * FROM t4 + WHERE substr(a, 2) = 'abc' COLLATE NOCASE + ORDER BY substr(a, 2), b; +} { + .ABC 1 .ABC 3 .abc 2 .abc 4 +} + +do_execsql_test 3.4.3 { + DROP INDEX i4; + UPDATE t4 SET a = printf('%s%d',a,b); + SELECT * FROM t4 ORDER BY Substr(a,-2) COLLATE nocase; +} {.ABC1 1 .abc2 2 .ABC3 3 .abc4 4} +do_execsql_test 3.4.4 { + SELECT * FROM t4 ORDER BY Substr(a,-2) COLLATE binary; +} {.ABC1 1 .ABC3 3 .abc2 2 .abc4 4} + +do_execsql_test 3.4.5 { + CREATE INDEX i4 ON t4( Substr(a,-2) COLLATE nocase ); + SELECT * FROM t4 ORDER BY Substr(a,-2) COLLATE nocase; +} {.ABC1 1 .abc2 2 .ABC3 3 .abc4 4} +do_execsql_test 3.4.5eqp { + EXPLAIN QUERY PLAN + SELECT * FROM t4 ORDER BY Substr(a,-2) COLLATE nocase; +} {/SCAN t4 USING INDEX i4/} +do_execsql_test 3.4.6 { + SELECT * FROM t4 ORDER BY Substr(a,-2) COLLATE binary; +} {.ABC1 1 .ABC3 3 .abc2 2 .abc4 4} + +# 2014-09-15: Verify that UPDATEs of columns not referenced by a +# index on expression do not modify the index. +# +unset -nocomplain cnt +set cnt 0 +proc refcnt {x} { + global cnt + incr cnt + return $x +} +db close +sqlite3 db :memory: +db function refcnt -deterministic refcnt +do_test 4.100 { + db eval { + CREATE TABLE t1(a,b,c,d,e,f); + CREATE INDEX t1abc ON t1(refcnt(a+b+c)); + } + set ::cnt +} {0} +do_test 4.110 { + db eval {INSERT INTO t1 VALUES(1,2,3,4,5,6);} + set ::cnt + # The refcnt() function is invoked once to compute the index value +} {1} +do_test 4.120 { + set ::cnt 0 + db eval {UPDATE t1 SET b=b+1;} + set ::cnt + # The refcnt() function is invoked twice, once to remove the old index + # entry and a second time to insert the new one. +} {2} +do_test 4.130 { + set ::cnt 0 + db eval {UPDATE t1 SET d=d+1;} + set ::cnt + # Refcnt() should not be invoked because that index does not change. +} {0} + +# Additional test cases to show that UPDATE does not modify indexes that +# do not involve unchanged columns. +# +ifcapable vtab { + load_static_extension db explain + do_execsql_test 4.200 { + CREATE TABLE t2(a,b,c,d,e,f); + INSERT INTO t2 VALUES(2,3,4,5,6,7); + CREATE INDEX t2abc ON t2(a+b+c); + CREATE INDEX t2cd ON t2(c*d); + CREATE INDEX t2def ON t2(d,e+25*f); + SELECT sqlite_master.name + FROM sqlite_master, explain('UPDATE t2 SET b=b+1') + WHERE explain.opcode LIKE 'Open%' + AND sqlite_master.rootpage=explain.p2 + ORDER BY 1; + } {t2 t2abc} + do_execsql_test 4.210 { + SELECT sqlite_master.name + FROM sqlite_master, explain('UPDATE t2 SET c=c+1') + WHERE explain.opcode LIKE 'Open%' + AND sqlite_master.rootpage=explain.p2 + ORDER BY 1; + } {t2 t2abc t2cd} + do_execsql_test 4.220 { + SELECT sqlite_master.name + FROM sqlite_master, explain('UPDATE t2 SET c=c+1, f=NULL') + WHERE explain.opcode LIKE 'Open%' + AND sqlite_master.rootpage=explain.p2 + ORDER BY 1; + } {t2 t2abc t2cd t2def} +} + +#------------------------------------------------------------------------- +# Test that ticket [d96eba87] has been fixed. +# +do_execsql_test 5.0 { + CREATE TABLE t5(a INTEGER, b INTEGER); + INSERT INTO t5 VALUES(2, 4), (3, 9); +} +do_execsql_test 5.1 { + SELECT * FROM t5 WHERE abs(a)=2 or abs(b)=9; +} {2 4 3 9} +do_execsql_test 5.2 { + CREATE INDEX t5a ON t5( abs(a) ); + CREATE INDEX t5b ON t5( abs(b) ); +} +do_execsql_test 5.4 { + SELECT * FROM t5 WHERE abs(a)=2 or abs(b)=9; +} {2 4 3 9} + +#------------------------------------------------------------------------- +do_execsql_test 6.0 { + CREATE TABLE x1(a INTEGER PRIMARY KEY, b); + INSERT INTO x1 VALUES + (1, 123), (2, '123'), (3, '123abc'), (4, 123.0), (5, 1234); +} + +do_execsql_test 6.1.1 { + SELECT a, b FROM x1 WHERE CAST(b AS INTEGER) = 123; +} {1 123 2 123 3 123abc 4 123.0} +do_execsql_test 6.1.2 { + CREATE INDEX x1i ON x1( CAST(b AS INTEGER) ); + SELECT a, b FROM x1 WHERE CAST(b AS INTEGER) = 123; +} {1 123 2 123 3 123abc 4 123.0} +do_eqp_test 6.1.3 { + SELECT a, b FROM x1 WHERE CAST(b AS INTEGER) = 123; +} {SEARCH x1 USING INDEX x1i (<expr>=?)} + +do_execsql_test 6.2.1 { + SELECT a, b FROM x1 WHERE CAST(b AS TEXT) = 123; +} {1 123 2 123} +do_execsql_test 6.2.2 { + CREATE INDEX x1i2 ON x1( CAST(b AS TEXT) ); + SELECT a, b FROM x1 WHERE CAST(b AS TEXT) = 123; +} {1 123 2 123} +do_eqp_test 6.2.3 { + SELECT a, b FROM x1 WHERE CAST(b AS TEXT) = 123; +} {SEARCH x1 USING INDEX x1i2 (<expr>=?)} + +do_execsql_test 7.0 { + CREATE TABLE IF NOT EXISTS t0(c0); + INSERT INTO t0(c0) VALUES (-9223372036854775808); + BEGIN; +} +do_catchsql_test 7.1 { + CREATE INDEX i0 ON t0(ABS(c0)); +} {1 {integer overflow}} +do_execsql_test 7.2 { + COMMIT; + SELECT sql FROM sqlite_master WHERE tbl_name = 't0'; + CREATE INDEX i0 ON t0(c0); +} {{CREATE TABLE t0(c0)}} +do_execsql_test 7.3 { + REINDEX; +} {} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 8.0 { + CREATE TABLE t0(c0); + CREATE INDEX i0 ON t0(c0) WHERE c0 NOT NULL; + INSERT INTO t0(c0) VALUES (NULL); +} + +do_execsql_test 8.1.1 { + SELECT * FROM t0 WHERE ~('' BETWEEN t0.c0 AND TRUE); +} {{}} +do_execsql_test 8.1.2 { + SELECT ~('' BETWEEN t0.c0 AND TRUE) FROM t0; +} {-1} + +foreach {tn expr} { + 1 " 0 == (34 BETWEEN c0 AND 33)" + 2 " 1 != (34 BETWEEN c0 AND 33)" + 3 "-1 < (34 BETWEEN c0 AND 33)" + 4 "-1 <= (34 BETWEEN c0 AND 33)" + 5 " 1 > (34 BETWEEN c0 AND 33)" + 6 " 1 >= (34 BETWEEN c0 AND 33)" + 7 " 1 - (34 BETWEEN c0 AND 33)" + 8 "-1 + (34 BETWEEN c0 AND 33)" + 9 " 1 | (34 BETWEEN c0 AND 33)" + 10 " 1 << (34 BETWEEN c0 AND 33)" + 11 " 1 >> (34 BETWEEN c0 AND 33)" + 12 " 1 || (34 BETWEEN c0 AND 33)" +} { + do_execsql_test 8.3.$tn.1 "SELECT * FROM t0 WHERE $expr ORDER BY c0" { {} } + do_execsql_test 8.3.$tn.2 "SELECT ($expr) IS TRUE FROM t0" { 1 } +} + +do_execsql_test 8.4 { + CREATE TABLE t1(a, b); + INSERT INTO t1 VALUES(1, 2), (3, 4); + CREATE TABLE t2(x, y); +} + +foreach {tn expr} { + 1 " 0 == (a=0 AND y=1)" + 2 " 1 != (a=0 AND y=1)" + 3 "-1 < (a=0 AND y=1)" + 4 "-1 <= (a=0 AND y=1)" + 5 " 1 > (a=0 AND y=1)" + 6 " 1 >= (a=0 AND y=1)" + 7 " 1 - (a=0 AND y=1)" + 8 "-1 + (a=0 AND y=1)" + 9 " 1 | (a=0 AND y=1)" + 10 "1 << (a=0 AND y=1)" + 11 "1 >> (a=0 AND y=1)" + 12 "1 || (a=0 AND y=1)" + + 13 " 0 == (10 BETWEEN y AND b)" + 14 " 1 != (10 BETWEEN y AND b)" + 15 "-1 < (10 BETWEEN y AND b)" + 16 "-1 <= (10 BETWEEN y AND b)" + 17 " 1 > (10 BETWEEN y AND b)" + 18 " 1 >= (10 BETWEEN y AND b)" + 19 " 1 - (10 BETWEEN y AND b)" + 20 "-1 + (10 BETWEEN y AND b)" + 21 " 1 | (10 BETWEEN y AND b)" + 22 " 1 << (10 BETWEEN y AND b)" + 23 " 1 >> (10 BETWEEN y AND b)" + 24 " 1 || (10 BETWEEN y AND b)" + + 25 " 1 || (10 BETWEEN y AND b)" +} { + do_execsql_test 8.5.$tn.1 " + SELECT * FROM t1 LEFT JOIN t2 WHERE $expr + " {1 2 {} {} 3 4 {} {}} + + do_execsql_test 8.5.$tn.2 " + SELECT ($expr) IS TRUE FROM t1 LEFT JOIN t2 + " {1 1} +} + +# 2023-03-24 https://sqlite.org/forum/forumpost/79cf371080 +# +reset_db +do_execsql_test 9.0 { + CREATE TABLE t1(a INT, b INT); + CREATE INDEX t1x ON t1(a, abs(b)); + CREATE TABLE t2(c INT, d INT); + INSERT INTO t1(a,b) VALUES(4,4),(5,-5),(5,20),(6,6); + INSERT INTO t2(c,d) VALUES(100,1),(200,1),(300,2); + SELECT *, + (SELECT max(c+abs(b)) FROM t2 GROUP BY d ORDER BY d LIMIT 1) AS subq + FROM t1 WHERE a=5; +} {5 -5 205 5 20 220} + +# 2023-04-03 https://sqlite.org/forum/forumpost/44270909bb +# and https://sqlite.org/forum/forumpost/e45108732c which are the +# same problem, namely the failure to omit the EP_Collate property +# from an expression node when changing it from TK_COLLATE into +# TK_AGG_COLUMN because it resolves to an indexed expression. +# +reset_db +do_execsql_test 10.0 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT); + CREATE INDEX t1x ON t1 (b, +b COLLATE NOCASE); + INSERT INTO t1(a,b) VALUES(1,'abcde'); + SELECT * FROM t1 AS a0 + WHERE (SELECT count(a0.b=+a0.b COLLATE NOCASE IN (b)) FROM t1 GROUP BY 2.5) + ORDER BY a0.b; +} {1 abcde} +do_execsql_test 10.1 { + CREATE TABLE t2(a TEXT); + INSERT INTO t2 VALUES('alice'),('bob'),('cindy'),('david'); + CREATE INDEX t2x ON t2 (+a COLLATE NOCASE); + SELECT count(+a COLLATE NOCASE IN (SELECT 1)) AS x + FROM t2 + GROUP BY SUBSTR(0,0); +} 4 + +# 2023-04-03 https://sqlite.org/forum/forumpost/409ebc7368 +# When a generated column appears in both an outer and an inner loop +# (that is to say, the same table is used in both loops) and the +# generated column is indexed and it is used inside an aggregate function, +# make sure that the terms resolve to the correct aggregate. +# +do_execsql_test 11.0 { + CREATE TABLE t3 (a INT, b AS (-a)); + CREATE INDEX t3x ON t3(b, a); + INSERT INTO t3(a) VALUES(44); + SELECT * FROM t3 AS a0 + WHERE (SELECT sum(-a0.a=b) FROM t3 GROUP BY b) + GROUP BY b; +} {44 -44} + +finish_test diff --git a/test/indexexpr3.test b/test/indexexpr3.test new file mode 100644 index 0000000000..76d3331f75 --- /dev/null +++ b/test/indexexpr3.test @@ -0,0 +1,121 @@ +# 2024-10-05 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is testing indexes on expressions. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix indexexpr3 + + +do_execsql_test 1.0 { + CREATE TABLE t1(a, j); + INSERT INTO t1 VALUES(1, '{x:"one"}'); + INSERT INTO t1 VALUES(2, '{x:"two"}'); + INSERT INTO t1 VALUES(3, '{x:"three"}'); + + CREATE INDEX i1 ON t1( json_extract(j, '$.x') ); + CREATE INDEX i2 ON t1( a, json_extract(j, '$.x') ); +} + +proc do_hasfunction_test {tn sql res} { + set nFunction 0 + db eval "EXPLAIN $sql" x { + if {$x(opcode)=="Function"} { + incr nFunction + } + } + + do_execsql_test $tn " + SELECT $nFunction; + $sql + " $res +} + +do_hasfunction_test 1.1 { + SELECT json_extract(j, '$.x') FROM t1 ORDER BY 1; +} { + 0 one three two +} + +do_hasfunction_test 1.2 { + SELECT json_extract(j, '$.x') FROM t1 WHERE a=2 +} { + 0 two +} + +do_hasfunction_test 1.3 { + SELECT coalesce(json_extract(j, '$.x'), 'five') FROM t1 WHERE a=2 +} { + 0 two +} + +do_hasfunction_test 1.4 { + SELECT json_extract(j, '$.x') || '.two' FROM t1 WHERE a=2 +} { + 0 two.two +} + +do_hasfunction_test 1.5 { + SELECT json_insert( '{}', '$.y', json_extract(j, '$.x') ) FROM t1 WHERE a=2 +} { + 2 {{"y":"two"}} +} + +do_hasfunction_test 1.6 { + SELECT json_insert( '{}', '$.y', coalesce( json_extract(j, '$.x'), 'five' ) ) + FROM t1 WHERE a=2 +} { + 2 {{"y":"two"}} +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 2.0 { + CREATE TABLE t1(a, b, j); + CREATE INDEX i1 ON t1( a, json_extract(j, '$.x') ); +} + +do_eqp_test 2.1 { + SELECT json_extract(j, '$.x') FROM t1 WHERE a=? +} { + t1 USING COVERING INDEX i1 +} + +do_eqp_test 2.2 { + SELECT b, json_extract(j, '$.x') FROM t1 WHERE a=? +} { + t1 USING INDEX i1 +} + +do_eqp_test 2.3 { + SELECT json_insert( '{}', json_extract(j, '$.x') ) FROM t1 WHERE a=? +} { + t1 USING INDEX i1 +} + +do_eqp_test 2.4 { + SELECT sum( json_extract(j, '$.x') ) FROM t1 WHERE a=? +} { + t1 USING COVERING INDEX i1 +} + +do_eqp_test 2.5 { + SELECT json_extract(j, '$.x'), sum( json_extract(j, '$.x') ) FROM t1 WHERE a=? +} { + t1 USING INDEX i1 +} + + + +finish_test + diff --git a/test/indexfault.test b/test/indexfault.test index efe493219e..0e65179a32 100644 --- a/test/indexfault.test +++ b/test/indexfault.test @@ -337,6 +337,16 @@ do_faultsim_test 4.2 -faults custom -prep { faultsim_test_result {0 {}} } +do_faultsim_test 5 -prep { + reset_db +} -body { + execsql { + CREATE TABLE reallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallylongname(a PRIMARY KEY) WITHOUT ROWID; + } +} -test { + faultsim_test_result {0 {}} +} + uninstall_custom_faultsim finish_test diff --git a/test/insert.test b/test/insert.test index cb675b90d1..fd08eb43b8 100644 --- a/test/insert.test +++ b/test/insert.test @@ -1,4 +1,4 @@ -# 2001 September 15 +# 2001-09-15 # # The author disclaims copyright to this source code. In place of # a legal notice, here is a blessing: @@ -11,7 +11,6 @@ # This file implements regression tests for SQLite library. The # focus of this file is testing the INSERT statement. # -# $Id: insert.test,v 1.31 2007/04/05 11:25:59 drh Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl @@ -414,7 +413,7 @@ do_execsql_test insert-11.1 { # More columns of input than there are columns in the table. -# Ticket http://www.sqlite.org/src/info/e9654505cfda9361 +# Ticket http://sqlite.org/src/info/e9654505cfda9361 # do_execsql_test insert-12.1 { CREATE TABLE t12a(a,b,c,d,e,f,g); @@ -435,7 +434,170 @@ do_execsql_test insert-12.3 { SELECT * FROM t12c; } {one xyzzy two} +# 2018-06-11. From OSSFuzz. A column cache malfunction in +# the constraint checking on an index of expressions causes +# an assertion fault in a REPLACE. Ticket +# https://sqlite.org/src/info/c2432ef9089ee73b +# +do_execsql_test insert-13.1 { + DROP TABLE IF EXISTS t13; + CREATE TABLE t13(a INTEGER PRIMARY KEY,b UNIQUE); + CREATE INDEX t13x1 ON t13(-b=b); + INSERT INTO t13 VALUES(1,5),(6,2); + REPLACE INTO t13 SELECT b,0 FROM t13; + SELECT * FROM t13 ORDER BY +b; +} {2 0 6 2 1 5} + +# 2019-01-17. From the chromium fuzzer. +# +do_execsql_test insert-14.1 { + DROP TABLE IF EXISTS t14; + CREATE TABLE t14(x INTEGER PRIMARY KEY); + INSERT INTO t14 VALUES(CASE WHEN 1 THEN null END); + SELECT x FROM t14; +} {1} + +integrity_check insert-14.2 + +# 2019-08-12. +# +do_execsql_test insert-15.1 { + DROP TABLE IF EXISTS t1; + DROP TABLE IF EXISTS t2; + CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT); + CREATE INDEX i1 ON t1(b); + CREATE TABLE t2(a, b); + INSERT INTO t2 VALUES(4, randomblob(31000)); + INSERT INTO t2 VALUES(4, randomblob(32000)); + INSERT INTO t2 VALUES(4, randomblob(33000)); + REPLACE INTO t1 SELECT a, b FROM t2; + SELECT a, length(b) FROM t1; +} {4 33000} + +# 2019-10-16 +# ticket https://sqlite.org/src/info/a8a4847a2d96f5de +# On a REPLACE INTO, if an AFTER trigger adds back the conflicting +# row, you can end up with the wrong number of rows in an index. +# +db close +sqlite3 db :memory: +do_catchsql_test insert-16.1 { + PRAGMA recursive_triggers = true; + CREATE TABLE t0(c0,c1); + CREATE UNIQUE INDEX i0 ON t0(c0); + INSERT INTO t0(c0,c1) VALUES(123,1); + CREATE TRIGGER tr0 AFTER DELETE ON t0 + BEGIN + INSERT INTO t0 VALUES(123,2); + END; + REPLACE INTO t0(c0,c1) VALUES(123,3); +} {1 {UNIQUE constraint failed: t0.c0}} +do_execsql_test insert-16.2 { + SELECT * FROM t0; +} {123 1} +integrity_check insert-16.3 +do_catchsql_test insert-16.4 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b); + CREATE INDEX t1b ON t1(b); + INSERT INTO t1 VALUES(1, 'one'); + CREATE TRIGGER tr3 AFTER DELETE ON t1 BEGIN + INSERT INTO t1 VALUES(1, 'three'); + END; + REPLACE INTO t1 VALUES(1, 'two'); +} {1 {UNIQUE constraint failed: t1.a}} +integrity_check insert-16.5 +do_catchsql_test insert-16.6 { + PRAGMA foreign_keys = 1; + CREATE TABLE p1(a, b UNIQUE); + CREATE TABLE c1(c, d REFERENCES p1(b) ON DELETE CASCADE); + CREATE TRIGGER tr6 AFTER DELETE ON c1 BEGIN + INSERT INTO p1 VALUES(4, 1); + END; + INSERT INTO p1 VALUES(1, 1); + INSERT INTO c1 VALUES(2, 1); + REPLACE INTO p1 VALUES(3, 1);2 +} {1 {UNIQUE constraint failed: p1.b}} +integrity_check insert-16.7 + +# 2019-10-25 ticket c1e19e12046d23fe +do_catchsql_test insert-17.1 { + PRAGMA temp.recursive_triggers = true; + DROP TABLE IF EXISTS t0; + CREATE TABLE t0(aa, bb); + CREATE UNIQUE INDEX t0bb ON t0(bb); + CREATE TRIGGER "r17.1" BEFORE DELETE ON t0 + BEGIN INSERT INTO t0(aa,bb) VALUES(99,1); + END; + INSERT INTO t0(aa,bb) VALUES(10,20); + REPLACE INTO t0(aa,bb) VALUES(30,20); +} {1 {UNIQUE constraint failed: t0.rowid}} +integrity_check insert-17.2 +do_catchsql_test insert-17.3 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a, b UNIQUE, c UNIQUE); + INSERT INTO t1(a,b,c) VALUES(1,1,1),(2,2,2),(3,3,3),(4,4,4); + CREATE TRIGGER "r17.3" AFTER DELETE ON t1 WHEN OLD.c<>3 BEGIN + INSERT INTO t1(rowid,a,b,c) VALUES(100,100,100,3); + END; + REPLACE INTO t1(rowid,a,b,c) VALUES(200,1,2,3); +} {1 {UNIQUE constraint failed: t1.c}} +integrity_check insert-17.4 +do_execsql_test insert-17.5 { + CREATE TABLE t2(a INTEGER PRIMARY KEY, b); + CREATE UNIQUE INDEX t2b ON t2(b); + INSERT INTO t2(a,b) VALUES(1,1),(2,2),(3,3),(4,4); + CREATE TABLE fire(x); + CREATE TRIGGER t2r1 AFTER DELETE ON t2 BEGIN + INSERT INTO fire VALUES(old.a); + END; + UPDATE OR REPLACE t2 SET a=4, b=3 WHERE a=1; + SELECT *, 'x' FROM t2 ORDER BY a; +} {2 2 x 4 3 x} +do_execsql_test insert-17.6 { + SELECT x FROM fire ORDER BY x; +} {3 4} +do_execsql_test insert-17.7 { + DELETE FROM t2; + DELETE FROM fire; + INSERT INTO t2(a,b) VALUES(1,1),(2,2),(3,3),(4,4); + UPDATE OR REPLACE t2 SET a=1, b=3 WHERE a=1; + SELECT *, 'x' FROM t2 ORDER BY a; +} {1 3 x 2 2 x 4 4 x} +do_execsql_test insert-17.8 { + SELECT x FROM fire ORDER BY x; +} {3} +do_execsql_test insert-17.10 { + CREATE TABLE t3(a INTEGER PRIMARY KEY, b INT, c INT, d INT); + CREATE UNIQUE INDEX t3bpi ON t3(b) WHERE c<=d; + CREATE UNIQUE INDEX t3d ON t3(d); + INSERT INTO t3(a,b,c,d) VALUES(1,1,1,1),(2,1,3,2),(3,4,5,6); + CREATE TRIGGER t3r1 AFTER DELETE ON t3 BEGIN + SELECT 'hi'; + END; + REPLACE INTO t3(a,b,c,d) VALUES(4,4,8,9); +} {} +do_execsql_test insert-17.11 { + SELECT *, 'x' FROM t3 ORDER BY a; +} {1 1 1 1 x 2 1 3 2 x 4 4 8 9 x} +do_execsql_test insert-17.12 { + REPLACE INTO t3(a,b,c,d) VALUES(5,1,11,2); + SELECT *, 'x' FROM t3 ORDER BY a; +} {1 1 1 1 x 4 4 8 9 x 5 1 11 2 x} + +do_execsql_test insert-17.13 { + DELETE FROM t3; + INSERT INTO t3(a,b,c,d) VALUES(1,1,1,1),(2,1,3,2),(3,4,5,6); + DROP TRIGGER t3r1; + CREATE TRIGGER t3r1 AFTER DELETE ON t3 BEGIN + INSERT INTO t3(b,c,d) VALUES(old.b,old.c,old.d); + END; +} {} +do_catchsql_test insert-17.14 { + REPLACE INTO t3(a,b,c,d) VALUES(4,4,8,9); +} {1 {UNIQUE constraint failed: t3.b}} +do_catchsql_test insert-17.15 { + REPLACE INTO t3(a,b,c,d) VALUES(5,1,11,2); +} {1 {UNIQUE constraint failed: t3.d}} -integrity_check insert-99.0 finish_test diff --git a/test/insert4.test b/test/insert4.test index 3eece87e5f..8bd65a006f 100644 --- a/test/insert4.test +++ b/test/insert4.test @@ -11,10 +11,10 @@ # This file implements regression tests for SQLite library. The # focus of this file is testing the INSERT transfer optimization. # -# $Id: insert4.test,v 1.10 2008/01/21 16:22:46 drh Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl +set testprefix insert4 ifcapable !view||!subquery { finish_test @@ -33,8 +33,8 @@ proc xferopt_test {testname N} { # Create tables used for testing. # +sqlite3_db_config db LEGACY_FILE_FORMAT 0 execsql { - PRAGMA legacy_file_format = 0; CREATE TABLE t1(a int, b int, check(b>a)); CREATE TABLE t2(x int, y int); CREATE VIEW v2 AS SELECT y, x FROM t2; @@ -54,7 +54,7 @@ do_test insert4-1.1 { catchsql { INSERT INTO t1 SELECT * FROM t2; } -} {1 {CHECK constraint failed: t1}} +} {1 {CHECK constraint failed: b>a}} xferopt_test insert4-1.2 0 do_test insert4-1.3 { execsql { @@ -101,7 +101,7 @@ do_test insert4-2.3.3 { INSERT INTO t1 SELECT * FROM t2 LIMIT 1; SELECT * FROM t1; } -} {1 {CHECK constraint failed: t1}} +} {1 {CHECK constraint failed: b>a}} xferopt_test insert4-2.3.4 0 # Do not run the transfer optimization if there is a DISTINCT @@ -119,7 +119,7 @@ do_test insert4-2.4.3 { DELETE FROM t1; INSERT INTO t1 SELECT DISTINCT * FROM t2; } -} {1 {CHECK constraint failed: t1}} +} {1 {CHECK constraint failed: b>a}} xferopt_test insert4-2.4.4 0 # The following procedure constructs two tables then tries to transfer @@ -315,7 +315,7 @@ do_test insert4-6.6 { catchsql { INSERT INTO t6b SELECT * FROM t6a; } -} {1 {CHECK constraint failed: t6b}} +} {1 {CHECK constraint failed: x<>'abc' COLLATE nocase}} do_test insert4-6.7 { execsql { DROP TABLE t6b; @@ -324,7 +324,7 @@ do_test insert4-6.7 { catchsql { INSERT INTO t6b SELECT * FROM t6a; } -} {1 {CHECK constraint failed: t6b}} +} {1 {CHECK constraint failed: x COLLATE nocase <>'abc'}} # Ticket [6284df89debdfa61db8073e062908af0c9b6118e] # Disable the xfer optimization if the destination table contains @@ -566,4 +566,63 @@ do_catchsql_test insert4-9.1 { INSERT INTO t1(x) VALUES(5 COLLATE xyzzy) UNION SELECT 0; } {1 {no such collation sequence: xyzzy}} +#------------------------------------------------------------------------- +# Check that running an integrity-check does not disable the xfer +# optimization for tables with CHECK constraints. +# +do_execsql_test 10.1 { + CREATE TABLE t8( + rid INTEGER, + pid INTEGER, + mid INTEGER, + px INTEGER DEFAULT(0) CHECK(px IN(0, 1)) + ); + CREATE TEMP TABLE x( + rid INTEGER, + pid INTEGER, + mid INTEGER, + px INTEGER DEFAULT(0) CHECK(px IN(0, 1)) + ); +} +do_test 10.2 { + set sqlite3_xferopt_count 0 + execsql { INSERT INTO x SELECT * FROM t8 } + set sqlite3_xferopt_count +} {1} + +do_test 10.3 { + execsql { PRAGMA integrity_check } + set sqlite3_xferopt_count 0 + execsql { INSERT INTO x SELECT * FROM t8 } + set sqlite3_xferopt_count +} {1} + +do_test 10.4 { + execsql { PRAGMA integrity_check } + set sqlite3_xferopt_count 0 + execsql { INSERT INTO x SELECT * FROM t8 RETURNING * } + set sqlite3_xferopt_count +} {0} + +#------------------------------------------------------------------------- +# xfer transfer between tables where the source has an empty partial index. +# +do_execsql_test 11.0 { + CREATE TABLE t9(a, b, c); + CREATE INDEX t9a ON t9(a); + CREATE INDEX t9b ON t9(b) WHERE c=0; + + INSERT INTO t9 VALUES(1, 1, 1); + INSERT INTO t9 VALUES(2, 2, 2); + INSERT INTO t9 VALUES(3, 3, 3); + + CREATE TABLE t10(a, b, c); + CREATE INDEX t10a ON t10(a); + CREATE INDEX t10b ON t10(b) WHERE c=0; + + INSERT INTO t10 SELECT * FROM t9; + SELECT * FROM t10; + PRAGMA integrity_check; +} {1 1 1 2 2 2 3 3 3 ok} + finish_test diff --git a/test/insertfault.test b/test/insertfault.test new file mode 100644 index 0000000000..53849a1dbb --- /dev/null +++ b/test/insertfault.test @@ -0,0 +1,36 @@ +# 2019-01-26 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# Test cases for INSERT + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix insertfault + +do_execsql_test 1.0 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c, d DEFAULT true); + INSERT INTO t1 DEFAULT VALUES; + SELECT * FROM t1; +} {1 {} {} 1} +faultsim_save_and_close + +breakpoint +do_faultsim_test 1 -faults oom* -prep { + faultsim_restore_and_reopen + db eval { SELECT * FROM sqlite_master } +} -body { + execsql { SELECT * FROM t1 } +} -test { + faultsim_test_result {0 {1 {} {} 1}} +} + + +finish_test diff --git a/test/instr.test b/test/instr.test index c8be4862b9..326f8eb9ee 100644 --- a/test/instr.test +++ b/test/instr.test @@ -248,4 +248,34 @@ do_execsql_test instr-1.62 { SELECT coalesce(instr(NULL,NULL), 999); } {999} +do_execsql_test instr-1.63 { + SELECT instr(X'', 'abc') +} 0 +do_execsql_test instr-1.64 { + CREATE TABLE x1(a, b); + INSERT INTO x1 VALUES(X'', 'abc'); + SELECT instr(a, b) FROM x1; +} 0 + +# 2019-09-16 ticket https://sqlite.org/src/info/587791f92620090e +# +do_execsql_test instr-2.0 { + DROP TABLE IF EXISTS t0; + CREATE TABLE t0(c0 PRIMARY KEY, c1); + INSERT INTO t0(c0) VALUES (x'bb'), (0); + SELECT COUNT(*) FROM t0 WHERE INSTR(x'aabb', t0.c0) ORDER BY t0.c0, t0.c1; +} {1} +do_execsql_test instr-2.1 { + SELECT quote(c0) FROM t0 WHERE INSTR(x'aabb', t0.c0) ORDER BY t0.c0, t0.c1; +} {X'BB'} +do_execsql_test instr-2.2 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(x); + INSERT INTO t1(x) VALUES('text'),(x'bb'); + SELECT quote(x) FROM t1 WHERE instr(x'aabb',x); +} {X'BB'} +do_execsql_test instr-2.3 { + SELECT quote(x) FROM t1 WHERE x>'zzz' AND instr(x'aabb',x); +} {X'BB'} + finish_test diff --git a/test/instrfault.test b/test/instrfault.test new file mode 100644 index 0000000000..848e46e4c2 --- /dev/null +++ b/test/instrfault.test @@ -0,0 +1,92 @@ +# 2016 November 4 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is testing OOM error handling within the built-in +# INSTR() function. +# + + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix instrfault + +# Use big NEEDLE and HAYSTACK strings. Strings so large they cannot +# use lookaside buffers. +# +set ::NEEDLE [string repeat "abcdefghijklmnopqrstuvwxyz" 10] +set ::HAYSTACK "[string repeat 123 10]$NEEDLE[string repeat 456 10]" + +foreach {enc} { + utf8 + utf16 +} { + reset_db + sqlite3_db_config_lookaside db 0 0 0 + + execsql "PRAGMA encoding = $enc" + do_execsql_test 1.$enc.1 { + CREATE TABLE t1(n, h); + INSERT INTO t1 VALUES($::NEEDLE, $::HAYSTACK); + } {} + + do_faultsim_test 1.$enc.1 -faults oom-t* -prep { + execsql { SELECT instr(h, n) FROM t1 } + } -body { + execsql { SELECT instr(h, n) FROM t1 } + } -test { + faultsim_test_result {0 31} + } + + do_faultsim_test 1.$enc.2 -faults oom-t* -prep { + execsql { SELECT instr($::HAYSTACK, $::NEEDLE) FROM t1 } + } -body { + execsql { SELECT instr($::HAYSTACK, $::NEEDLE) FROM t1 } + } -test { + faultsim_test_result {0 31} + } + + do_faultsim_test 1.$enc.3 -faults oom-t* -prep { + set ::stmt [sqlite3_prepare_v2 db "SELECT instr(?, ?)" -1 dummy] + sqlite3_bind_text $::stmt 1 $::HAYSTACK [string length $::HAYSTACK] + sqlite3_bind_text $::stmt 2 $::NEEDLE [string length $::NEEDLE] + } -body { + set rc [sqlite3_step $::stmt] + if {$rc=="SQLITE_NOMEM"} { error "out of memory" } + sqlite3_column_int $::stmt 0 + } -test { + faultsim_test_result {0 31} + sqlite3_finalize $::stmt + } + + do_faultsim_test 1.$enc.4 -faults oom-t* -prep { + set ::stmt [sqlite3_prepare_v2 db "SELECT instr(?, ?)" -1 dummy] + sqlite3_bind_blob $::stmt 1 $::HAYSTACK [string length $::HAYSTACK] + sqlite3_bind_blob $::stmt 2 $::NEEDLE [string length $::NEEDLE] + } -body { + set rc [sqlite3_step $::stmt] + if {$rc=="SQLITE_NOMEM"} { error "out of memory" } + sqlite3_column_int $::stmt 0 + } -test { + faultsim_test_result {0 31} + sqlite3_finalize $::stmt + } + + do_execsql_test 1.$enc.5.0 { + CREATE TABLE h1(a, b); + INSERT INTO h1 VALUES('abcdefg%200hijkl', randomblob(200)); + INSERT INTO h1 SELECT b, a FROM h1; + } + do_faultsim_test 1.$enc.5 -faults oom-t* -body { + execsql { SELECT rowid FROM h1 WHERE instr(a,b) } + } -test {} +} + +finish_test diff --git a/test/intarray.test b/test/intarray.test index 2aba080bd4..2250027c5d 100644 --- a/test/intarray.test +++ b/test/intarray.test @@ -42,11 +42,18 @@ do_test intarray-1.1 { set ia3 [sqlite3_intarray_create db ia3] set ia4 [sqlite3_intarray_create db ia4] db eval { - SELECT type, name FROM sqlite_temp_master + SELECT type, name FROM temp.sqlite_master ORDER BY name } } {table ia1 table ia2 table ia3 table ia4} +# Verify the ability to DROP and recreate an intarray virtual table. +do_test intarray-1.1b { + db eval {DROP TABLE ia1} + set rc [catch {sqlite3_intarray_create db ia1} ia1] + lappend rc $ia1 +} {/0 [0-9A-Z]+/} + do_test intarray-1.2 { db eval { SELECT b FROM t1 WHERE a IN ia3 ORDER BY a diff --git a/test/interrupt.test b/test/interrupt.test index 92ab4c3265..23d9869874 100644 --- a/test/interrupt.test +++ b/test/interrupt.test @@ -94,14 +94,28 @@ ifcapable {vacuum && !default_autovacuum} { } 1 } ifcapable {explain} { - do_test interrupt-2.5 { + do_test interrupt-2.5.1 { + sqlite3_is_interrupted $DB + } {0} + do_test interrupt-2.5.2 { + unset -nocomplain ::interrupt_count + set ::interrupt_count 0 set sql {EXPLAIN SELECT max(a,b), a, b FROM t1} execsql $sql - set rc [catch {db eval $sql {sqlite3_interrupt $DB}} msg] + set rc [catch {db eval $sql { + sqlite3_interrupt $DB; + incr ::interrupt_count [sqlite3_is_interrupted $DB]; + }} msg] lappend rc $msg } {1 interrupted} + do_test interrupt-2.5.3 { + set ::interrupt_count + } {1} } integrity_check interrupt-2.6 +do_test interrupt-2.7 { + sqlite3_is_interrupted $DB +} {0} # Ticket #594. If an interrupt occurs in the middle of a transaction # and that transaction is later rolled back, the internal schema tables do @@ -128,7 +142,7 @@ ifcapable tempdb { } {1 interrupted} do_test interrupt-3.$i.3 { execsql { - SELECT name FROM sqlite_temp_master; + SELECT name FROM temp.sqlite_master; } } {} do_test interrupt-3.$i.4 { @@ -139,7 +153,7 @@ ifcapable tempdb { do_test interrupt-3.$i.5 { catchsql {SELECT name FROM sqlite_temp_master}; execsql { - SELECT name FROM sqlite_temp_master; + SELECT name FROM temp.sqlite_master; } } {} } diff --git a/test/interrupt2.test b/test/interrupt2.test new file mode 100644 index 0000000000..b9c66bd1f4 --- /dev/null +++ b/test/interrupt2.test @@ -0,0 +1,159 @@ +# 2016 Aug 12 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this script is using the sqlite_interrupt() API to +# interrupt WAL checkpoint operations. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/wal_common.tcl +set testprefix interrupt2 + +if {[permutation]=="journaltest" || [permutation]=="inmemory_journal"} { + finish_test + return +} + +db close +testvfs tvfs -default 1 + +tvfs filter xWrite +tvfs script write_cb + +set ::trigger_interrupt 0 +proc write_cb {method args} { + set filename [lindex $args 0] + if {[file tail $filename]=="test.db" && $::trigger_interrupt} { + if {$::trigger_interrupt} { + incr ::trigger_interrupt -1 + if {$::trigger_interrupt==0} { sqlite3_interrupt db } + } + } + return 0 +} + +sqlite3 db test.db +do_execsql_test 1.0 { + CREATE TABLE t1(a, b); + CREATE INDEX t1a ON t1(a); + CREATE INDEX t1b ON t1(b); + PRAGMA journal_mode = wal; + + WITH ii(i) AS ( VALUES(1) UNION ALL SELECT i+1 FROM ii WHERE i<1000 ) + INSERT INTO t1 SELECT i, i FROM ii; +} {wal} + +foreach idelay { + 5 + 10 + 15 + 20 +} { + + set ::trigger_interrupt $idelay + do_catchsql_test 1.$idelay.1 { PRAGMA wal_checkpoint; } {1 interrupted} + do_execsql_test 1.$idelay.2 { SELECT count(*) FROM t1 } 1000 + + set ::trigger_interrupt $idelay + do_test 1.$idelay.3 { + list [catch { sqlite3_wal_checkpoint_v2 db truncate } msg] $msg + } {1 {SQLITE_INTERRUPT - interrupted}} + do_execsql_test 1.$idelay.4 { SELECT count(*) FROM t1 } 1000 +} + +#------------------------------------------------------------------------- +# Check that if there are other SQL statements running, a checkpoint does +# not clear the isInterrupted flag. +# +do_execsql_test 2.0 { + CREATE TEMP TABLE z1(a, b); + INSERT INTO z1 SELECT * FROM t1; +} + +do_test 2.1 { + set i 10 + set res [list [catch { + set i 10 + db eval {SELECT * FROM z1} { + incr i -1 + if {$i==0} { + set ::trigger_interrupt 10 + set cres [catch { sqlite3_wal_checkpoint_v2 db truncate } msg] + lappend cres $msg + } + } + } msg] $msg] + + list $cres $res +} {{1 {SQLITE_INTERRUPT - interrupted}} {1 interrupted}} + +do_execsql_test 2.0 { + SELECT count(*) FROM t1 + UNION ALL + SELECT count(*) FROM z1 +} {1000 1000} + +#------------------------------------------------------------------------- +# Check the effect of an interrupt during sqlite3_close(). +# +db_save_and_close + +db_restore_and_reopen +do_test 3.1.1 { + set ::trigger_interrupt 10 + db eval { SELECT * FROM sqlite_master } + db close + set {} {} +} {} +do_test 3.1.2 { + list [file exists test.db] [file exists test.db-wal] +} {1 1} + +db_restore_and_reopen +do_test 3.2.1 { + db eval { SELECT * FROM sqlite_master } + db close + set {} {} +} {} +do_test 3.2.2 { + list [file exists test.db] [file exists test.db-wal] +} {1 0} + +#------------------------------------------------------------------------- +# Check the effect of an interrupt during an automatic checkpoint +# +db_restore_and_reopen +do_test 4.0 { + execsql { PRAGMA wal_autocheckpoint = 10 } + set ::trigger_interrupt 10 + execsql { CREATE TABLE t2(x, y) } +} {} + +# The auto-checkpoint in test 4.0 should have been interrupted. So this +# db write should cause the wal file to grow. +do_test 4.1 { + set nFrame1 [wal_frame_count test.db-wal 1024] + execsql { CREATE TABLE t3(x, y) } + set nFrame2 [wal_frame_count test.db-wal 1024] + expr $nFrame2 > $nFrame1 +} {1} + +# The auto-checkpoint in test 4.0 should not have been interrupted. So +# this db write should not cause the wal file to grow. +do_test 4.2 { + set nFrame1 [wal_frame_count test.db-wal 1024] + execsql { CREATE TABLE t4(x, y) } + set nFrame2 [wal_frame_count test.db-wal 1024] + expr $nFrame2 == $nFrame1 +} {1} + +finish_test diff --git a/test/intpkey.test b/test/intpkey.test index 41858e5d46..a8d2fb2ffb 100644 --- a/test/intpkey.test +++ b/test/intpkey.test @@ -129,7 +129,7 @@ do_test intpkey-1.12.2 { EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a==4; } -} {/SEARCH TABLE t1 /} +} {/SEARCH t1 /} # Try to insert a non-integer value into the primary key field. This # should result in a data type mismatch. @@ -296,7 +296,7 @@ do_test intpkey-3.8 { count { SELECT * FROM t1 WHERE c=='world' AND a>7; } -} {11 hello world 4} +} {11 hello world 3} do_test intpkey-3.9 { count { SELECT * FROM t1 WHERE 7<a; @@ -604,5 +604,42 @@ do_test intpkey-15.7 { } } {} +# 2016-04-18 ticket https://sqlite.org/src/tktview/7d7525cb01b68712495d3a +# Be sure to escape quoted typenames. +# +do_execsql_test intpkey-16.0 { + CREATE TABLE t16a(id "INTEGER" PRIMARY KEY AUTOINCREMENT, b [TEXT], c `INT`); +} {} +do_execsql_test intpkey-16.1 { + PRAGMA table_info=t16a; +} {0 id INTEGER 0 {} 1 1 b TEXT 0 {} 0 2 c INT 0 {} 0} + +# 2016-05-06 ticket https://sqlite.org/src/tktview/16c9801ceba4923939085 +# When the schema contains an index on the IPK and no other index +# and a WHERE clause on a delete uses an OR where both sides referencing +# the IPK, then it is possible that the OP_Delete will fail because there +# deferred seek of the OP_Seek is not resolved prior to reaching the OP_Delete. +# +do_execsql_test intpkey-17.0 { + CREATE TABLE t17(x INTEGER PRIMARY KEY, y TEXT); + INSERT INTO t17(x,y) VALUES(123,'elephant'),(248,'giraffe'); + CREATE INDEX t17x ON t17(x); + DELETE FROM t17 WHERE x=99 OR x<130; + SELECT * FROM t17; +} {248 giraffe} +do_execsql_test intpkey-17.1 { + DROP INDEX t17x; + DELETE FROM t17; + INSERT INTO t17(x,y) VALUES(123,'elephant'),(248,'giraffe'); + CREATE UNIQUE INDEX t17x ON t17(abs(x)); + DELETE FROM t17 WHERE abs(x) IS NULL OR abs(x)<130; + SELECT * FROM t17; +} {248 giraffe} +do_execsql_test intpkey-17.2 { + DELETE FROM t17; + INSERT INTO t17(x,y) VALUES(123,'elephant'),(248,'giraffe'); + UPDATE t17 SET y='ostrich' WHERE abs(x)=248; + SELECT * FROM t17 ORDER BY +x; +} {123 elephant 248 ostrich} finish_test diff --git a/test/intreal.test b/test/intreal.test new file mode 100644 index 0000000000..d9ffe9909e --- /dev/null +++ b/test/intreal.test @@ -0,0 +1,116 @@ +# 2019-05-03 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# Tests to exercise the MEM_IntReal representation of Mem objects. +# +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set ::testprefix intreal + +sqlite3_create_function db +do_execsql_test 100 { + SELECT intreal(5); +} {5.0} +do_execsql_test 110 { + SELECT intreal(5)=5, 6=intreal(6); +} {1 1} +do_execsql_test 120 { + SELECT intreal(7)=7.0, 8.0=intreal(8); +} {1 1} +do_execsql_test 130 { + SELECT typeof(intreal(9)); +} {real} +do_execsql_test 140 { + SELECT 'a'||intreal(11)||'z'; +} {a11.0z} + +do_execsql_test 150 { + SELECT max(1.0,intreal(2),3.0), max(1,intreal(2),3); +} {3.0 3} +do_execsql_test 160 { + SELECT max(1.0,intreal(4),3.0), max(1,intreal(4),3); +} {4.0 4.0} +do_execsql_test 170 { + SELECT max(1.0,intreal(2),intreal(3),4.0), + max(1,intreal(2),intreal(3),4); +} {4.0 4} +do_execsql_test 180 { + SELECT max(1.0,intreal(5),intreal(3),4.0), + max(1,intreal(5),intreal(3),4); +} {5.0 5.0} + +#------------------------------------------------------------------------- +do_execsql_test 2.1 { + CREATE TABLE t2(a REAL); + INSERT INTO t2 VALUES( 836627109860825358 ); + SELECT substr(a,1,4) FROM t2 WHERE a = CAST(836627109860825358 AS REAL); +} {8.36} + +do_execsql_test 2.2 { + CREATE INDEX i2 ON t2(a); + SELECT substr(a,1,4) FROM t2 WHERE a = CAST(836627109860825358 AS REAL); +} {8.36} + +do_execsql_test 2.3 { + CREATE TABLE t0 (c0); + CREATE TABLE t1 (c1 REAL); + INSERT INTO t1(c1) VALUES (8366271098608253588); + INSERT INTO t0(c0) VALUES ('a'); +} +set D [db one {SELECT c1 FROM t1}] + +do_execsql_test 2.4 { + SELECT * FROM t1 WHERE (t1.c1 = CAST(8366271098608253588 AS REAL)); +} $D + +do_execsql_test 2.5 { + SELECT * FROM t0, t1 WHERE (t1.c1 = CAST(8366271098608253588 AS REAL)); +} [list a $D] + +do_execsql_test 2.6 { + SELECT * FROM t0, t1 + WHERE ( + t1.c1 >= CAST(8366271098608253588 AS REAL) + AND t1.c1 <= CAST(8366271098608253588 AS REAL) + ); +} [list a $D] + +# 2019-07-29 ticket ba2f4585cf495231 +# +db close +sqlite3 db :memory: +do_execsql_test 3.0 { + CREATE TABLE t0 (c0 REAL, c1); + CREATE UNIQUE INDEX i0 ON t0(c1, 0 | c0); + INSERT INTO t0(c0) VALUES (4750228396194493326), (0); + UPDATE OR REPLACE t0 SET c0 = 'a', c1 = ''; + SELECT * FROM t0 ORDER BY t0.c1; + PRAGMA integrity_check; +} {a {} ok} + + +reset_db +do_execsql_test 4.0 { + CREATE TABLE t1(a REAL, b AS ('expr') ); +} +do_execsql_test 4.1 { + INSERT INTO t1 VALUES( REPLACE(0, '', 'expr') ); +} +do_execsql_test 4.2 { + INSERT INTO t1 SELECT REPLACE(4, '', 'expr'); +} +do_execsql_test 4.3 { + SELECT typeof(a), a FROM t1; +} { + real 0.0 + real 4.0 +} + +finish_test diff --git a/test/io.test b/test/io.test index c5086c10ec..1dc84bdee0 100644 --- a/test/io.test +++ b/test/io.test @@ -424,7 +424,7 @@ ifcapable pager_pragmas { # The COMMIT requires a single fsync() - to the database file. execsql { COMMIT } list [file size test.db] [nSync] - } {39936 1} + } "[expr {[nonzero_reserved_bytes]?40960:39936}] 1" } #---------------------------------------------------------------------- @@ -443,7 +443,7 @@ sqlite3_simulate_device -char safe_append # on the journal file between steps (2) and (3) above. # set expected_sync_count 2 -if {$::tcl_platform(platform)=="unix"} { +if {$::tcl_platform(os) ne "Windows NT"} { ifcapable dirsync { incr expected_sync_count } @@ -465,7 +465,7 @@ do_test io-4.2.1 { execsql { INSERT INTO abc VALUES('c', 'd') } file exists test.db-journal } {1} -if {$::tcl_platform(platform)=="unix"} { +if {$::tcl_platform(platform) eq "unix"} { do_test io-4.2.2 { hexio_read test.db-journal 8 4 } {FFFFFFFF} @@ -640,4 +640,6 @@ foreach {tn sql} { } sqlite3_simulate_device -char {} -sectorsize 0 +unregister_devsim + finish_test diff --git a/test/ioerr.test b/test/ioerr.test index e59647fe50..c76c9c1e6f 100644 --- a/test/ioerr.test +++ b/test/ioerr.test @@ -172,7 +172,7 @@ ifcapable crashtest&&attach { # These tests can't be run on windows because the windows version of # SQLite holds a mandatory exclusive lock on journal files it has open. # -if {$tcl_platform(platform)!="windows"} { +if {$tcl_platform(platform) ne"windows" && ![atomic_batch_write test.db]} { do_ioerr_test ioerr-7 -tclprep { db close sqlite3 db2 test2.db @@ -211,7 +211,7 @@ do_ioerr_test ioerr-8 -ckrefcount true -tclprep { # For test coverage: Cause an IO error whilst reading the master-journal # name from a journal file. -if {$tcl_platform(platform)=="unix"} { +if {$tcl_platform(platform) eq "unix" && [atomic_batch_write test.db]==0} { do_ioerr_test ioerr-9 -ckrefcount true -tclprep { execsql { CREATE TABLE t1(a,b,c); @@ -225,7 +225,7 @@ if {$tcl_platform(platform)=="unix"} { } forcecopy test2.db-journal test.db-journal set f [open test.db-journal a] - fconfigure $f -encoding binary + fconfigure $f -translation binary puts -nonewline $f "hello" puts -nonewline $f "\x00\x00\x00\x05\x01\x02\x03\x04" puts -nonewline $f "\xd9\xd5\x05\xf9\x20\xa1\x63\xd7" @@ -334,6 +334,7 @@ do_ioerr_test ioerr-12 -ckrefcount true -erc 1 -tclprep { } sqlite3_simulate_device -char {} -sectorsize 0 catch {db close} +unregister_devsim do_ioerr_test ioerr-13 -ckrefcount true -erc 1 -sqlprep { PRAGMA auto_vacuum = incremental; diff --git a/test/ioerr5.test b/test/ioerr5.test index a430f53407..a0a74bd953 100644 --- a/test/ioerr5.test +++ b/test/ioerr5.test @@ -117,7 +117,7 @@ foreach locking_mode {normal exclusive} { # Read the contents of the database file into a Tcl variable. # set fd [open test.db] - fconfigure $fd -translation binary -encoding binary + fconfigure $fd -translation binary set zDatabase [read $fd] close $fd @@ -138,7 +138,7 @@ foreach locking_mode {normal exclusive} { # do_test ioerr5-1.$locking_mode-$iFail.4 { set fd [open test.db] - fconfigure $fd -translation binary -encoding binary + fconfigure $fd -translation binary set zDatabase2 [read $fd] close $fd expr {$zDatabase eq $zDatabase2} diff --git a/test/istrue.test b/test/istrue.test new file mode 100644 index 0000000000..f1ba63248f --- /dev/null +++ b/test/istrue.test @@ -0,0 +1,206 @@ +# 2018-02-26 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is testing expressions of the form +# +# x IS TRUE +# x IS FALSE +# x IS NOT TRUE +# x IS NOT FALSE +# +# Tests are also included for the use of TRUE and FALSE as +# literal values. + +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +do_execsql_test istrue-100 { + CREATE TABLE t1(x INTEGER PRIMARY KEY, y BOOLEAN); + INSERT INTO t1 VALUES(1, true),(2, false),(3, null); + SELECT x FROM t1 WHERE y IS TRUE; +} {1} +do_execsql_test istrue-110 { + SELECT x FROM t1 WHERE y IS FALSE; +} {2} +do_execsql_test istrue-120 { + SELECT x FROM t1 WHERE y IS NULL; +} {3} +do_execsql_test istrue-130 { + SELECT x FROM t1 WHERE y IS NOT TRUE; +} {2 3} +do_execsql_test istrue-140 { + SELECT x FROM t1 WHERE y IS NOT FALSE; +} {1 3} +do_execsql_test istrue-150 { + SELECT x FROM t1 WHERE y IS NOT NULL; +} {1 2} +unset -nocomplain X +set X 9 +do_execsql_test istrue-160 { + SELECT x FROM t1 WHERE y IS TRUE OR (8==$X) +} {1} +do_execsql_test istrue-170 { + SELECT x FROM t1 WHERE y IS FALSE OR (8==$X) +} {2} +do_execsql_test istrue-180 { + SELECT x FROM t1 WHERE y IS NULL OR (8==$X); +} {3} +do_execsql_test istrue-190 { + SELECT x FROM t1 WHERE y IS NOT TRUE OR (8==$X); +} {2 3} +do_execsql_test istrue-200 { + SELECT x FROM t1 WHERE y IS NOT FALSE OR (8==$X); +} {1 3} +do_execsql_test istrue-210 { + SELECT x FROM t1 WHERE y IS NOT NULL OR (8==$X); +} {1 2} + +do_execsql_test istrue-300 { + SELECT x, + y IS TRUE, y IS FALSE, y is NULL, + y IS NOT TRUE, y IS NOT FALSE, y IS NOT NULL, '|' + FROM t1 ORDER BY x; +} {1 1 0 0 0 1 1 | 2 0 1 0 1 0 1 | 3 0 0 1 1 1 0 |} + +do_execsql_test istrue-400 { + SELECT x FROM t1 WHERE true; +} {1 2 3} +do_execsql_test istrue-410 { + SELECT x FROM t1 WHERE false; +} {} + +do_execsql_test istrue-500 { + CREATE TABLE t2( + a INTEGER PRIMARY KEY, + b BOOLEAN DEFAULT true, + c BOOLEAN DEFAULT(true), + d BOOLEAN DEFAULT false, + e BOOLEAN DEFAULT(false) + ); + INSERT INTO t2 DEFAULT VALUES; + SELECT * FROM t2; +} {1 1 1 0 0} +do_execsql_test istrue-510 { + DROP TABLE t2; + CREATE TABLE t2( + a INTEGER PRIMARY KEY, + b BOOLEAN DEFAULT(not true), + c BOOLEAN DEFAULT(not false) + ); + INSERT INTO t2(a) VALUES(99); + SELECT * FROM t2; +} {99 0 1} +do_execsql_test istrue-520 { + DROP TABLE t2; + CREATE TABLE t2( + a INTEGER PRIMARY KEY, + b BOOLEAN CHECK(b IS TRUE), + c BOOLEAN CHECK(c IS FALSE), + d BOOLEAN CHECK(d IS NOT TRUE), + e BOOLEAN CHECK(e IS NOT FALSE) + ); + INSERT INTO t2 VALUES(1,true,false,null,null); + SELECT * FROM t2; +} {1 1 0 {} {}} +do_catchsql_test istrue-521 { + INSERT INTO t2 VALUES(2,false,false,null,null); +} {1 {CHECK constraint failed: b IS TRUE}} +do_catchsql_test istrue-522 { + INSERT INTO t2 VALUES(2,true,true,null,null); +} {1 {CHECK constraint failed: c IS FALSE}} +do_catchsql_test istrue-523 { + INSERT INTO t2 VALUES(2,true,false,true,null); +} {1 {CHECK constraint failed: d IS NOT TRUE}} +do_catchsql_test istrue-524 { + INSERT INTO t2 VALUES(2,true,false,null,false); +} {1 {CHECK constraint failed: e IS NOT FALSE}} + +foreach {tn val} [list 1 NaN 2 -NaN 3 NaN0 4 -NaN0 5 Inf 6 -Inf] { + do_execsql_test istrue-600.$tn.1 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(x); + } + do_test istrue-600.$tn.2 { + set ::STMT [sqlite3_prepare db "INSERT INTO t1 VALUES(?)" -1 TAIL] + sqlite3_bind_double $::STMT 1 $val + sqlite3_step $::STMT + sqlite3_reset $::STMT + sqlite3_finalize $::STMT + } {SQLITE_OK} + do_execsql_test istrue-600.$tn.3 { + SELECT x IS TRUE FROM t1; + } [expr {$tn in [list 5 6] ? {1} : {0}}] + do_execsql_test istrue-600.$tn.4 { + SELECT x IS FALSE FROM t1; + } {0} +} + +ifcapable altertable { + do_execsql_test istrue-700 { + CREATE TABLE t7( + a INTEGER PRIMARY KEY, + b BOOLEAN DEFAULT false, + c BOOLEAN DEFAULT true + ); + INSERT INTO t7(a) VALUES(1); + INSERT INTO t7(a,b,c) VALUES(2,true,false); + ALTER TABLE t7 ADD COLUMN d BOOLEAN DEFAULT false; + ALTER TABLE t7 ADD COLUMN e BOOLEAN DEFAULT true; + INSERT INTO t7(a,b,c) VALUES(3,true,false); + INSERT INTO t7 VALUES(4,false,true,true,false); + SELECT *,'x' FROM t7 ORDER BY a; + } {1 0 1 0 1 x 2 1 0 0 1 x 3 1 0 0 1 x 4 0 1 1 0 x} +} + +do_execsql_test istrue-710 { + SELECT 0.5 IS TRUE COLLATE NOCASE; + SELECT 0.5 IS TRUE COLLATE RTRIM; + SELECT 0.5 IS TRUE COLLATE BINARY; + + SELECT 0.5 IS TRUE; + SELECT 0.5 COLLATE NOCASE IS TRUE; + SELECT 0.0 IS FALSE; + + SELECT 0.0 IS FALSE COLLATE NOCASE; + SELECT 0.0 IS FALSE COLLATE RTRIM; + SELECT 0.0 IS FALSE COLLATE BINARY; +} {1 1 1 1 1 1 1 1 1} + +# 2020-06-12 bug report from Chromium +# https://bugs.chromium.org/p/chromium/issues/detail?id=1094247 +do_catchsql_test istrue-800 { + SELECT 9 IN (false.false); +} {1 {no such column: false.false}} +do_execsql_test istrue-810 { + CREATE TABLE t8(a INT, true INT, false INT, d INT); + INSERT INTO t8(a,true,false,d) VALUES(5,6,7,8),(4,3,2,1),('a','b','c','d'); + SELECT * FROM t8 ORDER BY false; +} {4 3 2 1 5 6 7 8 a b c d} +do_catchsql_test istrue-820 { + SELECT 9 IN (false.false) FROM t8; +} {1 {no such column: false.false}} +do_execsql_test istrue-830 { + CREATE TABLE false(true INT, false INT, x INT CHECK (5 IN (false.false))); +} {} +do_execsql_test istrue-840 { + INSERT INTO False VALUES(4,5,6); +} {} +do_catchsql_test istrue-841 { + INSERT INTO False VALUES(5,6,7); +} {1 {CHECK constraint failed: 5 IN (false.false)}} +do_execsql_test istrue-850 { + SELECT 9 IN (false.false) FROM false; +} {0} +do_execsql_test istrue-851 { + SELECT 5 IN (false.false) FROM false; +} {1} + +finish_test diff --git a/test/join.test b/test/join.test index 2b6951213f..b33a7560a1 100644 --- a/test/join.test +++ b/test/join.test @@ -1,4 +1,4 @@ -# 2002 May 24 +# 2002-05-24 # # The author disclaims copyright to this source code. In place of # a legal notice, here is a blessing: @@ -12,7 +12,6 @@ # # This file implements tests for joins, including outer joins. # -# $Id: join.test,v 1.27 2009/07/01 16:12:08 danielk1977 Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl @@ -251,6 +250,19 @@ do_test join-2.1 { } } {1 2 3 4 2 3 4 5 3 4 5 {}} +# EVIDENCE-OF: R-52129-05406 you can say things like "OUTER LEFT NATURAL +# JOIN" which means the same as "NATURAL LEFT OUTER JOIN". +do_test join-2.1b { + execsql { + SELECT * FROM t1 OUTER LEFT NATURAL JOIN t2; + } +} {1 2 3 4 2 3 4 5 3 4 5 {}} +do_test join-2.1c { + execsql { + SELECT * FROM t1 NATURAL LEFT OUTER JOIN t2; + } +} {1 2 3 4 2 3 4 5 3 4 5 {}} + # ticket #3522 do_test join-2.1.1 { execsql2 { @@ -273,11 +285,13 @@ do_test join-2.2 { SELECT * FROM t2 NATURAL LEFT OUTER JOIN t1; } } {1 2 3 {} 2 3 4 1 3 4 5 2} -do_test join-2.3 { - catchsql { - SELECT * FROM t1 NATURAL RIGHT OUTER JOIN t2; - } -} {1 {RIGHT and FULL OUTER JOINs are not currently supported}} + +#do_test join-2.3 { +# catchsql { +# SELECT * FROM t1 NATURAL RIGHT OUTER JOIN t2; +# } +#} {1 {RIGHT and FULL OUTER JOINs are not currently supported}} + do_test join-2.4 { execsql { SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.d @@ -308,7 +322,7 @@ do_test join-3.3 { catchsql { SELECT * FROM t1 JOIN t2 ON t1.a=t2.b USING(b); } -} {1 {cannot have both ON and USING clauses in the same join}} +} {1 {near "USING": syntax error}} do_test join-3.4.1 { catchsql { SELECT * FROM t1 JOIN t2 USING(a); @@ -327,36 +341,39 @@ do_test join-3.6 { SELECT * FROM t1 JOIN t2 ON t3.a=t2.b; } } {1 {no such column: t3.a}} + +# EVIDENCE-OF: R-47973-48020 you cannot say "INNER OUTER JOIN", because +# that would be contradictory. do_test join-3.7 { catchsql { SELECT * FROM t1 INNER OUTER JOIN t2; } -} {1 {unknown or unsupported join type: INNER OUTER}} +} {1 {unknown join type: INNER OUTER}} do_test join-3.8 { catchsql { SELECT * FROM t1 INNER OUTER CROSS JOIN t2; } -} {1 {unknown or unsupported join type: INNER OUTER CROSS}} +} {1 {unknown join type: INNER OUTER CROSS}} do_test join-3.9 { catchsql { SELECT * FROM t1 OUTER NATURAL INNER JOIN t2; } -} {1 {unknown or unsupported join type: OUTER NATURAL INNER}} +} {1 {unknown join type: OUTER NATURAL INNER}} do_test join-3.10 { catchsql { SELECT * FROM t1 LEFT BOGUS JOIN t2; } -} {1 {unknown or unsupported join type: LEFT BOGUS}} +} {1 {unknown join type: LEFT BOGUS}} do_test join-3.11 { catchsql { SELECT * FROM t1 INNER BOGUS CROSS JOIN t2; } -} {1 {unknown or unsupported join type: INNER BOGUS CROSS}} +} {1 {unknown join type: INNER BOGUS CROSS}} do_test join-3.12 { catchsql { SELECT * FROM t1 NATURAL AWK SED JOIN t2; } -} {1 {unknown or unsupported join type: NATURAL AWK SED}} +} {1 {unknown join type: NATURAL AWK SED}} do_test join-4.1 { execsql { @@ -440,6 +457,7 @@ do_test join-5.1 { # A test for ticket #247. # do_test join-7.1 { + sqlite3_db_config db SQLITE_DBCONFIG_DQS_DML 1 execsql { CREATE TABLE t7 (x, y); INSERT INTO t7 VALUES ("pa1", 1); @@ -672,20 +690,26 @@ jointest join-12.5 65 {1 {at most 64 tables in a join}} jointest join-12.6 66 {1 {at most 64 tables in a join}} jointest join-12.7 127 {1 {at most 64 tables in a join}} jointest join-12.8 128 {1 {at most 64 tables in a join}} -jointest join-12.9 1000 {1 {at most 64 tables in a join}} - -# If SQLite is built with SQLITE_MEMDEBUG, then the huge number of realloc() -# calls made by the following test cases are too time consuming to run. -# Without SQLITE_MEMDEBUG, realloc() is fast enough that these are not -# a problem. -ifcapable pragma&&compileoption_diags { - if {[lsearch [db eval {PRAGMA compile_options}] MEMDEBUG]<0} { - jointest join-12.10 65534 {1 {at most 64 tables in a join}} - jointest join-12.11 65535 {1 {too many references to "t14": max 65535}} - jointest join-12.12 65536 {1 {too many references to "t14": max 65535}} - jointest join-12.13 65537 {1 {too many references to "t14": max 65535}} - } -} + +# As of 2019-01-17, the number of elements in a SrcList is limited +# to 200. The following tests still run, but the answer is now +# an SQLITE_NOMEM error. +# +# jointest join-12.9 1000 {1 {at most 64 tables in a join}} +# +# If SQLite is built with SQLITE_MEMDEBUG, then the huge number of realloc() +# calls made by the following test cases are too time consuming to run. +# Without SQLITE_MEMDEBUG, realloc() is fast enough that these are not +# a problem. +# +# ifcapable pragma&&compileoption_diags { +# if {[lsearch [db eval {PRAGMA compile_options}] MEMDEBUG]<0} { +# jointest join-12.10 65534 {1 {at most 64 tables in a join}} +# jointest join-12.11 65535 {1 {too many references to "t14": max 65535}} +# jointest join-12.12 65536 {1 {too many references to "t14": max 65535}} +# jointest join-12.13 65537 {1 {too many references to "t14": max 65535}} +# } +# } #------------------------------------------------------------------------- @@ -712,5 +736,637 @@ do_execsql_test join-13.2 { SELECT * FROM aa LEFT JOIN bb, cc WHERE cc.c=aa.a; } {45 {} 45 45 {} 45} +# Verify that that iTable attributes the TK_IF_NULL_ROW operators in the +# expression tree are correctly updated by the query flattener. This was +# a bug discovered on 2017-05-22 by Mark Brand. +# +do_execsql_test join-14.1 { + SELECT * + FROM (SELECT 1 a) AS x + LEFT JOIN (SELECT 1, * FROM (SELECT * FROM (SELECT 1))); +} {1 1 1} +do_execsql_test join-14.2 { + SELECT * + FROM (SELECT 1 a) AS x + LEFT JOIN (SELECT 1, * FROM (SELECT * FROM (SELECT * FROM (SELECT 1)))) AS y + JOIN (SELECT * FROM (SELECT 9)) AS z; +} {1 1 1 9} +do_execsql_test join-14.3 { + SELECT * + FROM (SELECT 111) + LEFT JOIN (SELECT cc+222, * FROM (SELECT * FROM (SELECT 333 cc))); +} {111 555 333} + +do_execsql_test join-14.4 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(c PRIMARY KEY, a TEXT(10000), b TEXT(10000)); + SELECT * FROM (SELECT 111) LEFT JOIN (SELECT c+222 FROM t1) GROUP BY 1; +} {111 {}} +do_execsql_test join-14.4b { + SELECT * FROM (SELECT 111) LEFT JOIN (SELECT c+222 FROM t1); +} {111 {}} +do_execsql_test join-14.5 { + SELECT * FROM (SELECT 111 AS x UNION ALL SELECT 222) + LEFT JOIN (SELECT c+333 AS y FROM t1) ON x=y GROUP BY 1; +} {111 {} 222 {}} +do_execsql_test join-14.5b { + SELECT count(*) + FROM (SELECT 111 AS x UNION ALL SELECT 222) + LEFT JOIN (SELECT c+333 AS y FROM t1) ON x=y; +} {2} +do_execsql_test join-14.5c { + SELECT count(*) + FROM (SELECT c+333 AS y FROM t1) + RIGHT JOIN (SELECT 111 AS x UNION ALL SELECT 222) ON x=y; +} {2} +do_execsql_test join-14.6 { + SELECT * FROM (SELECT 111 AS x UNION ALL SELECT 111) + LEFT JOIN (SELECT c+333 AS y FROM t1) ON x=y GROUP BY 1; +} {111 {}} +do_execsql_test join-14.7 { + SELECT * FROM (SELECT 111 AS x UNION ALL SELECT 111 UNION ALL SELECT 222) + LEFT JOIN (SELECT c+333 AS y FROM t1) ON x=y GROUP BY 1; +} {111 {} 222 {}} +do_execsql_test join-14.8 { + INSERT INTO t1(c) VALUES(-111); + SELECT * FROM (SELECT 111 AS x UNION ALL SELECT 111 UNION ALL SELECT 222) + LEFT JOIN (SELECT c+333 AS y FROM t1) ON x=y GROUP BY 1; +} {111 {} 222 222} +do_execsql_test join-14.9 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(c PRIMARY KEY) WITHOUT ROWID; + SELECT * FROM (SELECT 111) LEFT JOIN (SELECT c+222 FROM t1) GROUP BY 1; +} {111 {}} + +# Verify the fix to ticket +# https://sqlite.org/src/tktview/7fde638e94287d2c948cd9389 +# +db close +sqlite3 db :memory: +do_execsql_test join-14.10 { + CREATE TABLE t1(a); + INSERT INTO t1 VALUES(1),(2),(3); + CREATE VIEW v2 AS SELECT a, 1 AS b FROM t1; + CREATE TABLE t3(x); + INSERT INTO t3 VALUES(2),(4); + SELECT *, '|' FROM t3 LEFT JOIN v2 ON a=x WHERE b=1; +} {2 2 1 |} +do_execsql_test join-14.11 { + SELECT *, '|' FROM t3 LEFT JOIN v2 ON a=x WHERE b+1=x; +} {2 2 1 |} +do_execsql_test join-14.12 { + SELECT *, '|' FROM t3 LEFT JOIN v2 ON a=x ORDER BY b; +} {4 {} {} | 2 2 1 |} + +# Verify the fix for ticket +# https://sqlite.org/src/info/892fc34f173e99d8 +# +db close +sqlite3 db :memory: +do_execsql_test join-14.20 { + CREATE TABLE t1(id INTEGER PRIMARY KEY); + CREATE TABLE t2(id INTEGER PRIMARY KEY, c2 INTEGER); + CREATE TABLE t3(id INTEGER PRIMARY KEY, c3 INTEGER); + INSERT INTO t1(id) VALUES(456); + INSERT INTO t3(id) VALUES(1),(2); + SELECT t1.id, x2.id, x3.id + FROM t1 + LEFT JOIN (SELECT * FROM t2) AS x2 ON t1.id=x2.c2 + LEFT JOIN t3 AS x3 ON x2.id=x3.c3; +} {456 {} {}} + +# 2018-03-24. +# E.Pasma discovered that the LEFT JOIN strength reduction optimization +# was misbehaving. The problem turned out to be that the +# sqlite3ExprImpliesNotNull() routine was saying that CASE expressions +# like +# +# CASE WHEN true THEN true ELSE x=0 END +# +# could never be true if x is NULL. The following test cases verify +# that this error has been resolved. +# +db close +sqlite3 db :memory: +do_execsql_test join-15.100 { + CREATE TABLE t1(a INT, b INT); + INSERT INTO t1 VALUES(1,2),(3,4); + CREATE TABLE t2(x INT, y INT); + SELECT *, 'x' + FROM t1 LEFT JOIN t2 + WHERE CASE WHEN FALSE THEN a=x ELSE 1 END; +} {1 2 {} {} x 3 4 {} {} x} +do_execsql_test join-15.105 { + SELECT *, 'x' + FROM t1 LEFT JOIN t2 + WHERE a IN (1,3,x,y); +} {1 2 {} {} x 3 4 {} {} x} +do_execsql_test join-15.106a { + SELECT *, 'x' + FROM t1 LEFT JOIN t2 + WHERE NOT ( 'x'='y' AND t2.y=1 ); +} {1 2 {} {} x 3 4 {} {} x} +do_execsql_test join-15.106b { + SELECT *, 'x' + FROM t1 LEFT JOIN t2 + WHERE ~ ( 'x'='y' AND t2.y=1 ); +} {1 2 {} {} x 3 4 {} {} x} +do_execsql_test join-15.107 { + SELECT *, 'x' + FROM t1 LEFT JOIN t2 + WHERE t2.y IS NOT 'abc' +} {1 2 {} {} x 3 4 {} {} x} +do_execsql_test join-15.110 { + DROP TABLE t1; + DROP TABLE t2; + CREATE TABLE t1(a INTEGER PRIMARY KEY, b INTEGER); + INSERT INTO t1(a,b) VALUES(1,0),(11,1),(12,1),(13,1),(121,12); + CREATE INDEX t1b ON t1(b); + CREATE TABLE t2(x INTEGER PRIMARY KEY); + INSERT INTO t2(x) VALUES(0),(1); + SELECT a1, a2, a3, a4, a5 + FROM (SELECT a AS a1 FROM t1 WHERE b=0) + JOIN (SELECT x AS x1 FROM t2) + LEFT JOIN (SELECT a AS a2, b AS b2 FROM t1) + ON x1 IS TRUE AND b2=a1 + JOIN (SELECT x AS x2 FROM t2) + ON x2<=CASE WHEN x1 THEN CASE WHEN a2 THEN 1 ELSE -1 END ELSE 0 END + LEFT JOIN (SELECT a AS a3, b AS b3 FROM t1) + ON x2 IS TRUE AND b3=a2 + JOIN (SELECT x AS x3 FROM t2) + ON x3<=CASE WHEN x2 THEN CASE WHEN a3 THEN 1 ELSE -1 END ELSE 0 END + LEFT JOIN (SELECT a AS a4, b AS b4 FROM t1) + ON x3 IS TRUE AND b4=a3 + JOIN (SELECT x AS x4 FROM t2) + ON x4<=CASE WHEN x3 THEN CASE WHEN a4 THEN 1 ELSE -1 END ELSE 0 END + LEFT JOIN (SELECT a AS a5, b AS b5 FROM t1) + ON x4 IS TRUE AND b5=a4 + ORDER BY a1, a2, a3, a4, a5; +} {1 {} {} {} {} 1 11 {} {} {} 1 12 {} {} {} 1 12 121 {} {} 1 13 {} {} {}} + +# 2019-02-05 Ticket https://sqlite.org/src/tktview/5948e09b8c415bc45da5c +# Error in join due to the LEFT JOIN strength reduction optimization. +# +do_execsql_test join-16.100 { + DROP TABLE IF EXISTS t1; + DROP TABLE IF EXISTS t2; + CREATE TABLE t1(a INT); + INSERT INTO t1(a) VALUES(1); + CREATE TABLE t2(b INT); + SELECT a, b + FROM t1 LEFT JOIN t2 ON 0 + WHERE (b IS NOT NULL)=0; +} {1 {}} + +# 2019-08-17 ticket https://sqlite.org/src/tktview/6710d2f7a13a299728ab +# Ensure that constants that derive from the right-hand table of a LEFT JOIN +# are never factored out, since they are not really constant. +# +do_execsql_test join-17.100 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(x); + INSERT INTO t1(x) VALUES(0),(1); + SELECT * FROM t1 LEFT JOIN (SELECT abs(1) AS y FROM t1) ON x WHERE NOT(y='a'); +} {1 1 1 1} +do_execsql_test join-17.110 { + SELECT * FROM t1 LEFT JOIN (SELECT abs(1)+2 AS y FROM t1) ON x + WHERE NOT(y='a'); +} {1 3 1 3} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test join-18.1 { + CREATE TABLE t0(a); + CREATE TABLE t1(b); + CREATE VIEW v0 AS SELECT a FROM t1 LEFT JOIN t0; + INSERT INTO t1 VALUES (1); +} {} + +do_execsql_test join-18.2 { + SELECT * FROM v0 WHERE NOT(v0.a IS FALSE); +} {{}} + +do_execsql_test join-18.3 { + SELECT * FROM t1 LEFT JOIN t0 WHERE NOT(a IS FALSE); +} {1 {}} + +do_execsql_test join-18.4 { + SELECT NOT(v0.a IS FALSE) FROM v0 +} {1} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test join-19.0 { + CREATE TABLE t1(a); + CREATE TABLE t2(b); + INSERT INTO t1(a) VALUES(0); + CREATE VIEW v0(c) AS SELECT t2.b FROM t1 LEFT JOIN t2; +} + +do_execsql_test join-19.1 { + SELECT * FROM v0 WHERE v0.c NOTNULL NOTNULL; +} {{}} + +do_execsql_test join-19.2 { + SELECT * FROM t1 LEFT JOIN t2 +} {0 {}} + +do_execsql_test join-19.3 { + SELECT * FROM t1 LEFT JOIN t2 WHERE (b IS NOT NULL) IS NOT NULL; +} {0 {}} + +do_execsql_test join-19.4 { + SELECT (b IS NOT NULL) IS NOT NULL FROM t1 LEFT JOIN t2 +} {1} + +do_execsql_test join-19.5 { + SELECT * FROM t1 LEFT JOIN t2 WHERE + (b IS NOT NULL AND b IS NOT NULL) IS NOT NULL; +} {0 {}} + +# 2019-11-02 ticket 623eff57e76d45f6 +# The optimization of exclusing the WHERE expression of a partial index +# from the WHERE clause of the query if the index is used does not work +# of the table of the index is the right-hand table of a LEFT JOIN. +# +db close +sqlite3 db :memory: +do_execsql_test join-20.1 { + CREATE TABLE t1(c1); + CREATE TABLE t0(c0); + INSERT INTO t0(c0) VALUES (0); + SELECT * FROM t0 LEFT JOIN t1 WHERE NULL IN (c1); +} {} +do_execsql_test join-20.2 { + CREATE INDEX t1x ON t1(0) WHERE NULL IN (c1); + SELECT * FROM t0 LEFT JOIN t1 WHERE NULL IN (c1); +} {} + +# 2025-05-29 forum post 7dee41d32506c4ae +# The complaint in the forum post appears to be the same as for the +# ticket on 2019-11-02, only for RIGHT JOIN instead of LEFT JOIN. Note +# that RIGHT JOIN did not yet exist in SQLite when the ticket was +# written and fixed. +# +do_execsql_test join-20.3 { + DROP TABLE t1; + CREATE TABLE t1(x INT); INSERT INTO t1(x) VALUES(1); + CREATE TABLE t2(y BOOLEAN); INSERT INTO t2(y) VALUES(false); + CREATE TABLE t3(z INT); INSERT INTO t3(z) VALUES(3); + CREATE INDEX t2y ON t2(y) WHERE y; + SELECT quote(z) FROM t1 RIGHT JOIN t2 ON y LEFT JOIN t3 ON y; +} {NULL} + +# 2019-11-30 ticket 7f39060a24b47353 +# Do not allow a WHERE clause term to qualify a partial index on the +# right table of a LEFT JOIN. +# +do_execsql_test join-21.10 { + DROP TABLE t0; + DROP TABLE t1; + CREATE TABLE t0(aa); + CREATE TABLE t1(bb); + INSERT INTO t0(aa) VALUES (1); + INSERT INTO t1(bb) VALUES (1); + SELECT 11, * FROM t1 LEFT JOIN t0 WHERE aa ISNULL; + SELECT 12, * FROM t1 LEFT JOIN t0 WHERE +aa ISNULL; + SELECT 13, * FROM t1 LEFT JOIN t0 ON aa ISNULL; + SELECT 14, * FROM t1 LEFT JOIN t0 ON +aa ISNULL; + CREATE INDEX i0 ON t0(aa) WHERE aa ISNULL; + SELECT 21, * FROM t1 LEFT JOIN t0 WHERE aa ISNULL; + SELECT 22, * FROM t1 LEFT JOIN t0 WHERE +aa ISNULL; + SELECT 23, * FROM t1 LEFT JOIN t0 ON aa ISNULL; + SELECT 24, * FROM t1 LEFT JOIN t0 ON +aa ISNULL; +} {13 1 {} 14 1 {} 23 1 {} 24 1 {}} + +# 2019-12-18 problem with a LEFT JOIN where the RHS is a view. +# Detected by Yongheng and Rui. +# Follows from the optimization attempt of check-in 41c27bc0ff1d3135 +# on 2017-04-18 +# +reset_db +do_execsql_test join-22.10 { + CREATE TABLE t0(a, b); + CREATE INDEX t0a ON t0(a); + INSERT INTO t0 VALUES(10,10),(10,11),(10,12); + SELECT DISTINCT c FROM t0 LEFT JOIN (SELECT a+1 AS c FROM t0) ORDER BY c ; +} {11} + +# 2019-12-22 ticket 7929c1efb2d67e98 +# Verification of testtag-20230227a +# +# 2023-02-27 https://sqlite.org/forum/forumpost/422e635f3beafbf6 +# Verification of testtag-20230227a, testtag-20230227b, and testtag-20230227c +# +reset_db +ifcapable vtab { + do_execsql_test join-23.10 { + CREATE TABLE t0(c0); + INSERT INTO t0(c0) VALUES(123); + CREATE VIEW v0(c0) AS SELECT 0 GROUP BY 1; + SELECT t0.c0, v0.c0, vt0.name + FROM v0, t0 LEFT JOIN pragma_table_info('t0') AS vt0 + ON vt0.name LIKE 'c0' + WHERE v0.c0 == 0; + } {123 0 c0} + do_execsql_test join-23.20 { + CREATE TABLE a(value TEXT); + INSERT INTO a(value) SELECT value FROM json_each('["a", "b", null]'); + CREATE TABLE b(value TEXT); + INSERT INTO b(value) SELECT value FROM json_each('["a", "c", null]'); + SELECT a.value, b.value FROM a RIGHT JOIN b ON a.value = b.value; + } {a a {} c {} {}} + do_execsql_test join-23.21 { + SELECT a.value, b.value FROM b LEFT JOIN a ON a.value = b.value; + } {a a {} c {} {}} + do_execsql_test join-23.22 { + SELECT a.value, b.value + FROM json_each('["a", "c", null]') AS b + LEFT JOIN + json_each('["a", "b", null]') AS a ON a.value = b.value; + } {a a {} c {} {}} + do_execsql_test join-23.23 { + SELECT a.value, b.value + FROM json_each('["a", "b", null]') AS a + RIGHT JOIN + json_each('["a", "c", null]') AS b ON a.value = b.value; + } {a a {} c {} {}} + do_execsql_test join-23.24 { + SELECT a.value, b.value + FROM json_each('["a", "b", null]') AS a + RIGHT JOIN + b ON a.value = b.value; + } {a a {} c {} {}} + do_execsql_test join-23.25 { + SELECT a.value, b.value + FROM a + RIGHT JOIN + json_each('["a", "c", null]') AS b ON a.value = b.value; + } {a a {} c {} {}} +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test join-24.1 { + CREATE TABLE t1(a PRIMARY KEY, x); + CREATE TABLE t2(b INT); + CREATE INDEX t1aa ON t1(a, a); + + INSERT INTO t1 VALUES('abc', 'def'); + INSERT INTO t2 VALUES(1); +} + +do_execsql_test join-24.2 { + SELECT * FROM t2 JOIN t1 WHERE a='abc' AND x='def'; +} {1 abc def} +do_execsql_test join-24.3 { + SELECT * FROM t2 JOIN t1 WHERE a='abc' AND x='abc'; +} {} + +do_execsql_test join-24.2 { + SELECT * FROM t2 LEFT JOIN t1 ON a=0 WHERE (x='x' OR x IS NULL); +} {1 {} {}} + +# 2020-09-30 ticket 66e4b0e271c47145 +# The query flattener inserts an "expr AND expr" expression as a substitution +# for the column of a view where that view column is part of an ON expression +# of a LEFT JOIN. +# +reset_db +do_execsql_test join-25.1 { + CREATE TABLE t0(c0 INT); + CREATE VIEW v0 AS SELECT (NULL AND 5) as c0 FROM t0; + INSERT INTO t0(c0) VALUES (NULL); + SELECT count(*) FROM v0 LEFT JOIN t0 ON v0.c0; +} {1} + +# 2022-04-21 Parser issue detected by dbsqlfuzz +# +reset_db +do_catchsql_test join-26.1 { + CREATE TABLE t4(a,b); + CREATE TABLE t5(a,c); + CREATE TABLE t6(a,d); + SELECT * FROM t5 JOIN ((t4 JOIN (t5 JOIN t6)) t7); +} {/1 {.*}/} + +# 2022-06-09 Invalid subquery flattening caused by +# check-in 3f45007d544e5f78 and detected by dbsqlfuzz +# +reset_db +do_execsql_test join-27.1 { + CREATE TABLE t1(a INT,b INT,c INT); INSERT INTO t1 VALUES(NULL,NULL,NULL); + CREATE TABLE t2(d INT,e INT); INSERT INTO t2 VALUES(NULL,NULL); + CREATE INDEX x2 ON t1(c,b); + CREATE TABLE t3(x INT); INSERT INTO t3 VALUES(NULL); +} +do_execsql_test join-27.2 { + WITH t99(b) AS MATERIALIZED ( + SELECT b FROM t2 LEFT JOIN t1 ON c IN (SELECT x FROM t3) + ) + SELECT 5 FROM t2 JOIN t99 ON b IN (1,2,3); +} {} +do_execsql_test join-27.3 { + WITH t99(b) AS NOT MATERIALIZED ( + SELECT b FROM t2 LEFT JOIN t1 ON c IN (SELECT x FROM t3) + ) + SELECT 5 FROM t2 JOIN t99 ON b IN (1,2,3); +} {} +do_execsql_test join-27.4 { + WITH t99(b) AS (SELECT b FROM t2 LEFT JOIN t1 ON c IN (SELECT x FROM t3)) + SELECT 5 FROM t2 JOIN t99 ON b IN (1,2,3); +} {} +do_execsql_test join-27.5 { + SELECT 5 + FROM t2 JOIN ( + SELECT b FROM t2 LEFT JOIN t1 ON c IN (SELECT x FROM t3) + ) AS t99 ON b IN (1,2,3); +} {} + +db null NULL +do_execsql_test join-27.6 { + INSERT INTO t1 VALUES(3,4,NULL); + INSERT INTO t2 VALUES(1,2); + WITH t99(b) AS ( + SELECT coalesce(b,3) FROM t2 AS x LEFT JOIN t1 ON c IN (SELECT x FROM t3) + ) + SELECT d, e, b FROM t2 JOIN t99 ON b IN (1,2,3) ORDER BY +d; +} {NULL NULL 3 NULL NULL 3 1 2 3 1 2 3} +do_execsql_test join-27.7 { + SELECT d, e, b2 + FROM t2 + JOIN (SELECT coalesce(b,3) AS b2 FROM t2 AS x LEFT JOIN t1 + ON c IN (SELECT x FROM t3)) AS t99 + ON b2 IN (1,2,3) ORDER BY +d; +} {NULL NULL 3 NULL NULL 3 1 2 3 1 2 3} + +do_execsql_test join-27.8 { + DELETE FROM t1; + DELETE FROM t2 WHERE d IS NOT NULL; + DELETE FROM t3; + SELECT * FROM t2 JOIN (SELECT b FROM t2 LEFT JOIN t1 + ON c IN (SELECT x FROM t3)) AS t99 ON b IN (1,2,3); +} {} + +do_execsql_test join-27.9 { + DELETE FROM t1; + DELETE FROM t2; + DELETE FROM t3; + INSERT INTO t1 VALUES(4,3,5); + INSERT INTO t2 VALUES(1,2); + INSERT INTO t3 VALUES(5); + SELECT * FROM t2 JOIN (SELECT b FROM t2 LEFT JOIN t1 + ON c IN (SELECT x FROM t3)) AS t99 ON b IS NULL; +} {} +do_execsql_test join-27.10 { + WITH t99(b) AS ( + SELECT b FROM t2 AS x LEFT JOIN t1 ON c IN (SELECT x FROM t3) + ) + SELECT d, e, b FROM t2 JOIN t99 ON b IS NULL; +} {} + + +# 2022-09-19 https://sqlite.org/forum/forumpost/96b9e5709cf47cda +# Performance regression relative to version 3.38.0 that resulted from +# a new query flattener restriction that was added to fixes the join-27.* +# tests above. The restriction needed to be removed and the join-27.* +# problem fixed another way. +# +reset_db +do_execsql_test join-28.1 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b INT, c INT); + CREATE TABLE t2(d INTEGER PRIMARY KEY, e INT); + CREATE VIEW t3(a,b,c,d,e) AS SELECT * FROM t1 LEFT JOIN t2 ON d=c; + CREATE TABLE t4(x INT, y INT); + INSERT INTO t1 VALUES(1,2,3); + INSERT INTO t2 VALUES(1,5); + INSERT INTO t4 VALUES(1,4); + SELECT a, b, y FROM t4 JOIN t3 ON a=x; +} {1 2 4} +do_eqp_test join-28.2 { + SELECT a, b, y FROM t4 JOIN t3 ON a=x; +} { + QUERY PLAN + |--SCAN t4 + `--SEARCH t1 USING INTEGER PRIMARY KEY (rowid=?) +} +# ^^^^^^^ Without the fix (if the query flattening optimization does not +# run) the query plan above would look like this: +# +# QUERY PLAN +# |--MATERIALIZE t3 +# | |--SCAN t1 +# | `--SEARCH t2 USING INTEGER PRIMARY KEY (rowid=?) LEFT-JOIN +# |--SCAN t4 +# `--SEARCH t3 USING AUTOMATIC COVERING INDEX (a=?) + + +# 2023-05-01 https://sqlite.org/forum/forumpost/96cd4a7e9e +# +reset_db +db null NULL +do_execsql_test join-29.1 { + CREATE TABLE t0(a INT); INSERT INTO t0(a) VALUES (1); + CREATE TABLE t1(b INT); INSERT INTO t1(b) VALUES (2); + CREATE VIEW v2(c) AS SELECT 3 FROM t1; + SELECT * FROM t1 JOIN v2 ON 0 FULL OUTER JOIN t0 ON true; +} {NULL NULL 1} +do_execsql_test join-29.2 { + SELECT * FROM t1 JOIN v2 ON 1=0 FULL OUTER JOIN t0 ON true; +} {NULL NULL 1} +do_execsql_test join-29.3 { + SELECT * FROM t1 JOIN v2 ON false FULL OUTER JOIN t0 ON true; +} {NULL NULL 1} + +# 2023-05-11 https://sqlite.org/forum/forumpost/49f2c7f690 +# Verify that omit-noop-join optimization does not apply if the table +# to be omitted has an inner-join constraint and there is a RIGHT JOIN +# anywhere in the query. +# +reset_db +db null NULL +do_execsql_test join-30.1 { + CREATE TABLE t0(z INT); INSERT INTO t0 VALUES(1),(2); + CREATE TABLE t1(a INT); INSERT INTO t1 VALUES(1); + CREATE TABLE t2(b INT); INSERT INTO t2 VALUES(2); + CREATE TABLE t3(c INT, d INT); INSERT INTO t3 VALUES(3,4); + CREATE TABLE t4(e INT); INSERT INTO t4 VALUES(5); + CREATE VIEW v5(x,y) AS SELECT c, d FROM t3 LEFT JOIN t4 ON false; +} +do_execsql_test join-30.2 { + SELECT DISTINCT a, b + FROM t1 RIGHT JOIN t2 ON a=b LEFT JOIN v5 ON false + WHERE x <= y; +} {} +do_execsql_test join-30.3 { + SELECT DISTINCT a, b + FROM t0 JOIN t1 ON z=a RIGHT JOIN t2 ON a=b LEFT JOIN v5 ON false + WHERE x <= y; +} {} + +# 2025-05-30 https://sqlite.org/forum/forumpost/4fc70203b61c7e12 +# +# When converting a USING(x) or NATURAL into the constraint expression +# t1.x==t2.x, mark the t1.x term as EP_CanBeNull if it is the left table +# of a RIGHT JOIN. +# +reset_db +db null NULL +do_execsql_test join-31.1 { + CREATE TABLE t1(c0 INT , c1 INT); INSERT INTO t1(c0, c1) VALUES(NULL,11); + CREATE TABLE t2(c0 INT NOT NULL); + CREATE TABLE t2n(c0 INT); + CREATE TABLE t3(x INT); INSERT INTO t3(x) VALUES(3); + CREATE TABLE t4(y INT); INSERT INTO t4(y) VALUES(4); + CREATE TABLE t5(c0 INT, x INT); INSERT INTO t5 VALUES(NULL, 5); +} +do_execsql_test join-31.2 { + SELECT * FROM t2 RIGHT JOIN t3 ON true LEFT JOIN t1 USING(c0); +} {NULL 3 NULL} +do_execsql_test join-31.3 { + SELECT * FROM t2 RIGHT JOIN t3 ON true NATURAL LEFT JOIN t1; +} {NULL 3 NULL} +do_execsql_test join-31.4 { + SELECT * FROM t2n RIGHT JOIN t3 ON true LEFT JOIN t1 USING(c0); +} {NULL 3 NULL} +do_execsql_test join-31.5 { + SELECT * FROM t5 LEFT JOIN t1 USING(c0); +} {NULL 5 NULL} +do_execsql_test join-31.6 { + SELECT * FROM t3 LEFT JOIN t2 ON true LEFT JOIN t1 USING(c0); +} {3 NULL NULL} +do_execsql_test join-31.7 { + SELECT * FROM t3 LEFT JOIN t2 ON true NATURAL LEFT JOIN t1; +} {3 NULL NULL} +do_execsql_test join-31.8 { + SELECT * FROM t3 LEFT JOIN t2 ON true JOIN t4 ON true NATURAL LEFT JOIN t1; +} {3 NULL 4 NULL} + +# 2025-06-16 https://sqlite.org/forum/forumpost/68f29a2005 +# +# The transitive-constraint optimization was not working for RIGHT JOIN. +# +reset_db +db null NULL +do_execsql_test join-32.1 { + CREATE TABLE t0(w INT); + CREATE TABLE t1(x INT); + CREATE TABLE t2(y INT UNIQUE); + CREATE VIEW v0(z) AS SELECT CAST(x AS INT) FROM t1 LEFT JOIN t2 ON true; + INSERT INTO t1(x) VALUES(123); + INSERT INTO t2(y) VALUES(NULL); +} +do_execsql_test join-32.2 { + SELECT * + FROM t0 JOIN v0 ON w=z + RIGHT JOIN t1 ON true + INNER JOIN t2 ON y IS z; +} {NULL NULL 123 NULL} +do_execsql_test join-32.3 { + SELECT * + FROM t0 JOIN v0 ON w=z + RIGHT JOIN t1 ON true + INNER JOIN t2 ON +y IS z; +} {NULL NULL 123 NULL} finish_test diff --git a/test/join2.test b/test/join2.test index 0f558c5a3d..6f2fe1d770 100644 --- a/test/join2.test +++ b/test/join2.test @@ -12,10 +12,10 @@ # # This file implements tests for joins, including outer joins. # -# $Id: join2.test,v 1.2 2005/01/21 03:12:16 danielk1977 Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl +set testprefix join2 do_test join2-1.1 { execsql { @@ -63,6 +63,12 @@ do_test join2-1.6 { t1 NATURAL LEFT OUTER JOIN t2 NATURAL JOIN t3 } } {1 11 111 1111} +do_test join2-1.6-rj { + execsql { + SELECT * FROM + t2 NATURAL RIGHT OUTER JOIN t1 NATURAL JOIN t3 + } +} {11 111 1 1111} ifcapable subquery { do_test join2-1.7 { execsql { @@ -70,6 +76,377 @@ ifcapable subquery { t1 NATURAL LEFT OUTER JOIN (t2 NATURAL JOIN t3) } } {1 11 111 1111 2 22 {} {} 3 33 {} {}} + do_test join2-1.7-rj { + execsql { + SELECT a, b, c, d FROM + t2 NATURAL JOIN t3 NATURAL RIGHT JOIN t1 + } + } {1 11 111 1111 2 22 {} {} 3 33 {} {}} +} + +#------------------------------------------------------------------------- +# Check that ticket [25e335f802ddc] has been resolved. It should be an +# error for the ON clause of a LEFT JOIN to refer to a table to its right. +# +do_execsql_test 2.0 { + CREATE TABLE aa(a); + CREATE TABLE bb(b); + CREATE TABLE cc(c); + INSERT INTO aa VALUES('one'); + INSERT INTO bb VALUES('one'); + INSERT INTO cc VALUES('one'); +} + +do_catchsql_test 2.1 { + SELECT * FROM aa LEFT JOIN cc ON (a=b) JOIN bb ON (b=coalesce(c,1)); +} {1 {ON clause references tables to its right}} +do_catchsql_test 2.1b { + SELECT * FROM aa RIGHT JOIN cc ON (a=b) JOIN bb ON (b=coalesce(c,1)); +} {1 {ON clause references tables to its right}} +do_catchsql_test 2.2 { + SELECT * FROM aa JOIN cc ON (a=b) JOIN bb ON (b=c); +} {0 {one one one}} + +#------------------------------------------------------------------------- +# Test that a problem causing where.c to overlook opportunities to +# omit unnecessary tables from a LEFT JOIN when UNIQUE, NOT NULL column +# that makes this possible happens to be the leftmost in its table. +# +reset_db +do_execsql_test 3.0 { + CREATE TABLE t1(k1 INTEGER PRIMARY KEY, k2, k3); + CREATE TABLE t2(k2 INTEGER PRIMARY KEY, v2); + + -- Prior to this problem being fixed, table t3_2 would be omitted from + -- the join queries below, but if t3_1 were used in its place it would + -- not. + CREATE TABLE t3_1(k3 PRIMARY KEY, v3) WITHOUT ROWID; + CREATE TABLE t3_2(v3, k3 PRIMARY KEY) WITHOUT ROWID; +} + +do_eqp_test 3.1 { + SELECT v2 FROM t1 LEFT JOIN t2 USING (k2) LEFT JOIN t3_1 USING (k3); +} { + QUERY PLAN + |--SCAN t1 + `--SEARCH t2 USING INTEGER PRIMARY KEY (rowid=?) LEFT-JOIN +} + +do_eqp_test 3.2 { + SELECT v2 FROM t1 LEFT JOIN t2 USING (k2) LEFT JOIN t3_2 USING (k3); +} { + QUERY PLAN + |--SCAN t1 + `--SEARCH t2 USING INTEGER PRIMARY KEY (rowid=?) LEFT-JOIN +} + +#------------------------------------------------------------------------- +# Test that tables other than the rightmost can be omitted from a +# LEFT JOIN query. +# +do_execsql_test 4.0 { + CREATE TABLE c1(k INTEGER PRIMARY KEY, v1); + CREATE TABLE c2(k INTEGER PRIMARY KEY, v2); + CREATE TABLE c3(k INTEGER PRIMARY KEY, v3); + + INSERT INTO c1 VALUES(1, 2); + INSERT INTO c2 VALUES(2, 3); + INSERT INTO c3 VALUES(3, 'v3'); + + INSERT INTO c1 VALUES(111, 1112); + INSERT INTO c2 VALUES(112, 1113); + INSERT INTO c3 VALUES(113, 'v1113'); +} +do_execsql_test 4.1.1 { + SELECT v1, v3 FROM c1 LEFT JOIN c2 ON (c2.k=v1) LEFT JOIN c3 ON (c3.k=v2); +} {2 v3 1112 {}} +do_execsql_test 4.1.2 { + SELECT v1, v3 FROM c1 LEFT JOIN c2 ON (c2.k=v1) LEFT JOIN c3 ON (c3.k=v1+1); +} {2 v3 1112 {}} + +do_execsql_test 4.1.3 { + SELECT DISTINCT v1, v3 FROM c1 LEFT JOIN c2 LEFT JOIN c3 ON (c3.k=v1+1); +} {2 v3 1112 {}} + +do_execsql_test 4.1.4 { + SELECT v1, v3 FROM c1 LEFT JOIN c2 LEFT JOIN c3 ON (c3.k=v1+1); +} {2 v3 2 v3 1112 {} 1112 {}} + +do_eqp_test 4.1.5 { + SELECT v1, v3 FROM c1 LEFT JOIN c2 ON (c2.k=v1) LEFT JOIN c3 ON (c3.k=v2); +} { + QUERY PLAN + |--SCAN c1 + |--SEARCH c2 USING INTEGER PRIMARY KEY (rowid=?) LEFT-JOIN + `--SEARCH c3 USING INTEGER PRIMARY KEY (rowid=?) LEFT-JOIN +} +do_eqp_test 4.1.6 { + SELECT v1, v3 FROM c1 LEFT JOIN c2 ON (c2.k=v1) LEFT JOIN c3 ON (c3.k=v1+1); +} { + QUERY PLAN + |--SCAN c1 + `--SEARCH c3 USING INTEGER PRIMARY KEY (rowid=?) LEFT-JOIN +} + +do_execsql_test 4.2.0 { + DROP TABLE c1; + DROP TABLE c2; + DROP TABLE c3; + CREATE TABLE c1(k UNIQUE, v1); + CREATE TABLE c2(k UNIQUE, v2); + CREATE TABLE c3(k UNIQUE, v3); + + INSERT INTO c1 VALUES(1, 2); + INSERT INTO c2 VALUES(2, 3); + INSERT INTO c3 VALUES(3, 'v3'); + + INSERT INTO c1 VALUES(111, 1112); + INSERT INTO c2 VALUES(112, 1113); + INSERT INTO c3 VALUES(113, 'v1113'); +} +do_execsql_test 4.2.1 { + SELECT v1, v3 FROM c1 LEFT JOIN c2 ON (c2.k=v1) LEFT JOIN c3 ON (c3.k=v2); +} {2 v3 1112 {}} +do_execsql_test 4.2.2 { + SELECT v1, v3 FROM c1 LEFT JOIN c2 ON (c2.k=v1) LEFT JOIN c3 ON (c3.k=v1+1); +} {2 v3 1112 {}} + +do_execsql_test 4.2.3 { + SELECT DISTINCT v1, v3 FROM c1 LEFT JOIN c2 LEFT JOIN c3 ON (c3.k=v1+1); +} {2 v3 1112 {}} + +do_execsql_test 4.2.4 { + SELECT v1, v3 FROM c1 LEFT JOIN c2 LEFT JOIN c3 ON (c3.k=v1+1); +} {2 v3 2 v3 1112 {} 1112 {}} + +do_eqp_test 4.2.5 { + SELECT v1, v3 FROM c1 LEFT JOIN c2 ON (c2.k=v1) LEFT JOIN c3 ON (c3.k=v2); +} { + QUERY PLAN + |--SCAN c1 + |--SEARCH c2 USING INDEX sqlite_autoindex_c2_1 (k=?) LEFT-JOIN + `--SEARCH c3 USING INDEX sqlite_autoindex_c3_1 (k=?) LEFT-JOIN +} +do_eqp_test 4.2.6 { + SELECT v1, v3 FROM c1 LEFT JOIN c2 ON (c2.k=v1) LEFT JOIN c3 ON (c3.k=v1+1); +} { + QUERY PLAN + |--SCAN c1 + `--SEARCH c3 USING INDEX sqlite_autoindex_c3_1 (k=?) LEFT-JOIN +} + +# 2017-11-23 (Thanksgiving day) +# OSSFuzz found an assertion fault in the new LEFT JOIN eliminator code. +# +do_execsql_test 4.3.0 { + DROP TABLE IF EXISTS t1; + DROP TABLE IF EXISTS t2; + CREATE TABLE t1(x PRIMARY KEY) WITHOUT ROWID; + CREATE TABLE t2(x); + SELECT a.x + FROM t1 AS a + LEFT JOIN t1 AS b ON (a.x=b.x) + LEFT JOIN t2 AS c ON (a.x=c.x); +} {} +do_execsql_test 4.3.1 { + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<10) + INSERT INTO t1(x) SELECT x FROM c; + INSERT INTO t2(x) SELECT x+9 FROM t1; + SELECT a.x, c.x + FROM t1 AS a + LEFT JOIN t1 AS b ON (a.x=b.x) + LEFT JOIN t2 AS c ON (a.x=c.x); +} {1 {} 2 {} 3 {} 4 {} 5 {} 6 {} 7 {} 8 {} 9 {} 10 10} + +do_execsql_test 5.0 { + CREATE TABLE s1 (a INTEGER PRIMARY KEY); + CREATE TABLE s2 (a INTEGER PRIMARY KEY); + CREATE TABLE s3 (a INTEGER); + CREATE UNIQUE INDEX ndx on s3(a); +} +do_eqp_test 5.1 { + SELECT s1.a FROM s1 left join s2 using (a); +} {SCAN s1} + +do_eqp_test 5.2 { + SELECT s1.a FROM s1 left join s3 using (a); +} {SCAN s1} + +do_execsql_test 6.0 { + CREATE TABLE u1(a INTEGER PRIMARY KEY, b, c); + CREATE TABLE u2(a INTEGER PRIMARY KEY, b, c); + CREATE INDEX u1ab ON u1(b, c); +} +do_eqp_test 6.1 { + SELECT u2.* FROM u2 LEFT JOIN u1 ON( u1.a=u2.a AND u1.b=u2.b AND u1.c=u2.c ); +} {SCAN u2} + +db close +sqlite3 db :memory: +do_execsql_test 7.0 { + CREATE TABLE t1(a,b); INSERT INTO t1 VALUES(1,2),(3,4),(5,6); + CREATE TABLE t2(c,d); INSERT INTO t2 VALUES(2,4),(3,6); + CREATE TABLE t3(x); INSERT INTO t3 VALUES(9); + CREATE VIEW test AS + SELECT *, 'x' + FROM t1 LEFT JOIN (SELECT * FROM t2, t3) ON (c=b AND x=9) + WHERE c IS NULL; + SELECT * FROM test; +} {3 4 {} {} {} x 5 6 {} {} {} x} + +#------------------------------------------------------------------------- +# Ticket [dfd66334]. +# +reset_db +do_execsql_test 8.0 { + CREATE TABLE t0(c0); + CREATE TABLE t1(c0); +} + +do_execsql_test 8.1 { + SELECT * FROM t0 LEFT JOIN t1 + WHERE (t1.c0 BETWEEN 0 AND 0) > ('' AND t0.c0); +} + +#------------------------------------------------------------------------- +# Ticket [45f4bf4eb] reported by Manuel Rigger (2020-04-25) +# +# Follow up error reported by Eric Speckman on the SQLite forum +# https://sqlite.org/forum/info/c49496d24d35bd7c (2020-08-19) +# +reset_db +do_execsql_test 9.0 { + CREATE TABLE t0(c0 INT); + CREATE VIEW v0(c0) AS SELECT CAST(t0.c0 AS INTEGER) FROM t0; + INSERT INTO t0(c0) VALUES (0); +} + +do_execsql_test 9.1 { + SELECT typeof(c0), c0 FROM v0 WHERE c0>='0' +} {integer 0} + +do_execsql_test 9.2 { + SELECT * FROM t0, v0 WHERE v0.c0 >= '0'; +} {0 0} + +do_execsql_test 9.3 { + SELECT * FROM t0 LEFT JOIN v0 WHERE v0.c0 >= '0'; +} {0 0} + +do_execsql_test 9.4 { + SELECT * FROM t0 LEFT JOIN v0 ON v0.c0 >= '0'; +} {0 0} + +do_execsql_test 9.5 { + SELECT * FROM t0 LEFT JOIN v0 ON v0.c0 >= '0' WHERE TRUE + UNION SELECT 0,0 WHERE 0; +} {0 0} + +do_execsql_test 9.10 { + CREATE TABLE t1 (aaa); + INSERT INTO t1 VALUES(23456); + CREATE TABLE t2(bbb); + CREATE VIEW v2(ccc) AS SELECT bbb IS 1234 FROM t2; + SELECT ccc, ccc IS NULL AS ddd FROM t1 LEFT JOIN v2; +} {{} 1} +optimization_control db query-flattener 0 +do_execsql_test 9.11 { + SELECT ccc, ccc IS NULL AS ddd FROM t1 LEFT JOIN v2; +} {{} 1} + +# 2023-03-01 https://sqlite.org/forum/forumpost/26387ea7ef +# When flattening a VIEW which is the RHS of a LEFT JOIN, always put +# an TK_IF_NULL_ROW operator on all accesses, even TK_COLUMN nodes, since +# the TK_COLUMN might reference an outer subquery. +# +reset_db +db null NULL +do_execsql_test 10.1 { + CREATE TABLE t1 (x INTEGER); + INSERT INTO t1 VALUES(1); -- Some true value + CREATE TABLE t2 (z TEXT); + INSERT INTO t2 VALUES('some value'); + CREATE TABLE t3(w TEXT); + INSERT INTO t3 VALUES('some other value'); +} +do_execsql_test 10.2 { + SELECT ( + SELECT 1 FROM t2 LEFT JOIN (SELECT x AS v FROM t3) ON 500=v WHERE (v OR FALSE) + ) FROM t1; +} NULL +do_execsql_test 10.3 { + SELECT ( + SELECT 1 FROM t2 LEFT JOIN (SELECT x AS v FROM t3) ON 500=v WHERE (v) + ) FROM t1; +} NULL +optimization_control db all 0 +do_execsql_test 10.4 { + SELECT ( + SELECT 1 FROM t2 LEFT JOIN (SELECT x AS v FROM t3) ON 500=v WHERE (v OR FALSE) + ) FROM t1; +} NULL + +# 2023-03-02 https://sqlite.org/forum/forumpost/402f05296d +# +# The TK_IF_NULL_ROW expression node must ensure that it does not overwrite +# the result register of an OP_Once subroutine. +# +optimization_control db all 1 +do_execsql_test 11.1 { + DROP TABLE t1; + DROP TABLE t2; + DROP TABLE t3; + CREATE TABLE t1(x TEXT, y INTEGER); + INSERT INTO t1(x,y) VALUES(NULL,-2),(NULL,1),('0',2); + CREATE TABLE t2(z INTEGER); + INSERT INTO t2(z) VALUES(2),(-2); + CREATE VIEW t3 AS SELECT z, (SELECT count(*) FROM t1) AS w FROM t2; + SELECT * FROM t1 LEFT JOIN t3 ON y=z; +} {NULL -2 -2 3 NULL 1 NULL NULL 0 2 2 3} + +# 2023-03-11 https://sqlite.org/forum/forumpost/b405033490fa56d9 +# The fix that test 11.1 above checks also caused a performance regression. +# This test case verifies that the performance regression has been resolved. +# +do_execsql_test 12.1 { + DROP TABLE t1; + DROP TABLE t2; + DROP VIEW t3; + CREATE TABLE t1(a INTEGER PRIMARY KEY); + WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<100) + INSERT INTO t1(a) SELECT n FROM c; + CREATE VIEW t2(b) AS SELECT a FROM t1; +} +do_vmstep_test 12.2 { + SELECT * FROM t1 LEFT JOIN t2 ON a=b LIMIT 10 OFFSET 98; +} 2000 {99 99 100 100} +do_eqp_test 12.3 { + SELECT * FROM t1 LEFT JOIN t2 ON a=b LIMIT 10 OFFSET 98; +} { + QUERY PLAN + |--SCAN t1 + `--SEARCH t1 USING INTEGER PRIMARY KEY (rowid=?) LEFT-JOIN +} + +# 2024-09-05 https://sqlite.org/forum/forumpost/8a1e467e905b8d27 +# When performing the Omit-Noop-Join optimization, if FROM clause terms +# to the right of the omitted join have the reverse-order bit set in the +# WhereInfo.revMask bitmask, those bits need to be shifted to account +# for the omitted join. +# +reset_db +do_execsql_test 13.0 { + CREATE TABLE t1(a1 INTEGER PRIMARY KEY, b1 INT); + CREATE TABLE t2(c2 INT, d2 INTEGER PRIMARY KEY); + CREATE TABLE t3(e3 INTEGER PRIMARY KEY); + INSERT INTO t1 VALUES(33,0); + INSERT INTO t2 VALUES(33,1),(33,2); } +do_execsql_test 13.1 { + SELECT t1.a1, t2.d2 + FROM (t1 LEFT JOIN t3 ON t3.e3=t1.b1) JOIN t2 ON t2.c2=t1.a1 + WHERE t1.a1=33 + ORDER BY t2.d2 DESC; +} {33 2 33 1} finish_test diff --git a/test/join5.test b/test/join5.test index 543cd4d27f..eb8ba3c7b5 100644 --- a/test/join5.test +++ b/test/join5.test @@ -13,10 +13,10 @@ # This file implements tests for left outer joins containing ON # clauses that restrict the scope of the left term of the join. # -# $Id: join5.test,v 1.2 2007/06/08 00:20:48 drh Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl +set testprefix join5 do_test join5-1.1 { @@ -106,7 +106,7 @@ do_test join5-2.12 { execsql {SELECT * FROM xy LEFT JOIN ab ON NULL WHERE NULL} } {} -# Ticket https://www.sqlite.org/src/tktview/6f2222d550f5b0ee7ed37601 +# Ticket https://sqlite.org/src/tktview/6f2222d550f5b0ee7ed37601 # Incorrect output on a LEFT JOIN. # do_execsql_test join5-3.1 { @@ -161,10 +161,10 @@ do_execsql_test join5-3.3 { SELECT * FROM x1 LEFT JOIN x2 JOIN x3 WHERE x3.d = x2.b; } {} -# Ticket https://www.sqlite.org/src/tktview/c2a19d81652f40568c770c43 on +# Ticket https://sqlite.org/src/tktview/c2a19d81652f40568c770c43 on # 2015-08-20. LEFT JOIN and the push-down optimization. # -do_execsql_test join6-4.1 { +do_execsql_test join5-4.1 { SELECT * FROM ( SELECT 'apple' fruit @@ -178,10 +178,281 @@ do_execsql_test join6-4.1 { SELECT 1 isyellow ) c ON b.fruit='banana'; } {apple apple {} banana banana 1} -do_execsql_test join6-4.2 { +do_execsql_test join5-4.2 { SELECT * FROM (SELECT 'apple' fruit UNION ALL SELECT 'banana') LEFT JOIN (SELECT 1) ON fruit='banana'; } {apple {} banana 1} +#------------------------------------------------------------------------- +do_execsql_test 5.0 { + CREATE TABLE y1(x, y, z); + INSERT INTO y1 VALUES(0, 0, 1); + CREATE TABLE y2(a); +} + +do_execsql_test 5.1 { + SELECT count(z) FROM y1 LEFT JOIN y2 ON x GROUP BY y; +} 1 + +do_execsql_test 5.2 { + SELECT count(z) FROM ( SELECT * FROM y1 ) LEFT JOIN y2 ON x GROUP BY y; +} 1 + +do_execsql_test 5.3 { + CREATE VIEW v1 AS SELECT x, y, z FROM y1; + SELECT count(z) FROM v1 LEFT JOIN y2 ON x GROUP BY y; +} 1 + +do_execsql_test 5.4 { + SELECT count(z) FROM ( SELECT * FROM y1 ) LEFT JOIN y2 ON x +} 1 + +do_execsql_test 5.5 { + SELECT * FROM ( SELECT * FROM y1 ) LEFT JOIN y2 ON x +} {0 0 1 {}} + +#------------------------------------------------------------------------- +# +reset_db +do_execsql_test 6.1 { + CREATE TABLE t1(x); + INSERT INTO t1 VALUES(1); + + CREATE TABLE t2(y INTEGER PRIMARY KEY,a,b); + INSERT INTO t2 VALUES(1,2,3); + CREATE INDEX t2a ON t2(a); + CREATE INDEX t2b ON t2(b); +} + +do_execsql_test 6.2 { + SELECT * FROM t1 LEFT JOIN t2 ON a=2 OR b=3 WHERE y IS NULL; +} {} + +do_execsql_test 6.3.1 { + CREATE TABLE t3(x); + INSERT INTO t3 VALUES(1); + CREATE TABLE t4(y, z); + SELECT ifnull(z, '!!!') FROM t3 LEFT JOIN t4 ON (x=y); +} {!!!} + +do_execsql_test 6.3.2 { + CREATE INDEX t4i ON t4(y, ifnull(z, '!!!')); + SELECT ifnull(z, '!!!') FROM t3 LEFT JOIN t4 ON (x=y); +} {!!!} + +# 2019-02-08 https://sqlite.org/src/info/4e8e4857d32d401f +reset_db +do_execsql_test 6.100 { + CREATE TABLE t1(aa, bb); + CREATE INDEX t1x1 on t1(abs(aa), abs(bb)); + INSERT INTO t1 VALUES(-2,-3),(+2,-3),(-2,+3),(+2,+3); + SELECT * FROM (t1) + WHERE ((abs(aa)=1 AND 1=2) OR abs(aa)=2) + AND abs(bb)=3 + ORDER BY +1, +2; +} {-2 -3 -2 3 2 -3 2 3} + +#------------------------------------------------------------------------- +# +reset_db +do_execsql_test 7.0 { + CREATE TABLE t1(x); + INSERT INTO t1 VALUES(1); +} + +do_execsql_test 7.1 { + CREATE TABLE t2(x, y, z); + CREATE INDEX t2xy ON t2(x, y); + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<50000 + ) + INSERT INTO t2 SELECT i/10, i, NULL FROM s; + ANALYZE; +} + +do_eqp_test 7.2 { + SELECT * FROM t1 LEFT JOIN t2 ON ( + t2.x = t1.x AND (t2.y=? OR (t2.y=? AND t2.z IS NOT NULL)) + ); +} { + QUERY PLAN + |--SCAN t1 + `--MULTI-INDEX OR + |--INDEX 1 + | `--SEARCH t2 USING INDEX t2xy (x=? AND y=?) LEFT-JOIN + `--INDEX 2 + `--SEARCH t2 USING INDEX t2xy (x=? AND y=?) LEFT-JOIN +} + +do_execsql_test 7.3 { + CREATE TABLE t3(x); + INSERT INTO t3(x) VALUES(1); + CREATE INDEX t3x ON t3(x); + + CREATE TABLE t4(x, y, z); + CREATE INDEX t4xy ON t4(x, y); + CREATE INDEX t4xz ON t4(x, z); + + WITH s(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<50000) + INSERT INTO t4 SELECT i/10, i, i FROM s; + + ANALYZE; + UPDATE sqlite_stat1 SET stat='1000000 10 1' WHERE idx='t3x'; + ANALYZE sqlite_schema; +} + +# If both sides of the OR reference the right-hand side of the LEFT JOIN +# then simplify the LEFT JOIN. +# +do_eqp_test 7.4 { + SELECT * FROM t3 LEFT JOIN t4 ON (t4.x = t3.x) WHERE (t4.y = ? OR t4.z = ?); +} { + QUERY PLAN + |--SCAN t4 + `--SEARCH t3 USING COVERING INDEX t3x (x=?) +} +# If only one side of the OR references the right-hand side of the LEFT JOIN +# then do not do the simplification +# +do_eqp_test 7.4b { + SELECT * FROM t3 LEFT JOIN t4 ON (t4.x = t3.x) WHERE (t4.y = ? OR t3.x = ?); +} { + QUERY PLAN + |--SCAN t3 + `--SEARCH t4 USING INDEX t4xz (x=?) LEFT-JOIN +} +do_eqp_test 7.4c { + SELECT * FROM t3 LEFT JOIN t4 ON (t4.x = t3.x) WHERE (t3.x = ? OR t4.z = ?); +} { + QUERY PLAN + |--SCAN t3 + `--SEARCH t4 USING INDEX t4xz (x=?) LEFT-JOIN +} +do_eqp_test 7.4d { + SELECT * FROM t3 CROSS JOIN t4 ON (t4.x = t3.x) WHERE (+t4.y = ? OR t4.z = ?); +} { + QUERY PLAN + |--SCAN t3 + |--BLOOM FILTER ON t4 (x=?) + `--SEARCH t4 USING INDEX t4xz (x=?) +} + +reset_db +do_execsql_test 8.0 { + CREATE TABLE t0 (c0, c1, PRIMARY KEY (c0, c1)); + CREATE TABLE t1 (c0); + + INSERT INTO t1 VALUES (2); + + INSERT INTO t0 VALUES(0, 10); + INSERT INTO t0 VALUES(1, 10); + INSERT INTO t0 VALUES(2, 10); + INSERT INTO t0 VALUES(3, 10); +} + +do_execsql_test 8.1 { + SELECT * FROM t0, t1 + WHERE (t0.c1 >= 1 OR t0.c1 < 1) AND t0.c0 IN (1, t1.c0) ORDER BY 1; +} { + 1 10 2 + 2 10 2 +} + + +# 2022-01-31 dbsqlfuzz 787d9bd73164c6f0c85469e2e48b2aff19af6938 +# +reset_db +do_execsql_test 9.1 { + CREATE TABLE t1(a ,b FLOAT); + INSERT INTO t1 VALUES(1,1); + CREATE INDEX t1x1 ON t1(a,b,a,a,a,a,a,a,a,a,a,b); + ANALYZE sqlite_schema; + INSERT INTO sqlite_stat1 VALUES('t1','t1x1','648 324 81 81 81 81 81 81 81081 81 81 81'); + ANALYZE sqlite_schema; +} +do_catchsql_test 9.2 { + SELECT a FROM + (SELECT a FROM t1 NATURAL LEFT JOIN t1) NATURAL LEFT JOIN t1 + WHERE (rowid,1)<=(5,0); +} {0 1} + +# 2022-03-02 https://sqlite.org/forum/info/50a1bbe08ce4c29c +# Bloom-filter pulldown is incompatible with skip-scan. +# +reset_db +do_execsql_test 10.1 { + CREATE TABLE t1(x INT); + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<20) + INSERT INTO t1(x) SELECT 0 FROM c; + CREATE INDEX t1x1 ON t1(x BETWEEN 0 AND 10, x); + ANALYZE; + DELETE FROM t1; + INSERT INTO t1 VALUES(0),(0); + CREATE VIEW v1 AS SELECT * FROM t1 NATURAL JOIN t1 WHERE (x BETWEEN 0 AND 10) OR true; + CREATE VIEW v2 AS SELECT * FROM v1 NATURAL JOIN v1; + CREATE VIEW v3 AS SELECT * FROM v2, v1 USING (x) GROUP BY x; + SELECT x FROM v3; +} {0} + +# 2022-03-24 https://sqlite.org/forum/forumpost/031e262a89b6a9d2 +# Bloom-filter on a LEFT JOIN with NULL-based WHERE constraints. +# +reset_db +do_execsql_test 11.1 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b INT); + CREATE TABLE t2(c INTEGER PRIMARY KEY, d INT); + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<8) + INSERT INTO t1(a,b) SELECT x, 10*x FROM c; + INSERT INTO t2(c,d) SELECT b*2, 100*a FROM t1; + ANALYZE; + DELETE FROM sqlite_stat1; + INSERT INTO sqlite_stat1(tbl,idx,stat) VALUES + ('t1',NULL,150105),('t2',NULL,98747); + ANALYZE sqlite_schema; +} {} +do_execsql_test 11.2 { + SELECT count(*) FROM t1 LEFT JOIN t2 ON c=b WHERE d IS NULL; +} {4} +do_execsql_test 11.3 { + SELECT count(*) FROM t1 LEFT JOIN t2 ON c=b WHERE d=100; +} {1} +do_execsql_test 11.4 { + SELECT count(*) FROM t1 LEFT JOIN t2 ON c=b WHERE d>=300; +} {2} + +# 2022-05-03 https://sqlite.org/forum/forumpost/2482b32700384a0f +# Bloom-filter pull-down does not handle NOT NULL constraints correctly. +# +reset_db +do_execsql_test 12.1 { + CREATE TABLE t1(a INT, b INT, c INT); + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<100) + INSERT INTO t1(a,b,c) SELECT x, x*1000, x*1000000 FROM c; + CREATE TABLE t2(b INT, x INT); + INSERT INTO t2(b,x) SELECT b, a FROM t1 WHERE a%3==0; + CREATE INDEX t2b ON t2(b); + CREATE TABLE t3(c INT, y INT); + INSERT INTO t3(c,y) SELECT c, a FROM t1 WHERE a%4==0; + CREATE INDEX t3c ON t3(c); + INSERT INTO t1(a,b,c) VALUES(200, 200000, NULL); + ANALYZE; +} {} +do_execsql_test 12.2 { + SELECT * FROM t1 NATURAL JOIN t2 NATURAL JOIN t3 WHERE x>0 AND y>0 + ORDER BY +a; +} { + 12 12000 12000000 12 12 + 24 24000 24000000 24 24 + 36 36000 36000000 36 36 + 48 48000 48000000 48 48 + 60 60000 60000000 60 60 + 72 72000 72000000 72 72 + 84 84000 84000000 84 84 + 96 96000 96000000 96 96 +} + + + + finish_test diff --git a/test/join6.test b/test/join6.test index 7fbf508e57..802f1b3745 100644 --- a/test/join6.test +++ b/test/join6.test @@ -147,6 +147,22 @@ ifcapable compound { } {1 91 92 3 93 5} } +do_execsql_test join6-5.1 { + CREATE TABLE tx(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o PRIMARY KEY) + WITHOUT ROWID; + INSERT INTO tx VALUES( + 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 + ); +} {} +do_execsql_test joint6-5.2 { + SELECT o FROM tx NATURAL JOIN tx; +} {15} + +do_execsql_test join6-5.3 { + CREATE TABLE ty(a,Ñ,x6,x7,x8,Q,I,v,x1,L,E,x2,x3,x4,x5,s,g PRIMARY KEY,b,c) + WITHOUT ROWID; + SELECT a FROM ty NATURAL JOIN ty; +} diff --git a/test/join7.test b/test/join7.test new file mode 100644 index 0000000000..42cc357418 --- /dev/null +++ b/test/join7.test @@ -0,0 +1,355 @@ +# 2022-04-09 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. +# +# This file implements tests for RIGHT and FULL OUTER JOINs. + +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +foreach {id schema} { + 1 { + CREATE TABLE t1(a INT, b INT); + INSERT INTO t1 VALUES(1,2),(1,3),(1,4); + CREATE INDEX t1a ON t1(a); + CREATE TABLE t2(c INT, d INT); + INSERT INTO t2 VALUES(3,33),(4,44),(5,55); + CREATE INDEX t2c ON t2(c); + CREATE VIEW dual(dummy) AS VALUES('x'); + } + 2 { + CREATE TABLE t1(a INT, b INT); + INSERT INTO t1 VALUES(1,2),(1,3),(1,4); + CREATE INDEX t1ab ON t1(a,b); + CREATE TABLE t2(c INT, d INT); + INSERT INTO t2 VALUES(3,33),(4,44),(5,55); + CREATE INDEX t2cd ON t2(c,d); + CREATE VIEW dual(dummy) AS VALUES('x'); + } + 3 { + CREATE TABLE t1(a INT, b INT); + INSERT INTO t1 VALUES(1,2),(1,3),(1,4); + CREATE INDEX t1a ON t1(a); + CREATE TABLE t2(c INT, d INT PRIMARY KEY) WITHOUT ROWID; + INSERT INTO t2 VALUES(3,33),(4,44),(5,55); + CREATE INDEX t2c ON t2(c); + CREATE VIEW dual(dummy) AS VALUES('x'); + } + 4 { + CREATE TABLE t1(a INT, b INT); + INSERT INTO t1 VALUES(1,2),(1,3),(1,4); + CREATE TABLE t2(c INTEGER PRIMARY KEY, d INT); + INSERT INTO t2 VALUES(3,33),(4,44),(5,55); + CREATE VIEW dual(dummy) AS VALUES('x'); + } + 5 { + CREATE TABLE t1(a INT, b INT); + INSERT INTO t1 VALUES(1,2),(1,3),(1,4); + CREATE TABLE t2(c INT PRIMARY KEY, d INT) WITHOUT ROWID; + INSERT INTO t2 VALUES(3,33),(4,44),(5,55); + CREATE VIEW dual(dummy) AS VALUES('x'); + } + 6 { + CREATE TABLE t1(a INT, b INT); + INSERT INTO t1 VALUES(1,2),(1,3),(1,4); + CREATE VIEW t2(c,d) AS VALUES(3,33),(4,44),(5,55); + CREATE VIEW dual(dummy) AS VALUES('x'); + } + 7 { + CREATE VIEW t1(a,b) AS VALUES(1,2),(1,3),(1,4); + CREATE TABLE t2(c INTEGER PRIMARY KEY, d INT); + INSERT INTO t2 VALUES(3,33),(4,44),(5,55); + CREATE VIEW dual(dummy) AS VALUES('x'); + } + 8 { + CREATE TABLE t1(a INT, b INT); + INSERT INTO t1 VALUES(1,2),(1,3),(1,4); + CREATE TABLE t2(c INT, d INT); + INSERT INTO t2 VALUES(3,33),(4,44),(5,55); + CREATE VIEW dual(dummy) AS VALUES('x'); + } + 9 { + CREATE TABLE t1(a INT, b INT); + INSERT INTO t1 VALUES(1,2),(1,3),(1,4); + CREATE TABLE t2a(c INTEGER PRIMARY KEY, i1 INT); + CREATE TABLE t2b(i1 INTEGER PRIMARY KEY, d INT); + CREATE VIEW t2(c,d) AS SELECT c, d FROM t2a NATURAL JOIN t2b; + INSERT INTO t2a VALUES(3,93),(4,94),(5,95),(6,96),(7,97); + INSERT INTO t2b VALUES(91,11),(92,22),(93,33),(94,44),(95,55); + CREATE TABLE dual(dummy TEXT); + INSERT INTO dual(dummy) VALUES('x'); + } + 10 { + CREATE TABLE t1(a INT, b INT, PRIMARY KEY(a,b)) WITHOUT ROWID; + INSERT INTO t1 VALUES(1,2),(1,3),(1,4); + CREATE TABLE t2a(c INTEGER PRIMARY KEY, i1 INT); + CREATE TABLE t2b(i1 INTEGER PRIMARY KEY, d INT); + CREATE VIEW t2(c,d) AS SELECT c, d FROM t2a NATURAL JOIN t2b; + INSERT INTO t2a VALUES(3,93),(4,94),(5,95),(6,96),(7,97); + INSERT INTO t2b VALUES(91,11),(92,22),(93,33),(94,44),(95,55); + CREATE TABLE dual(dummy TEXT); + INSERT INTO dual(dummy) VALUES('x'); + } +} { + reset_db + db nullvalue NULL + do_execsql_test join7-$id.setup $schema {} + + # Verified against PG-14 for case 1 + do_execsql_test join7-$id.10 { + SELECT b, d FROM t1 FULL OUTER JOIN t2 ON b=c ORDER BY +b; + } { + NULL 55 + 2 NULL + 3 33 + 4 44 + } + + # Verified against PG-14 for case 1 + do_execsql_test join7-$id.20 { + SELECT a, c FROM t1 FULL OUTER JOIN t2 ON b=c ORDER BY +b; + } { + NULL 5 + 1 NULL + 1 3 + 1 4 + } + + do_execsql_test join7-$id.30 { + SELECT * FROM t1 FULL OUTER JOIN t2 ON b=c ORDER BY +b; + } { + NULL NULL 5 55 + 1 2 NULL NULL + 1 3 3 33 + 1 4 4 44 + } + do_execsql_test join7-$id.31 { + SELECT t1.*, t2.* FROM t2 FULL OUTER JOIN t1 ON b=c ORDER BY +b; + } { + NULL NULL 5 55 + 1 2 NULL NULL + 1 3 3 33 + 1 4 4 44 + } + do_execsql_test join7-$id.32 { + SELECT t1.*, t2.* FROM t2 FULL OUTER JOIN t1 ON b=c + WHERE b=c + ORDER BY +b; + } { + 1 3 3 33 + 1 4 4 44 + } + do_execsql_test join7-$id.33 { + SELECT t1.*, t2.* FROM t2 FULL OUTER JOIN t1 ON b=c + WHERE b>0 + ORDER BY +b; + } { + 1 2 NULL NULL + 1 3 3 33 + 1 4 4 44 + } + do_execsql_test join7-$id.34 { + SELECT t1.*, t2.* FROM t2 FULL OUTER JOIN t1 ON b=c + WHERE b>0 OR b IS NULL + ORDER BY +b; + } { + NULL NULL 5 55 + 1 2 NULL NULL + 1 3 3 33 + 1 4 4 44 + } + do_execsql_test join7-$id.35 { + SELECT t1.*, t2.* FROM t2 FULL OUTER JOIN t1 ON b=c AND b>3 AND c>4 + ORDER BY coalesce(b,c,0); + } { + 1 2 NULL NULL + NULL NULL 3 33 + 1 3 NULL NULL + NULL NULL 4 44 + 1 4 NULL NULL + NULL NULL 5 55 + } + do_execsql_test join7-$id.36 { + SELECT t1.*, t2.* FROM t2 FULL OUTER JOIN t1 ON b=c AND b>3 WHERE c>4 + ORDER BY coalesce(b,c,0); + } { + NULL NULL 5 55 + } + do_execsql_test join7-$id.37 { + SELECT t1.*, t2.* FROM t2 FULL OUTER JOIN t1 ON b=c WHERE b>3 AND c>4 + ORDER BY coalesce(b,c,0); + } { + } + do_execsql_test join7-$id.38 { + SELECT t1.*, t2.* FROM t2 FULL OUTER JOIN t1 ON b=c WHERE b>3 OR c>4 + ORDER BY coalesce(b,c,0); + } { + 1 4 4 44 + NULL NULL 5 55 + } + do_execsql_test join7-$id.39 { + SELECT t1.*, t2.* FROM t2 FULL OUTER JOIN t1 ON b=c AND (b>3 OR c>4) + ORDER BY coalesce(b,c,0); + } { + 1 2 NULL NULL + NULL NULL 3 33 + 1 3 NULL NULL + 1 4 4 44 + NULL NULL 5 55 + } + do_execsql_test join7-$id.40 { + SELECT * FROM t1 RIGHT OUTER JOIN t2 ON b=c ORDER BY +b; + } { + NULL NULL 5 55 + 1 3 3 33 + 1 4 4 44 + } + do_execsql_test join7-$id.50 { + SELECT t1.*, t2.* FROM t2 LEFT OUTER JOIN t1 ON b=c ORDER BY +b; + } { + NULL NULL 5 55 + 1 3 3 33 + 1 4 4 44 + } + do_execsql_test join7-$id.60 { + SELECT * FROM dual JOIN t1 ON true RIGHT OUTER JOIN t2 ON b=c ORDER BY +b; + } { + NULL NULL NULL 5 55 + x 1 3 3 33 + x 1 4 4 44 + } + do_execsql_test join7-$id.70 { + SELECT t1.*, t2.* + FROM t2 LEFT JOIN (dual JOIN t1 ON true) ON b=c ORDER BY +b; + } { + NULL NULL 5 55 + 1 3 3 33 + 1 4 4 44 + } + do_execsql_test join7-$id.80 { + SELECT * FROM dual CROSS JOIN t1 RIGHT OUTER JOIN t2 ON b=c ORDER BY +b; + } { + NULL NULL NULL 5 55 + x 1 3 3 33 + x 1 4 4 44 + } + do_execsql_test join7-$id.81 { + SELECT dual.*, t1.*, t2.* + FROM t1 CROSS JOIN dual RIGHT OUTER JOIN t2 ON b=c ORDER BY +b; + } { + NULL NULL NULL 5 55 + x 1 3 3 33 + x 1 4 4 44 + } + do_execsql_test join7-$id.90 { + SELECT * FROM t1 LEFT OUTER JOIN t2 ON b=c ORDER BY +b; + } { + 1 2 NULL NULL + 1 3 3 33 + 1 4 4 44 + } + do_execsql_test join7-$id.100 { + SELECT * FROM t1 FULL OUTER JOIN t2 ON b=c AND a=1 ORDER BY +b; + } { + NULL NULL 5 55 + 1 2 NULL NULL + 1 3 3 33 + 1 4 4 44 + } + do_execsql_test join7-$id.101 { + SELECT t1.*, t2.* FROM t2 FULL OUTER JOIN t1 ON b=c AND a=1 ORDER BY +b; + } { + NULL NULL 5 55 + 1 2 NULL NULL + 1 3 3 33 + 1 4 4 44 + } + + # Verified against PG-14 for case 1 + do_execsql_test join7-$id.110 { + SELECT * FROM t1 FULL OUTER JOIN t2 ON b=c WHERE a=1 ORDER BY +b; + } { + 1 2 NULL NULL + 1 3 3 33 + 1 4 4 44 + } + + do_execsql_test join7-$id.111 { + SELECT t1.*, t2.* FROM t2 FULL OUTER JOIN t1 ON b=c WHERE a=1 ORDER BY +b; + } { + 1 2 NULL NULL + 1 3 3 33 + 1 4 4 44 + } + + # Verified against PG-14 for case 1 + do_execsql_test join7-$id.115 { + SELECT * FROM t1 FULL OUTER JOIN t2 ON b=c + WHERE a=1 OR a IS NULL ORDER BY +b; + } { + NULL NULL 5 55 + 1 2 NULL NULL + 1 3 3 33 + 1 4 4 44 + } + + do_execsql_test join7-$id.116 { + SELECT t1.*, t2.* FROM t2 FULL OUTER JOIN t1 ON b=c + WHERE a=1 OR a IS NULL ORDER BY +b; + } { + NULL NULL 5 55 + 1 2 NULL NULL + 1 3 3 33 + 1 4 4 44 + } + + # Verified against PG-14 for case 1: + do_execsql_test join7-$id.120 { + SELECT * FROM t1 FULL OUTER JOIN t2 ON b=c WHERE a IS NULL ORDER BY +d; + } { + NULL NULL 5 55 + } + + # Verified against PG-14 for case 1: + do_execsql_test join7-$id.130 { + SELECT * FROM t1 FULL OUTER JOIN t2 ON b=c AND d<=0 ORDER BY +b, +d; + } { + NULL NULL 3 33 + NULL NULL 4 44 + NULL NULL 5 55 + 1 2 NULL NULL + 1 3 NULL NULL + 1 4 NULL NULL + } + + # Verified against PG-14 for case 1: + do_execsql_test join7-$id.140 { + SELECT a, b, c, d + FROM t2 FULL OUTER JOIN t1 ON b=c AND d<=0 ORDER BY +b, +d; + } { + NULL NULL 3 33 + NULL NULL 4 44 + NULL NULL 5 55 + 1 2 NULL NULL + 1 3 NULL NULL + 1 4 NULL NULL + } + + do_execsql_test join7-$id.141 { + SELECT a, b, c, d + FROM t2 FULL OUTER JOIN t1 ON b=c AND d<=0 + ORDER BY +b, +d LIMIT 2 OFFSET 2 + } { + NULL NULL 5 55 + 1 2 NULL NULL + } +} +finish_test diff --git a/test/join8.test b/test/join8.test new file mode 100644 index 0000000000..fc50df32ff --- /dev/null +++ b/test/join8.test @@ -0,0 +1,815 @@ +# 2022-04-12 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# This file implements tests for RIGHT and FULL OUTER JOINs. + +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +ifcapable !vtab { + finish_test + return +} + +db null NULL +# EVIDENCE-OF: R-33754-02880 you can say "LEFT RIGHT JOIN" which is the +# same as "FULL JOIN". +do_execsql_test join8-10 { + CREATE TABLE t1(a,b,c); + CREATE TABLE t2(x,y); + CREATE INDEX t2x ON t2(x); + SELECT avg(DISTINCT b) FROM (SELECT * FROM t2 LEFT RIGHT JOIN t1 ON c); +} {NULL} + +# Pending optimization opportunity: +# Row-value initialization subroutines must be called from with the +# RIGHT JOIN body subroutine before the first use of any register containing +# the results of that subroutine. This seems dodgy. Test case: +# +reset_db +do_execsql_test join8-1000 { + CREATE TABLE t1(a INTEGER PRIMARY KEY AUTOINCREMENT,b,c,d,e,f,g,h,j,k,l,m,n,o,p,q,r,s); + CREATE INDEX t1x1 ON t1(g+h,j,k); + CREATE INDEX t1x2 ON t1(b); + INSERT INTO t1 DEFAULT VALUES; +} {} +do_catchsql_test join8-1010 { + SELECT a + FROM ( + SELECT a + FROM ( + SELECT a + FROM ( + SELECT a FROM t1 NATURAL LEFT JOIN t1 + WHERE (b, 2 ) IS ( SELECT 2 IN(2,2),2) + ) + NATURAL LEFT FULL JOIN t1 + WHERE ( rowid , 1 )<=(CASE 5 WHEN 619 THEN 841 ELSE 3374391096 END,0) + ORDER BY a ASC + ) + NATURAL LEFT JOIN t1 + WHERE (b, 2 ) IS ( SELECT 3 IN(3,3),3) + ) + NATURAL LEFT FULL JOIN t1 + WHERE ( rowid , 1 )<=(CASE 5 WHEN 619 THEN 841 ELSE 3374391096 END,0) + ORDER BY a ASC; +} {0 1} + +# Pending issue #2: (now resolved) +# Jump to addrHalt inside the RIGHT JOIN body subroutine bypasses the +# OP_Return, resulting in a subroutine loop. Test case: +# +reset_db +do_execsql_test join8-2000 { + CREATE TABLE t1(a int, b int, c int); + INSERT INTO t1 VALUES(1,2,3),(4,5,6); + CREATE TABLE t2(d int, e int); + INSERT INTO t2 VALUES(3,333),(4,444); + CREATE TABLE t3(f int, g int); + PRAGMA automatic_index=off; +} {} +do_catchsql_test join8-2010 { + SELECT * FROM t1 RIGHT JOIN t2 ON c=d JOIN t3 ON f=e; +} {0 {}} + +# Demonstrate that nested FULL JOINs and USING clauses work +# +reset_db +load_static_extension db series +do_execsql_test join8-3000 { + CREATE TABLE t1(id INTEGER PRIMARY KEY, a INT); + CREATE TABLE t2(id INTEGER PRIMARY KEY, b INT); + CREATE TABLE t3(id INTEGER PRIMARY KEY, c INT); + CREATE TABLE t4(id INTEGER PRIMARY KEY, d INT); + CREATE TABLE t5(id INTEGER PRIMARY KEY, e INT); + CREATE TABLE t6(id INTEGER PRIMARY KEY, f INT); + CREATE TABLE t7(id INTEGER PRIMARY KEY, g INT); + CREATE TABLE t8(id INTEGER PRIMARY KEY, h INT); + INSERT INTO t1 SELECT value, 1 FROM generate_series(1,256) WHERE value & 1; + INSERT INTO t2 SELECT value, 1 FROM generate_series(1,256) WHERE value & 2; + INSERT INTO t3 SELECT value, 1 FROM generate_series(1,256) WHERE value & 4; + INSERT INTO t4 SELECT value, 1 FROM generate_series(1,256) WHERE value & 8; + INSERT INTO t5 SELECT value, 1 FROM generate_series(1,256) WHERE value & 16; + INSERT INTO t6 SELECT value, 1 FROM generate_series(1,256) WHERE value & 32; + INSERT INTO t7 SELECT value, 1 FROM generate_series(1,256) WHERE value & 64; + INSERT INTO t8 SELECT value, 1 FROM generate_series(1,256) WHERE value & 128; + CREATE TABLE t9 AS + SELECT id, h, g, f, e, d, c, b, a + FROM t1 + NATURAL FULL JOIN t2 + NATURAL FULL JOIN t3 + NATURAL FULL JOIN t4 + NATURAL FULL JOIN t5 + NATURAL FULL JOIN t6 + NATURAL FULL JOIN t7 + NATURAL FULL JOIN t8; +} {} +do_execsql_test join8-3010 { + SELECT count(*) FROM t9; +} {255} +do_execsql_test join8-3020 { + SELECT id, count(*) FROM t9 GROUP BY id HAVING count(*)!=1; +} {} +do_execsql_test join8-3030 { + UPDATE t9 SET a=0 WHERE a IS NULL; + UPDATE t9 SET b=0 WHERE b IS NULL; + UPDATE t9 SET c=0 WHERE c IS NULL; + UPDATE t9 SET d=0 WHERE d IS NULL; + UPDATE t9 SET e=0 WHERE e IS NULL; + UPDATE t9 SET f=0 WHERE f IS NULL; + UPDATE t9 SET g=0 WHERE g IS NULL; + UPDATE t9 SET h=0 WHERE h IS NULL; + SELECT count(*) FROM t9 WHERE id=128*h+64*g+32*f+16*e+8*d+4*c+2*b+a; +} {255} +do_execsql_test join8-3040 { + SELECT * FROM t9 WHERE id<>128*h+64*g+32*f+16*e+8*d+4*c+2*b+a; +} {} + +# 2022-04-21 dbsqlfuzz find +# +reset_db +do_execsql_test join8-4000 { + CREATE TABLE t1(x INTEGER PRIMARY KEY, a, b); + INSERT INTO t1 VALUES(1,5555,4); + CREATE INDEX i1a ON t1(a); + CREATE INDEX i1b ON t1(b); + SELECT a FROM t1 NATURAL RIGHT JOIN t1 WHERE a=5555 OR (1,b)==(SELECT 2 IN (2,2),4); +} {5555} + +# 2022-04-23 dbsqlfuzz c7ee5500e3abddec3557016de777713b80c790d3 +# Escape from the right-join body subroutine via the ORDER BY LIMIT optimization. +# +reset_db +db null - +do_catchsql_test join8-5000 { + CREATE TABLE t1(x); + INSERT INTO t1(x) VALUES(NULL),(NULL); + CREATE TABLE t2(c, d); + INSERT INTO t2(c,d) SELECT x, x FROM t1; + CREATE INDEX t2dc ON t2(d, c); + SELECT (SELECT c FROM sqlite_temp_schema FULL JOIN t2 ON d IN (1,2,3) ORDER BY d) AS x FROM t1; +} {0 {- -}} + +# 2022-04-29 dbsqlfuzz 19f1102a70cf966ab249de56d944fc20dbebcfcf +# Verification of testtag-20230227b and testtag-20230227c +# +reset_db +do_execsql_test join8-6000 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT, c TEXT, d REAL); + INSERT INTO t1 VALUES(1,'A','aa',2.5); + SELECT * FROM t1 AS t2 NATURAL RIGHT JOIN t1 AS t3 + WHERE (a,b) IN (SELECT rowid, b FROM t1); +} {1 A aa 2.5} +do_execsql_test join8-6010 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a INT PRIMARY KEY, b TEXT, c TEXT, d INT) WITHOUT ROWID; + INSERT INTO t1 VALUES(15,'xray','baker',42); + SELECT value, t1.* FROM json_each('7') NATURAL RIGHT JOIN t1 + WHERE (a,b) IN (SELECT a, b FROM t1); +} {7 15 xray baker 42} +do_execsql_test join8-6020 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a INTEGER PRIMARY KEY,b); + INSERT INTO t1 VALUES(0,NULL),(1,2); + SELECT value, t1.* FROM json_each('17') NATURAL RIGHT JOIN t1 + WHERE (a,b) IN (SELECT rowid, b FROM t1); +} {17 1 2} +do_execsql_test join8-6021 { + SELECT value, t1.* FROM json_each('null') NATURAL RIGHT JOIN t1 + WHERE (a,b) IN (SELECT rowid, b FROM t1); +} {{} 1 2} +do_execsql_test join8-6022 { + CREATE TABLE a(key TEXT); + INSERT INTO a(key) VALUES('a'),('b'); + SELECT quote(a.key), b.value + FROM a RIGHT JOIN json_each('["a","c"]') AS b ON a.key=b.value; +} {'a' a NULL c} + +# Bloom filter usage by RIGHT and FULL JOIN +# +reset_db +do_execsql_test join8-7000 { +CREATE TABLE t1(a INT, b INT, c INT, d INT); + WITH RECURSIVE c(x) AS (VALUES(0) UNION ALL SELECT x+1 FROM c WHERE x<10) + INSERT INTO t1(a,b,c,d) SELECT x, x+100, x+200, x+300 FROM c; + CREATE TABLE t2(b INT, x INT); + INSERT INTO t2(b,x) SELECT b, a FROM t1 WHERE a%2=0; + CREATE INDEX t2b ON t2(b); + CREATE TABLE t3(c INT, y INT); + INSERT INTO t3(c,y) SELECT c, a FROM t1 WHERE a%3=0; + CREATE INDEX t3c ON t3(c); + CREATE TABLE t4(d INT, z INT); + INSERT INTO t4(d,z) SELECT d, a FROM t1 WHERE a%5=0; + CREATE INDEX t4d ON t4(d); + INSERT INTO t1(a,b,c,d) VALUES + (96,NULL,296,396), + (97,197,NULL,397), + (98,198,298,NULL), + (99,NULL,NULL,NULL); + ANALYZE sqlite_schema; + INSERT INTO sqlite_stat1 VALUES('t4','t4d','20 1'); + INSERT INTO sqlite_stat1 VALUES('t3','t3c','32 1'); + INSERT INTO sqlite_stat1 VALUES('t2','t2b','48 1'); + INSERT INTO sqlite_stat1 VALUES('t1',NULL,'100'); + ANALYZE sqlite_schema; +} {} +db null - +do_execsql_test join8-7010 { + WITH t0 AS MATERIALIZED ( + SELECT t1.*, t2.*, t3.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + ) + SELECT * FROM t0 FULL JOIN t4 ON t0.a=t4.d AND t4.z>0 + ORDER BY coalesce(t0.a, t0.y+200, t4.d); +} { + 6 106 206 306 106 6 206 6 - - + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 +} + +# EVIDENCE-OF: R-33754-02880 you can say "LEFT RIGHT JOIN" which is the +# same as "FULL JOIN". +do_execsql_test join8-7011 { + WITH t0 AS MATERIALIZED ( + SELECT t1.*, t2.*, t3.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + ) + SELECT * FROM t0 LEFT RIGHT JOIN t4 ON t0.a=t4.d AND t4.z>0 + ORDER BY coalesce(t0.a, t0.y+200, t4.d); +} { + 6 106 206 306 106 6 206 6 - - + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 +} + +do_execsql_test join8-7020 { + EXPLAIN QUERY PLAN + WITH t0 AS MATERIALIZED ( + SELECT t1.*, t2.*, t3.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + ) + SELECT * FROM t0 FULL JOIN t4 ON t0.a=t4.d AND t4.z>0 + ORDER BY coalesce(t0.a, t0.y+200, t4.d); +} {/.*BLOOM FILTER ON t2.*BLOOM FILTER ON t3.*/} + +# 2022-05-12 Difference with PG found (by Dan) while exploring +# https://sqlite.org/forum/forumpost/677a0ab93fcd9ccd +# +reset_db +do_execsql_test join8-8000 { + CREATE TABLE t1(a INT, b INT); + CREATE TABLE t2(c INT, d INT); + CREATE TABLE t3(e INT, f INT); + INSERT INTO t1 VALUES(1, 2); + INSERT INTO t2 VALUES(3, 4); + INSERT INTO t3 VALUES(5, 6); +} {} +do_execsql_test join8-8010 { + SELECT * + FROM t3 LEFT JOIN t2 ON true + JOIN t1 ON (t3.e IS t2.c); +} {} +do_execsql_test join8-8020 { + SELECT * + FROM t3 LEFT JOIN t2 ON true + JOIN t1 ON (t3.e IS NOT DISTINCT FROM t2.c); +} {} + +# 2022-05-13 The idea of reusing subquery cursors does not +# work, if the cursors are used both for scanning and lookups. +# +reset_db +db null - +do_execsql_test join8-9000 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT, c TEXT, d REAL); + INSERT INTO t1 VALUES(1,'E','bb',NULL),(2,NULL,NULL,NULL); + SELECT * FROM t1 NATURAL RIGHT JOIN t1 AS t2 WHERE (a,b) IN (SELECT a+0, b FROM t1); +} {1 E bb -} + +# 2022-05-14 https://sqlite.org/forum/forumpost/c06b10ad7e +# +reset_db +db null - +do_execsql_test join8-10000 { + CREATE TABLE t1(c0 INT UNIQUE); + CREATE TABLE t2(c0); + CREATE TABLE t2i(c0 INT); + CREATE TABLE t3(c0 INT); + INSERT INTO t1 VALUES(1); + INSERT INTO t2 VALUES(2); + INSERT INTO t2i VALUES(2); + INSERT INTO t3 VALUES(3); +} {} +do_execsql_test join8-10010 { + SELECT DISTINCT t1.c0, t3.c0 + FROM t2 NATURAL JOIN t1 RIGHT JOIN t3 ON t1.c0; +} {- 3} +do_execsql_test join8-10020 { + SELECT t1.c0, t3.c0 + FROM t2 NATURAL JOIN t1 RIGHT JOIN t3 ON t1.c0; +} {- 3} +do_execsql_test join8-10030 { + SELECT DISTINCT t1.c0, t3.c0 + FROM t2 NATURAL CROSS JOIN t1 RIGHT JOIN t3 ON t1.c0; +} {- 3} +do_execsql_test join8-10040 { + SELECT t1.c0, t3.c0 + FROM t1 NATURAL CROSS JOIN t2 RIGHT JOIN t3 ON t1.c0; +} {- 3} +do_execsql_test join8-10050 { + SELECT DISTINCT t1.c0, t3.c0 + FROM t2i NATURAL JOIN t1 RIGHT JOIN t3 ON t1.c0; +} {- 3} +do_execsql_test join8-10060 { + SELECT DISTINCT +t1.c0, t3.c0 + FROM t2 NATURAL JOIN t1 RIGHT JOIN t3 ON t1.c0; +} {- 3} +do_execsql_test join8-10070 { + SELECT DISTINCT +t1.c0, t3.c0 + FROM t1 NATURAL CROSS JOIN t2 RIGHT JOIN t3 ON t1.c0; +} {- 3} +do_execsql_test join8-10080 { + SELECT DISTINCT t1.c0, t3.c0 + FROM t2 NATURAL JOIN t1 RIGHT JOIN t3 ON t1.c0<>0; +} {- 3} + +# 2022-05-14 +# index-on-expr scan on a RIGHT JOIN +# dbsqlfuzz 39ee60004ff027a9e2846cf76e02cd5ac0953739 +# +reset_db +db null - +do_execsql_test join8-11000 { + CREATE TABLE t1(a); + CREATE TABLE t2(b); + INSERT INTO t2 VALUES(0),(1),(2); + SELECT * FROM t1 RIGHT JOIN t2 ON (a=b) WHERE 99+(b+1)!=99; +} {- 0 - 1 - 2} +do_execsql_test join8-11010 { + CREATE INDEX t2b ON t2(b+1) WHERE b IS NOT NULL; + SELECT * FROM t1 RIGHT JOIN t2 ON (a=b) WHERE 99+(b+1)!=99; +} {- 0 - 1 - 2} +do_execsql_test join8-11020 { + DROP TABLE t1; + DROP TABLE t2; + CREATE TABLE t1(a); + CREATE TABLE t2(b, c, d); + INSERT INTO t2 VALUES(1, 3, 'not-4'); + SELECT b, d FROM t1 RIGHT JOIN t2 WHERE (b+0)=1 AND d!=4; +} {1 not-4} +do_execsql_test join8-11030 { + CREATE INDEX i2 ON t2((b+0), d); + SELECT b, d FROM t1 RIGHT JOIN t2 WHERE (b+0)=1 AND d!=4; +} {1 not-4} +do_execsql_test join8-11040 { + DROP INDEX i2; + CREATE INDEX i2 ON t2((b+0), d) WHERE d IS NOT NULL; + SELECT b, d FROM t1 RIGHT JOIN t2 WHERE (b+0)=1 AND d!=4; +} {1 not-4} + +# 2022-05-23 +# NATURAL JOIN name resolution is more forgiving with LEFT JOIN +# https://sqlite.org/forum/forumpost/e90a8e6e6f +# +reset_db +db null - +do_execsql_test join8-12000 { + CREATE TABLE t1(a INT); INSERT INTO t1 VALUES(0),(1); + CREATE TABLE t2(a INT); INSERT INTO t2 VALUES(0),(2); + CREATE TABLE t3(a INT); INSERT INTO t3 VALUES(0),(3); +} {} +do_catchsql_test join8-12010 { + SELECT * FROM t1 RIGHT JOIN t2 ON t2.a<>0 NATURAL RIGHT JOIN t3; +} {1 {ambiguous reference to a in USING()}} +do_catchsql_test join8-12020 { + SELECT * FROM t1 RIGHT JOIN t2 ON t2.a<>0 NATURAL LEFT JOIN t3; +} {1 {ambiguous reference to a in USING()}} +do_catchsql_test join8-12030 { + SELECT * FROM t1 LEFT JOIN t2 ON t2.a<>0 NATURAL RIGHT JOIN t3; +} {1 {ambiguous reference to a in USING()}} + +# The following query should probably also return the same error as the +# previous three cases. However, historical versions of SQLite have always +# let it pass. We will not "fix" this, since to do so might break legacy +# applications. +# +do_catchsql_test join8-12040 { + SELECT * FROM t1 LEFT JOIN t2 ON t2.a<>0 NATURAL LEFT JOIN t3; +} {0 {0 2 1 2}} + +# 2022-05-24 +# https://sqlite.org/forum/forumpost/687b0bf563a1d4f1 +# +reset_db +do_execsql_test join8-13000 { + CREATE TABLE t0(t TEXT, u TEXT); INSERT INTO t0 VALUES('t', 'u'); + CREATE TABLE t1(v TEXT, w TEXT); INSERT INTO t1 VALUES('v', 'w'); + CREATE TABLE t2(x TEXT, y TEXT); INSERT INTO t2 VALUES('x', 'y'); + SELECT * FROM t0 JOIN t1 ON (t2.x NOTNULL) LEFT JOIN t2 ON false; + SELECT * FROM t0 JOIN t1 ON (t2.x NOTNULL) LEFT JOIN t2 ON false + WHERE t2.y ISNULL; +} {} + +# 2022-05-25 +# https://sqlite.org/forum/forumpost/5cfe08eed6 +# +reset_db +do_execsql_test join8-14000 { + CREATE TABLE t0(a TEXT, b TEXT, c TEXT); + CREATE TABLE t1(a TEXT); + INSERT INTO t1 VALUES('1'); + CREATE VIEW v0 AS SELECT 'xyz' AS d; + SELECT * FROM v0 RIGHT JOIN t1 ON t1.a<>'' INNER JOIN t0 ON t0.c<>''; + SELECT * FROM v0 RIGHT JOIN t1 ON t1.a<>'' INNER JOIN t0 ON t0.c<>'' WHERE b ISNULL; +} {} +do_execsql_test join8-14010 { + CREATE TABLE y0(a INT); + CREATE TABLE y1(b INT); INSERT INTO y1 VALUES(1), (2); + CREATE TABLE y2(c INT); INSERT INTO y2 VALUES(3), (4); +} {} +db null - +do_execsql_test join8-14020 { + SELECT * FROM y0 RIGHT JOIN y1 ON true INNER JOIN y2 ON true WHERE y2.c!=99 AND y2.c!=98; +} { + - 1 3 + - 1 4 + - 2 3 + - 2 4 +} + +# 2022-05-30 +# https://sqlite.org/forum/forumpost/3902c7b833 +# +reset_db +do_execsql_test join8-15000 { + CREATE TABLE t1(x INT); + CREATE TABLE t2(y INT); + CREATE TABLE t3(z INT); + INSERT INTO t1 VALUES(10); + INSERT INTO t3 VALUES(20),(30); +} +do_execsql_test join8-15010 { + SELECT * FROM t1 LEFT JOIN t2 ON true JOIN t3 ON t2.y IS NOT NULL; +} {} +do_execsql_test join8-15020 { + SELECT * FROM t1 LEFT JOIN t2 ON true JOIN t3 ON t2.y IS NOT NULL + WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600); +} {} +do_execsql_test join8-15100 { + PRAGMA automatic_index = 0; + CREATE TABLE t4(x TEXT); + CREATE TABLE t5(y TEXT); + CREATE TABLE t6(z TEXT); + INSERT INTO t4 VALUES('a'), ('b'); + INSERT INTO t5 VALUES('b'), ('c'); + INSERT INTO t6 VALUES('a'), ('d'); +} {} +db null - +do_execsql_test join8-15110 { + SELECT * FROM t4 LEFT JOIN t5 ON x=y LEFT JOIN t6 ON (x=z) ORDER BY +x; +} {a - a b b -} +do_execsql_test join8-15120 { + SELECT * FROM t4 LEFT JOIN t5 ON x=y LEFT JOIN t6 ON (x=z) + WHERE t5.y!='x' AND t4.x!='x'; +} {b b -} + +# 2022-05-31 +# https://sqlite.org/forum/forumpost/c2554d560b +reset_db +do_execsql_test join8-16000 { + CREATE TABLE t1(a TEXT); + CREATE TABLE t2(b TEXT); + CREATE TABLE t3(c TEXT); + INSERT INTO t2(b) VALUES ('x'); + INSERT INTO t3(c) VALUES ('y'), ('z'); +} {} +db null - +do_execsql_test join8-16010 { + SELECT * FROM t1 RIGHT JOIN t2 ON true LEFT JOIN t3 ON a<>''; +} {- x -} +do_execsql_test join8-16020 { + SELECT * FROM t1 RIGHT JOIN t2 ON true LEFT JOIN t3 ON a<>'' WHERE c IS NULL; +} {- x -} +do_execsql_test join8-16020 { + SELECT * FROM t1 RIGHT JOIN t2 ON true JOIN t3 ON a<>'' WHERE c IS NULL; +} {} +do_execsql_test join8-16030 { + SELECT * FROM t1 RIGHT JOIN t2 ON true JOIN t3 ON a<>''; +} {} +do_execsql_test join8-16040 { + SELECT * FROM t1 RIGHT JOIN t2 ON true LEFT JOIN t3 ON a<>'' WHERE c<>''; +} {} +do_execsql_test join8-16050 { + SELECT * FROM t1 RIGHT JOIN t2 ON true LEFT JOIN t3 ON a<>'' WHERE c IS NOT NULL; +} {} +do_execsql_test join8-16060 { + SELECT * FROM t1 RIGHT JOIN t2 ON true JOIN t3 ON a<>'' WHERE c<>''; +} {} +do_execsql_test join8-16070 { + SELECT * FROM t1 RIGHT JOIN t2 ON true JOIN t3 ON a<>'' WHERE c IS NOT NULL; +} {} + +# 2022-06-01 +# https://sqlite.org/forum/forumpost/087de2d9ec +# +reset_db +do_execsql_test join8-17000 { + CREATE TABLE t1(id INTEGER PRIMARY KEY, x INT, y INT); + CREATE TABLE t2(z INT); + INSERT INTO t1(id,x,y) VALUES(1, 0, 0); +} {} +db null NULL +do_execsql_test join8-17010 { + SELECT * FROM t2 RIGHT JOIN t1 ON true; +} {NULL 1 0 0} +do_execsql_test join8-17020 { + SELECT 99=id AND 0=y AS "truth" FROM t2 RIGHT JOIN t1 ON true; +} {0} +do_execsql_test join8-17030 { + SELECT (99, 0)==(id, y) AS "truth" FROM t2 RIGHT JOIN t1; +} {0} +do_execsql_test join8-17040 { + SELECT * FROM t2 RIGHT JOIN t1 WHERE 99=id AND 0=y; +} {} +do_execsql_test join8-17041 { + SELECT * FROM t2 RIGHT JOIN t1 WHERE 99=+id AND 0=y; +} {} +do_execsql_test join8-17050 { + SELECT * FROM t2 RIGHT JOIN t1 WHERE (99, 0)==(id,y); +} {} +do_execsql_test join8-17051 { + SELECT * FROM t2 RIGHT JOIN t1 WHERE (99, 0)==(+id,y); +} {} +do_execsql_test join8-17060 { + SELECT * FROM t2 RIGHT JOIN t1 WHERE 1=id AND 0=y; +} {NULL 1 0 0} +do_execsql_test join8-17061 { + SELECT * FROM t2 RIGHT JOIN t1 WHERE 1=+id AND 0=y; +} {NULL 1 0 0} +do_execsql_test join8-17070 { + SELECT * FROM t2 RIGHT JOIN t1 WHERE (1, 0)==(id,y); +} {NULL 1 0 0} +do_execsql_test join8-17071 { + SELECT * FROM t2 RIGHT JOIN t1 WHERE (1, 0)==(+id,y); +} {NULL 1 0 0} +do_execsql_test join8-17080 { + CREATE TABLE t3(a INTEGER PRIMARY KEY, b INT); + CREATE TABLE t4(x INT, y INT); + INSERT INTO t3(a,b) VALUES(1, 3); +} {} +do_execsql_test join8-17090 { + SELECT t3.a FROM t4 RIGHT JOIN t3 ON (x=a) WHERE (b, 4)=(SELECT 3, 4); +} {1} +do_execsql_test join8-17091 { + SELECT t3.a FROM t4 RIGHT JOIN t3 ON (x=a) WHERE (b, 4) IS (SELECT 3, 4); +} {1} + +# 2022-06-06 +# https://sqlite.org/forum/forumpost/206d99a16dd9212f +# tag-20191211-001 +# +reset_db +do_execsql_test join8-18000 { + CREATE TABLE t1(a BOOLEAN); INSERT INTO t1 VALUES (false); + CREATE TABLE t2(x INT); INSERT INTO t2 VALUES (0); + SELECT *, x NOTNULL, (x NOTNULL)=a FROM t2 RIGHT JOIN t1 ON true WHERE (x NOTNULL)=a; +} {} +do_execsql_test join8-18010 { + CREATE INDEX t1a ON t1(a); + SELECT *, x NOTNULL, (x NOTNULL)=a FROM t2 RIGHT JOIN t1 ON true WHERE (x NOTNULL)=a; +} {} + +do_execsql_test join8-18020 { + CREATE TABLE t3(z); + INSERT INTO t3 VALUES('t3value'); + SELECT *, x NOTNULL, (x NOTNULL)=a FROM t2 RIGHT JOIN t1 ON true INNER JOIN t3 ON (x NOTNULL)=a; +} {} + +ifcapable rtree { + do_execsql_test join8-18030 { + CREATE VIRTUAL TABLE rtree1 USING rtree(a, x1, x2); + INSERT INTO rtree1 VALUES(0, 0, 0); + } + do_execsql_test join8-18040 { + SELECT *, x NOTNULL, (x NOTNULL)=a FROM t2 + RIGHT JOIN rtree1 ON true INNER JOIN t3 ON (x NOTNULL)=+a; + } {} + do_execsql_test join8-18050 { + SELECT *, x NOTNULL, (x NOTNULL)=a FROM t2 + RIGHT JOIN rtree1 ON true INNER JOIN t3 ON (x NOTNULL)=a; + } {} +} + + +reset_db +do_execsql_test join8-19000 { + CREATE TABLE t1(a INT); + CREATE TABLE t2(b INT, c INT); + CREATE TABLE t3(d INT); + + INSERT INTO t1 VALUES(10); + INSERT INTO t2 VALUES(50,51); + INSERT INTO t3 VALUES(299); + + CREATE INDEX t2b ON t2( (b IS NOT NULL) ); +} + +do_execsql_test join8-19010 { + SELECT * FROM t1 LEFT JOIN t2 ON true INNER JOIN t3 ON (b IS NOT NULL)=0; +} + +# 2022-06-07 +# https://sqlite.org/forum/forumpost/323f86cc30 +reset_db +do_execsql_test join8-20000 { + CREATE TABLE t1(x TEXT); + INSERT INTO t1(x) VALUES('aaa'); + CREATE VIEW v0(y) AS SELECT x FROM t1; + CREATE TABLE t2(z TEXT); +} {} +db null - +do_execsql_test join8-20010 { + SELECT * FROM t2 JOIN v0 ON z<>'bbb' RIGHT JOIN t1 ON z<>'ccc'; +} {- - aaa} +do_execsql_test join8-20020 { + SELECT * FROM t2 JOIN v0 ON z<>'bbb' RIGHT JOIN t1 ON z<>'ccc' ORDER BY z; +} {- - aaa} +do_execsql_test join8-20030 { + SELECT 99 as "m" FROM t2 JOIN v0 ON z<>'bbb' RIGHT JOIN t1 ON z<>'ccc'; +} {99} +do_execsql_test join8-20040 { + SELECT 99 as "m" FROM t2 JOIN v0 ON z<>'bbb' RIGHT JOIN t1 ON z<>'ccc' ORDER BY z; +} {99} +do_execsql_test join8-20050 { + SELECT count(*) + FROM (SELECT 99 as "m" FROM t2 JOIN v0 ON z<>'' RIGHT JOIN t1 ON z<>'') AS "t3"; +} {1} +do_execsql_test join8-20060 { + SELECT count(*) + FROM (SELECT 99 as "m" FROM t2 JOIN v0 ON z<>'' RIGHT JOIN t1 ON z<>'' ORDER BY z) AS "t3"; +} {1} + +# 2022-06-10 +# https://sqlite.org/forum/forumpost/8e4c352937e82929 +# +# Do not allow constant propagation between ON and WHERE clause terms. +# (Updated 2022-06-20) See also https://sqlite.org/forum/forumpost/57bdf2217d +# +reset_db +do_execsql_test join8-21000 { + CREATE TABLE t1(a INT,b BOOLEAN); + CREATE TABLE t2(c INT); INSERT INTO t2 VALUES(NULL); + CREATE TABLE t3(d INT); +} +do_execsql_test join8-21010 { + SELECT (b IS TRUE) FROM t1 JOIN t3 ON (b=TRUE) RIGHT JOIN t2 ON TRUE; +} {0} +do_execsql_test join8-22020 { + SELECT * FROM t1 JOIN t3 ON (b=TRUE) RIGHT JOIN t2 ON TRUE WHERE (b IS TRUE); +} {} +do_execsql_test join8-22030 { + DROP TABLE t1; + DROP TABLE t2; + DROP TABLE t3; + CREATE TABLE t1(a INT); + CREATE TABLE t2(b INT); + CREATE TABLE t3(c INTEGER PRIMARY KEY, d INT); + CREATE INDEX t3d ON t3(d); + INSERT INTO t3 VALUES(0, 0); +} +do_catchsql_test join8-22031 { + SELECT * FROM t1 JOIN t2 ON d>b RIGHT JOIN t3 ON true WHERE +d = 0; +} {1 {ON clause references tables to its right}} +do_catchsql_test join8-22040 { + SELECT * FROM t1 JOIN t2 ON d>b RIGHT JOIN t3 ON true WHERE d = 0; +} {1 {ON clause references tables to its right}} + + +# 2022-06-10 +# https://sqlite.org/forum/forumpost/51e6959f61 +# +# Restrictions on the usage of WHERE clause constraints by joins that are +# involved with a RIGHT JOIN must also be applied to automatic indexes. +# +reset_db +do_execsql_test join8-22000 { + CREATE TABLE t1(a INT); + CREATE TABLE t2(b INT); + CREATE TABLE t3(c TEXT); INSERT INTO t3 VALUES('x'); + CREATE TABLE t4(d TEXT); INSERT INTO t4 VALUES('y'); + SELECT 99 + FROM t1 + LEFT JOIN t2 ON true + RIGHT JOIN t3 ON true + RIGHT JOIN t4 ON true + WHERE a=b; +} {} + +# 2022-06-13 +# https://sqlite.org/forum/forumpost/b40696f501 +# +# This optimization that converts "x ISNULL" into "FALSE" when column "x" has a +# NOT NULL constraint is too aggresive if the query contains RIGHT JOIN. +# +reset_db +db null - +do_execsql_test join8-23000 { + CREATE TABLE t1(a TEXT); + INSERT INTO t1 VALUES('c'); + CREATE TABLE t2(b TEXT, c TEXT NOT NULL); + INSERT INTO t2 VALUES('a', 'b'); + CREATE TABLE t3(d TEXT); + INSERT INTO t3 VALUES('x'); + CREATE TABLE t4(e TEXT); + INSERT INTO t4 VALUES('y'); +} +do_execsql_test join8-23010 { + SELECT * + FROM t1 + LEFT JOIN t2 ON TRUE + JOIN t3 ON c='' + RIGHT JOIN t4 ON b=''; +} {- - - - y} +do_execsql_test join8-23020 { + SELECT * + FROM t1 + LEFT JOIN t2 ON TRUE + JOIN t3 ON c='' + RIGHT JOIN t4 ON b='' + WHERE d ISNULL +} {- - - - y} + +# 2022-06-14 +# dbsqlfuzz 2f3101834d14325a976f601b9267a0fd323d6bbd +# +# When the OP_NullRow opcode creates a new cursor, it must +# set the cursor to no-reuse so that an OP_OpenEphemeral in +# a subroutine does not try to reuse it. +# +reset_db +db null - +do_execsql_test join8-24000 { + CREATE TABLE t4(b INT, c INT); + CREATE TABLE t5(a INT, f INT); + INSERT INTO t5 VALUES(1,2); + WITH t7(x, y) AS (SELECT 100, 200 FROM t5) + SELECT * FROM t4 JOIN t7 ON true RIGHT JOIN (SELECT y AS z FROM t7) AS t6 ON (x=z); +} {- - - - 200} + +# 2022-06-20 +# forum/forumpost/6650cd40b5634f35 +# +reset_db +do_execsql_test join8-25000 { + CREATE TABLE t1(a1 INT); + CREATE TABLE t2(b2 INT); + CREATE TABLE t3(c3 INT, d3 INT UNIQUE); + CREATE TABLE t4(e4 INT, f4 TEXT); + INSERT INTO t3(c3, d3) VALUES (2, 1); + INSERT INTO t4(f4) VALUES ('x'); + CREATE INDEX i0 ON t3(c3) WHERE d3 ISNULL; + ANALYZE main; +} +db null - +do_execsql_test join8-25010 { + SELECT * FROM t1 LEFT JOIN t2 ON true JOIN t3 ON (b2 IN (a1)) FULL JOIN t4 ON true; +} {- - - - - x} +do_execsql_test join8-25020 { + SELECT 1 FROM t1 LEFT JOIN t2 ON true JOIN t3 ON (b2 IN (a1)) FULL JOIN t4 ON true; +} {1} + +# 2022-07-13 +# forum/forumpost/174afeae57 +# +reset_db +db null - +do_execsql_test join8-26000 { + CREATE TABLE t1(a INT); + CREATE TABLE t2(b INT, c INT); + CREATE VIEW t3(d) AS SELECT NULL FROM t2 FULL OUTER JOIN t1 ON c=a UNION ALL SELECT b FROM t2; + INSERT INTO t1(a) VALUES (NULL); + INSERT INTO t2(b, c) VALUES (99, NULL); + SELECT DISTINCT b, c, d FROM t2, t3 WHERE b<>0 + UNION SELECT DISTINCT b, c, d FROM t2, t3 WHERE b ISNULL; +} {99 - - 99 - 99} + +finish_test diff --git a/test/join9.test b/test/join9.test new file mode 100644 index 0000000000..e547d4ce70 --- /dev/null +++ b/test/join9.test @@ -0,0 +1,565 @@ +# 2022-04-16 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. +# +# This file implements tests for RIGHT and FULL OUTER JOINs. + +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +foreach {id schema} { + 1 { + CREATE TABLE t3(id INTEGER PRIMARY KEY, w TEXT); + CREATE TABLE t4(id INTEGER PRIMARY KEY, x TEXT); + CREATE TABLE t5(id INTEGER PRIMARY KEY, y TEXT); + CREATE TABLE t6(id INTEGER PRIMARY KEY, z INT); + CREATE VIEW dual(dummy) AS VALUES('x'); + INSERT INTO t3(id,w) VALUES(2,'two'),(3,'three'),(6,'six'),(7,'seven'); + INSERT INTO t4(id,x) VALUES(2,'alice'),(4,'bob'),(6,'cindy'),(8,'dave'); + INSERT INTO t5(id,y) VALUES(1,'red'),(2,'orange'),(3,'yellow'),(4,'green'), + (5,'blue'); + INSERT INTO t6(id,z) VALUES(3,333),(4,444),(5,555),(0,1000),(9,999); + } + 2 { + CREATE TABLE t3(id INT PRIMARY KEY, w TEXT) WITHOUT ROWID; + CREATE TABLE t4(id INT PRIMARY KEY, x TEXT) WITHOUT ROWID; + CREATE TABLE t5(id INT PRIMARY KEY, y TEXT) WITHOUT ROWID; + CREATE TABLE t6(id INT PRIMARY KEY, z INT) WITHOUT ROWID; + CREATE TABLE dual(dummy TEXT); + INSERT INTO dual(dummy) VALUES('x'); + INSERT INTO t3(id,w) VALUES(2,'two'),(3,'three'),(6,'six'),(7,'seven'); + INSERT INTO t4(id,x) VALUES(2,'alice'),(4,'bob'),(6,'cindy'),(8,'dave'); + INSERT INTO t5(id,y) VALUES(1,'red'),(2,'orange'),(3,'yellow'),(4,'green'), + (5,'blue'); + INSERT INTO t6(id,z) VALUES(3,333),(4,444),(5,555),(0,1000),(9,999); + } + 3 { + CREATE TABLE t3x(id INTEGER PRIMARY KEY, w TEXT); + CREATE TABLE t4x(id INTEGER PRIMARY KEY, x TEXT); + CREATE TABLE t5x(id INTEGER PRIMARY KEY, y TEXT); + CREATE TABLE t6x(id INTEGER PRIMARY KEY, z INT); + CREATE VIEW dual(dummy) AS VALUES('x'); + INSERT INTO t3x(id,w) VALUES(2,'two'),(3,'three'),(6,'six'),(7,'seven'); + INSERT INTO t4x(id,x) VALUES(2,'alice'),(4,'bob'),(6,'cindy'),(8,'dave'); + INSERT INTO t5x(id,y) VALUES(1,'red'),(2,'orange'),(3,'yellow'),(4,'green'), + (5,'blue'); + INSERT INTO t6x(id,z) VALUES(3,333),(4,444),(5,555),(0,1000),(9,999); + CREATE VIEW t3 AS SELECT * FROM t3x LIMIT 1000; + CREATE VIEW t4 AS SELECT * FROM t4x LIMIT 1000; + CREATE VIEW t5 AS SELECT * FROM t5x LIMIT 1000; + CREATE VIEW t6 AS SELECT * FROM t6x LIMIT 1000; + } + 4 { + CREATE TABLE t3a(id INTEGER PRIMARY KEY, w TEXT); + CREATE TABLE t3b(id INTEGER PRIMARY KEY, w TEXT); + CREATE TABLE t4a(id INTEGER PRIMARY KEY, x TEXT); + CREATE TABLE t4b(id INTEGER PRIMARY KEY, x TEXT); + CREATE TABLE t5a(id INTEGER PRIMARY KEY, y TEXT); + CREATE TABLE t5b(id INTEGER PRIMARY KEY, y TEXT); + CREATE TABLE t6a(id INTEGER PRIMARY KEY, z INT); + CREATE TABLE t6b(id INTEGER PRIMARY KEY, z INT); + CREATE VIEW dual(dummy) AS VALUES('x'); + INSERT INTO t3a(id,w) VALUES(2,'two'),(3,'three'); + INSERT INTO t3b(id,w) VALUES(6,'six'),(7,'seven'); + INSERT INTO t4a(id,x) VALUES(2,'alice'),(4,'bob'); + INSERT INTO t4b(id,x) VALUES(6,'cindy'),(8,'dave'); + INSERT INTO t5a(id,y) VALUES(1,'red'),(2,'orange'),(3,'yellow'); + INSERT INTO t5b(id,y) VALUES(4,'green'),(5,'blue'); + INSERT INTO t6a(id,z) VALUES(3,333),(4,444); + INSERT INTO t6b(id,z) VALUES(5,555),(0,1000),(9,999); + CREATE VIEW t3 AS SELECT * FROM t3a UNION ALL SELECT * FROM t3b; + CREATE VIEW t4 AS SELECT * FROM t4a UNION ALL SELECT * FROM t4b; + CREATE VIEW t5 AS SELECT * FROM t5a UNION ALL SELECT * FROM t5b; + CREATE VIEW t6 AS SELECT * FROM t6a UNION ALL SELECT * FROM t6b; + } + 5 { + CREATE TABLE t3a(id INTEGER PRIMARY KEY, w TEXT) WITHOUT ROWID; + CREATE TABLE t3b(id INTEGER PRIMARY KEY, w TEXT); + CREATE TABLE t4a(id INTEGER PRIMARY KEY, x TEXT) WITHOUT ROWID; + CREATE TABLE t4b(id INTEGER PRIMARY KEY, x TEXT) WITHOUT ROWID; + CREATE TABLE t5a(id INTEGER PRIMARY KEY, y TEXT); + CREATE TABLE t5b(id INTEGER PRIMARY KEY, y TEXT) WITHOUT ROWID; + CREATE TABLE t6a(id INTEGER PRIMARY KEY, z INT); + CREATE TABLE t6b(id INTEGER PRIMARY KEY, z INT); + CREATE VIEW dual(dummy) AS VALUES('x'); + INSERT INTO t3a(id,w) VALUES(2,'two'),(3,'three'); + INSERT INTO t3b(id,w) VALUES(6,'six'),(7,'seven'); + INSERT INTO t4a(id,x) VALUES(2,'alice'),(4,'bob'); + INSERT INTO t4b(id,x) VALUES(6,'cindy'),(8,'dave'); + INSERT INTO t5a(id,y) VALUES(1,'red'),(2,'orange'),(3,'yellow'); + INSERT INTO t5b(id,y) VALUES(4,'green'),(5,'blue'); + INSERT INTO t6a(id,z) VALUES(3,333),(4,444); + INSERT INTO t6b(id,z) VALUES(5,555),(0,1000),(9,999); + CREATE VIEW t3 AS SELECT * FROM t3a UNION ALL SELECT * FROM t3b; + CREATE VIEW t4 AS SELECT * FROM t4a UNION ALL SELECT * FROM t4b LIMIT 50; + CREATE VIEW t5 AS SELECT * FROM t5a UNION ALL SELECT * FROM t5b LIMIT 100; + CREATE VIEW t6 AS SELECT * FROM t6a UNION ALL SELECT * FROM t6b; + } +} { + reset_db + db nullvalue - + do_execsql_test join9-$id.setup $schema {} + + # Verifid by PG-14 for case 1 + do_execsql_test join9-$id.100 { + SELECT *, t4.id, t5.id, t6.id + FROM t4 NATURAL LEFT JOIN t5 NATURAL LEFT JOIN t6 + ORDER BY 1; + } { + 2 alice orange - 2 2 - + 4 bob green 444 4 4 4 + 6 cindy - - 6 - - + 8 dave - - 8 - - + } + + do_execsql_test join9-$id.101 { + SELECT *, t4.id, t5.id, t6.id + FROM t4 NATURAL LEFT JOIN t5 NATURAL LEFT JOIN t6 + ORDER BY id; + } { + 2 alice orange - 2 2 - + 4 bob green 444 4 4 4 + 6 cindy - - 6 - - + 8 dave - - 8 - - + } + do_execsql_test join9-$id.102 { + SELECT *, t4.id, t5.id, t6.id + FROM t4 LEFT JOIN t5 USING(id) LEFT JOIN t6 USING(id) + ORDER BY id; + } { + 2 alice orange - 2 2 - + 4 bob green 444 4 4 4 + 6 cindy - - 6 - - + 8 dave - - 8 - - + } + + # Verifid by PG-14 using case 1 + do_execsql_test join9-$id.200 { + SELECT id, x, y, z, t4.id, t5.id, t6.id + FROM t5 NATURAL RIGHT JOIN t4 NATURAL LEFT JOIN t6 + ORDER BY 1; + } { + 2 alice orange - 2 2 - + 4 bob green 444 4 4 4 + 6 cindy - - 6 - - + 8 dave - - 8 - - + } + + do_execsql_test join9-$id.201 { + SELECT id, x, y, z, t4.id, t5.id, t6.id + FROM t5 NATURAL RIGHT JOIN t4 NATURAL LEFT JOIN t6 + ORDER BY id; + } { + 2 alice orange - 2 2 - + 4 bob green 444 4 4 4 + 6 cindy - - 6 - - + 8 dave - - 8 - - + } + + # Verified by PG-14 using case 1 + do_execsql_test join9-$id.300 { + SELECT *, t4.id, t5.id, t6.id + FROM t4 NATURAL RIGHT JOIN t5 NATURAL RIGHT JOIN t6 + ORDER BY 1; + } { + 0 - - 1000 - - 0 + 3 - yellow 333 - 3 3 + 4 bob green 444 4 4 4 + 5 - blue 555 - 5 5 + 9 - - 999 - - 9 + } + + do_execsql_test join9-$id.301 { + SELECT *, t4.id, t5.id, t6.id + FROM t4 NATURAL RIGHT JOIN t5 NATURAL RIGHT JOIN t6 + ORDER BY id; + } { + 0 - - 1000 - - 0 + 3 - yellow 333 - 3 3 + 4 bob green 444 4 4 4 + 5 - blue 555 - 5 5 + 9 - - 999 - - 9 + } + + # Verified by PG-14 for case 1 + do_execsql_test join9-$id.400 { + SELECT *, t4.id, t5.id, t6.id + FROM t4 NATURAL FULL JOIN t5 NATURAL FULL JOIN t6 + ORDER BY 1; + } { + 0 - - 1000 - - 0 + 1 - red - - 1 - + 2 alice orange - 2 2 - + 3 - yellow 333 - 3 3 + 4 bob green 444 4 4 4 + 5 - blue 555 - 5 5 + 6 cindy - - 6 - - + 8 dave - - 8 - - + 9 - - 999 - - 9 + } + + do_execsql_test join9-$id.401 { + SELECT *, t4.id, t5.id, t6.id + FROM t4 NATURAL FULL JOIN t5 NATURAL FULL JOIN t6 + ORDER BY id; + } { + 0 - - 1000 - - 0 + 1 - red - - 1 - + 2 alice orange - 2 2 - + 3 - yellow 333 - 3 3 + 4 bob green 444 4 4 4 + 5 - blue 555 - 5 5 + 6 cindy - - 6 - - + 8 dave - - 8 - - + 9 - - 999 - - 9 + } + do_execsql_test join9-$id.402 { + SELECT id, x, y, z, t4.id, t5.id, t6.id + FROM t4 NATURAL FULL JOIN t6 NATURAL FULL JOIN t5 + ORDER BY id; + } { + 0 - - 1000 - - 0 + 1 - red - - 1 - + 2 alice orange - 2 2 - + 3 - yellow 333 - 3 3 + 4 bob green 444 4 4 4 + 5 - blue 555 - 5 5 + 6 cindy - - 6 - - + 8 dave - - 8 - - + 9 - - 999 - - 9 + } + do_execsql_test join9-$id.403 { + SELECT id, x, y, z, t4.id, t5.id, t6.id + FROM t5 NATURAL FULL JOIN t4 NATURAL FULL JOIN t6 + ORDER BY id; + } { + 0 - - 1000 - - 0 + 1 - red - - 1 - + 2 alice orange - 2 2 - + 3 - yellow 333 - 3 3 + 4 bob green 444 4 4 4 + 5 - blue 555 - 5 5 + 6 cindy - - 6 - - + 8 dave - - 8 - - + 9 - - 999 - - 9 + } + do_execsql_test join9-$id.404 { + SELECT id, x, y, z, t4.id, t5.id, t6.id + FROM t5 NATURAL FULL JOIN t6 NATURAL FULL JOIN t4 + ORDER BY id; + } { + 0 - - 1000 - - 0 + 1 - red - - 1 - + 2 alice orange - 2 2 - + 3 - yellow 333 - 3 3 + 4 bob green 444 4 4 4 + 5 - blue 555 - 5 5 + 6 cindy - - 6 - - + 8 dave - - 8 - - + 9 - - 999 - - 9 + } + do_execsql_test join9-$id.405 { + SELECT id, x, y, z, t4.id, t5.id, t6.id + FROM t6 NATURAL FULL JOIN t4 NATURAL FULL JOIN t5 + ORDER BY id; + } { + 0 - - 1000 - - 0 + 1 - red - - 1 - + 2 alice orange - 2 2 - + 3 - yellow 333 - 3 3 + 4 bob green 444 4 4 4 + 5 - blue 555 - 5 5 + 6 cindy - - 6 - - + 8 dave - - 8 - - + 9 - - 999 - - 9 + } + do_execsql_test join9-$id.406 { + SELECT id, x, y, z, t4.id, t5.id, t6.id + FROM t6 NATURAL FULL JOIN t5 NATURAL FULL JOIN t4 + ORDER BY id; + } { + 0 - - 1000 - - 0 + 1 - red - - 1 - + 2 alice orange - 2 2 - + 3 - yellow 333 - 3 3 + 4 bob green 444 4 4 4 + 5 - blue 555 - 5 5 + 6 cindy - - 6 - - + 8 dave - - 8 - - + 9 - - 999 - - 9 + } + + # Verified by PG-14 using case 1 + do_execsql_test join9-$id.500 { + SELECT id, w, x, y, z + FROM t3 FULL JOIN t4 USING(id) + NATURAL FULL JOIN t5 + FULL JOIN t6 USING(id) + ORDER BY 1; + } { + 0 - - - 1000 + 1 - - red - + 2 two alice orange - + 3 three - yellow 333 + 4 - bob green 444 + 5 - - blue 555 + 6 six cindy - - + 7 seven - - - + 8 - dave - - + 9 - - - 999 + } + + # Verified by PG-14 using case 1 + do_execsql_test join9-$id.600 { + SELECT id, w, x, y, z + FROM t3 JOIN dual AS d1 ON true + FULL JOIN t4 USING(id) + JOIN dual AS d2 ON true + NATURAL FULL JOIN t5 + JOIN dual AS d3 ON true + FULL JOIN t6 USING(id) + CROSS JOIN dual AS d4 + ORDER BY 1; + } { + 0 - - - 1000 + 1 - - red - + 2 two alice orange - + 3 three - yellow 333 + 4 - bob green 444 + 5 - - blue 555 + 6 six cindy - - + 7 seven - - - + 8 - dave - - + 9 - - - 999 + } + + # Verified by PG-14 using case 1 + do_execsql_test join9-$id.700 { + SELECT id, w, x, y, z + FROM t3 JOIN dual AS d1 ON true + FULL JOIN t4 USING(id) + JOIN dual AS d2 ON true + NATURAL FULL JOIN t5 + JOIN dual AS d3 ON true + FULL JOIN t6 USING(id) + CROSS JOIN dual AS d4 + WHERE x<>'bob' OR x IS NULL + ORDER BY 1; + } { + 0 - - - 1000 + 1 - - red - + 2 two alice orange - + 3 three - yellow 333 + 5 - - blue 555 + 6 six cindy - - + 7 seven - - - + 8 - dave - - + 9 - - - 999 + } + + # Verified by PG-14 using case 1 + do_execsql_test join9-$id.800 { + WITH t7(id,a) AS MATERIALIZED (SELECT * FROM t4 WHERE false) + SELECT * + FROM t7 + JOIN t7 AS t7b USING(id) + FULL JOIN t3 USING(id); + } { + 2 - - two + 3 - - three + 6 - - six + 7 - - seven + } + + # Verified by PG-14 + do_execsql_test join9-$id.900 { + SELECT * + FROM (t3 NATURAL FULL JOIN t4) + NATURAL FULL JOIN + (t5 NATURAL FULL JOIN t6) + ORDER BY 1; + } { + 0 - - - 1000 + 1 - - red - + 2 two alice orange - + 3 three - yellow 333 + 4 - bob green 444 + 5 - - blue 555 + 6 six cindy - - + 7 seven - - - + 8 - dave - - + 9 - - - 999 + } + do_execsql_test join9-$id.910 { + SELECT * + FROM t3 NATURAL FULL JOIN + (t4 NATURAL FULL JOIN + (t5 NATURAL FULL JOIN t6)) + ORDER BY 1; + } { + 0 - - - 1000 + 1 - - red - + 2 two alice orange - + 3 three - yellow 333 + 4 - bob green 444 + 5 - - blue 555 + 6 six cindy - - + 7 seven - - - + 8 - dave - - + 9 - - - 999 + } + do_execsql_test join9-$id.920 { + SELECT * + FROM t3 FULL JOIN ( + t4 FULL JOIN ( + t5 FULL JOIN t6 USING (id) + ) USING(id) + ) USING(id) + ORDER BY 1; + } { + 0 - - - 1000 + 1 - - red - + 2 two alice orange - + 3 three - yellow 333 + 4 - bob green 444 + 5 - - blue 555 + 6 six cindy - - + 7 seven - - - + 8 - dave - - + 9 - - - 999 + } + do_execsql_test join9-$id.920 { + SELECT * + FROM t3 FULL JOIN ( + t4 FULL JOIN ( + t5 FULL JOIN t6 USING (id) + ) USING(id) + ) USING(id) + ORDER BY 1; + } { + 0 - - - 1000 + 1 - - red - + 2 two alice orange - + 3 three - yellow 333 + 4 - bob green 444 + 5 - - blue 555 + 6 six cindy - - + 7 seven - - - + 8 - dave - - + 9 - - - 999 + } + + # Verified by PG-14 + do_execsql_test join9-$id.930 { + SELECT * + FROM t3 FULL JOIN ( + t4 FULL JOIN ( + t5 FULL JOIN t6 USING(id) + ) USING(id) + ) AS j1 ON j1.id=t3.id + ORDER BY coalesce(t3.id,j1.id); + } { + - - 0 - - 1000 + - - 1 - red - + 2 two 2 alice orange - + 3 three 3 - yellow 333 + - - 4 bob green 444 + - - 5 - blue 555 + 6 six 6 cindy - - + 7 seven - - - - + - - 8 dave - - + - - 9 - - 999 + } + + # Verified by PG-14 + do_execsql_test join9-$id.940 { + SELECT * + FROM t3 FULL JOIN ( + t4 RIGHT JOIN ( + t5 FULL JOIN t6 USING(id) + ) USING(id) + ) AS j1 ON j1.id=t3.id + ORDER BY coalesce(t3.id,j1.id); + } { + - - 0 - - 1000 + - - 1 - red - + 2 two 2 alice orange - + 3 three 3 - yellow 333 + - - 4 bob green 444 + - - 5 - blue 555 + 6 six - - - - + 7 seven - - - - + - - 9 - - 999 + } + + # Verified by PG-14 + do_execsql_test join9-$id.950 { + SELECT * + FROM t3 FULL JOIN ( + t4 LEFT JOIN ( + t5 FULL JOIN t6 USING(id) + ) USING(id) + ) AS j1 ON j1.id=t3.id + ORDER BY coalesce(t3.id,j1.id); + } { + 2 two 2 alice orange - + 3 three - - - - + - - 4 bob green 444 + 6 six 6 cindy - - + 7 seven - - - - + - - 8 dave - - + } + + # Restriction (27) in the query flattener + # Verified by PG-14 + do_execsql_test join9-$id.1000 { + WITH t56(id,y,z) AS (SELECT * FROM t5 FULL JOIN t6 USING(id) LIMIT 50) + SELECT id,x,y,z FROM t4 JOIN t56 USING(id) + ORDER BY 1; + } { + 2 alice orange - + 4 bob green 444 + } + + # Verified by PG-14 + do_execsql_test join9-$id.1010 { + SELECT id,x,y,z + FROM t4 INNER JOIN (t5 FULL JOIN t6 USING(id)) USING(id) + ORDER BY 1; + } { + 2 alice orange - + 4 bob green 444 + } + + # Verified by PG-14 + do_execsql_test join9-$id.1020 { + SELECT id,x,y,z + FROM t4 FULL JOIN t5 USING(id) INNER JOIN t6 USING(id) + ORDER BY 1; + } { + 3 - yellow 333 + 4 bob green 444 + 5 - blue 555 + } + + # Verified by PG-14 + do_execsql_test join9-$id.1030 { + WITH t45(id,x,y) AS (SELECT * FROM t4 FULL JOIN t5 USING(id) LIMIT 50) + SELECT id,x,y,z FROM t45 JOIN t6 USING(id) + ORDER BY 1; + } { + 3 - yellow 333 + 4 bob green 444 + 5 - blue 555 + } + +} +finish_test diff --git a/test/joinA.test b/test/joinA.test new file mode 100644 index 0000000000..04d1e68cfd --- /dev/null +++ b/test/joinA.test @@ -0,0 +1,277 @@ +# 2022-04-18 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. +# +# This file implements tests for RIGHT and FULL OUTER JOINs. + +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +foreach {id schema} { + 1 { + CREATE TABLE t1(a INT, b INT, c INT, d INT); + CREATE TABLE t2(c INT, d INT, e INT, f INT); + CREATE TABLE t3(a INT, b INT, e INT, f INT); + CREATE TABLE t4(a INT, c INT, d INT, f INT); + INSERT INTO t1 VALUES(11,21,31,41),(12,22,32,42),(15,25,35,45),(18,28,38,48); + INSERT INTO t2 VALUES(12,22,32,42),(13,23,33,43),(15,25,35,45),(17,27,37,47); + INSERT INTO t3 VALUES(14,24,34,44),(15,25,35,45),(16,26,36,46); + INSERT INTO t4 VALUES(11,21,31,41),(13,23,33,43),(16,26,36,46),(19,29,39,49); + } + 2 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b INT, c INT, d INT); + CREATE TABLE t2(c INT, d INTEGER PRIMARY KEY, e INT, f INT); + CREATE TABLE t3(a INT, b INT, e INTEGER PRIMARY KEY, f INT); + CREATE TABLE t4(a INT, c INT, d INT, f INT PRIMARY KEY) WITHOUT ROWID; + INSERT INTO t1 VALUES(11,21,31,41),(12,22,32,42),(15,25,35,45),(18,28,38,48); + INSERT INTO t2 VALUES(12,22,32,42),(13,23,33,43),(15,25,35,45),(17,27,37,47); + INSERT INTO t3 VALUES(14,24,34,44),(15,25,35,45),(16,26,36,46); + INSERT INTO t4 VALUES(11,21,31,41),(13,23,33,43),(16,26,36,46),(19,29,39,49); + } + 3 { + CREATE TABLE t1a(a INT, b INT, c INT, d INT); + CREATE TABLE t2a(c INT, d INT, e INT, f INT); + CREATE TABLE t3a(a INT, b INT, e INT, f INT); + CREATE TABLE t4a(a INT, c INT, d INT, f INT); + INSERT INTO t1a VALUES(11,21,31,41),(12,22,32,42); + INSERT INTO t2a VALUES(12,22,32,42),(13,23,33,43); + INSERT INTO t3a VALUES(14,24,34,44),(15,25,35,45); + INSERT INTO t4a VALUES(11,21,31,41),(13,23,33,43); + CREATE TABLE t1b(a INT, b INT, c INT, d INT); + CREATE TABLE t2b(c INT, d INT, e INT, f INT); + CREATE TABLE t3b(a INT, b INT, e INT, f INT); + CREATE TABLE t4b(a INT, c INT, d INT, f INT); + INSERT INTO t1b VALUES(15,25,35,45),(18,28,38,48); + INSERT INTO t2b VALUES(15,25,35,45),(17,27,37,47); + INSERT INTO t3b VALUES(15,25,35,45),(16,26,36,46); + INSERT INTO t4b VALUES(16,26,36,46),(19,29,39,49); + CREATE VIEW t1 AS SELECT * FROM t1a UNION SELECT * FROM t1b; + CREATE VIEW t2 AS SELECT * FROM t2a UNION SELECT * FROM t2b; + CREATE VIEW t3 AS SELECT * FROM t3a UNION SELECT * FROM t3b; + CREATE VIEW t4 AS SELECT * FROM t4a UNION SELECT * FROM t4b; + } +} { + reset_db + db nullvalue - + do_execsql_test joinA-$id.setup $schema {} + + # Verified by PG-14 + do_execsql_test joinA-$id.100 { + SELECT a,b,c,d,t2.e,f,t3.e + FROM t1 + INNER JOIN t2 USING(c,d) + INNER JOIN t3 USING(a,b,f) + INNER JOIN t4 USING(a,c,d,f) + ORDER BY 1 nulls first, 3 nulls first; + } {} + + + # Verified by PG-14 + do_execsql_test joinA-$id.110 { + SELECT a,b,c,d,t2.e,f,t3.e + FROM t1 + LEFT JOIN t2 USING(c,d) + LEFT JOIN t3 USING(a,b,f) + LEFT JOIN t4 USING(a,c,d,f) + ORDER BY 1 nulls first, 3 nulls first; + } { + 11 21 31 41 - - - + 12 22 32 42 - - - + 15 25 35 45 - - - + 18 28 38 48 - - - + } + + # Verified by PG-14 + do_execsql_test joinA-$id.120 { + SELECT a,b,c,d,t2.e,f,t3.e + FROM t1 + LEFT JOIN t2 USING(c,d) + RIGHT JOIN t3 USING(a,b,f) + LEFT JOIN t4 USING(a,c,d,f) + ORDER BY 1 nulls first, 3 nulls first; + } { + 14 24 - - - 44 34 + 15 25 - - - 45 35 + 16 26 - - - 46 36 + } + + # Verified by PG-14 + do_execsql_test joinA-$id.130 { + SELECT a,b,c,d,t2.e,f,t3.e + FROM t1 + RIGHT JOIN t2 USING(c,d) + LEFT JOIN t3 USING(a,b,f) + RIGHT JOIN t4 USING(a,c,d,f) + ORDER BY 1 nulls first, 3 nulls first; + } { + 11 - 21 31 - 41 - + 13 - 23 33 - 43 - + 16 - 26 36 - 46 - + 19 - 29 39 - 49 - + } + + # Verified by PG-14 + do_execsql_test joinA-$id.140 { + SELECT a,b,c,d,t2.e,f,t3.e + FROM t1 + FULL JOIN t2 USING(c,d) + LEFT JOIN t3 USING(a,b,f) + RIGHT JOIN t4 USING(a,c,d,f) + ORDER BY 1 nulls first, 3 nulls first; + } { + 11 - 21 31 - 41 - + 13 - 23 33 - 43 - + 16 - 26 36 - 46 - + 19 - 29 39 - 49 - + } + + # Verified by PG-14 + do_execsql_test joinA-$id.150 { + SELECT a,b,c,d,t2.e,f,t3.e + FROM t1 + RIGHT JOIN t2 USING(c,d) + FULL JOIN t3 USING(a,b,f) + RIGHT JOIN t4 USING(a,c,d,f) + ORDER BY 1 nulls first, 3 nulls first; + } { + 11 - 21 31 - 41 - + 13 - 23 33 - 43 - + 16 - 26 36 - 46 - + 19 - 29 39 - 49 - + } + + # Verified by PG-14 + do_execsql_test joinA-$id.160 { + SELECT a,b,c,d,t2.e,f,t3.e + FROM t1 + RIGHT JOIN t2 USING(c,d) + LEFT JOIN t3 USING(a,b,f) + FULL JOIN t4 USING(a,c,d,f) + ORDER BY 1 nulls first, 3 nulls first; + } { + - - 12 22 32 42 - + - - 13 23 33 43 - + - - 15 25 35 45 - + - - 17 27 37 47 - + 11 - 21 31 - 41 - + 13 - 23 33 - 43 - + 16 - 26 36 - 46 - + 19 - 29 39 - 49 - + } + + # Verified by PG-14 + do_execsql_test joinA-$id.170 { + SELECT a,b,c,d,t2.e,f,t3.e + FROM t1 + LEFT JOIN t2 USING(c,d) + RIGHT JOIN t3 USING(a,b,f) + FULL JOIN t4 USING(a,c,d,f) + ORDER BY 1 nulls first, 3 nulls first; + } { + 11 - 21 31 - 41 - + 13 - 23 33 - 43 - + 14 24 - - - 44 34 + 15 25 - - - 45 35 + 16 26 - - - 46 36 + 16 - 26 36 - 46 - + 19 - 29 39 - 49 - + } + + # Verified by PG-14 + do_execsql_test joinA-$id.200 { + SELECT a,b,c,d,t2.e,f,t3.e + FROM t1 + FULL JOIN t2 USING(c,d) + FULL JOIN t3 USING(a,b,f) + FULL JOIN t4 USING(a,c,d,f) + ORDER BY 1 nulls first, 3 nulls first; + } { + - - 12 22 32 42 - + - - 13 23 33 43 - + - - 15 25 35 45 - + - - 17 27 37 47 - + 11 - 21 31 - 41 - + 11 21 31 41 - - - + 12 22 32 42 - - - + 13 - 23 33 - 43 - + 14 24 - - - 44 34 + 15 25 - - - 45 35 + 15 25 35 45 - - - + 16 26 - - - 46 36 + 16 - 26 36 - 46 - + 18 28 38 48 - - - + 19 - 29 39 - 49 - + } + + # Verified by PG-14 + do_execsql_test joinA-$id.201 { + SELECT a,b,c,d,t2.e,f,t3.e,t1.a + FROM t1 + FULL JOIN t2 USING(c,d) + FULL JOIN t3 USING(a,b,f) + FULL JOIN t4 USING(a,c,d,f) + WHERE t1.a!=0 + ORDER BY 1 nulls first, 3 nulls first; + } { + 11 21 31 41 - - - 11 + 12 22 32 42 - - - 12 + 15 25 35 45 - - - 15 + 18 28 38 48 - - - 18 + } + + # Verified by PG-14 + do_execsql_test joinA-$id.202 { + SELECT a,b,c,d,t2.e,f,t3.e,t3.a + FROM t1 + FULL JOIN t2 USING(c,d) + FULL JOIN t3 USING(a,b,f) + FULL JOIN t4 USING(a,c,d,f) + WHERE t3.a!=0 + ORDER BY 1 nulls first, 3 nulls first; + } { + 14 24 - - - 44 34 14 + 15 25 - - - 45 35 15 + 16 26 - - - 46 36 16 + } + + # Verified by PG-14 + do_execsql_test joinA-$id.203 { + SELECT a,b,c,d,t2.e,f,t3.e,t4.a + FROM t1 + FULL JOIN t2 USING(c,d) + FULL JOIN t3 USING(a,b,f) + FULL JOIN t4 USING(a,c,d,f) + WHERE t4.a!=0 + ORDER BY 1 nulls first, 3 nulls first; + } { + 11 - 21 31 - 41 - 11 + 13 - 23 33 - 43 - 13 + 16 - 26 36 - 46 - 16 + 19 - 29 39 - 49 - 19 + } + + # Verified by PG-14 + do_execsql_test joinA-$id.204 { + SELECT a,b,c,d,t2.e,f,t3.e + FROM t1 + FULL JOIN t2 USING(c,d) + FULL JOIN t3 USING(a,b,f) + FULL JOIN t4 USING(a,c,d,f) + WHERE t2.e!=0 + ORDER BY 1 nulls first, 3 nulls first; + } { + - - 12 22 32 42 - + - - 13 23 33 43 - + - - 15 25 35 45 - + - - 17 27 37 47 - + } +} +finish_test diff --git a/test/joinB.test b/test/joinB.test new file mode 100644 index 0000000000..baaeae677e --- /dev/null +++ b/test/joinB.test @@ -0,0 +1,7252 @@ +set testdir [file dirname $argv0] +# 2022-04-19 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# This file implements tests for JOINs. +# +# The test case output is all generated by PostgreSQL 14. This test module +# was created as follows: +# +# 1. Run a TCL script (included at the bottom of this file) that +# generates an input script for "psql" that will run man +# diverse tests on joins. +# +# 2. Run the script from step (1) through psql and collect the +# output. +# +# 3. Make a few minor global search-and-replace operations to convert +# the psql output into a form suitable for this test module. +# +# 4. Add this header, and the script content at the footer. +# +source $testdir/tester.tcl +db nullvalue - +db eval { + DROP TABLE IF EXISTS t1; + DROP TABLE IF EXISTS t2; + DROP TABLE IF EXISTS t3; + DROP TABLE IF EXISTS t4; + DROP TABLE IF EXISTS t5; + CREATE TABLE t1(a INT, b INT, c INT); + CREATE TABLE t2(a INT, b INT, d INT); + CREATE TABLE t3(a INT, b INT, e INT); + CREATE TABLE t4(a INT, b INT, f INT); + CREATE TABLE t5(a INT, b INT, g INT); + INSERT INTO t1 VALUES(11,21,31),(12,22,32),(15,25,35),(17,27,37); + INSERT INTO t2 VALUES(12,22,32),(13,23,33),(15,25,35),(18,28,38), + (NULL,NULL,36); + INSERT INTO t4 VALUES(11,21,31),(13,23,33),(15,25,35),(19,29,39); + INSERT INTO t3 SELECT * FROM t1 UNION SELECT * FROM t2 UNION SELECT * FROM t4; + INSERT INTO t5 SELECT * FROM t3 WHERE a>=15; +} +do_execsql_test joinB-1 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + INNER JOIN t3 USING(a) + INNER JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 +} +do_execsql_test joinB-2 { + SELECT a, c, d, e, f, g + FROM t1 + INNER JOIN t2 USING(a,b) + INNER JOIN t3 USING(a,b) + INNER JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 +} +do_execsql_test joinB-3 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + INNER JOIN t3 USING(a) + INNER JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 +} +do_execsql_test joinB-4 { + SELECT a, c, d, e, f, g + FROM t1 + INNER JOIN (t2 INNER JOIN t3 USING(a)) USING(a) + INNER JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 +} +do_execsql_test joinB-5 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + INNER JOIN t3 USING(a) + INNER JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - - - 19 +} +do_execsql_test joinB-6 { + SELECT a, b, c, d, e, f, g + FROM t1 + INNER JOIN t2 USING(a,b) + INNER JOIN t3 USING(a,b) + INNER JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 - - - - 37 + 18 28 - - - - 38 + 19 29 - - - - 39 +} +do_execsql_test joinB-7 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + INNER JOIN t3 USING(a) + INNER JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - - - 19 +} +do_execsql_test joinB-8 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL INNER JOIN t2 + NATURAL INNER JOIN t3 + NATURAL INNER JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-9 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + INNER JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 +} +do_execsql_test joinB-10 { + SELECT a, c, d, e, f, g + FROM t1 + INNER JOIN t2 USING(a,b) + INNER JOIN t3 USING(a,b) + LEFT JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 +} +do_execsql_test joinB-11 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + INNER JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 12 12 12 12 - - + 15 15 15 15 15 15 +} +do_execsql_test joinB-12 { + SELECT a, c, d, e, f, g + FROM t1 + INNER JOIN (t2 INNER JOIN t3 USING(a)) USING(a) + LEFT JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 12 32 32 32 - - + 15 35 35 35 35 35 +} +do_execsql_test joinB-13 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + INNER JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - - - 19 +} +do_execsql_test joinB-14 { + SELECT a, b, c, d, e, f, g + FROM t1 + INNER JOIN t2 USING(a,b) + INNER JOIN t3 USING(a,b) + LEFT JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 - - - - 37 + 18 28 - - - - 38 + 19 29 - - - - 39 +} +do_execsql_test joinB-15 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + INNER JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - - - 19 +} +do_execsql_test joinB-16 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL INNER JOIN t2 + NATURAL INNER JOIN t3 + NATURAL LEFT JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-17 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + INNER JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 19 - - - 19 19 +} +do_execsql_test joinB-18 { + SELECT a, c, d, e, f, g + FROM t1 + INNER JOIN t2 USING(a,b) + INNER JOIN t3 USING(a,b) + RIGHT JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 19 - - - 39 39 +} +do_execsql_test joinB-19 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + INNER JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 - - - 11 - + 13 - - - 13 - + 15 15 15 15 15 15 + 19 - - - 19 19 +} +do_execsql_test joinB-20 { + SELECT a, c, d, e, f, g + FROM t1 + INNER JOIN (t2 INNER JOIN t3 USING(a)) USING(a) + RIGHT JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 - - - 31 - + 13 - - - 33 - + 15 35 35 35 35 35 +} +do_execsql_test joinB-21 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + INNER JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - - 19 19 +} +do_execsql_test joinB-22 { + SELECT a, b, c, d, e, f, g + FROM t1 + INNER JOIN t2 USING(a,b) + INNER JOIN t3 USING(a,b) + RIGHT JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 - - - - 37 + 18 28 - - - - 38 + 19 29 - - - 39 39 +} +do_execsql_test joinB-23 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + INNER JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 - - - 11 - + 13 - - - 13 - + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - - 19 19 +} +do_execsql_test joinB-24 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL INNER JOIN t2 + NATURAL INNER JOIN t3 + NATURAL RIGHT JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-25 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + INNER JOIN t3 USING(a) + FULL JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 19 - - - 19 19 +} +do_execsql_test joinB-26 { + SELECT a, c, d, e, f, g + FROM t1 + INNER JOIN t2 USING(a,b) + INNER JOIN t3 USING(a,b) + FULL JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 19 - - - 39 39 +} +do_execsql_test joinB-27 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + INNER JOIN t3 USING(a) + FULL JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 - - - 11 - + 12 12 12 12 - - + 13 - - - 13 - + 15 15 15 15 15 15 + 19 - - - 19 19 +} +do_execsql_test joinB-28 { + SELECT a, c, d, e, f, g + FROM t1 + INNER JOIN (t2 INNER JOIN t3 USING(a)) USING(a) + FULL JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 - - - 31 - + 12 32 32 32 - - + 13 - - - 33 - + 15 35 35 35 35 35 +} +do_execsql_test joinB-29 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + INNER JOIN t3 USING(a) + FULL JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - - 19 19 +} +do_execsql_test joinB-30 { + SELECT a, b, c, d, e, f, g + FROM t1 + INNER JOIN t2 USING(a,b) + INNER JOIN t3 USING(a,b) + FULL JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 - - - - 37 + 18 28 - - - - 38 + 19 29 - - - 39 39 +} +do_execsql_test joinB-31 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + INNER JOIN t3 USING(a) + FULL JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 - - - 11 - + 12 12 12 12 - - + 13 - - - 13 - + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - - 19 19 +} +do_execsql_test joinB-32 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL INNER JOIN t2 + NATURAL INNER JOIN t3 + NATURAL FULL JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-33 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + INNER JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 +} +do_execsql_test joinB-34 { + SELECT a, c, d, e, f, g + FROM t1 + INNER JOIN t2 USING(a,b) + LEFT JOIN t3 USING(a,b) + INNER JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 +} +do_execsql_test joinB-35 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + INNER JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 +} +do_execsql_test joinB-36 { + SELECT a, c, d, e, f, g + FROM t1 + INNER JOIN (t2 LEFT JOIN t3 USING(a)) USING(a) + INNER JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 +} +do_execsql_test joinB-37 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + INNER JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - - - 19 +} +do_execsql_test joinB-38 { + SELECT a, b, c, d, e, f, g + FROM t1 + INNER JOIN t2 USING(a,b) + LEFT JOIN t3 USING(a,b) + INNER JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 - - - - 37 + 18 28 - - - - 38 + 19 29 - - - - 39 +} +do_execsql_test joinB-39 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + INNER JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - - - 19 +} +do_execsql_test joinB-40 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL INNER JOIN t2 + NATURAL LEFT JOIN t3 + NATURAL INNER JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-41 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 +} +do_execsql_test joinB-42 { + SELECT a, c, d, e, f, g + FROM t1 + INNER JOIN t2 USING(a,b) + LEFT JOIN t3 USING(a,b) + LEFT JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 +} +do_execsql_test joinB-43 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 12 12 12 12 - - + 15 15 15 15 15 15 +} +do_execsql_test joinB-44 { + SELECT a, c, d, e, f, g + FROM t1 + INNER JOIN (t2 LEFT JOIN t3 USING(a)) USING(a) + LEFT JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 12 32 32 32 - - + 15 35 35 35 35 35 +} +do_execsql_test joinB-45 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - - - 19 +} +do_execsql_test joinB-46 { + SELECT a, b, c, d, e, f, g + FROM t1 + INNER JOIN t2 USING(a,b) + LEFT JOIN t3 USING(a,b) + LEFT JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 - - - - 37 + 18 28 - - - - 38 + 19 29 - - - - 39 +} +do_execsql_test joinB-47 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - - - 19 +} +do_execsql_test joinB-48 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL INNER JOIN t2 + NATURAL LEFT JOIN t3 + NATURAL LEFT JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-49 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 19 - - - 19 19 +} +do_execsql_test joinB-50 { + SELECT a, c, d, e, f, g + FROM t1 + INNER JOIN t2 USING(a,b) + LEFT JOIN t3 USING(a,b) + RIGHT JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 19 - - - 39 39 +} +do_execsql_test joinB-51 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 - - - 11 - + 13 - - - 13 - + 15 15 15 15 15 15 + 19 - - - 19 19 +} +do_execsql_test joinB-52 { + SELECT a, c, d, e, f, g + FROM t1 + INNER JOIN (t2 LEFT JOIN t3 USING(a)) USING(a) + RIGHT JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 - - - 31 - + 13 - - - 33 - + 15 35 35 35 35 35 +} +do_execsql_test joinB-53 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - - 19 19 +} +do_execsql_test joinB-54 { + SELECT a, b, c, d, e, f, g + FROM t1 + INNER JOIN t2 USING(a,b) + LEFT JOIN t3 USING(a,b) + RIGHT JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 - - - - 37 + 18 28 - - - - 38 + 19 29 - - - 39 39 +} +do_execsql_test joinB-55 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 - - - 11 - + 13 - - - 13 - + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - - 19 19 +} +do_execsql_test joinB-56 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL INNER JOIN t2 + NATURAL LEFT JOIN t3 + NATURAL RIGHT JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-57 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + FULL JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 19 - - - 19 19 +} +do_execsql_test joinB-58 { + SELECT a, c, d, e, f, g + FROM t1 + INNER JOIN t2 USING(a,b) + LEFT JOIN t3 USING(a,b) + FULL JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 19 - - - 39 39 +} +do_execsql_test joinB-59 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + FULL JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 - - - 11 - + 12 12 12 12 - - + 13 - - - 13 - + 15 15 15 15 15 15 + 19 - - - 19 19 +} +do_execsql_test joinB-60 { + SELECT a, c, d, e, f, g + FROM t1 + INNER JOIN (t2 LEFT JOIN t3 USING(a)) USING(a) + FULL JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 - - - 31 - + 12 32 32 32 - - + 13 - - - 33 - + 15 35 35 35 35 35 +} +do_execsql_test joinB-61 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + FULL JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - - 19 19 +} +do_execsql_test joinB-62 { + SELECT a, b, c, d, e, f, g + FROM t1 + INNER JOIN t2 USING(a,b) + LEFT JOIN t3 USING(a,b) + FULL JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 - - - - 37 + 18 28 - - - - 38 + 19 29 - - - 39 39 +} +do_execsql_test joinB-63 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + FULL JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 - - - 11 - + 12 12 12 12 - - + 13 - - - 13 - + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - - 19 19 +} +do_execsql_test joinB-64 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL INNER JOIN t2 + NATURAL LEFT JOIN t3 + NATURAL FULL JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-65 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + INNER JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 19 - - 19 19 19 +} +do_execsql_test joinB-66 { + SELECT a, c, d, e, f, g + FROM t1 + INNER JOIN t2 USING(a,b) + RIGHT JOIN t3 USING(a,b) + INNER JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 19 - - 39 39 39 +} +do_execsql_test joinB-67 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + INNER JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 - - 11 11 - + 13 - - 13 13 - + 15 15 15 15 15 15 + 19 - - 19 19 19 +} +do_execsql_test joinB-68 { + SELECT a, c, d, e, f, g + FROM t1 + INNER JOIN (t2 RIGHT JOIN t3 USING(a)) USING(a) + INNER JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 31 - 31 31 - + 15 35 35 35 35 35 +} +do_execsql_test joinB-69 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + INNER JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-70 { + SELECT a, b, c, d, e, f, g + FROM t1 + INNER JOIN t2 USING(a,b) + RIGHT JOIN t3 USING(a,b) + INNER JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 - - - - 37 + 18 28 - - - - 38 + 19 29 - - 39 39 39 +} +do_execsql_test joinB-71 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + INNER JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 - - 11 11 - + 13 - - 13 13 - + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-72 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL INNER JOIN t2 + NATURAL RIGHT JOIN t3 + NATURAL INNER JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-73 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - 17 - 17 + 18 - - 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-74 { + SELECT a, c, d, e, f, g + FROM t1 + INNER JOIN t2 USING(a,b) + RIGHT JOIN t3 USING(a,b) + LEFT JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 17 - - 37 - 37 + 18 - - 38 - 38 + 19 - - 39 39 39 +} +do_execsql_test joinB-75 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 - - 11 11 - + 12 12 12 12 - - + 13 - - 13 13 - + 15 15 15 15 15 15 + 17 - - 17 - 17 + 18 - - 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-76 { + SELECT a, c, d, e, f, g + FROM t1 + INNER JOIN (t2 RIGHT JOIN t3 USING(a)) USING(a) + LEFT JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 31 - 31 31 - + 12 32 32 32 - - + 15 35 35 35 35 35 + 17 37 - 37 - - +} +do_execsql_test joinB-77 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - 17 - 17 + 18 - - 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-78 { + SELECT a, b, c, d, e, f, g + FROM t1 + INNER JOIN t2 USING(a,b) + RIGHT JOIN t3 USING(a,b) + LEFT JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 - - 37 - 37 + 18 28 - - 38 - 38 + 19 29 - - 39 39 39 +} +do_execsql_test joinB-79 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 - - 11 11 - + 12 12 12 12 - - + 13 - - 13 13 - + 15 15 15 15 15 15 + 17 - - 17 - 17 + 18 - - 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-80 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL INNER JOIN t2 + NATURAL RIGHT JOIN t3 + NATURAL LEFT JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-81 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 19 - - 19 19 19 +} +do_execsql_test joinB-82 { + SELECT a, c, d, e, f, g + FROM t1 + INNER JOIN t2 USING(a,b) + RIGHT JOIN t3 USING(a,b) + RIGHT JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 19 - - 39 39 39 +} +do_execsql_test joinB-83 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 - - 11 11 - + 13 - - 13 13 - + 15 15 15 15 15 15 + 19 - - 19 19 19 +} +do_execsql_test joinB-84 { + SELECT a, c, d, e, f, g + FROM t1 + INNER JOIN (t2 RIGHT JOIN t3 USING(a)) USING(a) + RIGHT JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 31 - 31 31 - + 13 - - - 33 - + 15 35 35 35 35 35 +} +do_execsql_test joinB-85 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-86 { + SELECT a, b, c, d, e, f, g + FROM t1 + INNER JOIN t2 USING(a,b) + RIGHT JOIN t3 USING(a,b) + RIGHT JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 - - - - 37 + 18 28 - - - - 38 + 19 29 - - 39 39 39 +} +do_execsql_test joinB-87 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 - - 11 11 - + 13 - - 13 13 - + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-88 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL INNER JOIN t2 + NATURAL RIGHT JOIN t3 + NATURAL RIGHT JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-89 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + FULL JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - 17 - 17 + 18 - - 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-90 { + SELECT a, c, d, e, f, g + FROM t1 + INNER JOIN t2 USING(a,b) + RIGHT JOIN t3 USING(a,b) + FULL JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 17 - - 37 - 37 + 18 - - 38 - 38 + 19 - - 39 39 39 +} +do_execsql_test joinB-91 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + FULL JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 - - 11 11 - + 12 12 12 12 - - + 13 - - 13 13 - + 15 15 15 15 15 15 + 17 - - 17 - 17 + 18 - - 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-92 { + SELECT a, c, d, e, f, g + FROM t1 + INNER JOIN (t2 RIGHT JOIN t3 USING(a)) USING(a) + FULL JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 31 - 31 31 - + 12 32 32 32 - - + 13 - - - 33 - + 15 35 35 35 35 35 + 17 37 - 37 - - +} +do_execsql_test joinB-93 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + FULL JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - 17 - 17 + 18 - - 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-94 { + SELECT a, b, c, d, e, f, g + FROM t1 + INNER JOIN t2 USING(a,b) + RIGHT JOIN t3 USING(a,b) + FULL JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 - - 37 - 37 + 18 28 - - 38 - 38 + 19 29 - - 39 39 39 +} +do_execsql_test joinB-95 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + FULL JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 - - 11 11 - + 12 12 12 12 - - + 13 - - 13 13 - + 15 15 15 15 15 15 + 17 - - 17 - 17 + 18 - - 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-96 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL INNER JOIN t2 + NATURAL RIGHT JOIN t3 + NATURAL FULL JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-97 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + FULL JOIN t3 USING(a) + INNER JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 19 - - 19 19 19 +} +do_execsql_test joinB-98 { + SELECT a, c, d, e, f, g + FROM t1 + INNER JOIN t2 USING(a,b) + FULL JOIN t3 USING(a,b) + INNER JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 19 - - 39 39 39 +} +do_execsql_test joinB-99 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + FULL JOIN t3 USING(a) + INNER JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 - - 11 11 - + 13 - - 13 13 - + 15 15 15 15 15 15 + 19 - - 19 19 19 +} +do_execsql_test joinB-100 { + SELECT a, c, d, e, f, g + FROM t1 + INNER JOIN (t2 FULL JOIN t3 USING(a)) USING(a) + INNER JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 31 - 31 31 - + 15 35 35 35 35 35 +} +do_execsql_test joinB-101 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + FULL JOIN t3 USING(a) + INNER JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-102 { + SELECT a, b, c, d, e, f, g + FROM t1 + INNER JOIN t2 USING(a,b) + FULL JOIN t3 USING(a,b) + INNER JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 - - - - 37 + 18 28 - - - - 38 + 19 29 - - 39 39 39 +} +do_execsql_test joinB-103 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + FULL JOIN t3 USING(a) + INNER JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 - - 11 11 - + 13 - - 13 13 - + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-104 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL INNER JOIN t2 + NATURAL FULL JOIN t3 + NATURAL INNER JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-105 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + FULL JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - 17 - 17 + 18 - - 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-106 { + SELECT a, c, d, e, f, g + FROM t1 + INNER JOIN t2 USING(a,b) + FULL JOIN t3 USING(a,b) + LEFT JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 17 - - 37 - 37 + 18 - - 38 - 38 + 19 - - 39 39 39 +} +do_execsql_test joinB-107 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + FULL JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 - - 11 11 - + 12 12 12 12 - - + 13 - - 13 13 - + 15 15 15 15 15 15 + 17 - - 17 - 17 + 18 - - 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-108 { + SELECT a, c, d, e, f, g + FROM t1 + INNER JOIN (t2 FULL JOIN t3 USING(a)) USING(a) + LEFT JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 31 - 31 31 - + 12 32 32 32 - - + 15 35 35 35 35 35 + 17 37 - 37 - - +} +do_execsql_test joinB-109 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + FULL JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - 17 - 17 + 18 - - 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-110 { + SELECT a, b, c, d, e, f, g + FROM t1 + INNER JOIN t2 USING(a,b) + FULL JOIN t3 USING(a,b) + LEFT JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 - - 37 - 37 + 18 28 - - 38 - 38 + 19 29 - - 39 39 39 +} +do_execsql_test joinB-111 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + FULL JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 - - 11 11 - + 12 12 12 12 - - + 13 - - 13 13 - + 15 15 15 15 15 15 + 17 - - 17 - 17 + 18 - - 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-112 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL INNER JOIN t2 + NATURAL FULL JOIN t3 + NATURAL LEFT JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-113 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + FULL JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 19 - - 19 19 19 +} +do_execsql_test joinB-114 { + SELECT a, c, d, e, f, g + FROM t1 + INNER JOIN t2 USING(a,b) + FULL JOIN t3 USING(a,b) + RIGHT JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 19 - - 39 39 39 +} +do_execsql_test joinB-115 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + FULL JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 - - 11 11 - + 13 - - 13 13 - + 15 15 15 15 15 15 + 19 - - 19 19 19 +} +do_execsql_test joinB-116 { + SELECT a, c, d, e, f, g + FROM t1 + INNER JOIN (t2 FULL JOIN t3 USING(a)) USING(a) + RIGHT JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 31 - 31 31 - + 13 - - - 33 - + 15 35 35 35 35 35 +} +do_execsql_test joinB-117 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + FULL JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-118 { + SELECT a, b, c, d, e, f, g + FROM t1 + INNER JOIN t2 USING(a,b) + FULL JOIN t3 USING(a,b) + RIGHT JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 - - - - 37 + 18 28 - - - - 38 + 19 29 - - 39 39 39 +} +do_execsql_test joinB-119 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + FULL JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 - - 11 11 - + 13 - - 13 13 - + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-120 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL INNER JOIN t2 + NATURAL FULL JOIN t3 + NATURAL RIGHT JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-121 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + FULL JOIN t3 USING(a) + FULL JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - 17 - 17 + 18 - - 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-122 { + SELECT a, c, d, e, f, g + FROM t1 + INNER JOIN t2 USING(a,b) + FULL JOIN t3 USING(a,b) + FULL JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 17 - - 37 - 37 + 18 - - 38 - 38 + 19 - - 39 39 39 +} +do_execsql_test joinB-123 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + FULL JOIN t3 USING(a) + FULL JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 - - 11 11 - + 12 12 12 12 - - + 13 - - 13 13 - + 15 15 15 15 15 15 + 17 - - 17 - 17 + 18 - - 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-124 { + SELECT a, c, d, e, f, g + FROM t1 + INNER JOIN (t2 FULL JOIN t3 USING(a)) USING(a) + FULL JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 31 - 31 31 - + 12 32 32 32 - - + 13 - - - 33 - + 15 35 35 35 35 35 + 17 37 - 37 - - +} +do_execsql_test joinB-125 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + FULL JOIN t3 USING(a) + FULL JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - 17 - 17 + 18 - - 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-126 { + SELECT a, b, c, d, e, f, g + FROM t1 + INNER JOIN t2 USING(a,b) + FULL JOIN t3 USING(a,b) + FULL JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 - - 37 - 37 + 18 28 - - 38 - 38 + 19 29 - - 39 39 39 +} +do_execsql_test joinB-127 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + INNER JOIN t2 USING(a) + FULL JOIN t3 USING(a) + FULL JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 - - 11 11 - + 12 12 12 12 - - + 13 - - 13 13 - + 15 15 15 15 15 15 + 17 - - 17 - 17 + 18 - - 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-128 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL INNER JOIN t2 + NATURAL FULL JOIN t3 + NATURAL FULL JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-129 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + INNER JOIN t3 USING(a) + INNER JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 +} +do_execsql_test joinB-130 { + SELECT a, c, d, e, f, g + FROM t1 + LEFT JOIN t2 USING(a,b) + INNER JOIN t3 USING(a,b) + INNER JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 +} +do_execsql_test joinB-131 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + INNER JOIN t3 USING(a) + INNER JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 15 15 15 15 15 15 +} +do_execsql_test joinB-132 { + SELECT a, c, d, e, f, g + FROM t1 + LEFT JOIN (t2 INNER JOIN t3 USING(a)) USING(a) + INNER JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 31 - - 31 - + 15 35 35 35 35 35 +} +do_execsql_test joinB-133 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + INNER JOIN t3 USING(a) + INNER JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - - - 19 +} +do_execsql_test joinB-134 { + SELECT a, b, c, d, e, f, g + FROM t1 + LEFT JOIN t2 USING(a,b) + INNER JOIN t3 USING(a,b) + INNER JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 - - - - 37 + 18 28 - - - - 38 + 19 29 - - - - 39 +} +do_execsql_test joinB-135 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + INNER JOIN t3 USING(a) + INNER JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - - - 19 +} +do_execsql_test joinB-136 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL LEFT JOIN t2 + NATURAL INNER JOIN t3 + NATURAL INNER JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-137 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + INNER JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 17 - 17 - 17 +} +do_execsql_test joinB-138 { + SELECT a, c, d, e, f, g + FROM t1 + LEFT JOIN t2 USING(a,b) + INNER JOIN t3 USING(a,b) + LEFT JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 17 37 - 37 - 37 +} +do_execsql_test joinB-139 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + INNER JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 17 - 17 - 17 +} +do_execsql_test joinB-140 { + SELECT a, c, d, e, f, g + FROM t1 + LEFT JOIN (t2 INNER JOIN t3 USING(a)) USING(a) + LEFT JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 31 - - 31 - + 12 32 32 32 - - + 15 35 35 35 35 35 + 17 37 - - - - +} +do_execsql_test joinB-141 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + INNER JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - - - - 18 + 19 - - - - 19 +} +do_execsql_test joinB-142 { + SELECT a, b, c, d, e, f, g + FROM t1 + LEFT JOIN t2 USING(a,b) + INNER JOIN t3 USING(a,b) + LEFT JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 37 - 37 - 37 + 18 28 - - - - 38 + 19 29 - - - - 39 +} +do_execsql_test joinB-143 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + INNER JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - - - - 18 + 19 - - - - 19 +} +do_execsql_test joinB-144 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL LEFT JOIN t2 + NATURAL INNER JOIN t3 + NATURAL LEFT JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-145 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + INNER JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 19 - - - 19 19 +} +do_execsql_test joinB-146 { + SELECT a, c, d, e, f, g + FROM t1 + LEFT JOIN t2 USING(a,b) + INNER JOIN t3 USING(a,b) + RIGHT JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 19 - - - 39 39 +} +do_execsql_test joinB-147 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + INNER JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 13 - - - 13 - + 15 15 15 15 15 15 + 19 - - - 19 19 +} +do_execsql_test joinB-148 { + SELECT a, c, d, e, f, g + FROM t1 + LEFT JOIN (t2 INNER JOIN t3 USING(a)) USING(a) + RIGHT JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 31 - - 31 - + 13 - - - 33 - + 15 35 35 35 35 35 +} +do_execsql_test joinB-149 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + INNER JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - - 19 19 +} +do_execsql_test joinB-150 { + SELECT a, b, c, d, e, f, g + FROM t1 + LEFT JOIN t2 USING(a,b) + INNER JOIN t3 USING(a,b) + RIGHT JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 - - - - 37 + 18 28 - - - - 38 + 19 29 - - - 39 39 +} +do_execsql_test joinB-151 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + INNER JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 13 - - - 13 - + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - - 19 19 +} +do_execsql_test joinB-152 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL LEFT JOIN t2 + NATURAL INNER JOIN t3 + NATURAL RIGHT JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-153 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + INNER JOIN t3 USING(a) + FULL JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 19 - - - 19 19 +} +do_execsql_test joinB-154 { + SELECT a, c, d, e, f, g + FROM t1 + LEFT JOIN t2 USING(a,b) + INNER JOIN t3 USING(a,b) + FULL JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 17 37 - 37 - 37 + 19 - - - 39 39 +} +do_execsql_test joinB-155 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + INNER JOIN t3 USING(a) + FULL JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 12 12 12 12 - - + 13 - - - 13 - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 19 - - - 19 19 +} +do_execsql_test joinB-156 { + SELECT a, c, d, e, f, g + FROM t1 + LEFT JOIN (t2 INNER JOIN t3 USING(a)) USING(a) + FULL JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 31 - - 31 - + 12 32 32 32 - - + 13 - - - 33 - + 15 35 35 35 35 35 + 17 37 - - - - +} +do_execsql_test joinB-157 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + INNER JOIN t3 USING(a) + FULL JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - - - - 18 + 19 - - - 19 19 +} +do_execsql_test joinB-158 { + SELECT a, b, c, d, e, f, g + FROM t1 + LEFT JOIN t2 USING(a,b) + INNER JOIN t3 USING(a,b) + FULL JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 37 - 37 - 37 + 18 28 - - - - 38 + 19 29 - - - 39 39 +} +do_execsql_test joinB-159 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + INNER JOIN t3 USING(a) + FULL JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 12 12 12 12 - - + 13 - - - 13 - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - - - - 18 + 19 - - - 19 19 +} +do_execsql_test joinB-160 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL LEFT JOIN t2 + NATURAL INNER JOIN t3 + NATURAL FULL JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-161 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + INNER JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 +} +do_execsql_test joinB-162 { + SELECT a, c, d, e, f, g + FROM t1 + LEFT JOIN t2 USING(a,b) + LEFT JOIN t3 USING(a,b) + INNER JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 +} +do_execsql_test joinB-163 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + INNER JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 15 15 15 15 15 15 +} +do_execsql_test joinB-164 { + SELECT a, c, d, e, f, g + FROM t1 + LEFT JOIN (t2 LEFT JOIN t3 USING(a)) USING(a) + INNER JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 31 - - 31 - + 15 35 35 35 35 35 +} +do_execsql_test joinB-165 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + INNER JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - - - 19 +} +do_execsql_test joinB-166 { + SELECT a, b, c, d, e, f, g + FROM t1 + LEFT JOIN t2 USING(a,b) + LEFT JOIN t3 USING(a,b) + INNER JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 - - - - 37 + 18 28 - - - - 38 + 19 29 - - - - 39 +} +do_execsql_test joinB-167 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + INNER JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - - - 19 +} +do_execsql_test joinB-168 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL LEFT JOIN t2 + NATURAL LEFT JOIN t3 + NATURAL INNER JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-169 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 17 - 17 - 17 +} +do_execsql_test joinB-170 { + SELECT a, c, d, e, f, g + FROM t1 + LEFT JOIN t2 USING(a,b) + LEFT JOIN t3 USING(a,b) + LEFT JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 17 37 - 37 - 37 +} +do_execsql_test joinB-171 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 17 - 17 - 17 +} +do_execsql_test joinB-172 { + SELECT a, c, d, e, f, g + FROM t1 + LEFT JOIN (t2 LEFT JOIN t3 USING(a)) USING(a) + LEFT JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 31 - - 31 - + 12 32 32 32 - - + 15 35 35 35 35 35 + 17 37 - - - - +} +do_execsql_test joinB-173 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - - - - 18 + 19 - - - - 19 +} +do_execsql_test joinB-174 { + SELECT a, b, c, d, e, f, g + FROM t1 + LEFT JOIN t2 USING(a,b) + LEFT JOIN t3 USING(a,b) + LEFT JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 37 - 37 - 37 + 18 28 - - - - 38 + 19 29 - - - - 39 +} +do_execsql_test joinB-175 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - - - - 18 + 19 - - - - 19 +} +do_execsql_test joinB-176 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL LEFT JOIN t2 + NATURAL LEFT JOIN t3 + NATURAL LEFT JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-177 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 19 - - - 19 19 +} +do_execsql_test joinB-178 { + SELECT a, c, d, e, f, g + FROM t1 + LEFT JOIN t2 USING(a,b) + LEFT JOIN t3 USING(a,b) + RIGHT JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 19 - - - 39 39 +} +do_execsql_test joinB-179 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 13 - - - 13 - + 15 15 15 15 15 15 + 19 - - - 19 19 +} +do_execsql_test joinB-180 { + SELECT a, c, d, e, f, g + FROM t1 + LEFT JOIN (t2 LEFT JOIN t3 USING(a)) USING(a) + RIGHT JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 31 - - 31 - + 13 - - - 33 - + 15 35 35 35 35 35 +} +do_execsql_test joinB-181 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - - 19 19 +} +do_execsql_test joinB-182 { + SELECT a, b, c, d, e, f, g + FROM t1 + LEFT JOIN t2 USING(a,b) + LEFT JOIN t3 USING(a,b) + RIGHT JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 - - - - 37 + 18 28 - - - - 38 + 19 29 - - - 39 39 +} +do_execsql_test joinB-183 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 13 - - - 13 - + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - - 19 19 +} +do_execsql_test joinB-184 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL LEFT JOIN t2 + NATURAL LEFT JOIN t3 + NATURAL RIGHT JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-185 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + FULL JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 19 - - - 19 19 +} +do_execsql_test joinB-186 { + SELECT a, c, d, e, f, g + FROM t1 + LEFT JOIN t2 USING(a,b) + LEFT JOIN t3 USING(a,b) + FULL JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 17 37 - 37 - 37 + 19 - - - 39 39 +} +do_execsql_test joinB-187 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + FULL JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 12 12 12 12 - - + 13 - - - 13 - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 19 - - - 19 19 +} +do_execsql_test joinB-188 { + SELECT a, c, d, e, f, g + FROM t1 + LEFT JOIN (t2 LEFT JOIN t3 USING(a)) USING(a) + FULL JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 31 - - 31 - + 12 32 32 32 - - + 13 - - - 33 - + 15 35 35 35 35 35 + 17 37 - - - - +} +do_execsql_test joinB-189 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + FULL JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - - - - 18 + 19 - - - 19 19 +} +do_execsql_test joinB-190 { + SELECT a, b, c, d, e, f, g + FROM t1 + LEFT JOIN t2 USING(a,b) + LEFT JOIN t3 USING(a,b) + FULL JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 37 - 37 - 37 + 18 28 - - - - 38 + 19 29 - - - 39 39 +} +do_execsql_test joinB-191 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + FULL JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 12 12 12 12 - - + 13 - - - 13 - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - - - - 18 + 19 - - - 19 19 +} +do_execsql_test joinB-192 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL LEFT JOIN t2 + NATURAL LEFT JOIN t3 + NATURAL FULL JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-193 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + INNER JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 19 - - 19 19 19 +} +do_execsql_test joinB-194 { + SELECT a, c, d, e, f, g + FROM t1 + LEFT JOIN t2 USING(a,b) + RIGHT JOIN t3 USING(a,b) + INNER JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 19 - - 39 39 39 +} +do_execsql_test joinB-195 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + INNER JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 13 - - 13 13 - + 15 15 15 15 15 15 + 19 - - 19 19 19 +} +do_execsql_test joinB-196 { + SELECT a, c, d, e, f, g + FROM t1 + LEFT JOIN (t2 RIGHT JOIN t3 USING(a)) USING(a) + INNER JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 31 - 31 31 - + 15 35 35 35 35 35 +} +do_execsql_test joinB-197 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + INNER JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-198 { + SELECT a, b, c, d, e, f, g + FROM t1 + LEFT JOIN t2 USING(a,b) + RIGHT JOIN t3 USING(a,b) + INNER JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 - - - - 37 + 18 28 - - - - 38 + 19 29 - - 39 39 39 +} +do_execsql_test joinB-199 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + INNER JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 13 - - 13 13 - + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-200 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL LEFT JOIN t2 + NATURAL RIGHT JOIN t3 + NATURAL INNER JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-201 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - - 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-202 { + SELECT a, c, d, e, f, g + FROM t1 + LEFT JOIN t2 USING(a,b) + RIGHT JOIN t3 USING(a,b) + LEFT JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 17 37 - 37 - 37 + 18 - - 38 - 38 + 19 - - 39 39 39 +} +do_execsql_test joinB-203 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - 11 11 - + 12 12 12 12 - - + 13 - - 13 13 - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - - 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-204 { + SELECT a, c, d, e, f, g + FROM t1 + LEFT JOIN (t2 RIGHT JOIN t3 USING(a)) USING(a) + LEFT JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 31 - 31 31 - + 12 32 32 32 - - + 15 35 35 35 35 35 + 17 37 - 37 - - +} +do_execsql_test joinB-205 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - - 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-206 { + SELECT a, b, c, d, e, f, g + FROM t1 + LEFT JOIN t2 USING(a,b) + RIGHT JOIN t3 USING(a,b) + LEFT JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 37 - 37 - 37 + 18 28 - - 38 - 38 + 19 29 - - 39 39 39 +} +do_execsql_test joinB-207 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - 11 11 - + 12 12 12 12 - - + 13 - - 13 13 - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - - 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-208 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL LEFT JOIN t2 + NATURAL RIGHT JOIN t3 + NATURAL LEFT JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-209 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 19 - - 19 19 19 +} +do_execsql_test joinB-210 { + SELECT a, c, d, e, f, g + FROM t1 + LEFT JOIN t2 USING(a,b) + RIGHT JOIN t3 USING(a,b) + RIGHT JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 19 - - 39 39 39 +} +do_execsql_test joinB-211 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 13 - - 13 13 - + 15 15 15 15 15 15 + 19 - - 19 19 19 +} +do_execsql_test joinB-212 { + SELECT a, c, d, e, f, g + FROM t1 + LEFT JOIN (t2 RIGHT JOIN t3 USING(a)) USING(a) + RIGHT JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 31 - 31 31 - + 13 - - - 33 - + 15 35 35 35 35 35 +} +do_execsql_test joinB-213 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-214 { + SELECT a, b, c, d, e, f, g + FROM t1 + LEFT JOIN t2 USING(a,b) + RIGHT JOIN t3 USING(a,b) + RIGHT JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 - - - - 37 + 18 28 - - - - 38 + 19 29 - - 39 39 39 +} +do_execsql_test joinB-215 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 13 - - 13 13 - + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-216 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL LEFT JOIN t2 + NATURAL RIGHT JOIN t3 + NATURAL RIGHT JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-217 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + FULL JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - - 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-218 { + SELECT a, c, d, e, f, g + FROM t1 + LEFT JOIN t2 USING(a,b) + RIGHT JOIN t3 USING(a,b) + FULL JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 17 37 - 37 - 37 + 18 - - 38 - 38 + 19 - - 39 39 39 +} +do_execsql_test joinB-219 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + FULL JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - 11 11 - + 12 12 12 12 - - + 13 - - 13 13 - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - - 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-220 { + SELECT a, c, d, e, f, g + FROM t1 + LEFT JOIN (t2 RIGHT JOIN t3 USING(a)) USING(a) + FULL JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 31 - 31 31 - + 12 32 32 32 - - + 13 - - - 33 - + 15 35 35 35 35 35 + 17 37 - 37 - - +} +do_execsql_test joinB-221 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + FULL JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - - 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-222 { + SELECT a, b, c, d, e, f, g + FROM t1 + LEFT JOIN t2 USING(a,b) + RIGHT JOIN t3 USING(a,b) + FULL JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 37 - 37 - 37 + 18 28 - - 38 - 38 + 19 29 - - 39 39 39 +} +do_execsql_test joinB-223 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + FULL JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - 11 11 - + 12 12 12 12 - - + 13 - - 13 13 - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - - 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-224 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL LEFT JOIN t2 + NATURAL RIGHT JOIN t3 + NATURAL FULL JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-225 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + FULL JOIN t3 USING(a) + INNER JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 19 - - 19 19 19 +} +do_execsql_test joinB-226 { + SELECT a, c, d, e, f, g + FROM t1 + LEFT JOIN t2 USING(a,b) + FULL JOIN t3 USING(a,b) + INNER JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 19 - - 39 39 39 +} +do_execsql_test joinB-227 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + FULL JOIN t3 USING(a) + INNER JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 13 - - 13 13 - + 15 15 15 15 15 15 + 19 - - 19 19 19 +} +do_execsql_test joinB-228 { + SELECT a, c, d, e, f, g + FROM t1 + LEFT JOIN (t2 FULL JOIN t3 USING(a)) USING(a) + INNER JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 31 - 31 31 - + 15 35 35 35 35 35 +} +do_execsql_test joinB-229 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + FULL JOIN t3 USING(a) + INNER JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-230 { + SELECT a, b, c, d, e, f, g + FROM t1 + LEFT JOIN t2 USING(a,b) + FULL JOIN t3 USING(a,b) + INNER JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 - - - - 37 + 18 28 - - - - 38 + 19 29 - - 39 39 39 +} +do_execsql_test joinB-231 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + FULL JOIN t3 USING(a) + INNER JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 13 - - 13 13 - + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-232 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL LEFT JOIN t2 + NATURAL FULL JOIN t3 + NATURAL INNER JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-233 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + FULL JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - - 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-234 { + SELECT a, c, d, e, f, g + FROM t1 + LEFT JOIN t2 USING(a,b) + FULL JOIN t3 USING(a,b) + LEFT JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 17 37 - 37 - 37 + 18 - - 38 - 38 + 19 - - 39 39 39 +} +do_execsql_test joinB-235 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + FULL JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - 11 11 - + 12 12 12 12 - - + 13 - - 13 13 - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - - 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-236 { + SELECT a, c, d, e, f, g + FROM t1 + LEFT JOIN (t2 FULL JOIN t3 USING(a)) USING(a) + LEFT JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 31 - 31 31 - + 12 32 32 32 - - + 15 35 35 35 35 35 + 17 37 - 37 - - +} +do_execsql_test joinB-237 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + FULL JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - - 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-238 { + SELECT a, b, c, d, e, f, g + FROM t1 + LEFT JOIN t2 USING(a,b) + FULL JOIN t3 USING(a,b) + LEFT JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 37 - 37 - 37 + 18 28 - - 38 - 38 + 19 29 - - 39 39 39 +} +do_execsql_test joinB-239 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + FULL JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - 11 11 - + 12 12 12 12 - - + 13 - - 13 13 - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - - 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-240 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL LEFT JOIN t2 + NATURAL FULL JOIN t3 + NATURAL LEFT JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-241 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + FULL JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 19 - - 19 19 19 +} +do_execsql_test joinB-242 { + SELECT a, c, d, e, f, g + FROM t1 + LEFT JOIN t2 USING(a,b) + FULL JOIN t3 USING(a,b) + RIGHT JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 19 - - 39 39 39 +} +do_execsql_test joinB-243 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + FULL JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 13 - - 13 13 - + 15 15 15 15 15 15 + 19 - - 19 19 19 +} +do_execsql_test joinB-244 { + SELECT a, c, d, e, f, g + FROM t1 + LEFT JOIN (t2 FULL JOIN t3 USING(a)) USING(a) + RIGHT JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 31 - 31 31 - + 13 - - - 33 - + 15 35 35 35 35 35 +} +do_execsql_test joinB-245 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + FULL JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-246 { + SELECT a, b, c, d, e, f, g + FROM t1 + LEFT JOIN t2 USING(a,b) + FULL JOIN t3 USING(a,b) + RIGHT JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 - - - - 37 + 18 28 - - - - 38 + 19 29 - - 39 39 39 +} +do_execsql_test joinB-247 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + FULL JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 13 - - 13 13 - + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-248 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL LEFT JOIN t2 + NATURAL FULL JOIN t3 + NATURAL RIGHT JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-249 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + FULL JOIN t3 USING(a) + FULL JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - - 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-250 { + SELECT a, c, d, e, f, g + FROM t1 + LEFT JOIN t2 USING(a,b) + FULL JOIN t3 USING(a,b) + FULL JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 17 37 - 37 - 37 + 18 - - 38 - 38 + 19 - - 39 39 39 +} +do_execsql_test joinB-251 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + FULL JOIN t3 USING(a) + FULL JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - 11 11 - + 12 12 12 12 - - + 13 - - 13 13 - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - - 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-252 { + SELECT a, c, d, e, f, g + FROM t1 + LEFT JOIN (t2 FULL JOIN t3 USING(a)) USING(a) + FULL JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 31 - 31 31 - + 12 32 32 32 - - + 13 - - - 33 - + 15 35 35 35 35 35 + 17 37 - 37 - - +} +do_execsql_test joinB-253 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + FULL JOIN t3 USING(a) + FULL JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - - 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-254 { + SELECT a, b, c, d, e, f, g + FROM t1 + LEFT JOIN t2 USING(a,b) + FULL JOIN t3 USING(a,b) + FULL JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 37 - 37 - 37 + 18 28 - - 38 - 38 + 19 29 - - 39 39 39 +} +do_execsql_test joinB-255 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + LEFT JOIN t2 USING(a) + FULL JOIN t3 USING(a) + FULL JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - 11 11 - + 12 12 12 12 - - + 13 - - 13 13 - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - - 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-256 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL LEFT JOIN t2 + NATURAL FULL JOIN t3 + NATURAL FULL JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-257 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + INNER JOIN t3 USING(a) + INNER JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 +} +do_execsql_test joinB-258 { + SELECT a, c, d, e, f, g + FROM t1 + RIGHT JOIN t2 USING(a,b) + INNER JOIN t3 USING(a,b) + INNER JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 +} +do_execsql_test joinB-259 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + INNER JOIN t3 USING(a) + INNER JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 13 - 13 13 13 - + 15 15 15 15 15 15 +} +do_execsql_test joinB-260 { + SELECT a, c, d, e, f, g + FROM t1 + RIGHT JOIN (t2 INNER JOIN t3 USING(a)) USING(a) + INNER JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 13 - 33 33 33 - + 15 35 35 35 35 35 +} +do_execsql_test joinB-261 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + INNER JOIN t3 USING(a) + INNER JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - - - 19 +} +do_execsql_test joinB-262 { + SELECT a, b, c, d, e, f, g + FROM t1 + RIGHT JOIN t2 USING(a,b) + INNER JOIN t3 USING(a,b) + INNER JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 - - - - 37 + 18 28 - - - - 38 + 19 29 - - - - 39 +} +do_execsql_test joinB-263 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + INNER JOIN t3 USING(a) + INNER JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - - - 19 +} +do_execsql_test joinB-264 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL RIGHT JOIN t2 + NATURAL INNER JOIN t3 + NATURAL INNER JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-265 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + INNER JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 18 - 18 18 - 18 +} +do_execsql_test joinB-266 { + SELECT a, c, d, e, f, g + FROM t1 + RIGHT JOIN t2 USING(a,b) + INNER JOIN t3 USING(a,b) + LEFT JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 18 - 38 38 - 38 +} +do_execsql_test joinB-267 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + INNER JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 18 - 18 18 - 18 +} +do_execsql_test joinB-268 { + SELECT a, c, d, e, f, g + FROM t1 + RIGHT JOIN (t2 INNER JOIN t3 USING(a)) USING(a) + LEFT JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 12 32 32 32 - - + 13 - 33 33 33 - + 15 35 35 35 35 35 + 18 - 38 38 - - +} +do_execsql_test joinB-269 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + INNER JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - 18 18 - 18 + 19 - - - - 19 +} +do_execsql_test joinB-270 { + SELECT a, b, c, d, e, f, g + FROM t1 + RIGHT JOIN t2 USING(a,b) + INNER JOIN t3 USING(a,b) + LEFT JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 - - - - 37 + 18 28 - 38 38 - 38 + 19 29 - - - - 39 +} +do_execsql_test joinB-271 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + INNER JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - 18 18 - 18 + 19 - - - - 19 +} +do_execsql_test joinB-272 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL RIGHT JOIN t2 + NATURAL INNER JOIN t3 + NATURAL LEFT JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-273 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + INNER JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 19 - - - 19 19 +} +do_execsql_test joinB-274 { + SELECT a, c, d, e, f, g + FROM t1 + RIGHT JOIN t2 USING(a,b) + INNER JOIN t3 USING(a,b) + RIGHT JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 19 - - - 39 39 +} +do_execsql_test joinB-275 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + INNER JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 - - - 11 - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 19 - - - 19 19 +} +do_execsql_test joinB-276 { + SELECT a, c, d, e, f, g + FROM t1 + RIGHT JOIN (t2 INNER JOIN t3 USING(a)) USING(a) + RIGHT JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 - - - 31 - + 13 - 33 33 33 - + 15 35 35 35 35 35 +} +do_execsql_test joinB-277 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + INNER JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - - 19 19 +} +do_execsql_test joinB-278 { + SELECT a, b, c, d, e, f, g + FROM t1 + RIGHT JOIN t2 USING(a,b) + INNER JOIN t3 USING(a,b) + RIGHT JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 - - - - 37 + 18 28 - - - - 38 + 19 29 - - - 39 39 +} +do_execsql_test joinB-279 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + INNER JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 - - - 11 - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - - 19 19 +} +do_execsql_test joinB-280 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL RIGHT JOIN t2 + NATURAL INNER JOIN t3 + NATURAL RIGHT JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-281 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + INNER JOIN t3 USING(a) + FULL JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 18 - 18 18 - 18 + 19 - - - 19 19 +} +do_execsql_test joinB-282 { + SELECT a, c, d, e, f, g + FROM t1 + RIGHT JOIN t2 USING(a,b) + INNER JOIN t3 USING(a,b) + FULL JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 18 - 38 38 - 38 + 19 - - - 39 39 +} +do_execsql_test joinB-283 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + INNER JOIN t3 USING(a) + FULL JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 - - - 11 - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 18 - 18 18 - 18 + 19 - - - 19 19 +} +do_execsql_test joinB-284 { + SELECT a, c, d, e, f, g + FROM t1 + RIGHT JOIN (t2 INNER JOIN t3 USING(a)) USING(a) + FULL JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 - - - 31 - + 12 32 32 32 - - + 13 - 33 33 33 - + 15 35 35 35 35 35 + 18 - 38 38 - - +} +do_execsql_test joinB-285 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + INNER JOIN t3 USING(a) + FULL JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - 18 18 - 18 + 19 - - - 19 19 +} +do_execsql_test joinB-286 { + SELECT a, b, c, d, e, f, g + FROM t1 + RIGHT JOIN t2 USING(a,b) + INNER JOIN t3 USING(a,b) + FULL JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 - - - - 37 + 18 28 - 38 38 - 38 + 19 29 - - - 39 39 +} +do_execsql_test joinB-287 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + INNER JOIN t3 USING(a) + FULL JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 - - - 11 - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - 18 18 - 18 + 19 - - - 19 19 +} +do_execsql_test joinB-288 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL RIGHT JOIN t2 + NATURAL INNER JOIN t3 + NATURAL FULL JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-289 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + INNER JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 +} +do_execsql_test joinB-290 { + SELECT a, c, d, e, f, g + FROM t1 + RIGHT JOIN t2 USING(a,b) + LEFT JOIN t3 USING(a,b) + INNER JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 +} +do_execsql_test joinB-291 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + INNER JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 13 - 13 13 13 - + 15 15 15 15 15 15 +} +do_execsql_test joinB-292 { + SELECT a, c, d, e, f, g + FROM t1 + RIGHT JOIN (t2 LEFT JOIN t3 USING(a)) USING(a) + INNER JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 13 - 33 33 33 - + 15 35 35 35 35 35 +} +do_execsql_test joinB-293 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + INNER JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - - - 19 +} +do_execsql_test joinB-294 { + SELECT a, b, c, d, e, f, g + FROM t1 + RIGHT JOIN t2 USING(a,b) + LEFT JOIN t3 USING(a,b) + INNER JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 - - - - 37 + 18 28 - - - - 38 + 19 29 - - - - 39 +} +do_execsql_test joinB-295 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + INNER JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - - - 19 +} +do_execsql_test joinB-296 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL RIGHT JOIN t2 + NATURAL LEFT JOIN t3 + NATURAL INNER JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-297 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 18 - 18 18 - 18 +} +do_execsql_test joinB-298 { + SELECT a, c, d, e, f, g + FROM t1 + RIGHT JOIN t2 USING(a,b) + LEFT JOIN t3 USING(a,b) + LEFT JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 18 - 38 38 - 38 +} +do_execsql_test joinB-299 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 18 - 18 18 - 18 +} +do_execsql_test joinB-300 { + SELECT a, c, d, e, f, g + FROM t1 + RIGHT JOIN (t2 LEFT JOIN t3 USING(a)) USING(a) + LEFT JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 12 32 32 32 - - + 13 - 33 33 33 - + 15 35 35 35 35 35 + 18 - 38 38 - - +} +do_execsql_test joinB-301 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - 18 18 - 18 + 19 - - - - 19 +} +do_execsql_test joinB-302 { + SELECT a, b, c, d, e, f, g + FROM t1 + RIGHT JOIN t2 USING(a,b) + LEFT JOIN t3 USING(a,b) + LEFT JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 - - - - 37 + 18 28 - 38 38 - 38 + 19 29 - - - - 39 +} +do_execsql_test joinB-303 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - 18 18 - 18 + 19 - - - - 19 +} +do_execsql_test joinB-304 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL RIGHT JOIN t2 + NATURAL LEFT JOIN t3 + NATURAL LEFT JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-305 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 19 - - - 19 19 +} +do_execsql_test joinB-306 { + SELECT a, c, d, e, f, g + FROM t1 + RIGHT JOIN t2 USING(a,b) + LEFT JOIN t3 USING(a,b) + RIGHT JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 19 - - - 39 39 +} +do_execsql_test joinB-307 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 - - - 11 - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 19 - - - 19 19 +} +do_execsql_test joinB-308 { + SELECT a, c, d, e, f, g + FROM t1 + RIGHT JOIN (t2 LEFT JOIN t3 USING(a)) USING(a) + RIGHT JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 - - - 31 - + 13 - 33 33 33 - + 15 35 35 35 35 35 +} +do_execsql_test joinB-309 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - - 19 19 +} +do_execsql_test joinB-310 { + SELECT a, b, c, d, e, f, g + FROM t1 + RIGHT JOIN t2 USING(a,b) + LEFT JOIN t3 USING(a,b) + RIGHT JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 - - - - 37 + 18 28 - - - - 38 + 19 29 - - - 39 39 +} +do_execsql_test joinB-311 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 - - - 11 - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - - 19 19 +} +do_execsql_test joinB-312 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL RIGHT JOIN t2 + NATURAL LEFT JOIN t3 + NATURAL RIGHT JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-313 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + FULL JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 18 - 18 18 - 18 + 19 - - - 19 19 +} +do_execsql_test joinB-314 { + SELECT a, c, d, e, f, g + FROM t1 + RIGHT JOIN t2 USING(a,b) + LEFT JOIN t3 USING(a,b) + FULL JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 18 - 38 38 - 38 + 19 - - - 39 39 +} +do_execsql_test joinB-315 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + FULL JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 - - - 11 - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 18 - 18 18 - 18 + 19 - - - 19 19 +} +do_execsql_test joinB-316 { + SELECT a, c, d, e, f, g + FROM t1 + RIGHT JOIN (t2 LEFT JOIN t3 USING(a)) USING(a) + FULL JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 - - - 31 - + 12 32 32 32 - - + 13 - 33 33 33 - + 15 35 35 35 35 35 + 18 - 38 38 - - +} +do_execsql_test joinB-317 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + FULL JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - 18 18 - 18 + 19 - - - 19 19 +} +do_execsql_test joinB-318 { + SELECT a, b, c, d, e, f, g + FROM t1 + RIGHT JOIN t2 USING(a,b) + LEFT JOIN t3 USING(a,b) + FULL JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 - - - - 37 + 18 28 - 38 38 - 38 + 19 29 - - - 39 39 +} +do_execsql_test joinB-319 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + FULL JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 - - - 11 - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - 18 18 - 18 + 19 - - - 19 19 +} +do_execsql_test joinB-320 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL RIGHT JOIN t2 + NATURAL LEFT JOIN t3 + NATURAL FULL JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-321 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + INNER JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 19 - - 19 19 19 +} +do_execsql_test joinB-322 { + SELECT a, c, d, e, f, g + FROM t1 + RIGHT JOIN t2 USING(a,b) + RIGHT JOIN t3 USING(a,b) + INNER JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 19 - - 39 39 39 +} +do_execsql_test joinB-323 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + INNER JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 - - 11 11 - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 19 - - 19 19 19 +} +do_execsql_test joinB-324 { + SELECT a, c, d, e, f, g + FROM t1 + RIGHT JOIN (t2 RIGHT JOIN t3 USING(a)) USING(a) + INNER JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 31 - 31 31 - + 13 - 33 33 33 - + 15 35 35 35 35 35 +} +do_execsql_test joinB-325 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + INNER JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-326 { + SELECT a, b, c, d, e, f, g + FROM t1 + RIGHT JOIN t2 USING(a,b) + RIGHT JOIN t3 USING(a,b) + INNER JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 - - - - 37 + 18 28 - - - - 38 + 19 29 - - 39 39 39 +} +do_execsql_test joinB-327 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + INNER JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 - - 11 11 - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-328 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL RIGHT JOIN t2 + NATURAL RIGHT JOIN t3 + NATURAL INNER JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-329 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-330 { + SELECT a, c, d, e, f, g + FROM t1 + RIGHT JOIN t2 USING(a,b) + RIGHT JOIN t3 USING(a,b) + LEFT JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 17 - - 37 - 37 + 18 - 38 38 - 38 + 19 - - 39 39 39 +} +do_execsql_test joinB-331 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 - - 11 11 - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 - - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-332 { + SELECT a, c, d, e, f, g + FROM t1 + RIGHT JOIN (t2 RIGHT JOIN t3 USING(a)) USING(a) + LEFT JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 31 - 31 31 - + 12 32 32 32 - - + 13 - 33 33 33 - + 15 35 35 35 35 35 + 17 37 - 37 - - + 18 - 38 38 - - +} +do_execsql_test joinB-333 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-334 { + SELECT a, b, c, d, e, f, g + FROM t1 + RIGHT JOIN t2 USING(a,b) + RIGHT JOIN t3 USING(a,b) + LEFT JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 - - 37 - 37 + 18 28 - 38 38 - 38 + 19 29 - - 39 39 39 +} +do_execsql_test joinB-335 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 - - 11 11 - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 - - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-336 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL RIGHT JOIN t2 + NATURAL RIGHT JOIN t3 + NATURAL LEFT JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-337 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 19 - - 19 19 19 +} +do_execsql_test joinB-338 { + SELECT a, c, d, e, f, g + FROM t1 + RIGHT JOIN t2 USING(a,b) + RIGHT JOIN t3 USING(a,b) + RIGHT JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 19 - - 39 39 39 +} +do_execsql_test joinB-339 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 - - 11 11 - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 19 - - 19 19 19 +} +do_execsql_test joinB-340 { + SELECT a, c, d, e, f, g + FROM t1 + RIGHT JOIN (t2 RIGHT JOIN t3 USING(a)) USING(a) + RIGHT JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 31 - 31 31 - + 13 - 33 33 33 - + 15 35 35 35 35 35 +} +do_execsql_test joinB-341 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-342 { + SELECT a, b, c, d, e, f, g + FROM t1 + RIGHT JOIN t2 USING(a,b) + RIGHT JOIN t3 USING(a,b) + RIGHT JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 - - - - 37 + 18 28 - - - - 38 + 19 29 - - 39 39 39 +} +do_execsql_test joinB-343 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 - - 11 11 - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-344 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL RIGHT JOIN t2 + NATURAL RIGHT JOIN t3 + NATURAL RIGHT JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-345 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + FULL JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-346 { + SELECT a, c, d, e, f, g + FROM t1 + RIGHT JOIN t2 USING(a,b) + RIGHT JOIN t3 USING(a,b) + FULL JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 17 - - 37 - 37 + 18 - 38 38 - 38 + 19 - - 39 39 39 +} +do_execsql_test joinB-347 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + FULL JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 - - 11 11 - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 - - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-348 { + SELECT a, c, d, e, f, g + FROM t1 + RIGHT JOIN (t2 RIGHT JOIN t3 USING(a)) USING(a) + FULL JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 31 - 31 31 - + 12 32 32 32 - - + 13 - 33 33 33 - + 15 35 35 35 35 35 + 17 37 - 37 - - + 18 - 38 38 - - +} +do_execsql_test joinB-349 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + FULL JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-350 { + SELECT a, b, c, d, e, f, g + FROM t1 + RIGHT JOIN t2 USING(a,b) + RIGHT JOIN t3 USING(a,b) + FULL JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 - - 37 - 37 + 18 28 - 38 38 - 38 + 19 29 - - 39 39 39 +} +do_execsql_test joinB-351 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + FULL JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 - - 11 11 - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 - - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-352 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL RIGHT JOIN t2 + NATURAL RIGHT JOIN t3 + NATURAL FULL JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-353 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + FULL JOIN t3 USING(a) + INNER JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 19 - - 19 19 19 +} +do_execsql_test joinB-354 { + SELECT a, c, d, e, f, g + FROM t1 + RIGHT JOIN t2 USING(a,b) + FULL JOIN t3 USING(a,b) + INNER JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 19 - - 39 39 39 +} +do_execsql_test joinB-355 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + FULL JOIN t3 USING(a) + INNER JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 - - 11 11 - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 19 - - 19 19 19 +} +do_execsql_test joinB-356 { + SELECT a, c, d, e, f, g + FROM t1 + RIGHT JOIN (t2 FULL JOIN t3 USING(a)) USING(a) + INNER JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 31 - 31 31 - + 13 - 33 33 33 - + 15 35 35 35 35 35 +} +do_execsql_test joinB-357 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + FULL JOIN t3 USING(a) + INNER JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-358 { + SELECT a, b, c, d, e, f, g + FROM t1 + RIGHT JOIN t2 USING(a,b) + FULL JOIN t3 USING(a,b) + INNER JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 - - - - 37 + 18 28 - - - - 38 + 19 29 - - 39 39 39 +} +do_execsql_test joinB-359 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + FULL JOIN t3 USING(a) + INNER JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 - - 11 11 - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-360 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL RIGHT JOIN t2 + NATURAL FULL JOIN t3 + NATURAL INNER JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-361 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + FULL JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-362 { + SELECT a, c, d, e, f, g + FROM t1 + RIGHT JOIN t2 USING(a,b) + FULL JOIN t3 USING(a,b) + LEFT JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 17 - - 37 - 37 + 18 - 38 38 - 38 + 19 - - 39 39 39 +} +do_execsql_test joinB-363 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + FULL JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + - - - - - - + 11 - - 11 11 - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 - - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-364 { + SELECT a, c, d, e, f, g + FROM t1 + RIGHT JOIN (t2 FULL JOIN t3 USING(a)) USING(a) + LEFT JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 31 - 31 31 - + 12 32 32 32 - - + 13 - 33 33 33 - + 15 35 35 35 35 35 + 17 37 - 37 - - + 18 - 38 38 - - +} +do_execsql_test joinB-365 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + FULL JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-366 { + SELECT a, b, c, d, e, f, g + FROM t1 + RIGHT JOIN t2 USING(a,b) + FULL JOIN t3 USING(a,b) + LEFT JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 - - 37 - 37 + 18 28 - 38 38 - 38 + 19 29 - - 39 39 39 +} +do_execsql_test joinB-367 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + FULL JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + - - - - - - + 11 - - 11 11 - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 - - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-368 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL RIGHT JOIN t2 + NATURAL FULL JOIN t3 + NATURAL LEFT JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-369 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + FULL JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 19 - - 19 19 19 +} +do_execsql_test joinB-370 { + SELECT a, c, d, e, f, g + FROM t1 + RIGHT JOIN t2 USING(a,b) + FULL JOIN t3 USING(a,b) + RIGHT JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 19 - - 39 39 39 +} +do_execsql_test joinB-371 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + FULL JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 - - 11 11 - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 19 - - 19 19 19 +} +do_execsql_test joinB-372 { + SELECT a, c, d, e, f, g + FROM t1 + RIGHT JOIN (t2 FULL JOIN t3 USING(a)) USING(a) + RIGHT JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 31 - 31 31 - + 13 - 33 33 33 - + 15 35 35 35 35 35 +} +do_execsql_test joinB-373 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + FULL JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-374 { + SELECT a, b, c, d, e, f, g + FROM t1 + RIGHT JOIN t2 USING(a,b) + FULL JOIN t3 USING(a,b) + RIGHT JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 - - - - 37 + 18 28 - - - - 38 + 19 29 - - 39 39 39 +} +do_execsql_test joinB-375 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + FULL JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 - - 11 11 - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-376 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL RIGHT JOIN t2 + NATURAL FULL JOIN t3 + NATURAL RIGHT JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-377 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + FULL JOIN t3 USING(a) + FULL JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-378 { + SELECT a, c, d, e, f, g + FROM t1 + RIGHT JOIN t2 USING(a,b) + FULL JOIN t3 USING(a,b) + FULL JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 17 - - 37 - 37 + 18 - 38 38 - 38 + 19 - - 39 39 39 +} +do_execsql_test joinB-379 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + FULL JOIN t3 USING(a) + FULL JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + - - - - - - + 11 - - 11 11 - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 - - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-380 { + SELECT a, c, d, e, f, g + FROM t1 + RIGHT JOIN (t2 FULL JOIN t3 USING(a)) USING(a) + FULL JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 31 - 31 31 - + 12 32 32 32 - - + 13 - 33 33 33 - + 15 35 35 35 35 35 + 17 37 - 37 - - + 18 - 38 38 - - +} +do_execsql_test joinB-381 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + FULL JOIN t3 USING(a) + FULL JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-382 { + SELECT a, b, c, d, e, f, g + FROM t1 + RIGHT JOIN t2 USING(a,b) + FULL JOIN t3 USING(a,b) + FULL JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 - - 37 - 37 + 18 28 - 38 38 - 38 + 19 29 - - 39 39 39 +} +do_execsql_test joinB-383 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + RIGHT JOIN t2 USING(a) + FULL JOIN t3 USING(a) + FULL JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + - - - - - - + 11 - - 11 11 - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 - - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-384 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL RIGHT JOIN t2 + NATURAL FULL JOIN t3 + NATURAL FULL JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-385 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + INNER JOIN t3 USING(a) + INNER JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 +} +do_execsql_test joinB-386 { + SELECT a, c, d, e, f, g + FROM t1 + FULL JOIN t2 USING(a,b) + INNER JOIN t3 USING(a,b) + INNER JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 +} +do_execsql_test joinB-387 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + INNER JOIN t3 USING(a) + INNER JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 13 - 13 13 13 - + 15 15 15 15 15 15 +} +do_execsql_test joinB-388 { + SELECT a, c, d, e, f, g + FROM t1 + FULL JOIN (t2 INNER JOIN t3 USING(a)) USING(a) + INNER JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 31 - - 31 - + 13 - 33 33 33 - + 15 35 35 35 35 35 +} +do_execsql_test joinB-389 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + INNER JOIN t3 USING(a) + INNER JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - - - 19 +} +do_execsql_test joinB-390 { + SELECT a, b, c, d, e, f, g + FROM t1 + FULL JOIN t2 USING(a,b) + INNER JOIN t3 USING(a,b) + INNER JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 - - - - 37 + 18 28 - - - - 38 + 19 29 - - - - 39 +} +do_execsql_test joinB-391 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + INNER JOIN t3 USING(a) + INNER JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - - - 19 +} +do_execsql_test joinB-392 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL FULL JOIN t2 + NATURAL INNER JOIN t3 + NATURAL INNER JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-393 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + INNER JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 +} +do_execsql_test joinB-394 { + SELECT a, c, d, e, f, g + FROM t1 + FULL JOIN t2 USING(a,b) + INNER JOIN t3 USING(a,b) + LEFT JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 17 37 - 37 - 37 + 18 - 38 38 - 38 +} +do_execsql_test joinB-395 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + INNER JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 +} +do_execsql_test joinB-396 { + SELECT a, c, d, e, f, g + FROM t1 + FULL JOIN (t2 INNER JOIN t3 USING(a)) USING(a) + LEFT JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 31 - - 31 - + 12 32 32 32 - - + 13 - 33 33 33 - + 15 35 35 35 35 35 + 17 37 - - - - + 18 - 38 38 - - +} +do_execsql_test joinB-397 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + INNER JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - - - 19 +} +do_execsql_test joinB-398 { + SELECT a, b, c, d, e, f, g + FROM t1 + FULL JOIN t2 USING(a,b) + INNER JOIN t3 USING(a,b) + LEFT JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 37 - 37 - 37 + 18 28 - 38 38 - 38 + 19 29 - - - - 39 +} +do_execsql_test joinB-399 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + INNER JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - - - 19 +} +do_execsql_test joinB-400 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL FULL JOIN t2 + NATURAL INNER JOIN t3 + NATURAL LEFT JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-401 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + INNER JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 19 - - - 19 19 +} +do_execsql_test joinB-402 { + SELECT a, c, d, e, f, g + FROM t1 + FULL JOIN t2 USING(a,b) + INNER JOIN t3 USING(a,b) + RIGHT JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 19 - - - 39 39 +} +do_execsql_test joinB-403 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + INNER JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 19 - - - 19 19 +} +do_execsql_test joinB-404 { + SELECT a, c, d, e, f, g + FROM t1 + FULL JOIN (t2 INNER JOIN t3 USING(a)) USING(a) + RIGHT JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 31 - - 31 - + 13 - 33 33 33 - + 15 35 35 35 35 35 +} +do_execsql_test joinB-405 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + INNER JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - - 19 19 +} +do_execsql_test joinB-406 { + SELECT a, b, c, d, e, f, g + FROM t1 + FULL JOIN t2 USING(a,b) + INNER JOIN t3 USING(a,b) + RIGHT JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 - - - - 37 + 18 28 - - - - 38 + 19 29 - - - 39 39 +} +do_execsql_test joinB-407 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + INNER JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - - 19 19 +} +do_execsql_test joinB-408 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL FULL JOIN t2 + NATURAL INNER JOIN t3 + NATURAL RIGHT JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-409 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + INNER JOIN t3 USING(a) + FULL JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - - 19 19 +} +do_execsql_test joinB-410 { + SELECT a, c, d, e, f, g + FROM t1 + FULL JOIN t2 USING(a,b) + INNER JOIN t3 USING(a,b) + FULL JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 17 37 - 37 - 37 + 18 - 38 38 - 38 + 19 - - - 39 39 +} +do_execsql_test joinB-411 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + INNER JOIN t3 USING(a) + FULL JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - - 19 19 +} +do_execsql_test joinB-412 { + SELECT a, c, d, e, f, g + FROM t1 + FULL JOIN (t2 INNER JOIN t3 USING(a)) USING(a) + FULL JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 31 - - 31 - + 12 32 32 32 - - + 13 - 33 33 33 - + 15 35 35 35 35 35 + 17 37 - - - - + 18 - 38 38 - - +} +do_execsql_test joinB-413 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + INNER JOIN t3 USING(a) + FULL JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - - 19 19 +} +do_execsql_test joinB-414 { + SELECT a, b, c, d, e, f, g + FROM t1 + FULL JOIN t2 USING(a,b) + INNER JOIN t3 USING(a,b) + FULL JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 37 - 37 - 37 + 18 28 - 38 38 - 38 + 19 29 - - - 39 39 +} +do_execsql_test joinB-415 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + INNER JOIN t3 USING(a) + FULL JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - - 19 19 +} +do_execsql_test joinB-416 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL FULL JOIN t2 + NATURAL INNER JOIN t3 + NATURAL FULL JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-417 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + INNER JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 +} +do_execsql_test joinB-418 { + SELECT a, c, d, e, f, g + FROM t1 + FULL JOIN t2 USING(a,b) + LEFT JOIN t3 USING(a,b) + INNER JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 +} +do_execsql_test joinB-419 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + INNER JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 13 - 13 13 13 - + 15 15 15 15 15 15 +} +do_execsql_test joinB-420 { + SELECT a, c, d, e, f, g + FROM t1 + FULL JOIN (t2 LEFT JOIN t3 USING(a)) USING(a) + INNER JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 31 - - 31 - + 13 - 33 33 33 - + 15 35 35 35 35 35 +} +do_execsql_test joinB-421 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + INNER JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - - - 19 +} +do_execsql_test joinB-422 { + SELECT a, b, c, d, e, f, g + FROM t1 + FULL JOIN t2 USING(a,b) + LEFT JOIN t3 USING(a,b) + INNER JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 - - - - 37 + 18 28 - - - - 38 + 19 29 - - - - 39 +} +do_execsql_test joinB-423 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + INNER JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - - - 19 +} +do_execsql_test joinB-424 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL FULL JOIN t2 + NATURAL LEFT JOIN t3 + NATURAL INNER JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-425 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 +} +do_execsql_test joinB-426 { + SELECT a, c, d, e, f, g + FROM t1 + FULL JOIN t2 USING(a,b) + LEFT JOIN t3 USING(a,b) + LEFT JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 17 37 - 37 - 37 + 18 - 38 38 - 38 +} +do_execsql_test joinB-427 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - 11 11 - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 +} +do_execsql_test joinB-428 { + SELECT a, c, d, e, f, g + FROM t1 + FULL JOIN (t2 LEFT JOIN t3 USING(a)) USING(a) + LEFT JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 31 - - 31 - + 12 32 32 32 - - + 13 - 33 33 33 - + 15 35 35 35 35 35 + 17 37 - - - - + 18 - 38 38 - - +} +do_execsql_test joinB-429 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - - - 19 +} +do_execsql_test joinB-430 { + SELECT a, b, c, d, e, f, g + FROM t1 + FULL JOIN t2 USING(a,b) + LEFT JOIN t3 USING(a,b) + LEFT JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 37 - 37 - 37 + 18 28 - 38 38 - 38 + 19 29 - - - - 39 +} +do_execsql_test joinB-431 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - 11 11 - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - - - 19 +} +do_execsql_test joinB-432 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL FULL JOIN t2 + NATURAL LEFT JOIN t3 + NATURAL LEFT JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-433 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 19 - - - 19 19 +} +do_execsql_test joinB-434 { + SELECT a, c, d, e, f, g + FROM t1 + FULL JOIN t2 USING(a,b) + LEFT JOIN t3 USING(a,b) + RIGHT JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 19 - - - 39 39 +} +do_execsql_test joinB-435 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 19 - - - 19 19 +} +do_execsql_test joinB-436 { + SELECT a, c, d, e, f, g + FROM t1 + FULL JOIN (t2 LEFT JOIN t3 USING(a)) USING(a) + RIGHT JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 31 - - 31 - + 13 - 33 33 33 - + 15 35 35 35 35 35 +} +do_execsql_test joinB-437 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - - 19 19 +} +do_execsql_test joinB-438 { + SELECT a, b, c, d, e, f, g + FROM t1 + FULL JOIN t2 USING(a,b) + LEFT JOIN t3 USING(a,b) + RIGHT JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 - - - - 37 + 18 28 - - - - 38 + 19 29 - - - 39 39 +} +do_execsql_test joinB-439 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - - 19 19 +} +do_execsql_test joinB-440 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL FULL JOIN t2 + NATURAL LEFT JOIN t3 + NATURAL RIGHT JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-441 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + FULL JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - - 19 19 +} +do_execsql_test joinB-442 { + SELECT a, c, d, e, f, g + FROM t1 + FULL JOIN t2 USING(a,b) + LEFT JOIN t3 USING(a,b) + FULL JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 17 37 - 37 - 37 + 18 - 38 38 - 38 + 19 - - - 39 39 +} +do_execsql_test joinB-443 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + FULL JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - 11 11 - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - - 19 19 +} +do_execsql_test joinB-444 { + SELECT a, c, d, e, f, g + FROM t1 + FULL JOIN (t2 LEFT JOIN t3 USING(a)) USING(a) + FULL JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 31 - - 31 - + 12 32 32 32 - - + 13 - 33 33 33 - + 15 35 35 35 35 35 + 17 37 - - - - + 18 - 38 38 - - +} +do_execsql_test joinB-445 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + FULL JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - - 19 19 +} +do_execsql_test joinB-446 { + SELECT a, b, c, d, e, f, g + FROM t1 + FULL JOIN t2 USING(a,b) + LEFT JOIN t3 USING(a,b) + FULL JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 37 - 37 - 37 + 18 28 - 38 38 - 38 + 19 29 - - - 39 39 +} +do_execsql_test joinB-447 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + LEFT JOIN t3 USING(a) + FULL JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - 11 11 - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - - 19 19 +} +do_execsql_test joinB-448 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL FULL JOIN t2 + NATURAL LEFT JOIN t3 + NATURAL FULL JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-449 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + INNER JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 19 - - 19 19 19 +} +do_execsql_test joinB-450 { + SELECT a, c, d, e, f, g + FROM t1 + FULL JOIN t2 USING(a,b) + RIGHT JOIN t3 USING(a,b) + INNER JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 19 - - 39 39 39 +} +do_execsql_test joinB-451 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + INNER JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 19 - - 19 19 19 +} +do_execsql_test joinB-452 { + SELECT a, c, d, e, f, g + FROM t1 + FULL JOIN (t2 RIGHT JOIN t3 USING(a)) USING(a) + INNER JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 31 - 31 31 - + 13 - 33 33 33 - + 15 35 35 35 35 35 +} +do_execsql_test joinB-453 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + INNER JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-454 { + SELECT a, b, c, d, e, f, g + FROM t1 + FULL JOIN t2 USING(a,b) + RIGHT JOIN t3 USING(a,b) + INNER JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 - - - - 37 + 18 28 - - - - 38 + 19 29 - - 39 39 39 +} +do_execsql_test joinB-455 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + INNER JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-456 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL FULL JOIN t2 + NATURAL RIGHT JOIN t3 + NATURAL INNER JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-457 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-458 { + SELECT a, c, d, e, f, g + FROM t1 + FULL JOIN t2 USING(a,b) + RIGHT JOIN t3 USING(a,b) + LEFT JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 17 37 - 37 - 37 + 18 - 38 38 - 38 + 19 - - 39 39 39 +} +do_execsql_test joinB-459 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - 11 11 - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-460 { + SELECT a, c, d, e, f, g + FROM t1 + FULL JOIN (t2 RIGHT JOIN t3 USING(a)) USING(a) + LEFT JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 31 - 31 31 - + 12 32 32 32 - - + 13 - 33 33 33 - + 15 35 35 35 35 35 + 17 37 - 37 - - + 18 - 38 38 - - +} +do_execsql_test joinB-461 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-462 { + SELECT a, b, c, d, e, f, g + FROM t1 + FULL JOIN t2 USING(a,b) + RIGHT JOIN t3 USING(a,b) + LEFT JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 37 - 37 - 37 + 18 28 - 38 38 - 38 + 19 29 - - 39 39 39 +} +do_execsql_test joinB-463 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - 11 11 - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-464 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL FULL JOIN t2 + NATURAL RIGHT JOIN t3 + NATURAL LEFT JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-465 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 19 - - 19 19 19 +} +do_execsql_test joinB-466 { + SELECT a, c, d, e, f, g + FROM t1 + FULL JOIN t2 USING(a,b) + RIGHT JOIN t3 USING(a,b) + RIGHT JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 19 - - 39 39 39 +} +do_execsql_test joinB-467 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 19 - - 19 19 19 +} +do_execsql_test joinB-468 { + SELECT a, c, d, e, f, g + FROM t1 + FULL JOIN (t2 RIGHT JOIN t3 USING(a)) USING(a) + RIGHT JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 31 - 31 31 - + 13 - 33 33 33 - + 15 35 35 35 35 35 +} +do_execsql_test joinB-469 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-470 { + SELECT a, b, c, d, e, f, g + FROM t1 + FULL JOIN t2 USING(a,b) + RIGHT JOIN t3 USING(a,b) + RIGHT JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 - - - - 37 + 18 28 - - - - 38 + 19 29 - - 39 39 39 +} +do_execsql_test joinB-471 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-472 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL FULL JOIN t2 + NATURAL RIGHT JOIN t3 + NATURAL RIGHT JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-473 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + FULL JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-474 { + SELECT a, c, d, e, f, g + FROM t1 + FULL JOIN t2 USING(a,b) + RIGHT JOIN t3 USING(a,b) + FULL JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 17 37 - 37 - 37 + 18 - 38 38 - 38 + 19 - - 39 39 39 +} +do_execsql_test joinB-475 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + FULL JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - 11 11 - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-476 { + SELECT a, c, d, e, f, g + FROM t1 + FULL JOIN (t2 RIGHT JOIN t3 USING(a)) USING(a) + FULL JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 31 - 31 31 - + 12 32 32 32 - - + 13 - 33 33 33 - + 15 35 35 35 35 35 + 17 37 - 37 - - + 18 - 38 38 - - +} +do_execsql_test joinB-477 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + FULL JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-478 { + SELECT a, b, c, d, e, f, g + FROM t1 + FULL JOIN t2 USING(a,b) + RIGHT JOIN t3 USING(a,b) + FULL JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 37 - 37 - 37 + 18 28 - 38 38 - 38 + 19 29 - - 39 39 39 +} +do_execsql_test joinB-479 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + RIGHT JOIN t3 USING(a) + FULL JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - 11 11 - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-480 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL FULL JOIN t2 + NATURAL RIGHT JOIN t3 + NATURAL FULL JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-481 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + FULL JOIN t3 USING(a) + INNER JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 19 - - 19 19 19 +} +do_execsql_test joinB-482 { + SELECT a, c, d, e, f, g + FROM t1 + FULL JOIN t2 USING(a,b) + FULL JOIN t3 USING(a,b) + INNER JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 19 - - 39 39 39 +} +do_execsql_test joinB-483 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + FULL JOIN t3 USING(a) + INNER JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 19 - - 19 19 19 +} +do_execsql_test joinB-484 { + SELECT a, c, d, e, f, g + FROM t1 + FULL JOIN (t2 FULL JOIN t3 USING(a)) USING(a) + INNER JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 31 - 31 31 - + 13 - 33 33 33 - + 15 35 35 35 35 35 +} +do_execsql_test joinB-485 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + FULL JOIN t3 USING(a) + INNER JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-486 { + SELECT a, b, c, d, e, f, g + FROM t1 + FULL JOIN t2 USING(a,b) + FULL JOIN t3 USING(a,b) + INNER JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 - - - - 37 + 18 28 - - - - 38 + 19 29 - - 39 39 39 +} +do_execsql_test joinB-487 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + FULL JOIN t3 USING(a) + INNER JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-488 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL FULL JOIN t2 + NATURAL FULL JOIN t3 + NATURAL INNER JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-489 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + FULL JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-490 { + SELECT a, c, d, e, f, g + FROM t1 + FULL JOIN t2 USING(a,b) + FULL JOIN t3 USING(a,b) + LEFT JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 17 37 - 37 - 37 + 18 - 38 38 - 38 + 19 - - 39 39 39 +} +do_execsql_test joinB-491 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + FULL JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + - - - - - - + 11 11 - 11 11 - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-492 { + SELECT a, c, d, e, f, g + FROM t1 + FULL JOIN (t2 FULL JOIN t3 USING(a)) USING(a) + LEFT JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 31 - 31 31 - + 12 32 32 32 - - + 13 - 33 33 33 - + 15 35 35 35 35 35 + 17 37 - 37 - - + 18 - 38 38 - - +} +do_execsql_test joinB-493 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + FULL JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-494 { + SELECT a, b, c, d, e, f, g + FROM t1 + FULL JOIN t2 USING(a,b) + FULL JOIN t3 USING(a,b) + LEFT JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 37 - 37 - 37 + 18 28 - 38 38 - 38 + 19 29 - - 39 39 39 +} +do_execsql_test joinB-495 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + FULL JOIN t3 USING(a) + LEFT JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + - - - - - - + 11 11 - 11 11 - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-496 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL FULL JOIN t2 + NATURAL FULL JOIN t3 + NATURAL LEFT JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-497 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + FULL JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 19 - - 19 19 19 +} +do_execsql_test joinB-498 { + SELECT a, c, d, e, f, g + FROM t1 + FULL JOIN t2 USING(a,b) + FULL JOIN t3 USING(a,b) + RIGHT JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 19 - - 39 39 39 +} +do_execsql_test joinB-499 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + FULL JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 19 - - 19 19 19 +} +do_execsql_test joinB-500 { + SELECT a, c, d, e, f, g + FROM t1 + FULL JOIN (t2 FULL JOIN t3 USING(a)) USING(a) + RIGHT JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 31 - 31 31 - + 13 - 33 33 33 - + 15 35 35 35 35 35 +} +do_execsql_test joinB-501 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + FULL JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-502 { + SELECT a, b, c, d, e, f, g + FROM t1 + FULL JOIN t2 USING(a,b) + FULL JOIN t3 USING(a,b) + RIGHT JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 - - - - 37 + 18 28 - - - - 38 + 19 29 - - 39 39 39 +} +do_execsql_test joinB-503 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + FULL JOIN t3 USING(a) + RIGHT JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 - - - - 17 + 18 - - - - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-504 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL FULL JOIN t2 + NATURAL FULL JOIN t3 + NATURAL RIGHT JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +do_execsql_test joinB-505 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + FULL JOIN t3 USING(a) + FULL JOIN t4 USING(a) + INNER JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-506 { + SELECT a, c, d, e, f, g + FROM t1 + FULL JOIN t2 USING(a,b) + FULL JOIN t3 USING(a,b) + FULL JOIN t4 USING(a,b) + INNER JOIN t5 USING(a,b) + WHERE a<>13 + ORDER BY 1 NULLS FIRST; +} { + 15 35 35 35 35 35 + 17 37 - 37 - 37 + 18 - 38 38 - 38 + 19 - - 39 39 39 +} +do_execsql_test joinB-507 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + FULL JOIN t3 USING(a) + FULL JOIN t4 USING(a) + LEFT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + - - - - - - + 11 11 - 11 11 - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-508 { + SELECT a, c, d, e, f, g + FROM t1 + FULL JOIN (t2 FULL JOIN t3 USING(a)) USING(a) + FULL JOIN (t4 LEFT JOIN t5 USING(a)) USING(a) + WHERE a<=18 + ORDER BY 1 NULLS FIRST; +} { + 11 31 - 31 31 - + 12 32 32 32 - - + 13 - 33 33 33 - + 15 35 35 35 35 35 + 17 37 - 37 - - + 18 - 38 38 - - +} +do_execsql_test joinB-509 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + FULL JOIN t3 USING(a) + FULL JOIN t4 USING(a) + RIGHT JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-510 { + SELECT a, b, c, d, e, f, g + FROM t1 + FULL JOIN t2 USING(a,b) + FULL JOIN t3 USING(a,b) + FULL JOIN t4 USING(a,b) + RIGHT JOIN t5 USING(a,b) + WHERE d<>33 OR d IS NULL + ORDER BY 1 NULLS FIRST; +} { + 15 25 35 35 35 35 35 + 17 27 37 - 37 - 37 + 18 28 - 38 38 - 38 + 19 29 - - 39 39 39 +} +do_execsql_test joinB-511 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 + FULL JOIN t2 USING(a) + FULL JOIN t3 USING(a) + FULL JOIN t4 USING(a) + FULL JOIN t5 USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + - - - - - - + 11 11 - 11 11 - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 +} +do_execsql_test joinB-512 { + SELECT b, c, d, e, f, g + FROM t1 + NATURAL FULL JOIN t2 + NATURAL FULL JOIN t3 + NATURAL FULL JOIN t4 + NATURAL FULL JOIN t5 + WHERE b BETWEEN 12 AND 17 + ORDER BY 1 NULLS FIRST; +} { +} +finish_test + +############################################################################## +# Here is the original TCL script that generated the psql input file: +# +# +# puts " +# \\pset border off +# \\pset tuples_only on +# \\pset null - +# +# DROP TABLE IF EXISTS t1; +# DROP TABLE IF EXISTS t2; +# DROP TABLE IF EXISTS t3; +# DROP TABLE IF EXISTS t4; +# DROP TABLE IF EXISTS t5; +# CREATE TABLE t1(a INT, b INT, c INT); +# CREATE TABLE t2(a INT, b INT, d INT); +# CREATE TABLE t3(a INT, b INT, e INT); +# CREATE TABLE t4(a INT, b INT, f INT); +# CREATE TABLE t5(a INT, b INT, g INT); +# INSERT INTO t1 VALUES(11,21,31),(12,22,32),(15,25,35),(17,27,37); +# INSERT INTO t2 VALUES(12,22,32),(13,23,33),(15,25,35),(18,28,38),(NULL,NULL,36); +# INSERT INTO t4 VALUES(11,21,31),(13,23,33),(15,25,35),(19,29,39); +# INSERT INTO t3 SELECT * FROM t1 UNION SELECT * FROM t2 UNION SELECT * FROM t4; +# INSERT INTO t5 SELECT * FROM t3 WHERE a>=15; +# " +# +# proc echo {prefix txt} { +# regsub -all {\n} $txt \n$prefix txt +# puts "$prefix$txt" +# } +# +# set n 0 +# set k 0 +# foreach j1 {INNER LEFT RIGHT FULL} { +# foreach j2 {INNER LEFT RIGHT FULL} { +# foreach j3 {INNER LEFT RIGHT FULL} { +# foreach j4 {INNER LEFT RIGHT FULL} { +# +# incr n +# incr k +# set q1 "" +# append q1 "SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a\n" +# append q1 " FROM t1\n" +# append q1 " $j1 JOIN t2 USING(a)\n" +# append q1 " $j2 JOIN t3 USING(a)\n" +# append q1 " $j3 JOIN t4 USING(a)\n" +# append q1 " $j4 JOIN t5 USING(a)\n" +# append q1 " ORDER BY 1 NULLS FIRST;" +# +# echo "\\qecho " "do_execsql_test joinB-$n \{" +# echo "\\qecho X " $q1 +# echo "\\qecho " "\} \{" +# puts $q1 +# echo "\\qecho " "\}" +# +# switch [expr {$k%4}] { +# 0 { +# set q2 "" +# append q2 "SELECT b, c, d, e, f, g\n" +# append q2 " FROM t1\n" +# append q2 " NATURAL $j1 JOIN t2\n" +# append q2 " NATURAL $j2 JOIN t3\n" +# append q2 " NATURAL $j3 JOIN t4\n" +# append q2 " NATURAL $j4 JOIN t5\n" +# append q2 " WHERE b BETWEEN 12 AND 17\n" +# append q2 " ORDER BY 1 NULLS FIRST;" +# incr n +# echo "\\qecho " "do_execsql_test joinB-$n \{" +# echo "\\qecho X " $q2 +# echo "\\qecho " "\} \{" +# puts $q2 +# echo "\\qecho " "\}" +# } +# 1 { +# set q2 "" +# append q2 "SELECT a, c, d, e, f, g\n" +# append q2 " FROM t1\n" +# append q2 " $j1 JOIN t2 USING(a,b)\n" +# append q2 " $j2 JOIN t3 USING(a,b)\n" +# append q2 " $j3 JOIN t4 USING(a,b)\n" +# append q2 " $j4 JOIN t5 USING(a,b)\n" +# append q2 " WHERE a<>13\n" +# append q2 " ORDER BY 1 NULLS FIRST;" +# incr n +# echo "\\qecho " "do_execsql_test joinB-$n \{" +# echo "\\qecho X " $q2 +# echo "\\qecho " "\} \{" +# puts $q2 +# echo "\\qecho " "\}" +# } +# 2 { +# set q2 "" +# append q2 "SELECT a, c, d, e, f, g\n" +# append q2 " FROM t1\n" +# append q2 " $j1 JOIN (t2 $j2 JOIN t3 USING(a)) USING(a)\n" +# append q2 " $j3 JOIN (t4 $j4 JOIN t5 USING(a)) USING(a)\n" +# append q2 " WHERE a<=18\n" +# append q2 " ORDER BY 1 NULLS FIRST;" +# incr n +# echo "\\qecho " "do_execsql_test joinB-$n \{" +# echo "\\qecho X " $q2 +# echo "\\qecho " "\} \{" +# puts $q2 +# echo "\\qecho " "\}" +# } +# 3 { +# set q2 "" +# append q2 "SELECT a, b, c, d, e, f, g\n" +# append q2 " FROM t1\n" +# append q2 " $j1 JOIN t2 USING(a,b)\n" +# append q2 " $j2 JOIN t3 USING(a,b)\n" +# append q2 " $j3 JOIN t4 USING(a,b)\n" +# append q2 " $j4 JOIN t5 USING(a,b)\n" +# append q2 " WHERE d<>33 OR d IS NULL\n" +# append q2 " ORDER BY 1 NULLS FIRST;" +# incr n +# echo "\\qecho " "do_execsql_test joinB-$n \{" +# echo "\\qecho X " $q2 +# echo "\\qecho " "\} \{" +# puts $q2 +# echo "\\qecho " "\}" +# } +# } +# +# } +# } +# } +# } +############################################################################## diff --git a/test/joinC.test b/test/joinC.test new file mode 100644 index 0000000000..a6f9395851 --- /dev/null +++ b/test/joinC.test @@ -0,0 +1,4594 @@ +# 2022-04-19 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# This file implements tests for JOINs. +# +# The test case output is all generated by PostgreSQL 14. This test module +# was created as follows: +# +# 1. Run a TCL script (included at the bottom of this file) that +# generates an input script for "psql" that will run man +# diverse tests on joins. +# +# 2. Run the script from step (1) through psql and collect the +# output. +# +# 3. Make a few minor global search-and-replace operations to convert +# the psql output into a form suitable for this test module. +# +# 4. Add this header, and the script content at the footer. +# +set testdir [file dirname $argv0] +source $testdir/tester.tcl +db nullvalue - +db eval { + DROP TABLE IF EXISTS t1; + DROP TABLE IF EXISTS t2; + DROP TABLE IF EXISTS t3; + DROP TABLE IF EXISTS t4; + DROP TABLE IF EXISTS t5; + CREATE TABLE t1(a INT, b INT, c INT); + CREATE TABLE t2(a INT, b INT, d INT); + CREATE TABLE t3(a INT, b INT, e INT); + CREATE TABLE t4(a INT, b INT, f INT); + CREATE TABLE t5(a INT, b INT, g INT); + INSERT INTO t1 VALUES(11,21,31),(12,22,32),(15,25,35),(17,27,37); + INSERT INTO t2 VALUES(12,22,32),(13,23,33),(15,25,35),(18,28,38), + (NULL,NULL,36); + INSERT INTO t4 VALUES(11,21,31),(13,23,33),(15,25,35),(19,29,39); + INSERT INTO t3 SELECT * FROM t1 UNION SELECT * FROM t2 UNION SELECT * FROM t4; + INSERT INTO t5 SELECT * FROM t3 WHERE a>=15; +} +do_execsql_test joinC-1 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 INNER JOIN ( + t3 INNER JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + +} +do_execsql_test joinC-2 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 INNER JOIN ( + t3 INNER JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + +} +do_execsql_test joinC-3 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 INNER JOIN ( + t3 INNER JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + +} +do_execsql_test joinC-4 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 INNER JOIN ( + t3 INNER JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + +} +do_execsql_test joinC-5 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 INNER JOIN ( + t3 LEFT JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 12 12 12 12 - - + 15 15 15 15 15 15 + +} +do_execsql_test joinC-6 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 INNER JOIN ( + t3 LEFT JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 12 12 12 12 - - + 15 15 15 15 15 15 + +} +do_execsql_test joinC-7 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 INNER JOIN ( + t3 LEFT JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 12 12 12 12 - - + 15 15 15 15 15 15 + +} +do_execsql_test joinC-8 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 INNER JOIN ( + t3 LEFT JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 12 12 12 12 - - + 15 15 15 15 15 15 + +} +do_execsql_test joinC-9 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 INNER JOIN ( + t3 RIGHT JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + +} +do_execsql_test joinC-10 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 INNER JOIN ( + t3 RIGHT JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + +} +do_execsql_test joinC-11 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 INNER JOIN ( + t3 RIGHT JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + +} +do_execsql_test joinC-12 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 INNER JOIN ( + t3 RIGHT JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + +} +do_execsql_test joinC-13 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 INNER JOIN ( + t3 FULL JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 12 12 12 12 - - + 15 15 15 15 15 15 + +} +do_execsql_test joinC-14 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 INNER JOIN ( + t3 FULL JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 12 12 12 12 - - + 15 15 15 15 15 15 + +} +do_execsql_test joinC-15 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 INNER JOIN ( + t3 FULL JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 12 12 12 12 - - + 15 15 15 15 15 15 + +} +do_execsql_test joinC-16 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 INNER JOIN ( + t3 FULL JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 12 12 12 12 - - + 15 15 15 15 15 15 + +} +do_execsql_test joinC-17 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 LEFT JOIN ( + t3 INNER JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 12 12 12 - - - + 15 15 15 15 15 15 + +} +do_execsql_test joinC-18 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 LEFT JOIN ( + t3 INNER JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 12 12 12 - - - + 15 15 15 15 15 15 + +} +do_execsql_test joinC-19 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 LEFT JOIN ( + t3 INNER JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 12 12 12 - - - + 15 15 15 15 15 15 + +} +do_execsql_test joinC-20 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 LEFT JOIN ( + t3 INNER JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 12 12 12 - - - + 15 15 15 15 15 15 + +} +do_execsql_test joinC-21 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 LEFT JOIN ( + t3 LEFT JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 12 12 12 12 - - + 15 15 15 15 15 15 + +} +do_execsql_test joinC-22 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 LEFT JOIN ( + t3 LEFT JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 12 12 12 12 - - + 15 15 15 15 15 15 + +} +do_execsql_test joinC-23 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 LEFT JOIN ( + t3 LEFT JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 12 12 12 12 - - + 15 15 15 15 15 15 + +} +do_execsql_test joinC-24 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 LEFT JOIN ( + t3 LEFT JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 12 12 12 12 - - + 15 15 15 15 15 15 + +} +do_execsql_test joinC-25 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 LEFT JOIN ( + t3 RIGHT JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 12 12 12 - - - + 15 15 15 15 15 15 + +} +do_execsql_test joinC-26 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 LEFT JOIN ( + t3 RIGHT JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 12 12 12 - - - + 15 15 15 15 15 15 + +} +do_execsql_test joinC-27 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 LEFT JOIN ( + t3 RIGHT JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 12 12 12 - - - + 15 15 15 15 15 15 + +} +do_execsql_test joinC-28 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 LEFT JOIN ( + t3 RIGHT JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 12 12 12 - - - + 15 15 15 15 15 15 + +} +do_execsql_test joinC-29 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 LEFT JOIN ( + t3 FULL JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 12 12 12 12 - - + 15 15 15 15 15 15 + +} +do_execsql_test joinC-30 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 LEFT JOIN ( + t3 FULL JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 12 12 12 12 - - + 15 15 15 15 15 15 + +} +do_execsql_test joinC-31 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 LEFT JOIN ( + t3 FULL JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 12 12 12 12 - - + 15 15 15 15 15 15 + +} +do_execsql_test joinC-32 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 LEFT JOIN ( + t3 FULL JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 12 12 12 12 - - + 15 15 15 15 15 15 + +} +do_execsql_test joinC-33 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 RIGHT JOIN ( + t3 INNER JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + +} +do_execsql_test joinC-34 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 RIGHT JOIN ( + t3 INNER JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 15 15 15 15 15 15 + +} +do_execsql_test joinC-35 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 RIGHT JOIN ( + t3 INNER JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 17 - 17 - 17 + +} +do_execsql_test joinC-36 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 RIGHT JOIN ( + t3 INNER JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + +} +do_execsql_test joinC-37 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 RIGHT JOIN ( + t3 LEFT JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 - - + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 17 - 17 - - + +} +do_execsql_test joinC-38 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 RIGHT JOIN ( + t3 LEFT JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 17 - 17 - - + +} +do_execsql_test joinC-39 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 RIGHT JOIN ( + t3 LEFT JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 - - + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + +} +do_execsql_test joinC-40 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 RIGHT JOIN ( + t3 LEFT JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + +} +do_execsql_test joinC-41 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 RIGHT JOIN ( + t3 RIGHT JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + +} +do_execsql_test joinC-42 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 RIGHT JOIN ( + t3 RIGHT JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 15 15 15 15 15 15 + +} +do_execsql_test joinC-43 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 RIGHT JOIN ( + t3 RIGHT JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 17 - 17 - 17 + +} +do_execsql_test joinC-44 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 RIGHT JOIN ( + t3 RIGHT JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + +} +do_execsql_test joinC-45 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 RIGHT JOIN ( + t3 FULL JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 - - + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 17 - 17 - - + +} +do_execsql_test joinC-46 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 RIGHT JOIN ( + t3 FULL JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 17 - 17 - - + +} +do_execsql_test joinC-47 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 RIGHT JOIN ( + t3 FULL JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 - - + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + +} +do_execsql_test joinC-48 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 RIGHT JOIN ( + t3 FULL JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + +} +do_execsql_test joinC-49 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 FULL JOIN ( + t3 INNER JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 12 12 12 - - - + 15 15 15 15 15 15 + +} +do_execsql_test joinC-50 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 FULL JOIN ( + t3 INNER JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 12 12 12 - - - + 15 15 15 15 15 15 + +} +do_execsql_test joinC-51 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 FULL JOIN ( + t3 INNER JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 12 12 12 - - - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + +} +do_execsql_test joinC-52 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 FULL JOIN ( + t3 INNER JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 12 12 12 - - - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + +} +do_execsql_test joinC-53 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 FULL JOIN ( + t3 LEFT JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 - - + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 17 - 17 - - + +} +do_execsql_test joinC-54 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 FULL JOIN ( + t3 LEFT JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 17 - 17 - - + +} +do_execsql_test joinC-55 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 FULL JOIN ( + t3 LEFT JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 - - + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + +} +do_execsql_test joinC-56 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 FULL JOIN ( + t3 LEFT JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + +} +do_execsql_test joinC-57 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 FULL JOIN ( + t3 RIGHT JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 12 12 12 - - - + 15 15 15 15 15 15 + +} +do_execsql_test joinC-58 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 FULL JOIN ( + t3 RIGHT JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 12 12 12 - - - + 15 15 15 15 15 15 + +} +do_execsql_test joinC-59 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 FULL JOIN ( + t3 RIGHT JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 12 12 12 - - - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + +} +do_execsql_test joinC-60 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 FULL JOIN ( + t3 RIGHT JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 12 12 12 - - - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + +} +do_execsql_test joinC-61 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 FULL JOIN ( + t3 FULL JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 - - + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 17 - 17 - - + +} +do_execsql_test joinC-62 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 FULL JOIN ( + t3 FULL JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 17 - 17 - - + +} +do_execsql_test joinC-63 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 FULL JOIN ( + t3 FULL JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 - - + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + +} +do_execsql_test joinC-64 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 INNER JOIN ( + t2 FULL JOIN ( + t3 FULL JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + +} +do_execsql_test joinC-65 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 INNER JOIN ( + t3 INNER JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 - - - - + 15 15 15 15 15 15 + 17 17 - - - - + +} +do_execsql_test joinC-66 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 INNER JOIN ( + t3 INNER JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 - - - - + 15 15 15 15 15 15 + 17 17 - - - - + +} +do_execsql_test joinC-67 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 INNER JOIN ( + t3 INNER JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 - - - - + 15 15 15 15 15 15 + 17 17 - - - - + +} +do_execsql_test joinC-68 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 INNER JOIN ( + t3 INNER JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 - - - - + 15 15 15 15 15 15 + 17 17 - - - - + +} +do_execsql_test joinC-69 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 INNER JOIN ( + t3 LEFT JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 17 - - - - + +} +do_execsql_test joinC-70 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 INNER JOIN ( + t3 LEFT JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 17 - - - - + +} +do_execsql_test joinC-71 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 INNER JOIN ( + t3 LEFT JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 17 - - - - + +} +do_execsql_test joinC-72 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 INNER JOIN ( + t3 LEFT JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 17 - - - - + +} +do_execsql_test joinC-73 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 INNER JOIN ( + t3 RIGHT JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 - - - - + 15 15 15 15 15 15 + 17 17 - - - - + +} +do_execsql_test joinC-74 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 INNER JOIN ( + t3 RIGHT JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 - - - - + 15 15 15 15 15 15 + 17 17 - - - - + +} +do_execsql_test joinC-75 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 INNER JOIN ( + t3 RIGHT JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 - - - - + 15 15 15 15 15 15 + 17 17 - - - - + +} +do_execsql_test joinC-76 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 INNER JOIN ( + t3 RIGHT JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 - - - - + 15 15 15 15 15 15 + 17 17 - - - - + +} +do_execsql_test joinC-77 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 INNER JOIN ( + t3 FULL JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 17 - - - - + +} +do_execsql_test joinC-78 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 INNER JOIN ( + t3 FULL JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 17 - - - - + +} +do_execsql_test joinC-79 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 INNER JOIN ( + t3 FULL JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 17 - - - - + +} +do_execsql_test joinC-80 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 INNER JOIN ( + t3 FULL JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 17 - - - - + +} +do_execsql_test joinC-81 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 LEFT JOIN ( + t3 INNER JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 12 - - - + 15 15 15 15 15 15 + 17 17 - - - - + +} +do_execsql_test joinC-82 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 LEFT JOIN ( + t3 INNER JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 12 - - - + 15 15 15 15 15 15 + 17 17 - - - - + +} +do_execsql_test joinC-83 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 LEFT JOIN ( + t3 INNER JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 12 - - - + 15 15 15 15 15 15 + 17 17 - - - - + +} +do_execsql_test joinC-84 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 LEFT JOIN ( + t3 INNER JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 12 - - - + 15 15 15 15 15 15 + 17 17 - - - - + +} +do_execsql_test joinC-85 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 LEFT JOIN ( + t3 LEFT JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 17 - - - - + +} +do_execsql_test joinC-86 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 LEFT JOIN ( + t3 LEFT JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 17 - - - - + +} +do_execsql_test joinC-87 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 LEFT JOIN ( + t3 LEFT JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 17 - - - - + +} +do_execsql_test joinC-88 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 LEFT JOIN ( + t3 LEFT JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 17 - - - - + +} +do_execsql_test joinC-89 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 LEFT JOIN ( + t3 RIGHT JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 12 - - - + 15 15 15 15 15 15 + 17 17 - - - - + +} +do_execsql_test joinC-90 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 LEFT JOIN ( + t3 RIGHT JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 12 - - - + 15 15 15 15 15 15 + 17 17 - - - - + +} +do_execsql_test joinC-91 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 LEFT JOIN ( + t3 RIGHT JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 12 - - - + 15 15 15 15 15 15 + 17 17 - - - - + +} +do_execsql_test joinC-92 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 LEFT JOIN ( + t3 RIGHT JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 12 - - - + 15 15 15 15 15 15 + 17 17 - - - - + +} +do_execsql_test joinC-93 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 LEFT JOIN ( + t3 FULL JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 17 - - - - + +} +do_execsql_test joinC-94 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 LEFT JOIN ( + t3 FULL JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 17 - - - - + +} +do_execsql_test joinC-95 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 LEFT JOIN ( + t3 FULL JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 17 - - - - + +} +do_execsql_test joinC-96 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 LEFT JOIN ( + t3 FULL JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 17 - - - - + +} +do_execsql_test joinC-97 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 RIGHT JOIN ( + t3 INNER JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 - - - - + 15 15 15 15 15 15 + 17 17 - - - - + +} +do_execsql_test joinC-98 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 RIGHT JOIN ( + t3 INNER JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 12 12 - - - - + 15 15 15 15 15 15 + 17 17 - - - - + +} +do_execsql_test joinC-99 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 RIGHT JOIN ( + t3 INNER JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 - - - - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + +} +do_execsql_test joinC-100 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 RIGHT JOIN ( + t3 INNER JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 12 12 - - - - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + +} +do_execsql_test joinC-101 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 RIGHT JOIN ( + t3 LEFT JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 - - + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 17 - 17 - - + +} +do_execsql_test joinC-102 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 RIGHT JOIN ( + t3 LEFT JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 17 - 17 - - + +} +do_execsql_test joinC-103 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 RIGHT JOIN ( + t3 LEFT JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 - - + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + +} +do_execsql_test joinC-104 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 RIGHT JOIN ( + t3 LEFT JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + +} +do_execsql_test joinC-105 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 RIGHT JOIN ( + t3 RIGHT JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 - - - - + 15 15 15 15 15 15 + 17 17 - - - - + +} +do_execsql_test joinC-106 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 RIGHT JOIN ( + t3 RIGHT JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 12 12 - - - - + 15 15 15 15 15 15 + 17 17 - - - - + +} +do_execsql_test joinC-107 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 RIGHT JOIN ( + t3 RIGHT JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 - - - - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + +} +do_execsql_test joinC-108 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 RIGHT JOIN ( + t3 RIGHT JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 12 12 - - - - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + +} +do_execsql_test joinC-109 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 RIGHT JOIN ( + t3 FULL JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 - - + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 17 - 17 - - + +} +do_execsql_test joinC-110 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 RIGHT JOIN ( + t3 FULL JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 17 - 17 - - + +} +do_execsql_test joinC-111 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 RIGHT JOIN ( + t3 FULL JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 - - + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + +} +do_execsql_test joinC-112 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 RIGHT JOIN ( + t3 FULL JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + +} +do_execsql_test joinC-113 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 FULL JOIN ( + t3 INNER JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 12 - - - + 15 15 15 15 15 15 + 17 17 - - - - + +} +do_execsql_test joinC-114 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 FULL JOIN ( + t3 INNER JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 12 12 12 - - - + 15 15 15 15 15 15 + 17 17 - - - - + +} +do_execsql_test joinC-115 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 FULL JOIN ( + t3 INNER JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 12 - - - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + +} +do_execsql_test joinC-116 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 FULL JOIN ( + t3 INNER JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 12 12 12 - - - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + +} +do_execsql_test joinC-117 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 FULL JOIN ( + t3 LEFT JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 - - + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 17 - 17 - - + +} +do_execsql_test joinC-118 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 FULL JOIN ( + t3 LEFT JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 17 - 17 - - + +} +do_execsql_test joinC-119 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 FULL JOIN ( + t3 LEFT JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 - - + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + +} +do_execsql_test joinC-120 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 FULL JOIN ( + t3 LEFT JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + +} +do_execsql_test joinC-121 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 FULL JOIN ( + t3 RIGHT JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 12 - - - + 15 15 15 15 15 15 + 17 17 - - - - + +} +do_execsql_test joinC-122 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 FULL JOIN ( + t3 RIGHT JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 12 12 12 - - - + 15 15 15 15 15 15 + 17 17 - - - - + +} +do_execsql_test joinC-123 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 FULL JOIN ( + t3 RIGHT JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 12 - - - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + +} +do_execsql_test joinC-124 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 FULL JOIN ( + t3 RIGHT JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 12 12 12 - - - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + +} +do_execsql_test joinC-125 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 FULL JOIN ( + t3 FULL JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 - - + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 17 - 17 - - + +} +do_execsql_test joinC-126 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 FULL JOIN ( + t3 FULL JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 17 - 17 - - + +} +do_execsql_test joinC-127 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 FULL JOIN ( + t3 FULL JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 - - + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + +} +do_execsql_test joinC-128 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 LEFT JOIN ( + t2 FULL JOIN ( + t3 FULL JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 12 12 12 12 - - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + +} +do_execsql_test joinC-129 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 INNER JOIN ( + t3 INNER JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + +} +do_execsql_test joinC-130 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 INNER JOIN ( + t3 INNER JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 13 - 13 13 13 - + 15 15 15 15 15 15 + +} +do_execsql_test joinC-131 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 INNER JOIN ( + t3 INNER JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 18 - 18 18 - 18 + +} +do_execsql_test joinC-132 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 INNER JOIN ( + t3 INNER JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 13 - 13 13 13 - + 15 15 15 15 15 15 + 18 - 18 18 - 18 + +} +do_execsql_test joinC-133 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 INNER JOIN ( + t3 LEFT JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 12 12 12 12 - - + 13 - 13 13 - - + 15 15 15 15 15 15 + 18 - 18 18 - - + +} +do_execsql_test joinC-134 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 INNER JOIN ( + t3 LEFT JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 18 - 18 18 - - + +} +do_execsql_test joinC-135 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 INNER JOIN ( + t3 LEFT JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 12 12 12 12 - - + 13 - 13 13 - - + 15 15 15 15 15 15 + 18 - 18 18 - 18 + +} +do_execsql_test joinC-136 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 INNER JOIN ( + t3 LEFT JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 18 - 18 18 - 18 + +} +do_execsql_test joinC-137 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 INNER JOIN ( + t3 RIGHT JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + +} +do_execsql_test joinC-138 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 INNER JOIN ( + t3 RIGHT JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 13 - 13 13 13 - + 15 15 15 15 15 15 + +} +do_execsql_test joinC-139 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 INNER JOIN ( + t3 RIGHT JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 18 - 18 18 - 18 + +} +do_execsql_test joinC-140 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 INNER JOIN ( + t3 RIGHT JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 13 - 13 13 13 - + 15 15 15 15 15 15 + 18 - 18 18 - 18 + +} +do_execsql_test joinC-141 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 INNER JOIN ( + t3 FULL JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 12 12 12 12 - - + 13 - 13 13 - - + 15 15 15 15 15 15 + 18 - 18 18 - - + +} +do_execsql_test joinC-142 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 INNER JOIN ( + t3 FULL JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 18 - 18 18 - - + +} +do_execsql_test joinC-143 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 INNER JOIN ( + t3 FULL JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 12 12 12 12 - - + 13 - 13 13 - - + 15 15 15 15 15 15 + 18 - 18 18 - 18 + +} +do_execsql_test joinC-144 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 INNER JOIN ( + t3 FULL JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 18 - 18 18 - 18 + +} +do_execsql_test joinC-145 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 LEFT JOIN ( + t3 INNER JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 12 12 12 - - - + 13 - 13 - - - + 15 15 15 15 15 15 + 18 - 18 - - - + +} +do_execsql_test joinC-146 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 LEFT JOIN ( + t3 INNER JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 12 12 12 - - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 18 - 18 - - - + +} +do_execsql_test joinC-147 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 LEFT JOIN ( + t3 INNER JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 12 12 12 - - - + 13 - 13 - - - + 15 15 15 15 15 15 + 18 - 18 18 - 18 + +} +do_execsql_test joinC-148 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 LEFT JOIN ( + t3 INNER JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 12 12 12 - - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 18 - 18 18 - 18 + +} +do_execsql_test joinC-149 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 LEFT JOIN ( + t3 LEFT JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 12 12 12 12 - - + 13 - 13 13 - - + 15 15 15 15 15 15 + 18 - 18 18 - - + +} +do_execsql_test joinC-150 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 LEFT JOIN ( + t3 LEFT JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 18 - 18 18 - - + +} +do_execsql_test joinC-151 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 LEFT JOIN ( + t3 LEFT JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 12 12 12 12 - - + 13 - 13 13 - - + 15 15 15 15 15 15 + 18 - 18 18 - 18 + +} +do_execsql_test joinC-152 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 LEFT JOIN ( + t3 LEFT JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 18 - 18 18 - 18 + +} +do_execsql_test joinC-153 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 LEFT JOIN ( + t3 RIGHT JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 12 12 12 - - - + 13 - 13 - - - + 15 15 15 15 15 15 + 18 - 18 - - - + +} +do_execsql_test joinC-154 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 LEFT JOIN ( + t3 RIGHT JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 12 12 12 - - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 18 - 18 - - - + +} +do_execsql_test joinC-155 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 LEFT JOIN ( + t3 RIGHT JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 12 12 12 - - - + 13 - 13 - - - + 15 15 15 15 15 15 + 18 - 18 18 - 18 + +} +do_execsql_test joinC-156 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 LEFT JOIN ( + t3 RIGHT JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 12 12 12 - - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 18 - 18 18 - 18 + +} +do_execsql_test joinC-157 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 LEFT JOIN ( + t3 FULL JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 12 12 12 12 - - + 13 - 13 13 - - + 15 15 15 15 15 15 + 18 - 18 18 - - + +} +do_execsql_test joinC-158 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 LEFT JOIN ( + t3 FULL JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 18 - 18 18 - - + +} +do_execsql_test joinC-159 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 LEFT JOIN ( + t3 FULL JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 12 12 12 12 - - + 13 - 13 13 - - + 15 15 15 15 15 15 + 18 - 18 18 - 18 + +} +do_execsql_test joinC-160 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 LEFT JOIN ( + t3 FULL JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 18 - 18 18 - 18 + +} +do_execsql_test joinC-161 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 RIGHT JOIN ( + t3 INNER JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 19 - - 19 19 19 + +} +do_execsql_test joinC-162 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 RIGHT JOIN ( + t3 INNER JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 19 - - 19 19 19 + +} +do_execsql_test joinC-163 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 RIGHT JOIN ( + t3 INNER JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 + +} +do_execsql_test joinC-164 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 RIGHT JOIN ( + t3 INNER JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 + +} +do_execsql_test joinC-165 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 RIGHT JOIN ( + t3 LEFT JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - 11 - - + 12 12 12 12 - - + 13 - 13 13 - - + 15 15 15 15 15 15 + 17 17 - 17 - - + 18 - 18 18 - - + 19 - - 19 19 19 + +} +do_execsql_test joinC-166 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 RIGHT JOIN ( + t3 LEFT JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - 11 11 - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - 17 - - + 18 - 18 18 - - + 19 - - 19 19 19 + +} +do_execsql_test joinC-167 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 RIGHT JOIN ( + t3 LEFT JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - 11 - - + 12 12 12 12 - - + 13 - 13 13 - - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 + +} +do_execsql_test joinC-168 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 RIGHT JOIN ( + t3 LEFT JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - 11 11 - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 + +} +do_execsql_test joinC-169 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 RIGHT JOIN ( + t3 RIGHT JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 19 - - 19 19 19 + +} +do_execsql_test joinC-170 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 RIGHT JOIN ( + t3 RIGHT JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 19 - - 19 19 19 + +} +do_execsql_test joinC-171 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 RIGHT JOIN ( + t3 RIGHT JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 + +} +do_execsql_test joinC-172 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 RIGHT JOIN ( + t3 RIGHT JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 + +} +do_execsql_test joinC-173 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 RIGHT JOIN ( + t3 FULL JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - 11 - - + 12 12 12 12 - - + 13 - 13 13 - - + 15 15 15 15 15 15 + 17 17 - 17 - - + 18 - 18 18 - - + 19 - - 19 19 19 + +} +do_execsql_test joinC-174 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 RIGHT JOIN ( + t3 FULL JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - 11 11 - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - 17 - - + 18 - 18 18 - - + 19 - - 19 19 19 + +} +do_execsql_test joinC-175 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 RIGHT JOIN ( + t3 FULL JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - 11 - - + 12 12 12 12 - - + 13 - 13 13 - - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 + +} +do_execsql_test joinC-176 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 RIGHT JOIN ( + t3 FULL JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - 11 11 - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 + +} +do_execsql_test joinC-177 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 FULL JOIN ( + t3 INNER JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 12 12 12 - - - + 13 - 13 - - - + 15 15 15 15 15 15 + 18 - 18 - - - + 19 - - 19 19 19 + +} +do_execsql_test joinC-178 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 FULL JOIN ( + t3 INNER JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - 11 11 - + 12 12 12 - - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 18 - 18 - - - + 19 - - 19 19 19 + +} +do_execsql_test joinC-179 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 FULL JOIN ( + t3 INNER JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 12 12 12 - - - + 13 - 13 - - - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 + +} +do_execsql_test joinC-180 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 FULL JOIN ( + t3 INNER JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - 11 11 - + 12 12 12 - - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 + +} +do_execsql_test joinC-181 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 FULL JOIN ( + t3 LEFT JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + - - - - - - + 11 11 - 11 - - + 12 12 12 12 - - + 13 - 13 13 - - + 15 15 15 15 15 15 + 17 17 - 17 - - + 18 - 18 18 - - + 19 - - 19 19 19 + +} +do_execsql_test joinC-182 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 FULL JOIN ( + t3 LEFT JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + - - - - - - + 11 11 - 11 11 - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - 17 - - + 18 - 18 18 - - + 19 - - 19 19 19 + +} +do_execsql_test joinC-183 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 FULL JOIN ( + t3 LEFT JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + - - - - - - + 11 11 - 11 - - + 12 12 12 12 - - + 13 - 13 13 - - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 + +} +do_execsql_test joinC-184 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 FULL JOIN ( + t3 LEFT JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + - - - - - - + 11 11 - 11 11 - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 + +} +do_execsql_test joinC-185 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 FULL JOIN ( + t3 RIGHT JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 12 12 12 - - - + 13 - 13 - - - + 15 15 15 15 15 15 + 18 - 18 - - - + 19 - - 19 19 19 + +} +do_execsql_test joinC-186 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 FULL JOIN ( + t3 RIGHT JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - 11 11 - + 12 12 12 - - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 18 - 18 - - - + 19 - - 19 19 19 + +} +do_execsql_test joinC-187 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 FULL JOIN ( + t3 RIGHT JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 12 12 12 - - - + 13 - 13 - - - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 + +} +do_execsql_test joinC-188 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 FULL JOIN ( + t3 RIGHT JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - 11 11 - + 12 12 12 - - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 + +} +do_execsql_test joinC-189 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 FULL JOIN ( + t3 FULL JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + - - - - - - + 11 11 - 11 - - + 12 12 12 12 - - + 13 - 13 13 - - + 15 15 15 15 15 15 + 17 17 - 17 - - + 18 - 18 18 - - + 19 - - 19 19 19 + +} +do_execsql_test joinC-190 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 FULL JOIN ( + t3 FULL JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + - - - - - - + 11 11 - 11 11 - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - 17 - - + 18 - 18 18 - - + 19 - - 19 19 19 + +} +do_execsql_test joinC-191 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 FULL JOIN ( + t3 FULL JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + - - - - - - + 11 11 - 11 - - + 12 12 12 12 - - + 13 - 13 13 - - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 + +} +do_execsql_test joinC-192 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 RIGHT JOIN ( + t2 FULL JOIN ( + t3 FULL JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + - - - - - - + 11 11 - 11 11 - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 + +} +do_execsql_test joinC-193 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 INNER JOIN ( + t3 INNER JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 - - - - + 15 15 15 15 15 15 + 17 17 - - - - + +} +do_execsql_test joinC-194 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 INNER JOIN ( + t3 INNER JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 - - - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - - - - + +} +do_execsql_test joinC-195 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 INNER JOIN ( + t3 INNER JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 - - - - + 15 15 15 15 15 15 + 17 17 - - - - + 18 - 18 18 - 18 + +} +do_execsql_test joinC-196 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 INNER JOIN ( + t3 INNER JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 - - - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - - - - + 18 - 18 18 - 18 + +} +do_execsql_test joinC-197 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 INNER JOIN ( + t3 LEFT JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 12 12 - - + 13 - 13 13 - - + 15 15 15 15 15 15 + 17 17 - - - - + 18 - 18 18 - - + +} +do_execsql_test joinC-198 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 INNER JOIN ( + t3 LEFT JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - - - - + 18 - 18 18 - - + +} +do_execsql_test joinC-199 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 INNER JOIN ( + t3 LEFT JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 12 12 - - + 13 - 13 13 - - + 15 15 15 15 15 15 + 17 17 - - - - + 18 - 18 18 - 18 + +} +do_execsql_test joinC-200 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 INNER JOIN ( + t3 LEFT JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - - - - + 18 - 18 18 - 18 + +} +do_execsql_test joinC-201 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 INNER JOIN ( + t3 RIGHT JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 - - - - + 15 15 15 15 15 15 + 17 17 - - - - + +} +do_execsql_test joinC-202 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 INNER JOIN ( + t3 RIGHT JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 - - - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - - - - + +} +do_execsql_test joinC-203 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 INNER JOIN ( + t3 RIGHT JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 - - - - + 15 15 15 15 15 15 + 17 17 - - - - + 18 - 18 18 - 18 + +} +do_execsql_test joinC-204 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 INNER JOIN ( + t3 RIGHT JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 - - - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - - - - + 18 - 18 18 - 18 + +} +do_execsql_test joinC-205 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 INNER JOIN ( + t3 FULL JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 12 12 - - + 13 - 13 13 - - + 15 15 15 15 15 15 + 17 17 - - - - + 18 - 18 18 - - + +} +do_execsql_test joinC-206 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 INNER JOIN ( + t3 FULL JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - - - - + 18 - 18 18 - - + +} +do_execsql_test joinC-207 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 INNER JOIN ( + t3 FULL JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 12 12 - - + 13 - 13 13 - - + 15 15 15 15 15 15 + 17 17 - - - - + 18 - 18 18 - 18 + +} +do_execsql_test joinC-208 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 INNER JOIN ( + t3 FULL JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - - - - + 18 - 18 18 - 18 + +} +do_execsql_test joinC-209 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 LEFT JOIN ( + t3 INNER JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - - - - + 12 12 12 - - - + 13 - 13 - - - + 15 15 15 15 15 15 + 17 17 - - - - + 18 - 18 - - - + +} +do_execsql_test joinC-210 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 LEFT JOIN ( + t3 INNER JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - - - - + 12 12 12 - - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - - - - + 18 - 18 - - - + +} +do_execsql_test joinC-211 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 LEFT JOIN ( + t3 INNER JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - - - - + 12 12 12 - - - + 13 - 13 - - - + 15 15 15 15 15 15 + 17 17 - - - - + 18 - 18 18 - 18 + +} +do_execsql_test joinC-212 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 LEFT JOIN ( + t3 INNER JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - - - - + 12 12 12 - - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - - - - + 18 - 18 18 - 18 + +} +do_execsql_test joinC-213 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 LEFT JOIN ( + t3 LEFT JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - - - - + 12 12 12 12 - - + 13 - 13 13 - - + 15 15 15 15 15 15 + 17 17 - - - - + 18 - 18 18 - - + +} +do_execsql_test joinC-214 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 LEFT JOIN ( + t3 LEFT JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - - - - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - - - - + 18 - 18 18 - - + +} +do_execsql_test joinC-215 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 LEFT JOIN ( + t3 LEFT JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - - - - + 12 12 12 12 - - + 13 - 13 13 - - + 15 15 15 15 15 15 + 17 17 - - - - + 18 - 18 18 - 18 + +} +do_execsql_test joinC-216 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 LEFT JOIN ( + t3 LEFT JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - - - - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - - - - + 18 - 18 18 - 18 + +} +do_execsql_test joinC-217 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 LEFT JOIN ( + t3 RIGHT JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - - - - + 12 12 12 - - - + 13 - 13 - - - + 15 15 15 15 15 15 + 17 17 - - - - + 18 - 18 - - - + +} +do_execsql_test joinC-218 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 LEFT JOIN ( + t3 RIGHT JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - - - - + 12 12 12 - - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - - - - + 18 - 18 - - - + +} +do_execsql_test joinC-219 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 LEFT JOIN ( + t3 RIGHT JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - - - - + 12 12 12 - - - + 13 - 13 - - - + 15 15 15 15 15 15 + 17 17 - - - - + 18 - 18 18 - 18 + +} +do_execsql_test joinC-220 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 LEFT JOIN ( + t3 RIGHT JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - - - - + 12 12 12 - - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - - - - + 18 - 18 18 - 18 + +} +do_execsql_test joinC-221 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 LEFT JOIN ( + t3 FULL JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - - - - + 12 12 12 12 - - + 13 - 13 13 - - + 15 15 15 15 15 15 + 17 17 - - - - + 18 - 18 18 - - + +} +do_execsql_test joinC-222 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 LEFT JOIN ( + t3 FULL JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - - - - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - - - - + 18 - 18 18 - - + +} +do_execsql_test joinC-223 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 LEFT JOIN ( + t3 FULL JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - - - - + 12 12 12 12 - - + 13 - 13 13 - - + 15 15 15 15 15 15 + 17 17 - - - - + 18 - 18 18 - 18 + +} +do_execsql_test joinC-224 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 LEFT JOIN ( + t3 FULL JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - - - - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - - - - + 18 - 18 18 - 18 + +} +do_execsql_test joinC-225 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 RIGHT JOIN ( + t3 INNER JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 - - - - + 15 15 15 15 15 15 + 17 17 - - - - + 19 - - 19 19 19 + +} +do_execsql_test joinC-226 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 RIGHT JOIN ( + t3 INNER JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 12 12 - - - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - - - - + 19 - - 19 19 19 + +} +do_execsql_test joinC-227 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 RIGHT JOIN ( + t3 INNER JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 - - - - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 + +} +do_execsql_test joinC-228 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 RIGHT JOIN ( + t3 INNER JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 12 12 - - - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 + +} +do_execsql_test joinC-229 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 RIGHT JOIN ( + t3 LEFT JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - 11 - - + 12 12 12 12 - - + 13 - 13 13 - - + 15 15 15 15 15 15 + 17 17 - 17 - - + 18 - 18 18 - - + 19 - - 19 19 19 + +} +do_execsql_test joinC-230 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 RIGHT JOIN ( + t3 LEFT JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - 11 11 - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - 17 - - + 18 - 18 18 - - + 19 - - 19 19 19 + +} +do_execsql_test joinC-231 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 RIGHT JOIN ( + t3 LEFT JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - 11 - - + 12 12 12 12 - - + 13 - 13 13 - - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 + +} +do_execsql_test joinC-232 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 RIGHT JOIN ( + t3 LEFT JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - 11 11 - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 + +} +do_execsql_test joinC-233 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 RIGHT JOIN ( + t3 RIGHT JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 - - - - + 15 15 15 15 15 15 + 17 17 - - - - + 19 - - 19 19 19 + +} +do_execsql_test joinC-234 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 RIGHT JOIN ( + t3 RIGHT JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 12 12 - - - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - - - - + 19 - - 19 19 19 + +} +do_execsql_test joinC-235 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 RIGHT JOIN ( + t3 RIGHT JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - - - - + 12 12 - - - - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 + +} +do_execsql_test joinC-236 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 RIGHT JOIN ( + t3 RIGHT JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + 11 11 - 11 11 - + 12 12 - - - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 + +} +do_execsql_test joinC-237 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 RIGHT JOIN ( + t3 FULL JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - 11 - - + 12 12 12 12 - - + 13 - 13 13 - - + 15 15 15 15 15 15 + 17 17 - 17 - - + 18 - 18 18 - - + 19 - - 19 19 19 + +} +do_execsql_test joinC-238 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 RIGHT JOIN ( + t3 FULL JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - 11 11 - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - 17 - - + 18 - 18 18 - - + 19 - - 19 19 19 + +} +do_execsql_test joinC-239 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 RIGHT JOIN ( + t3 FULL JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - 11 - - + 12 12 12 12 - - + 13 - 13 13 - - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 + +} +do_execsql_test joinC-240 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 RIGHT JOIN ( + t3 FULL JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - 11 11 - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 + +} +do_execsql_test joinC-241 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 FULL JOIN ( + t3 INNER JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - - - - + 12 12 12 - - - + 13 - 13 - - - + 15 15 15 15 15 15 + 17 17 - - - - + 18 - 18 - - - + 19 - - 19 19 19 + +} +do_execsql_test joinC-242 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 FULL JOIN ( + t3 INNER JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - 11 11 - + 12 12 12 - - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - - - - + 18 - 18 - - - + 19 - - 19 19 19 + +} +do_execsql_test joinC-243 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 FULL JOIN ( + t3 INNER JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - - - - + 12 12 12 - - - + 13 - 13 - - - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 + +} +do_execsql_test joinC-244 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 FULL JOIN ( + t3 INNER JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - 11 11 - + 12 12 12 - - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 + +} +do_execsql_test joinC-245 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 FULL JOIN ( + t3 LEFT JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + - - - - - - + 11 11 - 11 - - + 12 12 12 12 - - + 13 - 13 13 - - + 15 15 15 15 15 15 + 17 17 - 17 - - + 18 - 18 18 - - + 19 - - 19 19 19 + +} +do_execsql_test joinC-246 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 FULL JOIN ( + t3 LEFT JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + - - - - - - + 11 11 - 11 11 - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - 17 - - + 18 - 18 18 - - + 19 - - 19 19 19 + +} +do_execsql_test joinC-247 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 FULL JOIN ( + t3 LEFT JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + - - - - - - + 11 11 - 11 - - + 12 12 12 12 - - + 13 - 13 13 - - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 + +} +do_execsql_test joinC-248 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 FULL JOIN ( + t3 LEFT JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + - - - - - - + 11 11 - 11 11 - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 + +} +do_execsql_test joinC-249 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 FULL JOIN ( + t3 RIGHT JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - - - - + 12 12 12 - - - + 13 - 13 - - - + 15 15 15 15 15 15 + 17 17 - - - - + 18 - 18 - - - + 19 - - 19 19 19 + +} +do_execsql_test joinC-250 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 FULL JOIN ( + t3 RIGHT JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - 11 11 - + 12 12 12 - - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - - - - + 18 - 18 - - - + 19 - - 19 19 19 + +} +do_execsql_test joinC-251 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 FULL JOIN ( + t3 RIGHT JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - - - - + 12 12 12 - - - + 13 - 13 - - - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 + +} +do_execsql_test joinC-252 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 FULL JOIN ( + t3 RIGHT JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + 11 11 - 11 11 - + 12 12 12 - - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 + +} +do_execsql_test joinC-253 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 FULL JOIN ( + t3 FULL JOIN ( + t4 INNER JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + - - - - - - + 11 11 - 11 - - + 12 12 12 12 - - + 13 - 13 13 - - + 15 15 15 15 15 15 + 17 17 - 17 - - + 18 - 18 18 - - + 19 - - 19 19 19 + +} +do_execsql_test joinC-254 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 FULL JOIN ( + t3 FULL JOIN ( + t4 LEFT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + - - - - - - + 11 11 - 11 11 - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - 17 - - + 18 - 18 18 - - + 19 - - 19 19 19 + +} +do_execsql_test joinC-255 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 FULL JOIN ( + t3 FULL JOIN ( + t4 RIGHT JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + - - - - - - + 11 11 - 11 - - + 12 12 12 12 - - + 13 - 13 13 - - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 + +} +do_execsql_test joinC-256 { + SELECT a, t1.a, t2.a, t3.a, t4.a, t5.a + FROM t1 FULL JOIN ( + t2 FULL JOIN ( + t3 FULL JOIN ( + t4 FULL JOIN t5 USING(a) + ) USING(a) + ) USING(a) + ) USING(a) + ORDER BY 1 NULLS FIRST; +} { + - - - - - - + - - - - - - + 11 11 - 11 11 - + 12 12 12 12 - - + 13 - 13 13 13 - + 15 15 15 15 15 15 + 17 17 - 17 - 17 + 18 - 18 18 - 18 + 19 - - 19 19 19 + +} +finish_test diff --git a/test/joinD.test b/test/joinD.test new file mode 100644 index 0000000000..e4e4553b6c --- /dev/null +++ b/test/joinD.test @@ -0,0 +1,35136 @@ +# 2022-05-04 +# revised 2022-05-31 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# TESTRUNNER: slow +# +# This file implements tests for JOINs that use Bloom filters. +# +# The test case output is (mostly) all generated by PostgreSQL 14. This +# test module was created as follows: +# +# 1. Run a TCL script (included at the bottom of this file) that +# generates an input script for "psql" that will run man +# diverse tests on joins. +# +# 2. Run the script from step (1) through psql and collect the +# output. +# +# 3. Make a few minor global search-and-replace operations to convert +# the psql output into a form suitable for this test module. +# +# 4. Add this header, and the script content at the footer. +# +# A few extra tests that were not generated from postgresql output are +# added at the end. +# +set testdir [file dirname $argv0] +source $testdir/tester.tcl +db nullvalue - +db eval { + CREATE TABLE t1(a INT, b INT, c INT, d INT); + WITH RECURSIVE c(x) AS (VALUES(0) UNION ALL SELECT x+1 FROM c WHERE x<95) + INSERT INTO t1(a,b,c,d) SELECT x, x+100, x+200, x+300 FROM c; + CREATE TABLE t2(b INT, x INT); + INSERT INTO t2(b,x) SELECT b, a FROM t1 WHERE a%2=0; + CREATE INDEX t2b ON t2(b); + CREATE TABLE t3(c INT, y INT); + INSERT INTO t3(c,y) SELECT c, a FROM t1 WHERE a%3=0; + CREATE INDEX t3c ON t3(c); + CREATE TABLE t4(d INT, z INT); + INSERT INTO t4(d,z) SELECT d, a FROM t1 WHERE a%5=0; + CREATE INDEX t4d ON t4(d); + INSERT INTO t1(a,b,c,d) VALUES + (96,NULL,296,396), + (97,197,NULL,397), + (98,198,298,NULL), + (99,NULL,NULL,NULL); + ANALYZE; +} +do_execsql_test joinD-1 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-2 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-3 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-4 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-5 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON true + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-6 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-7 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-8 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-9 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-10 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-11 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-12 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-13 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-14 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-15 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t2.x>0 + INNER JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-16 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t2.x>0 + INNER JOIN t3 ON t3.y>0 + INNER JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.c IS NOT DISTINCT FROM t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-17 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t2.x>0 + INNER JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + INNER JOIN t4 ON t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.d IS NOT DISTINCT FROM t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-18 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b IN (t2.b,-2,-3) AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-19 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c IN (-4,t3.c,-5) AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-20 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-21 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-22 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-23 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-24 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON true + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-25 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-26 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-27 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-28 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-29 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-30 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-31 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-32 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-33 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-34 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t2.x>0 + INNER JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-35 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t2.x>0 + INNER JOIN t3 ON t3.y>0 + LEFT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.c IS NOT DISTINCT FROM t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-36 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t2.x>0 + INNER JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + LEFT JOIN t4 ON t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.d IS NOT DISTINCT FROM t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-37 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b IN (t2.b,-2,-3) AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-38 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c IN (-4,t3.c,-5) AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-39 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-40 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-41 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-42 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-43 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON true + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-44 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-45 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-46 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-47 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-48 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-49 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-50 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-51 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-52 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-53 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t2.x>0 + INNER JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-54 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t2.x>0 + INNER JOIN t3 ON t3.y>0 + RIGHT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.c IS NOT DISTINCT FROM t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 +} +do_execsql_test joinD-55 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t2.x>0 + INNER JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + RIGHT JOIN t4 ON t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.d IS NOT DISTINCT FROM t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-56 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b IN (t2.b,-2,-3) AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-57 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c IN (-4,t3.c,-5) AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-58 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-59 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-60 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-61 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-62 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON true + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-63 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-64 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-65 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-66 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-67 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-68 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-69 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-70 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-71 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d = t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-72 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t2.x>0 + INNER JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d = t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-73 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t2.x>0 + INNER JOIN t3 ON t3.y>0 + FULL JOIN t4 ON t1.d = t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.c IS NOT DISTINCT FROM t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 +} +do_execsql_test joinD-74 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t2.x>0 + INNER JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + FULL JOIN t4 ON t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.d = t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-75 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b IN (t2.b,-2,-3) AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-76 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c IN (-4,t3.c,-5) AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-77 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-78 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-79 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-80 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-81 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON true + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-82 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-83 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-84 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-85 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-86 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-87 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-88 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-89 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-90 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-91 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t2.x>0 + LEFT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-92 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t2.x>0 + LEFT JOIN t3 ON t3.y>0 + INNER JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.c IS NOT DISTINCT FROM t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-93 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t2.x>0 + LEFT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + INNER JOIN t4 ON t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.d IS NOT DISTINCT FROM t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-94 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b IN (t2.b,-2,-3) AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-95 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c IN (-4,t3.c,-5) AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-96 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - +} +do_execsql_test joinD-97 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - +} +do_execsql_test joinD-98 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - +} +do_execsql_test joinD-99 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - +} +do_execsql_test joinD-100 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON true + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - +} +do_execsql_test joinD-101 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-102 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - +} +do_execsql_test joinD-103 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-104 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - +} +do_execsql_test joinD-105 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-106 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-107 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-108 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-109 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - +} +do_execsql_test joinD-110 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t2.x>0 + LEFT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - +} +do_execsql_test joinD-111 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t2.x>0 + LEFT JOIN t3 ON t3.y>0 + LEFT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.c IS NOT DISTINCT FROM t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-112 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t2.x>0 + LEFT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + LEFT JOIN t4 ON t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.d IS NOT DISTINCT FROM t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-113 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b IN (t2.b,-2,-3) AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - +} +do_execsql_test joinD-114 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c IN (-4,t3.c,-5) AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - +} +do_execsql_test joinD-115 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-116 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-117 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-118 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-119 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON true + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-120 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-121 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-122 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-123 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-124 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-125 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-126 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-127 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-128 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-129 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t2.x>0 + LEFT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 +} +do_execsql_test joinD-130 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t2.x>0 + LEFT JOIN t3 ON t3.y>0 + RIGHT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.c IS NOT DISTINCT FROM t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 +} +do_execsql_test joinD-131 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t2.x>0 + LEFT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + RIGHT JOIN t4 ON t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.d IS NOT DISTINCT FROM t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-132 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b IN (t2.b,-2,-3) AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-133 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c IN (-4,t3.c,-5) AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-134 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-135 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-136 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - +} +do_execsql_test joinD-137 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-138 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON true + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - +} +do_execsql_test joinD-139 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-140 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-141 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-142 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-143 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-144 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-145 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-146 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-147 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d = t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-148 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t2.x>0 + LEFT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d = t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - - - - - 300 0 +} +do_execsql_test joinD-149 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t2.x>0 + LEFT JOIN t3 ON t3.y>0 + FULL JOIN t4 ON t1.d = t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.c IS NOT DISTINCT FROM t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 +} +do_execsql_test joinD-150 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t2.x>0 + LEFT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + FULL JOIN t4 ON t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.d = t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-151 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b IN (t2.b,-2,-3) AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-152 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c IN (-4,t3.c,-5) AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-153 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-154 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-155 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-156 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-157 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON true + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-158 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-159 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-160 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-161 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-162 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-163 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-164 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-165 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-166 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-167 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t2.x>0 + RIGHT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-168 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t2.x>0 + RIGHT JOIN t3 ON t3.y>0 + INNER JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.c IS NOT DISTINCT FROM t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-169 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t2.x>0 + RIGHT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + INNER JOIN t4 ON t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.d IS NOT DISTINCT FROM t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-170 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b IN (t2.b,-2,-3) AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-171 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c IN (-4,t3.c,-5) AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-172 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - +} +do_execsql_test joinD-173 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - +} +do_execsql_test joinD-174 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-175 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - +} +do_execsql_test joinD-176 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON true + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-177 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - +} +do_execsql_test joinD-178 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - +} +do_execsql_test joinD-179 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-180 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - +} +do_execsql_test joinD-181 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-182 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-183 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-184 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-185 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - +} +do_execsql_test joinD-186 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t2.x>0 + RIGHT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - 200 0 - - +} +do_execsql_test joinD-187 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t2.x>0 + RIGHT JOIN t3 ON t3.y>0 + LEFT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.c IS NOT DISTINCT FROM t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-188 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t2.x>0 + RIGHT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + LEFT JOIN t4 ON t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.d IS NOT DISTINCT FROM t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-189 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b IN (t2.b,-2,-3) AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - +} +do_execsql_test joinD-190 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c IN (-4,t3.c,-5) AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - +} +do_execsql_test joinD-191 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-192 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-193 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-194 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-195 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON true + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-196 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-197 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-198 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-199 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-200 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-201 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-202 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-203 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-204 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-205 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t2.x>0 + RIGHT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-206 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t2.x>0 + RIGHT JOIN t3 ON t3.y>0 + RIGHT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.c IS NOT DISTINCT FROM t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 +} +do_execsql_test joinD-207 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t2.x>0 + RIGHT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + RIGHT JOIN t4 ON t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.d IS NOT DISTINCT FROM t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-208 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b IN (t2.b,-2,-3) AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-209 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c IN (-4,t3.c,-5) AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-210 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-211 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-212 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-213 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-214 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON true + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-215 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - +} +do_execsql_test joinD-216 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-217 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-218 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-219 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-220 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-221 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-222 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-223 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d = t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-224 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t2.x>0 + RIGHT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d = t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - 200 0 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-225 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t2.x>0 + RIGHT JOIN t3 ON t3.y>0 + FULL JOIN t4 ON t1.d = t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.c IS NOT DISTINCT FROM t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 +} +do_execsql_test joinD-226 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t2.x>0 + RIGHT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + FULL JOIN t4 ON t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.d = t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-227 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b IN (t2.b,-2,-3) AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-228 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c IN (-4,t3.c,-5) AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-229 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-230 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-231 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-232 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-233 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON true + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-234 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-235 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-236 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-237 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-238 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-239 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-240 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-241 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-242 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c = t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-243 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t2.x>0 + FULL JOIN t3 ON t1.c = t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-244 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t2.x>0 + FULL JOIN t3 ON t3.y>0 + INNER JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.c = t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-245 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t2.x>0 + FULL JOIN t3 ON t1.c = t3.c AND t3.y>0 + INNER JOIN t4 ON t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.d IS NOT DISTINCT FROM t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-246 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b IN (t2.b,-2,-3) AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-247 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - +} +do_execsql_test joinD-248 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - +} +do_execsql_test joinD-249 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - +} +do_execsql_test joinD-250 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - +} +do_execsql_test joinD-251 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON true + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - +} +do_execsql_test joinD-252 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - +} +do_execsql_test joinD-253 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - +} +do_execsql_test joinD-254 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-255 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - +} +do_execsql_test joinD-256 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-257 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-258 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-259 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-260 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c = t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - +} +do_execsql_test joinD-261 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t2.x>0 + FULL JOIN t3 ON t1.c = t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - - - 200 0 - - +} +do_execsql_test joinD-262 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t2.x>0 + FULL JOIN t3 ON t3.y>0 + LEFT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.c = t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-263 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t2.x>0 + FULL JOIN t3 ON t1.c = t3.c AND t3.y>0 + LEFT JOIN t4 ON t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.d IS NOT DISTINCT FROM t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-264 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b IN (t2.b,-2,-3) AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - +} +do_execsql_test joinD-265 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-266 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-267 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-268 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-269 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON true + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-270 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-271 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-272 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-273 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-274 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-275 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-276 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-277 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-278 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c = t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-279 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t2.x>0 + FULL JOIN t3 ON t1.c = t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 +} +do_execsql_test joinD-280 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t2.x>0 + FULL JOIN t3 ON t3.y>0 + RIGHT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.c = t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-281 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t2.x>0 + FULL JOIN t3 ON t1.c = t3.c AND t3.y>0 + RIGHT JOIN t4 ON t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.d IS NOT DISTINCT FROM t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-282 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b IN (t2.b,-2,-3) AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-283 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-284 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-285 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - +} +do_execsql_test joinD-286 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-287 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON true + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - +} +do_execsql_test joinD-288 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - +} +do_execsql_test joinD-289 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-290 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-291 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-292 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-293 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-294 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-295 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-296 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c = t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d = t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-297 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t2.x>0 + FULL JOIN t3 ON t1.c = t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d = t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - - - 200 0 - - + - - - - - - - - 300 0 +} +do_execsql_test joinD-298 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t2.x>0 + FULL JOIN t3 ON t3.y>0 + FULL JOIN t4 ON t1.d = t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.c = t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-299 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t2.x>0 + FULL JOIN t3 ON t1.c = t3.c AND t3.y>0 + FULL JOIN t4 ON t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.d = t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-300 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 INNER JOIN t2 ON t1.b IN (t2.b,-2,-3) AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-301 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-302 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-303 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-304 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-305 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON true + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-306 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-307 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-308 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-309 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-310 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-311 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-312 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-313 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-314 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-315 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t2.x>0 + INNER JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-316 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t2.x>0 + INNER JOIN t3 ON t3.y>0 + INNER JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.c IS NOT DISTINCT FROM t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-317 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t2.x>0 + INNER JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + INNER JOIN t4 ON t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.d IS NOT DISTINCT FROM t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-318 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b IN (t2.b,-2,-3) AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-319 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c IN (-4,t3.c,-5) AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-320 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - +} +do_execsql_test joinD-321 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - +} +do_execsql_test joinD-322 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-323 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - +} +do_execsql_test joinD-324 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON true + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-325 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - +} +do_execsql_test joinD-326 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - +} +do_execsql_test joinD-327 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-328 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - +} +do_execsql_test joinD-329 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-330 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-331 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-332 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-333 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - +} +do_execsql_test joinD-334 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t2.x>0 + INNER JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-335 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t2.x>0 + INNER JOIN t3 ON t3.y>0 + LEFT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.c IS NOT DISTINCT FROM t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-336 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t2.x>0 + INNER JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + LEFT JOIN t4 ON t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.d IS NOT DISTINCT FROM t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-337 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b IN (t2.b,-2,-3) AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - +} +do_execsql_test joinD-338 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c IN (-4,t3.c,-5) AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - +} +do_execsql_test joinD-339 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-340 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-341 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-342 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-343 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON true + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-344 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-345 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-346 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-347 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-348 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-349 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-350 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-351 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-352 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-353 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t2.x>0 + INNER JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-354 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t2.x>0 + INNER JOIN t3 ON t3.y>0 + RIGHT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.c IS NOT DISTINCT FROM t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 +} +do_execsql_test joinD-355 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t2.x>0 + INNER JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + RIGHT JOIN t4 ON t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.d IS NOT DISTINCT FROM t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-356 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b IN (t2.b,-2,-3) AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-357 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c IN (-4,t3.c,-5) AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-358 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-359 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-360 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-361 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-362 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON true + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-363 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - +} +do_execsql_test joinD-364 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-365 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-366 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-367 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-368 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-369 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-370 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-371 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d = t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-372 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t2.x>0 + INNER JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d = t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-373 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t2.x>0 + INNER JOIN t3 ON t3.y>0 + FULL JOIN t4 ON t1.d = t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.c IS NOT DISTINCT FROM t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 +} +do_execsql_test joinD-374 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t2.x>0 + INNER JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + FULL JOIN t4 ON t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.d = t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-375 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b IN (t2.b,-2,-3) AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-376 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c IN (-4,t3.c,-5) AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-377 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 +} +do_execsql_test joinD-378 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 +} +do_execsql_test joinD-379 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-380 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 +} +do_execsql_test joinD-381 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON true + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-382 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-383 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 +} +do_execsql_test joinD-384 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 +} +do_execsql_test joinD-385 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 +} +do_execsql_test joinD-386 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-387 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-388 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-389 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-390 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 +} +do_execsql_test joinD-391 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t2.x>0 + LEFT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-392 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t2.x>0 + LEFT JOIN t3 ON t3.y>0 + INNER JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.c IS NOT DISTINCT FROM t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-393 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t2.x>0 + LEFT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + INNER JOIN t4 ON t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.d IS NOT DISTINCT FROM t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-394 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b IN (t2.b,-2,-3) AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 +} +do_execsql_test joinD-395 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c IN (-4,t3.c,-5) AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 +} +do_execsql_test joinD-396 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 0 100 200 300 - - - - - - + 1 101 201 301 - - - - - - + 2 102 202 302 102 2 - - - - + 3 103 203 303 - - 203 3 - - + 4 104 204 304 104 4 - - - - + 5 105 205 305 - - - - 305 5 + 6 106 206 306 106 6 206 6 - - + 7 107 207 307 - - - - - - + 8 108 208 308 108 8 - - - - + 9 109 209 309 - - 209 9 - - + 10 110 210 310 110 10 - - 310 10 + 11 111 211 311 - - - - - - + 12 112 212 312 112 12 212 12 - - + 13 113 213 313 - - - - - - + 14 114 214 314 114 14 - - - - + 15 115 215 315 - - 215 15 315 15 + 16 116 216 316 116 16 - - - - + 17 117 217 317 - - - - - - + 18 118 218 318 118 18 218 18 - - + 19 119 219 319 - - - - - - + 20 120 220 320 120 20 - - 320 20 + 21 121 221 321 - - 221 21 - - + 22 122 222 322 122 22 - - - - + 23 123 223 323 - - - - - - + 24 124 224 324 124 24 224 24 - - + 25 125 225 325 - - - - 325 25 + 26 126 226 326 126 26 - - - - + 27 127 227 327 - - 227 27 - - + 28 128 228 328 128 28 - - - - + 29 129 229 329 - - - - - - + 30 130 230 330 130 30 230 30 330 30 + 31 131 231 331 - - - - - - + 32 132 232 332 132 32 - - - - + 33 133 233 333 - - 233 33 - - + 34 134 234 334 134 34 - - - - + 35 135 235 335 - - - - 335 35 + 36 136 236 336 136 36 236 36 - - + 37 137 237 337 - - - - - - + 38 138 238 338 138 38 - - - - + 39 139 239 339 - - 239 39 - - + 40 140 240 340 140 40 - - 340 40 + 41 141 241 341 - - - - - - + 42 142 242 342 142 42 242 42 - - + 43 143 243 343 - - - - - - + 44 144 244 344 144 44 - - - - + 45 145 245 345 - - 245 45 345 45 + 46 146 246 346 146 46 - - - - + 47 147 247 347 - - - - - - + 48 148 248 348 148 48 248 48 - - + 49 149 249 349 - - - - - - + 50 150 250 350 150 50 - - 350 50 + 51 151 251 351 - - 251 51 - - + 52 152 252 352 152 52 - - - - + 53 153 253 353 - - - - - - + 54 154 254 354 154 54 254 54 - - + 55 155 255 355 - - - - 355 55 + 56 156 256 356 156 56 - - - - + 57 157 257 357 - - 257 57 - - + 58 158 258 358 158 58 - - - - + 59 159 259 359 - - - - - - + 60 160 260 360 160 60 260 60 360 60 + 61 161 261 361 - - - - - - + 62 162 262 362 162 62 - - - - + 63 163 263 363 - - 263 63 - - + 64 164 264 364 164 64 - - - - + 65 165 265 365 - - - - 365 65 + 66 166 266 366 166 66 266 66 - - + 67 167 267 367 - - - - - - + 68 168 268 368 168 68 - - - - + 69 169 269 369 - - 269 69 - - + 70 170 270 370 170 70 - - 370 70 + 71 171 271 371 - - - - - - + 72 172 272 372 172 72 272 72 - - + 73 173 273 373 - - - - - - + 74 174 274 374 174 74 - - - - + 75 175 275 375 - - 275 75 375 75 + 76 176 276 376 176 76 - - - - + 77 177 277 377 - - - - - - + 78 178 278 378 178 78 278 78 - - + 79 179 279 379 - - - - - - + 80 180 280 380 180 80 - - 380 80 + 81 181 281 381 - - 281 81 - - + 82 182 282 382 182 82 - - - - + 83 183 283 383 - - - - - - + 84 184 284 384 184 84 284 84 - - + 85 185 285 385 - - - - 385 85 + 86 186 286 386 186 86 - - - - + 87 187 287 387 - - 287 87 - - + 88 188 288 388 188 88 - - - - + 89 189 289 389 - - - - - - + 90 190 290 390 190 90 290 90 390 90 + 91 191 291 391 - - - - - - + 92 192 292 392 192 92 - - - - + 93 193 293 393 - - 293 93 - - + 94 194 294 394 194 94 - - - - + 95 195 295 395 - - - - 395 95 + 96 - 296 396 - - - - - - + 97 197 - 397 - - - - - - + 98 198 298 - - - - - - - + 99 - - - - - - - - - +} +do_execsql_test joinD-397 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 0 100 200 300 - - - - - - + 1 101 201 301 - - - - - - + 2 102 202 302 102 2 - - - - + 3 103 203 303 - - 203 3 - - + 4 104 204 304 104 4 - - - - + 5 105 205 305 - - - - 305 5 + 6 106 206 306 106 6 206 6 - - + 7 107 207 307 - - - - - - + 8 108 208 308 108 8 - - - - + 9 109 209 309 - - 209 9 - - + 10 110 210 310 110 10 - - 310 10 + 11 111 211 311 - - - - - - + 12 112 212 312 112 12 212 12 - - + 13 113 213 313 - - - - - - + 14 114 214 314 114 14 - - - - + 15 115 215 315 - - 215 15 315 15 + 16 116 216 316 116 16 - - - - + 17 117 217 317 - - - - - - + 18 118 218 318 118 18 218 18 - - + 19 119 219 319 - - - - - - + 20 120 220 320 120 20 - - 320 20 + 21 121 221 321 - - 221 21 - - + 22 122 222 322 122 22 - - - - + 23 123 223 323 - - - - - - + 24 124 224 324 124 24 224 24 - - + 25 125 225 325 - - - - 325 25 + 26 126 226 326 126 26 - - - - + 27 127 227 327 - - 227 27 - - + 28 128 228 328 128 28 - - - - + 29 129 229 329 - - - - - - + 30 130 230 330 130 30 230 30 330 30 + 31 131 231 331 - - - - - - + 32 132 232 332 132 32 - - - - + 33 133 233 333 - - 233 33 - - + 34 134 234 334 134 34 - - - - + 35 135 235 335 - - - - 335 35 + 36 136 236 336 136 36 236 36 - - + 37 137 237 337 - - - - - - + 38 138 238 338 138 38 - - - - + 39 139 239 339 - - 239 39 - - + 40 140 240 340 140 40 - - 340 40 + 41 141 241 341 - - - - - - + 42 142 242 342 142 42 242 42 - - + 43 143 243 343 - - - - - - + 44 144 244 344 144 44 - - - - + 45 145 245 345 - - 245 45 345 45 + 46 146 246 346 146 46 - - - - + 47 147 247 347 - - - - - - + 48 148 248 348 148 48 248 48 - - + 49 149 249 349 - - - - - - + 50 150 250 350 150 50 - - 350 50 + 51 151 251 351 - - 251 51 - - + 52 152 252 352 152 52 - - - - + 53 153 253 353 - - - - - - + 54 154 254 354 154 54 254 54 - - + 55 155 255 355 - - - - 355 55 + 56 156 256 356 156 56 - - - - + 57 157 257 357 - - 257 57 - - + 58 158 258 358 158 58 - - - - + 59 159 259 359 - - - - - - + 60 160 260 360 160 60 260 60 360 60 + 61 161 261 361 - - - - - - + 62 162 262 362 162 62 - - - - + 63 163 263 363 - - 263 63 - - + 64 164 264 364 164 64 - - - - + 65 165 265 365 - - - - 365 65 + 66 166 266 366 166 66 266 66 - - + 67 167 267 367 - - - - - - + 68 168 268 368 168 68 - - - - + 69 169 269 369 - - 269 69 - - + 70 170 270 370 170 70 - - 370 70 + 71 171 271 371 - - - - - - + 72 172 272 372 172 72 272 72 - - + 73 173 273 373 - - - - - - + 74 174 274 374 174 74 - - - - + 75 175 275 375 - - 275 75 375 75 + 76 176 276 376 176 76 - - - - + 77 177 277 377 - - - - - - + 78 178 278 378 178 78 278 78 - - + 79 179 279 379 - - - - - - + 80 180 280 380 180 80 - - 380 80 + 81 181 281 381 - - 281 81 - - + 82 182 282 382 182 82 - - - - + 83 183 283 383 - - - - - - + 84 184 284 384 184 84 284 84 - - + 85 185 285 385 - - - - 385 85 + 86 186 286 386 186 86 - - - - + 87 187 287 387 - - 287 87 - - + 88 188 288 388 188 88 - - - - + 89 189 289 389 - - - - - - + 90 190 290 390 190 90 290 90 390 90 + 91 191 291 391 - - - - - - + 92 192 292 392 192 92 - - - - + 93 193 293 393 - - 293 93 - - + 94 194 294 394 194 94 - - - - + 95 195 295 395 - - - - 395 95 + 96 - 296 396 - - - - - - + 97 197 - 397 - - - - - - + 98 198 298 - - - - - - - + 99 - - - - - - - - - +} +do_execsql_test joinD-398 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - +} +do_execsql_test joinD-399 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 1 101 201 301 - - - - - - + 2 102 202 302 102 2 - - - - + 3 103 203 303 - - 203 3 - - + 4 104 204 304 104 4 - - - - + 5 105 205 305 - - - - 305 5 + 6 106 206 306 106 6 206 6 - - + 7 107 207 307 - - - - - - + 8 108 208 308 108 8 - - - - + 9 109 209 309 - - 209 9 - - + 10 110 210 310 110 10 - - 310 10 + 11 111 211 311 - - - - - - + 12 112 212 312 112 12 212 12 - - + 13 113 213 313 - - - - - - + 14 114 214 314 114 14 - - - - + 15 115 215 315 - - 215 15 315 15 + 16 116 216 316 116 16 - - - - + 17 117 217 317 - - - - - - + 18 118 218 318 118 18 218 18 - - + 19 119 219 319 - - - - - - + 20 120 220 320 120 20 - - 320 20 + 21 121 221 321 - - 221 21 - - + 22 122 222 322 122 22 - - - - + 23 123 223 323 - - - - - - + 24 124 224 324 124 24 224 24 - - + 25 125 225 325 - - - - 325 25 + 26 126 226 326 126 26 - - - - + 27 127 227 327 - - 227 27 - - + 28 128 228 328 128 28 - - - - + 29 129 229 329 - - - - - - + 30 130 230 330 130 30 230 30 330 30 + 31 131 231 331 - - - - - - + 32 132 232 332 132 32 - - - - + 33 133 233 333 - - 233 33 - - + 34 134 234 334 134 34 - - - - + 35 135 235 335 - - - - 335 35 + 36 136 236 336 136 36 236 36 - - + 37 137 237 337 - - - - - - + 38 138 238 338 138 38 - - - - + 39 139 239 339 - - 239 39 - - + 40 140 240 340 140 40 - - 340 40 + 41 141 241 341 - - - - - - + 42 142 242 342 142 42 242 42 - - + 43 143 243 343 - - - - - - + 44 144 244 344 144 44 - - - - + 45 145 245 345 - - 245 45 345 45 + 46 146 246 346 146 46 - - - - + 47 147 247 347 - - - - - - + 48 148 248 348 148 48 248 48 - - + 49 149 249 349 - - - - - - + 50 150 250 350 150 50 - - 350 50 + 51 151 251 351 - - 251 51 - - + 52 152 252 352 152 52 - - - - + 53 153 253 353 - - - - - - + 54 154 254 354 154 54 254 54 - - + 55 155 255 355 - - - - 355 55 + 56 156 256 356 156 56 - - - - + 57 157 257 357 - - 257 57 - - + 58 158 258 358 158 58 - - - - + 59 159 259 359 - - - - - - + 60 160 260 360 160 60 260 60 360 60 + 61 161 261 361 - - - - - - + 62 162 262 362 162 62 - - - - + 63 163 263 363 - - 263 63 - - + 64 164 264 364 164 64 - - - - + 65 165 265 365 - - - - 365 65 + 66 166 266 366 166 66 266 66 - - + 67 167 267 367 - - - - - - + 68 168 268 368 168 68 - - - - + 69 169 269 369 - - 269 69 - - + 70 170 270 370 170 70 - - 370 70 + 71 171 271 371 - - - - - - + 72 172 272 372 172 72 272 72 - - + 73 173 273 373 - - - - - - + 74 174 274 374 174 74 - - - - + 75 175 275 375 - - 275 75 375 75 + 76 176 276 376 176 76 - - - - + 77 177 277 377 - - - - - - + 78 178 278 378 178 78 278 78 - - + 79 179 279 379 - - - - - - + 80 180 280 380 180 80 - - 380 80 + 81 181 281 381 - - 281 81 - - + 82 182 282 382 182 82 - - - - + 83 183 283 383 - - - - - - + 84 184 284 384 184 84 284 84 - - + 85 185 285 385 - - - - 385 85 + 86 186 286 386 186 86 - - - - + 87 187 287 387 - - 287 87 - - + 88 188 288 388 188 88 - - - - + 89 189 289 389 - - - - - - + 90 190 290 390 190 90 290 90 390 90 + 91 191 291 391 - - - - - - + 92 192 292 392 192 92 - - - - + 93 193 293 393 - - 293 93 - - + 94 194 294 394 194 94 - - - - + 95 195 295 395 - - - - 395 95 + 96 - 296 396 - - - - - - + 97 197 - 397 - - - - - - + 98 198 298 - - - - - - - + 99 - - - - - - - - - +} +do_execsql_test joinD-400 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON true + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - +} +do_execsql_test joinD-401 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - +} +do_execsql_test joinD-402 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 1 101 201 301 - - - - - - + 2 102 202 302 102 2 - - - - + 3 103 203 303 - - 203 3 - - + 4 104 204 304 104 4 - - - - + 5 105 205 305 - - - - 305 5 + 6 106 206 306 106 6 206 6 - - + 7 107 207 307 - - - - - - + 8 108 208 308 108 8 - - - - + 9 109 209 309 - - 209 9 - - + 10 110 210 310 110 10 - - 310 10 + 11 111 211 311 - - - - - - + 12 112 212 312 112 12 212 12 - - + 13 113 213 313 - - - - - - + 14 114 214 314 114 14 - - - - + 15 115 215 315 - - 215 15 315 15 + 16 116 216 316 116 16 - - - - + 17 117 217 317 - - - - - - + 18 118 218 318 118 18 218 18 - - + 19 119 219 319 - - - - - - + 20 120 220 320 120 20 - - 320 20 + 21 121 221 321 - - 221 21 - - + 22 122 222 322 122 22 - - - - + 23 123 223 323 - - - - - - + 24 124 224 324 124 24 224 24 - - + 25 125 225 325 - - - - 325 25 + 26 126 226 326 126 26 - - - - + 27 127 227 327 - - 227 27 - - + 28 128 228 328 128 28 - - - - + 29 129 229 329 - - - - - - + 30 130 230 330 130 30 230 30 330 30 + 31 131 231 331 - - - - - - + 32 132 232 332 132 32 - - - - + 33 133 233 333 - - 233 33 - - + 34 134 234 334 134 34 - - - - + 35 135 235 335 - - - - 335 35 + 36 136 236 336 136 36 236 36 - - + 37 137 237 337 - - - - - - + 38 138 238 338 138 38 - - - - + 39 139 239 339 - - 239 39 - - + 40 140 240 340 140 40 - - 340 40 + 41 141 241 341 - - - - - - + 42 142 242 342 142 42 242 42 - - + 43 143 243 343 - - - - - - + 44 144 244 344 144 44 - - - - + 45 145 245 345 - - 245 45 345 45 + 46 146 246 346 146 46 - - - - + 47 147 247 347 - - - - - - + 48 148 248 348 148 48 248 48 - - + 49 149 249 349 - - - - - - + 50 150 250 350 150 50 - - 350 50 + 51 151 251 351 - - 251 51 - - + 52 152 252 352 152 52 - - - - + 53 153 253 353 - - - - - - + 54 154 254 354 154 54 254 54 - - + 55 155 255 355 - - - - 355 55 + 56 156 256 356 156 56 - - - - + 57 157 257 357 - - 257 57 - - + 58 158 258 358 158 58 - - - - + 59 159 259 359 - - - - - - + 60 160 260 360 160 60 260 60 360 60 + 61 161 261 361 - - - - - - + 62 162 262 362 162 62 - - - - + 63 163 263 363 - - 263 63 - - + 64 164 264 364 164 64 - - - - + 65 165 265 365 - - - - 365 65 + 66 166 266 366 166 66 266 66 - - + 67 167 267 367 - - - - - - + 68 168 268 368 168 68 - - - - + 69 169 269 369 - - 269 69 - - + 70 170 270 370 170 70 - - 370 70 + 71 171 271 371 - - - - - - + 72 172 272 372 172 72 272 72 - - + 73 173 273 373 - - - - - - + 74 174 274 374 174 74 - - - - + 75 175 275 375 - - 275 75 375 75 + 76 176 276 376 176 76 - - - - + 77 177 277 377 - - - - - - + 78 178 278 378 178 78 278 78 - - + 79 179 279 379 - - - - - - + 80 180 280 380 180 80 - - 380 80 + 81 181 281 381 - - 281 81 - - + 82 182 282 382 182 82 - - - - + 83 183 283 383 - - - - - - + 84 184 284 384 184 84 284 84 - - + 85 185 285 385 - - - - 385 85 + 86 186 286 386 186 86 - - - - + 87 187 287 387 - - 287 87 - - + 88 188 288 388 188 88 - - - - + 89 189 289 389 - - - - - - + 90 190 290 390 190 90 290 90 390 90 + 91 191 291 391 - - - - - - + 92 192 292 392 192 92 - - - - + 93 193 293 393 - - 293 93 - - + 94 194 294 394 194 94 - - - - + 95 195 295 395 - - - - 395 95 + 96 - 296 396 - - - - - - + 97 197 - 397 - - - - - - + 98 198 298 - - - - - - - + 99 - - - - - - - - - +} +do_execsql_test joinD-403 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 +} +do_execsql_test joinD-404 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 1 101 201 301 - - - - - - + 2 102 202 302 102 2 - - - - + 3 103 203 303 - - 203 3 - - + 4 104 204 304 104 4 - - - - + 5 105 205 305 - - - - 305 5 + 6 106 206 306 106 6 206 6 - - + 7 107 207 307 - - - - - - + 8 108 208 308 108 8 - - - - + 9 109 209 309 - - 209 9 - - + 10 110 210 310 110 10 - - 310 10 + 11 111 211 311 - - - - - - + 12 112 212 312 112 12 212 12 - - + 13 113 213 313 - - - - - - + 14 114 214 314 114 14 - - - - + 15 115 215 315 - - 215 15 315 15 + 16 116 216 316 116 16 - - - - + 17 117 217 317 - - - - - - + 18 118 218 318 118 18 218 18 - - + 19 119 219 319 - - - - - - + 20 120 220 320 120 20 - - 320 20 + 21 121 221 321 - - 221 21 - - + 22 122 222 322 122 22 - - - - + 23 123 223 323 - - - - - - + 24 124 224 324 124 24 224 24 - - + 25 125 225 325 - - - - 325 25 + 26 126 226 326 126 26 - - - - + 27 127 227 327 - - 227 27 - - + 28 128 228 328 128 28 - - - - + 29 129 229 329 - - - - - - + 30 130 230 330 130 30 230 30 330 30 + 31 131 231 331 - - - - - - + 32 132 232 332 132 32 - - - - + 33 133 233 333 - - 233 33 - - + 34 134 234 334 134 34 - - - - + 35 135 235 335 - - - - 335 35 + 36 136 236 336 136 36 236 36 - - + 37 137 237 337 - - - - - - + 38 138 238 338 138 38 - - - - + 39 139 239 339 - - 239 39 - - + 40 140 240 340 140 40 - - 340 40 + 41 141 241 341 - - - - - - + 42 142 242 342 142 42 242 42 - - + 43 143 243 343 - - - - - - + 44 144 244 344 144 44 - - - - + 45 145 245 345 - - 245 45 345 45 + 46 146 246 346 146 46 - - - - + 47 147 247 347 - - - - - - + 48 148 248 348 148 48 248 48 - - + 49 149 249 349 - - - - - - + 50 150 250 350 150 50 - - 350 50 + 51 151 251 351 - - 251 51 - - + 52 152 252 352 152 52 - - - - + 53 153 253 353 - - - - - - + 54 154 254 354 154 54 254 54 - - + 55 155 255 355 - - - - 355 55 + 56 156 256 356 156 56 - - - - + 57 157 257 357 - - 257 57 - - + 58 158 258 358 158 58 - - - - + 59 159 259 359 - - - - - - + 60 160 260 360 160 60 260 60 360 60 + 61 161 261 361 - - - - - - + 62 162 262 362 162 62 - - - - + 63 163 263 363 - - 263 63 - - + 64 164 264 364 164 64 - - - - + 65 165 265 365 - - - - 365 65 + 66 166 266 366 166 66 266 66 - - + 67 167 267 367 - - - - - - + 68 168 268 368 168 68 - - - - + 69 169 269 369 - - 269 69 - - + 70 170 270 370 170 70 - - 370 70 + 71 171 271 371 - - - - - - + 72 172 272 372 172 72 272 72 - - + 73 173 273 373 - - - - - - + 74 174 274 374 174 74 - - - - + 75 175 275 375 - - 275 75 375 75 + 76 176 276 376 176 76 - - - - + 77 177 277 377 - - - - - - + 78 178 278 378 178 78 278 78 - - + 79 179 279 379 - - - - - - + 80 180 280 380 180 80 - - 380 80 + 81 181 281 381 - - 281 81 - - + 82 182 282 382 182 82 - - - - + 83 183 283 383 - - - - - - + 84 184 284 384 184 84 284 84 - - + 85 185 285 385 - - - - 385 85 + 86 186 286 386 186 86 - - - - + 87 187 287 387 - - 287 87 - - + 88 188 288 388 188 88 - - - - + 89 189 289 389 - - - - - - + 90 190 290 390 190 90 290 90 390 90 + 91 191 291 391 - - - - - - + 92 192 292 392 192 92 - - - - + 93 193 293 393 - - 293 93 - - + 94 194 294 394 194 94 - - - - + 95 195 295 395 - - - - 395 95 + 96 - 296 396 - - - - - - + 97 197 - 397 - - - - - - + 98 198 298 - - - - - - - + 99 - - - - - - - - - +} +do_execsql_test joinD-405 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-406 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-407 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-408 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-409 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 0 100 200 300 - - - - - - + 1 101 201 301 - - - - - - + 2 102 202 302 102 2 - - - - + 3 103 203 303 - - 203 3 - - + 4 104 204 304 104 4 - - - - + 5 105 205 305 - - - - 305 5 + 6 106 206 306 106 6 206 6 - - + 7 107 207 307 - - - - - - + 8 108 208 308 108 8 - - - - + 9 109 209 309 - - 209 9 - - + 10 110 210 310 110 10 - - 310 10 + 11 111 211 311 - - - - - - + 12 112 212 312 112 12 212 12 - - + 13 113 213 313 - - - - - - + 14 114 214 314 114 14 - - - - + 15 115 215 315 - - 215 15 315 15 + 16 116 216 316 116 16 - - - - + 17 117 217 317 - - - - - - + 18 118 218 318 118 18 218 18 - - + 19 119 219 319 - - - - - - + 20 120 220 320 120 20 - - 320 20 + 21 121 221 321 - - 221 21 - - + 22 122 222 322 122 22 - - - - + 23 123 223 323 - - - - - - + 24 124 224 324 124 24 224 24 - - + 25 125 225 325 - - - - 325 25 + 26 126 226 326 126 26 - - - - + 27 127 227 327 - - 227 27 - - + 28 128 228 328 128 28 - - - - + 29 129 229 329 - - - - - - + 30 130 230 330 130 30 230 30 330 30 + 31 131 231 331 - - - - - - + 32 132 232 332 132 32 - - - - + 33 133 233 333 - - 233 33 - - + 34 134 234 334 134 34 - - - - + 35 135 235 335 - - - - 335 35 + 36 136 236 336 136 36 236 36 - - + 37 137 237 337 - - - - - - + 38 138 238 338 138 38 - - - - + 39 139 239 339 - - 239 39 - - + 40 140 240 340 140 40 - - 340 40 + 41 141 241 341 - - - - - - + 42 142 242 342 142 42 242 42 - - + 43 143 243 343 - - - - - - + 44 144 244 344 144 44 - - - - + 45 145 245 345 - - 245 45 345 45 + 46 146 246 346 146 46 - - - - + 47 147 247 347 - - - - - - + 48 148 248 348 148 48 248 48 - - + 49 149 249 349 - - - - - - + 50 150 250 350 150 50 - - 350 50 + 51 151 251 351 - - 251 51 - - + 52 152 252 352 152 52 - - - - + 53 153 253 353 - - - - - - + 54 154 254 354 154 54 254 54 - - + 55 155 255 355 - - - - 355 55 + 56 156 256 356 156 56 - - - - + 57 157 257 357 - - 257 57 - - + 58 158 258 358 158 58 - - - - + 59 159 259 359 - - - - - - + 60 160 260 360 160 60 260 60 360 60 + 61 161 261 361 - - - - - - + 62 162 262 362 162 62 - - - - + 63 163 263 363 - - 263 63 - - + 64 164 264 364 164 64 - - - - + 65 165 265 365 - - - - 365 65 + 66 166 266 366 166 66 266 66 - - + 67 167 267 367 - - - - - - + 68 168 268 368 168 68 - - - - + 69 169 269 369 - - 269 69 - - + 70 170 270 370 170 70 - - 370 70 + 71 171 271 371 - - - - - - + 72 172 272 372 172 72 272 72 - - + 73 173 273 373 - - - - - - + 74 174 274 374 174 74 - - - - + 75 175 275 375 - - 275 75 375 75 + 76 176 276 376 176 76 - - - - + 77 177 277 377 - - - - - - + 78 178 278 378 178 78 278 78 - - + 79 179 279 379 - - - - - - + 80 180 280 380 180 80 - - 380 80 + 81 181 281 381 - - 281 81 - - + 82 182 282 382 182 82 - - - - + 83 183 283 383 - - - - - - + 84 184 284 384 184 84 284 84 - - + 85 185 285 385 - - - - 385 85 + 86 186 286 386 186 86 - - - - + 87 187 287 387 - - 287 87 - - + 88 188 288 388 188 88 - - - - + 89 189 289 389 - - - - - - + 90 190 290 390 190 90 290 90 390 90 + 91 191 291 391 - - - - - - + 92 192 292 392 192 92 - - - - + 93 193 293 393 - - 293 93 - - + 94 194 294 394 194 94 - - - - + 95 195 295 395 - - - - 395 95 + 96 - 296 396 - - - - - - + 97 197 - 397 - - - - - - + 98 198 298 - - - - - - - + 99 - - - - - - - - - +} +do_execsql_test joinD-410 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t2.x>0 + LEFT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - +} +do_execsql_test joinD-411 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t2.x>0 + LEFT JOIN t3 ON t3.y>0 + LEFT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.c IS NOT DISTINCT FROM t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-412 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t2.x>0 + LEFT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + LEFT JOIN t4 ON t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.d IS NOT DISTINCT FROM t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-413 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b IN (t2.b,-2,-3) AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 0 100 200 300 - - - - - - + 1 101 201 301 - - - - - - + 2 102 202 302 102 2 - - - - + 3 103 203 303 - - 203 3 - - + 4 104 204 304 104 4 - - - - + 5 105 205 305 - - - - 305 5 + 6 106 206 306 106 6 206 6 - - + 7 107 207 307 - - - - - - + 8 108 208 308 108 8 - - - - + 9 109 209 309 - - 209 9 - - + 10 110 210 310 110 10 - - 310 10 + 11 111 211 311 - - - - - - + 12 112 212 312 112 12 212 12 - - + 13 113 213 313 - - - - - - + 14 114 214 314 114 14 - - - - + 15 115 215 315 - - 215 15 315 15 + 16 116 216 316 116 16 - - - - + 17 117 217 317 - - - - - - + 18 118 218 318 118 18 218 18 - - + 19 119 219 319 - - - - - - + 20 120 220 320 120 20 - - 320 20 + 21 121 221 321 - - 221 21 - - + 22 122 222 322 122 22 - - - - + 23 123 223 323 - - - - - - + 24 124 224 324 124 24 224 24 - - + 25 125 225 325 - - - - 325 25 + 26 126 226 326 126 26 - - - - + 27 127 227 327 - - 227 27 - - + 28 128 228 328 128 28 - - - - + 29 129 229 329 - - - - - - + 30 130 230 330 130 30 230 30 330 30 + 31 131 231 331 - - - - - - + 32 132 232 332 132 32 - - - - + 33 133 233 333 - - 233 33 - - + 34 134 234 334 134 34 - - - - + 35 135 235 335 - - - - 335 35 + 36 136 236 336 136 36 236 36 - - + 37 137 237 337 - - - - - - + 38 138 238 338 138 38 - - - - + 39 139 239 339 - - 239 39 - - + 40 140 240 340 140 40 - - 340 40 + 41 141 241 341 - - - - - - + 42 142 242 342 142 42 242 42 - - + 43 143 243 343 - - - - - - + 44 144 244 344 144 44 - - - - + 45 145 245 345 - - 245 45 345 45 + 46 146 246 346 146 46 - - - - + 47 147 247 347 - - - - - - + 48 148 248 348 148 48 248 48 - - + 49 149 249 349 - - - - - - + 50 150 250 350 150 50 - - 350 50 + 51 151 251 351 - - 251 51 - - + 52 152 252 352 152 52 - - - - + 53 153 253 353 - - - - - - + 54 154 254 354 154 54 254 54 - - + 55 155 255 355 - - - - 355 55 + 56 156 256 356 156 56 - - - - + 57 157 257 357 - - 257 57 - - + 58 158 258 358 158 58 - - - - + 59 159 259 359 - - - - - - + 60 160 260 360 160 60 260 60 360 60 + 61 161 261 361 - - - - - - + 62 162 262 362 162 62 - - - - + 63 163 263 363 - - 263 63 - - + 64 164 264 364 164 64 - - - - + 65 165 265 365 - - - - 365 65 + 66 166 266 366 166 66 266 66 - - + 67 167 267 367 - - - - - - + 68 168 268 368 168 68 - - - - + 69 169 269 369 - - 269 69 - - + 70 170 270 370 170 70 - - 370 70 + 71 171 271 371 - - - - - - + 72 172 272 372 172 72 272 72 - - + 73 173 273 373 - - - - - - + 74 174 274 374 174 74 - - - - + 75 175 275 375 - - 275 75 375 75 + 76 176 276 376 176 76 - - - - + 77 177 277 377 - - - - - - + 78 178 278 378 178 78 278 78 - - + 79 179 279 379 - - - - - - + 80 180 280 380 180 80 - - 380 80 + 81 181 281 381 - - 281 81 - - + 82 182 282 382 182 82 - - - - + 83 183 283 383 - - - - - - + 84 184 284 384 184 84 284 84 - - + 85 185 285 385 - - - - 385 85 + 86 186 286 386 186 86 - - - - + 87 187 287 387 - - 287 87 - - + 88 188 288 388 188 88 - - - - + 89 189 289 389 - - - - - - + 90 190 290 390 190 90 290 90 390 90 + 91 191 291 391 - - - - - - + 92 192 292 392 192 92 - - - - + 93 193 293 393 - - 293 93 - - + 94 194 294 394 194 94 - - - - + 95 195 295 395 - - - - 395 95 + 96 - 296 396 - - - - - - + 97 197 - 397 - - - - - - + 98 198 298 - - - - - - - + 99 - - - - - - - - - +} +do_execsql_test joinD-414 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c IN (-4,t3.c,-5) AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 0 100 200 300 - - - - - - + 1 101 201 301 - - - - - - + 2 102 202 302 102 2 - - - - + 3 103 203 303 - - 203 3 - - + 4 104 204 304 104 4 - - - - + 5 105 205 305 - - - - 305 5 + 6 106 206 306 106 6 206 6 - - + 7 107 207 307 - - - - - - + 8 108 208 308 108 8 - - - - + 9 109 209 309 - - 209 9 - - + 10 110 210 310 110 10 - - 310 10 + 11 111 211 311 - - - - - - + 12 112 212 312 112 12 212 12 - - + 13 113 213 313 - - - - - - + 14 114 214 314 114 14 - - - - + 15 115 215 315 - - 215 15 315 15 + 16 116 216 316 116 16 - - - - + 17 117 217 317 - - - - - - + 18 118 218 318 118 18 218 18 - - + 19 119 219 319 - - - - - - + 20 120 220 320 120 20 - - 320 20 + 21 121 221 321 - - 221 21 - - + 22 122 222 322 122 22 - - - - + 23 123 223 323 - - - - - - + 24 124 224 324 124 24 224 24 - - + 25 125 225 325 - - - - 325 25 + 26 126 226 326 126 26 - - - - + 27 127 227 327 - - 227 27 - - + 28 128 228 328 128 28 - - - - + 29 129 229 329 - - - - - - + 30 130 230 330 130 30 230 30 330 30 + 31 131 231 331 - - - - - - + 32 132 232 332 132 32 - - - - + 33 133 233 333 - - 233 33 - - + 34 134 234 334 134 34 - - - - + 35 135 235 335 - - - - 335 35 + 36 136 236 336 136 36 236 36 - - + 37 137 237 337 - - - - - - + 38 138 238 338 138 38 - - - - + 39 139 239 339 - - 239 39 - - + 40 140 240 340 140 40 - - 340 40 + 41 141 241 341 - - - - - - + 42 142 242 342 142 42 242 42 - - + 43 143 243 343 - - - - - - + 44 144 244 344 144 44 - - - - + 45 145 245 345 - - 245 45 345 45 + 46 146 246 346 146 46 - - - - + 47 147 247 347 - - - - - - + 48 148 248 348 148 48 248 48 - - + 49 149 249 349 - - - - - - + 50 150 250 350 150 50 - - 350 50 + 51 151 251 351 - - 251 51 - - + 52 152 252 352 152 52 - - - - + 53 153 253 353 - - - - - - + 54 154 254 354 154 54 254 54 - - + 55 155 255 355 - - - - 355 55 + 56 156 256 356 156 56 - - - - + 57 157 257 357 - - 257 57 - - + 58 158 258 358 158 58 - - - - + 59 159 259 359 - - - - - - + 60 160 260 360 160 60 260 60 360 60 + 61 161 261 361 - - - - - - + 62 162 262 362 162 62 - - - - + 63 163 263 363 - - 263 63 - - + 64 164 264 364 164 64 - - - - + 65 165 265 365 - - - - 365 65 + 66 166 266 366 166 66 266 66 - - + 67 167 267 367 - - - - - - + 68 168 268 368 168 68 - - - - + 69 169 269 369 - - 269 69 - - + 70 170 270 370 170 70 - - 370 70 + 71 171 271 371 - - - - - - + 72 172 272 372 172 72 272 72 - - + 73 173 273 373 - - - - - - + 74 174 274 374 174 74 - - - - + 75 175 275 375 - - 275 75 375 75 + 76 176 276 376 176 76 - - - - + 77 177 277 377 - - - - - - + 78 178 278 378 178 78 278 78 - - + 79 179 279 379 - - - - - - + 80 180 280 380 180 80 - - 380 80 + 81 181 281 381 - - 281 81 - - + 82 182 282 382 182 82 - - - - + 83 183 283 383 - - - - - - + 84 184 284 384 184 84 284 84 - - + 85 185 285 385 - - - - 385 85 + 86 186 286 386 186 86 - - - - + 87 187 287 387 - - 287 87 - - + 88 188 288 388 188 88 - - - - + 89 189 289 389 - - - - - - + 90 190 290 390 190 90 290 90 390 90 + 91 191 291 391 - - - - - - + 92 192 292 392 192 92 - - - - + 93 193 293 393 - - 293 93 - - + 94 194 294 394 194 94 - - - - + 95 195 295 395 - - - - 395 95 + 96 - 296 396 - - - - - - + 97 197 - 397 - - - - - - + 98 198 298 - - - - - - - + 99 - - - - - - - - - +} +do_execsql_test joinD-415 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 + - - - - - - - - 300 0 +} +do_execsql_test joinD-416 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 + - - - - - - - - 300 0 +} +do_execsql_test joinD-417 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-418 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 + - - - - - - - - 300 0 +} +do_execsql_test joinD-419 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON true + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-420 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-421 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 + - - - - - - - - 300 0 +} +do_execsql_test joinD-422 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 +} +do_execsql_test joinD-423 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 +} +do_execsql_test joinD-424 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-425 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-426 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-427 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-428 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 + - - - - - - - - 300 0 +} +do_execsql_test joinD-429 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t2.x>0 + LEFT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 +} +do_execsql_test joinD-430 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t2.x>0 + LEFT JOIN t3 ON t3.y>0 + RIGHT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.c IS NOT DISTINCT FROM t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 +} +do_execsql_test joinD-431 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t2.x>0 + LEFT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + RIGHT JOIN t4 ON t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.d IS NOT DISTINCT FROM t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-432 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b IN (t2.b,-2,-3) AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 + - - - - - - - - 300 0 +} +do_execsql_test joinD-433 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c IN (-4,t3.c,-5) AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 + - - - - - - - - 300 0 +} +do_execsql_test joinD-434 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 0 100 200 300 - - - - - - + 1 101 201 301 - - - - - - + 2 102 202 302 102 2 - - - - + 3 103 203 303 - - 203 3 - - + 4 104 204 304 104 4 - - - - + 5 105 205 305 - - - - 305 5 + 6 106 206 306 106 6 206 6 - - + 7 107 207 307 - - - - - - + 8 108 208 308 108 8 - - - - + 9 109 209 309 - - 209 9 - - + 10 110 210 310 110 10 - - 310 10 + 11 111 211 311 - - - - - - + 12 112 212 312 112 12 212 12 - - + 13 113 213 313 - - - - - - + 14 114 214 314 114 14 - - - - + 15 115 215 315 - - 215 15 315 15 + 16 116 216 316 116 16 - - - - + 17 117 217 317 - - - - - - + 18 118 218 318 118 18 218 18 - - + 19 119 219 319 - - - - - - + 20 120 220 320 120 20 - - 320 20 + 21 121 221 321 - - 221 21 - - + 22 122 222 322 122 22 - - - - + 23 123 223 323 - - - - - - + 24 124 224 324 124 24 224 24 - - + 25 125 225 325 - - - - 325 25 + 26 126 226 326 126 26 - - - - + 27 127 227 327 - - 227 27 - - + 28 128 228 328 128 28 - - - - + 29 129 229 329 - - - - - - + 30 130 230 330 130 30 230 30 330 30 + 31 131 231 331 - - - - - - + 32 132 232 332 132 32 - - - - + 33 133 233 333 - - 233 33 - - + 34 134 234 334 134 34 - - - - + 35 135 235 335 - - - - 335 35 + 36 136 236 336 136 36 236 36 - - + 37 137 237 337 - - - - - - + 38 138 238 338 138 38 - - - - + 39 139 239 339 - - 239 39 - - + 40 140 240 340 140 40 - - 340 40 + 41 141 241 341 - - - - - - + 42 142 242 342 142 42 242 42 - - + 43 143 243 343 - - - - - - + 44 144 244 344 144 44 - - - - + 45 145 245 345 - - 245 45 345 45 + 46 146 246 346 146 46 - - - - + 47 147 247 347 - - - - - - + 48 148 248 348 148 48 248 48 - - + 49 149 249 349 - - - - - - + 50 150 250 350 150 50 - - 350 50 + 51 151 251 351 - - 251 51 - - + 52 152 252 352 152 52 - - - - + 53 153 253 353 - - - - - - + 54 154 254 354 154 54 254 54 - - + 55 155 255 355 - - - - 355 55 + 56 156 256 356 156 56 - - - - + 57 157 257 357 - - 257 57 - - + 58 158 258 358 158 58 - - - - + 59 159 259 359 - - - - - - + 60 160 260 360 160 60 260 60 360 60 + 61 161 261 361 - - - - - - + 62 162 262 362 162 62 - - - - + 63 163 263 363 - - 263 63 - - + 64 164 264 364 164 64 - - - - + 65 165 265 365 - - - - 365 65 + 66 166 266 366 166 66 266 66 - - + 67 167 267 367 - - - - - - + 68 168 268 368 168 68 - - - - + 69 169 269 369 - - 269 69 - - + 70 170 270 370 170 70 - - 370 70 + 71 171 271 371 - - - - - - + 72 172 272 372 172 72 272 72 - - + 73 173 273 373 - - - - - - + 74 174 274 374 174 74 - - - - + 75 175 275 375 - - 275 75 375 75 + 76 176 276 376 176 76 - - - - + 77 177 277 377 - - - - - - + 78 178 278 378 178 78 278 78 - - + 79 179 279 379 - - - - - - + 80 180 280 380 180 80 - - 380 80 + 81 181 281 381 - - 281 81 - - + 82 182 282 382 182 82 - - - - + 83 183 283 383 - - - - - - + 84 184 284 384 184 84 284 84 - - + 85 185 285 385 - - - - 385 85 + 86 186 286 386 186 86 - - - - + 87 187 287 387 - - 287 87 - - + 88 188 288 388 188 88 - - - - + 89 189 289 389 - - - - - - + 90 190 290 390 190 90 290 90 390 90 + 91 191 291 391 - - - - - - + 92 192 292 392 192 92 - - - - + 93 193 293 393 - - 293 93 - - + 94 194 294 394 194 94 - - - - + 95 195 295 395 - - - - 395 95 + 96 - 296 396 - - - - - - + 97 197 - 397 - - - - - - + 98 198 298 - - - - - - - + 99 - - - - - - - - - + - - - - - - - - 300 0 +} +do_execsql_test joinD-435 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 0 100 200 300 - - - - - - + 1 101 201 301 - - - - - - + 2 102 202 302 102 2 - - - - + 3 103 203 303 - - 203 3 - - + 4 104 204 304 104 4 - - - - + 5 105 205 305 - - - - 305 5 + 6 106 206 306 106 6 206 6 - - + 7 107 207 307 - - - - - - + 8 108 208 308 108 8 - - - - + 9 109 209 309 - - 209 9 - - + 10 110 210 310 110 10 - - 310 10 + 11 111 211 311 - - - - - - + 12 112 212 312 112 12 212 12 - - + 13 113 213 313 - - - - - - + 14 114 214 314 114 14 - - - - + 15 115 215 315 - - 215 15 315 15 + 16 116 216 316 116 16 - - - - + 17 117 217 317 - - - - - - + 18 118 218 318 118 18 218 18 - - + 19 119 219 319 - - - - - - + 20 120 220 320 120 20 - - 320 20 + 21 121 221 321 - - 221 21 - - + 22 122 222 322 122 22 - - - - + 23 123 223 323 - - - - - - + 24 124 224 324 124 24 224 24 - - + 25 125 225 325 - - - - 325 25 + 26 126 226 326 126 26 - - - - + 27 127 227 327 - - 227 27 - - + 28 128 228 328 128 28 - - - - + 29 129 229 329 - - - - - - + 30 130 230 330 130 30 230 30 330 30 + 31 131 231 331 - - - - - - + 32 132 232 332 132 32 - - - - + 33 133 233 333 - - 233 33 - - + 34 134 234 334 134 34 - - - - + 35 135 235 335 - - - - 335 35 + 36 136 236 336 136 36 236 36 - - + 37 137 237 337 - - - - - - + 38 138 238 338 138 38 - - - - + 39 139 239 339 - - 239 39 - - + 40 140 240 340 140 40 - - 340 40 + 41 141 241 341 - - - - - - + 42 142 242 342 142 42 242 42 - - + 43 143 243 343 - - - - - - + 44 144 244 344 144 44 - - - - + 45 145 245 345 - - 245 45 345 45 + 46 146 246 346 146 46 - - - - + 47 147 247 347 - - - - - - + 48 148 248 348 148 48 248 48 - - + 49 149 249 349 - - - - - - + 50 150 250 350 150 50 - - 350 50 + 51 151 251 351 - - 251 51 - - + 52 152 252 352 152 52 - - - - + 53 153 253 353 - - - - - - + 54 154 254 354 154 54 254 54 - - + 55 155 255 355 - - - - 355 55 + 56 156 256 356 156 56 - - - - + 57 157 257 357 - - 257 57 - - + 58 158 258 358 158 58 - - - - + 59 159 259 359 - - - - - - + 60 160 260 360 160 60 260 60 360 60 + 61 161 261 361 - - - - - - + 62 162 262 362 162 62 - - - - + 63 163 263 363 - - 263 63 - - + 64 164 264 364 164 64 - - - - + 65 165 265 365 - - - - 365 65 + 66 166 266 366 166 66 266 66 - - + 67 167 267 367 - - - - - - + 68 168 268 368 168 68 - - - - + 69 169 269 369 - - 269 69 - - + 70 170 270 370 170 70 - - 370 70 + 71 171 271 371 - - - - - - + 72 172 272 372 172 72 272 72 - - + 73 173 273 373 - - - - - - + 74 174 274 374 174 74 - - - - + 75 175 275 375 - - 275 75 375 75 + 76 176 276 376 176 76 - - - - + 77 177 277 377 - - - - - - + 78 178 278 378 178 78 278 78 - - + 79 179 279 379 - - - - - - + 80 180 280 380 180 80 - - 380 80 + 81 181 281 381 - - 281 81 - - + 82 182 282 382 182 82 - - - - + 83 183 283 383 - - - - - - + 84 184 284 384 184 84 284 84 - - + 85 185 285 385 - - - - 385 85 + 86 186 286 386 186 86 - - - - + 87 187 287 387 - - 287 87 - - + 88 188 288 388 188 88 - - - - + 89 189 289 389 - - - - - - + 90 190 290 390 190 90 290 90 390 90 + 91 191 291 391 - - - - - - + 92 192 292 392 192 92 - - - - + 93 193 293 393 - - 293 93 - - + 94 194 294 394 194 94 - - - - + 95 195 295 395 - - - - 395 95 + 96 - 296 396 - - - - - - + 97 197 - 397 - - - - - - + 98 198 298 - - - - - - - + 99 - - - - - - - - - + - - - - - - - - 300 0 +} +do_execsql_test joinD-436 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - +} +do_execsql_test joinD-437 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 1 101 201 301 - - - - - - + 2 102 202 302 102 2 - - - - + 3 103 203 303 - - 203 3 - - + 4 104 204 304 104 4 - - - - + 5 105 205 305 - - - - 305 5 + 6 106 206 306 106 6 206 6 - - + 7 107 207 307 - - - - - - + 8 108 208 308 108 8 - - - - + 9 109 209 309 - - 209 9 - - + 10 110 210 310 110 10 - - 310 10 + 11 111 211 311 - - - - - - + 12 112 212 312 112 12 212 12 - - + 13 113 213 313 - - - - - - + 14 114 214 314 114 14 - - - - + 15 115 215 315 - - 215 15 315 15 + 16 116 216 316 116 16 - - - - + 17 117 217 317 - - - - - - + 18 118 218 318 118 18 218 18 - - + 19 119 219 319 - - - - - - + 20 120 220 320 120 20 - - 320 20 + 21 121 221 321 - - 221 21 - - + 22 122 222 322 122 22 - - - - + 23 123 223 323 - - - - - - + 24 124 224 324 124 24 224 24 - - + 25 125 225 325 - - - - 325 25 + 26 126 226 326 126 26 - - - - + 27 127 227 327 - - 227 27 - - + 28 128 228 328 128 28 - - - - + 29 129 229 329 - - - - - - + 30 130 230 330 130 30 230 30 330 30 + 31 131 231 331 - - - - - - + 32 132 232 332 132 32 - - - - + 33 133 233 333 - - 233 33 - - + 34 134 234 334 134 34 - - - - + 35 135 235 335 - - - - 335 35 + 36 136 236 336 136 36 236 36 - - + 37 137 237 337 - - - - - - + 38 138 238 338 138 38 - - - - + 39 139 239 339 - - 239 39 - - + 40 140 240 340 140 40 - - 340 40 + 41 141 241 341 - - - - - - + 42 142 242 342 142 42 242 42 - - + 43 143 243 343 - - - - - - + 44 144 244 344 144 44 - - - - + 45 145 245 345 - - 245 45 345 45 + 46 146 246 346 146 46 - - - - + 47 147 247 347 - - - - - - + 48 148 248 348 148 48 248 48 - - + 49 149 249 349 - - - - - - + 50 150 250 350 150 50 - - 350 50 + 51 151 251 351 - - 251 51 - - + 52 152 252 352 152 52 - - - - + 53 153 253 353 - - - - - - + 54 154 254 354 154 54 254 54 - - + 55 155 255 355 - - - - 355 55 + 56 156 256 356 156 56 - - - - + 57 157 257 357 - - 257 57 - - + 58 158 258 358 158 58 - - - - + 59 159 259 359 - - - - - - + 60 160 260 360 160 60 260 60 360 60 + 61 161 261 361 - - - - - - + 62 162 262 362 162 62 - - - - + 63 163 263 363 - - 263 63 - - + 64 164 264 364 164 64 - - - - + 65 165 265 365 - - - - 365 65 + 66 166 266 366 166 66 266 66 - - + 67 167 267 367 - - - - - - + 68 168 268 368 168 68 - - - - + 69 169 269 369 - - 269 69 - - + 70 170 270 370 170 70 - - 370 70 + 71 171 271 371 - - - - - - + 72 172 272 372 172 72 272 72 - - + 73 173 273 373 - - - - - - + 74 174 274 374 174 74 - - - - + 75 175 275 375 - - 275 75 375 75 + 76 176 276 376 176 76 - - - - + 77 177 277 377 - - - - - - + 78 178 278 378 178 78 278 78 - - + 79 179 279 379 - - - - - - + 80 180 280 380 180 80 - - 380 80 + 81 181 281 381 - - 281 81 - - + 82 182 282 382 182 82 - - - - + 83 183 283 383 - - - - - - + 84 184 284 384 184 84 284 84 - - + 85 185 285 385 - - - - 385 85 + 86 186 286 386 186 86 - - - - + 87 187 287 387 - - 287 87 - - + 88 188 288 388 188 88 - - - - + 89 189 289 389 - - - - - - + 90 190 290 390 190 90 290 90 390 90 + 91 191 291 391 - - - - - - + 92 192 292 392 192 92 - - - - + 93 193 293 393 - - 293 93 - - + 94 194 294 394 194 94 - - - - + 95 195 295 395 - - - - 395 95 + 96 - 296 396 - - - - - - + 97 197 - 397 - - - - - - + 98 198 298 - - - - - - - + 99 - - - - - - - - - + - - - - - - - - 300 0 +} +do_execsql_test joinD-438 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON true + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - +} +do_execsql_test joinD-439 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - +} +do_execsql_test joinD-440 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 1 101 201 301 - - - - - - + 2 102 202 302 102 2 - - - - + 3 103 203 303 - - 203 3 - - + 4 104 204 304 104 4 - - - - + 5 105 205 305 - - - - 305 5 + 6 106 206 306 106 6 206 6 - - + 7 107 207 307 - - - - - - + 8 108 208 308 108 8 - - - - + 9 109 209 309 - - 209 9 - - + 10 110 210 310 110 10 - - 310 10 + 11 111 211 311 - - - - - - + 12 112 212 312 112 12 212 12 - - + 13 113 213 313 - - - - - - + 14 114 214 314 114 14 - - - - + 15 115 215 315 - - 215 15 315 15 + 16 116 216 316 116 16 - - - - + 17 117 217 317 - - - - - - + 18 118 218 318 118 18 218 18 - - + 19 119 219 319 - - - - - - + 20 120 220 320 120 20 - - 320 20 + 21 121 221 321 - - 221 21 - - + 22 122 222 322 122 22 - - - - + 23 123 223 323 - - - - - - + 24 124 224 324 124 24 224 24 - - + 25 125 225 325 - - - - 325 25 + 26 126 226 326 126 26 - - - - + 27 127 227 327 - - 227 27 - - + 28 128 228 328 128 28 - - - - + 29 129 229 329 - - - - - - + 30 130 230 330 130 30 230 30 330 30 + 31 131 231 331 - - - - - - + 32 132 232 332 132 32 - - - - + 33 133 233 333 - - 233 33 - - + 34 134 234 334 134 34 - - - - + 35 135 235 335 - - - - 335 35 + 36 136 236 336 136 36 236 36 - - + 37 137 237 337 - - - - - - + 38 138 238 338 138 38 - - - - + 39 139 239 339 - - 239 39 - - + 40 140 240 340 140 40 - - 340 40 + 41 141 241 341 - - - - - - + 42 142 242 342 142 42 242 42 - - + 43 143 243 343 - - - - - - + 44 144 244 344 144 44 - - - - + 45 145 245 345 - - 245 45 345 45 + 46 146 246 346 146 46 - - - - + 47 147 247 347 - - - - - - + 48 148 248 348 148 48 248 48 - - + 49 149 249 349 - - - - - - + 50 150 250 350 150 50 - - 350 50 + 51 151 251 351 - - 251 51 - - + 52 152 252 352 152 52 - - - - + 53 153 253 353 - - - - - - + 54 154 254 354 154 54 254 54 - - + 55 155 255 355 - - - - 355 55 + 56 156 256 356 156 56 - - - - + 57 157 257 357 - - 257 57 - - + 58 158 258 358 158 58 - - - - + 59 159 259 359 - - - - - - + 60 160 260 360 160 60 260 60 360 60 + 61 161 261 361 - - - - - - + 62 162 262 362 162 62 - - - - + 63 163 263 363 - - 263 63 - - + 64 164 264 364 164 64 - - - - + 65 165 265 365 - - - - 365 65 + 66 166 266 366 166 66 266 66 - - + 67 167 267 367 - - - - - - + 68 168 268 368 168 68 - - - - + 69 169 269 369 - - 269 69 - - + 70 170 270 370 170 70 - - 370 70 + 71 171 271 371 - - - - - - + 72 172 272 372 172 72 272 72 - - + 73 173 273 373 - - - - - - + 74 174 274 374 174 74 - - - - + 75 175 275 375 - - 275 75 375 75 + 76 176 276 376 176 76 - - - - + 77 177 277 377 - - - - - - + 78 178 278 378 178 78 278 78 - - + 79 179 279 379 - - - - - - + 80 180 280 380 180 80 - - 380 80 + 81 181 281 381 - - 281 81 - - + 82 182 282 382 182 82 - - - - + 83 183 283 383 - - - - - - + 84 184 284 384 184 84 284 84 - - + 85 185 285 385 - - - - 385 85 + 86 186 286 386 186 86 - - - - + 87 187 287 387 - - 287 87 - - + 88 188 288 388 188 88 - - - - + 89 189 289 389 - - - - - - + 90 190 290 390 190 90 290 90 390 90 + 91 191 291 391 - - - - - - + 92 192 292 392 192 92 - - - - + 93 193 293 393 - - 293 93 - - + 94 194 294 394 194 94 - - - - + 95 195 295 395 - - - - 395 95 + 96 - 296 396 - - - - - - + 97 197 - 397 - - - - - - + 98 198 298 - - - - - - - + 99 - - - - - - - - - + - - - - - - - - 300 0 +} +do_execsql_test joinD-441 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 +} +do_execsql_test joinD-442 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 1 101 201 301 - - - - - - + 2 102 202 302 102 2 - - - - + 3 103 203 303 - - 203 3 - - + 4 104 204 304 104 4 - - - - + 5 105 205 305 - - - - 305 5 + 6 106 206 306 106 6 206 6 - - + 7 107 207 307 - - - - - - + 8 108 208 308 108 8 - - - - + 9 109 209 309 - - 209 9 - - + 10 110 210 310 110 10 - - 310 10 + 11 111 211 311 - - - - - - + 12 112 212 312 112 12 212 12 - - + 13 113 213 313 - - - - - - + 14 114 214 314 114 14 - - - - + 15 115 215 315 - - 215 15 315 15 + 16 116 216 316 116 16 - - - - + 17 117 217 317 - - - - - - + 18 118 218 318 118 18 218 18 - - + 19 119 219 319 - - - - - - + 20 120 220 320 120 20 - - 320 20 + 21 121 221 321 - - 221 21 - - + 22 122 222 322 122 22 - - - - + 23 123 223 323 - - - - - - + 24 124 224 324 124 24 224 24 - - + 25 125 225 325 - - - - 325 25 + 26 126 226 326 126 26 - - - - + 27 127 227 327 - - 227 27 - - + 28 128 228 328 128 28 - - - - + 29 129 229 329 - - - - - - + 30 130 230 330 130 30 230 30 330 30 + 31 131 231 331 - - - - - - + 32 132 232 332 132 32 - - - - + 33 133 233 333 - - 233 33 - - + 34 134 234 334 134 34 - - - - + 35 135 235 335 - - - - 335 35 + 36 136 236 336 136 36 236 36 - - + 37 137 237 337 - - - - - - + 38 138 238 338 138 38 - - - - + 39 139 239 339 - - 239 39 - - + 40 140 240 340 140 40 - - 340 40 + 41 141 241 341 - - - - - - + 42 142 242 342 142 42 242 42 - - + 43 143 243 343 - - - - - - + 44 144 244 344 144 44 - - - - + 45 145 245 345 - - 245 45 345 45 + 46 146 246 346 146 46 - - - - + 47 147 247 347 - - - - - - + 48 148 248 348 148 48 248 48 - - + 49 149 249 349 - - - - - - + 50 150 250 350 150 50 - - 350 50 + 51 151 251 351 - - 251 51 - - + 52 152 252 352 152 52 - - - - + 53 153 253 353 - - - - - - + 54 154 254 354 154 54 254 54 - - + 55 155 255 355 - - - - 355 55 + 56 156 256 356 156 56 - - - - + 57 157 257 357 - - 257 57 - - + 58 158 258 358 158 58 - - - - + 59 159 259 359 - - - - - - + 60 160 260 360 160 60 260 60 360 60 + 61 161 261 361 - - - - - - + 62 162 262 362 162 62 - - - - + 63 163 263 363 - - 263 63 - - + 64 164 264 364 164 64 - - - - + 65 165 265 365 - - - - 365 65 + 66 166 266 366 166 66 266 66 - - + 67 167 267 367 - - - - - - + 68 168 268 368 168 68 - - - - + 69 169 269 369 - - 269 69 - - + 70 170 270 370 170 70 - - 370 70 + 71 171 271 371 - - - - - - + 72 172 272 372 172 72 272 72 - - + 73 173 273 373 - - - - - - + 74 174 274 374 174 74 - - - - + 75 175 275 375 - - 275 75 375 75 + 76 176 276 376 176 76 - - - - + 77 177 277 377 - - - - - - + 78 178 278 378 178 78 278 78 - - + 79 179 279 379 - - - - - - + 80 180 280 380 180 80 - - 380 80 + 81 181 281 381 - - 281 81 - - + 82 182 282 382 182 82 - - - - + 83 183 283 383 - - - - - - + 84 184 284 384 184 84 284 84 - - + 85 185 285 385 - - - - 385 85 + 86 186 286 386 186 86 - - - - + 87 187 287 387 - - 287 87 - - + 88 188 288 388 188 88 - - - - + 89 189 289 389 - - - - - - + 90 190 290 390 190 90 290 90 390 90 + 91 191 291 391 - - - - - - + 92 192 292 392 192 92 - - - - + 93 193 293 393 - - 293 93 - - + 94 194 294 394 194 94 - - - - + 95 195 295 395 - - - - 395 95 + 96 - 296 396 - - - - - - + 97 197 - 397 - - - - - - + 98 198 298 - - - - - - - + 99 - - - - - - - - - +} +do_execsql_test joinD-443 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-444 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-445 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-446 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-447 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d = t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 0 100 200 300 - - - - - - + 1 101 201 301 - - - - - - + 2 102 202 302 102 2 - - - - + 3 103 203 303 - - 203 3 - - + 4 104 204 304 104 4 - - - - + 5 105 205 305 - - - - 305 5 + 6 106 206 306 106 6 206 6 - - + 7 107 207 307 - - - - - - + 8 108 208 308 108 8 - - - - + 9 109 209 309 - - 209 9 - - + 10 110 210 310 110 10 - - 310 10 + 11 111 211 311 - - - - - - + 12 112 212 312 112 12 212 12 - - + 13 113 213 313 - - - - - - + 14 114 214 314 114 14 - - - - + 15 115 215 315 - - 215 15 315 15 + 16 116 216 316 116 16 - - - - + 17 117 217 317 - - - - - - + 18 118 218 318 118 18 218 18 - - + 19 119 219 319 - - - - - - + 20 120 220 320 120 20 - - 320 20 + 21 121 221 321 - - 221 21 - - + 22 122 222 322 122 22 - - - - + 23 123 223 323 - - - - - - + 24 124 224 324 124 24 224 24 - - + 25 125 225 325 - - - - 325 25 + 26 126 226 326 126 26 - - - - + 27 127 227 327 - - 227 27 - - + 28 128 228 328 128 28 - - - - + 29 129 229 329 - - - - - - + 30 130 230 330 130 30 230 30 330 30 + 31 131 231 331 - - - - - - + 32 132 232 332 132 32 - - - - + 33 133 233 333 - - 233 33 - - + 34 134 234 334 134 34 - - - - + 35 135 235 335 - - - - 335 35 + 36 136 236 336 136 36 236 36 - - + 37 137 237 337 - - - - - - + 38 138 238 338 138 38 - - - - + 39 139 239 339 - - 239 39 - - + 40 140 240 340 140 40 - - 340 40 + 41 141 241 341 - - - - - - + 42 142 242 342 142 42 242 42 - - + 43 143 243 343 - - - - - - + 44 144 244 344 144 44 - - - - + 45 145 245 345 - - 245 45 345 45 + 46 146 246 346 146 46 - - - - + 47 147 247 347 - - - - - - + 48 148 248 348 148 48 248 48 - - + 49 149 249 349 - - - - - - + 50 150 250 350 150 50 - - 350 50 + 51 151 251 351 - - 251 51 - - + 52 152 252 352 152 52 - - - - + 53 153 253 353 - - - - - - + 54 154 254 354 154 54 254 54 - - + 55 155 255 355 - - - - 355 55 + 56 156 256 356 156 56 - - - - + 57 157 257 357 - - 257 57 - - + 58 158 258 358 158 58 - - - - + 59 159 259 359 - - - - - - + 60 160 260 360 160 60 260 60 360 60 + 61 161 261 361 - - - - - - + 62 162 262 362 162 62 - - - - + 63 163 263 363 - - 263 63 - - + 64 164 264 364 164 64 - - - - + 65 165 265 365 - - - - 365 65 + 66 166 266 366 166 66 266 66 - - + 67 167 267 367 - - - - - - + 68 168 268 368 168 68 - - - - + 69 169 269 369 - - 269 69 - - + 70 170 270 370 170 70 - - 370 70 + 71 171 271 371 - - - - - - + 72 172 272 372 172 72 272 72 - - + 73 173 273 373 - - - - - - + 74 174 274 374 174 74 - - - - + 75 175 275 375 - - 275 75 375 75 + 76 176 276 376 176 76 - - - - + 77 177 277 377 - - - - - - + 78 178 278 378 178 78 278 78 - - + 79 179 279 379 - - - - - - + 80 180 280 380 180 80 - - 380 80 + 81 181 281 381 - - 281 81 - - + 82 182 282 382 182 82 - - - - + 83 183 283 383 - - - - - - + 84 184 284 384 184 84 284 84 - - + 85 185 285 385 - - - - 385 85 + 86 186 286 386 186 86 - - - - + 87 187 287 387 - - 287 87 - - + 88 188 288 388 188 88 - - - - + 89 189 289 389 - - - - - - + 90 190 290 390 190 90 290 90 390 90 + 91 191 291 391 - - - - - - + 92 192 292 392 192 92 - - - - + 93 193 293 393 - - 293 93 - - + 94 194 294 394 194 94 - - - - + 95 195 295 395 - - - - 395 95 + 96 - 296 396 - - - - - - + 97 197 - 397 - - - - - - + 98 198 298 - - - - - - - + 99 - - - - - - - - - + - - - - - - - - 300 0 +} +do_execsql_test joinD-448 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t2.x>0 + LEFT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d = t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - - - - - 300 0 +} +do_execsql_test joinD-449 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t2.x>0 + LEFT JOIN t3 ON t3.y>0 + FULL JOIN t4 ON t1.d = t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.c IS NOT DISTINCT FROM t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 +} +do_execsql_test joinD-450 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t2.x>0 + LEFT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + FULL JOIN t4 ON t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.d = t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-451 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b IN (t2.b,-2,-3) AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 0 100 200 300 - - - - - - + 1 101 201 301 - - - - - - + 2 102 202 302 102 2 - - - - + 3 103 203 303 - - 203 3 - - + 4 104 204 304 104 4 - - - - + 5 105 205 305 - - - - 305 5 + 6 106 206 306 106 6 206 6 - - + 7 107 207 307 - - - - - - + 8 108 208 308 108 8 - - - - + 9 109 209 309 - - 209 9 - - + 10 110 210 310 110 10 - - 310 10 + 11 111 211 311 - - - - - - + 12 112 212 312 112 12 212 12 - - + 13 113 213 313 - - - - - - + 14 114 214 314 114 14 - - - - + 15 115 215 315 - - 215 15 315 15 + 16 116 216 316 116 16 - - - - + 17 117 217 317 - - - - - - + 18 118 218 318 118 18 218 18 - - + 19 119 219 319 - - - - - - + 20 120 220 320 120 20 - - 320 20 + 21 121 221 321 - - 221 21 - - + 22 122 222 322 122 22 - - - - + 23 123 223 323 - - - - - - + 24 124 224 324 124 24 224 24 - - + 25 125 225 325 - - - - 325 25 + 26 126 226 326 126 26 - - - - + 27 127 227 327 - - 227 27 - - + 28 128 228 328 128 28 - - - - + 29 129 229 329 - - - - - - + 30 130 230 330 130 30 230 30 330 30 + 31 131 231 331 - - - - - - + 32 132 232 332 132 32 - - - - + 33 133 233 333 - - 233 33 - - + 34 134 234 334 134 34 - - - - + 35 135 235 335 - - - - 335 35 + 36 136 236 336 136 36 236 36 - - + 37 137 237 337 - - - - - - + 38 138 238 338 138 38 - - - - + 39 139 239 339 - - 239 39 - - + 40 140 240 340 140 40 - - 340 40 + 41 141 241 341 - - - - - - + 42 142 242 342 142 42 242 42 - - + 43 143 243 343 - - - - - - + 44 144 244 344 144 44 - - - - + 45 145 245 345 - - 245 45 345 45 + 46 146 246 346 146 46 - - - - + 47 147 247 347 - - - - - - + 48 148 248 348 148 48 248 48 - - + 49 149 249 349 - - - - - - + 50 150 250 350 150 50 - - 350 50 + 51 151 251 351 - - 251 51 - - + 52 152 252 352 152 52 - - - - + 53 153 253 353 - - - - - - + 54 154 254 354 154 54 254 54 - - + 55 155 255 355 - - - - 355 55 + 56 156 256 356 156 56 - - - - + 57 157 257 357 - - 257 57 - - + 58 158 258 358 158 58 - - - - + 59 159 259 359 - - - - - - + 60 160 260 360 160 60 260 60 360 60 + 61 161 261 361 - - - - - - + 62 162 262 362 162 62 - - - - + 63 163 263 363 - - 263 63 - - + 64 164 264 364 164 64 - - - - + 65 165 265 365 - - - - 365 65 + 66 166 266 366 166 66 266 66 - - + 67 167 267 367 - - - - - - + 68 168 268 368 168 68 - - - - + 69 169 269 369 - - 269 69 - - + 70 170 270 370 170 70 - - 370 70 + 71 171 271 371 - - - - - - + 72 172 272 372 172 72 272 72 - - + 73 173 273 373 - - - - - - + 74 174 274 374 174 74 - - - - + 75 175 275 375 - - 275 75 375 75 + 76 176 276 376 176 76 - - - - + 77 177 277 377 - - - - - - + 78 178 278 378 178 78 278 78 - - + 79 179 279 379 - - - - - - + 80 180 280 380 180 80 - - 380 80 + 81 181 281 381 - - 281 81 - - + 82 182 282 382 182 82 - - - - + 83 183 283 383 - - - - - - + 84 184 284 384 184 84 284 84 - - + 85 185 285 385 - - - - 385 85 + 86 186 286 386 186 86 - - - - + 87 187 287 387 - - 287 87 - - + 88 188 288 388 188 88 - - - - + 89 189 289 389 - - - - - - + 90 190 290 390 190 90 290 90 390 90 + 91 191 291 391 - - - - - - + 92 192 292 392 192 92 - - - - + 93 193 293 393 - - 293 93 - - + 94 194 294 394 194 94 - - - - + 95 195 295 395 - - - - 395 95 + 96 - 296 396 - - - - - - + 97 197 - 397 - - - - - - + 98 198 298 - - - - - - - + 99 - - - - - - - - - + - - - - - - - - 300 0 +} +do_execsql_test joinD-452 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c IN (-4,t3.c,-5) AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 0 100 200 300 - - - - - - + 1 101 201 301 - - - - - - + 2 102 202 302 102 2 - - - - + 3 103 203 303 - - 203 3 - - + 4 104 204 304 104 4 - - - - + 5 105 205 305 - - - - 305 5 + 6 106 206 306 106 6 206 6 - - + 7 107 207 307 - - - - - - + 8 108 208 308 108 8 - - - - + 9 109 209 309 - - 209 9 - - + 10 110 210 310 110 10 - - 310 10 + 11 111 211 311 - - - - - - + 12 112 212 312 112 12 212 12 - - + 13 113 213 313 - - - - - - + 14 114 214 314 114 14 - - - - + 15 115 215 315 - - 215 15 315 15 + 16 116 216 316 116 16 - - - - + 17 117 217 317 - - - - - - + 18 118 218 318 118 18 218 18 - - + 19 119 219 319 - - - - - - + 20 120 220 320 120 20 - - 320 20 + 21 121 221 321 - - 221 21 - - + 22 122 222 322 122 22 - - - - + 23 123 223 323 - - - - - - + 24 124 224 324 124 24 224 24 - - + 25 125 225 325 - - - - 325 25 + 26 126 226 326 126 26 - - - - + 27 127 227 327 - - 227 27 - - + 28 128 228 328 128 28 - - - - + 29 129 229 329 - - - - - - + 30 130 230 330 130 30 230 30 330 30 + 31 131 231 331 - - - - - - + 32 132 232 332 132 32 - - - - + 33 133 233 333 - - 233 33 - - + 34 134 234 334 134 34 - - - - + 35 135 235 335 - - - - 335 35 + 36 136 236 336 136 36 236 36 - - + 37 137 237 337 - - - - - - + 38 138 238 338 138 38 - - - - + 39 139 239 339 - - 239 39 - - + 40 140 240 340 140 40 - - 340 40 + 41 141 241 341 - - - - - - + 42 142 242 342 142 42 242 42 - - + 43 143 243 343 - - - - - - + 44 144 244 344 144 44 - - - - + 45 145 245 345 - - 245 45 345 45 + 46 146 246 346 146 46 - - - - + 47 147 247 347 - - - - - - + 48 148 248 348 148 48 248 48 - - + 49 149 249 349 - - - - - - + 50 150 250 350 150 50 - - 350 50 + 51 151 251 351 - - 251 51 - - + 52 152 252 352 152 52 - - - - + 53 153 253 353 - - - - - - + 54 154 254 354 154 54 254 54 - - + 55 155 255 355 - - - - 355 55 + 56 156 256 356 156 56 - - - - + 57 157 257 357 - - 257 57 - - + 58 158 258 358 158 58 - - - - + 59 159 259 359 - - - - - - + 60 160 260 360 160 60 260 60 360 60 + 61 161 261 361 - - - - - - + 62 162 262 362 162 62 - - - - + 63 163 263 363 - - 263 63 - - + 64 164 264 364 164 64 - - - - + 65 165 265 365 - - - - 365 65 + 66 166 266 366 166 66 266 66 - - + 67 167 267 367 - - - - - - + 68 168 268 368 168 68 - - - - + 69 169 269 369 - - 269 69 - - + 70 170 270 370 170 70 - - 370 70 + 71 171 271 371 - - - - - - + 72 172 272 372 172 72 272 72 - - + 73 173 273 373 - - - - - - + 74 174 274 374 174 74 - - - - + 75 175 275 375 - - 275 75 375 75 + 76 176 276 376 176 76 - - - - + 77 177 277 377 - - - - - - + 78 178 278 378 178 78 278 78 - - + 79 179 279 379 - - - - - - + 80 180 280 380 180 80 - - 380 80 + 81 181 281 381 - - 281 81 - - + 82 182 282 382 182 82 - - - - + 83 183 283 383 - - - - - - + 84 184 284 384 184 84 284 84 - - + 85 185 285 385 - - - - 385 85 + 86 186 286 386 186 86 - - - - + 87 187 287 387 - - 287 87 - - + 88 188 288 388 188 88 - - - - + 89 189 289 389 - - - - - - + 90 190 290 390 190 90 290 90 390 90 + 91 191 291 391 - - - - - - + 92 192 292 392 192 92 - - - - + 93 193 293 393 - - 293 93 - - + 94 194 294 394 194 94 - - - - + 95 195 295 395 - - - - 395 95 + 96 - 296 396 - - - - - - + 97 197 - 397 - - - - - - + 98 198 298 - - - - - - - + 99 - - - - - - - - - + - - - - - - - - 300 0 +} +do_execsql_test joinD-453 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-454 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-455 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-456 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-457 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON true + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-458 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-459 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-460 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-461 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-462 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-463 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-464 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-465 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-466 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-467 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t2.x>0 + RIGHT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-468 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t2.x>0 + RIGHT JOIN t3 ON t3.y>0 + INNER JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.c IS NOT DISTINCT FROM t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-469 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t2.x>0 + RIGHT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + INNER JOIN t4 ON t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.d IS NOT DISTINCT FROM t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-470 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b IN (t2.b,-2,-3) AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-471 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c IN (-4,t3.c,-5) AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-472 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - + - - - - - - 200 0 - - +} +do_execsql_test joinD-473 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - + - - - - - - 200 0 - - +} +do_execsql_test joinD-474 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-475 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - + - - - - - - 200 0 - - +} +do_execsql_test joinD-476 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON true + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-477 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - +} +do_execsql_test joinD-478 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - +} +do_execsql_test joinD-479 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-480 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - + - - - - - - 200 0 - - +} +do_execsql_test joinD-481 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-482 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-483 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-484 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-485 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - + - - - - - - 200 0 - - +} +do_execsql_test joinD-486 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t2.x>0 + RIGHT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - 200 0 - - +} +do_execsql_test joinD-487 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t2.x>0 + RIGHT JOIN t3 ON t3.y>0 + LEFT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.c IS NOT DISTINCT FROM t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-488 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t2.x>0 + RIGHT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + LEFT JOIN t4 ON t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.d IS NOT DISTINCT FROM t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-489 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b IN (t2.b,-2,-3) AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - + - - - - - - 200 0 - - +} +do_execsql_test joinD-490 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c IN (-4,t3.c,-5) AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - + - - - - - - 200 0 - - +} +do_execsql_test joinD-491 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-492 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-493 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-494 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-495 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON true + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-496 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-497 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-498 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-499 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-500 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-501 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-502 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-503 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-504 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-505 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t2.x>0 + RIGHT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-506 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t2.x>0 + RIGHT JOIN t3 ON t3.y>0 + RIGHT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.c IS NOT DISTINCT FROM t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 +} +do_execsql_test joinD-507 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t2.x>0 + RIGHT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + RIGHT JOIN t4 ON t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.d IS NOT DISTINCT FROM t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-508 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b IN (t2.b,-2,-3) AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-509 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c IN (-4,t3.c,-5) AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-510 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - + - - - - - - 200 0 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-511 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - + - - - - - - 200 0 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-512 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-513 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - + - - - - - - 200 0 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-514 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON true + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-515 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - +} +do_execsql_test joinD-516 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-517 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-518 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - + - - - - - - 200 0 - - + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-519 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-520 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-521 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-522 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-523 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d = t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - + - - - - - - 200 0 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-524 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t2.x>0 + RIGHT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d = t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - 200 0 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-525 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t2.x>0 + RIGHT JOIN t3 ON t3.y>0 + FULL JOIN t4 ON t1.d = t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.c IS NOT DISTINCT FROM t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 +} +do_execsql_test joinD-526 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t2.x>0 + RIGHT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + FULL JOIN t4 ON t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.d = t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-527 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b IN (t2.b,-2,-3) AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - + - - - - - - 200 0 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-528 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c IN (-4,t3.c,-5) AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - + - - - - - - 200 0 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-529 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 +} +do_execsql_test joinD-530 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 +} +do_execsql_test joinD-531 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-532 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 +} +do_execsql_test joinD-533 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON true + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-534 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-535 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 +} +do_execsql_test joinD-536 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 +} +do_execsql_test joinD-537 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 +} +do_execsql_test joinD-538 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-539 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-540 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-541 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-542 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c = t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 +} +do_execsql_test joinD-543 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t2.x>0 + FULL JOIN t3 ON t1.c = t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-544 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t2.x>0 + FULL JOIN t3 ON t3.y>0 + INNER JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.c = t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-545 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t2.x>0 + FULL JOIN t3 ON t1.c = t3.c AND t3.y>0 + INNER JOIN t4 ON t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.d IS NOT DISTINCT FROM t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-546 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b IN (t2.b,-2,-3) AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 +} +do_execsql_test joinD-547 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 0 100 200 300 - - - - - - + 1 101 201 301 - - - - - - + 2 102 202 302 102 2 - - - - + 3 103 203 303 - - 203 3 - - + 4 104 204 304 104 4 - - - - + 5 105 205 305 - - - - 305 5 + 6 106 206 306 106 6 206 6 - - + 7 107 207 307 - - - - - - + 8 108 208 308 108 8 - - - - + 9 109 209 309 - - 209 9 - - + 10 110 210 310 110 10 - - 310 10 + 11 111 211 311 - - - - - - + 12 112 212 312 112 12 212 12 - - + 13 113 213 313 - - - - - - + 14 114 214 314 114 14 - - - - + 15 115 215 315 - - 215 15 315 15 + 16 116 216 316 116 16 - - - - + 17 117 217 317 - - - - - - + 18 118 218 318 118 18 218 18 - - + 19 119 219 319 - - - - - - + 20 120 220 320 120 20 - - 320 20 + 21 121 221 321 - - 221 21 - - + 22 122 222 322 122 22 - - - - + 23 123 223 323 - - - - - - + 24 124 224 324 124 24 224 24 - - + 25 125 225 325 - - - - 325 25 + 26 126 226 326 126 26 - - - - + 27 127 227 327 - - 227 27 - - + 28 128 228 328 128 28 - - - - + 29 129 229 329 - - - - - - + 30 130 230 330 130 30 230 30 330 30 + 31 131 231 331 - - - - - - + 32 132 232 332 132 32 - - - - + 33 133 233 333 - - 233 33 - - + 34 134 234 334 134 34 - - - - + 35 135 235 335 - - - - 335 35 + 36 136 236 336 136 36 236 36 - - + 37 137 237 337 - - - - - - + 38 138 238 338 138 38 - - - - + 39 139 239 339 - - 239 39 - - + 40 140 240 340 140 40 - - 340 40 + 41 141 241 341 - - - - - - + 42 142 242 342 142 42 242 42 - - + 43 143 243 343 - - - - - - + 44 144 244 344 144 44 - - - - + 45 145 245 345 - - 245 45 345 45 + 46 146 246 346 146 46 - - - - + 47 147 247 347 - - - - - - + 48 148 248 348 148 48 248 48 - - + 49 149 249 349 - - - - - - + 50 150 250 350 150 50 - - 350 50 + 51 151 251 351 - - 251 51 - - + 52 152 252 352 152 52 - - - - + 53 153 253 353 - - - - - - + 54 154 254 354 154 54 254 54 - - + 55 155 255 355 - - - - 355 55 + 56 156 256 356 156 56 - - - - + 57 157 257 357 - - 257 57 - - + 58 158 258 358 158 58 - - - - + 59 159 259 359 - - - - - - + 60 160 260 360 160 60 260 60 360 60 + 61 161 261 361 - - - - - - + 62 162 262 362 162 62 - - - - + 63 163 263 363 - - 263 63 - - + 64 164 264 364 164 64 - - - - + 65 165 265 365 - - - - 365 65 + 66 166 266 366 166 66 266 66 - - + 67 167 267 367 - - - - - - + 68 168 268 368 168 68 - - - - + 69 169 269 369 - - 269 69 - - + 70 170 270 370 170 70 - - 370 70 + 71 171 271 371 - - - - - - + 72 172 272 372 172 72 272 72 - - + 73 173 273 373 - - - - - - + 74 174 274 374 174 74 - - - - + 75 175 275 375 - - 275 75 375 75 + 76 176 276 376 176 76 - - - - + 77 177 277 377 - - - - - - + 78 178 278 378 178 78 278 78 - - + 79 179 279 379 - - - - - - + 80 180 280 380 180 80 - - 380 80 + 81 181 281 381 - - 281 81 - - + 82 182 282 382 182 82 - - - - + 83 183 283 383 - - - - - - + 84 184 284 384 184 84 284 84 - - + 85 185 285 385 - - - - 385 85 + 86 186 286 386 186 86 - - - - + 87 187 287 387 - - 287 87 - - + 88 188 288 388 188 88 - - - - + 89 189 289 389 - - - - - - + 90 190 290 390 190 90 290 90 390 90 + 91 191 291 391 - - - - - - + 92 192 292 392 192 92 - - - - + 93 193 293 393 - - 293 93 - - + 94 194 294 394 194 94 - - - - + 95 195 295 395 - - - - 395 95 + 96 - 296 396 - - - - - - + 97 197 - 397 - - - - - - + 98 198 298 - - - - - - - + 99 - - - - - - - - - + - - - - - - 200 0 - - +} +do_execsql_test joinD-548 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 0 100 200 300 - - - - - - + 1 101 201 301 - - - - - - + 2 102 202 302 102 2 - - - - + 3 103 203 303 - - 203 3 - - + 4 104 204 304 104 4 - - - - + 5 105 205 305 - - - - 305 5 + 6 106 206 306 106 6 206 6 - - + 7 107 207 307 - - - - - - + 8 108 208 308 108 8 - - - - + 9 109 209 309 - - 209 9 - - + 10 110 210 310 110 10 - - 310 10 + 11 111 211 311 - - - - - - + 12 112 212 312 112 12 212 12 - - + 13 113 213 313 - - - - - - + 14 114 214 314 114 14 - - - - + 15 115 215 315 - - 215 15 315 15 + 16 116 216 316 116 16 - - - - + 17 117 217 317 - - - - - - + 18 118 218 318 118 18 218 18 - - + 19 119 219 319 - - - - - - + 20 120 220 320 120 20 - - 320 20 + 21 121 221 321 - - 221 21 - - + 22 122 222 322 122 22 - - - - + 23 123 223 323 - - - - - - + 24 124 224 324 124 24 224 24 - - + 25 125 225 325 - - - - 325 25 + 26 126 226 326 126 26 - - - - + 27 127 227 327 - - 227 27 - - + 28 128 228 328 128 28 - - - - + 29 129 229 329 - - - - - - + 30 130 230 330 130 30 230 30 330 30 + 31 131 231 331 - - - - - - + 32 132 232 332 132 32 - - - - + 33 133 233 333 - - 233 33 - - + 34 134 234 334 134 34 - - - - + 35 135 235 335 - - - - 335 35 + 36 136 236 336 136 36 236 36 - - + 37 137 237 337 - - - - - - + 38 138 238 338 138 38 - - - - + 39 139 239 339 - - 239 39 - - + 40 140 240 340 140 40 - - 340 40 + 41 141 241 341 - - - - - - + 42 142 242 342 142 42 242 42 - - + 43 143 243 343 - - - - - - + 44 144 244 344 144 44 - - - - + 45 145 245 345 - - 245 45 345 45 + 46 146 246 346 146 46 - - - - + 47 147 247 347 - - - - - - + 48 148 248 348 148 48 248 48 - - + 49 149 249 349 - - - - - - + 50 150 250 350 150 50 - - 350 50 + 51 151 251 351 - - 251 51 - - + 52 152 252 352 152 52 - - - - + 53 153 253 353 - - - - - - + 54 154 254 354 154 54 254 54 - - + 55 155 255 355 - - - - 355 55 + 56 156 256 356 156 56 - - - - + 57 157 257 357 - - 257 57 - - + 58 158 258 358 158 58 - - - - + 59 159 259 359 - - - - - - + 60 160 260 360 160 60 260 60 360 60 + 61 161 261 361 - - - - - - + 62 162 262 362 162 62 - - - - + 63 163 263 363 - - 263 63 - - + 64 164 264 364 164 64 - - - - + 65 165 265 365 - - - - 365 65 + 66 166 266 366 166 66 266 66 - - + 67 167 267 367 - - - - - - + 68 168 268 368 168 68 - - - - + 69 169 269 369 - - 269 69 - - + 70 170 270 370 170 70 - - 370 70 + 71 171 271 371 - - - - - - + 72 172 272 372 172 72 272 72 - - + 73 173 273 373 - - - - - - + 74 174 274 374 174 74 - - - - + 75 175 275 375 - - 275 75 375 75 + 76 176 276 376 176 76 - - - - + 77 177 277 377 - - - - - - + 78 178 278 378 178 78 278 78 - - + 79 179 279 379 - - - - - - + 80 180 280 380 180 80 - - 380 80 + 81 181 281 381 - - 281 81 - - + 82 182 282 382 182 82 - - - - + 83 183 283 383 - - - - - - + 84 184 284 384 184 84 284 84 - - + 85 185 285 385 - - - - 385 85 + 86 186 286 386 186 86 - - - - + 87 187 287 387 - - 287 87 - - + 88 188 288 388 188 88 - - - - + 89 189 289 389 - - - - - - + 90 190 290 390 190 90 290 90 390 90 + 91 191 291 391 - - - - - - + 92 192 292 392 192 92 - - - - + 93 193 293 393 - - 293 93 - - + 94 194 294 394 194 94 - - - - + 95 195 295 395 - - - - 395 95 + 96 - 296 396 - - - - - - + 97 197 - 397 - - - - - - + 98 198 298 - - - - - - - + 99 - - - - - - - - - + - - - - - - 200 0 - - +} +do_execsql_test joinD-549 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - +} +do_execsql_test joinD-550 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 1 101 201 301 - - - - - - + 2 102 202 302 102 2 - - - - + 3 103 203 303 - - 203 3 - - + 4 104 204 304 104 4 - - - - + 5 105 205 305 - - - - 305 5 + 6 106 206 306 106 6 206 6 - - + 7 107 207 307 - - - - - - + 8 108 208 308 108 8 - - - - + 9 109 209 309 - - 209 9 - - + 10 110 210 310 110 10 - - 310 10 + 11 111 211 311 - - - - - - + 12 112 212 312 112 12 212 12 - - + 13 113 213 313 - - - - - - + 14 114 214 314 114 14 - - - - + 15 115 215 315 - - 215 15 315 15 + 16 116 216 316 116 16 - - - - + 17 117 217 317 - - - - - - + 18 118 218 318 118 18 218 18 - - + 19 119 219 319 - - - - - - + 20 120 220 320 120 20 - - 320 20 + 21 121 221 321 - - 221 21 - - + 22 122 222 322 122 22 - - - - + 23 123 223 323 - - - - - - + 24 124 224 324 124 24 224 24 - - + 25 125 225 325 - - - - 325 25 + 26 126 226 326 126 26 - - - - + 27 127 227 327 - - 227 27 - - + 28 128 228 328 128 28 - - - - + 29 129 229 329 - - - - - - + 30 130 230 330 130 30 230 30 330 30 + 31 131 231 331 - - - - - - + 32 132 232 332 132 32 - - - - + 33 133 233 333 - - 233 33 - - + 34 134 234 334 134 34 - - - - + 35 135 235 335 - - - - 335 35 + 36 136 236 336 136 36 236 36 - - + 37 137 237 337 - - - - - - + 38 138 238 338 138 38 - - - - + 39 139 239 339 - - 239 39 - - + 40 140 240 340 140 40 - - 340 40 + 41 141 241 341 - - - - - - + 42 142 242 342 142 42 242 42 - - + 43 143 243 343 - - - - - - + 44 144 244 344 144 44 - - - - + 45 145 245 345 - - 245 45 345 45 + 46 146 246 346 146 46 - - - - + 47 147 247 347 - - - - - - + 48 148 248 348 148 48 248 48 - - + 49 149 249 349 - - - - - - + 50 150 250 350 150 50 - - 350 50 + 51 151 251 351 - - 251 51 - - + 52 152 252 352 152 52 - - - - + 53 153 253 353 - - - - - - + 54 154 254 354 154 54 254 54 - - + 55 155 255 355 - - - - 355 55 + 56 156 256 356 156 56 - - - - + 57 157 257 357 - - 257 57 - - + 58 158 258 358 158 58 - - - - + 59 159 259 359 - - - - - - + 60 160 260 360 160 60 260 60 360 60 + 61 161 261 361 - - - - - - + 62 162 262 362 162 62 - - - - + 63 163 263 363 - - 263 63 - - + 64 164 264 364 164 64 - - - - + 65 165 265 365 - - - - 365 65 + 66 166 266 366 166 66 266 66 - - + 67 167 267 367 - - - - - - + 68 168 268 368 168 68 - - - - + 69 169 269 369 - - 269 69 - - + 70 170 270 370 170 70 - - 370 70 + 71 171 271 371 - - - - - - + 72 172 272 372 172 72 272 72 - - + 73 173 273 373 - - - - - - + 74 174 274 374 174 74 - - - - + 75 175 275 375 - - 275 75 375 75 + 76 176 276 376 176 76 - - - - + 77 177 277 377 - - - - - - + 78 178 278 378 178 78 278 78 - - + 79 179 279 379 - - - - - - + 80 180 280 380 180 80 - - 380 80 + 81 181 281 381 - - 281 81 - - + 82 182 282 382 182 82 - - - - + 83 183 283 383 - - - - - - + 84 184 284 384 184 84 284 84 - - + 85 185 285 385 - - - - 385 85 + 86 186 286 386 186 86 - - - - + 87 187 287 387 - - 287 87 - - + 88 188 288 388 188 88 - - - - + 89 189 289 389 - - - - - - + 90 190 290 390 190 90 290 90 390 90 + 91 191 291 391 - - - - - - + 92 192 292 392 192 92 - - - - + 93 193 293 393 - - 293 93 - - + 94 194 294 394 194 94 - - - - + 95 195 295 395 - - - - 395 95 + 96 - 296 396 - - - - - - + 97 197 - 397 - - - - - - + 98 198 298 - - - - - - - + 99 - - - - - - - - - + - - - - - - 200 0 - - +} +do_execsql_test joinD-551 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON true + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - +} +do_execsql_test joinD-552 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - +} +do_execsql_test joinD-553 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 1 101 201 301 - - - - - - + 2 102 202 302 102 2 - - - - + 3 103 203 303 - - 203 3 - - + 4 104 204 304 104 4 - - - - + 5 105 205 305 - - - - 305 5 + 6 106 206 306 106 6 206 6 - - + 7 107 207 307 - - - - - - + 8 108 208 308 108 8 - - - - + 9 109 209 309 - - 209 9 - - + 10 110 210 310 110 10 - - 310 10 + 11 111 211 311 - - - - - - + 12 112 212 312 112 12 212 12 - - + 13 113 213 313 - - - - - - + 14 114 214 314 114 14 - - - - + 15 115 215 315 - - 215 15 315 15 + 16 116 216 316 116 16 - - - - + 17 117 217 317 - - - - - - + 18 118 218 318 118 18 218 18 - - + 19 119 219 319 - - - - - - + 20 120 220 320 120 20 - - 320 20 + 21 121 221 321 - - 221 21 - - + 22 122 222 322 122 22 - - - - + 23 123 223 323 - - - - - - + 24 124 224 324 124 24 224 24 - - + 25 125 225 325 - - - - 325 25 + 26 126 226 326 126 26 - - - - + 27 127 227 327 - - 227 27 - - + 28 128 228 328 128 28 - - - - + 29 129 229 329 - - - - - - + 30 130 230 330 130 30 230 30 330 30 + 31 131 231 331 - - - - - - + 32 132 232 332 132 32 - - - - + 33 133 233 333 - - 233 33 - - + 34 134 234 334 134 34 - - - - + 35 135 235 335 - - - - 335 35 + 36 136 236 336 136 36 236 36 - - + 37 137 237 337 - - - - - - + 38 138 238 338 138 38 - - - - + 39 139 239 339 - - 239 39 - - + 40 140 240 340 140 40 - - 340 40 + 41 141 241 341 - - - - - - + 42 142 242 342 142 42 242 42 - - + 43 143 243 343 - - - - - - + 44 144 244 344 144 44 - - - - + 45 145 245 345 - - 245 45 345 45 + 46 146 246 346 146 46 - - - - + 47 147 247 347 - - - - - - + 48 148 248 348 148 48 248 48 - - + 49 149 249 349 - - - - - - + 50 150 250 350 150 50 - - 350 50 + 51 151 251 351 - - 251 51 - - + 52 152 252 352 152 52 - - - - + 53 153 253 353 - - - - - - + 54 154 254 354 154 54 254 54 - - + 55 155 255 355 - - - - 355 55 + 56 156 256 356 156 56 - - - - + 57 157 257 357 - - 257 57 - - + 58 158 258 358 158 58 - - - - + 59 159 259 359 - - - - - - + 60 160 260 360 160 60 260 60 360 60 + 61 161 261 361 - - - - - - + 62 162 262 362 162 62 - - - - + 63 163 263 363 - - 263 63 - - + 64 164 264 364 164 64 - - - - + 65 165 265 365 - - - - 365 65 + 66 166 266 366 166 66 266 66 - - + 67 167 267 367 - - - - - - + 68 168 268 368 168 68 - - - - + 69 169 269 369 - - 269 69 - - + 70 170 270 370 170 70 - - 370 70 + 71 171 271 371 - - - - - - + 72 172 272 372 172 72 272 72 - - + 73 173 273 373 - - - - - - + 74 174 274 374 174 74 - - - - + 75 175 275 375 - - 275 75 375 75 + 76 176 276 376 176 76 - - - - + 77 177 277 377 - - - - - - + 78 178 278 378 178 78 278 78 - - + 79 179 279 379 - - - - - - + 80 180 280 380 180 80 - - 380 80 + 81 181 281 381 - - 281 81 - - + 82 182 282 382 182 82 - - - - + 83 183 283 383 - - - - - - + 84 184 284 384 184 84 284 84 - - + 85 185 285 385 - - - - 385 85 + 86 186 286 386 186 86 - - - - + 87 187 287 387 - - 287 87 - - + 88 188 288 388 188 88 - - - - + 89 189 289 389 - - - - - - + 90 190 290 390 190 90 290 90 390 90 + 91 191 291 391 - - - - - - + 92 192 292 392 192 92 - - - - + 93 193 293 393 - - 293 93 - - + 94 194 294 394 194 94 - - - - + 95 195 295 395 - - - - 395 95 + 96 - 296 396 - - - - - - + 97 197 - 397 - - - - - - + 98 198 298 - - - - - - - + 99 - - - - - - - - - +} +do_execsql_test joinD-554 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 +} +do_execsql_test joinD-555 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 1 101 201 301 - - - - - - + 2 102 202 302 102 2 - - - - + 3 103 203 303 - - 203 3 - - + 4 104 204 304 104 4 - - - - + 5 105 205 305 - - - - 305 5 + 6 106 206 306 106 6 206 6 - - + 7 107 207 307 - - - - - - + 8 108 208 308 108 8 - - - - + 9 109 209 309 - - 209 9 - - + 10 110 210 310 110 10 - - 310 10 + 11 111 211 311 - - - - - - + 12 112 212 312 112 12 212 12 - - + 13 113 213 313 - - - - - - + 14 114 214 314 114 14 - - - - + 15 115 215 315 - - 215 15 315 15 + 16 116 216 316 116 16 - - - - + 17 117 217 317 - - - - - - + 18 118 218 318 118 18 218 18 - - + 19 119 219 319 - - - - - - + 20 120 220 320 120 20 - - 320 20 + 21 121 221 321 - - 221 21 - - + 22 122 222 322 122 22 - - - - + 23 123 223 323 - - - - - - + 24 124 224 324 124 24 224 24 - - + 25 125 225 325 - - - - 325 25 + 26 126 226 326 126 26 - - - - + 27 127 227 327 - - 227 27 - - + 28 128 228 328 128 28 - - - - + 29 129 229 329 - - - - - - + 30 130 230 330 130 30 230 30 330 30 + 31 131 231 331 - - - - - - + 32 132 232 332 132 32 - - - - + 33 133 233 333 - - 233 33 - - + 34 134 234 334 134 34 - - - - + 35 135 235 335 - - - - 335 35 + 36 136 236 336 136 36 236 36 - - + 37 137 237 337 - - - - - - + 38 138 238 338 138 38 - - - - + 39 139 239 339 - - 239 39 - - + 40 140 240 340 140 40 - - 340 40 + 41 141 241 341 - - - - - - + 42 142 242 342 142 42 242 42 - - + 43 143 243 343 - - - - - - + 44 144 244 344 144 44 - - - - + 45 145 245 345 - - 245 45 345 45 + 46 146 246 346 146 46 - - - - + 47 147 247 347 - - - - - - + 48 148 248 348 148 48 248 48 - - + 49 149 249 349 - - - - - - + 50 150 250 350 150 50 - - 350 50 + 51 151 251 351 - - 251 51 - - + 52 152 252 352 152 52 - - - - + 53 153 253 353 - - - - - - + 54 154 254 354 154 54 254 54 - - + 55 155 255 355 - - - - 355 55 + 56 156 256 356 156 56 - - - - + 57 157 257 357 - - 257 57 - - + 58 158 258 358 158 58 - - - - + 59 159 259 359 - - - - - - + 60 160 260 360 160 60 260 60 360 60 + 61 161 261 361 - - - - - - + 62 162 262 362 162 62 - - - - + 63 163 263 363 - - 263 63 - - + 64 164 264 364 164 64 - - - - + 65 165 265 365 - - - - 365 65 + 66 166 266 366 166 66 266 66 - - + 67 167 267 367 - - - - - - + 68 168 268 368 168 68 - - - - + 69 169 269 369 - - 269 69 - - + 70 170 270 370 170 70 - - 370 70 + 71 171 271 371 - - - - - - + 72 172 272 372 172 72 272 72 - - + 73 173 273 373 - - - - - - + 74 174 274 374 174 74 - - - - + 75 175 275 375 - - 275 75 375 75 + 76 176 276 376 176 76 - - - - + 77 177 277 377 - - - - - - + 78 178 278 378 178 78 278 78 - - + 79 179 279 379 - - - - - - + 80 180 280 380 180 80 - - 380 80 + 81 181 281 381 - - 281 81 - - + 82 182 282 382 182 82 - - - - + 83 183 283 383 - - - - - - + 84 184 284 384 184 84 284 84 - - + 85 185 285 385 - - - - 385 85 + 86 186 286 386 186 86 - - - - + 87 187 287 387 - - 287 87 - - + 88 188 288 388 188 88 - - - - + 89 189 289 389 - - - - - - + 90 190 290 390 190 90 290 90 390 90 + 91 191 291 391 - - - - - - + 92 192 292 392 192 92 - - - - + 93 193 293 393 - - 293 93 - - + 94 194 294 394 194 94 - - - - + 95 195 295 395 - - - - 395 95 + 96 - 296 396 - - - - - - + 97 197 - 397 - - - - - - + 98 198 298 - - - - - - - + 99 - - - - - - - - - + - - - - - - 200 0 - - +} +do_execsql_test joinD-556 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-557 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-558 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-559 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-560 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c = t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 0 100 200 300 - - - - - - + 1 101 201 301 - - - - - - + 2 102 202 302 102 2 - - - - + 3 103 203 303 - - 203 3 - - + 4 104 204 304 104 4 - - - - + 5 105 205 305 - - - - 305 5 + 6 106 206 306 106 6 206 6 - - + 7 107 207 307 - - - - - - + 8 108 208 308 108 8 - - - - + 9 109 209 309 - - 209 9 - - + 10 110 210 310 110 10 - - 310 10 + 11 111 211 311 - - - - - - + 12 112 212 312 112 12 212 12 - - + 13 113 213 313 - - - - - - + 14 114 214 314 114 14 - - - - + 15 115 215 315 - - 215 15 315 15 + 16 116 216 316 116 16 - - - - + 17 117 217 317 - - - - - - + 18 118 218 318 118 18 218 18 - - + 19 119 219 319 - - - - - - + 20 120 220 320 120 20 - - 320 20 + 21 121 221 321 - - 221 21 - - + 22 122 222 322 122 22 - - - - + 23 123 223 323 - - - - - - + 24 124 224 324 124 24 224 24 - - + 25 125 225 325 - - - - 325 25 + 26 126 226 326 126 26 - - - - + 27 127 227 327 - - 227 27 - - + 28 128 228 328 128 28 - - - - + 29 129 229 329 - - - - - - + 30 130 230 330 130 30 230 30 330 30 + 31 131 231 331 - - - - - - + 32 132 232 332 132 32 - - - - + 33 133 233 333 - - 233 33 - - + 34 134 234 334 134 34 - - - - + 35 135 235 335 - - - - 335 35 + 36 136 236 336 136 36 236 36 - - + 37 137 237 337 - - - - - - + 38 138 238 338 138 38 - - - - + 39 139 239 339 - - 239 39 - - + 40 140 240 340 140 40 - - 340 40 + 41 141 241 341 - - - - - - + 42 142 242 342 142 42 242 42 - - + 43 143 243 343 - - - - - - + 44 144 244 344 144 44 - - - - + 45 145 245 345 - - 245 45 345 45 + 46 146 246 346 146 46 - - - - + 47 147 247 347 - - - - - - + 48 148 248 348 148 48 248 48 - - + 49 149 249 349 - - - - - - + 50 150 250 350 150 50 - - 350 50 + 51 151 251 351 - - 251 51 - - + 52 152 252 352 152 52 - - - - + 53 153 253 353 - - - - - - + 54 154 254 354 154 54 254 54 - - + 55 155 255 355 - - - - 355 55 + 56 156 256 356 156 56 - - - - + 57 157 257 357 - - 257 57 - - + 58 158 258 358 158 58 - - - - + 59 159 259 359 - - - - - - + 60 160 260 360 160 60 260 60 360 60 + 61 161 261 361 - - - - - - + 62 162 262 362 162 62 - - - - + 63 163 263 363 - - 263 63 - - + 64 164 264 364 164 64 - - - - + 65 165 265 365 - - - - 365 65 + 66 166 266 366 166 66 266 66 - - + 67 167 267 367 - - - - - - + 68 168 268 368 168 68 - - - - + 69 169 269 369 - - 269 69 - - + 70 170 270 370 170 70 - - 370 70 + 71 171 271 371 - - - - - - + 72 172 272 372 172 72 272 72 - - + 73 173 273 373 - - - - - - + 74 174 274 374 174 74 - - - - + 75 175 275 375 - - 275 75 375 75 + 76 176 276 376 176 76 - - - - + 77 177 277 377 - - - - - - + 78 178 278 378 178 78 278 78 - - + 79 179 279 379 - - - - - - + 80 180 280 380 180 80 - - 380 80 + 81 181 281 381 - - 281 81 - - + 82 182 282 382 182 82 - - - - + 83 183 283 383 - - - - - - + 84 184 284 384 184 84 284 84 - - + 85 185 285 385 - - - - 385 85 + 86 186 286 386 186 86 - - - - + 87 187 287 387 - - 287 87 - - + 88 188 288 388 188 88 - - - - + 89 189 289 389 - - - - - - + 90 190 290 390 190 90 290 90 390 90 + 91 191 291 391 - - - - - - + 92 192 292 392 192 92 - - - - + 93 193 293 393 - - 293 93 - - + 94 194 294 394 194 94 - - - - + 95 195 295 395 - - - - 395 95 + 96 - 296 396 - - - - - - + 97 197 - 397 - - - - - - + 98 198 298 - - - - - - - + 99 - - - - - - - - - + - - - - - - 200 0 - - +} +do_execsql_test joinD-561 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t2.x>0 + FULL JOIN t3 ON t1.c = t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - - - 200 0 - - +} +do_execsql_test joinD-562 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t2.x>0 + FULL JOIN t3 ON t3.y>0 + LEFT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.c = t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-563 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t2.x>0 + FULL JOIN t3 ON t1.c = t3.c AND t3.y>0 + LEFT JOIN t4 ON t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.d IS NOT DISTINCT FROM t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-564 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b IN (t2.b,-2,-3) AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 0 100 200 300 - - - - - - + 1 101 201 301 - - - - - - + 2 102 202 302 102 2 - - - - + 3 103 203 303 - - 203 3 - - + 4 104 204 304 104 4 - - - - + 5 105 205 305 - - - - 305 5 + 6 106 206 306 106 6 206 6 - - + 7 107 207 307 - - - - - - + 8 108 208 308 108 8 - - - - + 9 109 209 309 - - 209 9 - - + 10 110 210 310 110 10 - - 310 10 + 11 111 211 311 - - - - - - + 12 112 212 312 112 12 212 12 - - + 13 113 213 313 - - - - - - + 14 114 214 314 114 14 - - - - + 15 115 215 315 - - 215 15 315 15 + 16 116 216 316 116 16 - - - - + 17 117 217 317 - - - - - - + 18 118 218 318 118 18 218 18 - - + 19 119 219 319 - - - - - - + 20 120 220 320 120 20 - - 320 20 + 21 121 221 321 - - 221 21 - - + 22 122 222 322 122 22 - - - - + 23 123 223 323 - - - - - - + 24 124 224 324 124 24 224 24 - - + 25 125 225 325 - - - - 325 25 + 26 126 226 326 126 26 - - - - + 27 127 227 327 - - 227 27 - - + 28 128 228 328 128 28 - - - - + 29 129 229 329 - - - - - - + 30 130 230 330 130 30 230 30 330 30 + 31 131 231 331 - - - - - - + 32 132 232 332 132 32 - - - - + 33 133 233 333 - - 233 33 - - + 34 134 234 334 134 34 - - - - + 35 135 235 335 - - - - 335 35 + 36 136 236 336 136 36 236 36 - - + 37 137 237 337 - - - - - - + 38 138 238 338 138 38 - - - - + 39 139 239 339 - - 239 39 - - + 40 140 240 340 140 40 - - 340 40 + 41 141 241 341 - - - - - - + 42 142 242 342 142 42 242 42 - - + 43 143 243 343 - - - - - - + 44 144 244 344 144 44 - - - - + 45 145 245 345 - - 245 45 345 45 + 46 146 246 346 146 46 - - - - + 47 147 247 347 - - - - - - + 48 148 248 348 148 48 248 48 - - + 49 149 249 349 - - - - - - + 50 150 250 350 150 50 - - 350 50 + 51 151 251 351 - - 251 51 - - + 52 152 252 352 152 52 - - - - + 53 153 253 353 - - - - - - + 54 154 254 354 154 54 254 54 - - + 55 155 255 355 - - - - 355 55 + 56 156 256 356 156 56 - - - - + 57 157 257 357 - - 257 57 - - + 58 158 258 358 158 58 - - - - + 59 159 259 359 - - - - - - + 60 160 260 360 160 60 260 60 360 60 + 61 161 261 361 - - - - - - + 62 162 262 362 162 62 - - - - + 63 163 263 363 - - 263 63 - - + 64 164 264 364 164 64 - - - - + 65 165 265 365 - - - - 365 65 + 66 166 266 366 166 66 266 66 - - + 67 167 267 367 - - - - - - + 68 168 268 368 168 68 - - - - + 69 169 269 369 - - 269 69 - - + 70 170 270 370 170 70 - - 370 70 + 71 171 271 371 - - - - - - + 72 172 272 372 172 72 272 72 - - + 73 173 273 373 - - - - - - + 74 174 274 374 174 74 - - - - + 75 175 275 375 - - 275 75 375 75 + 76 176 276 376 176 76 - - - - + 77 177 277 377 - - - - - - + 78 178 278 378 178 78 278 78 - - + 79 179 279 379 - - - - - - + 80 180 280 380 180 80 - - 380 80 + 81 181 281 381 - - 281 81 - - + 82 182 282 382 182 82 - - - - + 83 183 283 383 - - - - - - + 84 184 284 384 184 84 284 84 - - + 85 185 285 385 - - - - 385 85 + 86 186 286 386 186 86 - - - - + 87 187 287 387 - - 287 87 - - + 88 188 288 388 188 88 - - - - + 89 189 289 389 - - - - - - + 90 190 290 390 190 90 290 90 390 90 + 91 191 291 391 - - - - - - + 92 192 292 392 192 92 - - - - + 93 193 293 393 - - 293 93 - - + 94 194 294 394 194 94 - - - - + 95 195 295 395 - - - - 395 95 + 96 - 296 396 - - - - - - + 97 197 - 397 - - - - - - + 98 198 298 - - - - - - - + 99 - - - - - - - - - + - - - - - - 200 0 - - +} +do_execsql_test joinD-565 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 + - - - - - - - - 300 0 +} +do_execsql_test joinD-566 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 + - - - - - - - - 300 0 +} +do_execsql_test joinD-567 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-568 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 + - - - - - - - - 300 0 +} +do_execsql_test joinD-569 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON true + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-570 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-571 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 + - - - - - - - - 300 0 +} +do_execsql_test joinD-572 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 +} +do_execsql_test joinD-573 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 +} +do_execsql_test joinD-574 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-575 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-576 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-577 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-578 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c = t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 + - - - - - - - - 300 0 +} +do_execsql_test joinD-579 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t2.x>0 + FULL JOIN t3 ON t1.c = t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 +} +do_execsql_test joinD-580 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t2.x>0 + FULL JOIN t3 ON t3.y>0 + RIGHT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.c = t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-581 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t2.x>0 + FULL JOIN t3 ON t1.c = t3.c AND t3.y>0 + RIGHT JOIN t4 ON t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.d IS NOT DISTINCT FROM t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-582 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b IN (t2.b,-2,-3) AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 + - - - - - - - - 300 0 +} +do_execsql_test joinD-583 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 0 100 200 300 - - - - - - + 1 101 201 301 - - - - - - + 2 102 202 302 102 2 - - - - + 3 103 203 303 - - 203 3 - - + 4 104 204 304 104 4 - - - - + 5 105 205 305 - - - - 305 5 + 6 106 206 306 106 6 206 6 - - + 7 107 207 307 - - - - - - + 8 108 208 308 108 8 - - - - + 9 109 209 309 - - 209 9 - - + 10 110 210 310 110 10 - - 310 10 + 11 111 211 311 - - - - - - + 12 112 212 312 112 12 212 12 - - + 13 113 213 313 - - - - - - + 14 114 214 314 114 14 - - - - + 15 115 215 315 - - 215 15 315 15 + 16 116 216 316 116 16 - - - - + 17 117 217 317 - - - - - - + 18 118 218 318 118 18 218 18 - - + 19 119 219 319 - - - - - - + 20 120 220 320 120 20 - - 320 20 + 21 121 221 321 - - 221 21 - - + 22 122 222 322 122 22 - - - - + 23 123 223 323 - - - - - - + 24 124 224 324 124 24 224 24 - - + 25 125 225 325 - - - - 325 25 + 26 126 226 326 126 26 - - - - + 27 127 227 327 - - 227 27 - - + 28 128 228 328 128 28 - - - - + 29 129 229 329 - - - - - - + 30 130 230 330 130 30 230 30 330 30 + 31 131 231 331 - - - - - - + 32 132 232 332 132 32 - - - - + 33 133 233 333 - - 233 33 - - + 34 134 234 334 134 34 - - - - + 35 135 235 335 - - - - 335 35 + 36 136 236 336 136 36 236 36 - - + 37 137 237 337 - - - - - - + 38 138 238 338 138 38 - - - - + 39 139 239 339 - - 239 39 - - + 40 140 240 340 140 40 - - 340 40 + 41 141 241 341 - - - - - - + 42 142 242 342 142 42 242 42 - - + 43 143 243 343 - - - - - - + 44 144 244 344 144 44 - - - - + 45 145 245 345 - - 245 45 345 45 + 46 146 246 346 146 46 - - - - + 47 147 247 347 - - - - - - + 48 148 248 348 148 48 248 48 - - + 49 149 249 349 - - - - - - + 50 150 250 350 150 50 - - 350 50 + 51 151 251 351 - - 251 51 - - + 52 152 252 352 152 52 - - - - + 53 153 253 353 - - - - - - + 54 154 254 354 154 54 254 54 - - + 55 155 255 355 - - - - 355 55 + 56 156 256 356 156 56 - - - - + 57 157 257 357 - - 257 57 - - + 58 158 258 358 158 58 - - - - + 59 159 259 359 - - - - - - + 60 160 260 360 160 60 260 60 360 60 + 61 161 261 361 - - - - - - + 62 162 262 362 162 62 - - - - + 63 163 263 363 - - 263 63 - - + 64 164 264 364 164 64 - - - - + 65 165 265 365 - - - - 365 65 + 66 166 266 366 166 66 266 66 - - + 67 167 267 367 - - - - - - + 68 168 268 368 168 68 - - - - + 69 169 269 369 - - 269 69 - - + 70 170 270 370 170 70 - - 370 70 + 71 171 271 371 - - - - - - + 72 172 272 372 172 72 272 72 - - + 73 173 273 373 - - - - - - + 74 174 274 374 174 74 - - - - + 75 175 275 375 - - 275 75 375 75 + 76 176 276 376 176 76 - - - - + 77 177 277 377 - - - - - - + 78 178 278 378 178 78 278 78 - - + 79 179 279 379 - - - - - - + 80 180 280 380 180 80 - - 380 80 + 81 181 281 381 - - 281 81 - - + 82 182 282 382 182 82 - - - - + 83 183 283 383 - - - - - - + 84 184 284 384 184 84 284 84 - - + 85 185 285 385 - - - - 385 85 + 86 186 286 386 186 86 - - - - + 87 187 287 387 - - 287 87 - - + 88 188 288 388 188 88 - - - - + 89 189 289 389 - - - - - - + 90 190 290 390 190 90 290 90 390 90 + 91 191 291 391 - - - - - - + 92 192 292 392 192 92 - - - - + 93 193 293 393 - - 293 93 - - + 94 194 294 394 194 94 - - - - + 95 195 295 395 - - - - 395 95 + 96 - 296 396 - - - - - - + 97 197 - 397 - - - - - - + 98 198 298 - - - - - - - + 99 - - - - - - - - - + - - - - - - 200 0 - - + - - - - - - - - 300 0 +} +do_execsql_test joinD-584 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 0 100 200 300 - - - - - - + 1 101 201 301 - - - - - - + 2 102 202 302 102 2 - - - - + 3 103 203 303 - - 203 3 - - + 4 104 204 304 104 4 - - - - + 5 105 205 305 - - - - 305 5 + 6 106 206 306 106 6 206 6 - - + 7 107 207 307 - - - - - - + 8 108 208 308 108 8 - - - - + 9 109 209 309 - - 209 9 - - + 10 110 210 310 110 10 - - 310 10 + 11 111 211 311 - - - - - - + 12 112 212 312 112 12 212 12 - - + 13 113 213 313 - - - - - - + 14 114 214 314 114 14 - - - - + 15 115 215 315 - - 215 15 315 15 + 16 116 216 316 116 16 - - - - + 17 117 217 317 - - - - - - + 18 118 218 318 118 18 218 18 - - + 19 119 219 319 - - - - - - + 20 120 220 320 120 20 - - 320 20 + 21 121 221 321 - - 221 21 - - + 22 122 222 322 122 22 - - - - + 23 123 223 323 - - - - - - + 24 124 224 324 124 24 224 24 - - + 25 125 225 325 - - - - 325 25 + 26 126 226 326 126 26 - - - - + 27 127 227 327 - - 227 27 - - + 28 128 228 328 128 28 - - - - + 29 129 229 329 - - - - - - + 30 130 230 330 130 30 230 30 330 30 + 31 131 231 331 - - - - - - + 32 132 232 332 132 32 - - - - + 33 133 233 333 - - 233 33 - - + 34 134 234 334 134 34 - - - - + 35 135 235 335 - - - - 335 35 + 36 136 236 336 136 36 236 36 - - + 37 137 237 337 - - - - - - + 38 138 238 338 138 38 - - - - + 39 139 239 339 - - 239 39 - - + 40 140 240 340 140 40 - - 340 40 + 41 141 241 341 - - - - - - + 42 142 242 342 142 42 242 42 - - + 43 143 243 343 - - - - - - + 44 144 244 344 144 44 - - - - + 45 145 245 345 - - 245 45 345 45 + 46 146 246 346 146 46 - - - - + 47 147 247 347 - - - - - - + 48 148 248 348 148 48 248 48 - - + 49 149 249 349 - - - - - - + 50 150 250 350 150 50 - - 350 50 + 51 151 251 351 - - 251 51 - - + 52 152 252 352 152 52 - - - - + 53 153 253 353 - - - - - - + 54 154 254 354 154 54 254 54 - - + 55 155 255 355 - - - - 355 55 + 56 156 256 356 156 56 - - - - + 57 157 257 357 - - 257 57 - - + 58 158 258 358 158 58 - - - - + 59 159 259 359 - - - - - - + 60 160 260 360 160 60 260 60 360 60 + 61 161 261 361 - - - - - - + 62 162 262 362 162 62 - - - - + 63 163 263 363 - - 263 63 - - + 64 164 264 364 164 64 - - - - + 65 165 265 365 - - - - 365 65 + 66 166 266 366 166 66 266 66 - - + 67 167 267 367 - - - - - - + 68 168 268 368 168 68 - - - - + 69 169 269 369 - - 269 69 - - + 70 170 270 370 170 70 - - 370 70 + 71 171 271 371 - - - - - - + 72 172 272 372 172 72 272 72 - - + 73 173 273 373 - - - - - - + 74 174 274 374 174 74 - - - - + 75 175 275 375 - - 275 75 375 75 + 76 176 276 376 176 76 - - - - + 77 177 277 377 - - - - - - + 78 178 278 378 178 78 278 78 - - + 79 179 279 379 - - - - - - + 80 180 280 380 180 80 - - 380 80 + 81 181 281 381 - - 281 81 - - + 82 182 282 382 182 82 - - - - + 83 183 283 383 - - - - - - + 84 184 284 384 184 84 284 84 - - + 85 185 285 385 - - - - 385 85 + 86 186 286 386 186 86 - - - - + 87 187 287 387 - - 287 87 - - + 88 188 288 388 188 88 - - - - + 89 189 289 389 - - - - - - + 90 190 290 390 190 90 290 90 390 90 + 91 191 291 391 - - - - - - + 92 192 292 392 192 92 - - - - + 93 193 293 393 - - 293 93 - - + 94 194 294 394 194 94 - - - - + 95 195 295 395 - - - - 395 95 + 96 - 296 396 - - - - - - + 97 197 - 397 - - - - - - + 98 198 298 - - - - - - - + 99 - - - - - - - - - + - - - - - - 200 0 - - + - - - - - - - - 300 0 +} +do_execsql_test joinD-585 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - +} +do_execsql_test joinD-586 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 1 101 201 301 - - - - - - + 2 102 202 302 102 2 - - - - + 3 103 203 303 - - 203 3 - - + 4 104 204 304 104 4 - - - - + 5 105 205 305 - - - - 305 5 + 6 106 206 306 106 6 206 6 - - + 7 107 207 307 - - - - - - + 8 108 208 308 108 8 - - - - + 9 109 209 309 - - 209 9 - - + 10 110 210 310 110 10 - - 310 10 + 11 111 211 311 - - - - - - + 12 112 212 312 112 12 212 12 - - + 13 113 213 313 - - - - - - + 14 114 214 314 114 14 - - - - + 15 115 215 315 - - 215 15 315 15 + 16 116 216 316 116 16 - - - - + 17 117 217 317 - - - - - - + 18 118 218 318 118 18 218 18 - - + 19 119 219 319 - - - - - - + 20 120 220 320 120 20 - - 320 20 + 21 121 221 321 - - 221 21 - - + 22 122 222 322 122 22 - - - - + 23 123 223 323 - - - - - - + 24 124 224 324 124 24 224 24 - - + 25 125 225 325 - - - - 325 25 + 26 126 226 326 126 26 - - - - + 27 127 227 327 - - 227 27 - - + 28 128 228 328 128 28 - - - - + 29 129 229 329 - - - - - - + 30 130 230 330 130 30 230 30 330 30 + 31 131 231 331 - - - - - - + 32 132 232 332 132 32 - - - - + 33 133 233 333 - - 233 33 - - + 34 134 234 334 134 34 - - - - + 35 135 235 335 - - - - 335 35 + 36 136 236 336 136 36 236 36 - - + 37 137 237 337 - - - - - - + 38 138 238 338 138 38 - - - - + 39 139 239 339 - - 239 39 - - + 40 140 240 340 140 40 - - 340 40 + 41 141 241 341 - - - - - - + 42 142 242 342 142 42 242 42 - - + 43 143 243 343 - - - - - - + 44 144 244 344 144 44 - - - - + 45 145 245 345 - - 245 45 345 45 + 46 146 246 346 146 46 - - - - + 47 147 247 347 - - - - - - + 48 148 248 348 148 48 248 48 - - + 49 149 249 349 - - - - - - + 50 150 250 350 150 50 - - 350 50 + 51 151 251 351 - - 251 51 - - + 52 152 252 352 152 52 - - - - + 53 153 253 353 - - - - - - + 54 154 254 354 154 54 254 54 - - + 55 155 255 355 - - - - 355 55 + 56 156 256 356 156 56 - - - - + 57 157 257 357 - - 257 57 - - + 58 158 258 358 158 58 - - - - + 59 159 259 359 - - - - - - + 60 160 260 360 160 60 260 60 360 60 + 61 161 261 361 - - - - - - + 62 162 262 362 162 62 - - - - + 63 163 263 363 - - 263 63 - - + 64 164 264 364 164 64 - - - - + 65 165 265 365 - - - - 365 65 + 66 166 266 366 166 66 266 66 - - + 67 167 267 367 - - - - - - + 68 168 268 368 168 68 - - - - + 69 169 269 369 - - 269 69 - - + 70 170 270 370 170 70 - - 370 70 + 71 171 271 371 - - - - - - + 72 172 272 372 172 72 272 72 - - + 73 173 273 373 - - - - - - + 74 174 274 374 174 74 - - - - + 75 175 275 375 - - 275 75 375 75 + 76 176 276 376 176 76 - - - - + 77 177 277 377 - - - - - - + 78 178 278 378 178 78 278 78 - - + 79 179 279 379 - - - - - - + 80 180 280 380 180 80 - - 380 80 + 81 181 281 381 - - 281 81 - - + 82 182 282 382 182 82 - - - - + 83 183 283 383 - - - - - - + 84 184 284 384 184 84 284 84 - - + 85 185 285 385 - - - - 385 85 + 86 186 286 386 186 86 - - - - + 87 187 287 387 - - 287 87 - - + 88 188 288 388 188 88 - - - - + 89 189 289 389 - - - - - - + 90 190 290 390 190 90 290 90 390 90 + 91 191 291 391 - - - - - - + 92 192 292 392 192 92 - - - - + 93 193 293 393 - - 293 93 - - + 94 194 294 394 194 94 - - - - + 95 195 295 395 - - - - 395 95 + 96 - 296 396 - - - - - - + 97 197 - 397 - - - - - - + 98 198 298 - - - - - - - + 99 - - - - - - - - - + - - - - - - 200 0 - - + - - - - - - - - 300 0 +} +do_execsql_test joinD-587 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON true + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - +} +do_execsql_test joinD-588 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - +} +do_execsql_test joinD-589 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 1 101 201 301 - - - - - - + 2 102 202 302 102 2 - - - - + 3 103 203 303 - - 203 3 - - + 4 104 204 304 104 4 - - - - + 5 105 205 305 - - - - 305 5 + 6 106 206 306 106 6 206 6 - - + 7 107 207 307 - - - - - - + 8 108 208 308 108 8 - - - - + 9 109 209 309 - - 209 9 - - + 10 110 210 310 110 10 - - 310 10 + 11 111 211 311 - - - - - - + 12 112 212 312 112 12 212 12 - - + 13 113 213 313 - - - - - - + 14 114 214 314 114 14 - - - - + 15 115 215 315 - - 215 15 315 15 + 16 116 216 316 116 16 - - - - + 17 117 217 317 - - - - - - + 18 118 218 318 118 18 218 18 - - + 19 119 219 319 - - - - - - + 20 120 220 320 120 20 - - 320 20 + 21 121 221 321 - - 221 21 - - + 22 122 222 322 122 22 - - - - + 23 123 223 323 - - - - - - + 24 124 224 324 124 24 224 24 - - + 25 125 225 325 - - - - 325 25 + 26 126 226 326 126 26 - - - - + 27 127 227 327 - - 227 27 - - + 28 128 228 328 128 28 - - - - + 29 129 229 329 - - - - - - + 30 130 230 330 130 30 230 30 330 30 + 31 131 231 331 - - - - - - + 32 132 232 332 132 32 - - - - + 33 133 233 333 - - 233 33 - - + 34 134 234 334 134 34 - - - - + 35 135 235 335 - - - - 335 35 + 36 136 236 336 136 36 236 36 - - + 37 137 237 337 - - - - - - + 38 138 238 338 138 38 - - - - + 39 139 239 339 - - 239 39 - - + 40 140 240 340 140 40 - - 340 40 + 41 141 241 341 - - - - - - + 42 142 242 342 142 42 242 42 - - + 43 143 243 343 - - - - - - + 44 144 244 344 144 44 - - - - + 45 145 245 345 - - 245 45 345 45 + 46 146 246 346 146 46 - - - - + 47 147 247 347 - - - - - - + 48 148 248 348 148 48 248 48 - - + 49 149 249 349 - - - - - - + 50 150 250 350 150 50 - - 350 50 + 51 151 251 351 - - 251 51 - - + 52 152 252 352 152 52 - - - - + 53 153 253 353 - - - - - - + 54 154 254 354 154 54 254 54 - - + 55 155 255 355 - - - - 355 55 + 56 156 256 356 156 56 - - - - + 57 157 257 357 - - 257 57 - - + 58 158 258 358 158 58 - - - - + 59 159 259 359 - - - - - - + 60 160 260 360 160 60 260 60 360 60 + 61 161 261 361 - - - - - - + 62 162 262 362 162 62 - - - - + 63 163 263 363 - - 263 63 - - + 64 164 264 364 164 64 - - - - + 65 165 265 365 - - - - 365 65 + 66 166 266 366 166 66 266 66 - - + 67 167 267 367 - - - - - - + 68 168 268 368 168 68 - - - - + 69 169 269 369 - - 269 69 - - + 70 170 270 370 170 70 - - 370 70 + 71 171 271 371 - - - - - - + 72 172 272 372 172 72 272 72 - - + 73 173 273 373 - - - - - - + 74 174 274 374 174 74 - - - - + 75 175 275 375 - - 275 75 375 75 + 76 176 276 376 176 76 - - - - + 77 177 277 377 - - - - - - + 78 178 278 378 178 78 278 78 - - + 79 179 279 379 - - - - - - + 80 180 280 380 180 80 - - 380 80 + 81 181 281 381 - - 281 81 - - + 82 182 282 382 182 82 - - - - + 83 183 283 383 - - - - - - + 84 184 284 384 184 84 284 84 - - + 85 185 285 385 - - - - 385 85 + 86 186 286 386 186 86 - - - - + 87 187 287 387 - - 287 87 - - + 88 188 288 388 188 88 - - - - + 89 189 289 389 - - - - - - + 90 190 290 390 190 90 290 90 390 90 + 91 191 291 391 - - - - - - + 92 192 292 392 192 92 - - - - + 93 193 293 393 - - 293 93 - - + 94 194 294 394 194 94 - - - - + 95 195 295 395 - - - - 395 95 + 96 - 296 396 - - - - - - + 97 197 - 397 - - - - - - + 98 198 298 - - - - - - - + 99 - - - - - - - - - + - - - - - - - - 300 0 +} +do_execsql_test joinD-590 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 +} +do_execsql_test joinD-591 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 1 101 201 301 - - - - - - + 2 102 202 302 102 2 - - - - + 3 103 203 303 - - 203 3 - - + 4 104 204 304 104 4 - - - - + 5 105 205 305 - - - - 305 5 + 6 106 206 306 106 6 206 6 - - + 7 107 207 307 - - - - - - + 8 108 208 308 108 8 - - - - + 9 109 209 309 - - 209 9 - - + 10 110 210 310 110 10 - - 310 10 + 11 111 211 311 - - - - - - + 12 112 212 312 112 12 212 12 - - + 13 113 213 313 - - - - - - + 14 114 214 314 114 14 - - - - + 15 115 215 315 - - 215 15 315 15 + 16 116 216 316 116 16 - - - - + 17 117 217 317 - - - - - - + 18 118 218 318 118 18 218 18 - - + 19 119 219 319 - - - - - - + 20 120 220 320 120 20 - - 320 20 + 21 121 221 321 - - 221 21 - - + 22 122 222 322 122 22 - - - - + 23 123 223 323 - - - - - - + 24 124 224 324 124 24 224 24 - - + 25 125 225 325 - - - - 325 25 + 26 126 226 326 126 26 - - - - + 27 127 227 327 - - 227 27 - - + 28 128 228 328 128 28 - - - - + 29 129 229 329 - - - - - - + 30 130 230 330 130 30 230 30 330 30 + 31 131 231 331 - - - - - - + 32 132 232 332 132 32 - - - - + 33 133 233 333 - - 233 33 - - + 34 134 234 334 134 34 - - - - + 35 135 235 335 - - - - 335 35 + 36 136 236 336 136 36 236 36 - - + 37 137 237 337 - - - - - - + 38 138 238 338 138 38 - - - - + 39 139 239 339 - - 239 39 - - + 40 140 240 340 140 40 - - 340 40 + 41 141 241 341 - - - - - - + 42 142 242 342 142 42 242 42 - - + 43 143 243 343 - - - - - - + 44 144 244 344 144 44 - - - - + 45 145 245 345 - - 245 45 345 45 + 46 146 246 346 146 46 - - - - + 47 147 247 347 - - - - - - + 48 148 248 348 148 48 248 48 - - + 49 149 249 349 - - - - - - + 50 150 250 350 150 50 - - 350 50 + 51 151 251 351 - - 251 51 - - + 52 152 252 352 152 52 - - - - + 53 153 253 353 - - - - - - + 54 154 254 354 154 54 254 54 - - + 55 155 255 355 - - - - 355 55 + 56 156 256 356 156 56 - - - - + 57 157 257 357 - - 257 57 - - + 58 158 258 358 158 58 - - - - + 59 159 259 359 - - - - - - + 60 160 260 360 160 60 260 60 360 60 + 61 161 261 361 - - - - - - + 62 162 262 362 162 62 - - - - + 63 163 263 363 - - 263 63 - - + 64 164 264 364 164 64 - - - - + 65 165 265 365 - - - - 365 65 + 66 166 266 366 166 66 266 66 - - + 67 167 267 367 - - - - - - + 68 168 268 368 168 68 - - - - + 69 169 269 369 - - 269 69 - - + 70 170 270 370 170 70 - - 370 70 + 71 171 271 371 - - - - - - + 72 172 272 372 172 72 272 72 - - + 73 173 273 373 - - - - - - + 74 174 274 374 174 74 - - - - + 75 175 275 375 - - 275 75 375 75 + 76 176 276 376 176 76 - - - - + 77 177 277 377 - - - - - - + 78 178 278 378 178 78 278 78 - - + 79 179 279 379 - - - - - - + 80 180 280 380 180 80 - - 380 80 + 81 181 281 381 - - 281 81 - - + 82 182 282 382 182 82 - - - - + 83 183 283 383 - - - - - - + 84 184 284 384 184 84 284 84 - - + 85 185 285 385 - - - - 385 85 + 86 186 286 386 186 86 - - - - + 87 187 287 387 - - 287 87 - - + 88 188 288 388 188 88 - - - - + 89 189 289 389 - - - - - - + 90 190 290 390 190 90 290 90 390 90 + 91 191 291 391 - - - - - - + 92 192 292 392 192 92 - - - - + 93 193 293 393 - - 293 93 - - + 94 194 294 394 194 94 - - - - + 95 195 295 395 - - - - 395 95 + 96 - 296 396 - - - - - - + 97 197 - 397 - - - - - - + 98 198 298 - - - - - - - + 99 - - - - - - - - - + - - - - - - 200 0 - - +} +do_execsql_test joinD-592 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-593 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-594 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-595 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-596 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c = t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d = t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 0 100 200 300 - - - - - - + 1 101 201 301 - - - - - - + 2 102 202 302 102 2 - - - - + 3 103 203 303 - - 203 3 - - + 4 104 204 304 104 4 - - - - + 5 105 205 305 - - - - 305 5 + 6 106 206 306 106 6 206 6 - - + 7 107 207 307 - - - - - - + 8 108 208 308 108 8 - - - - + 9 109 209 309 - - 209 9 - - + 10 110 210 310 110 10 - - 310 10 + 11 111 211 311 - - - - - - + 12 112 212 312 112 12 212 12 - - + 13 113 213 313 - - - - - - + 14 114 214 314 114 14 - - - - + 15 115 215 315 - - 215 15 315 15 + 16 116 216 316 116 16 - - - - + 17 117 217 317 - - - - - - + 18 118 218 318 118 18 218 18 - - + 19 119 219 319 - - - - - - + 20 120 220 320 120 20 - - 320 20 + 21 121 221 321 - - 221 21 - - + 22 122 222 322 122 22 - - - - + 23 123 223 323 - - - - - - + 24 124 224 324 124 24 224 24 - - + 25 125 225 325 - - - - 325 25 + 26 126 226 326 126 26 - - - - + 27 127 227 327 - - 227 27 - - + 28 128 228 328 128 28 - - - - + 29 129 229 329 - - - - - - + 30 130 230 330 130 30 230 30 330 30 + 31 131 231 331 - - - - - - + 32 132 232 332 132 32 - - - - + 33 133 233 333 - - 233 33 - - + 34 134 234 334 134 34 - - - - + 35 135 235 335 - - - - 335 35 + 36 136 236 336 136 36 236 36 - - + 37 137 237 337 - - - - - - + 38 138 238 338 138 38 - - - - + 39 139 239 339 - - 239 39 - - + 40 140 240 340 140 40 - - 340 40 + 41 141 241 341 - - - - - - + 42 142 242 342 142 42 242 42 - - + 43 143 243 343 - - - - - - + 44 144 244 344 144 44 - - - - + 45 145 245 345 - - 245 45 345 45 + 46 146 246 346 146 46 - - - - + 47 147 247 347 - - - - - - + 48 148 248 348 148 48 248 48 - - + 49 149 249 349 - - - - - - + 50 150 250 350 150 50 - - 350 50 + 51 151 251 351 - - 251 51 - - + 52 152 252 352 152 52 - - - - + 53 153 253 353 - - - - - - + 54 154 254 354 154 54 254 54 - - + 55 155 255 355 - - - - 355 55 + 56 156 256 356 156 56 - - - - + 57 157 257 357 - - 257 57 - - + 58 158 258 358 158 58 - - - - + 59 159 259 359 - - - - - - + 60 160 260 360 160 60 260 60 360 60 + 61 161 261 361 - - - - - - + 62 162 262 362 162 62 - - - - + 63 163 263 363 - - 263 63 - - + 64 164 264 364 164 64 - - - - + 65 165 265 365 - - - - 365 65 + 66 166 266 366 166 66 266 66 - - + 67 167 267 367 - - - - - - + 68 168 268 368 168 68 - - - - + 69 169 269 369 - - 269 69 - - + 70 170 270 370 170 70 - - 370 70 + 71 171 271 371 - - - - - - + 72 172 272 372 172 72 272 72 - - + 73 173 273 373 - - - - - - + 74 174 274 374 174 74 - - - - + 75 175 275 375 - - 275 75 375 75 + 76 176 276 376 176 76 - - - - + 77 177 277 377 - - - - - - + 78 178 278 378 178 78 278 78 - - + 79 179 279 379 - - - - - - + 80 180 280 380 180 80 - - 380 80 + 81 181 281 381 - - 281 81 - - + 82 182 282 382 182 82 - - - - + 83 183 283 383 - - - - - - + 84 184 284 384 184 84 284 84 - - + 85 185 285 385 - - - - 385 85 + 86 186 286 386 186 86 - - - - + 87 187 287 387 - - 287 87 - - + 88 188 288 388 188 88 - - - - + 89 189 289 389 - - - - - - + 90 190 290 390 190 90 290 90 390 90 + 91 191 291 391 - - - - - - + 92 192 292 392 192 92 - - - - + 93 193 293 393 - - 293 93 - - + 94 194 294 394 194 94 - - - - + 95 195 295 395 - - - - 395 95 + 96 - 296 396 - - - - - - + 97 197 - 397 - - - - - - + 98 198 298 - - - - - - - + 99 - - - - - - - - - + - - - - - - 200 0 - - + - - - - - - - - 300 0 +} +do_execsql_test joinD-597 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t2.x>0 + FULL JOIN t3 ON t1.c = t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d = t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - - - 200 0 - - + - - - - - - - - 300 0 +} +do_execsql_test joinD-598 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t2.x>0 + FULL JOIN t3 ON t3.y>0 + FULL JOIN t4 ON t1.d = t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.c = t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-599 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t2.x>0 + FULL JOIN t3 ON t1.c = t3.c AND t3.y>0 + FULL JOIN t4 ON t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.d = t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-600 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 LEFT JOIN t2 ON t1.b IN (t2.b,-2,-3) AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 0 100 200 300 - - - - - - + 1 101 201 301 - - - - - - + 2 102 202 302 102 2 - - - - + 3 103 203 303 - - 203 3 - - + 4 104 204 304 104 4 - - - - + 5 105 205 305 - - - - 305 5 + 6 106 206 306 106 6 206 6 - - + 7 107 207 307 - - - - - - + 8 108 208 308 108 8 - - - - + 9 109 209 309 - - 209 9 - - + 10 110 210 310 110 10 - - 310 10 + 11 111 211 311 - - - - - - + 12 112 212 312 112 12 212 12 - - + 13 113 213 313 - - - - - - + 14 114 214 314 114 14 - - - - + 15 115 215 315 - - 215 15 315 15 + 16 116 216 316 116 16 - - - - + 17 117 217 317 - - - - - - + 18 118 218 318 118 18 218 18 - - + 19 119 219 319 - - - - - - + 20 120 220 320 120 20 - - 320 20 + 21 121 221 321 - - 221 21 - - + 22 122 222 322 122 22 - - - - + 23 123 223 323 - - - - - - + 24 124 224 324 124 24 224 24 - - + 25 125 225 325 - - - - 325 25 + 26 126 226 326 126 26 - - - - + 27 127 227 327 - - 227 27 - - + 28 128 228 328 128 28 - - - - + 29 129 229 329 - - - - - - + 30 130 230 330 130 30 230 30 330 30 + 31 131 231 331 - - - - - - + 32 132 232 332 132 32 - - - - + 33 133 233 333 - - 233 33 - - + 34 134 234 334 134 34 - - - - + 35 135 235 335 - - - - 335 35 + 36 136 236 336 136 36 236 36 - - + 37 137 237 337 - - - - - - + 38 138 238 338 138 38 - - - - + 39 139 239 339 - - 239 39 - - + 40 140 240 340 140 40 - - 340 40 + 41 141 241 341 - - - - - - + 42 142 242 342 142 42 242 42 - - + 43 143 243 343 - - - - - - + 44 144 244 344 144 44 - - - - + 45 145 245 345 - - 245 45 345 45 + 46 146 246 346 146 46 - - - - + 47 147 247 347 - - - - - - + 48 148 248 348 148 48 248 48 - - + 49 149 249 349 - - - - - - + 50 150 250 350 150 50 - - 350 50 + 51 151 251 351 - - 251 51 - - + 52 152 252 352 152 52 - - - - + 53 153 253 353 - - - - - - + 54 154 254 354 154 54 254 54 - - + 55 155 255 355 - - - - 355 55 + 56 156 256 356 156 56 - - - - + 57 157 257 357 - - 257 57 - - + 58 158 258 358 158 58 - - - - + 59 159 259 359 - - - - - - + 60 160 260 360 160 60 260 60 360 60 + 61 161 261 361 - - - - - - + 62 162 262 362 162 62 - - - - + 63 163 263 363 - - 263 63 - - + 64 164 264 364 164 64 - - - - + 65 165 265 365 - - - - 365 65 + 66 166 266 366 166 66 266 66 - - + 67 167 267 367 - - - - - - + 68 168 268 368 168 68 - - - - + 69 169 269 369 - - 269 69 - - + 70 170 270 370 170 70 - - 370 70 + 71 171 271 371 - - - - - - + 72 172 272 372 172 72 272 72 - - + 73 173 273 373 - - - - - - + 74 174 274 374 174 74 - - - - + 75 175 275 375 - - 275 75 375 75 + 76 176 276 376 176 76 - - - - + 77 177 277 377 - - - - - - + 78 178 278 378 178 78 278 78 - - + 79 179 279 379 - - - - - - + 80 180 280 380 180 80 - - 380 80 + 81 181 281 381 - - 281 81 - - + 82 182 282 382 182 82 - - - - + 83 183 283 383 - - - - - - + 84 184 284 384 184 84 284 84 - - + 85 185 285 385 - - - - 385 85 + 86 186 286 386 186 86 - - - - + 87 187 287 387 - - 287 87 - - + 88 188 288 388 188 88 - - - - + 89 189 289 389 - - - - - - + 90 190 290 390 190 90 290 90 390 90 + 91 191 291 391 - - - - - - + 92 192 292 392 192 92 - - - - + 93 193 293 393 - - 293 93 - - + 94 194 294 394 194 94 - - - - + 95 195 295 395 - - - - 395 95 + 96 - 296 396 - - - - - - + 97 197 - 397 - - - - - - + 98 198 298 - - - - - - - + 99 - - - - - - - - - + - - - - - - 200 0 - - + - - - - - - - - 300 0 +} +do_execsql_test joinD-601 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-602 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-603 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-604 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-605 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON true + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-606 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-607 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-608 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-609 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-610 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-611 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-612 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-613 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-614 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-615 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t2.x>0 + INNER JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-616 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t2.x>0 + INNER JOIN t3 ON t3.y>0 + INNER JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.c IS NOT DISTINCT FROM t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-617 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t2.x>0 + INNER JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + INNER JOIN t4 ON t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.d IS NOT DISTINCT FROM t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-618 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b IN (t2.b,-2,-3) AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-619 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c IN (-4,t3.c,-5) AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-620 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-621 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-622 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-623 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-624 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON true + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-625 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-626 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-627 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-628 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-629 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-630 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-631 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-632 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-633 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-634 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t2.x>0 + INNER JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-635 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t2.x>0 + INNER JOIN t3 ON t3.y>0 + LEFT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.c IS NOT DISTINCT FROM t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-636 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t2.x>0 + INNER JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + LEFT JOIN t4 ON t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.d IS NOT DISTINCT FROM t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-637 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b IN (t2.b,-2,-3) AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-638 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c IN (-4,t3.c,-5) AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-639 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-640 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-641 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-642 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-643 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON true + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-644 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-645 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-646 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-647 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-648 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-649 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-650 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-651 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-652 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-653 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t2.x>0 + INNER JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-654 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t2.x>0 + INNER JOIN t3 ON t3.y>0 + RIGHT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.c IS NOT DISTINCT FROM t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 +} +do_execsql_test joinD-655 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t2.x>0 + INNER JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + RIGHT JOIN t4 ON t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.d IS NOT DISTINCT FROM t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-656 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b IN (t2.b,-2,-3) AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-657 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c IN (-4,t3.c,-5) AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-658 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-659 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-660 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-661 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-662 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON true + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-663 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-664 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-665 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-666 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-667 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-668 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-669 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-670 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-671 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d = t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-672 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t2.x>0 + INNER JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d = t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-673 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t2.x>0 + INNER JOIN t3 ON t3.y>0 + FULL JOIN t4 ON t1.d = t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.c IS NOT DISTINCT FROM t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 +} +do_execsql_test joinD-674 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t2.x>0 + INNER JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + FULL JOIN t4 ON t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.d = t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-675 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b IN (t2.b,-2,-3) AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-676 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c IN (-4,t3.c,-5) AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-677 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-678 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-679 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-680 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-681 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON true + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-682 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-683 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-684 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-685 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-686 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-687 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-688 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-689 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-690 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-691 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t2.x>0 + LEFT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-692 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t2.x>0 + LEFT JOIN t3 ON t3.y>0 + INNER JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.c IS NOT DISTINCT FROM t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-693 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t2.x>0 + LEFT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + INNER JOIN t4 ON t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.d IS NOT DISTINCT FROM t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-694 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b IN (t2.b,-2,-3) AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-695 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c IN (-4,t3.c,-5) AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-696 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - 100 0 - - - - +} +do_execsql_test joinD-697 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - 100 0 - - - - +} +do_execsql_test joinD-698 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - +} +do_execsql_test joinD-699 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - +} +do_execsql_test joinD-700 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON true + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - +} +do_execsql_test joinD-701 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-702 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - 100 0 - - - - +} +do_execsql_test joinD-703 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-704 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - 100 0 - - - - +} +do_execsql_test joinD-705 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-706 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-707 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-708 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-709 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - 100 0 - - - - +} +do_execsql_test joinD-710 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t2.x>0 + LEFT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - +} +do_execsql_test joinD-711 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t2.x>0 + LEFT JOIN t3 ON t3.y>0 + LEFT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.c IS NOT DISTINCT FROM t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-712 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t2.x>0 + LEFT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + LEFT JOIN t4 ON t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.d IS NOT DISTINCT FROM t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-713 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b IN (t2.b,-2,-3) AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - 100 0 - - - - +} +do_execsql_test joinD-714 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c IN (-4,t3.c,-5) AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - 100 0 - - - - +} +do_execsql_test joinD-715 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-716 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-717 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-718 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-719 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON true + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-720 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-721 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-722 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-723 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-724 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-725 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-726 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-727 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-728 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-729 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t2.x>0 + LEFT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 +} +do_execsql_test joinD-730 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t2.x>0 + LEFT JOIN t3 ON t3.y>0 + RIGHT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.c IS NOT DISTINCT FROM t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 +} +do_execsql_test joinD-731 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t2.x>0 + LEFT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + RIGHT JOIN t4 ON t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.d IS NOT DISTINCT FROM t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-732 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b IN (t2.b,-2,-3) AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-733 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c IN (-4,t3.c,-5) AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-734 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - 100 0 - - - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-735 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - 100 0 - - - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-736 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - +} +do_execsql_test joinD-737 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-738 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON true + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - +} +do_execsql_test joinD-739 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-740 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - 100 0 - - - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-741 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-742 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - 100 0 - - - - + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-743 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-744 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-745 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-746 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-747 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d = t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - 100 0 - - - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-748 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t2.x>0 + LEFT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d = t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - - - - - 300 0 +} +do_execsql_test joinD-749 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t2.x>0 + LEFT JOIN t3 ON t3.y>0 + FULL JOIN t4 ON t1.d = t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.c IS NOT DISTINCT FROM t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 +} +do_execsql_test joinD-750 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t2.x>0 + LEFT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + FULL JOIN t4 ON t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.d = t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-751 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b IN (t2.b,-2,-3) AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - 100 0 - - - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-752 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c IN (-4,t3.c,-5) AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - 100 0 - - - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-753 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-754 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-755 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-756 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-757 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON true + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-758 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-759 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-760 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-761 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-762 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-763 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-764 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-765 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-766 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-767 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t2.x>0 + RIGHT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-768 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t2.x>0 + RIGHT JOIN t3 ON t3.y>0 + INNER JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.c IS NOT DISTINCT FROM t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-769 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t2.x>0 + RIGHT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + INNER JOIN t4 ON t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.d IS NOT DISTINCT FROM t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-770 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b IN (t2.b,-2,-3) AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-771 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c IN (-4,t3.c,-5) AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-772 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - +} +do_execsql_test joinD-773 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - +} +do_execsql_test joinD-774 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-775 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - +} +do_execsql_test joinD-776 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON true + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-777 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - +} +do_execsql_test joinD-778 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - +} +do_execsql_test joinD-779 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-780 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - +} +do_execsql_test joinD-781 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-782 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-783 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-784 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-785 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - +} +do_execsql_test joinD-786 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t2.x>0 + RIGHT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - 200 0 - - +} +do_execsql_test joinD-787 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t2.x>0 + RIGHT JOIN t3 ON t3.y>0 + LEFT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.c IS NOT DISTINCT FROM t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-788 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t2.x>0 + RIGHT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + LEFT JOIN t4 ON t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.d IS NOT DISTINCT FROM t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-789 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b IN (t2.b,-2,-3) AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - +} +do_execsql_test joinD-790 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c IN (-4,t3.c,-5) AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - +} +do_execsql_test joinD-791 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-792 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-793 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-794 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-795 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON true + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-796 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-797 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-798 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-799 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-800 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-801 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-802 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-803 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-804 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-805 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t2.x>0 + RIGHT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-806 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t2.x>0 + RIGHT JOIN t3 ON t3.y>0 + RIGHT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.c IS NOT DISTINCT FROM t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 +} +do_execsql_test joinD-807 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t2.x>0 + RIGHT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + RIGHT JOIN t4 ON t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.d IS NOT DISTINCT FROM t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-808 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b IN (t2.b,-2,-3) AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-809 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c IN (-4,t3.c,-5) AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-810 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-811 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-812 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-813 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-814 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON true + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-815 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - +} +do_execsql_test joinD-816 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-817 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-818 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-819 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-820 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-821 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-822 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-823 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d = t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-824 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t2.x>0 + RIGHT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d = t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - 200 0 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-825 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t2.x>0 + RIGHT JOIN t3 ON t3.y>0 + FULL JOIN t4 ON t1.d = t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.c IS NOT DISTINCT FROM t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 +} +do_execsql_test joinD-826 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t2.x>0 + RIGHT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + FULL JOIN t4 ON t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.d = t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-827 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b IN (t2.b,-2,-3) AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-828 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c IN (-4,t3.c,-5) AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 315 15 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 345 45 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 375 75 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-829 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-830 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-831 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-832 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-833 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON true + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-834 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-835 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-836 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-837 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-838 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-839 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-840 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-841 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-842 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c = t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-843 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t2.x>0 + FULL JOIN t3 ON t1.c = t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-844 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t2.x>0 + FULL JOIN t3 ON t3.y>0 + INNER JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.c = t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-845 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t2.x>0 + FULL JOIN t3 ON t1.c = t3.c AND t3.y>0 + INNER JOIN t4 ON t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.d IS NOT DISTINCT FROM t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-846 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b IN (t2.b,-2,-3) AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-847 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - 100 0 - - - - + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - +} +do_execsql_test joinD-848 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - 100 0 - - - - + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - +} +do_execsql_test joinD-849 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - +} +do_execsql_test joinD-850 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - +} +do_execsql_test joinD-851 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON true + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - +} +do_execsql_test joinD-852 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - +} +do_execsql_test joinD-853 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - 100 0 - - - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - +} +do_execsql_test joinD-854 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-855 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - 100 0 - - - - + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - +} +do_execsql_test joinD-856 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-857 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-858 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-859 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-860 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c = t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - 100 0 - - - - + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - +} +do_execsql_test joinD-861 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t2.x>0 + FULL JOIN t3 ON t1.c = t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - - - 200 0 - - +} +do_execsql_test joinD-862 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t2.x>0 + FULL JOIN t3 ON t3.y>0 + LEFT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.c = t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-863 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t2.x>0 + FULL JOIN t3 ON t1.c = t3.c AND t3.y>0 + LEFT JOIN t4 ON t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.d IS NOT DISTINCT FROM t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-864 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b IN (t2.b,-2,-3) AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - 100 0 - - - - + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - +} +do_execsql_test joinD-865 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-866 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-867 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-868 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-869 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON true + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-870 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-871 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-872 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-873 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-874 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-875 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-876 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-877 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-878 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c = t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-879 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t2.x>0 + FULL JOIN t3 ON t1.c = t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 +} +do_execsql_test joinD-880 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t2.x>0 + FULL JOIN t3 ON t3.y>0 + RIGHT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.c = t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-881 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t2.x>0 + FULL JOIN t3 ON t1.c = t3.c AND t3.y>0 + RIGHT JOIN t4 ON t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.d IS NOT DISTINCT FROM t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-882 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b IN (t2.b,-2,-3) AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-883 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - 100 0 - - - - + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-884 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - 100 0 - - - - + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-885 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - +} +do_execsql_test joinD-886 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-887 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON true + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - +} +do_execsql_test joinD-888 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - +} +do_execsql_test joinD-889 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - 100 0 - - - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-890 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-891 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - 100 0 - - - - + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-892 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-893 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-894 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-895 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-896 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c = t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d = t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - 100 0 - - - - + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-897 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t2.x>0 + FULL JOIN t3 ON t1.c = t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d = t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - - - 200 0 - - + - - - - - - - - 300 0 +} +do_execsql_test joinD-898 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t2.x>0 + FULL JOIN t3 ON t3.y>0 + FULL JOIN t4 ON t1.d = t4.d AND t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.c = t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-899 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t2.x>0 + FULL JOIN t3 ON t1.c = t3.c AND t3.y>0 + FULL JOIN t4 ON t4.z>0 + WHERE t1.b IS NOT DISTINCT FROM t2.b AND t1.d = t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-900 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 RIGHT JOIN t2 ON t1.b IN (t2.b,-2,-3) AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - + - - - - 100 0 - - - - + - - - - - - 200 0 - - + - - - - - - 203 3 - - + - - - - - - 209 9 - - + - - - - - - 215 15 - - + - - - - - - 221 21 - - + - - - - - - 227 27 - - + - - - - - - 233 33 - - + - - - - - - 239 39 - - + - - - - - - 245 45 - - + - - - - - - 251 51 - - + - - - - - - 257 57 - - + - - - - - - 263 63 - - + - - - - - - 269 69 - - + - - - - - - 275 75 - - + - - - - - - 281 81 - - + - - - - - - 287 87 - - + - - - - - - 293 93 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 315 15 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 345 45 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 375 75 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-901 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-902 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-903 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-904 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON true + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-905 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-906 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-907 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-908 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-909 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-910 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-911 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-912 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-913 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b = t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-914 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t2.x>0 + INNER JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b = t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-915 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t2.x>0 + INNER JOIN t3 ON t3.y>0 + INNER JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b = t2.b AND t1.c IS NOT DISTINCT FROM t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-916 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t2.x>0 + INNER JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + INNER JOIN t4 ON t4.z>0 + WHERE t1.b = t2.b AND t1.d IS NOT DISTINCT FROM t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-917 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c IN (-4,t3.c,-5) AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-918 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - +} +do_execsql_test joinD-919 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-920 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - +} +do_execsql_test joinD-921 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON true + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-922 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - +} +do_execsql_test joinD-923 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - +} +do_execsql_test joinD-924 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-925 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - +} +do_execsql_test joinD-926 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-927 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-928 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-929 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-930 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b = t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - +} +do_execsql_test joinD-931 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t2.x>0 + INNER JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b = t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-932 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t2.x>0 + INNER JOIN t3 ON t3.y>0 + LEFT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b = t2.b AND t1.c IS NOT DISTINCT FROM t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-933 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t2.x>0 + INNER JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + LEFT JOIN t4 ON t4.z>0 + WHERE t1.b = t2.b AND t1.d IS NOT DISTINCT FROM t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-934 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c IN (-4,t3.c,-5) AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - +} +do_execsql_test joinD-935 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-936 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-937 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-938 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON true + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-939 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-940 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-941 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-942 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-943 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-944 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-945 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-946 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-947 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b = t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-948 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t2.x>0 + INNER JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b = t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-949 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t2.x>0 + INNER JOIN t3 ON t3.y>0 + RIGHT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b = t2.b AND t1.c IS NOT DISTINCT FROM t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-950 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t2.x>0 + INNER JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + RIGHT JOIN t4 ON t4.z>0 + WHERE t1.b = t2.b AND t1.d IS NOT DISTINCT FROM t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-951 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c IN (-4,t3.c,-5) AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-952 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-953 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-954 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-955 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON true + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-956 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - +} +do_execsql_test joinD-957 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-958 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-959 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-960 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-961 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-962 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-963 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + INNER JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-964 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b = t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d = t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-965 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t2.x>0 + INNER JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d = t4.d AND t4.z>0 + WHERE t1.b = t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-966 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t2.x>0 + INNER JOIN t3 ON t3.y>0 + FULL JOIN t4 ON t1.d = t4.d AND t4.z>0 + WHERE t1.b = t2.b AND t1.c IS NOT DISTINCT FROM t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-967 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t2.x>0 + INNER JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + FULL JOIN t4 ON t4.z>0 + WHERE t1.b = t2.b AND t1.d = t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-968 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + INNER JOIN t3 ON t1.c IN (-4,t3.c,-5) AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-969 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 +} +do_execsql_test joinD-970 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-971 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 +} +do_execsql_test joinD-972 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON true + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-973 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-974 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 +} +do_execsql_test joinD-975 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 +} +do_execsql_test joinD-976 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 +} +do_execsql_test joinD-977 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-978 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-979 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-980 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-981 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b = t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 +} +do_execsql_test joinD-982 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t2.x>0 + LEFT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b = t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-983 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t2.x>0 + LEFT JOIN t3 ON t3.y>0 + INNER JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b = t2.b AND t1.c IS NOT DISTINCT FROM t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-984 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t2.x>0 + LEFT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + INNER JOIN t4 ON t4.z>0 + WHERE t1.b = t2.b AND t1.d IS NOT DISTINCT FROM t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-985 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c IN (-4,t3.c,-5) AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 +} +do_execsql_test joinD-986 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 0 100 200 300 - - - - - - + 1 101 201 301 - - - - - - + 2 102 202 302 102 2 - - - - + 3 103 203 303 - - 203 3 - - + 4 104 204 304 104 4 - - - - + 5 105 205 305 - - - - 305 5 + 6 106 206 306 106 6 206 6 - - + 7 107 207 307 - - - - - - + 8 108 208 308 108 8 - - - - + 9 109 209 309 - - 209 9 - - + 10 110 210 310 110 10 - - 310 10 + 11 111 211 311 - - - - - - + 12 112 212 312 112 12 212 12 - - + 13 113 213 313 - - - - - - + 14 114 214 314 114 14 - - - - + 15 115 215 315 - - 215 15 315 15 + 16 116 216 316 116 16 - - - - + 17 117 217 317 - - - - - - + 18 118 218 318 118 18 218 18 - - + 19 119 219 319 - - - - - - + 20 120 220 320 120 20 - - 320 20 + 21 121 221 321 - - 221 21 - - + 22 122 222 322 122 22 - - - - + 23 123 223 323 - - - - - - + 24 124 224 324 124 24 224 24 - - + 25 125 225 325 - - - - 325 25 + 26 126 226 326 126 26 - - - - + 27 127 227 327 - - 227 27 - - + 28 128 228 328 128 28 - - - - + 29 129 229 329 - - - - - - + 30 130 230 330 130 30 230 30 330 30 + 31 131 231 331 - - - - - - + 32 132 232 332 132 32 - - - - + 33 133 233 333 - - 233 33 - - + 34 134 234 334 134 34 - - - - + 35 135 235 335 - - - - 335 35 + 36 136 236 336 136 36 236 36 - - + 37 137 237 337 - - - - - - + 38 138 238 338 138 38 - - - - + 39 139 239 339 - - 239 39 - - + 40 140 240 340 140 40 - - 340 40 + 41 141 241 341 - - - - - - + 42 142 242 342 142 42 242 42 - - + 43 143 243 343 - - - - - - + 44 144 244 344 144 44 - - - - + 45 145 245 345 - - 245 45 345 45 + 46 146 246 346 146 46 - - - - + 47 147 247 347 - - - - - - + 48 148 248 348 148 48 248 48 - - + 49 149 249 349 - - - - - - + 50 150 250 350 150 50 - - 350 50 + 51 151 251 351 - - 251 51 - - + 52 152 252 352 152 52 - - - - + 53 153 253 353 - - - - - - + 54 154 254 354 154 54 254 54 - - + 55 155 255 355 - - - - 355 55 + 56 156 256 356 156 56 - - - - + 57 157 257 357 - - 257 57 - - + 58 158 258 358 158 58 - - - - + 59 159 259 359 - - - - - - + 60 160 260 360 160 60 260 60 360 60 + 61 161 261 361 - - - - - - + 62 162 262 362 162 62 - - - - + 63 163 263 363 - - 263 63 - - + 64 164 264 364 164 64 - - - - + 65 165 265 365 - - - - 365 65 + 66 166 266 366 166 66 266 66 - - + 67 167 267 367 - - - - - - + 68 168 268 368 168 68 - - - - + 69 169 269 369 - - 269 69 - - + 70 170 270 370 170 70 - - 370 70 + 71 171 271 371 - - - - - - + 72 172 272 372 172 72 272 72 - - + 73 173 273 373 - - - - - - + 74 174 274 374 174 74 - - - - + 75 175 275 375 - - 275 75 375 75 + 76 176 276 376 176 76 - - - - + 77 177 277 377 - - - - - - + 78 178 278 378 178 78 278 78 - - + 79 179 279 379 - - - - - - + 80 180 280 380 180 80 - - 380 80 + 81 181 281 381 - - 281 81 - - + 82 182 282 382 182 82 - - - - + 83 183 283 383 - - - - - - + 84 184 284 384 184 84 284 84 - - + 85 185 285 385 - - - - 385 85 + 86 186 286 386 186 86 - - - - + 87 187 287 387 - - 287 87 - - + 88 188 288 388 188 88 - - - - + 89 189 289 389 - - - - - - + 90 190 290 390 190 90 290 90 390 90 + 91 191 291 391 - - - - - - + 92 192 292 392 192 92 - - - - + 93 193 293 393 - - 293 93 - - + 94 194 294 394 194 94 - - - - + 95 195 295 395 - - - - 395 95 + 96 - 296 396 - - - - - - + 97 197 - 397 - - - - - - + 98 198 298 - - - - - - - + 99 - - - - - - - - - + - - - - 100 0 - - - - +} +do_execsql_test joinD-987 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - +} +do_execsql_test joinD-988 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 1 101 201 301 - - - - - - + 2 102 202 302 102 2 - - - - + 3 103 203 303 - - 203 3 - - + 4 104 204 304 104 4 - - - - + 5 105 205 305 - - - - 305 5 + 6 106 206 306 106 6 206 6 - - + 7 107 207 307 - - - - - - + 8 108 208 308 108 8 - - - - + 9 109 209 309 - - 209 9 - - + 10 110 210 310 110 10 - - 310 10 + 11 111 211 311 - - - - - - + 12 112 212 312 112 12 212 12 - - + 13 113 213 313 - - - - - - + 14 114 214 314 114 14 - - - - + 15 115 215 315 - - 215 15 315 15 + 16 116 216 316 116 16 - - - - + 17 117 217 317 - - - - - - + 18 118 218 318 118 18 218 18 - - + 19 119 219 319 - - - - - - + 20 120 220 320 120 20 - - 320 20 + 21 121 221 321 - - 221 21 - - + 22 122 222 322 122 22 - - - - + 23 123 223 323 - - - - - - + 24 124 224 324 124 24 224 24 - - + 25 125 225 325 - - - - 325 25 + 26 126 226 326 126 26 - - - - + 27 127 227 327 - - 227 27 - - + 28 128 228 328 128 28 - - - - + 29 129 229 329 - - - - - - + 30 130 230 330 130 30 230 30 330 30 + 31 131 231 331 - - - - - - + 32 132 232 332 132 32 - - - - + 33 133 233 333 - - 233 33 - - + 34 134 234 334 134 34 - - - - + 35 135 235 335 - - - - 335 35 + 36 136 236 336 136 36 236 36 - - + 37 137 237 337 - - - - - - + 38 138 238 338 138 38 - - - - + 39 139 239 339 - - 239 39 - - + 40 140 240 340 140 40 - - 340 40 + 41 141 241 341 - - - - - - + 42 142 242 342 142 42 242 42 - - + 43 143 243 343 - - - - - - + 44 144 244 344 144 44 - - - - + 45 145 245 345 - - 245 45 345 45 + 46 146 246 346 146 46 - - - - + 47 147 247 347 - - - - - - + 48 148 248 348 148 48 248 48 - - + 49 149 249 349 - - - - - - + 50 150 250 350 150 50 - - 350 50 + 51 151 251 351 - - 251 51 - - + 52 152 252 352 152 52 - - - - + 53 153 253 353 - - - - - - + 54 154 254 354 154 54 254 54 - - + 55 155 255 355 - - - - 355 55 + 56 156 256 356 156 56 - - - - + 57 157 257 357 - - 257 57 - - + 58 158 258 358 158 58 - - - - + 59 159 259 359 - - - - - - + 60 160 260 360 160 60 260 60 360 60 + 61 161 261 361 - - - - - - + 62 162 262 362 162 62 - - - - + 63 163 263 363 - - 263 63 - - + 64 164 264 364 164 64 - - - - + 65 165 265 365 - - - - 365 65 + 66 166 266 366 166 66 266 66 - - + 67 167 267 367 - - - - - - + 68 168 268 368 168 68 - - - - + 69 169 269 369 - - 269 69 - - + 70 170 270 370 170 70 - - 370 70 + 71 171 271 371 - - - - - - + 72 172 272 372 172 72 272 72 - - + 73 173 273 373 - - - - - - + 74 174 274 374 174 74 - - - - + 75 175 275 375 - - 275 75 375 75 + 76 176 276 376 176 76 - - - - + 77 177 277 377 - - - - - - + 78 178 278 378 178 78 278 78 - - + 79 179 279 379 - - - - - - + 80 180 280 380 180 80 - - 380 80 + 81 181 281 381 - - 281 81 - - + 82 182 282 382 182 82 - - - - + 83 183 283 383 - - - - - - + 84 184 284 384 184 84 284 84 - - + 85 185 285 385 - - - - 385 85 + 86 186 286 386 186 86 - - - - + 87 187 287 387 - - 287 87 - - + 88 188 288 388 188 88 - - - - + 89 189 289 389 - - - - - - + 90 190 290 390 190 90 290 90 390 90 + 91 191 291 391 - - - - - - + 92 192 292 392 192 92 - - - - + 93 193 293 393 - - 293 93 - - + 94 194 294 394 194 94 - - - - + 95 195 295 395 - - - - 395 95 + 96 - 296 396 - - - - - - + 97 197 - 397 - - - - - - + 98 198 298 - - - - - - - + 99 - - - - - - - - - +} +do_execsql_test joinD-989 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON true + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - +} +do_execsql_test joinD-990 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - +} +do_execsql_test joinD-991 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 1 101 201 301 - - - - - - + 2 102 202 302 102 2 - - - - + 3 103 203 303 - - 203 3 - - + 4 104 204 304 104 4 - - - - + 5 105 205 305 - - - - 305 5 + 6 106 206 306 106 6 206 6 - - + 7 107 207 307 - - - - - - + 8 108 208 308 108 8 - - - - + 9 109 209 309 - - 209 9 - - + 10 110 210 310 110 10 - - 310 10 + 11 111 211 311 - - - - - - + 12 112 212 312 112 12 212 12 - - + 13 113 213 313 - - - - - - + 14 114 214 314 114 14 - - - - + 15 115 215 315 - - 215 15 315 15 + 16 116 216 316 116 16 - - - - + 17 117 217 317 - - - - - - + 18 118 218 318 118 18 218 18 - - + 19 119 219 319 - - - - - - + 20 120 220 320 120 20 - - 320 20 + 21 121 221 321 - - 221 21 - - + 22 122 222 322 122 22 - - - - + 23 123 223 323 - - - - - - + 24 124 224 324 124 24 224 24 - - + 25 125 225 325 - - - - 325 25 + 26 126 226 326 126 26 - - - - + 27 127 227 327 - - 227 27 - - + 28 128 228 328 128 28 - - - - + 29 129 229 329 - - - - - - + 30 130 230 330 130 30 230 30 330 30 + 31 131 231 331 - - - - - - + 32 132 232 332 132 32 - - - - + 33 133 233 333 - - 233 33 - - + 34 134 234 334 134 34 - - - - + 35 135 235 335 - - - - 335 35 + 36 136 236 336 136 36 236 36 - - + 37 137 237 337 - - - - - - + 38 138 238 338 138 38 - - - - + 39 139 239 339 - - 239 39 - - + 40 140 240 340 140 40 - - 340 40 + 41 141 241 341 - - - - - - + 42 142 242 342 142 42 242 42 - - + 43 143 243 343 - - - - - - + 44 144 244 344 144 44 - - - - + 45 145 245 345 - - 245 45 345 45 + 46 146 246 346 146 46 - - - - + 47 147 247 347 - - - - - - + 48 148 248 348 148 48 248 48 - - + 49 149 249 349 - - - - - - + 50 150 250 350 150 50 - - 350 50 + 51 151 251 351 - - 251 51 - - + 52 152 252 352 152 52 - - - - + 53 153 253 353 - - - - - - + 54 154 254 354 154 54 254 54 - - + 55 155 255 355 - - - - 355 55 + 56 156 256 356 156 56 - - - - + 57 157 257 357 - - 257 57 - - + 58 158 258 358 158 58 - - - - + 59 159 259 359 - - - - - - + 60 160 260 360 160 60 260 60 360 60 + 61 161 261 361 - - - - - - + 62 162 262 362 162 62 - - - - + 63 163 263 363 - - 263 63 - - + 64 164 264 364 164 64 - - - - + 65 165 265 365 - - - - 365 65 + 66 166 266 366 166 66 266 66 - - + 67 167 267 367 - - - - - - + 68 168 268 368 168 68 - - - - + 69 169 269 369 - - 269 69 - - + 70 170 270 370 170 70 - - 370 70 + 71 171 271 371 - - - - - - + 72 172 272 372 172 72 272 72 - - + 73 173 273 373 - - - - - - + 74 174 274 374 174 74 - - - - + 75 175 275 375 - - 275 75 375 75 + 76 176 276 376 176 76 - - - - + 77 177 277 377 - - - - - - + 78 178 278 378 178 78 278 78 - - + 79 179 279 379 - - - - - - + 80 180 280 380 180 80 - - 380 80 + 81 181 281 381 - - 281 81 - - + 82 182 282 382 182 82 - - - - + 83 183 283 383 - - - - - - + 84 184 284 384 184 84 284 84 - - + 85 185 285 385 - - - - 385 85 + 86 186 286 386 186 86 - - - - + 87 187 287 387 - - 287 87 - - + 88 188 288 388 188 88 - - - - + 89 189 289 389 - - - - - - + 90 190 290 390 190 90 290 90 390 90 + 91 191 291 391 - - - - - - + 92 192 292 392 192 92 - - - - + 93 193 293 393 - - 293 93 - - + 94 194 294 394 194 94 - - - - + 95 195 295 395 - - - - 395 95 + 96 - 296 396 - - - - - - + 97 197 - 397 - - - - - - + 98 198 298 - - - - - - - + 99 - - - - - - - - - + - - - - 100 0 - - - - +} +do_execsql_test joinD-992 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 +} +do_execsql_test joinD-993 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 1 101 201 301 - - - - - - + 2 102 202 302 102 2 - - - - + 3 103 203 303 - - 203 3 - - + 4 104 204 304 104 4 - - - - + 5 105 205 305 - - - - 305 5 + 6 106 206 306 106 6 206 6 - - + 7 107 207 307 - - - - - - + 8 108 208 308 108 8 - - - - + 9 109 209 309 - - 209 9 - - + 10 110 210 310 110 10 - - 310 10 + 11 111 211 311 - - - - - - + 12 112 212 312 112 12 212 12 - - + 13 113 213 313 - - - - - - + 14 114 214 314 114 14 - - - - + 15 115 215 315 - - 215 15 315 15 + 16 116 216 316 116 16 - - - - + 17 117 217 317 - - - - - - + 18 118 218 318 118 18 218 18 - - + 19 119 219 319 - - - - - - + 20 120 220 320 120 20 - - 320 20 + 21 121 221 321 - - 221 21 - - + 22 122 222 322 122 22 - - - - + 23 123 223 323 - - - - - - + 24 124 224 324 124 24 224 24 - - + 25 125 225 325 - - - - 325 25 + 26 126 226 326 126 26 - - - - + 27 127 227 327 - - 227 27 - - + 28 128 228 328 128 28 - - - - + 29 129 229 329 - - - - - - + 30 130 230 330 130 30 230 30 330 30 + 31 131 231 331 - - - - - - + 32 132 232 332 132 32 - - - - + 33 133 233 333 - - 233 33 - - + 34 134 234 334 134 34 - - - - + 35 135 235 335 - - - - 335 35 + 36 136 236 336 136 36 236 36 - - + 37 137 237 337 - - - - - - + 38 138 238 338 138 38 - - - - + 39 139 239 339 - - 239 39 - - + 40 140 240 340 140 40 - - 340 40 + 41 141 241 341 - - - - - - + 42 142 242 342 142 42 242 42 - - + 43 143 243 343 - - - - - - + 44 144 244 344 144 44 - - - - + 45 145 245 345 - - 245 45 345 45 + 46 146 246 346 146 46 - - - - + 47 147 247 347 - - - - - - + 48 148 248 348 148 48 248 48 - - + 49 149 249 349 - - - - - - + 50 150 250 350 150 50 - - 350 50 + 51 151 251 351 - - 251 51 - - + 52 152 252 352 152 52 - - - - + 53 153 253 353 - - - - - - + 54 154 254 354 154 54 254 54 - - + 55 155 255 355 - - - - 355 55 + 56 156 256 356 156 56 - - - - + 57 157 257 357 - - 257 57 - - + 58 158 258 358 158 58 - - - - + 59 159 259 359 - - - - - - + 60 160 260 360 160 60 260 60 360 60 + 61 161 261 361 - - - - - - + 62 162 262 362 162 62 - - - - + 63 163 263 363 - - 263 63 - - + 64 164 264 364 164 64 - - - - + 65 165 265 365 - - - - 365 65 + 66 166 266 366 166 66 266 66 - - + 67 167 267 367 - - - - - - + 68 168 268 368 168 68 - - - - + 69 169 269 369 - - 269 69 - - + 70 170 270 370 170 70 - - 370 70 + 71 171 271 371 - - - - - - + 72 172 272 372 172 72 272 72 - - + 73 173 273 373 - - - - - - + 74 174 274 374 174 74 - - - - + 75 175 275 375 - - 275 75 375 75 + 76 176 276 376 176 76 - - - - + 77 177 277 377 - - - - - - + 78 178 278 378 178 78 278 78 - - + 79 179 279 379 - - - - - - + 80 180 280 380 180 80 - - 380 80 + 81 181 281 381 - - 281 81 - - + 82 182 282 382 182 82 - - - - + 83 183 283 383 - - - - - - + 84 184 284 384 184 84 284 84 - - + 85 185 285 385 - - - - 385 85 + 86 186 286 386 186 86 - - - - + 87 187 287 387 - - 287 87 - - + 88 188 288 388 188 88 - - - - + 89 189 289 389 - - - - - - + 90 190 290 390 190 90 290 90 390 90 + 91 191 291 391 - - - - - - + 92 192 292 392 192 92 - - - - + 93 193 293 393 - - 293 93 - - + 94 194 294 394 194 94 - - - - + 95 195 295 395 - - - - 395 95 + 96 - 296 396 - - - - - - + 97 197 - 397 - - - - - - + 98 198 298 - - - - - - - + 99 - - - - - - - - - + - - - - 100 0 - - - - +} +do_execsql_test joinD-994 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-995 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-996 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-997 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-998 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b = t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 0 100 200 300 - - - - - - + 1 101 201 301 - - - - - - + 2 102 202 302 102 2 - - - - + 3 103 203 303 - - 203 3 - - + 4 104 204 304 104 4 - - - - + 5 105 205 305 - - - - 305 5 + 6 106 206 306 106 6 206 6 - - + 7 107 207 307 - - - - - - + 8 108 208 308 108 8 - - - - + 9 109 209 309 - - 209 9 - - + 10 110 210 310 110 10 - - 310 10 + 11 111 211 311 - - - - - - + 12 112 212 312 112 12 212 12 - - + 13 113 213 313 - - - - - - + 14 114 214 314 114 14 - - - - + 15 115 215 315 - - 215 15 315 15 + 16 116 216 316 116 16 - - - - + 17 117 217 317 - - - - - - + 18 118 218 318 118 18 218 18 - - + 19 119 219 319 - - - - - - + 20 120 220 320 120 20 - - 320 20 + 21 121 221 321 - - 221 21 - - + 22 122 222 322 122 22 - - - - + 23 123 223 323 - - - - - - + 24 124 224 324 124 24 224 24 - - + 25 125 225 325 - - - - 325 25 + 26 126 226 326 126 26 - - - - + 27 127 227 327 - - 227 27 - - + 28 128 228 328 128 28 - - - - + 29 129 229 329 - - - - - - + 30 130 230 330 130 30 230 30 330 30 + 31 131 231 331 - - - - - - + 32 132 232 332 132 32 - - - - + 33 133 233 333 - - 233 33 - - + 34 134 234 334 134 34 - - - - + 35 135 235 335 - - - - 335 35 + 36 136 236 336 136 36 236 36 - - + 37 137 237 337 - - - - - - + 38 138 238 338 138 38 - - - - + 39 139 239 339 - - 239 39 - - + 40 140 240 340 140 40 - - 340 40 + 41 141 241 341 - - - - - - + 42 142 242 342 142 42 242 42 - - + 43 143 243 343 - - - - - - + 44 144 244 344 144 44 - - - - + 45 145 245 345 - - 245 45 345 45 + 46 146 246 346 146 46 - - - - + 47 147 247 347 - - - - - - + 48 148 248 348 148 48 248 48 - - + 49 149 249 349 - - - - - - + 50 150 250 350 150 50 - - 350 50 + 51 151 251 351 - - 251 51 - - + 52 152 252 352 152 52 - - - - + 53 153 253 353 - - - - - - + 54 154 254 354 154 54 254 54 - - + 55 155 255 355 - - - - 355 55 + 56 156 256 356 156 56 - - - - + 57 157 257 357 - - 257 57 - - + 58 158 258 358 158 58 - - - - + 59 159 259 359 - - - - - - + 60 160 260 360 160 60 260 60 360 60 + 61 161 261 361 - - - - - - + 62 162 262 362 162 62 - - - - + 63 163 263 363 - - 263 63 - - + 64 164 264 364 164 64 - - - - + 65 165 265 365 - - - - 365 65 + 66 166 266 366 166 66 266 66 - - + 67 167 267 367 - - - - - - + 68 168 268 368 168 68 - - - - + 69 169 269 369 - - 269 69 - - + 70 170 270 370 170 70 - - 370 70 + 71 171 271 371 - - - - - - + 72 172 272 372 172 72 272 72 - - + 73 173 273 373 - - - - - - + 74 174 274 374 174 74 - - - - + 75 175 275 375 - - 275 75 375 75 + 76 176 276 376 176 76 - - - - + 77 177 277 377 - - - - - - + 78 178 278 378 178 78 278 78 - - + 79 179 279 379 - - - - - - + 80 180 280 380 180 80 - - 380 80 + 81 181 281 381 - - 281 81 - - + 82 182 282 382 182 82 - - - - + 83 183 283 383 - - - - - - + 84 184 284 384 184 84 284 84 - - + 85 185 285 385 - - - - 385 85 + 86 186 286 386 186 86 - - - - + 87 187 287 387 - - 287 87 - - + 88 188 288 388 188 88 - - - - + 89 189 289 389 - - - - - - + 90 190 290 390 190 90 290 90 390 90 + 91 191 291 391 - - - - - - + 92 192 292 392 192 92 - - - - + 93 193 293 393 - - 293 93 - - + 94 194 294 394 194 94 - - - - + 95 195 295 395 - - - - 395 95 + 96 - 296 396 - - - - - - + 97 197 - 397 - - - - - - + 98 198 298 - - - - - - - + 99 - - - - - - - - - + - - - - 100 0 - - - - +} +do_execsql_test joinD-999 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t2.x>0 + LEFT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b = t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - +} +do_execsql_test joinD-1000 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t2.x>0 + LEFT JOIN t3 ON t3.y>0 + LEFT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b = t2.b AND t1.c IS NOT DISTINCT FROM t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1001 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t2.x>0 + LEFT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + LEFT JOIN t4 ON t4.z>0 + WHERE t1.b = t2.b AND t1.d IS NOT DISTINCT FROM t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1002 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c IN (-4,t3.c,-5) AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 0 100 200 300 - - - - - - + 1 101 201 301 - - - - - - + 2 102 202 302 102 2 - - - - + 3 103 203 303 - - 203 3 - - + 4 104 204 304 104 4 - - - - + 5 105 205 305 - - - - 305 5 + 6 106 206 306 106 6 206 6 - - + 7 107 207 307 - - - - - - + 8 108 208 308 108 8 - - - - + 9 109 209 309 - - 209 9 - - + 10 110 210 310 110 10 - - 310 10 + 11 111 211 311 - - - - - - + 12 112 212 312 112 12 212 12 - - + 13 113 213 313 - - - - - - + 14 114 214 314 114 14 - - - - + 15 115 215 315 - - 215 15 315 15 + 16 116 216 316 116 16 - - - - + 17 117 217 317 - - - - - - + 18 118 218 318 118 18 218 18 - - + 19 119 219 319 - - - - - - + 20 120 220 320 120 20 - - 320 20 + 21 121 221 321 - - 221 21 - - + 22 122 222 322 122 22 - - - - + 23 123 223 323 - - - - - - + 24 124 224 324 124 24 224 24 - - + 25 125 225 325 - - - - 325 25 + 26 126 226 326 126 26 - - - - + 27 127 227 327 - - 227 27 - - + 28 128 228 328 128 28 - - - - + 29 129 229 329 - - - - - - + 30 130 230 330 130 30 230 30 330 30 + 31 131 231 331 - - - - - - + 32 132 232 332 132 32 - - - - + 33 133 233 333 - - 233 33 - - + 34 134 234 334 134 34 - - - - + 35 135 235 335 - - - - 335 35 + 36 136 236 336 136 36 236 36 - - + 37 137 237 337 - - - - - - + 38 138 238 338 138 38 - - - - + 39 139 239 339 - - 239 39 - - + 40 140 240 340 140 40 - - 340 40 + 41 141 241 341 - - - - - - + 42 142 242 342 142 42 242 42 - - + 43 143 243 343 - - - - - - + 44 144 244 344 144 44 - - - - + 45 145 245 345 - - 245 45 345 45 + 46 146 246 346 146 46 - - - - + 47 147 247 347 - - - - - - + 48 148 248 348 148 48 248 48 - - + 49 149 249 349 - - - - - - + 50 150 250 350 150 50 - - 350 50 + 51 151 251 351 - - 251 51 - - + 52 152 252 352 152 52 - - - - + 53 153 253 353 - - - - - - + 54 154 254 354 154 54 254 54 - - + 55 155 255 355 - - - - 355 55 + 56 156 256 356 156 56 - - - - + 57 157 257 357 - - 257 57 - - + 58 158 258 358 158 58 - - - - + 59 159 259 359 - - - - - - + 60 160 260 360 160 60 260 60 360 60 + 61 161 261 361 - - - - - - + 62 162 262 362 162 62 - - - - + 63 163 263 363 - - 263 63 - - + 64 164 264 364 164 64 - - - - + 65 165 265 365 - - - - 365 65 + 66 166 266 366 166 66 266 66 - - + 67 167 267 367 - - - - - - + 68 168 268 368 168 68 - - - - + 69 169 269 369 - - 269 69 - - + 70 170 270 370 170 70 - - 370 70 + 71 171 271 371 - - - - - - + 72 172 272 372 172 72 272 72 - - + 73 173 273 373 - - - - - - + 74 174 274 374 174 74 - - - - + 75 175 275 375 - - 275 75 375 75 + 76 176 276 376 176 76 - - - - + 77 177 277 377 - - - - - - + 78 178 278 378 178 78 278 78 - - + 79 179 279 379 - - - - - - + 80 180 280 380 180 80 - - 380 80 + 81 181 281 381 - - 281 81 - - + 82 182 282 382 182 82 - - - - + 83 183 283 383 - - - - - - + 84 184 284 384 184 84 284 84 - - + 85 185 285 385 - - - - 385 85 + 86 186 286 386 186 86 - - - - + 87 187 287 387 - - 287 87 - - + 88 188 288 388 188 88 - - - - + 89 189 289 389 - - - - - - + 90 190 290 390 190 90 290 90 390 90 + 91 191 291 391 - - - - - - + 92 192 292 392 192 92 - - - - + 93 193 293 393 - - 293 93 - - + 94 194 294 394 194 94 - - - - + 95 195 295 395 - - - - 395 95 + 96 - 296 396 - - - - - - + 97 197 - 397 - - - - - - + 98 198 298 - - - - - - - + 99 - - - - - - - - - + - - - - 100 0 - - - - +} +do_execsql_test joinD-1003 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 + - - - - - - - - 300 0 +} +do_execsql_test joinD-1004 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1005 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 + - - - - - - - - 300 0 +} +do_execsql_test joinD-1006 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON true + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1007 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1008 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 + - - - - - - - - 300 0 +} +do_execsql_test joinD-1009 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 +} +do_execsql_test joinD-1010 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 +} +do_execsql_test joinD-1011 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1012 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1013 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1014 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1015 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b = t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 + - - - - - - - - 300 0 +} +do_execsql_test joinD-1016 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t2.x>0 + LEFT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b = t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1017 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t2.x>0 + LEFT JOIN t3 ON t3.y>0 + RIGHT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b = t2.b AND t1.c IS NOT DISTINCT FROM t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1018 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t2.x>0 + LEFT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + RIGHT JOIN t4 ON t4.z>0 + WHERE t1.b = t2.b AND t1.d IS NOT DISTINCT FROM t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1019 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c IN (-4,t3.c,-5) AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 + - - - - - - - - 300 0 +} +do_execsql_test joinD-1020 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 0 100 200 300 - - - - - - + 1 101 201 301 - - - - - - + 2 102 202 302 102 2 - - - - + 3 103 203 303 - - 203 3 - - + 4 104 204 304 104 4 - - - - + 5 105 205 305 - - - - 305 5 + 6 106 206 306 106 6 206 6 - - + 7 107 207 307 - - - - - - + 8 108 208 308 108 8 - - - - + 9 109 209 309 - - 209 9 - - + 10 110 210 310 110 10 - - 310 10 + 11 111 211 311 - - - - - - + 12 112 212 312 112 12 212 12 - - + 13 113 213 313 - - - - - - + 14 114 214 314 114 14 - - - - + 15 115 215 315 - - 215 15 315 15 + 16 116 216 316 116 16 - - - - + 17 117 217 317 - - - - - - + 18 118 218 318 118 18 218 18 - - + 19 119 219 319 - - - - - - + 20 120 220 320 120 20 - - 320 20 + 21 121 221 321 - - 221 21 - - + 22 122 222 322 122 22 - - - - + 23 123 223 323 - - - - - - + 24 124 224 324 124 24 224 24 - - + 25 125 225 325 - - - - 325 25 + 26 126 226 326 126 26 - - - - + 27 127 227 327 - - 227 27 - - + 28 128 228 328 128 28 - - - - + 29 129 229 329 - - - - - - + 30 130 230 330 130 30 230 30 330 30 + 31 131 231 331 - - - - - - + 32 132 232 332 132 32 - - - - + 33 133 233 333 - - 233 33 - - + 34 134 234 334 134 34 - - - - + 35 135 235 335 - - - - 335 35 + 36 136 236 336 136 36 236 36 - - + 37 137 237 337 - - - - - - + 38 138 238 338 138 38 - - - - + 39 139 239 339 - - 239 39 - - + 40 140 240 340 140 40 - - 340 40 + 41 141 241 341 - - - - - - + 42 142 242 342 142 42 242 42 - - + 43 143 243 343 - - - - - - + 44 144 244 344 144 44 - - - - + 45 145 245 345 - - 245 45 345 45 + 46 146 246 346 146 46 - - - - + 47 147 247 347 - - - - - - + 48 148 248 348 148 48 248 48 - - + 49 149 249 349 - - - - - - + 50 150 250 350 150 50 - - 350 50 + 51 151 251 351 - - 251 51 - - + 52 152 252 352 152 52 - - - - + 53 153 253 353 - - - - - - + 54 154 254 354 154 54 254 54 - - + 55 155 255 355 - - - - 355 55 + 56 156 256 356 156 56 - - - - + 57 157 257 357 - - 257 57 - - + 58 158 258 358 158 58 - - - - + 59 159 259 359 - - - - - - + 60 160 260 360 160 60 260 60 360 60 + 61 161 261 361 - - - - - - + 62 162 262 362 162 62 - - - - + 63 163 263 363 - - 263 63 - - + 64 164 264 364 164 64 - - - - + 65 165 265 365 - - - - 365 65 + 66 166 266 366 166 66 266 66 - - + 67 167 267 367 - - - - - - + 68 168 268 368 168 68 - - - - + 69 169 269 369 - - 269 69 - - + 70 170 270 370 170 70 - - 370 70 + 71 171 271 371 - - - - - - + 72 172 272 372 172 72 272 72 - - + 73 173 273 373 - - - - - - + 74 174 274 374 174 74 - - - - + 75 175 275 375 - - 275 75 375 75 + 76 176 276 376 176 76 - - - - + 77 177 277 377 - - - - - - + 78 178 278 378 178 78 278 78 - - + 79 179 279 379 - - - - - - + 80 180 280 380 180 80 - - 380 80 + 81 181 281 381 - - 281 81 - - + 82 182 282 382 182 82 - - - - + 83 183 283 383 - - - - - - + 84 184 284 384 184 84 284 84 - - + 85 185 285 385 - - - - 385 85 + 86 186 286 386 186 86 - - - - + 87 187 287 387 - - 287 87 - - + 88 188 288 388 188 88 - - - - + 89 189 289 389 - - - - - - + 90 190 290 390 190 90 290 90 390 90 + 91 191 291 391 - - - - - - + 92 192 292 392 192 92 - - - - + 93 193 293 393 - - 293 93 - - + 94 194 294 394 194 94 - - - - + 95 195 295 395 - - - - 395 95 + 96 - 296 396 - - - - - - + 97 197 - 397 - - - - - - + 98 198 298 - - - - - - - + 99 - - - - - - - - - + - - - - 100 0 - - - - + - - - - - - - - 300 0 +} +do_execsql_test joinD-1021 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - +} +do_execsql_test joinD-1022 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 1 101 201 301 - - - - - - + 2 102 202 302 102 2 - - - - + 3 103 203 303 - - 203 3 - - + 4 104 204 304 104 4 - - - - + 5 105 205 305 - - - - 305 5 + 6 106 206 306 106 6 206 6 - - + 7 107 207 307 - - - - - - + 8 108 208 308 108 8 - - - - + 9 109 209 309 - - 209 9 - - + 10 110 210 310 110 10 - - 310 10 + 11 111 211 311 - - - - - - + 12 112 212 312 112 12 212 12 - - + 13 113 213 313 - - - - - - + 14 114 214 314 114 14 - - - - + 15 115 215 315 - - 215 15 315 15 + 16 116 216 316 116 16 - - - - + 17 117 217 317 - - - - - - + 18 118 218 318 118 18 218 18 - - + 19 119 219 319 - - - - - - + 20 120 220 320 120 20 - - 320 20 + 21 121 221 321 - - 221 21 - - + 22 122 222 322 122 22 - - - - + 23 123 223 323 - - - - - - + 24 124 224 324 124 24 224 24 - - + 25 125 225 325 - - - - 325 25 + 26 126 226 326 126 26 - - - - + 27 127 227 327 - - 227 27 - - + 28 128 228 328 128 28 - - - - + 29 129 229 329 - - - - - - + 30 130 230 330 130 30 230 30 330 30 + 31 131 231 331 - - - - - - + 32 132 232 332 132 32 - - - - + 33 133 233 333 - - 233 33 - - + 34 134 234 334 134 34 - - - - + 35 135 235 335 - - - - 335 35 + 36 136 236 336 136 36 236 36 - - + 37 137 237 337 - - - - - - + 38 138 238 338 138 38 - - - - + 39 139 239 339 - - 239 39 - - + 40 140 240 340 140 40 - - 340 40 + 41 141 241 341 - - - - - - + 42 142 242 342 142 42 242 42 - - + 43 143 243 343 - - - - - - + 44 144 244 344 144 44 - - - - + 45 145 245 345 - - 245 45 345 45 + 46 146 246 346 146 46 - - - - + 47 147 247 347 - - - - - - + 48 148 248 348 148 48 248 48 - - + 49 149 249 349 - - - - - - + 50 150 250 350 150 50 - - 350 50 + 51 151 251 351 - - 251 51 - - + 52 152 252 352 152 52 - - - - + 53 153 253 353 - - - - - - + 54 154 254 354 154 54 254 54 - - + 55 155 255 355 - - - - 355 55 + 56 156 256 356 156 56 - - - - + 57 157 257 357 - - 257 57 - - + 58 158 258 358 158 58 - - - - + 59 159 259 359 - - - - - - + 60 160 260 360 160 60 260 60 360 60 + 61 161 261 361 - - - - - - + 62 162 262 362 162 62 - - - - + 63 163 263 363 - - 263 63 - - + 64 164 264 364 164 64 - - - - + 65 165 265 365 - - - - 365 65 + 66 166 266 366 166 66 266 66 - - + 67 167 267 367 - - - - - - + 68 168 268 368 168 68 - - - - + 69 169 269 369 - - 269 69 - - + 70 170 270 370 170 70 - - 370 70 + 71 171 271 371 - - - - - - + 72 172 272 372 172 72 272 72 - - + 73 173 273 373 - - - - - - + 74 174 274 374 174 74 - - - - + 75 175 275 375 - - 275 75 375 75 + 76 176 276 376 176 76 - - - - + 77 177 277 377 - - - - - - + 78 178 278 378 178 78 278 78 - - + 79 179 279 379 - - - - - - + 80 180 280 380 180 80 - - 380 80 + 81 181 281 381 - - 281 81 - - + 82 182 282 382 182 82 - - - - + 83 183 283 383 - - - - - - + 84 184 284 384 184 84 284 84 - - + 85 185 285 385 - - - - 385 85 + 86 186 286 386 186 86 - - - - + 87 187 287 387 - - 287 87 - - + 88 188 288 388 188 88 - - - - + 89 189 289 389 - - - - - - + 90 190 290 390 190 90 290 90 390 90 + 91 191 291 391 - - - - - - + 92 192 292 392 192 92 - - - - + 93 193 293 393 - - 293 93 - - + 94 194 294 394 194 94 - - - - + 95 195 295 395 - - - - 395 95 + 96 - 296 396 - - - - - - + 97 197 - 397 - - - - - - + 98 198 298 - - - - - - - + 99 - - - - - - - - - + - - - - - - - - 300 0 +} +do_execsql_test joinD-1023 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON true + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - +} +do_execsql_test joinD-1024 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - +} +do_execsql_test joinD-1025 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 1 101 201 301 - - - - - - + 2 102 202 302 102 2 - - - - + 3 103 203 303 - - 203 3 - - + 4 104 204 304 104 4 - - - - + 5 105 205 305 - - - - 305 5 + 6 106 206 306 106 6 206 6 - - + 7 107 207 307 - - - - - - + 8 108 208 308 108 8 - - - - + 9 109 209 309 - - 209 9 - - + 10 110 210 310 110 10 - - 310 10 + 11 111 211 311 - - - - - - + 12 112 212 312 112 12 212 12 - - + 13 113 213 313 - - - - - - + 14 114 214 314 114 14 - - - - + 15 115 215 315 - - 215 15 315 15 + 16 116 216 316 116 16 - - - - + 17 117 217 317 - - - - - - + 18 118 218 318 118 18 218 18 - - + 19 119 219 319 - - - - - - + 20 120 220 320 120 20 - - 320 20 + 21 121 221 321 - - 221 21 - - + 22 122 222 322 122 22 - - - - + 23 123 223 323 - - - - - - + 24 124 224 324 124 24 224 24 - - + 25 125 225 325 - - - - 325 25 + 26 126 226 326 126 26 - - - - + 27 127 227 327 - - 227 27 - - + 28 128 228 328 128 28 - - - - + 29 129 229 329 - - - - - - + 30 130 230 330 130 30 230 30 330 30 + 31 131 231 331 - - - - - - + 32 132 232 332 132 32 - - - - + 33 133 233 333 - - 233 33 - - + 34 134 234 334 134 34 - - - - + 35 135 235 335 - - - - 335 35 + 36 136 236 336 136 36 236 36 - - + 37 137 237 337 - - - - - - + 38 138 238 338 138 38 - - - - + 39 139 239 339 - - 239 39 - - + 40 140 240 340 140 40 - - 340 40 + 41 141 241 341 - - - - - - + 42 142 242 342 142 42 242 42 - - + 43 143 243 343 - - - - - - + 44 144 244 344 144 44 - - - - + 45 145 245 345 - - 245 45 345 45 + 46 146 246 346 146 46 - - - - + 47 147 247 347 - - - - - - + 48 148 248 348 148 48 248 48 - - + 49 149 249 349 - - - - - - + 50 150 250 350 150 50 - - 350 50 + 51 151 251 351 - - 251 51 - - + 52 152 252 352 152 52 - - - - + 53 153 253 353 - - - - - - + 54 154 254 354 154 54 254 54 - - + 55 155 255 355 - - - - 355 55 + 56 156 256 356 156 56 - - - - + 57 157 257 357 - - 257 57 - - + 58 158 258 358 158 58 - - - - + 59 159 259 359 - - - - - - + 60 160 260 360 160 60 260 60 360 60 + 61 161 261 361 - - - - - - + 62 162 262 362 162 62 - - - - + 63 163 263 363 - - 263 63 - - + 64 164 264 364 164 64 - - - - + 65 165 265 365 - - - - 365 65 + 66 166 266 366 166 66 266 66 - - + 67 167 267 367 - - - - - - + 68 168 268 368 168 68 - - - - + 69 169 269 369 - - 269 69 - - + 70 170 270 370 170 70 - - 370 70 + 71 171 271 371 - - - - - - + 72 172 272 372 172 72 272 72 - - + 73 173 273 373 - - - - - - + 74 174 274 374 174 74 - - - - + 75 175 275 375 - - 275 75 375 75 + 76 176 276 376 176 76 - - - - + 77 177 277 377 - - - - - - + 78 178 278 378 178 78 278 78 - - + 79 179 279 379 - - - - - - + 80 180 280 380 180 80 - - 380 80 + 81 181 281 381 - - 281 81 - - + 82 182 282 382 182 82 - - - - + 83 183 283 383 - - - - - - + 84 184 284 384 184 84 284 84 - - + 85 185 285 385 - - - - 385 85 + 86 186 286 386 186 86 - - - - + 87 187 287 387 - - 287 87 - - + 88 188 288 388 188 88 - - - - + 89 189 289 389 - - - - - - + 90 190 290 390 190 90 290 90 390 90 + 91 191 291 391 - - - - - - + 92 192 292 392 192 92 - - - - + 93 193 293 393 - - 293 93 - - + 94 194 294 394 194 94 - - - - + 95 195 295 395 - - - - 395 95 + 96 - 296 396 - - - - - - + 97 197 - 397 - - - - - - + 98 198 298 - - - - - - - + 99 - - - - - - - - - + - - - - 100 0 - - - - + - - - - - - - - 300 0 +} +do_execsql_test joinD-1026 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 +} +do_execsql_test joinD-1027 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 1 101 201 301 - - - - - - + 2 102 202 302 102 2 - - - - + 3 103 203 303 - - 203 3 - - + 4 104 204 304 104 4 - - - - + 5 105 205 305 - - - - 305 5 + 6 106 206 306 106 6 206 6 - - + 7 107 207 307 - - - - - - + 8 108 208 308 108 8 - - - - + 9 109 209 309 - - 209 9 - - + 10 110 210 310 110 10 - - 310 10 + 11 111 211 311 - - - - - - + 12 112 212 312 112 12 212 12 - - + 13 113 213 313 - - - - - - + 14 114 214 314 114 14 - - - - + 15 115 215 315 - - 215 15 315 15 + 16 116 216 316 116 16 - - - - + 17 117 217 317 - - - - - - + 18 118 218 318 118 18 218 18 - - + 19 119 219 319 - - - - - - + 20 120 220 320 120 20 - - 320 20 + 21 121 221 321 - - 221 21 - - + 22 122 222 322 122 22 - - - - + 23 123 223 323 - - - - - - + 24 124 224 324 124 24 224 24 - - + 25 125 225 325 - - - - 325 25 + 26 126 226 326 126 26 - - - - + 27 127 227 327 - - 227 27 - - + 28 128 228 328 128 28 - - - - + 29 129 229 329 - - - - - - + 30 130 230 330 130 30 230 30 330 30 + 31 131 231 331 - - - - - - + 32 132 232 332 132 32 - - - - + 33 133 233 333 - - 233 33 - - + 34 134 234 334 134 34 - - - - + 35 135 235 335 - - - - 335 35 + 36 136 236 336 136 36 236 36 - - + 37 137 237 337 - - - - - - + 38 138 238 338 138 38 - - - - + 39 139 239 339 - - 239 39 - - + 40 140 240 340 140 40 - - 340 40 + 41 141 241 341 - - - - - - + 42 142 242 342 142 42 242 42 - - + 43 143 243 343 - - - - - - + 44 144 244 344 144 44 - - - - + 45 145 245 345 - - 245 45 345 45 + 46 146 246 346 146 46 - - - - + 47 147 247 347 - - - - - - + 48 148 248 348 148 48 248 48 - - + 49 149 249 349 - - - - - - + 50 150 250 350 150 50 - - 350 50 + 51 151 251 351 - - 251 51 - - + 52 152 252 352 152 52 - - - - + 53 153 253 353 - - - - - - + 54 154 254 354 154 54 254 54 - - + 55 155 255 355 - - - - 355 55 + 56 156 256 356 156 56 - - - - + 57 157 257 357 - - 257 57 - - + 58 158 258 358 158 58 - - - - + 59 159 259 359 - - - - - - + 60 160 260 360 160 60 260 60 360 60 + 61 161 261 361 - - - - - - + 62 162 262 362 162 62 - - - - + 63 163 263 363 - - 263 63 - - + 64 164 264 364 164 64 - - - - + 65 165 265 365 - - - - 365 65 + 66 166 266 366 166 66 266 66 - - + 67 167 267 367 - - - - - - + 68 168 268 368 168 68 - - - - + 69 169 269 369 - - 269 69 - - + 70 170 270 370 170 70 - - 370 70 + 71 171 271 371 - - - - - - + 72 172 272 372 172 72 272 72 - - + 73 173 273 373 - - - - - - + 74 174 274 374 174 74 - - - - + 75 175 275 375 - - 275 75 375 75 + 76 176 276 376 176 76 - - - - + 77 177 277 377 - - - - - - + 78 178 278 378 178 78 278 78 - - + 79 179 279 379 - - - - - - + 80 180 280 380 180 80 - - 380 80 + 81 181 281 381 - - 281 81 - - + 82 182 282 382 182 82 - - - - + 83 183 283 383 - - - - - - + 84 184 284 384 184 84 284 84 - - + 85 185 285 385 - - - - 385 85 + 86 186 286 386 186 86 - - - - + 87 187 287 387 - - 287 87 - - + 88 188 288 388 188 88 - - - - + 89 189 289 389 - - - - - - + 90 190 290 390 190 90 290 90 390 90 + 91 191 291 391 - - - - - - + 92 192 292 392 192 92 - - - - + 93 193 293 393 - - 293 93 - - + 94 194 294 394 194 94 - - - - + 95 195 295 395 - - - - 395 95 + 96 - 296 396 - - - - - - + 97 197 - 397 - - - - - - + 98 198 298 - - - - - - - + 99 - - - - - - - - - + - - - - 100 0 - - - - +} +do_execsql_test joinD-1028 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1029 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1030 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1031 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + LEFT JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1032 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b = t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d = t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 0 100 200 300 - - - - - - + 1 101 201 301 - - - - - - + 2 102 202 302 102 2 - - - - + 3 103 203 303 - - 203 3 - - + 4 104 204 304 104 4 - - - - + 5 105 205 305 - - - - 305 5 + 6 106 206 306 106 6 206 6 - - + 7 107 207 307 - - - - - - + 8 108 208 308 108 8 - - - - + 9 109 209 309 - - 209 9 - - + 10 110 210 310 110 10 - - 310 10 + 11 111 211 311 - - - - - - + 12 112 212 312 112 12 212 12 - - + 13 113 213 313 - - - - - - + 14 114 214 314 114 14 - - - - + 15 115 215 315 - - 215 15 315 15 + 16 116 216 316 116 16 - - - - + 17 117 217 317 - - - - - - + 18 118 218 318 118 18 218 18 - - + 19 119 219 319 - - - - - - + 20 120 220 320 120 20 - - 320 20 + 21 121 221 321 - - 221 21 - - + 22 122 222 322 122 22 - - - - + 23 123 223 323 - - - - - - + 24 124 224 324 124 24 224 24 - - + 25 125 225 325 - - - - 325 25 + 26 126 226 326 126 26 - - - - + 27 127 227 327 - - 227 27 - - + 28 128 228 328 128 28 - - - - + 29 129 229 329 - - - - - - + 30 130 230 330 130 30 230 30 330 30 + 31 131 231 331 - - - - - - + 32 132 232 332 132 32 - - - - + 33 133 233 333 - - 233 33 - - + 34 134 234 334 134 34 - - - - + 35 135 235 335 - - - - 335 35 + 36 136 236 336 136 36 236 36 - - + 37 137 237 337 - - - - - - + 38 138 238 338 138 38 - - - - + 39 139 239 339 - - 239 39 - - + 40 140 240 340 140 40 - - 340 40 + 41 141 241 341 - - - - - - + 42 142 242 342 142 42 242 42 - - + 43 143 243 343 - - - - - - + 44 144 244 344 144 44 - - - - + 45 145 245 345 - - 245 45 345 45 + 46 146 246 346 146 46 - - - - + 47 147 247 347 - - - - - - + 48 148 248 348 148 48 248 48 - - + 49 149 249 349 - - - - - - + 50 150 250 350 150 50 - - 350 50 + 51 151 251 351 - - 251 51 - - + 52 152 252 352 152 52 - - - - + 53 153 253 353 - - - - - - + 54 154 254 354 154 54 254 54 - - + 55 155 255 355 - - - - 355 55 + 56 156 256 356 156 56 - - - - + 57 157 257 357 - - 257 57 - - + 58 158 258 358 158 58 - - - - + 59 159 259 359 - - - - - - + 60 160 260 360 160 60 260 60 360 60 + 61 161 261 361 - - - - - - + 62 162 262 362 162 62 - - - - + 63 163 263 363 - - 263 63 - - + 64 164 264 364 164 64 - - - - + 65 165 265 365 - - - - 365 65 + 66 166 266 366 166 66 266 66 - - + 67 167 267 367 - - - - - - + 68 168 268 368 168 68 - - - - + 69 169 269 369 - - 269 69 - - + 70 170 270 370 170 70 - - 370 70 + 71 171 271 371 - - - - - - + 72 172 272 372 172 72 272 72 - - + 73 173 273 373 - - - - - - + 74 174 274 374 174 74 - - - - + 75 175 275 375 - - 275 75 375 75 + 76 176 276 376 176 76 - - - - + 77 177 277 377 - - - - - - + 78 178 278 378 178 78 278 78 - - + 79 179 279 379 - - - - - - + 80 180 280 380 180 80 - - 380 80 + 81 181 281 381 - - 281 81 - - + 82 182 282 382 182 82 - - - - + 83 183 283 383 - - - - - - + 84 184 284 384 184 84 284 84 - - + 85 185 285 385 - - - - 385 85 + 86 186 286 386 186 86 - - - - + 87 187 287 387 - - 287 87 - - + 88 188 288 388 188 88 - - - - + 89 189 289 389 - - - - - - + 90 190 290 390 190 90 290 90 390 90 + 91 191 291 391 - - - - - - + 92 192 292 392 192 92 - - - - + 93 193 293 393 - - 293 93 - - + 94 194 294 394 194 94 - - - - + 95 195 295 395 - - - - 395 95 + 96 - 296 396 - - - - - - + 97 197 - 397 - - - - - - + 98 198 298 - - - - - - - + 99 - - - - - - - - - + - - - - 100 0 - - - - + - - - - - - - - 300 0 +} +do_execsql_test joinD-1033 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t2.x>0 + LEFT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d = t4.d AND t4.z>0 + WHERE t1.b = t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - +} +do_execsql_test joinD-1034 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t2.x>0 + LEFT JOIN t3 ON t3.y>0 + FULL JOIN t4 ON t1.d = t4.d AND t4.z>0 + WHERE t1.b = t2.b AND t1.c IS NOT DISTINCT FROM t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1035 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t2.x>0 + LEFT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + FULL JOIN t4 ON t4.z>0 + WHERE t1.b = t2.b AND t1.d = t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1036 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + LEFT JOIN t3 ON t1.c IN (-4,t3.c,-5) AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 0 100 200 300 - - - - - - + 1 101 201 301 - - - - - - + 2 102 202 302 102 2 - - - - + 3 103 203 303 - - 203 3 - - + 4 104 204 304 104 4 - - - - + 5 105 205 305 - - - - 305 5 + 6 106 206 306 106 6 206 6 - - + 7 107 207 307 - - - - - - + 8 108 208 308 108 8 - - - - + 9 109 209 309 - - 209 9 - - + 10 110 210 310 110 10 - - 310 10 + 11 111 211 311 - - - - - - + 12 112 212 312 112 12 212 12 - - + 13 113 213 313 - - - - - - + 14 114 214 314 114 14 - - - - + 15 115 215 315 - - 215 15 315 15 + 16 116 216 316 116 16 - - - - + 17 117 217 317 - - - - - - + 18 118 218 318 118 18 218 18 - - + 19 119 219 319 - - - - - - + 20 120 220 320 120 20 - - 320 20 + 21 121 221 321 - - 221 21 - - + 22 122 222 322 122 22 - - - - + 23 123 223 323 - - - - - - + 24 124 224 324 124 24 224 24 - - + 25 125 225 325 - - - - 325 25 + 26 126 226 326 126 26 - - - - + 27 127 227 327 - - 227 27 - - + 28 128 228 328 128 28 - - - - + 29 129 229 329 - - - - - - + 30 130 230 330 130 30 230 30 330 30 + 31 131 231 331 - - - - - - + 32 132 232 332 132 32 - - - - + 33 133 233 333 - - 233 33 - - + 34 134 234 334 134 34 - - - - + 35 135 235 335 - - - - 335 35 + 36 136 236 336 136 36 236 36 - - + 37 137 237 337 - - - - - - + 38 138 238 338 138 38 - - - - + 39 139 239 339 - - 239 39 - - + 40 140 240 340 140 40 - - 340 40 + 41 141 241 341 - - - - - - + 42 142 242 342 142 42 242 42 - - + 43 143 243 343 - - - - - - + 44 144 244 344 144 44 - - - - + 45 145 245 345 - - 245 45 345 45 + 46 146 246 346 146 46 - - - - + 47 147 247 347 - - - - - - + 48 148 248 348 148 48 248 48 - - + 49 149 249 349 - - - - - - + 50 150 250 350 150 50 - - 350 50 + 51 151 251 351 - - 251 51 - - + 52 152 252 352 152 52 - - - - + 53 153 253 353 - - - - - - + 54 154 254 354 154 54 254 54 - - + 55 155 255 355 - - - - 355 55 + 56 156 256 356 156 56 - - - - + 57 157 257 357 - - 257 57 - - + 58 158 258 358 158 58 - - - - + 59 159 259 359 - - - - - - + 60 160 260 360 160 60 260 60 360 60 + 61 161 261 361 - - - - - - + 62 162 262 362 162 62 - - - - + 63 163 263 363 - - 263 63 - - + 64 164 264 364 164 64 - - - - + 65 165 265 365 - - - - 365 65 + 66 166 266 366 166 66 266 66 - - + 67 167 267 367 - - - - - - + 68 168 268 368 168 68 - - - - + 69 169 269 369 - - 269 69 - - + 70 170 270 370 170 70 - - 370 70 + 71 171 271 371 - - - - - - + 72 172 272 372 172 72 272 72 - - + 73 173 273 373 - - - - - - + 74 174 274 374 174 74 - - - - + 75 175 275 375 - - 275 75 375 75 + 76 176 276 376 176 76 - - - - + 77 177 277 377 - - - - - - + 78 178 278 378 178 78 278 78 - - + 79 179 279 379 - - - - - - + 80 180 280 380 180 80 - - 380 80 + 81 181 281 381 - - 281 81 - - + 82 182 282 382 182 82 - - - - + 83 183 283 383 - - - - - - + 84 184 284 384 184 84 284 84 - - + 85 185 285 385 - - - - 385 85 + 86 186 286 386 186 86 - - - - + 87 187 287 387 - - 287 87 - - + 88 188 288 388 188 88 - - - - + 89 189 289 389 - - - - - - + 90 190 290 390 190 90 290 90 390 90 + 91 191 291 391 - - - - - - + 92 192 292 392 192 92 - - - - + 93 193 293 393 - - 293 93 - - + 94 194 294 394 194 94 - - - - + 95 195 295 395 - - - - 395 95 + 96 - 296 396 - - - - - - + 97 197 - 397 - - - - - - + 98 198 298 - - - - - - - + 99 - - - - - - - - - + - - - - 100 0 - - - - + - - - - - - - - 300 0 +} +do_execsql_test joinD-1037 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1038 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1039 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1040 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON true + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1041 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1042 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1043 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1044 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1045 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1046 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1047 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1048 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1049 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b = t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1050 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t2.x>0 + RIGHT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b = t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1051 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t2.x>0 + RIGHT JOIN t3 ON t3.y>0 + INNER JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b = t2.b AND t1.c IS NOT DISTINCT FROM t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1052 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t2.x>0 + RIGHT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + INNER JOIN t4 ON t4.z>0 + WHERE t1.b = t2.b AND t1.d IS NOT DISTINCT FROM t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1053 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c IN (-4,t3.c,-5) AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1054 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - + - - - - - - 200 0 - - +} +do_execsql_test joinD-1055 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1056 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - + - - - - - - 200 0 - - +} +do_execsql_test joinD-1057 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON true + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1058 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - +} +do_execsql_test joinD-1059 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - +} +do_execsql_test joinD-1060 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1061 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - + - - - - - - 200 0 - - +} +do_execsql_test joinD-1062 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1063 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1064 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1065 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1066 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b = t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - + - - - - - - 200 0 - - +} +do_execsql_test joinD-1067 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t2.x>0 + RIGHT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b = t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1068 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t2.x>0 + RIGHT JOIN t3 ON t3.y>0 + LEFT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b = t2.b AND t1.c IS NOT DISTINCT FROM t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1069 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t2.x>0 + RIGHT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + LEFT JOIN t4 ON t4.z>0 + WHERE t1.b = t2.b AND t1.d IS NOT DISTINCT FROM t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1070 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c IN (-4,t3.c,-5) AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - + - - - - - - 200 0 - - +} +do_execsql_test joinD-1071 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-1072 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1073 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-1074 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON true + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1075 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1076 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-1077 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-1078 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-1079 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1080 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1081 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1082 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1083 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b = t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-1084 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t2.x>0 + RIGHT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b = t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1085 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t2.x>0 + RIGHT JOIN t3 ON t3.y>0 + RIGHT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b = t2.b AND t1.c IS NOT DISTINCT FROM t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1086 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t2.x>0 + RIGHT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + RIGHT JOIN t4 ON t4.z>0 + WHERE t1.b = t2.b AND t1.d IS NOT DISTINCT FROM t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1087 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c IN (-4,t3.c,-5) AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-1088 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - + - - - - - - 200 0 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-1089 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1090 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - + - - - - - - 200 0 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-1091 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON true + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1092 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - +} +do_execsql_test joinD-1093 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-1094 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-1095 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - + - - - - - - 200 0 - - + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-1096 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1097 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1098 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1099 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + RIGHT JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1100 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b = t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d = t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - + - - - - - - 200 0 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-1101 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t2.x>0 + RIGHT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d = t4.d AND t4.z>0 + WHERE t1.b = t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1102 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t2.x>0 + RIGHT JOIN t3 ON t3.y>0 + FULL JOIN t4 ON t1.d = t4.d AND t4.z>0 + WHERE t1.b = t2.b AND t1.c IS NOT DISTINCT FROM t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1103 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t2.x>0 + RIGHT JOIN t3 ON t1.c IS NOT DISTINCT FROM t3.c AND t3.y>0 + FULL JOIN t4 ON t4.z>0 + WHERE t1.b = t2.b AND t1.d = t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1104 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c IN (-4,t3.c,-5) AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - + - - - - - - 200 0 - - + - - - - - - - - 300 0 + - - - - - - - - 305 5 + - - - - - - - - 310 10 + - - - - - - - - 320 20 + - - - - - - - - 325 25 + - - - - - - - - 335 35 + - - - - - - - - 340 40 + - - - - - - - - 350 50 + - - - - - - - - 355 55 + - - - - - - - - 365 65 + - - - - - - - - 370 70 + - - - - - - - - 380 80 + - - - - - - - - 385 85 + - - - - - - - - 395 95 +} +do_execsql_test joinD-1105 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 +} +do_execsql_test joinD-1106 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1107 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 +} +do_execsql_test joinD-1108 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON true + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1109 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1110 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 +} +do_execsql_test joinD-1111 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 +} +do_execsql_test joinD-1112 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 +} +do_execsql_test joinD-1113 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1114 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1115 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1116 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c + INNER JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1117 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b = t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c = t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 +} +do_execsql_test joinD-1118 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t2.x>0 + FULL JOIN t3 ON t1.c = t3.c AND t3.y>0 + INNER JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b = t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1119 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t2.x>0 + FULL JOIN t3 ON t3.y>0 + INNER JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b = t2.b AND t1.c = t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1120 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t2.x>0 + FULL JOIN t3 ON t1.c = t3.c AND t3.y>0 + INNER JOIN t4 ON t4.z>0 + WHERE t1.b = t2.b AND t1.d IS NOT DISTINCT FROM t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1121 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 0 100 200 300 - - - - - - + 1 101 201 301 - - - - - - + 2 102 202 302 102 2 - - - - + 3 103 203 303 - - 203 3 - - + 4 104 204 304 104 4 - - - - + 5 105 205 305 - - - - 305 5 + 6 106 206 306 106 6 206 6 - - + 7 107 207 307 - - - - - - + 8 108 208 308 108 8 - - - - + 9 109 209 309 - - 209 9 - - + 10 110 210 310 110 10 - - 310 10 + 11 111 211 311 - - - - - - + 12 112 212 312 112 12 212 12 - - + 13 113 213 313 - - - - - - + 14 114 214 314 114 14 - - - - + 15 115 215 315 - - 215 15 315 15 + 16 116 216 316 116 16 - - - - + 17 117 217 317 - - - - - - + 18 118 218 318 118 18 218 18 - - + 19 119 219 319 - - - - - - + 20 120 220 320 120 20 - - 320 20 + 21 121 221 321 - - 221 21 - - + 22 122 222 322 122 22 - - - - + 23 123 223 323 - - - - - - + 24 124 224 324 124 24 224 24 - - + 25 125 225 325 - - - - 325 25 + 26 126 226 326 126 26 - - - - + 27 127 227 327 - - 227 27 - - + 28 128 228 328 128 28 - - - - + 29 129 229 329 - - - - - - + 30 130 230 330 130 30 230 30 330 30 + 31 131 231 331 - - - - - - + 32 132 232 332 132 32 - - - - + 33 133 233 333 - - 233 33 - - + 34 134 234 334 134 34 - - - - + 35 135 235 335 - - - - 335 35 + 36 136 236 336 136 36 236 36 - - + 37 137 237 337 - - - - - - + 38 138 238 338 138 38 - - - - + 39 139 239 339 - - 239 39 - - + 40 140 240 340 140 40 - - 340 40 + 41 141 241 341 - - - - - - + 42 142 242 342 142 42 242 42 - - + 43 143 243 343 - - - - - - + 44 144 244 344 144 44 - - - - + 45 145 245 345 - - 245 45 345 45 + 46 146 246 346 146 46 - - - - + 47 147 247 347 - - - - - - + 48 148 248 348 148 48 248 48 - - + 49 149 249 349 - - - - - - + 50 150 250 350 150 50 - - 350 50 + 51 151 251 351 - - 251 51 - - + 52 152 252 352 152 52 - - - - + 53 153 253 353 - - - - - - + 54 154 254 354 154 54 254 54 - - + 55 155 255 355 - - - - 355 55 + 56 156 256 356 156 56 - - - - + 57 157 257 357 - - 257 57 - - + 58 158 258 358 158 58 - - - - + 59 159 259 359 - - - - - - + 60 160 260 360 160 60 260 60 360 60 + 61 161 261 361 - - - - - - + 62 162 262 362 162 62 - - - - + 63 163 263 363 - - 263 63 - - + 64 164 264 364 164 64 - - - - + 65 165 265 365 - - - - 365 65 + 66 166 266 366 166 66 266 66 - - + 67 167 267 367 - - - - - - + 68 168 268 368 168 68 - - - - + 69 169 269 369 - - 269 69 - - + 70 170 270 370 170 70 - - 370 70 + 71 171 271 371 - - - - - - + 72 172 272 372 172 72 272 72 - - + 73 173 273 373 - - - - - - + 74 174 274 374 174 74 - - - - + 75 175 275 375 - - 275 75 375 75 + 76 176 276 376 176 76 - - - - + 77 177 277 377 - - - - - - + 78 178 278 378 178 78 278 78 - - + 79 179 279 379 - - - - - - + 80 180 280 380 180 80 - - 380 80 + 81 181 281 381 - - 281 81 - - + 82 182 282 382 182 82 - - - - + 83 183 283 383 - - - - - - + 84 184 284 384 184 84 284 84 - - + 85 185 285 385 - - - - 385 85 + 86 186 286 386 186 86 - - - - + 87 187 287 387 - - 287 87 - - + 88 188 288 388 188 88 - - - - + 89 189 289 389 - - - - - - + 90 190 290 390 190 90 290 90 390 90 + 91 191 291 391 - - - - - - + 92 192 292 392 192 92 - - - - + 93 193 293 393 - - 293 93 - - + 94 194 294 394 194 94 - - - - + 95 195 295 395 - - - - 395 95 + 96 - 296 396 - - - - - - + 97 197 - 397 - - - - - - + 98 198 298 - - - - - - - + 99 - - - - - - - - - + - - - - 100 0 - - - - + - - - - - - 200 0 - - +} +do_execsql_test joinD-1122 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - +} +do_execsql_test joinD-1123 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 1 101 201 301 - - - - - - + 2 102 202 302 102 2 - - - - + 3 103 203 303 - - 203 3 - - + 4 104 204 304 104 4 - - - - + 5 105 205 305 - - - - 305 5 + 6 106 206 306 106 6 206 6 - - + 7 107 207 307 - - - - - - + 8 108 208 308 108 8 - - - - + 9 109 209 309 - - 209 9 - - + 10 110 210 310 110 10 - - 310 10 + 11 111 211 311 - - - - - - + 12 112 212 312 112 12 212 12 - - + 13 113 213 313 - - - - - - + 14 114 214 314 114 14 - - - - + 15 115 215 315 - - 215 15 315 15 + 16 116 216 316 116 16 - - - - + 17 117 217 317 - - - - - - + 18 118 218 318 118 18 218 18 - - + 19 119 219 319 - - - - - - + 20 120 220 320 120 20 - - 320 20 + 21 121 221 321 - - 221 21 - - + 22 122 222 322 122 22 - - - - + 23 123 223 323 - - - - - - + 24 124 224 324 124 24 224 24 - - + 25 125 225 325 - - - - 325 25 + 26 126 226 326 126 26 - - - - + 27 127 227 327 - - 227 27 - - + 28 128 228 328 128 28 - - - - + 29 129 229 329 - - - - - - + 30 130 230 330 130 30 230 30 330 30 + 31 131 231 331 - - - - - - + 32 132 232 332 132 32 - - - - + 33 133 233 333 - - 233 33 - - + 34 134 234 334 134 34 - - - - + 35 135 235 335 - - - - 335 35 + 36 136 236 336 136 36 236 36 - - + 37 137 237 337 - - - - - - + 38 138 238 338 138 38 - - - - + 39 139 239 339 - - 239 39 - - + 40 140 240 340 140 40 - - 340 40 + 41 141 241 341 - - - - - - + 42 142 242 342 142 42 242 42 - - + 43 143 243 343 - - - - - - + 44 144 244 344 144 44 - - - - + 45 145 245 345 - - 245 45 345 45 + 46 146 246 346 146 46 - - - - + 47 147 247 347 - - - - - - + 48 148 248 348 148 48 248 48 - - + 49 149 249 349 - - - - - - + 50 150 250 350 150 50 - - 350 50 + 51 151 251 351 - - 251 51 - - + 52 152 252 352 152 52 - - - - + 53 153 253 353 - - - - - - + 54 154 254 354 154 54 254 54 - - + 55 155 255 355 - - - - 355 55 + 56 156 256 356 156 56 - - - - + 57 157 257 357 - - 257 57 - - + 58 158 258 358 158 58 - - - - + 59 159 259 359 - - - - - - + 60 160 260 360 160 60 260 60 360 60 + 61 161 261 361 - - - - - - + 62 162 262 362 162 62 - - - - + 63 163 263 363 - - 263 63 - - + 64 164 264 364 164 64 - - - - + 65 165 265 365 - - - - 365 65 + 66 166 266 366 166 66 266 66 - - + 67 167 267 367 - - - - - - + 68 168 268 368 168 68 - - - - + 69 169 269 369 - - 269 69 - - + 70 170 270 370 170 70 - - 370 70 + 71 171 271 371 - - - - - - + 72 172 272 372 172 72 272 72 - - + 73 173 273 373 - - - - - - + 74 174 274 374 174 74 - - - - + 75 175 275 375 - - 275 75 375 75 + 76 176 276 376 176 76 - - - - + 77 177 277 377 - - - - - - + 78 178 278 378 178 78 278 78 - - + 79 179 279 379 - - - - - - + 80 180 280 380 180 80 - - 380 80 + 81 181 281 381 - - 281 81 - - + 82 182 282 382 182 82 - - - - + 83 183 283 383 - - - - - - + 84 184 284 384 184 84 284 84 - - + 85 185 285 385 - - - - 385 85 + 86 186 286 386 186 86 - - - - + 87 187 287 387 - - 287 87 - - + 88 188 288 388 188 88 - - - - + 89 189 289 389 - - - - - - + 90 190 290 390 190 90 290 90 390 90 + 91 191 291 391 - - - - - - + 92 192 292 392 192 92 - - - - + 93 193 293 393 - - 293 93 - - + 94 194 294 394 194 94 - - - - + 95 195 295 395 - - - - 395 95 + 96 - 296 396 - - - - - - + 97 197 - 397 - - - - - - + 98 198 298 - - - - - - - + 99 - - - - - - - - - + - - - - - - 200 0 - - +} +do_execsql_test joinD-1124 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON true + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - +} +do_execsql_test joinD-1125 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - +} +do_execsql_test joinD-1126 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 1 101 201 301 - - - - - - + 2 102 202 302 102 2 - - - - + 3 103 203 303 - - 203 3 - - + 4 104 204 304 104 4 - - - - + 5 105 205 305 - - - - 305 5 + 6 106 206 306 106 6 206 6 - - + 7 107 207 307 - - - - - - + 8 108 208 308 108 8 - - - - + 9 109 209 309 - - 209 9 - - + 10 110 210 310 110 10 - - 310 10 + 11 111 211 311 - - - - - - + 12 112 212 312 112 12 212 12 - - + 13 113 213 313 - - - - - - + 14 114 214 314 114 14 - - - - + 15 115 215 315 - - 215 15 315 15 + 16 116 216 316 116 16 - - - - + 17 117 217 317 - - - - - - + 18 118 218 318 118 18 218 18 - - + 19 119 219 319 - - - - - - + 20 120 220 320 120 20 - - 320 20 + 21 121 221 321 - - 221 21 - - + 22 122 222 322 122 22 - - - - + 23 123 223 323 - - - - - - + 24 124 224 324 124 24 224 24 - - + 25 125 225 325 - - - - 325 25 + 26 126 226 326 126 26 - - - - + 27 127 227 327 - - 227 27 - - + 28 128 228 328 128 28 - - - - + 29 129 229 329 - - - - - - + 30 130 230 330 130 30 230 30 330 30 + 31 131 231 331 - - - - - - + 32 132 232 332 132 32 - - - - + 33 133 233 333 - - 233 33 - - + 34 134 234 334 134 34 - - - - + 35 135 235 335 - - - - 335 35 + 36 136 236 336 136 36 236 36 - - + 37 137 237 337 - - - - - - + 38 138 238 338 138 38 - - - - + 39 139 239 339 - - 239 39 - - + 40 140 240 340 140 40 - - 340 40 + 41 141 241 341 - - - - - - + 42 142 242 342 142 42 242 42 - - + 43 143 243 343 - - - - - - + 44 144 244 344 144 44 - - - - + 45 145 245 345 - - 245 45 345 45 + 46 146 246 346 146 46 - - - - + 47 147 247 347 - - - - - - + 48 148 248 348 148 48 248 48 - - + 49 149 249 349 - - - - - - + 50 150 250 350 150 50 - - 350 50 + 51 151 251 351 - - 251 51 - - + 52 152 252 352 152 52 - - - - + 53 153 253 353 - - - - - - + 54 154 254 354 154 54 254 54 - - + 55 155 255 355 - - - - 355 55 + 56 156 256 356 156 56 - - - - + 57 157 257 357 - - 257 57 - - + 58 158 258 358 158 58 - - - - + 59 159 259 359 - - - - - - + 60 160 260 360 160 60 260 60 360 60 + 61 161 261 361 - - - - - - + 62 162 262 362 162 62 - - - - + 63 163 263 363 - - 263 63 - - + 64 164 264 364 164 64 - - - - + 65 165 265 365 - - - - 365 65 + 66 166 266 366 166 66 266 66 - - + 67 167 267 367 - - - - - - + 68 168 268 368 168 68 - - - - + 69 169 269 369 - - 269 69 - - + 70 170 270 370 170 70 - - 370 70 + 71 171 271 371 - - - - - - + 72 172 272 372 172 72 272 72 - - + 73 173 273 373 - - - - - - + 74 174 274 374 174 74 - - - - + 75 175 275 375 - - 275 75 375 75 + 76 176 276 376 176 76 - - - - + 77 177 277 377 - - - - - - + 78 178 278 378 178 78 278 78 - - + 79 179 279 379 - - - - - - + 80 180 280 380 180 80 - - 380 80 + 81 181 281 381 - - 281 81 - - + 82 182 282 382 182 82 - - - - + 83 183 283 383 - - - - - - + 84 184 284 384 184 84 284 84 - - + 85 185 285 385 - - - - 385 85 + 86 186 286 386 186 86 - - - - + 87 187 287 387 - - 287 87 - - + 88 188 288 388 188 88 - - - - + 89 189 289 389 - - - - - - + 90 190 290 390 190 90 290 90 390 90 + 91 191 291 391 - - - - - - + 92 192 292 392 192 92 - - - - + 93 193 293 393 - - 293 93 - - + 94 194 294 394 194 94 - - - - + 95 195 295 395 - - - - 395 95 + 96 - 296 396 - - - - - - + 97 197 - 397 - - - - - - + 98 198 298 - - - - - - - + 99 - - - - - - - - - + - - - - 100 0 - - - - +} +do_execsql_test joinD-1127 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 +} +do_execsql_test joinD-1128 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 1 101 201 301 - - - - - - + 2 102 202 302 102 2 - - - - + 3 103 203 303 - - 203 3 - - + 4 104 204 304 104 4 - - - - + 5 105 205 305 - - - - 305 5 + 6 106 206 306 106 6 206 6 - - + 7 107 207 307 - - - - - - + 8 108 208 308 108 8 - - - - + 9 109 209 309 - - 209 9 - - + 10 110 210 310 110 10 - - 310 10 + 11 111 211 311 - - - - - - + 12 112 212 312 112 12 212 12 - - + 13 113 213 313 - - - - - - + 14 114 214 314 114 14 - - - - + 15 115 215 315 - - 215 15 315 15 + 16 116 216 316 116 16 - - - - + 17 117 217 317 - - - - - - + 18 118 218 318 118 18 218 18 - - + 19 119 219 319 - - - - - - + 20 120 220 320 120 20 - - 320 20 + 21 121 221 321 - - 221 21 - - + 22 122 222 322 122 22 - - - - + 23 123 223 323 - - - - - - + 24 124 224 324 124 24 224 24 - - + 25 125 225 325 - - - - 325 25 + 26 126 226 326 126 26 - - - - + 27 127 227 327 - - 227 27 - - + 28 128 228 328 128 28 - - - - + 29 129 229 329 - - - - - - + 30 130 230 330 130 30 230 30 330 30 + 31 131 231 331 - - - - - - + 32 132 232 332 132 32 - - - - + 33 133 233 333 - - 233 33 - - + 34 134 234 334 134 34 - - - - + 35 135 235 335 - - - - 335 35 + 36 136 236 336 136 36 236 36 - - + 37 137 237 337 - - - - - - + 38 138 238 338 138 38 - - - - + 39 139 239 339 - - 239 39 - - + 40 140 240 340 140 40 - - 340 40 + 41 141 241 341 - - - - - - + 42 142 242 342 142 42 242 42 - - + 43 143 243 343 - - - - - - + 44 144 244 344 144 44 - - - - + 45 145 245 345 - - 245 45 345 45 + 46 146 246 346 146 46 - - - - + 47 147 247 347 - - - - - - + 48 148 248 348 148 48 248 48 - - + 49 149 249 349 - - - - - - + 50 150 250 350 150 50 - - 350 50 + 51 151 251 351 - - 251 51 - - + 52 152 252 352 152 52 - - - - + 53 153 253 353 - - - - - - + 54 154 254 354 154 54 254 54 - - + 55 155 255 355 - - - - 355 55 + 56 156 256 356 156 56 - - - - + 57 157 257 357 - - 257 57 - - + 58 158 258 358 158 58 - - - - + 59 159 259 359 - - - - - - + 60 160 260 360 160 60 260 60 360 60 + 61 161 261 361 - - - - - - + 62 162 262 362 162 62 - - - - + 63 163 263 363 - - 263 63 - - + 64 164 264 364 164 64 - - - - + 65 165 265 365 - - - - 365 65 + 66 166 266 366 166 66 266 66 - - + 67 167 267 367 - - - - - - + 68 168 268 368 168 68 - - - - + 69 169 269 369 - - 269 69 - - + 70 170 270 370 170 70 - - 370 70 + 71 171 271 371 - - - - - - + 72 172 272 372 172 72 272 72 - - + 73 173 273 373 - - - - - - + 74 174 274 374 174 74 - - - - + 75 175 275 375 - - 275 75 375 75 + 76 176 276 376 176 76 - - - - + 77 177 277 377 - - - - - - + 78 178 278 378 178 78 278 78 - - + 79 179 279 379 - - - - - - + 80 180 280 380 180 80 - - 380 80 + 81 181 281 381 - - 281 81 - - + 82 182 282 382 182 82 - - - - + 83 183 283 383 - - - - - - + 84 184 284 384 184 84 284 84 - - + 85 185 285 385 - - - - 385 85 + 86 186 286 386 186 86 - - - - + 87 187 287 387 - - 287 87 - - + 88 188 288 388 188 88 - - - - + 89 189 289 389 - - - - - - + 90 190 290 390 190 90 290 90 390 90 + 91 191 291 391 - - - - - - + 92 192 292 392 192 92 - - - - + 93 193 293 393 - - 293 93 - - + 94 194 294 394 194 94 - - - - + 95 195 295 395 - - - - 395 95 + 96 - 296 396 - - - - - - + 97 197 - 397 - - - - - - + 98 198 298 - - - - - - - + 99 - - - - - - - - - + - - - - 100 0 - - - - + - - - - - - 200 0 - - +} +do_execsql_test joinD-1129 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1130 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1131 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1132 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c + LEFT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1133 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b = t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c = t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 0 100 200 300 - - - - - - + 1 101 201 301 - - - - - - + 2 102 202 302 102 2 - - - - + 3 103 203 303 - - 203 3 - - + 4 104 204 304 104 4 - - - - + 5 105 205 305 - - - - 305 5 + 6 106 206 306 106 6 206 6 - - + 7 107 207 307 - - - - - - + 8 108 208 308 108 8 - - - - + 9 109 209 309 - - 209 9 - - + 10 110 210 310 110 10 - - 310 10 + 11 111 211 311 - - - - - - + 12 112 212 312 112 12 212 12 - - + 13 113 213 313 - - - - - - + 14 114 214 314 114 14 - - - - + 15 115 215 315 - - 215 15 315 15 + 16 116 216 316 116 16 - - - - + 17 117 217 317 - - - - - - + 18 118 218 318 118 18 218 18 - - + 19 119 219 319 - - - - - - + 20 120 220 320 120 20 - - 320 20 + 21 121 221 321 - - 221 21 - - + 22 122 222 322 122 22 - - - - + 23 123 223 323 - - - - - - + 24 124 224 324 124 24 224 24 - - + 25 125 225 325 - - - - 325 25 + 26 126 226 326 126 26 - - - - + 27 127 227 327 - - 227 27 - - + 28 128 228 328 128 28 - - - - + 29 129 229 329 - - - - - - + 30 130 230 330 130 30 230 30 330 30 + 31 131 231 331 - - - - - - + 32 132 232 332 132 32 - - - - + 33 133 233 333 - - 233 33 - - + 34 134 234 334 134 34 - - - - + 35 135 235 335 - - - - 335 35 + 36 136 236 336 136 36 236 36 - - + 37 137 237 337 - - - - - - + 38 138 238 338 138 38 - - - - + 39 139 239 339 - - 239 39 - - + 40 140 240 340 140 40 - - 340 40 + 41 141 241 341 - - - - - - + 42 142 242 342 142 42 242 42 - - + 43 143 243 343 - - - - - - + 44 144 244 344 144 44 - - - - + 45 145 245 345 - - 245 45 345 45 + 46 146 246 346 146 46 - - - - + 47 147 247 347 - - - - - - + 48 148 248 348 148 48 248 48 - - + 49 149 249 349 - - - - - - + 50 150 250 350 150 50 - - 350 50 + 51 151 251 351 - - 251 51 - - + 52 152 252 352 152 52 - - - - + 53 153 253 353 - - - - - - + 54 154 254 354 154 54 254 54 - - + 55 155 255 355 - - - - 355 55 + 56 156 256 356 156 56 - - - - + 57 157 257 357 - - 257 57 - - + 58 158 258 358 158 58 - - - - + 59 159 259 359 - - - - - - + 60 160 260 360 160 60 260 60 360 60 + 61 161 261 361 - - - - - - + 62 162 262 362 162 62 - - - - + 63 163 263 363 - - 263 63 - - + 64 164 264 364 164 64 - - - - + 65 165 265 365 - - - - 365 65 + 66 166 266 366 166 66 266 66 - - + 67 167 267 367 - - - - - - + 68 168 268 368 168 68 - - - - + 69 169 269 369 - - 269 69 - - + 70 170 270 370 170 70 - - 370 70 + 71 171 271 371 - - - - - - + 72 172 272 372 172 72 272 72 - - + 73 173 273 373 - - - - - - + 74 174 274 374 174 74 - - - - + 75 175 275 375 - - 275 75 375 75 + 76 176 276 376 176 76 - - - - + 77 177 277 377 - - - - - - + 78 178 278 378 178 78 278 78 - - + 79 179 279 379 - - - - - - + 80 180 280 380 180 80 - - 380 80 + 81 181 281 381 - - 281 81 - - + 82 182 282 382 182 82 - - - - + 83 183 283 383 - - - - - - + 84 184 284 384 184 84 284 84 - - + 85 185 285 385 - - - - 385 85 + 86 186 286 386 186 86 - - - - + 87 187 287 387 - - 287 87 - - + 88 188 288 388 188 88 - - - - + 89 189 289 389 - - - - - - + 90 190 290 390 190 90 290 90 390 90 + 91 191 291 391 - - - - - - + 92 192 292 392 192 92 - - - - + 93 193 293 393 - - 293 93 - - + 94 194 294 394 194 94 - - - - + 95 195 295 395 - - - - 395 95 + 96 - 296 396 - - - - - - + 97 197 - 397 - - - - - - + 98 198 298 - - - - - - - + 99 - - - - - - - - - + - - - - 100 0 - - - - + - - - - - - 200 0 - - +} +do_execsql_test joinD-1134 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t2.x>0 + FULL JOIN t3 ON t1.c = t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b = t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - +} +do_execsql_test joinD-1135 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t2.x>0 + FULL JOIN t3 ON t3.y>0 + LEFT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b = t2.b AND t1.c = t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1136 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t2.x>0 + FULL JOIN t3 ON t1.c = t3.c AND t3.y>0 + LEFT JOIN t4 ON t4.z>0 + WHERE t1.b = t2.b AND t1.d IS NOT DISTINCT FROM t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1137 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 + - - - - - - - - 300 0 +} +do_execsql_test joinD-1138 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1139 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 + - - - - - - - - 300 0 +} +do_execsql_test joinD-1140 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON true + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1141 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1142 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 + - - - - - - - - 300 0 +} +do_execsql_test joinD-1143 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 +} +do_execsql_test joinD-1144 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 +} +do_execsql_test joinD-1145 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1146 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1147 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1148 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c + RIGHT JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1149 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b = t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c = t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 + - - - - - - - - 300 0 +} +do_execsql_test joinD-1150 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t2.x>0 + FULL JOIN t3 ON t1.c = t3.c AND t3.y>0 + RIGHT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b = t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1151 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t2.x>0 + FULL JOIN t3 ON t3.y>0 + RIGHT JOIN t4 ON t1.d IS NOT DISTINCT FROM t4.d AND t4.z>0 + WHERE t1.b = t2.b AND t1.c = t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1152 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t2.x>0 + FULL JOIN t3 ON t1.c = t3.c AND t3.y>0 + RIGHT JOIN t4 ON t4.z>0 + WHERE t1.b = t2.b AND t1.d IS NOT DISTINCT FROM t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1153 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 0 100 200 300 - - - - - - + 1 101 201 301 - - - - - - + 2 102 202 302 102 2 - - - - + 3 103 203 303 - - 203 3 - - + 4 104 204 304 104 4 - - - - + 5 105 205 305 - - - - 305 5 + 6 106 206 306 106 6 206 6 - - + 7 107 207 307 - - - - - - + 8 108 208 308 108 8 - - - - + 9 109 209 309 - - 209 9 - - + 10 110 210 310 110 10 - - 310 10 + 11 111 211 311 - - - - - - + 12 112 212 312 112 12 212 12 - - + 13 113 213 313 - - - - - - + 14 114 214 314 114 14 - - - - + 15 115 215 315 - - 215 15 315 15 + 16 116 216 316 116 16 - - - - + 17 117 217 317 - - - - - - + 18 118 218 318 118 18 218 18 - - + 19 119 219 319 - - - - - - + 20 120 220 320 120 20 - - 320 20 + 21 121 221 321 - - 221 21 - - + 22 122 222 322 122 22 - - - - + 23 123 223 323 - - - - - - + 24 124 224 324 124 24 224 24 - - + 25 125 225 325 - - - - 325 25 + 26 126 226 326 126 26 - - - - + 27 127 227 327 - - 227 27 - - + 28 128 228 328 128 28 - - - - + 29 129 229 329 - - - - - - + 30 130 230 330 130 30 230 30 330 30 + 31 131 231 331 - - - - - - + 32 132 232 332 132 32 - - - - + 33 133 233 333 - - 233 33 - - + 34 134 234 334 134 34 - - - - + 35 135 235 335 - - - - 335 35 + 36 136 236 336 136 36 236 36 - - + 37 137 237 337 - - - - - - + 38 138 238 338 138 38 - - - - + 39 139 239 339 - - 239 39 - - + 40 140 240 340 140 40 - - 340 40 + 41 141 241 341 - - - - - - + 42 142 242 342 142 42 242 42 - - + 43 143 243 343 - - - - - - + 44 144 244 344 144 44 - - - - + 45 145 245 345 - - 245 45 345 45 + 46 146 246 346 146 46 - - - - + 47 147 247 347 - - - - - - + 48 148 248 348 148 48 248 48 - - + 49 149 249 349 - - - - - - + 50 150 250 350 150 50 - - 350 50 + 51 151 251 351 - - 251 51 - - + 52 152 252 352 152 52 - - - - + 53 153 253 353 - - - - - - + 54 154 254 354 154 54 254 54 - - + 55 155 255 355 - - - - 355 55 + 56 156 256 356 156 56 - - - - + 57 157 257 357 - - 257 57 - - + 58 158 258 358 158 58 - - - - + 59 159 259 359 - - - - - - + 60 160 260 360 160 60 260 60 360 60 + 61 161 261 361 - - - - - - + 62 162 262 362 162 62 - - - - + 63 163 263 363 - - 263 63 - - + 64 164 264 364 164 64 - - - - + 65 165 265 365 - - - - 365 65 + 66 166 266 366 166 66 266 66 - - + 67 167 267 367 - - - - - - + 68 168 268 368 168 68 - - - - + 69 169 269 369 - - 269 69 - - + 70 170 270 370 170 70 - - 370 70 + 71 171 271 371 - - - - - - + 72 172 272 372 172 72 272 72 - - + 73 173 273 373 - - - - - - + 74 174 274 374 174 74 - - - - + 75 175 275 375 - - 275 75 375 75 + 76 176 276 376 176 76 - - - - + 77 177 277 377 - - - - - - + 78 178 278 378 178 78 278 78 - - + 79 179 279 379 - - - - - - + 80 180 280 380 180 80 - - 380 80 + 81 181 281 381 - - 281 81 - - + 82 182 282 382 182 82 - - - - + 83 183 283 383 - - - - - - + 84 184 284 384 184 84 284 84 - - + 85 185 285 385 - - - - 385 85 + 86 186 286 386 186 86 - - - - + 87 187 287 387 - - 287 87 - - + 88 188 288 388 188 88 - - - - + 89 189 289 389 - - - - - - + 90 190 290 390 190 90 290 90 390 90 + 91 191 291 391 - - - - - - + 92 192 292 392 192 92 - - - - + 93 193 293 393 - - 293 93 - - + 94 194 294 394 194 94 - - - - + 95 195 295 395 - - - - 395 95 + 96 - 296 396 - - - - - - + 97 197 - 397 - - - - - - + 98 198 298 - - - - - - - + 99 - - - - - - - - - + - - - - 100 0 - - - - + - - - - - - 200 0 - - + - - - - - - - - 300 0 +} +do_execsql_test joinD-1154 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - +} +do_execsql_test joinD-1155 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE (t2.x>0 OR t2.x IS NULL) + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 1 101 201 301 - - - - - - + 2 102 202 302 102 2 - - - - + 3 103 203 303 - - 203 3 - - + 4 104 204 304 104 4 - - - - + 5 105 205 305 - - - - 305 5 + 6 106 206 306 106 6 206 6 - - + 7 107 207 307 - - - - - - + 8 108 208 308 108 8 - - - - + 9 109 209 309 - - 209 9 - - + 10 110 210 310 110 10 - - 310 10 + 11 111 211 311 - - - - - - + 12 112 212 312 112 12 212 12 - - + 13 113 213 313 - - - - - - + 14 114 214 314 114 14 - - - - + 15 115 215 315 - - 215 15 315 15 + 16 116 216 316 116 16 - - - - + 17 117 217 317 - - - - - - + 18 118 218 318 118 18 218 18 - - + 19 119 219 319 - - - - - - + 20 120 220 320 120 20 - - 320 20 + 21 121 221 321 - - 221 21 - - + 22 122 222 322 122 22 - - - - + 23 123 223 323 - - - - - - + 24 124 224 324 124 24 224 24 - - + 25 125 225 325 - - - - 325 25 + 26 126 226 326 126 26 - - - - + 27 127 227 327 - - 227 27 - - + 28 128 228 328 128 28 - - - - + 29 129 229 329 - - - - - - + 30 130 230 330 130 30 230 30 330 30 + 31 131 231 331 - - - - - - + 32 132 232 332 132 32 - - - - + 33 133 233 333 - - 233 33 - - + 34 134 234 334 134 34 - - - - + 35 135 235 335 - - - - 335 35 + 36 136 236 336 136 36 236 36 - - + 37 137 237 337 - - - - - - + 38 138 238 338 138 38 - - - - + 39 139 239 339 - - 239 39 - - + 40 140 240 340 140 40 - - 340 40 + 41 141 241 341 - - - - - - + 42 142 242 342 142 42 242 42 - - + 43 143 243 343 - - - - - - + 44 144 244 344 144 44 - - - - + 45 145 245 345 - - 245 45 345 45 + 46 146 246 346 146 46 - - - - + 47 147 247 347 - - - - - - + 48 148 248 348 148 48 248 48 - - + 49 149 249 349 - - - - - - + 50 150 250 350 150 50 - - 350 50 + 51 151 251 351 - - 251 51 - - + 52 152 252 352 152 52 - - - - + 53 153 253 353 - - - - - - + 54 154 254 354 154 54 254 54 - - + 55 155 255 355 - - - - 355 55 + 56 156 256 356 156 56 - - - - + 57 157 257 357 - - 257 57 - - + 58 158 258 358 158 58 - - - - + 59 159 259 359 - - - - - - + 60 160 260 360 160 60 260 60 360 60 + 61 161 261 361 - - - - - - + 62 162 262 362 162 62 - - - - + 63 163 263 363 - - 263 63 - - + 64 164 264 364 164 64 - - - - + 65 165 265 365 - - - - 365 65 + 66 166 266 366 166 66 266 66 - - + 67 167 267 367 - - - - - - + 68 168 268 368 168 68 - - - - + 69 169 269 369 - - 269 69 - - + 70 170 270 370 170 70 - - 370 70 + 71 171 271 371 - - - - - - + 72 172 272 372 172 72 272 72 - - + 73 173 273 373 - - - - - - + 74 174 274 374 174 74 - - - - + 75 175 275 375 - - 275 75 375 75 + 76 176 276 376 176 76 - - - - + 77 177 277 377 - - - - - - + 78 178 278 378 178 78 278 78 - - + 79 179 279 379 - - - - - - + 80 180 280 380 180 80 - - 380 80 + 81 181 281 381 - - 281 81 - - + 82 182 282 382 182 82 - - - - + 83 183 283 383 - - - - - - + 84 184 284 384 184 84 284 84 - - + 85 185 285 385 - - - - 385 85 + 86 186 286 386 186 86 - - - - + 87 187 287 387 - - 287 87 - - + 88 188 288 388 188 88 - - - - + 89 189 289 389 - - - - - - + 90 190 290 390 190 90 290 90 390 90 + 91 191 291 391 - - - - - - + 92 192 292 392 192 92 - - - - + 93 193 293 393 - - 293 93 - - + 94 194 294 394 194 94 - - - - + 95 195 295 395 - - - - 395 95 + 96 - 296 396 - - - - - - + 97 197 - 397 - - - - - - + 98 198 298 - - - - - - - + 99 - - - - - - - - - + - - - - - - 200 0 - - + - - - - - - - - 300 0 +} +do_execsql_test joinD-1156 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON true + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t1.b=t2.b AND t2.x>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - +} +do_execsql_test joinD-1157 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 3 103 203 303 - - 203 3 - - + 6 106 206 306 106 6 206 6 - - + 9 109 209 309 - - 209 9 - - + 12 112 212 312 112 12 212 12 - - + 15 115 215 315 - - 215 15 315 15 + 18 118 218 318 118 18 218 18 - - + 21 121 221 321 - - 221 21 - - + 24 124 224 324 124 24 224 24 - - + 27 127 227 327 - - 227 27 - - + 30 130 230 330 130 30 230 30 330 30 + 33 133 233 333 - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + 39 139 239 339 - - 239 39 - - + 42 142 242 342 142 42 242 42 - - + 45 145 245 345 - - 245 45 345 45 + 48 148 248 348 148 48 248 48 - - + 51 151 251 351 - - 251 51 - - + 54 154 254 354 154 54 254 54 - - + 57 157 257 357 - - 257 57 - - + 60 160 260 360 160 60 260 60 360 60 + 63 163 263 363 - - 263 63 - - + 66 166 266 366 166 66 266 66 - - + 69 169 269 369 - - 269 69 - - + 72 172 272 372 172 72 272 72 - - + 75 175 275 375 - - 275 75 375 75 + 78 178 278 378 178 78 278 78 - - + 81 181 281 381 - - 281 81 - - + 84 184 284 384 184 84 284 84 - - + 87 187 287 387 - - 287 87 - - + 90 190 290 390 190 90 290 90 390 90 + 93 193 293 393 - - 293 93 - - +} +do_execsql_test joinD-1158 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t3.y>0 OR t3.y IS NULL + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 1 101 201 301 - - - - - - + 2 102 202 302 102 2 - - - - + 3 103 203 303 - - 203 3 - - + 4 104 204 304 104 4 - - - - + 5 105 205 305 - - - - 305 5 + 6 106 206 306 106 6 206 6 - - + 7 107 207 307 - - - - - - + 8 108 208 308 108 8 - - - - + 9 109 209 309 - - 209 9 - - + 10 110 210 310 110 10 - - 310 10 + 11 111 211 311 - - - - - - + 12 112 212 312 112 12 212 12 - - + 13 113 213 313 - - - - - - + 14 114 214 314 114 14 - - - - + 15 115 215 315 - - 215 15 315 15 + 16 116 216 316 116 16 - - - - + 17 117 217 317 - - - - - - + 18 118 218 318 118 18 218 18 - - + 19 119 219 319 - - - - - - + 20 120 220 320 120 20 - - 320 20 + 21 121 221 321 - - 221 21 - - + 22 122 222 322 122 22 - - - - + 23 123 223 323 - - - - - - + 24 124 224 324 124 24 224 24 - - + 25 125 225 325 - - - - 325 25 + 26 126 226 326 126 26 - - - - + 27 127 227 327 - - 227 27 - - + 28 128 228 328 128 28 - - - - + 29 129 229 329 - - - - - - + 30 130 230 330 130 30 230 30 330 30 + 31 131 231 331 - - - - - - + 32 132 232 332 132 32 - - - - + 33 133 233 333 - - 233 33 - - + 34 134 234 334 134 34 - - - - + 35 135 235 335 - - - - 335 35 + 36 136 236 336 136 36 236 36 - - + 37 137 237 337 - - - - - - + 38 138 238 338 138 38 - - - - + 39 139 239 339 - - 239 39 - - + 40 140 240 340 140 40 - - 340 40 + 41 141 241 341 - - - - - - + 42 142 242 342 142 42 242 42 - - + 43 143 243 343 - - - - - - + 44 144 244 344 144 44 - - - - + 45 145 245 345 - - 245 45 345 45 + 46 146 246 346 146 46 - - - - + 47 147 247 347 - - - - - - + 48 148 248 348 148 48 248 48 - - + 49 149 249 349 - - - - - - + 50 150 250 350 150 50 - - 350 50 + 51 151 251 351 - - 251 51 - - + 52 152 252 352 152 52 - - - - + 53 153 253 353 - - - - - - + 54 154 254 354 154 54 254 54 - - + 55 155 255 355 - - - - 355 55 + 56 156 256 356 156 56 - - - - + 57 157 257 357 - - 257 57 - - + 58 158 258 358 158 58 - - - - + 59 159 259 359 - - - - - - + 60 160 260 360 160 60 260 60 360 60 + 61 161 261 361 - - - - - - + 62 162 262 362 162 62 - - - - + 63 163 263 363 - - 263 63 - - + 64 164 264 364 164 64 - - - - + 65 165 265 365 - - - - 365 65 + 66 166 266 366 166 66 266 66 - - + 67 167 267 367 - - - - - - + 68 168 268 368 168 68 - - - - + 69 169 269 369 - - 269 69 - - + 70 170 270 370 170 70 - - 370 70 + 71 171 271 371 - - - - - - + 72 172 272 372 172 72 272 72 - - + 73 173 273 373 - - - - - - + 74 174 274 374 174 74 - - - - + 75 175 275 375 - - 275 75 375 75 + 76 176 276 376 176 76 - - - - + 77 177 277 377 - - - - - - + 78 178 278 378 178 78 278 78 - - + 79 179 279 379 - - - - - - + 80 180 280 380 180 80 - - 380 80 + 81 181 281 381 - - 281 81 - - + 82 182 282 382 182 82 - - - - + 83 183 283 383 - - - - - - + 84 184 284 384 184 84 284 84 - - + 85 185 285 385 - - - - 385 85 + 86 186 286 386 186 86 - - - - + 87 187 287 387 - - 287 87 - - + 88 188 288 388 188 88 - - - - + 89 189 289 389 - - - - - - + 90 190 290 390 190 90 290 90 390 90 + 91 191 291 391 - - - - - - + 92 192 292 392 192 92 - - - - + 93 193 293 393 - - 293 93 - - + 94 194 294 394 194 94 - - - - + 95 195 295 395 - - - - 395 95 + 96 - 296 396 - - - - - - + 97 197 - 397 - - - - - - + 98 198 298 - - - - - - - + 99 - - - - - - - - - + - - - - 100 0 - - - - + - - - - - - - - 300 0 +} +do_execsql_test joinD-1159 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 5 105 205 305 - - - - 305 5 + 10 110 210 310 110 10 - - 310 10 + 15 115 215 315 - - 215 15 315 15 + 20 120 220 320 120 20 - - 320 20 + 25 125 225 325 - - - - 325 25 + 30 130 230 330 130 30 230 30 330 30 + 35 135 235 335 - - - - 335 35 + 40 140 240 340 140 40 - - 340 40 + 45 145 245 345 - - 245 45 345 45 + 50 150 250 350 150 50 - - 350 50 + 55 155 255 355 - - - - 355 55 + 60 160 260 360 160 60 260 60 360 60 + 65 165 265 365 - - - - 365 65 + 70 170 270 370 170 70 - - 370 70 + 75 175 275 375 - - 275 75 375 75 + 80 180 280 380 180 80 - - 380 80 + 85 185 285 385 - - - - 385 85 + 90 190 290 390 190 90 290 90 390 90 + 95 195 295 395 - - - - 395 95 +} +do_execsql_test joinD-1160 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d + WHERE t4.z IS NULL OR t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 1 101 201 301 - - - - - - + 2 102 202 302 102 2 - - - - + 3 103 203 303 - - 203 3 - - + 4 104 204 304 104 4 - - - - + 5 105 205 305 - - - - 305 5 + 6 106 206 306 106 6 206 6 - - + 7 107 207 307 - - - - - - + 8 108 208 308 108 8 - - - - + 9 109 209 309 - - 209 9 - - + 10 110 210 310 110 10 - - 310 10 + 11 111 211 311 - - - - - - + 12 112 212 312 112 12 212 12 - - + 13 113 213 313 - - - - - - + 14 114 214 314 114 14 - - - - + 15 115 215 315 - - 215 15 315 15 + 16 116 216 316 116 16 - - - - + 17 117 217 317 - - - - - - + 18 118 218 318 118 18 218 18 - - + 19 119 219 319 - - - - - - + 20 120 220 320 120 20 - - 320 20 + 21 121 221 321 - - 221 21 - - + 22 122 222 322 122 22 - - - - + 23 123 223 323 - - - - - - + 24 124 224 324 124 24 224 24 - - + 25 125 225 325 - - - - 325 25 + 26 126 226 326 126 26 - - - - + 27 127 227 327 - - 227 27 - - + 28 128 228 328 128 28 - - - - + 29 129 229 329 - - - - - - + 30 130 230 330 130 30 230 30 330 30 + 31 131 231 331 - - - - - - + 32 132 232 332 132 32 - - - - + 33 133 233 333 - - 233 33 - - + 34 134 234 334 134 34 - - - - + 35 135 235 335 - - - - 335 35 + 36 136 236 336 136 36 236 36 - - + 37 137 237 337 - - - - - - + 38 138 238 338 138 38 - - - - + 39 139 239 339 - - 239 39 - - + 40 140 240 340 140 40 - - 340 40 + 41 141 241 341 - - - - - - + 42 142 242 342 142 42 242 42 - - + 43 143 243 343 - - - - - - + 44 144 244 344 144 44 - - - - + 45 145 245 345 - - 245 45 345 45 + 46 146 246 346 146 46 - - - - + 47 147 247 347 - - - - - - + 48 148 248 348 148 48 248 48 - - + 49 149 249 349 - - - - - - + 50 150 250 350 150 50 - - 350 50 + 51 151 251 351 - - 251 51 - - + 52 152 252 352 152 52 - - - - + 53 153 253 353 - - - - - - + 54 154 254 354 154 54 254 54 - - + 55 155 255 355 - - - - 355 55 + 56 156 256 356 156 56 - - - - + 57 157 257 357 - - 257 57 - - + 58 158 258 358 158 58 - - - - + 59 159 259 359 - - - - - - + 60 160 260 360 160 60 260 60 360 60 + 61 161 261 361 - - - - - - + 62 162 262 362 162 62 - - - - + 63 163 263 363 - - 263 63 - - + 64 164 264 364 164 64 - - - - + 65 165 265 365 - - - - 365 65 + 66 166 266 366 166 66 266 66 - - + 67 167 267 367 - - - - - - + 68 168 268 368 168 68 - - - - + 69 169 269 369 - - 269 69 - - + 70 170 270 370 170 70 - - 370 70 + 71 171 271 371 - - - - - - + 72 172 272 372 172 72 272 72 - - + 73 173 273 373 - - - - - - + 74 174 274 374 174 74 - - - - + 75 175 275 375 - - 275 75 375 75 + 76 176 276 376 176 76 - - - - + 77 177 277 377 - - - - - - + 78 178 278 378 178 78 278 78 - - + 79 179 279 379 - - - - - - + 80 180 280 380 180 80 - - 380 80 + 81 181 281 381 - - 281 81 - - + 82 182 282 382 182 82 - - - - + 83 183 283 383 - - - - - - + 84 184 284 384 184 84 284 84 - - + 85 185 285 385 - - - - 385 85 + 86 186 286 386 186 86 - - - - + 87 187 287 387 - - 287 87 - - + 88 188 288 388 188 88 - - - - + 89 189 289 389 - - - - - - + 90 190 290 390 190 90 290 90 390 90 + 91 191 291 391 - - - - - - + 92 192 292 392 192 92 - - - - + 93 193 293 393 - - 293 93 - - + 94 194 294 394 194 94 - - - - + 95 195 295 395 - - - - 395 95 + 96 - 296 396 - - - - - - + 97 197 - 397 - - - - - - + 98 198 298 - - - - - - - + 99 - - - - - - - - - + - - - - 100 0 - - - - + - - - - - - 200 0 - - +} +do_execsql_test joinD-1161 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1162 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d + WHERE t4.z>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 15 115 215 315 - - 215 15 315 15 + 30 130 230 330 130 30 230 30 330 30 + 45 145 245 345 - - 245 45 345 45 + 60 160 260 360 160 60 260 60 360 60 + 75 175 275 375 - - 275 75 375 75 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1163 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d AND t4.z>0 + WHERE t2.x>0 AND t3.y>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1164 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b=t2.b + FULL JOIN t3 ON t1.c=t3.c + FULL JOIN t4 ON t1.d=t4.d + WHERE t2.x>0 AND t3.y>0 AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 30 130 230 330 130 30 230 30 330 30 + 60 160 260 360 160 60 260 60 360 60 + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1165 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t1.b = t2.b AND t2.x>0 + FULL JOIN t3 ON t1.c = t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d = t4.d AND t4.z>0 + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 0 100 200 300 - - - - - - + 1 101 201 301 - - - - - - + 2 102 202 302 102 2 - - - - + 3 103 203 303 - - 203 3 - - + 4 104 204 304 104 4 - - - - + 5 105 205 305 - - - - 305 5 + 6 106 206 306 106 6 206 6 - - + 7 107 207 307 - - - - - - + 8 108 208 308 108 8 - - - - + 9 109 209 309 - - 209 9 - - + 10 110 210 310 110 10 - - 310 10 + 11 111 211 311 - - - - - - + 12 112 212 312 112 12 212 12 - - + 13 113 213 313 - - - - - - + 14 114 214 314 114 14 - - - - + 15 115 215 315 - - 215 15 315 15 + 16 116 216 316 116 16 - - - - + 17 117 217 317 - - - - - - + 18 118 218 318 118 18 218 18 - - + 19 119 219 319 - - - - - - + 20 120 220 320 120 20 - - 320 20 + 21 121 221 321 - - 221 21 - - + 22 122 222 322 122 22 - - - - + 23 123 223 323 - - - - - - + 24 124 224 324 124 24 224 24 - - + 25 125 225 325 - - - - 325 25 + 26 126 226 326 126 26 - - - - + 27 127 227 327 - - 227 27 - - + 28 128 228 328 128 28 - - - - + 29 129 229 329 - - - - - - + 30 130 230 330 130 30 230 30 330 30 + 31 131 231 331 - - - - - - + 32 132 232 332 132 32 - - - - + 33 133 233 333 - - 233 33 - - + 34 134 234 334 134 34 - - - - + 35 135 235 335 - - - - 335 35 + 36 136 236 336 136 36 236 36 - - + 37 137 237 337 - - - - - - + 38 138 238 338 138 38 - - - - + 39 139 239 339 - - 239 39 - - + 40 140 240 340 140 40 - - 340 40 + 41 141 241 341 - - - - - - + 42 142 242 342 142 42 242 42 - - + 43 143 243 343 - - - - - - + 44 144 244 344 144 44 - - - - + 45 145 245 345 - - 245 45 345 45 + 46 146 246 346 146 46 - - - - + 47 147 247 347 - - - - - - + 48 148 248 348 148 48 248 48 - - + 49 149 249 349 - - - - - - + 50 150 250 350 150 50 - - 350 50 + 51 151 251 351 - - 251 51 - - + 52 152 252 352 152 52 - - - - + 53 153 253 353 - - - - - - + 54 154 254 354 154 54 254 54 - - + 55 155 255 355 - - - - 355 55 + 56 156 256 356 156 56 - - - - + 57 157 257 357 - - 257 57 - - + 58 158 258 358 158 58 - - - - + 59 159 259 359 - - - - - - + 60 160 260 360 160 60 260 60 360 60 + 61 161 261 361 - - - - - - + 62 162 262 362 162 62 - - - - + 63 163 263 363 - - 263 63 - - + 64 164 264 364 164 64 - - - - + 65 165 265 365 - - - - 365 65 + 66 166 266 366 166 66 266 66 - - + 67 167 267 367 - - - - - - + 68 168 268 368 168 68 - - - - + 69 169 269 369 - - 269 69 - - + 70 170 270 370 170 70 - - 370 70 + 71 171 271 371 - - - - - - + 72 172 272 372 172 72 272 72 - - + 73 173 273 373 - - - - - - + 74 174 274 374 174 74 - - - - + 75 175 275 375 - - 275 75 375 75 + 76 176 276 376 176 76 - - - - + 77 177 277 377 - - - - - - + 78 178 278 378 178 78 278 78 - - + 79 179 279 379 - - - - - - + 80 180 280 380 180 80 - - 380 80 + 81 181 281 381 - - 281 81 - - + 82 182 282 382 182 82 - - - - + 83 183 283 383 - - - - - - + 84 184 284 384 184 84 284 84 - - + 85 185 285 385 - - - - 385 85 + 86 186 286 386 186 86 - - - - + 87 187 287 387 - - 287 87 - - + 88 188 288 388 188 88 - - - - + 89 189 289 389 - - - - - - + 90 190 290 390 190 90 290 90 390 90 + 91 191 291 391 - - - - - - + 92 192 292 392 192 92 - - - - + 93 193 293 393 - - 293 93 - - + 94 194 294 394 194 94 - - - - + 95 195 295 395 - - - - 395 95 + 96 - 296 396 - - - - - - + 97 197 - 397 - - - - - - + 98 198 298 - - - - - - - + 99 - - - - - - - - - + - - - - 100 0 - - - - + - - - - - - 200 0 - - + - - - - - - - - 300 0 +} +do_execsql_test joinD-1166 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t2.x>0 + FULL JOIN t3 ON t1.c = t3.c AND t3.y>0 + FULL JOIN t4 ON t1.d = t4.d AND t4.z>0 + WHERE t1.b = t2.b + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 2 102 202 302 102 2 - - - - + 4 104 204 304 104 4 - - - - + 6 106 206 306 106 6 206 6 - - + 8 108 208 308 108 8 - - - - + 10 110 210 310 110 10 - - 310 10 + 12 112 212 312 112 12 212 12 - - + 14 114 214 314 114 14 - - - - + 16 116 216 316 116 16 - - - - + 18 118 218 318 118 18 218 18 - - + 20 120 220 320 120 20 - - 320 20 + 22 122 222 322 122 22 - - - - + 24 124 224 324 124 24 224 24 - - + 26 126 226 326 126 26 - - - - + 28 128 228 328 128 28 - - - - + 30 130 230 330 130 30 230 30 330 30 + 32 132 232 332 132 32 - - - - + 34 134 234 334 134 34 - - - - + 36 136 236 336 136 36 236 36 - - + 38 138 238 338 138 38 - - - - + 40 140 240 340 140 40 - - 340 40 + 42 142 242 342 142 42 242 42 - - + 44 144 244 344 144 44 - - - - + 46 146 246 346 146 46 - - - - + 48 148 248 348 148 48 248 48 - - + 50 150 250 350 150 50 - - 350 50 + 52 152 252 352 152 52 - - - - + 54 154 254 354 154 54 254 54 - - + 56 156 256 356 156 56 - - - - + 58 158 258 358 158 58 - - - - + 60 160 260 360 160 60 260 60 360 60 + 62 162 262 362 162 62 - - - - + 64 164 264 364 164 64 - - - - + 66 166 266 366 166 66 266 66 - - + 68 168 268 368 168 68 - - - - + 70 170 270 370 170 70 - - 370 70 + 72 172 272 372 172 72 272 72 - - + 74 174 274 374 174 74 - - - - + 76 176 276 376 176 76 - - - - + 78 178 278 378 178 78 278 78 - - + 80 180 280 380 180 80 - - 380 80 + 82 182 282 382 182 82 - - - - + 84 184 284 384 184 84 284 84 - - + 86 186 286 386 186 86 - - - - + 88 188 288 388 188 88 - - - - + 90 190 290 390 190 90 290 90 390 90 + 92 192 292 392 192 92 - - - - + 94 194 294 394 194 94 - - - - +} +do_execsql_test joinD-1167 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t2.x>0 + FULL JOIN t3 ON t3.y>0 + FULL JOIN t4 ON t1.d = t4.d AND t4.z>0 + WHERE t1.b = t2.b AND t1.c = t3.c + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 6 106 206 306 106 6 206 6 - - + 12 112 212 312 112 12 212 12 - - + 18 118 218 318 118 18 218 18 - - + 24 124 224 324 124 24 224 24 - - + 30 130 230 330 130 30 230 30 330 30 + 36 136 236 336 136 36 236 36 - - + 42 142 242 342 142 42 242 42 - - + 48 148 248 348 148 48 248 48 - - + 54 154 254 354 154 54 254 54 - - + 60 160 260 360 160 60 260 60 360 60 + 66 166 266 366 166 66 266 66 - - + 72 172 272 372 172 72 272 72 - - + 78 178 278 378 178 78 278 78 - - + 84 184 284 384 184 84 284 84 - - + 90 190 290 390 190 90 290 90 390 90 +} +do_execsql_test joinD-1168 { + SELECT t1.*, t2.*, t3.*, t4.* + FROM t1 FULL JOIN t2 ON t2.x>0 + FULL JOIN t3 ON t1.c = t3.c AND t3.y>0 + FULL JOIN t4 ON t4.z>0 + WHERE t1.b = t2.b AND t1.d = t4.d + ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0); +} { + 10 110 210 310 110 10 - - 310 10 + 20 120 220 320 120 20 - - 320 20 + 30 130 230 330 130 30 230 30 330 30 + 40 140 240 340 140 40 - - 340 40 + 50 150 250 350 150 50 - - 350 50 + 60 160 260 360 160 60 260 60 360 60 + 70 170 270 370 170 70 - - 370 70 + 80 180 280 380 180 80 - - 380 80 + 90 190 290 390 190 90 290 90 390 90 +} +############################################################################# +# The following are extra tests added manually +do_execsql_test joinD-extra-1000 { + CREATE VIEW v1 AS + SELECT * + FROM t1 INNER JOIN t2 ON t1.b=t2.b AND t2.x>0 + RIGHT JOIN t3 ON t1.c=t3.c AND t3.y>0 + LEFT JOIN t4 ON t1.d=t4.d AND t4.z>0; + CREATE TRIGGER v1r1 INSTEAD OF UPDATE OF c ON v1 BEGIN + UPDATE t1 SET c=new.c WHERE (a,b,c,d) IS (old.a,old.b,old.c,old.d); + UPDATE t3 SET c=new.c WHERE (c,y) IS (old.c,old.y); + END; + SELECT * FROM v1 WHERE y BETWEEN 30 AND 40 ORDER BY y; +} { + 30 130 230 330 130 30 230 30 330 30 + - - - - - - 233 33 - - + 36 136 236 336 136 36 236 36 - - + - - - - - - 239 39 - - +} +do_execsql_test joinD-extra-1010 { + BEGIN; + UPDATE v1 SET c=c+1000 WHERE y BETWEEN 30 and 40; + SELECT * FROM v1 WHERE y BETWEEN 30 AND 40 ORDER BY y; + ROLLBACK; +} { + 30 130 1230 330 130 30 1230 30 330 30 + - - - - - - 233 33 - - + 36 136 1236 336 136 36 1236 36 - - + - - - - - - 239 39 - - +} +finish_test +############################################################################# +# This is the TCL script used to generate the psql script that generated +# the data above. +# +# puts " +# \\pset border off +# \\pset tuples_only on +# \\pset null - +# +# DROP TABLE IF EXISTS t1; +# DROP TABLE IF EXISTS t2; +# DROP TABLE IF EXISTS t3; +# DROP TABLE IF EXISTS t4; +# CREATE TABLE t1(a INT, b INT, c INT, d INT); +# WITH RECURSIVE c(x) AS (VALUES(0) UNION ALL SELECT x+1 FROM c WHERE x<95) +# INSERT INTO t1(a,b,c,d) SELECT x, x+100, x+200, x+300 FROM c; +# CREATE TABLE t2(b INT, x INT); +# INSERT INTO t2(b,x) SELECT b, a FROM t1 WHERE a%2=0; +# CREATE INDEX t2b ON t2(b); +# CREATE TABLE t3(c INT, y INT); +# INSERT INTO t3(c,y) SELECT c, a FROM t1 WHERE a%3=0; +# CREATE INDEX t3c ON t3(c); +# CREATE TABLE t4(d INT, z INT); +# INSERT INTO t4(d,z) SELECT d, a FROM t1 WHERE a%5=0; +# CREATE INDEX t4d ON t4(d); +# INSERT INTO t1(a,b,c,d) VALUES +# (96,NULL,296,396), +# (97,197,NULL,397), +# (98,198,298,NULL), +# (99,NULL,NULL,NULL); +# " +# +# proc echo {prefix txt} { +# regsub -all {\n} $txt \n$prefix txt +# puts "$prefix$txt" +# } +# +# set n 0 +# foreach j1 {INNER LEFT RIGHT FULL} { +# foreach j2 {INNER LEFT RIGHT FULL} { +# foreach j3 {INNER LEFT RIGHT FULL} { +# +# incr n +# set q1 "" +# append q1 "SELECT t1.*, t2.*, t3.*, t4.*\n" +# append q1 " FROM t1 $j1 JOIN t2 ON t1.b=t2.b AND t2.x>0\n" +# append q1 " $j2 JOIN t3 ON t1.c=t3.c AND t3.y>0\n" +# append q1 " $j3 JOIN t4 ON t1.d=t4.d AND t4.z>0\n" +# append q1 " ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0);" +# +# echo "\\qecho " "do_execsql_test joinB-$n \{" +# echo "\\qecho X " $q1 +# echo "\\qecho " "\} \{" +# puts $q1 +# echo "\\qecho " "\}" +# +# if {$j1!="FULL"} { +# incr n +# set q1 "" +# append q1 "SELECT t1.*, t2.*, t3.*, t4.*\n" +# append q1 " FROM t1 $j1 JOIN t2 ON t1.b IS NOT DISTINCT FROM t2.b AND t2.x>0\n" +# append q1 " $j2 JOIN t3 ON t1.c=t3.c AND t3.y>0\n" +# append q1 " $j3 JOIN t4 ON t1.d=t4.d AND t4.z>0\n" +# append q1 " ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0);" +# +# echo "\\qecho " "do_execsql_test joinB-$n \{" +# echo "\\qecho X " $q1 +# echo "\\qecho " "\} \{" +# puts $q1 +# echo "\\qecho " "\}" +# } +# +# incr n +# set q1 "" +# append q1 "SELECT t1.*, t2.*, t3.*, t4.*\n" +# append q1 " FROM t1 $j1 JOIN t2 ON t1.b=t2.b\n" +# append q1 " $j2 JOIN t3 ON t1.c=t3.c AND t3.y>0\n" +# append q1 " $j3 JOIN t4 ON t1.d=t4.d AND t4.z>0\n" +# append q1 " WHERE t2.x>0\n" +# append q1 " ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0);" +# +# echo "\\qecho " "do_execsql_test joinB-$n \{" +# echo "\\qecho X " $q1 +# echo "\\qecho " "\} \{" +# puts $q1 +# echo "\\qecho " "\}" +# +# incr n +# set q1 "" +# append q1 "SELECT t1.*, t2.*, t3.*, t4.*\n" +# append q1 " FROM t1 $j1 JOIN t2 ON t1.b=t2.b\n" +# append q1 " $j2 JOIN t3 ON t1.c=t3.c AND t3.y>0\n" +# append q1 " $j3 JOIN t4 ON t1.d=t4.d AND t4.z>0\n" +# append q1 " WHERE (t2.x>0 OR t2.x IS NULL)\n" +# append q1 " ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0);" +# +# echo "\\qecho " "do_execsql_test joinB-$n \{" +# echo "\\qecho X " $q1 +# echo "\\qecho " "\} \{" +# puts $q1 +# echo "\\qecho " "\}" +# +# incr n +# set q1 "" +# append q1 "SELECT t1.*, t2.*, t3.*, t4.*\n" +# append q1 " FROM t1 $j1 JOIN t2 ON true\n" +# append q1 " $j2 JOIN t3 ON t1.c=t3.c AND t3.y>0\n" +# append q1 " $j3 JOIN t4 ON t1.d=t4.d AND t4.z>0\n" +# append q1 " WHERE t1.b=t2.b AND t2.x>0\n" +# append q1 " ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0);" +# +# echo "\\qecho " "do_execsql_test joinB-$n \{" +# echo "\\qecho X " $q1 +# echo "\\qecho " "\} \{" +# puts $q1 +# echo "\\qecho " "\}" +# +# incr n +# set q1 "" +# append q1 "SELECT t1.*, t2.*, t3.*, t4.*\n" +# append q1 " FROM t1 $j1 JOIN t2 ON t1.b=t2.b AND t2.x>0\n" +# append q1 " $j2 JOIN t3 ON t1.c=t3.c\n" +# append q1 " $j3 JOIN t4 ON t1.d=t4.d AND t4.z>0\n" +# append q1 " WHERE t3.y>0\n" +# append q1 " ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0);" +# +# echo "\\qecho " "do_execsql_test joinB-$n \{" +# echo "\\qecho X " $q1 +# echo "\\qecho " "\} \{" +# puts $q1 +# echo "\\qecho " "\}" +# +# incr n +# set q1 "" +# append q1 "SELECT t1.*, t2.*, t3.*, t4.*\n" +# append q1 " FROM t1 $j1 JOIN t2 ON t1.b=t2.b AND t2.x>0\n" +# append q1 " $j2 JOIN t3 ON t1.c=t3.c\n" +# append q1 " $j3 JOIN t4 ON t1.d=t4.d AND t4.z>0\n" +# append q1 " WHERE t3.y>0 OR t3.y IS NULL\n" +# append q1 " ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0);" +# +# echo "\\qecho " "do_execsql_test joinB-$n \{" +# echo "\\qecho X " $q1 +# echo "\\qecho " "\} \{" +# puts $q1 +# echo "\\qecho " "\}" +# +# incr n +# set q1 "" +# append q1 "SELECT t1.*, t2.*, t3.*, t4.*\n" +# append q1 " FROM t1 $j1 JOIN t2 ON t1.b=t2.b AND t2.x>0\n" +# append q1 " $j2 JOIN t3 ON t1.c=t3.c AND t3.y>0\n" +# append q1 " $j3 JOIN t4 ON t1.d=t4.d\n" +# append q1 " WHERE t4.z>0\n" +# append q1 " ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0);" +# +# echo "\\qecho " "do_execsql_test joinB-$n \{" +# echo "\\qecho X " $q1 +# echo "\\qecho " "\} \{" +# puts $q1 +# echo "\\qecho " "\}" +# +# incr n +# set q1 "" +# append q1 "SELECT t1.*, t2.*, t3.*, t4.*\n" +# append q1 " FROM t1 $j1 JOIN t2 ON t1.b=t2.b AND t2.x>0\n" +# append q1 " $j2 JOIN t3 ON t1.c=t3.c AND t3.y>0\n" +# append q1 " $j3 JOIN t4 ON t1.d=t4.d\n" +# append q1 " WHERE t4.z IS NULL OR t4.z>0\n" +# append q1 " ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0);" +# +# echo "\\qecho " "do_execsql_test joinB-$n \{" +# echo "\\qecho X " $q1 +# echo "\\qecho " "\} \{" +# puts $q1 +# echo "\\qecho " "\}" +# +# incr n +# set q1 "" +# append q1 "SELECT t1.*, t2.*, t3.*, t4.*\n" +# append q1 " FROM t1 $j1 JOIN t2 ON t1.b=t2.b\n" +# append q1 " $j2 JOIN t3 ON t1.c=t3.c AND t3.y>0\n" +# append q1 " $j3 JOIN t4 ON t1.d=t4.d\n" +# append q1 " WHERE t2.x>0 AND t4.z>0\n" +# append q1 " ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0);" +# +# echo "\\qecho " "do_execsql_test joinB-$n \{" +# echo "\\qecho X " $q1 +# echo "\\qecho " "\} \{" +# puts $q1 +# echo "\\qecho " "\}" +# +# incr n +# set q1 "" +# append q1 "SELECT t1.*, t2.*, t3.*, t4.*\n" +# append q1 " FROM t1 $j1 JOIN t2 ON t1.b=t2.b AND t2.x>0\n" +# append q1 " $j2 JOIN t3 ON t1.c=t3.c\n" +# append q1 " $j3 JOIN t4 ON t1.d=t4.d\n" +# append q1 " WHERE t4.z>0 AND t3.y>0\n" +# append q1 " ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0);" +# +# echo "\\qecho " "do_execsql_test joinB-$n \{" +# echo "\\qecho X " $q1 +# echo "\\qecho " "\} \{" +# puts $q1 +# echo "\\qecho " "\}" +# +# incr n +# set q1 "" +# append q1 "SELECT t1.*, t2.*, t3.*, t4.*\n" +# append q1 " FROM t1 $j1 JOIN t2 ON t1.b=t2.b\n" +# append q1 " $j2 JOIN t3 ON t1.c=t3.c\n" +# append q1 " $j3 JOIN t4 ON t1.d=t4.d AND t4.z>0\n" +# append q1 " WHERE t2.x>0 AND t3.y>0\n" +# append q1 " ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0);" +# +# echo "\\qecho " "do_execsql_test joinB-$n \{" +# echo "\\qecho X " $q1 +# echo "\\qecho " "\} \{" +# puts $q1 +# echo "\\qecho " "\}" +# +# incr n +# set q1 "" +# append q1 "SELECT t1.*, t2.*, t3.*, t4.*\n" +# append q1 " FROM t1 $j1 JOIN t2 ON t1.b=t2.b\n" +# append q1 " $j2 JOIN t3 ON t1.c=t3.c\n" +# append q1 " $j3 JOIN t4 ON t1.d=t4.d\n" +# append q1 " WHERE t2.x>0 AND t3.y>0 AND t4.z>0\n" +# append q1 " ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0);" +# +# echo "\\qecho " "do_execsql_test joinB-$n \{" +# echo "\\qecho X " $q1 +# echo "\\qecho " "\} \{" +# puts $q1 +# echo "\\qecho " "\}" +# +# set op1 [expr {$j1=="FULL"?"=":"IS NOT DISTINCT FROM"}] +# set op2 [expr {$j2=="FULL"?"=":"IS NOT DISTINCT FROM"}] +# set op3 [expr {$j3=="FULL"?"=":"IS NOT DISTINCT FROM"}] +# incr n +# set q1 "" +# append q1 "SELECT t1.*, t2.*, t3.*, t4.*\n" +# append q1 " FROM t1 $j1 JOIN t2 ON t1.b $op1 t2.b AND t2.x>0\n" +# append q1 " $j2 JOIN t3 ON t1.c $op2 t3.c AND t3.y>0\n" +# append q1 " $j3 JOIN t4 ON t1.d $op3 t4.d AND t4.z>0\n" +# append q1 " ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0);" +# +# echo "\\qecho " "do_execsql_test joinB-$n \{" +# echo "\\qecho X " $q1 +# echo "\\qecho " "\} \{" +# puts $q1 +# echo "\\qecho " "\}" +# +# incr n +# set q1 "" +# append q1 "SELECT t1.*, t2.*, t3.*, t4.*\n" +# append q1 " FROM t1 $j1 JOIN t2 ON t2.x>0\n" +# append q1 " $j2 JOIN t3 ON t1.c $op2 t3.c AND t3.y>0\n" +# append q1 " $j3 JOIN t4 ON t1.d $op3 t4.d AND t4.z>0\n" +# append q1 " WHERE t1.b $op1 t2.b\n" +# append q1 " ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0);" +# +# echo "\\qecho " "do_execsql_test joinB-$n \{" +# echo "\\qecho X " $q1 +# echo "\\qecho " "\} \{" +# puts $q1 +# echo "\\qecho " "\}" +# +# incr n +# set q1 "" +# append q1 "SELECT t1.*, t2.*, t3.*, t4.*\n" +# append q1 " FROM t1 $j1 JOIN t2 ON t2.x>0\n" +# append q1 " $j2 JOIN t3 ON t3.y>0\n" +# append q1 " $j3 JOIN t4 ON t1.d $op3 t4.d AND t4.z>0\n" +# append q1 " WHERE t1.b $op1 t2.b AND t1.c $op2 t3.c\n" +# append q1 " ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0);" +# +# echo "\\qecho " "do_execsql_test joinB-$n \{" +# echo "\\qecho X " $q1 +# echo "\\qecho " "\} \{" +# puts $q1 +# echo "\\qecho " "\}" +# +# incr n +# set q1 "" +# append q1 "SELECT t1.*, t2.*, t3.*, t4.*\n" +# append q1 " FROM t1 $j1 JOIN t2 ON t2.x>0\n" +# append q1 " $j2 JOIN t3 ON t1.c $op2 t3.c AND t3.y>0\n" +# append q1 " $j3 JOIN t4 ON t4.z>0\n" +# append q1 " WHERE t1.b $op1 t2.b AND t1.d $op3 t4.d\n" +# append q1 " ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0);" +# +# echo "\\qecho " "do_execsql_test joinB-$n \{" +# echo "\\qecho X " $q1 +# echo "\\qecho " "\} \{" +# puts $q1 +# echo "\\qecho " "\}" +# +# if {$j1!="FULL"} { +# incr n +# set q1 "" +# append q1 "SELECT t1.*, t2.*, t3.*, t4.*\n" +# append q1 " FROM t1 $j1 JOIN t2 ON t1.b IN (t2.b,-2,-3) AND t2.x>0\n" +# append q1 " $j2 JOIN t3 ON t1.c=t3.c AND t3.y>0\n" +# append q1 " $j3 JOIN t4 ON t1.d=t4.d AND t4.z>0\n" +# append q1 " ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0);" +# +# echo "\\qecho " "do_execsql_test joinB-$n \{" +# echo "\\qecho X " $q1 +# echo "\\qecho " "\} \{" +# puts $q1 +# echo "\\qecho " "\}" +# } +# +# if {$j2!="FULL"} { +# incr n +# set q1 "" +# append q1 "SELECT t1.*, t2.*, t3.*, t4.*\n" +# append q1 " FROM t1 $j1 JOIN t2 ON t1.b=t2.b AND t2.x>0\n" +# append q1 " $j2 JOIN t3 ON t1.c IN (-4,t3.c,-5) AND t3.y>0\n" +# append q1 " $j3 JOIN t4 ON t1.d=t4.d AND t4.z>0\n" +# append q1 " ORDER BY coalesce(t1.a,t2.b,t3.c,t4.d,0);" +# +# echo "\\qecho " "do_execsql_test joinB-$n \{" +# echo "\\qecho X " $q1 +# echo "\\qecho " "\} \{" +# puts $q1 +# echo "\\qecho " "\}" +# } +# } +# } +# } +# diff --git a/test/joinE.test b/test/joinE.test new file mode 100644 index 0000000000..8c8e72ab2d --- /dev/null +++ b/test/joinE.test @@ -0,0 +1,443 @@ +# 2022-05-13 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# This file implements tests for JOINs that use Bloom filters. +# +# The test case output is (mostly) all generated by PostgreSQL 14. This +# test module was created as follows: +# +# 1. Run a TCL script (included at the bottom of this file) that +# generates an input script for "psql" that will run man +# diverse tests on joins. +# +# 2. Run the script from step (1) through psql and collect the +# output. +# +# 3. Make a few minor global search-and-replace operations to convert +# the psql output into a form suitable for this test module. +# +# 4. Add this header, and the script content at the footer. +# +set testdir [file dirname $argv0] +source $testdir/tester.tcl +db nullvalue - +db eval { + CREATE TABLE t1(a INT); + INSERT INTO t1 VALUES(1),(NULL); + CREATE TABLE t2(b INT); + INSERT INTO t2 VALUES(2),(NULL); +} +do_execsql_test joinE-1 { + SELECT a, b + FROM t1 INNER JOIN t2 ON true + ORDER BY coalesce(a,b,3); +} { + 1 2 + 1 - + - 2 + - - +} +do_execsql_test joinE-2 { + SELECT a, b + FROM t1 INNER JOIN t2 ON true WHERE a IS NULL + ORDER BY coalesce(a,b,3); +} { + - 2 + - - +} +do_execsql_test joinE-3 { + SELECT a, b + FROM t1 INNER JOIN t2 ON a IS NULL + ORDER BY coalesce(a,b,3); +} { + - 2 + - - +} +do_execsql_test joinE-4 { + SELECT a, b + FROM t1 INNER JOIN t2 ON true WHERE b IS NULL + ORDER BY coalesce(a,b,3); +} { + 1 - + - - +} +do_execsql_test joinE-5 { + SELECT a, b + FROM t1 INNER JOIN t2 ON b IS NULL + ORDER BY coalesce(a,b,3); +} { + 1 - + - - +} +do_execsql_test joinE-6 { + SELECT a, b + FROM t1 LEFT JOIN t2 ON true + ORDER BY coalesce(a,b,3); +} { + 1 2 + 1 - + - 2 + - - +} +do_execsql_test joinE-7 { + SELECT a, b + FROM t1 LEFT JOIN t2 ON true WHERE a IS NULL + ORDER BY coalesce(a,b,3); +} { + - 2 + - - +} +do_execsql_test joinE-8 { + SELECT a, b + FROM t1 LEFT JOIN t2 ON a IS NULL + ORDER BY coalesce(a,b,3); +} { + 1 - + - 2 + - - +} +do_execsql_test joinE-9 { + SELECT a, b + FROM t1 LEFT JOIN t2 ON true WHERE b IS NULL + ORDER BY coalesce(a,b,3); +} { + 1 - + - - +} +do_execsql_test joinE-10 { + SELECT a, b + FROM t1 LEFT JOIN t2 ON b IS NULL + ORDER BY coalesce(a,b,3); +} { + 1 - + - - +} +do_execsql_test joinE-11 { + SELECT a, b + FROM t1 RIGHT JOIN t2 ON true + ORDER BY coalesce(a,b,3); +} { + 1 2 + 1 - + - 2 + - - +} +do_execsql_test joinE-12 { + SELECT a, b + FROM t1 RIGHT JOIN t2 ON true WHERE a IS NULL + ORDER BY coalesce(a,b,3); +} { + - 2 + - - +} +do_execsql_test joinE-13 { + SELECT a, b + FROM t1 RIGHT JOIN t2 ON a IS NULL + ORDER BY coalesce(a,b,3); +} { + - 2 + - - +} +do_execsql_test joinE-14 { + SELECT a, b + FROM t1 RIGHT JOIN t2 ON true WHERE b IS NULL + ORDER BY coalesce(a,b,3); +} { + 1 - + - - +} +do_execsql_test joinE-15 { + SELECT a, b + FROM t1 RIGHT JOIN t2 ON b IS NULL + ORDER BY coalesce(a,b,3); +} { + 1 - + - 2 + - - +} +do_execsql_test joinE-16 { + SELECT a, b + FROM t1 FULL JOIN t2 ON true + ORDER BY coalesce(a,b,3); +} { + 1 2 + 1 - + - 2 + - - +} +do_execsql_test joinE-17 { + SELECT a, b + FROM t1 FULL JOIN t2 ON true WHERE a IS NULL + ORDER BY coalesce(a,b,3); +} { + - 2 + - - +} + +# PG-14 is unable to perform this join. It says: FULL JOIN is only +# supported with merge-joinable or hash-joinable join conditions +# +# do_execsql_test joinE-18 { +# SELECT a, b +# FROM t1 FULL JOIN t2 ON a IS NULL +# ORDER BY coalesce(a,b,3); +# } { +# } + +do_execsql_test joinE-19 { + SELECT a, b + FROM t1 FULL JOIN t2 ON true WHERE b IS NULL + ORDER BY coalesce(a,b,3); +} { + 1 - + - - +} + +# PG-14 is unable to perform this join. It says: FULL JOIN is only +# supported with merge-joinable or hash-joinable join conditions +# +# do_execsql_test joinE-20 { +# SELECT a, b +# FROM t1 FULL JOIN t2 ON b IS NULL +# ORDER BY coalesce(a,b,3); +# } { +# } + +db eval { + DELETE FROM t1; + INSERT INTO t1 VALUES(1); + DELETE FROM t2; + INSERT INTO t2 VALUES(NULL); +} + +do_execsql_test joinE-21 { + SELECT a, b + FROM t1 INNER JOIN t2 ON true + ORDER BY coalesce(a,b,3); +} { + 1 - +} +do_execsql_test joinE-22 { + SELECT a, b + FROM t1 INNER JOIN t2 ON true WHERE a IS NULL + ORDER BY coalesce(a,b,3); +} { +} +do_execsql_test joinE-23 { + SELECT a, b + FROM t1 INNER JOIN t2 ON a IS NULL + ORDER BY coalesce(a,b,3); +} { +} +do_execsql_test joinE-24 { + SELECT a, b + FROM t1 INNER JOIN t2 ON true WHERE b IS NULL + ORDER BY coalesce(a,b,3); +} { + 1 - +} +do_execsql_test joinE-25 { + SELECT a, b + FROM t1 INNER JOIN t2 ON b IS NULL + ORDER BY coalesce(a,b,3); +} { + 1 - +} +do_execsql_test joinE-26 { + SELECT a, b + FROM t1 LEFT JOIN t2 ON true + ORDER BY coalesce(a,b,3); +} { + 1 - +} +do_execsql_test joinE-27 { + SELECT a, b + FROM t1 LEFT JOIN t2 ON true WHERE a IS NULL + ORDER BY coalesce(a,b,3); +} { +} +do_execsql_test joinE-28 { + SELECT a, b + FROM t1 LEFT JOIN t2 ON a IS NULL + ORDER BY coalesce(a,b,3); +} { + 1 - +} +do_execsql_test joinE-29 { + SELECT a, b + FROM t1 LEFT JOIN t2 ON true WHERE b IS NULL + ORDER BY coalesce(a,b,3); +} { + 1 - +} +do_execsql_test joinE-30 { + SELECT a, b + FROM t1 LEFT JOIN t2 ON b IS NULL + ORDER BY coalesce(a,b,3); +} { + 1 - +} +do_execsql_test joinE-31 { + SELECT a, b + FROM t1 RIGHT JOIN t2 ON true + ORDER BY coalesce(a,b,3); +} { + 1 - +} + +do_execsql_test joinE-32 { + SELECT a, b + FROM t1 RIGHT JOIN t2 ON true WHERE a IS NULL + ORDER BY coalesce(a,b,3); +} { +} + +do_execsql_test joinE-33 { + SELECT a, b + FROM t1 RIGHT JOIN t2 ON a IS NULL + ORDER BY coalesce(a,b,3); +} { + - - +} +do_execsql_test joinE-34 { + SELECT a, b + FROM t1 RIGHT JOIN t2 ON true WHERE b IS NULL + ORDER BY coalesce(a,b,3); +} { + 1 - +} +do_execsql_test joinE-35 { + SELECT a, b + FROM t1 RIGHT JOIN t2 ON b IS NULL + ORDER BY coalesce(a,b,3); +} { + 1 - +} +do_execsql_test joinE-36 { + SELECT a, b + FROM t1 FULL JOIN t2 ON true + ORDER BY coalesce(a,b,3); +} { + 1 - +} +do_execsql_test joinE-37 { + SELECT a, b + FROM t1 FULL JOIN t2 ON true WHERE a IS NULL + ORDER BY coalesce(a,b,3); +} { +} + +# PG-14 is unable +# +# do_execsql_test joinE-38 { +# SELECT a, b +# FROM t1 FULL JOIN t2 ON a IS NULL +# ORDER BY coalesce(a,b,3); +# } { +# } + +do_execsql_test joinE-39 { + SELECT a, b + FROM t1 FULL JOIN t2 ON true WHERE b IS NULL + ORDER BY coalesce(a,b,3); +} { + 1 - +} + +# PG-14 is unable +# do_execsql_test joinE-40 { +# SELECT a, b +# FROM t1 FULL JOIN t2 ON b IS NULL +# ORDER BY coalesce(a,b,3); +# } { +# } + +finish_test + +############################################################################## +# This is the PG-14 test script generator +# +# puts " +# \\pset border off +# \\pset tuples_only on +# \\pset null - +# +# DROP TABLE IF EXISTS t1; +# DROP TABLE IF EXISTS t2; +# CREATE TABLE t1(a INT); +# INSERT INTO t1 VALUES(1),(NULL); +# CREATE TABLE t2(b INT); +# INSERT INTO t2 VALUES(2),(NULL); +# " +# +# proc echo {prefix txt} { +# regsub -all {\n} $txt \n$prefix txt +# puts "$prefix$txt" +# } +# +# set n 0 +# set k 0 +# foreach j1 {INNER LEFT RIGHT FULL} { +# foreach on1 { +# true +# {true WHERE a IS NULL} +# {a IS NULL} +# {true WHERE b IS NULL} +# {b IS NULL} +# } { +# +# incr n +# incr k +# set q1 "" +# append q1 "SELECT a, b\n" +# append q1 " FROM t1 $j1 JOIN t2 ON $on1\n" +# append q1 " ORDER BY coalesce(a,b,3);" +# +# echo "\\qecho " "do_execsql_test joinE-$n \{" +# echo "\\qecho X " $q1 +# echo "\\qecho " "\} \{" +# puts $q1 +# echo "\\qecho " "\}" +# +# } +# } +# +# puts " +# DELETE FROM t1; +# INSERT INTO t1 VALUES(1); +# DELETE FROM t2; +# INSERT INTO t2 VALUES(NULL); +# " +# +# foreach j1 {INNER LEFT RIGHT FULL} { +# foreach on1 { +# true +# {true WHERE a IS NULL} +# {a IS NULL} +# {true WHERE b IS NULL} +# {b IS NULL} +# } { +# +# incr n +# incr k +# set q1 "" +# append q1 "SELECT a, b\n" +# append q1 " FROM t1 $j1 JOIN t2 ON $on1\n" +# append q1 " ORDER BY coalesce(a,b,3);" +# +# echo "\\qecho " "do_execsql_test joinE-$n \{" +# echo "\\qecho X " $q1 +# echo "\\qecho " "\} \{" +# puts $q1 +# echo "\\qecho " "\}" +# +# } +# } diff --git a/test/joinF.test b/test/joinF.test new file mode 100644 index 0000000000..0848e37d14 --- /dev/null +++ b/test/joinF.test @@ -0,0 +1,613 @@ +# 2022-05-31 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# This file implements tests for JOINs +# +# The test case output is (mostly) all generated by PostgreSQL 14. This +# test module was created as follows: +# +# 1. Run a TCL script (included at the bottom of this file) that +# generates an input script for "psql" that will run man +# diverse tests on joins. +# +# 2. Run the script from step (1) through psql and collect the +# output. +# +# 3. Make a few minor global search-and-replace operations to convert +# the psql output into a form suitable for this test module. +# +# 4. Add this header, and the script content at the footer. +# +# A few extra tests that were not generated from postgresql output are +# added at the end. +# +set testdir [file dirname $argv0] +source $testdir/tester.tcl +db nullvalue - +db eval { + CREATE TABLE t1(x INT); + CREATE TABLE t2(y INT); + CREATE TABLE t3(z INT); + CREATE TABLE t4(w INT); + INSERT INTO t1 VALUES(10); + INSERT INTO t3 VALUES(20),(30); + INSERT INTO t4 VALUES(50); +} +do_execsql_test joinF-1 { + SELECT * + FROM t1 INNER JOIN t2 ON true + INNER JOIN t3 ON t2.y IS NOT NULL + INNER JOIN t4 ON true + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { +} +do_execsql_test joinF-2 { + SELECT * + FROM t1 INNER JOIN t2 ON true + INNER JOIN t3 ON t2.y IS NOT NULL + INNER JOIN t4 ON true + WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { +} +do_execsql_test joinF-3 { + SELECT * + FROM t1 INNER JOIN t2 ON true + INNER JOIN t3 ON t2.y IS NOT NULL + LEFT JOIN t4 ON true + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { +} +do_execsql_test joinF-4 { + SELECT * + FROM t1 INNER JOIN t2 ON true + INNER JOIN t3 ON t2.y IS NOT NULL + LEFT JOIN t4 ON true + WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { +} +do_execsql_test joinF-5 { + SELECT * + FROM t1 INNER JOIN t2 ON true + INNER JOIN t3 ON t2.y IS NOT NULL + RIGHT JOIN t4 ON true + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { + - - - 50 +} +do_execsql_test joinF-6 { + SELECT * + FROM t1 INNER JOIN t2 ON true + INNER JOIN t3 ON t2.y IS NOT NULL + RIGHT JOIN t4 ON true + WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { +} +do_execsql_test joinF-7 { + SELECT * + FROM t1 INNER JOIN t2 ON true + LEFT JOIN t3 ON t2.y IS NOT NULL + INNER JOIN t4 ON true + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { +} +do_execsql_test joinF-8 { + SELECT * + FROM t1 INNER JOIN t2 ON true + LEFT JOIN t3 ON t2.y IS NOT NULL + INNER JOIN t4 ON true + WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { +} +do_execsql_test joinF-9 { + SELECT * + FROM t1 INNER JOIN t2 ON true + LEFT JOIN t3 ON t2.y IS NOT NULL + LEFT JOIN t4 ON true + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { +} +do_execsql_test joinF-10 { + SELECT * + FROM t1 INNER JOIN t2 ON true + LEFT JOIN t3 ON t2.y IS NOT NULL + LEFT JOIN t4 ON true + WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { +} +do_execsql_test joinF-11 { + SELECT * + FROM t1 INNER JOIN t2 ON true + LEFT JOIN t3 ON t2.y IS NOT NULL + RIGHT JOIN t4 ON true + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { + - - - 50 +} +do_execsql_test joinF-12 { + SELECT * + FROM t1 INNER JOIN t2 ON true + LEFT JOIN t3 ON t2.y IS NOT NULL + RIGHT JOIN t4 ON true + WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { +} +do_execsql_test joinF-13 { + SELECT * + FROM t1 INNER JOIN t2 ON true + RIGHT JOIN t3 ON t2.y IS NOT NULL + INNER JOIN t4 ON true + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { + - - 20 50 + - - 30 50 +} +do_execsql_test joinF-14 { + SELECT * + FROM t1 INNER JOIN t2 ON true + RIGHT JOIN t3 ON t2.y IS NOT NULL + INNER JOIN t4 ON true + WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { + - - 20 50 + - - 30 50 +} +do_execsql_test joinF-15 { + SELECT * + FROM t1 INNER JOIN t2 ON true + RIGHT JOIN t3 ON t2.y IS NOT NULL + LEFT JOIN t4 ON true + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { + - - 20 50 + - - 30 50 +} +do_execsql_test joinF-16 { + SELECT * + FROM t1 INNER JOIN t2 ON true + RIGHT JOIN t3 ON t2.y IS NOT NULL + LEFT JOIN t4 ON true + WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { + - - 20 50 + - - 30 50 +} +do_execsql_test joinF-17 { + SELECT * + FROM t1 INNER JOIN t2 ON true + RIGHT JOIN t3 ON t2.y IS NOT NULL + RIGHT JOIN t4 ON true + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { + - - 20 50 + - - 30 50 +} +do_execsql_test joinF-18 { + SELECT * + FROM t1 INNER JOIN t2 ON true + RIGHT JOIN t3 ON t2.y IS NOT NULL + RIGHT JOIN t4 ON true + WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { + - - 20 50 + - - 30 50 +} +do_execsql_test joinF-19 { + SELECT * + FROM t1 LEFT JOIN t2 ON true + INNER JOIN t3 ON t2.y IS NOT NULL + INNER JOIN t4 ON true + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { +} +do_execsql_test joinF-20 { + SELECT * + FROM t1 LEFT JOIN t2 ON true + INNER JOIN t3 ON t2.y IS NOT NULL + INNER JOIN t4 ON true + WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { +} +do_execsql_test joinF-21 { + SELECT * + FROM t1 LEFT JOIN t2 ON true + INNER JOIN t3 ON t2.y IS NOT NULL + LEFT JOIN t4 ON true + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { +} +do_execsql_test joinF-22 { + SELECT * + FROM t1 LEFT JOIN t2 ON true + INNER JOIN t3 ON t2.y IS NOT NULL + LEFT JOIN t4 ON true + WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { +} +do_execsql_test joinF-23 { + SELECT * + FROM t1 LEFT JOIN t2 ON true + INNER JOIN t3 ON t2.y IS NOT NULL + RIGHT JOIN t4 ON true + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { + - - - 50 +} +do_execsql_test joinF-24 { + SELECT * + FROM t1 LEFT JOIN t2 ON true + INNER JOIN t3 ON t2.y IS NOT NULL + RIGHT JOIN t4 ON true + WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { +} +do_execsql_test joinF-25 { + SELECT * + FROM t1 LEFT JOIN t2 ON true + LEFT JOIN t3 ON t2.y IS NOT NULL + INNER JOIN t4 ON true + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { + 10 - - 50 +} +do_execsql_test joinF-26 { + SELECT * + FROM t1 LEFT JOIN t2 ON true + LEFT JOIN t3 ON t2.y IS NOT NULL + INNER JOIN t4 ON true + WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { +} +do_execsql_test joinF-27 { + SELECT * + FROM t1 LEFT JOIN t2 ON true + LEFT JOIN t3 ON t2.y IS NOT NULL + LEFT JOIN t4 ON true + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { + 10 - - 50 +} +do_execsql_test joinF-28 { + SELECT * + FROM t1 LEFT JOIN t2 ON true + LEFT JOIN t3 ON t2.y IS NOT NULL + LEFT JOIN t4 ON true + WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { +} +do_execsql_test joinF-29 { + SELECT * + FROM t1 LEFT JOIN t2 ON true + LEFT JOIN t3 ON t2.y IS NOT NULL + RIGHT JOIN t4 ON true + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { + 10 - - 50 +} +do_execsql_test joinF-30 { + SELECT * + FROM t1 LEFT JOIN t2 ON true + LEFT JOIN t3 ON t2.y IS NOT NULL + RIGHT JOIN t4 ON true + WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { +} +do_execsql_test joinF-31 { + SELECT * + FROM t1 LEFT JOIN t2 ON true + RIGHT JOIN t3 ON t2.y IS NOT NULL + INNER JOIN t4 ON true + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { + - - 20 50 + - - 30 50 +} +do_execsql_test joinF-32 { + SELECT * + FROM t1 LEFT JOIN t2 ON true + RIGHT JOIN t3 ON t2.y IS NOT NULL + INNER JOIN t4 ON true + WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { + - - 20 50 + - - 30 50 +} +do_execsql_test joinF-33 { + SELECT * + FROM t1 LEFT JOIN t2 ON true + RIGHT JOIN t3 ON t2.y IS NOT NULL + LEFT JOIN t4 ON true + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { + - - 20 50 + - - 30 50 +} +do_execsql_test joinF-34 { + SELECT * + FROM t1 LEFT JOIN t2 ON true + RIGHT JOIN t3 ON t2.y IS NOT NULL + LEFT JOIN t4 ON true + WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { + - - 20 50 + - - 30 50 +} +do_execsql_test joinF-35 { + SELECT * + FROM t1 LEFT JOIN t2 ON true + RIGHT JOIN t3 ON t2.y IS NOT NULL + RIGHT JOIN t4 ON true + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { + - - 20 50 + - - 30 50 +} +do_execsql_test joinF-36 { + SELECT * + FROM t1 LEFT JOIN t2 ON true + RIGHT JOIN t3 ON t2.y IS NOT NULL + RIGHT JOIN t4 ON true + WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { + - - 20 50 + - - 30 50 +} +do_execsql_test joinF-37 { + SELECT * + FROM t1 RIGHT JOIN t2 ON true + INNER JOIN t3 ON t2.y IS NOT NULL + INNER JOIN t4 ON true + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { +} +do_execsql_test joinF-38 { + SELECT * + FROM t1 RIGHT JOIN t2 ON true + INNER JOIN t3 ON t2.y IS NOT NULL + INNER JOIN t4 ON true + WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { +} +do_execsql_test joinF-39 { + SELECT * + FROM t1 RIGHT JOIN t2 ON true + INNER JOIN t3 ON t2.y IS NOT NULL + LEFT JOIN t4 ON true + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { +} +do_execsql_test joinF-40 { + SELECT * + FROM t1 RIGHT JOIN t2 ON true + INNER JOIN t3 ON t2.y IS NOT NULL + LEFT JOIN t4 ON true + WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { +} +do_execsql_test joinF-41 { + SELECT * + FROM t1 RIGHT JOIN t2 ON true + INNER JOIN t3 ON t2.y IS NOT NULL + RIGHT JOIN t4 ON true + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { + - - - 50 +} +do_execsql_test joinF-42 { + SELECT * + FROM t1 RIGHT JOIN t2 ON true + INNER JOIN t3 ON t2.y IS NOT NULL + RIGHT JOIN t4 ON true + WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { +} +do_execsql_test joinF-43 { + SELECT * + FROM t1 RIGHT JOIN t2 ON true + LEFT JOIN t3 ON t2.y IS NOT NULL + INNER JOIN t4 ON true + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { +} +do_execsql_test joinF-44 { + SELECT * + FROM t1 RIGHT JOIN t2 ON true + LEFT JOIN t3 ON t2.y IS NOT NULL + INNER JOIN t4 ON true + WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { +} +do_execsql_test joinF-45 { + SELECT * + FROM t1 RIGHT JOIN t2 ON true + LEFT JOIN t3 ON t2.y IS NOT NULL + LEFT JOIN t4 ON true + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { +} +do_execsql_test joinF-46 { + SELECT * + FROM t1 RIGHT JOIN t2 ON true + LEFT JOIN t3 ON t2.y IS NOT NULL + LEFT JOIN t4 ON true + WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { +} +do_execsql_test joinF-47 { + SELECT * + FROM t1 RIGHT JOIN t2 ON true + LEFT JOIN t3 ON t2.y IS NOT NULL + RIGHT JOIN t4 ON true + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { + - - - 50 +} +do_execsql_test joinF-48 { + SELECT * + FROM t1 RIGHT JOIN t2 ON true + LEFT JOIN t3 ON t2.y IS NOT NULL + RIGHT JOIN t4 ON true + WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { +} +do_execsql_test joinF-49 { + SELECT * + FROM t1 RIGHT JOIN t2 ON true + RIGHT JOIN t3 ON t2.y IS NOT NULL + INNER JOIN t4 ON true + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { + - - 20 50 + - - 30 50 +} +do_execsql_test joinF-50 { + SELECT * + FROM t1 RIGHT JOIN t2 ON true + RIGHT JOIN t3 ON t2.y IS NOT NULL + INNER JOIN t4 ON true + WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { + - - 20 50 + - - 30 50 +} +do_execsql_test joinF-51 { + SELECT * + FROM t1 RIGHT JOIN t2 ON true + RIGHT JOIN t3 ON t2.y IS NOT NULL + LEFT JOIN t4 ON true + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { + - - 20 50 + - - 30 50 +} +do_execsql_test joinF-52 { + SELECT * + FROM t1 RIGHT JOIN t2 ON true + RIGHT JOIN t3 ON t2.y IS NOT NULL + LEFT JOIN t4 ON true + WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { + - - 20 50 + - - 30 50 +} +do_execsql_test joinF-53 { + SELECT * + FROM t1 RIGHT JOIN t2 ON true + RIGHT JOIN t3 ON t2.y IS NOT NULL + RIGHT JOIN t4 ON true + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { + - - 20 50 + - - 30 50 +} +do_execsql_test joinF-54 { + SELECT * + FROM t1 RIGHT JOIN t2 ON true + RIGHT JOIN t3 ON t2.y IS NOT NULL + RIGHT JOIN t4 ON true + WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600) + ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0); +} { + - - 20 50 + - - 30 50 +} +finish_test + +############################################################################ +# This is the TCL script used to generate the psql script that generated +# the data above. +# +# puts " +# \\pset border off +# \\pset tuples_only on +# \\pset null - +# +# DROP TABLE IF EXISTS t1; +# DROP TABLE IF EXISTS t2; +# DROP TABLE IF EXISTS t3; +# DROP TABLE IF EXISTS t4; +# CREATE TABLE t1(x INT); +# CREATE TABLE t2(y INT); +# CREATE TABLE t3(z INT); +# CREATE TABLE t4(w INT); +# INSERT INTO t1 VALUES(10); +# INSERT INTO t3 VALUES(20),(30); +# INSERT INTO t4 VALUES(50); +# " +# +# proc echo {prefix txt} { +# regsub -all {\n} $txt \n$prefix txt +# puts "$prefix$txt" +# } +# +# set n 0 +# foreach j1 {INNER LEFT RIGHT} { +# foreach j2 {INNER LEFT RIGHT} { +# foreach j3 {INNER LEFT RIGHT} { +# +# incr n +# set q1 "" +# append q1 "SELECT *\n" +# append q1 " FROM t1 $j1 JOIN t2 ON true\n" +# append q1 " $j2 JOIN t3 ON t2.y IS NOT NULL\n" +# append q1 " $j3 JOIN t4 ON true\n" +# append q1 " ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);" +# +# echo "\\qecho " "do_execsql_test joinF-$n \{" +# echo "\\qecho X " $q1 +# echo "\\qecho " "\} \{" +# puts $q1 +# echo "\\qecho " "\}" +# +# incr n +# set q1 "" +# append q1 "SELECT *\n" +# append q1 " FROM t1 $j1 JOIN t2 ON true\n" +# append q1 " $j2 JOIN t3 ON t2.y IS NOT NULL\n" +# append q1 " $j3 JOIN t4 ON true\n" +# append q1 " WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600)\n" +# append q1 " ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);" +# +# echo "\\qecho " "do_execsql_test joinF-$n \{" +# echo "\\qecho X " $q1 +# echo "\\qecho " "\} \{" +# puts $q1 +# echo "\\qecho " "\}" +# +# } +# } +# } +# diff --git a/test/joinH.test b/test/joinH.test new file mode 100644 index 0000000000..56fa9c7ec3 --- /dev/null +++ b/test/joinH.test @@ -0,0 +1,477 @@ +# 2022 May 17 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix joinH + +do_execsql_test 1.0 { + CREATE TABLE t1(a INT); + CREATE TABLE t2(b INT); + INSERT INTO t2(b) VALUES(NULL); +} + +db nullvalue NULL + +do_execsql_test 1.1 { + SELECT DISTINCT a FROM t1 FULL JOIN t2 ON true WHERE (b ISNULL); +} {NULL} +do_execsql_test 1.2 { + SELECT a FROM t1 FULL JOIN t2 ON true; +} {NULL} +do_execsql_test 1.3 { + SELECT a FROM t1 FULL JOIN t2 ON true WHERE (b ISNULL); +} {NULL} +do_execsql_test 1.4 { + SELECT DISTINCT a FROM t1 FULL JOIN t2 ON true; +} {NULL} + +#----------------------------------------------------------- + +reset_db +do_execsql_test 2.0 { + CREATE TABLE r3(x); + CREATE TABLE r4(y INTEGER PRIMARY KEY); + INSERT INTO r4 VALUES(55); +} + +do_execsql_test 2.1 { + SELECT 'value!' FROM r3 FULL JOIN r4 ON (y=x); +} {value!} + +do_execsql_test 2.2 { + SELECT 'value!' FROM r3 FULL JOIN r4 ON (y=x) WHERE +y=55; +} {value!} + +#----------------------------------------------------------- +reset_db +do_execsql_test 3.1 { + CREATE TABLE t0 (c0); + CREATE TABLE t1 (c0); + CREATE TABLE t2 (c0 , c1 , c2 , UNIQUE (c0), UNIQUE (c2 DESC)); + INSERT INTO t2 VALUES ('x', 'y', 'z'); + ANALYZE; + CREATE VIEW v0(c0) AS SELECT FALSE; +} + +do_catchsql_test 3.2 { + SELECT * FROM t0 LEFT OUTER JOIN t1 ON v0.c0 INNER JOIN v0 INNER JOIN t2 ON (t2.c2 NOT NULL); +} {1 {ON clause references tables to its right}} + +#------------------------------------------------------------- + +reset_db +do_execsql_test 4.1 { + CREATE TABLE t1(a,b,c,d,e,f,g,h,PRIMARY KEY(a,b,c)) WITHOUT ROWID; + CREATE TABLE t2(i, j); + INSERT INTO t2 VALUES(10, 20); +} + +do_execsql_test 4.2 { + SELECT (d IS NULL) FROM t1 RIGHT JOIN t2 ON (j=33); +} {1} + +do_execsql_test 4.3 { + CREATE INDEX i1 ON t1( (d IS NULL), d ); +} + +do_execsql_test 4.4 { + SELECT (d IS NULL) FROM t1 RIGHT JOIN t2 ON (j=33); +} {1} + +#------------------------------------------------------------------------- +# +reset_db +do_execsql_test 5.0 { + CREATE TABLE t0(w); + CREATE TABLE t1(x); + CREATE TABLE t2(y); + CREATE TABLE t3(z); + INSERT INTO t3 VALUES('t3val'); +} + +do_execsql_test 5.1 { + SELECT * FROM t1 INNER JOIN t2 ON (0) RIGHT OUTER JOIN t3; +} {{} {} t3val} + +do_execsql_test 5.2 { + SELECT * FROM t1 INNER JOIN t2 ON (0) FULL OUTER JOIN t3; +} {{} {} t3val} + +do_execsql_test 5.3 { + SELECT * FROM t3 LEFT JOIN t2 ON (0); +} {t3val {}} + +do_execsql_test 5.4 { + SELECT * FROM t0 RIGHT JOIN t1 INNER JOIN t2 ON (0) RIGHT JOIN t3 +} {{} {} {} t3val} + +do_execsql_test 5.5 { + SELECT * FROM t0 RIGHT JOIN t1 INNER JOIN t2 ON (0) +} {} + + +reset_db +db null NULL +do_execsql_test 6.0 { + CREATE TABLE t1(a INT); + CREATE TABLE t2(b INT); + INSERT INTO t1 VALUES(3); + SELECT CASE WHEN t2.b THEN 0 ELSE 1 END FROM t1 LEFT JOIN t2 ON true; +} {1} +do_execsql_test 6.1 { + SELECT * FROM t1 LEFT JOIN t2 ON true WHERE CASE WHEN t2.b THEN 0 ELSE 1 END; +} {3 NULL} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 7.0 { + CREATE TABLE t1(a, b); + CREATE TABLE t2(c); + CREATE TABLE t3(d); + + INSERT INTO t1 VALUES ('a', 'a'); + INSERT INTO t2 VALUES ('ddd'); + INSERT INTO t3 VALUES(1234); +} + +do_execsql_test 7.1 { + SELECT t2.rowid FROM t1 JOIN (t2 JOIN t3); +} {1} + +do_execsql_test 7.1 { + UPDATE t1 SET b = t2.rowid FROM t2, t3; +} + +do_execsql_test 7.2 { + SELECT * FROM t1 +} {a 1} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 8.0 { + CREATE TABLE x1(a INTEGER PRIMARY KEY, b); + CREATE TABLE x2(c, d); + CREATE TABLE x3(rowid, _rowid_); + + CREATE TABLE x4(rowid, _rowid_, oid); + + INSERT INTO x1 VALUES(1000, 'thousand'); + INSERT INTO x2 VALUES('c', 'd'); + INSERT INTO x3(oid, rowid, _rowid_) VALUES(43, 'hello', 'world'); + INSERT INTO x4(oid, rowid, _rowid_) VALUES('forty three', 'hello', 'world'); +} + +do_execsql_test 8.1 { + SELECT x3.oid FROM x1 JOIN (x2 JOIN x3 ON c='c') +} 43 + +breakpoint +do_execsql_test 8.2 { + SELECT x3.rowid FROM x1 JOIN (x2 JOIN x3 ON c='c') +} {hello} + +do_execsql_test 8.3 { + SELECT x4.oid FROM x1 JOIN (x2 JOIN x4 ON c='c') +} {{forty three}} + + +#--------------------------------------------------------------------- +# +reset_db +do_execsql_test 9.0 { + CREATE TABLE x1(a); + CREATE TABLE x2(b); + CREATE TABLE x3(c); + + CREATE TABLE wo1(a PRIMARY KEY, b) WITHOUT ROWID; + CREATE TABLE wo2(a PRIMARY KEY, rowid) WITHOUT ROWID; + CREATE TABLE wo3(a PRIMARY KEY, b) WITHOUT ROWID; +} + +do_catchsql_test 9.1 { + SELECT rowid FROM wo1, x1, x2; +} {1 {ambiguous column name: rowid}} +do_catchsql_test 9.2 { + SELECT rowid FROM wo1, (x1, x2); +} {1 {ambiguous column name: rowid}} +do_catchsql_test 9.3 { + SELECT rowid FROM wo1 JOIN (x1 JOIN x2); +} {1 {ambiguous column name: rowid}} +do_catchsql_test 9.4 { + SELECT a FROM wo1, x1, x2; +} {1 {ambiguous column name: a}} + + +# It is not possible to use "rowid" in a USING clause. +# +do_catchsql_test 9.5 { + SELECT * FROM x1 JOIN x2 USING (rowid); +} {1 {cannot join using column rowid - column not present in both tables}} +do_catchsql_test 9.6 { + SELECT * FROM wo2 JOIN x2 USING (rowid); +} {1 {cannot join using column rowid - column not present in both tables}} + +# "rowid" columns are not matched by NATURAL JOIN. If they were, then +# the SELECT below would return zero rows. +do_execsql_test 9.7 { + INSERT INTO x1(rowid, a) VALUES(101, 'A'); + INSERT INTO x2(rowid, b) VALUES(55, 'B'); + SELECT * FROM x1 NATURAL JOIN x2; +} {A B} + +do_execsql_test 9.8 { + INSERT INTO wo1(a, b) VALUES('mya', 'myb'); + INSERT INTO wo2(a, rowid) VALUES('mypk', 'myrowid'); + INSERT INTO wo3(a, b) VALUES('MYA', 'MYB'); + INSERT INTO x3(rowid, c) VALUES(99, 'x3B'); +} + +do_catchsql_test 9.8 { + SELECT rowid FROM x1 JOIN (x2 JOIN wo2); +} {0 myrowid} +do_catchsql_test 9.9 { + SELECT _rowid_ FROM wo1 JOIN (wo3 JOIN x3) +} {0 99} +do_catchsql_test 9.10 { + SELECT oid FROM wo1 JOIN (wo3 JOIN x3) +} {0 99} +do_catchsql_test 9.11 { + SELECT oid FROM wo2 JOIN (wo3 JOIN x3) +} {0 99} + +reset_db +do_execsql_test 10.0 { + CREATE TABLE rt0 (c0 INTEGER, c1 INTEGER, c2 INTEGER, c3 INTEGER, c4 INTEGER); + CREATE TABLE rt3 (c3 INTEGER); + + INSERT INTO rt0(c3, c1) VALUES (x'', '1'); + INSERT INTO rt0(c3, c1) VALUES ('-1', -1e500); + INSERT INTO rt0(c3, c1) VALUES (1, x''); + + CREATE VIEW v6(c0, c1, c2) AS SELECT 0, 0, 0; +} + +do_execsql_test 10.1 { + SELECT COUNT(*) FROM rt0 LEFT JOIN rt3 JOIN v6 ON ((CASE v6.c0 WHEN rt0.c4 THEN rt3.c3 END) NOT BETWEEN (rt0.c4) AND (NULL)) WHERE (rt0.c1); -- 2 +} {0} + +do_execsql_test 10.2 { + SELECT COUNT(*) FROM rt0 LEFT JOIN rt3 RIGHT OUTER JOIN v6 ON ((CASE v6.c0 WHEN rt0.c4 THEN rt3.c3 END) NOT BETWEEN (rt0.c4) AND (NULL)) WHERE (rt0.c1); -- 2 +} {0} + +#------------------------------------------------------------------------- + +do_execsql_test 11.1 { + CREATE TABLE t1(a, b); + CREATE TABLE t2(c, d); + CREATE TABLE t3(e, f); + + INSERT INTO t1 VALUES(1, 1); + INSERT INTO t2 VALUES(2, 2); + INSERT INTO t3 VALUES(3, 3); +} + +do_execsql_test 11.2 { + SELECT * FROM t1 LEFT JOIN t2 RIGHT JOIN t3 ON (t2.c=10) +} {{} {} {} {} 3 3} + +do_execsql_test 11.3 { + SELECT * FROM t1 LEFT JOIN t2 RIGHT JOIN t3 ON (t2.c=10) WHERE t1.a=1 +} {} + +#------------------------------------------------------------------------- +reset_db + +do_execsql_test 12.1 { + CREATE TABLE t1(a1 INT, b1 TEXT); + INSERT INTO t1 VALUES(88,''); + CREATE TABLE t2(c2 INT, d2 TEXT); + INSERT INTO t2 VALUES(88,''); + CREATE TABLE t3(e3 TEXT PRIMARY KEY); + INSERT INTO t3 VALUES(''); +} + +do_execsql_test 12.2 { + SELECT * FROM t1 LEFT JOIN t2 ON true RIGHT JOIN t3 ON d2=e3 WHERE c2 BETWEEN NULL AND a1; +} +do_execsql_test 12.3 { + SELECT * FROM t1 LEFT JOIN t2 ON true RIGHT JOIN t3 ON d2=e3 WHERE c2 BETWEEN NULL AND a1; +} + +#------------------------------------------------------------------------- +# 2024-04-05 dbsqlfuzz b9e65e2f110df998f1306571fae7af6c01e4d92b +reset_db +do_execsql_test 13.1 { + CREATE TABLE t1(a INT AS (b), b INT); + INSERT INTO t1(b) VALUES(123); + CREATE TABLE t2(a INT, c INT); + SELECT a FROM t2 NATURAL RIGHT JOIN t1; +} {123} +do_execsql_test 13.2 { + CREATE INDEX t1a ON t1(a); + SELECT a FROM t2 NATURAL RIGHT JOIN t1; +} {123} +# Further tests of the same logic (indexes on expressions +# used by RIGHT JOIN) from check-in ffe23af73fcb324d and +# forum post https://sqlite.org/forum/forumpost/9b491e1debf0b67a. +db null NULL +do_execsql_test 13.3 { + CREATE TABLE t3(a INT, b INT); + CREATE UNIQUE INDEX t3x ON t3(a, a+b); + INSERT INTO t3(a,b) VALUES(1,2),(4,8),(16,32),(4,80),(1,-300); + CREATE TABLE t4(x INT, y INT); + INSERT INTO t4(x,y) SELECT a, b FROM t3; + INSERT INTO t4(x,y) VALUES(99,99); + SELECT a1.a, sum( a1.a+a1.b ) FROM t3 AS a1 RIGHT JOIN t4 ON a=x + GROUP BY a1.a ORDER BY 1; +} {NULL NULL 1 -592 4 192 16 48} +do_execsql_test 13.4 { + SELECT sum( a1.a+a1.b ) FROM t3 AS a1 RIGHT JOIN t3 ON true + GROUP BY a1.a ORDER BY 1; +} {-1480 240 480} + +#------------------------------------------------------------------------- +# 2025-05-30 +# https://sqlite.org/forum/forumpost/5028c785b6 +# +reset_db + +do_execsql_test 14.0 { + CREATE TABLE t1(c0 INT); + CREATE TABLE t2(c0 BLOB); + CREATE TABLE t3(c0 BLOB); + CREATE TABLE t4(c4 BLOB); + INSERT INTO t1(c0) VALUES(0); + INSERT INTO t3(c0) VALUES('0'); +} + +do_execsql_test 14.1.1 { + SELECT * FROM t1 NATURAL LEFT JOIN t2 NATURAL JOIN t3; +} {0} + +do_execsql_test 14.1.2 { + SELECT * FROM t1 NATURAL LEFT JOIN t2 NATURAL JOIN t3 FULL JOIN t4 ON true; +} {0 {}} + +do_execsql_test 14.1.3 { + SELECT * FROM (t1 NATURAL LEFT JOIN t2 NATURAL JOIN t3) FULL JOIN t4 ON true; +} {0 {}} + +do_execsql_test 14.1.4 { + SELECT * + FROM (t1 NATURAL LEFT JOIN t2 NATURAL JOIN t3) AS qq FULL JOIN t4 ON true; +} {0 {}} + +do_execsql_test 14.2.1 { + SELECT * FROM t3 NATURAL LEFT JOIN t2 NATURAL JOIN t1; +} {0} + +do_execsql_test 14.2.2 { + SELECT * FROM t3 NATURAL LEFT JOIN t2 NATURAL JOIN t1 FULL JOIN t4 ON true; +} {0 {}} + +do_execsql_test 14.2.3 { + SELECT * FROM (t3 NATURAL LEFT JOIN t2 NATURAL JOIN t1) FULL JOIN t4 ON true; +} {0 {}} + +do_execsql_test 14.2.4 { + SELECT * + FROM (t3 NATURAL LEFT JOIN t2 NATURAL JOIN t1) AS qq FULL JOIN t4 ON true; +} {0 {}} + +# 2025-06-01 +# +reset_db +do_execsql_test 15.1 { + CREATE TABLE t0(c0); + CREATE TABLE t1(c0); + CREATE TABLE t2(c0); + INSERT INTO t0 VALUES ('1.0'); + INSERT INTO t2(c0) VALUES (9); + SELECT t0.c0,t2.c0 FROM (SELECT CAST(t0.c0 as REAL) AS c0 FROM t0) as subquery NATURAL LEFT JOIN t1 NATURAL JOIN t0 RIGHT JOIN t2 ON 1; +} {1.0 9} +do_execsql_test 15.2 { + CREATE TABLE x1(x COLLATE nocase); + CREATE TABLE x2(x); + CREATE TABLE x3(x); + CREATE TABLE t4(y); + INSERT INTO x1 VALUES('ABC'); + INSERT INTO x3 VALUES('abc'); + SELECT lower(x), quote(y) FROM x1 LEFT JOIN x2 USING (x) JOIN x3 USING (x) FULL JOIN t4; +} {abc NULL} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 16.0 { + CREATE TABLE t0_a (c0 INT); + CREATE TABLE t0_b (c0 INT); + + CREATE TABLE t2 (c0 INT); + INSERT INTO t2 VALUES (1); +} + +do_execsql_test 16.1 { + SELECT * FROM t2 LEFT JOIN t0_b +} {1 {}} + +do_execsql_test 16.2.1 { + SELECT * FROM (t0_a RIGHT JOIN ( t2 LEFT JOIN t0_b)) +} {{} 1 {}} +do_execsql_test 16.2.2 { + SELECT * FROM (t0_a RIGHT JOIN (SELECT * FROM t2 LEFT JOIN t0_b)) +} {{} 1 {}} + + +do_catchsql_test 16.3.1 { + SELECT * FROM (t0_a RIGHT JOIN ( t2 LEFT JOIN t0_b) USING (c0)); +} {1 {ambiguous column name: c0}} + +do_execsql_test 16.3.2 { + SELECT * FROM (t0_a RIGHT JOIN (SELECT * FROM t2 LEFT JOIN t0_b) USING (c0)); +} {1 {}} + +do_execsql_test 16.4.0 { + CREATE TABLE x0(a TEXT); + CREATE TABLE x1(a TEXT); + CREATE TABLE x2(a TEXT); + + INSERT INTO x1 VALUES('blue'); + INSERT INTO x2 VALUES('red'); +} + +do_catchsql_test 16.4.1 { + SELECT * FROM x0 RIGHT JOIN ( x1, x2) USING (a) +} {1 {ambiguous column name: a}} + +do_execsql_test 16.4.2 { + SELECT * FROM x0 RIGHT JOIN (SELECT * FROM x1, x2) USING (a) +} {blue red} + +reset_db +do_execsql_test 16.5.0 { + CREATE TABLE t0 (c0 INT); + CREATE TABLE t1 (c0 INT); + CREATE TABLE t2 (c0 INT); + + INSERT INTO t1(c0) VALUES (NULL); + INSERT INTO t2 VALUES (1); + + CREATE VIEW v0(c0) AS SELECT 1 AS col_0 FROM t0; +} + +do_catchsql_test 16.5.2 { + SELECT * FROM (t0 NATURAL RIGHT JOIN (t0 FULL JOIN (v0 NATURAL FULL JOIN t2) ON TRUE)) NATURAL FULL JOIN t1; +} {1 {ambiguous column name: c0}} + +finish_test diff --git a/test/joinI.test b/test/joinI.test new file mode 100644 index 0000000000..577ca4c2c3 --- /dev/null +++ b/test/joinI.test @@ -0,0 +1,125 @@ +# 2022 May 17 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix joinI + +do_execsql_test 1.0 { + CREATE TABLE t1(a INT); + CREATE TABLE t2(b INT); + CREATE TABLE t3(c INT); +} + +foreach {tn sql} { + 1 "SELECT * FROM t1 RIGHT JOIN t2 ON t2.b=t3.c CROSS JOIN t3" + 2 "SELECT * FROM t1 RIGHT JOIN t2 ON t2.b=(SELECT t3.c) CROSS JOIN t3" + 3 "SELECT * FROM t1 RIGHT JOIN t2 ON CASE WHEN t2.b THEN t3.c ELSE 1 END CROSS JOIN t3" +} { + do_catchsql_test 1.1.$tn $sql {1 {ON clause references tables to its right}} +} + + +#------------------------------------------------------------------------- +reset_db + +do_execsql_test 2.0 { + CREATE TABLE t0(c0 INT, c1 INT); + CREATE TABLE t1 (c0 INT); + + CREATE VIEW v1(c0) AS SELECT t0.c0 FROM t0 NATURAL RIGHT JOIN t1; + CREATE VIEW v2(c0) AS SELECT 0 FROM v1; + + INSERT INTO t0(c0, c1) VALUES (-1, 0); + INSERT INTO t1(c0) VALUES (NULL); + + SELECT * FROM v1 INNER JOIN (v2 CROSS JOIN t0) ON (t0.c0 < t0.c1); +} {{} 0 -1 0} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 3.0 { + CREATE TABLE t0(c0, c1); + CREATE TABLE t1(v); + CREATE TABLE t2(w); + CREATE TABLE t3(x); + CREATE TABLE t4(y); + CREATE TABLE t5(z); +} + +do_execsql_test 3.1 { + SELECT 1234 FROM t4 + RIGHT JOIN t5 + CROSS JOIN (t2 CROSS JOIN t0) AS a1 ON (a1.c0 < a1.c1); +} + +do_execsql_test 3.2 { + SELECT 1234 FROM t4 + RIGHT JOIN t5 + CROSS JOIN (t2 CROSS JOIN t1 CROSS JOIN t0) AS a1 ON (a1.c0 < a1.c1); +} + +do_execsql_test 3.3 { + SELECT 5678 FROM t0 RIGHT JOIN t1 ON ( + SELECT 1 FROM t2 RIGHT JOIN t3 ON t2.w + ) CROSS JOIN t4; +} + +do_catchsql_test 3.4 { + SELECT 5678 FROM t0 RIGHT JOIN t1 ON ( + SELECT 1 FROM t2 RIGHT JOIN t3 ON t4.y + ) CROSS JOIN t4; +} {1 {ON clause references tables to its right}} + +do_execsql_test 3.5 { + SELECT 5678 FROM t0 RIGHT JOIN t1 ON ( + SELECT 1 FROM t2 RIGHT JOIN t3 ON 0=t1.v + ) CROSS JOIN t4; +} + +do_catchsql_test 3.6 { + SELECT 5678 FROM t0 RIGHT JOIN t1 ON ( + SELECT 1 FROM t2 RIGHT JOIN t3 ON max(0,t5.z) CROSS JOIN t5 + ) CROSS JOIN t4; +} {1 {ON clause references tables to its right}} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 4.0 { + CREATE TABLE t1(a); + CREATE TABLE t2(b); + CREATE TABLE t3(c, d); +} + +do_catchsql_test 4.1 { + SELECT c+d AS cd FROM t1 LEFT JOIN t2 ON (cd=5) CROSS JOIN t3; +} {1 {ON clause references tables to its right}} + + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 5.0 { + CREATE TABLE parent1(parent1key, child1key, Child2key, child3key); + CREATE TABLE child1 ( child1key NVARCHAR, value NVARCHAR ); + CREATE TABLE child2 ( child2key NVARCHAR, value NVARCHAR ); +} + +do_execsql_test 5.1 { + SELECT parent1.parent1key, child1.value, child2.value + FROM parent1 + LEFT OUTER JOIN child1 ON child1.child1key = parent1.child1key + INNER JOIN child2 ON child2.child2key = parent1.child2key; +} + +finish_test + diff --git a/test/journal1.test b/test/journal1.test index c89dd2b4c9..56c862f055 100644 --- a/test/journal1.test +++ b/test/journal1.test @@ -22,7 +22,12 @@ source $testdir/tester.tcl # These tests will not work on windows because windows uses # manditory file locking which breaks the copy_file command. # -if {$tcl_platform(platform)=="windows"} { +# Or with atomic_batch_write systems, as journal files are +# not created. +# +if {$tcl_platform(platform) eq "windows" + || [atomic_batch_write test.db] +} { finish_test return } diff --git a/test/journal2.test b/test/journal2.test index 8f9b4d0b71..7cbe0ef2c5 100644 --- a/test/journal2.test +++ b/test/journal2.test @@ -204,7 +204,7 @@ db close # delete the journal file when committing the transaction that switches # the system to WAL mode. # -ifcapable wal { +if {[wal_is_capable]} { do_test journal2-2.1 { faultsim_delete_and_reopen set ::oplog [list] diff --git a/test/journal3.test b/test/journal3.test index 939cc27c70..a29b68d54a 100644 --- a/test/journal3.test +++ b/test/journal3.test @@ -20,7 +20,9 @@ source $testdir/malloc_common.tcl # If a connection is required to create a journal file, it creates it with # the same file-system permissions as the database file itself. Test this. # -if {$::tcl_platform(platform) == "unix"} { +if {$::tcl_platform(os) ne "Windows NT" + && [atomic_batch_write test.db]==0 +} { # Changed on 2012-02-13: umask is deliberately ignored for -wal, -journal, # and -shm files. @@ -36,12 +38,16 @@ if {$::tcl_platform(platform) == "unix"} { } { db close #set effective [format %.5o [expr $permissions & ~$umask]] + set res "/[regsub {^00} $permissions {0.}]/" + if {$tcl_version>=8.7} { + regsub {^00} $permissions {0o} permissions + } set effective $permissions do_test journal3-1.2.$tn.1 { catch { forcedelete test.db-journal } file attributes test.db -permissions $permissions file attributes test.db -permissions - } $permissions + } $res do_test journal3-1.2.$tn.2 { file exists test.db-journal } {0} do_test journal3-1.2.$tn.3 { sqlite3 db test.db @@ -53,7 +59,7 @@ if {$::tcl_platform(platform) == "unix"} { } {1} do_test journal3-1.2.$tn.4 { file attr test.db-journal -perm - } $effective + } $res do_execsql_test journal3-1.2.$tn.5 { ROLLBACK } {} } diff --git a/test/jrnlmode.test b/test/jrnlmode.test index 2ba56f2b00..99823a8725 100644 --- a/test/jrnlmode.test +++ b/test/jrnlmode.test @@ -65,7 +65,16 @@ do_test jrnlmode-1.2 { PRAGMA temp.journal_mode; } } [list persist persist [temp_journal_mode persist]] -do_test jrnlmode-1.4 { +do_test jrnlmode-1.4a { + # When defensive is on, unable to set journal_mode to OFF + sqlite3_db_config db DEFENSIVE 1 + execsql { + PRAGMA journal_mode = off; + } +} {persist} +do_test jrnlmode-1.4b { + # When defensive is on, unable to set journal_mode to OFF + sqlite3_db_config db DEFENSIVE 0 execsql { PRAGMA journal_mode = off; } @@ -302,6 +311,7 @@ ifcapable autovacuum&&pragma { # The following test caes, jrnlmode-5.*, test the journal_size_limit # pragma. ifcapable pragma { +if {[atomic_batch_write test.db]==0} { db close forcedelete test.db test2.db test3.db sqlite3 db test.db @@ -454,8 +464,10 @@ ifcapable pragma { list [file exists test.db-journal] [file size test.db-journal] } {1 0} } +} ifcapable pragma { +if {[atomic_batch_write test.db]==0} { # These tests are not run as part of the "journaltest" permutation, # as the test_journal.c layer is incompatible with in-memory journaling. if {[permutation] ne "journaltest"} { @@ -507,6 +519,7 @@ ifcapable pragma { } {0} } } +} ifcapable pragma { catch { db close } diff --git a/test/jrnlmode2.test b/test/jrnlmode2.test index 6ea87d704b..6cc54dc5df 100644 --- a/test/jrnlmode2.test +++ b/test/jrnlmode2.test @@ -18,6 +18,11 @@ ifcapable {!pager_pragmas} { return } +if {[atomic_batch_write test.db]} { + finish_test + return +} + #------------------------------------------------------------------------- # The tests in this file check that the following two bugs (both now fixed) # do not reappear. diff --git a/test/json/README.md b/test/json/README.md new file mode 100644 index 0000000000..4ebbda6d3f --- /dev/null +++ b/test/json/README.md @@ -0,0 +1,66 @@ +The files in this subdirectory are used to help measure the performance +of the SQLite JSON functions, especially in relation to handling large +JSON inputs. + +# 1.0 Prerequisites + + * Standard SQLite build environment (SQLite source tree, compiler, make, etc.) + + * Valgrind + + * Fossil (only the "fossil xdiff" command is used by this procedure) + + * tclsh + +# 2.0 Setup + + * Run: "`tclsh json-generator.tcl | sqlite3 json100mb.db`" to create + the 100 megabyte test database. Do this so that the "json100mb.db" + file lands in the directory from which you will run tests, not in + the test/json subdirectory of the source tree. + + * Make a copy of "json100mb.db" into "jsonb100mb.db" - change the prefix + from "json" to "jsonb". + + * Bring up jsonb100mb.db in the sqlite3 command-line shell. + Convert all of the content into JSONB using a commands like this: + +> UPDATE data1 SET x=jsonb(x); +> VACUUM; + + * Build the baseline sqlite3.c file with sqlite3.h and shell.c. + +> make clean sqlite3.c + + * Run "`sh json-speed-check.sh trunk`". This creates the baseline + profile in "jout-trunk.txt" for the preformance test using text JSON. + + * Run "`sh json-speed-check.sh trunk --jsonb`". This creates the + baseline profile in "joutb-trunk.txt" for the performance test + for processing JSONB + + * (Optional) Verify that the json100mb.db database really does contain + approximately 100MB of JSON content by running: + +> SELECT sum(length(x)) FROM data1; +> SELECT * FROM data1 WHERE NOT json_valid(x); + +# 3.0 Testing + + * Build the sqlite3.c (with sqlite3.h and shell.c) to be tested. + + * Run "`sh json-speed-check.sh x1`". The profile output will appear + in jout-x1.txt. Substitute any label you want in place of "x1". + + * Run "`sh json-speed-check.sh x1 --jsonb`". The profile output will appear + in joutb-x1.txt. Substitute any label you want in place of "x1". + + * Run the script shown below in the CLI. + Divide 2500 by the real elapse time from this test + to get an estimate for number of MB/s that the JSON parser is + able to process. + +> .open json100mb.db +> .timer on +> WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<25) +> SELECT sum(json_valid(x)) FROM c, data1; diff --git a/test/json/json-generator.tcl b/test/json/json-generator.tcl new file mode 100644 index 0000000000..d499bc7300 --- /dev/null +++ b/test/json/json-generator.tcl @@ -0,0 +1,401 @@ +#!/usr/bin/tclsh +# +# Generate SQL that will populate an SQLite database with about 100 megabytes +# of pseudo-random JSON text. +# +# tclsh json-generator.tcl | sqlite3 json110mb.db +# +# srand() is used to initialize the random seed so that the same JSON +# is generated for every run. +# +expr srand(12345678) +set wordlist { + ability able abroad access account act + action active actor add address adept + adroit advance advice affect age ageless + agency agent agile agree air airfare + airline airport alert almond alpha always + amend amount amplify analyst anchor angel + angelic angle ankle annual answer antique + anybody anyhow appeal apple apricot apt + area argon arm army arrival arsenic + art artful article arugula aside ask + aspect assist assume atom atone attempt + author autumn average avocado award awl + azure back bacon bag bagel bake + baker balance ball balloon bamboo banana + band banjo bank barium base basil + basin basis basket bass bat bath + battery beach beak bean bear bearcub + beauty beef beet beige being bell + belly belt bench bend benefit best + beta better beyond bicycle bid big + bike bill bird biscuit bismuth bisque + bit black blank blest blind bliss + block bloom blue board boat body + bokchoy bone bonus book bookish boot + border boron boss bossy bottle bottom + bow bowl bowtie box brain brainy + branch brave bravely bread break breath + breezy brick bridge brie brief briefly + bright broad broil bromine bronze brother + brow brown brush buddy budget buffalo + bug bugle bull bunch burger burly + burrito bus busy butter button buy + buyer byte cab cabbage cabinet cable + cadet cadmium caesium cake calcium caliper + call caller calm calmly camera camp + can canary cancel candle candy cap + capable caper capital captain car carbon + card care career careful carp carpet + carrot carry case cash cassava casual + cat catch catfish catsear catsup cause + cave celery cell century chain chair + chalk chance change channel chapter chard + charge charity chart check cheddar cheery + cheese chicken chicory chiffon child chin + chip chives choice chowder chum church + circle city claim clam class classic + classy clay clean cleaner clear clearly + clerk click client climate clock clorine + closet clothes cloud clown club clue + cluster coach coast coat cobbler cobolt + cod code coffee colby cold collar + college comb combine comet comfort command + comment common company complex concept concern + concert conduit consist contact contest context + control convert cook cookie copilot copper + copy coral cordial corn corner corny + correct cost count counter country county + couple courage course court cover cow + cowbird crab crack craft crash crazy + cream credit creek cress crevice crew + crimson croaker crop cross crowd cube + cuckoo cuisine culture cup current curve + cut cyan cycle dagger daily dance + dare darter data date day daylily + deal dear dearly debate debit decade + decimal deep deft deftly degree delay + deluxe deposit depth design desk detail + device dew diamond diet dig dill + dinner dip direct dirt dish disk + display diver divide divine doctor dodger + donut door dot double dough draft + drag dragon drama draw drawer drawing + dream drill drink drive driver drop + drum dry dryer drywall duck due + dump dusk dust duty dye eagle + ear earring earth ease east easy + eat economy edge editor eel effect + effort egg eight elbow elegant element + elf elk email emerald employ end + endive endless energy engine enjoy enter + entry equal equip error escape essay + eternal evening event exam example excuse + exit expert extent extreme eye face + fact factor factual fail failure fair + fajita fall family fan fang farm + farmer fat fault feature feed feel + feeling fench fennel festive few fiber + field fig figure file fill film + filter final finance finding finger finish + fire fish fishing fit fitting five + fix flier flight floor floral florine + flour flow flower fly flying focus + fold folding food foot force forest + forever forgive form formal format fortune + forum frame free freedom freely fresh + friend frog front fruit fuchsia fuel + fun funny future gain galaxy gallium + game gamma gap garage garden garlic + gas gate gather gauge gear gem + gene general gentle gently gherkin ghost + gift give glad glass gleeful glossy + glove glue goal goat goby gold + goldeye golf good gouda goulash gourd + grab grace grade gram grand grape + grapes grass gravy gray great green + grits grocery ground group grouper grout + growth guard guave guess guest guide + guitar gumbo guppy habit hacksaw haddock + hafnium hagfish hair half halibut hall + hammer hand handle handy hanger happy + hat havarti hay haybale head health + healthy hearing heart hearty heat heavy + heel height helium hello help helpful + herald herring hide high highly highway + hill hip hipster hire history hit + hoki hold hole holiday holly home + honest honey hook hope hopeful horizon + horn horse host hotel hour house + housing human humane humor hunt hurry + ice icecube icefish icy idea ideal + image impact impress inch income indigo + initial inkpen insect inside intense invite + iodine iridium iron island issue item + ivory jacket jargon javelin jello jelly + jewel job jocund join joint joke + jovial joy joyful joyous judge juice + jump junior jury just justice kale + keel keep kelp ketchup key keyhole + keyway khaki kick kid kidney kiloohm + kind kindly king kitchen kite kiwi + knee knife krill krypton kumquat lab + lace lack ladder lake lamp lamprey + land laser laugh law lawn lawyer + layer lead leader leading leaf leafy + league leather leave lecture leek leg + lemon length lentil lesson let letter + lettuce level library life lift light + lily lime limit line linen link + lip list listen lithium lively living + lizard load loan lobster local lock + log long longfin look lotus love + lovely loving low lucid luck luffa + lunch lung machine magenta magnet mail + main major make mall manager mango + manner many map march market maroon + martian master match math matter maximum + maybe meal meaning meat media medium + meet meeting melody melon member memory + mention menu mercury merry mess message + messy metal meter method micron middle + might mile milk mind mine minimum + minnow minor mint minute mirror miss + mission misty mix mixer mixture mobile + mode model moment monitor monk month + moon moray morning most motor mouse + mouth move mover movie much mud + mudfish muffin mullet munster muon muscle + music mustard nail name nation native + natural nature navy neat neatly nebula + neck needle neon nerve net network + neutron news nibble nice nickel night + niobium nobody noise noodle normal north + nose note nothing notice nova novel + number nurse nursery oar object offer + office officer oil okay okra old + olive one onion open opening opinion + option orange orbit orchid order oregano + other ounce outcome outside oven owner + oxygen oyster pace pack package page + pager paint pair pale pan pancake + papaya paper pardon parent park parking + parsley parsnip part partner party pass + passage past pasta path patient pattern + pause pay pea peace peach peacock + peahen peak peanut pear pearl pen + penalty pencil pension people pepper perch + perfect period permit person phase phone + photo phrase physics piano pick picture + pie piece pigeon pike pilot pin + pink pinkie pious pipe pitch pizza + place plan plane planet plant planter + plastic plate play player playful plenty + pliers plum pod poem poet poetry + point police policy pollock pony pool + pop popover poptart pork port portal + post pot potato pound powder power + present press price pride primary print + prior private prize problem process produce + product profile profit program project promise + prompt proof proper protein proton public + puff puffer pull pumpkin pup pupfish + pure purple purpose push put quality + quark quarter quiet quill quit quote + rabbit raccoon race radiant radio radish + radium radon rain rainbow raise ramp + ranch range rasp rate ratio ray + razor reach read reading real reality + reason recipe record recover red redeem + reed reef refuse region regret regular + relaxed release relief relish remote remove + rent repair repeat reply report request + reserve resist resolve resort rest result + return reveal review reward ribbon rice + rich ride ridge right ring rise + risk river rivet road roast rock + rocket role roll roof room rope + rose rough roughy round row royal + rub ruby rudder ruin rule run + runner rush rust sacred saddle safe + safety sail salad salami sale salmon + salt sample sand sander sandy sauce + save saving saw scale scampi scene + scheme school score screen script sea + search season seat second secret sector + seemly self sell senate senior sense + series serve set shake shape share + shark shell shift shine shiny ship + shock shoe shoot shop shovel show + side sign signal silk silly silver + simple sing singer single sink site + size skill skin sky slate sleep + sleepy slice slide slip smart smell + smelt smile smoke smooth snap snipe + snow snowy sock socket sodium soft + softly soil sole solid song sorrel + sort soul sound soup source south + space spare speech speed spell spend + sphere spice spider spirit spite split + spoon sport spot spray spread spring + squab square squash stable staff stage + stand staple star start state status + stay steak steel step stern stew + stick still stock stone stop store + storm story strain street stress strike + string stroke strong studio study stuff + style sugar suit sulfur summer sun + sunny sunset super superb surf survey + sweet swim swing switch symbol system + table tackle tail tale talk tan + tank tap tape target task taste + tau tea teach teal team tear + tell ten tender tennis tent term + test tetra text thanks theme theory + thing think thread throat thumb ticket + tidy tie tiger till time timely + tin tip title toast today toe + tomato tone tongue tool tooth top + topic total touch tough tour towel + tower town track trade train trash + travel tray treat tree trick trip + trout trowel truck trupet trust truth + try tube tuna tune turf turkey + turn turnip tutor tux tweet twist + two type union unique unit upbeat + upper use useful user usual valley + value van vase vast veil vein + velvet verse very vessel vest video + view violet visit visual vivid voice + volume vowel voyage waffle wait wake + walk wall warm warmth wasabi wash + watch water wave wax way wealth + wear web wedge week weekly weight + west whale what wheat wheel when + where while who whole why will + win wind window wing winner winter + wire wish witty wolf wonder wood + wool woolly word work worker world + worry worth worthy wrap wrench wrist + writer xenon yak yam yard yarrow + year yearly yellow yew yogurt young + youth zebra zephyr zinc zone zoo +} +set nwordlist [llength $wordlist] + +proc random_char {} { + return [string index \ + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" \ + [expr {int(rand()*52)}]] +} +proc random_label {} { + set label [random_char] + while {rand()>0.8} { + append label [random_char] + } + if {rand()>0.9} {append label -} + append label [format %d [expr {int(rand()*100)}]] + return $label +} +proc random_numeric {} { + set n [expr {(rand()*2-1.0)*1e6}] + switch [expr {int(rand()*6)}] { + 0 {set format %.3f} + 1 {set format %.6E} + 2 {set format %.4e} + default {set format %g} + } + return [format $format $n] +} + + +proc random_json {limit indent} { + global nwordlist wordlist + set res {} + if {$indent==0 || ($limit>0 && rand()>0.5)} { + incr limit -1 + incr indent 2 + set n [expr {int(rand()*5)+1}] + if {$n==5} {incr n [expr {int(rand()*10)}]} + if {rand()>0.5} { + set res \173\n + for {set i 0} {$i<$n} {incr i} { + append res [string repeat { } $indent] + if {rand()>0.8} { + if {rand()>0.5} { + set sep ":\n [string repeat { } $indent]" + } else { + set sep " : " + } + } else { + set sep : + } + append res \"[random_label]\"$sep[random_json $limit $indent] + if {$i<$n-1} {append res ,} + append res \n + } + incr indent -2 + append res [string repeat { } $indent] + append res \175 + return $res + } else { + set res \[\n + for {set i 0} {$i<$n} {incr i} { + append res [string repeat { } $indent] + append res [random_json $limit $indent] + if {$i<$n-1} {append res ,} + append res \n + } + incr indent -2 + append res [string repeat { } $indent] + append res \] + return $res + } + } elseif {rand()>0.9} { + if {rand()>0.7} {return "true"} + if {rand()>0.5} {return "false"} + return "null" + } elseif {rand()>0.5} { + return [random_numeric] + } else { + set res \" + set n [expr {int(rand()*4)+1}] + if {$n>=4} {set n [expr {$n+int(rand()*6)}]} + for {set i 0} {$i<$n} {incr i} { + if {rand()<0.05} { + set w [random_numeric] + } else { + set k [expr {int(rand()*$nwordlist)}] + set w [lindex $wordlist $k] + } + if {rand()<0.07} { + set w \\\"$w\\\" + } + if {$i<$n-1} { + switch [expr {int(rand()*9)}] { + 0 {set sp {, }} + 1 {set sp "\\n "} + 2 {set sp "-"} + default {set sp { }} + } + append res $w$sp + } else { + append res $w + if {rand()<0.2} {append res .} + } + } + return $res\" + } +} + +puts "CREATE TABLE IF NOT EXISTS data1(x JSON);" +puts "BEGIN;" +set sz 0 +for {set i 0} {$sz<100000000} {incr i} { + set j [random_json 7 0] + incr sz [string length $j] + puts "INSERT INTO data1(x) VALUES('$j');" +} +puts "COMMIT;" +puts "SELECT sum(length(x)) FROM data1;" diff --git a/test/json/json-q1.txt b/test/json/json-q1.txt new file mode 100644 index 0000000000..d122a2d826 --- /dev/null +++ b/test/json/json-q1.txt @@ -0,0 +1,24 @@ +.mode qbox +.timer on +.param set $label 'q87' +SELECT rowid, x->>$label FROM data1 WHERE x->>$label IS NOT NULL; + +CREATE TEMP TABLE t2(x JSON TEXT); +WITH RECURSIVE + c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<25000), + array1(y) AS ( + SELECT json_group_array( + json_object('x',x,'y',random(),'z',hex(randomblob(50))) + ) + FROM c + ), + c2(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c2 WHERE n<5) +INSERT INTO t2(x) + SELECT json_object('a',n,'b',n*2,'c',y,'d',3,'e',5,'f',6) FROM array1, c2; +CREATE INDEX t2x1 ON t2(x->>'a'); +CREATE INDEX t2x2 ON t2(x->>'b'); +CREATE INDEX t2x3 ON t2(x->>'e'); +CREATE INDEX t2x4 ON t2(x->>'f'); +UPDATE t2 SET x=json_replace(x,'$.f',(x->>'f')+1); +UPDATE t2 SET x=json_set(x,'$.e',(x->>'f')-1); +UPDATE t2 SET x=json_remove(x,'$.d'); diff --git a/test/json/json-speed-check.sh b/test/json/json-speed-check.sh new file mode 100755 index 0000000000..1eabbb3db2 --- /dev/null +++ b/test/json/json-speed-check.sh @@ -0,0 +1,87 @@ +#!/bin/bash +# +# This is a template for a script used for day-to-day size and +# performance monitoring of SQLite. Typical usage: +# +# sh speed-check.sh trunk # Baseline measurement of trunk +# sh speed-check.sh x1 # Measure some experimental change +# fossil xdiff --tk jout-trunk.txt jout-x1.txt # View changes +# +# There are multiple output files, all with a base name given by +# the first argument: +# +# summary-$BASE.txt # Copy of standard output +# jout-$BASE.txt # cachegrind output +# explain-$BASE.txt # EXPLAIN listings (only with --explain) +# +if test "$1" = "" +then + echo "Usage: $0 OUTPUTFILE [OPTIONS]" + exit +fi +NAME=$1 +shift +#CC_OPTS="-DSQLITE_ENABLE_RTREE -DSQLITE_ENABLE_MEMSYS5" +CC_OPTS="-DSQLITE_ENABLE_MEMSYS5" +CC=gcc +LEAN_OPTS="-DSQLITE_THREADSAFE=0" +LEAN_OPTS="$LEAN_OPTS -DSQLITE_DEFAULT_MEMSTATUS=0" +LEAN_OPTS="$LEAN_OPTS -DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1" +LEAN_OPTS="$LEAN_OPTS -DSQLITE_LIKE_DOESNT_MATCH_BLOBS" +LEAN_OPTS="$LEAN_OPTS -DSQLITE_MAX_EXPR_DEPTH=0" +LEAN_OPTS="$LEAN_OPTS -DSQLITE_OMIT_DECLTYPE" +LEAN_OPTS="$LEAN_OPTS -DSQLITE_OMIT_DEPRECATED" +LEAN_OPTS="$LEAN_OPTS -DSQLITE_OMIT_PROGRESS_CALLBACK" +LEAN_OPTS="$LEAN_OPTS -DSQLITE_OMIT_SHARED_CACHE" +LEAN_OPTS="$LEAN_OPTS -DSQLITE_USE_ALLOCA" +BASELINE="trunk" +TYPE="json" +doExplain=0 +doCachegrind=1 +doVdbeProfile=0 +doWal=1 +doDiff=1 +doJsonB=0 +while test "$1" != ""; do + case $1 in + --nodiff) + doDiff=0 + ;; + --lean) + CC_OPTS="$CC_OPTS $LEAN_OPTS" + ;; + --clang) + CC=clang + ;; + --gcc7) + CC=gcc-7 + ;; + --jsonb) + doJsonB=1 + TYPE="jsonb" + ;; + -*) + CC_OPTS="$CC_OPTS $1" + ;; + *) + BASELINE=$1 + ;; + esac + shift +done +echo "NAME = $NAME" | tee summary-$NAME.txt +echo "CC_OPTS = $CC_OPTS" | tee -a summary-$NAME.txt +rm -f cachegrind.out.* jsonshell +$CC -g -Os -Wall -I. $CC_OPTS ./shell.c ./sqlite3.c -o jsonshell -ldl -lpthread +ls -l jsonshell | tee -a summary-$NAME.txt +home=`echo $0 | sed -e 's,/[^/]*$,,'` +DB=$TYPE''100mb.db +echo ./jsonshell $DB "<$home/$TYPE-q1.txt" +valgrind --tool=cachegrind ./jsonshell json100mb_b.db <$home/$TYPE-q1.txt \ + 2>&1 | tee -a summary-$NAME.txt +cg_anno.tcl cachegrind.out.* >$TYPE-$NAME.txt +echo '*****************************************************' >>$TYPE-$NAME.txt +sed 's/^[0-9=-]\{9\}/==00000==/' summary-$NAME.txt >>$TYPE-$NAME.txt +if test "$NAME" != "$BASELINE" -a $doDiff -ne 0; then + fossil xdiff --tk -c 20 $TYPE-$BASELINE.txt $TYPE-$NAME.txt +fi diff --git a/test/json/jsonb-q1.txt b/test/json/jsonb-q1.txt new file mode 100644 index 0000000000..e78c63670a --- /dev/null +++ b/test/json/jsonb-q1.txt @@ -0,0 +1,24 @@ +.mode qbox +.timer on +.param set $label 'q87' +SELECT rowid, x->>$label FROM data1 WHERE x->>$label IS NOT NULL; + +CREATE TEMP TABLE t2(x JSON TEXT); +WITH RECURSIVE + c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<25000), + array1(y) AS ( + SELECT json_group_array( + json_object('x',x,'y',random(),'z',hex(randomblob(50))) + ) + FROM c + ), + c2(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c2 WHERE n<5) +INSERT INTO t2(x) + SELECT jsonb_object('a',n,'b',n*2,'c',y,'d',3,'e',5,'f',6) FROM array1, c2; +CREATE INDEX t2x1 ON t2(x->>'a'); +CREATE INDEX t2x2 ON t2(x->>'b'); +CREATE INDEX t2x3 ON t2(x->>'e'); +CREATE INDEX t2x4 ON t2(x->>'f'); +UPDATE t2 SET x=jsonb_replace(x,'$.f',(x->>'f')+1); +UPDATE t2 SET x=jsonb_set(x,'$.e',(x->>'f')-1); +UPDATE t2 SET x=jsonb_remove(x,'$.d'); diff --git a/test/json101.test b/test/json101.test index 9b780a379b..7582d14a64 100644 --- a/test/json101.test +++ b/test/json101.test @@ -15,11 +15,6 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl -ifcapable !json1 { - finish_test - return -} - do_execsql_test json101-1.1.00 { SELECT json_array(1,2.5,null,'hello'); } {[1,2.5,null,"hello"]} @@ -41,6 +36,9 @@ do_execsql_test json101-1.2 { do_catchsql_test json101-1.3 { SELECT json_array(1,printf('%.1000c','x'),x'abcd',3); } {1 {JSON cannot hold BLOB values}} +do_catchsql_test json101-1.3b { + SELECT jsonb_array(1,printf('%.1000c','x'),x'abcd',3); +} {1 {JSON cannot hold BLOB values}} do_execsql_test json101-1.4 { SELECT json_array(-9223372036854775808,9223372036854775807,0,1,-1, 0.0, 1.0, -1.0, -1e99, +2e100, @@ -52,36 +50,83 @@ do_execsql_test json101-1.4 { 'abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ', 99); } {[-9223372036854775808,9223372036854775807,0,1,-1,0.0,1.0,-1.0,-1.0e+99,2.0e+100,"one","two","three",4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,null,21,22,23,24,25,26,27,28,29,30,31,"abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ",99]} +do_execsql_test json101-1.4b { + SELECT json(jsonb_array(-9223372036854775808,9223372036854775807,0,1,-1, + 0.0, 1.0, -1.0, -1e99, +2e100, + 'one','two','three', + 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, + 19, NULL, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, + 'abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ', + 'abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ', + 'abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ', + 99)); +} {[-9223372036854775808,9223372036854775807,0,1,-1,0.0,1.0,-1.0,-1.0e+99,2.0e+100,"one","two","three",4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,null,21,22,23,24,25,26,27,28,29,30,31,"abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ",99]} do_execsql_test json101-2.1 { SELECT json_object('a',1,'b',2.5,'c',null,'d','String Test'); } {{{"a":1,"b":2.5,"c":null,"d":"String Test"}}} +do_execsql_test json101-2.1b { + SELECT json(jsonb_object('a',1,'b',2.5,'c',null,'d','String Test')); +} {{{"a":1,"b":2.5,"c":null,"d":"String Test"}}} do_catchsql_test json101-2.2 { SELECT json_object('a',printf('%.1000c','x'),2,2.5); } {1 {json_object() labels must be TEXT}} +do_catchsql_test json101-2.2b { + SELECT jsonb_object('a',printf('%.1000c','x'),2,2.5); +} {1 {json_object() labels must be TEXT}} +do_execsql_test json101-2.2.2 { + SELECT json_object('a',json_array('xyx',77,4.5),'x',2.5); +} {{{"a":["xyx",77,4.5],"x":2.5}}} +do_execsql_test json101-2.2.2b { + SELECT json(jsonb_object('a',json_array('xyx',77,4.5),'x',2.5)); +} {{{"a":["xyx",77,4.5],"x":2.5}}} +do_execsql_test json101-2.2.3 { + SELECT json_object('a',jsonb_array('xyx',77,4.5),'x',2.5); +} {{{"a":["xyx",77,4.5],"x":2.5}}} +do_execsql_test json101-2.2.3b { + SELECT json(jsonb_object('a',jsonb_array('xyx',77,4.5),'x',2.5)); +} {{{"a":["xyx",77,4.5],"x":2.5}}} do_catchsql_test json101-2.3 { SELECT json_object('a',1,'b'); } {1 {json_object() requires an even number of arguments}} do_catchsql_test json101-2.4 { SELECT json_object('a',printf('%.1000c','x'),'b',x'abcd'); } {1 {JSON cannot hold BLOB values}} +do_execsql_test json101-2.5 { + SELECT json_object('a',printf('%.10c','x'),'b',jsonb_array(1,2,3)); +} {{{"a":"xxxxxxxxxx","b":[1,2,3]}}} do_execsql_test json101-3.1 { SELECT json_replace('{"a":1,"b":2}','$.a','[3,4,5]'); } {{{"a":"[3,4,5]","b":2}}} +do_execsql_test json101-3.1b { + SELECT json(jsonb_replace('{"a":1,"b":2}','$.a','[3,4,5]')); +} {{{"a":"[3,4,5]","b":2}}} do_execsql_test json101-3.2 { SELECT json_replace('{"a":1,"b":2}','$.a',json('[3,4,5]')); } {{{"a":[3,4,5],"b":2}}} +do_execsql_test json101-3.2b { + SELECT json_replace('{"a":1,"b":2}','$.a',jsonb('[3,4,5]')); +} {{{"a":[3,4,5],"b":2}}} do_execsql_test json101-3.3 { SELECT json_type(json_set('{"a":1,"b":2}','$.b','{"x":3,"y":4}'),'$.b'); } {text} +do_execsql_test json101-3.3b { + SELECT json_type(jsonb_set('{"a":1,"b":2}','$.b','{"x":3,"y":4}'),'$.b'); +} {text} do_execsql_test json101-3.4 { SELECT json_type(json_set('{"a":1,"b":2}','$.b',json('{"x":3,"y":4}')),'$.b'); } {object} +do_execsql_test json101-3.4b { + SELECT json_type(jsonb_set('{"a":1,"b":2}','$.b',jsonb('{"x":3,"y":4}')),'$.b'); +} {object} ifcapable vtab { -do_execsql_test json101-3.5 { - SELECT fullkey, atom, '|' FROM json_tree(json_set('{}','$.x',123,'$.x',456)); -} {{$} {} | {$.x} 456 |} + do_execsql_test json101-3.5 { + SELECT fullkey, atom, '|' FROM json_tree(json_set('{}','$.x',123,'$.x',456)); + } {{$} {} | {$.x} 456 |} + do_execsql_test json101-3.5b { + SELECT fullkey, atom, '|' FROM json_tree(jsonb_set('{}','$.x',123,'$.x',456)); + } {{$} {} | {$.x} 456 |} } # Per rfc7159, any JSON value is allowed at the top level, and whitespace @@ -124,17 +169,27 @@ do_execsql_test json101-4.7 { do_execsql_test json101-4.8 { SELECT x FROM j1 WHERE json_insert(x)<>x; } {} +do_execsql_test json101-4.9 { + SELECT json_insert('{"a":1}','$.b',CAST(x'0000' AS text)); +} {{{"a":1,"b":"\u0000\u0000"}}} # json_extract(JSON,'$') will return objects and arrays without change. # -do_execsql_test json-4.10 { +do_execsql_test json101-4.10 { SELECT count(*) FROM j1 WHERE json_type(x) IN ('object','array'); SELECT x FROM j1 WHERE json_extract(x,'$')<>x AND json_type(x) IN ('object','array'); } {4} +do_execsql_test json101-4.10b { + CREATE TABLE j1b AS SELECT jsonb(x) AS "x" FROM j1; + SELECT count(*) FROM j1b WHERE json_type(x) IN ('object','array'); + SELECT json(x) FROM j1b + WHERE json_extract(x,'$')<>json(x) + AND json_type(x) IN ('object','array'); +} {4} -do_execsql_test json-5.1 { +do_execsql_test json101-5.1 { CREATE TABLE j2(id INTEGER PRIMARY KEY, json, src); INSERT INTO j2(id,json,src) VALUES(1,'{ @@ -260,11 +315,17 @@ do_execsql_test json-5.1 { } ]','https://adobe.github.io/Spry/samples/data_region/JSONDataSetSample.html'); SELECT count(*) FROM j2; -} {3} + CREATE TABLE j2b(id INTEGER PRIMARY KEY, json, src); + INSERT INTO J2b(id,json,src) SELECT id, jsonb(json), src FROM j2; + SELECT count(*) FROM j2b; +} {3 3} -do_execsql_test json-5.2 { +do_execsql_test json101-5.2 { SELECT id, json_valid(json), json_type(json), '|' FROM j2 ORDER BY id; } {1 1 object | 2 1 object | 3 1 array |} +do_execsql_test json101-5.2b { + SELECT id, json_valid(json,5), json_type(json), '|' FROM j2b ORDER BY id; +} {1 1 object | 2 1 object | 3 1 array |} ifcapable !vtab { finish_test @@ -273,13 +334,19 @@ ifcapable !vtab { # fullkey is always the same as path+key (with appropriate formatting) # -do_execsql_test json-5.3 { +do_execsql_test json101-5.3 { SELECT j2.rowid, jx.rowid, fullkey, path, key FROM j2, json_tree(j2.json) AS jx WHERE fullkey!=(path || CASE WHEN typeof(key)=='integer' THEN '['||key||']' ELSE '.'||key END); } {} -do_execsql_test json-5.4 { +do_execsql_test json101-5.3b { + SELECT j2b.rowid, jx.rowid, fullkey, path, key + FROM j2b, json_tree(j2b.json) AS jx + WHERE fullkey!=(path || CASE WHEN typeof(key)=='integer' THEN '['||key||']' + ELSE '.'||key END); +} {} +do_execsql_test json101-5.4 { SELECT j2.rowid, jx.rowid, fullkey, path, key FROM j2, json_each(j2.json) AS jx WHERE fullkey!=(path || CASE WHEN typeof(key)=='integer' THEN '['||key||']' @@ -290,37 +357,72 @@ do_execsql_test json-5.4 { # Verify that the json_each.json and json_tree.json output is always the # same as input. # -do_execsql_test json-5.5 { +do_execsql_test json101-5.5 { SELECT j2.rowid, jx.rowid, fullkey, path, key FROM j2, json_each(j2.json) AS jx WHERE jx.json<>j2.json; } {} -do_execsql_test json-5.6 { +do_execsql_test json101-5.6 { SELECT j2.rowid, jx.rowid, fullkey, path, key FROM j2, json_tree(j2.json) AS jx WHERE jx.json<>j2.json; } {} -do_execsql_test json-5.7 { +do_execsql_test json101-5.7 { SELECT j2.rowid, jx.rowid, fullkey, path, key FROM j2, json_each(j2.json) AS jx WHERE jx.value<>jx.atom AND type NOT IN ('array','object'); } {} -do_execsql_test json-5.8 { +do_execsql_test json101-5.8 { SELECT j2.rowid, jx.rowid, fullkey, path, key FROM j2, json_tree(j2.json) AS jx WHERE jx.value<>jx.atom AND type NOT IN ('array','object'); } {} -do_execsql_test json-6.1 { +# 2024-02-16 https://sqlite.org/forum/forumpost/ecb94cd210 +# Regression in json_tree()/json_each(). The value column +# should have the "J" subtype if the value is an array or +# object. +# +do_execsql_test json101-5.10 { + SELECT json_insert('{}','$.a',value) FROM json_tree('[1,2,3]') WHERE atom IS NULL; +} {{{"a":[1,2,3]}}} +# ^^^^^^^--- In double-quotes, a string literal, prior to bug fix + +do_execsql_test json101-5.11 { + SELECT json_insert('{}','$.a',value) FROM json_tree('"[1,2,3]"'); +} {{{"a":"[1,2,3]"}}} + +do_execsql_test json101-6.1 { SELECT json_valid('{"a":55,"b":72,}'); } {0} -do_execsql_test json-6.2 { +do_execsql_test json101-6.2 { + SELECT json_error_position('{"a":55,"b":72,}'); +} {0} +do_execsql_test json101-6.3 { + SELECT json_valid(json('{"a":55,"b":72,}')); +} {1} +do_execsql_test json101-6.4 { + SELECT json_valid('{"a":55,"b":72 , }'); +} {0} +do_execsql_test json101-6.5 { + SELECT json_error_position('{"a":55,"b":72 , }'); +} {0} +do_execsql_test json101-6.6 { + SELECT json_error_position('{"a":55,"b":72,,}'); +} {16} +do_execsql_test json101-6.7 { SELECT json_valid('{"a":55,"b":72}'); } {1} -do_execsql_test json-6.3 { - SELECT json_valid('["a",55,"b",72,]'); +do_execsql_test json101-6.8 { + SELECT json_error_position('["a",55,"b",72,]'); +} {0} +do_execsql_test json101-6.9 { + SELECT json_error_position('["a",55,"b",72 , ]'); } {0} -do_execsql_test json-6.4 { +do_execsql_test json101-6.10 { + SELECT json_error_position('["a",55,"b",72,,]'); +} {16} +do_execsql_test json101-6.11 { SELECT json_valid('["a",55,"b",72]'); } {1} @@ -336,25 +438,744 @@ foreach {tn isvalid ws} { 7.6 1 char(0x20,0x09,0x0a,0x0d,0x20) 7.7 0 char(0x20,0x09,0x0a,0x0c,0x0d,0x20) } { - do_execsql_test json-$tn.1 \ + do_execsql_test json101-$tn.1 \ "SELECT json_valid(printf('%s{%s\"x\"%s:%s9%s}%s', $::ws,$::ws,$::ws,$::ws,$::ws,$::ws));" \ $isvalid } -# Ticket https://www.sqlite.org/src/info/ad2559db380abf8e +# Ticket https://sqlite.org/src/info/ad2559db380abf8e # Control characters must be escaped in JSON strings. # -do_execsql_test json-8.1 { +do_execsql_test json101-8.1 { DROP TABLE IF EXISTS t8; CREATE TABLE t8(a,b); INSERT INTO t8(a) VALUES('abc' || char(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35) || 'xyz'); UPDATE t8 SET b=json_array(a); SELECT b FROM t8; } {{["abc\u0001\u0002\u0003\u0004\u0005\u0006\u0007\b\t\n\u000b\f\r\u000e\u000f\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f !\"#xyz"]}} -do_execsql_test json-8.2 { +do_execsql_test json101-8.1b { + DROP TABLE IF EXISTS t8; + CREATE TABLE t8(a,b); + INSERT INTO t8(a) VALUES('abc' || char(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35) || 'xyz'); + UPDATE t8 SET b=jsonb_array(a); + SELECT json(b) FROM t8; +} {{["abc\u0001\u0002\u0003\u0004\u0005\u0006\u0007\b\t\n\u000b\f\r\u000e\u000f\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f !\"#xyz"]}} +do_execsql_test json101-8.2 { SELECT a=json_extract(b,'$[0]') FROM t8; } {1} +# 2017-04-12. Regression reported on the mailing list by Rolf Ade +# +do_execsql_test json101-8.3 { + SELECT json_valid(char(0x22,0xe4,0x22)); +} {1} +do_execsql_test json101-8.4 { + SELECT unicode(json_extract(char(0x22,228,0x22),'$')); +} {228} + +# The json_quote() function transforms an SQL value into a JSON value. +# String values are quoted and interior quotes are escaped. NULL values +# are rendered as the unquoted string "null". +# +do_execsql_test json101-9.1 { + SELECT json_quote('abc"xyz'); +} {{"abc\"xyz"}} +do_execsql_test json101-9.2 { + SELECT json_quote(3.14159); +} {3.14159} +do_execsql_test json101-9.3 { + SELECT json_quote(12345); +} {12345} +do_execsql_test json101-9.4 { + SELECT json_quote(null); +} {"null"} +do_catchsql_test json101-9.5 { + SELECT json_quote(x'3031323334'); +} {1 {JSON cannot hold BLOB values}} +do_catchsql_test json101-9.6 { + SELECT json_quote(123,456) +} {1 {wrong number of arguments to function json_quote()}} +do_catchsql_test json101-9.7 { + SELECT json_quote() +} {1 {wrong number of arguments to function json_quote()}} + +# Make sure only valid backslash-escapes are accepted. +# +do_execsql_test json101-10.1 { + SELECT json_valid('" \ "'); +} {0} +do_execsql_test json101-10.2 { + SELECT json_valid('" \! "'); +} {0} +do_execsql_test json101-10.3 { + SELECT json_valid('" \" "'); +} {1} +do_execsql_test json101-10.4 { + SELECT json_valid('" \# "'); +} {0} +do_execsql_test json101-10.5 { + SELECT json_valid('" \$ "'); +} {0} +do_execsql_test json101-10.6 { + SELECT json_valid('" \% "'); +} {0} +do_execsql_test json101-10.7 { + SELECT json_valid('" \& "'); +} {0} +do_execsql_test json101-10.8 { + SELECT json_valid('" \'' "'); +} {0} +do_execsql_test json101-10.9 { + SELECT json_valid('" \( "'); +} {0} +do_execsql_test json101-10.10 { + SELECT json_valid('" \) "'); +} {0} +do_execsql_test json101-10.11 { + SELECT json_valid('" \* "'); +} {0} +do_execsql_test json101-10.12 { + SELECT json_valid('" \+ "'); +} {0} +do_execsql_test json101-10.13 { + SELECT json_valid('" \, "'); +} {0} +do_execsql_test json101-10.14 { + SELECT json_valid('" \- "'); +} {0} +do_execsql_test json101-10.15 { + SELECT json_valid('" \. "'); +} {0} +do_execsql_test json101-10.16 { + SELECT json_valid('" \/ "'); +} {1} +do_execsql_test json101-10.17 { + SELECT json_valid('" \0 "'); +} {0} +do_execsql_test json101-10.18 { + SELECT json_valid('" \1 "'); +} {0} +do_execsql_test json101-10.19 { + SELECT json_valid('" \2 "'); +} {0} +do_execsql_test json101-10.20 { + SELECT json_valid('" \3 "'); +} {0} +do_execsql_test json101-10.21 { + SELECT json_valid('" \4 "'); +} {0} +do_execsql_test json101-10.22 { + SELECT json_valid('" \5 "'); +} {0} +do_execsql_test json101-10.23 { + SELECT json_valid('" \6 "'); +} {0} +do_execsql_test json101-10.24 { + SELECT json_valid('" \7 "'); +} {0} +do_execsql_test json101-10.25 { + SELECT json_valid('" \8 "'); +} {0} +do_execsql_test json101-10.26 { + SELECT json_valid('" \9 "'); +} {0} +do_execsql_test json101-10.27 { + SELECT json_valid('" \: "'); +} {0} +do_execsql_test json101-10.28 { + SELECT json_valid('" \; "'); +} {0} +do_execsql_test json101-10.29 { + SELECT json_valid('" \< "'); +} {0} +do_execsql_test json101-10.30 { + SELECT json_valid('" \= "'); +} {0} +do_execsql_test json101-10.31 { + SELECT json_valid('" \> "'); +} {0} +do_execsql_test json101-10.32 { + SELECT json_valid('" \? "'); +} {0} +do_execsql_test json101-10.33 { + SELECT json_valid('" \@ "'); +} {0} +do_execsql_test json101-10.34 { + SELECT json_valid('" \A "'); +} {0} +do_execsql_test json101-10.35 { + SELECT json_valid('" \B "'); +} {0} +do_execsql_test json101-10.36 { + SELECT json_valid('" \C "'); +} {0} +do_execsql_test json101-10.37 { + SELECT json_valid('" \D "'); +} {0} +do_execsql_test json101-10.38 { + SELECT json_valid('" \E "'); +} {0} +do_execsql_test json101-10.39 { + SELECT json_valid('" \F "'); +} {0} +do_execsql_test json101-10.40 { + SELECT json_valid('" \G "'); +} {0} +do_execsql_test json101-10.41 { + SELECT json_valid('" \H "'); +} {0} +do_execsql_test json101-10.42 { + SELECT json_valid('" \I "'); +} {0} +do_execsql_test json101-10.43 { + SELECT json_valid('" \J "'); +} {0} +do_execsql_test json101-10.44 { + SELECT json_valid('" \K "'); +} {0} +do_execsql_test json101-10.45 { + SELECT json_valid('" \L "'); +} {0} +do_execsql_test json101-10.46 { + SELECT json_valid('" \M "'); +} {0} +do_execsql_test json101-10.47 { + SELECT json_valid('" \N "'); +} {0} +do_execsql_test json101-10.48 { + SELECT json_valid('" \O "'); +} {0} +do_execsql_test json101-10.49 { + SELECT json_valid('" \P "'); +} {0} +do_execsql_test json101-10.50 { + SELECT json_valid('" \Q "'); +} {0} +do_execsql_test json101-10.51 { + SELECT json_valid('" \R "'); +} {0} +do_execsql_test json101-10.52 { + SELECT json_valid('" \S "'); +} {0} +do_execsql_test json101-10.53 { + SELECT json_valid('" \T "'); +} {0} +do_execsql_test json101-10.54 { + SELECT json_valid('" \U "'); +} {0} +do_execsql_test json101-10.55 { + SELECT json_valid('" \V "'); +} {0} +do_execsql_test json101-10.56 { + SELECT json_valid('" \W "'); +} {0} +do_execsql_test json101-10.57 { + SELECT json_valid('" \X "'); +} {0} +do_execsql_test json101-10.58 { + SELECT json_valid('" \Y "'); +} {0} +do_execsql_test json101-10.59 { + SELECT json_valid('" \Z "'); +} {0} +do_execsql_test json101-10.60 { + SELECT json_valid('" \[ "'); +} {0} +do_execsql_test json101-10.61 { + SELECT json_valid('" \\ "'); +} {1} +do_execsql_test json101-10.62 { + SELECT json_valid('" \] "'); +} {0} +do_execsql_test json101-10.63 { + SELECT json_valid('" \^ "'); +} {0} +do_execsql_test json101-10.64 { + SELECT json_valid('" \_ "'); +} {0} +do_execsql_test json101-10.65 { + SELECT json_valid('" \` "'); +} {0} +do_execsql_test json101-10.66 { + SELECT json_valid('" \a "'); +} {0} +do_execsql_test json101-10.67 { + SELECT json_valid('" \b "'); +} {1} +do_execsql_test json101-10.68 { + SELECT json_valid('" \c "'); +} {0} +do_execsql_test json101-10.69 { + SELECT json_valid('" \d "'); +} {0} +do_execsql_test json101-10.70 { + SELECT json_valid('" \e "'); +} {0} +do_execsql_test json101-10.71 { + SELECT json_valid('" \f "'); +} {1} +do_execsql_test json101-10.72 { + SELECT json_valid('" \g "'); +} {0} +do_execsql_test json101-10.73 { + SELECT json_valid('" \h "'); +} {0} +do_execsql_test json101-10.74 { + SELECT json_valid('" \i "'); +} {0} +do_execsql_test json101-10.75 { + SELECT json_valid('" \j "'); +} {0} +do_execsql_test json101-10.76 { + SELECT json_valid('" \k "'); +} {0} +do_execsql_test json101-10.77 { + SELECT json_valid('" \l "'); +} {0} +do_execsql_test json101-10.78 { + SELECT json_valid('" \m "'); +} {0} +do_execsql_test json101-10.79 { + SELECT json_valid('" \n "'); +} {1} +do_execsql_test json101-10.80 { + SELECT json_valid('" \o "'); +} {0} +do_execsql_test json101-10.81 { + SELECT json_valid('" \p "'); +} {0} +do_execsql_test json101-10.82 { + SELECT json_valid('" \q "'); +} {0} +do_execsql_test json101-10.83 { + SELECT json_valid('" \r "'); +} {1} +do_execsql_test json101-10.84 { + SELECT json_valid('" \s "'); +} {0} +do_execsql_test json101-10.85 { + SELECT json_valid('" \t "'); +} {1} +do_execsql_test json101-10.86.0 { + SELECT json_valid('" \u "'); +} {0} +do_execsql_test json101-10.86.1 { + SELECT json_valid('" \ua "'); +} {0} +do_execsql_test json101-10.86.2 { + SELECT json_valid('" \uab "'); +} {0} +do_execsql_test json101-10.86.3 { + SELECT json_valid('" \uabc "'); +} {0} +do_execsql_test json101-10.86.4 { + SELECT json_valid('" \uabcd "'); +} {1} +do_execsql_test json101-10.86.5 { + SELECT json_valid('" \uFEDC "'); +} {1} +do_execsql_test json101-10.86.6 { + SELECT json_valid('" \u1234 "'); +} {1} +do_execsql_test json101-10.87 { + SELECT json_valid('" \v "'); +} {0} +do_execsql_test json101-10.88 { + SELECT json_valid('" \w "'); +} {0} +do_execsql_test json101-10.89 { + SELECT json_valid('" \x "'); +} {0} +do_execsql_test json101-10.90 { + SELECT json_valid('" \y "'); +} {0} +do_execsql_test json101-10.91 { + SELECT json_valid('" \z "'); +} {0} +do_execsql_test json101-10.92 { + SELECT json_valid('" \{ "'); +} {0} +do_execsql_test json101-10.93 { + SELECT json_valid('" \| "'); +} {0} +do_execsql_test json101-10.94 { + SELECT json_valid('" \} "'); +} {0} +do_execsql_test json101-10.95 { + SELECT json_valid('" \~ "'); +} {0} + +#-------------------------------------------------------------------------- +# 2017-04-11. https://sqlite.org/src/info/981329adeef51011 +# Stack overflow on deeply nested JSON. +# +# The following tests confirm that deeply nested JSON is considered invalid. +# +do_execsql_test json101-11.0 { + /* Shallow enough to be parsed */ + SELECT json_valid(printf('%.1000c0%.1000c','[',']')); +} {1} +do_execsql_test json101-11.1 { + /* Too deep by one */ + SELECT json_valid(printf('%.1001c0%.1001c','[',']')); +} {0} +do_execsql_test json101-11.2 { + /* Shallow enough to be parsed { */ + SELECT json_valid(replace(printf('%.1000c0%.1000c','[','}'),'[','{"a":')); + /* } */ +} {1} +do_execsql_test json101-11.3 { + /* Too deep by one { */ + SELECT json_valid(replace(printf('%.1001c0%.1001c','[','}'),'[','{"a":')); + /* } */ +} {0} + +# 2017-10-27. Demonstrate the ability to access an element from +# a json structure even though the element name constains a "." +# character, by quoting the element name in the path. +# +do_execsql_test json101-12.100 { + CREATE TABLE t12(x); + INSERT INTO t12(x) VALUES( + '{"settings": + {"layer2": + {"hapax.legomenon": + {"forceDisplay":true, + "transliterate":true, + "add.footnote":true, + "summary.report":true}, + "dis.legomenon": + {"forceDisplay":true, + "transliterate":false, + "add.footnote":false, + "summary.report":true}, + "tris.legomenon": + {"forceDisplay":true, + "transliterate":false, + "add.footnote":false, + "summary.report":false} + } + } + }'); +} {} + +do_execsql_test json101-12.110 { + SELECT json_remove(x, '$.settings.layer2."dis.legomenon".forceDisplay') + FROM t12; +} {{{"settings":{"layer2":{"hapax.legomenon":{"forceDisplay":true,"transliterate":true,"add.footnote":true,"summary.report":true},"dis.legomenon":{"transliterate":false,"add.footnote":false,"summary.report":true},"tris.legomenon":{"forceDisplay":true,"transliterate":false,"add.footnote":false,"summary.report":false}}}}}} +do_execsql_test json101-12.110b { + SELECT json_remove(jsonb(x), '$.settings.layer2."dis.legomenon".forceDisplay') + FROM t12; +} {{{"settings":{"layer2":{"hapax.legomenon":{"forceDisplay":true,"transliterate":true,"add.footnote":true,"summary.report":true},"dis.legomenon":{"transliterate":false,"add.footnote":false,"summary.report":true},"tris.legomenon":{"forceDisplay":true,"transliterate":false,"add.footnote":false,"summary.report":false}}}}}} +do_execsql_test json101-12.120 { + SELECT json_extract(x, '$.settings.layer2."tris.legomenon"."summary.report"') + FROM t12; +} {0} +do_execsql_test json101-12.120b { + SELECT json_extract(jsonb(x), '$.settings.layer2."tris.legomenon"."summary.report"') + FROM t12; +} {0} + +# 2018-01-26 +# ticket https://sqlite.org/src/tktview/80177f0c226ff54f6ddd41 +# Make sure the query planner knows about the arguments to table-valued functions. +# +do_execsql_test json101-13.100 { + DROP TABLE IF EXISTS t1; + DROP TABLE IF EXISTS t2; + CREATE TABLE t1(id, json); + INSERT INTO t1(id,json) VALUES(1,'{"items":[3,5]}'); + CREATE TABLE t2(id, json); + INSERT INTO t2(id,json) VALUES(2,'{"value":2}'); + INSERT INTO t2(id,json) VALUES(3,'{"value":3}'); + INSERT INTO t2(id,json) VALUES(4,'{"value":4}'); + INSERT INTO t2(id,json) VALUES(5,'{"value":5}'); + INSERT INTO t2(id,json) VALUES(6,'{"value":6}'); + SELECT *, 'NL' FROM t1 CROSS JOIN t2 + WHERE EXISTS(SELECT 1 FROM json_each(t1.json,'$.items') AS Z + WHERE Z.value==t2.id); +} {1 {{"items":[3,5]}} 3 {{"value":3}} NL 1 {{"items":[3,5]}} 5 {{"value":5}} NL} +do_execsql_test json101-13.110 { + SELECT *, 'NL' FROM t2 CROSS JOIN t1 + WHERE EXISTS(SELECT 1 FROM json_each(t1.json,'$.items') AS Z + WHERE Z.value==t2.id); +} {3 {{"value":3}} 1 {{"items":[3,5]}} NL 5 {{"value":5}} 1 {{"items":[3,5]}} NL} + +# 2018-05-16 +# Incorrect fullkey output from json_each() +# when the input JSON is not an array or object. +# +do_execsql_test json101-14.100 { + SELECT fullkey FROM json_each('123'); +} {$} +do_execsql_test json101-14.110 { + SELECT fullkey FROM json_each('123.56'); +} {$} +do_execsql_test json101-14.120 { + SELECT fullkey FROM json_each('"hello"'); +} {$} +do_execsql_test json101-14.130 { + SELECT fullkey FROM json_each('null'); +} {$} +do_execsql_test json101-14.140 { + SELECT fullkey FROM json_tree('123'); +} {$} +do_execsql_test json101-14.150 { + SELECT fullkey FROM json_tree('123.56'); +} {$} +do_execsql_test json101-14.160 { + SELECT fullkey FROM json_tree('"hello"'); +} {$} +do_execsql_test json101-14.170 { + SELECT fullkey FROM json_tree('null'); +} {$} + +# 2018-12-03 +# Make sure the table-valued functions contained within parentheses +# work correctly. +# +# Bug reported via private email. See TH3 for more information. +# +do_execsql_test json101-15.100 { + SELECT * FROM JSON_EACH('{"a":1, "b":2}'); +} {a 1 integer 1 1 {} {$.a} {$} b 2 integer 2 5 {} {$.b} {$}} +do_execsql_test json101-15.110 { + SELECT xyz.* FROM JSON_EACH('{"a":1, "b":2}') AS xyz; +} {a 1 integer 1 1 {} {$.a} {$} b 2 integer 2 5 {} {$.b} {$}} +do_execsql_test json101-15.120 { + SELECT * FROM (JSON_EACH('{"a":1, "b":2}')); +} {a 1 integer 1 1 {} {$.a} {$} b 2 integer 2 5 {} {$.b} {$}} +do_execsql_test json101-15.130 { + SELECT xyz.* FROM (JSON_EACH('{"a":1, "b":2}')) AS xyz; +} {a 1 integer 1 1 {} {$.a} {$} b 2 integer 2 5 {} {$.b} {$}} + +# 2019-11-10 +# Mailing list bug report on the handling of surrogate pairs +# in JSON. +# +do_execsql_test json101-16.10 { + SELECT length(json_extract('"abc\uD834\uDD1Exyz"','$')); +} {7} +do_execsql_test json101-16.20 { + SELECT length(json_extract('"\uD834\uDD1E"','$')); +} {1} +do_execsql_test json101-16.30 { + SELECT unicode(json_extract('"\uD834\uDD1E"','$')); +} {119070} + +# 2022-01-30 dbsqlfuzz 4678cf825d27f87c9b8343720121e12cf944b71a +do_execsql_test json101-17.1 { + DROP TABLE IF EXISTS t1; + DROP TABLE IF EXISTS t2; + CREATE TABLE t1(a,b,c); + CREATE TABLE t2(d); + SELECT * FROM t1 LEFT JOIN t2 ON (SELECT b FROM json_each ORDER BY 1); +} {} + +# 2022-04-04 forum post https://sqlite.org/forum/forumpost/c082aeab43 +do_execsql_test json101-18.1 { + SELECT json_valid('{"":5}'); +} {1} +do_execsql_test json101-18.2 { + SELECT json_extract('{"":5}', '$.""'); +} {5} +do_execsql_test json101-18.3 { + SELECT json_extract('[3,{"a":4,"":[5,{"hi":6},7]},8]', '$[1].""[1].hi'); +} {6} +do_execsql_test json101-18.4 { + SELECT json_extract('[3,{"a":4,"":[5,{"hi":6},7]},8]', '$[1].""[1]."hi"'); +} {6} +do_catchsql_test json101-18.5 { + SELECT json_extract('{"":8}', '$.'); +} {1 {bad JSON path: '$.'}} + +# 2022-08-29 https://sqlite.org/forum/forumpost/9b9e4716c0d7bbd1 +# This is not a problem specifically with JSON functions. It is +# a problem with transaction control. But the json() function makes +# the problem more easily accessible, so it is tested here. +# +do_execsql_test json101-19.1 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(x); +} {} +do_catchsql_test json101-19.2 { + BEGIN; + INSERT INTO t1 VALUES(0), (json('not-valid-json')); +} {1 {malformed JSON}} +do_execsql_test json101-19.3 { + COMMIT; + SELECT * FROM t1; +} {} + +# 2023-03-17 positive and negative infinities +# +do_execsql_test json101-20.1 { + SELECT json_object('a',2e370,'b',-3e380); +} {{{"a":9.0e+999,"b":-9.0e+999}}} +do_execsql_test json101-20.2 { + SELECT json_object('a',2e370,'b',-3e380)->>'a'; +} Inf +do_execsql_test json101-20.3 { + SELECT json_object('a',2e370,'b',-3e380)->>'b'; +} {-Inf} + +# 2023-05-02 https://sqlite.org/forum/forumpost/06c6334412 +# JSON functions should normally return NULL when given +# a NULL value as the JSON input. +# +db null NULL +if {[db exists {SELECT * FROM pragma_compile_options WHERE compile_options LIKE '%legacy_json_valid%'}]} { + do_execsql_test json101-21.1-legacy { + SELECT json_valid(NULL); + } 0 +} else { + do_execsql_test json101-21.1-correct { + SELECT json_valid(NULL); + } NULL +} +do_execsql_test json101-21.2 { + SELECT json_error_position(NULL); +} NULL +do_execsql_test json101-21.3 { + SELECT json(NULL); +} NULL +do_execsql_test json101-21.4 { + SELECT json_array(NULL); +} {[null]} +do_execsql_test json101-21.5 { + SELECT json_extract(NULL); +} NULL +do_execsql_test json101-21.6 { + SELECT json_insert(NULL,'$',123); +} NULL +do_execsql_test json101-21.7 { + SELECT NULL->0; +} NULL +do_execsql_test json101-21.8 { + SELECT NULL->>0; +} NULL +do_execsql_test json101-21.9 { + SELECT '{a:5}'->NULL; +} NULL +do_execsql_test json101-21.10 { + SELECT '{a:5}'->>NULL; +} NULL +do_catchsql_test json101-21.11 { + SELECT json_object(NULL,5); +} {1 {json_object() labels must be TEXT}} +do_execsql_test json101-21.12 { + SELECT json_patch(NULL,'{a:5}'); +} NULL +do_execsql_test json101-21.13 { + SELECT json_patch('{a:5}',NULL); +} NULL +do_execsql_test json101-21.14 { + SELECT json_patch(NULL,NULL); +} NULL +do_execsql_test json101-21.15 { + SELECT json_remove(NULL,'$'); +} NULL +do_execsql_test json101-21.16 { + SELECT json_remove('{a:5,b:7}',NULL); +} NULL +do_execsql_test json101-21.17 { + SELECT json_replace(NULL,'$.a',123); +} NULL +do_execsql_test json101-21.18 { + SELECT json_replace('{a:5,b:7}',NULL,NULL); +} {{{"a":5,"b":7}}} +do_execsql_test json101-21.19 { + SELECT json_set(NULL,'$.a',123); +} NULL +do_execsql_test json101-21.20 { + SELECT json_set('{a:5,b:7}',NULL,NULL); +} {{{"a":5,"b":7}}} +do_execsql_test json101-21.21 { + SELECT json_type(NULL); +} NULL +do_execsql_test json101-21.22 { + SELECT json_type('{a:5,b:7}',NULL); +} NULL +do_execsql_test json101-21.23 { + SELECT json_quote(NULL); +} null +do_execsql_test json101-21.24 { + SELECT count(*) FROM json_each(NULL); +} 0 +do_execsql_test json101-21.25 { + SELECT count(*) FROM json_tree(NULL); +} 0 +do_execsql_test json101-21.26 { + WITH c(x) AS (VALUES(1),(2.0),(NULL),('three')) + SELECT json_group_array(x) FROM c; +} {[1,2.0,null,"three"]} +do_execsql_test json101-21.27 { + WITH c(x,y) AS (VALUES('a',1),('b',2.0),('c',NULL),(NULL,'three'),('e','four')) + SELECT json_group_object(x,y) FROM c; +} {{{"a":1,"b":2.0,"c":null,"e":"four"}}} + +# 2023-10-09 https://sqlite.org/forum/forumpost/b25edc1d46 +# UAF due to JSON cache overflow +# +do_execsql_test json101-22.1 { + SELECT json_set( + '{}', + '$.a', json('1'), + '$.a', json('2'), + '$.b', json('3'), + '$.b', json('4'), + '$.c', json('5'), + '$.c', json('6') + ); +} {{{"a":2,"b":4,"c":6}}} +do_execsql_test json101-22.2 { + SELECT json_replace( + '{"a":7,"b":8,"c":9}', + '$.a', json('1'), + '$.a', json('2'), + '$.b', json('3'), + '$.b', json('4'), + '$.c', json('5'), + '$.c', json('6') + ); +} {{{"a":2,"b":4,"c":6}}} + +# 2023-10-17 https://sqlite.org/forum/forumpost/fc0e3f1e2a +# Incorrect accesss to '$[0]' in parsed + edited JSON. +# +do_execsql_test json101-23.1 { + SELECT j, j->>0, j->>1 + FROM (SELECT json_set(json_set('[]','$[#]',0), '$[#]',1) AS j); +} {{[0,1]} 0 1} +do_execsql_test json101-23.2 { + SELECT j, j->>0, j->>1 + FROM (SELECT json_set('[]','$[#]',0,'$[#]',1) AS j); +} {{[0,1]} 0 1} + +# Insert/Set/Replace where the path specifies substructure that +# does not yet exist +# +proc tx x {return [string map [list ( \173 ) \175 ' \042 < \133 > \135] $x]} +foreach {id start path ins set repl} { + 1 {{}} {$.a.b.c} ('a':('b':('c':9))) ('a':('b':('c':9))) () + 2 {{a:4}} {$.a.b.c} ('a':4) ('a':4) ('a':4) + 3 {{a:{}}} {$.a.b.c} ('a':('b':('c':9))) ('a':('b':('c':9))) ('a':()) + 4 {[0,1,2]} {$[3].a[0].b} <0,1,2,('a':<('b':9)>)> <0,1,2,('a':<('b':9)>)> <0,1,2> + 5 {[0,1,2]} {$[1].a[0].b} <0,1,2> <0,1,2> <0,1,2> + 6 {[0,{},2]} {$[1].a[0].b} <0,('a':<('b':9)>),2> <0,('a':<('b':9)>),2> <0,(),2> + 7 {[0,1,2]} {$[3][0].b} <0,1,2,<('b':9)>> <0,1,2,<('b':9)>> <0,1,2> + 8 {[0,1,2]} {$[1][0].b} <0,1,2> <0,1,2> <0,1,2> +} { + do_execsql_test json101-24.$id.insert { + SELECT json_insert($start,$path,9); + } [list [tx $ins]] + do_execsql_test json101-24.$id.set { + SELECT json_set($start,$path,9); + } [list [tx $set]] + do_execsql_test json101-24.$id.replace { + SELECT json_replace($start,$path,9); + } [list [tx $repl]] +} finish_test diff --git a/test/json102.test b/test/json102.test index a4d88dbeaa..54a0e1e0e0 100644 --- a/test/json102.test +++ b/test/json102.test @@ -18,164 +18,495 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl -ifcapable !json1 { - finish_test - return -} - do_execsql_test json102-100 { SELECT json_object('ex','[52,3.14159]'); } {{{"ex":"[52,3.14159]"}}} +do_execsql_test json102-100b { + SELECT json(jsonb_object('ex','[52,3.14159]')); +} {{{"ex":"[52,3.14159]"}}} do_execsql_test json102-110 { SELECT json_object('ex',json('[52,3.14159]')); } {{{"ex":[52,3.14159]}}} +do_execsql_test json102-110-2 { + SELECT json(jsonb_object('ex',json('[52,3.14159]'))); +} {{{"ex":[52,3.14159]}}} +do_execsql_test json102-110-3 { + SELECT json_object('ex',jsonb('[52,3.14159]')); +} {{{"ex":[52,3.14159]}}} +do_execsql_test json102-110-3 { + SELECT json(jsonb_object('ex',jsonb('[52,3.14159]'))); +} {{{"ex":[52,3.14159]}}} do_execsql_test json102-120 { SELECT json_object('ex',json_array(52,3.14159)); } {{{"ex":[52,3.14159]}}} +do_execsql_test json102-120-2 { + SELECT json(jsonb_object('ex',json_array(52,3.14159))); +} {{{"ex":[52,3.14159]}}} +do_execsql_test json102-120-3 { + SELECT json_object('ex',jsonb_array(52,3.14159)); +} {{{"ex":[52,3.14159]}}} +do_execsql_test json102-120-4 { + SELECT json(jsonb_object('ex',jsonb_array(52,3.14159))); +} {{{"ex":[52,3.14159]}}} do_execsql_test json102-130 { SELECT json(' { "this" : "is", "a": [ "test" ] } '); } {{{"this":"is","a":["test"]}}} +do_execsql_test json102-130b { + SELECT json(jsonb(' { "this" : "is", "a": [ "test" ] } ')); +} {{{"this":"is","a":["test"]}}} do_execsql_test json102-140 { SELECT json_array(1,2,'3',4); } {{[1,2,"3",4]}} +do_execsql_test json102-140b { + SELECT json(jsonb_array(1,2,'3',4)); +} {{[1,2,"3",4]}} do_execsql_test json102-150 { SELECT json_array('[1,2]'); } {{["[1,2]"]}} +do_execsql_test json102-150b { + SELECT json(jsonb_array('[1,2]')); +} {{["[1,2]"]}} do_execsql_test json102-160 { SELECT json_array(json_array(1,2)); } {{[[1,2]]}} +do_execsql_test json102-160-2 { + SELECT json_array(jsonb_array(1,2)); +} {{[[1,2]]}} +do_execsql_test json102-160-3 { + SELECT json(jsonb_array(json_array(1,2))); +} {{[[1,2]]}} +do_execsql_test json102-160-4 { + SELECT json(jsonb_array(jsonb_array(1,2))); +} {{[[1,2]]}} do_execsql_test json102-170 { SELECT json_array(1,null,'3','[4,5]','{"six":7.7}'); } {{[1,null,"3","[4,5]","{\"six\":7.7}"]}} +do_execsql_test json102-170b { + SELECT json(jsonb_array(1,null,'3','[4,5]','{"six":7.7}')); +} {{[1,null,"3","[4,5]","{\"six\":7.7}"]}} do_execsql_test json102-180 { SELECT json_array(1,null,'3',json('[4,5]'),json('{"six":7.7}')); } {{[1,null,"3",[4,5],{"six":7.7}]}} +do_execsql_test json102-180-2 { + SELECT json_array(1,null,'3',jsonb('[4,5]'),json('{"six":7.7}')); +} {{[1,null,"3",[4,5],{"six":7.7}]}} +do_execsql_test json102-180-3 { + SELECT json(jsonb_array(1,null,'3',json('[4,5]'),json('{"six":7.7}'))); +} {{[1,null,"3",[4,5],{"six":7.7}]}} +do_execsql_test json102-180-4 { + SELECT json(jsonb_array(1,null,'3',jsonb('[4,5]'),jsonb('{"six":7.7}'))); +} {{[1,null,"3",[4,5],{"six":7.7}]}} do_execsql_test json102-190 { SELECT json_array_length('[1,2,3,4]'); } {{4}} +do_execsql_test json102-190b { + SELECT json_array_length(jsonb('[1,2,3,4]')); +} {{4}} +do_execsql_test json102-191 { + SELECT json_array_length( json_remove('[1,2,3,4]','$[2]') ); +} {{3}} +do_execsql_test json102-191b { + SELECT json_array_length( jsonb_remove('[1,2,3,4]','$[2]') ); +} {{3}} do_execsql_test json102-200 { SELECT json_array_length('[1,2,3,4]', '$'); } {{4}} +do_execsql_test json102-200b { + SELECT json_array_length(jsonb('[1,2,3,4]'), '$'); +} {{4}} do_execsql_test json102-210 { SELECT json_array_length('[1,2,3,4]', '$[2]'); } {{0}} +do_execsql_test json102-210b { + SELECT json_array_length(jsonb('[1,2,3,4]'), '$[2]'); +} {{0}} +do_execsql_test json102-220 { + SELECT json_array_length('{"one":[1,2,3]}'); +} {{0}} do_execsql_test json102-220 { SELECT json_array_length('{"one":[1,2,3]}'); } {{0}} -do_execsql_test json102-230 { - SELECT json_array_length('{"one":[1,2,3]}', '$.one'); +do_execsql_test json102-230b { + SELECT json_array_length(jsonb('{"one":[1,2,3]}'), '$.one'); } {{3}} do_execsql_test json102-240 { SELECT json_array_length('{"one":[1,2,3]}', '$.two'); } {{}} +do_execsql_test json102-240b { + SELECT json_array_length(jsonb('{"one":[1,2,3]}'), '$.two'); +} {{}} do_execsql_test json102-250 { SELECT json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$'); } {{{"a":2,"c":[4,5,{"f":7}]}}} +do_execsql_test json102-250-2 { + SELECT json_extract(jsonb('{"a":2,"c":[4,5,{"f":7}]}'), '$'); +} {{{"a":2,"c":[4,5,{"f":7}]}}} +do_execsql_test json102-250-3 { + SELECT json(jsonb_extract('{"a":2,"c":[4,5,{"f":7}]}', '$')); +} {{{"a":2,"c":[4,5,{"f":7}]}}} +do_execsql_test json102-250-4 { + SELECT json(jsonb_extract(jsonb('{"a":2,"c":[4,5,{"f":7}]}'), '$')); +} {{{"a":2,"c":[4,5,{"f":7}]}}} do_execsql_test json102-260 { SELECT json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$.c'); } {{[4,5,{"f":7}]}} +do_execsql_test json102-260-2 { + SELECT json_extract(jsonb('{"a":2,"c":[4,5,{"f":7}]}'), '$.c'); +} {{[4,5,{"f":7}]}} +do_execsql_test json102-260-3 { + SELECT json(jsonb_extract('{"a":2,"c":[4,5,{"f":7}]}', '$.c')); +} {{[4,5,{"f":7}]}} +do_execsql_test json102-260-4 { + SELECT json(jsonb_extract(jsonb('{"a":2,"c":[4,5,{"f":7}]}'), '$.c')); +} {{[4,5,{"f":7}]}} do_execsql_test json102-270 { SELECT json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$.c[2]'); } {{{"f":7}}} +do_execsql_test json102-270-2 { + SELECT json_extract(jsonb('{"a":2,"c":[4,5,{"f":7}]}'), '$.c[2]'); +} {{{"f":7}}} +do_execsql_test json102-270-3 { + SELECT json(jsonb_extract(jsonb('{"a":2,"c":[4,5,{"f":7}]}'), '$.c[2]')); +} {{{"f":7}}} +do_execsql_test json102-270-4 { + SELECT json(jsonb_extract('{"a":2,"c":[4,5,{"f":7}]}', '$.c[2]')); +} {{{"f":7}}} do_execsql_test json102-280 { SELECT json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$.c[2].f'); } {{7}} +do_execsql_test json102-280b { + SELECT jsonb_extract('{"a":2,"c":[4,5,{"f":7}]}', '$.c[2].f'); +} {{7}} do_execsql_test json102-290 { SELECT json_extract('{"a":2,"c":[4,5],"f":7}','$.c','$.a'); } {{[[4,5],2]}} +do_execsql_test json102-290-2 { + SELECT json_extract(jsonb('{"a":2,"c":[4,5],"f":7}'),'$.c','$.a'); +} {{[[4,5],2]}} +do_execsql_test json102-290-3 { + SELECT json(jsonb_extract('{"a":2,"c":[4,5],"f":7}','$.c','$.a')); +} {{[[4,5],2]}} +do_execsql_test json102-290-4 { + SELECT json(jsonb_extract(jsonb('{"a":2,"c":[4,5],"f":7}'),'$.c','$.a')); +} {{[[4,5],2]}} do_execsql_test json102-300 { SELECT json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$.x'); } {{}} +do_execsql_test json102-300b { + SELECT jsonb_extract('{"a":2,"c":[4,5,{"f":7}]}', '$.x'); +} {{}} do_execsql_test json102-310 { SELECT json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$.x', '$.a'); } {{[null,2]}} +do_execsql_test json102-310-2 { + SELECT json_extract(jsonb('{"a":2,"c":[4,5,{"f":7}]}'), '$.x', '$.a'); +} {{[null,2]}} +do_execsql_test json102-310-3 { + SELECT json(jsonb_extract(jsonb('{"a":2,"c":[4,5,{"f":7}]}'), '$.x', '$.a')); +} {{[null,2]}} +do_execsql_test json102-310-43 { + SELECT json(jsonb_extract('{"a":2,"c":[4,5,{"f":7}]}', '$.x', '$.a')); +} {{[null,2]}} do_execsql_test json102-320 { SELECT json_insert('{"a":2,"c":4}', '$.a', 99); } {{{"a":2,"c":4}}} +do_execsql_test json102-320-2 { + SELECT json_insert(jsonb('{"a":2,"c":4}'), '$.a', 99); +} {{{"a":2,"c":4}}} +do_execsql_test json102-320-3 { + SELECT json(jsonb_insert('{"a":2,"c":4}', '$.a', 99)); +} {{{"a":2,"c":4}}} +do_execsql_test json102-320-4 { + SELECT json(jsonb_insert(jsonb('{"a":2,"c":4}'), '$.a', 99)); +} {{{"a":2,"c":4}}} do_execsql_test json102-330 { SELECT json_insert('{"a":2,"c":4}', '$.e', 99); } {{{"a":2,"c":4,"e":99}}} +do_execsql_test json102-330-2 { + SELECT json_insert(jsonb('{"a":2,"c":4}'), '$.e', 99); +} {{{"a":2,"c":4,"e":99}}} +do_execsql_test json102-330-3 { + SELECT json(jsonb_insert('{"a":2,"c":4}', '$.e', 99)); +} {{{"a":2,"c":4,"e":99}}} +do_execsql_test json102-330-4 { + SELECT json(jsonb_insert(jsonb('{"a":2,"c":4}'), '$.e', 99)); +} {{{"a":2,"c":4,"e":99}}} do_execsql_test json102-340 { SELECT json_replace('{"a":2,"c":4}', '$.a', 99); } {{{"a":99,"c":4}}} +do_execsql_test json102-340-2 { + SELECT json_replace(jsonb('{"a":2,"c":4}'), '$.a', 99); +} {{{"a":99,"c":4}}} +do_execsql_test json102-340-3 { + SELECT json(jsonb_replace('{"a":2,"c":4}', '$.a', 99)); +} {{{"a":99,"c":4}}} +do_execsql_test json102-340-4 { + SELECT json(jsonb_replace(jsonb('{"a":2,"c":4}'), '$.a', 99)); +} {{{"a":99,"c":4}}} do_execsql_test json102-350 { SELECT json_replace('{"a":2,"c":4}', '$.e', 99); } {{{"a":2,"c":4}}} +do_execsql_test json102-350-2 { + SELECT json_replace(jsonb('{"a":2,"c":4}'), '$.e', 99); +} {{{"a":2,"c":4}}} +do_execsql_test json102-350-3 { + SELECT json(jsonb_replace('{"a":2,"c":4}', '$.e', 99)); +} {{{"a":2,"c":4}}} +do_execsql_test json102-350-4 { + SELECT json(jsonb_replace(jsonb('{"a":2,"c":4}'), '$.e', 99)); +} {{{"a":2,"c":4}}} do_execsql_test json102-360 { SELECT json_set('{"a":2,"c":4}', '$.a', 99); } {{{"a":99,"c":4}}} +do_execsql_test json102-360-2 { + SELECT json_set(jsonb('{"a":2,"c":4}'), '$.a', 99); +} {{{"a":99,"c":4}}} +do_execsql_test json102-360-3 { + SELECT json(jsonb_set('{"a":2,"c":4}', '$.a', 99)); +} {{{"a":99,"c":4}}} +do_execsql_test json102-360-4 { + SELECT json(jsonb_set(jsonb('{"a":2,"c":4}'), '$.a', 99)); +} {{{"a":99,"c":4}}} do_execsql_test json102-370 { SELECT json_set('{"a":2,"c":4}', '$.e', 99); } {{{"a":2,"c":4,"e":99}}} +do_execsql_test json102-370-2 { + SELECT json_set(jsonb('{"a":2,"c":4}'), '$.e', 99); +} {{{"a":2,"c":4,"e":99}}} +do_execsql_test json102-370-3 { + SELECT json(jsonb_set('{"a":2,"c":4}', '$.e', 99)); +} {{{"a":2,"c":4,"e":99}}} +do_execsql_test json102-370-4 { + SELECT json(jsonb_set(jsonb('{"a":2,"c":4}'), '$.e', 99)); +} {{{"a":2,"c":4,"e":99}}} do_execsql_test json102-380 { SELECT json_set('{"a":2,"c":4}', '$.c', '[97,96]'); } {{{"a":2,"c":"[97,96]"}}} +do_execsql_test json102-380-2 { + SELECT json_set(jsonb('{"a":2,"c":4}'), '$.c', '[97,96]'); +} {{{"a":2,"c":"[97,96]"}}} +do_execsql_test json102-380-3 { + SELECT json(jsonb_set('{"a":2,"c":4}', '$.c', '[97,96]')); +} {{{"a":2,"c":"[97,96]"}}} +do_execsql_test json102-380-4 { + SELECT json(jsonb_set(jsonb('{"a":2,"c":4}'), '$.c', '[97,96]')); +} {{{"a":2,"c":"[97,96]"}}} do_execsql_test json102-390 { SELECT json_set('{"a":2,"c":4}', '$.c', json('[97,96]')); } {{{"a":2,"c":[97,96]}}} +do_execsql_test json102-390-2 { + SELECT json_set(jsonb('{"a":2,"c":4}'), '$.c', json('[97,96]')); +} {{{"a":2,"c":[97,96]}}} +do_execsql_test json102-390-3 { + SELECT json(jsonb_set('{"a":2,"c":4}', '$.c', json('[97,96]'))); +} {{{"a":2,"c":[97,96]}}} +do_execsql_test json102-390-4 { + SELECT json(jsonb_set(jsonb('{"a":2,"c":4}'), '$.c', json('[97,96]'))); +} {{{"a":2,"c":[97,96]}}} +do_execsql_test json102-390-5 { + SELECT json_set('{"a":2,"c":4}', '$.c', jsonb('[97,96]')); +} {{{"a":2,"c":[97,96]}}} +do_execsql_test json102-390-6 { + SELECT json_set(jsonb('{"a":2,"c":4}'), '$.c', jsonb('[97,96]')); +} {{{"a":2,"c":[97,96]}}} +do_execsql_test json102-390-7 { + SELECT json(jsonb_set('{"a":2,"c":4}', '$.c', jsonb('[97,96]'))); +} {{{"a":2,"c":[97,96]}}} +do_execsql_test json102-390-8 { + SELECT json(jsonb_set(jsonb('{"a":2,"c":4}'), '$.c', jsonb('[97,96]'))); +} {{{"a":2,"c":[97,96]}}} do_execsql_test json102-400 { SELECT json_set('{"a":2,"c":4}', '$.c', json_array(97,96)); } {{{"a":2,"c":[97,96]}}} +do_execsql_test json102-400-2 { + SELECT json_set(jsonb('{"a":2,"c":4}'), '$.c', json_array(97,96)); +} {{{"a":2,"c":[97,96]}}} +do_execsql_test json102-400-3 { + SELECT json(jsonb_set('{"a":2,"c":4}', '$.c', json_array(97,96))); +} {{{"a":2,"c":[97,96]}}} +do_execsql_test json102-400-4 { + SELECT json(jsonb_set(jsonb('{"a":2,"c":4}'), '$.c', json_array(97,96))); +} {{{"a":2,"c":[97,96]}}} +do_execsql_test json102-400-5 { + SELECT json_set('{"a":2,"c":4}', '$.c', jsonb_array(97,96)); +} {{{"a":2,"c":[97,96]}}} +do_execsql_test json102-400-6 { + SELECT json_set(jsonb('{"a":2,"c":4}'), '$.c', jsonb_array(97,96)); +} {{{"a":2,"c":[97,96]}}} +do_execsql_test json102-400-7 { + SELECT json(jsonb_set('{"a":2,"c":4}', '$.c', jsonb_array(97,96))); +} {{{"a":2,"c":[97,96]}}} +do_execsql_test json102-400-8 { + SELECT json(jsonb_set(jsonb('{"a":2,"c":4}'), '$.c', jsonb_array(97,96))); +} {{{"a":2,"c":[97,96]}}} do_execsql_test json102-410 { SELECT json_object('a',2,'c',4); } {{{"a":2,"c":4}}} +do_execsql_test json102-410b { + SELECT json(jsonb_object('a',2,'c',4)); +} {{{"a":2,"c":4}}} do_execsql_test json102-420 { SELECT json_object('a',2,'c','{e:5}'); } {{{"a":2,"c":"{e:5}"}}} +do_execsql_test json102-420b { + SELECT json(jsonb_object('a',2,'c','{e:5}')); +} {{{"a":2,"c":"{e:5}"}}} do_execsql_test json102-430 { SELECT json_object('a',2,'c',json_object('e',5)); } {{{"a":2,"c":{"e":5}}}} +do_execsql_test json102-430-2 { + SELECT json(jsonb_object('a',2,'c',json_object('e',5))); +} {{{"a":2,"c":{"e":5}}}} +do_execsql_test json102-430-3 { + SELECT json_object('a',2,'c',jsonb_object('e',5)); +} {{{"a":2,"c":{"e":5}}}} +do_execsql_test json102-430-4 { + SELECT json(jsonb_object('a',2,'c',jsonb_object('e',5))); +} {{{"a":2,"c":{"e":5}}}} do_execsql_test json102-440 { SELECT json_remove('[0,1,2,3,4]','$[2]'); } {{[0,1,3,4]}} +do_execsql_test json102-440-2 { + SELECT json_remove(jsonb('[0,1,2,3,4]'),'$[2]'); +} {{[0,1,3,4]}} +do_execsql_test json102-440-3 { + SELECT json(jsonb_remove('[0,1,2,3,4]','$[2]')); +} {{[0,1,3,4]}} +do_execsql_test json102-440-4 { + SELECT json(jsonb_remove(jsonb('[0,1,2,3,4]'),'$[2]')); +} {{[0,1,3,4]}} do_execsql_test json102-450 { SELECT json_remove('[0,1,2,3,4]','$[2]','$[0]'); } {{[1,3,4]}} +do_execsql_test json102-450-2 { + SELECT json_remove(jsonb('[0,1,2,3,4]'),'$[2]','$[0]'); +} {{[1,3,4]}} +do_execsql_test json102-450-3 { + SELECT json(jsonb_remove('[0,1,2,3,4]','$[2]','$[0]')); +} {{[1,3,4]}} +do_execsql_test json102-450-4 { + SELECT json(jsonb_remove(jsonb('[0,1,2,3,4]'),'$[2]','$[0]')); +} {{[1,3,4]}} do_execsql_test json102-460 { SELECT json_remove('[0,1,2,3,4]','$[0]','$[2]'); } {{[1,2,4]}} +do_execsql_test json102-460-2 { + SELECT json_remove(jsonb('[0,1,2,3,4]'),'$[0]','$[2]'); +} {{[1,2,4]}} +do_execsql_test json102-460-3 { + SELECT json(jsonb_remove('[0,1,2,3,4]','$[0]','$[2]')); +} {{[1,2,4]}} +do_execsql_test json102-460-4 { + SELECT json(jsonb_remove(jsonb('[0,1,2,3,4]'),'$[0]','$[2]')); +} {{[1,2,4]}} do_execsql_test json102-470 { SELECT json_remove('{"x":25,"y":42}'); } {{{"x":25,"y":42}}} +do_execsql_test json102-470-2 { + SELECT json_remove(jsonb('{"x":25,"y":42}')); +} {{{"x":25,"y":42}}} +do_execsql_test json102-470-3 { + SELECT json(jsonb_remove('{"x":25,"y":42}')); +} {{{"x":25,"y":42}}} +do_execsql_test json102-470-4 { + SELECT json(jsonb_remove(jsonb('{"x":25,"y":42}'))); +} {{{"x":25,"y":42}}} do_execsql_test json102-480 { SELECT json_remove('{"x":25,"y":42}','$.z'); } {{{"x":25,"y":42}}} +do_execsql_test json102-480-2 { + SELECT json_remove(jsonb('{"x":25,"y":42}'),'$.z'); +} {{{"x":25,"y":42}}} +do_execsql_test json102-480-3 { + SELECT json(jsonb_remove('{"x":25,"y":42}','$.z')); +} {{{"x":25,"y":42}}} +do_execsql_test json102-480-4 { + SELECT json(jsonb_remove(jsonb('{"x":25,"y":42}'),'$.z')); +} {{{"x":25,"y":42}}} do_execsql_test json102-490 { SELECT json_remove('{"x":25,"y":42}','$.y'); } {{{"x":25}}} +do_execsql_test json102-490-2 { + SELECT json_remove(jsonb('{"x":25,"y":42}'),'$.y'); +} {{{"x":25}}} +do_execsql_test json102-490-3 { + SELECT json(jsonb_remove('{"x":25,"y":42}','$.y')); +} {{{"x":25}}} +do_execsql_test json102-490-4 { + SELECT json(jsonb_remove(jsonb('{"x":25,"y":42}'),'$.y')); +} {{{"x":25}}} do_execsql_test json102-500 { SELECT json_remove('{"x":25,"y":42}','$'); } {{}} +do_execsql_test json102-500-2 { + SELECT json_remove(jsonb('{"x":25,"y":42}'),'$'); +} {{}} +do_execsql_test json102-500-3 { + SELECT json(jsonb_remove('{"x":25,"y":42}','$')); +} {{}} +do_execsql_test json102-500-4 { + SELECT json(jsonb_remove(jsonb('{"x":25,"y":42}'),'$')); +} {{}} do_execsql_test json102-510 { SELECT json_type('{"a":[2,3.5,true,false,null,"x"]}'); } {{object}} +do_execsql_test json102-510b { + SELECT json_type(x'cc0f1761cb0b133235332e350102001778'); +} {{object}} do_execsql_test json102-520 { SELECT json_type('{"a":[2,3.5,true,false,null,"x"]}','$'); } {{object}} +do_execsql_test json102-520b { + SELECT json_type(x'cc0f1761cb0b133235332e350102001778','$'); +} {{object}} do_execsql_test json102-530 { SELECT json_type('{"a":[2,3.5,true,false,null,"x"]}','$.a'); } {{array}} +do_execsql_test json102-530b { + SELECT json_type(x'cc0f1761cb0b133235332e350102001778','$.a'); +} {{array}} do_execsql_test json102-540 { SELECT json_type('{"a":[2,3.5,true,false,null,"x"]}','$.a[0]'); } {{integer}} +do_execsql_test json102-540b { + SELECT json_type(x'cc0f1761cb0b133235332e350102001778','$.a[0]'); +} {{integer}} do_execsql_test json102-550 { SELECT json_type('{"a":[2,3.5,true,false,null,"x"]}','$.a[1]'); } {{real}} +do_execsql_test json102-550b { + SELECT json_type(x'cc0f1761cb0b133235332e350102001778','$.a[1]'); +} {{real}} do_execsql_test json102-560 { SELECT json_type('{"a":[2,3.5,true,false,null,"x"]}','$.a[2]'); } {{true}} +do_execsql_test json102-560b { + SELECT json_type(x'cc0f1761cb0b133235332e350102001778','$.a[2]'); +} {{true}} do_execsql_test json102-570 { SELECT json_type('{"a":[2,3.5,true,false,null,"x"]}','$.a[3]'); } {{false}} +do_execsql_test json102-570b { + SELECT json_type(x'cc0f1761cb0b133235332e350102001778','$.a[3]'); +} {{false}} do_execsql_test json102-580 { SELECT json_type('{"a":[2,3.5,true,false,null,"x"]}','$.a[4]'); } {{null}} +do_execsql_test json102-580b { + SELECT json_type(x'cc0f1761cb0b133235332e350102001778','$.a[4]'); +} {{null}} do_execsql_test json102-590 { SELECT json_type('{"a":[2,3.5,true,false,null,"x"]}','$.a[5]'); } {{text}} +do_execsql_test json102-590b { + SELECT json_type(x'cc0f1761cb0b133235332e350102001778','$.a[5]'); +} {{text}} do_execsql_test json102-600 { SELECT json_type('{"a":[2,3.5,true,false,null,"x"]}','$.a[6]'); } {{}} +do_execsql_test json102-600b { + SELECT json_type(x'cc0f1761cb0b133235332e350102001778','$.a[6]'); +} {{}} do_execsql_test json102-610 { SELECT json_valid(char(123)||'"x":35'||char(125)); } {{1}} @@ -185,17 +516,24 @@ do_execsql_test json102-620 { ifcapable vtab { do_execsql_test json102-1000 { - CREATE TABLE user(name,phone); + CREATE TABLE user(name,phone,phoneb); INSERT INTO user(name,phone) VALUES ('Alice','["919-555-2345","804-555-3621"]'), ('Bob','["201-555-8872"]'), ('Cindy','["704-555-9983"]'), ('Dave','["336-555-8421","704-555-4321","803-911-4421"]'); + UPDATE user SET phoneb=jsonb(phone); SELECT DISTINCT user.name FROM user, json_each(user.phone) WHERE json_each.value LIKE '704-%' ORDER BY 1; } {Cindy Dave} +do_execsql_test json102-1000b { + SELECT DISTINCT user.name + FROM user, json_each(user.phoneb) + WHERE json_each.value LIKE '704-%' + ORDER BY 1; +} {Cindy Dave} do_execsql_test json102-1010 { UPDATE user @@ -255,6 +593,12 @@ do_execsql_test json102-1110 { WHERE json_tree.type NOT IN ('object','array') ORDER BY +big.rowid, +json_tree.id } $correct_answer +do_execsql_test json102-1110b { + SELECT big.rowid, fullkey, value + FROM big, json_tree(jsonb(big.json)) + WHERE json_tree.type NOT IN ('object','array') + ORDER BY +big.rowid, +json_tree.id +} $correct_answer do_execsql_test json102-1120 { SELECT big.rowid, fullkey, atom FROM big, json_tree(big.json) @@ -298,4 +642,181 @@ for {set i 0} {$i<100} {incr i} { } {1} } +#------------------------------------------------------------------------- +# 2017-04-08 ticket b93be8729a895a528e2849fca99f7 +# JSON extension accepts invalid numeric values +# +# JSON does not allow leading zeros. But the JSON extension was +# allowing them. The following tests verify that the problem is now +# fixed. +# +foreach {id j x0 x5} { + 1401 {'{"x":01}'} 0 0 + 1402 {'{"x":-01}'} 0 0 + 1403 {'{"x":0}'} 1 1 + 1404 {'{"x":-0}'} 1 1 + 1405 {'{"x":0.1}'} 1 1 + 1406 {'{"x":-0.1}'} 1 1 + 1407 {'{"x":0.0000}'} 1 1 + 1408 {'{"x":-0.0000}'} 1 1 + 1409 {'{"x":01.5}'} 0 0 + 1410 {'{"x":-01.5}'} 0 0 + 1411 {'{"x":00}'} 0 0 + 1412 {'{"x":-00}'} 0 0 + 1413 {'{"x":+0}'} 0 1 + 1414 {'{"x":+5}'} 0 1 + 1415 {'{"x":+5.5}'} 0 1 +} { + do_execsql_test json102-$id " + SELECT json_valid($j), NOT json_error_position($j); + " [list $x0 $x5] +} + +#------------------------------------------------------------------------ +# 2017-04-10 ticket 6c9b5514077fed34551f98e64c09a10dc2fc8e16 +# JSON extension accepts strings containing control characters. +# +# The JSON spec requires that all control characters be escaped. +# +do_execsql_test json102-1500 { + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<0x20) + SELECT x FROM c WHERE json_valid(printf('{"a":"x%sz"}', char(x))) ORDER BY x; +} {32} + +# All control characters are escaped +# +do_execsql_test json102-1501 { + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<0x1f) + SELECT sum(json_valid(json_quote('a'||char(x)||'z'))) FROM c ORDER BY x; +} {31} + +# 2022-01-10 tests for -> and ->> operators +# +reset_db +do_execsql_test json102-1600 { + CREATE TABLE t1(id INTEGER PRIMARY KEY, x JSON); + INSERT INTO t1(id,x) VALUES + (1, '{"a":null}'), + (2, '{"a":123}'), + (3, '{"a":4.5}'), + (4, '{"a":"six"}'), + (5, '{"a":[7,8]}'), + (6, '{"a":{"b":9}}'), + (7, '{"b":999}'); + SELECT + id, + x->'a' AS '->', + CASE WHEN subtype(x->'a') THEN 'json' ELSE typeof(x->'a') END AS 'type', + x->>'a' AS '->>', + CASE WHEN subtype(x->>'a') THEN 'json' ELSE typeof(x->>'a') END AS 'type', + json_extract(x,'$.a') AS 'json_extract', + CASE WHEN subtype(json_extract(x,'$.a')) + THEN 'json' ELSE typeof(json_extract(x,'$.a')) END AS 'type' + FROM t1 ORDER BY id; +} [list \ + 1 null json {} null {} null \ + 2 123 json 123 integer 123 integer \ + 3 4.5 json 4.5 real 4.5 real \ + 4 {"six"} json six text six text \ + 5 {[7,8]} json {[7,8]} text {[7,8]} json \ + 6 {{"b":9}} json {{"b":9}} text {{"b":9}} json \ + 7 {} null {} null {} null +] +do_execsql_test json102-1610 { + DELETE FROM t1; + INSERT INTO t1(x) VALUES('[null,123,4.5,"six",[7,8],{"b":9}]'); + WITH c(y) AS (VALUES(0),(1),(2),(3),(4),(5),(6)) + SELECT + y, + x->y AS '->', + CASE WHEN subtype(x->y) THEN 'json' ELSE typeof(x->y) END AS 'type', + x->>y AS '->>', + CASE WHEN subtype(x->>y) THEN 'json' ELSE typeof(x->>y) END AS 'type', + json_extract(x,format('$[%d]',y)) AS 'json_extract', + CASE WHEN subtype(json_extract(x,format('$[%d]',y))) + THEN 'json' ELSE typeof(json_extract(x,format('$[%d]',y))) END AS 'type' + FROM c, t1 ORDER BY y; +} [list \ + 0 null json {} null {} null \ + 1 123 json 123 integer 123 integer \ + 2 4.5 json 4.5 real 4.5 real \ + 3 {"six"} json six text six text \ + 4 {[7,8]} json {[7,8]} text {[7,8]} json \ + 5 {{"b":9}} json {{"b":9}} text {{"b":9}} json \ + 6 {} null {} null {} null +] +do_execsql_test json102-1620 { + DELETE FROM t1; + INSERT INTO t1(x) VALUES('[null,123,4.5,"six",[7,8],{"b":9}]'); + WITH c(y) AS (VALUES(0),(1),(2),(3),(4),(5),(6)) + SELECT + y, + x->y AS '->', + CASE WHEN subtype(if(json_valid(x),x->y)) THEN 'json' + ELSE typeof(x->y) END AS 'type', + x->>y AS '->>', + CASE WHEN subtype(x->>y) THEN 'json' ELSE typeof(x->>y) END AS 'type', + json_extract(x,format('$[%d]',y)) AS 'json_extract', + CASE WHEN subtype(json_extract(x,format('$[%d]',y))) + THEN 'json' ELSE typeof(json_extract(x,format('$[%d]',y))) END AS 'type' + FROM c, t1 ORDER BY y; +} [list \ + 0 null json {} null {} null \ + 1 123 json 123 integer 123 integer \ + 2 4.5 json 4.5 real 4.5 real \ + 3 {"six"} json six text six text \ + 4 {[7,8]} json {[7,8]} text {[7,8]} json \ + 5 {{"b":9}} json {{"b":9}} text {{"b":9}} json \ + 6 {} null {} null {} null +] + +reset_db +do_execsql_test json102-1700 { + CREATE TABLE t1(a1 DATE, a2 INTEGER PRIMARY KEY, a3 INTEGER, memo TEXT); + CREATE INDEX t1x1 ON t1(a3, a1, memo->>'y'); + INSERT INTO t1(a2,a1,a3,memo) VALUES (876, '2023-08-03', 5, '{"x":77,"y":4}'); +} +do_execsql_test json102-1710 { + UPDATE t1 SET memo = JSON_REMOVE(memo, '$.y'); + PRAGMA integrity_check; + SELECT * FROM t1; +} {ok 2023-08-03 876 5 {{"x":77}}} +do_execsql_test json102-1720 { + UPDATE t1 SET memo = JSON_SET(memo, '$.y', 6) + WHERE a2 IN (876) AND JSON_TYPE(memo, '$.y') IS NULL; + PRAGMA integrity_check; + SELECT * FROM t1; +} {ok 2023-08-03 876 5 {{"x":77,"y":6}}} + +# 2024-05-21 https://sqlite.org/forum/forumpost/9e52cdfe15c3926e +# What if the RHS of the -> or ->> operator is a string that looks +# like a number? PostgreSQL treats it as a string. +# +do_execsql_test json102-1800 { + SELECT '{"1":"one","2":"two","3":"three"}'->>'2'; +} two +db null NULL +do_execsql_test json102-1801 { + SELECT '{"1":"one","2":"two","3":"three"}'->>2; +} NULL +do_execsql_test json102-1810 { + SELECT '["zero","one","two"]'->>'1'; +} NULL +do_execsql_test json102-1811 { + SELECT '["zero","one","two"]'->>1; +} one +do_execsql_test json102-1820 { + SELECT '{"1":"one","2":"two","3":"three"}'->'2'; +} {{"two"}} +do_execsql_test json102-1821 { + SELECT '{"1":"one","2":"two","3":"three"}'->2; +} {NULL} +do_execsql_test json102-1830 { + SELECT '["zero","one","two"]'->'1'; +} {NULL} +do_execsql_test json102-1831 { + SELECT '["zero","one","two"]'->1; +} {{"one"}} + + finish_test diff --git a/test/json103.test b/test/json103.test index d7d12e3378..f94217ac10 100644 --- a/test/json103.test +++ b/test/json103.test @@ -14,11 +14,6 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl -ifcapable !json1 { - finish_test - return -} - do_execsql_test json103-100 { CREATE TABLE t1(a,b,c); WITH RECURSIVE c(x) AS (VALUES(1) UNION SELECT x+1 FROM c WHERE x<100) @@ -60,7 +55,7 @@ do_execsql_test json103-220 { WHERE rowid<7 GROUP BY b ORDER BY b; } {0 {{"n3":3,"n6":6}} 1 {{"n1":1,"n4":4}} 2 {{"n2":2,"n5":5}}} -# ticket https://www.sqlite.org/src/info/f45ac567eaa9f93c 2016-01-30 +# ticket https://sqlite.org/src/info/f45ac567eaa9f93c 2016-01-30 # Invalid JSON generated by json_group_array() # # The underlying problem is a failure to reset Mem.eSubtype @@ -75,4 +70,24 @@ do_execsql_test json103-300 { FROM t1; } {{[1,"abc"]} {[{"x":1},{"x":"abc"}]}} +# json_group_array() and json_group_object() work as window functions. +# +ifcapable windowfunc { + do_execsql_test json103-400 { + CREATE TABLE t4(x); + INSERT INTO t4 VALUES + (1), + ('a,b'), + (3), + ('x"y'), + (5), + (6), + (7); + SELECT json_group_array(x) OVER (ROWS 2 PRECEDING) FROM t4; + } {{[1]} {[1,"a,b"]} {[1,"a,b",3]} {["a,b",3,"x\"y"]} {[3,"x\"y",5]} {["x\"y",5,6]} {[5,6,7]}} + do_execsql_test json103-410 { + SELECT json_group_object(rowid, x) OVER (ROWS 2 PRECEDING) FROM t4; + } {{{"1":1}} {{"1":1,"2":"a,b"}} {{"1":1,"2":"a,b","3":3}} {{"2":"a,b","3":3,"4":"x\"y"}} {{"3":3,"4":"x\"y","5":5}} {{"4":"x\"y","5":5,"6":6}} {{"5":5,"6":6,"7":7}}} +} + finish_test diff --git a/test/json104.test b/test/json104.test new file mode 100644 index 0000000000..c3c43d1e98 --- /dev/null +++ b/test/json104.test @@ -0,0 +1,195 @@ +# 2017-03-22 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements tests for json_patch(A,B) SQL function. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix json104 + +# This is the example from pages 2 and 3 of RFC-7396 +do_execsql_test json104-100 { + SELECT json_patch('{ + "a": "b", + "c": { + "d": "e", + "f": "g" + } + }','{ + "a":"z", + "c": { + "f": null + } + }'); +} {{{"a":"z","c":{"d":"e"}}}} +do_execsql_test json104-101 { + SELECT json_patch('{ + "a": "b", + "c": { + "d": "e", + "f": "g" + } + }','{ + a:"z", + c: { + f: null + } + }'); +} {{{"a":"z","c":{"d":"e"}}}} +do_execsql_test json104-102 { + SELECT json_patch('{ + a: "b", + c: { + d: "e", + f: "g" + } + }','{ + "a":"z", + "c": { + "f": null + } + }'); +} {{{"a":"z","c":{"d":"e"}}}} +do_execsql_test json104-103 { + SELECT json_patch('{ + a: "b", + c: { + d: "e", + f: "g" + } + }','{ + a:"z", + c: { + f: null + } + }'); +} {{{"a":"z","c":{"d":"e"}}}} + + +# This is the example from pages 4 and 5 of RFC-7396 +do_execsql_test json104-110 { + SELECT json_patch('{ + "title": "Goodbye!", + "author" : { + "givenName" : "John", + "familyName" : "Doe" + }, + "tags":[ "example", "sample" ], + "content": "This will be unchanged" + }','{ + "title": "Hello!", + "phoneNumber": "+01-123-456-7890", + "author": { + "familyName": null + }, + "tags": [ "example" ] + }'); +} {{{"title":"Hello!","author":{"givenName":"John"},"tags":["example"],"content":"This will be unchanged","phoneNumber":"+01-123-456-7890"}}} + +do_execsql_test json104-200 { + SELECT json_patch('[1,2,3]','{"x":null}'); +} {{{}}} +do_execsql_test json104-210 { + SELECT json_patch('[1,2,3]','{"x":null,"y":1,"z":null}'); +} {{{"y":1}}} +do_execsql_test json104-220 { + SELECT json_patch('{}','{"a":{"bb":{"ccc":null}}}'); +} {{{"a":{"bb":{}}}}} +do_execsql_test json104-221 { + SELECT json_patch('{}','{"a":{"bb":{"ccc":[1,null,3]}}}'); +} {{{"a":{"bb":{"ccc":[1,null,3]}}}}} +do_execsql_test json104-222 { + SELECT json_patch('{}','{"a":{"bb":{"ccc":[1,{"dddd":null},3]}}}'); +} {{{"a":{"bb":{"ccc":[1,{"dddd":null},3]}}}}} + +# Example test cases at the end of the RFC-7396 document +do_execsql_test json104-300 { + SELECT json_patch('{"a":"b"}','{"a":"c"}'); +} {{{"a":"c"}}} +do_execsql_test json104-300a { + SELECT coalesce(json_patch(null,'{"a":"c"}'), 'real-null'); +} {{real-null}} +do_execsql_test json104-301 { + SELECT json_patch('{"a":"b"}','{"b":"c"}'); +} {{{"a":"b","b":"c"}}} +do_execsql_test json104-302 { + SELECT json_patch('{"a":"b"}','{"a":null}'); +} {{{}}} +do_execsql_test json104-303 { + SELECT json_patch('{"a":"b","b":"c"}','{"a":null}'); +} {{{"b":"c"}}} +do_execsql_test json104-304 { + SELECT json_patch('{"a":["b"]}','{"a":"c"}'); +} {{{"a":"c"}}} +do_execsql_test json104-305 { + SELECT json_patch('{"a":"c"}','{"a":["b"]}'); +} {{{"a":["b"]}}} +do_execsql_test json104-306 { + SELECT json_patch('{"a":{"b":"c"}}','{"a":{"b":"d","c":null}}'); +} {{{"a":{"b":"d"}}}} +do_execsql_test json104-307 { + SELECT json_patch('{"a":[{"b":"c"}]}','{"a":[1]}'); +} {{{"a":[1]}}} +do_execsql_test json104-308 { + SELECT json_patch('["a","b"]','["c","d"]'); +} {{["c","d"]}} +do_execsql_test json104-309 { + SELECT json_patch('{"a":"b"}','["c"]'); +} {{["c"]}} +do_execsql_test json104-310 { + SELECT json_patch('{"a":"foo"}','null'); +} {{null}} +do_execsql_test json104-310a { + SELECT coalesce(json_patch('{"a":"foo"}',null), 'real-null'); +} {{real-null}} +do_execsql_test json104-311 { + SELECT json_patch('{"a":"foo"}','"bar"'); +} {{"bar"}} +do_execsql_test json104-312 { + SELECT json_patch('{"e":null}','{"a":1}'); +} {{{"e":null,"a":1}}} +do_execsql_test json104-313 { + SELECT json_patch('[1,2]','{"a":"b","c":null}'); +} {{{"a":"b"}}} +do_execsql_test json104-314 { + SELECT json_patch('{}','{"a":{"bb":{"ccc":null}}}'); +} {{{"a":{"bb":{}}}}} +do_execsql_test json104-320 { + SELECT json_patch('{"x":{"one":1}}','{"x":{"two":2},"x":"three"}'); +} {{{"x":"three"}}} + +#------------------------------------------------------------------------- + +do_execsql_test 401 { + CREATE TABLE obj(x); + INSERT INTO obj VALUES('{"a":1,"b":2}'); + SELECT * FROM obj; +} {{{"a":1,"b":2}}} +do_execsql_test 402 { + UPDATE obj SET x = json_insert(x, '$.c', 3); + SELECT * FROM obj; +} {{{"a":1,"b":2,"c":3}}} +do_execsql_test 403 { + SELECT json_extract(x, '$.b') FROM obj; + SELECT json_extract(x, '$."b"') FROM obj; +} {2 2} +do_execsql_test 404 { + UPDATE obj SET x = json_set(x, '$."b"', 555); + SELECT json_extract(x, '$.b') FROM obj; + SELECT json_extract(x, '$."b"') FROM obj; +} {555 555} +do_execsql_test 405 { + UPDATE obj SET x = json_set(x, '$."d"', 4); + SELECT json_extract(x, '$."d"') FROM obj; +} {4} + + +finish_test diff --git a/test/json105.test b/test/json105.test new file mode 100644 index 0000000000..509db94e11 --- /dev/null +++ b/test/json105.test @@ -0,0 +1,113 @@ +# 2019-11-22 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements tests for "[#]" extension to json-path +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix json105 + +# This is the example from pages 2 and 3 of RFC-7396 +db eval { + CREATE TABLE t1(j); + INSERT INTO t1(j) VALUES('{"a":1,"b":[1,[2,3],4],"c":99}'); +} +proc json_extract_test {testnum path result} { + do_execsql_test json105-1.$testnum "SELECT quote(json_extract(j,$path)) FROM t1" $result +} +json_extract_test 10 {'$.b[#]'} NULL +json_extract_test 20 {'$.b[#-1]'} 4 +json_extract_test 30 {'$.b[#-2]'} {'[2,3]'} +json_extract_test 31 {'$.b[#-02]'} {'[2,3]'} +json_extract_test 40 {'$.b[#-3]'} 1 +json_extract_test 50 {'$.b[#-4]'} NULL +json_extract_test 60 {'$.b[#-2][#-1]'} 3 +json_extract_test 70 {'$.b[0]','$.b[#-1]'} {'[1,4]'} + +json_extract_test 100 {'$.a[#-1]'} NULL +json_extract_test 110 {'$.b[#-000001]'} 4 + +proc json_remove_test {testnum path result} { + do_execsql_test json105-2.$testnum "SELECT quote(json_remove(j,$path)) FROM t1" $result +} +json_remove_test 10 {'$.b[#]'} {'{"a":1,"b":[1,[2,3],4],"c":99}'} +json_remove_test 20 {'$.b[#-0]'} {'{"a":1,"b":[1,[2,3],4],"c":99}'} +json_remove_test 30 {'$.b[#-1]'} {'{"a":1,"b":[1,[2,3]],"c":99}'} +json_remove_test 40 {'$.b[#-2]'} {'{"a":1,"b":[1,4],"c":99}'} +json_remove_test 50 {'$.b[#-3]'} {'{"a":1,"b":[[2,3],4],"c":99}'} +json_remove_test 60 {'$.b[#-4]'} {'{"a":1,"b":[1,[2,3],4],"c":99}'} +json_remove_test 70 {'$.b[#-2][#-1]'} {'{"a":1,"b":[1,[2],4],"c":99}'} + +json_remove_test 100 {'$.b[0]','$.b[#-1]'} {'{"a":1,"b":[[2,3]],"c":99}'} +json_remove_test 110 {'$.b[#-1]','$.b[0]'} {'{"a":1,"b":[[2,3]],"c":99}'} +json_remove_test 120 {'$.b[#-1]','$.b[#-2]'} {'{"a":1,"b":[[2,3]],"c":99}'} +json_remove_test 130 {'$.b[#-1]','$.b[#-1]'} {'{"a":1,"b":[1],"c":99}'} +json_remove_test 140 {'$.b[#-2]','$.b[#-1]'} {'{"a":1,"b":[1],"c":99}'} + +proc json_insert_test {testnum x result} { + do_execsql_test json105-3.$testnum "SELECT quote(json_insert(j,$x)) FROM t1" $result +} +json_insert_test 10 {'$.b[#]','AAA'} {'{"a":1,"b":[1,[2,3],4,"AAA"],"c":99}'} +json_insert_test 20 {'$.b[1][#]','AAA'} {'{"a":1,"b":[1,[2,3,"AAA"],4],"c":99}'} +json_insert_test 30 {'$.b[1][#]','AAA','$.b[#]','BBB'} \ + {'{"a":1,"b":[1,[2,3,"AAA"],4,"BBB"],"c":99}'} +json_insert_test 40 {'$.b[#]','AAA','$.b[#]','BBB'} \ + {'{"a":1,"b":[1,[2,3],4,"AAA","BBB"],"c":99}'} + +proc json_set_test {testnum x result} { + do_execsql_test json105-4.$testnum "SELECT quote(json_set(j,$x)) FROM t1" $result +} +json_set_test 10 {'$.b[#]','AAA'} {'{"a":1,"b":[1,[2,3],4,"AAA"],"c":99}'} +json_set_test 20 {'$.b[1][#]','AAA'} {'{"a":1,"b":[1,[2,3,"AAA"],4],"c":99}'} +json_set_test 30 {'$.b[1][#]','AAA','$.b[#]','BBB'} \ + {'{"a":1,"b":[1,[2,3,"AAA"],4,"BBB"],"c":99}'} +json_set_test 40 {'$.b[#]','AAA','$.b[#]','BBB'} \ + {'{"a":1,"b":[1,[2,3],4,"AAA","BBB"],"c":99}'} +json_set_test 50 {'$.b[#-1]','AAA'} {'{"a":1,"b":[1,[2,3],"AAA"],"c":99}'} +json_set_test 60 {'$.b[1][#-1]','AAA'} {'{"a":1,"b":[1,[2,"AAA"],4],"c":99}'} +json_set_test 70 {'$.b[1][#-1]','AAA','$.b[#-1]','BBB'} \ + {'{"a":1,"b":[1,[2,"AAA"],"BBB"],"c":99}'} +json_set_test 80 {'$.b[#-1]','AAA','$.b[#-1]','BBB'} \ + {'{"a":1,"b":[1,[2,3],"BBB"],"c":99}'} + +proc json_replace_test {testnum x result} { + do_execsql_test json105-5.$testnum "SELECT quote(json_replace(j,$x)) FROM t1" $result +} +json_replace_test 10 {'$.b[#]','AAA'} {'{"a":1,"b":[1,[2,3],4],"c":99}'} +json_replace_test 20 {'$.b[1][#]','AAA'} {'{"a":1,"b":[1,[2,3],4],"c":99}'} +json_replace_test 30 {'$.b[1][#]','AAA','$.b[#]','BBB'} \ + {'{"a":1,"b":[1,[2,3],4],"c":99}'} +json_replace_test 40 {'$.b[#]','AAA','$.b[#]','BBB'} \ + {'{"a":1,"b":[1,[2,3],4],"c":99}'} +json_replace_test 50 {'$.b[#-1]','AAA'} {'{"a":1,"b":[1,[2,3],"AAA"],"c":99}'} +json_replace_test 60 {'$.b[1][#-1]','AAA'} {'{"a":1,"b":[1,[2,"AAA"],4],"c":99}'} +json_replace_test 70 {'$.b[1][#-1]','AAA','$.b[#-1]','BBB'} \ + {'{"a":1,"b":[1,[2,"AAA"],"BBB"],"c":99}'} +json_replace_test 80 {'$.b[#-1]','AAA','$.b[#-1]','BBB'} \ + {'{"a":1,"b":[1,[2,3],"BBB"],"c":99}'} + +do_catchsql_test json105-6.10 { + SELECT json_extract(j, '$.b[#-]') FROM t1; +} {1 {bad JSON path: '$.b[#-]'}} +do_catchsql_test json105-6.20 { + SELECT json_extract(j, '$.b[#9]') FROM t1; +} {1 {bad JSON path: '$.b[#9]'}} +do_catchsql_test json105-6.30 { + SELECT json_extract(j, '$.b[#+2]') FROM t1; +} {1 {bad JSON path: '$.b[#+2]'}} +do_catchsql_test json105-6.40 { + SELECT json_extract(j, '$.b[#-1') FROM t1; +} {1 {bad JSON path: '$.b[#-1'}} +do_catchsql_test json105-6.50 { + SELECT json_extract(j, '$.b[#-1x]') FROM t1; +} {1 {bad JSON path: '$.b[#-1x]'}} + +finish_test diff --git a/test/json106.test b/test/json106.test new file mode 100644 index 0000000000..06859a10b4 --- /dev/null +++ b/test/json106.test @@ -0,0 +1,79 @@ +# 2023-12-18 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# Invariant tests for JSON built around the randomjson extension +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix json106 + +# These tests require virtual table "json_tree" to run. +ifcapable !vtab { finish_test ; return } + +load_static_extension db randomjson +db eval { + CREATE TEMP TABLE t1(j0,j5,p); + CREATE TEMP TABLE kv(n,key,val); +} +unset -nocomplain ii +for {set ii 1} {$ii<=5000} {incr ii} { + do_execsql_test $ii.1 { + DELETE FROM t1; + INSERT INTO t1(j0,j5) VALUES(random_json($ii),random_json5($ii)); + SELECT json_valid(j0), json_valid(j5,2) FROM t1; + } {1 1} + do_execsql_test $ii.2 { + SELECT count(*) + FROM t1, json_tree(j0) AS rt + WHERE rt.type NOT IN ('object','array') + AND rt.atom IS NOT (j0 ->> rt.fullkey); + } 0 + do_execsql_test $ii.3 { + SELECT count(*) + FROM t1, json_tree(j5) AS rt + WHERE rt.type NOT IN ('object','array') + AND rt.atom IS NOT (j0 ->> rt.fullkey); + } 0 + do_execsql_test $ii.4 { + DELETE FROM kv; + INSERT INTO kv + SELECT rt.rowid, rt.fullkey, rt.atom + FROM t1, json_tree(j0) AS rt + WHERE rt.type NOT IN ('object','array'); + } + do_execsql_test $ii.5 { + SELECT count(*) + FROM t1, kv + WHERE key NOT LIKE '%]' + AND json_remove(j5,key)->>key IS NOT NULL + } 0 + do_execsql_test $ii.6 { + SELECT count(*) + FROM t1, kv + WHERE key NOT LIKE '%]' + AND json_insert(json_remove(j5,key),key,val)->>key IS NOT val + } 0 + do_execsql_test $ii.7 { + UPDATE t1 SET p=json_patch(j0,j5); + SELECT count(*) + FROM t1, kv + WHERE p->>key IS NOT val + } 0 + do_execsql_test $ii.8 { + SELECT j0 FROM t1 WHERE json(j0)!=json(json_pretty(j0)); + } {} + do_execsql_test $ii.9 { + SELECT j5 FROM t1 WHERE json(j5)!=json(json_pretty(j5)); + } {} +} + + +finish_test diff --git a/test/json107.test b/test/json107.test new file mode 100644 index 0000000000..779b557fba --- /dev/null +++ b/test/json107.test @@ -0,0 +1,86 @@ +# 2024-01-23 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# Legacy JSON bug: If the input is a BLOB that when cast into TEXT looks +# like valid JSON, then treat it as valid JSON. +# +# The original intent of the JSON functions was to raise an error on any +# BLOB input. That intent was clearly documented, but the code failed to +# to implement it. Subsequently, many applications began to depend on the +# incorrect behavior, especially apps that used readfile() to read JSON +# content, since readfile() returns a BLOB. So we need to support the +# bug moving forward. +# +# The tests in this fail verify that the original buggy behavior is +# preserved. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix json107 + +if {[db one {PRAGMA encoding}]!="UTF-8"} { + # These tests only work for a UTF-8 encoding. + finish_test + return +} + +do_execsql_test 1.1 { + SELECT json_valid( CAST('{"a":1}' AS BLOB) ); +} 1 +do_execsql_test 1.1.1 { + SELECT json_valid( CAST('{"a":1}' AS BLOB), 1); +} 1 +do_execsql_test 1.1.2 { + SELECT json_valid( CAST('{"a":1}' AS BLOB), 2); +} 1 +do_execsql_test 1.1.4 { + SELECT json_valid( CAST('{"a":1}' AS BLOB), 4); +} 0 +do_execsql_test 1.1.8 { + SELECT json_valid( CAST('{"a":1}' AS BLOB), 8); +} 0 + +do_execsql_test 1.2.1 { + SELECT CAST('{"a":123}' AS blob) -> 'a'; +} 123 +do_execsql_test 1.2.2 { + SELECT CAST('{"a":123}' AS blob) ->> 'a'; +} 123 +do_execsql_test 1.2.3 { + SELECT json_extract(CAST('{"a":123}' AS blob), '$.a'); +} 123 +do_execsql_test 1.3 { + SELECT json_insert(CAST('{"a":123}' AS blob),'$.b',456); +} {{{"a":123,"b":456}}} +do_execsql_test 1.4 { + SELECT json_remove(CAST('{"a":123,"b":456}' AS blob),'$.a'); +} {{{"b":456}}} +do_execsql_test 1.5 { + SELECT json_set(CAST('{"a":123,"b":456}' AS blob),'$.a',789); +} {{{"a":789,"b":456}}} +do_execsql_test 1.6 { + SELECT json_replace(CAST('{"a":123,"b":456}' AS blob),'$.a',789); +} {{{"a":789,"b":456}}} +do_execsql_test 1.7 { + SELECT json_type(CAST('{"a":123,"b":456}' AS blob)); +} object +do_execsql_test 1.8 { + SELECT json(CAST('{"a":123,"b":456}' AS blob)); +} {{{"a":123,"b":456}}} + +ifcapable vtab { + do_execsql_test 2.1 { + SELECT key, value FROM json_tree( CAST('{"a":123,"b":456}' AS blob) ) + WHERE atom; + } {a 123 b 456} +} +finish_test diff --git a/test/json108.test b/test/json108.test new file mode 100644 index 0000000000..71f3814dce --- /dev/null +++ b/test/json108.test @@ -0,0 +1,45 @@ +# 2024-03-06 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# Invariant tests for JSON built around the randomjson extension +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix json108 + +# These tests require virtual table "json_tree" to run. +ifcapable !vtab { finish_test ; return } + +load_static_extension db randomjson +db eval { + CREATE TEMP TABLE t1(j0,j5); + WITH RECURSIVE c(n) AS (VALUES(0) UNION ALL SELECT n+1 FROM c WHERE n<9) + INSERT INTO t1 SELECT random_json(n), random_json5(n) FROM c; +} + +do_execsql_test 1.1 { + SELECT count(*) FROM t1 WHERE json(j0)==json(json_pretty(j0,NULL)); +} 10 +do_execsql_test 1.2 { + SELECT count(*) FROM t1 WHERE json(j0)==json(json_pretty(j0,NULL)); +} 10 +do_execsql_test 1.3 { + SELECT count(*) FROM t1 WHERE json(j0)==json(json_pretty(j0,'')); +} 10 +do_execsql_test 1.4 { + SELECT count(*) FROM t1 WHERE json(j0)==json(json_pretty(j0,char(9))); +} 10 +do_execsql_test 1.5 { + SELECT count(*) FROM t1 WHERE json(j0)==json(json_pretty(j0,'/*hello*/')); +} 10 + + +finish_test diff --git a/test/json501.test b/test/json501.test new file mode 100644 index 0000000000..bfd21055a7 --- /dev/null +++ b/test/json501.test @@ -0,0 +1,336 @@ +# 2023-04-27 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements tests for the JSON5 enhancements to the +# JSON SQL functions extension to the SQLite library. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix json501 + +# From https://spec.json5.org/#introduction +# +#----------------------------------------------------------------------------- +# Summary of Features +# +# The following ECMAScript 5.1 features, which are not supported in JSON, have +# been extended to JSON5. +# +# Objects +# +# 1) Object keys may be an ECMAScript 5.1 IdentifierName. +# 2) Objects may have a single trailing comma. +# +# Arrays +# +# 3) Arrays may have a single trailing comma. +# +# Strings +# +# 4) Strings may be single quoted. +# 5) Strings may span multiple lines by escaping new line characters. +# 6) Strings may include character escapes. +# +# Numbers +# +# 7) Numbers may be hexadecimal. +# 8) Numbers may have a leading or trailing decimal point. +# 9) Numbers may be IEEE 754 positive infinity, negative infinity, and NaN. +# 10) Numbers may begin with an explicit plus sign. +# +# Comments +# +# 11) Single and multi-line comments are allowed. +# +# White Space +# +# 12) Additional white space characters are allowed. +#----------------------------------------------------------------------------- +# +# Test number in this file are of the form X.Y where X is one of the item +# numbers in the feature list above and Y is the test sequence number. +# + +############################################################################### +# 1) Object keys may be an ECMAScript 5.1 IdentifierName. +do_execsql_test 1.1 { + WITH c(x) AS (VALUES('{a:5,b:6}')) + SELECT x->>'a', json(x), json_valid(x), NOT json_error_position(x) FROM c; +} {5 {{"a":5,"b":6}} 0 1} +do_execsql_test 1.2 { + SELECT '[7,null,{a:5,b:6},[8,9]]'->>'$[2].b'; +} {6} +do_execsql_test 1.3 { + SELECT '{ $123 : 789 }'->>'$."$123"'; +} 789 +do_execsql_test 1.4 { + SELECT '{ _123$xyz : 789 }'->>'$."_123$xyz"'; +} 789 +do_execsql_test 1.5 { + SELECT '{ MNO_123$xyz : 789 }'->>'$."MNO_123$xyz"'; +} 789 + +do_execsql_test 1.6 { + SELECT json('{ MNO_123$xyz : 789 }'); +} [list {{"MNO_123$xyz":789}}] + +do_catchsql_test 1.10 { + SELECT json('{ MNO_123/xyz : 789 }'); +} {1 {malformed JSON}} + +do_execsql_test 1.11 { + SELECT '{ MNO_123æxyz : 789 }'->>'MNO_123æxyz'; +} {789} + +############################################################################### +# 2) Objects may have a single trailing comma. + +do_execsql_test 2.1 { + WITH c(x) AS (VALUES('{"a":5, "b":6, }')) + SELECT x->>'b', json(x), json_valid(x), NOT json_error_position(x) FROM c; +} {6 {{"a":5,"b":6}} 0 1} +do_execsql_test 2.2 { + SELECT '{a:5, b:6 , }'->>'b'; +} 6 +do_catchsql_test 2.3 { + SELECT '{a:5, b:6 ,, }'->>'b'; +} {1 {malformed JSON}} +do_catchsql_test 2.4 { + SELECT '{a:5, b:6, ,}'->>'b'; +} {1 {malformed JSON}} + +############################################################################### +# 3) Arrays may have a single trailing comma. + +do_execsql_test 3.1 { + WITH c(x) AS (VALUES('[5, 6,]')) + SELECT x->>1, json(x), json_valid(x), NOT json_error_position(x) FROM c; +} {6 {[5,6]} 0 1} +do_execsql_test 3.2 { + SELECT '[5, 6 , ]'->>1; +} 6 +do_catchsql_test 3.3 { + SELECT '[5, 6,,]'->>1; +} {1 {malformed JSON}} +do_catchsql_test 3.4 { + SELECT '[5, 6 , , ]'->>1; +} {1 {malformed JSON}} + +############################################################################### +# 4) Strings may be single quoted. + +do_execsql_test 4.1 { + WITH c(x) AS (VALUES('{"a": ''abcd''}')) + SELECT x->>'a', json(x), json_valid(x), NOT json_error_position(x) FROM c; +} {abcd {{"a":"abcd"}} 0 1} +do_execsql_test 4.2 { + SELECT '{b: 123, ''a'': ''ab\''cd''}'->>'a'; +} {ab'cd} + +############################################################################### +# 5) Strings may span multiple lines by escaping new line characters. + +do_execsql_test 5.1 { + WITH c(x) AS (VALUES('{a: "abc'||char(0x5c,0x0a)||'xyz"}')) + SELECT x->>'a', json(x), json_valid(x), NOT json_error_position(x) FROM c; +} {abcxyz {{"a":"abcxyz"}} 0 1} +do_execsql_test 5.2 { + SELECT ('{a: "abc'||char(0x5c,0x0d)||'xyz"}')->>'a'; +} {abcxyz} +do_execsql_test 5.3 { + SELECT ('{a: "abc'||char(0x5c,0x0d,0x0a)||'xyz"}')->>'a'; +} {abcxyz} +do_execsql_test 5.4 { + SELECT ('{a: "abc'||char(0x5c,0x2028)||'xyz"}')->>'a'; +} {abcxyz} +do_execsql_test 5.5 { + SELECT ('{a: "abc'||char(0x5c,0x2029)||'xyz"}')->>'a'; +} {abcxyz} + + +############################################################################### +# 6) Strings may include character escapes. + +do_execsql_test 6.1 { + SELECT ('{a: "abc'||char(0x5c,0x27)||'xyz"}')->>'a'; +} {abc'xyz} +do_execsql_test 6.2 { + SELECT ('{a: "abc'||char(0x5c,0x22)||'xyz"}')->>'a'; +} {abc"xyz} +do_execsql_test 6.3 { + SELECT ('{a: "abc'||char(0x5c,0x5c)||'xyz"}')->>'a'; +} {{abc\xyz}} +do_execsql_test 6.4 { + SELECT hex(('{a: "abc\bxyz"}')->>'a'); +} {6162630878797A} +do_execsql_test 6.5 { + SELECT hex(('{a: "abc\f\n\r\t\vxyz"}')->>'a'); +} {6162630C0A0D090B78797A} +do_execsql_test 6.6 { + SELECT hex(('{a: "abc\0xyz"}')->>'a'); +} {6162630078797A} +do_execsql_test 6.7 { + SELECT '{a: "abc\x35\x4f\x6Exyz"}'->>'a'; +} {abc5Onxyz} +do_execsql_test 6.8 { + SELECT '{a: "\x6a\x6A\x6b\x6B\x6c\x6C\x6d\x6D\x6e\x6E\x6f\x6F"}'->>'a'; +} {jjkkllmmnnoo} + +############################################################################### +# 7) Numbers may be hexadecimal. + +do_execsql_test 7.1 { + SELECT '{a: 0x0}'->>'a'; +} 0 +do_execsql_test 7.2 { + SELECT '{a: -0x0}'->>'a'; +} 0 +do_execsql_test 7.3 { + SELECT '{a: +0x0}'->>'a'; +} 0 +do_execsql_test 7.4 { + SELECT '{a: 0xabcdef}'->>'a'; +} 11259375 +do_execsql_test 7.5 { + SELECT '{a: -0xaBcDeF}'->>'a'; +} -11259375 +do_execsql_test 7.6 { + SELECT '{a: +0xABCDEF}'->>'a'; +} 11259375 + +############################################################################### +# 8) Numbers may have a leading or trailing decimal point. + +do_execsql_test 8.1 { + WITH c(x) AS (VALUES('{x: 4.}')) SELECT x->>'x', json(x) FROM c; +} {4.0 {{"x":4.0}}} +do_execsql_test 8.2 { + WITH c(x) AS (VALUES('{x: +4.}')) SELECT x->>'x', json(x) FROM c; +} {4.0 {{"x":4.0}}} +do_execsql_test 8.3 { + WITH c(x) AS (VALUES('{x: -4.}')) SELECT x->>'x', json(x) FROM c; +} {-4.0 {{"x":-4.0}}} +do_execsql_test 8.3 { + WITH c(x) AS (VALUES('{x: .5}')) SELECT x->>'x', json(x) FROM c; +} {0.5 {{"x":0.5}}} +do_execsql_test 8.4 { + WITH c(x) AS (VALUES('{x: -.5}')) SELECT x->>'x', json(x) FROM c; +} {-0.5 {{"x":-0.5}}} +do_execsql_test 8.5 { + WITH c(x) AS (VALUES('{x: +.5}')) SELECT x->>'x', json(x) FROM c; +} {0.5 {{"x":0.5}}} +do_execsql_test 8.6 { + WITH c(x) AS (VALUES('{x: 4.e0}')) SELECT x->>'x', json(x) FROM c; +} {4.0 {{"x":4.0e0}}} +do_execsql_test 8.7 { + WITH c(x) AS (VALUES('{x: +4.e1}')) SELECT x->>'x', json(x) FROM c; +} {40.0 {{"x":4.0e1}}} +do_execsql_test 8.8 { + WITH c(x) AS (VALUES('{x: -4.e2}')) SELECT x->>'x', json(x) FROM c; +} {-400.0 {{"x":-4.0e2}}} +do_execsql_test 8.9 { + WITH c(x) AS (VALUES('{x: .5e3}')) SELECT x->>'x', json(x) FROM c; +} {500.0 {{"x":0.5e3}}} +do_execsql_test 8.10 { + WITH c(x) AS (VALUES('{x: -.5e-1}')) SELECT x->>'x', json(x) FROM c; +} {-0.05 {{"x":-0.5e-1}}} +do_execsql_test 8.11 { + WITH c(x) AS (VALUES('{x: +.5e-2}')) SELECT x->>'x', json(x) FROM c; +} {0.005 {{"x":0.5e-2}}} + + +############################################################################### +# 9) Numbers may be IEEE 754 positive infinity, negative infinity, and NaN. + +do_execsql_test 9.1 { + WITH c(x) AS (VALUES('{x: +Infinity}')) SELECT x->>'x', json(x) FROM c; +} {Inf {{"x":9e999}}} +do_execsql_test 9.2 { + WITH c(x) AS (VALUES('{x: -Infinity}')) SELECT x->>'x', json(x) FROM c; +} {-Inf {{"x":-9e999}}} +do_execsql_test 9.3 { + WITH c(x) AS (VALUES('{x: Infinity}')) SELECT x->>'x', json(x) FROM c; +} {Inf {{"x":9e999}}} +do_execsql_test 9.4 { + WITH c(x) AS (VALUES('{x: NaN}')) SELECT x->>'x', json(x) FROM c; +} {{} {{"x":null}}} + +############################################################################### +# 10) Numbers may begin with an explicit plus sign. + +do_execsql_test 10.1 { + SELECT '{a: +123}'->'a'; +} 123 + +############################################################################### +# 11) Single and multi-line comments are allowed. + +do_execsql_test 11.1 { + SELECT ' /* abc */ { /*def*/ aaa /* xyz */ : // to the end of line + 123 /* xyz */ , /* 123 */ }'->>'aaa'; +} 123 + +############################################################################### +# 12) Additional white space characters are allowed. + +do_execsql_test 12.1 { + SELECT (char(0x09,0x0a,0x0b,0x0c,0x0d,0x20,0xa0,0x2028,0x2029) + || '{a: "xyz"}')->>'a'; +} xyz +do_execsql_test 12.2 { + SELECT ('{a:' || char(0x09,0x0a,0x0b,0x0c,0x0d,0x20,0xa0,0x2028,0x2029) + || '"xyz"}')->>'a'; +} xyz +do_execsql_test 12.3 { + SELECT (char(0x1680,0x2000,0x2001,0x2002,0x2003,0x2004,0x2005, + 0x2006,0x2007,0x2008,0x2009,0x200a,0x3000,0xfeff) + || '{a: "xyz"}')->>'a'; +} xyz +do_execsql_test 12.4 { + SELECT ('{a: ' ||char(0x1680,0x2000,0x2001,0x2002,0x2003,0x2004,0x2005, + 0x2006,0x2007,0x2008,0x2009,0x200a,0x3000,0xfeff) + || ' "xyz"}')->>'a'; +} xyz + +# 2023-11-08 forum/forumpost/ddcad3e884 +# +do_execsql_test 13.1 { + SELECT json('{x:''a "b" c''}'); +} {{{"x":"a \"b\" c"}}} + +# 2024-01-31 +# Allow control characters within JSON5 string literals. +# +for {set c 1} {$c<=0x1f} {incr c} { + do_execsql_test 14.$c.1 { + SELECT json_valid('"abc' || char($c) || 'xyz"'); + } {0} + do_execsql_test 14.$c.2 { + SELECT json_valid('"abc' || char($c) || 'xyz"', 2); + } {1} + switch $c { + 8 {set e "\\b"} + 9 {set e "\\t"} + 10 {set e "\\n"} + 12 {set e "\\f"} + 13 {set e "\\r"} + default {set e [format "\\u00%02x" $c]} + } + do_execsql_test 14.$c.3 { + SELECT json('{label:"abc' || char($c) || 'xyz"}'); + } "{{\"label\":\"abc${e}xyz\"}}" + do_execsql_test 14.$c.4 { + SELECT jsonb('{label:"abc' || char($c) || 'xyz"}') -> '$'; + } "{{\"label\":\"abc${e}xyz\"}}" +} + + +finish_test diff --git a/test/json502.test b/test/json502.test new file mode 100644 index 0000000000..1eba00dba5 --- /dev/null +++ b/test/json502.test @@ -0,0 +1,80 @@ +# 2023-04-28 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements tests for the JSON5 enhancements to the +# JSON SQL functions extension to the SQLite library. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix json502 + +ifcapable vtab { + +do_execsql_test 1.1 { + CREATE TABLE t1(x JSON); + INSERT INTO t1(x) VALUES('{a:{b:{c:"hello",},},}'); + SELECT fullkey FROM t1, json_tree(x); +} {{$} {$.a} {$.a.b} {$.a.b.c}} + +} + +do_execsql_test 2.1 { + SELECT json_error_position('{a:null,{"h":[1,[1,2,3]],"j":"abc"}:true}'); +} 9 +do_catchsql_test 2.2 { + SELECT json('{a:null,{"h":[1,[1,2,3]],"j":"abc"}:true}'); +} {1 {malformed JSON}} +do_catchsql_test 2.3 { + SELECT '{a:null,{"h":[1,[1,2,3]],"j":"abc"}:true}'->'$h[#-1]'; +} {1 {malformed JSON}} + +# Verify that escaped label names are compared correctly. +# +do_execsql_test 3.1 { + SELECT '{"a\x62c":123}' ->> 'abc'; +} 123 +do_execsql_test 3.2 { + SELECT '{"abc":123}' ->> 'a\x62c'; +} 123 + +db null null +do_execsql_test 3.3 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(x); + INSERT INTO t1 VALUES(json_insert('{}','$.a\',111,'$."b\\"',222)); + INSERT INTO t1 VALUES(jsonb_insert('{}','$.a\',111,'$."b\\"',222)); + SELECT x->'$.a\', x->'$.a\\', x->'$."a\\"', x->'$."b\\"' FROM t1; +} {111 null 111 222 111 null 111 222} + +do_execsql_test 3.4 { + SELECT json_patch('{"a\x62c":123}','{"ab\x63":456}') ->> 'abc'; +} 456 + +ifcapable vtab { + do_execsql_test 4.1 { + SELECT * FROM json_tree('{"\u0017":1}','$."\x17"'); + } {{\x17} 1 integer 1 1 null {$."\x17"} {$}} +} + +# JSON PATH parsing bug involving backslash escapes, reported via +# private email from Florent De'Neve on 2024-09-04. +# +do_execsql_test 5.1 { + SELECT json_extract('{"A\"Key":1}', '$.A"Key'); +} 1 +do_execsql_test 5.2 { + SELECT json_extract('{"A\"Key":1}', '$."A\"Key"'); +} 1 +do_execsql_test 5.3 { + SELECT JSON_SET('{}', '$."\"Key"', 1); +} {{{"\"Key":1}}} + +finish_test diff --git a/test/jsonb01.test b/test/jsonb01.test new file mode 100644 index 0000000000..8f16428dcc --- /dev/null +++ b/test/jsonb01.test @@ -0,0 +1,53 @@ +# 2023-11-15 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# Test cases for JSONB +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +do_execsql_test jsonb01-1.1 { + CREATE TABLE t1(x JSON BLOB); + INSERT INTO t1 VALUES(jsonb('{a:5,b:{x:10,y:11},c:[1,2,3,4]}')); +} +foreach {id path res} { + 1 {$.a} {{{"b":{"x":10,"y":11},"c":[1,2,3,4]}}} + 2 {$.b} {{{"a":5,"c":[1,2,3,4]}}} + 3 {$.c} {{{"a":5,"b":{"x":10,"y":11}}}} + 4 {$.d} {{{"a":5,"b":{"x":10,"y":11},"c":[1,2,3,4]}}} + 5 {$.b.x} {{{"a":5,"b":{"y":11},"c":[1,2,3,4]}}} + 6 {$.b.y} {{{"a":5,"b":{"x":10},"c":[1,2,3,4]}}} + 7 {$.c[0]} {{{"a":5,"b":{"x":10,"y":11},"c":[2,3,4]}}} + 8 {$.c[1]} {{{"a":5,"b":{"x":10,"y":11},"c":[1,3,4]}}} + 9 {$.c[2]} {{{"a":5,"b":{"x":10,"y":11},"c":[1,2,4]}}} + 10 {$.c[3]} {{{"a":5,"b":{"x":10,"y":11},"c":[1,2,3]}}} + 11 {$.c[4]} {{{"a":5,"b":{"x":10,"y":11},"c":[1,2,3,4]}}} + 12 {$.c[#]} {{{"a":5,"b":{"x":10,"y":11},"c":[1,2,3,4]}}} + 13 {$.c[#-1]} {{{"a":5,"b":{"x":10,"y":11},"c":[1,2,3]}}} + 14 {$.c[#-2]} {{{"a":5,"b":{"x":10,"y":11},"c":[1,2,4]}}} + 15 {$.c[#-3]} {{{"a":5,"b":{"x":10,"y":11},"c":[1,3,4]}}} + 16 {$.c[#-4]} {{{"a":5,"b":{"x":10,"y":11},"c":[2,3,4]}}} + 17 {$.c[#-5]} {{{"a":5,"b":{"x":10,"y":11},"c":[1,2,3,4]}}} + 18 {$.c[#-6]} {{{"a":5,"b":{"x":10,"y":11},"c":[1,2,3,4]}}} +} { + do_execsql_test jsonb01-1.2.$id.1 { + SELECT json(jsonb_remove(x,$path)) FROM t1; + } $res + do_execsql_test jsonb01-1.2.$id.2 { + SELECT json_remove(x,$path) FROM t1; + } $res +} + +do_catchsql_test jsonb01-2.0 { + SELECT x'8ce6ffffffff171333' -> '$'; +} {1 {malformed JSON}} + +finish_test diff --git a/test/kvtest.c b/test/kvtest.c new file mode 100644 index 0000000000..624c80b746 --- /dev/null +++ b/test/kvtest.c @@ -0,0 +1,1137 @@ +/* +** 2016-12-28 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** This file implements "key-value" performance test for SQLite. The +** purpose is to compare the speed of SQLite for accessing large BLOBs +** versus reading those same BLOB values out of individual files in the +** filesystem. +** +** Run "kvtest" with no arguments for on-line help, or see comments below. +** +** HOW TO COMPILE: +** +** (1) Gather this source file and a recent SQLite3 amalgamation with its +** header into the working directory. You should have: +** +** kvtest.c >--- this file +** sqlite3.c \___ SQLite +** sqlite3.h / amlagamation & header +** +** (2) Run you compiler against the two C source code files. +** +** (a) On linux or mac: +** +** OPTS="-DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION" +** gcc -Os -I. $OPTS kvtest.c sqlite3.c -o kvtest +** +** The $OPTS options can be omitted. The $OPTS merely omit +** the need to link against -ldl and -lpthread, or whatever +** the equivalent libraries are called on your system. +** +** (b) Windows with MSVC: +** +** cl -I. kvtest.c sqlite3.c +** +** USAGE: +** +** (1) Create a test database by running "kvtest init" with appropriate +** options. See the help message for available options. +** +** (2) Construct the corresponding pile-of-files database on disk using +** the "kvtest export" command. +** +** (3) Run tests using "kvtest run" against either the SQLite database or +** the pile-of-files database and with appropriate options. +** +** For example: +** +** ./kvtest init x1.db --count 100000 --size 10000 +** mkdir x1 +** ./kvtest export x1.db x1 +** ./kvtest run x1.db --count 10000 --max-id 1000000 +** ./kvtest run x1 --count 10000 --max-id 1000000 +*/ +static const char zHelp[] = +"Usage: kvtest COMMAND ARGS...\n" +"\n" +" kvtest init DBFILE --count N --size M --pagesize X\n" +"\n" +" Generate a new test database file named DBFILE containing N\n" +" BLOBs each of size M bytes. The page size of the new database\n" +" file will be X. Additional options:\n" +"\n" +" --variance V Randomly vary M by plus or minus V\n" +"\n" +" kvtest export DBFILE DIRECTORY [--tree]\n" +"\n" +" Export all the blobs in the kv table of DBFILE into separate\n" +" files in DIRECTORY. DIRECTORY is created if it does not previously\n" +" exist. If the --tree option is used, then the blobs are written\n" +" into a hierarchy of directories, using names like 00/00/00,\n" +" 00/00/01, 00/00/02, and so forth. Without the --tree option, all\n" +" files are in the top-level directory with names like 000000, 000001,\n" +" 000002, and so forth.\n" +"\n" +" kvtest stat DBFILE [options]\n" +"\n" +" Display summary information about DBFILE. Options:\n" +"\n" +" --vacuum Run VACUUM on the database file\n" +"\n" +" kvtest run DBFILE [options]\n" +"\n" +" Run a performance test. DBFILE can be either the name of a\n" +" database or a directory containing sample files. Options:\n" +"\n" +" --asc Read blobs in ascending order\n" +" --blob-api Use the BLOB API\n" +" --cache-size N Database cache size\n" +" --count N Read N blobs\n" +" --desc Read blobs in descending order\n" +" --fsync Synchronous file writes\n" +" --integrity-check Run \"PRAGMA integrity_check\" after test\n" +" --max-id N Maximum blob key to use\n" +" --mmap N Mmap as much as N bytes of DBFILE\n" +" --multitrans Each read or write in its own transaction\n" +" --nocheckpoint Omit the checkpoint on WAL mode writes\n" +" --nosync Set \"PRAGMA synchronous=OFF\"\n" +" --jmode MODE Set MODE journal mode prior to starting\n" +" --random Read blobs in a random order\n" +" --start N Start reading with this blob key\n" +" --stats Output operating stats before exiting\n" +" --update Do an overwrite test\n" +; + +/* Reference resources used */ +#include <stdio.h> +#include <stdlib.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <assert.h> +#include <string.h> +#include "sqlite3.h" + +#ifndef _WIN32 +# include <unistd.h> +#else + /* Provide Windows equivalent for the needed parts of unistd.h */ +# include <direct.h> +# include <io.h> +# define R_OK 2 +# define S_ISREG(m) (((m) & S_IFMT) == S_IFREG) +# define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) +# define access _access +#endif + +#if !defined(_MSC_VER) +# include <stdint.h> +#endif + +/* +** The following macros are used to cast pointers to integers and +** integers to pointers. The way you do this varies from one compiler +** to the next, so we have developed the following set of #if statements +** to generate appropriate macros for a wide range of compilers. +** +** The correct "ANSI" way to do this is to use the intptr_t type. +** Unfortunately, that typedef is not available on all compilers, or +** if it is available, it requires an #include of specific headers +** that vary from one machine to the next. +** +** Ticket #3860: The llvm-gcc-4.2 compiler from Apple chokes on +** the ((void*)&((char*)0)[X]) construct. But MSVC chokes on ((void*)(X)). +** So we have to define the macros in different ways depending on the +** compiler. +*/ +#if defined(__PTRDIFF_TYPE__) /* This case should work for GCC */ +# define SQLITE_INT_TO_PTR(X) ((void*)(__PTRDIFF_TYPE__)(X)) +# define SQLITE_PTR_TO_INT(X) ((sqlite3_int64)(__PTRDIFF_TYPE__)(X)) +#else +# define SQLITE_INT_TO_PTR(X) ((void*)(intptr_t)(X)) +# define SQLITE_PTR_TO_INT(X) ((sqlite3_int64)(intptr_t)(X)) +#endif + +/* +** Show the help text and quit. +*/ +static void showHelp(void){ + fprintf(stdout, "%s", zHelp); + exit(1); +} + +/* +** Show an error message an quit. +*/ +static void fatalError(const char *zFormat, ...){ + va_list ap; + fprintf(stdout, "ERROR: "); + va_start(ap, zFormat); + vfprintf(stdout, zFormat, ap); + va_end(ap); + fprintf(stdout, "\n"); + exit(1); +} + +/* +** Return the value of a hexadecimal digit. Return -1 if the input +** is not a hex digit. +*/ +static int hexDigitValue(char c){ + if( c>='0' && c<='9' ) return c - '0'; + if( c>='a' && c<='f' ) return c - 'a' + 10; + if( c>='A' && c<='F' ) return c - 'A' + 10; + return -1; +} + +/* +** Interpret zArg as an integer value, possibly with suffixes. +*/ +static int integerValue(const char *zArg){ + int v = 0; + static const struct { char *zSuffix; int iMult; } aMult[] = { + { "KiB", 1024 }, + { "MiB", 1024*1024 }, + { "GiB", 1024*1024*1024 }, + { "KB", 1000 }, + { "MB", 1000000 }, + { "GB", 1000000000 }, + { "K", 1000 }, + { "M", 1000000 }, + { "G", 1000000000 }, + }; + int i; + int isNeg = 0; + if( zArg[0]=='-' ){ + isNeg = 1; + zArg++; + }else if( zArg[0]=='+' ){ + zArg++; + } + if( zArg[0]=='0' && zArg[1]=='x' ){ + int x; + zArg += 2; + while( (x = hexDigitValue(zArg[0]))>=0 ){ + v = (v<<4) + x; + zArg++; + } + }else{ + while( zArg[0]>='0' && zArg[0]<='9' ){ + v = v*10 + zArg[0] - '0'; + zArg++; + } + } + for(i=0; i<sizeof(aMult)/sizeof(aMult[0]); i++){ + if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){ + v *= aMult[i].iMult; + break; + } + } + return isNeg? -v : v; +} + + +/* +** Check the filesystem object zPath. Determine what it is: +** +** PATH_DIR A single directory holding many files +** PATH_TREE A directory hierarchy with files at the leaves +** PATH_DB An SQLite database +** PATH_NEXIST Does not exist +** PATH_OTHER Something else +** +** PATH_DIR means all of the separate files are grouped together +** into a single directory with names like 000000, 000001, 000002, and +** so forth. PATH_TREE means there is a hierarchy of directories so +** that no single directory has too many entries. The files have names +** like 00/00/00, 00/00/01, 00/00/02 and so forth. The decision between +** PATH_DIR and PATH_TREE is determined by the presence of a subdirectory +** named "00" at the top-level. +*/ +#define PATH_DIR 1 +#define PATH_TREE 2 +#define PATH_DB 3 +#define PATH_NEXIST 0 +#define PATH_OTHER 99 +static int pathType(const char *zPath){ + struct stat x; + int rc; + if( access(zPath,R_OK) ) return PATH_NEXIST; + memset(&x, 0, sizeof(x)); + rc = stat(zPath, &x); + if( rc<0 ) return PATH_OTHER; + if( S_ISDIR(x.st_mode) ){ + char *zLayer1 = sqlite3_mprintf("%s/00", zPath); + memset(&x, 0, sizeof(x)); + rc = stat(zLayer1, &x); + sqlite3_free(zLayer1); + if( rc<0 ) return PATH_DIR; + if( S_ISDIR(x.st_mode) ) return PATH_TREE; + return PATH_DIR; + } + if( (x.st_size%512)==0 ) return PATH_DB; + return PATH_OTHER; +} + +/* +** Return the size of a file in bytes. Or return -1 if the +** named object is not a regular file or does not exist. +*/ +static sqlite3_int64 fileSize(const char *zPath){ + struct stat x; + int rc; + memset(&x, 0, sizeof(x)); + rc = stat(zPath, &x); + if( rc<0 ) return -1; + if( !S_ISREG(x.st_mode) ) return -1; + return x.st_size; +} + +/* +** A Pseudo-random number generator with a fixed seed. Use this so +** that the same sequence of "random" numbers are generated on each +** run, for repeatability. +*/ +static unsigned int randInt(void){ + static unsigned int x = 0x333a13cd; + static unsigned int y = 0xecb2adea; + x = (x>>1) ^ ((1+~(x&1)) & 0xd0000001); + y = y*1103515245 + 12345; + return x^y; +} + +/* +** Do database initialization. +*/ +static int initMain(int argc, char **argv){ + char *zDb; + int i, rc; + int nCount = 1000; + int sz = 10000; + int iVariance = 0; + int pgsz = 4096; + sqlite3 *db; + char *zSql; + char *zErrMsg = 0; + + assert( strcmp(argv[1],"init")==0 ); + assert( argc>=3 ); + zDb = argv[2]; + for(i=3; i<argc; i++){ + char *z = argv[i]; + if( z[0]!='-' ) fatalError("unknown argument: \"%s\"", z); + if( z[1]=='-' ) z++; + if( strcmp(z, "-count")==0 ){ + if( i==argc-1 ) fatalError("missing argument on \"%s\"", argv[i]); + nCount = integerValue(argv[++i]); + if( nCount<1 ) fatalError("the --count must be positive"); + continue; + } + if( strcmp(z, "-size")==0 ){ + if( i==argc-1 ) fatalError("missing argument on \"%s\"", argv[i]); + sz = integerValue(argv[++i]); + if( sz<1 ) fatalError("the --size must be positive"); + continue; + } + if( strcmp(z, "-variance")==0 ){ + if( i==argc-1 ) fatalError("missing argument on \"%s\"", argv[i]); + iVariance = integerValue(argv[++i]); + continue; + } + if( strcmp(z, "-pagesize")==0 ){ + if( i==argc-1 ) fatalError("missing argument on \"%s\"", argv[i]); + pgsz = integerValue(argv[++i]); + if( pgsz<512 || pgsz>65536 || ((pgsz-1)&pgsz)!=0 ){ + fatalError("the --pagesize must be power of 2 between 512 and 65536"); + } + continue; + } + fatalError("unknown option: \"%s\"", argv[i]); + } + rc = sqlite3_open(zDb, &db); + if( rc ){ + fatalError("cannot open database \"%s\": %s", zDb, sqlite3_errmsg(db)); + } + zSql = sqlite3_mprintf( + "DROP TABLE IF EXISTS kv;\n" + "PRAGMA page_size=%d;\n" + "VACUUM;\n" + "BEGIN;\n" + "CREATE TABLE kv(k INTEGER PRIMARY KEY, v BLOB);\n" + "WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<%d)" + " INSERT INTO kv(k,v) SELECT x, randomblob(%d+(random()%%(%d))) FROM c;\n" + "COMMIT;\n", + pgsz, nCount, sz, iVariance+1 + ); + rc = sqlite3_exec(db, zSql, 0, 0, &zErrMsg); + if( rc ) fatalError("database create failed: %s", zErrMsg); + sqlite3_free(zSql); + sqlite3_close(db); + return 0; +} + +/* +** Analyze an existing database file. Report its content. +*/ +static int statMain(int argc, char **argv){ + char *zDb; + int i, rc; + sqlite3 *db; + char *zSql; + sqlite3_stmt *pStmt; + int doVacuum = 0; + + assert( strcmp(argv[1],"stat")==0 ); + assert( argc>=3 ); + zDb = argv[2]; + for(i=3; i<argc; i++){ + char *z = argv[i]; + if( z[0]!='-' ) fatalError("unknown argument: \"%s\"", z); + if( z[1]=='-' ) z++; + if( strcmp(z, "-vacuum")==0 ){ + doVacuum = 1; + continue; + } + fatalError("unknown option: \"%s\"", argv[i]); + } + rc = sqlite3_open(zDb, &db); + if( rc ){ + fatalError("cannot open database \"%s\": %s", zDb, sqlite3_errmsg(db)); + } + if( doVacuum ){ + printf("Vacuuming...."); fflush(stdout); + sqlite3_exec(db, "VACUUM", 0, 0, 0); + printf(" done\n"); + } + zSql = sqlite3_mprintf( + "SELECT count(*), min(length(v)), max(length(v)), avg(length(v))" + " FROM kv" + ); + rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); + if( rc ) fatalError("cannot prepare SQL [%s]: %s", zSql, sqlite3_errmsg(db)); + sqlite3_free(zSql); + if( sqlite3_step(pStmt)==SQLITE_ROW ){ + printf("Number of entries: %8d\n", sqlite3_column_int(pStmt, 0)); + printf("Average value size: %8d\n", sqlite3_column_int(pStmt, 3)); + printf("Minimum value size: %8d\n", sqlite3_column_int(pStmt, 1)); + printf("Maximum value size: %8d\n", sqlite3_column_int(pStmt, 2)); + }else{ + printf("No rows\n"); + } + sqlite3_finalize(pStmt); + zSql = sqlite3_mprintf("PRAGMA page_size"); + rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); + if( rc ) fatalError("cannot prepare SQL [%s]: %s", zSql, sqlite3_errmsg(db)); + sqlite3_free(zSql); + if( sqlite3_step(pStmt)==SQLITE_ROW ){ + printf("Page-size: %8d\n", sqlite3_column_int(pStmt, 0)); + } + sqlite3_finalize(pStmt); + zSql = sqlite3_mprintf("PRAGMA page_count"); + rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); + if( rc ) fatalError("cannot prepare SQL [%s]: %s", zSql, sqlite3_errmsg(db)); + sqlite3_free(zSql); + if( sqlite3_step(pStmt)==SQLITE_ROW ){ + printf("Page-count: %8d\n", sqlite3_column_int(pStmt, 0)); + } + sqlite3_finalize(pStmt); + zSql = sqlite3_mprintf("PRAGMA freelist_count"); + rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); + if( rc ) fatalError("cannot prepare SQL [%s]: %s", zSql, sqlite3_errmsg(db)); + sqlite3_free(zSql); + if( sqlite3_step(pStmt)==SQLITE_ROW ){ + printf("Freelist-count: %8d\n", sqlite3_column_int(pStmt, 0)); + } + sqlite3_finalize(pStmt); + rc = sqlite3_prepare_v2(db, "PRAGMA integrity_check(10)", -1, &pStmt, 0); + if( rc ) fatalError("cannot prepare integrity check: %s", sqlite3_errmsg(db)); + while( sqlite3_step(pStmt)==SQLITE_ROW ){ + printf("Integrity-check: %s\n", sqlite3_column_text(pStmt, 0)); + } + sqlite3_finalize(pStmt); + sqlite3_close(db); + return 0; +} + +/* +** remember(V,PTR) +** +** Return the integer value V. Also save the value of V in a +** C-language variable whose address is PTR. +*/ +static void rememberFunc( + sqlite3_context *pCtx, + int argc, + sqlite3_value **argv +){ + sqlite3_int64 v; + sqlite3_int64 ptr; + assert( argc==2 ); + v = sqlite3_value_int64(argv[0]); + ptr = sqlite3_value_int64(argv[1]); + *(sqlite3_int64*)SQLITE_INT_TO_PTR(ptr) = v; + sqlite3_result_int64(pCtx, v); +} + +/* +** Make sure a directory named zDir exists. +*/ +static void kvtest_mkdir(const char *zDir){ +#if defined(_WIN32) + (void)mkdir(zDir); +#else + (void)mkdir(zDir, 0755); +#endif +} + +/* +** Export the kv table to individual files in the filesystem +*/ +static int exportMain(int argc, char **argv){ + char *zDb; + char *zDir; + sqlite3 *db; + sqlite3_stmt *pStmt; + int rc; + int ePathType; + int nFN; + char *zFN; + char *zTail; + size_t nWrote; + int i; + + assert( strcmp(argv[1],"export")==0 ); + assert( argc>=3 ); + if( argc<4 ) fatalError("Usage: kvtest export DATABASE DIRECTORY [OPTIONS]"); + zDb = argv[2]; + zDir = argv[3]; + kvtest_mkdir(zDir); + for(i=4; i<argc; i++){ + const char *z = argv[i]; + if( z[0]=='-' && z[1]=='-' ) z++; + if( strcmp(z,"-tree")==0 ){ + zFN = sqlite3_mprintf("%s/00", zDir); + kvtest_mkdir(zFN); + sqlite3_free(zFN); + continue; + } + fatalError("unknown argument: \"%s\"\n", argv[i]); + } + ePathType = pathType(zDir); + if( ePathType!=PATH_DIR && ePathType!=PATH_TREE ){ + fatalError("object \"%s\" is not a directory", zDir); + } + rc = sqlite3_open(zDb, &db); + if( rc ){ + fatalError("cannot open database \"%s\": %s", zDb, sqlite3_errmsg(db)); + } + rc = sqlite3_prepare_v2(db, "SELECT k, v FROM kv ORDER BY k", -1, &pStmt, 0); + if( rc ){ + fatalError("prepare_v2 failed: %s\n", sqlite3_errmsg(db)); + } + nFN = (int)strlen(zDir); + zFN = sqlite3_mprintf("%s/00/00/00.extra---------------------", zDir); + if( zFN==0 ){ + fatalError("malloc failed\n"); + } + zTail = zFN + nFN + 1; + while( sqlite3_step(pStmt)==SQLITE_ROW ){ + int iKey = sqlite3_column_int(pStmt, 0); + sqlite3_int64 nData = sqlite3_column_bytes(pStmt, 1); + const void *pData = sqlite3_column_blob(pStmt, 1); + FILE *out; + if( ePathType==PATH_DIR ){ + sqlite3_snprintf(20, zTail, "%06d", iKey); + }else{ + sqlite3_snprintf(20, zTail, "%02d", iKey/10000); + kvtest_mkdir(zFN); + sqlite3_snprintf(20, zTail, "%02d/%02d", iKey/10000, (iKey/100)%100); + kvtest_mkdir(zFN); + sqlite3_snprintf(20, zTail, "%02d/%02d/%02d", + iKey/10000, (iKey/100)%100, iKey%100); + } + out = fopen(zFN, "wb"); + nWrote = fwrite(pData, 1, (size_t)nData, out); + fclose(out); + printf("\r%s ", zTail); fflush(stdout); + if( nWrote!=(size_t)nData ){ + fatalError("Wrote only %d of %d bytes to %s\n", + (int)nWrote, nData, zFN); + } + } + sqlite3_finalize(pStmt); + sqlite3_close(db); + sqlite3_free(zFN); + printf("\n"); + return 0; +} + +/* +** Read the content of file zName into memory obtained from sqlite3_malloc64() +** and return a pointer to the buffer. The caller is responsible for freeing +** the memory. +** +** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes +** read. +** +** For convenience, a nul-terminator byte is always appended to the data read +** from the file before the buffer is returned. This byte is not included in +** the final value of (*pnByte), if applicable. +** +** NULL is returned if any error is encountered. The final value of *pnByte +** is undefined in this case. +*/ +static unsigned char *readFile(const char *zName, sqlite3_int64 *pnByte){ + FILE *in; /* FILE from which to read content of zName */ + sqlite3_int64 nIn; /* Size of zName in bytes */ + size_t nRead; /* Number of bytes actually read */ + unsigned char *pBuf; /* Content read from disk */ + + nIn = fileSize(zName); + if( nIn<0 ) return 0; + in = fopen(zName, "rb"); + if( in==0 ) return 0; + pBuf = sqlite3_malloc64( nIn ); + if( pBuf==0 ) return 0; + nRead = fread(pBuf, (size_t)nIn, 1, in); + fclose(in); + if( nRead!=1 ){ + sqlite3_free(pBuf); + return 0; + } + if( pnByte ) *pnByte = nIn; + return pBuf; +} + +/* +** Overwrite a file with randomness. Do not change the size of the +** file. +*/ +static void updateFile(const char *zName, sqlite3_int64 *pnByte, int doFsync){ + FILE *out; /* FILE from which to read content of zName */ + sqlite3_int64 sz; /* Size of zName in bytes */ + size_t nWritten; /* Number of bytes actually read */ + unsigned char *pBuf; /* Content to store on disk */ + const char *zMode = "wb"; /* Mode for fopen() */ + + sz = fileSize(zName); + if( sz<0 ){ + fatalError("No such file: \"%s\"", zName); + } + *pnByte = sz; + if( sz==0 ) return; + pBuf = sqlite3_malloc64( sz ); + if( pBuf==0 ){ + fatalError("Cannot allocate %lld bytes\n", sz); + } + sqlite3_randomness((int)sz, pBuf); +#if defined(_WIN32) + if( doFsync ) zMode = "wbc"; +#endif + out = fopen(zName, zMode); + if( out==0 ){ + fatalError("Cannot open \"%s\" for writing\n", zName); + } + nWritten = fwrite(pBuf, 1, (size_t)sz, out); + if( doFsync ){ +#if defined(_WIN32) + fflush(out); +#else + fsync(fileno(out)); +#endif + } + fclose(out); + if( nWritten!=(size_t)sz ){ + fatalError("Wrote only %d of %d bytes to \"%s\"\n", + (int)nWritten, (int)sz, zName); + } + sqlite3_free(pBuf); +} + +/* +** Return the current time in milliseconds since the beginning of +** the Julian epoch. +*/ +static sqlite3_int64 timeOfDay(void){ + static sqlite3_vfs *clockVfs = 0; + sqlite3_int64 t; + if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0); + if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){ + clockVfs->xCurrentTimeInt64(clockVfs, &t); + }else{ + double r; + clockVfs->xCurrentTime(clockVfs, &r); + t = (sqlite3_int64)(r*86400000.0); + } + return t; +} + +#ifdef __linux__ +/* +** Attempt to display I/O stats on Linux using /proc/PID/io +*/ +static void displayLinuxIoStats(FILE *out){ + FILE *in; + char z[200]; + sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid()); + in = fopen(z, "rb"); + if( in==0 ) return; + while( fgets(z, sizeof(z), in)!=0 ){ + static const struct { + const char *zPattern; + const char *zDesc; + } aTrans[] = { + { "rchar: ", "Bytes received by read():" }, + { "wchar: ", "Bytes sent to write():" }, + { "syscr: ", "Read() system calls:" }, + { "syscw: ", "Write() system calls:" }, + { "read_bytes: ", "Bytes read from storage:" }, + { "write_bytes: ", "Bytes written to storage:" }, + { "cancelled_write_bytes: ", "Cancelled write bytes:" }, + }; + int i; + for(i=0; i<sizeof(aTrans)/sizeof(aTrans[0]); i++){ + int n = (int)strlen(aTrans[i].zPattern); + if( strncmp(aTrans[i].zPattern, z, n)==0 ){ + fprintf(out, "%-36s %s", aTrans[i].zDesc, &z[n]); + break; + } + } + } + fclose(in); +} +#endif + +/* +** Display memory stats. +*/ +static int display_stats( + sqlite3 *db, /* Database to query */ + int bReset /* True to reset SQLite stats */ +){ + int iCur; + int iHiwtr; + FILE *out = stdout; + + fprintf(out, "\n"); + + iHiwtr = iCur = -1; + sqlite3_status(SQLITE_STATUS_MEMORY_USED, &iCur, &iHiwtr, bReset); + fprintf(out, + "Memory Used: %d (max %d) bytes\n", + iCur, iHiwtr); + iHiwtr = iCur = -1; + sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &iCur, &iHiwtr, bReset); + fprintf(out, "Number of Outstanding Allocations: %d (max %d)\n", + iCur, iHiwtr); + iHiwtr = iCur = -1; + sqlite3_status(SQLITE_STATUS_PAGECACHE_USED, &iCur, &iHiwtr, bReset); + fprintf(out, + "Number of Pcache Pages Used: %d (max %d) pages\n", + iCur, iHiwtr); + iHiwtr = iCur = -1; + sqlite3_status(SQLITE_STATUS_PAGECACHE_OVERFLOW, &iCur, &iHiwtr, bReset); + fprintf(out, + "Number of Pcache Overflow Bytes: %d (max %d) bytes\n", + iCur, iHiwtr); + iHiwtr = iCur = -1; + sqlite3_status(SQLITE_STATUS_MALLOC_SIZE, &iCur, &iHiwtr, bReset); + fprintf(out, "Largest Allocation: %d bytes\n", + iHiwtr); + iHiwtr = iCur = -1; + sqlite3_status(SQLITE_STATUS_PAGECACHE_SIZE, &iCur, &iHiwtr, bReset); + fprintf(out, "Largest Pcache Allocation: %d bytes\n", + iHiwtr); + + iHiwtr = iCur = -1; + sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset); + fprintf(out, "Pager Heap Usage: %d bytes\n", + iCur); + iHiwtr = iCur = -1; + sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1); + fprintf(out, "Page cache hits: %d\n", iCur); + iHiwtr = iCur = -1; + sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1); + fprintf(out, "Page cache misses: %d\n", iCur); + iHiwtr = iCur = -1; + sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1); + fprintf(out, "Page cache writes: %d\n", iCur); + iHiwtr = iCur = -1; + +#ifdef __linux__ + displayLinuxIoStats(out); +#endif + + return 0; +} + +/* Blob access order */ +#define ORDER_ASC 1 +#define ORDER_DESC 2 +#define ORDER_RANDOM 3 + + +/* +** Run a performance test +*/ +static int runMain(int argc, char **argv){ + int eType; /* Is zDb a database or a directory? */ + char *zDb; /* Database or directory name */ + int i; /* Loop counter */ + int rc; /* Return code from SQLite calls */ + int nCount = 1000; /* Number of blob fetch operations */ + int nExtra = 0; /* Extra cycles */ + int iKey = 1; /* Next blob key */ + int iMax = 0; /* Largest allowed key */ + int iPagesize = 0; /* Database page size */ + int iCache = 1000; /* Database cache size in kibibytes */ + int bBlobApi = 0; /* Use the incremental blob I/O API */ + int bStats = 0; /* Print stats before exiting */ + int eOrder = ORDER_ASC; /* Access order */ + int isUpdateTest = 0; /* Do in-place updates rather than reads */ + int doIntegrityCk = 0; /* Run PRAGMA integrity_check after the test */ + int noSync = 0; /* Disable synchronous mode */ + int doFsync = 0; /* Update disk files synchronously */ + int doMultiTrans = 0; /* Each operation in its own transaction */ + int noCheckpoint = 0; /* Omit the checkpoint in WAL mode */ + sqlite3 *db = 0; /* Database connection */ + sqlite3_stmt *pStmt = 0; /* Prepared statement for SQL access */ + sqlite3_blob *pBlob = 0; /* Handle for incremental Blob I/O */ + sqlite3_int64 tmStart; /* Start time */ + sqlite3_int64 tmElapsed; /* Elapsed time */ + int mmapSize = 0; /* --mmap N argument */ + sqlite3_int64 nData = 0; /* Bytes of data */ + sqlite3_int64 nTotal = 0; /* Total data read */ + unsigned char *pData = 0; /* Content of the blob */ + sqlite3_int64 nAlloc = 0; /* Space allocated for pData[] */ + const char *zJMode = 0; /* Journal mode */ + + + assert( strcmp(argv[1],"run")==0 ); + assert( argc>=3 ); + zDb = argv[2]; + eType = pathType(zDb); + if( eType==PATH_OTHER ) fatalError("unknown object type: \"%s\"", zDb); + if( eType==PATH_NEXIST ) fatalError("object does not exist: \"%s\"", zDb); + for(i=3; i<argc; i++){ + char *z = argv[i]; + if( z[0]!='-' ) fatalError("unknown argument: \"%s\"", z); + if( z[1]=='-' ) z++; + if( strcmp(z, "-asc")==0 ){ + eOrder = ORDER_ASC; + continue; + } + if( strcmp(z, "-blob-api")==0 ){ + bBlobApi = 1; + continue; + } + if( strcmp(z, "-cache-size")==0 ){ + if( i==argc-1 ) fatalError("missing argument on \"%s\"", argv[i]); + iCache = integerValue(argv[++i]); + continue; + } + if( strcmp(z, "-count")==0 ){ + if( i==argc-1 ) fatalError("missing argument on \"%s\"", argv[i]); + nCount = integerValue(argv[++i]); + if( nCount<1 ) fatalError("the --count must be positive"); + continue; + } + if( strcmp(z, "-desc")==0 ){ + eOrder = ORDER_DESC; + continue; + } + if( strcmp(z, "-fsync")==0 ){ + doFsync = 1; + continue; + } + if( strcmp(z, "-integrity-check")==0 ){ + doIntegrityCk = 1; + continue; + } + if( strcmp(z, "-jmode")==0 ){ + if( i==argc-1 ) fatalError("missing argument on \"%s\"", argv[i]); + zJMode = argv[++i]; + continue; + } + if( strcmp(z, "-mmap")==0 ){ + if( i==argc-1 ) fatalError("missing argument on \"%s\"", argv[i]); + mmapSize = integerValue(argv[++i]); + if( nCount<0 ) fatalError("the --mmap must be non-negative"); + continue; + } + if( strcmp(z, "-max-id")==0 ){ + if( i==argc-1 ) fatalError("missing argument on \"%s\"", argv[i]); + iMax = integerValue(argv[++i]); + continue; + } + if( strcmp(z, "-multitrans")==0 ){ + doMultiTrans = 1; + continue; + } + if( strcmp(z, "-nocheckpoint")==0 ){ + noCheckpoint = 1; + continue; + } + if( strcmp(z, "-nosync")==0 ){ + noSync = 1; + continue; + } + if( strcmp(z, "-random")==0 ){ + eOrder = ORDER_RANDOM; + continue; + } + if( strcmp(z, "-start")==0 ){ + if( i==argc-1 ) fatalError("missing argument on \"%s\"", argv[i]); + iKey = integerValue(argv[++i]); + if( iKey<1 ) fatalError("the --start must be positive"); + continue; + } + if( strcmp(z, "-stats")==0 ){ + bStats = 1; + continue; + } + if( strcmp(z, "-update")==0 ){ + isUpdateTest = 1; + continue; + } + fatalError("unknown option: \"%s\"", argv[i]); + } + if( eType==PATH_DB ){ + /* Recover any prior crashes prior to starting the timer */ + sqlite3_open(zDb, &db); + sqlite3_exec(db, "SELECT rowid FROM sqlite_schema LIMIT 1", 0, 0, 0); + sqlite3_close(db); + db = 0; + } + tmStart = timeOfDay(); + if( eType==PATH_DB ){ + char *zSql; + rc = sqlite3_open(zDb, &db); + if( rc ){ + fatalError("cannot open database \"%s\": %s", zDb, sqlite3_errmsg(db)); + } + zSql = sqlite3_mprintf("PRAGMA mmap_size=%d", mmapSize); + sqlite3_exec(db, zSql, 0, 0, 0); + sqlite3_free(zSql); + zSql = sqlite3_mprintf("PRAGMA cache_size=%d", iCache); + sqlite3_exec(db, zSql, 0, 0, 0); + sqlite3_free(zSql); + if( noSync ){ + sqlite3_exec(db, "PRAGMA synchronous=OFF", 0, 0, 0); + } + pStmt = 0; + sqlite3_prepare_v2(db, "PRAGMA page_size", -1, &pStmt, 0); + if( sqlite3_step(pStmt)==SQLITE_ROW ){ + iPagesize = sqlite3_column_int(pStmt, 0); + } + sqlite3_finalize(pStmt); + sqlite3_prepare_v2(db, "PRAGMA cache_size", -1, &pStmt, 0); + if( sqlite3_step(pStmt)==SQLITE_ROW ){ + iCache = sqlite3_column_int(pStmt, 0); + }else{ + iCache = 0; + } + sqlite3_finalize(pStmt); + pStmt = 0; + if( zJMode ){ + zSql = sqlite3_mprintf("PRAGMA journal_mode=%Q", zJMode); + sqlite3_exec(db, zSql, 0, 0, 0); + sqlite3_free(zSql); + if( noCheckpoint ){ + sqlite3_exec(db, "PRAGMA wal_autocheckpoint=0", 0, 0, 0); + } + } + sqlite3_prepare_v2(db, "PRAGMA journal_mode", -1, &pStmt, 0); + if( sqlite3_step(pStmt)==SQLITE_ROW ){ + zJMode = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0)); + }else{ + zJMode = "???"; + } + sqlite3_finalize(pStmt); + if( iMax<=0 ){ + sqlite3_prepare_v2(db, "SELECT max(k) FROM kv", -1, &pStmt, 0); + if( sqlite3_step(pStmt)==SQLITE_ROW ){ + iMax = sqlite3_column_int(pStmt, 0); + } + sqlite3_finalize(pStmt); + } + pStmt = 0; + if( !doMultiTrans ) sqlite3_exec(db, "BEGIN", 0, 0, 0); + } + if( iMax<=0 ) iMax = 1000; + for(i=0; i<nCount; i++){ + if( eType==PATH_DIR || eType==PATH_TREE ){ + /* CASE 1: Reading or writing blobs out of separate files */ + char *zKey; + if( eType==PATH_DIR ){ + zKey = sqlite3_mprintf("%s/%06d", zDb, iKey); + }else{ + zKey = sqlite3_mprintf("%s/%02d/%02d/%02d", zDb, iKey/10000, + (iKey/100)%100, iKey%100); + } + nData = 0; + if( isUpdateTest ){ + updateFile(zKey, &nData, doFsync); + }else{ + pData = readFile(zKey, &nData); + sqlite3_free(pData); + } + sqlite3_free(zKey); + }else if( bBlobApi ){ + /* CASE 2: Reading from database using the incremental BLOB I/O API */ + if( pBlob==0 ){ + rc = sqlite3_blob_open(db, "main", "kv", "v", iKey, + isUpdateTest, &pBlob); + if( rc ){ + fatalError("could not open sqlite3_blob handle: %s", + sqlite3_errmsg(db)); + } + }else{ + rc = sqlite3_blob_reopen(pBlob, iKey); + } + if( rc==SQLITE_OK ){ + nData = sqlite3_blob_bytes(pBlob); + if( nAlloc<nData+1 ){ + nAlloc = nData+100; + pData = sqlite3_realloc64(pData, nAlloc); + } + if( pData==0 ) fatalError("cannot allocate %d bytes", nData+1); + if( isUpdateTest ){ + sqlite3_randomness((int)nData, pData); + rc = sqlite3_blob_write(pBlob, pData, (int)nData, 0); + if( rc!=SQLITE_OK ){ + fatalError("could not write the blob at %d: %s", iKey, + sqlite3_errmsg(db)); + } + }else{ + rc = sqlite3_blob_read(pBlob, pData, (int)nData, 0); + if( rc!=SQLITE_OK ){ + fatalError("could not read the blob at %d: %s", iKey, + sqlite3_errmsg(db)); + } + } + } + }else{ + /* CASE 3: Reading from database using SQL */ + if( pStmt==0 ){ + if( isUpdateTest ){ + sqlite3_create_function(db, "remember", 2, SQLITE_UTF8, 0, + rememberFunc, 0, 0); + + rc = sqlite3_prepare_v2(db, + "UPDATE kv SET v=randomblob(remember(length(v),?2))" + " WHERE k=?1", -1, &pStmt, 0); + sqlite3_bind_int64(pStmt, 2, SQLITE_PTR_TO_INT(&nData)); + }else{ + rc = sqlite3_prepare_v2(db, + "SELECT v FROM kv WHERE k=?1", -1, &pStmt, 0); + } + if( rc ){ + fatalError("cannot prepare query: %s", sqlite3_errmsg(db)); + } + }else{ + sqlite3_reset(pStmt); + } + sqlite3_bind_int(pStmt, 1, iKey); + nData = 0; + rc = sqlite3_step(pStmt); + if( rc==SQLITE_ROW ){ + nData = sqlite3_column_bytes(pStmt, 0); + pData = (unsigned char*)sqlite3_column_blob(pStmt, 0); + } + } + if( eOrder==ORDER_ASC ){ + iKey++; + if( iKey>iMax ) iKey = 1; + }else if( eOrder==ORDER_DESC ){ + iKey--; + if( iKey<=0 ) iKey = iMax; + }else{ + iKey = (randInt()%iMax)+1; + } + nTotal += nData; + if( nData==0 ){ nCount++; nExtra++; } + } + if( nAlloc ) sqlite3_free(pData); + if( pStmt ) sqlite3_finalize(pStmt); + if( pBlob ) sqlite3_blob_close(pBlob); + if( bStats ){ + display_stats(db, 0); + } + if( db ){ + if( !doMultiTrans ) sqlite3_exec(db, "COMMIT", 0, 0, 0); + if( !noCheckpoint ){ + sqlite3_close(db); + db = 0; + } + } + tmElapsed = timeOfDay() - tmStart; + if( db && noCheckpoint ){ + sqlite3_close(db); + db = 0; + } + if( nExtra ){ + printf("%d cycles due to %d misses\n", nCount, nExtra); + } + if( eType==PATH_DB ){ + printf("SQLite version: %s\n", sqlite3_libversion()); + if( doIntegrityCk ){ + sqlite3_open(zDb, &db); + sqlite3_prepare_v2(db, "PRAGMA integrity_check", -1, &pStmt, 0); + while( sqlite3_step(pStmt)==SQLITE_ROW ){ + printf("integrity-check: %s\n", sqlite3_column_text(pStmt, 0)); + } + sqlite3_finalize(pStmt); + sqlite3_close(db); + db = 0; + } + } + printf("--count %d --max-id %d", nCount-nExtra, iMax); + switch( eOrder ){ + case ORDER_RANDOM: printf(" --random\n"); break; + case ORDER_DESC: printf(" --desc\n"); break; + default: printf(" --asc\n"); break; + } + if( eType==PATH_DB ){ + printf("--cache-size %d --jmode %s\n", iCache, zJMode); + printf("--mmap %d%s\n", mmapSize, bBlobApi ? " --blob-api" : ""); + if( noSync ) printf("--nosync\n"); + } + if( iPagesize ) printf("Database page size: %d\n", iPagesize); + printf("Total elapsed time: %.3f\n", tmElapsed/1000.0); + if( isUpdateTest ){ + printf("Microseconds per BLOB write: %.3f\n", tmElapsed*1000.0/nCount); + printf("Content write rate: %.1f MB/s\n", nTotal/(1000.0*tmElapsed)); + }else{ + printf("Microseconds per BLOB read: %.3f\n", tmElapsed*1000.0/nCount); + printf("Content read rate: %.1f MB/s\n", nTotal/(1000.0*tmElapsed)); + } + return 0; +} + + +int main(int argc, char **argv){ + if( argc<3 ) showHelp(); + if( strcmp(argv[1],"init")==0 ){ + return initMain(argc, argv); + } + if( strcmp(argv[1],"export")==0 ){ + return exportMain(argc, argv); + } + if( strcmp(argv[1],"run")==0 ){ + return runMain(argc, argv); + } + if( strcmp(argv[1],"stat")==0 ){ + return statMain(argc, argv); + } + showHelp(); + return 0; +} diff --git a/test/lemon-test01.y b/test/lemon-test01.y new file mode 100644 index 0000000000..67890c6376 --- /dev/null +++ b/test/lemon-test01.y @@ -0,0 +1,80 @@ +// A test case for the LEMON parser generator. Run as follows: +// +// lemon lemon-test01.y && gcc -g lemon-test01.c && ./a.out +// +// This testcase was made obsolete by check-in 7cca80808cef192f on +// 2021-08-17 (associated with Forum Thread +// https://sqlite.org/forum/forumpost/bd91fd965c9803c4) and no longer +// works. It is retained for historical reference only. +// +%token_prefix TK_ +%token_type int +%default_type int +%include { + static int nSyntaxError = 0; + static int nAccept = 0; + static int nFailure = 0; +} + +all ::= A B. +all ::= error B. + +%syntax_error { + nSyntaxError++; +} +%parse_accept { + nAccept++; +} +%parse_failure { + nFailure++; +} +%code { + #include <assert.h> + #include "lemon-test01.h" + static int nTest = 0; + static int nErr = 0; + static void testCase(int testId, int shouldBe, int actual){ + nTest++; + if( shouldBe==actual ){ + printf("test %d: ok\n", testId); + }else{ + printf("test %d: got %d, expected %d\n", testId, actual, shouldBe); + nErr++; + } + } + int main(int argc, char **argv){ + yyParser xp; + ParseInit(&xp); + Parse(&xp, TK_A, 0); + Parse(&xp, TK_B, 0); + Parse(&xp, 0, 0); + ParseFinalize(&xp); + testCase(100, 0, nSyntaxError); + testCase(110, 1, nAccept); + testCase(120, 0, nFailure); + nSyntaxError = nAccept = nFailure = 0; + ParseInit(&xp); + Parse(&xp, TK_B, 0); + Parse(&xp, TK_B, 0); + Parse(&xp, 0, 0); + ParseFinalize(&xp); + testCase(200, 1, nSyntaxError); + testCase(210, 1, nAccept); + testCase(220, 0, nFailure); + nSyntaxError = nAccept = nFailure = 0; + ParseInit(&xp); + Parse(&xp, TK_A, 0); + Parse(&xp, TK_A, 0); + Parse(&xp, 0, 0); + ParseFinalize(&xp); + testCase(200, 1, nSyntaxError); + testCase(210, 0, nAccept); + testCase(220, 0, nFailure); + if( nErr==0 ){ + printf("%d tests pass\n", nTest); + }else{ + printf("%d errors out %d tests\n", nErr, nTest); + } + return nErr; + } +} diff --git a/test/like.test b/test/like.test index fba89e9037..1f9a810e5d 100644 --- a/test/like.test +++ b/test/like.test @@ -17,6 +17,7 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl +set testprefix like # Create some sample data to work with. # @@ -160,19 +161,20 @@ ifcapable !like_opt { # proc queryplan {sql} { set ::sqlite_sort_count 0 + db cache flush set data [execsql $sql] if {$::sqlite_sort_count} {set x sort} {set x nosort} lappend data $x set eqp [execsql "EXPLAIN QUERY PLAN $sql"] # puts eqp=$eqp foreach {a b c x} $eqp { - if {[regexp { TABLE (\w+ AS )?(\w+) USING COVERING INDEX (\w+)\y} \ - $x all as tab idx]} { + if {[regexp {(SCAN|SEARCH) (\w+ AS )?(\w+) USING COVERING INDEX (\w+)\y} \ + $x all ss as tab idx]} { lappend data {} $idx - } elseif {[regexp { TABLE (\w+ AS )?(\w+) USING.* INDEX (\w+)\y} \ - $x all as tab idx]} { + } elseif {[regexp {(SCAN|SEARCH) (\w+ AS )?(\w+) USING.* INDEX (\w+)\y} \ + $x all ss as tab idx]} { lappend data $tab $idx - } elseif {[regexp { TABLE (\w+ AS )?(\w+)\y} $x all as tab]} { + } elseif {[regexp {(SCAN|SEARCH) (\w+ AS )?(\w+)\y} $x all ss as tab]} { lappend data $tab * } } @@ -196,7 +198,7 @@ do_test like-3.2 { # With an index on t1.x and case sensitivity on, optimize completely. # -do_test like-3.3 { +do_test like-3.3.100 { set sqlite_like_count 0 execsql { PRAGMA case_sensitive_like=on; @@ -206,10 +208,57 @@ do_test like-3.3 { SELECT x FROM t1 WHERE x LIKE 'abc%' ORDER BY 1; } } {abc abcd nosort {} i1} -do_test like-3.4 { +do_test like-3.3.100.cnt { set sqlite_like_count } 0 +# The like optimization works even when the pattern is a bound parameter +# +# Exception: It does not work if sqlite3_prepare() is used instead of +# sqlite3_prepare_v2(), as in that case the statement cannot be reprepared +# after the parameter is bound. +# +unset -nocomplain ::likepat +set ::likepat abc% +if {[permutation]!="prepare"} { + do_test like-3.3.102 { + set sqlite_like_count 0 + queryplan { + SELECT x FROM t1 WHERE x LIKE $::likepat ORDER BY 1; + } + } {abc abcd nosort {} i1} + do_test like-3.3.103 { + set sqlite_like_count + } 0 +} + +# Except, the like optimization does not work for bound parameters if +# the query planner stability guarantee is active. +# +do_test like-3.3.104 { + set sqlite_like_count 0 + sqlite3_db_config db QPSG 1 + queryplan { + SELECT x FROM t1 WHERE x LIKE $::likepat ORDER BY 1; + } +} {abc abcd nosort {} i1} +do_test like-3.3.105 { + set sqlite_like_count +} 12 + +# The query planner stability guarantee does not disrupt explicit patterns +# +do_test like-3.3.105 { + set sqlite_like_count 0 + queryplan { + SELECT x FROM t1 WHERE x LIKE 'abc%' ORDER BY 1; + } +} {abc abcd nosort {} i1} +do_test like-3.3.106 { + set sqlite_like_count +} 0 +sqlite3_db_config db QPSG 0 + # The LIKE optimization still works when the RHS is a string with no # wildcard. Ticket [e090183531fc2747] # @@ -677,21 +726,21 @@ ifcapable like_opt&&!icu { set res [sqlite3_exec_hex db { EXPLAIN QUERY PLAN SELECT x FROM t2 WHERE x LIKE '%ff%25' }] - regexp {SCAN TABLE t2} $res + regexp {SCAN t2} $res } {1} } do_test like-9.5.1 { set res [sqlite3_exec_hex db { - SELECT x FROM t2 WHERE x LIKE '%fe%25' + SELECT 1 FROM t2 WHERE x LIKE '%fe%25' }] - } {0 {}} + } {0 {1 1}} ifcapable explain { do_test like-9.5.2 { set res [sqlite3_exec_hex db { EXPLAIN QUERY PLAN SELECT x FROM t2 WHERE x LIKE '%fe%25' }] regexp {INDEX i2} $res - } {1} + } {0} } # Do an SQL statement. Append the search count to the end of the result. @@ -964,7 +1013,7 @@ do_execsql_test like-12.16 { SELECT id FROM t12b WHERE x LIKE 'abc%' COLLATE binary ORDER BY +id; } {/SCAN/} -# Ticket [https://www.sqlite.org/src/tktview/80369eddd5c94d49f7fbbcf5] +# Ticket [https://sqlite.org/src/tktview/80369eddd5c94d49f7fbbcf5] # 2016-01-20 # do_execsql_test like-13.1 { @@ -980,6 +1029,135 @@ do_execsql_test like-13.4 { SELECT char(0x4d) LIKE char(0x6d); } {1} +# Performance testing for patterns with many wildcards. These LIKE and GLOB +# patterns were quite slow with SQLite 3.15.2 and earlier. +# +do_test like-14.1 { + set x [lindex [time { + db one {SELECT 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaz'GLOB'*a*a*a*a*a*a*a*a*y'} + }] 0] + set tlimit [expr {1000 * $::sqlite_options(configslower)}] + puts -nonewline " ($x ms - want less than $tlimit) " + expr {$x<$tlimit} +} {1} +ifcapable !icu { + do_test like-14.2 { + set x [lindex [time { + db one {SELECT 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaz'LIKE'%a%a%a%a%a%a%a%a%y'} + }] 0] + set tlimit [expr {1000 * $::sqlite_options(configslower)}] + puts -nonewline " ($x ms - want less than $tlimit) " + expr {$x<$tlimit} + } {1} +} + +ifcapable !icu { +# As of 2017-07-27 (3.21.0) the LIKE optimization works with ESCAPE as +# long as the ESCAPE is a single-byte literal. +# +db close +sqlite3 db :memory: +do_execsql_test like-15.100 { + CREATE TABLE t15(x TEXT COLLATE nocase, y, PRIMARY KEY(x)); + INSERT INTO t15(x,y) VALUES + ('abcde',1), ('ab%de',2), ('a_cde',3), + ('uvwxy',11),('uvwx%',12),('uvwx_',13), + ('_bcde',21),('%bcde',22), + ('abcd_',31),('abcd%',32), + ('ab%xy',41); + SELECT y FROM t15 WHERE x LIKE 'ab/%d%' ESCAPE '/'; +} {2} +do_execsql_test like-15.101 { + EXPLAIN QUERY PLAN + SELECT y FROM t15 WHERE x LIKE 'ab/%d%' ESCAPE '/'; +} {/SEARCH/} +do_execsql_test like-15.102 { + EXPLAIN QUERY PLAN + SELECT y FROM t15 WHERE x LIKE 'ab/%d%' ESCAPE '//'; +} {/SCAN/} +do_execsql_test like-15.103 { + EXPLAIN QUERY PLAN + SELECT y FROM t15 WHERE x LIKE 'ab/%d%' ESCAPE ''; +} {/SCAN/} +do_execsql_test like-15.110 { + SELECT y FROM t15 WHERE x LIKE 'abcdx%%' ESCAPE 'x'; +} {32} +do_execsql_test like-15.111 { + SELECT y FROM t15 WHERE x LIKE 'abx%%' ESCAPE 'x' ORDER BY +y +} {2 41} +do_execsql_test like-15.112 { + EXPLAIN QUERY PLAN + SELECT y FROM t15 WHERE x LIKE 'abx%%' ESCAPE 'x' ORDER BY +y +} {/SEARCH/} +do_execsql_test like-15.120 { + SELECT y FROM t15 WHERE x LIKE '/%bc%' ESCAPE '/'; +} {22} +do_execsql_test like-15.121 { + EXPLAIN QUERY PLAN + SELECT y FROM t15 WHERE x LIKE '/%bc%' ESCAPE '/'; +} {/SEARCH/} +} + +#------------------------------------------------------------------------- +# Tests for ticket [b1d8c79314]. +# +reset_db +do_execsql_test 16.0 { + CREATE TABLE t1(a INTEGER COLLATE NOCASE); + CREATE INDEX i1 ON t1(a); + INSERT INTO t1 VALUES(' 1x'); + INSERT INTO t1 VALUES(' 1-'); +} +do_execsql_test 16.1 { + SELECT * FROM t1 WHERE a LIKE ' 1%'; +} {{ 1x} { 1-}} +do_execsql_test 16.2 { + SELECT * FROM t1 WHERE a LIKE ' 1-'; +} {{ 1-}} + +# 2020-03-19 +# The ESCAPE clause on LIKE takes precedence over wildcards +# +do_execsql_test 17.0 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(id INTEGER PRIMARY KEY, x TEXT); + INSERT INTO t1 VALUES + (1,'abcde'), + (2,'abc_'), + (3,'abc__'), + (4,'abc%'), + (5,'abc%%'); + SELECT id FROM t1 WHERE x LIKE 'abc%%' ESCAPE '%'; +} {4} +do_execsql_test 17.1 { + SELECT id FROM t1 WHERE x LIKE 'abc__' ESCAPE '_'; +} {2} + +# 2021-02-15 ticket c0aeea67d58ae0fd +# +do_execsql_test 17.1 { + SELECT 'x' LIKE '%' ESCAPE '_'; +} {1} + +# 2023-08-15 https://sqlite.org/forum/forumpost/925dc9f67804c540 +# +reset_db +sqlite3_db_config db DEFENSIVE 1 +db eval {PRAGMA trusted_schema=OFF} +do_execsql_test 18.0 { + CREATE TABLE t1(x INT, y TEXT); + INSERT INTO t1 VALUES(1,'abc'),(2,'ABC'),(3,'Abc'); + CREATE VIEW t2 AS SELECT * FROM t1 WHERE y LIKE 'a%'; + SELECT * FROM t2; +} {1 abc 2 ABC 3 Abc} +do_execsql_test 18.1 { + PRAGMA case_sensitive_like=OFF; + SELECT * FROM t2; +} {1 abc 2 ABC 3 Abc} +do_execsql_test 18.2 { + PRAGMA case_sensitive_like=ON; + SELECT * FROM t2; +} {1 abc} finish_test diff --git a/test/like2.test b/test/like2.test index 4fd16420ad..2a4474edce 100644 --- a/test/like2.test +++ b/test/like2.test @@ -1006,4 +1006,12 @@ do_test like-2.126.3 { } {126} +do_test like-3.1 { + db eval "SELECT '\u01C0' LIKE '%\x80'" +} {0} +do_test like-3.2 { + db eval "SELECT '\u0080' LIKE '%\x80'" +} {1} + + finish_test diff --git a/test/like3.test b/test/like3.test index 9280c2c5d2..03681606c3 100644 --- a/test/like3.test +++ b/test/like3.test @@ -112,4 +112,255 @@ do_execsql_test like3-4.2ck { SELECT quote(x) FROM t4 WHERE x LIKE 'ab%' ORDER BY +x ASC; } {'abc' 'abd' 'abe' X'616263' X'616264' X'616265'} +# 2018-09-10 ticket https://sqlite.org/src/tktview/c94369cae9b561b1f996 +# The like optimization fails for a column with numeric affinity if +# the pattern '/%' or begins with the escape character. +# +do_execsql_test like3-5.100 { + CREATE TABLE t5a(x INT UNIQUE COLLATE nocase); + INSERT INTO t5a(x) VALUES('/abc'),(123),(-234); + SELECT x FROM t5a WHERE x LIKE '/%'; +} {/abc} +do_eqp_test like3-5.101 { + SELECT x FROM t5a WHERE x LIKE '/%'; +} { + QUERY PLAN + `--SCAN t5a +} +do_execsql_test like3-5.110 { + SELECT x FROM t5a WHERE x LIKE '/a%'; +} {/abc} +ifcapable !icu { +do_eqp_test like3-5.111 { + SELECT x FROM t5a WHERE x LIKE '/a%'; +} { + QUERY PLAN + `--SEARCH t5a USING COVERING INDEX sqlite_autoindex_t5a_1 (x>? AND x<?) +} +} +do_execsql_test like3-5.120 { + SELECT x FROM t5a WHERE x LIKE '^12%' ESCAPE '^'; +} {123} +do_eqp_test like3-5.121 { + SELECT x FROM t5a WHERE x LIKE '^12%' ESCAPE '^'; +} { + QUERY PLAN + `--SCAN t5a +} +do_execsql_test like3-5.122 { + SELECT x FROM t5a WHERE x LIKE '^-2%' ESCAPE '^'; +} {-234} +do_eqp_test like3-5.123 { + SELECT x FROM t5a WHERE x LIKE '^12%' ESCAPE '^'; +} { + QUERY PLAN + `--SCAN t5a +} + +do_execsql_test like3-5.200 { + CREATE TABLE t5b(x INT UNIQUE COLLATE binary); + INSERT INTO t5b(x) VALUES('/abc'),(123),(-234); + SELECT x FROM t5b WHERE x GLOB '/*'; +} {/abc} +do_eqp_test like3-5.201 { + SELECT x FROM t5b WHERE x GLOB '/*'; +} { + QUERY PLAN + `--SCAN t5b +} +do_execsql_test like3-5.210 { + SELECT x FROM t5b WHERE x GLOB '/a*'; +} {/abc} +do_eqp_test like3-5.211 { + SELECT x FROM t5b WHERE x GLOB '/a*'; +} { + QUERY PLAN + `--SEARCH t5b USING COVERING INDEX sqlite_autoindex_t5b_1 (x>? AND x<?) +} + +# 2019-05-01 +# another case of the above reported on the mailing list by Manuel Rigger. +# +do_execsql_test like3-5.300 { + CREATE TABLE t5c (c0 REAL); + CREATE INDEX t5c_0 ON t5c(c0 COLLATE NOCASE); + INSERT INTO t5c(rowid, c0) VALUES (99,'+/'); + SELECT * FROM t5c WHERE (c0 LIKE '+/'); +} {+/} + +# 2019-05-08 +# Yet another case for the above from Manuel Rigger. +# +do_execsql_test like3-5.400 { + DROP TABLE IF EXISTS t0; + CREATE TABLE t0(c0 INT UNIQUE COLLATE NOCASE); + INSERT INTO t0(c0) VALUES ('./'); + SELECT * FROM t0 WHERE t0.c0 LIKE './'; +} {./} + +# 2019-06-14 +# Ticket https://sqlite.org/src/info/ce8717f0885af975 +do_execsql_test like3-5.410 { + DROP TABLE IF EXISTS t0; + CREATE TABLE t0(c0 INT UNIQUE COLLATE NOCASE); + INSERT INTO t0(c0) VALUES ('.1%'); + SELECT * FROM t0 WHERE t0.c0 LIKE '.1%'; +} {.1%} + +# 2019-09-03 +# Ticket https://sqlite.org/src/info/0f0428096f +do_execsql_test like3-5.420 { + DROP TABLE IF EXISTS t0; + CREATE TABLE t0(c0 UNIQUE); + INSERT INTO t0(c0) VALUES(-1); + SELECT * FROM t0 WHERE t0.c0 GLOB '-*'; +} {-1} +do_execsql_test like3-5.421 { + SELECT t0.c0 GLOB '-*' FROM t0; +} {1} + + + +# 2019-02-27 +# Verify that the LIKE optimization works with an ESCAPE clause when +# using PRAGMA case_sensitive_like=ON. +# +ifcapable !icu { +do_execsql_test like3-6.100 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(path TEXT COLLATE nocase PRIMARY KEY,a,b,c) WITHOUT ROWID; +} +do_eqp_test like3-6.110 { + SELECT * FROM t1 WHERE path LIKE 'a%'; +} { + QUERY PLAN + `--SEARCH t1 USING PRIMARY KEY (path>? AND path<?) +} +do_eqp_test like3-6.120 { + SELECT * FROM t1 WHERE path LIKE 'a%' ESCAPE 'x'; +} { + QUERY PLAN + `--SEARCH t1 USING PRIMARY KEY (path>? AND path<?) +} +do_execsql_test like3-6.200 { + DROP TABLE IF EXISTS t2; + CREATE TABLE t2(path TEXT,x,y,z); + CREATE INDEX t2path ON t2(path COLLATE nocase); + CREATE INDEX t2path2 ON t2(path); +} +do_eqp_test like3-6.210 { + SELECT * FROM t2 WHERE path LIKE 'a%'; +} { + QUERY PLAN + `--SEARCH t2 USING INDEX t2path (path>? AND path<?) +} +do_eqp_test like3-6.220 { + SELECT * FROM t2 WHERE path LIKE 'a%' ESCAPE '\'; +} { + QUERY PLAN + `--SEARCH t2 USING INDEX t2path (path>? AND path<?) +} +db eval {PRAGMA case_sensitive_like=ON} +do_eqp_test like3-6.230 { + SELECT * FROM t2 WHERE path LIKE 'a%'; +} { + QUERY PLAN + `--SEARCH t2 USING INDEX t2path2 (path>? AND path<?) +} +do_eqp_test like3-6.240 { + SELECT * FROM t2 WHERE path LIKE 'a%' ESCAPE '\'; +} { + QUERY PLAN + `--SEARCH t2 USING INDEX t2path2 (path>? AND path<?) +} +} + +#------------------------------------------------------------------------- + +ifcapable utf16 { + reset_db + do_execsql_test like3-7.0 { + PRAGMA encoding = 'UTF-16be'; + + CREATE TABLE Example(word TEXT NOT NULL); + CREATE INDEX Example_word on Example(word); + + INSERT INTO Example VALUES(char(0x307F)); + } + + do_execsql_test like3-7.1 { + SELECT char(0x307F)=='み'; + } {1} + + do_execsql_test like3-7.1 { + SELECT * FROM Example WHERE word GLOB 'み*' + } {み} + + do_execsql_test like3-7.2 { + SELECT * FROM Example WHERE word >= char(0x307F) AND word < char(0x3080); + } {み} +} + +#------------------------------------------------------------------------- +reset_db + +# See forum thread https://sqlite.org/forum/info/d7b90d92ffbfc61f +foreach enc { + UTF-8 + UTF-16le + UTF-16be +} { + ifcapable icu { + if {$enc=="UTF-8"} { + # The invalid UTF8 used in these tests is incompatible with ICU + # https://sqlite.org/forum/forumpost/2ca8a09a7e + continue + } + } + foreach {tn expr} { + 1 "CAST (X'FF' AS TEXT)" + 2 "CAST (X'FFBF' AS TEXT)" + 3 "CAST (X'FFBFBF' AS TEXT)" + 4 "CAST (X'FFBFBFBF' AS TEXT)" + + 5 "'abc' || CAST (X'FF' AS TEXT)" + 6 "'def' || CAST (X'FFBF' AS TEXT)" + 7 "'ghi' || CAST (X'FFBFBF' AS TEXT)" + 8 "'jkl' || CAST (X'FFBFBFBF' AS TEXT)" + } { + reset_db + execsql "PRAGMA encoding = '$enc'" + set tn utf[string range $enc 4 end].$tn + do_execsql_test like3-8.$tn.1 { + CREATE TABLE t1(x); + } + + do_execsql_test like3-8.$tn.2 { + PRAGMA encoding + } $enc + + do_execsql_test like3-8.$tn.3 " + INSERT INTO t1 VALUES( $expr ) + " + + do_execsql_test like3-8.$tn.4 { + SELECT typeof(x) FROM t1 + } {text} + + set x [db one {SELECT x || '%' FROM t1}] + + do_execsql_test like3-8.$tn.5 { + SELECT rowid FROM t1 WHERE x LIKE $x + } 1 + + do_execsql_test like3-8.$tn.6 { + CREATE INDEX i1 ON t1(x); + } + + do_execsql_test like3-8.$tn.7 { + SELECT rowid FROM t1 WHERE x LIKE $x + } 1 + } +} + finish_test diff --git a/test/limit.test b/test/limit.test index d72749770c..05fbfb7801 100644 --- a/test/limit.test +++ b/test/limit.test @@ -641,5 +641,28 @@ do_execsql_test limit-14.7 { SELECT 123 LIMIT -1 OFFSET 1 } {} +# 2021-03-05 dbsqlfuzz crash-d811039c9f44f2d43199d5889fcf4085ef6221b9 +# +reset_db +do_execsql_test limit-15.1 { + CREATE TABLE t1(a PRIMARY KEY, b TEXT); + CREATE TABLE t4(c PRIMARY KEY, d); + CREATE TABLE t5(e PRIMARY KEY, f); + CREATE TABLE t6(g, h); + CREATE TABLE t3_a(k, v); + CREATE TABLE t3_b(k, v); + CREATE VIEW t3 AS SELECT * FROM t3_a UNION ALL SELECT * FROM t3_b; + INSERT INTO t5(e,f) VALUES(500000,'orange'); + INSERT INTO t4(c,d) VALUES(300000,'blue'),(400,'green'),(8000,'grey'); + INSERT INTO t1(a,b) VALUES(300000,'purple'); + INSERT INTO t3_a VALUES(300000,'yellow'),(500,'pink'),(8000,'red'); + INSERT INTO t6 default values; + SELECT ( + SELECT 100000 FROM + (SELECT 200000 FROM t6 WHERE a = ( SELECT 300000 FROM t3 WHERE a ) ), + (SELECT 400000 FROM t5 WHERE e=500000), + (SELECT 600000 FROM t4 WHERE c=a) + ) FROM t1; +} {100000} finish_test diff --git a/test/limit2.test b/test/limit2.test new file mode 100644 index 0000000000..2ecd8ab5f5 --- /dev/null +++ b/test/limit2.test @@ -0,0 +1,217 @@ +# 2016-05-20 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is testing the LIMIT in combination with ORDER BY +# and in particular, the optimizations in the inner loop that cause an +# early exit of the inner loop when the LIMIT is reached and the inner +# loop is emitting rows in ORDER BY order. + + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix limit2 + +do_execsql_test limit2-100 { + CREATE TABLE t1(a,b); + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<1000) + INSERT INTO t1(a,b) SELECT 1, (x*17)%1000 + 1000 FROM c; + INSERT INTO t1(a,b) VALUES(2,2),(3,1006),(4,4),(5,9999); + CREATE INDEX t1ab ON t1(a,b); +} +set sqlite_search_count 0 +do_execsql_test limit2-100.1 { + SELECT a, b, '|' FROM t1 WHERE a IN (2,4,5,3,1) ORDER BY b LIMIT 5; +} {2 2 | 4 4 | 1 1000 | 1 1001 | 1 1002 |} +set fast_count $sqlite_search_count +set sqlite_search_count 0 +do_execsql_test limit2-100.2 { + SELECT a, b, '|' FROM t1 WHERE a IN (2,4,5,3,1) ORDER BY +b LIMIT 5; +} {2 2 | 4 4 | 1 1000 | 1 1001 | 1 1002 |} +do_test limit2-100.3 { + set slow_count $sqlite_search_count + expr {$fast_count < 0.02*$slow_count} +} {1} + +do_execsql_test limit2-110 { + CREATE TABLE t2(x,y); + INSERT INTO t2(x,y) VALUES('a',1),('a',2),('a',3),('a',4); + INSERT INTO t2(x,y) VALUES('b',1),('c',2),('d',3),('e',4); + CREATE INDEX t2xy ON t2(x,y); +} +set sqlite_search_count 0 +do_execsql_test limit2-110.1 { + SELECT a, b, '|' FROM t2, t1 WHERE t2.x='a' AND t1.a=t2.y ORDER BY t1.b LIMIT 5; +} {2 2 | 4 4 | 1 1000 | 1 1001 | 1 1002 |} +set fast_count $sqlite_search_count +set sqlite_search_count 0 +do_execsql_test limit2-110.2 { + SELECT a, b, '|' FROM t2, t1 WHERE t2.x='a' AND t1.a=t2.y ORDER BY +t1.b LIMIT 5; +} {2 2 | 4 4 | 1 1000 | 1 1001 | 1 1002 |} +set slow_count $sqlite_search_count +do_test limit2-110.3 { + expr {$fast_count < 0.02*$slow_count} +} {1} + +do_execsql_test limit2-120 { + DROP INDEX t1ab; + CREATE INDEX t1ab ON t1(a,b DESC); +} +set sqlite_search_count 0 +do_execsql_test limit2-120.1 { + SELECT a, b, '|' FROM t1 WHERE a IN (2,4,5,3,1) ORDER BY b DESC LIMIT 5; +} {5 9999 | 1 1999 | 1 1998 | 1 1997 | 1 1996 |} +set fast_count $sqlite_search_count +set sqlite_search_count 0 +do_execsql_test limit2-120.2 { + SELECT a, b, '|' FROM t1 WHERE a IN (2,4,5,3,1) ORDER BY +b DESC LIMIT 5; +} {5 9999 | 1 1999 | 1 1998 | 1 1997 | 1 1996 |} +do_test limit2-120.3 { + set slow_count $sqlite_search_count + expr {$fast_count < 0.02*$slow_count} +} {1} + +# Bug report against the new ORDER BY LIMIT optimization just prior to +# release. (Unreleased so there is no ticket). +# +# Make sure the optimization is not applied if the inner loop can only +# provide a single row of output. +# +do_execsql_test limit2-200 { + CREATE TABLE t200(a, b); + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<1000) + INSERT INTO t200(a,b) SELECT x, x FROM c; + CREATE TABLE t201(x INTEGER PRIMARY KEY, y); + INSERT INTO t201(x,y) VALUES(2,12345); + + SELECT *, '|' FROM t200, t201 WHERE x=b ORDER BY y LIMIT 3; +} {2 2 2 12345 |} +do_execsql_test limit2-210 { + SELECT *, '|' FROM t200 LEFT JOIN t201 ON x=b ORDER BY y LIMIT 3; +} {1 1 {} {} | 3 3 {} {} | 4 4 {} {} |} + +# Bug in the ORDER BY LIMIT optimization reported on 2016-09-06. +# Ticket https://sqlite.org/src/info/559733b09e96 +# +do_execsql_test limit2-300 { + CREATE TABLE t300(a,b,c); + CREATE INDEX t300x ON t300(a,b,c); + INSERT INTO t300 VALUES(0,1,99),(0,1,0),(0,0,0); + SELECT *,'.' FROM t300 WHERE a=0 AND (c=0 OR c=99) ORDER BY c DESC; +} {0 1 99 . 0 0 0 . 0 1 0 .} +do_execsql_test limit2-310 { + SELECT *,'.' FROM t300 WHERE a=0 AND (c=0 OR c=99) ORDER BY c DESC LIMIT 1; +} {0 1 99 .} + +# Make sure the SELECT loop is ordered correctly for the direction of +# the ORDER BY +# +do_execsql_test limit2-400 { + CREATE TABLE t400(a,b); + CREATE INDEX t400_ab ON t400(a,b); + INSERT INTO t400(a,b) VALUES(1,90),(1,40),(2,80),(2,30),(3,70),(3,20); + SELECT *,'x' FROM t400 WHERE a IN (1,2,3) ORDER BY b DESC LIMIT 3; + SELECT *,'y' FROM t400 WHERE a IN (1,2,3) ORDER BY +b DESC LIMIT 3; +} {1 90 x 2 80 x 3 70 x 1 90 y 2 80 y 3 70 y} + + +do_execsql_test 500 { + CREATE TABLE t500(i INTEGER PRIMARY KEY, j); + INSERT INTO t500 VALUES(1, 1); + INSERT INTO t500 VALUES(2, 2); + INSERT INTO t500 VALUES(3, 3); + INSERT INTO t500 VALUES(4, 0); + INSERT INTO t500 VALUES(5, 5); + SELECT j FROM t500 WHERE i IN (1,2,3,4,5) ORDER BY j DESC LIMIT 3; +} {5 3 2} +do_execsql_test 501 { + CREATE TABLE t501(i INTEGER PRIMARY KEY, j); + INSERT INTO t501 VALUES(1, 5); + INSERT INTO t501 VALUES(2, 4); + INSERT INTO t501 VALUES(3, 3); + INSERT INTO t501 VALUES(4, 6); + INSERT INTO t501 VALUES(5, 1); + SELECT j FROM t501 WHERE i IN (1,2,3,4,5) ORDER BY j LIMIT 3; +} {1 3 4} +do_execsql_test 502 { + CREATE TABLE t502(i INT PRIMARY KEY, j); + INSERT INTO t502 VALUES(1, 5); + INSERT INTO t502 VALUES(2, 4); + INSERT INTO t502 VALUES(3, 3); + INSERT INTO t502 VALUES(4, 6); + INSERT INTO t502 VALUES(5, 1); + SELECT j FROM t502 WHERE i IN (1,2,3,4,5) ORDER BY j LIMIT 3; +} {1 3 4} + +# Ticket https://sqlite.org/src/info/123c9ba32130a6c9 2017-12-13 +# Incorrect result when an idnex is used for an ordered join. +# +# This test case is in the limit2.test module because the problem was first +# exposed by check-in https://sqlite.org/src/info/559733b09e which +# implemented the ORDER BY LIMIT optimization that limit2.test strives to +# test. +# +do_execsql_test 600 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a, b); INSERT INTO t1 VALUES(1,2); + DROP TABLE IF EXISTS t2; + CREATE TABLE t2(x, y); INSERT INTO t2 VALUES(1,3); + CREATE INDEX t1ab ON t1(a,b); + SELECT y FROM t1, t2 WHERE a=x AND b<=y ORDER BY b DESC; +} {3} + +# Ticket https://sqlite.org/src/info/9936b2fa443fec03 2018-09-08 +# Infinite loop due to the ORDER BY LIMIT optimization. +# +do_execsql_test 700 { + DROP TABLE IF EXISTS t1; + DROP TABLE IF EXISTS t2; + CREATE TABLE t1(aa VARCHAR PRIMARY KEY NOT NULL,bb,cc,x VARCHAR(400)); + INSERT INTO t1(aa,bb,cc) VALUES('maroon','meal','lecture'); + INSERT INTO t1(aa,bb,cc) VALUES('reality','meal','catsear'); + CREATE TABLE t2(aa VARCHAR PRIMARY KEY, dd INT DEFAULT 1, ee, x VARCHAR(100)); + INSERT INTO t2(aa,dd,ee) VALUES('maroon',0,'travel'),('reality',0,'hour'); + CREATE INDEX t2x1 ON t2(dd,ee); + ANALYZE; + DROP TABLE IF EXISTS sqlite_stat4; + DELETE FROM sqlite_stat1; + INSERT INTO sqlite_stat1 VALUES + ('t2','t2x1','3 3 3'), + ('t2','sqlite_autoindex_t2_1','3 1'), + ('t1','sqlite_autoindex_t1_1','2 1'); + ANALYZE sqlite_master; + SELECT * + FROM t1 LEFT JOIN t2 ON t1.aa=t2.aa + WHERE t1.bb='meal' + ORDER BY t2.dd DESC + LIMIT 1; +} {maroon meal lecture {} maroon 0 travel {}} +do_execsql_test 710 { + DROP TABLE t1; + DROP TABLE t2; + CREATE TABLE t1(aa, bb); + INSERT INTO t1 VALUES('maroon','meal'); + CREATE TABLE t2(cc, dd, ee, x VARCHAR(100)); + INSERT INTO t2(cc,dd,ee) VALUES('maroon',1,'one'); + INSERT INTO t2(cc,dd,ee) VALUES('maroon',2,'two'); + INSERT INTO t2(cc,dd,ee) VALUES('maroon',0,'zero'); + CREATE INDEX t2ddee ON t2(dd,ee); + CREATE INDEX t2cc ON t2(cc); + ANALYZE; + SELECT t2.cc, t2.dd, t2.ee FROM t1 CROSS JOIN t2 ON t1.aa=t2.cc + ORDER BY t2.dd LIMIT 1; +} {maroon 0 zero} +do_execsql_test 720 { + SELECT t2.cc, t2.dd, t2.ee FROM t1 CROSS JOIN t2 ON t1.aa=t2.cc + WHERE t1.bb='meal' + ORDER BY t2.dd LIMIT 1; +} {maroon 0 zero} + +finish_test diff --git a/test/literal.test b/test/literal.test new file mode 100644 index 0000000000..5aa331e39b --- /dev/null +++ b/test/literal.test @@ -0,0 +1,103 @@ +# 2024-01-19 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# This file implements tests for SQL literals + + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set ::testprefix literal + +proc test_literal {tn lit type val} { + do_execsql_test $tn.1 "SELECT typeof( $lit ), $lit" [list $type $val] + + ifcapable altertable { + do_execsql_test $tn.2 " + DROP TABLE IF EXISTS x1; + CREATE TABLE x1(a); + INSERT INTO x1 VALUES(123); + ALTER TABLE x1 ADD COLUMN b DEFAULT $lit ; + SELECT typeof(b), b FROM x1; + " [list $type $val] + } + + do_execsql_test $tn.3 " + DROP TABLE IF EXISTS x1; + CREATE TABLE x1(a DEFAULT $lit); + INSERT INTO x1 DEFAULT VALUES; + SELECT typeof(a), a FROM x1; + " [list $type $val] +} + +proc test_literal_error {tn lit unrec} { + do_catchsql_test $tn "SELECT $lit" "1 {unrecognized token: \"$unrec\"}" +} + + +test_literal 1.0 45 integer 45 +test_literal 1.1 0xFF integer 255 +test_literal 1.2 0xFFFFFFFF integer [expr 0xFFFFFFFF] +test_literal 1.3 0x123FFFFFFFF integer [expr 0x123FFFFFFFF] +test_literal 1.4 -0x123FFFFFFFF integer [expr -1 * 0x123FFFFFFFF] +test_literal 1.5 0xFFFFFFFFFFFFFFFF integer -1 +test_literal 1.7 0x7FFFFFFFFFFFFFFF integer [expr 0x7FFFFFFFFFFFFFFF] +test_literal 1.8 -0x7FFFFFFFFFFFFFFF integer [expr -0x7FFFFFFFFFFFFFFF] +test_literal 1.9 +0x7FFFFFFFFFFFFFFF integer [expr +0x7FFFFFFFFFFFFFFF] +test_literal 1.10 -45 integer -45 +test_literal 1.11 '0xFF' text 0xFF +test_literal 1.12 '-0xFF' text -0xFF +test_literal 1.13 -'0xFF' integer 0 +test_literal 1.14 -9223372036854775808 integer -9223372036854775808 + +test_literal 2.1 1e12 real 1000000000000.0 +test_literal 2.2 1.0 real 1.0 +test_literal 2.3 1e1000 real Inf +test_literal 2.4 -1e1000 real -Inf + +test_literal 3.1 1_000 integer 1000 +test_literal 3.2 1.1_1 real 1.11 +test_literal 3.3 1_0.1_1 real 10.11 +test_literal 3.4 1e1_000 real Inf +test_literal 3.5 12_3_456.7_8_9 real 123456.789 +test_literal 3.6 9_223_372_036_854_775_807 integer 9223372036854775807 +test_literal 3.7 9_223_372_036_854_775_808 real 9.22337203685478e+18 +test_literal 3.8 -9_223_372_036_854_775_808 integer -9223372036854775808 + +foreach {tn lit unrec} { + 0 123a456 123a456 + 1 1_ 1_ + 2 1_.4 1_.4 + 3 1e_4 1e_4 + 4 1_e4 1_e4 + 5 1.4_e4 1.4_e4 + 6 1.4e+_4 1.4e + 7 1.4e-_4 1.4e + 8 1.4e4_ 1.4e4_ + 9 1.4_e4 1.4_e4 + 10 1.4e_4 1.4e_4 + 11 12__34 12__34 + 12 1234_ 1234_ + 13 12._34 12._34 + 14 12_.34 12_.34 + 15 12.34_ 12.34_ + 16 1.0e1_______2 1.0e1_______2 +} { + test_literal_error 4.$tn $lit $unrec +} + +# dbsqlfuzz e3186a9e7826e9cd7f4085aa4452f8696485f9e1 +# See tag-20240224-a and -b +# +do_catchsql_test 5.1 { + SELECT 1 ORDER BY 2_3; +} {1 {1st ORDER BY term out of range - should be between 1 and 1}} + +finish_test diff --git a/test/literal2.tcl b/test/literal2.tcl new file mode 100644 index 0000000000..e14a03587b --- /dev/null +++ b/test/literal2.tcl @@ -0,0 +1,40 @@ +# 2018 May 19 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +source [file join [file dirname $argv0] pg_common.tcl] + +#========================================================================= + + +start_test literal2 "2024 Jan 23" + +execsql_test 1.0 { SELECT 123_456 } +errorsql_test 1.1 { SELECT 123__456 } + +execsql_float_test 2.1 { SELECT 1.0e1_2 } + + +execsql_test 3.0.0 { SELECT 0xFF_FF } +execsql_test 3.0.1 { SELECT 0xFF_EF } +errorsql_test 3.0.2 { SELECT 0xFF__EF } +# errorsql_test 3.0.3 { SELECT 0x_FFEF } +errorsql_test 3.0.4 { SELECT 0xFFEF_ } + +execsql_test 3.1.0 { SELECT 0XFF_FF } +execsql_test 3.1.1 { SELECT 0XFF_EF } +errorsql_test 3.1.2 { SELECT 0XFF__EF } +# errorsql_test 3.1.3 { SELECT 0X_FFEF } +errorsql_test 3.1.4 { SELECT 0XFFEF_ } + +finish_test + + diff --git a/test/literal2.test b/test/literal2.test new file mode 100644 index 0000000000..ed177ca261 --- /dev/null +++ b/test/literal2.test @@ -0,0 +1,84 @@ +# 2024 Jan 23 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. +# + +#################################################### +# DO NOT EDIT! THIS FILE IS AUTOMATICALLY GENERATED! +#################################################### + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix literal2 + +do_execsql_test 1.0 { + SELECT 123_456 +} {123456} + +# PG says ERROR: trailing junk after numeric literal at or near "123_" +do_test 1.1 { catch { execsql { + SELECT 123__456 +} } } 1 + + +do_test 2.1 { + set myres {} + foreach r [db eval {SELECT 1.0e1_2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1000000000000.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + +do_execsql_test 3.0.0 { + SELECT 0xFF_FF +} {65535} + +do_execsql_test 3.0.1 { + SELECT 0xFF_EF +} {65519} + +# PG says ERROR: trailing junk after numeric literal at or near "0xFF_" +do_test 3.0.2 { catch { execsql { + SELECT 0xFF__EF +} } } 1 + +# PG says ERROR: trailing junk after numeric literal at or near "0xFFEF_" +do_test 3.0.4 { catch { execsql { + SELECT 0xFFEF_ +} } } 1 + +do_execsql_test 3.1.0 { + SELECT 0XFF_FF +} {65535} + +do_execsql_test 3.1.1 { + SELECT 0XFF_EF +} {65519} + +# PG says ERROR: trailing junk after numeric literal at or near "0XFF_" +do_test 3.1.2 { catch { execsql { + SELECT 0XFF__EF +} } } 1 + +# PG says ERROR: trailing junk after numeric literal at or near "0XFFEF_" +do_test 3.1.4 { catch { execsql { + SELECT 0XFFEF_ +} } } 1 + +finish_test diff --git a/test/loadext.test b/test/loadext.test index 7ba4c0cf77..e95d5f6eb8 100644 --- a/test/loadext.test +++ b/test/loadext.test @@ -61,14 +61,18 @@ set dlerror_notadll {%s: file too short} set dlerror_nosymbol {%s: undefined symbol: %s} if {$::tcl_platform(os) eq "Darwin"} { - set dlerror_nosuchfile {dlopen(%s, 10): image not found} - set dlerror_notadll {dlopen(%1$s, 10): no suitable image found.*} - set dlerror_nosymbol {dlsym(XXX, %2$s): symbol not found} + set dlerror_nosuchfile {dlopen.%s, 10.: .*image.*found.*} + set dlerror_notadll {dlopen.%1$s, 10.: .*image.*found.*} + set dlerror_nosymbol {dlsym.XXX, %2$s.: symbol not found} } -if {$::tcl_platform(platform) eq "windows"} { +if {$::tcl_platform(os) eq "Windows NT"} { set dlerror_nosuchfile {The specified module could not be found.*} - set dlerror_notadll {%%1 is not a valid Win32 application.*} + if {$::tcl_platform(platform) eq "unix"} { + set dlerror_notadll $dlerror_nosuchfile + } else { + set dlerror_notadll {%%1 is not a valid Win32 application.*} + } set dlerror_nosymbol {The specified procedure could not be found.*} } @@ -81,7 +85,7 @@ if {![file exists $testextension]} { set testextsrc $srcdir/test_loadext.c set cmdline [concat exec gcc $gcc_shared] - lappend cmdline -Wall -I$srcdir -I. -g $testextsrc -o $testextension + lappend cmdline -Wall -I$srcdir -I. -I.. -g $testextsrc -o $testextension if {[catch $cmdline msg]} { puts "Skipping loadext tests: Test extension not built..." @@ -111,7 +115,7 @@ do_test loadext-1.2 { # do_test loadext-1.3 { sqlite3 db2 test.db - sqlite3_enable_load_extension db2 1 + sqlite3_db_config db2 SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION 1 catchsql { SELECT half(1.0); } db2 @@ -256,6 +260,7 @@ do_test loadext-4.2 { } } {0 {{}}} +# disable all extension loading do_test loadext-4.3 { sqlite3_enable_load_extension db 0 catchsql { @@ -263,6 +268,15 @@ do_test loadext-4.3 { } } {1 {not authorized}} +# enable C-api extension loading only. Show that the SQL function +# still does not work. +do_test loadext-4.4 { + sqlite3_db_config db SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION 1 + catchsql { + SELECT load_extension($::testextension,'testloadext_init') + } +} {1 {not authorized}} + source $testdir/malloc_common.tcl diff --git a/test/lock.test b/test/lock.test index f785c4bb23..3f4631a623 100644 --- a/test/lock.test +++ b/test/lock.test @@ -21,7 +21,7 @@ source $testdir/tester.tcl # do_test lock-1.0 { # Give a complex pathname to stress the path simplification logic in - # the vxworks driver and in test_async. + # the vxworks driver. file mkdir tempdir/t1/t2 sqlite3 db2 ./tempdir/../tempdir/t1/.//t2/../../..//test.db set dummy {} @@ -145,7 +145,7 @@ do_test lock-1.21 { # connections, because UNIX supports reader/writer locks. Under windows, # this is not possible. # -if {$::tcl_platform(platform)=="unix"} { +if {$::tcl_platform(platform) eq "unix"} { do_test lock-1.22 { db eval {SELECT * FROM t1} qv { set r [catch {db2 eval {SELECT a FROM t1}} msg] @@ -423,8 +423,9 @@ do_test lock-6.5 { # * there exists one or more active read-only statements, and # * a transaction that modified zero database pages is committed. # -set temp_status unlocked -if {$TEMP_STORE>=2} {set temp_status unknown} +#set temp_status unlocked +#if {$TEMP_STORE>=2} {set temp_status unknown} +set temp_status unknown do_test lock-7.1 { set STMT [sqlite3_prepare $DB "SELECT * FROM sqlite_master" -1 TAIL] sqlite3_step $STMT diff --git a/test/lock4.test b/test/lock4.test index b0b1c74fbe..58dd206997 100644 --- a/test/lock4.test +++ b/test/lock4.test @@ -17,6 +17,14 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl +if {[atomic_batch_write test.db]} { + # This test uses two processes, one of which blocks until the other + # creates a *-journal file. Which doesn't work if atomic writes are + # available. + finish_test + return +} + do_not_use_codec # Initialize the test.db database so that it is non-empty diff --git a/test/lock5.test b/test/lock5.test index 99214afb19..8ebc277018 100644 --- a/test/lock5.test +++ b/test/lock5.test @@ -11,10 +11,10 @@ # This file implements regression tests for SQLite library. The # focus of this script is database locks. # -# $Id: lock5.test,v 1.6 2008/12/04 12:34:16 drh Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl +set testprefix lock5 # This file is only run if using the unix backend compiled with the # SQLITE_ENABLE_LOCKING_STYLE macro. @@ -101,10 +101,7 @@ do_test lock5-dotfile.X { ##################################################################### forcedelete test.db -if {[catch {sqlite3 db test.db -vfs unix-flock} msg]} { - finish_test - return -} +if {0==[catch {sqlite3 db test.db -vfs unix-flock} msg]} { do_test lock5-flock.1 { sqlite3 db test.db -vfs unix-flock @@ -149,13 +146,67 @@ do_test lock5-flock.8 { db2 close } {} +do_test lock5-flock.9 { + sqlite3 db test.db -vfs unix-flock + execsql { + SELECT * FROM t1 + } +} {1 2} + +do_test lock5-flock.10 { + sqlite3 db2 test.db -vfs unix-flock + execsql { + SELECT * FROM t1 + } db2 +} {1 2} + +do_test lock5-flock.10 { + execsql { + PRAGMA cache_size = 1; + BEGIN; + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<10000 + ) + INSERT INTO t1 SELECT i, i+1 FROM s; + } + + catchsql { + SELECT * FROM t1 + } db2 +} {1 {database is locked}} + +if {[permutation]!="inmemory_journal"} { + do_test lock5-flock.11 { + forcecopy test.db test.db2 + forcecopy test.db-journal test.db2-journal + db2 close + sqlite3 db2 test.db2 -vfs unix-flock + catchsql { + SELECT * FROM t1 + } db2 + } {0 {1 2}} + + do_test lock5-flock.12 { + file exists test.db2-journal + } 0 +} + +db close +db2 close + +} + ##################################################################### +reset_db + do_test lock5-none.1 { sqlite3 db test.db -vfs unix-none sqlite3 db2 test.db -vfs unix-none execsql { PRAGMA mmap_size = 0 } db2 execsql { + CREATE TABLE t1(a, b); + INSERT INTO t1 VALUES(1, 2); BEGIN; INSERT INTO t1 VALUES(3, 4); } @@ -178,10 +229,12 @@ do_test lock5-none.5 { } {1 2} ifcapable memorymanage { - do_test lock5-none.6 { - sqlite3_release_memory 1000000 - execsql {SELECT * FROM t1} db2 - } {1 2 3 4} + if {[permutation]!="memsubsys1" && [permutation]!="memsubsys2"} { + do_test lock5-none.6 { + sqlite3_release_memory 1000000 + execsql {SELECT * FROM t1} db2 + } {1 2 3 4} + } } do_test lock5-none.X { @@ -193,4 +246,74 @@ ifcapable lock_proxy_pragmas { set env(SQLITE_FORCE_PROXY_LOCKING) $::using_proxy } +##################################################################### +reset_db +if {[permutation]!="inmemory_journal"} { + + # 1. Create a large database using the unix-dotfile VFS + # 2. Write a large transaction to the db, so that the cache spills, but do + # not commit it. + # 3. Make a copy of the database files on disk. + # 4. Try to read from the copy using unix-dotfile VFS. This fails because + # the dotfile still exists, so SQLite thinks the database is locked. + # 5. Remove the dotfile. + # 6. Try to read the db again. This time, the old transaction is rolled + # back and the read permitted. + # + do_test 2.dotfile.1 { + sqlite3 db test.db -vfs unix-dotfile + execsql { + PRAGMA cache_size = 10; + CREATE TABLE t1(x, y, z); + CREATE INDEX t1x ON t1(x); + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<1000 + ) + INSERT INTO t1 SELECT hex(randomblob(20)), hex(randomblob(500)), i FROM s; + } + } {} + + do_execsql_test 2.dotfile.2 { + BEGIN; + UPDATE t1 SET z=z+1, x=hex(randomblob(20)); + } + + do_test 2.dotfile.3 { + list \ + [file exists test.db] \ + [file exists test.db-journal] \ + [file exists test.db.lock] + } {1 1 1} + + do_test 2.dotfile.4 { + forcecopy test.db test.db2 + forcecopy test.db-journal test.db2-journal + file mkdir test.db2.lock + + sqlite3 db2 test.db2 -vfs unix-dotfile + catchsql { + SELECT count(*) FROM t1; + } db2 + } {1 {database is locked}} + + do_test 2.dotfile.5 { + file delete test.db2.lock + execsql { + PRAGMA integrity_check + } db2 + } {ok} + + db2 close + + do_test 2.dotfile.6 { + forcecopy test.db test.db2 + forcecopy test.db-journal test.db2-journal + + sqlite3 db2 file:test.db2?nolock=1 -vfs unix-dotfile -uri 1 + catchsql { + SELECT count(*) FROM t1; + } db2 + } {0 1000} +} + finish_test diff --git a/test/lock_common.tcl b/test/lock_common.tcl index a758e7af2e..7c79e4b4c5 100644 --- a/test/lock_common.tcl +++ b/test/lock_common.tcl @@ -15,18 +15,20 @@ proc do_multiclient_test {varname script} { - foreach code [list { + foreach {tn code} [list 1 { if {[info exists ::G(valgrind)]} { db close ; continue } set ::code2_chan [launch_testfixture] set ::code3_chan [launch_testfixture] proc code2 {tcl} { testfixture $::code2_chan $tcl } proc code3 {tcl} { testfixture $::code3_chan $tcl } - set tn 1 - } { + } 2 { proc code2 {tcl} { uplevel #0 $tcl } proc code3 {tcl} { uplevel #0 $tcl } - set tn 2 }] { + # Do not run multi-process tests with the unix-excl VFS. + # + if {$tn==1 && [permutation]=="unix-excl"} continue + faultsim_delete_and_reopen proc code1 {tcl} { uplevel #0 $tcl } @@ -143,6 +145,7 @@ proc testfixture_nb_cb {varname chan} { } if { $line == "OVER" } { + if {[string range $varname 0 1]!="::"} { global $varname } set $varname [lindex $::tfnb($chan) 1] unset ::tfnb($chan) close $chan diff --git a/test/lookaside.test b/test/lookaside.test index a89110ee1d..e1d91c8543 100644 --- a/test/lookaside.test +++ b/test/lookaside.test @@ -24,7 +24,7 @@ ifcapable !lookaside { # The tests in this file configure the lookaside allocator after a # connection is opened. This will not work if there is any "presql" # configured (SQL run within the [sqlite3] wrapper in tester.tcl). -if {[info exists ::G(perm:presql)]} { +if {[info exists ::G(perm:dbconfig)] && $::G(perm:dbconfig)!=""} { finish_test return } @@ -33,10 +33,11 @@ test_set_config_pagecache 0 0 catch {db close} sqlite3_shutdown -sqlite3_config_scratch 0 0 sqlite3_initialize autoinstall_test_functions + sqlite3 db test.db +db cache size 4 # Make sure sqlite3_db_config() and sqlite3_db_status are working. # @@ -100,6 +101,7 @@ do_test lookaside-2.2 { expr {$x==0 && $y<$z && $z>10 && $z<100} } {1} do_test lookaside-2.3 { + db eval {SELECT 1} sqlite3_db_config_lookaside db 0 50 50 } {5} ;# SQLITE_BUSY do_test lookaside-2.4 { diff --git a/test/main.test b/test/main.test index 3f35afe20c..556a8bdfcc 100644 --- a/test/main.test +++ b/test/main.test @@ -319,7 +319,7 @@ do_test main-3.1 { sqlite3 db testdb set v [catch {execsql {SELECT * from T1 where x!!5}} msg] lappend v $msg -} {1 {unrecognized token: "!!"}} +} {1 {unrecognized token: "!"}} do_test main-3.2 { catch {db close} foreach f [glob -nocomplain testdb/*] {forcedelete $f} @@ -434,7 +434,7 @@ do_test main-3.2.28 { } {0 246} do_test main-3.2.29 { catchsql {select 123/} -} {1 {near "/": syntax error}} +} {1 {incomplete input}} do_test main-3.2.30 { catchsql {select 123--5} } {0 123} @@ -467,72 +467,15 @@ do_test main-3.4 { do_test main-3.5 { set v [catch {execsql {create}} msg] lappend v $msg -} {1 {near "create": syntax error}} +} {1 {incomplete input}} do_test main-3.6 { catchsql {SELECT 'abc' + #9} } {1 {near "#9": syntax error}} -# The following test-case tests the linked list code used to manage -# sqlite3_vfs structures. -if {$::tcl_platform(platform)=="unix" - && [info command sqlite3async_initialize]!=""} { - ifcapable threadsafe { - do_test main-4.1 { - sqlite3_crash_enable 1 - sqlite3_crash_enable 0 - - sqlite3async_initialize "" 1 - sqlite3async_shutdown - - sqlite3_crash_enable 1 - sqlite3async_initialize "" 1 - sqlite3_crash_enable 0 - sqlite3async_shutdown - - sqlite3_crash_enable 1 - sqlite3async_initialize "" 1 - sqlite3async_shutdown - sqlite3_crash_enable 0 - - sqlite3async_initialize "" 1 - sqlite3_crash_enable 1 - sqlite3_crash_enable 0 - sqlite3async_shutdown - - sqlite3async_initialize "" 1 - sqlite3_crash_enable 1 - sqlite3async_shutdown - sqlite3_crash_enable 0 - } {} - do_test main-4.2 { - set rc [catch {sqlite3 db test.db -vfs crash} msg] - list $rc $msg - } {1 {no such vfs: crash}} - do_test main-4.3 { - set rc [catch {sqlite3 db test.db -vfs async} msg] - list $rc $msg - } {1 {no such vfs: async}} - } -} - # Print the version number so that it can be picked up by releasetest.tcl. # puts [db one {SELECT 'VERSION: ' || sqlite_version() || ' ' || sqlite_source_id();}] - -# Do deliberate failures if the TEST_FAILURE environment variable is set. -# This is done to verify that failure notifications are detected by the -# releasetest.tcl script, or possibly by other scripts involved in automatic -# testing. -# -if {[info exists ::env(TEST_FAILURE)]} { - set res 123 - if {$::env(TEST_FAILURE)==0} {set res 234} - do_test main-99.1 { - bad_behavior $::env(TEST_FAILURE) - set x 123 - } $res -} - + finish_test diff --git a/test/malloc.test b/test/malloc.test index dbf4699b27..e53bfcd192 100644 --- a/test/malloc.test +++ b/test/malloc.test @@ -329,7 +329,7 @@ ifcapable crashtest&&attach { } } -if {$tcl_platform(platform)!="windows"} { +if {$tcl_platform(platform) ne "windows" && [atomic_batch_write test.db]==0} { do_malloc_test 14 -tclprep { catch {db close} sqlite3 db2 test2.db diff --git a/test/malloc3.test b/test/malloc3.test index f4a6c3bbe9..b497ab66e9 100644 --- a/test/malloc3.test +++ b/test/malloc3.test @@ -27,6 +27,17 @@ if {!$MEMDEBUG} { return } +# Do not run these tests if F2FS batch writes are supported. In this case, +# it is possible for a single DML statement in an implicit transaction +# to fail with SQLITE_NOMEM, but for the transaction to still end up +# committed to disk. Which confuses the tests in this module. +# +if {[atomic_batch_write test.db]} { + puts "Skipping malloc3 tests: atomic-batch support" + finish_test + return +} + # Do not run these tests with an in-memory journal. # diff --git a/test/malloc5.test b/test/malloc5.test index 8f0db04c2a..906ac89834 100644 --- a/test/malloc5.test +++ b/test/malloc5.test @@ -39,11 +39,27 @@ ifcapable !memorymanage { return } +# The sizes of memory allocations from system malloc() might vary, +# depending on the memory allocator algorithms used. The following +# routine is designed to support answers that fall within a range +# of values while also supplying easy-to-understand "expected" values +# when errors occur. +# +proc value_in_range {target x args} { + set v [lindex $args 0] + if {$v!=""} { + if {$v<$target*$x} {return $v} + if {$v>$target/$x} {return $v} + } + return "number between [expr {int($target*$x)}] and [expr {int($target/$x)}]" +} +set mrange 0.98 ;# plus or minus 2% + test_set_config_pagecache 0 100 sqlite3_soft_heap_limit 0 sqlite3 db test.db -db eval {PRAGMA cache_size=1} +# db eval {PRAGMA cache_size=1} do_test malloc5-1.1 { # Simplest possible test. Call sqlite3_release_memory when there is exactly @@ -71,24 +87,8 @@ do_test malloc5-1.3 { # in the cache belonging to db2. # set ::pgalloc [sqlite3_release_memory] -} {0} - -# The sizes of memory allocations from system malloc() might vary, -# depending on the memory allocator algorithms used. The following -# routine is designed to support answers that fall within a range -# of values while also supplying easy-to-understand "expected" values -# when errors occur. -# -proc value_in_range {target x args} { - set v [lindex $args 0] - if {$v!=""} { - if {$v<$target*$x} {return $v} - if {$v>$target/$x} {return $v} - } - return "number between [expr {int($target*$x)}] and [expr {int($target/$x)}]" -} -set mrange 0.98 ;# plus or minus 2% - + value_in_range 1288 0.75 +} [value_in_range 1288 0.75] do_test malloc5-1.4 { # Commit the transaction and open a new one. Read 1 page into the cache. @@ -117,12 +117,11 @@ do_test malloc5-1.6 { db2 close execsql { BEGIN; - SELECT * FROM abc; CREATE TABLE def(d, e, f); + SELECT * FROM abc; } value_in_range $::pgalloc $::mrange [sqlite3_release_memory 500] } [value_in_range $::pgalloc $::mrange] - do_test malloc5-1.7 { # Database should not be locked this time. sqlite3 db2 test.db @@ -175,8 +174,11 @@ do_test malloc5-2.2 { execsql { COMMIT; } - list $nRelease $data -} [list $pgalloc [list 1 2 3 4 5 6]] + value_in_range $::pgalloc $::mrange $nRelease +} [value_in_range $::pgalloc $::mrange] +do_test malloc5-2.2.1 { + set data +} {1 2 3 4 5 6} do_test malloc5-3.1 { # Simple test to show that if two pagers are opened from within this @@ -236,12 +238,12 @@ do_test malloc5-4.2 { db eval {PRAGMA cache_size=1} db cache flush sqlite3_release_memory - sqlite3_soft_heap_limit 100000 + sqlite3_soft_heap_limit 200000 sqlite3_memory_highwater 1 execsql {SELECT * FROM abc} set nMaxBytes [sqlite3_memory_highwater 1] puts -nonewline " (Highwater mark: $nMaxBytes) " - expr $nMaxBytes <= 110000 + expr $nMaxBytes <= 210000 } {1} do_test malloc5-4.3 { # Check that the content of table abc is at least roughly as expected. @@ -346,7 +348,7 @@ do_test malloc5-6.2.2 { # If we now try to reclaim some memory, it should come from the db2 cache. sqlite3_release_memory 3000 expr [nPage db] + [nPage db2] -} {4} +} {1} do_test malloc5-6.2.3 { # Access the db2 cache again, so that all the db2 pages have been used # more recently than all the db pages. Then try to reclaim 3000 bytes. @@ -354,7 +356,7 @@ do_test malloc5-6.2.3 { execsql { SELECT * FROM abc } db2 sqlite3_release_memory 3000 expr [nPage db] + [nPage db2] -} {4} +} {0} do_test malloc5-6.3.1 { # Now open a transaction and update 2 pages in the db2 cache. Then @@ -364,6 +366,8 @@ do_test malloc5-6.3.1 { # sync() to free up the dirty db2 pages. The only page that cannot be # freed is page1 of db2. Because there is an open transaction, the # btree layer holds a reference to page 1 in the db2 cache. + # + # UPDATE: No longer. As release_memory() does not cause a sync() execsql { BEGIN; UPDATE abc SET c = randstr(100,100) @@ -377,13 +381,13 @@ do_test malloc5-6.3.2 { # non-dirty pages held by db2. sqlite3_release_memory [expr 7*1132] list [nPage db] [nPage db2] -} {1 3} +} {0 3} do_test malloc5-6.3.3 { # Try to release another 1000 bytes. This should come fromt the db # cache, since all three pages held by db2 are either in-use or diry. sqlite3_release_memory 1000 list [nPage db] [nPage db2] -} {1 3} +} {0 3} do_test malloc5-6.3.4 { # Now release 9900 more (about 9 pages worth). This should expunge # the rest of the db cache. But the db2 cache remains intact, because @@ -394,20 +398,20 @@ do_test malloc5-6.3.4 { sqlite3_release_memory 9900 } list [nPage db] [nPage db2] -} {1 3} +} {0 3} do_test malloc5-6.3.5 { # But if we are really insistent, SQLite will consent to call sync() # if there is no other option. UPDATE: As of 3.6.2, SQLite will not # call sync() in this scenario. So no further memory can be reclaimed. sqlite3_release_memory 1000 list [nPage db] [nPage db2] -} {1 3} +} {0 3} do_test malloc5-6.3.6 { # The referenced page (page 1 of the db2 cache) will not be freed no # matter how much memory we ask for: sqlite3_release_memory 31459 list [nPage db] [nPage db2] -} {1 3} +} {0 3} db2 close diff --git a/test/mallocA.test b/test/mallocA.test index a78073d833..ff91d2af26 100644 --- a/test/mallocA.test +++ b/test/mallocA.test @@ -96,24 +96,6 @@ do_faultsim_test 6.2 -faults oom* -body { } -test { faultsim_test_result [list 0 {1 2}] } -ifcapable stat3 { - do_test 6.3-prep { - execsql { - PRAGMA writable_schema = 1; - CREATE TABLE sqlite_stat4 AS - SELECT tbl, idx, neq, nlt, ndlt, sqlite_record(sample) AS sample - FROM sqlite_stat3; - } - } {} - do_faultsim_test 6.3 -faults oom* -body { - execsql { - ANALYZE sqlite_master; - SELECT rowid FROM t1 WHERE a='abc' AND b<'y'; - } - } -test { - faultsim_test_result [list 0 {1 2}] - } -} do_execsql_test 7.0 { PRAGMA cache_size = 5; diff --git a/test/mallocI.test b/test/mallocI.test index 1229455505..ee4a603dee 100644 --- a/test/mallocI.test +++ b/test/mallocI.test @@ -61,4 +61,15 @@ do_malloc_test mallocI-4 -tclprep { } catch { db2 close } +do_faultsim_test mallocI-5 -faults oom* -prep { + catch { db close } + sqlite3 db test.db + sqlite3_db_config_lookaside db 0 0 0 +} -body { + db eval { Select CAST(1 AS blob) } +} -test { + faultsim_test_result {0 1} +} + + finish_test diff --git a/test/mallocK.test b/test/mallocK.test index 45ee7905c3..567a4c1f9d 100644 --- a/test/mallocK.test +++ b/test/mallocK.test @@ -121,10 +121,11 @@ do_execsql_test 6.0 { ifcapable stat4 { do_eqp_test 6.1 { SELECT DISTINCT c FROM t3 WHERE b BETWEEN '.xx..' AND '.xxxx'; - } { - 0 0 0 {SEARCH TABLE t3 USING INDEX i3 (ANY(a) AND b>? AND b<?)} - 0 0 0 {USE TEMP B-TREE FOR DISTINCT} - } + } [string map {"\n " \n} { + QUERY PLAN + |--SEARCH t3 USING INDEX i3 (ANY(a) AND b>? AND b<?) + `--USE TEMP B-TREE FOR DISTINCT + }] } do_faultsim_test 6 -faults oom* -body { diff --git a/test/mallocM.test b/test/mallocM.test new file mode 100644 index 0000000000..4da3a9e112 --- /dev/null +++ b/test/mallocM.test @@ -0,0 +1,48 @@ +# 2017 March 13 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# Further OOM tests. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/malloc_common.tcl +set testprefix mallocM + +sqlite3_db_config_lookaside db 0 0 0 + +do_execsql_test 1.0 { + CREATE TABLE t1(x); +} +do_faultsim_test 1 -faults oom* -body { + execsql { + SELECT 'abc' FROM ( SELECT 'xyz' FROM t1 WHERE (SELECT 1) ) + } +} -test { + faultsim_test_result {0 {}} +} + +do_execsql_test 2.0.1 { SELECT instr(x'', x'') } {1} +do_execsql_test 2.0.2 { SELECT instr(x'12345678', x'') } {1} +do_execsql_test 2.0.3 { SELECT instr(x'', x'1234') } {0} + +do_faultsim_test 2.1 -faults oom* -body { + execsql { SELECT instr (x'00', zeroblob(1)) } +} -test { + faultsim_test_result {0 1} +} + +do_faultsim_test 2.2 -faults oom* -body { + execsql { SELECT instr (zeroblob(1), x'00') } +} -test { + faultsim_test_result {0 1} +} + +finish_test diff --git a/test/malloc_common.tcl b/test/malloc_common.tcl index 2d0e57e4fc..7d3c942f77 100644 --- a/test/malloc_common.tcl +++ b/test/malloc_common.tcl @@ -664,6 +664,7 @@ proc do_write_test {name tbl sql} { if {$::DO_MALLOC_TEST } { set answers [list {1 {out of memory}} {0 {}}] + lappend answers [list 1 {unable to open a temporary database file for storing temporary tables}] if {$::DO_MALLOC_TEST==1} { set modes {100000 persistent} } else { diff --git a/test/malloctraceviewer.tcl b/test/malloctraceviewer.tcl new file mode 100644 index 0000000000..4517fdc361 --- /dev/null +++ b/test/malloctraceviewer.tcl @@ -0,0 +1,253 @@ + +package require sqlite3 +package require Tk + +############################################################################# +# Code to set up scrollbars for widgets. This is generic, boring stuff. +# +namespace eval autoscroll { + proc scrollable {widget path args} { + ::ttk::frame $path + set w [$widget ${path}.widget {*}$args] + set vs [::ttk::scrollbar ${path}.vs] + set hs [::ttk::scrollbar ${path}.hs -orient horizontal] + grid $w -row 0 -column 0 -sticky nsew + + grid rowconfigure $path 0 -weight 1 + grid columnconfigure $path 0 -weight 1 + + set grid [list grid $vs -row 0 -column 1 -sticky nsew] + $w configure -yscrollcommand [list ::autoscroll::scrollcommand $grid $vs] + $vs configure -command [list $w yview] + set grid [list grid $hs -row 1 -column 0 -sticky nsew] + $w configure -xscrollcommand [list ::autoscroll::scrollcommand $grid $hs] + $hs configure -command [list $w xview] + + return $w + } + proc scrollcommand {grid sb args} { + $sb set {*}$args + set isRequired [expr {[lindex $args 0] != 0.0 || [lindex $args 1] != 1.0}] + if {$isRequired && ![winfo ismapped $sb]} { + {*}$grid + } + if {!$isRequired && [winfo ismapped $sb]} { + grid forget $sb + } + } + namespace export scrollable +} +namespace import ::autoscroll::* +############################################################################# + +proc populate_text_widget {db} { + $::O(text) configure -state normal + set id [lindex [$::O(tree) selection] 0] + set frame [lindex $id end] + + set line [$db one {SELECT line FROM frame WHERE frame = $frame}] + if {$line ne ""} { + regexp {^([^:]*):([0-9]*)} $line -> file line + set content [$db one "SELECT content FROM file WHERE name = '$file'"] + $::O(text) delete 0.0 end + + set iLine 1 + foreach L [split $content "\n"] { + if {$iLine == $line} { + $::O(text) insert end "$L\n" highlight + } else { + $::O(text) insert end "$L\n" + } + incr iLine + } + $::O(text) yview -pickplace ${line}.0 + } + $::O(text) configure -state disabled +} + +proc populate_index {db} { + $::O(text) configure -state normal + + $::O(text) delete 0.0 end + $::O(text) insert end "\n\n" + + set L [format " % -40s%12s%12s\n" "Test Case" "Allocations" "Bytes"] + $::O(text) insert end $L + $::O(text) insert end " [string repeat - 64]\n" + + $db eval { + SELECT 'TOTAL' AS ztest, sum(ncall) AS calls, sum(nbyte) AS bytes + FROM malloc + UNION ALL + SELECT ztest AS ztest, sum(ncall) AS calls, sum(nbyte) AS bytes + FROM malloc + GROUP BY ztest + + ORDER BY 3 DESC + } { + set tags [list $ztest] + if {$ztest eq $::O(current)} { + lappend tags highlight + } + set L [format " % -40s%12s%12s\n" $ztest $calls $bytes] + $::O(text) insert end $L $tags + + $::O(text) tag bind $ztest <1> [list populate_tree_widget $db $ztest] + $::O(text) tag bind $ztest <Enter> [list $::O(text) configure -cursor hand2] + $::O(text) tag bind $ztest <Leave> [list $::O(text) configure -cursor ""] + } + + $::O(text) configure -state disabled +} + +proc sort_tree_compare {iLeft iRight} { + global O + switch -- [expr (int($O(tree_sort)/2))] { + 0 { + set left [$O(tree) item $iLeft -text] + set right [$O(tree) item $iRight -text] + set res [string compare $left $right] + } + 1 { + set left [lindex [$O(tree) item $iLeft -values] 0] + set right [lindex [$O(tree) item $iRight -values] 0] + set res [expr $left - $right] + } + 2 { + set left [lindex [$O(tree) item $iLeft -values] 1] + set right [lindex [$O(tree) item $iRight -values] 1] + set res [expr $left - $right] + } + } + if {$O(tree_sort)&0x01} { + set res [expr -1 * $res] + } + return $res +} + +proc sort_tree {iMode} { + global O + if {$O(tree_sort) == $iMode} { + incr O(tree_sort) + } else { + set O(tree_sort) $iMode + } + set T $O(tree) + set items [$T children {}] + set items [lsort -command sort_tree_compare $items] + for {set ii 0} {$ii < [llength $items]} {incr ii} { + $T move [lindex $items $ii] {} $ii + } +} + +proc trim_frames {stack} { + while {[info exists ::O(ignore.[lindex $stack 0])]} { + set stack [lrange $stack 1 end] + } + return $stack +} + +proc populate_tree_widget {db zTest} { + $::O(tree) delete [$::O(tree) children {}] + + for {set ii 0} {$ii < 15} {incr ii} { + $db eval { + SELECT + sum(ncall) AS calls, + sum(nbyte) AS bytes, + trim_frames(lrange(lstack, 0, $ii)) AS stack + FROM malloc + WHERE (zTest = $zTest OR $zTest = 'TOTAL') AND llength(lstack)>$ii + GROUP BY stack + HAVING stack != '' + } { + set parent_id [lrange $stack 0 end-1] + set frame [lindex $stack end] + set line [$db one {SELECT line FROM frame WHERE frame = $frame}] + set line [lindex [split $line /] end] + set v [list $calls $bytes] + + catch { + $::O(tree) insert $parent_id end -id $stack -text $line -values $v + } + } + } + + set ::O(current) $zTest + populate_index $db +} + + + +set O(tree_sort) 0 + +::ttk::panedwindow .pan -orient horizontal +set O(tree) [scrollable ::ttk::treeview .pan.tree] + +frame .pan.right +set O(text) [scrollable text .pan.right.text] +button .pan.right.index -command {populate_index mddb} -text "Show Index" +pack .pan.right.index -side top -fill x +pack .pan.right.text -fill both -expand true + +$O(text) tag configure highlight -background wheat +$O(text) configure -wrap none -height 35 + +.pan add .pan.tree +.pan add .pan.right + +$O(tree) configure -columns {calls bytes} +$O(tree) heading #0 -text Line -anchor w -command {sort_tree 0} +$O(tree) heading calls -text Calls -anchor w -command {sort_tree 2} +$O(tree) heading bytes -text Bytes -anchor w -command {sort_tree 4} +$O(tree) column #0 -width 150 +$O(tree) column calls -width 100 +$O(tree) column bytes -width 100 + +pack .pan -fill both -expand 1 + +#-------------------------------------------------------------------- +# Open the database containing the malloc data. The user specifies the +# database to use by passing the file-name on the command line. +# +proc open_database {} { + if {[info exists ::BUILTIN]} { + sqlite3 mddb :memory: + mddb eval $::BUILTIN + wm title . $::argv0 + } else { + set zFilename [lindex $::argv 0] + if {$zFilename eq ""} { + set zFilename mallocs.sql + } + set fd [open $zFilename] + set zHdr [read $fd 15] + if {$zHdr eq "SQLite format 3"} { + close $fd + sqlite3 mddb $zFilename + } else { + seek $fd 0 + sqlite3 mddb :memory: + mddb eval [read $fd] + close $fd + } + wm title . $zFilename + } + + mddb function lrange -argcount 3 lrange + mddb function llength -argcount 1 llength + mddb function trim_frames -argcount 1 trim_frames + + mddb eval { + SELECT frame FROM frame + WHERE line LIKE '%malloc.c:%' OR line LIKE '%mem2.c:%' + } { + set ::O(ignore.$frame) 1 + } +} + +open_database +bind $O(tree) <<TreeviewSelect>> [list populate_text_widget mddb] + +populate_tree_widget mddb [mddb one {SELECT zTest FROM malloc LIMIT 1}] + diff --git a/test/memdb1.test b/test/memdb1.test new file mode 100644 index 0000000000..c0510abae7 --- /dev/null +++ b/test/memdb1.test @@ -0,0 +1,283 @@ +# 2018-01-02 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is the "memdb" VFS +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix memdb1 +do_not_use_codec + +ifcapable !deserialize { + finish_test + return +} + +# Create a MEMDB and populate it with some dummy data. +# Then extract the database into the $::db1 variable. +# Verify that the size of $::db1 is the same as the size of +# the database. +# +unset -nocomplain db1 +unset -nocomplain sz1 +unset -nocomplain pgsz +do_test 100 { + db eval { + CREATE TABLE t1(a,b); + INSERT INTO t1 VALUES(1,2); + } + set ::pgsz [db one {PRAGMA page_size}] + set ::sz1 [expr {$::pgsz*[db one {PRAGMA page_count}]}] + set ::db1 [db serialize] + expr {[string length $::db1]==$::sz1} +} 1 +set fd [open db1.db wb] +puts -nonewline $fd $db1 +close $fd + +# Create a new MEMDB and initialize it to the content of $::db1 +# Verify that the content is the same. +# +db close +sqlite3 db +db deserialize $db1 +do_execsql_test 110 { + SELECT * FROM t1; +} {1 2} + +# What happens when we try to VACUUM a MEMDB database? +# +do_execsql_test 120 { + PRAGMA auto_vacuum = off; + VACUUM; +} {} +do_execsql_test 130 { + CREATE TABLE t2(x, y); + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<100) + INSERT INTO t2(x, y) SELECT x, randomblob(1000) FROM c; + DROP TABLE t2; + PRAGMA page_count; +} {116} +do_execsql_test 140 { + VACUUM; + PRAGMA page_count; +} {2} + +do_test 150 { + catch {db deserialize -unknown 1 $db1} msg + set msg +} {unknown option: -unknown} +do_test 151 { + db deserialize -readonly 1 $db1 + db eval {SELECT * FROM t1} +} {1 2} +do_test 152 { + catchsql {INSERT INTO t1 VALUES(3,4);} +} {1 {attempt to write a readonly database}} + +do_test 160 { + db deserialize -maxsize 32768 $db1 + db eval {SELECT * FROM t1} +} {1 2} +do_test 161 { + db eval {INSERT INTO t1 VALUES(3,4); SELECT * FROM t1} +} {1 2 3 4} +do_test 162 { + catchsql {INSERT INTO t1 VALUES(5,randomblob(100000))} +} {1 {database or disk is full}} + + +# Build a largish on-disk database and serialize it. Verify that the +# serialization works. +# +db close +forcedelete test.db +sqlite3 db test.db +do_execsql_test 200 { + CREATE TABLE t3(x, y); + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<400) + INSERT INTO t3(x, y) SELECT x, randomblob(1000) FROM c; + PRAGMA quick_check; +} {ok} +set fd [open test.db rb] +unset -nocomplain direct +set direct [read $fd] +close $fd +do_test 210 { + string length [db serialize] +} [string length $direct] +do_test 220 { + db eval {ATTACH ':memory:' AS aux1} + db deserialize aux1 $::direct + db eval { + SELECT x, y FROM main.t3 EXCEPT SELECT x, y FROM aux1.t3; + } +} {} +unset -nocomplain direct + +# Do the same with a :memory: database. +# +db close +sqlite3 db :memory: +do_execsql_test 300 { + CREATE TABLE t3(x, y); + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<400) + INSERT INTO t3(x, y) SELECT x, randomblob(1000) FROM c; + PRAGMA quick_check; +} {ok} +do_test 310 { + db eval {ATTACH ':memory:' AS aux1} + db deserialize aux1 [db serialize main] + db eval { + SELECT x, y FROM main.t3 EXCEPT SELECT x, y FROM aux1.t3; + } +} {} + +# Deserialize an empty database +# +db close +sqlite3 db +db deserialize {} +do_execsql_test 400 { + PRAGMA integrity_check; +} {ok} +do_execsql_test 410 { + CREATE TABLE t4(a,b); + INSERT INTO t4 VALUES('hello','world!'); + PRAGMA integrity_check; + SELECT * FROM t4; +} {ok hello world!} +do_execsql_test 420 { + PRAGMA journal_mode=TRUNCATE; + PRAGMA journal_mode=OFF; + PRAGMA journal_mode=DELETE; + PRAGMA journal_mode=WAL; + PRAGMA journal_mode=PERSIST; + PRAGMA journal_mode=MEMORY; + PRAGMA journal_mode=OFF; + PRAGMA journal_mode=DELETE; +} {truncate off delete delete persist memory off delete} + +# Deserialize something that is not a database. +# +db close +sqlite3 db +do_test 500 { + set rc [catch {db deserialize not-a-database} msg] + lappend rc $msg +} {0 {}} +do_catchsql_test 510 { + PRAGMA integrity_check; +} {1 {file is not a database}} + +# Abuse the serialize and deserialize commands. Make sure errors are caught. +# +do_test 600 { + set rc [catch {db deserialize} msg] + lappend rc $msg +} {1 {wrong # args: should be "db deserialize ?DATABASE? VALUE"}} +do_test 610 { + set rc [catch {db deserialize a b c} msg] + lappend rc $msg +} {1 {unknown option: a}} +do_test 620 { + set rc [catch {db serialize a b} msg] + lappend rc $msg +} {1 {wrong # args: should be "db serialize ?DATABASE?"}} + +# 2021-07-19 https://sqlite.org/forum/forumpost/e1cbb5f450b98aa6 +# The TEMP database cannot participate in serialization or +# deserialization. +# +reset_db +do_test 650 { + db eval { + CREATE TEMP TABLE t0(a); + CREATE TABLE t1(x); + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<1000) + INSERT INTO t1(x) SELECT random() FROM c; + } + set rc [catch {db deserialize temp [db serialize main]} err] + lappend rc err +} {1 err} + +#------------------------------------------------------------------------- +ifcapable vtab { + reset_db + do_execsql_test 700 { + CREATE TABLE t1(a, b); + PRAGMA schema_version = 0; + } + do_test 710 { + set ser [db serialize main] + db close + sqlite3 db + db deserialize main $ser + catchsql { + CREATE VIRTUAL TABLE t1 USING rtree(id, a, b, c, d); + } + } {1 {table t1 already exists}} +} + + +#------------------------------------------------------------------------- +# dbsqlfuzz 0a13dfb474d4f2f11a48a2ea57075c96fb456dd7 +# +if {[wal_is_capable]} { + reset_db + do_execsql_test 800 { + PRAGMA auto_vacuum = 0; + PRAGMA page_size = 8192; + PRAGMA journal_mode = wal; + CREATE TABLE t1(x, y); + INSERT INTO t1 VALUES(1, 2); + CREATE TABLE t2(x, y); + } {wal} + db close + + set fd [open test.db] + fconfigure $fd -translation binary + set data [read $fd [expr 20*1024]] + close $fd + + sqlite3 db "" + db deserialize $data + + do_execsql_test 810 { + PRAGMA locking_mode = exclusive; + SELECT * FROM t1 + } {exclusive 1 2} + + do_execsql_test 820 { + INSERT INTO t1 VALUES(3, 4); + SELECT * FROM t1; + } {1 2 3 4} + + do_catchsql_test 830 { + PRAGMA wal_checkpoint; + } {1 {database disk image is malformed}} +} + +# 2024-01-20 +# https://sqlite.org/forum/forumpost/498777780e16880a +# +# Make sure a database is initialized before serializing it. +# +reset_db +sqlite3 dbempty :memory: +do_test 900 { + set len [string length [dbempty serialize]] + expr {$len>0} +} 1 +dbempty close + +finish_test diff --git a/test/memdb2.test b/test/memdb2.test new file mode 100644 index 0000000000..7c2144991f --- /dev/null +++ b/test/memdb2.test @@ -0,0 +1,76 @@ +# 2022-12-05 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is the "memdb" VFS +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix memdb2 +do_not_use_codec + +ifcapable !deserialize { + finish_test + return +} + +db close + +#------------------------------------------------------------------------- +# Test that when using a memdb database, it is not possible to upgrade +# to an EXCLUSIVE lock if some other client is holding SHARED. +# +foreach {tn fname} { + 1 file:/test.db?vfs=memdb + 2 file:\\test.db?vfs=memdb +} { + if {$tn==2} breakpoint + sqlite3 db $fname -uri 1 + sqlite3 db2 $fname -uri 1 + + + do_execsql_test 1.$tn.1 { + CREATE TABLE t1(x, y); + INSERT INTO t1 VALUES(1, 2); + } + + do_execsql_test -db db2 1.$tn.2 { + BEGIN; + SELECT * FROM t1; + } {1 2} + + do_execsql_test 1.$tn.3 { + BEGIN; + INSERT INTO t1 VALUES(3, 4); + } + + do_catchsql_test 1.$tn.4 { + COMMIT + } {1 {database is locked}} + + do_execsql_test -db db2 1.$tn.5 { + SELECT * FROM t1; + END; + } {1 2} + + do_execsql_test 1.$tn.6 { + COMMIT + } {} + + do_execsql_test -db db2 1.$tn.7 { + SELECT * FROM t1 + } {1 2 3 4} + + db close + db2 close +} + +finish_test diff --git a/test/memjournal.test b/test/memjournal.test new file mode 100644 index 0000000000..73f984b553 --- /dev/null +++ b/test/memjournal.test @@ -0,0 +1,48 @@ +# 2021 May 24 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# Tests focused on the in-memory journal. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/malloc_common.tcl +set testprefix memjournal + + +do_execsql_test 1.0 { + PRAGMA journal_mode = memory; + CREATE TABLE t1(a); +} {memory} + +set nRow [expr 1] + +do_execsql_test 1.1 { + BEGIN; + INSERT INTO t1 VALUES( randomblob(500) ); +} {} + +do_test 1.2 { + for {set i 1} {$i <= 500} {incr i} { + execsql { + SAVEPOINT one; + UPDATE t1 SET a=randomblob(500); + } + execsql { SAVEPOINT abc } + execsql { UPDATE t1 SET a=randomblob(500) WHERE rowid<=$i AND 0 } + execsql { RELEASE abc } + } +} {} + +do_execsql_test 1.3 { + COMMIT; +} + +finish_test diff --git a/test/memjournal2.test b/test/memjournal2.test new file mode 100644 index 0000000000..d08bcb5a6a --- /dev/null +++ b/test/memjournal2.test @@ -0,0 +1,61 @@ +# 2022 Jan 01 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# Tests focused on the in-memory journal. +# +# TESTRUNNER: slow + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/malloc_common.tcl +set testprefix memjournal2 + +do_execsql_test 1.0 { + PRAGMA journal_mode = memory; + CREATE TABLE t1(a INTEGER PRIMARY KEY, b UNIQUE); +} {memory} + +set nRow [expr 2000] + +do_execsql_test 1.1 { + BEGIN; + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<$nRow + ) + INSERT INTO t1 SELECT NULL, randomblob(700) FROM s; +} + +for {set jj 200} {$jj <= 300} {incr jj} { + do_execsql_test 1.2.$jj.1 { + SAVEPOINT one; + UPDATE t1 SET b=randomblob(700) WHERE a<=$jj; + } + do_execsql_test 1.2.$jj.2 { + SAVEPOINT two; + UPDATE t1 SET b=randomblob(700) WHERE a==1; + ROLLBACK TO two; + RELEASE two; + } + do_execsql_test 1.2.$jj.3 { + SAVEPOINT two; + UPDATE t1 SET b=randomblob(700) WHERE a==1; + ROLLBACK TO two; + RELEASE two; + } + + do_execsql_test 1.2.$jj.4 { + PRAGMA integrity_check; + ROLLBACK TO one; + RELEASE one; + } {ok} +} + + +finish_test diff --git a/test/memleak.test b/test/memleak.test index a24a901f50..8443162ed6 100644 --- a/test/memleak.test +++ b/test/memleak.test @@ -38,8 +38,6 @@ set EXCLUDE { misuse.test memleak.test btree2.test - async.test - async2.test trans.test crash.test autovacuum_crash.test diff --git a/test/memsubsys1.test b/test/memsubsys1.test index 8265ce6317..fda35a918b 100644 --- a/test/memsubsys1.test +++ b/test/memsubsys1.test @@ -16,7 +16,7 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl sqlite3_reset_auto_extension -# This test assumes that no page-cache or scratch buffers are installed +# This test assumes that no page-cache buffers are installed # by default when a new database connection is opened. As a result, it # will not work with the "memsubsys1" permutation. # @@ -100,10 +100,12 @@ reset_highwater_marks build_test_db memsubsys1-2 {PRAGMA page_size=1024; PRAGMA mmap_size=0} #show_memstats set MEMORY_MANAGEMENT $sqlite_options(memorymanage) -ifcapable !malloc_usable_size { - do_test memsubsys1-2.3 { - set pg_ovfl [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0] 2] - } [expr ($TEMP_STORE>1 || $MEMORY_MANAGEMENT==0)*1024] +ifcapable pagecache_overflow_stats { + ifcapable !malloc_usable_size { + do_test memsubsys1-2.3 { + set pg_ovfl [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0] 2] + } [expr ($TEMP_STORE>1 || $MEMORY_MANAGEMENT==0)*1024] + } } do_test memsubsys1-2.4 { set pg_used [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0] 2] @@ -154,12 +156,11 @@ do_test memsubsys1-3.2.5 { set s_used [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0] 2] } 0 -# Test 4: Activate both PAGECACHE and SCRATCH. +# Test 4: Activate PAGECACHE # db close sqlite3_shutdown sqlite3_config_pagecache [expr 1024+$xtra_size] 50 -sqlite3_config_scratch 6000 2 sqlite3_initialize reset_highwater_marks build_test_db memsubsys1-4 {PRAGMA page_size=1024} @@ -173,146 +174,12 @@ do_test memsubsys1-4.4 { } 0 do_test memsubsys1-4.5 { set maxreq [lindex [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0] 2] - expr {$maxreq<7000} -} 1 -do_test memsubsys1-4.6 { - set s_used [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0] 2] -} 1 - -# Test 5: Activate both PAGECACHE and SCRATCH. But make the page size is -# such that the SCRATCH allocations are too small. -# -db close -sqlite3_shutdown -sqlite3_config_pagecache [expr 4096+$xtra_size] 24 -sqlite3_config_scratch 4000 2 -sqlite3_initialize -reset_highwater_marks -build_test_db memsubsys1-5 {PRAGMA page_size=4096} -#show_memstats -do_test memsubsys1-5.3 { - set pg_used [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0] 2] -} {/^2[34]$/} -do_test memsubsys1-5.4 { - set maxreq [lindex [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0] 2] - expr {$maxreq>4096} -} 1 -do_test memsubsys1-5.5 { - set s_used [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0] 2] -} 0 -do_test memsubsys1-5.6 { - set s_ovfl [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0] 2] - expr {$s_ovfl>6000} -} 1 - -# Test 6: Activate both PAGECACHE and SCRATCH with a 4k page size. -# Make it so that SCRATCH is large enough -# -db close -sqlite3_shutdown -sqlite3_config_pagecache [expr 4096+$xtra_size] 24 -sqlite3_config_scratch 25300 1 -sqlite3_initialize -reset_highwater_marks -build_test_db memsubsys1-6 {PRAGMA page_size=4096} -#show_memstats -do_test memsubsys1-6.3 { - set pg_used [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0] 2] -} {/^2[34]$/} -#do_test memsubsys1-6.4 { -# set maxreq [lindex [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0] 2] -# expr {$maxreq>4096 && $maxreq<=(4096+$xtra_size)} -#} 1 -do_test memsubsys1-6.5 { - set s_used [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0] 2] -} 1 -do_test memsubsys1-6.6 { - set s_ovfl [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0] 2] -} 0 - -# Test 7: Activate both PAGECACHE and SCRATCH with a 4k page size. -# Set cache_size small so that no PAGECACHE overflow occurs. Verify -# that maximum allocation size is small. -# -db close -sqlite3_shutdown -sqlite3_config_pagecache [expr 4096+$xtra_size] 24 -sqlite3_config_scratch 25300 1 -sqlite3_initialize -reset_highwater_marks -build_test_db memsubsys1-7 { - PRAGMA page_size=4096; - PRAGMA cache_size=10; - PRAGMA temp_store=memory; -} -#show_memstats -do_test memsubsys1-7.3 { - set pg_used [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0] 2] - expr {$pg_used<24} -} 1 -do_test memsubsys1-7.4 { - set pg_ovfl [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0] 2] -} 0 -do_test memsubsys1-7.5 { - set maxreq [lindex [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0] 2] - expr {$maxreq<4100} -} 1 -do_test memsubsys1-7.6 { - set s_used [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0] 2] + expr {$maxreq<9000} } 1 -do_test memsubsys1-7.7 { - set s_ovfl [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0] 2] -} 0 - -# Test 8: Disable PAGECACHE. Make available SCRATCH zero. Verify that -# the SCRATCH overflow logic works. -# -db close -sqlite3_shutdown -sqlite3_config_pagecache 0 0 -sqlite3_config_scratch 25000 0 -sqlite3_initialize -reset_highwater_marks -do_test memsubsys1-8.1 { - set pg_used [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0] 2] -} 0 -do_test memsubsys1-8.2 { - set s_ovfl [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0] 2] -} 0 -do_test memsubsys1-8.3 { - sqlite3 db :memory: - db eval { - CREATE TABLE t1(x); - INSERT INTO t1 VALUES(zeroblob(400)); - INSERT INTO t1 VALUES(zeroblob(400)); - INSERT INTO t1 SELECT * FROM t1; - INSERT INTO t1 SELECT * FROM t1; - INSERT INTO t1 SELECT * FROM t1; - } - expr {[lindex [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0] 2]>0} -} 1 -db close -sqlite3_shutdown -sqlite3_config_memstatus 0 -sqlite3_initialize -do_test memsubsys1-8.4 { - sqlite3 db :memory: - db eval { - CREATE TABLE t1(x); - INSERT INTO t1 VALUES(zeroblob(400)); - INSERT INTO t1 VALUES(zeroblob(400)); - INSERT INTO t1 SELECT * FROM t1; - INSERT INTO t1 SELECT * FROM t1; - INSERT INTO t1 SELECT * FROM t1; - SELECT rowid FROM t1; - } -} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16} - db close sqlite3_shutdown sqlite3_config_memstatus 1 -sqlite3_config_scratch 0 0 sqlite3_config_lookaside 100 500 sqlite3_config serialized sqlite3_initialize diff --git a/test/memsubsys2.test b/test/memsubsys2.test index 68e4e22d32..9085dd2952 100644 --- a/test/memsubsys2.test +++ b/test/memsubsys2.test @@ -74,18 +74,23 @@ do_test memsubsys2-1.2 { # Test 2: Verify that the highwater mark increases after a large # allocation. # -sqlite3_memory_highwater 1 -set highwater [sqlite3_memory_highwater 0] -do_test memsubsys2-2.1 { - sqlite3_free [set x [sqlite3_malloc 100000]] - expr {$x!="0"} -} {1} -do_test memsubsys2-2.2.1 { - expr {[sqlite3_memory_highwater 0]>=[sqlite3_memory_used]+100000} -} {1} -do_test memsubsys2-2.2.2 { - expr {[sqlite3_memory_highwater 0]>=$highwater+50000} -} {1} +# Do not run this if [sqlite3_memory_used] returns 0. This indicates +# an SQLITE_DEFAULT_MEMSTATUS=0 build. +# +if {[sqlite3_memory_used]!=0} { + sqlite3_memory_highwater 1 + set highwater [sqlite3_memory_highwater 0] + do_test memsubsys2-2.1 { + sqlite3_free [set x [sqlite3_malloc 100000]] + expr {$x!="0"} + } {1} + do_test memsubsys2-2.2.1 { + expr {[sqlite3_memory_highwater 0]>=[sqlite3_memory_used]+100000} + } {1} + do_test memsubsys2-2.2.2 { + expr {[sqlite3_memory_highwater 0]>=$highwater+50000} + } {1} +} # Test 3: Verify that turning of memstatus disables the statistics # tracking. diff --git a/test/merge1.test b/test/merge1.test new file mode 100644 index 0000000000..686271648a --- /dev/null +++ b/test/merge1.test @@ -0,0 +1,145 @@ +# 2021-12-29 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# Testing the compound-SELECT merge algorithm to ensure that it works +# when it tries to balance the merge tree. + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix merge1 + +ifcapable !vtab { + finish_test + return +} + +load_static_extension db series + + +optimization_control db all on +do_execsql_test 100 { + WITH data(v) AS ( + SELECT value FROM generate_series(1,35,3) + UNION ALL + SELECT value FROM generate_series(10,30,4) + UNION ALL + SELECT value FROM generate_series(20,50,5) + UNION ALL + SELECT value FROM generate_series(30,60,6) + UNION ALL + SELECT value FROM generate_series(1,50,7) + UNION ALL + SELECT value FROM generate_series(10,80,8) + ) + SELECT v FROM data ORDER BY v; +} {1 1 4 7 8 10 10 10 13 14 15 16 18 18 19 20 22 22 22 25 25 26 26 28 29 30 30 30 31 34 34 35 36 36 40 42 42 43 45 48 50 50 50 54 58 60 66 74} +do_eqp_test 101 { + WITH data(v) AS ( + SELECT value FROM generate_series(1,35,3) + UNION ALL + SELECT value FROM generate_series(10,30,4) + UNION ALL + SELECT value FROM generate_series(20,50,5) + UNION ALL + SELECT value FROM generate_series(30,60,6) + UNION ALL + SELECT value FROM generate_series(1,50,7) + UNION ALL + SELECT value FROM generate_series(10,80,8) + ) + SELECT v FROM data ORDER BY v; +} { + QUERY PLAN + `--MERGE (UNION ALL) + |--LEFT + | `--MERGE (UNION ALL) + | |--LEFT + | | `--MERGE (UNION ALL) + | | |--LEFT + | | | `--SCAN generate_series VIRTUAL TABLE INDEX 0x17: + | | `--RIGHT + | | `--SCAN generate_series VIRTUAL TABLE INDEX 0x17: + | `--RIGHT + | `--SCAN generate_series VIRTUAL TABLE INDEX 0x17: + `--RIGHT + `--MERGE (UNION ALL) + |--LEFT + | `--MERGE (UNION ALL) + | |--LEFT + | | `--SCAN generate_series VIRTUAL TABLE INDEX 0x17: + | `--RIGHT + | `--SCAN generate_series VIRTUAL TABLE INDEX 0x17: + `--RIGHT + `--SCAN generate_series VIRTUAL TABLE INDEX 0x17: +} + +# Same test with the blanced-merge optimization +# disabled. Should give the exact same answer. +# +optimization_control db balanced-merge off +db cache flush +do_execsql_test 110 { + WITH data(v) AS ( + SELECT value FROM generate_series(1,35,3) + UNION ALL + SELECT value FROM generate_series(10,30,4) + UNION ALL + SELECT value FROM generate_series(20,50,5) + UNION ALL + SELECT value FROM generate_series(30,60,6) + UNION ALL + SELECT value FROM generate_series(1,50,7) + UNION ALL + SELECT value FROM generate_series(10,80,8) + ) + SELECT v FROM data ORDER BY v; +} {1 1 4 7 8 10 10 10 13 14 15 16 18 18 19 20 22 22 22 25 25 26 26 28 29 30 30 30 31 34 34 35 36 36 40 42 42 43 45 48 50 50 50 54 58 60 66 74} +do_eqp_test 111 { + WITH data(v) AS ( + SELECT value FROM generate_series(1,35,3) + UNION ALL + SELECT value FROM generate_series(10,30,4) + UNION ALL + SELECT value FROM generate_series(20,50,5) + UNION ALL + SELECT value FROM generate_series(30,60,6) + UNION ALL + SELECT value FROM generate_series(1,50,7) + UNION ALL + SELECT value FROM generate_series(10,80,8) + ) + SELECT v FROM data ORDER BY v; +} { + QUERY PLAN + `--MERGE (UNION ALL) + |--LEFT + | `--MERGE (UNION ALL) + | |--LEFT + | | `--MERGE (UNION ALL) + | | |--LEFT + | | | `--MERGE (UNION ALL) + | | | |--LEFT + | | | | `--MERGE (UNION ALL) + | | | | |--LEFT + | | | | | `--SCAN generate_series VIRTUAL TABLE INDEX 0x17: + | | | | `--RIGHT + | | | | `--SCAN generate_series VIRTUAL TABLE INDEX 0x17: + | | | `--RIGHT + | | | `--SCAN generate_series VIRTUAL TABLE INDEX 0x17: + | | `--RIGHT + | | `--SCAN generate_series VIRTUAL TABLE INDEX 0x17: + | `--RIGHT + | `--SCAN generate_series VIRTUAL TABLE INDEX 0x17: + `--RIGHT + `--SCAN generate_series VIRTUAL TABLE INDEX 0x17: +} + +finish_test diff --git a/test/minmax.test b/test/minmax.test index fb9bbb383a..232ac14a05 100644 --- a/test/minmax.test +++ b/test/minmax.test @@ -294,17 +294,22 @@ do_test minmax-8.2 { # is a subquery. # ifcapable {compound && subquery} { + do_test minmax-9.0 { + execsql { + SELECT max(rowid) AS yy FROM t4 UNION SELECT max(rowid) FROM t5 + } + } {3} do_test minmax-9.1 { execsql { - SELECT max(rowid) FROM ( - SELECT max(rowid) FROM t4 UNION SELECT max(rowid) FROM t5 + SELECT max(yy) FROM ( + SELECT max(rowid) AS yy FROM t4 UNION SELECT max(rowid) FROM t5 ) } - } {{}} + } {3} do_test minmax-9.2 { execsql { - SELECT max(rowid) FROM ( - SELECT max(rowid) FROM t4 EXCEPT SELECT max(rowid) FROM t5 + SELECT max(yy) FROM ( + SELECT max(rowid) AS yy FROM t4 EXCEPT SELECT max(rowid) FROM t5 ) } } {{}} @@ -628,5 +633,29 @@ do_test_13_noopt 13.7 { SELECT min(c), count(c) FROM t1 WHERE a='a'; } {1 5} +# 2016-07-26. https://sqlite.org/src/info/a0bac8b3c3d1bb75 +# Incorrect result on a min() query after a CREATE INDEX. +# +do_execsql_test 14.1 { + CREATE TABLE t14(a INTEGER, b INTEGER); + INSERT INTO t14(a,b) VALUES(100,2),(200,2),(300,2),(400,1),(500,2); + SELECT min(a) FROM t14 WHERE b='2' AND a>'50'; +} {100} +do_execsql_test 14.2 { + CREATE INDEX t14ba ON t14(b,a); + SELECT min(a) FROM t14 WHERE b='2' AND a>'50'; +} {100} + +# 2021-08-21. https://sqlite.org/forum/forumpost/cfcb4b461d +# +reset_db +do_execsql_test 15.1 { + CREATE TABLE t1(a); + CREATE TABLE t2(b); + CREATE TABLE t3(c); + INSERT INTO t1 VALUES(0); + INSERT INTO t2 VALUES(5); + SELECT MIN((SELECT b FROM t2 UNION SELECT x FROM (SELECT x FROM (SELECT 1 AS x WHERE t1.a=1) UNION ALL SELECT c FROM t3))) FROM t1; +} {5} finish_test diff --git a/test/minmax2.test b/test/minmax2.test index da8fec30ce..9ea6b679c6 100644 --- a/test/minmax2.test +++ b/test/minmax2.test @@ -21,8 +21,8 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl do_test minmax2-1.0 { + sqlite3_db_config db LEGACY_FILE_FORMAT 0 execsql { - PRAGMA legacy_file_format=0; BEGIN; CREATE TABLE t1(x, y); INSERT INTO t1 VALUES(1,1); @@ -283,17 +283,22 @@ do_test minmax2-8.2 { # is a subquery. # ifcapable {compound && subquery} { + do_test minmax2-9.0 { + execsql { + SELECT max(rowid) FROM t4 UNION SELECT max(rowid) FROM t5 + } + } {3} do_test minmax2-9.1 { execsql { - SELECT max(rowid) FROM ( - SELECT max(rowid) FROM t4 UNION SELECT max(rowid) FROM t5 + SELECT max(yy) FROM ( + SELECT max(rowid) AS yy FROM t4 UNION SELECT max(rowid) FROM t5 ) } - } {{}} + } {3} do_test minmax2-9.2 { execsql { - SELECT max(rowid) FROM ( - SELECT max(rowid) FROM t4 EXCEPT SELECT max(rowid) FROM t5 + SELECT max(yy) FROM ( + SELECT max(rowid) AS yy FROM t4 EXCEPT SELECT max(rowid) FROM t5 ) } } {{}} @@ -383,5 +388,39 @@ do_test minmax2-10.12 { } } {{} {}} +# 2017-10-26. Extend the min/max optimization to indexes on expressions +# +do_execsql_test minmax2-11.100 { + CREATE TABLE t11(a,b,c); + INSERT INTO t11(a,b,c) VALUES(1,10,5),(2,8,11),(3,1,4),(4,20,1),(5,16,4); + CREATE INDEX t11bc ON t11(b+c); + SELECT max(b+c) FROM t11; +} {21} +do_execsql_test minmax2-11.110 { + SELECT a, max(b+c) FROM t11; +} {4 21} +do_test minmax2-11.111 { + db eval {SELECT max(b+c) FROM t11} + db status step +} {0} +do_test minmax2-11.112 { + db eval {SELECT max(c+b) FROM t11} + db status step +} {4} +do_execsql_test minmax2-11.120 { + SELECT a, min(b+c) FROM t11; +} {3 5} +do_test minmax2-11.121 { + db eval {SELECT min(b+c) FROM t11} + db status step +} {0} +do_test minmax2-11.122 { + db eval {SELECT min(c+b) FROM t11} + db status step +} {4} +do_execsql_test minmax2-11.130 { + INSERT INTO t11(a,b,c) VALUES(6,NULL,0),(7,0,NULL); + SELECT a, min(b+c) FROM t11; +} {3 5} finish_test diff --git a/test/minmax4.test b/test/minmax4.test index 8063538bfd..775fee86be 100644 --- a/test/minmax4.test +++ b/test/minmax4.test @@ -19,6 +19,7 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl +set testprefix minmax4 ifcapable !compound { finish_test @@ -148,6 +149,88 @@ do_test minmax4-2.7 { } } {1 1 {} 2 2 2 3 3 5 5} +#------------------------------------------------------------------------- +foreach {tn sql} { + 1 { CREATE INDEX i1 ON t1(a) } + 2 { CREATE INDEX i1 ON t1(a DESC) } + 3 { } +} { + reset_db + do_execsql_test 3.$tn.0 { + CREATE TABLE t1(a, b); + INSERT INTO t1 VALUES(NULL, 1); + } + execsql $sql + do_execsql_test 3.$tn.1 { + SELECT min(a), b FROM t1; + } {{} 1} + do_execsql_test 3.$tn.2 { + SELECT min(a), b FROM t1 WHERE a<50; + } {{} {}} + do_execsql_test 3.$tn.3 { + INSERT INTO t1 VALUES(2, 2); + } + do_execsql_test 3.$tn.4 { + SELECT min(a), b FROM t1; + } {2 2} + do_execsql_test 3.$tn.5 { + SELECT min(a), b FROM t1 WHERE a<50; + } {2 2} +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 4.0 { + CREATE TABLE t0 (c0, c1); + CREATE INDEX i0 ON t0(c1, c1 + 1 DESC); + INSERT INTO t0(c0) VALUES (1); +} +do_execsql_test 4.1 { + SELECT MIN(t0.c1), t0.c0 FROM t0 WHERE t0.c1 ISNULL; +} {{} 1} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 5.0 { + CREATE TABLE t1 (a, b); + INSERT INTO t1 VALUES(123, NULL); + CREATE INDEX i1 ON t1(a, b DESC); +} +do_execsql_test 5.1 { + SELECT MIN(a) FROM t1 WHERE a=123; +} {123} +#------------------------------------------------------------------------- +# Tests for ticket f8a7060ece. +# +reset_db +do_execsql_test 6.1.0 { + CREATE TABLE t1(a, b, c); + INSERT INTO t1 VALUES(NULL, 1, 'x'); + CREATE INDEX i1 ON t1(a); +} +do_execsql_test 6.1.1 { + SELECT min(a), b, c FROM t1 WHERE c='x'; +} {{} 1 x} +do_execsql_test 6.1.2 { + INSERT INTO t1 VALUES(1, 2, 'y'); +} {} +do_execsql_test 6.1.3 { + SELECT min(a), b, c FROM t1 WHERE c='x'; +} {{} 1 x} + +do_execsql_test 6.2.0 { + CREATE TABLE t0(c0 UNIQUE, c1); + INSERT INTO t0(c1) VALUES (0); + INSERT INTO t0(c0) VALUES (0); + CREATE VIEW v0(c0, c1) AS + SELECT t0.c1, t0.c0 FROM t0 WHERE CAST(t0.rowid AS INT) = 1; +} +do_execsql_test 6.2.1 { + SELECT c0, c1 FROM v0; +} {0 {}} +do_execsql_test 6.2.2 { + SELECT v0.c0, MIN(v0.c1) FROM v0; +} {0 {}} finish_test diff --git a/test/misc1.test b/test/misc1.test index 400a4517b4..8110d38678 100644 --- a/test/misc1.test +++ b/test/misc1.test @@ -479,26 +479,28 @@ ifcapable curdir { # Make sure a database connection still works after changing the # working directory. # -do_test misc1-14.1 { - file mkdir tempdir - cd tempdir - execsql {BEGIN} - file exists ./test.db-journal -} {0} -do_test misc1-14.2a { - execsql {UPDATE t1 SET a=a||'x' WHERE 0} - file exists ../test.db-journal -} {0} -do_test misc1-14.2b { - execsql {UPDATE t1 SET a=a||'y' WHERE 1} - file exists ../test.db-journal -} {1} -do_test misc1-14.3 { - cd .. - forcedelete tempdir - execsql {COMMIT} - file exists ./test.db-journal -} {0} +if {[atomic_batch_write test.db]==0} { + do_test misc1-14.1 { + file mkdir tempdir + cd tempdir + execsql {BEGIN} + file exists ./test.db-journal + } {0} + do_test misc1-14.2a { + execsql {UPDATE t1 SET a=a||'x' WHERE 0} + file exists ../test.db-journal + } {0} + do_test misc1-14.2b { + execsql {UPDATE t1 SET a=a||'y' WHERE 1} + file exists ../test.db-journal + } {1} + do_test misc1-14.3 { + cd .. + forcedelete tempdir + execsql {COMMIT} + file exists ./test.db-journal + } {0} +} } # A failed create table should not leave the table in the internal @@ -590,6 +592,9 @@ do_test misc1-18.1 { set n [sqlite3_sleep 100] expr {$n>=100} } {1} +do_test misc1-18.2 { + sqlite3_sleep -100 +} {0} # 2014-01-10: In a CREATE TABLE AS, if one or more of the column names # are an empty string, that is still OK. @@ -603,14 +608,17 @@ do_execsql_test misc1-19.2 { SELECT * FROM t19b; } {4 5 6} -# 2015-05-20: CREATE TABLE AS should not store INT value is a TEXT +# 2015-05-20: CREATE TABLE AS should not store INT value in a TEXT # column. # +# 2022-12-14: Change: The column is not TEXT if the AS SELECT is +# a compound with different types on each arm. +# do_execsql_test misc1-19.3 { CREATE TABLE t19c(x TEXT); CREATE TABLE t19d AS SELECT * FROM t19c UNION ALL SELECT 1234; SELECT x, typeof(x) FROM t19d; -} {1234 text} +} {1234 integer} # 2014-05-16: Tests for the SQLITE_TESTCTRL_FAULT_INSTALL feature. # @@ -620,11 +628,11 @@ proc fault_callback {n} { lappend ::fault_callbacks $n return 0 } -do_test misc1-19.1 { +do_test misc1-19.11 { sqlite3_test_control_fault_install fault_callback set fault_callbacks } {0} -do_test misc1-19.2 { +do_test misc1-19.12 { sqlite3_test_control_fault_install set fault_callbacks } {0} @@ -646,17 +654,18 @@ do_catchsql_test misc1-21.1 { } {1 {near "#0": syntax error}} do_catchsql_test misc1-21.2 { VALUES(0,0x0MATCH#0; -} {1 {near ";": syntax error}} +} {1 {unrecognized token: "0x0MATCH"}} # 2015-04-15 do_execsql_test misc1-22.1 { - SELECT ""+3 FROM (SELECT ""+5); + SELECT ''+3 FROM (SELECT ''+5); } {3} # 2015-04-19: NULL pointer dereference on a corrupt schema # db close sqlite3 db :memory: +sqlite3_db_config db DEFENSIVE 0 do_execsql_test misc1-23.1 { CREATE TABLE t1(x); PRAGMA writable_schema=ON; @@ -672,6 +681,7 @@ do_execsql_test misc1-23.1 { db close database_may_be_corrupt sqlite3 db :memory: +sqlite3_db_config db DEFENSIVE 0 do_catchsql_test misc1-23.2 { CREATE TABLE t1(x UNIQUE); PRAGMA writable_schema=ON; @@ -683,6 +693,7 @@ do_catchsql_test misc1-23.2 { } {1 {no such table: F}} db close sqlite3 db :memory: +sqlite3_db_config db DEFENSIVE 0 do_catchsql_test misc1-23.3 { CREATE TABLE t1(x UNIQUE); PRAGMA writable_schema=ON; @@ -699,7 +710,7 @@ do_catchsql_test misc1-23.3 { # do_test misc1-24.0 { list [catch { sqlite3_prepare_v2 db ! -1 dummy } msg] $msg -} {1 {(1) unrecognized token: "!}} +} {1 {(1) unrecognized token: "!"}} # The following query (provided by Kostya Serebryany) used to take 25 # minutes to prepare. This has been speeded up to about 250 milliseconds. @@ -709,5 +720,56 @@ SELECT-1 UNION SELECT 5 UNION SELECT 0 UNION SElECT*from(SELECT-5) UNION SELECT $group,:conc ap0,1)fro,(select"",:PBAG,c,a,b,b,c,a,b,c,e,d,d,c,a,b,b,c,d,d,c,a,b,c,e,d,c,d,c,a,b,c,e,c,d,c,d,c,a,b,b,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,d,c,a,b,c,e,d,d,c,a,b,b,c,c,a,b,b,c,d,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,a,b,c,e,d,d,c,a,b,b,b,c,e,d,d,c,a,b,b,c,c,a,b,b,c,d,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,d,c,e,d,d,c,a,b,b,c,c,a,b,b,E,d,c,d,c,b,c,d,c,d,c,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,a,b,c,e,d,d,c,a,b,b,c,d,d,c,a,b,c,e,d,c,d,c,a,b,c,e,c,d,c,d,c,a,b,b,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,d,c,a,b,c,e,d,d,c,a,b,b,c,c,a,b,b,c,d,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,d,c,e,d,d,c,a,b,b,c,c,a,b,b,E,d,c,d,c,b,c,d,c,d,c,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,a,b,c,e,d,d,c,a,b,b,c,d,d,c,a,b,c,e,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,c,d,c,c,a,a,b,d,d,c,a,b,b,c,d,c,a,b,e,e,d,b,c,d,c,a,b,b,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,d,c,a,b,c,e,d,d,c,a,b,b,c,c,a,b,b,c,d,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d, foreign_keysc,d,c,e,d,d,c,a,b,b,c,c,a,b,b,E,d,c,d,c,b,c,d,c,d,c,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,a,b,d,d,c,a,b,b,c,d,c,a,b,e,e,d,b,c,d,c,a,b,b,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,d,c,a,b,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,d,c,e,d,d,c,a,b,b,c,c,a,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,d,c,a,b,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,d,c,e,d,d,c,a,b,b,c,c,a,b,b,E,d,c,d,c,b,c,d,c,d,c,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,a,b,c,e,d,d,c,a,b,b,c,d,d,c,a,b,c,e,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,c,d,c,a,b,d,d,c,a,a,b,d,d,c,a,b,b,c,d,c,a,b,e,e,d,b,c,d,c,a,b,b,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,d,c,a,b,c,e,d,d,c,a,b,b,c,c,a,b,b,c,d,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,d,c,e,d,d,c,a,b,b,c,c,a,b,b,E,d,c,d,c,b,c,d,c,d,c,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,a,b,d,d,c,a,b,b,c,d,c,a,b,e,e,d,b,c,d,c,a,b,b,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,d,c,a,b,c,e,d,d,c,a,b,b,c,c,a,b,b,c,d,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,d,c,e,d,d,c,a,b,b,c,c,a,b,b,E,d,c,d,c,b,c,d,c,d,c,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,a,bb,b,E,d,c,d,c,b,c,d,c,d,c,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,a,b,c,e,d,d,c,a,b,b,c,d,d,c,a,b,c,e,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,c,d,c,a,b,d,d,c,a,a,b,d,d,c,a,b,b,c,d,c,a,b,e,e,d,b,c,d,c,a,b,b,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,MAato_aecSELEC,+?b," "O,"i","a",""b ,5 ))KEY)SELECT*FROM((k()reaC,k,K) eA,k '' )t ,K M); } {1 {'k' is not a function}} +# 2017-09-17 +# +# Sometimes sqlite3ExprListAppend() can be invoked on an ExprList that +# was obtained from sqlite3ExprListDup(). +# +do_execsql_test misc1-26.0 { + DROP TABLE IF EXISTS abc; + CREATE TABLE abc(a, b, c); + SELECT randomblob(min(max(coalesce(EXISTS (SELECT 1 FROM ( SELECT (SELECT 2147483647) NOT IN (SELECT 2147483649 UNION ALL SELECT DISTINCT -1) IN (SELECT 2147483649), 'fault', (SELECT ALL -1 INTERSECT SELECT 'experiments') IN (SELECT ALL 56.1 ORDER BY 'experiments' DESC) FROM (SELECT DISTINCT 2147483648, 'hardware' UNION ALL SELECT -2147483648, 'experiments' ORDER BY 2147483648 LIMIT 1 OFFSET 123456789.1234567899) GROUP BY (SELECT ALL 0 INTERSECT SELECT 'in') IN (SELECT DISTINCT 'experiments' ORDER BY zeroblob(1000) LIMIT 56.1 OFFSET -456) HAVING EXISTS (SELECT 'fault' EXCEPT SELECT DISTINCT 56.1) UNION SELECT 'The', 'The', 2147483649 UNION ALL SELECT DISTINCT 'hardware', 'first', 'experiments' ORDER BY 'hardware' LIMIT 123456789.1234567899 OFFSET -2147483647)) NOT IN (SELECT (SELECT DISTINCT (SELECT 'The') FROM abc ORDER BY EXISTS (SELECT -1 INTERSECT SELECT ALL NULL) ASC) IN (SELECT DISTINCT EXISTS (SELECT ALL 123456789.1234567899 ORDER BY 1 ASC, NULL DESC) FROM sqlite_master INTERSECT SELECT 456)), (SELECT ALL 'injection' UNION ALL SELECT ALL (SELECT DISTINCT 'first' UNION SELECT DISTINCT 'The') FROM (SELECT 456, 'in', 2147483649))),1), 500)), 'first', EXISTS (SELECT DISTINCT 456 FROM abc ORDER BY 'experiments' DESC) FROM abc; +} {} + +# 2017-12-29 +# +# The following behaviors (duplicate column names on an INSERT or UPDATE) +# are undocumented. <<--- Not so. There is a long-standing requirement +# in lang_update.in to say that when the columns to be updated appear more +# than once in an UPDATE statement that only the rightmost expression is used. +# See e_update-1.6.* for the tests. This is unfortunate, since omitting +# that requirement would greatly simplify the fix to the problem identified +# by forum post https://sqlite.org/forum/info/16ca0e9f32c38567 +# +# These tests are added to ensure that historical behavior +# does not change accidentally. +# +# For duplication columns on an INSERT, the first value is used. +# For duplication columns on an UPDATE, the last value is used. +# +do_execsql_test misc1-27.0 { + CREATE TABLE dup1(a,b,c); + INSERT INTO dup1(a,b,c,a,b,c) VALUES(1,2,3,4,5,6); + SELECT a,b,c FROM dup1; +} {1 2 3} +do_execsql_test misc1-27.1 { + UPDATE dup1 SET a=7, b=8, c=9, a=10, b=11, c=12; + SELECT a,b,c FROM dup1; +} {10 11 12} + +# 2018-12-20 +# +# The Cursor.seekOp debugging value set incorrectly +# in OP_NotExists. +# +sqlite3 db :memory: +do_execsql_test misc1-28.0 { + CREATE TABLE t1(x); + CREATE UNIQUE INDEX t1x ON t1(x) WHERE x=1; + INSERT OR ABORT INTO t1 DEFAULT VALUES; + UPDATE OR REPLACE t1 SET x = 1; + PRAGMA integrity_check; + SELECT * FROM t1; +} {ok 1} finish_test diff --git a/test/misc2.test b/test/misc2.test index bb544c5fbb..607799ea21 100644 --- a/test/misc2.test +++ b/test/misc2.test @@ -54,19 +54,37 @@ do_test misc2-2.1 { } } {} ifcapable subquery { - do_test misc2-2.2 { - execsql { + ifcapable allow_rowid_in_view { + do_catchsql_test misc2-2.2 { SELECT rowid, * FROM (SELECT * FROM t1, t2); - } - } {{} 1 2 3 7 8 9} + } {0 {{} 1 2 3 7 8 9}} + } else { + do_catchsql_test misc2-2.2 { + SELECT rowid, * FROM (SELECT * FROM t1, t2); + } {1 {no such column: rowid}} + } + do_catchsql_test misc2-2.2b { + SELECT 'rowid', * FROM (SELECT * FROM t1, t2); + } {0 {rowid 1 2 3 7 8 9}} } + ifcapable view { - do_test misc2-2.3 { - execsql { + ifcapable allow_rowid_in_view { + do_catchsql_test misc2-2.3 { CREATE VIEW v1 AS SELECT * FROM t1, t2; SELECT rowid, * FROM v1; - } - } {{} 1 2 3 7 8 9} + } {0 {{} 1 2 3 7 8 9}} + } else { + do_catchsql_test misc2-2.3 { + CREATE VIEW v1 AS SELECT * FROM t1, t2; + SELECT rowid, * FROM v1; + } {1 {no such column: rowid}} + } + + + do_catchsql_test misc2-2.3b { + SELECT 'rowid', * FROM v1; + } {0 {rowid 1 2 3 7 8 9}} } ;# ifcapable view # Ticket #2002 and #1952. diff --git a/test/misc3.test b/test/misc3.test index bc1f0ff911..f64b0fe1dd 100644 --- a/test/misc3.test +++ b/test/misc3.test @@ -88,8 +88,8 @@ do_test misc3-2.4 { execsql {SELECT 2e-25*0.5e250} } 1e+225 do_test misc3-2.5 { - execsql {SELECT 2.0e-250*0.5e25} -} 1e-225 + execsql {SELECT format('%.15e',2.0e-250*0.5e25)} +} {1.0000000000000e-225} do_test misc3-2.6 { execsql {SELECT '-2.0e-127' * '-0.5e27'} } 1e-100 diff --git a/test/misc4.test b/test/misc4.test index 79e756ec63..3d391882c3 100644 --- a/test/misc4.test +++ b/test/misc4.test @@ -212,6 +212,7 @@ do_test misc4-6.2 { # db close sqlite3 db :memory: +sqlite3_db_config db DEFENSIVE 0 do_catchsql_test misc4-7.1 { CREATE TABLE t7(x); PRAGMA writable_schema=ON; diff --git a/test/misc5.test b/test/misc5.test index 30176b8082..43ee2781a1 100644 --- a/test/misc5.test +++ b/test/misc5.test @@ -522,7 +522,8 @@ if {[permutation] == ""} { catchsql { CREATE TABLE t1(a,b,c); } - } {1 {file is encrypted or is not a database}} + } {1 {file is not a database}} + reset_db } # Ticket #1371. Allow floating point numbers of the form .N or N. @@ -569,9 +570,11 @@ ifcapable subquery&&compound { } # Overflow the lemon parser stack by providing an overly complex -# expression. Make sure that the overflow is detected and reported. +# expression. Make sure that the overflow is detected and the +# stack is grown automatically such that the application calling +# SQLite never notices. # -do_test misc5-7.1 { +do_test misc5-7.1.1 { execsql {CREATE TABLE t1(x)} set sql "INSERT INTO t1 VALUES(" set tail "" @@ -579,15 +582,28 @@ do_test misc5-7.1 { append sql "(1+" append tail ")" } - append sql 2$tail + append sql "0$tail); SELECT * FROM t1;" catchsql $sql -} {1 {parser stack overflow}} +} {0 200} +do_test misc5-7.1.2 { + execsql {DELETE FROM t1} + set sql "INSERT INTO t1 VALUES(" + set tail "" + for {set i 0} {$i<900} {incr i} { + append sql "(1+" + append tail ")" + } + append sql "0$tail); SELECT * FROM t1;" + catchsql $sql +} {0 900} + # Parser stack overflow is silently ignored when it occurs while parsing the # schema and PRAGMA writable_schema is turned on. # do_test misc5-7.2 { sqlite3 db2 :memory: + sqlite3_db_config db2 DEFENSIVE 0 catchsql { CREATE TABLE t1(x UNIQUE); PRAGMA writable_schema=ON; diff --git a/test/misc7.test b/test/misc7.test index 8fd5fe7546..f4ef2d2103 100644 --- a/test/misc7.test +++ b/test/misc7.test @@ -14,6 +14,7 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl +set testprefix misc7 if {[clang_sanitize_address]==0} { do_test misc7-1-misuse { @@ -42,15 +43,17 @@ do_test misc7-4 { # Try to open a file with a directory where its journal file should be. # -do_test misc7-5 { - delete_file mydir - file mkdir mydir-journal - sqlite3 db2 ./mydir - catchsql { - CREATE TABLE abc(a, b, c); - } db2 -} {1 {unable to open database file}} -db2 close +if {[atomic_batch_write test.db]==0} { + do_test misc7-5 { + delete_file mydir + file mkdir mydir-journal + sqlite3 db2 ./mydir + catchsql { + CREATE TABLE abc(a, b, c); + } db2 + } {1 {unable to open database file}} + db2 close +} #-------------------------------------------------------------------- # The following tests, misc7-6.* test the libraries behaviour when @@ -97,7 +100,7 @@ proc do_fileopen_test {prefix sql} { execsql { CREATE TABLE abc(a PRIMARY KEY, b, c); } db close -if {$tcl_platform(platform)!="windows"} { +if {$tcl_platform(platform) ne "windows"} { do_fileopen_test misc7-6.1 { BEGIN; INSERT INTO abc VALUES(1, 2, 3); @@ -224,6 +227,9 @@ ifcapable vtab { } } {1} set sqlite_where_trace 0 + do_catchsql_test misc7-10.1 { + INSERT INTO t1(a,b,c) VALUES(12345,2,3) ON CONFLICT(a) DO NOTHING; + } {1 {UPSERT not implemented for virtual table "t1"}} # Specify an ORDER BY clause that cannot be indexed. do_test misc7-11 { @@ -267,22 +273,27 @@ forcedelete test.db-journal sqlite3 db test.db ifcapable explain { - do_execsql_test misc7-14.1 { + do_execsql_test misc7-14.0 { CREATE TABLE abc(a PRIMARY KEY, b, c); - EXPLAIN QUERY PLAN SELECT * FROM abc AS t2 WHERE rowid = 1; - } { - 0 0 0 {SEARCH TABLE abc AS t2 USING INTEGER PRIMARY KEY (rowid=?)} - } - do_execsql_test misc7-14.2 { - EXPLAIN QUERY PLAN SELECT * FROM abc AS t2 WHERE a = 1; - } {0 0 0 - {SEARCH TABLE abc AS t2 USING INDEX sqlite_autoindex_abc_1 (a=?)} - } - do_execsql_test misc7-14.3 { - EXPLAIN QUERY PLAN SELECT * FROM abc AS t2 ORDER BY a; - } {0 0 0 - {SCAN TABLE abc AS t2 USING INDEX sqlite_autoindex_abc_1} } + do_eqp_test misc7-14.1 { + SELECT * FROM abc AS t2 WHERE rowid = 1; + } { + QUERY PLAN + `--SEARCH t2 USING INTEGER PRIMARY KEY (rowid=?) +} + do_eqp_test misc7-14.2 { + SELECT * FROM abc AS t2 WHERE a = 1; +} { + QUERY PLAN + `--SEARCH t2 USING INDEX sqlite_autoindex_abc_1 (a=?) +} + do_eqp_test misc7-14.3 { + SELECT * FROM abc AS t2 ORDER BY a; + } { + QUERY PLAN + `--SCAN t2 USING INDEX sqlite_autoindex_abc_1 +} } db close @@ -379,7 +390,7 @@ do_test misc7-16.X { # These tests do not work on windows due to restrictions in the # windows file system. # -if {$tcl_platform(platform)!="windows"} { +if {$tcl_platform(platform) ne "windows"} { # Some network filesystems (ex: AFP) do not support setting read-only # permissions. Only run these tests if full unix permission setting @@ -426,6 +437,7 @@ if {$tcl_platform(platform)!="windows"} { set ::pending_byte_page [expr ($::sqlite_pending_byte / 1024) + 1] sqlite3_test_control_pending_byte $::sqlite_pending_byte do_test misc7-17.3 { + sqlite3_db_config db DEFENSIVE 0 db eval { pragma writable_schema = true; UPDATE sqlite_master @@ -443,12 +455,13 @@ if {$tcl_platform(platform)!="windows"} { catchsql { SELECT count(*) FROM t3; } - } {1 {database disk image is malformed}} + } {1 {malformed database schema (t3) - invalid rootpage}} } } # Ticket #2470 # +reset_db do_test misc7-18.1 { execsql { CREATE TABLE table_1 (col_10); @@ -518,8 +531,43 @@ do_test misc7-22.3 { do_test misc7-22.4 { sqlite3_extended_errcode db } SQLITE_READONLY_ROLLBACK - -db close +catch { db close } forcedelete test.db +if {$::tcl_platform(platform) eq "unix" + && [atomic_batch_write test.db]==0 +} { + reset_db + do_execsql_test 23.0 { + CREATE TABLE t1(x, y); + INSERT INTO t1 VALUES(1, 2); + } + + do_test 23.1 { + db close + forcedelete tst + file mkdir tst + forcecopy test.db tst/test.db + file attributes tst -permissions r-xr-xr-x + } {} + + sqlite3 db tst/test.db + do_execsql_test 23.2 { + SELECT * FROM t1; + } {1 2} + + do_catchsql_test 23.3 { + INSERT INTO t1 VALUES(3, 4); + } {1 {attempt to write a readonly database}} + + do_test 23.4 { + sqlite3_extended_errcode db + } {SQLITE_READONLY_DIRECTORY} + + do_test 23.5 { + db close + forcedelete tst + } {} +} + finish_test diff --git a/test/misc8.test b/test/misc8.test index 3ff52e56f1..60b44fe1c7 100644 --- a/test/misc8.test +++ b/test/misc8.test @@ -57,6 +57,10 @@ do_catchsql_test misc8-1.7 { ORDER BY rowid; } {1 {abort due to ROLLBACK}} +do_catchsql_test misc8-1.8 { + PRAGMA empty_result_callbacks = 1; + SELECT eval('SELECT * FROM t1 WHERE 1 = 0;'); +} {0 {{}}} reset_db @@ -94,5 +98,49 @@ do_execsql_test misc8-2.1 { 0 10 {} 10 {} {} } +# 2016-02-26: An assertion fault found by the libFuzzer project +# +ifcapable allow_rowid_in_view { + set nosuch "1 {ambiguous column name: rowid}" +} else { + set nosuch "1 {no such column: rowid}" +} +do_catchsql_test misc8-3.0 { + SELECT * + FROM + ( + (SELECT 0 AS i) AS x1, + (SELECT 1) AS x2 + ) AS x3, + (SELECT 6 AS j UNION ALL SELECT 7) AS x4 + WHERE i<rowid + ORDER BY 1; +} $nosuch + +# The SQLITE_DBCONFIG_MAINDBNAME interface +# +db close +forcedelete test.db test2.db +sqlite3 db test.db +do_execsql_test misc8-4.0 { + CREATE TABLE t1(a,b,c); + INSERT INTO t1 VALUES(1,2,3); + ATTACH 'test2.db' AS aux2; + CREATE TABLE aux2.t2(c,d,e); + INSERT INTO t2 VALUES(4,5,6); + SELECT * FROM t1, t2; +} {1 2 3 4 5 6} +do_execsql_test misc8-4.1 { + PRAGMA database_list; +} {/0 main .* 2 aux2/} +dbconfig_maindbname_icecube db +do_execsql_test misc8-4.2 { + SELECT name FROM icecube.sqlite_master; +} {t1} +do_test misc8-4.3 { + regexp {0 icecube .* 2 aux2} [db eval {PRAGMA database_list}] +} 1 + + finish_test diff --git a/test/misuse.test b/test/misuse.test index d5d836cbfb..640cb5a4d3 100644 --- a/test/misuse.test +++ b/test/misuse.test @@ -172,19 +172,23 @@ do_test misuse-4.3 { lappend v $msg $r } {0 {} SQLITE_BUSY} -if {[clang_sanitize_address]==0} { +# All of the following tests can potentially (though rarely) +# lead to segfaults, which is unsettling. So they are disabled +# for now__________________________ +# v +if {[clang_sanitize_address]==0 && 0} { do_test misuse-4.4 { # Flush the TCL statement cache here, otherwise the sqlite3_close() will # fail because there are still un-finalized() VDBEs. db cache flush sqlite3_close $::DB catchsql2 {SELECT * FROM t1} - } {1 {library routine called out of sequence}} + } {1 {bad parameter or other API misuse}} do_test misuse-4.5 { catchsql { SELECT * FROM t1 } - } {1 {library routine called out of sequence}} + } {1 {bad parameter or other API misuse}} # Attempt to use a database after it has been closed. # @@ -204,7 +208,19 @@ if {[clang_sanitize_address]==0} { sqlite3_prepare $::DB {SELECT * FROM t1} -1 TAIL } msg] lappend r $msg - } {1 {(21) library routine called out of sequence}} + } {1 {(21) bad parameter or other API misuse}} } +#------------------------------------------------------------------------- +reset_db +do_test misuse-6.0 { + sqlite3_set_errmsg db 1 "an error has occurred" +} {SQLITE_OK} +do_test misuse-6.1 { + sqlite3_errmsg db +} {an error has occurred} +do_test misuse-6.2 { + sqlite3_set_errmsg "" 1 "an error has occurred" +} {SQLITE_MISUSE} + finish_test diff --git a/test/mjournal.test b/test/mjournal.test new file mode 100644 index 0000000000..7aaa86bae0 --- /dev/null +++ b/test/mjournal.test @@ -0,0 +1,162 @@ +# 2017 September 15 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix mjournal + +if {[permutation]=="inmemory_journal"} { + finish_test + return +} + +# Test that nothing bad happens if a journal file contains a pointer to +# a master journal file that does not have a "-" in the name. At one point +# this was causing a segfault on unix. +# +do_execsql_test 1.0 { + CREATE TABLE t1(a, b); +} + +do_test 1.1 { + forcedelete test.db2journal test.db-journal + + close [open test.db-journal w] + + hexio_write test.db-journal 0 746573742e6462326a6f75726e616c00 + hexio_write test.db-journal 16 00000010 + hexio_write test.db-journal 20 000005e1 + hexio_write test.db-journal 24 d9d505f920a163d7 + + close [open test.db2journal w] + hexio_write test.db2journal 0 abcd +} {2} + +do_execsql_test 1.2 { + SELECT * FROM t1; +} + +do_test 1.3 { + forcedelete test0db2journal test.db-journal + close [open test.db-journal w] + hexio_write test.db-journal 0 74657374306462326a6f75726e616c00 + hexio_write test.db-journal 16 00000010 + hexio_write test.db-journal 20 000005e3 + hexio_write test.db-journal 24 d9d505f920a163d7 + + close [open test0db2journal w] + hexio_write test0db2journal 0 abcd +} {2} + +do_execsql_test 1.4 { + SELECT * FROM t1; +} + +# And now test that nothing bad happens if a master journal contains a +# pointer to a journal file that does not have a "-" in the name. +# +do_test 1.5 { + forcedelete test.db2-master test.db-journal test1 + close [open test.db-journal w] + hexio_write test.db-journal 0 746573742e6462322d6d617374657200 + hexio_write test.db-journal 16 00000010 + hexio_write test.db-journal 20 0000059f + hexio_write test.db-journal 24 d9d505f920a163d7 + + close [open test.db2-master w] + hexio_write test.db2-master 0 746573743100 + + close [open test1 w] + hexio_write test1 0 abcd +} {2} + +do_execsql_test 1.6 { + SELECT * FROM t1; +} + +#------------------------------------------------------------------------- +# Check that master journals are not created if the transaction involves +# multiple temp files. +# +db close +testvfs tvfs +tvfs filter xOpen +tvfs script open_cb +set ::open "" +proc open_cb {method file arglist} { + lappend ::open $file +} + +proc contains_mj {} { + foreach f $::open { + set t [file tail $f] + if {[string match *mj* $t]} { return 1 } + } + return 0 +} + +# Like [do_execsql_test], except that a boolean indicating whether or +# not a master journal file was opened ([file tail] contains "mj") or +# not. Example: +# +# do_hasmj_test 1.0 { SELECT 'a', 'b' } {0 a b} +# +proc do_hasmj_test {tn sql expected} { + set ::open [list] + uplevel [list do_test $tn [subst -nocommands { + set res [execsql "$sql"] + concat [contains_mj] [set res] + }] [list {*}$expected]] +} + +forcedelete test.db +forcedelete test.db2 +forcedelete test.db3 +sqlite3 db test.db -vfs tvfs + +do_execsql_test 2.0 { + ATTACH 'test.db2' AS dbfile; + ATTACH '' AS dbtemp; + ATTACH ':memory:' AS dbmem; + + CREATE TABLE t1(x); + CREATE TABLE dbfile.t2(x); + CREATE TABLE dbtemp.t3(x); + CREATE TABLE dbmem.t4(x); +} + +# Two real files. +do_hasmj_test 2.1 { + BEGIN; + INSERT INTO t1 VALUES(1); + INSERT INTO t2 VALUES(1); + COMMIT; +} {1} + +# One real, one temp file. +do_hasmj_test 2.2 { + BEGIN; + INSERT INTO t1 VALUES(1); + INSERT INTO t3 VALUES(1); + COMMIT; +} {0} + +# One file, one :memory: db. +do_hasmj_test 2.3 { + BEGIN; + INSERT INTO t1 VALUES(1); + INSERT INTO t4 VALUES(1); + COMMIT; +} {0} + +finish_test diff --git a/test/mmap1.test b/test/mmap1.test index 199a058754..6a9625427a 100644 --- a/test/mmap1.test +++ b/test/mmap1.test @@ -12,7 +12,7 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl -ifcapable !mmap { +ifcapable !mmap||!incrblob { finish_test return } @@ -45,18 +45,18 @@ proc register_rblob_code {dbname seed} { } -# For cases 1.1 and 1.4, the number of pages read using xRead() is 4 on -# unix and 9 on windows. The difference is that windows only ever maps +# For cases 1.1 and 1.4, the number of pages read using xRead() is 8 on +# unix and 12 on windows. The difference is that windows only ever maps # an integer number of OS pages (i.e. creates mappings that are a multiple # of 4KB in size). Whereas on unix any sized mapping may be created. # foreach {t mmap_size nRead c2init} { - 1.1 { PRAGMA mmap_size = 67108864 } /[49]/ {PRAGMA mmap_size = 0} - 1.2 { PRAGMA mmap_size = 53248 } 150 {PRAGMA mmap_size = 0} - 1.3 { PRAGMA mmap_size = 0 } 344 {PRAGMA mmap_size = 0} - 1.4 { PRAGMA mmap_size = 67108864 } /[49]/ {PRAGMA mmap_size = 67108864 } - 1.5 { PRAGMA mmap_size = 53248 } 150 {PRAGMA mmap_size = 67108864 } - 1.6 { PRAGMA mmap_size = 0 } 344 {PRAGMA mmap_size = 67108864 } + 1.1 { PRAGMA mmap_size = 67108864 } /8|12/ {PRAGMA mmap_size = 0} + 1.2 { PRAGMA mmap_size = 53248 } /15[34]/ {PRAGMA mmap_size = 0} + 1.3 { PRAGMA mmap_size = 0 } 344 {PRAGMA mmap_size = 0} + 1.4 { PRAGMA mmap_size = 67108864 } /12|8/ {PRAGMA mmap_size = 67108864 } + 1.5 { PRAGMA mmap_size = 53248 } /15[34]/ {PRAGMA mmap_size = 67108864 } + 1.6 { PRAGMA mmap_size = 0 } 344 {PRAGMA mmap_size = 67108864 } } { do_multiclient_test tn { @@ -88,7 +88,7 @@ foreach {t mmap_size nRead c2init} { sql2 { DELETE FROM t1 WHERE rowid%2; } do_test $t.$tn.2 { sql1 "SELECT count(*) FROM t1; PRAGMA integrity_check ; PRAGMA page_count" - } {16 ok 42} + } "16 ok [expr {42+[nonzero_reserved_bytes]}]" # Have connection 2 grow the file. Check connection 1 can still read it. sql2 { INSERT INTO t1 SELECT rblob(500), rblob(500) FROM t1 } @@ -104,7 +104,9 @@ foreach {t mmap_size nRead c2init} { # Check that the number of pages read by connection 1 indicates that the # "PRAGMA mmap_size" command worked. - do_test $t.$tn.5 { nRead db } $nRead + if {[nonzero_reserved_bytes]==0} { + do_test $t.$tn.5 { nRead db } $nRead + } } } @@ -274,5 +276,48 @@ do_test 5.5 { sqlite3_finalize $::STMT } SQLITE_OK +# +# The "6.*" tests are designed to test the interaction of mmap with file +# truncation (e.g. on Win32) via the VACUUM command. +# +forcedelete test2.db +sqlite3 db2 test2.db +do_test 6.0 { + db2 eval { + PRAGMA auto_vacuum = 0; + PRAGMA page_size = 4096; + } +} {} +do_test 6.1 { + db2 eval { + CREATE TABLE t1(x); + INSERT INTO t1(x) VALUES(randomblob(1000000)); + } +} {} +do_test 6.2 { + db2 eval { + PRAGMA mmap_size = 1048576; + } +} {1048576} +do_test 6.3 { + expr {[file size test2.db] > 1000000} +} {1} +do_test 6.4 { + db2 eval { + DELETE FROM t1; + } +} {} +do_test 6.5 { + expr {[file size test2.db] > 1000000} +} {1} +do_test 6.6 { + db2 eval { + VACUUM; + } +} {} +do_test 6.7 { + expr {[file size test2.db] < 1000000} +} {1} +db2 close finish_test diff --git a/test/mmap2.test b/test/mmap2.test index 1f8346b915..df96671dfb 100644 --- a/test/mmap2.test +++ b/test/mmap2.test @@ -21,10 +21,10 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl set testprefix mmap2 -if {$::tcl_platform(platform)!="unix" || [test_syscall defaultvfs] != "unix"} { +if {[llength [info commands test_syscall]]==0} { finish_test return -} +} ifcapable !mmap { finish_test return diff --git a/test/mmap3.test b/test/mmap3.test index 07b5152968..184dda5f86 100644 --- a/test/mmap3.test +++ b/test/mmap3.test @@ -19,6 +19,9 @@ ifcapable !mmap||!vtab { source $testdir/lock_common.tcl set testprefix mmap3 +# A codec shuts down memory-mapped I/O +if {[nonzero_reserved_bytes]} {finish_test; return;} + do_test mmap3-1.0 { load_static_extension db wholenumber db eval { diff --git a/test/mmapcorrupt.test b/test/mmapcorrupt.test new file mode 100644 index 0000000000..d434ec1836 --- /dev/null +++ b/test/mmapcorrupt.test @@ -0,0 +1,55 @@ +# 2024 January 23 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# Test special cases of corrupt database handling in mmap-mode. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix mmapcorrupt + +ifcapable !mmap { + finish_test + return +} +database_may_be_corrupt + +db close +sqlite3_shutdown +sqlite3_config_lookaside 0 0 +sqlite3_initialize + +reset_db +do_execsql_test 1.0 { + PRAGMA page_size = 16384; + CREATE TABLE tn1(a PRIMARY KEY) WITHOUT ROWID; + CREATE TABLE t0(a PRIMARY KEY) WITHOUT ROWID; + CREATE TABLE t1(a PRIMARY KEY) WITHOUT ROWID; + INSERT INTO t1 VALUES('B'); +} +db close + +set sz [file size test.db] +hexio_write test.db [expr $sz-3] 800380 + +sqlite3 db test.db +do_execsql_test 2.1 { + PRAGMA mmap_size = 1000000; + SELECT sql FROM sqlite_schema LIMIT 1; + SELECT * FROM t0; +} {1000000 {CREATE TABLE tn1(a PRIMARY KEY) WITHOUT ROWID}} + +do_execsql_test 2.2 { + INSERT INTO t0 SELECT * FROM t1; +} + +finish_test + diff --git a/test/mmapwarm.test b/test/mmapwarm.test new file mode 100644 index 0000000000..b077047ebc --- /dev/null +++ b/test/mmapwarm.test @@ -0,0 +1,81 @@ +# 20 September 18 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl + + +if 0 { + db close + sqlite3_shutdown + proc msg {args} { puts $args } + test_sqlite3_log msg + sqlite3 db test.db +} + +set testprefix mmapwarm + + +do_execsql_test 1.0 { + PRAGMA auto_vacuum = 0; + CREATE TABLE t1(x, y); + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<500 + ) + INSERT INTO t1 SELECT randomblob(400), randomblob(500) FROM s; + PRAGMA page_count; +} {507} +db close + +do_test 1.1 { + sqlite3 db test.db + db eval {PRAGMA mmap_size = 1000000} + sqlite3_mmap_warm db +} {SQLITE_OK} + +do_test 1.2 { + db close + sqlite3 db test.db + db eval {PRAGMA mmap_size = 1000000} + sqlite3_mmap_warm db "main" +} {SQLITE_OK} + +do_test 1.3 { + sqlite3 db test.db + sqlite3_mmap_warm db +} {SQLITE_OK} + +do_test 1.4 { + db close + sqlite3 db test.db + sqlite3_mmap_warm db "main" +} {SQLITE_OK} + +do_test 2.0 { + db close + sqlite3 db test.db + db eval BEGIN + sqlite3_mmap_warm db "main" +} {SQLITE_MISUSE} + +do_faultsim_test 3 -faults oom* -prep { + sqlite3 db test.db + sqlite3_db_config_lookaside db 0 0 0 + db eval { PRAGMA mmap_size = 1000000 } + db eval { SELECT * FROM sqlite_master } +} -body { + sqlite3_mmap_warm db "main" +} -test { + faultsim_test_result {0 SQLITE_OK} {0 SQLITE_NOMEM} +} + +finish_test diff --git a/test/multiplex.test b/test/multiplex.test index 5db56f264a..2f1a02fb87 100644 --- a/test/multiplex.test +++ b/test/multiplex.test @@ -182,6 +182,11 @@ do_test multiplex-2.1.3 { file size [multiplex_name test.x 0] } {4096} do_test multiplex-2.1.4 { execsql { INSERT INTO t1 VALUES(3, randomblob(1100)) } } {} +do_execsql_test multiplex-2.1.5 { + PRAGMA multiplex_enabled; + PRAGMA multiplex_filecount; + PRAGMA multiplex_chunksize; +} {1 1 2147418112} do_test multiplex-2.2.1 { execsql { INSERT INTO t1 VALUES(3, randomblob(1100)) } @@ -195,15 +200,16 @@ do_test multiplex-2.3.1 { unset -nocomplain ::log -do_test multiplex-2.4.1 { - sqlite3_multiplex_shutdown -} {SQLITE_MISUSE} +#do_test multiplex-2.4.1 { +# sqlite3_multiplex_shutdown +#} {SQLITE_MISUSE} do_test multiplex-2.4.2 { execsql { INSERT INTO t1 VALUES(3, randomblob(1100)) } } {} -do_test multiplex-2.4.3 { - set ::log -} {SQLITE_MISUSE {sqlite3_multiplex_shutdown() called while database connections are still open}} +#do_test multiplex-2.4.3 { +# set ::log +#} {SQLITE_MISUSE {sqlite3_multiplex_shutdown() called while database connections are still open}} + do_test multiplex-2.4.4 { file size [multiplex_name test.x 0] } {7168} do_test multiplex-2.4.5 { db close @@ -266,6 +272,18 @@ do_test multiplex-2.5.8 { do_test multiplex-2.5.9 { file size [multiplex_name test.x 0] } [list $g_chunk_size] do_test multiplex-2.5.10 { file size [multiplex_name test.x 1] } [list $g_chunk_size] +do_execsql_test multiplex-2.5.11 { + PRAGMA multiplex_enabled; + PRAGMA multiplex_filecount; + PRAGMA multiplex_chunksize; +} {1 3 65536} +sqlite3 db test.x +do_execsql_test multiplex-2.5.12 { + PRAGMA multiplex_filecount; + PRAGMA multiplex_chunksize; +} {3 65536} + + do_test multiplex-2.5.99 { db close @@ -445,11 +463,7 @@ multiplex_set db main 32768 16 # Return a list of all currently defined multiplexs. proc multiplex_list {} { - set allq {} - foreach q [sqlite3_multiplex_dump] { - lappend allq [lindex $q 0] - } - return [lsort $allq] + glob -nocomplain test2.db* } do_test multiplex-4.1.6 { @@ -494,7 +508,7 @@ do_test multiplex-4.1.11 { do_test multiplex-4.1.12 { db close multiplex_list -} {} +} {test2.db} #------------------------------------------------------------------------- diff --git a/test/multiplex3.test b/test/multiplex3.test index c1e741acdb..3188350163 100644 --- a/test/multiplex3.test +++ b/test/multiplex3.test @@ -82,6 +82,8 @@ do_faultsim_test 1 -prep { multiplex_restore_db sqlite3 db file:test.db?8_3_names=1 sqlite3_multiplex_control db main chunk_size [expr 256*1024] + execsql { PRAGMA journal_mode = truncate } + execsql { PRAGMA synchronous = off } } -body { execsql { UPDATE t1 SET a=randomblob(12), b=randomblob(1500) WHERE (rowid%32)=0 diff --git a/test/mutex1.test b/test/mutex1.test index 340e271175..cb189a7a8a 100644 --- a/test/mutex1.test +++ b/test/mutex1.test @@ -38,7 +38,7 @@ proc mutex_counters {varname} { #------------------------------------------------------------------------- # Tests mutex1-1.* test that sqlite3_config() returns SQLITE_MISUSE if # is called at the wrong time. And that the first time sqlite3_initialize -# is called it obtains the 'static_master' mutex 3 times and a recursive +# is called it obtains the 'static_main' mutex 3 times and a recursive # mutex (sqlite3Config.pInitMutex) twice. Subsequent calls are no-ops # that do not require any mutexes. # @@ -75,7 +75,7 @@ do_test mutex1-1.6 { do_test mutex1-1.7 { mutex_counters counters - # list $counters(total) $counters(static_master) + # list $counters(total) $counters(static_main) expr {$counters(total)>0} } {1} @@ -86,7 +86,7 @@ do_test mutex1-1.8 { do_test mutex1-1.9 { mutex_counters counters - list $counters(total) $counters(static_master) + list $counters(total) $counters(static_main) } {0 0} #------------------------------------------------------------------------- @@ -97,27 +97,36 @@ do_test mutex1-1.9 { # * Multi-threaded mode, # * Single-threaded mode. # -ifcapable threadsafe&&shared_cache { +ifcapable threadsafe1&&shared_cache { set enable_shared_cache [sqlite3_enable_shared_cache 1] foreach {mode mutexes} { singlethread {} multithread { fast static_app1 static_app2 static_app3 - static_lru static_master static_mem static_open + static_lru static_main static_mem static_open static_prng static_pmem static_vfs1 static_vfs2 static_vfs3 } serialized { fast recursive static_app1 static_app2 - static_app3 static_lru static_master static_mem + static_app3 static_lru static_main static_mem static_open static_prng static_pmem static_vfs1 static_vfs2 static_vfs3 } } { + # For journal_mode=memory, the static_prng mutex is not required. This + # is because the header of an in-memory journal does not contain + # any random bytes, and so no call to sqlite3_randomness() is made. + if {[permutation]=="inmemory_journal"} { + set idx [lsearch $mutexes static_prng] + if {$idx>=0} { set mutexes [lreplace $mutexes $idx $idx] } + } + do_test mutex1.2.$mode.1 { catch {db close} sqlite3_shutdown + sqlite3_config_memstatus 1 sqlite3_config $mode } SQLITE_OK diff --git a/test/nan.test b/test/nan.test index df3f65b8e6..8d8a98ab3b 100644 --- a/test/nan.test +++ b/test/nan.test @@ -151,45 +151,47 @@ sqlite3_finalize $::STMT # Then it reads the value of the database to verify it is converted into # NULL. # -do_test nan-3.1 { - db eval { - DELETE FROM t1; - INSERT INTO t1 VALUES(0.5); - PRAGMA auto_vacuum=OFF; - PRAGMA page_size=1024; - VACUUM; - } - hexio_read test.db 2040 8 -} {3FE0000000000000} -do_test nan-3.2 { - db eval { - SELECT x, typeof(x) FROM t1 - } -} {0.5 real} -do_test nan-3.3 { - db close - hexio_write test.db 2040 FFF8000000000000 - sqlite3 db test.db - db eval {SELECT x, typeof(x) FROM t1} -} {{} null} -do_test nan-3.4 { - db close - hexio_write test.db 2040 7FF8000000000000 - sqlite3 db test.db - db eval {SELECT x, typeof(x) FROM t1} -} {{} null} -do_test nan-3.5 { - db close - hexio_write test.db 2040 FFFFFFFFFFFFFFFF - sqlite3 db test.db - db eval {SELECT x, typeof(x) FROM t1} -} {{} null} -do_test nan-3.6 { - db close - hexio_write test.db 2040 7FFFFFFFFFFFFFFF - sqlite3 db test.db - db eval {SELECT x, typeof(x) FROM t1} -} {{} null} +if {![nonzero_reserved_bytes]} { + do_test nan-3.1 { + db eval { + DELETE FROM t1; + INSERT INTO t1 VALUES(0.5); + PRAGMA auto_vacuum=OFF; + PRAGMA page_size=1024; + VACUUM; + } + hexio_read test.db 2040 8 + } {3FE0000000000000} + do_test nan-3.2 { + db eval { + SELECT x, typeof(x) FROM t1 + } + } {0.5 real} + do_test nan-3.3 { + db close + hexio_write test.db 2040 FFF8000000000000 + sqlite3 db test.db + db eval {SELECT x, typeof(x) FROM t1} + } {{} null} + do_test nan-3.4 { + db close + hexio_write test.db 2040 7FF8000000000000 + sqlite3 db test.db + db eval {SELECT x, typeof(x) FROM t1} + } {{} null} + do_test nan-3.5 { + db close + hexio_write test.db 2040 FFFFFFFFFFFFFFFF + sqlite3 db test.db + db eval {SELECT x, typeof(x) FROM t1} + } {{} null} + do_test nan-3.6 { + db close + hexio_write test.db 2040 7FFFFFFFFFFFFFFF + sqlite3 db test.db + db eval {SELECT x, typeof(x) FROM t1} + } {{} null} +} # Verify that the sqlite3AtoF routine is able to handle extreme # numbers. @@ -279,6 +281,7 @@ do_test nan-4.14 { # These tests test some really, really small floating point numbers. # +load_static_extension db decimal if {$tcl_platform(platform) != "symbian"} { # These two are not run on symbian because tcl has trouble converting # the very small numbers back to text form (probably due to a difference @@ -289,15 +292,15 @@ if {$tcl_platform(platform) != "symbian"} { set small \ [string repeat 0 10000].[string repeat 0 323][string repeat 9 10000] db eval "INSERT INTO t1 VALUES($small)" - db eval {SELECT x, typeof(x) FROM t1} - } {9.88131291682493e-324 real} + db eval {SELECT decimal_exp(x), typeof(x) FROM t1} + } {/9\.88131291682493\d*e-324 real/} do_test nan-4.16 { db eval {DELETE FROM t1} set small \ -[string repeat 0 10000].[string repeat 0 323][string repeat 9 10000] db eval "INSERT INTO t1 VALUES($small)" - db eval {SELECT x, typeof(x) FROM t1} - } {-9.88131291682493e-324 real} + db eval {SELECT decimal_exp(x), typeof(x) FROM t1} + } {/-9\.88131291682493\d*e-324 real/} } do_test nan-4.17 { db eval {DELETE FROM t1} @@ -364,8 +367,10 @@ do_realnum_test nan-4.35 { } } {0.0 real} - - - +do_realnum_test nan-4.40 { + db eval { + SELECT cast('-1e999' AS real); + } +} {-inf} finish_test diff --git a/test/nockpt.test b/test/nockpt.test new file mode 100644 index 0000000000..f5d11732be --- /dev/null +++ b/test/nockpt.test @@ -0,0 +1,149 @@ +# 2016 October 31 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is testing the SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE +# option. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/lock_common.tcl +source $testdir/malloc_common.tcl +source $testdir/wal_common.tcl +ifcapable !wal {finish_test ; return } +if {[permutation]=="journaltest" || [permutation]=="inmemory_journal"} { + finish_test + return +} + +set testprefix nockpt + +do_execsql_test 1.0 { + PRAGMA auto_vacuum=OFF; + PRAGMA page_size = 1024; + PRAGMA journal_mode = wal; + CREATE TABLE c1(x, y, z); + INSERT INTO c1 VALUES(1, 2, 3); +} {wal} + +do_test 1.1 { file exists test.db-wal } 1 +do_test 1.2 { file size test.db-wal } [wal_file_size 3 1024] +do_test 1.3 { db close } {} +do_test 1.4 { file exists test.db-wal } 0 + +sqlite3 db test.db +do_execsql_test 1.5 { + INSERT INTO c1 VALUES(4, 5, 6); + INSERT INTO c1 VALUES(7, 8, 9); +} +do_test 1.6 { file exists test.db-wal } 1 +do_test 1.7 { sqlite3_db_config db NO_CKPT_ON_CLOSE 1 } {1} +do_test 1.8 { file size test.db-wal } [wal_file_size 2 1024] +do_test 1.9 { db close } {} +do_test 1.10 { file exists test.db-wal } 1 +do_test 1.11 { file size test.db-wal } [wal_file_size 2 1024] + +sqlite3 db test.db +do_execsql_test 1.12 { + SELECT * FROM c1 +} {1 2 3 4 5 6 7 8 9} + +do_execsql_test 1.13 { PRAGMA main.journal_mode } {wal} +do_test 1.14 { sqlite3_db_config db NO_CKPT_ON_CLOSE 1 } {1} +do_execsql_test 1.14 { PRAGMA main.journal_mode = delete } {delete} +do_test 1.15 { file exists test.db-wal } {0} + +if {$::tcl_platform(platform) ne "windows"} { +#------------------------------------------------------------------------- +# Test an unusual scenario: +# +# 1. A wal mode db is opened and written. Then sqlite3_close_v2() used +# to close the db handle while there is still an unfinalized +# statement (so the db handle stays open). +# +# 2. The db, wal and *-shm files are deleted from the file system. +# +# 3. Another connection creates a new wal mode db at the same file-system +# location as the previous one. +# +# 4. The statement left unfinalized in (1) is finalized. +# +# The test is to ensure that the connection left open in step (1) does +# not try to delete the wal file from the file-system as part of step +# 4. +# +reset_db +db close + +# Open a connection on a wal database. Write to it a bit. Then prepare +# a statement and call sqlite3_close_v2() (so that the statement handle +# holds the db connection open). +# +set ::db1 [sqlite3_open_v2 test.db SQLITE_OPEN_READWRITE ""] +do_test 2.0 { + lindex [ + sqlite3_exec $::db1 { + PRAGMA journal_mode = wal; + CREATE TABLE t1(x PRIMARY KEY, y UNIQUE, z); + INSERT INTO t1 VALUES(1, 2, 3); + PRAGMA wal_checkpoint; + }] 0 +} {0} +set ::stmt [sqlite3_prepare $::db1 "SELECT * FROM t1" -1 dummy] +sqlite3_close_v2 $::db1 + +# Delete the database, wal and shm files. +# +catch {forcedelete test.db} +catch {forcedelete test.db-wal} +catch {forcedelete test.db-shm} + +# Open and populate a new database file at the same file-system location +# as the one just deleted. Contrive a partial checkpoint on it. +# +sqlite3 db test.db +sqlite3 db2 test.db +do_execsql_test 2.1 { + PRAGMA auto_vacuum=OFF; + PRAGMA journal_mode = wal; + CREATE TABLE y1(a PRIMARY KEY, b UNIQUE, c); + INSERT INTO y1 VALUES('a', 'b', 'c'); + INSERT INTO y1 VALUES('d', 'e', 'f'); +} {wal} +do_execsql_test -db db2 2.2 { + BEGIN; + SELECT * FROM y1; +} {a b c d e f} +do_execsql_test 2.3 { + UPDATE y1 SET c='g' WHERE a='d'; + PRAGMA wal_checkpoint; +} {0 11 10} +do_execsql_test -db db2 2.4 { + COMMIT +} + +# Finalize the statement handle, causing the first connection to be +# closed. Test that this has not corrupted the database file by +# deleting the new wal file from the file-system. If it has, this +# test should fail with an IO or corruption error. +# +do_test 2.5 { + sqlite3_finalize $::stmt + sqlite3 db3 test.db + execsql { + PRAGMA integrity_check; + SELECT * FROM y1; + } db3 +} {ok a b c d e g} +} + + +finish_test diff --git a/test/nolock.test b/test/nolock.test index 331af08ad7..e732dcf13b 100644 --- a/test/nolock.test +++ b/test/nolock.test @@ -182,4 +182,39 @@ do_test nolock-3.12 { db2 close db close tvfs delete + +if {[permutation]!="inmemory_journal"} { + # 2016-03-11: Make sure all works when transitioning to WAL mode + # under nolock. + # + do_test nolock-4.1 { + forcedelete test.db + sqlite3 db file:test.db?nolock=1 -uri 1 + db eval { + PRAGMA journal_mode=WAL; + CREATE TABLE t1(x); + INSERT INTO t1 VALUES('youngling'); + SELECT * FROM t1; + } + } {delete youngling} + db close + + do_test nolock-4.2 { + forcedelete test.db + sqlite3 db test.db + db eval { + PRAGMA journal_mode=WAL; + CREATE TABLE t1(x); + INSERT INTO t1 VALUES('catbird'); + SELECT * FROM t1; + } + } {wal catbird} + do_test nolock-4.3 { + db close + sqlite3 db file:test.db?nolock=1 -uri 1 + set rc [catch {db eval {SELECT * FROM t1}} msg] + lappend rc $msg + } {1 {unable to open database file}} +} + finish_test diff --git a/test/normalize.test b/test/normalize.test new file mode 100644 index 0000000000..29a5aabb26 --- /dev/null +++ b/test/normalize.test @@ -0,0 +1,394 @@ +# 2018-01-08 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# Tests for the sqlite3_normalize() extension function. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix normalize + +foreach {tnum sql norm} { + 100 + {SELECT * FROM t1 WHERE a IN (1) AND b=51.42} + {select*from t1 where a in(?,?,?)and b=?;} + + 110 + {SELECT a, b+15, c FROM t1 WHERE d NOT IN (SELECT x FROM t2);} + {select a,b+?,c from t1 where d not in(select x from t2);} + + 120 + { SELECT NULL, b FROM t1 -- comment text + WHERE d IN (WITH t(a) AS (VALUES(5)) /* CTE */ + SELECT a FROM t) + OR e='hello'; + } + {select?,b from t1 where d in(with t(a)as(values(?))select a from t)or e=?;} + + 121 + {/*Initial comment*/ + -- another comment line + SELECT NULL /* comment */ , b FROM t1 -- comment text + WHERE d IN (WITH t(a) AS (VALUES(5)) /* CTE */ + SELECT a FROM t) + OR e='hello'; + } + {select?,b from t1 where d in(with t(a)as(values(?))select a from t)or e=?;} + + 130 + {/* Query containing parameters */ + SELECT x,$::abc(15),y,@abc,z,?99,w FROM t1 /* Trailing comment */} + {select x,?,y,?,z,?,w from t1;} + + 140 + {/* Long list on the RHS of IN */ + SELECT 15 IN (1,2,3,(SELECT * FROM t1),'xyz',x'abcd',22*(x+5),null);} + {select?in(?,?,?);} + + 150 + {SELECT x'abc'; -- illegal token} + {} + + 160 + {SELECT a,NULL,b FROM t1 WHERE c IS NOT NULL or D is null or e=5} + {select a,?,b from t1 where c is not null or d is null or e=?;} + + 170 + {/* IN list exactly 5 bytes long */ + SELECT * FROM t1 WHERE x IN (1,2,3);} + {select*from t1 where x in(?,?,?);} + 180 + { } + {} +} { + do_test $tnum [list sqlite3_normalize $sql] $norm +} + +ifcapable normalize { +do_test 200 { + execsql { + CREATE TABLE t1(a,b); + } +} {} +do_test 201 { + set STMT [sqlite3_prepare_v3 $DB \ + "SELECT a, b FROM t1 WHERE b = ? ORDER BY a;" -1 0 TAIL] + + sqlite3_bind_null $STMT 1 +} {} +do_test 202 { + sqlite3_normalized_sql $STMT +} {SELECT a,b FROM t1 WHERE b=?ORDER BY a;} +do_test 203 { + sqlite3_finalize $STMT +} {SQLITE_OK} + +do_test 210 { + set STMT [sqlite3_prepare_v3 $DB \ + "SELECT a, b FROM t1 WHERE b = ? ORDER BY a;" -1 2 TAIL] + + sqlite3_bind_null $STMT 1 +} {} +do_test 211 { + sqlite3_normalized_sql $STMT +} {SELECT a,b FROM t1 WHERE b=?ORDER BY a;} +do_test 212 { + sqlite3_finalize $STMT +} {SQLITE_OK} + +do_test 220 { + set STMT [sqlite3_prepare_v3 $DB \ + "SELECT a, b FROM t1 WHERE b = 'a' ORDER BY a;" -1 2 TAIL] +} {/^[0-9A-Fa-f]+$/} +do_test 221 { + sqlite3_normalized_sql $STMT +} {SELECT a,b FROM t1 WHERE b=?ORDER BY a;} +do_test 222 { + sqlite3_finalize $STMT +} {SQLITE_OK} + +do_test 297 { + execsql { + DROP TABLE t1; + } +} {} +do_test 298 { + execsql { + CREATE TABLE t1(a,b,c,d,e,"col f",w,x,y,z); + CREATE TABLE t2(x,"col y"); + } +} {} +do_test 299 { + sqlite3_create_function db +} {SQLITE_OK} + +foreach {tnum sql flags norm} { + 300 + {SELECT * FROM t1 WHERE a IN (1) AND b=51.42} + 0x2 + {0 {SELECT*FROM t1 WHERE a IN(?,?,?)AND b=?;}} + + 310 + {SELECT a, b+15, c FROM t1 WHERE d NOT IN (SELECT x FROM t2);} + 0x2 + {0 {SELECT a,b+?,c FROM t1 WHERE d NOT IN(SELECT x FROM t2);}} + + 320 + { SELECT NULL, b FROM t1 -- comment text + WHERE d IN (WITH t(a) AS (VALUES(5)) /* CTE */ + SELECT a FROM t) + OR e='hello'; + } + 0x2 + {0 {SELECT?,b FROM t1 WHERE d IN(WITH t(a)AS(VALUES(?))SELECT a FROM t)OR e=?;}} + + 321 + {/*Initial comment*/ + -- another comment line + SELECT NULL /* comment */ , b FROM t1 -- comment text + WHERE d IN (WITH t(a) AS (VALUES(5)) /* CTE */ + SELECT a FROM t) + OR e='hello'; + } + 0x2 + {0 {SELECT?,b FROM t1 WHERE d IN(WITH t(a)AS(VALUES(?))SELECT a FROM t)OR e=?;}} + + 330 + {/* Query containing parameters */ + SELECT x,$::abc(15),y,@abc,z,?99,w FROM t1 /* Trailing comment */} + 0x2 + {0 {SELECT x,?,y,?,z,?,w FROM t1;}} + + 340 + {/* Long list on the RHS of IN */ + SELECT 15 IN (1,2,3,(SELECT * FROM t1),'xyz',x'abcd',22*(x+5),null);} + 0x2 + {1 {(1) no such column: x}} + + 350 + {SELECT x'abc'; -- illegal token} + 0x2 + {1 {(1) unrecognized token: "x'abc'"}} + + 360 + {SELECT a,NULL,b FROM t1 WHERE c IS NOT NULL or D is null or e=5} + 0x2 + {0 {SELECT a,?,b FROM t1 WHERE c IS NOT NULL OR d IS NULL OR e=?;}} + + 370 + {/* IN list exactly 5 bytes long */ + SELECT * FROM t1 WHERE x IN (1,2,3);} + 0x2 + {0 {SELECT*FROM t1 WHERE x IN(?,?,?);}} + + 400 + {SELECT a FROM t1 WHERE x IN (1,2,3) AND sqlite_version();} + 0x2 + {0 {SELECT a FROM t1 WHERE x IN(?,?,?)AND sqlite_version();}} + + 410 + {SELECT a FROM t1 WHERE x IN (1,2,3) AND hex8();} + 0x2 + {1 {(1) wrong number of arguments to function hex8()}} + + 420 + {SELECT a FROM t1 WHERE x IN (1,2,3) AND hex8('abc');} + 0x2 + {0 {SELECT a FROM t1 WHERE x IN(?,?,?)AND hex8(?);}} + + 430 + {SELECT "a" FROM t1 WHERE "x" IN ("1","2",'3');} + 0x2 + {0 {SELECT a FROM t1 WHERE x IN(?,?,?);}} + + 440 + {SELECT 'a' FROM t1 WHERE 'x';} + 0x2 + {0 {SELECT?FROM t1 WHERE?;}} + + 450 + {SELECT [a] FROM t1 WHERE [x];} + 0x2 + {0 {SELECT a FROM t1 WHERE x;}} + + 460 + {SELECT * FROM t1 WHERE x IN (x);} + 0x2 + {0 {SELECT*FROM t1 WHERE x IN(x);}} + + 470 + {SELECT * FROM t1 WHERE x IN (x,a);} + 0x2 + {0 {SELECT*FROM t1 WHERE x IN(x,a);}} + + 480 + {SELECT * FROM t1 WHERE x IN ([x],"a");} + 0x2 + {0 {SELECT*FROM t1 WHERE x IN(x,a);}} + + 500 + {SELECT * FROM t1 WHERE x IN ([x],"a",'b',sqlite_version());} + 0x2 + {0 {SELECT*FROM t1 WHERE x IN(x,a,?,sqlite_version());}} + + 520 + {SELECT * FROM t1 WHERE x IN (SELECT x FROM t1);} + 0x2 + {0 {SELECT*FROM t1 WHERE x IN(SELECT x FROM t1);}} + + 540 + {SELECT * FROM t1 WHERE x IN ((SELECT x FROM t1));} + 0x2 + {0 {SELECT*FROM t1 WHERE x IN((SELECT x FROM t1));}} + + 550 + {SELECT a, a+1, a||'b', a+"b" FROM t1;} + 0x2 + {0 {SELECT a,a+?,a||?,a+b FROM t1;}} + + 570 + {SELECT * FROM t1 WHERE x IN (1);} + 0x2 + {0 {SELECT*FROM t1 WHERE x IN(?,?,?);}} + + 580 + {SELECT * FROM t1 WHERE x IN (1,2);} + 0x2 + {0 {SELECT*FROM t1 WHERE x IN(?,?,?);}} + + 590 + {SELECT * FROM t1 WHERE x IN (1,2,3);} + 0x2 + {0 {SELECT*FROM t1 WHERE x IN(?,?,?);}} + + 600 + {SELECT * FROM t1 WHERE x IN (1,2,3,4);} + 0x2 + {0 {SELECT*FROM t1 WHERE x IN(?,?,?);}} + + 610 + {SELECT * FROM t1 WHERE x IN (SELECT x FROM t1);} + 0x2 + {0 {SELECT*FROM t1 WHERE x IN(SELECT x FROM t1);}} + + 620 + {SELECT * FROM t1 WHERE x IN (SELECT x FROM t1 WHERE x IN (1,2,3));} + 0x2 + {0 {SELECT*FROM t1 WHERE x IN(SELECT x FROM t1 WHERE x IN(?,?,?));}} + + 630 + {SELECT * FROM t1 WHERE x IN (SELECT x FROM t1 WHERE x IN (x));} + 0x2 + {0 {SELECT*FROM t1 WHERE x IN(SELECT x FROM t1 WHERE x IN(x));}} + + 640 + {SELECT x FROM t1 WHERE x IN (SELECT x FROM t1 WHERE x IN ( + SELECT x FROM t1 WHERE x IN (SELECT x FROM t1 WHERE x IN ( + SELECT x FROM t1 WHERE x IN (x)))));} + 0x2 + {0 {SELECT x FROM t1 WHERE x IN(SELECT x FROM t1 WHERE x IN(SELECT x FROM t1 WHERE x IN(SELECT x FROM t1 WHERE x IN(SELECT x FROM t1 WHERE x IN(x)))));}} + + 650 + {SELECT x FROM t1 WHERE x IN (SELECT x FROM t1 WHERE x IN ( + SELECT x FROM t1 WHERE x IN (SELECT x FROM t1 WHERE x IN ( + SELECT x FROM t1 WHERE x IN (1)))));} + 0x2 + {0 {SELECT x FROM t1 WHERE x IN(SELECT x FROM t1 WHERE x IN(SELECT x FROM t1 WHERE x IN(SELECT x FROM t1 WHERE x IN(SELECT x FROM t1 WHERE x IN(?,?,?)))));}} + + 660 + {SELECT x FROM t1 WHERE x IN (1) UNION ALL SELECT x FROM t1 WHERE x IN (1);} + 0x2 + {0 {SELECT x FROM t1 WHERE x IN(?,?,?)UNION ALL SELECT x FROM t1 WHERE x IN(?,?,?);}} + + 670 + {SELECT "col f", [col f] FROM t1;} + 0x2 + {0 {SELECT"col f","col f"FROM t1;}} + + 680 + {SELECT a, "col f" FROM t1 LEFT OUTER JOIN t2 ON [t1].[col f] == [t2].[col y];} + 0x2 + {0 {SELECT a,"col f"FROM t1 LEFT OUTER JOIN t2 ON t1."col f"==t2."col y";}} + + 690 + {SELECT * FROM ( WITH x AS ( SELECT * FROM t1 WHERE x IN ( 1)) SELECT 10);} + 0x2 + {0 {SELECT*FROM(WITH x AS(SELECT*FROM t1 WHERE x IN(?,?,?))SELECT?);}} + + 700 + {SELECT rowid, oid, _rowid_ FROM t1;} + 0x2 + {0 {SELECT rowid,oid,_rowid_ FROM t1;}} + + 710 + {SELECT x FROM t1 WHERE x IS NULL;} + 0x2 + {0 {SELECT x FROM t1 WHERE x IS NULL;}} + + 740 + {SELECT x FROM t1 WHERE x IS NOT NULL;} + 0x2 + {0 {SELECT x FROM t1 WHERE x IS NOT NULL;}} + + 750 + {SELECT x FROM t1 WHERE x = NULL;} + 0x2 + {0 {SELECT x FROM t1 WHERE x=?;}} + + 760 + {SELECT x FROM t1 WHERE x IN ([x] IS NOT NULL, NULL, 1, 'a', "b", x'00');} + 0x2 + {0 {SELECT x FROM t1 WHERE x IN(x IS NOT NULL,?,?,?,b,?);}} + + 800 + {ATTACH "normalize800.db" AS somefile;} + 0x2 + {0 {ATTACH"normalize800.db"AS somefile;}} + + 810 + {ATTACH DATABASE "normalize810.db" AS somefile;} + 0x2 + {0 {ATTACH DATABASE"normalize810.db"AS somefile;}} + + 900 + {INSERT INTO t1 (x) VALUES("sl1"), (1), ("sl2"), ('i');} + 0x2 + {0 {INSERT INTO t1(x)VALUES(?),(?),(?),(?);}} + + 910 + {UPDATE t1 SET x = "sl1" WHERE x IN (1, "sl2", 'i');} + 0x2 + {0 {UPDATE t1 SET x=?WHERE x IN(?,?,?);}} + + 920 + {UPDATE t1 SET x = "y" WHERE x IN (1, "sl1", 'i');} + 0x2 + {0 {UPDATE t1 SET x=y WHERE x IN(?,?,?);}} + + 930 + {DELETE FROM t1 WHERE x IN (1, "sl1", 'i');} + 0x2 + {0 {DELETE FROM t1 WHERE x IN(?,?,?);}} +} { + do_test $tnum { + set code [catch { + set STMT [sqlite3_prepare_v3 $DB $sql -1 $flags TAIL] + sqlite3_normalized_sql $STMT + } res] + if {[info exists STMT]} { + sqlite3_finalize $STMT; unset STMT + } + list $code $res + } $norm +} +} + +finish_test diff --git a/test/notify3.test b/test/notify3.test index 4b5e8016b4..6d65b644d1 100644 --- a/test/notify3.test +++ b/test/notify3.test @@ -108,8 +108,8 @@ if {[presql] == ""} { } " 0 0 0 0 $err SQLITE_LOCKED SQLITE_LOCKED_SHAREDCACHE 1 0 0 1 $err SQLITE_LOCKED_SHAREDCACHE SQLITE_LOCKED_SHAREDCACHE - 2 0 1 0 $err SQLITE_LOCKED SQLITE_LOCKED_SHAREDCACHE - 3 0 1 1 $err SQLITE_LOCKED_SHAREDCACHE SQLITE_LOCKED_SHAREDCACHE + 2 0 1 0 $noerr SQLITE_OK SQLITE_OK + 3 0 1 1 $noerr SQLITE_OK SQLITE_OK 4 1 0 0 $err SQLITE_LOCKED SQLITE_LOCKED_SHAREDCACHE 5 1 0 1 $err SQLITE_LOCKED_SHAREDCACHE SQLITE_LOCKED_SHAREDCACHE 6 1 1 0 $noerr SQLITE_OK SQLITE_OK diff --git a/test/notnull.test b/test/notnull.test index 23fd33d4ba..23abe31f28 100644 --- a/test/notnull.test +++ b/test/notnull.test @@ -561,4 +561,48 @@ do_test notnull-5.5 { execsql { SELECT * FROM t1 } } {1 2} +#------------------------------------------------------------------------- +# Check that UNIQUE NOT NULL indexes are always recognized as such. +# +proc uses_op_next {sql} { + db eval "EXPLAIN $sql" a { + if {$a(opcode)=="Next"} { return 1 } + } + return 0 +} + +proc do_uses_op_next_test {tn sql res} { + uplevel [list do_test $tn [list uses_op_next $sql] $res] +} + +reset_db +do_execsql_test notnull-6.0 { + CREATE TABLE t1(a UNIQUE); + CREATE TABLE t2(a NOT NULL UNIQUE); + CREATE TABLE t3(a UNIQUE NOT NULL); + CREATE TABLE t4(a NOT NULL); + CREATE UNIQUE INDEX t4a ON t4(a); + + CREATE TABLE t5(a PRIMARY KEY); + CREATE TABLE t6(a PRIMARY KEY NOT NULL); + CREATE TABLE t7(a NOT NULL PRIMARY KEY); + CREATE TABLE t8(a PRIMARY KEY) WITHOUT ROWID; + + CREATE TABLE t9(a PRIMARY KEY UNIQUE NOT NULL); + CREATE TABLE t10(a UNIQUE PRIMARY KEY NOT NULL); +} + +do_uses_op_next_test notnull-6.1 "SELECT * FROM t1 WHERE a IS ?" 1 +do_uses_op_next_test notnull-6.2 "SELECT * FROM t2 WHERE a IS ?" 0 +do_uses_op_next_test notnull-6.3 "SELECT * FROM t3 WHERE a IS ?" 0 +do_uses_op_next_test notnull-6.4 "SELECT * FROM t4 WHERE a IS ?" 0 + +do_uses_op_next_test notnull-6.5 "SELECT * FROM t5 WHERE a IS ?" 1 +do_uses_op_next_test notnull-6.6 "SELECT * FROM t6 WHERE a IS ?" 0 +do_uses_op_next_test notnull-6.7 "SELECT * FROM t7 WHERE a IS ?" 0 +do_uses_op_next_test notnull-6.8 "SELECT * FROM t8 WHERE a IS ?" 0 + +do_uses_op_next_test notnull-6.9 "SELECT * FROM t8 WHERE a IS ?" 0 +do_uses_op_next_test notnull-6.10 "SELECT * FROM t8 WHERE a IS ?" 0 + finish_test diff --git a/test/notnull2.test b/test/notnull2.test new file mode 100644 index 0000000000..67d7c26a8d --- /dev/null +++ b/test/notnull2.test @@ -0,0 +1,122 @@ +# 2021 February 15 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is testing optimizations associated with "IS NULL" +# and "IS NOT NULL" operators on columns with NOT NULL constraints. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix notnull2 + +do_execsql_test 1.0 { + CREATE TABLE t1(a, b); + CREATE TABLE t2(c, d NOT NULL); + + WITH x(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM x WHERE i<1000 + ) + INSERT INTO t1 SELECT i, i FROM x; + INSERT INTO t2 SELECT * FROM t1; +} + + +do_vmstep_test 1.1.1 { + SELECT * FROM t1 LEFT JOIN t2 WHERE a=c AND d IS NULL; +} 100 {} +do_vmstep_test 1.1.2 { + SELECT * FROM t1 LEFT JOIN t2 WHERE a=c AND c IS NULL; +} +1000 {} + +do_vmstep_test 1.2.1 { + SELECT * FROM ( SELECT * FROM t2 ) WHERE d IS NULL +} 100 {} +do_vmstep_test 1.2.2 { + SELECT * FROM ( SELECT * FROM t2 ) WHERE c IS NULL +} +1000 {} + +do_vmstep_test 1.3.1 { + SELECT * FROM t2 WHERE d IS NULL +} 100 {} +do_vmstep_test 1.3.2 { + SELECT * FROM t2 WHERE c IS NULL +} +1000 {} + +do_vmstep_test 1.4.1 { + SELECT (d IS NOT NULL) FROM t2 WHERE 0==( d IS NOT NULL ) +} 100 {} +do_vmstep_test 1.4.2 { + SELECT * FROM t2 WHERE 0==( c IS NOT NULL ) +} +1000 {} + +do_vmstep_test 1.5.1 { + SELECT count(*) FROM t2 WHERE EXISTS( + SELECT 1 FROM t1 WHERE t1.a=450 AND t2.d IS NULL + ) +} 7000 {0} +do_vmstep_test 1.5.2 { + SELECT count(*) FROM t2 WHERE EXISTS( + SELECT 1 FROM t1 WHERE t1.a=450 AND t2.c IS NULL + ) +} 4000 {0} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 2.0 { + CREATE TABLE T1(a INTEGER PRIMARY KEY, b); + CREATE TABLE T3(k, v); +} + +do_execsql_test 2.1 { + SELECT * FROM (SELECT a, b FROM t1) LEFT JOIN t3 ON a IS NULL; +} + + + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 3.0 { + CREATE TABLE t0(c0 PRIMARY KEY); + INSERT INTO t0(c0) VALUES (0); +} +do_execsql_test 3.1 { + SELECT * FROM t0 WHERE ((c0 NOT NULL) AND 1) OR (c0 == NULL); +} {0} + +# 2021-07-22 https://sqlite.org/forum/forumpost/2078b7edd2 +# +reset_db +do_execsql_test 4.0 { + SELECT *, '/' + FROM ( + SELECT NULL val FROM (SELECT 1) + UNION ALL + SELECT 'missing' FROM (SELECT 1) + ) a + LEFT JOIN (SELECT 1) + ON a.val IS NULL; +} {{} 1 / missing {} /} +do_execsql_test 4.1 { + CREATE TABLE t1(a INT); + INSERT INTO t1(a) VALUES(1); + CREATE TABLE t2(b INT); + SELECT * FROM (SELECT 3 AS c FROM t1) AS t3 LEFT JOIN t2 ON c IS NULL; +} {3 {}} + +# 2024-03-08 https://sqlite.org/forum/forumpost/440f2a2f17 +# +reset_db +do_execsql_test 5.0 { + CREATE TABLE t1(a INT NOT NULL); + SELECT a IS NULL, a IS NOT NULL, count(*) FROM t1; +} {1 0 0} + +finish_test diff --git a/test/notnullfault.test b/test/notnullfault.test new file mode 100644 index 0000000000..0db8e4adbd --- /dev/null +++ b/test/notnullfault.test @@ -0,0 +1,55 @@ +# 2021 February 15 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is testing optimizations associated with "IS NULL" +# and "IS NOT NULL" operators on columns with NOT NULL constraints. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix notnullfault + +do_execsql_test 1.0 { + CREATE TABLE t1(a, b); + CREATE TABLE t2(c, d NOT NULL); +} +faultsim_save_and_close + +do_faultsim_test 1 -prep { + faultsim_restore_and_reopen +} -body { + execsql { + SELECT * FROM t2 WHERE d NOT NULL + } +} -test { + faultsim_test_result {0 {}} +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 2.0 { + CREATE TABLE t1(a, b, c); + CREATE TABLE t2(a, b, c, PRIMARY KEY(a, b, c)) WITHOUT ROWID; +} +faultsim_save_and_close + +do_faultsim_test 2.1 -faults oom-t* -prep { + faultsim_restore_and_reopen +} -body { + execsql { + SELECT dense_rank() OVER win FROM t2 + WINDOW win AS (ORDER BY c IS NULL) + } +} -test { + faultsim_test_result {0 {}} +} + +finish_test diff --git a/test/null.test b/test/null.test index e8eeb9740b..5057e2537e 100644 --- a/test/null.test +++ b/test/null.test @@ -296,5 +296,12 @@ do_execsql_test null-9.3 { SELECT * FROM t5 WHERE a IS NULL AND b = 'x'; } {{} x two {} x ii} +# 2020-09-30 ticket 5c4e7aa793943803 +reset_db +do_execsql_test null-10.1 { + CREATE TABLE t0(c0 PRIMARY KEY DESC); + INSERT INTO t0(c0) VALUES (0); + SELECT * FROM t0 WHERE t0.c0 > NULL; +} {} finish_test diff --git a/test/nulls1.test b/test/nulls1.test new file mode 100644 index 0000000000..c44d0af251 --- /dev/null +++ b/test/nulls1.test @@ -0,0 +1,343 @@ +# 2019 August 10 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix nulls1 + +do_execsql_test 1.0 { + DROP TABLE IF EXISTS t3; + CREATE TABLE t3(a INTEGER); + INSERT INTO t3 VALUES(NULL), (10), (30), (20), (NULL); +} {} + +for {set a 0} {$a < 3} {incr a} { + foreach {tn limit} { + 1 "" + 2 "LIMIT 10" + } { + do_execsql_test 1.$a.$tn.1 " + SELECT a FROM t3 ORDER BY a nULLS FIRST $limit + " {{} {} 10 20 30} + + do_execsql_test 1.$a.$tn.2 " + SELECT a FROM t3 ORDER BY a nULLS LAST $limit + " {10 20 30 {} {}} + + do_execsql_test 1.$a.$tn.3 " + SELECT a FROM t3 ORDER BY a DESC nULLS FIRST $limit + " {{} {} 30 20 10} + + do_execsql_test 1.$a.$tn.4 " + SELECT a FROM t3 ORDER BY a DESC nULLS LAST $limit + " {30 20 10 {} {}} + } + + switch $a { + 0 { + execsql { CREATE INDEX i1 ON t3(a) } + } + 1 { + execsql { DROP INDEX i1 ; CREATE INDEX i1 ON t3(a DESC) } + } + } +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 2.0 { + CREATE TABLE t2(a, b, c); + CREATE INDEX i2 ON t2(a, b); + INSERT INTO t2 VALUES(1, 1, 1); + INSERT INTO t2 VALUES(1, NULL, 2); + INSERT INTO t2 VALUES(1, NULL, 3); + INSERT INTO t2 VALUES(1, 4, 4); +} + +do_execsql_test 2.1 { + SELECT * FROM t2 WHERE a=1 ORDER BY b NULLS LAST +} { + 1 1 1 1 4 4 1 {} 2 1 {} 3 +} + +do_execsql_test 2.2 { + SELECT * FROM t2 WHERE a=1 ORDER BY b DESC NULLS FIRST +} { + 1 {} 3 + 1 {} 2 + 1 4 4 + 1 1 1 +} + +#------------------------------------------------------------------------- +# +reset_db +do_execsql_test 3.0 { + CREATE TABLE t1(a, b, c, d, UNIQUE (b)); +} +foreach {tn sql err} { + 1 { CREATE INDEX i1 ON t1(a ASC NULLS LAST) } LAST + 2 { CREATE INDEX i1 ON t1(a ASC NULLS FIRST) } FIRST + 3 { CREATE INDEX i1 ON t1(a, b ASC NULLS LAST) } LAST + 4 { CREATE INDEX i1 ON t1(a, b ASC NULLS FIRST) } FIRST + 5 { CREATE INDEX i1 ON t1(a DESC NULLS LAST) } LAST + 6 { CREATE INDEX i1 ON t1(a DESC NULLS FIRST) } FIRST + 7 { CREATE INDEX i1 ON t1(a, b DESC NULLS LAST) } LAST + 8 { CREATE INDEX i1 ON t1(a, b DESC NULLS FIRST) } FIRST + 9 { CREATE TABLE t2(a, b, PRIMARY KEY(a DESC, b NULLS FIRST)) } FIRST + 10 { CREATE TABLE t2(a, b, UNIQUE(a DESC NULLS FIRST, b)) } FIRST + 11 { INSERT INTO t1 VALUES(1, 2, 3, 4) + ON CONFLICT (b DESC NULLS LAST) DO UPDATE SET a = a+1 } LAST + 12 { + CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN + INSERT INTO t1 VALUES(1, 2, 3, 4) + ON CONFLICT (b DESC NULLS FIRST) DO UPDATE SET a = a+1; + END + } FIRST +} { + do_catchsql_test 3.1.$tn $sql "1 {unsupported use of NULLS $err}" +} + +do_execsql_test 3.2 { + CREATE TABLE first(nulls, last); + INSERT INTO first(last, nulls) VALUES(100,200), (300,400), (200,300); + SELECT * FROM first ORDER BY nulls; +} { + 200 100 + 300 200 + 400 300 +} + +#------------------------------------------------------------------------- +# +ifcapable vtab { + register_echo_module db + do_execsql_test 4.0 { + CREATE TABLE tx(a INTEGER PRIMARY KEY, b, c); + CREATE INDEX i1 ON tx(b); + INSERT INTO tx VALUES(1, 1, 1); + INSERT INTO tx VALUES(2, NULL, 2); + INSERT INTO tx VALUES(3, 3, 3); + INSERT INTO tx VALUES(4, NULL, 4); + INSERT INTO tx VALUES(5, 5, 5); + CREATE VIRTUAL TABLE te USING echo(tx); + } + + do_execsql_test 4.1 { + SELECT * FROM tx ORDER BY b NULLS FIRST; + } {2 {} 2 4 {} 4 1 1 1 3 3 3 5 5 5} + do_execsql_test 4.2 { + SELECT * FROM te ORDER BY b NULLS FIRST; + } {2 {} 2 4 {} 4 1 1 1 3 3 3 5 5 5} + + do_execsql_test 4.3 { + SELECT * FROM tx ORDER BY b NULLS LAST; + } {1 1 1 3 3 3 5 5 5 2 {} 2 4 {} 4} + do_execsql_test 4.4 { + SELECT * FROM te ORDER BY b NULLS LAST; + } {1 1 1 3 3 3 5 5 5 2 {} 2 4 {} 4} +} + +#------------------------------------------------------------------------- +# +do_execsql_test 5.0 { + CREATE TABLE t4(a, b, c); + INSERT INTO t4 VALUES(1, 1, 11); + INSERT INTO t4 VALUES(1, 2, 12); + INSERT INTO t4 VALUES(1, NULL, 1); + + INSERT INTO t4 VALUES(2, NULL, 1); + INSERT INTO t4 VALUES(2, 2, 12); + INSERT INTO t4 VALUES(2, 1, 11); + + INSERT INTO t4 VALUES(3, NULL, 1); + INSERT INTO t4 VALUES(3, 2, 12); + INSERT INTO t4 VALUES(3, NULL, 3); +} + +do_execsql_test 5.1 { + SELECT * FROM t4 WHERE a IN (1, 2, 3) ORDER BY a, b NULLS LAST +} { + 1 1 11 1 2 12 1 {} 1 + 2 1 11 2 2 12 2 {} 1 + 3 2 12 3 {} 1 3 {} 3 +} +do_execsql_test 5.2 { + CREATE INDEX t4ab ON t4(a, b); + SELECT * FROM t4 WHERE a IN (1, 2, 3) ORDER BY a, b NULLS LAST +} { + 1 1 11 1 2 12 1 {} 1 + 2 1 11 2 2 12 2 {} 1 + 3 2 12 3 {} 1 3 {} 3 +} +do_eqp_test 5.3 { + SELECT * FROM t4 WHERE a IN (1, 2, 3) ORDER BY a, b NULLS LAST +} { + QUERY PLAN + `--SEARCH t4 USING INDEX t4ab (a=?) +} + +do_execsql_test 5.4 { + SELECT * FROM t4 WHERE a IN (1, 2, 3) ORDER BY a DESC, b DESC NULLS FIRST +} { + 3 {} 3 3 {} 1 3 2 12 + 2 {} 1 2 2 12 2 1 11 + 1 {} 1 1 2 12 1 1 11 +} +do_eqp_test 5.5 { + SELECT * FROM t4 WHERE a IN (1, 2, 3) ORDER BY a DESC, b DESC NULLS FIRST +} { + QUERY PLAN + `--SEARCH t4 USING INDEX t4ab (a=?) +} + +#------------------------------------------------------------------------- +# +do_execsql_test 6.0 { + CREATE TABLE t5(a, b, c); + WITH s(i) AS ( + VALUES(1) UNION ALL SELECT i+1 FROM s WHERE i<200 + ) + INSERT INTO t5 SELECT i%2, CASE WHEN (i%10)==0 THEN NULL ELSE i END, i FROM s; +} + +set res1 [db eval { SELECT a,b FROM t5 WHERE a=1 ORDER BY b NULLS LAST, c }] +set res2 [db eval { + SELECT a,b FROM t5 WHERE a=1 ORDER BY b DESC NULLS FIRST, c DESC +}] + +do_execsql_test 6.1.1 { + CREATE INDEX t5ab ON t5(a, b, c); + SELECT a,b FROM t5 WHERE a=1 ORDER BY b NULLS LAST, c; +} $res1 +do_eqp_test 6.1.2 { + SELECT a,b FROM t5 WHERE a=1 ORDER BY b NULLS LAST, c; +} { + QUERY PLAN + `--SEARCH t5 USING COVERING INDEX t5ab (a=?) +} +do_execsql_test 6.2.1 { + SELECT a,b FROM t5 WHERE a=1 ORDER BY b DESC NULLS FIRST, c DESC +} $res2 +do_eqp_test 6.2.2 { + SELECT a,b FROM t5 WHERE a=1 ORDER BY b DESC NULLS FIRST, c DESC +} { + QUERY PLAN + `--SEARCH t5 USING COVERING INDEX t5ab (a=?) +} + +#------------------------------------------------------------------------- +do_execsql_test 7.0 { + CREATE TABLE t71(a, b, c); + CREATE INDEX t71abc ON t71(a, b, c); + + SELECT * FROM t71 WHERE a=1 AND b=2 ORDER BY c NULLS LAST; + SELECT * FROM t71 WHERE a=1 AND b=2 ORDER BY c DESC NULLS FIRST; + + SELECT * FROM t71 ORDER BY a NULLS LAST; + SELECT * FROM t71 ORDER BY a DESC NULLS FIRST; +} + +# 2019-12-18 gramfuzz1 find +# NULLS LAST not allows on an INTEGER PRIMARY KEY. +# +do_catchsql_test 8.0 { + CREATE TABLE t80(a, b INTEGER, PRIMARY KEY(b NULLS LAST)) WITHOUT ROWID; +} {1 {unsupported use of NULLS LAST}} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 9.0 { + CREATE TABLE v0 (c1, c2, c3); + CREATE INDEX v3 ON v0 (c1, c2, c3); +} +do_execsql_test 9.1 { + ANALYZE sqlite_master; + INSERT INTO sqlite_stat1 VALUES('v0','v3','648 324 81'); + ANALYZE sqlite_master; +} + +do_execsql_test 9.2 { + INSERT INTO v0 VALUES + (1, 10, 'b'), + (1, 10, 'd'), + (1, 10, NULL), + (2, 10, 'a'), + (2, 10, NULL), + (1, 10, 'c'), + (2, 10, 'b'), + (1, 10, 'a'), + (1, 10, NULL), + (2, 10, NULL), + (2, 10, 'd'), + (2, 10, 'c'); +} + +do_execsql_test 9.3 { + SELECT c1, c2, ifnull(c3, 'NULL') FROM v0 + WHERE c2=10 ORDER BY c1, c3 NULLS LAST +} { + 1 10 a 1 10 b 1 10 c 1 10 d 1 10 NULL 1 10 NULL + 2 10 a 2 10 b 2 10 c 2 10 d 2 10 NULL 2 10 NULL +} + +do_eqp_test 9.4 { + SELECT c1, c2, ifnull(c3, 'NULL') FROM v0 + WHERE c2=10 ORDER BY c1, c3 NULLS LAST +} {SEARCH v0 USING COVERING INDEX v3 (ANY(c1) AND c2=?)} + + +# 2020-03-01 ticket e12a0ae526bb51c7 +# NULLS LAST on a LEFT JOIN +# +reset_db +do_execsql_test 10.10 { + CREATE TABLE t1(x); + INSERT INTO t1(x) VALUES('X'); + CREATE TABLE t2(c, d); + CREATE INDEX t2dc ON t2(d, c); + SELECT c FROM t1 LEFT JOIN t2 ON d=NULL ORDER BY d, c NULLS LAST; +} {{}} +do_execsql_test 10.20 { + INSERT INTO t2(c,d) VALUES(5,'X'),(6,'Y'),(7,'Z'),(3,'A'),(4,'B'); + SELECT c FROM t1 LEFT JOIN t2 ON d=x ORDER BY d, c NULLS LAST; +} {5} +do_execsql_test 10.30 { + UPDATE t2 SET d='X'; + UPDATE t2 SET c=NULL WHERE c=6; + SELECT c FROM t1 LEFT JOIN t2 ON d=x ORDER BY d NULLS FIRST, c NULLS FIRST; +} {{} 3 4 5 7} +do_execsql_test 10.40 { + SELECT c FROM t1 LEFT JOIN t2 ON d=x ORDER BY d NULLS LAST, c NULLS LAST; +} {3 4 5 7 {}} +do_execsql_test 10.41 { + SELECT c FROM t1 LEFT JOIN t2 ON d=x ORDER BY c NULLS LAST; +} {3 4 5 7 {}} +do_execsql_test 10.42 { + SELECT c FROM t1 LEFT JOIN t2 ON d=x ORDER BY +d NULLS LAST, +c NULLS LAST; +} {3 4 5 7 {}} +do_execsql_test 10.50 { + INSERT INTO t1(x) VALUES(NULL),('Y'); + SELECT x, c, d, '|' FROM t1 LEFT JOIN t2 ON d=x + ORDER BY d NULLS LAST, c NULLS LAST; +} {X 3 X | X 4 X | X 5 X | X 7 X | X {} X | {} {} {} | Y {} {} |} +do_execsql_test 10.51 { + SELECT x, c, d, '|' FROM t1 LEFT JOIN t2 ON d=x + ORDER BY +d NULLS LAST, +c NULLS LAST; +} {X 3 X | X 4 X | X 5 X | X 7 X | X {} X | {} {} {} | Y {} {} |} + + + + + +finish_test diff --git a/test/offset1.test b/test/offset1.test index 91dc0b00a1..5b04bd836c 100644 --- a/test/offset1.test +++ b/test/offset1.test @@ -156,6 +156,47 @@ do_execsql_test offset1-1.4.9 { LIMIT 9 OFFSET 1; } {2 b 3 c 4 d 5 e 6 w 7 x 8 y 9 z} - +# 2022-08-04 +# https://sqlite.org/forum/forumpost/6b5e9188f0657616 +# +do_execsql_test offset1-2.0 { + CREATE TABLE employees ( + id integer primary key, + name text, + city text, + department text, + salary integer + ); + INSERT INTO employees VALUES + (11,'Diane','London','hr',70), + (12,'Bob','London','hr',78), + (21,'Emma','London','it',84), + (22,'Grace','Berlin','it',90), + (23,'Henry','London','it',104), + (24,'Irene','Berlin','it',104), + (25,'Frank','Berlin','it',120), + (31,'Cindy','Berlin','sales',96), + (32,'Dave','London','sales',96), + (33,'Alice','Berlin','sales',100); + CREATE VIEW v AS + SELECT * FROM ( + SELECT * FROM employees + WHERE salary < 100 + ORDER BY salary desc) + UNION ALL + SELECT * FROM ( + SELECT * FROM employees + WHERE salary >= 100 + ORDER BY salary asc); +} {} +do_execsql_test offset1-2.1 { + SELECT * FROM v LIMIT 5 OFFSET 2; +} { + 22 Grace Berlin it 90 + 21 Emma London it 84 + 12 Bob London hr 78 + 11 Diane London hr 70 + 33 Alice Berlin sales 100 +} finish_test diff --git a/test/optfuzz-db01.c b/test/optfuzz-db01.c new file mode 100644 index 0000000000..e11f15cc2e --- /dev/null +++ b/test/optfuzz-db01.c @@ -0,0 +1,947 @@ +/* content of file testdb01.db */ +unsigned char data001[] = { + 83, 81, 76,105,116,101, 32,102,111,114,109, 97,116, 32, 51, 0, 2, 0, 1, + 1, 0, 64, 32, 32, 0, 0, 0, 2, 0, 0, 0, 35, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 31, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 2, 0, 46, 32,152, 5, 0, 0, 0, 7, 1,221, 0, 0, 0, 0, 35, 1,251, + 1,246, 1,241, 1,236, 1,231, 1,226, 1,221, 84, 4, 7, 23, 17, 17, 1, + 129, 19,116, 97, 98,108,101,116, 52,116, 52, 5, 67, 82, 69, 65, 84, 69, 32, + 84, 65, 66, 76, 69, 32,116, 52, 40, 97, 32, 73, 78, 84, 32, 85, 78, 73, 81, + 85, 69, 32, 78, 79, 84, 32, 78, 85, 76, 76, 44, 32, 98, 32, 73, 78, 84, 32, + 85, 78, 73, 81, 85, 69, 32, 78, 79, 84, 32, 78, 85, 76, 76, 44, 99, 44,100, + 44,101, 41, 35, 6, 6, 23, 55, 17, 1, 0,105,110,100,101,120,115,113,108, + 105,116,101, 95, 97,117,116,111,105,110,100,101,120, 95,116, 52, 95, 50,116, + 52, 7, 35, 5, 6, 23, 55, 17, 1, 0,105,110,100,101,120,115,113,108,105, + 116,101, 95, 97,117,116,111,105,110,100,101,120, 95,116, 52, 95, 49,116, 52, + 6, 42, 3, 6, 23, 17, 17, 1, 65,116, 97, 98,108,101,116, 51,116, 51, 4, + 67, 82, 69, 65, 84, 69, 32, 84, 65, 66, 76, 69, 32,116, 51, 40, 97, 44, 98, + 44, 99, 44,100, 44,101, 41, 95, 2, 7, 23, 17, 17, 1,129, 41,116, 97, 98, + 108,101,116, 50,116, 50, 3, 67, 82, 69, 65, 84, 69, 32, 84, 65, 66, 76, 69, + 32,116, 50, 40, 97, 32, 73, 78, 84, 44, 32, 98, 32, 73, 78, 84, 44, 32, 99, + 32, 73, 78, 84, 44,100, 32, 73, 78, 84, 44,101, 32, 73, 78, 84, 44, 80, 82, + 73, 77, 65, 82, 89, 32, 75, 69, 89, 40, 98, 44, 97, 41, 41, 87, 73, 84, 72, + 79, 85, 84, 32, 82, 79, 87, 73, 68, 83, 1, 7, 23, 17, 17, 1,129, 17,116, + 97, 98,108,101,116, 49,116, 49, 2, 67, 82, 69, 65, 84, 69, 32, 84, 65, 66, + 76, 69, 32,116, 49, 40, 97, 32, 73, 78, 84, 69, 71, 69, 82, 32, 80, 82, 73, + 77, 65, 0, 0, 0, 34, 32, 0, 0, 0, 33, 29, 0, 0, 0, 32, 26, 0, 0, + 0, 31, 23, 0, 0, 0, 30, 19, 0, 0, 0, 11, 14, 0, 0, 0, 9, 7, 5, + 0, 0, 0, 1, 1,251, 0, 0, 0, 0, 16, 1,251, 1,195, 1,180, 1,166, + 1,151, 1,136, 1,121, 1,105, 1, 91, 1, 76, 1, 61, 1, 46, 1, 29, 1, + 14, 0,252, 0,238, 0,224, 0,209, 0,194, 0,177, 0,157, 0,143, 0,128, + 0,110, 0, 94, 0, 78, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 14, 28, 6, 0, 1, 1, 1, 23, 17, 67, 31,119,111,114,107,115, 14, 27, + 6, 0, 1, 1, 1, 23, 22, 71, 3, 97,110,103,101,108, 16, 26, 6, 0, 1, + 1, 1, 27, 40, 98, 17,109,111,114,110,105,110,103, 13, 25, 6, 0, 1, 1, + 1, 21, 10, 7, 19,103,111,110,101, 12, 24, 6, 0, 1, 1, 9, 21, 43, 46, + 119, 97,121,115, 18, 23, 6, 0, 1, 1, 1, 31, 6, 37, 31,115, 97, 99,114, + 105,102,105, 99,101, 15, 22, 6, 0, 1, 1, 1, 25, 45, 71, 28,116,104,111, + 117,103,104, 13, 21, 6, 0, 1, 1, 1, 21, 22, 92, 18,115,111,109,101, 13, + 20, 6, 0, 9, 1, 1, 23, 2, 45, 97, 98,111,118,101, 12, 19, 6, 0, 1, + 1, 8, 21, 4, 58,119, 97,121,115, 12, 18, 6, 0, 1, 1, 1, 19, 44, 19, + 43,119, 97,114, 16, 17, 6, 0, 1, 1, 1, 27, 29, 74, 36, 98,101,116,119, + 101,101,110, 13, 16, 6, 0, 1, 1, 1, 21, 44, 52, 19,112,111,111,114, 15, + 15, 6, 0, 1, 1, 1, 25, 6, 3, 11,116,101,109,112,108,101, 13, 14, 6, + 0, 1, 1, 1, 21, 35, 48, 27,100,105,101,100, 13, 13, 6, 0, 1, 1, 1, + 21, 4, 21, 39,100,111,116,104, 13, 12, 6, 0, 1, 1, 1, 21, 4, 38, 36, + 115,101,110,100, 12, 11, 6, 0, 1, 1, 1, 19, 13, 48, 22,115,105,120, 14, + 10, 6, 0, 1, 1, 1, 23, 41, 89, 14,115,101,114,118,101, 13, 9, 6, 0, + 8, 1, 1, 23, 16, 50, 98,101,103, 97,116, 13, 8, 6, 0, 1, 1, 1, 21, + 42, 49, 34,115,101,110,100, 13, 7, 6, 0, 1, 1, 1, 21, 21, 91, 38,110, + 101, 97,114, 12, 6, 6, 0, 1, 1, 1, 19, 2, 37, 11, 99, 97,110, 13, 5, + 6, 0, 1, 1, 1, 21, 25, 27, 28,103,111,110,101, 13, 4, 6, 0, 1, 1, + 1, 21, 41, 32, 35,110,101, 97,114, 14, 3, 6, 0, 1, 1, 1, 23, 32, 24, + 26,115,101,114,118,101, 13, 2, 6, 0, 1, 1, 1, 21, 45, 14, 39,115, 97, + 118,101, 13, 1, 6, 0, 1, 1, 1, 21, 40, 68, 0, 0, 0, 15, 28, 2, 0, + 0, 0, 1, 1,238, 0, 0, 0, 0, 22, 1,238, 1,197, 1,181, 1,166, 1, + 151, 1,137, 1,121, 1,104, 1, 84, 1, 73, 1, 59, 1, 41, 1, 26, 1, 11, + 0,253, 0,238, 0,223, 0,207, 0,191, 0,175, 0,159, 0,144, 0,129, 0, + 113, 0, 97, 0, 82, 0, 68, 0, 0, 13, 6, 1, 1, 1, 1, 19, 26, 34, 15, + 20, 97,114,107, 14, 6, 1, 1, 1, 1, 21, 25, 5, 27, 28,103,111,110,101, + 15, 6, 1, 1, 1, 1, 23, 22, 47, 16, 40, 97,110,103,101,114, 15, 6, 1, + 1, 1, 1, 23, 22, 27, 71, 3, 97,110,103,101,108, 14, 6, 1, 1, 1, 1, + 21, 22, 21, 92, 18,115,111,109,101, 14, 6, 1, 1, 1, 1, 21, 21, 7, 91, + 38,110,101, 97,114, 15, 6, 1, 1, 1, 1, 23, 20, 42, 18, 5, 98,101,103, + 97,116, 15, 6, 1, 1, 1, 1, 23, 17, 37, 66, 18,100,119,101,108,116, 15, + 6, 1, 1, 1, 1, 23, 17, 28, 67, 31,119,111,114,107,115, 15, 6, 1, 1, + 1, 8, 25, 16, 32, 7,112,108, 97, 99,101,115, 14, 6, 1, 1, 1, 1, 21, + 16, 30, 81, 25,119, 97,108,107, 14, 6, 1, 1, 1, 1, 21, 14, 40, 30, 26, + 115,101,110,100, 13, 6, 1, 1, 1, 1, 19, 13, 11, 48, 22,115,105,120, 14, + 6, 1, 1, 1, 1, 21, 10, 38, 97, 34,115,104,101,119, 14, 6, 1, 1, 1, + 1, 21, 10, 25, 7, 19,103,111,110,101, 17, 6, 1, 1, 1, 1, 27, 9, 50, + 92, 29,116,104,101,114,101,105,110, 13, 6, 1, 1, 1, 1, 19, 9, 49, 51, + 38,111,105,108, 10, 6, 1, 1, 1, 1, 0, 7, 33, 72, 31, 19, 6, 1, 1, + 1, 1, 31, 6, 23, 37, 31,115, 97, 99,114,105,102,105, 99,101, 16, 6, 1, + 1, 1, 1, 25, 6, 15, 3, 11,116,101,109,112,108,101, 15, 6, 1, 1, 1, + 1, 23, 5, 43, 23, 41, 98,101,103, 97,116, 13, 6, 1, 1, 1, 8, 21, 4, + 19, 58,119, 97,121,115, 14, 6, 1, 1, 1, 1, 21, 4, 13, 21, 39,100,111, + 116,104, 14, 6, 1, 1, 1, 1, 21, 4, 12, 38, 36,115,101,110,100, 15, 6, + 1, 1, 1, 1, 23, 3, 39, 21, 45, 98,101,103, 97,116, 13, 6, 1, 1, 1, + 1, 19, 2, 6, 37, 11, 99, 97,110, 14, 6, 9, 1, 1, 1, 23, 20, 2, 45, + 97, 98,111,118,101, 14, 6, 8, 1, 1, 1, 23, 36, 52, 17, 99,104, 0, 0, + 0, 21, 13, 6, 1, 1, 1, 1, 19, 26, 34, 15, 20, 97,114,107, 13, 0, 0, + 0, 35, 0, 92, 0, 1,244, 1,232, 1,216, 1,204, 1,186, 1,171, 1,160, + 1,149, 1,138, 1,128, 1,117, 1,106, 1, 92, 1, 76, 1, 65, 1, 49, 1, + 32, 1, 21, 1, 10, 0,255, 0,241, 0,225, 0,214, 0,203, 0,192, 0,180, + 0,168, 0,156, 0,144, 0,132, 0,124, 0,116, 0,108, 0,100, 0, 92, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 35, 6, 0, 0, 0, + 0, 0, 6, 34, 6, 0, 0, 0, 0, 0, 6, 33, 6, 0, 0, 0, 0, 0, 6, + 32, 6, 0, 0, 0, 0, 0, 6, 31, 6, 0, 0, 0, 0, 0, 10, 30, 6, 1, + 1, 1, 1, 0, 48, 37, 93, 7, 10, 29, 6, 1, 1, 1, 1, 0, 28, 17, 67, + 31, 10, 28, 6, 1, 1, 1, 1, 0, 22, 45, 71, 28, 10, 27, 6, 1, 1, 1, + 1, 0, 12, 4, 38, 36, 10, 26, 6, 1, 1, 1, 1, 0, 49, 9, 51, 38, 9, + 25, 6, 1, 1, 1, 0, 0, 17, 29, 74, 9, 24, 6, 1, 1, 1, 0, 0, 47, + 22, 16, 9, 23, 6, 1, 1, 1, 0, 0, 32, 16, 7, 14, 22, 6, 1, 1, 1, + 0, 23, 42, 20, 18, 98,101,103, 97,116, 12, 21, 6, 1, 1, 1, 0, 19, 34, + 26, 15, 97,114,107, 9, 20, 6, 1, 1, 0, 1, 0, 49, 9, 38, 9, 19, 6, + 1, 1, 0, 1, 0, 44, 48, 9, 9, 18, 6, 1, 1, 0, 1, 0, 21, 22, 18, + 15, 17, 6, 1, 1, 0, 1, 25, 35, 38, 22, 99,117, 98,105,116,115, 14, 16, + 6, 1, 1, 0, 1, 23, 37, 17, 18,100,119,101,108,116, 9, 15, 6, 1, 0, + 1, 1, 0, 49, 51, 38, 14, 14, 6, 1, 0, 1, 1, 23, 10, 89, 14,115,101, + 114,118,101, 12, 13, 6, 9, 0, 1, 1, 21, 68, 32,100,111,116,104, 9, 12, + 6, 1, 0, 1, 1, 0, 47, 16, 40, 9, 11, 6, 1, 0, 1, 1, 0, 25, 7, + 19, 8, 10, 6, 0, 1, 1, 8, 0, 16, 7, 9, 9, 6, 0, 1, 1, 1, 0, + 16, 81, 25, 9, 8, 6, 0, 1, 1, 1, 0, 7, 72, 31, 9, 7, 6, 0, 1, + 1, 1, 0, 6, 37, 31, 13, 6, 6, 0, 1, 1, 1, 21, 21, 91, 38,110,101, + 97,114, 16, 5, 6, 1, 1, 1, 1, 25, 15, 6, 3, 11,116,101,109,112,108, + 101, 10, 4, 6, 1, 1, 1, 1, 0, 21, 22, 92, 18, 14, 3, 6, 1, 1, 1, + 1, 21, 4, 41, 32, 35,110,101, 97,114, 10, 2, 6, 1, 1, 1, 1, 0, 46, + 28, 88, 22, 10, 1, 6, 1, 1, 1, 1, 0, 17, 29, 74, 36, 13, 0, 0, 0, + 15, 1, 71, 0, 1,243, 1,230, 1,217, 1,204, 1,191, 1,179, 1,167, 1, + 155, 1,143, 1,131, 1,119, 1,107, 1, 95, 1, 83, 1, 71, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 10, 15, 6, 1, 1, 1, 1, 0, 48, 37, 93, 7, 10, 14, 6, 1, 1, 1, 1, + 0, 22, 45, 71, 28, 10, 13, 6, 1, 1, 1, 1, 0, 12, 4, 38, 36, 10, 12, + 6, 1, 1, 1, 0, 1, 32, 16, 7, 79, 10, 11, 6, 1, 1, 1, 0, 1, 42, + 20, 18, 19, 10, 10, 6, 1, 1, 1, 0, 1, 34, 26, 15, 13, 10, 9, 6, 1, + 1, 0, 1, 1, 49, 9, 38, 97, 10, 8, 6, 1, 1, 0, 1, 1, 44, 48, 9, + 90, 10, 7, 6, 1, 1, 0, 1, 1, 35, 38, 22, 33, 10, 6, 6, 1, 1, 0, + 1, 1, 37, 17, 18, 18, 11, 5, 6, 1, 1, 1, 1, 1, 15, 6, 3, 11, 43, + 11, 4, 6, 1, 1, 1, 1, 1, 21, 22, 92, 18, 62, 11, 3, 6, 1, 1, 1, + 1, 1, 4, 41, 32, 35, 36, 11, 2, 6, 1, 1, 1, 1, 1, 46, 28, 88, 22, + 77, 11, 1, 6, 1, 1, 1, 1, 1, 17, 29, 74, 36, 61, 10, 0, 0, 0, 15, + 1,167, 0, 1,250, 1,244, 1,238, 1,233, 1,227, 1,221, 1,215, 1,209, + 1,203, 1,197, 1,191, 1,185, 1,179, 1,173, 1,167, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 5, 3, 1, 1, 49, 9, 5, 3, 1, 1, 48, 15, 5, 3, 1, 1, 46, 2, 5, + 3, 1, 1, 44, 8, 5, 3, 1, 1, 42, 11, 5, 3, 1, 1, 37, 6, 5, 3, + 1, 1, 35, 7, 5, 3, 1, 1, 34, 10, 5, 3, 1, 1, 32, 12, 5, 3, 1, + 1, 22, 14, 5, 3, 1, 1, 21, 4, 4, 3, 1, 9, 17, 5, 3, 1, 1, 15, + 5, 5, 3, 1, 1, 12, 13, 5, 3, 1, 1, 4, 3, 10, 0, 0, 0, 15, 1, + 167, 0, 1,250, 1,244, 1,238, 1,232, 1,226, 1,220, 1,214, 1,208, 1, + 202, 1,197, 1,191, 1,185, 1,179, 1,173, 1,167, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, + 3, 1, 1, 48, 8, 5, 3, 1, 1, 45, 14, 5, 3, 1, 1, 41, 3, 5, 3, + 1, 1, 38, 7, 5, 3, 1, 1, 37, 15, 4, 3, 1, 9, 29, 5, 3, 1, 1, + 28, 2, 5, 3, 1, 1, 26, 10, 5, 3, 1, 1, 22, 4, 5, 3, 1, 1, 20, + 11, 5, 3, 1, 1, 17, 6, 5, 3, 1, 1, 16, 12, 5, 3, 1, 1, 9, 9, + 5, 3, 1, 1, 6, 5, 5, 3, 1, 1, 4, 13, 5, 0, 0, 0, 2, 1,246, + 0, 0, 0, 0, 27, 1,251, 1,246, 1,168, 1,148, 1,130, 1,107, 1, 86, + 1, 65, 1, 44, 1, 27, 1, 14, 0,250, 0,224, 0,205, 0,184, 0,165, 0, + 145, 0,123, 0,106, 0, 86, 0, 67, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 17, 23, 6, 0, 23, 1, 1, 21,107,110,111,119,110, 52, 19,112, + 111,111,114, 18, 22, 6, 0, 23, 1, 1, 23, 97, 98,111,118,101, 24, 26,115, + 101,114,118,101, 15, 21, 6, 0, 19, 1, 1, 21,119, 97,114, 52, 19,112,111, + 111,114, 20, 20, 6, 0, 27, 1, 8, 25,110,111,116,104,105,110,103, 7,112, + 108, 97, 99,101,115, 18, 19, 6, 0, 23, 1, 1, 23, 98,101,103, 97,116, 90, + 27,116,114,117,116,104, 17, 18, 6, 0, 23, 1, 1, 21,100,119,101,108,116, + 21, 39,100,111,116,104, 19, 17, 6, 0, 27, 1, 1, 21,109,111,114,110,105, + 110,103, 52, 19,112,111,111,114, 17, 16, 6, 0, 21, 1, 1, 23,115,104,101, + 119, 90, 27,116,114,117,116,104, 24, 15, 6, 0, 27, 1, 1, 31,116,104,101, + 114,101,105,110, 37, 31,115, 97, 99,114,105,102,105, 99,101, 18, 14, 6, 0, + 23, 1, 8, 25,115,109,111,116,101, 7,112,108, 97, 99,101,115, 11, 13, 6, + 0, 19, 1, 1, 0, 97,114,107, 72, 31, 15, 12, 6, 0, 21, 1, 8, 21,119, + 105,110,101, 58,119, 97,121,115, 19, 11, 6, 0, 21, 1, 1, 27,115,111,109, + 101, 98, 17,109,111,114,110,105,110,103, 19, 10, 6, 0, 27, 1, 1, 21, 98, + 101,116,119,101,101,110, 92, 18,115,111,109,101, 19, 9, 6, 0, 21, 1, 1, + 27,115, 97,118,101, 74, 36, 98,101,116,119,101,101,110, 21, 8, 6, 0, 25, + 1, 1, 27,116,104,111,117,103,104, 98, 17,109,111,114,110,105,110,103, 16, + 7, 6, 0, 21, 1, 1, 21,115,101,110,100, 49, 34,115,101,110,100, 18, 6, + 6, 0, 25, 1, 1, 21,119,105,115,100,111,109, 38, 36,115,101,110,100, 16, + 5, 6, 0, 23, 1, 9, 21, 97,110,103,101,114, 46,119, 97,121,115, 14, 4, + 6, 0, 19, 1, 1, 19, 99, 97,110, 19, 43,119, 97,114, 16, 3, 6, 0, 23, + 1, 1, 19,111,102,102,101,114, 48, 22,115,105,120, 16, 2, 6, 0, 23, 1, + 8, 21,119,111,114,107,115, 58,119, 97,121,115, 16, 1, 6, 0, 23, 1, 1, + 19, 0, 0, 0, 26, 45, 0, 0, 0, 25, 23, 13, 0, 0, 0, 7, 0, 48, 0, + 1,171, 1, 74, 1, 30, 0,126, 0,249, 0,212, 0, 48, 0, 81, 0, 0, 84, + 4, 7, 23, 17, 17, 1,129, 19,116, 97, 98,108,101,116, 52,116, 52, 5, 67, + 82, 69, 76, 7, 7, 23, 17, 17, 1,129, 3,116, 97, 98,108,101,116, 53,116, + 53, 8, 67, 82, 69, 65, 84, 69, 32, 84, 65, 66, 76, 69, 32,116, 53, 40, 97, + 32, 73, 78, 84, 69, 71, 69, 82, 32, 80, 82, 73, 77, 65, 82, 89, 32, 75, 69, + 89, 44, 32, 98, 32, 84, 69, 88, 84, 32, 85, 78, 73, 81, 85, 69, 44, 99, 44, + 100, 44,101, 41, 84, 4, 7, 23, 17, 17, 1,129, 19,116, 97, 98,108,101,116, + 52,116, 52, 5, 67, 82, 69, 65, 84, 69, 32, 84, 65, 66, 76, 69, 32,116, 52, + 40, 97, 32, 73, 78, 84, 32, 85, 78, 73, 81, 85, 69, 32, 78, 79, 84, 32, 78, + 85, 76, 76, 44, 32, 98, 32, 73, 78, 84, 32, 85, 78, 73, 81, 85, 69, 32, 78, + 79, 84, 32, 78, 85, 76, 76, 44, 99, 44,100, 44,101, 41, 35, 6, 6, 23, 55, + 17, 1, 0,105,110,100,101,120,115,113,108,105,116,101, 95, 97,117,116,111, + 105,110,100,101,120, 95,116, 52, 95, 50,116, 52, 7, 35, 5, 6, 23, 55, 17, + 1, 0,105,110,100,101,120,115,113,108,105,116,101, 95, 97,117,116,111,105, + 110,100,101,120, 95,116, 52, 95, 49,116, 52, 6, 42, 3, 6, 23, 17, 17, 1, + 65,116, 97, 98,108,101,116, 51,116, 51, 4, 67, 82, 69, 65, 84, 69, 32, 84, + 65, 66, 76, 69, 32,116, 51, 40, 97, 44, 98, 44, 99, 44,100, 44,101, 41, 95, + 2, 7, 23, 17, 17, 1,129, 41,116, 97, 98,108,101,116, 50,116, 50, 3, 67, + 82, 69, 65, 84, 69, 32, 84, 65, 66, 76, 69, 32,116, 50, 40, 97, 32, 73, 78, + 84, 44, 32, 98, 32, 73, 78, 84, 44, 32, 99, 32, 73, 78, 84, 44,100, 32, 73, + 78, 84, 44,101, 32, 73, 78, 84, 44, 80, 82, 73, 77, 65, 82, 89, 32, 75, 69, + 89, 40, 98, 44, 97, 41, 41, 87, 73, 84, 72, 79, 85, 84, 32, 82, 79, 87, 73, + 68, 83, 1, 7, 23, 17, 17, 1,129, 17,116, 97, 98,108,101,116, 49,116, 49, + 2, 67, 82, 69, 65, 84, 69, 32, 84, 65, 66, 76, 69, 32,116, 49, 40, 97, 32, + 73, 78, 84, 69, 71, 69, 82, 32, 80, 82, 73, 77, 65, 82, 89, 32, 75, 69, 89, + 44, 32, 98, 32, 73, 78, 84, 44, 32, 99, 32, 73, 78, 84, 44, 32,100, 32, 73, + 78, 84, 44, 32,101, 32, 73, 78, 84, 41, 2, 0, 0, 0, 1, 1,243, 0, 0, + 0, 0, 29, 1,243, 1,218, 1,209, 1,199, 1,187, 1,179, 1,169, 1,158, + 1,145, 1,136, 1,127, 1,117, 1,107, 1, 98, 1, 82, 1, 72, 1, 63, 1, + 51, 1, 42, 1, 30, 1, 20, 1, 12, 1, 3, 0,248, 0,239, 0,225, 0,216, + 0,207, 0,197, 0,188, 0,180, 0,170, 0,161, 0,152, 0,141, 0,129, 0, + 118, 0,106, 0, 97, 0, 0, 0, 0, 0, 0, 0, 8, 3, 21, 1,116,114,101, + 101, 49, 11, 3, 27, 1,116,104,121,115,101,108,102, 27, 10, 3, 25, 1,116, + 104,111,117,103,104, 8, 11, 3, 27, 1,116,104,101,114,101,105,110, 15, 10, + 3, 25, 1,116,101,109,112,108,101, 43, 8, 3, 21, 1,116,101,108,108, 25, + 8, 3, 21, 1,115,111,109,101, 11, 9, 3, 23, 1,115,109,111,116,101, 14, + 7, 3, 19, 1,115,105,120, 48, 8, 3, 21, 1,115,104,101,119, 16, 9, 3, + 23, 1,115,101,114,118,101, 37, 8, 3, 21, 1,115,101,110,100, 7, 8, 3, + 21, 1,115, 97,118,101, 9, 13, 3, 31, 1,115, 97, 99,114,105,102,105, 99, + 101, 24, 8, 3, 21, 1,112,111,111,114, 40, 10, 3, 25, 1,112,108, 97, 99, + 101,115, 28, 8, 3, 21, 1,112, 97,114,116, 30, 7, 3, 19, 1,111,105,108, + 46, 9, 3, 23, 1,111,102,102,101,114, 3, 11, 3, 27, 1,110,111,116,104, + 105,110,103, 20, 8, 3, 21, 1,110,101, 97,114, 36, 11, 3, 27, 1,109,111, + 114,110,105,110,103, 17, 8, 3, 21, 1,108,111,110,103, 35, 9, 3, 23, 1, + 107,110,111,119,110, 23, 15, 3, 35, 1,105,110,104, 97, 98,105,116, 97,110, + 116,115, 45, 8, 3, 21, 1,103,111,110,101, 32, 9, 3, 23, 1,102,114,117, + 105,116, 38, 9, 3, 23, 1,100,119,101,108,116, 18, 8, 3, 21, 1,100,111, + 116,104, 39, 8, 3, 21, 1,100,105,101,100, 47, 12, 3, 29, 1,100,101,112, + 97,114,116,101,100, 26, 10, 3, 25, 1, 99,117, 98,105,116,115, 33, 9, 3, + 23, 1, 99,104,105,108,100, 42, 7, 3, 19, 1, 99, 97,110, 4, 11, 3, 27, + 1, 98,101,116,119,101,101,110, 10, 9, 3, 23, 1, 98,101,103, 97,116, 19, + 8, 3, 21, 1, 98,101, 97,114, 29, 7, 3, 19, 1, 97,114,107, 13, 9, 3, + 23, 1, 97,110,103,101,114, 5, 9, 3, 23, 1, 97,110,103, 0, 0, 0, 28, + 8, 3, 21, 1,116,114,101,101, 49, 13, 1,104, 0, 7, 0, 24, 0, 1, 67, + 1, 13, 0,225, 0,177, 0,109, 1,171, 0, 24, 0, 0, 83, 14, 7, 21, 19, + 19, 8,129, 17,118,105,101,119,118, 50, 48,118, 50, 48, 67, 82, 69, 65, 84, + 69, 32, 86, 73, 69, 87, 32,118, 50, 48, 40, 97, 44, 98, 44, 99, 44,100, 44, + 101, 41, 32, 65, 83, 32, 83, 69, 76, 69, 67, 84, 32, 97, 44, 98, 44, 99, 44, + 100, 44,101, 32, 70, 82, 79, 77, 32,116, 50, 32, 87, 72, 69, 82, 69, 32, 97, + 60, 62, 50, 53, 66, 12, 6, 21, 19, 19, 8,113,118,105,101,119,118, 48, 48, + 118, 48, 48, 67, 82, 69, 65, 84, 69, 32, 86, 73, 69, 87, 32,118, 48, 48, 40, + 97, 44, 98, 44, 99, 44,100, 44,101, 41, 32, 65, 83, 32, 83, 69, 76, 69, 67, + 84, 32, 49, 44, 49, 44, 49, 44, 49, 44, 39,111,110,101, 39, 46, 11, 6, 23, + 21, 17, 1, 69,105,110,100,101,120,116, 50,101,100,116, 50, 14, 67, 82, 69, + 65, 84, 69, 32, 73, 78, 68, 69, 88, 32,116, 50,101,100, 32, 79, 78, 32,116, + 50, 40,101, 44,100, 41, 42, 10, 6, 23, 19, 17, 1, 63,105,110,100,101,120, + 116, 49,101,116, 49, 13, 67, 82, 69, 65, 84, 69, 32, 73, 78, 68, 69, 88, 32, + 116, 49,101, 32, 79, 78, 32,116, 49, 40,101, 41, 52, 9, 6, 23, 21, 17, 1, + 81,105,110,100,101,120,116, 51,120, 49,116, 51, 12, 67, 82, 69, 65, 84, 69, + 32, 73, 78, 68, 69, 88, 32,116, 51,120, 49, 32, 79, 78, 32,116, 51, 40, 97, + 44, 98, 44, 99, 44,100, 44,101, 41, 35, 8, 6, 23, 55, 17, 1, 0,105,110, + 100,101,120,115,113,108,105,116,101, 95, 97,117,116,111,105,110,100,101,120, + 95,116, 53, 95, 49,116, 53, 10, 0, 0, 0, 67, 17, 17, 1,129, 3,116, 97, + 98,108,101,116, 53,116, 53, 8, 67, 82, 69, 65, 84, 69, 32, 84, 65, 66, 76, + 69, 32,116, 53, 40, 97, 32, 73, 78, 84, 69, 71, 69, 82, 32, 80, 82, 73, 77, + 65, 82, 89, 32, 75, 69, 89, 44, 32, 98, 32, 84, 69, 88, 84, 32, 85, 78, 83, + 13, 7, 21, 19, 19, 8,129, 17,118,105,101,119,118, 49, 48,118, 49, 48, 67, + 82, 69, 65, 84, 69, 32, 86, 73, 69, 87, 32,118, 49, 48, 40, 97, 44, 98, 44, + 99, 44,100, 44,101, 41, 32, 65, 83, 32, 83, 69, 76, 69, 67, 84, 32, 97, 44, + 98, 44, 99, 44,100, 44,101, 32, 70, 82, 79, 77, 32,116, 49, 32, 87, 72, 69, + 82, 69, 32, 97, 60, 62, 50, 53, 2, 0, 0, 0, 1, 1,240, 0, 0, 0, 0, + 24, 1,240, 1,220, 1,211, 1,199, 1,187, 1,176, 1,164, 1,148, 1,133, + 1,116, 1, 99, 1, 86, 1, 67, 1, 55, 1, 43, 1, 31, 1, 18, 1, 5, 0, + 249, 0,236, 0,224, 0,209, 0,191, 0,174, 0,157, 0,145, 0,132, 0,120, + 0,108, 0, 95, 0, 83, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 7, 1, 0, + 1, 1, 0, 1, 49, 51, 38, 15, 12, 7, 1, 1, 1, 1, 0, 1, 48, 37, 93, + 7, 30, 11, 7, 1, 1, 1, 0, 0, 1, 47, 22, 16, 24, 11, 7, 1, 0, 1, + 1, 0, 1, 47, 16, 40, 12, 12, 7, 1, 1, 1, 1, 0, 1, 46, 28, 88, 22, + 2, 11, 7, 1, 1, 0, 1, 0, 1, 44, 48, 9, 19, 16, 7, 1, 1, 1, 0, + 23, 1, 42, 20, 18, 98,101,103, 97,116, 22, 16, 7, 1, 1, 0, 1, 23, 1, + 37, 17, 18,100,119,101,108,116, 16, 17, 7, 1, 1, 0, 1, 25, 1, 35, 38, + 22, 99,117, 98,105,116,115, 17, 14, 7, 1, 1, 1, 0, 19, 1, 34, 26, 15, + 97,114,107, 21, 11, 7, 1, 1, 1, 0, 0, 1, 32, 16, 7, 23, 12, 7, 1, + 1, 1, 1, 0, 1, 28, 17, 67, 31, 29, 11, 7, 1, 0, 1, 1, 0, 1, 25, + 7, 19, 11, 12, 7, 1, 1, 1, 1, 0, 1, 22, 45, 71, 28, 28, 12, 7, 1, + 1, 1, 1, 0, 1, 21, 22, 92, 18, 4, 11, 7, 1, 1, 0, 1, 0, 1, 21, + 22, 18, 18, 11, 7, 1, 1, 1, 1, 0, 9, 17, 29, 74, 36, 11, 7, 1, 1, + 1, 0, 0, 1, 17, 29, 74, 25, 18, 7, 1, 1, 1, 1, 25, 1, 15, 6, 3, + 11,116,101,109,112,108,101, 5, 12, 7, 1, 1, 1, 1, 0, 1, 12, 4, 38, + 36, 27, 16, 7, 1, 0, 1, 1, 23, 1, 10, 89, 14,115,101,114,118,101, 14, + 16, 7, 1, 1, 1, 1, 21, 1, 4, 41, 32, 35,110,101, 97,114, 3, 14, 7, + 9, 0, 1, 1, 21, 1, 68, 32,100,111,116,104, 13, 15, 7, 0, 1, 1, 1, + 21, 1, 21, 91, 38,110,101, 97,114, 6, 11, 7, 0, 1, 1, 1, 0, 1, 16, + 81, 25, 9, 10, 7, 0, 1, 1, 8, 0, 1, 16, 7, 10, 11, 7, 0, 1, 1, + 1, 0, 1, 7, 72, 31, 8, 11, 7, 0, 1, 1, 1, 0, 1, 6, 37, 31, 7, + 8, 7, 0, 0, 0, 0, 0, 1, 35, 8, 7, 0, 0, 0, 0, 0, 1, 34, 8, + 7, 0, 0, 0, 0, 0, 1, 33, 8, 7, 0, 0, 0, 23, 11, 7, 1, 0, 1, + 1, 0, 1, 49, 51, 38, 15, 2, 0, 0, 0, 1, 1,241, 0, 0, 0, 0, 18, + 1,241, 1,221, 1,211, 1,203, 1,193, 1,183, 1,173, 1,163, 1,151, 1, + 143, 1,133, 1,122, 1,109, 1,100, 1, 92, 1, 83, 1, 74, 1, 64, 1, 55, + 1, 46, 1, 34, 1, 22, 1, 13, 1, 4, 0,252, 0,241, 0,232, 0,218, 0, + 209, 0,200, 0,191, 0,182, 0,173, 0,163, 0,153, 0,144, 0,136, 0,127, + 0,116, 0,105, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 3, + 25, 1,116,101,109,112,108,101, 48, 10, 3, 25, 1,116,101,109,112,108,101, + 15, 8, 3, 21, 1,115,111,109,101, 21, 7, 3, 19, 1,115,105,120, 11, 8, + 3, 21, 1,115,104,101,119, 38, 9, 3, 23, 1,115,101,114,118,101, 10, 9, + 3, 23, 1,115,101,114,118,101, 3, 8, 3, 21, 1,115,101,110,100, 40, 8, + 3, 21, 1,115,101,110,100, 29, 8, 3, 21, 1,115,101,110,100, 12, 8, 3, + 21, 1,115,101,110,100, 8, 8, 3, 21, 1,115, 97,118,101, 2, 13, 3, 31, + 1,115, 97, 99,114,105,102,105, 99,101, 23, 8, 3, 21, 1,112,111,111,114, + 16, 10, 3, 25, 1,112,108, 97, 99,101,115, 32, 7, 3, 19, 1,111,105,108, + 49, 8, 3, 21, 1,110,101, 97,114, 7, 8, 3, 21, 1,110,101, 97,114, 4, + 11, 3, 27, 1,109,111,114,110,105,110,103, 41, 11, 3, 27, 1,109,111,114, + 110,105,110,103, 26, 8, 3, 21, 1,103,111,110,101, 25, 8, 3, 21, 1,103, + 111,110,101, 5, 9, 3, 23, 1,100,119,101,108,116, 37, 8, 3, 21, 1,100, + 111,116,104, 44, 8, 3, 21, 1,100,111,116,104, 13, 7, 3, 21, 9,100,111, + 116,104, 8, 3, 21, 1,100,105,101,100, 14, 12, 3, 29, 1,100,101,112, 97, + 114,116,101,100, 46, 10, 3, 25, 1, 99,117, 98,105,116,115, 35, 9, 3, 23, + 1, 99,104,105,108,100, 36, 7, 3, 19, 1, 99, 97,110, 6, 11, 3, 27, 1, + 98,101,116,119,101,101,110, 17, 9, 3, 23, 1, 98,101,103, 97,116, 43, 9, + 3, 23, 1, 98,101,103, 97,116, 42, 9, 3, 23, 1, 98,101,103, 97,116, 39, + 9, 3, 23, 1, 98,101,103, 97,116, 9, 7, 3, 19, 1, 97,114,107, 34, 9, + 3, 23, 1, 97,110,103,101,114, 47, 9, 3, 23, 1, 97,110,103,101,108, 27, + 9, 3, 23, 1, 97, 98,111,118,101, 45, 0, 0, 0, 17, 10, 3, 25, 1,116, + 101,109,112,108,101, 48, 2, 0, 0, 0, 1, 1,239, 0, 0, 0, 0, 20, 1, + 239, 1,206, 1,192, 1,180, 1,166, 1,152, 1,138, 1,125, 1,109, 1, 97, + 1, 84, 1, 69, 1, 52, 1, 39, 1, 26, 1, 14, 1, 1, 0,243, 0,230, 0, + 217, 0,201, 0,185, 0,172, 0,159, 0,147, 0,133, 0,120, 0,102, 0, 89, + 0, 76, 0, 0, 0, 0, 12, 5, 21, 1, 1, 1,115,101,110,100, 26, 14, 40, + 12, 5, 21, 1, 1, 1,115, 97,118,101, 39, 45, 2, 17, 5, 31, 1, 1, 1, + 115, 97, 99,114,105,102,105, 99,101, 31, 6, 23, 12, 5, 21, 1, 1, 1,112, + 111,111,114, 19, 44, 16, 13, 5, 25, 8, 1, 1,112,108, 97, 99,101,115, 16, + 32, 11, 5, 19, 1, 1, 1,111,105,108, 38, 9, 49, 12, 5, 21, 1, 1, 1, + 110,101, 97,114, 38, 21, 7, 12, 5, 21, 1, 1, 1,110,101, 97,114, 35, 41, + 4, 15, 5, 27, 1, 1, 1,109,111,114,110,105,110,103, 17, 40, 26, 15, 5, + 27, 1, 1, 1,109,111,114,110,105,110,103, 13, 46, 41, 12, 5, 21, 1, 1, + 1,103,111,110,101, 28, 25, 5, 12, 5, 21, 1, 1, 1,103,111,110,101, 19, + 10, 25, 13, 5, 23, 1, 1, 1,100,119,101,108,116, 18, 17, 37, 12, 5, 21, + 1, 1, 1,100,111,116,104, 39, 4, 13, 11, 5, 21, 1, 1, 9,100,111,116, + 104, 32, 40, 12, 5, 21, 1, 1, 1,100,111,116,104, 9, 48, 44, 12, 5, 21, + 1, 1, 1,100,105,101,100, 27, 35, 14, 16, 5, 29, 1, 1, 1,100,101,112, + 97,114,116,101,100, 22, 28, 46, 14, 5, 25, 1, 1, 1, 99,117, 98,105,116, + 115, 22, 38, 35, 12, 5, 23, 1, 8, 1, 99,104,105,108,100, 17, 36, 11, 5, + 19, 1, 1, 1, 99, 97,110, 11, 2, 6, 15, 5, 27, 1, 1, 1, 98,101,116, + 119,101,101,110, 36, 29, 17, 12, 5, 23, 1, 8, 1, 98,101,103, 97,116, 50, + 9, 13, 5, 23, 1, 1, 1, 98,101,103, 97,116, 45, 3, 39, 13, 5, 23, 1, + 1, 1, 98,101,103, 97,116, 41, 5, 43, 13, 5, 23, 1, 1, 1, 98,101,103, + 97,116, 5, 20, 42, 11, 5, 19, 1, 1, 1, 97,114,107, 20, 26, 34, 13, 5, + 23, 1, 1, 1, 97,110,103,101,114, 40, 22, 47, 13, 5, 23, 1, 1, 1, 97, + 110,103,101,108, 3, 22, 27, 12, 5, 23, 1, 9, 1, 97, 98,111,118,101, 45, + 20, 13, 5, 23, 1, 1, 1, 0, 0, 0, 19, 12, 5, 21, 1, 1, 1,115,101, + 110,100, 26, 14, 40, 13, 0, 0, 0, 28, 0, 78, 0, 1,241, 1,226, 1,210, + 1,195, 1,180, 1,166, 1,151, 1,136, 1,121, 1,105, 1, 91, 1, 76, 1, + 61, 1, 46, 1, 29, 1, 14, 0,252, 0,238, 0,224, 0,209, 0,194, 0,177, + 0,157, 0,143, 0,128, 0,110, 0, 94, 0, 78, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 14, 28, 6, 0, 1, 1, 1, 23, 17, 67, 31,119, + 111,114,107,115, 14, 27, 6, 0, 1, 1, 1, 23, 22, 71, 3, 97,110,103,101, + 108, 16, 26, 6, 0, 1, 1, 1, 27, 40, 98, 17,109,111,114,110,105,110,103, + 13, 25, 6, 0, 1, 1, 1, 21, 10, 7, 19,103,111,110,101, 12, 24, 6, 0, + 1, 1, 9, 21, 43, 46,119, 97,121,115, 18, 23, 6, 0, 1, 1, 1, 31, 6, + 37, 31,115, 97, 99,114,105,102,105, 99,101, 15, 22, 6, 0, 1, 1, 1, 25, + 45, 71, 28,116,104,111,117,103,104, 13, 21, 6, 0, 1, 1, 1, 21, 22, 92, + 18,115,111,109,101, 13, 20, 6, 0, 9, 1, 1, 23, 2, 45, 97, 98,111,118, + 101, 12, 19, 6, 0, 1, 1, 8, 21, 4, 58,119, 97,121,115, 12, 18, 6, 0, + 1, 1, 1, 19, 44, 19, 43,119, 97,114, 16, 17, 6, 0, 1, 1, 1, 27, 29, + 74, 36, 98,101,116,119,101,101,110, 13, 16, 6, 0, 1, 1, 1, 21, 44, 52, + 19,112,111,111,114, 15, 15, 6, 0, 1, 1, 1, 25, 6, 3, 11,116,101,109, + 112,108,101, 13, 14, 6, 0, 1, 1, 1, 21, 35, 48, 27,100,105,101,100, 13, + 13, 6, 0, 1, 1, 1, 21, 4, 21, 39,100,111,116,104, 13, 12, 6, 0, 1, + 1, 1, 21, 4, 38, 36,115,101,110,100, 12, 11, 6, 0, 1, 1, 1, 19, 13, + 48, 22,115,105,120, 14, 10, 6, 0, 1, 1, 1, 23, 41, 89, 14,115,101,114, + 118,101, 13, 9, 6, 0, 8, 1, 1, 23, 16, 50, 98,101,103, 97,116, 13, 8, + 6, 0, 1, 1, 1, 21, 42, 49, 34,115,101,110,100, 13, 7, 6, 0, 1, 1, + 1, 21, 21, 91, 38,110,101, 97,114, 12, 6, 6, 0, 1, 1, 1, 19, 2, 37, + 11, 99, 97,110, 13, 5, 6, 0, 1, 1, 1, 21, 25, 27, 28,103,111,110,101, + 13, 4, 6, 0, 1, 1, 1, 21, 41, 32, 35,110,101, 97,114, 14, 3, 6, 0, + 1, 1, 1, 23, 32, 24, 26,115,101,114,118,101, 13, 2, 6, 0, 1, 1, 1, + 21, 45, 14, 39,115, 97,118,101, 13, 1, 6, 0, 1, 1, 1, 21, 40, 68, 32, + 100,111,116,104, 13, 0, 0, 0, 22, 0,166, 0, 1,241, 1,226, 1,210, 1, + 194, 1,183, 1,169, 1,152, 1,137, 1,121, 1,106, 1, 90, 1, 75, 1, 57, + 1, 41, 1, 25, 1, 10, 0,250, 0,231, 0,215, 0,198, 0,184, 0,166, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, + 50, 6, 0, 1, 1, 1, 27, 9, 92, 29,116,104,101,114,101,105,110, 12, 49, + 6, 0, 1, 1, 1, 19, 9, 51, 38,111,105,108, 15, 48, 6, 0, 1, 1, 1, + 25, 37, 93, 7,116,101,109,112,108,101, 14, 47, 6, 0, 1, 1, 1, 23, 22, + 16, 40, 97,110,103,101,114, 17, 46, 6, 0, 1, 1, 1, 29, 28, 88, 22,100, + 101,112, 97,114,116,101,100, 14, 45, 6, 0, 1, 1, 1, 23, 47, 54, 12, 97, + 98,111,118,101, 13, 44, 6, 0, 1, 1, 1, 21, 48, 15, 9,100,111,116,104, + 14, 43, 6, 0, 1, 1, 1, 23, 5, 23, 41, 98,101,103, 97,116, 14, 42, 6, + 0, 1, 1, 1, 23, 20, 18, 5, 98,101,103, 97,116, 16, 41, 6, 0, 1, 1, + 1, 27, 46, 92, 13,109,111,114,110,105,110,103, 13, 40, 6, 0, 1, 1, 1, + 21, 14, 30, 26,115,101,110,100, 14, 39, 6, 0, 1, 1, 1, 23, 3, 21, 45, + 98,101,103, 97,116, 13, 38, 6, 0, 1, 1, 1, 21, 10, 97, 34,115,104,101, + 119, 14, 37, 6, 0, 1, 1, 1, 23, 17, 66, 18,100,119,101,108,116, 13, 36, + 6, 0, 8, 1, 1, 23, 52, 17, 99,104,105,108,100, 15, 35, 6, 0, 1, 1, + 1, 25, 38, 34, 22, 99,117, 98,105,116,115, 12, 34, 6, 0, 1, 1, 1, 19, + 26, 15, 20, 97,114,107, 9, 33, 6, 0, 1, 1, 1, 0, 7, 72, 31, 14, 32, + 6, 0, 1, 1, 8, 25, 16, 7,112,108, 97, 99,101,115, 14, 31, 6, 0, 1, + 1, 1, 23, 39, 90, 27,116,114,117,116,104, 13, 30, 6, 0, 1, 1, 1, 21, + 16, 81, 25,119, 97,108,107, 13, 29, 6, 0, 1, 1, 1, 21, 34, 62, 27,115, + 101,110,100, 10, 0, 0, 0, 41, 0,116, 0, 1,251, 1,241, 1,231, 1,221, + 1,211, 1,203, 1,193, 1,183, 1,173, 1,163, 1,151, 1,143, 1,133, 1, + 122, 1,109, 1,100, 1, 92, 1, 83, 1, 74, 1, 64, 1, 55, 1, 46, 1, 34, + 1, 22, 1, 13, 1, 4, 0,252, 0,241, 0,232, 0,218, 0,209, 0,200, 0, + 191, 0,182, 0,173, 0,163, 0,153, 0,144, 0,136, 0,127, 0,116, 0,105, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11,116,101, + 109,112,108,101, 48, 10, 3, 25, 1,116,101,109,112,108,101, 15, 8, 3, 21, + 1,115,111,109,101, 21, 7, 3, 19, 1,115,105,120, 11, 8, 3, 21, 1,115, + 104,101,119, 38, 9, 3, 23, 1,115,101,114,118,101, 10, 9, 3, 23, 1,115, + 101,114,118,101, 3, 8, 3, 21, 1,115,101,110,100, 40, 8, 3, 21, 1,115, + 101,110,100, 29, 8, 3, 21, 1,115,101,110,100, 12, 8, 3, 21, 1,115,101, + 110,100, 8, 8, 3, 21, 1,115, 97,118,101, 2, 13, 3, 31, 1,115, 97, 99, + 114,105,102,105, 99,101, 23, 8, 3, 21, 1,112,111,111,114, 16, 10, 3, 25, + 1,112,108, 97, 99,101,115, 32, 7, 3, 19, 1,111,105,108, 49, 8, 3, 21, + 1,110,101, 97,114, 7, 8, 3, 21, 1,110,101, 97,114, 4, 11, 3, 27, 1, + 109,111,114,110,105,110,103, 41, 11, 3, 27, 1,109,111,114,110,105,110,103, + 26, 8, 3, 21, 1,103,111,110,101, 25, 8, 3, 21, 1,103,111,110,101, 5, + 9, 3, 23, 1,100,119,101,108,116, 37, 8, 3, 21, 1,100,111,116,104, 44, + 8, 3, 21, 1,100,111,116,104, 13, 7, 3, 21, 9,100,111,116,104, 8, 3, + 21, 1,100,105,101,100, 14, 12, 3, 29, 1,100,101,112, 97,114,116,101,100, + 46, 10, 3, 25, 1, 99,117, 98,105,116,115, 35, 9, 3, 23, 1, 99,104,105, + 108,100, 36, 7, 3, 19, 1, 99, 97,110, 6, 11, 3, 27, 1, 98,101,116,119, + 101,101,110, 17, 9, 3, 23, 1, 98,101,103, 97,116, 43, 9, 3, 23, 1, 98, + 101,103, 97,116, 42, 9, 3, 23, 1, 98,101,103, 97,116, 39, 9, 3, 23, 1, + 98,101,103, 97,116, 9, 7, 3, 19, 1, 97,114,107, 34, 9, 3, 23, 1, 97, + 110,103,101,114, 47, 9, 3, 23, 1, 97,110,103,101,108, 27, 9, 3, 23, 1, + 97, 98,111,118,101, 45, 9, 3, 23, 1, 97, 98,111,118,101, 20, 4, 3, 0, + 1, 33, 10, 0, 0, 0, 8, 1,178, 0, 1,244, 1,233, 1,223, 1,214, 1, + 206, 1,197, 1,188, 1,178, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, + 3, 23, 1,119,111,114,107,115, 28, 8, 3, 21, 1,119, 97,121,115, 24, 8, + 3, 21, 1,119, 97,121,115, 19, 7, 3, 19, 1,119, 97,114, 18, 8, 3, 21, + 1,119, 97,108,107, 30, 9, 3, 23, 1,116,114,117,116,104, 31, 10, 3, 25, + 1,116,104,111,117,103,104, 22, 11, 3, 27, 1,116,104,101,114,101,105,110, + 50, 10, 0, 0, 0, 31, 0, 89, 0, 1,247, 1,233, 1,220, 1,206, 1,192, + 1,180, 1,166, 1,152, 1,138, 1,125, 1,109, 1, 97, 1, 84, 1, 69, 1, + 52, 1, 39, 1, 26, 1, 14, 1, 1, 0,243, 0,230, 0,217, 0,201, 0,185, + 0,172, 0,159, 0,147, 0,133, 0,120, 0,102, 0, 89, 0, 76, 0, 0, 0, + 0, 0, 0, 0, 13, 1, 1,115,101,110,100, 26, 14, 40, 12, 5, 21, 1, 1, + 1,115, 97,118,101, 39, 45, 2, 17, 5, 31, 1, 1, 1,115, 97, 99,114,105, + 102,105, 99,101, 31, 6, 23, 12, 5, 21, 1, 1, 1,112,111,111,114, 19, 44, + 16, 13, 5, 25, 8, 1, 1,112,108, 97, 99,101,115, 16, 32, 11, 5, 19, 1, + 1, 1,111,105,108, 38, 9, 49, 12, 5, 21, 1, 1, 1,110,101, 97,114, 38, + 21, 7, 12, 5, 21, 1, 1, 1,110,101, 97,114, 35, 41, 4, 15, 5, 27, 1, + 1, 1,109,111,114,110,105,110,103, 17, 40, 26, 15, 5, 27, 1, 1, 1,109, + 111,114,110,105,110,103, 13, 46, 41, 12, 5, 21, 1, 1, 1,103,111,110,101, + 28, 25, 5, 12, 5, 21, 1, 1, 1,103,111,110,101, 19, 10, 25, 13, 5, 23, + 1, 1, 1,100,119,101,108,116, 18, 17, 37, 12, 5, 21, 1, 1, 1,100,111, + 116,104, 39, 4, 13, 11, 5, 21, 1, 1, 9,100,111,116,104, 32, 40, 12, 5, + 21, 1, 1, 1,100,111,116,104, 9, 48, 44, 12, 5, 21, 1, 1, 1,100,105, + 101,100, 27, 35, 14, 16, 5, 29, 1, 1, 1,100,101,112, 97,114,116,101,100, + 22, 28, 46, 14, 5, 25, 1, 1, 1, 99,117, 98,105,116,115, 22, 38, 35, 12, + 5, 23, 1, 8, 1, 99,104,105,108,100, 17, 36, 11, 5, 19, 1, 1, 1, 99, + 97,110, 11, 2, 6, 15, 5, 27, 1, 1, 1, 98,101,116,119,101,101,110, 36, + 29, 17, 12, 5, 23, 1, 8, 1, 98,101,103, 97,116, 50, 9, 13, 5, 23, 1, + 1, 1, 98,101,103, 97,116, 45, 3, 39, 13, 5, 23, 1, 1, 1, 98,101,103, + 97,116, 41, 5, 43, 13, 5, 23, 1, 1, 1, 98,101,103, 97,116, 5, 20, 42, + 11, 5, 19, 1, 1, 1, 97,114,107, 20, 26, 34, 13, 5, 23, 1, 1, 1, 97, + 110,103,101,114, 40, 22, 47, 13, 5, 23, 1, 1, 1, 97,110,103,101,108, 3, + 22, 27, 12, 5, 23, 1, 9, 1, 97, 98,111,118,101, 45, 20, 13, 5, 23, 1, + 1, 1, 97, 98,111,118,101, 12, 47, 45, 8, 5, 0, 1, 1, 1, 31, 7, 33, + 10, 0, 0, 0, 18, 1, 13, 0, 1,243, 1,230, 1,217, 1,203, 1,189, 1, + 176, 1,164, 1,151, 1,136, 1,121, 1,105, 1, 90, 1, 76, 1, 63, 1, 51, + 1, 39, 1, 27, 1, 13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 13, 5, 23, 1, 1, 1,119,111,114,107,115, 31, 17, 28, 11, 5, + 21, 9, 1, 1,119, 97,121,115, 43, 24, 11, 5, 21, 8, 1, 1,119, 97,121, + 115, 4, 19, 11, 5, 19, 1, 1, 1,119, 97,114, 43, 44, 18, 12, 5, 21, 1, + 1, 1,119, 97,108,107, 25, 16, 30, 13, 5, 23, 1, 1, 1,116,114,117,116, + 104, 27, 39, 31, 14, 5, 25, 1, 1, 1,116,104,111,117,103,104, 28, 45, 22, + 15, 5, 27, 1, 1, 1,116,104,101,114,101,105,110, 29, 9, 50, 14, 5, 25, + 1, 1, 1,116,101,109,112,108,101, 11, 6, 15, 14, 5, 25, 1, 1, 1,116, + 101,109,112,108,101, 7, 37, 48, 12, 5, 21, 1, 1, 1,115,111,109,101, 18, + 22, 21, 11, 5, 19, 1, 1, 1,115,105,120, 22, 13, 11, 12, 5, 21, 1, 1, + 1,115,104,101,119, 34, 10, 38, 13, 5, 23, 1, 1, 1,115,101,114,118,101, + 26, 32, 3, 13, 5, 23, 1, 1, 1,115,101,114,118,101, 14, 41, 10, 12, 5, + 21, 1, 1, 1,115,101,110,100, 36, 4, 12, 12, 5, 21, 1, 1, 1,115,101, + 110,100, 34, 42, 8, 12, 5, 21, 1, 1, 1,115,101,110,100, 27, 34, 29, 10, + 0, 0, 0, 28, 0, 82, 0, 1,241, 1,226, 1,211, 1,197, 1,181, 1,166, + 1,151, 1,137, 1,121, 1,104, 1, 84, 1, 73, 1, 59, 1, 41, 1, 26, 1, + 11, 0,253, 0,238, 0,223, 0,207, 0,191, 0,175, 0,159, 0,144, 0,129, + 0,113, 0, 97, 0, 82, 0, 68, 0, 0, 0, 0, 0, 14, 1, 1, 19, 26, 34, + 15, 20, 97,114,107, 14, 6, 1, 1, 1, 1, 21, 25, 5, 27, 28,103,111,110, + 101, 15, 6, 1, 1, 1, 1, 23, 22, 47, 16, 40, 97,110,103,101,114, 15, 6, + 1, 1, 1, 1, 23, 22, 27, 71, 3, 97,110,103,101,108, 14, 6, 1, 1, 1, + 1, 21, 22, 21, 92, 18,115,111,109,101, 14, 6, 1, 1, 1, 1, 21, 21, 7, + 91, 38,110,101, 97,114, 15, 6, 1, 1, 1, 1, 23, 20, 42, 18, 5, 98,101, + 103, 97,116, 15, 6, 1, 1, 1, 1, 23, 17, 37, 66, 18,100,119,101,108,116, + 15, 6, 1, 1, 1, 1, 23, 17, 28, 67, 31,119,111,114,107,115, 15, 6, 1, + 1, 1, 8, 25, 16, 32, 7,112,108, 97, 99,101,115, 14, 6, 1, 1, 1, 1, + 21, 16, 30, 81, 25,119, 97,108,107, 14, 6, 1, 1, 1, 1, 21, 14, 40, 30, + 26,115,101,110,100, 13, 6, 1, 1, 1, 1, 19, 13, 11, 48, 22,115,105,120, + 14, 6, 1, 1, 1, 1, 21, 10, 38, 97, 34,115,104,101,119, 14, 6, 1, 1, + 1, 1, 21, 10, 25, 7, 19,103,111,110,101, 17, 6, 1, 1, 1, 1, 27, 9, + 50, 92, 29,116,104,101,114,101,105,110, 13, 6, 1, 1, 1, 1, 19, 9, 49, + 51, 38,111,105,108, 10, 6, 1, 1, 1, 1, 0, 7, 33, 72, 31, 19, 6, 1, + 1, 1, 1, 31, 6, 23, 37, 31,115, 97, 99,114,105,102,105, 99,101, 16, 6, + 1, 1, 1, 1, 25, 6, 15, 3, 11,116,101,109,112,108,101, 15, 6, 1, 1, + 1, 1, 23, 5, 43, 23, 41, 98,101,103, 97,116, 13, 6, 1, 1, 1, 8, 21, + 4, 19, 58,119, 97,121,115, 14, 6, 1, 1, 1, 1, 21, 4, 13, 21, 39,100, + 111,116,104, 14, 6, 1, 1, 1, 1, 21, 4, 12, 38, 36,115,101,110,100, 15, + 6, 1, 1, 1, 1, 23, 3, 39, 21, 45, 98,101,103, 97,116, 13, 6, 1, 1, + 1, 1, 19, 2, 6, 37, 11, 99, 97,110, 14, 6, 9, 1, 1, 1, 23, 20, 2, + 45, 97, 98,111,118,101, 14, 6, 8, 1, 1, 1, 23, 36, 52, 17, 99,104,105, + 108,100, 14, 6, 8, 1, 1, 1, 23, 9, 16, 50, 98,101,103, 97,116, 10, 0, + 0, 0, 21, 0,177, 0, 1,237, 1,219, 1,203, 1,188, 1,173, 1,156, 1, + 139, 1,123, 1,109, 1, 91, 1, 76, 1, 60, 1, 45, 1, 31, 1, 16, 1, 2, + 0,243, 0,226, 0,208, 0,192, 0,177, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 14, 6, 1, 1, 1, 1, 21, 48, 44, 15, 9,100,111,116,104, + 15, 6, 1, 1, 1, 1, 23, 47, 45, 54, 12, 97, 98,111,118,101, 17, 6, 1, + 1, 1, 1, 27, 46, 41, 92, 13,109,111,114,110,105,110,103, 16, 6, 1, 1, + 1, 1, 25, 45, 22, 71, 28,116,104,111,117,103,104, 14, 6, 1, 1, 1, 1, + 21, 45, 2, 14, 39,115, 97,118,101, 13, 6, 1, 1, 1, 1, 19, 44, 18, 19, + 43,119, 97,114, 14, 6, 1, 1, 1, 1, 21, 44, 16, 52, 19,112,111,111,114, + 13, 6, 1, 1, 1, 9, 21, 43, 24, 46,119, 97,121,115, 14, 6, 1, 1, 1, + 1, 21, 42, 8, 49, 34,115,101,110,100, 15, 6, 1, 1, 1, 1, 23, 41, 10, + 89, 14,115,101,114,118,101, 14, 6, 1, 1, 1, 1, 21, 41, 4, 32, 35,110, + 101, 97,114, 17, 6, 1, 1, 1, 1, 27, 40, 26, 98, 17,109,111,114,110,105, + 110,103, 13, 6, 1, 9, 1, 1, 21, 40, 68, 32,100,111,116,104, 15, 6, 1, + 1, 1, 1, 23, 39, 31, 90, 27,116,114,117,116,104, 16, 6, 1, 1, 1, 1, + 25, 38, 35, 34, 22, 99,117, 98,105,116,115, 16, 6, 1, 1, 1, 1, 25, 37, + 48, 93, 7,116,101,109,112,108,101, 14, 6, 1, 1, 1, 1, 21, 35, 14, 48, + 27,100,105,101,100, 14, 6, 1, 1, 1, 1, 21, 34, 29, 62, 27,115,101,110, + 100, 15, 6, 1, 1, 1, 1, 23, 32, 3, 24, 26,115,101,114,118,101, 17, 6, + 1, 1, 1, 1, 27, 29, 17, 74, 36, 98,101,116,119,101,101,110, 18, 6, 1, + 1, 1, 1, 29, 28, 46, 88, 22,100,101,112, 97,114,116,101,100, 10, 0, 0, + 0, 32, 0, 95, 0, 1,247, 1,238, 1,229, 1,220, 1,211, 1,199, 1,187, + 1,176, 1,164, 1,148, 1,133, 1,116, 1, 99, 1, 86, 1, 67, 1, 55, 1, + 43, 1, 31, 1, 18, 1, 5, 0,249, 0,236, 0,224, 0,209, 0,191, 0,174, + 0,157, 0,145, 0,132, 0,120, 0,108, 0, 95, 0, 83, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 12, 1, 1, 0, 1, 49, 51, 38, 15, 12, 7, 1, + 1, 1, 1, 0, 1, 48, 37, 93, 7, 30, 11, 7, 1, 1, 1, 0, 0, 1, 47, + 22, 16, 24, 11, 7, 1, 0, 1, 1, 0, 1, 47, 16, 40, 12, 12, 7, 1, 1, + 1, 1, 0, 1, 46, 28, 88, 22, 2, 11, 7, 1, 1, 0, 1, 0, 1, 44, 48, + 9, 19, 16, 7, 1, 1, 1, 0, 23, 1, 42, 20, 18, 98,101,103, 97,116, 22, + 16, 7, 1, 1, 0, 1, 23, 1, 37, 17, 18,100,119,101,108,116, 16, 17, 7, + 1, 1, 0, 1, 25, 1, 35, 38, 22, 99,117, 98,105,116,115, 17, 14, 7, 1, + 1, 1, 0, 19, 1, 34, 26, 15, 97,114,107, 21, 11, 7, 1, 1, 1, 0, 0, + 1, 32, 16, 7, 23, 12, 7, 1, 1, 1, 1, 0, 1, 28, 17, 67, 31, 29, 11, + 7, 1, 0, 1, 1, 0, 1, 25, 7, 19, 11, 12, 7, 1, 1, 1, 1, 0, 1, + 22, 45, 71, 28, 28, 12, 7, 1, 1, 1, 1, 0, 1, 21, 22, 92, 18, 4, 11, + 7, 1, 1, 0, 1, 0, 1, 21, 22, 18, 18, 11, 7, 1, 1, 1, 1, 0, 9, + 17, 29, 74, 36, 11, 7, 1, 1, 1, 0, 0, 1, 17, 29, 74, 25, 18, 7, 1, + 1, 1, 1, 25, 1, 15, 6, 3, 11,116,101,109,112,108,101, 5, 12, 7, 1, + 1, 1, 1, 0, 1, 12, 4, 38, 36, 27, 16, 7, 1, 0, 1, 1, 23, 1, 10, + 89, 14,115,101,114,118,101, 14, 16, 7, 1, 1, 1, 1, 21, 1, 4, 41, 32, + 35,110,101, 97,114, 3, 14, 7, 9, 0, 1, 1, 21, 1, 68, 32,100,111,116, + 104, 13, 15, 7, 0, 1, 1, 1, 21, 1, 21, 91, 38,110,101, 97,114, 6, 11, + 7, 0, 1, 1, 1, 0, 1, 16, 81, 25, 9, 10, 7, 0, 1, 1, 8, 0, 1, + 16, 7, 10, 11, 7, 0, 1, 1, 1, 0, 1, 7, 72, 31, 8, 11, 7, 0, 1, + 1, 1, 0, 1, 6, 37, 31, 7, 8, 7, 0, 0, 0, 0, 0, 1, 35, 8, 7, + 0, 0, 0, 0, 0, 1, 34, 8, 7, 0, 0, 0, 0, 0, 1, 33, 8, 7, 0, + 0, 0, 0, 0, 1, 32, 8, 7, 0, 0, 0, 0, 0, 1, 31, 10, 0, 0, 0, + 2, 1,231, 0, 1,244, 1,231, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 12, 7, 1, 1, 1, 1, 0, 1, 49, 9, 51, + 38, 26, 11, 7, 1, 1, 0, 1, 0, 1, 49, 9, 38, 20, 13, 0, 0, 0, 23, + 0, 67, 0, 1,238, 1,220, 1,202, 1,186, 1,168, 1,148, 1,130, 1,107, + 1, 86, 1, 65, 1, 44, 1, 27, 1, 14, 0,250, 0,224, 0,205, 0,184, 0, + 165, 0,145, 0,123, 0,106, 0, 86, 0, 67, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 17, 23, 6, 0, 23, 1, 1, 21,107,110,111,119,110, 52, + 19,112,111,111,114, 18, 22, 6, 0, 23, 1, 1, 23, 97, 98,111,118,101, 24, + 26,115,101,114,118,101, 15, 21, 6, 0, 19, 1, 1, 21,119, 97,114, 52, 19, + 112,111,111,114, 20, 20, 6, 0, 27, 1, 8, 25,110,111,116,104,105,110,103, + 7,112,108, 97, 99,101,115, 18, 19, 6, 0, 23, 1, 1, 23, 98,101,103, 97, + 116, 90, 27,116,114,117,116,104, 17, 18, 6, 0, 23, 1, 1, 21,100,119,101, + 108,116, 21, 39,100,111,116,104, 19, 17, 6, 0, 27, 1, 1, 21,109,111,114, + 110,105,110,103, 52, 19,112,111,111,114, 17, 16, 6, 0, 21, 1, 1, 23,115, + 104,101,119, 90, 27,116,114,117,116,104, 24, 15, 6, 0, 27, 1, 1, 31,116, + 104,101,114,101,105,110, 37, 31,115, 97, 99,114,105,102,105, 99,101, 18, 14, + 6, 0, 23, 1, 8, 25,115,109,111,116,101, 7,112,108, 97, 99,101,115, 11, + 13, 6, 0, 19, 1, 1, 0, 97,114,107, 72, 31, 15, 12, 6, 0, 21, 1, 8, + 21,119,105,110,101, 58,119, 97,121,115, 19, 11, 6, 0, 21, 1, 1, 27,115, + 111,109,101, 98, 17,109,111,114,110,105,110,103, 19, 10, 6, 0, 27, 1, 1, + 21, 98,101,116,119,101,101,110, 92, 18,115,111,109,101, 19, 9, 6, 0, 21, + 1, 1, 27,115, 97,118,101, 74, 36, 98,101,116,119,101,101,110, 21, 8, 6, + 0, 25, 1, 1, 27,116,104,111,117,103,104, 98, 17,109,111,114,110,105,110, + 103, 16, 7, 6, 0, 21, 1, 1, 21,115,101,110,100, 49, 34,115,101,110,100, + 18, 6, 6, 0, 25, 1, 1, 21,119,105,115,100,111,109, 38, 36,115,101,110, + 100, 16, 5, 6, 0, 23, 1, 9, 21, 97,110,103,101,114, 46,119, 97,121,115, + 14, 4, 6, 0, 19, 1, 1, 19, 99, 97,110, 19, 43,119, 97,114, 16, 3, 6, + 0, 23, 1, 1, 19,111,102,102,101,114, 48, 22,115,105,120, 16, 2, 6, 0, + 23, 1, 8, 21,119,111,114,107,115, 58,119, 97,121,115, 16, 1, 6, 0, 23, + 1, 1, 19,116,114,117,116,104, 37, 11, 99, 97,110, 13, 0, 0, 0, 22, 0, + 64, 0, 1,230, 1,213, 1,191, 1,169, 1,148, 1,130, 1,108, 1, 89, 1, + 70, 1, 51, 1, 34, 1, 16, 0,253, 0,233, 0,214, 0,194, 0,174, 0,151, + 0,132, 0,109, 0, 90, 0, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 24, 45, 6, 0, 35, 1, 1, 23,105,110,104, 97, 98,105,116, 97,110,116, + 115, 23, 41, 98,101,103, 97,116, 17, 44, 6, 0, 23, 1, 1, 21, 97,110,103, + 101,108, 48, 27,100,105,101,100, 21, 43, 6, 0, 25, 1, 1, 27,116,101,109, + 112,108,101, 74, 36, 98,101,116,119,101,101,110, 17, 42, 6, 0, 23, 1, 1, + 21, 99,104,105,108,100, 81, 25,119, 97,108,107, 21, 41, 6, 0, 21, 1, 1, + 31,119, 97,121,115, 37, 31,115, 97, 99,114,105,102,105, 99,101, 18, 40, 6, + 0, 21, 1, 1, 25,112,111,111,114, 93, 7,116,101,109,112,108,101, 18, 39, + 6, 0, 21, 1, 1, 25,100,111,116,104, 3, 11,116,101,109,112,108,101, 17, + 38, 6, 0, 23, 1, 1, 21,102,114,117,105,116, 62, 27,115,101,110,100, 18, + 37, 6, 0, 23, 1, 1, 23,115,101,114,118,101, 90, 27,116,114,117,116,104, + 17, 36, 6, 0, 21, 1, 1, 23,110,101, 97,114, 90, 27,116,114,117,116,104, + 16, 35, 6, 0, 21, 1, 1, 21,108,111,110,103, 14, 39,115, 97,118,101, 15, + 34, 6, 0, 21, 1, 1, 19,119, 97,108,107, 15, 20, 97,114,107, 17, 33, 6, + 0, 25, 1, 9, 21, 99,117, 98,105,116,115, 46,119, 97,121,115, 17, 32, 6, + 0, 21, 1, 1, 23,103,111,110,101, 23, 41, 98,101,103, 97,116, 17, 31, 6, + 0, 23, 1, 1, 21,119,104,105,108,101, 49, 34,115,101,110,100, 20, 30, 6, + 0, 21, 1, 1, 29,112, 97,114,116, 88, 22,100,101,112, 97,114,116,101,100, + 16, 29, 6, 0, 21, 1, 1, 21, 98,101, 97,114, 92, 18,115,111,109,101, 19, + 28, 6, 0, 25, 1, 1, 23,112,108, 97, 99,101,115, 23, 41, 98,101,103, 97, + 116, 20, 27, 6, 0, 27, 1, 1, 23,116,104,121,115,101,108,102, 54, 12, 97, + 98,111,118,101, 20, 26, 6, 0, 29, 1, 1, 21,100,101,112, 97,114,116,101, + 100, 92, 18,115,111,109,101, 15, 25, 6, 0, 21, 1, 1, 19,116,101,108,108, + 19, 43,119, 97,114, 24, 24, 6, 0, 31, 1, 1, 27,115, 97, 99,114,105,102, + 105, 99,101, 92, 13,109,111,114,110,105,110,103, 13, 0, 0, 0, 5, 1,162, + 0, 1,239, 1,221, 1,203, 1,182, 1,162, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 50, 6, 0, 23, 1, 1, + 23,119,114, 97,116,104, 21, 45, 98,101,103, 97,116, 19, 49, 6, 0, 21, 1, + 1, 27,116,114,101,101, 98, 17,109,111,114,110,105,110,103, 16, 48, 6, 0, + 19, 1, 1, 23,115,105,120, 71, 3, 97,110,103,101,108, 16, 47, 6, 0, 21, + 1, 1, 21,100,105,101,100, 7, 19,103,111,110,101, 15, 46, 6, 0, 19, 1, + 1, 21,111,105,108, 81, 25,119, 97,108,107, 10, 0, 0, 0, 40, 0,106, 0, + 1,246, 1,236, 1,226, 1,218, 1,209, 1,199, 1,187, 1,179, 1,169, 1, + 158, 1,145, 1,136, 1,127, 1,117, 1,107, 1, 98, 1, 82, 1, 72, 1, 63, + 1, 51, 1, 42, 1, 30, 1, 20, 1, 12, 1, 3, 0,248, 0,239, 0,225, 0, + 216, 0,207, 0,197, 0,188, 0,180, 0,170, 0,161, 0,152, 0,141, 0,129, + 0,118, 0,106, 0, 97, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9,116,114, + 101,101, 49, 11, 3, 27, 1,116,104,121,115,101,108,102, 27, 10, 3, 25, 1, + 116,104,111,117,103,104, 8, 11, 3, 27, 1,116,104,101,114,101,105,110, 15, + 10, 3, 25, 1,116,101,109,112,108,101, 43, 8, 3, 21, 1,116,101,108,108, + 25, 8, 3, 21, 1,115,111,109,101, 11, 9, 3, 23, 1,115,109,111,116,101, + 14, 7, 3, 19, 1,115,105,120, 48, 8, 3, 21, 1,115,104,101,119, 16, 9, + 3, 23, 1,115,101,114,118,101, 37, 8, 3, 21, 1,115,101,110,100, 7, 8, + 3, 21, 1,115, 97,118,101, 9, 13, 3, 31, 1,115, 97, 99,114,105,102,105, + 99,101, 24, 8, 3, 21, 1,112,111,111,114, 40, 10, 3, 25, 1,112,108, 97, + 99,101,115, 28, 8, 3, 21, 1,112, 97,114,116, 30, 7, 3, 19, 1,111,105, + 108, 46, 9, 3, 23, 1,111,102,102,101,114, 3, 11, 3, 27, 1,110,111,116, + 104,105,110,103, 20, 8, 3, 21, 1,110,101, 97,114, 36, 11, 3, 27, 1,109, + 111,114,110,105,110,103, 17, 8, 3, 21, 1,108,111,110,103, 35, 9, 3, 23, + 1,107,110,111,119,110, 23, 15, 3, 35, 1,105,110,104, 97, 98,105,116, 97, + 110,116,115, 45, 8, 3, 21, 1,103,111,110,101, 32, 9, 3, 23, 1,102,114, + 117,105,116, 38, 9, 3, 23, 1,100,119,101,108,116, 18, 8, 3, 21, 1,100, + 111,116,104, 39, 8, 3, 21, 1,100,105,101,100, 47, 12, 3, 29, 1,100,101, + 112, 97,114,116,101,100, 26, 10, 3, 25, 1, 99,117, 98,105,116,115, 33, 9, + 3, 23, 1, 99,104,105,108,100, 42, 7, 3, 19, 1, 99, 97,110, 4, 11, 3, + 27, 1, 98,101,116,119,101,101,110, 10, 9, 3, 23, 1, 98,101,103, 97,116, + 19, 8, 3, 21, 1, 98,101, 97,114, 29, 7, 3, 19, 1, 97,114,107, 13, 9, + 3, 23, 1, 97,110,103,101,114, 5, 9, 3, 23, 1, 97,110,103,101,108, 44, + 9, 3, 23, 1, 97, 98,111,118,101, 22, 10, 0, 0, 0, 9, 1,171, 0, 1, + 247, 1,238, 1,230, 1,221, 1,211, 1,202, 1,191, 1,181, 1,171, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 9, 3, 23, 1,119,114, 97,116,104, 50, 9, 3, 23, 1,119,111,114,107,115, + 2, 10, 3, 25, 1,119,105,115,100,111,109, 6, 8, 3, 21, 1,119,105,110, + 101, 12, 9, 3, 23, 1,119,104,105,108,101, 31, 8, 3, 21, 1,119, 97,121, + 115, 41, 7, 3, 19, 1,119, 97,114, 21, 8, 3, 21, 1,119, 97,108,107, 34, + 8, 3, 23, 9,116,114,117,116,104, 13, 0, 0, 0, 5, 0, 84, 0, 1, 78, + 0,249, 0,177, 1,163, 0, 84, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 91, 19, + 7, 21, 19, 19, 8,129, 33,118,105,101,119,118, 50, 49,118, 50, 49, 67, 82, + 69, 65, 84, 69, 32, 86, 73, 69, 87, 32,118, 50, 49, 40, 97, 44, 98, 44, 99, + 44,100, 44,101, 41, 32, 65, 83, 32, 83, 69, 76, 69, 67, 84, 32, 97, 44, 98, + 44, 99, 44,100, 44,101, 32, 70, 82, 79, 77, 32,116, 50, 32, 79, 82, 68, 69, + 82, 32, 66, 89, 32, 98, 32, 76, 73, 77, 73, 84, 32, 49, 48, 70, 17, 6, 21, + 19, 19, 8,121,118,105,101,119,118, 53, 48,118, 53, 48, 67, 82, 69, 65, 84, + 69, 32, 86, 73, 69, 87, 32,118, 53, 48, 40, 97, 44, 98, 41, 32, 65, 83, 32, + 83, 69, 76, 69, 67, 84, 32, 97, 44, 98, 32, 70, 82, 79, 77, 32,116, 53, 32, + 87, 72, 69, 82, 69, 32, 97, 60, 62, 50, 53, 83, 16, 7, 21, 19, 19, 8,129, + 17,118,105,101,119,118, 52, 48,118, 52, 48, 67, 82, 69, 65, 84, 69, 32, 86, + 73, 69, 87, 32,118, 52, 48, 40, 97, 44, 98, 44, 99, 44,100, 44,101, 41, 32, + 65, 83, 32, 83, 69, 76, 69, 67, 84, 32, 97, 44, 98, 44, 99, 44,100, 44,101, + 32, 70, 82, 79, 77, 32,116, 52, 32, 87, 72, 69, 82, 69, 32, 97, 60, 62, 50, + 53, 83, 15, 7, 21, 19, 19, 8,129, 17,118,105,101,119,118, 51, 48,118, 51, + 48, 67, 82, 69, 65, 84, 69, 32, 86, 73, 69, 87, 32,118, 51, 48, 40, 97, 44, + 98, 44, 99, 44,100, 44,101, 41, 32, 65, 83, 32, 83, 69, 76, 69, 67, 84, 32, + 97, 44, 98, 44, 99, 44,100, 44,101, 32, 70, 82, 79, 77, 32,116, 51, 32, 87, + 72, 69, 82, 69, 32, 97, 60, 62, 50, 53, 91, 18, 7, 21, 19, 19, 8,129, 33, + 118,105,101,119,118, 49, 49,118, 49, 49, 67, 82, 69, 65, 84, 69, 32, 86, 73, + 69, 87, 32,118, 49, 49, 40, 97, 44, 98, 44, 99, 44,100, 44,101, 41, 32, 65, + 83, 32, 83, 69, 76, 69, 67, 84, 32, 97, 44, 98, 44, 99, 44,100, 44,101, 32, + 70, 82, 79, 77, 32,116, 49, 32, 79, 82, 68, 69, 82, 32, 66, 89, 32, 98, 32, + 76, 73, 77, 73, 84, 32, 49, 48, 13, 1,163, 0, 4, 0, 40, 0, 1, 70, 0, + 233, 0,152, 0, 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,110, 23, 7, 21, 19, 19, 8,129, 71, + 118,105,101,119,118, 49, 50,118, 49, 50, 67, 82, 69, 65, 84, 69, 32, 86, 73, + 69, 87, 32,118, 49, 50, 40, 97, 44, 98, 44, 99, 44,100, 44,101, 41, 32, 65, + 83, 10, 32, 32, 83, 69, 76, 69, 67, 84, 32,115,117,109, 40, 97, 41, 44, 32, + 97,118,103, 40, 98, 41, 44, 32, 99,111,117,110,116, 40, 42, 41, 44, 32,109, + 105,110, 40,100, 41, 44, 32,101, 32, 70, 82, 79, 77, 32,116, 49, 32, 71, 82, + 79, 85, 80, 32, 66, 89, 32, 53, 79, 22, 7, 21, 19, 19, 8,129, 9,118,105, + 101,119,118, 53, 49,118, 53, 49, 67, 82, 69, 65, 84, 69, 32, 86, 73, 69, 87, + 32,118, 53, 49, 40, 97, 44, 98, 41, 32, 65, 83, 32, 83, 69, 76, 69, 67, 84, + 32, 97, 44, 98, 32, 70, 82, 79, 77, 32,116, 53, 32, 79, 82, 68, 69, 82, 32, + 66, 89, 32, 98, 32, 76, 73, 77, 73, 84, 32, 49, 48, 91, 21, 7, 21, 19, 19, + 8,129, 33,118,105,101,119,118, 52, 49,118, 52, 49, 67, 82, 69, 65, 84, 69, + 32, 86, 73, 69, 87, 32,118, 52, 49, 40, 97, 44, 98, 44, 99, 44,100, 44,101, + 41, 32, 65, 83, 32, 83, 69, 76, 69, 67, 84, 32, 97, 44, 98, 44, 99, 44,100, + 44,101, 32, 70, 82, 79, 77, 32,116, 52, 32, 79, 82, 68, 69, 82, 32, 66, 89, + 32, 98, 32, 76, 73, 77, 73, 84, 32, 49, 48, 91, 20, 7, 21, 19, 19, 8,129, + 33,118,105,101,119,118, 51, 49,118, 51, 49, 67, 82, 69, 65, 84, 69, 32, 86, + 73, 69, 87, 32,118, 51, 49, 40, 97, 44, 98, 44, 99, 44,100, 44,101, 41, 32, + 65, 83, 32, 83, 69, 76, 69, 67, 84, 32, 97, 44, 98, 44, 99, 44,100, 44,101, + 32, 70, 82, 79, 77, 32,116, 51, 32, 79, 82, 68, 69, 82, 32, 66, 89, 32, 98, + 32, 76, 73, 77, 73, 84, 32, 49, 48, 0, 0, 0, 93, 19, 19, 8,129, 33,118, + 105,101,119,118, 50, 49,118, 50, 49, 67, 82, 69, 65, 84, 69, 32, 86, 73, 69, + 87, 32,118, 50, 49, 40, 97, 44, 98, 44, 99, 44,100, 44,101, 41, 32, 65, 83, + 32, 83, 69, 76, 69, 67, 84, 32, 97, 44, 98, 44, 99, 44,100, 44,101, 32, 70, + 82, 79, 77, 32,116, 50, 32, 79, 82, 68, 69, 82, 32, 66, 89, 32, 98, 32, 76, + 73, 77, 73, 84, 32, 49, 48, 13, 0, 0, 0, 3, 0, 66, 0, 1,107, 0,214, + 0, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,129, 17, 26, + 7, 21, 19, 19, 8,130, 13,118,105,101,119,118, 52, 50,118, 52, 50, 67, 82, + 69, 65, 84, 69, 32, 86, 73, 69, 87, 32,118, 52, 50, 40, 97, 44, 98, 44, 99, + 44,100, 44,101, 41, 32, 65, 83, 10, 32, 32, 83, 69, 76, 69, 67, 84, 32,115, + 117,109, 40, 97, 41, 44, 32, 97,118,103, 40, 98, 41, 44, 32, 99,111,117,110, + 116, 40, 42, 41, 44, 32,109,105,110, 40,100, 41, 44, 32,101, 32, 70, 82, 79, + 77, 32,116, 52, 32, 71, 82, 79, 85, 80, 32, 66, 89, 32, 53, 10, 32, 32, 32, + 32, 72, 65, 86, 73, 78, 71, 32,109,105,110, 40,100, 41, 60, 51, 48, 32, 79, + 82, 68, 69, 82, 32, 66, 89, 32, 51, 44, 32, 49,129, 18, 25, 7, 21, 19, 19, + 8,130, 15,118,105,101,119,118, 51, 50,118, 51, 50, 67, 82, 69, 65, 84, 69, + 32, 86, 73, 69, 87, 32,118, 51, 50, 40, 97, 44, 98, 44, 99, 44,100, 44,101, + 41, 32, 65, 83, 10, 32, 32, 83, 69, 76, 69, 67, 84, 32,115,117,109, 40, 97, + 41, 44, 32, 97,118,103, 40, 98, 41, 44, 32, 99,111,117,110,116, 40, 42, 41, + 44, 32,109,105,110, 40,100, 41, 44, 32,101, 32, 70, 82, 79, 77, 32,116, 51, + 32, 71, 82, 79, 85, 80, 32, 66, 89, 32, 53, 10, 32, 32, 32, 32, 72, 65, 86, + 73, 78, 71, 32, 99,111,117,110,116, 40, 42, 41, 62, 49, 32, 79, 82, 68, 69, + 82, 32, 66, 89, 32, 51, 44, 32, 49,129, 18, 24, 7, 21, 19, 19, 8,130, 15, + 118,105,101,119,118, 50, 50,118, 50, 50, 67, 82, 69, 65, 84, 69, 32, 86, 73, + 69, 87, 32,118, 50, 50, 40, 97, 44, 98, 44, 99, 44,100, 44,101, 41, 32, 65, + 83, 10, 32, 32, 83, 69, 76, 69, 67, 84, 32,115,117,109, 40, 97, 41, 44, 32, + 97,118,103, 40, 98, 41, 44, 32, 99,111,117,110,116, 40, 42, 41, 44, 32,109, + 105,110, 40,100, 41, 44, 32,101, 32, 70, 82, 79, 77, 32,116, 50, 32, 71, 82, + 79, 85, 80, 32, 66, 89, 32, 53, 10, 32, 32, 32, 32, 72, 65, 86, 73, 78, 71, + 32, 99,111,117,110,116, 40, 42, 41, 62, 49, 32, 79, 82, 68, 69, 82, 32, 66, + 89, 32, 51, 44, 32, 49, 13, 1,108, 0, 3, 0, 83, 0, 0,225, 0, 83, 1, + 136, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,129, 11, 28, 7, 21, 19, + 19, 8,130, 1,118,105,101,119,118, 49, 51,118, 49, 51, 67, 82, 69, 65, 84, + 69, 32, 86, 73, 69, 87, 32,118, 49, 51, 40, 97, 44, 98, 44, 99, 44,100, 44, + 101, 41, 32, 65, 83, 10, 32, 32, 83, 69, 76, 69, 67, 84, 32, 97, 44, 98, 44, + 99, 44,100, 44,101, 32, 70, 82, 79, 77, 32,116, 49, 10, 32, 32, 85, 78, 73, + 79, 78, 32, 83, 69, 76, 69, 67, 84, 32, 97, 44, 98, 44, 99, 44,100, 44,101, + 32, 70, 82, 79, 77, 32,116, 50, 10, 32, 32, 85, 78, 73, 79, 78, 32, 83, 69, + 76, 69, 67, 84, 32, 97, 44, 98, 44, 99, 44,100, 44,101, 32, 70, 82, 79, 77, + 32,116, 51,129, 8, 27, 7, 21, 19, 19, 8,129,123,118,105,101,119,118, 53, + 50,118, 53, 50, 67, 82, 69, 65, 84, 69, 32, 86, 73, 69, 87, 32,118, 53, 50, + 40, 97, 44, 98, 44, 99, 44,100, 44,101, 41, 32, 65, 83, 10, 32, 32, 83, 69, + 76, 69, 67, 84, 32, 99,111,117,110,116, 40, 42, 41, 44, 32,109,105,110, 40, + 98, 41, 44, 32,115,117, 98,115,116,114, 40, 98, 44, 49, 44, 49, 41, 44, 32, + 109,105,110, 40, 97, 41, 44, 32,109, 97,120, 40, 97, 41, 32, 70, 82, 79, 77, + 32,116, 53, 10, 32, 32, 32, 71, 82, 79, 85, 80, 32, 66, 89, 32, 51, 32, 79, + 82, 68, 69, 82, 32, 66, 89, 32, 49, 0, 0, 0, 28, 21, 19, 19, 8,130, 13, + 118,105,101,119,118, 52, 50,118, 52, 50, 67, 82, 69, 65, 84, 69, 32, 86,118, + 29, 7, 21, 19, 19, 8,129, 87,118,105,101,119,118, 50, 51,118, 50, 51, 67, + 82, 69, 65, 84, 69, 32, 86, 73, 69, 87, 32,118, 50, 51, 40, 97, 44, 98, 44, + 99, 44,100, 44,101, 41, 32, 65, 83, 10, 32, 32, 83, 69, 76, 69, 67, 84, 32, + 97, 44, 98, 44, 99, 44,100, 44,101, 32, 70, 82, 79, 77, 32,116, 49, 10, 32, + 32, 69, 88, 67, 69, 80, 84, 32, 83, 69, 76, 69, 67, 84, 32, 97, 44, 98, 44, + 99, 44,100, 44,101, 32, 70, 82, 79, 77, 32,116, 49, 32, 87, 72, 69, 82, 69, + 32, 98, 60, 50, 53, 13, 0, 0, 0, 3, 0, 40, 0, 1,134, 1, 12, 0, 40, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0,129, 97, 32, 7, 21, 19, 19, 8,131, 45,118,105, + 101,119,118, 54, 50,118, 54, 50, 67, 82, 69, 65, 84, 69, 32, 86, 73, 69, 87, + 32,118, 54, 50, 40, 97, 44, 98, 44, 99, 44,100, 44,101, 41, 32, 65, 83, 10, + 32, 32, 83, 69, 76, 69, 67, 84, 32,116, 49, 46, 97, 44,116, 50, 46, 98, 44, + 116, 51, 46, 99, 44,116, 52, 46,100, 44,116, 53, 46, 98, 10, 32, 32, 32, 32, + 70, 82, 79, 77, 32,116, 49, 32, 74, 79, 73, 78, 32,116, 50, 32, 79, 78, 32, + 40,116, 49, 46, 97, 61,116, 50, 46, 98, 41, 10, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 74, 79, 73, 78, 32,116, 51, 32, 79, 78, 32, 40,116, 49, + 46, 97, 61,116, 51, 46, 97, 41, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 74, 79, 73, 78, 32,116, 52, 32, 79, 78, 32, 40,116, 52, 46, 98, 61, + 116, 51, 46, 98, 41, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 76, + 69, 70, 84, 32, 74, 79, 73, 78, 32,116, 53, 32, 79, 78, 32, 40,116, 53, 46, + 97, 61,116, 49, 46, 99, 41,120, 31, 7, 21, 19, 19, 8,129, 91,118,105,101, + 119,118, 54, 49,118, 54, 49, 67, 82, 69, 65, 84, 69, 32, 86, 73, 69, 87, 32, + 118, 54, 49, 40, 97, 44, 98, 44, 99, 44,100, 44,101, 41, 32, 65, 83, 10, 32, + 32, 83, 69, 76, 69, 67, 84, 32,116, 50, 46, 97, 44,116, 51, 46, 98, 44,116, + 50, 46, 99, 44,116, 51, 46,100, 44,116, 50, 46,101, 10, 32, 32, 32, 32, 70, + 82, 79, 77, 32,116, 50, 32, 76, 69, 70, 84, 32, 74, 79, 73, 78, 32,116, 51, + 32, 79, 78, 32, 40,116, 50, 46, 97, 61,116, 51, 46, 97, 41,120, 30, 7, 21, + 19, 19, 8,129, 91,118,105,101,119,118, 54, 48,118, 54, 48, 67, 82, 69, 65, + 84, 69, 32, 86, 73, 69, 87, 32,118, 54, 48, 40, 97, 44, 98, 44, 99, 44,100, + 44,101, 41, 32, 65, 83, 10, 32, 32, 83, 69, 76, 69, 67, 84, 32,116, 49, 46, + 97, 44,116, 50, 46, 98, 44,116, 49, 46, 99, 44,116, 50, 46,100, 44,116, 49, + 46,101, 10, 32, 32, 32, 32, 70, 82, 79, 77, 32,116, 49, 32, 76, 69, 70, 84, + 32, 74, 79, 73, 78, 32,116, 50, 32, 79, 78, 32, 40,116, 49, 46, 97, 61,116, + 50, 46, 98, 41, 13, 0, 0, 0, 1, 1, 73, 0, 1, 73, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,129, 52, 33, 7, 21, 19, 19, 8,130, + 83,118,105,101,119,118, 55, 48,118, 55, 48, 67, 82, 69, 65, 84, 69, 32, 86, + 73, 69, 87, 32,118, 55, 48, 40, 97, 44, 98, 44, 99, 44,100, 44,101, 41, 32, + 65, 83, 10, 32, 32, 87, 73, 84, 72, 32, 82, 69, 67, 85, 82, 83, 73, 86, 69, + 32, 99, 48, 40,120, 41, 32, 65, 83, 32, 40, 86, 65, 76, 85, 69, 83, 40, 49, + 41, 32, 85, 78, 73, 79, 78, 32, 65, 76, 76, 32, 83, 69, 76, 69, 67, 84, 32, + 120, 43, 49, 32, 70, 82, 79, 77, 32, 99, 48, 32, 87, 72, 69, 82, 69, 32,120, + 60, 57, 41, 10, 32, 32, 83, 69, 76, 69, 67, 84, 32,120, 44, 32, 98, 44, 32, + 99, 44, 32,100, 44, 32,101, 32, 70, 82, 79, 77, 32, 99, 48, 32, 74, 79, 73, + 78, 32,116, 49, 32, 79, 78, 32, 40,116, 49, 46, 97, 61, 53, 48, 45, 99, 48, + 46,120, 41, +}; diff --git a/test/optfuzz-db01.txt b/test/optfuzz-db01.txt new file mode 100644 index 0000000000..d9eef9320d --- /dev/null +++ b/test/optfuzz-db01.txt @@ -0,0 +1,142 @@ +-- Run this script through the sqlite3 command-line shell in order to generate +-- a database file containing lots of data for testing purposes. +-- +-- This script assumes that the "bin2c" program is available on ones $PATH. +-- The "bin2c" program reads a binary file and outputs C-code that creates +-- an array of bytes holding the content of that file. +-- +-- This script is designed to create many tables and views all having +-- 5 columns, "a" through "e", and with a variety of integers, short strings, +-- and NULL values. +-- +.open -new testdb01.db +PRAGMA page_size=512; +BEGIN; +CREATE TABLE t1(a INTEGER PRIMARY KEY, b INT, c INT, d INT, e INT); +WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<50) +INSERT INTO t1(a,b,c,d,e) SELECT x,abs(random()%51), + abs(random()%100), abs(random()%51), abs(random()%100) FROM c; +CREATE TABLE t2(a INT, b INT, c INT,d INT,e INT,PRIMARY KEY(b,a))WITHOUT ROWID; +INSERT INTO t2 SELECT * FROM t1; +CREATE TABLE t3(a,b,c,d,e); +INSERT INTO t3 SELECT a,b,c,d,e FROM t1 ORDER BY random() LIMIT 5; +INSERT INTO t3 SELECT null,b,c,d,e FROM t1 ORDER BY random() LIMIT 5; +INSERT INTO t3 SELECT a,null,c,d,e FROM t1 ORDER BY random() LIMIT 5; +INSERT INTO t3 SELECT a,b,null,d,e FROM t1 ORDER BY random() LIMIT 5; +INSERT INTO t3 SELECT a,b,c,null,e FROM t1 ORDER BY random() LIMIT 5; +INSERT INTO t3 SELECT a,b,c,d,null FROM t1 ORDER BY random() LIMIT 5; +INSERT INTO t3 SELECT null,null,null,null,null FROM t1 LIMIT 5; +CREATE INDEX t3x1 ON t3(a,b,c,d,e); +CREATE TABLE t4(a INT UNIQUE NOT NULL, b INT UNIQUE NOT NULL,c,d,e); +INSERT OR IGNORE INTO t4 SELECT a,b,c,d,e FROM t3; +CREATE TABLE t5(a INTEGER PRIMARY KEY, b TEXT UNIQUE,c,d,e); +INSERT INTO t5(b) VALUES + ('truth'), + ('works'), + ('offer'), + ('can'), + ('anger'), + ('wisdom'), + ('send'), + ('though'), + ('save'), + ('between'), + ('some'), + ('wine'), + ('ark'), + ('smote'), + ('therein'), + ('shew'), + ('morning'), + ('dwelt'), + ('begat'), + ('nothing'), + ('war'), + ('above'), + ('known'), + ('sacrifice'), + ('tell'), + ('departed'), + ('thyself'), + ('places'), + ('bear'), + ('part'), + ('while'), + ('gone'), + ('cubits'), + ('walk'), + ('long'), + ('near'), + ('serve'), + ('fruit'), + ('doth'), + ('poor'), + ('ways'), + ('child'), + ('temple'), + ('angel'), + ('inhabitants'), + ('oil'), + ('died'), + ('six'), + ('tree'), + ('wrath'); +UPDATE t1 SET e=(SELECT b FROM t5 WHERE t5.a=(t1.e%51)); +UPDATE t5 SET (c,d,e) = + (SELECT c,d,e FROM t1 WHERE t1.a=abs(t5.a+random()/100)%50+1); +UPDATE t2 SET e=(SELECT b FROM t5 WHERE t5.a=(t2.e%51)); +UPDATE t3 SET e=(SELECT b FROM t5 WHERE t5.a=t3.e); +CREATE INDEX t1e ON t1(e); +CREATE INDEX t2ed ON t2(e,d); +CREATE VIEW v00(a,b,c,d,e) AS SELECT 1,1,1,1,'one'; +CREATE VIEW v10(a,b,c,d,e) AS SELECT a,b,c,d,e FROM t1 WHERE a<>25; +CREATE VIEW v20(a,b,c,d,e) AS SELECT a,b,c,d,e FROM t2 WHERE a<>25; +CREATE VIEW v30(a,b,c,d,e) AS SELECT a,b,c,d,e FROM t3 WHERE a<>25; +CREATE VIEW v40(a,b,c,d,e) AS SELECT a,b,c,d,e FROM t4 WHERE a<>25; +CREATE VIEW v50(a,b) AS SELECT a,b FROM t5 WHERE a<>25; +CREATE VIEW v11(a,b,c,d,e) AS SELECT a,b,c,d,e FROM t1 ORDER BY b LIMIT 10; +CREATE VIEW v21(a,b,c,d,e) AS SELECT a,b,c,d,e FROM t2 ORDER BY b LIMIT 10; +CREATE VIEW v31(a,b,c,d,e) AS SELECT a,b,c,d,e FROM t3 ORDER BY b LIMIT 10; +CREATE VIEW v41(a,b,c,d,e) AS SELECT a,b,c,d,e FROM t4 ORDER BY b LIMIT 10; +CREATE VIEW v51(a,b) AS SELECT a,b FROM t5 ORDER BY b LIMIT 10; +CREATE VIEW v12(a,b,c,d,e) AS + SELECT sum(a), avg(b), count(*), min(d), e FROM t1 GROUP BY 5; +CREATE VIEW v22(a,b,c,d,e) AS + SELECT sum(a), avg(b), count(*), min(d), e FROM t2 GROUP BY 5 + HAVING count(*)>1 ORDER BY 3, 1; +CREATE VIEW v32(a,b,c,d,e) AS + SELECT sum(a), avg(b), count(*), min(d), e FROM t3 GROUP BY 5 + HAVING count(*)>1 ORDER BY 3, 1; +CREATE VIEW v42(a,b,c,d,e) AS + SELECT sum(a), avg(b), count(*), min(d), e FROM t4 GROUP BY 5 + HAVING min(d)<30 ORDER BY 3, 1; +CREATE VIEW v52(a,b,c,d,e) AS + SELECT count(*), min(b), substr(b,1,1), min(a), max(a) FROM t5 + GROUP BY 3 ORDER BY 1; + +CREATE VIEW v13(a,b,c,d,e) AS + SELECT a,b,c,d,e FROM t1 + UNION SELECT a,b,c,d,e FROM t2 + UNION SELECT a,b,c,d,e FROM t3; +CREATE VIEW v23(a,b,c,d,e) AS + SELECT a,b,c,d,e FROM t1 + EXCEPT SELECT a,b,c,d,e FROM t1 WHERE b<25; + +CREATE VIEW v60(a,b,c,d,e) AS + SELECT t1.a,t2.b,t1.c,t2.d,t1.e + FROM t1 LEFT JOIN t2 ON (t1.a=t2.b); +CREATE VIEW v61(a,b,c,d,e) AS + SELECT t2.a,t3.b,t2.c,t3.d,t2.e + FROM t2 LEFT JOIN t3 ON (t2.a=t3.a); +CREATE VIEW v62(a,b,c,d,e) AS + SELECT t1.a,t2.b,t3.c,t4.d,t5.b + FROM t1 JOIN t2 ON (t1.a=t2.b) + JOIN t3 ON (t1.a=t3.a) + JOIN t4 ON (t4.b=t3.b) + LEFT JOIN t5 ON (t5.a=t1.c); +CREATE VIEW v70(a,b,c,d,e) AS + WITH RECURSIVE c0(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c0 WHERE x<9) + SELECT x, b, c, d, e FROM c0 JOIN t1 ON (t1.a=50-c0.x); +COMMIT; +VACUUM; +.shell bin2c testdb01.db diff --git a/test/optfuzz.c b/test/optfuzz.c new file mode 100644 index 0000000000..2acb0ce350 --- /dev/null +++ b/test/optfuzz.c @@ -0,0 +1,308 @@ +/* +** 2018-03-21 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** This program attempts to verify the correctness of the SQLite query +** optimizer by fuzzing. +** +** The input is an SQL script, presumably generated by a fuzzer. The +** argument is the name of the input. If no files are named, standard +** input is read. +** +** The SQL script is run twice, once with optimization enabled, and again +** with optimization disabled. If the output is not equivalent, an error +** is printed and the program returns non-zero. +*/ + +/* Include the SQLite amalgamation, after making appropriate #defines. +*/ +#define SQLITE_THREADSAFE 0 +#define SQLITE_OMIT_LOAD_EXTENSION 1 +#include "sqlite3.c" + +/* Content of the read-only test database */ +#include "optfuzz-db01.c" + +/* +** Prepare a single SQL statement. Panic if anything goes wrong +*/ +static sqlite3_stmt *prepare_sql(sqlite3 *db, const char *zFormat, ...){ + char *zSql; + int rc; + sqlite3_stmt *pStmt = 0; + va_list ap; + + va_start(ap, zFormat); + zSql = sqlite3_vmprintf(zFormat, ap); + va_end(ap); + rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); + if( rc ){ + printf("Error: %s\nSQL: %s\n", + sqlite3_errmsg(db), zSql); + exit(1); + } + sqlite3_free(zSql); + return pStmt; +} + +/* +** Run SQL. Panic if anything goes wrong +*/ +static void run_sql(sqlite3 *db, const char *zFormat, ...){ + char *zSql; + int rc; + char *zErr = 0; + va_list ap; + + va_start(ap, zFormat); + zSql = sqlite3_vmprintf(zFormat, ap); + va_end(ap); + rc = sqlite3_exec(db, zSql, 0, 0, &zErr); + if( rc || zErr ){ + printf("Error: %s\nsqlite3_errmsg: %s\nSQL: %s\n", + zErr, sqlite3_errmsg(db), zSql); + exit(1); + } + sqlite3_free(zSql); +} + +/* +** Run one or more SQL statements contained in zSql against database dbRun. +** Store the input in database dbOut. +*/ +static int optfuzz_exec( + sqlite3 *dbRun, /* The database on which the SQL executes */ + const char *zSql, /* The SQL to be executed */ + sqlite3 *dbOut, /* Store results in this database */ + const char *zOutTab, /* Store results in this table of dbOut */ + int *pnStmt, /* Write the number of statements here */ + int *pnRow, /* Write the number of rows here */ + int bTrace /* Print query results if true */ +){ + int rc = SQLITE_OK; /* Return code */ + const char *zLeftover; /* Tail of unprocessed SQL */ + sqlite3_stmt *pStmt = 0; /* The current SQL statement */ + sqlite3_stmt *pIns = 0; /* Statement to insert into dbOut */ + const char *zCol; /* Single column value */ + int nCol; /* Number of output columns */ + char zLine[4000]; /* Complete row value */ + + run_sql(dbOut, "BEGIN"); + run_sql(dbOut, "CREATE TABLE IF NOT EXISTS staging(x TEXT)"); + run_sql(dbOut, "CREATE TABLE IF NOT EXISTS \"%w\"(x TEXT)", zOutTab); + pIns = prepare_sql(dbOut, "INSERT INTO staging(x) VALUES(?1)"); + *pnRow = *pnStmt = 0; + while( rc==SQLITE_OK && zSql && zSql[0] ){ + zLeftover = 0; + rc = sqlite3_prepare_v2(dbRun, zSql, -1, &pStmt, &zLeftover); + zSql = zLeftover; + assert( rc==SQLITE_OK || pStmt==0 ); + if( rc!=SQLITE_OK ){ + printf("Error with [%s]\n%s\n", zSql, sqlite3_errmsg(dbRun)); + break; + } + if( !pStmt ) continue; + (*pnStmt)++; + nCol = sqlite3_column_count(pStmt); + run_sql(dbOut, "DELETE FROM staging;"); + while( sqlite3_step(pStmt)==SQLITE_ROW ){ + int i, j; + for(i=j=0; i<nCol && j<sizeof(zLine)-50; i++){ + int eType = sqlite3_column_type(pStmt, i); + if( eType==SQLITE_NULL ){ + zCol = "NULL"; + }else{ + zCol = (const char*)sqlite3_column_text(pStmt, i); + } + if( i ) zLine[j++] = ','; + if( eType==SQLITE_TEXT ){ + sqlite3_snprintf(sizeof(zLine)-j, zLine+j, "'%q'", zCol); + }else{ + sqlite3_snprintf(sizeof(zLine)-j, zLine+j, "%s", zCol); + } + j += (int)strlen(zLine+j); + } + /* Detect if any row is too large and throw an error, because we will + ** want to go back and look more closely at that case */ + if( j>=sizeof(zLine)-100 ){ + printf("Excessively long output line: %d bytes\n" ,j); + exit(1); + } + if( bTrace ){ + printf("%s\n", zLine); + } + (*pnRow)++; + sqlite3_bind_text(pIns, 1, zLine, j, SQLITE_TRANSIENT); + rc = sqlite3_step(pIns); + assert( rc==SQLITE_DONE ); + rc = sqlite3_reset(pIns); + } + run_sql(dbOut, + "INSERT INTO \"%w\"(x) VALUES('### %q ###')", + zOutTab, sqlite3_sql(pStmt) + ); + run_sql(dbOut, + "INSERT INTO \"%w\"(x) SELECT group_concat(x,char(10))" + " FROM (SELECT x FROM staging ORDER BY x)", + zOutTab + ); + run_sql(dbOut, "COMMIT"); + sqlite3_finalize(pStmt); + pStmt = 0; + } + sqlite3_finalize(pStmt); + sqlite3_finalize(pIns); + return rc; +} + +/* +** Read the content of file zName into memory obtained from sqlite3_malloc64() +** and return a pointer to the buffer. The caller is responsible for freeing +** the memory. +** +** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes +** read. +** +** For convenience, a nul-terminator byte is always appended to the data read +** from the file before the buffer is returned. This byte is not included in +** the final value of (*pnByte), if applicable. +** +** NULL is returned if any error is encountered. The final value of *pnByte +** is undefined in this case. +*/ +static char *readFile(const char *zName, int *pnByte){ + FILE *in = fopen(zName, "rb"); + long nIn; + size_t nRead; + char *pBuf; + if( in==0 ) return 0; + fseek(in, 0, SEEK_END); + nIn = ftell(in); + rewind(in); + pBuf = sqlite3_malloc64( nIn+1 ); + if( pBuf==0 ) return 0; + nRead = fread(pBuf, nIn, 1, in); + fclose(in); + if( nRead!=1 ){ + sqlite3_free(pBuf); + return 0; + } + pBuf[nIn] = 0; + if( pnByte ) *pnByte = nIn; + return pBuf; +} + +int main(int argc, char **argv){ + int nIn = 0; /* Number of input files */ + char **azIn = 0; /* Names of input files */ + sqlite3 *dbOut = 0; /* Database to hold results */ + sqlite3 *dbRun = 0; /* Database used for tests */ + int bTrace = 0; /* Show query results */ + int bShowValid = 0; /* Just list inputs that are valid SQL */ + int nRow, nStmt; /* Number of rows and statements */ + int i, rc; + + for(i=1; i<argc; i++){ + const char *z = argv[i]; + if( z[0]=='-' && z[1]=='-' ) z++; + if( strcmp(z,"-help")==0 ){ + printf("Usage: %s [OPTIONS] FILENAME ...\n", argv[0]); + printf("Options:\n"); + printf(" --help Show his message\n"); + printf(" --output-trace Show each line of SQL output\n"); + printf(" --valid-sql List FILEs that are valid SQL\n"); + return 0; + } + else if( strcmp(z,"-output-trace")==0 ){ + bTrace = 1; + } + else if( strcmp(z,"-valid-sql")==0 ){ + bShowValid = 1; + } + else if( z[0]=='-' ){ + printf("unknown option \"%s\". Use --help for details\n", argv[i]); + return 1; + } + else { + nIn++; + azIn = realloc(azIn, sizeof(azIn[0])*nIn); + if( azIn==0 ){ + printf("out of memory\n"); + exit(1); + } + azIn[nIn-1] = argv[i]; + } + } + + sqlite3_open(":memory:", &dbOut); + sqlite3_open(":memory:", &dbRun); + sqlite3_deserialize(dbRun, "main", data001, sizeof(data001), + sizeof(data001), SQLITE_DESERIALIZE_READONLY); + for(i=0; i<nIn; i++){ + char *zSql = readFile(azIn[i], 0); + sqlite3_stmt *pCk; + sqlite3_exec(dbRun, "ROLLBACK", 0, 0, 0); + if( bShowValid ){ + rc = sqlite3_exec(dbRun, zSql, 0, 0, 0); + if( rc==SQLITE_OK ) printf("%s\n", azIn[i]); + sqlite3_free(zSql); + continue; + } + sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, dbRun, 0); + if( bTrace ) printf("%s: Optimized\n", azIn[i]); + rc = optfuzz_exec(dbRun, zSql, dbOut, "opt", &nStmt, &nRow, bTrace); + if( rc ){ + printf("%s: optimized run failed: %s\n", + azIn[i], sqlite3_errmsg(dbRun)); + }else{ + sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, dbRun, 0xffff); + if( bTrace ) printf("%s: Non-optimized\n", azIn[i]); + rc = optfuzz_exec(dbRun, zSql, dbOut, "noopt", &nStmt, &nRow, bTrace); + if( rc ){ + printf("%s: non-optimized run failed: %s\n", + azIn[i], sqlite3_errmsg(dbRun)); + exit(1); + } + pCk = prepare_sql(dbOut, + "SELECT (SELECT group_concat(x,char(10)) FROM opt)==" + " (SELECT group_concat(x,char(10)) FROM noopt)"); + rc = sqlite3_step(pCk); + if( rc!=SQLITE_ROW ){ + printf("%s: comparison failed\n", sqlite3_errmsg(dbOut)); + exit(1); + } + if( !sqlite3_column_int(pCk, 0) ){ + printf("%s: opt/no-opt outputs differ\n", azIn[i]); + pCk = prepare_sql(dbOut, + "SELECT group_concat(x,char(10)) FROM opt " + "UNION ALL " + "SELECT group_concat(x,char(10)) FROM noopt"); + sqlite3_step(pCk); + printf("opt:\n%s\n", sqlite3_column_text(pCk,0)); + sqlite3_step(pCk); + printf("noopt:\n%s\n", sqlite3_column_text(pCk,0)); + exit(1); + }else{ + printf("%s: %d stmts %d rows ok\n", azIn[i], nStmt, nRow); + } + sqlite3_finalize(pCk); + } + sqlite3_free(zSql); + } + sqlite3_close(dbRun); + sqlite3_close(dbOut); + free(azIn); + if( sqlite3_memory_used() ){ + printf("Memory leak of %lld bytes\n", sqlite3_memory_used()); + exit(1); + } + return 0; +} diff --git a/test/orderby1.test b/test/orderby1.test index 831936ae96..73eda83992 100644 --- a/test/orderby1.test +++ b/test/orderby1.test @@ -43,6 +43,7 @@ do_test 1.0 { (NULL, 1, 3, 'one-c'), (NULL, 2, 1, 'two-a'), (NULL, 3, 1, 'three-a'); + ANALYZE; COMMIT; } } {} @@ -180,6 +181,7 @@ do_test 2.0 { (1, 3, 'one-c'), (20, 1, 'two-a'), (3, 1, 'three-a'); + ANALYZE; COMMIT; } } {} @@ -327,6 +329,7 @@ do_test 3.0 { (NULL, 1, 3, 'one-c'), (NULL, 2, 1, 'two-a'), (NULL, 3, 1, 'three-a'); + ANALYZE; COMMIT; } } {} @@ -454,9 +457,12 @@ do_test 4.0 { # No sorting of queries that omit the FROM clause. # -do_execsql_test 5.0 { - EXPLAIN QUERY PLAN SELECT 5 ORDER BY 1 -} {} +do_eqp_test 5.0 { + SELECT 5 ORDER BY 1 +} { + QUERY PLAN + `--SCAN CONSTANT ROW +} do_execsql_test 5.1 { EXPLAIN QUERY PLAN SELECT 5 UNION ALL SELECT 3 ORDER BY 1 } {~/B-TREE/} @@ -512,8 +518,9 @@ do_execsql_test 8.0 { do_eqp_test 8.1 { SELECT * FROM t1 ORDER BY a, b; } { - 0 0 0 {SCAN TABLE t1 USING INDEX i1} - 0 0 0 {USE TEMP B-TREE FOR RIGHT PART OF ORDER BY} + QUERY PLAN + |--SCAN t1 USING INDEX i1 + `--USE TEMP B-TREE FOR LAST TERM OF ORDER BY } do_execsql_test 8.2 { @@ -529,7 +536,7 @@ do_test 8.3 { } 5000 #--------------------------------------------------------------------------- -# https://www.sqlite.org/src/tktview/cb3aa0641d9a413841c004293a4fc06cdc122029 +# https://sqlite.org/src/tktview/cb3aa0641d9a413841c004293a4fc06cdc122029 # # Adverse interaction between scalar subqueries and the partial-sorting # logic. @@ -544,5 +551,19 @@ do_execsql_test 9.0 { SELECT (SELECT x||y FROM t2, t1 ORDER BY x, y); } {13} +# Problem found by OSSFuzz on 2018-05-05. This was caused by a new +# optimization that had not been previously released. +# +do_execsql_test 10.0 { + CREATE TABLE t10(a,b); + INSERT INTO t10 VALUES(1,2),(8,9),(3,4),(5,4),(0,7); + CREATE INDEX t10b ON t10(b); + SELECT b, rowid, '^' FROM t10 ORDER BY b, a LIMIT 4; +} {2 1 ^ 4 3 ^ 4 4 ^ 7 5 ^} + +do_catchsql_test 11.0 { + VALUES(2) EXCEPT SELECT '' ORDER BY abc +} {1 {1st ORDER BY term does not match any column in the result set}} + finish_test diff --git a/test/orderby3.test b/test/orderby3.test index f005f0d2e8..09e9156d0f 100644 --- a/test/orderby3.test +++ b/test/orderby3.test @@ -11,7 +11,7 @@ # This file implements regression tests for SQLite library. The # focus of this file is testing that the optimizations that disable # ORDER BY clauses work correctly on a 3-way join. See ticket -# http://www.sqlite.org/src/956e4d7f89 +# http://sqlite.org/src/956e4d7f89 # diff --git a/test/orderby4.test b/test/orderby4.test index ec6eb041f6..944a91f37d 100644 --- a/test/orderby4.test +++ b/test/orderby4.test @@ -12,7 +12,7 @@ # focus of this file is testing that the optimizations that disable # ORDER BY clauses work correctly on multi-value primary keys and # unique indices when only some prefix of the terms in the key are -# used. See ticket http://www.sqlite.org/src/info/a179fe74659 +# used. See ticket http://sqlite.org/src/info/a179fe74659 # diff --git a/test/orderby5.test b/test/orderby5.test index c9cce703ba..ccdcf1de0c 100644 --- a/test/orderby5.test +++ b/test/orderby5.test @@ -83,7 +83,7 @@ do_execsql_test 2.1a { do_execsql_test 2.1b { EXPLAIN QUERY PLAN - SELECT * FROM t1 WHERE likelihood(a=0, 0.05) ORDER BY a, b, c; + SELECT * FROM t1 WHERE likelihood(a=0, 0.03) ORDER BY a, b, c; } {/B-TREE/} do_execsql_test 2.2 { @@ -126,5 +126,62 @@ do_execsql_test 3.1 { SELECT a FROM t3 WHERE b=2 AND c=3 ORDER BY d DESC, e DESC, b, c, a DESC; } {~/B-TREE/} +#------------------------------------------------------------------------- +do_execsql_test 4.1.0 { + CREATE TABLE t4(b COLLATE nocase); + INSERT INTO t4 VALUES('abc'); + INSERT INTO t4 VALUES('ABC'); + INSERT INTO t4 VALUES('aBC'); +} +do_execsql_test 4.1.1 { + SELECT * FROM t4 ORDER BY b COLLATE binary +} {ABC aBC abc} +do_execsql_test 4.1.2 { + SELECT * FROM t4 WHERE b='abc' ORDER BY b COLLATE binary +} {ABC aBC abc} + +do_execsql_test 4.2.1 { + CREATE TABLE Records(typeID INTEGER, key TEXT COLLATE nocase, value TEXT); + CREATE INDEX RecordsIndex ON Records(typeID, key, value); +} +do_execsql_test 4.2.2 { + explain query plan + SELECT typeID, key, value FROM Records + WHERE typeID = 2 AND key = 'x' + ORDER BY key, value; +} {~/TEMP B-TREE/} +do_execsql_test 4.2.3 { + explain query plan + SELECT typeID, key, value FROM Records + WHERE typeID = 2 AND (key = 'x' COLLATE binary) + ORDER BY key, value; +} {~/TEMP B-TREE/} +do_execsql_test 4.2.4 { + explain query plan + SELECT typeID, key, value FROM Records + WHERE typeID = 2 + ORDER BY key, value; +} {~/TEMP B-TREE/} + +db collate hello [list string match] +do_execsql_test 4.3.1 { + CREATE TABLE t5(a INTEGER PRIMARY KEY, b COLLATE hello, c, d); +} +db close +sqlite3 db test.db +do_catchsql_test 4.3.2 { + SELECT a FROM t5 WHERE b='def' ORDER BY b; +} {1 {no such collation sequence: hello}} + +# 2020-02-13 ticket 41c1456a6e61c0e7 +do_execsql_test 4.4.0 { + DROP TABLE t1; + CREATE TABLE t1(a); + DROP TABLE t2; + CREATE TABLE t2(b INTEGER PRIMARY KEY, c INT); + SELECT DISTINCT * + FROM t1 LEFT JOIN t2 ON b=c AND b=(SELECT a FROM t1) + WHERE c>10; +} {} finish_test diff --git a/test/orderbyA.test b/test/orderbyA.test new file mode 100644 index 0000000000..4400f4e05f --- /dev/null +++ b/test/orderbyA.test @@ -0,0 +1,147 @@ +# 2019-09-21 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. +# +# Specifically, it tests cases where the expressions in a GROUP BY +# clause are the same as those in the ORDER BY clause. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set ::testprefix orderbyA + +proc do_sortcount_test {tn sql cnt res} { + set eqp [execsql "EXPLAIN QUERY PLAN $sql"] + set rcnt [regexp -all {USE TEMP} $eqp] + uplevel [list do_test $tn.1 [list set {} $rcnt] $cnt] + uplevel [list do_execsql_test $tn.2 $sql $res] +} + +do_execsql_test 1.0 { + CREATE TABLE t1(a, b, c); + INSERT INTO t1 VALUES('one', 1, 11); + INSERT INTO t1 VALUES('three', 7, 11); + INSERT INTO t1 VALUES('one', 2, 11); + INSERT INTO t1 VALUES('one', 3, 11); + INSERT INTO t1 VALUES('two', 4, 11); + INSERT INTO t1 VALUES('two', 6, 11); + INSERT INTO t1 VALUES('three', 8, 11); + INSERT INTO t1 VALUES('two', 5, 11); + INSERT INTO t1 VALUES('three', 9, 11); +} + +foreach {tn idx} { + 1 {} + 2 {CREATE INDEX i1 ON t1(a)} + 3 {CREATE INDEX i1 ON t1(a DESC)} +} { + execsql { DROP INDEX IF EXISTS i1 } + execsql $idx + + # $match is the number of temp-table sorts we expect if the GROUP BY + # can use the same sort order as the ORDER BY. $nomatch is the number + # of expected sorts if the GROUP BY and ORDER BY are not compatible. + set match 1 + set nomatch 2 + if {$tn>=2} { + set match 0 + set nomatch 1 + } + + do_sortcount_test 1.$tn.1.1 { + SELECT a, sum(b) FROM t1 GROUP BY a ORDER BY a + } $match {one 6 three 24 two 15} + do_sortcount_test 1.$tn.1.2 { + SELECT a, sum(b) FROM t1 GROUP BY a ORDER BY a DESC + } $match {two 15 three 24 one 6} + + do_sortcount_test 1.$tn.2.1 { + SELECT a, sum(b) FROM t1 GROUP BY a ORDER BY a||'' + } $nomatch {one 6 three 24 two 15} + do_sortcount_test 1.$tn.2.2 { + SELECT a, sum(b) FROM t1 GROUP BY a ORDER BY a||'' DESC + } $nomatch {two 15 three 24 one 6} + + do_sortcount_test 1.$tn.3.1 { + SELECT a, sum(b) FROM t1 GROUP BY a ORDER BY a NULLS LAST + } $nomatch {one 6 three 24 two 15} + do_sortcount_test 1.$tn.3.2 { + SELECT a, sum(b) FROM t1 GROUP BY a ORDER BY a DESC NULLS FIRST + } $nomatch {two 15 three 24 one 6} +} + +#------------------------------------------------------------------------- +do_execsql_test 2.0 { + CREATE TABLE t2(a, b, c); + INSERT INTO t2 VALUES(1, 'one', 1); + INSERT INTO t2 VALUES(1, 'two', 2); + INSERT INTO t2 VALUES(1, 'one', 3); + INSERT INTO t2 VALUES(1, 'two', 4); + INSERT INTO t2 VALUES(1, 'one', 5); + INSERT INTO t2 VALUES(1, 'two', 6); + + INSERT INTO t2 VALUES(2, 'one', 7); + INSERT INTO t2 VALUES(2, 'two', 8); + INSERT INTO t2 VALUES(2, 'one', 9); + INSERT INTO t2 VALUES(2, 'two', 10); + INSERT INTO t2 VALUES(2, 'one', 11); + INSERT INTO t2 VALUES(2, 'two', 12); + + INSERT INTO t2 VALUES(NULL, 'one', 13); + INSERT INTO t2 VALUES(NULL, 'two', 14); + INSERT INTO t2 VALUES(NULL, 'one', 15); + INSERT INTO t2 VALUES(NULL, 'two', 16); + INSERT INTO t2 VALUES(NULL, 'one', 17); + INSERT INTO t2 VALUES(NULL, 'two', 18); +} + +foreach {tn idx} { + 1 {} + + 2 { CREATE INDEX i2 ON t2(a, b) } + 3 { CREATE INDEX i2 ON t2(a DESC, b DESC) } + + 4 { CREATE INDEX i2 ON t2(a, b DESC) } + 5 { CREATE INDEX i2 ON t2(a DESC, b) } +} { + execsql { DROP INDEX IF EXISTS i2 } + execsql $idx + + + set nSort [expr ($tn==2 || $tn==3) ? 0 : 1] + do_sortcount_test 2.$tn.1.1 { + SELECT a, b, sum(c) FROM t2 GROUP BY a, b ORDER BY a, b; + } $nSort {{} one 45 {} two 48 1 one 9 1 two 12 2 one 27 2 two 30} + do_sortcount_test 2.$tn.1.2 { + SELECT a, b, sum(c) FROM t2 GROUP BY a, b ORDER BY a DESC, b DESC; + } $nSort {2 two 30 2 one 27 1 two 12 1 one 9 {} two 48 {} one 45} + + set nSort [expr ($tn==4 || $tn==5) ? 0 : 1] + do_sortcount_test 2.$tn.2.1 { + SELECT a, b, sum(c) FROM t2 GROUP BY a, b ORDER BY a, b DESC; + } $nSort { {} two 48 {} one 45 1 two 12 1 one 9 2 two 30 2 one 27 } + do_sortcount_test 2.$tn.2.2 { + SELECT a, b, sum(c) FROM t2 GROUP BY a, b ORDER BY a DESC, b; + } $nSort { 2 one 27 2 two 30 1 one 9 1 two 12 {} one 45 {} two 48 } + + # ORDER BY can never piggyback on the GROUP BY sort if it uses + # non-standard NULLS behaviour. + set nSort [expr $tn==1 ? 2 : 1] + do_sortcount_test 2.$tn.3.1 { + SELECT a, b, sum(c) FROM t2 GROUP BY a, b ORDER BY a, b DESC NULLS FIRST; + } $nSort { {} two 48 {} one 45 1 two 12 1 one 9 2 two 30 2 one 27 } + do_sortcount_test 2.$tn.3.2 { + SELECT a, b, sum(c) FROM t2 GROUP BY a, b ORDER BY a DESC, b NULLS LAST; + } $nSort { 2 one 27 2 two 30 1 one 9 1 two 12 {} one 45 {} two 48 } +} + + +finish_test diff --git a/test/orderbyB.test b/test/orderbyB.test new file mode 100644 index 0000000000..42d2de7982 --- /dev/null +++ b/test/orderbyB.test @@ -0,0 +1,94 @@ +# 2024-08-15 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. +# +# Specifically, it tests cases with order-by-subquery optimization in which +# an ORDER BY in a subquery is used to help resolve an ORDER BY in the +# outer query without having to do an extra sort. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set ::testprefix orderbyb + +db null NULL +do_execsql_test 1.0 { + CREATE TABLE t1(a TEXT, b TEXT, c INT); + INSERT INTO t1 VALUES(NULL,NULL,NULL); + WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<7) + INSERT INTO t1(a,b,c) SELECT char(p,p), char(q,q), n FROM + (SELECT ((n-1)%4)+0x61 AS p, abs(n*2-9+(n>=5))+0x60 AS q, n FROM c); + UPDATE t1 SET b=upper(b) WHERE c=1; + CREATE TABLE t2(k TEXT PRIMARY KEY, v INT) WITHOUT ROWID; + WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<7) + INSERT INTO t2(k,v) SELECT char(0x60+n,0x60+n), n FROM c; + WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<7) + INSERT INTO t2(k,v) SELECT char(0x40+n,0x40+n), n FROM c; + SELECT a,b,c,tx.v AS 'v-a', ty.v AS 'v-b' + FROM t1 LEFT JOIN t2 AS tx ON tx.k=a + LEFT JOIN t2 AS ty ON ty.k=b + ORDER BY c; +} { + NULL NULL NULL NULL NULL + aa GG 1 1 7 + bb ee 2 2 5 + cc cc 3 3 3 + dd aa 4 4 1 + aa bb 5 1 2 + bb dd 6 2 4 + cc ff 7 3 6 +} + +do_eqp_execsql_test 1.1 { + WITH t3(x,y) AS (SELECT a, b FROM t1 ORDER BY a, b LIMIT 8) + SELECT x, y, v FROM t3 LEFT JOIN t2 ON k=t3.y ORDER BY x, y COLLATE nocase; +} { + QUERY PLAN + |--CO-ROUTINE t3 + | |--SCAN t1 + | `--USE TEMP B-TREE FOR ORDER BY + |--SCAN t3 + |--SEARCH t2 USING PRIMARY KEY (k=?) LEFT-JOIN + `--USE TEMP B-TREE FOR LAST TERM OF ORDER BY +} { + NULL NULL NULL + aa bb 2 + aa GG 7 + bb dd 4 + bb ee 5 + cc cc 3 + cc ff 6 + dd aa 1 +} + +do_eqp_execsql_test 1.2 { + WITH t3(x,y) AS MATERIALIZED (SELECT a, b COLLATE nocase FROM t1 ORDER BY 1,2) + SELECT x, y, v FROM t3 LEFT JOIN t2 ON k=t3.y ORDER BY x,y; +} { + QUERY PLAN + |--MATERIALIZE t3 + | |--SCAN t1 + | `--USE TEMP B-TREE FOR ORDER BY + |--SCAN t3 + `--SEARCH t2 USING PRIMARY KEY (k=?) LEFT-JOIN +} { + NULL NULL NULL + aa bb 2 + aa GG 7 + bb dd 4 + bb ee 5 + cc cc 3 + cc ff 6 + dd aa 1 +} + + +finish_test diff --git a/test/oserror.test b/test/oserror.test index 246a9d4023..43d72569d9 100644 --- a/test/oserror.test +++ b/test/oserror.test @@ -16,7 +16,10 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl -if {$::tcl_platform(platform)!="unix"} { finish_test ; return } +if {[llength [info commands test_syscall]]==0} { + finish_test + return +} set ::testprefix oserror db close @@ -52,18 +55,32 @@ proc do_re_test {tn script expression} { # an error may be reported for either open() or getcwd() here. # if {![clang_sanitize_address]} { + unset -nocomplain rc + unset -nocomplain nOpen + set nOpen 20000 do_test 1.1.1 { set ::log [list] - list [catch { - for {set i 0} {$i < 2000} {incr i} { sqlite3 dbh_$i test.db -readonly 1 } - } msg] $msg - } {1 {unable to open database file}} + set ::rc [catch { + for {set i 0} {$i < $::nOpen} {incr i} { sqlite3 dbh_$i test.db -readonly 1 } + } msg] + if {$::rc==0} { + # Some system (ex: Debian) are able to create 20000+ file descriptiors + # such systems will not fail here + set x ok + } elseif {$::rc==1 && $msg=="unable to open database file"} { + set x ok + } else { + set x [list $::rc $msg] + } + } {ok} do_test 1.1.2 { - catch { for {set i 0} {$i < 2000} {incr i} { dbh_$i close } } - } {1} - do_re_test 1.1.3 { - lindex $::log 0 - } {^os_unix.c:\d+: \(\d+\) (open|getcwd)\(.*test.db\) - } + catch { for {set i 0} {$i < $::nOpen} {incr i} { dbh_$i close } } + } $::rc + if {$rc} { + do_re_test 1.1.3 { + lindex $::log 0 + } {^os_unix.c:\d+: \(\d+\) (open|getcwd)\(.*test.db\) - } + } } diff --git a/test/ossfuzz.c b/test/ossfuzz.c new file mode 100644 index 0000000000..8e80b98ef6 --- /dev/null +++ b/test/ossfuzz.c @@ -0,0 +1,206 @@ +/* +** This module interfaces SQLite to the Google OSS-Fuzz, fuzzer as a service. +** (https://github.com/google/oss-fuzz) +*/ +#include <stddef.h> +#if !defined(_MSC_VER) +# include <stdint.h> +#endif +#include <stdio.h> +#include <string.h> +#include "sqlite3.h" + +#if defined(_MSC_VER) +typedef unsigned char uint8_t; +#endif + +/* Global debugging settings. OSS-Fuzz will have all debugging turned +** off. But if LLVMFuzzerTestOneInput() is called interactively from +** the ossshell utility program, then these flags might be set. +*/ +static unsigned mDebug = 0; +#define FUZZ_SQL_TRACE 0x0001 /* Set an sqlite3_trace() callback */ +#define FUZZ_SHOW_MAX_DELAY 0x0002 /* Show maximum progress callback delay */ +#define FUZZ_SHOW_ERRORS 0x0004 /* Print error messages from SQLite */ + +/* The ossshell utility program invokes this interface to see the +** debugging flags. Unused by OSS-Fuzz. +*/ +void ossfuzz_set_debug_flags(unsigned x){ + mDebug = x; +} + +/* Return the current real-world time in milliseconds since the +** Julian epoch (-4714-11-24). +*/ +static sqlite3_int64 timeOfDay(void){ + static sqlite3_vfs *clockVfs = 0; + sqlite3_int64 t; + if( clockVfs==0 ){ + clockVfs = sqlite3_vfs_find(0); + if( clockVfs==0 ) return 0; + } + if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){ + clockVfs->xCurrentTimeInt64(clockVfs, &t); + }else{ + double r; + clockVfs->xCurrentTime(clockVfs, &r); + t = (sqlite3_int64)(r*86400000.0); + } + return t; +} + +/* An instance of the following object is passed by pointer as the +** client data to various callbacks. +*/ +typedef struct FuzzCtx { + sqlite3 *db; /* The database connection */ + sqlite3_int64 iCutoffTime; /* Stop processing at this time. */ + sqlite3_int64 iLastCb; /* Time recorded for previous progress callback */ + sqlite3_int64 mxInterval; /* Longest interval between two progress calls */ + unsigned nCb; /* Number of progress callbacks */ + unsigned execCnt; /* Number of calls to the sqlite3_exec callback */ +} FuzzCtx; + +/* +** Progress handler callback. +** +** The argument is the cutoff-time after which all processing should +** stop. So return non-zero if the cut-off time is exceeded. +*/ +static int progress_handler(void *pClientData) { + FuzzCtx *p = (FuzzCtx*)pClientData; + sqlite3_int64 iNow = timeOfDay(); + int rc = iNow>=p->iCutoffTime; + sqlite3_int64 iDiff = iNow - p->iLastCb; + if( iDiff > p->mxInterval ) p->mxInterval = iDiff; + p->nCb++; + return rc; +} + +/* +** Disallow debugging pragmas such as "PRAGMA vdbe_debug" and +** "PRAGMA parser_trace" since they can dramatically increase the +** amount of output without actually testing anything useful. +*/ +static int block_debug_pragmas( + void *Notused, + int eCode, + const char *zArg1, + const char *zArg2, + const char *zArg3, + const char *zArg4 +){ + if( eCode==SQLITE_PRAGMA + && (sqlite3_strnicmp("vdbe_", zArg1, 5)==0 + || sqlite3_stricmp("parser_trace", zArg1)==0) + ){ + return SQLITE_DENY; + } + return SQLITE_OK; +} + +/* +** Callback for sqlite3_exec(). +*/ +static int exec_handler(void *pClientData, int argc, char **argv, char **namev){ + FuzzCtx *p = (FuzzCtx*)pClientData; + int i; + if( argv ){ + for(i=0; i<argc; i++) sqlite3_free(sqlite3_mprintf("%s", argv[i])); + } + return (p->execCnt--)<=0 || progress_handler(pClientData); +} + +/* +** Main entry point. The fuzzer invokes this function with each +** fuzzed input. +*/ +int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { + char *zErrMsg = 0; /* Error message returned by sqlite_exec() */ + uint8_t uSelector; /* First byte of input data[] */ + int rc; /* Return code from various interfaces */ + char *zSql; /* Zero-terminated copy of data[] */ + FuzzCtx cx; /* Fuzzing context */ + + memset(&cx, 0, sizeof(cx)); + if( size<3 ) return 0; /* Early out if unsufficient data */ + + /* Extract the selector byte from the beginning of the input. But only + ** do this if the second byte is a \n. If the second byte is not \n, + ** then use a default selector */ + if( data[1]=='\n' ){ + uSelector = data[0]; data += 2; size -= 2; + }else{ + uSelector = 0xfd; + } + + /* Open the database connection. Only use an in-memory database. */ + if( sqlite3_initialize() ) return 0; + rc = sqlite3_open_v2("fuzz.db", &cx.db, + SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_MEMORY, 0); + if( rc ) return 0; + + /* Invoke the progress handler frequently to check to see if we + ** are taking too long. The progress handler will return true + ** (which will block further processing) if more than 10 seconds have + ** elapsed since the start of the test. + */ + cx.iLastCb = timeOfDay(); + cx.iCutoffTime = cx.iLastCb + 10000; /* Now + 10 seconds */ +#ifndef SQLITE_OMIT_PROGRESS_CALLBACK + sqlite3_progress_handler(cx.db, 10, progress_handler, (void*)&cx); +#endif + + /* Set a limit on the maximum size of a prepared statement */ + sqlite3_limit(cx.db, SQLITE_LIMIT_VDBE_OP, 25000); + + /* Set a limit on the maximum LIKE or GLOB pattern length due to + ** https://issues.oss-fuzz.com/issues/453240497. The default is 50K + ** which is causing timeouts in OSS-Fuzz */ + sqlite3_limit(cx.db, SQLITE_LIMIT_LIKE_PATTERN_LENGTH, 250); + + /* Limit total memory available to SQLite to 20MB */ + sqlite3_hard_heap_limit64(20000000); + + /* Set a limit on the maximum length of a string or BLOB. Without this + ** limit, fuzzers will invoke randomblob(N) for a large N, and the process + ** will timeout trying to generate the huge blob */ + sqlite3_limit(cx.db, SQLITE_LIMIT_LENGTH, 50000); + + /* Bit 1 of the selector enables foreign key constraints */ + sqlite3_db_config(cx.db, SQLITE_DBCONFIG_ENABLE_FKEY, uSelector&1, &rc); + uSelector >>= 1; + + /* Do not allow debugging pragma statements that might cause excess output */ + sqlite3_set_authorizer(cx.db, block_debug_pragmas, 0); + + /* Remaining bits of the selector determine a limit on the number of + ** output rows */ + cx.execCnt = uSelector + 1; + + /* Run the SQL. The sqlite_exec() interface expects a zero-terminated + ** string, so make a copy. */ + zSql = sqlite3_mprintf("%.*s", (int)size, data); +#ifndef SQLITE_OMIT_COMPLETE + sqlite3_complete(zSql); +#endif + sqlite3_exec(cx.db, zSql, exec_handler, (void*)&cx, &zErrMsg); + + /* Show any errors */ + if( (mDebug & FUZZ_SHOW_ERRORS)!=0 && zErrMsg ){ + printf("Error: %s\n", zErrMsg); + } + + /* Cleanup and return */ + sqlite3_free(zErrMsg); + sqlite3_free(zSql); + sqlite3_exec(cx.db, "PRAGMA temp_store_directory=''", 0, 0, 0); + sqlite3_close(cx.db); + + if( mDebug & FUZZ_SHOW_MAX_DELAY ){ + printf("Progress callback count....... %d\n", cx.nCb); + printf("Max time between callbacks.... %d ms\n", (int)cx.mxInterval); + } + return 0; +} diff --git a/test/ossshell.c b/test/ossshell.c new file mode 100644 index 0000000000..54849f97f6 --- /dev/null +++ b/test/ossshell.c @@ -0,0 +1,100 @@ +/* +** This is a test interface for the ossfuzz.c module. The ossfuzz.c module +** is an adaptor for OSS-FUZZ. (https://github.com/google/oss-fuzz) +** +** This program links against ossfuzz.c. It reads files named on the +** command line and passes them one by one into ossfuzz.c. +*/ +#include <stddef.h> +#if !defined(_MSC_VER) +# include <stdint.h> +#endif +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "sqlite3.h" + +#if defined(_MSC_VER) +typedef unsigned char uint8_t; +#endif + +/* +** The entry point in ossfuzz.c that this routine will be calling +*/ +int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size); + +/* Must match equivalent #defines in ossfuzz.c */ +#define FUZZ_SQL_TRACE 0x0001 /* Set an sqlite3_trace() callback */ +#define FUZZ_SHOW_MAX_DELAY 0x0002 /* Show maximum progress callback delay */ +#define FUZZ_SHOW_ERRORS 0x0004 /* Show SQL errors */ +extern void ossfuzz_set_debug_flags(unsigned); + + + +/* +** Read files named on the command-line and invoke the fuzzer for +** each one. +*/ +int main(int argc, char **argv){ + FILE *in; + int i; + int nErr = 0; + uint8_t *zBuf = 0; + size_t sz; + unsigned mDebug = 0; + + for(i=1; i<argc; i++){ + const char *zFilename = argv[i]; + if( zFilename[0]=='-' ){ + if( zFilename[1]=='-' ) zFilename++; + if( strcmp(zFilename, "-show-errors")==0 ){ + mDebug |= FUZZ_SHOW_ERRORS; + ossfuzz_set_debug_flags(mDebug); + }else + if( strcmp(zFilename, "-show-max-delay")==0 ){ + mDebug |= FUZZ_SHOW_MAX_DELAY; + ossfuzz_set_debug_flags(mDebug); + }else + if( strcmp(zFilename, "-sql-trace")==0 ){ + mDebug |= FUZZ_SQL_TRACE; + ossfuzz_set_debug_flags(mDebug); + }else + { + printf("unknown option \"%s\"\n", argv[i]); + printf("should be one of: --show-errors --show-max-delay" + " --sql-trace\n"); + exit(1); + } + continue; + } + in = fopen(zFilename, "rb"); + if( in==0 ){ + fprintf(stderr, "cannot open \"%s\"\n", zFilename); + nErr++; + continue; + } + fseek(in, 0, SEEK_END); + sz = ftell(in); + rewind(in); + zBuf = realloc(zBuf, sz); + if( zBuf==0 ){ + fprintf(stderr, "cannot malloc() for %d bytes\n", (int)sz); + exit(1); + } + if( fread(zBuf, sz, 1, in)!=1 ){ + fprintf(stderr, "cannot read %d bytes from \"%s\"\n", + (int)sz, zFilename); + nErr++; + }else{ + printf("%s... ", zFilename); + if( mDebug ) printf("\n"); + fflush(stdout); + (void)LLVMFuzzerTestOneInput(zBuf, sz); + if( mDebug ) printf("%s: ", zFilename); + printf("ok\n"); + } + fclose(in); + } + free(zBuf); + return nErr; +} diff --git a/test/pager1.test b/test/pager1.test index 005b356080..9a39e6f374 100644 --- a/test/pager1.test +++ b/test/pager1.test @@ -17,6 +17,15 @@ source $testdir/malloc_common.tcl source $testdir/wal_common.tcl set testprefix pager1 +if {[atomic_batch_write test.db]} { + finish_test + return +} +ifcapable !incrblob { + finish_test + return +} + # Do not use a codec for tests in this file, as the database file is # manipulated directly using tcl scripts (using the [hexio_write] command). # @@ -271,7 +280,7 @@ do_execsql_test pager1-3.1.2 { } {3 0} do_catchsql_test pager1-3.1.3 { INSERT INTO t1 SELECT a+3, randomblob(1500) FROM t1 -} {1 {CHECK constraint failed: counter}} +} {1 {CHECK constraint failed: i<5}} do_execsql_test pager1-3.4 { SELECT * FROM counter } {3 0} do_execsql_test pager1-3.5 { SELECT a FROM t1 } {1 2 3} do_execsql_test pager1-3.6 { COMMIT } {} @@ -445,7 +454,7 @@ do_test pager1.4.2.1 { tstvfs delete } {} -if {$::tcl_platform(platform)!="windows"} { +if {$::tcl_platform(os) ne "Windows NT"} { do_test pager1.4.2.2 { faultsim_restore_and_reopen execsql { @@ -529,6 +538,7 @@ set pwd [get_pwd] testvfs tv -default 1 tv script copy_on_mj_delete set ::mj_filename_length 0 +set ::mj_delete_cnt 0 proc copy_on_mj_delete {method filename args} { if {[string match *mj* [file tail $filename]]} { # @@ -542,6 +552,7 @@ proc copy_on_mj_delete {method filename args} { set ::mj_filename_length [string length $filename] } faultsim_save + incr ::mj_delete_cnt } return SQLITE_OK } @@ -556,7 +567,7 @@ foreach {tn1 tcl} { # make sure SQLite doesn't get confused by this. # set nPadding [expr 511 - $::mj_filename_length] - if {$tcl_platform(platform)=="windows"} { + if {$tcl_platform(platform) eq "windows"} { # TBD need to figure out how to do this correctly for Windows!!! set nPadding [expr 255 - $::mj_filename_length] } @@ -579,29 +590,68 @@ foreach {tn1 tcl} { } } { eval $tcl - foreach {tn2 sql} { + foreach {tn2 sql usesMJ} { o { PRAGMA main.synchronous=OFF; PRAGMA aux.synchronous=OFF; PRAGMA journal_mode = DELETE; - } + } 0 o512 { PRAGMA main.synchronous=OFF; PRAGMA aux.synchronous=OFF; PRAGMA main.page_size = 512; PRAGMA aux.page_size = 512; PRAGMA journal_mode = DELETE; - } + } 0 n { PRAGMA main.synchronous=NORMAL; PRAGMA aux.synchronous=NORMAL; PRAGMA journal_mode = DELETE; - } + } 1 f { PRAGMA main.synchronous=FULL; PRAGMA aux.synchronous=FULL; PRAGMA journal_mode = DELETE; - } + } 1 + w1 { + PRAGMA main.synchronous=NORMAL; + PRAGMA aux.synchronous=NORMAL; + PRAGMA journal_mode = WAL; + } 0 + w2 { + PRAGMA main.synchronous=NORMAL; + PRAGMA aux.synchronous=NORMAL; + PRAGMA main.journal_mode=DELETE; + PRAGMA aux.journal_mode=WAL; + } 0 + o1a { + PRAGMA main.synchronous=FULL; + PRAGMA aux.synchronous=OFF; + PRAGMA journal_mode=DELETE; + } 0 + o1b { + PRAGMA main.synchronous=OFF; + PRAGMA aux.synchronous=NORMAL; + PRAGMA journal_mode=DELETE; + } 0 + m1 { + PRAGMA main.synchronous=NORMAL; + PRAGMA aux.synchronous=NORMAL; + PRAGMA main.journal_mode=DELETE; + PRAGMA aux.journal_mode = MEMORY; + } 0 + t1 { + PRAGMA main.synchronous=NORMAL; + PRAGMA aux.synchronous=NORMAL; + PRAGMA main.journal_mode=DELETE; + PRAGMA aux.journal_mode = TRUNCATE; + } 1 + p1 { + PRAGMA main.synchronous=NORMAL; + PRAGMA aux.synchronous=NORMAL; + PRAGMA main.journal_mode=DELETE; + PRAGMA aux.journal_mode = PERSIST; + } 1 } { set tn "${tn1}.${tn2}" @@ -613,6 +663,7 @@ foreach {tn1 tcl} { # tv filter xDelete do_test pager1-4.4.$tn.1 { + set ::mj_delete_cnt 0 faultsim_delete_and_reopen $prefix execsql " ATTACH '${prefix}2' AS aux; @@ -634,6 +685,13 @@ foreach {tn1 tcl} { } } {} tv filter {} + + # Verify that a master journal was deleted only for those cases where + # master journals really ought to be used + # + do_test pager1-4.4.$tn.1b { + set ::mj_delete_cnt + } $usesMJ # Check that the transaction was committed successfully. # @@ -644,25 +702,33 @@ foreach {tn1 tcl} { SELECT * FROM b } {won too free double-you why zed} - # Restore the file-system and reopen the databases. Check that it now - # appears that the transaction was not committed (because the file-system - # was restored to the state where it had not been). - # - do_test pager1-4.4.$tn.4 { - faultsim_restore_and_reopen $prefix - execsql "ATTACH '${prefix}2' AS aux" - } {} - do_execsql_test pager1-4.4.$tn.5 {SELECT * FROM a} {double-you why zed} - do_execsql_test pager1-4.4.$tn.6 {SELECT * FROM b} {won too free} + if {$usesMJ} { + # Restore the file-system and reopen the databases. Check that it now + # appears that the transaction was not committed (because the file-system + # was restored to the state where it had not been). + # + do_test pager1-4.4.$tn.4 { + faultsim_restore_and_reopen $prefix + execsql "ATTACH '${prefix}2' AS aux" + } {} + do_execsql_test pager1-4.4.$tn.5 {SELECT * FROM a} {double-you why zed} + do_execsql_test pager1-4.4.$tn.6 {SELECT * FROM b} {won too free} + } # Restore the file-system again. This time, before reopening the databases, # delete the master-journal file from the file-system. It now appears that # the transaction was committed (no master-journal file == no rollback). # do_test pager1-4.4.$tn.7 { - faultsim_restore_and_reopen $prefix - foreach f [glob ${prefix}-mj*] { forcedelete $f } + if {$::mj_delete_cnt>0} { + faultsim_restore_and_reopen $prefix + foreach f [glob ${prefix}-mj*] { forcedelete $f } + } else { + db close + sqlite3 db $prefix + } execsql "ATTACH '${prefix}2' AS aux" + glob -nocomplain ${prefix}-mj* } {} do_execsql_test pager1-4.4.$tn.8 { SELECT * FROM a @@ -678,7 +744,6 @@ db close tv delete forcedelete $dirname - # Set up a VFS to make a copy of the file-system just before deleting a # journal file to commit a transaction. The transaction modifies exactly # two database pages (and page 1 - the change counter). @@ -1088,7 +1153,7 @@ do_test pager1-5.5.1 { PRAGMA journal_mode = PERSIST; CREATE TABLE t3(a, b); INSERT INTO t3 SELECT randomblob(1500), randomblob(1500) FROM t1; - UPDATE t3 SET b = randomblob(1500); + UPDATE t3 SET b = randomblob(1501); } expr [file size test.db-journal] > 15000 } {1} @@ -1340,26 +1405,47 @@ do_test pager1-9.3.1 { execsql { PRAGMA page_size = 1024 } for {set ii 0} {$ii < 4} {incr ii} { execsql "CREATE TABLE t${ii}(a, b)" } } {} -do_test pager1-9.3.2 { - sqlite3 db2 test.db2 - - execsql { - PRAGMA page_size = 4096; - PRAGMA synchronous = OFF; - CREATE TABLE t1(a, b); - CREATE TABLE t2(a, b); - } db2 - - sqlite3_backup B db2 main db main - B step 30 - list [B step 10000] [B finish] -} {SQLITE_DONE SQLITE_OK} -do_test pager1-9.3.3 { - db2 close - db close - tv delete - file size test.db2 -} [file size test.db] +if {[nonzero_reserved_bytes]} { + # backup with a page size changes is not possible with the codec + # + do_test pager1-9.3.2codec { + sqlite3 db2 test.db2 + execsql { + PRAGMA page_size = 4096; + PRAGMA synchronous = OFF; + CREATE TABLE t1(a, b); + CREATE TABLE t2(a, b); + } db2 + sqlite3_backup B db2 main db main + B step 30 + list [B step 10000] [B finish] + } {SQLITE_READONLY SQLITE_READONLY} + do_test pager1-9.3.3codec { + db2 close + db close + tv delete + file size test.db2 + } [file size test.db2] +} else { + do_test pager1-9.3.2 { + sqlite3 db2 test.db2 + execsql { + PRAGMA page_size = 4096; + PRAGMA synchronous = OFF; + CREATE TABLE t1(a, b); + CREATE TABLE t2(a, b); + } db2 + sqlite3_backup B db2 main db main + B step 30 + list [B step 10000] [B finish] + } {SQLITE_DONE SQLITE_OK} + do_test pager1-9.3.3 { + db2 close + db close + tv delete + file size test.db2 + } [file size test.db] +} do_test pager1-9.4.1 { faultsim_delete_and_reopen @@ -1628,7 +1714,7 @@ do_execsql_test pager1-13.1.1 { UPDATE t1 SET b = a_string(400); } {persist} -if {$::tcl_platform(platform)!="windows"} { +if {$::tcl_platform(os) ne "Windows NT"} { # Run transactions of increasing sizes. Eventually, one (or more than one) # of these will write just enough content that one of the old headers created # by the transaction in the block above lies immediately after the content @@ -1653,7 +1739,7 @@ for {set nUp 1} {$nUp<64} {incr nUp} { } } -if {$::tcl_platform(platform)!="windows"} { +if {$::tcl_platform(os) ne "Windows NT"} { # Same test as above. But this time with an index on the table. # do_execsql_test pager1-13.2.1 { @@ -1682,6 +1768,12 @@ tv delete #------------------------------------------------------------------------- # Test specal "PRAGMA journal_mode=OFF" test cases. # +# Do not run these tests for SQLITE_ENABLE_ZIPVFS builds. Such builds +# cause the pager to enter the error state if a statement transaction +# cannot be rolled back due to a prior "PRAGMA journal_mode=OFF". Which +# causes these tests to fail. +# +if {[info commands zip_register]==""} { faultsim_delete_and_reopen do_execsql_test pager1-14.1.1 { PRAGMA journal_mode = OFF; @@ -1706,8 +1798,11 @@ do_catchsql_test pager1-14.1.4 { } {1 {UNIQUE constraint failed: t1.rowid}} do_execsql_test pager1-14.1.5 { COMMIT; +} +do_execsql_test pager1-14.1.6 { SELECT * FROM t1; } {1 2 2 2} +} #------------------------------------------------------------------------- # Test opening and closing the pager sub-system with different values @@ -1797,6 +1892,7 @@ do_test pager1-18.1 { do_test pager1-18.2 { set root [db one "SELECT rootpage FROM sqlite_master"] set lockingpage [expr (0x10000/1024) + 1] + sqlite3_db_config db DEFENSIVE 0 execsql { PRAGMA writable_schema = 1; UPDATE sqlite_master SET rootpage = $lockingpage; @@ -1847,19 +1943,24 @@ do_test pager1-18.4 { catchsql { SELECT length(x||'') FROM t2 } db2 } {1 {database disk image is malformed}} db2 close -do_test pager1-18.5 { - sqlite3 db "" - execsql { - CREATE TABLE t1(a, b); - CREATE TABLE t2(a, b); - PRAGMA writable_schema = 1; - UPDATE sqlite_master SET rootpage=5 WHERE tbl_name = 't1'; - PRAGMA writable_schema = 0; - ALTER TABLE t1 RENAME TO x1; - } - catchsql { SELECT * FROM x1 } -} {1 {database disk image is malformed}} -db close +extra_schema_checks 0 +ifcapable altertable { + do_test pager1-18.5 { + sqlite3 db "" + sqlite3_db_config db DEFENSIVE 0 + execsql { + CREATE TABLE t1(a, b); + CREATE TABLE t2(a, b); + PRAGMA writable_schema = 1; + UPDATE sqlite_master SET rootpage=5 WHERE tbl_name = 't1'; + PRAGMA writable_schema = 0; + ALTER TABLE t1 RENAME TO x1; + } + catchsql { SELECT * FROM x1 } + } {1 {database disk image is malformed}} + db close +} +extra_schema_checks 1 do_test pager1-18.6 { faultsim_delete_and_reopen @@ -2391,13 +2492,23 @@ do_test pager1-29.1 { } file size test.db } [expr 1024*3] -do_test pager1-29.2 { - execsql { - PRAGMA page_size = 4096; - VACUUM; - } - file size test.db -} [expr 4096*3] +if {[nonzero_reserved_bytes]} { + # VACUUM with size changes is not possible with the codec. + do_test pager1-29.2 { + catchsql { + PRAGMA page_size = 4096; + VACUUM; + } + } {1 {attempt to write a readonly database}} +} else { + do_test pager1-29.2 { + execsql { + PRAGMA page_size = 4096; + VACUUM; + } + file size test.db + } [expr 4096*3] +} #------------------------------------------------------------------------- # Test that if an empty database file (size 0 bytes) is opened in @@ -2427,7 +2538,7 @@ do_test pager1-30.1 { # file can still be rolled back. This is required for backward compatibility - # versions of SQLite prior to 3.5.8 always set this field to zero. # -if {$tcl_platform(platform)=="unix"} { +if {$::tcl_platform(os) ne "Windows NT"} { do_test pager1-31.1 { faultsim_delete_and_reopen execsql { @@ -2501,7 +2612,7 @@ forcedelete test.db # and the call to unlink() returns an ENOENT error, the COMMIT does not # succeed. # -if {$::tcl_platform(platform)=="unix"} { +if {$::tcl_platform(os) ne "Windows NT"} { do_test pager1-33.1 { sqlite3 db test.db execsql { @@ -2631,7 +2742,7 @@ do_test 38.1 { close $fd sqlite3 db test.db catchsql { CREATE TABLE t1(x) } -} {1 {file is encrypted or is not a database}} +} {1 {file is not a database}} do_test 38.2 { catch { db close } forcedelete test.db @@ -2814,4 +2925,32 @@ do_test 43.3 { sqlite3_db_status db CACHE_MISS 0 } {0 1 0} +# 2022-03-01 Forum post https://sqlite.org/forum/forumpost/3b9e894312 +# Ensure that max_page_count gets adjusted upward, if needed, on a +# ROLLBACK. +# +db close +sqlite3 db :memory: +do_execsql_test 44.1 { + PRAGMA page_size=4096; + PRAGMA auto_vacuum=FULL; + CREATE TABLE t1(a INTEGER PRIMARY KEY, b ANY); + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<50) + INSERT INTO t1(a,b) SELECT x, zeroblob(1000) FROM c; + CREATE TABLE t2 AS SELECT * FROM t1; + PRAGMA page_count; +} {31} +do_execsql_test 44.2 { + BEGIN; + DROP TABLE t2; + PRAGMA incremental_vacuum=50; + PRAGMA page_count; + PRAGMA max_page_count=2; +} {16 16} +do_execsql_test 44.3 { + ROLLBACK; + PRAGMA page_count; + PRAGMA max_page_count; +} {31 31} + finish_test diff --git a/test/pager2.test b/test/pager2.test index 0e2b33b833..ef05cc76a2 100644 --- a/test/pager2.test +++ b/test/pager2.test @@ -147,22 +147,25 @@ do_test pager2-2.2 { file size test.db } {3072} -#------------------------------------------------------------------------- -# Test that shared in-memory databases seem to work. -# -db close -do_test pager2-3.1 { - forcedelete test.db - sqlite3_shutdown - sqlite3_config_uri 1 - - sqlite3 db1 {file:test.db?mode=memory&cache=shared} - sqlite3 db2 {file:test.db?mode=memory&cache=shared} - sqlite3 db3 test.db - - db1 eval { CREATE TABLE t1(a, b) } - db2 eval { INSERT INTO t1 VALUES(1, 2) } - list [catch { db3 eval { INSERT INTO t1 VALUES(3, 4) } } msg] $msg -} {1 {no such table: t1}} +ifcapable shared_cache { + #------------------------------------------------------------------------- + # Test that shared in-memory databases seem to work. + # + db close + do_test pager2-3.1 { + forcedelete test.db + sqlite3_shutdown + sqlite3_config_uri 1 + + sqlite3 db1 {file:test.db?mode=memory&cache=shared} + sqlite3 db2 {file:test.db?mode=memory&cache=shared} + sqlite3 db3 test.db + + db1 eval { CREATE TABLE t1(a, b) } + db2 eval { INSERT INTO t1 VALUES(1, 2) } + list [catch { db3 eval { INSERT INTO t1 VALUES(3, 4) } } msg] $msg + } {1 {no such table: t1}} + db1 close +} finish_test diff --git a/test/pager3.test b/test/pager3.test index 23435a79b7..e815f2788b 100644 --- a/test/pager3.test +++ b/test/pager3.test @@ -16,6 +16,10 @@ source $testdir/lock_common.tcl source $testdir/malloc_common.tcl source $testdir/wal_common.tcl +if {[atomic_batch_write test.db]} { + finish_test + return +} foreach {tn sql res j} { 1 "PRAGMA journal_mode = DELETE" delete 0 diff --git a/test/pager4.test b/test/pager4.test index 2cf73d1b17..537f529dd3 100644 --- a/test/pager4.test +++ b/test/pager4.test @@ -13,7 +13,7 @@ # is unlinked or renamed out from under SQLite. # -if {$tcl_platform(platform)!="unix"} return +if {$tcl_platform(os) eq "Windows NT"} return set testdir [file dirname $argv0] source $testdir/tester.tcl diff --git a/test/pagerfault.test b/test/pagerfault.test index c0f5de69ac..6e82b90090 100644 --- a/test/pagerfault.test +++ b/test/pagerfault.test @@ -20,7 +20,7 @@ if {[permutation] == "inmemory_journal"} { return } -if {$::tcl_platform(platform)=="windows"} { +if {$::tcl_platform(os) eq "Windows NT"} { finish_test return } @@ -676,7 +676,7 @@ do_faultsim_test pagerfault-14a -prep { } -body { if {[catch {db backup test.db2} msg]} { error [regsub {.*: } $msg {}] } } -test { - faultsim_test_result {0 {}} {1 {}} {1 {SQL logic error or missing database}} + faultsim_test_result {0 {}} {1 {}} {1 {SQL logic error}} } # If TEMP_STORE is 2 or greater, then the database [db2] will be created @@ -684,22 +684,24 @@ do_faultsim_test pagerfault-14a -prep { # is not possible to change the page-size of an in-memory database. Even # using the backup API. # -if {$TEMP_STORE<2} { - do_faultsim_test pagerfault-14b -prep { - catch { db2 close } - faultsim_restore_and_reopen +# Update: It is no longer possible to change the page size of any temp +# database after it has been created. +# +do_faultsim_test pagerfault-14b -prep { + catch { db2 close } + faultsim_restore_and_reopen sqlite3 db2 "" db2 eval { PRAGMA page_size = 4096; CREATE TABLE xx(a) } - } -body { - sqlite3_backup B db2 main db main - B step 200 - set rc [B finish] - if {[string match SQLITE_IOERR_* $rc]} {set rc SQLITE_IOERR} - if {$rc != "SQLITE_OK"} { error [sqlite3_test_errstr $rc] } - set {} {} - } -test { - faultsim_test_result {0 {}} {1 {sqlite3_backup_init() failed}} - } +} -body { + sqlite3_backup B db2 main db main + B step 200 + set rc [B finish] + if {[string match SQLITE_IOERR_* $rc]} {set rc SQLITE_IOERR} + if {$rc != "SQLITE_OK"} { error [sqlite3_test_errstr $rc] } + set {} {} +} -test { + faultsim_test_result {1 {attempt to write a readonly database}} \ + {1 {sqlite3_backup_init() failed}} } do_faultsim_test pagerfault-14c -prep { @@ -1201,12 +1203,14 @@ do_faultsim_test pagerfault-26 -prep { set contents [db eval {SELECT * FROM t1}] if {$contents != "1 2"} { error "Bad database contents ($contents)" } - set sz [file size test.db] - if {$testrc!=0 && $sz!=1024*3 && $sz!=4096*3} { - error "Expected file size to be 3072 or 12288 bytes - actual size $sz bytes" - } - if {$testrc==0 && $sz!=4096*3} { - error "Expected file size to be 12288 bytes - actual size $sz bytes" + if {[atomic_batch_write test.db]==0} { + set sz [file size test.db] + if {$testrc!=0 && $sz!=1024*3 && $sz!=4096*3} { + error "Expected file size 3072 or 12288 bytes - actual size $sz bytes" + } + if {$testrc==0 && $sz!=4096*3} { + error "Expected file size to be 12288 bytes - actual size $sz bytes" + } } } diff --git a/test/pageropt.test b/test/pageropt.test index 7191661ba5..f58b8ee781 100644 --- a/test/pageropt.test +++ b/test/pageropt.test @@ -16,12 +16,17 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl +do_not_use_codec ifcapable {!pager_pragmas||secure_delete||direct_read} { finish_test return } +# A non-zero reserved_bytes value changes the number of pages in the +# database file, which messes up the results in this test. +if {[nonzero_reserved_bytes]} {finish_test; return;} + # Run the SQL statement supplied by the argument and return # the results. Prepend four integers to the beginning of the # result which are diff --git a/test/parser1.test b/test/parser1.test index 78c1a40c63..b8d3d8b420 100644 --- a/test/parser1.test +++ b/test/parser1.test @@ -28,6 +28,7 @@ do_catchsql_test parser1-1.1 { # COLLATE, ASC, and DESC keywords on the id list of a FK constraint, and that # those keywords are silently ignored. # +sqlite3_db_config db DEFENSIVE 0 do_execsql_test parser1-1.2 { CREATE TABLE t1( a TEXT PRIMARY KEY, @@ -76,4 +77,49 @@ do_catchsql_test parser1-2.2 { SELECT x FROM c; } {1 {syntax error after column name "x"}} +# Verify that the comma between multiple table constraints is +# optional. +# +# The missing comma is technically a syntax error. But we have to support +# it because there might be legacy databases that omit the commas in their +# sqlite_master tables. +# +do_execsql_test parser1-3.1 { + CREATE TABLE t300(id INTEGER PRIMARY KEY); + CREATE TABLE t301( + id INTEGER PRIMARY KEY, + c1 INTEGER NOT NULL, + c2 INTEGER NOT NULL, + c3 BOOLEAN NOT NULL DEFAULT 0, + FOREIGN KEY(c1) REFERENCES t300(id) ON DELETE CASCADE ON UPDATE RESTRICT + /* no comma */ + FOREIGN KEY(c2) REFERENCES t300(id) ON DELETE CASCADE ON UPDATE RESTRICT + /* no comma */ + UNIQUE(c1, c2) + ); + PRAGMA foreign_key_list(t301); +} {0 0 t300 c2 id RESTRICT CASCADE NONE 1 0 t300 c1 id RESTRICT CASCADE NONE} + +# 2025-07-01 https://sqlite.org/forum/forumpost/f4878de3e7dd4764 +# Do not allow parse-time optimizations to omit aggregate functions, +# because doing so can change the meaning of the query. +# +unset -nocomplain zero +set zero [expr {0+0}] +do_execsql_test parser1-4.1 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(x); + SELECT max(x) AND $zero FROM t1; +} 0 +do_execsql_test parser1-4.2 { + SELECT max(x) AND 0 FROM t1; +} 0 +do_execsql_test parser1-4.3 { + SELECT max(x) IN () FROM t1; +} 0 +do_execsql_test parser1-4.4 { + SELECT max(x) NOT IN () FROM t1; +} 1 + + finish_test diff --git a/test/pendingrace.test b/test/pendingrace.test new file mode 100644 index 0000000000..80160149a3 --- /dev/null +++ b/test/pendingrace.test @@ -0,0 +1,123 @@ +# 2023 January 31 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix pendingrace + +# This test file tests that a race condition surrounding hot-journal +# rollback that once existed has been resolved. The problem was that +# if, when attempting to upgrade from a SHARED to EXCLUSIVE lock in +# order to roll back a hot journal, a connection failed to take the +# lock, the file-descriptor was left holding a PENDING lock for +# a very short amount of time. In a multi-threaded deployment, this +# could allow a second connection to read the database without rolling +# back the hot journal. +# + +testvfs tvfs +db close +sqlite3 db test.db -vfs tvfs + +# Create a 20 page database using connection [db]. Connection [db] uses +# Tcl VFS wrapper "tvfs", but it is configured to do straight pass-through +# for now. +# +do_execsql_test 1.0 { + PRAGMA cache_size = 5; + CREATE TABLE t1(a, b); + CREATE INDEX i1 ON t1(a, b); + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<10 + ) + INSERT INTO t1 SELECT hex(randomblob(100)), hex(randomblob(100)) FROM s; +} {} +do_test 1.1a { + set nPg [db one { PRAGMA page_count }] + expr ($nPg==20 || $nPg==21) +} 1 + +# Simulate a crash in another process. This leaves the db with a hot-journal. +# Without the journal the db is corrupt. +# +sqlite3 db2 test.db +do_execsql_test -db db2 1.1 { + PRAGMA cache_size = 5; + BEGIN; + UPDATE t1 SET b=hex(randomblob(100)); +} +db_save +db2 close +proc my_db_restore {} { + forcecopy sv_test.db-journal test.db-journal + + set fd1 [open sv_test.db r] + fconfigure $fd1 -translation binary + set data [read $fd1] + close $fd1 + + set fd1 [open test.db w] + fconfigure $fd1 -translation binary + puts -nonewline $fd1 $data + close $fd1 +} +my_db_restore +do_test 1.2 { + file exists test.db-journal +} {1} + +# Set up connection [db2] to use Tcl VFS wrapper [tvfs2]. Which is configured +# so that the first call to xUnlock() fails. And then all VFS calls thereafter +# fail as well. +# +testvfs tvfs2 +tvfs2 filter xUnlock +tvfs2 script xUnlock +set ::seen_unlock 0 +proc xUnlock {args} { + if {$::seen_unlock==0} { + set ::seen_unlock 1 + tvfs2 ioerr 1 1 + tvfs2 filter {xLock xUnlock} + } + return "" +} +sqlite3 db2 test.db -vfs tvfs2 + +# Configure [tvfs] (used by [db]) so that within the first call to xAccess, +# [db2] attempts to read the db. This causes [db2] to fail to upgrade to +# EXCLUSIVE, leaving it with a PENDING lock. Which it holds on to, +# as the xUnlock() and all subsequent VFS calls fail. +# +tvfs filter xAccess +tvfs script xAccess +set ::seen_access 0 +proc xAccess {args} { + if {$::seen_access==0} { + set ::seen_access 1 + catch { db2 eval { SELECT count(*)+0 FROM t1 } } + breakpoint + } + return "" +} + +# Run an integrity check using [db]. +do_catchsql_test 1.3 { + PRAGMA integrity_check +} {1 {database is locked}} + +db close +db2 close +tvfs delete +tvfs2 delete + +finish_test diff --git a/test/percentile.test b/test/percentile.test index b2bd061e86..25096a953c 100644 --- a/test/percentile.test +++ b/test/percentile.test @@ -9,7 +9,8 @@ # #*********************************************************************** # This file implements regression tests for SQLite library. The -# focus of this file is percentile.c extension +# focus of this file is percentile.c extension. This also tests +# the SQLITE_ENABLE_ORDERED_SET_AGGREGATES compile-time option. # set testdir [file dirname $argv0] @@ -18,25 +19,71 @@ source $testdir/tester.tcl # Basic test of the percentile() function. # do_test percentile-1.0 { - load_static_extension db percentile execsql { CREATE TABLE t1(x); INSERT INTO t1 VALUES(1),(4),(6),(7),(8),(9),(11),(11),(11); } execsql {SELECT percentile(x,0) FROM t1} } {1.0} -foreach {in out} { - 100 11.0 - 50 8.0 - 12.5 4.0 - 15 4.4 - 20 5.2 - 80 11.0 - 89 11.0 +foreach {in out disc} { + 100 11.0 11.0 + 50 8.0 8.0 + 12.5 4.0 4.0 + 15 4.4 4.0 + 20 5.2 4.0 + 80 11.0 11.0 + 89 11.0 11.0 } { - do_test percentile-1.1.$in { + do_test percentile-1.1.$in.1 { execsql {SELECT percentile(x,$in) FROM t1} } $out + do_test percentile-1.1.$in.2 { + execsql {SELECT percentile_cont(x,$in*0.01) FROM t1} + } $out + do_test percentile-1.1.$in.3 { + execsql {SELECT percentile_disc(x,$in*0.01) FROM t1} + } $disc + if {$in==50} { + do_test percentile-1.1.$in.4 { + execsql {SELECT median(x) FROM t1} + } $out + } + ifcapable ordered_set_aggregates { + do_test percentile-1.1.$in.5 { + execsql {SELECT percentile($in)WITHIN GROUP(ORDER BY x) FROM t1} + } $out + do_test percentile-1.1.$in.6 { + execsql {SELECT percentile_cont($in*0.01) WITHIN GROUP(ORDER BY x) + FROM t1} + } $out + do_test percentile-1.1.$in.7 { + execsql {SELECT percentile_disc($in*0.01) WITHIN GROUP(ORDER BY x) + FROM t1} + } $disc + if {$in==50} { + do_test percentile-1.1.$in.8 { + execsql {SELECT median() WITHIN GROUP (ORDER BY x) FROM t1} + } $out + } + } +} +do_execsql_test percentile-1.1.median { + SELECT median(x) FROM t1; +} 8.0 +ifcapable ordered_set_aggregates { + do_execsql_test percentile-1.1.median { + SELECT median() WITHIN GROUP (ORDER BY x) FROM t1; + } 8.0 + do_execsql_test percentile-1.1.distinct.1 { + SELECT median(DISTINCT x) FROM t1; + } 7.0 + do_catchsql_test percentile-1.1.distinct.2 { + SELECT percentile(DISTINCT 50) WITHIN GROUP (ORDER BY x) FROM t1; + } {1 {DISTINCT not allowed on ordered-set aggregate percentile()}} +} else { + do_catchsql_test percentile-1.1.median { + SELECT median() WITHIN GROUP (ORDER BY x) FROM t1; + } {1 {near "(": syntax error}} } # Add some NULL values. @@ -44,28 +91,69 @@ foreach {in out} { do_test percentile-1.2 { execsql {INSERT INTO t1 VALUES(NULL),(NULL);} } {} -foreach {in out} { - 100 11.0 - 50 8.0 - 12.5 4.0 - 15 4.4 - 20 5.2 - 80 11.0 - 89 11.0 +foreach {in out disc} { + 100 11.0 11.0 + 50 8.0 8.0 + 12.5 4.0 4.0 + 15 4.4 4.0 + 20 5.2 4.0 + 80 11.0 11.0 + 89 11.0 11.0 } { - do_test percentile-1.3.$in { + do_test percentile-1.3.$in.1 { execsql {SELECT percentile(x,$in) FROM t1} } $out + do_test percentile-1.3.$in.2 { + execsql {SELECT percentile_cont(x,$in*0.01) FROM t1} + } $out + do_test percentile-1.3.$in.3 { + execsql {SELECT percentile_disc(x,$in*0.01) FROM t1} + } $disc + if {$in==50} { + do_test percentile-1.3.$in.4 { + execsql {SELECT median(x) FROM t1} + } $out + } + ifcapable ordered_set_aggregates { + do_test percentile-1.3.$in.5 { + execsql {SELECT percentile($in)WITHIN GROUP(ORDER BY x) FROM t1} + } $out + do_test percentile-1.3.$in.6 { + execsql {SELECT percentile_cont($in*0.01) WITHIN GROUP(ORDER BY x) + FROM t1} + } $out + do_test percentile-1.3.$in.7 { + execsql {SELECT percentile_disc($in*0.01) WITHIN GROUP(ORDER BY x) + FROM t1} + } $disc + if {$in==50} { + do_test percentile-1.3.$in.8 { + execsql {SELECT median() WITHIN GROUP (ORDER BY x) FROM t1} + } $out + } + } } # The second argument to percentile can change some, but not much. # -do_test percentile-1.4 { +do_test percentile-1.4.1 { catchsql {SELECT round(percentile(x, 15+0.000001*rowid),1) FROM t1} } {0 4.4} -do_test percentile-1.5 { - catchsql {SELECT round(percentile(x, 15+0.1*rowid),1) FROM t1} -} {1 {2nd argument to percentile() is not the same for all input rows}} +do_test percentile-1.4.2 { + catchsql {SELECT round(percentile_cont(x,(15+0.000001*rowid)*0.01),1) FROM t1} +} {0 4.4} +do_test percentile-1.4.3 { + catchsql {SELECT percentile_disc(x, (15+0.000001*rowid)*0.01) FROM t1} +} {0 4.0} +do_test percentile-1.5.1 { + catchsql {SELECT percentile(x, 15+0.1*rowid) FROM t1} +} {1 {the fraction argument to percentile() is not the same for all input rows}} +do_test percentile-1.5.2 { + catchsql {SELECT percentile_cont(x, (15+0.1*rowid)*0.01) FROM t1} +} {1 {the fraction argument to percentile_cont() is not the same for all input rows}} +do_test percentile-1.5.3 { + catchsql {SELECT percentile_disc(x, (15+0.1*rowid)*0.01) FROM t1} +} {1 {the fraction argument to percentile_disc() is not the same for all input rows}} # Input values in a random order # @@ -75,59 +163,152 @@ do_test percentile-1.6 { INSERT INTO t2 SELECT x+0.0 FROM t1 ORDER BY random(); } } {} -foreach {in out} { - 100 11.0 - 50 8.0 - 12.5 4.0 - 15 4.4 - 20 5.2 - 80 11.0 - 89 11.0 +foreach {in out disc} { + 100 11.0 11.0 + 50 8.0 8.0 + 12.5 4.0 4.0 + 15 4.4 4.0 + 20 5.2 4.0 + 80 11.0 11.0 + 89 11.0 11.0 } { - do_test percentile-1.7.$in { + do_test percentile-1.7.$in.1 { execsql {SELECT percentile(x,$in) FROM t2} } $out + do_test percentile-1.7.$in.2 { + execsql {SELECT percentile_cont(x,$in*0.01) FROM t2} + } $out + do_test percentile-1.7.$in.3 { + execsql {SELECT percentile_disc(x,$in*0.01) FROM t2} + } $disc + if {$in==50} { + do_test percentile-1.7.$in.4 { + execsql {SELECT median(x) FROM t2} + } $out + } + ifcapable ordered_set_aggregates { + do_test percentile-1.7.$in.5 { + execsql {SELECT percentile($in)WITHIN GROUP(ORDER BY x) FROM t2} + } $out + do_test percentile-1.7.$in.6 { + execsql {SELECT percentile_cont($in*0.01) WITHIN GROUP(ORDER BY x) + FROM t2} + } $out + do_test percentile-1.7.$in.7 { + execsql {SELECT percentile_disc($in*0.01) WITHIN GROUP(ORDER BY x) + FROM t2} + } $disc + if {$in==50} { + do_test percentile-1.7.$in.8 { + execsql {SELECT median() WITHIN GROUP (ORDER BY x) FROM t2} + } $out + } + } } # Wrong number of arguments # -do_test percentile-1.8 { +do_test percentile-1.8.1 { catchsql {SELECT percentile(x,0,1) FROM t1} } {1 {wrong number of arguments to function percentile()}} -do_test percentile-1.9 { +do_test percentile-1.8.2 { + catchsql {SELECT percentile_cont(x,0,1) FROM t1} +} {1 {wrong number of arguments to function percentile_cont()}} +do_test percentile-1.8.3 { + catchsql {SELECT percentile_disc(x,0,1) FROM t1} +} {1 {wrong number of arguments to function percentile_disc()}} +do_test percentile-1.8.4 { + catchsql {SELECT median(x,0) FROM t1} +} {1 {wrong number of arguments to function median()}} +ifcapable ordered_set_aggregates { + do_test percentile-1.8.5 { + catchsql {SELECT percentile(0,1) WITHIN GROUP(ORDER BY x) FROM t1} + } {1 {wrong number of arguments to function percentile()}} + do_test percentile-1.8.2 { + catchsql {SELECT percentile_cont(0,1)WITHIN GROUP (ORDER BY x) FROM t1} + } {1 {wrong number of arguments to function percentile_cont()}} + do_test percentile-1.8.3 { + catchsql {SELECT percentile_disc(0,1)WITHIN GROUP (ORDER BY x) FROM t1} + } {1 {wrong number of arguments to function percentile_disc()}} + do_test percentile-1.8.4 { + catchsql {SELECT median(x) WITHIN GROUP (ORDER BY x) FROM t1} + } {1 {wrong number of arguments to function median()}} +} +do_test percentile-1.9.1 { catchsql {SELECT percentile(x) FROM t1} } {1 {wrong number of arguments to function percentile()}} +do_test percentile-1.9.2 { + catchsql {SELECT percentile_cont(x) FROM t1} +} {1 {wrong number of arguments to function percentile_cont()}} +do_test percentile-1.9.3 { + catchsql {SELECT percentile_disc(x) FROM t1} +} {1 {wrong number of arguments to function percentile_disc()}} +do_test percentile-1.9.4 { + catchsql {SELECT median() FROM t1} +} {1 {wrong number of arguments to function median()}} +ifcapable ordered_set_aggregates { + do_test percentile-1.9.5 { + catchsql {SELECT percentile() WITHIN GROUP(ORDER BY x) FROM t1} + } {1 {wrong number of arguments to function percentile()}} + do_test percentile-1.9.6 { + catchsql {SELECT percentile_cont()WITHIN GROUP (ORDER BY x) FROM t1} + } {1 {wrong number of arguments to function percentile_cont()}} + do_test percentile-1.9.7 { + catchsql {SELECT percentile_disc()WITHIN GROUP (ORDER BY x) FROM t1} + } {1 {wrong number of arguments to function percentile_disc()}} +} # Second argument must be numeric # do_test percentile-1.10 { catchsql {SELECT percentile(x,null) FROM t1} -} {1 {2nd argument to percentile() is not a number between 0.0 and 100.0}} +} {1 {the fraction argument to percentile() is not between 0.0 and 100.0}} do_test percentile-1.11 { catchsql {SELECT percentile(x,'fifty') FROM t1} -} {1 {2nd argument to percentile() is not a number between 0.0 and 100.0}} +} {1 {the fraction argument to percentile() is not between 0.0 and 100.0}} do_test percentile-1.12 { catchsql {SELECT percentile(x,x'3530') FROM t1} -} {1 {2nd argument to percentile() is not a number between 0.0 and 100.0}} +} {1 {the fraction argument to percentile() is not between 0.0 and 100.0}} # Second argument is out of range # do_test percentile-1.13 { catchsql {SELECT percentile(x,-0.0000001) FROM t1} -} {1 {2nd argument to percentile() is not a number between 0.0 and 100.0}} +} {1 {the fraction argument to percentile() is not between 0.0 and 100.0}} do_test percentile-1.14 { catchsql {SELECT percentile(x,100.0000001) FROM t1} -} {1 {2nd argument to percentile() is not a number between 0.0 and 100.0}} +} {1 {the fraction argument to percentile() is not between 0.0 and 100.0}} +do_test percentile-1.14.2 { + catchsql {SELECT percentile_cont(x,1.0000001) FROM t1} +} {1 {the fraction argument to percentile_cont() is not between 0.0 and 1.0}} +do_test percentile-1.14.3 { + catchsql {SELECT percentile_disc(x,1.0000001) FROM t1} +} {1 {the fraction argument to percentile_disc() is not between 0.0 and 1.0}} # First argument is not NULL and is not NUMERIC # -do_test percentile-1.15 { +do_test percentile-1.15.1 { catchsql { BEGIN; UPDATE t1 SET x='50' WHERE x IS NULL; SELECT percentile(x, 50) FROM t1; } -} {1 {1st argument to percentile() is not numeric}} +} {1 {input to percentile() is not numeric}} +do_test percentile-1.15.2 { + catchsql { + SELECT percentile_cont(x, 0.50) FROM t1; + } +} {1 {input to percentile_cont() is not numeric}} +do_test percentile-1.15.3 { + catchsql { + SELECT percentile_disc(x, 0.50) FROM t1; + } +} {1 {input to percentile_disc() is not numeric}} +do_test percentile-1.15.4 { + catchsql { + SELECT median(x) FROM t1; + } +} {1 {input to median() is not numeric}} do_test percentile-1.16 { catchsql { ROLLBACK; @@ -135,7 +316,7 @@ do_test percentile-1.16 { UPDATE t1 SET x=x'3530' WHERE x IS NULL; SELECT percentile(x, 50) FROM t1; } -} {1 {1st argument to percentile() is not numeric}} +} {1 {input to percentile() is not numeric}} do_test percentile-1.17 { catchsql { ROLLBACK; @@ -163,7 +344,7 @@ do_test percentile-1.19 { # Infinity as an input # -do_test percentile-1.20 { +do_test percentile-1.20.1 { catchsql { DELETE FROM t1; INSERT INTO t1 SELECT x+0.0 FROM t2; @@ -171,6 +352,43 @@ do_test percentile-1.20 { SELECT percentile(x,50) from t1; } } {1 {Inf input to percentile()}} +do_test percentile-1.20.2 { + catchsql { + SELECT percentile_cont(x,0.50) from t1; + } +} {1 {Inf input to percentile_cont()}} +do_test percentile-1.20.3 { + catchsql { + SELECT percentile_disc(x,0.50) from t1; + } +} {1 {Inf input to percentile_disc()}} +do_test percentile-1.20.4 { + catchsql { + SELECT median(x) from t1; + } +} {1 {Inf input to median()}} +ifcapable ordered_set_aggregates { + do_test percentile-1.20.5 { + catchsql { + SELECT percentile(50) WITHIN GROUP (ORDER BY x) from t1; + } + } {1 {Inf input to percentile()}} + do_test percentile-1.20.6 { + catchsql { + SELECT percentile_cont(0.50) WITHIN GROUP (ORDER BY x) from t1; + } + } {1 {Inf input to percentile_cont()}} + do_test percentile-1.20.7 { + catchsql { + SELECT percentile_disc(0.50) WITHIN GROUP(ORDER BY X) from t1; + } + } {1 {Inf input to percentile_disc()}} + do_test percentile-1.20.8 { + catchsql { + SELECT median() WITHIN GROUP (ORDER BY x) from t1; + } + } {1 {Inf input to median()}} +} do_test percentile-1.21 { catchsql { UPDATE t1 SET x=-1.0e300*1.0e300 WHERE rowid=5; @@ -206,4 +424,172 @@ ifcapable vtab { } } +# median() as a window function. (2024-08-31) +# +do_execsql_test percentile-3.0 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c, d); + INSERT INTO t1 VALUES (1, 'A', 'one', 8.4), + (2, 'B', 'two', 7.1), + (3, 'C', 'three', 5.9), + (4, 'D', 'one', 11.0), + (5, 'E', 'two', 12.5), + (6, 'F', 'three', 0.0), + (7, 'G', 'one', 2.7); +} +foreach {id oba expr} { + 1 0 "median(d)" + 2 0 "percentile(d,50)" + 3 0 "percentile_cont(d,0.5)" + 4 1 "median() WITHIN GROUP (ORDER BY d)" + 5 1 "percentile(50) WITHIN GROUP (ORDER BY d)" + 6 1 "percentile_cont(0.5) WITHIN GROUP (ORDER BY d)" +} { + if {$oba} { + ifcapable !ordered_set_aggregates break + } + set sql "SELECT a, b, c, d, \ + group_concat(b,'.') OVER w1 AS 'elements', \ + $expr OVER w1 AS 'median' \ + FROM t1 \ + WINDOW w1 AS (ORDER BY c, a ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING)" + do_execsql_test percentile-3.$id.1 $sql { + 1 A one 8.4 A.D 9.7 + 4 D one 11.0 A.D.G 8.4 + 7 G one 2.7 D.G.C 5.9 + 3 C three 5.9 G.C.F 2.7 + 6 F three 0.0 C.F.B 5.9 + 2 B two 7.1 F.B.E 7.1 + 5 E two 12.5 B.E 9.8 + } + + set sql "SELECT a, b, c, d, \ + group_concat(b,'.') OVER w1 AS 'elements', \ + $expr OVER w1 AS 'median' \ + FROM t1 \ + WINDOW w1 AS (ORDER BY c, a \ + ROWS BETWEEN UNBOUNDED PRECEDING AND 1 FOLLOWING)" + do_execsql_test percentile-3.$id.2 $sql { + 1 A one 8.4 A.D 9.7 + 4 D one 11.0 A.D.G 8.4 + 7 G one 2.7 A.D.G.C 7.15 + 3 C three 5.9 A.D.G.C.F 5.9 + 6 F three 0.0 A.D.G.C.F.B 6.5 + 2 B two 7.1 A.D.G.C.F.B.E 7.1 + 5 E two 12.5 A.D.G.C.F.B.E 7.1 + } + + set sql "SELECT a, b, c, d, \ + group_concat(b,'.') OVER w1 AS 'elements', \ + $expr OVER w1 AS 'median' \ + FROM t1 \ + WINDOW w1 AS (ORDER BY c, a \ + ROWS BETWEEN 1 PRECEDING AND UNBOUNDED FOLLOWING)" + do_execsql_test percentile-3.$id.3 $sql { + 1 A one 8.4 A.D.G.C.F.B.E 7.1 + 4 D one 11.0 A.D.G.C.F.B.E 7.1 + 7 G one 2.7 D.G.C.F.B.E 6.5 + 3 C three 5.9 G.C.F.B.E 5.9 + 6 F three 0.0 C.F.B.E 6.5 + 2 B two 7.1 F.B.E 7.1 + 5 E two 12.5 B.E 9.8 + } +} + +# Test case adapted from examples shown at +# https://database.guide/3-functions-to-calculate-the-median-in-sql/ +# +do_execsql_test percential-4.0 { + CREATE TABLE products( + vendorId INT, + productId INTEGER PRIMARY KEY, + productName REAL, + price REAL + ); + INSERT INTO products VALUES + (1001, 17, 'Left-handed screwdriver', 25.99), + (1001, 49, 'Right-handed screwdriver', 25.99), + (1001, 216, 'Long weight (blue)', 14.75), + (1001, 31, 'Long weight (green)', 11.99), + (1002, 37, 'Sledge hammer', 33.49), + (1003, 7, 'Chainsaw', 245.00), + (1003, 8, 'Straw dog box', 55.99), + (1003, 12, 'Hammock', 11.01), + (1004, 113, 'Teapot', 12.45), + (1004, 117, 'Bottomless coffee mug', 9.99); +} +do_execsql_test percentile-4.1 { + SELECT VendorId, ProductId, /* ProductName,*/ Price, + avg(price) OVER (PARTITION BY vendorId) AS "Average", + median(price) OVER (PARTITION BY vendorId) AS "Median" + FROM products + ORDER BY vendorId, productId; +} { + 1001 17 25.99 19.68 20.37 + 1001 31 11.99 19.68 20.37 + 1001 49 25.99 19.68 20.37 + 1001 216 14.75 19.68 20.37 + 1002 37 33.49 33.49 33.49 + 1003 7 245.0 104.0 55.99 + 1003 8 55.99 104.0 55.99 + 1003 12 11.01 104.0 55.99 + 1004 113 12.45 11.22 11.22 + 1004 117 9.99 11.22 11.22 +} +do_execsql_test percentile-4.2 { + SELECT vendorId, median(price) FROM products + GROUP BY 1 ORDER BY 1; +} {1001 20.37 1002 33.49 1003 55.99 1004 11.22} + +do_execsql_test percentile-5.0 { + CREATE TABLE user(name TEXT, class TEXT, cost REAL); + INSERT INTO user VALUES + ('Alice', 'Y', 3578.27), + ('Bob', 'X', 3399.99), + ('Cindy', 'Z', 699.10), + ('Dave', 'Y', 3078.27), + ('Emma', 'Z', 2319.99), + ('Fred', 'Y', 539.99), + ('Gina', 'X', 2320.49), + ('Hank', 'W', 24.99), + ('Irma', 'W', 24.99), + ('Jake', 'X', 2234.99), + ('Kim', 'Y', 4319.99), + ('Liam', 'X', 4968.59), + ('Mia', 'W', 59.53), + ('Nate', 'W', 23.50); +} +do_execsql_test percentile-5.1 { + SELECT name, class, cost, + percentile(cost, 0) OVER w1 AS 'P0', + percentile(cost, 25) OVER w1 AS 'P1', + percentile(cost, 50) OVER w1 AS 'P2', + percentile(cost, 75) OVER w1 AS 'P3', + percentile(cost, 100) OVER w1 AS 'P4' + FROM user + WINDOW w1 AS (PARTITION BY class) + ORDER BY class, cost; +} { + Nate W 23.5 23.5 24.6175 24.99 33.625 59.53 + Hank W 24.99 23.5 24.6175 24.99 33.625 59.53 + Irma W 24.99 23.5 24.6175 24.99 33.625 59.53 + Mia W 59.53 23.5 24.6175 24.99 33.625 59.53 + Jake X 2234.99 2234.99 2299.115 2860.24 3792.14 4968.59 + Gina X 2320.49 2234.99 2299.115 2860.24 3792.14 4968.59 + Bob X 3399.99 2234.99 2299.115 2860.24 3792.14 4968.59 + Liam X 4968.59 2234.99 2299.115 2860.24 3792.14 4968.59 + Fred Y 539.99 539.99 2443.7 3328.27 3763.7 4319.99 + Dave Y 3078.27 539.99 2443.7 3328.27 3763.7 4319.99 + Alice Y 3578.27 539.99 2443.7 3328.27 3763.7 4319.99 + Kim Y 4319.99 539.99 2443.7 3328.27 3763.7 4319.99 + Cindy Z 699.1 699.1 1104.3225 1509.545 1914.7675 2319.99 + Emma Z 2319.99 699.1 1104.3225 1509.545 1914.7675 2319.99 +} + +# Fuzzer find. +do_execsql_test percentile-6.0 { + WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<12) + SELECT median(iif(n%2,0.1,1.0)) FROM c; +} 0.55 + finish_test diff --git a/test/permutations.test b/test/permutations.test index c099889067..02f4827189 100644 --- a/test/permutations.test +++ b/test/permutations.test @@ -10,9 +10,11 @@ #*********************************************************************** # -set testdir [file dirname $argv0] -source $testdir/tester.tcl -db close +if {[info vars ::trd::tcltest]==""} { + set testdir [file dirname $argv0] + source $testdir/tester.tcl + db close +} #------------------------------------------------------------------------- # test_suite NAME OPTIONS @@ -86,25 +88,38 @@ proc test_set {args} { # set alltests [list] foreach f [glob $testdir/*.test] { lappend alltests [file tail $f] } -foreach f [glob -nocomplain $testdir/../ext/rtree/*.test] { +foreach f [glob -nocomplain \ + $testdir/../ext/rtree/*.test \ + $testdir/../ext/fts5/test/*.test \ + $testdir/../ext/expert/*.test \ + $testdir/../ext/lsm1/test/*.test \ + $testdir/../ext/recover/*.test \ + $testdir/../ext/rbu/*.test \ + $testdir/../ext/intck/*.test \ +] { + lappend alltests $f +} +foreach f [glob -nocomplain $testdir/../ext/session/*.test] { lappend alltests $f } +unset f -if {$::tcl_platform(platform)!="unix"} { +if {$::tcl_platform(platform) ne "unix"} { set alltests [test_set $alltests -exclude crash.test crash2.test] } set alltests [test_set $alltests -exclude { - all.test async.test quick.test veryquick.test + all.test quick.test veryquick.test memleak.test permutations.test soak.test fts3.test mallocAll.test rtree.test full.test extraquick.test + session.test rbu.test }] set allquicktests [test_set $alltests -exclude { - async2.test async3.test backup_ioerr.test corrupt.test + backup_ioerr.test corrupt.test corruptC.test crash.test crash2.test crash3.test crash4.test crash5.test crash6.test crash7.test delete3.test e_fts3.test fts3rnd.test fkey_malloc.test fuzz.test fuzz3.test fuzz_malloc.test in2.test loadext.test - misc7.test mutex2.test notify2.test onefile.test pagerfault2.test + misc7.test mutex2.test onefile.test pagerfault2.test savepoint4.test savepoint6.test select9.test speed1.test speed1p.test speed2.test speed3.test speed4.test speed4p.test sqllimits1.test tkt2686.test thread001.test thread002.test @@ -113,22 +128,44 @@ set allquicktests [test_set $alltests -exclude { vtab_err.test walslow.test walcrash.test walcrash3.test walthread.test rtree3.test indexfault.test securedel2.test sort3.test sort4.test fts4growth.test fts4growth2.test - bigsort.test rbu.test walprotocol.test mmap4.test fuzzer2.test + bigsort.test walprotocol.test mmap4.test fuzzer2.test walcrash2.test e_fkey.test backup.test fts4merge.test fts4merge2.test fts4merge4.test fts4check.test + fts4merge5.test fts3cov.test fts3snippet.test fts3corrupt2.test fts3an.test fts3defer.test fts4langid.test fts3sort.test fts5unicode.test + recovercorrupt.test rtree4.test + sessionbig.test + + writecrash.test view3.test + fts5dlidx.test fts5ac.test fts4merge3.test fts5prefix.test + sessionB.test }] if {[info exists ::env(QUICKTEST_INCLUDE)]} { set allquicktests [concat $allquicktests $::env(QUICKTEST_INCLUDE)] } if {[info exists ::env(QUICKTEST_OMIT)]} { - foreach x [split $::env(QUICKTEST_OMIT) ,] { - regsub -all \\y$x\\y $allquicktests {} allquicktests + # If environment variable QUICKTEST_OMIT is set, it is a comma-separated + # list of regular expressions to match against test file names in + # the "allquicktests" set. Any matches are excluded. Only the filename + # is matched, not any directory component of the path. + set all [list] + foreach a $allquicktests { + set bIn 1 + foreach x [split $::env(QUICKTEST_OMIT) ,] { + if {[regexp $x [file tail $a]]} { + set bIn 0 + break + } + } + if {$bIn} { + lappend all $a + } } + set allquicktests $all } # If the TEST_FAILURE environment variable is set, it means that we what to @@ -157,7 +194,14 @@ test_suite "veryquick" -prefix "" -description { This test suite is the same as the "quick" tests, except that some files that test malloc and IO errors are omitted. } -files [ - test_set $allquicktests -exclude *malloc* *ioerr* *fault* *bigfile* *_err* + test_set $allquicktests -exclude *malloc* *ioerr* *fault* *bigfile* *_err* \ + *fts5corrupt* *fts5big* *fts5aj* *rbucrash* +] + +test_suite "shell" -prefix "" -description { + Run tests of the command-line shell +} -files [ + test_set [glob $testdir/shell*.test] ] test_suite "extraquick" -prefix "" -description { @@ -184,8 +228,9 @@ test_suite "valgrind" -prefix "" -description { fail under valgrind) omitted. } -files [ test_set $allquicktests -exclude *malloc* *ioerr* *fault* *_err* wal.test \ - shell*.test crash8.test atof1.test selectG.test \ - tkt-fc62af4523.test numindex1.test + shell2.test shell6.test shell7.test \ + crash8.test atof1.test selectG.test \ + tkt-fc62af4523.test numindex1.test corruptK.test ] -initialize { set ::G(valgrind) 1 } -shutdown { @@ -239,22 +284,27 @@ test_suite "threads" -prefix "" -description { test_suite "fts3" -prefix "" -description { All FTS3 tests except fts3rnd.test. } -files { - fts3aa.test fts3ab.test fts3ac.test fts3ad.test fts3ae.test - fts3af.test fts3ag.test fts3ah.test fts3ai.test fts3aj.test - fts3ak.test fts3al.test fts3am.test fts3an.test fts3ao.test - fts3atoken.test fts3b.test fts3c.test fts3cov.test fts3d.test - fts3defer.test fts3defer2.test fts3e.test fts3expr.test fts3expr2.test - fts3expr3.test - fts3near.test fts3query.test fts3shared.test fts3snippet.test - fts3sort.test - fts3fault.test fts3malloc.test fts3matchinfo.test - fts3aux1.test fts3comp1.test fts3auto.test - fts4aa.test fts4content.test - fts3conf.test fts3prefix.test fts3fault2.test fts3corrupt.test - fts3corrupt2.test fts3first.test fts4langid.test fts4merge.test - fts4check.test fts4unicode.test fts4noti.test - fts3varint.test - fts4growth.test fts4growth2.test + fts3aa.test fts3ab.test fts3ac.test fts3ad.test + fts3ae.test fts3af.test fts3ag.test fts3ah.test + fts3ai.test fts3aj.test fts3ak.test fts3al.test + fts3am.test fts3an.test fts3ao.test fts3atoken.test + fts3auto.test fts3aux1.test fts3aux2.test fts3b.test + fts3comp1.test fts3conf.test fts3corrupt2.test fts3corrupt.test + fts3corrupt4.test + fts3cov.test fts3c.test fts3defer2.test fts3defer3.test + fts3defer.test fts3drop.test fts3d.test fts3e.test + fts3expr2.test fts3expr3.test fts3expr4.test fts3expr5.test + fts3expr.test fts3fault2.test fts3fault.test fts3first.test + fts3join.test fts3malloc.test fts3matchinfo.test fts3near.test + fts3offsets.test fts3prefix2.test fts3prefix.test fts3query.test + fts3shared.test fts3snippet.test fts3sort.test fts3tok1.test + fts3tok_err.test fts3varint.test fts4aa.test fts4check.test + fts4content.test fts4docid.test fts4growth2.test fts4growth.test + fts4incr.test fts4langid.test fts4lastrowid.test fts4merge2.test + fts4merge4.test fts4merge.test fts4noti.test fts4onepass.test + fts4opt.test fts4unicode.test + fts3corrupt3.test + fts3misc.test } test_suite "fts5" -prefix "" -description { @@ -269,6 +319,16 @@ test_suite "fts5-light" -prefix "" -description { -exclude *corrupt* *fault* *big* *fts5aj* ] +test_suite "window" -prefix "" -description { + All window function related tests . +} -files [ + test_set [glob -nocomplain $::testdir/window*.test] +] + +test_suite "lsm1" -prefix "" -description { + All LSM1 tests. +} -files [glob -nocomplain $::testdir/../ext/lsm1/test/*.test] + test_suite "nofaultsim" -prefix "" -description { "Very" quick test suite. Runs in less than 5 minutes on a workstation. This test suite is the same as the "quick" tests, except that some files @@ -372,6 +432,30 @@ test_suite "vfslog" -prefix "" -description { wal* mmap* ] +test_suite "atomic-batch-write" -prefix "" -description { + Like veryquick.test, but must be run on a file-system that supports + atomic-batch-writes. Tests that depend on the journal file being present + are omitted. +} -files [ + test_set $allquicktests -exclude *malloc* *ioerr* *fault* *bigfile* *_err* \ + *fts5corrupt* *fts5big* *fts5aj* \ + crash8.test delete_db.test \ + exclusive.test journal3.test \ + journal1.test \ + jrnlmode.test jrnlmode2.test \ + lock4.test pager1.test \ + pager3.test sharedA.test \ + symlink.test stmt.test \ + sync.test sync2.test \ + tempdb.test tkt3457.test \ + vacuum5.test wal2.test \ + walmode.test zerodamage.test +] -initialize { + if {[atomic_batch_write test.db]==0} { + error "File system does NOT support atomic-batch-write" + } +} + lappend ::testsuitelist xxx #------------------------------------------------------------------------- # Define the coverage related test suites: @@ -381,10 +465,18 @@ lappend ::testsuitelist xxx test_suite "coverage-wal" -description { Coverage tests for file wal.c. } -files { - wal.test wal2.test wal3.test walmode.test - walbak.test walhook.test walcrash2.test walcksum.test - walfault.test walbig.test walnoshm.test - wal5.test + wal.test wal2.test wal3.test wal4.test wal5.test + wal64k.test wal6.test wal7.test wal8.test wal9.test + walbak.test walbig.test walblock.test walcksum.test walcrash2.test + walcrash3.test walcrash4.test walcrash.test walfault.test walhook.test + walmode.test walnoshm.test waloverwrite.test walpersist.test + walprotocol2.test walprotocol.test walro2.test walrofault.test + walro.test walshared.test walslow.test walvfs.test + walfault2.test + nockpt.test + + snapshot2.test snapshot3.test snapshot4.test + snapshot_fault.test snapshot.test snapshot_up.test } test_suite "coverage-pager" -description { @@ -398,8 +490,8 @@ test_suite "coverage-analyze" -description { Coverage tests for file analyze.c. } -files { analyze3.test analyze4.test analyze5.test analyze6.test - analyze7.test analyze8.test analyze9.test analyzeA.test - analyze.test analyzeB.test mallocA.test + analyze7.test analyze8.test analyze9.test + analyze.test mallocA.test } test_suite "coverage-sorter" -description { @@ -414,33 +506,31 @@ lappend ::testsuitelist xxx # Define the permutation test suites: # -# Run some tests using pre-allocated page and scratch blocks. +# Run some tests using pre-allocated page blocks. # # mmap1.test is excluded because a good number of its tests depend on # the page-cache being larger than the database. But this permutation # causes the effective limit on the page-cache to be just 24 pages. # test_suite "memsubsys1" -description { - Tests using pre-allocated page and scratch blocks + Tests using pre-allocated page blocks } -files [ test_set $::allquicktests -exclude ioerr5.test malloc5.test mmap1.test ] -initialize { test_set_config_pagecache 4096 24 catch {db close} sqlite3_shutdown - sqlite3_config_scratch 25000 1 sqlite3_initialize autoinstall_test_functions } -shutdown { test_restore_config_pagecache catch {db close} sqlite3_shutdown - sqlite3_config_scratch 0 0 sqlite3_initialize autoinstall_test_functions } -# Run some tests using pre-allocated page and scratch blocks. This time +# Run some tests using pre-allocated page blocks. This time # the allocations are too small to use in most cases. # # Both ioerr5.test and malloc5.test are excluded because they test the @@ -448,21 +538,19 @@ test_suite "memsubsys1" -description { # This functionality is disabled if a pre-allocated page block is provided. # test_suite "memsubsys2" -description { - Tests using small pre-allocated page and scratch blocks + Tests using small pre-allocated page blocks } -files [ test_set $::allquicktests -exclude ioerr5.test malloc5.test ] -initialize { test_set_config_pagecache 512 5 catch {db close} sqlite3_shutdown - sqlite3_config_scratch 1000 1 sqlite3_initialize autoinstall_test_functions } -shutdown { test_restore_config_pagecache catch {db close} sqlite3_shutdown - sqlite3_config_scratch 0 0 sqlite3_initialize autoinstall_test_functions } @@ -510,21 +598,12 @@ test_suite "singlethread" -description { test_suite "nomutex" -description { Tests run with the SQLITE_OPEN_MULTITHREADED flag passed to sqlite3_open(). } -initialize { - rename sqlite3 sqlite3_nomutex - proc sqlite3 {args} { - if {[string range [lindex $args 0] 0 0] ne "-"} { - lappend args -fullmutex 0 -nomutex 1 - } - uplevel [concat sqlite3_nomutex $args] - } + set ::G(perm:sqlite3_args) [list -fullmutex 0 -nomutex 1] } -files { delete.test delete2.test insert.test rollback.test select1.test select2.test trans.test update.test vacuum.test types.test types2.test types3.test -} -shutdown { - rename sqlite3 {} - rename sqlite3_nomutex sqlite3 -} +} # Run some tests in SQLITE_CONFIG_MULTITHREAD mode. # @@ -553,20 +632,11 @@ test_suite "multithread" -description { test_suite "fullmutex" -description { Tests run in SQLITE_OPEN_FULLMUTEX mode } -initialize { - rename sqlite3 sqlite3_fullmutex - proc sqlite3 {args} { - if {[string range [lindex $args 0] 0 0] ne "-"} { - lappend args -nomutex 0 -fullmutex 1 - } - uplevel [concat sqlite3_fullmutex $args] - } + set ::G(perm:sqlite3_args) [list -nomutex 0 -fullmutex 1] } -files { delete.test delete2.test insert.test rollback.test select1.test select2.test trans.test update.test vacuum.test types.test types2.test types3.test -} -shutdown { - rename sqlite3 {} - rename sqlite3_fullmutex sqlite3 } # Run some tests using the "onefile" demo. @@ -574,19 +644,10 @@ test_suite "fullmutex" -description { test_suite "onefile" -description { Run some tests using the "test_onefile.c" demo } -initialize { - rename sqlite3 sqlite3_onefile - proc sqlite3 {args} { - if {[string range [lindex $args 0] 0 0] ne "-"} { - lappend args -vfs fs - } - uplevel [concat sqlite3_onefile $args] - } + set ::G(perm:sqlite3_args) [list -vfs fs] } -files { conflict.test insert.test insert2.test insert3.test rollback.test select1.test select2.test select3.test -} -shutdown { - rename sqlite3 {} - rename sqlite3_onefile sqlite3 } # Run some tests using UTF-16 databases. @@ -598,7 +659,7 @@ test_suite "utf16" -description { } -files { alter.test alter3.test analyze.test analyze3.test analyze4.test analyze5.test analyze6.test - analyze7.test analyze8.test analyze9.test analyzeA.test analyzeB.test + analyze7.test analyze8.test analyze9.test auth.test bind.test blob.test capi2.test capi3.test collate1.test collate2.test collate3.test collate4.test collate5.test collate6.test conflict.test date.test delete.test expr.test fkey1.test func.test @@ -612,6 +673,7 @@ test_suite "utf16" -description { trace.test trigger1.test trigger2.test trigger3.test trigger4.test types2.test types.test unique.test update.test vacuum.test view.test where.test + bestindex1.test } # Run some tests in exclusive locking mode. @@ -719,15 +781,16 @@ test_suite "inmemory_journal" -description { ioerr.test ioerr2.test ioerr3.test ioerr4.test ioerr5.test vacuum3.test incrblob_err.test diskfull.test backup_ioerr.test e_fts3.test fts3cov.test fts3malloc.test fts3rnd.test - fts3snippet.test mmapfault.test + fts3snippet.test mmapfault.test sessionfault.test sessionfault2.test # Exclude test scripts that use tcl IO to access journal files or count # the number of fsync() calls. pager.test exclusive.test jrnlmode.test sync.test misc1.test journal1.test conflict.test crash8.test tkt3457.test io.test - journal3.test 8_3_names.test + journal3.test 8_3_names.test shmlock.test + pendingrace.test - pager1.test async4.test corrupt.test filefmt.test pager2.test + pager1.test corrupt.test filefmt.test pager2.test corrupt5.test corruptA.test pageropt.test # Exclude stmt.test, which expects sub-journals to use temporary files. @@ -737,117 +800,127 @@ test_suite "inmemory_journal" -description { # WAL mode is different. wal* tkt-2d1a5c67d.test backcompat.test e_wal* rowallock.test + + # This test does not work as the "PRAGMA journal_mode = memory" + # statement switches the database out of wal mode at inopportune + # times. + snapshot_fault.test + + # This test assumes a journal file is created on disk. + delete_db.test + + # This test depends on a successful recovery from the pager error + # state. Which is not possible with an in-memory journal + fts5fault1.test + + recoverpgsz.test }] -ifcapable mem3 { - test_suite "memsys3" -description { - Run tests using the allocator in mem3.c. - } -files [test_set $::allquicktests -exclude { - autovacuum.test delete3.test manydb.test - bigrow.test incrblob2.test memdb.test - bitvec.test index2.test memsubsys1.test - capi3c.test ioerr.test memsubsys2.test - capi3.test join3.test pagesize.test - collate5.test limit.test backup_ioerr.test - backup_malloc.test - }] -initialize { - catch {db close} - sqlite3_reset_auto_extension - sqlite3_shutdown - sqlite3_config_heap 25000000 0 - sqlite3_config_lookaside 0 0 - ifcapable mem5 { - # If both memsys3 and memsys5 are enabled in the build, the call to - # [sqlite3_config_heap] will initialize the system to use memsys5. - # The following overrides this preference and installs the memsys3 - # allocator. - sqlite3_install_memsys3 - } - install_malloc_faultsim 1 - sqlite3_initialize - autoinstall_test_functions - } -shutdown { - catch {db close} - sqlite3_shutdown - sqlite3_config_heap 0 0 - sqlite3_config_lookaside 100 500 - install_malloc_faultsim 1 - sqlite3_initialize - autoinstall_test_functions +test_suite "memsys3" -description { + Run tests using the allocator in mem3.c. +} -files [test_set $::allquicktests -exclude { + autovacuum.test delete3.test manydb.test + bigrow.test incrblob2.test memdb.test + bitvec.test index2.test memsubsys1.test + capi3c.test ioerr.test memsubsys2.test + capi3.test join3.test pagesize.test + collate5.test limit.test backup_ioerr.test + backup_malloc.test +}] -initialize { + catch {db close} + sqlite3_reset_auto_extension + sqlite3_shutdown + sqlite3_config_heap 25000000 0 + sqlite3_config_lookaside 0 0 + ifcapable mem5 { + # If both memsys3 and memsys5 are enabled in the build, the call to + # [sqlite3_config_heap] will initialize the system to use memsys5. + # The following overrides this preference and installs the memsys3 + # allocator. + sqlite3_install_memsys3 } + install_malloc_faultsim 1 + sqlite3_initialize + autoinstall_test_functions +} -shutdown { + catch {db close} + sqlite3_shutdown + sqlite3_config_heap 0 0 + sqlite3_config_lookaside 100 500 + install_malloc_faultsim 1 + sqlite3_initialize + autoinstall_test_functions } -ifcapable mem5 { - test_suite "memsys5" -description { - Run tests using the allocator in mem5.c. - } -files [test_set $::allquicktests -exclude { - autovacuum.test delete3.test manydb.test - bigrow.test incrblob2.test memdb.test - bitvec.test index2.test memsubsys1.test - capi3c.test ioerr.test memsubsys2.test - capi3.test join3.test pagesize.test - collate5.test limit.test zeroblob.test - }] -initialize { - catch {db close} - sqlite3_shutdown - sqlite3_config_heap 25000000 64 - sqlite3_config_lookaside 0 0 - install_malloc_faultsim 1 - sqlite3_initialize - autoinstall_test_functions - } -shutdown { - catch {db close} - sqlite3_shutdown - sqlite3_config_heap 0 0 - sqlite3_config_lookaside 100 500 - install_malloc_faultsim 1 - sqlite3_initialize - autoinstall_test_functions - } +test_suite "memsys5" -description { + Run tests using the allocator in mem5.c. +} -files [test_set $::allquicktests -exclude { + autovacuum.test delete3.test manydb.test + bigrow.test incrblob2.test memdb.test + bitvec.test index2.test memsubsys1.test + capi3c.test ioerr.test memsubsys2.test + capi3.test join3.test pagesize.test + collate5.test limit.test zeroblob.test +}] -initialize { + catch {db close} + sqlite3_shutdown + sqlite3_config_heap 25000000 64 + sqlite3_config_lookaside 0 0 + install_malloc_faultsim 1 + sqlite3_initialize + autoinstall_test_functions +} -shutdown { + catch {db close} + sqlite3_shutdown + sqlite3_config_heap 0 0 + sqlite3_config_lookaside 100 500 + install_malloc_faultsim 1 + sqlite3_initialize + autoinstall_test_functions +} - test_suite "memsys5-2" -description { - Run tests using the allocator in mem5.c in a different configuration. - } -files { - select1.test - } -initialize { - catch {db close} - sqlite3_shutdown - sqlite3_config_memstatus 0 - sqlite3_config_heap 40000000 16 - sqlite3_config_lookaside 0 0 - install_malloc_faultsim 1 - sqlite3_initialize - autoinstall_test_functions - } -shutdown { - catch {db close} - sqlite3_shutdown - sqlite3_config_heap 0 0 - sqlite3_config_lookaside 100 500 - install_malloc_faultsim 1 - sqlite3_initialize - autoinstall_test_functions - } +test_suite "memsys5-2" -description { + Run tests using the allocator in mem5.c in a different configuration. +} -files { + select1.test +} -initialize { + catch {db close} + sqlite3_shutdown + sqlite3_config_memstatus 0 + sqlite3_config_heap 40000000 16 + sqlite3_config_lookaside 0 0 + install_malloc_faultsim 1 + sqlite3_initialize + autoinstall_test_functions +} -shutdown { + catch {db close} + sqlite3_shutdown + sqlite3_config_heap 0 0 + sqlite3_config_lookaside 100 500 + install_malloc_faultsim 1 + sqlite3_initialize + autoinstall_test_functions } -ifcapable threadsafe { - test_suite "no_mutex_try" -description { - The sqlite3_mutex_try() interface always fails - } -files [ - test_set $::allquicktests -exclude mutex1.test mutex2.test - ] -initialize { - catch {db close} - sqlite3_shutdown - install_mutex_counters 1 - set ::disable_mutex_try 1 - sqlite3_initialize - autoinstall_test_functions - } -shutdown { - catch {db close} - sqlite3_shutdown - install_mutex_counters 0 - sqlite3_initialize - autoinstall_test_functions - } +test_suite "no_mutex_try" -description { + The sqlite3_mutex_try() interface always fails +} -files [ + test_set $::allquicktests -exclude mutex1.test mutex2.test +] -initialize { + catch {db close} + sqlite3_shutdown + install_mutex_counters 1 + set ::disable_mutex_try 1 + sqlite3_initialize + autoinstall_test_functions +} -shutdown { + catch {db close} + catch {db2 close} + catch {db3 close} + sqlite3_shutdown + install_mutex_counters 0 + sqlite3_initialize + autoinstall_test_functions } # run_tests "crash_safe_append" -description { @@ -875,24 +948,14 @@ ifcapable threadsafe { test_suite "safe_append" -description { Run some tests on a SAFE_APPEND file-system. } -initialize { - rename sqlite3 sqlite3_safeappend - proc sqlite3 {args} { - if {[string range [lindex $args 0] 0 0] ne "-"} { - lappend args -vfs devsym - } - uplevel [concat sqlite3_safeappend $args] - } + set ::G(perm:sqlite3_args) [list -vfs devsym] sqlite3_simulate_device -char safe_append -} -shutdown { - rename sqlite3 {} - rename sqlite3_shutdown sqlite3 } -files [ - test_set $::allquicktests shared_err.test -exclude async3.test + test_set $::allquicktests shared_err.test ] # The set of tests to run on the alternative-pcache set perm-alt-pcache-testset { - async.test attach.test delete.test delete2.test index.test @@ -933,7 +996,11 @@ test_suite "journaltest" -description { unregister_jt_vfs } -files [test_set $::allquicktests -exclude { wal* incrvacuum.test ioerr.test corrupt4.test io.test crash8.test - async4.test bigfile.test backcompat.test + bigfile.test backcompat.test e_wal* fstat.test mmap2.test + pager1.test syscall.test tkt3457.test *malloc* mmap* multiplex* nolock* + pager2.test *fault* rowal* snapshot* superlock* symlink.test + delete_db.test shmlock.test chunksize.test + busy2.test avfs.test external_reader.test }] if {[info commands register_demovfs] != ""} { @@ -970,34 +1037,100 @@ test_suite "rtree" -description { All R-tree related tests. Provides coverage of source file rtree.c. } -files [glob -nocomplain $::testdir/../ext/rtree/*.test] +test_suite "session" -description { + All session module related tests. +} -files [glob -nocomplain $::testdir/../ext/session/*.test] + +test_suite "session_eec" -description { + All session module related tests with sqlite3_extended_result_codes() set. +} -files [ + glob -nocomplain $::testdir/../ext/session/*.test +] -dbconfig { + sqlite3_extended_result_codes $::dbhandle 1 +} + +test_suite "session_strm" -description { + All session module related tests using the streaming APIs. +} -files [ + glob -nocomplain $::testdir/../ext/session/*.test +] -dbconfig { + set ::sqlite3session_streams 1 +} + test_suite "rbu" -description { RBU tests. } -files [ - test_set [glob -nocomplain $::testdir/../ext/rbu/*.test] -exclude rbu.test + test_set [glob -nocomplain $::testdir/../ext/rbu/*.test] ] test_suite "no_optimization" -description { Run test scripts with optimizations disabled using the sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS) interface. -} -files { - where.test where2.test where3.test where4.test where5.test - where6.test where7.test where8.test where9.test - whereA.test whereB.test wherelimit.test - select1.test select2.test select3.test select4.test select5.test - select7.test select8.test selectA.test selectC.test -} -dbconfig { +} -files [ + test_set \ + [glob -nocomplain $::testdir/window*.test] \ + where.test where2.test where3.test where4.test where5.test \ + where6.test where7.test where8.test where9.test \ + whereA.test whereB.test wherelimit.test \ + select1.test select2.test select3.test select4.test select5.test \ + select7.test select8.test selectA.test selectC.test \ + -exclude windowpushd.test +] -dbconfig { optimization_control $::dbhandle all 0 } test_suite "prepare" -description { Run tests with the db connection using sqlite3_prepare() instead of _v2(). } -dbconfig { - db_use_legacy_prepare $::dbhandle 1 + $::dbhandle version -use-legacy-prepare 1 #$::dbhandle cache size 0 } -files [ - test_set $allquicktests -exclude *malloc* *ioerr* *fault* + test_set $allquicktests -exclude *malloc* *ioerr* *fault* \ + stmtvtab1.test index9.test ] +test_suite "sorterref" -prefix "" -description { + Run the "veryquick" test suite with SQLITE_CONFIG_SORTERREF_SIZE set + to 0 so that sorter-references are used whenever possible. +} -files [ + test_set $allquicktests -exclude *malloc* *ioerr* *fault* *bigfile* *_err* \ + *fts5corrupt* *fts5big* *fts5aj* +] -initialize { + catch {db close} + sqlite3_shutdown + sqlite3_config_sorterref 0 + sqlite3_initialize + autoinstall_test_functions +} -shutdown { + catch {db close} + sqlite3_shutdown + sqlite3_config_sorterref -1 + sqlite3_initialize + autoinstall_test_functions +} + +test_suite "maindbname" -prefix "" -description { + Run the "veryquick" test suite with SQLITE_DBCONFIG_MAINDBNAME used to + set the name of database 0 to "icecube". +} -files [ + test_set $allquicktests -exclude *malloc* *ioerr* *fault* *bigfile* *_err* \ + *fts5corrupt* *fts5big* *fts5aj* +] -dbconfig { + dbconfig_maindbname_icecube $::dbhandle +} + +test_suite "win_unc_locking" -prefix "" -description { + Run the "wal*" tests with UNC style single fd locking. +} -files [ + test_set \ + {*}[glob [file join $testdir wal*]] \ + -exclude *fault* *malloc* *crash* *slow* walsetlk.test +] -initialize { + set sqlite3_win_test_unc_locking 1 +} -shutdown { + set sqlite3_win_test_unc_locking 0 +} + # End of tests ############################################################################# @@ -1008,32 +1141,54 @@ test_suite "prepare" -description { # -description TITLE # -initialize SCRIPT # -shutdown SCRIPT -# -presql SQL # -files LIST-OF-FILES # -prefix NAME +# -dbconfig SCRIPT # proc run_tests {name args} { + set options(-initialize) "" + set options(-shutdown) "" + set options(-prefix) "" + set options(-dbconfig) "" + set options(-presql) "" + array set options $args set ::G(perm:name) $name set ::G(perm:prefix) $options(-prefix) - set ::G(perm:presql) $options(-presql) set ::G(isquick) 1 set ::G(perm:dbconfig) $options(-dbconfig) + set ::G(perm:presql) $options(-presql) - uplevel $options(-initialize) + set filelist [lsort $options(-files)] + if {[info exists ::env(TCLTEST_PART)]} { + regexp {^([0-9]*)/([0-9]*)$} $::env(TCLTEST_PART) -> A B + set nFile [expr {([llength $filelist]+$B-1)/$B}] + set filelist [lrange $filelist [expr ($A-1)*$nFile] [expr $A*$nFile-1]] + } - foreach file [lsort $options(-files)] { + foreach file $filelist { if {[file tail $file] == $file} { set file [file join $::testdir $file] } + + if {[info exists ::env(SQLITE_TEST_PATTERN_LIST)]} { + set ok 0 + foreach p $::env(SQLITE_TEST_PATTERN_LIST) { + set p [string map {% *} $p] + if {[string match $p [file tail $file]]} {set ok 1 ; break} + } + if {!$ok} continue + } + + uplevel $options(-initialize) slave_test_file $file + uplevel $options(-shutdown) + unset -nocomplain ::G(perm:sqlite3_args) } - uplevel $options(-shutdown) - unset ::G(perm:name) unset ::G(perm:prefix) - unset ::G(perm:presql) unset ::G(perm:dbconfig) + unset ::G(perm:presql) } proc run_test_suite {name} { @@ -1047,34 +1202,70 @@ proc help {} { puts "Usage: $::argv0 TESTSUITE ?TESTFILE?" puts "" puts "Available test-suites are:" + + set iPos 0 foreach k $::testsuitelist { - if {[info exists ::testspec($k)]==0} { - puts " ----------------------------------------" - puts "" - } else { - array set o $::testspec($k) - puts "Test suite: \"$k\"" - set d [string trim $o(-description)] - set d [regsub {\n *} $d "\n "] - puts " $d" - puts "" + if {[info exists ::testspec($k)]} { + switch $iPos { + 0 { + puts "" + puts -nonewline " [format %-30s $k]" + } + + 1 { + puts -nonewline [format %-30s $k] + } + + 2 { + puts -nonewline $k + } + } + + set iPos [expr (($iPos+1) % 3)] } } + puts "" exit -1 } -if {[info script] == $argv0} { +if {[file tail $argv0] == "permutations.test"} { proc main {argv} { if {[llength $argv]==0} { help } else { - set suite [lindex $argv 0] - if {[info exists ::testspec($suite)]==0} help + + # See if the first argument is a named test-suite. + # + set suite [file tail [lindex $argv 0]] + if {[info exists ::testspec($suite)]} { + set S $::testspec($suite) + set i 1 + } else { + set suite default + set S [list] + set i 0 + } + set extra "" - if {[llength $argv]>1} { set extra [list -files [lrange $argv 1 end]] } - eval run_tests $suite $::testspec($suite) $extra + if {$i < [llength $argv] && [string range [lindex $argv $i] 0 0]!="-" } { + set files [list] + for {} {$i < [llength $argv]} {incr i} { + set pattern [string map {% *} [lindex $argv $i]] + if {[string range $pattern 0 0]=="-"} break + foreach f $::alltests { + set tail [file tail $f] + if {[lsearch $files $f]<0 && [string match $pattern $tail]} { + lappend files $f + } + } + } + set extra [list -files $files] + } + + eval [list run_tests $suite] $S $extra } } main $argv + set argv {} finish_test } diff --git a/test/pg_common.tcl b/test/pg_common.tcl new file mode 100644 index 0000000000..dd16659a67 --- /dev/null +++ b/test/pg_common.tcl @@ -0,0 +1,175 @@ +# 2018 May 19 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +package require sqlite3 +package require Pgtcl + +set db [pg_connect -conninfo "dbname=postgres user=postgres password=postgres"] +sqlite3 sqlite "" + +proc execsql {sql} { + + set sql [string map {{WITHOUT ROWID} {}} $sql] + + set lSql [list] + set frag "" + while {[string length $sql]>0} { + set i [string first ";" $sql] + if {$i>=0} { + append frag [string range $sql 0 $i] + set sql [string range $sql $i+1 end] + if {[sqlite complete $frag]} { + lappend lSql $frag + set frag "" + } + } else { + set frag $sql + set sql "" + } + } + if {$frag != ""} { + lappend lSql $frag + } + #puts $lSql + + set ret "" + set nChar 0 + foreach stmt $lSql { + set res [pg_exec $::db $stmt] + set err [pg_result $res -error] + if {$err!=""} { error $err } + + for {set i 0} {$i < [pg_result $res -numTuples]} {incr i} { + set t [pg_result $res -getTuple $i] + set nNew [string length $t] + if {$nChar>0 && ($nChar+$nNew+3)>75} { + append ret "\n " + set nChar 0 + } else { + if {$nChar>0} { + append ret " " + incr nChar 3 + } + } + incr nChar $nNew + append ret $t + } + pg_result $res -clear + } + + set ret +} + +proc execsql_test {tn sql} { + set res [execsql $sql] + set sql [string map {string_agg group_concat} $sql] + # set sql [string map [list {NULLS FIRST} {}] $sql] + # set sql [string map [list {NULLS LAST} {}] $sql] + puts $::fd "do_execsql_test $tn {" + puts $::fd " [string trim $sql]" + puts $::fd "} {$res}" + puts $::fd "" +} + +proc errorsql_test {tn sql} { + set rc [catch {execsql $sql} msg] + if {$rc==0} { + error "errorsql_test SQL did not cause an error!" + } + set msg [lindex [split [string trim $msg] "\n"] 0] + puts $::fd "# PG says $msg" + set sql [string map {string_agg group_concat} $sql] + puts $::fd "do_test $tn { catch { execsql {" + puts $::fd " [string trim $sql]" + puts $::fd "} } } 1" + puts $::fd "" +} + +# Same as [execsql_test], except coerce all results to floating point values +# with two decimal points. +# +proc execsql_float_test {tn sql} { + set F "%.4f" + set T 0.0001 + set res [execsql $sql] + set res2 [list] + foreach r $res { + if {$r != ""} { set r [format $F $r] } + lappend res2 $r + } + + set sql [string trim $sql] +puts $::fd [subst -nocommands { +do_test $tn { + set myres {} + foreach r [db eval {$sql}] { + lappend myres [format $F [set r]] + } + set res2 {$res2} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-$T) || [set r]>([set r2]+$T)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} +}] +} + +proc start_test {name date} { + set dir [file dirname $::argv0] + set output [file join $dir $name.test] + set ::fd [open $output w] +puts $::fd [string trimleft " +# $date +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. +# + +#################################################### +# DO NOT EDIT! THIS FILE IS AUTOMATICALLY GENERATED! +#################################################### +"] + puts $::fd {set testdir [file dirname $argv0]} + puts $::fd {source $testdir/tester.tcl} + puts $::fd "set testprefix $name" + puts $::fd "" +} + +proc -- {args} { + puts $::fd "# $args" +} + +proc ========== {args} { + puts $::fd "#[string repeat = 74]" + puts $::fd "" +} + +proc finish_test {} { + puts $::fd finish_test + close $::fd +} + +proc ifcapable {arg} { + puts $::fd "ifcapable $arg { finish_test ; return }" +} + diff --git a/test/pragma.test b/test/pragma.test index befa5cf548..5249cc7c41 100644 --- a/test/pragma.test +++ b/test/pragma.test @@ -251,6 +251,31 @@ do_test pragma-1.14.4 { PRAGMA synchronous; } } {2} + +do_execsql_test 1.15.1 { + PRAGMA default_cache_size = 0; +} +do_execsql_test 1.15.2 { + PRAGMA default_cache_size; +} $DFLT_CACHE_SZ +do_execsql_test 1.15.3 { + PRAGMA default_cache_size = -500; +} +do_execsql_test 1.15.4 { + PRAGMA default_cache_size; +} 500 +do_execsql_test 1.15.3 { + PRAGMA default_cache_size = 500; +} +do_execsql_test 1.15.4 { + PRAGMA default_cache_size; +} 500 +db close +hexio_write test.db 48 FFFFFF00 +sqlite3 db test.db +do_execsql_test 1.15.4 { + PRAGMA default_cache_size; +} 256 } ;# ifcapable pager_pragmas # Test turning "flag" pragmas on and off. @@ -347,31 +372,38 @@ ifcapable attach { db close sqlite3 db test.db execsql {PRAGMA integrity_check} - } {{row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2}} + } {{wrong # of entries in index i2} {row 1 missing from index i2} {row 2 missing from index i2}} do_test pragma-3.3 { execsql {PRAGMA integrity_check=1} - } {{row 1 missing from index i2}} + } {{wrong # of entries in index i2}} do_test pragma-3.4 { execsql { ATTACH DATABASE 'test.db' AS t2; PRAGMA integrity_check } - } {{row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2}} + } {{wrong # of entries in index i2} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2} {row 1 missing from index i2} {row 2 missing from index i2}} do_test pragma-3.5 { execsql { PRAGMA integrity_check=4 } - } {{row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2} {row 1 missing from index i2}} - do_test pragma-3.6 { - execsql { - PRAGMA integrity_check=xyz - } - } {{row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2}} + } {{wrong # of entries in index i2} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2}} + do_catchsql_test pragma-3.5.2 { + PRAGMA integrity_check='4' + } {1 {no such table: 4}} + do_catchsql_test pragma-3.6 { + PRAGMA integrity_check=xyz + } {1 {no such table: xyz}} + do_catchsql_test pragma-3.6b { + PRAGMA integrity_check=t2 + } {0 {{wrong # of entries in index i2} {row 1 missing from index i2} {row 2 missing from index i2}}} + do_catchsql_test pragma-3.6c { + PRAGMA integrity_check=sqlite_schema + } {0 ok} do_test pragma-3.7 { execsql { PRAGMA integrity_check=0 } - } {{row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2}} + } {{wrong # of entries in index i2} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2} {row 1 missing from index i2} {row 2 missing from index i2}} # Add additional corruption by appending unused pages to the end of # the database file testerr.db @@ -398,93 +430,99 @@ ifcapable attach { do_test pragma-3.8.2 { execsql {PRAGMA QUICK_CHECK} } {ok} - do_test pragma-3.9 { + do_test pragma-3.9a { execsql { ATTACH 'testerr.db' AS t2; PRAGMA integrity_check } } {{*** in database t2 *** -Page 4 is never used -Page 5 is never used -Page 6 is never used} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2}} +Page 4: never used +Page 5: never used +Page 6: never used} {wrong # of entries in index i2} {row 1 missing from index i2} {row 2 missing from index i2}} + do_execsql_test pragma-3.9b { + PRAGMA t2.integrity_check=t2; + } {{wrong # of entries in index i2} {row 1 missing from index i2} {row 2 missing from index i2}} + do_execsql_test pragma-3.9c { + PRAGMA t2.integrity_check=sqlite_schema; + } {ok} do_test pragma-3.10 { execsql { PRAGMA integrity_check=1 } } {{*** in database t2 *** -Page 4 is never used}} +Page 4: never used}} do_test pragma-3.11 { execsql { PRAGMA integrity_check=5 } } {{*** in database t2 *** -Page 4 is never used -Page 5 is never used -Page 6 is never used} {row 1 missing from index i2} {row 2 missing from index i2}} +Page 4: never used +Page 5: never used +Page 6: never used} {wrong # of entries in index i2} {row 1 missing from index i2}} do_test pragma-3.12 { execsql { PRAGMA integrity_check=4 } } {{*** in database t2 *** -Page 4 is never used -Page 5 is never used -Page 6 is never used} {row 1 missing from index i2}} +Page 4: never used +Page 5: never used +Page 6: never used} {wrong # of entries in index i2}} do_test pragma-3.13 { execsql { PRAGMA integrity_check=3 } } {{*** in database t2 *** -Page 4 is never used -Page 5 is never used -Page 6 is never used}} +Page 4: never used +Page 5: never used +Page 6: never used}} do_test pragma-3.14 { execsql { PRAGMA integrity_check(2) } } {{*** in database t2 *** -Page 4 is never used -Page 5 is never used}} +Page 4: never used +Page 5: never used}} do_test pragma-3.15 { execsql { ATTACH 'testerr.db' AS t3; PRAGMA integrity_check } } {{*** in database t2 *** -Page 4 is never used -Page 5 is never used -Page 6 is never used} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2} {*** in database t3 *** -Page 4 is never used -Page 5 is never used -Page 6 is never used} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2}} +Page 4: never used +Page 5: never used +Page 6: never used} {wrong # of entries in index i2} {row 1 missing from index i2} {row 2 missing from index i2} {*** in database t3 *** +Page 4: never used +Page 5: never used +Page 6: never used} {wrong # of entries in index i2} {row 1 missing from index i2} {row 2 missing from index i2}} do_test pragma-3.16 { execsql { PRAGMA integrity_check(10) } } {{*** in database t2 *** -Page 4 is never used -Page 5 is never used -Page 6 is never used} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2} {*** in database t3 *** -Page 4 is never used -Page 5 is never used -Page 6 is never used} {row 1 missing from index i2}} +Page 4: never used +Page 5: never used +Page 6: never used} {wrong # of entries in index i2} {row 1 missing from index i2} {row 2 missing from index i2} {*** in database t3 *** +Page 4: never used +Page 5: never used +Page 6: never used} {wrong # of entries in index i2}} do_test pragma-3.17 { execsql { PRAGMA integrity_check=8 } } {{*** in database t2 *** -Page 4 is never used -Page 5 is never used -Page 6 is never used} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2} {*** in database t3 *** -Page 4 is never used -Page 5 is never used}} +Page 4: never used +Page 5: never used +Page 6: never used} {wrong # of entries in index i2} {row 1 missing from index i2} {row 2 missing from index i2} {*** in database t3 *** +Page 4: never used +Page 5: never used}} do_test pragma-3.18 { execsql { PRAGMA integrity_check=4 } } {{*** in database t2 *** -Page 4 is never used -Page 5 is never used -Page 6 is never used} {row 1 missing from index i2}} +Page 4: never used +Page 5: never used +Page 6: never used} {wrong # of entries in index i2}} } do_test pragma-3.19 { catch {db close} @@ -497,28 +535,46 @@ Page 6 is never used} {row 1 missing from index i2}} # Verify that PRAGMA integrity_check catches UNIQUE and NOT NULL # constraint violations. # -do_execsql_test pragma-3.20 { - CREATE TABLE t1(a,b); - CREATE INDEX t1a ON t1(a); - INSERT INTO t1 VALUES(1,1),(2,2),(3,3),(2,4),(NULL,5),(NULL,6); - PRAGMA writable_schema=ON; - UPDATE sqlite_master SET sql='CREATE UNIQUE INDEX t1a ON t1(a)' - WHERE name='t1a'; - UPDATE sqlite_master SET sql='CREATE TABLE t1(a NOT NULL,b)' - WHERE name='t1'; - PRAGMA writable_schema=OFF; - ALTER TABLE t1 RENAME TO t1x; - PRAGMA integrity_check; -} {{non-unique entry in index t1a} {NULL value in t1x.a} {non-unique entry in index t1a} {NULL value in t1x.a}} -do_execsql_test pragma-3.21 { - PRAGMA integrity_check(3); -} {{non-unique entry in index t1a} {NULL value in t1x.a} {non-unique entry in index t1a}} -do_execsql_test pragma-3.22 { - PRAGMA integrity_check(2); -} {{non-unique entry in index t1a} {NULL value in t1x.a}} -do_execsql_test pragma-3.23 { - PRAGMA integrity_check(1); -} {{non-unique entry in index t1a}} +ifcapable altertable { + sqlite3_db_config db DEFENSIVE 0 + do_execsql_test pragma-3.20 { + CREATE TABLE t1(a,b); + CREATE INDEX t1a ON t1(a); + INSERT INTO t1 VALUES(1,1),(2,2),(3,3),(2,4),(NULL,5),(NULL,6); + PRAGMA writable_schema=ON; + UPDATE sqlite_master SET sql='CREATE UNIQUE INDEX t1a ON t1(a)' + WHERE name='t1a'; + UPDATE sqlite_master SET sql='CREATE TABLE t1(a NOT NULL,b)' + WHERE name='t1'; + PRAGMA writable_schema=OFF; + ALTER TABLE t1 RENAME TO t1x; + PRAGMA integrity_check; + } {{non-unique entry in index t1a} {NULL value in t1x.a} {non-unique entry in index t1a} {NULL value in t1x.a}} + do_execsql_test pragma-3.21 { + PRAGMA integrity_check(3); + } {{non-unique entry in index t1a} {NULL value in t1x.a} {non-unique entry in index t1a}} + do_execsql_test pragma-3.22 { + PRAGMA integrity_check(2); + } {{non-unique entry in index t1a} {NULL value in t1x.a}} + do_execsql_test pragma-3.23 { + PRAGMA integrity_check(1); + } {{non-unique entry in index t1a}} + + # forum post https://sqlite.org/forum/forumpost/ee4f6fa5ab + do_execsql_test pragma-3.24 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a); + INSERT INTO t1 VALUES (1); + ALTER TABLE t1 ADD COLUMN b NOT NULL DEFAULT 0.25; + SELECT * FROM t1; + PRAGMA integrity_check(t1); + } {1 0.25 ok} + do_execsql_test pragma-3.25 { + ALTER TABLE t1 ADD COLUMN c CHECK (1); + SELECT * FROM t1; + PRAGMA integrity_check(t1); + } {1 0.25 {} ok} +} # PRAGMA integrity check (or more specifically the sqlite3BtreeCount() # interface) used to leave index cursors in an inconsistent state @@ -528,7 +584,7 @@ do_execsql_test pragma-3.23 { # that problem has been fixed. # do_test pragma-3.30 { - db close + catch { db close } delete_file test.db sqlite3 db test.db db eval { @@ -544,6 +600,63 @@ do_test pragma-3.30 { } } {} +# The values stored in indexes must be byte-for-byte identical to the +# values stored in tables. +# +reset_db +do_execsql_test pragma-3.40 { + CREATE TABLE t1( + a INTEGER PRIMARY KEY, + b TEXT COLLATE nocase, + c INT COLLATE nocase, + d TEXT + ); + INSERT INTO t1(a,b,c,d) VALUES + (1, 'one','one','one'), + (2, 'two','two','two'), + (3, 'three','three','three'), + (4, 'four','four','four'), + (5, 'five','five','five'); + CREATE INDEX t1bcd ON t1(b,c,d); + CREATE TABLE t2( + a INTEGER PRIMARY KEY, + b TEXT COLLATE nocase, + c INT COLLATE nocase, + d TEXT + ); + INSERT INTO t2(a,b,c,d) VALUES + (1, 'one','one','one'), + (2, 'two','two','TWO'), + (3, 'three','THREE','three'), + (4, 'FOUR','four','four'), + (5, 'FIVE','FIVE','five'); + CREATE INDEX t2bcd ON t2(b,c,d); + CREATE TEMP TABLE saved_schema AS SELECT name, rootpage FROM sqlite_schema; + PRAGMA writable_schema=ON; + UPDATE sqlite_schema + SET rootpage=(SELECT rootpage FROM saved_schema WHERE name='t2bcd') + WHERE name='t1bcd'; + UPDATE sqlite_schema + SET rootpage=(SELECT rootpage FROM saved_schema WHERE name='t1bcd') + WHERE name='t2bcd'; + PRAGMA Writable_schema=RESET; +} +ifcapable vtab { + do_execsql_test pragma-3.41 { + SELECT integrity_check AS x FROM pragma_integrity_check ORDER BY 1; + } { + {row 2 missing from index t1bcd} + {row 2 missing from index t2bcd} + {row 3 values differ from index t1bcd} + {row 3 values differ from index t2bcd} + {row 4 values differ from index t1bcd} + {row 4 values differ from index t2bcd} + {row 5 values differ from index t1bcd} + {row 5 values differ from index t2bcd} + } +} +db eval {DROP TABLE t2} + # Test modifying the cache_size of an attached database. ifcapable pager_pragmas&&attach { do_test pragma-4.1 { @@ -628,10 +741,10 @@ ifcapable tempdb&&attach { } do_test pragma-6.2 { execsql { - CREATE TABLE t2(a,b,c); + CREATE TABLE t2(a TYPE_X, b [TYPE_Y], c "TYPE_Z"); pragma table_info(t2) } -} {0 a {} 0 {} 0 1 b {} 0 {} 0 2 c {} 0 {} 0} +} {0 a TYPE_X 0 {} 0 1 b TYPE_Y 0 {} 0 2 c TYPE_Z 0 {} 0} do_test pragma-6.2.1 { execsql { pragma table_info; @@ -779,7 +892,7 @@ do_test pragma-6.7 { ORDER BY cid} } [concat \ {0 one INT 1 -1 0} \ - {1 two text 0 {} 0} \ + {1 two TEXT 0 {} 0} \ {2 three {VARCHAR(45, 65)} 0 'abcde' 0} \ {3 four REAL 0 X'abcdef' 0} \ {4 five {} 0 CURRENT_TIME 0} \ @@ -866,15 +979,15 @@ do_test pragma-8.1.2 { PRAGMA schema_version; } } {schema_version 105} -do_test pragma-8.1.3 { - execsql { - PRAGMA schema_version = 106; - } -} {} -do_test pragma-8.1.4 { - execsql { - PRAGMA schema_version; - } +sqlite3_db_config db DEFENSIVE 1 +do_execsql_test pragma-8.1.3 { + PRAGMA schema_version = 106; + PRAGMA schema_version; +} 105 +sqlite3_db_config db DEFENSIVE 0 +do_execsql_test pragma-8.1.4 { + PRAGMA schema_version = 106; + PRAGMA schema_version; } 106 # Check that creating a table modifies the schema-version (this is really @@ -1083,7 +1196,20 @@ do_test pragma-8.2.15 { # "memory" or "disk" as appropriate. # proc check_temp_store {} { - db eval {CREATE TEMP TABLE IF NOT EXISTS a(b)} + db eval { + PRAGMA temp.cache_size = 1; + CREATE TEMP TABLE IF NOT EXISTS a(b); + DELETE FROM a; + INSERT INTO a VALUES(randomblob(1000)); + INSERT INTO a SELECT * FROM a; + INSERT INTO a SELECT * FROM a; + INSERT INTO a SELECT * FROM a; + INSERT INTO a SELECT * FROM a; + INSERT INTO a SELECT * FROM a; + INSERT INTO a SELECT * FROM a; + INSERT INTO a SELECT * FROM a; + INSERT INTO a SELECT * FROM a; + } db eval {PRAGMA database_list} { if {$name=="temp"} { set bt [btree_from_db db 1] @@ -1683,7 +1809,7 @@ testvfs tvfs sqlite3 db test.db -vfs tvfs do_test pragma-19.1 { catchsql {PRAGMA error} -} {1 {SQL logic error or missing database}} +} {1 {SQL logic error}} do_test pragma-19.2 { catchsql {PRAGMA error='This is the error message'} } {1 {This is the error message}} @@ -1697,7 +1823,7 @@ do_test pragma-19.5 { file tail [lindex [execsql {PRAGMA filename}] 0] } {test.db} -if {$tcl_platform(platform)=="windows"} { +if {$tcl_platform(platform) eq "windows"} { # Test data_store_directory pragma # db close @@ -1741,73 +1867,75 @@ forcedelete data_dir } ;# endif windows database_may_be_corrupt +if {![nonzero_reserved_bytes]} { -do_test 21.1 { - # Create a corrupt database in testerr.db. And a non-corrupt at test.db. - # - db close - forcedelete test.db - sqlite3 db test.db - execsql { - PRAGMA page_size = 1024; - PRAGMA auto_vacuum = 0; - CREATE TABLE t1(a PRIMARY KEY, b); - INSERT INTO t1 VALUES(1, 1); - } - for {set i 0} {$i < 10} {incr i} { - execsql { INSERT INTO t1 SELECT a + (1 << $i), b + (1 << $i) FROM t1 } - } - db close - forcecopy test.db testerr.db - hexio_write testerr.db 15000 [string repeat 55 100] -} {100} - -set mainerr {*** in database main *** + do_test 21.1 { + # Create a corrupt database in testerr.db. And a non-corrupt at test.db. + # + db close + forcedelete test.db + sqlite3 db test.db + execsql { + PRAGMA page_size = 1024; + PRAGMA auto_vacuum = 0; + CREATE TABLE t1(a PRIMARY KEY, b); + INSERT INTO t1 VALUES(1, 1); + } + for {set i 0} {$i < 10} {incr i} { + execsql { INSERT INTO t1 SELECT a + (1 << $i), b + (1 << $i) FROM t1 } + } + db close + forcecopy test.db testerr.db + hexio_write testerr.db 15000 [string repeat 55 100] + } {100} + + set mainerr {*** in database main *** Multiple uses for byte 672 of page 15} -set auxerr {*** in database aux *** + set auxerr {*** in database aux *** Multiple uses for byte 672 of page 15} - -set mainerr {/{\*\*\* in database main \*\*\* + + set mainerr {/{\*\*\* in database main \*\*\* Multiple uses for byte 672 of page 15}.*/} -set auxerr {/{\*\*\* in database aux \*\*\* + set auxerr {/{\*\*\* in database aux \*\*\* Multiple uses for byte 672 of page 15}.*/} - -do_test 22.2 { - catch { db close } - sqlite3 db testerr.db - execsql { PRAGMA integrity_check } -} $mainerr - -do_test 22.3.1 { - catch { db close } - sqlite3 db test.db - execsql { - ATTACH 'testerr.db' AS 'aux'; - PRAGMA integrity_check; - } -} $auxerr -do_test 22.3.2 { - execsql { PRAGMA main.integrity_check; } -} {ok} -do_test 22.3.3 { - execsql { PRAGMA aux.integrity_check; } -} $auxerr - -do_test 22.4.1 { - catch { db close } - sqlite3 db testerr.db - execsql { - ATTACH 'test.db' AS 'aux'; - PRAGMA integrity_check; - } -} $mainerr -do_test 22.4.2 { - execsql { PRAGMA main.integrity_check; } -} $mainerr -do_test 22.4.3 { - execsql { PRAGMA aux.integrity_check; } -} {ok} - + + do_test 22.2 { + catch { db close } + sqlite3 db testerr.db + execsql { PRAGMA integrity_check } + } $mainerr + + do_test 22.3.1 { + catch { db close } + sqlite3 db test.db + execsql { + ATTACH 'testerr.db' AS 'aux'; + PRAGMA integrity_check; + } + } $auxerr + do_test 22.3.2 { + execsql { PRAGMA main.integrity_check; } + } {ok} + do_test 22.3.3 { + execsql { PRAGMA aux.integrity_check; } + } $auxerr + + do_test 22.4.1 { + catch { db close } + sqlite3 db testerr.db + execsql { + ATTACH 'test.db' AS 'aux'; + PRAGMA integrity_check; + } + } $mainerr + do_test 22.4.2 { + execsql { PRAGMA main.integrity_check; } + } $mainerr + do_test 22.4.3 { + execsql { PRAGMA aux.integrity_check; } + } {ok} +} + db close forcedelete test.db test.db-wal test.db-journal sqlite3 db test.db @@ -1818,10 +1946,11 @@ do_test 23.1 { CREATE INDEX i1 ON t1(b,c); CREATE INDEX i2 ON t1(c,d); CREATE INDEX i2x ON t1(d COLLATE nocase, c DESC); + CREATE INDEX i3 ON t1(d,b+c,c); CREATE TABLE t2(x INTEGER REFERENCES t1); } db2 eval {SELECT name FROM sqlite_master} -} {t1 i1 i2 i2x t2} +} {t1 i1 i2 i2x i3 t2} do_test 23.2a { db eval { DROP INDEX i2; @@ -1848,13 +1977,14 @@ do_test 23.2b { # means left-most. Key columns come before auxiliary columns.) # # (The second column of output from PRAGMA index_xinfo is...) -# EVIDENCE-OF: R-40889-06838 The rank of the column within the table +# EVIDENCE-OF: R-06603-49335 The rank of the column within the table # being indexed, or -1 if the index-column is the rowid of the table -# being indexed. +# being indexed and -2 if the index is on an expression. # # (The third column of output from PRAGMA index_xinfo is...) -# EVIDENCE-OF: R-22751-28901 The name of the column being indexed, or -# NULL if the index-column is the rowid of the table being indexed. +# EVIDENCE-OF: R-40641-22898 The name of the column being indexed, or +# NULL if the index-column is the rowid of the table being indexed or an +# expression. # # (The fourth column of output from PRAGMA index_xinfo is...) # EVIDENCE-OF: R-11847-09179 1 if the index-column is sorted in reverse @@ -1874,6 +2004,9 @@ do_test 23.2c { do_test 23.2d { db2 eval {PRAGMA index_xinfo(i2x)} } {0 3 d 0 nocase 1 1 2 c 1 BINARY 1 2 -1 {} 0 BINARY 0} +do_test 23.2e { + db2 eval {PRAGMA index_xinfo(i3)} +} {0 3 d 0 BINARY 1 1 -2 {} 0 BINARY 1 2 2 c 0 BINARY 1 3 -1 {} 0 BINARY 0} # EVIDENCE-OF: R-64103-17776 PRAGMA schema.index_list(table-name); This # pragma returns one row for each index associated with the given table. @@ -1895,19 +2028,22 @@ do_test 23.2d { # do_test 23.3 { db eval { + DROP INDEX IF EXISTS i3; CREATE INDEX i3 ON t1(d,b,c); } capture_pragma db2 out {PRAGMA index_list(t1)} db2 eval {SELECT seq, name, "unique", origin, '|' FROM out ORDER BY seq} } {0 i3 0 c | 1 i2 0 c | 2 i2x 0 c | 3 i1 0 c |} -do_test 23.4 { - db eval { - ALTER TABLE t1 ADD COLUMN e; - } - db2 eval { - PRAGMA table_info(t1); - } -} {/4 e {} 0 {} 0/} +ifcapable altertable { + do_test 23.4 { + db eval { + ALTER TABLE t1 ADD COLUMN e; + } + db2 eval { + PRAGMA table_info(t1); + } + } {/4 e {} 0 {} 0/} +} do_test 23.5 { db eval { DROP TABLE t2; @@ -1917,6 +2053,42 @@ do_test 23.5 { PRAGMA foreign_key_list(t2); } } {0 0 t1 y {} {NO ACTION} {NO ACTION} NONE} +db2 close +ifcapable !has_codec { + reset_db + do_execsql_test 24.0 { + PRAGMA page_size = 1024; + CREATE TABLE t1(a, b, c); + CREATE INDEX i1 ON t1(b); + INSERT INTO t1 VALUES('a', 'b', 'c'); + PRAGMA integrity_check; + } {ok} + + set r [db one {SELECT rootpage FROM sqlite_master WHERE name = 't1'}] + db close + hexio_write test.db [expr $r*1024 - 16] 000000000000000701040f0f1f616263 + + sqlite3 db test.db + do_catchsql_test 24.1 { + SELECT * FROM t1; + } {1 {database disk image is malformed}} + do_catchsql_test 24.2 { + PRAGMA integrity_check; + } {0 {{database disk image is malformed}}} +} database_never_corrupt + +# 2023-03-27. Register allocation issue in integrity_check discovered +# by new assert() statements added in [6f8b97f31a4c8552]. +# dbsqlfuzz dc9ab26037cf5ef797d28cd1ae0855ade584216d +# tag-20230327-1 +# +reset_db +do_execsql_test 25.0 { + CREATE TABLE t1(a INT, b AS (a*2) NOT NULL); + CREATE TEMP TABLE t2(a PRIMARY KEY, b, c UNIQUE) WITHOUT ROWID; + CREATE UNIQUE INDEX t2x ON t2(c,b); + PRAGMA integrity_check; +} ok finish_test diff --git a/test/pragma3.test b/test/pragma3.test index b7ea4d3fc6..c4794c743c 100644 --- a/test/pragma3.test +++ b/test/pragma3.test @@ -16,6 +16,11 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl +if {[sqlite3 -has-codec]} { + finish_test + return +} + do_execsql_test pragma3-100 { PRAGMA data_version; } {1} @@ -220,7 +225,7 @@ ifcapable shared_cache { # This will not work with the in-memory journal permutation, as opening # [db2] switches the journal mode back to "memory" # -ifcapable wal { +if {[wal_is_capable]} { if {[permutation]!="inmemory_journal"} { sqlite3 db test.db @@ -250,4 +255,33 @@ if {[permutation]!="inmemory_journal"} { } } +#------------------------------------------------------------------------- +# Check that empty write transactions do not cause the return of "PRAGMA +# data_version" to be decremented with journal_mode=PERSIST and +# locking_mode=EXCLUSIVE +# +foreach {tn sql} { + A { + } + B { + PRAGMA journal_mode = PERSIST; + PRAGMA locking_mode = EXCLUSIVE; + } +} { + reset_db + execsql $sql + + do_execsql_test pragma3-510$tn { + CREATE TABLE t1(x, y); + INSERT INTO t1 VALUES(1, 2); + PRAGMA data_version; + } {1} + + do_execsql_test pragma3-520$tn { + BEGIN EXCLUSIVE; + COMMIT; + PRAGMA data_version; + } {1} +} + finish_test diff --git a/test/pragma4.test b/test/pragma4.test new file mode 100644 index 0000000000..2ba87c0c60 --- /dev/null +++ b/test/pragma4.test @@ -0,0 +1,335 @@ +# 2017 Jan 4 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix pragma4 + +proc do_pragma_ncol_test {tn sql nCol} { + set ::stmt 0 + set ::stmt [sqlite3_prepare_v2 db $sql -1 dummy] + uplevel [list do_test $tn { sqlite3_column_count $::stmt } $nCol] + sqlite3_finalize $::stmt +} + +# If there is no RHS argument, the following PRAGMA statements operate as +# queries, returning a single row containing a single column. +# +# Or, if there is RHS argument, they return zero rows of zero columns. +# +foreach {tn sql} { + 1 "PRAGMA application_id = 10" + 2 "PRAGMA automatic_index = 1" + 3 "PRAGMA auto_vacuum = 1" + 4 "PRAGMA cache_size = -100" + 5 "PRAGMA cache_spill = 1" + 6 "PRAGMA cell_size_check = 1" + 7 "PRAGMA checkpoint_fullfsync = 1" + 8 "PRAGMA count_changes = 1" + 9 "PRAGMA default_cache_size = 100" + 10 "PRAGMA defer_foreign_keys = 1" + 11 "PRAGMA empty_result_callbacks = 1" + 12 "PRAGMA encoding = 'utf-8'" + 13 "PRAGMA foreign_keys = 1" + 14 "PRAGMA full_column_names = 1" + 15 "PRAGMA fullfsync = 1" + 16 "PRAGMA ignore_check_constraints = 1" + 18 "PRAGMA page_size = 511" + 19 "PRAGMA page_size = 512" + 20 "PRAGMA query_only = false" + 21 "PRAGMA read_uncommitted = true" + 22 "PRAGMA recursive_triggers = false" + 23 "PRAGMA reverse_unordered_selects = false" + 24 "PRAGMA schema_version = 211" + 25 "PRAGMA short_column_names = 1" + 26 "PRAGMA synchronous = full" + 29 "PRAGMA temp_store = memory" + 30 "PRAGMA user_version = 405" + 31 "PRAGMA writable_schema = 1" +} { + reset_db + + # Without RHS: + do_pragma_ncol_test 1.$tn.1 [lindex [split $sql =] 0] 1 + + # With RHS: + do_pragma_ncol_test 1.$tn.2 $sql 0 +} + +# These pragmas should never return any values. +# +foreach {tn sql} { + 1 "PRAGMA shrink_memory" + 2 "PRAGMA shrink_memory = 10" + 3 "PRAGMA case_sensitive_like = 0" + 4 "PRAGMA case_sensitive_like = 1" + 5 "PRAGMA case_sensitive_like" +} { + + do_pragma_ncol_test 1.$tn.1 $sql 0 +} + +# EXPLAIN on a PRAGMA integrity_check. +# Verify that that P4_INTARRAY argument to OP_IntegrityCk is rendered +# correctly. +# +catch {db close} +forcedelete test.db +sqlite3 db test.db +do_test pragma4-2.100 { + db eval { + PRAGMA page_size=512; + CREATE TABLE t1(x); + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<10000) + INSERT INTO t1(x) SELECT zeroblob(300) FROM c; + CREATE TABLE t2(y); + DROP TABLE t1; + } + string map {\[ x \] x \173 {} \175 {}} \ + [db eval {EXPLAIN PRAGMA integrity_check}] +} {/ IntegrityCk 1 2 8 x[0-9]+,1x /} + + +#-------------------------------------------------------------------------- +# +reset_db +forcedelete test.db2 +do_execsql_test 4.1.1 { + CREATE TABLE t1(a, b, c); + ATTACH 'test.db2' AS aux; + CREATE TABLE aux.t2(d, e, f); +} +do_execsql_test 4.1.2 { PRAGMA table_info = t1 } { + 0 a {} 0 {} 0 1 b {} 0 {} 0 2 c {} 0 {} 0 +} +do_execsql_test 4.1.3 { PRAGMA table_info = t2 } { + 0 d {} 0 {} 0 1 e {} 0 {} 0 2 f {} 0 {} 0 +} +do_test 4.1.4 { + sqlite3 db3 test.db + sqlite3 db2 test.db2 + execsql { DROP TABLE t1 } db3 + execsql { DROP TABLE t2 } db2 +} {} +if {[permutation]=="prepare"} { + do_catchsql_test 4.1.5a { + PRAGMA table_info(t1) + } {1 {database schema has changed}} +} +do_execsql_test 4.1.5 { + PRAGMA table_info(t1) +} +do_execsql_test 4.1.6 { PRAGMA table_info(t2) } + +db2 close +db3 close +reset_db +forcedelete test.db2 +do_execsql_test 4.2.1 { + CREATE TABLE t1(a, b, c); + ATTACH 'test.db2' AS aux; + CREATE TABLE aux.t2(d, e, f); +} +ifcapable vtab { + do_execsql_test 4.2.2 { SELECT * FROM pragma_table_info('t1') } { + 0 a {} 0 {} 0 1 b {} 0 {} 0 2 c {} 0 {} 0 + } + do_execsql_test 4.2.3 { SELECT * FROM pragma_table_info('t2') } { + 0 d {} 0 {} 0 1 e {} 0 {} 0 2 f {} 0 {} 0 + } +} +do_test 4.2.4 { + sqlite3 db3 test.db + sqlite3 db2 test.db2 + execsql { DROP TABLE t1 } db3 + execsql { DROP TABLE t2 } db2 +} {} +ifcapable vtab { + do_execsql_test 4.2.5 { SELECT * FROM pragma_table_info('t1') } + do_execsql_test 4.2.6 { SELECT * FROM pragma_table_info('t2') } +} + +db2 close +db3 close +reset_db +forcedelete test.db2 +do_execsql_test 4.3.1 { + CREATE TABLE t1(a, b, c); + CREATE INDEX i1 ON t1(b); + ATTACH 'test.db2' AS aux; + CREATE TABLE aux.t2(d, e, f); + CREATE INDEX aux.i2 ON t2(e); +} +ifcapable vtab { + do_execsql_test 4.3.2 { SELECT * FROM pragma_index_info('i1') } {0 1 b} + do_execsql_test 4.3.3 { SELECT * FROM pragma_index_info('i2') } {0 1 e} +} +do_test 4.3.4 { + sqlite3 db3 test.db + sqlite3 db2 test.db2 + execsql { DROP INDEX i1 } db3 + execsql { DROP INDEX i2 } db2 +} {} +if {[permutation]=="prepare"} { catchsql { SELECT * FROM sqlite_master } } +ifcapable vtab { + do_execsql_test 4.3.5 { SELECT * FROM pragma_index_info('i1') } + do_execsql_test 4.3.6 { SELECT * FROM pragma_index_info('i2') } +} + +execsql {SELECT * FROM main.sqlite_master, aux.sqlite_master} +do_execsql_test 4.4.0 { + CREATE INDEX main.i1 ON t1(b, c); + CREATE INDEX aux.i2 ON t2(e, f); +} +ifcapable vtab { + do_execsql_test 4.4.1 { SELECT * FROM pragma_index_list('t1') } {0 i1 0 c 0} + do_execsql_test 4.4.2 { SELECT * FROM pragma_index_list('t2') } {0 i2 0 c 0} +} +do_test 4.4.3 { + execsql { DROP INDEX i1 } db3 + execsql { DROP INDEX i2 } db2 +} {} +if {[permutation]=="prepare"} { + catchsql { SELECT * FROM sqlite_master, aux.sqlite_master } +} +ifcapable vtab { + do_execsql_test 4.4.5 { SELECT * FROM pragma_index_list('t1') } {} + do_execsql_test 4.4.6 { SELECT * FROM pragma_index_list('t2') } {} +} +execsql {SELECT * FROM main.sqlite_master, aux.sqlite_master} + +do_execsql_test 4.5.0 { + CREATE UNIQUE INDEX main.i1 ON t1(a); + CREATE UNIQUE INDEX aux.i2 ON t2(d); + CREATE TABLE main.c1 (a, b, c REFERENCES t1(a)); + CREATE TABLE aux.c2 (d, e, r REFERENCES t2(d)); +} +ifcapable vtab { + do_execsql_test 4.5.1 { SELECT * FROM pragma_foreign_key_list('c1') } { + 0 0 t1 c a {NO ACTION} {NO ACTION} NONE + } + do_execsql_test 4.5.2 { SELECT * FROM pragma_foreign_key_list('c2') } { + 0 0 t2 r d {NO ACTION} {NO ACTION} NONE + } +} +do_test 4.5.3 { + execsql { DROP TABLE c1 } db3 + execsql { DROP TABLE c2 } db2 +} {} +if {[permutation]=="prepare"} { + catchsql { SELECT * FROM sqlite_master, aux.sqlite_master } +} +ifcapable vtab { + do_execsql_test 4.5.4 { SELECT * FROM pragma_foreign_key_list('c1') } + do_execsql_test 4.5.5 { SELECT * FROM pragma_foreign_key_list('c2') } +} +execsql {SELECT * FROM main.sqlite_master, aux.sqlite_master} + +do_execsql_test 4.6.0 { + CREATE TABLE main.c1 (a, b, c REFERENCES t1(a)); + CREATE TABLE aux.c2 (d, e, r REFERENCES t2(d)); + INSERT INTO main.c1 VALUES(1, 2, 3); + INSERT INTO aux.c2 VALUES(4, 5, 6); +} +do_execsql_test 4.6.1 { pragma foreign_key_check('c1') } { + c1 1 t1 0 +} +do_execsql_test 4.6.2 { pragma foreign_key_check('c2') } { + c2 1 t2 0 +} +do_test 4.6.3 { + execsql { DROP TABLE c2 } db2 +} {} +do_execsql_test 4.6.4 { pragma foreign_key_check('c1') } {c1 1 t1 0} +do_catchsql_test 4.6.5 { + pragma foreign_key_check('c2') +} {1 {no such table: c2}} + +do_execsql_test 5.0 { + CREATE TABLE t4(a DEFAULT 'abc' /* comment */, b DEFAULT -1 -- comment + , c DEFAULT +4.0 /* another comment */ + ); + PRAGMA table_info = t4; +} { + 0 a {} 0 'abc' 0 1 b {} 0 -1 0 2 c {} 0 +4.0 0 +} + +# 2024-03-24 https://sqlite.org/forum/forumpost/85b6a8b6705fb77a +# +catch {db2 close} +catch {db3 close} +ifcapable vtab { + reset_db + do_execsql_test 6.0 { + DROP TABLE IF EXISTS t1; + DROP TABLE IF EXISTS t2; + CREATE TABLE t1(a INT PRIMARY KEY, b INT); + CREATE TABLE t2(c INT PRIMARY KEY, d INT REFERENCES t1); + SELECT t.name, f."table", f."from", i.name, i.pk + FROM pragma_table_list() AS t + JOIN pragma_foreign_key_list(t.name, t.schema) AS f + JOIN pragma_table_info(f."table", t.schema) AS i + WHERE i.pk; + } {t2 t1 d a 1} + + # With a corrupt VIEW in the schema, the PRAGMA table_list command + # will generate internal errors. Confirm that these internal errors + # do not appears on the log. https://sqlite.org/src/forumpost/00ee467e + test_sqlite3_log [list lappend ::log] + set ::log {} + do_execsql_test 6.1 { + CREATE VIEW v1 AS SELECT abs(a) FROM t1; + PRAGMA writable_schema=ON; + UPDATE sqlite_schema + SET sql=replace(sql,'abs(a)','nosuchfunc(a)') + WHERE name='v1'; + PRAGMA writable_schema=RESET; + } {} + do_execsql_test 6.2 { + PRAGMA table_list; + } {main v1 view 0 0 0 main t2 table 2 0 0 main t1 table 2 0 0 main sqlite_schema table 5 0 0 temp sqlite_temp_schema table 5 0 0} + do_test 6.3 { + set ::log + } {} + test_sqlite3_log +} + +# 2024-05-08 https://sqlite.org/forum/forumpost/cf29a33e94 +# +ifcapable vtab { + do_execsql_test 7.0 { + CREATE TABLE t3 ("a" TEXT, "b" TEXT); + CREATE TABLE t4 ("a" TEXT, "b" TEXT, "c" TEXT); + } + + do_execsql_test 7.1 { + CREATE TABLE pragma_t3 AS SELECT * FROM pragma_table_info('t3'); + CREATE TABLE pragma_t4 AS SELECT * FROM pragma_table_info('t4'); + } + + do_execsql_test 7.2 { + SELECT pragma_t4.name, pragma_t3.name + FROM pragma_t4 RIGHT JOIN pragma_t3 ON (pragma_t4.name=pragma_t3.name); + } {a a b b} + + do_execsql_test 7.3 { + SELECT t4.name, t3.name + FROM pragma_table_info('t4') t4 + RIGHT JOIN pragma_table_info('t3') t3 ON (t4.name=t3.name); + } {a a b b} +} + + + + +finish_test diff --git a/test/pragma5.test b/test/pragma5.test new file mode 100644 index 0000000000..6d9b5bbcdc --- /dev/null +++ b/test/pragma5.test @@ -0,0 +1,70 @@ +# 2017 August 25 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. +# +# This file implements tests for the PRAGMA command. Specifically, +# those pragmas that are not disabled at build time by setting: +# +# -DSQLITE_OMIT_INTROSPECTION_PRAGMAS +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix pragma5 + +if { [catch {db one "SELECT count(*) FROM pragma_function_list"}] } { + finish_test + return +} + +db function external external + +do_execsql_test 1.0 { + PRAGMA table_info(pragma_function_list) +} { + 0 name {} 0 {} 0 + 1 builtin {} 0 {} 0 + 2 type {} 0 {} 0 + 3 enc {} 0 {} 0 + 4 narg {} 0 {} 0 + 5 flags {} 0 {} 0 +} +do_execsql_test 1.1 { + SELECT DISTINCT name, builtin + FROM pragma_function_list WHERE name='upper' AND builtin +} {upper 1} +do_execsql_test 1.2 { + SELECT DISTINCT name, builtin + FROM pragma_function_list WHERE name LIKE 'exter%'; +} {external 0} + +ifcapable fts5 { + do_execsql_test 2.0 { + PRAGMA table_info(pragma_module_list) + } { + 0 name {} 0 {} 0 + } + do_execsql_test 2.1 { + SELECT * FROM pragma_module_list WHERE name='fts5' + } {fts5} +} + +do_execsql_test 3.0 { + PRAGMA table_info(pragma_pragma_list) +} { + 0 name {} 0 {} 0 +} +do_execsql_test 3.1 { + SELECT * FROM pragma_pragma_list WHERE name='pragma_list' +} {pragma_list} + + +finish_test diff --git a/test/pragma6.test b/test/pragma6.test new file mode 100644 index 0000000000..fc5566af10 --- /dev/null +++ b/test/pragma6.test @@ -0,0 +1,74 @@ +# 2024 February 27 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements tests for PRAGMAs quick_check and integrity_check. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix pragma6 + +database_may_be_corrupt + +#------------------------------------------------------------------------- +# +do_test 1.0 { + sqlite3 db {} + db deserialize [decode_hexdb { + .open --hexdb + | size 12288 pagesize 4096 filename crash-540f4c1eb1e7ac.db + | page 1 offset 0 + | 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. + | 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 03 .....@ ........ + | 32: 00 bb 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ + | 96: 00 00 00 00 0d 00 00 00 02 0f 7f 00 0f c3 0f 7f ................ + | 3952: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 42 ...............B + | 3968: 02 06 17 11 11 01 71 74 61 62 6c 65 74 32 74 32 ......qtablet2t2 + | 3984: 03 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 32 .CREATE TABLE t2 + | 4000: 28 61 20 49 4e 54 2c 20 62 20 41 53 20 28 61 2a (a INT, b AS (a* + | 4016: 32 29 20 53 54 4f 52 45 44 20 4e 4f 54 20 4e 55 2) STORED NOT NU + | 4032: 4c 4c 29 3b 01 06 17 11 11 01 63 74 61 62 6c 65 LL);......ctable + | 4048: 74 31 74 31 02 43 52 45 41 54 45 20 54 41 42 4c t1t1.CREATE TABL + | 4064: 45 20 74 31 28 61 20 49 4e 54 2c 20 62 20 41 53 E t1(a INT, b AS + | 4080: 20 28 61 2a 32 29 20 4e 4f 54 20 4e 55 4c 4c 29 (a*2) NOT NULL) + | page 2 offset 4096 + | 0: 0d 00 00 00 05 0f e7 00 00 00 00 00 00 00 00 00 ................ + | 4064: 00 00 00 00 00 00 00 00 03 05 02 01 05 03 04 02 ................ + | 4080: 01 04 03 03 02 01 03 03 02 02 01 02 02 01 02 09 ................ + | page 3 offset 8192 + | 0: 0d 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ + | 4048: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 05 05 ................ + | 4064: 03 01 01 05 0a 05 04 03 01 01 04 08 05 03 03 01 ................ + | 4080: 01 03 06 05 02 03 00 00 00 00 00 00 00 00 00 00 ................ + | end crash-540f4c1eb1e7ac.db + }] +} {} + +do_test 1.1 { + execsql { + CREATE TEMP TABLE t2( + a t1 PRIMARY KEY default 27, + b default(current_timestamp), + d TEXT UNIQUE DEFAULT 'ch`arlie', + c TEXT UNIQUE DEFAULT 084, + UNIQUE(c,b,b,a,b) + ) WITHOUT ROWID; + } + catchsql { INSERT INTO t1(a) VALUES(zeroblob(40000)) } + set {} {} +} {} + +do_test 1.2 { + execsql { PRAGMA integrity_check; } + execsql { PRAGMA quick_check; } + set {} {} +} {} + +finish_test diff --git a/test/pragmafault.test b/test/pragmafault.test new file mode 100644 index 0000000000..34718aeca1 --- /dev/null +++ b/test/pragmafault.test @@ -0,0 +1,39 @@ +# 2010 June 15 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/lock_common.tcl +source $testdir/malloc_common.tcl +set testprefix pragmafault + +db close +sqlite3 db test.db +sqlite3_db_config_lookaside db 0 0 0 +do_execsql_test 1.0 { + CREATE TABLE t1(a, b, CHECK(a!=b)); + INSERT INTO t1 VALUES(1, 2); + INSERT INTO t1 VALUES(3, 4); +} +faultsim_save_and_close + +do_faultsim_test 1 -prep { + faultsim_restore_and_reopen +} -body { + catchsql { PRAGMA integrity_check } + set {} 0 +} -test { + faultsim_test_result {0 0} +} + + +finish_test diff --git a/test/prefixes.test b/test/prefixes.test new file mode 100644 index 0000000000..2080905805 --- /dev/null +++ b/test/prefixes.test @@ -0,0 +1,88 @@ +# 2018-01-15 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is prefixes.c extension +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix unionvtab + +ifcapable !vtab { + finish_test + return +} + +load_static_extension db prefixes + +foreach {tn zLeft zRight expected} { + 1 abcdxxx abcyy 3 + 2 abcdxxx bcyyy 0 + 3 abcdxxx ab 2 + 4 ab abcd 2 + + 5 "xyz\u1234xz" "xyz\u1234xy" 5 + 6 "xyz\u1234" "xyz\u1234xy" 4 + 7 "xyz\u1234" "xyz\u1234" 4 + 8 "xyz\u1234xy" "xyz\u1234" 4 + 9 "xyz\u1234xy" "xyz\u1233" 3 + 10 "xyz\u1234xy" "xyz\u1235" 3 +} { + do_execsql_test 1.$tn { SELECT prefix_length($zLeft, $zRight) } $expected +} + + +do_execsql_test 2.0 { + CREATE TABLE t1(k TEXT UNIQUE, v INTEGER); + INSERT INTO t1 VALUES + ('aback', 1), + ('abaft', 2), + ('abandon', 3), + ('abandoned', 4), + ('abandoning', 5), + ('abandonment', 6), + ('abandons', 7), + ('abase', 8), + ('abased', 9), + ('abasement', 10), + ('abasements', 11), + ('abases', 12), + ('abash', 13), + ('abashed', 14), + ('abashes', 15), + ('abashing', 16), + ('abasing', 17), + ('abate', 18), + ('abated', 19), + ('abatement', 20), + ('abatements', 21); +} + +foreach {tn INPUT expected} { + 1 abatementt abatement + 2 abashet abash + 3 abandonio abandon + 4 abasemenu abase +} { + do_execsql_test 2.$tn { + WITH finder(str) AS ( + SELECT (SELECT max(k) FROM t1 WHERE k<=$INPUT) + UNION ALL + SELECT ( + SELECT max(k) FROM t1 + WHERE k<=substr($INPUT, 1, prefix_length(finder.str, $INPUT)) + ) FROM finder WHERE length(finder.str)>0 + ) + SELECT str FROM finder WHERE length(str)==prefix_length(str, $INPUT) LIMIT 1 + } $expected +} + +finish_test diff --git a/test/printf.test b/test/printf.test index 6103d8acf8..cc439e6172 100644 --- a/test/printf.test +++ b/test/printf.test @@ -16,7 +16,6 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl - do_test printf-1.1.1 { sqlite3_mprintf_int {abc: %d %x %o :xyz}\ 1 1 1 @@ -538,9 +537,11 @@ do_test printf-2.1.2.8 { do_test printf-2.1.2.9 { sqlite3_mprintf_double {abc: %d %d (%1.1g) :xyz} 1 1 1.0e-20 } {abc: 1 1 (1e-20) :xyz} -do_test printf-2.1.2.10 { - sqlite3_mprintf_double {abc: %*.*f} 2000000000 1000000000 1.0e-20 -} {abc: } +if {$SQLITE_MAX_LENGTH<=[expr 1000*1000*1000]} { + do_test printf-2.1.2.10 { + sqlite3_mprintf_double {abc: %*.*f} 2000000000 1000000000 1.0e-20 + } {} +} do_test printf-2.1.3.1 { sqlite3_mprintf_double {abc: (%*.*f) :xyz} 1 1 1.0 } {abc: (1.0) :xyz} @@ -3757,26 +3758,96 @@ do_test printf-15.3 { # Now test malloc() failure within a sqlite3_mprintf(): # -ifcapable memdebug { - foreach var {a b c d} { - set $var [string repeat $var 400] - } - set str1 "[string repeat A 360]%d%d%s" - set str2 [string repeat B 5000] - set zSuccess "[string repeat A 360]11[string repeat B 5000]" - foreach ::iRepeat {0 1} { - set nTestNum 1 - while {1} { - sqlite3_memdebug_fail $nTestNum -repeat $::iRepeat - set z [sqlite3_mprintf_str $str1 1 1 $str2] - set nFail [sqlite3_memdebug_fail -1 -benign nBenign] - do_test printf-malloc-$::iRepeat.$nTestNum { - expr {($nFail>0 && $z eq "") || ($nFail==$nBenign && $z eq $zSuccess)} - } {1} - if {$nFail == 0} break - incr nTestNum - } +foreach var {a b c d} { + set $var [string repeat $var 400] +} +set str1 "[string repeat A 360]%d%d%s" +set str2 [string repeat B 5000] +set zSuccess "[string repeat A 360]11[string repeat B 5000]" +foreach ::iRepeat {0 1} { + set nTestNum 1 + while {1} { + sqlite3_memdebug_fail $nTestNum -repeat $::iRepeat + set z [sqlite3_mprintf_str $str1 1 1 $str2] + set nFail [sqlite3_memdebug_fail -1 -benign nBenign] + do_test printf-malloc-$::iRepeat.$nTestNum { + expr {($nFail>0 && $z eq "") || ($nFail==$nBenign && $z eq $zSuccess)} + } {1} + if {$nFail == 0} break + incr nTestNum } } +# 2020-05-23 +# ticket 23439ea582241138 +# +do_execsql_test printf-16.1 { + SELECT printf('%.*g',2147483647,0.01); +} {0.01} + +# 2023-02-23 https://sqlite.org/forum/forumpost/d1387c3979c7f557 +# Loss of precision when doing floating-point to decimal +# conversions on values that have no factional part. +# +do_execsql_test printf-17.1 { + SELECT format('%!.20g', 13.0); +} 13.0 +do_execsql_test printf-17.2 { + SELECT format('%.3e', 199990000.0); +} 2.000e+08 +do_execsql_test printf-17.3 { + SELECT format('%.3f', 199990000.0); +} 199990000.000 +do_execsql_test printf-17.4 { + SELECT format('%.3g', 199990000.0); +} 2e+08 +do_execsql_test printf-17.5 { + SELECT format('%.4e', 199990000.0); +} 1.9999e+08 +do_execsql_test printf-17.6 { + SELECT format('%.4f', 199990000.0); +} 199990000.0000 +do_execsql_test printf-17.7 { + SELECT format('%.4g', 199990000.0); +} 2e+08 +do_execsql_test printf-17.8 { + SELECT format('%.5e', 199990000.0); +} 1.99990e+08 +do_execsql_test printf-17.9 { + SELECT format('%.5f', 199990000.0); +} 199990000.00000 +do_execsql_test printf-17.10 { + SELECT format('%.5g', 199990000.0); +} 1.9999e+08 +do_execsql_test printf-17.11 { + SELECT format('%.30f',1.0000000000000000076e-50); +} 0.000000000000000000000000000000 + +#------------------------------------------------------------------------- +# dbsqlfuzz ad651aad4bb2100f3a724129a555d8d773366d46 +# +db close +sqlite3 db test.db +sqlite3_db_config_lookaside db 0 0 0 +do_execsql_test printf-18.1 { + SELECT length( format('%,.249f', -5.0e-300) ); +} {252} + +# 2024-02-16 +# https://sqlite.org/forum/info/393708f4a8 +# +# The problem introduced by on 2023-07-03 by +# https://sqlite.org/src/info/32befb224b254639 +# +do_execsql_test printf-19.1 { + SELECT format('%0.0f %0.0g %0.0g', 0.9, 0.09, 1.9); +} {{1 0.09 2}} +do_execsql_test printf-19.2 { + SELECT format('%0.0f %#0.0f',0.0, 0.0); +} {{0 0.}} +do_execsql_test printf-19.3 { + SELECT format('%,.0f %,.0f',12345e+10, 12345e+11); +} {{123,450,000,000,000 1,234,500,000,000,000}} + + finish_test diff --git a/test/printf2.test b/test/printf2.test index fb031bd68f..2f6208b17f 100644 --- a/test/printf2.test +++ b/test/printf2.test @@ -12,10 +12,13 @@ # focus of this file is testing the printf() SQL function. # # -# EVIDENCE-OF: R-63057-40065 The printf(FORMAT,...) SQL function works +# EVIDENCE-OF: R-32560-14372 The format(FORMAT,...) SQL function works # like the sqlite3_mprintf() C-language function and the printf() # function from the standard C library. # +# EVIDENCE-OF: R-64900-53159 The printf() SQL function is an alias for +# the format() SQL function. +# set testdir [file dirname $argv0] source $testdir/tester.tcl @@ -24,7 +27,7 @@ source $testdir/tester.tcl # then the result is NULL. # do_execsql_test printf2-1.1 { - SELECT quote(printf()), quote(printf(NULL,1,2,3)); + SELECT quote(format()), quote(format(NULL,1,2,3)); } {NULL NULL} @@ -32,31 +35,31 @@ do_execsql_test printf2-1.2 { SELECT printf('hello'); } {hello} do_execsql_test printf2-1.3 { - SELECT printf('%d,%d,%d',55,-11,3421); + SELECT format('%d,%d,%d',55,-11,3421); } {55,-11,3421} do_execsql_test printf2-1.4 { SELECT printf('%d,%d,%d',55,'-11',3421); } {55,-11,3421} do_execsql_test printf2-1.5 { - SELECT printf('%d,%d,%d,%d',55,'-11',3421); + SELECT format('%d,%d,%d,%d',55,'-11',3421); } {55,-11,3421,0} do_execsql_test printf2-1.6 { SELECT printf('%.2f',3.141592653); } {3.14} do_execsql_test printf2-1.7 { - SELECT printf('%.*f',2,3.141592653); + SELECT format('%.*f',2,3.141592653); } {3.14} do_execsql_test printf2-1.8 { SELECT printf('%*.*f',5,2,3.141592653); } {{ 3.14}} do_execsql_test printf2-1.9 { - SELECT printf('%d',314159.2653); + SELECT format('%d',314159.2653); } {314159} do_execsql_test printf2-1.10 { SELECT printf('%lld',314159.2653); } {314159} do_execsql_test printf2-1.11 { - SELECT printf('%lld%n',314159.2653,'hi'); + SELECT format('%lld%n',314159.2653,'hi'); } {314159} do_execsql_test printf2-1.12 { SELECT printf('%n',0); @@ -65,7 +68,7 @@ do_execsql_test printf2-1.12 { # EVIDENCE-OF: R-17002-27534 The %z format is interchangeable with %s. # do_execsql_test printf2-1.12 { - SELECT printf('%.*z',5,'abcdefghijklmnop'); + SELECT format('%.*z',5,'abcdefghijklmnop'); } {abcde} do_execsql_test printf2-1.13 { SELECT printf('%c','abcdefghijklmnop'); @@ -116,7 +119,95 @@ do_execsql_test printf2-3.5 { SELECT printf('|%7.8c|%-7.8c|','*','*'); } {|********|********|} +# The "," separator +do_execsql_test printf2-4.1 { + SELECT printf('|%,d|%,d|',0,-1); +} {|0|-1|} +do_execsql_test printf2-4.2 { + SELECT printf('|%,d|%,d|',12,-12); +} {|12|-12|} +do_execsql_test printf2-4.3 { + SELECT printf('|%,d|%,d|',123,-123); +} {|123|-123|} +do_execsql_test printf2-4.4 { + SELECT printf('|%,d|%,d|',1234,-1234); +} {|1,234|-1,234|} +do_execsql_test printf2-4.5 { + SELECT printf('|%,d|%,d|',12345,-12345); +} {|12,345|-12,345|} +do_execsql_test printf2-4.6 { + SELECT printf('|%,d|%,d|',123456,-123456); +} {|123,456|-123,456|} +do_execsql_test printf2-4.7 { + SELECT printf('|%,d|%,d|',1234567,-1234567); +} {|1,234,567|-1,234,567|} +do_execsql_test printf2-4.8 { + SELECT printf('|%,d|%,d|',12345678,-12345678); +} {|12,345,678|-12,345,678|} +do_execsql_test printf2-4.9 { + SELECT printf('|%,d|%,d|',123456789,-123456789); +} {|123,456,789|-123,456,789|} +do_execsql_test printf2-4.10 { + SELECT printf('|%,d|%,d|',1234567890,-1234567890); +} {|1,234,567,890|-1,234,567,890|} + +# 2018-02-19. Unicode characters with %c +do_execsql_test printf2-5.100 { + SELECT printf('(%8c)',char(11106)); +} {{( ⭢)}} +do_execsql_test printf2-5.101 { + SELECT printf('(%-8c)',char(11106)); +} {{(⭢ )}} +do_execsql_test printf2-5.102 { + SELECT printf('(%5.3c)',char(1492)); +} {{( ההה)}} +do_execsql_test printf2-5.103 { + SELECT printf('(%-5.3c)',char(1492)); +} {{(ההה )}} +do_execsql_test printf2-5.104 { + SELECT printf('(%3.3c)',char(1492)); +} {{(ההה)}} +do_execsql_test printf2-5.105 { + SELECT printf('(%-3.3c)',char(1492)); +} {{(ההה)}} +do_execsql_test printf2-5.104 { + SELECT printf('(%2c)',char(1513)); +} {{( ש)}} +do_execsql_test printf2-5.106 { + SELECT printf('(%-2c)',char(1513)); +} {{(ש )}} +# 2018-02-19. Unicode characters with the "!" flag in %s and friends. +do_execsql_test printf2-6.100 { + SELECT printf('(%!.3s)','הנה מה־טוב ומה־נעים שבת אחים גם־יחד'); +} {(הנה)} +do_execsql_test printf2-6.101 { + SELECT printf('(%.6s)','הנה מה־טוב ומה־נעים שבת אחים גם־יחד'); +} {(הנה)} +do_execsql_test printf2-6.102 { + SELECT printf('(%!5.3s)','הנה מה־טוב ומה־נעים שבת אחים גם־יחד'); +} {{( הנה)}} +do_execsql_test printf2-6.103 { + SELECT printf('(%8.6s)','הנה מה־טוב ומה־נעים שבת אחים גם־יחד'); +} {{( הנה)}} +do_execsql_test printf2-6.104 { + SELECT printf('(%!-5.3s)','הנה מה־טוב ומה־נעים שבת אחים גם־יחד'); +} {{(הנה )}} +do_execsql_test printf2-6.105 { + SELECT printf('(%-8.6s)','הנה מה־טוב ומה־נעים שבת אחים גם־יחד'); +} {{(הנה )}} +do_execsql_test printf2-6.106 { + SELECT printf('(%!.3Q)','הנה מה־טוב ומה־נעים שבת אחים גם־יחד'); +} {('הנה')} +do_execsql_test printf2-6.107 { + SELECT printf('(%.6Q)','הנה מה־טוב ומה־נעים שבת אחים גם־יחד'); +} {('הנה')} +do_execsql_test printf2-6.108 { + SELECT printf('(%!7.3Q)','הנה מה־טוב ומה־נעים שבת אחים גם־יחד'); +} {{( 'הנה')}} +do_execsql_test printf2-6.109 { + SELECT printf('(%10.6Q)','הנה מה־טוב ומה־נעים שבת אחים גם־יחד'); +} {{( 'הנה')}} finish_test diff --git a/test/pushdown.test b/test/pushdown.test new file mode 100644 index 0000000000..cb9042d258 --- /dev/null +++ b/test/pushdown.test @@ -0,0 +1,359 @@ +# 2017-04-29 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# Test cases for the push-down optimizations. +# +# +# There are two different meanings for "push-down optimization". +# +# (1) "MySQL push-down" means that WHERE clause terms that can be +# evaluated using only the index and without reference to the +# table are run first, so that if they are false, unnecessary table +# seeks are avoided. See https://sqlite.org/src/info/d7bb79ed3a40419d +# from 2017-04-29. +# +# (2) "WHERE-clause pushdown" means to push WHERE clause terms in +# outer queries down into subqueries. See +# https://sqlite.org/src/info/6df18e949d367629 from 2015-06-02. +# +# This module started out as tests for MySQL push-down only. But because +# of naming ambiguity, it has picked up test cases for WHERE-clause push-down +# over the years. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix pushdown + +do_execsql_test 1.0 { + CREATE TABLE t1(a, b, c); + INSERT INTO t1 VALUES(1, 'b1', 'c1'); + INSERT INTO t1 VALUES(2, 'b2', 'c2'); + INSERT INTO t1 VALUES(3, 'b3', 'c3'); + INSERT INTO t1 VALUES(4, 'b4', 'c4'); + CREATE INDEX i1 ON t1(a, c); +} + +proc f {val} { + lappend ::L $val + return 0 +} +db func f f + +do_test 1.1 { + set L [list] + execsql { SELECT * FROM t1 WHERE a=2 AND f(b) AND f(c) } + set L +} {c2} + +do_test 1.2 { + set L [list] + execsql { SELECT * FROM t1 WHERE a=3 AND f(c) AND f(b) } + set L +} {c3} + +do_execsql_test 1.3 { + DROP INDEX i1; + CREATE INDEX i1 ON t1(a, b); +} +do_test 1.4 { + set L [list] + execsql { SELECT * FROM t1 WHERE a=2 AND f(b) AND f(c) } + set L +} {b2} + +do_test 1.5 { + set L [list] + execsql { SELECT * FROM t1 WHERE a=3 AND f(c) AND f(b) } + set L +} {b3} + +#----------------------------------------------- + +do_execsql_test 2.0 { + CREATE TABLE u1(a, b, c); + CREATE TABLE u2(x, y, z); + + INSERT INTO u1 VALUES('a1', 'b1', 'c1'); + INSERT INTO u2 VALUES('a1', 'b1', 'c1'); +} + +do_test 2.1 { + set L [list] + execsql { + SELECT * FROM u1 WHERE f('one')=123 AND 123=( + SELECT x FROM u2 WHERE x=a AND f('two') + ) + } + set L +} {one} + +do_test 2.2 { + set L [list] + execsql { + SELECT * FROM u1 WHERE 123=( + SELECT x FROM u2 WHERE x=a AND f('two') + ) AND f('three')=123 + } + set L +} {three} + +# 2022-11-25 dbsqlfuzz crash-3a548de406a50e896c1bf7142692d35d339d697f +# Disable the WHERE-clause push-down optimization for compound subqueries +# if any arm of the compound has an incompatible affinity. +# +reset_db +do_execsql_test 3.1 { + CREATE TABLE t0(c0 INT); + INSERT INTO t0 VALUES(0); + CREATE TABLE t1_a(a INTEGER PRIMARY KEY, b TEXT); + INSERT INTO t1_a VALUES(1,'one'); + CREATE TABLE t1_b(c INTEGER PRIMARY KEY, d TEXT); + INSERT INTO t1_b VALUES(2,'two'); + CREATE VIEW v0 AS SELECT CAST(t0.c0 AS INTEGER) AS c0 FROM t0; + CREATE VIEW v1(a,b) AS SELECT a, b FROM t1_a UNION ALL SELECT c, 0 FROM t1_b; + SELECT v1.a, quote(v1.b), t0.c0 AS cd FROM t0 LEFT JOIN v0 ON v0.c0!=0,v1; +} { + 1 'one' 0 + 2 0 0 +} +do_execsql_test 3.2 { + SELECT a, quote(b), cd FROM ( + SELECT v1.a, v1.b, t0.c0 AS cd FROM t0 LEFT JOIN v0 ON v0.c0!=0, v1 + ) WHERE a=2 AND b='0' AND cd=0; +} {} +do_execsql_test 3.3 { + SELECT a, quote(b), cd FROM ( + SELECT v1.a, v1.b, t0.c0 AS cd FROM t0 LEFT JOIN v0 ON v0.c0!=0, v1 + ) WHERE a=1 AND b='one' AND cd=0; +} {1 'one' 0} +do_execsql_test 3.4 { + SELECT a, quote(b), cd FROM ( + SELECT v1.a, v1.b, t0.c0 AS cd FROM t0 LEFT JOIN v0 ON v0.c0!=0, v1 + ) WHERE a=2 AND b=0 AND cd=0; +} { + 2 0 0 +} + +# 2023-02-22 https://sqlite.org/forum/forumpost/bcc4375032 +# Performance regression caused by check-in [1ad41840c5e0fa70] from 2022-11-25. +# That check-in added a new restriction on push-down. The new restriction is +# no longer necessary after check-in [27655c9353620aa5] from 2022-12-14. +# +do_execsql_test 3.5 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a INT, b INT, c TEXT, PRIMARY KEY(a,b)) WITHOUT ROWID; + INSERT INTO t1(a,b,c) VALUES + (1,100,'abc'), + (2,200,'def'), + (3,300,'abc'); + DROP TABLE IF EXISTS t2; + CREATE TABLE t2(a INT, b INT, c TEXT, PRIMARY KEY(a,b)) WITHOUT ROWID; + INSERT INTO t2(a,b,c) VALUES + (1,110,'efg'), + (2,200,'hij'), + (3,330,'klm'); + CREATE VIEW v3 AS + SELECT a, b, c FROM t1 + UNION ALL + SELECT a, b, 'xyz' FROM t2; + SELECT * FROM v3 WHERE a=2 AND b=200; +} {2 200 def 2 200 xyz} +do_eqp_test 3.6 { + SELECT * FROM v3 WHERE a=2 AND b=200; +} { + QUERY PLAN + |--CO-ROUTINE v3 + | `--COMPOUND QUERY + | |--LEFT-MOST SUBQUERY + | | `--SEARCH t1 USING PRIMARY KEY (a=? AND b=?) + | `--UNION ALL + | `--SEARCH t2 USING PRIMARY KEY (a=? AND b=?) + `--SCAN v3 +} +# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +# We want both arms of the compound subquery to use the +# primary key. + +# The following is a test of the count-of-view optimization. This does +# not have anything to do with push-down. It is here because this is a +# convenient place to put the test. +# +do_execsql_test 3.7 { + SELECT count(*) FROM v3; +} 6 +do_eqp_test 3.8 { + SELECT count(*) FROM v3; +} { + QUERY PLAN + |--SCAN CONSTANT ROW + |--SCALAR SUBQUERY xxxxxx + | `--SCAN t1 + `--SCALAR SUBQUERY xxxxxx + `--SCAN t2 +} +# ^^^^^^^^^^^^^^^^^^^^ +# The query should be converted into: +# SELECT (SELECT count(*) FROM t1)+(SELECT count(*) FROM t2) + +# 2023-05-09 https://sqlite.org/forum/forumpost/a7d4be7fb6 +# Restriction (9) on the WHERE-clause push-down optimization. +# +reset_db +db null - +do_execsql_test 4.1 { + CREATE TABLE t1(a INT); + CREATE TABLE t2(b INT); + CREATE TABLE t3(c INT); + INSERT INTO t3(c) VALUES(3); + CREATE TABLE t4(d INT); + CREATE TABLE t5(e INT); + INSERT INTO t5(e) VALUES(5); + CREATE VIEW v6(f,g) AS SELECT d, e FROM t4 RIGHT JOIN t5 ON true; + SELECT * FROM t1 JOIN t2 ON false RIGHT JOIN t3 ON true CROSS JOIN v6; +} {- - 3 - 5} +do_execsql_test 4.2 { + SELECT * FROM v6 JOIN t5 ON false RIGHT JOIN t3 ON true; +} {- - - 3} +do_execsql_test 4.3 { + SELECT * FROM t1 JOIN t2 ON false JOIN v6 ON true RIGHT JOIN t3 ON true; +} {- - - - 3} + +# 2023-05-15 https://sqlite.org/forum/forumpost/f3f546025a +# This is restriction (6) on sqlite3ExprIsSingleTableConstraint(). +# That restriction (now) used to implement restriction (9) on push-down. +# It is used for other things too, so it is not purely a push-down +# restriction. But it seems convenient to put it here. +# +reset_db +db null - +do_execsql_test 5.0 { + CREATE TABLE t1(a INT); INSERT INTO t1 VALUES(1); + CREATE TABLE t2(b INT); INSERT INTO t2 VALUES(2); + CREATE TABLE t3(c INT); INSERT INTO t3 VALUES(3); + CREATE TABLE t4(d INT); INSERT INTO t4 VALUES(4); + CREATE TABLE t5(e INT); INSERT INTO t5 VALUES(5); + SELECT * + FROM t1 JOIN t2 ON null RIGHT JOIN t3 ON true + LEFT JOIN (t4 JOIN t5 ON d+1=e) ON d=4 + WHERE e>0; +} {- - 3 4 5} + + +# 2024-04-05 +# Allow push-down of operators of the form "expr IN table". +# +reset_db +do_execsql_test 6.0 { + CREATE TABLE t01(w,x,y,z); + CREATE TABLE t02(w,x,y,z); + CREATE VIEW t0(w,x,y,z) AS + SELECT w,x,y,z FROM t01 UNION ALL SELECT w,x,y,z FROM t02; + CREATE INDEX t01x ON t01(w,x,y); + CREATE INDEX t02x ON t02(w,x,y); + CREATE VIEW v1(k) AS VALUES(77),(88),(99); + CREATE TABLE k1(k); + INSERT INTO k1 SELECT * FROM v1; +} +do_eqp_test 6.1 { + WITH k(n) AS (VALUES(77),(88),(99)) + SELECT max(z) FROM t0 WHERE w=123 AND x IN k AND y BETWEEN 44 AND 55; +} { + QUERY PLAN + |--CO-ROUTINE t0 + | `--COMPOUND QUERY + | |--LEFT-MOST SUBQUERY + | | |--SEARCH t01 USING INDEX t01x (w=? AND x=? AND y>? AND y<?) + | | `--LIST SUBQUERY xxxxxx + | | |--MATERIALIZE k + | | | `--SCAN 3 CONSTANT ROWS + | | |--SCAN k + | | `--CREATE BLOOM FILTER + | `--UNION ALL + | |--SEARCH t02 USING INDEX t02x (w=? AND x=? AND y>? AND y<?) + | `--REUSE LIST SUBQUERY xxxxxx + |--SEARCH t0 + `--REUSE LIST SUBQUERY xxxxxx +} +# ^^^^--- The key feature above is that the SEARCH for each subquery +# uses all three fields of the index w, x, and y. Prior to the push-down +# of "expr IN table", only the w term of the index would be used. Similar +# for the following tests: +# +do_eqp_test 6.2 { + SELECT max(z) FROM t0 WHERE w=123 AND x IN v1 AND y BETWEEN 44 AND 55; +} { + QUERY PLAN + |--CO-ROUTINE t0 + | `--COMPOUND QUERY + | |--LEFT-MOST SUBQUERY + | | |--SEARCH t01 USING INDEX t01x (w=? AND x=? AND y>? AND y<?) + | | `--LIST SUBQUERY xxxxxx + | | |--CO-ROUTINE v1 + | | | `--SCAN 3 CONSTANT ROWS + | | |--SCAN v1 + | | `--CREATE BLOOM FILTER + | `--UNION ALL + | |--SEARCH t02 USING INDEX t02x (w=? AND x=? AND y>? AND y<?) + | `--REUSE LIST SUBQUERY xxxxxx + |--SEARCH t0 + `--REUSE LIST SUBQUERY xxxxxx +} +do_eqp_test 6.3 { + SELECT max(z) FROM t0 WHERE w=123 AND x IN k1 AND y BETWEEN 44 AND 55; +} { + QUERY PLAN + |--CO-ROUTINE t0 + | `--COMPOUND QUERY + | |--LEFT-MOST SUBQUERY + | | |--SEARCH t01 USING INDEX t01x (w=? AND x=? AND y>? AND y<?) + | | `--LIST SUBQUERY xxxxxx + | | |--SCAN k1 + | | `--CREATE BLOOM FILTER + | `--UNION ALL + | |--SEARCH t02 USING INDEX t02x (w=? AND x=? AND y>? AND y<?) + | `--REUSE LIST SUBQUERY xxxxxx + |--SEARCH t0 + `--REUSE LIST SUBQUERY xxxxxx +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 7.0 { + CREATE TABLE t0_1(a INT , b INT, c INT); + CREATE TABLE t0_2(a INT , b INT, c INT); + + INSERT INTO t0_1 (a, b, c) VALUES (1, 0, 1); + INSERT INTO t0_2 (a, b, c) VALUES (1, 0, 1); + + CREATE TABLE empty1(x); + CREATE TABLE empty2(y); +} + +do_execsql_test 7.1 { + SELECT t0_2.c + FROM (SELECT '0000' AS c0 FROM empty2 RIGHT JOIN t0_1 ON 1) AS v0 + LEFT JOIN empty1 ON v0.c0, t0_2 + RIGHT JOIN ( + SELECT 5678 AS col0 FROM (SELECT 0) + ) AS sub1 ON 1; +} {1} + +do_execsql_test 7.2 { + SELECT t0_2.c + FROM (SELECT '0000' AS c0 FROM empty2 RIGHT JOIN t0_1 ON 1) AS v0 + LEFT JOIN empty1 ON v0.c0, t0_2 + RIGHT JOIN ( + SELECT 5678 AS col0 FROM (SELECT 0) + ) AS sub1 ON 1 WHERE +t0_2.c; +} {1} + +finish_test diff --git a/test/quickcheck.test b/test/quickcheck.test new file mode 100644 index 0000000000..18c42a13d0 --- /dev/null +++ b/test/quickcheck.test @@ -0,0 +1,33 @@ +# 2023 January 28 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix quickcheck + +do_execsql_test 1.0 { + CREATE TABLE t1( + a INTEGER NOT NULL, b INTEGER NOT NULL, c AS (a+1), + PRIMARY KEY(b, a) + ) WITHOUT ROWID; + + INSERT INTO t1 VALUES(1, 2); + INSERT INTO t1 VALUES(3, 4); +} + +do_execsql_test 1.1 { + PRAGMA quick_check +} { + ok +} + +finish_test diff --git a/test/quota.test b/test/quota.test index 7af55a8330..f49600043e 100644 --- a/test/quota.test +++ b/test/quota.test @@ -216,7 +216,7 @@ do_test quota-3.2.9 { set ::quota [list] proc quota_callback {file limitvar size} { upvar $limitvar limit - if {$::tcl_platform(platform)=="windows"} { + if {$::tcl_platform(platform) eq "windows"} { set file [ lindex [string map {\\ \/} $file] 0 ] } lappend ::quota $file $size @@ -351,7 +351,7 @@ do_test quota-4.3.1 { } {} unset -nocomplain quotagroup -if {$tcl_platform(platform)=="windows"} { +if {$tcl_platform(platform) eq "windows"} { set quotagroup *\\quota-test-A?.db } else { set quotagroup */quota-test-A?.db @@ -402,7 +402,7 @@ do_test quota-4.4.7 { } [expr {[file size quota-test-A1.db]+[file size quota-test-A2.db]}] unset -nocomplain quotagroup -if {$tcl_platform(platform)=="windows"} { +if {$tcl_platform(platform) eq "windows"} { set quotagroup *\\quota-test-B* } else { set quotagroup */quota-test-B* @@ -527,4 +527,6 @@ do_faultsim_test quota-5.6 -prep { } catch { sqlite3_quota_shutdown } +catch { db close } +forcedelete test.db finish_test diff --git a/test/quote.test b/test/quote.test index f13d6f9ee9..4e40a9e57f 100644 --- a/test/quote.test +++ b/test/quote.test @@ -16,6 +16,7 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl +set testprefix quote # Create a table with a strange name and with strange column names. # @@ -84,6 +85,102 @@ do_test quote-1.6 { } msg ] lappend r $msg } {0 {}} - + +#------------------------------------------------------------------------- +# Check that it is not possible to use double-quotes for a string +# constant in a CHECK constraint or CREATE INDEX statement. However, +# SQLite can load such a schema from disk. +# +reset_db +sqlite3_db_config db SQLITE_DBCONFIG_DQS_DDL 0 +sqlite3_db_config db SQLITE_DBCONFIG_DQS_DML 1 +do_execsql_test 2.0 { + CREATE TABLE t1(x, y, z); +} +foreach {tn sql errname} { + 1 { CREATE TABLE xyz(a, b, c CHECK (c!="null") ) } null + 2 { CREATE INDEX i2 ON t1(x, y, z||"abc") } abc + 3 { CREATE INDEX i3 ON t1("w") } w + 4 { CREATE INDEX i4 ON t1(x) WHERE z="w" } w +} { + do_catchsql_test 2.1.$tn $sql [list 1 "no such column: \"$errname\" - should this be a string literal in single-quotes?"] +} + +do_execsql_test 2.2 { + PRAGMA writable_schema = 1; + CREATE TABLE xyz(a, b, c CHECK (c!="null") ); + CREATE INDEX i2 ON t1(x, y, z||"abc"); + CREATE INDEX i3 ON t1("w"||""); + CREATE INDEX i4 ON t1(x) WHERE z="w"; +} + +db close +sqlite3 db test.db + +do_execsql_test 2.3.1 { + INSERT INTO xyz VALUES(1, 2, 3); +} +do_catchsql_test 2.3.2 { + INSERT INTO xyz VALUES(1, 2, 'null'); +} {1 {CHECK constraint failed: c!="null"}} + +do_execsql_test 2.4 { + INSERT INTO t1 VALUES(1, 2, 3); + INSERT INTO t1 VALUES(4, 5, 'w'); + SELECT * FROM t1 WHERE z='w'; +} {4 5 w} +do_execsql_test 2.5 { + SELECT sql FROM sqlite_master; +} { + {CREATE TABLE t1(x, y, z)} + {CREATE TABLE xyz(a, b, c CHECK (c!="null") )} + {CREATE INDEX i2 ON t1(x, y, z||"abc")} + {CREATE INDEX i3 ON t1("w"||"")} + {CREATE INDEX i4 ON t1(x) WHERE z="w"} +} + +# 2021-03-13 +# ticket 1c24a659e6d7f3a1 +ifcapable altertable { + reset_db + do_catchsql_test 3.0 { + CREATE TABLE t1(a,b); + CREATE INDEX x1 on t1("b"); + ALTER TABLE t1 DROP COLUMN b; + } {1 {error in index x1 after drop column: no such column: "b" - should this be a string literal in single-quotes?}} + do_catchsql_test 3.1 { + DROP TABLE t1; + CREATE TABLE t1(a,"b"); + CREATE INDEX x1 on t1("b"); + ALTER TABLE t1 DROP COLUMN b; + } {1 {error in index x1 after drop column: no such column: "b" - should this be a string literal in single-quotes?}} + do_catchsql_test 3.2 { + DROP TABLE t1; + CREATE TABLE t1(a,'b'); + CREATE INDEX x1 on t1("b"); + ALTER TABLE t1 DROP COLUMN b; + } {1 {error in index x1 after drop column: no such column: "b" - should this be a string literal in single-quotes?}} + do_catchsql_test 3.3 { + DROP TABLE t1; + CREATE TABLE t1(a,"b"); + CREATE INDEX x1 on t1('b'); + ALTER TABLE t1 DROP COLUMN b; + } {1 {error in index x1 after drop column: no such column: b}} + do_catchsql_test 3.4 { + DROP TABLE t1; + CREATE TABLE t1(a, b, c); + CREATE INDEX x1 ON t1("a"||"b"); + INSERT INTO t1 VALUES(1,2,3),(1,4,5); + ALTER TABLE t1 DROP COLUMN b; + } {1 {error in index x1 after drop column: no such column: "b" - should this be a string literal in single-quotes?}} + sqlite3_db_config db SQLITE_DBCONFIG_DQS_DDL 1 + do_catchsql_test 3.5 { + DROP TABLE t1; + CREATE TABLE t1(a, b, c); + CREATE INDEX x1 ON t1("a"||"x"); + INSERT INTO t1 VALUES(1,2,3),(1,4,5); + ALTER TABLE t1 DROP COLUMN b; + } {0 {}} +} finish_test diff --git a/test/readonly.test b/test/readonly.test new file mode 100644 index 0000000000..00392266eb --- /dev/null +++ b/test/readonly.test @@ -0,0 +1,58 @@ +# 2024 March 21 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# This file contains tests for using databases in read-only mode on +# unix. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +if {$tcl_platform(platform) eq "windows"} { + finish_test + return +} +source $testdir/lock_common.tcl +source $testdir/wal_common.tcl +set ::testprefix readonly + +do_execsql_test 1.0 { + CREATE TABLE t1(a, b); + INSERT INTO t1 VALUES(1, 2), (3, 4), (5, 6); +} + +db close +file attributes test.db -permissions r--r--r-- + +sqlite3 db test.db + +do_catchsql_test 1.1 { + INSERT INTO t1 VALUES(7, 8); +} {1 {attempt to write a readonly database}} + +do_execsql_test 1.2 { + BEGIN; + SELECT * FROM t1; +} {1 2 3 4 5 6} + +# The following attempts to open a read/write fd on the database 20,000 +# times. And each time instead opens a read-only fd. At one point this +# was failing to reuse cached fds, causing a "too many open file-descriptors" +# error. +do_test 1.3 { + for {set ii 0} {$ii < 20000} {incr ii} { + sqlite3 db2 test.db + db2 close + } + set {} {} +} {} + + +finish_test diff --git a/test/recover.test b/test/recover.test new file mode 100644 index 0000000000..ad6b7298dc --- /dev/null +++ b/test/recover.test @@ -0,0 +1,182 @@ +# 2019 April 23 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# TESTRUNNER: shell +# +# Test the shell tool ".ar" command. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix recover + +ifcapable !vtab { + finish_test; return +} +set CLI [test_find_cli] + +proc compare_result {db1 db2 sql} { + set r1 [$db1 eval $sql] + set r2 [$db2 eval $sql] + if {$r1 != $r2} { + puts "r1: $r1" + puts "r2: $r2" + error "mismatch for $sql" + } + return "" +} + +proc compare_dbs {db1 db2} { + compare_result $db1 $db2 "SELECT sql FROM sqlite_master ORDER BY 1" + foreach tbl [$db1 eval {SELECT name FROM sqlite_master WHERE type='table'}] { + compare_result $db1 $db2 "SELECT * FROM $tbl" + } +} + +proc recover_with_opts {opts} { + set cmd ".recover $opts" + set fd [open [list |$::CLI test.db $cmd]] + fconfigure $fd -translation binary + set sql [read $fd] + close $fd + + # Remove the ".dbconfig defensive off" line + set sql [string map {".dbconfig defensive off" ""} $sql] + + forcedelete test.db2 + sqlite3 db2 test.db2 + execsql $sql db2 + db2 close +} + +proc do_recover_test {tn {tsql {}} {res {}}} { + recover_with_opts "" + + sqlite3 db2 test.db2 + if {$tsql==""} { + uplevel [list do_test $tn [list compare_dbs db db2] {}] + } else { + uplevel [list do_execsql_test -db db2 $tn $tsql $res] + } + db2 close +} + +set doc { + hello + world +} +do_execsql_test 1.1.1 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c); + INSERT INTO t1 VALUES(1, 4, X'1234567800'); + INSERT INTO t1 VALUES(2, 'test', 8.1); + INSERT INTO t1 VALUES(3, $doc, 8.4); +} +do_recover_test 1.1.2 + +do_execsql_test 1.2.1 " + DELETE FROM t1; + INSERT INTO t1 VALUES(13, 'hello\r\nworld', 13); +" +do_recover_test 1.2.2 + +do_execsql_test 1.3.1 " + CREATE TABLE t2(i INTEGER PRIMARY KEY AUTOINCREMENT, b, c); + INSERT INTO t2 VALUES(NULL, 1, 2); + INSERT INTO t2 VALUES(NULL, 3, 4); + INSERT INTO t2 VALUES(NULL, 5, 6); + CREATE TABLE t3(i INTEGER PRIMARY KEY AUTOINCREMENT, b, c); + INSERT INTO t3 VALUES(NULL, 1, 2); + INSERT INTO t3 VALUES(NULL, 3, 4); + INSERT INTO t3 VALUES(NULL, 5, 6); + DELETE FROM t2; +" +do_recover_test 1.3.2 + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 2.1.0 { + PRAGMA auto_vacuum = 0; + CREATE TABLE t1(a, b, c, PRIMARY KEY(b, c)) WITHOUT ROWID; + INSERT INTO t1 VALUES(1, 2, 3); + INSERT INTO t1 VALUES(4, 5, 6); + INSERT INTO t1 VALUES(7, 8, 9); +} + +do_recover_test 2.1.1 + +do_execsql_test 2.2.0 { + PRAGMA writable_schema = 1; + DELETE FROM sqlite_master WHERE name='t1'; +} +do_recover_test 2.2.1 { + SELECT name FROM sqlite_master +} {lost_and_found} + +do_execsql_test 2.3.0 { + CREATE TABLE lost_and_found(a, b, c); +} +do_recover_test 2.3.1 { + SELECT name FROM sqlite_master +} {lost_and_found lost_and_found_0} + +do_execsql_test 2.4.0 { + CREATE TABLE lost_and_found_0(a, b, c); +} +do_recover_test 2.4.1 { + SELECT name FROM sqlite_master; + SELECT * FROM lost_and_found_1; +} {lost_and_found lost_and_found_0 lost_and_found_1 + 2 2 3 {} 2 3 1 + 2 2 3 {} 5 6 4 + 2 2 3 {} 8 9 7 +} + +#------------------------------------------------------------------------- +reset_db +do_recover_test 3.0 + +#------------------------------------------------------------------------- +reset_db +execsql { PRAGMA secure_delete = 0 } +execsql { PRAGMA auto_vacuum = 0 } +do_execsql_test 4.0 { + CREATE TABLE t1(a, b, c); + CREATE TABLE t2(d, e, f); + CREATE TABLE t3(g, h, i); + + INSERT INTO t2 VALUES(1, 2, 3); + INSERT INTO t2 VALUES('a', 'b', 'c'); + + INSERT INTO t3 VALUES('one', 'two', 'three'); + DROP TABLE t1; + DROP TABLE t2; +} + +recover_with_opts "" +sqlite3 db2 test.db2 +do_execsql_test -db db2 4.1.1 { + SELECT name FROM sqlite_schema +} {t3 lost_and_found} +do_execsql_test -db db2 4.1.2 { + SELECT id, c0, c1, c2 FROM lost_and_found +} {1 1 2 3 2 a b c} +db2 close + +recover_with_opts -ignore-freelist +sqlite3 db2 test.db2 +do_execsql_test -db db2 4.2.1 { + SELECT name FROM sqlite_schema +} {t3} +do_execsql_test -db db2 4.2.2 { + SELECT * FROM t3 +} {one two three} +db2 close + +finish_test diff --git a/test/regexp1.test b/test/regexp1.test index 0e63cd98c8..0401b13d72 100644 --- a/test/regexp1.test +++ b/test/regexp1.test @@ -28,18 +28,49 @@ do_test regexp1-1.1 { } } {1 3} +do_execsql_test regexp1-1.1.2 { + SELECT regexpi('abc','ABC'); +} {1} +do_execsql_test regexp1-1.1.3 { + SELECT regexpi('ABC','ABC'); +} {1} +do_execsql_test regexp1-1.1.4 { + SELECT regexpi('ABC','abc'); +} {1} +do_execsql_test regexp1-1.1.5 { + SELECT regexpi('ABC.','ABC'); +} {0} + do_execsql_test regexp1-1.2 { SELECT x FROM t1 WHERE y REGEXP 'by|in' ORDER BY x; } {1 2 3 4} -do_execsql_test regexp1-1.3 { +do_execsql_test regexp1-1.3.1 { SELECT x FROM t1 WHERE y REGEXP 'by|Christ' ORDER BY x; } {1 2 4} +do_execsql_test regexp1-1.3.2 { + SELECT x FROM t1 WHERE regexp('by|christ',y) ORDER BY x; +} {1 2} +do_execsql_test regexp1-1.3.3 { + SELECT x FROM t1 WHERE regexpi('by|christ',y) ORDER BY x; +} {1 2 4} +do_execsql_test regexp1-1.3.4 { + SELECT x FROM t1 WHERE regexpi('BY|CHRIST',y) ORDER BY x; +} {1 2 4} do_execsql_test regexp1-1.4 { SELECT x FROM t1 WHERE y REGEXP 'shal+ al+' ORDER BY x; } {4} -do_execsql_test regexp1-1.5 { +do_execsql_test regexp1-1.5.1 { SELECT x FROM t1 WHERE y REGEXP 'shall x*y*z*all' ORDER BY x; } {4} +do_execsql_test regexp1-1.5.2 { + SELECT x FROM t1 WHERE regexp('shall x*y*z*all',y) ORDER BY x; +} {4} +do_execsql_test regexp1-1.5.3 { + SELECT x FROM t1 WHERE regexp('SHALL x*y*z*all',y) ORDER BY x; +} {} +do_execsql_test regexp1-1.5.4 { + SELECT x FROM t1 WHERE regexpi('SHALL x*y*z*all',y) ORDER BY x; +} {4} do_execsql_test regexp1-1.6 { SELECT x FROM t1 WHERE y REGEXP 'shallx?y? ?z?all' ORDER BY x; } {4} @@ -208,4 +239,97 @@ do_execsql_test regexp1-2.22 { SELECT 'abc$¢€xyz' REGEXP '^abc[^\u0025-X][^ -\u007f][^\u20ab]xyz$' } {1} +# 2022-07-03 +# https://sqlite.org/forum/forumpost/96692f8ba5 +# The REGEXP extension mishandles the prefix search optimization when +# the prefix contains 3-byte UTF8 characters. +# +reset_db +load_static_extension db regexp +do_execsql_test regexp1-3.1 { + CREATE TABLE t1(id INTEGER PRIMARY KEY, a TEXT); + INSERT INTO t1(id, a) VALUES(1, '日本語'); + SELECT a, hex(a), length(a) FROM t1; +} {日本語 E697A5E69CACE8AA9E 3} +do_execsql_test regexp1-3.2 { + SELECT * FROM t1 WHERE a='日本語'; +} {1 日本語} +do_execsql_test regexp1-3.3 { + SELECT * FROM t1 WHERE a LIKE '日本語'; +} {1 日本語} +do_execsql_test regexp1-3.4 { + SELECT * FROM t1 wHERE a REGEXP '日本語'; +} {1 日本語} + +# 2022-07-03 +# https://sqlite.org/forum/forumpost/96692f8ba5 Issue #2 +# The '$' token in REGEXP contained within other elements. +# +do_execsql_test regexp1-4.1 {SELECT 'xab' REGEXP 'a(b$|cd)';} {1} +do_execsql_test regexp1-4.1b {SELECT 'xab' REGEXP '(b$|cd)';} {1} +do_execsql_test regexp1-4.2 {SELECT 'xaby' REGEXP 'a(b$|cd)';} {0} +do_execsql_test regexp1-4.3 {SELECT 'xacd' REGEXP 'a(b$|cd)';} {1} +do_execsql_test regexp1-4.4 {SELECT 'xacdy' REGEXP 'a(b$|cd)';} {1} +do_execsql_test regexp1-4.5 {SELECT 'xab' REGEXP 'a(cd|b$)';} {1} +do_execsql_test regexp1-4.6 {SELECT 'xaby' REGEXP 'a(cd|b$)';} {0} +do_execsql_test regexp1-4.7 {SELECT 'xacd' REGEXP 'a(cd|b$)';} {1} +do_execsql_test regexp1-4.8 {SELECT 'xacdy' REGEXP 'a(cd|b$)';} {1} +do_execsql_test regexp1-4.9 {SELECT 'xab' REGEXP 'a(cd|b$|e)';} {1} +do_execsql_test regexp1-4.10 {SELECT 'xaby' REGEXP 'a(cd|b$|e)';} {0} +do_execsql_test regexp1-4.11 {SELECT 'xacd' REGEXP 'a(cd|b$|e)';} {1} +do_execsql_test regexp1-4.12 {SELECT 'xacdy' REGEXP 'a(cd|b$|e)';} {1} + +# 2022-07-18 +# https://sqlite.org/forum/forumpost/57cbaf1d0e +# Incorrect bytecode for {M,N} when M is zero. +# +do_execsql_test regexp1-5.1 {SELECT 'fooX' REGEXP '^[a-z][a-z0-9]{0,30}$';} {0} +do_execsql_test regexp1-5.2 {SELECT 'fooX' REGEXP '^[a-z][a-z0-9]{0,30}X$';} {1} +do_execsql_test regexp1-5.3 {SELECT 'fooX' REGEXP '^[a-z][a-z0-9]{0,2}X$';} {1} +do_execsql_test regexp1-5.4 {SELECT 'foooX' REGEXP '^[a-z][a-z0-9]{0,2}X$';} {0} +do_execsql_test regexp1-5.5 {SELECT 'foooX' REGEXP '^[a-z][a-z0-9]{0,3}X$';} {1} + +# 2022-07-18 +# https://sqlite.org/forum/forumpost/18f87fdcdf +# Allow "^" to occur inside of "(..)" +# +do_execsql_test regexp1-6.1 {SELECT 'foo' REGEXP '[a-z]';} {1} +do_execsql_test regexp1-6.2 {SELECT 'foo' REGEXP '^[a-z]+$';} {1} +do_execsql_test regexp1-6.3 {SELECT 'foo' REGEXP '^([a-z]+)$';} {1} +do_execsql_test regexp1-6.4 {SELECT 'foo' REGEXP '(^[a-z]+)$';} {1} +do_execsql_test regexp1-6.5 {SELECT 'foo' REGEXP '(^[a-z]+$)';} {1} +do_execsql_test regexp1-6.6 {SELECT 'abc' REGEXP '(^abc|def)';} {1} +do_execsql_test regexp1-6.7 {SELECT 'xabc' REGEXP '(^abc|def)';} {0} +do_execsql_test regexp1-6.8 {SELECT 'def' REGEXP '(^abc|def)';} {1} +do_execsql_test regexp1-6.9 {SELECT 'xdef' REGEXP '(^abc|def)';} {1} + +# 2022-11-17 +# https://sqlite.org/forum/forumpost/3ffe058b04 +# +do_execsql_test regexp1-7.1 { + SELECT char(0x61,0x7ff,0x62) REGEXP char(0x7ff); +} 1 +do_execsql_test regexp1-7.2 { + SELECT char(0x61,0x800,0x62) REGEXP char(0x800); +} 1 +do_execsql_test regexp1-7.3 { + SELECT char(0x61,0xabc,0x62) REGEXP char(0xabc); +} 1 +do_execsql_test regexp1-7.4 { + SELECT char(0x61,0xfff,0x62) REGEXP char(0xfff); +} 1 +do_execsql_test regexp1-7.5 { + SELECT char(0x61,0x1000,0x62) REGEXP char(0x1000); +} 1 +do_execsql_test regexp1-7.10 { + SELECT char(0x61,0xffff,0x62) REGEXP char(0xffff); +} 1 +do_execsql_test regexp1-7.11 { + SELECT char(0x61,0x10000,0x62) REGEXP char(0x10000); +} 1 +do_execsql_test regexp1-7.12 { + SELECT char(0x61,0x10ffff,0x62) REGEXP char(0x10ffff); +} 1 + + finish_test diff --git a/test/regexp2.test b/test/regexp2.test new file mode 100644 index 0000000000..55ce59d05a --- /dev/null +++ b/test/regexp2.test @@ -0,0 +1,159 @@ +# 2016 February 19 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# This file implements tests for the REGEXP operator in ext/misc/regexp.c. +# It focuses on the use of the sqlite3_set_auxdata()/get_auxdata() APIs. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix regexp2 + +load_static_extension db regexp + +#------------------------------------------------------------------------- +# Test that triggers do not become confused and use aux-data created by +# a different trigger for a different REGEXP invocation. +# +do_execsql_test 1.0 { + CREATE TABLE t1(a, b, c); + CREATE TABLE x1(x, y, z); + CREATE TABLE x2(x, y, z); + + CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN + INSERT INTO x1 VALUES( + new.a REGEXP 'abc', + new.b REGEXP 'abc', + new.c REGEXP 'abc' + ); + END; + + CREATE TRIGGER tr2 AFTER INSERT ON t1 BEGIN + INSERT INTO x2 VALUES( + new.a REGEXP 'def', + new.b REGEXP 'def', + new.c REGEXP 'def' + ); + END; + + INSERT INTO t1 VALUES('abc', 'def', 'abc'); + SELECT * FROM t1; +} {abc def abc} + +do_execsql_test 1.1 { SELECT * FROM x1 } {1 0 1} +do_execsql_test 1.2 { SELECT * FROM x2 } {0 1 0} + +#------------------------------------------------------------------------- +# Test that if an exception is thrown several triggers deep, all aux-data +# objects are cleaned up correctly. +# +proc sql_error {} { + error "SQL error!" +} +db func error sql_error +do_execsql_test 2.0 { + CREATE TABLE t2(a, b); + CREATE TABLE t3(c, d); + CREATE TABLE t4(e, f); + + CREATE TRIGGER t2_tr1 AFTER UPDATE ON t2 BEGIN + UPDATE t3 SET d = new.b WHERE c = old.a; + END; + + CREATE TRIGGER t3_tr1 AFTER UPDATE ON t3 BEGIN + UPDATE t4 SET f = new.d WHERE e = old.c AND new.d REGEXP 'a.*'; + END; + + CREATE TRIGGER t4_tr1 AFTER UPDATE ON t4 BEGIN + SELECT CASE WHEN new.f REGEXP '.*y.*' THEN error() ELSE 1 END; + END; + + INSERT INTO t2 VALUES(1, 'a_x_1'); + INSERT INTO t2 VALUES(2, 'a_y_1'); + + INSERT INTO t3 VALUES(1, 'b1'); + INSERT INTO t3 VALUES(2, 'b2'); + + INSERT INTO t4 VALUES(1, 'b1'); + INSERT INTO t4 VALUES(2, 'b2'); +} {} + +do_catchsql_test 2.1 { + UPDATE t2 SET a=a+1 WHERE b REGEXP 'a.*' AND b REGEXP '.*1'; +} {1 {SQL error!}} + +# Test that the triggers used in the test above work as expected. +# +do_execsql_test 2.2 { + UPDATE t2 SET b = 'a_abc_1'; +} {} +do_execsql_test 2.3 { + SELECT * FROM t2; + SELECT * FROM t3; + SELECT * FROM t4; +} {1 a_abc_1 2 a_abc_1 1 a_abc_1 2 a_abc_1 1 a_abc_1 2 a_abc_1} + +#------------------------------------------------------------------------- +# Test that trigger parameters (i.e. new.* and old.*) refs are not +# considered to be constant across separate invocations of the trigger. +# +do_execsql_test 3.0 { + CREATE TABLE t5(a); + CREATE TABLE t6(x); + + CREATE TRIGGER t5tr AFTER DELETE ON t5 BEGIN + DELETE FROM t6 WHERE t6.x REGEXP old.a; + END; + + INSERT INTO t5 VALUES ('^a.*'), ('^b.*'), ('^c.*'); + INSERT INTO t6 VALUES ('eab'), ('abc'), ('bcd'), ('cde'), ('dea'); + + DELETE FROM t5; + SELECT * FROM t6; +} {eab dea} + +# 2021-06-04 Forum https://sqlite.org/forum/forumpost/9104f0d9e7 +# +do_execsql_test 4.1 {SELECT 'abc' REGEXP '\W'} {0} +do_execsql_test 4.2 {SELECT 'a c' REGEXP '\W'} {1} +do_execsql_test 4.3 {SELECT ' ' REGEXP '\W'} {1} +do_execsql_test 4.4 {SELECT 'abc' REGEXP '\w'} {1} +do_execsql_test 4.5 {SELECT 'a c' REGEXP '\w'} {1} +do_execsql_test 4.6 {SELECT ' ' REGEXP '\w'} {0} +do_execsql_test 4.7 {SELECT 'abc' REGEXP '\D'} {1} +do_execsql_test 4.8 {SELECT 'abc' REGEXP '[^a-z]'} {0} +do_execsql_test 4.9 {SELECT 'a c' REGEXP '[^a-z]'} {1} +do_execsql_test 4.10 {SELECT ' ' REGEXP '[^a-z]'} {1} +do_execsql_test 4.11 {SELECT 'abc' REGEXP '[a-z]'} {1} +do_execsql_test 4.12 {SELECT 'a c' REGEXP '[a-z]'} {1} +do_execsql_test 4.13 {SELECT ' ' REGEXP '[a-z]'} {0} +do_execsql_test 4.14 {SELECT 'abc' REGEXP '[^a-z]{2}'} {0} +do_execsql_test 4.15 {SELECT 'a c' REGEXP '[^a-z]{2}'} {0} +do_execsql_test 4.16 {SELECT ' ' REGEXP '[^a-z]{2}'} {1} +do_execsql_test 4.17 {SELECT 'abc' REGEXP '\W{1,1}'} {0} +do_execsql_test 4.18 {SELECT 'abc' REGEXP '\W{1}'} {0} + +do_execsql_test 5.0 { + SELECT 'abc' REGEXP 'a{1,999}bc'; +} 1 +do_catchsql_test 5.1 { + SELECT 'abc' REGEXP 'a{1,25000}bc'; +} {1 {REGEXP pattern too big}} +do_execsql_test 5.2 { + SELECT 'abc' REGEXP 'a{999}bc'; +} 0 +do_catchsql_test 5.3 { + SELECT 'abc' REGEXP 'a{25000}bc'; +} {1 {REGEXP pattern too big}} + + + +finish_test diff --git a/test/reindex.test b/test/reindex.test index 9d5ecd9ee2..88e489b0b1 100644 --- a/test/reindex.test +++ b/test/reindex.test @@ -15,6 +15,7 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl +set testprefix reindex # There is nothing to test if REINDEX is disable for this build. # @@ -168,4 +169,37 @@ do_test reindex-3.99 { db2 close } {} +#------------------------------------------------------------------------- +foreach {tn wo} {1 "" 2 "WITHOUT ROWID"} { + reset_db + eval [string map [list %without_rowid% $wo] { + do_execsql_test 4.$tn.0 { + CREATE TABLE t0 ( + c0 INTEGER PRIMARY KEY DESC, + c1 UNIQUE DEFAULT NULL + ) %without_rowid% ; + INSERT INTO t0(c0) VALUES (1), (2), (3), (4), (5); + SELECT c0 FROM t0 WHERE c1 IS NULL ORDER BY 1; + } {1 2 3 4 5} + + do_execsql_test 4.$tn.1 { + REINDEX; + } + + do_execsql_test 4.$tn.2 { + SELECT c0 FROM t0 WHERE c1 IS NULL ORDER BY 1; + } {1 2 3 4 5} + + do_execsql_test 4.$tn.3 { + SELECT c0 FROM t0 WHERE c1 IS NULL AND c0 IN (1,2,3,4,5); + } {1 2 3 4 5} + + do_execsql_test 4.$tn.4 { + PRAGMA integrity_check; + } {ok} + }] +} + + + finish_test diff --git a/test/releasetest.tcl b/test/releasetest.tcl deleted file mode 100644 index bb902eec3e..0000000000 --- a/test/releasetest.tcl +++ /dev/null @@ -1,960 +0,0 @@ -#!/usr/bin/tclsh -# -# Documentation for this script. This may be output to stderr -# if the script is invoked incorrectly. See the [process_options] -# proc below. -# -set ::USAGE_MESSAGE { -This Tcl script is used to test the various configurations required -before releasing a new version. Supported command line options (all -optional) are: - - --srcdir TOP-OF-SQLITE-TREE (see below) - --platform PLATFORM (see below) - --config CONFIGNAME (Run only CONFIGNAME) - --quick (Run "veryquick.test" only) - --veryquick (Run "make smoketest" only) - --msvc (Use MSVC as the compiler) - --buildonly (Just build testfixture - do not run) - --dryrun (Print what would have happened) - --info (Show diagnostic info) - --with-tcl=DIR (Use TCL build at DIR) - --jobs N (Use N processes - default 1) - --progress (Show progress messages) - -The default value for --srcdir is the parent of the directory holding -this script. - -The script determines the default value for --platform using the -$tcl_platform(os) and $tcl_platform(machine) variables. Supported -platforms are "Linux-x86", "Linux-x86_64", "Darwin-i386", -"Darwin-x86_64", "Windows NT-intel", and "Windows NT-amd64". - -Every test begins with a fresh run of the configure script at the top -of the SQLite source tree. -} - -# Return a timestamp of the form HH:MM:SS -# -proc now {} { - return [clock format [clock seconds] -format %H:%M:%S] -} - -# Omit comments (text between # and \n) in a long multi-line string. -# -proc strip_comments {in} { - regsub -all {#[^\n]*\n} $in {} out - return $out -} - -array set ::Configs [strip_comments { - "Default" { - -O2 - --disable-amalgamation --disable-shared - } - "Sanitize" { - CC=clang -fsanitize=undefined - -DSQLITE_ENABLE_STAT4 - } - "Have-Not" { - # The "Have-Not" configuration sets all possible -UHAVE_feature options - # in order to verify that the code works even on platforms that lack - # these support services. - -DHAVE_FDATASYNC=0 - -DHAVE_GMTIME_R=0 - -DHAVE_ISNAN=0 - -DHAVE_LOCALTIME_R=0 - -DHAVE_LOCALTIME_S=0 - -DHAVE_MALLOC_USABLE_SIZE=0 - -DHAVE_STRCHRNUL=0 - -DHAVE_USLEEP=0 - -DHAVE_UTIME=0 - } - "Unlock-Notify" { - -O2 - -DSQLITE_ENABLE_UNLOCK_NOTIFY - -DSQLITE_THREADSAFE - -DSQLITE_TCL_DEFAULT_FULLMUTEX=1 - } - "Secure-Delete" { - -O2 - -DSQLITE_SECURE_DELETE=1 - -DSQLITE_SOUNDEX=1 - } - "Update-Delete-Limit" { - -O2 - -DSQLITE_DEFAULT_FILE_FORMAT=4 - -DSQLITE_ENABLE_UPDATE_DELETE_LIMIT=1 - -DSQLITE_ENABLE_STMT_SCANSTATUS - -DSQLITE_LIKE_DOESNT_MATCH_BLOBS - -DSQLITE_ENABLE_CURSOR_HINTS - --enable-json1 - } - "Check-Symbols" { - -DSQLITE_MEMDEBUG=1 - -DSQLITE_ENABLE_FTS3_PARENTHESIS=1 - -DSQLITE_ENABLE_FTS3=1 - -DSQLITE_ENABLE_RTREE=1 - -DSQLITE_ENABLE_MEMSYS5=1 - -DSQLITE_ENABLE_MEMSYS3=1 - -DSQLITE_ENABLE_COLUMN_METADATA=1 - -DSQLITE_ENABLE_UPDATE_DELETE_LIMIT=1 - -DSQLITE_SECURE_DELETE=1 - -DSQLITE_SOUNDEX=1 - -DSQLITE_ENABLE_ATOMIC_WRITE=1 - -DSQLITE_ENABLE_MEMORY_MANAGEMENT=1 - -DSQLITE_ENABLE_OVERSIZE_CELL_CHECK=1 - -DSQLITE_ENABLE_STAT4 - -DSQLITE_ENABLE_STMT_SCANSTATUS - --enable-json1 --enable-fts5 - } - "Debug-One" { - --disable-shared - -O2 - -DSQLITE_DEBUG=1 - -DSQLITE_MEMDEBUG=1 - -DSQLITE_MUTEX_NOOP=1 - -DSQLITE_TCL_DEFAULT_FULLMUTEX=1 - -DSQLITE_ENABLE_FTS3=1 - -DSQLITE_ENABLE_RTREE=1 - -DSQLITE_ENABLE_MEMSYS5=1 - -DSQLITE_ENABLE_MEMSYS3=1 - -DSQLITE_ENABLE_COLUMN_METADATA=1 - -DSQLITE_ENABLE_STAT4 - -DSQLITE_ENABLE_HIDDEN_COLUMNS - -DSQLITE_MAX_ATTACHED=125 - } - "Fast-One" { - -O6 - -DSQLITE_ENABLE_FTS4=1 - -DSQLITE_ENABLE_RTREE=1 - -DSQLITE_ENABLE_STAT4 - -DSQLITE_ENABLE_RBU - -DSQLITE_MAX_ATTACHED=125 - -DLONGDOUBLE_TYPE=double - } - "Device-One" { - -O2 - -DSQLITE_DEBUG=1 - -DSQLITE_DEFAULT_AUTOVACUUM=1 - -DSQLITE_DEFAULT_CACHE_SIZE=64 - -DSQLITE_DEFAULT_PAGE_SIZE=1024 - -DSQLITE_DEFAULT_TEMP_CACHE_SIZE=32 - -DSQLITE_DISABLE_LFS=1 - -DSQLITE_ENABLE_ATOMIC_WRITE=1 - -DSQLITE_ENABLE_IOTRACE=1 - -DSQLITE_ENABLE_MEMORY_MANAGEMENT=1 - -DSQLITE_MAX_PAGE_SIZE=4096 - -DSQLITE_OMIT_LOAD_EXTENSION=1 - -DSQLITE_OMIT_PROGRESS_CALLBACK=1 - -DSQLITE_OMIT_VIRTUALTABLE=1 - -DSQLITE_ENABLE_HIDDEN_COLUMNS - -DSQLITE_TEMP_STORE=3 - --enable-json1 - } - "Device-Two" { - -DSQLITE_4_BYTE_ALIGNED_MALLOC=1 - -DSQLITE_DEFAULT_AUTOVACUUM=1 - -DSQLITE_DEFAULT_CACHE_SIZE=1000 - -DSQLITE_DEFAULT_LOCKING_MODE=0 - -DSQLITE_DEFAULT_PAGE_SIZE=1024 - -DSQLITE_DEFAULT_TEMP_CACHE_SIZE=1000 - -DSQLITE_DISABLE_LFS=1 - -DSQLITE_ENABLE_FTS3=1 - -DSQLITE_ENABLE_MEMORY_MANAGEMENT=1 - -DSQLITE_ENABLE_RTREE=1 - -DSQLITE_MAX_COMPOUND_SELECT=50 - -DSQLITE_MAX_PAGE_SIZE=32768 - -DSQLITE_OMIT_TRACE=1 - -DSQLITE_TEMP_STORE=3 - -DSQLITE_THREADSAFE=2 - --enable-json1 --enable-fts5 - } - "Locking-Style" { - -O2 - -DSQLITE_ENABLE_LOCKING_STYLE=1 - } - "OS-X" { - -O1 # Avoid a compiler bug in gcc 4.2.1 build 5658 - -DSQLITE_OMIT_LOAD_EXTENSION=1 - -DSQLITE_DEFAULT_MEMSTATUS=0 - -DSQLITE_THREADSAFE=2 - -DSQLITE_OS_UNIX=1 - -DSQLITE_ENABLE_JSON1=1 - -DSQLITE_ENABLE_LOCKING_STYLE=1 - -DUSE_PREAD=1 - -DSQLITE_ENABLE_RTREE=1 - -DSQLITE_ENABLE_FTS3=1 - -DSQLITE_ENABLE_FTS3_PARENTHESIS=1 - -DSQLITE_DEFAULT_CACHE_SIZE=1000 - -DSQLITE_MAX_LENGTH=2147483645 - -DSQLITE_MAX_VARIABLE_NUMBER=500000 - -DSQLITE_DEBUG=1 - -DSQLITE_PREFER_PROXY_LOCKING=1 - -DSQLITE_ENABLE_API_ARMOR=1 - --enable-json1 --enable-fts5 - } - "Extra-Robustness" { - -DSQLITE_ENABLE_OVERSIZE_CELL_CHECK=1 - -DSQLITE_MAX_ATTACHED=62 - } - "Devkit" { - -DSQLITE_DEFAULT_FILE_FORMAT=4 - -DSQLITE_MAX_ATTACHED=30 - -DSQLITE_ENABLE_COLUMN_METADATA - -DSQLITE_ENABLE_FTS4 - -DSQLITE_ENABLE_FTS4_PARENTHESIS - -DSQLITE_DISABLE_FTS4_DEFERRED - -DSQLITE_ENABLE_RTREE - --enable-json1 --enable-fts5 - } - "No-lookaside" { - -DSQLITE_TEST_REALLOC_STRESS=1 - -DSQLITE_OMIT_LOOKASIDE=1 - -DHAVE_USLEEP=1 - } - "Valgrind" { - -DSQLITE_ENABLE_STAT4 - -DSQLITE_ENABLE_FTS4 - -DSQLITE_ENABLE_RTREE - -DSQLITE_ENABLE_HIDDEN_COLUMNS - --enable-json1 - } - - # The next group of configurations are used only by the - # Failure-Detection platform. They are all the same, but we need - # different names for them all so that they results appear in separate - # subdirectories. - # - Fail0 {-O0} - Fail2 {-O0} - Fail3 {-O0} - Fail4 {-O0} - FuzzFail1 {-O0} - FuzzFail2 {-O0} -}] - -array set ::Platforms [strip_comments { - Linux-x86_64 { - "Check-Symbols" checksymbols - "Fast-One" fuzztest - "Debug-One" "mptest test" - "Have-Not" test - "Secure-Delete" test - "Unlock-Notify" "QUICKTEST_INCLUDE=notify2.test test" - "Update-Delete-Limit" test - "Extra-Robustness" test - "Device-Two" test - "No-lookaside" test - "Devkit" test - "Sanitize" {QUICKTEST_OMIT=func4.test,nan.test test} - "Device-One" fulltest - "Default" "threadtest fulltest" - "Valgrind" valgrindtest - } - Linux-i686 { - "Devkit" test - "Have-Not" test - "Unlock-Notify" "QUICKTEST_INCLUDE=notify2.test test" - "Device-One" test - "Device-Two" test - "Default" "threadtest fulltest" - } - Darwin-i386 { - "Locking-Style" "mptest test" - "Have-Not" test - "OS-X" "threadtest fulltest" - } - Darwin-x86_64 { - "Locking-Style" "mptest test" - "Have-Not" test - "OS-X" "threadtest fulltest" - } - "Windows NT-intel" { - "Have-Not" test - "Default" "mptest fulltestonly" - } - "Windows NT-amd64" { - "Have-Not" test - "Default" "mptest fulltestonly" - } - - # The Failure-Detection platform runs various tests that deliberately - # fail. This is used as a test of this script to verify that this script - # correctly identifies failures. - # - Failure-Detection { - Fail0 "TEST_FAILURE=0 test" - Sanitize "TEST_FAILURE=1 test" - Fail2 "TEST_FAILURE=2 valgrindtest" - Fail3 "TEST_FAILURE=3 valgrindtest" - Fail4 "TEST_FAILURE=4 test" - FuzzFail1 "TEST_FAILURE=5 test" - FuzzFail2 "TEST_FAILURE=5 valgrindtest" - } -}] - - -# End of configuration section. -######################################################################### -######################################################################### - -# Configuration verification: Check that each entry in the list of configs -# specified for each platforms exists. -# -foreach {key value} [array get ::Platforms] { - foreach {v t} $value { - if {0==[info exists ::Configs($v)]} { - puts stderr "No such configuration: \"$v\"" - exit -1 - } - } -} - -# Output log. Disabled for slave interpreters. -# -if {[lindex $argv end]!="--slave"} { - set LOG [open releasetest-out.txt w] - proc PUTS {txt} { - puts $txt - puts $::LOG $txt - flush $::LOG - } - proc PUTSNNL {txt} { - puts -nonewline $txt - puts -nonewline $::LOG $txt - flush $::LOG - } - proc PUTSERR {txt} { - puts stderr $txt - puts $::LOG $txt - flush $::LOG - } - puts $LOG "$argv0 $argv" - set tm0 [clock format [clock seconds] -format {%Y-%m-%d %H:%M:%S} -gmt 1] - puts $LOG "start-time: $tm0 UTC" -} else { - proc PUTS {txt} { - puts $txt - } - proc PUTSNNL {txt} { - puts -nonewline $txt - } - proc PUTSERR {txt} { - puts stderr $txt - } -} - -# Open the file $logfile and look for a report on the number of errors -# and the number of test cases run. Add these values to the global -# $::NERRCASE and $::NTESTCASE variables. -# -# If any errors occur, then write into $errmsgVar the text of an appropriate -# one-line error message to show on the output. -# -proc count_tests_and_errors {logfile rcVar errmsgVar} { - if {$::DRYRUN} return - upvar 1 $rcVar rc $errmsgVar errmsg - set fd [open $logfile rb] - set seen 0 - while {![eof $fd]} { - set line [gets $fd] - if {[regexp {(\d+) errors out of (\d+) tests} $line all nerr ntest]} { - incr ::NERRCASE $nerr - incr ::NTESTCASE $ntest - set seen 1 - if {$nerr>0} { - set rc 1 - set errmsg $line - } - } - if {[regexp {runtime error: +(.*)} $line all msg]} { - # skip over "value is outside range" errors - if {[regexp {value .* is outside the range of representable} $line]} { - # noop - } else { - incr ::NERRCASE - if {$rc==0} { - set rc 1 - set errmsg $msg - } - } - } - if {[regexp {fatal error +(.*)} $line all msg]} { - incr ::NERRCASE - if {$rc==0} { - set rc 1 - set errmsg $msg - } - } - if {[regexp {ERROR SUMMARY: (\d+) errors.*} $line all cnt] && $cnt>0} { - incr ::NERRCASE - if {$rc==0} { - set rc 1 - set errmsg $all - } - } - if {[regexp {^VERSION: 3\.\d+.\d+} $line]} { - set v [string range $line 9 end] - if {$::SQLITE_VERSION eq ""} { - set ::SQLITE_VERSION $v - } elseif {$::SQLITE_VERSION ne $v} { - set rc 1 - set errmsg "version conflict: {$::SQLITE_VERSION} vs. {$v}" - } - } - } - close $fd - if {$::BUILDONLY} { - incr ::NTESTCASE - if {$rc!=0} { - set errmsg "Build failed" - } - } elseif {!$seen} { - set rc 1 - set errmsg "Test did not complete" - if {[file readable core]} { - append errmsg " - core file exists" - } - } -} - -#-------------------------------------------------------------------------- -# This command is invoked as the [main] routine for scripts run with the -# "--slave" option. -# -# For each test (i.e. "configure && make test" execution), the master -# process spawns a process with the --slave option. It writes two lines -# to the slaves stdin. The first contains a single boolean value - the -# value of ::TRACE to use in the slave script. The second line contains a -# list in the same format as each element of the list passed to the -# [run_all_test_suites] command in the master process. -# -# The slave then runs the "configure && make test" commands specified. It -# exits successfully if the tests passes, or with a non-zero error code -# otherwise. -# -proc run_slave_test {} { - # Read global vars configuration from stdin. - set V [gets stdin] - foreach {::TRACE ::MSVC ::DRYRUN} $V {} - - # Read the test-suite configuration from stdin. - set T [gets stdin] - foreach {title dir configOpts testtarget makeOpts cflags opts} $T {} - - # Create and switch to the test directory. - set ::env(SQLITE_TMPDIR) [file normalize $dir] - trace_cmd file mkdir $dir - trace_cmd cd $dir - catch {file delete core} - catch {file delete test.log} - - # Run the "./configure && make" commands. - set rc 0 - set rc [catch [configureCommand $configOpts]] - if {!$rc} { - if {[info exists ::env(TCLSH_CMD)]} { - set savedEnv(TCLSH_CMD) $::env(TCLSH_CMD) - } else { - unset -nocomplain savedEnv(TCLSH_CMD) - } - set ::env(TCLSH_CMD) [file nativename [info nameofexecutable]] - set rc [catch [makeCommand $testtarget $makeOpts $cflags $opts]] - if {[info exists savedEnv(TCLSH_CMD)]} { - set ::env(TCLSH_CMD) $savedEnv(TCLSH_CMD) - } else { - unset -nocomplain ::env(TCLSH_CMD) - } - } - - # Exis successfully if the test passed, or with a non-zero error code - # otherwise. - exit $rc -} - -# This command is invoked in the master process each time a slave -# file-descriptor is readable. -# -proc slave_fileevent {fd T tm1} { - global G - foreach {title dir configOpts testtarget makeOpts cflags opts} $T {} - - if {[eof $fd]} { - fconfigure $fd -blocking 1 - set rc [catch { close $fd }] - - set errmsg {} - set logfile [file join $dir test.log] - if {[file exists $logfile]} { - count_tests_and_errors [file join $dir test.log] rc errmsg - } elseif {$rc==0 && !$::DRYRUN} { - set rc 1 - set errmsg "no test.log file..." - } - - if {!$::TRACE} { - set tm2 [clock seconds] - set hours [expr {($tm2-$tm1)/3600}] - set minutes [expr {(($tm2-$tm1)/60)%60}] - set seconds [expr {($tm2-$tm1)%60}] - set tm [format (%02d:%02d:%02d) $hours $minutes $seconds] - - if {$rc} { - set status FAIL - incr ::NERR - } else { - set status Ok - } - - set n [string length $title] - if {$::PROGRESS_MSGS} { - PUTS "finished: ${title}[string repeat . [expr {53-$n}]] $status $tm" - } else { - PUTS "${title}[string repeat . [expr {63-$n}]] $status $tm" - } - if {$errmsg!=""} {PUTS " $errmsg"} - flush stdout - } - - incr G(nJob) -1 - } else { - set line [gets $fd] - if {[string trim $line] != ""} { - puts "Trace : $title - \"$line\"" - } - } -} - -#-------------------------------------------------------------------------- -# The only argument passed to this function is a list of test-suites to -# run. Each "test-suite" is itself a list consisting of the following -# elements: -# -# * Test title (for display). -# * The name of the directory to run the test in. -# * The argument for [configureCommand] -# * The first argument for [makeCommand] -# * The second argument for [makeCommand] -# * The third argument for [makeCommand] -# -proc run_all_test_suites {alltests} { - global G - set tests $alltests - - set G(nJob) 0 - - while {[llength $tests]>0 || $G(nJob)>0} { - if {$G(nJob)>=$::JOBS || [llength $tests]==0} { - vwait G(nJob) - } - - if {[llength $tests]>0} { - set T [lindex $tests 0] - set tests [lrange $tests 1 end] - foreach {title dir configOpts testtarget makeOpts cflags opts} $T {} - if {$::PROGRESS_MSGS && !$::TRACE} { - set n [string length $title] - PUTS "starting: ${title} at [now]" - flush stdout - } - - # Run the job. - # - set tm1 [clock seconds] - incr G(nJob) - set script [file normalize [info script]] - set fd [open "|[info nameofexecutable] $script --slave" r+] - fconfigure $fd -blocking 0 - fileevent $fd readable [list slave_fileevent $fd $T $tm1] - puts $fd [list $::TRACE $::MSVC $::DRYRUN] - puts $fd [list {*}$T] - flush $fd - } - } -} - -proc add_test_suite {listvar name testtarget config} { - upvar $listvar alltests - - # Tcl variable $opts is used to build up the value used to set the - # OPTS Makefile variable. Variable $cflags holds the value for - # CFLAGS. The makefile will pass OPTS to both gcc and lemon, but - # CFLAGS is only passed to gcc. - # - set makeOpts "" - set cflags [expr {$::MSVC ? "-Zi" : "-g"}] - set opts "" - set title ${name}($testtarget) - set configOpts $::WITHTCL - - regsub -all {#[^\n]*\n} $config \n config - foreach arg $config { - if {[regexp {^-[UD]} $arg]} { - lappend opts $arg - } elseif {[regexp {^[A-Z]+=} $arg]} { - lappend testtarget $arg - } elseif {[regexp {^--(enable|disable)-} $arg]} { - if {$::MSVC} { - if {$arg eq "--disable-amalgamation"} { - lappend makeOpts USE_AMALGAMATION=0 - continue - } - if {$arg eq "--disable-shared"} { - lappend makeOpts USE_CRT_DLL=0 DYNAMIC_SHELL=0 - continue - } - if {$arg eq "--enable-fts5"} { - lappend opts -DSQLITE_ENABLE_FTS5 - continue - } - if {$arg eq "--enable-json1"} { - lappend opts -DSQLITE_ENABLE_JSON1 - continue - } - if {$arg eq "--enable-shared"} { - lappend makeOpts USE_CRT_DLL=1 DYNAMIC_SHELL=1 - continue - } - } - lappend configOpts $arg - } else { - if {$::MSVC} { - if {$arg eq "-g"} { - lappend cflags -Zi - continue - } - if {[regexp -- {^-O(\d+)$} $arg all level]} then { - lappend makeOpts OPTIMIZATIONS=$level - continue - } - } - lappend cflags $arg - } - } - - # Disable sync to make testing faster. - # - lappend opts -DSQLITE_NO_SYNC=1 - - # Some configurations already set HAVE_USLEEP; in that case, skip it. - # - if {[lsearch -regexp $opts {^-DHAVE_USLEEP(?:=|$)}]==-1} { - lappend opts -DHAVE_USLEEP=1 - } - - # Add the define for this platform. - # - if {$::tcl_platform(platform)=="windows"} { - lappend opts -DSQLITE_OS_WIN=1 - } else { - lappend opts -DSQLITE_OS_UNIX=1 - } - - # Set the sub-directory to use. - # - set dir [string tolower [string map {- _ " " _} $name]] - - # Join option lists into strings, using space as delimiter. - # - set makeOpts [join $makeOpts " "] - set cflags [join $cflags " "] - set opts [join $opts " "] - - lappend alltests [list \ - $title $dir $configOpts $testtarget $makeOpts $cflags $opts] -} - -# The following procedure returns the "configure" command to be exectued for -# the current platform, which may be Windows (via MinGW, etc). -# -proc configureCommand {opts} { - if {$::MSVC} return [list]; # This is not needed for MSVC. - set result [list trace_cmd exec] - if {$::tcl_platform(platform)=="windows"} { - lappend result sh - } - lappend result $::SRCDIR/configure --enable-load-extension - foreach x $opts {lappend result $x} - lappend result >& test.log -} - -# The following procedure returns the "make" command to be executed for the -# specified targets, compiler flags, and options. -# -proc makeCommand { targets makeOpts cflags opts } { - set result [list trace_cmd exec] - if {$::MSVC} { - set nmakeDir [file nativename $::SRCDIR] - set nmakeFile [file nativename [file join $nmakeDir Makefile.msc]] - lappend result nmake /f $nmakeFile TOP=$nmakeDir - } else { - lappend result make - } - foreach makeOpt $makeOpts { - lappend result $makeOpt - } - lappend result clean - foreach target $targets { - lappend result $target - } - lappend result CFLAGS=$cflags OPTS=$opts >>& test.log -} - -# The following procedure prints its arguments if ::TRACE is true. -# And it executes the command of its arguments in the calling context -# if ::DRYRUN is false. -# -proc trace_cmd {args} { - if {$::TRACE} { - PUTS $args - } - set res "" - if {!$::DRYRUN} { - set res [uplevel 1 $args] - } - return $res -} - - -# This proc processes the command line options passed to this script. -# Currently the only option supported is "-makefile", default -# "releasetest.mk". Set the ::MAKEFILE variable to the value of this -# option. -# -proc process_options {argv} { - set ::SRCDIR [file normalize [file dirname [file dirname $::argv0]]] - set ::QUICK 0 - set ::MSVC 0 - set ::BUILDONLY 0 - set ::DRYRUN 0 - set ::TRACE 0 - set ::JOBS 1 - set ::PROGRESS_MSGS 0 - set ::WITHTCL {} - set config {} - set platform $::tcl_platform(os)-$::tcl_platform(machine) - - for {set i 0} {$i < [llength $argv]} {incr i} { - set x [lindex $argv $i] - if {[regexp {^--[a-z]} $x]} {set x [string range $x 1 end]} - switch -glob -- $x { - -slave { - run_slave_test - exit - } - - -srcdir { - incr i - set ::SRCDIR [file normalize [lindex $argv $i]] - } - - -platform { - incr i - set platform [lindex $argv $i] - } - - -jobs { - incr i - set ::JOBS [lindex $argv $i] - } - - -progress { - set ::PROGRESS_MSGS 1 - } - - -quick { - set ::QUICK 1 - } - -veryquick { - set ::QUICK 2 - } - - -config { - incr i - set config [lindex $argv $i] - } - - -msvc { - set ::MSVC 1 - } - - -buildonly { - set ::BUILDONLY 1 - } - - -dryrun { - set ::DRYRUN 1 - } - - -trace { - set ::TRACE 1 - } - - -info { - PUTS "Command-line Options:" - PUTS " --srcdir $::SRCDIR" - PUTS " --platform [list $platform]" - PUTS " --config [list $config]" - if {$::QUICK} { - if {$::QUICK==1} {PUTS " --quick"} - if {$::QUICK==2} {PUTS " --veryquick"} - } - if {$::MSVC} {PUTS " --msvc"} - if {$::BUILDONLY} {PUTS " --buildonly"} - if {$::DRYRUN} {PUTS " --dryrun"} - if {$::TRACE} {PUTS " --trace"} - PUTS "\nAvailable --platform options:" - foreach y [lsort [array names ::Platforms]] { - PUTS " [list $y]" - } - PUTS "\nAvailable --config options:" - foreach y [lsort [array names ::Configs]] { - PUTS " [list $y]" - } - exit - } - - -g { - lappend ::EXTRACONFIG [lindex $argv $i] - } - - -with-tcl=* { - set ::WITHTCL -$x - } - - -D* - - -O* - - -enable-* - - -disable-* - - *=* { - lappend ::EXTRACONFIG [lindex $argv $i] - } - - default { - PUTSERR "" - PUTSERR [string trim $::USAGE_MESSAGE] - exit -1 - } - } - } - - if {0==[info exists ::Platforms($platform)]} { - PUTS "Unknown platform: $platform" - PUTSNNL "Set the -platform option to " - set print [list] - foreach p [array names ::Platforms] { - lappend print "\"$p\"" - } - lset print end "or [lindex $print end]" - PUTS "[join $print {, }]." - exit - } - - if {$config!=""} { - if {[llength $config]==1} {lappend config fulltest} - set ::CONFIGLIST $config - } else { - if {$::JOBS>1} { - set ::CONFIGLIST {} - foreach {target zConfig} [lreverse $::Platforms($platform)] { - append ::CONFIGLIST [format " %-25s %s\n" \ - [list $zConfig] [list $target]] - } - } else { - set ::CONFIGLIST $::Platforms($platform) - } - } - PUTS "Running the following test configurations for $platform:" - PUTS " [string trim $::CONFIGLIST]" - PUTSNNL "Flags:" - if {$::PROGRESS_MSGS} {PUTSNNL " --progress"} - if {$::DRYRUN} {PUTSNNL " --dryrun"} - if {$::BUILDONLY} {PUTSNNL " --buildonly"} - if {$::MSVC} {PUTSNNL " --msvc"} - switch -- $::QUICK { - 1 {PUTSNNL " --quick"} - 2 {PUTSNNL " --veryquick"} - } - if {$::JOBS>1} {PUTSNNL " --jobs $::JOBS"} - PUTS "" -} - -# Main routine. -# -proc main {argv} { - - # Process any command line options. - set ::EXTRACONFIG {} - process_options $argv - PUTS [string repeat * 79] - - set ::NERR 0 - set ::NTEST 0 - set ::NTESTCASE 0 - set ::NERRCASE 0 - set ::SQLITE_VERSION {} - set STARTTIME [clock seconds] - foreach {zConfig target} $::CONFIGLIST { - if {$::MSVC && ($zConfig eq "Sanitize" || "checksymbols" in $target - || "valgrindtest" in $target)} { - PUTS "Skipping $zConfig / $target for MSVC..." - continue - } - if {$target ne "checksymbols"} { - switch -- $::QUICK { - 1 {set target quicktest} - 2 {set target smoketest} - } - if {$::BUILDONLY} { - set target testfixture - if {$::tcl_platform(platform)=="windows"} { - append target .exe - } - } - } - set config_options [concat $::Configs($zConfig) $::EXTRACONFIG] - - incr NTEST - add_test_suite all $zConfig $target $config_options - - # If the configuration included the SQLITE_DEBUG option, then remove - # it and run veryquick.test. If it did not include the SQLITE_DEBUG option - # add it and run veryquick.test. - if {$target!="checksymbols" && $target!="valgrindtest" - && $target!="fuzzoomtest" && !$::BUILDONLY && $::QUICK<2} { - set debug_idx [lsearch -glob $config_options -DSQLITE_DEBUG*] - set xtarget $target - regsub -all {fulltest[a-z]*} $xtarget test xtarget - regsub -all {fuzzoomtest} $xtarget fuzztest xtarget - if {$debug_idx < 0} { - incr NTEST - append config_options " -DSQLITE_DEBUG=1" - add_test_suite all "${zConfig}_debug" $xtarget $config_options - } else { - incr NTEST - regsub { *-DSQLITE_MEMDEBUG[^ ]* *} $config_options { } config_options - regsub { *-DSQLITE_DEBUG[^ ]* *} $config_options { } config_options - add_test_suite all "${zConfig}_ndebug" $xtarget $config_options - } - } - } - - run_all_test_suites $all - - set elapsetime [expr {[clock seconds]-$STARTTIME}] - set hr [expr {$elapsetime/3600}] - set min [expr {($elapsetime/60)%60}] - set sec [expr {$elapsetime%60}] - set etime [format (%02d:%02d:%02d) $hr $min $sec] - if {$::JOBS>1} {append etime " $::JOBS cores"} - if {[catch {exec hostname} HNAME]==0} {append etime " on $HNAME"} - PUTS [string repeat * 79] - incr ::NERRCASE $::NERR - PUTS "$::NERRCASE failures out of $::NTESTCASE tests in $etime" - if {$::SQLITE_VERSION ne ""} { - PUTS "SQLite $::SQLITE_VERSION" - } -} - -main $argv diff --git a/test/reservebytes.test b/test/reservebytes.test new file mode 100644 index 0000000000..51b76c7232 --- /dev/null +++ b/test/reservebytes.test @@ -0,0 +1,51 @@ +# 2025 August 27 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix reservebytes + + + +reset_db +file_control_reservebytes db 0 + + +do_execsql_test 1.0 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c); + CREATE INDEX i1 ON t1(b, c); + WITH s(i) AS ( + VALUES(1) UNION ALL SELECT i+1 FROM s WHERE i<1000 + ) + INSERT INTO t1 SELECT NULL, i, hex(randomblob(500)) FROM s; +} + +sqlite3 db2 test.db +if {[permutation]=="prepare"} { db2 cache size 0 } + +do_execsql_test -db db2 1.1 { PRAGMA integrity_check } {ok} + +file_control_reservebytes db 8 +do_test 1.2.1 { hexio_read test.db 20 1 } {00} +do_execsql_test -db db2 1.2.2 { PRAGMA integrity_check } {ok} + +do_execsql_test 1.3.2 { VACUUM } +do_execsql_test -db db2 1.3.4 { PRAGMA integrity_check } {ok} +do_test 1.3.5 { hexio_read test.db 20 1 } {08} + +file_control_reservebytes db 16 +do_test 1.4.1 { hexio_read test.db 20 1 } {08} +do_execsql_test 1.4.2 { VACUUM } +do_execsql_test -db db2 1.4.3 { PRAGMA integrity_check } {ok} +do_test 1.4.4 { hexio_read test.db 20 1 } {10} + +finish_test diff --git a/test/resetdb.test b/test/resetdb.test new file mode 100644 index 0000000000..f2d325164c --- /dev/null +++ b/test/resetdb.test @@ -0,0 +1,300 @@ +# 2018-04-28 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# Test cases for SQLITE_DBCONFIG_RESET_DATABASE +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix resetdb + +do_not_use_codec + +ifcapable !vtab||!compound { + finish_test + return +} + +# In the "inmemory_journal" permutation, each new connection executes +# "PRAGMA journal_mode = memory". This fails with SQLITE_BUSY if attempted +# on a wal mode database with existing connections. For this and a few +# other reasons, this test is not run as part of "inmemory_journal". +# +# Permutation "journaltest" does not support wal mode. +# +if {[permutation]=="inmemory_journal" + || [permutation]=="journaltest" +} { + finish_test + return +} + +# Create a sample database +do_execsql_test 100 { + PRAGMA auto_vacuum = 0; + PRAGMA page_size=4096; + CREATE TABLE t1(a,b); + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<20) + INSERT INTO t1(a,b) SELECT x, randomblob(300) FROM c; + CREATE INDEX t1a ON t1(a); + CREATE INDEX t1b ON t1(b); + SELECT sum(a), sum(length(b)) FROM t1; + PRAGMA integrity_check; + PRAGMA journal_mode; + PRAGMA page_count; +} {210 6000 ok delete 8} + +# Verify that the same content is seen from a separate database connection +sqlite3 db2 test.db +do_test 110 { + execsql { + SELECT sum(a), sum(length(b)) FROM t1; + PRAGMA integrity_check; + PRAGMA journal_mode; + PRAGMA page_count; + } db2 +} {210 6000 ok delete 8} + +do_test 200 { + # Thoroughly corrupt the database file by overwriting the first + # page with randomness. + sqlite3_db_config db DEFENSIVE 0 + catchsql { + UPDATE sqlite_dbpage SET data=randomblob(4096) WHERE pgno=1; + PRAGMA quick_check; + } +} {1 {file is not a database}} +do_test 201 { + catchsql { + PRAGMA quick_check; + } db2 +} {1 {file is not a database}} + +do_test 210 { + # Reset the database file using SQLITE_DBCONFIG_RESET_DATABASE + sqlite3_db_config db RESET_DB 1 + db eval VACUUM + sqlite3_db_config db RESET_DB 0 + + # If using sqlite3_prepare() instead of _v2() or _v3(), the block + # below raises an SQLITE_SCHEMA error. The following fixes this. + if {[permutation]=="prepare"} { catchsql "SELECT * FROM sqlite_master" db2 } + + # Verify that the reset took, even on the separate database connection + catchsql { + PRAGMA page_count; + PRAGMA page_size; + PRAGMA quick_check; + PRAGMA journal_mode; + } db2 +} {0 {1 4096 ok delete}} + +# Delete the old connections and database and start over again +# with a different page size and in WAL mode. +# +db close +db2 close +forcedelete test.db +sqlite3 db test.db +do_execsql_test 300 { + PRAGMA auto_vacuum = 0; + PRAGMA page_size=8192; + PRAGMA journal_mode=WAL; + CREATE TABLE t1(a,b); + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<20) + INSERT INTO t1(a,b) SELECT x, randomblob(1300) FROM c; + CREATE INDEX t1a ON t1(a); + CREATE INDEX t1b ON t1(b); + SELECT sum(a), sum(length(b)) FROM t1; + PRAGMA integrity_check; + PRAGMA journal_mode; + PRAGMA page_size; + PRAGMA page_count; +} {wal 210 26000 ok wal 8192 12} +sqlite3 db2 test.db +do_test 310 { + execsql { + SELECT sum(a), sum(length(b)) FROM t1; + PRAGMA integrity_check; + PRAGMA journal_mode; + PRAGMA page_size; + PRAGMA page_count; + } db2 +} {210 26000 ok wal 8192 12} + +# Corrupt the database again +sqlite3_db_config db DEFENSIVE 0 +do_catchsql_test 320 { + UPDATE sqlite_dbpage SET data=randomblob(8192) WHERE pgno=1; + PRAGMA quick_check +} {1 {file is not a database}} + +do_test 330 { + catchsql { + PRAGMA quick_check + } db2 +} {1 {file is not a database}} + +db2 cache flush ;# Required by permutation "prepare". + +# Reset the database yet again. Verify that the page size and +# journal mode are preserved. +# +do_test 400 { + sqlite3_db_config db RESET_DB 1 + db eval VACUUM + sqlite3_db_config db RESET_DB 0 + catchsql { + PRAGMA page_count; + PRAGMA page_size; + PRAGMA journal_mode; + PRAGMA quick_check; + } db2 +} {0 {1 8192 wal ok}} +db2 close + +# Reset the database yet again. This time immediately after it is closed +# and reopened. So that the VACUUM is the first statement run. +# +db close +sqlite3 db test.db +do_test 500 { + sqlite3_finalize [ + sqlite3_prepare db "SELECT 1 FROM sqlite_master LIMIT 1" -1 tail + ] + sqlite3_db_config db RESET_DB 1 + db eval VACUUM + sqlite3_db_config db RESET_DB 0 + sqlite3 db2 test.db + catchsql { + PRAGMA page_count; + PRAGMA page_size; + PRAGMA journal_mode; + PRAGMA quick_check; + } db2 +} {0 {1 8192 wal ok}} +db2 close + +#------------------------------------------------------------------------- +reset_db +sqlite3 db2 test.db +do_execsql_test 600 { + PRAGMA journal_mode = wal; + CREATE TABLE t1(a); + INSERT INTO t1 VALUES(1), (2), (3), (4); +} {wal} + +do_execsql_test -db db2 610 { + SELECT * FROM t1 +} {1 2 3 4} + +do_test 620 { + set res [list] + db2 eval {SELECT a FROM t1} { + lappend res $a + if {$a==3} { + sqlite3_db_config db RESET_DB 1 + db eval VACUUM + sqlite3_db_config db RESET_DB 0 + } + } + + set res +} {1 2 3 4} + +do_execsql_test -db db2 630 { + SELECT * FROM sqlite_master +} {} + +#------------------------------------------------------------------------- +db2 close +reset_db + +do_execsql_test 700 { + PRAGMA page_size=512; + PRAGMA auto_vacuum = 0; + CREATE TABLE t1(a,b,c); + CREATE INDEX t1a ON t1(a); + CREATE INDEX t1bc ON t1(b,c); + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<10) + INSERT INTO t1(a,b,c) SELECT x, randomblob(100),randomblob(100) FROM c; + PRAGMA page_count; + PRAGMA integrity_check; +} {19 ok} + +if {[nonzero_reserved_bytes]} { + finish_test + return +} + +sqlite3_db_config db DEFENSIVE 0 +do_execsql_test 710 { + UPDATE sqlite_dbpage SET data= + X'53514C69746520666F726D61742033000200030100402020000000000000001300000000000000000000000300000004000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000D00000003017C0001D801AC017C00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002E03061715110145696E6465787431626374310443524541544520494E4445582074316263204F4E20743128622C63292A0206171311013F696E64657874316174310343524541544520494E44455820743161204F4E20743128612926010617111101397461626C657431743102435245415445205441424C4520743128612C622C6329' WHERE pgno=1; +} + +do_execsql_test 720 { + PRAGMA integrity_check; +} {ok} + +do_test 730 { + sqlite3_db_config db RESET_DB 1 + db eval VACUUM + sqlite3_db_config db RESET_DB 0 +} {0} + +do_execsql_test 740 { + PRAGMA page_count; + PRAGMA integrity_check; +} {1 ok} + +#------------------------------------------------------------------------- +ifcapable utf16 { + reset_db + do_execsql_test 800 { + PRAGMA encoding = 'utf8'; + CREATE TABLE t1(a, b); + PRAGMA encoding; + } {UTF-8} + + db close + sqlite3 db test.db + + sqlite3 db2 test.db + do_execsql_test -db db2 810 { + CREATE TEMP TABLE t2(x); + INSERT INTO t2 VALUES('hello world'); + SELECT name FROM sqlite_schema; + } {t1} + do_test 820 { + db eval "PRAGMA encoding = 'utf16'" + sqlite3_db_config db RESET_DB 1 + } {1} + do_test 830 { + db eval VACUUM + sqlite3_db_config db RESET_DB 0 + } {0} + do_test 840 { + string range [db eval { + CREATE TABLE t1(a, b); + INSERT INTO t1 VALUES('one', 'two'); + PRAGMA encoding; + }] 0 5 + } {UTF-16} + + do_test 850 { + catchsql { SELECT * FROM t1; } db2 + } {1 {attached databases must use the same text encoding as main database}} + do_test 860 { + catchsql { SELECT * FROM t2; } db2 + } {1 {attached databases must use the same text encoding as main database}} +} + +finish_test diff --git a/test/returning1.test b/test/returning1.test new file mode 100644 index 0000000000..9ab646a3b7 --- /dev/null +++ b/test/returning1.test @@ -0,0 +1,597 @@ +# 2021-01-28 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is the new RETURNING clause +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix returning1 + +do_execsql_test 1.0 { + CREATE TABLE t1(a INTEGER PRIMARY KEY,b,c DEFAULT 'pax'); + INSERT INTO t1(b) VALUES(10),('happy'),(NULL) RETURNING a,b,c; +} {1 10 pax 2 happy pax 3 {} pax} +do_execsql_test 1.1 { + SELECT * FROM t1; +} {1 10 pax 2 happy pax 3 {} pax} +do_execsql_test 1.2 { + INSERT INTO t1(b,c) VALUES(5,99) RETURNING b,c,a,rowid; +} {5 99 4 4} +do_execsql_test 1.3 { + SELECT * FROM t1; +} {1 10 pax 2 happy pax 3 {} pax 4 5 99} +do_execsql_test 1.4 { + INSERT INTO t1 DEFAULT VALUES RETURNING *; +} {5 {} pax} +do_execsql_test 1.5 { + SELECT * FROM t1; +} {1 10 pax 2 happy pax 3 {} pax 4 5 99 5 {} pax} +do_execsql_test 1.6 { + CREATE TABLE t2(x,y,z); + INSERT INTO t2 VALUES(11,12,13),(21,'b','c'),(31,'b-value',4.75); +} +do_execsql_test 1.7 { + INSERT INTO t1 SELECT * FROM t2 RETURNING *; +} {11 12 13 21 b c 31 b-value 4.75} +do_execsql_test 1.8 { + SELECT *, '|' FROM t1; +} {1 10 pax | 2 happy pax | 3 {} pax | 4 5 99 | 5 {} pax | 11 12 13 | 21 b c | 31 b-value 4.75 |} + +do_execsql_test 2.1 { + UPDATE t1 SET c='bellum' WHERE c='pax' RETURNING rowid, b, '|'; +} {1 10 | 2 happy | 3 {} | 5 {} |} +do_execsql_test 2.2 { + SELECT *, '|' FROM t1; +} {1 10 bellum | 2 happy bellum | 3 {} bellum | 4 5 99 | 5 {} bellum | 11 12 13 | 21 b c | 31 b-value 4.75 |} + +do_execsql_test 3.1 { + DELETE FROM t1 WHERE c='bellum' RETURNING rowid, *, '|'; +} {1 1 10 bellum | 2 2 happy bellum | 3 3 {} bellum | 5 5 {} bellum |} +do_execsql_test 3.2 { + SELECT *, '|' FROM t1; +} {4 5 99 | 11 12 13 | 21 b c | 31 b-value 4.75 |} + +do_execsql_test 4.1 { + CREATE TABLE t4(a INT, b INT DEFAULT 1234, c INT DEFAULT -16); + CREATE UNIQUE INDEX t4a ON t4(a); + INSERT INTO t4(a,b,c) VALUES(1,2,3); +} {} +do_execsql_test 4.2 { + INSERT INTO t4(a,b,c) VALUES(1,22,33) + ON CONFLICT(a) DO UPDATE SET b=44 + RETURNING *; +} {1 44 3} +do_execsql_test 4.3 { + SELECT * FROM t4; +} {1 44 3} +do_execsql_test 4.4 { + DELETE FROM t4; + INSERT INTO t4 VALUES(1,2,3),(4,5,6),(7,8,9); +} {} +do_execsql_test 4.5 { + INSERT INTO t4(a,b,c) VALUES(2,3,4),(4,5,6),(5,6,7) + ON CONFLICT(a) DO UPDATE SET b=100 + RETURNING *, '|'; +} {2 3 4 | 4 100 6 | 5 6 7 |} + +#------------------------------------------------------------------------- +# Test RETURNING on a table with virtual columns. +# +reset_db +do_execsql_test 5.0 { + CREATE TABLE t1(xyz); + CREATE TABLE t2(a as (1+1), b); +} + +do_execsql_test 5.1 { + UPDATE t2 SET b='123' WHERE b='abc' RETURNING (SELECT b FROM t1); +} {} + +do_execsql_test 5.2 { + INSERT INTO t2(b) VALUES('abc'); +} + +do_execsql_test 5.3 { + UPDATE t2 SET b='123' WHERE b='abc' RETURNING (SELECT b FROM t1); +} {{}} + +do_execsql_test 5.4 { + INSERT INTO t2(b) VALUES('abc'); + INSERT INTO t1(xyz) VALUES(1); + UPDATE t2 SET b='123' WHERE b='abc' RETURNING b; +} {123} + +do_execsql_test 5.5 { + INSERT INTO t2(b) VALUES('abc'); + UPDATE t2 SET b='123' WHERE b='abc' RETURNING (SELECT b FROM t1); +} {123} + +# Ticket 132994c8b1063bfb +reset_db +do_catchsql_test 6.0 { + CREATE TABLE t1(id INTEGER PRIMARY KEY); + CREATE TABLE t2(x INT, y INT); + INSERT INTO t1 VALUES(1),(2),(4),(9); + INSERT INTO t2 VALUES(3,7), (4,25), (5,99); + UPDATE t1 SET id=id+y FROM t2 WHERE t1.id=t2.x RETURNING t2.*; +} {1 {RETURNING may not use "TABLE.*" wildcards}} +do_catchsql_test 6.1 { + UPDATE t1 SET id=id+y FROM t2 WHERE t1.id=t2.x RETURNING *, '|'; + SELECT * FROM t1 ORDER BY id; +} {0 {29 | 1 2 9 29}} + +# Forum https://sqlite.org/forum/forumpost/85aef8bc01 +# Do not silently ignore nonsense table names in the RETURNING clause. +# Raise an error. +# +reset_db +do_execsql_test 7.1 { + CREATE TABLE t1(a INT, b INT); + CREATE TABLE t2(x INT, y INT); + INSERT INTO t1(a,b) VALUES(1,2); + INSERT INTO t2(x,y) VALUES(1,30); +} {} +do_catchsql_test 7.2 { + UPDATE t1 SET b=b+1 RETURNING new.b; +} {1 {no such column: new.b}} +do_catchsql_test 7.3 { + UPDATE t1 SET b=b+1 RETURNING old.b; +} {1 {no such column: old.b}} +do_catchsql_test 7.4 { + UPDATE t1 SET b=b+1 RETURNING another.b; +} {1 {no such column: another.b}} +do_catchsql_test 7.5 { + UPDATE t1 SET b=b+y FROM t2 WHERE t2.x=t1.a RETURNING t2.x; +} {1 {no such column: t2.x}} +do_catchsql_test 7.6 { + UPDATE t1 SET b=b+y FROM t2 WHERE t2.x=t1.a RETURNING t1.b; +} {0 32} + +# This is goofy: The RETURNING clause does not honor the alias +# for the table being modified. This might change in the future. +# +do_catchsql_test 7.7 { + UPDATE t1 AS alias SET b=123 RETURNING alias.b; +} {1 {no such column: alias.b}} +do_catchsql_test 7.8 { + UPDATE t1 AS alias SET b=alias.b+1000 RETURNING t1.b; +} {0 1032} + +# Forum: https://sqlite.org/forum/info/34c81d83c9177f46 +reset_db +do_execsql_test 8.1 { + CREATE TABLE t1(a); + CREATE TABLE t2(b,c); + INSERT INTO t1 VALUES(1); + INSERT INTO t2 VALUES(3,40); +} {} +do_catchsql_test 8.2 { + INSERT INTO t1 VALUES(3) RETURNING a, (SELECT c FROM t2 WHERE new.a=t2.b) AS x; +} {1 {no such column: new.a}} +do_catchsql_test 8.3 { + INSERT INTO t1 VALUES(3) RETURNING a, (SELECT c FROM t2 WHERE old.a=t2.b) AS x; +} {1 {no such column: old.a}} +do_catchsql_test 8.4 { + INSERT INTO t1 VALUES(3) RETURNING a, (SELECT c FROM t2 WHERE t1.a=t2.b) AS x; +} {0 {3 40}} + +ifcapable vtab { +# dbsqlfuzz finds/crash-486f791cbe2dc45839310073e71367a1d8ad22dd +do_catchsql_test 9.1 { + UPDATE pragma_encoding SET encoding='UTF-8' RETURNING a, b, *; +} {1 {table pragma_encoding may not be modified}} +} ;# ifcapable vtab + +# dbsqlfuzz crash-0081f863d7b2002045ac2361879fc80dfebb98f1 +reset_db +do_execsql_test 10.1 { + CREATE TABLE t1_a(a, b); + CREATE VIEW t1 AS SELECT a, b FROM t1_a; + + INSERT INTO t1_a VALUES('x', 'y'); + INSERT INTO t1_a VALUES('x', 'y'); + INSERT INTO t1_a VALUES('x', 'y'); + + CREATE TABLE log(op, r, a, b); +} +do_execsql_test 10.2 { + CREATE TRIGGER tr1 INSTEAD OF INSERT ON t1 BEGIN + INSERT INTO log VALUES('insert', new.rowid, new.a, new.b); + END; + CREATE TRIGGER tr2 INSTEAD OF UPDATE ON t1 BEGIN + INSERT INTO log VALUES('update', new.rowid, new.a, new.b); + END; +} + +ifcapable !allow_rowid_in_view { + do_catchsql_test 10.3a { + INSERT INTO t1(a, b) VALUES(1234, 5678) RETURNING rowid; + } {1 {no such column: new.rowid}} + + do_catchsql_test 10.3b { + UPDATE t1 SET a='z' WHERE b='y' RETURNING rowid; + } {1 {no such column: new.rowid}} + + do_execsql_test 10.4 { + SELECT * FROM log; + } {} +} else { + # Note: The values returned by the RETURNING clauses of the following + # two statements are the rowid columns of views. These values are not + # well defined, so the INSERT returns -1, and the UPDATE returns NULL. + # These match the values used for new.rowid expressions, but not much + # else. + do_catchsql_test 10.3a { + INSERT INTO t1(a, b) VALUES(1234, 5678) RETURNING rowid; + } {0 -1} + + do_catchsql_test 10.3b { + UPDATE t1 SET a='z' WHERE b='y' RETURNING rowid; + } {0 {{} {} {}}} + + do_execsql_test 10.4 { + SELECT * FROM log; + } { + insert -1 1234 5678 update {} z y update {} z y update {} z y + } +} + +# 2021-04-27 dbsqlfuzz 78b9400770ef8cc7d9427dfba26f4fcf46ea7dc2 +# Returning clauses on TEMP tables with triggers. +# +reset_db +do_execsql_test 11.1 { + CREATE TEMP TABLE t1(a,b); + CREATE TEMP TABLE t2(c,d); + CREATE TEMP TABLE t3(e,f); + CREATE TEMP TABLE log(op,x,y); + CREATE TEMP TRIGGER t1r1 AFTER INSERT ON t1 BEGIN + INSERT INTO log(op,x,y) VALUES('I1',new.a,new.b); + END; + CREATE TEMP TRIGGER t1r2 BEFORE DELETE ON t1 BEGIN + INSERT INTO log(op,x,y) VALUES('D1',old.a,old.b); + END; + CREATE TEMP TRIGGER t2r3 AFTER UPDATE ON t1 BEGIN + INSERT INTO log(op,x,y) VALUES('U1',new.a,new.b); + END; + CREATE TEMP TRIGGER t2r1 BEFORE INSERT ON t2 BEGIN + INSERT INTO log(op,x,y) VALUES('I2',new.c,new.d); + END; + CREATE TEMP TRIGGER t3r1 AFTER DELETE ON t3 BEGIN + INSERT INTO log(op,x,y) VALUES('D3',old.e,old.f); + END; + CREATE TEMP TRIGGER t3r2 BEFORE UPDATE ON t3 BEGIN + INSERT INTO log(op,x,y) VALUES('U3',new.e,new.f); + END; + INSERT INTO t1(a,b) VALUES(1,2),('happy','glad') RETURNING a, b, '|'; +} {1 2 | happy glad |} +do_execsql_test 11.2 { + UPDATE t1 SET b=9 WHERE a=1 RETURNING a, b, 'x'; +} {1 9 x} +do_execsql_test 11.3 { + DELETE FROM t1 WHERE a<>'xray' RETURNING a, b, '@'; +} {1 9 @ happy glad @} +do_execsql_test 11.4 { + SELECT * FROM log; + DELETE FROM log; +} {I1 1 2 I1 happy glad U1 1 9 D1 1 9 D1 happy glad} +do_execsql_test 11.5 { + INSERT INTO t2 VALUES('bravo','charlie') RETURNING d, c, 'z'; +} {charlie bravo z} +do_execsql_test 11.6 { + SELECT * FROM log; + DELETE FROM log; +} {I2 bravo charlie} +do_execsql_test 11.7 { + INSERT INTO t3(e) VALUES(1),(2),(3) RETURNING 'I', e; + UPDATE t3 SET f=e+100 RETURNING 'U', e, f; + DELETE FROM t3 WHERE f>100 RETURNING 'D', e, f; +} {I 1 I 2 I 3 U 1 101 U 2 102 U 3 103 D 1 101 D 2 102 D 3 103} +do_execsql_test 11.6 { + SELECT * FROM log; + DELETE FROM log; +} {U3 1 101 U3 2 102 U3 3 103 D3 1 101 D3 2 102 D3 3 103} + +reset_db +do_execsql_test 11.11 { + CREATE TEMP TABLE t1(a,b); + CREATE TRIGGER r1 BEFORE INSERT ON t1 BEGIN SELECT 1; END; + DELETE FROM t1 RETURNING *; + DROP TRIGGER r1; + INSERT INTO t1 VALUES(5,30); +} {} +do_execsql_test 11.12 { + SELECT * FROM t1; +} {5 30} + +# RETURNING column names are dequoted. +# https://sqlite.org/forum/forumpost/033daf0b32 +# +reset_db +do_test 12.1 { + db eval {CREATE TABLE t1(x INT, y INT)} + unset -nocomplain cname + db eval {INSERT INTO t1(x) VALUES(1) RETURNING "x";} cname {} + lsort [array names cname] +} {* x} +do_test 12.2 { + unset -nocomplain cname + db eval {INSERT INTO t1(x) VALUES(2) RETURNING [x];} cname {} + lsort [array names cname] +} {* x} +do_test 12.3 { + unset -nocomplain cname + db eval {INSERT INTO t1(x) VALUES(3) RETURNING x AS [xyz];} cname {} + lsort [array names cname] +} {* xyz} +do_test 12.4 { + unset -nocomplain cname + db eval {INSERT INTO t1(x,y) VALUES(4,5) RETURNING "x"+"y";} cname {} + lsort [array names cname] +} {{"x"+"y"} *} + +ifcapable rtree { +#------------------------------------------------------------------------- +# Based on dbsqlfuzz find crash-ffbba524cac354b2a61bfd677cec9d2a4333f49a +reset_db +do_execsql_test 13.0 { + CREATE VIRTUAL TABLE t1 USING rtree(a, b, c); + CREATE TABLE t2(x); +} + +do_execsql_test 13.1 { + INSERT INTO t1(a,b,c) VALUES(1,2,3) + RETURNING (SELECT b FROM t2); +} {{}} +} ;# end ifcapable rtree + +# 2021-12-01 Forum post https://sqlite.org/forum/forumpost/793beaf322 +# Need to report foreign key constraint errors prior to RETURNING +# +reset_db +do_execsql_test 14.0 { + PRAGMA foreign_keys(1); + CREATE TABLE Parent(id INTEGER PRIMARY KEY); + CREATE TABLE Child(id INTEGER PRIMARY KEY, parent_id INTEGER REFERENCES Parent(id)); +} {} +do_catchsql_test 14.1 { + INSERT INTO child(parent_id) VALUES(123) RETURNING id; +} {1 {FOREIGN KEY constraint failed}} + +# 2021-12-28 Forum post https://sqlite.org/forum/forumpost/e0c7574ab2 +# Incorrect affinity for REAL values that can be represented as integers. +# +reset_db +sqlite3_test_control SQLITE_TESTCTRL_INTERNAL_FUNCTIONS db +do_execsql_test 15.0 { + CREATE TABLE t1(x REAL); + INSERT INTO t1(x) VALUES(5.0) RETURNING x, affinity(x); +} {5.0 real} +do_execsql_test 15.1 { + UPDATE t1 SET x=x+1 RETURNING x, affinity(x); +} {6.0 real} +do_execsql_test 15.2 { + DELETE FROM t1 RETURNING x, affinity(x); +} {6.0 real} + +# 2022-02-28 Forum post https://sqlite.org/forum/forumpost/595e132f71 +# RETURNING with the xfer optimization +# +reset_db +do_execsql_test 16.0 { + CREATE TABLE t1(a,b,c); + INSERT INTO t1 VALUES(1,2,3),('a','b','c'); + CREATE TEMP TABLE t2(x,y,z); + INSERT INTO t2 SELECT * FROM t1 RETURNING *; +} {1 2 3 a b c} +do_execsql_test 16.1 { + SELECT * FROM t2; +} {1 2 3 a b c} + + +foreach {tn temp} { + 1 "" + 2 TEMP +} { + reset_db + do_execsql_test 17.$tn.0 " + CREATE $temp TABLE foo ( + fooid INTEGER PRIMARY KEY, + fooval INTEGER NOT NULL UNIQUE, + refcnt INTEGER NOT NULL DEFAULT 1 + ); + " + do_execsql_test 17.$tn.1 { + INSERT INTO foo (fooval) VALUES (17), (4711), (17) + ON CONFLICT DO + UPDATE SET refcnt = refcnt+1 + RETURNING fooid; + } { + 1 2 1 + } +} + +# 2022-01-13 https://sqlite.org/forum/forumpost/d010a26798 +# +reset_db +do_execsql_test 17.0 { + CREATE TABLE bug(id INTEGER PRIMARY KEY NOT NULL, x); + INSERT INTO bug(id,x) VALUES(20, NULL); + UPDATE bug SET x=NULL WHERE id = 20 RETURNING quote(x), x IS NULL; +} {NULL 1} + +# 2023-03-08 https://sqlite.org/forum/forumpost/f5a2b1db87 +# NULL pointer dereference following an error. +# +do_execsql_test 18.0 { + CREATE TABLE v0(c1 INT); + CREATE VIEW view_2(c1) AS SELECT CASE WHEN c1 COLLATE TRUE THEN TRUE ELSE TRUE END FROM v0; + CREATE TRIGGER x1 INSTEAD OF INSERT ON view_2 BEGIN SELECT true; END; +} +do_catchsql_test 18.1 { + INSERT INTO view_2 DEFAULT VALUES RETURNING *; +} {1 {no such collation sequence: TRUE}} + +# 2023-03-16 +# https://sqlite.org/forum/forumpost/c99d6e0329 +# ticket d15b3a4ea901ef0d +# ticket 89d259d45b855a0d +# +# A RETURNING clause on an IF NOT EXISTS trigger does not generate +# an error if the trigger already exists. +# +do_execsql_test 19.0 { + DROP TABLE IF EXISTS t1;CREATE TABLE t1(a); + CREATE TRIGGER r1 AFTER UPDATE ON t1 BEGIN VALUES(0); END; +} {} +do_catchsql_test 19.1 { + CREATE TRIGGER IF NOT EXISTS r1 AFTER DELETE ON t1 BEGIN + INSERT INTO t1(a) VALUES (1) RETURNING FALSE; + INSERT INTO t1(a) VALUES (2) RETURNING TRUE; + END; +} {0 {}} + +# 2024-04-24 +# https://sqlite.org/forum/forumpost/2c83569ce8945d39 +# +# If the RETURNING clause includes subqueries that reference the +# table being modified, make sure that the subqueries are identified +# as correlated so that the results are recomputed after each step +# instead of being computed once and reused. +# +reset_db +db null N +do_execsql_test 20.1 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b INT); + INSERT INTO t1 VALUES(1,10),(2,20),(3,30),(4,40),(6,60),(8,80); + BEGIN; + DELETE FROM t1 WHERE a<>3 + RETURNING a, + (SELECT min(a) FROM t1), + (SELECT max(a) FROM t1), + (SELECT round(avg(a),2) FROM t1); + ROLLBACK; +} { + 1 2 8 4.6 + 2 3 8 5.25 + 4 3 8 5.67 + 6 3 8 5.5 + 8 3 3 3.0 +} +do_execsql_test 20.2 { + BEGIN; + DELETE FROM t1 + RETURNING a, + (SELECT min(a) FROM t1), + (SELECT max(a) FROM t1), + (SELECT round(avg(a),2) FROM t1); + ROLLBACK; +} { + 1 2 8 4.6 + 2 3 8 5.25 + 3 4 8 6.0 + 4 6 8 7.0 + 6 8 8 8.0 + 8 N N N +} +do_execsql_test 20.3 { + BEGIN; + DELETE FROM t1 + RETURNING a, + (SELECT min(t2.a)+t1.a*100 FROM t1 AS t2), + (SELECT max(t2.a)+t1.a*100 FROM t1 AS t2), + (SELECT round(avg(t2.a),2)+t1.a*100 FROM t1 AS t2); + ROLLBACK; +} { + 1 102 108 104.6 + 2 203 208 205.25 + 3 304 308 306.0 + 4 406 408 407.0 + 6 608 608 608.0 + 8 N N N +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 21.0 { + PRAGMA writable_schema=ON; + INSERT INTO sqlite_schema DEFAULT VALUES RETURNING sqlite_schema.name; +} {{}} + +do_execsql_test 21.1 { + INSERT INTO sqlite_temp_schema DEFAULT VALUES RETURNING sqlite_temp_schema.name; +} {{}} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 22.0 { + PRAGMA writable_schema=ON; + CREATE TABLE xyz (a); +} +do_catchsql_test 22.1 { + INSERT INTO sqlite_temp_schema DEFAULT VALUES + RETURNING + (SELECT * FROM xyz AS sqlite_master WHERE a=sqlite_master.name); +} {1 {no such column: sqlite_master.name}} + + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 23.0 { + PRAGMA recursive_triggers = 1; + CREATE TABLE t1(x, y); + CREATE TRIGGER t1insert AFTER INSERT ON t1 WHEN new.x<5 BEGIN + INSERT INTO t1 VALUES(new.x+1, new.y); + END; +} + +do_execsql_test 23.1 { + INSERT INTO t1 VALUES(1, 'one') RETURNING *; +} {1 one} + +do_execsql_test 23.2 { + SELECT * FROM t1 +} {1 one 2 one 3 one 4 one 5 one} + +#------------------------------------------------------------------------- +reset_db +ifcapable fts5 { + + do_execsql_test 24.0 { + CREATE VIRTUAL TABLE ft USING fts5(c); + CREATE TABLE t1(x); + INSERT INTO t1 VALUES('x'); + } + + db close + + sqlite3 db test.db + sqlite3 db2 test.db + + do_execsql_test 24.1 { + SELECT * FROM t1 + } {x} + + do_execsql_test -db db2 24.2 { + CREATE TABLE t2(y); + INSERT INTO t2 VALUES('y'); + } {} + + db2 close + + do_execsql_test 24.3 { + INSERT INTO ft VALUES('hello world') RETURNING * + } {{hello world}} +} + + +finish_test diff --git a/test/returningfault.test b/test/returningfault.test new file mode 100644 index 0000000000..b0177e6dc8 --- /dev/null +++ b/test/returningfault.test @@ -0,0 +1,85 @@ +# 2022 January 5 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/malloc_common.tcl + +set ::testprefix returningfault + + +do_execsql_test 1.0 { + CREATE TABLE t1 (b); +} {} +faultsim_save_and_close + +do_faultsim_test 1 -faults oom-t* -prep { + faultsim_restore_and_reopen +} -body { + execsql { + INSERT INTO t1(b) VALUES(65) RETURNING ( + SELECT * FROM sqlite_temp_schema + ) AS aaa; + } +} -test { + faultsim_test_result {1 {sub-select returns 5 columns - expected 1}} +} + +ifcapable vtab { + reset_db + do_execsql_test 2.0 { + CREATE TABLE t1(x); + } + + proc eponymous_cmd {method args} { + switch -- $method { + xConnect { + db eval { SELECT * FROM sqlite_schema } + return "CREATE TABLE t1 (a, b)" + } + + xBestIndex { + return "idxnum 555" + } + + xFilter { + return [list sql {SELECT 123, 'A', 'B'}] + } + + xUpdate { + return 123 + } + + } + + return {} + } + + faultsim_save_and_close + + do_faultsim_test 2 -faults oom* -prep { + faultsim_restore_and_reopen + register_tcl_module db eponymous_cmd + db eval { SELECT * FROM t1 } + sqlite3 db2 test.db + db2 eval { CREATE TABLE t2(y) } + db2 close + } -body { + db eval { + INSERT INTO tcl VALUES('hello', 'world') RETURNING * + } + } -test { + faultsim_test_result {0 {hello world}} {1 {vtable constructor failed: tcl}} + } +} + +finish_test diff --git a/test/rollback.test b/test/rollback.test index 7abafece61..f32ce523e5 100644 --- a/test/rollback.test +++ b/test/rollback.test @@ -82,6 +82,8 @@ do_test rollback-1.9 { if {$tcl_platform(platform) == "unix" && [permutation] ne "onefile" && [permutation] ne "inmemory_journal" + && [permutation] ne "atomic-batch-write" + && [atomic_batch_write test.db]==0 } { do_test rollback-2.1 { execsql { @@ -109,7 +111,7 @@ if {$tcl_platform(platform) == "unix" ] set iOffset [expr (([file size testA.db-journal] + 511)/512)*512] set fd [open testA.db-journal a+] - fconfigure $fd -encoding binary -translation binary + fconfigure $fd -translation binary seek $fd $iOffset puts -nonewline $fd $zAppend diff --git a/test/rollback2.test b/test/rollback2.test index 3ba0f3f9fd..21ebdeaca0 100644 --- a/test/rollback2.test +++ b/test/rollback2.test @@ -101,7 +101,7 @@ do_rollback_test 2.2 -setup { # do_eqp_test 3.1 { SELECT i FROM t1 WHERE (i%2)==0 ORDER BY h DESC; -} {0 0 0 {SCAN TABLE t1 USING INDEX i1}} +} {SCAN t1 USING INDEX i1} do_rollback_test 3.2 -setup { BEGIN; DELETE FROM t1 WHERE (i%2)==1; @@ -131,7 +131,7 @@ do_execsql_test 4.1 { UPDATE t1 SET h = $leader || h; } do_eqp_test 4.2 { SELECT i FROM t1 WHERE (i%2)==0 ORDER BY h ASC; -} {0 0 0 {SCAN TABLE t1 USING INDEX i1}} +} {SCAN t1 USING INDEX i1} do_rollback_test 4.3 -setup { BEGIN; DELETE FROM t1 WHERE (i%2)==1; diff --git a/test/round1.test b/test/round1.test new file mode 100644 index 0000000000..2857593be8 --- /dev/null +++ b/test/round1.test @@ -0,0 +1,42 @@ +# 2019-05-24 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# Test cases for rounding behavior of floating point values. +# +# TESTRUNNER: slow + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix round1 + +expr srand(0) +unset -nocomplain iTest +for {set iTest 1} {$iTest<=50000} {incr iTest} { + set x1 [expr int(rand()*100000)] + set x2 [expr int(rand()*100000)+1000*int(rand()*10000)] + set n [expr int(rand()*8)+1] + set x3 [string range [format %09d $x2] [expr {9-$n}] end] + set r $x1.$x3 + set ans [string trimright $r 0] + if {[string match *. $ans]} {set ans ${ans}0} + do_test $iTest/$n/${r}4=>$ans { + set x [db one "SELECT round(${r}4,$n)"] + } $ans + set x4 [string range [format %09d [expr {$x2+1}]] [expr {9-$n}] end] + if {[string trim $x3 9]==""} {incr x1} + set r2 $x1.$x4 + set ans [string trimright $r2 0] + if {[string match *. $ans]} {set ans ${ans}0} + do_test $iTest/$n/${r}6=>$ans { + set x [db one "SELECT round(${r}6,$n)"] + } $ans +} + +finish_test diff --git a/test/rowid.test b/test/rowid.test index 56336453fa..84f0e4d300 100644 --- a/test/rowid.test +++ b/test/rowid.test @@ -18,6 +18,7 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl +set testprefix rowid # Basic ROWID functionality tests. # @@ -659,6 +660,32 @@ do_test rowid-11.4 { execsql {SELECT rowid, a FROM t5 WHERE rowid<='abc'} } {1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8} +do_test rowid-11.asc.1 { + execsql {SELECT rowid, a FROM t5 WHERE rowid>'abc' ORDER BY 1 ASC} +} {} +do_test rowid-11.asc.2 { + execsql {SELECT rowid, a FROM t5 WHERE rowid>='abc' ORDER BY 1 ASC} +} {} +do_test rowid-11.asc.3 { + execsql {SELECT rowid, a FROM t5 WHERE rowid<'abc' ORDER BY 1 ASC} +} {1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8} +do_test rowid-11.asc.4 { + execsql {SELECT rowid, a FROM t5 WHERE rowid<='abc' ORDER BY 1 ASC} +} {1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8} + +do_test rowid-11.desc.1 { + execsql {SELECT rowid, a FROM t5 WHERE rowid>'abc' ORDER BY 1 DESC} +} {} +do_test rowid-11.desc.2 { + execsql {SELECT rowid, a FROM t5 WHERE rowid>='abc' ORDER BY 1 DESC} +} {} +do_test rowid-11.desc.3 { + execsql {SELECT rowid, a FROM t5 WHERE rowid<'abc' ORDER BY 1 DESC} +} {8 8 7 7 6 6 5 5 4 4 3 3 2 2 1 1} +do_test rowid-11.desc.4 { + execsql {SELECT rowid, a FROM t5 WHERE rowid<='abc' ORDER BY 1 DESC} +} {8 8 7 7 6 6 5 5 4 4 3 3 2 2 1 1} + # Test the automatic generation of rowids when the table already contains # a rowid with the maximum value. # @@ -719,4 +746,88 @@ do_execsql_test rowid-13.1 { SELECT last_insert_rowid(); } {1234 5 2234 | 2234 4990756 3234 | 3234 10458756 4234 | 4234} +#------------------------------------------------------------------------- +do_execsql_test rowid-14.0 { + CREATE TABLE t14(x INTEGER PRIMARY KEY); + INSERT INTO t14(x) VALUES (100); +} +do_execsql_test rowid-14.1 { + SELECT * FROM t14 WHERE x < 'a' ORDER BY rowid ASC; +} {100} +do_execsql_test rowid-14.2 { + SELECT * FROM t14 WHERE x < 'a' ORDER BY rowid DESC; +} {100} + +do_execsql_test rowid-14.3 { + DELETE FROM t14; + SELECT * FROM t14 WHERE x < 'a' ORDER BY rowid ASC; +} {} +do_execsql_test rowid-14.4 { + SELECT * FROM t14 WHERE x < 'a' ORDER BY rowid DESC; +} {} + +reset_db +do_execsql_test rowid-15.0 { + PRAGMA reverse_unordered_selects=true; + CREATE TABLE t1 (c0, c1); + CREATE TABLE t2 (c0 INT UNIQUE); + INSERT INTO t1(c0, c1) VALUES (0, 0), (0, NULL); + INSERT INTO t2(c0) VALUES (1); +} + +do_execsql_test rowid-15.1 { + SELECT t2.c0, t1.c1 FROM t1, t2 + WHERE (t2.rowid <= 'a') OR (t1.c0 <= t2.c0) LIMIT 100 +} {1 {} 1 0} + +do_execsql_test rowid-15.2 { + SELECT 1, NULL INTERSECT SELECT * FROM ( + SELECT t2.c0, t1.c1 FROM t1, t2 + WHERE ((t2.rowid <= 'a')) OR (t1.c0 <= t2.c0) ORDER BY 'a' DESC LIMIT 100 + ); +} {1 {}} + +#------------------------------------------------------------------------- +# Check that an unqualified "rowid" can be used in join queries so long +# as only one of the source objects has a rowid column. +# +reset_db +do_execsql_test 16.0 { + CREATE TABLE t1(x); + CREATE TABLE t2(y PRIMARY KEY) WITHOUT ROWID; + CREATE VIEW v1 AS SELECT x FROM t1; + CREATE TABLE t3(z); + + INSERT INTO t1(rowid, x) VALUES(1, 1); + INSERT INTO t2(y) VALUES(2); + INSERT INTO t3(rowid, z) VALUES(3, 3); +} + +ifcapable allow_rowid_in_view { + set nosuch "1 {ambiguous column name: rowid}" + do_execsql_test 16.1 { SELECT rowid FROM t1, t2; } {1} + do_catchsql_test 16.2 { SELECT rowid FROM t1, v1; } $nosuch + do_catchsql_test 16.3 { SELECT rowid FROM t3, v1; } $nosuch + do_catchsql_test 16.4 { SELECT rowid FROM t3, (SELECT 123); } $nosuch + + do_execsql_test 16.5 { SELECT rowid FROM t2, t1; } {1} + do_catchsql_test 16.6 { SELECT rowid FROM v1, t1; } $nosuch + do_catchsql_test 16.7 { SELECT rowid FROM v1, t3; } $nosuch + do_execsql_test 16.8 { SELECT rowid FROM (SELECT 123), t3; } {3} +} else { + do_execsql_test 16.1 { SELECT rowid FROM t1, t2; } {1} + do_execsql_test 16.2 { SELECT rowid FROM t1, v1; } {1} + do_execsql_test 16.3 { SELECT rowid FROM t3, v1; } {3} + do_execsql_test 16.4 { SELECT rowid FROM t3, (SELECT 123); } {3} + + do_execsql_test 16.5 { SELECT rowid FROM t2, t1; } {1} + do_execsql_test 16.6 { SELECT rowid FROM v1, t1; } {1} + do_execsql_test 16.7 { SELECT rowid FROM v1, t3; } {3} + do_execsql_test 16.8 { SELECT rowid FROM (SELECT 123), t3; } {3} +} + +do_catchsql_test 16.9 { + SELECT rowid FROM t1, t3; +} {1 {ambiguous column name: rowid}} + finish_test diff --git a/test/rowvalue.test b/test/rowvalue.test new file mode 100644 index 0000000000..b6286302d1 --- /dev/null +++ b/test/rowvalue.test @@ -0,0 +1,858 @@ +# 2016 June 17 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is testing the SELECT statement. +# + + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set ::testprefix rowvalue + +do_execsql_test 0.0 { + CREATE TABLE one(o); + INSERT INTO one VALUES(1); +} + +foreach {tn v1 v2 eq ne is isnot} { + 1 "1, 2, 3" "1, 2, 3" 1 0 1 0 + 2 "1, 0, 3" "1, 2, 3" 0 1 0 1 + 3 "1, 2, NULL" "1, 2, 3" {} {} 0 1 + 4 "1, 2, NULL" "1, 2, NULL" {} {} 1 0 + 5 "NULL, NULL, NULL" "NULL, NULL, NULL" {} {} 1 0 + + 6 "1, NULL, 1" "1, 1, 1" {} {} 0 1 + 7 "1, NULL, 1" "1, 1, 2" 0 1 0 1 +} { + do_execsql_test 1.$tn.eq "SELECT ($v1) == ($v2)" [list $eq] + do_execsql_test 1.$tn.ne "SELECT ($v1) != ($v2)" [list $ne] + + do_execsql_test 1.$tn.is "SELECT ($v1) IS ($v2)" [list $is] + do_execsql_test 1.$tn.isnot "SELECT ($v1) IS NOT ($v2)" [list $isnot] + + do_execsql_test 1.$tn.2.eq "SELECT (SELECT $v1) == (SELECT $v2)" [list $eq] + do_execsql_test 1.$tn.2.ne "SELECT (SELECT $v1) != (SELECT $v2)" [list $ne] +} + +foreach {tn v1 v2 lt gt le ge} { + 1 "(1, 1, 3)" "(1, 2, 3)" 1 0 1 0 + 2 "(1, 2, 3)" "(1, 2, 3)" 0 0 1 1 + 3 "(1, 3, 3)" "(1, 2, 3)" 0 1 0 1 + + 4 "(1, NULL, 3)" "(1, 2, 3)" {} {} {} {} + 5 "(1, 3, 3)" "(1, NULL, 3)" {} {} {} {} + 6 "(1, NULL, 3)" "(1, NULL, 3)" {} {} {} {} +} { + foreach {tn2 expr res} [list \ + 2.$tn.lt "$v1 < $v2" $lt \ + 2.$tn.gt "$v1 > $v2" $gt \ + 2.$tn.le "$v1 <= $v2" $le \ + 2.$tn.ge "$v1 >= $v2" $ge \ + ] { + do_execsql_test $tn2 "SELECT $expr" [list $res] + + set map(0) [list] + set map() [list] + set map(1) [list 1] + do_execsql_test $tn2.where1 "SELECT * FROM one WHERE $expr" $map($res) + + set map(0) [list 1] + set map() [list] + set map(1) [list] + do_execsql_test $tn2.where2 "SELECT * FROM one WHERE NOT $expr" $map($res) + } +} + +do_execsql_test 3.0 { + CREATE TABLE t1(x, y); + INSERT INTO t1 VALUES(1, 1); + INSERT INTO t1 VALUES(1, 2); + INSERT INTO t1 VALUES(2, 3); + INSERT INTO t1 VALUES(2, 4); + INSERT INTO t1 VALUES(3, 5); + INSERT INTO t1 VALUES(3, 6); +} + +foreach {tn r order} { + 1 "(1, 1)" "ORDER BY y" + 2 "(1, 1)" "ORDER BY x, y" + 3 "(1, 2)" "ORDER BY x, y DESC" + 4 "(3, 6)" "ORDER BY x DESC, y DESC" + 5 "((3, 5))" "ORDER BY x DESC, y" + 6 "(SELECT 3, 5)" "ORDER BY x DESC, y" +} { + do_execsql_test 3.$tn.1 "SELECT $r == (SELECT x,y FROM t1 $order)" 1 + do_execsql_test 3.$tn.2 "SELECT $r == (SELECT * FROM t1 $order)" 1 + + do_execsql_test 3.$tn.3 " + SELECT (SELECT * FROM t1 $order) == (SELECT * FROM t1 $order) + " 1 + do_execsql_test 3.$tn.4 " + SELECT (SELECT 0, 0) == (SELECT * FROM t1 $order) + " 0 +} + +foreach {tn expr res} { + 1 {(2, 2) BETWEEN (2, 2) AND (3, 3)} 1 + 2 {(2, 2) BETWEEN (2, NULL) AND (3, 3)} {} + 3 {(2, 2) BETWEEN (3, NULL) AND (3, 3)} 0 +} { + do_execsql_test 4.$tn "SELECT $expr" [list $res] +} + +foreach {tn expr res} { + 1 {(2, 4) IN (SELECT * FROM t1)} 1 + 2 {(3, 4) IN (SELECT * FROM t1)} 0 + + 3 {(NULL, 4) IN (SELECT * FROM t1)} {} + 4 {(NULL, 0) IN (SELECT * FROM t1)} 0 + + 5 {(NULL, 4) NOT IN (SELECT * FROM t1)} {} + 6 {(NULL, 0) NOT IN (SELECT * FROM t1)} 1 +} { + do_execsql_test 5.$tn "SELECT $expr" [list $res] +} + +do_execsql_test 6.0 { + CREATE TABLE hh(a, b, c); + INSERT INTO hh VALUES('abc', 1, 'i'); + INSERT INTO hh VALUES('ABC', 1, 'ii'); + INSERT INTO hh VALUES('def', 2, 'iii'); + INSERT INTO hh VALUES('DEF', 2, 'iv'); + INSERT INTO hh VALUES('GHI', 3, 'v'); + INSERT INTO hh VALUES('ghi', 3, 'vi'); + + CREATE INDEX hh_ab ON hh(a, b); +} + +do_execsql_test 6.1 { + SELECT c FROM hh WHERE (a, b) = (SELECT 'abc', 1); +} {i} +do_execsql_test 6.2 { + SELECT c FROM hh WHERE (a, b) = (SELECT 'abc' COLLATE nocase, 1); +} {i} +do_execsql_test 6.3 { + SELECT c FROM hh WHERE a = (SELECT 'abc' COLLATE nocase) AND b = (SELECT 1); +} {i} +do_execsql_test 6.4 { + SELECT c FROM hh WHERE +a = (SELECT 'abc' COLLATE nocase) AND b = (SELECT 1); +} {i} +do_execsql_test 6.5 { + SELECT c FROM hh WHERE a = (SELECT 'abc') COLLATE nocase AND b = (SELECT 1); +} {i ii} +do_catchsql_test 6.6 { + SELECT c FROM hh WHERE (a, b) = (SELECT 'abc', 1) COLLATE nocase; +} {1 {row value misused}} +do_catchsql_test 6.7 { + SELECT c FROM hh WHERE (a, b) = 1; +} {1 {row value misused}} +do_execsql_test 6.8 { + SELECT c FROM hh WHERE (a COLLATE nocase, b) = (SELECT 'def', 2); +} {iii iv} +do_execsql_test 6.9 { + SELECT c FROM hh WHERE (a COLLATE nocase, b) IS NOT (SELECT 'def', 2); +} {i ii v vi} +do_execsql_test 6.10 { + SELECT c FROM hh WHERE (b, a) = (SELECT 2, 'def'); +} {iii} + +do_execsql_test 7.0 { + CREATE TABLE xy(i INTEGER PRIMARY KEY, j, k); + INSERT INTO xy VALUES(1, 1, 1); + INSERT INTO xy VALUES(2, 2, 2); + INSERT INTO xy VALUES(3, 3, 3); + INSERT INTO xy VALUES(4, 4, 4); +} + + +foreach {tn sql res eqp} { + 1 "SELECT * FROM xy WHERE (i, j) IS (2, 2)" {2 2 2} + "SEARCH xy USING INTEGER PRIMARY KEY (rowid=?)" + + 2 "SELECT * FROM xy WHERE (k, j) < (2, 3)" {1 1 1 2 2 2} + "SCAN xy" + + 3 "SELECT * FROM xy WHERE (i, j) < (2, 3)" {1 1 1 2 2 2} + "SEARCH xy USING INTEGER PRIMARY KEY (rowid<?)" + + 4 "SELECT * FROM xy WHERE (i, j) > (2, 1)" {2 2 2 3 3 3 4 4 4} + "SEARCH xy USING INTEGER PRIMARY KEY (rowid>?)" + + 5 "SELECT * FROM xy WHERE (i, j) > ('2', 1)" {2 2 2 3 3 3 4 4 4} + "SEARCH xy USING INTEGER PRIMARY KEY (rowid>?)" + +} { + do_eqp_test 7.$tn.1 $sql $eqp + do_execsql_test 7.$tn.2 $sql $res +} + +do_execsql_test 8.0 { + CREATE TABLE j1(a); +} +do_execsql_test 8.1 { + SELECT * FROM j1 WHERE (select min(a) FROM j1) IN (?, ?, ?) +} + +do_execsql_test 9.0 { + CREATE TABLE t2(a INTEGER PRIMARY KEY, b, c); + INSERT INTO t2 VALUES(1, 1, 1); + INSERT INTO t2 VALUES(2, 2, 2); + INSERT INTO t2 VALUES(3, 3, 3); + INSERT INTO t2 VALUES(4, 4, 4); + INSERT INTO t2 VALUES(5, 5, 5); +} + +foreach {tn q res} { + 1 "(a, b) > (2, 1)" {2 3 4 5} + 2 "(a, b) > (2, 2)" {3 4 5} + 3 "(a, b) < (4, 5)" {1 2 3 4} + 4 "(a, b) < (4, 3)" {1 2 3} +} { + do_execsql_test 9.$tn "SELECT c FROM t2 WHERE $q" $res +} + +do_execsql_test 10.0 { + CREATE TABLE dual(dummy); INSERT INTO dual(dummy) VALUES('X'); + CREATE TABLE t3(a TEXT,b TEXT,c TEXT,d TEXT,e TEXT,f TEXT); + CREATE INDEX t3x ON t3(b,c,d,e,f); + + SELECT a FROM t3 + WHERE (c,d) IN (SELECT 'c','d' FROM dual) + AND (a,b,e) IN (SELECT 'a','b','d' FROM dual); +} + +do_catchsql_test 11.1 { + CREATE TABLE t11(a); + SELECT * FROM t11 WHERE (a,a)<=1; +} {1 {row value misused}} +do_catchsql_test 11.2 { + SELECT * FROM t11 WHERE (a,a)<1; +} {1 {row value misused}} +do_catchsql_test 11.3 { + SELECT * FROM t11 WHERE (a,a)>=1; +} {1 {row value misused}} +do_catchsql_test 11.4 { + SELECT * FROM t11 WHERE (a,a)>1; +} {1 {row value misused}} +do_catchsql_test 11.5 { + SELECT * FROM t11 WHERE (a,a)==1; +} {1 {row value misused}} +do_catchsql_test 11.6 { + SELECT * FROM t11 WHERE (a,a)<>1; +} {1 {row value misused}} +do_catchsql_test 11.7 { + SELECT * FROM t11 WHERE (a,a) IS 1; +} {1 {row value misused}} +do_catchsql_test 11.8 { + SELECT * FROM t11 WHERE (a,a) IS NOT 1; +} {1 {row value misused}} + +# 2016-10-27: https://sqlite.org/src/tktview/fef4bb4bd9185ec8f +# Incorrect result from a LEFT JOIN with a row-value constraint +# +do_execsql_test 12.1 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a INT,b INT); INSERT INTO t1 VALUES(1,2); + DROP TABLE IF EXISTS t2; + CREATE TABLE t2(x INT,y INT); INSERT INTO t2 VALUES(3,4); + SELECT *,'x' FROM t1 LEFT JOIN t2 ON (a,b)=(x,y); +} {1 2 {} {} x} +db null - +do_execsql_test 12.2 { + SELECT t1.*, t2.* FROM t2 RIGHT JOIN t1 ON (a,b)=(x,y); +} {1 2 - -} +do_execsql_test 12.3 { + SELECT t1.*, t2.* FROM t1 FULL JOIN t2 ON (a,b)=(x,y) + ORDER BY coalesce(a,x); +} { + 1 2 - - + - - 3 4 +} +db null {} + + +foreach {tn sql} { + 0 "SELECT (1,2) AS x WHERE x=3" + 1 "SELECT (1,2) BETWEEN 1 AND 2" + 2 "SELECT 1 BETWEEN (1,2) AND 2" + 3 "SELECT 2 BETWEEN 1 AND (1,2)" + 4 "SELECT (1,2) FROM (SELECT 1) ORDER BY 1" + 5 "SELECT (1,2) FROM (SELECT 1) GROUP BY 1" +} { + do_catchsql_test 13.$tn $sql {1 {row value misused}} +} + +do_execsql_test 14.0 { + CREATE TABLE t12(x); + INSERT INTO t12 VALUES(2), (4); +} +do_execsql_test 14.1 "SELECT 1 WHERE (2,2) BETWEEN (1,1) AND (3,3)" 1 +do_execsql_test 14.2 "SELECT CASE (2,2) WHEN (1, 1) THEN 2 ELSE 1 END" 1 +do_execsql_test 14.3 "SELECT CASE (SELECT 2,2) WHEN (1, 1) THEN 2 ELSE 1 END" 1 +do_execsql_test 14.4 "SELECT 1 WHERE (SELECT 2,2) BETWEEN (1,1) AND (3,3)" 1 +do_execsql_test 14.5 "SELECT 1 FROM t12 WHERE (x,1) BETWEEN (1,1) AND (3,3)" 1 +do_execsql_test 14.6 { + SELECT 1 FROM t12 WHERE (1,x) BETWEEN (1,1) AND (3,3) +} {1 1} + +#------------------------------------------------------------------------- +# Test that errors are not concealed by the SELECT flattening or +# WHERE-clause push-down optimizations. +do_execsql_test 14.1 { + CREATE TABLE x1(a PRIMARY KEY, b); + CREATE TABLE x2(a INTEGER PRIMARY KEY, b); +} + +foreach {tn n sql} { + 1 0 "SELECT * FROM (SELECT (1, 1) AS c FROM x1) WHERE c=1" + 2 2 "SELECT * FROM (SELECT 1 AS x, (SELECT 8,9) AS y) WHERE y<1" + 3 3 "SELECT * FROM (SELECT 1 AS x, (SELECT 8,9,10) AS y) WHERE y<1" + 4 0 "SELECT * FROM (SELECT (a, b) AS c FROM x1), x2 WHERE c=a" + 5 0 "SELECT * FROM (SELECT a AS c, (1, 2, 3) FROM x1), x2 WHERE c=a" + 6 0 "SELECT * FROM (SELECT 1 AS c, (1, 2, 3) FROM x1) WHERE c=1" +} { + if {$n==0} { + set err "row value misused" + } else { + set err "sub-select returns $n columns - expected 1" + } + do_catchsql_test 14.2.$tn $sql [list 1 $err] +} + +#-------------------------------------------------------------------------- +# Test for vector size mismatches concealed by unexpanded subqueries. +# +do_catchsql_test 15.1 { + DETACH (SELECT * FROM (SELECT 1,2))<3; +} {1 {row value misused}} +do_catchsql_test 15.2 { + UPDATE x1 SET a=(SELECT * FROM (SELECT b,2))<3; +} {1 {row value misused}} +do_catchsql_test 15.3 { + UPDATE x1 SET a=NULL WHERE a<(SELECT * FROM (SELECT b,2)); +} {1 {sub-select returns 2 columns - expected 1}} +do_catchsql_test 15.4 { + DELETE FROM x1 WHERE a<(SELECT * FROM (SELECT b,2)); +} {1 {sub-select returns 2 columns - expected 1}} +do_catchsql_test 15.5 { + INSERT INTO x1(a,b) VALUES(1,(SELECT * FROM (SELECT 1,2))<3); +} {1 {row value misused}} + +#------------------------------------------------------------------------- +# Row-values used in UPDATE statements within TRIGGERs +# +# Ticket https://sqlite.org/src/info/8c9458e703666e1a +# +do_execsql_test 16.1 { + CREATE TABLE t16a(a,b,c); + INSERT INTO t16a VALUES(1,2,3); + CREATE TABLE t16b(x); + INSERT INTO t16b(x) VALUES(1); + CREATE TRIGGER t16r AFTER UPDATE ON t16b BEGIN + UPDATE t16a SET (a,b,c)=(SELECT new.x,new.x+1,new.x+2); + END; + UPDATE t16b SET x=7; + SELECT * FROM t16a; +} {7 8 9} +do_execsql_test 16.2 { + UPDATE t16b SET x=97; + SELECT * FROM t16a; +} {97 98 99} + +do_execsql_test 16.3 { + CREATE TABLE t16c(a, b, c, d, e); + INSERT INTO t16c VALUES(1, 'a', 'b', 'c', 'd'); + CREATE TRIGGER t16c1 AFTER INSERT ON t16c BEGIN + UPDATE t16c SET (c, d) = (SELECT 'A', 'B'), (e, b) = (SELECT 'C', 'D') + WHERE a = new.a-1; + END; + + SELECT * FROM t16c; +} {1 a b c d} + +do_execsql_test 16.4 { + INSERT INTO t16c VALUES(2, 'w', 'x', 'y', 'z'); + SELECT * FROM t16c; +} { + 1 D A B C + 2 w x y z +} + +do_execsql_test 16.5 { + DROP TRIGGER t16c1; + PRAGMA recursive_triggers = 1; + INSERT INTO t16c VALUES(3, 'i', 'ii', 'iii', 'iv'); + CREATE TRIGGER t16c1 AFTER UPDATE ON t16c WHEN new.a>1 BEGIN + UPDATE t16c SET (e, d) = ( + SELECT b, c FROM t16c WHERE a = new.a-1 + ), (c, b) = ( + SELECT d, e FROM t16c WHERE a = new.a-1 + ) WHERE a = new.a-1; + END; + + UPDATE t16c SET a=a WHERE a=3; + SELECT * FROM t16c; +} { + 1 C B A D + 2 z y x w + 3 i ii iii iv +} + +do_execsql_test 17.0 { + CREATE TABLE b1(a, b); + CREATE TABLE b2(x); +} + +do_execsql_test 17.1 { + SELECT * FROM b2 CROSS JOIN b1 + WHERE b2.x=b1.a AND (b1.a, 2) + IN (VALUES(1, 2)); +} {} + +do_execsql_test 18.0 { + CREATE TABLE b3 ( a, b, PRIMARY KEY (a, b) ); + CREATE TABLE b4 ( a ); + CREATE TABLE b5 ( a, b ); + INSERT INTO b3 VALUES (1, 1), (1, 2); + INSERT INTO b4 VALUES (1); + INSERT INTO b5 VALUES (1, 1), (1, 2); +} + +do_execsql_test 18.1 { + SELECT * FROM b3 WHERE (SELECT b3.a, b3.b) IN ( SELECT a, b FROM b5 ) +} {1 1 1 2} +do_execsql_test 18.2 { + SELECT * FROM b3 WHERE (VALUES(b3.a, b3.b)) IN ( SELECT a, b FROM b5 ); +} {1 1 1 2} +do_execsql_test 18.3 { + SELECT * FROM b3 WHERE (b3.a, b3.b) IN ( SELECT a, b FROM b5 ); +} {1 1 1 2} +do_execsql_test 18.4 { + SELECT * FROM b3 JOIN b4 ON b4.a = b3.a + WHERE (SELECT b3.a, b3.b) IN ( SELECT a, b FROM b5 ); +} {1 1 1 1 2 1} +do_execsql_test 18.5 { + SELECT * FROM b3 JOIN b4 ON b4.a = b3.a + WHERE (VALUES(b3.a, b3.b)) IN ( SELECT a, b FROM b5 ); +} {1 1 1 1 2 1} +do_execsql_test 18.6 { + SELECT * FROM b3 JOIN b4 ON b4.a = b3.a + WHERE (b3.a, b3.b) IN ( SELECT a, b FROM b5 ); +} {1 1 1 1 2 1} + + +# 2018-02-13 Ticket https://sqlite.org/src/tktview/f484b65f3d6230593c3 +# Incorrect result from a row-value comparison in the WHERE clause. +# +do_execsql_test 19.1 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a INTEGER PRIMARY KEY,b); + INSERT INTO t1(a,b) VALUES(1,11),(2,22),(3,33),(4,44); + SELECT * FROM t1 WHERE (a,b)>(0,0) ORDER BY a; +} {1 11 2 22 3 33 4 44} +do_execsql_test 19.2 { + SELECT * FROM t1 WHERE (a,b)>=(0,0) ORDER BY a; +} {1 11 2 22 3 33 4 44} +do_execsql_test 19.3 { + SELECT * FROM t1 WHERE (a,b)<(5,0) ORDER BY a DESC; +} {4 44 3 33 2 22 1 11} +do_execsql_test 19.4 { + SELECT * FROM t1 WHERE (a,b)<=(5,0) ORDER BY a DESC; +} {4 44 3 33 2 22 1 11} +do_execsql_test 19.5 { + SELECT * FROM t1 WHERE (a,b)>(3,0) ORDER BY a; +} {3 33 4 44} +do_execsql_test 19.6 { + SELECT * FROM t1 WHERE (a,b)>=(3,0) ORDER BY a; +} {3 33 4 44} +do_execsql_test 19.7 { + SELECT * FROM t1 WHERE (a,b)<(3,0) ORDER BY a DESC; +} {2 22 1 11} +do_execsql_test 19.8 { + SELECT * FROM t1 WHERE (a,b)<=(3,0) ORDER BY a DESC; +} {2 22 1 11} +do_execsql_test 19.9 { + SELECT * FROM t1 WHERE (a,b)>(3,32) ORDER BY a; +} {3 33 4 44} +do_execsql_test 19.10 { + SELECT * FROM t1 WHERE (a,b)>(3,33) ORDER BY a; +} {4 44} +do_execsql_test 19.11 { + SELECT * FROM t1 WHERE (a,b)>=(3,33) ORDER BY a; +} {3 33 4 44} +do_execsql_test 19.12 { + SELECT * FROM t1 WHERE (a,b)>=(3,34) ORDER BY a; +} {4 44} +do_execsql_test 19.13 { + SELECT * FROM t1 WHERE (a,b)<(3,34) ORDER BY a DESC; +} {3 33 2 22 1 11} +do_execsql_test 19.14 { + SELECT * FROM t1 WHERE (a,b)<(3,33) ORDER BY a DESC; +} {2 22 1 11} +do_execsql_test 19.15 { + SELECT * FROM t1 WHERE (a,b)<=(3,33) ORDER BY a DESC; +} {3 33 2 22 1 11} +do_execsql_test 19.16 { + SELECT * FROM t1 WHERE (a,b)<=(3,32) ORDER BY a DESC; +} {2 22 1 11} +do_execsql_test 19.21 { + SELECT * FROM t1 WHERE (0,0)<(a,b) ORDER BY a; +} {1 11 2 22 3 33 4 44} +do_execsql_test 19.22 { + SELECT * FROM t1 WHERE (0,0)<=(a,b) ORDER BY a; +} {1 11 2 22 3 33 4 44} +do_execsql_test 19.23 { + SELECT * FROM t1 WHERE (5,0)>(a,b) ORDER BY a DESC; +} {4 44 3 33 2 22 1 11} +do_execsql_test 19.24 { + SELECT * FROM t1 WHERE (5,0)>=(a,b) ORDER BY a DESC; +} {4 44 3 33 2 22 1 11} +do_execsql_test 19.25 { + SELECT * FROM t1 WHERE (3,0)<(a,b) ORDER BY a; +} {3 33 4 44} +do_execsql_test 19.26 { + SELECT * FROM t1 WHERE (3,0)<=(a,b) ORDER BY a; +} {3 33 4 44} +do_execsql_test 19.27 { + SELECT * FROM t1 WHERE (3,0)>(a,b) ORDER BY a DESC; +} {2 22 1 11} +do_execsql_test 19.28 { + SELECT * FROM t1 WHERE (3,0)>=(a,b) ORDER BY a DESC; +} {2 22 1 11} +do_execsql_test 19.29 { + SELECT * FROM t1 WHERE (3,32)<(a,b) ORDER BY a; +} {3 33 4 44} +do_execsql_test 19.30 { + SELECT * FROM t1 WHERE (3,33)<(a,b) ORDER BY a; +} {4 44} +do_execsql_test 19.31 { + SELECT * FROM t1 WHERE (3,33)<=(a,b) ORDER BY a; +} {3 33 4 44} +do_execsql_test 19.32 { + SELECT * FROM t1 WHERE (3,34)<=(a,b) ORDER BY a; +} {4 44} +do_execsql_test 19.33 { + SELECT * FROM t1 WHERE (3,34)>(a,b) ORDER BY a DESC; +} {3 33 2 22 1 11} +do_execsql_test 19.34 { + SELECT * FROM t1 WHERE (3,33)>(a,b) ORDER BY a DESC; +} {2 22 1 11} +do_execsql_test 19.35 { + SELECT * FROM t1 WHERE (3,33)>=(a,b) ORDER BY a DESC; +} {3 33 2 22 1 11} +do_execsql_test 19.36 { + SELECT * FROM t1 WHERE (3,32)>=(a,b) ORDER BY a DESC; +} {2 22 1 11} + +# 2018-02-18: Memory leak nested row-value. Detected by OSSFuzz. +# +do_catchsql_test 20.1 { + SELECT 1 WHERE (2,(2,0)) IS (2,(2,0)); +} {0 1} + +# 2018-11-03: Ticket https://sqlite.org/src/info/1a84668dcfdebaf1 +# Assertion fault when doing row-value operations on a primary key +# containing duplicate columns. +# +do_execsql_test 21.0 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a,b,PRIMARY KEY(b,b)); + INSERT INTO t1 VALUES(1,2),(3,4),(5,6); + SELECT * FROM t1 WHERE (a,b) IN (VALUES(1,2)); +} {1 2} + +# 2019-08-09: Multi-column subquery on the RHS of an IN operator. +# +do_execsql_test 22.100 { + SELECT (SELECT 3,4 UNION SELECT 5,6 ORDER BY 1) IN (SELECT 3,4); + SELECT (SELECT 3,4 UNION SELECT 5,6 ORDER BY 1) IN (SELECT 5,6); + SELECT (SELECT 5,6 UNION SELECT 3,4 ORDER BY 1) IN (SELECT 3,4); + SELECT (SELECT 5,6 UNION SELECT 3,4 ORDER BY 1) IN (SELECT 5,6); + SELECT (SELECT 3,4 UNION SELECT 5,6 ORDER BY 1 DESC) IN (SELECT 3,4); + SELECT (SELECT 3,4 UNION SELECT 5,6 ORDER BY 1 DESC) IN (SELECT 5,6); + SELECT (SELECT 5,6 UNION SELECT 3,4 ORDER BY 1 DESC) IN (SELECT 3,4); + SELECT (SELECT 5,6 UNION SELECT 3,4 ORDER BY 1 DESC) IN (SELECT 5,6); +} {1 0 1 0 0 1 0 1} + +# 2019-10-21 Ticket b47e3627ecaadbde +# +do_execsql_test 23.100 { + DROP TABLE IF EXISTS t0; + CREATE TABLE t0(aa COLLATE NOCASE, bb); + INSERT INTO t0 VALUES('a', 'A'); + SELECT (+bb,1) >= (aa, 1), (aa,1)<=(+bb,1) FROM t0; + SELECT 2 FROM t0 WHERE (+bb,1) >= (aa,1); + SELECT 3 FROM t0 WHERE (aa,1) <= (+bb,1); +} {0 1 3} +do_execsql_test 23.110 { + SELECT (SELECT +bb,1) >= (aa, 1), (aa,1)<=(SELECT +bb,1) FROM t0; + SELECT 2 FROM t0 WHERE (SELECT +bb,1) >= (aa,1); + SELECT 3 FROM t0 WHERE (aa,1) <= (SELECT +bb,1); +} {0 1 3} + +# 2019-10-22 Ticket 6ef984af8972c2eb +do_execsql_test 24.100 { + DROP TABLE t0; + CREATE TABLE t0(c0 TEXT PRIMARY KEY); + INSERT INTO t0(c0) VALUES (''); + SELECT (t0.c0, TRUE) > (CAST(0 AS REAL), FALSE) FROM t0; + SELECT 2 FROM t0 WHERE (t0.c0, TRUE) > (CAST('' AS REAL), FALSE); +} {1 2} + +# 2019-10-23 Ticket 135c9da7513e5a97 +do_execsql_test 25.10 { + DROP TABLE t0; + CREATE TABLE t0(c0 UNIQUE); + INSERT INTO t0(c0) VALUES('a'); + SELECT (t0.c0, 0) < ('B' COLLATE NOCASE, 0) FROM t0; + SELECT 2 FROM t0 WHERE (t0.c0, 0) < ('B' COLLATE NOCASE, 0); +} {1 2} +do_execsql_test 25.20 { + SELECT ('B' COLLATE NOCASE, 0)> (t0.c0, 0) FROM t0; + SELECT 2 FROM t0 WHERE ('B' COLLATE NOCASE, 0)> (t0.c0, 0); +} {1 2} +do_execsql_test 25.30 { + SELECT ('B', 0)> (t0.c0 COLLATE nocase, 0) FROM t0; + SELECT 2 FROM t0 WHERE ('B', 0)> (t0.c0 COLLATE nocase, 0); +} {1 2} +do_execsql_test 25.40 { + SELECT (t0.c0 COLLATE nocase, 0) < ('B', 0) FROM t0; + SELECT 2 FROM t0 WHERE (t0.c0 COLLATE nocase, 0) < ('B', 0); +} {1 2} + +# 2019-11-04 Ticket 02aa2bd02f97d0f2 +# The TK_VECTOR operator messes up sqlite3ExprImpliesNonNull() which +# causes incorrect LEFT JOIN strength reduction. TK_VECTOR should be +# treated the same as TK_OR. +# +db close +sqlite3 db :memory: +do_execsql_test 26.10 { + CREATE TABLE t0(c0); + CREATE TABLE t1(c1); + INSERT INTO t1(c1) VALUES (0); + SELECT (c0, x'') != (NULL, 0) FROM t1 LEFT JOIN t0; +} {1} +do_execsql_test 26.20 { + SELECT 2 FROM t1 LEFT JOIN t0 ON (c0, x'') != (NULL, 0); +} {2} +do_execsql_test 26.21 { + SELECT 21 FROM t0 RIGHT JOIN t1 ON (c0, x'') != (NULL, 0); +} {21} +do_execsql_test 26.30 { + SELECT 3 FROM t1 LEFT JOIN t0 WHERE (c0, x'') != (NULL, 0); +} {3} +do_execsql_test 26.31 { + SELECT 31 FROM t0 RIGHT JOIN t1 WHERE (c0, x'') != (NULL, 0); +} {31} + +# 2019-12-30 ticket 892575cdba4e1e36 +# +reset_db +do_catchsql_test 27.10 { + CREATE TABLE t0(c0 CHECK(((0, 0) > (0, c0)))); + INSERT INTO t0(c0) VALUES(0) ON CONFLICT(c0) DO UPDATE SET c0 = 3; +} {1 {ON CONFLICT clause does not match any PRIMARY KEY or UNIQUE constraint}} + +# 2021-02-03 +# https://bugs.chromium.org/p/chromium/issues/detail?id=1173511 +# Faulty assert() statement. +# +reset_db +do_catchsql_test 28.10 { + CREATE TABLE t0(c0 PRIMARY KEY, c1); + CREATE TRIGGER trigger0 BEFORE DELETE ON t0 BEGIN + SELECT (SELECT c0,c1 FROM t0) FROM t0; + END ; + DELETE FROM t0; +} {1 {sub-select returns 2 columns - expected 1}} + +# 2021-03-19 +# dbsqlfuzz find of a NEVER(). +do_catchsql_test 29.1 { + SELECT (SELECT 1 WHERE ((SELECT 1 WHERE (2,(2,0)) IS (2,(20))),(2,0)) IS (2,(20))) WHERE (2,(2,0)) IS (2 IN(SELECT 1 WHERE (2,(2,2,0)) IS (2,(20))),(20)); +} {1 {row value misused}} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 30.0 { + CREATE TABLE t1(x, y, z); + CREATE TABLE t2(a, b); + + INSERT INTO t1 VALUES(1000, 2000, 3000); + INSERT INTO t2 VALUES(NULL, NULL); +} + +do_execsql_test 30.1 { + UPDATE t2 SET (a,b)=( + SELECT max( t1.x ) OVER( PARTITION BY sum( (SELECT t1.y) ) ), 2 + ) + FROM t1; +} {} + +do_execsql_test 30.2 { + SELECT * FROM t2 +} {1000 2} + +reset_db +do_execsql_test 30.3 { + CREATE TABLE t1(x INT PRIMARY KEY, y, z); + CREATE TABLE t2(a,b,c,d,e,PRIMARY KEY(a,b))WITHOUT ROWID; + + UPDATE t2 SET (d,d,a)=(SELECT EXISTS(SELECT 1 IN(SELECT max( 1 IN(SELECT x ORDER BY 1)) OVER(PARTITION BY sum((SELECT y FROM t1 UNION SELECT x ORDER BY 1)))INTERSECT SELECT EXISTS(SELECT 1 FROM t1 UNION SELECT x ORDER BY 1) ORDER BY 1) ORDERa)|9 AS blob, 2, 3) FROM t1 WHERE x<a; +} + +# 2022-01-21 https://sqlite.org/forum/forumpost/ab95010d410a0a55 +reset_db +do_execsql_test 31.1 { + CREATE TABLE a(a1 PRIMARY KEY,a2); + INSERT INTO a VALUES(1,5); + CREATE TABLE b(b1 UNIQUE,b2); + SELECT * FROM a LEFT JOIN b ON b2=NULL AND b2=5 WHERE (b1,substr(b.b1,1,1))==(SELECT 1024,'b'); +} {} +do_execsql_test 31.1b { + SELECT * FROM b RIGHT JOIN a ON b2=NULL AND b2=5 WHERE (b1,substr(b.b1,1,1))==(SELECT 1024,'b'); +} {} +do_execsql_test 31.2 { + CREATE TABLE t1(a); + INSERT INTO t1 VALUES(0); + CREATE TABLE t2(b,c,d); + INSERT INTO t2 VALUES(NULL,123,456); + SELECT * FROM t1 LEFT JOIN t2 ON b=NULL WHERE (c,d)==(SELECT 123, 456+a); +} {} +do_execsql_test 31.2b { + SELECT * FROM t2 RIGHT JOIN t1 ON b=NULL WHERE (c,d)==(SELECT 123, 456+a); +} {} + +# 2022-02-03 dbsqlfuzz 80a9fade844b4fb43564efc972bcb2c68270f5d1 +reset_db +do_execsql_test 32.1 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b INT, c INT); + CREATE TABLE t2(d INTEGER PRIMARY KEY); + INSERT INTO t1(a,b,c) VALUES(500,654,456); + INSERT INTO t1(a,b,c) VALUES(501,655,456); + INSERT INTO t1(a,b,c) VALUES(502,654,122); + INSERT INTO t1(a,b,c) VALUES(503,654,221); + INSERT INTO t1(a,b,c) VALUES(601,654,122); + INSERT INTO t2(d) VALUES(456); + INSERT INTO t2(d) VALUES(122); + SELECT a FROM ( + SELECT t1.a FROM t2, t1 + WHERE (987, t1.b) = ( SELECT 987, 654 ) AND t2.d=t1.c + ) AS t3 + WHERE a=1234 OR a<=567; +} {500 502} + +# 2022-07-15 +# https://sqlite.org/forum/forumpost/3607259d3c +# +reset_db +do_execsql_test 33.1 { + CREATE TABLE t1(a INT, b INT PRIMARY KEY) WITHOUT ROWID; + INSERT INTO t1(a, b) VALUES (0, 1),(15,-7),(3,100); + ANALYZE; +} {} +do_execsql_test 33.2 { + SELECT * FROM t1 WHERE (b,a) BETWEEN (0,5) AND (99,-2); +} {0 1} +do_execsql_test 33.3 { + SELECT * FROM t1 WHERE (b,a) BETWEEN (-8,5) AND (0,-2); +} {15 -7} +do_execsql_test 33.3 { + SELECT * FROM t1 WHERE (b,a) BETWEEN (3,5) AND (100,4); +} {3 100} +do_execsql_test 33.3 { + SELECT * FROM t1 WHERE (b,a) BETWEEN (3,5) AND (100,2); +} {} +do_execsql_test 33.3 { + SELECT * FROM t1 WHERE (a,b) BETWEEN (-2,99) AND (1,0); +} {0 1} +do_execsql_test 33.3 { + SELECT * FROM t1 WHERE (a,b) BETWEEN (14,99) AND (16,0); +} {15 -7} +do_execsql_test 33.3 { + SELECT * FROM t1 WHERE (a,b) BETWEEN (2,99) AND (4,0); +} {3 100} + +# 2025-04-15 https://sqlite.org/forum/forumpost/b9647a113b465950 +# Incorrect result when the schema includes a table with a UNIQUE +# constraint and one of the columns in the UNIQUE constraint is the +# INTEGER PRIMARY KEY, and the columns that UNIQUE constraint are +# used in a rowvalue-IN operator constraint. +# +# 2025-07-07 Discovered that the original fix was incomplete and +# new tests added. See tag-20250707-01 in the code. +# +reset_db +do_execsql_test 34.1 { + CREATE TABLE items ( + Id INTEGER /* rowid alias */, + Item INTEGER /* any type */, + Test TEXT /* TEXT or BLOB */, + Filler, /* any type */ + PRIMARY KEY(Id), + UNIQUE(Item, Id) + ); + INSERT INTO items (Id, Item) + VALUES (1, 2), (2, 2), (3, 3), (4, 5); + UPDATE items SET test='ok' + WHERE (Id, Item) IN (SELECT Id, Item FROM items); + SELECT Id, Item, test FROM items ORDER BY id; +} {1 2 ok 2 2 ok 3 3 ok 4 5 ok} +db null NULL +do_execsql_test 34.2 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c, d); + CREATE INDEX idx ON t1(b,a); + INSERT INTO t1(a,b) VALUES (1, 22); + SELECT * FROM t1 INDEXED BY idx WHERE (b,a) IN (SELECT b,a FROM t1); +} {1 22 NULL NULL} +do_execsql_test 34.3 { + DROP TABLE t1; + CREATE TABLE t1(a, b, c, d); + CREATE INDEX idx ON t1(b,a,a); + INSERT INTO t1(a,b) VALUES (1, 22); + SELECT * FROM t1 INDEXED BY idx WHERE (b,a) IN (SELECT b,a FROM t1); +} {1 22 NULL NULL} +do_execsql_test 34.4 { + DROP TABLE t1; + CREATE TABLE t1(id INTEGER PRIMARY KEY, a INT); + CREATE INDEX t1a ON t1(a,id); -- index includes PRIMARY KEY + CREATE TABLE t2(id INTEGER PRIMARY KEY); + WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<100) + INSERT INTO t1(id,a) SELECT n, 777 FROM c; + INSERT INTO t2 SELECT id FROM t1; + SELECT * + FROM t1 JOIN t2 USING(id) + WHERE t1.a=777 AND t2.id>999 + ORDER BY t1.id; +} {} +do_execsql_test 34.5 { + EXPLAIN QUERY PLAN + SELECT * + FROM t1 JOIN t2 USING(id) + WHERE t1.a=777 AND t2.id>999 + ORDER BY t1.id; +} {/SEARCH t1 USING COVERING INDEX t1a .a=. AND id>../} + +# 2025-08-04 forum post eab63506cf +# +do_execsql_test 35.0 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(c1 TEXT, c2 INTEGER, PRIMARY KEY(c1, c2)); + INSERT INTO t1(c1, c2) VALUES ('a', 1); + SELECT ('a', 1) IN (SELECT c1, c2 from t1); +} 1 +do_execsql_test 35.1 { + SELECT (1, 'a') IN (SELECT c2, c1 from t1); +} 1 + + +finish_test diff --git a/test/rowvalue2.test b/test/rowvalue2.test new file mode 100644 index 0000000000..1502e31a54 --- /dev/null +++ b/test/rowvalue2.test @@ -0,0 +1,279 @@ +# 2016 June 17 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is testing the SELECT statement. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set ::testprefix rowvalue2 + +do_execsql_test 1.0 { + CREATE TABLE t1(a, b, c); + INSERT INTO t1 VALUES(0, 0, 0); + INSERT INTO t1 VALUES(0, 1, 1); + INSERT INTO t1 VALUES(1, 0, 2); + INSERT INTO t1 VALUES(1, 1, 3); + + CREATE INDEX i1 ON t1(a, b); +} + +do_execsql_test 1.1.1 { SELECT c FROM t1 WHERE (a, b) >= (1, 0) } {2 3} +do_execsql_test 1.1.2 { SELECT c FROM t1 WHERE (a, b) > (1, 0) } {3} + +#------------------------------------------------------------------------- + +do_execsql_test 2.0.1 { + CREATE TABLE t2(a INTEGER, b INTEGER, c INTEGER, d INTEGER); + CREATE INDEX i2 ON t2(a, b, c); +} +do_test 2.0.2 { + foreach a {0 1 2 3} { + foreach b {0 1 2 3} { + foreach c {0 1 2 3} { + execsql { INSERT INTO t2 VALUES($a, $b, $c, $c + $b*4 + $a*16); } + }}} +} {} + +do_execsql_test 2.1 { + SELECT d FROM t2 WHERE (a, b) > (2, 2); +} [db eval { SELECT d FROM t2 WHERE a>2 OR (a=2 AND b>2) }] + +do_execsql_test 2.2 { + SELECT d FROM t2 WHERE (a, b) >= (2, 2); +} [db eval { SELECT d FROM t2 WHERE a>2 OR (a=2 AND b>=2) }] + +do_execsql_test 2.3 { + SELECT d FROM t2 WHERE a=1 AND (b, c) >= (1, 2); +} [db eval { SELECT d FROM t2 WHERE +a=1 AND (b>1 OR (b==1 AND c>=2)) }] + +do_execsql_test 2.4 { + SELECT d FROM t2 WHERE a=1 AND (b, c) > (1, 2); +} [db eval { SELECT d FROM t2 WHERE +a=1 AND (b>1 OR (b==1 AND c>2)) }] + +#------------------------------------------------------------------------- + +set words { +airfare airfield airfields airflow airfoil +airfoils airframe airframes airily airing +airings airless airlift airlifts airline +airliner airlines airlock airlocks airmail +airmails airman airmen airplane airplanes + +arraignment arraignments arraigns arrange arranged +arrangement arrangements arranger arrangers arranges +arranging arrant array arrayed arrays +arrears arrest arrested arrester arresters +arresting arrestingly arrestor arrestors arrests + +edifices edit edited editing edition +editions editor editorial editorially editorials +editors edits educable educate educated +educates educating education educational educationally +educations educator educators eel eelgrass +} + +do_test 3.0 { + execsql { CREATE TABLE t3(a, b, c, w); } + foreach w $words { + set a [string range $w 0 2] + set b [string range $w 3 5] + set c [string range $w 6 end] + execsql { INSERT INTO t3 VALUES($a, $b, $c, $w) } + } +} {} + + +foreach {tn idx} { + IDX1 {} + IDX2 { CREATE INDEX i3 ON t3(a, b, c); } + IDX3 { CREATE INDEX i3 ON t3(a, b); } + IDX4 { CREATE INDEX i3 ON t3(a); } +} { + execsql { DROP INDEX IF EXISTS i3 } + execsql $idx + + foreach w $words { + set a [string range $w 0 2] + set b [string range $w 3 5] + set c [string range $w 6 end] + + foreach op [list > >= < <= == IS] { + do_execsql_test 3.1.$tn.$w.$op [subst -novar { + SELECT rowid FROM t3 WHERE (a, b, c) [set op] ($a, $b, $c) + ORDER BY +rowid + }] [db eval [subst -novar { + SELECT rowid FROM t3 WHERE w [set op] $w ORDER BY +rowid + }]] + + do_execsql_test 3.1.$tn.$w.$op.subselect [subst -novar { + SELECT rowid FROM t3 WHERE (a, b, c) [set op] ( + SELECT a, b, c FROM t3 WHERE w = $w + ) + ORDER BY +rowid + }] [db eval [subst -novar { + SELECT rowid FROM t3 WHERE w [set op] $w ORDER BY +rowid + }]] + } + + } +} + +#------------------------------------------------------------------------- +# + +do_execsql_test 4.0 { + CREATE TABLE t4(a, b, c); + INSERT INTO t4 VALUES(NULL, NULL, NULL); + INSERT INTO t4 VALUES(NULL, NULL, 0); + INSERT INTO t4 VALUES(NULL, NULL, 1); + INSERT INTO t4 VALUES(NULL, 0, NULL); + INSERT INTO t4 VALUES(NULL, 0, 0); + INSERT INTO t4 VALUES(NULL, 0, 1); + INSERT INTO t4 VALUES(NULL, 1, NULL); + INSERT INTO t4 VALUES(NULL, 1, 0); + INSERT INTO t4 VALUES(NULL, 1, 1); + + INSERT INTO t4 VALUES( 0, NULL, NULL); + INSERT INTO t4 VALUES( 0, NULL, 0); + INSERT INTO t4 VALUES( 0, NULL, 1); + INSERT INTO t4 VALUES( 0, 0, NULL); + INSERT INTO t4 VALUES( 0, 0, 0); + INSERT INTO t4 VALUES( 0, 0, 1); + INSERT INTO t4 VALUES( 0, 1, NULL); + INSERT INTO t4 VALUES( 0, 1, 0); + INSERT INTO t4 VALUES( 0, 1, 1); + + INSERT INTO t4 VALUES( 1, NULL, NULL); + INSERT INTO t4 VALUES( 1, NULL, 0); + INSERT INTO t4 VALUES( 1, NULL, 1); + INSERT INTO t4 VALUES( 1, 0, NULL); + INSERT INTO t4 VALUES( 1, 0, 0); + INSERT INTO t4 VALUES( 1, 0, 1); + INSERT INTO t4 VALUES( 1, 1, NULL); + INSERT INTO t4 VALUES( 1, 1, 0); + INSERT INTO t4 VALUES( 1, 1, 1); +} + +proc make_expr1 {cList vList op} { + return "([join $cList ,]) $op ([join $vList ,])" +} + +proc make_expr3 {cList vList op} { + set n [llength $cList] + + set aList [list] + foreach c [lrange $cList 0 end-1] v [lrange $vList 0 end-1] { + lappend aList "$c == $v" + } + lappend aList "[lindex $cList end] $op [lindex $vList end]" + + return "([join $aList { AND }])" +} + +proc make_expr2 {cList vList op} { + set ret "" + + switch -- $op { + == - IS { + set aList [list] + foreach c $cList v $vList { lappend aList "($c $op $v)" } + set ret [join $aList " AND "] + } + + < - > { + set oList [list] + for {set i 0} {$i < [llength $cList]} {incr i} { + lappend oList [make_expr3 [lrange $cList 0 $i] [lrange $vList 0 $i] $op] + } + set ret [join $oList " OR "] + } + + <= - >= { + set o2 [string range $op 0 0] + set oList [list] + for {set i 0} {$i < [llength $cList]-1} {incr i} { + lappend oList [make_expr3 [lrange $cList 0 $i] [lrange $vList 0 $i] $o2] + } + lappend oList [make_expr3 $cList $vList $op] + set ret [join $oList " OR "] + } + + + default { + error "Unknown op: $op" + } + } + + set ret +} + +foreach {tn idx} { + IDX1 {} + IDX2 { CREATE INDEX i4 ON t4(a, b, c); } + IDX3 { CREATE INDEX i4 ON t4(a, b); } + IDX4 { CREATE INDEX i4 ON t4(a); } +} { + execsql { DROP INDEX IF EXISTS i4 } + execsql $idx + + foreach {tn2 vector} { + 1 {0 0 0} + 2 {1 1 1} + 3 {0 0 NULL} + 4 {0 NULL 0} + 5 {NULL 0 0} + 6 {1 1 NULL} + 7 {1 NULL 1} + 8 {NULL 1 1} + } { + foreach op { IS == < <= > >= } { + set e1 [make_expr1 {a b c} $vector $op] + set e2 [make_expr2 {a b c} $vector $op] + + do_execsql_test 4.$tn.$tn2.$op \ + "SELECT rowid FROM t4 WHERE $e2 ORDER BY +rowid" [ + db eval "SELECT rowid FROM t4 WHERE $e1 ORDER BY +rowid" + ] + } + } +} + +do_execsql_test 5.0 { + CREATE TABLE r1(a TEXT, iB TEXT); + CREATE TABLE r2(x TEXT, zY INTEGER); + CREATE INDEX r1ab ON r1(a, iB); + + INSERT INTO r1 VALUES(35, 35); + INSERT INTO r2 VALUES(35, 36); + INSERT INTO r2 VALUES(35, 4); + INSERT INTO r2 VALUES(35, 35); +} {} + +foreach {tn lhs rhs} { + 1 {x +zY} {a iB} + 2 {x zY} {a iB} + 3 {x zY} {a +iB} + 4 {+x zY} {a iB} + 5 {x zY} {+a iB} +} { + foreach op { IS == < <= > >= } { + set e1 [make_expr1 $lhs $rhs $op] + set e2 [make_expr2 $lhs $rhs $op] + do_execsql_test 5.$tn.$op \ + "SELECT * FROM r1, r2 WHERE $e2 ORDER BY iB" [db eval \ + "SELECT * FROM r1, r2 WHERE $e1 ORDER BY iB" + ] + } +} + + +finish_test diff --git a/test/rowvalue3.test b/test/rowvalue3.test new file mode 100644 index 0000000000..7d07976948 --- /dev/null +++ b/test/rowvalue3.test @@ -0,0 +1,221 @@ +# 2016 June 17 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is testing "(...) IN (SELECT ...)" expressions +# where the SELECT statement returns more than one column. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set ::testprefix rowvalue3 + +do_execsql_test 1.0 { + CREATE TABLE t1(a, b, c); + CREATE INDEX i1 ON t1(a, b); + INSERT INTO t1 VALUES(1, 2, 3); + INSERT INTO t1 VALUES(4, 5, 6); + INSERT INTO t1 VALUES(7, 8, 9); +} + +foreach {tn sql res} { + 1 "SELECT 1 WHERE (4, 5) IN (SELECT a, b FROM t1)" 1 + 2 "SELECT 1 WHERE (5, 5) IN (SELECT a, b FROM t1)" {} + 3 "SELECT 1 WHERE (5, 4) IN (SELECT a, b FROM t1)" {} + 4 "SELECT 1 WHERE (5, 4) IN (SELECT b, a FROM t1)" 1 + 5 "SELECT 1 WHERE (SELECT a, b FROM t1 WHERE c=6) IN (SELECT a, b FROM t1)" 1 + 6 "SELECT (5, 4) IN (SELECT a, b FROM t1)" 0 + 7 "SELECT 1 WHERE (5, 4) IN (SELECT +b, +a FROM t1)" 1 + 8 "SELECT (5, 4) IN (SELECT +b, +a FROM t1)" 1 + 9 "SELECT (1, 2) IN (SELECT rowid, b FROM t1)" 1 + 10 "SELECT 1 WHERE (1, 2) IN (SELECT rowid, b FROM t1)" 1 + 11 "SELECT 1 WHERE (1, NULL) IN (SELECT rowid, b FROM t1)" {} + 12 "SELECT 1 FROM t1 WHERE (a, b) = (SELECT +a, +b FROM t1)" {1} +} { + do_execsql_test 1.$tn $sql $res +} + +#------------------------------------------------------------------------- + +do_execsql_test 2.0 { + CREATE TABLE z1(x, y, z); + CREATE TABLE kk(a, b); + + INSERT INTO z1 VALUES('a', 'b', 'c'); + INSERT INTO z1 VALUES('d', 'e', 'f'); + INSERT INTO z1 VALUES('g', 'h', 'i'); + + -- INSERT INTO kk VALUES('y', 'y'); + INSERT INTO kk VALUES('d', 'e'); + -- INSERT INTO kk VALUES('x', 'x'); + +} + +foreach {tn idx} { + 1 { } + 2 { CREATE INDEX z1idx ON z1(x, y) } + 3 { CREATE UNIQUE INDEX z1idx ON z1(x, y) } + 4 { CREATE INDEX z1idx ON kk(a, b) } +} { + execsql "DROP INDEX IF EXISTS z1idx" + execsql $idx + + do_execsql_test 2.$tn.1 { + SELECT * FROM z1 WHERE x IN (SELECT a FROM kk) + } {d e f} + + do_execsql_test 2.$tn.2 { + SELECT * FROM z1 WHERE (x,y) IN (SELECT a, b FROM kk) + } {d e f} + + do_execsql_test 2.$tn.3 { + SELECT * FROM z1 WHERE (x, +y) IN (SELECT a, b FROM kk) + } {d e f} + + do_execsql_test 2.$tn.4 { + SELECT * FROM z1 WHERE (x, +y) IN (SELECT a, b||'x' FROM kk) + } {} + + do_execsql_test 2.$tn.5 { + SELECT * FROM z1 WHERE (+x, y) IN (SELECT a, b FROM kk) + } {d e f} +} + +#------------------------------------------------------------------------- +# + +do_execsql_test 3.0 { + CREATE TABLE c1(a, b, c, d); + INSERT INTO c1(rowid, a, b) VALUES(1, NULL, 1); + INSERT INTO c1(rowid, a, b) VALUES(2, 2, NULL); + INSERT INTO c1(rowid, a, b) VALUES(3, 2, 2); + INSERT INTO c1(rowid, a, b) VALUES(4, 3, 3); + + INSERT INTO c1(rowid, a, b, c, d) VALUES(101, 'a', 'b', 1, 1); + INSERT INTO c1(rowid, a, b, c, d) VALUES(102, 'a', 'b', 1, 2); + INSERT INTO c1(rowid, a, b, c, d) VALUES(103, 'a', 'b', 1, 3); + INSERT INTO c1(rowid, a, b, c, d) VALUES(104, 'a', 'b', 2, 1); + INSERT INTO c1(rowid, a, b, c, d) VALUES(105, 'a', 'b', 2, 2); + INSERT INTO c1(rowid, a, b, c, d) VALUES(106, 'a', 'b', 2, 3); + INSERT INTO c1(rowid, a, b, c, d) VALUES(107, 'a', 'b', 3, 1); + INSERT INTO c1(rowid, a, b, c, d) VALUES(108, 'a', 'b', 3, 2); + INSERT INTO c1(rowid, a, b, c, d) VALUES(109, 'a', 'b', 3, 3); +} + + +foreach {tn idx} { + 1 { } + 2 { CREATE INDEX c1ab ON c1(a, b); } + 3 { CREATE INDEX c1ba ON c1(b, a); } + + 4 { CREATE INDEX c1cd ON c1(c, d); } + 5 { CREATE INDEX c1dc ON c1(d, c); } +} { + drop_all_indexes + + foreach {tn2 sql res} { + 1 "SELECT (1, 2) IN (SELECT a, b FROM c1)" {0} + 2 "SELECT (1, 1) IN (SELECT a, b FROM c1)" {{}} + 3 "SELECT (2, 1) IN (SELECT a, b FROM c1)" {{}} + 4 "SELECT (2, 2) IN (SELECT a, b FROM c1)" {1} + 5 "SELECT c, d FROM c1 WHERE (c, d) IN (SELECT d, c FROM c1)" + { 1 1 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3 } + + 6 "SELECT c, d FROM c1 WHERE (c,d) IN (SELECT d, c FROM c1) ORDER BY c DESC" + { 3 1 3 2 3 3 2 1 2 2 2 3 1 1 1 2 1 3 } + + 7 { + SELECT c, d FROM c1 WHERE (c,d) IN (SELECT d, c FROM c1) + ORDER BY c DESC, d ASC + } { 3 1 3 2 3 3 2 1 2 2 2 3 1 1 1 2 1 3 } + + 8 { + SELECT c, d FROM c1 WHERE (c,d) IN (SELECT d, c FROM c1) + ORDER BY c ASC, d DESC + } { 1 3 1 2 1 1 2 3 2 2 2 1 3 3 3 2 3 1 } + + 9 { + SELECT c, d FROM c1 WHERE (c,d) IN (SELECT d, c FROM c1) + ORDER BY c ASC, d ASC + } { 1 1 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3 } + 10 { + SELECT c, d FROM c1 WHERE (c,d) IN (SELECT d, c FROM c1) + ORDER BY c DESC, d DESC + } { 3 3 3 2 3 1 2 3 2 2 2 1 1 3 1 2 1 1 } + + } { + do_execsql_test 3.$tn.$tn2 $sql $res + } +} + +#------------------------------------------------------------------------- + +do_execsql_test 4.0 { + CREATE TABLE hh(a, b, c); + + INSERT INTO hh VALUES('a', 'a', 1); + INSERT INTO hh VALUES('a', 'b', 2); + INSERT INTO hh VALUES('b', 'a', 3); + INSERT INTO hh VALUES('b', 'b', 4); + + CREATE TABLE k1(x, y); + INSERT INTO k1 VALUES('a', 'a'); + INSERT INTO k1 VALUES('b', 'b'); + INSERT INTO k1 VALUES('a', 'b'); + INSERT INTO k1 VALUES('b', 'a'); +} + +foreach {tn idx} { + 1 { } + 2 { CREATE INDEX h1 ON hh(a, b); } + 3 { CREATE UNIQUE INDEX k1idx ON k1(x, y) } + 4 { CREATE UNIQUE INDEX k1idx ON k1(x, y DESC) } + 5 { + CREATE INDEX h1 ON hh(a, b); + CREATE UNIQUE INDEX k1idx ON k1(x, y); + } + 6 { + CREATE INDEX h1 ON hh(a, b); + CREATE UNIQUE INDEX k1idx ON k1(x, y DESC); + } +} { + drop_all_indexes + execsql $idx + foreach {tn2 orderby res} { + 1 "a ASC, b ASC" {1 2 3 4} + 2 "a ASC, b DESC" {2 1 4 3} + 3 "a DESC, b ASC" {3 4 1 2} + 4 "a DESC, b DESC" {4 3 2 1} + } { + do_execsql_test 4.$tn.$tn2 " + SELECT c FROM hh WHERE (a, b) in (SELECT x, y FROM k1) ORDER BY $orderby + " $res + } +} + +#------------------------------------------------------------------------- + +# 2016-11-17. Query flattening in a vector SELECT on the RHS of an IN +# operator. Ticket https://sqlite.org/src/info/da7841375186386c +# +do_execsql_test 5.0 { + DROP TABLE IF EXISTS t1; + DROP TABLE IF EXISTS t2; + CREATE TABLE T1(a TEXT); + INSERT INTO T1(a) VALUES ('aaa'); + CREATE TABLE T2(a TEXT PRIMARY KEY,n INT); + INSERT INTO T2(a, n) VALUES('aaa',0); + SELECT * FROM T2 + WHERE (a,n) IN (SELECT T1.a, V.n + FROM T1, (SELECT * FROM (SELECT 0 n) T3) V); +} {aaa 0} + + +finish_test diff --git a/test/rowvalue4.test b/test/rowvalue4.test new file mode 100644 index 0000000000..1ef5fc2920 --- /dev/null +++ b/test/rowvalue4.test @@ -0,0 +1,307 @@ +# 2016 July 29 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is syntax errors involving row-value constructors +# and sub-selects that return multiple arguments. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set ::testprefix rowvalue4 + +#------------------------------------------------------------------------- +# Test some error conditions: +# +# * row values used where they are not supported, +# * row values or sub-selects that contain/return the wrong number +# of elements. +# +do_execsql_test 1.0 { + CREATE TABLE t1(a, b, c); + CREATE INDEX t1bac ON t1(b, a, c); +} + +foreach {tn e} { + 1 "(1, 2, 3)" + 2 "1 + (1, 2)" + 3 "(1,2,3) == (1, 2)" +} { + do_catchsql_test 1.$tn "SELECT $e" {1 {row value misused}} +} + +foreach {tn s error} { + 1 "SELECT * FROM t1 WHERE a = (1, 2)" {row value misused} + 2 "SELECT * FROM t1 WHERE b = (1, 2)" {row value misused} + 3 "SELECT * FROM t1 WHERE NOT (b = (1, 2))" {row value misused} + 4 "SELECT * FROM t1 LIMIT (1, 2)" {row value misused} + 5 "SELECT (a, b) IN (SELECT * FROM t1) FROM t1" + {sub-select returns 3 columns - expected 2} + + 6 "SELECT * FROM t1 WHERE (a, b) IN (SELECT * FROM t1)" + {sub-select returns 3 columns - expected 2} + 7 "SELECT * FROM t1 WHERE (c, c) <= 1" {row value misused} + 8 "SELECT * FROM t1 WHERE (b, b) <= 1" {row value misused} +} { + do_catchsql_test 2.$tn "$s" [list 1 $error] +} + +#------------------------------------------------------------------------- +do_execsql_test 2.0 { + CREATE TABLE t2(a, b, c, d); + INSERT INTO t2 VALUES(1, 1, 1, 1); + INSERT INTO t2 VALUES(1, 1, 2, 2); + INSERT INTO t2 VALUES(1, 1, 3, 3); + INSERT INTO t2 VALUES(1, 2, 1, 4); + INSERT INTO t2 VALUES(1, 2, 2, 5); + INSERT INTO t2 VALUES(1, 2, 3, 6); + INSERT INTO t2 VALUES(1, 3, 1, 7); + INSERT INTO t2 VALUES(1, 3, 2, 8); + INSERT INTO t2 VALUES(1, 3, 3, 9); + + INSERT INTO t2 VALUES(2, 1, 1, 10); + INSERT INTO t2 VALUES(2, 1, 2, 11); + INSERT INTO t2 VALUES(2, 1, 3, 12); + INSERT INTO t2 VALUES(2, 2, 1, 13); + INSERT INTO t2 VALUES(2, 2, 2, 14); + INSERT INTO t2 VALUES(2, 2, 3, 15); + INSERT INTO t2 VALUES(2, 3, 1, 16); + INSERT INTO t2 VALUES(2, 3, 2, 17); + INSERT INTO t2 VALUES(2, 3, 3, 18); + + INSERT INTO t2 VALUES(3, 1, 1, 19); + INSERT INTO t2 VALUES(3, 1, 2, 20); + INSERT INTO t2 VALUES(3, 1, 3, 21); + INSERT INTO t2 VALUES(3, 2, 1, 22); + INSERT INTO t2 VALUES(3, 2, 2, 23); + INSERT INTO t2 VALUES(3, 2, 3, 24); + INSERT INTO t2 VALUES(3, 3, 1, 25); + INSERT INTO t2 VALUES(3, 3, 2, 26); + INSERT INTO t2 VALUES(3, 3, 3, 27); +} + +foreach {nm idx} { + idx1 {} + idx2 { CREATE INDEX t2abc ON t2(a, b, c); } + idx3 { CREATE INDEX t2abc ON t2(a, b DESC, c); } + idx4 { CREATE INDEX t2abc ON t2(a DESC, b DESC, c DESC); } + idx5 { CREATE INDEX t2abc ON t2(a ASC, b ASC, c ASC); } + idx6 { CREATE INDEX t2abc ON t2(a DESC, b, c); } + idx7 { CREATE INDEX t2abc ON t2(a DESC, b DESC) } + idx8 { CREATE INDEX t2abc ON t2(c, b, a); } + idx9 { CREATE INDEX t2d ON t2(d); } + idx10 { CREATE INDEX t2abc ON t2(a DESC, b, c DESC); } +} { + drop_all_indexes + execsql $idx + + foreach {tn where res} { + 1 "(a, b, c) < (2, 2, 2)" {1 2 3 4 5 6 7 8 9 10 11 12 13} + 2 "(a, b, c) <= (2, 2, 2)" {1 2 3 4 5 6 7 8 9 10 11 12 13 14} + 3 "(a, b, c) > (2, 2, 2)" {15 16 17 18 19 20 21 22 23 24 25 26 27} + 4 "(a, b, c) >= (2, 2, 2)" {14 15 16 17 18 19 20 21 22 23 24 25 26 27} + 5 "(a, b, c) >= (2, 2, NULL)" {16 17 18 19 20 21 22 23 24 25 26 27} + 6 "(a, b, c) <= (2, 2, NULL)" {1 2 3 4 5 6 7 8 9 10 11 12} + 7 "(a, b, c) >= (2, NULL, NULL)" {19 20 21 22 23 24 25 26 27} + 8 "(a, b, c) <= (2, NULL, NULL)" {1 2 3 4 5 6 7 8 9} + + 9 "(a, b, c) < (SELECT a, b, c FROM t2 WHERE d=14)" + {1 2 3 4 5 6 7 8 9 10 11 12 13} + + 10 "(a, b, c) = (SELECT a, b, c FROM t2 WHERE d=14)" 14 + + 11 "a = 2 AND (b, c) > (2, 2)" {15 16 17 18} + 12 "a = 2 AND (b, c) < (3, 3) AND (b, c) > (1, 1)" {11 12 13 14 15 16 17} + } { + set result [db eval "SELECT d FROM t2 WHERE $where"] + do_test 2.1.$nm.$tn { lsort -integer $result } $res + } + + foreach {tn e res} { + 1 "(2, 1) IN (SELECT a, b FROM t2)" 1 + 2 "(2, 1) IN (SELECT a, b FROM t2 ORDER BY d)" 1 + 3 "(2, 1) IN (SELECT a, b FROM t2 ORDER BY d LIMIT 9)" 0 + 4 "(2, 1) IN (SELECT a, b FROM t2 ORDER BY d LIMIT 10)" 1 + + 5 "(3, 3) = (SELECT a, b FROM t2 ORDER BY d DESC LIMIT 1)" 1 + 6 "(3, 3) = (SELECT a, b FROM t2 ORDER BY d ASC LIMIT 1)" 0 + 7 "(1, NULL) = (SELECT a, b FROM t2 ORDER BY d ASC LIMIT 1)" {{}} + + 8 "(3, 1) = (SELECT b, c FROM t2 ORDER BY d DESC LIMIT 1 OFFSET 2)" 1 + 9 "(3, 1) = (SELECT b, c FROM t2 ORDER BY d ASC LIMIT 1 OFFSET 2)" 0 + 10 "(1, NULL) = (SELECT b, c FROM t2 ORDER BY d ASC LIMIT 1 OFFSET 2)" {{}} + + 11 "(3, 3) = (SELECT max(a), max(b) FROM t2)" 1 + 12 "(3, 1) = (SELECT max(a), min(b) FROM t2)" 1 + 13 "(NULL, NULL) = (SELECT max(a), min(b) FROM t2)" {{}} + + 14 "(2, 1) IN (SELECT a, b FROM t2 ORDER BY d LIMIT 5 OFFSET 11)" 1 + 15 "(2, 1) IN (SELECT a, b FROM t2 ORDER BY d LIMIT 5 OFFSET 12)" 0 + } { + do_execsql_test 2.2.$nm.$tn "SELECT $e" $res + } +} + +ifcapable stat4 { + do_execsql_test 3.0 { + CREATE TABLE c1(a, b, c, d); + INSERT INTO c1(a, b) VALUES(1, 'a'); + INSERT INTO c1(a, b) VALUES(1, 'b'); + INSERT INTO c1(a, b) VALUES(1, 'c'); + INSERT INTO c1(a, b) VALUES(1, 'd'); + INSERT INTO c1(a, b) VALUES(1, 'e'); + INSERT INTO c1(a, b) VALUES(1, 'f'); + INSERT INTO c1(a, b) VALUES(1, 'g'); + INSERT INTO c1(a, b) VALUES(1, 'h'); + INSERT INTO c1(a, b) VALUES(1, 'i'); + INSERT INTO c1(a, b) VALUES(1, 'j'); + INSERT INTO c1(a, b) VALUES(1, 'k'); + INSERT INTO c1(a, b) VALUES(1, 'l'); + INSERT INTO c1(a, b) VALUES(1, 'm'); + INSERT INTO c1(a, b) VALUES(1, 'n'); + INSERT INTO c1(a, b) VALUES(1, 'o'); + INSERT INTO c1(a, b) VALUES(1, 'p'); + INSERT INTO c1(a, b) VALUES(2, 'a'); + INSERT INTO c1(a, b) VALUES(2, 'b'); + INSERT INTO c1(a, b) VALUES(2, 'c'); + INSERT INTO c1(a, b) VALUES(2, 'd'); + INSERT INTO c1(a, b) VALUES(2, 'e'); + INSERT INTO c1(a, b) VALUES(2, 'f'); + INSERT INTO c1(a, b) VALUES(2, 'g'); + INSERT INTO c1(a, b) VALUES(2, 'h'); + + INSERT INTO c1(c, d) SELECT a, b FROM c1; + + CREATE INDEX c1ab ON c1(a, b); + CREATE INDEX c1cd ON c1(c, d); + ANALYZE; + } + + do_eqp_test 3.1.1 { SELECT * FROM c1 WHERE a=1 AND c=2 } \ + {SEARCH c1 USING INDEX c1cd (c=?)} + + do_eqp_test 3.1.2 { SELECT * FROM c1 WHERE a=1 AND b>'d' AND c=2 } \ + {SEARCH c1 USING INDEX c1cd (c=?)} + + do_eqp_test 3.1.3 { SELECT * FROM c1 WHERE a=1 AND b>'l' AND c=2 } \ + {SEARCH c1 USING INDEX c1ab (a=? AND b>?)} + + do_eqp_test 3.2.1 { SELECT * FROM c1 WHERE a=1 AND c>1 } \ + {SEARCH c1 USING INDEX c1cd (c>?)} + + do_eqp_test 3.2.2 { SELECT * FROM c1 WHERE a=1 AND c>0 } \ + {SEARCH c1 USING INDEX c1ab (a=?)} + + do_eqp_test 3.2.3 { SELECT * FROM c1 WHERE a=1 AND c>=1 } \ + {SEARCH c1 USING INDEX c1ab (a=?)} + + do_eqp_test 3.2.4 { SELECT * FROM c1 WHERE a=1 AND (c, d)>(1, 'c') } \ + {SEARCH c1 USING INDEX c1ab (a=?)} + + do_eqp_test 3.2.5 { SELECT * FROM c1 WHERE a=1 AND (c, d)>(1, 'o') } \ + {SEARCH c1 USING INDEX c1cd ((c,d)>(?,?))} + + do_eqp_test 3.2.6 { SELECT * FROM c1 WHERE a=1 AND (c, +b)>(1, 'c') } \ + {SEARCH c1 USING INDEX c1ab (a=?)} + +} + +#------------------------------------------------------------------------ + +do_execsql_test 5.0 { + CREATE TABLE d1(x, y); + CREATE TABLE d2(a, b, c); + CREATE INDEX d2ab ON d2(a, b); + CREATE INDEX d2c ON d2(c); + + WITH i(i) AS ( + VALUES(1) UNION ALL SELECT i+1 FROM i WHERE i<1000 + ) + INSERT INTO d2 SELECT i/100, i%100, i/100 FROM i; + ANALYZE; +} + +do_eqp_test 5.1 { + SELECT * FROM d2 WHERE + (a, b) IN (SELECT x, y FROM d1) AND + (c) IN (SELECT y FROM d1) +} { + QUERY PLAN + |--SEARCH d2 USING INDEX d2ab (a=? AND b=?) + |--LIST SUBQUERY xxxxxx + | |--SCAN d1 + | `--CREATE BLOOM FILTER + `--LIST SUBQUERY xxxxxx + |--SCAN d1 + `--CREATE BLOOM FILTER +} + +do_execsql_test 6.0 { + CREATE TABLE e1(a, b, c, d, e); + CREATE INDEX e1ab ON e1(a, b); + CREATE INDEX e1cde ON e1(c, d, e); +} + +do_eqp_test 6.1 { + SELECT * FROM e1 WHERE (a, b) > (?, ?) +} {SEARCH e1 USING INDEX e1ab ((a,b)>(?,?))} + +do_eqp_test 6.2 { + SELECT * FROM e1 WHERE (a, b) < (?, ?) +} {SEARCH e1 USING INDEX e1ab ((a,b)<(?,?))} + +do_eqp_test 6.3 { + SELECT * FROM e1 WHERE c = ? AND (d, e) > (?, ?) +} {SEARCH e1 USING INDEX e1cde (c=? AND (d,e)>(?,?))} + +do_eqp_test 6.4 { + SELECT * FROM e1 WHERE c = ? AND (d, e) < (?, ?) +} {SEARCH e1 USING INDEX e1cde (c=? AND (d,e)<(?,?))} + +do_eqp_test 6.5 { + SELECT * FROM e1 WHERE (d, e) BETWEEN (?, ?) AND (?, ?) AND c = ? +} {SEARCH e1 USING INDEX e1cde (c=? AND (d,e)>(?,?) AND (d,e)<(?,?))} + +#------------------------------------------------------------------------- + +do_execsql_test 7.1 { + CREATE TABLE f1(a, b, c); + CREATE INDEX f1ab ON f1(a, b); +} + +do_catchsql_test 7.2 { + SELECT (a COLLATE nocase, b) IN (SELECT a, b FROM f1) FROM f1; +} {0 {}} + +do_catchsql_test 7.3 { + SELECT (a COLLATE nose, b) IN (SELECT a, b FROM f1) FROM f1; +} {1 {no such collation sequence: nose}} + +do_catchsql_test 7.4 { + SELECT * FROM f1 WHERE (?, ? COLLATE nose) > (a, b); +} {1 {no such collation sequence: nose}} + +#------------------------------------------------------------------------- +drop_all_tables +do_execsql_test 8.1 { + CREATE TABLE c1(x, y); + CREATE TABLE c2(a, b, c); + CREATE INDEX c2ab ON c2(a, b); + CREATE INDEX c2c ON c2(c); + + CREATE TABLE c3(d); +} +do_catchsql_test 8.2 { + SELECT * FROM c2 CROSS JOIN c3 WHERE + ( (a, b) == (SELECT x, y FROM c1) AND c3.d = c ) OR + ( c == (SELECT x, y FROM c1) AND c3.d = c ) +} {1 {row value misused}} + +finish_test diff --git a/test/rowvalue5.test b/test/rowvalue5.test new file mode 100644 index 0000000000..b91dfa49ac --- /dev/null +++ b/test/rowvalue5.test @@ -0,0 +1,117 @@ +# 2016 July 29 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is syntax errors involving row-values and +# virtual tables. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set ::testprefix rowvalue5 + +ifcapable !vtab { + finish_test + return +} + +proc vtab_command {method args} { + switch -- $method { + xConnect { + return "CREATE TABLE t1(a, b, c, d, expr)" + } + + xBestIndex { + set COL(0) a + set COL(1) b + set COL(2) c + set COL(3) d + set COL(4) expr + + set OP(eq) = + set OP(ne) != + set OP(gt) > + set OP(le) <= + set OP(lt) < + set OP(ge) >= + set OP(match) MATCH + set OP(like) LIKE + set OP(glob) GLOB + set OP(regexp) REGEXP + + set hdl [lindex $args 0] + set clist [$hdl constraints] + + set ret [list] + set elist [list] + set i 0 + foreach c $clist { + array set C $c + if {$C(usable)} { + lappend ret omit $i + lappend elist "$COL($C(column)) $OP($C(op)) %$i%" + } + incr i + } + + lappend ret idxstr [join $elist " AND "] + #puts "xBestIndex: $ret" + return $ret + } + + xFilter { + foreach {idxnum idxstr arglist} $args {} + set i 0 + set ee $idxstr + foreach a $arglist { + if {[string is double $a]==0} { + set a "'[string map {' ''} $a]'" + } + set ee [string map [list "%$i%" $a] $ee] + incr i + } + set ee [string map [list "'" "''"] $ee] + + set ret [list sql "SELECT 1, 'a', 'b', 'c', 'd', '$ee'"] + #puts "xFilter: $ret" + return $ret + } + } + + return {} +} + +register_tcl_module db +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE x1 USING tcl(vtab_command); +} {} + + +foreach {tn where res} { + 1 "1" {{}} + 2 "a=1" {{a = 1}} + 3 "a=1 AND 4 = b" {{a = 1 AND b = 4}} + 4 "c>'hello'" {{c > 'hello'}} + 5 "c<='hel''lo'" {{c <= 'hel''lo'}} + 6 "(a, b) = (SELECT 9, 10)" {{a = 9 AND b = 10}} + 7 "(+a, b) = (SELECT 'a', 'b')" {{b = 'b'}} + 8 "(a, +b) = (SELECT 'a', 'b')" {{a = 'a'}} + 11 "(+a, b) IN (SELECT 'a', 'b')" {{b = 'b'}} + 12 "(a, +b) IN (SELECT 'a', 'b')" {{a = 'a'}} + + 13 "(a, b) < ('d', 'e')" {{a <= 'd'}} + 14 "(a, b) < ('a', 'c')" {{a <= 'a'}} + 15 "(a, b) <= ('a', 'b')" {{a <= 'a'}} + 16 "(a, b) < ('a', 'b')" {} +} { + do_execsql_test 1.$tn "SELECT expr FROM x1 WHERE $where" $res +} + +finish_test diff --git a/test/rowvalue6.test b/test/rowvalue6.test new file mode 100644 index 0000000000..d90e61c811 --- /dev/null +++ b/test/rowvalue6.test @@ -0,0 +1,36 @@ +# 2016-08-18 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# The focus of this file is handling of NULL values in row-value IN +# expressions. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set ::testprefix rowvalue6 + +do_execsql_test 1.1 { + CREATE TABLE t1(a,b,c); + CREATE INDEX t1x1 ON t1(a,b); + INSERT INTO t1 VALUES(1,NULL,200); + + CREATE TABLE t2(x,y,z); + INSERT INTO t2 VALUES(1,NULL,55); + + SELECT c FROM t1 WHERE (a,b) IN (SELECT x,y FROM t2 WHERE z==55); +} {} +do_execsql_test 1.2 { + INSERT INTO t1 VALUES(2,3,400); + INSERT INTO t2 VALUES(2,3,55); + + SELECT c FROM t1 WHERE (a,b) IN (SELECT x,y FROM t2 WHERE z==55); +} {400} + +finish_test diff --git a/test/rowvalue7.test b/test/rowvalue7.test new file mode 100644 index 0000000000..2823667e73 --- /dev/null +++ b/test/rowvalue7.test @@ -0,0 +1,68 @@ +# 2016-08-18 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# The focus of this file is vector assignments in the SET clause of +# an UPDATE statement. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set ::testprefix rowvalue7 + +do_execsql_test 1.1 { + CREATE TABLE t1(a,b,c,d); + CREATE INDEX t1x ON t1(a,b); + INSERT INTO t1(a,b,c,d) VALUES(1,2,0,0),(3,4,0,0),(5,6,0,0); + CREATE TABLE t2(w,x,y,z); + CREATE INDEX t2x ON t2(w,x); + INSERT INTO t2(w,x,y,z) VALUES(1,2,11,22),(8,9,88,99),(3,5,33,55),(5,6,55,66); + + SELECT *,'|' FROM t1 ORDER BY a; +} {1 2 0 0 | 3 4 0 0 | 5 6 0 0 |} + +do_execsql_test 1.2 { + UPDATE t1 SET (c,d) = (SELECT y,z FROM t2 WHERE (w,x)=(a,b)); + SELECT *,'|' FROM t1 ORDER BY a; +} {1 2 11 22 | 3 4 {} {} | 5 6 55 66 |} + +do_execsql_test 1.3 { + UPDATE t1 SET (c,d) = (SELECT y,z FROM t2 WHERE w=a); + SELECT *,'|' FROM t1 ORDER BY a; +} {1 2 11 22 | 3 4 33 55 | 5 6 55 66 |} + +do_execsql_test 1.4 { + UPDATE t1 SET (c) = 99 WHERE a=3; + SELECT *,'|' FROM t1 ORDER BY a; +} {1 2 11 22 | 3 4 99 55 | 5 6 55 66 |} + +do_execsql_test 1.5 { + UPDATE t1 SET b = 8, (c,d) = (SELECT 123,456) WHERE a=3; + SELECT *,'|' FROM t1 ORDER BY a; +} {1 2 11 22 | 3 8 123 456 | 5 6 55 66 |} + +do_catchsql_test 2.1 { + UPDATE t1 SET (c,d) = (SELECT x,y,z FROM t2 WHERE w=a); +} {1 {2 columns assigned 3 values}} + +do_catchsql_test 2.2 { + UPDATE t1 SET (b,c,d) = (SELECT x,y FROM t2 WHERE w=a); +} {1 {3 columns assigned 2 values}} + +# 2019-08-26 +# ticket https://sqlite.org/src/info/78acc9d40f0786e8 +# +do_catchsql_test 3.0 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a,b); + INSERT INTO t1 VALUES(1,2); + UPDATE t1 SET (a,a,a,b)=(SELECT 99,100); +} {1 {4 columns assigned 2 values}} + +finish_test diff --git a/test/rowvalue8.test b/test/rowvalue8.test new file mode 100644 index 0000000000..432dad1278 --- /dev/null +++ b/test/rowvalue8.test @@ -0,0 +1,59 @@ +# 2016-08-22 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# Use of row values in CASE statements. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set ::testprefix rowvalue8 + +do_execsql_test 1.1 { + CREATE TABLE t1(a INTEGER PRIMARY KEY,b,c,d); + INSERT INTO t1(a,b,c,d) VALUES + (1,1,2,3), + (2,2,3,4), + (3,1,2,4), + (4,2,3,5), + (5,3,4,6), + (6,4,5,9); + SELECT a, CASE (b,c) WHEN (1,2) THEN 'aleph' + WHEN (2,3) THEN 'bet' + WHEN (3,4) THEN 'gimel' + ELSE '-' END, + '|' + FROM t1 + ORDER BY a; +} {1 aleph | 2 bet | 3 aleph | 4 bet | 5 gimel | 6 - |} +do_execsql_test 1.2 { + SELECT a, CASE (b,c,d) WHEN (1,2,3) THEN 'aleph' + WHEN (2,3,4) THEN 'bet' + WHEN (3,4,6) THEN 'gimel' + ELSE '-' END, + '|' + FROM t1 + ORDER BY a; +} {1 aleph | 2 bet | 3 - | 4 - | 5 gimel | 6 - |} + +do_execsql_test 2.1 { + CREATE TABLE t2(x INTEGER PRIMARY KEY, y); + INSERT INTO t2(x,y) VALUES(1,6),(2,5),(3,4),(4,3),(5,2),(6,1); + SELECT x, CASE (SELECT b,c FROM t1 WHERE a=y) + WHEN (1,2) THEN 'aleph' + WHEN (2,3) THEN 'bet' + WHEN (3,4) THEN 'gimel' + ELSE '-' END, + '|' + FROM t2 + ORDER BY +x; +} {1 - | 2 gimel | 3 bet | 4 aleph | 5 bet | 6 aleph |} + + +finish_test diff --git a/test/rowvalue9.test b/test/rowvalue9.test new file mode 100644 index 0000000000..baa13f4f94 --- /dev/null +++ b/test/rowvalue9.test @@ -0,0 +1,352 @@ +# 2016 September 3 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is testing SQL statements that use row value +# constructors. +# + + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set ::testprefix rowvalue9 + +# Tests: +# +# 1.*: Test that affinities are handled correctly by various row-value +# operations without indexes. +# +# 2.*: Test an affinity bug that came up during testing. +# +# 3.*: Test a row-value version of the bug tested by 2.*. +# +# 4.*: Test that affinities are handled correctly by various row-value +# operations with assorted indexes. +# + +do_execsql_test 1.0.1 { + CREATE TABLE a1(c, b INTEGER, a TEXT, PRIMARY KEY(a, b)); + + INSERT INTO a1 (rowid, c, b, a) VALUES(3, '0x03', 1, 1); + INSERT INTO a1 (rowid, c, b, a) VALUES(14, '0x0E', 2, 2); + INSERT INTO a1 (rowid, c, b, a) VALUES(15, '0x0F', 3, 3); + INSERT INTO a1 (rowid, c, b, a) VALUES(92, '0x5C', 4, 4); + + CREATE TABLE a2(x BLOB, y BLOB); + INSERT INTO a2(x, y) VALUES(1, 1); + INSERT INTO a2(x, y) VALUES(2, '2'); + INSERT INTO a2(x, y) VALUES('3', 3); + INSERT INTO a2(x, y) VALUES('4', '4'); +} + +do_execsql_test 1.0.2 { + SELECT x, typeof(x), y, typeof(y) FROM a2 ORDER BY rowid +} { + 1 integer 1 integer + 2 integer 2 text + 3 text 3 integer + 4 text 4 text +} + +do_execsql_test 1.1.1 { + SELECT (SELECT rowid FROM a1 WHERE a=x AND b=y) FROM a2 +} {{} {} 15 92} +do_execsql_test 1.1.2 { + SELECT (SELECT rowid FROM a1 WHERE (a, b) = (x, y)) FROM a2 +} {{} {} 15 92} + +do_execsql_test 1.2.3 { + SELECT a1.rowid FROM a1, a2 WHERE a=x AND b=y; +} {15 92} +do_execsql_test 1.2.4 { + SELECT a1.rowid FROM a1, a2 WHERE (a, b) = (x, y) +} {15 92} + + +do_execsql_test 1.3.1 { + SELECT a1.rowid FROM a1, a2 WHERE coalesce(NULL,x)=a AND coalesce(NULL,y)=b +} {3 14 15 92} +do_execsql_test 1.3.2 { + SELECT a1.rowid FROM a1, a2 + WHERE (coalesce(NULL,x), coalesce(NULL,y)) = (a, b) +} {3 14 15 92} + +do_execsql_test 1.4.1 { + SELECT a1.rowid FROM a1, a2 WHERE +x=a AND +y=b +} {3 14 15 92} +do_execsql_test 1.4.2 { + SELECT a1.rowid FROM a1, a2 WHERE (+x, +y) = (a, b) +} {3 14 15 92} + +do_execsql_test 1.5.1 { + SELECT (SELECT rowid FROM a1 WHERE a=+x AND b=+y) FROM a2 +} {3 14 15 92} +do_execsql_test 1.5.2 { + SELECT (SELECT rowid FROM a1 WHERE (a, b) = (+x, +y)) FROM a2 +} {3 14 15 92} +do_execsql_test 1.5.3 { + SELECT (SELECT rowid FROM a1 WHERE (+x, +y) = (a, b)) FROM a2 +} {3 14 15 92} + +do_execsql_test 1.6.1 { + SELECT a1.rowid FROM a1 WHERE (a, b) IN (SELECT x, y FROM a2) +} {15 92} +do_execsql_test 1.6.2 { + SELECT a1.rowid FROM a1, a2 WHERE EXISTS ( + SELECT 1 FROM a1 WHERE a=x AND b=y + ) +} {3 14 15 92 3 14 15 92} + +# Test that [199df416] is fixed. +# +do_execsql_test 2.1 { + CREATE TABLE b1(a TEXT); + CREATE TABLE b2(x BLOB); + INSERT INTO b1 VALUES(1); + INSERT INTO b2 VALUES(1); +} +do_execsql_test 2.2 { SELECT * FROM b1, b2 WHERE a=x; } {} +do_execsql_test 2.3 { SELECT * FROM b1 WHERE a IN (SELECT x FROM b2) } {} +do_execsql_test 2.4 { CREATE UNIQUE INDEX b1a ON b1(a); } +do_execsql_test 2.5 { SELECT * FROM b1 WHERE a IN (SELECT x FROM b2) } {} + +# Test that a multi-column version of the query that revealed problem +# [199df416] also works. +# +do_execsql_test 3.1 { + CREATE TABLE c1(a INTEGER, b TEXT); + INSERT INTO c1 VALUES(1, 1); + CREATE TABLE c2(x BLOB, y BLOB); + INSERT INTO c2 VALUES(1, 1); +} +do_execsql_test 3.2 { + SELECT * FROM c1 WHERE (a, b) IN (SELECT x, y FROM c2) +} {} +do_execsql_test 3.3 { + CREATE UNIQUE INDEX c1ab ON c1(a, b); + SELECT * FROM c1 WHERE (a, b) IN (SELECT x, y FROM c2) +} {} +do_execsql_test 3.4 { + SELECT * FROM c1 WHERE (a, +b) IN (SELECT x, y FROM c2) +} {} + +do_execsql_test 3.5 { + SELECT c1.rowid FROM c1 WHERE b = (SELECT y FROM c2); +} {} +do_execsql_test 3.6 { + SELECT c1.rowid FROM c1 WHERE (a, b) = (SELECT x, y FROM c2); +} {} + + +#------------------------------------------------------------------------- +# +do_execsql_test 4.0 { + CREATE TABLE d1(a TEXT, b INTEGER, c NUMERIC); + CREATE TABLE d2(x BLOB, y BLOB); + + INSERT INTO d1 VALUES(1, 1, 1); + INSERT INTO d1 VALUES(2, 2, 2); + INSERT INTO d1 VALUES(3, 3, 3); + INSERT INTO d1 VALUES(4, 4, 4); + + INSERT INTO d2 VALUES (1, 1); + INSERT INTO d2 VALUES (2, '2'); + INSERT INTO d2 VALUES ('3', 3); + INSERT INTO d2 VALUES ('4', '4'); +} + +foreach {tn idx} { + 1 {} + 2 { CREATE INDEX idx ON d1(a) } + 3 { CREATE INDEX idx ON d1(a, c) } + 4 { CREATE INDEX idx ON d1(c) } + 5 { CREATE INDEX idx ON d1(c, a) } + + 6 { + CREATE INDEX idx ON d1(c, a) ; + CREATE INDEX idx1 ON d2(x, y); + } + + 7 { + CREATE INDEX idx ON d1(c, a) ; + CREATE UNIQUE INDEX idx2 ON d2(x, y) ; + } + + 8 { + CREATE INDEX idx ON d1(c) ; + CREATE UNIQUE INDEX idx2 ON d2(x); + } + +} { + execsql { DROP INDEX IF EXISTS idx } + execsql { DROP INDEX IF EXISTS idx2 } + execsql { DROP INDEX IF EXISTS idx3 } + execsql $idx + + do_execsql_test 4.$tn.1 { + SELECT rowid FROM d1 WHERE (a, c) IN (SELECT x, y FROM d2); + } {3 4} + + do_execsql_test 4.$tn.2 { + SELECT rowid FROM d1 WHERE (c, a) IN (SELECT x, y FROM d2); + } {2 4} + + do_execsql_test 4.$tn.3 { + SELECT rowid FROM d1 WHERE (+c, a) IN (SELECT x, y FROM d2); + } {2} + + do_execsql_test 4.$tn.4 { + SELECT rowid FROM d1 WHERE (c, a) = ( + SELECT x, y FROM d2 WHERE d2.rowid=d1.rowid + ); + } {2 4} + + do_execsql_test 4.$tn.5 { + SELECT d1.rowid FROM d1, d2 WHERE a = y; + } {2 4} + + do_execsql_test 4.$tn.6 { + SELECT d1.rowid FROM d1 WHERE a = ( + SELECT y FROM d2 where d2.rowid=d1.rowid + ); + } {2 4} +} + +do_execsql_test 5.0 { + CREATE TABLE e1(a TEXT, c NUMERIC); + CREATE TABLE e2(x BLOB, y BLOB); + + INSERT INTO e1 VALUES(2, 2); + + INSERT INTO e2 VALUES ('2', 2); + INSERT INTO e2 VALUES ('2', '2'); + INSERT INTO e2 VALUES ('2', '2.0'); + + CREATE INDEX e1c ON e1(c); +} + +do_execsql_test 5.1 { + SELECT rowid FROM e1 WHERE (a, c) IN (SELECT x, y FROM e2); +} {1} +do_execsql_test 5.2 { + SELECT rowid FROM e2 WHERE rowid IN (SELECT +c FROM e1); +} {2} +do_execsql_test 5.3 { + SELECT rowid FROM e2 WHERE rowid IN (SELECT 0+c FROM e1); +} {2} + +#------------------------------------------------------------------------- +# +do_execsql_test 6.0 { + CREATE TABLE f1(a, b); + CREATE TABLE f2(c, d); + CREATE TABLE f3(e, f); +} + +do_execsql_test 6.1 { + SELECT * FROM f3 WHERE (e, f) IN ( + SELECT a, b FROM f1 UNION ALL SELECT c, d FROM f2 + ); +} +do_execsql_test 6.2 { + CREATE INDEX f3e ON f3(e); + SELECT * FROM f3 WHERE (e, f) IN ( + SELECT a, b FROM f1 UNION ALL SELECT c, d FROM f2 + ); +} + + +#------------------------------------------------------------------------- +# +do_execsql_test 7.0 { + CREATE TABLE g1(a, b); + INSERT INTO g1 VALUES + (1, 1), (1, 2), (1, 3), (1, 'i'), (1, 'j'), + (1, 6), (1, 7), (1, 8), (1, 9), (1, 10), + (1, 4), (1, 5); + + CREATE TABLE g2(x, y); + CREATE INDEX g2x ON g2(x); + + INSERT INTO g2 VALUES(1, 4); + INSERT INTO g2 VALUES(1, 5); +} + +do_execsql_test 7.1 { + SELECT * FROM g2 WHERE (x, y) IN ( + SELECT a, b FROM g1 ORDER BY +a, +b LIMIT 10 + ); +} { 1 4 1 5 } + +do_execsql_test 7.2 { + SELECT * FROM g2 WHERE (x, y) IN ( + SELECT a, b FROM g1 ORDER BY a, b LIMIT 10 + ); +} { 1 4 1 5 } + +do_execsql_test 7.3 { + SELECT * FROM g2 WHERE (x, y) IN ( + SELECT a, b FROM g1 ORDER BY 1, 2 LIMIT 10 + ); +} { 1 4 1 5 } + +#------------------------------------------------------------------------- +# +do_execsql_test 8.1 { + CREATE TABLE t1(a ,b FLOAT); + CREATE INDEX t1x1 ON t1(a,b,a,a,a,a,a,a,a,a,a,b); +} + +do_catchsql_test 8.2 { + SELECT a FROM t1 NATURAL JOIN t1 WHERE (a,b)> (SELECT 2 IN (SELECT 2,2), 2); +} {1 {sub-select returns 2 columns - expected 1}} + +#------------------------------------------------------------------------- +# +reset_db +do_execsql_test 9.0 { + CREATE TABLE t1(a, b); + INSERT INTO t1 VALUES(1, 1), (1, 2), (2, 2), (2, 3), (3, 3), (3, 4), (4, 4); +} + +do_execsql_test 9.1 { + SELECT * FROM t1 WHERE (a, b) IN ( (3, 3), (2, 2) ); +} { + 2 2 3 3 +} +do_execsql_test 9.2 { + CREATE INDEX i1 ON t1(a); +} + +do_execsql_test 9.4 { + SELECT * FROM t1 WHERE (a, b) IN ( (3, 3), (2, 2) ); +} { + 2 2 3 3 +} +do_eqp_test 9.4e { + SELECT * FROM t1 WHERE (a, b) IN ( (3, 3), (2, 2) ); +} { + *SEARCH t1 USING INDEX i1* +} + +do_execsql_test 9.5 { + CREATE INDEX i2 ON t1(b, a); + SELECT * FROM t1 WHERE (a, b) IN ( (3, 3), (2, 2) ); +} { + 2 2 3 3 +} +do_eqp_test 9.5e { + SELECT * FROM t1 WHERE (a, b) IN ( (3, 3), (2, 2) ); +} { + *SEARCH t1 USING COVERING INDEX i2* +} + + +finish_test diff --git a/test/rowvalueA.test b/test/rowvalueA.test new file mode 100644 index 0000000000..8760c2c396 --- /dev/null +++ b/test/rowvalueA.test @@ -0,0 +1,76 @@ +# 2021 July 6 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix rowvalueA + +do_execsql_test 1.0 { + SELECT (1, 2) IN ( (3, 4), (5, 6), (1, 3) ); +} {0} + +do_execsql_test 1.1 { + SELECT (1, 2) IN ( (3, 4), (5, 6), (1, 2) ); +} {1} + +do_execsql_test 1.2 { + SELECT (1, 2) IN ( (3, 2) ); +} {0} + +do_execsql_test 1.3 { + SELECT (1, 2) IN ( (1, 2) ); +} {1} + +do_execsql_test 1.4 { + SELECT (1, 2) IN ( ); +} {0} + +do_execsql_test 1.5 { + SELECT (1, 2) NOT IN ( ); +} {1} + +for {set ii 0} {$ii < 2000} {incr ii} { + lappend L "($ii, $ii)" +} + +do_execsql_test 1.6.1 " + SELECT (400,400) IN ( [join $L ,] ) +" 1 + +do_execsql_test 1.6.2 " + SELECT (1500,1500) IN ( [join $L ,] ) +" 1 + +do_execsql_test 1.6.2 " + SELECT (1500,1499) IN ( [join $L ,] ) +" 0 + +#------------------------------------------------------------------------- + +do_catchsql_test 2.0 { + SELECT (1, 2) IN ( (1, 2), (3, 4, 5), (5, 6) ) +} {1 {IN(...) element has 3 terms - expected 2}} + +do_catchsql_test 2.1 { + SELECT (1, 2) IN ( (1, 2), 4, (5, 6) ) +} {1 {IN(...) element has 1 term - expected 2}} + +do_catchsql_test 2.2 { + SELECT (1, 2, 3) IN ( (1, 2), (3, 4), (5, 6) ) +} {1 {IN(...) element has 2 terms - expected 3}} + +do_catchsql_test 2.3 { + SELECT 2 IN ( (1, 2), (3, 4), (5, 6) ) +} {1 {row value misused}} + +finish_test diff --git a/test/rowvaluefault.test b/test/rowvaluefault.test new file mode 100644 index 0000000000..a06ef886e9 --- /dev/null +++ b/test/rowvaluefault.test @@ -0,0 +1,89 @@ +# 2016 June 17 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. +# + + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/malloc_common.tcl +set ::testprefix rowvaluefault + +do_execsql_test 1.0 { + CREATE TABLE xyz(one, two, thr, fou); + INSERT INTO xyz VALUES('A', 'A', 'A', 1); + INSERT INTO xyz VALUES('B', 'B', 'B', 2); + INSERT INTO xyz VALUES('C', 'C', 'C', 3); + INSERT INTO xyz VALUES('D', 'D', 'D', 4); + + CREATE UNIQUE INDEX xyz_one_two ON xyz(one, two); +} + +do_faultsim_test 1 -faults oom* -body { + execsql { SELECT fou FROM xyz WHERE (one, two, thr) = ('B', 'B', 'B') } +} -test { + faultsim_test_result {0 2} +} + +do_faultsim_test 2 -faults oom* -body { + execsql { SELECT fou FROM xyz WHERE (two, thr) IS ('C', 'C') } +} -test { + faultsim_test_result {0 3} +} + +do_faultsim_test 3 -faults oom* -body { + execsql { SELECT fou FROM xyz WHERE (one, two, thr) > ('B', 'B', 'B') } +} -test { + faultsim_test_result {0 {3 4}} +} + +do_faultsim_test 4 -faults oom* -body { + execsql { SELECT fou FROM xyz WHERE (one, two) IN (SELECT one, two FROM xyz) } +} -test { + faultsim_test_result {0 {1 2 3 4}} +} + +do_faultsim_test 5 -faults oom* -body { + execsql { + SELECT fou FROM xyz + WHERE (one, two, thr) IN (SELECT one, two, thr FROM xyz) + } +} -test { + faultsim_test_result {0 {1 2 3 4}} +} + +do_faultsim_test 6 -faults oom* -body { + execsql { + SELECT fou FROM xyz + WHERE (one, two, thr) BETWEEN ('B', 'B', 'B') AND ('C', 'C', 'C') } +} -test { + faultsim_test_result {0 {2 3}} +} + +do_faultsim_test 7 -faults oom* -body { + execsql { + SELECT fou FROM xyz + WHERE (one, two, thr) IN ( ('a','b','c'), ('A','A','A'), (1,2,3) ); + } +} -test { + faultsim_test_result {0 1} +} + +do_faultsim_test 8 -faults oom* -body { + execsql { + SELECT fou FROM xyz + WHERE (two, one) IN ( ('a','b','c'), ('A','A','A'), (1,2,3) ); + } +} -test { + faultsim_test_result {1 {IN(...) element has 3 terms - expected 2}} +} + +finish_test diff --git a/test/rowvaluevtab.test b/test/rowvaluevtab.test new file mode 100644 index 0000000000..d3e0ac7b8a --- /dev/null +++ b/test/rowvaluevtab.test @@ -0,0 +1,95 @@ +# 2018 October 14 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set ::testprefix rowvaluevtab + +ifcapable !vtab { + finish_test + return +} + +register_echo_module db + +do_execsql_test 1.0 { + CREATE TABLE t1(a, b, c); + CREATE INDEX t1b ON t1(b); + INSERT INTO t1 VALUES('one', 1, 1); + INSERT INTO t1 VALUES('two', 1, 2); + INSERT INTO t1 VALUES('three', 1, 3); + INSERT INTO t1 VALUES('four', 2, 1); + INSERT INTO t1 VALUES('five', 2, 2); + INSERT INTO t1 VALUES('six', 2, 3); + INSERT INTO t1 VALUES('seven', 3, 1); + INSERT INTO t1 VALUES('eight', 3, 2); + INSERT INTO t1 VALUES('nine', 3, 3); + + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<10000 + ) INSERT INTO t1 SELECT NULL, NULL, NULL FROM s; + CREATE VIRTUAL TABLE e1 USING echo(t1); +} + +proc do_vfilter4_test {tn sql expected} { + set res [list] + db eval "explain $sql" { + if {$opcode=="VFilter"} { + lappend res $p4 + } + } + uplevel [list do_test $tn [list set {} $res] [list {*}$expected]] +} + +do_execsql_test 1.1 { + SELECT a FROM e1 WHERE (b, c) = (2, 2) +} {five} +do_vfilter4_test 1.1f { + SELECT a FROM e1 WHERE (b, c) = (?, ?) +} {{SELECT rowid, a, b, c FROM 't1' WHERE b = ?}} + +do_execsql_test 1.2 { + SELECT a FROM e1 WHERE (b, c) > (2, 2) +} {six seven eight nine} +do_vfilter4_test 1.2f { + SELECT a FROM e1 WHERE (b, c) > (2, 2) +} { + {SELECT rowid, a, b, c FROM 't1' WHERE b >= ?} +} + +do_execsql_test 1.3 { + SELECT a FROM e1 WHERE (b, c) >= (2, 2) +} {five six seven eight nine} +do_vfilter4_test 1.3f { + SELECT a FROM e1 WHERE (b, c) >= (2, 2) +} { + {SELECT rowid, a, b, c FROM 't1' WHERE b >= ?} +} + +do_execsql_test 1.3 { + SELECT a FROM e1 WHERE (b, c) BETWEEN (1, 2) AND (2, 3) +} {two three four five six} +do_vfilter4_test 1.3f { + SELECT a FROM e1 WHERE (b, c) BETWEEN (1, 2) AND (2, 3) +} { + {SELECT rowid, a, b, c FROM 't1' WHERE b >= ? AND b <= ?} +} + +do_execsql_test 1.4 { + SELECT a FROM e1 WHERE (b, c) IN ( VALUES(2, 2) ) +} {five} +do_vfilter4_test 1.4f { + SELECT a FROM e1 WHERE (b, c) IN ( VALUES(2, 2) ) +} {{SELECT rowid, a, b, c FROM 't1' WHERE b = ?}} + +finish_test diff --git a/test/savepoint.test b/test/savepoint.test index 8055e61d9e..3952981dab 100644 --- a/test/savepoint.test +++ b/test/savepoint.test @@ -16,6 +16,8 @@ source $testdir/tester.tcl source $testdir/lock_common.tcl source $testdir/malloc_common.tcl +forcedelete test2.db + #---------------------------------------------------------------------- # The following tests - savepoint-1.* - test that the SAVEPOINT, RELEASE # and ROLLBACK TO comands are correctly parsed, and that the auto-commit @@ -616,12 +618,16 @@ ifcapable auth { # First make sure it is not possible to attach or detach a database while # a savepoint is open (it is not possible if any transaction is open). # +# UPDATE 2017-07-26: It is not possible to ATTACH and DETACH within a +# a transaction. +# do_test savepoint-10.1.1 { catchsql { SAVEPOINT one; ATTACH 'test2.db' AS aux; + DETACH aux; } -} {1 {cannot ATTACH database within transaction}} +} {0 {}} do_test savepoint-10.1.2 { execsql { RELEASE one; @@ -630,8 +636,9 @@ do_test savepoint-10.1.2 { catchsql { SAVEPOINT one; DETACH aux; + ATTACH 'test2.db' AS aux; } -} {1 {cannot DETACH database within transaction}} +} {0 {}} do_test savepoint-10.1.3 { execsql { RELEASE one; diff --git a/test/savepoint6.test b/test/savepoint6.test index b1d0d46f5c..6b41ef2da9 100644 --- a/test/savepoint6.test +++ b/test/savepoint6.test @@ -15,6 +15,10 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl proc sql {zSql} { + if {0 && $::debug_op} { + puts stderr "$zSql ;" + flush stderr + } uplevel db eval [list $zSql] #puts stderr "$zSql ;" } @@ -67,11 +71,13 @@ proc x_to_y {x} { # delete_rows XVALUES # proc savepoint {zName} { + if {$::debug_op} { puts stderr "savepoint $zName" ; flush stderr } catch { sql "SAVEPOINT $zName" } lappend ::lSavepoint [list $zName [array get ::aEntry]] } proc rollback {zName} { + if {$::debug_op} { puts stderr "rollback $zName" ; flush stderr } catch { sql "ROLLBACK TO $zName" } for {set i [expr {[llength $::lSavepoint]-1}]} {$i>=0} {incr i -1} { set zSavepoint [lindex $::lSavepoint $i 0] @@ -89,6 +95,7 @@ proc rollback {zName} { } proc release {zName} { + if {$::debug_op} { puts stderr "release $zName" ; flush stderr } catch { sql "RELEASE $zName" } for {set i [expr {[llength $::lSavepoint]-1}]} {$i>=0} {incr i -1} { set zSavepoint [lindex $::lSavepoint $i 0] @@ -104,6 +111,7 @@ proc release {zName} { } proc insert_rows {lX} { + if {$::debug_op} { puts stderr "insert_rows $lX" ; flush stderr } foreach x $lX { set y [x_to_y $x] @@ -116,6 +124,7 @@ proc insert_rows {lX} { } proc delete_rows {lX} { + if {$::debug_op} { puts stderr "delete_rows $lX" ; flush stderr } foreach x $lX { # Update database [db] sql "DELETE FROM t1 WHERE x = $x" @@ -164,6 +173,11 @@ proc random_integers {nRes nRange} { } #------------------------------------------------------------------------- +set ::debug_op 0 +proc debug_ops {} { + set ::debug_op 1 +} + proc database_op {} { set i [expr int(rand()*2)] if {$i==0} { @@ -185,9 +199,6 @@ proc savepoint_op {} { set C [lindex $cmds [expr int(rand()*6)]] set N [lindex $names [expr int(rand()*5)]] - #puts stderr " $C $N ; " - #flush stderr - $C $N return ok } diff --git a/test/savepoint7.test b/test/savepoint7.test index d8a02f1f80..2652cc3972 100644 --- a/test/savepoint7.test +++ b/test/savepoint7.test @@ -95,4 +95,38 @@ do_test savepoint7-2.2 { list $rc $msg [db eval {SELECT * FROM t2}] } {1 {abort due to ROLLBACK} {}} +# Ticket: https://sqlite.org/src/tktview/7f7f8026eda387d544b +# Segfault in the in-memory journal logic triggered by a tricky +# combination of SAVEPOINT operations. +# +unset -nocomplain i +for {set i 248} {$i<=253} {incr i} { + do_test savepoint7-3.$i { + db close + forcedelete test.db + sqlite3 db test.db + db eval { + PRAGMA page_size=1024; + PRAGMA temp_store=MEMORY; + BEGIN; + CREATE TABLE t1(x INTEGER PRIMARY KEY, y TEXT); + WITH RECURSIVE c(x) AS (VALUES(1) UNION SELECT x+1 FROM c WHERE x<$::i) + INSERT INTO t1(x,y) SELECT x*10, printf('%04d%.800c',x,'*') FROM c; + SAVEPOINT one; + SELECT count(*) FROM t1; + WITH RECURSIVE c(x) AS (VALUES(1) UNION SELECT x+1 FROM c WHERE x<$::i) + INSERT INTO t1(x,y) SELECT x*10+1, printf('%04d%.800c',x,'*') FROM c; + ROLLBACK TO one; + SELECT count(*) FROM t1; + SAVEPOINT twoB; + WITH RECURSIVE c(x) AS (VALUES(1) UNION SELECT x+1 FROM c WHERE x<10) + INSERT INTO t1(x,y) SELECT x*10+2, printf('%04d%.800c',x,'*') FROM c; + ROLLBACK TO twoB; + RELEASE one; + COMMIT; + } + } [list $i $i] +} + + finish_test diff --git a/test/scanstatus.test b/test/scanstatus.test index ed24d97437..549e7fd3c8 100644 --- a/test/scanstatus.test +++ b/test/scanstatus.test @@ -30,32 +30,42 @@ do_execsql_test 1.0 { } proc do_scanstatus_test {tn res} { - set stmt [db_last_stmt_ptr db] + set stmt [db version -last-stmt-ptr] set idx 0 set ret [list] while {1} { set r [sqlite3_stmt_scanstatus $stmt $idx] if {[llength $r]==0} break - lappend ret {*}$r + foreach v {nLoop nVisit nEst zName zExplain} { + lappend ret $v [dict get $r $v] + } incr idx } uplevel [list do_test $tn [list set {} $ret] [list {*}$res]] } -do_execsql_test 1.1 { SELECT count(*) FROM t1, t2; } 6 -do_scanstatus_test 1.2 { - nLoop 1 nVisit 2 nEst 1048576.0 zName t1 zExplain {SCAN TABLE t1} - nLoop 2 nVisit 6 nEst 1048576.0 zName t2 zExplain {SCAN TABLE t2} +do_execsql_test 1.1a { SELECT count(*) FROM t1, t2; } 6 +do_scanstatus_test 1.1b { + nLoop 1 nVisit 2 nEst 1048576.0 zName t1 zExplain {SCAN t1} + nLoop 2 nVisit 6 nEst 1048576.0 zName t2 zExplain {SCAN t2} } +sqlite3_db_config db STMT_SCANSTATUS 0 + +do_execsql_test 1.2a { SELECT count(*) FROM t1, t2; } 6 +do_scanstatus_test 1.2b { +} + +sqlite3_db_config db STMT_SCANSTATUS 1 + do_execsql_test 1.3 { ANALYZE; SELECT count(*) FROM t1, t2; } 6 do_scanstatus_test 1.4 { - nLoop 1 nVisit 2 nEst 2.0 zName t1 zExplain {SCAN TABLE t1} - nLoop 2 nVisit 6 nEst 3.0 zName t2 zExplain {SCAN TABLE t2} + nLoop 1 nVisit 2 nEst 2.0 zName t1 zExplain {SCAN t1} + nLoop 2 nVisit 6 nEst 3.0 zName t2 zExplain {SCAN t2} } do_execsql_test 1.5 { ANALYZE } @@ -64,8 +74,8 @@ do_execsql_test 1.6 { } 4 do_scanstatus_test 1.7 { nLoop 1 nVisit 2 nEst 2.0 zName t2 zExplain - {SEARCH TABLE t2 USING INTEGER PRIMARY KEY (rowid>?)} - nLoop 2 nVisit 4 nEst 2.0 zName t1 zExplain {SCAN TABLE t1} + {SEARCH t2 USING INTEGER PRIMARY KEY (rowid>?)} + nLoop 2 nVisit 4 nEst 2.0 zName t1 zExplain {SCAN t1} } do_execsql_test 1.8 { @@ -74,24 +84,25 @@ do_execsql_test 1.8 { do_scanstatus_test 1.9 { nLoop 2 nVisit 4 nEst 2.0 zName t2 zExplain - {SEARCH TABLE t2 USING INTEGER PRIMARY KEY (rowid>?)} - nLoop 4 nVisit 8 nEst 2.0 zName t1 zExplain {SCAN TABLE t1} + {SEARCH t2 USING INTEGER PRIMARY KEY (rowid>?)} + nLoop 4 nVisit 8 nEst 2.0 zName t1 zExplain {SCAN t1} } do_test 1.9 { - sqlite3_stmt_scanstatus_reset [db_last_stmt_ptr db] + sqlite3_stmt_scanstatus_reset [db version -last-stmt-ptr] } {} do_scanstatus_test 1.10 { nLoop 0 nVisit 0 nEst 2.0 zName t2 zExplain - {SEARCH TABLE t2 USING INTEGER PRIMARY KEY (rowid>?)} - nLoop 0 nVisit 0 nEst 2.0 zName t1 zExplain {SCAN TABLE t1} + {SEARCH t2 USING INTEGER PRIMARY KEY (rowid>?)} + nLoop 0 nVisit 0 nEst 2.0 zName t1 zExplain {SCAN t1} } #------------------------------------------------------------------------- # Try a few different types of scans. # reset_db +sqlite3_db_config db STMT_SCANSTATUS 1 do_execsql_test 2.1 { CREATE TABLE x1(i INTEGER PRIMARY KEY, j); INSERT INTO x1 VALUES(1, 'one'); @@ -105,7 +116,7 @@ do_execsql_test 2.1 { do_scanstatus_test 2.2 { nLoop 1 nVisit 1 nEst 1.0 zName x1 - zExplain {SEARCH TABLE x1 USING INTEGER PRIMARY KEY (rowid=?)} + zExplain {SEARCH x1 USING INTEGER PRIMARY KEY (rowid=?)} } do_execsql_test 2.3.1 { @@ -113,7 +124,7 @@ do_execsql_test 2.3.1 { } {2 two} do_scanstatus_test 2.3.2 { nLoop 1 nVisit 1 nEst 10.0 zName x1j - zExplain {SEARCH TABLE x1 USING COVERING INDEX x1j (j=?)} + zExplain {SEARCH x1 USING COVERING INDEX x1j (j=?)} } do_execsql_test 2.4.1 { @@ -121,7 +132,7 @@ do_execsql_test 2.4.1 { } {4 four 1 one 3 three} do_scanstatus_test 2.4.2 { nLoop 1 nVisit 3 nEst 262144.0 zName x1j - zExplain {SEARCH TABLE x1 USING COVERING INDEX x1j (j<?)} + zExplain {SEARCH x1 USING COVERING INDEX x1j (j<?)} } do_execsql_test 2.5.1 { @@ -129,7 +140,7 @@ do_execsql_test 2.5.1 { } {2 two} do_scanstatus_test 2.5.2 { nLoop 1 nVisit 1 nEst 262144.0 zName x1j - zExplain {SEARCH TABLE x1 USING COVERING INDEX x1j (j>?)} + zExplain {SEARCH x1 USING COVERING INDEX x1j (j>?)} } do_execsql_test 2.6.1 { @@ -137,7 +148,7 @@ do_execsql_test 2.6.1 { } {3 three 2 two} do_scanstatus_test 2.6.2 { nLoop 1 nVisit 2 nEst 16384.0 zName x1j - zExplain {SEARCH TABLE x1 USING COVERING INDEX x1j (j>? AND j<?)} + zExplain {SEARCH x1 USING COVERING INDEX x1j (j>? AND j<?)} } do_execsql_test 2.7.1 { @@ -150,7 +161,7 @@ do_execsql_test 2.7.1 { do_scanstatus_test 2.7.2 { nLoop 1 nVisit 2 nEst 16384.0 zName x2j - zExplain {SEARCH TABLE x2 USING INDEX x2j (j>? AND j<?)} + zExplain {SEARCH x2 USING INDEX x2j (j>? AND j<?)} } do_execsql_test 2.8.1 { @@ -158,7 +169,7 @@ do_execsql_test 2.8.1 { } do_scanstatus_test 2.8.2 { nLoop 1 nVisit 0 nEst 8.0 zName x2ij - zExplain {SEARCH TABLE x2 USING INDEX x2ij (i=? AND j=?)} + zExplain {SEARCH x2 USING INDEX x2ij (i=? AND j=?)} } do_execsql_test 2.9.1 { @@ -166,7 +177,7 @@ do_execsql_test 2.9.1 { } do_scanstatus_test 2.9.2 { nLoop 1 nVisit 0 nEst 8.0 zName x2ij - zExplain {SEARCH TABLE x2 USING INDEX x2ij (i=? AND j=?)} + zExplain {SEARCH x2 USING INDEX x2ij (i=? AND j=?)} } do_execsql_test 2.10.1 { @@ -174,7 +185,7 @@ do_execsql_test 2.10.1 { } {3 three {3 three}} do_scanstatus_test 2.10.2 { nLoop 1 nVisit 1 nEst 8.0 zName x2ij - zExplain {SEARCH TABLE x2 USING INDEX x2ij (i=? AND j=?)} + zExplain {SEARCH x2 USING INDEX x2ij (i=? AND j=?)} } #------------------------------------------------------------------------- @@ -194,9 +205,9 @@ do_execsql_test 3.2.1 { } {4 13} do_scanstatus_test 3.2.2 { nLoop 1 nVisit 1 nEst 10.0 zName a1a - zExplain {SEARCH TABLE a1 USING INDEX a1a (a=?)} + zExplain {SEARCH a1 USING INDEX a1a (a=?)} nLoop 1 nVisit 1 nEst 10.0 zName a1bc - zExplain {SEARCH TABLE a1 USING INDEX a1bc (b=?)} + zExplain {SEARCH a1 USING INDEX a1bc (b=?)} } do_execsql_test 3.2.1 { @@ -204,9 +215,9 @@ do_execsql_test 3.2.1 { } {30} do_scanstatus_test 3.2.2 { nLoop 1 nVisit 9 nEst 16384.0 zName a1a - zExplain {SEARCH TABLE a1 USING INDEX a1a (a>? AND a<?)} + zExplain {SEARCH a1 USING INDEX a1a (a>? AND a<?)} nLoop 1 nVisit 21 nEst 16384.0 zName a1bc - zExplain {SEARCH TABLE a1 USING INDEX a1bc (b>? AND b<?)} + zExplain {SEARCH a1 USING INDEX a1bc (b>? AND b<?)} } do_execsql_test 3.3.1 { @@ -215,9 +226,9 @@ do_execsql_test 3.3.1 { } {90} do_scanstatus_test 3.2.2 { nLoop 1 nVisit 10 nEst 16384.0 zName a1bc - zExplain {SEARCH TABLE a1 AS y USING COVERING INDEX a1bc (b>? AND b<?)} + zExplain {SEARCH y USING COVERING INDEX a1bc (b>? AND b<?)} nLoop 10 nVisit 90 nEst 16384.0 zName a1a - zExplain {SEARCH TABLE a1 AS x USING COVERING INDEX a1a (a>? AND a<?)} + zExplain {SEARCH x USING COVERING INDEX a1a (a>? AND a<?)} } do_execsql_test 3.4.1 { @@ -225,7 +236,7 @@ do_execsql_test 3.4.1 { } {4} do_scanstatus_test 3.4.2 { nLoop 1 nVisit 4 nEst 40.0 zName a1a - zExplain {SEARCH TABLE a1 USING COVERING INDEX a1a (a=?)} + zExplain {SEARCH a1 USING COVERING INDEX a1a (a=?)} } do_execsql_test 3.4.1 { @@ -233,7 +244,7 @@ do_execsql_test 3.4.1 { } {4} do_scanstatus_test 3.4.2 { nLoop 1 nVisit 4 nEst 4.0 zName a1 - zExplain {SEARCH TABLE a1 USING INTEGER PRIMARY KEY (rowid=?)} + zExplain {SEARCH a1 USING INTEGER PRIMARY KEY (rowid=?)} } #------------------------------------------------------------------------- @@ -254,7 +265,7 @@ do_execsql_test 4.0 { } do_execsql_test 4.1.1 { INSERT INTO t1 VALUES(1, 2, 3); } -do_scanstatus_test 4.1.2 { } +do_scanstatus_test 4.1.2 {} do_execsql_test 4.2 { CREATE TABLE p1(x PRIMARY KEY); @@ -266,15 +277,16 @@ do_execsql_test 4.2 { do_execsql_test 4.2.1 { DELETE FROM p1 WHERE x=4 } do_scanstatus_test 4.2.2 { nLoop 1 nVisit 1 nEst 1.0 zName sqlite_autoindex_p1_1 - zExplain {SEARCH TABLE p1 USING INDEX sqlite_autoindex_p1_1 (x=?)} + zExplain {SEARCH p1 USING INDEX sqlite_autoindex_p1_1 (x=?)} - nLoop 1 nVisit 3 nEst 262144.0 zName c1 zExplain {SCAN TABLE c1} + nLoop 1 nVisit 3 nEst 262144.0 zName c1 zExplain {SCAN c1} } #------------------------------------------------------------------------- # Further tests of different scan types. # reset_db +sqlite3_db_config db STMT_SCANSTATUS 1 proc tochar {i} { set alphabet {a b c d e f g h i j k l m n o p q r s t u v w x y z} return [lindex $alphabet [expr $i % [llength $alphabet]]] @@ -311,11 +323,13 @@ do_execsql_test 5.0 { do_execsql_test 5.1.1 { SELECT count(*) FROM t1 WHERE a IN (SELECT b FROM t1 AS ii) } {2} -do_scanstatus_test 5.1.2 { - nLoop 1 nVisit 10 nEst 10.0 zName t1bc - zExplain {SCAN TABLE t1 AS ii USING COVERING INDEX t1bc} - nLoop 1 nVisit 2 nEst 8.0 zName sqlite_autoindex_t1_1 - zExplain {SEARCH TABLE t1 USING COVERING INDEX sqlite_autoindex_t1_1 (a=?)} +ifcapable stat4 { + do_scanstatus_test 5.1.2 { + nLoop 1 nVisit 10 nEst 10.0 zName t1 + zExplain {SCAN ii} + nLoop 1 nVisit 2 nEst 8.0 zName sqlite_autoindex_t1_1 + zExplain {SEARCH t1 USING COVERING INDEX sqlite_autoindex_t1_1 (a=?)} + } } do_execsql_test 5.2.1 { @@ -323,49 +337,54 @@ do_execsql_test 5.2.1 { } {2} do_scanstatus_test 5.2.2 { nLoop 1 nVisit 2 nEst 2.0 zName sqlite_autoindex_t1_1 - zExplain {SEARCH TABLE t1 USING COVERING INDEX sqlite_autoindex_t1_1 (a=?)} + zExplain {SEARCH t1 USING COVERING INDEX sqlite_autoindex_t1_1 (a=?)} } do_eqp_test 5.3.1 { SELECT count(*) FROM t2 WHERE y = 'j'; -} {0 0 0 {SEARCH TABLE t2 USING COVERING INDEX t2xy (ANY(x) AND y=?)}} +} {SEARCH t2 USING COVERING INDEX t2xy (ANY(x) AND y=?)} do_execsql_test 5.3.2 { SELECT count(*) FROM t2 WHERE y = 'j'; } {19} do_scanstatus_test 5.3.3 { nLoop 1 nVisit 19 nEst 56.0 zName t2xy zExplain - {SEARCH TABLE t2 USING COVERING INDEX t2xy (ANY(x) AND y=?)} + {SEARCH t2 USING COVERING INDEX t2xy (ANY(x) AND y=?)} } -do_eqp_test 5.4.1 { - SELECT count(*) FROM t1, t2 WHERE y = c; -} { - 0 0 0 {SCAN TABLE t1 USING COVERING INDEX t1bc} - 0 1 1 {SEARCH TABLE t2 USING COVERING INDEX t2xy (ANY(x) AND y=?)} -} -do_execsql_test 5.4.2 { - SELECT count(*) FROM t1, t2 WHERE y = c; -} {200} -do_scanstatus_test 5.4.3 { - nLoop 1 nVisit 10 nEst 10.0 zName t1bc - zExplain {SCAN TABLE t1 USING COVERING INDEX t1bc} - nLoop 10 nVisit 200 nEst 56.0 zName t2xy - zExplain {SEARCH TABLE t2 USING COVERING INDEX t2xy (ANY(x) AND y=?)} +ifcapable stat4 { + do_eqp_test 5.4.1 { + SELECT count(*) FROM t1, t2 WHERE y = c; + } { + QUERY PLAN + |--SCAN t1 + `--SEARCH t2 USING COVERING INDEX t2xy (ANY(x) AND y=?) + } + do_execsql_test 5.4.2 { + SELECT count(*) FROM t1, t2 WHERE y = c; + } {200} + do_scanstatus_test 5.4.3 { + nLoop 1 nVisit 10 nEst 10.0 zName t1 + zExplain {SCAN t1} + nLoop 10 nVisit 200 nEst 56.0 zName t2xy + zExplain {SEARCH t2 USING COVERING INDEX t2xy (ANY(x) AND y=?)} + } } do_eqp_test 5.5.1 { SELECT count(*) FROM t1, t3 WHERE y = c; } { - 0 0 1 {SCAN TABLE t3} - 0 1 0 {SEARCH TABLE t1 USING AUTOMATIC COVERING INDEX (c=?)} + QUERY PLAN + |--SCAN t3 + |--BLOOM FILTER ON t1 (c=?) + `--SEARCH t1 USING AUTOMATIC COVERING INDEX (c=?) } do_execsql_test 5.5.2 { SELECT count(*) FROM t1, t3 WHERE y = c; } {200} do_scanstatus_test 5.5.3 { - nLoop 1 nVisit 501 nEst 480.0 zName t3 zExplain {SCAN TABLE t3} + nLoop 1 nVisit 501 nEst 480.0 zName t3 zExplain {SCAN t3} nLoop 501 nVisit 200 nEst 20.0 zName auto-index zExplain - {SEARCH TABLE t1 USING AUTOMATIC COVERING INDEX (c=?)} + {SEARCH t1 USING AUTOMATIC COVERING INDEX (c=?)} } #------------------------------------------------------------------------- @@ -390,7 +409,7 @@ ifcapable fts3 { } {6} do_scanstatus_test 6.1.2 { nLoop 1 nVisit 6 nEst 24.0 zName ft1 zExplain - {SCAN TABLE ft1 VIRTUAL TABLE INDEX 3:} + {SCAN ft1 VIRTUAL TABLE INDEX 3:} } } diff --git a/test/scanstatus2.test b/test/scanstatus2.test new file mode 100644 index 0000000000..c94db88f23 --- /dev/null +++ b/test/scanstatus2.test @@ -0,0 +1,345 @@ +# 2022 December 5 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix scanstatus2 + +ifcapable !scanstatus { + finish_test + return +} + +sqlite3_db_config db STMT_SCANSTATUS 1 + +do_execsql_test 1.0 { + CREATE TABLE t1(a, b); + CREATE TABLE t2(x, y); + INSERT INTO t1 VALUES(1, 2); + INSERT INTO t1 VALUES(3, 4); + INSERT INTO t2 VALUES('a', 'b'); + INSERT INTO t2 VALUES('c', 'd'); + INSERT INTO t2 VALUES('e', 'f'); +} + +proc do_zexplain_test {v2 tn sql res} { + db eval $sql + set stmt [db version -last-stmt-ptr] + set idx 0 + set ret [list] + + set cmd sqlite3_stmt_scanstatus + set f [list] + if {$v2} { lappend f complex } + + while {1} { + set r [sqlite3_stmt_scanstatus -flags $f $stmt $idx] + if {[llength $r]==0} break + lappend ret [dict get $r zExplain] + incr idx + } + uplevel [list do_test $tn [list set {} $ret] [list {*}$res]] +} + +proc get_cycles {stmt} { + set r [sqlite3_stmt_scanstatus $stmt -1] + dict get $r nCycle +} + +proc foreach_scan {varname stmt body {debug 0}} { + upvar $varname var + for {set ii 0} {1} {incr ii} { + set f "complex" + if {$debug} { set f "complex debug" } + set r [sqlite3_stmt_scanstatus -flags $f $stmt $ii] + if {[llength $r]==0} break + array set var $r + uplevel $body + } +} + +proc get_eqp_graph {stmt iPar nIndent} { + set res "" + foreach_scan A $stmt { + if {$A(iParentId)==$iPar} { + set txt $A(zExplain) + if {$A(nCycle)>=0} { + append txt " (nCycle=$A(nCycle))" + } + append res "[string repeat - $nIndent]$txt\n" + append res [get_eqp_graph $stmt $A(iSelectId) [expr $nIndent+2]] + } + } + set res +} + +proc get_graph {stmt} { + set nCycle [get_cycles $stmt] + set res "QUERY (nCycle=$nCycle)\n" + append res [get_eqp_graph $stmt 0 2] +} + +proc do_graph_test {tn sql res} { + db eval $sql + set stmt [db version -last-stmt-ptr] + set graph [string trim [get_graph $stmt]] + + set graph [regsub -all {nCycle=[0-9]+} $graph nCycle=nnn] + uplevel [list do_test $tn [list set {} $graph] [string trim $res]] +} + +proc puts_graph {sql} { + db eval $sql + set stmt [db version -last-stmt-ptr] + puts [string trim [get_graph $stmt]] +} + +proc puts_debug_info {sql} { + db eval $sql + set stmt [db version -last-stmt-ptr] + foreach_scan X $stmt { + puts -nonewline "$X(debug_explain) $X(zExplain): " + puts -nonewline "loop=$X(debug_loop) visit=$X(debug_visit) " + puts "csr=$X(debug_csr) range=$X(debug_range)" + } 1 +} + +do_zexplain_test 0 1.1 { + SELECT (SELECT a FROM t1 WHERE b=x) FROM t2 WHERE y=2 +} { + {SCAN t2} + {SCAN t1} +} +do_zexplain_test 1 1.2 { + SELECT (SELECT a FROM t1 WHERE b=x) FROM t2 WHERE y=2 +} { + {SCAN t2} + {CORRELATED SCALAR SUBQUERY 1} + {SCAN t1} +} + +do_graph_test 1.3 { + SELECT (SELECT a FROM t1 WHERE b=x) FROM t2 WHERE y=2 +} { +QUERY (nCycle=nnn) +--SCAN t2 (nCycle=nnn) +--CORRELATED SCALAR SUBQUERY 1 (nCycle=nnn) +----SCAN t1 (nCycle=nnn) +} + +do_graph_test 1.4 { + WITH v2(x,y) AS MATERIALIZED ( + SELECT x,y FROM t2 + ) + SELECT * FROM t1, v2 ORDER BY y; +} { +QUERY (nCycle=nnn) +--MATERIALIZE v2 (nCycle=nnn) +----SCAN t2 (nCycle=nnn) +--SCAN t1 (nCycle=nnn) +--SCAN v2 (nCycle=nnn) +--USE TEMP B-TREE FOR ORDER BY (nCycle=nnn) +} + +#------------------------------------------------------------------------- +ifcapable fts5 { + reset_db + sqlite3_db_config db STMT_SCANSTATUS 1 + do_execsql_test 2.0 { + CREATE VIRTUAL TABLE ft USING fts5(a); + INSERT INTO ft VALUES('abc'); + INSERT INTO ft VALUES('def'); + INSERT INTO ft VALUES('ghi'); + } + + do_graph_test 2.1 { + SELECT * FROM ft('def') + } { +QUERY (nCycle=nnn) +--SCAN ft VIRTUAL TABLE INDEX 0:M1 (nCycle=nnn) + } +} + +#------------------------------------------------------------------------- +reset_db +sqlite3_db_config db STMT_SCANSTATUS 1 +do_execsql_test 3.0 { + CREATE TABLE x1(a, b); + CREATE TABLE x2(c, d); + + WITH s(i) AS (SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<1000) + INSERT INTO x1 SELECT i, i FROM s; + INSERT INTO x2 SELECT a, b FROM x1; +} + +do_graph_test 2.1 { + SELECT * FROM x1, x2 WHERE c=+a; +} { +QUERY (nCycle=nnn) +--SCAN x1 (nCycle=nnn) +--CREATE AUTOMATIC INDEX ON x2(c, d) (nCycle=nnn) +--BLOOM FILTER ON x2 (c=?) +--SEARCH x2 USING AUTOMATIC COVERING INDEX (c=?) (nCycle=nnn) +} + +#------------------------------------------------------------------------- +reset_db +sqlite3_db_config db STMT_SCANSTATUS 1 +do_execsql_test 4.0 { + CREATE TABLE rt1 (id INTEGER PRIMARY KEY, x1, x2); + CREATE TABLE rt2 (id, x1, x2); +} + +do_graph_test 4.1 { + SELECT * FROM rt1, rt2 WHERE rt1.id%2 AND rt2.x1=rt1.x1; +} { +QUERY (nCycle=nnn) +--SCAN rt1 (nCycle=nnn) +--CREATE AUTOMATIC INDEX ON rt2(x1, id, x2) (nCycle=nnn) +--BLOOM FILTER ON rt2 (x1=?) +--SEARCH rt2 USING AUTOMATIC COVERING INDEX (x1=?) (nCycle=nnn) +} + +do_graph_test 4.2 { + SELECT rt2.id FROM rt1, rt2 WHERE rt1.id%2 AND rt2.x1=rt1.x1; +} { +QUERY (nCycle=nnn) +--SCAN rt1 (nCycle=nnn) +--CREATE AUTOMATIC INDEX ON rt2(x1, id) (nCycle=nnn) +--BLOOM FILTER ON rt2 (x1=?) +--SEARCH rt2 USING AUTOMATIC COVERING INDEX (x1=?) (nCycle=nnn) +} + +do_graph_test 4.3 { + SELECT rt2.id FROM rt1, rt2 WHERE rt1.id%2 AND (rt2.x1+1)=(rt1.x1+1); +} { +QUERY (nCycle=nnn) +--SCAN rt1 (nCycle=nnn) +--SCAN rt2 (nCycle=nnn) +} + +do_graph_test 4.4 { + SELECT rt2.id FROM rt1, rt2 WHERE rt1.id%2 AND rt2.x1=(rt1.x1+1) AND rt2.id>5; +} { +QUERY (nCycle=nnn) +--SCAN rt1 (nCycle=nnn) +--CREATE AUTOMATIC INDEX ON rt2(x1, id) WHERE <expr> (nCycle=nnn) +--BLOOM FILTER ON rt2 (x1=?) +--SEARCH rt2 USING AUTOMATIC PARTIAL COVERING INDEX (x1=?) (nCycle=nnn) +} + +do_graph_test 4.5 { + SELECT v1.cnt FROM rt1, ( + SELECT count(*) AS cnt, rt2.x1 AS x1 FROM rt2 GROUP BY x1 + ) AS v1 WHERE rt1.x1=v1.x1 +} { +QUERY (nCycle=nnn) +--CO-ROUTINE v1 +----SCAN rt2 (nCycle=nnn) +----USE TEMP B-TREE FOR GROUP BY (nCycle=nnn) +--SCAN rt1 (nCycle=nnn) +--CREATE AUTOMATIC INDEX ON v1(x1, cnt) (nCycle=nnn) +--BLOOM FILTER ON v1 (x1=?) +--SEARCH v1 USING AUTOMATIC COVERING INDEX (x1=?) (nCycle=nnn) +} + +#------------------------------------------------------------------------- +reset_db + +ifcapable trace { + do_execsql_test 5.0 { + CREATE TABLE t1(x, y); + CREATE TRIGGER tr1 AFTER DELETE ON t1 BEGIN + SELECT 1; + END; + INSERT INTO t1 VALUES(1, 2); + } + + proc trace {stmt sql} { + array set A [sqlite3_stmt_scanstatus -flags complex [format %llx $stmt] 0] + lappend ::trace_explain $A(zExplain) + } + db trace_v2 trace + + set ::trace_explain [list] + do_execsql_test 5.1 { + DELETE FROM t1 WHERE x=1; + } + + do_test 5.2 { + set ::trace_explain + } {{SCAN t1} {SCAN t1} {SCAN t1}} +} + +#------------------------------------------------------------------------- +reset_db +sqlite3_db_config db STMT_SCANSTATUS 1 + +do_execsql_test 6.0 { + CREATE TABLE t1(a, b); + INSERT INTO t1 VALUES(1, 'one'); + INSERT INTO t1 VALUES(2, 'two'); + INSERT INTO t1 VALUES(3, 'three'); + INSERT INTO t1 VALUES(4, 'four'); + INSERT INTO t1 VALUES(5, 'five'); + INSERT INTO t1 VALUES(6, 'six'); + INSERT INTO t1 VALUES(7, 'seven'); + INSERT INTO t1 VALUES(8, 'eight'); +} + +do_graph_test 6.1 { + SELECT (a % 2), group_concat(b) FROM t1 GROUP BY 1 +} { +QUERY (nCycle=nnn) +--SCAN t1 (nCycle=nnn) +--USE TEMP B-TREE FOR GROUP BY (nCycle=nnn) +} + +set sql { + WITH xy(x, y) AS ( SELECT (a % 2), group_concat(b) FROM t1 GROUP BY 1) + SELECT * FROM xy WHERE x=1 +} +do_graph_test 6.2 $sql { +QUERY (nCycle=nnn) +--CO-ROUTINE xy +----SCAN t1 (nCycle=nnn) +----USE TEMP B-TREE FOR GROUP BY (nCycle=nnn) +--SCAN xy (nCycle=nnn) +} + +do_graph_test 6.3 { + WITH xy(x, y) AS ( SELECT (a % 2), group_concat(b) FROM t1 GROUP BY 1) + SELECT * FROM xy, xy AS xy2 +} { +QUERY (nCycle=nnn) +--MATERIALIZE xy (nCycle=nnn) +----SCAN t1 (nCycle=nnn) +----USE TEMP B-TREE FOR GROUP BY (nCycle=nnn) +--SCAN xy (nCycle=nnn) +--SCAN xy2 (nCycle=nnn) +} + +#------------------------------------------------------------------------- +reset_db + +# Check that an OOB parameter (45) does not cause asan or valgrind errors. +# +do_test 7.0 { + db eval {SELECT * FROM sqlite_schema} + set stmt [db version -last-stmt-ptr] + sqlite3_stmt_scanstatus -flags complex $stmt 1000000 +} {} + +#explain_i { SELECT (a % 2), group_concat(b) FROM t1 GROUP BY 1 } +#puts_debug_info { SELECT (a % 2), group_concat(b) FROM t1 GROUP BY 1 } + +finish_test diff --git a/test/schema.test b/test/schema.test index afca39ed60..c7daef20ba 100644 --- a/test/schema.test +++ b/test/schema.test @@ -209,17 +209,29 @@ ifcapable utf16 { #--------------------------------------------------------------------- # Tests 8.1 and 8.2 check that prepared statements are invalidated when -# the authorization function is set. +# the authorization function is set to a non-null function. Tests 8.11 +# and 8.12 verify that no invalidations occur when the authorizer is +# cleared. # ifcapable auth { + proc noop_auth {args} {return SQLITE_OK} do_test schema-8.1 { set ::STMT [sqlite3_prepare $::DB {SELECT * FROM sqlite_master} -1 TAIL] - db auth {} + db auth noop_auth sqlite3_step $::STMT } {SQLITE_ERROR} - do_test schema-8.3 { + do_test schema-8.2 { sqlite3_finalize $::STMT } {SQLITE_SCHEMA} + do_test schema-8.11 { + set ::STMT [sqlite3_prepare $::DB {SELECT * FROM sqlite_master} -1 TAIL] + db auth {} + sqlite3_step $::STMT + } {SQLITE_ROW} + do_test schema-8.12 { + sqlite3_finalize $::STMT + } {SQLITE_OK} + } #--------------------------------------------------------------------- diff --git a/test/schema3.test b/test/schema3.test index ba7d745eb7..39d4632330 100644 --- a/test/schema3.test +++ b/test/schema3.test @@ -16,6 +16,12 @@ source $testdir/tester.tcl source $testdir/malloc_common.tcl source $testdir/lock_common.tcl +# If SQLITE_OMIT_ALTERTABLE is defined, omit this file. +ifcapable !altertable { + finish_test + return +} + # This block tests that if one client modifies the database schema, a # second client updates its internal cache of the database schema before # executing any queries. Specifically, it does not return a "no such column" diff --git a/test/schema4.test b/test/schema4.test index 6618d75388..9b794ee555 100644 --- a/test/schema4.test +++ b/test/schema4.test @@ -149,7 +149,7 @@ ifcapable altertable { } {} do_execsql_test schema4-2.8 { - select sql from sqlite_temp_master WHERE type='table'; + select sql from temp.sqlite_master WHERE type='table'; } {{CREATE TABLE x1(x)}} do_execsql_test schema4-2.7 { ALTER TABLE tbl RENAME TO tbl2 } {} diff --git a/test/schema6.test b/test/schema6.test new file mode 100644 index 0000000000..0eb8a1c434 --- /dev/null +++ b/test/schema6.test @@ -0,0 +1,164 @@ +# 2017-07-30 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# This file implements tests to show that certain CREATE TABLE statements +# generate identical database files. For example, changes in identifier +# names, white-space, and formatting of the CREATE TABLE statement should +# produce identical table content. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set ::testprefix schema6 +do_not_use_codec + +# Command: check_same_database_content TESTNAME SQL1 SQL2 SQL3 ... +# +# This command creates fresh databases using SQL1 and subsequent arguments +# and checks to make sure the content of all database files is byte-for-byte +# identical. Page 1 of the database files is allowed to be different, since +# page 1 contains the sqlite_master table which is expected to vary. +# +proc check_same_database_content {basename args} { + set i 0 + set hash {} + foreach sql $args { + catch {db close} + forcedelete test.db + sqlite3 db test.db + db eval $sql + set pgsz [db one {PRAGMA page_size}] + db close + set sz [file size test.db] + set thishash [md5file test.db $pgsz [expr {$sz-$pgsz}]] + if {$i==0} { + set hash $thishash + } else { + do_test $basename-$i "set x $thishash" $hash + } + incr i + } +} + +# Command: check_different_database_content TESTNAME SQL1 SQL2 SQL3 ... +# +# This command creates fresh databases using SQL1 and subsequent arguments +# and checks to make sure the content of all database files is different +# in ways other than on page 1. +# +proc check_different_database_content {basename args} { + set i 0 + set hashes {} + foreach sql $args { + forcedelete test.db + sqlite3 db test.db + db eval $sql + set pgsz [db one {PRAGMA page_size}] + db close + set sz [file size test.db] + set thishash [md5file test.db $pgsz [expr {$sz-$pgsz}]] + set j [lsearch $hashes $thishash] + if {$j>=0} { + do_test $basename-$i "set x {$i is the same as $j}" "All are different" + } else { + do_test $basename-$i "set x {All are different}" "All are different" + } + lappend hashes $thishash + incr i + } +} + +check_same_database_content 100 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b UNIQUE); + INSERT INTO t1(a,b) VALUES(123,'Four score and seven years ago...'); +} { + CREATE TABLE t1(xyz INTEGER, abc, PRIMARY KEY(xyz), UNIQUE(abc)); + INSERT INTO t1(xyz,abc) VALUES(123,'Four score and seven years ago...'); +} { + CREATE TABLE t1(xyz INTEGER, abc, UNIQUE(abc), PRIMARY KEY(xyz)); + INSERT INTO t1(xyz,abc) VALUES(123,'Four score and seven years ago...'); +} { + CREATE TABLE t1(a INTEGER PRIMARY KEY ASC, b UNIQUE); + INSERT INTO t1(a,b) VALUES(123,'Four score and seven years ago...'); +} { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b); + CREATE UNIQUE INDEX t1b ON t1(b); + INSERT INTO t1(a,b) VALUES(123,'Four score and seven years ago...'); +} { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b); + INSERT INTO t1(a,b) VALUES(123,'Four score and seven years ago...'); + CREATE UNIQUE INDEX t1b ON t1(b); +} + +check_same_database_content 110 { + CREATE TABLE t1(a INTEGER PRIMARY KEY UNIQUE, b UNIQUE); + INSERT INTO t1(a,b) VALUES(123,'Four score and seven years ago...'); +} { + CREATE TABLE t1(a INTEGER UNIQUE PRIMARY KEY, b UNIQUE); + INSERT INTO t1(a,b) VALUES(123,'Four score and seven years ago...'); +} { + CREATE TABLE t1(a INTEGER UNIQUE PRIMARY KEY, b UNIQUE, UNIQUE(a)); + INSERT INTO t1(a,b) VALUES(123,'Four score and seven years ago...'); +} { + CREATE TABLE t1(a INTEGER UNIQUE PRIMARY KEY, b); + CREATE UNIQUE INDEX t1b ON t1(b); + INSERT INTO t1(a,b) VALUES(123,'Four score and seven years ago...'); +} { + CREATE TABLE t1(a INTEGER UNIQUE PRIMARY KEY, b); + INSERT INTO t1(a,b) VALUES(123,'Four score and seven years ago...'); + CREATE UNIQUE INDEX t1b ON t1(b); +} + +check_same_database_content 120 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b UNIQUE) WITHOUT ROWID; + INSERT INTO t1(a,b) VALUES(123,'Four score and seven years ago...'); +} { + CREATE TABLE t1(xyz INTEGER, abc, PRIMARY KEY(xyz), UNIQUE(abc))WITHOUT ROWID; + INSERT INTO t1(xyz,abc) VALUES(123,'Four score and seven years ago...'); +} { + CREATE TABLE t1(xyz INTEGER, abc, UNIQUE(abc), PRIMARY KEY(xyz))WITHOUT ROWID; + INSERT INTO t1(xyz,abc) VALUES(123,'Four score and seven years ago...'); +} { + CREATE TABLE t1(a INTEGER PRIMARY KEY ASC, b UNIQUE) WITHOUT ROWID; + INSERT INTO t1(a,b) VALUES(123,'Four score and seven years ago...'); +} { + CREATE TABLE t1(a INTEGER PRIMARY KEY UNIQUE, b UNIQUE) WITHOUT ROWID; + INSERT INTO t1(a,b) VALUES(123,'Four score and seven years ago...'); +} { + CREATE TABLE t1(a INTEGER UNIQUE PRIMARY KEY, b UNIQUE) WITHOUT ROWID; + INSERT INTO t1(a,b) VALUES(123,'Four score and seven years ago...'); +} { + CREATE TABLE t1(a INTEGER UNIQUE PRIMARY KEY, b UNIQUE, UNIQUE(a)) + WITHOUT ROWID; + INSERT INTO t1(a,b) VALUES(123,'Four score and seven years ago...'); +} { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b) WITHOUT ROWID; + CREATE UNIQUE INDEX t1b ON t1(b); + INSERT INTO t1(a,b) VALUES(123,'Four score and seven years ago...'); +} { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b) WITHOUT ROWID; + INSERT INTO t1(a,b) VALUES(123,'Four score and seven years ago...'); + CREATE UNIQUE INDEX t1b ON t1(b); +} + +check_different_database_content 130 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b UNIQUE); + INSERT INTO t1(a,b) VALUES(123,'Four score and seven years ago...'); +} { + CREATE TABLE t1(a INTEGER PRIMARY KEY UNIQUE, b UNIQUE); + INSERT INTO t1(a,b) VALUES(123,'Four score and seven years ago...'); +} { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b UNIQUE) WITHOUT ROWID; + INSERT INTO t1(a,b) VALUES(123,'Four score and seven years ago...'); +} + + +finish_test diff --git a/test/schemafault.test b/test/schemafault.test new file mode 100644 index 0000000000..a04c78a0b4 --- /dev/null +++ b/test/schemafault.test @@ -0,0 +1,31 @@ +# 2018-08-19 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# Test OOM injection in schema-related operations. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/malloc_common.tcl +set testprefix schemafault + +do_execsql_test 1.0 { + CREATE TABLE t2(aaa INTTT); + CREATE VIEW v2(xxx , yyy) AS SELECT aaa, aaa+1 FROM t2; +} + +do_faultsim_test 1 -faults oom-* -prep { +} -body { + execsql { SELECT * FROM v2 } +} -test { + faultsim_test_result {0 {}} +} + +finish_test diff --git a/test/securedel.test b/test/securedel.test index 7111e08e71..8323a30497 100644 --- a/test/securedel.test +++ b/test/securedel.test @@ -17,8 +17,12 @@ source $testdir/tester.tcl unset -nocomplain DEFAULT_SECDEL set DEFAULT_SECDEL 0 -ifcapable secure_delete { - set DEFAULT_SECDEL 1 +ifcapable fast_secure_delete { + set DEFAULT_SECDEL 2 +} else { + ifcapable secure_delete { + set DEFAULT_SECDEL 1 + } } @@ -52,6 +56,30 @@ do_test securedel-1.4 { PRAGMA db2.secure_delete; } } {1 1} +do_test securedel-1.5 { + db eval { + PRAGMA secure_delete=FAST; + PRAGMA db2.secure_delete; + } +} {2 2} +do_test securedel-1.6 { + db eval { + PRAGMA secure_delete=ON; + PRAGMA db2.secure_delete; + } +} {1 1} +do_test securedel-1.7 { + db eval { + PRAGMA main.secure_delete=FAST; + PRAGMA db2.secure_delete; + } +} {2 1} +do_test securedel-1.8 { + db eval { + PRAGMA main.secure_delete=ON; + PRAGMA db2.secure_delete; + } +} {1 1} do_test securedel-2.1 { db eval { diff --git a/test/seekscan1.test b/test/seekscan1.test new file mode 100644 index 0000000000..0e53c19166 --- /dev/null +++ b/test/seekscan1.test @@ -0,0 +1,68 @@ +# 2022 October 7 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix seekscan1 + +do_execsql_test 1.0 { + CREATE TABLE t1(a TEXT, b INT, c INT NOT NULL, PRIMARY KEY(a,b,c)); + WITH RECURSIVE c(x) AS (VALUES(0) UNION ALL SELECT x+1 FROM c WHERE x<1997) + INSERT INTO t1(a,b,c) SELECT printf('xyz%d',x/10),x/6,x FROM c; + INSERT INTO t1 VALUES('abc',234,6); + INSERT INTO t1 VALUES('abc',345,7); + ANALYZE; +} + + +do_execsql_test 1.1 { + SELECT a,b,c FROM t1 + WHERE b IN (234, 345) AND c BETWEEN 6 AND 6.5 AND a='abc' + ORDER BY a, b; +} { + abc 234 6 +} + +do_execsql_test 1.2 { + SELECT a,b,c FROM t1 + WHERE b IN (234, 345) AND c BETWEEN 6 AND 7 AND a='abc' + ORDER BY a, b; +} { + abc 234 6 + abc 345 7 +} + +do_execsql_test 1.3 { + SELECT a,b,c FROM t1 + WHERE b IN (234, 345) AND c >=6 AND a='abc' + ORDER BY a, b; +} { + abc 234 6 + abc 345 7 +} + +do_execsql_test 1.4 { + SELECT a,b,c FROM t1 + WHERE b IN (234, 345) AND c<=7 AND a='abc' + ORDER BY a, b; +} { + abc 234 6 + abc 345 7 +} + +do_execsql_test 1.5 { + SELECT a,b,c FROM t1 WHERE b IN (235, 345) AND c<=3 AND a='abc' ORDER BY a, b; +} + + +finish_test diff --git a/test/select1.test b/test/select1.test index 4d6c07f2d0..44e63d252d 100644 --- a/test/select1.test +++ b/test/select1.test @@ -545,14 +545,14 @@ do_test select1-6.9.7 { set x [execsql2 { SELECT * FROM test1 a, (select 5, 6) LIMIT 1 }] - regsub -all {sq_[0-9a-fA-F_]+} $x {subquery} x + regsub -all {subquery-\d+} $x {subquery-0} x set x -} {a.f1 11 a.f2 22 sqlite_subquery.5 5 sqlite_subquery.6 6} +} {a.f1 11 a.f2 22 (subquery-0).5 5 (subquery-0).6 6} do_test select1-6.9.8 { set x [execsql2 { SELECT * FROM test1 a, (select 5 AS x, 6 AS y) AS b LIMIT 1 }] - regsub -all {subquery_[0-9a-fA-F]+_} $x {subquery} x + regsub -all {subquery-\d+} $x {subquery-0} x set x } {a.f1 11 a.f2 22 b.x 5 b.y 6} do_test select1-6.9.9 { @@ -688,7 +688,7 @@ do_test select1-7.2 { do_test select1-7.3 { set v [catch {execsql {SELECT f1 FROM test1 as 'hi', test2 as}} msg] lappend v $msg -} {1 {near "as": syntax error}} +} {1 {incomplete input}} do_test select1-7.4 { set v [catch {execsql { SELECT f1 FROM test1 ORDER BY; @@ -1080,5 +1080,134 @@ do_test select1-16.1 { do_catchsql_test select1-16.2 { SELECT 1 FROM sqlite_master LIMIT 1,#1; } {1 {near "#1": syntax error}} - + +# 2019-01-16 Chromium bug 922312 +# Sorting with a LIMIT clause using SRT_EphemTab and SRT_Table +# +do_execsql_test select1-17.1 { + DROP TABLE IF EXISTS t1; + DROP TABLE IF EXISTS t2; + CREATE TABLE t1(x); INSERT INTO t1 VALUES(1); + CREATE TABLE t2(y,z); INSERT INTO t2 VALUES(2,3); + CREATE INDEX t2y ON t2(y); + SELECT * FROM t1,(SELECT * FROM t2 WHERE y=2 ORDER BY y,z); +} {1 2 3} +do_execsql_test select1-17.2 { + SELECT * FROM t1,(SELECT * FROM t2 WHERE y=2 ORDER BY y,z LIMIT 4); +} {1 2 3} +do_execsql_test select1-17.3 { + SELECT * FROM t1,(SELECT * FROM t2 WHERE y=2 + UNION ALL SELECT * FROM t2 WHERE y=3 ORDER BY y,z LIMIT 4); +} {1 2 3} + +# 2019-07-24 Ticket https://sqlite.org/src/tktview/c52b09c7f38903b1311 +# +do_execsql_test select1-18.1 { + DROP TABLE IF EXISTS t1; + DROP TABLE IF EXISTS t2; + CREATE TABLE t1(c); + CREATE TABLE t2(x PRIMARY KEY, y); + INSERT INTO t1(c) VALUES(123); + INSERT INTO t2(x) VALUES(123); + SELECT x FROM t2, t1 WHERE x BETWEEN c AND null OR x AND + x IN ((SELECT x FROM (SELECT x FROM t2, t1 + WHERE x BETWEEN (SELECT x FROM (SELECT x COLLATE rtrim + FROM t2, t1 WHERE x BETWEEN c AND null + OR x AND x IN (c)), t1 WHERE x BETWEEN c AND null + OR x AND x IN (c)) AND null + OR NOT EXISTS(SELECT -4.81 FROM t1, t2 WHERE x BETWEEN c AND null + OR x AND x IN ((SELECT x FROM (SELECT x FROM t2, t1 + WHERE x BETWEEN (SELECT x FROM (SELECT x BETWEEN c AND null + OR x AND x IN (c)), t1 WHERE x BETWEEN c AND null + OR x AND x IN (c)) AND null + OR x AND x IN (c)), t1 WHERE x BETWEEN c AND null + OR x AND x IN (c)))) AND x IN (c) + ), t1 WHERE x BETWEEN c AND null + OR x AND x IN (c))); +} {} +do_execsql_test select1-18.2 { + DROP TABLE IF EXISTS t1; + DROP TABLE IF EXISTS t2; + CREATE TABLE t1(c); + CREATE TABLE t2(x PRIMARY KEY, y); + INSERT INTO t1(c) VALUES(123); + INSERT INTO t2(x) VALUES(123); + SELECT x FROM t2, t1 WHERE x BETWEEN c AND (c+1) OR x AND + x IN ((SELECT x FROM (SELECT x FROM t2, t1 + WHERE x BETWEEN (SELECT x FROM (SELECT x COLLATE rtrim + FROM t2, t1 WHERE x BETWEEN c AND (c+1) + OR x AND x IN (c)), t1 WHERE x BETWEEN c AND (c+1) + OR x AND x IN (c)) AND (c+1) + OR NOT EXISTS(SELECT -4.81 FROM t1, t2 WHERE x BETWEEN c AND (c+1) + OR x AND x IN ((SELECT x FROM (SELECT x FROM t2, t1 + WHERE x BETWEEN (SELECT x FROM (SELECT x BETWEEN c AND (c+1) + OR x AND x IN (c)), t1 WHERE x BETWEEN c AND (c+1) + OR x AND x IN (c)) AND (c+1) + OR x AND x IN (c)), t1 WHERE x BETWEEN c AND (c+1) + OR x AND x IN (c)))) AND x IN (c) + ), t1 WHERE x BETWEEN c AND (c+1) + OR x AND x IN (c))); +} {123} +do_execsql_test select1-18.3 { + SELECT 1 FROM t1 WHERE ( + SELECT 2 FROM t2 WHERE ( + SELECT 3 FROM ( + SELECT x FROM t2 WHERE x=c OR x=(SELECT x FROM (VALUES(0))) + ) WHERE x>c OR x=c + ) + ); +} {1} +do_execsql_test select1-18.4 { + SELECT 1 FROM t1, t2 WHERE ( + SELECT 3 FROM ( + SELECT x FROM t2 WHERE x=c OR x=(SELECT x FROM (VALUES(0))) + ) WHERE x>c OR x=c + ); +} {1} + +# 2019-12-17 gramfuzz find +# +do_execsql_test select1-19.10 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(x); +} {} +do_catchsql_test select1-19.20 { + INSERT INTO t1 + SELECT 1,2,3,4,5,6,7 + UNION ALL SELECT 1,2,3,4,5,6,7 + ORDER BY 1; +} {1 {table t1 has 1 columns but 7 values were supplied}} +do_catchsql_test select1-19.21 { + INSERT INTO t1 + SELECT 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 + UNION ALL SELECT 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 + ORDER BY 1; +} {1 {table t1 has 1 columns but 15 values were supplied}} + +# 2020-01-01 Found by Yongheng's fuzzer +# +reset_db +do_execsql_test select1-20.10 { + CREATE TABLE t1 ( + a INTEGER PRIMARY KEY, + b AS('Y') UNIQUE + ); + INSERT INTO t1(a) VALUES (10); + SELECT * FROM t1 JOIN t1 USING(a,b) + WHERE ((SELECT t1.a FROM t1 AS x GROUP BY b) AND b=0) + OR a = 10; +} {10 Y} +do_execsql_test select1-20.20 { + SELECT ifnull(a, max((SELECT 123))), count(a) FROM t1 ; +} {10 1} + +# 2020-10-02 dbsqlfuzz find +reset_db +do_execsql_test select1-21.1 { + CREATE TABLE t1(a IMTEGES PRIMARY KEY,R); + CREATE TABLE t2(x UNIQUE); + CREATE VIEW v1a(z,y) AS SELECT x IS NULL, x FROM t2; + SELECT a,(+a)b,(+a)b,(+a)b,NOT EXISTS(SELECT null FROM t2),CASE z WHEN 487 THEN 992 WHEN 391 THEN 203 WHEN 10 THEN '?k<D Q' END,'' FROM t1 LEFT JOIN v1a ON z=b; +} {} + finish_test diff --git a/test/select3.test b/test/select3.test index b5dbef5fb4..ab16ab9fd8 100644 --- a/test/select3.test +++ b/test/select3.test @@ -118,11 +118,18 @@ do_test select3-2.14 { } {1 {near ";": syntax error}} # Cannot have a HAVING without a GROUP BY +# +# Update: As of 3.39.0, you can. # -do_test select3-3.1 { - set v [catch {execsql {SELECT log, count(*) FROM t1 HAVING log>=4}} msg] - lappend v $msg -} {1 {a GROUP BY clause is required before HAVING}} +do_execsql_test select3-3.1 { + SELECT log, count(*) FROM t1 HAVING log>=4 +} {} +do_execsql_test select3-3.2 { + SELECT count(*) FROM t1 HAVING log>=4 +} {} +do_execsql_test select3-3.3 { + SELECT count(*) FROM t1 HAVING log!=400 +} {31} # Toss in some HAVING clauses # @@ -261,4 +268,169 @@ do_test select3-8.2 { } } {real} +# 2019-05-09 ticket https://sqlite.org/src/tktview/6c1d3febc00b22d457c7 +# +unset -nocomplain x +foreach {id x} { + 100 127 + 101 128 + 102 -127 + 103 -128 + 104 -129 + 110 32767 + 111 32768 + 112 -32767 + 113 -32768 + 114 -32769 + 120 2147483647 + 121 2147483648 + 122 -2147483647 + 123 -2147483648 + 124 -2147483649 + 130 140737488355327 + 131 140737488355328 + 132 -140737488355327 + 133 -140737488355328 + 134 -140737488355329 + 140 9223372036854775807 + 141 -9223372036854775807 + 142 -9223372036854775808 + 143 9223372036854775806 + 144 9223372036854775805 + 145 -9223372036854775806 + 146 -9223372036854775805 + +} { + set x [expr {$x+0}] + do_execsql_test select3-8.$id { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1 (c0, c1 REAL PRIMARY KEY); + INSERT INTO t1(c0, c1) VALUES (0, $x), (0, 0); + UPDATE t1 SET c0 = NULL; + UPDATE OR REPLACE t1 SET c1 = 1; + SELECT DISTINCT * FROM t1 WHERE (t1.c0 IS NULL); + PRAGMA integrity_check; + } {{} 1.0 ok} +} + +# 2020-03-10 ticket e0c2ad1aa8a9c691 +reset_db +do_execsql_test select3-9.100 { + CREATE TABLE t0(c0 REAL, c1 REAL GENERATED ALWAYS AS (c0)); + INSERT INTO t0(c0) VALUES (1); + SELECT * FROM t0 GROUP BY c0; +} {1.0 1.0} + +reset_db +do_execsql_test select3.10.100 { + CREATE TABLE t1(a, b); + CREATE TABLE t2(c, d); + SELECT max(t1.a), + (SELECT 'xyz' FROM (SELECT * FROM t2 WHERE 0) WHERE t1.b=1) + FROM t1; +} {{} {}} + +#------------------------------------------------------------------------- +# dbsqlfuzz crash-8e17857db2c5a9294c975123ac807156a6559f13.txt +# Associated with the flatten-left-join branch circa 2022-06-23. +# +foreach {tn sql} { + 1 { + CREATE TABLE t1(a TEXT); + CREATE TABLE t2(x INT); + CREATE INDEX t2x ON t2(x); + INSERT INTO t1 VALUES('abc'); + } + 2 { + CREATE TABLE t1(a TEXT); + CREATE TABLE t2(x INT); + INSERT INTO t1 VALUES('abc'); + } + 3 { + CREATE TABLE t1(a TEXT); + CREATE TABLE t2(x INT); + INSERT INTO t1 VALUES('abc'); + PRAGMA automatic_index=OFF; + } +} { + reset_db + do_execsql_test select3-11.$tn.1 $sql + do_execsql_test select3.11.$tn.2 { + SELECT max(a), val FROM t1 LEFT JOIN ( + SELECT 'constant' AS val FROM t2 WHERE x=1234 + ) + } {abc {}} + do_execsql_test select3.11.$tn.3 { + INSERT INTO t2 VALUES(123); + SELECT max(a), val FROM t1 LEFT JOIN ( + SELECT 'constant' AS val FROM t2 WHERE x=1234 + ) + } {abc {}} + do_execsql_test select3.11.$tn.4 { + INSERT INTO t2 VALUES(1234); + SELECT max(a), val FROM t1 LEFT JOIN ( + SELECT 'constant' AS val FROM t2 WHERE x=1234 + ) + } {abc constant} +} + +reset_db +do_execsql_test 12.0 { + CREATE TABLE t1(a); + CREATE TABLE t2(x); +} +do_execsql_test 12.1 { + SELECT count(x), m FROM t1 LEFT JOIN (SELECT x, 59 AS m FROM t2) GROUP BY a; +} +do_execsql_test 12.2 { + INSERT INTO t1 VALUES(1), (1), (2), (3); + SELECT count(x), m FROM t1 LEFT JOIN (SELECT x, 59 AS m FROM t2) GROUP BY a; +} { + 0 {} + 0 {} + 0 {} +} +do_execsql_test 12.3 { + INSERT INTO t2 VALUES(45); + SELECT count(x), m FROM t1 LEFT JOIN (SELECT x, 59 AS m FROM t2) GROUP BY a; +} { + 2 59 + 1 59 + 1 59 +} +do_execsql_test 12.4 { + INSERT INTO t2 VALUES(210); + SELECT count(x), m FROM t1 LEFT JOIN (SELECT x, 59 AS m FROM t2) GROUP BY a; +} { + 4 59 + 2 59 + 2 59 +} +do_execsql_test 12.5 { + INSERT INTO t2 VALUES(NULL); + SELECT count(x), m FROM t1 LEFT JOIN (SELECT x, 59 AS m FROM t2) GROUP BY a; +} { + 4 59 + 2 59 + 2 59 +} +do_execsql_test 12.6 { + DELETE FROM t2; + DELETE FROM t1; + INSERT INTO t1 VALUES('value'); + INSERT INTO t2 VALUES('hello'); +} {} +do_execsql_test 12.7 { + SELECT group_concat(x), m FROM t1 + LEFT JOIN (SELECT x, 59 AS m FROM t2) GROUP BY a; +} { + hello 59 +} +do_execsql_test 12.8 { + SELECT group_concat(x), m, n FROM t1 + LEFT JOIN (SELECT x, 59 AS m, 60 AS n FROM t2) GROUP BY a; +} { + hello 59 60 +} + finish_test diff --git a/test/select4.test b/test/select4.test index be8d0e0ab1..890897f2a9 100644 --- a/test/select4.test +++ b/test/select4.test @@ -12,7 +12,6 @@ # focus of this file is testing UNION, INTERSECT and EXCEPT operators # in SELECT statements. # -# $Id: select4.test,v 1.30 2009/04/16 00:24:24 drh Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl @@ -916,7 +915,7 @@ do_execsql_test select4-14.17 { VALUES(1),(2),(3),(4) UNION ALL SELECT 5 LIMIT 3; } {1 2 3} -# Ticket https://www.sqlite.org/src/info/d06a25c84454a372 +# Ticket https://sqlite.org/src/info/d06a25c84454a372 # Incorrect answer due to two co-routines using the same registers and expecting # those register values to be preserved across a Yield. # @@ -936,4 +935,109 @@ do_execsql_test select4-15.1 { ORDER BY 1; } {1 33 456 2 33 789} +# Enhancement (2016-03-15): Use a co-routine for subqueries if the +# subquery is guaranteed to be the outer-most query +# +do_execsql_test select4-16.1 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z, + PRIMARY KEY(a,b DESC)) WITHOUT ROWID; + + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<100) + INSERT INTO t1(a,b,c,d) + SELECT x%10, x/10, x, printf('xyz%dabc',x) FROM c; + + SELECT t3.c FROM + (SELECT a,max(b) AS m FROM t1 WHERE a>=5 GROUP BY a) AS t2 + JOIN t1 AS t3 + WHERE t2.a=t3.a AND t2.m=t3.b + ORDER BY t3.a; +} {95 96 97 98 99} +do_execsql_test select4-16.2 { + SELECT t3.c FROM + (SELECT a,max(b) AS m FROM t1 WHERE a>=5 GROUP BY a) AS t2 + CROSS JOIN t1 AS t3 + WHERE t2.a=t3.a AND t2.m=t3.b + ORDER BY t3.a; +} {95 96 97 98 99} +do_execsql_test select4-16.3 { + SELECT t3.c FROM + (SELECT a,max(b) AS m FROM t1 WHERE a>=5 GROUP BY a) AS t2 + LEFT JOIN t1 AS t3 + WHERE t2.a=t3.a AND t2.m=t3.b + ORDER BY t3.a; +} {95 96 97 98 99} + +# Ticket https://sqlite.org/src/tktview/f7f8c97e975978d45 on 2016-04-25 +# +# The where push-down optimization from 2015-06-02 is suppose to disable +# on aggregate subqueries. But if the subquery is a compound where the +# last SELECT is non-aggregate but some other SELECT is an aggregate, the +# test is incomplete and the optimization is not properly disabled. +# +# The following test cases verify that the fix works. +# +do_execsql_test select4-17.1 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a int, b int); + INSERT INTO t1 VALUES(1,2),(1,18),(2,19); + SELECT x, y FROM ( + SELECT 98 AS x, 99 AS y + UNION + SELECT a AS x, sum(b) AS y FROM t1 GROUP BY a + ) AS w WHERE y>=20 + ORDER BY +x; +} {1 20 98 99} +do_execsql_test select4-17.2 { + SELECT x, y FROM ( + SELECT a AS x, sum(b) AS y FROM t1 GROUP BY a + UNION + SELECT 98 AS x, 99 AS y + ) AS w WHERE y>=20 + ORDER BY +x; +} {1 20 98 99} +do_catchsql_test select4-17.3 { + SELECT x, y FROM ( + SELECT a AS x, sum(b) AS y FROM t1 GROUP BY a LIMIT 3 + UNION + SELECT 98 AS x, 99 AS y + ) AS w WHERE y>=20 + ORDER BY +x; +} {1 {LIMIT clause should come after UNION not before}} + +# 2020-04-03 ticket 51166be0159fd2ce from Yong Heng. +# Adverse interaction between the constant propagation and push-down +# optimizations. +# +reset_db +do_execsql_test select4-18.1 { + CREATE VIEW v0(v0) AS WITH v0 AS(SELECT 0 v0) SELECT(SELECT min(v0) OVER()) FROM v0 GROUP BY v0; + SELECT *FROM v0 v1 JOIN v0 USING(v0) WHERE datetime(v0) = (v0.v0)AND v0 = 10; +} {} +do_execsql_test select4-18.2 { + CREATE VIEW t1(aa) AS + WITH t2(bb) AS (SELECT 123) + SELECT (SELECT min(bb) OVER()) FROM t2 GROUP BY bb; + SELECT * FROM t1; +} {123} +do_execsql_test select4-18.3 { + SELECT * FROM t1 AS z1 JOIN t1 AS z2 USING(aa) + WHERE abs(z1.aa)=z2.aa AND z1.aa=123; +} {123} + +# 2021-03-31 Fix an assert() problem in the logic at the end of sqlite3Select() +# that validates AggInfo. The checks to ensure that AggInfo.aCol[].pCExpr +# references a valid expression was looking at an expression that had been +# deleted by the truth optimization in sqlite3ExprAnd() which was invoked by +# the push-down optimization. This is harmless in delivery builds, as that code +# only runs with SQLITE_DEBUG. But it should still be fixed. The problem +# was discovered by dbsqlfuzz (crash-dece7b67a3552ed7e571a7bda903afd1f7bd9b21) +# +reset_db +do_execsql_test select4-19.1 { + CREATE TABLE t1(x); + INSERT INTO t1 VALUES(99); + SELECT sum((SELECT 1 FROM (SELECT 2 WHERE x IS NULL) WHERE 0)) FROM t1; +} {{}} + finish_test diff --git a/test/select5.test b/test/select5.test index 3a787fc767..8de306cf40 100644 --- a/test/select5.test +++ b/test/select5.test @@ -12,7 +12,6 @@ # focus of this file is testing aggregate functions and the # GROUP BY and HAVING clauses of SELECT statements. # -# $Id: select5.test,v 1.20 2008/08/21 14:15:59 drh Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl @@ -154,7 +153,7 @@ do_test select5-5.5 { execsql { SELECT a, b FROM t2 GROUP BY a; } -} {1 4 6 4} +} {1 2 6 4} # Test rendering of columns for the GROUP BY clause. # @@ -251,7 +250,13 @@ do_test select5-8.8 { } } {two 3 one 9} +# 2021-04-26 forum https://sqlite.org/forum/forumpost/74330094d8 +reset_db +do_execsql_test select5-9.1 { + CREATE TABLE t1(a INT, b INT); + INSERT INTO t1(a,b) VALUES(1,null),(null,null),(1,null); + CREATE UNIQUE INDEX t1b ON t1(abs(b)); + SELECT quote(a), quote(b), '|' FROM t1 GROUP BY a, abs(b); +} {NULL NULL | 1 NULL |} - - finish_test diff --git a/test/select6.test b/test/select6.test index 590512a6b0..301ec11c36 100644 --- a/test/select6.test +++ b/test/select6.test @@ -12,7 +12,6 @@ # focus of this file is testing SELECT statements that contain # subqueries in their FROM clause. # -# $Id: select6.test,v 1.29 2009/01/09 01:12:28 drh Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl @@ -170,7 +169,6 @@ do_test select6-3.2 { FROM (SELECT count(*) as p , b as q FROM t2 GROUP BY q) AS a, (SELECT max(a) as r, b as s FROM t2 GROUP BY s) as b WHERE a.q=b.s ORDER BY a.q) - ORDER BY "a.q" } } {1 1 1 2 2 3 3 4 7 4 8 15 5 5 20} do_test select6-3.3 { @@ -613,5 +611,60 @@ do_execsql_test 11.100 { FROM ( SELECT count(*) AS cnt FROM t1 ); } {{}} +# 2019-05-29 ticket https://sqlite.org/src/info/c41afac34f15781f +# A LIMIT clause in a subquery is incorrectly applied to a subquery. +# +do_execsql_test 12.100 { + DROP TABLE t1; + DROP TABLE t2; + CREATE TABLE t1(a); + INSERT INTO t1 VALUES(1); + INSERT INTO t1 VALUES(2); + CREATE TABLE t2(b); + INSERT INTO t2 VALUES(3); + SELECT * FROM ( + SELECT * FROM (SELECT * FROM t1 LIMIT 1) + UNION ALL + SELECT * from t2); +} {1 3} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 13.100 { + + CREATE TABLE t1(y INT); + INSERT INTO t1 (y) VALUES (1); + + CREATE TABLE t2(x INTEGER); + INSERT INTO t2 VALUES(0); + + CREATE TABLE empty1(z); +} + +do_execsql_test 13.110 { + SELECT t1.y + FROM ( SELECT 'AAA' ) + INNER JOIN ( + SELECT 1 AS abc FROM ( + SELECT 1 FROM t2 LEFT JOIN empty1 + ) + ) AS sub0 ON sub0.abc + , t1 + RIGHT JOIN (SELECT 'BBB' FROM ( SELECT 'CCC' )) +} {1} + +do_execsql_test 13.120 { + SELECT t1.y + FROM ( SELECT 'AAA' ) + INNER JOIN ( + SELECT 1 AS abc FROM ( + SELECT 1 FROM t2 LEFT JOIN empty1 + ) + ) AS sub0 ON sub0.abc + , t1 + RIGHT JOIN (SELECT 'BBB' FROM ( SELECT 'CCC' )) + WHERE t1.y +} {1} + finish_test diff --git a/test/select7.test b/test/select7.test index 0df84e13d9..0c4051006a 100644 --- a/test/select7.test +++ b/test/select7.test @@ -114,26 +114,22 @@ ifcapable {subquery && compound} { CREATE TABLE t2(a,b); SELECT 5 IN (SELECT a,b FROM t2); } - } [list 1 \ - {only a single result allowed for a SELECT that is part of an expression}] + } {1 {sub-select returns 2 columns - expected 1}} do_test select7-5.2 { catchsql { SELECT 5 IN (SELECT * FROM t2); } - } [list 1 \ - {only a single result allowed for a SELECT that is part of an expression}] + } {1 {sub-select returns 2 columns - expected 1}} do_test select7-5.3 { catchsql { SELECT 5 IN (SELECT a,b FROM t2 UNION SELECT b,a FROM t2); } - } [list 1 \ - {only a single result allowed for a SELECT that is part of an expression}] + } {1 {sub-select returns 2 columns - expected 1}} do_test select7-5.4 { catchsql { SELECT 5 IN (SELECT * FROM t2 UNION SELECT * FROM t2); } - } [list 1 \ - {only a single result allowed for a SELECT that is part of an expression}] + } {1 {sub-select returns 2 columns - expected 1}} } # Verify that an error occurs if you have too many terms on a @@ -159,6 +155,38 @@ if {[clang_sanitize_address]==0} { } } +# https://issues.chromium.org/issues/358174302 +# Need to support an unlimited number of terms in a VALUES clause, even +# if some of those terms contain double-quoted string literals. +# +do_execsql_test select7-6.5 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a,b,c); +} +sqlite3_limit db SQLITE_LIMIT_COMPOUND_SELECT 10 +sqlite3_db_config db SQLITE_DBCONFIG_DQS_DML 0 +do_catchsql_test select7-6.6 { + INSERT INTO t1 VALUES + (NULL,0,""), (X'',0.0,0.0), (X'',X'',""), (0.0,0.0,""), (NULL,NULL,0.0), + (0,"",0), (0.0,X'',0), ("",X'',0.0), (0.0,X'',NULL), (0,NULL,""), + (0,"",NULL), (0.0,NULL,X''), ("",X'',NULL), (NULL,0,""), + (0,NULL,0), (X'',X'',0.0); +} {1 {no such column: "" - should this be a string literal in single-quotes?}} +do_execsql_test select7-6.7 { + SELECT count(*) FROM t1; +} {0} +sqlite3_db_config db SQLITE_DBCONFIG_DQS_DML 1 +do_catchsql_test select7-6.8 { + INSERT INTO t1 VALUES + (NULL,0,""), (X'',0.0,0.0), (X'',X'',""), (0.0,0.0,""), (NULL,NULL,0.0), + (0,"",0), (0.0,X'',0), ("",X'',0.0), (0.0,X'',NULL), (0,NULL,""), + (0,"",NULL), (0.0,NULL,X''), ("",X'',NULL), (NULL,0,""), + (0,NULL,0), (X'',X'',0.0); +} {0 {}} +do_execsql_test select7-6.9 { + SELECT count(*) FROM t1; +} {16} + # This block of tests verifies that bug aa92c76cd4 is fixed. # do_test select7-7.1 { diff --git a/test/select9.test b/test/select9.test index 4c42236dc8..bbed8e18fe 100644 --- a/test/select9.test +++ b/test/select9.test @@ -436,19 +436,19 @@ do_test select9-5.1 { EXPLAIN QUERY PLAN SELECT * FROM v5 WHERE x='12345' ORDER BY y; } -} {~/SCAN TABLE/} ;# Uses indices with "*" +} {~/SCAN/} ;# Uses indices with "*" do_test select9-5.2 { db eval { EXPLAIN QUERY PLAN SELECT x, y FROM v5 WHERE x='12345' ORDER BY y; } -} {~/SCAN TABLE/} ;# Uses indices with "x, y" +} {~/SCAN/} ;# Uses indices with "x, y" do_test select9-5.3 { db eval { EXPLAIN QUERY PLAN SELECT x, y FROM v5 WHERE +x='12345' ORDER BY y; } -} {/SCAN TABLE/} ;# Full table scan if the "+x" prevents index usage. +} {/SCAN/} ;# Full table scan if the "+x" prevents index usage. # 2013-07-09: Ticket [490a4b7235624298]: # "WHERE 0" on the first element of a UNION causes an assertion fault diff --git a/test/selectA.test b/test/selectA.test index 0338338902..91b1548488 100644 --- a/test/selectA.test +++ b/test/selectA.test @@ -1336,11 +1336,14 @@ do_eqp_test 4.1.2 { SELECT a, b FROM t4 WHERE f()==f() ORDER BY 1,2 } { - 1 0 0 {SCAN TABLE t5 USING INDEX i2} - 1 0 0 {USE TEMP B-TREE FOR RIGHT PART OF ORDER BY} - 2 0 0 {SCAN TABLE t4 USING INDEX i1} - 2 0 0 {USE TEMP B-TREE FOR RIGHT PART OF ORDER BY} - 0 0 0 {COMPOUND SUBQUERIES 1 AND 2 (UNION ALL)} + QUERY PLAN + `--MERGE (UNION ALL) + |--LEFT + | |--SCAN t5 USING INDEX i2 + | `--USE TEMP B-TREE FOR LAST TERM OF ORDER BY + `--RIGHT + |--SCAN t4 USING INDEX i1 + `--USE TEMP B-TREE FOR LAST TERM OF ORDER BY } do_execsql_test 4.1.3 { @@ -1434,5 +1437,74 @@ do_catchsql_test 5.4 { SELECT * FROM t8 UNION SELECT * FROM t9 ORDER BY a+b COLLATE NOCASE } {1 {1st ORDER BY term does not match any column in the result set}} +do_execsql_test 6.1 { + DROP TABLE IF EXISTS t1; + DROP TABLE IF EXISTS t2; + CREATE TABLE t1(a INTEGER); + CREATE TABLE t2(b TEXT); + INSERT INTO t2(b) VALUES('12345'); + SELECT * FROM (SELECT a FROM t1 UNION SELECT b FROM t2) WHERE a=a; +} {12345} + +# 2020-06-15 ticket 8f157e8010b22af0 +# +reset_db +do_execsql_test 7.1 { + CREATE TABLE t1(c1); INSERT INTO t1 VALUES(12),(123),(1234),(NULL),('abc'); + CREATE TABLE t2(c2); INSERT INTO t2 VALUES(44),(55),(123); + CREATE TABLE t3(c3,c4); INSERT INTO t3 VALUES(66,1),(123,2),(77,3); + CREATE VIEW t4 AS SELECT c3 FROM t3; + CREATE VIEW t5 AS SELECT c3 FROM t3 ORDER BY c4; +} +do_execsql_test 7.2 { + SELECT * FROM t1, t2 WHERE c1=(SELECT 123 INTERSECT SELECT c2 FROM t4) AND c1=123; +} {123 123} +do_execsql_test 7.3 { + SELECT * FROM t1, t2 WHERE c1=(SELECT 123 INTERSECT SELECT c2 FROM t5) AND c1=123; +} {123 123} +do_execsql_test 7.4 { + CREATE TABLE a(b); + CREATE VIEW c(d) AS SELECT b FROM a ORDER BY b; + SELECT sum(d) OVER( PARTITION BY(SELECT 0 FROM c JOIN a WHERE b =(SELECT b INTERSECT SELECT d FROM c) AND b = 123)) FROM c; +} {} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 8.0 { + CREATE TABLE x1(x); + CREATE TABLE t1(a, b, c, d); + CREATE INDEX t1a ON t1(a); + CREATE INDEX t1b ON t1(b); +} + +do_execsql_test 8.1 { + SELECT 'ABCD' FROM t1 + WHERE (a=? OR b=?) + AND (0 OR (SELECT 'xyz' INTERSECT SELECT a ORDER BY 1)) +} {} + +#------------------------------------------------------------------------- +# dbsqlfuzz a34f455c91ad75a0cf8cd9476841903f42930a7a +# +reset_db +do_execsql_test 9.0 { + CREATE TABLE t1(a COLLATE nocase); + CREATE TABLE t2(b COLLATE nocase); + + INSERT INTO t1 VALUES('ABC'); + INSERT INTO t2 VALUES('abc'); +} + +do_execsql_test 9.1 { + SELECT a FROM t1 INTERSECT SELECT b FROM t2; +} {ABC} + +do_execsql_test 9.2 { + SELECT * FROM ( + SELECT a FROM t1 INTERSECT SELECT b FROM t2 + ) WHERE a||'' = 'ABC'; +} {ABC} + + finish_test diff --git a/test/selectC.test b/test/selectC.test index dedac41fc4..42fa1d11b8 100644 --- a/test/selectC.test +++ b/test/selectC.test @@ -14,6 +14,7 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl +set testprefix selectC # Ticket # do_test selectC-1.1 { @@ -229,8 +230,46 @@ do_execsql_test selectC-4.2 { select a from (select distinct a, b from t_distinct_bug) } {1 1 1} +do_execsql_test selectC-4.2b { + CREATE VIEW v42b AS SELECT DISTINCT a, b FROM t_distinct_bug; + SELECT a FROM v42b; +} {1 1 1} + do_execsql_test selectC-4.3 { select a, udf() from (select distinct a, b from t_distinct_bug) } {1 1 1 2 1 3} +#------------------------------------------------------------------------- +# Test that the problem in ticket #190c2507 has been fixed. +# +do_execsql_test 5.0 { + CREATE TABLE x1(a); + CREATE TABLE x2(b); + CREATE TABLE x3(c); + CREATE VIEW vvv AS SELECT b FROM x2 ORDER BY 1; + + INSERT INTO x1 VALUES('a'), ('b'); + INSERT INTO x2 VALUES(22), (23), (25), (24), (21); + INSERT INTO x3 VALUES(302), (303), (301); +} + +do_execsql_test 5.1 { + CREATE TABLE x4 AS SELECT b FROM vvv UNION ALL SELECT c from x3; + SELECT * FROM x4; +} {21 22 23 24 25 302 303 301} + +do_execsql_test 5.2 { + SELECT * FROM x1, x4 +} { + a 21 a 22 a 23 a 24 a 25 a 302 a 303 a 301 + b 21 b 22 b 23 b 24 b 25 b 302 b 303 b 301 +} + +do_execsql_test 5.3 { + SELECT * FROM x1, (SELECT b FROM vvv UNION ALL SELECT c from x3) ORDER BY 1,2; +} { + a 21 a 22 a 23 a 24 a 25 a 301 a 302 a 303 + b 21 b 22 b 23 b 24 b 25 b 301 b 302 b 303 +} + finish_test diff --git a/test/selectD.test b/test/selectD.test index 89f999eb6d..818d8ccc0b 100644 --- a/test/selectD.test +++ b/test/selectD.test @@ -169,6 +169,6 @@ do_execsql_test selectD-4.1 { WHERE x1.d>5 GROUP BY x1.d) AS x2 ON t41.b=x2.d; -} {/.*SEARCH SUBQUERY 1 AS x2 USING AUTOMATIC.*/} +} {/SEARCH x2 USING AUTOMATIC/} finish_test diff --git a/test/selectG.test b/test/selectG.test index 86d89b121b..fab4c4ed4d 100644 --- a/test/selectG.test +++ b/test/selectG.test @@ -36,4 +36,24 @@ do_test 100 { } } {100000 5000050000 50000.5 1} +# 2018-01-14. A 100K-entry VALUES clause within a scalar expression does +# not cause processor stack overflow. +# +do_test 110 { + set sql "SELECT (VALUES" + for {set i 1} {$i<100000} {incr i} { + append sql "($i)," + } + append sql "($i));" + db eval $sql +} {1} + +# Only the left-most term of a multi-valued VALUES within a scalar +# expression is evaluated. +# +do_test 120 { + set n [llength [split [db eval "explain $sql"] \n]] + expr {$n<10} +} {1} + finish_test diff --git a/test/selectH.test b/test/selectH.test new file mode 100644 index 0000000000..41f0999fe3 --- /dev/null +++ b/test/selectH.test @@ -0,0 +1,145 @@ +# 2023-02-16 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# Test cases for the omit-unused-subquery-column optimization. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix selectH + +do_execsql_test 1.1 { + CREATE TABLE t1( + c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, + c10, c11, c12, c13, c14, c15, c16, c17, c18, c19, + c20, c21, c22, c23, c24, c25, c26, c27, c28, c29, + c30, c31, c32, c33, c34, c35, c36, c37, c38, c39, + c40, c41, c42, c43, c44, c45, c46, c47, c48, c49, + c50, c51, c52, c53, c54, c55, c56, c57, c58, c59, + c60, c61, c62, c63, c64, c65 + ); + INSERT INTO t1 VALUES( + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, + 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, + 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, + 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, + 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, + 60, 61, 62, 63, 64, 65 + ); + CREATE INDEX t1c60 ON t1(c60); +} + +# The SQL counter(N) function adjusts the value of the global +# TCL variable ::selectH_cnt by the value N and returns the new +# value. By putting calls to counter(N) as unused columns in a +# view or subquery, we can check to see if the counter gets incremented, +# and if not that means that the unused column was omitted. +# +unset -nocomplain selectH_cnt +set selectH_cnt 0 +proc selectH_counter {amt} { + global selectH_cnt + incr selectH_cnt $amt + return $selectH_cnt +} +db func counter selectH_counter + +do_execsql_test 1.2 { + SELECT DISTINCT c44 FROM ( + SELECT c0 AS a, *, counter(1) FROM t1 + UNION ALL + SELECT c1 AS a, *, counter(1) FROM t1 + ) WHERE c60=60; +} {44} +do_test 1.3 { + set ::selectH_cnt +} {0} + +do_execsql_test 2.1 { + SELECT a FROM ( + SELECT counter(1) AS cnt, c15 AS a, *, c62 AS b FROM t1 + UNION ALL + SELECT counter(1) AS cnt, c16 AS a, *, c61 AS b FROM t1 + ORDER BY b + ); +} {16 15} +do_test 2.2 { + set ::selectH_cnt +} {0} + +do_execsql_test 3.1 { + CREATE VIEW v1 AS + SELECT c16 AS a, *, counter(1) AS x FROM t1 + UNION ALL + SELECT c17 AS a, *, counter(1) AS x FROM t1 + UNION ALL + SELECT c18 AS a, *, counter(1) AS x FROM t1 + UNION ALL + SELECT c19 AS a, *, counter(1) AS x FROM t1; + SELECT count(*) FROM v1 WHERE c60=60; +} {4} +do_test 3.2 { + set ::selectH_cnt +} {0} +do_execsql_test 3.3 { + SELECT count(a) FROM v1 WHERE c60=60; +} {4} +do_execsql_test 3.4 { + SELECT a FROM v1 WHERE c60=60; +} {16 17 18 19} +do_test 3.5 { + set ::selectH_cnt +} {0} +do_execsql_test 3.6 { + SELECT x FROM v1 WHERE c60=60; +} {1 2 3 4} +do_test 3.7 { + set ::selectH_cnt +} {4} + +# 2023-02-25 dbsqlfuzz bf1d3ed6e0e0dd8766027797d43db40c776d2b15 +# +do_execsql_test 4.1 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT); + SELECT 1 FROM (SELECT DISTINCT name COLLATE rtrim FROM sqlite_schema + UNION ALL SELECT a FROM t1); +} {1 1} + +do_execsql_test 4.2 { + SELECT DISTINCT name COLLATE rtrim FROM sqlite_schema + UNION ALL + SELECT a FROM t1 +} {v1 t1} + +#------------------------------------------------------------------------- +# forum post https://sqlite.org/forum/forumpost/b83c7b2168 +# +reset_db +do_execsql_test 5.0 { + CREATE TABLE t1 (val1); + INSERT INTO t1 VALUES(4); + INSERT INTO t1 VALUES(5); + CREATE TABLE t2 (val2); +} +do_execsql_test 5.1 { + SELECT DISTINCT val1 FROM t1 UNION ALL SELECT val2 FROM t2; +} { + 4 5 +} +do_execsql_test 5.2 { + SELECT count(1234) FROM ( + SELECT DISTINCT val1 FROM t1 UNION ALL SELECT val2 FROM t2 + ) +} {2} + +finish_test diff --git a/test/server1.test b/test/server1.test deleted file mode 100644 index 90673ef000..0000000000 --- a/test/server1.test +++ /dev/null @@ -1,171 +0,0 @@ -# 2006 January 09 -# -# The author disclaims copyright to this source code. In place of -# a legal notice, here is a blessing: -# -# May you do good and not evil. -# May you find forgiveness for yourself and forgive others. -# May you share freely, never taking more than you give. -# -#*********************************************************************** -# This file implements regression tests for SQLite library. The -# focus of this script is testing the server mode of SQLite. -# -# This file is derived from thread1.test -# -# $Id: server1.test,v 1.5 2007/08/29 18:20:17 drh Exp $ - - -set testdir [file dirname $argv0] -source $testdir/tester.tcl - -# Skip this whole file if the server testing code is not enabled -# -if {[llength [info command client_step]]==0 || [sqlite3 -has-codec]} { - finish_test - return -} - -# The sample server implementation does not work right when memory -# management is enabled. -# -ifcapable (memorymanage||mutex_noop) { - finish_test - return -} - -# Create some data to work with -# -do_test server1-1.1 { - execsql { - CREATE TABLE t1(a,b); - INSERT INTO t1 VALUES(1,'abcdefgh'); - INSERT INTO t1 SELECT a+1, b||b FROM t1; - INSERT INTO t1 SELECT a+2, b||b FROM t1; - INSERT INTO t1 SELECT a+4, b||b FROM t1; - SELECT count(*), max(length(b)) FROM t1; - } -} {8 64} - -# Interleave two threads on read access. Then make sure a third -# thread can write the database. In other words: -# -# read-lock A -# read-lock B -# unlock A -# unlock B -# write-lock C -# -do_test server1-1.2 { - client_create A test.db - client_create B test.db - client_create C test.db - client_compile A {SELECT a FROM t1} - client_step A - client_result A -} SQLITE_ROW -do_test server1-1.3 { - client_argc A -} 1 -do_test server1-1.4 { - client_argv A 0 -} 1 -do_test server1-1.5 { - client_compile B {SELECT b FROM t1} - client_step B - client_result B -} SQLITE_ROW -do_test server1-1.6 { - client_argc B -} 1 -do_test server1-1.7 { - client_argv B 0 -} abcdefgh -do_test server1-1.8 { - client_finalize A - client_result A -} SQLITE_OK -do_test server1-1.9 { - client_finalize B - client_result B -} SQLITE_OK -do_test server1-1.10 { - client_compile C {CREATE TABLE t2(x,y)} - client_step C - client_result C -} SQLITE_DONE -do_test server1-1.11 { - client_finalize C - client_result C -} SQLITE_OK -do_test server1-1.12 { - catchsql {SELECT name FROM sqlite_master} - execsql {SELECT name FROM sqlite_master} -} {t1 t2} - - -# Read from table t1. Do not finalize the statement. This -# will leave the lock pending. -# -do_test server1-2.1 { - client_halt * - client_create A test.db - client_compile A {SELECT a FROM t1} - client_step A - client_result A -} SQLITE_ROW - -# Read from the same table from another thread. This is allows. -# -do_test server1-2.2 { - client_create B test.db - client_compile B {SELECT b FROM t1} - client_step B - client_result B -} SQLITE_ROW - -# Write to a different table from another thread. This is allowed -# because in server mode with a shared cache we have table-level locking. -# -do_test server1-2.3 { - client_create C test.db - client_compile C {INSERT INTO t2 VALUES(98,99)} - client_step C - client_result C - client_finalize C - client_result C -} SQLITE_OK - -# But we cannot insert into table t1 because threads A and B have it locked. -# -do_test server1-2.4 { - client_compile C {INSERT INTO t1 VALUES(98,99)} - client_step C - client_result C - client_finalize C - client_result C -} SQLITE_LOCKED -do_test server1-2.5 { - client_finalize B - client_wait B - client_compile C {INSERT INTO t1 VALUES(98,99)} - client_step C - client_result C - client_finalize C - client_result C -} SQLITE_LOCKED - -# Insert into t1 is successful after finishing the other two threads. -do_test server1-2.6 { - client_finalize A - client_wait A - client_compile C {INSERT INTO t1 VALUES(98,99)} - client_step C - client_result C - client_finalize C - client_result C -} SQLITE_OK - -client_halt * -sqlite3_enable_shared_cache 0 -finish_test diff --git a/test/session.test b/test/session.test new file mode 100644 index 0000000000..4fd1ec0184 --- /dev/null +++ b/test/session.test @@ -0,0 +1,22 @@ +# 2008 June 23 +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file runs all rtree related tests. +# + +set testdir [file dirname $argv0] +source $testdir/permutations.test + +ifcapable session { + # First run tests with sqlite3_extended_error_codes() set, then + # again with it clear. + run_test_suite session_eec + run_test_suite session + run_test_suite session_strm +} + +finish_test diff --git a/test/sessionfuzz-data1.db b/test/sessionfuzz-data1.db new file mode 100644 index 0000000000..df10e10bcd Binary files /dev/null and b/test/sessionfuzz-data1.db differ diff --git a/test/sessionfuzz.c b/test/sessionfuzz.c new file mode 100644 index 0000000000..c389a5e996 --- /dev/null +++ b/test/sessionfuzz.c @@ -0,0 +1,1016 @@ +/* +** 2018-03-01 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** This file implements a program used for fuzz-testing the session +** module. +** +** Usage: +** +** sessionfuzz setup -- Generate starter test cases +** sessionfuzz run FILE ... -- Run a test fuzz on FILE +** sesssiofuzz run SQLAR ... -- Run all test cases in the SQL Archive +** +** Compiling: +** +** (1) Have a version of SQLite that supports SQLITE_ENABLE_MEMDB +** in the local directory. +** (2) Run: +** +** gcc -Wall -O3 -o sessionfuzz sessionfuzz.c -lz +** +** Use with AFL (American Fuzzy Lop - http://lcamtuf.coredump.cx/afl/) +** +** (1) ./afl-gcc -O3 -o sessionfuzz sessionfuzz.c -lz +** (2) mkdir session-init session-run session-cases +** (3) cd session-init; ../sessionfuzz setup; cd .. +** (4) ./afl -i session-init -o session-run -- ./sessionfuzz run @@ +** ... let the previous step run for a while. Weeks, maybe. +** (5) ./afl-cmin -i session-run -o session-cases +** +** The afl-cmin command on step (5) writes a minimal set of test cases +** for coverage into the session-cases directory. Gather the cases written +** there into an SQL Archive using a command like this: +** +** sqlite3 session-cases.db -Ac session-cases +** +** Then repeat the test using: +** +** ./sessionfuzz run session-cases.db +*/ + +/* +** We will import the entire SQLite source file to make compiling easier +*/ +#ifdef SQLITE_DEBUG +#undef SQLITE_DEBUG +#endif + +#ifdef SQLITE_THREADSAFE +#undef SQLITE_THREADSAFE +#endif + +#define SQLITE_DEBUG 1 +#define SQLITE_THREADSAFE 0 +#undef SQLITE_OMIT_LOAD_EXTENSION +#define SQLITE_OMIT_LOAD_EXTENSION 0 +#define SQLITE_ENABLE_SESSION 1 +#define SQLITE_ENABLE_PREUPDATE_HOOK 1 +#define SQLITE_ENABLE_DESERIALIZE 1 +#include "sqlite3.c" + +/* Code to populate the database */ +static const char zFillSql[] = + "INSERT INTO t1(a,b,c,d) VALUES\n" + " (1,2,3,4),\n" + " (2,3.5,'four',x'556677'),\n" + " (3,null,'xyz',15),\n" + " (4,'bubba',0x80000000,0.0);\n" + "INSERT INTO t1 SELECT a+4,c,d,b FROM t1;\n" + "INSERT INTO t1 SELECT a+8,d,b,c FROM t1;\n" + "INSERT INTO t1 SELECT a+16,d,c,b FROM t1;\n" + "INSERT INTO t1 SELECT a+32,b,d,c FROM t1;\n" + "INSERT INTO t2 SELECT printf('x%dy',a),b,c FROM t1;\n" + "INSERT INTO t3 SELECT a*1.1,b,c FROM t1;\n" + "INSERT INTO t4 SELECT a||','||quote(b) FROM t1;\n" +; + +/* A database file created by running the two scripts above */ +static const unsigned char aDbBytes[] = { + 83, 81, 76,105,116,101, 32,102,111,114,109, 97,116, 32, 51, 0, 2, 0, 1, + 1, 0, 64, 32, 32, 0, 0, 0, 13, 0, 0, 0, 22, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 5, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 13, 0, 46, 32,152, 13, 1,186, 0, 6, 0,176, 0, 1,194, 1, 84, 1,150, + 0,238, 1, 48, 0,176, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 60, 6, 6, 23, 17, 17, 1,101,116, 97, 98,108,101,116, + 52,116, 52, 7, 67, 82, 69, 65, 84, 69, 32, 84, 65, 66, 76, 69, 32,116, 52, + 40,122, 32, 80, 82, 73, 77, 65, 82, 89, 32, 75, 69, 89, 41, 32, 87, 73, 84, + 72, 79, 85, 84, 32, 82, 79, 87, 73, 68, 64, 4, 6, 23, 17, 17, 1,109,116, + 97, 98,108,101,116, 51,116, 51, 5, 67, 82, 69, 65, 84, 69, 32, 84, 65, 66, + 76, 69, 32,116, 51, 40,119, 32, 82, 69, 65, 76, 32, 80, 82, 73, 77, 65, 82, + 89, 32, 75, 69, 89, 32, 78, 79, 84, 32, 78, 85, 76, 76, 44,120, 44,121, 41, + 34, 5, 5, 23, 55, 17, 1,105,110,100,101,120,115,113,108,105,116,101, 95, + 97,117,116,111,105,110,100,101,120, 95,116, 51, 95, 49,116, 51, 6, 64, 2, + 6, 23, 17, 17, 1,109,116, 97, 98,108,101,116, 50,116, 50, 3, 67, 82, 69, + 65, 84, 69, 32, 84, 65, 66, 76, 69, 32,116, 50, 40,101, 32, 84, 69, 88, 84, + 32, 80, 82, 73, 77, 65, 82, 89, 32, 75, 69, 89, 32, 78, 79, 84, 32, 78, 85, + 76, 76, 44,102, 44,103, 41, 34, 3, 5, 23, 55, 17, 1,105,110,100,101,120, + 115,113,108,105,116,101, 95, 97,117,116,111,105,110,100,101,120, 95,116, 50, + 95, 49,116, 50, 4, 0, 0, 0, 8, 0, 0, 0, 0, 60, 1, 6, 23, 17, 17, + 1,101,116, 97, 98,108,101,116, 49,116, 49, 2, 67, 82, 69, 65, 84, 69, 32, + 84, 65, 66, 76, 69, 32,116, 49, 40, 97, 32, 73, 78, 84, 69, 71, 69, 82, 32, + 80, 82, 73, 77, 65, 82, 89, 32, 75, 69, 89, 44, 98, 44, 99, 44,100, 41, 5, + 0, 0, 0, 2, 1,246, 0, 0, 0, 0, 10, 1,251, 1,246, 1,177, 1,155, + 1,145, 1,119, 1,109, 1, 87, 1, 76, 1, 50, 1, 40, 1, 18, 1, 7, 0, + 237, 0,227, 0,205, 0,195, 0,169, 0,159, 0,137, 0,126, 0,100, 0, 90, + 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 20, 26, 5, 0, 21, 7, 18,102,111, + 117,114, 64, 12, 0, 0, 0, 0, 0, 0, 85,102,119, 8, 25, 5, 0, 1, 1, + 1, 3, 2, 4, 24, 24, 5, 0, 23, 7, 5, 98,117, 98, 98, 97, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0,128, 0, 0, 0, 9, 23, 5, 0, 0, 1, 19, 15, + 120,121,122, 20, 22, 5, 0, 7, 18, 21, 64, 12, 0, 0, 0, 0, 0, 0, 85, + 102,119,102,111,117,114, 8, 21, 5, 0, 1, 1, 1, 2, 4, 3, 24, 20, 5, + 0, 7, 5, 23, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,128, 0, 0, 0, 98, + 117, 98, 98, 97, 8, 19, 4, 0, 1, 19, 15,120,121,122, 20, 18, 5, 0, 18, + 21, 7, 85,102,119,102,111,117,114, 64, 12, 0, 0, 0, 0, 0, 0, 8, 17, + 5, 0, 1, 1, 1, 4, 3, 2, 24, 16, 5, 0, 23, 5, 7, 98,117, 98, 98, + 97, 0, 0,128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 15, 5, 0, + 0, 19, 1,120,121,122, 15, 20, 14, 5, 0, 7, 21, 18, 64, 12, 0, 0, 0, + 0, 0, 0,102,111,117,114, 85,102,119, 8, 13, 5, 0, 1, 1, 1, 2, 3, + 4, 24, 12, 5, 0, 7, 23, 5, 0, 0, 0, 0, 0, 0, 0, 0, 98,117, 98, + 98, 97, 0, 0,128, 0, 0, 0, 9, 11, 5, 0, 1, 0, 19, 15,120,121,122, + 20, 10, 5, 0, 18, 7, 21, 85,102,119, 64, 12, 0, 0, 0, 0, 0, 0,102, + 111,117,114, 8, 9, 5, 0, 1, 1, 1, 4, 2, 3, 24, 8, 5, 0, 5, 7, + 23, 0, 0,128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 98,117, 98, 98, + 97, 8, 7, 4, 0, 19, 1,120,121,122, 15, 20, 6, 5, 0, 21, 18, 7,102, + 111,117,114, 85,102,119, 64, 12, 0, 0, 0, 0, 0, 0, 8, 5, 5, 0, 1, + 1, 1, 3, 4, 2, 24, 4, 5, 0, 23, 5, 7, 98,117, 98, 98, 97, 0, 0, + 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 3, 5, 0, 0, 19, 1, + 120,121,122, 15, 20, 2, 5, 0, 7, 21, 18, 64, 12, 0, 0, 0, 0, 0, 0, + 102,111,117,114, 85,102,119, 0, 0, 0, 9, 52, 0, 0, 0, 8, 26, 5, 0, + 0, 0, 2, 1,246, 0, 0, 0, 0, 13, 1,251, 1,246, 1,181, 1,165, 1, + 152, 1,129, 1,118, 1, 97, 1, 87, 1, 64, 1, 52, 1, 30, 1, 17, 0,252, + 0,240, 0,223, 0,209, 0,185, 0,173, 0,152, 0,141, 0,118, 0,106, 0, + 84, 0, 72, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 27, 3, 21, 19,120, + 50, 55,121,120,121,122, 20, 26, 4, 21, 21, 7,120, 50, 54,121,102,111,117, + 114, 64, 12, 0, 0, 0, 0, 0, 0, 10, 25, 4, 21, 1, 1,120, 50, 53,121, + 3, 2, 21, 24, 4, 21, 23, 7,120, 50, 52,121, 98,117, 98, 98, 97, 0, 0, + 0, 0, 0, 0, 0, 0, 9, 23, 4, 21, 0, 1,120, 50, 51,121, 15, 19, 22, + 4, 21, 7, 18,120, 50, 50,121, 64, 12, 0, 0, 0, 0, 0, 0, 85,102,119, + 10, 21, 4, 21, 1, 1,120, 50, 49,121, 2, 4, 22, 20, 4, 21, 7, 5,120, + 50, 48,121, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,128, 0, 0, 0, 12, 19, + 4, 21, 1, 19,120, 49, 57,121, 15,120,121,122, 15, 18, 4, 21, 18, 21,120, + 49, 56,121, 85,102,119,102,111,117,114, 10, 17, 4, 21, 1, 1,120, 49, 55, + 121, 4, 3, 19, 16, 4, 21, 23, 5,120, 49, 54,121, 98,117, 98, 98, 97, 0, + 0,128, 0, 0, 0, 11, 15, 4, 21, 0, 19,120, 49, 53,121,120,121,122, 20, + 14, 4, 21, 7, 21,120, 49, 52,121, 64, 12, 0, 0, 0, 0, 0, 0,102,111, + 117,114, 10, 13, 4, 21, 1, 1,120, 49, 51,121, 2, 3, 21, 12, 4, 21, 7, + 23,120, 49, 50,121, 0, 0, 0, 0, 0, 0, 0, 0, 98,117, 98, 98, 97, 8, + 11, 3, 21, 1,120, 49, 49,121, 15, 19, 10, 4, 21, 18, 7,120, 49, 48,121, + 85,102,119, 64, 12, 0, 0, 0, 0, 0, 0, 9, 9, 4, 19, 1, 1,120, 57, + 121, 4, 2, 21, 8, 4, 19, 5, 7,120, 56,121, 0, 0,128, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 11, 7, 4, 19, 19, 1,120, 55,121,120,121,122, + 15, 14, 6, 4, 19, 21, 18,120, 54,121,102,111,117,114, 85,102,119, 9, 5, + 4, 19, 1, 1,120, 53,121, 3, 4, 18, 4, 4, 19, 23, 5,120, 52,121, 98, + 117, 98, 98, 97, 0, 0,128, 0, 0, 0, 10, 3, 4, 19, 0, 19,120, 51,121, + 120,121,122, 19, 2, 4, 19, 7, 21,120, 50,121, 64, 12, 0, 0, 0, 0, 0, + 0,102,111,117,114, 9, 0, 0, 0, 12, 53, 0, 0, 0, 11, 27, 2, 0, 0, + 0, 1, 1,243, 0, 0, 0, 0, 15, 1,243, 1,220, 1,211, 1,202, 1,193, + 1,184, 1,175, 1,166, 1,159, 1,150, 1,141, 1,132, 1,123, 1,114, 1, + 105, 1, 96, 1, 87, 1, 78, 1, 69, 1, 61, 1, 52, 1, 43, 1, 34, 1, 25, + 1, 16, 1, 7, 0,254, 0,245, 0,236, 0,227, 0,219, 0,210, 0,201, 0, + 192, 0,183, 0,174, 0,165, 0,156, 0,147, 0,138, 0,129, 0,121, 0,112, + 0,103, 0, 0, 0, 8, 3, 21, 1,120, 53, 49,121, 51, 8, 3, 21, 1,120, + 53, 48,121, 50, 7, 3, 19, 1,120, 52,121, 4, 8, 3, 21, 1,120, 52, 57, + 121, 49, 8, 3, 21, 1,120, 52, 56,121, 48, 8, 3, 21, 1,120, 52, 55,121, + 47, 8, 3, 21, 1,120, 52, 54,121, 46, 8, 3, 21, 1,120, 52, 53,121, 45, + 8, 3, 21, 1,120, 52, 52,121, 44, 8, 3, 21, 1,120, 52, 51,121, 43, 8, + 3, 21, 1,120, 52, 50,121, 42, 8, 3, 21, 1,120, 52, 49,121, 41, 8, 3, + 21, 1,120, 52, 48,121, 40, 7, 3, 19, 1,120, 51,121, 3, 8, 3, 21, 1, + 120, 51, 57,121, 39, 8, 3, 21, 1,120, 51, 56,121, 38, 8, 3, 21, 1,120, + 51, 55,121, 37, 8, 3, 21, 1,120, 51, 54,121, 36, 8, 3, 21, 1,120, 51, + 53,121, 35, 8, 3, 21, 1,120, 51, 52,121, 34, 8, 3, 21, 1,120, 51, 51, + 121, 33, 8, 3, 21, 1,120, 51, 50,121, 32, 8, 3, 21, 1,120, 51, 49,121, + 31, 8, 3, 21, 1,120, 51, 48,121, 30, 7, 3, 19, 1,120, 50,121, 2, 8, + 3, 21, 1,120, 50, 57,121, 29, 8, 3, 21, 1,120, 50, 56,121, 28, 8, 3, + 21, 1,120, 50, 55,121, 27, 8, 3, 21, 1,120, 50, 54,121, 26, 8, 3, 21, + 1,120, 50, 53,121, 25, 8, 3, 21, 1,120, 50, 52,121, 24, 8, 3, 21, 1, + 120, 50, 51,121, 23, 8, 3, 21, 1,120, 50, 50,121, 22, 8, 3, 21, 1,120, + 50, 49,121, 21, 8, 3, 21, 1,120, 50, 48,121, 20, 6, 3, 19, 9,120, 49, + 121, 8, 3, 21, 1,120, 49, 57,121, 19, 8, 3, 21, 1,120, 49, 56,121, 18, + 8, 3, 21, 1,120, 49, 55,121, 17, 8, 3, 21, 1,120, 49, 54,121, 16, 8, + 3, 21, 1,120, 49, 53,121, 15, 8, 3, 21, 1,120, 49, 52,121, 14, 8, 3, + 21, 1,120, 49, 51,121, 13, 8, 3, 21, 1,120, 49, 50,121, 12, 8, 3, 21, + 1,120, 0, 0, 0, 14, 8, 3, 21, 1,120, 53, 49,121, 51, 5, 0, 0, 0, + 2, 1,246, 0, 0, 0, 0, 18, 1,251, 1,246, 1,156, 1,135, 1,117, 1, + 89, 1, 73, 1, 55, 1, 41, 1, 14, 0,254, 0,228, 0,211, 0,186, 0,170, + 0,149, 0,131, 0,110, 0, 94, 0, 69, 0, 54, 13, 23, 4, 7, 0, 1, 64, + 57, 76,204,204,204,204,205, 15, 23, 22, 4, 7, 7, 18, 64, 56, 51, 51, 51, + 51, 51, 52, 64, 12, 0, 0, 0, 0, 0, 0, 85,102,119, 14, 21, 4, 7, 1, + 1, 64, 55, 25,153,153,153,153,154, 2, 4, 19, 20, 4, 1, 7, 5, 22, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0,128, 0, 0, 0, 16, 19, 4, 7, 1, 19, + 64, 52,230,102,102,102,102,103, 15,120,121,122, 19, 18, 4, 7, 18, 21, 64, + 51,204,204,204,204,204,205, 85,102,119,102,111,117,114, 14, 17, 4, 7, 1, + 1, 64, 50,179, 51, 51, 51, 51, 52, 4, 3, 23, 16, 4, 7, 23, 5, 64, 49, + 153,153,153,153,153,154, 98,117, 98, 98, 97, 0, 0,128, 0, 0, 0, 15, 15, + 4, 7, 0, 19, 64, 48,128, 0, 0, 0, 0, 0,120,121,122, 24, 14, 4, 7, + 7, 21, 64, 46,204,204,204,204,204,206, 64, 12, 0, 0, 0, 0, 0, 0,102, + 111,117,114, 14, 13, 4, 7, 1, 1, 64, 44,153,153,153,153,153,154, 2, 3, + 25, 12, 4, 7, 7, 23, 64, 42,102,102,102,102,102,103, 0, 0, 0, 0, 0, + 0, 0, 0, 98,117, 98, 98, 97, 12, 11, 3, 7, 1, 64, 40, 51, 51, 51, 51, + 51, 52, 15, 16, 10, 4, 1, 18, 7, 11, 85,102,119, 64, 12, 0, 0, 0, 0, + 0, 0, 14, 9, 4, 7, 1, 1, 64, 35,204,204,204,204,204,205, 4, 2, 26, + 8, 4, 7, 5, 7, 64, 33,153,153,153,153,153,154, 0, 0,128, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 16, 7, 4, 7, 19, 1, 64, 30,204,204,204, + 204,204,206,120,121,122, 15, 19, 6, 4, 7, 21, 18, 64, 26,102,102,102,102, + 102,103,102,111,117,114, 85,102,119, 14, 5, 4, 7, 1, 1, 64, 22, 0, 0, + 0, 0, 0, 0, 3, 4, 23, 4, 4, 7, 23, 5, 64, 17,153,153,153,153,153, + 154, 98,117, 98, 98, 97, 0, 0,128, 0, 0, 0, 15, 3, 4, 7, 0, 19, 64, + 10,102,102,102,102,102,103,120,121,122, 24, 2, 4, 7, 7, 21, 64, 1,153, + 153,153,153,153,154, 64, 12, 0, 0, 0, 0, 0, 0,102,111,117,114, 14, 1, + 4, 7, 1, 1, 0, 0, 0, 17, 45, 0, 0, 0, 16, 23, 2, 0, 0, 0, 1, + 1,239, 0, 0, 0, 0, 20, 1,239, 1,205, 1,192, 1,179, 1,166, 1,153, + 1,140, 1,134, 1,121, 1,108, 1, 95, 1, 82, 1, 69, 1, 56, 1, 43, 1, + 30, 1, 17, 1, 11, 0,254, 0,241, 0,228, 0,215, 0,202, 0,189, 0,176, + 0,163, 0,150, 0,144, 0,131, 0,118, 0,105, 0, 92, 0, 79, 0, 12, 3, + 7, 1, 64, 67, 64, 0, 0, 0, 0, 0, 35, 12, 3, 7, 1, 64, 66,179, 51, + 51, 51, 51, 52, 34, 12, 3, 7, 1, 64, 66, 38,102,102,102,102,103, 33, 12, + 3, 7, 1, 64, 65,153,153,153,153,153,154, 32, 12, 3, 7, 1, 64, 65, 12, + 204,204,204,204,205, 31, 5, 3, 1, 1, 33, 30, 12, 3, 7, 1, 64, 63,230, + 102,102,102,102,103, 29, 12, 3, 7, 1, 64, 62,204,204,204,204,204,206, 28, + 12, 3, 7, 1, 64, 61,179, 51, 51, 51, 51, 52, 27, 12, 3, 7, 1, 64, 60, + 153,153,153,153,153,154, 26, 12, 3, 7, 1, 64, 59,128, 0, 0, 0, 0, 1, + 25, 12, 3, 7, 1, 64, 58,102,102,102,102,102,103, 24, 12, 3, 7, 1, 64, + 57, 76,204,204,204,204,205, 23, 12, 3, 7, 1, 64, 56, 51, 51, 51, 51, 51, + 52, 22, 12, 3, 7, 1, 64, 55, 25,153,153,153,153,154, 21, 5, 3, 1, 1, + 22, 20, 12, 3, 7, 1, 64, 52,230,102,102,102,102,103, 19, 12, 3, 7, 1, + 64, 51,204,204,204,204,204,205, 18, 12, 3, 7, 1, 64, 50,179, 51, 51, 51, + 51, 52, 17, 12, 3, 7, 1, 64, 49,153,153,153,153,153,154, 16, 12, 3, 7, + 1, 64, 48,128, 0, 0, 0, 0, 0, 15, 12, 3, 7, 1, 64, 46,204,204,204, + 204,204,206, 14, 12, 3, 7, 1, 64, 44,153,153,153,153,153,154, 13, 12, 3, + 7, 1, 64, 42,102,102,102,102,102,103, 12, 12, 3, 7, 1, 64, 40, 51, 51, + 51, 51, 51, 52, 11, 5, 3, 1, 1, 11, 10, 12, 3, 7, 1, 64, 35,204,204, + 204,204,204,205, 9, 12, 3, 7, 1, 64, 33,153,153,153,153,153,154, 8, 12, + 3, 7, 1, 64, 30,204,204,204,204,204,206, 7, 12, 3, 7, 1, 64, 26,102, + 102,102,102,102,103, 6, 12, 3, 7, 1, 64, 22, 0, 0, 0, 0, 0, 0, 5, + 12, 3, 7, 1, 64, 17,153,153,153,153,153,154, 4, 12, 3, 7, 1, 64, 10, + 102,102,102,102,102,103, 3, 12, 3, 7, 1, 64, 1,153,153, 0, 0, 0, 19, + 12, 3, 7, 1, 64, 67, 64, 0, 0, 0, 0, 0, 35, 2, 0, 0, 0, 1, 1, + 242, 0, 0, 0, 0, 22, 1,242, 1,218, 1,211, 1,202, 1,192, 1,179, 1, + 172, 1,157, 1,149, 1,141, 1,132, 1,125, 1,116, 1,106, 1, 93, 1, 86, + 1, 74, 1, 63, 1, 47, 1, 40, 1, 31, 1, 16, 1, 8, 0,255, 0,248, 0, + 239, 0,229, 0,216, 0,209, 0,197, 0,186, 0,174, 0,158, 0,151, 0,136, + 0,128, 0,119, 0,112, 0,103, 0, 93, 0, 9, 2, 27, 52, 55, 44, 78, 85, + 76, 76, 8, 2, 25, 52, 54, 44, 51, 46, 53, 6, 2, 21, 52, 53, 44, 50, 8, + 2, 25, 52, 52, 44, 48, 46, 48, 7, 2, 23, 52, 51, 44, 49, 53, 14, 2, 37, + 52, 50, 44, 88, 39, 53, 53, 54, 54, 55, 55, 39, 6, 2, 21, 52, 49, 44, 52, + 15, 2, 39, 52, 48, 44, 50, 49, 52, 55, 52, 56, 51, 54, 52, 56, 11, 2, 31, + 52, 44, 39, 98,117, 98, 98, 97, 39, 10, 2, 29, 51, 57, 44, 39,120,121,122, + 39, 11, 2, 31, 51, 56, 44, 39,102,111,117,114, 39, 6, 2, 21, 51, 55, 44, + 51, 12, 2, 33, 51, 54, 44, 39, 98,117, 98, 98, 97, 39, 9, 2, 27, 51, 53, + 44, 78, 85, 76, 76, 8, 2, 25, 51, 52, 44, 51, 46, 53, 6, 2, 21, 51, 51, + 44, 50, 8, 2, 25, 51, 50, 44, 48, 46, 48, 7, 2, 23, 51, 49, 44, 49, 53, + 14, 2, 37, 51, 48, 44, 88, 39, 53, 53, 54, 54, 55, 55, 39, 8, 2, 25, 51, + 44, 78, 85, 76, 76, 6, 2, 21, 50, 57, 44, 52, 15, 2, 39, 50, 56, 44, 50, + 49, 52, 55, 52, 56, 51, 54, 52, 56, 10, 2, 29, 50, 55, 44, 39,120,121,122, + 39, 11, 2, 31, 50, 54, 44, 39,102,111,117,114, 39, 6, 2, 21, 50, 53, 44, + 51, 12, 2, 33, 50, 52, 44, 39, 98,117, 98, 98, 97, 39, 9, 2, 27, 50, 51, + 44, 78, 85, 76, 76, 8, 2, 25, 50, 50, 44, 51, 46, 53, 6, 2, 21, 50, 49, + 44, 50, 8, 2, 25, 50, 48, 44, 48, 46, 48, 7, 2, 23, 50, 44, 51, 46, 53, + 7, 2, 23, 49, 57, 44, 49, 53, 14, 2, 37, 49, 56, 44, 88, 39, 53, 53, 54, + 54, 55, 55, 39, 6, 2, 21, 49, 55, 44, 52, 12, 2, 33, 49, 54, 44, 39, 98, + 117, 98, 98, 97, 39, 9, 2, 27, 49, 53, 44, 78, 85, 76, 76, 8, 2, 25, 49, + 52, 44, 51, 46, 53, 6, 2, 21, 49, 51, 44, 50, 8, 2, 25, 49, 50, 44, 48, + 46, 48, 7, 2, 23, 49, 49, 44, 49, 53, 14, 2, 37, 49, 48, 44, 88, 0, 0, + 0, 21, 9, 2, 27, 52, 55, 44, 78, 85, 76, 76, 13, 0, 0, 0, 26, 0, 68, + 0, 1,246, 1,224, 1,213, 1,187, 1,177, 1,155, 1,145, 1,119, 1,109, + 1, 87, 1, 76, 1, 50, 1, 40, 1, 18, 1, 7, 0,237, 0,227, 0,205, 0, + 195, 0,169, 0,159, 0,137, 0,126, 0,100, 0, 90, 0, 68, 0, 0, 0, 0, + 0, 0, 0, 0, 20, 26, 5, 0, 21, 7, 18,102,111,117,114, 64, 12, 0, 0, + 0, 0, 0, 0, 85,102,119, 8, 25, 5, 0, 1, 1, 1, 3, 2, 4, 24, 24, + 5, 0, 23, 7, 5, 98,117, 98, 98, 97, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0,128, 0, 0, 0, 9, 23, 5, 0, 0, 1, 19, 15,120,121,122, 20, 22, 5, + 0, 7, 18, 21, 64, 12, 0, 0, 0, 0, 0, 0, 85,102,119,102,111,117,114, + 8, 21, 5, 0, 1, 1, 1, 2, 4, 3, 24, 20, 5, 0, 7, 5, 23, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0,128, 0, 0, 0, 98,117, 98, 98, 97, 8, 19, + 4, 0, 1, 19, 15,120,121,122, 20, 18, 5, 0, 18, 21, 7, 85,102,119,102, + 111,117,114, 64, 12, 0, 0, 0, 0, 0, 0, 8, 17, 5, 0, 1, 1, 1, 4, + 3, 2, 24, 16, 5, 0, 23, 5, 7, 98,117, 98, 98, 97, 0, 0,128, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 15, 5, 0, 0, 19, 1,120,121,122, + 15, 20, 14, 5, 0, 7, 21, 18, 64, 12, 0, 0, 0, 0, 0, 0,102,111,117, + 114, 85,102,119, 8, 13, 5, 0, 1, 1, 1, 2, 3, 4, 24, 12, 5, 0, 7, + 23, 5, 0, 0, 0, 0, 0, 0, 0, 0, 98,117, 98, 98, 97, 0, 0,128, 0, + 0, 0, 9, 11, 5, 0, 1, 0, 19, 15,120,121,122, 20, 10, 5, 0, 18, 7, + 21, 85,102,119, 64, 12, 0, 0, 0, 0, 0, 0,102,111,117,114, 8, 9, 5, + 0, 1, 1, 1, 4, 2, 3, 24, 8, 5, 0, 5, 7, 23, 0, 0,128, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 98,117, 98, 98, 97, 8, 7, 4, 0, 19, + 1,120,121,122, 15, 20, 6, 5, 0, 21, 18, 7,102,111,117,114, 85,102,119, + 64, 12, 0, 0, 0, 0, 0, 0, 8, 5, 5, 0, 1, 1, 1, 3, 4, 2, 24, + 4, 5, 0, 23, 5, 7, 98,117, 98, 98, 97, 0, 0,128, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 9, 3, 5, 0, 0, 19, 1,120,121,122, 15, 20, 2, + 5, 0, 7, 21, 18, 64, 12, 0, 0, 0, 0, 0, 0,102,111,117,114, 85,102, + 119, 8, 1, 5, 0, 1, 1, 1, 2, 3, 4, 13, 0, 0, 0, 26, 0, 63, 0, + 1,245, 1,219, 1,209, 1,187, 1,177, 1,151, 1,141, 1,119, 1,108, 1, + 82, 1, 72, 1, 50, 1, 39, 1, 13, 1, 3, 0,237, 0,227, 0,201, 0,191, + 0,169, 0,158, 0,132, 0,122, 0,100, 0, 89, 0, 63, 0, 0, 0, 24, 52, + 5, 0, 7, 23, 5, 0, 0, 0, 0, 0, 0, 0, 0, 98,117, 98, 98, 97, 0, + 0,128, 0, 0, 0, 9, 51, 5, 0, 1, 0, 19, 15,120,121,122, 20, 50, 5, + 0, 18, 7, 21, 85,102,119, 64, 12, 0, 0, 0, 0, 0, 0,102,111,117,114, + 8, 49, 5, 0, 1, 1, 1, 4, 2, 3, 24, 48, 5, 0, 23, 7, 5, 98,117, + 98, 98, 97, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,128, 0, 0, 0, 9, 47, + 5, 0, 0, 1, 19, 15,120,121,122, 20, 46, 5, 0, 7, 18, 21, 64, 12, 0, + 0, 0, 0, 0, 0, 85,102,119,102,111,117,114, 8, 45, 5, 0, 1, 1, 1, + 2, 4, 3, 24, 44, 5, 0, 7, 5, 23, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0,128, 0, 0, 0, 98,117, 98, 98, 97, 8, 43, 4, 0, 1, 19, 15,120,121, + 122, 20, 42, 5, 0, 18, 21, 7, 85,102,119,102,111,117,114, 64, 12, 0, 0, + 0, 0, 0, 0, 8, 41, 5, 0, 1, 1, 1, 4, 3, 2, 24, 40, 5, 0, 5, + 23, 7, 0, 0,128, 0, 0, 0, 98,117, 98, 98, 97, 0, 0, 0, 0, 0, 0, + 0, 0, 9, 39, 5, 0, 19, 0, 1,120,121,122, 15, 20, 38, 5, 0, 21, 7, + 18,102,111,117,114, 64, 12, 0, 0, 0, 0, 0, 0, 85,102,119, 8, 37, 5, + 0, 1, 1, 1, 3, 2, 4, 24, 36, 5, 0, 23, 7, 5, 98,117, 98, 98, 97, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,128, 0, 0, 0, 9, 35, 5, 0, 0, + 1, 19, 15,120,121,122, 20, 34, 5, 0, 7, 18, 21, 64, 12, 0, 0, 0, 0, + 0, 0, 85,102,119,102,111,117,114, 8, 33, 5, 0, 1, 1, 1, 2, 4, 3, + 24, 32, 5, 0, 7, 5, 23, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,128, 0, + 0, 0, 98,117, 98, 98, 97, 8, 31, 4, 0, 1, 19, 15,120,121,122, 20, 30, + 5, 0, 18, 21, 7, 85,102,119,102,111,117,114, 64, 12, 0, 0, 0, 0, 0, + 0, 8, 29, 5, 0, 1, 1, 1, 4, 3, 2, 24, 28, 5, 0, 5, 23, 7, 0, + 0,128, 0, 0, 0, 98,117, 98, 98, 97, 0, 0, 0, 0, 0, 0, 0, 0, 9, + 27, 5, 0, 19, 0, 1,120,121,122, 15, 13, 0, 0, 0, 12, 1, 50, 0, 1, + 246, 1,224, 1,213, 1,187, 1,177, 1,155, 1,145, 1,119, 1,109, 1, 87, + 1, 76, 1, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 24, 64, 5, 0, 7, 23, 5, + 0, 0, 0, 0, 0, 0, 0, 0, 98,117, 98, 98, 97, 0, 0,128, 0, 0, 0, + 9, 63, 5, 0, 1, 0, 19, 15,120,121,122, 20, 62, 5, 0, 18, 7, 21, 85, + 102,119, 64, 12, 0, 0, 0, 0, 0, 0,102,111,117,114, 8, 61, 5, 0, 1, + 1, 1, 4, 2, 3, 24, 60, 5, 0, 5, 7, 23, 0, 0,128, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 98,117, 98, 98, 97, 8, 59, 4, 0, 19, 1,120, + 121,122, 15, 20, 58, 5, 0, 21, 18, 7,102,111,117,114, 85,102,119, 64, 12, + 0, 0, 0, 0, 0, 0, 8, 57, 5, 0, 1, 1, 1, 3, 4, 2, 24, 56, 5, + 0, 23, 5, 7, 98,117, 98, 98, 97, 0, 0,128, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 9, 55, 5, 0, 0, 19, 1,120,121,122, 15, 20, 54, 5, 0, + 7, 21, 18, 64, 12, 0, 0, 0, 0, 0, 0,102,111,117,114, 85,102,119, 8, + 53, 5, 0, 1, 1, 1, 2, 3, 4, 13, 0, 0, 0, 27, 0, 72, 0, 1,245, + 1,224, 1,212, 1,192, 1,181, 1,165, 1,152, 1,129, 1,118, 1, 97, 1, + 87, 1, 64, 1, 52, 1, 30, 1, 17, 0,252, 0,240, 0,223, 0,209, 0,185, + 0,173, 0,152, 0,141, 0,118, 0,106, 0, 84, 0, 72, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 10, 27, 3, 21, 19,120, 50, 55,121,120,121,122, 20, 26, + 4, 21, 21, 7,120, 50, 54,121,102,111,117,114, 64, 12, 0, 0, 0, 0, 0, + 0, 10, 25, 4, 21, 1, 1,120, 50, 53,121, 3, 2, 21, 24, 4, 21, 23, 7, + 120, 50, 52,121, 98,117, 98, 98, 97, 0, 0, 0, 0, 0, 0, 0, 0, 9, 23, + 4, 21, 0, 1,120, 50, 51,121, 15, 19, 22, 4, 21, 7, 18,120, 50, 50,121, + 64, 12, 0, 0, 0, 0, 0, 0, 85,102,119, 10, 21, 4, 21, 1, 1,120, 50, + 49,121, 2, 4, 22, 20, 4, 21, 7, 5,120, 50, 48,121, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0,128, 0, 0, 0, 12, 19, 4, 21, 1, 19,120, 49, 57,121, + 15,120,121,122, 15, 18, 4, 21, 18, 21,120, 49, 56,121, 85,102,119,102,111, + 117,114, 10, 17, 4, 21, 1, 1,120, 49, 55,121, 4, 3, 19, 16, 4, 21, 23, + 5,120, 49, 54,121, 98,117, 98, 98, 97, 0, 0,128, 0, 0, 0, 11, 15, 4, + 21, 0, 19,120, 49, 53,121,120,121,122, 20, 14, 4, 21, 7, 21,120, 49, 52, + 121, 64, 12, 0, 0, 0, 0, 0, 0,102,111,117,114, 10, 13, 4, 21, 1, 1, + 120, 49, 51,121, 2, 3, 21, 12, 4, 21, 7, 23,120, 49, 50,121, 0, 0, 0, + 0, 0, 0, 0, 0, 98,117, 98, 98, 97, 8, 11, 3, 21, 1,120, 49, 49,121, + 15, 19, 10, 4, 21, 18, 7,120, 49, 48,121, 85,102,119, 64, 12, 0, 0, 0, + 0, 0, 0, 9, 9, 4, 19, 1, 1,120, 57,121, 4, 2, 21, 8, 4, 19, 5, + 7,120, 56,121, 0, 0,128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, + 7, 4, 19, 19, 1,120, 55,121,120,121,122, 15, 14, 6, 4, 19, 21, 18,120, + 54,121,102,111,117,114, 85,102,119, 9, 5, 4, 19, 1, 1,120, 53,121, 3, + 4, 18, 4, 4, 19, 23, 5,120, 52,121, 98,117, 98, 98, 97, 0, 0,128, 0, + 0, 0, 10, 3, 4, 19, 0, 19,120, 51,121,120,121,122, 19, 2, 4, 19, 7, + 21,120, 50,121, 64, 12, 0, 0, 0, 0, 0, 0,102,111,117,114, 9, 1, 4, + 19, 1, 1,120, 49,121, 2, 3, 13, 0, 0, 0, 26, 0, 78, 0, 1,235, 1, + 223, 1,206, 1,192, 1,168, 1,156, 1,135, 1,124, 1,101, 1, 89, 1, 67, + 1, 55, 1, 34, 1, 22, 1, 5, 0,247, 0,223, 0,211, 0,190, 0,179, 0, + 156, 0,144, 0,123, 0,113, 0, 90, 0, 78, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 53, 4, 21, 1, 1,120, 53, 51, + 121, 2, 3, 21, 52, 4, 21, 7, 23,120, 53, 50,121, 0, 0, 0, 0, 0, 0, + 0, 0, 98,117, 98, 98, 97, 8, 51, 3, 21, 1,120, 53, 49,121, 15, 19, 50, + 4, 21, 18, 7,120, 53, 48,121, 85,102,119, 64, 12, 0, 0, 0, 0, 0, 0, + 10, 49, 4, 21, 1, 1,120, 52, 57,121, 4, 2, 21, 48, 4, 21, 23, 7,120, + 52, 56,121, 98,117, 98, 98, 97, 0, 0, 0, 0, 0, 0, 0, 0, 9, 47, 4, + 21, 0, 1,120, 52, 55,121, 15, 19, 46, 4, 21, 7, 18,120, 52, 54,121, 64, + 12, 0, 0, 0, 0, 0, 0, 85,102,119, 10, 45, 4, 21, 1, 1,120, 52, 53, + 121, 2, 4, 22, 44, 4, 21, 7, 5,120, 52, 52,121, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0,128, 0, 0, 0, 12, 43, 4, 21, 1, 19,120, 52, 51,121, 15, + 120,121,122, 15, 42, 4, 21, 18, 21,120, 52, 50,121, 85,102,119,102,111,117, + 114, 10, 41, 4, 21, 1, 1,120, 52, 49,121, 4, 3, 19, 40, 4, 21, 5, 23, + 120, 52, 48,121, 0, 0,128, 0, 0, 0, 98,117, 98, 98, 97, 10, 39, 3, 21, + 19,120, 51, 57,121,120,121,122, 20, 38, 4, 21, 21, 7,120, 51, 56,121,102, + 111,117,114, 64, 12, 0, 0, 0, 0, 0, 0, 10, 37, 4, 21, 1, 1,120, 51, + 55,121, 3, 2, 21, 36, 4, 21, 23, 7,120, 51, 54,121, 98,117, 98, 98, 97, + 0, 0, 0, 0, 0, 0, 0, 0, 9, 35, 4, 21, 0, 1,120, 51, 53,121, 15, + 19, 34, 4, 21, 7, 18,120, 51, 52,121, 64, 12, 0, 0, 0, 0, 0, 0, 85, + 102,119, 10, 33, 4, 21, 1, 1,120, 51, 51,121, 2, 4, 22, 32, 4, 21, 7, + 5,120, 51, 50,121, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,128, 0, 0, 0, + 12, 31, 4, 21, 1, 19,120, 51, 49,121, 15,120,121,122, 15, 30, 4, 21, 18, + 21,120, 51, 48,121, 85,102,119,102,111,117,114, 10, 29, 4, 21, 1, 1,120, + 50, 57,121, 4, 3, 19, 28, 4, 21, 5, 23,120, 50, 56,121, 0, 0,128, 0, + 0, 0, 98,117, 98, 98, 97, 13, 0, 0, 0, 11, 1, 67, 0, 1,234, 1,221, + 1,200, 1,188, 1,171, 1,157, 1,133, 1,121, 1,100, 1, 90, 1, 67, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 21, 64, 4, 21, 7, 23,120, 54, 52,121, 0, 0, + 0, 0, 0, 0, 0, 0, 98,117, 98, 98, 97, 8, 63, 3, 21, 1,120, 54, 51, + 121, 15, 19, 62, 4, 21, 18, 7,120, 54, 50,121, 85,102,119, 64, 12, 0, 0, + 0, 0, 0, 0, 10, 61, 4, 21, 1, 1,120, 54, 49,121, 4, 2, 22, 60, 4, + 21, 5, 7,120, 54, 48,121, 0, 0,128, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 12, 59, 4, 21, 19, 1,120, 53, 57,121,120,121,122, 15, 15, 58, 4, + 21, 21, 18,120, 53, 56,121,102,111,117,114, 85,102,119, 10, 57, 4, 21, 1, + 1,120, 53, 55,121, 3, 4, 19, 56, 4, 21, 23, 5,120, 53, 54,121, 98,117, + 98, 98, 97, 0, 0,128, 0, 0, 0, 11, 55, 4, 21, 0, 19,120, 53, 53,121, + 120,121,122, 20, 54, 4, 21, 7, 21,120, 53, 52,121, 64, 12, 0, 0, 0, 0, + 0, 0,102,111,117,114, 10, 0, 0, 0, 45, 0,112, 0, 1,247, 1,238, 1, + 229, 1,220, 1,211, 1,202, 1,193, 1,184, 1,175, 1,166, 1,159, 1,150, + 1,141, 1,132, 1,123, 1,114, 1,105, 1, 96, 1, 87, 1, 78, 1, 69, 1, + 61, 1, 52, 1, 43, 1, 34, 1, 25, 1, 16, 1, 7, 0,254, 0,245, 0,236, + 0,227, 0,219, 0,210, 0,201, 0,192, 0,183, 0,174, 0,165, 0,156, 0, + 147, 0,138, 0,129, 0,121, 0,112, 0,103, 0, 0, 0, 0, 0, 0, 9,120, + 53, 49,121, 51, 8, 3, 21, 1,120, 53, 48,121, 50, 7, 3, 19, 1,120, 52, + 121, 4, 8, 3, 21, 1,120, 52, 57,121, 49, 8, 3, 21, 1,120, 52, 56,121, + 48, 8, 3, 21, 1,120, 52, 55,121, 47, 8, 3, 21, 1,120, 52, 54,121, 46, + 8, 3, 21, 1,120, 52, 53,121, 45, 8, 3, 21, 1,120, 52, 52,121, 44, 8, + 3, 21, 1,120, 52, 51,121, 43, 8, 3, 21, 1,120, 52, 50,121, 42, 8, 3, + 21, 1,120, 52, 49,121, 41, 8, 3, 21, 1,120, 52, 48,121, 40, 7, 3, 19, + 1,120, 51,121, 3, 8, 3, 21, 1,120, 51, 57,121, 39, 8, 3, 21, 1,120, + 51, 56,121, 38, 8, 3, 21, 1,120, 51, 55,121, 37, 8, 3, 21, 1,120, 51, + 54,121, 36, 8, 3, 21, 1,120, 51, 53,121, 35, 8, 3, 21, 1,120, 51, 52, + 121, 34, 8, 3, 21, 1,120, 51, 51,121, 33, 8, 3, 21, 1,120, 51, 50,121, + 32, 8, 3, 21, 1,120, 51, 49,121, 31, 8, 3, 21, 1,120, 51, 48,121, 30, + 7, 3, 19, 1,120, 50,121, 2, 8, 3, 21, 1,120, 50, 57,121, 29, 8, 3, + 21, 1,120, 50, 56,121, 28, 8, 3, 21, 1,120, 50, 55,121, 27, 8, 3, 21, + 1,120, 50, 54,121, 26, 8, 3, 21, 1,120, 50, 53,121, 25, 8, 3, 21, 1, + 120, 50, 52,121, 24, 8, 3, 21, 1,120, 50, 51,121, 23, 8, 3, 21, 1,120, + 50, 50,121, 22, 8, 3, 21, 1,120, 50, 49,121, 21, 8, 3, 21, 1,120, 50, + 48,121, 20, 6, 3, 19, 9,120, 49,121, 8, 3, 21, 1,120, 49, 57,121, 19, + 8, 3, 21, 1,120, 49, 56,121, 18, 8, 3, 21, 1,120, 49, 55,121, 17, 8, + 3, 21, 1,120, 49, 54,121, 16, 8, 3, 21, 1,120, 49, 53,121, 15, 8, 3, + 21, 1,120, 49, 52,121, 14, 8, 3, 21, 1,120, 49, 51,121, 13, 8, 3, 21, + 1,120, 49, 50,121, 12, 8, 3, 21, 1,120, 49, 49,121, 11, 8, 3, 21, 1, + 120, 49, 48,121, 10, 10, 0, 0, 0, 18, 1, 99, 0, 1,247, 1,238, 1,229, + 1,220, 1,211, 1,202, 1,193, 1,184, 1,176, 1,167, 1,158, 1,149, 1, + 140, 1,131, 1,123, 1,115, 1,107, 1, 99, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, + 3, 19, 1,120, 57,121, 9, 7, 3, 19, 1,120, 56,121, 8, 7, 3, 19, 1, + 120, 55,121, 7, 7, 3, 19, 1,120, 54,121, 6, 8, 3, 21, 1,120, 54, 52, + 121, 64, 8, 3, 21, 1,120, 54, 51,121, 63, 8, 3, 21, 1,120, 54, 50,121, + 62, 8, 3, 21, 1,120, 54, 49,121, 61, 8, 3, 21, 1,120, 54, 48,121, 60, + 7, 3, 19, 1,120, 53,121, 5, 8, 3, 21, 1,120, 53, 57,121, 59, 8, 3, + 21, 1,120, 53, 56,121, 58, 8, 3, 21, 1,120, 53, 55,121, 57, 8, 3, 21, + 1,120, 53, 54,121, 56, 8, 3, 21, 1,120, 53, 53,121, 55, 8, 3, 21, 1, + 120, 53, 52,121, 54, 8, 3, 21, 1,120, 53, 51,121, 53, 8, 3, 21, 1,120, + 53, 50,121, 52, 13, 0, 0, 0, 23, 0, 54, 0, 1,240, 1,214, 1,197, 1, + 172, 1,156, 1,135, 1,117, 1, 89, 1, 73, 1, 55, 1, 41, 1, 14, 0,254, + 0,228, 0,211, 0,186, 0,170, 0,149, 0,131, 0,110, 0, 94, 0, 69, 0, + 54, 13, 23, 4, 7, 0, 1, 64, 57, 76,204,204,204,204,205, 15, 23, 22, 4, + 7, 7, 18, 64, 56, 51, 51, 51, 51, 51, 52, 64, 12, 0, 0, 0, 0, 0, 0, + 85,102,119, 14, 21, 4, 7, 1, 1, 64, 55, 25,153,153,153,153,154, 2, 4, + 19, 20, 4, 1, 7, 5, 22, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,128, 0, + 0, 0, 16, 19, 4, 7, 1, 19, 64, 52,230,102,102,102,102,103, 15,120,121, + 122, 19, 18, 4, 7, 18, 21, 64, 51,204,204,204,204,204,205, 85,102,119,102, + 111,117,114, 14, 17, 4, 7, 1, 1, 64, 50,179, 51, 51, 51, 51, 52, 4, 3, + 23, 16, 4, 7, 23, 5, 64, 49,153,153,153,153,153,154, 98,117, 98, 98, 97, + 0, 0,128, 0, 0, 0, 15, 15, 4, 7, 0, 19, 64, 48,128, 0, 0, 0, 0, + 0,120,121,122, 24, 14, 4, 7, 7, 21, 64, 46,204,204,204,204,204,206, 64, + 12, 0, 0, 0, 0, 0, 0,102,111,117,114, 14, 13, 4, 7, 1, 1, 64, 44, + 153,153,153,153,153,154, 2, 3, 25, 12, 4, 7, 7, 23, 64, 42,102,102,102, + 102,102,103, 0, 0, 0, 0, 0, 0, 0, 0, 98,117, 98, 98, 97, 12, 11, 3, + 7, 1, 64, 40, 51, 51, 51, 51, 51, 52, 15, 16, 10, 4, 1, 18, 7, 11, 85, + 102,119, 64, 12, 0, 0, 0, 0, 0, 0, 14, 9, 4, 7, 1, 1, 64, 35,204, + 204,204,204,204,205, 4, 2, 26, 8, 4, 7, 5, 7, 64, 33,153,153,153,153, + 153,154, 0, 0,128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 7, 4, + 7, 19, 1, 64, 30,204,204,204,204,204,206,120,121,122, 15, 19, 6, 4, 7, + 21, 18, 64, 26,102,102,102,102,102,103,102,111,117,114, 85,102,119, 14, 5, + 4, 7, 1, 1, 64, 22, 0, 0, 0, 0, 0, 0, 3, 4, 23, 4, 4, 7, 23, + 5, 64, 17,153,153,153,153,153,154, 98,117, 98, 98, 97, 0, 0,128, 0, 0, + 0, 15, 3, 4, 7, 0, 19, 64, 10,102,102,102,102,102,103,120,121,122, 24, + 2, 4, 7, 7, 21, 64, 1,153,153,153,153,153,154, 64, 12, 0, 0, 0, 0, + 0, 0,102,111,117,114, 14, 1, 4, 7, 1, 1, 63,241,153,153,153,153,153, + 154, 2, 3, 13, 0, 0, 0, 22, 0, 68, 0, 1,229, 1,213, 1,187, 1,171, + 1,146, 1,130, 1,116, 1, 98, 1, 70, 1, 54, 1, 29, 1, 14, 0,243, 0, + 227, 0,201, 0,185, 0,167, 0,151, 0,130, 0,112, 0, 84, 0, 68, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 45, 4, 7, 1, + 1, 64, 72,192, 0, 0, 0, 0, 1, 2, 4, 26, 44, 4, 7, 7, 5, 64, 72, + 51, 51, 51, 51, 51, 52, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,128, 0, 0, + 0, 16, 43, 4, 7, 1, 19, 64, 71,166,102,102,102,102,103, 15,120,121,122, + 19, 42, 4, 7, 18, 21, 64, 71, 25,153,153,153,153,154, 85,102,119,102,111, + 117,114, 14, 41, 4, 7, 1, 1, 64, 70,140,204,204,204,204,205, 4, 3, 16, + 40, 4, 1, 5, 23, 44, 0, 0,128, 0, 0, 0, 98,117, 98, 98, 97, 14, 39, + 3, 7, 19, 64, 69,115, 51, 51, 51, 51, 52,120,121,122, 24, 38, 4, 7, 21, + 7, 64, 68,230,102,102,102,102,103,102,111,117,114, 64, 12, 0, 0, 0, 0, + 0, 0, 14, 37, 4, 7, 1, 1, 64, 68, 89,153,153,153,153,154, 3, 2, 25, + 36, 4, 7, 23, 7, 64, 67,204,204,204,204,204,205, 98,117, 98, 98, 97, 0, + 0, 0, 0, 0, 0, 0, 0, 13, 35, 4, 7, 0, 1, 64, 67, 64, 0, 0, 0, + 0, 0, 15, 23, 34, 4, 7, 7, 18, 64, 66,179, 51, 51, 51, 51, 52, 64, 12, + 0, 0, 0, 0, 0, 0, 85,102,119, 14, 33, 4, 7, 1, 1, 64, 66, 38,102, + 102,102,102,103, 2, 4, 26, 32, 4, 7, 7, 5, 64, 65,153,153,153,153,153, + 154, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,128, 0, 0, 0, 16, 31, 4, 7, + 1, 19, 64, 65, 12,204,204,204,204,205, 15,120,121,122, 12, 30, 4, 1, 18, + 21, 33, 85,102,119,102,111,117,114, 14, 29, 4, 7, 1, 1, 64, 63,230,102, + 102,102,102,103, 4, 3, 23, 28, 4, 7, 5, 23, 64, 62,204,204,204,204,204, + 206, 0, 0,128, 0, 0, 0, 98,117, 98, 98, 97, 14, 27, 3, 7, 19, 64, 61, + 179, 51, 51, 51, 51, 52,120,121,122, 24, 26, 4, 7, 21, 7, 64, 60,153,153, + 153,153,153,154,102,111,117,114, 64, 12, 0, 0, 0, 0, 0, 0, 14, 25, 4, + 7, 1, 1, 64, 59,128, 0, 0, 0, 0, 1, 3, 2, 25, 24, 4, 7, 23, 7, + 64, 58,102,102,102,102,102,103, 98,117, 98, 98, 97, 0, 0, 0, 0, 0, 0, + 0, 0, 13, 0, 0, 0, 19, 0,121, 0, 1,231, 1,216, 1,189, 1,173, 1, + 148, 1,134, 1,107, 1, 91, 1, 65, 1, 48, 1, 23, 1, 7, 0,242, 0,224, + 0,203, 0,187, 0,162, 0,148, 0,121, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 25, 64, 4, 7, 7, 23, 64, 81,153,153, + 153,153,153,154, 0, 0, 0, 0, 0, 0, 0, 0, 98,117, 98, 98, 97, 12, 63, + 3, 7, 1, 64, 81, 83, 51, 51, 51, 51, 52, 15, 23, 62, 4, 7, 18, 7, 64, + 81, 12,204,204,204,204,205, 85,102,119, 64, 12, 0, 0, 0, 0, 0, 0, 14, + 61, 4, 7, 1, 1, 64, 80,198,102,102,102,102,103, 4, 2, 19, 60, 4, 1, + 5, 7, 66, 0, 0,128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 59, + 4, 7, 19, 1, 64, 80, 57,153,153,153,153,154,120,121,122, 15, 19, 58, 4, + 7, 21, 18, 64, 79,230,102,102,102,102,103,102,111,117,114, 85,102,119, 14, + 57, 4, 7, 1, 1, 64, 79, 89,153,153,153,153,154, 3, 4, 23, 56, 4, 7, + 23, 5, 64, 78,204,204,204,204,204,206, 98,117, 98, 98, 97, 0, 0,128, 0, + 0, 0, 15, 55, 4, 7, 0, 19, 64, 78, 64, 0, 0, 0, 0, 1,120,121,122, + 24, 54, 4, 7, 7, 21, 64, 77,179, 51, 51, 51, 51, 52, 64, 12, 0, 0, 0, + 0, 0, 0,102,111,117,114, 14, 53, 4, 7, 1, 1, 64, 77, 38,102,102,102, + 102,103, 2, 3, 25, 52, 4, 7, 7, 23, 64, 76,153,153,153,153,153,154, 0, + 0, 0, 0, 0, 0, 0, 0, 98,117, 98, 98, 97, 12, 51, 3, 7, 1, 64, 76, + 12,204,204,204,204,205, 15, 23, 50, 4, 7, 18, 7, 64, 75,128, 0, 0, 0, + 0, 1, 85,102,119, 64, 12, 0, 0, 0, 0, 0, 0, 14, 49, 4, 7, 1, 1, + 64, 74,243, 51, 51, 51, 51, 52, 4, 2, 25, 48, 4, 7, 23, 7, 64, 74,102, + 102,102,102,102,103, 98,117, 98, 98, 97, 0, 0, 0, 0, 0, 0, 0, 0, 13, + 47, 4, 7, 0, 1, 64, 73,217,153,153,153,153,154, 15, 23, 46, 4, 7, 7, + 18, 64, 73, 76,204,204,204,204,205, 64, 12, 0, 0, 0, 0, 0, 0, 85,102, + 119, 10, 0, 0, 0, 34, 0, 92, 0, 1,244, 1,231, 1,218, 1,205, 1,192, + 1,179, 1,166, 1,153, 1,140, 1,134, 1,121, 1,108, 1, 95, 1, 82, 1, + 69, 1, 56, 1, 43, 1, 30, 1, 17, 1, 11, 0,254, 0,241, 0,228, 0,215, + 0,202, 0,189, 0,176, 0,163, 0,150, 0,144, 0,131, 0,118, 0,105, 0, + 92, 0, 79, 0, 0, 0, 0, 13, 64, 67, 64, 0, 0, 0, 0, 0, 35, 12, 3, + 7, 1, 64, 66,179, 51, 51, 51, 51, 52, 34, 12, 3, 7, 1, 64, 66, 38,102, + 102,102,102,103, 33, 12, 3, 7, 1, 64, 65,153,153,153,153,153,154, 32, 12, + 3, 7, 1, 64, 65, 12,204,204,204,204,205, 31, 5, 3, 1, 1, 33, 30, 12, + 3, 7, 1, 64, 63,230,102,102,102,102,103, 29, 12, 3, 7, 1, 64, 62,204, + 204,204,204,204,206, 28, 12, 3, 7, 1, 64, 61,179, 51, 51, 51, 51, 52, 27, + 12, 3, 7, 1, 64, 60,153,153,153,153,153,154, 26, 12, 3, 7, 1, 64, 59, + 128, 0, 0, 0, 0, 1, 25, 12, 3, 7, 1, 64, 58,102,102,102,102,102,103, + 24, 12, 3, 7, 1, 64, 57, 76,204,204,204,204,205, 23, 12, 3, 7, 1, 64, + 56, 51, 51, 51, 51, 51, 52, 22, 12, 3, 7, 1, 64, 55, 25,153,153,153,153, + 154, 21, 5, 3, 1, 1, 22, 20, 12, 3, 7, 1, 64, 52,230,102,102,102,102, + 103, 19, 12, 3, 7, 1, 64, 51,204,204,204,204,204,205, 18, 12, 3, 7, 1, + 64, 50,179, 51, 51, 51, 51, 52, 17, 12, 3, 7, 1, 64, 49,153,153,153,153, + 153,154, 16, 12, 3, 7, 1, 64, 48,128, 0, 0, 0, 0, 0, 15, 12, 3, 7, + 1, 64, 46,204,204,204,204,204,206, 14, 12, 3, 7, 1, 64, 44,153,153,153, + 153,153,154, 13, 12, 3, 7, 1, 64, 42,102,102,102,102,102,103, 12, 12, 3, + 7, 1, 64, 40, 51, 51, 51, 51, 51, 52, 11, 5, 3, 1, 1, 11, 10, 12, 3, + 7, 1, 64, 35,204,204,204,204,204,205, 9, 12, 3, 7, 1, 64, 33,153,153, + 153,153,153,154, 8, 12, 3, 7, 1, 64, 30,204,204,204,204,204,206, 7, 12, + 3, 7, 1, 64, 26,102,102,102,102,102,103, 6, 12, 3, 7, 1, 64, 22, 0, + 0, 0, 0, 0, 0, 5, 12, 3, 7, 1, 64, 17,153,153,153,153,153,154, 4, + 12, 3, 7, 1, 64, 10,102,102,102,102,102,103, 3, 12, 3, 7, 1, 64, 1, + 153,153,153,153,153,154, 2, 11, 3, 7, 9, 63,241,153,153,153,153,153,154, + 10, 0, 0, 0, 29, 0,149, 0, 1,243, 1,230, 1,217, 1,204, 1,198, 1, + 185, 1,172, 1,159, 1,146, 1,133, 1,120, 1,107, 1, 94, 1, 81, 1, 68, + 1, 55, 1, 42, 1, 29, 1, 16, 1, 3, 0,246, 0,233, 0,220, 0,207, 0, + 201, 0,188, 0,175, 0,162, 0,149, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 3, 7, + 1, 64, 81,153,153,153,153,153,154, 64, 12, 3, 7, 1, 64, 81, 83, 51, 51, + 51, 51, 52, 63, 12, 3, 7, 1, 64, 81, 12,204,204,204,204,205, 62, 12, 3, + 7, 1, 64, 80,198,102,102,102,102,103, 61, 5, 3, 1, 1, 66, 60, 12, 3, + 7, 1, 64, 80, 57,153,153,153,153,154, 59, 12, 3, 7, 1, 64, 79,230,102, + 102,102,102,103, 58, 12, 3, 7, 1, 64, 79, 89,153,153,153,153,154, 57, 12, + 3, 7, 1, 64, 78,204,204,204,204,204,206, 56, 12, 3, 7, 1, 64, 78, 64, + 0, 0, 0, 0, 1, 55, 12, 3, 7, 1, 64, 77,179, 51, 51, 51, 51, 52, 54, + 12, 3, 7, 1, 64, 77, 38,102,102,102,102,103, 53, 12, 3, 7, 1, 64, 76, + 153,153,153,153,153,154, 52, 12, 3, 7, 1, 64, 76, 12,204,204,204,204,205, + 51, 12, 3, 7, 1, 64, 75,128, 0, 0, 0, 0, 1, 50, 12, 3, 7, 1, 64, + 74,243, 51, 51, 51, 51, 52, 49, 12, 3, 7, 1, 64, 74,102,102,102,102,102, + 103, 48, 12, 3, 7, 1, 64, 73,217,153,153,153,153,154, 47, 12, 3, 7, 1, + 64, 73, 76,204,204,204,204,205, 46, 12, 3, 7, 1, 64, 72,192, 0, 0, 0, + 0, 1, 45, 12, 3, 7, 1, 64, 72, 51, 51, 51, 51, 51, 52, 44, 12, 3, 7, + 1, 64, 71,166,102,102,102,102,103, 43, 12, 3, 7, 1, 64, 71, 25,153,153, + 153,153,154, 42, 12, 3, 7, 1, 64, 70,140,204,204,204,204,205, 41, 5, 3, + 1, 1, 44, 40, 12, 3, 7, 1, 64, 69,115, 51, 51, 51, 51, 52, 39, 12, 3, + 7, 1, 64, 68,230,102,102,102,102,103, 38, 12, 3, 7, 1, 64, 68, 89,153, + 153,153,153,154, 37, 12, 3, 7, 1, 64, 67,204,204,204,204,204,205, 36, 10, + 0, 0, 0, 41, 0,103, 0, 1,250, 1,235, 1,227, 1,218, 1,211, 1,202, + 1,192, 1,179, 1,172, 1,157, 1,149, 1,141, 1,132, 1,125, 1,116, 1, + 106, 1, 93, 1, 86, 1, 74, 1, 63, 1, 47, 1, 40, 1, 31, 1, 16, 1, 8, + 0,255, 0,248, 0,239, 0,229, 0,216, 0,209, 0,197, 0,186, 0,174, 0, + 158, 0,151, 0,136, 0,128, 0,119, 0,112, 0,103, 0, 93, 0, 0, 0, 0, + 10, 55, 44, 78, 85, 76, 76, 8, 2, 25, 52, 54, 44, 51, 46, 53, 6, 2, 21, + 52, 53, 44, 50, 8, 2, 25, 52, 52, 44, 48, 46, 48, 7, 2, 23, 52, 51, 44, + 49, 53, 14, 2, 37, 52, 50, 44, 88, 39, 53, 53, 54, 54, 55, 55, 39, 6, 2, + 21, 52, 49, 44, 52, 15, 2, 39, 52, 48, 44, 50, 49, 52, 55, 52, 56, 51, 54, + 52, 56, 11, 2, 31, 52, 44, 39, 98,117, 98, 98, 97, 39, 10, 2, 29, 51, 57, + 44, 39,120,121,122, 39, 11, 2, 31, 51, 56, 44, 39,102,111,117,114, 39, 6, + 2, 21, 51, 55, 44, 51, 12, 2, 33, 51, 54, 44, 39, 98,117, 98, 98, 97, 39, + 9, 2, 27, 51, 53, 44, 78, 85, 76, 76, 8, 2, 25, 51, 52, 44, 51, 46, 53, + 6, 2, 21, 51, 51, 44, 50, 8, 2, 25, 51, 50, 44, 48, 46, 48, 7, 2, 23, + 51, 49, 44, 49, 53, 14, 2, 37, 51, 48, 44, 88, 39, 53, 53, 54, 54, 55, 55, + 39, 8, 2, 25, 51, 44, 78, 85, 76, 76, 6, 2, 21, 50, 57, 44, 52, 15, 2, + 39, 50, 56, 44, 50, 49, 52, 55, 52, 56, 51, 54, 52, 56, 10, 2, 29, 50, 55, + 44, 39,120,121,122, 39, 11, 2, 31, 50, 54, 44, 39,102,111,117,114, 39, 6, + 2, 21, 50, 53, 44, 51, 12, 2, 33, 50, 52, 44, 39, 98,117, 98, 98, 97, 39, + 9, 2, 27, 50, 51, 44, 78, 85, 76, 76, 8, 2, 25, 50, 50, 44, 51, 46, 53, + 6, 2, 21, 50, 49, 44, 50, 8, 2, 25, 50, 48, 44, 48, 46, 48, 7, 2, 23, + 50, 44, 51, 46, 53, 7, 2, 23, 49, 57, 44, 49, 53, 14, 2, 37, 49, 56, 44, + 88, 39, 53, 53, 54, 54, 55, 55, 39, 6, 2, 21, 49, 55, 44, 52, 12, 2, 33, + 49, 54, 44, 39, 98,117, 98, 98, 97, 39, 9, 2, 27, 49, 53, 44, 78, 85, 76, + 76, 8, 2, 25, 49, 52, 44, 51, 46, 53, 6, 2, 21, 49, 51, 44, 50, 8, 2, + 25, 49, 50, 44, 48, 46, 48, 7, 2, 23, 49, 49, 44, 49, 53, 14, 2, 37, 49, + 48, 44, 88, 39, 53, 53, 54, 54, 55, 55, 39, 5, 2, 19, 49, 44, 50, 10, 0, + 0, 0, 22, 1, 32, 0, 1,243, 1,236, 1,230, 1,215, 1,207, 1,198, 1, + 191, 1,182, 1,172, 1,159, 1,152, 1,140, 1,129, 1,118, 1,102, 1, 95, + 1, 80, 1, 72, 1, 63, 1, 53, 1, 38, 1, 32, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 5, 2, 19, 57, 44, 52, 14, 2, 37, 56, 44, 50, 49, 52, 55, 52, 56, 51, + 54, 52, 56, 9, 2, 27, 55, 44, 39,120,121,122, 39, 8, 2, 25, 54, 52, 44, + 48, 46, 48, 7, 2, 23, 54, 51, 44, 49, 53, 14, 2, 37, 54, 50, 44, 88, 39, + 53, 53, 54, 54, 55, 55, 39, 6, 2, 21, 54, 49, 44, 52, 15, 2, 39, 54, 48, + 44, 50, 49, 52, 55, 52, 56, 51, 54, 52, 56, 10, 2, 29, 54, 44, 39,102,111, + 117,114, 39, 10, 2, 29, 53, 57, 44, 39,120,121,122, 39, 11, 2, 31, 53, 56, + 44, 39,102,111,117,114, 39, 6, 2, 21, 53, 55, 44, 51, 12, 2, 33, 53, 54, + 44, 39, 98,117, 98, 98, 97, 39, 9, 2, 27, 53, 53, 44, 78, 85, 76, 76, 8, + 2, 25, 53, 52, 44, 51, 46, 53, 6, 2, 21, 53, 51, 44, 50, 8, 2, 25, 53, + 50, 44, 48, 46, 48, 7, 2, 23, 53, 49, 44, 49, 53, 14, 2, 37, 53, 48, 44, + 88, 39, 53, 53, 54, 54, 55, 55, 39, 5, 2, 19, 53, 44, 51, 6, 2, 21, 52, + 57, 44, 52, 12, 2, 33, 52, 56, 44, 39, 98,117, 98, 98, 97, 39, +}; + +/* Help message */ +static const char zHelp[] = + "Usage:\n" + " sessionfuzz setup -- Generate seed files c1.txt, c2.txt, etc.\n" + " sessionfuzz run FILE ... -- Run against fuzzed changeset FILE\n" + " sessionfuzz run SQLAR ... -- Run against all files in the SQL Archive\n" +; + +#include <stdio.h> +#include <string.h> +#include <assert.h> +#ifndef OMIT_ZLIB +#include <zlib.h> +#endif + +/* +** Implementation of the "sqlar_uncompress(X,SZ)" SQL function +** +** Parameter SZ is interpreted as an integer. If it is less than or +** equal to zero, then this function returns a copy of X. Or, if +** SZ is equal to the size of X when interpreted as a blob, also +** return a copy of X. Otherwise, decompress blob X using zlib +** utility function uncompress() and return the results (another +** blob). +*/ +static void sqlarUncompressFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ +#ifdef OMIT_ZLIB + sqlite3_result_value(context, argv[0]); +#else + uLong nData; + uLongf sz; + + assert( argc==2 ); + sz = sqlite3_value_int(argv[1]); + + if( sz<=0 || sz==(nData = sqlite3_value_bytes(argv[0])) ){ + sqlite3_result_value(context, argv[0]); + }else{ + const Bytef *pData= sqlite3_value_blob(argv[0]); + Bytef *pOut = sqlite3_malloc(sz); + if( Z_OK!=uncompress(pOut, &sz, pData, nData) ){ + sqlite3_result_error(context, "error in uncompress()", -1); + }else{ + sqlite3_result_blob(context, pOut, sz, SQLITE_TRANSIENT); + } + sqlite3_free(pOut); + } +#endif +} + + +/* Run a chunk of SQL. If any errors happen, print an error message +** and exit. +*/ +static void runSql(sqlite3 *db, const char *zSql){ + int rc; + char *zErr = 0; + rc = sqlite3_exec(db, zSql, 0, 0, &zErr); + if( rc || zErr ){ + fprintf(stderr, "SQL failed: rc=%d zErr=[%s]\n", rc, zErr); + fprintf(stderr, "SQL: [%s]\n", zSql); + exit(1); + } +} + +/* +** Write buffer to disk +*/ +static void writeFile(const char *zFilename, const void *pData, int nData){ + FILE *out; + int n; + out = fopen(zFilename, "wb"); + if( out==0 ){ + fprintf(stderr, "cannot open \"%s\" for writing\n", zFilename); + exit(1); + } + n = (int)fwrite(pData, 1, nData, out); + fclose(out); + if( n!=nData ){ + fprintf(stderr, "only wrote %d of %d bytes to \"%s\"\n",n,nData,zFilename); + exit(1); + } +} + +/* +** Generate a changeset from session pSess and write it to zFile +*/ +static void makeChangeset(const char *zFile, sqlite3_session *pSess){ + void *pChg; + int nChg; + int rc; + rc = sqlite3session_changeset(pSess, &nChg, &pChg); + if( rc ){ + fprintf(stderr, "sqlite3session_changeset() returned %d\n", rc); + exit(1); + } + writeFile(zFile, pChg, nChg); + sqlite3_free(pChg); +} + +/* +** Read a file from disk. Space to hold the answer is obtained from +** sqlite3_malloc64(). +*/ +static void readFile(const char *zName, void **ppData, int *pnData){ + FILE *in = fopen(zName, "rb"); + long nIn; + size_t nRead; + char *pBuf; + *ppData = 0; + *pnData = 0; + if( in==0 ){ + fprintf(stderr, "Cannot open \"%s\" for reading\n", zName); + exit(1); + } + fseek(in, 0, SEEK_END); + nIn = ftell(in); + rewind(in); + pBuf = sqlite3_malloc64( nIn+1 ); + if( pBuf==0 ){ + fprintf(stderr, "Failed to malloc %lld bytes\n", (sqlite3_int64)(nIn+1)); + exit(1); + } + nRead = fread(pBuf, 1, nIn, in); + fclose(in); + if( nRead!=(size_t)nIn ){ + fprintf(stderr, "Read only %d of %d bytes from %s\n", (int)nRead, (int)nIn, + zName); + exit(1); + } + pBuf[nIn] = 0; + *pnData = nIn; + *ppData = pBuf; +} + +/* +** The conflict callback +*/ +static int conflictCall( + void *NotUsed, + int eConflict, + sqlite3_changeset_iter *p +){ + (void)NotUsed; + (void)p; + return SQLITE_CHANGESET_OMIT; +} + +/* +** Reset the database file +*/ +static void db_reset(sqlite3 *db){ + unsigned char *pData; + int nData; + int rc; + + nData = sizeof(aDbBytes); + pData = sqlite3_malloc64( nData ); + if( pData==0 ){ + fprintf(stderr, "could not allocate %d bytes\n", nData); + exit(1); + } + memcpy(pData, aDbBytes, nData); + rc = sqlite3_deserialize(db, 0, pData, nData, nData, + SQLITE_DESERIALIZE_FREEONCLOSE | SQLITE_DESERIALIZE_RESIZEABLE); + if( rc ){ + fprintf(stderr, "sqlite3_deserialize() failed with %d: %s\n", + rc, sqlite3_errmsg(db)); + exit(1); + } +} + +/* +** Given a full file pathname, return a pointer to the tail. +** Example: +** +** input: /home/drh/sqlite/abc.db +** output: abc.db +*/ +static const char *fileTail(const char *z){ + const char *zOut = z; + while( z[0] ){ + if( z[0]=='/' && z[1]!=0 ) zOut = &z[1]; + z++; + } + return zOut; +} + +int main(int argc, char **argv){ + const char *zCmd; + sqlite3 *db; + int rc; + sqlite3_session *pSess; + sqlite3_stmt *pStmt; + void *pChgset; + int nChgset; + int bVerbose = 0; + + if( argc<2 ){ + fprintf(stderr, "%s", zHelp); + exit(1); + } + rc = sqlite3_open_v2(":memory:",&db, + SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, "memdb"); + if( rc ){ + fprintf(stderr, "Failed to open :memory: database: %s\n", + sqlite3_errmsg(db)); + exit(1); + } + db_reset(db); + zCmd = argv[1]; + if( strcmp(zCmd, "setup")==0 ){ + if( argc!=2 ){ + fprintf(stdout, "Wrong number of arguments.\n%s", zHelp); + exit(1); + } + runSql(db, zFillSql); + rc = sqlite3session_create(db, "main", &pSess); + if( rc ){ + fprintf(stderr, "sqlite3session_create() returns %d\n", rc); + exit(1); + } + rc = sqlite3session_attach(pSess, 0); + if( rc ){ + fprintf(stderr, "sqlite3session_attach(db,0) returns %d\n", rc); + exit(1); + } + runSql(db, "INSERT INTO t4(z) VALUES('');"); + makeChangeset("c1.txt", pSess); + runSql(db, + "UPDATE t1 SET b=c, c=b WHERE a IN (5,7);\n" + "DELETE FROM t2 WHERE rowid IN (8,2);\n" + "INSERT OR IGNORE INTO t4 SELECT b FROM t1 WHERE b IS TRUE LIMIT 2;"); + makeChangeset("c2.txt", pSess); + runSql(db, "UPDATE t3 SET x=y, y=NULL WHERE rowid IN (1,3);"); + makeChangeset("c3.txt", pSess); + sqlite3session_delete(pSess); + }else + if( strcmp(zCmd, "run")==0 ){ + int i; + if( argc<3 ){ + fprintf(stdout, "Wrong number of arguments.\n%s", zHelp); + exit(1); + } + for(i=2; i<argc; i++){ + if( strcmp(argv[i],"-v")==0 ){ + bVerbose = 1; + continue; + } + readFile(argv[i], &pChgset, &nChgset); + if( nChgset >= 512 + && memcmp(pChgset, "SQLite format 3", 16)==0 + ){ + sqlite3 *db2; + sqlite3_stmt *pStmt2; + int nCase = 0; + /* This file is an SQL Archive containing many changesets */ + if( !bVerbose ){ printf("%s: ", fileTail(argv[i])); fflush(stdout); } + sqlite3_open_v2(":memory:", &db2, + SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE, "memdb"); + sqlite3_deserialize(db2, 0, pChgset, nChgset, nChgset, + SQLITE_DESERIALIZE_READONLY | SQLITE_DESERIALIZE_FREEONCLOSE); + sqlite3_create_function(db2, "sqlar_uncompress", 2, SQLITE_UTF8, 0, + sqlarUncompressFunc, 0, 0); + rc = sqlite3_prepare_v2(db2, "SELECT name, sqlar_uncompress(data,sz)" + " FROM sqlar", -1, &pStmt2, 0); + if( rc ){ + fprintf(stderr, "SQL error: %s\n", sqlite3_errmsg(db2)); + exit(1); + } + while( SQLITE_ROW==sqlite3_step(pStmt2) ){ + if( bVerbose ){ + printf("%s/%s:", fileTail(argv[i]), sqlite3_column_text(pStmt2,0)); + fflush(stdout); + } + runSql(db, "BEGIN"); + pChgset = (unsigned char*)sqlite3_column_blob(pStmt2, 1); + nChgset = sqlite3_column_bytes(pStmt2, 1); + rc = sqlite3changeset_apply(db, nChgset, pChgset, 0, conflictCall, 0); + if( bVerbose ){ + printf(" Ok. rc=%d\n", rc); + fflush(stdout); + } + runSql(db, "ROLLBACK"); + nCase++; + } + sqlite3_finalize(pStmt2); + sqlite3_close(db2); + if( bVerbose ) printf("%s: ", fileTail(argv[i])); + printf(" %d cases, 0 crashes\n", nCase); + fflush(stdout); + }else{ + /* The named file is just an ordinary changeset */ + printf("%s:", fileTail(argv[i])); + fflush(stdout); + runSql(db, "BEGIN"); + rc = sqlite3changeset_apply(db, nChgset, pChgset, 0, conflictCall, 0); + printf(" %d\n", rc); + fflush(stdout); + runSql(db, "ROLLBACK"); + sqlite3_free(pChgset); + } + } + }else + { + fprintf(stderr, "%s", zHelp); + exit(1); + } + rc = sqlite3_prepare_v2(db, "PRAGMA integrity_check;", -1, &pStmt, 0); + if( rc ){ + fprintf(stderr, "SQL error: %s\n", sqlite3_errmsg(db)); + exit(1); + } + if( sqlite3_step(pStmt)!=SQLITE_ROW + || strcmp((const char*)sqlite3_column_text(pStmt,0),"ok")!=0 + ){ + fprintf(stderr, "Integrity check failed!\n"); + do{ + fprintf(stderr, "%s\n", sqlite3_column_text(pStmt,0)); + }while( sqlite3_step(pStmt)==SQLITE_ROW ); + } + sqlite3_finalize(pStmt); + sqlite3_close(db); + if( sqlite3_memory_used()>0 ){ + fprintf(stderr, "memory leak of %lld bytes\n", + sqlite3_memory_used()); + exit(1); + } + return 0; +} diff --git a/test/shared.test b/test/shared.test index 4eab476582..42292ab40f 100644 --- a/test/shared.test +++ b/test/shared.test @@ -160,11 +160,9 @@ do_test shared-$av.1.8 { } {} do_test shared-$av.2.1 { - # Open connection db3 to the database. Use a different path to the same - # file so that db3 does *not* share the same pager cache as db and db2 - # (there should be two open file handles). - if {$::tcl_platform(platform)=="unix"} { - sqlite3 db3 ./test.db + # Open connection db3 to the database. + if {$::tcl_platform(platform) eq "unix"} { + sqlite3 db3 "file:test.db?cache=private" -uri 1 } else { sqlite3 db3 TEST.DB } @@ -314,7 +312,7 @@ do_test shared-$av.4.1.3 { } {2} # Sanity check: Create a table in ./test.db via handle db, and test that handle -# db2 can "see" the new table immediately. A handle using a seperate pager +# db2 can "see" the new table immediately. A handle using a separate pager # cache would have to reload the database schema before this were possible. # do_test shared-$av.4.2.1 { @@ -799,8 +797,8 @@ do_test shared-$av.10.2 { do_test shared-$av.10.3 { # An external connection should be able to read the database, but not # prepare a write operation. - if {$::tcl_platform(platform)=="unix"} { - sqlite3 db3 ./test.db + if {$::tcl_platform(platform) eq "unix"} { + sqlite3 db3 "file:test.db?cache=private" -uri 1 } else { sqlite3 db3 TEST.DB } @@ -1145,38 +1143,40 @@ do_test shared-$av-16.8 { # Shared cache on named memory databases attached to readonly connections. # -do_test shared-$av-16.8.1 { +if {![sqlite3 -has-codec]} { + do_test shared-$av-16.8.1 { + db1 close + db2 close + + sqlite3 db test1.db + db eval { + CREATE TABLE yy(a, b); + INSERT INTO yy VALUES(77, 88); + } + db close + + sqlite3 db1 test1.db -uri 1 -readonly 1 + sqlite3 db2 test2.db -uri 1 + + db1 eval { + ATTACH 'file:mem?mode=memory&cache=shared' AS shared; + CREATE TABLE shared.xx(a, b); + INSERT INTO xx VALUES(55, 66); + } + db2 eval { + ATTACH 'file:mem?mode=memory&cache=shared' AS shared; + SELECT * FROM xx; + } + } {55 66} + + do_test shared-$av-16.8.2 { db1 eval { SELECT * FROM yy } } {77 88} + do_test shared-$av-16.8.3 { + list [catch {db1 eval { INSERT INTO yy VALUES(1, 2) }} msg] $msg + } {1 {attempt to write a readonly database}} + db1 close db2 close - - sqlite3 db test1.db - db eval { - CREATE TABLE yy(a, b); - INSERT INTO yy VALUES(77, 88); - } - db close - - sqlite3 db1 test1.db -uri 1 -readonly 1 - sqlite3 db2 test2.db -uri 1 - - db1 eval { - ATTACH 'file:mem?mode=memory&cache=shared' AS shared; - CREATE TABLE shared.xx(a, b); - INSERT INTO xx VALUES(55, 66); - } - db2 eval { - ATTACH 'file:mem?mode=memory&cache=shared' AS shared; - SELECT * FROM xx; - } -} {55 66} - -do_test shared-$av-16.8.2 { db1 eval { SELECT * FROM yy } } {77 88} -do_test shared-$av-16.8.3 { - list [catch {db1 eval { INSERT INTO yy VALUES(1, 2) }} msg] $msg -} {1 {attempt to write a readonly database}} - -db1 close -db2 close +} } ;# end of autovacuum on/off loop diff --git a/test/shared3.test b/test/shared3.test index 1d09b69b82..2e32398cac 100644 --- a/test/shared3.test +++ b/test/shared3.test @@ -70,13 +70,14 @@ do_test shared3-2.5 { # test case shared3-2.3 above). The goal of the following tests is to # ensure that the cache-size really is 10 pages. # -if {$::tcl_platform(platform)=="unix"} { - set alternative_name ./test.db -} else { - set alternative_name TEST.DB -} +#if {$::tcl_platform(platform) eq "unix"} { +# set alternative_name ./test.db +#} else { +# set alternative_name TEST.DB +#} do_test shared3-2.6 { - sqlite3 db3 $alternative_name + #sqlite3 db3 $alternative_name + sqlite3 db3 "file:./test.db?cache=private" -uri 1 catchsql {select count(*) from sqlite_master} db3 } {0 1} do_test shared3-2.7 { @@ -96,7 +97,7 @@ do_test shared3-2.8 { # statement above should have caused the pager to grab an exclusive lock # on the database file so that the cache could be spilled. # - catch { sqlite3 db3 $alternative_name } + catch { sqlite3 db3 "file:./test.db?cache=private" -uri 1 } catchsql {select count(*) from sqlite_master} db3 } {1 {database is locked}} diff --git a/test/shared6.test b/test/shared6.test index 499cbb0eb5..18f9f537b6 100644 --- a/test/shared6.test +++ b/test/shared6.test @@ -139,7 +139,7 @@ do_test shared6-1.X { # that connect to the same file using different VFS implementations do # not share a cache. # -if {$::tcl_platform(platform) eq "unix"} { +if {$::tcl_platform(os) ne "Windows NT"} { do_test shared6-2.1 { sqlite3 db1 test.db -vfs unix sqlite3 db2 test.db -vfs unix diff --git a/test/shared8.test b/test/shared8.test index 73f0d47627..f9607262b4 100644 --- a/test/shared8.test +++ b/test/shared8.test @@ -62,6 +62,7 @@ do_test 1.0 { } {1 i 2 ii 3 iii 4 iv} do_test 1.1 { + sqlite3_db_config db1 DEFENSIVE 0 execsql { PRAGMA writable_schema = 1; DELETE FROM sqlite_master WHERE 1; diff --git a/test/shared9.test b/test/shared9.test index 1982a593e3..5bb46c2686 100644 --- a/test/shared9.test +++ b/test/shared9.test @@ -19,7 +19,7 @@ source $testdir/tester.tcl source $testdir/lock_common.tcl set testprefix shared9 -ifcapable !view||!trigger { +ifcapable !view||!trigger||!shared_cache { finish_test return } diff --git a/test/sharedA.test b/test/sharedA.test index 146fb26be0..ef57c89cdf 100644 --- a/test/sharedA.test +++ b/test/sharedA.test @@ -19,6 +19,16 @@ if {[run_thread_tests]==0} { finish_test ; return } db close set ::testprefix sharedA +ifcapable !shared_cache { + finish_test + return +} + +if {[atomic_batch_write test.db]} { + finish_test + return +} + set ::enable_shared_cache [sqlite3_enable_shared_cache 1] #------------------------------------------------------------------------- diff --git a/test/sharedB.test b/test/sharedB.test index 1b15755076..2a7e49cd91 100644 --- a/test/sharedB.test +++ b/test/sharedB.test @@ -24,6 +24,11 @@ if {[run_thread_tests]==0} { finish_test ; return } db close set ::testprefix sharedB +ifcapable !shared_cache { + finish_test + return +} + set ::enable_shared_cache [sqlite3_enable_shared_cache 1] #------------------------------------------------------------------------- diff --git a/test/shared_err.test b/test/shared_err.test index 96e5ee4540..fdc1c13975 100644 --- a/test/shared_err.test +++ b/test/shared_err.test @@ -378,7 +378,7 @@ do_malloc_test shared_err-6 -tclbody { # sqlite3_enable_shared_cache 0 # } msg # set msg -#} {library routine called out of sequence} +#} {bad parameter or other API misuse} # Again provoke a malloc() failure when a cursor position is being saved, # this time during a ROLLBACK operation by some other handle. diff --git a/test/shell1.test b/test/shell1.test index 2fda62ee13..abf214a907 100644 --- a/test/shell1.test +++ b/test/shell1.test @@ -11,6 +11,7 @@ # # The focus of this file is testing the CLI shell tool. # +# TESTRUNNER: shell # # Test plan: @@ -18,18 +19,12 @@ # shell1-1.*: Basic command line option handling. # shell1-2.*: Basic "dot" command token parsing. # shell1-3.*: Basic test that "dot" command can be called. +# shell1-{4-8}.*: Test various "dot" commands's functionality. +# shell1-9.*: Basic test that "dot" commands and SQL intermix ok. # set testdir [file dirname $argv0] source $testdir/tester.tcl -if {$tcl_platform(platform)=="windows"} { - set CLI "sqlite3.exe" -} else { - set CLI "./sqlite3" -} -if {![file executable $CLI]} { - finish_test - return -} +set CLI [test_cli_invocation] db close forcedelete test.db test.db-journal test.db-wal sqlite3 db test.db @@ -53,13 +48,13 @@ do_test shell1-1.1.1b { } {1 1} # error on extra options do_test shell1-1.1.2 { - catchcmd "test.db \"select 3\" \"select 4\"" "" + catchcmd "test.db \"select+3\" \"select+4\"" "" } {0 {3 4}} # error on extra options do_test shell1-1.1.3 { catchcmd "test.db FOO test.db BAD" ".quit" -} {1 {Error: near "FOO": syntax error}} +} {/1 .Error: in prepare, near "FOO": syntax error*/} # -help do_test shell1-1.2.1 { @@ -72,6 +67,10 @@ do_test shell1-1.2.1 { } {1 1 1 1} # -init filename read/process named file +forcedelete FOO +set out [open FOO w] +puts $out "" +close $out do_test shell1-1.3.1 { catchcmd "-init FOO test.db" "" } {0 {}} @@ -80,24 +79,24 @@ do_test shell1-1.3.2 { } {0 {}} do_test shell1-1.3.3 { catchcmd "-init FOO test.db BAD .quit" "" -} {1 {Error: near "BAD": syntax error}} +} {/1 .Error: in prepare, near "BAD": syntax error*/} # -echo print commands before execution do_test shell1-1.4.1 { - catchcmd "-echo test.db" "" + catchcmd "-echo test.db" "" } {0 {}} # -[no]header turn headers on or off do_test shell1-1.5.1 { - catchcmd "-header test.db" "" + catchcmd "-header test.db" "" } {0 {}} do_test shell1-1.5.2 { - catchcmd "-noheader test.db" "" + catchcmd "-noheader test.db" "" } {0 {}} # -bail stop after hitting an error do_test shell1-1.6.1 { - catchcmd "-bail test.db" "" + catchcmd "-bail test.db" "" } {0 {}} # -interactive force interactive I/O @@ -111,40 +110,40 @@ do_test shell1-1.7.1 { # -batch force batch I/O do_test shell1-1.8.1 { - catchcmd "-batch test.db" "" + catchcmd "-batch test.db" "" } {0 {}} # -column set output mode to 'column' do_test shell1-1.9.1 { - catchcmd "-column test.db" "" + catchcmd "-column test.db" "" } {0 {}} # -csv set output mode to 'csv' do_test shell1-1.10.1 { - catchcmd "-csv test.db" "" + catchcmd "-csv test.db" "" } {0 {}} # -html set output mode to HTML do_test shell1-1.11.1 { - catchcmd "-html test.db" "" + catchcmd "-html test.db" "" } {0 {}} # -line set output mode to 'line' do_test shell1-1.12.1 { - catchcmd "-line test.db" "" + catchcmd "-line test.db" "" } {0 {}} # -list set output mode to 'list' do_test shell1-1.13.1 { - catchcmd "-list test.db" "" + catchcmd "-list test.db" "" } {0 {}} # -separator 'x' set output field separator (|) do_test shell1-1.14.1 { - catchcmd "-separator 'x' test.db" "" + catchcmd "-separator 'x' test.db" "" } {0 {}} do_test shell1-1.14.2 { - catchcmd "-separator x test.db" "" + catchcmd "-separator x test.db" "" } {0 {}} do_test shell1-1.14.3 { set res [catchcmd "-separator" ""] @@ -155,7 +154,7 @@ do_test shell1-1.14.3 { # -stats print memory stats before each finalize do_test shell1-1.14b.1 { - catchcmd "-stats test.db" "" + catchcmd "-stats test.db" "" } {0 {}} # -nullvalue 'text' set text string for NULL values @@ -177,13 +176,23 @@ do_test shell1-1.16.1 { set x [catchcmd "-version test.db" ""] } {/3.[0-9.]+ 20\d\d-[01]\d-\d\d \d\d:\d\d:\d\d [0-9a-f]+/} +# Handle no-more-options option +forcedelete ./--db +do_test shell1-1.17.1 { + catchcmd {-- --db "CREATE TABLE T(c1);"} +} {0 {}} +do_test shell1-1.17.2 { + catchcmd {-- --db "SELECT name from sqlite_schema;"} +} {0 T} +forcedelete ./--db + #---------------------------------------------------------------------------- # Test cases shell1-2.*: Basic "dot" command token parsing. # # check first token handling do_test shell1-2.1.1 { - catchcmd "test.db" ".foo" + catchcmd "test.db" ".foo" } {1 {Error: unknown command or invalid arguments: "foo". Enter ".help" for help}} do_test shell1-2.1.2 { catchcmd "test.db" ".\"foo OFF\"" @@ -207,10 +216,10 @@ do_test shell1-2.2.4 { } {0 {}} do_test shell1-2.2.5 { catchcmd "test.db" ".mode \"insert FOO" -} {1 {Error: mode should be one of: ascii column csv html insert line list tabs tcl}} +} {1 {Error: mode should be one of: ascii box column csv html insert json line list markdown qbox quote table tabs tcl}} do_test shell1-2.2.6 { catchcmd "test.db" ".mode \'insert FOO" -} {1 {Error: mode should be one of: ascii column csv html insert line list tabs tcl}} +} {1 {Error: mode should be one of: ascii box column csv html insert json line list markdown qbox quote table tabs tcl}} # check multiple tokens, and quoted tokens do_test shell1-2.3.1 { @@ -238,7 +247,7 @@ do_test shell1-2.3.7 { # check quoted args are unquoted do_test shell1-2.4.1 { catchcmd "test.db" ".mode FOO" -} {1 {Error: mode should be one of: ascii column csv html insert line list tabs tcl}} +} {1 {Error: mode should be one of: ascii box column csv html insert json line list markdown qbox quote table tabs tcl}} do_test shell1-2.4.2 { catchcmd "test.db" ".mode csv" } {0 {}} @@ -246,6 +255,11 @@ do_test shell1-2.4.2 { catchcmd "test.db" ".mode \"csv\"" } {0 {}} +# check that certain quoted arg escapes work +do_test shell1-2.5.1 { + catchcmd ":memory:" ".print \"\\060\\077 \\x3f\\x30 \\a\\t\"" +} [list 0 "0? ?0 \a\t"] + #---------------------------------------------------------------------------- # Test cases shell1-3.*: Basic test that "dot" command can be called. @@ -255,6 +269,7 @@ do_test shell1-2.4.2 { do_test shell1-3.1.1 { catchcmd "test.db" ".backup" } {1 {missing FILENAME argument on .backup}} +forcedelete FOO do_test shell1-3.1.2 { catchcmd "test.db" ".backup FOO" } {0 {}} @@ -264,7 +279,7 @@ do_test shell1-3.1.3 { do_test shell1-3.1.4 { # too many arguments catchcmd "test.db" ".backup FOO BAR BAD" -} {1 {too many arguments to .backup}} +} {1 {Usage: .backup ?DB? ?OPTIONS? FILENAME}} # .bail ON|OFF Stop after hitting an error. Default OFF do_test shell1-3.2.1 { @@ -281,14 +296,18 @@ do_test shell1-3.2.4 { catchcmd "test.db" ".bail OFF BAD" } {1 {Usage: .bail on|off}} +# This test will not work on winrt, as winrt has no concept of the absolute +# paths that the test expects in the result. It uses relative paths only. +ifcapable vtab&&!winrt { # .databases List names and files of attached databases do_test shell1-3.3.1 { catchcmd "-csv test.db" ".databases" -} "/0 +.*main +[string map {/ .} [string range [get_pwd] 0 10]].*/" +} "/0.+main.+[string map {/ ".{1,2}"} [string range [get_pwd] 0 10]].*/" do_test shell1-3.3.2 { # extra arguments ignored catchcmd "test.db" ".databases BAD" -} "/0 +.*main +[string map {/ .} [string range [get_pwd] 0 10]].*/" +} "/0.+main.+[string map {/ ".{1,2}"} [string range [get_pwd] 0 10]].*/" +} # .dump ?TABLE? ... Dump the database in an SQL text format # If TABLE specified, only dump tables matching @@ -303,10 +322,11 @@ do_test shell1-3.4.2 { list [regexp {BEGIN TRANSACTION;} $res] \ [regexp {COMMIT;} $res] } {1 1} -do_test shell1-3.4.3 { - # too many arguments - catchcmd "test.db" ".dump FOO BAD" -} {1 {Usage: .dump ?LIKE-PATTERN?}} +# The .dump command now accepts multiple arguments +#do_test shell1-3.4.3 { +# # too many arguments +# catchcmd "test.db" ".dump FOO BAD" +#} {1 {Usage: .dump ?--preserve-rowids? ?--newlines? ?LIKE-PATTERN?}} # .echo ON|OFF Turn command echo on or off do_test shell1-3.5.1 { @@ -384,7 +404,7 @@ do_test shell1-3.10.1 { } {1 1 1} do_test shell1-3.10.2 { # we allow .help to take extra args (it is help after all) - set res [catchcmd "test.db" ".help BAD"] + set res [catchcmd "test.db" ".help *"] # look for a few of the possible help commands list [regexp {.help} $res] \ [regexp {.quit} $res] \ @@ -394,17 +414,14 @@ do_test shell1-3.10.2 { # .import FILE TABLE Import data from FILE into TABLE do_test shell1-3.11.1 { catchcmd "test.db" ".import" -} {1 {Usage: .import FILE TABLE}} +} {/1 .ERROR: missing FILE argument.*/} do_test shell1-3.11.2 { catchcmd "test.db" ".import FOO" -} {1 {Usage: .import FILE TABLE}} -#do_test shell1-3.11.2 { -# catchcmd "test.db" ".import FOO BAR" -#} {1 {Error: no such table: BAR}} +} {/1 .ERROR: missing TABLE argument.*/} do_test shell1-3.11.3 { # too many arguments catchcmd "test.db" ".import FOO BAR BAD" -} {1 {Usage: .import FILE TABLE}} +} {/1 .ERROR: extra argument: "BAD".*./} # .indexes ?TABLE? Show names of all indexes # If TABLE specified, only show indexes for tables @@ -435,10 +452,10 @@ do_test shell1-3.12.3 { # tcl TCL list elements do_test shell1-3.13.1 { catchcmd "test.db" ".mode" -} {1 {Error: mode should be one of: ascii column csv html insert line list tabs tcl}} +} {0 {current output mode: list --escape ascii}} do_test shell1-3.13.2 { catchcmd "test.db" ".mode FOO" -} {1 {Error: mode should be one of: ascii column csv html insert line list tabs tcl}} +} {1 {Error: mode should be one of: ascii box column csv html insert json line list markdown qbox quote table tabs tcl}} do_test shell1-3.13.3 { catchcmd "test.db" ".mode csv" } {0 {}} @@ -468,17 +485,6 @@ do_test shell1-3.13.11 { catchcmd "test.db" ".mode tcl BAD" } {0 {}} -# don't allow partial mode type matches -do_test shell1-3.13.12 { - catchcmd "test.db" ".mode l" -} {1 {Error: mode should be one of: ascii column csv html insert line list tabs tcl}} -do_test shell1-3.13.13 { - catchcmd "test.db" ".mode li" -} {1 {Error: mode should be one of: ascii column csv html insert line list tabs tcl}} -do_test shell1-3.13.14 { - catchcmd "test.db" ".mode lin" -} {0 {}} - # .nullvalue STRING Print STRING in place of NULL values do_test shell1-3.14.1 { catchcmd "test.db" ".nullvalue" @@ -493,15 +499,30 @@ do_test shell1-3.14.3 { # .output FILENAME Send output to FILENAME do_test shell1-3.15.1 { - catchcmd "test.db" ".output" -} {0 {}} + catchcmd "test.db" ".output +.print x" +} {0 x} do_test shell1-3.15.2 { - catchcmd "test.db" ".output FOO" -} {0 {}} + catchcmd "test.db" ".output FOO +.print x +.output +SELECT readfile('FOO');" +} {0 {x +}} do_test shell1-3.15.3 { # too many arguments catchcmd "test.db" ".output FOO BAD" -} {1 {Usage: .output FILE}} +} {1 {ERROR: extra parameter: "BAD". Usage: +.output ?FILE? Send output to FILE or stdout if FILE is omitted + If FILE begins with '|' then open it as a pipe. + If FILE is 'off' then output is disabled. + Options: + --bom Prefix output with a UTF8 byte-order mark + -e Send output to the system text editor + --plain Use text/plain for -w option + -w Send output to a web browser + -x Send output as CSV to a spreadsheet +child process exited abnormally}} # .output stdout Send output to the screen do_test shell1-3.16.1 { @@ -510,7 +531,17 @@ do_test shell1-3.16.1 { do_test shell1-3.16.2 { # too many arguments catchcmd "test.db" ".output stdout BAD" -} {1 {Usage: .output FILE}} +} {1 {ERROR: extra parameter: "BAD". Usage: +.output ?FILE? Send output to FILE or stdout if FILE is omitted + If FILE begins with '|' then open it as a pipe. + If FILE is 'off' then output is disabled. + Options: + --bom Prefix output with a UTF8 byte-order mark + -e Send output to the system text editor + --plain Use text/plain for -w option + -w Send output to a web browser + -x Send output as CSV to a spreadsheet +child process exited abnormally}} # .prompt MAIN CONTINUE Replace the standard prompts do_test shell1-3.17.1 { @@ -564,6 +595,7 @@ do_test shell1-3.20.4 { catchcmd "test.db" ".restore FOO BAR BAD" } {1 {Usage: .restore ?DB? FILE}} +ifcapable vtab { # .schema ?TABLE? Show the CREATE statements # If TABLE specified, only show tables matching # LIKE pattern TABLE. @@ -576,7 +608,7 @@ do_test shell1-3.21.2 { do_test shell1-3.21.3 { # too many arguments catchcmd "test.db" ".schema FOO BAD" -} {1 {Usage: .schema ?LIKE-PATTERN?}} +} {1 {Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?}} do_test shell1-3.21.4 { catchcmd "test.db" { @@ -586,9 +618,13 @@ do_test shell1-3.21.4 { } catchcmd "test.db" ".schema" } {0 {CREATE TABLE t1(x); -CREATE VIEW v2 AS SELECT x+1 AS y FROM t1; -CREATE VIEW v1 AS SELECT y+1 FROM v2;}} -db eval {DROP VIEW v1; DROP VIEW v2; DROP TABLE t1;} +CREATE VIEW v2 AS SELECT x+1 AS y FROM t1 +/* v2(y) */; +CREATE VIEW v1 AS SELECT y+1 FROM v2 +/* v1("y+1") */;}} + + catch {db eval {DROP VIEW v1; DROP VIEW v2; DROP TABLE t1;}} +} # .separator STRING Change column separator used by output and .import do_test shell1-3.22.1 { @@ -625,9 +661,9 @@ do_test shell1-3.23.2 { } {1 {Usage: .show}} # .stats ON|OFF Turn stats on or off -do_test shell1-3.23b.1 { - catchcmd "test.db" ".stats" -} {1 {Usage: .stats on|off}} +#do_test shell1-3.23b.1 { +# catchcmd "test.db" ".stats" +#} {1 {Usage: .stats on|off|stmt|vmstep}} do_test shell1-3.23b.2 { catchcmd "test.db" ".stats ON" } {0 {}} @@ -637,7 +673,20 @@ do_test shell1-3.23b.3 { do_test shell1-3.23b.4 { # too many arguments catchcmd "test.db" ".stats OFF BAD" -} {1 {Usage: .stats on|off}} +} {1 {Usage: .stats ?on|off|stmt|vmstep?}} + +# Ticket 7be932dfa60a8a6b3b26bcf7623ec46e0a403ddb 2018-06-07 +# Adverse interaction between .stats and .eqp +# +do_test shell1-3.23b.5 { + catchcmd "test.db" [string map {"\n " "\n"} { + CREATE TEMP TABLE t1(x); + INSERT INTO t1 VALUES(1),(2); + .stats on + .eqp full + SELECT * FROM t1; + }] +} {/1\n2\n/} # .tables ?TABLE? List names of tables # If TABLE specified, only list tables matching @@ -686,11 +735,11 @@ do_test shell1-3.26.4 { # this should be treated the same as a '1' width for col 1 and 2 } {0 {}} do_test shell1-3.26.5 { - catchcmd "test.db" ".mode column\n.width 10 -10\nSELECT 'abcdefg', 123456;" + catchcmd "test.db" ".mode column\n.header off\n.width 10 -10\nSELECT 'abcdefg', 123456;" # this should be treated the same as a '1' width for col 1 and 2 } {0 {abcdefg 123456}} do_test shell1-3.26.6 { - catchcmd "test.db" ".mode column\n.width -10 10\nSELECT 'abcdefg', 123456;" + catchcmd "test.db" ".mode column\n.header off\n.width -10 10\nSELECT 'abcdefg', 123456;" # this should be treated the same as a '1' width for col 1 and 2 } {0 { abcdefg 123456 }} @@ -699,9 +748,12 @@ do_test shell1-3.26.6 { do_test shell1-3.27.1 { catchcmd "test.db" ".timer" } {1 {Usage: .timer on|off}} -do_test shell1-3.27.2 { - catchcmd "test.db" ".timer ON" -} {0 {}} +ifcapable !winrt { + # No timer support on winrt. + do_test shell1-3.27.2 { + catchcmd "test.db" ".timer ON" + } {0 {}} +} do_test shell1-3.27.3 { catchcmd "test.db" ".timer OFF" } {0 {}} @@ -746,19 +798,217 @@ do_test shell1-4.1 { } {0 {PRAGMA foreign_keys=OFF; BEGIN TRANSACTION; CREATE TABLE t1(x); -INSERT INTO "t1" VALUES(NULL); -INSERT INTO "t1" VALUES(''); -INSERT INTO "t1" VALUES(1); -INSERT INTO "t1" VALUES(2.25); -INSERT INTO "t1" VALUES('hello'); -INSERT INTO "t1" VALUES(X'807F'); +INSERT INTO t1 VALUES(NULL); +INSERT INTO t1 VALUES(''); +INSERT INTO t1 VALUES(1); +INSERT INTO t1 VALUES(2.25); +INSERT INTO t1 VALUES('hello'); +INSERT INTO t1 VALUES(X'807f'); +CREATE TABLE t3(x,y); +INSERT INTO t3 VALUES(1,NULL); +INSERT INTO t3 VALUES(2,''); +INSERT INTO t3 VALUES(3,1); +INSERT INTO t3 VALUES(4,2.25); +INSERT INTO t3 VALUES(5,'hello'); +INSERT INTO t3 VALUES(6,X'807f'); +COMMIT;}} + + +ifcapable vtab { + +# The --preserve-rowids option to .dump +# +do_test shell1-4.1.1 { + catchcmd test.db {.dump --preserve-rowids} +} {0 {PRAGMA foreign_keys=OFF; +BEGIN TRANSACTION; +CREATE TABLE t1(x); +INSERT INTO t1(rowid,x) VALUES(1,NULL); +INSERT INTO t1(rowid,x) VALUES(2,''); +INSERT INTO t1(rowid,x) VALUES(3,1); +INSERT INTO t1(rowid,x) VALUES(4,2.25); +INSERT INTO t1(rowid,x) VALUES(5,'hello'); +INSERT INTO t1(rowid,x) VALUES(6,X'807f'); CREATE TABLE t3(x,y); -INSERT INTO "t3" VALUES(1,NULL); -INSERT INTO "t3" VALUES(2,''); -INSERT INTO "t3" VALUES(3,1); -INSERT INTO "t3" VALUES(4,2.25); -INSERT INTO "t3" VALUES(5,'hello'); -INSERT INTO "t3" VALUES(6,X'807F'); +INSERT INTO t3(rowid,x,y) VALUES(1,1,NULL); +INSERT INTO t3(rowid,x,y) VALUES(2,2,''); +INSERT INTO t3(rowid,x,y) VALUES(3,3,1); +INSERT INTO t3(rowid,x,y) VALUES(4,4,2.25); +INSERT INTO t3(rowid,x,y) VALUES(5,5,'hello'); +INSERT INTO t3(rowid,x,y) VALUES(6,6,X'807f'); +COMMIT;}} + +# If the table contains an INTEGER PRIMARY KEY, do not record a separate +# rowid column in the output. +# +do_test shell1-4.1.2 { + db close + forcedelete test2.db + sqlite3 db test2.db + db eval { + CREATE TABLE t1(x INTEGER PRIMARY KEY, y); + INSERT INTO t1 VALUES(1,null), (2,''), (3,1), + (4,2.25), (5,'hello'), (6,x'807f'); + } + catchcmd test2.db {.dump --preserve-rowids} +} {0 {PRAGMA foreign_keys=OFF; +BEGIN TRANSACTION; +CREATE TABLE t1(x INTEGER PRIMARY KEY, y); +INSERT INTO t1 VALUES(1,NULL); +INSERT INTO t1 VALUES(2,''); +INSERT INTO t1 VALUES(3,1); +INSERT INTO t1 VALUES(4,2.25); +INSERT INTO t1 VALUES(5,'hello'); +INSERT INTO t1 VALUES(6,X'807f'); +COMMIT;}} + +# Verify that the table named [table] is correctly quoted and that +# an INTEGER PRIMARY KEY DESC is not an alias for the rowid. +# +do_test shell1-4.1.3 { + db close + forcedelete test2.db + sqlite3 db test2.db + db eval { + CREATE TABLE [table](x INTEGER PRIMARY KEY DESC, y); + INSERT INTO [table] VALUES(1,null), (12,''), (23,1), + (34,2.25), (45,'hello'), (56,x'807f'); + } + catchcmd test2.db {.dump --preserve-rowids} +} {0 {PRAGMA foreign_keys=OFF; +BEGIN TRANSACTION; +CREATE TABLE [table](x INTEGER PRIMARY KEY DESC, y); +INSERT INTO "table"(rowid,x,y) VALUES(1,1,NULL); +INSERT INTO "table"(rowid,x,y) VALUES(2,12,''); +INSERT INTO "table"(rowid,x,y) VALUES(3,23,1); +INSERT INTO "table"(rowid,x,y) VALUES(4,34,2.25); +INSERT INTO "table"(rowid,x,y) VALUES(5,45,'hello'); +INSERT INTO "table"(rowid,x,y) VALUES(6,56,X'807f'); +COMMIT;}} + +# Do not record rowids for a WITHOUT ROWID table. Also check correct quoting +# of table names that contain odd characters. +# +do_test shell1-4.1.4 { + db close + forcedelete test2.db + sqlite3 db test2.db + db eval { + CREATE TABLE [ta<>ble](x INTEGER PRIMARY KEY, y) WITHOUT ROWID; + INSERT INTO [ta<>ble] VALUES(1,null), (12,''), (23,1), + (34,2.25), (45,'hello'), (56,x'807f'); + } + catchcmd test2.db {.dump --preserve-rowids} +} {0 {PRAGMA foreign_keys=OFF; +BEGIN TRANSACTION; +CREATE TABLE [ta<>ble](x INTEGER PRIMARY KEY, y) WITHOUT ROWID; +INSERT INTO "ta<>ble" VALUES(1,NULL); +INSERT INTO "ta<>ble" VALUES(12,''); +INSERT INTO "ta<>ble" VALUES(23,1); +INSERT INTO "ta<>ble" VALUES(34,2.25); +INSERT INTO "ta<>ble" VALUES(45,'hello'); +INSERT INTO "ta<>ble" VALUES(56,X'807f'); +COMMIT;}} + +# Do not record rowids if the rowid is inaccessible +# +do_test shell1-4.1.5 { + db close + forcedelete test2.db + sqlite3 db test2.db + db eval { + CREATE TABLE t1(_ROWID_,rowid,oid); + INSERT INTO t1 VALUES(1,null,'alpha'), (12,'',99), (23,1,x'b0b1b2'); + } + catchcmd test2.db {.dump --preserve-rowids} +} {0 {PRAGMA foreign_keys=OFF; +BEGIN TRANSACTION; +CREATE TABLE t1(_ROWID_,rowid,oid); +INSERT INTO t1 VALUES(1,NULL,'alpha'); +INSERT INTO t1 VALUES(12,'',99); +INSERT INTO t1 VALUES(23,1,X'b0b1b2'); +COMMIT;}} + +} else { + +do_test shell1-4.1.6 { + db close + forcedelete test2.db + sqlite3 db test2.db + db eval { + CREATE TABLE t1(x INTEGER PRIMARY KEY, y); + INSERT INTO t1 VALUES(1,null), (2,''), (3,1), + (4,2.25), (5,'hello'), (6,x'807f'); + } + catchcmd test2.db {.dump --preserve-rowids} +} {1 {The --preserve-rowids option is not compatible with SQLITE_OMIT_VIRTUALTABLE}} + +} + +# DELETE content of sqlite_sequence prior to repopulating, +# but only if the sqlite_sequence table is non-empty. +# Forum: 2024-10-13T17:10:01z and 2025-10-29T19:38:43z +# +do_test shell1-4.1.7 { + db close + forcedelete test2.db + sqlite3 db test2.db + db eval { + CREATE TABLE t1(a INTEGER PRIMARY KEY AUTOINCREMENT, b); + INSERT INTO t1 VALUES(1,2),(20,21),(15,16); + } + catchcmd test2.db {.dump} +} {0 {PRAGMA foreign_keys=OFF; +BEGIN TRANSACTION; +CREATE TABLE t1(a INTEGER PRIMARY KEY AUTOINCREMENT, b); +INSERT INTO t1 VALUES(1,2); +INSERT INTO t1 VALUES(15,16); +INSERT INTO t1 VALUES(20,21); +PRAGMA writable_schema=ON; +CREATE TABLE IF NOT EXISTS sqlite_sequence(name,seq); +DELETE FROM sqlite_sequence; +INSERT INTO sqlite_sequence VALUES('t1',20); +PRAGMA writable_schema=OFF; +COMMIT;}} +do_test shell1-4.1.8 { + db close + forcedelete test2.db + sqlite3 db test2.db + db eval { + CREATE TABLE t1(a INTEGER PRIMARY KEY AUTOINCREMENT, b); + INSERT INTO t1 VALUES(1,2),(20,21),(15,16); + CREATE TABLE t2(x,y); + INSERT INTO t2 VALUES(99,88); + DROP TABLE t1; + } + catchcmd test2.db {.dump} +} {0 {PRAGMA foreign_keys=OFF; +BEGIN TRANSACTION; +CREATE TABLE t2(x,y); +INSERT INTO t2 VALUES(99,88); +COMMIT;}} +do_test shell1-4.1.9 { + db close + forcedelete test2.db + sqlite3 db test2.db + db eval { + CREATE TABLE t1(a INTEGER PRIMARY KEY AUTOINCREMENT, b); + INSERT INTO t1 VALUES(1,2),(20,21),(15,16); + CREATE TABLE t2(x,y); + INSERT INTO t2 VALUES(99,88); + INSERT INTO sqlite_sequence VALUES('extra',999); + DROP TABLE t1; + } + catchcmd test2.db {.dump} +} {0 {PRAGMA foreign_keys=OFF; +BEGIN TRANSACTION; +CREATE TABLE t2(x,y); +INSERT INTO t2 VALUES(99,88); +PRAGMA writable_schema=ON; +CREATE TABLE IF NOT EXISTS sqlite_sequence(name,seq); +DELETE FROM sqlite_sequence; +INSERT INTO sqlite_sequence VALUES('extra',999); +PRAGMA writable_schema=OFF; COMMIT;}} # Test the output of ".mode insert" @@ -861,6 +1111,12 @@ do_test shell1-4.6 { ";" "$"} 7} +# Test the output of ".mode quote" +# +do_test shell1-4.7 { + catchcmd test.db ".mode quote\nselect x'0123456789ABCDEF';" +} {0 X'0123456789abcdef'} + # Test using arbitrary byte data with the shell via standard input/output. # do_test shell1-5.0 { @@ -875,13 +1131,20 @@ do_test shell1-5.0 { # return character (and on Windows, the end-of-file character) # cannot be used here. # - if {$i==0x0D || ($tcl_platform(platform)=="windows" && $i==0x1A)} { + if {$i==0x0D || ($tcl_platform(platform) eq "windows" && $i==0x1A)} { continue } + # Tcl 8.7 maps 0x80 through 0x9f into valid UTF8. So skip those tests. + if {$i>=0x80} { + if {$i<=0x9F || $tcl_version>=9.0} continue + if {$tcl_platform(platform) eq "windows"} continue + } + if {$i>=0xE0 && $tcl_platform(os) eq "OpenBSD"} continue + if {$i>=0xE0 && $i<=0xEF && $tcl_platform(os) eq "Linux"} continue set hex [format %02X $i] set char [subst \\x$hex]; set oldChar $char set escapes [list] - if {$tcl_platform(platform)=="windows"} { + if {$tcl_platform(platform) eq "windows"} { # # NOTE: On Windows, we need to escape all the whitespace characters, # the alarm (\a) character, and those with special meaning to @@ -889,7 +1152,7 @@ do_test shell1-5.0 { # set escapes [list \ \a \\a \b \\b \t \\t \n \\n \v \\v \f \\f \r \\r \ - " " "\" \"" \" \\\" ' \"'\" \\ \\\\] + " " "\" \"" \" \\\" \\ \\\\] } else { # # NOTE: On Unix, we need to escape most of the whitespace characters @@ -903,19 +1166,225 @@ do_test shell1-5.0 { # set escapes [list \ \t \\t \n \\n \v \\v \f \\f \ - " " "\" \"" \" \\\" ' \"'\" \\ \\\\] + " " "\" \"" \" \\\" \\ \\\\] } set char [string map $escapes $char] - set x [catchcmdex test.db ".print $char\n"] + set x [catchcmdex test.db ".print \"$char\"\n"] set code [lindex $x 0] set res [lindex $x 1] if {$code ne "0"} { error "failed with error: $res" } if {$res ne "$oldChar\n"} { - error "failed with byte $hex mismatch" + if {[llength $res] > 0} { + set got [format %02X [scan $res %c]] + } else { + set got <empty> + } + error "failed with byte $hex mismatch, got $got" } } } {} +# These test cases do not work on MinGW +if 0 { + +# The string used here is the word "test" in Chinese. +# In UTF-8, it is encoded as: \xE6\xB5\x8B\xE8\xAF\x95 +set test \u6D4B\u8BD5 + +do_test shell1-6.0 { + set fileName $test; append fileName .db + catch {forcedelete $fileName} + set x [catchcmdex $fileName "CREATE TABLE t1(x);\n.schema\n"] + set code [lindex $x 0] + set res [string trim [lindex $x 1]] + if {$code ne "0"} { + error "failed with error: $res" + } + if {$res ne "CREATE TABLE t1(x);"} { + error "failed with mismatch: $res" + } + if {![file exists $fileName]} { + error "file \"$fileName\" (Unicode) does not exist" + } + forcedelete $fileName +} {} + +do_test shell1-6.1 { + catch {forcedelete test3.db} + set x [catchcmdex test3.db \ + "CREATE TABLE [encoding convertto utf-8 $test](x);\n.schema\n"] + set code [lindex $x 0] + set res [string trim [lindex $x 1]] + if {$code ne "0"} { + error "failed with error: $res" + } + if {$res ne "CREATE TABLE ${test}(x);"} { + error "failed with mismatch: $res" + } + forcedelete test3.db +} {} +} + +db close +forcedelete test.db test.db-journal test.db-wal +sqlite3 db test.db + +# The shell tool ".schema" command uses virtual table "pragma_database_list" +# +ifcapable vtab { + +do_test shell1-7.1.1 { + db eval { + CREATE TABLE Z (x TEXT PRIMARY KEY); + CREATE TABLE _ (x TEXT PRIMARY KEY); + CREATE TABLE YY (x TEXT PRIMARY KEY); + CREATE TABLE __ (x TEXT PRIMARY KEY); + CREATE TABLE WWW (x TEXT PRIMARY KEY); + CREATE TABLE ___ (x TEXT PRIMARY KEY); + } +} {} +do_test shell1-7.1.2 { + catchcmd "test.db" ".schema _" +} {0 {CREATE TABLE Z (x TEXT PRIMARY KEY); +CREATE TABLE _ (x TEXT PRIMARY KEY);}} +do_test shell1-7.1.3 { + catchcmd "test.db" ".schema \"\\\\_\"" +} {0 {CREATE TABLE _ (x TEXT PRIMARY KEY);}} +do_test shell1-7.1.4 { + catchcmd "test.db" ".schema __" +} {0 {CREATE TABLE YY (x TEXT PRIMARY KEY); +CREATE TABLE __ (x TEXT PRIMARY KEY);}} +do_test shell1-7.1.5 { + catchcmd "test.db" ".schema \"\\\\_\\\\_\"" +} {0 {CREATE TABLE __ (x TEXT PRIMARY KEY);}} +do_test shell1-7.1.6 { + catchcmd "test.db" ".schema ___" +} {0 {CREATE TABLE WWW (x TEXT PRIMARY KEY); +CREATE TABLE ___ (x TEXT PRIMARY KEY);}} +do_test shell1-7.1.7 { + catchcmd "test.db" ".schema \"\\\\_\\\\_\\\\_\"" +} {0 {CREATE TABLE ___ (x TEXT PRIMARY KEY);}} + +} + +# Test case for the ieee754 and decimal extensions in the shell. +# See the "floatingpoint.html" file in the documentation for more +# information. +# +do_test shell1-8.1 { + catchcmd ":memory:" { + -- The pow2 table will hold all the necessary powers of two. + CREATE TABLE pow2(x INTEGER PRIMARY KEY, v TEXT); + WITH RECURSIVE c(x,v) AS ( + VALUES(0,'1') + UNION ALL + SELECT x+1, decimal_mul(v,'2') FROM c WHERE x+1<=971 + ) INSERT INTO pow2(x,v) SELECT x, v FROM c; + WITH RECURSIVE c(x,v) AS ( + VALUES(-1,'0.5') + UNION ALL + SELECT x-1, decimal_mul(v,'0.5') FROM c WHERE x-1>=-1075 + ) INSERT INTO pow2(x,v) SELECT x, v FROM c; + + -- This query finds the decimal representation of each value in the "c" table. + WITH c(n) AS (VALUES(47.49)) + ----XXXXX----------- Replace with whatever you want + SELECT decimal_mul(ieee754_mantissa(c.n),pow2.v) + FROM pow2, c WHERE pow2.x=ieee754_exponent(c.n); + } +} {0 47.49000000000000198951966012828052043914794921875} +do_test_with_ansi_output shell1-8.2 { + catchcmd :memory: { +.mode box +SELECT ieee754(47.49) AS x; + } +} {0 {┌───────────────────────────────┐ +│ x │ +├───────────────────────────────┤ +│ ieee754(6683623321994527,-47) │ +└───────────────────────────────┘}} +do_test_with_ansi_output shell1-8.3 { + catchcmd ":memory: --box" { + select ieee754(6683623321994527,-47) as x; + } +} {0 {┌───────┐ +│ x │ +├───────┤ +│ 47.49 │ +└───────┘}} +do_test shell1-8.4 { + catchcmd ":memory: --table" {SELECT ieee754_mantissa(47.49) AS M, ieee754_exponent(47.49) AS E;} +} {0 {+------------------+-----+ +| M | E | ++------------------+-----+ +| 6683623321994527 | -47 | ++------------------+-----+}} +do_test_with_ansi_output shell1-8.5 { + catchcmd ":memory: --box" { +create table t(a text, b int); +insert into t values ('too long for one line', 1), ('shorter', NULL); +.header on +.width 10 10 +.nullvalue NADA +select * from t;} +} {0 {┌────────────┬────────────┐ +│ a │ b │ +├────────────┼────────────┤ +│ too long f │ 1 │ +│ or one lin │ │ +│ e │ │ +├────────────┼────────────┤ +│ shorter │ NADA │ +└────────────┴────────────┘}} + +#---------------------------------------------------------------------------- +# Test cases shell1-9.*: Basic test that "dot" commands and SQL intermix ok. +# +do_test shell1-9.1 { + catchcmd :memory: { +.mode csv +/* +x */ select 1,2; --x + -- .nada +; +.mode csv +--x +select 2,1; select 3,4; +} +} {0 {1,2 +2,1 +3,4}} + +#---------------------------------------------------------------------------- +# Test cases shell1-10.*: Test that certain static extensions are there. +# +do_test shell1-10.1 { + catchcmd :memory: { +.mode list +.header off +select base64(base64(cast('digity-doo' as blob))), + base85(base85(cast('digity-doo' as blob))); +} +} {0 digity-doo|digity-doo} + +#---------------------------------------------------------------------------- +# Test cases shell1-11.*: +# +do_test shell1-11.1 { + catchcmd :memory: { +.mode list +.header off +select base64(zeroblob(2000000000)); +} +} {/1.*too big.*/} +do_test shell1-11.2 { + catchcmd :memory: { +.mode list +.header off +select base85(zeroblob(2000000000)); +} +} {/1.*too big.*/} + finish_test diff --git a/test/shell2.test b/test/shell2.test index 616610bd4b..5f700a9a1d 100644 --- a/test/shell2.test +++ b/test/shell2.test @@ -8,6 +8,7 @@ # May you share freely, never taking more than you give. # #*********************************************************************** +# TESTRUNNER: shell # # The focus of this file is testing the CLI shell tool. # @@ -20,15 +21,7 @@ # set testdir [file dirname $argv0] source $testdir/tester.tcl -if {$tcl_platform(platform)=="windows"} { - set CLI "sqlite3.exe" -} else { - set CLI "./sqlite3" -} -if {![file executable $CLI]} { - finish_test - return -} +set CLI [test_find_cli] db close forcedelete test.db test.db-journal test.db-wal sqlite3 db test.db @@ -51,10 +44,10 @@ do_test shell2-1.1.1 { # Shell silently ignores extra parameters. # Ticket [f5cb008a65]. do_test shell2-1.2.1 { - set rc [catch { eval exec $CLI \":memory:\" \"select 3\" \"select 4\" } msg] - list $rc $msg + catchcmdex {:memory: "select+3" "select+4"} } {0 {3 -4}} +4 +}} # Test a problem reported on the mailing list. The shell was at one point # returning the generic SQLITE_ERROR message ("SQL error or missing database") @@ -71,7 +64,7 @@ do_test shell2-1.3 { UPDATE OR REPLACE t5 SET a = 4 WHERE a = 1; } -} {1 {Error: near line 9: too many levels of trigger recursion}} +} {1 {Runtime error near line 9: too many levels of trigger recursion}} @@ -131,7 +124,7 @@ SELECT * FROM foo;} # NB. whitespace is important do_test shell2-1.4.5 { forcedelete foo.db - catchcmd "foo.db" {.echo ON + catchcmdex "foo.db" {.echo ON CREATE TABLE foo1(a); INSERT INTO foo1(a) VALUES(1); CREATE TABLE foo2(b); @@ -144,16 +137,13 @@ SELECT * FROM foo1; SELECT * FROM foo2; INSERT INTO foo1(a) VALUES(1); CREATE TABLE foo2(b); INSERT INTO foo2(b) VALUES(1); -SELECT * FROM foo1; +SELECT * FROM foo1; SELECT * FROM foo2; 1 -SELECT * FROM foo2; 1 -INSERT INTO foo1(a) VALUES(2); -INSERT INTO foo2(b) VALUES(2); -SELECT * FROM foo1; +INSERT INTO foo1(a) VALUES(2); INSERT INTO foo2(b) VALUES(2); +SELECT * FROM foo1; SELECT * FROM foo2; 1 2 -SELECT * FROM foo2; 1 2 }} @@ -163,7 +153,7 @@ SELECT * FROM foo2; # NB. whitespace is important do_test shell2-1.4.6 { forcedelete foo.db - catchcmd "foo.db" {.echo ON + catchcmdex "foo.db" {.echo ON .headers ON CREATE TABLE foo1(a); INSERT INTO foo1(a) VALUES(1); @@ -178,22 +168,205 @@ CREATE TABLE foo1(a); INSERT INTO foo1(a) VALUES(1); CREATE TABLE foo2(b); INSERT INTO foo2(b) VALUES(1); -SELECT * FROM foo1; +SELECT * FROM foo1; SELECT * FROM foo2; a 1 -SELECT * FROM foo2; b 1 -INSERT INTO foo1(a) VALUES(2); -INSERT INTO foo2(b) VALUES(2); -SELECT * FROM foo1; +INSERT INTO foo1(a) VALUES(2); INSERT INTO foo2(b) VALUES(2); +SELECT * FROM foo1; SELECT * FROM foo2; a 1 2 -SELECT * FROM foo2; b 1 2 }} +# Test for rejection of incomplete input at EOF. +# Reported at https://sqlite.org/forum/forumpost/718f489a43be3197 +do_test shell2-1.4.7 { + catchcmd ":memory:" { + SELECT 'unclosed;} +} {1 {Parse error near line 2: unrecognized token: "'unclosed;" + SELECT 'unclosed; + ^--- error here}} + +# Verify that safe mode rejects certain UDFs +# Reported at https://sqlite.org/forum/forumpost/07beac8056151b2f +do_test shell2-1.4.8 { + catchcmd "-safe :memory:" { + SELECT edit('DoNotCare');} +} {1 {line 2: cannot use the edit() function in safe mode}} +do_test shell2-1.4.9 { + catchcmd "-safe :memory:" { + SELECT writefile('DoNotCare', x'');} +} {1 {line 2: cannot use the writefile() function in safe mode}} + +# Verify that .clone handles sequence table. +# See https://sqlite.org/forum/forumpost/71ff9e6c4c +do_test shell2-1.4.9 { + forcedelete clone.db + set res [catchcmd :memory: [string trim { + CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT); + INSERT INTO t VALUES (1),(2); +.clone clone.db +.open clone.db + SELECT max(seq) FROM sqlite_sequence;}]] +} {0 {t... done +done +2}} + +ifcapable vtab { +# Verify that generate_series stays sane near 64-bit range boundaries. +# See overflow report at https://sqlite.org/forum/forumpost/5d34ce5280 +do_test shell2-1.4.10 { + set res [catchcmd :memory: [string trim { + SELECT * FROM generate_series(9223372036854775807,9223372036854775807,1); + SELECT * FROM generate_series(9223372036854775807,9223372036854775807,-1); + SELECT avg(value),min(value),max(value) FROM generate_series( + -9223372036854775808,9223372036854775807,1085102592571150095); + SELECT * FROM generate_series(-9223372036854775808,9223372036854775807, + 9223372036854775807); + SELECT value FROM generate_series(-4611686018427387904, + 4611686018427387904, 4611686018427387904) ORDER BY value DESC; + SELECT * FROM generate_series(0,-2,-1); + SELECT * FROM generate_series(0,-2); + SELECT * FROM generate_series(0,2) LIMIT 3;}]] +} {0 {9223372036854775807 +9223372036854775807 +-0.5|-9223372036854775808|9223372036854775807 +-9223372036854775808 +-1 +9223372036854775806 +4611686018427387904 +0 +-4611686018427387904 +0 +-1 +-2 +0 +1 +2}} +} ;# ifcapable vtab + +ifcapable vtab { +# Bug discovered while messing around, .import hangs with +# bit 7 set in column separator. +do_test shell2-1.4.11 { + forcedelete dummy.csv + set df [open dummy.csv w] + puts $df dog,cat + close $df + set res [catchcmd :memory: [string trim { + CREATE TABLE t(line text); +.mode ascii +.separator "\377" "\n" +.import dummy.csv t + SELECT count(*) FROM t;}]] +} {0 1} +} ;# ifcapable vtab + +# Bug from forum post 7cbe081746dd3803 +# Keywords as column names were producing an error message. +do_test shell2-1.4.12 { + set res [catchcmd :memory: [string trim { + CREATE TABLE "group"("order" text); + INSERT INTO "group" VALUES ('ABC'); +.sha3sum}]] +} {0 ca08bc02b7e95c7df431a3a4b1cc0f8d8743914793473f55b5558e03} + +#------------------------------------------------------------------------- + +foreach {tn hexdump expect} { + 0 { +| size 8192 pagesize 4096 filename my.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 03 00 00 00 02 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 03 ................ +| 96: 00 2e 8d f8 0d 00 00 00 01 0f df 00 0f df 00 00 ................ +| 4048: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1f ................ +| 4064: 01 06 17 0f 0f 01 2f 74 61 62 6c 65 74 74 02 43 ....../tablett.C +| 4080: 52 45 41 54 45 20 54 41 42 4c 45 20 74 28 78 29 REATE TABLE t(x) +| page 2 offset 4096 +| 0: 0d 00 00 00 02 0f ee 00 0f f7 0f ee 00 00 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 07 02 ................ +| 4080: 02 17 77 6f 72 6c 64 07 01 02 17 68 65 6c 6c 6f ..world....hello +| end my.db + } + {0 {}} + + 1 { +| size 2147483647 pagesize 4096 filename my.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 03 00 00 00 02 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 03 ................ +| 96: 00 2e 8d f8 0d 00 00 00 01 0f df 00 0f df 00 00 ................ +| 4048: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1f ................ +| 4064: 01 06 17 0f 0f 01 2f 74 61 62 6c 65 74 74 02 43 ....../tablett.C +| 4080: 52 45 41 54 45 20 54 41 42 4c 45 20 74 28 78 29 REATE TABLE t(x) +| page 2 offset 4096 +| 0: 0d 00 00 00 02 0f ee 00 0f f7 0f ee 00 00 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 07 02 ................ +| 4080: 02 17 77 6f 72 6c 64 07 01 02 17 68 65 6c 6c 6f ..world....hello +| end my.db + } + {1 {Error: out of memory}} + + 2 { +| size 8192 pagesize 4096 filename my.db +| page 1 offset 2147483647 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 03 00 00 00 02 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 03 ................ +| 96: 00 2e 8d f8 0d 00 00 00 01 0f df 00 0f df 00 00 ................ +| 4048: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1f ................ +| 4064: 01 06 17 0f 0f 01 2f 74 61 62 6c 65 74 74 02 43 ....../tablett.C +| 4080: 52 45 41 54 45 20 54 41 42 4c 45 20 74 28 78 29 REATE TABLE t(x) +| page 2 offset 4096 +| 0: 0d 00 00 00 02 0f ee 00 0f f7 0f ee 00 00 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 07 02 ................ +| 4080: 02 17 77 6f 72 6c 64 07 01 02 17 68 65 6c 6c 6f ..world....hello +| end my.db + } + {0 {}} + + 3 { +| size 8192 pagesize 4096 filename my.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 03 00 00 00 02 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 03 ................ +| 96: 00 2e 8d f8 0d 00 00 00 01 0f df 00 0f df 00 00 ................ +| 4048: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1f ................ +| 4064: 01 06 17 0f 0f 01 2f 74 61 62 6c 65 74 74 02 43 ....../tablett.C +| 4080: 52 45 41 54 45 20 54 41 42 4c 45 20 74 28 78 29 REATE TABLE t(x) +| page 2 offset 4096 +| 2147483647: 0d 00 00 00 02 0f ee 00 0f f7 0f ee 00 00 00 00 ................ +| 4064: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 07 02 ................ +| 4080: 02 17 77 6f 72 6c 64 07 01 02 17 68 65 6c 6c 6f ..world....hello +| end my.db +} + {0 {}} + +} { + set fd [open dump.txt w] + puts $fd [string trim $hexdump] + close $fd + do_test shell2-2.$tn.1 { + set rc [ catchcmd "" ".open --hexdb dump.txt"] + } $expect +} + + finish_test diff --git a/test/shell3.test b/test/shell3.test index ce1fd4ecb5..6bb49f5c3d 100644 --- a/test/shell3.test +++ b/test/shell3.test @@ -8,6 +8,7 @@ # May you share freely, never taking more than you give. # #*********************************************************************** +# TESTRUNNER: shell # # The focus of this file is testing the CLI shell tool. # @@ -18,22 +19,28 @@ # # shell3-1.*: Basic tests for running SQL statments from command line. # shell3-2.*: Basic tests for running SQL file from command line. +# shell3-3.*: Basic tests for processing odd SQL constructs. # set testdir [file dirname $argv0] source $testdir/tester.tcl -if {$tcl_platform(platform)=="windows"} { - set CLI "sqlite3.exe" -} else { - set CLI "./sqlite3" -} -if {![file executable $CLI]} { - finish_test - return -} +set CLI [test_cli_invocation] db close forcedelete test.db test.db-journal test.db-wal sqlite3 db test.db + +# There are inconsistencies in command-line argument quoting on Windows. +# In particular, individual applications are responsible for command-line +# parsing in Windows, not the shell. Depending on whether the sqlite3.exe +# program is compiled with MinGW or MSVC, the command-line parsing is +# different. This causes problems for the tests below. To avoid +# issues, these tests are disabled for windows. +# +if {$::tcl_platform(platform) eq "windows"} { + finish_test + return +} + #---------------------------------------------------------------------------- # shell3-1.*: Basic tests for running SQL statments from command line. # @@ -62,7 +69,7 @@ do_test shell3-1.6 { } {0 {}} do_test shell3-1.7 { catchcmd "foo.db \"CREATE TABLE\"" -} {1 {Error: near "TABLE": syntax error}} +} {1 {Error: in prepare, incomplete input}} #---------------------------------------------------------------------------- # shell3-2.*: Basic tests for running SQL file from command line. @@ -92,6 +99,50 @@ do_test shell3-2.6 { } {0 {}} do_test shell3-2.7 { catchcmd "foo.db" "CREATE TABLE" -} {1 {Error: incomplete SQL: CREATE TABLE}} +} {1 {Parse error near line 1: incomplete input}} + + +#---------------------------------------------------------------------------- +# shell3-3.*: Basic tests for processing odd SQL constructs. +# + +# Run combinations of odd identifiers, comments, semicolon placement +do_test shell3-3.1 { + forcedelete foo.db + set rc [ catchcmd "foo.db" {CREATE TABLE t1(" +a--. +" --x +); CREATE TABLE t2("a[""b""]"); +.header on +INSERT INTO t1 VALUES (' +x''y'); +INSERT INTO t2 VALUES (' +/*. +.*/ x +''y'); +SELECT * from t1 limit 1; +SELECT * from t2 limit 1; +} ] + set fexist [file exist foo.db] + list $rc $fexist +} {{0 { +a--. + + +x'y +a["b"] + +/*. +.*/ x +'y}} 1} + +do_test shell3-3.2 { + catchcmd "" { +.open xyz.db +SELECT ; + } +} {1 {Parse error near line 3: near ";": syntax error + SELECT ; + ^--- error here}} finish_test diff --git a/test/shell4.test b/test/shell4.test index fcb0b2b715..3ced0702e4 100644 --- a/test/shell4.test +++ b/test/shell4.test @@ -8,6 +8,7 @@ # May you share freely, never taking more than you give. # #*********************************************************************** +# TESTRUNNER: shell # # The focus of this file is testing the CLI shell tool. # These tests are specific to the .stats command. @@ -18,18 +19,13 @@ # # shell4-1.*: Basic tests specific to the "stats" command. # shell4-2.*: Basic tests for ".trace" +# shell4-3.*: The ".read" command takes the shell out of interactive mode +# shell4-4.*: Input redirects cannot recurse too much # set testdir [file dirname $argv0] source $testdir/tester.tcl -if {$tcl_platform(platform)=="windows"} { - set CLI "sqlite3.exe" -} else { - set CLI "./sqlite3" -} -if {![file executable $CLI]} { - finish_test - return -} +set CLI [test_cli_invocation] +set CLI_ONLY [test_find_cli] db close forcedelete test.db test.db-journal test.db-wal sqlite3 db test.db @@ -61,9 +57,9 @@ do_test shell4-1.2.2 { } {0} # .stats ON|OFF Turn stats on or off -do_test shell4-1.3.1 { - catchcmd "test.db" ".stats" -} {1 {Usage: .stats on|off}} +#do_test shell4-1.3.1 { +# catchcmd "test.db" ".stats" +#} {1 {Usage: .stats on|off}} do_test shell4-1.3.2 { catchcmd "test.db" ".stats ON" } {0 {}} @@ -73,7 +69,7 @@ do_test shell4-1.3.3 { do_test shell4-1.3.4 { # too many arguments catchcmd "test.db" ".stats OFF BAD" -} {1 {Usage: .stats on|off}} +} {1 {Usage: .stats ?on|off|stmt|vmstep?}} # NB. whitespace is important do_test shell4-1.4.1 { @@ -113,16 +109,16 @@ SELECT 1; [regexp {Autoindex Inserts} $res] } {1 1 1} +ifcapable trace { do_test shell4-2.1 { - catchcmd ":memory:" "CREATE TABLE t1(x);\n.trace" -} {1 {Usage: .trace FILE|off}} + catchcmd ":memory:" "CREATE TABLE t1(x);\n.trace --unknown" +} {1 {Unknown option "--unknown" on ".trace"}} do_test shell4-2.2 { catchcmd ":memory:" "CREATE TABLE t1(x);\n.trace off\n.trace off\n" } {0 {}} do_test shell4-2.3 { - catchcmd ":memory:" ".trace stdout\n.trace\n.trace off\n.dump\n" -} {/^1 {PRAGMA.*Usage:.*}$/} -ifcapable trace { + catchcmd ":memory:" ".trace stdout\n.dump\n.trace off\n" +} {/^0 {SELECT.*}$/} do_test shell4-2.4 { catchcmd ":memory:" ".trace stdout\nCREATE TABLE t1(x);SELECT * FROM t1;" } {0 {CREATE TABLE t1(x); @@ -130,7 +126,32 @@ SELECT * FROM t1;}} do_test shell4-2.5 { catchcmd ":memory:" "CREATE TABLE t1(x);\n.trace stdout\nSELECT * FROM t1;" } {0 {SELECT * FROM t1;}} +do_test shell4-2.6 { + catchcmd ":memory:" { +CREATE TABLE t1(x); +.trace --stmt stdout +SELECT * FROM t1;} +} {0 {SELECT * FROM t1;}} } +do_test shell4-3.1 { + set fd [open t1.txt wb] + puts $fd "SELECT 'squirrel';" + close $fd + exec $::CLI_ONLY :memory: --interactive ".read t1.txt" +} {squirrel} +do_test_with_ansi_output shell4-3.2 { + set fd [open t1.txt wb] + puts $fd "SELECT 'pound: \302\243';" + close $fd + exec $::CLI_ONLY :memory: --interactive ".read t1.txt" +} {pound: £} + +do_test shell4-4.1 { + set fd [open t1.txt wb] + puts $fd ".read t1.txt" + close $fd + catchcmd ":memory:" ".read t1.txt" +} {1 {t1.txt: Input nesting limit (25) reached at line 1. Check recursion.}} finish_test diff --git a/test/shell5.test b/test/shell5.test index b921accca5..70a2298bcb 100644 --- a/test/shell5.test +++ b/test/shell5.test @@ -8,6 +8,7 @@ # May you share freely, never taking more than you give. # #*********************************************************************** +# TESTRUNNER: shell # # The focus of this file is testing the CLI shell tool. # These tests are specific to the .import command. @@ -21,15 +22,7 @@ # set testdir [file dirname $argv0] source $testdir/tester.tcl -if {$tcl_platform(platform)=="windows"} { - set CLI "sqlite3.exe" -} else { - set CLI "./sqlite3" -} -if {![file executable $CLI]} { - finish_test - return -} +set CLI [test_cli_invocation] db close forcedelete test.db test.db-journal test.db-wal @@ -40,17 +33,14 @@ forcedelete test.db test.db-journal test.db-wal # .import FILE TABLE Import data from FILE into TABLE do_test shell5-1.1.1 { catchcmd "test.db" ".import" -} {1 {Usage: .import FILE TABLE}} +} {/1 .ERROR: missing FILE argument.*/} do_test shell5-1.1.2 { catchcmd "test.db" ".import FOO" -} {1 {Usage: .import FILE TABLE}} -#do_test shell5-1.1.2 { -# catchcmd "test.db" ".import FOO BAR" -#} {1 {Error: no such table: BAR}} +} {/1 .ERROR: missing TABLE argument.*/} do_test shell5-1.1.3 { # too many arguments catchcmd "test.db" ".import FOO BAR BAD" -} {1 {Usage: .import FILE TABLE}} +} {/1 .ERROR: extra argument.*/} # .separator STRING Change separator used by output mode and .import do_test shell5-1.2.1 { @@ -94,13 +84,22 @@ do_test shell5-1.4.1 { .import FOO t1}] } {1 {Error: cannot open "FOO"}} +# the remainder of these test cases require virtual tables. +# +ifcapable !vtab { + puts "Skipping subsequent tests due to SQLITE_OMIT_VIRTUALTABLE" + finish_test + return +} + # empty import file do_test shell5-1.4.2 { forcedelete shell5.csv set in [open shell5.csv w] close $in - set res [catchcmd "test.db" {.import shell5.csv t1 -SELECT COUNT(*) FROM t1;}] + set res [catchcmd ":memory:" {ATTACH 'test.db' AS test; +.import -schema test shell5.csv t1 +SELECT COUNT(*) FROM test.t1;}] } {0 0} # import file with 1 row, 1 column (expecting 2 cols) @@ -108,7 +107,8 @@ do_test shell5-1.4.3 { set in [open shell5.csv w] puts $in "1" close $in - set res [catchcmd "test.db" {.import shell5.csv t1}] + set res [catchcmd ":memory:" {ATTACH 'test.db' AS test; +.import -schema test shell5.csv t1}] } {1 {shell5.csv:1: expected 2 columns but found 1 - filling the rest with NULL}} # import file with 1 row, 3 columns (expecting 2 cols) @@ -116,7 +116,8 @@ do_test shell5-1.4.4 { set in [open shell5.csv w] puts $in "1|2|3" close $in - set res [catchcmd "test.db" {.import shell5.csv t1}] + set res [catchcmd ":memory:" {ATTACH 'test.db' AS test; +.import --schema test shell5.csv t1}] } {1 {shell5.csv:1: expected 2 columns but found 3 - extras ignored}} # import file with 1 row, 2 columns @@ -137,8 +138,9 @@ do_test shell5-1.4.6 { puts $in "2|3" puts $in "3|4" close $in - set res [catchcmd "test.db" {.import shell5.csv t1 -SELECT COUNT(*) FROM t1;}] + set res [catchcmd ":memory:" {ATTACH 'test.db' AS test; +.import -schema test shell5.csv t1 +SELECT COUNT(*) FROM test.t1;}] } {0 3} # import file with 1 row, 2 columns, using a comma @@ -146,9 +148,10 @@ do_test shell5-1.4.7 { set in [open shell5.csv w] puts $in "4,5" close $in - set res [catchcmd "test.db" {.separator , -.import shell5.csv t1 -SELECT COUNT(*) FROM t1;}] + set res [catchcmd ":memory:" {ATTACH 'test.db' AS test; +.separator , +.import --schema test shell5.csv t1 +SELECT COUNT(*) FROM test.t1;}] } {0 4} # import file with 1 row, 2 columns, text data @@ -192,6 +195,37 @@ do_test shell5-1.4.10.2 { catchcmd "test.db" {SELECT b FROM t1 WHERE a='7';} } {0 {Now is the time for all good men to come to the aid of their country.}} +# import file with 2 rows, 2 columns and an initial BOM +# +do_test shell5-1.4.11 { + set in [open shell5.csv wb] + puts -nonewline $in "\xef\xbb\xbf" + puts $in "2|3" + puts $in "4|5" + close $in + set res [catchcmd "test.db" {CREATE TABLE t2(x INT, y INT); +.import shell5.csv t2 +.mode quote +.header on +SELECT * FROM t2;}] + string map {\n | \n\r |} $res +} {0 {'x','y'|2,3|4,5}} + +# import file with 2 rows, 2 columns or text with an initial BOM +# +do_test shell5-1.4.12 { + set in [open shell5.csv wb] + puts $in "\xef\xbb\xbf\"two\"|3" + puts $in "4|5" + close $in + set res [catchcmd "test.db" {DELETE FROM t2; +.import shell5.csv t2 +.mode quote +.header on +SELECT * FROM t2;}] + string map {\n | \n\r |} $res +} {0 {'x','y'|'two',3|4,5}} + # check importing very long field do_test shell5-1.5.1 { set str [string repeat X 999] @@ -218,7 +252,8 @@ do_test shell5-1.6.1 { set in [open shell5.csv w] puts $in $data close $in - set res [catchcmd "test.db" {.import shell5.csv t2 + set res [catchcmd "test.db" {DROP TABLE IF EXISTS t2; +.import shell5.csv t2 SELECT COUNT(*) FROM t2;}] } {0 1} @@ -236,7 +271,7 @@ do_test shell5-1.7.1 { SELECT COUNT(*) FROM t3;}] } [list 0 $rows] -# Inport from a pipe. (Unix only, as it requires "awk") +# Import from a pipe. (Unix only, as it requires "awk") if {$tcl_platform(platform)=="unix"} { do_test shell5-1.8 { forcedelete test.db @@ -383,7 +418,7 @@ CREATE TABLE t4(a, b); # do_test shell5-3.1 { set fd [open shell5.csv w] - fconfigure $fd -encoding binary -translation binary + fconfigure $fd -translation binary puts -nonewline $fd "\"test 1\"\x1F,test 2\r\n\x1E" puts -nonewline $fd "test 3\x1Ftest 4\n" close $fd @@ -435,4 +470,139 @@ CREATE TABLE t7(a, b, c); db eval { SELECT * FROM t7 ORDER BY a } } {1 2 3 4 5 {} 6 7 8} +do_test shell5-4.3 { + forcedelete shell5.csv + set fd [open shell5.csv w] + puts $fd ",," + puts $fd "1,2,3" + close $fd + catchcmd test.db [string trim { +.mode csv +CREATE TABLE t8(a, b, c); +.import -skip 1 shell5.csv t8 +.nullvalue # + }] + db eval { SELECT * FROM t8 } +} {1 2 3} + +do_test shell5-4.4 { + forcedelete shell5.csv + set fd [open shell5.csv w] + puts $fd "1,2,3" + close $fd + catchcmd test.db [string trim { +.mode csv +CREATE TEMP TABLE t8(a, b, c); +.import shell5.csv t8 +.nullvalue # +SELECT * FROM temp.t8 + }] +} {0 1,2,3} + +#---------------------------------------------------------------------------- +# Tests for the shell automatic column rename. +# +db close + +# Import columns containing duplicates +do_test shell5-5.1 { + set out [open shell5.csv w] + fconfigure $out -translation lf + puts $out {"","x","x","y","z","z_0","z_5","z"} + puts $out {0,"x2","x3","y4","z5","z6","z7","z8"} + close $out + forcedelete test.db + catchcmd test.db {.import -csv shell5.csv t1 +.mode line +SELECT * FROM t1;} +} {1 { ? = 0 + x_02 = x2 + x_03 = x3 + y = y4 + z_05 = z5 + z_0 = z6 + z_5 = z7 + z_08 = z8 +Columns renamed during .import shell5.csv due to duplicates: +"x" to "x_02", +"x" to "x_03", +"z" to "z_05", +"z" to "z_08"}} + +do_test shell5-5.1 { + set out [open shell5.csv w] + fconfigure $out -translation lf + puts $out {"COW","cow","CoW","cOw"} + puts $out {"uuu","lll","ulu","lul"} + close $out + forcedelete test.db + catchcmd test.db {.import -csv shell5.csv t1 +.mode line +SELECT * FROM t1;} +} {1 {COW_1 = uuu +cow_2 = lll +CoW_3 = ulu +cOw_4 = lul +Columns renamed during .import shell5.csv due to duplicates: +"COW" to "COW_1", +"cow" to "cow_2", +"CoW" to "CoW_3", +"cOw" to "cOw_4"}} + +#---------------------------------------------------------------------------- +# Tests for preserving utf-8 that is not also ASCII. +# + +do_test_with_ansi_output shell5-6.1 { + set out [open shell5.csv w] + fconfigure $out -translation lf + puts $out {あい,うえお} + puts $out {1,2} + close $out + forcedelete test.db + catchcmd test.db {.import -csv shell5.csv t1 +.mode line +SELECT * FROM t1;} +} {0 { あい = 1 +うえお = 2}} + +do_test_with_ansi_output shell5-6.2 { + set out [open shell5.csv w] + fconfigure $out -translation lf + puts $out {1,2} + puts $out {あい,うえお} + close $out + forcedelete test.db + catchcmd test.db {.import -csv shell5.csv t1 +.mode line +SELECT * FROM t1;} +} {0 { 1 = あい + 2 = うえお}} + +# 2024-03-11 https://sqlite.org/forum/forumpost/ca014d7358 +# Import into a table that contains computed columns. +# +do_test shell5-7.1 { + set out [open shell5.csv w] + fconfigure $out -translation lf + puts $out {aaa|bbb} + close $out + forcedelete test.db + catchcmd :memory: {CREATE TABLE t1(a TEXT, b TEXT, c AS (a||b)); +.import shell5.csv t1 +SELECT * FROM t1;} +} {0 aaa|bbb|aaabbb} + +#------------------------------------------------------------------------- + +do_test shell5-8.1 { + + set out [open shell5.csv w] + fconfigure $out -translation lf + puts $out x + close $out + + catchcmd :memory: {.import --csv shell5.csv '""""""""""""""""""""""""""""""""""""""""""""""'} +} {0 {}} + finish_test diff --git a/test/shell6.test b/test/shell6.test new file mode 100644 index 0000000000..4841d6c01a --- /dev/null +++ b/test/shell6.test @@ -0,0 +1,130 @@ +# 2016 December 15 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# TESTRUNNER: shell +# +# Test the shell tool ".lint fkey-indexes" command. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +ifcapable !vtab {finish_test; return} +set testprefix shell6 +set CLI [test_find_cli] +db close +forcedelete test.db test.db-journal test.db-wal + +foreach {tn schema output} { + 1 { + CREATE TABLE p1(a PRIMARY KEY, b); + CREATE TABLE c1(x, y REFERENCES p1); + } { + CREATE INDEX 'c1_y' ON 'c1'('y'); --> p1(a) + } + + 2 { + CREATE TABLE p1(a PRIMARY KEY, b); + CREATE TABLE c2(x REFERENCES p1, y REFERENCES p1); + } { + CREATE INDEX 'c2_y' ON 'c2'('y'); --> p1(a) + CREATE INDEX 'c2_x' ON 'c2'('x'); --> p1(a) + } + + 3 { + CREATE TABLE 'p 1'(a, b, c, PRIMARY KEY(c, b)); + CREATE TABLE 'c 1'(x, y, z, FOREIGN KEY (z, y) REFERENCES 'p 1'); + } { + CREATE INDEX 'c 1_z_y' ON 'c 1'('z', 'y'); --> p 1(c,b) + } + + 4 { + CREATE TABLE p1(a, 'b b b' PRIMARY KEY); + CREATE TABLE c1('x y z' REFERENCES p1); + CREATE INDEX i1 ON c1('x y z') WHERE "x y z" IS NOT NULL; + } { + } + + 5 { + CREATE TABLE p1(a, 'b b b' PRIMARY KEY); + CREATE TABLE c1('x y z' REFERENCES p1); + CREATE INDEX i1 ON c1('x y z') WHERE "x y z" IS NOT 12; + } { + CREATE INDEX 'c1_x y z' ON 'c1'('x y z'); --> p1(b b b) + } + + 6 { + CREATE TABLE x1(a, b, c, UNIQUE(a, b)); + CREATE TABLE y1(a, b, c, FOREIGN KEY(b, a) REFERENCES x1(a, b)); + CREATE INDEX y1i ON y1(a, c, b); + } { + CREATE INDEX 'y1_b_a' ON 'y1'('b', 'a'); --> x1(a,b) + } + + 6 { + CREATE TABLE x1(a COLLATE nocase, b, UNIQUE(a)); + CREATE TABLE y1(a COLLATE rtrim REFERENCES x1(a)); + } { + CREATE INDEX 'y1_a' ON 'y1'('a' COLLATE nocase); --> x1(a) + } + + 7 { + CREATE TABLE x1(a PRIMARY KEY COLLATE nocase, b); + CREATE TABLE y1(a REFERENCES x1); + } { + CREATE INDEX 'y1_a' ON 'y1'('a' COLLATE nocase); --> x1(a) + } + + 8 { + CREATE TABLE x1(a, b COLLATE nocase, c COLLATE rtrim, PRIMARY KEY(c, b, a)); + CREATE TABLE y1(d, e, f, FOREIGN KEY(d, e, f) REFERENCES x1); + } { + CREATE INDEX 'y1_d_e_f' ON 'y1'('d' COLLATE rtrim, 'e' COLLATE nocase, 'f'); --> x1(c,b,a) + } + + 9 { + CREATE TABLE p1(a, b UNIQUE); + CREATE TABLE c1(x INTEGER PRIMARY KEY REFERENCES p1(b)); + } { + } + + 10 { + CREATE TABLE parent (id INTEGER PRIMARY KEY); + CREATE TABLE child2 (id INT PRIMARY KEY, parentID INT REFERENCES parent) + WITHOUT ROWID; + } { + CREATE INDEX 'child2_parentID' ON 'child2'('parentID'); --> parent(id) + } + +} { + forcedelete test.db + sqlite3 db test.db + execsql $schema + + set expected "" + foreach line [split $output "\n"] { + set line [string trim $line] + if {$line!=""} { + append expected "$line\n" + } + } + + do_test 1.$tn.1 { + set RES [catchcmd test.db [list .lint fkey-indexes]] + } [list 0 [string trim $expected]] + + do_test 1.$tn.2 { + execsql [lindex $RES 1] + catchcmd test.db [list .lint fkey-indexes] + } {0 {}} + + db close +} + +finish_test diff --git a/test/shell7.test b/test/shell7.test new file mode 100644 index 0000000000..460789e544 --- /dev/null +++ b/test/shell7.test @@ -0,0 +1,52 @@ +# 2016 December 17 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# TESTRUNNER: shell +# +# Test the readfile() function built into the shell tool. Specifically, +# that it does not truncate the blob read at the first embedded 0x00 +# byte. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix shell7 +set CLI [test_find_cli] + + +do_execsql_test 1.0 { + CREATE TABLE f1(tn INTEGER PRIMARY KEY, x BLOB); + CREATE TABLE f2(tn INTEGER PRIMARY KEY, x BLOB); + + INSERT INTO f1 VALUES(1, X'01020304'); + INSERT INTO f1 VALUES(2, X'01000304'); + INSERT INTO f1 VALUES(3, randomblob(200)); +} + +foreach {tn l x} [db eval { SELECT tn, length(x) AS l, x FROM f1 }] { + forcedelete shell7_test.bin + set fd [open shell7_test.bin w] + fconfigure $fd -translation binary + puts -nonewline $fd $x + close $fd + + do_test 1.$tn.1 { file size shell7_test.bin } $l + do_test 1.$tn.2 { + catchcmd test.db "INSERT INTO f2 VALUES($tn, readfile('shell7_test.bin'));" + } {0 {}} + + do_execsql_test 1.$tn.3 { + SELECT (SELECT x FROM f1 WHERE tn=1)==(SELECT x FROM f2 WHERE tn=1) + } {1} +} + + + +finish_test diff --git a/test/shell8.test b/test/shell8.test new file mode 100644 index 0000000000..e555396365 --- /dev/null +++ b/test/shell8.test @@ -0,0 +1,222 @@ +# 2017 December 9 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# TESTRUNNER: shell +# +# Test the shell tool ".ar" command. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix shell8 + +ifcapable !vtab { + finish_test; return +} +set CLI [test_cli_invocation] + +# Check to make sure the shell has been compiled with ".archive" support. +# +if {[string match {*unknown command*} [catchcmd :memory: .archive]]} { + finish_test; return +} + +proc populate_dir {dirname spec} { + # First delete the current tree, if one exists. + file delete -force $dirname + + # Recreate the root of the new tree. + file mkdir $dirname + + # Add each file to the new tree. + foreach {f d} $spec { + set path [file join $dirname $f] + file mkdir [file dirname $path] + set fd [open $path w] + puts -nonewline $fd $d + close $fd + } +} + +proc dir_content {dirname} { + lsort [glob -nocomplain $dirname/*] +} + +proc dir_to_list {dirname {n -1}} { + if {$n<0} {set n [llength [file split $dirname]]} + + set res [list] + foreach f [glob -nocomplain $dirname/*] { + set mtime [file mtime $f] + if {$::tcl_platform(platform) ne "windows"} { + set perm [file attributes $f -perm] + } else { + set perm 0 + } + set relpath [file join {*}[lrange [file split $f] $n end]] + lappend res + if {[file isdirectory $f]} { + lappend res [list $relpath / $mtime $perm] + lappend res {*}[dir_to_list $f] + } else { + set fd [open $f] + set data [read $fd] + close $fd + lappend res [list $relpath $data $mtime $perm] + } + } + lsort $res +} + +proc dir_compare {d1 d2} { + set l1 [dir_to_list $d1] + set l2 [dir_to_list $d1] + string compare $l1 $l2 +} + +foreach {tn tcl} { + 1 { + set c1 ".ar c ar1" + set x1 ".ar x" + + set c2 ".ar cC ar1 ." + set x2 ".ar Cx ar3" + + set c3 ".ar cCf ar1 test_xyz.db ." + set x3 ".ar Cfx ar3 test_xyz.db" + } + + 2 { + set c1 ".ar -c ar1" + set x1 ".ar -x" + + set c2 ".ar -cC ar1 ." + set x2 ".ar -xC ar3" + + set c3 ".ar -cCar1 -ftest_xyz.db ." + set x3 ".ar -x -C ar3 -f test_xyz.db" + } + + 3 { + set c1 ".ar --create ar1" + set x1 ".ar --extract" + + set c2 ".ar --directory ar1 --create ." + set x2 ".ar --extract --dir ar3" + + set c3 ".ar --creat --dir ar1 --file test_xyz.db ." + set x3 ".ar --e --dir ar3 --f test_xyz.db" + } + + 4 { + set c1 ".ar --cr ar1" + set x1 ".ar --e" + + set c2 ".ar -C ar1 -c ." + set x2 ".ar -x -C ar3" + + set c3 ".ar -c --directory ar1 --file test_xyz.db ." + set x3 ".ar -x --directory ar3 --file test_xyz.db" + } +} { + eval $tcl + + # Populate directory "ar1" with some files. + # + populate_dir ar1 { + file1 "abcd" + file2 "efgh" + dir1/file3 "ijkl" + } + set expected [dir_to_list ar1] + + do_test 1.$tn.1 { + catchcmd test_ar.db $c1 + file delete -force ar1 + catchcmd test_ar.db $x1 + dir_to_list ar1 + } $expected + + do_test 1.$tn.2 { + file delete -force ar3 + catchcmd test_ar.db $c2 + catchcmd test_ar.db $x2 + dir_to_list ar3 + } $expected + + do_test 1.$tn.3 { + file delete -force ar3 + file delete -force test_xyz.db + catchcmd ":memory:" $c3 + catchcmd ":memory:" $x3 + dir_to_list ar3 + } $expected + + # This is a repeat of test 1.$tn.1, except that there is a 2 second + # pause between creating the archive and extracting its contents. + # This is to test that timestamps are set correctly. + # + # Because it is slow, only do this for $tn==1. + if {$tn==1} { + do_test 1.$tn.4 { + catchcmd test_ar.db $c1 + file delete -force ar1 + after 2000 + catchcmd test_ar.db $x1 + dir_to_list ar1 + } $expected + } +} + +do_test 2.1.1 { + populate_dir ar2 { + file1 "abcd" + file2 "efgh" + junk1 "j1" + junk2 "j2" + dir1/file3 "ijkl" + } + populate_dir ar4 { + file2 "efgh" + } + catchcmd shell8.db {.ar -c} + catchcmd shell8.db {.ar -C ar2 -i .} + catchcmd shell8.db {.ar -r ./file2 ./dir1} + catchcmd shell8.db {.ar -g -r ./ju*2} + catchcmd shell8.db {.ar -C ar4 -x .} + regsub -all {ar4} [dir_content ar4] ar2 +} {ar2/file1 ar2/file2 ar2/junk1} + +# Test symbolic links. +# +if {$tcl_platform(platform)=="unix"} { + populate_dir ar2 { + file1 "1234" + file2 "3456" + } + file link ar2/link1 file1 + + forcedelete shell8.db + forcedelete link1 + + do_test 3.1 { + catchcmd shell8.db {.ar -C ar2 -c file2 link1 } + } {0 {}} + + do_test 3.2 { + catchcmd shell8.db {.ar -x} + } {0 {}} + + do_test 3.3 { + catchcmd shell8.db {.ar -x} + } {0 {}} +} + +finish_test diff --git a/test/shell9.test b/test/shell9.test new file mode 100644 index 0000000000..869cb075da --- /dev/null +++ b/test/shell9.test @@ -0,0 +1,149 @@ +# 2024 Jan 8 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# TESTRUNNER: shell +# +# The focus of this file is testing the CLI shell tool. Specifically, +# testing that it is possible to run a ".dump" script that creates +# virtual tables without explicitly disabling defensive mode. +# +# And, that it can process a ".dump" script that contains strings +# delimited using double-quotes in the schema (DQS_DDL setting). +# + +# Test plan: +# +# shell1-1.*: Basic command line option handling. +# shell1-2.*: Basic "dot" command token parsing. +# shell1-3.*: Basic test that "dot" command can be called. +# shell1-{4-8}.*: Test various "dot" commands's functionality. +# shell1-9.*: Basic test that "dot" commands and SQL intermix ok. +# +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set CLI [test_cli_invocation] + +set ::testprefix shell9 + +ifcapable !fts5 { + finish_test + return +} + +#---------------------------------------------------------------------------- +# Test cases shell9-1.* verify that scripts output by .dump may be parsed +# by the shell tool without explicitly disabling DEFENSIVE mode, unless +# the shell is in safe mode. +# +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE t1 USING fts5(a, b, c); + INSERT INTO t1 VALUES('one', 'two', 'three'); +} +db close + +# Create .dump file in "testdump.txt". +# +set out [open testdump.txt w] +puts $out [lindex [catchcmd test.db .dump] 1] +close $out + +# Check testdump.txt can be processed if the initial db is empty. +# +do_test 1.1.1 { + forcedelete test.db + catchcmd test.db ".read testdump.txt" +} {0 {}} +sqlite3 db test.db +do_execsql_test 1.1.2 { + SELECT * FROM t1; +} {one two three} + +# Check testdump.txt cannot be processed if the initial db is not empty. +# +reset_db +do_execsql_test 1.2.1 { + CREATE TABLE t4(hello); +} +db close +do_test 1.2.2 { + catchcmd test.db ".read testdump.txt" +} {1 {Parse error near line 5: table sqlite_master may not be modified}} + +# Check testdump.txt cannot be processed if the db is in safe mode +# +do_test 1.3.1 { + forcedelete test.db + catchsafecmd test.db ".read testdump.txt" +} {1 {line 1: cannot run .read in safe mode}} +do_test 1.3.2 { + set fd [open testdump.txt] + set script [read $fd] + close $fd + forcedelete test.db + catchsafecmd test.db $script +} {1 {Parse error near line 5: table sqlite_master may not be modified}} +do_test 1.3.3 { + # Quick check that the above would have worked but for safe mode. + forcedelete test.db + catchcmd test.db $script +} {0 {}} + +#---------------------------------------------------------------------------- +# Test cases shell9-2.* verify that a warning is printed at the top of +# .dump scripts that contain virtual tables. +# +proc contains_warning {text} { + return [string match "*WARNING: Script requires that*" $text] +} + +reset_db +do_execsql_test 2.0.1 { + CREATE TABLE t1(x); + CREATE TABLE t2(y); + INSERT INTO t1 VALUES('one'); + INSERT INTO t2 VALUES('two'); +} +do_test 2.0.2 { + contains_warning [catchcmd test.db .dump] +} 0 + +do_execsql_test 2.1.1 { + CREATE virtual TABLE r1 USING fts5(x); +} +do_test 2.1.2 { + contains_warning [catchcmd test.db .dump] +} 1 + +do_test 2.2.1 { + contains_warning [catchcmd test.db ".dump t1"] +} 0 +do_test 2.2.2 { + contains_warning [catchcmd test.db ".dump r1"] +} 1 + +#------------------------------------------------------------------------- +reset_db +sqlite3_db_config db DQS_DDL 1 +do_execsql_test 3.1.0 { + CREATE TABLE t4(hello, check( hello IS NOT "xyz") ); +} +db close + +# Create .dump file in "testdump.txt". +# +set out [open testdump.txt w] +puts $out [lindex [catchcmd test.db .dump] 1] +close $out +do_test 3.1.1 { + forcedelete test.db + catchcmd test.db ".read testdump.txt" +} {0 {}} + +finish_test diff --git a/test/shellA.test b/test/shellA.test new file mode 100644 index 0000000000..f3959d4283 --- /dev/null +++ b/test/shellA.test @@ -0,0 +1,257 @@ +# 2025-02-24 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# TESTRUNNER: shell +# +# Test cases for the command-line shell - focusing on .mode and +# especially control-character escaping and the --escape option. +# +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set CLI [test_cli_invocation] + +do_execsql_test shellA-1.0 { + CREATE TABLE t1(a INT, x TEXT); + INSERT INTO t1 VALUES + (1, 'line with '' single quote'), + (2, concat(char(0x1b),'[31mVT-100 codes',char(0x1b),'[0m')), + (3, NULL), + (4, 1234), + (5, 568.25), + (6, unistr('new\u000aline')), + (7, unistr('carriage\u000dreturn')), + (8, 'last line'); +} {} + +# Initial verification that the database created correctly +# and that our calls to the CLI are working. +# +do_test_with_ansi_output shellA-1.2 { + exec {*}$CLI test.db {.mode box --escape symbol} {SELECT * FROM t1;} +} { +┌───┬──────────────────────────┐ +│ a │ x │ +├───┼──────────────────────────┤ +│ 1 │ line with ' single quote │ +├───┼──────────────────────────┤ +│ 2 │ ␛[31mVT-100 codes␛[0m │ +├───┼──────────────────────────┤ +│ 3 │ │ +├───┼──────────────────────────┤ +│ 4 │ 1234 │ +├───┼──────────────────────────┤ +│ 5 │ 568.25 │ +├───┼──────────────────────────┤ +│ 6 │ new │ +│ │ line │ +├───┼──────────────────────────┤ +│ 7 │ carriage␍return │ +├───┼──────────────────────────┤ +│ 8 │ last line │ +└───┴──────────────────────────┘ +} + +# ".mode list" +# +do_test shellA-1.3 { + exec {*}$CLI test.db {SELECT x FROM t1 WHERE a=2;} +} { +^[[31mVT-100 codes^[[0m +} +do_test_with_ansi_output shellA-1.4 { + exec {*}$CLI test.db --escape symbol {SELECT x FROM t1 WHERE a=2;} +} { +␛[31mVT-100 codes␛[0m +} +do_test shellA-1.5 { + exec {*}$CLI test.db --escape ascii {SELECT x FROM t1 WHERE a=2;} +} { +^[[31mVT-100 codes^[[0m +} +do_test_with_ansi_output shellA-1.6 { + exec {*}$CLI test.db {.mode list --escape symbol} {SELECT x FROM t1 WHERE a=2;} +} { +␛[31mVT-100 codes␛[0m +} +do_test shellA-1.7 { + exec {*}$CLI test.db {.mode list --escape ascii} {SELECT x FROM t1 WHERE a=2;} +} { +^[[31mVT-100 codes^[[0m +} +do_test shellA-1.8 { + file delete -force out.txt + exec {*}$CLI test.db {.mode list --escape off} {SELECT x FROM t1 WHERE a=7;} \ + >out.txt + set fd [open out.txt rb] + set res [read $fd] + close $fd + string trim $res +} "carriage\rreturn" +do_test shellA-1.9 { + set rc [catch { + exec {*}$CLI test.db {.mode test --escape xyz} + } msg] + lappend rc $msg +} {1 {unknown control character escape mode "xyz" - choices: ascii symbol off}} +do_test shellA-1.10 { + set rc [catch { + exec {*}$CLI --escape abc test.db .q + } msg] + lappend rc $msg +} {1 {unknown control character escape mode "abc" - choices: ascii symbol off}} + +# ".mode quote" +# +do_test shellA-2.1 { + exec {*}$CLI test.db --quote {SELECT a, x FROM t1 WHERE a IN (1,2,6,7,8)} +} { +1,'line with '' single quote' +2,unistr('\u001b[31mVT-100 codes\u001b[0m') +6,'new +line' +7,unistr('carriage\u000dreturn') +8,'last line' +} +do_test shellA-2.2 { + exec {*}$CLI test.db --quote {.mode} +} {current output mode: quote --escape ascii} +do_test shellA-2.3 { + exec {*}$CLI test.db --quote --escape SYMBOL {.mode} +} {current output mode: quote --escape symbol} +do_test shellA-2.4 { + exec {*}$CLI test.db --quote --escape OFF {.mode} +} {current output mode: quote --escape off} + + +# ".mode line" +# +do_test_with_ansi_output shellA-3.1 { + exec {*}$CLI test.db --line --escape symbol \ + {SELECT a, x FROM t1 WHERE a IN (1,2,6,7,8)} +} { + a = 1 + x = line with ' single quote + + a = 2 + x = ␛[31mVT-100 codes␛[0m + + a = 6 + x = new +line + + a = 7 + x = carriage␍return + + a = 8 + x = last line +} +do_test shellA-3.2 { + exec {*}$CLI test.db --line --escape ascii \ + {SELECT a, x FROM t1 WHERE a IN (1,2,6,7,8)} +} { + a = 1 + x = line with ' single quote + + a = 2 + x = ^[[31mVT-100 codes^[[0m + + a = 6 + x = new +line + + a = 7 + x = carriage^Mreturn + + a = 8 + x = last line +} + +# ".mode box" +# +do_test_with_ansi_output shellA-4.1 { + exec {*}$CLI test.db --box --escape ascii \ + {SELECT a, x FROM t1 WHERE a IN (1,2,6,7,8)} +} { +┌───┬──────────────────────────┐ +│ a │ x │ +├───┼──────────────────────────┤ +│ 1 │ line with ' single quote │ +├───┼──────────────────────────┤ +│ 2 │ ^[[31mVT-100 codes^[[0m │ +├───┼──────────────────────────┤ +│ 6 │ new │ +│ │ line │ +├───┼──────────────────────────┤ +│ 7 │ carriage^Mreturn │ +├───┼──────────────────────────┤ +│ 8 │ last line │ +└───┴──────────────────────────┘ +} +do_test_with_ansi_output shellA-4.2 { + exec {*}$CLI test.db {.mode qbox} {SELECT a, x FROM t1 WHERE a IN (1,2,6,7,8)} +} { +┌───┬───────────────────────────────────────────┐ +│ a │ x │ +├───┼───────────────────────────────────────────┤ +│ 1 │ 'line with '' single quote' │ +├───┼───────────────────────────────────────────┤ +│ 2 │ unistr('\u001b[31mVT-100 codes\u001b[0m') │ +├───┼───────────────────────────────────────────┤ +│ 6 │ 'new │ +│ │ line' │ +├───┼───────────────────────────────────────────┤ +│ 7 │ unistr('carriage\u000dreturn') │ +├───┼───────────────────────────────────────────┤ +│ 8 │ 'last line' │ +└───┴───────────────────────────────────────────┘ +} + +# ".mode insert" +# +do_test shellA-5.1 { + exec {*}$CLI test.db {.mode insert t1 --escape ascii} \ + {SELECT a, x FROM t1 WHERE a IN (1,2,6,7,8)} +} { +INSERT INTO t1 VALUES(1,'line with '' single quote'); +INSERT INTO t1 VALUES(2,unistr('\u001b[31mVT-100 codes\u001b[0m')); +INSERT INTO t1 VALUES(6,unistr('new\u000aline')); +INSERT INTO t1 VALUES(7,unistr('carriage\u000dreturn')); +INSERT INTO t1 VALUES(8,'last line'); +} +do_test shellA-5.2 { + exec {*}$CLI test.db {.mode insert t1 --escape symbol} \ + {SELECT a, x FROM t1 WHERE a IN (1,2,6,7,8)} +} { +INSERT INTO t1 VALUES(1,'line with '' single quote'); +INSERT INTO t1 VALUES(2,unistr('\u001b[31mVT-100 codes\u001b[0m')); +INSERT INTO t1 VALUES(6,unistr('new\u000aline')); +INSERT INTO t1 VALUES(7,unistr('carriage\u000dreturn')); +INSERT INTO t1 VALUES(8,'last line'); +} +do_test shellA-5.3 { + file delete -force out.txt + exec {*}$CLI test.db {.mode insert t1 --escape off} \ + {SELECT a, x FROM t1 WHERE a IN (1,2,6,7,8)} >out.txt + set fd [open out.txt rb] + set res [read $fd] + close $fd + string trim [string map [list \r\n \n] $res] +} " +INSERT INTO t1 VALUES(1,'line with '' single quote'); +INSERT INTO t1 VALUES(2,'\033\13331mVT-100 codes\033\1330m'); +INSERT INTO t1 VALUES(6,'new +line'); +INSERT INTO t1 VALUES(7,'carriage\rreturn'); +INSERT INTO t1 VALUES(8,'last line'); +" + +finish_test diff --git a/test/shmlock.test b/test/shmlock.test new file mode 100644 index 0000000000..fce0cf8f5e --- /dev/null +++ b/test/shmlock.test @@ -0,0 +1,180 @@ +# 2018 December 6 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +set testprefix shmlock + +ifcapable !wal {finish_test ; return } + +sqlite3 db2 test.db +sqlite3 db3 test.db + +do_execsql_test 1.0 { + PRAGMA journal_mode = wal; + CREATE TABLE t1(a, b); + INSERT INTO t1 VALUES(1, 2); +} {wal} +do_test 1.1 { execsql { SELECT * FROM t1 } db2 } {1 2} +do_test 1.2 { execsql { SELECT * FROM t1 } db3 } {1 2} + +foreach {tn dbhandle cmd res} { + 1 db {shared lock 7 1} OK + 2 db2 {exclusive lock 7 1} BUSY + 3 db {shared unlock 7 1} OK + 4 db2 {exclusive lock 7 1} OK + 5 db {shared lock 7 1} BUSY + 6 db {exclusive lock 7 1} BUSY + 7 db2 {exclusive unlock 7 1} OK + + 8 db {exclusive lock 0 8} OK + 9 db {exclusive unlock 0 8} OK + 10 db2 {exclusive lock 0 8} OK + 11 db2 {exclusive unlock 0 8} OK + + 12 db {shared lock 0 1} OK + 13 db2 {shared lock 0 1} OK + 14 db3 {shared lock 0 1} OK + 15 db3 {shared unlock 0 1} OK + 16 db3 {exclusive lock 0 1} BUSY + 17 db2 {shared unlock 0 1} OK + 18 db3 {exclusive lock 0 1} BUSY + 19 db {shared unlock 0 1} OK + 20 db3 {exclusive lock 0 1} OK + 21 db3 {exclusive unlock 0 1} OK + + 22 db {shared lock 3 1} OK + 23 db2 {exclusive lock 2 2} BUSY + 24 db {shared lock 2 1} OK + 25 db2 {exclusive lock 0 5} BUSY + 26 db2 {exclusive lock 0 4} BUSY + 27 db2 {exclusive lock 0 3} BUSY + 28 db {shared unlock 3 1} OK + 29 db2 {exclusive lock 2 2} BUSY + 28 db {shared unlock 2 1} OK + 29 db2 {exclusive lock 2 2} OK + 29 db2 {exclusive unlock 2 2} OK +} { + do_test 1.3.$tn [list vfs_shmlock $dbhandle main {*}$cmd] "SQLITE_$res" +} + +db close +db2 close +db3 close + +if {[permutation]=="unix-excl"} { + do_test 2.0 { + for {set i 0} {$i < 256} {incr i} { + sqlite3 db$i test.db + execsql { SELECT * FROM t1 } db$i + } + for {set i 0} {$i < 255} {incr i} { + set rc [vfs_shmlock db$i main shared lock 4 1] + if {$rc != "SQLITE_OK"} { error $rc } + } + + vfs_shmlock db255 main shared lock 4 1 + } {SQLITE_BUSY} + + do_test 2.1 { vfs_shmlock db255 main exclusive lock 4 1 } SQLITE_BUSY + do_test 2.2 { vfs_shmlock db0 main shared unlock 4 1 } SQLITE_OK + do_test 2.3 { vfs_shmlock db255 main shared lock 4 1 } SQLITE_OK + do_test 2.4 { vfs_shmlock db255 main shared unlock 4 1 } SQLITE_OK + do_test 2.5 { vfs_shmlock db255 main exclusive lock 4 1 } SQLITE_BUSY + + do_test 2.6 { + for {set i 1} {$i < 255} {incr i} { + set rc [vfs_shmlock db255 main exclusive lock 4 1] + if {$rc != "SQLITE_BUSY"} { error $rc } + set rc [vfs_shmlock db$i main shared unlock 4 1] + if {$rc != "SQLITE_OK"} { error $rc } + } + + vfs_shmlock db255 main exclusive lock 4 1 + } {SQLITE_OK} + + vfs_shmlock db255 main exclusive unlock 4 1 + + for {set i 0} {$i < 256} {incr i} { + db$i close + } +} + +sqlite3 db0 test.db +sqlite3 db1 test.db +do_test 3.1 { execsql { SELECT * FROM t1 } db0 } {1 2} +do_test 3.2 { execsql { SELECT * FROM t1 } db1 } {1 2} +if {$tcl_platform(os) eq "Windows NT"} { + set isWindows 1 +} else { + set isWindows 0 +} + +set L(0) {n n n n n n n n} +set L(1) {n n n n n n n n} +proc random_lock_test {idx} { + global L + set iSlot [expr int(rand()*8)] + if {[expr int(rand()*2)]} { + # Unlock operation + if {[lindex $L($idx) $iSlot]!="n"} { + vfs_shmlock db$idx main [lindex $L($idx) $iSlot] unlock $iSlot 1 + lset L($idx) $iSlot n + } + } else { + # Lock operation + if {[lindex $L($idx) $iSlot]=="n"} { + set locktype [lindex {e s} [expr int(rand()*2)]] + set n 1 + if {$locktype=="e"} { + for {set l $iSlot} {$l<8 && [lindex $L($idx) $l]=="n"} {incr l} {} + set n [expr int(rand()*($l-$iSlot))+1] + # The LockFile() and UnlockFile() apis on windows require that + # every unlock correspond exactly to a prior lock. Hence, we cannot + # lock arbitrary ranges in this test on windows. + if {$::isWindows} {set n 1} + # puts "iSlot=$iSlot l=$l L=$L($idx)" + # puts "$iSlot $n" + } + set res [vfs_shmlock db$idx main $locktype lock $iSlot $n] + + set bBusy 0 + for {set i $iSlot} {$i<($iSlot+$n)} {incr i} { + set other [lindex $L([expr ($idx+1)%2]) $i] + if {($other!="n" && $locktype=="e")||($other=="e" && $locktype=="s")} { + if {$res != "SQLITE_BUSY"} { error "BUSY not detected" } + set bBusy 1 + break + } + } + + if {$bBusy==0} { + if {$res != "SQLITE_OK"} { error "BUSY false-positive" } + for {set i $iSlot} {$i<($iSlot+$n)} {incr i} { + lset L($idx) $i $locktype + } + } + } + } +} + +set nStep 100000 +for {set i 0} {$i < $nStep} {incr i} { + random_lock_test 0 + random_lock_test 1 +} + +db0 close +db1 close + +finish_test diff --git a/test/shrink.test b/test/shrink.test index 7c9bed08b0..cee6919e8c 100644 --- a/test/shrink.test +++ b/test/shrink.test @@ -17,6 +17,13 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl test_set_config_pagecache 0 0 +if {[sqlite3_memory_used]==0} { + # SQLITE_DEFAULT_MEMSTATUS=0 build. + finish_test + return +} + + unset -nocomplain baseline do_test shrink-1.1 { db eval { @@ -24,7 +31,7 @@ do_test shrink-1.1 { CREATE TABLE t1(x,y); INSERT INTO t1 VALUES(randomblob(1000000),1); } - set ::baseline sqlite3_memory_used + set ::baseline [sqlite3_memory_used] # EVIDENCE-OF: R-58814-63508 The sqlite3_db_release_memory(D) interface # attempts to free as much heap memory as possible from database # connection D. diff --git a/test/skipscan1.test b/test/skipscan1.test index ac26711603..379de1956c 100644 --- a/test/skipscan1.test +++ b/test/skipscan1.test @@ -43,23 +43,23 @@ do_execsql_test skipscan1-1.2 { } {abc 345 7 8 | def 345 9 10 |} do_execsql_test skipscan1-1.2eqp { EXPLAIN QUERY PLAN - SELECT a,b,c,d,'|' FROM t1 WHERE b=345 ORDER BY a; + SELECT a,b,c,d,'|' FROM t1 WHERE d<>99 AND b=345 ORDER BY a; } {/* USING INDEX t1abc (ANY(a) AND b=?)*/} do_execsql_test skipscan1-1.2sort { EXPLAIN QUERY PLAN - SELECT a,b,c,d,'|' FROM t1 WHERE b=345 ORDER BY a; + SELECT a,b,c,d,'|' FROM t1 WHERE d<>99 AND b=345 ORDER BY a; } {~/*ORDER BY*/} do_execsql_test skipscan1-1.3 { - SELECT a,b,c,d,'|' FROM t1 WHERE b=345 ORDER BY a DESC; + SELECT a,b,c,d,'|' FROM t1 WHERE d<>99 AND b=345 ORDER BY a DESC; } {def 345 9 10 | abc 345 7 8 |} do_execsql_test skipscan1-1.3eqp { EXPLAIN QUERY PLAN - SELECT a,b,c,d,'|' FROM t1 WHERE b=345 ORDER BY a; + SELECT a,b,c,d,'|' FROM t1 WHERE d<>99 AND b=345 ORDER BY a DESC; } {/* USING INDEX t1abc (ANY(a) AND b=?)*/} do_execsql_test skipscan1-1.3sort { EXPLAIN QUERY PLAN - SELECT a,b,c,d,'|' FROM t1 WHERE b=345 ORDER BY a; + SELECT a,b,c,d,'|' FROM t1 WHERE d<>99 AND b=345 ORDER BY a DESC; } {~/*ORDER BY*/} do_execsql_test skipscan1-1.4 { @@ -150,15 +150,15 @@ do_execsql_test skipscan1-2.1 { } {} do_execsql_test skipscan1-2.2 { - SELECT a,b,c,d,'|' FROM t2 WHERE b=345 ORDER BY a; + SELECT a,b,c,d,'|' FROM t2 WHERE d<>99 AND b=345 ORDER BY a; } {abc 345 7 8 | def 345 9 10 |} do_execsql_test skipscan1-2.2eqp { EXPLAIN QUERY PLAN - SELECT a,b,c,d,'|' FROM t2 WHERE b=345 ORDER BY a; + SELECT a,b,c,d,'|' FROM t2 WHERE d<>99 AND b=345 ORDER BY a; } {/* USING INDEX sqlite_autoindex_t2_1 (ANY(a) AND b=?)*/} do_execsql_test skipscan1-2.2sort { EXPLAIN QUERY PLAN - SELECT a,b,c,d,'|' FROM t2 WHERE b=345 ORDER BY a; + SELECT a,b,c,d,'|' FROM t2 WHERE d<>99 AND b=345 ORDER BY a; } {~/*ORDER BY*/} @@ -234,7 +234,6 @@ do_execsql_test skipscan1-5.2 { ANALYZE; DELETE FROM sqlite_stat1; DROP TABLE IF EXISTS sqlite_stat4; - DROP TABLE IF EXISTS sqlite_stat3; INSERT INTO sqlite_stat1 VALUES('t5','t5i1','2702931 3 2 2 2 2'); INSERT INTO sqlite_stat1 VALUES('t5','t5i2','2702931 686 2 2 2'); ANALYZE sqlite_master; @@ -322,4 +321,112 @@ do_execsql_test skipscan1-8.2 { ORDER BY +x; } {1 AB 2 CD} +# Segfault reported on the mailing list by Keith Medcalf on 2016-09-18. +# A skip-scan with a "column IN (SELECT ...)" on the second term of the +# index. +# +do_execsql_test skipscan1-9.2 { + CREATE TABLE t9a(a,b,c); + CREATE INDEX t9a_ab ON t9a(a,b); + CREATE TABLE t9b(x,y); + ANALYZE sqlite_master; + INSERT INTO sqlite_stat1 VALUES('t9a','t9a_ab','1000000 250000 1'); + ANALYZE sqlite_master; + EXPLAIN QUERY PLAN + SELECT * FROM t9a WHERE b IN (SELECT x FROM t9b WHERE y!=5); +} {/USING INDEX t9a_ab .ANY.a. AND b=./} + + +optimization_control db skip-scan off +do_execsql_test skipscan1-9.3 { + EXPLAIN QUERY PLAN + SELECT * FROM t9a WHERE b IN (SELECT x FROM t9b WHERE y!=5); +} {/{SCAN t9a}/} +optimization_control db all on + +do_execsql_test skipscan1-2.1 { + CREATE TABLE t6(a TEXT, b INT, c INT, d INT); + CREATE INDEX t6abc ON t6(a,b,c); + INSERT INTO t6 VALUES('abc',123,4,5); + + ANALYZE; + DELETE FROM sqlite_stat1; + INSERT INTO sqlite_stat1 VALUES('t6','t6abc','10000 5000 2000 10'); + ANALYZE sqlite_master; + DELETE FROM t6; +} {} + +do_execsql_test skipscan1-2.2eqp { + EXPLAIN QUERY PLAN + SELECT a,b,c,d,'|' FROM t6 WHERE d<>99 AND b=345 ORDER BY a; +} {/* USING INDEX t6abc (ANY(a) AND b=?)*/} +do_execsql_test skipscan1-2.2 { + SELECT a,b,c,d,'|' FROM t6 WHERE d<>99 AND b=345 ORDER BY a; +} {} + +do_execsql_test skipscan1-2.3eqp { + EXPLAIN QUERY PLAN + SELECT a,b,c,d,'|' FROM t6 WHERE d<>99 AND b=345 ORDER BY a DESC; +} {/* USING INDEX t6abc (ANY(a) AND b=?)*/} +do_execsql_test skipscan1-2.3 { + SELECT a,b,c,d,'|' FROM t6 WHERE d<>99 AND b=345 ORDER BY a DESC; +} {} + +# 2019-07-29 Ticket ced41c7c7d6b4d36 +# A skipscan query is not order-distinct +# +db close +sqlite3 db :memory: +do_execsql_test skipscan1-3.1 { + CREATE TABLE t1 (c1, c2, c3, c4, PRIMARY KEY(c4, c3)); + INSERT INTO t1 VALUES(3,0,1,NULL); + INSERT INTO t1 VALUES(0,4,1,NULL); + INSERT INTO t1 VALUES(5,6,1,NULL); + INSERT INTO t1 VALUES(0,4,1,NULL); + ANALYZE sqlite_master; + INSERT INTO sqlite_stat1 VALUES('t1','sqlite_autoindex_t1_1','18 18 6'); + ANALYZE sqlite_master; + SELECT DISTINCT quote(c1), quote(c2), quote(c3), quote(c4), '|' + FROM t1 WHERE t1.c3 = 1; +} {3 0 1 NULL | 0 4 1 NULL | 5 6 1 NULL |} +do_eqp_test skipscan1-3.2 { + SELECT DISTINCT quote(c1), quote(c2), quote(c3), quote(c4), '|' + FROM t1 WHERE t1.c3 = 1; +} { + QUERY PLAN + |--SEARCH t1 USING INDEX sqlite_autoindex_t1_1 (ANY(c4) AND c3=?) + `--USE TEMP B-TREE FOR DISTINCT +} + +# 2020-01-06 ticket 304017f5f04a0035 +# +reset_db +do_execsql_test skipscan1-4.10 { + CREATE TABLE t1(a,b INT); + INSERT INTO t1(a,b) VALUES(1,2),(3,3),(4,5); + CREATE UNIQUE INDEX i1 ON t1(b,b,a,a,a,a,a,b,a); + ANALYZE; + DROP TABLE IF EXISTS sqlite_stat4; + INSERT INTO sqlite_stat1 VALUES('t1','i1','30 30 30 2 2 2 2 2 2 2'); + ANALYZE sqlite_master; + + SELECT DISTINCT a + FROM t1 + WHERE a = b + AND a = 3 + AND b IN (1,3,2,4) + AND b >= 0 + AND a <= 10; +} {3} + +# 2023-03-24 https://sqlite.org/forum/forumpost/8cc1dc0fe9 +# +reset_db +do_execsql_test skipscan1-5.0 { + CREATE TABLE t1(a TEXT, UNIQUE(a,a,a)); + INSERT INTO t1 VALUES (hex(zeroblob(241))),(1),(2),(3); + ANALYZE; + SELECT max(a) FROM t1 WHERE a IN t1; +} {3} + finish_test diff --git a/test/skipscan2.test b/test/skipscan2.test index a42ff2d057..aa870d4565 100644 --- a/test/skipscan2.test +++ b/test/skipscan2.test @@ -157,7 +157,6 @@ do_execsql_test skipscan2-2.1 { CREATE INDEX peoplew_idx1 ON peoplew(role, height); INSERT INTO peoplew(name,role,height) SELECT name, role, height FROM people; - ALTER TABLE people RENAME TO old_people; SELECT name FROM peoplew WHERE height>=180 ORDER BY +name; } {David Jack Patrick Quiana Xavier} do_execsql_test skipscan2-2.2 { @@ -199,7 +198,8 @@ do_test skipscan2-3.2 { } {} do_eqp_test skipscan2-3.3eqp { SELECT * FROM t3 WHERE b=42; -} {0 0 0 {SEARCH TABLE t3 USING PRIMARY KEY (ANY(a) AND b=?)}} +} {SEARCH t3 USING PRIMARY KEY (ANY(a) AND b=?)} + finish_test diff --git a/test/skipscan5.test b/test/skipscan5.test index 7c3b166a8c..026356e6c0 100644 --- a/test/skipscan5.test +++ b/test/skipscan5.test @@ -30,7 +30,7 @@ do_execsql_test 1.1 { expr srand(4) do_test 1.2 { - for {set i 0} {$i < 100} {incr i} { + for {set i 0} {$i < 1000} {incr i} { set a [expr int(rand()*4.0) + 1] set b [expr int(rand()*20.0) + 1] execsql { INSERT INTO t1 VALUES($a, $b, NULL) } @@ -41,20 +41,20 @@ do_test 1.2 { foreach {tn q res} { 1 "b = 5" {/*ANY(a) AND b=?*/} 2 "b > 12 AND b < 16" {/*ANY(a) AND b>? AND b<?*/} - 3 "b > 2 AND b < 16" {/*SCAN TABLE t1*/} + 3 "b > 2 AND b < 16" {/*SCAN t1*/} 4 "b > 18 AND b < 25" {/*ANY(a) AND b>? AND b<?*/} - 5 "b > 15" {/*ANY(a) AND b>?*/} - 6 "b > 5" {/*SCAN TABLE t1*/} - 7 "b < 15" {/*SCAN TABLE t1*/} + 5 "b > 16" {/*ANY(a) AND b>?*/} + 6 "b > 5" {/*SCAN t1*/} + 7 "b < 15" {/*SCAN t1*/} 8 "b < 5" {/*ANY(a) AND b<?*/} 9 "5 > b" {/*ANY(a) AND b<?*/} 10 "b = '5'" {/*ANY(a) AND b=?*/} 11 "b > '12' AND b < '16'" {/*ANY(a) AND b>? AND b<?*/} - 12 "b > '2' AND b < '16'" {/*SCAN TABLE t1*/} + 12 "b > '2' AND b < '16'" {/*SCAN t1*/} 13 "b > '18' AND b < '25'" {/*ANY(a) AND b>? AND b<?*/} - 14 "b > '15'" {/*ANY(a) AND b>?*/} - 15 "b > '5'" {/*SCAN TABLE t1*/} - 16 "b < '15'" {/*SCAN TABLE t1*/} + 14 "b > '16'" {/*ANY(a) AND b>?*/} + 15 "b > '5'" {/*SCAN t1*/} + 16 "b < '15'" {/*SCAN t1*/} 17 "b < '5'" {/*ANY(a) AND b<?*/} 18 "'5' > b" {/*ANY(a) AND b<?*/} } { @@ -104,11 +104,11 @@ foreach {tn dbenc coll} { foreach {tn2 q res} { 1 { c BETWEEN 'd' AND 'e' } {/*ANY(a) AND ANY(b) AND c>? AND c<?*/} - 2 { c BETWEEN 'b' AND 'r' } {/*SCAN TABLE t2*/} + 2 { c BETWEEN 'b' AND 'r' } {/*SCAN t2*/} 3 { c > 'q' } {/*ANY(a) AND ANY(b) AND c>?*/} - 4 { c > 'e' } {/*SCAN TABLE t2*/} - 5 { c < 'q' } {/*SCAN TABLE t2*/} - 6 { c < 'c' } {/*ANY(a) AND ANY(b) AND c<?*/} + 4 { c > 'e' } {/*SCAN t2*/} + 5 { c < 'q' } {/*SCAN t2*/} + 6 { c < 'b' } {/*ANY(a) AND ANY(b) AND c<?*/} } { set sql "EXPLAIN QUERY PLAN SELECT * FROM t2 WHERE $q" do_execsql_test 2.$tn.$tn2 $sql $res @@ -169,11 +169,11 @@ do_test 3.2 { foreach {tn q res} { 1 "b BETWEEN -10000 AND -8000" {/*ANY(a) AND b>? AND b<?*/} - 2 "b BETWEEN -10000 AND 'qqq'" {/*SCAN TABLE t3*/} - 3 "b < X'5555'" {/*SCAN TABLE t3*/} + 2 "b BETWEEN -10000 AND 'qqq'" {/*SCAN t3*/} + 3 "b < X'5555'" {/*SCAN t3*/} 4 "b > X'5555'" {/*ANY(a) AND b>?*/} 5 "b > 'zzz'" {/*ANY(a) AND b>?*/} - 6 "b < 'zzz'" {/*SCAN TABLE t3*/} + 6 "b < 'zzz'" {/*SCAN t3*/} } { set sql "EXPLAIN QUERY PLAN SELECT * FROM t3 WHERE $q" do_execsql_test 3.3.$tn $sql $res diff --git a/test/skipscan6.test b/test/skipscan6.test index 026c4d7b00..a97f440eef 100644 --- a/test/skipscan6.test +++ b/test/skipscan6.test @@ -167,10 +167,10 @@ do_execsql_test 3.0 { INSERT INTO t2 SELECT * FROM t3; ANALYZE; - SELECT * FROM sqlite_stat1; + SELECT * FROM sqlite_stat1 ORDER BY +idx; } { - t2 t2_ba {100 20 1 1} t2 t2_a {100 1} + t2 t2_ba {100 20 1 1} t3 t3_a {100 1} t3 t3_ba {100 20 1 1} } @@ -179,22 +179,13 @@ do_execsql_test 3.0 { # do_eqp_test 3.1 { SELECT * FROM t3 WHERE a = ? AND c = ? -} { - 0 0 0 {SEARCH TABLE t3 USING INDEX t3_a (a=?)} -} +} {SEARCH t3 USING INDEX t3_a (a=?)} # The same query on table t2. This should use index "t2_a", for the # same reason. At one point though, it was mistakenly using a skip-scan. # do_eqp_test 3.2 { SELECT * FROM t2 WHERE a = ? AND c = ? -} { - 0 0 0 {SEARCH TABLE t2 USING INDEX t2_a (a=?)} -} - -finish_test - - - +} {SEARCH t2 USING INDEX t2_a (a=?)} finish_test diff --git a/test/snapshot.test b/test/snapshot.test index 63a0e30175..75a8af214a 100644 --- a/test/snapshot.test +++ b/test/snapshot.test @@ -17,323 +17,481 @@ source $testdir/tester.tcl ifcapable !snapshot {finish_test; return} set testprefix snapshot -#------------------------------------------------------------------------- -# Check some error conditions in snapshot_get(). It is an error if: -# -# 1) snapshot_get() is called on a non-WAL database, or -# 2) there is an open write transaction on the database. -# -do_execsql_test 1.0 { - CREATE TABLE t1(a, b); - INSERT INTO t1 VALUES(1, 2); - INSERT INTO t1 VALUES(3, 4); +# This test does not work with the inmemory_journal permutation. The reason +# is that each connection opened as part of this permutation executes +# "PRAGMA journal_mode=memory", which fails if the database is in wal mode +# and there are one or more existing connections. +if {[permutation]=="inmemory_journal"} { + finish_test + return } -do_test 1.1.1 { - execsql { BEGIN; SELECT * FROM t1; } - list [catch { sqlite3_snapshot_get db main } msg] $msg -} {1 SQLITE_ERROR} -do_execsql_test 1.1.2 COMMIT - -do_test 1.2.1 { - execsql { - PRAGMA journal_mode = WAL; - BEGIN; - INSERT INTO t1 VALUES(5, 6); - INSERT INTO t1 VALUES(7, 8); +foreach {tn tcl} { + 1 { + proc snapshot_get {DB DBNAME} { + uplevel [list sqlite3_snapshot_get $DB $DBNAME] + } + proc snapshot_open {DB DBNAME SNAPSHOT} { + uplevel [list sqlite3_snapshot_open $DB $DBNAME $SNAPSHOT] + } + proc snapshot_free {SNAPSHOT} { + uplevel [list sqlite3_snapshot_free $SNAPSHOT] + } + proc snapshot_cmp {SNAPSHOT1 SNAPSHOT2} { + uplevel [list sqlite3_snapshot_cmp $SNAPSHOT1 $SNAPSHOT2] + } } - list [catch { sqlite3_snapshot_get db main } msg] $msg -} {1 SQLITE_ERROR} -do_execsql_test 1.3.2 COMMIT -#------------------------------------------------------------------------- -# Check that a simple case works. Reuse the database created by the -# block of tests above. -# -do_execsql_test 2.1.0 { - BEGIN; - SELECT * FROM t1; -} {1 2 3 4 5 6 7 8} - -do_test 2.1.1 { - set snapshot [sqlite3_snapshot_get db main] - execsql { - COMMIT; - INSERT INTO t1 VALUES(9, 10); - SELECT * FROM t1; + 2 { + proc snapshot_get {DB DBNAME} { + uplevel [list sqlite3_snapshot_get_blob $DB $DBNAME] + } + proc snapshot_open {DB DBNAME SNAPSHOT} { + uplevel [list sqlite3_snapshot_open_blob $DB $DBNAME $SNAPSHOT] + } + proc snapshot_free {SNAPSHOT} { + } + proc snapshot_cmp {SNAPSHOT1 SNAPSHOT2} { + uplevel [list sqlite3_snapshot_cmp_blob $SNAPSHOT1 $SNAPSHOT2] + } } -} {1 2 3 4 5 6 7 8 9 10} +} { -do_test 2.1.2 { - execsql BEGIN - sqlite3_snapshot_open db main $snapshot - execsql { - SELECT * FROM t1; + reset_db + eval $tcl + + #------------------------------------------------------------------------- + # Check some error conditions in snapshot_get(). It is an error if: + # + # 1) snapshot_get() is called on a non-WAL database, or + # 2) there is an open write transaction on the database. + # 3) the database handle is in auto-commit mode + # + do_execsql_test $tn.1.0 { + CREATE TABLE t1(a, b); + INSERT INTO t1 VALUES(1, 2); + INSERT INTO t1 VALUES(3, 4); } -} {1 2 3 4 5 6 7 8} -do_test 2.1.3 { - sqlite3_snapshot_free $snapshot - execsql COMMIT -} {} + do_test $tn.1.1.1 { + execsql { BEGIN; SELECT * FROM t1; } + list [catch { snapshot_get db main } msg] $msg + } {1 SQLITE_ERROR} + do_execsql_test $tn.1.1.2 COMMIT + + do_test $tn.1.2.1 { + execsql { + PRAGMA journal_mode = WAL; + BEGIN; + INSERT INTO t1 VALUES(5, 6); + INSERT INTO t1 VALUES(7, 8); + } + list [catch { snapshot_get db main } msg] $msg + } {1 SQLITE_ERROR} + do_execsql_test $tn.1.2.2 COMMIT + + do_test $tn.1.3.1 { + list [catch { snapshot_get db main } msg] $msg + } {1 SQLITE_ERROR} + do_test $tn.1.3.2 { + db trans { set snap [snapshot_get db main] } + snapshot_free $snap + } {} -do_test 2.2.0 { - sqlite3 db2 test.db - execsql { + #------------------------------------------------------------------------- + # Check that a simple case works. Reuse the database created by the + # block of tests above. + # + do_execsql_test $tn.2.1.0 { BEGIN; SELECT * FROM t1; - } db2 -} {1 2 3 4 5 6 7 8 9 10} - -do_test 2.2.1 { - set snapshot [sqlite3_snapshot_get db2 main] - execsql { - INSERT INTO t1 VALUES(11, 12); - SELECT * FROM t1; - } -} {1 2 3 4 5 6 7 8 9 10 11 12} + } {1 2 3 4 5 6 7 8} -do_test 2.2.2 { - execsql BEGIN - sqlite3_snapshot_open db main $snapshot - execsql { - SELECT * FROM t1; - } -} {1 2 3 4 5 6 7 8 9 10} + do_test $tn.2.1.1 { + set snapshot [snapshot_get db main] + execsql { + COMMIT; + INSERT INTO t1 VALUES(9, 10); + SELECT * FROM t1; + } + } {1 2 3 4 5 6 7 8 9 10} -do_test 2.2.3 { - sqlite3_snapshot_free $snapshot - execsql COMMIT - execsql COMMIT db2 - db2 close -} {} - -do_test 2.3.1 { - execsql { DELETE FROM t1 WHERE a>6 } - set snapshot [sqlite3_snapshot_get db main] - execsql { - INSERT INTO t1 VALUES('a', 'b'); - INSERT INTO t1 VALUES('c', 'd'); - SELECT * FROM t1; - } -} {1 2 3 4 5 6 a b c d} -do_test 2.3.2 { - execsql BEGIN - sqlite3_snapshot_open db main $snapshot - execsql { SELECT * FROM t1 } -} {1 2 3 4 5 6} - -do_test 2.3.3 { - catchsql { - INSERT INTO t1 VALUES('x','y') - } -} {1 {database is locked}} -do_test 2.3.4 { - execsql COMMIT - sqlite3_snapshot_free $snapshot -} {} - -#------------------------------------------------------------------------- -# Check some errors in sqlite3_snapshot_open(). It is an error if: -# -# 1) the db is in auto-commit mode, -# 2) the db has an open (read or write) transaction, -# 3) the db is not a wal database, -# -# Reuse the database created by earlier tests. -# -do_execsql_test 3.0.0 { - CREATE TABLE t2(x, y); - INSERT INTO t2 VALUES('a', 'b'); - INSERT INTO t2 VALUES('c', 'd'); - BEGIN; - SELECT * FROM t2; -} {a b c d} -do_test 3.0.1 { - set snapshot [sqlite3_snapshot_get db main] - execsql { COMMIT } - execsql { INSERT INTO t2 VALUES('e', 'f'); } -} {} - -do_test 3.1 { - list [catch {sqlite3_snapshot_open db main $snapshot } msg] $msg -} {1 SQLITE_ERROR} - -do_test 3.2.1 { - execsql { + do_test $tn.2.1.2 { + execsql BEGIN + snapshot_open db main $snapshot + execsql { + SELECT * FROM t1; + } + } {1 2 3 4 5 6 7 8} + + do_test $tn.2.1.3 { + snapshot_free $snapshot + execsql COMMIT + } {} + + do_test $tn.2.2.0 { + sqlite3 db2 test.db + execsql { + BEGIN; + SELECT * FROM t1; + } db2 + } {1 2 3 4 5 6 7 8 9 10} + + do_test $tn.2.2.1 { + set snapshot [snapshot_get db2 main] + execsql { + INSERT INTO t1 VALUES(11, 12); + SELECT * FROM t1; + } + } {1 2 3 4 5 6 7 8 9 10 11 12} + + do_test $tn.2.2.2 { + execsql BEGIN + snapshot_open db main $snapshot + execsql { + SELECT * FROM t1; + } + } {1 2 3 4 5 6 7 8 9 10} + + do_test $tn.2.2.3 { + snapshot_free $snapshot + execsql COMMIT + execsql COMMIT db2 + db2 close + } {} + + do_test $tn.2.3.1 { + execsql { DELETE FROM t1 WHERE a>6 } + db trans { set snapshot [snapshot_get db main] } + execsql { + INSERT INTO t1 VALUES('a', 'b'); + INSERT INTO t1 VALUES('c', 'd'); + SELECT * FROM t1; + } + } {1 2 3 4 5 6 a b c d} + do_test $tn.2.3.2 { + execsql BEGIN + snapshot_open db main $snapshot + execsql { SELECT * FROM t1 } + } {1 2 3 4 5 6} + + do_test $tn.2.3.3 { + catchsql { + INSERT INTO t1 VALUES('x','y') + } + } {1 {database is locked}} + do_test $tn.2.3.4 { + execsql COMMIT + snapshot_free $snapshot + } {} + + #------------------------------------------------------------------------- + # Check some errors in snapshot_open(). It is an error if: + # + # 1) the db is in auto-commit mode, + # 2) the db has an open (read or write) transaction, + # 3) the db is not a wal database, + # + # Reuse the database created by earlier tests. + # + do_execsql_test $tn.3.0.0 { + CREATE TABLE t2(x, y); + INSERT INTO t2 VALUES('a', 'b'); + INSERT INTO t2 VALUES('c', 'd'); BEGIN; SELECT * FROM t2; - } -} {a b c d e f} -do_test 3.2.2 { - list [catch {sqlite3_snapshot_open db main $snapshot } msg] $msg -} {1 SQLITE_ERROR} - -do_test 3.2.3 { - execsql { - COMMIT; - BEGIN; - INSERT INTO t2 VALUES('g', 'h'); - } - list [catch {sqlite3_snapshot_open db main $snapshot } msg] $msg -} {1 SQLITE_ERROR} -do_execsql_test 3.2.4 COMMIT - -do_test 3.3.1 { - execsql { PRAGMA journal_mode = DELETE } - execsql { BEGIN } - list [catch {sqlite3_snapshot_open db main $snapshot } msg] $msg -} {1 SQLITE_ERROR} - -do_test 3.3.2 { - sqlite3_snapshot_free $snapshot - execsql COMMIT -} {} - -#------------------------------------------------------------------------- -# Check that SQLITE_BUSY_SNAPSHOT is returned if the specified snapshot -# no longer exists because the wal file has been checkpointed. -# -# 1. Reading a snapshot from the middle of a wal file is not possible -# after the wal file has been checkpointed. -# -# 2. That a snapshot from the end of a wal file can not be read once -# the wal file has been wrapped. -# -do_execsql_test 4.1.0 { - PRAGMA journal_mode = wal; - CREATE TABLE t3(i, j); - INSERT INTO t3 VALUES('o', 't'); - INSERT INTO t3 VALUES('t', 'f'); - BEGIN; - SELECT * FROM t3; -} {wal o t t f} - -do_test 4.1.1 { - set snapshot [sqlite3_snapshot_get db main] - execsql COMMIT -} {} -do_test 4.1.2 { - execsql { - INSERT INTO t3 VALUES('f', 's'); - BEGIN; - } - sqlite3_snapshot_open db main $snapshot - execsql { SELECT * FROM t3 } -} {o t t f} - -do_test 4.1.3 { - execsql { - COMMIT; - PRAGMA wal_checkpoint; - BEGIN; - } - list [catch {sqlite3_snapshot_open db main $snapshot} msg] $msg -} {1 SQLITE_BUSY_SNAPSHOT} -do_test 4.1.4 { - sqlite3_snapshot_free $snapshot - execsql COMMIT -} {} - -do_test 4.2.1 { - execsql { - INSERT INTO t3 VALUES('s', 'e'); - INSERT INTO t3 VALUES('n', 't'); + } {a b c d} + do_test $tn.3.0.1 { + set snapshot [snapshot_get db main] + execsql { COMMIT } + execsql { INSERT INTO t2 VALUES('e', 'f'); } + } {} + + do_test $tn.3.1 { + list [catch {snapshot_open db main $snapshot } msg] $msg + } {1 SQLITE_ERROR} + + do_test $tn.3.2.1 { + execsql { + BEGIN; + SELECT * FROM t2; + } + } {a b c d e f} + + # Update - it is no longer an error to have a read-transaction open, + # provided there are no active SELECT statements. + do_test $tn.3.2.2a { + db eval "SELECT * FROM t2" { + set res [list [catch {snapshot_open db main $snapshot } msg] $msg] + break + } + set res + } {1 SQLITE_ERROR} + do_test $tn.3.2.2b { + snapshot_open db main $snapshot + } {} + + do_test $tn.3.2.3 { + execsql { + COMMIT; + BEGIN; + INSERT INTO t2 VALUES('g', 'h'); + } + list [catch {snapshot_open db main $snapshot } msg] $msg + } {1 SQLITE_ERROR} + do_execsql_test $tn.3.2.4 COMMIT + + do_test $tn.3.3.1a { + execsql { PRAGMA journal_mode = DELETE } + execsql { BEGIN } + list [catch {snapshot_open db main $snapshot } msg] $msg + } {1 SQLITE_ERROR} + + do_test $tn.3.3.1b { + execsql { COMMIT ; BEGIN ; SELECT * FROM t2 } + list [catch {snapshot_open db main $snapshot } msg] $msg + } {1 SQLITE_ERROR} + + do_test $tn.$tn.3.3.2 { + snapshot_free $snapshot + execsql COMMIT + } {} + + #------------------------------------------------------------------------- + # Check that SQLITE_ERROR_SNAPSHOT is returned if the specified snapshot + # no longer exists because the wal file has been checkpointed. + # + # 1. Reading a snapshot from the middle of a wal file is not possible + # after the wal file has been checkpointed. + # + # 2. That a snapshot from the end of a wal file can not be read once + # the wal file has been wrapped. + # + do_execsql_test $tn.4.1.0 { + PRAGMA journal_mode = wal; + CREATE TABLE t3(i, j); + INSERT INTO t3 VALUES('o', 't'); + INSERT INTO t3 VALUES('t', 'f'); BEGIN; SELECT * FROM t3; - } -} {o t t f f s s e n t} -do_test 4.2.2 { - set snapshot [sqlite3_snapshot_get db main] - execsql { - COMMIT; - PRAGMA wal_checkpoint; + } {wal o t t f} + + do_test $tn.4.1.1 { + set snapshot [snapshot_get db main] + execsql COMMIT + } {} + do_test $tn.4.1.2 { + execsql { + INSERT INTO t3 VALUES('f', 's'); + BEGIN; + } + snapshot_open db main $snapshot + execsql { SELECT * FROM t3 } + } {o t t f} + + do_test $tn.4.1.3 { + execsql { + COMMIT; + PRAGMA wal_checkpoint; + BEGIN; + } + list [catch {snapshot_open db main $snapshot} msg] $msg + } {1 SQLITE_ERROR_SNAPSHOT} + do_test $tn.4.1.4 { + snapshot_free $snapshot + execsql COMMIT + } {} + + do_test $tn.4.2.1 { + execsql { + INSERT INTO t3 VALUES('s', 'e'); + INSERT INTO t3 VALUES('n', 't'); + BEGIN; + SELECT * FROM t3; + } + } {o t t f f s s e n t} + do_test $tn.4.2.2 { + set snapshot [snapshot_get db main] + execsql { + COMMIT; + PRAGMA wal_checkpoint; + BEGIN; + } + snapshot_open db main $snapshot + execsql { SELECT * FROM t3 } + } {o t t f f s s e n t} + do_test $tn.4.2.3 { + execsql { + COMMIT; + INSERT INTO t3 VALUES('e', 't'); + BEGIN; + } + list [catch {snapshot_open db main $snapshot} msg] $msg + } {1 SQLITE_ERROR_SNAPSHOT} + do_test $tn.4.2.4 { + snapshot_free $snapshot + } {} + + #------------------------------------------------------------------------- + # Check that SQLITE_BUSY is returned if a checkpoint is running when + # sqlite3_snapshot_open() is called. + # + reset_db + db close + testvfs tvfs + sqlite3 db test.db -vfs tvfs + + do_execsql_test $tn.5.1 { + PRAGMA journal_mode = wal; + CREATE TABLE x1(x, xx, xxx); + INSERT INTO x1 VALUES('z', 'zz', 'zzz'); BEGIN; + SELECT * FROM x1; + } {wal z zz zzz} + + do_test $tn.5.2 { + set ::snapshot [snapshot_get db main] + sqlite3 db2 test.db -vfs tvfs + execsql { + INSERT INTO x1 VALUES('a', 'aa', 'aaa'); + COMMIT; + } + } {} + + set t53 0 + proc write_callback {args} { + do_test $tn.5.3.[incr ::t53] { + execsql BEGIN + list [catch { snapshot_open db main $::snapshot } msg] $msg + } {1 SQLITE_BUSY} + catchsql COMMIT } - sqlite3_snapshot_open db main $snapshot - execsql { SELECT * FROM t3 } -} {o t t f f s s e n t} -do_test 4.2.3 { - execsql { - COMMIT; - INSERT INTO t3 VALUES('e', 't'); + + tvfs filter xWrite + tvfs script write_callback + db2 eval { PRAGMA wal_checkpoint } + db close + db2 close + tvfs delete + snapshot_free $snapshot + + #------------------------------------------------------------------------- + # Test that sqlite3_snapshot_get() may be called immediately after + # "BEGIN; PRAGMA user_version;". And that sqlite3_snapshot_open() may + # be called after opening the db handle and running the script + # "PRAGMA user_version; BEGIN". + reset_db + do_execsql_test $tn.6.1 { + PRAGMA journal_mode = wal; + CREATE TABLE x1(x, xx, xxx); + INSERT INTO x1 VALUES('z', 'zz', 'zzz'); BEGIN; - } - list [catch {sqlite3_snapshot_open db main $snapshot} msg] $msg -} {1 SQLITE_BUSY_SNAPSHOT} -do_test 4.2.4 { - sqlite3_snapshot_free $snapshot -} {} - -#------------------------------------------------------------------------- -# Check that SQLITE_BUSY is returned if a checkpoint is running when -# sqlite3_snapshot_open() is called. -# -reset_db -db close -testvfs tvfs -sqlite3 db test.db -vfs tvfs - -do_execsql_test 5.1 { - PRAGMA journal_mode = wal; - CREATE TABLE x1(x, xx, xxx); - INSERT INTO x1 VALUES('z', 'zz', 'zzz'); - BEGIN; - SELECT * FROM x1; -} {wal z zz zzz} - -do_test 5.2 { - set ::snapshot [sqlite3_snapshot_get db main] - sqlite3 db2 test.db -vfs tvfs - execsql { - INSERT INTO x1 VALUES('a', 'aa', 'aaa'); - COMMIT; - } -} {} + PRAGMA user_version; + } {wal 0} + do_test $tn.6.2 { + set ::snapshot [snapshot_get db main] + execsql { + INSERT INTO x1 VALUES('a', 'aa', 'aaa'); + COMMIT; + } + } {} + do_test $tn.6.3 { + sqlite3 db2 test.db + db2 eval "PRAGMA user_version ; BEGIN" + snapshot_open db2 main $::snapshot + db2 eval { SELECT * FROM x1 } + } {z zz zzz} + do_test $tn.6.4 { + db2 close + sqlite3 db2 test.db + db2 eval "PRAGMA application_id" + db2 eval "BEGIN" + snapshot_open db2 main $::snapshot + db2 eval { SELECT * FROM x1 } + } {z zz zzz} -set t53 0 -proc write_callback {args} { - do_test 5.3.[incr ::t53] { - execsql BEGIN - list [catch { sqlite3_snapshot_open db main $::snapshot } msg] $msg - } {1 SQLITE_BUSY} - catchsql COMMIT -} + do_test $tn.6.5 { + db2 close + sqlite3 db2 test.db + db2 eval "BEGIN" + list [catch {snapshot_open db2 main $::snapshot} msg] $msg + } {1 SQLITE_ERROR} + + snapshot_free $snapshot + + #------------------------------------------------------------------------- + # The following tests investigate the sqlite3_snapshot_cmp() API. + # -tvfs filter xWrite -tvfs script write_callback -db2 eval { PRAGMA wal_checkpoint } -db close -db2 close -tvfs delete -sqlite3_snapshot_free $snapshot - -#------------------------------------------------------------------------- -# Test that sqlite3_snapshot_get() may be called immediately after -# "BEGIN; PRAGMA user_version;". And that sqlite3_snapshot_open() may -# be called after opening the db handle and running the script -# "PRAGMA user_version; BEGIN". -reset_db -do_execsql_test 6.1 { - PRAGMA journal_mode = wal; - CREATE TABLE x1(x, xx, xxx); - INSERT INTO x1 VALUES('z', 'zz', 'zzz'); - BEGIN; - PRAGMA user_version; -} {wal 0} -do_test 6.2 { - set ::snapshot [sqlite3_snapshot_get db main] - execsql { - INSERT INTO x1 VALUES('a', 'aa', 'aaa'); - COMMIT; + # Compare snapshots $p1 and $p2, checking that the result is $r. + # + proc do_snapshot_cmp_test {tn p1 p2 r} { + uplevel [list do_test $tn.1 [list snapshot_cmp $p1 $p2] $r] + uplevel [list do_test $tn.2 [list snapshot_cmp $p2 $p1] [expr $r*-1]] + uplevel [list do_test $tn.3 [list snapshot_cmp $p1 $p1] 0] + uplevel [list do_test $tn.4 [list snapshot_cmp $p2 $p2] 0] } -} {} -do_test 6.3 { - sqlite3 db2 test.db - db2 eval "PRAGMA user_version ; BEGIN" - sqlite3_snapshot_open db2 main $::snapshot - db2 eval { SELECT * FROM x1 } -} {z zz zzz} -sqlite3_snapshot_free $snapshot + + catch { db2 close } + reset_db + + do_execsql_test $tn.7.1 { + PRAGMA journal_mode = wal; + CREATE TABLE t1(x); + } wal + + do_test $tn.7.1.2 { + execsql { BEGIN ; PRAGMA application_id } + set p1 [snapshot_get db main] + execsql { + INSERT INTO t1 VALUES(10); + COMMIT; + } + execsql { BEGIN ; PRAGMA application_id } + set p2 [snapshot_get db main] + execsql COMMIT + } {} + + do_snapshot_cmp_test $tn.7.1.3 $p1 $p2 -1 + snapshot_free $p1 + snapshot_free $p2 + + do_execsql_test $tn.7.2.1 { + INSERT INTO t1 VALUES(11); + INSERT INTO t1 VALUES(12); + INSERT INTO t1 VALUES(13); + BEGIN; + PRAGMA application_id; + } {0} + do_test $tn.7.2.2 { + set p1 [snapshot_get db main] + execsql { + COMMIT; + INSERT INTO t1 VALUES(14); + PRAGMA wal_checkpoint; + BEGIN; + PRAGMA application_id; + } + set p2 [snapshot_get db main] + execsql COMMIT + } {} + + do_snapshot_cmp_test $tn.7.2.3 $p1 $p2 -1 + snapshot_free $p2 + + do_test $tn.7.3.1 { + execsql { + INSERT INTO t1 VALUES(14); + BEGIN; + PRAGMA application_id; + } + set p2 [snapshot_get db main] + execsql COMMIT + } {} + + do_snapshot_cmp_test $tn.7.3.2 $p1 $p2 -1 + snapshot_free $p1 + snapshot_free $p2 +} finish_test diff --git a/test/snapshot2.test b/test/snapshot2.test new file mode 100644 index 0000000000..0f4f177cb3 --- /dev/null +++ b/test/snapshot2.test @@ -0,0 +1,240 @@ +# 2016 November 18 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The focus +# of this file is the sqlite3_snapshot_xxx() APIs. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +ifcapable !snapshot {finish_test; return} +set testprefix snapshot2 + +# This test does not work with the inmemory_journal permutation. The reason +# is that each connection opened as part of this permutation executes +# "PRAGMA journal_mode=memory", which fails if the database is in wal mode +# and there are one or more existing connections. +if {[permutation]=="inmemory_journal"} { + finish_test + return +} + +#------------------------------------------------------------------------- +# Check that it is not possible to obtain a snapshot immediately after +# a wal mode database with an empty wal file is opened. But it is after +# the file has been written, even by some other connection. +# +do_execsql_test 1.0 { + PRAGMA journal_mode = wal; + CREATE TABLE t1(a, b, c); + INSERT INTO t1 VALUES(1, 2, 3); + INSERT INTO t1 VALUES(4, 5, 6); +} {wal} + +db close +do_test 1.1.1 { list [file exists test.db] [file exists test.db-wal] } {1 0} + +sqlite3 db test.db +do_execsql_test 1.1.2 { SELECT * FROM t1 } {1 2 3 4 5 6} + +do_test 1.1.3 { + execsql BEGIN + list [catch { sqlite3_snapshot_get_blob db main } msg] $msg +} {1 SQLITE_ERROR} +execsql COMMIT + +do_test 1.1.4 { + execsql { INSERT INTO t1 VALUES(7, 8, 9) } + execsql BEGIN + string length [sqlite3_snapshot_get_blob db main] +} 48 +execsql COMMIT + +db close +do_test 1.2.1 { list [file exists test.db] [file exists test.db-wal] } {1 0} + +sqlite3 db test.db +do_execsql_test 1.2.2 { SELECT * FROM t1 } {1 2 3 4 5 6 7 8 9} + +do_test 1.2.3 { + execsql BEGIN + list [catch { sqlite3_snapshot_get_blob db main } msg] $msg +} {1 SQLITE_ERROR} +execsql COMMIT + +do_test 1.2.4 { + sqlite3 db2 test.db + execsql { INSERT INTO t1 VALUES(10, 11, 12) } db2 + execsql BEGIN + string length [sqlite3_snapshot_get_blob db main] +} 48 +execsql COMMIT +db2 close + +#------------------------------------------------------------------------- +# Simple tests for sqlite3_snapshot_recover(). +# +reset_db +do_execsql_test 2.0 { + CREATE TABLE t1(x); + PRAGMA journal_mode = wal; + INSERT INTO t1 VALUES(1); + INSERT INTO t1 VALUES(2); +} {wal} + +do_test 2.1 { + db trans { set snap [sqlite3_snapshot_get_blob db main] } + sqlite3_db_config db NO_CKPT_ON_CLOSE 1 + db close + sqlite3 db test.db + + execsql {SELECT * FROM sqlite_master} + execsql BEGIN + sqlite3_snapshot_open_blob db main $snap + execsql COMMIT; + execsql { INSERT INTO t1 VALUES(3); } +} {} + +do_test 2.2 { + sqlite3_db_config db NO_CKPT_ON_CLOSE 1 + db close + sqlite3 db test.db + + execsql {SELECT * FROM sqlite_master} + execsql BEGIN + list [catch { sqlite3_snapshot_open_blob db main $snap } msg] $msg +} {1 SQLITE_ERROR_SNAPSHOT} + +do_test 2.3 { + execsql COMMIT + sqlite3_snapshot_recover db main + execsql BEGIN + sqlite3_snapshot_open_blob db main $snap + execsql { SELECT * FROM t1 } +} {1 2} + +do_test 2.4 { + execsql COMMIT + execsql { SELECT * FROM t1 } +} {1 2 3} + +do_test 2.5 { + execsql { PRAGMA wal_checkpoint } + sqlite3_db_config db NO_CKPT_ON_CLOSE 1 + db close + sqlite3 db test.db + + sqlite3_snapshot_recover db main + execsql BEGIN + list [catch { sqlite3_snapshot_open_blob db main $snap } msg] $msg +} {1 SQLITE_ERROR_SNAPSHOT} + +#------------------------------------------------------------------------- +# Check that calling sqlite3_snapshot_recover() does not confuse the +# pager cache. +reset_db +do_execsql_test 3.0 { + PRAGMA journal_mode = wal; + CREATE TABLE t1(x, y); + INSERT INTO t1 VALUES('a', 'b'); + INSERT INTO t1 VALUES('c', 'd'); +} {wal} +do_test 3.1 { + sqlite3 db2 test.db + execsql { INSERT INTO t1 VALUES('e', 'f') } db2 + db2 close + sqlite3_snapshot_recover db main +} {} +do_execsql_test 3.2 { + SELECT * FROM t1; +} {a b c d e f} + +#------------------------------------------------------------------------- +# Check that sqlite3_snapshot_recover() returns an error if it is called +# with an open read-transaction. Or on a database that does not exist. Or +# on the temp database. Or on a db that is not in wal mode. +# +do_test 4.1 { + sqlite3_snapshot_recover db main +} {} +do_test 4.2 { + execsql { + BEGIN; + SELECT * FROM sqlite_master; + } + list [catch { sqlite3_snapshot_recover db main } msg] $msg +} {1 SQLITE_ERROR} +do_test 4.3 { + execsql COMMIT + sqlite3_snapshot_recover db main +} {} +do_test 4.4 { + list [catch { sqlite3_snapshot_recover db aux } msg] $msg +} {1 SQLITE_ERROR} +do_test 4.5 { + forcedelete test.db2 + execsql { + ATTACH 'test.db2' AS aux; + PRAGMA aux.journal_mode = wal; + CREATE TABLE aux.t2(x, y); + } + list [catch { sqlite3_snapshot_recover db aux } msg] $msg +} {0 {}} +do_test 4.6 { + list [catch { sqlite3_snapshot_recover db temp } msg] $msg +} {1 SQLITE_ERROR} +do_test 4.7 { + execsql { + PRAGMA aux.journal_mode = delete; + } + list [catch { sqlite3_snapshot_recover db aux } msg] $msg +} {1 SQLITE_ERROR} + +#------------------------------------------------------------------------- +reset_db +sqlite3 db2 test.db +do_execsql_test 5.0 { + CREATE TABLE t2(x); + PRAGMA journal_mode = wal; + INSERT INTO t2 VALUES('abc'); + INSERT INTO t2 VALUES('def'); + INSERT INTO t2 VALUES('ghi'); +} {wal} + +do_test 5.1 { + execsql { + SELECT * FROM t2; + BEGIN; + } db2 + set snap [sqlite3_snapshot_get_blob db2 main] + db2 eval END +} {} + +do_test 5.2 { + execsql BEGIN db2 + sqlite3_snapshot_open_blob db2 main $snap + db2 eval { SELECT * FROM t2 ; END } +} {abc def ghi} + +do_test 5.3 { + execsql { PRAGMA wal_checkpoint = RESTART } + execsql BEGIN db2 + sqlite3_snapshot_open_blob db2 main $snap + db2 eval { SELECT * FROM t2 ; END } +} {abc def ghi} + +do_test 5.4 { + execsql { INSERT INTO t2 VALUES('jkl') } + execsql BEGIN db2 + list [catch { sqlite3_snapshot_open_blob db2 main $snap } msg] $msg +} {1 SQLITE_ERROR_SNAPSHOT} + + +finish_test diff --git a/test/snapshot3.test b/test/snapshot3.test new file mode 100644 index 0000000000..6d57b1d0c4 --- /dev/null +++ b/test/snapshot3.test @@ -0,0 +1,141 @@ +# 2016 September 23 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The focus +# of this file is the sqlite3_snapshot_xxx() APIs. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +ifcapable !snapshot {finish_test; return} +set testprefix snapshot3 + +# This test does not work with the inmemory_journal permutation. The reason +# is that each connection opened as part of this permutation executes +# "PRAGMA journal_mode=memory", which fails if the database is in wal mode +# and there are one or more existing connections. +if {[permutation]=="inmemory_journal"} { + finish_test + return +} + +#------------------------------------------------------------------------- +# This block of tests verifies that it is not possible to wrap the wal +# file - using a writer or a "PRAGMA wal_checkpoint = TRUNCATE" - while +# there is an open snapshot transaction (transaction opened using +# sqlite3_snapshot_open()). +# +do_execsql_test 1.0 { + CREATE TABLE t1(y); + PRAGMA journal_mode = wal; + INSERT INTO t1 VALUES(1); + INSERT INTO t1 VALUES(2); + INSERT INTO t1 VALUES(3); + INSERT INTO t1 VALUES(4); +} {wal} + +do_test 1.1 { + sqlite3 db2 test.db + sqlite3 db3 test.db + + execsql {SELECT * FROM sqlite_master} db2 + execsql {SELECT * FROM sqlite_master} db3 + + db2 trans { set snap [sqlite3_snapshot_get_blob db2 main] } + db2 eval { SELECT * FROM t1 } +} {1 2 3 4} + +do_test 1.2 { + execsql BEGIN db2 + sqlite3_snapshot_open_blob db2 main $snap + db2 eval { SELECT * FROM t1 } +} {1 2 3 4} + +do_test 1.2 { + execsql END db2 + execsql { PRAGMA wal_checkpoint } + + execsql BEGIN db2 + sqlite3_snapshot_open_blob db2 main $snap + db2 eval { SELECT * FROM t1 } +} {1 2 3 4} + +set sz [file size test.db-wal] +do_test 1.3 { + execsql { PRAGMA wal_checkpoint = truncate } + file size test.db-wal +} $sz + +do_test 1.4 { + execsql BEGIN db3 + list [catch { sqlite3_snapshot_open_blob db3 main $snap } msg] $msg +} {0 {}} + +do_test 1.5 { + db3 eval { SELECT * FROM t1; END } +} {1 2 3 4} + +do_test 1.6 { + db2 eval { SELECT * FROM t1; END } +} {1 2 3 4} + +do_test 1.7 { + execsql { PRAGMA wal_checkpoint = truncate } + file size test.db-wal +} 0 + +do_test 1.8 { + execsql BEGIN db3 + list [catch { sqlite3_snapshot_open_blob db3 main $snap } msg] $msg +} {1 SQLITE_ERROR_SNAPSHOT} + +db3 close +db2 close + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 2.0 { + PRAGMA journal_mode = wal; + CREATE TABLE t1(a, b); + INSERT INTO t1 VALUES(1, 2); + INSERT INTO t1 VALUES(3, 4); +} {wal} + +sqlite3 db2 test.db +sqlite3 db3 test.db +do_execsql_test -db db2 2.0.1 { + SELECT * FROM t1 +} {1 2 3 4} +do_execsql_test -db db3 2.0.2 { + SELECT * FROM t1 +} {1 2 3 4} + +do_execsql_test -db db2 2.2 { + PRAGMA wal_checkpoint; +} {0 4 4} + +do_test 2.1 { + db eval { BEGIN } + set snap [sqlite3_snapshot_get db main] + set {} {} +} {} + +do_execsql_test -db db2 2.3 { + INSERT INTO t1 VALUES(5, 6); +} {} + +do_test 2.2 { + execsql { BEGIN } db3 + sqlite3_snapshot_open db3 main $snap +} {} + +sqlite3_snapshot_free $snap + +finish_test diff --git a/test/snapshot4.test b/test/snapshot4.test new file mode 100644 index 0000000000..ac7f6ad6c2 --- /dev/null +++ b/test/snapshot4.test @@ -0,0 +1,74 @@ +# 2018 August 28 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The focus +# of this file is the sqlite3_snapshot_xxx() APIs. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +ifcapable !snapshot {finish_test; return} +set testprefix snapshot4 + +# This test does not work with the inmemory_journal permutation. The reason +# is that each connection opened as part of this permutation executes +# "PRAGMA journal_mode=memory", which fails if the database is in wal mode +# and there are one or more existing connections. +if {[permutation]=="inmemory_journal"} { + finish_test + return +} + +sqlite3 db2 test.db + +do_execsql_test 1.0 { + PRAGMA cache_size = 10; + CREATE TABLE t1(a, b); + INSERT INTO t1 VALUES(1, randomblob(400)); + PRAGMA journal_mode = wal; + WITH s(i) AS ( + SELECT 2 UNION ALL SELECT i+1 FROM s WHERE i<100 + ) + INSERT INTO t1 SELECT i, randomblob(400) FROM s; +} {wal} + +do_test 1.1 { + execsql { + BEGIN; + SELECT count(*) FROM t1; + } +} {100} + +do_test 1.2 { + db2 eval { + SELECT count(*) FROM t1; + CREATE TABLE t2(x); + } +} {100} + +do_test 1.3 { + set ::snap [sqlite3_snapshot_get_blob db main] + db2 eval { PRAGMA wal_checkpoint } +} {0 54 52} + +do_test 1.4 { + execsql { + COMMIT; + SELECT * FROM sqlite_master; + BEGIN; + } + sqlite3_snapshot_open_blob db main $::snap + execsql { + SELECT count(*) FROM t1 + } +} {100} + + +finish_test diff --git a/test/snapshot_fault.test b/test/snapshot_fault.test index 3ac13daefd..10c5094594 100644 --- a/test/snapshot_fault.test +++ b/test/snapshot_fault.test @@ -23,6 +23,7 @@ set testprefix snapshot_fault # checkpointing the db. # do_faultsim_test 1.0 -prep { + catch { db2 close } faultsim_delete_and_reopen sqlite3 db2 test.db db2 eval { @@ -47,7 +48,7 @@ do_faultsim_test 1.0 -prep { } -test { db2 eval BEGIN if {[catch { sqlite3_snapshot_open db2 main $::snapshot } msg]} { - if {$msg != "SQLITE_BUSY_SNAPSHOT" && $msg != "SQLITE_BUSY"} { + if {$msg != "SQLITE_ERROR_SNAPSHOT" && $msg != "SQLITE_BUSY"} { error "error is $msg" } } else { @@ -98,7 +99,7 @@ do_faultsim_test 2.0 -prep { db eval BEGIN if {[catch { sqlite3_snapshot_open db main $::snapshot } msg]} { - if {$msg != "SQLITE_BUSY_SNAPSHOT" && $msg != "SQLITE_BUSY"} { + if {$msg != "SQLITE_ERROR_SNAPSHOT" && $msg != "SQLITE_BUSY"} { error "error is $msg" } } else { @@ -146,7 +147,7 @@ do_faultsim_test 3.0 -prep { error $msg } } -test { - faultsim_test_result {0 {}} {1 SQLITE_IOERR} \ + faultsim_test_result {0 {}} {1 SQLITE_IOERR} {1 SQLITE_NOMEM} \ {1 SQLITE_IOERR_NOMEM} {1 SQLITE_IOERR_READ} if {$testrc==0} { set res [db eval { @@ -159,6 +160,93 @@ do_faultsim_test 3.0 -prep { sqlite3_snapshot_free $::snapshot } +#------------------------------------------------------------------------- +# Test the handling of faults that occur within sqlite3_snapshot_recover(). +# +reset_db +do_execsql_test 4.0 { + PRAGMA journal_mode = wal; + CREATE TABLE t1(zzz); + INSERT INTO t1 VALUES('abc'); + INSERT INTO t1 VALUES('def'); +} {wal} +faultsim_save_and_close + +do_test 4.0.1 { + faultsim_restore_and_reopen + db eval { SELECT * FROM sqlite_master } + sqlite3_snapshot_recover db main +} {} +db close + +do_faultsim_test 4.0 -faults oom* -prep { + faultsim_restore_and_reopen + db eval { SELECT * FROM sqlite_master } +} -body { + sqlite3_snapshot_recover db main +} -test { + faultsim_test_result {0 {}} {1 SQLITE_NOMEM} {1 SQLITE_IOERR_NOMEM} +} + +# The following test cases contrive to call sqlite3_snapshot_recover() +# before all pages of the *-shm file have been mapped. This tests an +# extra branch of error handling logic in snapshot_recover(). +# +reset_db +do_execsql_test 4.1.0 { + PRAGMA page_size = 512; + PRAGMA journal_mode = wal; + PRAGMA wal_autocheckpoint = 0; + CREATE TABLE t1(zzz); + INSERT INTO t1 VALUES(randomblob( 500 * 9500 )); + PRAGMA user_version = 211; +} {wal 0} + +do_test 4.1.1 { + list [file size test.db-shm] [file size test.db] +} {98304 512} + +faultsim_save_and_close +do_faultsim_test 4.1 -faults shm* -prep { + catch { db2 close } + catch { db close } + faultsim_restore_and_reopen + sqlite3 db2 test.db + db2 eval { SELECT * FROM sqlite_master } + db eval BEGIN + sqlite3_snapshot_get_blob db main + db eval COMMIT +} -body { + sqlite3_snapshot_recover db main +} -test { + faultsim_test_result {0 {}} {1 SQLITE_IOERR} +} + +#------------------------------------------------------------------------- +# Test the handling of faults that occur within sqlite3_snapshot_get(). +# +reset_db +do_execsql_test 5.0 { + PRAGMA page_size = 512; + PRAGMA journal_mode = wal; + PRAGMA wal_autocheckpoint = 0; + CREATE TABLE t1(zzz); + INSERT INTO t1 VALUES(randomblob( 5000 )); + PRAGMA user_version = 211; +} {wal 0} +faultsim_save_and_close + +do_faultsim_test 5 -prep { + faultsim_restore_and_reopen + execsql { SELECT count(*) FROM sqlite_master } + execsql BEGIN +} -body { + sqlite3_snapshot_get_blob db main + set {} {} +} -test { + execsql END + faultsim_test_result {0 {}} {1 SQLITE_IOERR} {1 SQLITE_NOMEM} +} finish_test diff --git a/test/snapshot_up.test b/test/snapshot_up.test new file mode 100644 index 0000000000..5f389e7be5 --- /dev/null +++ b/test/snapshot_up.test @@ -0,0 +1,185 @@ +# 2018 August 6 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# Tests for calling sqlite3_snapshot_open() when there is already +# a read transaction open on the database. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +ifcapable !snapshot {finish_test; return} +set testprefix snapshot_up + +# This test does not work with the inmemory_journal permutation. The reason +# is that each connection opened as part of this permutation executes +# "PRAGMA journal_mode=memory", which fails if the database is in wal mode +# and there are one or more existing connections. +if {[permutation]=="inmemory_journal"} { + finish_test + return +} + +db timeout 1000 + +do_execsql_test 1.0 { + CREATE TABLE t1(a, b, c); + PRAGMA journal_mode = wal; + INSERT INTO t1 VALUES(1, 2, 3); + INSERT INTO t1 VALUES(4, 5, 6); + INSERT INTO t1 VALUES(7, 8, 9); +} {wal} + +do_test 1.1 { + execsql BEGIN + set ::snap1 [sqlite3_snapshot_get db main] + execsql COMMIT + execsql { INSERT INTO t1 VALUES(10, 11, 12); } + execsql BEGIN + set ::snap2 [sqlite3_snapshot_get db main] + execsql COMMIT + execsql { INSERT INTO t1 VALUES(13, 14, 15); } + execsql BEGIN + set ::snap3 [sqlite3_snapshot_get db main] + execsql COMMIT +} {} + +do_execsql_test 1.2 { + BEGIN; + SELECT * FROM t1 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15} + +do_test 1.3 { + sqlite3_snapshot_open db main $::snap1 + execsql { SELECT * FROM t1 } +} {1 2 3 4 5 6 7 8 9} + +do_test 1.4 { + sqlite3_snapshot_open db main $::snap2 + execsql { SELECT * FROM t1 } +} {1 2 3 4 5 6 7 8 9 10 11 12} + +do_test 1.5 { + sqlite3 db2 test.db + execsql { PRAGMA wal_checkpoint } db2 +} {0 5 4} + +do_execsql_test 1.6 { + SELECT * FROM t1 +} {1 2 3 4 5 6 7 8 9 10 11 12} + +do_test 1.7 { + list [catch { sqlite3_snapshot_open db main $::snap1 } msg] $msg +} {1 SQLITE_ERROR_SNAPSHOT} + +do_execsql_test 1.8 { + SELECT * FROM t1 +} {1 2 3 4 5 6 7 8 9 10 11 12} + +do_test 1.9 { + execsql { COMMIT ; BEGIN } + list [catch { sqlite3_snapshot_open db main $::snap1 } msg] $msg +} {1 SQLITE_ERROR_SNAPSHOT} + +do_test 1.10 { + execsql { COMMIT } + execsql { + PRAGMA wal_checkpoint; + DELETE FROM t1 WHERE a = 1; + } db2 + execsql BEGIN + set ::snap4 [sqlite3_snapshot_get db main] + execsql COMMIT + execsql { + DELETE FROM t1 WHERE a = 4; + } db2 +} {} + +do_test 1.11 { + execsql { + BEGIN; + SELECT * FROM t1 + } +} {7 8 9 10 11 12 13 14 15} +do_test 1.12 { + sqlite3_snapshot_open db main $::snap4 + execsql { SELECT * FROM t1 } +} {4 5 6 7 8 9 10 11 12 13 14 15} + +do_test 1.13 { + list [catch { sqlite3_snapshot_open db main $::snap3 } msg] $msg +} {1 SQLITE_ERROR_SNAPSHOT} +do_test 1.14 { + execsql { SELECT * FROM t1 } +} {4 5 6 7 8 9 10 11 12 13 14 15} + +db close +db2 close +sqlite3 db test.db +do_execsql_test 1.15 { + BEGIN; + SELECT * FROM t1 +} {7 8 9 10 11 12 13 14 15} +do_test 1.16 { + list [catch { sqlite3_snapshot_open db main $::snap4 } msg] $msg +} {1 SQLITE_ERROR_SNAPSHOT} +do_execsql_test 1.17 { COMMIT } + +sqlite3_snapshot_free $::snap1 +sqlite3_snapshot_free $::snap2 +sqlite3_snapshot_free $::snap3 +sqlite3_snapshot_free $::snap4 + +#------------------------------------------------------------------------- +catch { db close } +sqlite3 db test.db +sqlite3 db2 test.db +sqlite3 db3 test.db + +proc xBusy {args} { return 1 } +db3 busy xBusy + +do_test 2.1 { + execsql { INSERT INTO t1 VALUES(16, 17, 18) } db2 + execsql BEGIN + set ::snap1 [sqlite3_snapshot_get db main] + execsql COMMIT + execsql { INSERT INTO t1 VALUES(19, 20, 21) } db2 + execsql BEGIN + set ::snap2 [sqlite3_snapshot_get db main] + execsql COMMIT + set {} {} +} {} + +do_execsql_test -db db2 2.2 { + BEGIN; + INSERT INTO t1 VALUES(19, 20, 21); +} + +do_test 2.3 { + execsql BEGIN + sqlite3_snapshot_open db main $::snap1 + execsql { SELECT * FROM t1 } +} {7 8 9 10 11 12 13 14 15 16 17 18} + +proc xBusy {args} { + set ::res [list [catch { sqlite3_snapshot_open db main $::snap2 } msg] $msg] + return 1 +} +db3 busy xBusy +do_test 2.4 { + execsql {PRAGMA wal_checkpoint = restart} db3 + set ::res +} {1 SQLITE_BUSY} + +sqlite3_snapshot_free $::snap1 +sqlite3_snapshot_free $::snap2 + +finish_test diff --git a/test/soak.test b/test/soak.test index c457dec4a9..e8535a1456 100644 --- a/test/soak.test +++ b/test/soak.test @@ -67,12 +67,12 @@ set SOAKTESTS { set G(isquick) 1 -set soak_starttime [clock seconds] +set soak_starttime [clock_seconds] set soak_finishtime [expr {$soak_starttime + $TIMEOUT}] # Loop until the timeout is reached or an error occurs. # -for {set iRun 0} {[clock seconds] < $soak_finishtime} {incr iRun} { +for {set iRun 0} {[clock_seconds] < $soak_finishtime} {incr iRun} { set iIdx [expr {$iRun % [llength $SOAKTESTS]}] source [file join $testdir [lindex $SOAKTESTS $iIdx]] diff --git a/test/sort.test b/test/sort.test index d73ecea480..e6da6c6baf 100644 --- a/test/sort.test +++ b/test/sort.test @@ -595,4 +595,36 @@ do_execsql_test 17.1 { SELECT * FROM sqlite_master ORDER BY sql; } {} +# 2022-12-03 Ticket e8b674241947eb3b +# Improve estimates for the cost of sorting relative +# to the cost of doing an index lookup, so as to get +# a better query plan. See the ticket for a deetailed +# example. +# +reset_db +do_execsql_test 18.1 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c); + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<50) + -- increase to 5000 for actual test data ----^^ + INSERT INTO t1(a,b,c) SELECT x, random()%5000, random()%5000 FROM c; + CREATE TABLE t2(d,e,f); + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<500) + -- increase to 50000 for actual test data -----^^^ + INSERT INTO t2(d,e,f) SELECT + NULLIF(0, random()%2), random()%5000, random()%5000 + FROM c; + ANALYZE; + UPDATE sqlite_stat1 SET stat='50000' WHERE tbl='t2'; + UPDATE sqlite_stat1 SET stat='5000' WHERE tbl='t1'; + ANALYZE sqlite_schema; +} {} +do_execsql_test 18.2 { + EXPLAIN QUERY PLAN + SELECT a FROM t1 JOIN t2 + WHERE a IN (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20) + AND a=CASE WHEN d IS NOT NULL THEN e ELSE f END + ORDER BY a; +} {/.*SCAN t2.*SEARCH t1.*/} +# ^^^^^^^--^^^^^^^^^--- t2 should be the outer loop. + finish_test diff --git a/test/sort2.test b/test/sort2.test index f686654d53..9fe4b4ccf3 100644 --- a/test/sort2.test +++ b/test/sort2.test @@ -69,7 +69,7 @@ foreach {tn script} { # Because it uses so much data, this test can take 12-13 seconds even on # a modern workstation. So it is omitted from "veryquick" and other # permutations.test tests. - if {[isquick]==0} { + if {[isquick]==0 && [clang_sanitize_address]==0} { do_execsql_test $tn.3 { PRAGMA cache_size = 5; WITH r(x,y) AS ( diff --git a/test/sort4.test b/test/sort4.test index 13d9a5999a..17aa28baec 100644 --- a/test/sort4.test +++ b/test/sort4.test @@ -8,6 +8,8 @@ # May you share freely, never taking more than you give. # #*********************************************************************** +# TESTRUNNER: superslow +# # This file implements regression tests for SQLite library. # # The tests in this file are brute force tests of the multi-threaded @@ -24,20 +26,22 @@ sqlite3_initialize sqlite3 db test.db -# Configure the sorter to use 3 background threads. -# -# EVIDENCE-OF: R-19249-32353 SQLITE_LIMIT_WORKER_THREADS The maximum -# number of auxiliary worker threads that a single prepared statement -# may start. -# -do_test sort4-init001 { - db eval {PRAGMA threads=5} - sqlite3_limit db SQLITE_LIMIT_WORKER_THREADS -1 -} {5} -do_test sort4-init002 { - sqlite3_limit db SQLITE_LIMIT_WORKER_THREADS 3 - db eval {PRAGMA threads} -} {3} +if {![string match *MAX_WORKER_THREADS=0* [db eval {PRAGMA compile_options}]]} { + # Configure the sorter to use 3 background threads. + # + # EVIDENCE-OF: R-19249-32353 SQLITE_LIMIT_WORKER_THREADS The maximum + # number of auxiliary worker threads that a single prepared statement + # may start. + # + do_test sort4-init001 { + db eval {PRAGMA threads=5} + sqlite3_limit db SQLITE_LIMIT_WORKER_THREADS -1 + } {5} + do_test sort4-init002 { + sqlite3_limit db SQLITE_LIMIT_WORKER_THREADS 3 + db eval {PRAGMA threads} + } {3} +} # Minimum number of seconds to run for. If the value is 0, each test diff --git a/test/sort5.test b/test/sort5.test index 5b1292bb48..80dce01ab6 100644 --- a/test/sort5.test +++ b/test/sort5.test @@ -41,4 +41,82 @@ do_execsql_test 1.2 { db close tvfs delete + +#------------------------------------------------------------------------- +# Test that the PMA size is determined correctly. The PMA size should be +# roughly the same amount of memory allocated to the main pager cache, or +# 250 pages if this is larger. +# +testvfs tvfs +tvfs script tv_callback +tvfs filter {xOpen xWrite} + +proc tv_callback {method args} { + global iTemp + global F + switch $method { + xOpen { + if {[lindex $args 0]==""} { return "temp[incr iTemp]" } + return "SQLITE_OK" + } + + xWrite { + foreach {filename id off amt} $args {} + if {[info exists F($id)]==0 || $F($id)<($off + $amt)} { + set F($id) [expr $off+$amt] + } + } + } +} + +catch { db close } +forcedelete test.db +sqlite3 db test.db -vfs tvfs +execsql { CREATE TABLE t1(x) } +execsql { PRAGMA temp_store = 1 } + +# Each iteration of the following loop attempts to sort 10001 records +# each a bit over 100 bytes in size. In total a little more than 1MiB +# of data. +# +foreach {tn pgsz cachesz bTemp} { + 1 4096 1000 0 + 2 1024 1000 1 + + 3 4096 -1000 1 + 4 1024 -1000 1 + + 5 4096 -9000 0 + 6 1024 -9000 0 +} { + if {$::TEMP_STORE>2} { + set bTemp 0 + } + do_execsql_test 2.$tn.0 " + PRAGMA page_size = $pgsz; + VACUUM; + PRAGMA cache_size = $cachesz; + " + + if {[db one {PRAGMA page_size}]!=$pgsz} { + # SEE is not able to change page sizes and that messes up the + # results that follow. + continue + } + + do_test 2.$tn.1 { + set ::iTemp 0 + catch { array unset F } + execsql { + WITH x(i, j) AS ( + SELECT 1, randomblob(100) + UNION ALL + SELECT i+1, randomblob(100) FROM x WHERE i<10000 + ) + SELECT * FROM x ORDER BY j; + } + expr {[array names F]!=""} + } $bTemp +} + finish_test diff --git a/test/sorterref.test b/test/sorterref.test new file mode 100644 index 0000000000..adf1cf53b2 --- /dev/null +++ b/test/sorterref.test @@ -0,0 +1,56 @@ +# 2018 April 14. +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix sorterref + +# If SQLITE_OMIT_ALTERTABLE is defined, omit this file. +ifcapable !altertable { + finish_test + return +} + +do_execsql_test 1.0 { + CREATE TABLE t1(a, b, c); + INSERT INTO t1 VALUES(1, 2, 3); + INSERT INTO t1 VALUES(4, 5, 6); + ALTER TABLE t1 ADD COLUMN d DEFAULT 'string'; + INSERT INTO t1 VALUES(7, 8, 9, 'text'); +} + +do_execsql_test 1.1 { + SELECT * FROM t1 ORDER BY b; +} { + 1 2 3 string 4 5 6 string 7 8 9 text +} + +do_execsql_test 2.0 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a, b); + CREATE TABLE t2(c, d, PRIMARY KEY(c)) WITHOUT ROWID; + + INSERT INTO t1 VALUES(1, 2); + INSERT INTO t1 VALUES(2, 3); + INSERT INTO t1 VALUES(3, 4); + + INSERT INTO t2 VALUES(1, 'one'); + INSERT INTO t2 VALUES(3, 'three'); +} + +do_execsql_test 2.1 { + SELECT * FROM t1 LEFT JOIN t2 ON (a=c) ORDER BY b; +} {1 2 1 one 2 3 {} {} 3 4 3 three} + + + +finish_test diff --git a/test/speed1.test b/test/speed1.test index 86d13ba4ae..0dec3ba83c 100644 --- a/test/speed1.test +++ b/test/speed1.test @@ -14,12 +14,14 @@ # $Id: speed1.test,v 1.11 2009/04/09 01:23:49 drh Exp $ # +catch {db close} sqlite3_shutdown #sqlite3_config_scratch 29000 1 set old_lookaside [sqlite3_config_lookaside 1000 300] #sqlite3_config_pagecache 1024 10000 set testdir [file dirname $argv0] source $testdir/tester.tcl +reset_db speed_trial_init speed1 # Set a uniform random seed diff --git a/test/speed1p.test b/test/speed1p.test index 6bf7b10e1f..fc05067e3c 100644 --- a/test/speed1p.test +++ b/test/speed1p.test @@ -16,12 +16,14 @@ # $Id: speed1p.test,v 1.7 2009/04/09 01:23:49 drh Exp $ # +catch { db close } sqlite3_shutdown #sqlite3_config_scratch 29000 1 set old_lookaside [sqlite3_config_lookaside 2048 300] #sqlite3_config_pagecache 1024 11000 set testdir [file dirname $argv0] source $testdir/tester.tcl +reset_db speed_trial_init speed1 sqlite3_memdebug_vfs_oom_test 0 diff --git a/test/speed3.test b/test/speed3.test index 1beaeb74f3..4aa90943ba 100644 --- a/test/speed3.test +++ b/test/speed3.test @@ -105,7 +105,7 @@ proc io_log {db} { puts "Normal : Read $stats2(read), wrote $stats2(write)" } -proc reset_db {} { +proc speed3_reset_db {} { db close sqlite3 db test.db db eval { @@ -117,7 +117,7 @@ proc reset_db {} { } forcedelete test2.db test2.db-journal -reset_db +speed3_reset_db # Set up a database in auto-vacuum mode and create a database schema. # @@ -154,7 +154,7 @@ do_test speed3-0.4 { # Delete all content in a table, one row at a time. # #io_log db -reset_db +speed3_reset_db speed_trial speed3-1.incrvacuum $::NROW row {DELETE FROM main.t1 WHERE 1} speed_trial speed3-1.normal $::NROW row {DELETE FROM aux.t1 WHERE 1} io_log db @@ -164,7 +164,7 @@ io_log db # #db eval {PRAGMA incremental_vacuum(500000)} populate_t1 db -reset_db +speed3_reset_db speed_trial speed3-2.incrvacuum $::NROW row {SELECT c FROM main.t1} speed_trial speed3-2.normal $::NROW row {SELECT c FROM aux.t1} io_log db diff --git a/test/speed4p.test b/test/speed4p.test index 024232e1b8..78ff9138db 100644 --- a/test/speed4p.test +++ b/test/speed4p.test @@ -168,7 +168,6 @@ speed_trial_tcl speed4p-subselect1 10000 stmt $script set script { db eval BEGIN for {set ii 1} {$ii < 10000} {incr ii} { - set v [expr {$ii*3}] db eval {UPDATE t1 SET i=i+1 WHERE rowid=$ii} } db eval COMMIT diff --git a/test/speedtest.md b/test/speedtest.md new file mode 100644 index 0000000000..135e562aed --- /dev/null +++ b/test/speedtest.md @@ -0,0 +1,53 @@ +# Performance And Size Measurements + +This document shows a procedure for making performance and size +comparisons between two versions of the SQLite Amalgamation "sqlite3.c". +You will need: + + * fossil + * valgrind + * tclsh + * A script or program named "open" that brings up *.txt files in an + editor for viewing. (Macs provide this by default. You'll need to + come up with your own on Linux and Windows.) + * An SQLite source tree + +The procedure described in this document is not the only way to make +performance and size measurements. Use this as a guide and make +adjustments as needed. + +## Establish the baseline measurement + + * Begin at the root the SQLite source tree + * <b>mkdir -p ../speed</b> <br> + &uarr; Speed measurement output files will go into this directory. + You can actually put those files wherever you want. This is just a + suggestion. It might be good to keep these files outside of the + source tree so that "fossil clean" does not delete them. + * Obtain the baseline SQLite amalgamation. For the purpose of this + technical note, assume the baseline SQLite sources are in files + "../baseline/sqlite3.c" and "../baseline/sqlite3.h". + * <b>test/speedtest.tcl ../baseline/sqlite3.c ../speed/baseline.txt</b> <br> + &uarr; The performance measure will be written into ../speed/baseline.txt + and that file will be brought up in an editor for easy viewing. <br> + &uarr; The "sqlite3.h" will be taken from the directory that contains + the "sqlite3.c" amalgamation file. + +## Comparing the current checkout against the baseline + + * <b>make sqlite3.c</b> + * <b>test/speedtest.tcl sqlite3.c ../speed/test.txt ../speed/baseline.txt</b> <br> + &uarr; Test results written into ../speed/test.txt and then + "fossil xdiff" is run to compare ../speed/baseline.txt against + the new test results. + +## When to do this + +Performance and size checks should be done prior to trunk check-ins. +Sometimes a seemingly innocuous change can have large performance +impacts. A large impact does not mean that the change cannot continue, +but it is important to be aware of the impact. + +## Additional hints + +Use the --help option to test/speedtest.tcl to see other available options. diff --git a/test/speedtest.tcl b/test/speedtest.tcl new file mode 100755 index 0000000000..9cb81c0fcd --- /dev/null +++ b/test/speedtest.tcl @@ -0,0 +1,313 @@ +#!/bin/sh +# the next line restarts using tclsh \ +exec tclsh "$0" ${1+"$@"} +# +# This program runs performance testing on sqlite3.c. Usage: +set usage {USAGE: + + speedtest.tcl sqlite3.c x1.txt trunk.txt -Os -DSQLITE_ENABLE_STAT4 + | | | `-----------------------' + File to test ----' | | | + | | `- options + Output filename --------' | + `--- optional prior output to diff + +Do a cache-grind performance analysis of the sqlite3.c file named and +write the results into the output file. The ".txt" is appended to the +output file (and diff-file) name if it is not already present. If the +diff-file is specified then show a diff from the diff-file to the new +output. + +Other options include: + CC=... Specify an alternative C compiler. Default is "gcc". + -D... -D and -O options are passed through to the C compiler. + --dryrun Show what would happen but don't do anything. + --help Show this help screen. + --lean "Lean" mode. + --lookaside N SZ Lookahead uses N slots of SZ bytes each. + --osmalloc Use the OS native malloc() instead of MEMSYS5 + --pagesize N Use N as the page size. + --quiet | -q "Quite". Put results in file but don't pop up editor + --size N Change the test size. 100 means 100%. Default: 5. + --testset TEST Specify the specific testset to use. The default + is "mix1". Other options include: "main", "json", + "cte", "orm", "fp", "rtree". +} +set srcfile {} +set outfile {} +set difffile {} +set cflags {-DSQLITE_OMIT_LOAD_EXTENSION -DSQLITE_THREADSAFE=0} +set cc gcc +set testset mix1 +set dryrun 0 +set quiet 0 +set osmalloc 0 +set speedtestflags {--shrink-memory --reprepare --stats} +lappend speedtestflags --journal wal --size 5 + +for {set i 0} {$i<[llength $argv]} {incr i} { + set arg [lindex $argv $i] + if {[string index $arg 0]=="-"} { + switch -- $arg { + -pagesize - + --pagesize { + lappend speedtestflags --pagesize + incr i + lappend speedtestflags [lindex $argv $i] + } + -lookaside - + --lookaside { + lappend speedtestflags --lookaside + incr i + lappend speedtestflags [lindex $argv $i] + incr i + lappend speedtestflags [lindex $argv $i] + } + -lean - + --lean { + lappend cflags \ + -DSQLITE_DEFAULT_MEMSTATUS=0 \ + -DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1 \ + -DSQLITE_LIKE_DOESNT_MATCH_BLOBS=1 \ + -DSQLITE_MAX_EXPR_DEPTH=1 \ + -DSQLITE_OMIT_DECLTYPE \ + -DSQLITE_OMIT_DEPRECATED \ + -DSQLITE_OMIT_PROGRESS_CALLBACK \ + -DSQLITE_OMIT_SHARED_CACHE \ + -DSQLITE_USE_ALLOCA + } + -testset - + --testset { + incr i + set testset [lindex $argv $i] + } + -size - + --size { + incr i + set newsize [lindex $argv $i] + if {$newsize<1} {set newsize 1} + set speedtestflags \ + [regsub {.-size \d+} $speedtestflags "-size $newsize"] + } + -n - + -dryrun - + --dryrun { + set dryrun 1 + } + -osmalloc - + --osmalloc { + set osmalloc 1 + } + -? - + -help - + --help { + puts $usage + exit 0 + } + -q - + -quiet - + --quiet { + set quiet 1 + } + default { + lappend cflags $arg + } + } + continue + } + if {[string match CC=* $arg]} { + set cc [string range $arg 3 end] + continue + } + if {[string match *.c $arg]} { + if {$srcfile!=""} { + puts stderr "multiple source files: $srcfile $arg" + exit 1 + } + set srcfile $arg + continue + } + if {[lsearch {main cte rtree orm fp json parsenumber mix1} $arg]>=0} { + set testset $arg + continue + } + if {$outfile==""} { + set outfile $arg + continue + } + if {$difffile==""} { + set difffile $arg + continue + } + puts stderr "unknown option: \"$arg\". Use --help for more info." + exit 1 +} +if {[lsearch -glob $cflags -O*]<0} { + lappend cflags -Os +} +if {!$osmalloc} { + append speedtestflags { --heap 40000000 64} +} +if {!$osmalloc && [lsearch -glob $cflags {-DSQLITE_ENABLE_MEMSYS*}]<0} { + lappend cflags -DSQLITE_ENABLE_MEMSYS5 +} +if {[lsearch -glob $cflags {-DSQLITE_ENABLE_RTREE*}]<0} { + lappend cflags -DSQLITE_ENABLE_RTREE +} +if {$srcfile==""} { + puts stderr "no sqlite3.c source file specified" + exit 1 +} +if {![file readable $srcfile]} { + puts stderr "source file \"$srcfile\" does not exist" + exit 1 +} +if {$outfile==""} { + puts stderr "no output file specified" + exit 1 +} +if {![string match *.* [file tail $outfile]]} { + append outfile .txt +} +if {$difffile!=""} { + if {![file exists $difffile]} { + if {[file exists $difffile.txt]} { + append difffile .txt + } else { + puts stderr "No such file: \"$difffile\"" + exit 1 + } + } +} + +set cccmd [list $cc -g] +lappend cccmd -I[file dir $srcfile] +lappend cccmd {*}[lsort $cflags] +lappend cccmd [file dir $argv0]/speedtest1.c +lappend cccmd $srcfile +lappend cccmd -o speedtest1 +puts $cccmd +if {!$dryrun} { + exec {*}$cccmd +} +lappend speedtestflags --testset $testset +set stcmd [list valgrind --tool=cachegrind ./speedtest1 {*}$speedtestflags] +lappend stcmd speedtest1.db +lappend stcmd >valgrind-out.txt 2>valgrind-err.txt +puts $stcmd +if {!$dryrun} { + foreach file {speedtest1.db speedtest1.db-journal speedtest1.db-wal + speedtest1.db-shm} { + if {[file exists $file]} {file delete $file} + } + exec {*}$stcmd +} + +set maxmtime 0 +set cgfile {} +foreach cgout [glob -nocomplain cachegrind.out.*] { + if {[file mtime $cgout]>$maxmtime} { + set cgfile $cgout + set maxmtime [file mtime $cgfile] + } +} +if {$cgfile==""} { + puts "no cachegrind output" + exit 1 +} + +############# Process the cachegrind.out.# file ########################## +set fd [open $outfile wb] +set in [open "|cg_annotate --show=Ir --auto=yes --context=40 $cgfile" r] +set dest ! +set out(!) {} +set linenum 0 +set cntlines 0 ;# true to remember cycle counts on each line +set seenSqlite3 0 ;# true if we have seen the sqlite3.c file +while {![eof $in]} { + set line [string map {\t { }} [gets $in]] + if {[regexp {^-- Auto-annotated source: (.*)} $line all name]} { + set dest $name + if {[string match */sqlite3.c $dest]} { + set cntlines 1 + set seenSqlite3 1 + } else { + set cntlines 0 + } + } elseif {[regexp {^-- line (\d+) ------} $line all ln]} { + set line [lreplace $line 2 2 {#}] + set linenum [expr {$ln-1}] + } elseif {[regexp {^The following files chosen for } $line]} { + set dest ! + } + append out($dest) $line\n + if {$cntlines} { + incr linenum + if {[regexp {^ *([0-9,]+) } $line all x]} { + set x [string map {, {}} $x] + set cycles($linenum) $x + } + } +} +foreach x [lsort [array names out]] { + puts $fd $out($x) +} +# If the sqlite3.c file has been seen, then output a summary of the +# cycle counts for each file that went into making up sqlite3.c +# +if {$seenSqlite3} { + close $in + set in [open sqlite3.c] + set linenum 0 + set fn sqlite3.c + set pattern1 {^/\*+ Begin file ([^ ]+) \*} + set pattern2 {^/\*+ Continuing where we left off in ([^ ]+) \*} + while {![eof $in]} { + set line [gets $in] + incr linenum + if {[regexp $pattern1 $line all newfn]} { + set fn $newfn + } elseif {[regexp $pattern2 $line all newfn]} { + set fn $newfn + } elseif {[info exists cycles($linenum)]} { + incr fcycles($fn) $cycles($linenum) + } + } + close $in + puts $fd \ + {**********************************************************************} + set lx {} + set sum 0 + foreach {fn cnt} [array get fcycles] { + lappend lx [list $cnt $fn] + incr sum $cnt + } + puts $fd [format {%20s %14d %8.3f%%} TOTAL $sum 100] + foreach entry [lsort -index 0 -integer -decreasing $lx] { + foreach {cnt fn} $entry break + puts $fd [format {%20s %14d %8.3f%%} $fn $cnt [expr {$cnt*100.0/$sum}]] + } +} +puts $fd "Executable size:" +close $fd +exec size speedtest1 >>$outfile +# +# Processed cachegrind output should now be in the $outfile +############################################################################# + +if {$quiet} { + # Skip this last part of popping up a GUI viewer +} elseif {$difffile!=""} { + set fossilcmd {fossil xdiff --tk -c 20} + lappend fossilcmd $difffile + lappend fossilcmd $outfile + lappend fossilcmd & + puts $fossilcmd + if {!$dryrun} { + exec {*}$fossilcmd + } +} else { + if {!$dryrun} { + exec open $outfile + } +} diff --git a/test/speedtest1.c b/test/speedtest1.c index b41c732053..a127f62e9f 100644 --- a/test/speedtest1.c +++ b/test/speedtest1.c @@ -1,45 +1,83 @@ /* ** A program for performance testing. ** +** To build this program against an historical version of SQLite for comparison +** testing: +** +** Unix: +** +** ./configure --all +** make clean speedtest1 +** mv speedtest1 speedtest1-current +** cp $HISTORICAL_SQLITE3_C_H . +** touch sqlite3.c sqlite3.h .target_source +** make speedtest1 +** mv speedtest1 speedtest1-baseline +** +** Windows: +** +** nmake /f Makefile.msc clean speedtest1.exe +** mv speedtest1.exe speedtest1-current.exe +** cp $HISTORICAL_SQLITE_C_H . +** touch sqlite3.c sqlite3.h .target_source +** nmake /f Makefile.msc speedtest1.exe +** mv speedtest1.exe speedtest1-baseline.exe +** ** The available command-line options are described below: */ static const char zHelp[] = "Usage: %s [--options] DATABASE\n" "Options:\n" " --autovacuum Enable AUTOVACUUM mode\n" - " --cachesize N Set the cache size to N\n" + " --big-transactions Add BEGIN/END around all large tests\n" + " --cachesize N Set PRAGMA cache_size=N. Note: N is pages, not bytes\n" + " --checkpoint Run PRAGMA wal_checkpoint after each test case\n" " --exclusive Enable locking_mode=EXCLUSIVE\n" " --explain Like --sqlonly but with added EXPLAIN keywords\n" + " --fullfsync Enable fullfsync=TRUE\n" + " --hard-heap-limit N The hard limit on the maximum heap size\n" " --heap SZ MIN Memory allocator uses SZ bytes & min allocation MIN\n" " --incrvacuum Enable incremenatal vacuum mode\n" " --journal M Set the journal_mode to M\n" " --key KEY Set the encryption key to KEY\n" " --lookaside N SZ Configure lookaside for N slots of SZ bytes each\n" + " --memdb Use an in-memory database\n" + " --mmap SZ MMAP the first SZ bytes of the database file\n" " --multithread Set multithreaded mode\n" " --nomemstat Disable memory statistics\n" + " --nomutex Open db with SQLITE_OPEN_NOMUTEX\n" " --nosync Set PRAGMA synchronous=OFF\n" " --notnull Add NOT NULL constraints to table columns\n" + " --output FILE Store SQL output in FILE\n" " --pagesize N Set the page size to N\n" " --pcache N SZ Configure N pages of pagecache each of size SZ bytes\n" " --primarykey Use PRIMARY KEY instead of UNIQUE where appropriate\n" + " --repeat N Repeat each SELECT N times (default: 1)\n" " --reprepare Reprepare each statement upon every invocation\n" - " --scratch N SZ Configure scratch memory for N slots of SZ bytes each\n" + " --reserve N Reserve N bytes on each database page\n" + " --script FILE Write an SQL script for the test into FILE\n" " --serialized Set serialized threading mode\n" " --singlethread Set single-threaded mode - disables all mutexing\n" " --sqlonly No-op. Only show the SQL that would have been run.\n" " --shrink-memory Invoke sqlite3_db_release_memory() frequently.\n" " --size N Relative test size. Default=100\n" + " --soft-heap-limit N The soft limit on the maximum heap size\n" + " --strict Use STRICT table where appropriate\n" " --stats Show statistics at the end\n" - " --testset T Run test-set T\n" + " --stmtscanstatus Activate SQLITE_DBCONFIG_STMT_SCANSTATUS\n" + " --temp N N from 0 to 9. 0: no temp table. 9: all temp tables\n" + " --testset T Run test-set T (main, cte, rtree, orm, fp, json,\n" + " star, app, debug). Can be a comma-separated list\n" + " of values, with /SCALE suffixes or macro \"mix1\"\n" " --trace Turn on SQL tracing\n" " --threads N Use up to N threads for sorting\n" " --utf16be Set text encoding to UTF-16BE\n" " --utf16le Set text encoding to UTF-16LE\n" - " --verify Run additional verification steps.\n" + " --verify Run additional verification steps\n" + " --vfs NAME Use the given (preinstalled) VFS\n" " --without-rowid Use WITHOUT ROWID where appropriate\n" ; - #include "sqlite3.h" #include <assert.h> #include <stdio.h> @@ -47,19 +85,37 @@ static const char zHelp[] = #include <stdarg.h> #include <string.h> #include <ctype.h> +#ifndef _WIN32 +# include <unistd.h> +#else +# include <io.h> +#endif #define ISSPACE(X) isspace((unsigned char)(X)) #define ISDIGIT(X) isdigit((unsigned char)(X)) #if SQLITE_VERSION_NUMBER<3005000 # define sqlite3_int64 sqlite_int64 #endif -#ifdef SQLITE_ENABLE_RBU -# include "sqlite3rbu.h" -#endif + +typedef sqlite3_uint64 u64; + +/* +** State structure for a Hash hash in progress +*/ +typedef struct HashContext HashContext; +struct HashContext { + unsigned char isInit; /* True if initialized */ + unsigned char i, j; /* State variables */ + unsigned char s[256]; /* State variables */ + unsigned char r[32]; /* Result */ +}; + /* All global state is held in this structure */ static struct Global { sqlite3 *db; /* The open database connection */ + const char *zDbName; /* Name of the database file */ + const char *zVfs; /* --vfs NAME */ sqlite3_stmt *pStmt; /* Current SQL statement */ sqlite3_int64 iStart; /* Start-time for the current test */ sqlite3_int64 iTotal; /* Total time */ @@ -69,15 +125,33 @@ static struct Global { int bExplain; /* Print SQL with EXPLAIN prefix */ int bVerify; /* Try to verify that results are correct */ int bMemShrink; /* Call sqlite3_db_release_memory() often */ + int eTemp; /* 0: no TEMP. 9: always TEMP. */ int szTest; /* Scale factor for test iterations */ + int szBase; /* Base size prior to testset scaling */ + int nRepeat; /* Repeat selects this many times */ + int doCheckpoint; /* Run PRAGMA wal_checkpoint after each trans */ + int nReserve; /* Reserve bytes */ + int stmtScanStatus; /* True to activate Stmt ScanStatus reporting */ + int doBigTransactions; /* Enable transactions on tests 410 and 510 */ const char *zWR; /* Might be WITHOUT ROWID */ const char *zNN; /* Might be NOT NULL */ const char *zPK; /* Might be UNIQUE or PRIMARY KEY */ unsigned int x, y; /* Pseudo-random number generator state */ + u64 nResByte; /* Total number of result bytes */ int nResult; /* Size of the current result */ char zResult[3000]; /* Text of the current result */ + FILE *pScript; /* Write an SQL script into this file */ +#ifndef SPEEDTEST_OMIT_HASH + FILE *hashFile; /* Store all hash results in this file */ + HashContext hash; /* Hash of all output */ +#endif } g; +/* Return " TEMP" or "", as appropriate for creating a table. +*/ +static const char *isTemp(int N){ + return g.eTemp>=N ? " TEMP" : ""; +} /* Print an error message and exit */ static void fatal_error(const char *zMsg, ...){ @@ -85,9 +159,81 @@ static void fatal_error(const char *zMsg, ...){ va_start(ap, zMsg); vfprintf(stderr, zMsg, ap); va_end(ap); +#ifdef SQLITE_SPEEDTEST1_WASM + /* Emscripten complains when exit() is called and anything is left + in the I/O buffers. */ + fflush(stdout); + fflush(stderr); +#endif exit(1); } +#ifndef SPEEDTEST_OMIT_HASH +/**************************************************************************** +** Hash algorithm used to verify that compilation is not miscompiled +** in such a was as to generate an incorrect result. +*/ + +/* +** Initialize a new hash. iSize determines the size of the hash +** in bits and should be one of 224, 256, 384, or 512. Or iSize +** can be zero to use the default hash size of 256 bits. +*/ +static void HashInit(void){ + unsigned int k; + g.hash.i = 0; + g.hash.j = 0; + for(k=0; k<256; k++) g.hash.s[k] = k; +} + +/* +** Make consecutive calls to the HashUpdate function to add new content +** to the hash +*/ +static void HashUpdate( + const unsigned char *aData, + unsigned int nData +){ + unsigned char t; + unsigned char i = g.hash.i; + unsigned char j = g.hash.j; + unsigned int k; + if( g.hashFile ) fwrite(aData, 1, nData, g.hashFile); + for(k=0; k<nData; k++){ + j += g.hash.s[i] + aData[k]; + t = g.hash.s[j]; + g.hash.s[j] = g.hash.s[i]; + g.hash.s[i] = t; + i++; + } + g.hash.i = i; + g.hash.j = j; +} + +/* +** After all content has been added, invoke HashFinal() to compute +** the final hash. The hash result is stored in g.hash.r[]. +*/ +static void HashFinal(void){ + unsigned int k; + unsigned char t, i, j; + i = g.hash.i; + j = g.hash.j; + for(k=0; k<32; k++){ + i++; + t = g.hash.s[i]; + j += t; + g.hash.s[i] = g.hash.s[j]; + g.hash.s[j] = t; + t += g.hash.s[i]; + g.hash.r[k] = g.hash.s[t]; + } +} + +/* End of the Hash hashing logic +*****************************************************************************/ +#endif /* SPEEDTEST_OMIT_HASH */ + /* ** Return the value of a hexadecimal digit. Return -1 if the input ** is not a hex digit. @@ -268,10 +414,12 @@ int speedtest1_numbername(unsigned int n, char *zOut, int nOut){ #define NAMEWIDTH 60 static const char zDots[] = "......................................................................."; +static int iTestNumber = 0; /* Current test # for begin/end_test(). */ void speedtest1_begin_test(int iTestNum, const char *zTestName, ...){ int n = (int)strlen(zTestName); char *zName; va_list ap; + iTestNumber = iTestNum; va_start(ap, zTestName); zName = sqlite3_vmprintf(zTestName, ap); va_end(ap); @@ -280,6 +428,11 @@ void speedtest1_begin_test(int iTestNum, const char *zTestName, ...){ zName[NAMEWIDTH] = 0; n = NAMEWIDTH; } + if( g.pScript ){ + fprintf(g.pScript,"-- begin test %d %.*s\n", iTestNumber, n, zName) + /* maintenance reminder: ^^^ code in ext/wasm expects %d to be + ** field #4 (as in: cut -d' ' -f4). */; + } if( g.bSqlOnly ){ printf("/* %4d - %s%.*s */\n", iTestNum, zName, NAMEWIDTH-n, zDots); }else{ @@ -293,9 +446,17 @@ void speedtest1_begin_test(int iTestNum, const char *zTestName, ...){ g.y = 0x44f9eac8; } +/* Forward reference */ +void speedtest1_exec(const char*,...); + /* Complete a test case */ void speedtest1_end_test(void){ sqlite3_int64 iElapseTime = speedtest1_timestamp() - g.iStart; + if( g.doCheckpoint ) speedtest1_exec("PRAGMA wal_checkpoint;"); + assert( iTestNumber > 0 ); + if( g.pScript ){ + fprintf(g.pScript,"-- end test %d\n", iTestNumber); + } if( !g.bSqlOnly ){ g.iTotal += iElapseTime; printf("%4d.%03ds\n", (int)(iElapseTime/1000), (int)(iElapseTime%1000)); @@ -304,6 +465,7 @@ void speedtest1_end_test(void){ sqlite3_finalize(g.pStmt); g.pStmt = 0; } + iTestNumber = 0; } /* Report end of testing */ @@ -312,6 +474,21 @@ void speedtest1_final(void){ printf(" TOTAL%.*s %4d.%03ds\n", NAMEWIDTH-5, zDots, (int)(g.iTotal/1000), (int)(g.iTotal%1000)); } + if( g.bVerify ){ +#ifndef SPEEDTEST_OMIT_HASH + int i; +#endif + printf("Verification Hash: %llu ", g.nResByte); +#ifndef SPEEDTEST_OMIT_HASH + HashUpdate((const unsigned char*)"\n", 1); + HashFinal(); + for(i=0; i<24; i++){ + printf("%02x", g.hash.r[i]); + } + if( g.hashFile && g.hashFile!=stdout ) fclose(g.hashFile); +#endif + printf("\n"); + } } /* Print an SQL statement to standard output */ @@ -352,7 +529,11 @@ void speedtest1_exec(const char *zFormat, ...){ printSql(zSql); }else{ char *zErrMsg = 0; - int rc = sqlite3_exec(g.db, zSql, 0, 0, &zErrMsg); + int rc; + if( g.pScript ){ + fprintf(g.pScript,"%s;\n",zSql); + } + rc = sqlite3_exec(g.db, zSql, 0, 0, &zErrMsg); if( zErrMsg ) fatal_error("SQL error: %s\n%s\n", zErrMsg, zSql); if( rc!=SQLITE_OK ) fatal_error("exec error: %s\n", sqlite3_errmsg(g.db)); } @@ -360,6 +541,47 @@ void speedtest1_exec(const char *zFormat, ...){ speedtest1_shrink_memory(); } +/* Run SQL and return the first column of the first row as a string. The +** returned string is obtained from sqlite_malloc() and must be freed by +** the caller. +*/ +char *speedtest1_once(const char *zFormat, ...){ + va_list ap; + char *zSql; + sqlite3_stmt *pStmt; + char *zResult = 0; + int rc; + va_start(ap, zFormat); + zSql = sqlite3_vmprintf(zFormat, ap); + va_end(ap); + if( g.bSqlOnly ){ + printSql(zSql); + }else{ + int rc = sqlite3_prepare_v2(g.db, zSql, -1, &pStmt, 0); + if( rc ){ + fatal_error("SQL error: %s\n", sqlite3_errmsg(g.db)); + } + if( g.pScript ){ + char *z = sqlite3_expanded_sql(pStmt); + fprintf(g.pScript,"%s\n",z); + sqlite3_free(z); + } + if( sqlite3_step(pStmt)==SQLITE_ROW ){ + const char *z = (const char*)sqlite3_column_text(pStmt, 0); + if( z ) zResult = sqlite3_mprintf("%s", z); + } + rc = sqlite3_reset(pStmt); + if( rc!=SQLITE_OK ){ + fatal_error("%s\nError code %d: %s\n", + sqlite3_sql(pStmt), rc, sqlite3_errmsg(g.db)); + } + sqlite3_finalize(pStmt); + } + sqlite3_free(zSql); + speedtest1_shrink_memory(); + return zResult; +} + /* Prepare an SQL statement */ void speedtest1_prepare(const char *zFormat, ...){ va_list ap; @@ -382,16 +604,54 @@ void speedtest1_prepare(const char *zFormat, ...){ /* Run an SQL statement previously prepared */ void speedtest1_run(void){ - int i, n, len; + int i, n, len, rc; if( g.bSqlOnly ) return; assert( g.pStmt ); g.nResult = 0; + if( g.pScript ){ + char *z = sqlite3_expanded_sql(g.pStmt); + fprintf(g.pScript,"%s\n",z); + sqlite3_free(z); + } while( sqlite3_step(g.pStmt)==SQLITE_ROW ){ n = sqlite3_column_count(g.pStmt); for(i=0; i<n; i++){ const char *z = (const char*)sqlite3_column_text(g.pStmt, i); if( z==0 ) z = "nil"; len = (int)strlen(z); +#ifndef SPEEDTEST_OMIT_HASH + if( g.bVerify ){ + int eType = sqlite3_column_type(g.pStmt, i); + unsigned char zPrefix[2]; + zPrefix[0] = '\n'; + zPrefix[1] = "-IFTBN"[eType]; + if( g.nResByte ){ + HashUpdate(zPrefix, 2); + }else{ + HashUpdate(zPrefix+1, 1); + } + if( eType==SQLITE_FLOAT ){ + /* Omit the value of floating-point results from the verification + ** hash. The only thing we record is the fact that the result was + ** a floating-point value. */ + g.nResByte += 2; + }else if( eType==SQLITE_BLOB ){ + int nBlob = sqlite3_column_bytes(g.pStmt, i); + int iBlob; + unsigned char zChar[2]; + const unsigned char *aBlob = sqlite3_column_blob(g.pStmt, i); + for(iBlob=0; iBlob<nBlob; iBlob++){ + zChar[0] = "0123456789abcdef"[aBlob[iBlob]>>4]; + zChar[1] = "0123456789abcdef"[aBlob[iBlob]&15]; + HashUpdate(zChar,2); + } + g.nResByte += nBlob*2 + 2; + }else{ + HashUpdate((unsigned char*)z, len); + g.nResByte += len + 2; + } + } +#endif if( g.nResult+len<sizeof(g.zResult)-2 ){ if( g.nResult>0 ) g.zResult[g.nResult++] = ' '; memcpy(g.zResult + g.nResult, z, len+1); @@ -403,22 +663,32 @@ void speedtest1_run(void){ if( g.bReprepare ){ sqlite3_stmt *pNew; sqlite3_prepare_v2(g.db, sqlite3_sql(g.pStmt), -1, &pNew, 0); - sqlite3_finalize(g.pStmt); + rc = sqlite3_finalize(g.pStmt); + if( rc!=SQLITE_OK ){ + fatal_error("%s\nError code %d: %s\n", + sqlite3_sql(pNew), rc, sqlite3_errmsg(g.db)); + } g.pStmt = pNew; }else #endif { - sqlite3_reset(g.pStmt); + rc = sqlite3_reset(g.pStmt); + if( rc!=SQLITE_OK ){ + fatal_error("%s\nError code %d: %s\n", + sqlite3_sql(g.pStmt), rc, sqlite3_errmsg(g.db)); + } } speedtest1_shrink_memory(); } +#ifndef SQLITE_OMIT_DEPRECATED /* The sqlite3_trace() callback function */ static void traceCallback(void *NotUsed, const char *zSql){ int n = (int)strlen(zSql); while( n>0 && (zSql[n-1]==';' || ISSPACE(zSql[n-1])) ) n--; fprintf(stderr,"%.*s;\n", n, zSql); } +#endif /* SQLITE_OMIT_DEPRECATED */ /* Substitute random() function that gives the same random ** sequence on each run, for repeatability. */ @@ -443,6 +713,68 @@ static int est_square_root(int x){ return y0; } + +#if SQLITE_VERSION_NUMBER<3005004 +/* +** An implementation of group_concat(). Used only when testing older +** versions of SQLite that lack the built-in group_concat(). +*/ +struct groupConcat { + char *z; + int nAlloc; + int nUsed; +}; +static void groupAppend(struct groupConcat *p, const char *z, int n){ + if( p->nUsed+n >= p->nAlloc ){ + int n2 = (p->nAlloc+n+1)*2; + char *z2 = sqlite3_realloc(p->z, n2); + if( z2==0 ) return; + p->z = z2; + p->nAlloc = n2; + } + memcpy(p->z+p->nUsed, z, n); + p->nUsed += n; +} +static void groupStep( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + const char *zVal; + struct groupConcat *p; + const char *zSep; + int nVal, nSep; + assert( argc==1 || argc==2 ); + if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; + p= (struct groupConcat*)sqlite3_aggregate_context(context, sizeof(*p)); + + if( p ){ + int firstTerm = p->nUsed==0; + if( !firstTerm ){ + if( argc==2 ){ + zSep = (char*)sqlite3_value_text(argv[1]); + nSep = sqlite3_value_bytes(argv[1]); + }else{ + zSep = ","; + nSep = 1; + } + if( nSep ) groupAppend(p, zSep, nSep); + } + zVal = (char*)sqlite3_value_text(argv[0]); + nVal = sqlite3_value_bytes(argv[0]); + if( zVal ) groupAppend(p, zVal, nVal); + } +} +static void groupFinal(sqlite3_context *context){ + struct groupConcat *p; + p = sqlite3_aggregate_context(context, 0); + if( p && p->z ){ + p->z[p->nUsed] = 0; + sqlite3_result_text(context, p->z, p->nUsed, sqlite3_free); + } +} +#endif + /* ** The main and default testset */ @@ -451,17 +783,18 @@ void testset_main(void){ int n; /* iteration count */ int sz; /* Size of the tables */ int maxb; /* Maximum swizzled value */ - unsigned x1, x2; /* Parameters */ - int len; /* Length of the zNum[] string */ + unsigned x1 = 0, x2 = 0; /* Parameters */ + int len = 0; /* Length of the zNum[] string */ char zNum[2000]; /* A number name */ sz = n = g.szTest*500; + zNum[0] = 0; maxb = roundup_allones(sz); speedtest1_begin_test(100, "%d INSERTs into table with no index", n); speedtest1_exec("BEGIN"); - speedtest1_exec("CREATE TABLE t1(a INTEGER %s, b INTEGER %s, c TEXT %s);", - g.zNN, g.zNN, g.zNN); - speedtest1_prepare("INSERT INTO t1 VALUES(?1,?2,?3); -- %d times", n); + speedtest1_exec("CREATE%s TABLE z1(a INTEGER %s, b INTEGER %s, c TEXT %s);", + isTemp(9), g.zNN, g.zNN, g.zNN); + speedtest1_prepare("INSERT INTO z1 VALUES(?1,?2,?3); -- %d times", n); for(i=1; i<=n; i++){ x1 = swizzle(i,maxb); speedtest1_numbername(x1, zNum, sizeof(zNum)); @@ -477,9 +810,10 @@ void testset_main(void){ n = sz; speedtest1_begin_test(110, "%d ordered INSERTS with one index/PK", n); speedtest1_exec("BEGIN"); - speedtest1_exec("CREATE TABLE t2(a INTEGER %s %s, b INTEGER %s, c TEXT %s) %s", - g.zNN, g.zPK, g.zNN, g.zNN, g.zWR); - speedtest1_prepare("INSERT INTO t2 VALUES(?1,?2,?3); -- %d times", n); + speedtest1_exec( + "CREATE%s TABLE z2(a INTEGER %s %s, b INTEGER %s, c TEXT %s) %s", + isTemp(5), g.zNN, g.zPK, g.zNN, g.zNN, g.zWR); + speedtest1_prepare("INSERT INTO z2 VALUES(?1,?2,?3); -- %d times", n); for(i=1; i<=n; i++){ x1 = swizzle(i,maxb); speedtest1_numbername(x1, zNum, sizeof(zNum)); @@ -495,8 +829,9 @@ void testset_main(void){ n = sz; speedtest1_begin_test(120, "%d unordered INSERTS with one index/PK", n); speedtest1_exec("BEGIN"); - speedtest1_exec("CREATE TABLE t3(a INTEGER %s %s, b INTEGER %s, c TEXT %s) %s", - g.zNN, g.zPK, g.zNN, g.zNN, g.zWR); + speedtest1_exec( + "CREATE%s TABLE t3(a INTEGER %s %s, b INTEGER %s, c TEXT %s) %s", + isTemp(3), g.zNN, g.zPK, g.zNN, g.zNN, g.zWR); speedtest1_prepare("INSERT INTO t3 VALUES(?1,?2,?3); -- %d times", n); for(i=1; i<=n; i++){ x1 = swizzle(i,maxb); @@ -509,17 +844,23 @@ void testset_main(void){ speedtest1_exec("COMMIT"); speedtest1_end_test(); +#if SQLITE_VERSION_NUMBER<3005004 + sqlite3_create_function(g.db, "group_concat", 1, SQLITE_UTF8, 0, + 0, groupStep, groupFinal); +#endif n = 25; speedtest1_begin_test(130, "%d SELECTS, numeric BETWEEN, unindexed", n); speedtest1_exec("BEGIN"); speedtest1_prepare( - "SELECT count(*), avg(b), sum(length(c)) FROM t1\n" + "SELECT count(*), avg(b), sum(length(c)), group_concat(c) FROM z1\n" " WHERE b BETWEEN ?1 AND ?2; -- %d times", n ); for(i=1; i<=n; i++){ - x1 = speedtest1_random()%maxb; - x2 = speedtest1_random()%10 + sz/5000 + x1; + if( (i-1)%g.nRepeat==0 ){ + x1 = speedtest1_random()%maxb; + x2 = speedtest1_random()%10 + sz/5000 + x1; + } sqlite3_bind_int(g.pStmt, 1, x1); sqlite3_bind_int(g.pStmt, 2, x2); speedtest1_run(); @@ -532,16 +873,18 @@ void testset_main(void){ speedtest1_begin_test(140, "%d SELECTS, LIKE, unindexed", n); speedtest1_exec("BEGIN"); speedtest1_prepare( - "SELECT count(*), avg(b), sum(length(c)) FROM t1\n" + "SELECT count(*), avg(b), sum(length(c)), group_concat(c) FROM z1\n" " WHERE c LIKE ?1; -- %d times", n ); for(i=1; i<=n; i++){ - x1 = speedtest1_random()%maxb; - zNum[0] = '%'; - len = speedtest1_numbername(i, zNum+1, sizeof(zNum)-2); - zNum[len] = '%'; - zNum[len+1] = 0; - sqlite3_bind_text(g.pStmt, 1, zNum, len, SQLITE_STATIC); + if( (i-1)%g.nRepeat==0 ){ + x1 = speedtest1_random()%maxb; + zNum[0] = '%'; + len = speedtest1_numbername(i, zNum+1, sizeof(zNum)-2); + zNum[len] = '%'; + zNum[len+1] = 0; + } + sqlite3_bind_text(g.pStmt, 1, zNum, len+1, SQLITE_STATIC); speedtest1_run(); } speedtest1_exec("COMMIT"); @@ -552,16 +895,18 @@ void testset_main(void){ speedtest1_begin_test(142, "%d SELECTS w/ORDER BY, unindexed", n); speedtest1_exec("BEGIN"); speedtest1_prepare( - "SELECT a, b, c FROM t1 WHERE c LIKE ?1\n" + "SELECT a, b, c FROM z1 WHERE c LIKE ?1\n" " ORDER BY a; -- %d times", n ); for(i=1; i<=n; i++){ - x1 = speedtest1_random()%maxb; - zNum[0] = '%'; - len = speedtest1_numbername(i, zNum+1, sizeof(zNum)-2); - zNum[len] = '%'; - zNum[len+1] = 0; - sqlite3_bind_text(g.pStmt, 1, zNum, len, SQLITE_STATIC); + if( (i-1)%g.nRepeat==0 ){ + x1 = speedtest1_random()%maxb; + zNum[0] = '%'; + len = speedtest1_numbername(i, zNum+1, sizeof(zNum)-2); + zNum[len] = '%'; + zNum[len+1] = 0; + } + sqlite3_bind_text(g.pStmt, 1, zNum, len+1, SQLITE_STATIC); speedtest1_run(); } speedtest1_exec("COMMIT"); @@ -571,16 +916,18 @@ void testset_main(void){ speedtest1_begin_test(145, "%d SELECTS w/ORDER BY and LIMIT, unindexed", n); speedtest1_exec("BEGIN"); speedtest1_prepare( - "SELECT a, b, c FROM t1 WHERE c LIKE ?1\n" + "SELECT a, b, c FROM z1 WHERE c LIKE ?1\n" " ORDER BY a LIMIT 10; -- %d times", n ); for(i=1; i<=n; i++){ - x1 = speedtest1_random()%maxb; - zNum[0] = '%'; - len = speedtest1_numbername(i, zNum+1, sizeof(zNum)-2); - zNum[len] = '%'; - zNum[len+1] = 0; - sqlite3_bind_text(g.pStmt, 1, zNum, len, SQLITE_STATIC); + if( (i-1)%g.nRepeat==0 ){ + x1 = speedtest1_random()%maxb; + zNum[0] = '%'; + len = speedtest1_numbername(i, zNum+1, sizeof(zNum)-2); + zNum[len] = '%'; + zNum[len+1] = 0; + } + sqlite3_bind_text(g.pStmt, 1, zNum, len+1, SQLITE_STATIC); speedtest1_run(); } speedtest1_exec("COMMIT"); @@ -589,10 +936,10 @@ void testset_main(void){ speedtest1_begin_test(150, "CREATE INDEX five times"); speedtest1_exec("BEGIN;"); - speedtest1_exec("CREATE UNIQUE INDEX t1b ON t1(b);"); - speedtest1_exec("CREATE INDEX t1c ON t1(c);"); - speedtest1_exec("CREATE UNIQUE INDEX t2b ON t2(b);"); - speedtest1_exec("CREATE INDEX t2c ON t2(c DESC);"); + speedtest1_exec("CREATE UNIQUE INDEX t1b ON z1(b);"); + speedtest1_exec("CREATE INDEX t1c ON z1(c);"); + speedtest1_exec("CREATE UNIQUE INDEX t2b ON z2(b);"); + speedtest1_exec("CREATE INDEX t2c ON z2(c DESC);"); speedtest1_exec("CREATE INDEX t3bc ON t3(b,c);"); speedtest1_exec("COMMIT;"); speedtest1_end_test(); @@ -602,12 +949,14 @@ void testset_main(void){ speedtest1_begin_test(160, "%d SELECTS, numeric BETWEEN, indexed", n); speedtest1_exec("BEGIN"); speedtest1_prepare( - "SELECT count(*), avg(b), sum(length(c)) FROM t1\n" + "SELECT count(*), avg(b), sum(length(c)), group_concat(a) FROM z1\n" " WHERE b BETWEEN ?1 AND ?2; -- %d times", n ); for(i=1; i<=n; i++){ - x1 = speedtest1_random()%maxb; - x2 = speedtest1_random()%10 + sz/5000 + x1; + if( (i-1)%g.nRepeat==0 ){ + x1 = speedtest1_random()%maxb; + x2 = speedtest1_random()%10 + sz/5000 + x1; + } sqlite3_bind_int(g.pStmt, 1, x1); sqlite3_bind_int(g.pStmt, 2, x2); speedtest1_run(); @@ -620,12 +969,14 @@ void testset_main(void){ speedtest1_begin_test(161, "%d SELECTS, numeric BETWEEN, PK", n); speedtest1_exec("BEGIN"); speedtest1_prepare( - "SELECT count(*), avg(b), sum(length(c)) FROM t2\n" + "SELECT count(*), avg(b), sum(length(c)), group_concat(a) FROM z2\n" " WHERE a BETWEEN ?1 AND ?2; -- %d times", n ); for(i=1; i<=n; i++){ - x1 = speedtest1_random()%maxb; - x2 = speedtest1_random()%10 + sz/5000 + x1; + if( (i-1)%g.nRepeat==0 ){ + x1 = speedtest1_random()%maxb; + x2 = speedtest1_random()%10 + sz/5000 + x1; + } sqlite3_bind_int(g.pStmt, 1, x1); sqlite3_bind_int(g.pStmt, 2, x2); speedtest1_run(); @@ -638,12 +989,14 @@ void testset_main(void){ speedtest1_begin_test(170, "%d SELECTS, text BETWEEN, indexed", n); speedtest1_exec("BEGIN"); speedtest1_prepare( - "SELECT count(*), avg(b), sum(length(c)) FROM t1\n" + "SELECT count(*), avg(b), sum(length(c)), group_concat(a) FROM z1\n" " WHERE c BETWEEN ?1 AND (?1||'~'); -- %d times", n ); for(i=1; i<=n; i++){ - x1 = swizzle(i, maxb); - len = speedtest1_numbername(x1, zNum, sizeof(zNum)-1); + if( (i-1)%g.nRepeat==0 ){ + x1 = swizzle(i, maxb); + len = speedtest1_numbername(x1, zNum, sizeof(zNum)-1); + } sqlite3_bind_text(g.pStmt, 1, zNum, len, SQLITE_STATIC); speedtest1_run(); } @@ -654,22 +1007,22 @@ void testset_main(void){ speedtest1_begin_test(180, "%d INSERTS with three indexes", n); speedtest1_exec("BEGIN"); speedtest1_exec( - "CREATE TABLE t4(\n" + "CREATE%s TABLE t4(\n" " a INTEGER %s %s,\n" " b INTEGER %s,\n" " c TEXT %s\n" ") %s", - g.zNN, g.zPK, g.zNN, g.zNN, g.zWR); + isTemp(1), g.zNN, g.zPK, g.zNN, g.zNN, g.zWR); speedtest1_exec("CREATE INDEX t4b ON t4(b)"); speedtest1_exec("CREATE INDEX t4c ON t4(c)"); - speedtest1_exec("INSERT INTO t4 SELECT * FROM t1"); + speedtest1_exec("INSERT INTO t4 SELECT * FROM z1"); speedtest1_exec("COMMIT"); speedtest1_end_test(); n = sz; speedtest1_begin_test(190, "DELETE and REFILL one table", n); - speedtest1_exec("DELETE FROM t2;"); - speedtest1_exec("INSERT INTO t2 SELECT * FROM t1;"); + speedtest1_exec("DELETE FROM z2;"); + speedtest1_exec("INSERT INTO z2 SELECT * FROM z1;"); speedtest1_end_test(); @@ -679,8 +1032,8 @@ void testset_main(void){ speedtest1_begin_test(210, "ALTER TABLE ADD COLUMN, and query"); - speedtest1_exec("ALTER TABLE t2 ADD COLUMN d DEFAULT 123"); - speedtest1_exec("SELECT sum(d) FROM t2"); + speedtest1_exec("ALTER TABLE z2 ADD COLUMN d INT DEFAULT 123"); + speedtest1_exec("SELECT sum(d) FROM z2"); speedtest1_end_test(); @@ -688,7 +1041,7 @@ void testset_main(void){ speedtest1_begin_test(230, "%d UPDATES, numeric BETWEEN, indexed", n); speedtest1_exec("BEGIN"); speedtest1_prepare( - "UPDATE t2 SET d=b*2 WHERE b BETWEEN ?1 AND ?2; -- %d times", n + "UPDATE z2 SET d=b*2 WHERE b BETWEEN ?1 AND ?2; -- %d times", n ); for(i=1; i<=n; i++){ x1 = speedtest1_random()%maxb; @@ -705,7 +1058,7 @@ void testset_main(void){ speedtest1_begin_test(240, "%d UPDATES of individual rows", n); speedtest1_exec("BEGIN"); speedtest1_prepare( - "UPDATE t2 SET d=b*3 WHERE a=?1; -- %d times", n + "UPDATE z2 SET d=b*3 WHERE a=?1; -- %d times", n ); for(i=1; i<=n; i++){ x1 = speedtest1_random()%sz + 1; @@ -716,12 +1069,12 @@ void testset_main(void){ speedtest1_end_test(); speedtest1_begin_test(250, "One big UPDATE of the whole %d-row table", sz); - speedtest1_exec("UPDATE t2 SET d=b*4"); + speedtest1_exec("UPDATE z2 SET d=b*4"); speedtest1_end_test(); speedtest1_begin_test(260, "Query added column after filling"); - speedtest1_exec("SELECT sum(d) FROM t2"); + speedtest1_exec("SELECT sum(d) FROM z2"); speedtest1_end_test(); @@ -730,7 +1083,7 @@ void testset_main(void){ speedtest1_begin_test(270, "%d DELETEs, numeric BETWEEN, indexed", n); speedtest1_exec("BEGIN"); speedtest1_prepare( - "DELETE FROM t2 WHERE b BETWEEN ?1 AND ?2; -- %d times", n + "DELETE FROM z2 WHERE b BETWEEN ?1 AND ?2; -- %d times", n ); for(i=1; i<=n; i++){ x1 = speedtest1_random()%maxb + 1; @@ -759,16 +1112,16 @@ void testset_main(void){ speedtest1_begin_test(290, "Refill two %d-row tables using REPLACE", sz); - speedtest1_exec("REPLACE INTO t2(a,b,c) SELECT a,b,c FROM t1"); - speedtest1_exec("REPLACE INTO t3(a,b,c) SELECT a,b,c FROM t1"); + speedtest1_exec("REPLACE INTO z2(a,b,c) SELECT a,b,c FROM z1"); + speedtest1_exec("REPLACE INTO t3(a,b,c) SELECT a,b,c FROM z1"); speedtest1_end_test(); speedtest1_begin_test(300, "Refill a %d-row table using (b&1)==(a&1)", sz); - speedtest1_exec("DELETE FROM t2;"); - speedtest1_exec("INSERT INTO t2(a,b,c)\n" - " SELECT a,b,c FROM t1 WHERE (b&1)==(a&1);"); - speedtest1_exec("INSERT INTO t2(a,b,c)\n" - " SELECT a,b,c FROM t1 WHERE (b&1)<>(a&1);"); + speedtest1_exec("DELETE FROM z2;"); + speedtest1_exec("INSERT INTO z2(a,b,c)\n" + " SELECT a,b,c FROM z1 WHERE (b&1)==(a&1);"); + speedtest1_exec("INSERT INTO z2(a,b,c)\n" + " SELECT a,b,c FROM z1 WHERE (b&1)<>(a&1);"); speedtest1_end_test(); @@ -776,11 +1129,11 @@ void testset_main(void){ speedtest1_begin_test(310, "%d four-ways joins", n); speedtest1_exec("BEGIN"); speedtest1_prepare( - "SELECT t1.c FROM t1, t2, t3, t4\n" + "SELECT z1.c FROM z1, z2, t3, t4\n" " WHERE t4.a BETWEEN ?1 AND ?2\n" " AND t3.a=t4.b\n" - " AND t2.a=t3.b\n" - " AND t1.c=t2.c" + " AND z2.a=t3.b\n" + " AND z1.c=z2.c;" ); for(i=1; i<=n; i++){ x1 = speedtest1_random()%sz + 1; @@ -795,13 +1148,91 @@ void testset_main(void){ speedtest1_begin_test(320, "subquery in result set", n); speedtest1_prepare( "SELECT sum(a), max(c),\n" - " avg((SELECT a FROM t2 WHERE 5+t2.b=t1.b) AND rowid<?1), max(c)\n" - " FROM t1 WHERE rowid<?1;" + " avg((SELECT a FROM z2 WHERE 5+z2.b=z1.b) AND rowid<?1), max(c)\n" + " FROM z1 WHERE rowid<?1;" ); sqlite3_bind_int(g.pStmt, 1, est_square_root(g.szTest)*50); speedtest1_run(); speedtest1_end_test(); + sz = n = g.szTest*700; + zNum[0] = 0; + maxb = roundup_allones(sz/3); + speedtest1_begin_test(400, "%d REPLACE ops on an IPK", n); + speedtest1_exec("BEGIN"); + speedtest1_exec("CREATE%s TABLE t5(a INTEGER PRIMARY KEY, b %s);", + isTemp(9), g.zNN); + speedtest1_prepare("REPLACE INTO t5 VALUES(?1,?2); -- %d times",n); + for(i=1; i<=n; i++){ + x1 = swizzle(i,maxb); + speedtest1_numbername(i, zNum, sizeof(zNum)); + sqlite3_bind_int(g.pStmt, 1, (sqlite3_int64)x1); + sqlite3_bind_text(g.pStmt, 2, zNum, -1, SQLITE_STATIC); + speedtest1_run(); + } + speedtest1_exec("COMMIT"); + speedtest1_end_test(); + speedtest1_begin_test(410, "%d SELECTS on an IPK", n); + if( g.doBigTransactions ){ + /* Historical note: tests 410 and 510 have historically not used + ** explicit transactions. The --big-transactions flag was added + ** 2022-09-08 to support the WASM/OPFS build, as the run-times + ** approach 1 minute for each of these tests if they're not in an + ** explicit transaction. The run-time effect of --big-transaciions + ** on native builds is negligible. */ + speedtest1_exec("BEGIN"); + } + speedtest1_prepare("SELECT b FROM t5 WHERE a=?1; -- %d times",n); + for(i=1; i<=n; i++){ + x1 = swizzle(i,maxb); + sqlite3_bind_int(g.pStmt, 1, (sqlite3_int64)x1); + speedtest1_run(); + } + if( g.doBigTransactions ){ + speedtest1_exec("COMMIT"); + } + speedtest1_end_test(); + + sz = n = g.szTest*700; + zNum[0] = 0; + maxb = roundup_allones(sz/3); + speedtest1_begin_test(500, "%d REPLACE on TEXT PK", n); + speedtest1_exec("BEGIN"); + speedtest1_exec("CREATE%s TABLE t6(a TEXT PRIMARY KEY, b %s)%s;", + isTemp(9), g.zNN, + sqlite3_libversion_number()>=3008002 ? "WITHOUT ROWID" : ""); + speedtest1_prepare("REPLACE INTO t6 VALUES(?1,?2); -- %d times",n); + for(i=1; i<=n; i++){ + x1 = swizzle(i,maxb); + speedtest1_numbername(x1, zNum, sizeof(zNum)); + sqlite3_bind_int(g.pStmt, 2, i); + sqlite3_bind_text(g.pStmt, 1, zNum, -1, SQLITE_STATIC); + speedtest1_run(); + } + speedtest1_exec("COMMIT"); + speedtest1_end_test(); + speedtest1_begin_test(510, "%d SELECTS on a TEXT PK", n); + if( g.doBigTransactions ){ + /* See notes for test 410. */ + speedtest1_exec("BEGIN"); + } + speedtest1_prepare("SELECT b FROM t6 WHERE a=?1; -- %d times",n); + for(i=1; i<=n; i++){ + x1 = swizzle(i,maxb); + speedtest1_numbername(x1, zNum, sizeof(zNum)); + sqlite3_bind_text(g.pStmt, 1, zNum, -1, SQLITE_STATIC); + speedtest1_run(); + } + if( g.doBigTransactions ){ + speedtest1_exec("COMMIT"); + } + speedtest1_end_test(); + speedtest1_begin_test(520, "%d SELECT DISTINCT", n); + speedtest1_exec("SELECT DISTINCT b FROM t5;"); + speedtest1_exec("SELECT DISTINCT b FROM t6;"); + speedtest1_end_test(); + + speedtest1_begin_test(980, "PRAGMA integrity_check"); speedtest1_exec("PRAGMA integrity_check"); speedtest1_end_test(); @@ -955,16 +1386,654 @@ void testset_cte(void){ speedtest1_begin_test(400, "EXCEPT operator on %d-element tables", nElem); speedtest1_prepare( "WITH RECURSIVE \n" - " t1(x) AS (VALUES(2) UNION ALL SELECT x+2 FROM t1 WHERE x<%d),\n" - " t2(y) AS (VALUES(3) UNION ALL SELECT y+3 FROM t2 WHERE y<%d)\n" + " z1(x) AS (VALUES(2) UNION ALL SELECT x+2 FROM z1 WHERE x<%d),\n" + " z2(y) AS (VALUES(3) UNION ALL SELECT y+3 FROM z2 WHERE y<%d)\n" "SELECT count(x), avg(x) FROM (\n" - " SELECT x FROM t1 EXCEPT SELECT y FROM t2 ORDER BY 1\n" + " SELECT x FROM z1 EXCEPT SELECT y FROM z2 ORDER BY 1\n" ");", nElem, nElem ); speedtest1_run(); speedtest1_end_test(); +} +/* +** Compute a pseudo-random floating point ascii number. +*/ +void speedtest1_random_ascii_fp(char *zFP){ + int x = speedtest1_random(); + int y = speedtest1_random(); + int z; + z = y%10; + if( z<0 ) z = -z; + y /= 10; + sqlite3_snprintf(100,zFP,"%d.%de%d",y,z,x%200); +} + +/* +** A testset for floating-point numbers. +*/ +void testset_fp(void){ + int n; + int i; + char zFP1[100]; + char zFP2[100]; + + n = g.szTest*5000; + speedtest1_begin_test(100, "Fill a table with %d FP values", n*2); + speedtest1_exec("BEGIN"); + speedtest1_exec("CREATE%s TABLE z1(a REAL %s, b REAL %s);", + isTemp(1), g.zNN, g.zNN); + speedtest1_prepare("INSERT INTO z1 VALUES(?1,?2); -- %d times", n); + for(i=1; i<=n; i++){ + speedtest1_random_ascii_fp(zFP1); + speedtest1_random_ascii_fp(zFP2); + sqlite3_bind_text(g.pStmt, 1, zFP1, -1, SQLITE_STATIC); + sqlite3_bind_text(g.pStmt, 2, zFP2, -1, SQLITE_STATIC); + speedtest1_run(); + } + speedtest1_exec("COMMIT"); + speedtest1_end_test(); + + n = g.szTest/25 + 2; + speedtest1_begin_test(110, "%d range queries", n); + speedtest1_prepare("SELECT sum(b) FROM z1 WHERE a BETWEEN ?1 AND ?2"); + for(i=1; i<=n; i++){ + speedtest1_random_ascii_fp(zFP1); + speedtest1_random_ascii_fp(zFP2); + sqlite3_bind_text(g.pStmt, 1, zFP1, -1, SQLITE_STATIC); + sqlite3_bind_text(g.pStmt, 2, zFP2, -1, SQLITE_STATIC); + speedtest1_run(); + } + speedtest1_end_test(); + + speedtest1_begin_test(120, "CREATE INDEX three times"); + speedtest1_exec("BEGIN;"); + speedtest1_exec("CREATE INDEX t1a ON z1(a);"); + speedtest1_exec("CREATE INDEX t1b ON z1(b);"); + speedtest1_exec("CREATE INDEX t1ab ON z1(a,b);"); + speedtest1_exec("COMMIT;"); + speedtest1_end_test(); + + n = g.szTest/3 + 2; + speedtest1_begin_test(130, "%d indexed range queries", n); + speedtest1_prepare("SELECT sum(b) FROM z1 WHERE a BETWEEN ?1 AND ?2"); + for(i=1; i<=n; i++){ + speedtest1_random_ascii_fp(zFP1); + speedtest1_random_ascii_fp(zFP2); + sqlite3_bind_text(g.pStmt, 1, zFP1, -1, SQLITE_STATIC); + sqlite3_bind_text(g.pStmt, 2, zFP2, -1, SQLITE_STATIC); + speedtest1_run(); + } + speedtest1_end_test(); + + n = g.szTest*5000; + speedtest1_begin_test(140, "%d calls to round()", n); + speedtest1_exec("SELECT sum(round(a,2)+round(b,4)) FROM z1;"); + speedtest1_end_test(); + + + speedtest1_begin_test(150, "%d printf() calls", n*4); + speedtest1_exec( + "WITH c(fmt) AS (VALUES('%%g'),('%%e'),('%%!g'),('%%.20f'))" + "SELECT sum(printf(fmt,a)) FROM z1, c" + ); + speedtest1_end_test(); +} + +/* +** A testset for star-schema queries. +*/ +void testset_star(void){ + int n; + int i; + n = g.szTest*50; + speedtest1_begin_test(100, "Create a fact table with %d entries", n); + speedtest1_exec( + "CREATE TABLE facttab(" + " attr01 INT," + " attr02 INT," + " attr03 INT," + " data01 TEXT," + " attr04 INT," + " attr05 INT," + " attr06 INT," + " attr07 INT," + " attr08 INT," + " factid INTEGER PRIMARY KEY," + " data02 TEXT" + ");" + ); + speedtest1_exec( + "WITH RECURSIVE counter(nnn) AS" + "(VALUES(1) UNION ALL SELECT nnn+1 FROM counter WHERE nnn<%d)" + "INSERT INTO facttab(attr01,attr02,attr03,attr04,attr05," + "attr06,attr07,attr08,data01,data02)" + "SELECT random()%%12, random()%%13, random()%%14, random()%%15," + "random()%%16, random()%%17, random()%%18, random()%%19," + "concat('data-',nnn), format('%%x',random()) FROM counter;", + n + ); + speedtest1_end_test(); + + speedtest1_begin_test(110, "Create indexes on all attributes columns"); + for(i=1; i<=8; i++){ + speedtest1_exec( + "CREATE INDEX fact_attr%02d ON facttab(attr%02d)", i, i + ); + } + speedtest1_end_test(); + + speedtest1_begin_test(120, "Create dimension tables"); + for(i=1; i<=8; i++){ + speedtest1_exec( + "CREATE TABLE dimension%02d(" + "beta%02d INT, " + "content%02d TEXT, " + "rate%02d REAL)", + i, i, i, i + ); + speedtest1_exec( + "WITH RECURSIVE ctr(nn) AS" + " (VALUES(1) UNION ALL SELECT nn+1 FROM ctr WHERE nn<%d)" + " INSERT INTO dimension%02d" + " SELECT nn%%(%d), concat('content-%02d-',nn)," + " (random()%%10000)*0.125 FROM ctr;", + 4*(i+1), i, 2*(i+1), i + ); + if( i&2 ){ + speedtest1_exec( + "CREATE INDEX dim%02d ON dimension%02d(beta%02d);", + i, i, i + ); + }else{ + speedtest1_exec( + "CREATE INDEX dim%02d ON dimension%02d(beta%02d,content%02d);", + i, i, i, i + ); + } + } + speedtest1_end_test(); + + speedtest1_begin_test(130, "Star query over the entire fact table"); + speedtest1_exec( + "SELECT count(*), max(content04), min(content03), sum(rate04), avg(rate05)" + " FROM facttab, dimension01, dimension02, dimension03, dimension04," + " dimension05, dimension06, dimension07, dimension08" + " WHERE attr01=beta01" + " AND attr02=beta02" + " AND attr03=beta03" + " AND attr04=beta04" + " AND attr05=beta05" + " AND attr06=beta06" + " AND attr07=beta07" + " AND attr08=beta08" + ";" + ); + speedtest1_end_test(); + + speedtest1_begin_test(130, "Star query with LEFT JOINs"); + speedtest1_exec( + "SELECT count(*), max(content04), min(content03), sum(rate04), avg(rate05)" + " FROM facttab LEFT JOIN dimension01 ON attr01=beta01" + " LEFT JOIN dimension02 ON attr02=beta02" + " JOIN dimension03 ON attr03=beta03" + " JOIN dimension04 ON attr04=beta04" + " JOIN dimension05 ON attr05=beta05" + " LEFT JOIN dimension06 ON attr06=beta06" + " JOIN dimension07 ON attr07=beta07" + " JOIN dimension08 ON attr08=beta08" + " WHERE facttab.data01 LIKE 'data-9%%'" + ";" + ); + speedtest1_end_test(); +} + +/* +** Tests that simulate an application opening and closing an SQLite database +** frequently. Fossil is used as the model. The focus here is on rapidly +** parsing the database schema and rapidly generating prepared statements, +** in other words, rapid start-up of Fossil-like applications. +** +** The same database has no data, so the performance of sqlite3_step() is +** not significant to this testset. +*/ +static void testset_app(void){ + int i, n; + speedtest1_begin_test(100, "Generate a Fossil-like database schema"); + speedtest1_exec( + "BEGIN;" + "CREATE TABLE blob(\n" + " rid INTEGER PRIMARY KEY,\n" + " rcvid INTEGER,\n" + " size INTEGER,\n" + " uuid TEXT UNIQUE NOT NULL,\n" + " content BLOB,\n" + " CHECK( length(uuid)>=40 AND rid>0 )\n" + ");\n" + "CREATE TABLE delta(\n" + " rid INTEGER PRIMARY KEY,\n" + " srcid INTEGER NOT NULL REFERENCES blob\n" + ");\n" + "CREATE TABLE rcvfrom(\n" + " rcvid INTEGER PRIMARY KEY,\n" + " uid INTEGER REFERENCES user,\n" + " mtime DATETIME,\n" + " nonce TEXT UNIQUE,\n" + " ipaddr TEXT\n" + ");\n" + "CREATE TABLE private(rid INTEGER PRIMARY KEY);\n" + "CREATE TABLE accesslog(\n" + " uname TEXT,\n" + " ipaddr TEXT,\n" + " success BOOLEAN,\n" + " mtime TIMESTAMP\n" + ");\n" + "CREATE TABLE user(\n" + " uid INTEGER PRIMARY KEY,\n" + " login TEXT UNIQUE,\n" + " pw TEXT,\n" + " cap TEXT,\n" + " cookie TEXT,\n" + " ipaddr TEXT,\n" + " cexpire DATETIME,\n" + " info TEXT,\n" + " mtime DATE,\n" + " photo BLOB\n" + ", jx TEXT DEFAULT '{}');\n" + "CREATE TABLE reportfmt(\n" + " rn INTEGER PRIMARY KEY,\n" + " owner TEXT,\n" + " title TEXT UNIQUE,\n" + " mtime INTEGER,\n" + " cols TEXT,\n" + " sqlcode TEXT\n" + ", jx TEXT DEFAULT '{}');\n" + "CREATE TABLE config(\n" + " name TEXT PRIMARY KEY NOT NULL,\n" + " value CLOB, mtime INTEGER,\n" + " CHECK( typeof(name)='text' AND length(name)>=1 )\n" + ") WITHOUT ROWID;\n" + "CREATE TABLE shun(uuid PRIMARY KEY, mtime INTEGER, scom TEXT)\n" + " WITHOUT ROWID;\n" + "CREATE TABLE concealed(\n" + " hash TEXT PRIMARY KEY,\n" + " content TEXT\n" + ", mtime INTEGER) WITHOUT ROWID;\n" + "CREATE TABLE admin_log(\n" + " id INTEGER PRIMARY KEY,\n" + " time INTEGER, -- Seconds since 1970\n" + " page TEXT, -- path of page\n" + " who TEXT, -- User who made the change\n" + " what TEXT -- What changed\n" + ");\n" + "CREATE TABLE unversioned(\n" + " name TEXT PRIMARY KEY,\n" + " rcvid INTEGER,\n" + " mtime DATETIME,\n" + " hash TEXT,\n" + " sz INTEGER,\n" + " encoding INT,\n" + " content BLOB\n" + ") WITHOUT ROWID;\n" + "CREATE TABLE subscriber(\n" + " subscriberId INTEGER PRIMARY KEY,\n" + " subscriberCode BLOB DEFAULT (randomblob(32)) UNIQUE,\n" + " semail TEXT UNIQUE COLLATE nocase,\n" + " suname TEXT,\n" + " sverified BOOLEAN DEFAULT true,\n" + " sdonotcall BOOLEAN,\n" + " sdigest BOOLEAN,\n" + " ssub TEXT,\n" + " sctime INTDATE,\n" + " mtime INTDATE,\n" + " smip TEXT\n" + ", lastContact INT);\n" + "CREATE TABLE pending_alert(\n" + " eventid TEXT PRIMARY KEY,\n" + " sentSep BOOLEAN DEFAULT false,\n" + " sentDigest BOOLEAN DEFAULT false\n" + ", sentMod BOOLEAN DEFAULT false) WITHOUT ROWID;\n" + "CREATE TABLE filename(\n" + " fnid INTEGER PRIMARY KEY,\n" + " name TEXT UNIQUE\n" + ") STRICT;\n" + "CREATE TABLE mlink(\n" + " mid INTEGER,\n" + " fid INTEGER,\n" + " pmid INTEGER,\n" + " pid INTEGER,\n" + " fnid INTEGER REFERENCES filename,\n" + " pfnid INTEGER,\n" + " mperm INTEGER,\n" + " isaux INT DEFAULT 0\n" + ") STRICT;\n" + "CREATE TABLE plink(\n" + " pid INTEGER REFERENCES blob,\n" + " cid INTEGER REFERENCES blob,\n" + " isprim INT,\n" + " mtime REAL,\n" + " baseid INTEGER REFERENCES blob,\n" + " UNIQUE(pid, cid)\n" + ") STRICT;\n" + "CREATE TABLE leaf(rid INTEGER PRIMARY KEY);\n" + "CREATE TABLE event(\n" + " type TEXT,\n" + " mtime REAL,\n" + " objid INTEGER PRIMARY KEY,\n" + " tagid INTEGER,\n" + " uid INTEGER REFERENCES user,\n" + " bgcolor TEXT,\n" + " euser TEXT,\n" + " user TEXT,\n" + " ecomment TEXT,\n" + " comment TEXT,\n" + " brief TEXT,\n" + " omtime REAL\n" + ") STRICT;\n" + "CREATE TABLE phantom(\n" + " rid INTEGER PRIMARY KEY\n" + ");\n" + "CREATE TABLE orphan(\n" + " rid INTEGER PRIMARY KEY,\n" + " baseline INTEGER\n" + ") STRICT;\n" + "CREATE TABLE unclustered(\n" + " rid INTEGER PRIMARY KEY\n" + ");\n" + "CREATE TABLE unsent(\n" + " rid INTEGER PRIMARY KEY\n" + ");\n" + "CREATE TABLE tag(\n" + " tagid INTEGER PRIMARY KEY,\n" + " tagname TEXT UNIQUE\n" + ") STRICT;\n" + "CREATE TABLE tagxref(\n" + " tagid INTEGER REFERENCES tag,\n" + " tagtype INTEGER,\n" + " srcid INTEGER REFERENCES blob,\n" + " origid INTEGER REFERENCES blob,\n" + " value TEXT,\n" + " mtime REAL,\n" + " rid INTEGER REFERENCES blob,\n" + " UNIQUE(rid, tagid)\n" + ") STRICT;\n" + "CREATE TABLE backlink(\n" + " target TEXT,\n" + " srctype INT,\n" + " srcid INT,\n" + " mtime REAL,\n" + " UNIQUE(target, srctype, srcid)\n" + ") STRICT;\n" + "CREATE TABLE attachment(\n" + " attachid INTEGER PRIMARY KEY,\n" + " isLatest INT DEFAULT 0,\n" + " mtime REAL,\n" + " src TEXT,\n" + " target TEXT,\n" + " filename TEXT,\n" + " comment TEXT,\n" + " user TEXT\n" + ") STRICT;\n" + "CREATE TABLE cherrypick(\n" + " parentid INT,\n" + " childid INT,\n" + " isExclude INT DEFAULT false,\n" + " PRIMARY KEY(parentid, childid)\n" + ") WITHOUT ROWID, STRICT;\n" + "CREATE TABLE vcache(\n" + " vid INTEGER, -- check-in ID\n" + " fname TEXT, -- filename\n" + " rid INTEGER, -- artifact ID\n" + " PRIMARY KEY(vid,fname)\n" + ") WITHOUT ROWID;\n" + "CREATE TABLE synclog(\n" + " sfrom TEXT,\n" + " sto TEXT,\n" + " stime INT NOT NULL,\n" + " stype TEXT,\n" + " PRIMARY KEY(sfrom,sto)\n" + ") WITHOUT ROWID;\n" + "CREATE TABLE chat(\n" + " msgid INTEGER PRIMARY KEY AUTOINCREMENT,\n" + " mtime JULIANDAY,\n" + " lmtime TEXT,\n" + " xfrom TEXT,\n" + " xmsg TEXT,\n" + " fname TEXT,\n" + " fmime TEXT,\n" + " mdel INT,\n" + " file BLOB\n" + ");\n" + "CREATE TABLE ftsdocs(\n" + " rowid INTEGER PRIMARY KEY,\n" + " type CHAR(1),\n" + " rid INTEGER,\n" + " name TEXT,\n" + " idxed BOOLEAN,\n" + " label TEXT,\n" + " url TEXT,\n" + " mtime DATE,\n" + " bx TEXT,\n" + " UNIQUE(type,rid)\n" + ");\n" + "CREATE TABLE ticket(\n" + " -- Do not change any column that begins with tkt_\n" + " tkt_id INTEGER PRIMARY KEY,\n" + " tkt_uuid TEXT UNIQUE,\n" + " tkt_mtime DATE,\n" + " tkt_ctime DATE,\n" + " -- Add as many fields as required below this line\n" + " type TEXT,\n" + " status TEXT,\n" + " subsystem TEXT,\n" + " priority TEXT,\n" + " severity TEXT,\n" + " foundin TEXT,\n" + " private_contact TEXT,\n" + " resolution TEXT,\n" + " title TEXT,\n" + " comment TEXT\n" + ");\n" + "CREATE TABLE ticketchng(\n" + " -- Do not change any column that begins with tkt_\n" + " tkt_id INTEGER REFERENCES ticket,\n" + " tkt_rid INTEGER REFERENCES blob,\n" + " tkt_mtime DATE,\n" + " tkt_user TEXT,\n" + " -- Add as many fields as required below this line\n" + " login TEXT,\n" + " username TEXT,\n" + " mimetype TEXT,\n" + " icomment TEXT\n" + ");\n" + "CREATE TABLE forumpost(\n" + " fpid INTEGER PRIMARY KEY,\n" + " froot INT,\n" + " fprev INT,\n" + " firt INT,\n" + " fmtime REAL\n" + ");\n" + "CREATE INDEX delta_i1 ON delta(srcid);\n" + "CREATE INDEX blob_rcvid ON blob(rcvid);\n" + "CREATE INDEX subscriberUname\n" + " ON subscriber(suname) WHERE suname IS NOT NULL;\n" + "CREATE INDEX mlink_i1 ON mlink(mid);\n" + "CREATE INDEX mlink_i2 ON mlink(fnid);\n" + "CREATE INDEX mlink_i3 ON mlink(fid);\n" + "CREATE INDEX mlink_i4 ON mlink(pid);\n" + "CREATE INDEX plink_i2 ON plink(cid,pid);\n" + "CREATE INDEX event_i1 ON event(mtime);\n" + "CREATE INDEX orphan_baseline ON orphan(baseline);\n" + "CREATE INDEX tagxref_i1 ON tagxref(tagid, mtime);\n" + "CREATE INDEX backlink_src ON backlink(srcid, srctype);\n" + "CREATE INDEX attachment_idx1 ON attachment(target, filename, mtime);\n" + "CREATE INDEX attachment_idx2 ON attachment(src);\n" + "CREATE INDEX cherrypick_cid ON cherrypick(childid);\n" + "CREATE INDEX ftsdocIdxed ON ftsdocs(type,rid,name) WHERE idxed==0;\n" + "CREATE INDEX ftsdocName ON ftsdocs(name) WHERE type='w';\n" + "CREATE INDEX ticketchng_idx1 ON ticketchng(tkt_id, tkt_mtime);\n" + "CREATE INDEX forumthread ON forumpost(froot,fmtime);\n" + "CREATE VIEW artifact(rid,rcvid,size,atype,srcid,hash,content) AS\n" + " SELECT blob.rid,rcvid,size,1,srcid,uuid,content\n" + " FROM blob LEFT JOIN delta ON (blob.rid=delta.rid);\n" + "CREATE VIEW ftscontent AS\n" + " SELECT rowid, type, rid, name, idxed, label, url, mtime,\n" + " title(type,rid,name) AS 'title', body(type,rid,name) AS 'body'\n" + " FROM ftsdocs;\n" + ); + if( sqlite3_compileoption_used("ENABLE_FTS5") ){ + speedtest1_exec( + "CREATE VIRTUAL TABLE ftsidx\n" + " USING fts5(content=\"ftscontent\", title, body);\n" + "CREATE VIRTUAL TABLE chatfts1 USING fts5(\n" + " xmsg, content=chat, content_rowid=msgid,tokenize=porter);\n" + ); + }else{ + speedtest1_exec( + "CREATE TABLE ftsidx_data(id INTEGER PRIMARY KEY, block BLOB);\n" + "CREATE TABLE ftsidx_idx(segid, term, pgno, PRIMARY KEY(segid, term))\n" + " WITHOUT ROWID;\n" + "CREATE TABLE ftsidx_docsize(id INTEGER PRIMARY KEY, sz BLOB);\n" + "CREATE TABLE ftsidx_config(k PRIMARY KEY, v) WITHOUT ROWID;\n" + "CREATE TABLE chatfts1_data(id INTEGER PRIMARY KEY, block BLOB);\n" + "CREATE TABLE chatfts1_idx(segid, term, pgno, PRIMARY KEY(segid, term))\n" + " WITHOUT ROWID;\n" + "CREATE TABLE chatfts1_docsize(id INTEGER PRIMARY KEY, sz BLOB);\n" + "CREATE TABLE chatfts1_config(k PRIMARY KEY, v) WITHOUT ROWID;\n" + ); + } + speedtest1_exec( + "ANALYZE sqlite_schema;\n" + "INSERT INTO sqlite_stat1(tbl,idx,stat) VALUES\n" + " ('ftsidx_config','ftsidx_config','1 1'),\n" + " ('ftsidx_idx','ftsidx_idx','4215 401 1'),\n" + " ('user','sqlite_autoindex_user_1','25 1'),\n" + " ('phantom',NULL,'26'),\n" + " ('reportfmt','sqlite_autoindex_reportfmt_1','9 1'),\n" + " ('rcvfrom','sqlite_autoindex_rcvfrom_1','18445 401'),\n" + " ('private',NULL,'99'),\n" + " ('mlink','mlink_i4','116678 401'),\n" + " ('mlink','mlink_i3','121212 2'),\n" + " ('mlink','mlink_i2','106372 401'),\n" + " ('mlink','mlink_i1','99298 5'),\n" + " ('ftsidx_data',NULL,'3795'),\n" + " ('leaf',NULL,'1559'),\n" + " ('delta','delta_i1','66340 1'),\n" + " ('unversioned','unversioned','3 1'),\n" + " ('pending_alert','pending_alert','3 1'),\n" + " ('cherrypick','cherrypick_cid','680 2'),\n" + " ('cherrypick','cherrypick','628 1 1'),\n" + " ('config','config','128 1'),\n" + " ('ftsidx_docsize',NULL,'33848'),\n" + " ('event','event_i1','36096 1'),\n" + " ('plink','plink_i2','38236 1 1'),\n" + " ('plink','sqlite_autoindex_plink_1','38357 1 1'),\n" + " ('shun','shun','10 1'),\n" + " ('concealed','concealed','110 1'),\n" + " ('vcache','vcache','1888 401 1'),\n" + " ('ftsdocs','ftsdocName','19 1'),\n" + " ('ftsdocs','ftsdocIdxed','168 84 1 1'),\n" + " ('ftsdocs','sqlite_autoindex_ftsdocs_1','37312 401 1'),\n" + " ('subscriber','subscriberUname','5 1'),\n" + " ('subscriber','sqlite_autoindex_subscriber_2','37 1'),\n" + " ('subscriber','sqlite_autoindex_subscriber_1','37 1'),\n" + " ('tag','sqlite_autoindex_tag_1','2990 1'),\n" + " ('filename','sqlite_autoindex_filename_1','3168 1'),\n" + " ('chat',NULL,'56124'),\n" + " ('tagxref','tagxref_i1','40992 401 2'),\n" + " ('tagxref','sqlite_autoindex_tagxref_1','79233 3 1'),\n" + " ('attachment','attachment_idx2','11 1'),\n" + " ('attachment','attachment_idx1','11 2 2 1'),\n" + " ('blob','blob_rcvid','128240 201'),\n" + " ('blob','sqlite_autoindex_blob_1','126480 1'),\n" + " ('synclog','synclog','12 3 1'),\n" + " ('backlink','backlink_src','2160 2 2'),\n" + " ('backlink','sqlite_autoindex_backlink_1','2340 2 2 1'),\n" + " ('accesslog',NULL,'38'),\n" + " ('chatfts1_config','chatfts1_config','1 1'),\n" + " ('chatfts1_idx','chatfts1_idx','688 230 1'),\n" + " ('ticket','sqlite_autoindex_ticket_1','794 1'),\n" + " ('ticketchng','ticketchng_idx1','2089 3 1'),\n" + " ('forumpost','forumthread','4 4 1'),\n" + " ('unclustered',NULL,'12');\n" + "COMMIT;" + ); + speedtest1_end_test(); + + n = g.szTest*3; + speedtest1_begin_test(110, "Open and use the database %d times", n); + for(i=0; i<n; i++){ + sqlite3 *dbMain = g.db; + sqlite3 *dbAux = 0; + if( g.zDbName && g.zDbName[0] ){ + if( sqlite3_open_v2(g.zDbName, &dbAux, SQLITE_OPEN_READWRITE, g.zVfs) ){ + fatal_error("Cannot open database file: %s\n", g.zDbName); + } + g.db = dbAux; + } + speedtest1_exec( + "SELECT name FROM pragma_table_list /*scan*/" + " WHERE schema='repository' AND type IN ('table','virtual')" + " AND name NOT IN ('admin_log', 'blob','delta','rcvfrom','user','alias'," + "'config','shun','private','reportfmt'," + "'concealed','accesslog','modreq'," + "'purgeevent','purgeitem','unversioned'," + "'subscriber','pending_alert','chat')" + " AND name NOT GLOB 'sqlite_*'" + " AND name NOT GLOB 'fx_*';" + "SELECT 1 FROM pragma_table_xinfo('ticket') WHERE name = 'mimetype';" + ); + speedtest1_exec( + "SELECT" + " name," + " value," + " unixepoch()/86400-value," + " date(value*86400,'unixepoch')" + " FROM config" + " WHERE name in ('email-renew-warning','email-renew-cutoff');" + "SELECT count(*) FROM pending_alert WHERE NOT sentDigest;" + ); + speedtest1_exec( + "WITH priors(rid,who) AS (" + " SELECT firt, coalesce(euser,user)" + " FROM forumpost LEFT JOIN event ON fpid=objid" + " WHERE fpid=12345" + " UNION ALL" + " SELECT firt, coalesce(euser,user)" + " FROM priors, forumpost LEFT JOIN event ON fpid=objid" + " WHERE fpid=rid" + ")" + "SELECT ','||group_concat(DISTINCT 'u'||who)||" + "','||group_concat(rid) FROM priors;" + ); + speedtest1_exec( + "CREATE TEMP TABLE IF NOT EXISTS ok(rid INTEGER PRIMARY KEY);\n" + ); + speedtest1_exec( + "WITH RECURSIVE\n" + " parent(pid,cid,isCP) AS (\n" + " SELECT plink.pid, plink.cid, 0 AS xisCP FROM plink\n" + " UNION ALL\n" + " SELECT parentid, childid, 1 FROM cherrypick WHERE NOT isExclude\n" + " ),\n" + " ancestor(rid, mtime, isCP) AS (\n" + " SELECT 123, mtime, 0 FROM event WHERE objid=$object\n" + " UNION\n" + " SELECT parent.pid, event.mtime, parent.isCP\n" + " FROM ancestor, parent, event\n" + " WHERE parent.cid=ancestor.rid\n" + " AND event.objid=parent.pid\n" + " AND NOT ancestor.isCP\n" + " AND (event.mtime>=$date OR parent.pid=$pid)\n" + " ORDER BY mtime DESC LIMIT 10\n" + " )\n" + " INSERT OR IGNORE INTO ok SELECT rid FROM ancestor;" + ); + sqlite3_close(dbAux); + g.db = dbMain; + } + speedtest1_end_test(); } #ifdef SQLITE_ENABLE_RTREE @@ -1021,10 +2090,11 @@ void testset_rtree(int p1, int p2){ unsigned mxCoord; unsigned x0, x1, y0, y1, z0, z1; unsigned iStep; - int *aCheck = sqlite3_malloc( sizeof(int)*g.szTest*100 ); + unsigned mxRowid; + int *aCheck = sqlite3_malloc( sizeof(int)*g.szTest*500 ); mxCoord = 15000; - n = g.szTest*100; + mxRowid = n = g.szTest*500; speedtest1_begin_test(100, "%d INSERTs into an r-tree", n); speedtest1_exec("BEGIN"); speedtest1_exec("CREATE VIRTUAL TABLE rt1 USING rtree(id,x0,x1,y0,y1,z0,z1)"); @@ -1047,11 +2117,11 @@ void testset_rtree(int p1, int p2){ speedtest1_end_test(); speedtest1_begin_test(101, "Copy from rtree to a regular table"); - speedtest1_exec("CREATE TABLE t1(id INTEGER PRIMARY KEY,x0,x1,y0,y1,z0,z1)"); - speedtest1_exec("INSERT INTO t1 SELECT * FROM rt1"); + speedtest1_exec("CREATE TABLE z1(id INTEGER PRIMARY KEY,x0,x1,y0,y1,z0,z1)"); + speedtest1_exec("INSERT INTO z1 SELECT * FROM rt1"); speedtest1_end_test(); - n = g.szTest*20; + n = g.szTest*200; speedtest1_begin_test(110, "%d one-dimensional intersect slice queries", n); speedtest1_prepare("SELECT count(*) FROM rt1 WHERE x0>=?1 AND x1<=?2"); iStep = mxCoord/n; @@ -1064,9 +2134,9 @@ void testset_rtree(int p1, int p2){ speedtest1_end_test(); if( g.bVerify ){ - n = g.szTest*20; + n = g.szTest*200; speedtest1_begin_test(111, "Verify result from 1-D intersect slice queries"); - speedtest1_prepare("SELECT count(*) FROM t1 WHERE x0>=?1 AND x1<=?2"); + speedtest1_prepare("SELECT count(*) FROM z1 WHERE x0>=?1 AND x1<=?2"); iStep = mxCoord/n; for(i=0; i<n; i++){ sqlite3_bind_int(g.pStmt, 1, i*iStep); @@ -1079,8 +2149,8 @@ void testset_rtree(int p1, int p2){ } speedtest1_end_test(); } - - n = g.szTest*20; + + n = g.szTest*200; speedtest1_begin_test(120, "%d one-dimensional overlap slice queries", n); speedtest1_prepare("SELECT count(*) FROM rt1 WHERE y1>=?1 AND y0<=?2"); iStep = mxCoord/n; @@ -1093,9 +2163,9 @@ void testset_rtree(int p1, int p2){ speedtest1_end_test(); if( g.bVerify ){ - n = g.szTest*20; + n = g.szTest*200; speedtest1_begin_test(121, "Verify result from 1-D overlap slice queries"); - speedtest1_prepare("SELECT count(*) FROM t1 WHERE y1>=?1 AND y0<=?2"); + speedtest1_prepare("SELECT count(*) FROM z1 WHERE y1>=?1 AND y0<=?2"); iStep = mxCoord/n; for(i=0; i<n; i++){ sqlite3_bind_int(g.pStmt, 1, i*iStep); @@ -1108,9 +2178,8 @@ void testset_rtree(int p1, int p2){ } speedtest1_end_test(); } - - n = g.szTest*20; + n = g.szTest*200; speedtest1_begin_test(125, "%d custom geometry callback queries", n); sqlite3_rtree_geometry_callback(g.db, "xslice", xsliceGeometryCallback, 0); speedtest1_prepare("SELECT count(*) FROM rt1 WHERE id MATCH xslice(?1,?2)"); @@ -1126,7 +2195,7 @@ void testset_rtree(int p1, int p2){ } speedtest1_end_test(); - n = g.szTest*80; + n = g.szTest*400; speedtest1_begin_test(130, "%d three-dimensional intersect box queries", n); speedtest1_prepare("SELECT count(*) FROM rt1 WHERE x1>=?1 AND x0<=?2" " AND y1>=?1 AND y0<=?2 AND z1>=?1 AND z0<=?2"); @@ -1139,7 +2208,7 @@ void testset_rtree(int p1, int p2){ } speedtest1_end_test(); - n = g.szTest*100; + n = g.szTest*500; speedtest1_begin_test(140, "%d rowid queries", n); speedtest1_prepare("SELECT * FROM rt1 WHERE id=?1"); for(i=1; i<=n; i++){ @@ -1147,9 +2216,525 @@ void testset_rtree(int p1, int p2){ speedtest1_run(); } speedtest1_end_test(); + + n = g.szTest*50; + speedtest1_begin_test(150, "%d UPDATEs using rowid", n); + speedtest1_prepare("UPDATE rt1 SET x0=x0+100, x1=x1+100 WHERE id=?1"); + for(i=1; i<=n; i++){ + sqlite3_bind_int(g.pStmt, 1, (i*251)%mxRowid + 1); + speedtest1_run(); + } + speedtest1_end_test(); + + n = g.szTest*5; + speedtest1_begin_test(155, "%d UPDATEs using one-dimensional overlap", n); + speedtest1_prepare("UPDATE rt1 SET x0=x0-100, x1=x1-100" + " WHERE y1>=?1 AND y0<=?1+5"); + iStep = mxCoord/n; + for(i=0; i<n; i++){ + sqlite3_bind_int(g.pStmt, 1, i*iStep); + speedtest1_run(); + aCheck[i] = atoi(g.zResult); + } + speedtest1_end_test(); + + n = g.szTest*50; + speedtest1_begin_test(160, "%d DELETEs using rowid", n); + speedtest1_prepare("DELETE FROM rt1 WHERE id=?1"); + for(i=1; i<=n; i++){ + sqlite3_bind_int(g.pStmt, 1, (i*257)%mxRowid + 1); + speedtest1_run(); + } + speedtest1_end_test(); + + + n = g.szTest*5; + speedtest1_begin_test(165, "%d DELETEs using one-dimensional overlap", n); + speedtest1_prepare("DELETE FROM rt1 WHERE y1>=?1 AND y0<=?1+5"); + iStep = mxCoord/n; + for(i=0; i<n; i++){ + sqlite3_bind_int(g.pStmt, 1, i*iStep); + speedtest1_run(); + aCheck[i] = atoi(g.zResult); + } + speedtest1_end_test(); + + speedtest1_begin_test(170, "Restore deleted entries using INSERT OR IGNORE"); + speedtest1_exec("INSERT OR IGNORE INTO rt1 SELECT * FROM z1"); + speedtest1_end_test(); } #endif /* SQLITE_ENABLE_RTREE */ +/* +** A testset that does key/value storage on tables with many columns. +** This is the kind of workload generated by ORMs such as CoreData. +*/ +void testset_orm(void){ + unsigned i, j, n; + unsigned nRow; + unsigned x1, len; + char zNum[2000]; /* A number name */ + static const char zType[] = /* Types for all non-PK columns, in order */ + "IBBIIITIVVITBTBFBFITTFBTBVBVIFTBBFITFFVBIFIVBVVVBTVTIBBFFIVIBTB" + "TVTTFTVTVFFIITIFBITFTTFFFVBIIBTTITFTFFVVVFIIITVBBVFFTVVB"; + + nRow = n = g.szTest*250; + speedtest1_begin_test(100, "Fill %d rows", n); + speedtest1_exec( + "BEGIN;" + "CREATE TABLE ZLOOKSLIKECOREDATA (" + " ZPK INTEGER PRIMARY KEY," + " ZTERMFITTINGHOUSINGCOMMAND INTEGER," + " ZBRIEFGOBYDODGERHEIGHT BLOB," + " ZCAPABLETRIPDOORALMOND BLOB," + " ZDEPOSITPAIRCOLLEGECOMET INTEGER," + " ZFRAMEENTERSIMPLEMOUTH INTEGER," + " ZHOPEFULGATEHOLECHALK INTEGER," + " ZSLEEPYUSERGRANDBOWL TIMESTAMP," + " ZDEWPEACHCAREERCELERY INTEGER," + " ZHANGERLITHIUMDINNERMEET VARCHAR," + " ZCLUBRELEASELIZARDADVICE VARCHAR," + " ZCHARGECLICKHUMANEHIRE INTEGER," + " ZFINGERDUEPIZZAOPTION TIMESTAMP," + " ZFLYINGDOCTORTABLEMELODY BLOB," + " ZLONGFINLEAVEIMAGEOIL TIMESTAMP," + " ZFAMILYVISUALOWNERMATTER BLOB," + " ZGOLDYOUNGINITIALNOSE FLOAT," + " ZCAUSESALAMITERMCYAN BLOB," + " ZSPREADMOTORBISCUITBACON FLOAT," + " ZGIFTICEFISHGLUEHAIR INTEGER," + " ZNOTICEPEARPOLICYJUICE TIMESTAMP," + " ZBANKBUFFALORECOVERORBIT TIMESTAMP," + " ZLONGDIETESSAYNATURE FLOAT," + " ZACTIONRANGEELEGANTNEUTRON BLOB," + " ZCADETBRIGHTPLANETBANK TIMESTAMP," + " ZAIRFORGIVEHEADFROG BLOB," + " ZSHARKJUSTFRUITMOVIE VARCHAR," + " ZFARMERMORNINGMIRRORCONCERN BLOB," + " ZWOODPOETRYCOBBLERBENCH VARCHAR," + " ZHAFNIUMSCRIPTSALADMOTOR INTEGER," + " ZPROBLEMCLUBPOPOVERJELLY FLOAT," + " ZEIGHTLEADERWORKERMOST TIMESTAMP," + " ZGLASSRESERVEBARIUMMEAL BLOB," + " ZCLAMBITARUGULAFAJITA BLOB," + " ZDECADEJOYOUSWAVEHABIT FLOAT," + " ZCOMPANYSUMMERFIBERELF INTEGER," + " ZTREATTESTQUILLCHARGE TIMESTAMP," + " ZBROWBALANCEKEYCHOWDER FLOAT," + " ZPEACHCOPPERDINNERLAKE FLOAT," + " ZDRYWALLBEYONDBROWNBOWL VARCHAR," + " ZBELLYCRASHITEMLACK BLOB," + " ZTENNISCYCLEBILLOFFICER INTEGER," + " ZMALLEQUIPTHANKSGLUE FLOAT," + " ZMISSREPLYHUMANLIVING INTEGER," + " ZKIWIVISUALPRIDEAPPLE VARCHAR," + " ZWISHHITSKINMOTOR BLOB," + " ZCALMRACCOONPROGRAMDEBIT VARCHAR," + " ZSHINYASSISTLIVINGCRAB VARCHAR," + " ZRESOLVEWRISTWRAPAPPLE VARCHAR," + " ZAPPEALSIMPLESECONDHOUSING BLOB," + " ZCORNERANCHORTAPEDIVER TIMESTAMP," + " ZMEMORYREQUESTSOURCEBIG VARCHAR," + " ZTRYFACTKEEPMILK TIMESTAMP," + " ZDIVERPAINTLEATHEREASY INTEGER," + " ZSORTMISTYQUOTECABBAGE BLOB," + " ZTUNEGASBUFFALOCAPITAL BLOB," + " ZFILLSTOPLAWJOYFUL FLOAT," + " ZSTEELCAREFULPLATENUMBER FLOAT," + " ZGIVEVIVIDDIVINEMEANING INTEGER," + " ZTREATPACKFUTURECONVERT VARCHAR," + " ZCALMLYGEMFINISHEFFECT INTEGER," + " ZCABBAGESOCKEASEMINUTE BLOB," + " ZPLANETFAMILYPUREMEMORY TIMESTAMP," + " ZMERRYCRACKTRAINLEADER BLOB," + " ZMINORWAYPAPERCLASSY TIMESTAMP," + " ZEAGLELINEMINEMAIL VARCHAR," + " ZRESORTYARDGREENLET TIMESTAMP," + " ZYARDOREGANOVIVIDJEWEL TIMESTAMP," + " ZPURECAKEVIVIDNEATLY FLOAT," + " ZASKCONTACTMONITORFUN TIMESTAMP," + " ZMOVEWHOGAMMAINCH VARCHAR," + " ZLETTUCEBIRDMEETDEBATE TIMESTAMP," + " ZGENENATURALHEARINGKITE VARCHAR," + " ZMUFFINDRYERDRAWFORTUNE FLOAT," + " ZGRAYSURVEYWIRELOVE FLOAT," + " ZPLIERSPRINTASKOREGANO INTEGER," + " ZTRAVELDRIVERCONTESTLILY INTEGER," + " ZHUMORSPICESANDKIDNEY TIMESTAMP," + " ZARSENICSAMPLEWAITMUON INTEGER," + " ZLACEADDRESSGROUNDCAREFUL FLOAT," + " ZBAMBOOMESSWASABIEVENING BLOB," + " ZONERELEASEAVERAGENURSE INTEGER," + " ZRADIANTWHENTRYCARD TIMESTAMP," + " ZREWARDINSIDEMANGOINTENSE FLOAT," + " ZNEATSTEWPARTIRON TIMESTAMP," + " ZOUTSIDEPEAHENCOUNTICE TIMESTAMP," + " ZCREAMEVENINGLIPBRANCH FLOAT," + " ZWHALEMATHAVOCADOCOPPER FLOAT," + " ZLIFEUSELEAFYBELL FLOAT," + " ZWEALTHLINENGLEEFULDAY VARCHAR," + " ZFACEINVITETALKGOLD BLOB," + " ZWESTAMOUNTAFFECTHEARING INTEGER," + " ZDELAYOUTCOMEHORNAGENCY INTEGER," + " ZBIGTHINKCONVERTECONOMY BLOB," + " ZBASEGOUDAREGULARFORGIVE TIMESTAMP," + " ZPATTERNCLORINEGRANDCOLBY TIMESTAMP," + " ZCYANBASEFEEDADROIT INTEGER," + " ZCARRYFLOORMINNOWDRAGON TIMESTAMP," + " ZIMAGEPENCILOTHERBOTTOM FLOAT," + " ZXENONFLIGHTPALEAPPLE TIMESTAMP," + " ZHERRINGJOKEFEATUREHOPEFUL FLOAT," + " ZCAPYEARLYRIVETBRUSH FLOAT," + " ZAGEREEDFROGBASKET VARCHAR," + " ZUSUALBODYHALIBUTDIAMOND VARCHAR," + " ZFOOTTAPWORDENTRY VARCHAR," + " ZDISHKEEPBLESTMONITOR FLOAT," + " ZBROADABLESOLIDCASUAL INTEGER," + " ZSQUAREGLEEFULCHILDLIGHT INTEGER," + " ZHOLIDAYHEADPONYDETAIL INTEGER," + " ZGENERALRESORTSKYOPEN TIMESTAMP," + " ZGLADSPRAYKIDNEYGUPPY VARCHAR," + " ZSWIMHEAVYMENTIONKIND BLOB," + " ZMESSYSULFURDREAMFESTIVE BLOB," + " ZSKYSKYCLASSICBRIEF VARCHAR," + " ZDILLASKHOKILEMON FLOAT," + " ZJUNIORSHOWPRESSNOVA FLOAT," + " ZSIZETOEAWARDFRESH TIMESTAMP," + " ZKEYFAILAPRICOTMETAL VARCHAR," + " ZHANDYREPAIRPROTONAIRPORT VARCHAR," + " ZPOSTPROTEINHANDLEACTOR BLOB" + ");" + ); + speedtest1_prepare( + "INSERT INTO ZLOOKSLIKECOREDATA(ZPK,ZAIRFORGIVEHEADFROG," + "ZGIFTICEFISHGLUEHAIR,ZDELAYOUTCOMEHORNAGENCY,ZSLEEPYUSERGRANDBOWL," + "ZGLASSRESERVEBARIUMMEAL,ZBRIEFGOBYDODGERHEIGHT," + "ZBAMBOOMESSWASABIEVENING,ZFARMERMORNINGMIRRORCONCERN," + "ZTREATPACKFUTURECONVERT,ZCAUSESALAMITERMCYAN,ZCALMRACCOONPROGRAMDEBIT," + "ZHOLIDAYHEADPONYDETAIL,ZWOODPOETRYCOBBLERBENCH,ZHAFNIUMSCRIPTSALADMOTOR," + "ZUSUALBODYHALIBUTDIAMOND,ZOUTSIDEPEAHENCOUNTICE,ZDIVERPAINTLEATHEREASY," + "ZWESTAMOUNTAFFECTHEARING,ZSIZETOEAWARDFRESH,ZDEWPEACHCAREERCELERY," + "ZSTEELCAREFULPLATENUMBER,ZCYANBASEFEEDADROIT,ZCALMLYGEMFINISHEFFECT," + "ZHANDYREPAIRPROTONAIRPORT,ZGENENATURALHEARINGKITE,ZBROADABLESOLIDCASUAL," + "ZPOSTPROTEINHANDLEACTOR,ZLACEADDRESSGROUNDCAREFUL,ZIMAGEPENCILOTHERBOTTOM," + "ZPROBLEMCLUBPOPOVERJELLY,ZPATTERNCLORINEGRANDCOLBY,ZNEATSTEWPARTIRON," + "ZAPPEALSIMPLESECONDHOUSING,ZMOVEWHOGAMMAINCH,ZTENNISCYCLEBILLOFFICER," + "ZSHARKJUSTFRUITMOVIE,ZKEYFAILAPRICOTMETAL,ZCOMPANYSUMMERFIBERELF," + "ZTERMFITTINGHOUSINGCOMMAND,ZRESORTYARDGREENLET,ZCABBAGESOCKEASEMINUTE," + "ZSQUAREGLEEFULCHILDLIGHT,ZONERELEASEAVERAGENURSE,ZBIGTHINKCONVERTECONOMY," + "ZPLIERSPRINTASKOREGANO,ZDECADEJOYOUSWAVEHABIT,ZDRYWALLBEYONDBROWNBOWL," + "ZCLUBRELEASELIZARDADVICE,ZWHALEMATHAVOCADOCOPPER,ZBELLYCRASHITEMLACK," + "ZLETTUCEBIRDMEETDEBATE,ZCAPABLETRIPDOORALMOND,ZRADIANTWHENTRYCARD," + "ZCAPYEARLYRIVETBRUSH,ZAGEREEDFROGBASKET,ZSWIMHEAVYMENTIONKIND," + "ZTRAVELDRIVERCONTESTLILY,ZGLADSPRAYKIDNEYGUPPY,ZBANKBUFFALORECOVERORBIT," + "ZFINGERDUEPIZZAOPTION,ZCLAMBITARUGULAFAJITA,ZLONGFINLEAVEIMAGEOIL," + "ZLONGDIETESSAYNATURE,ZJUNIORSHOWPRESSNOVA,ZHOPEFULGATEHOLECHALK," + "ZDEPOSITPAIRCOLLEGECOMET,ZWEALTHLINENGLEEFULDAY,ZFILLSTOPLAWJOYFUL," + "ZTUNEGASBUFFALOCAPITAL,ZGRAYSURVEYWIRELOVE,ZCORNERANCHORTAPEDIVER," + "ZREWARDINSIDEMANGOINTENSE,ZCADETBRIGHTPLANETBANK,ZPLANETFAMILYPUREMEMORY," + "ZTREATTESTQUILLCHARGE,ZCREAMEVENINGLIPBRANCH,ZSKYSKYCLASSICBRIEF," + "ZARSENICSAMPLEWAITMUON,ZBROWBALANCEKEYCHOWDER,ZFLYINGDOCTORTABLEMELODY," + "ZHANGERLITHIUMDINNERMEET,ZNOTICEPEARPOLICYJUICE,ZSHINYASSISTLIVINGCRAB," + "ZLIFEUSELEAFYBELL,ZFACEINVITETALKGOLD,ZGENERALRESORTSKYOPEN," + "ZPURECAKEVIVIDNEATLY,ZKIWIVISUALPRIDEAPPLE,ZMESSYSULFURDREAMFESTIVE," + "ZCHARGECLICKHUMANEHIRE,ZHERRINGJOKEFEATUREHOPEFUL,ZYARDOREGANOVIVIDJEWEL," + "ZFOOTTAPWORDENTRY,ZWISHHITSKINMOTOR,ZBASEGOUDAREGULARFORGIVE," + "ZMUFFINDRYERDRAWFORTUNE,ZACTIONRANGEELEGANTNEUTRON,ZTRYFACTKEEPMILK," + "ZPEACHCOPPERDINNERLAKE,ZFRAMEENTERSIMPLEMOUTH,ZMERRYCRACKTRAINLEADER," + "ZMEMORYREQUESTSOURCEBIG,ZCARRYFLOORMINNOWDRAGON,ZMINORWAYPAPERCLASSY," + "ZDILLASKHOKILEMON,ZRESOLVEWRISTWRAPAPPLE,ZASKCONTACTMONITORFUN," + "ZGIVEVIVIDDIVINEMEANING,ZEIGHTLEADERWORKERMOST,ZMISSREPLYHUMANLIVING," + "ZXENONFLIGHTPALEAPPLE,ZSORTMISTYQUOTECABBAGE,ZEAGLELINEMINEMAIL," + "ZFAMILYVISUALOWNERMATTER,ZSPREADMOTORBISCUITBACON,ZDISHKEEPBLESTMONITOR," + "ZMALLEQUIPTHANKSGLUE,ZGOLDYOUNGINITIALNOSE,ZHUMORSPICESANDKIDNEY)" + "VALUES(?1,?26,?20,?93,?8,?33,?3,?81,?28,?60,?18,?47,?109,?29,?30,?104,?86," + "?54,?92,?117,?9,?58,?97,?61,?119,?73,?107,?120,?80,?99,?31,?96,?85,?50,?71," + "?42,?27,?118,?36,?2,?67,?62,?108,?82,?94,?76,?35,?40,?11,?88,?41,?72,?4," + "?83,?102,?103,?112,?77,?111,?22,?13,?34,?15,?23,?116,?7,?5,?90,?57,?56," + "?75,?51,?84,?25,?63,?37,?87,?114,?79,?38,?14,?10,?21,?48,?89,?91,?110," + "?69,?45,?113,?12,?101,?68,?105,?46,?95,?74,?24,?53,?39,?6,?64,?52,?98," + "?65,?115,?49,?70,?59,?32,?44,?100,?55,?66,?16,?19,?106,?43,?17,?78);" + ); + for(i=0; i<n; i++){ + x1 = speedtest1_random(); + speedtest1_numbername(x1%1000, zNum, sizeof(zNum)); + len = (int)strlen(zNum); + sqlite3_bind_int(g.pStmt, 1, i^0xf); + for(j=0; zType[j]; j++){ + switch( zType[j] ){ + case 'I': + case 'T': + sqlite3_bind_int64(g.pStmt, j+2, x1); + break; + case 'F': + sqlite3_bind_double(g.pStmt, j+2, (double)x1); + break; + case 'V': + case 'B': + sqlite3_bind_text64(g.pStmt, j+2, zNum, len, + SQLITE_STATIC, SQLITE_UTF8); + break; + } + } + speedtest1_run(); + } + speedtest1_exec("COMMIT;"); + speedtest1_end_test(); + + n = g.szTest*250; + speedtest1_begin_test(110, "Query %d rows by rowid", n); + speedtest1_prepare( + "SELECT ZCYANBASEFEEDADROIT,ZJUNIORSHOWPRESSNOVA,ZCAUSESALAMITERMCYAN," + "ZHOPEFULGATEHOLECHALK,ZHUMORSPICESANDKIDNEY,ZSWIMHEAVYMENTIONKIND," + "ZMOVEWHOGAMMAINCH,ZAPPEALSIMPLESECONDHOUSING,ZHAFNIUMSCRIPTSALADMOTOR," + "ZNEATSTEWPARTIRON,ZLONGFINLEAVEIMAGEOIL,ZDEWPEACHCAREERCELERY," + "ZXENONFLIGHTPALEAPPLE,ZCALMRACCOONPROGRAMDEBIT,ZUSUALBODYHALIBUTDIAMOND," + "ZTRYFACTKEEPMILK,ZWEALTHLINENGLEEFULDAY,ZLONGDIETESSAYNATURE," + "ZLIFEUSELEAFYBELL,ZTREATPACKFUTURECONVERT,ZMEMORYREQUESTSOURCEBIG," + "ZYARDOREGANOVIVIDJEWEL,ZDEPOSITPAIRCOLLEGECOMET,ZSLEEPYUSERGRANDBOWL," + "ZBRIEFGOBYDODGERHEIGHT,ZCLUBRELEASELIZARDADVICE,ZCAPABLETRIPDOORALMOND," + "ZDRYWALLBEYONDBROWNBOWL,ZASKCONTACTMONITORFUN,ZKIWIVISUALPRIDEAPPLE," + "ZNOTICEPEARPOLICYJUICE,ZPEACHCOPPERDINNERLAKE,ZSTEELCAREFULPLATENUMBER," + "ZGLADSPRAYKIDNEYGUPPY,ZCOMPANYSUMMERFIBERELF,ZTENNISCYCLEBILLOFFICER," + "ZIMAGEPENCILOTHERBOTTOM,ZWESTAMOUNTAFFECTHEARING,ZDIVERPAINTLEATHEREASY," + "ZSKYSKYCLASSICBRIEF,ZMESSYSULFURDREAMFESTIVE,ZMERRYCRACKTRAINLEADER," + "ZBROADABLESOLIDCASUAL,ZGLASSRESERVEBARIUMMEAL,ZTUNEGASBUFFALOCAPITAL," + "ZBANKBUFFALORECOVERORBIT,ZTREATTESTQUILLCHARGE,ZBAMBOOMESSWASABIEVENING," + "ZREWARDINSIDEMANGOINTENSE,ZEAGLELINEMINEMAIL,ZCALMLYGEMFINISHEFFECT," + "ZKEYFAILAPRICOTMETAL,ZFINGERDUEPIZZAOPTION,ZCADETBRIGHTPLANETBANK," + "ZGOLDYOUNGINITIALNOSE,ZMISSREPLYHUMANLIVING,ZEIGHTLEADERWORKERMOST," + "ZFRAMEENTERSIMPLEMOUTH,ZBIGTHINKCONVERTECONOMY,ZFACEINVITETALKGOLD," + "ZPOSTPROTEINHANDLEACTOR,ZHERRINGJOKEFEATUREHOPEFUL,ZCABBAGESOCKEASEMINUTE," + "ZMUFFINDRYERDRAWFORTUNE,ZPROBLEMCLUBPOPOVERJELLY,ZGIVEVIVIDDIVINEMEANING," + "ZGENENATURALHEARINGKITE,ZGENERALRESORTSKYOPEN,ZLETTUCEBIRDMEETDEBATE," + "ZBASEGOUDAREGULARFORGIVE,ZCHARGECLICKHUMANEHIRE,ZPLANETFAMILYPUREMEMORY," + "ZMINORWAYPAPERCLASSY,ZCAPYEARLYRIVETBRUSH,ZSIZETOEAWARDFRESH," + "ZARSENICSAMPLEWAITMUON,ZSQUAREGLEEFULCHILDLIGHT,ZSHINYASSISTLIVINGCRAB," + "ZCORNERANCHORTAPEDIVER,ZDECADEJOYOUSWAVEHABIT,ZTRAVELDRIVERCONTESTLILY," + "ZFLYINGDOCTORTABLEMELODY,ZSHARKJUSTFRUITMOVIE,ZFAMILYVISUALOWNERMATTER," + "ZFARMERMORNINGMIRRORCONCERN,ZGIFTICEFISHGLUEHAIR,ZOUTSIDEPEAHENCOUNTICE," + "ZSPREADMOTORBISCUITBACON,ZWISHHITSKINMOTOR,ZHOLIDAYHEADPONYDETAIL," + "ZWOODPOETRYCOBBLERBENCH,ZAIRFORGIVEHEADFROG,ZBROWBALANCEKEYCHOWDER," + "ZDISHKEEPBLESTMONITOR,ZCLAMBITARUGULAFAJITA,ZPLIERSPRINTASKOREGANO," + "ZRADIANTWHENTRYCARD,ZDELAYOUTCOMEHORNAGENCY,ZPURECAKEVIVIDNEATLY," + "ZPATTERNCLORINEGRANDCOLBY,ZHANDYREPAIRPROTONAIRPORT,ZAGEREEDFROGBASKET," + "ZSORTMISTYQUOTECABBAGE,ZFOOTTAPWORDENTRY,ZRESOLVEWRISTWRAPAPPLE," + "ZDILLASKHOKILEMON,ZFILLSTOPLAWJOYFUL,ZACTIONRANGEELEGANTNEUTRON," + "ZRESORTYARDGREENLET,ZCREAMEVENINGLIPBRANCH,ZWHALEMATHAVOCADOCOPPER," + "ZGRAYSURVEYWIRELOVE,ZBELLYCRASHITEMLACK,ZHANGERLITHIUMDINNERMEET," + "ZCARRYFLOORMINNOWDRAGON,ZMALLEQUIPTHANKSGLUE,ZTERMFITTINGHOUSINGCOMMAND," + "ZONERELEASEAVERAGENURSE,ZLACEADDRESSGROUNDCAREFUL" + " FROM ZLOOKSLIKECOREDATA WHERE ZPK=?1;" + ); + for(i=0; i<n; i++){ + x1 = speedtest1_random()%nRow; + sqlite3_bind_int(g.pStmt, 1, x1); + speedtest1_run(); + } + speedtest1_end_test(); +} + +/* +*/ +void testset_trigger(void){ + int jj, ii; + char zNum[2000]; /* A number name */ + + const int NROW = 500*g.szTest; + const int NROW2 = 100*g.szTest; + + speedtest1_exec( + "BEGIN;" + "CREATE TABLE z1(rowid INTEGER PRIMARY KEY, i INTEGER, t TEXT);" + "CREATE TABLE z2(rowid INTEGER PRIMARY KEY, i INTEGER, t TEXT);" + "CREATE TABLE t3(rowid INTEGER PRIMARY KEY, i INTEGER, t TEXT);" + "CREATE VIEW v1 AS SELECT rowid, i, t FROM z1;" + "CREATE VIEW v2 AS SELECT rowid, i, t FROM z2;" + "CREATE VIEW v3 AS SELECT rowid, i, t FROM t3;" + ); + for(jj=1; jj<=3; jj++){ + speedtest1_prepare("INSERT INTO t%d VALUES(NULL,?1,?2)", jj); + for(ii=0; ii<NROW; ii++){ + int x1 = speedtest1_random() % NROW; + speedtest1_numbername(x1, zNum, sizeof(zNum)); + sqlite3_bind_int(g.pStmt, 1, x1); + sqlite3_bind_text(g.pStmt, 2, zNum, -1, SQLITE_STATIC); + speedtest1_run(); + } + } + speedtest1_exec( + "CREATE INDEX i1 ON z1(t);" + "CREATE INDEX i2 ON z2(t);" + "CREATE INDEX i3 ON t3(t);" + "COMMIT;" + ); + + speedtest1_begin_test(100, "speed4p-join1"); + speedtest1_prepare( + "SELECT * FROM z1, z2, t3 WHERE z1.oid = z2.oid AND z2.oid = t3.oid" + ); + speedtest1_run(); + speedtest1_end_test(); + + speedtest1_begin_test(110, "speed4p-join2"); + speedtest1_prepare( + "SELECT * FROM z1, z2, t3 WHERE z1.t = z2.t AND z2.t = t3.t" + ); + speedtest1_run(); + speedtest1_end_test(); + + speedtest1_begin_test(120, "speed4p-view1"); + for(jj=1; jj<=3; jj++){ + speedtest1_prepare("SELECT * FROM v%d WHERE rowid = ?", jj); + for(ii=0; ii<NROW2; ii+=3){ + sqlite3_bind_int(g.pStmt, 1, ii*3); + speedtest1_run(); + } + } + speedtest1_end_test(); + + speedtest1_begin_test(130, "speed4p-table1"); + for(jj=1; jj<=3; jj++){ + speedtest1_prepare("SELECT * FROM t%d WHERE rowid = ?", jj); + for(ii=0; ii<NROW2; ii+=3){ + sqlite3_bind_int(g.pStmt, 1, ii*3); + speedtest1_run(); + } + } + speedtest1_end_test(); + + speedtest1_begin_test(140, "speed4p-table1"); + for(jj=1; jj<=3; jj++){ + speedtest1_prepare("SELECT * FROM t%d WHERE rowid = ?", jj); + for(ii=0; ii<NROW2; ii+=3){ + sqlite3_bind_int(g.pStmt, 1, ii*3); + speedtest1_run(); + } + } + speedtest1_end_test(); + + speedtest1_begin_test(150, "speed4p-subselect1"); + speedtest1_prepare("SELECT " + "(SELECT t FROM z1 WHERE rowid = ?1)," + "(SELECT t FROM z2 WHERE rowid = ?1)," + "(SELECT t FROM t3 WHERE rowid = ?1)" + ); + for(jj=0; jj<NROW2; jj++){ + sqlite3_bind_int(g.pStmt, 1, jj*3); + speedtest1_run(); + } + speedtest1_end_test(); + + speedtest1_begin_test(160, "speed4p-rowid-update"); + speedtest1_exec("BEGIN"); + speedtest1_prepare("UPDATE z1 SET i=i+1 WHERE rowid=?1"); + for(jj=0; jj<NROW2; jj++){ + sqlite3_bind_int(g.pStmt, 1, jj); + speedtest1_run(); + } + speedtest1_exec("COMMIT"); + speedtest1_end_test(); + + speedtest1_exec("CREATE TABLE t5(t TEXT PRIMARY KEY, i INTEGER);"); + speedtest1_begin_test(170, "speed4p-insert-ignore"); + speedtest1_exec("INSERT OR IGNORE INTO t5 SELECT t, i FROM z1"); + speedtest1_end_test(); + + speedtest1_exec( + "CREATE TABLE log(op TEXT, r INTEGER, i INTEGER, t TEXT);" + "CREATE TABLE t4(rowid INTEGER PRIMARY KEY, i INTEGER, t TEXT);" + "CREATE TRIGGER t4_trigger1 AFTER INSERT ON t4 BEGIN" + " INSERT INTO log VALUES('INSERT INTO t4', new.rowid, new.i, new.t);" + "END;" + "CREATE TRIGGER t4_trigger2 AFTER UPDATE ON t4 BEGIN" + " INSERT INTO log VALUES('UPDATE OF t4', new.rowid, new.i, new.t);" + "END;" + "CREATE TRIGGER t4_trigger3 AFTER DELETE ON t4 BEGIN" + " INSERT INTO log VALUES('DELETE OF t4', old.rowid, old.i, old.t);" + "END;" + "BEGIN;" + ); + + speedtest1_begin_test(180, "speed4p-trigger1"); + speedtest1_prepare("INSERT INTO t4 VALUES(NULL, ?1, ?2)"); + for(jj=0; jj<NROW2; jj++){ + speedtest1_numbername(jj, zNum, sizeof(zNum)); + sqlite3_bind_int(g.pStmt, 1, jj); + sqlite3_bind_text(g.pStmt, 2, zNum, -1, SQLITE_STATIC); + speedtest1_run(); + } + speedtest1_end_test(); + + /* + ** Note: Of the queries, only half actually update a row. This property + ** was copied over from speed4p.test, where it was probably introduced + ** inadvertantly. + */ + speedtest1_begin_test(190, "speed4p-trigger2"); + speedtest1_prepare("UPDATE t4 SET i = ?1, t = ?2 WHERE rowid = ?3"); + for(jj=1; jj<=NROW2*2; jj+=2){ + speedtest1_numbername(jj*2, zNum, sizeof(zNum)); + sqlite3_bind_int(g.pStmt, 1, jj*2); + sqlite3_bind_text(g.pStmt, 2, zNum, -1, SQLITE_STATIC); + sqlite3_bind_int(g.pStmt, 3, jj); + speedtest1_run(); + } + speedtest1_end_test(); + + /* + ** Note: Same again. + */ + speedtest1_begin_test(200, "speed4p-trigger3"); + speedtest1_prepare("DELETE FROM t4 WHERE rowid = ?1"); + for(jj=1; jj<=NROW2*2; jj+=2){ + sqlite3_bind_int(g.pStmt, 1, jj*2); + speedtest1_run(); + } + speedtest1_end_test(); + speedtest1_exec("COMMIT"); + + /* + ** The following block contains the same tests as the above block that + ** tests triggers, with one crucial difference: no triggers are defined. + ** So the difference in speed between these tests and the preceding ones + ** is the amount of time taken to compile and execute the trigger programs. + */ + speedtest1_exec( + "DROP TABLE t4;" + "DROP TABLE log;" + "VACUUM;" + "CREATE TABLE t4(rowid INTEGER PRIMARY KEY, i INTEGER, t TEXT);" + "BEGIN;" + ); + speedtest1_begin_test(210, "speed4p-notrigger1"); + speedtest1_prepare("INSERT INTO t4 VALUES(NULL, ?1, ?2)"); + for(jj=0; jj<NROW2; jj++){ + speedtest1_numbername(jj, zNum, sizeof(zNum)); + sqlite3_bind_int(g.pStmt, 1, jj); + sqlite3_bind_text(g.pStmt, 2, zNum, -1, SQLITE_STATIC); + speedtest1_run(); + } + speedtest1_end_test(); + speedtest1_begin_test(210, "speed4p-notrigger2"); + speedtest1_prepare("UPDATE t4 SET i = ?1, t = ?2 WHERE rowid = ?3"); + for(jj=1; jj<=NROW2*2; jj+=2){ + speedtest1_numbername(jj*2, zNum, sizeof(zNum)); + sqlite3_bind_int(g.pStmt, 1, jj*2); + sqlite3_bind_text(g.pStmt, 2, zNum, -1, SQLITE_STATIC); + sqlite3_bind_int(g.pStmt, 3, jj); + speedtest1_run(); + } + speedtest1_end_test(); + speedtest1_begin_test(220, "speed4p-notrigger3"); + speedtest1_prepare("DELETE FROM t4 WHERE rowid = ?1"); + for(jj=1; jj<=NROW2*2; jj+=2){ + sqlite3_bind_int(g.pStmt, 1, jj*2); + speedtest1_run(); + } + speedtest1_end_test(); + speedtest1_exec("COMMIT"); +} + /* ** A testset used for debugging speedtest1 itself. */ @@ -1167,124 +2752,417 @@ void testset_debug1(void){ } } +/* +** Performance tests for JSON. +*/ +void testset_json(void){ + unsigned int r = 0x12345678; + sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SEED, r, g.db); + speedtest1_begin_test(100, "table J1 is %d rows of JSONB", + g.szTest*5); + speedtest1_exec( + "CREATE TABLE j1(x JSONB);\n" + "WITH RECURSIVE\n" + " jval(n,j) AS (\n" + " VALUES(0,'{}'),(1,'[]'),(2,'true'),(3,'false'),(4,'null'),\n" + " (5,'{x:1,y:2}'),(6,'0.0'),(7,'3.14159'),(8,'-99.9'),\n" + " (9,'[1,2,\"\\n\\u2192\\\"\\u2190\",4]')\n" + " ),\n" + " c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<26*26-1),\n" + " array1(y) AS MATERIALIZED (\n" + " SELECT jsonb_group_array(\n" + " jsonb_object('x',x,\n" + " 'y',jsonb(coalesce(j,random()%%10000)),\n" + " 'z',hex(randomblob(50)))\n" + " )\n" + " FROM c LEFT JOIN jval ON (x%%20)=n\n" + " ),\n" + " object1(z) AS MATERIALIZED (\n" + " SELECT jsonb_group_object(char(0x61+x%%26,0x61+(x/26)%%26),\n" + " jsonb( coalesce(j,random()%%10000)))\n" + " FROM c LEFT JOIN jval ON (x%%20)=n\n" + " ),\n" + " c2(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c2 WHERE n<%d)\n" + "INSERT INTO j1(x)\n" + " SELECT jsonb_object('a',n,'b',n+10000,'c',jsonb(y),'d',jsonb(z),\n" + " 'e',n+20000,'f',n+30000)\n" + " FROM array1, object1, c2;", + g.szTest*5 + ); + speedtest1_end_test(); + + speedtest1_begin_test(110, "table J2 is %d rows from J1 converted to text", g.szTest); + speedtest1_exec( + "CREATE TABLE j2(x JSON TEXT);\n" + "INSERT INTO j2(x) SELECT json(x) FROM j1 LIMIT %d", g.szTest + ); + speedtest1_end_test(); + + speedtest1_begin_test(120, "create indexes on JSON expressions on J1"); + speedtest1_exec( + "BEGIN;\n" + "CREATE INDEX j1x1 ON j1(x->>'a');\n" + "CREATE INDEX j1x2 ON j1(x->>'b');\n" + "CREATE INDEX j1x3 ON j1(x->>'f');\n" + "COMMIT;\n" + ); + speedtest1_end_test(); + + speedtest1_begin_test(130, "create indexes on JSON expressions on J2"); + speedtest1_exec( + "BEGIN;\n" + "CREATE INDEX j2x1 ON j2(x->>'a');\n" + "CREATE INDEX j2x2 ON j2(x->>'b');\n" + "CREATE INDEX j2x3 ON j2(x->>'f');\n" + "COMMIT;\n" + ); + speedtest1_end_test(); + + speedtest1_begin_test(140, "queries against J1"); + speedtest1_exec( + "WITH c(n) AS (VALUES(0) UNION ALL SELECT n+1 FROM c WHERE n<7)\n" + " SELECT sum(x->>format('$.c[%%d].x',n)) FROM c, j1;\n" + + "WITH c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<5)\n" + " SELECT sum(x->>format('$.\"c\"[#-%%d].y',n)) FROM c, j1;\n" + + "SELECT sum(x->>'$.d.ez' + x->>'$.d.\"xz\"' + x->>'a' + x->>'$.c[10].y') FROM j1;\n" + + "SELECT x->>'$.d.tz[2]', x->'$.d.tz' FROM j1;\n" + ); + speedtest1_end_test(); + + speedtest1_begin_test(141, "queries involving json_type()"); + speedtest1_exec( + "WITH c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<20)\n" + " SELECT json_type(x,format('$.c[#-%%d].y',n)), count(*)\n" + " FROM c, j1\n" + " WHERE j1.rowid=1\n" + " GROUP BY 1 ORDER BY 2;" + ); + speedtest1_end_test(); + + + speedtest1_begin_test(150, "json_insert()/set()/remove() on every row of J1"); + speedtest1_exec( + "BEGIN;\n" + "UPDATE j1 SET x=jsonb_insert(x,'$.g',(x->>'f')+1,'$.h',3.14159,'$.i','hello',\n" + " '$.j',json('{x:99}'),'$.k','{y:98}');\n" + "UPDATE j1 SET x=jsonb_set(x,'$.e',(x->>'f')-1);\n" + "UPDATE j1 SET x=jsonb_remove(x,'$.d');\n" + "COMMIT;\n" + ); + speedtest1_end_test(); + + speedtest1_begin_test(160, "json_insert()/set()/remove() on every row of J2"); + speedtest1_exec( + "BEGIN;\n" + "UPDATE j2 SET x=json_insert(x,'$.g',(x->>'f')+1);\n" + "UPDATE j2 SET x=json_set(x,'$.e',(x->>'f')-1);\n" + "UPDATE j2 SET x=json_remove(x,'$.d');\n" + "COMMIT;\n" + ); + speedtest1_end_test(); + +} + +/* +** This testset focuses on the speed of parsing numeric literals (integers +** and real numbers). This was added to test the impact of allowing "_" +** characters to appear in numeric SQL literals to make them easier to read. +** For example, "SELECT 1_000_000;" instead of "SELECT 1000000;". +*/ +void testset_parsenumber(void){ + const char *zSql1 = "SELECT 1, 12, 123, 1234, 12345, 123456"; + const char *zSql2 = "SELECT 8227256643844975616, 7932208612563860480, " + "2010730661871032832, 9138463067404021760, " + "2557616153664746496, 2557616153664746496"; + const char *zSql3 = "SELECT 1.0, 1.2, 1.23, 123.4, 1.2345, 1.23456"; + const char *zSql4 = "SELECT 8.227256643844975616, 7.932208612563860480, " + "2.010730661871032832, 9.138463067404021760, " + "2.557616153664746496, 2.557616153664746496"; + + const int NROW = 100*g.szTest; + int ii; + + speedtest1_begin_test(100, "parsing %d small integers", NROW); + for(ii=0; ii<NROW; ii++){ + sqlite3_exec(g.db, zSql1, 0, 0, 0); + } + speedtest1_end_test(); + + speedtest1_begin_test(110, "parsing %d large integers", NROW); + for(ii=0; ii<NROW; ii++){ + sqlite3_exec(g.db, zSql2, 0, 0, 0); + } + speedtest1_end_test(); + + speedtest1_begin_test(200, "parsing %d small reals", NROW); + for(ii=0; ii<NROW; ii++){ + sqlite3_exec(g.db, zSql3, 0, 0, 0); + } + speedtest1_end_test(); + + speedtest1_begin_test(210, "parsing %d large reals", NROW); + for(ii=0; ii<NROW; ii++){ + sqlite3_exec(g.db, zSql4, 0, 0, 0); + } + speedtest1_end_test(); +} + +#ifdef __linux__ +#include <sys/types.h> +#include <unistd.h> + +/* +** Attempt to display I/O stats on Linux using /proc/PID/io +*/ +static void displayLinuxIoStats(FILE *out){ + FILE *in; + char z[200]; + sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid()); + in = fopen(z, "rb"); + if( in==0 ) return; + while( fgets(z, sizeof(z), in)!=0 ){ + static const struct { + const char *zPattern; + const char *zDesc; + } aTrans[] = { + { "rchar: ", "Bytes received by read():" }, + { "wchar: ", "Bytes sent to write():" }, + { "syscr: ", "Read() system calls:" }, + { "syscw: ", "Write() system calls:" }, + { "read_bytes: ", "Bytes rcvd from storage:" }, + { "write_bytes: ", "Bytes sent to storage:" }, + { "cancelled_write_bytes: ", "Cancelled write bytes:" }, + }; + int i; + for(i=0; i<sizeof(aTrans)/sizeof(aTrans[0]); i++){ + int n = (int)strlen(aTrans[i].zPattern); + if( strncmp(aTrans[i].zPattern, z, n)==0 ){ + fprintf(out, "-- %-28s %s", aTrans[i].zDesc, &z[n]); + break; + } + } + } + fclose(in); +} +#endif + +#if SQLITE_VERSION_NUMBER<3006018 +# define sqlite3_sourceid(X) "(before 3.6.18)" +#endif + +#if SQLITE_CKSUMVFS_STATIC +int sqlite3_register_cksumvfs(const char*); +#endif + +static int xCompileOptions(void *pCtx, int nVal, char **azVal, char **azCol){ + printf("-- Compile option: %s\n", azVal[0]); + return SQLITE_OK; +} int main(int argc, char **argv){ int doAutovac = 0; /* True for --autovacuum */ int cacheSize = 0; /* Desired cache size. 0 means default */ int doExclusive = 0; /* True for --exclusive */ + int doFullFSync = 0; /* True for --fullfsync */ int nHeap = 0, mnHeap = 0; /* Heap size from --heap */ int doIncrvac = 0; /* True for --incrvacuum */ const char *zJMode = 0; /* Journal mode */ const char *zKey = 0; /* Encryption key */ - int nLook = 0, szLook = 0; /* --lookaside configuration */ + int nHardHeapLmt = 0; /* The hard heap limit */ + int nSoftHeapLmt = 0; /* The soft heap limit */ + int nLook = -1, szLook = 0; /* --lookaside configuration */ int noSync = 0; /* True for --nosync */ int pageSize = 0; /* Desired page size. 0 means default */ int nPCache = 0, szPCache = 0;/* --pcache configuration */ int doPCache = 0; /* True if --pcache is seen */ - int nScratch = 0, szScratch=0;/* --scratch configuration */ int showStats = 0; /* True for --stats */ int nThread = 0; /* --threads value */ - const char *zTSet = "main"; /* Which --testset torun */ + int mmapSize = 0; /* How big of a memory map to use */ + int memDb = 0; /* --memdb. Use an in-memory database */ + int openFlags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE + ; /* SQLITE_OPEN_xxx flags. */ + char *zTSet = "mix1"; /* Which --testset torun */ int doTrace = 0; /* True for --trace */ const char *zEncoding = 0; /* --utf16be or --utf16le */ - const char *zDbName = 0; /* Name of the test database */ void *pHeap = 0; /* Allocated heap space */ void *pLook = 0; /* Allocated lookaside space */ void *pPCache = 0; /* Allocated storage for pcache */ - void *pScratch = 0; /* Allocated storage for scratch */ int iCur, iHi; /* Stats values, current and "highwater" */ int i; /* Loop counter */ int rc; /* API return code */ + /* "mix1" is a macro testset: */ + static char zMix1Tests[] = + "main,orm/25,cte/20,json,fp/3,parsenumber/25,rtree/10,star" +#if !defined(SQLITE_SPEEDTEST1_WASM) + ",app" + /* This test misbehaves in WASM builds: sqlite3_open_v2() is + failing to find the db file for reasons not yet understood. */ +#endif + ; + +#ifdef SQLITE_SPEEDTEST1_WASM + /* Resetting all state is important for the WASM build, which may + ** call main() multiple times. */ + memset(&g, 0, sizeof(g)); + iTestNumber = 0; +#endif +#ifdef SQLITE_CKSUMVFS_STATIC + sqlite3_register_cksumvfs(0); +#endif + /* + ** Confirms that argc has at least N arguments following argv[i]. */ +#define ARGC_VALUE_CHECK(N) \ + if( i>=argc-(N) ) fatal_error("missing argument on %s\n", argv[i]) + /* Display the version of SQLite being tested */ + printf("-- Speedtest1 for SQLite %s %.48s\n", + sqlite3_libversion(), sqlite3_sourceid()); + /* Process command-line arguments */ + g.zDbName = 0; + g.zVfs = 0; g.zWR = ""; g.zNN = ""; g.zPK = "UNIQUE"; g.szTest = 100; + g.szBase = 100; + g.nRepeat = 1; for(i=1; i<argc; i++){ const char *z = argv[i]; if( z[0]=='-' ){ do{ z++; }while( z[0]=='-' ); if( strcmp(z,"autovacuum")==0 ){ doAutovac = 1; + }else if( strcmp(z,"big-transactions")==0 ){ + g.doBigTransactions = 1; }else if( strcmp(z,"cachesize")==0 ){ - if( i>=argc-1 ) fatal_error("missing argument on %s\n", argv[i]); - i++; - cacheSize = integerValue(argv[i]); + ARGC_VALUE_CHECK(1); + cacheSize = integerValue(argv[++i]); }else if( strcmp(z,"exclusive")==0 ){ doExclusive = 1; + }else if( strcmp(z,"fullfsync")==0 ){ + doFullFSync = 1; + }else if( strcmp(z,"checkpoint")==0 ){ + g.doCheckpoint = 1; }else if( strcmp(z,"explain")==0 ){ g.bSqlOnly = 1; g.bExplain = 1; + }else if( strcmp(z,"hard-heap-limit")==0 ){ + ARGC_VALUE_CHECK(1); + nHardHeapLmt = integerValue(argv[i+1]); + i += 1; }else if( strcmp(z,"heap")==0 ){ - if( i>=argc-2 ) fatal_error("missing arguments on %s\n", argv[i]); + ARGC_VALUE_CHECK(2); nHeap = integerValue(argv[i+1]); mnHeap = integerValue(argv[i+2]); i += 2; }else if( strcmp(z,"incrvacuum")==0 ){ doIncrvac = 1; }else if( strcmp(z,"journal")==0 ){ - if( i>=argc-1 ) fatal_error("missing argument on %s\n", argv[i]); + ARGC_VALUE_CHECK(1); zJMode = argv[++i]; }else if( strcmp(z,"key")==0 ){ - if( i>=argc-1 ) fatal_error("missing argument on %s\n", argv[i]); + ARGC_VALUE_CHECK(1); zKey = argv[++i]; }else if( strcmp(z,"lookaside")==0 ){ - if( i>=argc-2 ) fatal_error("missing arguments on %s\n", argv[i]); + ARGC_VALUE_CHECK(2); nLook = integerValue(argv[i+1]); szLook = integerValue(argv[i+2]); i += 2; + }else if( strcmp(z,"memdb")==0 ){ + memDb = 1; +#if SQLITE_VERSION_NUMBER>=3006000 }else if( strcmp(z,"multithread")==0 ){ sqlite3_config(SQLITE_CONFIG_MULTITHREAD); }else if( strcmp(z,"nomemstat")==0 ){ sqlite3_config(SQLITE_CONFIG_MEMSTATUS, 0); +#endif +#if SQLITE_VERSION_NUMBER>=3007017 + }else if( strcmp(z, "mmap")==0 ){ + ARGC_VALUE_CHECK(1); + mmapSize = integerValue(argv[++i]); + #endif + }else if( strcmp(z,"nomutex")==0 ){ + openFlags |= SQLITE_OPEN_NOMUTEX; }else if( strcmp(z,"nosync")==0 ){ noSync = 1; }else if( strcmp(z,"notnull")==0 ){ g.zNN = "NOT NULL"; -#ifdef SQLITE_ENABLE_RBU - }else if( strcmp(z,"rbu")==0 ){ - sqlite3ota_create_vfs("rbu", 0); - sqlite3_vfs_register(sqlite3_vfs_find("rbu"), 1); + }else if( strcmp(z,"output")==0 ){ +#ifdef SPEEDTEST_OMIT_HASH + fatal_error("The --output option is not supported with" + " -DSPEEDTEST_OMIT_HASH\n"); +#else + ARGC_VALUE_CHECK(1); + i++; + if( strcmp(argv[i],"-")==0 ){ + g.hashFile = stdout; + }else{ + g.hashFile = fopen(argv[i], "wb"); + if( g.hashFile==0 ){ + fatal_error("cannot open \"%s\" for writing\n", argv[i]); + } + } #endif }else if( strcmp(z,"pagesize")==0 ){ - if( i>=argc-1 ) fatal_error("missing argument on %s\n", argv[i]); + ARGC_VALUE_CHECK(1); pageSize = integerValue(argv[++i]); }else if( strcmp(z,"pcache")==0 ){ - if( i>=argc-2 ) fatal_error("missing arguments on %s\n", argv[i]); + ARGC_VALUE_CHECK(2); nPCache = integerValue(argv[i+1]); szPCache = integerValue(argv[i+2]); doPCache = 1; i += 2; }else if( strcmp(z,"primarykey")==0 ){ g.zPK = "PRIMARY KEY"; + }else if( strcmp(z,"repeat")==0 ){ + ARGC_VALUE_CHECK(1); + g.nRepeat = integerValue(argv[++i]); }else if( strcmp(z,"reprepare")==0 ){ g.bReprepare = 1; - }else if( strcmp(z,"scratch")==0 ){ - if( i>=argc-2 ) fatal_error("missing arguments on %s\n", argv[i]); - nScratch = integerValue(argv[i+1]); - szScratch = integerValue(argv[i+2]); - i += 2; +#if SQLITE_VERSION_NUMBER>=3006000 }else if( strcmp(z,"serialized")==0 ){ sqlite3_config(SQLITE_CONFIG_SERIALIZED); }else if( strcmp(z,"singlethread")==0 ){ sqlite3_config(SQLITE_CONFIG_SINGLETHREAD); +#endif + }else if( strcmp(z,"script")==0 ){ + ARGC_VALUE_CHECK(1); + if( g.pScript ) fclose(g.pScript); + g.pScript = fopen(argv[++i], "wb"); + if( g.pScript==0 ){ + fatal_error("unable to open output file \"%s\"\n", argv[i]); + } }else if( strcmp(z,"sqlonly")==0 ){ g.bSqlOnly = 1; }else if( strcmp(z,"shrink-memory")==0 ){ g.bMemShrink = 1; }else if( strcmp(z,"size")==0 ){ - if( i>=argc-1 ) fatal_error("missing argument on %s\n", argv[i]); - g.szTest = integerValue(argv[++i]); + ARGC_VALUE_CHECK(1); + g.szTest = g.szBase = integerValue(argv[++i]); + }else if( strcmp(z,"soft-heap-limit")==0 ){ + ARGC_VALUE_CHECK(1); + nSoftHeapLmt = integerValue(argv[i+1]); + i += 1; }else if( strcmp(z,"stats")==0 ){ showStats = 1; + }else if( strcmp(z,"temp")==0 ){ + ARGC_VALUE_CHECK(1); + i++; + if( argv[i][0]<'0' || argv[i][0]>'9' || argv[i][1]!=0 ){ + fatal_error("argument to --temp should be integer between 0 and 9"); + } + g.eTemp = argv[i][0] - '0'; }else if( strcmp(z,"testset")==0 ){ - if( i>=argc-1 ) fatal_error("missing argument on %s\n", argv[i]); + ARGC_VALUE_CHECK(1); zTSet = argv[++i]; }else if( strcmp(z,"trace")==0 ){ doTrace = 1; }else if( strcmp(z,"threads")==0 ){ - if( i>=argc-1 ) fatal_error("missing argument on %s\n", argv[i]); + ARGC_VALUE_CHECK(1); nThread = integerValue(argv[++i]); }else if( strcmp(z,"utf16le")==0 ){ zEncoding = "utf16le"; @@ -1292,9 +3170,34 @@ int main(int argc, char **argv){ zEncoding = "utf16be"; }else if( strcmp(z,"verify")==0 ){ g.bVerify = 1; +#ifndef SPEEDTEST_OMIT_HASH + HashInit(); +#endif + }else if( strcmp(z,"vfs")==0 ){ + ARGC_VALUE_CHECK(1); + g.zVfs = argv[++i]; + }else if( strcmp(z,"reserve")==0 ){ + ARGC_VALUE_CHECK(1); + g.nReserve = atoi(argv[++i]); + }else if( strcmp(z,"stmtscanstatus")==0 ){ + g.stmtScanStatus = 1; }else if( strcmp(z,"without-rowid")==0 ){ - g.zWR = "WITHOUT ROWID"; + if( strstr(g.zWR,"WITHOUT")!=0 ){ + /* no-op */ + }else if( strstr(g.zWR,"STRICT")!=0 ){ + g.zWR = "WITHOUT ROWID,STRICT"; + }else{ + g.zWR = "WITHOUT ROWID"; + } g.zPK = "PRIMARY KEY"; + }else if( strcmp(z,"strict")==0 ){ + if( strstr(g.zWR,"STRICT")!=0 ){ + /* no-op */ + }else if( strstr(g.zWR,"WITHOUT")!=0 ){ + g.zWR = "WITHOUT ROWID,STRICT"; + }else{ + g.zWR = "STRICT"; + } }else if( strcmp(z, "help")==0 || strcmp(z,"?")==0 ){ printf(zHelp, argv[0]); exit(0); @@ -1302,18 +3205,14 @@ int main(int argc, char **argv){ fatal_error("unknown option: %s\nUse \"%s -?\" for help\n", argv[i], argv[0]); } - }else if( zDbName==0 ){ - zDbName = argv[i]; + }else if( g.zDbName==0 ){ + g.zDbName = argv[i]; }else{ fatal_error("surplus argument: %s\nUse \"%s -?\" for help\n", argv[i], argv[0]); } } -#if 0 - if( zDbName==0 ){ - fatal_error(zHelp, argv[0]); - } -#endif +#undef ARGC_VALUE_CHECK #if SQLITE_VERSION_NUMBER>=3006001 if( nHeap>0 ){ pHeap = malloc( nHeap ); @@ -1330,33 +3229,56 @@ int main(int argc, char **argv){ rc = sqlite3_config(SQLITE_CONFIG_PAGECACHE, pPCache, szPCache, nPCache); if( rc ) fatal_error("pcache configuration failed: %d\n", rc); } - if( nScratch>0 && szScratch>0 ){ - pScratch = malloc( nScratch*(sqlite3_int64)szScratch ); - if( pScratch==0 ) fatal_error("cannot allocate %lld-byte scratch\n", - nScratch*(sqlite3_int64)szScratch); - rc = sqlite3_config(SQLITE_CONFIG_SCRATCH, pScratch, szScratch, nScratch); - if( rc ) fatal_error("scratch configuration failed: %d\n", rc); - } - if( nLook>0 ){ + if( nLook>=0 ){ sqlite3_config(SQLITE_CONFIG_LOOKASIDE, 0, 0); } #endif - + sqlite3_initialize(); + + if( g.zDbName!=0 ){ + sqlite3_vfs *pVfs = sqlite3_vfs_find(g.zVfs); + /* For some VFSes, e.g. opfs, unlink() is not sufficient. Use the + ** selected (or default) VFS's xDelete method to delete the + ** database. This is specifically important for the "opfs" VFS + ** when running from a WASM build of speedtest1, so that the db + ** can be cleaned up properly. For historical compatibility, we'll + ** also simply unlink(). */ + if( pVfs!=0 ){ + pVfs->xDelete(pVfs, g.zDbName, 1); + } + unlink(g.zDbName); + } + /* Open the database and the input file */ - if( sqlite3_open(zDbName, &g.db) ){ - fatal_error("Cannot open database file: %s\n", zDbName); + if( sqlite3_open_v2(memDb ? ":memory:" : g.zDbName, &g.db, + openFlags, g.zVfs) ){ + fatal_error("Cannot open database file: %s\n", g.zDbName); } #if SQLITE_VERSION_NUMBER>=3006001 if( nLook>0 && szLook>0 ){ pLook = malloc( nLook*szLook ); - rc = sqlite3_db_config(g.db, SQLITE_DBCONFIG_LOOKASIDE, pLook, szLook,nLook); + rc = sqlite3_db_config(g.db, SQLITE_DBCONFIG_LOOKASIDE,pLook,szLook,nLook); if( rc ) fatal_error("lookaside configuration failed: %d\n", rc); } #endif + if( g.nReserve>0 ){ + sqlite3_file_control(g.db, 0, SQLITE_FCNTL_RESERVE_BYTES, &g.nReserve); + } + if( g.stmtScanStatus ){ + sqlite3_db_config(g.db, SQLITE_DBCONFIG_STMT_SCANSTATUS, 1, 0); + } /* Set database connection options */ sqlite3_create_function(g.db, "random", 0, SQLITE_UTF8, 0, randomFunc, 0, 0); +#ifndef SQLITE_OMIT_DEPRECATED if( doTrace ) sqlite3_trace(g.db, traceCallback, 0); +#endif + if( memDb>0 ){ + speedtest1_exec("PRAGMA temp_store=memory"); + } + if( mmapSize>0 ){ + speedtest1_exec("PRAGMA mmap_size=%d", mmapSize); + } speedtest1_exec("PRAGMA threads=%d", nThread); if( zKey ){ speedtest1_exec("PRAGMA key('%s')", zKey); @@ -1375,34 +3297,119 @@ int main(int argc, char **argv){ if( cacheSize ){ speedtest1_exec("PRAGMA cache_size=%d", cacheSize); } - if( noSync ) speedtest1_exec("PRAGMA synchronous=OFF"); + if( noSync ){ + speedtest1_exec("PRAGMA synchronous=OFF"); + }else if( doFullFSync ){ + speedtest1_exec("PRAGMA fullfsync=ON"); + } if( doExclusive ){ speedtest1_exec("PRAGMA locking_mode=EXCLUSIVE"); } if( zJMode ){ speedtest1_exec("PRAGMA journal_mode=%s", zJMode); } + if( nHardHeapLmt>0 ){ + speedtest1_exec("PRAGMA hard_heap_limit=%d", nHardHeapLmt); + } + if( nSoftHeapLmt>0 ){ + speedtest1_exec("PRAGMA soft_heap_limit=%d", nSoftHeapLmt); + } + if( zJMode ){ + speedtest1_exec("PRAGMA journal_mode=%s", zJMode); + } if( g.bExplain ) printf(".explain\n.echo on\n"); - if( strcmp(zTSet,"main")==0 ){ - testset_main(); - }else if( strcmp(zTSet,"debug1")==0 ){ - testset_debug1(); - }else if( strcmp(zTSet,"cte")==0 ){ - testset_cte(); - }else if( strcmp(zTSet,"rtree")==0 ){ + if( strcmp(zTSet,"mix1")==0 ) zTSet = zMix1Tests; + do{ + char *zThisTest = zTSet; + char *zSep; + char *zComma = strchr(zThisTest,','); + if( zComma ){ + *zComma = 0; + zTSet = zComma+1; + }else{ + zTSet = ""; + } + zSep = strchr(zThisTest, '/'); + if( zSep ){ + int kk; + for(kk=1; zSep[kk] && ISDIGIT(zSep[kk]); kk++){} + if( kk==1 || zSep[kk]!=0 ){ + fatal_error("bad modifier on testset name: \"%s\"", zThisTest); + } + g.szTest = g.szBase*integerValue(zSep+1)/100; + if( g.szTest<=0 ) g.szTest = 1; + zSep[0] = 0; + }else{ + g.szTest = g.szBase; + } + if( g.iTotal>0 || zComma==0 ){ + printf(" Begin testset \"%s\"\n", zThisTest); + } + if( strcmp(zThisTest,"main")==0 ){ + testset_main(); + }else if( strcmp(zThisTest,"debug1")==0 ){ + testset_debug1(); + }else if( strcmp(zThisTest,"orm")==0 ){ + testset_orm(); + }else if( strcmp(zThisTest,"cte")==0 ){ + testset_cte(); + }else if( strcmp(zThisTest,"star")==0 ){ + testset_star(); + }else if( strcmp(zThisTest,"app")==0 ){ + testset_app(); + }else if( strcmp(zThisTest,"fp")==0 ){ + testset_fp(); + }else if( strcmp(zThisTest,"json")==0 ){ + testset_json(); + }else if( strcmp(zThisTest,"trigger")==0 ){ + testset_trigger(); + }else if( strcmp(zThisTest,"parsenumber")==0 ){ + testset_parsenumber(); + }else if( strcmp(zThisTest,"rtree")==0 ){ #ifdef SQLITE_ENABLE_RTREE - testset_rtree(6, 147); + testset_rtree(6, 147); #else - fatal_error("compile with -DSQLITE_ENABLE_RTREE to enable " - "the R-Tree tests\n"); + fatal_error("compile with -DSQLITE_ENABLE_RTREE to enable " + "the R-Tree tests\n"); #endif - }else{ - fatal_error("unknown testset: \"%s\"\nChoices: main debug1 cte rtree\n", - zTSet); - } + }else{ + fatal_error("unknown testset: \"%s\"\n" + "Choices: cte debug1 fp main orm rtree trigger\n", + zThisTest); + } + if( zTSet[0] ){ + char *zSql, *zObj; + speedtest1_begin_test(999, "Reset the database"); + while( 1 ){ + zObj = speedtest1_once( + "SELECT name FROM main.sqlite_master" + " WHERE sql LIKE 'CREATE %%TABLE%%'"); + if( zObj==0 ) break; + zSql = sqlite3_mprintf("DROP TABLE main.\"%w\"", zObj); + speedtest1_exec(zSql); + sqlite3_free(zSql); + sqlite3_free(zObj); + } + while( 1 ){ + zObj = speedtest1_once( + "SELECT name FROM temp.sqlite_master" + " WHERE sql LIKE 'CREATE %%TABLE%%'"); + if( zObj==0 ) break; + zSql = sqlite3_mprintf("DROP TABLE main.\"%w\"", zObj); + speedtest1_exec(zSql); + sqlite3_free(zSql); + sqlite3_free(zObj); + } + speedtest1_end_test(); + } + }while( zTSet[0] ); speedtest1_final(); + if( showStats ){ + sqlite3_exec(g.db, "PRAGMA compile_options", xCompileOptions, 0, 0); + } + /* Database connection statistics printed after both prepared statements ** have been finalized */ #if SQLITE_VERSION_NUMBER>=3007009 @@ -1446,21 +3453,35 @@ int main(int argc, char **argv){ #endif sqlite3_status(SQLITE_STATUS_PAGECACHE_OVERFLOW, &iCur, &iHi, 0); printf("-- Pcache Overflow Bytes: %d (max %d)\n", iCur,iHi); - sqlite3_status(SQLITE_STATUS_SCRATCH_OVERFLOW, &iCur, &iHi, 0); - printf("-- Scratch Overflow Bytes: %d (max %d)\n", iCur,iHi); sqlite3_status(SQLITE_STATUS_MALLOC_SIZE, &iCur, &iHi, 0); printf("-- Largest Allocation: %d bytes\n",iHi); sqlite3_status(SQLITE_STATUS_PAGECACHE_SIZE, &iCur, &iHi, 0); printf("-- Largest Pcache Allocation: %d bytes\n",iHi); - sqlite3_status(SQLITE_STATUS_SCRATCH_SIZE, &iCur, &iHi, 0); - printf("-- Largest Scratch Allocation: %d bytes\n", iHi); } #endif +#ifdef __linux__ + if( showStats ){ + displayLinuxIoStats(stdout); + } +#endif + if( g.pScript ){ + fclose(g.pScript); + } + /* Release memory */ free( pLook ); free( pPCache ); - free( pScratch ); free( pHeap ); return 0; } + +#ifdef SQLITE_SPEEDTEST1_WASM +/* +** A workaround for some inconsistent behaviour with how +** main() does (or does not) get exported to WASM. +*/ +int wasm_main(int argc, char **argv){ + return main(argc, argv); +} +#endif diff --git a/test/spellfix.test b/test/spellfix.test index 8128bb59d2..68bcfd5adb 100644 --- a/test/spellfix.test +++ b/test/spellfix.test @@ -279,7 +279,7 @@ ifcapable trace { do_tracesql_test 6.2.3 { SELECT word, distance FROM t3 WHERE rowid = 10 AND word MATCH 'kiiner'; } {keener 300 - {SELECT id, word, rank, k1 FROM "main"."t3_vocab" WHERE langid=0 AND k2>=?1 AND k2<?2} + {SELECT id, word, rank, coalesce(k1,word) FROM "main"."t3_vocab" WHERE langid=0 AND k2>=?1 AND k2<?2} } } diff --git a/test/spellfix3.test b/test/spellfix3.test index ce002edd4f..e24ea31ee7 100644 --- a/test/spellfix3.test +++ b/test/spellfix3.test @@ -35,9 +35,18 @@ do_execsql_test 140 { } {160} do_execsql_test 200 { SELECT spellfix1_scriptcode('+3.14159'); -} {999} +} {215} do_execsql_test 210 { SELECT spellfix1_scriptcode('And God said: "Да будет свет"'); } {998} +do_execsql_test 220 { + SELECT spellfix1_scriptcode('+3.14159 light'); +} {215} +do_execsql_test 230 { + SELECT spellfix1_scriptcode('+3.14159 свет'); +} {220} +do_execsql_test 240 { + SELECT spellfix1_scriptcode('וַיֹּ֥אמֶר +3.14159'); +} {125} finish_test diff --git a/test/spellfix4.test b/test/spellfix4.test new file mode 100644 index 0000000000..caf6d5139a --- /dev/null +++ b/test/spellfix4.test @@ -0,0 +1,353 @@ +# 2018-02-14 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# Test cases for the editdist3() function in the spellfix extension. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix spellfix4 + +ifcapable !vtab { finish_test ; return } + +load_static_extension db spellfix + +do_execsql_test 100 { + CREATE TABLE cost1(iLang, cFrom, cTo, iCost); + INSERT INTO cost1 VALUES + (0, '', '?', 97), + (0, '?', '', 98), + (0, '?', '?', 99), + (0, 'm', 'n', 50), + (0, 'n', 'm', 50) + ; + SELECT editdist3('cost1'); + SELECT editdist3('anchor','amchor'); +} {{} 50} +do_execsql_test 110 { + SELECT editdist3('anchor','anchoxr'); +} {97} +do_execsql_test 111 { + SELECT editdist3('anchor','xanchor'); +} {97} +do_execsql_test 112 { + SELECT editdist3('anchor','anchorx'); +} {97} +do_execsql_test 120 { + SELECT editdist3('anchor','anchr'); +} {98} +do_execsql_test 121 { + SELECT editdist3('anchor','ancho'); +} {98} +do_execsql_test 122 { + SELECT editdist3('anchor','nchor'); +} {98} +do_execsql_test 130 { + SELECT editdist3('anchor','anchur'); +} {99} +do_execsql_test 131 { + SELECT editdist3('anchor','onchor'); +} {99} +do_execsql_test 132 { + SELECT editdist3('anchor','anchot'); +} {99} +do_execsql_test 140 { + SELECT editdist3('anchor','omchor'); +} {149} + +do_execsql_test 200 { + INSERT INTO cost1 VALUES + (0, 'a', 'ä', 5), + (0, 'ss', 'ß', 8) + ; + SELECT editdist3('cost1'); + SELECT editdist3('strasse','straße'); + SELECT editdist3('straße','strasse'); +} {{} 8 196} +do_execsql_test 210 { + SELECT editdist3('baume','bäume'); +} {5} +do_execsql_test 220 { + SELECT editdist3('baum','bäume'); +} {102} +do_execsql_test 230 { + INSERT INTO cost1 VALUES + (0, 'ä', 'a', 5), + (0, 'ß', 'ss', 8) + ; + SELECT editdist3('cost1'); + SELECT editdist3('strasse','straße'); + SELECT editdist3('straße','strasse'); +} {{} 8 8} + +do_execsql_test 300 { + DELETE FROM cost1; + INSERT INTO cost1 VALUES + (0, '', '?', 97), + (0, '?', '', 98), + (0, '?', '?', 99), + (0, 'a', 'e', 50), + (0, 'a', 'i', 70), + (0, 'a', 'o', 75), + (0, 'a', 'u', 81), + (0, 'e', 'a', 50), + (0, 'e', 'i', 52), + (0, 'e', 'o', 72), + (0, 'e', 'u', 82), + (0, 'i', 'a', 70), + (0, 'i', 'e', 52), + (0, 'i', 'o', 75), + (0, 'i', 'u', 83), + (0, 'o', 'a', 75), + (0, 'o', 'e', 72), + (0, 'o', 'i', 75), + (0, 'o', 'u', 40), + (0, 'u', 'a', 81), + (0, 'u', 'e', 82), + (0, 'u', 'i', 83), + (0, 'u', 'o', 40), + (0, 'm', 'n', 45), + (0, 'n', 'm', 45) + ; + CREATE TABLE words(x TEXT); + INSERT INTO words VALUES + ('abraham'), + ('action'), + ('africa'), + ('aladdin'), + ('alert'), + ('alien'), + ('amazon'), + ('analog'), + ('animal'), + ('apollo'), + ('archive'), + ('arnold'), + ('aspirin'), + ('august'), + ('average'), + ('bahama'), + ('bambino'), + ('barcode'), + ('bazooka'), + ('belgium'), + ('between'), + ('biology'), + ('blonde'), + ('border'), + ('brave'), + ('british'), + ('bucket'), + ('button'), + ('caesar'), + ('camilla'), + ('cannon'), + ('caramel'), + ('carpet'), + ('catalog'), + ('century'), + ('chaos'), + ('chef'), + ('china'), + ('circus'), + ('classic'), + ('clinic'), + ('coconut'), + ('combine'), + ('complex'), + ('congo'), + ('convert'), + ('cosmos'), + ('crack'), + ('crown'), + ('cyclone'), + ('deal'), + ('delete'), + ('denver'), + ('detail'), + ('diana'), + ('direct'), + ('dolby'), + ('double'), + ('dublin'), + ('echo'), + ('edition'), + ('electra'), + ('emotion'), + ('enjoy'), + ('escape'), + ('everest'), + ('exile'), + ('express'), + ('family'), + ('ferrari'), + ('filter'), + ('fish'), + ('florida'), + ('ford'), + ('forum'), + ('frank'), + ('frozen'), + ('gallery'), + ('garlic'), + ('geneva'), + ('gibson'), + ('gloria'), + ('gordon'), + ('gravity'), + ('ground'), + ('habitat'), + ('harlem'), + ('hazard'), + ('herbert'), + ('hobby'), + ('house'), + ('icon'), + ('immune'), + ('india'), + ('inside'), + ('isotope'), + ('jamaica'), + ('jazz'), + ('joker'), + ('juliet'), + ('jupiter'), + ('kevin'), + ('korea'), + ('latin'), + ('legal'), + ('lexicon'), + ('limbo'), + ('lithium'), + ('logo'), + ('lucas'), + ('madrid'), + ('major'), + ('manual'), + ('mars'), + ('maximum'), + ('medical'), + ('mental'), + ('meter'), + ('miguel'), + ('mimosa'), + ('miranda'), + ('modern'), + ('money'), + ('morgan'), + ('motor'), + ('mystic'), + ('nebula'), + ('network'), + ('nice'), + ('nitro'), + ('norway'), + ('nurse'), + ('octavia'), + ('olympic'), + ('opus'), + ('orient'), + ('othello'), + ('pacific'), + ('panama'), + ('paper'), + ('parking'), + ('pasta'), + ('paul'), + ('people'), + ('permit'), + ('phrase'), + ('pilgrim'), + ('planet'), + ('pocket'), + ('police'), + ('popular'), + ('prefer'), + ('presto'), + ('private'), + ('project'), + ('proxy'), + ('python'), + ('quota'), + ('rainbow'), + ('raymond'), + ('region'), + ('report'), + ('reward'), + ('risk'), + ('robot'), + ('rose'), + ('russian'), + ('sailor'), + ('salt'), + ('saturn'), + ('scorpio'), + ('second'), + ('seminar'), + ('shadow'), + ('shave'), + ('shock'), + ('silence'), + ('sinatra'), + ('sleep'), + ('social'), + ('sonata'), + ('spain'), + ('sphere'), + ('spray'), + ('state'), + ('stone'), + ('strong'), + ('sugar'), + ('supreme'), + ('swing'), + ('talent'), + ('telecom'), + ('thermos'), + ('tina'), + ('tommy'), + ('torso'), + ('trade'), + ('trick'), + ('tropic'), + ('turtle'), + ('uniform'), + ('user'), + ('vega'), + ('vertigo'), + ('village'), + ('visible'), + ('vocal'), + ('voyage'), + ('weekend'), + ('winter'), + ('year'), + ('zipper') + ; + SELECT editdist3('cost1'); +} {{}} +do_execsql_test 310 { + SELECT editdist3(a.x,b.x), a.x, b.x + FROM words a, words b + WHERE a.x<b.x + ORDER BY 1, 2 + LIMIT 20 +} {139 bucket pocket 144 meter motor 149 manual mental 169 crack trick 173 sinatra sonata 174 edition emotion 174 major motor 174 risk rose 174 state stone 194 deal detail 196 alert talent 196 analog catalog 196 deal legal 196 ford forum 196 risk trick 196 stone strong 197 china tina 197 congo logo 197 diana tina 197 florida gloria} +do_execsql_test 320 { + SELECT md5sum(ed||'/'||sx||'/'||sy||',') FROM ( + SELECT editdist3(a.x,b.x) AS ed, a.x AS sx, b.x AS sy + FROM words a, words b + WHERE a.x<b.x + ORDER BY 1, 2 + ) +} {69d0a31872203a775e19325ea98cd053} + +finish_test diff --git a/test/sqlcipher-backup.test b/test/sqlcipher-backup.test new file mode 100644 index 0000000000..92058e07bd --- /dev/null +++ b/test/sqlcipher-backup.test @@ -0,0 +1,157 @@ +# SQLCipher +# codec.test developed by Stephen Lombardo (Zetetic LLC) +# sjlombardo at zetetic dot net +# http://zetetic.net +# +# Copyright (c) 2018, ZETETIC LLC +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of the ZETETIC LLC nor the +# names of its contributors may be used to endorse or promote products +# derived from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY +# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY +# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# +# This file implements regression tests for SQLite library. The +# focus of this script is testing code cipher features. +# +# NOTE: tester.tcl has overridden the definition of sqlite3 to +# automatically pass in a key value. Thus tests in this file +# should explicitly close and open db with sqlite_orig in order +# to bypass default key assignment. + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/sqlcipher.tcl + +# backup from plaintext to plaintext +# is allowed +do_test sqlcipher-backup-plain-plain { + sqlite_orig db test.db + set rc {} + execsql { + CREATE TABLE t1(a,b); + INSERT INTO t1 VALUES(1, randstr(16384,16384)); + } + + set md5a [execsql {SELECT md5sum(a,b) FROM t1}] + sqlite_orig db2 backup.db + sqlite3_backup B db2 main db main + lappend rc [B step -1] + lappend rc [B finish] + + db close + db2 close + + sqlite_orig db backup.db + + set md5b [execsql {SELECT md5sum(a,b) FROM t1}] + + lappend rc [ execsql { + PRAGMA integrity_check; + } ] + + lappend rc [string equal $md5a $md5b] +} {SQLITE_DONE SQLITE_OK ok 1} +db close +file delete -force test.db +file delete -force backup.db + +# backup from encrypted to encrypted +# is allowed +do_test sqlcipher-backup-encrypted-encrypted { + sqlite_orig db test.db + set rc {} + execsql { + PRAGMA key = 'testkey'; + CREATE TABLE t1(a,b); + INSERT INTO t1 VALUES(1, randstr(16384,16384)); + } + set md5a [execsql {SELECT md5sum(a,b) FROM t1}] + + sqlite_orig db2 backup.db + execsql { PRAGMA key = 'testkey' } db2; + + sqlite3_backup B db2 main db main + lappend rc [B step -1] + lappend rc [B finish] + + db close + db2 close + + sqlite_orig db backup.db + execsql { PRAGMA key = 'testkey' }; + + set md5b [execsql {SELECT md5sum(a,b) FROM t1}] + + lappend rc [ execsql { + PRAGMA integrity_check; + PRAGMA cipher_integrity_check; + } ] + + lappend rc [string equal $md5a $md5b] + +} {SQLITE_DONE SQLITE_OK ok 1} +db close +file delete -force test.db +file delete -force backup.db + +# backup from plaintext to encrypted +# is blocked +do_test sqlcipher-backup-plain-encrypted { + sqlite_orig db test.db + set rc {} + execsql { + CREATE TABLE t1(a,b); + INSERT INTO t1 VALUES(1, randstr(16384,16384)); + } + + sqlite_orig db2 backup.db + execsql { PRAGMA key = 'testkey' } db2; + + lappend rc [catch {sqlite3_backup B db2 main db main}] + lappend rc [sqlite3_errcode db2] + lappend rc [sqlite3_errmsg db2] +} {1 SQLITE_ERROR {backup is not supported with encrypted databases}} +db close +db2 close +file delete -force test.db +file delete -force backup.db + +# backup from encrypted to plaintext +# is blocked +do_test sqlcipher-backup-encrypted-plain { + sqlite_orig db test.db + set rc {} + execsql { + PRAGMA key = 'testkey'; + CREATE TABLE t1(a,b); + INSERT INTO t1 VALUES(1, randstr(16384,16384)); + } + + sqlite_orig db2 backup.db + + lappend rc [catch {sqlite3_backup B db2 main db main}] + lappend rc [sqlite3_errcode db2] + lappend rc [sqlite3_errmsg db2] +} {1 SQLITE_ERROR {backup is not supported with encrypted databases}} +db close +db2 close +file delete -force test.db +file delete -force backup.db + +finish_test diff --git a/test/sqlcipher-codecerror.test b/test/sqlcipher-codecerror.test new file mode 100644 index 0000000000..12cafbd79a --- /dev/null +++ b/test/sqlcipher-codecerror.test @@ -0,0 +1,171 @@ +# SQLCipher +# codec.test developed by Stephen Lombardo (Zetetic LLC) +# sjlombardo at zetetic dot net +# http://zetetic.net +# +# Copyright (c) 2018, ZETETIC LLC +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of the ZETETIC LLC nor the +# names of its contributors may be used to endorse or promote products +# derived from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY +# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY +# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# +# This file implements regression tests for SQLite library. The +# focus of this script is testing code cipher features. +# +# NOTE: tester.tcl has overridden the definition of sqlite3 to +# automatically pass in a key value. Thus tests in this file +# should explicitly close and open db with sqlite_orig in order +# to bypass default key assignment. + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/sqlcipher.tcl + +proc codec-test-setup {} { + sqlite_orig db test.db + + execsql { + PRAGMA key = 'testkey'; + CREATE table t1(a INTEGER PRIMARY KEY,b); + BEGIN; + } + + for {set i 1} {$i<=10000} {incr i} { + execsql "INSERT INTO t1(a,b) VALUES($i,'value $i');" + } + + execsql { + COMMIT; + } + + db close +} + + +do_test codec-error-journal-delete { + codec-test-setup + + sqlite_orig db test.db + + catchsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_test_on = fail_encrypt; + UPDATE t1 SET b = 'fail' WHERE a = 5000; + } + + db close + sqlite_orig db test.db + + execsql { + PRAGMA cipher_test_off = fail_encrypt; + PRAGMA key = 'testkey'; + PRAGMA cipher_integrity_check; + PRAGMA integrity_check; + SELECT b FROM t1 where a = 5000; + } + +} {ok ok {value 5000}} +db close +file delete -force test.db + +do_test codec-error-journal-wal { + codec-test-setup + + sqlite_orig db test.db + + catchsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_test_on = fail_encrypt; + UPDATE t1 SET b = 'fail' WHERE a = 5000; + } + + db close + sqlite_orig db test.db + + execsql { + PRAGMA cipher_test_off = fail_encrypt; + PRAGMA key = 'testkey'; + PRAGMA cipher_integrity_check; + PRAGMA integrity_check; + SELECT b FROM t1 where a = 5000; + } + +} {ok ok {value 5000}} +db close +file delete -force test.db + +do_test codec-error-journal-wal-transaction { + codec-test-setup + + sqlite_orig db test.db + + catchsql { + PRAGMA key = 'testkey'; + BEGIN; + UPDATE t1 SET b = 'success' WHERE a = 1; + PRAGMA cipher_test_on = fail_encrypt; + UPDATE t1 SET b = 'fail' WHERE a = 5000; + COMMIT; + } + + db close + sqlite_orig db test.db + + execsql { + PRAGMA cipher_test_off = fail_encrypt; + PRAGMA key = 'testkey'; + PRAGMA cipher_integrity_check; + PRAGMA integrity_check; + SELECT b FROM t1 where a = 1; + SELECT b FROM t1 where a = 5000; + } + +} {ok ok {value 1} {value 5000}} +db close +file delete -force test.db + +do_test codec-error-journal-wal-read { + codec-test-setup + + sqlite_orig db test.db + + catchsql { + PRAGMA key = 'testkey'; + SELECT count(*) FROM sqlite_schema; + PRAGMA cipher_test_on = fail_decrypt; + UPDATE t1 SET b = 'fail' WHERE a = 5000; + } + + db close + sqlite_orig db test.db + + execsql { + PRAGMA cipher_test_off = fail_decrypt; + PRAGMA key = 'testkey'; + PRAGMA cipher_integrity_check; + PRAGMA integrity_check; + SELECT b FROM t1 where a = 5000; + } + +} {ok ok {value 5000}} +db close +file delete -force test.db + +finish_test diff --git a/test/sqlcipher-compatibility.test b/test/sqlcipher-compatibility.test new file mode 100644 index 0000000000..656b1c4588 --- /dev/null +++ b/test/sqlcipher-compatibility.test @@ -0,0 +1,1519 @@ +# SQLCipher +# codec.test developed by Stephen Lombardo (Zetetic LLC) +# sjlombardo at zetetic dot net +# http://zetetic.net +# +# Copyright (c) 2018, ZETETIC LLC +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of the ZETETIC LLC nor the +# names of its contributors may be used to endorse or promote products +# derived from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY +# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY +# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# +# This file implements regression tests for SQLite library. The +# focus of this script is testing code cipher features. +# +# NOTE: tester.tcl has overridden the definition of sqlite3 to +# automatically pass in a key value. Thus tests in this file +# should explicitly close and open db with sqlite_orig in order +# to bypass default key assignment. + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/sqlcipher.tcl + +set old_pending_byte [sqlite3_test_control_pending_byte 0x40000000] + +# create an unencrypted database, attach a new encrypted volume +# copy data between, verify the encypted database is good afterwards +do_test unencrypted-attach { + sqlite_orig db test.db + + execsql { + CREATE TABLE t1(a,b); + BEGIN; + } + + for {set i 1} {$i<=1000} {incr i} { + set r [expr {int(rand()*500000)}] + execsql "INSERT INTO t1 VALUES($i,$r);" + } + + execsql { + COMMIT; + ATTACH DATABASE 'test2.db' AS db2 KEY 'testkey'; + CREATE TABLE db2.t1(a,b); + INSERT INTO db2.t1 SELECT * FROM t1; + DETACH DATABASE db2; + } + + sqlite_orig db2 test2.db + execsql { + PRAGMA key='testkey'; + SELECT count(*) FROM t1; + } db2 +} {ok 1000} +db close +db2 close +file delete -force test.db +file delete -force test2.db + +# create an unencrypted database, attach a new encrypted volume +# using a raw key copy data between, verify the encypted +# database is good afterwards +do_test unencrypted-attach-raw-key { + sqlite_orig db test.db + + execsql { + CREATE TABLE t1(a,b); + BEGIN; + } + + for {set i 1} {$i<=1000} {incr i} { + set r [expr {int(rand()*500000)}] + execsql "INSERT INTO t1 VALUES($i,$r);" + } + + execsql { + COMMIT; + ATTACH DATABASE 'test2.db' AS db2 KEY "x'10483C6EB40B6C31A448C22A66DED3B5E5E8D5119CAC8327B655C8B5C4836481'"; + CREATE TABLE db2.t1(a,b); + INSERT INTO db2.t1 SELECT * FROM t1; + DETACH DATABASE db2; + } + + sqlite_orig db2 test2.db + execsql { + PRAGMA key="x'10483C6EB40B6C31A448C22A66DED3B5E5E8D5119CAC8327B655C8B5C4836481'"; + SELECT count(*) FROM t1; + } db2 +} {ok 1000} +db close +db2 close +file delete -force test.db +file delete -force test2.db + +# open a 4.0 database +do_test compat-open-4.0-database { + sqlite_orig db $sampleDir/sqlcipher-4.0-testkey.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA integrity_check; + SELECT count(*) FROM t1; + } +} {ok ok 78536} +db close + +# create an encrypted database, attach an default-key encrypted volume +# copy data between, verify the second database +do_test encrypted-attach-default-key { + sqlite_orig db test.db + + execsql { + PRAGMA key='testkey'; + CREATE TABLE t1(a,b); + BEGIN; + } + + for {set i 1} {$i<=1000} {incr i} { + set r [expr {int(rand()*500000)}] + execsql "INSERT INTO t1 VALUES($i,$r);" + } + + execsql { + COMMIT; + ATTACH DATABASE 'test2.db' AS test; + CREATE TABLE test.t1(a,b); + INSERT INTO test.t1 SELECT * FROM t1; + DETACH DATABASE test; + } + + sqlite_orig db2 test2.db + + execsql { + PRAGMA key='testkey'; + SELECT count(*) FROM t1; + } db2 +} {ok 1000} +db close +db2 close +file delete -force test.db +file delete -force test2.db + +# create an encrypted database, attach an unencrypted volume +# copy data between, verify the unencypted database is good afterwards +do_test encrypted-attach-unencrypted { + sqlite_orig db test.db + + execsql { + CREATE TABLE t1(a,b); + } + + sqlite_orig db2 test2.db + execsql { + PRAGMA key = 'testkey'; + CREATE TABLE t1(a,b); + BEGIN; + } db2 + + for {set i 1} {$i<=1000} {incr i} { + set r [expr {int(rand()*500000)}] + execsql "INSERT INTO t1 VALUES($i,$r);" db2 + } + + execsql { + COMMIT; + ATTACH DATABASE 'test.db' AS test KEY ''; + INSERT INTO test.t1 SELECT * FROM t1; + DETACH DATABASE test; + } db2 + + execsql { + SELECT count(*) FROM t1; + } +} {1000} +db close +db2 close +file delete -force test.db +file delete -force test2.db + +# create an unencrypted database, attach an encrypted database +# then copy the data to it via sqlcipher_export and verify results +do_test unencrypted-to-encrypted-export { + sqlite_orig db test.db + + execsql { + CREATE TABLE t1(a,b); + BEGIN; + } + + for {set i 1} {$i<=1000} {incr i} { + set r [expr {int(rand()*500000)}] + execsql "INSERT INTO t1 VALUES($i,$r);" + } + + execsql { + COMMIT; + ATTACH DATABASE 'test2.db' AS test2 KEY 'testkey2'; + SELECT sqlcipher_export('test2'); + DETACH DATABASE test2; + } + db close + + sqlite_orig db test2.db + execsql { + PRAGMA key = 'testkey2'; + SELECT count(*) FROM t1; + } + + execsql { + SELECT count(*) FROM t1; + } +} {1000} +db close +file delete -force test.db +file delete -force test2.db + +do_test unencrypted-corrupt-to-encrypted-export { + sqlite_orig db test.db + + execsql { + CREATE TABLE t1(a,b); + INSERT INTO t1 VALUES (1,2); + + PRAGMA writable_schema = ON; + + UPDATE sqlite_schema SET sql = 'CREATE TABLE IF NOT EXISTS t1(a,b)' + WHERE tbl_name = 't1'; + + PRAGMA writable_schema = OFF; + INSERT INTO t1 VALUES (3,4); + + SELECT * FROM t1; + + ATTACH DATABASE 'test2.db' AS test2 KEY 'testkey2'; + + SELECT sqlcipher_export('test2'); + } + db close + + sqlite_orig db test2.db + execsql { + PRAGMA key = 'testkey2'; + SELECT count(*) FROM sqlite_schema; + SELECT count(*) FROM t1; + } +} {ok 1 2} +db close +file delete -force test.db +file delete -force test2.db + + +# create an encrypted database, attach an unencrypted database +# with data in it, then import the data back into the encrypted DB +# and verify +do_test unencrypted-to-encrypted-import { + sqlite_orig db test.db + + execsql { + CREATE TABLE t1(a,b); + BEGIN; + } + + for {set i 1} {$i<=1000} {incr i} { + set r [expr {int(rand()*500000)}] + execsql "INSERT INTO t1 VALUES($i,$r);" + } + + execsql { + COMMIT; + } + db close + + sqlite_orig db test2.db + + execsql { + PRAGMA key = 'testkey2'; + ATTACH DATABASE 'test.db' AS test KEY ''; + SELECT sqlcipher_export('main', 'test'); + DETACH DATABASE test; + } + db close + + sqlite_orig db test2.db + execsql { + PRAGMA key = 'testkey2'; + SELECT count(*) FROM t1; + } +} {ok 1000} +db close +file delete -force test.db +file delete -force test2.db + +# create an unencrypted database, attach an unencrypted volume +# copy data between, verify the unencypted database is good afterwards +do_test unencrypted-attach-unencrypted { + sqlite_orig db test.db + + execsql { + CREATE TABLE t1(a,b); + } + + sqlite_orig db2 test2.db + execsql { + CREATE TABLE t1(a,b); + BEGIN; + } db2 + + for {set i 1} {$i<=1000} {incr i} { + set r [expr {int(rand()*500000)}] + execsql "INSERT INTO t1 VALUES($i,$r);" db2 + } + + execsql { + COMMIT; + ATTACH DATABASE 'test.db' AS test; + INSERT INTO test.t1 SELECT * FROM t1; + DETACH DATABASE test; + } db2 + + execsql { + SELECT count(*) FROM t1; + } +} {1000} +db close +db2 close +file delete -force test.db +file delete -force test2.db + + +# open a 1.1.8 database using the new code, HMAC disabled +do_test open-1.1.8-database { + file copy -force $sampleDir/sqlcipher-1.1.8-testkey.db test.db + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_use_hmac = off; + PRAGMA kdf_iter = 4000; + PRAGMA cipher_page_size = 1024; + PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA1; + SELECT count(*) FROM t1; + SELECT distinct * FROM t1; + } +} {ok 75709 1 1 one one 1 2 one two 1 2} +db close +file delete -force test.db + +# open a 1.1.8 database without hmac, then copy the data +do_test attach-and-copy-1.1.8 { + file copy -force $sampleDir/sqlcipher-1.1.8-testkey.db test.db + sqlite_orig db test.db + + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_use_hmac = OFF; + PRAGMA kdf_iter = 4000; + PRAGMA cipher_page_size = 1024; + PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA1; + ATTACH DATABASE 'test-new.db' AS db2 KEY 'testkey-hmac'; + CREATE TABLE db2.t1(a,b); + INSERT INTO db2.t1 SELECT * FROM main.t1; + DETACH DATABASE db2; + } + db close + + sqlite_orig db test-new.db + execsql { + PRAGMA key = 'testkey-hmac'; + SELECT count(*) FROM t1; + SELECT distinct * FROM t1; + } +} {ok 75709 1 1 one one 1 2 one two 1 2} +db close +file delete -force test.db +file delete -force test-new.db + +# open a standard database, then attach a new +# database with completely different options. +# copy data between them, and verify that the +# new database can be opened with the proper data +do_test attached-database-pragmas { + sqlite_orig db test.db + + execsql { + PRAGMA key = 'testkey'; + CREATE TABLE t1(a,b); + BEGIN; + } + + for {set i 1} {$i<=1000} {incr i} { + set r [expr {int(rand()*500000)}] + execsql "INSERT INTO t1 VALUES($i,'value $r');" + } + + execsql { + COMMIT; + ATTACH DATABASE 'test2.db' AS db2 KEY 'testkey2'; + PRAGMA db2.cipher_page_size = 8192; + PRAGMA db2.kdf_iter = 1000; + PRAGMA db2.cipher_use_hmac = OFF; + CREATE TABLE db2.t1(a,b); + INSERT INTO db2.t1 SELECT * FROM main.t1; + DETACH DATABASE db2; + } + db close + + sqlite_orig db test2.db + execsql { + PRAGMA key = 'testkey2'; + PRAGMA cipher_page_size = 8192; + PRAGMA kdf_iter = 1000; + PRAGMA cipher_use_hmac = OFF; + SELECT count(*) FROM t1; + } +} {ok 1000} +db close +file delete -force test.db +file delete -force test2.db + +# use the sqlcipher_export function +# on a non-existent database. Verify +# the error gets through. +do_test export-error { + sqlite_orig db test.db + + catchsql { + PRAGMA key = 'testkey'; + CREATE TABLE t1(a,b); + SELECT sqlcipher_export('nodb'); + } +} {1 {unknown database nodb}} +db close +file delete -force test.db + +# verify sqlcipher_export with NULL parameters +do_test export-nulls { + sqlite_orig db test.db + + catchsql { + SELECT sqlcipher_export(NULL); + } + +} {1 {target database can't be NULL}} +db close +file delete -force test.db + +do_test export-nulls { + sqlite_orig db test.db + + catchsql { + SELECT sqlcipher_export('main', NULL); + } + +} {1 {target database can't be NULL}} +db close +file delete -force test.db + + +# use the sqlcipher_export function + +# use the sqlcipher_export function +# to copy a complicated database. +# tests autoincrement fields, +# indexes, views, and triggers, +# tables and virtual tables +do_test export-database { + sqlite_orig db test.db + + execsql { + PRAGMA key = 'testkey'; + CREATE TABLE t1(a INTEGER PRIMARY KEY AUTOINCREMENT, b, c); + CREATE UNIQUE INDEX b_idx ON t1(b); + CREATE INDEX c_idx ON t1(c); + + CREATE TABLE t2(b,c); + CREATE TRIGGER t2_after_insert AFTER INSERT ON t2 + BEGIN + INSERT INTO t1(b,c) VALUES (new.b, new.c); + END; + + CREATE VIEW v1 AS + SELECT c FROM t1; + + CREATE VIRTUAL TABLE fts USING fts5(a,b); + + BEGIN; + -- start with one known value + INSERT INTO t2 VALUES(1000000,'value 1000000'); + } + + for {set i 1} {$i<=999} {incr i} { + set r [expr {int(rand()*500000)}] + execsql "INSERT INTO t2 VALUES($i,'value $r');" + } + + execsql { + INSERT INTO fts SELECT b,c FROM t1; + COMMIT; + + ATTACH DATABASE 'test2.db' AS db2 KEY 'testkey2'; + PRAGMA db2.cipher_page_size = 8192; + + SELECT sqlcipher_export('db2'); + + DETACH DATABASE db2; + } + db close + + sqlite_orig db test2.db + execsql { + PRAGMA key = 'testkey2'; + PRAGMA cipher_page_size = 8192; + SELECT count(*) FROM t1; + SELECT count(*) FROM v1; + SELECT count(*) FROM sqlite_sequence; + SELECT seq FROM sqlite_sequence WHERE name = 't1'; + INSERT INTO t2 VALUES(10001, 'value 938383'); + SELECT count(*) FROM t1; -- verify the trigger worked + SELECT seq FROM sqlite_sequence WHERE name = 't1'; -- verify that autoincrement worked + SELECT a FROM fts WHERE b MATCH '1000000'; + } +} {ok 1000 1000 1 1000 1001 1001 1000000} +db close +file delete -force test.db +file delete -force test2.db + +# use the sqlcipher_export function +# to copy a complicated attached database to the main database +do_test export-attached-database { + sqlite_orig db test.db + + execsql { + PRAGMA key = 'testkey'; + CREATE TABLE t1(a INTEGER PRIMARY KEY AUTOINCREMENT, b, c); + CREATE UNIQUE INDEX b_idx ON t1(b); + CREATE INDEX c_idx ON t1(c); + + CREATE TABLE t2(b,c); + CREATE TRIGGER t2_after_insert AFTER INSERT ON t2 + BEGIN + INSERT INTO t1(b,c) VALUES (new.b, new.c); + END; + + CREATE VIEW v1 AS + SELECT c FROM t1; + + CREATE VIRTUAL TABLE fts USING fts5(a,b); + + BEGIN; + -- start with one known value + INSERT INTO t2 VALUES(1000000,'value 1000000'); + } + + for {set i 1} {$i<=999} {incr i} { + set r [expr {int(rand()*500000)}] + execsql "INSERT INTO t2 VALUES($i,'value $r');" + } + + execsql { + INSERT INTO fts SELECT b,c FROM t1; + COMMIT; + } + db close + + sqlite_orig db test2.db + execsql { + PRAGMA key = 'testkey2'; + + CREATE TABLE t3(a INTEGER PRIMARY KEY AUTOINCREMENT, b, c); + CREATE UNIQUE INDEX d_idx ON t3(b); + INSERT INTO t3(b,c) VALUES ('one', 'two'); + + ATTACH DATABASE 'test.db' AS db KEY 'testkey'; + + SELECT sqlcipher_export('main', 'db'); + + DETACH DATABASE db; + INSERT INTO t3(b,c) VALUES ('three', 'four'); + } + db close + + sqlite_orig db test2.db + execsql { + PRAGMA key = 'testkey2'; + SELECT count(*) FROM t1; + SELECT count(*) FROM v1; + SELECT count(*) FROM sqlite_sequence; + SELECT seq FROM sqlite_sequence WHERE name = 't1'; + INSERT INTO t2 VALUES(10001, 'value 938383'); + SELECT count(*) FROM t1; -- verify the trigger worked + SELECT seq FROM sqlite_sequence WHERE name = 't1'; -- verify that autoincrement worked + SELECT a FROM fts WHERE b MATCH '1000000'; + SELECT count(*) FROM t3; + } +} {ok 1000 1000 2 1000 1001 1001 1000000 2} +db close +file delete -force test.db +file delete -force test2.db + + +# open the database then insert a bunch of data. +# then delete it and run a manual vacuum +# verify that the file has become smaller +# but can still be opened with the proper +# key. also test vacuum into functionality introduced +# in sqlite 3.27.1 +do_test vacuum { + sqlite_orig db test.db + set rc {} + + execsql { + PRAGMA key = 'testkey'; + CREATE table t1(a,b); + BEGIN; + } + + for {set i 1} {$i<=10000} {incr i} { + set r [expr {int(rand()*500000)}] + execsql "INSERT INTO t1 VALUES($i,'value $r');" + } + + lappend rc [execsql { + COMMIT; + SELECT count(*) FROM t1; + }] + + # grab current size of file + set sz [file size test.db] + + execsql { + DELETE FROM t1 WHERE rowid > 5000; + VACUUM into 'test-vacuum.db'; + VACUUM; + } + db close + + # grab separate vacuum file size + set sz2 [file size test-vacuum.db] + + # grab test.db file size, post vacuum + set sz3 [file size test.db] + + # verify that the new size is + # smaller than the old size + if {$sz > $sz2} { lappend rc true } + if {$sz > $sz3} { lappend rc true } + + sqlite_orig db test-vacuum.db + lappend rc [execsql { + PRAGMA key = 'testkey'; + SELECT count(*) FROM t1; + }] + db close + + sqlite_orig db test.db + lappend rc [execsql { + PRAGMA key = 'testkey'; + SELECT count(*) FROM t1; + }] + +} {10000 true true {ok 5000} {ok 5000}} +db close +file delete -force test.db +file delete -force test-vacuum.db + +# open a 1.1.8 database (no HMAC, 4K iter), then +# try to open another 1.1.8 database. The +# attached database should have the same hmac +# setting as the original +do_test default-hmac-kdf-attach { + file copy -force $sampleDir/sqlcipher-1.1.8-testkey.db test.db + file copy -force $sampleDir/sqlcipher-1.1.8-testkey.db sqlcipher-1.1.8-testkey.db + sqlite_orig db test.db + execsql { + PRAGMA cipher_default_use_hmac = OFF; + PRAGMA cipher_default_kdf_iter = 4000; + PRAGMA cipher_default_page_size = 1024; + PRAGMA cipher_default_kdf_algorithm = PBKDF2_HMAC_SHA1; + PRAGMA key = 'testkey'; + SELECT count(*) FROM t1; + ATTACH 'sqlcipher-1.1.8-testkey.db' AS db2 KEY 'testkey'; + SELECT count(*) from db2.t1; + PRAGMA cipher_default_use_hmac = ON; + PRAGMA cipher_default_kdf_iter = 256000; + PRAGMA cipher_default_page_size = 4096; + PRAGMA cipher_default_kdf_algorithm = PBKDF2_HMAC_SHA512; + } +} {ok 75709 75709} +db close +file delete -force test.db +file delete -force sqlcipher-1.1.8-testkey.db + +# open a 2.0 database (with HMAC), then +# try to a 1.1.8 database. this should +# fail because the hmac setting for the +# attached database is not compatible +do_test attach-1.1.8-database-from-2.0-fails { + file copy -force $sampleDir/sqlcipher-1.1.8-testkey.db sqlcipher-1.1.8-testkey.db + sqlite_orig db test.db + catchsql { + PRAGMA key = 'testkey'; + CREATE table t1(a,b); + ATTACH 'sqlcipher-1.1.8-testkey.db' AS db2 KEY 'testkey'; + } +} {1 {file is not a database}} +db close +file delete -force test.db +file delete -force sqlcipher-1.1.8-testkey.db + +# open a 2.0 database (with HMAC, 4k iter), then +# set the default hmac setting to OFF. +# try to a 1.1.8 database. this should +# succeed now that hmac is off by default +# before the attach +do_test change-default-hmac-kdf-attach { + file copy -force $sampleDir/sqlcipher-1.1.8-testkey.db sqlcipher-1.1.8-testkey.db + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + CREATE table t1(a,b); + INSERT INTO t1(a,b) VALUES (1,2); + } + db close + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + SELECT count(*) FROM t1; + PRAGMA cipher_default_use_hmac = OFF; + PRAGMA cipher_default_kdf_iter = 4000; + PRAGMA cipher_default_page_size = 1024; + PRAGMA cipher_default_kdf_algorithm = PBKDF2_HMAC_SHA1; + ATTACH 'sqlcipher-1.1.8-testkey.db' AS db2 KEY 'testkey'; + SELECT count(*) from db2.t1; + PRAGMA cipher_default_use_hmac = ON; + PRAGMA cipher_default_kdf_iter = 256000; + PRAGMA cipher_default_page_size = 4096; + PRAGMA cipher_default_kdf_algorithm = PBKDF2_HMAC_SHA512; + } +} {ok 1 75709} +db close +file delete -force test.db +file delete -force sqlcipher-1.1.8-testkey.db + + +# create a new database, insert some data +# and delete some data with +# auto_vacuum on +do_test auto-vacuum-full { + sqlite_orig db test.db + + execsql { + PRAGMA key = 'test123'; + PRAGMA auto_vacuum = FULL; + CREATE TABLE t1(a,b); + BEGIN; + } + + for {set i 1} {$i<10000} {incr i} { + set r [expr {int(rand()*32767)}] + set r1 [expr {int(rand()*32767)}] + execsql "INSERT INTO t1 VALUES($r,$r1);" + } + set r [expr {int(rand()*32767)}] + execsql "DELETE FROM t1 WHERE a < $r;" + + execsql { + COMMIT; + PRAGMA integrity_check; + PRAGMA freelist_count; + SELECT (count(*) > 0) FROM t1; + } +} {ok 0 1} +db close +file delete -force test.db + +# create a new database, insert some data +# and delete some data with +# auto_vacuum incremental +do_test auto-vacuum-incremental { + sqlite_orig db test.db + + execsql { + PRAGMA key = 'test123'; + PRAGMA auto_vacuum = INCREMENTAL; + CREATE TABLE t1(a,b); + BEGIN; + } + + for {set i 1} {$i<10000} {incr i} { + set r [expr {int(rand()*32767)}] + set r1 [expr {int(rand()*32767)}] + execsql "INSERT INTO t1 VALUES($r,$r1);" + } + set r [expr {int(rand()*32767)}] + execsql "DELETE FROM t1 WHERE a < $r;" + + execsql { + COMMIT; + PRAGMA incremental_vacuum; + PRAGMA freelist_count; + PRAGMA integrity_check; + SELECT (count(*) > 0) FROM t1; + } +} {0 ok 1} +db close +file delete -force test.db + + +# create a database with many hundred tables such that the schema +# will overflow the first several pages of the database. verify the schema +# is intact on open. +do_test multipage-schema { + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + BEGIN EXCLUSIVE; + } db + + for {set i 1} {$i<=300} {incr i} { + execsql "CREATE TABLE tab$i (a TEXT, b TEXT, c TEXT, d TEXT, e TEXT, f TEXT, g TEXT, h TEXT, i TEXT, j TEXT, k, TEXT, l, m TEXT, n TEXT, o TEXT, p TEXT);" db + } + + execsql { + COMMIT; + } db + + db close + sqlite_orig db test.db + + execsql { + PRAGMA key = 'testkey'; + SELECT count(*) FROM sqlite_schema where type = 'table'; + } db + +} {ok 300} +db close +file delete -force test.db + +# create a database with many hundred tables such that the schema +# will overflow the first several pages of the database. this time, enable +# autovacuum on the database, which will cause sqlite to do some "short reads" +# after the end of the main database file. verify that there are no HMAC errors +# resulting from the short reads, and that the schema is intact when +# the database is reopened +do_test multipage-schema-autovacuum-shortread { + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA auto_vacuum = FULL; + BEGIN EXCLUSIVE; + } db + + for {set i 1} {$i<=300} {incr i} { + execsql "CREATE TABLE tab$i (a TEXT, b TEXT, c TEXT, d TEXT, e TEXT, f TEXT, g TEXT, h TEXT, i TEXT, j TEXT, k, TEXT, l, m TEXT, n TEXT, o TEXT, p TEXT);" db + } + + execsql { + COMMIT; + } db + + db close + sqlite_orig db test.db + + execsql { + PRAGMA key = 'testkey'; + SELECT count(*) FROM sqlite_schema where type = 'table'; + } db + +} {ok 300} +db close +file delete -force test.db + +# same as multi-page-schema-autovacuum-shortread, except +# using write ahead log mode +do_test multipage-schema-autovacuum-shortread-wal { + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA auto_vacuum = FULL; + PRAGMA journal_mode = WAL; + BEGIN EXCLUSIVE; + } db + + for {set i 1} {$i<=300} {incr i} { + execsql "CREATE TABLE tab$i (a TEXT, b TEXT, c TEXT, d TEXT, e TEXT, f TEXT, g TEXT, h TEXT, i TEXT, j TEXT, k, TEXT, l, m TEXT, n TEXT, o TEXT, p TEXT);" db + } + + execsql { + COMMIT; + } db + + db close + sqlite_orig db test.db + + execsql { + PRAGMA key = 'testkey'; + SELECT count(*) FROM sqlite_schema where type = 'table'; + } db +} {ok 300} +db close +file delete -force test.db + +# open a 3.0 database with little endian hmac page numbers (default) +# verify it can be opened +do_test open-3.0-le-database { + sqlite_orig db $sampleDir/sqlcipher-3.0-testkey.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_page_size = 1024; + PRAGMA kdf_iter = 64000; + PRAGMA cipher_hmac_algorithm = HMAC_SHA1; + PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA1; + SELECT count(*) FROM t1; + SELECT distinct * FROM t1; + } +} {ok 78536 1 1 one one 1 2 one two} +db close + +# open a 2.0 database with little endian hmac page numbers (default) +# verify it can be opened +do_test open-2.0-le-database { + sqlite_orig db $sampleDir/sqlcipher-2.0-le-testkey.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA kdf_iter = 4000; + PRAGMA cipher_page_size = 1024; + PRAGMA cipher_hmac_algorithm = HMAC_SHA1; + PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA1; + SELECT count(*) FROM t1; + SELECT distinct * FROM t1; + } +} {ok 78536 1 1 one one 1 2 one two} +db close + +# open a 2.0 database with big-endian hmac page numbers +# verify it can be opened +do_test open-2.0-be-database { + sqlite_orig db $sampleDir/sqlcipher-2.0-be-testkey.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_hmac_pgno = be; + PRAGMA kdf_iter = 4000; + PRAGMA cipher_page_size = 1024; + PRAGMA cipher_hmac_algorithm = HMAC_SHA1; + PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA1; + SELECT count(*) FROM t1; + SELECT distinct * FROM t1; + } +} {ok {PRAGMA cipher_hmac_pgno is deprecated, please remove from use} 78536 1 1 one one 1 2 one two} +db close + +# open a 2.0 database with big-endian hmac page numbers +# attach a new database with little endian page numbers (default) +# copy schema between the two, and verify the latter +# can be opened +do_test be-to-le-migration { + sqlite_orig db $sampleDir/sqlcipher-2.0-be-testkey.db + + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_hmac_pgno = be; + PRAGMA kdf_iter = 4000; + PRAGMA cipher_page_size = 1024; + PRAGMA cipher_hmac_algorithm = HMAC_SHA1; + PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA1; + ATTACH DATABASE 'test.db' AS db2 KEY 'testkey'; + CREATE TABLE db2.t1(a,b); + INSERT INTO db2.t1 SELECT * FROM main.t1; + DETACH DATABASE db2; + } + db close + + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + SELECT count(*) FROM t1; + SELECT distinct * FROM t1; + } +} {ok 78536 1 1 one one 1 2 one two} +db close +file delete -force test.db + + + +# open a 2.0 beta database with 4000 round hmac kdf and 0x00 +# hmac salt mask +# verify it can be opened +do_test open-2.0-beta-database { + sqlite_orig db $sampleDir/sqlcipher-2.0-beta-testkey.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA kdf_iter = 4000; + PRAGMA fast_kdf_iter = 4000; + PRAGMA cipher_hmac_salt_mask = "x'00'"; + PRAGMA cipher_page_size = 1024; + PRAGMA cipher_hmac_algorithm = HMAC_SHA1; + PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA1; + SELECT count(*) FROM t1; + SELECT distinct * FROM t1; + } +} {ok {PRAGMA fast_kdf_iter is deprecated, please remove from use} {PRAGMA cipher_hmac_salt_mask is deprecated, please remove from use} 38768 test-0-0 test-0-1 test-1-0 test-1-1} +db close + +# open a 2.0 beta database +# attach a new standard database +# copy schema between the two, and verify the latter +# can be opened +do_test 2.0-beta-to-2.0-migration { + sqlite_orig db $sampleDir/sqlcipher-2.0-beta-testkey.db + + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_hmac_salt_mask = "x'00'"; + PRAGMA kdf_iter = 4000; + PRAGMA fast_kdf_iter = 4000; + PRAGMA cipher_page_size = 1024; + PRAGMA cipher_hmac_algorithm = HMAC_SHA1; + PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA1; + SELECT count(*) FROM sqlite_schema; + + PRAGMA cipher_hmac_salt_mask = "x'3a'"; + ATTACH DATABASE 'test.db' AS db2 KEY 'testkey'; + + CREATE TABLE db2.t1(a,b); + INSERT INTO db2.t1 SELECT * FROM main.t1; + DETACH DATABASE db2; + } + db close + + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + SELECT distinct * FROM t1; + } +} {ok test-0-0 test-0-1 test-1-0 test-1-1} +db close +file delete -force test.db + +do_test migrate-1.1.8-database-to-current-format { + file copy -force $sampleDir/sqlcipher-1.1.8-testkey.db test.db + sqlite_orig db test.db + set rc {} + + lappend rc [execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_migrate; + SELECT count(*) FROM sqlite_schema; + }] + db close + + sqlite_orig db test.db + lappend rc [execsql { + PRAGMA key = 'testkey'; + SELECT count(*) FROM sqlite_schema; + PRAGMA journal_mode; + }] +} {{ok 0 1} {ok 1 delete}} +db close +file delete -force test.db + +do_test migrate-2-0-le-database-to-current-format { + file copy -force $sampleDir/sqlcipher-2.0-le-testkey.db test.db + sqlite_orig db test.db + set rc {} + + lappend rc [execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_migrate; + SELECT count(*) FROM sqlite_schema; + }] + db close + + sqlite_orig db test.db + lappend rc [execsql { + PRAGMA key = 'testkey'; + SELECT count(*) FROM sqlite_schema; + PRAGMA journal_mode; + }] +} {{ok 0 1} {ok 1 delete}} +db close +file delete -force test.db + +do_test migrate-3-0-database-to-current-format { + file copy -force $sampleDir/sqlcipher-3.0-testkey.db test.db + sqlite_orig db test.db + set rc {} + + lappend rc [execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_migrate; + SELECT count(*) FROM sqlite_schema; + }] + db close + + sqlite_orig db test.db + lappend rc [execsql { + PRAGMA key = 'testkey'; + SELECT count(*) FROM sqlite_schema; + PRAGMA journal_mode; + }] +} {{ok 0 1} {ok 1 delete}} +db close +file delete -force test.db + +do_test migrate-wal-database-to-current { + file copy -force $sampleDir/sqlcipher-3.0-testkey.db test.db + sqlite_orig db test.db + set rc {} + + lappend rc [execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_page_size = 1024; PRAGMA kdf_iter = 64000; PRAGMA cipher_hmac_algorithm = HMAC_SHA1; PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA1; + PRAGMA journal_mode = wal; + }] + db close + + sqlite_orig db test.db + lappend rc [execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_migrate; + PRAGMA journal_mode; + }] + db close + + sqlite_orig db test.db + lappend rc [execsql { + PRAGMA key = 'testkey'; + SELECT count(*) FROM sqlite_schema; + PRAGMA journal_mode; + }] +} {{ok wal} {ok 0 wal} {ok 1 wal}} +db close +file delete -force test.db + +# test original database is left untouched after +# a failed migration e.g. due to low disk space +do_test migrate-failure { + file copy -force $sampleDir/sqlcipher-3.0-testkey.db test.db + sqlite_orig db test.db + + set rc {} + + lappend rc [execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_test_on = fail_migrate; + PRAGMA cipher_migrate; + }] + db close + + lappend rc [file exists test.db-migrated] + + sqlite_orig db test.db + lappend rc [execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_test_off = fail_migrate; + PRAGMA cipher_compatibility = 3; + SELECT count(*) FROM sqlite_schema; + }] +} {{ok 1} 0 {ok 1}} +db close +file delete -force test.db + +# if a migration failes the database should be in a permanent error state +do_test migrate-failure-not-readable { + file copy -force $sampleDir/sqlcipher-3.0-testkey.db test.db + sqlite_orig db test.db + + set rc {} + lappend rc [execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_test_on = fail_migrate; + PRAGMA cipher_migrate; + }] + + lappend rc [catchsql { + SELECT count(*) FROM sqlite_schema; + }] + db close + + sqlite_orig db test.db + lappend rc [execsql { + PRAGMA cipher_test_off = fail_migrate; + PRAGMA cipher_test; + }] +} {{ok 1} {1 {out of memory}} 0} +db close +file delete -force test.db + +# if cipher_migrate is called on a current-version databse +# is should do nothing and just report OK +do_test migrate-current-format-noop { + file copy -force $sampleDir/sqlcipher-4.0-testkey.db test.db + sqlite_orig db test.db + + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_migrate; + SELECT count(*) FROM sqlite_schema; + } +} {ok 0 1} +db close +file delete -force test.db + +do_test key-database-by-name { + sqlite_orig db test.db + execsql { + attach database 'new.db' as new; + pragma new.key = 'foo'; + create table new.t1(a,b); + insert into new.t1(a,b) values('foo', 'bar'); + detach database new; + } + db close + + sqlite_orig db new.db + execsql { + pragma key = 'foo'; + select * from t1; + } +} {ok foo bar} +db close +file delete -force test.db +file delete -force new.db + +do_test key-multiple-databases-with-different-keys-using-pragma { + sqlite_orig db test.db + execsql { + pragma key = 'foobar'; + create table t1(a,b); + insert into t1(a,b) values('baz','qux'); + attach database 'new.db' as new; + pragma new.key = 'foo'; + create table new.t1(a,b); + insert into new.t1(a,b) values('foo', 'bar'); + detach database new; + } + db close + + sqlite_orig db new.db + execsql { + pragma key = 'foo'; + attach database 'test.db' as test key 'foobar'; + select * from t1; + select * from test.t1; + detach database test; + } +} {ok foo bar baz qux} +db close +file delete -force test.db +file delete -force new.db + + +# Requires SQLCipher to be built with -DSQLCIPHER_TEST +if_built_with_libtomcrypt verify-random-data-alters-file-content { + file delete -force test.db + file delete -force test2.db + file delete -force test3.db + set rc {} + + sqlite_orig db test.db + execsql { + PRAGMA key="x'2DD29CA851E7B56E4697B0E1F08507293D761A05CE4D1B628663F411A8086D99'"; + create table t1(a,b); + } + db close + sqlite_orig db test2.db + execsql { + PRAGMA key="x'2DD29CA851E7B56E4697B0E1F08507293D761A05CE4D1B628663F411A8086D99'"; + create table t1(a,b); + } + db close + sqlite_orig db test3.db + execsql { + PRAGMA key="x'2DD29CA851E7B56E4697B0E1F08507293D761A05CE4D1B628663F411A8086D99'"; + PRAGMA cipher_add_random = "x'deadbaad'"; + create table t1(a,b); + } + db close + lappend rc [cmpFilesChunked test.db test2.db] + lappend rc [cmpFilesChunked test2.db test3.db] +} {0 1} +db close +file delete -force test.db +file delete -force test2.db +file delete -force test3.db + +do_test can-migrate-with-keys-longer-than-64-characters { + sqlite_orig db test.db + execsql { + PRAGMA key = "012345678901234567890123456789012345678901234567890123456789012345"; + PRAGMA cipher_page_size = 1024; + PRAGMA kdf_iter = 4000; + PRAGMA cipher_hmac_algorithm = HMAC_SHA1; + PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA1; + PRAGMA user_version = 5; + } + db close + + sqlite_orig db test.db + execsql { + PRAGMA key = "012345678901234567890123456789012345678901234567890123456789012345"; + PRAGMA cipher_migrate; + } + db close + + sqlite_orig db test.db + execsql { + PRAGMA key = "012345678901234567890123456789012345678901234567890123456789012345"; + PRAGMA user_version; + } +} {ok 5} +db close +file delete -force test.db + +do_test can-migrate-with-raw-hex-key { + sqlite_orig db test.db + execsql { + PRAGMA key = "x'2DD29CA851E7B56E4697B0E1F08507293D761A05CE4D1B628663F411A8086D99'"; + PRAGMA cipher_page_size = 1024; + PRAGMA kdf_iter = 4000; + PRAGMA cipher_use_hmac = off; + PRAGMA user_version = 5; + } + db close + + sqlite_orig db test.db + execsql { + PRAGMA key = "x'2DD29CA851E7B56E4697B0E1F08507293D761A05CE4D1B628663F411A8086D99'"; + PRAGMA cipher_migrate; + } + db close + + sqlite_orig db test.db + execsql { + PRAGMA key = "x'2DD29CA851E7B56E4697B0E1F08507293D761A05CE4D1B628663F411A8086D99'"; + PRAGMA user_version; + } + +} {ok 5} +db close +file delete -force test.db + +do_test attach_database_with_non_default_page_size { + sqlite_orig db test2.db + execsql { + PRAGMA key = 'test'; + PRAGMA cipher_page_size = 8192; + CREATE TABLE t1(a,b); + INSERT INTO t1(a,b) values('one for the money', 'two for the show'); + INSERT INTO t1(a,b) values('three to get ready', 'now, go cat, go'); + } + db close + + sqlite_orig db test.db + execsql { + PRAGMA cipher_default_page_size = 8192; + PRAGMA key = 'test'; + ATTACH DATABASE 'test2.db' as test2 KEY 'test'; + SELECT count(*) FROM test2.t1; + PRAGMA cipher_default_page_size = 4096; + } +} {ok 2} +db close +file delete -force test.db test2.db + +do_test verify-cipher-export-with-trace-configured { + sqlite_orig db plain.db + execsql { + CREATE TABLE t1(a,b); + INSERT INTO t1(a,b) VALUES(1,2); + } + set TRACE_OUT {} + db trace trace_proc + execsql { + ATTACH DATABASE 'encrypted.db' AS encrypted KEY 'encrypted'; + SELECT sqlcipher_export('encrypted'); + DETACH DATABASE encrypted; + } + set TRACE_OUT +} {{ATTACH DATABASE 'encrypted.db' AS encrypted KEY 'encrypted';} {SELECT sqlcipher_export('encrypted');} {DETACH DATABASE encrypted;}} +set TRACE_OUT {} +db close +file delete -force plain.db +file delete -force encrypted.db + +# open a 1.1.8 database using cipher_compatibility +do_test compat-open-1.1.8-database { + sqlite_orig db $sampleDir/sqlcipher-1.1.8-testkey.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_compatibility = 1; + PRAGMA integrity_check; + SELECT count(*) FROM t1; + } +} {ok ok 75709} +db close + +# open a 2.0 database using cipher_compatibility +do_test compat-open-2.0-database { + sqlite_orig db $sampleDir/sqlcipher-2.0-le-testkey.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_compatibility = 2; + PRAGMA integrity_check; + SELECT count(*) FROM t1; + } +} {ok ok 78536} +db close + +# open a 3.0 database using cipher_compatibility +do_test compat-open-3.0-database { + sqlite_orig db $sampleDir/sqlcipher-3.0-testkey.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_compatibility = 3; + PRAGMA integrity_check; + SELECT count(*) FROM t1; + } +} {ok ok 78536} +db close + +# open a 4.0 database using cipher_compatibility +do_test compat-open-4.0-database { + sqlite_orig db $sampleDir/sqlcipher-4.0-testkey.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_compatibility = 4; + PRAGMA integrity_check; + SELECT count(*) FROM t1; + } +} {ok ok 78536} +db close + +# open a 1.1.8 database using cipher_default_compatibility +do_test default-compat-open-1.1.8-database { + sqlite_orig db $sampleDir/sqlcipher-1.1.8-testkey.db + execsql { + PRAGMA cipher_default_compatibility = 1; + PRAGMA key = 'testkey'; + PRAGMA integrity_check; + SELECT count(*) FROM t1; + } +} {ok ok 75709} +db close + +# open a 2.0 database using cipher_default_compatibility +do_test default-compat-open-2.0-database { + sqlite_orig db $sampleDir/sqlcipher-2.0-le-testkey.db + execsql { + PRAGMA cipher_default_compatibility = 2; + PRAGMA key = 'testkey'; + PRAGMA integrity_check; + SELECT count(*) FROM t1; + } +} {ok ok 78536} + +# open a 3.0 database using cipher_default_compatibility +do_test default-compat-open-3.0-database { + sqlite_orig db $sampleDir/sqlcipher-3.0-testkey.db + execsql { + PRAGMA cipher_default_compatibility = 3; + PRAGMA key = 'testkey'; + PRAGMA integrity_check; + SELECT count(*) FROM t1; + } +} {ok ok 78536} + +# re-open a 4.0 database using cipher_default_compatibility +do_test default-compat-open-4.0-database { + sqlite_orig db $sampleDir/sqlcipher-4.0-testkey.db + execsql { + PRAGMA cipher_default_compatibility = 4; + PRAGMA key = 'testkey'; + PRAGMA integrity_check; + SELECT count(*) FROM t1; + } +} {ok ok 78536} + +# create a database using a full keyspec consising of +# 64 characters for the encryption key, 64 for the hmac key +# and 32 for the salt +do_test test-full-keyspec { + sqlite_orig db test.db + execsql { + PRAGMA key = "x'0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'"; + CREATE TABLE t1(a,b); + INSERT INTO t1(a,b) VALUES (1,2); + } + db close + + sqlite_orig db test.db + execsql { + PRAGMA key = "x'0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'"; + SELECT count(*) FROM t1; + PRAGMA cipher_salt; + } + +} {ok 1 00000000000000000000000000000000} +db close +file delete -force test.db + +sqlite3_test_control_pending_byte $old_pending_byte + +finish_test diff --git a/test/sqlcipher-core.test b/test/sqlcipher-core.test new file mode 100644 index 0000000000..22179feb73 --- /dev/null +++ b/test/sqlcipher-core.test @@ -0,0 +1,1078 @@ +# SQLCipher +# codec.test developed by Stephen Lombardo (Zetetic LLC) +# sjlombardo at zetetic dot net +# http://zetetic.net +# +# Copyright (c) 2018, ZETETIC LLC +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of the ZETETIC LLC nor the +# names of its contributors may be used to endorse or promote products +# derived from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY +# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY +# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# +# This file implements regression tests for SQLite library. The +# focus of this script is testing code cipher features. +# +# NOTE: tester.tcl has overridden the definition of sqlite3 to +# automatically pass in a key value. Thus tests in this file +# should explicitly close and open db with sqlite_orig in order +# to bypass default key assignment. + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/sqlcipher.tcl + +# The database is initially empty. +# set an hex key create some basic data +# create table and insert operations should work +# close database, open it again with the same +# hex key. verify that the table is readable +# and the data just inserted is visible +setup test.db "\"x'98483C6EB40B6C31A448C22A66DED3B5E5E8D5119CAC8327B655C8B5C4836481'\"" +do_test will-open-with-correct-raw-key { + sqlite_orig db test.db + execsql { + PRAGMA key = "x'98483C6EB40B6C31A448C22A66DED3B5E5E8D5119CAC8327B655C8B5C4836481'"; + SELECT name FROM sqlite_schema WHERE type='table'; + SELECT * from t1; + } +} {ok t1 test1 test2} +db close +file delete -force test.db + +# set an encryption key (non-hex) and create some basic data +# create table and insert operations should work +# close database, open it again with the same +# key. verify that the table is readable +# and the data just inserted is visible +setup test.db "'testkey'" +do_test will-open-with-correct-derived-key { + + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + SELECT name FROM sqlite_schema WHERE type='table'; + SELECT * from t1; + } +} {ok t1 test1 test2} +db close +file delete -force test.db + +# run test with sqlite3_key function directly +setup test.db "'testkey'" +do_test will-open-with-sqlite3key { + + sqlite_orig db test.db + sqlite3_key db "testkey" + execsql { + SELECT name FROM sqlite_schema WHERE type='table'; + SELECT * from t1; + } +} {t1 test1 test2} +db close +file delete -force test.db + +# set an encryption key (non-hex) and create +# temp tables, verify you can read from +# sqlite_temp_master +setup test.db "'testkey'" +do_test test-temp-master { + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + CREATE TEMPORARY TABLE temp_t1(a,b); + INSERT INTO temp_t1(a,b) VALUES ('test1', 'test2'); + SELECT name FROM sqlite_temp_master WHERE type='table'; + SELECT * from temp_t1; + } +} {ok temp_t1 test1 test2} +db close +file delete -force test.db + +# verify that a when a standard database is encrypted the first +# 16 bytes are not "SQLite format 3\0" +do_test test-sqlcipher-header-overwrite { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + CREATE TABLE t1(a,b); + } + db close + set header [hexio_read test.db 0 16] + string equal $header "53514C69746520666F726D6174203300" +} {0} +file delete -force test.db + +# open the database and try to read from it without +# providing a passphrase. verify that the +# an error is returned from the library +setup test.db "'testkey'" +do_test wont-open-without-key { + sqlite_orig db test.db + catchsql { + SELECT name FROM sqlite_schema WHERE type='table'; + } +} {1 {file is not a database}} +db close +file delete -force test.db + +# open the database and try to set an invalid +# passphrase. verify that an error is returned +# and that data couldn't be read +setup test.db "'testkey'" +do_test wont-open-with-invalid-derived-key { + sqlite_orig db test.db + catchsql { + PRAGMA key = 'testkey2'; + SELECT name FROM sqlite_schema WHERE type='table'; + } +} {1 {file is not a database}} +db close +file delete -force test.db + +# open the database and try to set an invalid +# hex key. verify that an error is returned +# and that data couldn't be read +setup test.db "'testkey'" +do_test wont-open-with-invalid-raw-key { + sqlite_orig db test.db + catchsql { + PRAGMA key = "x'98483C6EB40B6C31A448C22A66DED3B5E5E8D5119CAC8327B655C8B5C4836480'"; + SELECT name FROM sqlite_schema WHERE type='table'; + } +} {1 {file is not a database}} +db close +file delete -force test.db + +# test a large number of inserts in a transaction to a memory database +do_test memory-database { + sqlite_orig db :memory: + execsql { + PRAGMA key = 'testkey3'; + BEGIN; + CREATE TABLE t2(a,b); + } + for {set i 1} {$i<=25000} {incr i} { + set r [expr {int(rand()*500000)}] + execsql "INSERT INTO t2 VALUES($i,$r);" + } + execsql { + COMMIT; + SELECT count(*) FROM t2; + DELETE FROM t2; + SELECT count(*) FROM t2; + } +} {25000 0} +db close + +# test a large number of inserts in a transaction for multiple pages +do_test multi-page-database { + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + CREATE TABLE t2(a,b); + BEGIN; + } + for {set i 1} {$i<=25000} {incr i} { + set r [expr {int(rand()*500000)}] + execsql "INSERT INTO t2 VALUES($i,$r);" + } + execsql { + COMMIT; + SELECT count(*) FROM t2; + } +} {25000} +db close +file delete -force test.db + +# attach an encrypted database +# without specifying key, verify it fails +# even if the source passwords are the same +# because the kdf salts are different +setup test.db "'testkey'" +do_test attach-database-with-default-key { + sqlite_orig db2 test2.db + set rc {} + + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_add_random = "x'deadbaad'"; + CREATE TABLE t2(a,b); + INSERT INTO t2 VALUES ('test1', 'test2'); + } db2 + + lappend rc [catchsql { + ATTACH 'test.db' AS db; + } db2] + + lappend rc [string equal [hexio_read test.db 0 16] [hexio_read test2.db 0 16]] + +} {{1 {file is not a database}} 0} +db2 close +file delete -force test.db +file delete -force test2.db + +# attach an empty encrypted database +# without specifying key, verify the database has the same +# salt and as the original +setup test.db "'testkey'" +do_test attach-empty-database-with-default-key { + sqlite_orig db test.db + set rc {} + + execsql { + PRAGMA key='testkey'; + INSERT INTO t1(a,b) values (1,2); + ATTACH DATABASE 'test2.db' AS test; + CREATE TABLE test.t1(a,b); + INSERT INTO test.t1 SELECT * FROM t1; + DETACH DATABASE test; + } + + sqlite_orig db2 test2.db + + lappend rc [execsql { + PRAGMA key='testkey'; + SELECT count(*) FROM t1; + } db2] + lappend rc [string equal [hexio_read test.db 0 16] [hexio_read test2.db 0 16]] +} {{ok 2} 1} +db close +db2 close +file delete -force test.db +file delete -force test2.db + +# attach an empty encrypted database as the first operation on a keyed database. Verify +# that the new database has the same salt as the original. +# +# HISTORICAL NOTE: The original behavior of SQLCipher under these conditions +# was that the databases would have different salts but the same keys. This was because +# derivation of the key spec would not have occurred yet. However, upstream check-in +# https://sqlite.org/src/info/a02da71f3a80dd8e changed this behavior by +# forcing a read of the main database schema during the attach operation. +# This causes the main database to be opened and the key derivation logic to fire which +# reads the salt. Thus the current behavior of this test should now be identical +# to the previous attach-empty-database-with-default-key. + +setup test.db "'testkey'" +do_test attach-empty-database-with-default-key-first-op { + sqlite_orig db test.db + set rc {} + + execsql { + PRAGMA key='testkey'; + ATTACH DATABASE 'test2.db' AS test; + CREATE TABLE test.t1(a,b); + INSERT INTO test.t1 SELECT * FROM t1; + DETACH DATABASE test; + } + + sqlite_orig db2 test2.db + + lappend rc [execsql { + PRAGMA key='testkey'; + SELECT count(*) FROM t1; + } db2] + + lappend rc [string equal [hexio_read test.db 0 16] [hexio_read test2.db 0 16]] +} {{ok 1} 1} +db close +db2 close +file delete -force test.db +file delete -force test2.db + +# attach an empty encrypted database +# on a keyed database when PRAGMA cipher_store_pass = 1 +# and verify different salts +setup test.db "'testkey'" +do_test attach-empty-database-with-cipher-store-pass { + sqlite_orig db test.db + set rc {} + + execsql { + PRAGMA key='testkey'; + PRAGMA cipher_store_pass = 1; + INSERT INTO t1(a,b) VALUES (1,2); + ATTACH DATABASE 'test2.db' AS test; + CREATE TABLE test.t1(a,b); + INSERT INTO test.t1 SELECT * FROM t1; + DETACH DATABASE test; + } + + sqlite_orig db2 test2.db + + lappend rc [execsql { + PRAGMA key='testkey'; + SELECT count(*) FROM t1; + } db2] + lappend rc [string equal [hexio_read test.db 0 16] [hexio_read test2.db 0 16]] +} {{ok 2} 0} +db close +db2 close +file delete -force test.db +file delete -force test2.db + +# attach an encrypted database +# without specifying key, verify it attaches +# correctly when PRAGMA cipher_store_pass = 1 +# is set +do_test attach-database-with-default-key-using-cipher-store-pass { + sqlite_orig db1 test.db + execsql { + PRAGMA key = 'testkey'; + CREATE TABLE t1(a,b); + INSERT INTO t1(a,b) VALUES('foo', 'bar'); + } db1 + db1 close + + sqlite_orig db2 test2.db + execsql { + PRAGMA key = 'testkey'; + CREATE TABLE t2(a,b); + INSERT INTO t2 VALUES ('test1', 'test2'); + } db2 + db2 close + + sqlite_orig db1 test.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_store_pass = 1; + ATTACH DATABASE 'test2.db' as db2; + SELECT sqlcipher_export('db2'); + DETACH DATABASE db2; + } db1 + db1 close + + sqlite_orig db2 test2.db + execsql { + PRAGMA key = 'testkey'; + SELECT * FROM t1; + } db2 + +} {ok foo bar} +db2 close +file delete -force test.db +file delete -force test2.db + +# attach an encrypted database +# where both database have the same +# key explicitly and verify they have different +# salt values +setup test.db "'testkey'" +do_test attach-database-with-same-key { + sqlite_orig db2 test2.db + + set rc {} + + execsql { + PRAGMA key = 'testkey'; + CREATE TABLE t2(a,b); + INSERT INTO t2 VALUES ('test1', 'test2'); + } db2 + + lappend rc [execsql { + SELECT count(*) FROM t2; + ATTACH 'test.db' AS db KEY 'testkey'; + SELECT count(*) FROM db.t1; + } db2] + + lappend rc [string equal [hexio_read test.db 0 16] [hexio_read test2.db 0 16]] +} {{1 1} 0} +db2 close +file delete -force test.db +file delete -force test2.db + +# attach an encrypted database +# where databases have different keys +setup test.db "'testkey'" +do_test attach-database-with-different-keys { + sqlite_orig db2 test2.db + + execsql { + PRAGMA key = 'testkey2'; + CREATE TABLE t2(a,b); + INSERT INTO t2 VALUES ('test1', 'test2'); + } db2 + + execsql { + ATTACH 'test.db' AS db KEY 'testkey'; + SELECT count(*) FROM db.t1; + SELECT count(*) FROM t2; + } db2 + +} {1 1} +db2 close +file delete -force test.db +file delete -force test2.db + +# test locking across multiple handles +setup test.db "'testkey'" +do_test locking-across-multiple-handles-start { + sqlite_orig db test.db + + execsql { + PRAGMA key = 'testkey'; + BEGIN EXCLUSIVE; + INSERT INTO t1 VALUES(1,2); + } + + sqlite_orig dba test.db + catchsql { + PRAGMA key = 'testkey'; + SELECT count(*) FROM t1; + } dba + + } {1 {database is locked}} + +do_test locking-accross-multiple-handles-finish { + execsql { + COMMIT; + } + + execsql { + SELECT count(*) FROM t1; + } dba +} {2} +db close +dba close +file delete -force test.db + +# alter schema +setup test.db "'testkey'" +do_test alter-schema { + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + ALTER TABLE t1 ADD COLUMN c; + INSERT INTO t1 VALUES (1,2,3); + INSERT INTO t1 VALUES (1,2,4); + CREATE TABLE t1a (a); + INSERT INTO t1a VALUES ('teststring'); + } + db close + + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + SELECT count(*) FROM t1 WHERE a IS NOT NULL; + SELECT count(*) FROM t1 WHERE c IS NOT NULL; + SELECT * FROM t1a; + } + +} {ok 3 2 teststring} +db close +file delete -force test.db + +# test alterations of KDF iterations and ciphers +# rekey then add +setup test.db "'testkey'" +do_test verify-errors-for-rekey-kdf-and-cipher-changes { + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA rekey_kdf_iter = 1000; + PRAGMA rekey_cipher = 'aes-256-ecb'; + } +} {ok {PRAGMA rekey_kdf_iter is no longer supported.} {PRAGMA rekey_cipher is no longer supported.}} +db close +file delete -force test.db + + +setup test.db "'testkey'" +do_test verify-errors-for-cipher-change { + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher = 'aes-256-ecb'; + } +} {ok {PRAGMA cipher is no longer supported.}} +db close +file delete -force test.db + + +# 1. create a database with a custom page size, +# 2. create table and insert operations should work +# 3. close database, open it again with the same +# key and page size +# 4. verify that the table is readable +# and the data just inserted is visible +do_test custom-pagesize-pragma-cipher-page-size { + sqlite_orig db test.db + + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_page_size = 8192; + CREATE table t1(a,b); + BEGIN; + } + + for {set i 1} {$i<=1000} {incr i} { + set r [expr {int(rand()*500000)}] + execsql "INSERT INTO t1 VALUES($i,'value $r');" + } + + execsql { + COMMIT; + } + + db close + sqlite_orig db test.db + + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_page_size = 8192; + SELECT count(*) FROM t1; + } + +} {ok 1000} +db close +file delete -force test.db + +# run the same logic as previous test but use +# pragma page_size instead +do_test custom-pagesize-pragma-pagesize { + sqlite_orig db test.db + + execsql { + PRAGMA key = 'testkey'; + PRAGMA page_size = 8192; + CREATE table t1(a,b); + BEGIN; + } + + for {set i 1} {$i<=1000} {incr i} { + set r [expr {int(rand()*500000)}] + execsql "INSERT INTO t1 VALUES($i,'value $r');" + } + + execsql { + COMMIT; + } + + db close + sqlite_orig db test.db + + execsql { + PRAGMA key = 'testkey'; + PRAGMA page_size = 8192; + SELECT count(*) FROM t1; + } + +} {ok 1000} +db close +file delete -force test.db + +# open the database with the default page size +## and verfiy that it is not readable +do_test custom-pagesize-must-match { + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_page_size = 8192; + CREATE table t1(a,b); + } + + db close + sqlite_orig db test.db + + catchsql { + PRAGMA key = 'testkey'; + SELECT name FROM sqlite_schema WHERE type='table'; + } +} {1 {file is not a database}} +db close +file delete -force test.db + + +# 1. create a database with WAL journal mode +# 2. create table and insert operations should work +# 3. close database, open it again +# 4. verify that the table is present, readable, and that +# the journal mode is WAL +do_test journal-mode-wal { + sqlite_orig db test.db + + execsql { + PRAGMA key = 'testkey'; + PRAGMA journal_mode = WAL; + CREATE table t1(a,b); + BEGIN; + } + + for {set i 1} {$i<=1000} {incr i} { + set r [expr {int(rand()*500000)}] + execsql "INSERT INTO t1 VALUES($i,'value $r');" + } + + execsql { + COMMIT; + } + + db close + sqlite_orig db test.db + + execsql { + PRAGMA key = 'testkey'; + SELECT count(*) FROM t1; + PRAGMA journal_mode; + } + +} {ok 1000 wal} +db close +file delete -force test.db + +# open a database and try to use an invalid +# passphrase. verify that an error is returned +# and that data couldn't be read. without closing the databsae +# set the correct key and verify it is working. +setup test.db "'testkey'" +do_test multiple-key-calls-safe-wrong-key-first { + sqlite_orig db test.db + set rc {} + + lappend rc [catchsql { + PRAGMA key = 'testkey2'; + SELECT count(*) FROM sqlite_schema; + }] + + lappend rc [execsql { + PRAGMA key = 'testkey'; + SELECT count(*) FROM sqlite_schema; + }] +} {{1 {file is not a database}} {ok 1}} +db close +file delete -force test.db + +# open a databse and use the valid key. Then +# use pragma key to try to set an invalid key +# without closing the database. It should not do anything + +setup test.db "'testkey'" +do_test multiple-key-calls-safe { + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA cache_size = 0; + SELECT name FROM sqlite_schema WHERE type='table'; + PRAGMA key = 'wrong key'; + SELECT name FROM sqlite_schema WHERE type='table'; + PRAGMA key = 'testkey'; + SELECT name FROM sqlite_schema WHERE type='table'; + } +} {ok t1 ok t1 ok t1} + +db close +file delete -force test.db + +# open a databse and use the valid key. Then +# use pragma cipher_compatability to adjust settings that +# would normally trigger key derivation again. +# the new settings should be ignored + +setup test.db "'testkey'" +do_test setting-changes-after-key-calls-safe { + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA cache_size = 0; + SELECT name FROM sqlite_schema WHERE type='table'; + INSERT INTO t1(a,b) VALUES (2,zeroblob(8192)); + PRAGMA cipher_compatibility=3; + INSERT INTO t1(a,b) VALUES (3,zeroblob(8192)); + SELECT name FROM sqlite_schema WHERE type='table'; + SELECT count(*) FROM t1; + } +} {ok t1 t1 3} +db close +file delete -force test.db + +# 1. create a database with a custom hmac kdf iteration count, +# 2. create table and insert operations should work +# 3. close database, open it again with the same +# key and hmac kdf iteration count +# 4. verify that the table is readable +# and the data just inserted is visible +do_test custom-hmac-kdf-iter { + sqlite_orig db test.db + + execsql { + PRAGMA key = 'testkey'; + PRAGMA kdf_iter = 10; + CREATE table t1(a,b); + BEGIN; + } + + for {set i 1} {$i<=1000} {incr i} { + set r [expr {int(rand()*500000)}] + execsql "INSERT INTO t1 VALUES($i,'value $r');" + } + + execsql { + COMMIT; + } + + db close + sqlite_orig db test.db + + execsql { + PRAGMA key = 'testkey'; + PRAGMA kdf_iter = 10; + SELECT count(*) FROM t1; + } + +} {ok 1000} +db close + +# open the database with the default hmac +# kdf iteration count +# to verify that it is not readable +do_test custom-hmac-kdf-iter-must-match { + sqlite_orig db test.db + catchsql { + PRAGMA key = 'testkey'; + SELECT name FROM sqlite_schema WHERE type='table'; + } +} {1 {file is not a database}} +db close +file delete -force test.db + +# open the database and turn on auto_vacuum +# then insert a bunch of data, delete it +# and verify that the file has become smaller +# but can still be opened with the proper +# key +do_test auto-vacuum { + sqlite_orig db test.db + set rc {} + + execsql { + PRAGMA key = 'testkey'; + PRAGMA auto_vacuum=FULL; + CREATE table t1(a,b); + BEGIN; + } + + for {set i 1} {$i<=10000} {incr i} { + set r [expr {int(rand()*500000)}] + execsql "INSERT INTO t1 VALUES($i,'value $r');" + } + + lappend rc [execsql { + COMMIT; + SELECT count(*) FROM t1; + }] + + # grab current size of file + set sz [file size test.db] + + # delete some records, and verify + # autovacuum removes them + execsql { + DELETE FROM t1 WHERE rowid > 5000; + } + + db close + + # grab new file size, post + # autovacuum + set sz2 [file size test.db] + + # verify that the new size is + # smaller than the old size + if {$sz > $sz2} { lappend rc true } + + sqlite_orig db test.db + + lappend rc [execsql { + PRAGMA key = 'testkey'; + SELECT count(*) FROM t1; + }] + +} {10000 true {ok 5000}} +db close +file delete -force test.db + +# test kdf_iter and other pragmas +# before a key is set. Verify that they +# are no-ops +do_test cipher-options-before-keys { + sqlite_orig db test.db + + execsql { + PRAGMA kdf_iter = 1000; + PRAGMA cipher_page_size = 8192; + PRAGMA cipher_use_hmac = OFF; + PRAGMA key = 'testkey'; + CREATE table t1(a,b); + INSERT INTO t1 VALUES(1,2); + } + db close + + sqlite_orig db test.db + + execsql { + PRAGMA key = 'testkey'; + SELECT count(*) FROM t1; + } + +} {ok 1} +db close +file delete -force test.db + +# create two new database files, write to each +# and verify that they have different (i.e. random) +# salt values +do_test test-random-salt { + sqlite_orig db test.db + sqlite_orig db2 test2.db + execsql { + PRAGMA key = 'test'; + CREATE TABLE t1(a,b); + INSERT INTO t1(a,b) VALUES (1,2); + } + execsql { + PRAGMA key = 'test'; + CREATE TABLE t1(a,b); + INSERT INTO t1(a,b) VALUES (1,2); + } db2 + string equal [hexio_read test.db 0 16] [hexio_read test2.db 0 16] +} {0} +db close +db2 close +file delete -force test.db +file delete -force test2.db + +# test scenario where multiple handles are opened +# to a file that does not exist, where both handles +# use the same key +do_test multiple-handles-same-key-and-salt { + sqlite_orig db test.db + sqlite_orig dba test.db + + execsql { + PRAGMA key = 'testkey'; + } + execsql { + PRAGMA key = 'testkey'; + } dba + + execsql { + CREATE TABLE t1(a,b); + INSERT INTO t1 VALUES(1,2); + } + + execsql { + SELECT count(*) FROM t1; + } + execsql { + SELECT count(*) FROM t1; + } dba + +} {1} +db close +dba close +file delete -force test.db + +do_test test_flags_fail_encrypt { + sqlite_orig db :memory: + execsql { + PRAGMA cipher_test; + PRAGMA cipher_test_on = fail_encrypt; + PRAGMA cipher_test; + PRAGMA cipher_test_off = fail_encrypt; + PRAGMA cipher_test; + } +} {0 1 0} +db close + +do_test test_flags_fail_decrypt { + sqlite_orig db :memory: + execsql { + PRAGMA cipher_test; + PRAGMA cipher_test_on = fail_decrypt; + PRAGMA cipher_test; + PRAGMA cipher_test_off = fail_decrypt; + PRAGMA cipher_test; + } +} {0 2 0} +db close + +do_test test_flags_fail_migrate { + sqlite_orig db :memory: + execsql { + PRAGMA cipher_test; + PRAGMA cipher_test_on = fail_migrate; + PRAGMA cipher_test; + PRAGMA cipher_test_off = fail_migrate; + PRAGMA cipher_test; + } +} {0 4 0} +db close + +do_test test_flags_combo { + sqlite_orig db :memory: + execsql { + PRAGMA cipher_test; + PRAGMA cipher_test_on = fail_encrypt; + PRAGMA cipher_test_on = fail_migrate; + PRAGMA cipher_test; + PRAGMA cipher_test_off = fail_encrypt; + PRAGMA cipher_test_off = fail_migrate; + PRAGMA cipher_test; + } +} {0 5 0} +db close + +# test empty key +# it should raise an error +do_test empty-key { + sqlite_orig db test.db + + catchsql { + PRAGMA key = ''; + } + +} {1 {An error occurred with PRAGMA key or rekey. PRAGMA key requires a key of one or more characters. PRAGMA rekey can only be run on an existing encrypted database. Use sqlcipher_export() and ATTACH to convert encrypted/plaintext databases.}} +db close +file delete -force test.db + +# configure URI filename support +# create a new encrypted database with the key via parameter +# close database +# open normally providing key via pragma verify +# correct key works +do_test uri-key { + sqlite_orig db file:test.db?a=a&key=testkey&c=c + + execsql { + CREATE TABLE t1(a,b); + INSERT INTO t1 VALUES(1,2); + } + + db close + sqlite_orig db test.db + + catchsql { + PRAGMA key = 'testkey'; + SELECT count(*) FROM t1; + } + + db close + sqlite_orig db test.db + + execsql { + PRAGMA key = 'testkey'; + SELECT count(*) FROM t1; + } + +} {ok 1} +db close + +# verify wrong key fails +do_test uri-key-2 { + sqlite_orig db test.db + catchsql { + PRAGMA key = 'test'; + SELECT count(*) FROM t1; + } +} {1 {file is not a database}} +db close +file delete -force test.db + +# test cipher_status reports encrypted for database +do_test cipher-status-encrypted { + sqlite_orig db test.db + + execsql { + PRAGMA key = 'test'; + PRAGMA cipher_status; + } + +} {ok 1} +db close +file delete -force test.db + +# test cipher_status reports not-encrypted for plaintext database +do_test cipher-status-plaintext { + sqlite_orig db test.db + + execsql { + PRAGMA cipher_status; + } + +} {0} +db close +file delete -force test.db + +# test cipher_status reports encrypted for attached database +# when main database is plaintext and attached database is +# keyed +do_test cipher-status-attached-encrypted { + sqlite_orig db test.db + + execsql { + PRAGMA cipher_status; + ATTACH DATABASE 'test2.db' AS test2 KEY 'test'; + PRAGMA test2.cipher_status; + DETACH DATABASE test2; + } + +} {0 1} +db close +file delete -force test.db +file delete -force test2.db + +# test cipher_status reports plaintext for attached database +# when main database is keyed and attached database is not +do_test cipher-status-attached-plaintext { + sqlite_orig db test.db + + execsql { + PRAGMA KEY = 'test'; + PRAGMA cipher_status; + ATTACH DATABASE 'test2.db' AS test2 KEY ''; + PRAGMA test2.cipher_status; + DETACH DATABASE test2; + } + +} {ok 1 0} +db close +file delete -force test.db +file delete -force test2.db + +# test cipher_status reports unencrypted if key derivation +# and operation fails +setup test.db "'testkey'" +do_test cipher-status-badkey { + sqlite_orig db test.db + + catchsql { + PRAGMA key = 'test'; + SELECT count(*) FROM sqlite_master; + } + execsql { + PRAGMA cipher_status; + } + +} {0} +db close +file delete -force test.db + +finish_test + diff --git a/test/sqlcipher-integrity.test b/test/sqlcipher-integrity.test new file mode 100644 index 0000000000..dbf903fcd0 --- /dev/null +++ b/test/sqlcipher-integrity.test @@ -0,0 +1,383 @@ +# SQLCipher +# codec.test developed by Stephen Lombardo (Zetetic LLC) +# sjlombardo at zetetic dot net +# http://zetetic.net +# +# Copyright (c) 2018, ZETETIC LLC +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of the ZETETIC LLC nor the +# names of its contributors may be used to endorse or promote products +# derived from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY +# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY +# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# +# This file implements regression tests for SQLite library. The +# focus of this script is testing code cipher features. +# +# NOTE: tester.tcl has overridden the definition of sqlite3 to +# automatically pass in a key value. Thus tests in this file +# should explicitly close and open db with sqlite_orig in order +# to bypass default key assignment. + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/sqlcipher.tcl + +# 1. create a database and insert a bunch of data, close the database +# 2. seek to the middle of the first database page and write some junk +# 3. Open the database and verify that the database is no longer readable +do_test hmac-tamper-resistence-first-page { + sqlite_orig db test.db + + execsql { + PRAGMA key = 'testkey'; + CREATE table t1(a,b); + BEGIN; + } + + for {set i 1} {$i<=1000} {incr i} { + set r [expr {int(rand()*500000)}] + execsql "INSERT INTO t1 VALUES($i,'value $r');" + } + + execsql { + COMMIT; + } + + db close + + # write some junk into the hmac segment, leaving + # the page data valid but with an invalid signature + hexio_write test.db 1000 000000 + + sqlite_orig db test.db + + catchsql { + PRAGMA key = 'testkey'; + SELECT count(*) FROM t1; + } + +} {1 {file is not a database}} +db close +file delete -force test.db + +# 1. create a database and insert a bunch of data, close the database +# 2. seek to the middle of a database page and write some junk +# 3. Open the database and verify that the database is still readable +do_test nohmac-not-tamper-resistent { + sqlite_orig db test.db + + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_use_hmac = OFF; + PRAGMA cipher_page_size = 1024; + CREATE table t1(a,b); + BEGIN; + } + + for {set i 1} {$i<=1000} {incr i} { + set r [expr {int(rand()*500000)}] + execsql "INSERT INTO t1 VALUES($i,'value $r');" + } + + execsql { + COMMIT; + } + + db close + + # write some junk into the middle of the page + hexio_write test.db 2560 000000 + + sqlite_orig db test.db + + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_use_hmac = OFF; + PRAGMA cipher_page_size = 1024; + SELECT count(*) FROM t1; + } + +} {ok 1000} +db close +file delete -force test.db + +# 1. create a database and insert a bunch of data, close the database +# 2. seek to the middle of a database page (not the first page) and write bad data +# 3. Open the database and verify that the database is no longer readable +do_test hmac-tamper-resistence { + sqlite_orig db test.db + + execsql { + PRAGMA key = 'testkey'; + CREATE table t1(a,b); + BEGIN; + } + + for {set i 1} {$i<=1000} {incr i} { + set r [expr {int(rand()*500000)}] + execsql "INSERT INTO t1 VALUES($i,'value $r');" + } + + execsql { + COMMIT; + } + + db close + + # write some junk into the hmac segment, leaving + # the page data valid but with an invalid signature + hexio_write test.db 16500 000000 + + sqlite_orig db test.db + + catchsql { + PRAGMA key = 'testkey'; + SELECT count(*) FROM t1; + } + +} {1 {database disk image is malformed}} +db close +file delete -force test.db + +# test that integrity checks work on a pristine +# newly created database +do_test integrity-check-clean-database { + sqlite_orig db test.db + + execsql { + PRAGMA key = 'testkey'; + CREATE table t1(a,b); + BEGIN; + } + + for {set i 1} {$i<=10000} {incr i} { + execsql "INSERT INTO t1 VALUES($i,'value $i');" + } + + execsql { + COMMIT; + } + + db close + + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_integrity_check; + PRAGMA integrity_check; + SELECT count(*) FROM t1; + } + +} {ok ok 10000} +db close +file delete -force test.db + +# try cipher_integrity_check on an in-memory database +# which should fail because the file doesn't exist +do_test memory-integrity-check-should-fail { + sqlite_orig db :memory: + execsql { + PRAGMA key = 'testkey'; + CREATE TABLE t1(a,b); + INSERT INTO t1(a,b) values (1,2); + PRAGMA cipher_integrity_check; + } +} {ok {database file is undefined}} +db close + +# try cipher_integrity_check on a valid 1.1.8 database +# should fail because version 1.0 doesn't use HMAC +do_test version-1-integrity-check-fail-no-hmac { + file copy -force $sampleDir/sqlcipher-1.1.8-testkey.db test.db + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_compatibility = 1; + PRAGMA cipher_integrity_check; + } +} {ok {HMAC is not enabled, unable to integrity check}} +db close +file delete -force test.db + +# try cipher_integrity_check on a valid 2 database +do_test version-2-integrity-check-valid { + file copy -force $sampleDir/sqlcipher-2.0-le-testkey.db test.db + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_compatibility = 2; + PRAGMA cipher_integrity_check; + } +} {ok} +db close +file delete -force test.db + +# try cipher_integrity_check on a corrupted version 2 database +do_test version-2-integrity-check-invalid { + file copy -force $sampleDir/sqlcipher-2.0-le-testkey.db test.db + hexio_write test.db 8202 000000 + hexio_write test.db 10250 000000 + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_compatibility = 2; + PRAGMA cipher_integrity_check; + } +} {ok {HMAC verification failed for page 9} {HMAC verification failed for page 11}} +db close +file delete -force test.db + +# try cipher_integrity_check on a valid version 3 database +do_test version-3-integrity-check-valid { + file copy -force $sampleDir/sqlcipher-3.0-testkey.db test.db + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_compatibility = 3; + PRAGMA cipher_integrity_check; + } +} {ok} +db close +file delete -force test.db + +# try cipher_integrity_check on a corrupted version 3 database +do_test version-3-integrity-check-invalid { + file copy -force $sampleDir/sqlcipher-3.0-testkey.db test.db + hexio_write test.db 8202 000000 + hexio_write test.db 10250 000000 + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_compatibility = 3; + PRAGMA cipher_integrity_check; + } +} {ok {HMAC verification failed for page 9} {HMAC verification failed for page 11}} +db close +file delete -force test.db + +# try cipher_integrity_check on a valid version 4 database +do_test version-4-integrity-check-valid { + file copy -force $sampleDir/sqlcipher-4.0-testkey.db test.db + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_integrity_check; + } +} {ok} +db close +file delete -force test.db + +# try cipher_integrity_check on a corrupted version 4 database +do_test version-4-integrity-check-invalid { + file copy -force $sampleDir/sqlcipher-4.0-testkey.db test.db + # corrupt page data + hexio_write test.db 5120 000000 + # corrupt iv + hexio_write test.db 12208 000000 + # corrupt the mac segment + hexio_write test.db 16320 000000 + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_integrity_check; + } +} {ok {HMAC verification failed for page 2} {HMAC verification failed for page 3} {HMAC verification failed for page 4}} +db close +file delete -force test.db + +# try cipher_integrity_check on a corrupted version 4 database +do_test version-4-integrity-check-invalid-last-page { + file copy -force $sampleDir/sqlcipher-4.0-testkey.db test.db + hexio_write test.db 978944 0000 + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_integrity_check; + } +} {ok {page 240 has an invalid size of 2 bytes (expected 4096 bytes)}} +db close +file delete -force test.db + +# verify cipher_integrity_check works on a plaintext header db +do_test integrity-check-plaintext-header { + sqlite_orig db test.db + set rc {} + + execsql { + PRAGMA key = 'test'; + PRAGMA cipher_plaintext_header_size = 32; + CREATE TABLE t1(a,b); + INSERT INTO t1(a,b) VALUES (1,2); + } + + lappend rc [execsql { + PRAGMA cipher_integrity_check; + }] + + lappend rc [string equal [hexio_read test.db 16 5] "1000010150"] + + hexio_write test.db 120 000000 + hexio_write test.db 5120 000000 + + lappend rc [execsql { + PRAGMA cipher_integrity_check; + }] +} {{} 1 {{HMAC verification failed for page 1} {HMAC verification failed for page 2}}} +db close +file delete -force test.db + +# test that changing the key in the middle of database operations does +# not cause a corruption +do_test change-key-middle { + sqlite_orig db test.db + + set rc {} + + execsql { + PRAGMA key = 'testkey'; + CREATE table t1(a,b); + } + + for {set i 1} {$i<=1000} {incr i} { + execsql "INSERT INTO t1 VALUES($i,'value $i');" + } + + execsql { + PRAGMA key = 'diffkey'; + } + + for {set i 1} {$i<=1000} {incr i} { + execsql "INSERT INTO t1 VALUES($i,'value $i');" + } + + db close + + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + SELECT name FROM sqlite_schema; + PRAGMA cipher_integrity_check; + PRAGMA integrity_check; + SELECT count(*) FROM t1; + } +} {ok t1 ok 2000} +db close +file delete -force test.db + +finish_test diff --git a/test/sqlcipher-plaintext-header.test b/test/sqlcipher-plaintext-header.test new file mode 100644 index 0000000000..023d414549 --- /dev/null +++ b/test/sqlcipher-plaintext-header.test @@ -0,0 +1,473 @@ +# SQLCipher +# codec.test developed by Stephen Lombardo (Zetetic LLC) +# sjlombardo at zetetic dot net +# http://zetetic.net +# +# Copyright (c) 2018, ZETETIC LLC +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of the ZETETIC LLC nor the +# names of its contributors may be used to endorse or promote products +# derived from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY +# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY +# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# +# This file implements regression tests for SQLite library. The +# focus of this script is testing code cipher features. +# +# NOTE: tester.tcl has overridden the definition of sqlite3 to +# automatically pass in a key value. Thus tests in this file +# should explicitly close and open db with sqlite_orig in order +# to bypass default key assignment. + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/sqlcipher.tcl + +set hexkeyspec "\"x'98483C6EB40B6C31A448C22A66DED3B5E5E8D5119CAC8327B655C8B5C483648101010101010101010101010101010101'\"" + +# verify default plaintext header size is 0 +do_test test-default-plaintext-header-size { + sqlite_orig db :memory: + execsql { + PRAGMA cipher_default_plaintext_header_size; + } +} {0} + +# verify pragma cipher_salt returns the first 16 bytes +# of an existing database +do_test test-pragma-salt-get { + sqlite_orig db test.db + execsql { PRAGMA key = 'test'; } + set salt [execsql { + CREATE TABLE t1(a,b); + PRAGMA cipher_salt; + }] + set header [string tolower [hexio_read test.db 0 16]] + string equal $header $salt +} {1} +db close +file delete -force test.db + +# explicitly set the salt of a new database +do_test test-pragma-salt-set { + set rc {} + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + PRAGMA cipher_salt = "x'01010101010101010101010101010101'"; + CREATE TABLE t1(a,b); + INSERT INTO t1(a,b) VALUES (1,2); + } + db close + + lappend rc [hexio_read test.db 0 16] + + sqlite_orig db test.db + lappend rc [execsql " + PRAGMA key = 'test'; + SELECT count(*) FROM t1; + PRAGMA cipher_salt; + "] + +} {01010101010101010101010101010101 {ok 1 01010101010101010101010101010101}} +db close +file delete -force test.db + + +# verify that a raw key with a fixed salt will work +# the first 16 bytes of database should be equal to the specified salt +# which is the last 32 characters of the hex key spec. +# also verify return value of cipher_salt +do_test test-raw-key-with-salt-spec { + set rc {} + sqlite_orig db test.db + execsql " + PRAGMA key = $hexkeyspec; + CREATE TABLE t1(a,b); + INSERT INTO t1(a,b) VALUES (1,2); + " + db close + + lappend rc [hexio_read test.db 0 16] + + sqlite_orig db test.db + lappend rc [execsql " + PRAGMA key = $hexkeyspec; + SELECT count(*) FROM t1; + PRAGMA cipher_salt; + "] +} {01010101010101010101010101010101 {ok 1 01010101010101010101010101010101}} +db close +file delete -force test.db + +# verify that a raw key with an invalid salt will not work to +# open an existing database. +# should cause hmac failure due to invalid generated HMAC key +do_test test-raw-key-with-invalid-salt-spec { + sqlite_orig db test.db + execsql " + PRAGMA key = $hexkeyspec; + CREATE TABLE t1(a,b); + INSERT INTO t1(a,b) VALUES (1,2); + " + db close + + sqlite_orig db test.db + catchsql { + PRAGMA key="x'98483C6EB40B6C31A448C22A66DED3B5E5E8D5119CAC8327B655C8B5C483648100000000000000000000000000000001'"; + SELECT count(*) FROM t1; + } +} {1 {file is not a database}} +db close +file delete -force test.db + +# verify that a raw key with a bad salt *will* work if page HMAC is disabled +# in this case the salt will not actually be used for anything +# because the encryption key is provided explicitly +do_test test-raw-key-with-invalid-salt-spec-no-hmac { + sqlite_orig db test.db + execsql " + PRAGMA key = $hexkeyspec; + PRAGMA cipher_use_hmac = OFF; + CREATE TABLE t1(a,b); + INSERT INTO t1(a,b) VALUES (1,2); + " + db close + + sqlite_orig db test.db + execsql { + PRAGMA key="x'98483C6EB40B6C31A448C22A66DED3B5E5E8D5119CAC8327B655C8B5C483648100000000000000000000000000000001'"; + PRAGMA cipher_use_hmac = OFF; + SELECT count(*) FROM t1; + } +} {ok 1} +db close +file delete -force test.db + +# verify that invalid cipher_plaintext_header_sizes don't work +# 1. less than zero +# 2. Larger than available page size +# 2. Not a multiple of block size +do_test test-invalid-plaintext-header-sizes { + set rc {} + sqlite_orig db test.db + lappend rc [catchsql " + PRAGMA key = $hexkeyspec; + PRAGMA cipher_plaintext_header_size = -1; + CREATE TABLE t1(a,b); + "] + db close + sqlite_orig db test.db + lappend rc [catchsql " + PRAGMA key = $hexkeyspec; + PRAGMA cipher_plaintext_header_size = 4096; + CREATE TABLE t1(a,b); + "] + db close + sqlite_orig db test.db + lappend rc [catchsql " + PRAGMA key = $hexkeyspec; + PRAGMA cipher_plaintext_header_size = 24; + CREATE TABLE t1(a,b); + "] +} {{1 {out of memory}} {1 {out of memory}} {1 {out of memory}}} +db close +file delete -force test.db + +# verify that a valid cipher_plaintext_header_size leaves the +# start of the database unencrypted, i.e. "SQLite format 3\0" +do_test test-valid-plaintext-header-size { + set rc {} + sqlite_orig db test.db + execsql " + PRAGMA key = $hexkeyspec; + PRAGMA cipher_plaintext_header_size = 16; + CREATE TABLE t1(a,b); + INSERT INTO t1(a,b) VALUES (1,2); + " + db close + + lappend rc [hexio_read test.db 0 16] + + sqlite_orig db test.db + lappend rc [execsql " + PRAGMA key = $hexkeyspec; + PRAGMA cipher_plaintext_header_size = 16; + SELECT count(*) FROM t1; + PRAGMA cipher_plaintext_header_size; + "] +} {53514C69746520666F726D6174203300 {ok 1 16}} +db close +file delete -force test.db + +# when using a standard mode database and 32 byte +# plaintext header, ensure that bytes 16 - 19 +# corresponding to the page size and file versions, and reserve size +# are readable and equal to 1024, 1, 1, and 80 respectively +do_test test-plaintext-header-journal-delete-mode-readable { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + PRAGMA cipher_plaintext_header_size = 32; + CREATE TABLE t1(a,b); + INSERT INTO t1(a,b) VALUES (1,2); + } + db close + string equal [hexio_read test.db 16 5] "1000010150" +} {1} +file delete -force test.db + + +# when using a WAL mode database and 32 byte +# plaintext header, ensure that bytes 16 - 19 +# corresponding to the page size and file versions, and reserve size +# are readable and equal to 1024, 2, 2 and 80 respectively +do_test test-plaintext-header-journal-wal-mode-readable { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + PRAGMA cipher_plaintext_header_size = 32; + PRAGMA journal_mode = WAL; + CREATE TABLE t1(a,b); + INSERT INTO t1(a,b) VALUES (1,2); + } + db close + string equal [hexio_read test.db 16 5] "1000020250" +} {1} +file delete -force test.db + +# verify that a valid default_cipher_plaintext_header_size leaves the +# start of the database unencrypted right from the start +# , i.e. "SQLite format 3\0" +do_test test-valid-default-plaintext-header-size { + set rc {} + sqlite_orig db test.db + execsql { + PRAGMA cipher_default_plaintext_header_size = 16; + PRAGMA key = 'test'; + } + + set salt [execsql { + CREATE TABLE t1(a,b); + INSERT INTO t1(a,b) VALUES (1,2); + PRAGMA cipher_salt; + }] + db close + + lappend rc [hexio_read test.db 0 16] + + sqlite_orig db test.db + execsql { PRAGMA key = 'test'; } + lappend rc [execsql " + PRAGMA cipher_salt = \"x'$salt'\"; + SELECT count(*) FROM t1; + PRAGMA cipher_plaintext_header_size; + "] + + # reset the default back to 0 or subsequent tests will fail + execsql "PRAGMA cipher_default_plaintext_header_size = 0;" + + lappend rc [string equal $salt "53514c69746520666f726d6174203300"] +} {53514C69746520666F726D6174203300 {1 16} 0} +db close +file delete -force test.db + +# verify that a valid default_cipher_plaintext_header_size +# operates properly on an attached database, and that the +# salt pragma operates on the attached database as well +do_test test-valid-default-plaintext-header-size-attach { + set rc {} + sqlite_orig db test.db + execsql { + PRAGMA cipher_default_plaintext_header_size = 16; + PRAGMA key = 'test'; + } + set salt [execsql { + CREATE TABLE temp(a); + ATTACH DATABASE 'test2.db' as db2; + CREATE TABLE db2.t2(a,b); + INSERT INTO db2.t2(a,b) VALUES (1,2); + PRAGMA db2.cipher_salt; + DETACH DATABASE db2; + }] + db close + lappend rc [hexio_read test2.db 0 16] + + sqlite_orig db test2.db + execsql { PRAGMA key = 'test'; } + lappend rc [execsql " + PRAGMA cipher_salt = \"x'$salt'\"; + SELECT count(*) FROM t2; + PRAGMA cipher_plaintext_header_size; + "] + + # reset the default back to 0 or subsequent tests will fail + execsql "PRAGMA cipher_default_plaintext_header_size = 0;" + + lappend rc [string equal $salt "53514c69746520666f726d6174203300"] +} {53514C69746520666F726D6174203300 {1 16} 0} +db close +file delete -force test.db +file delete -force test2.db + + +# migrate a standard database in place to use a +# plaintext header offset by opening it, adjusting +# the pragma, and rewriting the first page +do_test test-plaintext-header-migrate-journal-delete { + set rc {} + sqlite_orig db test.db + execsql " + PRAGMA key = $hexkeyspec; + CREATE TABLE t1(a,b); + INSERT INTO t1(a,b) VALUES (1,2); + " + db close + + lappend rc [hexio_read test.db 0 16] + + sqlite_orig db test.db + execsql " + PRAGMA key = $hexkeyspec; + SELECT count(*) FROM t1; + PRAGMA cipher_plaintext_header_size = 32; + PRAGMA user_version = 1; + " + db close + lappend rc [hexio_read test.db 0 21] + + sqlite_orig db test.db + lappend rc [execsql " + PRAGMA key = $hexkeyspec; + PRAGMA cipher_plaintext_header_size = 32; + SELECT count(*) FROM t1; + "] + +} {01010101010101010101010101010101 53514C69746520666F726D61742033001000010150 {ok 1}} +db close +file delete -force test.db + +# migrate a wal mode database in place to use a +# plaintext header offset by opening it, adjusting +# the pragma, and rewriting the first page +do_test test-plaintext-header-migrate-journal-wal { + set rc {} + sqlite_orig db test.db + execsql " + PRAGMA key = $hexkeyspec; + PRAGMA journal_mode = WAL; + CREATE TABLE t1(a,b); + INSERT INTO t1(a,b) VALUES (1,2); + " + db close + + lappend rc [hexio_read test.db 0 16] + + sqlite_orig db test.db + lappend rc [execsql " + PRAGMA key = $hexkeyspec; + SELECT count(*) FROM t1; + PRAGMA journal_mode; + PRAGMA cipher_plaintext_header_size = 32; + PRAGMA user_version = 1; + PRAGMA wal_checkpoint(FULL); + "] + db close + lappend rc [hexio_read test.db 0 21] + + sqlite_orig db test.db + lappend rc [execsql " + PRAGMA key = $hexkeyspec; + PRAGMA cipher_plaintext_header_size = 32; + SELECT count(*) FROM t1; + PRAGMA journal_mode; + "] + +} {01010101010101010101010101010101 {ok 1 wal 0 1 1} 53514C69746520666F726D61742033001000020250 {ok 1 wal}} +db close +file delete -force test.db + +# migrate a wal mode database in place to use a plaintext header +# but instead of using a raw key syntax, use a derived key +# but explicitly set the salt using cipher_salt +do_test test-plaintext-header-migrate-journal-wal-string-key-random-salt { + set rc {} + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + PRAGMA journal_mode = WAL; + CREATE TABLE t1(a,b); + INSERT INTO t1(a,b) VALUES (1,2); + } + db close + + set salt [hexio_read test.db 0 16] + + sqlite_orig db test.db + lappend rc [execsql " + PRAGMA key = 'test'; + SELECT count(*) FROM t1; + PRAGMA journal_mode; + PRAGMA cipher_plaintext_header_size = 32; + PRAGMA user_version = 1; + PRAGMA wal_checkpoint(FULL); + "] + db close + + lappend rc [hexio_read test.db 0 21] + + sqlite_orig db test.db + lappend rc [execsql " + PRAGMA key = 'test'; + PRAGMA cipher_salt = \"x'$salt'\"; + PRAGMA cipher_plaintext_header_size = 32; + SELECT count(*) FROM t1; + PRAGMA journal_mode; + "] + + +} {{ok 1 wal 0 1 1} 53514C69746520666F726D61742033001000020250 {ok 1 wal}} +db close +file delete -force test.db + +# when cipher_salt is the first statement a new salt should be generated +# and it should match the salt after key derviation occurs. At no point +# should the salt be zero +do_test plaintext-header-size-salt-first-op { + set rc {} + sqlite_orig db test.db + execsql { PRAGMA key = 'test'; } + set salt1 [execsql { + PRAGMA cipher_plaintext_header_size = 16; + PRAGMA cipher_salt; + }] + + set salt2 [execsql { + CREATE TABLE t1(a,b); + INSERT INTO t1(a,b) VALUES (1,2); + PRAGMA cipher_salt; + }] + + lappend rc [string equal $salt1 "00000000000000000000000000000000"] + lappend rc [string equal $salt2 "00000000000000000000000000000000"] + lappend rc [string equal $salt1 $salt2] +} {0 0 1} +db close +file delete -force test.db + +finish_test diff --git a/test/sqlcipher-pragmas.test b/test/sqlcipher-pragmas.test new file mode 100644 index 0000000000..2ad946bd0d --- /dev/null +++ b/test/sqlcipher-pragmas.test @@ -0,0 +1,499 @@ +# SQLCipher +# codec.test developed by Stephen Lombardo (Zetetic LLC) +# sjlombardo at zetetic dot net +# http://zetetic.net +# +# Copyright (c) 2018, ZETETIC LLC +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of the ZETETIC LLC nor the +# names of its contributors may be used to endorse or promote products +# derived from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY +# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY +# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# +# This file implements regression tests for SQLite library. The +# focus of this script is testing code cipher features. +# +# NOTE: tester.tcl has overridden the definition of sqlite3 to +# automatically pass in a key value. Thus tests in this file +# should explicitly close and open db with sqlite_orig in order +# to bypass default key assignment. + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/sqlcipher.tcl + +# verify the pragma cipher_version +# returns the currently configured +# sqlcipher version +do_test verify-pragma-cipher-version { + sqlite_orig db test.db + execsql { + PRAGMA cipher_version; + } +} {{4.13.0 community}} +db close +file delete -force test.db + +# verify the pragma cipher_use_hmac +# is set to true be default +do_test verify-pragma-cipher-use-hmac-default { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + PRAGMA cipher_use_hmac; + } +} {ok 1} +db close +file delete -force test.db + +# verify the pragma cipher_use_hmac +# reports the flag turned off +do_test verify-pragma-cipher-use-hmac-off { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + PRAGMA cipher_use_hmac = off; + PRAGMA cipher_use_hmac; + } +} {ok 0} +db close +file delete -force test.db + +# verify the pragma default_cipher_use_hmac +# is set to true by default +do_test verify-pragma-cipher-default-use-hmac-default { + sqlite_orig db test.db + execsql { + PRAGMA cipher_default_use_hmac; + } +} {1} +db close +file delete -force test.db + +# verify the pragma default_cipher_use_hmac +# reports the flag turned off +do_test verify-pragma-cipher-default-use-hmac-off { + sqlite_orig db test.db + execsql { + PRAGMA cipher_default_use_hmac = off; + PRAGMA cipher_default_use_hmac; + -- Be sure to turn cipher_default_use_hmac + -- back on or it will break later tests + -- (it's a global flag) + PRAGMA cipher_default_use_hmac = ON; + } +} {0} +db close +file delete -force test.db + +# verify the pragma default_cipher_kdf_iter +# is set to 256000 by default +do_test verify-pragma-cipher-default-kdf-iter-default { + sqlite_orig db test.db + execsql { + PRAGMA cipher_default_kdf_iter; + } +} {256000} +db close +file delete -force test.db + + +# verify the pragma default_cipher_kdf_ter +# reports changes +do_test verify-pragma-cipher-default-use-hmac-off { + sqlite_orig db test.db + execsql { + PRAGMA cipher_default_kdf_iter = 1000; + PRAGMA cipher_default_kdf_iter; + PRAGMA cipher_default_kdf_iter = 256000; + } +} {1000} +db close +file delete -force test.db + +# verify the pragma kdf_iter +# reports the default value +do_test verify-pragma-kdf-iter-reports-default { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + PRAGMA kdf_iter; + } +} {ok 256000} +db close +file delete -force test.db + +# verify the pragma kdf_iter +# reports value changed +do_test verify-pragma-kdf-iter-reports-value-changed { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + PRAGMA kdf_iter = 8000; + PRAGMA kdf_iter; + } + } {ok 8000} +db close +file delete -force test.db + +# verify the pragma fast_kdf_iter +# reports the default value +do_test verify-pragma-fast-kdf-iter-reports-default { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + PRAGMA fast_kdf_iter; + } +} {ok 2} +db close +file delete -force test.db + +# verify the pragma fast_kdf_iter +# reports value changed +do_test verify-pragma-kdf-iter-reports-value-changed { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + PRAGMA fast_kdf_iter = 4000; + PRAGMA fast_kdf_iter; + } +} {ok {PRAGMA fast_kdf_iter is deprecated, please remove from use} 4000} +db close +file delete -force test.db + +# verify the pragma cipher_page_size +# reports default value +do_test verify-pragma-cipher-page-size-default { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + PRAGMA cipher_page_size; + } +} {ok 4096} +db close +file delete -force test.db + +# verify the pragma cipher_page_size +# reports change in value +do_test verify-pragma-cipher-page-size-changed { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + PRAGMA cipher_page_size = 8192; + PRAGMA cipher_page_size; + } +} {ok 8192} +db close +file delete -force test.db + +# verify that a call to pragma page_size +# will report change via both page_size and cipher_page_size +# when there is an attached codec +do_test verify-pragma-page-size-encrypted { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + PRAGMA page_size = 8192; + PRAGMA page_size; + PRAGMA cipher_page_size; + } +} {ok 8192 8192} +db close +file delete -force test.db + +# verify that a call to pragma page_size +# will not report a change to cipher_page_size for an +# unencrypted database +do_test verify-pragma-page-size-plaintext { + sqlite_orig db test.db + execsql { + PRAGMA page_size = 8192; + PRAGMA page_size; + PRAGMA cipher_page_size; + } +} {8192} +db close +file delete -force test.db + +# verify setting cipher_store_pass before key +# does not cause segfault +do_test verify-cipher-store-pass-before-key-does-not-segfault { + sqlite_orig db test.db + execsql { + PRAGMA cipher_store_pass = 1; + PRAGMA key = 'test'; + } +} {ok} +db close +file delete -force test.db + +# verify setting cipher_store_pass results in deprecation warning +do_test verify-cipher-store-pass-deprecated { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + PRAGMA cipher_store_pass = 1; + } +} {ok {PRAGMA cipher_store_pass is deprecated, please remove from use}} +db close +file delete -force test.db + +# verify the pragma cipher +# reports the default value +if_built_with_openssl verify-pragma-cipher-default { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + PRAGMA cipher; + } +} {ok AES-256-CBC} +db close +file delete -force test.db + +# verify the pragma cipher_hmac_salt_mask reports default +do_test verify-pragma-hmac-salt-mask-reports-default { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + PRAGMA cipher_hmac_salt_mask; + } +} {ok 3a} +db close +file delete -force test.db + +# verify the pragma cipher_hmac_salt_mask reports +# reports value changed +do_test verify-pragma-hmac-salt-mask-reports-value-changed { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + PRAGMA cipher_hmac_salt_mask = "x'11'"; + PRAGMA cipher_hmac_salt_mask; + PRAGMA cipher_hmac_salt_mask = "x'3a'"; + } +} {ok {PRAGMA cipher_hmac_salt_mask is deprecated, please remove from use} 11 {PRAGMA cipher_hmac_salt_mask is deprecated, please remove from use}} +db close +file delete -force test.db + +# verify the pragma cipher_hmac_pgno reports default +do_test verify-pragma-hmac-pgno-reports-default { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + PRAGMA cipher_hmac_pgno; + } +} {ok le} +db close +file delete -force test.db + +# verify the pragma cipher_hmac_pgno +# reports value changed +do_test verify-pragma-hmac-pgno-reports-value-changed { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + PRAGMA cipher_hmac_pgno = be; + PRAGMA cipher_hmac_pgno; + PRAGMA cipher_hmac_pgno = native; + PRAGMA cipher_hmac_pgno; + PRAGMA cipher_hmac_pgno = le; + PRAGMA cipher_hmac_pgno; + } +} {ok {PRAGMA cipher_hmac_pgno is deprecated, please remove from use} be {PRAGMA cipher_hmac_pgno is deprecated, please remove from use} native {PRAGMA cipher_hmac_pgno is deprecated, please remove from use} le} +db close +file delete -force test.db + +# verify the pragma cipher_hmac_algorithm works properly +do_test verify-pragma-cipher-hmac-algorithm-reports-default { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + PRAGMA cipher_hmac_algorithm; + } +} {ok HMAC_SHA512} +db close +file delete -force test.db + +do_test verify-pragma-cipher-hmac-algorithm-reports-value-changed { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + PRAGMA cipher_hmac_algorithm = HMAC_SHA1; + PRAGMA cipher_hmac_algorithm; + } +} {ok HMAC_SHA1} +db close +file delete -force test.db + +do_test verify-pragma-cipher-default-hmac-algorithm { + sqlite_orig db test.db + execsql { + PRAGMA cipher_default_hmac_algorithm; + PRAGMA cipher_default_hmac_algorithm = HMAC_SHA1; + PRAGMA cipher_default_hmac_algorithm; + PRAGMA cipher_default_hmac_algorithm = HMAC_SHA512; + } +} {HMAC_SHA512 HMAC_SHA1} +db close +file delete -force test.db + +# verify the pragma cipher_kdf_algorithm works properly +do_test verify-pragma-cipher-kdf-algorithm-reports-default { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + PRAGMA cipher_kdf_algorithm; + } +} {ok PBKDF2_HMAC_SHA512} +db close +file delete -force test.db + +do_test verify-pragma-cipher-kdf-algorithm-reports-value-changed { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA1; + PRAGMA cipher_kdf_algorithm; + } +} {ok PBKDF2_HMAC_SHA1} +db close +file delete -force test.db + +do_test verify-pragma-cipher-default-kdf-algorithm { + sqlite_orig db test.db + execsql { + PRAGMA cipher_default_kdf_algorithm; + PRAGMA cipher_default_kdf_algorithm = PBKDF2_HMAC_SHA1; + PRAGMA cipher_default_kdf_algorithm; + PRAGMA cipher_default_kdf_algorithm = PBKDF2_HMAC_SHA512; + } +} {PBKDF2_HMAC_SHA512 PBKDF2_HMAC_SHA1} +db close +file delete -force test.db + +if_built_with_openssl verify-default-cipher { + sqlite_orig db test.db + execsql { + PRAGMA key='test'; + PRAGMA cipher; + } +} {ok AES-256-CBC} +db close +file delete -force test.db + +if_built_with_libtomcrypt verify-default-cipher { + sqlite_orig db test.db + execsql { + PRAGMA key='test'; + PRAGMA cipher; + } +} {ok aes-256-cbc} +db close +file delete -force test.db + +if_built_with_commoncrypto verify-default-cipher { + sqlite_orig db test.db + execsql { + PRAGMA key='test'; + PRAGMA cipher; + } +} {ok aes-256-cbc} +db close +file delete -force test.db + +if_built_with_nss verify-default-cipher { + sqlite_orig db test.db + execsql { + PRAGMA key='test'; + PRAGMA cipher; + } +} {ok aes-256-cbc} +db close +file delete -force test.db + +do_test verify-cipher_settings_default { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + PRAGMA cipher_settings; + } +} {ok {PRAGMA kdf_iter = 256000;} {PRAGMA cipher_page_size = 4096;} {PRAGMA cipher_use_hmac = 1;} {PRAGMA cipher_plaintext_header_size = 0;} {PRAGMA cipher_hmac_algorithm = HMAC_SHA512;} {PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA512;}} +db close +file delete -force test.db + +do_test verify-cipher_settings_v1 { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + PRAGMA cipher_compatibility = 1; + PRAGMA cipher_settings; + } +} {ok {PRAGMA kdf_iter = 4000;} {PRAGMA cipher_page_size = 1024;} {PRAGMA cipher_use_hmac = 0;} {PRAGMA cipher_plaintext_header_size = 0;} {PRAGMA cipher_hmac_algorithm = HMAC_SHA1;} {PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA1;}} +db close +file delete -force test.db + +do_test verify-cipher_default_settings_v1 { + sqlite_orig db test.db + execsql { + PRAGMA cipher_default_compatibility = 1; + PRAGMA cipher_default_settings; + PRAGMA cipher_default_compatibility = 4; + } +} {{PRAGMA cipher_default_kdf_iter = 4000;} {PRAGMA cipher_default_page_size = 1024;} {PRAGMA cipher_default_use_hmac = 0;} {PRAGMA cipher_default_plaintext_header_size = 0;} {PRAGMA cipher_default_hmac_algorithm = HMAC_SHA1;} {PRAGMA cipher_default_kdf_algorithm = PBKDF2_HMAC_SHA1;}} +db close +file delete -force test.db + +do_test verify-cipher_default_settings_default { + sqlite_orig db test.db + execsql { + PRAGMA cipher_default_settings; + } +} {{PRAGMA cipher_default_kdf_iter = 256000;} {PRAGMA cipher_default_page_size = 4096;} {PRAGMA cipher_default_use_hmac = 1;} {PRAGMA cipher_default_plaintext_header_size = 0;} {PRAGMA cipher_default_hmac_algorithm = HMAC_SHA512;} {PRAGMA cipher_default_kdf_algorithm = PBKDF2_HMAC_SHA512;}} +db close +file delete -force test.db + +do_test verify-cipher_log_source { + sqlite_orig db :memory: + execsql { + PRAGMA cipher_log_source; -- default should be ANY + PRAGMA cipher_log_source = NONE; --reset to NONE + PRAGMA cipher_log_source = PROVIDER; -- add PROVIDER to log source + PRAGMA cipher_log_source = CORE; -- add CORE to log source + PRAGMA cipher_log_source = MEMORY; -- add MEMORY to log source + PRAGMA cipher_log_source = NOTASOURCE; -- stay the same + PRAGMA cipher_log_source = ANY; -- reset to ANY + } +} {ANY NONE PROVIDER {CORE PROVIDER} {CORE MEMORY PROVIDER} {CORE MEMORY PROVIDER} ANY} +db close + +do_test verify-cipher_log_level { + sqlite_orig db :memory: + execsql { + PRAGMA cipher_log_level; -- default should be WARN + PRAGMA cipher_log_level = TRACE; + PRAGMA cipher_log_level = DEBUG; + PRAGMA cipher_log_level = INFO; + PRAGMA cipher_log_level = WARN; + PRAGMA cipher_log_level = ERROR; + PRAGMA cipher_log_level = NOTALEVEL; -- an unknown level should set back to none + } +} {WARN TRACE DEBUG INFO WARN ERROR NONE} +db close + +finish_test diff --git a/test/sqlcipher-rekey.test b/test/sqlcipher-rekey.test new file mode 100644 index 0000000000..267b890777 --- /dev/null +++ b/test/sqlcipher-rekey.test @@ -0,0 +1,275 @@ +# SQLCipher +# codec.test developed by Stephen Lombardo (Zetetic LLC) +# sjlombardo at zetetic dot net +# http://zetetic.net +# +# Copyright (c) 2018, ZETETIC LLC +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of the ZETETIC LLC nor the +# names of its contributors may be used to endorse or promote products +# derived from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY +# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY +# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# +# This file implements regression tests for SQLite library. The +# focus of this script is testing code cipher features. +# +# NOTE: tester.tcl has overridden the definition of sqlite3 to +# automatically pass in a key value. Thus tests in this file +# should explicitly close and open db with sqlite_orig in order +# to bypass default key assignment. + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/sqlcipher.tcl + +# Test rekey as first operation on an empty database +# it should raise an error +do_test rekey-as-first-op-on-empty { + sqlite_orig db test.db + + catchsql { + PRAGMA rekey = 'testkey'; + } + +} {1 {An error occurred with PRAGMA key or rekey. PRAGMA key requires a key of one or more characters. PRAGMA rekey can only be run on an existing encrypted database. Use sqlcipher_export() and ATTACH to convert encrypted/plaintext databases.}} +db close +file delete -force test.db + +# test a rekey operation as the first op on an existing database +# then test that now the new key opens the database +# now close database re-open with new key +setup test.db "'testkey'" +do_test rekey-as-first-operation { + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA rekey = 'testkeynew'; + } + db close + + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkeynew'; + SELECT name FROM sqlite_schema WHERE type='table'; + } +} {ok t1} +db close +file delete -force test.db + +# create a new database, insert some data +# then rekey it with the same password +do_test rekey-same-passkey { + sqlite_orig db test.db + + execsql { + PRAGMA key = 'test123'; + CREATE TABLE t1(a,b); + BEGIN; + } + + for {set i 1} {$i<=1000} {incr i} { + set r [expr {int(rand()*500000)}] + execsql "INSERT INTO t1 VALUES($i,'value $r');" + } + + execsql { + COMMIT; + SELECT count(*) FROM t1; + PRAGMA rekey = 'test123'; + SELECT count(*) FROM t1; + } +} {1000 ok 1000} +db close +file delete -force test.db + +# create a new database, insert some data +# then rekey it. Make sure it is immediately +# readable. Then close it and make sure it can be +# read back +do_test rekey-and-query-1 { + sqlite_orig db test.db + + execsql { + PRAGMA key = 'test123'; + CREATE TABLE t1(a,b); + BEGIN; + } + + for {set i 1} {$i<=1000} {incr i} { + set r [expr {int(rand()*500000)}] + execsql "INSERT INTO t1 VALUES($i,'value $r');" + } + + execsql { + COMMIT; + SELECT count(*) FROM t1; + PRAGMA rekey = 'test321'; + SELECT count(*) FROM t1; + } +} {1000 ok 1000} + +db close + +do_test rekey-and-query-2 { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test321'; + SELECT count(*) FROM t1; + } +} {ok 1000} +db close +file delete -force test.db + +# create a new database, insert some data +# delete about 50% of the data +# write some new data +# delete another 50% +# then rekey it. Make sure it is immediately +# readable. Then close it and make sure it can be +# read back. This test will ensure that Secure Delete +# is enabled and all pages are being written and are not +# being optimized out by sqlite3PagerDontWrite +do_test rekey-delete-and-query-1 { + sqlite_orig db test.db + + execsql { + PRAGMA key = 'test123'; + CREATE TABLE t1(a,b); + CREATE INDEX ta_a ON t1(a); + BEGIN; + } + + for {set i 1} {$i<1000} {incr i} { + set r1 [expr {int(rand()*32767)}] + execsql "INSERT INTO t1 VALUES($i,$r1);" + } + + execsql "DELETE FROM t1 WHERE a < 500;" + + set r1 [expr {int(rand()*32767)}] + execsql "UPDATE t1 SET b = $r1 WHERE a < 750;" + + execsql "DELETE FROM t1 WHERE a > 750;" + + execsql { + COMMIT; + SELECT (count(*) > 0) FROM t1; + } +} {1} +db close + +do_test rekey-delete-and-query-2 { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test123'; + PRAGMA rekey = 'test321'; + SELECT count(*) > 1 FROM t1; + PRAGMA integrity_check; + } +} {ok ok 1 ok} +db close + +do_test rekey-delete-and-query-3 { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test321'; + SELECT count(*) > 1 FROM t1; + } +} {ok 1} +db close +file delete -force test.db + + +# same as previous test, but use WAL +do_test rekey-delete-and-query-wal-1 { + sqlite_orig db test.db + + execsql { + PRAGMA key = 'test123'; + PRAGMA journal_mode = WAL; + CREATE TABLE t1(a,b); + CREATE INDEX ta_a ON t1(a); + BEGIN; + } + + for {set i 1} {$i<1000} {incr i} { + set r1 [expr {int(rand()*32767)}] + execsql "INSERT INTO t1 VALUES($i,$r1);" + } + + execsql "DELETE FROM t1 WHERE a < 500;" + + set r1 [expr {int(rand()*32767)}] + execsql "UPDATE t1 SET b = $r1 WHERE a < 750;" + + execsql "DELETE FROM t1 WHERE a > 750;" + + execsql { + COMMIT; + SELECT (count(*) > 0) FROM t1; + } +} {1} +db close + +do_test rekey-delete-and-query-wal-2 { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test123'; + PRAGMA journal_mode = WAL; + PRAGMA rekey = 'test321'; + SELECT count(*) > 1 FROM t1; + PRAGMA integrity_check; + } +} {ok wal ok 1 ok} +db close + +do_test rekey-delete-and-query-wal-3 { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test321'; + PRAGMA journal_mode = WAL; + SELECT count(*) > 1 FROM t1; + } +} {ok wal 1} +db close +file delete -force test.db + +do_test rekey-database-by-name { + sqlite_orig db test.db + execsql { + attach database 'new.db' as new; + pragma new.key = 'foo'; + create table new.t1(a,b); + insert into new.t1(a,b) values('foo', 'bar'); + pragma new.rekey = 'bar'; + detach database new; + } + db close + + sqlite_orig db new.db + execsql { + pragma key = 'bar'; + select * from t1; + } +} {ok foo bar} +db close +file delete -force test.db +file delete -force new.db + +finish_test diff --git a/test/sqlcipher-template.test b/test/sqlcipher-template.test new file mode 100644 index 0000000000..4372ee4910 --- /dev/null +++ b/test/sqlcipher-template.test @@ -0,0 +1,41 @@ +# SQLCipher +# codec.test developed by Stephen Lombardo (Zetetic LLC) +# sjlombardo at zetetic dot net +# http://zetetic.net +# +# Copyright (c) 2018, ZETETIC LLC +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of the ZETETIC LLC nor the +# names of its contributors may be used to endorse or promote products +# derived from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY +# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY +# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# +# This file implements regression tests for SQLite library. The +# focus of this script is testing code cipher features. +# +# NOTE: tester.tcl has overridden the definition of sqlite3 to +# automatically pass in a key value. Thus tests in this file +# should explicitly close and open db with sqlite_orig in order +# to bypass default key assignment. + +#set testdir [file dirname $argv0] +#source $testdir/tester.tcl +#source $testdir/sqlcipher.tcl + +#finish_test diff --git a/test/sqlcipher-zmemory.test b/test/sqlcipher-zmemory.test new file mode 100644 index 0000000000..272fccecfe --- /dev/null +++ b/test/sqlcipher-zmemory.test @@ -0,0 +1,61 @@ + +# SQLCipher +# codec.test developed by Stephen Lombardo (Zetetic LLC) +# sjlombardo at zetetic dot net +# http://zetetic.net +# +# Copyright (c) 2018, ZETETIC LLC +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of the ZETETIC LLC nor the +# names of its contributors may be used to endorse or promote products +# derived from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY +# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY +# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# +# This file implements regression tests for SQLite library. The +# focus of this script is testing code cipher features. +# +# NOTE: tester.tcl has overridden the definition of sqlite3 to +# automatically pass in a key value. Thus tests in this file +# should explicitly close and open db with sqlite_orig in order +# to bypass default key assignment. + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/sqlcipher.tcl + +# verify memory security behavior +# initially should report OFF +# then enable, check that it is ON +# try to turn if off, but verify that it +# can't be unset. +do_test verify-memory-security { + sqlite_orig db test.db + execsql { + PRAGMA cipher_memory_security; + PRAGMA cipher_memory_security = ON; + PRAGMA cipher_memory_security; + PRAGMA cipher_memory_security = OFF; + PRAGMA cipher_memory_security; + } +} {0 1 1} +db close +file delete -force test.db + +finish_test + diff --git a/test/sqlcipher.tcl b/test/sqlcipher.tcl new file mode 100644 index 0000000000..4752316c69 --- /dev/null +++ b/test/sqlcipher.tcl @@ -0,0 +1,113 @@ +# codec.test developed by Stephen Lombardo (Zetetic LLC) +# sjlombardo at zetetic dot net +# http://zetetic.net +# +# Copyright (c) 2018, ZETETIC LLC +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of the ZETETIC LLC nor the +# names of its contributors may be used to endorse or promote products +# derived from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY +# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY +# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# +# This file implements regression tests for SQLite library. The +# focus of this script is testing code cipher features. +# +# NOTE: tester.tcl has overridden the definition of sqlite3 to +# automatically pass in a key value. Thus tests in this file +# should explicitly close and open db with sqlite_orig in order +# to bypass default key assignment. + + +set testdir [file dirname $argv0] +set sampleDir [file normalize [file dirname [file dirname $argv0]]]/sqlcipher-resources + +catch {file delete -force test.db} +file delete -force test2.db +file delete -force test3.db +file delete -force test4.db + +# If the library is not compiled with has_codec support then +# skip all tests in this file. +if {![sqlite_orig -has-codec]} { + finish_test + return +} + +proc setup {file key} { + sqlite_orig db $file + execsql "PRAGMA key=$key;" + execsql { + CREATE table t1(a,b); + INSERT INTO t1 VALUES ('test1', 'test2'); + } db + db close +} + +proc get_cipher_provider {} { + sqlite_orig db test.db + return [execsql { + PRAGMA key = 'test'; + PRAGMA cipher_provider; + }]; +} + +proc if_built_with_openssl {name cmd expected} { + if {[get_cipher_provider] == "openssl"} { + do_test $name $cmd $expected + } +} + +proc if_built_with_libtomcrypt {name cmd expected} { + if {[get_cipher_provider] == "libtomcrypt"} { + do_test $name $cmd $expected + } +} + +proc if_built_with_commoncrypto {name cmd expected} { + if {[get_cipher_provider] == "commoncrypto"} { + do_test $name $cmd $expected + } +} + +proc if_built_with_nss {name cmd expected} { + if {[get_cipher_provider] == "nss"} { + do_test $name $cmd $expected + } +} + +proc cmpFilesChunked {file1 file2 {chunksize 16384}} { + set f1 [open $file1]; fconfigure $f1 -translation binary + set f2 [open $file2]; fconfigure $f2 -translation binary + while {1} { + set d1 [read $f1 $chunksize] + set d2 [read $f2 $chunksize] + set diff [string compare $d1 $d2] + if {$diff != 0 || [eof $f1] || [eof $f2]} { + close $f1; close $f2 + return $diff + } + } + return 0 +} + +proc trace_proc sql { + global TRACE_OUT + lappend TRACE_OUT [string trim $sql] +} + diff --git a/test/sqlcipher.test b/test/sqlcipher.test new file mode 100644 index 0000000000..7d008f8d91 --- /dev/null +++ b/test/sqlcipher.test @@ -0,0 +1,65 @@ +# codec.test developed by Stephen Lombardo (Zetetic LLC) +# sjlombardo at zetetic dot net +# http://zetetic.net +# +# Copyright (c) 2018, ZETETIC LLC +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of the ZETETIC LLC nor the +# names of its contributors may be used to endorse or promote products +# derived from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY +# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY +# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# +# This file implements regression tests for SQLite library. The +# focus of this script is testing code cipher features. +# +# NOTE: tester.tcl has overridden the definition of sqlite3 to +# automatically pass in a key value. Thus tests in this file +# should explicitly close and open db with sqlite_orig in order +# to bypass default key assignment. + + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/sqlcipher.tcl +source $testdir/permutations.test + +set pretests "" + +sqlite_orig db :memory: +execsql { + PRAGMA cipher_log = 'sqlcipher-test.log'; +} +db close + +test_suite "sqlcipher" -prefix "" -description { + Runs SQLCipher tests +} -files [ + test_set $pretests \ + sqlcipher-core.test \ + sqlcipher-compatibility.test \ + sqlcipher-rekey.test \ + sqlcipher-plaintext-header.test \ + sqlcipher-pragmas.test \ + sqlcipher-integrity.test \ + sqlcipher-codecerror.test \ + sqlcipher-backup.test \ + sqlcipher-zmemory.test +] +run_test_suite sqlcipher +finish_test diff --git a/test/sqldiff1.test b/test/sqldiff1.test index 3201fb3654..5a3c11cb9c 100644 --- a/test/sqldiff1.test +++ b/test/sqldiff1.test @@ -14,16 +14,8 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl -if {$tcl_platform(platform)=="windows"} { - set PROG "sqldiff.exe" -} else { - set PROG "./sqldiff" -} -if {![file exe $PROG]} { - puts "sqldiff cannot run because $PROG is not available" - finish_test - return -} +set PROG [test_find_sqldiff] + db close forcedelete test.db test2.db sqlite3 db test.db @@ -43,6 +35,10 @@ do_test sqldiff-1.0 { DELETE FROM x2.t2 WHERE a=48; INSERT INTO x2.t1(a,b) VALUES(1234,'hello'); INSERT INTO x2.t2(a,b) VALUES(50.5,'xyzzy'); + INSERT INTO x2.t2(a,b) VALUES(51.5,''); + INSERT INTO x2.t2(a,b) VALUES(52.5,''||X'0d0a'); + INSERT INTO x2.t2(a,b) VALUES(53.5,'one'||X'0a0d'); + INSERT INTO x2.t2(a,b) VALUES(54.5,'one'||X'0a'||'two'); CREATE TABLE x2.t3(a,b,c); INSERT INTO x2.t3 VALUES(111,222,333); CREATE TABLE main.t4(x,y,z); @@ -58,8 +54,35 @@ do_test sqldiff-1.1 { INSERT INTO t1(a,b) VALUES(1234,'hello'); DELETE FROM t2 WHERE a=48; INSERT INTO t2(a,b) VALUES(50.5,'xyzzy'); +INSERT INTO t2(a,b) VALUES(51.5,''); +INSERT INTO t2(a,b) VALUES(52.5,''||X'0d0a'); +INSERT INTO t2(a,b) VALUES(53.5,'one'||X'0a0d'); +INSERT INTO t2(a,b) VALUES(54.5,'one'||X'0a' +||'two'); CREATE TABLE t3(a,b,c); INSERT INTO t3(rowid,a,b,c) VALUES(1,111,222,333); DROP TABLE t4;} +db close +forcedelete test.db test2.db +sqlite3 db test.db + +do_test sqldiff-2.0 { + db eval { + CREATE TABLE t1(a INTEGER PRIMARY KEY); + } + db close + sqlite3 db test2.db + db eval { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b); + } + db close + set line "exec $PROG test.db test2.db" + unset -nocomplain ::MSG + catch {eval $line} ::MSG +} {0} +do_test sqldiff-2.1 { + set ::MSG +} {ALTER TABLE t1 ADD COLUMN b;} + finish_test diff --git a/test/sqllimits1.test b/test/sqllimits1.test index 9508b5233d..efe656db6a 100644 --- a/test/sqllimits1.test +++ b/test/sqllimits1.test @@ -16,6 +16,7 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl +set testprefix sqllimits1 # Verify that the default per-connection limits are the same as # the compile-time hard limits. @@ -74,6 +75,13 @@ do_test sqllimits1-1.23 { sqlite3_limit db SQLITE_LIMIT_TOOBIG 123 } {-1} +# Minimum value for SQLITE_LIMIT_LENGTH is 30 +# +do_test sqllimits1-1.30 { + set prior [sqlite3_limit db SQLITE_LIMIT_LENGTH 1] + sqlite3_limit db SQLITE_LIMIT_LENGTH $prior +} 30 + # Decrease all limits by half. Verify that the new limits take. # @@ -293,8 +301,14 @@ do_test sqllimits1-5.9 { } {1 {string or blob too big}} do_test sqllimits1-5.10 { - set ::str [string repeat %J 2100] - catchsql { SELECT strftime($::str, '2003-10-31') } + # Prior to 3.37.0 strftime() allocated a large static buffer into + # which to format its output. Using that strategy, 2100 repeats was + # enough to exceed 100KiB and provoke the error. As of 3.37.0 strftime() + # uses the StrAccum functions, so it requires 12100 to fail. + # + # set ::str [string repeat %J 2100] + set ::str [string repeat %J 12100] + catchsql { SELECT length(strftime($::str, '2003-10-31')) } } {1 {string or blob too big}} do_test sqllimits1-5.11 { @@ -341,7 +355,7 @@ do_test sqllimits1-5.14.6 { } {SQLITE_TOOBIG} ifcapable utf16 { do_test sqllimits1-5.14.7 { - catch {sqlite3_bind_text16 $::STMT 1 $::str1 $np1} res + catch {sqlite3_bind_text16 $::STMT 1 $::str1 [expr $np1+1]} res set res } {SQLITE_TOOBIG} } @@ -372,7 +386,7 @@ db eval {DROP TABLE t4} sqlite3_limit db SQLITE_LIMIT_SQL_LENGTH 0x7fffffff set strvalue [string repeat A $::SQLITE_LIMIT_LENGTH] do_test sqllimits1-5.16 { - catchsql "SELECT '$strvalue'" + catchsql "SELECT '$strvalue' AS x" } [list 0 $strvalue] do_test sqllimits1-5.17.1 { catchsql "SELECT 'A$strvalue'" @@ -387,7 +401,7 @@ do_test sqllimits1-5.17.3 { } [list 1 {string or blob too big}] set blobvalue [string repeat 41 $::SQLITE_LIMIT_LENGTH] do_test sqllimits1-5.18 { - catchsql "SELECT x'$blobvalue'" + catchsql "SELECT x'$blobvalue' AS x" } [list 0 $strvalue] do_test sqllimits1-5.19 { catchsql "SELECT '41$blobvalue'" @@ -395,7 +409,7 @@ do_test sqllimits1-5.19 { unset blobvalue ifcapable datetime { - set strvalue [string repeat D [expr {$SQLITE_LIMIT_LENGTH-12}]] + set strvalue [string repeat D [expr {$SQLITE_LIMIT_LENGTH-11}]] do_test sqllimits1-5.20 { catchsql {SELECT strftime('%Y ' || $::strvalue, '2008-01-02')} } [list 0 [list "2008 $strvalue"]] @@ -700,6 +714,7 @@ if {$SQLITE_MAX_EXPR_DEPTH==0} { }] } "1 {Expression tree is too large (maximum depth $::SQLITE_MAX_EXPR_DEPTH)}" +if 0 { # Attempting to beat the expression depth limit using nested SELECT # queries causes a parser stack overflow. do_test sqllimits1-9.2 { @@ -711,7 +726,6 @@ if {$SQLITE_MAX_EXPR_DEPTH==0} { catchsql [subst { $expr }] } "1 {parser stack overflow}" -if 0 { do_test sqllimits1-9.3 { execsql { PRAGMA max_page_count = 1000000; -- 1 GB @@ -863,16 +877,21 @@ do_test sqllimits1-15.2 { # This test case doesn't really belong with the other limits tests. # It is in this file because it is taxing to run, like the limits tests. # -do_test sqllimits1-16.1 { - set ::N [expr int(([expr pow(2,32)]/50) + 1)] - expr (($::N*50) & 0xffffffff)<55 -} {1} -do_test sqllimits1-16.2 { - set ::format "[string repeat A 60][string repeat "%J" $::N]" - catchsql { - SELECT strftime($::format, 1); - } -} {1 {string or blob too big}} +# Update for 3.37.0: strftime() used to allocate a large static buffer +# into which it would write its result. With that implementation, the +# following would trigger an SQLITE_TOOBIG error. But strftime() now +# uses the StrAccum functions, causing this test to fail. +# +#do_test sqllimits1-16.1 { +# set ::N [expr int(([expr pow(2,32)]/50) + 1)] +# expr (($::N*50) & 0xffffffff)<55 +#} {1} +#do_test sqllimits1-16.2 { +# set ::format "[string repeat A 60][string repeat "%J" $::N]" +# catchsql { +# SELECT strftime($::format, 1); +# } +#} {1 {string or blob too big}} do_catchsql_test sqllimits1.17.0 { SELECT *,*,*,*,*,*,*,* FROM ( @@ -889,4 +908,78 @@ do_catchsql_test sqllimits1.17.0 { foreach {key value} [array get saved] { catch {set $key $value} } + +#------------------------------------------------------------------------- +# At one point the following caused an assert() to fail. +# +sqlite3_limit db SQLITE_LIMIT_LENGTH 10000 +set nm [string repeat x 10000] +do_catchsql_test sqllimits1-17.1 " + CREATE TABLE $nm (x PRIMARY KEY) +" {1 {string or blob too big}} + +#------------------------------------------------------------------------- +# +sqlite3_limit db SQLITE_LIMIT_COMPOUND_SELECT 10 +do_catchsql_test sqllimits1-18.1 { + CREATE TABLE b1(x); + INSERT INTO b1 VALUES(1), (2), (3), (4), (5), (6), (7), (8), (9), (10), (11); +} {0 {}} + +do_catchsql_test sqllimits1-18.2 { + INSERT INTO b1 VALUES(1), (2), (3), (4), (5), (6), (7), (8), (9), (10) + UNION VALUES(11); +} {0 {}} + +#------------------------------------------------------------------------- +# +reset_db +ifcapable utf16 { + do_execsql_test 19.0 { + PRAGMA encoding = 'utf16'; + } + set bigstr [string repeat abcdefghij 5000] + set bigstr16 [encoding convertto unicode $bigstr] + + do_test 19.1 { + string length $bigstr16 + } {100000} + + do_test 19.2 { + set ::stmt [sqlite3_prepare db "SELECT length( ? )" -1 TAIL] + sqlite3_bind_text16 $::stmt 1 $bigstr16 100000 + sqlite3_step $::stmt + set val [sqlite3_column_int $::stmt 0] + sqlite3_finalize $::stmt + set val + } {50000} + + sqlite3_limit db SQLITE_LIMIT_LENGTH 100000 + + do_test 19.3 { + set ::stmt [sqlite3_prepare db "SELECT length( ? )" -1 TAIL] + sqlite3_bind_text16 $::stmt 1 $bigstr16 100000 + sqlite3_step $::stmt + set val [sqlite3_column_int $::stmt 0] + sqlite3_finalize $::stmt + set val + } {50000} + + sqlite3_limit db SQLITE_LIMIT_LENGTH 99999 + + do_test 19.4 { + set ::stmt [sqlite3_prepare db "SELECT length( ? )" -1 TAIL] + list [catch { sqlite3_bind_text16 $::stmt 1 $bigstr16 100000 } msg] $msg + } {1 SQLITE_TOOBIG} + sqlite3_finalize $::stmt + + sqlite3_limit db SQLITE_LIMIT_LENGTH 100000 + + do_test 19.5 { + set ::stmt [sqlite3_prepare db "SELECT length( ? )" -1 TAIL] + list [catch { sqlite3_bind_text16 $::stmt 1 $bigstr16 100002 } msg] $msg + } {1 SQLITE_TOOBIG} + sqlite3_finalize $::stmt +} + finish_test diff --git a/test/starschema1.test b/test/starschema1.test new file mode 100644 index 0000000000..bb7d8aa79b --- /dev/null +++ b/test/starschema1.test @@ -0,0 +1,584 @@ +# 2024-05-28 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# Test cases for the ability of the query planner to cope with +# star-schema queries. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set ::testprefix starschema1 + +do_execsql_test 1.1 { +CREATE TABLE t1( + a01 INT, a02 INT, a03 INT, a04 INT, a05 INT, a06 INT, a07 INT, a08 INT, + a09 INT, a10 INT, a11 INT, a12 INT, a13 INT, a14 INT, a15 INT, a16 INT, + a17 INT, a18 INT, a19 INT, a20 INT, a21 INT, a22 INT, a23 INT, a24 INT, + a25 INT, a26 INT, a27 INT, a28 INT, a29 INT, a30 INT, a31 INT, a32 INT, + a33 INT, a34 INT, a35 INT, a36 INT, a37 INT, a38 INT, a39 INT, a40 INT, + a41 INT, a42 INT, a43 INT, a44 INT, a45 INT, a46 INT, a47 INT, a48 INT, + a49 INT, a50 INT, a51 INT, a52 INT, a53 INT, a54 INT, a55 INT, a56 INT, + a57 INT, a58 INT, a59 INT, a60 INT, a61 INT, a62 INT, a63 INT, d TEXT); +CREATE TABLE x01(b01 INT, c01 TEXT); +CREATE TABLE x02(b02 INT, c02 TEXT); +CREATE TABLE x03(b03 INT, c03 TEXT); +CREATE TABLE x04(b04 INT, c04 TEXT); +CREATE TABLE x05(b05 INT, c05 TEXT); +CREATE TABLE x06(b06 INT, c06 TEXT); +CREATE TABLE x07(b07 INT, c07 TEXT); +CREATE TABLE x08(b08 INT, c08 TEXT); +CREATE TABLE x09(b09 INT, c09 TEXT); +CREATE TABLE x10(b10 INT, c10 TEXT); +CREATE TABLE x11(b11 INT, c11 TEXT); +CREATE TABLE x12(b12 INT, c12 TEXT); +CREATE TABLE x13(b13 INT, c13 TEXT); +CREATE TABLE x14(b14 INT, c14 TEXT); +CREATE TABLE x15(b15 INT, c15 TEXT); +CREATE TABLE x16(b16 INT, c16 TEXT); +CREATE TABLE x17(b17 INT, c17 TEXT); +CREATE TABLE x18(b18 INT, c18 TEXT); +CREATE TABLE x19(b19 INT, c19 TEXT); +CREATE TABLE x20(b20 INT, c20 TEXT); +CREATE TABLE x21(b21 INT, c21 TEXT); +CREATE TABLE x22(b22 INT, c22 TEXT); +CREATE TABLE x23(b23 INT, c23 TEXT); +CREATE TABLE x24(b24 INT, c24 TEXT); +CREATE TABLE x25(b25 INT, c25 TEXT); +CREATE TABLE x26(b26 INT, c26 TEXT); +CREATE TABLE x27(b27 INT, c27 TEXT); +CREATE TABLE x28(b28 INT, c28 TEXT); +CREATE TABLE x29(b29 INT, c29 TEXT); +CREATE TABLE x30(b30 INT, c30 TEXT); +CREATE TABLE x31(b31 INT, c31 TEXT); +CREATE TABLE x32(b32 INT, c32 TEXT); +CREATE TABLE x33(b33 INT, c33 TEXT); +CREATE TABLE x34(b34 INT, c34 TEXT); +CREATE TABLE x35(b35 INT, c35 TEXT); +CREATE TABLE x36(b36 INT, c36 TEXT); +CREATE TABLE x37(b37 INT, c37 TEXT); +CREATE TABLE x38(b38 INT, c38 TEXT); +CREATE TABLE x39(b39 INT, c39 TEXT); +CREATE TABLE x40(b40 INT, c40 TEXT); +CREATE TABLE x41(b41 INT, c41 TEXT); +CREATE TABLE x42(b42 INT, c42 TEXT); +CREATE TABLE x43(b43 INT, c43 TEXT); +CREATE TABLE x44(b44 INT, c44 TEXT); +CREATE TABLE x45(b45 INT, c45 TEXT); +CREATE TABLE x46(b46 INT, c46 TEXT); +CREATE TABLE x47(b47 INT, c47 TEXT); +CREATE TABLE x48(b48 INT, c48 TEXT); +CREATE TABLE x49(b49 INT, c49 TEXT); +CREATE TABLE x50(b50 INT, c50 TEXT); +CREATE TABLE x51(b51 INT, c51 TEXT); +CREATE TABLE x52(b52 INT, c52 TEXT); +CREATE TABLE x53(b53 INT, c53 TEXT); +CREATE TABLE x54(b54 INT, c54 TEXT); +CREATE TABLE x55(b55 INT, c55 TEXT); +CREATE TABLE x56(b56 INT, c56 TEXT); +CREATE TABLE x57(b57 INT, c57 TEXT); +CREATE TABLE x58(b58 INT, c58 TEXT); +CREATE TABLE x59(b59 INT, c59 TEXT); +CREATE TABLE x60(b60 INT, c60 TEXT); +CREATE TABLE x61(b61 INT, c61 TEXT); +CREATE TABLE x62(b62 INT, c62 TEXT); +CREATE TABLE x63(b63 INT, c63 TEXT); +/**** Uncomment to generate actual data ************************************ +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<172800) + INSERT INTO t1 + SELECT stmtrand()%12, stmtrand()%13, stmtrand()%14, stmtrand()%15, + stmtrand()%16, stmtrand()%17, stmtrand()%18, stmtrand()%19, + stmtrand()%20, stmtrand()%21, stmtrand()%22, stmtrand()%23, + stmtrand()%24, stmtrand()%25, stmtrand()%26, stmtrand()%27, + stmtrand()%28, stmtrand()%29, stmtrand()%30, stmtrand()%31, + stmtrand()%32, stmtrand()%33, stmtrand()%34, stmtrand()%35, + stmtrand()%36, stmtrand()%37, stmtrand()%38, stmtrand()%39, + stmtrand()%40, stmtrand()%41, stmtrand()%42, stmtrand()%43, + stmtrand()%28, stmtrand()%29, stmtrand()%30, stmtrand()%31, + stmtrand()%32, stmtrand()%33, stmtrand()%34, stmtrand()%35, + stmtrand()%36, stmtrand()%37, stmtrand()%38, stmtrand()%39, + stmtrand()%40, stmtrand()%41, stmtrand()%42, stmtrand()%43, + stmtrand()%28, stmtrand()%29, stmtrand()%30, stmtrand()%31, + stmtrand()%32, stmtrand()%33, stmtrand()%34, stmtrand()%35, + stmtrand()%36, stmtrand()%37, stmtrand()%38, stmtrand()%39, + stmtrand()%40, stmtrand()%41, stmtrand()%42, stmtrand() FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<8) + INSERT INTO x01 SELECT n%4, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<12) + INSERT INTO x02 SELECT n%6, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<16) + INSERT INTO x03 SELECT n%8, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<20) + INSERT INTO x04 SELECT n%10, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<24) + INSERT INTO x05 SELECT n%12, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<32) + INSERT INTO x06 SELECT n%16, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<36) + INSERT INTO x07 SELECT n%18, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<40) + INSERT INTO x08 SELECT n%20, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<44) + INSERT INTO x09 SELECT n%22, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<48) + INSERT INTO x10 SELECT n%24, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<52) + INSERT INTO x11 SELECT n%26, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<56) + INSERT INTO x12 SELECT n%28, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<60) + INSERT INTO x13 SELECT n%30, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<64) + INSERT INTO x14 SELECT n%32, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<72) + INSERT INTO x15 SELECT n%36, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<80) + INSERT INTO x16 SELECT n%40, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<8) + INSERT INTO x17 SELECT n%4, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<12) + INSERT INTO x18 SELECT n%6, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<16) + INSERT INTO x19 SELECT n%8, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<20) + INSERT INTO x20 SELECT n%10, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<24) + INSERT INTO x21 SELECT n%12, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<32) + INSERT INTO x22 SELECT n%16, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<36) + INSERT INTO x23 SELECT n%18, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<40) + INSERT INTO x24 SELECT n%20, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<44) + INSERT INTO x25 SELECT n%22, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<48) + INSERT INTO x26 SELECT n%24, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<52) + INSERT INTO x27 SELECT n%26, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<56) + INSERT INTO x28 SELECT n%28, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<60) + INSERT INTO x29 SELECT n%30, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<64) + INSERT INTO x30 SELECT n%32, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<72) + INSERT INTO x31 SELECT n%36, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<80) + INSERT INTO x32 SELECT n%40, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<8) + INSERT INTO x33 SELECT n%4, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<12) + INSERT INTO x34 SELECT n%6, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<16) + INSERT INTO x35 SELECT n%8, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<20) + INSERT INTO x36 SELECT n%10, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<24) + INSERT INTO x37 SELECT n%12, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<32) + INSERT INTO x38 SELECT n%16, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<36) + INSERT INTO x39 SELECT n%18, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<40) + INSERT INTO x40 SELECT n%20, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<44) + INSERT INTO x41 SELECT n%22, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<48) + INSERT INTO x42 SELECT n%24, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<52) + INSERT INTO x43 SELECT n%26, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<56) + INSERT INTO x44 SELECT n%28, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<60) + INSERT INTO x45 SELECT n%30, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<64) + INSERT INTO x46 SELECT n%32, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<72) + INSERT INTO x47 SELECT n%36, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<80) + INSERT INTO x48 SELECT n%40, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<8) + INSERT INTO x49 SELECT n%4, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<12) + INSERT INTO x50 SELECT n%6, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<16) + INSERT INTO x51 SELECT n%8, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<20) + INSERT INTO x52 SELECT n%10, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<24) + INSERT INTO x53 SELECT n%12, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<32) + INSERT INTO x54 SELECT n%16, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<36) + INSERT INTO x55 SELECT n%18, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<40) + INSERT INTO x56 SELECT n%20, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<44) + INSERT INTO x57 SELECT n%22, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<48) + INSERT INTO x58 SELECT n%24, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<52) + INSERT INTO x59 SELECT n%26, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<56) + INSERT INTO x60 SELECT n%28, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<60) + INSERT INTO x61 SELECT n%30, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<64) + INSERT INTO x62 SELECT n%32, format('%d-or-0x%04x',n,n) FROM c; +WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<72) + INSERT INTO x63 SELECT n%36, format('%d-or-0x%04x',n,n) FROM c; +****************************************************************************/ +CREATE INDEX t1a01 ON t1(a01); +CREATE INDEX t1a02 ON t1(a02); +CREATE INDEX t1a03 ON t1(a03); +CREATE INDEX t1a04 ON t1(a04); +CREATE INDEX t1a05 ON t1(a05); +CREATE INDEX t1a06 ON t1(a06); +CREATE INDEX t1a07 ON t1(a07); +CREATE INDEX t1a08 ON t1(a08); +CREATE INDEX t1a09 ON t1(a09); +CREATE INDEX t1a10 ON t1(a10); +CREATE INDEX t1a11 ON t1(a11); +CREATE INDEX t1a12 ON t1(a12); +CREATE INDEX t1a13 ON t1(a13); +CREATE INDEX t1a14 ON t1(a14); +CREATE INDEX t1a15 ON t1(a15); +CREATE INDEX t1a16 ON t1(a16); +CREATE INDEX t1a17 ON t1(a17); +CREATE INDEX t1a18 ON t1(a18); +CREATE INDEX t1a19 ON t1(a19); +CREATE INDEX t1a20 ON t1(a20); +CREATE INDEX t1a21 ON t1(a21); +CREATE INDEX t1a22 ON t1(a22); +CREATE INDEX t1a23 ON t1(a23); +CREATE INDEX t1a24 ON t1(a24); +CREATE INDEX t1a25 ON t1(a25); +CREATE INDEX t1a26 ON t1(a26); +CREATE INDEX t1a27 ON t1(a27); +CREATE INDEX t1a28 ON t1(a28); +CREATE INDEX t1a29 ON t1(a29); +CREATE INDEX t1a30 ON t1(a30); +CREATE INDEX t1a31 ON t1(a31); +CREATE INDEX t1a32 ON t1(a32); +CREATE INDEX t1a33 ON t1(a33); +CREATE INDEX t1a34 ON t1(a34); +CREATE INDEX t1a35 ON t1(a35); +CREATE INDEX t1a36 ON t1(a36); +CREATE INDEX t1a37 ON t1(a37); +CREATE INDEX t1a38 ON t1(a38); +CREATE INDEX t1a39 ON t1(a39); +CREATE INDEX t1a40 ON t1(a40); +CREATE INDEX t1a41 ON t1(a41); +CREATE INDEX t1a42 ON t1(a42); +CREATE INDEX t1a43 ON t1(a43); +CREATE INDEX t1a44 ON t1(a44); +CREATE INDEX t1a45 ON t1(a45); +CREATE INDEX t1a46 ON t1(a46); +CREATE INDEX t1a47 ON t1(a47); +CREATE INDEX t1a48 ON t1(a48); +CREATE INDEX t1a49 ON t1(a49); +CREATE INDEX t1a50 ON t1(a50); +CREATE INDEX t1a51 ON t1(a51); +CREATE INDEX t1a52 ON t1(a52); +CREATE INDEX t1a53 ON t1(a53); +CREATE INDEX t1a54 ON t1(a54); +CREATE INDEX t1a55 ON t1(a55); +CREATE INDEX t1a56 ON t1(a56); +CREATE INDEX t1a57 ON t1(a57); +CREATE INDEX t1a58 ON t1(a58); +CREATE INDEX t1a59 ON t1(a59); +CREATE INDEX t1a60 ON t1(a60); +CREATE INDEX t1a61 ON t1(a61); +CREATE INDEX t1a62 ON t1(a62); +CREATE INDEX t1a63 ON t1(a63); +CREATE INDEX x01x ON x01(b01); +CREATE INDEX x02x ON x02(b02); +CREATE INDEX x03x ON x03(b03); +CREATE INDEX x04x ON x04(b04); +CREATE INDEX x05x ON x05(b05); +CREATE INDEX x06x ON x06(b06); +CREATE INDEX x07x ON x07(b07); +CREATE INDEX x08x ON x08(b08); +CREATE INDEX x09x ON x09(b09); +CREATE INDEX x10x ON x10(b10); +CREATE INDEX x11x ON x11(b11); +CREATE INDEX x12x ON x12(b12); +CREATE INDEX x13x ON x13(b13); +CREATE INDEX x14x ON x14(b14); +CREATE INDEX x15x ON x15(b15); +CREATE INDEX x16x ON x16(b16); +CREATE INDEX x17x ON x17(b17); +CREATE INDEX x18x ON x18(b18); +CREATE INDEX x19x ON x19(b19); +CREATE INDEX x20x ON x20(b20); +CREATE INDEX x21x ON x21(b21); +CREATE INDEX x22x ON x22(b22); +CREATE INDEX x23x ON x23(b23); +CREATE INDEX x24x ON x24(b24); +CREATE INDEX x25x ON x25(b25); +CREATE INDEX x26x ON x26(b26); +CREATE INDEX x27x ON x27(b27); +CREATE INDEX x28x ON x28(b28); +CREATE INDEX x29x ON x29(b29); +CREATE INDEX x30x ON x30(b30); +CREATE INDEX x31x ON x31(b31); +CREATE INDEX x32x ON x32(b32); +CREATE INDEX x33x ON x33(b33); +CREATE INDEX x34x ON x34(b34); +CREATE INDEX x35x ON x35(b35); +CREATE INDEX x36x ON x36(b36); +CREATE INDEX x37x ON x37(b37); +CREATE INDEX x38x ON x38(b38); +CREATE INDEX x39x ON x39(b39); +CREATE INDEX x40x ON x40(b40); +CREATE INDEX x41x ON x41(b41); +CREATE INDEX x42x ON x42(b42); +CREATE INDEX x43x ON x43(b43); +CREATE INDEX x44x ON x44(b44); +CREATE INDEX x45x ON x45(b45); +CREATE INDEX x46x ON x46(b46); +CREATE INDEX x47x ON x47(b47); +CREATE INDEX x48x ON x48(b48); +CREATE INDEX x49x ON x49(b49); +CREATE INDEX x50x ON x50(b50); +CREATE INDEX x51x ON x51(b51); +CREATE INDEX x52x ON x52(b52); +CREATE INDEX x53x ON x53(b53); +CREATE INDEX x54x ON x54(b54); +CREATE INDEX x55x ON x55(b55); +CREATE INDEX x56x ON x56(b56); +CREATE INDEX x57x ON x57(b57); +CREATE INDEX x58x ON x58(b58); +CREATE INDEX x59x ON x59(b59); +CREATE INDEX x60x ON x60(b60); +CREATE INDEX x61x ON x61(b61); +CREATE INDEX x62x ON x62(b62); +CREATE INDEX x63x ON x63(b63); +ANALYZE sqlite_schema; +INSERT INTO sqlite_stat1(tbl,idx,stat) VALUES + ('t1','t1a01','172800 14400'), + ('t1','t1a02','172800 13293'), + ('t1','t1a03','172800 12343'), + ('t1','t1a04','172800 11520'), + ('t1','t1a05','172800 10800'), + ('t1','t1a06','172800 10165'), + ('t1','t1a07','172800 9600'), + ('t1','t1a08','172800 9095'), + ('t1','t1a09','172800 8640'), + ('t1','t1a10','172800 8229'), + ('t1','t1a11','172800 7855'), + ('t1','t1a12','172800 7514'), + ('t1','t1a13','172800 7200'), + ('t1','t1a14','172800 6912'), + ('t1','t1a15','172800 6647'), + ('t1','t1a16','172800 6400'), + ('t1','t1a17','172800 6172'), + ('t1','t1a18','172800 5959'), + ('t1','t1a19','172800 5760'), + ('t1','t1a20','172800 5575'), + ('t1','t1a21','172800 5400'), + ('t1','t1a22','172800 5237'), + ('t1','t1a23','172800 5083'), + ('t1','t1a24','172800 4938'), + ('t1','t1a25','172800 4800'), + ('t1','t1a26','172800 4671'), + ('t1','t1a27','172800 4548'), + ('t1','t1a28','172800 4431'), + ('t1','t1a29','172800 4320'), + ('t1','t1a30','172800 4215'), + ('t1','t1a31','172800 4115'), + ('t1','t1a32','172800 4019'), + ('t1','t1a33','172800 6172'), + ('t1','t1a34','172800 5959'), + ('t1','t1a35','172800 5760'), + ('t1','t1a36','172800 5575'), + ('t1','t1a37','172800 5400'), + ('t1','t1a38','172800 5237'), + ('t1','t1a39','172800 5083'), + ('t1','t1a40','172800 4938'), + ('t1','t1a41','172800 4800'), + ('t1','t1a42','172800 4671'), + ('t1','t1a43','172800 4548'), + ('t1','t1a44','172800 4431'), + ('t1','t1a45','172800 4320'), + ('t1','t1a46','172800 4215'), + ('t1','t1a47','172800 4115'), + ('t1','t1a48','172800 4019'), + ('t1','t1a49','172800 6172'), + ('t1','t1a50','172800 5959'), + ('t1','t1a51','172800 5760'), + ('t1','t1a52','172800 5575'), + ('t1','t1a53','172800 5400'), + ('t1','t1a54','172800 5237'), + ('t1','t1a55','172800 5083'), + ('t1','t1a56','172800 4938'), + ('t1','t1a57','172800 4800'), + ('t1','t1a58','172800 4671'), + ('t1','t1a59','172800 4548'), + ('t1','t1a60','172800 4431'), + ('t1','t1a61','172800 4320'), + ('t1','t1a62','172800 4215'), + ('t1','t1a63','172800 4115'), + ('x01','x01x','80 2'), + ('x02','x02x','120 2'), + ('x03','x03x','160 2'), + ('x04','x04x','20 2'), + ('x05','x05x','24 2'), + ('x06','x06x','32 2'), + ('x07','x07x','36 2'), + ('x08','x08x','40 2'), + ('x09','x09x','44 2'), + ('x10','x10x','48 2'), + ('x11','x11x','52 2'), + ('x12','x12x','56 2'), + ('x13','x13x','60 2'), + ('x14','x14x','64 2'), + ('x15','x15x','72 2'), + ('x16','x16x','80 2'), + ('x17','x17x','80 2'), + ('x18','x18x','120 2'), + ('x19','x19x','160 2'), + ('x20','x20x','20 2'), + ('x21','x21x','24 2'), + ('x22','x22x','32 2'), + ('x23','x23x','36 2'), + ('x24','x24x','40 2'), + ('x25','x25x','44 2'), + ('x26','x26x','48 2'), + ('x27','x27x','52 2'), + ('x28','x28x','56 2'), + ('x29','x29x','60 2'), + ('x30','x30x','64 2'), + ('x31','x31x','72 2'), + ('x32','x32x','80 2'), + ('x33','x33x','80 2'), + ('x34','x34x','120 2'), + ('x35','x35x','160 2'), + ('x36','x36x','20 2'), + ('x37','x37x','24 2'), + ('x38','x38x','32 2'), + ('x39','x39x','36 2'), + ('x40','x40x','40 2'), + ('x41','x41x','44 2'), + ('x42','x42x','48 2'), + ('x43','x43x','52 2'), + ('x44','x44x','56 2'), + ('x45','x45x','60 2'), + ('x46','x46x','64 2'), + ('x47','x47x','72 2'), + ('x48','x48x','80 2'), + ('x49','x49x','80 2'), + ('x50','x50x','120 2'), + ('x51','x51x','160 2'), + ('x52','x52x','20 2'), + ('x53','x53x','24 2'), + ('x54','x54x','32 2'), + ('x55','x55x','36 2'), + ('x56','x56x','40 2'), + ('x57','x57x','44 2'), + ('x58','x58x','48 2'), + ('x59','x59x','52 2'), + ('x60','x60x','56 2'), + ('x61','x61x','60 2'), + ('x62','x62x','64 2'), + ('x63','x63x','72 2'); +ANALYZE sqlite_schema; +} +do_execsql_test 1.2 { + EXPLAIN QUERY PLAN + SELECT c01, c02, c03 + FROM t1, x01, x02, x03 + WHERE a01=b01 AND a02=b02 AND a03=b03; +} {/SCAN t1.*SEARCH.*SEARCH.*SEARCH/} +do_execsql_test 1.3 { + EXPLAIN QUERY PLAN + SELECT c01, c02, c03, c04 + FROM t1, x01, x02, x03, x04 + WHERE a01=b01 AND a02=b02 AND a03=b03 AND a04=b04; +} {/SCAN .*SEARCH .*SEARCH .*SEARCH .*SEARCH /} +do_execsql_test 1.4 { + EXPLAIN QUERY PLAN + SELECT c01, c02, c03, c04, c05 + FROM t1, x01, x02, x03, x04, x05 + WHERE a01=b01 AND a02=b02 AND a03=b03 AND a04=b04 AND a05=b05; +} {/SCAN .*SEARCH .*SEARCH .*SEARCH .*SEARCH .*SEARCH/} +do_execsql_test 1.5 { + EXPLAIN QUERY PLAN + SELECT c01, c02, c03, c04, c05, c06 + FROM t1, x01, x02, x03, x04, x05, x06 + WHERE a01=b01 AND a02=b02 AND a03=b03 AND a04=b04 AND a05=b05 + AND a06=b06; +} {/SCAN .*SEARCH .*SEARCH .*SEARCH .*SEARCH .*SEARCH .*SEARCH/} +do_execsql_test 1.6 { + EXPLAIN QUERY PLAN + SELECT c01, c02, c03, c04, c05, c06, c07 + FROM t1, x01, x02, x03, x04, x05, x06, x07 + WHERE a01=b01 AND a02=b02 AND a03=b03 AND a04=b04 AND a05=b05 + AND a06=b06 AND a07=b07; +} {/SCAN .*SEARCH .*SEARCH .*SEARCH .*SEARCH .*SEARCH .*SEARCH .*SEARCH/} +do_execsql_test 1.7 { + EXPLAIN QUERY PLAN + SELECT c01, c02, c03, c04, c05, c06, c07, c08 + FROM t1, x01, x02, x03, x04, x05, x06, x07, x08 + WHERE a01=b01 AND a02=b02 AND a03=b03 AND a04=b04 AND a05=b05 + AND a06=b06 AND a07=b07 AND a08=b08; +} {~/SCAN.*SCAN/} +do_execsql_test 1.8 { + EXPLAIN QUERY PLAN + SELECT c01, c02, c03, c04, c05, c06, c07, c08, + c09, c10, c11, c12, c13, c14, c15, c16, + c17, c18, c19, c20, c21, c22, c23, c24, + c25, c26, c27, c28, c29, c30, c31, c32 + FROM t1, x01, x02, x03, x04, x05, x06, x07, x08, + x09, x10, x11, x12, x13, x14, x15, x16, + x17, x18, x19, x20, x21, x22, x23, x24, + x25, x26, x27, x28, x29, x30, x31, x32 + WHERE a01=b01 AND a02=b02 AND a03=b03 AND a04=b04 AND a05=b05 AND a06=b06 + AND a07=b07 AND a08=b08 AND a09=b09 AND a10=b10 AND a11=b11 AND a12=b12 + AND a13=b13 AND a14=b14 AND a15=b15 AND a16=b16 AND a17=b17 AND a18=b18 + AND a19=b19 AND a20=b20 AND a21=b21 AND a22=b22 AND a23=b23 AND a24=b24 + AND a25=b25 AND a26=b26 AND a27=b27 AND a28=b28 AND a29=b29 AND a30=b30 + AND a31=b31 AND a32=b32; +} {~/SCAN.*SCAN/} +do_execsql_test 1.9 { + EXPLAIN QUERY PLAN + SELECT c01, c02, c03, c04, c05, c06, c07, c08, + c09, c10, c11, c12, c13, c14, c15, c16, + c17, c18, c19, c20, c21, c22, c23, c24, + c25, c26, c27, c28, c29, c30, c31, c32, + c33, c34, c35, c36, c37, c38, c39, c40, + c41, c42, c43, c44, c45, c46, c47, c48, + c49, c50, c51, c52, c53, c54, c55, c56, + c57, c58, c59, c60, c61, c62, c63 + FROM t1, x01, x02, x03, x04, x05, x06, x07, x08, + x09, x10, x11, x12, x13, x14, x15, x16, + x17, x18, x19, x20, x21, x22, x23, x24, + x25, x26, x27, x28, x29, x30, x31, x32, + x33, x34, x35, x36, x37, x38, x39, x40, + x41, x42, x43, x44, x45, x46, x47, x48, + x49, x50, x51, x52, x53, x54, x55, x56, + x57, x58, x59, x60, x61, x62, x63 + WHERE a01=b01 AND a02=b02 AND a03=b03 AND a04=b04 AND a05=b05 AND a06=b06 + AND a07=b07 AND a08=b08 AND a09=b09 AND a10=b10 AND a11=b11 AND a12=b12 + AND a13=b13 AND a14=b14 AND a15=b15 AND a16=b16 AND a17=b17 AND a18=b18 + AND a19=b19 AND a20=b20 AND a21=b21 AND a22=b22 AND a23=b23 AND a24=b24 + AND a25=b25 AND a26=b26 AND a27=b27 AND a28=b28 AND a29=b29 AND a30=b30 + AND a31=b31 AND a32=b32 AND a33=b33 AND a34=b34 AND a35=b35 AND a36=b36 + AND a37=b37 AND a38=b38 AND a39=b39 AND a40=b40 AND a41=b41 AND a42=b42 + AND a43=b43 AND a44=b44 AND a45=b45 AND a46=b46 AND a47=b47 AND a48=b48 + AND a49=b49 AND a50=b50 AND a51=b51 AND a52=b52 AND a53=b53 AND a54=b54 + AND a55=b55 AND a56=b56 AND a57=b57 AND a58=b58 AND a59=b59 AND a60=b60 + AND a61=b61 AND a62=b62 AND a63=b63; +} {~/SCAN.*SCAN/} + + + +finish_test diff --git a/test/startup.c b/test/startup.c new file mode 100644 index 0000000000..7262189229 --- /dev/null +++ b/test/startup.c @@ -0,0 +1,628 @@ +/* +** 2021-01-01 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** This file implements a program used to measure the start-up performance +** of SQLite. +** +** To use: +** +** ./startup init +** valgrind --tool=cachegrind ./startup run +** +** +** The "./startup init" command creates the test database file named +** "startup.db". The performance test is run by the "./startup run" +** command. That command does nothing but open the database file and +** parse the entire schema. +*/ +#include <stdio.h> +#include <unistd.h> +#include <string.h> +#include <stdlib.h> +#include <ctype.h> +#include "sqlite3.h" + +static const char zHelp[] = + "Usage: %s COMMAND\n" + "Commands:\n" + " init Initialized the startup.db database file\n" + " run Run the startup performance test\n" + "Options:\n" + " --dbname NAME Set the name of the test database file\n" + " --heap SZ MIN Memory allocator uses SZ bytes & min allocation MIN\n" + " --stats Show statistics at the end\n" +/* TBD + " --journal M Set the journal_mode to M\n" + " --lookaside N SZ Configure lookaside for N slots of SZ bytes each\n" + " --mmap SZ MMAP the first SZ bytes of the database file\n" + " --multithread Set multithreaded mode\n" + " --nomemstat Disable memory statistics\n" + " --pagesize N Set the page size to N\n" + " --pcache N SZ Configure N pages of pagecache each of size SZ bytes\n" + " --serialized Set serialized threading mode\n" + " --singlethread Set single-threaded mode - disables all mutexing\n" + " --utf16be Set text encoding to UTF-16BE\n" + " --utf16le Set text encoding to UTF-16LE\n" + " --utf8 Set text encoding to UTF-8\n" +*/ +; + +static void usage(const char *argv0){ + printf(zHelp, argv0); + exit(1); +} + +/* +** The test schema is derived from the Fossil repository for SQLite itself. +** The schema covers the repository, the local checkout database, and +** the global configuration database. +*/ +static const char zTestSchema[] = + "CREATE TABLE repo_blob(\n" + " rid INTEGER PRIMARY KEY,\n" + " rcvid INTEGER,\n" + " size INTEGER,\n" + " uuid TEXT UNIQUE NOT NULL,\n" + " content BLOB,\n" + " CHECK( length(uuid)>=40 AND rid>0 )\n" + ");\n" + "CREATE TABLE repo_delta(\n" + " rid INTEGER PRIMARY KEY,\n" + " srcid INTEGER NOT NULL REFERENCES blob\n" + ");\n" + "CREATE TABLE repo_rcvfrom(\n" + " rcvid INTEGER PRIMARY KEY,\n" + " uid INTEGER REFERENCES user,\n" + " mtime DATETIME,\n" + " nonce TEXT UNIQUE,\n" + " ipaddr TEXT\n" + ");\n" + "CREATE TABLE repo_private(rid INTEGER PRIMARY KEY);\n" + "CREATE TABLE repo_accesslog(\n" + " uname TEXT,\n" + " ipaddr TEXT,\n" + " success BOOLEAN,\n" + " mtime TIMESTAMP);\n" + "CREATE TABLE repo_user(\n" + " uid INTEGER PRIMARY KEY,\n" + " login TEXT UNIQUE,\n" + " pw TEXT,\n" + " cap TEXT,\n" + " cookie TEXT,\n" + " ipaddr TEXT,\n" + " cexpire DATETIME,\n" + " info TEXT,\n" + " mtime DATE,\n" + " photo BLOB\n" + ");\n" + "CREATE TABLE repo_reportfmt(\n" + " rn INTEGER PRIMARY KEY,\n" + " owner TEXT,\n" + " title TEXT UNIQUE,\n" + " mtime INTEGER,\n" + " cols TEXT,\n" + " sqlcode TEXT\n" + ");\n" + "CREATE TABLE repo_sqlite_stat2(tbl,idx,sampleno,sample);\n" + "CREATE TABLE repo_sqlite_stat1(tbl,idx,stat);\n" + "CREATE TABLE repo_sqlite_stat3(tbl,idx,neq,nlt,ndlt,sample);\n" + "CREATE TABLE repo_config(\n" + " name TEXT PRIMARY KEY NOT NULL,\n" + " value CLOB, mtime INTEGER,\n" + " CHECK( typeof(name)='text' AND length(name)>=1 )\n" + ") WITHOUT ROWID;\n" + "CREATE TABLE repo_shun(uuid PRIMARY KEY,\n" + " mtime INTEGER,\n" + " scom TEXT) WITHOUT ROWID;\n" + "CREATE TABLE repo_concealed(\n" + " hash TEXT PRIMARY KEY,\n" + " content TEXT\n" + ", mtime INTEGER) WITHOUT ROWID;\n" + "CREATE TABLE repo_admin_log(\n" + " id INTEGER PRIMARY KEY,\n" + " time INTEGER, -- Seconds since 1970\n" + " page TEXT, -- path of page\n" + " who TEXT, -- User who made the change\n" + " what TEXT -- What changed\n" + ");\n" + "CREATE TABLE repo_unversioned(\n" + " name TEXT PRIMARY KEY,\n" + " rcvid INTEGER,\n" + " mtime DATETIME,\n" + " hash TEXT,\n" + " sz INTEGER,\n" + " encoding INT,\n" + " content BLOB\n" + ") WITHOUT ROWID;\n" + "CREATE TABLE repo_subscriber(\n" + " subscriberId INTEGER PRIMARY KEY,\n" + " subscriberCode BLOB DEFAULT (randomblob(32)) UNIQUE,\n" + " semail TEXT UNIQUE COLLATE nocase,\n" + " suname TEXT,\n" + " sverified BOOLEAN DEFAULT true,\n" + " sdonotcall BOOLEAN,\n" + " sdigest BOOLEAN,\n" + " ssub TEXT,\n" + " sctime INTDATE,\n" + " mtime INTDATE,\n" + " smip TEXT\n" + ");\n" + "CREATE TABLE repo_pending_alert(\n" + " eventid TEXT PRIMARY KEY,\n" + " sentSep BOOLEAN DEFAULT false,\n" + " sentDigest BOOLEAN DEFAULT false\n" + ", sentMod BOOLEAN DEFAULT false) WITHOUT ROWID;\n" + "CREATE INDEX repo_delta_i1 ON repo_delta(srcid);\n" + "CREATE INDEX repo_blob_rcvid ON repo_blob(rcvid);\n" + "CREATE INDEX repo_subscriberUname\n" + " ON repo_subscriber(suname) WHERE suname IS NOT NULL;\n" + "CREATE VIEW repo_artifact(rid,rcvid,size,atype,srcid,hash,content) AS\n" + " SELECT blob.rid,rcvid,size,1,srcid,uuid,content\n" + " FROM repo_blob LEFT JOIN repo_delta ON (blob.rid=delta.rid);\n" + "CREATE TABLE repo_filename(\n" + " fnid INTEGER PRIMARY KEY,\n" + " name TEXT UNIQUE\n" + ");\n" + "CREATE TABLE repo_mlink(\n" + " mid INTEGER,\n" + " fid INTEGER,\n" + " pmid INTEGER,\n" + " pid INTEGER,\n" + " fnid INTEGER REFERENCES filename,\n" + " pfnid INTEGER,\n" + " mperm INTEGER,\n" + " isaux BOOLEAN DEFAULT 0\n" + ");\n" + "CREATE INDEX repo_mlink_i1 ON repo_mlink(mid);\n" + "CREATE INDEX repo_mlink_i2 ON repo_mlink(fnid);\n" + "CREATE INDEX repo_mlink_i3 ON repo_mlink(fid);\n" + "CREATE INDEX repo_mlink_i4 ON repo_mlink(pid);\n" + "CREATE TABLE repo_plink(\n" + " pid INTEGER REFERENCES blob,\n" + " cid INTEGER REFERENCES blob,\n" + " isprim BOOLEAN,\n" + " mtime DATETIME,\n" + " baseid INTEGER REFERENCES blob,\n" + " UNIQUE(pid, cid)\n" + ");\n" + "CREATE INDEX repo_plink_i2 ON repo_plink(cid,pid);\n" + "CREATE TABLE repo_leaf(rid INTEGER PRIMARY KEY);\n" + "CREATE TABLE repo_event(\n" + " type TEXT,\n" + " mtime DATETIME,\n" + " objid INTEGER PRIMARY KEY,\n" + " tagid INTEGER,\n" + " uid INTEGER REFERENCES user,\n" + " bgcolor TEXT,\n" + " euser TEXT,\n" + " user TEXT,\n" + " ecomment TEXT,\n" + " comment TEXT,\n" + " brief TEXT,\n" + " omtime DATETIME\n" + ");\n" + "CREATE INDEX repo_event_i1 ON repo_event(mtime);\n" + "CREATE TABLE repo_phantom(\n" + " rid INTEGER PRIMARY KEY\n" + ");\n" + "CREATE TABLE repo_orphan(\n" + " rid INTEGER PRIMARY KEY,\n" + " baseline INTEGER\n" + ");\n" + "CREATE INDEX repo_orphan_baseline ON repo_orphan(baseline);\n" + "CREATE TABLE repo_unclustered(\n" + " rid INTEGER PRIMARY KEY\n" + ");\n" + "CREATE TABLE repo_unsent(\n" + " rid INTEGER PRIMARY KEY\n" + ");\n" + "CREATE TABLE repo_tag(\n" + " tagid INTEGER PRIMARY KEY,\n" + " tagname TEXT UNIQUE\n" + ");\n" + "CREATE TABLE repo_tagxref(\n" + " tagid INTEGER REFERENCES tag,\n" + " tagtype INTEGER,\n" + " srcid INTEGER REFERENCES blob,\n" + " origid INTEGER REFERENCES blob,\n" + " value TEXT,\n" + " mtime TIMESTAMP,\n" + " rid INTEGER REFERENCE blob,\n" + " UNIQUE(rid, tagid)\n" + ");\n" + "CREATE INDEX repo_tagxref_i1 ON repo_tagxref(tagid, mtime);\n" + "CREATE TABLE repo_backlink(\n" + " target TEXT,\n" + " srctype INT,\n" + " srcid INT,\n" + " mtime TIMESTAMP,\n" + " UNIQUE(target, srctype, srcid)\n" + ");\n" + "CREATE INDEX repo_backlink_src ON repo_backlink(srcid, srctype);\n" + "CREATE TABLE repo_attachment(\n" + " attachid INTEGER PRIMARY KEY,\n" + " isLatest BOOLEAN DEFAULT 0,\n" + " mtime TIMESTAMP,\n" + " src TEXT,\n" + " target TEXT,\n" + " filename TEXT,\n" + " comment TEXT,\n" + " user TEXT\n" + ");\n" + "CREATE INDEX repo_attachment_idx1\n" + " ON repo_attachment(target, filename, mtime);\n" + "CREATE INDEX repo_attachment_idx2 ON repo_attachment(src);\n" + "CREATE TABLE repo_cherrypick(\n" + " parentid INT,\n" + " childid INT,\n" + " isExclude BOOLEAN DEFAULT false,\n" + " PRIMARY KEY(parentid, childid)\n" + ") WITHOUT ROWID;\n" + "CREATE INDEX repo_cherrypick_cid ON repo_cherrypick(childid);\n" + "CREATE TABLE repo_ticket(\n" + " -- Do not change any column that begins with tkt_\n" + " tkt_id INTEGER PRIMARY KEY,\n" + " tkt_uuid TEXT UNIQUE,\n" + " tkt_mtime DATE,\n" + " tkt_ctime DATE,\n" + " -- Add as many fields as required below this line\n" + " type TEXT,\n" + " status TEXT,\n" + " subsystem TEXT,\n" + " priority TEXT,\n" + " severity TEXT,\n" + " foundin TEXT,\n" + " private_contact TEXT,\n" + " resolution TEXT,\n" + " title TEXT,\n" + " comment TEXT\n" + ");\n" + "CREATE TABLE repo_ticketchng(\n" + " -- Do not change any column that begins with tkt_\n" + " tkt_id INTEGER REFERENCES ticket,\n" + " tkt_rid INTEGER REFERENCES blob,\n" + " tkt_mtime DATE,\n" + " -- Add as many fields as required below this line\n" + " login TEXT,\n" + " username TEXT,\n" + " mimetype TEXT,\n" + " icomment TEXT\n" + ");\n" + "CREATE INDEX repo_ticketchng_idx1 ON repo_ticketchng(tkt_id, tkt_mtime);\n" + "CREATE TRIGGER repo_alert_trigger1\n" + "AFTER INSERT ON repo_event BEGIN\n" + " INSERT INTO repo_pending_alert(eventid)\n" + " SELECT printf('%.1c%d',new.type,new.objid) WHERE true\n" + " ON CONFLICT(eventId) DO NOTHING;\n" + "END;\n" + "CREATE TABLE repo_vcache(\n" + " vid INTEGER, -- check-in ID\n" + " fname TEXT, -- filename\n" + " rid INTEGER, -- artifact ID\n" + " PRIMARY KEY(vid,fname)\n" + ") WITHOUT ROWID;\n" + "CREATE TABLE localdb_vvar(\n" + " name TEXT PRIMARY KEY NOT NULL,\n" + " value CLOB,\n" + " CHECK( typeof(name)='text' AND length(name)>=1 )\n" + ");\n" + "CREATE TABLE localdb_vfile(\n" + " id INTEGER PRIMARY KEY,\n" + " vid INTEGER REFERENCES blob,\n" + " chnged INT DEFAULT 0,\n" + " deleted BOOLEAN DEFAULT 0,\n" + " isexe BOOLEAN,\n" + " islink BOOLEAN,\n" + " rid INTEGER,\n" + " mrid INTEGER,\n" + " mtime INTEGER,\n" + " pathname TEXT,\n" + " origname TEXT, mhash,\n" + " UNIQUE(pathname,vid)\n" + ");\n" + "CREATE TABLE localdb_sqlite_stat1(tbl,idx,stat);\n" + "CREATE TABLE localdb_vcache(\n" + " vid INTEGER, -- check-in ID\n" + " fname TEXT, -- filename\n" + " rid INTEGER, -- artifact ID\n" + " PRIMARY KEY(vid,fname)\n" + ") WITHOUT ROWID;\n" + "CREATE TABLE localdb_stash(\n" + " stashid INTEGER PRIMARY KEY,\n" + " vid INTEGER,\n" + " hash TEXT,\n" + " comment TEXT,\n" + " ctime TIMESTAMP\n" + ");\n" + "CREATE TABLE localdb_stashfile(\n" + " stashid INTEGER REFERENCES stash,\n" + " isAdded BOOLEAN,\n" + " isRemoved BOOLEAN,\n" + " isExec BOOLEAN,\n" + " isLink BOOLEAN,\n" + " rid INTEGER,\n" + " hash TEXT,\n" + " origname TEXT,\n" + " newname TEXT,\n" + " delta BLOB,\n" + " PRIMARY KEY(newname, stashid)\n" + ");\n" + "CREATE TABLE localdb_vmerge(\n" + " id INTEGER REFERENCES vfile,\n" + " merge INTEGER,\n" + " mhash TEXT\n" + ");\n" + "CREATE UNIQUE INDEX localdb_vmergex1 ON localdb_vmerge(id,mhash);\n" + "CREATE TRIGGER localdb_vmerge_ck1 AFTER INSERT ON localdb_vmerge\n" + "WHEN new.mhash IS NULL BEGIN\n" + " SELECT raise(FAIL,\n" + " 'trying to update a newer checkout with an older version of Fossil');\n" + "END;\n" + "CREATE TABLE configdb_global_config(\n" + " name TEXT PRIMARY KEY,\n" + " value TEXT\n" + ");\n" + "CREATE TABLE configdb_sqlite_stat1(tbl,idx,stat);\n" +; + +#ifdef __linux__ +#include <sys/types.h> +#include <unistd.h> + +/* +** Attempt to display I/O stats on Linux using /proc/PID/io +*/ +static void displayLinuxIoStats(FILE *out){ + FILE *in; + char z[200]; + sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid()); + in = fopen(z, "rb"); + if( in==0 ) return; + while( fgets(z, sizeof(z), in)!=0 ){ + static const struct { + const char *zPattern; + const char *zDesc; + } aTrans[] = { + { "rchar: ", "Bytes received by read():" }, + { "wchar: ", "Bytes sent to write():" }, + { "syscr: ", "Read() system calls:" }, + { "syscw: ", "Write() system calls:" }, + { "read_bytes: ", "Bytes rcvd from storage:" }, + { "write_bytes: ", "Bytes sent to storage:" }, + { "cancelled_write_bytes: ", "Cancelled write bytes:" }, + }; + int i; + for(i=0; i<sizeof(aTrans)/sizeof(aTrans[0]); i++){ + int n = (int)strlen(aTrans[i].zPattern); + if( strncmp(aTrans[i].zPattern, z, n)==0 ){ + fprintf(out, "-- %-28s %s", aTrans[i].zDesc, &z[n]); + break; + } + } + } + fclose(in); +} +#endif + +/* +** Return the value of a hexadecimal digit. Return -1 if the input +** is not a hex digit. +*/ +static int hexDigitValue(char c){ + if( c>='0' && c<='9' ) return c - '0'; + if( c>='a' && c<='f' ) return c - 'a' + 10; + if( c>='A' && c<='F' ) return c - 'A' + 10; + return -1; +} + +/* +** Interpret zArg as an integer value, possibly with suffixes. +*/ +static int integerValue(const char *zArg){ + sqlite3_int64 v = 0; + static const struct { char *zSuffix; int iMult; } aMult[] = { + { "KiB", 1024 }, + { "MiB", 1024*1024 }, + { "GiB", 1024*1024*1024 }, + { "KB", 1000 }, + { "MB", 1000000 }, + { "GB", 1000000000 }, + { "K", 1000 }, + { "M", 1000000 }, + { "G", 1000000000 }, + }; + int i; + int isNeg = 0; + if( zArg[0]=='-' ){ + isNeg = 1; + zArg++; + }else if( zArg[0]=='+' ){ + zArg++; + } + if( zArg[0]=='0' && zArg[1]=='x' ){ + int x; + zArg += 2; + while( (x = hexDigitValue(zArg[0]))>=0 ){ + v = (v<<4) + x; + zArg++; + } + }else{ + while( isdigit(zArg[0]) ){ + v = v*10 + zArg[0] - '0'; + zArg++; + } + } + for(i=0; i<sizeof(aMult)/sizeof(aMult[0]); i++){ + if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){ + v *= aMult[i].iMult; + break; + } + } + if( v>0x7fffffff ){ + printf("ERROR: parameter too large - max 2147483648\n"); + exit(1); + } + return (int)(isNeg? -v : v); +} + + +int main(int argc, char **argv){ + const char *zCmd = 0; + int i; + int bAutovac = 0; + int showStats = 0; + const char *zDbName = "./startup.db"; + int nHeap = 0; + int mnHeap = 0; + + for(i=1; i<argc; i++){ + const char *z = argv[i]; + if( z[0]!='-' ){ + if( zCmd ){ + usage(argv[0]); + } + zCmd = z; + continue; + } + if( z[1]=='-' ) z++; + if( strcmp(z, "-autovacuum")==0 ){ + bAutovac = 1; + }else + if( strcmp(z, "-dbname")==0 ){ + if( i==argc-1 ){ + printf("ERROR: missing argument on \"%s\"\n", argv[0]); + exit(1); + } + zDbName = argv[++i]; + }else + if( strcmp(z,"-heap")==0 ){ + if( i>=argc-2 ){ + printf("ERROR: missing arguments on %s\n", argv[i]); + exit(1); + } + nHeap = integerValue(argv[i+1]); + mnHeap = integerValue(argv[i+2]); + i += 2; + }else + if( strcmp(z,"-stats")==0 ){ + showStats = 1; + }else + { + printf("ERROR: unknown option \"%s\"\n", argv[i]); + usage(argv[0]); + } + } + if( zCmd==0 ){ + printf("ERROR: no COMMAND specified\n"); + usage(argv[0]); + } + if( strcmp(zCmd, "run")==0 ){ + sqlite3 *db; + int rc; + char *zErr = 0; + void *pHeap = 0; + if( nHeap>0 ){ + pHeap = malloc( nHeap ); + if( pHeap==0 ){ + printf("ERROR: cannot allocate %d-byte heap\n", nHeap); + exit(1); + } + rc = sqlite3_config(SQLITE_CONFIG_HEAP, pHeap, nHeap, mnHeap); + if( rc ){ + printf("ERROR: heap configuration failed: %d\n", rc); + exit(1); + } + } + rc = sqlite3_open(zDbName, &db); + if( rc ){ + printf("SQLite error: %s\n", sqlite3_errmsg(db)); + }else{ + sqlite3_exec(db, "PRAGMA synchronous", 0, 0, &zErr); + } + if( zErr ){ + printf("ERROR: %s\n", zErr); + sqlite3_free(zErr); + } + if( showStats ){ + int iCur, iHi; + sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED, &iCur, &iHi, 0); + printf("-- Lookaside Slots Used: %d (max %d)\n", iCur,iHi); + sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT, &iCur, &iHi, 0); + printf("-- Successful lookasides: %d\n", iHi); + sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, &iCur,&iHi,0); + printf("-- Lookaside size faults: %d\n", iHi); + sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, &iCur,&iHi,0); + printf("-- Lookaside OOM faults: %d\n", iHi); + sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHi, 0); + printf("-- Pager Heap Usage: %d bytes\n", iCur); + sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHi, 1); + printf("-- Page cache hits: %d\n", iCur); + sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHi, 1); + printf("-- Page cache misses: %d\n", iCur); + sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHi, 1); + printf("-- Page cache writes: %d\n", iCur); + sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHi, 0); + printf("-- Schema Heap Usage: %d bytes\n", iCur); + sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHi, 0); + printf("-- Statement Heap Usage: %d bytes\n", iCur); + } + sqlite3_close(db); + free(pHeap); + /* Global memory usage statistics printed after the database connection + ** has closed. Memory usage should be zero at this point. */ + if( showStats ){ + int iCur, iHi; + sqlite3_status(SQLITE_STATUS_MEMORY_USED, &iCur, &iHi, 0); + printf("-- Memory Used (bytes): %d (max %d)\n", iCur,iHi); + sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &iCur, &iHi, 0); + printf("-- Outstanding Allocations: %d (max %d)\n", iCur,iHi); + sqlite3_status(SQLITE_STATUS_PAGECACHE_OVERFLOW, &iCur, &iHi, 0); + printf("-- Pcache Overflow Bytes: %d (max %d)\n", iCur,iHi); + sqlite3_status(SQLITE_STATUS_MALLOC_SIZE, &iCur, &iHi, 0); + printf("-- Largest Allocation: %d bytes\n",iHi); + sqlite3_status(SQLITE_STATUS_PAGECACHE_SIZE, &iCur, &iHi, 0); + printf("-- Largest Pcache Allocation: %d bytes\n",iHi); +#ifdef __linux__ + displayLinuxIoStats(stdout); +#endif + } + return 0; + } + if( strcmp(zCmd, "init")==0 ){ + sqlite3 *db; + char *zAux; + char *zErr = 0; + int rc; + unlink(zDbName); + zAux = sqlite3_mprintf("%s-journal", zDbName); + unlink(zAux); + sqlite3_free(zAux); + zAux = sqlite3_mprintf("%s-wal", zDbName); + unlink(zAux); + sqlite3_free(zAux); + rc = sqlite3_open(zDbName, &db); + if( rc ){ + printf("SQLite error: %s\n", sqlite3_errmsg(db)); + }else{ + sqlite3_exec(db, "BEGIN", 0, 0, 0); + sqlite3_exec(db, zTestSchema, 0, 0, &zErr); + sqlite3_exec(db, "COMMIT", 0, 0, 0); + } + if( zErr ){ + printf("ERROR: %s\n", zErr); + sqlite3_free(zErr); + } + sqlite3_close(db); + return 0; + + } +} diff --git a/test/stat.test b/test/stat.test index 288153dbb3..4705844cec 100644 --- a/test/stat.test +++ b/test/stat.test @@ -21,6 +21,10 @@ ifcapable !vtab||!compound { return } +# This module uses hard-coded results that depend on exact measurements of +# pages sizes at the byte level, and hence will not work if the reserved_bytes +# value is nonzero. +if {[nonzero_reserved_bytes]} {finish_test; return;} set ::asc 1 proc a_string {n} { string range [string repeat [incr ::asc]. $n] 1 $n } @@ -28,18 +32,36 @@ db func a_string a_string register_dbstat_vtab db do_execsql_test stat-0.0 { + PRAGMA table_info(dbstat); +} {/0 name TEXT .* 1 path TEXT .* 9 pgsize INTEGER/} + +# Attempts to drop an eponymous virtual table are a no-op. +do_catchsql_test stat-0.1a { + DROP TABLE dbstat; +} {1 {table dbstat may not be dropped}} +do_execsql_test stat-0.1b { + PRAGMA table_info=dbstat; +} {/0 name TEXT .* 1 path TEXT .* 9 pgsize INTEGER/} + +db close +forcedelete test.db +sqlite3 db test.db +db func a_string a_string +register_dbstat_vtab db +do_execsql_test stat-0.2 { PRAGMA auto_vacuum = OFF; CREATE VIRTUAL TABLE temp.stat USING dbstat; SELECT * FROM stat; } {} -ifcapable wal { + +if {[wal_is_capable]} { do_execsql_test stat-0.1 { PRAGMA journal_mode = WAL; PRAGMA journal_mode = delete; SELECT name, path, pageno, pagetype, ncell, payload, unused, mx_payload FROM stat; - } {wal delete sqlite_master / 1 leaf 0 0 916 0} + } {wal delete sqlite_schema / 1 leaf 0 0 916 0} } do_test stat-1.0 { @@ -65,9 +87,9 @@ do_test stat-1.2 { do_test stat-1.3 { execsql { SELECT name, path, pageno, pagetype, ncell, payload, unused, mx_payload - FROM stat WHERE name = 'sqlite_master'; + FROM stat WHERE name = 'sqlite_schema'; } -} {sqlite_master / 1 leaf 2 77 831 40} +} {sqlite_schema / 1 leaf 2 77 831 40} do_test stat-1.4 { execsql { DROP TABLE t1; @@ -88,7 +110,7 @@ do_execsql_test stat-2.1 { INSERT INTO t3 SELECT a_string(110+rowid), a_string(221+rowid) FROM t3 ORDER BY rowid; SELECT name, path, pageno, pagetype, ncell, payload, unused, mx_payload - FROM stat WHERE name != 'sqlite_master'; + FROM stat WHERE name != 'sqlite_schema' ORDER BY name; } [list \ sqlite_autoindex_t3_1 / 3 internal 3 368 623 125 \ sqlite_autoindex_t3_1 /000/ 8 leaf 8 946 46 123 \ @@ -114,6 +136,14 @@ do_execsql_test stat-2.1 { t3 /00f/ 23 leaf 2 738 268 370 \ ] +do_execsql_test stat-2.1agg { + SELECT * FROM dbstat WHERE aggregate=TRUE ORDER BY name; +} [list \ + sqlite_autoindex_t3_1 {} 5 {} 32 3898 1065 132 {} 5120 \ + sqlite_schema {} 1 {} 2 84 824 49 {} 1024 \ + t3 {} 17 {} 47 11188 5815 370 {} 17408 \ +] + # With every index entry overflowing, make sure no pages are missed # (other than the locking page which is 64 in this test build.) # @@ -130,7 +160,7 @@ do_execsql_test stat-3.1 { CREATE INDEX i4 ON t4(x); INSERT INTO t4(rowid, x) VALUES(2, a_string(7777)); SELECT name, path, pageno, pagetype, ncell, payload, unused, mx_payload - FROM stat WHERE name != 'sqlite_master'; + FROM stat WHERE name != 'sqlite_schema' ORDER BY name; } [list \ i4 / 3 leaf 1 103 905 7782 \ i4 /000+000000 4 overflow 0 1020 0 0 \ @@ -151,6 +181,15 @@ do_execsql_test stat-3.1 { t4 /000+000006 18 overflow 0 1020 0 0 \ ] +do_execsql_test stat-3.2 { + SELECT *, '|' FROM dbstat WHERE aggregate=TRUE ORDER BY name; +} [list \ + i4 {} 9 {} 1 7782 1386 7782 {} 9216 | \ + sqlite_schema {} 1 {} 2 74 834 40 {} 1024 | \ + t4 {} 8 {} 1 7780 367 7780 {} 8192 | \ +] + + do_execsql_test stat-4.1 { CREATE TABLE t5(x); CREATE INDEX i5 ON t5(x); @@ -181,6 +220,16 @@ do_execsql_test stat-5.1 { t1 /001+000000 4 overflow 0 1020 0 0 \ ] +do_execsql_test stat-5.20 { + SELECT name, quote(path), pageno, quote(pagetype), ncell, payload, + unused, mx_payload, '|' FROM dbstat('main',1); +} {sqlite_schema NULL 1 NULL 1 34 878 34 | tx NULL 1 NULL 0 0 1016 0 |} +do_execsql_test stat-5.21 { + SELECT name, quote(path), pageno, quote(pagetype), ncell, payload, + unused, mx_payload, '|' FROM dbstat('aux1',1); +} {sqlite_schema NULL 1 NULL 1 34 878 34 | t1 NULL 3 NULL 2 3033 5 1517 |} + + do_catchsql_test stat-6.1 { CREATE VIRTUAL TABLE temp.s2 USING dbstat(mainx); } {1 {no such database: mainx}} @@ -200,27 +249,27 @@ do_execsql_test 7.1 { do_execsql_test 7.1.1 { SELECT * FROM dbstat('123'); } { - sqlite_master / 1 leaf 1 37 875 37 0 1024 + sqlite_schema / 1 leaf 1 37 875 37 0 1024 x1 / 2 leaf 1 4 1008 4 1024 1024 } do_execsql_test 7.1.2 { SELECT * FROM dbstat(123); } { - sqlite_master / 1 leaf 1 37 875 37 0 1024 + sqlite_schema / 1 leaf 1 37 875 37 0 1024 x1 / 2 leaf 1 4 1008 4 1024 1024 } do_execsql_test 7.1.3 { CREATE VIRTUAL TABLE x2 USING dbstat('123'); SELECT * FROM x2; } { - sqlite_master / 1 leaf 1 37 875 37 0 1024 + sqlite_schema / 1 leaf 1 37 875 37 0 1024 x1 / 2 leaf 1 4 1008 4 1024 1024 } do_execsql_test 7.1.4 { CREATE VIRTUAL TABLE x3 USING dbstat(123); SELECT * FROM x3; } { - sqlite_master / 1 leaf 1 37 875 37 0 1024 + sqlite_schema / 1 leaf 1 37 875 37 0 1024 x1 / 2 leaf 1 4 1008 4 1024 1024 } @@ -233,7 +282,7 @@ do_execsql_test 7.2 { do_execsql_test 7.2.1 { SELECT * FROM dbstat('123corp'); } { - sqlite_master / 1 leaf 1 37 875 37 0 1024 + sqlite_schema / 1 leaf 1 37 875 37 0 1024 x1 / 2 leaf 1 4 1008 4 1024 1024 } do_catchsql_test 7.2.2 { @@ -243,7 +292,7 @@ do_execsql_test 7.2.3 { CREATE VIRTUAL TABLE x2 USING dbstat('123corp'); SELECT * FROM x2; } { - sqlite_master / 1 leaf 1 37 875 37 0 1024 + sqlite_schema / 1 leaf 1 37 875 37 0 1024 x1 / 2 leaf 1 4 1008 4 1024 1024 } do_catchsql_test 7.2.4 { @@ -251,4 +300,29 @@ do_catchsql_test 7.2.4 { SELECT * FROM x3; } {1 {unrecognized token: "123corp"}} + +do_execsql_test 8.1 { + CREATE VIRTUAL TABLE st4 USING dbstat; +} +do_execsql_test 8.2 { + SELECT * FROM st4 WHERE st4.aggregate = NULL; +} +do_execsql_test 8.3 { + SELECT aggregate=1 FROM st4 WHERE aggregate = 5 +} +do_execsql_test 8.4 { + SELECT * FROM st4 WHERE name = NULL; +} {} +do_execsql_test 8.5 { + SELECT * FROM st4 WHERE schema = NULL; +} {} + +#------------------------------------------------------------------------- +reset_db +breakpoint +do_catchsql_test 9.1 { + CREATE TABLE dbstat(x, y); + DROP TABLE nosuchdb.dbstat; +} {/1 {(no such table: nosuchdb.dbstat|table dbstat may not be dropped)}/} + finish_test diff --git a/test/statfault.test b/test/statfault.test index ce79e328d8..19e0a67874 100644 --- a/test/statfault.test +++ b/test/statfault.test @@ -41,5 +41,14 @@ do_faultsim_test 1 -faults * -prep { faultsim_test_result {0 8} } +do_faultsim_test 2 -faults * -prep { + faultsim_restore_and_reopen + register_dbstat_vtab db + execsql { SELECT 1 FROM sqlite_master LIMIT 1 } +} -body { + db eval { SELECT * FROM sss } { db eval { SELECT randomblob(5000) } } +} -test { + faultsim_test_result {0 {}} +} finish_test diff --git a/test/stmt.test b/test/stmt.test index 49a41414b7..138ce19860 100644 --- a/test/stmt.test +++ b/test/stmt.test @@ -16,6 +16,11 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl +if {[atomic_batch_write test.db]} { + finish_test + return +} + do_test stmt-1.1 { execsql { CREATE TABLE t1(a integer primary key, b INTEGER NOT NULL) } } {} @@ -46,7 +51,8 @@ do_test stmt-1.4 { INSERT INTO t1 SELECT a+1, b+1 FROM t1; } set sqlite_open_file_count -} {3} + # 2016-03-04: statement-journal open deferred +} {2} do_test stmt-1.5 { execsql COMMIT set sqlite_open_file_count @@ -61,7 +67,8 @@ do_test stmt-1.6.1 { do_test stmt-1.6.2 { execsql { INSERT INTO t1 SELECT a+4, b+4 FROM t1 } set sqlite_open_file_count -} {3} + # 2016-03-04: statement-journal open deferred +} {2} do_test stmt-1.7 { execsql COMMIT set sqlite_open_file_count @@ -84,7 +91,7 @@ filecount stmt-2.3 { INSERT INTO t1 SELECT 9, 9 } 2 filecount stmt-2.4 { INSERT INTO t1 SELECT 9, 9; INSERT INTO t1 SELECT 10, 10; -} 3 +} 2 do_test stmt-2.5 { execsql { CREATE INDEX i1 ON t1(b) } @@ -92,6 +99,6 @@ do_test stmt-2.5 { filecount stmt-2.6 { REPLACE INTO t1 VALUES(5, 5); REPLACE INTO t1 VALUES(5, 5); -} 3 +} 2 finish_test diff --git a/test/stmtrand.test b/test/stmtrand.test new file mode 100644 index 0000000000..e2f6935b85 --- /dev/null +++ b/test/stmtrand.test @@ -0,0 +1,48 @@ +# 2024-05-24 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# Verify that the stmtrand() extension function works. +# +# Stmtrand() is a pseudo-random number generator designed for testing. +# it has the property that it returns the same sequence of pseudo-random +# numbers for each SQL statement invocation. This makes it suitable for +# testing because the results are reproducible. +# +# The optional argument is the seed for the PRNG. The seed is only used +# on the first invocation. If the argument is omitted, a seed of 0 is +# used. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix stmtrand + +load_static_extension db stmtrand + +do_execsql_test 1.1 { + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<30) + SELECT stmtrand()%10 FROM c +} {4 1 8 3 6 1 4 9 2 1 2 1 0 1 8 1 4 7 6 1 8 7 0 3 0 9 4 5 9 9} +do_execsql_test 1.2 { + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<30) + SELECT stmtrand(0)%10 FROM c +} {4 1 8 3 6 1 4 9 2 1 2 1 0 1 8 1 4 7 6 1 8 7 0 3 0 9 4 5 9 9} +do_execsql_test 1.3 { + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<30) + SELECT stmtrand(1)%10 FROM c +} {3 8 9 4 7 2 3 8 5 2 7 0 3 0 7 2 5 6 1 8 5 6 7 8 7 2 9 6 8 0} +do_execsql_test 1.4 { + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<30) + SELECT stmtrand(x)%10 FROM c +} {3 8 9 4 7 2 3 8 5 2 7 0 3 0 7 2 5 6 1 8 5 6 7 8 7 2 9 6 8 0} + + +finish_test diff --git a/test/stmtvtab1.test b/test/stmtvtab1.test new file mode 100644 index 0000000000..e8a79fdacd --- /dev/null +++ b/test/stmtvtab1.test @@ -0,0 +1,82 @@ +# 2017-06-29 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# Testing of the STMT virtual table. +# +# This also validates the SQLITE_STMTSTATUS_REPREPARE and +# SQLITE_STMTSTATUS_RUN values for sqlite3_stmt_status(). +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +ifcapable !stmtvtab { + finish_test + return +} + +db cache flush +db cache size 20 +unset -nocomplain x y z +set x giraffe +set y mango +set z alabama +do_execsql_test stmtvtab1-100 { + CREATE TABLE t1(a,b,c); + INSERT INTO t1 VALUES($a,$b,$c); + CREATE INDEX t1a ON t1(a); + SELECT run, sql FROM sqlite_stmt ORDER BY 1; +} {1 {SELECT run, sql FROM sqlite_stmt ORDER BY 1;} 1 {CREATE INDEX t1a ON t1(a);} 1 {INSERT INTO t1 VALUES($a,$b,$c);} 1 {CREATE TABLE t1(a,b,c);}} +set x neon +set y event +set z future +do_execsql_test stmtvtab1-110 { + INSERT INTO t1 VALUES($a,$b,$c); + SELECT reprep,run,SQL FROM sqlite_stmt WHERE sql LIKE '%INSERT%' AND NOT busy; +} {1 2 {INSERT INTO t1 VALUES($a,$b,$c);}} +set x network +set y fit +set z metal +do_execsql_test stmtvtab1-120 { + INSERT INTO t1 VALUES($a,$b,$c); + SELECT reprep,run,SQL FROM sqlite_stmt WHERE sql LIKE '%INSERT%' AND NOT busy; +} {1 3 {INSERT INTO t1 VALUES($a,$b,$c);}} +set x history +set y detail +set z grace +do_execsql_test stmtvtab1-130 { + CREATE INDEX t1b ON t1(b); + INSERT INTO t1 VALUES($a,$b,$c); + SELECT reprep,run,SQL FROM sqlite_stmt WHERE sql LIKE '%INSERT%' AND NOT busy; +} {2 4 {INSERT INTO t1 VALUES($a,$b,$c);}} + +# All statements are still in cache +# +do_execsql_test stmtvtab1-140 { + SELECT count(*) FROM sqlite_stmt WHERE NOT busy; +} {6} + +# None of the prepared statements should use more than a couple thousand +# bytes of memory +# +#db eval {SELECT mem, sql FROM sqlite_stmt} {puts [format {%5d %s} $mem $sql]} +do_execsql_test stmtvtab1-150 { + SELECT count(*) FROM sqlite_stmt WHERE mem>5000; +} {0} + +# Flushing the cache clears all of the prepared statements. +# +db cache flush +do_execsql_test stmtvtab1-160 { + SELECT * FROM sqlite_stmt WHERE NOT busy; +} {} + +finish_test diff --git a/test/strict1.test b/test/strict1.test new file mode 100644 index 0000000000..c4c086bdb9 --- /dev/null +++ b/test/strict1.test @@ -0,0 +1,262 @@ +# 2021-08-18 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# This file implements regression tests for SQLite library. The +# focus of this file is testing STRICT tables. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix strict1 + +# STRICT tables have on a limited number of allowed datatypes. +# +do_catchsql_test strict1-1.1 { + CREATE TABLE t1(a) STRICT; +} {1 {missing datatype for t1.a}} +do_catchsql_test strict1-1.2 { + CREATE TABLE t1(a PRIMARY KEY) STRICT, WITHOUT ROWID; +} {1 {missing datatype for t1.a}} +do_catchsql_test strict1-1.3 { + CREATE TABLE t1(a PRIMARY KEY) WITHOUT ROWID, STRICT; +} {1 {missing datatype for t1.a}} +do_catchsql_test strict1-1.4 { + CREATE TABLE t1(a BANJO PRIMARY KEY) WITHOUT ROWID, STRICT; +} {1 {unknown datatype for t1.a: "BANJO"}} +do_catchsql_test strict1-1.5 { + CREATE TABLE t1(a TEXT PRIMARY KEY, b INT, c INTEGER, d REAL, e BLOB, f DATE) strict; +} {1 {unknown datatype for t1.f: "DATE"}} +do_catchsql_test strict1-1.6 { + CREATE TABLE t1(a TEXT PRIMARY KEY, b INT, c INTEGER, d REAL, e BLOB, f TEXT(50)) WITHOUT ROWID, STRICT; +} {1 {unknown datatype for t1.f: "TEXT(50)"}} + +do_execsql_test strict1-2.0 { + CREATE TABLE t1( + a INT, + b INTEGER, + c BLOB, + d TEXT, + e REAL + ) STRICT; +} {} +ifcapable vtab { + do_execsql_test strict1-2.0a { + SELECT strict FROM pragma_table_list('t1'); + } {1} +} +do_catchsql_test strict1-2.1 { + INSERT INTO t1(a) VALUES('xyz'); +} {1 {cannot store TEXT value in INT column t1.a}} +do_catchsql_test strict1-2.2 { + INSERT INTO t1(b) VALUES('xyz'); +} {1 {cannot store TEXT value in INTEGER column t1.b}} +do_catchsql_test strict1-2.3 { + INSERT INTO t1(c) VALUES('xyz'); +} {1 {cannot store TEXT value in BLOB column t1.c}} +do_catchsql_test strict1-2.4 { + INSERT INTO t1(d) VALUES(x'3142536475'); +} {1 {cannot store BLOB value in TEXT column t1.d}} +do_catchsql_test strict1-2.5 { + INSERT INTO t1(e) VALUES('xyz'); +} {1 {cannot store TEXT value in REAL column t1.e}} + + +do_execsql_test strict1-3.1 { + INSERT INTO t1(a, b) VALUES(1,2),('3','4'),(5.0, 6.0),(null,null); + SELECT a, b, '|' FROM t1; +} {1 2 | 3 4 | 5 6 | {} {} |} +do_catchsql_test strict1-3.2 { + INSERT INTO t1(a) VALUES(1.2); +} {1 {cannot store REAL value in INT column t1.a}} +do_catchsql_test strict1-3.3 { + INSERT INTO t1(a) VALUES(x'313233'); +} {1 {cannot store BLOB value in INT column t1.a}} +do_catchsql_test strict1-3.4 { + INSERT INTO t1(b) VALUES(1.2); +} {1 {cannot store REAL value in INTEGER column t1.b}} +do_catchsql_test strict1-3.5 { + INSERT INTO t1(b) VALUES(x'313233'); +} {1 {cannot store BLOB value in INTEGER column t1.b}} + +do_execsql_test strict1-4.1 { + DELETE FROM t1; + INSERT INTO t1(c) VALUES(x'313233'), (NULL); + SELECT typeof(c), c FROM t1; +} {blob 123 null {}} +do_catchsql_test strict1-4.2 { + INSERT INTO t1(c) VALUES('456'); +} {1 {cannot store TEXT value in BLOB column t1.c}} + +do_execsql_test strict1-5.1 { + DELETE FROM t1; + INSERT INTO t1(d) VALUES('xyz'),(4),(5.5),(NULL); + SELECT typeof(d), d FROM t1; +} {text xyz text 4 text 5.5 null {}} +do_catchsql_test strict1-5.2 { + INSERT INTO t1(d) VALUES(x'4567'); +} {1 {cannot store BLOB value in TEXT column t1.d}} + +do_execsql_test strict1-6.1 { + DELETE FROM t1; + INSERT INTO t1(e) VALUES(1),(2.5),('3'),('4.5'),(6.0),(NULL); + SELECT typeof(e), e FROM t1; +} {real 1.0 real 2.5 real 3.0 real 4.5 real 6.0 null {}} +do_catchsql_test strict1-6.2 { + INSERT INTO t1(e) VALUES('xyz'); +} {1 {cannot store TEXT value in REAL column t1.e}} +do_catchsql_test strict1-6.3 { + INSERT INTO t1(e) VALUES(x'3456'); +} {1 {cannot store BLOB value in REAL column t1.e}} + +ifcapable altertable { + do_execsql_test strict1-7.1 { + DROP TABLE IF EXISTS t4; + CREATE TABLE t4( + a INT AS (b*2) VIRTUAL, + b INT AS (c*2) STORED, + c INT PRIMARY KEY + ) STRICT; + INSERT INTO t4(c) VALUES(1); + SELECT * FROM t4; + } {4 2 1} + do_catchsql_test strict1-7.2 { + ALTER TABLE t4 ADD COLUMN d VARCHAR; + } {1 {error in table t4 after add column: unknown datatype for t4.d: "VARCHAR"}} + do_catchsql_test strict1-7.3 { + ALTER TABLE t4 ADD COLUMN d; + } {1 {error in table t4 after add column: missing datatype for t4.d}} +} + +# 2022-01-17 https://sqlite.org/forum/forumpost/fa012c77796d9399 +# +reset_db +do_execsql_test strict1-8.1 { + CREATE TABLE csv_import_table ( + "debit" TEXT, + "credit" TEXT + ); + INSERT INTO csv_import_table VALUES ('', '250.00'); + CREATE TABLE IF NOT EXISTS transactions ( + debit REAL, + credit REAL, + amount REAL GENERATED ALWAYS AS (ifnull(credit, 0.0) - ifnull(debit, 0.0)) + ) STRICT; + INSERT INTO transactions + SELECT + nullif(debit, '') AS debit, + nullif(credit, '') AS credit + FROM csv_import_table; + SELECT * FROM transactions; +} {{} 250.0 250.0} +do_execsql_test strict1-8.2 { + CREATE TABLE t1(x REAL, y REAL AS (x)) STRICT; + INSERT INTO t1 VALUES(5),(4611686018427387904); + SELECT *, '|' FROM t1; +} {/5.0 5.0 4.6116\d*e\+18 4.6116\d+e\+18 |/} + +# 2025-06-18 https://sqlite.org/forum/forumpost/6caf195248a849e4 +# +# Enforce STRICT table type constraints on STORED generated columns +# +do_execsql_test strict1-9.1 { + CREATE TABLE strict ( + k INTEGER PRIMARY KEY, + c1 REAL AS(if(k=11,1.5, k=12,2, k=13,'x', k=14,x'34', 0.0)) STORED, + c2 INT AS(if(k=21,1.5, k=22,2, k=23,'x', k=24,x'34', 0)) STORED, + c3 TEXT AS(if(k=31,1.5, k=32,2, k=33,'x', k=34,x'34', 'x')) STORED, + c4 BLOB AS(if(k=41,1.5, k=42,2, k=43,'x', k=44,x'34', x'00')) STORED, + c5 ANY AS(if(k=51,1.5, k=52,2, k=53,'x', k=54,x'34', 0)) STORED + ) STRICT; + INSERT INTO strict(k) VALUES(11); + INSERT INTO strict(k) VALUES(12); + INSERT INTO strict(k) VALUES(22); + INSERT INTO strict(k) VALUES(31); + INSERT INTO strict(k) VALUES(32); + INSERT INTO strict(k) VALUES(33); + INSERT INTO strict(k) VALUES(44); + PRAGMA integrity_check; +} {ok} +do_catchsql_test strict1-9.2.13 { + INSERT INTO strict(k) VALUES(13); +} {1 {cannot store TEXT value in REAL column strict.c1}} +do_catchsql_test strict1-9.2.14 { + INSERT INTO strict(k) VALUES(14); +} {1 {cannot store BLOB value in REAL column strict.c1}} +do_catchsql_test strict1-9.2.21 { + INSERT INTO strict(k) VALUES(21); +} {1 {cannot store REAL value in INT column strict.c2}} +do_catchsql_test strict1-9.2.23 { + INSERT INTO strict(k) VALUES(23); +} {1 {cannot store TEXT value in INT column strict.c2}} +do_catchsql_test strict1-9.2.24 { + INSERT INTO strict(k) VALUES(24); +} {1 {cannot store BLOB value in INT column strict.c2}} +do_catchsql_test strict1-9.2.34 { + INSERT INTO strict(k) VALUES(34); +} {1 {cannot store BLOB value in TEXT column strict.c3}} +do_catchsql_test strict1-9.2.41 { + INSERT INTO strict(k) VALUES(41); +} {1 {cannot store REAL value in BLOB column strict.c4}} +do_catchsql_test strict1-9.2.42 { + INSERT INTO strict(k) VALUES(42); +} {1 {cannot store INT value in BLOB column strict.c4}} +do_catchsql_test strict1-9.2.43 { + INSERT INTO strict(k) VALUES(43); +} {1 {cannot store TEXT value in BLOB column strict.c4}} + +do_execsql_test strict1-9.3 { + DROP TABLE strict; + CREATE TABLE strict ( + k INTEGER PRIMARY KEY, + c1 REAL AS(if(k=11,1.5, k=12,2, k=13,'x', k=14,x'34', 0.0)) VIRTUAL, + c2 INT AS(if(k=21,1.5, k=22,2, k=23,'x', k=24,x'34', 0)) VIRTUAL, + c3 TEXT AS(if(k=31,1.5, k=32,2, k=33,'x', k=34,x'34', 'x')) VIRTUAL, + c4 BLOB AS(if(k=41,1.5, k=42,2, k=43,'x', k=44,x'34', x'00')) VIRTUAL, + c5 ANY AS(if(k=51,1.5, k=52,2, k=53,'x', k=54,x'34', 0)) VIRTUAL + ) STRICT; + INSERT INTO strict(k) VALUES(11); + INSERT INTO strict(k) VALUES(12); + INSERT INTO strict(k) VALUES(22); + INSERT INTO strict(k) VALUES(31); + INSERT INTO strict(k) VALUES(32); + INSERT INTO strict(k) VALUES(33); + INSERT INTO strict(k) VALUES(44); + PRAGMA integrity_check; +} {ok} +do_catchsql_test strict1-9.4.13 { + INSERT INTO strict(k) VALUES(13); +} {1 {cannot store TEXT value in REAL column strict.c1}} +do_catchsql_test strict1-9.4.14 { + INSERT INTO strict(k) VALUES(14); +} {1 {cannot store BLOB value in REAL column strict.c1}} +do_catchsql_test strict1-9.4.21 { + INSERT INTO strict(k) VALUES(21); +} {1 {cannot store REAL value in INT column strict.c2}} +do_catchsql_test strict1-9.4.23 { + INSERT INTO strict(k) VALUES(23); +} {1 {cannot store TEXT value in INT column strict.c2}} +do_catchsql_test strict1-9.4.24 { + INSERT INTO strict(k) VALUES(24); +} {1 {cannot store BLOB value in INT column strict.c2}} +do_catchsql_test strict1-9.4.34 { + INSERT INTO strict(k) VALUES(34); +} {1 {cannot store BLOB value in TEXT column strict.c3}} +do_catchsql_test strict1-9.4.41 { + INSERT INTO strict(k) VALUES(41); +} {1 {cannot store REAL value in BLOB column strict.c4}} +do_catchsql_test strict1-9.4.42 { + INSERT INTO strict(k) VALUES(42); +} {1 {cannot store INT value in BLOB column strict.c4}} +do_catchsql_test strict1-9.4.43 { + INSERT INTO strict(k) VALUES(43); +} {1 {cannot store TEXT value in BLOB column strict.c4}} + +finish_test diff --git a/test/strict2.test b/test/strict2.test new file mode 100644 index 0000000000..be5952284d --- /dev/null +++ b/test/strict2.test @@ -0,0 +1,158 @@ +# 2021-08-19 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# This file implements regression tests for SQLite library. The +# focus of this file is testing STRICT tables. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix strict2 + +# PRAGMA integrity_check on a STRICT table should verify that +# all of the values are of the correct type. +# +do_execsql_test strict2-1.1 { + CREATE TABLE t1( + a INT, + b INTEGER, + c TEXT, + d REAL, + e BLOB + ) STRICT; + CREATE TABLE t1nn( + a INT NOT NULL, + b INTEGER NOT NULL, + c TEXT NOT NULL, + d REAL NOT NULL, + e BLOB NOT NULL + ) STRICT; + CREATE TABLE t2(a,b,c,d,e); + INSERT INTO t1(a,b,c,d,e) VALUES(1,1,'one',1.0,x'b1'),(2,2,'two',2.25,x'b2b2b2'); + PRAGMA writable_schema=on; + UPDATE sqlite_schema SET rootpage=(SELECT rootpage FROM sqlite_schema WHERE name='t1'); +} {} +db close +sqlite3 db test.db +do_execsql_test strict2-1.2 { + PRAGMA quick_check('t1'); +} {ok} +do_execsql_test strict2-1.3 { + UPDATE t2 SET a=2.5 WHERE b=2; + PRAGMA quick_check('t1'); +} {{non-INT value in t1.a}} +do_execsql_test strict2-1.4 { + UPDATE t2 SET a='xyz' WHERE b=2; + PRAGMA quick_check('t1'); +} {{non-INT value in t1.a}} +do_execsql_test strict2-1.5 { + UPDATE t2 SET a=x'445566' WHERE b=2; + PRAGMA quick_check('t1'); +} {{non-INT value in t1.a}} +do_execsql_test strict2-1.6 { + UPDATE t2 SET a=2.5 WHERE b=2; + PRAGMA quick_check('t1nn'); +} {{non-INT value in t1nn.a}} +do_execsql_test strict2-1.7 { + UPDATE t2 SET a='xyz' WHERE b=2; + PRAGMA quick_check('t1nn'); +} {{non-INT value in t1nn.a}} +do_execsql_test strict2-1.8 { + UPDATE t2 SET a=x'445566' WHERE b=2; + PRAGMA quick_check('t1nn'); +} {{non-INT value in t1nn.a}} + +do_execsql_test strict2-1.13 { + UPDATE t2 SET a=2 WHERE b=2; + UPDATE t2 SET b=2.5 WHERE a=2; + PRAGMA quick_check('t1'); +} {{non-INTEGER value in t1.b}} +do_execsql_test strict2-1.14 { + UPDATE t2 SET b='two' WHERE a=2; + PRAGMA quick_check('t1'); +} {{non-INTEGER value in t1.b}} +do_execsql_test strict2-1.15 { + UPDATE t2 SET b=x'b0b1b2b3b4' WHERE a=2; + PRAGMA quick_check('t1'); +} {{non-INTEGER value in t1.b}} +do_execsql_test strict2-1.16 { + UPDATE t2 SET b=NULL WHERE a=2; + PRAGMA quick_check('t1'); +} {ok} +do_execsql_test strict2-1.17 { + UPDATE t2 SET b=2.5 WHERE a=2; + PRAGMA quick_check('t1nn'); +} {{non-INTEGER value in t1nn.b}} +do_execsql_test strict2-1.18 { + UPDATE t2 SET b=NULL WHERE a=2; + PRAGMA quick_check('t1nn'); +} {{NULL value in t1nn.b}} + +do_execsql_test strict2-1.23 { + UPDATE t2 SET b=2 WHERE a=2; + UPDATE t2 SET c=9 WHERE a=2; + PRAGMA quick_check('t1'); +} {{non-TEXT value in t1.c}} +do_execsql_test strict2-1.24 { + UPDATE t2 SET c=9.5 WHERE a=2; + PRAGMA quick_check('t1'); +} {{non-TEXT value in t1.c}} +do_execsql_test strict2-1.25 { + UPDATE t2 SET c=x'b0b1b2b3b4' WHERE a=2; + PRAGMA quick_check('t1'); +} {{non-TEXT value in t1.c}} + +do_execsql_test strict2-1.33 { + UPDATE t2 SET c='two' WHERE a=2; + UPDATE t2 SET d=9 WHERE a=2; + PRAGMA quick_check('t1'); +} {ok} +do_execsql_test strict2-1.34 { + UPDATE t2 SET d='nine' WHERE a=2; + PRAGMA quick_check('t1'); +} {{non-REAL value in t1.d}} +do_execsql_test strict2-1.35 { + UPDATE t2 SET d=x'b0b1b2b3b4' WHERE a=2; + PRAGMA quick_check('t1'); +} {{non-REAL value in t1.d}} + +do_execsql_test strict2-1.43 { + UPDATE t2 SET d=2.5 WHERE a=2; + UPDATE t2 SET e=9 WHERE a=2; + PRAGMA quick_check('t1'); +} {{non-BLOB value in t1.e}} +do_execsql_test strict2-1.44 { + UPDATE t2 SET e=9.5 WHERE a=2; + PRAGMA quick_check('t1'); +} {{non-BLOB value in t1.e}} +do_execsql_test strict2-1.45 { + UPDATE t2 SET e='hello' WHERE a=2; + PRAGMA quick_check('t1'); +} {{non-BLOB value in t1.e}} + +do_execsql_test strict2-2.0 { + DROP TABLE IF EXISTS t2; + CREATE TABLE t2(a INT, b ANY) STRICT; + INSERT INTO t2(a,b) VALUES(1,2),(3,4.5),(5,'six'),(7,x'8888'),(9,NULL); + PRAGMA integrity_check(t2); +} {ok} + +do_execsql_test strict2-3.0 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(id ANY PRIMARY KEY, x TEXT); + INSERT INTO t1 VALUES(1,2),('three','four'),(x'5555','six'),(NULL,'eight'); + PRAGMA writable_schema=ON; + UPDATE sqlite_schema SET sql=(sql||'STRICT') WHERE name='t1'; + PRAGMA writable_schema=RESET; + PRAGMA integrity_check(t1); +} {{NULL value in t1.id}} + +finish_test diff --git a/test/subjournal.test b/test/subjournal.test new file mode 100644 index 0000000000..8d4c4499b0 --- /dev/null +++ b/test/subjournal.test @@ -0,0 +1,69 @@ +# 2017 May 9 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix subjournal + +do_execsql_test 1.0 { + PRAGMA temp_store = memory; + CREATE TABLE t1(a,b,c); + INSERT INTO t1 VALUES(1, 2, 3); +} {} +do_execsql_test 1.1 { + BEGIN; + INSERT INTO t1 VALUES(4, 5, 6); + SAVEPOINT one; + INSERT INTO t1 VALUES(7, 8, 9); + ROLLBACK TO one; + SELECT * FROM t1; +} {1 2 3 4 5 6} +do_execsql_test 1.2 { + COMMIT; +} + +do_execsql_test 2.0 { + PRAGMA cache_size = 5; + CREATE TABLE t2(a BLOB); + CREATE INDEX i2 ON t2(a); + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<100 + ) INSERT INTO t2 SELECT randomblob(500) FROM s; +} + +do_test 2.1 { + forcedelete test.db2 + sqlite3 db2 test2.db + sqlite3_backup B db2 main db main + set nPage [db one {PRAGMA page_count}] + B step [expr $nPage-10] +} {SQLITE_OK} + +do_execsql_test 2.2 { + BEGIN; + UPDATE t2 SET a=randomblob(499); + SAVEPOINT two; + UPDATE t2 SET a=randomblob(498); + ROLLBACK TO two; + COMMIT; + PRAGMA integrity_check; +} {ok} + +do_test 2.3 { + B step 1000 +} {SQLITE_DONE} +do_test 2.4 { + B finish + execsql { PRAGMA integrity_check } db2 +} {ok} + +finish_test diff --git a/test/subquery.test b/test/subquery.test index 06facbbae0..898c8f7560 100644 --- a/test/subquery.test +++ b/test/subquery.test @@ -11,8 +11,6 @@ # This file implements regression tests for SQLite library. The # focus of this script is testing correlated subqueries # -# $Id: subquery.test,v 1.17 2009/01/09 01:12:28 drh Exp $ -# set testdir [file dirname $argv0] source $testdir/tester.tcl @@ -477,7 +475,7 @@ do_test subquery-5.1 { INSERT INTO t5 VALUES(3,33); INSERT INTO t5 VALUES(4,44); SELECT b FROM t5 WHERE a IN - (SELECT callcnt(y)+0 FROM t4 WHERE x="two") + (SELECT callcnt(y)+0 FROM t4 WHERE x='two') } } {22} do_test subquery-5.2 { @@ -594,4 +592,161 @@ do_execsql_test subquery-8.1 { SELECT (SELECT 0 FROM (SELECT * FROM (SELECT 0))) AS x WHERE x; } {} +# 2022-01-12 https://sqlite.org/forum/forumpost/0ec80f12d02acb3f +# +reset_db +do_execsql_test subquery-9.1 { + CREATE TABLE t1(x); + INSERT INTO t1 VALUES(1),(1),(1); + SELECT (SELECT DISTINCT x FROM t1 ORDER BY +x LIMIT 1 OFFSET 100) FROM t1; +} {{} {} {}} +do_execsql_test subquery-9.2 { + SELECT (SELECT DISTINCT x FROM t1 ORDER BY +x LIMIT 1 OFFSET 0) FROM t1; +} {1 1 1} +do_execsql_test subquery-9.3 { + INSERT INTO t1 VALUES(2); + SELECT (SELECT DISTINCT x FROM t1 ORDER BY +x LIMIT 1 OFFSET 1) FROM t1; +} {2 2 2 2} +do_execsql_test subquery-9.4 { + SELECT (SELECT DISTINCT x FROM t1 ORDER BY +x LIMIT 1 OFFSET 2) FROM t1; +} {{} {} {} {}} + +# 2023-09-15 +# Query planner performance regression reported by private email +# on 2023-09-14, caused by VIEWSCAN optimization of check-in 609fbb94b8f01d67 +# from 2022-09-01. +# +reset_db +do_execsql_test subquery-10.1 { + CREATE TABLE t1(aa TEXT, bb INT, cc TEXT); + CREATE INDEX x11 on t1(bb); + CREATE INDEX x12 on t1(aa); + CREATE TABLE t2(aa TEXT, xx INT); + ANALYZE sqlite_master; + INSERT INTO sqlite_stat1(tbl, idx, stat) VALUES('t1', 'x11', '156789 28'); + INSERT INTO sqlite_stat1(tbl, idx, stat) VALUES('t1', 'x12', '156789 1'); + ANALYZE sqlite_master; +} +do_eqp_test subquery-10.2 { + WITH v1(aa,cc,bb) AS (SELECT aa, cc, bb FROM t1 WHERE bb=12345), + v2(aa,mx) AS (SELECT aa, max(xx) FROM t2 GROUP BY aa) + SELECT * FROM v1 JOIN v2 ON v1.aa=v2.aa; +} { + QUERY PLAN + |--CO-ROUTINE v2 + | |--SCAN t2 + | `--USE TEMP B-TREE FOR GROUP BY + |--SEARCH t1 USING INDEX x11 (bb=?) + `--SEARCH v2 USING AUTOMATIC COVERING INDEX (aa=?) +} +# ^^^^^^^^^^^^^ +# Prior to the fix the incorrect (slow) plan caused by the +# VIEWSCAN optimization was: +# +# QUERY PLAN +# |--CO-ROUTINE v2 +# | |--SCAN t2 +# | `--USE TEMP B-TREE FOR GROUP BY +# |--SCAN v2 +# `--SEARCH t1 USING INDEX x12 (aa=?) +# +#--------------------------------------------------------------------------- +# Follow-up on 2025-04-14. Performance issue found while working +# on Fossil (Fossil check-in 2025-04-13T19:54). +# +do_execsql_test 10.3 { + CREATE TABLE blob( + rid INTEGER PRIMARY KEY, + size INT, + uuid TEXT + ); + CREATE TABLE delta( + rid INTEGER PRIMARY KEY, + srcid INT + ); + CREATE INDEX delta_i1 ON delta(srcid); +} +do_eqp_test subquery-10.4 { + WITH RECURSIVE deltasof(rid) AS ( + SELECT rid FROM delta WHERE srcid=125020 + UNION + SELECT delta.rid FROM deltasof, delta + WHERE delta.srcid=deltasof.rid) + SELECT deltasof.rid, blob.uuid FROM deltasof, blob + WHERE blob.rid=deltasof.rid; +} { + QUERY PLAN + |--CO-ROUTINE deltasof + | |--SETUP + | | `--SEARCH delta USING COVERING INDEX delta_i1 (srcid=?) + | `--RECURSIVE STEP + | |--SCAN deltasof + | `--SEARCH delta USING COVERING INDEX delta_i1 (srcid=?) + |--SCAN deltasof + `--SEARCH blob USING INTEGER PRIMARY KEY (rowid=?) +} +# ^^^^^^^^^^^^^^^^^^ +# deltasof should be the outer loop and blob the inner loop +# Prior to the fix, SQLite was doing it the other way around. + +#----------------------------------------------------------------------------- +# 2024-04-25 Column affinities for columns of compound subqueries +# +reset_db +sqlite3_test_control SQLITE_TESTCTRL_INTERNAL_FUNCTIONS db +do_execsql_test subquery-11.1 { + CREATE TABLE t1(ix INT, rx REAL, bx BLOB, tx TEXT, ax); + INSERT INTO t1 VALUES(1,1.0,x'31','x',NULL); + WITH c(a) AS (SELECT 'y' UNION SELECT tx FROM t1) SELECT affinity(a) FROM c; + WITH c(a) AS (SELECT tx FROM t1 UNION SELECT 'y') SELECT affinity(a) FROM c; +} {text text text text} +do_execsql_test subquery-11.2 { + WITH c(a) AS (SELECT 2 UNION SELECT tx FROM t1) SELECT affinity(a) FROM c; + WITH c(a) AS (SELECT tx FROM t1 UNION SELECT 2) SELECT affinity(a) FROM c; +} {blob blob blob blob} +do_execsql_test subquery-11.3 { + WITH c(a) AS (SELECT 2.0 UNION SELECT tx FROM t1) SELECT affinity(a) FROM c; + WITH c(a) AS (SELECT tx FROM t1 UNION SELECT 2.0) SELECT affinity(a) FROM c; +} {blob blob blob blob} +do_execsql_test subquery-11.4 { + WITH c(a) AS (SELECT null UNION SELECT tx FROM t1) SELECT affinity(a) FROM c; + WITH c(a) AS (SELECT tx FROM t1 UNION SELECT null) SELECT affinity(a) FROM c; +} {text text text text} +do_execsql_test subquery-11.5 { + WITH c(a) AS (SELECT x'32' UNION SELECT tx FROM t1) SELECT affinity(a) FROM c; + WITH c(a) AS (SELECT tx FROM t1 UNION SELECT x'32') SELECT affinity(a) FROM c; +} {text text text text} +do_execsql_test subquery-11.6 { + WITH c(a) AS (SELECT 3 UNION SELECT ix FROM t1) SELECT affinity(a) FROM c; + WITH c(a) AS (SELECT ix FROM t1 UNION SELECT 3) SELECT affinity(a) FROM c; +} {integer integer integer integer} +do_execsql_test subquery-11.7 { + WITH c(a) AS (SELECT 3.0 UNION SELECT ix FROM t1) SELECT affinity(a) FROM c; + WITH c(a) AS (SELECT ix FROM t1 UNION SELECT 3.0) SELECT affinity(a) FROM c; +} {integer integer integer integer} +do_execsql_test subquery-11.8 { + WITH c(a) AS (SELECT '3' UNION SELECT ix FROM t1) SELECT affinity(a) FROM c; + WITH c(a) AS (SELECT ix FROM t1 UNION SELECT '3') SELECT affinity(a) FROM c; +} {blob blob blob blob} +do_execsql_test subquery-11.10 { + WITH c(a) AS (SELECT x'32' UNION SELECT ix FROM t1) SELECT affinity(a) FROM c; + WITH c(a) AS (SELECT ix FROM t1 UNION SELECT x'32') SELECT affinity(a) FROM c; +} {integer integer integer integer} +do_execsql_test subquery-11.11 { + WITH c(a) AS (SELECT 4 UNION SELECT rx FROM t1) SELECT affinity(a) FROM c; + WITH c(a) AS (SELECT rx FROM t1 UNION SELECT 4) SELECT affinity(a) FROM c; +} {real real real real} +do_execsql_test subquery-11.12 { + WITH c(a) AS (SELECT '4' UNION SELECT rx FROM t1) SELECT affinity(a) FROM c; + WITH c(a) AS (SELECT rx FROM t1 UNION SELECT '4') SELECT affinity(a) FROM c; +} {blob blob blob blob} +do_execsql_test subquery-11.13 { + WITH c(a) AS (SELECT null UNION SELECT rx FROM t1) SELECT affinity(a) FROM c; + WITH c(a) AS (SELECT rx FROM t1 UNION SELECT null) SELECT affinity(a) FROM c; +} {real real real real} +do_execsql_test subquery-11.14 { + WITH c(a) AS (SELECT x'b4' UNION SELECT rx FROM t1) SELECT affinity(a) FROM c; + WITH c(a) AS (SELECT rx FROM t1 UNION SELECT x'b4') SELECT affinity(a) FROM c; +} {real real real real} + finish_test diff --git a/test/subquery2.test b/test/subquery2.test index de637d5d25..99a1e903f8 100644 --- a/test/subquery2.test +++ b/test/subquery2.test @@ -104,7 +104,7 @@ do_execsql_test 2.2 { } {2 3 3 6 4 10} ############################################################################ -# Ticket http://www.sqlite.org/src/info/d11a6e908f (2014-09-20) +# Ticket http://sqlite.org/src/info/d11a6e908f (2014-09-20) # Query planner fault on three-way nested join with compound inner SELECT # do_execsql_test 3.0 { @@ -148,5 +148,139 @@ do_execsql_test 3.2 { ); } {a 4 b 3 c 2 d 1} +#------------------------------------------------------------------------- + +do_execsql_test 4.0 { + CREATE TABLE t6(x); +} + +foreach {tn sql} { + 1 { + SELECT 'abc' FROM ( + SELECT x FROM t6 ORDER BY 1 + UNION ALL + SELECT x FROM t6 + ) + } + 2 { + SELECT 'abc' FROM ( + SELECT x FROM t6 + UNION ALL + SELECT x FROM t6 ORDER BY 1 + UNION ALL + SELECT x FROM t6 + ) + } + 3 { + SELECT 'abc' FROM ( + SELECT x FROM t6 ORDER BY 1 + UNION ALL + SELECT x FROM t6 ORDER BY 1 + UNION ALL + SELECT x FROM t6 + ) + } + 4 { + SELECT 'abc' FROM ( + SELECT x FROM t6 + UNION ALL + SELECT x FROM t6 ORDER BY 1 + UNION ALL + SELECT x FROM t6 ORDER BY 1 + UNION ALL + SELECT x FROM t6 + ) + } +} { + do_catchsql_test 4.$tn $sql [list {*}{ + 1 {ORDER BY clause should come after UNION ALL not before} + }] +} + +#------------------------------------------------------------------------- +# Test that ticket [9cdc5c46] is fixed. +# +reset_db +do_execsql_test 5.0 { + CREATE TABLE t1(x); + INSERT INTO t1 VALUES('ALFKI'); + INSERT INTO t1 VALUES('ANATR'); + + CREATE TABLE t2(y, z); + CREATE INDEX t2y ON t2 (y); + INSERT INTO t2 VALUES('ANATR', '1997-08-08 00:00:00'); + INSERT INTO t2 VALUES('ALFKI', '1997-08-25 00:00:00'); +} +do_execsql_test 5.1 { + SELECT ( SELECT y FROM t2 WHERE x = y ORDER BY y, z) FROM t1; +} {ALFKI ANATR} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 6.0 { + CREATE TABLE t1(x); + INSERT INTO t1 VALUES(1234); +} + +do_execsql_test 6.1 { + SELECT DISTINCT 'string' FROM t1 LIMIT 1 OFFSET 5; +} + +do_execsql_test 6.2 { + SELECT ( + SELECT 'string' FROM t1 LIMIT 1 OFFSET 5 + ); +} {{}} + +do_execsql_test 6.3 { + SELECT ( + SELECT DISTINCT 'string' FROM t1 LIMIT 1 OFFSET 5 + ); +} {{}} + +do_execsql_test 6.4 { + SELECT ( + SELECT DISTINCT 'string' FROM t1 ORDER BY 1 LIMIT 1 OFFSET 5 + ); +} {{}} + +do_execsql_test 6.5 { + SELECT ( + SELECT 'string' FROM t1 ORDER BY 1 LIMIT 1 OFFSET 5 + ); +} {{}} + +do_execsql_test 6.6 { + SELECT (SELECT DISTINCT x, x FROM t1 LIMIT 1 OFFSET 5)==(1234, 1234) +} {{}} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 7.0 { + CREATE TABLE t1(x); + CREATE INDEX i1 ON t1(x); + INSERT INTO t1 VALUES(1234); +} + +do_execsql_test 7.1 { + SELECT ( + SELECT DISTINCT 'string' FROM t1 ORDER BY x LIMIT 1 OFFSET 5 + ); +} {{}} + +do_execsql_test 7.2 { + DROP INDEX i1; + CREATE UNIQUE INDEX i1 ON t1(x); +} + +do_execsql_test 7.3 { + SELECT ( + SELECT DISTINCT x FROM t1 ORDER BY 1 LIMIT 1 OFFSET 5 + ); +} {{}} + +do_execsql_test 7.4 { + SELECT (SELECT DISTINCT x FROM t1 ORDER BY +x LIMIT 1 OFFSET 0); +} {1234} finish_test diff --git a/test/subselect.test b/test/subselect.test index 247f68ee80..4d0efde47a 100644 --- a/test/subselect.test +++ b/test/subselect.test @@ -40,7 +40,7 @@ do_test subselect-1.1 { do_test subselect-1.2 { set v [catch {execsql {SELECT * FROM t1 WHERE a = (SELECT * FROM t1)}} msg] lappend v $msg -} {1 {only a single result allowed for a SELECT that is part of an expression}} +} {1 {row value misused}} # A subselect without an aggregate. # diff --git a/test/substr.test b/test/substr.test index bcd0588d2d..16ce080529 100644 --- a/test/substr.test +++ b/test/substr.test @@ -11,7 +11,6 @@ # This file implements regression tests for SQLite library. The # focus of this file is testing the built-in SUBSTR() functions. # -# $Id: substr.test,v 1.7 2009/02/03 13:10:54 drh Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl @@ -39,7 +38,7 @@ proc substr-test {id string i1 i2 result} { set qstr '[string map {' ''} $string]' do_test substr-$id.2 [subst { execsql { - SELECT substr($qstr, $i1, $i2) + SELECT substring($qstr, $i1, $i2) } }] [list $result] } @@ -55,7 +54,7 @@ proc subblob-test {id hex i1 i2 hexresult} { }] [list $hexresult] do_test substr-$id.2 [subst { execsql { - SELECT hex(substr(x'$hex', $i1, $i2)) + SELECT hex(substring(x'$hex', $i1, $i2)) } }] [list $hexresult] } @@ -93,7 +92,7 @@ do_test substr-1.92 { db eval {SELECT ifnull(substr('abcdefg',NULL,1),'nil')} } nil do_test substr-1.93 { - db eval {SELECT ifnull(substr('abcdefg',NULL),'nil')} + db eval {SELECT ifnull(substring('abcdefg',NULL),'nil')} } nil do_test substr-1.94 { db eval {SELECT ifnull(substr('abcdefg',1,NULL),'nil')} @@ -149,7 +148,7 @@ proc substr-2-test {id string idx result} { set qstr '[string map {' ''} $string]' do_test substr-$id.2 [subst { execsql { - SELECT substr($qstr, $idx) + SELECT substring($qstr, $idx) } }] [list $result] } diff --git a/test/subtype1.test b/test/subtype1.test index 1019611024..6d0df4fa6b 100644 --- a/test/subtype1.test +++ b/test/subtype1.test @@ -28,4 +28,60 @@ do_execsql_test subtype1-130 { SELECT test_setsubtype('hello',123); } {hello} +# 2022-06-09 +# https://sqlite.org/forum/forumpost/3d9caa45cbe38c78 +# +# Avoid carrying subtypes through into a subquery that has been flattened +# or to which the outer WHERE clause has been pushed down. +# +reset_db +do_execsql_test subtype1-200 { + CREATE TABLE t1(a); INSERT INTO t1 VALUES ('x'); + CREATE VIEW t2(b) AS SELECT json(TRUE); + CREATE TABLE t3(b); INSERT INTO t3 VALUES(json(TRUE)); +} +do_execsql_test subtype1-210 { + SELECT * FROM t3, t1 WHERE NOT json_quote(b); +} {1 x} +do_execsql_test subtype1-220 { + SELECT * FROM t2, t1 WHERE NOT json_quote(b); +} {1 x} +do_execsql_test subtype1-230 { + WITH t4(a) AS MATERIALIZED (SELECT json(1)) SELECT subtype(a) FROM t4; +} {0} +do_execsql_test subtype1-231 { + WITH t4(a) AS NOT MATERIALIZED (SELECT json(1)) SELECT subtype(a) FROM t4; +} {0} + +# 2023-03-01 +# https://sqlite.org/forum/forumpost/37dd14a538 +# +# Additional tests to show that subtypes do not traverse subquery boundaries. +# +do_execsql_test subtype1-300 { + CREATE TABLE t0(c0); + INSERT INTO t0 VALUES ('1'); + CREATE VIEW v0(c0) AS SELECT CASE WHEN 1 THEN json_patch('1', '1') END + FROM t0 GROUP BY t0.c0; + SELECT * FROM v0 WHERE json_quote(v0.c0) != '1'; +} {1} +do_execsql_test subtype1-310 { + SELECT *, json_quote(y) FROM (SELECT +json('1') AS y); +} {1 {"1"}} +do_execsql_test subtype1-320 { + SELECT *, json_quote(y) FROM (SELECT +json('1') AS y) + WHERE json_quote(y)='"1"'; +} {1 {"1"}} + +# 2024-11-26 +# subtype survives if() +# +do_execsql_test subtype1-400 { + WITH t400(id,j) AS (VALUES + (1,'{a:{x:1,y:2},b:{x:3,y:4}}'), + (2,'not json') + ) + SELECT id, subtype(if(json_valid(j,6),j->'a')) FROM t400; +} {1 74 2 0} + finish_test diff --git a/test/superlock.test b/test/superlock.test index 8199d5218d..10e7caa298 100644 --- a/test/superlock.test +++ b/test/superlock.test @@ -15,6 +15,7 @@ source $testdir/tester.tcl source $testdir/lock_common.tcl set testprefix superlock +do_not_use_codec # Test organization: # @@ -165,7 +166,7 @@ do_multiclient_test tn { proc read_content {file} { if {[file exists $file]==0} {return ""} set fd [open $file] - fconfigure $fd -encoding binary -translation binary + fconfigure $fd -translation binary set content [read $fd] close $fd return $content @@ -173,7 +174,7 @@ proc read_content {file} { proc write_content {file content} { set fd [open $file w+] - fconfigure $fd -encoding binary -translation binary + fconfigure $fd -translation binary puts -nonewline $fd $content close $fd } @@ -238,13 +239,23 @@ db_swap test.db2 test.db do_catchsql_test 6.9 { SELECT * FROM t1 } {0 {1 2 3 4}} do_catchsql_test 6.10 { SELECT * FROM t2 } {1 {no such table: t2}} -do_execsql_test 6.11 { - PRAGMA journal_mode = delete; - PRAGMA page_size = 512; - VACUUM; - PRAGMA journal_mode = wal; - INSERT INTO t1 VALUES(5, 6); -} {delete wal} +if {[nonzero_reserved_bytes]} { + # Vacuum with a size change is not allowed with the codec + do_execsql_test 6.11codec { + PRAGMA journal_mode = delete; + VACUUM; + PRAGMA journal_mode = wal; + INSERT INTO t1 VALUES(5, 6); + } {delete wal} +} else { + do_execsql_test 6.11 { + PRAGMA journal_mode = delete; + PRAGMA page_size = 512; + VACUUM; + PRAGMA journal_mode = wal; + INSERT INTO t1 VALUES(5, 6); + } {delete wal} +} db_swap test.db2 test.db do_catchsql_test 6.12 { SELECT * FROM t1 } {1 {no such table: t1}} diff --git a/test/swarmvtab.test b/test/swarmvtab.test new file mode 100644 index 0000000000..b4b94487ac --- /dev/null +++ b/test/swarmvtab.test @@ -0,0 +1,246 @@ +# 2017-07-15 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is the "swarmvtab" extension +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix swarmvtab +do_not_use_codec + +ifcapable !vtab { + finish_test + return +} + +load_static_extension db unionvtab + +set nFile $sqlite_open_file_count + +do_execsql_test 1.0 { + CREATE TABLE t0(a INTEGER PRIMARY KEY, b TEXT); + WITH s(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<400) + INSERT INTO t0 SELECT i, hex(randomblob(50)) FROM s; + + CREATE TABLE dir(f, t, imin, imax); +} + +do_test 1.1 { + for {set i 0} {$i < 40} {incr i} { + set iMin [expr $i*10 + 1] + set iMax [expr $iMin+9] + + forcedelete "test.db$i" + execsql [subst { + ATTACH 'test.db$i' AS aux; + CREATE TABLE aux.t$i (a INTEGER PRIMARY KEY, b TEXT); + INSERT INTO aux.t$i SELECT * FROM t0 WHERE a BETWEEN $iMin AND $iMax; + DETACH aux; + INSERT INTO dir VALUES('test.db$i', 't$i', $iMin, $iMax); + }] + } + + execsql { + CREATE VIRTUAL TABLE temp.s1 USING swarmvtab('SELECT * FROM dir'); + } +} {} + +do_execsql_test 1.2 { + DROP TABLE s1; +} {} + +do_execsql_test 1.3 { + CREATE VIRTUAL TABLE temp.s1 USING swarmvtab('SELECT * FROM dir'); + SELECT count(*) FROM s1 WHERE rowid<50; +} {49} + +proc do_compare_test {tn where} { + set sql [subst { + SELECT (SELECT group_concat(a || ',' || b, ',') FROM t0 WHERE $where) + IS + (SELECT group_concat(a || ',' || b, ',') FROM s1 WHERE $where) + }] + + uplevel [list do_execsql_test $tn $sql 1] +} + +do_compare_test 1.4.1 "rowid = 700" +do_compare_test 1.4.2 "rowid = -1" +do_compare_test 1.4.3 "rowid = 0" +do_compare_test 1.4.4 "rowid = 55" +do_compare_test 1.4.5 "rowid BETWEEN 20 AND 100" +do_compare_test 1.4.6 "rowid > 350" +do_compare_test 1.4.7 "rowid >= 350" +do_compare_test 1.4.8 "rowid >= 200" +do_compare_test 1.4.9 "1" + +# Multiple simultaneous cursors. +# +do_execsql_test 1.5.1.(5-seconds-or-so) { + SELECT count(*) FROM s1 a, s1 b WHERE b.rowid<=200; +} {80000} +do_execsql_test 1.5.2 { + SELECT count(*) FROM s1 a, s1 b, s1 c + WHERE a.rowid=b.rowid AND b.rowid=c.rowid; +} {400} + +# Empty source tables. +# +do_test 1.6.0 { + for {set i 0} {$i < 20} {incr i} { + sqlite3 db2 test.db$i + db2 eval " DELETE FROM t$i " + db2 close + } + db eval { DELETE FROM t0 WHERE rowid<=200 } +} {} + +do_compare_test 1.6.1 "rowid = 700" +do_compare_test 1.6.2 "rowid = -1" +do_compare_test 1.6.3 "rowid = 0" +do_compare_test 1.6.4 "rowid = 55" +do_compare_test 1.6.5 "rowid BETWEEN 20 AND 100" +do_compare_test 1.6.6 "rowid > 350" +do_compare_test 1.6.7 "rowid >= 350" +do_compare_test 1.6.8 "rowid >= 200" +do_compare_test 1.6.9 "1" +do_compare_test 1.6.10 "rowid >= 5" + +do_test 1.x { + set sqlite_open_file_count +} [expr $nFile+9] + +do_test 1.y { db close } {} + +# Delete all the database files created above. +# +for {set i 0} {$i < 40} {incr i} { forcedelete "test.db$i" } + +#------------------------------------------------------------------------- +# Test some error conditions: +# +# 2.1: Database file does not exist. +# 2.2: Table does not exist. +# 2.3: Table schema does not match. +# 2.4: Syntax error in SELECT statement. +# +reset_db +load_static_extension db unionvtab +do_test 2.0.1 { + db eval { + CREATE TABLE t0(a INTEGER PRIMARY KEY, b TEXT); + WITH s(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<400) + INSERT INTO t0 SELECT i, hex(randomblob(50)) FROM s; + CREATE TABLE dir(f, t, imin, imax); + } + + for {set i 0} {$i < 40} {incr i} { + set iMin [expr $i*10 + 1] + set iMax [expr $iMin+9] + + forcedelete "test.db$i" + db eval [subst { + ATTACH 'test.db$i' AS aux; + CREATE TABLE aux.t$i (a INTEGER PRIMARY KEY, b TEXT); + INSERT INTO aux.t$i SELECT * FROM t0 WHERE a BETWEEN $iMin AND $iMax; + DETACH aux; + INSERT INTO dir VALUES('test.db$i', 't$i', $iMin, $iMax); + }] + } + execsql { + CREATE VIRTUAL TABLE temp.s1 USING swarmvtab('SELECT * FROM dir'); + } +} {} + +do_test 2.0.2 { + forcedelete test.db5 + + sqlite3 db2 test.db15 + db2 eval { DROP TABLE t15 } + db2 close + + sqlite3 db2 test.db25 + db2 eval { + DROP TABLE t25; + CREATE TABLE t25(x, y, z PRIMARY KEY); + } + db2 close +} {} + +do_catchsql_test 2.1 { + SELECT * FROM s1 WHERE rowid BETWEEN 1 AND 100; +} {1 {unable to open database file}} +do_catchsql_test 2.2 { + SELECT * FROM s1 WHERE rowid BETWEEN 101 AND 200; +} {1 {no such rowid table: t15}} +do_catchsql_test 2.3 { + SELECT * FROM s1 WHERE rowid BETWEEN 201 AND 300; +} {1 {source table schema mismatch}} + +do_catchsql_test 2.4 { + CREATE VIRTUAL TABLE temp.x1 USING swarmvtab('SELECT * FROMdir'); +} {1 {sql error: near "FROMdir": syntax error}} +do_catchsql_test 2.5 { + CREATE VIRTUAL TABLE temp.x1 USING swarmvtab('SELECT * FROMdir', 'fetchdb'); +} {1 {sql error: near "FROMdir": syntax error}} + +for {set i 0} {$i < 40} {incr i} { + forcedelete "test.db$i" +} + +#------------------------------------------------------------------------- +# Test the outcome of the fetch function throwing an exception. +# +proc fetch_db {file} { + error "fetch_db error!" +} + +db func fetch_db fetch_db + +do_catchsql_test 3.1 { + CREATE VIRTUAL TABLE temp.xyz USING swarmvtab( + 'VALUES + (''test.db1'', ''t1'', 1, 10), + (''test.db2'', ''t1'', 11, 20) + ', 'fetch_db_no_such_function' + ); +} {1 {sql error: no such function: fetch_db_no_such_function}} + +do_catchsql_test 3.2 { + CREATE VIRTUAL TABLE temp.xyz USING swarmvtab( + 'VALUES + (''test.db1'', ''t1'', 1, 10), + (''test.db2'', ''t1'', 11, 20) + ', 'fetch_db' + ); +} {1 {fetch_db error!}} + +do_execsql_test 3.3.1 { + ATTACH 'test.db1' AS aux; + CREATE TABLE aux.t1(a INTEGER PRIMARY KEY, b); + INSERT INTO aux.t1 VALUES(1, NULL); + INSERT INTO aux.t1 VALUES(2, NULL); + INSERT INTO aux.t1 VALUES(9, NULL); + DETACH aux; + CREATE VIRTUAL TABLE temp.xyz USING swarmvtab( + 'VALUES + (''test.db1'', ''t1'', 1, 10), + (''test.db2'', ''t1'', 11, 20) + ', 'fetch_db' + ); +} {} + +do_catchsql_test 3.3.2 { SELECT * FROM xyz } {1 {fetch_db error!}} + + + +finish_test diff --git a/test/swarmvtab2.test b/test/swarmvtab2.test new file mode 100644 index 0000000000..1cc7fbb378 --- /dev/null +++ b/test/swarmvtab2.test @@ -0,0 +1,75 @@ +# 2017-07-15 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is the "swarmvtab" extension +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix swarmvtab2 +do_not_use_codec + +ifcapable !vtab { + finish_test + return +} + + +db close +foreach name [glob -nocomplain test*.db] { + forcedelete $name +} +sqlite3 db test.db +load_static_extension db unionvtab +proc create_database {filename} { + sqlite3 dbx $filename + set num [regsub -all {[^0-9]+} $filename {}] + set num [string trimleft $num 0] + set start [expr {$num*1000}] + set end [expr {$start+999}] + dbx eval { + CREATE TABLE t2(a INTEGER PRIMARY KEY,b); + WITH RECURSIVE c(x) AS ( + VALUES($start) UNION ALL SELECT x+1 FROM c WHERE x<$end + ) + INSERT INTO t2(a,b) SELECT x, printf('**%05d**',x) FROM c; + } + dbx close +} +db func create_database create_database +do_execsql_test 100 { + CREATE TABLE t1(filename, tablename, istart, iend); + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<99) + INSERT INTO t1 SELECT printf('test%03d.db',x),'t2',x*1000,x*1000+999 FROM c; + CREATE VIRTUAL TABLE temp.v1 USING swarmvtab( + 'SELECT * FROM t1', 'create_database' + ); +} {} +do_execsql_test 110 { + SELECT b FROM v1 WHERE a=3875; +} {**03875**} +do_test 120 { + lsort [glob -nocomplain test?*.db] +} {test001.db test003.db} +do_execsql_test 130 { + SELECT b FROM v1 WHERE a BETWEEN 3999 AND 4000 ORDER BY a; +} {**03999** **04000**} +do_test 140 { + lsort [glob -nocomplain test?*.db] +} {test001.db test003.db test004.db} +do_execsql_test 150 { + SELECT b FROM v1 WHERE a>=99998; +} {**99998** **99999**} +do_test 160 { + lsort -dictionary [glob -nocomplain test?*.db] +} {test001.db test003.db test004.db test099.db} + +finish_test diff --git a/test/swarmvtab3.test b/test/swarmvtab3.test new file mode 100644 index 0000000000..8ca2471308 --- /dev/null +++ b/test/swarmvtab3.test @@ -0,0 +1,235 @@ +# 2017-07-15 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is the "swarmvtab" extension +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix swarmvtab3 +do_not_use_codec + +ifcapable !vtab { + finish_test + return +} + +load_static_extension db unionvtab + +set nFile $sqlite_open_file_count + +do_execsql_test 1.0 { + CREATE TEMP TABLE swarm(id, tbl, minval, maxval); +} + +# Set up 100 databases with filenames "remote_test.dbN", where N is between +# 0 and 99. +do_test 1.1 { + for {set i 0} {$i < 100} {incr i} { + set file remote_test.db$i + forcedelete $file + forcedelete test.db$i + sqlite3 rrr $file + rrr eval { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b); + INSERT INTO t1 VALUES($i, $i); + } + rrr close + db eval { + INSERT INTO swarm VALUES($i, 't1', $i, $i); + } + set ::dbcache(test.db$i) 0 + } +} {} + +proc missing_db {filename} { + set remote "remote_$filename" + forcedelete $filename + file copy $remote $filename +} +db func missing_db missing_db + +proc openclose_db {filename bClose} { + if {$bClose} { + incr ::dbcache($filename) -1 + } else { + incr ::dbcache($filename) 1 + } + if {$::dbcache($filename)==0} { + forcedelete $filename + } +} +db func openclose_db openclose_db + +proc check_dbcache {} { + set n 0 + for {set i 0} {$i<100} {incr i} { + set exists [file exists test.db$i] + if {$exists!=($::dbcache(test.db$i)!=0)} { + error "inconsistent ::dbcache and disk ($i) - $exists" + } + incr n $exists + } + return $n +} + +foreach {tn nMaxOpen cvt} { + 1 5 { + CREATE VIRTUAL TABLE temp.s USING swarmvtab( + 'SELECT :prefix || id, tbl, minval, minval FROM swarm', + :prefix='test.db', + missing=missing_db, + openclose=openclose_db, + maxopen=5 + ) + } + + 2 3 { + CREATE VIRTUAL TABLE temp.s USING swarmvtab( + 'SELECT :prefix || id, tbl, minval, minval FROM swarm', + :prefix='test.db', + missing = 'missing_db', + openclose=[openclose_db], + maxopen = 3 + ) + } + + 3 1 { + CREATE VIRTUAL TABLE temp.s USING swarmvtab( + 'SELECT :prefix||''.''||:suffix||id, tbl, minval, minval FROM swarm', + :prefix=test, :suffix=db, + missing = 'missing_db', + openclose=[openclose_db], + maxopen = 1 + ) + } + +} { + execsql { DROP TABLE IF EXISTS s } + + do_execsql_test 1.$tn.1 $cvt + + do_execsql_test 1.$tn.2 { + SELECT b FROM s WHERE a<10; + } {0 1 2 3 4 5 6 7 8 9} + + do_test 1.$tn.3 { check_dbcache } $nMaxOpen + + do_execsql_test 1.$tn.4 { + SELECT b FROM s WHERE (b%10)=0; + } {0 10 20 30 40 50 60 70 80 90} + + do_test 1.$tn.5 { check_dbcache } $nMaxOpen +} + +execsql { DROP TABLE IF EXISTS s } +for {set i 0} {$i < 100} {incr i} { + forcedelete remote_test.db$i +} + +#---------------------------------------------------------------------------- +# +do_execsql_test 2.0 { + DROP TABLE IF EXISTS swarm; + CREATE TEMP TABLE swarm(file, tbl, minval, maxval, ctx); +} + +catch { array unset ::dbcache } + +# Set up 100 databases with filenames "remote_test.dbN", where N is a +# random integer between 0 and 1,000,000 +# 0 and 99. +do_test 2.1 { + catch { array unset ctx_used } + for {set i 0} {$i < 100} {incr i} { + while 1 { + set ctx [expr abs(int(rand() *1000000))] + if {[info exists ctx_used($ctx)]==0} break + } + set ctx_used($ctx) 1 + + set file test_remote.db$ctx + forcedelete $file + forcedelete test.db$i + sqlite3 rrr $file + rrr eval { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b); + INSERT INTO t1 VALUES($i, $i); + } + rrr close + db eval { + INSERT INTO swarm VALUES('test.db' || $i, 't1', $i, $i, $file) + } + set ::dbcache(test.db$i) 0 + } +} {} + +proc missing_db {filename ctx} { + file copy $ctx $filename +} +db func missing_db missing_db + +proc openclose_db {filename ctx bClose} { + if {$bClose} { + incr ::dbcache($filename) -1 + } else { + incr ::dbcache($filename) 1 + } + if {$::dbcache($filename)==0} { + forcedelete $filename + } +} +db func openclose_db openclose_db + +proc check_dbcache {} { + set n 0 + foreach k [array names ::dbcache] { + set exists [file exists $k] + if {$exists!=($::dbcache($k)!=0)} { + error "inconsistent ::dbcache and disk ($k) - $exists" + } + incr n $exists + } + return $n +} + +foreach {tn nMaxOpen cvt} { + 2 5 { + CREATE VIRTUAL TABLE temp.s USING swarmvtab( + 'SELECT file, tbl, minval, minval, ctx FROM swarm', + missing=missing_db, + openclose=openclose_db, + maxopen=5 + ) + } +} { + execsql { DROP TABLE IF EXISTS s } + + do_execsql_test 3.$tn.1 $cvt + + do_execsql_test 3.$tn.2 { + SELECT b FROM s WHERE a<10; + } {0 1 2 3 4 5 6 7 8 9} + + do_test 3.$tn.3 { check_dbcache } $nMaxOpen + + do_execsql_test 3.$tn.4 { + SELECT b FROM s WHERE (b%10)=0; + } {0 10 20 30 40 50 60 70 80 90} + + do_test 3.$tn.5 { check_dbcache } $nMaxOpen +} + +db close +forcedelete {*}[glob test.db*] +forcedelete {*}[glob test_remote.db*] + +finish_test diff --git a/test/swarmvtabfault.test b/test/swarmvtabfault.test new file mode 100644 index 0000000000..8cca3b2744 --- /dev/null +++ b/test/swarmvtabfault.test @@ -0,0 +1,65 @@ +# 2017-07-15 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is error handling in the swarmvtab extension. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix swarmvtabfault + +ifcapable !vtab { + finish_test + return +} + +proc fetch_db {file} { + forcedelete $file + sqlite3 dbX $file + set rc [catch { + dbX eval { CREATE TABLE t1(a INTEGER PRIMARY KEY, b) } + } res] + dbX close + if {$rc!=0} {error $res} +} + +forcedelete test.db1 +forcedelete test.db2 + +do_execsql_test 1.0 { + ATTACH 'test.db1' AS aux; + CREATE TABLE aux.t1(a INTEGER PRIMARY KEY, b); + INSERT INTO aux.t1 VALUES(1, NULL); + INSERT INTO aux.t1 VALUES(2, NULL); + INSERT INTO aux.t1 VALUES(9, NULL); + DETACH aux; +} {} + +faultsim_save_and_close +do_faultsim_test 1.1 -faults oom* -prep { + faultsim_restore_and_reopen + db func fetch_db fetch_db + load_static_extension db unionvtab + db eval { + CREATE VIRTUAL TABLE temp.xyz USING swarmvtab( + 'VALUES + ("test.db1", "t1", 1, 10), + ("test.db2", "t1", 11, 20) + ', 'fetch_db' + ); + } +} -body { + execsql { SELECT a FROM xyz } +} -test { + faultsim_test_result {0 {1 2 9}} {1 {sql error: out of memory}} +} + +finish_test diff --git a/test/symlink.test b/test/symlink.test index 949102cf8a..fc78a04720 100644 --- a/test/symlink.test +++ b/test/symlink.test @@ -17,7 +17,7 @@ source $testdir/tester.tcl set testprefix symlink # This only runs on unix. -if {$::tcl_platform(platform)!="unix"} { +if {$::tcl_platform(os) eq "Windows NT"} { finish_test return } @@ -37,11 +37,30 @@ do_test 1.1 { sqlite3_db_filename db2 main } [file join [pwd] test.db] +# But not with the -nofollow flag +# +do_test 1.1.2 { + db2 close + set rc [catch {sqlite3 db2 test.db2 -nofollow 1} msg] + lappend rc $msg +} {1 {unable to open database file}} + +# If the main database is successfully opened with -nofollow, then -nofollow +# is also used for ATTACH. +# +do_test 1.1.3 { + catch {db2 close} + sqlite3 db2 test.db -nofollow 1 +} {} +do_test 1.1.4 { + catchsql {ATTACH 'test.db2' AS aux1;} db2 +} {1 {unable to open database: test.db2}} + # Test that if the symlink points to a file that does not exists, it is # created when it is opened. # do_test 1.2.1 { - db2 close + catch {db2 close} db close forcedelete test.db file exists test.db @@ -102,7 +121,7 @@ foreach {tn f} {1 test.db2 2 test.db3} { INSERT INTO t1 VALUES(1); } db2 file exists test.db-journal - } 1 + } [expr [atomic_batch_write test.db]==0] do_test 2.$tn.3 { list [file exists test2.db-journal] [file exists test3.db-journal] } {0 0} @@ -188,4 +207,32 @@ do_test 4.4.2 { list [file exists x/test.db-wal] [file exists w/test.db-wal] } {1 0} +#------------------------------------------------------------------------- +# Check that extra ".." in a path are ignored. +reset_db +do_execsql_test 5.0 { + CREATE TABLE xyz(x, y, z); + INSERT INTO xyz VALUES(1, 2, 3); +} + +set path [pwd] +set nLink [llength [split $path /]] +set path "[string repeat ../ [expr $nLink*2]]..${path}/test.db" + +sqlite3 db2 $path +do_execsql_test -db db2 5.1 { + SELECT * FROM xyz; +} {1 2 3} +db close + +forcedelete test.db2 +file link test.db2 $path +sqlite3 db2 test.db2 +do_execsql_test -db db2 5.2 { + SELECT * FROM xyz; +} {1 2 3} +forcedelete test.db2 + + + finish_test diff --git a/test/symlink2.test b/test/symlink2.test new file mode 100644 index 0000000000..7305f6dd38 --- /dev/null +++ b/test/symlink2.test @@ -0,0 +1,118 @@ +# 2019 November 18 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is testing that SQLite can follow symbolic links. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix symlink2 + +# This only runs on Windows. +if {$::tcl_platform(platform) ne "windows"} { + finish_test + return +} + +proc createWin32Symlink { link target } { + exec -- $::env(ComSpec) /c mklink \ + [file nativename $link] [file nativename $target] + return "" +} + +proc deleteWin32Symlink { link } { + exec -- $::env(ComSpec) /c del [file nativename $link] + return "" +} + +proc canCreateWin32Symlink {} { + set link [file join $::testdir lnk[pid].sym] + if {[file exists $link]} { return 0 } + set target [info nameofexecutable] + if {[catch {createWin32Symlink $link $target}] == 0} { + deleteWin32Symlink $link + return 1 + } + return 0 +} + +# Creating symlinks may require administrator privileges on Windows. +if {![canCreateWin32Symlink]} { + finish_test + return +} + +# Ensure that test.db has been created. +# +do_execsql_test 1.0 { + CREATE TABLE t1(x, y); + INSERT INTO t1 VALUES(1,9999); +} + +forcedelete link.db +do_test 2.0 { + createWin32Symlink link.db test.db +} {} + +do_test 2.1 { + file exists test.db +} {1} + +do_test 2.2 { + file exists link.db +} {1} + +do_test 3.1 { + execsql { SELECT x, y FROM t1; } db +} {1 9999} + +do_test 3.2 { + sqlite3 db2 link.db + execsql { SELECT x, y FROM t1; } db2 +} {1 9999} + +do_test 3.3 { + sqlite3 db3 test.db -nofollow true + execsql { SELECT x, y FROM t1; } db3 +} {1 9999} + +do_test 3.4 { + db3 close +} {} + +# The -nofollow option does not work on Windows +do_test 3.5 { + list [catch { + sqlite3 db4 link.db -nofollow true + execsql { SELECT x, y FROM t1; } db4 + } res] $res +} {0 {1 9999}} + +catch {db4 close} + +do_test 4.0 { + db2 close + deleteWin32Symlink link.db +} {} + +do_test 4.1 { + file exists test.db +} {1} + +do_test 4.2 { + file exists link.db +} {0} + +do_test 5.1 { + execsql { SELECT x, y FROM t1; } db +} {1 9999} + +finish_test diff --git a/test/sync.test b/test/sync.test index 2ee636a266..b24800d100 100644 --- a/test/sync.test +++ b/test/sync.test @@ -26,11 +26,15 @@ ifcapable !pager_pragmas||!attach { finish_test return } +if {[atomic_batch_write test.db]} { + finish_test + return +} set sqlite_sync_count 0 proc cond_incr_sync_count {adj} { global sqlite_sync_count - if {$::tcl_platform(platform) == "windows"} { + if {$::tcl_platform(os) eq "Windows NT"} { incr sqlite_sync_count $adj } else { ifcapable !dirsync { @@ -81,6 +85,7 @@ do_test sync-1.3 { set sqlite_sync_count } 11 ifcapable pager_pragmas { +if {[permutation]!="journaltest"} { do_test sync-1.4 { set sqlite_sync_count 0 execsql { @@ -94,6 +99,7 @@ ifcapable pager_pragmas { set sqlite_sync_count } 0 } +} finish_test diff --git a/test/sync2.test b/test/sync2.test new file mode 100644 index 0000000000..ce8132c714 --- /dev/null +++ b/test/sync2.test @@ -0,0 +1,161 @@ +# 2017 March 16 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. +# +# Specificly, it tests that "PRAGMA synchronous" appears to work. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix sync2 + +# +# These tests are only applicable when pager pragma are +# enabled. Also, since every test uses an ATTACHed database, they +# are only run when ATTACH is enabled. +# +ifcapable !pager_pragmas||!attach||!dirsync { + finish_test + return +} +if {$::tcl_platform(os) eq "Windows NT" + || [permutation] == "journaltest" + || [permutation] == "inmemory_journal" + || [atomic_batch_write test.db] +} { + finish_test + return +} + +proc execsql_sync {sql} { + set s $::sqlite_sync_count + set res [execsql $sql] + concat [expr $::sqlite_sync_count-$s] $res +} + +proc do_execsql_sync_test {tn sql res} { + uplevel [list do_test $tn [list execsql_sync $sql] [list {*}$res]] +} + +#----------------------------------------------------------------------- +# Tests for journal mode. +# +sqlite3 db test.db +do_execsql_test 1.0 { + CREATE TABLE t1(a, b); + INSERT INTO t1 VALUES(1, 2); +} + +do_execsql_sync_test 1.1 { INSERT INTO t1 VALUES(3, 4) } 4 + +# synchronous=normal. So, 1 sync on the directory, 1 on the journal, 1 +# on the db file. 3 in total. +do_execsql_test 1.2.1 { PRAGMA main.synchronous = NORMAL } +do_execsql_test 1.2.2 { PRAGMA main.synchronous } 1 +do_execsql_sync_test 1.2.3 { INSERT INTO t1 VALUES(5, 6) } 3 + +# synchronous=off. No syncs. +do_execsql_test 1.3.1 { PRAGMA main.synchronous = OFF } +do_execsql_test 1.3.2 { PRAGMA main.synchronous } 0 +do_execsql_sync_test 1.3.3 { INSERT INTO t1 VALUES(7, 8) } 0 + +# synchronous=full, journal_mode=delete. So, 1 sync on the directory, +# 2 on the journal, 1 on the db file. 4 in total. +do_execsql_test 1.4.1 { PRAGMA main.synchronous = FULL } +do_execsql_test 1.4.2 { PRAGMA main.synchronous } 2 +do_execsql_sync_test 1.4.3 { INSERT INTO t1 VALUES(9, 10) } 4 + +#----------------------------------------------------------------------- +# Tests for wal mode. +# +do_execsql_test 1.5 { PRAGMA journal_mode = wal } {wal} + +# sync=full, journal_mode=wal. One sync on the directory, two on the +# wal file. +do_execsql_sync_test 1.6 { INSERT INTO t1 VALUES(11, 12) } 3 + +# One sync on the wal file. +do_execsql_sync_test 1.7 { INSERT INTO t1 VALUES(13, 14) } 1 + +# No syncs. +do_execsql_test 1.8.1 { PRAGMA main.synchronous = NORMAL } +do_execsql_test 1.8.2 { PRAGMA main.synchronous } 1 +do_execsql_sync_test 1.8.3 { INSERT INTO t1 VALUES(15, 16) } 0 + +# One sync on wal file, one on the db file. +do_execsql_sync_test 1.9 { PRAGMA wal_checkpoint } {2 0 3 3} + +# No syncs. +do_execsql_test 1.10.1 { PRAGMA main.synchronous = OFF } +do_execsql_test 1.10.2 { PRAGMA main.synchronous } 0 +do_execsql_sync_test 1.10.3 { INSERT INTO t1 VALUES(17, 18) } 0 + +#----------------------------------------------------------------------- +# Tests for the compile time settings SQLITE_DEFAULT_SYNCHRONOUS and +# SQLITE_DEFAULT_WAL_SYNCHRONOUS. These tests only run if the former +# is set to "2" and the latter to "1". This is not the default, but +# it is currently the recommended configuration. +# +# https://sqlite.org/compile.html#recommended_compile_time_options +# +if {$SQLITE_DEFAULT_SYNCHRONOUS==2 && $SQLITE_DEFAULT_WAL_SYNCHRONOUS==1} { + + db close + sqlite3 db test.db + + # Wal mode, sync=normal. The first transaction does one sync on directory, + # one on the wal file. The second does no syncs. + do_execsql_sync_test 1.11.1 { INSERT INTO t1 VALUES(19, 20) } 2 + do_execsql_sync_test 1.11.2 { INSERT INTO t1 VALUES(21, 22) } 0 + do_execsql_test 1.11.3 { PRAGMA main.synchronous } 1 + + # One sync on wal file, one on the db file. + do_execsql_sync_test 1.12 { PRAGMA wal_checkpoint } {2 0 2 2} + + # First transaction syncs the wal file once, the second not at all. + # one on the wal file. The second does no syncs. + do_execsql_sync_test 1.13.1 { INSERT INTO t1 VALUES(22, 23) } 1 + do_execsql_sync_test 1.13.2 { INSERT INTO t1 VALUES(24, 25) } 0 + + do_execsql_test 1.14 { PRAGMA journal_mode = delete } {delete} + + # Delete mode, sync=full. The first transaction does one sync on + # directory, two on the journal file, one on the db. The second does + # the same. + do_execsql_sync_test 1.15.1 { INSERT INTO t1 VALUES(26, 27) } 4 + do_execsql_sync_test 1.15.2 { INSERT INTO t1 VALUES(28, 29) } 4 + do_execsql_test 1.15.3 { PRAGMA main.synchronous } 2 + + # Switch back to wal mode. + do_execsql_test 1.16 { PRAGMA journal_mode = wal } {wal} + + do_execsql_sync_test 1.17.1 { INSERT INTO t1 VALUES(30, 31) } 2 + do_execsql_sync_test 1.17.2 { INSERT INTO t1 VALUES(32, 33) } 0 + do_execsql_test 1.17.3 { PRAGMA main.synchronous } 1 + + # Now set synchronous=off, then switch back to delete mode. Check + # that the db handle is still using synchronous=off. + do_execsql_test 1.18.3 { PRAGMA main.synchronous=off } + do_execsql_test 1.18 { PRAGMA journal_mode = delete } {delete} + + do_execsql_sync_test 1.19.1 { INSERT INTO t1 VALUES(34, 35) } 0 + do_execsql_sync_test 1.19.2 { INSERT INTO t1 VALUES(36, 37) } 0 + do_execsql_test 1.19.3 { PRAGMA main.synchronous } 0 + + # Close and reopen the db. Back to synchronous=normal. + db close + sqlite3 db test.db + do_execsql_sync_test 1.20.1 { INSERT INTO t1 VALUES(38, 39) } 4 + do_execsql_sync_test 1.20.2 { INSERT INTO t1 VALUES(40, 41) } 4 + do_execsql_test 1.20.3 { PRAGMA main.synchronous } 2 +} + +finish_test diff --git a/test/syscall.test b/test/syscall.test index 5716b35a6e..fe4a4177fe 100644 --- a/test/syscall.test +++ b/test/syscall.test @@ -61,7 +61,7 @@ foreach s { fcntl read pread write pwrite fchmod fallocate pread64 pwrite64 unlink openDirectory mkdir rmdir statvfs fchown geteuid umask mmap munmap mremap - getpagesize readlink lstat + getpagesize readlink lstat ioctl } { if {[test_syscall exists $s]} {lappend syscall_list $s} } @@ -211,15 +211,15 @@ forcedelete test.db test.db2 proc create_db_file {nByte} { set fd [open test.db w] - fconfigure $fd -translation binary -encoding binary + fconfigure $fd -translation binary puts -nonewline $fd [string range "xSQLite" 1 $nByte] close $fd } foreach {nByte res} { 1 {0 {}} - 2 {1 {file is encrypted or is not a database}} - 3 {1 {file is encrypted or is not a database}} + 2 {1 {file is not a database}} + 3 {1 {file is not a database}} } { do_test 7.$nByte { create_db_file $nByte diff --git a/test/tabfunc01.test b/test/tabfunc01.test index d85cb20ff5..9a2017c46a 100644 --- a/test/tabfunc01.test +++ b/test/tabfunc01.test @@ -22,13 +22,23 @@ ifcapable !vtab { return } load_static_extension db series +load_static_extension db remember do_execsql_test tabfunc01-1.1 { SELECT *, '|' FROM generate_series WHERE start=1 AND stop=9 AND step=2; } {1 | 3 | 5 | 7 | 9 |} +do_execsql_test tabfunc01-1.1b { + PRAGMA table_xinfo(generate_series); +} {0 value {} 0 {} 0 0 1 start {} 0 {} 0 1 2 stop {} 0 {} 0 1 3 step {} 0 {} 0 1} do_execsql_test tabfunc01-1.2 { - SELECT *, '|' FROM generate_series LIMIT 5; + SELECT *, '|' FROM generate_series(0) LIMIT 5; } {0 | 1 | 2 | 3 | 4 |} +do_catchsql_test tabfunc01-1.2b { + SELECT *, '|' FROM generate_series LIMIT 5; +} {1 {first argument to "generate_series()" missing or unusable}} +do_catchsql_test tabfunc01-1.2c { + SELECT *, '|' FROM generate_series(value) LIMIT 5; +} {1 {first argument to "generate_series()" missing or unusable}} do_catchsql_test tabfunc01-1.3 { CREATE VIRTUAL TABLE t1 USING generate_series; } {1 {no such module: generate_series}} @@ -50,10 +60,10 @@ do_execsql_test tabfunc01-1.8 { } {30 25 20 15 10 5 0} do_execsql_test tabfunc01-1.9 { SELECT rowid, * FROM generate_series(0,32,5) ORDER BY value DESC; -} {1 30 2 25 3 20 4 15 5 10 6 5 7 0} +} {30 30 25 25 20 20 15 15 10 10 5 5 0 0} do_execsql_test tabfunc01-1.10 { SELECT rowid, * FROM generate_series(0,32,5) ORDER BY +value DESC; -} {7 30 6 25 5 20 4 15 3 10 2 5 1 0} +} {30 30 25 25 20 20 15 15 10 10 5 5 0 0} do_execsql_test tabfunc01-1.20 { CREATE VIEW v1(a,b) AS VALUES(1,2),(3,4); @@ -99,25 +109,571 @@ do_execsql_test tabfunc01-2.2 { } {2 1 | 2 2 | 3 1 | 3 2 | 3 3 |} do_execsql_test tabfunc01-2.50 { - SELECT * FROM generate_series() LIMIT 5; + SELECT * FROM generate_series(0) LIMIT 5; } {0 1 2 3 4} do_execsql_test tabfunc01-3.1 { SELECT DISTINCT value FROM generate_series(1,x), t1 ORDER BY 1; } {1 2 3} -# Eponymous virtual table exists in the "main" schema only +do_eqp_test tabfunc01-3.10 { + SELECT value FROM generate_series(1,10) ORDER BY value; +} { + QUERY PLAN + `--SCAN generate_series VIRTUAL TABLE INDEX 0x13: +} +do_eqp_test tabfunc01-3.11 { + SELECT value FROM generate_series(1,10) ORDER BY +value; +} { + QUERY PLAN + |--SCAN generate_series VIRTUAL TABLE INDEX 0x3: + `--USE TEMP B-TREE FOR ORDER BY +} +do_eqp_test tabfunc01-3.12 { + SELECT value FROM generate_series(1,10) ORDER BY value, stop; +} { + QUERY PLAN + `--SCAN generate_series VIRTUAL TABLE INDEX 0x13: +} +do_eqp_test tabfunc01-3.13 { + SELECT value FROM generate_series(1,10) ORDER BY stop, value; +} { + QUERY PLAN + |--SCAN generate_series VIRTUAL TABLE INDEX 0x3: + `--USE TEMP B-TREE FOR ORDER BY +} + + +do_eqp_test tabfunc01-3.20 { + WITH t1(a) AS ( + SELECT value FROM generate_series(0,10,2) + UNION ALL + SELECT value FROM generate_series(9,18,3) + ) + SELECT * FROM t1 ORDER BY a; +} { + QUERY PLAN + `--MERGE (UNION ALL) + |--LEFT + | `--SCAN generate_series VIRTUAL TABLE INDEX 0x17: + `--RIGHT + `--SCAN generate_series VIRTUAL TABLE INDEX 0x17: +} + + +# Eponymous virtual table exists in all schemas. # do_execsql_test tabfunc01-4.1 { SELECT * FROM main.generate_series(1,4) } {1 2 3 4} -do_catchsql_test tabfunc01-4.2 { +do_execsql_test tabfunc01-4.2 { SELECT * FROM temp.generate_series(1,4) -} {1 {no such table: temp.generate_series}} -do_catchsql_test tabfunc01-4.3 { +} {1 2 3 4} +do_execsql_test tabfunc01-4.3 { ATTACH ':memory:' AS aux1; CREATE TABLE aux1.t1(a,b,c); SELECT * FROM aux1.generate_series(1,4) -} {1 {no such table: aux1.generate_series}} +} {1 2 3 4} + +# 2018-12-03: Fix bug reported by by private email. +do_execsql_test tabfunc01-4.4 { + SELECT * FROM (generate_series(1,5,2)) AS x LIMIT 10; +} {1 3 5} + +# 2025-03-13 forum post bf2dc8e909983511 +# +do_execsql_test tabfunc01-5.1 { + SELECT value + FROM generate_series(60,73,6) + WHERE value=66; +} 66 +do_execsql_test tabfunc01-5.2 { + SELECT value + FROM generate_series(73,60,-6) + WHERE value=67; +} 67 + +# 2025-03-22 forum post 0d5d63257e3ff4f6 +# +do_execsql_test tabfunc01-6.1 { + SELECT value FROM generate_series(1,10) WHERE value<5.5; +} {1 2 3 4 5} +do_execsql_test tabfunc01-6.2 { + SELECT value FROM generate_series(1,10) WHERE value<5.0; +} {1 2 3 4} +do_execsql_test tabfunc01-6.3 { + SELECT value FROM generate_series(1,10) WHERE value<=5.5; +} {1 2 3 4 5} +do_execsql_test tabfunc01-6.4 { + SELECT value FROM generate_series(1,10) WHERE value<=5.0; +} {1 2 3 4 5} +do_execsql_test tabfunc01-6.5 { + SELECT value FROM generate_series(1,10) WHERE value>5.5; +} {6 7 8 9 10} +do_execsql_test tabfunc01-6.6 { + SELECT value FROM generate_series(1,10) WHERE value>5.0; +} {6 7 8 9 10} +do_execsql_test tabfunc01-6.7 { + SELECT value FROM generate_series(1,10) WHERE value>=5.5; +} {6 7 8 9 10} +do_execsql_test tabfunc01-6.8 { + SELECT value FROM generate_series(1,10) WHERE value>=5.0; +} {5 6 7 8 9 10} +do_execsql_test tabfunc01-6.9 { + SELECT value FROM generate_series(10,1,-1) WHERE value<5.5; +} {5 4 3 2 1} +do_execsql_test tabfunc01-6.10 { + SELECT value FROM generate_series(10,1,-1) WHERE value<5.0; +} {4 3 2 1} +do_execsql_test tabfunc01-6.11 { + SELECT value FROM generate_series(10,1,-1) WHERE value<=5.5; +} {5 4 3 2 1} +do_execsql_test tabfunc01-6.12 { + SELECT value FROM generate_series(10,1,-1) WHERE value<=5.0; +} {5 4 3 2 1} +do_execsql_test tabfunc01-6.13 { + SELECT value FROM generate_series(10,1,-1) WHERE value>5.5; +} {10 9 8 7 6} +do_execsql_test tabfunc01-6.14 { + SELECT value FROM generate_series(10,1,-1) WHERE value>5.0; +} {10 9 8 7 6} +do_execsql_test tabfunc01-6.15 { + SELECT value FROM generate_series(10,1,-1) WHERE value>=5.5; +} {10 9 8 7 6} +do_execsql_test tabfunc01-6.16 { + SELECT value FROM generate_series(10,1,-1) WHERE value>=5.0; +} {10 9 8 7 6 5} +do_execsql_test tabfunc01-6.17 { + SELECT value FROM generate_series(1,10) WHERE value==5.5; +} {} +do_execsql_test tabfunc01-6.18 { + SELECT value FROM generate_series(1,10) WHERE value==5.0; +} {5} + +# The next series of tests is verifying that virtual table are able +# to optimize the IN operator, even on terms that are not marked "omit". +# When the generate_series virtual table is compiled for the testfixture, +# the special -DSQLITE_SERIES_CONSTRAINT_VERIFY=1 option is used, which +# causes the xBestIndex method of generate_series to leave the +# sqlite3_index_constraint_usage.omit flag set to 0, which should cause +# the SQLite core to verify the start=, stop=, and step= constraints on +# each step of output. At one point, the IN operator could not be used +# by virtual tables unless omit was set. +# +do_execsql_test tabfunc01-500 { + SELECT * FROM generate_series WHERE start IN (1,7) AND stop=20 AND step=10 + ORDER BY +1; +} {1 7 11 17} + +# Table-valued functions on the RHS of an IN operator +# +do_execsql_test tabfunc01-600 { + CREATE TABLE t600(a INTEGER PRIMARY KEY, b TEXT); + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<100) + INSERT INTO t600(a,b) SELECT x, printf('(%03d)',x) FROM c; + SELECT b FROM t600 WHERE a IN generate_series(2,52,10); +} {(002) (012) (022) (032) (042) (052)} + + +do_test tabfunc01-700 { + set PTR1 [intarray_addr 5 7 13 17 23] + sqlite3_create_function db + db eval { + SELECT b FROM t600, carray(inttoptr($PTR1),5) WHERE a=value; + } +} {(005) (007) (013) (017) (023)} +do_test tabfunc01-701 { + db eval { + SELECT b FROM t600 WHERE a IN carray(inttoptr($PTR1),5,'int32'); + } +} {(005) (007) (013) (017) (023)} +do_test tabfunc01-702 { + db eval { + SELECT b FROM t600 WHERE a IN carray(inttoptr($PTR1),4,'int32'); + } +} {(005) (007) (013) (017)} +do_catchsql_test tabfunc01-710 { + SELECT b FROM t600 WHERE a IN carray(inttoptr($PTR1),5,'int33'); +} {1 {unknown datatype: 'int33'}} + +do_test tabfunc01-720 { + set PTR2 [int64array_addr 5 7 13 17 23] + db eval { + SELECT b FROM t600, carray(inttoptr($PTR2),5,'int64') WHERE a=value; + } +} {(005) (007) (013) (017) (023)} +do_test tabfunc01-721 { + db eval { + SELECT remember(123,inttoptr($PTR2)); + SELECT value FROM carray(inttoptr($PTR2),5,'int64'); + } +} {123 123 7 13 17 23} +do_test tabfunc01-722 { + set PTR3 [expr {$PTR2+16}] + db eval { + SELECT remember(987,inttoptr($PTR3)); + SELECT value FROM carray(inttoptr($PTR2),5,'int64'); + } +} {987 123 7 987 17 23} + +do_test tabfunc01-730 { + set PTR4 [doublearray_addr 5.0 7.0 13.0 17.0 23.0] + db eval { + SELECT b FROM t600, carray(inttoptr($PTR4),5,'double') WHERE a=value; + } +} {(005) (007) (013) (017) (023)} + +do_test tabfunc01-740 { + set PTR5 [textarray_addr x5 x7 x13 x17 x23] + db eval { + SELECT b FROM t600, carray(inttoptr($PTR5),5,'char*') + WHERE a=trim(value,'x'); + } +} {(005) (007) (013) (017) (023)} + +do_test tabfunc01-750 { + db eval { + SELECT aa.value, bb.value, '|' + FROM carray(inttoptr($PTR4),5,'double') AS aa + JOIN carray(inttoptr($PTR5),5,'char*') AS bb ON aa.rowid=bb.rowid; + } +} {5.0 x5 | 7.0 x7 | 13.0 x13 | 17.0 x17 | 23.0 x23 |} + +# ticket https://sqlite.org/src/info/2ae0c599b735d59e +# Verification of testtag-20230227a +do_test tabfunc01-751 { + db eval { + SELECT aa.value, bb.value, '|' + FROM carray(inttoptr($PTR4),5,'double') AS aa + LEFT JOIN carray(inttoptr($PTR5),5,'char*') AS bb ON aa.rowid=bb.rowid; + } +} {5.0 x5 | 7.0 x7 | 13.0 x13 | 17.0 x17 | 23.0 x23 |} + +ifcapable altertable { + do_test tabfunc01-800 { + catchsql { + ALTER TABLE generate_series ADD COLUMN col2; + } + } {1 {virtual tables may not be altered}} + do_test tabfunc01-810 { + catchsql { + ALTER TABLE generate_series RENAME TO flubber; + } + } {1 {table generate_series may not be altered}} + do_test tabfunc01-820 { + catchsql { + ALTER TABLE generate_series RENAME start TO flubber; + } + } {1 {table generate_series may not be altered}} + do_test tabfunc01-830 { + catchsql { + ALTER TABLE generate_series DROP COLUMN start; + } + } {1 {table generate_series may not be altered}} + do_test tabfunc01-900 { + catchsql { + ALTER TABLE pragma_compile_options ADD COLUMN col2; + } + } {1 {virtual tables may not be altered}} + do_test tabfunc01-910 { + catchsql { + ALTER TABLE pragma_compile_options RENAME TO flubber; + } + } {1 {table pragma_compile_options may not be altered}} + do_test tabfunc01-920 { + catchsql { + ALTER TABLE pragma_compile_options RENAME start TO flubber; + } + } {1 {table pragma_compile_options may not be altered}} + do_test tabfunc01-930 { + catchsql { + ALTER TABLE pragma_compile_options DROP COLUMN start; + } + } {1 {table pragma_compile_options may not be altered}} +} + +#----------------------------------------------------------------------------- +# 2024-04-26 LIMIT and OFFSET passed into virtual tables +# https://sqlite.org/forum/forumpost/c243b8f856 +# +do_execsql_test tabfunc01-900 { + SELECT * FROM ( + SELECT * FROM generate_series(1,10) + UNION ALL + SELECT * FROM generate_series(101,104) + ) LIMIT 10 OFFSET 5; +} {6 7 8 9 10 101 102 103 104} +do_execsql_test tabfunc01-910 { + SELECT * FROM ( + SELECT * FROM generate_series(1,10) + UNION ALL + SELECT * FROM generate_series(101,104) + ) LIMIT -1 OFFSET 5; +} {6 7 8 9 10 101 102 103 104} +do_execsql_test tabfunc01-920 { + SELECT * FROM ( + SELECT * FROM generate_series(1,10) + UNION ALL + SELECT * FROM generate_series(101,104) + ) LIMIT -1 OFFSET 0; +} {1 2 3 4 5 6 7 8 9 10 101 102 103 104} + +# Also: https://sqlite.org/forum/forumpost/ce1a06c2c8 +# +do_execsql_test tabfunc01-930 { + SELECT * FROM generate_series(9223372036854775807, + -9223372036854775808, + -9223372036854775808); +} {9223372036854775807 -1} +do_execsql_test tabfunc01-931 { + SELECT * FROM generate_series(9223372036854775807, + -9223372036854775808, + -9223372036854775808) + LIMIT 100 OFFSET 1; +} {-1} +do_execsql_test tabfunc01-932 { + SELECT * FROM generate_series(9223372036854775807, + -9223372036854775808, + -9223372036854775808) + LIMIT 100 OFFSET 2; +} {} +do_execsql_test tabfunc01-933 { + SELECT * FROM generate_series(9223372036854775807, + -9223372036854775808, + -9223372036854775808) + LIMIT 100 OFFSET 9223372036854775807; +} {} +do_execsql_test tabfunc01-940 { + SELECT * FROM generate_series(1,11,2); +} {1 3 5 7 9 11} +do_execsql_test tabfunc01-941 { + SELECT * FROM generate_series(1,11,2) LIMIT 100 OFFSET 4; +} {9 11} +do_execsql_test tabfunc01-942 { + SELECT * FROM generate_series(1,11,2) LIMIT 100 OFFSET 5; +} {11} +do_execsql_test tabfunc01-943 { + SELECT * FROM generate_series(1,11,2) LIMIT 100 OFFSET 6; +} {} +do_execsql_test tabfunc01-944 { + SELECT * FROM generate_series(1,11,2) LIMIT 100 OFFSET 7; +} {} +do_execsql_test tabfunc01-945 { + SELECT * FROM generate_series(1,11,2) LIMIT 100 OFFSET 9223372036854775807; +} {} +do_execsql_test tabfunc01-950 { + SELECT * FROM generate_series(1,11,1) LIMIT 100 OFFSET 10; +} {11} +do_execsql_test tabfunc01-951 { + SELECT * FROM generate_series(1,11,2) LIMIT 100 OFFSET 9223372036854775807; +} {} +do_execsql_test tabfunc01-952 { + SELECT * FROM generate_series(11,1,-1) LIMIT 100 OFFSET 10; +} {1} +do_execsql_test tabfunc01-953 { + SELECT * FROM generate_series(11,1,-1) LIMIT 100 OFFSET 9223372036854775807; +} {} +do_execsql_test tabfunc01-954 { + SELECT * FROM generate_series(1,11,1) LIMIT 3 OFFSET 3; +} {4 5 6} + + +#------------------------------------------------------------------------- +# Forum post https://sqlite.org/forum/forumpost/e7c3ae1215 +# +foreach {tn where res} { + 1000 "where value = 2" 2 + 1010 "where value in (2)" 2 + 1020 "where value in (select 2)" 2 + 1030 "where value = 2 OR value = 4" {2 4} + 1040 "where value in (2, 4)" {2 4} +} { + do_execsql_test $tn " + SELECT value FROM generate_series(1, 5) $where + " $res +} + +do_execsql_test 1100 { + select 1 as c_0 + from + generate_series(1, 1) as ref_3 + where (ref_3.value) in (select 1); +} {1} + +# 2025-03-18 /forumpost/1e17219c88 +# The generate_series() table-valued function is modified so that its +# rowid is always its value. That way it can be used on the RHS of a +# RIGHT JOIN. +# +do_execsql_test 1200 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(value INT); + INSERT INTO t1 VALUES (1),(2),(3); + SELECT t1.value, t2.value + FROM t1 RIGHT JOIN generate_series(1,3,1) AS t2 USING(value); +} {1 1 2 2 3 3} + +#-------------------------------------------------------------------------- +# New test cases for generate_series associated with the refactor on branch +# serices-refactor circa 2025-09-27. +# +do_execsql_test 1300 { + SELECT value + FROM generate_series(-9223372036854775808, 9223372036854775807, 2) + WHERE value BETWEEN 1 AND 5; +} {2 4} +do_execsql_test 1301 { + SELECT value + FROM generate_series(-9223372036854775808, 9223372036854775807, 2) + WHERE value BETWEEN 0 AND 6; +} {0 2 4 6} +do_execsql_test 1302 { + SELECT value + FROM generate_series(-9223372036854775808, 9223372036854775807, 2) + WHERE value BETWEEN 0.5 AND 6.25; +} {2 4 6} +do_execsql_test 1303 { + SELECT value + FROM generate_series(-9223372036854775808, 9223372036854775807, 2) + WHERE value BETWEEN 9223372036854775803 AND 9223372036854775807 +} {9223372036854775804 9223372036854775806} +do_execsql_test 1304 { + SELECT value + FROM generate_series(-9223372036854775808, 9223372036854775807, 2) + WHERE value BETWEEN 9223372036854775803 AND 9223372036854775807 + ORDER BY value DESC; +} {9223372036854775806 9223372036854775804} + +do_execsql_test 1310 { + SELECT value FROM generate_series(0) LIMIT 5; +} {0 1 2 3 4} +do_execsql_test 1311 { + SELECT value FROM generate_series(5) LIMIT 5; +} {5 6 7 8 9} +do_execsql_test 1312 { + SELECT value FROM generate_series(0) WHERE stop=6; +} {0 1 2 3 4 5 6} +do_execsql_test 1313 { + SELECT value FROM generate_series(1,10) WHERE step IS NULL; +} {} +do_execsql_test 1314 { + SELECT value FROM generate_series(0xfffffffd); +} {4294967293 4294967294 4294967295} +do_execsql_test 1315 { + SELECT value FROM generate_series(4,3,1); +} {} +do_execsql_test 1316 { + SELECT value FROM generate_series(3,4,-1); +} {} + +do_execsql_test 1320 { + SELECT value FROM generate_series(-9223372036854775808,+9223372036854775807) + WHERE value=9.2234e18; +} {} +do_execsql_test 1321 { + SELECT value FROM generate_series(-9223372036854775808,+9223372036854775807) + WHERE value=-9.2234e18; +} {} +do_execsql_test 1322 { + SELECT value FROM generate_series(-9223372036854775808,+9223372036854775807) + WHERE value>9223372036854775807; +} {} +do_execsql_test 1323 { + SELECT value FROM generate_series(-9223372036854775808,+9223372036854775807) + WHERE value>9223372036854775806; +} {9223372036854775807} +do_execsql_test 1324 { + SELECT value FROM generate_series(-9223372036854775808,+9223372036854775807) + WHERE value>=9223372036854775807; +} {9223372036854775807} + +do_execsql_test 1330 { + SELECT value FROM generate_series(-9223372036854775808,+9223372036854775807) + WHERE value<-9223372036854775808; +} {} +do_execsql_test 1331 { + SELECT value FROM generate_series(-9223372036854775808,+9223372036854775807) + WHERE value<=-9223372036854775808; +} {-9223372036854775808} +do_execsql_test 1332 { + SELECT value FROM generate_series(-9223372036854775808,+9223372036854775807) + WHERE value<-9223372036854775807; +} {-9223372036854775808} +do_execsql_test 1333 { + SELECT value FROM generate_series(-9223372036854775808,+9223372036854775807) + WHERE value BETWEEN 4 AND 1; +} {} + +do_execsql_test 1340 { + SELECT value FROM generate_series(100,0,-10) + WHERE value BETWEEN 33 AND 66; +} {60 50 40} +do_execsql_test 1341 { + SELECT value FROM generate_series(100,0,-10) + WHERE value BETWEEN 33 AND 60; +} {60 50 40} +do_execsql_test 1342 { + SELECT value FROM generate_series(100,0,-10) + WHERE value BETWEEN 33 AND 59; +} {50 40} +do_execsql_test 1343 { + SELECT value + FROM generate_series(-9223372036854760000,-9223372036854775808,-10000); +} {-9223372036854760000 -9223372036854770000} +do_execsql_test 1344 { + SELECT value + FROM generate_series(-9223372036854760000,-9223372036854775808,-10000) + WHERE value < -9223372036854770001; +} {} +do_execsql_test 1345 { + SELECT value + FROM generate_series(9223372036854760000,9223372036854775807,10000); +} {9223372036854760000 9223372036854770000} +do_execsql_test 1346 { + SELECT value + FROM generate_series(9223372036854760000,9223372036854775807,10000) + WHERE value > 9223372036854770001; +} {} + +do_execsql_test 1350 { + SELECT value FROM generate_series(100,0,-10) + WHERE value BETWEEN 33 AND 38; +} {} +do_execsql_test 1351 { + SELECT value FROM generate_series(0,100,+10) + WHERE value BETWEEN 33 AND 38; +} {} + +do_execsql_test 1360 { + SELECT * FROM generate_series(0,-9223372036854775808,-9223372036854775808); +} {0 -9223372036854775808} +do_execsql_test 1361 { + SELECT * FROM generate_series(0,-9223372036854775808,-9223372036854775808) + LIMIT 1 OFFSET 0; +} {0} +do_execsql_test 1362 { + SELECT * FROM generate_series(0,-9223372036854775808,-9223372036854775808) + ORDER BY value ASC; +} {-9223372036854775808 0} +do_execsql_test 1363 { + SELECT * FROM generate_series(0,-9223372036854775808,-9223372036854775808) + ORDER BY value ASC LIMIT 10 OFFSET 1; +} {0} +do_execsql_test 1364 { + SELECT * FROM generate_series(0,-9223372036854775808,-9223372036854775808) + ORDER BY value ASC LIMIT 10 OFFSET 40000000; +} {} + +do_execsql_test 1370 { + SELECT * FROM generate_series(0,0,0); +} {} + + + +# Free up memory allocations +intarray_addr +int64array_addr +doublearray_addr +textarray_addr finish_test diff --git a/test/table.test b/test/table.test index e24e3b9ed4..46ecd7d23b 100644 --- a/test/table.test +++ b/test/table.test @@ -276,6 +276,7 @@ do_test table-5.2.2 { db close forcedelete test.db sqlite3 db test.db + sqlite3_db_config db DEFENSIVE 0 db eval { CREATE TABLE t0(a,b); CREATE INDEX t ON t0(a); @@ -783,15 +784,15 @@ do_catchsql_test table-16.5 { } {1 {unknown function: count()}} do_catchsql_test table-16.6 { DROP TABLE t16; - CREATE TABLE t16(x DEFAULT(group_concat('x',','))); + CREATE TABLE t16(x DEFAULT(string_agg('x',','))); INSERT INTO t16(rowid) VALUES(123); SELECT rowid, x FROM t16; -} {1 {unknown function: group_concat()}} +} {1 {unknown function: string_agg()}} do_catchsql_test table-16.7 { INSERT INTO t16 DEFAULT VALUES; -} {1 {unknown function: group_concat()}} +} {1 {unknown function: string_agg()}} -# Ticket [https://www.sqlite.org/src/info/094d39a4c95ee4abbc417f04214617675ba15c63] +# Ticket [https://sqlite.org/src/info/094d39a4c95ee4abbc417f04214617675ba15c63] # describes a assertion fault that occurs on a CREATE TABLE .. AS SELECT statement. # the following test verifies that the problem has been fixed. # @@ -809,7 +810,7 @@ do_execsql_test table-17.1 { } {1 1 | 2 2 |} # 2015-06-16 -# Ticket [https://www.sqlite.org/src/tktview/873cae2b6e25b1991ce5e9b782f9cd0409b96063] +# Ticket [https://sqlite.org/src/tktview/873cae2b6e25b1991ce5e9b782f9cd0409b96063] # Make sure a CREATE TABLE AS statement correctly rolls back partial changes to the # sqlite_master table when the SELECT on the right-hand side aborts. # @@ -824,7 +825,7 @@ do_execsql_test table-18.2 { } {ok} # 2015-09-09 -# Ticket [https://www.sqlite.org/src/info/acd12990885d9276] +# Ticket [https://sqlite.org/src/info/acd12990885d9276] # "CREATE TABLE ... AS SELECT ... FROM sqlite_master" fails because the row # in the sqlite_master table for the next table is initially populated # with a NULL instead of a record created by OP_Record. diff --git a/test/tableapi.test b/test/tableapi.test index 122267fea8..7720737474 100644 --- a/test/tableapi.test +++ b/test/tableapi.test @@ -22,9 +22,7 @@ ifcapable !gettable { return } -ifcapable memdebug { - source $testdir/malloc_common.tcl -} +source $testdir/malloc_common.tcl do_test tableapi-1.0 { set ::dbx [sqlite3_open test.db] @@ -232,25 +230,23 @@ ifcapable schema_pragmas { } # do_malloc_test closes and deletes the usual db connections and files on -# each iteration. $::dbx is a seperate connection, and on Windows, will +# each iteration. $::dbx is a separate connection, and on Windows, will # cause the file deletion of test.db to fail, so we move the close of $::dbx # up to here before the do_malloc_test. do_test tableapi-99.0 { sqlite3_close $::dbx } {SQLITE_OK} -ifcapable memdebug { - do_malloc_test tableapi-7 -sqlprep { - DROP TABLE IF EXISTS t1; - CREATE TABLE t1(a,b); - INSERT INTO t1 VALUES(1,2); - INSERT INTO t1 VALUES(3,4); - INSERT INTO t1 SELECT a+4, b+4 FROM t1; - INSERT INTO t1 SELECT a+8, b+8 FROM t1; - } -tclbody { - set r [sqlite3_get_table_printf db {SELECT rowid, a, b FROM t1} {}] - if {[llength $r]<26} {error "out of memory"} - } +do_malloc_test tableapi-7 -sqlprep { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a,b); + INSERT INTO t1 VALUES(1,2); + INSERT INTO t1 VALUES(3,4); + INSERT INTO t1 SELECT a+4, b+4 FROM t1; + INSERT INTO t1 SELECT a+8, b+8 FROM t1; +} -tclbody { + set r [sqlite3_get_table_printf db {SELECT rowid, a, b FROM t1} {}] + if {[llength $r]<26} {error "out of memory"} } finish_test diff --git a/test/tclsqlite.test b/test/tclsqlite.test index 8d7fea0d2a..5f373ea18a 100644 --- a/test/tclsqlite.test +++ b/test/tclsqlite.test @@ -9,7 +9,7 @@ # #*********************************************************************** # This file implements regression tests for TCL interface to the -# SQLite library. +# SQLite library. # # Actually, all tests are based on the TCL interface, so the main # interface is pretty well tested. This file contains some addition @@ -17,25 +17,32 @@ # # $Id: tclsqlite.test,v 1.73 2009/03/16 13:19:36 danielk1977 Exp $ +catch {sqlite3} + set testdir [file dirname $argv0] source $testdir/tester.tcl +set testprefix tcl # Check the error messages generated by tclsqlite # +set r "sqlite_orig HANDLE ?FILENAME? ?-vfs VFSNAME? ?-readonly BOOLEAN? ?-create BOOLEAN? ?-nofollow BOOLEAN? ?-nomutex BOOLEAN? ?-fullmutex BOOLEAN? ?-uri BOOLEAN?" if {[sqlite3 -has-codec]} { - set r "sqlite_orig HANDLE FILENAME ?-key CODEC-KEY?" -} else { - set r "sqlite_orig HANDLE FILENAME ?-vfs VFSNAME? ?-readonly BOOLEAN? ?-create BOOLEAN? ?-nomutex BOOLEAN? ?-fullmutex BOOLEAN? ?-uri BOOLEAN?" + append r " ?-key CODECKEY?" } do_test tcl-1.1 { - set v [catch {sqlite3 bogus} msg] + set v [catch {sqlite3 -bogus} msg] + regsub {really_sqlite3} $msg {sqlite3} msg + lappend v $msg +} [list 1 "wrong # args: should be \"$r\""] +do_test tcl-1.1.1 { + set v [catch {sqlite3} msg] regsub {really_sqlite3} $msg {sqlite3} msg lappend v $msg } [list 1 "wrong # args: should be \"$r\""] do_test tcl-1.2 { set v [catch {db bogus} msg] lappend v $msg -} {1 {bad option "bogus": must be authorizer, backup, busy, cache, changes, close, collate, collation_needed, commit_hook, complete, copy, enable_load_extension, errorcode, eval, exists, function, incrblob, interrupt, last_insert_rowid, nullvalue, onecolumn, profile, progress, rekey, restore, rollback_hook, status, timeout, total_changes, trace, transaction, unlock_notify, update_hook, version, or wal_hook}} +} {1 {bad option "bogus": must be authorizer, backup, bind_fallback, busy, cache, changes, close, collate, collation_needed, commit_hook, complete, config, copy, deserialize, enable_load_extension, errorcode, erroroffset, eval, exists, function, incrblob, interrupt, last_insert_rowid, nullvalue, onecolumn, preupdate, profile, progress, rekey, restore, rollback_hook, serialize, status, timeout, total_changes, trace, trace_v2, transaction, unlock_notify, update_hook, version, or wal_hook}} do_test tcl-1.2.1 { set v [catch {db cache bogus} msg] lappend v $msg @@ -114,7 +121,7 @@ ifcapable {complete} { do_test tcl-1.14 { set v [catch {db eval} msg] lappend v $msg -} {1 {wrong # args: should be "db eval SQL ?ARRAY-NAME? ?SCRIPT?"}} +} {1 {wrong # args: should be "db eval ?OPTIONS? SQL ?VAR-NAME? ?SCRIPT?"}} do_test tcl-1.15 { set v [catch {db function} msg] lappend v $msg @@ -159,7 +166,7 @@ do_test tcl-2.1 { ifcapable schema_pragmas { do_test tcl-2.2 { execsql "PRAGMA table_info(t\u0123x)" - } "0 a int 0 {} 0 1 b\u1235 float 0 {} 0" + } "0 a INT 0 {} 0 1 b\u1235 float 0 {} 0" } do_test tcl-2.3 { execsql "INSERT INTO t\u0123x VALUES(1,2.3)" @@ -352,6 +359,19 @@ do_test tcl-9.3 { execsql {SELECT typeof(ret_int())} } {integer} +proc breakAsNullUdf args { + if {"1" eq [lindex $args 0]} {return -code break} +} +do_test tcl-9.4 { + db function banu breakAsNullUdf + execsql {SELECT typeof(banu()), typeof(banu(1))} +} {text null} +do_test tcl-9.5 { + db nullvalue banunull + db eval {SELECT banu(), banu(1)} +} {{} banunull} + + # Recursive calls to the same user-defined function # ifcapable tclvar { @@ -364,9 +384,10 @@ ifcapable tclvar { db function r1 userfunc_r1 execsql {SELECT r1(10)} } {55} - do_test tcl-9.11 { - execsql {SELECT r1(100)} - } {5050} + # Fails under -fsanitize=address,undefined due to stack overflow + # do_test tcl-9.11 { + # execsql {SELECT r1(100)} + # } {5050} } # Tests for the new transaction method @@ -457,7 +478,7 @@ do_test tcl-10.13 { db eval {SELECT * FROM t4} } {1 2 5 6 7} -# Now test that [db transaction] commands may be nested with +# Now test that [db transaction] commands may be nested with # the expected results. # do_test tcl-10.14 { @@ -467,7 +488,7 @@ do_test tcl-10.14 { INSERT INTO t4 VALUES('one'); } - catch { + catch { db transaction { db eval { INSERT INTO t4 VALUES('two') } db transaction { @@ -636,6 +657,299 @@ do_test tcl-14.2 { db one {SELECT x FROM t6 WHERE xCall()!='value'} } {} +# Verify that the "exists" and "onecolumn" methods work when +# a "profile" is registered. +# +catch {db close} +sqlite3 db :memory: +proc noop-profile {args} { + return +} +do_test tcl-15.0 { + db eval {CREATE TABLE t1(a); INSERT INTO t1 VALUES(1),(2),(3);} + db onecolumn {SELECT a FROM t1 WHERE a>2} +} {3} +do_test tcl-15.1 { + db exists {SELECT a FROM t1 WHERE a>2} +} {1} +do_test tcl-15.2 { + db exists {SELECT a FROM t1 WHERE a>3} +} {0} +db profile noop-profile +do_test tcl-15.3 { + db onecolumn {SELECT a FROM t1 WHERE a>2} +} {3} +do_test tcl-15.4 { + db exists {SELECT a FROM t1 WHERE a>2} +} {1} +do_test tcl-15.5 { + db exists {SELECT a FROM t1 WHERE a>3} +} {0} + + +# 2017-06-26: The -withoutnulls flag to "db eval". +# +# In the "db eval -withoutnulls SQL TARGET" form, NULL results cause the +# corresponding target entry to be unset. The default behavior (without +# the -withoutnulls flags) is for the corresponding target value to get +# the [db nullvalue] string. +# +catch {db close} +forcedelete test.db +sqlite3 db test.db +do_execsql_test tcl-16.100 { + CREATE TABLE t1(a,b); + INSERT INTO t1 VALUES(1,2),(2,NULL),(3,'xyz'); +} +do_test tcl-16.101 { + set res {} + unset -nocomplain x + db eval {SELECT * FROM t1} x { + lappend res $x(a) [array names x] + } + set res +} {1 {a b *} 2 {a b *} 3 {a b *}} +do_test tcl-16.102 { + set res [catch { + db eval -unknown {SELECT * FROM t1} x { + lappend res $x(a) [array names x] + } + } rc] + lappend res $rc +} {1 {unknown option: "-unknown"}} +do_test tcl-16.103 { + set res {} + unset -nocomplain x + db eval -withoutnulls {SELECT * FROM t1} x { + lappend res $x(a) [array names x] + } + set res +} {1 {a b *} 2 {a *} 3 {a b *}} + +#------------------------------------------------------------------------- +# Test the -type option to [db function]. +# +reset_db +proc add {a b} { return [expr $a + $b] } +proc ret {a} { return $a } + +db function add_i -returntype integer add +db function add_r -ret real add +db function add_t -return text add +db function add_b -returntype blob add +db function add_a -returntype any add + +db function ret_i -returntype int ret +db function ret_r -returntype real ret +db function ret_t -returntype text ret +db function ret_b -returntype blob ret +db function ret_a -r any ret + +do_execsql_test 17.0 { + SELECT quote( add_i(2, 3) ); + SELECT quote( add_r(2, 3) ); + SELECT quote( add_t(2, 3) ); + SELECT quote( add_b(2, 3) ); + SELECT quote( add_a(2, 3) ); +} {5 5.0 '5' X'35' 5} + +do_execsql_test 17.1 { + SELECT quote( add_i(2.2, 3.3) ); + SELECT quote( add_r(2.2, 3.3) ); + SELECT quote( add_t(2.2, 3.3) ); + SELECT quote( add_b(2.2, 3.3) ); + SELECT quote( add_a(2.2, 3.3) ); +} {5.5 5.5 '5.5' X'352E35' 5.5} + +do_execsql_test 17.2 { + SELECT quote( ret_i(2.5) ); + SELECT quote( ret_r(2.5) ); + SELECT quote( ret_t(2.5) ); + SELECT quote( ret_b(2.5) ); + SELECT quote( ret_a(2.5) ); +} {2.5 2.5 '2.5' X'322E35' 2.5} + +do_execsql_test 17.3 { + SELECT quote( ret_i('2.5') ); + SELECT quote( ret_r('2.5') ); + SELECT quote( ret_t('2.5') ); + SELECT quote( ret_b('2.5') ); + SELECT quote( ret_a('2.5') ); +} {2.5 2.5 '2.5' X'322E35' '2.5'} + +do_execsql_test 17.4 { + SELECT quote( ret_i('abc') ); + SELECT quote( ret_r('abc') ); + SELECT quote( ret_t('abc') ); + SELECT quote( ret_b('abc') ); + SELECT quote( ret_a('abc') ); +} {'abc' 'abc' 'abc' X'616263' 'abc'} + +do_execsql_test 17.5 { + SELECT quote( ret_i(X'616263') ); + SELECT quote( ret_r(X'616263') ); + SELECT quote( ret_t(X'616263') ); + SELECT quote( ret_b(X'616263') ); + SELECT quote( ret_a(X'616263') ); +} {'abc' 'abc' 'abc' X'616263' X'616263'} + +do_test 17.6.1 { + list [catch { db function xyz -return object ret } msg] $msg +} {1 {bad type "object": must be integer, real, text, blob, or any}} + +do_test 17.6.2 { + list [catch { db function xyz -return ret } msg] $msg +} {1 {option requires an argument: -return}} + +do_test 17.6.3 { + list [catch { db function xyz -n object ret } msg] $msg +} {1 {bad option "-n": must be -argcount, -deterministic, -directonly, -innocuous, or -returntype}} + +# 2019-02-28: The "bind_fallback" command. +# +do_test 18.100 { + unset -nocomplain bindings abc def ghi jkl mno e01 e02 + set bindings(abc) [expr {1+2}] + set bindings(def) {hello} + set bindings(ghi) [expr {3.1415926*1.0}] + proc bind_callback {nm} { + global bindings + set n2 [string range $nm 1 end] + if {[info exists bindings($n2)]} { + return $bindings($n2) + } + if {[string match e* $n2]} { + error "no such variable: $nm" + } + return -code return {} + } + db bind_fallback bind_callback + db eval {SELECT $abc, typeof($abc), $def, typeof($def), $ghi, typeof($ghi)} +} {3 integer hello text 3.1415926 real} +do_test 18.110 { + db eval {SELECT quote(@def), typeof(@def)} +} {X'68656C6C6F' blob} +do_execsql_test 18.120 { + SELECT typeof($mno); +} {null} +do_catchsql_test 18.130 { + SELECT $e01; +} {1 {no such variable: $e01}} +do_test 18.140 { + db bind_fallback +} {bind_callback} +do_test 18.200 { + db bind_fallback {} + db eval {SELECT $abc, typeof($abc), $def, typeof($def), $ghi, typeof($ghi)} +} {{} null {} null {} null} +do_test 18.300 { + unset -nocomplain bindings + proc bind_callback {nm} {lappend ::bindings $nm} + db bind_fallback bind_callback + db eval {SELECT $abc, @def, $ghi(123), :mno} + set bindings +} {{$abc} @def {$ghi(123)} :mno} +do_test 18.900 { + set rc [catch {db bind_fallback a b} msg] + lappend rc $msg +} {1 {wrong # args: should be "db bind_fallback ?CALLBACK?"}} +do_test 18.910 { + db bind_fallback bind_fallback_does_not_exist +} {} +do_catchsql_test 19.911 { + SELECT $abc, typeof($abc), $def, typeof($def), $ghi, typeof($ghi); +} {1 {invalid command name "bind_fallback_does_not_exist"}} +db bind_fallback {} + +# 2025-05-05: the -asdict eval flag +# +do_test 20.0 { + execsql {CREATE TABLE tad(a,b)} + execsql {INSERT INTO tad(a,b) VALUES('aa','bb'),('AA','BB')} + db eval -asdict { + SELECT a, b FROM tad WHERE 0 + } D {} + set D +} {* {a b}} + +do_test 20.1 { + unset D + set i 0 + set res {} + set colNames {} + db eval -asdict { + SELECT a, b FROM tad ORDER BY a + } D { + dict set D i [incr i] + lappend res $i [dict get $D a] [dict get $D b] + if {1 == $i} { + set colNames [dict get $D *] + } + } + lappend res $colNames + unset D + set res +} {1 AA BB 2 aa bb {a b}} + +do_test 20.2 { + set res {} + db eval -asdict -withoutnulls { + SELECT n, a, b FROM ( + SELECT 1 as n, 'aa' as a, NULL as b + UNION ALL + SELECT 2 as n, NULL as a, 'bb' as b + ) + ORDER BY n + } D { + dict unset D * + lappend res [dict values $D] + } + unset D + execsql {DROP TABLE tad} + set res +} {{1 aa} {2 bb}} + +#------------------------------------------------------------------------- +do_test 21.0 { + db transaction { + db close + } +} {} + +do_test 21.1 { + sqlite3 db test.db + set rc [catch { + db eval {SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3} { db close } + } msg] + list $rc $msg +} {1 {invalid command name "db"}} + + + +proc closedb {} { + db close + return 10 +} +proc func1 {} { return 1 } + +sqlite3 db test.db +db func closedb closedb +db func func1 func1 + +do_test 21.2 { + set rc [catch { + db eval { + SELECT closedb(),func1() UNION ALL SELECT 20,30 UNION ALL SELECT 30,40 + } + } msg] + list $rc $msg +} {0 {10 1 20 30 30 40}} + +sqlite3 db :memory: +do_test 22.1 { + catch {db eval {SELECT 1 2 3;}} msg + db erroroffset +} {9} finish_test diff --git a/test/tempdb.test b/test/tempdb.test index ecd23d4e32..a32ef06f44 100644 --- a/test/tempdb.test +++ b/test/tempdb.test @@ -17,6 +17,11 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl +if {[atomic_batch_write test.db]} { + finish_test + return +} + # Use a temporary database. # db close @@ -76,7 +81,7 @@ do_test tempdb-2.2 { } catchsql { INSERT INTO t1 SELECT * FROM t2 } set sqlite_open_file_count -} [expr 1 + (0==$jrnl_in_memory) + (0==$subj_in_memory)] +} [expr 1 + (0==$jrnl_in_memory)] do_test tempdb-2.3 { execsql { PRAGMA temp_store = 'memory'; diff --git a/test/tempdb2.test b/test/tempdb2.test new file mode 100644 index 0000000000..37cb4b33ba --- /dev/null +++ b/test/tempdb2.test @@ -0,0 +1,99 @@ +# 2016 March 3 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix tempdb2 + +db close +sqlite3 db "" + +set unlocked unlocked +if {$::TEMP_STORE>=2} { set unlocked unknown } + +proc int2str {i} { string range [string repeat "$i." 450] 0 899 } +db func int2str int2str + +#------------------------------------------------------------------------- +# +# 1.1: Write a big transaction to the db. One so large that it forces +# the file to be created and the cache flushed to disk on COMMIT. +# +# 1.2: Write a small transaction - one small enough that it remains in +# memory on COMMIT. All the pages of table [t1] are now dirty. +# +# 1.3: Delete the contents of [t1]. This moves all of its leaves to the +# free-list and causes the btree layer to call PagerDontWrite() on +# each of them. +# +# Then do a big update on table [t2]. So big that the former leaves +# of [t1] are forced out of the cache. Then roll back the transaction. +# If the PagerDontWrite() calls are honoured and the data is not written +# to disk, the update made in test 1.2 will be lost at this point. Or, if +# they are ignored (as they should be for temp databases), the update +# will be safely written out to disk before the cache entries are +# discarded. +# +do_execsql_test 1.1 { + PRAGMA page_size=1024; + PRAGMA cache_size=50; + + BEGIN; + CREATE TABLE t1(a INTEGER PRIMARY KEY, b); + INSERT INTO t1 VALUES(1, int2str(1)); + INSERT INTO t1 VALUES(2, int2str(1)); + INSERT INTO t1 VALUES(3, int2str(1)); + + CREATE TABLE t2(a INTEGER PRIMARY KEY, b); + WITH c(x) AS ( VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<100 ) + INSERT INTO t2 SELECT x, int2str(x) FROM c; + COMMIT; + + PRAGMA lock_status; +} [list main $unlocked temp closed] + +do_execsql_test 1.2 { + UPDATE t1 SET b=int2str(2); + SELECT b=int2str(2) FROM t1 +} {1 1 1} + +do_execsql_test 1.3 { + BEGIN; + DELETE FROM t1; + UPDATE t2 SET b=int2str(a+1); + ROLLBACK; +} + +do_execsql_test 1.4 { + SELECT b=int2str(2) FROM t1 +} {1 1 1} + +#------------------------------------------------------------------------- +db close +sqlite3 db "" +db func int2str int2str + +do_execsql_test 2.0 { + PRAGMA cache_size = -100; + CREATE TABLE t1(a INTEGER PRIMARY KEY, b); + WITH c(x) AS ( VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<100 ) + INSERT INTO t1 SELECT x, int2str(x) FROM c; +} + +do_execsql_test 2.1 { + INSERT INTO t1 VALUES(10001, int2str(1001) || int2str(1001) || int2str(1001)); +} + +do_execsql_test 2.2 { + SELECT b FROM t1 WHERE a = 10001; +} "[int2str 1001][int2str 1001][int2str 1001]" + +finish_test diff --git a/test/tempfault.test b/test/tempfault.test new file mode 100644 index 0000000000..4eae116a72 --- /dev/null +++ b/test/tempfault.test @@ -0,0 +1,134 @@ +# 2016 April 11 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# This file contains tests for fault-injection when SQLite is used with +# a temp file database. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/malloc_common.tcl +set testprefix tempfault + +# sqlite3_memdebug_vfs_oom_test 0 + +do_faultsim_test 1 -faults * -prep { + sqlite3 db "" + db eval { + PRAGMA page_size = 1024; + CREATE TABLE t1(a, b); + INSERT INTO t1 VALUES(1, 2); + INSERT INTO t1 VALUES(3, 4); + } +} -body { + execsql { INSERT INTO t1 VALUES(5, 6) } +} -test { + faultsim_test_result {0 {}} + set rc [catch { execsql { SELECT * FROM t1 } } msg] + if {$rc==0 && $msg != "1 2 3 4 5 6" && $msg != "1 2 3 4"} { + error "data mismatch 1: $msg" + } + if {$testrc==0 && $msg != "1 2 3 4 5 6"} { + error "data mismatch 2: $msg" + } + faultsim_integrity_check +} + +do_faultsim_test 2 -faults * -prep { + sqlite3 db "" + db eval { + PRAGMA page_size = 1024; + PRAGMA cache_size = 10; + CREATE TABLE t1(a, b); + CREATE INDEX i1 ON t1(b, a); + WITH x(i) AS (SELECT 1 UNION ALL SELECT i+1 FROM x WHERE i<100) + INSERT INTO t1 SELECT randomblob(100), randomblob(100) FROM x; + } +} -body { + execsql { UPDATE t1 SET a = randomblob(99) } +} -test { + faultsim_test_result {0 {}} + faultsim_integrity_check db +} + +catch { db close } +do_faultsim_test 2.1 -faults * -prep { + if {[info commands db]==""} { + sqlite3 db "" + execsql { + PRAGMA page_size = 1024; + PRAGMA cache_size = 10; + CREATE TABLE t1(a, b); + CREATE INDEX i1 ON t1(b, a); + WITH x(i) AS (SELECT 1 UNION ALL SELECT i+1 FROM x WHERE i<100) + INSERT INTO t1 SELECT randomblob(100), randomblob(100) FROM x; + } + } +} -body { + execsql { UPDATE t1 SET a = randomblob(99) } +} -test { + faultsim_test_result {0 {}} + faultsim_integrity_check db +} + +do_faultsim_test 3 -faults * -prep { + sqlite3 db "" + db eval { + PRAGMA page_size = 1024; + PRAGMA cache_size = 10; + CREATE TABLE t1(a, b); + CREATE INDEX i1 ON t1(b, a); + WITH x(i) AS (SELECT 1 UNION ALL SELECT i+1 FROM x WHERE i<50) + INSERT INTO t1 SELECT randomblob(100), randomblob(100) FROM x; + } +} -body { + execsql { + BEGIN; + UPDATE t1 SET a = randomblob(99); + SAVEPOINT abc; + UPDATE t1 SET a = randomblob(98) WHERE (rowid%10)==0; + ROLLBACK TO abc; + UPDATE t1 SET a = randomblob(97) WHERE (rowid%5)==0; + ROLLBACK TO abc; + COMMIT; + } +} -test { + faultsim_test_result {0 {}} + faultsim_integrity_check db +} + +do_faultsim_test 4 -faults * -prep { + sqlite3 db "" + db eval { + PRAGMA page_size = 1024; + PRAGMA cache_size = 10; + CREATE TABLE t1(a, b); + CREATE INDEX i1 ON t1(b, a); + WITH x(i) AS (SELECT 1 UNION ALL SELECT i+1 FROM x WHERE i<50) + INSERT INTO t1 SELECT randomblob(100), randomblob(100) FROM x; + } +} -body { + execsql { + BEGIN; + UPDATE t1 SET a = randomblob(99); + SAVEPOINT abc; + UPDATE t1 SET a = randomblob(98) WHERE (rowid%10)==0; + ROLLBACK TO abc; + UPDATE t1 SET a = randomblob(97) WHERE (rowid%5)==0; + ROLLBACK TO abc; + COMMIT; + } +} -test { + faultsim_test_result {0 {}} +} + +sqlite3_memdebug_vfs_oom_test 1 +finish_test diff --git a/test/temptable2.test b/test/temptable2.test new file mode 100644 index 0000000000..2ee4adb9f7 --- /dev/null +++ b/test/temptable2.test @@ -0,0 +1,360 @@ +# 2016 March 3 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# TESTRUNNER: slow +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix temptable2 + +do_execsql_test 1.1 { + CREATE TEMP TABLE t1(a, b); + CREATE INDEX i1 ON t1(a, b); +} + +do_execsql_test 1.2 { + WITH x(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM x WHERE i<100000 ) + INSERT INTO t1 SELECT randomblob(100), randomblob(100) FROM X; +} {} + +do_execsql_test 1.3 { + PRAGMA temp.integrity_check; +} {ok} + +#------------------------------------------------------------------------- +# +reset_db +do_execsql_test 2.1 { + CREATE TEMP TABLE t2(a, b); + INSERT INTO t2 VALUES(1, 2); +} {} + +do_execsql_test 2.2 { + BEGIN; + INSERT INTO t2 VALUES(3, 4); + SELECT * FROM t2; +} {1 2 3 4} + +do_execsql_test 2.3 { + ROLLBACK; + SELECT * FROM t2; +} {1 2} + +#------------------------------------------------------------------------- +# +reset_db +do_execsql_test 3.1.1 { + PRAGMA main.cache_size = 10; + PRAGMA temp.cache_size = 10; + + CREATE TEMP TABLE t1(a, b); + CREATE INDEX i1 ON t1(a, b); + + WITH x(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM x WHERE i<1000 ) + INSERT INTO t1 SELECT randomblob(100), randomblob(100) FROM x; + + SELECT count(*) FROM t1; +} {1000} +do_execsql_test 3.1.2 { + BEGIN; + UPDATE t1 SET b=randomblob(100) WHERE (rowid%10)==0; + ROLLBACK; +} +do_execsql_test 3.1.3 { + SELECT count(*) FROM t1; +} {1000} +do_execsql_test 3.1.4 { PRAGMA temp.integrity_check } {ok} + +do_execsql_test 3.2.1 { + BEGIN; + UPDATE t1 SET b=randomblob(100) WHERE (rowid%10)==0; + SAVEPOINT abc; + UPDATE t1 SET b=randomblob(100) WHERE (rowid%10)==1; + ROLLBACK TO abc; + UPDATE t1 SET b=randomblob(100) WHERE (rowid%10)==2; + COMMIT; +} +do_execsql_test 3.2.2 { PRAGMA temp.integrity_check } {ok} + +#------------------------------------------------------------------------- +# +reset_db +do_execsql_test 4.1.1 { + PRAGMA main.cache_size = 10; + PRAGMA temp.cache_size = 10; + + CREATE TEMP TABLE t1(a, b); + CREATE INDEX i1 ON t1(a, b); + + WITH x(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM x WHERE i<10 ) + INSERT INTO t1 SELECT randomblob(100), randomblob(100) FROM x; + + SELECT count(*) FROM t1; + PRAGMA temp.page_count; +} {10 9} + +do_execsql_test 4.1.2 { + BEGIN; + UPDATE t1 SET b=randomblob(100); + ROLLBACK; +} + +do_execsql_test 4.1.3 { + CREATE TEMP TABLE t2(a, b); + CREATE INDEX i2 ON t2(a, b); + WITH x(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM x WHERE i<500 ) + INSERT INTO t2 SELECT randomblob(100), randomblob(100) FROM x; + + SELECT count(*) FROM t2; + SELECT count(*) FROM t1; +} {500 10} + +do_test 4.1.4 { + set n [db one { PRAGMA temp.page_count }] + expr ($n >280 && $n < 300) +} 1 + +do_execsql_test 4.1.4 { PRAGMA temp.integrity_check } {ok} + +#------------------------------------------------------------------------- +# +reset_db +do_execsql_test 5.1.1 { + PRAGMA main.cache_size = 10; + PRAGMA temp.cache_size = 10; + + CREATE TEMP TABLE t2(a, b); + CREATE INDEX i2 ON t2(a, b); + WITH x(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM x WHERE i<500 ) + INSERT INTO t2 SELECT randomblob(100), randomblob(100) FROM x; + + CREATE TEMP TABLE t1(a, b); + CREATE INDEX i1 ON t1(a, b); + INSERT INTO t1 VALUES(1, 2); +} + +# Test that the temp database is now much bigger than the configured +# cache size (10 pages). +do_test 5.1.2 { + set n [db one { PRAGMA temp.page_count }] + expr ($n > 270 && $n < 290) +} {1} + +do_execsql_test 5.1.3 { + BEGIN; + UPDATE t1 SET a=2; + UPDATE t2 SET a=randomblob(100); + SELECT count(*) FROM t1; + ROLLBACK; +} {1} + +do_execsql_test 5.1.4 { + UPDATE t2 SET a=randomblob(100); + + SELECT * FROM t1; +} {1 2} + +do_execsql_test 5.1.5 { PRAGMA temp.integrity_check } {ok} + +#------------------------------------------------------------------------- +# Test this: +# +# 1. Page is DIRTY at the start of a transaction. +# 2. Page is written out as part of the transaction. +# 3. Page is then read back in. +# 4. Transaction is rolled back. Is the page now clean or dirty? +# +# This actually does work. Step 4 marks the page as clean. But it also +# writes to the database file itself. So marking it clean is correct - +# the page does match the contents of the db file. +# +reset_db + +do_execsql_test 6.1 { + PRAGMA main.cache_size = 10; + PRAGMA temp.cache_size = 10; + + CREATE TEMP TABLE t1(x); + INSERT INTO t1 VALUES('one'); + + CREATE TEMP TABLE t2(a, b); + CREATE INDEX i2 ON t2(a, b); + WITH x(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM x WHERE i<500 ) + INSERT INTO t2 SELECT randomblob(100), randomblob(100) FROM x; +} + +do_execsql_test 6.2 { + UPDATE t1 SET x='two'; -- step 1 + BEGIN; + UPDATE t2 SET a=randomblob(100); -- step 2 + SELECT * FROM t1; -- step 3 + ROLLBACK; -- step 4 + + SELECT count(*) FROM t2; + SELECT * FROM t1; +} {two 500 two} + +#------------------------------------------------------------------------- +# +reset_db +sqlite3 db "" +do_execsql_test 7.1 { + PRAGMA auto_vacuum=INCREMENTAL; + CREATE TABLE t1(x); + INSERT INTO t1 VALUES(zeroblob(900)); + INSERT INTO t1 VALUES(zeroblob(900)); + INSERT INTO t1 SELECT x FROM t1; + INSERT INTO t1 SELECT x FROM t1; + INSERT INTO t1 SELECT x FROM t1; + INSERT INTO t1 SELECT x FROM t1; + BEGIN; + DELETE FROM t1 WHERE rowid%2; + PRAGMA incremental_vacuum(4); + ROLLBACK; + PRAGMA integrity_check; +} {ok} + +#------------------------------------------------------------------------- +# Try changing the page size using a backup operation when pages are +# stored in main-memory only. +# +reset_db +do_execsql_test 8.1 { + PRAGMA auto_vacuum = OFF; + CREATE TABLE t2(a, b); + CREATE INDEX i2 ON t2(a, b); + WITH x(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM x WHERE i<20 ) + INSERT INTO t2 SELECT randomblob(100), randomblob(100) FROM x ORDER BY 1, 2; + PRAGMA page_count; +} {13} + +do_test 8.2 { + sqlite3 tmp "" + execsql { + PRAGMA auto_vacuum = OFF; + PRAGMA page_size = 8192; + CREATE TABLE t1(a, b); + CREATE INDEX i1 ON t1(a, b); + WITH x(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM x WHERE i<100 ) + INSERT INTO t1 SELECT randomblob(100), randomblob(100) FROM x ORDER BY 1, 2; + PRAGMA page_count; + } tmp +} {10} + +do_test 8.3 { + sqlite3_backup B tmp main db main + B step 5 + B finish +} {SQLITE_READONLY} + +do_test 8.4 { + execsql { + SELECT count(*) FROM t1; + PRAGMA integrity_check; + PRAGMA page_size; + } tmp +} {100 ok 8192} + +do_test 8.5 { + tmp eval { UPDATE t1 SET a=randomblob(100) } +} {} + +do_test 8.6 { + sqlite3_backup B tmp main db main + B step 1000 + B finish +} {SQLITE_READONLY} + +tmp close + +#------------------------------------------------------------------------- +# Try inserts and deletes with a large db in auto-vacuum mode. Check +# +foreach {tn mode} { + 1 delete + 2 wal +} { + reset_db + sqlite3 db "" + do_execsql_test 9.$tn.1.1 { + PRAGMA cache_size = 15; + PRAGMA auto_vacuum = 1; + } + execsql "PRAGMA journal_mode = $mode" + + do_execsql_test 9.$tn.1.2 { + CREATE TABLE tx(a, b); + CREATE INDEX i1 ON tx(a); + CREATE INDEX i2 ON tx(b); + WITH x(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM x WHERE i<1000 ) + INSERT INTO tx SELECT randomblob(100), randomblob(100) FROM x; + } + + for {set i 2} {$i<20} {incr i} { + do_execsql_test 9.$tn.$i.1 { DELETE FROM tx WHERE (random()%3)==0 } + + do_execsql_test 9.$tn.$i.2 { PRAGMA integrity_check } ok + + do_execsql_test 9.$tn.$i.3 { + WITH x(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM x WHERE i<400 ) + INSERT INTO tx SELECT randomblob(100), randomblob(100) FROM x; + } + + do_execsql_test 9.$tn.$i.4 { PRAGMA integrity_check } ok + + do_execsql_test 9.$tn.$i.5 { + BEGIN; + DELETE FROM tx WHERE (random()%3)==0; + WITH x(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM x WHERE i<500 ) + INSERT INTO tx SELECT randomblob(100), randomblob(100) FROM x; + COMMIT; + } + + do_execsql_test 9.$tn.$i.6 { PRAGMA integrity_check } ok + } +} + +#------------------------------------------------------------------------- +# When using mmap mode with a temp file, SQLite must search the cache +# before using a mapped page even when there is no write transaction +# open. For a temp file, the on-disk version may not be up to date. +# +sqlite3 db "" +do_execsql_test 10.0 { + PRAGMA cache_size = 50; + PRAGMA page_size = 1024; + CREATE TABLE t1(a, b, PRIMARY KEY(a)) WITHOUT ROWID; + CREATE INDEX i1 ON t1(a); + CREATE TABLE t2(x, y); + INSERT INTO t2 VALUES(1, 2); +} + +do_execsql_test 10.1 { + BEGIN; + WITH x(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM x WHERE i<500 ) + INSERT INTO t1 SELECT randomblob(100), randomblob(100) FROM x; + COMMIT; + INSERT INTO t2 VALUES(3, 4); +} + +ifcapable mmap { + if {[permutation]!="journaltest" && $::TEMP_STORE<2} { + # The journaltest permutation does not support mmap, so this part of + # the test is omitted. + do_execsql_test 10.2 { PRAGMA mmap_size = 512000 } 512000 + } +} + +do_execsql_test 10.3 { SELECT * FROM t2 } {1 2 3 4} +do_execsql_test 10.4 { PRAGMA integrity_check } ok + +finish_test diff --git a/test/temptable3.test b/test/temptable3.test new file mode 100644 index 0000000000..16f51d7832 --- /dev/null +++ b/test/temptable3.test @@ -0,0 +1,41 @@ +# 2016-05-10 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix temptable3 + +db close +sqlite3 db {} +do_execsql_test 1.1 { + PRAGMA cache_size = 1; + PRAGMA page_size = 1024; + PRAGMA auto_vacuum = 2; + CREATE TABLE t1(x); + INSERT INTO t1 VALUES( randomblob(800) ); + INSERT INTO t1 VALUES( randomblob(800) ); + CREATE TABLE t2(x); + PRAGMA integrity_check; +} {ok} + +db close +sqlite3 db {} +do_execsql_test 1.2 { + PRAGMA cache_size = 1; + PRAGMA auto_vacuum = 2; + CREATE TABLE t1(x); + CREATE TABLE t2(x UNIQUE); + INSERT INTO t2 VALUES(1), (2), (3); + DROP TABLE t1; + PRAGMA integrity_check; +} {ok} + +finish_test diff --git a/test/temptrigger.test b/test/temptrigger.test index 551c620479..e4277adf65 100644 --- a/test/temptrigger.test +++ b/test/temptrigger.test @@ -236,7 +236,7 @@ do_test 5.1 { do_execsql_test 5.2 { SELECT * FROM sqlite_master; - SELECT * FROM sqlite_temp_master; + SELECT * FROM temp.sqlite_master; } { trigger tr1 t1 0 {CREATE TRIGGER tr1 BEFORE INSERT ON t1 BEGIN SELECT 1,2,3; END} diff --git a/test/tester.tcl b/test/tester.tcl index dc532c97fa..5a3da60cc5 100644 --- a/test/tester.tcl +++ b/test/tester.tcl @@ -25,6 +25,7 @@ # copy_file FROM TO # delete_file FILENAME # drop_all_tables ?DB? +# drop_all_indexes ?DB? # forcecopy FROM TO # forcedelete FILENAME # @@ -88,6 +89,9 @@ # verbose # +# Only run this script once. If sourced a second time, make it a no-op +if {[info exists ::tester_tcl_has_run]} return + # Set the precision of FP arithmatic used by the interpreter. And # configure SQLite to take database file locks on the page that begins # 64KB into the database file instead of the one 1GB in. This means @@ -128,6 +132,7 @@ if {[info command sqlite_orig]==""} { set ::dbhandle [lindex $args 0] uplevel #0 $::G(perm:dbconfig) } + [lindex $args 0] cache size 3 set res } else { # This command is not opening a new database connection. Pass the @@ -172,8 +177,14 @@ proc get_pwd {} { # case of the result to what Tcl considers canonical, which would # defeat the purpose of this procedure. # + if {[info exists ::env(ComSpec)]} { + set comSpec $::env(ComSpec) + } else { + # NOTE: Hard-code the typical default value. + set comSpec {C:\Windows\system32\cmd.exe} + } return [string map [list \\ /] \ - [string trim [exec -- $::env(ComSpec) /c echo %CD%]]] + [string trim [exec -- $comSpec /c CD]]] } else { return [pwd] } @@ -299,66 +310,6 @@ proc do_delete_file {force args} { } } -if {$::tcl_platform(platform) eq "windows"} { - proc do_remove_win32_dir {args} { - set nRetry [getFileRetries] ;# Maximum number of retries. - set nDelay [getFileRetryDelay] ;# Delay in ms before retrying. - - foreach dirName $args { - # On windows, sometimes even a [remove_win32_dir] can fail just after - # a directory is emptied. The cause is usually "tag-alongs" - programs - # like anti-virus software, automatic backup tools and various explorer - # extensions that keep a file open a little longer than we expect, - # causing the delete to fail. - # - # The solution is to wait a short amount of time before retrying the - # removal. - # - if {$nRetry > 0} { - for {set i 0} {$i < $nRetry} {incr i} { - set rc [catch { - remove_win32_dir $dirName - } msg] - if {$rc == 0} break - if {$nDelay > 0} { after $nDelay } - } - if {$rc} { error $msg } - } else { - remove_win32_dir $dirName - } - } - } - - proc do_delete_win32_file {args} { - set nRetry [getFileRetries] ;# Maximum number of retries. - set nDelay [getFileRetryDelay] ;# Delay in ms before retrying. - - foreach fileName $args { - # On windows, sometimes even a [delete_win32_file] can fail just after - # a file is closed. The cause is usually "tag-alongs" - programs like - # anti-virus software, automatic backup tools and various explorer - # extensions that keep a file open a little longer than we expect, - # causing the delete to fail. - # - # The solution is to wait a short amount of time before retrying the - # delete. - # - if {$nRetry > 0} { - for {set i 0} {$i < $nRetry} {incr i} { - set rc [catch { - delete_win32_file $fileName - } msg] - if {$rc == 0} break - if {$nDelay > 0} { after $nDelay } - } - if {$rc} { error $msg } - } else { - delete_win32_file $fileName - } - } - } -} - proc execpresql {handle args} { trace remove execution $handle enter [list execpresql $handle] if {[info exists ::G(perm:presql)]} { @@ -373,6 +324,29 @@ proc do_not_use_codec {} { set ::do_not_use_codec 1 reset_db } +unset -nocomplain do_not_use_codec + +# Return true if the "reserved_bytes" integer on database files is non-zero. +# +proc nonzero_reserved_bytes {} { + return [sqlite3 -has-codec] +} + +# Print a HELP message and exit +# +proc print_help_and_quit {} { + puts {Options: + --pause Wait for user input before continuing + --soft-heap-limit=N Set the soft-heap-limit to N + --hard-heap-limit=N Set the hard-heap-limit to N + --maxerror=N Quit after N errors + --verbose=(0|1) Control the amount of output. Default '1' + --output=FILE set --verbose=2 and output to FILE. Implies -q + -q Shorthand for --verbose=0 + --help This message +} + exit 1 +} # The following block only runs the first time this file is sourced. It # does not run in slave interpreters (since the ::cmdlinearg array is @@ -385,6 +359,7 @@ if {[info exists cmdlinearg]==0} { # # --pause # --soft-heap-limit=NN + # --hard-heap-limit=NN # --maxerror=NN # --malloctrace=N # --backtrace=N @@ -396,9 +371,12 @@ if {[info exists cmdlinearg]==0} { # --match=$pattern # --verbose=$val # --output=$filename + # -q Reduce output + # --testdir=$dir Run tests in subdirectory $dir # --help # set cmdlinearg(soft-heap-limit) 0 + set cmdlinearg(hard-heap-limit) 0 set cmdlinearg(maxerror) 1000 set cmdlinearg(malloctrace) 0 set cmdlinearg(backtrace) 10 @@ -410,6 +388,7 @@ if {[info exists cmdlinearg]==0} { set cmdlinearg(match) "" set cmdlinearg(verbose) "" set cmdlinearg(output) "" + set cmdlinearg(testdir) "testdir" set leftover [list] foreach a $argv { @@ -424,21 +403,30 @@ if {[info exists cmdlinearg]==0} { {^-+soft-heap-limit=.+$} { foreach {dummy cmdlinearg(soft-heap-limit)} [split $a =] break } + {^-+hard-heap-limit=.+$} { + foreach {dummy cmdlinearg(hard-heap-limit)} [split $a =] break + } {^-+maxerror=.+$} { foreach {dummy cmdlinearg(maxerror)} [split $a =] break } {^-+malloctrace=.+$} { foreach {dummy cmdlinearg(malloctrace)} [split $a =] break if {$cmdlinearg(malloctrace)} { + if {0==$::sqlite_options(memdebug)} { + set err "Error: --malloctrace=1 requires an SQLITE_MEMDEBUG build" + puts stderr $err + exit 1 + } sqlite3_memdebug_log start } } {^-+backtrace=.+$} { foreach {dummy cmdlinearg(backtrace)} [split $a =] break - sqlite3_memdebug_backtrace $value + sqlite3_memdebug_backtrace $cmdlinearg(backtrace) } {^-+binarylog=.+$} { foreach {dummy cmdlinearg(binarylog)} [split $a =] break + set cmdlinearg(binarylog) [file normalize $cmdlinearg(binarylog)] } {^-+soak=.+$} { foreach {dummy cmdlinearg(soak)} [split $a =] break @@ -471,6 +459,7 @@ if {[info exists cmdlinearg]==0} { {^-+output=.+$} { foreach {dummy cmdlinearg(output)} [split $a =] break + set cmdlinearg(output) [file normalize $cmdlinearg(output)] if {$cmdlinearg(verbose)==""} { set cmdlinearg(verbose) 2 } @@ -483,12 +472,35 @@ if {[info exists cmdlinearg]==0} { error "option --verbose= must be set to a boolean or to \"file\"" } } + {^-+testdir=.*$} { + foreach {dummy cmdlinearg(testdir)} [split $a =] break + } + {.*help.*} { + print_help_and_quit + } + {^-q$} { + set cmdlinearg(output) test-out.txt + set cmdlinearg(verbose) 2 + } default { - lappend leftover $a + if {[file tail $a]==$a} { + lappend leftover $a + } else { + lappend leftover [file normalize $a] + } } } } + unset -nocomplain a + set testdir [file normalize $testdir] + set cmdlinearg(TESTFIXTURE_HOME) [file dirname [info nameofexec]] + set cmdlinearg(INFO_SCRIPT) [file normalize [info script]] + set argv0 [file normalize $argv0] + if {$cmdlinearg(testdir)!=""} { + file mkdir $cmdlinearg(testdir) + cd $cmdlinearg(testdir) + } set argv $leftover # Install the malloc layer used to inject OOM errors. And the 'automatic' @@ -521,13 +533,18 @@ if {[info exists cmdlinearg]==0} { if {$cmdlinearg(verbose)==""} { set cmdlinearg(verbose) 1 } + + if {[info commands vdbe_coverage]!=""} { + vdbe_coverage start + } } # Update the soft-heap-limit each time this script is run. In that # way if an individual test file changes the soft-heap-limit, it # will be reset at the start of the next test file. # -sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit) +sqlite3_soft_heap_limit64 $cmdlinearg(soft-heap-limit) +sqlite3_hard_heap_limit64 $cmdlinearg(hard-heap-limit) # Create a test database # @@ -672,6 +689,17 @@ proc puts {args} { uplevel puts_override $args } # Invoke the do_test procedure to run a single test # +# The $expected parameter is the expected result. The result is the return +# value from the last TCL command in $cmd. +# +# Normally, $expected must match exactly. But if $expected is of the form +# "/regexp/" then regular expression matching is used. If $expected is +# "~/regexp/" then the regular expression must NOT match. If $expected is +# of the form "#/value-list/" then each term in value-list must be numeric +# and must approximately match the corresponding numeric term in $result. +# Values must match within 10%. Or if the $expected term is A..B then the +# $result term must be in between A and B. +# proc do_test {name cmd expected} { global argv cmdlinearg @@ -705,7 +733,10 @@ proc do_test {name cmd expected} { output2 "\nError: $result" fail_test $name } else { - if {[regexp {^~?/.*/$} $expected]} { + if {[permutation]=="maindbname"} { + set result [string map [list [string tolower ICECUBE] main] $result] + } + if {[regexp {^[~#]?/.*/$} $expected]} { # "expected" is of the form "/PATTERN/" then the result if correct if # regular expression PATTERN matches the result. "~/PATTERN/" means # the regular expression must not match. @@ -719,6 +750,21 @@ proc do_test {name cmd expected} { set ok [regexp $re $result] } set ok [expr {!$ok}] + } elseif {[string index $expected 0]=="#"} { + # Numeric range value comparison. Each term of the $result is matched + # against one term of $expect. Both $result and $expected terms must be + # numeric. The values must match within 10%. Or if $expected is of the + # form A..B then the $result term must be between A and B. + set e2 [string range $expected 2 end-1] + foreach i $result j $e2 { + if {[regexp {^(-?\d+)\.\.(-?\d)$} $j all A B]} { + set ok [expr {$i+0>=$A && $i+0<=$B}] + } else { + set ok [expr {$i+0>=0.9*$j && $i+0<=1.1*$j}] + } + if {!$ok} break + } + if {$ok && [llength $result]!=[llength $e2]} {set ok 0} } else { set re [string range $expected 1 end-1] if {[string index $re 0]=="*"} { @@ -741,6 +787,9 @@ proc do_test {name cmd expected} { } } else { set ok [expr {[string compare $result $expected]==0}] + if {!$ok} { + set ok [fpnum_compare $result $expected] + } } if {!$ok} { # if {![info exists ::testprefix] || $::testprefix eq ""} { @@ -760,6 +809,15 @@ proc do_test {name cmd expected} { flush stdout } +# Like do_test except the test is not run in a slave interpreter +# on Windows because of issues with ANSI and UTF8 I/O on Win11. +# +proc do_test_with_ansi_output {name cmd expected} { + if {![info exists ::SLAVE] || $::tcl_platform(platform) ne "windows"} { + uplevel 1 [list do_test $name $cmd $expected] + } +} + proc dumpbytes {s} { set r "" for {set i 0} {$i < [string length $s]} {incr i} { @@ -778,11 +836,20 @@ proc catchcmd {db {cmd ""}} { set rc [catch { eval $line } msg] list $rc $msg } +proc catchsafecmd {db {cmd ""}} { + global CLI + set out [open cmds.txt w] + puts $out $cmd + close $out + set line "exec $CLI -safe $db < cmds.txt" + set rc [catch { eval $line } msg] + list $rc $msg +} proc catchcmdex {db {cmd ""}} { global CLI set out [open cmds.txt w] - fconfigure $out -encoding binary -translation binary + fconfigure $out -translation binary puts -nonewline $out $cmd close $out set line "exec -keepnewline -- $CLI $db < cmds.txt" @@ -790,7 +857,7 @@ proc catchcmdex {db {cmd ""}} { foreach chan $chans { catch { set modes($chan) [fconfigure $chan] - fconfigure $chan -encoding binary -translation binary -buffering none + fconfigure $chan -translation binary -buffering none } } set rc [catch { eval $line } msg] @@ -805,9 +872,9 @@ proc catchcmdex {db {cmd ""}} { proc filepath_normalize {p} { # test cases should be written to assume "unix"-like file paths - if {$::tcl_platform(platform)!="unix"} { - # lreverse*2 as a hack to remove any unneeded {} after the string map - lreverse [lreverse [string map {\\ /} [regsub -nocase -all {[a-z]:[/\\]+} $p {/}]]] + if {$::tcl_platform(platform) ne "unix"} { + string map [list \\ / \{/ / .db\} .db] \ + [regsub -nocase -all {[a-z]:[/\\]+} $p {/}] } { set p } @@ -837,10 +904,72 @@ proc fix_testname {varname} { } } -proc do_execsql_test {testname sql {result {}}} { +proc normalize_list {L} { + set L2 [list] + foreach l $L {lappend L2 $l} + set L2 +} + +# Run SQL and verify that the number of "vmsteps" required is greater +# than or less than some constant. +# +proc do_vmstep_test {tn sql nstep {res {}}} { + uplevel [list do_execsql_test $tn.0 $sql $res] + + set vmstep [db status vmstep] + if {[string range $nstep 0 0]=="+"} { + set body "if {$vmstep<$nstep} { + error \"got $vmstep, expected more than [string range $nstep 1 end]\" + }" + } else { + set body "if {$vmstep>$nstep} { + error \"got $vmstep, expected less than $nstep\" + }" + } + + # set name "$tn.vmstep=$vmstep,expect=$nstep" + set name "$tn.1" + uplevel [list do_test $name $body {}] +} + + +# Either: +# +# do_execsql_test TESTNAME SQL ?RES? +# do_execsql_test -db DB TESTNAME SQL ?RES? +# +proc do_execsql_test {args} { + set db db + if {[lindex $args 0]=="-db"} { + set db [lindex $args 1] + set args [lrange $args 2 end] + } + + if {[llength $args]==2} { + foreach {testname sql} $args {} + set result "" + } elseif {[llength $args]==3} { + foreach {testname sql result} $args {} + + # With some versions of Tcl on windows, if $result is all whitespace but + # contains some CR/LF characters, the [list {*}$result] below returns a + # copy of $result instead of a zero length string. Not clear exactly why + # this is. The following is a workaround. + if {[llength $result]==0} { set result "" } + } else { + error [string trim { + wrong # args: should be "do_execsql_test ?-db DB? testname sql ?result?" + }] + } + fix_testname testname - uplevel do_test [list $testname] [list "execsql {$sql}"] [list [list {*}$result]] + + uplevel do_test \ + [list $testname] \ + [list "execsql {$sql} $db"] \ + [list [list {*}$result]] } + proc do_catchsql_test {testname sql result} { fix_testname testname uplevel do_test [list $testname] [list "catchsql {$sql}"] [list $result] @@ -850,10 +979,116 @@ proc do_timed_execsql_test {testname sql {result {}}} { uplevel do_test [list $testname] [list "execsql_timed {$sql}"]\ [list [list {*}$result]] } + +# Run an EXPLAIN QUERY PLAN $sql in database "db". Then rewrite the output +# as an ASCII-art graph and return a string that is that graph. +# +# Hexadecimal literals in the output text are converted into "xxxxxx" since those +# literals are pointer values that might very from one run of the test to the +# next, yet we want the output to be consistent. +# +proc query_plan_graph {sql} { + db eval "EXPLAIN QUERY PLAN $sql" { + set dx($id) $detail + lappend cx($parent) $id + } + set a "\n QUERY PLAN\n" + append a [append_graph " " dx cx 0] + regsub -all {SUBQUERY 0x[A-F0-9]+\y} $a {SUBQUERY xxxxxx} a + regsub -all {(MATERIALIZE|CO-ROUTINE|SUBQUERY) \d+\y} $a {\1 xxxxxx} a + regsub -all {\((join|subquery)-\d+\)} $a {(\1-xxxxxx)} a + return $a +} + +# Helper routine for [query_plan_graph SQL]: +# +# Output rows of the graph that are children of $level. +# +# prefix: Prepend to every output line +# +# dxname: Name of an array variable that stores text describe +# The description for $id is $dx($id) +# +# cxname: Name of an array variable holding children of item. +# Children of $id are $cx($id) +# +# level: Render all lines that are children of $level +# +proc append_graph {prefix dxname cxname level} { + upvar $dxname dx $cxname cx + set a "" + set x $cx($level) + set n [llength $x] + for {set i 0} {$i<$n} {incr i} { + set id [lindex $x $i] + if {$i==$n-1} { + set p1 "`--" + set p2 " " + } else { + set p1 "|--" + set p2 "| " + } + append a $prefix$p1$dx($id)\n + if {[info exists cx($id)]} { + append a [append_graph "$prefix$p2" dx cx $id] + } + } + return $a +} + +# Do an EXPLAIN QUERY PLAN test on input $sql with expected results $res +# +# If $res begins with a "\s+QUERY PLAN\n" then it is assumed to be the +# complete graph which must match the output of [query_plan_graph $sql] +# exactly. +# +# If $res does not begin with "\s+QUERY PLAN\n" then take it is a string +# that must be found somewhere in the query plan output. +# proc do_eqp_test {name sql res} { - uplevel do_execsql_test $name [list "EXPLAIN QUERY PLAN $sql"] [list $res] + if {[regexp {^\s+QUERY PLAN\n} $res]} { + + set query_plan [query_plan_graph $sql] + + if {[list {*}$query_plan]==[list {*}$res]} { + uplevel [list do_test $name [list set {} ok] ok] + } else { + uplevel [list \ + do_test $name [list query_plan_graph $sql] $res + ] + } + } else { + if {[string index $res 0]!="/"} { + set res "/*$res*/" + } + uplevel do_execsql_test $name [list "EXPLAIN QUERY PLAN $sql"] [list $res] + } } +# Do both an eqp_test and an execsql_test on the same SQL. +# +proc do_eqp_execsql_test {name sql res1 res2} { + if {[regexp {^\s+QUERY PLAN\n} $res1]} { + + set query_plan [query_plan_graph $sql] + + if {[list {*}$query_plan]==[list {*}$res1]} { + uplevel [list do_test ${name}a [list set {} ok] ok] + } else { + uplevel [list \ + do_test ${name}a [list query_plan_graph $sql] $res1 + ] + } + } else { + if {[string index $res 0]!="/"} { + set res1 "/*$res1*/" + } + uplevel do_execsql_test ${name}a [list "EXPLAIN QUERY PLAN $sql"] [list $res1] + } + uplevel do_execsql_test ${name}b [list $sql] [list $res2] +} + + #------------------------------------------------------------------------- # Usage: do_select_tests PREFIX ?SWITCHES? TESTLIST # @@ -986,13 +1221,36 @@ proc speed_trial_summary {name} { } } -# Run this routine last +# Clear out left-over configuration setup from the end of a test # -proc finish_test {} { - catch {db close} +proc finish_test_precleanup {} { catch {db1 close} catch {db2 close} catch {db3 close} + catch {unregister_devsim} + catch {unregister_jt_vfs} + catch {unregister_demovfs} +} + +# Run this routine last +# +proc finish_test {} { + global argv + finish_test_precleanup + if {[llength $argv]>0} { + # If additional test scripts are specified on the command-line, + # run them also, before quitting. + proc finish_test {} { + finish_test_precleanup + return + } + foreach extra $argv { + puts "Running \"$extra\"" + db_delete_and_reopen + uplevel #0 source $extra + } + } + catch {db close} if {0==[info exists ::SLAVE]} { finalize_testing } } proc finalize_testing {} { @@ -1010,7 +1268,8 @@ proc finalize_testing {} { db close sqlite3_reset_auto_extension - sqlite3_soft_heap_limit 0 + sqlite3_soft_heap_limit64 0 + sqlite3_hard_heap_limit64 0 set nTest [incr_ntest] set nErr [set_test_counter errors] @@ -1029,10 +1288,15 @@ proc finalize_testing {} { out of $nTest tests" } else { set cpuinfo {} - if {[catch {exec hostname} hname]==0} {set cpuinfo [string trim $hname]} + if {[catch {exec hostname} hname]==0} { + regsub {\.local$} $hname {} hname + set cpuinfo [string trim $hname] + } append cpuinfo " $::tcl_platform(os)" append cpuinfo " [expr {$::tcl_platform(pointerSize)*8}]-bit" - append cpuinfo " [string map {E -e} $::tcl_platform(byteOrder)]" + if {[string match big* $::tcl_platform(byteOrder)]} { + append cpuinfo " [string map {E -e} $::tcl_platform(byteOrder)]" + } output2 "SQLite [sqlite3 -sourceid]" output2 "$nErr errors out of $nTest tests on $cpuinfo" } @@ -1067,22 +1331,30 @@ proc finalize_testing {} { if {$::cmdlinearg(binarylog)} { vfslog finalize binarylog } - if {$sqlite_open_file_count} { - output2 "$sqlite_open_file_count files were left open" - incr nErr + if {[info exists ::run_thread_tests_called]==0} { + if {$sqlite_open_file_count} { + output2 "$sqlite_open_file_count files were left open" + incr nErr + } } + # BEGIN SQLCIPHER + # prior to calculating malloc stats, call sqlite3_shutdown to invoke + # sqlcipher_extra_shutdown() to release private heap memory if all + # private allocations have been freed + sqlite3_shutdown + # END SQLCIPHER if {[lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1]>0 || [sqlite3_memory_used]>0} { output2 "Unfreed memory: [sqlite3_memory_used] bytes in\ [lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1] allocations" incr nErr - ifcapable memdebug||mem5||(mem3&&debug) { + ifcapable mem5||(mem3&&debug) { output2 "Writing unfreed memory log to \"./memleak.txt\"" sqlite3_memdebug_dump ./memleak.txt } } else { output2 "All memory allocations freed - no leaks" - ifcapable memdebug||mem5 { + ifcapable mem5 { sqlite3_memdebug_dump ./memusage.txt } } @@ -1093,17 +1365,19 @@ proc finalize_testing {} { output2 "Number of malloc() : [sqlite3_memdebug_malloc_count] calls" } if {$::cmdlinearg(malloctrace)} { - output2 "Writing mallocs.sql..." - memdebug_log_sql + output2 "Writing mallocs.tcl..." + memdebug_log_sql mallocs.tcl sqlite3_memdebug_log stop sqlite3_memdebug_log clear - if {[sqlite3_memory_used]>0} { - output2 "Writing leaks.sql..." + output2 "Writing leaks.tcl..." sqlite3_memdebug_log sync - memdebug_log_sql leaks.sql + memdebug_log_sql leaks.tcl } } + if {[info commands vdbe_coverage]!=""} { + vdbe_coverage_report + } foreach f [glob -nocomplain test.db-*-journal] { forcedelete $f } @@ -1113,6 +1387,39 @@ proc finalize_testing {} { exit [expr {$nErr>0}] } +proc vdbe_coverage_report {} { + puts "Writing vdbe coverage report to vdbe_coverage.txt" + set lSrc [list] + set iLine 0 + if {[file exists ../sqlite3.c]} { + set fd [open ../sqlite3.c] + set iLine + while { ![eof $fd] } { + set line [gets $fd] + incr iLine + if {[regexp {^/\** Begin file (.*\.c) \**/} $line -> file]} { + lappend lSrc [list $iLine $file] + } + } + close $fd + } + set fd [open vdbe_coverage.txt w] + foreach miss [vdbe_coverage report] { + foreach {line branch never} $miss {} + set nextfile "" + while {[llength $lSrc]>0 && [lindex $lSrc 0 0] < $line} { + set nextfile [lindex $lSrc 0 1] + set lSrc [lrange $lSrc 1 end] + } + if {$nextfile != ""} { + puts $fd "" + puts $fd "### $nextfile ###" + } + puts $fd "Vdbe branch $line: never $never (path $branch)" + } + close $fd +} + # Display memory statistics for analysis and debugging purposes. # proc show_memstats {} { @@ -1132,14 +1439,6 @@ proc show_memstats {} { set x [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0] set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]] output1 "Page-cache overflow: $val" - set x [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0] - set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]] - output1 "Scratch memory used: $val" - set x [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0] - set y [sqlite3_status SQLITE_STATUS_SCRATCH_SIZE 0] - set val [format {now %10d max %10d max-size %10d} \ - [lindex $x 1] [lindex $x 2] [lindex $y 2]] - output1 "Scratch overflow: $val" ifcapable yytrackmaxstackdepth { set x [sqlite3_status SQLITE_STATUS_PARSER_STACK 0] set val [format { max %10d} [lindex $x 2]] @@ -1208,9 +1507,9 @@ proc explain_i {sql {db db}} { set D "" } foreach opcode { - Seek SeekGe SeekGt SeekLe SeekLt NotFound Last Rewind + Seek SeekGE SeekGT SeekLE SeekLT NotFound Last Rewind NoConflict Next Prev VNext VPrev VFilter - SorterSort SorterNext + SorterSort SorterNext NextIfOpen } { set color($opcode) $B } @@ -1231,9 +1530,15 @@ proc explain_i {sql {db db}} { set bSeenGoto 1 } + if {$opcode=="Once"} { + for {set i $addr} {$i<$p2} {incr i} { + set star($i) $addr + } + } + if {$opcode=="Next" || $opcode=="Prev" || $opcode=="VNext" || $opcode=="VPrev" - || $opcode=="SorterNext" + || $opcode=="SorterNext" || $opcode=="NextIfOpen" } { for {set i $p2} {$i<$addr} {incr i} { incr x($i) 2 @@ -1257,6 +1562,12 @@ proc explain_i {sql {db db}} { } set I [string repeat " " $x($addr)] + if {[info exists star($addr)]} { + set ii [expr $x($star($addr))] + append I " " + set I [string replace $I $ii $ii *] + } + set col "" catch { set col $color($opcode) } @@ -1267,6 +1578,47 @@ proc explain_i {sql {db db}} { output2 "---- ------------ ------ ------ ------ ---------------- -- -" } +proc execsql_pp {sql {db db}} { + set nCol 0 + $db eval $sql A { + if {$nCol==0} { + set nCol [llength $A(*)] + foreach c $A(*) { + set aWidth($c) [string length $c] + lappend data $c + } + } + foreach c $A(*) { + set n [string length $A($c)] + if {$n > $aWidth($c)} { + set aWidth($c) $n + } + lappend data $A($c) + } + } + if {$nCol>0} { + set nTotal 0 + foreach e [array names aWidth] { incr nTotal $aWidth($e) } + incr nTotal [expr ($nCol-1) * 3] + incr nTotal 4 + + set fmt "" + foreach c $A(*) { + lappend fmt "% -$aWidth($c)s" + } + set fmt "| [join $fmt { | }] |" + + puts [string repeat - $nTotal] + for {set i 0} {$i < [llength $data]} {incr i $nCol} { + set vals [lrange $data $i [expr $i+$nCol-1]] + puts [format $fmt {*}$vals] + if {$i==0} { puts [string repeat - $nTotal] } + } + puts [string repeat - $nTotal] + } +} + + # Show the VDBE program for an SQL statement but omit the Trace # opcode at the beginning. This procedure can be used to prove # that different SQL statements generate exactly the same VDBE code. @@ -1387,7 +1739,7 @@ proc ifcapable {expr code {else ""} {elsecode ""}} { return -code $c $r } -# This proc execs a seperate process that crashes midway through executing +# This proc execs a separate process that crashes midway through executing # the SQL script $sql on database test.db. # # The crash occurs during a sync() of file $crashfile. When the crash @@ -1412,6 +1764,7 @@ proc crashsql {args} { set tclbody {} set crashfile "" set dc "" + set dfltvfs 0 set sql [lindex $args end] for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} { @@ -1425,7 +1778,8 @@ proc crashsql {args} { elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \ elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \ elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \ - elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \ + elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" }\ + elseif {$n>1 && [string first $z -dfltvfs]==0} {set dfltvfs $z2 }\ else { error "Unrecognized option: $z" } } @@ -1437,11 +1791,19 @@ proc crashsql {args} { # cfSync(), which can be different then what TCL uses by # default, so here we force it to the "nativename" format. set cfile [string map {\\ \\\\} [file nativename [file join [get_pwd] $crashfile]]] + ifcapable winrt { + # Except on winrt. Winrt has no way to transform a relative path into + # an absolute one, so it just uses the relative paths. + set cfile $crashfile + } set f [open crash.tcl w] - puts $f "sqlite3_crash_enable 1" + puts $f "sqlite3_initialize ; sqlite3_shutdown" + puts $f "catch { install_malloc_faultsim 1 }" + puts $f "sqlite3_crash_enable 1 $dfltvfs" puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile" puts $f "sqlite3_test_control_pending_byte $::sqlite_pending_byte" + puts $f "autoinstall_test_functions" # This block sets the cache size of the main database to 10 # pages. This is done in case the build is configured to omit @@ -1467,6 +1829,57 @@ proc crashsql {args} { puts $f "$sql" puts $f "}" } + close $f + set r [catch { + exec [info nameofexec] crash.tcl >@stdout 2>@stdout + } msg] + + # Windows/ActiveState TCL returns a slightly different + # error message. We map that to the expected message + # so that we don't have to change all of the test + # cases. + if {$::tcl_platform(platform) eq "windows"} { + if {$msg=="child killed: unknown signal"} { + set msg "child process exited abnormally" + } + } + if {$r && [string match {*ERROR: LeakSanitizer*} $msg]} { + set msg "child process exited abnormally" + } + + lappend r $msg +} + +# crash_on_write ?-devchar DEVCHAR? CRASHDELAY SQL +# +proc crash_on_write {args} { + + set nArg [llength $args] + if {$nArg<2 || $nArg%2} { + error "bad args: $args" + } + set zSql [lindex $args end] + set nDelay [lindex $args end-1] + + set devchar {} + for {set ii 0} {$ii < $nArg-2} {incr ii 2} { + set opt [lindex $args $ii] + switch -- [lindex $args $ii] { + -devchar { + set devchar [lindex $args [expr $ii+1]] + } + + default { error "unrecognized option: $opt" } + } + } + + set f [open crash.tcl w] + puts $f "sqlite3_crash_on_write $nDelay" + puts $f "sqlite3_test_control_pending_byte $::sqlite_pending_byte" + puts $f "sqlite3 db test.db -vfs writecrash" + puts $f "db eval {$zSql}" + puts $f "set {} {}" + close $f set r [catch { exec [info nameofexec] crash.tcl >@stdout @@ -1476,7 +1889,7 @@ proc crashsql {args} { # error message. We map that to the expected message # so that we don't have to change all of the test # cases. - if {$::tcl_platform(platform)=="windows"} { + if {$::tcl_platform(platform) eq "windows"} { if {$msg=="child killed: unknown signal"} { set msg "child process exited abnormally" } @@ -1594,21 +2007,23 @@ proc do_ioerr_test {testname args} { set ::sqlite_io_error_hardhit 0 set r [catch $::ioerrorbody msg] set ::errseen $r - set rc [sqlite3_errcode $::DB] - if {$::ioerropts(-erc)} { - # If we are in extended result code mode, make sure all of the - # IOERRs we get back really do have their extended code values. - # If an extended result code is returned, the sqlite3_errcode - # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn - # where nnnn is a number - if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} { - return $rc - } - } else { - # If we are not in extended result code mode, make sure no - # extended error codes are returned. - if {[regexp {\+\d} $rc]} { - return $rc + if {[info commands db]!=""} { + set rc [sqlite3_errcode db] + if {$::ioerropts(-erc)} { + # If we are in extended result code mode, make sure all of the + # IOERRs we get back really do have their extended code values. + # If an extended result code is returned, the sqlite3_errcode + # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn + # where nnnn is a number + if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} { + return $rc + } + } else { + # If we are not in extended result code mode, make sure no + # extended error codes are returned. + if {[regexp {\+\d} $rc]} { + return $rc + } } } # The test repeats as long as $::go is non-zero. $::go starts out @@ -1783,7 +2198,7 @@ proc dbcksum {db dbname} { return [md5 $txt] } -proc memdebug_log_sql {{filename mallocs.sql}} { +proc memdebug_log_sql {filename} { set data [sqlite3_memdebug_log dump] set nFrame [expr [llength [lindex $data 0]]-2] @@ -1808,9 +2223,11 @@ proc memdebug_log_sql {{filename mallocs.sql}} { set tbl2 "CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n" set tbl3 "CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n" + set pid [pid] + foreach f [array names frames] { set addr [format %x $f] - set cmd "addr2line -e [info nameofexec] $addr" + set cmd "eu-addr2line --pid=$pid $addr" set line [eval exec $cmd] append sql "INSERT INTO ${database}.frame VALUES($f, '$line');\n" @@ -1829,8 +2246,18 @@ proc memdebug_log_sql {{filename mallocs.sql}} { append sql "INSERT INTO ${database}.file VALUES('$f', '$contents');\n" } + set escaped "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;" + set escaped [string map [list "{" "\\{" "}" "\\}" "\\" "\\\\"] $escaped] + set fd [open $filename w] - puts $fd "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;" + puts $fd "set BUILTIN {" + puts $fd $escaped + puts $fd "}" + puts $fd {set BUILTIN [string map [list "\\{" "{" "\\}" "}" "\\\\" "\\"] $BUILTIN]} + set mtv [open $::testdir/malloctraceviewer.tcl] + set txt [read $mtv] + close $mtv + puts $fd $txt close $fd } @@ -1858,6 +2285,16 @@ proc drop_all_tables {{db db}} { } } +# Drop all auxiliary indexes from the main database opened by handle [db]. +# +proc drop_all_indexes {{db db}} { + set L [$db eval { + SELECT name FROM sqlite_master WHERE type='index' AND sql LIKE 'create%' + }] + foreach idx $L { $db eval "DROP INDEX $idx" } +} + + #------------------------------------------------------------------------- # If a test script is executed with global variable $::G(perm:name) set to # "wal", then the tests are run in WAL mode. Otherwise, they should be run @@ -1894,6 +2331,12 @@ proc wal_check_journal_mode {testname {db db}} { } } +proc wal_is_capable {} { + ifcapable !wal { return 0 } + if {[permutation]=="journaltest"} { return 0 } + return 1 +} + proc permutation {} { set perm "" catch {set perm $::G(perm:name)} @@ -2085,13 +2528,95 @@ proc test_restore_config_pagecache {} { catch {db3 close} sqlite3_shutdown - eval sqlite3_config_pagecache $::old_pagecache_config - unset ::old_pagecache_config + if {[info exists ::old_pagecache_config]} { + eval sqlite3_config_pagecache $::old_pagecache_config + unset ::old_pagecache_config + } sqlite3_initialize autoinstall_test_functions sqlite3 db test.db } +proc test_binary_name {nm} { + if {$::tcl_platform(platform) eq "windows"} { + set ret "$nm.exe" + } else { + set ret $nm + } + file normalize [file join $::cmdlinearg(TESTFIXTURE_HOME) $ret] +} + +proc test_find_binary {nm} { + set ret [test_binary_name $nm] + if {![file executable $ret]} { + finish_test + return "" + } + return $ret +} + +# Find the name of the 'shell' executable (e.g. "sqlite3.exe") to use for +# the tests in shell*.test. If no such executable can be found, invoke +# [finish_test ; return] in the callers context. +# +proc test_find_cli {} { + set prog [test_find_binary sqlite3] + if {$prog==""} { return -code return } + return $prog +} + +# Find invocation of the 'shell' executable (e.g. "sqlite3.exe") to use +# for the tests in shell*.test with optional valgrind prefix when the +# environment variable SQLITE_CLI_VALGRIND_OPT is set. The set value +# operates as follows: +# empty or 0 => no valgrind prefix; +# 1 => valgrind options for memory leak check; +# other => use value as valgrind options. +# If shell not found, invoke [finish_test ; return] in callers context. +# +proc test_cli_invocation {} { + set prog [test_find_binary sqlite3] + if {$prog==""} { return -code return } + set vgrun [expr {[permutation]=="valgrind"}] + if {$vgrun || [info exists ::env(SQLITE_CLI_VALGRIND_OPT)]} { + if {$vgrun} { + set vgo "--quiet" + } else { + set vgo $::env(SQLITE_CLI_VALGRIND_OPT) + } + if {$vgo == 0 || $vgo eq ""} { + return $prog + } elseif {$vgo == 1} { + return "valgrind --quiet --leak-check=yes $prog" + } else { + return "valgrind $vgo $prog" + } + } else { + return $prog + } +} + +# Find the name of the 'sqldiff' executable (e.g. "sqlite3.exe") to use for +# the tests in sqldiff tests. If no such executable can be found, invoke +# [finish_test ; return] in the callers context. +# +proc test_find_sqldiff {} { + set prog [test_find_binary sqldiff] + if {$prog==""} { return -code return } + return $prog +} + +# Call sqlite3_expanded_sql() on all statements associated with database +# connection $db. This sometimes finds use-after-free bugs if run with +# valgrind or address-sanitizer. +proc expand_all_sql {db} { + set stmt "" + while {[set stmt [sqlite3_next_stmt $db $stmt]]!=""} { + sqlite3_expanded_sql $stmt + } +} + + # If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set # to non-zero, then set the global variable $AUTOVACUUM to 1. set AUTOVACUUM $sqlite_options(default_autovacuum) @@ -2104,6 +2629,9 @@ set sqlite_fts3_enable_parentheses 0 # this setting by invoking "database_can_be_corrupt" # database_never_corrupt +extra_schema_checks 1 source $testdir/thread_common.tcl source $testdir/malloc_common.tcl + +set tester_tcl_has_run 1 diff --git a/test/testloadext.c b/test/testloadext.c new file mode 100644 index 0000000000..94cd312e44 --- /dev/null +++ b/test/testloadext.c @@ -0,0 +1,98 @@ +/* +** 2025-10-14 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +** Test the ability of run-time extension loading to use the +** very latest interfaces. +** +** Compile something like this: +** +** Linux: gcc -g -fPIC shared testloadext.c -o testloadext.so +** +** Mac: cc -g -fPIC -dynamiclib testloadext.c -o testloadext.dylib +** +** Win11: cl testloadext.c -link -dll -out:testloadext.dll +*/ +#include "sqlite3ext.h" +SQLITE_EXTENSION_INIT1 +#include <assert.h> +#include <string.h> + +/* +** Implementation of the set_errmsg(CODE,MSG) SQL function. +** +** Raise an error that has numeric code CODE and text message MSG +** using the sqlite3_set_errmsg() API. +*/ +static void seterrmsgfunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + sqlite3 *db; + char *zRes; + int rc; + assert( argc==2 ); + db = sqlite3_context_db_handle(context); + rc = sqlite3_set_errmsg(db, + sqlite3_value_int(argv[0]), + sqlite3_value_text(argv[1])); + zRes = sqlite3_mprintf("%d %d %s", + rc, sqlite3_errcode(db), sqlite3_errmsg(db)); + sqlite3_result_text64(context, zRes, strlen(zRes), + SQLITE_TRANSIENT, SQLITE_UTF8); + sqlite3_free(zRes); +} + +/* +** Implementation of the tempbuf_spill() SQL function. +** +** Return the value of SQLITE_DBSTATUS_TEMPBUF_SPILL. +*/ +static void tempbuf_spill_func( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + sqlite3 *db; + sqlite3_int64 iHi = 0, iCur = 0; + int rc; + int bReset; + assert( argc==1 ); + bReset = sqlite3_value_int(argv[0]); + db = sqlite3_context_db_handle(context); + (void)sqlite3_db_status64(db, SQLITE_DBSTATUS_TEMPBUF_SPILL, + &iCur, &iHi, bReset); + sqlite3_result_int64(context, iCur); +} + + +#ifdef _WIN32 +__declspec(dllexport) +#endif +int sqlite3_testloadext_init( + sqlite3 *db, + char **pzErrMsg, + const sqlite3_api_routines *pApi +){ + int rc = SQLITE_OK; + SQLITE_EXTENSION_INIT2(pApi); + (void)pzErrMsg; /* Unused parameter */ + rc = sqlite3_create_function(db, "set_errmsg", 2, + SQLITE_UTF8, + 0, seterrmsgfunc, 0, 0); + if( rc ) return rc; + rc = sqlite3_create_function(db, "tempbuf_spill", 1, + SQLITE_UTF8, + 0, tempbuf_spill_func, 0, 0); + if( rc ) return rc; + return SQLITE_OK; +} diff --git a/test/testrunner.tcl b/test/testrunner.tcl new file mode 100755 index 0000000000..5150363685 --- /dev/null +++ b/test/testrunner.tcl @@ -0,0 +1,1850 @@ +#!/bin/sh +# Script to runs tests for SQLite. Run with option "help" for more info. \ +exec tclsh "$0" "$@" + +set dir [pwd] +set testdir [file normalize [file dirname $argv0]] +set saved $argv +set argv [list] +source [file join $testdir testrunner_data.tcl] + +# Estimated amount of work required by displaytype, relative to 'tcl' +# +set estwork(tcl) 1 +set estwork(fuzz) 22 +set estwork(bld) 66 +set estwork(make) 102 + +set estworkfile [file join $testdir testrunner_estwork.tcl] +if {[file readable $estworkfile]} { + source $estworkfile +} +source [file join $testdir permutations.test] +set argv $saved +cd $dir + +# This script requires an interpreter that supports [package require sqlite3] +# to run. If this is not such an intepreter, see if there is a [testfixture] +# in the current directory. If so, run the command using it. If not, +# recommend that the user build one. +# +proc find_interpreter {} { + global dir + set interpreter [file tail [info nameofexec]] + set rc [catch { package require sqlite3 }] + if {$rc} { + if {[file readable pkgIndex.tcl] && [catch {source pkgIndex.tcl}]==0} { + set rc [catch { package require sqlite3 }] + } + } + if {$rc} { + if { [string match -nocase testfixture* $interpreter]==0 + && [file executable ./testfixture] + } { + puts "Failed to find tcl package sqlite3. Restarting with ./testfixture.." + set status [catch { + exec [trd_get_bin_name testfixture] [info script] {*}$::argv >@ stdout + } msg] + exit $status + } + } + if {$rc} { + puts "Cannot find tcl package sqlite3: Trying to build it now..." + if {$::tcl_platform(platform) eq "windows"} { + set bat [open make-tcl-extension.bat w] + puts $bat "nmake /f Makefile.msc tclextension" + close $bat + catch {exec -ignorestderr -- make-tcl-extension.bat} + } else { + catch {exec make tclextension} + } + if {[file readable pkgIndex.tcl] && [catch {source pkgIndex.tcl}]==0} { + set rc [catch { package require sqlite3 }] + } + if {$rc==0} { + puts "The SQLite tcl extension was successfully built and loaded." + puts "Run \"make tclextension-install\" to avoid having to rebuild\ + it in the future." + } else { + puts "Unable to build the SQLite tcl extension" + } + } + if {$rc} { + puts stderr "Cannot find a working instance of the SQLite tcl extension." + puts stderr "Run \"make tclextension\" or \"make testfixture\" and\ + try again..." + exit 1 + } +} +find_interpreter + +# Usually this script is run by [testfixture]. But it can also be run +# by a regular [tclsh]. For these cases, emulate the [clock_milliseconds] +# command. +if {[info commands clock_milliseconds]==""} { + proc clock_milliseconds {} { + clock milliseconds + } +} + +#------------------------------------------------------------------------- +# Usage: +# +proc usage {} { + set a0 [file tail $::argv0] + + puts [string trim [subst -nocommands { +Usage: + $a0 ?SWITCHES? ?PERMUTATION? ?PATTERNS? + $a0 PERMUTATION FILE + $a0 errors ?-v|--verbose? ?-s|--summary? ?PATTERN? + $a0 help + $a0 joblist ?PATTERN? + $a0 njob ?NJOB? + $a0 script ?-msvc? CONFIG + $a0 status ?-d SECS? ?--cls? + $a0 halt + $a0 estwork + + where SWITCHES are: + --buildonly Build test exes but do not run tests + --cases DISPLAYNAME Only run test that match DISPLAYNAME + --config CONFIGS Only use configs on comma-separate list CONFIGS + --dryrun Write what would have happened to testrunner.log + --explain Write summary to stdout + --fuzzdb FILENAME Additional external fuzzcheck database + --jobs NUM Run tests using NUM separate processes + --omit CONFIGS Omit configs on comma-separated list CONFIGS + --status Show the full "status" report while running + --stop-on-coredump Stop running if any test segfaults + --stop-on-error Stop running after any reported error + --zipvfs ZIPVFSDIR ZIPVFS source directory + +Special values for PERMUTATION that work with plain tclsh: + + list - show all allowed PERMUTATION arguments. + mdevtest - tests recommended prior to normal development check-ins. + release - full release test with various builds. + sdevtest - like mdevtest but using ASAN and UBSAN. + +Other PERMUTATION arguments must be run using testfixture, not tclsh: + + all - all tcl test scripts, plus a subset of test scripts rerun + with various permutations. + full - all tcl test scripts. + veryquick - a fast subset of the tcl test scripts. This is the default. + +If no PATTERN arguments are present, all tests specified by the PERMUTATION +are run. Otherwise, each pattern is interpreted as a glob pattern. Only +those tcl tests for which the final component of the filename matches at +least one specified pattern are run. The glob wildcard '*' is prepended +to the pattern if it does not start with '^' and appended to every +pattern that does not end with '$'. + +If no PATTERN arguments are present, then various fuzztest, threadtest +and other tests are run as part of the "release" permutation. These are +omitted if any PATTERN arguments are specified on the command line. + +If a PERMUTATION is specified and is followed by the path to a Tcl script +instead of a list of patterns, then that single Tcl test script is run +with the specified permutation. + +The "status" and "njob" commands are designed to be run from the same +directory as a running testrunner.tcl script that is running tests. The +"status" command prints a report describing the current state and progress +of the tests. Use the "-d N" option to have the status display clear the +screen and repeat every N seconds. The "njob" command may be used to query +or modify the number of sub-processes the test script uses to run tests. + +The "script" command outputs the script used to build a configuration. +Add the "-msvc" option for a Windows-compatible script. For a list of +available configurations enter "$a0 script help". + +The "errors" commands shows the output of tests that failed in the +most recent run. Complete output is shown if the -v or --verbose options +are used. Otherwise, an attempt is made to minimize the output to show +only the parts that contain the error messages. The --summary option just +shows the jobs that failed. If PATTERN are provided, the error information +is only provided for jobs that match PATTERN. + +Full documentation here: https://sqlite.org/src/doc/trunk/doc/testrunner.md + }]] + + exit 1 +} +#------------------------------------------------------------------------- + +#------------------------------------------------------------------------- +# Try to estimate a the number of processes to use. +# +# Command [guess_number_of_cores] attempts to glean the number of logical +# cores. Command [default_njob] returns the default value for the --jobs +# switch. +# +proc guess_number_of_cores {} { + if {[catch {number_of_cores} ret]} { + set ret 4 + if {$::tcl_platform(platform) eq "windows"} { + catch { set ret $::env(NUMBER_OF_PROCESSORS) } + } else { + if {$::tcl_platform(os)=="Darwin"} { + set cmd "sysctl -n hw.logicalcpu" + } else { + set cmd "nproc" + } + catch { + set fd [open "|$cmd" r] + set ret [gets $fd] + close $fd + set ret [expr $ret] + } + } + } + return $ret +} + +proc default_njob {} { + global env + if {[info exists env(NJOB)] && $env(NJOB)>=1} { + return $env(NJOB) + } + set nCore [guess_number_of_cores] + if {$nCore<=2} { + set nHelper 1 + } else { + set nHelper [expr int($nCore*0.5)] + } + return $nHelper +} +#------------------------------------------------------------------------- + +#------------------------------------------------------------------------- +# Setup various default values in the global TRG() array. +# +set TRG(dbname) [file normalize testrunner.db] +set TRG(logname) [file normalize testrunner.log] +set TRG(build.logname) [file normalize testrunner_build.log] +set TRG(info_script) [file normalize [info script]] +set TRG(timeout) 10000 ;# Default busy-timeout for testrunner.db +set TRG(nJob) [default_njob] ;# Default number of helper processes +set TRG(patternlist) [list] +set TRG(cmdline) $argv +set TRG(reporttime) 2000 +set TRG(fuzztest) 0 ;# is the fuzztest option present. +set TRG(zipvfs) "" ;# -zipvfs option, if any +set TRG(buildonly) 0 ;# True if --buildonly option +set TRG(config) {} ;# Only build the named configurations +set TRG(omitconfig) {} ;# Do not build these configurations +set TRG(dryrun) 0 ;# True if --dryrun option +set TRG(explain) 0 ;# True for the --explain option +set TRG(stopOnError) 0 ;# Stop running at first failure +set TRG(stopOnCore) 0 ;# Stop on a core-dump +set TRG(fullstatus) 0 ;# Full "status" report while running +set TRG(case) {} ;# Only run cases matching this GLOB pattern + +switch -nocase -glob -- $tcl_platform(os) { + *darwin* { + set TRG(platform) osx + set TRG(make) make.sh + set TRG(makecmd) "bash make.sh" + set TRG(testfixture) testfixture + set TRG(shell) sqlite3 + set TRG(run) run.sh + set TRG(runcmd) "bash run.sh" + } + *linux* - MSYS_NT* - MINGW64_NT* - MINGW32_NT* { + set TRG(platform) linux + set TRG(make) make.sh + set TRG(makecmd) "bash make.sh" + set TRG(testfixture) testfixture + set TRG(shell) sqlite3 + set TRG(run) run.sh + set TRG(runcmd) "bash run.sh" + } + *openbsd* { + set TRG(platform) linux + set TRG(make) make.sh + set TRG(makecmd) "sh make.sh" + set TRG(testfixture) testfixture + set TRG(shell) sqlite3 + set TRG(run) run.sh + set TRG(runcmd) "sh run.sh" + } + *win* { + set TRG(platform) win + set TRG(make) make.bat + set TRG(makecmd) "call make.bat" + set TRG(testfixture) testfixture.exe + set TRG(shell) sqlite3.exe + set TRG(run) run.bat + set TRG(runcmd) "run.bat" + if {"unix" eq $tcl_platform(platform)} { + # Presumably cygwin. This block gets testrunner.tcl started on + # Cygwin but then downstream tests all fail, at least in part + # because of the discrepancies in build target names which need + # .exe on cygwin but not on other Unix-like platforms. + set TRG(platform) cygwin + set TRG(make) make.sh + set TRG(makecmd) "bash make.sh" + set TRG(testfixture) testfixture + set TRG(shell) sqlite3 + set TRG(run) run.sh + set TRG(runcmd) "bash run.sh" + } + } + default { + puts "tcl_platform(os)=$::tcl_platform(os)" + error "cannot determine platform!" + } +} +#------------------------------------------------------------------------- + +#------------------------------------------------------------------------- +# The database schema used by the testrunner.db database. +# +set TRG(schema) { + DROP TABLE IF EXISTS jobs; + DROP TABLE IF EXISTS config; + + /* + ** This table contains one row for each job that testrunner.tcl must run + ** before the entire test run is finished. + ** + ** jobid: + ** Unique identifier for each job. Must be a +ve non-zero number. + ** + ** displaytype: + ** 3 or 4 letter mnemonic for the class of tests this belongs to e.g. + ** "fuzz", "tcl", "make" etc. + ** + ** displayname: + ** Name/description of job. For display purposes. + ** + ** build: + ** If the job requires a make.bat/make.sh make wrapper (i.e. to build + ** something), the name of the build configuration it uses. See + ** testrunner_data.tcl for a list of build configs. e.g. "Win32-MemDebug". + ** + ** dirname: + ** If the job should use a well-known directory name for its + ** sub-directory instead of an anonymous "testdir[1234...]" sub-dir + ** that is deleted after the job is finished. + ** + ** cmd: + ** Bash or batch script to run the job. + ** + ** depid: + ** The jobid value of a job that this job depends on. This job may not + ** be run before its depid job has finished successfully. + ** + ** priority: + ** Higher values run first. Sometimes. + */ + CREATE TABLE jobs( + /* Fields populated when db is initialized */ + jobid INTEGER PRIMARY KEY, -- id to identify job + displaytype TEXT NOT NULL, -- Type of test (for one line report) + displayname TEXT NOT NULL, -- Human readable job name + build TEXT NOT NULL DEFAULT '', -- make.sh/make.bat file request, if any + dirname TEXT NOT NULL DEFAULT '', -- directory name, if required + cmd TEXT NOT NULL, -- shell command to run + depid INTEGER, -- identifier of dependency (or '') + priority INTEGER NOT NULL, -- higher priority jobs may run earlier + + /* Fields updated as jobs run */ + starttime INTEGER, -- Start time (milliseconds since 1970) + endtime INTEGER, -- End time + span INTEGER, -- Total run-time in milliseconds + estwork INTEGER, -- Estimated amount of work + estkey TEXT, -- Key used to compute estwork + state TEXT CHECK( state IN ('','ready','running','done','failed','omit','halt') ), + ntest INT, -- Number of test cases run + nerr INT, -- Number of errors reported + svers TEXT, -- Reported SQLite version + pltfm TEXT, -- Host platform reported + output TEXT, -- test output + cwd TEXT -- working directory for test + ); + + CREATE TABLE config( + name TEXT COLLATE nocase PRIMARY KEY, + value + ) WITHOUT ROWID; + + CREATE INDEX i1 ON jobs(state, priority); + CREATE INDEX i2 ON jobs(depid); +} +#------------------------------------------------------------------------- + +#-------------------------------------------------------------------------- +# Check if this script is being invoked to run a single file. If so, +# run it. +# +if {[llength $argv]==2 + && ([lindex $argv 0]=="" || [info exists ::testspec([lindex $argv 0])]) + && [file exists [lindex $argv 1]] +} { + set permutation [lindex $argv 0] + set script [file normalize [lindex $argv 1]] + set ::argv [list] + + set testdir [file dirname $argv0] + source $::testdir/tester.tcl + + if {$permutation=="full"} { + + unset -nocomplain ::G(isquick) + reset_db + + } elseif {$permutation!="default" && $permutation!=""} { + + if {[info exists ::testspec($permutation)]==0} { + error "no such permutation: $permutation" + } + + array set O $::testspec($permutation) + set ::G(perm:name) $permutation + set ::G(perm:prefix) $O(-prefix) + set ::G(isquick) 1 + set ::G(perm:dbconfig) $O(-dbconfig) + set ::G(perm:presql) $O(-presql) + + rename finish_test helper_finish_test + proc finish_test {} " + uplevel { + $O(-shutdown) + } + helper_finish_test + " + + eval $O(-initialize) + } + + reset_db + source $script + exit +} +#-------------------------------------------------------------------------- + +#-------------------------------------------------------------------------- +# Check if this is the "njob" command: +# +if {([llength $argv]==2 || [llength $argv]==1) + && [string compare -nocase njob [lindex $argv 0]]==0 +} { + sqlite3 mydb $TRG(dbname) + if {[llength $argv]==2} { + set param [lindex $argv 1] + if {[string is integer $param]==0 || $param<0 || $param>128} { + puts stderr "parameter must be an integer between 0 and 128" + exit 1 + } + + mydb eval { REPLACE INTO config VALUES('njob', $param); } + } + set res [mydb one { SELECT value FROM config WHERE name='njob' }] + mydb close + puts "$res" + exit +} +#-------------------------------------------------------------------------- + +#-------------------------------------------------------------------------- +# Check if this is the "halt" command: +# +if {[llength $argv]==1 + && [string compare -nocase halt [lindex $argv 0]]==0 +} { + sqlite3 mydb $TRG(dbname) + mydb eval {UPDATE jobs SET state='halt' WHERE state IN ('ready','')} + mydb close + exit +} +#-------------------------------------------------------------------------- + +#-------------------------------------------------------------------------- +# Check if this is the "estwork" command: +# +# Generate (on standard output) a set of estwork() values based on the lastest +# test case, that can be used to replace the test/testrunner_estwork.tcl file. +# +if {[llength $argv]==1 + && [string compare -nocase estwork [lindex $argv 0]]==0 +} { + sqlite3 mydb $TRG(dbname) + set njob [mydb one {SELECT count(*) FROM jobs WHERE state='done'}] + if {$njob<1000} { + puts "Too few completed jobs to do a work estimate." + puts "Have $njob but not need at least 1000." + mydb close + exit 1 + } + set badjobs [mydb one {SELECT count(*) FROM jobs WHERE state<>'done'}] + if {$badjobs} { + puts "Database contains $badjobs incomplete jobs." + mydb close + exit 1 + } + set half [mydb one {SELECT count(*)/2 FROM jobs WHERE displaytype='tcl'}] + set scale [mydb one {SELECT span FROM jobs WHERE displaytype='tcl' + ORDER BY span LIMIT 1 OFFSET $half}] + mydb eval { + SELECT estkey, CAST(avg(span)/$scale AS INT) AS cost + FROM jobs + GROUP BY estkey + HAVING cost>=2 + } { + set estwork($estkey) $cost + } + set avgtcl [mydb one {SELECT avg(span) FROM jobs WHERE displaytype='tcl'}] + set estwork(tcl) 1 + foreach type {bld fuzz make} { + set avg [mydb one {SELECT avg(span) FROM jobs WHERE displaytype=$type}] + if {$avg!=""} { + set estwork($type) [expr {int($avg/$avgtcl)}] + } + } + mydb close + puts "# Estimated relative cost of various jobs, based on the \"estkey\" field." + puts "# Computed by the \"test/testrunner.tcl estwork\" command." + puts "#" + foreach key [lsort [array names estwork]] { + puts "set [list estwork($key)] $estwork($key)" + } + exit +} +#-------------------------------------------------------------------------- + +#-------------------------------------------------------------------------- +# Check if this is the "help" command: +# +if {[string compare -nocase help [lindex $argv 0]]==0} { + usage +} +#-------------------------------------------------------------------------- + +#-------------------------------------------------------------------------- +# Check if this is the "script" command: +# +if {[string compare -nocase script [lindex $argv 0]]==0} { + if {[llength $argv]!=2 && !([llength $argv]==3&&[lindex $argv 1]=="-msvc")} { + usage + } + + set bMsvc [expr ([llength $argv]==3)] + set config [lindex $argv [expr [llength $argv]-1]] + + puts [trd_buildscript $config [file dirname $testdir] $bMsvc] + exit +} + +# Compute an elapse time string MM:SS or HH:MM:SS based on the +# number of milliseconds in the argument. +# +proc elapsetime {ms} { + if {$ms==""} {set ms 0} + set s [expr {int(($ms+500.0)*0.001)}] + set hr [expr {$s/3600}] + set mn [expr {($s/60)%60}] + set sc [expr {$s%60}] + if {$hr>0} { + return [format %02d:%02d:%02d $hr $mn $sc] + } else { + return [format %02d:%02d $mn $sc] + } +} + +# Helper routine for show_status +# +proc display_job {jobdict {tm ""}} { + array set job $jobdict + if {[string length $job(displayname)]>65} { + set dfname [format %.65s... $job(displayname)] + } else { + set dfname [format %-68s $job(displayname)] + } + set dtm "" + if {$tm!=""} { + set dtm [expr {$tm-$job(starttime)}] + set dtm [format %8s [elapsetime $dtm]] + } else { + set dtm [format %8s ""] + } + puts " $dfname $dtm" +} + +# This procedure shows the "status" page. It uses the database +# connect passed in as the "db" parameter. If the "cls" parameter +# is true, then VT100 escape codes are used to format the display. +# +proc show_status {db cls} { + global TRG + $db eval BEGIN + if {[catch { + set cmdline [$db one { SELECT value FROM config WHERE name='cmdline' }] + set nJob [$db one { SELECT value FROM config WHERE name='njob' }] + } msg]} { + if {$cls} {puts "\033\[H\033\[2J"} + puts "Cannot read database: $TRG(dbname)" + return + } + set now [clock_milliseconds] + set tm [$db one { + SELECT + COALESCE((SELECT value FROM config WHERE name='end'), $now) - + (SELECT value FROM config WHERE name='start') + }] + + set totalw 0 + foreach s {"" ready running done failed omit} { set S($s) 0; set W($s) 0; } + set workpending 0 + $db eval { + SELECT state, count(*) AS cnt, sum(estwork) AS ew FROM jobs GROUP BY 1 + } { + incr S($state) $cnt + incr W($state) $ew + incr totalw $ew + } + set nt 0 + set ne 0 + $db eval { + SELECT sum(ntest) AS nt, sum(nerr) AS ne FROM jobs HAVING nt>0 + } break + set fin [expr $W(done)+$W(failed)+$W(omit)] + if {$cmdline!=""} {set cmdline " $cmdline"} + + if {$cls} { + # Move the cursor to the top-left corner. Each iteration will simply + # overwrite. + puts -nonewline "\033\[H" + flush stdout + } + puts [format %-79.79s "Command: \[testrunner.tcl$cmdline\]"] + puts [format %-79.79s "Summary: [elapsetime $tm], $fin/$totalw jobs,\ + $ne errors, $nt tests"] + + set srcdir [file dirname [file dirname $TRG(info_script)]] + set line "Running: $S(running) (max: $nJob)" + if {$S(running)>0 && [set pct [expr {int(($fin*100.0)/$totalw)}]]>=4} { + set tmleft [expr {($tm/double($fin))*($totalw-$fin)}] + if {$tmleft<0.02*$tm} { + set tmleft [expr {$tm*0.02}] + } + set etc " ETC [elapsetime $tmleft]" + if {[string length $line]+[string length $etc]<80} { + append line $etc + } + # append line " $pct%" + } + puts [format %-79.79s $line] + if {$S(running)>0} { + $db eval { + SELECT * FROM jobs WHERE state='running' ORDER BY starttime + } job { + display_job [array get job] $now + } + } + if {$S(failed)>0} { + # $toshow is the number of failures to report. In $cls mode, + # status tries to limit the number of failure reported so that + # the status display does not overflow a 24-line terminal. It will + # always show at least the most recent 4 failures, even if an overflow + # is needed. No limit is imposed for a status within $cls. + # + if {$cls && $S(failed)>18-$S(running)} { + set toshow [expr {18-$S(running)}] + if {$toshow<4} {set toshow 4} + set shown " (must recent $toshow shown)" + } else { + set toshow $S(failed) + set shown "" + } + puts [format %-79s "Failed: $S(failed) $shown"] + $db eval { + SELECT * FROM jobs WHERE state='failed' + ORDER BY endtime DESC LIMIT $toshow + } job { + display_job [array get job] + } + set nOmit [$db one {SELECT count(*) FROM jobs WHERE state='omit'}] + if {$nOmit} { + puts [format %-79s " ... $nOmit jobs omitted due to failures"] + } + } + if {$cls} { + # Clear everything else to the bottom of the screen + puts -nonewline "\033\[0J" + flush stdout + } + $db eval COMMIT +} + + + +#-------------------------------------------------------------------------- +# Check if this is the "status" command: +# +if {[llength $argv]>=1 + && [string compare -nocase status [lindex $argv 0]]==0 +} { + set delay 0 + set cls 0 + for {set ii 1} {$ii<[llength $argv]} {incr ii} { + set a0 [lindex $argv $ii] + if {$a0=="-d" && $ii+1<[llength $argv]} { + incr ii + set delay [lindex $argv $ii] + if {![string is integer -strict $delay]} { + puts "Argument to -d should be an integer" + exit 1 + } + } elseif {$a0=="-cls" || $a0=="--cls"} { + set cls 1 + } else { + puts "unknown option: \"$a0\"" + exit 1 + } + } + + if {![file readable $TRG(dbname)]} { + puts "Database missing: $TRG(dbname)" + exit + } + sqlite3 mydb $TRG(dbname) + mydb timeout 2000 + + # Clear the whole screen initially. + # + if {$delay>0 || $cls} {puts -nonewline "\033\[2J"} + + while {1} { + show_status mydb [expr {$delay>0 || $cls}] + if {$delay<=0} break + after [expr {$delay*1000}] + } + mydb close + exit +} + +#-------------------------------------------------------------------------- +# Check if this is the "joblist" command: +# +if {[llength $argv]>=1 + && [string compare -nocase "joblist" [lindex $argv 0]]==0 +} { + set pattern {} + for {set ii 1} {$ii<[llength $argv]} {incr ii} { + set a0 [lindex $argv $ii] + if {$pattern==""} { + set pattern [string trim $a0 *] + } else { + puts "unknown option: \"$a0\"" + exit 1 + } + } + set SQL {SELECT displaytype, displayname, state FROM jobs} + if {$pattern!=""} { + regsub -all {[^a-zA-Z0-9*.-/]} $pattern ? pattern + set pattern [string tolower $pattern] + append SQL \ + " WHERE lower(concat(state,' ',displaytype,' ',displayname)) GLOB '*$pattern*'" + } + append SQL " ORDER BY starttime" + + if {![file readable $TRG(dbname)]} { + puts "Database missing: $TRG(dbname)" + exit + } + sqlite3 mydb $TRG(dbname) + mydb timeout 2000 + + mydb eval $SQL { + set label UNKNOWN + switch -- $state { + ready {set label READY} + done {set label DONE} + failed {set label FAILED} + omit {set label OMIT} + running {set label RUNNING} + } + puts [format {%-7s %-5s %s} $label $displaytype $displayname] + } + mydb close + exit +} + +# Scan the output of all jobs looking for the summary lines that +# report the number of test cases and the number of errors. +# Aggregate these numbers and return them. +# +proc aggregate_test_counts {db} { + set ne 0 + set nt 0 + $db eval {SELECT sum(nerr) AS ne, sum(ntest) as nt FROM jobs} break + return [list $ne $nt] +} + +#-------------------------------------------------------------------------- +# Check if this is the "errors" command: +# +if {[llength $argv]>=1 + && ([string compare -nocase errors [lindex $argv 0]]==0 || + [string match err* [lindex $argv 0]]==1) +} { + set verbose 0 + set pattern {} + set summary 0 + for {set ii 1} {$ii<[llength $argv]} {incr ii} { + set a0 [lindex $argv $ii] + if {$a0=="-v" || $a0=="--verbose" || $a0=="-verbose"} { + set verbose 1 + } elseif {$a0=="-s" || $a0=="--summary" || $a0=="-summary"} { + set summary 1 + } elseif {$pattern==""} { + set pattern *[string trim $a0 *]* + } else { + puts "unknown option: \"$a0\"". Use --help for more info." + exit 1 + } + } + set cnt 0 + sqlite3 mydb $TRG(dbname) + mydb timeout 5000 + if {$summary} { + set sql "SELECT displayname FROM jobs WHERE state='failed'" + } else { + set sql "SELECT displaytype, displayname, output FROM jobs \ + WHERE state='failed'" + } + if {$pattern!=""} { + regsub -all {[^a-zA-Z0-9*/ ?]} $pattern . pattern + append sql " AND displayname GLOB '$pattern'" + } + mydb eval $sql { + if {$summary} { + puts "FAILED: $displayname" + continue + } + puts "**** $displayname ****" + if {$verbose || $displaytype!="tcl"} { + puts $output + } else { + foreach line [split $output \n] { + if {[string match {!*} $line] || [string match *failed* $line]} { + puts $line + } + } + } + incr cnt + } + if {$pattern==""} { + set summary [aggregate_test_counts mydb] + mydb close + puts "Total [lindex $summary 0] errors out of [lindex $summary 1] tests" + } else { + mydb close + } + exit +} + +#------------------------------------------------------------------------- +# Parse the command line. +# +for {set ii 0} {$ii < [llength $argv]} {incr ii} { + set isLast [expr $ii==([llength $argv]-1)] + set a [lindex $argv $ii] + set n [string length $a] + + if {[string range $a 0 0]=="-"} { + if {($n>2 && [string match "$a*" --jobs]) || $a=="-j"} { + incr ii + set TRG(nJob) [lindex $argv $ii] + if {$isLast} { usage } + } elseif {($n>2 && [string match "$a*" --zipvfs]) || $a=="-z"} { + incr ii + set TRG(zipvfs) [file normalize [lindex $argv $ii]] + if {$isLast} { usage } + } elseif {($n>2 && [string match "$a*" --buildonly]) || $a=="-b"} { + set TRG(buildonly) 1 + } elseif {($n>2 && [string match "$a*" --config]) || $a=="-c"} { + incr ii + set TRG(config) [lindex $argv $ii] + } elseif {($n>2 && [string match "$a*" --dryrun]) || $a=="-d"} { + set TRG(dryrun) 1 + } elseif {($n>2 && [string match "$a*" --explain]) || $a=="-e"} { + set TRG(explain) 1 + } elseif {$n>2 && [string match "$a*" --omit]} { + incr ii + set TRG(omitconfig) [lindex $argv $ii] + } elseif {$n>2 && [string match "$a*" --cases]} { + incr ii + set TRG(case) [lindex $argv $ii] + } elseif {$n>2 && [string match "$a*" --fuzzdb]} { + incr ii + set env(FUZZDB) [lindex $argv $ii] + } elseif {[string match "$a*" --stop-on-error]} { + set TRG(stopOnError) 1 + } elseif {[string match "$a*" --stop-on-coredump]} { + set TRG(stopOnCore) 1 + } elseif {[string match "$a*" --status]} { + if {$tcl_platform(platform) eq "windows"} { + puts stdout \ +"The --status option is not available on Windows. A suggested work-around" + puts stdout \ +"is to run the following command in a separate window:\n" + puts stdout " [info nameofexe] $argv0 status -d 2\n" + } else { + set TRG(fullstatus) 1 + } + } else { + usage + } + } else { + lappend TRG(patternlist) [string map {% *} $a] + } +} +set argv [list] + +# This script runs individual tests - tcl scripts or [make xyz] commands - +# in directories named "testdir$N", where $N is an integer. This variable +# contains a list of integers indicating the directories in use. +# +# This variable is accessed only via the following commands: +# +# dirs_nHelper +# Return the number of entries currently in the list. +# +# dirs_freeDir IDIR +# Remove value IDIR from the list. It is an error if it is not present. +# +# dirs_allocDir +# Select a value that is not already in the list. Add it to the list +# and return it. +# +set TRG(dirs_in_use) [list] + +proc dirs_nHelper {} { + global TRG + llength $TRG(dirs_in_use) +} +proc dirs_freeDir {iDir} { + global TRG + set out [list] + foreach d $TRG(dirs_in_use) { + if {$iDir!=$d} { lappend out $d } + } + if {[llength $out]!=[llength $TRG(dirs_in_use)]-1} { + error "dirs_freeDir could not find $iDir" + } + set TRG(dirs_in_use) $out +} +proc dirs_allocDir {} { + global TRG + array set inuse [list] + foreach d $TRG(dirs_in_use) { + set inuse($d) 1 + } + for {set iRet 0} {[info exists inuse($iRet)]} {incr iRet} { } + lappend TRG(dirs_in_use) $iRet + return $iRet +} + +# Check that directory $dir exists. If it does not, create it. If +# it does, delete its contents. +# +proc create_or_clear_dir {dir} { + set dir [file normalize $dir] + catch { file mkdir $dir } + foreach f [glob -nocomplain [file join $dir *]] { + catch { file delete -force $f } + } +} + +proc build_to_dirname {bname} { + set fold [string tolower [string map {- _} $bname]] + return "testrunner_build_$fold" +} + +#------------------------------------------------------------------------- + +proc r_write_db {tcl} { + trdb eval { BEGIN EXCLUSIVE } + uplevel $tcl + trdb eval { COMMIT } +} + +# Obtain a new job to be run by worker $iJob (an integer). A job is +# returned as a three element list: +# +# {$build $config $file} +# +proc r_get_next_job {iJob} { + global T + + if {($iJob%2)} { + set orderby "ORDER BY priority ASC" + } else { + set orderby "ORDER BY priority DESC" + } + + set ret [list] + + r_write_db { + set query " + SELECT * FROM jobs AS j WHERE state='ready' $orderby LIMIT 1 + " + trdb eval $query job { + set tm [clock_milliseconds] + set T($iJob) $tm + set jobid $job(jobid) + + set cwd $job(dirname) + if {$cwd==""} { + set cwd [dirname $iJob] + } + + trdb eval { + UPDATE jobs + SET starttime=$tm, state='running', cwd=$cwd + WHERE jobid=$jobid + } + + set ret [array get job] + } + } + + return $ret +} + +# Usage: +# +# add_job OPTION ARG OPTION ARG... +# +# where available OPTIONS are: +# +# -displaytype +# -displayname +# -build +# -dirname +# -cmd +# -depid +# -priority +# +# Returns the jobid value for the new job. +# +proc add_job {args} { + global estwork + + set options { + -displaytype -displayname -build -dirname + -cmd -depid -priority + } + + # Set default values of options. + set A(-dirname) "" + set A(-depid) "" + set A(-priority) 0 + set A(-build) "" + + array set A $args + + # Check all required options are present. And that no extras are present. + foreach o $options { + if {[info exists A($o)]==0} { error "missing required option $o" } + } + foreach o [array names A] { + if {[lsearch -exact $options $o]<0} { error "unrecognized option: $o" } + } + + set state "" + if {$A(-depid)==""} { set state ready } + set type $A(-displaytype) + set displayname $A(-displayname) + switch $type { + tcl { + set ek [file tail [lindex $displayname end]] + } + bld { + set ek [lindex $displayname end] + } + fuzz { + set ek [lrange $displayname 1 2] + } + make { + set ek [lindex $displayname end] + } + } + if {[info exists estwork($ek)]} { + set ew $estwork($ek) + } else { + set ew $estwork($type) + } + + trdb eval { + INSERT INTO jobs( + displaytype, displayname, build, dirname, cmd, depid, priority, + estwork, estkey, state + ) VALUES ( + $type, + $A(-displayname), + $A(-build), + $A(-dirname), + $A(-cmd), + $A(-depid), + $A(-priority), + $ew, + $ek, + $state + ) + } + + trdb last_insert_rowid +} + +# Look to see if $jobcmd matches any of the glob patterns given in +# $patternlist. Return true if there is a match. Return false +# if no match is seen. +# +# An empty patternlist matches everything +# +proc job_matches_any_pattern {patternlist jobcmd} { + set bMatch 0 + if {[llength $patternlist]==0} {return 1} + foreach p $patternlist { + set p [string trim $p *] + if {[string index $p 0]=="^"} { + set p [string range $p 1 end] + } else { + set p "*$p" + } + if {[string index $p end]=="\$"} { + set p [string range $p 0 end-1] + } else { + set p "$p*" + } + if {[string match $p $jobcmd]} { + set bMatch 1 + break + } + } + return $bMatch +} + + +# Argument $build is either an empty string, or else a list of length 3 +# describing the job to build testfixture. In the usual form: +# +# {ID DIRNAME DISPLAYNAME} +# +# e.g +# +# {1 /home/user/sqlite/test/testrunner_bld_xyz All-Debug} +# +proc add_tcl_jobs {build config patternlist {shelldepid ""}} { + global TRG + set ntcljob 0 + + set topdir [file dirname $::testdir] + set testrunner_tcl [file normalize [info script]] + + if {$build==""} { + set testfixture [info nameofexec] + } else { + set testfixture [file join [lindex $build 1] $TRG(testfixture)] + } + if {[lindex $build 2]=="Valgrind"} { + set setvar "export OMIT_MISUSE=1\n" + set testfixture "${setvar}valgrind -v --error-exitcode=1 $testfixture" + } + + # The ::testspec array is populated by permutations.test + foreach f [dict get $::testspec($config) -files] { + + if {![job_matches_any_pattern $patternlist "$config [file tail $f]"]} { + continue + } + + if {[file pathtype $f]!="absolute"} { set f [file join $::testdir $f] } + set f [file normalize $f] + + set displayname [string map [list $topdir/ {}] $f] + if {$config=="full" || $config=="veryquick"} { + set cmd "$testfixture $f" + } else { + set cmd "$testfixture $testrunner_tcl $config $f" + set displayname "config=$config $displayname" + } + if {$build!=""} { + set displayname "[lindex $build 2] $displayname" + } + + set lProp [trd_test_script_properties $f] + set priority 0 + if {[lsearch $lProp slow]>=0} { set priority 2 } + if {[lsearch $lProp superslow]>=0} { set priority 4 } + + set depid [lindex $build 0] + if {$shelldepid!="" && [lsearch $lProp shell]>=0} { set depid $shelldepid } + + incr ntcljob + add_job \ + -displaytype tcl \ + -displayname $displayname \ + -cmd $cmd \ + -depid $depid \ + -priority $priority + } + if {$ntcljob==0 && [llength $build]>0} { + set bldid [lindex $build 0] + trdb eval {DELETE FROM jobs WHERE rowid=$bldid} + } +} + +proc add_build_job {buildname target {postcmd ""} {depid ""}} { + global TRG + + set dirname "[string tolower [string map {- _} $buildname]]_$target" + set dirname "testrunner_bld_$dirname" + + set cmd "$TRG(makecmd) $target" + if {$postcmd!=""} { + append cmd "\n" + append cmd $postcmd + } + + set id [add_job \ + -displaytype bld \ + -displayname "Build $buildname ($target)" \ + -dirname $dirname \ + -build $buildname \ + -cmd $cmd \ + -depid $depid \ + -priority 3 + ] + + list $id [file normalize $dirname] $buildname +} + +proc add_shell_build_job {buildname dirname depid} { + global TRG + + if {$TRG(platform)=="win"} { + set path [string map {/ \\} "$dirname/"] + set copycmd "xcopy $TRG(shell) $path" + } else { + set copycmd "cp $TRG(shell) $dirname/" + } + + return [ + add_build_job $buildname $TRG(shell) $copycmd $depid + ] +} + + +proc add_make_job {bld target} { + global TRG + + if {$TRG(platform)=="win"} { + set path [string map {/ \\} [lindex $bld 1]] + set cmd "xcopy /S $path\\* ." + } else { + set cmd "cp -r [lindex $bld 1]/* ." + } + append cmd "\n$TRG(makecmd) $target" + + add_job \ + -displaytype make \ + -displayname "[lindex $bld 2] make $target" \ + -cmd $cmd \ + -depid [lindex $bld 0] \ + -priority 1 +} + +proc add_fuzztest_jobs {buildname patternlist} { + global env TRG + # puts buildname=$buildname + + foreach {interpreter scripts} [trd_fuzztest_data $buildname] { + set bldDone 0 + set subcmd [lrange $interpreter 1 end] + set interpreter [lindex $interpreter 0] + + if {[string match fuzzcheck* $interpreter] + && [info exists env(FUZZDB)] + && [file readable $env(FUZZDB)] + && $buildname ne "Windows-Win32Heap" + && $buildname ne "Windows-Memdebug" + } { + set TRG(FUZZDB) $env(FUZZDB) + set fname [file normalize $env(FUZZDB)] + set N [expr {([file size $fname]+4999999)/5000000}] + for {set i 0} {$i<$N} {incr i} { + lappend scripts [list --slice $i $N $fname] + } + } + + foreach s $scripts { + + # Fuzz data files fuzzdata1.db and fuzzdata2.db are larger than + # the others. So ensure that these are run as a higher priority. + if {[llength $s]==1} { + set tail [file tail $s] + } else { + set fname [lindex $s end] + set tail [lrange $s 0 end-1] + lappend tail [file tail $fname] + } + if {![job_matches_any_pattern $patternlist "$interpreter $tail"]} { + continue + } + if {!$bldDone} { + set bld [add_build_job $buildname $interpreter] + foreach {depid dirname displayname} $bld {} + set bldDone 1 + } + if {[string match ?-slice* $tail]} { + set priority 15 + } elseif {$tail=="fuzzdata1.db" + || $tail=="fuzzdata2.db" + || $tail=="fuzzdata8.db"} { + set priority 5 + } else { + set priority 1 + } + add_job \ + -displaytype fuzz \ + -displayname "$buildname $interpreter $tail" \ + -depid $depid \ + -cmd "[file join $dirname $interpreter] $subcmd $s" \ + -priority $priority + } + } +} + +proc add_zipvfs_jobs {} { + global TRG + source [file join $TRG(zipvfs) test zipvfs_testrunner.tcl] + + set bld [add_build_job Zipvfs $TRG(testfixture)] + foreach s [zipvfs_testrunner_files] { + set cmd "[file join [lindex $bld 1] $TRG(testfixture)] $s" + add_job \ + -displaytype tcl \ + -displayname "Zipvfs [file tail $s]" \ + -cmd $cmd \ + -depid [lindex $bld 0] + } + + set ::env(SQLITE_TEST_DIR) $::testdir +} + +# Used to add jobs for "mdevtest" and "sdevtest". +# +proc add_devtest_jobs {lBld patternlist} { + global TRG + + foreach b $lBld { + set bld [add_build_job $b $TRG(testfixture)] + add_tcl_jobs $bld veryquick $patternlist SHELL + add_fuzztest_jobs $b $patternlist + + if {[trdb one "SELECT EXISTS (SELECT 1 FROM jobs WHERE depid='SHELL')"]} { + set sbld [add_shell_build_job $b [lindex $bld 1] [lindex $bld 0]] + set sbldid [lindex $sbld 0] + trdb eval { + UPDATE jobs SET depid=$sbldid WHERE depid='SHELL' + } + } + + } +} + +# Check to ensure that the interpreter is a full-blown "testfixture" +# build and not just a "tclsh". If this is not the case, issue an +# error message and exit. +# +proc must_be_testfixture {} { + if {[lsearch [info commands] sqlite3_soft_heap_limit]<0} { + puts "Use testfixture, not tclsh, for these arguments." + exit 1 + } +} + +proc add_jobs_from_cmdline {patternlist} { + global TRG + + if {$TRG(zipvfs)!=""} { + add_zipvfs_jobs + if {[llength $patternlist]==0} return + } + + if {[llength $patternlist]==0} { + set patternlist [list veryquick] + } + + set first [lindex $patternlist 0] + switch -- $first { + all { + must_be_testfixture + set patternlist [lrange $patternlist 1 end] + set clist [trd_all_configs] + foreach c $clist { + add_tcl_jobs "" $c $patternlist + } + } + + devtest - + mdevtest { + set config_set { + All-O0 + All-Debug + } + add_devtest_jobs $config_set [lrange $patternlist 1 end] + } + + sdevtest { + set config_set { + All-Sanitize + All-Debug + } + add_devtest_jobs $config_set [lrange $patternlist 1 end] + } + + release { + set patternlist [lrange $patternlist 1 end] + foreach b [trd_builds $TRG(platform)] { + if {$TRG(config)!="" && ![regexp "\\y$b\\y" $TRG(config)]} continue + if {[regexp "\\y$b\\y" $TRG(omitconfig)]} continue + set bld [add_build_job $b $TRG(testfixture)] + foreach c [trd_configs $TRG(platform) $b] { + add_tcl_jobs $bld $c $patternlist SHELL + } + + foreach e [trd_extras $TRG(platform) $b] { + if {$e=="fuzztest"} { + add_fuzztest_jobs $b $patternlist + } elseif {[job_matches_any_pattern $patternlist $e]} { + add_make_job $bld $e + } + } + + if {[trdb one "SELECT EXISTS(SELECT 1 + FROM jobs WHERE depid='SHELL')"]} { + set sbld [add_shell_build_job $b [lindex $bld 1] [lindex $bld 0]] + set sbldid [lindex $sbld 0] + trdb eval { + UPDATE jobs SET depid=$sbldid WHERE depid='SHELL' + } + } + } + } + + list { + set allperm [array names ::testspec] + lappend allperm all mdevtest sdevtest release list + puts "Allowed values for the PERMUTATION argument: [lsort $allperm]" + exit 0 + } + + default { + must_be_testfixture + if {[info exists ::testspec($first)]} { + add_tcl_jobs "" $first [lrange $patternlist 1 end] + } else { + add_tcl_jobs "" full $patternlist + } + } + } + + # If the "--case DISPLAYNAME" option appears on the command-line, mark + # all tests other than DISPLAYNAME as 'omit'. + # + if {[info exists TRG(case)] && $TRG(case) ne ""} { + set jid [trdb one { + SELECT jobid FROM jobs WHERE displayname GLOB $TRG(case) + }] + if {$jid eq ""} { + puts "ERROR: No jobs match \"$TRG(case)\"." + puts "The argument to --cases must GLOB match the jobs.displayname column" + puts "of the testrunner.db database." + trdb eval {UPDATE jobs SET state='omit'} + } else { + trdb eval { + WITH RECURSIVE keepers(jid,did) AS ( + SELECT jobid,depid FROM jobs + WHERE displayname GLOB $TRG(case) + UNION + SELECT jobid,depid FROM jobs, keepers WHERE jobid=did + ) + DELETE FROM jobs WHERE jobid NOT IN (SELECT jid FROM keepers); + } + } + } +} + +proc make_new_testset {} { + global TRG + + trdb eval {PRAGMA journal_mode=WAL;} + r_write_db { + trdb eval $TRG(schema) + set nJob $TRG(nJob) + set cmdline $TRG(cmdline) + set tm [clock_milliseconds] + trdb eval { REPLACE INTO config VALUES('njob', $nJob ); } + trdb eval { REPLACE INTO config VALUES('cmdline', $cmdline ); } + trdb eval { REPLACE INTO config VALUES('start', $tm ); } + + add_jobs_from_cmdline $TRG(patternlist) + + } +} + +proc mark_job_as_finished {jobid output state endtm} { + set ntest 1 + set nerr 0 + if {$endtm>0} { + set re {\y(\d+) errors out of (\d+) tests( on [^\n]+\n)?} + if {[regexp $re $output all a b pltfm]} { + set nerr $a + set ntest $b + } + regexp {\ySQLite \d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d [0-9a-fA-F]+} \ + $output svers + } + r_write_db { + if {$state=="failed"} { + set childstate omit + if {$nerr<=0} {set nerr 1} + } else { + set childstate ready + } + if {[info exists pltfm]} {set pltfm [string trim $pltfm]} + trdb eval { + UPDATE jobs + SET output=$output, state=$state, endtime=$endtm, span=$endtm-starttime, + ntest=$ntest, nerr=$nerr, svers=$svers, pltfm=$pltfm + WHERE jobid=$jobid; + UPDATE jobs SET state=$childstate WHERE depid=$jobid AND state!='halt'; + UPDATE config SET value=value+$nerr WHERE name='nfail'; + UPDATE config SET value=value+$ntest WHERE name='ntest'; + } + } +} + +proc script_input_ready {fd iJob jobid} { + global TRG + global O + global T + + if {[eof $fd]} { + trdb eval { SELECT * FROM jobs WHERE jobid=$jobid } job {} + + # If this job specified a directory name, then delete the run.sh/run.bat + # file from it before continuing. This is because the contents of this + # directory might be copied by some other job, and we don't want to copy + # the run.sh file in this case. + if {$job(dirname)!=""} { + file delete -force [file join $job(dirname) $TRG(run)] + } + + set ::done 1 + fconfigure $fd -blocking 1 + set state "done" + set rc [catch { close $fd } msg] + if {$rc} { + puts [format %-79.79s "FAILED: $job(displayname) ($iJob)"] + set state "failed" + if {$TRG(stopOnError)} { + puts "OUTPUT: $O($iJob)" + exit 1 + } + if {$TRG(stopOnCore) && [string first {core dumped} $O($iJob)]>0} { + puts "OUTPUT: $O($iJob)" + exit 1 + } + } + + set tm [clock_milliseconds] + set jobtm [expr {$tm - $job(starttime)}] + + puts $TRG(log) "### $job(displayname) ${jobtm}ms ($state)" + puts $TRG(log) [string trim $O($iJob)] + + mark_job_as_finished $jobid $O($iJob) $state $tm + + dirs_freeDir $iJob + launch_some_jobs + incr ::wakeup + } else { + set rc [catch { gets $fd line } res] + if {$rc} { + puts "ERROR $res" + } + if {$res>=0} { + append O($iJob) "$line\n" + } + } + +} + +proc dirname {ii} { + return "testdir$ii" +} + +proc launch_another_job {iJob} { + global TRG + global O + global T + + set testfixture [info nameofexec] + set script $TRG(info_script) + + set O($iJob) "" + + set jobdict [r_get_next_job $iJob] + if {$jobdict==""} { return 0 } + array set job $jobdict + + set dir $job(dirname) + if {$dir==""} { set dir [dirname $iJob] } + create_or_clear_dir $dir + + if {$job(build)!=""} { + set srcdir [file dirname $::testdir] + if {$job(build)=="Zipvfs"} { + set script [zipvfs_testrunner_script] + } else { + set bWin [expr {$TRG(platform)=="win"}] + set script [trd_buildscript $job(build) $srcdir $bWin] + } + set fd [open [file join $dir $TRG(make)] w] + puts $fd $script + close $fd + } + + # Add a batch/shell file command to set the directory used for temp + # files to the test's working directory. Otherwise, tests that use + # large numbers of temp files (e.g. zipvfs), might generate temp + # filename collisions. + if {$TRG(platform)=="win"} { + set set_tmp_dir "SET SQLITE_TMPDIR=[file normalize $dir]" + } else { + set set_tmp_dir "export SQLITE_TMPDIR=\"[file normalize $dir]\"" + } + + if { $TRG(dryrun) } { + + mark_job_as_finished $job(jobid) "" done 0 + dirs_freeDir $iJob + if {$job(build)!=""} { + puts $TRG(log) "(cd $dir ; $job(cmd) )" + } else { + puts $TRG(log) "$job(cmd)" + } + + } else { + set pwd [pwd] + cd $dir + set fd [open $TRG(run) w] + puts $fd $set_tmp_dir + puts $fd $job(cmd) + close $fd + set fd [open "|$TRG(runcmd) 2>@1" r] + cd $pwd + + fconfigure $fd -blocking false -translation binary + fileevent $fd readable [list script_input_ready $fd $iJob $job(jobid)] + } + + return 1 +} + +# Show the testing progress report +# +proc progress_report {} { + global TRG + + if {$TRG(fullstatus)} { + if {$::tcl_platform(platform) eq "windows"} { + exec [info nameofexe] $::argv0 status --cls + } else { + show_status trdb 1 + } + } else { + set tmms [expr [clock_milliseconds] - $TRG(starttime)] + set tm [format "%d" [expr int($tmms/1000.0 + 0.5)]] + + set wtotal 0 + set wdone 0 + r_write_db { + trdb eval { + SELECT displaytype, state, count(*) AS cnt, sum(estwork) AS ew + FROM jobs + GROUP BY 1, 2 + } { + set v($state,$displaytype) $cnt + incr t($displaytype) $cnt + incr wtotal $ew + if {$state=="done" || $state=="failed" || $state=="omit"} { + incr wdone $ew + } + } + } + + set text "" + foreach j [lsort [array names t]] { + foreach k {done failed running} { incr v($k,$j) 0 } + set fin [expr $v(done,$j) + $v(failed,$j)] + lappend text "${j}($fin/$t($j))" + if {$v(failed,$j)>0} { + lappend text "f$v(failed,$j)" + } + if {$v(running,$j)>0} { + lappend text "r$v(running,$j)" + } + } + set report "[elapsetime $tmms] [join $text { }]" + if {$wdone>0 && [set pct [expr {int(($wdone*100.0)/$wtotal)}]]>=4} { + set tmleft [expr {($tmms/double($wdone))*($wtotal-$wdone)}] + set etc " ETC [elapsetime $tmleft]" + if {[string length $report]+[string length $etc]<80} { + append report $etc + } + # append report " $pct%" + } + puts -nonewline [format %-79.79s $report]\r + flush stdout + } + after $TRG(reporttime) progress_report +} + +proc launch_some_jobs {} { + global TRG + set nJob [trdb one { SELECT value FROM config WHERE name='njob' }] + + while {[dirs_nHelper]<$nJob} { + set iDir [dirs_allocDir] + if {0==[launch_another_job $iDir]} { + dirs_freeDir $iDir + break; + } + } +} + +proc run_testset {} { + global TRG + set ii 0 + + set TRG(starttime) [clock_milliseconds] + set TRG(log) [open $TRG(logname) w] + + launch_some_jobs + + if {$TRG(fullstatus)} {puts "\033\[2J"} + progress_report + while {[dirs_nHelper]>0} { + after 500 {incr ::wakeup} + vwait ::wakeup + } + close $TRG(log) + progress_report + puts "" + + r_write_db { + set tm [clock_milliseconds] + trdb eval { REPLACE INTO config VALUES('end', $tm ); } + set nErr [trdb one {SELECT count(*) FROM jobs WHERE state='failed'}] + if {$nErr>0} { + puts "$nErr failures:" + trdb eval { + SELECT displayname FROM jobs WHERE state='failed' + } { + puts "FAILED: $displayname" + } + } + set nOmit [trdb one {SELECT count(*) FROM jobs WHERE state='omit'}] + if {$nOmit>0} { + puts "$nOmit jobs skipped due to prior failures" + } + } + + puts "Test database is $TRG(dbname)" + puts "Test log is $TRG(logname)" + if {[info exists TRG(FUZZDB)]} { + puts "Extra fuzzcheck data taken from $TRG(FUZZDB)" + } + trdb eval { + SELECT sum(ntest) AS totaltest, + sum(nerr) AS totalerr + FROM jobs + } break + trdb eval { + SELECT max(endtime)-min(starttime) AS totaltime + FROM jobs WHERE endtime>0 + } break; + set et [elapsetime $totaltime] + set pltfm {} + trdb eval { + SELECT pltfm, count(*) FROM jobs WHERE pltfm IS NOT NULL + ORDER BY 2 DESC LIMIT 1 + } break + if {$totalerr==""} {set totalerr 0} + if {$totaltest==""} {set totaltest 0} + puts "$totalerr errors out of $totaltest tests in $et $pltfm" + trdb eval { + SELECT DISTINCT substr(svers,1,79) as v1 FROM jobs WHERE svers IS NOT NULL + } {puts $v1} + +} + +# Handle the --buildonly option, if it was specified. +# +proc handle_buildonly {} { + global TRG + if {$TRG(buildonly)} { + r_write_db { + trdb eval { DELETE FROM jobs WHERE displaytype!='bld' } + } + } +} + +# Handle the --explain option. Provide a human-readable +# explanation of all the tests that are in the trdb database jobs +# table. +# +proc explain_layer {indent depid} { + global TRG + if {$TRG(buildonly)} { + set showtests 0 + } else { + set showtests 1 + } + trdb eval {SELECT jobid, displayname, displaytype, dirname + FROM jobs WHERE depid=$depid ORDER BY displayname} { + if {$displaytype=="bld"} { + puts "${indent}$displayname in $dirname" + explain_layer "${indent} " $jobid + } elseif {$showtests} { + puts "${indent}$displayname" + } + } +} +proc explain_tests {} { + explain_layer "" "" +} + +sqlite3 trdb $TRG(dbname) +trdb timeout $TRG(timeout) +set tm [lindex [time { make_new_testset }] 0] +if {$TRG(explain)} { + explain_tests +} else { + if {$TRG(nJob)>1} { + puts "splitting work across $TRG(nJob) cores" + } + puts "built testset in [expr $tm/1000]ms.." + handle_buildonly + run_testset +} +trdb close diff --git a/test/testrunner_data.tcl b/test/testrunner_data.tcl new file mode 100644 index 0000000000..e74caee1d6 --- /dev/null +++ b/test/testrunner_data.tcl @@ -0,0 +1,711 @@ + + + +namespace eval trd { + variable tcltest + variable extra + variable all_configs + variable build + + + # Tcl tests to run for various builds. + # + set tcltest(linux.Fast-One) veryquick + set tcltest(linux.Debug-One) veryquick + set tcltest(linux.Debug-Two) veryquick + set tcltest(linux.Have-Not) veryquick + set tcltest(linux.Secure-Delete) veryquick + set tcltest(linux.Unlock-Notify) veryquick + set tcltest(linux.Update-Delete-Limit) veryquick + set tcltest(linux.Extra-Robustness) veryquick + set tcltest(linux.Device-Two) veryquick + set tcltest(linux.No-lookaside) veryquick + set tcltest(linux.Devkit) veryquick + set tcltest(linux.Apple) veryquick + set tcltest(linux.Android) veryquick + set tcltest(linux.Sanitize) veryquick + set tcltest(linux.Device-One) all + set tcltest(linux.Default) all_plus_autovacuum_crash + set tcltest(linux.Valgrind) valgrind + + set tcltest(osx.Locking-Style) veryquick + set tcltest(osx.Have-Not) veryquick + set tcltest(osx.Apple) all_less_no_mutex_try + + set tcltest(win.Stdcall) veryquick + set tcltest(win.Have-Not) veryquick + set tcltest(win.Windows-Memdebug) veryquick + set tcltest(win.Windows-Win32Heap) veryquick + set tcltest(win.Windows-Sanitize) veryquick + set tcltest(win.Windows-WinRT) veryquick + set tcltest(win.Default) {full win_unc_locking} + + # Extra [make xyz] tests that should be run for various builds. + # + set extra(linux.Check-Symbols) checksymbols + set extra(linux.Fast-One) {fuzztest sourcetest} + set extra(linux.Debug-One) {fuzztest sourcetest mptest} + set extra(linux.Debug-Two) {fuzztest sourcetest} + set extra(linux.Have-Not) {fuzztest sourcetest} + set extra(linux.Secure-Delete) {fuzztest sourcetest} + set extra(linux.Unlock-Notify) {fuzztest sourcetest} + set extra(linux.Update-Delete-Limit) {fuzztest sourcetest} + set extra(linux.Extra-Robustness) {fuzztest sourcetest} + set extra(linux.Device-Two) {fuzztest sourcetest threadtest} + set extra(linux.No-lookaside) {fuzztest sourcetest} + set extra(linux.Devkit) {fuzztest sourcetest} + set extra(linux.Android) {fuzztest sourcetest} + set extra(linux.Apple) {fuzztest sourcetest} + set extra(linux.Sanitize) {fuzztest sourcetest} + set extra(linux.Default) {fuzztest sourcetest threadtest} + + set extra(osx.Apple) {fuzztest threadtest} + set extra(osx.Have-Not) {fuzztest sourcetest} + set extra(osx.Locking-Style) {mptest fuzztest sourcetest} + + set extra(win.Default) mptest + set extra(win.Stdcall) {fuzztest sourcetest} + set extra(win.Windows-Memdebug) {fuzztest sourcetest} + set extra(win.Windows-Win32Heap) {fuzztest sourcetest} + set extra(win.Windows-Sanitize) fuzztest + set extra(win.Have-Not) {fuzztest sourcetest} + + # The following mirrors the set of test suites invoked by "all.test". + # + set all_configs { + full no_optimization memsubsys1 memsubsys2 singlethread + multithread onefile utf16 exclusive persistent_journal + persistent_journal_error no_journal no_journal_error + autovacuum_ioerr no_mutex_try fullmutex journaltest + inmemory_journal pcache0 pcache10 pcache50 pcache90 + pcache100 prepare mmap + } + + #----------------------------------------------------------------------- + # Start of build() definitions. + # + set build(Default) { + -O2 + --disable-amalgamation --disable-shared + --enable-session + -DSQLITE_ENABLE_RBU + -DSQLITE_ENABLE_STMT_SCANSTATUS + } + + # These two are used by [testrunner.tcl mdevtest] (All-O0) and + # [testrunner.tcl sdevtest] (All-Sanitize). + # + set build(All-Debug) { + --with-debug --enable-all + -DSQLITE_ENABLE_ORDERED_SET_AGGREGATES + -DSQLITE_ENABLE_NORMALIZE + } + set build(All-O0) { + -O0 --enable-all + } + set build(All-Sanitize) { + -DSQLITE_OMIT_LOOKASIDE=1 + --enable-all -fsanitize=address,undefined -fno-sanitize-recover=undefined + } + + set build(Sanitize) { + CC=clang -fsanitize=address,undefined -fno-sanitize-recover=undefined + -DSQLITE_ENABLE_STAT4 + -DSQLITE_OMIT_LOOKASIDE=1 + -DSQLITE_ENABLE_NORMALIZE + -DCONFIG_SLOWDOWN_FACTOR=5.0 + -DSQLITE_ENABLE_RBU + --with-debug + --enable-all + } + set build(Stdcall) { + -DWITHOUT_JIMSH=1 + -DUSE_STDCALL=1 + -DSQLITE_USE_ONLY_WIN32=1 + -O2 + } + + # The "Have-Not" configuration sets all possible -UHAVE_feature options + # in order to verify that the code works even on platforms that lack + # these support services. + set build(Have-Not) { + -DHAVE_FDATASYNC=0 + -DHAVE_GMTIME_R=0 + -DHAVE_ISNAN=0 + -DHAVE_LOCALTIME_R=0 + -DHAVE_LOCALTIME_S=0 + -DHAVE_MALLOC_USABLE_SIZE=0 + -DHAVE_STRCHRNUL=0 + -DHAVE_USLEEP=0 + -DHAVE_UTIME=0 + } + set build(Unlock-Notify) { + -O2 + -DSQLITE_ENABLE_UNLOCK_NOTIFY + -DSQLITE_THREADSAFE + -DSQLITE_TCL_DEFAULT_FULLMUTEX=1 + } + set build(Secure-Delete) { + -O2 + -DSQLITE_SECURE_DELETE=1 + -DSQLITE_SOUNDEX=1 + } + set build(Update-Delete-Limit) { + -O2 + -DSQLITE_DEFAULT_FILE_FORMAT=4 + -DSQLITE_ENABLE_UPDATE_DELETE_LIMIT=1 + -DSQLITE_ENABLE_STMT_SCANSTATUS + -DSQLITE_LIKE_DOESNT_MATCH_BLOBS + -DSQLITE_ENABLE_CURSOR_HINTS + } + set build(Check-Symbols) { + -DSQLITE_MEMDEBUG=1 + -DSQLITE_ENABLE_FTS3_PARENTHESIS=1 + -DSQLITE_ENABLE_FTS3=1 + -DSQLITE_ENABLE_RTREE=1 + -DSQLITE_ENABLE_MEMSYS5=1 + -DSQLITE_ENABLE_MEMSYS3=1 + -DSQLITE_ENABLE_COLUMN_METADATA=1 + -DSQLITE_ENABLE_UPDATE_DELETE_LIMIT=1 + -DSQLITE_SECURE_DELETE=1 + -DSQLITE_SOUNDEX=1 + -DSQLITE_ENABLE_ATOMIC_WRITE=1 + -DSQLITE_ENABLE_MEMORY_MANAGEMENT=1 + -DSQLITE_ENABLE_NORMALIZE + -DSQLITE_ENABLE_OVERSIZE_CELL_CHECK=1 + -DSQLITE_ENABLE_STAT4 + -DSQLITE_ENABLE_STMT_SCANSTATUS + --enable-fts5 --enable-session + } + set build(Debug-One) { + --disable-shared + -O2 -funsigned-char + -DSQLITE_DEBUG=1 + -DSQLITE_MEMDEBUG=1 + -DSQLITE_MUTEX_NOOP=1 + -DSQLITE_TCL_DEFAULT_FULLMUTEX=1 + -DSQLITE_ENABLE_FTS3=1 + -DSQLITE_ENABLE_RTREE=1 + -DSQLITE_ENABLE_MEMSYS5=1 + -DSQLITE_ENABLE_NORMALIZE + -DSQLITE_ENABLE_COLUMN_METADATA=1 + -DSQLITE_ENABLE_STAT4 + -DSQLITE_ENABLE_HIDDEN_COLUMNS + -DSQLITE_MAX_ATTACHED=125 + -DSQLITE_MUTATION_TEST + --enable-fts5 + } + set build(Debug-Two) { + -DSQLITE_DEFAULT_MEMSTATUS=0 + -DSQLITE_MAX_EXPR_DEPTH=0 + --with-debug + } + set build(Fast-One) { + -O6 + -DSQLITE_ENABLE_FTS4=1 + -DSQLITE_ENABLE_RTREE=1 + -DSQLITE_ENABLE_STAT4 + -DSQLITE_ENABLE_RBU + -DSQLITE_MAX_ATTACHED=125 + -DSQLITE_MAX_MMAP_SIZE=12884901888 + -DSQLITE_ENABLE_SORTER_MMAP=1 + --enable-session + } + set build(Device-One) { + -O2 + -DSQLITE_DEBUG=1 + -DSQLITE_DEFAULT_AUTOVACUUM=1 + -DSQLITE_DEFAULT_CACHE_SIZE=64 + -DSQLITE_DEFAULT_PAGE_SIZE=1024 + -DSQLITE_DEFAULT_TEMP_CACHE_SIZE=32 + -DSQLITE_DISABLE_LFS=1 + -DSQLITE_ENABLE_ATOMIC_WRITE=1 + -DSQLITE_ENABLE_IOTRACE=1 + -DSQLITE_ENABLE_MEMORY_MANAGEMENT=1 + -DSQLITE_MAX_PAGE_SIZE=4096 + -DSQLITE_OMIT_LOAD_EXTENSION=1 + -DSQLITE_OMIT_PROGRESS_CALLBACK=1 + -DSQLITE_OMIT_VIRTUALTABLE=1 + -DSQLITE_ENABLE_HIDDEN_COLUMNS + -DSQLITE_TEMP_STORE=3 + } + set build(Device-Two) { + -DSQLITE_4_BYTE_ALIGNED_MALLOC=1 + -DSQLITE_DEFAULT_AUTOVACUUM=1 + -DSQLITE_DEFAULT_CACHE_SIZE=1000 + -DSQLITE_DEFAULT_LOCKING_MODE=0 + -DSQLITE_DEFAULT_PAGE_SIZE=1024 + -DSQLITE_DEFAULT_TEMP_CACHE_SIZE=1000 + -DSQLITE_DISABLE_LFS=1 + -DSQLITE_ENABLE_FTS3=1 + -DSQLITE_ENABLE_MEMORY_MANAGEMENT=1 + -DSQLITE_ENABLE_RTREE=1 + -DSQLITE_MAX_COMPOUND_SELECT=50 + -DSQLITE_MAX_PAGE_SIZE=32768 + -DSQLITE_OMIT_TRACE=1 + -DSQLITE_TEMP_STORE=3 + -DSQLITE_THREADSAFE=2 + --enable-fts5 --enable-session + } + set build(Locking-Style) { + -O2 + -DSQLITE_ENABLE_LOCKING_STYLE=1 + } + set build(Android) { + -Os + -DHAVE_USLEEP=1 + -DSQLITE_HAVE_ISNAN + -DSQLITE_POWERSAFE_OVERWRITE=1 + -DSQLITE_DEFAULT_FILE_FORMAT=4 + -DSQLITE_DEFAULT_AUTOVACUUM=1 + -DSQLITE_ENABLE_MEMORY_MANAGEMENT=1 + -DSQLITE_ENABLE_FTS3 + -DSQLITE_ENABLE_FTS3_BACKWARDS + -DSQLITE_ENABLE_FTS4 + -DSQLITE_SECURE_DELETE + -DSQLITE_ENABLE_BATCH_ATOMIC_WRITE + -DBIONIC_IOCTL_NO_SIGNEDNESS_OVERLOAD + -DSQLITE_ALLOW_ROWID_IN_VIEW + -DSQLITE_ENABLE_BYTECODE_VTAB + -Wno-unused-parameter + -Werror + -DUSE_PREAD64 + -Dfdatasync=fdatasync + -DHAVE_MALLOC_H=1 + -DHAVE_MALLOC_USABLE_SIZE + -DSQLITE_ENABLE_DBSTAT_VTAB + } + # Compile-options used by Android but omitted from these + # tests: + # -DNDEBUG=1 + # -DSQLITE_DEFAULT_LEGACY_ALTER_TABLE + # -DSQLITE_DEFAULT_JOURNAL_SIZE_LIMIT=1048576 + # -DSQLITE_DEFAULT_FILE_PERMISSIONS=0600 + # -DSQLITE_OMIT_BUILTIN_TEST + # -DSQLITE_OMIT_LOAD_EXTENSION + # -DSQLITE_OMIT_COMPILEOPTION_DIAGS + # + set build(Apple) { + -Os + -DHAVE_GMTIME_R=1 + -DHAVE_ISNAN=1 + -DHAVE_LOCALTIME_R=1 + -DHAVE_PREAD=1 + -DHAVE_PWRITE=1 + -DHAVE_UTIME=1 + -DSQLITE_DEFAULT_CACHE_SIZE=1000 + -DSQLITE_DEFAULT_CKPTFULLFSYNC=1 + -DSQLITE_DEFAULT_MEMSTATUS=1 + -DSQLITE_DEFAULT_PAGE_SIZE=1024 + -DSQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS=1 + -DSQLITE_ENABLE_API_ARMOR=1 + -DSQLITE_ENABLE_AUTO_PROFILE=1 + -DSQLITE_ENABLE_FLOCKTIMEOUT=1 + -DSQLITE_ENABLE_FTS3=1 + -DSQLITE_ENABLE_FTS3_PARENTHESIS=1 + -DSQLITE_ENABLE_FTS3_TOKENIZER=1 + -DSQLITE_ENABLE_NORMALIZE=1 + -DSQLITE_ENABLE_PERSIST_WAL=1 + -DSQLITE_ENABLE_PURGEABLE_PCACHE=1 + -DSQLITE_ENABLE_RTREE=1 + -DSQLITE_ENABLE_SETLK_TIMEOUT=2 + -DSQLITE_ENABLE_SNAPSHOT=1 + -DSQLITE_ENABLE_UPDATE_DELETE_LIMIT=1 + -DSQLITE_MAX_LENGTH=2147483645 + -DSQLITE_MAX_VARIABLE_NUMBER=500000 + -DSQLITE_NO_SYNC=1 + -DSQLITE_OMIT_AUTORESET=1 + -DSQLITE_OMIT_LOAD_EXTENSION=1 + -DSQLITE_PREFER_PROXY_LOCKING=1 + -DSQLITE_SERIES_CONSTRAINT_VERIFY=1 + -DSQLITE_THREADSAFE=2 + -DSQLITE_USE_URI=1 + -DSQLITE_WRITE_WALFRAME_PREBUFFERED=1 + -DUSE_GUARDED_FD=1 + -DUSE_PREAD=1 + --enable-fts5 + } + set build(Extra-Robustness) { + -DSQLITE_ENABLE_OVERSIZE_CELL_CHECK=1 + -DSQLITE_MAX_ATTACHED=62 + } + set build(Devkit) { + -DSQLITE_DEFAULT_FILE_FORMAT=4 + -DSQLITE_MAX_ATTACHED=30 + -DSQLITE_ENABLE_COLUMN_METADATA + -DSQLITE_ENABLE_FTS4 + -DSQLITE_ENABLE_FTS5 + -DSQLITE_ENABLE_FTS4_PARENTHESIS + -DSQLITE_DISABLE_FTS4_DEFERRED + -DSQLITE_ENABLE_RTREE + --enable-fts5 + } + set build(No-lookaside) { + -DSQLITE_TEST_REALLOC_STRESS=1 + -DSQLITE_OMIT_LOOKASIDE=1 + } + set build(Valgrind) { + -DSQLITE_ENABLE_STAT4 + -DSQLITE_ENABLE_FTS4 + -DSQLITE_ENABLE_RTREE + -DSQLITE_ENABLE_HIDDEN_COLUMNS + -DCONFIG_SLOWDOWN_FACTOR=8.0 + } + + set build(Windows-Memdebug) { + MEMDEBUG=1 + DEBUG=3 + } + set build(Windows-Win32Heap) { + WIN32HEAP=1 + DEBUG=4 + ENABLE_SETLK=1 + } + set build(Windows-Sanitize) { + ASAN=1 + } + + set build(Windows-WinRT) { + FOR_WINRT=1 + ENABLE_SETLK=1 + -DSQLITE_TEMP_STORE=3 + } +} + + +#------------------------------------------------------------------------- +proc trd_import {} { + uplevel { + variable ::trd::tcltest + variable ::trd::extra + variable ::trd::all_configs + variable ::trd::build + } +} + +proc trd_builds {platform} { + trd_import + + set klist [lsort -uniq [concat \ + [array names tcltest ${platform}.*] \ + [array names extra ${platform}.*] \ + ]] + if {[llength $klist]==0} { + error "no such platform: $platform" + } + + set ret "" + foreach k $klist { + foreach {p c} [split $k "."] {} + lappend ret $c + } + set ret +} + +proc trd_configs {platform bld} { + trd_import + + set clist [list] + + if {[info exists tcltest($platform.$bld)]} { + set clist $tcltest($platform.$bld) + if {$clist=="all"} { + set clist $all_configs + } elseif {$clist=="all_plus_autovacuum_crash"} { + set clist [concat $all_configs autovacuum_crash] + } elseif {$clist=="all_less_no_mutex_try"} { + set idx [lsearch $all_configs no_mutex_try] + set clist [lreplace $all_configs $idx $idx] + } + } + + set clist +} + +proc trd_extras {platform bld} { + trd_import + if {[info exists extra($platform.$bld)]==0} { return [list] } + return $extra($platform.$bld) +} + +# Usage: +# +# trd_fuzztest_data $buildname +# +# This returns data used by testrunner.tcl to run commands equivalent +# to [make fuzztest]. The returned value is a list, which should be +# interpreted as a sequence of pairs. The first element of each pair +# is an interpreter name. The second element is a list of files. +# testrunner.tcl automatically creates one job to build each interpreter, +# and one to run each of the files with it once it has been built. +# +# In practice, the returned value looks like this: +# +# { +# {fuzzcheck {$testdir/fuzzdata1.db $testdir/fuzzdata2.db ...}} +# {{sessionfuzz run} $testdir/sessionfuzz-data1.db} +# } +# +# where $testdir is replaced by the full-path to the test-directory (the +# directory containing this file). "fuzzcheck" and "sessionfuzz" have .exe +# extensions on windows. +# +proc trd_fuzztest_data {buildname} { + set EXE "" + set lFuzzDb [glob [file join $::testdir fuzzdata*.db]] + set lSessionDb [glob [file join $::testdir sessionfuzz-data*.db]] + set sanBuilds {All-Debug Apple Have-Not Update-Delete-Limit} + + if {$::tcl_platform(platform) eq "windows"} { + return [list fuzzcheck.exe $lFuzzDb] + } else { + set lRet [list [trd_get_bin_name fuzzcheck] $lFuzzDb] + if {[lsearch $sanBuilds $buildname]>=0} { + lappend lRet [trd_get_bin_name fuzzcheck-asan] $lFuzzDb + if {$::tcl_platform(os) ne "OpenBSD"} { + lappend lRet [trd_get_bin_name fuzzcheck-ubsan] $lFuzzDb + } + } + lappend lRet {sessionfuzz run} $lSessionDb + return $lRet + } +} + + +proc trd_all_configs {} { + trd_import + set all_configs +} + +proc trimscript {text} { + set text [string map {"\n " "\n"} [string trim $text]] +} + +proc make_sh_script {srcdir opts cflags makeOpts configOpts} { + + set tcldir [::tcl::pkgconfig get libdir,install] + set myopts "" + if {[info exists ::env(OPTS)]} { + append myopts "# From environment variable:\n" + append myopts "OPTS=$::env(OPTS)\n\n" + } + foreach o [lsort $opts] { + append myopts "OPTS=\"\$OPTS $o\"\n" + } + + return [trimscript [subst -nocommands { + set -e + if [ "\$#" -ne 1 ] ; then + echo "Usage: \$0 <target>" + exit -1 + fi + + SRCDIR="$srcdir" + TCLDIR="$tcldir" + + if [ ! -f Makefile ] ; then + \$SRCDIR/configure --with-tcl=\$TCLDIR $configOpts + fi + + $myopts + CFLAGS="$cflags" + + make \$1 "CFLAGS=\$CFLAGS" "OPTS=\$OPTS" $makeOpts + }]] +} + +# Generate the text of a *.bat script. +# +proc make_bat_file {srcdir opts cflags makeOpts} { + set srcdir [file nativename [file normalize $srcdir]] + + return [trimscript [subst -nocommands { + set TARGET=%1 + set TMP=%CD% + nmake /f $srcdir\\Makefile.msc TOP="$srcdir" %TARGET% "CCOPTS=$cflags" "OPTS=$opts" $makeOpts + }]] +} + + +# Generate the text of a shell script. +# +proc make_script {cfg srcdir bMsvc} { + set opts [list] ;# OPTS value + set cflags [expr {$bMsvc ? "-Zi" : "-g"}] ;# CFLAGS value + set makeOpts [list] ;# Extra args for [make] + set configOpts [list] ;# Extra args for [configure] + + # Define either SQLITE_OS_WIN or SQLITE_OS_UNIX, as appropriate. + if {$::tcl_platform(os) eq "Windows NT"} { + lappend opts -DSQLITE_OS_WIN=1 + } else { + lappend opts -DSQLITE_OS_UNIX=1 + } + + # Unless the configuration specifies -DHAVE_USLEEP=0, set -DHAVE_USLEEP=1. + # + if {[lsearch $cfg "-DHAVE_USLEEP=0"]<0} { + lappend cfg -DHAVE_USLEEP=1 + } + + # Loop through the parameters of the nominated configuration, updating + # $opts, $cflags, $makeOpts and $configOpts along the way. Rules are as + # follows: + # + # 1. If the parameter begins with "-D", add it to $opts. + # + # 2. If the parameter begins with "--" add it to $configOpts. Unless + # this command is preparing a script for MSVC - then add an + # equivalent to $makeOpts or $opts. + # + # 3. If the parameter begins with "-" add it to $cflags. If in MSVC + # mode and the parameter is an -O<integer> option, instead add + # an OPTIMIZATIONS=<integer> switch to $makeOpts. + # + # 4. If none of the above apply, add the parameter to $makeOpts + # + foreach param $cfg { + + if {[string range $param 0 1]=="-D"} { + lappend opts $param + continue + } + + if {[string range $param 0 1]=="--"} { + if {$bMsvc==0} { + lappend configOpts $param + } else { + + switch -- $param { + --disable-amalgamation { + lappend makeOpts USE_AMALGAMATION=0 + } + --disable-shared { + lappend makeOpts USE_CRT_DLL=0 DYNAMIC_SHELL=0 + } + --enable-fts5 { + lappend opts -DSQLITE_ENABLE_FTS5 + } + --enable-shared { + lappend makeOpts USE_CRT_DLL=1 DYNAMIC_SHELL=1 + } + --enable-session { + lappend opts -DSQLITE_ENABLE_PREUPDATE_HOOK + lappend opts -DSQLITE_ENABLE_SESSION + } + --enable-all { + } + --with-debug { + # lappend makeOpts OPTIMIZATIONS=0 + lappend opts -DSQLITE_DEBUG + } + default { + error "Cannot translate $param for MSVC" + } + } + } + + continue + } + + if {[string range $param 0 0]=="-"} { + + if {$bMsvc} { + if {[regexp -- {^-O(\d+)$} $param -> level]} { + lappend makeOpts OPTIMIZATIONS=$level + continue + } + if {$param eq "-fsanitize=address,undefined"} { + lappend makeOpts ASAN=1 + continue + } + } + + lappend cflags $param + continue + } + + lappend makeOpts $param + } + + if {$bMsvc==0} { + set zRet [make_sh_script $srcdir $opts $cflags $makeOpts $configOpts] + } else { + set zRet [make_bat_file $srcdir $opts $cflags $makeOpts] + } +} + +# Usage: +# +# trd_buildscript CONFIG SRCDIR MSVC +# +# This command returns the full text of a script (either a shell script or +# an ms-dos bat file) that may be used to build SQLite source code according +# to a nominated configuration. +# +# Parameter CONFIG must be a configuration defined above in the ::trd::build +# array. SRCDIR is the root directory of an SQLite source tree (the parent +# directory of that containing this script). MSVC is a boolean - true to +# use the MSVC compiler, false otherwise. +# +proc trd_buildscript {config srcdir bMsvc} { + trd_import + + # Ensure that the named configuration exists. + if {![info exists build($config)]} { + if {$config!="help"} { + puts "No such build config: $config" + } + puts "Available configurations: [lsort [array names build]]" + flush stdout + exit 1 + } + + # Generate and return the script. + return [make_script $build($config) $srcdir $bMsvc] +} + +# Usage: +# +# trd_test_script_properties PATH +# +# The argument must be a path to a Tcl test script. This function scans the +# first 100 lines of the script for lines that look like: +# +# TESTRUNNER: <properties> +# +# where <properties> is a list of identifiers, each of which defines a +# property of the test script. Example properties are "slow" or "superslow". +# +proc trd_test_script_properties {path} { + # Use this global array as a cache: + global trd_test_script_properties_cache + + if {![info exists trd_test_script_properties_cache($path)]} { + set fd [open $path] + set ret [list] + for {set line 0} {$line < 100 && ![eof $fd]} {incr line} { + set text [gets $fd] + if {[string match -nocase *testrunner:* $text]} { + regexp -nocase {.*testrunner:(.*)} $text -> properties + lappend ret {*}$properties + } + } + set trd_test_script_properties_cache($path) $ret + close $fd + } + + set trd_test_script_properties_cache($path) +} + +# Usage: +# +# trd_get_bin_name executable-file-name +# +# If the tcl platform is "unix", return $bin, else return +# ${bin}.exe. +proc trd_get_bin_name {bin} { + global tcl_platform + if {"unix" eq $tcl_platform(platform)} {return $bin} + return $bin.exe +} diff --git a/test/testrunner_estwork.tcl b/test/testrunner_estwork.tcl new file mode 100644 index 0000000000..c139394a56 --- /dev/null +++ b/test/testrunner_estwork.tcl @@ -0,0 +1,488 @@ +# Estimated relative cost of various jobs, based on the "estkey" field. +# Computed by the "test/testrunner.tcl estwork" command. +# +set estwork((fuzzcheck)) 1291 +set estwork((fuzzcheck-asan)) 2427 +set estwork((fuzzcheck-ubsan)) 2749 +set estwork((sessionfuzz)) 1276 +set estwork((sqlite3)) 1281 +set estwork((testfixture)) 1546 +set estwork(aggerror.test) 5 +set estwork(alter.test) 20 +set estwork(alter3.test) 2 +set estwork(alterauth.test) 5 +set estwork(altercol.test) 5 +set estwork(alterdropcol.test) 17 +set estwork(alterlegacy.test) 2 +set estwork(altertab.test) 34 +set estwork(altertab2.test) 2 +set estwork(altertab3.test) 12 +set estwork(amatch1.test) 40 +set estwork(atof1.test) 146 +set estwork(atomic2.test) 2 +set estwork(auth.test) 2 +set estwork(autoindex1.test) 5 +set estwork(autoindex3.test) 5 +set estwork(autovacuum.test) 15 +set estwork(avfs.test) 16 +set estwork(avtrans.test) 285 +set estwork(backup2.test) 15 +set estwork(bestindex4.test) 12 +set estwork(bigrow.test) 5 +set estwork(bitvec.test) 94 +set estwork(bld) 99 +set estwork(blob.test) 2 +set estwork(boundary1.test) 8 +set estwork(boundary2.test) 16 +set estwork(boundary3.test) 14 +set estwork(btree01.test) 20 +set estwork(busy.test) 2 +set estwork(busy2.test) 288 +set estwork(capi3.test) 5 +set estwork(capi3b.test) 6 +set estwork(capi3c.test) 5 +set estwork(capi3d.test) 8 +set estwork(capi3e.test) 20 +set estwork(changes.test) 11 +set estwork(chunksize.test) 5 +set estwork(closure01.test) 63 +set estwork(collate5.test) 2 +set estwork(conflict.test) 8 +set estwork(conflict2.test) 4 +set estwork(conflict3.test) 8 +set estwork(corrupt2.test) 2 +set estwork(corrupt4.test) 91 +set estwork(corrupt7.test) 5 +set estwork(corrupt8.test) 3 +set estwork(corruptB.test) 4 +set estwork(corruptF.test) 6 +set estwork(count.test) 18 +set estwork(crash8.test) 14 +set estwork(createtab.test) 3 +set estwork(csv01.test) 7 +set estwork(cursorhint.test) 4 +set estwork(date.test) 17 +set estwork(date4.test) 64 +set estwork(date5.test) 3 +set estwork(dbdata.test) 77 +set estwork(dbfuzz001.test) 2 +set estwork(dbstatus.test) 4 +set estwork(decimal.test) 2 +set estwork(delete.test) 4 +set estwork(diskfull.test) 34 +set estwork(e_blobbytes.test) 11 +set estwork(e_createtable.test) 14 +set estwork(e_delete.test) 3 +set estwork(e_droptrigger.test) 6 +set estwork(e_dropview.test) 2 +set estwork(e_expr.test) 251 +set estwork(e_select.test) 5 +set estwork(e_select2.test) 5 +set estwork(e_vacuum.test) 6 +set estwork(e_walauto.test) 51 +set estwork(e_walckpt.test) 6 +set estwork(enc.test) 2 +set estwork(enc3.test) 11 +set estwork(enc4.test) 3 +set estwork(eval.test) 2 +set estwork(exists.test) 4 +set estwork(existsexpr.test) 44 +set estwork(expert1.test) 14 +set estwork(expr.test) 5 +set estwork(filectrl.test) 5 +set estwork(filefmt.test) 6 +set estwork(fkey2.test) 9 +set estwork(fpconv1.test) 40 +set estwork(fts3aa.test) 18 +set estwork(fts3ad.test) 10 +set estwork(fts3ag.test) 6 +set estwork(fts3aj.test) 8 +set estwork(fts3am.test) 5 +set estwork(fts3auto.test) 29 +set estwork(fts3b.test) 41 +set estwork(fts3c.test) 22 +set estwork(fts3conf.test) 3 +set estwork(fts3corrupt4.test) 7 +set estwork(fts3corrupt5.test) 8 +set estwork(fts3corrupt6.test) 13 +set estwork(fts3d.test) 2 +set estwork(fts3defer2.test) 2 +set estwork(fts3expr.test) 10 +set estwork(fts3expr2.test) 8 +set estwork(fts3expr3.test) 145 +set estwork(fts3f.test) 9 +set estwork(fts3first.test) 3 +set estwork(fts3matchinfo.test) 4 +set estwork(fts3misc.test) 54 +set estwork(fts3prefix.test) 4 +set estwork(fts3prefix2.test) 8 +set estwork(fts3query.test) 23 +set estwork(fts3varint.test) 2 +set estwork(fts4aa.test) 34 +set estwork(fts4content.test) 5 +set estwork(fts4incr.test) 19 +set estwork(fts4noti.test) 3 +set estwork(fts4opt.test) 129 +set estwork(fts4unicode.test) 45 +set estwork(fts5aa.test) 442 +set estwork(fts5ab.test) 54 +set estwork(fts5ad.test) 33 +set estwork(fts5ae.test) 3 +set estwork(fts5af.test) 13 +set estwork(fts5ag.test) 13 +set estwork(fts5ah.test) 265 +set estwork(fts5al.test) 2 +set estwork(fts5auto.test) 92 +set estwork(fts5connect.test) 5 +set estwork(fts5content.test) 9 +set estwork(fts5contentless.test) 23 +set estwork(fts5contentless2.test) 189 +set estwork(fts5contentless4.test) 234 +set estwork(fts5contentless5.test) 9 +set estwork(fts5delete.test) 62 +set estwork(fts5doclist.test) 11 +set estwork(fts5expr.test) 10 +set estwork(fts5full.test) 42 +set estwork(fts5hash.test) 17 +set estwork(fts5integrity.test) 84 +set estwork(fts5interrupt.test) 16 +set estwork(fts5locale.test) 5 +set estwork(fts5matchinfo.test) 11 +set estwork(fts5merge.test) 29 +set estwork(fts5merge2.test) 9 +set estwork(fts5misc.test) 8 +set estwork(fts5multiclient.test) 2 +set estwork(fts5optimize.test) 13 +set estwork(fts5optimize2.test) 737 +set estwork(fts5optimize3.test) 280 +set estwork(fts5origintext.test) 144 +set estwork(fts5origintext3.test) 8 +set estwork(fts5origintext4.test) 30 +set estwork(fts5origintext5.test) 462 +set estwork(fts5origintext6.test) 55 +set estwork(fts5porter.test) 57 +set estwork(fts5query.test) 8 +set estwork(fts5restart.test) 10 +set estwork(fts5rowid.test) 35 +set estwork(fts5secure.test) 61 +set estwork(fts5secure3.test) 796 +set estwork(fts5secure4.test) 141 +set estwork(fts5secure5.test) 29 +set estwork(fts5secure6.test) 33 +set estwork(fts5secure7.test) 680 +set estwork(fts5simple.test) 7 +set estwork(fts5simple2.test) 2 +set estwork(fts5synonym.test) 13 +set estwork(fts5synonym2.test) 384 +set estwork(fts5tok2.test) 92 +set estwork(fts5tokenizer.test) 2 +set estwork(fts5tokenizer3.test) 8 +set estwork(fts5trigram.test) 2 +set estwork(fts5unicode2.test) 28 +set estwork(fts5unicode3.test) 72 +set estwork(fts5unindexed.test) 7 +set estwork(fts5update.test) 161 +set estwork(fts5update2.test) 11 +set estwork(fts5vocab.test) 11 +set estwork(fts5vocab2.test) 15 +set estwork(func.test) 36 +set estwork(fuzz) 68 +set estwork(fuzz-oss1.test) 11 +set {estwork(fuzzcheck --slice)} 499 +set {estwork(fuzzcheck fuzzdata1.db)} 301 +set {estwork(fuzzcheck fuzzdata2.db)} 275 +set {estwork(fuzzcheck fuzzdata3.db)} 72 +set {estwork(fuzzcheck fuzzdata4.db)} 30 +set {estwork(fuzzcheck fuzzdata5.db)} 299 +set {estwork(fuzzcheck fuzzdata6.db)} 91 +set {estwork(fuzzcheck fuzzdata7.db)} 201 +set {estwork(fuzzcheck fuzzdata8.db)} 331 +set {estwork(fuzzcheck-asan --slice)} 2037 +set {estwork(fuzzcheck-asan fuzzdata1.db)} 3112 +set {estwork(fuzzcheck-asan fuzzdata2.db)} 8753 +set {estwork(fuzzcheck-asan fuzzdata3.db)} 459 +set {estwork(fuzzcheck-asan fuzzdata4.db)} 123 +set {estwork(fuzzcheck-asan fuzzdata5.db)} 1178 +set {estwork(fuzzcheck-asan fuzzdata6.db)} 1356 +set {estwork(fuzzcheck-asan fuzzdata7.db)} 938 +set {estwork(fuzzcheck-asan fuzzdata8.db)} 1451 +set {estwork(fuzzcheck-ubsan --slice)} 1729 +set {estwork(fuzzcheck-ubsan fuzzdata1.db)} 1180 +set {estwork(fuzzcheck-ubsan fuzzdata2.db)} 876 +set {estwork(fuzzcheck-ubsan fuzzdata3.db)} 306 +set {estwork(fuzzcheck-ubsan fuzzdata4.db)} 95 +set {estwork(fuzzcheck-ubsan fuzzdata5.db)} 1356 +set {estwork(fuzzcheck-ubsan fuzzdata6.db)} 333 +set {estwork(fuzzcheck-ubsan fuzzdata7.db)} 883 +set {estwork(fuzzcheck-ubsan fuzzdata8.db)} 1124 +set estwork(fuzzer1.test) 6 +set estwork(gencol1.test) 3 +set estwork(hook.test) 2 +set estwork(in4.test) 4 +set estwork(in7.test) 6 +set estwork(incrblob2.test) 2 +set estwork(incrblob3.test) 5 +set estwork(incrvacuum.test) 10 +set estwork(incrvacuum2.test) 17 +set estwork(incrvacuum3.test) 17 +set estwork(index.test) 3 +set estwork(index2.test) 8 +set estwork(index4.test) 33 +set estwork(index5.test) 99 +set estwork(index6.test) 2 +set estwork(indexexpr1.test) 2 +set estwork(insert3.test) 5 +set estwork(insert4.test) 2 +set estwork(intarray.test) 8 +set estwork(intck1.test) 34 +set estwork(intck2.test) 37 +set estwork(interrupt.test) 28 +set estwork(io.test) 3 +set estwork(join.test) 3 +set estwork(join3.test) 6 +set estwork(join5.test) 20 +set estwork(join7.test) 2 +set estwork(join8.test) 3 +set estwork(join9.test) 2 +set estwork(joinA.test) 11 +set estwork(joinB.test) 7 +set estwork(joinC.test) 6 +set estwork(joinD.test) 168 +set estwork(json103.test) 6 +set estwork(json106.test) 690 +set estwork(keyword1.test) 3 +set estwork(like2.test) 2 +set estwork(like3.test) 2 +set estwork(limit.test) 3 +set estwork(literal.test) 6 +set estwork(lock.test) 39 +set estwork(lock4.test) 2 +set estwork(lock5.test) 4 +set estwork(make) 102 +set estwork(manydb.test) 12 +set estwork(mem5.test) 2 +set estwork(memdb.test) 9 +set estwork(memdb1.test) 2 +set estwork(memjournal2.test) 164 +set estwork(memsubsys1.test) 8 +set estwork(misc1.test) 6 +set estwork(misc2.test) 6 +set estwork(misc5.test) 11 +set estwork(misc8.test) 5 +set estwork(mmap1.test) 8 +set estwork(mmap2.test) 10 +set estwork(mmapwarm.test) 2 +set estwork(multiplex.test) 30 +set estwork(multiplex2.test) 7 +set estwork(nan.test) 2 +set estwork(notify3.test) 5 +set estwork(orderby1.test) 11 +set estwork(orderby2.test) 2 +set estwork(orderby5.test) 11 +set estwork(orderby6.test) 6 +set estwork(orderby8.test) 20 +set estwork(orderbyA.test) 2 +set estwork(oserror.test) 7 +set estwork(ovfl.test) 11 +set estwork(pager1.test) 81 +set estwork(pager2.test) 129 +set estwork(pagesize.test) 3 +set estwork(percentile.test) 86 +set estwork(pragma.test) 4 +set estwork(pragma4.test) 18 +set estwork(printf.test) 21 +set estwork(printf2.test) 5 +set estwork(quota.test) 14 +set estwork(randexpr1.test) 35 +set estwork(rbu10.test) 16 +set estwork(rbu13.test) 5 +set estwork(rbuexlock.test) 5 +set estwork(rbumisc.test) 2 +set estwork(rbutemplimit.test) 2 +set estwork(readonly.test) 95 +set estwork(recover.test) 3 +set estwork(recover1.test) 7 +set estwork(recovercorrupt3.test) 5 +set estwork(recoverold.test) 7 +set estwork(recoverpgsz.test) 6 +set estwork(recoverrowid.test) 3 +set estwork(returning1.test) 6 +set estwork(rollback2.test) 2 +set estwork(round1.test) 261 +set estwork(rowhash.test) 85 +set estwork(rowid.test) 3 +set estwork(rowvalue.test) 2 +set estwork(rowvalue2.test) 38 +set estwork(rowvalue4.test) 2 +set estwork(rowvalueA.test) 2 +set estwork(rowvaluevtab.test) 2 +set estwork(rtree1.test) 4 +set estwork(rtree2.test) 758 +set estwork(rtree6.test) 6 +set estwork(rtree8.test) 15 +set estwork(rtree9.test) 15 +set estwork(rtreeA.test) 7 +set estwork(rtreeB.test) 7 +set estwork(rtreeE.test) 47 +set estwork(rtreeH.test) 24 +set estwork(rtreecheck.test) 2 +set estwork(rtreedoc.test) 8 +set estwork(rtreedoc3.test) 76 +set estwork(savepoint.test) 10 +set estwork(savepoint2.test) 57 +set estwork(schema2.test) 15 +set estwork(schema3.test) 2 +set estwork(schema5.test) 5 +set estwork(select1.test) 2 +set estwork(select2.test) 17 +set estwork(select3.test) 2 +set estwork(selectA.test) 2 +set estwork(selectB.test) 2 +set estwork(selectG.test) 30 +set estwork(session1.test) 5 +set estwork(session2.test) 33 +set estwork(session5.test) 63 +set estwork(session9.test) 3 +set estwork(sessionG.test) 60 +set estwork(sessionH.test) 18 +set estwork(sessionalter.test) 3 +set estwork(sessionat.test) 2 +set estwork(sessionblob.test) 3 +set {estwork(sessionfuzz sessionfuzz-data1.db)} 5 +set estwork(sessioninvert.test) 2 +set estwork(sessionnoop.test) 2 +set estwork(sessionnoop2.test) 8 +set estwork(sessionrebase.test) 8 +set estwork(shared.test) 7 +set estwork(sharedA.test) 48 +set estwork(shell1.test) 36 +set estwork(shell2.test) 11 +set estwork(shell3.test) 3 +set estwork(shell4.test) 2 +set estwork(shell5.test) 16 +set estwork(shell6.test) 3 +set estwork(shell8.test) 104 +set estwork(shell9.test) 3 +set estwork(shellA.test) 2 +set estwork(shmlock.test) 27 +set estwork(sidedelete.test) 10 +set estwork(skipscan1.test) 7 +set estwork(skipscan2.test) 5 +set estwork(sort.test) 38 +set estwork(sort2.test) 540 +set estwork(sort5.test) 16 +set estwork(spellfix.test) 5 +set estwork(spellfix2.test) 5 +set estwork(spellfix4.test) 11 +set estwork(starschema1.test) 2 +set estwork(strict1.test) 5 +set estwork(swarmvtab.test) 110 +set estwork(swarmvtab3.test) 14 +set estwork(syscall.test) 4 +set estwork(table.test) 62 +set estwork(tableapi.test) 8 +set estwork(tcl) 1 +set estwork(tclsqlite.test) 29 +set estwork(temptable2.test) 274 +set estwork(thread3.test) 21 +set estwork(timediff1.test) 5 +set estwork(tkt-2d1a5c67d.test) 6 +set estwork(tkt-38cb5df375.test) 2 +set estwork(tkt-4dd95f6943.test) 2 +set estwork(tkt-5e10420e8d.test) 5 +set estwork(tkt-6bfb98dfc0.test) 5 +set estwork(tkt-80e031a00f.test) 25 +set estwork(tkt-9d68c883.test) 3 +set estwork(tkt-9f2eb3abac.test) 5 +set estwork(tkt-b1d3a2e531.test) 5 +set estwork(tkt-b72787b1.test) 5 +set estwork(tkt-b75a9ca6b0.test) 8 +set estwork(tkt-d11f09d36e.test) 9 +set estwork(tkt-fc62af4523.test) 10 +set estwork(tkt1435.test) 7 +set estwork(tkt1644.test) 5 +set estwork(tkt1667.test) 20 +set estwork(tkt1873.test) 2 +set estwork(tkt2192.test) 3 +set estwork(tkt2285.test) 19 +set estwork(tkt2332.test) 2 +set estwork(tkt2409.test) 24 +set estwork(tkt3334.test) 5 +set estwork(tkt3357.test) 10 +set estwork(tkt3630.test) 2 +set estwork(tkt3832.test) 8 +set estwork(tkt3838.test) 5 +set estwork(tkt3918.test) 3 +set estwork(tkt4018.test) 48 +set estwork(tokenize.test) 9 +set estwork(tpch01.test) 6 +set estwork(trans.test) 258 +set estwork(trigger2.test) 15 +set estwork(trigger5.test) 2 +set estwork(trigger8.test) 30 +set estwork(triggerA.test) 36 +set estwork(triggerB.test) 4 +set estwork(triggerC.test) 67 +set estwork(triggerD.test) 3 +set estwork(types.test) 2 +set estwork(types2.test) 2 +set estwork(unionall2.test) 9 +set estwork(unionvtab.test) 2 +set estwork(update.test) 7 +set estwork(upsert3.test) 6 +set estwork(upsert5.test) 5 +set estwork(vacuum.test) 2 +set estwork(vacuum5.test) 3 +set estwork(vacuum6.test) 174 +set estwork(vacuummem.test) 132 +set estwork(varint.test) 8 +set estwork(view.test) 2 +set estwork(vtab1.test) 13 +set estwork(vtab6.test) 12 +set estwork(vtabC.test) 33 +set estwork(vtabD.test) 56 +set estwork(vtab_alter.test) 6 +set estwork(wal.test) 38 +set estwork(wal2.test) 7 +set estwork(wal3.test) 196 +set estwork(wal4.test) 70 +set estwork(wal5.test) 22 +set estwork(wal64k.test) 20 +set estwork(wal7.test) 3 +set estwork(wal9.test) 24 +set estwork(walbak.test) 2 +set estwork(walcksum.test) 3 +set estwork(walcrash4.test) 59 +set estwork(waloverwrite.test) 3 +set estwork(walpersist.test) 6 +set estwork(walprotocol2.test) 2 +set estwork(walro2.test) 11 +set estwork(walsetlk.test) 1610 +set estwork(walsetlk3.test) 3 +set estwork(walsetlk_recover.test) 193 +set estwork(walshared.test) 5 +set estwork(walvfs.test) 476 +set estwork(where.test) 24 +set estwork(where3.test) 4 +set estwork(where6.test) 2 +set estwork(where7.test) 20 +set estwork(where8.test) 95 +set estwork(where9.test) 16 +set estwork(whereB.test) 5 +set estwork(whereE.test) 6 +set estwork(whereN.test) 2 +set estwork(window1.test) 5 +set estwork(window2.test) 8 +set estwork(window3.test) 89 +set estwork(window4.test) 3 +set estwork(window5.test) 2 +set estwork(window8.test) 33 +set estwork(windowA.test) 5 +set estwork(windowD.test) 6 +set estwork(with1.test) 114 +set estwork(with2.test) 4 +set estwork(with5.test) 2 +set estwork(withM.test) 4 +set estwork(without_rowid1.test) 2 +set estwork(without_rowid3.test) 9 +set estwork(without_rowid4.test) 6 diff --git a/test/thread001.test b/test/thread001.test index a796c57b4a..1390f0d991 100644 --- a/test/thread001.test +++ b/test/thread001.test @@ -15,6 +15,7 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl if {[run_thread_tests]==0} { finish_test ; return } +ifcapable !shared_cache { finish_test ; return } set ::enable_shared_cache [sqlite3_enable_shared_cache] @@ -141,5 +142,6 @@ foreach {tn same_db shared_cache} [list \ } sqlite3_enable_shared_cache $::enable_shared_cache +catch { db close } set sqlite_open_file_count 0 finish_test diff --git a/test/thread002.test b/test/thread002.test index b39c9ae7cd..3e9439e213 100644 --- a/test/thread002.test +++ b/test/thread002.test @@ -19,7 +19,7 @@ set testdir [file dirname $argv0] set do_not_use_codec 1 source $testdir/tester.tcl if {[run_thread_tests]==0} { finish_test ; return } - +ifcapable !shared_cache { finish_test ; return } db close set ::enable_shared_cache [sqlite3_enable_shared_cache 1] diff --git a/test/thread3.test b/test/thread3.test new file mode 100644 index 0000000000..79a75bdbb7 --- /dev/null +++ b/test/thread3.test @@ -0,0 +1,77 @@ +# 2023 May 13 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +set testdir [file dirname $argv0] + +source $testdir/tester.tcl +source $testdir/lock_common.tcl +if {[run_thread_tests]==0} { finish_test ; return } + +set testprefix thread3 + +do_execsql_test 1.0 { + CREATE TABLE t1(a, b); + PRAGMA journal_mode = DELETE; +} {delete} + +proc wait_for_var {varname} { + if {0==[uplevel [list info exists $varname]]} { + uplevel [list vwait $varname] + } + uplevel [list set $varname] +} + +set nAttempt 250 + +do_test 1.1 { + for {set i 0} {$i < $nAttempt} {incr i} { + unset -nocomplain X + unset -nocomplain Y + + sqlthread spawn X { + sqlite3 dbI test.db + dbI timeout 100 + set rc 1 + set nBusy 0 + while {$rc} { + set rc [catch { + dbI eval { INSERT INTO t1 VALUES(203, 'message') RETURNING a; } + } msg] + if {$rc} { incr nBusy } + } + dbI close + set nBusy + } + + sqlthread spawn Y { + sqlite3 dbR test.db + catch { + dbR eval { SELECT count(*) FROM t1 } + } msg + dbR close + set msg + } + + wait_for_var X + wait_for_var Y + incr nTotalBusy $X + } + + execsql { SELECT count(*) FROM t1 } + set {} {} +} {} + +do_execsql_test "1.Total BUSY errors: $nTotalBusy .2" { + SELECT count(*) FROM t1; +} $nAttempt + +finish_test diff --git a/test/thread_common.tcl b/test/thread_common.tcl index 6b17082ad4..1ebc6573a9 100644 --- a/test/thread_common.tcl +++ b/test/thread_common.tcl @@ -95,7 +95,7 @@ proc run_thread_tests {{print_warning 0}} { if {[info commands sqlthread] eq ""} { set zProblem "SQLite build is not threadsafe" } - if {![info exists ::tcl_platform(threaded)]} { + if {![tcl::pkgconfig get threaded]} { set zProblem "Linked against a non-threadsafe Tcl build" } if {[info exists zProblem]} { @@ -107,4 +107,3 @@ proc run_thread_tests {{print_warning 0}} { } return 0 - diff --git a/test/threadtest3.c b/test/threadtest3.c index 6062b64285..3a12c5889b 100644 --- a/test/threadtest3.c +++ b/test/threadtest3.c @@ -78,15 +78,31 @@ #include <sqlite3.h> -#include <unistd.h> -#include <stdio.h> -#include <pthread.h> -#include <assert.h> -#include <sys/types.h> -#include <sys/stat.h> -#include <string.h> -#include <fcntl.h> -#include <errno.h> + +#ifdef _WIN32 +# include <stdio.h> +# include <string.h> +# include <assert.h> +# include <process.h> +# include <windows.h> +# include <sys/types.h> +# include <sys/stat.h> +# include <errno.h> +# include <fcntl.h> +# include <io.h> +#else +# include <unistd.h> +# include <stdio.h> +# include <pthread.h> +# include <assert.h> +# include <sys/types.h> +# include <sys/stat.h> +# include <string.h> +# include <fcntl.h> +# include <errno.h> + +# define O_BINARY 0 +#endif #include "test_multiplex.h" @@ -436,8 +452,13 @@ struct Thread { int iTid; /* Thread number within test */ void* pArg; /* Pointer argument passed by caller */ +#ifdef _WIN32 + uintptr_t winTid; /* Thread handle */ +#else pthread_t tid; /* Thread id */ +#endif char *(*xProc)(int, void*); /* Thread main proc */ + char *zRes; /* Value returned by xProc */ Thread *pNext; /* Next in this list of threads */ }; @@ -471,9 +492,13 @@ static void print_and_free_err(Error *p){ static void system_error(Error *pErr, int iSys){ pErr->rc = iSys; +#if _WIN32 + pErr->zErr = sqlite3_mprintf("%s", strerror(iSys)); +#else pErr->zErr = (char *)sqlite3_malloc(512); strerror_r(iSys, pErr->zErr, 512); pErr->zErr[511] = '\0'; +#endif } static void sqlite_error( @@ -512,7 +537,7 @@ static void clear_error_x( } static int busyhandler(void *pArg, int n){ - usleep(10*1000); + sqlite3_sleep(10); return 1; } @@ -749,10 +774,20 @@ static void integrity_check_x( } } +#ifdef _WIN32 +static unsigned __stdcall launch_thread_main(void *pArg){ + Thread *p = (Thread *)pArg; + p->zRes = p->xProc(p->iTid, p->pArg); + _endthreadex(0); + return 0; /* NOT REACHED */ +} +#else static void *launch_thread_main(void *pArg){ Thread *p = (Thread *)pArg; - return (void *)p->xProc(p->iTid, p->pArg); + p->zRes = p->xProc(p->iTid, p->pArg); + return 0; } +#endif static void launch_thread_x( Error *pErr, /* IN/OUT: Error code */ @@ -771,7 +806,13 @@ static void launch_thread_x( p->pArg = pArg; p->xProc = xProc; +#ifdef _WIN32 + rc = SQLITE_OK; + p->winTid = _beginthreadex(0, 0, launch_thread_main, (void*)p, 0, 0); + if( p->winTid==0 ) rc = errno ? errno : rc; +#else rc = pthread_create(&p->tid, NULL, launch_thread_main, (void *)p); +#endif if( rc!=0 ){ system_error(pErr, rc); sqlite3_free(p); @@ -789,29 +830,47 @@ static void join_all_threads_x( Thread *p; Thread *pNext; for(p=pThreads->pThread; p; p=pNext){ +#ifndef _WIN32 void *ret; - pNext = p->pNext; +#endif int rc; + pNext = p->pNext; + +#ifdef _WIN32 + do { + rc = WaitForSingleObjectEx((HANDLE)p->winTid, INFINITE, TRUE); + }while( rc==WAIT_IO_COMPLETION ); + CloseHandle((HANDLE)p->winTid); +#else rc = pthread_join(p->tid, &ret); +#endif + if( rc!=0 ){ if( pErr->rc==SQLITE_OK ) system_error(pErr, rc); }else{ - printf("Thread %d says: %s\n", p->iTid, (ret==0 ? "..." : (char *)ret)); + printf("Thread %d says: %s\n", p->iTid, (p->zRes==0 ? "..." : p->zRes)); fflush(stdout); } + sqlite3_free(p->zRes); sqlite3_free(p); } pThreads->pThread = 0; } +#ifdef _WIN32 +# define THREADTEST3_STAT _stat +#else +# define THREADTEST3_STAT stat +#endif + static i64 filesize_x( Error *pErr, const char *zFile ){ i64 iRet = 0; if( pErr->rc==SQLITE_OK ){ - struct stat sStat; - if( stat(zFile, &sStat) ){ + struct THREADTEST3_STAT sStat; + if( THREADTEST3_STAT(zFile, &sStat) ){ iRet = -1; }else{ iRet = sStat.st_size; @@ -836,12 +895,12 @@ static void filecopy_x( int fd2; unlink(zTo); - fd1 = open(zFrom, O_RDONLY); + fd1 = open(zFrom, O_RDONLY|O_BINARY); if( fd1<0 ){ system_error(pErr, errno); return; } - fd2 = open(zTo, O_RDWR|O_CREAT|O_EXCL, 0644); + fd2 = open(zTo, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0644); if( fd2<0 ){ system_error(pErr, errno); close(fd1); @@ -967,7 +1026,7 @@ static char *walthread1_ckpt_thread(int iTid, void *pArg){ opendb(&err, &db, "test.db", 0); while( !timetostop(&err) ){ - usleep(500*1000); + sqlite3_sleep(500); execsql(&err, &db, "PRAGMA wal_checkpoint"); if( err.rc==SQLITE_OK ) nCkpt++; clear_error(&err, SQLITE_BUSY); @@ -1415,7 +1474,7 @@ static void dynamic_triggers(int nMs){ launch_thread(&err, &threads, dynamic_triggers_2, 0); launch_thread(&err, &threads, dynamic_triggers_2, 0); - sleep(2); + sqlite3_sleep(2*1000); sqlite3_enable_shared_cache(0); launch_thread(&err, &threads, dynamic_triggers_2, 0); @@ -1433,6 +1492,7 @@ static void dynamic_triggers(int nMs){ #include "tt3_lookaside1.c" #include "tt3_vacuum.c" #include "tt3_stress.c" +#include "tt3_shared.c" int main(int argc, char **argv){ struct ThreadTest { @@ -1457,6 +1517,7 @@ int main(int argc, char **argv){ { vacuum1, "vacuum1", 10000 }, { stress1, "stress1", 10000 }, { stress2, "stress2", 60000 }, + { shared1, "shared1", 10000 }, }; static char *substArgv[] = { 0, "*", 0 }; int i, iArg; diff --git a/test/threadtest5.c b/test/threadtest5.c new file mode 100644 index 0000000000..6e6610ff66 --- /dev/null +++ b/test/threadtest5.c @@ -0,0 +1,339 @@ +/* +** 2021-05-12 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** Testing threading behavior when multiple database connections in separate +** threads of the same process are all talking to the same database file. +** +** For best results, ensure that SQLite is compiled with HAVE_USLEEP=1 +** +** Only works on unix platforms. +** +** Usage: +** +** ./threadtest5 ?DATABASE? +** +** If DATABASE is omitted, it defaults to using file:/mem?vfs=memdb. +*/ +#include "sqlite3.h" +#include <pthread.h> +#include <stdio.h> +#include <unistd.h> +#include <stdlib.h> +#include <string.h> +#include <stdarg.h> + +/* Name of the in-memory database */ +static char *zDbName = 0; + +/* True for debugging */ +static int eVerbose = 0; + +/* If rc is not SQLITE_OK, then print an error message and stop +** the test. +*/ +static void error_out(int rc, const char *zCtx, int lineno){ + if( rc!=SQLITE_OK ){ + fprintf(stderr, "error %d at %d in \"%s\"\n", rc, lineno, zCtx); + exit(-1); + } +} + +#if 0 +/* Return the number of milliseconds since the Julian epoch (-4714-11-24). +*/ +static sqlite3_int64 gettime(void){ + sqlite3_int64 tm; + sqlite3_vfs *pVfs = sqlite3_vfs_find(0); + pVfs->xCurrentTimeInt64(pVfs, &tm); + return tm; +} +#endif + +/* Run the SQL in the second argument. +*/ +static int exec( + sqlite3 *db, + const char *zId, + int lineno, + const char *zFormat, + ... +){ + int rc; + va_list ap; + char *zSql; + va_start(ap, zFormat); + zSql = sqlite3_vmprintf(zFormat, ap); + va_end(ap); + if( eVerbose){ + printf("%s:%d: [%s]\n", zId, lineno, zSql); + fflush(stdout); + } + rc = sqlite3_exec(db, zSql, 0, 0, 0); + if( rc && eVerbose ){ + printf("%s:%d: return-code %d\n", zId, lineno, rc); + fflush(stdout); + } + sqlite3_free(zSql); + return rc; +} + +/* Generate a perpared statement from the input SQL +*/ +static sqlite3_stmt *prepare( + sqlite3 *db, + const char *zId, + int lineno, + const char *zFormat, + ... +){ + int rc; + va_list ap; + char *zSql; + sqlite3_stmt *pStmt = 0; + va_start(ap, zFormat); + zSql = sqlite3_vmprintf(zFormat, ap); + va_end(ap); + if( eVerbose){ + printf("%s:%d: [%s]\n", zId, lineno, zSql); + fflush(stdout); + } + + rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); + if( rc ){ + printf("%s:%d: ERROR - %s\n", zId, lineno, sqlite3_errmsg(db)); + exit(-1); + } + sqlite3_free(zSql); + return pStmt; +} + +/* +** Wait for table zTable to exist in the schema. +*/ +static void waitOnTable(sqlite3 *db, const char *zWorker, const char *zTable){ + while(1){ + int eFound = 0; + sqlite3_stmt *q = prepare(db, zWorker, __LINE__, + "SELECT 1 FROM sqlite_schema WHERE name=%Q", zTable); + if( sqlite3_step(q)==SQLITE_ROW && sqlite3_column_int(q,0)!=0 ){ + eFound = 1; + } + sqlite3_finalize(q); + if( eFound ) return; + sqlite3_sleep(1); + } +} + +/* +** Return true if x is a prime number +*/ +static int isPrime(int x){ + int i; + if( x<2 ) return 1; + for(i=2; i*i<=x; i++){ + if( (x%i)==0 ) return 0; + } + return 1; +} + +/* Each worker thread runs an instance of the following */ +static void *worker(void *pArg){ + int rc; + const char *zName = (const char*)pArg; + sqlite3 *db = 0; + + if( eVerbose ){ + printf("%s: startup\n", zName); + fflush(stdout); + } + + rc = sqlite3_open(zDbName, &db); + error_out(rc, "sqlite3_open", __LINE__); + sqlite3_busy_timeout(db, 2000); + + while( 1 ){ + sqlite3_stmt *q1; + int tid = -1; + q1 = prepare(db, zName, __LINE__, + "UPDATE task SET doneby=%Q" + " WHERE tid=(SELECT tid FROM task WHERE doneby IS NULL LIMIT 1)" + "RETURNING tid", zName + ); + if( sqlite3_step(q1)==SQLITE_ROW ){ + tid = sqlite3_column_int(q1,0); + } + sqlite3_finalize(q1); + if( tid<0 ) break; + if( eVerbose ){ + printf("%s: starting task %d\n", zName, tid); + fflush(stdout); + } + if( tid==1 ){ + exec(db, zName, __LINE__, + "CREATE TABLE IF NOT EXISTS p1(x INTEGER PRIMARY KEY);" + ); + }else if( tid>=2 && tid<=51 ){ + int a, b, i; + waitOnTable(db, zName, "p1"); + a = (tid-2)*200 + 1; + b = a+200; + for(i=a; i<b; i++){ + if( isPrime(i) ){ + exec(db, zName, __LINE__, + "INSERT INTO p1(x) VALUES(%d)", i); + } + } + }else if( tid==52 ){ + exec(db, zName, __LINE__, + "CREATE TABLE IF NOT EXISTS p2(x INTEGER PRIMARY KEY);" + "WITH RECURSIVE" + " c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<10000)" + "INSERT INTO p2(x) SELECT x FROM c;" + ); + }else if( tid>=53 && tid<=62 ){ + int a, b, i; + waitOnTable(db, zName, "p2"); + a = (tid-53)*10 + 2; + b = a+9; + for(i=a; i<=b; i++){ + exec(db, zName, __LINE__, + "DELETE FROM p2 WHERE x>%d AND (x %% %d)==0", i, i); + } + } + if( eVerbose ){ + printf("%s: completed task %d\n", zName, tid); + fflush(stdout); + } + sqlite3_sleep(1); + } + + sqlite3_close(db); + + if( eVerbose ){ + printf("%s: exit\n", zName); + fflush(stdout); + } + return 0; +} + +/* Print a usage comment and die */ +static void usage(const char *argv0){ + printf("Usage: %s [options]\n", argv0); + printf( + " -num-workers N Run N worker threads\n" + " -v Debugging output\n" + ); + exit(1); +} + +/* Maximum number of threads */ +#define MX_WORKER 100 + +/* +** Main routine +*/ +int main(int argc, char **argv){ + int i; + int nWorker = 4; + int rc; + sqlite3 *db = 0; + sqlite3_stmt *q; + pthread_t aWorker[MX_WORKER]; + char aWorkerName[MX_WORKER][8]; + + for(i=1; i<argc; i++){ + const char *zArg = argv[i]; + if( zArg[0]!='-' ){ + if( zDbName==0 ){ + zDbName = argv[i]; + continue; + } + printf("unknown argument: %s\n", zArg); + usage(argv[0]); + } + if( zArg[1]=='-' ) zArg++; + if( strcmp(zArg, "-v")==0 ){ + eVerbose = 1; + continue; + } + if( strcmp(zArg, "-num-workers")==0 && i+1<argc ){ + nWorker = atoi(argv[++i]); + if( nWorker<1 || nWorker>MX_WORKER ){ + printf("number of threads must be between 1 and %d\n", MX_WORKER); + exit(1); + } + continue; + } + printf("unknown option: %s\n", argv[i]); + usage(argv[0]); + } + if( zDbName==0 ) zDbName = "file:/mem?vfs=memdb"; + + sqlite3_config(SQLITE_CONFIG_URI, (int)1); + rc = sqlite3_open(zDbName, &db); + error_out(rc, "sqlite3_open", __LINE__); + + rc = exec(db, "SETUP", __LINE__, + "DROP TABLE IF EXISTS task;\n" + "DROP TABLE IF EXISTS p1;\n" + "DROP TABLE IF EXISTS p2;\n" + "DROP TABLE IF EXISTS verify;\n" + "CREATE TABLE IF NOT EXISTS task(\n" + " tid INTEGER PRIMARY KEY,\n" + " doneby TEXT\n" + ");\n" + "WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<100)" + "INSERT INTO task(tid) SELECT x FROM c;\n" + ); + error_out(rc, "sqlite3_exec", __LINE__); + + for(i=0; i<nWorker; i++){ + sqlite3_snprintf(sizeof(aWorkerName[i]), aWorkerName[i], + "W%02d", i); + pthread_create(&aWorker[i], 0, worker, aWorkerName[i]); + } + for(i=0; i<nWorker; i++){ + pthread_join(aWorker[i], 0); + } + + for(i=0; i<nWorker; i++){ + q = prepare(db, "MAIN", __LINE__, + "SELECT group_concat(tid,',') FROM task WHERE doneby=%Q", + aWorkerName[i]); + if( sqlite3_step(q)==SQLITE_ROW ){ + printf("%s: %s\n", aWorkerName[i], sqlite3_column_text(q,0)); + } + sqlite3_finalize(q); + } + q = prepare(db, "MAIN", __LINE__, "SELECT count(*) FROM p2"); + if( sqlite3_step(q)!=SQLITE_ROW || sqlite3_column_int(q,0)<10 ){ + printf("incorrect result\n"); + exit(-1); + } + sqlite3_finalize(q); + q = prepare(db, "MAIN", __LINE__, "SELECT x FROM p1 EXCEPT SELECT x FROM p2"); + if( sqlite3_step(q)==SQLITE_ROW ){ + printf("incorrect result\n"); + exit(-1); + } + sqlite3_finalize(q); + q = prepare(db, "MAIN", __LINE__, "SELECT x FROM p2 EXCEPT SELECT x FROM p1"); + if( sqlite3_step(q)==SQLITE_ROW ){ + printf("incorrect result\n"); + exit(-1); + } + sqlite3_finalize(q); + printf("OK\n"); + + sqlite3_close(db); + return 0; +} diff --git a/test/time-wordcount.sh b/test/time-wordcount.sh new file mode 100644 index 0000000000..df9edc6f2c --- /dev/null +++ b/test/time-wordcount.sh @@ -0,0 +1,34 @@ +#!/bin/sh +# +# This script runs the wordcount program in different ways and generates +# an output useful for performance comparisons. +# + +# Select the source text to be analyzed. +# +if test "x$1" = "x"; +then echo "Usage: $0 FILENAME [ARGS...]"; exit 1; +fi + +# Do test runs +# +rm -f wcdb1.db +./wordcount --tag A: --timer --summary wcdb1.db $* --insert +rm -f wcdb2.db +./wordcount --tag B: --timer --summary wcdb2.db $* --insert --without-rowid +rm -f wcdb1.db +./wordcount --tag C: --timer --summary wcdb1.db $* --replace +rm -f wcdb2.db +./wordcount --tag D: --timer --summary wcdb2.db $* --replace --without-rowid +rm -f wcdb1.db +./wordcount --tag E: --timer --summary wcdb1.db $* --select +rm -f wcdb2.db +./wordcount --tag F: --timer --summary wcdb2.db $* --select --without-rowid +./wordcount --tag G: --timer --summary wcdb1.db $* --query +./wordcount --tag H: --timer --summary wcdb1.db $* --query --without-rowid +./wordcount --tag I: --timer --summary wcdb1.db $* --delete +./wordcount --tag J: --timer --summary wcdb2.db $* --delete --without-rowid + +# Clean up temporary files created. +# +rm -f wcdb1.db wcdb2.db diff --git a/test/timediff1.test b/test/timediff1.test new file mode 100644 index 0000000000..f8176d8810 --- /dev/null +++ b/test/timediff1.test @@ -0,0 +1,222 @@ +# 2023-05-30 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is testing date and time functions. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +# Skip this whole file if date and time functions are omitted +# at compile-time +# +ifcapable {!datetime} { + finish_test + return +} + +proc datetest {tnum expr result} { + do_test timediff-$tnum [subst { + execsql "SELECT coalesce($expr,'NULL')" + }] [list $result] +} +set tcl_precision 15 + + +# February overflow on a leap year +datetest 1.1 {datetime('2000-01-31','+1 month')} {2000-03-02 00:00:00} +datetest 1.2 {datetime('2004-01-29','+1 month')} {2004-02-29 00:00:00} +datetest 1.3 {datetime('2000-03-01','-1 day')} {2000-02-29 00:00:00} +datetest 1.4 {datetime('2000-03-31','-1 month')} {2000-03-02 00:00:00} +datetest 1.5 {datetime('2000-03-30','-1 month')} {2000-03-01 00:00:00} +datetest 1.6 {datetime('2000-03-29','-1 month')} {2000-02-29 00:00:00} +datetest 1.7 {datetime('2000-03-28','-1 month')} {2000-02-28 00:00:00} +datetest 1.8 {datetime('2000-02-29','+1 year')} {2001-03-01 00:00:00} +datetest 1.9 {datetime('2000-02-29','+4 years')} {2004-02-29 00:00:00} + +datetest 1.10 {datetime('1998-11-10','+0001-03-19 12:34:56')} \ + {2000-02-29 12:34:56} +datetest 1.11 {datetime('2000-01-31','+0004-01-00 12:34:56')} \ + {2004-03-02 12:34:56} +datetest 1.12 {datetime('2000-01-29','+0008-01-00 12:34:56')} \ + {2008-02-29 12:34:56} +datetest 1.13 {datetime('2001-03-31','-0001-01-00 06:10')} \ + {2000-03-01 17:50:00} + + +# February overflow on a non-leap year +datetest 2.1 {datetime('2001-01-31','+1 month')} {2001-03-03 00:00:00} +datetest 2.2 {datetime('2005-01-29','+1 month')} {2005-03-01 00:00:00} +datetest 2.3 {datetime('2001-03-01','-1 day')} {2001-02-28 00:00:00} +datetest 2.4 {datetime('2001-03-31','-1 month')} {2001-03-03 00:00:00} +datetest 2.5 {datetime('2001-03-30','-1 month')} {2001-03-02 00:00:00} +datetest 2.6 {datetime('2001-03-29','-1 month')} {2001-03-01 00:00:00} +datetest 2.7 {datetime('2001-03-28','-1 month')} {2001-02-28 00:00:00} + +datetest 2.10 {datetime('1999-11-10','+0001-03-19 12:34:56')} \ + {2001-03-01 12:34:56} +datetest 2.11 {datetime('2000-01-31','+0005-01-00 12:34:56')} \ + {2005-03-03 12:34:56} +datetest 2.12 {datetime('2000-01-29','+0009-01-00 12:34:56')} \ + {2009-03-01 12:34:56} +datetest 2.13 {datetime('2002-03-31','-0001-01-00 06:10')} \ + {2001-03-02 17:50:00} + +# timediff +datetest 3.1 {timediff('2000-03-02','2000-01-31')} {+0000-01-00 00:00:00.000} +datetest 3.2 {timediff('2000-01-31','2000-03-02')} {-0000-01-02 00:00:00.000} +datetest 3.3 {timediff('2000-03-02','1999-01-31')} {+0001-01-00 00:00:00.000} +datetest 3.4 {timediff('1999-01-31','2000-03-02')} {-0001-01-02 00:00:00.000} + +unset -nocomplain p1 +unset -nocomplain p2 +set p1 { + 0 {-4713-11-24 12:00:00} + 1 {-2000-04-30 05:19:26} + 2 {0000-01-01 12:34:56} + 3 {1776-07-04 13:00:00} + 4 {1969-07-20 20:17} + 5 {2440587.5} + 6 {2000-05-29 14:26} + 7 {2023-05-29 18:11} + 8 {2050-05-29 14:26} + 9 {4796-02-29 11:23:55.46} +} +set p2 { + A {1066-10-14} + B {1900-02-28 11:00} + C {1900-03-01 12:00} + D {1904-02-29 11:25} + E {2000-02-29 13:00} + E {2000-03-01 14:00} + F {2001-03-31 15:15} + G {2002-04-01 16:59} + H {2003-04-30 17:00} + I {2004-05-01 23:59:59} + J {2005-06-01} + K {2006-06-30 01:23:45} + L {2007-12-31 02:00} + M {2008-01-01 01:59} + N {3152-07-04 12:00} + P {9999-12-31 23:59:59} +} + +foreach {x1 d1} $p1 { + foreach {x2 d2} $p2 { + set r1 [db one {SELECT datetime($d1)}] + do_execsql_test timediff-4-$x1$x2 { + SELECT datetime($d2, timediff($d1,$d2)); + } [list $r1] + set r2 [db one {SELECT datetime($d2)}] + do_execsql_test timediff-4-$x2$x1 { + SELECT datetime($d1, timediff($d2,$d1)); + } [list $r2] + } +} + +# Partial time-diffs as modifiers +# +datetest 5-1 {datetime('2000-01-01','+0001-02-03')} {2001-03-04 00:00:00} +datetest 5-2 {datetime('2000-01-01','+0001-02-03x')} {NULL} +datetest 5-3 {datetime('2000-01-01','+0001-11-03')} {2001-12-04 00:00:00} +datetest 5-4 {datetime('2000-01-01','+0001-12-03')} {NULL} +datetest 5-5 {datetime('2000-01-01','+0001-02-30')} {2001-03-31 00:00:00} +datetest 5-6 {datetime('2000-01-01','+0001-02-31')} {NULL} +datetest 5-7 {datetime('2000-01-01','+0001-02-03 0')} {NULL} +datetest 5-8 {datetime('2000-01-01','+0001-02-03 01')} {NULL} +datetest 5-9 {datetime('2000-01-01','+0001-02-03 01:')} {NULL} +datetest 5-10 {datetime('2000-01-01','+0001-02-03 01:0')} {NULL} +datetest 5-11 {datetime('2000-01-01','+0001-02-03 01:02')} {2001-03-04 01:02:00} +datetest 5-12 {datetime('2000-01-01','+0001-02-03 01:02:')} {NULL} +datetest 5-13 {datetime('2000-01-01','+0001-02-03 01:02:0')} {NULL} +datetest 5-14 {datetime('2000-01-01','+0001-02-03 01:02:03')} \ + {2001-03-04 01:02:03} +datetest 5-15 {datetime('2000-01-01','+0001-02-03 01:02:03.')} NULL +datetest 5-16 {datetime('2000-01-01','+0001-02-03 01:02:03.5')} \ + {2001-03-04 01:02:03} +datetest 5-17 {datetime('2000-01-01','+0001-02-03 01:02:03.50')} \ + {2001-03-04 01:02:03} +datetest 5-18 {datetime('2000-01-01','+0001-02-03 01:02:03.500')} \ + {2001-03-04 01:02:03} +datetest 5-19 {datetime('2000-01-01','+0001-02-03 01:02:03.500x')} {NULL} +datetest 5-20 {datetime('2000-01-01','+0001-02-03 01:02:03.500 x')} {NULL} + +unset -nocomplain p1 +unset -nocomplain p2 +set p1 { + a {2000-01-01 00:00:00} + b {2000-01-31 23:59:59} + c {2000-02-01 00:00:00} + d {2000-02-29 23:59:59} + e {2000-03-01 00:00:00} + f {2000-03-31 23:59:59} + g {2000-04-01 00:00:00} + h {2000-04-30 23:59:59} + i {2000-05-01 00:00:00} + j {2000-05-31 23:59:59} + k {2000-06-01 00:00:00} + l {2000-06-30 23:59:59} + m {2000-07-01 00:00:00} + n {2000-07-31 23:59:59} + o {2000-08-01 00:00:00} + p {2000-08-31 23:59:59} + q {2000-09-01 00:00:00} + r {2000-09-30 23:59:59} + s {2000-10-01 00:00:00} + t {2000-10-31 23:59:59} + u {2000-11-01 00:00:00} + v {2000-11-30 23:59:59} + w {2000-12-01 00:00:00} + x {2000-12-31 23:59:59} +} +set p2 { + A {2001-01-01 00:00:00} + B {2001-01-31 23:59:59} + C {2001-02-01 00:00:00} + D {2001-02-28 23:59:59} + E {2001-03-01 00:00:00} + F {2001-03-31 23:59:59} + G {2001-04-01 00:00:00} + H {2001-04-30 23:59:59} + I {2001-05-01 00:00:00} + J {2001-05-31 23:59:59} + K {2001-06-01 00:00:00} + L {2001-06-30 23:59:59} + M {2001-07-01 00:00:00} + N {2001-07-31 23:59:59} + O {2001-08-01 00:00:00} + P {2001-08-31 23:59:59} + Q {2001-09-01 00:00:00} + R {2001-09-30 23:59:59} + S {2001-10-01 00:00:00} + T {2001-10-31 23:59:59} + U {2001-11-01 00:00:00} + V {2001-11-30 23:59:59} + W {2001-12-01 00:00:00} + X {2001-12-31 23:59:59} +} + +foreach {x1 d1} $p1 { + foreach {x2 d2} $p2 { + set r1 [db one {SELECT datetime($d1)}] + do_execsql_test timediff-6-$x1$x2 { + SELECT datetime($d2, timediff($d1,$d2)); + } [list $r1] + set r2 [db one {SELECT datetime($d2)}] + do_execsql_test timediff-6-$x2$x1 { + SELECT datetime($d1, timediff($d2,$d1)); + } [list $r2] + } +} + + + +finish_test diff --git a/test/tkt-18458b1a.test b/test/tkt-18458b1a.test new file mode 100644 index 0000000000..4a61274859 --- /dev/null +++ b/test/tkt-18458b1a.test @@ -0,0 +1,52 @@ +# 2019 September 10 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. In particular, +# that problems related to ticket [18458b1a] have been fixed. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix tkt-18458b1a + +foreach tn {1 2} { + reset_db + if {$tn==1} { + # Disable the flattener and push-down optimizations + optimization_control db query-flattener 0 + optimization_control db push-down 0 + } else { + # Enable them + optimization_control db query-flattener 1 + optimization_control db push-down 1 + } + + db cache size 0 + + do_execsql_test $tn.1.1 { + CREATE TABLE t0(c0 COLLATE NOCASE); + INSERT INTO t0(c0) VALUES ('B'); + CREATE VIEW v0(c0, c1) AS SELECT DISTINCT t0.c0, 'a' FROM t0; + } + + do_execsql_test $tn.1.2 { + SELECT count(*) FROM v0 WHERE c1 >= c0; + } 1 + + do_execsql_test $tn.1.3 { + SELECT count(*) FROM v0 WHERE NOT NOT (c1 >= c0); + } 1 + + do_execsql_test $tn.1.4 { + SELECT count(*) FROM v0 WHERE ((c1 >= c0) OR 0+0); + } 1 +} + +finish_test diff --git a/test/tkt-26ff0c2d1e.test b/test/tkt-26ff0c2d1e.test index 83a4f3d674..d7c5c6bae3 100644 --- a/test/tkt-26ff0c2d1e.test +++ b/test/tkt-26ff0c2d1e.test @@ -31,3 +31,5 @@ do_test bug-20100512-3 { sqlite3_column_int $STMT 0 } {555} sqlite3_finalize $STMT + +finish_test diff --git a/test/tkt-2d1a5c67d.test b/test/tkt-2d1a5c67d.test index 3fef187ecd..5dad781692 100644 --- a/test/tkt-2d1a5c67d.test +++ b/test/tkt-2d1a5c67d.test @@ -19,7 +19,8 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl set testprefix tkt-2d1a5c67d -ifcapable {!wal || !vtab} {finish_test; return} +ifcapable {!vtab || !incrblob} {finish_test; return} +if {[wal_is_capable]==0} {finish_test; return} for {set ii 1} {$ii<=10} {incr ii} { do_test tkt-2d1a5c67d.1.$ii { @@ -101,6 +102,7 @@ do_test 3.4 { set blobs [list] for {set i 1} {$i<100} {incr i} { set b [db incrblob -readonly t3 b $i] + fconfigure $b -translation binary read $b lappend blobs $b } diff --git a/test/tkt-313723c356.test b/test/tkt-313723c356.test index 8c08c34976..5325fd2292 100644 --- a/test/tkt-313723c356.test +++ b/test/tkt-313723c356.test @@ -18,7 +18,7 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl source $testdir/malloc_common.tcl -ifcapable !wal { finish_test ; return } +if {![wal_is_capable]} { finish_test ; return } do_execsql_test tkt-313723c356.1 { PRAGMA page_size = 1024; diff --git a/test/tkt-385a5b56b9.test b/test/tkt-385a5b56b9.test index 1338435ed6..4451389b1e 100644 --- a/test/tkt-385a5b56b9.test +++ b/test/tkt-385a5b56b9.test @@ -34,20 +34,17 @@ do_execsql_test 2.0 { CREATE UNIQUE INDEX t2y ON t2(y); } -do_eqp_test 2.1 { SELECT DISTINCT x FROM t2 } { - 0 0 0 {SCAN TABLE t2 USING COVERING INDEX t2x} -} +do_eqp_test 2.1 { SELECT DISTINCT x FROM t2 } \ + {SCAN t2 USING COVERING INDEX t2x} -do_eqp_test 2.2 { SELECT DISTINCT y FROM t2 } { - 0 0 0 {SCAN TABLE t2 USING COVERING INDEX t2y} -} +do_eqp_test 2.2 { SELECT DISTINCT y FROM t2 } \ + {SCAN t2 USING COVERING INDEX t2y} -do_eqp_test 2.3 { SELECT DISTINCT x, y FROM t2 WHERE y=10 } { - 0 0 0 {SEARCH TABLE t2 USING INDEX t2y (y=?)} -} +do_eqp_test 2.3 { SELECT DISTINCT x, y FROM t2 WHERE y=10 } \ + {SEARCH t2 USING INDEX t2y (y=?)} + +do_eqp_test 2.4 { SELECT DISTINCT x, y FROM t2 WHERE x=10 } \ + {SEARCH t2 USING INDEX t2x (x=?)} -do_eqp_test 2.4 { SELECT DISTINCT x, y FROM t2 WHERE x=10 } { - 0 0 0 {SEARCH TABLE t2 USING INDEX t2x (x=?)} -} finish_test diff --git a/test/tkt-3a77c9714e.test b/test/tkt-3a77c9714e.test index b7d366f36e..734dff38f6 100644 --- a/test/tkt-3a77c9714e.test +++ b/test/tkt-3a77c9714e.test @@ -1,4 +1,4 @@ -# 2011 December 06 +# 2011-12-06 # # The author disclaims copyright to this source code. In place of # a legal notice, here is a blessing: @@ -68,5 +68,19 @@ do_execsql_test 2.2 { ) } {FACTORING FACTOR SWIMMING SWIMM} +# Similar problem discovered by dbsqlfuzz on 2019-09-18 +# +do_execsql_test 3.0 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(i INT PRIMARY KEY, a, b); + INSERT INTO t1 VALUES(NULL,'one','i'); + CREATE INDEX i1a ON t1(a); + CREATE INDEX i1b ON t1(b); + SELECT (SELECT 1 + FROM (SELECT 1 FROM t1 WHERE a=1 OR b='i') + WHERE a='o' + OR b IN (SELECT a=('b' IN (SELECT 'a')))) + FROM t1; +} {{}} finish_test diff --git a/test/tkt-5d863f876e.test b/test/tkt-5d863f876e.test index 86024e300b..9a2fa3f470 100644 --- a/test/tkt-5d863f876e.test +++ b/test/tkt-5d863f876e.test @@ -18,7 +18,7 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl source $testdir/lock_common.tcl set ::testprefix tkt-5d863f876e -ifcapable !wal {finish_test ; return } +if {![wal_is_capable]} {finish_test ; return } do_multiclient_test tn { do_test $tn.1 { diff --git a/test/tkt-78e04e52ea.test b/test/tkt-78e04e52ea.test index 975e5b3d1c..1b49f5d6d2 100644 --- a/test/tkt-78e04e52ea.test +++ b/test/tkt-78e04e52ea.test @@ -41,10 +41,8 @@ do_test tkt-78e04-1.3 { } } {} do_test tkt-78e04-1.4 { - execsql { - EXPLAIN QUERY PLAN SELECT "" FROM "" WHERE "" LIKE 'abc%'; - } -} {0 0 0 {SCAN TABLE USING COVERING INDEX i1}} + db eval {EXPLAIN QUERY PLAN SELECT "" FROM "" WHERE "" LIKE '1e5%';} +} {/*SCAN USING COVERING INDEX i1*/} do_test tkt-78e04-1.5 { execsql { DROP TABLE ""; @@ -57,12 +55,12 @@ do_test tkt-78e04-2.1 { CREATE INDEX "" ON t2(x); EXPLAIN QUERY PLAN SELECT * FROM t2 WHERE x=5; } -} {0 0 0 {SEARCH TABLE t2 USING COVERING INDEX (x=?)}} +} {/*SEARCH t2 USING COVERING INDEX (x=?)*/} do_test tkt-78e04-2.2 { execsql { DROP INDEX ""; EXPLAIN QUERY PLAN SELECT * FROM t2 WHERE x=2; } -} {0 0 0 {SCAN TABLE t2}} +} {/*SCAN t2*/} finish_test diff --git a/test/tkt-7a31705a7e6.test b/test/tkt-7a31705a7e6.test index e3e402917d..fd862484ea 100644 --- a/test/tkt-7a31705a7e6.test +++ b/test/tkt-7a31705a7e6.test @@ -23,3 +23,5 @@ do_execsql_test tkt-7a31705a7e6-1.1 { CREATE TABLE t2x (b INTEGER PRIMARY KEY); SELECT t1.a FROM ((t1 JOIN t2 ON t1.a=t2.a) AS x JOIN t2x ON x.b=t2x.b) as y; } {} + +finish_test diff --git a/test/tkt-7bbfb7d442.test b/test/tkt-7bbfb7d442.test index 56d4caeb3e..c020b515e2 100644 --- a/test/tkt-7bbfb7d442.test +++ b/test/tkt-7bbfb7d442.test @@ -146,7 +146,7 @@ do_execsql_test 2.2 { } {31 10} do_execsql_test 2.3 { - SELECT CASE WHEN DeliveredQty=10 THEN "TEST PASSED!" ELSE "TEST FAILED!" END + SELECT CASE WHEN DeliveredQty=10 THEN 'TEST PASSED!' ELSE 'TEST FAILED!' END FROM InventoryControl WHERE SKU=31; } {{TEST PASSED!}} diff --git a/test/tkt-80e031a00f.test b/test/tkt-80e031a00f.test index af1d636eed..56449dba23 100644 --- a/test/tkt-80e031a00f.test +++ b/test/tkt-80e031a00f.test @@ -20,19 +20,14 @@ source $testdir/tester.tcl source $testdir/lock_common.tcl source $testdir/malloc_common.tcl -# EVIDENCE-OF: R-58875-56087 The IN and NOT IN operators take a single -# scalar operand on the left and a vector operand on the right formed by -# an explicit list of zero or more scalars or by a single subquery. -# # EVIDENCE-OF: R-52275-55503 When the right operand is an empty set, the # result of IN is false and the result of NOT IN is true, regardless of # the left operand and even if the left operand is NULL. # -# EVIDENCE-OF: R-13595-45863 Note that SQLite allows the parenthesized +# EVIDENCE-OF: R-64309-54027 Note that SQLite allows the parenthesized # list of scalar values on the right-hand side of an IN or NOT IN -# operator to be an empty list but most other SQL database database -# engines and the SQL92 standard require the list to contain at least -# one element. +# operator to be an empty list but most other SQL database engines and +# the SQL92 standard require the list to contain at least one element. # do_execsql_test tkt-80e031a00f.1 {SELECT 1 IN ()} 0 do_execsql_test tkt-80e031a00f.1b {SELECT 1 IN (2)} 0 diff --git a/test/tkt-8454a207b9.test b/test/tkt-8454a207b9.test index 88a8614f82..42b95c8f53 100644 --- a/test/tkt-8454a207b9.test +++ b/test/tkt-8454a207b9.test @@ -18,6 +18,12 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl +# If SQLITE_OMIT_ALTERTABLE is defined, omit this file. +ifcapable !altertable { + finish_test + return +} + do_test tkt-8454a207b9.1 { db eval { CREATE TABLE t1(a); @@ -43,7 +49,7 @@ do_test tkt-8454a207b9.4 { ALTER TABLE t1 ADD COLUMN e DEFAULT -123.0; SELECT e, typeof(e) FROM t1; } -} {-123 integer} +} {-123.0 real} do_test tkt-8454a207b9.5 { db eval { ALTER TABLE t1 ADD COLUMN f DEFAULT -123.5; diff --git a/test/tkt-94c04eaadb.test b/test/tkt-94c04eaadb.test deleted file mode 100644 index 9de8aea28d..0000000000 --- a/test/tkt-94c04eaadb.test +++ /dev/null @@ -1,72 +0,0 @@ -# 2009 October 19 -# -# The author disclaims copyright to this source code. In place of -# a legal notice, here is a blessing: -# -# May you do good and not evil. -# May you find forgiveness for yourself and forgive others. -# May you share freely, never taking more than you give. -# -#*********************************************************************** -# This file implements regression tests for SQLite library. -# - -set testdir [file dirname $argv0] -source $testdir/tester.tcl - -if {[info commands sqlite3async_initialize] eq ""} { - # The async logic is not built into this system - finish_test - return -} - -# Create a database. -do_test tkt-94c94-1.1 { - execsql { CREATE TABLE t1(a, b) } -} {} - -# Grow the file to larger than 4096MB (2^32 bytes) -db close -if {[catch {fake_big_file 4096 [get_pwd]/test.db} msg]} { - puts "**** Unable to create a file larger than 4096 MB. *****" - finish_test - return -} - -# Switch to async mode. -sqlite3async_initialize "" 1 -sqlite3 db test.db -sqlite3 db2 test.db - -# Read from and write to the db just past the 4096MB mark. -# -do_test tkt-94c94-2.1 { - execsql { CREATE TABLE t2(x, y) } db -} {} -do_test tkt-94c94-2.2 { - execsql { INSERT INTO t2 VALUES(1, 2) } db2 -} {} -do_test tkt-94c94-2.3 { - execsql { SELECT * FROM t2 } db -} {1 2} -do_test tkt-94c94-2.4 { - sqlite3async_control halt idle - sqlite3async_start - sqlite3async_wait -} {} -do_test tkt-94c94-2.5 { - execsql { SELECT * FROM t2 } db -} {1 2} -do_test tkt-94c94-2.6 { - sqlite3async_start - sqlite3async_wait -} {} - -db close -db2 close -sqlite3async_start -sqlite3async_wait -sqlite3async_control halt never -sqlite3async_shutdown - -finish_test diff --git a/test/tkt-99378177930f87bd.test b/test/tkt-99378177930f87bd.test new file mode 100644 index 0000000000..ba9fdc7027 --- /dev/null +++ b/test/tkt-99378177930f87bd.test @@ -0,0 +1,196 @@ +# 2022-11-23 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. +# +# This file implements tests to verify that the enhancement +# request documented by ticket 99378177930f87bd is working. +# +# The enhancement is that if an aggregate query with a GROUP BY clause +# uses subexpressions in the arguments to aggregate functions that are +# also columns of an index, then the values are pulled from the index +# rather than being recomputed. This has the potential to make some +# indexed queries works as if the index were covering. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +do_execsql_test tkt-99378-100 { + CREATE TABLE t1(a INT, b TEXT, c INT, d INT); + INSERT INTO t1(a,b,c,d) VALUES + (1, '{"x":1}', 12, 3), + (1, '{"x":2}', 4, 5), + (1, '{"x":1}', 6, 11), + (2, '{"x":1}', 22, 3), + (2, '{"x":2}', 4, 5), + (3, '{"x":1}', 6, 7); + CREATE INDEX t1x ON t1(d, a, b->>'x', c); +} {} +do_execsql_test tkt-99378-110 { + SELECT a, + SUM(1) AS t1, + SUM(CASE WHEN b->>'x'=1 THEN 1 END) AS t2, + SUM(c) AS t3, + SUM(CASE WHEN b->>'x'=1 THEN c END) AS t4 + FROM t1 + WHERE d BETWEEN 0 and 10 + GROUP BY a; +} { + 1 2 1 16 12 + 2 2 1 26 22 + 3 1 1 6 6 +} + +# The proof that the index on the expression is being used is in the +# fact that the byte code contains no "Function" opcodes. In other words, +# the ->> operator (which is implemented by a function) is never invoked. +# Instead, the b->>'x' value is pulled out of the index. +# +do_execsql_test tkt-99378-120 { + EXPLAIN + SELECT a, + SUM(1) AS t1, + SUM(CASE WHEN b->>'x'=1 THEN 1 END) AS t2, + SUM(c) AS t3, + SUM(CASE WHEN b->>'x'=1 THEN c END) AS t4 + FROM t1 + WHERE d BETWEEN 0 and 10 + GROUP BY a; +} {~/Function/} + + +do_execsql_test tkt-99378-130 { + SELECT a, + SUM(1) AS t1, + SUM(CASE WHEN b->>'x'=1 THEN 1 END) AS t2, + SUM(c) AS t3, + SUM(CASE WHEN b->>'x'=1 THEN c END) AS t4 + FROM t1 + WHERE d BETWEEN 0 and 10 + GROUP BY +a; +} { + 1 2 1 16 12 + 2 2 1 26 22 + 3 1 1 6 6 +} +do_execsql_test tkt-99378-140 { + EXPLAIN + SELECT a, + SUM(1) AS t1, + SUM(CASE WHEN b->>'x'=1 THEN 1 END) AS t2, + SUM(c) AS t3, + SUM(CASE WHEN b->>'x'=1 THEN c END) AS t4 + FROM t1 + WHERE d BETWEEN 0 and 10 + GROUP BY +a; +} {~/Function/} + +do_execsql_test tkt-99378-200 { + DROP INDEX t1x; + CREATE INDEX t1x ON t1(a, d, b->>'x', c); +} +do_execsql_test tkt-99378-210 { + SELECT a, + SUM(1) AS t1, + SUM(CASE WHEN b->>'x'=1 THEN 1 END) AS t2, + SUM(c) AS t3, + SUM(CASE WHEN b->>'x'=1 THEN c END) AS t4 + FROM t1 + WHERE d BETWEEN 0 and 10 + GROUP BY a; +} { + 1 2 1 16 12 + 2 2 1 26 22 + 3 1 1 6 6 +} +do_execsql_test tkt-99378-220 { + EXPLAIN + SELECT a, + SUM(1) AS t1, + SUM(CASE WHEN b->>'x'=1 THEN 1 END) AS t2, + SUM(c) AS t3, + SUM(CASE WHEN b->>'x'=1 THEN c END) AS t4 + FROM t1 + WHERE d BETWEEN 0 and 10 + GROUP BY a; +} {~/Function/} +do_execsql_test tkt-99378-230 { + SELECT a, + SUM(1) AS t1, + SUM(CASE WHEN b->>'x'=1 THEN 1 END) AS t2, + SUM(c) AS t3, + SUM(CASE WHEN b->>'x'=1 THEN c END) AS t4 + FROM t1 + WHERE d BETWEEN 0 and 10 + GROUP BY a; +} { + 1 2 1 16 12 + 2 2 1 26 22 + 3 1 1 6 6 +} +do_execsql_test tkt-99378-240 { + EXPLAIN + SELECT a, + SUM(1) AS t1, + SUM(CASE WHEN b->>'x'=1 THEN 1 END) AS t2, + SUM(c) AS t3, + SUM(CASE WHEN b->>'x'=1 THEN c END) AS t4 + FROM t1 + WHERE d BETWEEN 0 and 10 + GROUP BY a; +} {~/Function/} + +# 2022-12-20 dbsqlfuzz a644e70d7683a7ca59c71861a153c1dccf8850b9 +# +do_execsql_test tkt-99378-300 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a INT); + CREATE INDEX i1 ON t1(a,a=a); + INSERT INTO t1 VALUES(1),(2),(3),(4); + SELECT * FROM t1 NATURAL JOIN t1 + WHERE a==1 + OR ( + (SELECT avg( + (SELECT sum((SELECT 1 FROM t1 NATURAL RIGHT JOIN t1 WHERE a=a)))) AS xyz + ) + AND a==2 + ); +} {1 2} +do_execsql_test tkt-99378-310 { + DROP INDEX i1; + SELECT * FROM t1 NATURAL JOIN t1 + WHERE a==1 + OR ( + (SELECT avg( + (SELECT sum((SELECT 1 FROM t1 NATURAL RIGHT JOIN t1 WHERE a=a)))) AS xyz + ) + AND a==2 + ); +} {1 2} + +# 2023-03-04 https://sqlite.org/forum/forumpost/a68313d054 +# +# See also indexexpr1-2200 added on 2023-03-18. +# +do_execsql_test tkt-99378-400 { + DROP TABLE t1; + CREATE TABLE t0(w); + INSERT INTO t0(w) VALUES(1); + CREATE TABLE t1(x); + INSERT INTO t1(x) VALUES(1); + CREATE INDEX t1x ON t1(x > 0); + CREATE VIEW t2(y) AS SELECT avg(w) FROM t0 GROUP BY w>1; + CREATE VIEW t3(z) AS SELECT count(*) FROM t2 WHERE y BETWEEN 0 and 0; + SELECT count(*) FROM t1 NOT INDEXED WHERE (SELECT z FROM t3); + SELECT count(*) FROM t1 INDEXED BY t1x WHERE (SELECT z FROM t3); +} {0 0} + +finish_test diff --git a/test/tkt-9d68c883.test b/test/tkt-9d68c883.test index 18dc6ccd67..ba91b39e39 100644 --- a/test/tkt-9d68c883.test +++ b/test/tkt-9d68c883.test @@ -50,4 +50,6 @@ for {set i 0} {$i < 100} {incr i} { } {ok} } +catch { db close } +unregister_devsim finish_test diff --git a/test/tkt-a7debbe0.test b/test/tkt-a7debbe0.test new file mode 100644 index 0000000000..cc72eb7b5e --- /dev/null +++ b/test/tkt-a7debbe0.test @@ -0,0 +1,102 @@ +# 2019 September 10 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. In particular, +# that problems related to ticket a7debbe0ad1 have been fixed. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix tkt-a7debbe0 + +foreach tn {1 2} { + reset_db + if {$tn==1} { + # Disable the flattener + optimization_control db query-flattener 0 + } else { + # Enable the flattener + optimization_control db query-flattener 1 + } + + do_execsql_test $tn.1.0 { + CREATE TABLE t0(xyz INTEGER); + INSERT INTO t0(xyz) VALUES(456); + CREATE VIEW v2(a, B) AS + SELECT 'a', 'B' COLLATE NOCASE FROM t0; + CREATE TABLE t2(a, B COLLATE NOCASE); + INSERT INTO t2 VALUES('a', 'B'); + CREATE VIEW v3(a, B) AS + SELECT 'a' COLLATE BINARY, 'B' COLLATE NOCASE FROM t0; + + CREATE VIEW v4(a, B) AS + SELECT 'a', +CAST('B' COLLATE NOCASE AS TEXT) FROM t0; + + CREATE VIEW v5(a, B) AS + SELECT 'a', ('B' COLLATE NOCASE) || '' FROM t0; + } + + # Table t2 and views v2 through v5 should all be equivalent. + do_execsql_test $tn.1.1.1 { SELECT a >= B FROM t2; } 1 + do_execsql_test $tn.1.1.2 { SELECT 'a' >= 'B' COLLATE NOCASE } 0 + do_execsql_test $tn.1.1.3 { SELECT a >= B FROM v2 } 1 + do_execsql_test $tn.1.1.4 { SELECT a >= B FROM v3 } 1 + do_execsql_test $tn.1.1.5 { SELECT a >= B FROM v4 } 1 + do_execsql_test $tn.1.1.6 { SELECT a >= B FROM v5 } 1 + + do_execsql_test $tn.1.2.1 { SELECT B < a FROM t2 } 0 + do_execsql_test $tn.1.2.2 { SELECT 'B' COLLATE NOCASE < 'a' } 0 + do_execsql_test $tn.1.2.3 { SELECT B < a FROM v2 } 0 + do_execsql_test $tn.1.2.4 { SELECT B < a FROM v3 } 0 + do_execsql_test $tn.1.2.5 { SELECT a < B FROM v4 } 0 + do_execsql_test $tn.1.2.6 { SELECT a < B FROM v5 } 0 + + #------------------------------------------------------------------------- + do_execsql_test $tn.2.0 { + CREATE TABLE t5(a, b COLLATE NOCASE); + INSERT INTO t5 VALUES(1, 'XYZ'); + } + + # Result should be 0, as column "xyz" from the sub-query has implicit + # collation sequence BINARY. + do_execsql_test $tn.2.1 { + SELECT xyz==b FROM ( SELECT a, 'xyz' AS xyz FROM t5 ), t5; + } {0} + + # Result should be 1, as literal 'xyz' has no collation sequence, so + # the comparison uses the implicit collation sequence of the RHS - NOCASE. + do_execsql_test $tn.2.2 { + SELECT 'xyz'==b FROM ( SELECT a, 'xyz' AS xyz FROM t5 ), t5; + } {1} + + #----------------------------------------------------------------------- + # The test case submitted with the ticket. + # + do_execsql_test $tn.3.0 { + DROP TABLE t0; + DROP VIEW v2; + + CREATE TABLE t0(c0); + INSERT INTO t0(c0) VALUES(''); + CREATE VIEW v2(c0, c1) AS + SELECT 'B' COLLATE NOCASE, 'a' FROM t0 ORDER BY t0.c0; + SELECT SUM(count) FROM ( + SELECT v2.c1 BETWEEN v2.c0 AND v2.c1 as count FROM v2 + ); + } 1 + + # The result is 1, as the collation used is the implicit collation sequence + # of v2.c1 - BINARY. + do_execsql_test $tn.3.1 { + SELECT v2.c1 BETWEEN v2.c0 AND v2.c1 as count FROM v2; + } 1 +} + +finish_test diff --git a/test/tkt-a8a0d2996a.test b/test/tkt-a8a0d2996a.test index 6b15e410e7..2644937029 100644 --- a/test/tkt-a8a0d2996a.test +++ b/test/tkt-a8a0d2996a.test @@ -84,10 +84,12 @@ do_execsql_test 4.3 { } {104.5} do_execsql_test 4.4 { SELECT '-9223372036854775807x'-'1x'; -} {-9.22337203685478e+18} +} {-9223372036854775808} do_execsql_test 4.5 { SELECT '9223372036854775806x'+'1x'; -} {9.22337203685478e+18} +} {9223372036854775807} do_execsql_test 4.6 { - SELECT '1234x'/'10y'; -} {123.4} + SELECT '1234x'/'10y', '1234x'/'10.y', '1234x'/'1e1y'; +} {123 123.4 123.4} + +finish_test diff --git a/test/tkt-b75a9ca6b0.test b/test/tkt-b75a9ca6b0.test index 0c81a534da..f5c8804493 100644 --- a/test/tkt-b75a9ca6b0.test +++ b/test/tkt-b75a9ca6b0.test @@ -32,41 +32,41 @@ do_execsql_test 1.1 { CREATE INDEX i1 ON t1(x, y); } -set idxscan {0 0 0 {SCAN TABLE t1 USING COVERING INDEX i1}} -set tblscan {0 0 0 {SCAN TABLE t1}} -set grpsort {0 0 0 {USE TEMP B-TREE FOR GROUP BY}} -set sort {0 0 0 {USE TEMP B-TREE FOR ORDER BY}} +set idxscan {SCAN t1 USING COVERING INDEX i1} +set tblscan {SCAN t1} +set grpsort {USE TEMP B-TREE FOR GROUP BY} +set sort {USE TEMP B-TREE FOR ORDER BY} foreach {tn q res eqp} [subst -nocommands { 1 "SELECT * FROM t1 GROUP BY x, y ORDER BY x,y" {1 3 2 2 3 1} {$idxscan} 2 "SELECT * FROM t1 GROUP BY x, y ORDER BY x" - {1 3 2 2 3 1} {$idxscan $sort} + {1 3 2 2 3 1} {$idxscan*$sort} 3 "SELECT * FROM t1 GROUP BY y, x ORDER BY y, x" - {3 1 2 2 1 3} {$idxscan $sort} + {3 1 2 2 1 3} {$idxscan*$sort} 4 "SELECT * FROM t1 GROUP BY x ORDER BY x" {1 3 2 2 3 1} {$idxscan} 5 "SELECT * FROM t1 GROUP BY y ORDER BY y" - {3 1 2 2 1 3} {$tblscan $grpsort} + {3 1 2 2 1 3} {$tblscan*$grpsort} 6 "SELECT * FROM t1 GROUP BY y ORDER BY x" - {1 3 2 2 3 1} {$tblscan $grpsort $sort} + {1 3 2 2 3 1} {$tblscan*$grpsort*$sort} 7 "SELECT * FROM t1 GROUP BY x, y ORDER BY x, y DESC" - {1 3 2 2 3 1} {$idxscan $sort} + {1 3 2 2 3 1} {$idxscan*$sort} 8 "SELECT * FROM t1 GROUP BY x, y ORDER BY x DESC, y DESC" - {3 1 2 2 1 3} {$idxscan $sort} + {3 1 2 2 1 3} {$idxscan} 9 "SELECT * FROM t1 GROUP BY x, y ORDER BY x ASC, y ASC" {1 3 2 2 3 1} {$idxscan} 10 "SELECT * FROM t1 GROUP BY x, y ORDER BY x COLLATE nocase, y" - {1 3 2 2 3 1} {$idxscan $sort} + {1 3 2 2 3 1} {$idxscan*$sort} }] { do_execsql_test 1.$tn.1 $q $res diff --git a/test/tkt-bd484a090c.test b/test/tkt-bd484a090c.test index 3d2b599958..7867c8dc97 100644 --- a/test/tkt-bd484a090c.test +++ b/test/tkt-bd484a090c.test @@ -30,7 +30,7 @@ do_test 2.1 { catchsql { SELECT datetime('now', 'localtime') } } {1 {local time unavailable}} do_test 2.2 { - catchsql { SELECT datetime('now', 'utc') } + catchsql { SELECT datetime('2000-01-01', 'utc') } } {1 {local time unavailable}} sqlite3_test_control SQLITE_TESTCTRL_LOCALTIME_FAULT 0 diff --git a/test/tkt-c694113d5.test b/test/tkt-c694113d5.test new file mode 100644 index 0000000000..bac91fc900 --- /dev/null +++ b/test/tkt-c694113d5.test @@ -0,0 +1,36 @@ +# 2018-07-24 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. Specifically, +# it tests that ticket [c694113e50321afdf952e2d1235b08ba663f8399]: +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +do_test tkt-c694113d5.100 { + sqlite3 db :memory: + db eval { + CREATE TABLE t1(a INTEGER PRIMARY KEY); + CREATE TABLE t2(d INTEGER PRIMARY KEY,e,f); + INSERT INTO t1(a) VALUES(1),(2),(3),(4); + } + set answer {} + db eval {SELECT a FROM t1 WHERE NOT EXISTS(SELECT 1 FROM t2 WHERE d=a)} { + if {$a==3} { + lappend answer "CREATE INDEX" + db eval {CREATE INDEX t2e ON t2(e);} + } + lappend answer "a=$a" + } + set answer +} {a=1 a=2 {CREATE INDEX} a=3 a=4} + +finish_test diff --git a/test/tkt-cbd054fa6b.test b/test/tkt-cbd054fa6b.test index 2951233a5b..435d807873 100644 --- a/test/tkt-cbd054fa6b.test +++ b/test/tkt-cbd054fa6b.test @@ -16,7 +16,7 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl -ifcapable !stat4&&!stat3 { +ifcapable !stat4 { finish_test return } @@ -55,24 +55,17 @@ do_test tkt-cbd05-1.1 { } {10} do_test tkt-cbd05-1.2 { db eval { ANALYZE; } - ifcapable stat4 { - db eval { - PRAGMA writable_schema = 1; - CREATE VIEW vvv AS - SELECT tbl,idx,neq,nlt,ndlt,test_extract(sample,0) AS sample - FROM sqlite_stat4; - PRAGMA writable_schema = 0; - } - } else { - db eval { - CREATE VIEW vvv AS - SELECT tbl,idx,neq,nlt,ndlt,sample FROM sqlite_stat3; - } + db eval { + PRAGMA writable_schema = 1; + CREATE VIEW vvv AS + SELECT tbl,idx,neq,nlt,ndlt,test_extract(sample,0) AS sample + FROM sqlite_stat4; + PRAGMA writable_schema = 0; } } {} do_test tkt-cbd05-1.3 { execsql { - SELECT tbl,idx,group_concat(s(sample),' ') + SELECT tbl,idx,string_agg(s(sample),' ') FROM vvv WHERE idx = 't1_x' GROUP BY tbl,idx diff --git a/test/tkt-f67b41381a.test b/test/tkt-f67b41381a.test index 1ddec988cd..43e5cc7dbb 100644 --- a/test/tkt-f67b41381a.test +++ b/test/tkt-f67b41381a.test @@ -15,6 +15,11 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl set testprefix tkt-f67b41381a +ifcapable !altertable { + finish_test + return +} + do_execsql_test 1.0 { CREATE TABLE t1(a); INSERT INTO t1 VALUES(1); diff --git a/test/tkt-f7b4edec.test b/test/tkt-f7b4edec.test index f6d3d5b95a..d21da31917 100644 --- a/test/tkt-f7b4edec.test +++ b/test/tkt-f7b4edec.test @@ -16,6 +16,7 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl +ifcapable !shared_cache { finish_test ; return } # Open two database connections to the same database file in # shared cache mode. Create update hooks that will fire on diff --git a/test/tkt2854.test b/test/tkt2854.test index 914ddd1b69..e5ef2da7c0 100644 --- a/test/tkt2854.test +++ b/test/tkt2854.test @@ -28,17 +28,7 @@ set ::enable_shared_cache [sqlite3_enable_shared_cache 1] do_test tkt2854-1.1 { sqlite3 db test.db sqlite3 db2 test.db - - # This is taken from shared.test. The Windows VFS expands - # ./test.db (and test.db) to be the same thing so the path - # matches and they share a cache. By changing the case - # for Windows platform, we get around this and get a separate - # connection. - if {$::tcl_platform(platform)=="unix"} { - sqlite3 db3 ./test.db - } else { - sqlite3 db3 TEST.DB - } + sqlite3 db3 "file:test.db?cache=private" -uri 1 db eval { CREATE TABLE abc(a, b, c); diff --git a/test/tkt3292.test b/test/tkt3292.test index 0f95244643..717a29c9a0 100644 --- a/test/tkt3292.test +++ b/test/tkt3292.test @@ -20,8 +20,8 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl do_test tkt3292-1.1 { + sqlite3_db_config db LEGACY_FILE_FORMAT 0 execsql { - PRAGMA legacy_file_format=OFF; CREATE TABLE t1(a INTEGER PRIMARY KEY, b INT); INSERT INTO t1 VALUES(0, 1); INSERT INTO t1 VALUES(1, 1); diff --git a/test/tkt3334.test b/test/tkt3334.test index 5473ab4cf8..3527932de3 100644 --- a/test/tkt3334.test +++ b/test/tkt3334.test @@ -82,3 +82,5 @@ do_test tkt3334-1.10 { SELECT count(*) FROM (SELECT a FROM t1) WHERE a=1; } } {3} + +finish_test diff --git a/test/tkt3442.test b/test/tkt3442.test index ee9169ce2c..aaf2662398 100644 --- a/test/tkt3442.test +++ b/test/tkt3442.test @@ -34,35 +34,25 @@ do_test tkt3442-1.1 { } } {} - -# Explain Query Plan -# -proc EQP {sql} { - uplevel "execsql {EXPLAIN QUERY PLAN $sql}" -} - - # These tests perform an EXPLAIN QUERY PLAN on both versions of the # SELECT referenced in ticket #3442 (both '5000' and "5000") # and verify that the query plan is the same. # -ifcapable explain { - do_test tkt3442-1.2 { - EQP { SELECT node FROM listhash WHERE id='5000' LIMIT 1; } - } {0 0 0 {SEARCH TABLE listhash USING INDEX ididx (id=?)}} - do_test tkt3442-1.3 { - EQP { SELECT node FROM listhash WHERE id="5000" LIMIT 1; } - } {0 0 0 {SEARCH TABLE listhash USING INDEX ididx (id=?)}} -} +sqlite3_db_config db SQLITE_DBCONFIG_DQS_DML 1 +do_eqp_test tkt3442-1.2 { + SELECT node FROM listhash WHERE id='5000' LIMIT 1; +} {SEARCH listhash USING INDEX ididx (id=?)} +do_eqp_test tkt3442-1.3 { + SELECT node FROM listhash WHERE id="5000" LIMIT 1; +} {SEARCH listhash USING INDEX ididx (id=?)} # Some extra tests testing other permutations of 5000. # -ifcapable explain { - do_test tkt3442-1.4 { - EQP { SELECT node FROM listhash WHERE id=5000 LIMIT 1; } - } {0 0 0 {SEARCH TABLE listhash USING INDEX ididx (id=?)}} -} +do_eqp_test tkt3442-1.4 { + SELECT node FROM listhash WHERE id=5000 LIMIT 1; +} {SEARCH listhash USING INDEX ididx (id=?)} + do_test tkt3442-1.5 { catchsql { SELECT node FROM listhash WHERE id=[5000] LIMIT 1; diff --git a/test/tkt3457.test b/test/tkt3457.test index 0475741322..17b6c72cd4 100644 --- a/test/tkt3457.test +++ b/test/tkt3457.test @@ -15,7 +15,11 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl -if {$tcl_platform(platform) != "unix"} { +if {[llength [info commands test_syscall]]==0} { + finish_test + return +} +if {[atomic_batch_write test.db]} { finish_test return } @@ -54,7 +58,7 @@ do_test tkt3457-1.1 { # start of the first journal-header has not been written by SQLite. # So write it now. set fd [open bak.db-journal a+] - fconfigure $fd -encoding binary -translation binary + fconfigure $fd -translation binary seek $fd 0 puts -nonewline $fd "\xd9\xd5\x05\xf9\x20\xa1\x63\xd7" close $fd diff --git a/test/tkt3630.test b/test/tkt3630.test index b329c48dcf..1fe7ce70fa 100644 --- a/test/tkt3630.test +++ b/test/tkt3630.test @@ -23,7 +23,7 @@ source $testdir/tester.tcl do_test tkt3630-1 { db eval { CREATE TEMP TABLE temp1(a,b,c); - SELECT * FROM sqlite_temp_master WHERE sql GLOB '*TEMP*'; + SELECT * FROM temp.sqlite_master WHERE sql GLOB '*TEMP*'; } } {} do_test tkt3630-2 { @@ -39,7 +39,7 @@ ifcapable altertable { db eval { ALTER TABLE temp2 ADD COLUMN d; ALTER TABLE temp2 RENAME TO temp2rn; - SELECT name FROM sqlite_temp_master WHERE name LIKE 'temp2%'; + SELECT name FROM temp.sqlite_master WHERE name LIKE 'temp2%'; } } {temp2rn} } diff --git a/test/tkt3793.test b/test/tkt3793.test index 074aab2df0..07e47eb468 100644 --- a/test/tkt3793.test +++ b/test/tkt3793.test @@ -13,7 +13,6 @@ # This file implements tests to verify that ticket #3793 has been # fixed. # -# $Id: tkt3793.test,v 1.2 2009/06/01 16:42:18 shane Exp $ set testdir [file dirname $argv0] @@ -26,18 +25,10 @@ ifcapable !shared_cache||!attach { set ::enable_shared_cache [sqlite3_enable_shared_cache 1] do_test tkt3793-1.1 { - # This is taken from shared.test. The Windows VFS expands - # ./test.db (and test.db) to be the same thing so the path - # matches and they share a cache. By changing the case - # for Windows platform, we get around this and get a separate - # connection. - if {$::tcl_platform(platform)=="unix"} { - sqlite3 db1 test.db - sqlite3 db2 test.db - } else { - sqlite3 db1 TEST.DB - sqlite3 db2 TEST.DB - } + db close + sqlite3 db "file:test.db" -uri 1 + sqlite3 db1 "file:test.db?cache=private" -uri 1 + sqlite3 db2 "file:test.db?cache=shared" -uri 1 execsql { BEGIN; CREATE TABLE t1(a, b); diff --git a/test/tkt3810.test b/test/tkt3810.test index 7e1b2ec5c9..e0798aacb4 100644 --- a/test/tkt3810.test +++ b/test/tkt3810.test @@ -62,7 +62,7 @@ do_test tkt3810-3 { # an orphan. # do_test tkt3810-4 { - execsql {SELECT name FROM sqlite_temp_master ORDER BY name} + execsql {SELECT name FROM temp.sqlite_master ORDER BY name} } {r1} # Because it is an orphan, it cannot be dropped. @@ -84,4 +84,20 @@ do_test tkt3810-6 { db2 close +# 2020-11-06 forum post https://sqlite.org/forum/forumpost/157dc791df +# +reset_db +do_test tkt3810-100 { + db eval { + ATTACH ':memory:' AS aux1; + CREATE TABLE aux1.t1(x); + CREATE TEMP TRIGGER r1 DELETE ON t1 BEGIN SELECT *; END; + CREATE VIEW t1 AS SELECT *; + } + catch {db eval { + CREATE VIRTUAL TABLE t2 USING nosuchmodule; + }} + db eval {CREATE TABLE t3(z);} +} {} + finish_test diff --git a/test/tkt3841.test b/test/tkt3841.test index df6de5c2f3..542c1bb5e7 100644 --- a/test/tkt3841.test +++ b/test/tkt3841.test @@ -22,6 +22,7 @@ ifcapable !subquery { return } +sqlite3_db_config db SQLITE_DBCONFIG_DQS_DML 1 do_test tkt3841.1 { execsql { CREATE TABLE table2 (key TEXT, x TEXT); diff --git a/test/tkt3935.test b/test/tkt3935.test index abbeb3f866..a4f1f61dad 100644 --- a/test/tkt3935.test +++ b/test/tkt3935.test @@ -34,13 +34,13 @@ do_test tkt3935.3 { do_test tkt3935.4 { catchsql { SELECT a FROM (t1) AS t ON b USING(a) } -} {1 {a JOIN clause is required before ON}} +} {1 {near "USING": syntax error}} do_test tkt3935.5 { catchsql { SELECT a FROM (t1) AS t ON b } } {1 {a JOIN clause is required before ON}} do_test tkt3935.6 { catchsql { SELECT a FROM (SELECT * FROM t1) AS t ON b USING(a) } -} {1 {a JOIN clause is required before ON}} +} {1 {near "USING": syntax error}} do_test tkt3935.7 { catchsql { SELECT a FROM (SELECT * FROM t1) AS t ON b } } {1 {a JOIN clause is required before ON}} @@ -49,7 +49,7 @@ do_test tkt3935.8 { } {1 {a JOIN clause is required before ON}} do_test tkt3935.9 { catchsql { SELECT a FROM t1 AS t ON b USING(a) } -} {1 {a JOIN clause is required before ON}} +} {1 {near "USING": syntax error}} do_test tkt3935.10 { catchsql { SELECT a FROM t1 AS t USING(a) } } {1 {a JOIN clause is required before USING}} diff --git a/test/tkt4018.test b/test/tkt4018.test index 2bc41d47aa..77582a5c41 100644 --- a/test/tkt4018.test +++ b/test/tkt4018.test @@ -16,6 +16,7 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl +do_not_use_codec proc testsql {sql} { set fd [open tf_main.tcl w] diff --git a/test/tpch01.test b/test/tpch01.test index ce48f8ec23..338ac48a07 100644 --- a/test/tpch01.test +++ b/test/tpch01.test @@ -165,13 +165,13 @@ do_test tpch01-1.1 { order by o_year;}] set ::eqpres -} {/0 0 0 {SEARCH TABLE part USING INDEX bootleg_pti .P_TYPE=..} 0 1 2 {SEARCH TABLE lineitem USING INDEX lpki2 .L_PARTKEY=..}.*/} +} {/*SEARCH part USING INDEX bootleg_pti *SEARCH lineitem USING INDEX lpki2*/} do_test tpch01-1.1b { set ::eqpres -} {/.* customer .* nation AS n1 .*/} +} {/.* customer .* n1 .*/} do_test tpch01-1.1c { set ::eqpres -} {/.* supplier .* nation AS n2 .*/} +} {/.* supplier .* n2 .*/} do_eqp_test tpch01-1.2 { select @@ -187,6 +187,14 @@ group by c_custkey, c_name, c_acctbal, c_phone, n_name, c_address, c_comment order by revenue desc; -} {0 0 1 {SEARCH TABLE orders USING INDEX odi (O_ORDERDATE>? AND O_ORDERDATE<?)} 0 1 0 {SEARCH TABLE customer USING INDEX cpki (C_CUSTKEY=?)} 0 2 3 {SEARCH TABLE nation USING INDEX npki (N_NATIONKEY=?)} 0 3 2 {SEARCH TABLE lineitem USING INDEX lpki (L_ORDERKEY=?)} 0 0 0 {USE TEMP B-TREE FOR GROUP BY} 0 0 0 {USE TEMP B-TREE FOR ORDER BY}} +} { + QUERY PLAN + |--SEARCH orders USING INDEX odi (O_ORDERDATE>? AND O_ORDERDATE<?) + |--SEARCH customer USING INDEX cpki (C_CUSTKEY=?) + |--SEARCH nation USING INDEX npki (N_NATIONKEY=?) + |--SEARCH lineitem USING INDEX lpki (L_ORDERKEY=?) + |--USE TEMP B-TREE FOR GROUP BY + `--USE TEMP B-TREE FOR ORDER BY +} finish_test diff --git a/test/trace.test b/test/trace.test index fd51d7ab64..37914857fc 100644 --- a/test/trace.test +++ b/test/trace.test @@ -197,7 +197,7 @@ ifcapable trigger { UPDATE t1 SET a=a+1; } set TRACE_OUT - } {{UPDATE t1 SET a=a+1;} {-- TRIGGER r1t1} {-- TRIGGER r1t2} {-- TRIGGER r1t1} {-- TRIGGER r1t2} {-- TRIGGER r1t1} {-- TRIGGER r1t2}} + } {{UPDATE t1 SET a=a+1;} {-- TRIGGER r1t1} {-- UPDATE t2 SET a=new.a WHERE rowid=new.rowid} {-- TRIGGER r1t2} {-- SELECT 'hello'} {-- TRIGGER r1t1} {-- UPDATE t2 SET a=new.a WHERE rowid=new.rowid} {-- TRIGGER r1t2} {-- SELECT 'hello'} {-- TRIGGER r1t1} {-- UPDATE t2 SET a=new.a WHERE rowid=new.rowid} {-- TRIGGER r1t2} {-- SELECT 'hello'}} } # With 3.6.21, we add the ability to expand host parameters in the trace diff --git a/test/trace3.test b/test/trace3.test new file mode 100644 index 0000000000..639aefafa6 --- /dev/null +++ b/test/trace3.test @@ -0,0 +1,362 @@ +# 2016 July 14 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The focus of +# this test file is the "sqlite3_trace_v2()" and "sqlite3_expanded_sql()" +# APIs. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +ifcapable !trace { finish_test ; return } +set ::testprefix trace3 + +proc trace_v2_error { args } { + lappend ::stmtlist(error) [string trim $args] + error "trace error"; # this will be ignored. +} +proc trace_v2_record { args } { + lappend ::stmtlist(record) [string trim $args] +} +proc trace_v2_nop { args } {}; # do nothing. + +do_test trace3-1.0 { + execsql { + CREATE TABLE t1(a,b); + INSERT INTO t1 VALUES(1,NULL); + INSERT INTO t1 VALUES(2,-1); + INSERT INTO t1 VALUES(3,0); + INSERT INTO t1 VALUES(4,1); + INSERT INTO t1 VALUES(5,-2147483648); + INSERT INTO t1 VALUES(6,2147483647); + INSERT INTO t1 VALUES(7,-9223372036854775808); + INSERT INTO t1 VALUES(8,9223372036854775807); + INSERT INTO t1 VALUES(9,-1.0); + INSERT INTO t1 VALUES(10,0.0); + INSERT INTO t1 VALUES(11,1.0); + INSERT INTO t1 VALUES(12,''); + INSERT INTO t1 VALUES(13,'1'); + INSERT INTO t1 VALUES(14,'one'); + INSERT INTO t1 VALUES(15,x'abcd0123'); + INSERT INTO t1 VALUES(16,x'4567cdef'); + } +} {} + +do_test trace3-1.1 { + set rc [catch {db trace_v2 1 2 3} msg] + lappend rc $msg +} {1 {wrong # args: should be "db trace_v2 ?CALLBACK? ?MASK?"}} +do_test trace3-1.2 { + set rc [catch {db trace_v2 1 bad} msg] + lappend rc $msg +} {1 {bad trace type "bad": must be statement, profile, row, or close}} + +do_test trace3-2.1 { + db trace_v2 trace_v2_nop + db trace_v2 +} {trace_v2_nop} + +do_test trace3-3.1 { + unset -nocomplain ::stmtlist + db trace_v2 trace_v2_nop + execsql { + SELECT a, b FROM t1 ORDER BY a; + } + array get ::stmtlist +} {} +do_test trace3-3.2 { + set ::stmtlist(error) {} + db trace_v2 trace_v2_error + execsql { + SELECT a, b FROM t1 ORDER BY a; + } + set ::stmtlist(error) +} {/^\{-?\d+ \{SELECT a, b FROM t1 ORDER BY a;\}\}$/} +do_test trace3-3.3 { + set ::stmtlist(record) {} + db trace_v2 trace_v2_record + execsql { + SELECT a, b FROM t1 ORDER BY a; + } + set ::stmtlist(record) +} {/^\{-?\d+ \{SELECT a, b FROM t1 ORDER BY a;\}\}$/} +do_test trace3-3.4 { + set ::stmtlist(record) {} + db trace_v2 trace_v2_record statement + execsql { + SELECT a, b FROM t1 ORDER BY a; + } + set ::stmtlist(record) +} {/^\{-?\d+ \{SELECT a, b FROM t1 ORDER BY a;\}\}$/} +do_test trace3-3.5 { + set ::stmtlist(record) {} + db trace_v2 trace_v2_record 1 + execsql { + SELECT a, b FROM t1 ORDER BY a; + } + set ::stmtlist(record) +} {/^\{-?\d+ \{SELECT a, b FROM t1 ORDER BY a;\}\}$/} + +do_test trace3-4.1 { + set ::stmtlist(record) {} + db trace_v2 trace_v2_record profile + execsql { + SELECT a, b FROM t1 ORDER BY a; + } + set ::stmtlist(record) +} {/^\{-?\d+ -?\d+\}$/} +do_test trace3-4.2 { + set ::stmtlist(record) {} + db trace_v2 trace_v2_record 2 + execsql { + SELECT a, b FROM t1 ORDER BY a; + } + set ::stmtlist(record) +} {/^\{-?\d+ -?\d+\}$/} + +do_test trace3-4.3 { + set ::stmtlist(record) {} + db trace_v2 trace_v2_record profile + execsql { + SELECT a, b FROM t1 ORDER BY a; + } + set stmt [lindex [lindex $::stmtlist(record) 0] 0] + set ns [lindex [lindex $::stmtlist(record) 0] 1] + list $stmt [expr {$ns >= 0 && $ns <= 9999999}]; # less than 0.010 seconds +} {/^-?\d+ 1$/} +do_test trace3-4.4 { + set cnt 0 + while {1} { + set ::stmtlist(record) {} + db trace_v2 trace_v2_record 2 + execsql { + SELECT a, b FROM t1 ORDER BY a; + } + set stmt [lindex [lindex $::stmtlist(record) 0] 0] + set ns [lindex [lindex $::stmtlist(record) 0] 1] + if {$ns<0 || $ns>9999999} { #less than 0.010 seconds + incr cnt + if {$cnt>3} { + set res "time out of bounds. Expected less than 99999999. Got $ns" + break + } + } else { + set res 1 + break + } + } + list $stmt $res +} {/^-?\d+ 1$/} + +do_test trace3-5.1 { + set ::stmtlist(record) {} + db trace_v2 trace_v2_record row + execsql { + SELECT a, b FROM t1 ORDER BY a; + } + set ::stmtlist(record) +} "/^[string trim [string repeat {-?\d+ } 16]]\$/" +do_test trace3-5.2 { + set ::stmtlist(record) {} + db trace_v2 trace_v2_record 4 + execsql { + SELECT a, b FROM t1 ORDER BY a; + } + set ::stmtlist(record) +} "/^[string trim [string repeat {-?\d+ } 16]]\$/" + +do_test trace3-6.1 { + set ::stmtlist(record) {} + db trace_v2 trace_v2_record {profile row} + execsql { + SELECT a, b FROM t1 ORDER BY a; + } + set ::stmtlist(record) +} "/^[string trim [string repeat {-?\d+ } 16]] \\\{-?\\d+ -?\\d+\\\}\$/" +do_test trace3-6.2 { + set ::stmtlist(record) {} + db trace_v2 trace_v2_record {statement profile row} + execsql { + SELECT a, b FROM t1 ORDER BY a; + } + set ::stmtlist(record) +} "/^\\\{-?\\d+ \\\{SELECT a, b FROM t1 ORDER BY a;\\\}\\\} [string trim \ +[string repeat {-?\d+ } 16]] \\\{-?\\d+ -?\\d+\\\}\$/" + +do_test trace3-7.1 { + set DB [sqlite3_connection_pointer db] + + set STMT [sqlite3_prepare_v2 $DB \ + "SELECT a, b FROM t1 WHERE b = ? ORDER BY a;" -1 TAIL] +} {/^[0-9A-Fa-f]+$/} + +do_test trace3-8.1 { + list [sqlite3_bind_null $STMT 1] [sqlite3_expanded_sql $STMT] +} {{} {SELECT a, b FROM t1 WHERE b = NULL ORDER BY a;}} +do_test trace3-8.2 { + list [sqlite3_bind_int $STMT 1 123] [sqlite3_expanded_sql $STMT] +} {{} {SELECT a, b FROM t1 WHERE b = 123 ORDER BY a;}} +do_test trace3-8.3 { + list [sqlite3_bind_int64 $STMT 1 123] [sqlite3_expanded_sql $STMT] +} {{} {SELECT a, b FROM t1 WHERE b = 123 ORDER BY a;}} +do_test trace3-8.4 { + list [sqlite3_bind_text $STMT 1 "some string" 11] \ + [sqlite3_expanded_sql $STMT] +} {{} {SELECT a, b FROM t1 WHERE b = 'some string' ORDER BY a;}} +do_test trace3-8.5 { + list [sqlite3_bind_text $STMT 1 "some 'bad' string" 17] \ + [sqlite3_expanded_sql $STMT] +} {{} {SELECT a, b FROM t1 WHERE b = 'some ''bad'' string' ORDER BY a;}} +do_test trace3-8.6 { + list [sqlite3_bind_double $STMT 1 123] [sqlite3_expanded_sql $STMT] +} {{} {SELECT a, b FROM t1 WHERE b = 123.0 ORDER BY a;}} +do_test trace3-8.7 { + list [sqlite3_bind_text16 $STMT 1 \ + [encoding convertto unicode hi\000yall\000] 16] \ + [sqlite3_expanded_sql $STMT] +} {{} {SELECT a, b FROM t1 WHERE b = 'hi' ORDER BY a;}} +do_test trace3-8.8 { + list [sqlite3_bind_blob $STMT 1 "\x12\x34\x56" 3] \ + [sqlite3_expanded_sql $STMT] +} {{} {SELECT a, b FROM t1 WHERE b = x'123456' ORDER BY a;}} +do_test trace3-8.9 { + list [sqlite3_bind_blob $STMT 1 "\xAB\xCD\xEF" 3] \ + [sqlite3_expanded_sql $STMT] +} {{} {SELECT a, b FROM t1 WHERE b = x'abcdef' ORDER BY a;}} + +do_test trace3-9.1 { + sqlite3_finalize $STMT +} {SQLITE_OK} + +do_test trace3-10.1 { + db trace_v2 "" + db trace_v2 +} {} +do_test trace3-10.2 { + unset -nocomplain ::stmtlist + db trace_v2 "" {statement profile row} + execsql { + SELECT a, b FROM t1 ORDER BY a; + } + array get ::stmtlist +} {} + +do_test trace3-11.1 { + set ::stmtlist(record) {} + db trace_v2 trace_v2_record close + db close + set ::stmtlist(record) +} {/^-?\d+$/} + +reset_db + +do_test trace3-11.2 { + set ::stmtlist(record) {} + db trace_v2 trace_v2_record 8 + db close + set ::stmtlist(record) +} {/^-?\d+$/} + +#------------------------------------------------------------------------- +reset_db +do_test 12.1.0 { + set ::STMT [sqlite3_prepare_v2 $DB \ + "SELECT ?1 || ?1 || ?1 || ?2 || ?3 || ?4 || ? || ?1 || ?" -1 TAIL + ] + sqlite3_bind_parameter_count $::STMT +} {6} + +do_test 12.1.1 { + sqlite3_bind_text $STMT 1 "A" 1 + sqlite3_bind_text $STMT 2 "B" 1 + sqlite3_bind_text $STMT 3 "C" 1 + sqlite3_bind_text $STMT 4 "D" 1 + sqlite3_bind_text $STMT 5 "E" 1 + sqlite3_bind_text $STMT 6 "F" 1 + sqlite3_expanded_sql $STMT +} {SELECT 'A' || 'A' || 'A' || 'B' || 'C' || 'D' || 'E' || 'A' || 'F'} + +do_test 12.1.2 { + sqlite3_step $STMT + sqlite3_column_text $STMT 0 +} {AAABCDEAF} + +do_test 12.1.3 { + sqlite3_finalize $STMT +} {SQLITE_OK} + +do_test 12.2.0 { + execsql { + CREATE TABLE nameFtsFuzzySearchTable( + word, distance, langid, score, top, scope + ); + } + set ::STMT [sqlite3_prepare_v2 $DB { + SELECT + substr(word,1,length(?1)-1) AS term, + distance, + langid, + score + FROM + nameFtsFuzzySearchTable + WHERE + word MATCH (?1) AND abs(?1) = abs(term) + AND top = ?2 AND distance > ?3 AND scope = ?4 AND langid = ? + GROUP BY term, langid + HAVING (1.0 - ((distance / 100.0) / CAST( length(?1) - 1 AS REAL ))) >= ? + } -1 TAIL] + sqlite3_bind_parameter_count $::STMT +} {6} + +do_test 12.1.1 { + sqlite3_bind_text $STMT 1 "A" 1 + sqlite3_bind_text $STMT 2 "B" 1 + sqlite3_bind_text $STMT 3 "C" 1 + sqlite3_bind_text $STMT 4 "D" 1 + sqlite3_bind_text $STMT 5 "E" 1 + sqlite3_bind_text $STMT 6 "F" 1 + sqlite3_expanded_sql $STMT +} { + SELECT + substr(word,1,length('A')-1) AS term, + distance, + langid, + score + FROM + nameFtsFuzzySearchTable + WHERE + word MATCH ('A') AND abs('A') = abs(term) + AND top = 'B' AND distance > 'C' AND scope = 'D' AND langid = 'E' + GROUP BY term, langid + HAVING (1.0 - ((distance / 100.0) / CAST( length('A') - 1 AS REAL ))) >= 'F' + } + +do_test 12.1.2 { + sqlite3_finalize $STMT +} {SQLITE_OK} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 13.0 { + CREATE TABLE T1(a, b); + INSERT INTO t1 VALUES(1, 2), (3, 4); +} + +proc trace_callback {args} {} +db trace_v2 trace_callback profile + +do_test 13.1 { + db eval { SELECT * FROM t1 } { + db trace_v2 "" "" + } + set {} {} +} {} + +finish_test diff --git a/test/trans.test b/test/trans.test index bbaedc5450..6b8ad217a0 100644 --- a/test/trans.test +++ b/test/trans.test @@ -11,7 +11,6 @@ # This file implements regression tests for SQLite library. The # focus of this script is database locks. # -# $Id: trans.test,v 1.41 2009/04/28 16:37:59 danielk1977 Exp $ set testdir [file dirname $argv0] @@ -39,6 +38,19 @@ do_test trans-1.1 { SELECT b FROM two ORDER BY a; } } {I V X} +do_test trans-1.2.1 { + sqlite3_txn_state db +} {0} +do_test trans-1.2.2 { + sqlite3_txn_state db main +} {0} +do_test trans-1.2.3 { + sqlite3_txn_state db temp +} {0} +do_test trans-1.2.4 { + sqlite3_txn_state db no-such-schema +} {-1} + do_test trans-1.9 { sqlite3 altdb test.db execsql {SELECT b FROM one ORDER BY a} altdb @@ -55,6 +67,9 @@ do_test trans-2.1 { set v [catch {execsql {BEGIN}} msg] lappend v $msg } {0 {}} +do_test trans-2.1b { + sqlite3_txn_state db +} {0} do_test trans-2.2 { set v [catch {execsql {END}} msg] lappend v $msg @@ -95,6 +110,16 @@ do_test trans-3.1 { SELECT a FROM one ORDER BY a; } } {1 2 3} +do_test trans-3.1b { + sqlite3_txn_state db +} {2} +do_test trans-3.1c { + sqlite3_txn_state db main +} {2} +do_test trans-3.1d { + sqlite3_txn_state db temp +} {0} + do_test trans-3.2 { catchsql { SELECT a FROM two ORDER BY a; @@ -139,6 +164,10 @@ do_test trans-3.9 { do_test trans-3.10 { execsql {END TRANSACTION} } {} +do_test trans-3.10b { + sqlite3_txn_state db +} {0} + do_test trans-3.11 { set v [catch {execsql { @@ -252,6 +281,15 @@ do_test trans-5.2 { execsql {BEGIN TRANSACTION} execsql {SELECT name FROM sqlite_master WHERE type='table' ORDER BY name} } {} +do_test trans-5.2b { + sqlite3_txn_state db +} {1} +do_test trans-5.2c { + sqlite3_txn_state db main +} {1} +do_test trans-5.2d { + sqlite3_txn_state db temp +} {0} do_test trans-5.3 { execsql {CREATE TABLE one(a text, b int)} execsql {SELECT name FROM sqlite_master WHERE type='table' ORDER BY name} diff --git a/test/transitive1.test b/test/transitive1.test index 97dc5a71a9..80c53e8ffb 100644 --- a/test/transitive1.test +++ b/test/transitive1.test @@ -344,13 +344,86 @@ do_execsql_test transitive1-560 { do_execsql_test transitive1-560eqp { EXPLAIN QUERY PLAN SELECT * FROM c1 WHERE x=y AND y=z AND z='abc'; -} {/SCAN TABLE c1/} +} {/SCAN c1/} do_execsql_test transitive1-570 { SELECT * FROM c1 WHERE x=y AND z=y AND z='abc'; } {} do_execsql_test transitive1-570eqp { EXPLAIN QUERY PLAN SELECT * FROM c1 WHERE x=y AND z=y AND z='abc'; -} {/SEARCH TABLE c1 USING INDEX c1x/} +} {/SEARCH c1 USING INDEX c1x/} + +# 2021-05-04 forum https://sqlite.org/forum/forumpost/eb8613976a +reset_db +do_execsql_test transitive1-600 { + CREATE TABLE t0(a0 INT, b1 INT); + CREATE INDEX t0b1 ON t0(b1); + CREATE TABLE t1(w,x,y,z3 INT); + INSERT INTO t0(a0, b1) VALUES (0,1); + INSERT INTO t1(w,x,y,z3) VALUES (7,8,9,1); +} {} +do_execsql_test transitive1-610 { + SELECT ALL * FROM t0,t1 WHERE b1=z3 AND a0=z3; +} {} +do_execsql_test transitive1-620 { + SELECT ALL * FROM t0,t1 WHERE likely(b1=z3) AND a0=z3; +} {} +do_execsql_test transitive1-630 { + DROP TABLE t0; + DROP TABLE t1; + CREATE TABLE t0(c0 INT, c1 INT UNIQUE); + CREATE TABLE t1(c0 INT); + INSERT INTO t0(c0, c1) VALUES (0, 1); + INSERT INTO t1(c0) VALUES (1); + SELECT ALL * FROM t1 NATURAL JOIN t0 WHERE (t1.c0=t0.c1); + SELECT ALL * FROM t1 NATURAL JOIN t0 WHERE (likely(t1.c0=t0.c1)); + SELECT ALL * FROM t1,t0 WHERE (likely(t1.c0=t0.c1) AND t1.c0=t0.c0); +} {} + +#------------------------------------------------------------------------- +# 2021-08-31 forum https://sqlite.org/forum/forumpost/8d1b58f112 +reset_db +do_execsql_test transitive1-700 { + CREATE TABLE t1(a INT PRIMARY KEY); + INSERT INTO t1(a) VALUES(1),(2),(3); + CREATE TABLE t2(x INTEGER PRIMARY KEY,y INT); + INSERT INTO t2(y) VALUES(2),(3); +} + +do_execsql_test transitive1-710 { + SELECT * FROM t1 CROSS JOIN t2 WHERE t2.y=t1.a AND t1.a=t2.x +} {} + +do_execsql_test transitive1-720 { + SELECT * FROM t1 CROSS JOIN t2 WHERE likely(t2.y=t1.a) AND unlikely(t1.a=t2.x) +} {} + +# 2021-10-04 forum https://sqlite.org/forum/forumpost/a65cacbf5e1c41ba +# +reset_db +do_execsql_test transitive1-800 { + CREATE TABLE t1(a INT); + INSERT INTO t1 VALUES(0),(3); + CREATE TABLE t2(b INT UNIQUE, c INT); + INSERT INTO t2 VALUES(1,4) ,(0,5); + SELECT * FROM t1 WHERE EXISTS (SELECT 1 FROM t2 WHERE c=a AND b IS a); + SELECT * FROM t1 WHERE EXISTS (SELECT 1 FROM t2 WHERE a=c AND a IS b); + SELECT * FROM t1 WHERE EXISTS (SELECT 1 FROM t2 WHERE a=c AND b IS a); + SELECT * FROM t1 WHERE EXISTS (SELECT 1 FROM t2 WHERE c=a AND a IS b); +} {} +do_execsql_test transitive1-810 { + CREATE TABLE t3(a INTEGER PRIMARY KEY,b); + INSERT INTO t3(a,b) VALUES(1,2),(5,5),(7,11); + SELECT * FROM t3 WHERE a=b AND a='5'; +} {5 5} +do_execsql_test transitive1-811 { + SELECT * FROM t3 WHERE a=b AND a='4'; +} {} +do_execsql_test transitive1-812 { + SELECT * FROM t3 WHERE a=b AND a='7'; +} {} +do_execsql_test transitive1-813 { + SELECT * FROM t3 WHERE a=b AND a='5x'; +} {} finish_test diff --git a/test/trigger1.test b/test/trigger1.test index a190efd464..67943677f9 100644 --- a/test/trigger1.test +++ b/test/trigger1.test @@ -728,4 +728,127 @@ do_execsql_test trigger1-17.0 { PRAGMA integrity_check; } {ok} +# 2018-04-26 +# When a BEFORE UPDATE trigger changes a column value in a row being +# updated, and that column value is used by the UPDATE to change other +# column, the value used to compute the update is from before the trigger. +# In the example that follows, the value of "b" in "c=b" is 2 (the value +# prior to running the BEFORE UPDATE trigger) not 1000. +# +do_execsql_test trigger1-18.0 { + CREATE TABLE t18(a PRIMARY KEY,b,c); + INSERT INTO t18(a,b,c) VALUES(1,2,3); + CREATE TRIGGER t18r1 BEFORE UPDATE ON t18 BEGIN + UPDATE t18 SET b=1000 WHERE a=old.a; + END; + UPDATE t18 SET c=b WHERE a=1; + SELECT * FROM t18; +} {1 1000 2} ;# Not: 1 1000 1000 +do_execsql_test trigger1-18.1 { + DELETE FROM t18; + INSERT INTO t18(a,b,c) VALUES(1,2,3); + UPDATE t18 SET c=b, b=b+1 WHERE a=1; + SELECT * FROM t18; +} {1 3 2} ;# Not: 1 1001 1000 + +# 2018-04-26 ticket [https://sqlite.org/src/tktview/d85fffd6ffe856092e] +# VDBE Program uses an expired value. +# +do_execsql_test trigger1-19.0 { + CREATE TABLE t19(a INT PRIMARY KEY, b, c)WITHOUT ROWID; + INSERT INTO t19(a,b,c) VALUES(1,2,3); + CREATE TRIGGER t19r3 BEFORE UPDATE ON t19 BEGIN SELECT new.b; END; + UPDATE t19 SET c=b WHERE a=1; + SELECT * FROM t19; +} {1 2 2} +do_execsql_test trigger1-19.1 { + DELETE FROM t19; + INSERT INTO t19(a,b,c) VALUES(1,2,3); + UPDATE t19 SET c=CASE WHEN b=2 THEN b ELSE b+99 END WHERE a=1; + SELECT * FROM t19; +} {1 2 2} + +# 2019-08-26 Chromium sqlite3_fts3_lpm_fuzzer find. +# +db close +sqlite3 db :memory: +do_execsql_test trigger1-20.1 { + CREATE TABLE t20_1(x); + ATTACH ':memory:' AS aux; + CREATE TABLE aux.t20_2(y); + CREATE TABLE aux.t20_3(z); + CREATE TEMP TRIGGER r20_3 AFTER INSERT ON t20_2 BEGIN UPDATE t20_3 SET z=z+1; END; + DETACH aux; + DROP TRIGGER r20_3; +} {} + +# 2019-10-24 ticket 50c09fc2cf0d91ce +# +db close +sqlite3 db :memory: +do_execsql_test trigger1-21.1 { + PRAGMA recursive_triggers = true; + CREATE TABLE t0(a, b, c UNIQUE); + CREATE UNIQUE INDEX i0 ON t0(b) WHERE a; + CREATE TRIGGER tr0 AFTER DELETE ON t0 BEGIN + DELETE FROM t0; + END; + INSERT INTO t0(a,b,c) VALUES(0,0,9),(1,1,1); + REPLACE INTO t0(a,b,c) VALUES(2,0,9); + SELECT * FROM t0; +} {2 0 9} + +# 2020-01-04 From Yongheng +# The test case below caused problems for the register validity +# tracking logic. There was no bug in the release build. The +# only problem was a false-positive in the register validity +# tracking. +# +reset_db +do_execsql_test trigger1-22.10 { + CREATE TABLE t1( + a INTEGER PRIMARY KEY, + b DOUBLE + ); + CREATE TRIGGER x AFTER UPDATE ON t1 BEGIN + SELECT sum(b)OVER(ORDER BY (SELECT b FROM t1 AS x + WHERE b IN (t1.a,127,t1.b) + GROUP BY b)) + FROM t1 + GROUP BY a; + END; + CREATE TEMP TRIGGER x BEFORE INSERT ON t1 BEGIN + UPDATE t1 + SET b=randomblob(10) + WHERE b >= 'E' + AND a < (SELECT a FROM t1 WHERE a<22 GROUP BY b); + END; + INSERT INTO t1(b) VALUES('Y'),('X'),('Z'); + SELECT a, CASE WHEN typeof(b)='text' THEN quote(b) ELSE '<blob>' END, '|' FROM t1; +} {1 <blob> | 2 'X' | 3 'Z' |} + +# 2022-03-06 https://sqlite.org/forum/forumpost/2024e94071 +# Harmless assertion fault following a syntax error. +# +reset_db +do_catchsql_test trigger1-23.1 { + CREATE TABLE t1(a INT); + CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN + INSERT INTO t1 SELECT e_master LIMIT 1,#1; + END; +} {1 {near "#1": syntax error}} + +# 2024-05-08 Allow arbitrary expressions as the 2nd argument to RAISE(). +# +do_catchsql_test trigger1-24.1 { + CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN + SELECT raise(abort,format('attempt to insert %d where is not a power of 2',new.a)) + WHERE (new.a & (new.a-1))!=0; + END; + INSERT INTO t1 VALUES(0),(1),(2),(4),(8),(65536); +} {0 {}} +do_catchsql_test trigger1-24.2 { + INSERT INTO t1 VALUES(9876); +} {1 {attempt to insert 9876 where is not a power of 2}} + finish_test diff --git a/test/trigger2.test b/test/trigger2.test index 7b939bdab7..70d59f3a0b 100644 --- a/test/trigger2.test +++ b/test/trigger2.test @@ -49,6 +49,7 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl +set testprefix trigger2 ifcapable {!trigger} { finish_test return @@ -752,8 +753,40 @@ do_test trigger2-9.1 { } } {} +integrity_check trigger2-9.99 + +# 2019-11-02 Problem found by TH3, related to generated column support. +db close +sqlite3 db :memory: +do_execsql_test trigger2-10.1 { + CREATE TABLE t1(a,b,c,d); + CREATE VIEW v2(a,b,c,d) AS SELECT * FROM t1; + CREATE TRIGGER v2ins INSTEAD OF INSERT ON v2 BEGIN + INSERT INTO t1(a,b,c,d) VALUES(new.a, new.b, new.c, new.d); + END; + INSERT INTO v2(a,d) VALUES(11,14); + SELECT * FROM t1; +} {11 {} {} 14} + } ;# ifcapable view -integrity_check trigger2-9.9 +#------------------------------------------------------------------------- +reset_db +do_execsql_test 11.1 { + CREATE TABLE t1(a INT PRIMARY KEY, b, c REAL, d, e); + CREATE TABLE t2(a INT, b, c REAL, d, e, PRIMARY KEY(a,b)) WITHOUT ROWID; + CREATE UNIQUE INDEX t2c ON t2(c); + CREATE UNIQUE INDEX t2d ON t2(d); + CREATE UNIQUE INDEX t2e ON t2(e); +} + +do_catchsql_test 11.2 { + CREATE TRIGGER r1 BEFORE INSERT ON t1 BEGIN + INSERT INTO t2(a,b,c,d,e) VALUES(91,NULL,93,94,?1) + ON CONFLICT(b,a) DO NOTHING + ON CONFLICT DO UPDATE SET b=?1; + END; +} {1 {trigger cannot use variables}} + finish_test diff --git a/test/trigger7.test b/test/trigger7.test index 847d78cd7a..ecc86dbb13 100644 --- a/test/trigger7.test +++ b/test/trigger7.test @@ -106,6 +106,7 @@ do_test trigger7-3.1 { # in the series. # do_test trigger7-99.1 { + sqlite3_db_config db DEFENSIVE 0 execsql { PRAGMA writable_schema=on; UPDATE sqlite_master SET sql='nonsense'; @@ -113,6 +114,6 @@ do_test trigger7-99.1 { db close catch { sqlite3 db test.db } catchsql { DROP TRIGGER t2r5 } -} {1 {malformed database schema (t2r12)}} +} {/1 {malformed database schema .*}/} finish_test diff --git a/test/trigger9.test b/test/trigger9.test index 326fa63d4c..47940de577 100644 --- a/test/trigger9.test +++ b/test/trigger9.test @@ -242,15 +242,27 @@ do_execsql_test 4.1 { END; } -do_execsql_test 4.2 { - DELETE FROM v1 WHERE rowid=1; -} {} - -do_execsql_test 4.3 { - UPDATE v1 SET a=b WHERE rowid=2; -} {} - - +ifcapable !allow_rowid_in_view { + do_catchsql_test 4.2 { + DELETE FROM v1 WHERE rowid=1; + } {1 {no such column: rowid}} + do_catchsql_test 4.3 { + UPDATE v1 SET a=b WHERE rowid=2; + } {1 {no such column: rowid}} +} else { + do_execsql_test 4.2a { + DELETE FROM log; + } + do_catchsql_test 4.2 { + DELETE FROM v1 WHERE rowid=1; + } {0 {}} + do_catchsql_test 4.3 { + UPDATE v1 SET a=b WHERE rowid=2; + } {0 {}} + do_execsql_test 4.3b { + SELECT * FROM log; + } +} finish_test diff --git a/test/triggerA.test b/test/triggerA.test index 821e2d90e4..598d291295 100644 --- a/test/triggerA.test +++ b/test/triggerA.test @@ -200,13 +200,6 @@ do_test triggerA-2.11 { } } {3 305 3 9900305 4 404 4 9900404 5 504 5 9900504} -# Only run the reamining tests if memory debugging is turned on. -# -ifcapable !memdebug { - puts "Skipping triggerA malloc tests: not compiled with -DSQLITE_MEMDEBUG..." - finish_test - return -} source $testdir/malloc_common.tcl # Save a copy of the current database configuration. diff --git a/test/triggerC.test b/test/triggerC.test index 3e47521fd1..a2a0603873 100644 --- a/test/triggerC.test +++ b/test/triggerC.test @@ -1042,4 +1042,33 @@ do_execsql_test 15.2.1 { do_execsql_test 15.2.2 { SELECT * FROM x2; } {1 2 3 4} do_execsql_test 15.2.3 { SELECT * FROM """x2"""; } {3 11 x y} +#------------------------------------------------------------------------- +# At one point queries such as the following were causing segfaults. +# +do_catchsql_test 16.1 { + SELECT raise(ABORT, 'msg') FROM sqlite_master + UNION SELECT 1 + ORDER BY raise(IGNORE); +} {1 {1st ORDER BY term does not match any column in the result set}} + +do_catchsql_test 16.2 { + SELECT count(*) FROM sqlite_master + GROUP BY raise(IGNORE) + HAVING raise(ABORT, 'msg'); +} {1 {RAISE() may only be used within a trigger-program}} + +#------------------------------------------------------------------------- +# Datatype mismatch on IPK when there are BEFORE triggers. +# +do_execsql_test 17.0 { + CREATE TABLE xyz(x INTEGER PRIMARY KEY, y, z); + CREATE TRIGGER xyz_tr BEFORE INSERT ON xyz BEGIN + SELECT new.x; + END; +} +do_catchsql_test 17.1 { + INSERT INTO xyz VALUES('hello', 2, 3); +} {1 {datatype mismatch}} + + finish_test diff --git a/test/triggerE.test b/test/triggerE.test index a82ac9d2a5..de4b068582 100644 --- a/test/triggerE.test +++ b/test/triggerE.test @@ -57,6 +57,9 @@ foreach {tn defn} { 7 { BEFORE DELETE ON t1 BEGIN SELECT * FROM t2 ORDER BY ?; END; } 8 { BEFORE UPDATE ON t1 BEGIN UPDATE t2 SET c = ?; END; } 9 { BEFORE UPDATE ON t1 BEGIN UPDATE t2 SET c = 1 WHERE d = ?; END; } + 10 { AFTER INSERT ON t1 BEGIN SELECT * FROM pragma_stats(?); END; } + 11 { BEFORE INSERT ON t1 BEGIN + INSERT INTO t1 SELECT max(b) OVER(ORDER BY $1) FROM t1; END } } { catchsql {drop trigger tr1} do_catchsql_test 1.1.$tn "CREATE TRIGGER tr1 $defn" [list 1 $errmsg] @@ -67,6 +70,7 @@ foreach {tn defn} { # Test that variable references within trigger definitions loaded from # the sqlite_master table are automatically converted to NULL. # +sqlite3_db_config db DEFENSIVE 0 do_execsql_test 2.1 { PRAGMA writable_schema = 1; INSERT INTO sqlite_master VALUES('trigger', 'tr1', 't1', 0, diff --git a/test/triggerF.test b/test/triggerF.test new file mode 100644 index 0000000000..2e3e35e3b2 --- /dev/null +++ b/test/triggerF.test @@ -0,0 +1,71 @@ +# 2017 January 4 +# +# The author disclaims copyright to this source code. In place of +# a legal notice', here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix triggerF +ifcapable {!trigger} { + finish_test + return +} + + +foreach {tn sql log} { + 1 {} {} + + 2 { + CREATE TRIGGER trd AFTER DELETE ON t1 BEGIN + INSERT INTO log VALUES(old.a || old.b || (SELECT count(*) FROM t1)); + END; + } {1one2 2two1 3three1} + + 3 { + CREATE TRIGGER trd BEFORE DELETE ON t1 BEGIN + INSERT INTO log VALUES(old.a || old.b || (SELECT count(*) FROM t1)); + END; + } {1one3 2two2 3three2} + + 4 { + CREATE TRIGGER tr1 AFTER DELETE ON t1 BEGIN + INSERT INTO log VALUES(old.a || old.b || (SELECT count(*) FROM t1)); + END; + CREATE TRIGGER tr2 BEFORE DELETE ON t1 BEGIN + INSERT INTO log VALUES(old.a || old.b || (SELECT count(*) FROM t1)); + END; + } {1one3 1one2 2two2 2two1 3three2 3three1} + +} { + reset_db + do_execsql_test 1.$tn.0 { + PRAGMA recursive_triggers = on; + CREATE TABLE t1(a INT PRIMARY KEY, b) WITHOUT ROWID; + CREATE TABLE log(t); + } + + execsql $sql + + do_execsql_test 1.$tn.1 { + INSERT INTO t1 VALUES(1, 'one'); + INSERT INTO t1 VALUES(2, 'two'); + INSERT INTO t1 VALUES(3, 'three'); + + DELETE FROM t1 WHERE a=1; + INSERT OR REPLACE INTO t1 VALUES(2, 'three'); + UPDATE OR REPLACE t1 SET a=3 WHERE a=2; + } + + do_execsql_test 1.$tn.2 { + SELECT * FROM log ORDER BY rowid; + } $log +} + +finish_test diff --git a/test/triggerG.test b/test/triggerG.test new file mode 100644 index 0000000000..b078fffb2e --- /dev/null +++ b/test/triggerG.test @@ -0,0 +1,94 @@ +# 2017-03-24 +# +# The author disclaims copyright to this source code. In place of +# a legal notice', here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix triggerG +ifcapable {!trigger} { + finish_test + return +} + +# Test cases for ticket +# https://sqlite.org/src/tktview/06796225f59c057cd120f +# +# The OP_Once opcode was not working correctly for recursive triggers. +# +do_execsql_test 100 { + PRAGMA recursive_triggers = 1; + + CREATE TABLE t1(a); + CREATE INDEX i1 ON t1(a); + INSERT INTO t1(a) VALUES(0),(2),(3),(8),(9); + CREATE TABLE t2(b); + CREATE TABLE t3(c); + + CREATE TRIGGER tr AFTER INSERT ON t3 BEGIN + INSERT INTO t3 SELECT new.c+1 WHERE new.c<5; + INSERT INTO t2 SELECT new.c*100+a FROM t1 WHERE a IN (1, 2, 3, 4); + END; + + INSERT INTO t3 VALUES(2); + SELECT c FROM t3 ORDER BY c;; +} {2 3 4 5} +do_execsql_test 110 { + SELECT b FROM t2 ORDER BY b; +} {202 203 302 303 402 403 502 503} + +do_execsql_test 200 { + DELETE FROM t1; + INSERT INTO t1(a) VALUES(0),(2),(3),(8),(9); + DELETE FROM t2; + DELETE FROM t3; + DROP TRIGGER tr; + CREATE TRIGGER tr AFTER INSERT ON t3 BEGIN + INSERT INTO t3 SELECT new.c+1 WHERE new.c<5; + INSERT INTO t2 SELECT new.c*10000+xx.a*100+yy.a + FROM t1 AS xx, t1 AS yy + WHERE xx.a IN (1,2,3,4) + AND yy.a IN (2,3,4,5); + END; + + INSERT INTO t3 VALUES(2); + SELECT b FROM t2 ORDER BY b; +} {20202 20203 20302 20303 30202 30203 30302 30303 40202 40203 40302 40303 50202 50203 50302 50303} + +# At one point the following was causing an assert() to fail. +# +do_execsql_test 300 { + CREATE TABLE t4(x); + CREATE TRIGGER tr4 AFTER INSERT ON t4 BEGIN + SELECT 0x2147483648e0e0099 AS y WHERE y; + END; +} + +do_catchsql_test 310 { + INSERT INTO t4 VALUES(1); +} {1 {hex literal too big: 0x2147483648e0e0099}} + +#------------------------------------------------------------------------- +# +do_execsql_test 400 { + CREATE VIEW v0(a) AS SELECT 1234; + CREATE TRIGGER t0001 INSTEAD OF DELETE ON v0 BEGIN + SELECT old.a; + END; +} +do_execsql_test 405 { + SELECT a FROM v0; +} {1234} +do_execsql_test 410 { + DELETE FROM v0; +} + + +finish_test diff --git a/test/triggerupfrom.test b/test/triggerupfrom.test new file mode 100644 index 0000000000..a731045c4e --- /dev/null +++ b/test/triggerupfrom.test @@ -0,0 +1,173 @@ +# 2020 July 14 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix triggerupfrom + +do_execsql_test 1.0 { + CREATE TABLE map(k, v); + INSERT INTO map VALUES(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four'); + + CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c); + + CREATE TRIGGER tr AFTER INSERT ON t1 BEGIN + UPDATE t1 SET c = v FROM map WHERE k=new.a AND a=new.a; + END; +} + +do_execsql_test 1.1 { + INSERT INTO t1(a) VALUES(1); +} + +do_execsql_test 1.2 { + SELECT a, c FROM t1 ORDER BY a; +} {1 one} + +do_execsql_test 1.3 { + INSERT INTO t1(a) VALUES(2), (3), (4), (5); + SELECT a, c FROM t1 ORDER BY a; +} {1 one 2 two 3 three 4 four 5 {}} + +forcedelete test.db2 +do_execsql_test 2.0 { + ATTACH 'test.db2' AS aux; + CREATE TABLE aux.t3(x, y); + INSERT INTO aux.t3 VALUES('x', 'y'); +} + +do_catchsql_test 2.1 { + CREATE TRIGGER tr2 AFTER INSERT ON t1 BEGIN + UPDATE t1 SET b = y FROM aux.t3 WHERE k=new.a; + END; +} {1 {trigger tr2 cannot reference objects in database aux}} + +do_execsql_test 2.2 { + CREATE TEMP TRIGGER tr2 AFTER INSERT ON t1 BEGIN + UPDATE t1 SET b = y FROM aux.t3 WHERE a=new.a; + END; + INSERT INTO t1(a) VALUES(10), (20); + SELECT * FROM t1; +} { + 1 {} one + 2 {} two + 3 {} three + 4 {} four + 5 {} {} + 10 y {} + 20 y {} +} + +do_execsql_test 2.3 { + CREATE TABLE link(f, t); + INSERT INTO link VALUES(5, 2), (20, 10), (2, 1); + CREATE TRIGGER tr3 BEFORE DELETE ON t1 BEGIN + UPDATE t1 SET b=coalesce(old.b,old.c) FROM main.link WHERE a=t AND old.a=f; + END; + DELETE FROM t1 WHERE a=2; + SELECT * FROM t1; +} { + 1 two one + 3 {} three + 4 {} four + 5 {} {} + 10 y {} + 20 y {} +} + +db close +sqlite3 db "" +do_catchsql_test 2.4 { + ATTACH 'test.db' AS yyy; + SELECT * FROM t1; +} {1 {malformed database schema (tr3) - trigger tr3 cannot reference objects in database main}} + +#------------------------------------------------------------------------- +reset_db +forcedelete test.db2 +do_execsql_test 3.0 { + CREATE TABLE mmm(x, y); + INSERT INTO mmm VALUES(1, 'one'); + INSERT INTO mmm VALUES(2, 'two'); + INSERT INTO mmm VALUES(3, 'three'); + + ATTACH 'test.db2' AS aux; + CREATE TABLE aux.t1(a, b); + CREATE TABLE aux.mmm(x, y); + INSERT INTO aux.mmm VALUES(1, 'ONE'); + INSERT INTO aux.mmm VALUES(2, 'TWO'); + INSERT INTO aux.mmm VALUES(3, 'THREE'); + + CREATE TRIGGER aux.ttt AFTER INSERT ON t1 BEGIN + UPDATE t1 SET b=y FROM mmm WHERE x=new.a AND a=new.a; + END; + + INSERT INTO t1(a) VALUES (2); + SELECT * FROM t1; +} {2 TWO} + +#------------------------------------------------------------------------- +# Test that INSTEAD OF UPDATE triggers on views work with UPDATE...FROM +# statements. Including, if the library is built with ENABLE_HIDDEN_COLUMNS, +# that they work correctly on views with hidden columns. +# +reset_db +do_execsql_test 4.0 { + CREATE TABLE t1(k, a, b); + INSERT INTO t1 VALUES('a', 1, 'one'); + INSERT INTO t1 VALUES('b', 2, 'two'); + INSERT INTO t1 VALUES('c', 3, 'three'); + INSERT INTO t1 VALUES('d', 4, 'four'); + + CREATE TABLE log(x); + CREATE VIEW v1 AS SELECT k, a, b AS __hidden__b FROM t1; + CREATE TRIGGER tr1 INSTEAD OF UPDATE ON v1 BEGIN + INSERT INTO log VALUES( + '('||old.a||','||old.__hidden__b||')->('||new.a||','||new.__hidden__b||')' + ); + END; +} + +ifcapable hiddencolumns { + do_execsql_test 4.1-hc-enabled { + SELECT * FROM v1 + } {a 1 b 2 c 3 d 4} +} else { + do_execsql_test 4.1-hc-disabled { + SELECT * FROM v1 + } {a 1 one b 2 two c 3 three d 4 four} +} + +do_execsql_test 4.2 { + UPDATE v1 SET a='xyz' WHERE k IN ('a', 'c'); + SELECT * FROM log; + DELETE FROM log; +} { + (1,one)->(xyz,one) + (3,three)->(xyz,three) +} + +do_execsql_test 4.3 { + CREATE TABLE map(k, v); + INSERT INTO map VALUES('b', 'twelve'); + INSERT INTO map VALUES('d', 'fourteen'); + UPDATE v1 SET a=map.v FROM map WHERE v1.k=map.k; + SELECT * FROM log; + DELETE FROM log; +} { + (2,two)->(twelve,two) + (4,four)->(fourteen,four) +} + + + +finish_test diff --git a/test/trustschema1.test b/test/trustschema1.test new file mode 100644 index 0000000000..8edaf80515 --- /dev/null +++ b/test/trustschema1.test @@ -0,0 +1,262 @@ +# 2020-01-08 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# Test cases for managing execution of code snippets found in untrusted +# schemas. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix trustschema1 + +# edgy functions used in generated columns +# +proc f1 {x} {return $x} +do_test 1.100 { + db function f1 -innocuous -deterministic f1 + db function f2 -deterministic f1 + db function f3 -directonly -deterministic f1 + db eval { + CREATE TABLE t1(a, b AS (f1(a+1)), c AS (f2(a+2))); + INSERT INTO t1 VALUES(100),(200); + } +} {} +do_catchsql_test 1.110 { + SELECT a, b, c FROM t1; +} {0 {100 101 102 200 201 202}} +do_execsql_test 1.120 { + PRAGMA trusted_schema=OFF; +} {} +do_catchsql_test 1.130 { + SELECT a, b FROM t1; +} {0 {100 101 200 201}} +do_catchsql_test 1.140 { + SELECT a, b, c FROM t1; +} {1 {unsafe use of f2()}} +do_catchsql_test 1.150 { + PRAGMA trusted_schema=ON; + DROP TABLE t1; + CREATE TABLE t1(a, b AS (f3(a+1))); +} {1 {unsafe use of f3()}} +do_execsql_test 1.160 { + PRAGMA trusted_schema=OFF; + CREATE TEMP TABLE temp1(a,b AS (f3(a+1))); + INSERT INTO temp1(a) VALUES(100),(900); + SELECT * FROM temp1; +} {100 101 900 901} + +# edgy functions used in CHECK constraints +# +do_catchsql_test 1.200 { + PRAGMA trusted_schema=ON; + CREATE TABLE t2(a,b,c,CHECK(f3(c)==c)); +} {1 {unsafe use of f3()}} +do_catchsql_test 1.210 { + PRAGMA trusted_schema=Off; + CREATE TABLE t2(a,b,c,CHECK(f2(c)==c)); +} {1 {unsafe use of f2()}} +do_catchsql_test 1.211 { + PRAGMA trusted_schema=On; + CREATE TABLE t2(a,b,c,CHECK(f2(c)==c)); +} {0 {}} +do_catchsql_test 1.220 { + INSERT INTO t2 VALUES(1,2,3); + SELECT * FROM t2; +} {0 {1 2 3}} +do_catchsql_test 1.230 { + PRAGMA trusted_schema=off; + INSERT INTO t2 VALUES(4,5,6); +} {1 {unsafe use of f2()}} +do_execsql_test 1.231 { + SELECT * FROM t2; +} {1 2 3} +# Ok to put as many edgy functions as you want in a +# TEMP table. +do_execsql_test 1.240 { + PRAGMA trusted_schema=OFF; + CREATE TEMP TABLE temp2(a, b, CHECK(f3(b)==b)); + INSERT INTO temp2(a,b) VALUES(1,2),('x','y'); + SELECT * FROM temp2; +} {1 2 x y} + +# edgy functions used in DEFAULT constraints +# +do_catchsql_test 1.300 { + CREATE TABLE t3(a,b DEFAULT(f2(25))); +} {0 {}} +do_catchsql_test 1.310 { + PRAGMA trusted_schema=Off; + INSERT INTO t3(a) VALUES(1); +} {1 {unsafe use of f2()}} +do_catchsql_test 1.311 { + INSERT INTO t3(a,b) VALUES(1,2); +} {0 {}} +do_execsql_test 1.320 { + CREATE TEMP TABLE temp3(a, b DEFAULT(f3(31))); + INSERT INTO temp3(a) VALUES(22); + SELECT * FROM temp3; +} {22 31} + +# edgy functions used in partial indexes. +# +do_execsql_test 1.400 { + CREATE TABLE t4(a,b,c); + INSERT INTO t4 VALUES(1,2,3),('a','b','c'),(4,'d',0); + SELECT * FROM t4; + CREATE TEMP TABLE temp4(a,b,c); + INSERT INTO temp4 SELECT * FROM t4; +} {1 2 3 a b c 4 d 0} +do_catchsql_test 1.410 { + CREATE INDEX t4a ON t4(a) WHERE f3(c); +} {1 {unsafe use of f3()}} +do_catchsql_test 1.420 { + PRAGMA trusted_schema=OFF; + CREATE INDEX t4a ON t4(a) WHERE f2(c); +} {1 {unsafe use of f2()}} +do_execsql_test 1.421 { + CREATE INDEX t4a ON t4(a) WHERE f1(c); + SELECT a FROM t4 WHERE f1(c) ORDER BY a; +} {1} +do_execsql_test 1.430 { + PRAGMA trusted_schema=ON; + CREATE INDEX t4b ON t4(b) WHERE f2(c); + SELECT b FROM t4 WHERE f2(c) ORDER BY b; +} {2} +do_execsql_test 1.440 { + PRAGMA trusted_schema=OFF; + CREATE INDEX temp4a ON temp4(a) WHERE f3(c); + SELECT a FROM temp4 WHERE f2(c) ORDER BY a; +} {1} + +# edgy functions used in index expressions +# +do_execsql_test 1.500 { + CREATE TABLE t5(a,b,c); + INSERT INTO t5 VALUES(1,2,3),(4,5,6),(7,0,-3); + SELECT * FROM t5; + CREATE TEMP TABLE temp5(a,b,c); + INSERT INTO temp5 SELECT * FROM t5; +} {1 2 3 4 5 6 7 0 -3} +do_catchsql_test 1.510 { + CREATE INDEX t5x1 ON t5(a+f3(b)); +} {1 {unsafe use of f3()}} +do_catchsql_test 1.520 { + PRAGMA trusted_schema=OFF; + CREATE INDEX t5x1 ON t5(a+f2(b)); +} {1 {unsafe use of f2()}} +do_execsql_test 1.521 { + CREATE INDEX t5x1 ON t5(a+f1(b)); + SELECT * FROM t5 INDEXED BY t5x1 WHERE a+f1(b)=3; +} {1 2 3} +do_execsql_test 1.530 { + PRAGMA trusted_schema=ON; + CREATE INDEX t5x2 ON t5(b+f2(c)); + SELECT * FROM t5 INDEXED BY t5x2 WHERE b+f2(c)=11; +} {4 5 6} +do_execsql_test 1.540 { + PRAGMA trusted_schema=OFF; + CREATE INDEX temp5x1 ON temp5(a+f3(b)); + SELECT * FROM temp5 INDEXED BY temp5x1 WHERE a+f3(b)=7; +} {7 0 -3} + +# edgy functions in VIEWs +# +reset_db +db function f1 -innocuous -deterministic f1 +db function f2 -deterministic f1 +db function f3 -directonly -deterministic f1 +do_execsql_test 2.100 { + CREATE TABLE t1(a,b,c); + INSERT INTO t1 VALUES(1,2,3),(100,50,75),(-11,22,-33); + CREATE VIEW v1a AS SELECT f3(a+b) FROM t1; + SELECT f3(a+b) FROM t1; +} {3 150 11} +do_catchsql_test 2.110 { + PRAGMA trusted_schema=ON; + SELECT * FROM v1a; +} {1 {unsafe use of f3()}} +do_catchsql_test 2.111 { + PRAGMA trusted_schema=OFF; + SELECT * FROM v1a; +} {1 {unsafe use of f3()}} +do_execsql_test 2.120 { + DROP VIEW v1a; + CREATE TEMP VIEW v1a AS SELECT f3(a+b) FROM t1; + SELECT * FROM v1a; +} {3 150 11} +do_execsql_test 2.130 { + CREATE VIEW v1b AS SELECT f2(b+c) FROM t1; + SELECT f2(b+c) FROM t1; +} {5 125 -11} +do_catchsql_test 2.140 { + PRAGMA trusted_schema=ON; + SELECT * FROM v1b; +} {0 {5 125 -11}} +do_catchsql_test 2.141 { + PRAGMA trusted_schema=OFF; + SELECT * FROM v1b; +} {1 {unsafe use of f2()}} +do_execsql_test 2.150 { + DROP VIEW v1b; + CREATE TEMP VIEW v1b AS SELECT f2(b+c) FROM t1; + SELECT * FROM v1b; +} {5 125 -11} + +# edgy functions inside of triggers +# +do_execsql_test 3.100 { + DELETE FROM t1; + CREATE TABLE t2(x); + CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN + INSERT INTO t2(x) SELECT f3(new.a); + END; +} {} +do_catchsql_test 3.110 { + INSERT INTO t1 VALUES(7,6,5); +} {1 {unsafe use of f3()}} +do_execsql_test 3.111 { + SELECT * FROM t1; + SELECT * FROM t2; +} {} + +do_execsql_test 3.120 { + DROP TRIGGER r1; + CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN + INSERT INTO t2(x) SELECT f2(new.a)+100; + END; + PRAGMA trusted_schema=ON; + INSERT INTO t1 VALUES(7,6,5); + SELECT * FROM t1, t2; +} {7 6 5 107} +do_catchsql_test 3.130 { + DELETE FROM t1; + DELETE FROM t2; + PRAGMA trusted_schema=OFF; + INSERT INTO t1 VALUES(7,6,5); +} {1 {unsafe use of f2()}} +do_execsql_test 3.131 { + SELECT * FROM t1; + SELECT * FROM t2; +} {} + +# 2023-01-09 https://sqlite.org/forum/forumpost/c88a671ad083d153 +# +do_execsql_test 4.1 { + PRAGMA trusted_schema=OFF; + CREATE VIEW test41(x) AS SELECT json_extract('{"a":123}','$.a'); + SELECT * FROM test41; +} 123 +do_execsql_test 4.2 { + PRAGMA trusted_schema=ON; + SELECT * FROM test41; +} 123 + +finish_test diff --git a/test/tt3_checkpoint.c b/test/tt3_checkpoint.c index 060a698211..ec9d0727e6 100644 --- a/test/tt3_checkpoint.c +++ b/test/tt3_checkpoint.c @@ -75,7 +75,7 @@ static char *checkpoint_starvation_reader(int iTid, void *pArg){ i64 iCount1, iCount2; sql_script(&err, &db, "BEGIN"); iCount1 = execsql_i64(&err, &db, "SELECT count(x) FROM t1"); - usleep(CHECKPOINT_STARVATION_READMS*1000); + sqlite3_sleep(CHECKPOINT_STARVATION_READMS); iCount2 = execsql_i64(&err, &db, "SELECT count(x) FROM t1"); sql_script(&err, &db, "COMMIT"); @@ -107,7 +107,7 @@ static void checkpoint_starvation_main(int nMs, CheckpointStarvationCtx *p){ for(i=0; i<4; i++){ launch_thread(&err, &threads, checkpoint_starvation_reader, 0); - usleep(CHECKPOINT_STARVATION_READMS*1000/4); + sqlite3_sleep(CHECKPOINT_STARVATION_READMS/4); } sqlite3_wal_hook(db.db, checkpoint_starvation_walhook, (void *)p); diff --git a/test/tt3_shared.c b/test/tt3_shared.c new file mode 100644 index 0000000000..5bdadd1e03 --- /dev/null +++ b/test/tt3_shared.c @@ -0,0 +1,55 @@ +/* +** 2020 September 5 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** +*/ + + +/* +*/ +static char *shared_thread1(int iTid, void *pArg){ + Error err = {0}; /* Error code and message */ + + while( !timetostop(&err) ){ + Sqlite db = {0}; /* SQLite database connection */ + opendb(&err, &db, "test.db", 0); + sql_script(&err, &db, "SELECT * FROM t1"); + closedb(&err, &db); + } + print_and_free_err(&err); + return sqlite3_mprintf("done!"); +} + + +static void shared1(int nMs){ + Error err = {0}; + Sqlite db = {0}; /* SQLite database connection */ + Threadset threads = {0}; + int ii; + + opendb(&err, &db, "test.db", 1); + sql_script(&err, &db, "CREATE TABLE t1(x)"); + closedb(&err, &db); + + setstoptime(&err, nMs); + sqlite3_enable_shared_cache(1); + + for(ii=0; ii<5; ii++){ + launch_thread(&err, &threads, shared_thread1, 0); + } + + join_all_threads(&err, &threads); + sqlite3_enable_shared_cache(0); + + print_and_free_err(&err); +} + diff --git a/test/tt3_stress.c b/test/tt3_stress.c index cdfab9c09c..be917b7320 100644 --- a/test/tt3_stress.c +++ b/test/tt3_stress.c @@ -41,7 +41,7 @@ static char *stress_thread_2(int iTid, void *pArg){ Sqlite db = {0}; /* SQLite database connection */ while( !timetostop(&err) ){ opendb(&err, &db, "test.db", 0); - sql_script(&err, &db, "SELECT * FROM sqlite_master;"); + sql_script(&err, &db, "SELECT * FROM sqlite_schema;"); clear_error(&err, SQLITE_LOCKED); closedb(&err, &db); } @@ -266,7 +266,7 @@ static char *stress2_workload19(int iTid, void *pArg){ const char *zDb = (const char*)pArg; while( !timetostop(&err) ){ opendb(&err, &db, zDb, 0); - sql_script(&err, &db, "SELECT * FROM sqlite_master;"); + sql_script(&err, &db, "SELECT * FROM sqlite_schema;"); clear_error(&err, SQLITE_LOCKED); closedb(&err, &db); } @@ -362,7 +362,3 @@ static void stress2(int nMs){ sqlite3_enable_shared_cache(0); print_and_free_err(&err); } - - - - diff --git a/test/tt3_vacuum.c b/test/tt3_vacuum.c index 023fdfd74d..c41a6b487d 100644 --- a/test/tt3_vacuum.c +++ b/test/tt3_vacuum.c @@ -23,9 +23,9 @@ static char *vacuum1_thread_writer(int iTid, void *pArg){ Error err = {0}; /* Error code and message */ Sqlite db = {0}; /* SQLite database connection */ - opendb(&err, &db, "test.db", 0); i64 i = 0; + opendb(&err, &db, "test.db", 0); while( !timetostop(&err) ){ i++; diff --git a/test/types2.test b/test/types2.test index 4a70aa5fe2..d6d84e65b5 100644 --- a/test/types2.test +++ b/test/types2.test @@ -333,7 +333,7 @@ ifcapable subquery { test_boolset types2-8.8 {o IN (SELECT t FROM t4)} {7} test_boolset types2-8.9 {i IN (SELECT o FROM t4)} {9 10 11 12} test_boolset types2-8.6 {n IN (SELECT o FROM t4)} {9 10 11 12} - test_boolset types2-8.7 {t IN (SELECT o FROM t4)} {9 11} + test_boolset types2-8.7 {t IN (SELECT o FROM t4)} {} test_boolset types2-8.8 {o IN (SELECT o FROM t4)} {9 10} } diff --git a/test/types3.test b/test/types3.test index 807ae84f9d..457ee6d68a 100644 --- a/test/types3.test +++ b/test/types3.test @@ -12,18 +12,15 @@ # of this file is testing the interaction of SQLite manifest types # with Tcl dual-representations. # -# $Id: types3.test,v 1.8 2008/04/28 13:02:58 drh Exp $ -# set testdir [file dirname $argv0] source $testdir/tester.tcl # A variable with only a string representation comes in as TEXT do_test types3-1.1 { - set V {} - append V x + set V [format %s xxxxx] concat [tcl_variable_type V] [execsql {SELECT typeof(:V)}] -} {string text} +} {text} # A variable with an integer representation comes in as INTEGER do_test types3-1.2 { @@ -96,4 +93,32 @@ do_test types3-2.6 { tcl_variable_type V } {} +# See https://sqlite.org/forum/forumpost/3776b48e71 +# +# On a text-affinity comparison of two values where one of +# the values has both MEM_Str and a numeric type like MEM_Int, +# make sure that only the MEM_Str representation is used. +# +sqlite3_create_function db +do_execsql_test types3-3.1 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(x TEXT PRIMARY KEY); + INSERT INTO t1 VALUES('1'); + SELECT * FROM t1 WHERE NOT x=upper(1); +} {} +do_execsql_test types3-3.2 { + SELECT * FROM t1 WHERE NOT x=add_text_type(1); +} {} +do_execsql_test types3-3.3 { + SELECT * FROM t1 WHERE NOT x=add_int_type('1'); +} {} +do_execsql_test types3-3.4 { + DELETE FROM t1; + INSERT INTO t1 VALUES(1.25); + SELECT * FROM t1 WHERE NOT x=add_real_type('1.25'); +} {} +do_execsql_test types3-3.5 { + SELECT * FROM t1 WHERE NOT x=add_text_type(1.25); +} {} + finish_test diff --git a/test/unhex.test b/test/unhex.test new file mode 100644 index 0000000000..af2cfae390 --- /dev/null +++ b/test/unhex.test @@ -0,0 +1,102 @@ +# 2023 January 23 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +set testdir [file dirname $argv0] +source [file join $testdir tester.tcl] + +set testprefix unhex + + +foreach {tn hex} { + 1 0000 + 2 FFFF + 3 0123456789ABCDEF +} { + do_execsql_test 1.$tn.1 { + SELECT hex( unhex( $hex ) ); + } $hex + + do_execsql_test 1.$tn.2 { + SELECT hex( unhex( lower( $hex ) ) ); + } $hex +} + +do_execsql_test 2.0 { + SELECT typeof( unhex('') ), length( unhex('') ); +} {blob 0} + +foreach {tn hex} { + 1 ABC + 2 hello + 3 123456x7 + 4 0xff +} { + do_execsql_test 2.$tn { + SELECT unhex( $hex ) IS NULL; + } 1 +} + +do_catchsql_test 3.0 { + SELECT unhex(); +} {1 {wrong number of arguments to function unhex()}} +do_catchsql_test 3.1 { + SELECT unhex('ABCD', '1234', ''); +} {1 {wrong number of arguments to function unhex()}} + +#-------------------------------------------------------------------------- +# Test the 2-argument version. +# +# Zap global x array set in some previous test. +if {[array exists x]} {array unset x} +foreach {tn hex} { + 1 "FFFF ABCD" + 2 "FFFF ABCD" + 3 "FFFFABCD " + 4 " FFFFABCD" + 5 "--FFFF AB- -CD- " + 6 "--" + 7 " --" +} { + set out "" + foreach x [split $hex ""] { + if {[string is xdigit $x]} { append out $x } + } + + do_execsql_test 5.$tn.1 { + SELECT hex( unhex($hex, ' -') ); + } [list $out] +} + +do_execsql_test 6.0 { + SELECT typeof( unhex(' ', ' -') ), length( unhex('-', ' -') ); +} {blob 0} + + +do_execsql_test 6.1 " + SELECT hex( unhex('\u0E01ABCD\u0E02', '\uE01\uE02') ) +" {ABCD} +do_execsql_test 6.2 " + SELECT typeof( unhex('\u0E01ABCD\u0E02', '\uE03\uE02') ) +" {null} +do_execsql_test 6.3 " + SELECT hex( unhex('\u0E01AB CD\uE02\uE01', '\uE01 \uE02') ) +" {ABCD} + +#-------------------------------------------------------------------------- +# Test that if either argument is NULL, the returned value is also NULL. +# +do_execsql_test 6.4.1 { SELECT typeof(unhex(NULL)) } {null} +do_execsql_test 6.4.2 { SELECT typeof(unhex(NULL, ' ')) } {null} +do_execsql_test 6.4.3 { SELECT typeof(unhex('1234', NULL)) } {null} + + +finish_test diff --git a/test/unionall.test b/test/unionall.test new file mode 100644 index 0000000000..9057199070 --- /dev/null +++ b/test/unionall.test @@ -0,0 +1,453 @@ +# 2020-12-16 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is flattening UNION ALL sub-queries. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix unionall + +do_execsql_test 1.0 { + CREATE TABLE t1_a(a INTEGER PRIMARY KEY, b TEXT); + CREATE TABLE t1_b(c INTEGER PRIMARY KEY, d TEXT); + CREATE TABLE t1_c(e INTEGER PRIMARY KEY, f TEXT); + + INSERT INTO t1_a VALUES(1, 'one'), (4, 'four'); + INSERT INTO t1_b VALUES(2, 'two'), (5, 'five'); + INSERT INTO t1_c VALUES(3, 'three'), (6, 'six'); + + CREATE VIEW t1 AS + SELECT a, b FROM t1_a UNION ALL + SELECT c, d FROM t1_b UNION ALL + SELECT e, f FROM t1_c; + + CREATE TABLE i1(x); + INSERT INTO i1 VALUES(2), (5), (6), (1); +} + +do_execsql_test 1.1 { + SELECT a, b FROM ( + SELECT a, b FROM t1_a UNION ALL + SELECT c, d FROM t1_b UNION ALL + SELECT e, f FROM t1_c + ) ORDER BY a +} { + 1 one 2 two 3 three 4 four 5 five 6 six +} + +do_execsql_test 1.2 { + SELECT a, b FROM t1 ORDER BY a +} { + 1 one 2 two 3 three 4 four 5 five 6 six +} + +do_execsql_test 1.3 { + SELECT a, b FROM i1, t1 WHERE a=x ORDER BY a +} {1 one 2 two 5 five 6 six} + + +# 2022-10-31 part of ticket 57c47526c34f01e8 +# The queries below were causing an assertion fault in +# the comparison operators of the VDBE. +# +reset_db +database_never_corrupt +optimization_control db all 0 +do_execsql_test 1.10 { + CREATE TABLE t0(c0 INT); + INSERT INTO t0 VALUES(0); + CREATE TABLE t1_a(a INTEGER PRIMARY KEY, b TEXT); + INSERT INTO t1_a VALUES(1,'one'); + CREATE TABLE t1_b(c INTEGER PRIMARY KEY, d TEXT); + INSERT INTO t1_b VALUES(2,'two'); + CREATE VIEW t1 AS SELECT a, b FROM t1_a UNION ALL SELECT c, c FROM t1_b; + SELECT * FROM (SELECT t1.a, t1.b AS b, t0.c0 FROM t0, t1); +} {1 one 0 2 2 0} +do_execsql_test 1.11 { + SELECT * FROM (SELECT t1.a, t1.b AS b, t0.c0 FROM t0, t1) WHERE b=2; +} {2 2 0} + +#------------------------------------------------------------------------- +reset_db + +do_execsql_test 2.1.0 { + CREATE TABLE t1(x, y); + INSERT INTO t1 VALUES(1, 'one'); + INSERT INTO t1 VALUES(1, 'ONE'); + INSERT INTO t1 VALUES(2, 'two'); + INSERT INTO t1 VALUES(2, 'TWO'); + INSERT INTO t1 VALUES(3, 'three'); + INSERT INTO t1 VALUES(3, 'THREE'); +} + +do_execsql_test 2.1.1 { + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<3 + ) + SELECT * FROM ( + SELECT 0 AS i UNION ALL SELECT i FROM s UNION ALL SELECT 0 + ), t1 WHERE x=i; +} { + 1 1 one 1 1 ONE 2 2 two 2 2 TWO 3 3 three 3 3 THREE +} + +do_catchsql_test 2.1.2 { + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<3 UNION ALL SELECT 4 + ) + SELECT * FROM s, t1 WHERE x=i; +} {1 {circular reference: s}} + +do_execsql_test 2.2.0 { + CREATE TABLE t2_a(k INTEGER PRIMARY KEY, v TEXT); + CREATE TABLE t2_b(k INTEGER PRIMARY KEY, v TEXT); + + CREATE VIEW t2 AS + SELECT * FROM t2_a + UNION ALL + SELECT * FROM t2_b; + + CREATE TRIGGER t2_insert INSTEAD OF INSERT ON t2 BEGIN + INSERT INTO t2_a SELECT new.k, new.v WHERE (new.k%2)==0; + INSERT INTO t2_b SELECT new.k, new.v WHERE (new.k%2)==1; + END; + + INSERT INTO t2 VALUES(5, 'v'), (4, 'iv'), (3, 'iii'), (2, 'ii'); +} + +do_execsql_test 2.2.1 { + SELECT * FROM t1, t2 WHERE x=k; +} { + 2 two 2 ii 2 TWO 2 ii 3 three 3 iii 3 THREE 3 iii +} + +do_execsql_test 2.2.2 { + SELECT * FROM t1 LEFT JOIN t2 ON (x=k); +} { + 1 one {} {} + 1 ONE {} {} + 2 two 2 ii 2 TWO 2 ii 3 three 3 iii 3 THREE 3 iii +} + +do_execsql_test 2.2.3 { + SELECT x1.*, x2.* FROM t2 AS x1, t2 AS x2 WHERE x1.k=x2.k+1 +} { + 4 iv 3 iii + 3 iii 2 ii + 5 v 4 iv +} + +do_execsql_test 2.2.4 { + SELECT * FROM t1, t2 WHERE x=k ORDER BY y; +} { + 3 THREE 3 iii + 2 TWO 2 ii + 3 three 3 iii + 2 two 2 ii +} +do_execsql_test 2.2.5 { + SELECT * FROM t1, t2 WHERE x=k ORDER BY y||''; +} { + 3 THREE 3 iii + 2 TWO 2 ii + 3 three 3 iii + 2 two 2 ii +} +do_execsql_test 2.2.6 { + SELECT * FROM t1, t2 WHERE x=k ORDER BY v +} { + 2 two 2 ii + 2 TWO 2 ii + 3 three 3 iii + 3 THREE 3 iii +} +do_execsql_test 2.2.7 { + SELECT * FROM t1, t2 WHERE x=k ORDER BY v||'' +} { + 2 two 2 ii + 2 TWO 2 ii + 3 three 3 iii + 3 THREE 3 iii +} +do_execsql_test 2.2.8 { + SELECT * FROM t1, t2 WHERE x=k ORDER BY k,v||'' +} { + 2 two 2 ii + 2 TWO 2 ii + 3 three 3 iii + 3 THREE 3 iii +} +do_execsql_test 2.2.9a { + SELECT * FROM t1, t2 ORDER BY +k +} { + 1 one 2 ii 1 ONE 2 ii 2 two 2 ii + 2 TWO 2 ii 3 three 2 ii 3 THREE 2 ii + + 1 one 3 iii 1 ONE 3 iii 2 two 3 iii + 2 TWO 3 iii 3 three 3 iii 3 THREE 3 iii + + 1 one 4 iv 1 ONE 4 iv 2 two 4 iv + 2 TWO 4 iv 3 three 4 iv 3 THREE 4 iv + + 1 one 5 v 1 ONE 5 v 2 two 5 v + 2 TWO 5 v 3 three 5 v 3 THREE 5 v +} + +do_execsql_test 2.2.9b { + SELECT * FROM t1, t2 ORDER BY k +} { + 1 one 2 ii 1 ONE 2 ii 2 two 2 ii + 2 TWO 2 ii 3 three 2 ii 3 THREE 2 ii + + 1 one 3 iii 1 ONE 3 iii 2 two 3 iii + 2 TWO 3 iii 3 three 3 iii 3 THREE 3 iii + + 1 one 4 iv 1 ONE 4 iv 2 two 4 iv + 2 TWO 4 iv 3 three 4 iv 3 THREE 4 iv + + 1 one 5 v 1 ONE 5 v 2 two 5 v + 2 TWO 5 v 3 three 5 v 3 THREE 5 v +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 3.0 { + CREATE TABLE t1(c INTEGER PRIMARY KEY, d TEXT); + INSERT INTO t1 VALUES(1,2); + CREATE TABLE t3_a(k INTEGER PRIMARY KEY, v TEXT); + INSERT INTO t3_a VALUES(2,'ii'); + CREATE TABLE t3_b(k INTEGER PRIMARY KEY, v TEXT); + CREATE VIEW t3 AS + SELECT * FROM t3_a + UNION ALL + SELECT * FROM t3_b; +} {} + +do_execsql_test 3.1 { + SELECT * FROM t1, t3 ORDER BY k; +} {1 2 2 ii} + +reset_db +do_execsql_test 4.0 { + + CREATE TABLE t1_a(a INTEGER PRIMARY KEY, b TEXT); + INSERT INTO t1_a VALUES(123, 't1_a'); + CREATE TABLE t1_b(c INTEGER PRIMARY KEY, d TEXT); + + CREATE VIEW t1 AS + SELECT a, b FROM t1_a + UNION ALL + SELECT c, d FROM t1_b; + + CREATE TABLE t3_a(k INTEGER PRIMARY KEY, v TEXT); + INSERT INTO t3_a VALUES(456, 't3_a'); + CREATE TABLE t3_b(k INTEGER PRIMARY KEY, v TEXT); + + CREATE VIEW t3 AS + SELECT * FROM t3_a + UNION ALL + SELECT * FROM t3_b; +} + +do_execsql_test 4.1 { + SELECT * FROM t1, t3 ORDER BY k; +} {123 t1_a 456 t3_a} + +do_execsql_test 4.2 { + SELECT * FROM (SELECT * FROM t1, t3) ORDER BY k; +} {123 t1_a 456 t3_a} + +do_execsql_test 4.3 { + SELECT * FROM (SELECT * FROM t1, t3), ( + SELECT max(a) OVER () FROM t1 + UNION ALL + SELECT min(a) OVER () FROM t1 + ) + ORDER BY k; +} { + 123 t1_a 456 t3_a 123 + 123 t1_a 456 t3_a 123 +} + +do_execsql_test 4.3 { + SELECT * FROM (SELECT * FROM t1, t3), ( + SELECT group_concat(a) OVER (ORDER BY a), + group_concat(a) OVER (ORDER BY a), + group_concat(a) OVER (ORDER BY a), + group_concat(a) OVER (ORDER BY a), + group_concat(a) OVER (ORDER BY a), + group_concat(a) OVER (ORDER BY a), + group_concat(a) OVER (ORDER BY a), + group_concat(a) OVER (ORDER BY a), + group_concat(a) OVER (ORDER BY a) + FROM t1 + ) + ORDER BY k; +} { + 123 t1_a 456 t3_a 123 123 123 123 123 123 123 123 123 +} + +do_execsql_test 4.3 { + SELECT * FROM (SELECT * FROM t1, t3) AS o, ( + SELECT * FROM t1 LEFT JOIN t3 ON a=k + ); +} { + 123 t1_a 456 t3_a 123 t1_a {} {} +} + +# 2020-12-30: dbsqlfuzz find +reset_db +do_execsql_test 5.1 { + CREATE TABLE t1_a(a INTEGER PRIMARY KEY, b TEXT); + INSERT INTO t1_a VALUES(1,'one'); + INSERT INTO t1_a VALUES(0,NULL); + CREATE TABLE t1_b(c INTEGER PRIMARY KEY, d TEXT); + INSERT INTO t1_b VALUES(2,'two'); + INSERT INTO t1_b VALUES(5,'five'); + CREATE TABLE t1_c(e INTEGER PRIMARY KEY, f TEXT); + INSERT INTO t1_c VALUES(3,'three'); + INSERT INTO t1_c VALUES(6,'six'); + CREATE TABLE t2(k,v); + INSERT INTO t2 VALUES(5,'v'); + INSERT INTO t2 VALUES(4,'iv'); + INSERT INTO t2 VALUES(3,'iii'); + INSERT INTO t2 VALUES(2,'ii'); + CREATE TABLE t3_a(k INTEGER PRIMARY KEY, v TEXT); + INSERT INTO t3_a VALUES(2,'ii'); + INSERT INTO t3_a VALUES(4,'iv'); + CREATE TABLE t3_b(k INTEG5R PRIMARY KEY, v TEXT); + INSERT INTO t3_b VALUES(NULL,'iii'); + INSERT INTO t3_b VALUES(NULL,'v'); + CREATE VIEW t1 AS + SELECT a, b FROM t1_a UNION ALL + SELECT c, d FROM t1_b UNION ALL + SELECT e, f FROM t1_c; + CREATE VIEW t3 AS + SELECT * FROM t3_a + UNION ALL + SELECT * FROM t3_b; + CREATE TRIGGER t3_insert INSTEAD OF INSERT ON t3 BEGIN + INSERT INTO t3_a SELECT new.k, new.v WHERE (new.k%2)==0; + INSERT INTO t3_b SELECT new.k, new.v WHERE (new.k%2)==1; + END; +} {} +do_execsql_test 5.10 { + SELECT *, '+' FROM t1 LEFT JOIN t2 ON (a NOT IN(SELECT v FROM t1, t3 WHERE a=k)=NOT EXISTS(SELECT 1 FROM t1 LEFT JOIN t3 ON (a=k))); +} {0 {} {} {} + 1 one {} {} + 2 two {} {} + 5 five {} {} + 3 three {} {} + 6 six {} {} +} +do_execsql_test 5.20 { + SELECT *, '+' FROM t1 LEFT JOIN t3 ON (a NOT IN(SELECT v FROM t1 LEFT JOIN t2 ON (a=k))=k); +} {0 {} {} {} + 1 one {} {} + 2 two {} {} + 5 five {} {} + 3 three {} {} + 6 six {} {} +} +ifcapable vtab { +do_catchsql_test 5.30 { + SELECT * FROM (t1 NATURAL JOIN pragma_table_xinfo('t1_a') NATURAL JOIN t3) t1 + NATURAL JOIN t2 NATURAL JOIN t3 + WHERE rowid ISNULL>0 AND 0%y; +} {1 {ambiguous column name: rowid}} +} + +reset_db +do_execsql_test 6.0 { + CREATE TABLE t1(a,b); + INSERT INTO t1 VALUES(1,2); + CREATE TABLE t2(a,b); + INSERT INTO t2 VALUES(3,4); + + CREATE TABLE t3(a,b); + INSERT INTO t3 VALUES(5,6); + CREATE TABLE t4(a,b); + INSERT INTO t4 VALUES(7,8); + + CREATE TABLE t5(a,b); + INSERT INTO t5 VALUES(9,10); +} + +do_execsql_test 6.1 { + WITH x(c) AS ( + SELECT 1000 FROM t1 UNION ALL SELECT 800 FROM t2 + ), + y(d) AS ( + SELECT 100 FROM t3 UNION ALL SELECT 400 FROM t4 + ) + SELECT * FROM t5, x, y; +} { + 9 10 1000 100 9 10 1000 400 + 9 10 800 100 9 10 800 400 +} + +# 2021-04-26 dbsqlfuzz 88ed5c66789fced139d148aed823cba7c0926dd7 +reset_db +do_execsql_test 7.1 { + WITH c1(x) AS (VALUES(0) UNION ALL SELECT 100+x FROM c1 WHERE x<100 UNION ALL SELECT 1+x FROM c1 WHERE x<1) + SELECT x, y, '|' + FROM c1 AS x1, (SELECT x+1 AS y FROM c1 WHERE x<1 UNION ALL SELECT 1+x FROM c1 WHERE 1<x) AS x2 + ORDER BY x, y; +} {0 1 | 0 101 | 0 102 | 1 1 | 1 101 | 1 102 | 100 1 | 100 101 | 100 102 | 101 1 | 101 101 | 101 102 |} + +# 2022-10-31 ticket https://sqlite.org/src/info/57c47526c34f01e8 +# dbsqlfuzz 37230460b46b3b6049f0d768eb801f3428189382 +# UNION ALL subqueries or views which have arms with different +# affinities should not be flattened. +# +reset_db +do_execsql_test 8.1 { + CREATE TABLE t0(c0 INT); + INSERT INTO t0 VALUES(0); + CREATE TABLE t1_a(a INTEGER PRIMARY KEY, b TEXT); + INSERT INTO t1_a VALUES(1,'one'); + INSERT INTO t1_a VALUES(4,'four'); + CREATE TABLE t1_b(c INTEGER PRIMARY KEY, d TEXT); + INSERT INTO t1_b VALUES(2,'two'); + INSERT INTO t1_b VALUES(5,'five'); + CREATE TABLE t1_c(e INTEGER PRIMARY KEY, f TEXT); + INSERT INTO t1_c VALUES(3,'three'); + INSERT INTO t1_c VALUES(6,'six'); + CREATE VIEW v0(c0) AS SELECT CAST(t0.c0 AS INTEGER) FROM t0; + CREATE VIEW t1 AS + SELECT a, b FROM t1_a UNION ALL + SELECT c, c FROM t1_b UNION ALL + SELECT e, f FROM t1_c; + SELECT t1.a, t1.b, t0.c0 AS c, v0.c0 AS d FROM t0 LEFT JOIN v0 ON v0.c0>'0',t1; +} {1 one 0 {} 4 four 0 {} 2 2 0 {} 5 5 0 {} 3 three 0 {} 6 six 0 {}} + +optimization_control db all 1 +do_execsql_test 8.2 { + SELECT * FROM (SELECT t1.a, t1.b, t0.c0 AS c, v0.c0 AS d FROM t0 LEFT JOIN v0 ON v0.c0>'0',t1) WHERE b=2; +} {2 2 0 {}} +do_execsql_test 8.3 { + SELECT * FROM (SELECT t1.a, t1.b, t0.c0 AS c, v0.c0 AS d FROM t0 LEFT JOIN v0 ON v0.c0>'0',t1) WHERE b=2.0; +} {2 2 0 {}} +do_execsql_test 8.4 { + SELECT * FROM (SELECT t1.a, t1.b, t0.c0 AS c, v0.c0 AS d FROM t0 LEFT JOIN v0 ON v0.c0>'0',t1) WHERE b='2'; +} {} +optimization_control db query-flattener,push-down 0 +do_execsql_test 8.5 { + SELECT * FROM (SELECT t1.a, t1.b, t0.c0 AS c, v0.c0 AS d FROM t0 LEFT JOIN v0 ON v0.c0>'0',t1) WHERE b=2; +} {2 2 0 {}} +do_execsql_test 8.6 { + SELECT * FROM (SELECT t1.a, t1.b, t0.c0 AS c, v0.c0 AS d FROM t0 LEFT JOIN v0 ON v0.c0>'0',t1) WHERE b=2.0; +} {2 2 0 {}} +do_execsql_test 8.7 { + SELECT * FROM (SELECT t1.a, t1.b, t0.c0 AS c, v0.c0 AS d FROM t0 LEFT JOIN v0 ON v0.c0>'0',t1) WHERE b='2'; +} {} +optimization_control db all 0 +do_execsql_test 8.8 { + SELECT * FROM (SELECT t1.a, t1.b, t0.c0 AS c, v0.c0 AS d FROM t0 LEFT JOIN v0 ON v0.c0>'0',t1) WHERE b=2; +} {2 2 0 {}} +do_execsql_test 8.9 { + SELECT * FROM (SELECT t1.a, t1.b, t0.c0 AS c, v0.c0 AS d FROM t0 LEFT JOIN v0 ON v0.c0>'0',t1) WHERE b=2.0; +} {2 2 0 {}} +do_execsql_test 8.10 { + SELECT * FROM (SELECT t1.a, t1.b, t0.c0 AS c, v0.c0 AS d FROM t0 LEFT JOIN v0 ON v0.c0>'0',t1) WHERE b='2'; +} {} + + +finish_test diff --git a/test/unionall2.test b/test/unionall2.test new file mode 100644 index 0000000000..5f095e4069 --- /dev/null +++ b/test/unionall2.test @@ -0,0 +1,58 @@ +# 2020-12-22 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is flattening UNION ALL sub-queries. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix unionall2 + +do_execsql_test 1.0 { + CREATE TABLE t1(a, b); + CREATE TABLE t2(c, d); + + CREATE VIEW v1 AS SELECT * FROM t1, t2; + CREATE VIEW v2 AS SELECT * FROM t1, t2; + + CREATE VIEW vA AS + SELECT * FROM v1, ( + SELECT * FROM t1 LEFT JOIN t2 ON (a=c) + ) + UNION ALL + SELECT * FROM v1, v2 +} + +do_execsql_test 1.1 { + SELECT 1 FROM vA, vA, vA, vA, vA, vA, vA, vA, vA, vA +} + +#------------------------------------------------------------------------- + +do_execsql_test 2.1 { + CREATE TABLE y1(a INTEGER, b); + CREATE TABLE y2(c INTEGER, d); + + CREATE TABLE x3_a(a INTEGER PRIMARY KEY, b TEXT); + CREATE TABLE x3_b(c INTEGER PRIMARY KEY, d TEXT); +} + +do_execsql_test 2.2 { + + SELECT * FROM y1 CROSS JOIN y2 WHERE y1.a=y2.c AND y2.c IN ( + SELECT a FROM x3_a UNION ALL + SELECT c FROM x3_b ORDER BY 1 + ) +} + + + +finish_test diff --git a/test/unionallfault.test b/test/unionallfault.test new file mode 100644 index 0000000000..c78abe548c --- /dev/null +++ b/test/unionallfault.test @@ -0,0 +1,36 @@ +# 2020-12-16 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix unionallfault + +do_execsql_test 1.0 { + CREATE TABLE t1(x,y,z); + CREATE TABLE t3(x,y,z); +} +faultsim_save_and_close + + +do_faultsim_test 1 -faults oom-t* -prep { + faultsim_restore_and_reopen +} -body { + execsql { + SELECT * FROM t1, ( + SELECT x FROM t1 UNION ALL SELECT y FROM t1 + ), t3 + } +} -test { + faultsim_test_result {0 {}} +} + +finish_test diff --git a/test/unionvtab.test b/test/unionvtab.test new file mode 100644 index 0000000000..ac5ecb7abb --- /dev/null +++ b/test/unionvtab.test @@ -0,0 +1,457 @@ +# 2017-07-15 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is percentile.c extension +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix unionvtab + +ifcapable !vtab { + finish_test + return +} + +load_static_extension db unionvtab + +#------------------------------------------------------------------------- +# Warm body tests. +# +forcedelete test.db2 +do_execsql_test 1.0 { + ATTACH 'test.db2' AS aux; + + CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT); + CREATE TABLE t2(a INTEGER PRIMARY KEY, b TEXT); + CREATE TABLE aux.t3(a INTEGER PRIMARY KEY, b TEXT); + + + INSERT INTO t1 VALUES(1, 'one'), (2, 'two'), (3, 'three'); + INSERT INTO t2 VALUES(10, 'ten'), (11, 'eleven'), (12, 'twelve'); + INSERT INTO t3 VALUES(20, 'twenty'), (21, 'twenty-one'), (22, 'twenty-two'); +} + +do_execsql_test 1.1 { + CREATE VIRTUAL TABLE temp.uuu USING unionvtab( + "VALUES(NULL, 't1', 1, 9), ('main', 't2', 10, 19), ('aux', 't3', 20, 29)" + ); + SELECT * FROM uuu; +} { + 1 one 2 two 3 three + 10 ten 11 eleven 12 twelve + 20 twenty 21 twenty-one 22 twenty-two +} + +do_execsql_test 1.2 { + PRAGMA table_info(uuu); +} { + 0 a INTEGER 0 {} 0 + 1 b TEXT 0 {} 0 +} + +do_execsql_test 1.3 { + SELECT * FROM uuu WHERE rowid = 3; + SELECT * FROM uuu WHERE rowid = 11; +} {3 three 11 eleven} + +do_execsql_test 1.4 { + SELECT * FROM uuu WHERE rowid IN (12, 10, 2); +} {2 two 10 ten 12 twelve} + +do_execsql_test 1.5 { + SELECT * FROM uuu WHERE rowid BETWEEN 3 AND 11; +} {3 three 10 ten 11 eleven} + +do_execsql_test 1.6 { + SELECT * FROM uuu WHERE rowid BETWEEN 11 AND 15; +} {11 eleven 12 twelve} + +do_execsql_test 1.7 { + SELECT * FROM uuu WHERE rowid BETWEEN -46 AND 1500; +} { + 1 one 2 two 3 three + 10 ten 11 eleven 12 twelve + 20 twenty 21 twenty-one 22 twenty-two +} + +do_execsql_test 1.8 { + CREATE TABLE src(db, tbl, min, max); + INSERT INTO src VALUES(NULL, 't1', 1, 9); + INSERT INTO src VALUES('main', 't2', 10, 19); + INSERT INTO src VALUES('aux', 't3', 20, 29); + CREATE VIRTUAL TABLE temp.opp USING unionvtab(src); + SELECT * FROM opp; +} { + 1 one 2 two 3 three + 10 ten 11 eleven 12 twelve + 20 twenty 21 twenty-one 22 twenty-two +} + +do_execsql_test 1.9 { + CREATE VIRTUAL TABLE temp.qll USING unionvtab( + 'SELECT * FROM src WHERE db!=''xyz''' + ); + SELECT * FROM qll WHERE rowid BETWEEN 10 AND 21; +} { + 10 ten 11 eleven 12 twelve + 20 twenty 21 twenty-one +} + +#------------------------------------------------------------------------- +# Error conditions. +# +# 2.1.*: Attempt to create a unionvtab table outside of the TEMP schema. +# 2.2.*: Tables that do not exist. +# 2.3.*: Non rowid tables. +# 2.4.*: Tables with mismatched schemas. +# 2.5.*: A unionvtab table with zero source tables. +# +do_catchsql_test 2.1.1 { + CREATE VIRTUAL TABLE u1 USING unionvtab("VALUES(NULL, 't1', 1, 100)"); +} {1 {unionvtab tables must be created in TEMP schema}} +do_catchsql_test 2.1.2 { + CREATE VIRTUAL TABLE main.u1 USING unionvtab("VALUES('', 't1', 1, 100)"); +} {1 {unionvtab tables must be created in TEMP schema}} +do_catchsql_test 2.1.3 { + CREATE VIRTUAL TABLE aux.u1 USING unionvtab("VALUES('', 't1', 1, 100)"); +} {1 {unionvtab tables must be created in TEMP schema}} + +do_catchsql_test 2.2.1 { + CREATE VIRTUAL TABLE temp.u1 USING unionvtab("VALUES(NULL, 't555', 1, 100)"); +} {1 {no such rowid table: t555}} +do_catchsql_test 2.2.2 { + CREATE VIRTUAL TABLE temp.u1 USING unionvtab("VALUES('aux', 't555', 1, 100)"); +} {1 {no such rowid table: aux.t555}} +do_catchsql_test 2.2.3 { + CREATE VIRTUAL TABLE temp.u1 USING unionvtab("VALUES('xua', 't555', 1, 100)"); +} {1 {no such rowid table: xua.t555}} + +do_execsql_test 2.3.0 { + CREATE TABLE wr1(a, b, c PRIMARY KEY) WITHOUT ROWID; + CREATE VIEW v1 AS SELECT * FROM t1; + CREATE VIEW v2 AS SELECT _rowid_, * FROM t1; + + CREATE TABLE wr2(a, _rowid_ INTEGER, c PRIMARY KEY) WITHOUT ROWID; + CREATE TABLE wr3(a, b, _rowid_ PRIMARY KEY) WITHOUT ROWID; +} +do_catchsql_test 2.3.1 { + CREATE VIRTUAL TABLE temp.u1 USING unionvtab("VALUES('main', 'wr1', 1, 2)"); +} {1 {no such rowid table: main.wr1}} +do_catchsql_test 2.3.2 { + CREATE VIRTUAL TABLE temp.u1 USING unionvtab("VALUES(NULL, 'v1', 1, 2)"); +} {1 {no such rowid table: v1}} +do_catchsql_test 2.3.3 { + CREATE VIRTUAL TABLE temp.u1 USING unionvtab("VALUES(NULL, 'v2', 1, 2)"); +} {1 {no such rowid table: v2}} +do_catchsql_test 2.3.4 { + CREATE VIRTUAL TABLE temp.u1 USING unionvtab("VALUES(NULL, 'wr2', 1, 2)"); +} {1 {no such rowid table: wr2}} +do_catchsql_test 2.3.5 { + CREATE VIRTUAL TABLE temp.u1 USING unionvtab("VALUES(NULL, 'wr3', 1, 2)"); +} {1 {no such rowid table: wr3}} + +do_execsql_test 2.4.0 { + CREATE TABLE x1(a BLOB, b); + CREATE TABLE x2(a BLOB, b); + CREATE TEMP TABLE x3(a BLOB, b); + + CREATE TABLE aux.y1(one, two, three INTEGER PRIMARY KEY); + CREATE TEMP TABLE y2(one, two, three INTEGER PRIMARY KEY); + CREATE TABLE y3(one, two, three INTEGER PRIMARY KEY); +} + +foreach {tn dbs res} { + 1 {x1 x2 x3} {0 {}} + 2 {y1 y2 y3} {0 {}} + 3 {x1 y2 y3} {1 {source table schema mismatch}} + 4 {x1 y2 x3} {1 {source table schema mismatch}} + 5 {x1 x2 y3} {1 {source table schema mismatch}} +} { + set L [list] + set iMin 0 + foreach e $dbs { + set E [split $e .] + if {[llength $E]>1} { + lappend L "('[lindex $E 0]', '[lindex $E 1]', $iMin, $iMin)" + } else { + lappend L "(NULL, '$e', $iMin, $iMin)" + } + incr iMin + } + + set sql "CREATE VIRTUAL TABLE temp.a1 USING unionvtab(\"VALUES [join $L ,]\")" + do_catchsql_test 2.4.$tn " + DROP TABLE IF EXISTS temp.a1; + CREATE VIRTUAL TABLE temp.a1 USING unionvtab(\"VALUES [join $L ,]\"); + " $res +} + +do_catchsql_test 2.5 { + CREATE VIRTUAL TABLE temp.b1 USING unionvtab( + [SELECT 'main', 'b1', 0, 100 WHERE 0] + ) +} {1 {no source tables configured}} + +foreach {tn sql} { + 1 { VALUES('main', 't1', 10, 20), ('main', 't2', 30, 29) } + 2 { VALUES('main', 't1', 10, 20), ('main', 't2', 15, 30) } +} { + do_catchsql_test 2.6.$tn " + CREATE VIRTUAL TABLE temp.a1 USING unionvtab(`$sql`) + " {1 {rowid range mismatch error}} +} + +do_catchsql_test 2.7.1 { + CREATE VIRTUAL TABLE temp.b1 USING unionvtab(1, 2, 3, 4) +} {1 {wrong number of arguments for unionvtab}} + +#------------------------------------------------------------------------- +# +reset_db +load_static_extension db unionvtab +do_execsql_test 3.0 { + CREATE TABLE tbl1(a INTEGER PRIMARY KEY, b); + CREATE TABLE tbl2(a INTEGER PRIMARY KEY, b); + CREATE TABLE tbl3(a INTEGER PRIMARY KEY, b); + + WITH ss(ii) AS ( SELECT 1 UNION ALL SELECT ii+1 FROM ss WHERE ii<100 ) + INSERT INTO tbl1 SELECT ii, '1.' || ii FROM ss; + + WITH ss(ii) AS ( SELECT 1 UNION ALL SELECT ii+1 FROM ss WHERE ii<100 ) + INSERT INTO tbl2 SELECT ii, '2.' || ii FROM ss; + + WITH ss(ii) AS ( SELECT 1 UNION ALL SELECT ii+1 FROM ss WHERE ii<100 ) + INSERT INTO tbl3 SELECT ii, '3.' || ii FROM ss; + + CREATE VIRTUAL TABLE temp.uu USING unionvtab( + "VALUES(NULL,'tbl2', 26, 74), (NULL,'tbl3', 75, 100), (NULL,'tbl1', 1, 25)" + ); +} + +do_execsql_test 3.1 { + SELECT * FROM uu WHERE rowid = 10; +} {10 {1.10}} +do_execsql_test 3.2 { + SELECT * FROM uu WHERE rowid = 25; +} {25 {1.25}} + +do_execsql_test 3.3 { SELECT count(*) FROM uu WHERE rowid <= 24 } {24} + +# The following queries get the "wrong" answers. This is because the +# module assumes that each source table contains rowids from only within +# the range specified. For example, (rowid <= 25) matches 100 rows. This +# is because the module implements (rowid <= 25) as a full table scan +# of tbl1 only. +do_execsql_test 3.4.1 { SELECT count(*) FROM uu WHERE rowid <= 25 } {100} +do_execsql_test 3.4.2 { SELECT count(*) FROM uu WHERE rowid <= 26 } {126} +do_execsql_test 3.4.3 { SELECT count(*) FROM uu WHERE rowid <= 73 } {173} +do_execsql_test 3.4.4 { SELECT count(*) FROM uu WHERE rowid <= 74 } {200} +do_execsql_test 3.4.5 { SELECT count(*) FROM uu WHERE rowid <= 75 } {275} +do_execsql_test 3.4.6 { SELECT count(*) FROM uu WHERE rowid <= 99 } {299} +do_execsql_test 3.4.7 { SELECT count(*) FROM uu WHERE rowid <= 100 } {300} +do_execsql_test 3.4.8 { SELECT count(*) FROM uu WHERE rowid <= 101 } {300} + +do_execsql_test 3.5.1 { SELECT count(*) FROM uu WHERE rowid < 25 } {24} +do_execsql_test 3.5.2 { SELECT count(*) FROM uu WHERE rowid < 26 } {100} +do_execsql_test 3.5.3 { SELECT count(*) FROM uu WHERE rowid < 27 } {126} +do_execsql_test 3.5.4 { SELECT count(*) FROM uu WHERE rowid < 73 } {172} +do_execsql_test 3.5.5 { SELECT count(*) FROM uu WHERE rowid < 74 } {173} +do_execsql_test 3.5.6 { SELECT count(*) FROM uu WHERE rowid < 75 } {200} +do_execsql_test 3.5.7 { SELECT count(*) FROM uu WHERE rowid < 76 } {275} +do_execsql_test 3.5.8 { SELECT count(*) FROM uu WHERE rowid < 99 } {298} +do_execsql_test 3.5.9 { SELECT count(*) FROM uu WHERE rowid < 100 } {299} +do_execsql_test 3.5.10 { SELECT count(*) FROM uu WHERE rowid < 101 } {300} + +do_execsql_test 3.6.1 { SELECT count(*) FROM uu WHERE rowid > 24 } {276} +do_execsql_test 3.6.1 { SELECT count(*) FROM uu WHERE rowid > 25 } {200} +do_execsql_test 3.6.2 { SELECT count(*) FROM uu WHERE rowid > 26 } {174} +do_execsql_test 3.6.3 { SELECT count(*) FROM uu WHERE rowid > 27 } {173} +do_execsql_test 3.6.4 { SELECT count(*) FROM uu WHERE rowid > 73 } {127} +do_execsql_test 3.6.5 { SELECT count(*) FROM uu WHERE rowid > 74 } {100} +do_execsql_test 3.6.6 { SELECT count(*) FROM uu WHERE rowid > 75 } {25} +do_execsql_test 3.6.7 { SELECT count(*) FROM uu WHERE rowid > 76 } {24} +do_execsql_test 3.6.8 { SELECT count(*) FROM uu WHERE rowid > 99 } {1} +do_execsql_test 3.6.9 { SELECT count(*) FROM uu WHERE rowid > 100 } {0} +do_execsql_test 3.6.10 { SELECT count(*) FROM uu WHERE rowid > 101 } {0} + +do_execsql_test 3.7.1 { SELECT count(*) FROM uu WHERE rowid >= 24 } {277} +do_execsql_test 3.7.1 { SELECT count(*) FROM uu WHERE rowid >= 25 } {276} +do_execsql_test 3.7.2 { SELECT count(*) FROM uu WHERE rowid >= 26 } {200} +do_execsql_test 3.7.3 { SELECT count(*) FROM uu WHERE rowid >= 27 } {174} +do_execsql_test 3.7.4 { SELECT count(*) FROM uu WHERE rowid >= 73 } {128} +do_execsql_test 3.7.5 { SELECT count(*) FROM uu WHERE rowid >= 74 } {127} +do_execsql_test 3.7.6 { SELECT count(*) FROM uu WHERE rowid >= 75 } {100} +do_execsql_test 3.7.7 { SELECT count(*) FROM uu WHERE rowid >= 76 } {25} +do_execsql_test 3.7.8 { SELECT count(*) FROM uu WHERE rowid >= 99 } {2} +do_execsql_test 3.7.9 { SELECT count(*) FROM uu WHERE rowid >= 100 } {1} +do_execsql_test 3.7.10 { SELECT count(*) FROM uu WHERE rowid >= 101 } {0} + +set L [expr 9223372036854775807] +set S [expr -9223372036854775808] + +do_execsql_test 3.8.1 { SELECT count(*) FROM uu WHERE rowid >= $S } {300} +do_execsql_test 3.8.2 { SELECT count(*) FROM uu WHERE rowid > $S } {300} +do_execsql_test 3.8.3 { SELECT count(*) FROM uu WHERE rowid <= $S } {0} +do_execsql_test 3.8.4 { SELECT count(*) FROM uu WHERE rowid < $S } {0} + +do_execsql_test 3.9.1 { SELECT count(*) FROM uu WHERE rowid >= $L } {0} +do_execsql_test 3.9.2 { SELECT count(*) FROM uu WHERE rowid > $L } {0} +do_execsql_test 3.9.3 { SELECT count(*) FROM uu WHERE rowid <= $L } {300} +do_execsql_test 3.9.4 { SELECT count(*) FROM uu WHERE rowid < $L } {300} + +do_execsql_test 3.10.1 { SELECT count(*) FROM uu WHERE a < 25 } {24} +do_execsql_test 3.10.2 { SELECT count(*) FROM uu WHERE a < 26 } {100} +do_execsql_test 3.10.3 { SELECT count(*) FROM uu WHERE a < 27 } {126} +do_execsql_test 3.10.4 { SELECT count(*) FROM uu WHERE a < 73 } {172} +do_execsql_test 3.10.5 { SELECT count(*) FROM uu WHERE a < 74 } {173} +do_execsql_test 3.10.6 { SELECT count(*) FROM uu WHERE a < 75 } {200} +do_execsql_test 3.10.7 { SELECT count(*) FROM uu WHERE a < 76 } {275} +do_execsql_test 3.10.8 { SELECT count(*) FROM uu WHERE a < 99 } {298} +do_execsql_test 3.10.9 { SELECT count(*) FROM uu WHERE a < 100 } {299} +do_execsql_test 3.10.10 { SELECT count(*) FROM uu WHERE a < 101 } {300} + + +#------------------------------------------------------------------------- +# +do_execsql_test 4.0 { + CREATE TABLE s1(k INTEGER PRIMARY KEY, v); + INSERT INTO s1 VALUES($S, 'one'); + INSERT INTO s1 VALUES($S+1, 'two'); + INSERT INTO s1 VALUES($S+2, 'three'); + + CREATE TABLE l1(k INTEGER PRIMARY KEY, v); + INSERT INTO l1 VALUES($L, 'six'); + INSERT INTO l1 VALUES($L-1, 'five'); + INSERT INTO l1 VALUES($L-2, 'four'); + + CREATE VIRTUAL TABLE temp.sl USING unionvtab( + "SELECT NULL, 'l1', 0, 9223372036854775807 + UNION ALL + SELECT NULL, 's1', -9223372036854775808, -1" + ); +} + +do_execsql_test 4.1 { + SELECT * FROM sl; +} { + -9223372036854775808 one -9223372036854775807 two -9223372036854775806 three + 9223372036854775805 four 9223372036854775806 five 9223372036854775807 six +} + +foreach {k v} { + -9223372036854775808 one -9223372036854775807 two -9223372036854775806 three + 9223372036854775805 four 9223372036854775806 five 9223372036854775807 six +} { + do_execsql_test 4.2.$v { SELECT * FROM sl WHERE rowid=$k } [list $k $v] +} + +do_execsql_test 4.3.1 { + SELECT * FROM sl WHERE rowid>-9223372036854775808 +} { + -9223372036854775807 two -9223372036854775806 three + 9223372036854775805 four 9223372036854775806 five 9223372036854775807 six +} +do_execsql_test 4.3.2 { + SELECT * FROM sl WHERE rowid>=-9223372036854775808 +} { + -9223372036854775808 one -9223372036854775807 two -9223372036854775806 three + 9223372036854775805 four 9223372036854775806 five 9223372036854775807 six +} +do_execsql_test 4.3.3 { + SELECT * FROM sl WHERE rowid<=-9223372036854775808 +} { + -9223372036854775808 one +} +do_execsql_test 4.3.4 { + SELECT * FROM sl WHERE rowid<-9223372036854775808 +} {} + +do_execsql_test 4.4.1 { + SELECT * FROM sl WHERE rowid<9223372036854775807 +} { + -9223372036854775808 one -9223372036854775807 two -9223372036854775806 three + 9223372036854775805 four 9223372036854775806 five +} +do_execsql_test 4.4.2 { + SELECT * FROM sl WHERE rowid<=9223372036854775807 +} { + -9223372036854775808 one -9223372036854775807 two -9223372036854775806 three + 9223372036854775805 four 9223372036854775806 five 9223372036854775807 six +} +do_execsql_test 4.4.3 { + SELECT * FROM sl WHERE rowid>=9223372036854775807 +} { + 9223372036854775807 six +} +do_execsql_test 4.4.4 { + SELECT * FROM sl WHERE rowid>9223372036854775807 +} {} + +#------------------------------------------------------------------------- +# More than 8 source tables. +# +do_execsql_test 5.0 { + CREATE TABLE c0(one, two INTEGER PRIMARY KEY); + CREATE TABLE c1(one, two INTEGER PRIMARY KEY); + CREATE TABLE c2(one, two INTEGER PRIMARY KEY); + CREATE TABLE c3(one, two INTEGER PRIMARY KEY); + CREATE TABLE c4(one, two INTEGER PRIMARY KEY); + CREATE TABLE c5(one, two INTEGER PRIMARY KEY); + CREATE TABLE c6(one, two INTEGER PRIMARY KEY); + CREATE TABLE c7(one, two INTEGER PRIMARY KEY); + CREATE TABLE c8(one, two INTEGER PRIMARY KEY); + CREATE TABLE c9(one, two INTEGER PRIMARY KEY); + + INSERT INTO c0 VALUES('zero', 0); + INSERT INTO c1 VALUES('one', 1); + INSERT INTO c2 VALUES('two', 2); + INSERT INTO c3 VALUES('three', 3); + INSERT INTO c4 VALUES('four', 4); + INSERT INTO c5 VALUES('five', 5); + INSERT INTO c6 VALUES('six', 6); + INSERT INTO c7 VALUES('seven', 7); + INSERT INTO c8 VALUES('eight', 8); + INSERT INTO c9 VALUES('nine', 9); + + CREATE VIRTUAL TABLE temp.cc USING unionvtab([ + SELECT 'main', 'c9', 9, 9 UNION ALL + SELECT 'main', 'c8', 8, 8 UNION ALL + SELECT 'main', 'c7', 7, 7 UNION ALL + SELECT 'main', 'c6', 6, 6 UNION ALL + SELECT 'main', 'c5', 5, 5 UNION ALL + SELECT 'main', 'c4', 4, 4 UNION ALL + SELECT 'main', 'c3', 3, 3 UNION ALL + SELECT 'main', 'c2', 2, 2 UNION ALL + SELECT 'main', 'c1', 1, 1 UNION ALL + SELECT 'main', 'c0', 0, 0 + ]); + + SELECT sum(two) FROM cc; +} {45} + +do_execsql_test 5.1 { + SELECT one FROM cc WHERE one>='seven' +} {zero two three six seven} + +do_execsql_test 5.2 { + SELECT y.one FROM cc AS x, cc AS y WHERE x.one=y.one AND x.rowid>5 +} {six seven eight nine} + +do_execsql_test 5.3 { + SELECT cc.one FROM c4, cc WHERE cc.rowid>c4.rowid +} {five six seven eight nine} + +do_execsql_test 5.4 { + SELECT * FROM cc WHERE two LIKE '6' +} {six 6} + +finish_test diff --git a/test/unionvtabfault.test b/test/unionvtabfault.test new file mode 100644 index 0000000000..f66c9ac978 --- /dev/null +++ b/test/unionvtabfault.test @@ -0,0 +1,83 @@ +# 2017-07-15 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is percentile.c extension +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix unionvtabfault + +ifcapable !vtab { + finish_test + return +} + +forcedelete test.db2 +do_execsql_test 1.0 { + ATTACH 'test.db2' AS aux; + CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT); + CREATE TABLE t2(a INTEGER PRIMARY KEY, b TEXT); + CREATE TABLE aux.t3(a INTEGER PRIMARY KEY, b TEXT); + + INSERT INTO t1 VALUES(1, 'one'), (2, 'two'), (3, 'three'); + INSERT INTO t2 VALUES(10, 'ten'), (11, 'eleven'), (12, 'twelve'); + INSERT INTO t3 VALUES(20, 'twenty'), (21, 'twenty-one'), (22, 'twenty-two'); +} +faultsim_save_and_close + +do_faultsim_test 1.1 -faults * -prep { + faultsim_restore_and_reopen + load_static_extension db unionvtab + execsql { ATTACH 'test.db2' AS aux; } + execsql { CREATE TEMP TABLE xyz(x); } +} -body { + execsql { + CREATE VIRTUAL TABLE temp.uuu USING unionvtab( + "VALUES(NULL, 't1', 1, 9), ('main', 't2', 10, 19), ('aux', 't3', 20, 29)" + ); + } +} -test { + faultsim_test_result {0 {}} \ + {1 {vtable constructor failed: uuu}} \ + {1 {sql error: interrupted}} +} + +faultsim_restore_and_reopen +load_static_extension db unionvtab +execsql { ATTACH 'test.db2' AS aux; } +execsql { CREATE TEMP TABLE xyz(x); } +execsql { + CREATE VIRTUAL TABLE temp.uuu USING unionvtab( + "VALUES(NULL, 't1', 1, 9), ('main', 't2', 10, 19), ('aux', 't3', 20, 29)" + ); +} +do_faultsim_test 1.2 -faults oom* -prep { +} -body { + execsql { SELECT * FROM uuu } +} -test { + faultsim_test_result {0 {1 one 2 two 3 three 10 ten 11 eleven 12 twelve 20 twenty 21 twenty-one 22 twenty-two}} +} + +#------------------------------------------------------------------------- +# Error while registering the two vtab modules. +do_faultsim_test 2.0 -faults * -prep { + catch { db close } + sqlite3 db :memory: +} -body { + load_static_extension db unionvtab +} -test { + faultsim_test_result {0 {}} {1 {initialization of unionvtab failed: }} +} + + + +finish_test diff --git a/test/unixexcl.test b/test/unixexcl.test index d6762178dd..c24945e5e3 100644 --- a/test/unixexcl.test +++ b/test/unixexcl.test @@ -18,7 +18,7 @@ source $testdir/tester.tcl source $testdir/lock_common.tcl source $testdir/malloc_common.tcl -if {$::tcl_platform(platform)!="unix" || [info commands test_syscall]==""} { +if {[llength [info commands test_syscall]]==0} { finish_test return } @@ -87,6 +87,7 @@ do_multiclient_test tn { sql1 { PRAGMA auto_vacuum = 0; PRAGMA journal_mode = WAL; + PRAGMA synchronous = FULL; CREATE TABLE t1(a, b); INSERT INTO t1 VALUES(1, 2); } diff --git a/test/unordered.test b/test/unordered.test index 147e91f0d9..80595d95f7 100644 --- a/test/unordered.test +++ b/test/unordered.test @@ -40,28 +40,27 @@ foreach idxmode {ordered unordered} { sqlite3 db test.db foreach {tn sql r(ordered) r(unordered)} { 1 "SELECT * FROM t1 ORDER BY a" - {0 0 0 {SCAN TABLE t1 USING INDEX i1}} - {0 0 0 {SCAN TABLE t1} 0 0 0 {USE TEMP B-TREE FOR ORDER BY}} + {SCAN t1 USING INDEX i1} + {SCAN t1*USE TEMP B-TREE FOR ORDER BY} 2 "SELECT * FROM t1 WHERE a > 100" - {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (a>?)}} - {0 0 0 {SCAN TABLE t1}} + {SEARCH t1 USING INDEX i1 (a>?)} + {SCAN t1} 3 "SELECT * FROM t1 WHERE a = ? ORDER BY rowid" - {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (a=?)}} - {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (a=?)} - 0 0 0 {USE TEMP B-TREE FOR ORDER BY}} + {SEARCH t1 USING INDEX i1 (a=?)} + {SEARCH t1 USING INDEX i1 (a=?)*USE TEMP B-TREE FOR ORDER BY} 4 "SELECT max(a) FROM t1" - {0 0 0 {SEARCH TABLE t1 USING COVERING INDEX i1}} - {0 0 0 {SEARCH TABLE t1}} + {SEARCH t1 USING COVERING INDEX i1} + {SEARCH t1} 5 "SELECT group_concat(b) FROM t1 GROUP BY a" - {0 0 0 {SCAN TABLE t1 USING INDEX i1}} - {0 0 0 {SCAN TABLE t1} 0 0 0 {USE TEMP B-TREE FOR GROUP BY}} + {SCAN t1 USING INDEX i1} + {SCAN t1*USE TEMP B-TREE FOR GROUP BY} 6 "SELECT * FROM t1 WHERE a = ?" - {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (a=?)}} - {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (a=?)}} + {SEARCH t1 USING INDEX i1 (a=?)} + {SEARCH t1 USING INDEX i1 (a=?)} 7 "SELECT count(*) FROM t1" - {0 0 0 {SCAN TABLE t1 USING COVERING INDEX i1}} - {0 0 0 {SCAN TABLE t1}} + {SCAN t1 USING COVERING INDEX i1} + {SCAN t1} } { do_eqp_test 1.$idxmode.$tn $sql $r($idxmode) } diff --git a/test/update.test b/test/update.test index d7baf6e702..0a380fa030 100644 --- a/test/update.test +++ b/test/update.test @@ -508,6 +508,18 @@ ifcapable subquery { SELECT a,e FROM t1; } } {1 15 2 8} + do_test update-11.3 { + execsql { + UPDATE t1 AS xyz SET e=e+1 WHERE xyz.a IN (SELECT a FROM t1); + SELECT a,e FROM t1; + } + } {1 16 2 9} + do_test update-11.4 { + execsql { + UPDATE t1 AS xyz SET e=e+1 WHERE EXISTS(SELECT 1 FROM t1 WHERE t1.a<xyz.a); + SELECT a,e FROM t1; + } + } {1 16 2 10} } integrity_check update-12.1 @@ -604,19 +616,175 @@ do_test update-14.4 { } ;# ifcapable {trigger} -# Ticket [https://www.sqlite.org/src/tktview/43107840f1c02] on 2014-10-29 +# Ticket [https://sqlite.org/src/tktview/43107840f1c02] on 2014-10-29 # An assertion fault on UPDATE # -do_execsql_test update-15.1 { - CREATE TABLE t15(a INTEGER PRIMARY KEY, b); - INSERT INTO t15(a,b) VALUES(10,'abc'),(20,'def'),(30,'ghi'); - ALTER TABLE t15 ADD COLUMN c; - CREATE INDEX t15c ON t15(c); - INSERT INTO t15(a,b) - VALUES(5,'zyx'),(15,'wvu'),(25,'tsr'),(35,'qpo'); - UPDATE t15 SET c=printf("y%d",a) WHERE c IS NULL; - SELECT a,b,c,'|' FROM t15 ORDER BY a; -} {5 zyx y5 | 10 abc y10 | 15 wvu y15 | 20 def y20 | 25 tsr y25 | 30 ghi y30 | 35 qpo y35 |} +ifcapable altertable { + do_execsql_test update-15.1 { + CREATE TABLE t15(a INTEGER PRIMARY KEY, b); + INSERT INTO t15(a,b) VALUES(10,'abc'),(20,'def'),(30,'ghi'); + ALTER TABLE t15 ADD COLUMN c; + CREATE INDEX t15c ON t15(c); + INSERT INTO t15(a,b) + VALUES(5,'zyx'),(15,'wvu'),(25,'tsr'),(35,'qpo'); + UPDATE t15 SET c=printf('y%d',a) WHERE c IS NULL; + SELECT a,b,c,'|' FROM t15 ORDER BY a; + } {5 zyx y5 | 10 abc y10 | 15 wvu y15 | 20 def y20 | 25 tsr y25 | 30 ghi y30 | 35 qpo y35 |} +} + +# Unreleased bug in UPDATE caused by the UPSERT changes. +# Found by OSSFuzz as soon as the UPSERT changes landed on trunk. +# Never released into the wild. 2018-04-19. +# +do_execsql_test update-16.1 { + CREATE TABLE t16(a INTEGER PRIMARY KEY ON CONFLICT REPLACE, b UNIQUE); + INSERT INTO t16(a,b) VALUES(1,2),(3,4),(5,6); + UPDATE t16 SET a=a; + SELECT * FROM t16 ORDER BY +a; +} {1 2 3 4 5 6} + +# 2019-12-09 gramfuzz find +# If a partial index that does not reference any column of its table (which is you +# must admit is a very strange index, but one that is allowed) is used by an UPDATE +# statement, void the use of OP_DeferredSeek on the main loop, as the seek will not +# be resolved prior to the OP_Delete. +# +do_execsql_test update-17.10 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(x,y); + INSERT INTO t1(x) VALUES(1); + CREATE INDEX t1x1 ON t1(1) WHERE 3; + UPDATE t1 SET x=2, y=3 WHERE 3; + SELECT * FROM t1; +} {2 3} + +# 2019-12-22 ticket 5ad2aa6921faa1ee +# Make a hard-copy of values that need to be run through OP_RealAffinity +# rather than a soft-copy. This is not strictly necessary, but it avoids +# a memory-accounting assert(). +# +reset_db +do_execsql_test update-18.10 { + PRAGMA encoding = 'UTF16'; + CREATE TABLE t0(c0 REAL, c1); + INSERT INTO t0(c0,c1) VALUES('xyz',11),('uvw',22); + CREATE INDEX i0 ON t0(c1) WHERE c0 GLOB 3; + CREATE INDEX i1 ON t0(c0,c1) WHERE typeof(c0)='text' AND typeof(c1)='integer'; + UPDATE t0 SET c1=345; + SELECT * FROM t0; +} {xyz 345 uvw 345} +# 2019-12-22 ticket c62c5e58524b204d +# This is really the same underlying problem as 5ad2aa6921faa1ee +# +reset_db +do_execsql_test update-18.20 { + PRAGMA encoding = 'utf16'; + CREATE TABLE t0(c0 TEXT); + CREATE INDEX i0 ON t0(0 LIKE COALESCE(c0, 0)); + INSERT INTO t0(c0) VALUES (0), (0); + SELECT * FROM t0; +} {0 0} + +# 2019-12-28 assertion fault reported by Yongheng +# Similar to ticket ec8abb025e78f40c +# An UPDATE was reaching the OP_Delete after running OP_DeferredSeek +# without ever hitting an OP_Column. The enhanced solution is to +# fix OP_Delete so that it can do the seek itself. +# +reset_db +do_execsql_test update-19.10 { + CREATE TABLE t1( + a TEXT, + b INTEGER PRIMARY KEY UNIQUE + ); + INSERT INTO t1 VALUES(1,2); + UPDATE t1 SET a = quote(b) WHERE b>=2; + SELECT * FROM t1; +} {2 2} + +# 2019-12-29 ticket https://sqlite.org/src/info/314cc133e5ada126 +# REPLACE conflict resolution during an UPDATE causes a DELETE trigger +# to fire. If that DELETE trigger subsequently modifies the row +# being updated, bad things can happen. Prevent this by prohibiting +# triggers from making changes to the table being updated while doing +# REPLACE conflict resolution on the UPDATE. +# +# See also tickets: +# https://sqlite.org/src/info/c1e19e12046d23fe 2019-10-25 +# https://sqlite.org/src/info/a8a4847a2d96f5de 2019-10-16 +# +reset_db +do_execsql_test update-20.10 { + PRAGMA recursive_triggers = true; + CREATE TABLE t1(a UNIQUE ON CONFLICT REPLACE, b); + INSERT INTO t1(a,b) VALUES(4,12),(9,13); + CREATE INDEX i0 ON t1(b); + CREATE TRIGGER tr0 DELETE ON t1 BEGIN + UPDATE t1 SET b = a; + END; + PRAGMA integrity_check; +} {ok} +do_catchsql_test update-20.20 { + UPDATE t1 SET a=0; +} {1 {constraint failed}} +do_execsql_test update-20.30 { + PRAGMA integrity_check; +} {ok} + +# 2023-03-16 https://sqlite.org/forum/forumpost/0007d1fdb1 +# A subquery in the WHERE clause of an UPDATE and behind a +# short-circuit evaluation caused problems because multi-row +# single-pass was selected. +# +# Similar problem for DELETE tested by delete-12.0. +# https://sqlite.org/src/info/73f0036f045bf371 +# +reset_db +do_execsql_test update-21.1 { + CREATE TABLE t1 (vkey INTEGER, c5 INTEGER); + INSERT INTO t1 VALUES(3,NULL),(6,-54); +} +db null NULL +do_execsql_test update-21.2 { + BEGIN; + UPDATE t1 SET vkey = 100 WHERE c5 is null; + SELECT * FROM t1 ORDER BY vkey, c5; + ROLLBACK; +} {6 -54 100 NULL} +do_execsql_test update-21.3 { + BEGIN; + UPDATE t1 SET vkey = 100 WHERE NOT (-10*(select min(vkey) from t1) >= c5); + SELECT * FROM t1 ORDER BY vkey, c5; + ROLLBACK; +} {3 NULL 6 -54} +do_execsql_test update-21.4 { + BEGIN; + UPDATE t1 SET vkey = 100 WHERE c5 is null OR NOT (-10*(select min(vkey) from t1) >= c5); + SELECT * FROM t1 ORDER BY vkey, c5; + ROLLBACK; +} {6 -54 100 NULL} +# Follow-up on 2023-07-31 (forum post https://sqlite.org/forum/forumpost/8ab195fd44e75ed0): +# Only disable one-pass if the subquery is in the WHERE clause. The SET expressions +# do not count. +do_execsql_test update-21.11 { + DROP TABLE t1; + CREATE TABLE t1(a INTEGER PRIMARY KEY, b INT); + CREATE TABLE t2(d INT); +} +do_eqp_test update-21.12 { + WITH t3(x,y) AS (SELECT d, row_number()OVER() FROM t2) + UPDATE t1 SET b=(SELECT y FROM t3 WHERE t1.a=t3.x); +} { + QUERY PLAN + |--SCAN t1 + `--CORRELATED SCALAR SUBQUERY xxxxxx + |--CO-ROUTINE t3 + | |--CO-ROUTINE (subquery-xxxxxx) + | | `--SCAN t2 + | `--SCAN (subquery-xxxxxx) + |--BLOOM FILTER ON t3 (x=?) + `--SEARCH t3 USING AUTOMATIC COVERING INDEX (x=?) +} finish_test diff --git a/test/update2.test b/test/update2.test new file mode 100644 index 0000000000..41a7224d7a --- /dev/null +++ b/test/update2.test @@ -0,0 +1,235 @@ +# 2017 January 9 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix update2 + +db func repeat [list string repeat] + +#------------------------------------------------------------------------- +# 1.1.* A one-pass UPDATE that does balance() operations on the IPK index +# that it is scanning. +# +# 1.2.* Same again, but with a WITHOUT ROWID table. +# +set nrow [expr 10] +do_execsql_test 1.1.0 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b); + CREATE TABLE t2(a INTEGER PRIMARY KEY, b); + WITH s(i) AS ( SELECT 0 UNION ALL SELECT i+1 FROM s WHERE i<$nrow ) + INSERT INTO t1(b) SELECT char((i % 26) + 65) FROM s; + INSERT INTO t2 SELECT * FROM t1; +} + +do_execsql_test 1.1.1 { + UPDATE t1 SET b = repeat(b, 100) +} + +do_execsql_test 1.1.2 { + SELECT * FROM t1; +} [db eval { SELECT a, repeat(b, 100) FROM t2 }] + +do_execsql_test 1.2.0 { + DROP TABLE t1; + CREATE TABLE t1(a INT PRIMARY KEY, b) WITHOUT ROWID; + WITH s(i) AS ( SELECT 0 UNION ALL SELECT i+1 FROM s WHERE i<$nrow ) + INSERT INTO t1(a, b) SELECT i+1, char((i % 26) + 65) FROM s; +} + +#explain_i { UPDATE t1 SET b = repeat(b, 100) } +do_execsql_test 1.2.1 { + UPDATE t1 SET b = repeat(b, 100) +} + +do_execsql_test 1.2.2 { + SELECT * FROM t1; +} [db eval { SELECT a, repeat(b, 100) FROM t2 }] + + +#------------------------------------------------------------------------- +# A one-pass UPDATE that does balance() operations on the IPK index +# that it is scanning. +# +do_execsql_test 2.1 { + CREATE TABLE t3(a PRIMARY KEY, b, c); + CREATE INDEX t3i ON t3(b); +} {} +do_execsql_test 2.2 { UPDATE t3 SET c=1 WHERE b=? } {} +do_execsql_test 2.3 { UPDATE t3 SET c=1 WHERE rowid=? } {} + +#------------------------------------------------------------------------- +# +do_execsql_test 3.0 { + CREATE TABLE t4(a PRIMARY KEY, b, c) WITHOUT ROWID; + CREATE INDEX t4c ON t4(c); + INSERT INTO t4 VALUES(1, 2, 3); + INSERT INTO t4 VALUES(2, 3, 4); +} + +do_execsql_test 3.1 { + UPDATE t4 SET c=c+2 WHERE c>2; + SELECT a, c FROM t4 ORDER BY a; +} {1 5 2 6} + +#------------------------------------------------------------------------- +# +foreach {tn sql} { + 1 { + CREATE TABLE b1(a INTEGER PRIMARY KEY, b, c); + CREATE TABLE c1(a INTEGER PRIMARY KEY, b, c, d) + } + 2 { + CREATE TABLE b1(a INT PRIMARY KEY, b, c) WITHOUT ROWID; + CREATE TABLE c1(a INT PRIMARY KEY, b, c, d) WITHOUT ROWID; + } +} { + execsql { DROP TABLE IF EXISTS b1; DROP TABLE IF EXISTS c1; } + execsql $sql + + do_execsql_test 4.$tn.0 { + CREATE UNIQUE INDEX b1c ON b1(c); + INSERT INTO b1 VALUES(1, 'a', 1); + INSERT INTO b1 VALUES(2, 'b', 15); + INSERT INTO b1 VALUES(3, 'c', 3); + INSERT INTO b1 VALUES(4, 'd', 4); + INSERT INTO b1 VALUES(5, 'e', 5); + INSERT INTO b1 VALUES(6, 'f', 6); + INSERT INTO b1 VALUES(7, 'g', 7); + } + + do_execsql_test 4.$tn.1 { + UPDATE OR REPLACE b1 SET c=c+10 WHERE a BETWEEN 4 AND 7; + SELECT * FROM b1 ORDER BY a; + } { + 1 a 1 + 3 c 3 + 4 d 14 + 5 e 15 + 6 f 16 + 7 g 17 + } + + do_execsql_test 4.$tn.2 { + CREATE INDEX c1d ON c1(d, b); + CREATE UNIQUE INDEX c1c ON c1(c, b); + + INSERT INTO c1 VALUES(1, 'a', 1, 1); + INSERT INTO c1 VALUES(2, 'a', 15, 2); + INSERT INTO c1 VALUES(3, 'a', 3, 3); + INSERT INTO c1 VALUES(4, 'a', 4, 4); + INSERT INTO c1 VALUES(5, 'a', 5, 5); + INSERT INTO c1 VALUES(6, 'a', 6, 6); + INSERT INTO c1 VALUES(7, 'a', 7, 7); + } + + do_execsql_test 4.$tn.3 { + UPDATE OR REPLACE c1 SET c=c+10 WHERE d BETWEEN 4 AND 7; + SELECT * FROM c1 ORDER BY a; + } { + 1 a 1 1 + 3 a 3 3 + 4 a 14 4 + 5 a 15 5 + 6 a 16 6 + 7 a 17 7 + } + + do_execsql_test 4.$tn.4 { PRAGMA integrity_check } ok + + do_execsql_test 4.$tn.5 { + DROP INDEX c1d; + DROP INDEX c1c; + DELETE FROM c1; + + INSERT INTO c1 VALUES(1, 'a', 1, 1); + INSERT INTO c1 VALUES(2, 'a', 15, 2); + INSERT INTO c1 VALUES(3, 'a', 3, 3); + INSERT INTO c1 VALUES(4, 'a', 4, 4); + INSERT INTO c1 VALUES(5, 'a', 5, 5); + INSERT INTO c1 VALUES(6, 'a', 6, 6); + INSERT INTO c1 VALUES(7, 'a', 7, 7); + + CREATE INDEX c1d ON c1(d); + CREATE UNIQUE INDEX c1c ON c1(c); + } + + do_execsql_test 4.$tn.6 { + UPDATE OR REPLACE c1 SET c=c+10 WHERE d BETWEEN 4 AND 7; + SELECT * FROM c1 ORDER BY a; + } { + 1 a 1 1 + 3 a 3 3 + 4 a 14 4 + 5 a 15 5 + 6 a 16 6 + 7 a 17 7 + } +} + +#------------------------------------------------------------------------- +# +do_execsql_test 5.0 { + CREATE TABLE x1(a INTEGER PRIMARY KEY, b, c); + CREATE INDEX x1c ON x1(b, c); + INSERT INTO x1 VALUES(1, 'a', 1); + INSERT INTO x1 VALUES(2, 'a', 2); + INSERT INTO x1 VALUES(3, 'a', 3); +} + +do_execsql_test 5.1.1 { + UPDATE x1 SET c=c+1 WHERE b='a'; +} + +do_execsql_test 5.1.2 { + SELECT * FROM x1; +} {1 a 2 2 a 3 3 a 4} + +do_test 5.2 { + catch { array unset A } + db eval { EXPLAIN UPDATE x1 SET c=c+1 WHERE b='a' } { incr A($opcode) } + set A(NotExists) +} {1} + +#------------------------------------------------------------------------- +do_execsql_test 6.0 { + CREATE TABLE d1(a,b); + CREATE INDEX d1b ON d1(a); + CREATE INDEX d1c ON d1(b); + INSERT INTO d1 VALUES(1,2); +} + +do_execsql_test 6.1 { + UPDATE d1 SET a = a+2 WHERE a>0 OR b>0; +} + +do_execsql_test 6.2 { + SELECT * FROM d1; +} {3 2} + +# 2019-01-22 Bug in UPDATE OR REPLACE discovered by the +# Matt Denton's LPM fuzzer +# +do_execsql_test 7.100 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(x,y); + CREATE UNIQUE INDEX t1x1 ON t1(x) WHERE x IS NOT NULL; + INSERT INTO t1(x) VALUES(NULL),(NULL); + CREATE INDEX t1x2 ON t1(y); + SELECT quote(x), quote(y), '|' FROM t1; +} {NULL NULL | NULL NULL |} +do_execsql_test 7.110 { + UPDATE OR REPLACE t1 SET x=1; + SELECT quote(x), quote(y), '|' FROM t1; +} {1 NULL |} + +finish_test diff --git a/test/upfrom1.tcl b/test/upfrom1.tcl new file mode 100644 index 0000000000..22fc68a31c --- /dev/null +++ b/test/upfrom1.tcl @@ -0,0 +1,115 @@ +# 2020 April 22 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +source [file join [file dirname $argv0] pg_common.tcl] + +#========================================================================= + +start_test upfrom1 "2020 April 22" + +foreach {tn wo} { + 1 "WITHOUT ROWID" + 2 "" +} { +eval [string map [list %TN% $tn %WITHOUT_ROWID% $wo] { +execsql_test 1.%TN%.0 { + DROP TABLE IF EXISTS t2; + CREATE TABLE t2(a INTEGER PRIMARY KEY, b INTEGER, c INTEGER) %WITHOUT_ROWID%; + INSERT INTO t2 VALUES(1, 2, 3); + INSERT INTO t2 VALUES(4, 5, 6); + INSERT INTO t2 VALUES(7, 8, 9); + + DROP TABLE IF EXISTS chng; + CREATE TABLE chng(a INTEGER, b INTEGER, c INTEGER); + INSERT INTO chng VALUES(1, 100, 1000); + INSERT INTO chng VALUES(7, 700, 7000); +} + +execsql_test 1.%TN%.1 { + SELECT * FROM t2; +} + +execsql_test 1.%TN%.2 { + UPDATE t2 SET b = chng.b, c = chng.c FROM chng WHERE chng.a = t2.a; + SELECT * FROM t2 ORDER BY a; +} + +execsql_test 1.%TN%.3 { + DELETE FROM t2; + INSERT INTO t2 VALUES(1, 2, 3); + INSERT INTO t2 VALUES(4, 5, 6); + INSERT INTO t2 VALUES(7, 8, 9); +} + +execsql_test 1.%TN%.4 { + UPDATE t2 SET (b, c) = (SELECT b, c FROM chng WHERE a=t2.a) + WHERE a IN (SELECT a FROM chng); + SELECT * FROM t2 ORDER BY a; +} + +execsql_test 1.%TN%.5 { + DROP TABLE IF EXISTS t3; + CREATE TABLE t3(a INTEGER PRIMARY KEY, b INTEGER, c TEXT) %WITHOUT_ROWID%; + INSERT INTO t3 VALUES(1, 1, 'one'); + INSERT INTO t3 VALUES(2, 2, 'two'); + INSERT INTO t3 VALUES(3, 3, 'three'); + + DROP TABLE IF EXISTS t4; + CREATE TABLE t4(x TEXT); + INSERT INTO t4 VALUES('five'); + + SELECT * FROM t3 ORDER BY a; +} + +execsql_test 1.%TN%.6 { + UPDATE t3 SET c=x FROM t4; + SELECT * FROM t3 ORDER BY a; +} +}]} + +execsql_test 2.1 { + DROP TABLE IF EXISTS t5; + DROP TABLE IF EXISTS m1; + DROP TABLE IF EXISTS m2; + CREATE TABLE t5(a INTEGER PRIMARY KEY, b TEXT, c TEXT); + CREATE TABLE m1(x INTEGER PRIMARY KEY, y TEXT); + CREATE TABLE m2(u INTEGER PRIMARY KEY, v TEXT); + + INSERT INTO t5 VALUES(1, 'one', 'ONE'); + INSERT INTO t5 VALUES(2, 'two', 'TWO'); + INSERT INTO t5 VALUES(3, 'three', 'THREE'); + INSERT INTO t5 VALUES(4, 'four', 'FOUR'); + + INSERT INTO m1 VALUES(1, 'i'); + INSERT INTO m1 VALUES(2, 'ii'); + INSERT INTO m1 VALUES(3, 'iii'); + + INSERT INTO m2 VALUES(1, 'I'); + INSERT INTO m2 VALUES(3, 'II'); + INSERT INTO m2 VALUES(4, 'III'); +} + +execsql_test 2.2 { + UPDATE t5 SET b=y, c=v FROM m1 LEFT JOIN m2 ON (x=u) WHERE x=a; + SELECT * FROM t5 ORDER BY a; +} + +errorsql_test 2.3.1 { + UPDATE t5 SET b=1 FROM t5; +} +errorsql_test 2.3.2 { + UPDATE t5 AS apples SET b=1 FROM t5 AS apples; +} + + +finish_test + diff --git a/test/upfrom1.test b/test/upfrom1.test new file mode 100644 index 0000000000..204f3512e5 --- /dev/null +++ b/test/upfrom1.test @@ -0,0 +1,210 @@ +# 2020 April 22 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. +# + +#################################################### +# DO NOT EDIT! THIS FILE IS AUTOMATICALLY GENERATED! +#################################################### + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix upfrom1 + +do_execsql_test 1.1.0 { + DROP TABLE IF EXISTS t2; + CREATE TABLE t2(a INTEGER PRIMARY KEY, b INTEGER, c INTEGER) WITHOUT ROWID; + INSERT INTO t2 VALUES(1, 2, 3); + INSERT INTO t2 VALUES(4, 5, 6); + INSERT INTO t2 VALUES(7, 8, 9); + + DROP TABLE IF EXISTS chng; + CREATE TABLE chng(a INTEGER, b INTEGER, c INTEGER); + INSERT INTO chng VALUES(1, 100, 1000); + INSERT INTO chng VALUES(7, 700, 7000); +} {} + +do_execsql_test 1.1.1 { + SELECT * FROM t2; +} {1 2 3 4 5 6 7 8 9} + +do_execsql_test 1.1.2 { + UPDATE t2 SET b = chng.b, c = chng.c FROM chng WHERE chng.a = t2.a; + SELECT * FROM t2 ORDER BY a; +} {1 100 1000 4 5 6 7 700 7000} + +do_execsql_test 1.1.3 { + DELETE FROM t2; + INSERT INTO t2 VALUES(1, 2, 3); + INSERT INTO t2 VALUES(4, 5, 6); + INSERT INTO t2 VALUES(7, 8, 9); +} {} + +do_execsql_test 1.1.4 { + UPDATE t2 SET (b, c) = (SELECT b, c FROM chng WHERE a=t2.a) + WHERE a IN (SELECT a FROM chng); + SELECT * FROM t2 ORDER BY a; +} {1 100 1000 4 5 6 7 700 7000} + +do_execsql_test 1.1.5 { + DROP TABLE IF EXISTS t3; + CREATE TABLE t3(a INTEGER PRIMARY KEY, b INTEGER, c TEXT) WITHOUT ROWID; + INSERT INTO t3 VALUES(1, 1, 'one'); + INSERT INTO t3 VALUES(2, 2, 'two'); + INSERT INTO t3 VALUES(3, 3, 'three'); + + DROP TABLE IF EXISTS t4; + CREATE TABLE t4(x TEXT); + INSERT INTO t4 VALUES('five'); + + SELECT * FROM t3 ORDER BY a; +} {1 1 one 2 2 two 3 3 three} + +do_execsql_test 1.1.6 { + UPDATE t3 SET c=x FROM t4; + SELECT * FROM t3 ORDER BY a; +} {1 1 five 2 2 five 3 3 five} + +do_execsql_test 1.2.0 { + DROP TABLE IF EXISTS t2; + CREATE TABLE t2(a INTEGER PRIMARY KEY, b INTEGER, c INTEGER) ; + INSERT INTO t2 VALUES(1, 2, 3); + INSERT INTO t2 VALUES(4, 5, 6); + INSERT INTO t2 VALUES(7, 8, 9); + + DROP TABLE IF EXISTS chng; + CREATE TABLE chng(a INTEGER, b INTEGER, c INTEGER); + INSERT INTO chng VALUES(1, 100, 1000); + INSERT INTO chng VALUES(7, 700, 7000); +} {} + +do_execsql_test 1.2.1 { + SELECT * FROM t2; +} {1 2 3 4 5 6 7 8 9} + +do_execsql_test 1.2.2 { + UPDATE t2 SET b = chng.b, c = chng.c FROM chng WHERE chng.a = t2.a; + SELECT * FROM t2 ORDER BY a; +} {1 100 1000 4 5 6 7 700 7000} + +do_execsql_test 1.2.3 { + DELETE FROM t2; + INSERT INTO t2 VALUES(1, 2, 3); + INSERT INTO t2 VALUES(4, 5, 6); + INSERT INTO t2 VALUES(7, 8, 9); +} {} + +do_execsql_test 1.2.4 { + UPDATE t2 SET (b, c) = (SELECT b, c FROM chng WHERE a=t2.a) + WHERE a IN (SELECT a FROM chng); + SELECT * FROM t2 ORDER BY a; +} {1 100 1000 4 5 6 7 700 7000} + +do_execsql_test 1.2.5 { + DROP TABLE IF EXISTS t3; + CREATE TABLE t3(a INTEGER PRIMARY KEY, b INTEGER, c TEXT) ; + INSERT INTO t3 VALUES(1, 1, 'one'); + INSERT INTO t3 VALUES(2, 2, 'two'); + INSERT INTO t3 VALUES(3, 3, 'three'); + + DROP TABLE IF EXISTS t4; + CREATE TABLE t4(x TEXT); + INSERT INTO t4 VALUES('five'); + + SELECT * FROM t3 ORDER BY a; +} {1 1 one 2 2 two 3 3 three} + +do_execsql_test 1.2.6 { + UPDATE t3 SET c=x FROM t4; + SELECT * FROM t3 ORDER BY a; +} {1 1 five 2 2 five 3 3 five} + +do_execsql_test 2.1 { + DROP TABLE IF EXISTS t5; + DROP TABLE IF EXISTS m1; + DROP TABLE IF EXISTS m2; + CREATE TABLE t5(a INTEGER PRIMARY KEY, b TEXT, c TEXT); + CREATE TABLE m1(x INTEGER PRIMARY KEY, y TEXT); + CREATE TABLE m2(u INTEGER PRIMARY KEY, v TEXT); + + INSERT INTO t5 VALUES(1, 'one', 'ONE'); + INSERT INTO t5 VALUES(2, 'two', 'TWO'); + INSERT INTO t5 VALUES(3, 'three', 'THREE'); + INSERT INTO t5 VALUES(4, 'four', 'FOUR'); + + INSERT INTO m1 VALUES(1, 'i'); + INSERT INTO m1 VALUES(2, 'ii'); + INSERT INTO m1 VALUES(3, 'iii'); + + INSERT INTO m2 VALUES(1, 'I'); + INSERT INTO m2 VALUES(3, 'II'); + INSERT INTO m2 VALUES(4, 'III'); +} {} + +do_execsql_test 2.2 { + UPDATE t5 SET b=y, c=v FROM m1 LEFT JOIN m2 ON (x=u) WHERE x=a; + SELECT * FROM t5 ORDER BY a; +} {1 i I 2 ii {} 3 iii II 4 four FOUR} + +# PG says ERROR: table name "t5" specified more than once +do_test 2.3.1 { catch { execsql { + UPDATE t5 SET b=1 FROM t5; +} } } 1 + +# PG says ERROR: table name "apples" specified more than once +do_test 2.3.2 { catch { execsql { + UPDATE t5 AS apples SET b=1 FROM t5 AS apples; +} } } 1 + +# Problem found by OSSFuzz on 2020-07-20 +# https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=24282 +# +reset_db +do_execsql_test 3.1 { + CREATE TABLE t0(a); + CREATE TABLE t1(b); + UPDATE t1 SET b=sum(a) FROM t0; + SELECT * FROM t0, t1; +} {} + +# Problem described by forum post https://sqlite.org/forum/forumpost/a274248080 +# +reset_db +do_execsql_test 4.1 { + CREATE TABLE t1(x INT); INSERT INTO t1 VALUES(1); + CREATE TABLE t2(y INT); INSERT INTO t2 VALUES(2); + WITH t1 AS (SELECT y+100 AS x FROM t2) + UPDATE t1 SET x=(SELECT x FROM t1); + SELECT x, y FROM t1, t2; +} {102 2} +do_execsql_test 4.2 { + WITH t1 AS (SELECT y+100 AS x FROM t2) + UPDATE t1 SET x=x+y FROM t2; + SELECT x, y FROM t1, t2; +} {104 2} + +# 2021-05-20 +# Forum https://sqlite.org/forum/forumpost/339f487de5 by Yu Liang +# A bad assert() +# +reset_db +do_execsql_test 5.1 { + CREATE TABLE t1(a); + INSERT INTO t1(a) VALUES(5); + CREATE VIEW t2 AS SELECT a FROM t1 UNION ALL SELECT a FROM t1; + CREATE TABLE t3(b,c); + INSERT INTO t3(b,c) VALUES(1,2); + UPDATE t3 SET (c,b) = (SELECT 3,4) FROM t1, t2; + SELECT * FROM t3; +} {4 3} + + +finish_test diff --git a/test/upfrom2.test b/test/upfrom2.test new file mode 100644 index 0000000000..600fa05e4b --- /dev/null +++ b/test/upfrom2.test @@ -0,0 +1,405 @@ +# 2020 April 29 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix upfrom2 + +# Test cases: +# +# 1.*: Test that triggers are fired correctly for UPDATE FROM statements, +# and only once for each row. Except for INSTEAD OF triggers on +# views - these are fired once for each row returned by the join, +# including duplicates. +# +# 2.*: Test adding ORDER BY and LIMIT clauses with UPDATE FROM statements. +# +# 5.*: Test that specifying the target table name or alias in the FROM +# clause of an UPDATE statement is an error. +# + +foreach {tn wo} { + 1 "" + 2 "WITHOUT ROWID" +} { + reset_db + + eval [string map [list %WO% $wo %TN% $tn] { + do_execsql_test 1.%TN%.0 { + CREATE TABLE log(t TEXT); + CREATE TABLE t1(x PRIMARY KEY, y, z UNIQUE) %WO%; + CREATE INDEX t1y ON t1(y); + + INSERT INTO t1 VALUES(1, 'i', 'one'); + INSERT INTO t1 VALUES(2, 'ii', 'two'); + INSERT INTO t1 VALUES(3, 'iii', 'three'); + INSERT INTO t1 VALUES(4, 'iv', 'four'); + + CREATE TRIGGER tr1 BEFORE UPDATE ON t1 BEGIN + INSERT INTO log VALUES(old.z || '->' || new.z); + END; + CREATE TRIGGER tr2 AFTER UPDATE ON t1 BEGIN + INSERT INTO log VALUES(old.y || '->' || new.y); + END; + } + + do_execsql_test 1.%TN%.1 { + WITH data(k, v) AS ( + VALUES(3, 'thirty'), (1, 'ten') + ) + UPDATE t1 SET z=v FROM data WHERE x=k; + + SELECT * FROM t1; + SELECT * FROM log; + } { + 1 i ten 2 ii two 3 iii thirty 4 iv four + one->ten i->i + three->thirty iii->iii + } + + do_execsql_test 1.%TN%.2 { + CREATE TABLE t2(a, b); + CREATE TABLE t3(k, v); + + INSERT INTO t3 VALUES(5, 'v'); + INSERT INTO t3 VALUES(12, 'xii'); + + INSERT INTO t2 VALUES(2, 12); + INSERT INTO t2 VALUES(3, 5); + + DELETE FROM log; + UPDATE t1 SET y=v FROM t2, t3 WHERE t1.x=t2.a AND t3.k=t2.b; + + SELECT * FROM t1; + SELECT * FROM log; + } { + 1 i ten 2 xii two 3 v thirty 4 iv four + two->two ii->xii + thirty->thirty iii->v + } + + do_execsql_test 1.%TN%.3 { + DELETE FROM log; + WITH data(k, v) AS ( + VALUES(1, 'seven'), (1, 'eight'), (2, 'eleven'), (2, 'twelve') + ) + UPDATE t1 SET z=v FROM data WHERE x=k; + + SELECT * FROM t1; + SELECT * FROM log; + } { + 1 i eight 2 xii twelve 3 v thirty 4 iv four + ten->eight i->i + two->twelve xii->xii + } + + do_test 1.%TN%.4 { db changes } {2} + + do_execsql_test 1.%TN%.5 { + CREATE VIEW v1 AS SELECT * FROM t1; + CREATE TRIGGER v1tr INSTEAD OF UPDATE ON v1 BEGIN + UPDATE t1 SET y=new.y, z=new.z WHERE x=new.x; + END; + + DELETE FROM log; + WITH data(k, v) AS ( + VALUES(3, 'thirteen'), (3, 'fourteen'), (4, 'fifteen'), (4, 'sixteen') + ) + UPDATE v1 SET z=v FROM data WHERE x=k; + } + + do_execsql_test 1.%TN%.6 { + SELECT * FROM v1; + SELECT * FROM log; + } { + 1 i eight 2 xii twelve 3 v fourteen 4 iv sixteen + thirty->thirteen v->v + thirteen->fourteen v->v + four->fifteen iv->iv + fifteen->sixteen iv->iv + } + + #-------------------------------------------------------------- + + do_execsql_test 1.%TN%.7 { + CREATE TABLE o1(w, x, y, z UNIQUE, PRIMARY KEY(w, x)) %WO%; + CREATE INDEX o1y ON t1(y); + + INSERT INTO o1 VALUES(0, 0, 'i', 'one'); + INSERT INTO o1 VALUES(0, 1, 'ii', 'two'); + INSERT INTO o1 VALUES(1, 0, 'iii', 'three'); + INSERT INTO o1 VALUES(1, 1, 'iv', 'four'); + + CREATE TRIGGER tro1 BEFORE UPDATE ON o1 BEGIN + INSERT INTO log VALUES(old.z || '->' || new.z); + END; + CREATE TRIGGER tro2 AFTER UPDATE ON o1 BEGIN + INSERT INTO log VALUES(old.y || '->' || new.y); + END; + } + + do_execsql_test 1.%TN%.8 { + DELETE FROM log; + WITH data(k, v) AS ( + VALUES(3, 'thirty'), (1, 'ten') + ) + UPDATE o1 SET z=v FROM data WHERE (1+x+w*2)=k; + + SELECT * FROM o1; + SELECT * FROM log; + } { + 0 0 i ten 0 1 ii two 1 0 iii thirty 1 1 iv four + one->ten i->i + three->thirty iii->iii + } + + do_execsql_test 1.%TN%.9 { + DELETE FROM log; + UPDATE o1 SET y=v FROM t2, t3 WHERE (1+o1.w*2+o1.x)=t2.a AND t3.k=t2.b; + + SELECT * FROM o1; + SELECT * FROM log; + } { + 0 0 i ten 0 1 xii two 1 0 v thirty 1 1 iv four + two->two ii->xii + thirty->thirty iii->v + } + + do_execsql_test 1.%TN%.10 { + DELETE FROM log; + WITH data(k, v) AS ( + VALUES(1, 'seven'), (1, 'eight'), (2, 'eleven'), (2, 'twelve') + ) + UPDATE o1 SET z=v FROM data WHERE (1+w*2+x)=k; + + SELECT * FROM o1; + SELECT * FROM log; + } { + 0 0 i eight 0 1 xii twelve 1 0 v thirty 1 1 iv four + ten->eight i->i + two->twelve xii->xii + } + + do_test 1.%TN%.11 { db changes } {2} + + do_execsql_test 1.%TN%.12 { + CREATE VIEW w1 AS SELECT * FROM o1; + CREATE TRIGGER w1tr INSTEAD OF UPDATE ON w1 BEGIN + UPDATE o1 SET y=new.y, z=new.z WHERE w=new.w AND x=new.x; + END; + + DELETE FROM log; + WITH data(k, v) AS ( + VALUES(3, 'thirteen'), (3, 'fourteen'), (4, 'fifteen'), (4, 'sixteen') + ) + UPDATE w1 SET z=v FROM data WHERE (1+w*2+x)=k; + } + + do_execsql_test 1.%TN%.13 { + SELECT * FROM w1; + SELECT * FROM log; + } { + 0 0 i eight 0 1 xii twelve 1 0 v fourteen 1 1 iv sixteen + thirty->thirteen v->v + thirteen->fourteen v->v + four->fifteen iv->iv + fifteen->sixteen iv->iv + } + +}] +} + +ifcapable update_delete_limit { +foreach {tn wo} { + 1 "" + 2 "WITHOUT ROWID" +} { + reset_db + +eval [string map [list %WO% $wo %TN% $tn] { + do_execsql_test 2.%TN%.1 { + CREATE TABLE x1(a INTEGER PRIMARY KEY, b) %WO%; + INSERT INTO x1 VALUES + (1, 'one'), (2, 'two'), (3, 'three'), (4, 'four'), + (5, 'five'), (6, 'six'), (7, 'seven'), (8, 'eight'); + } + + do_execsql_test 2.%TN%.2 { + CREATE TABLE data1(x, y); + INSERT INTO data1 VALUES + (1, 'eleven'), (1, 'twenty-one'), (2, 'twelve'), (2, 'twenty-two'), + (3, 'thirteen'), (3, 'twenty-three'), (4, 'fourteen'), (4, 'twenty-four'); + } + + do_execsql_test 2.%TN%.3 { + UPDATE x1 SET b=y FROM data1 WHERE a=x ORDER BY a LIMIT 3; + SELECT * FROM x1; + } { + 1 eleven 2 twelve 3 thirteen 4 four 5 five 6 six 7 seven 8 eight + } + + do_execsql_test 2.%TN%.4 { + UPDATE x1 SET b=b||y FROM data1 WHERE a=x ORDER BY b LIMIT 3; + SELECT * FROM x1; + } { + 1 eleveneleven 2 twelve 3 thirteenthirteen 4 fourfourteen + 5 five 6 six 7 seven 8 eight + } + + do_catchsql_test 2.%TN%.5 { + UPDATE x1 SET b=b||b ORDER BY b; + } {1 {ORDER BY without LIMIT on UPDATE}} + do_catchsql_test 2.%TN%.6 { + UPDATE x1 SET b=b||y FROM data1 WHERE a=x ORDER BY b; + } {1 {ORDER BY without LIMIT on UPDATE}} + + #----------------------------------------------------------------------- + + do_execsql_test 2.%TN%.6 { + DROP TABLE x1; + CREATE TABLE x1(u, v, b, PRIMARY KEY(u, v)) %WO%; + INSERT INTO x1 VALUES + (0, 1, 'one'), (1, 0, 'two'), (1, 1, 'three'), (2, 0, 'four'), + (2, 1, 'five'), (3, 0, 'six'), (3, 1, 'seven'), (4, 0, 'eight'); + } + + do_execsql_test 2.%TN%.7 { + UPDATE x1 SET b=y FROM data1 WHERE (u*2+v)=x ORDER BY u, v LIMIT 3; + SELECT * FROM x1; + } { + 0 1 eleven 1 0 twelve 1 1 thirteen 2 0 four + 2 1 five 3 0 six 3 1 seven 4 0 eight + } + + do_execsql_test 2.%TN%.8 { + UPDATE x1 SET b=b||y FROM data1 WHERE (u*2+v)=x ORDER BY b LIMIT 3; + SELECT * FROM x1; + } { + 0 1 eleveneleven 1 0 twelve 1 1 thirteenthirteen 2 0 fourfourteen + 2 1 five 3 0 six 3 1 seven 4 0 eight + } + + +}] +}} + +reset_db +do_execsql_test 3.0 { + CREATE TABLE data(x, y, z); + CREATE VIEW t1 AS SELECT * FROM data; + CREATE TRIGGER t1_insert INSTEAD OF INSERT ON t1 BEGIN + INSERT INTO data VALUES(new.x, new.y, new.z); + END; + CREATE TRIGGER t1_update INSTEAD OF UPDATE ON t1 BEGIN + INSERT INTO log VALUES(old.z || '->' || new.z); + END; + + CREATE TABLE log(t TEXT); + + INSERT INTO t1 VALUES(1, 'i', 'one'); + INSERT INTO t1 VALUES(2, 'ii', 'two'); + INSERT INTO t1 VALUES(3, 'iii', 'three'); + INSERT INTO t1 VALUES(4, 'iv', 'four'); +} + +do_execsql_test 3.1 { + WITH input(k, v) AS ( + VALUES(3, 'thirty'), (1, 'ten') + ) + UPDATE t1 SET z=v FROM input WHERE x=k; +} + +foreach {tn sql} { + 2 { + CREATE TABLE x1(a INT PRIMARY KEY, b, c) WITHOUT ROWID; + } + 1 { + CREATE TABLE x1(a INTEGER PRIMARY KEY, b, c); + } + 3 { + CREATE TABLE x1(a INT PRIMARY KEY, b, c); + } +} { + + reset_db + execsql $sql + + do_execsql_test 4.$tn.0 { + INSERT INTO x1 VALUES(1, 1, 1); + INSERT INTO x1 VALUES(2, 2, 2); + INSERT INTO x1 VALUES(3, 3, 3); + INSERT INTO x1 VALUES(4, 4, 4); + INSERT INTO x1 VALUES(5, 5, 5); + CREATE TABLE map(o, t); + INSERT INTO map VALUES(3, 30), (4, 40), (1, 10); + } + + do_execsql_test 4.$tn.1 { + UPDATE x1 SET a=t FROM map WHERE a=o; + SELECT * FROM x1 ORDER BY a; + } {2 2 2 5 5 5 10 1 1 30 3 3 40 4 4} +} + +reset_db +do_execsql_test 5.0 { + CREATE TABLE x1(a, b, c); + CREATE TABLE x2(a, b, c); +} + +foreach {tn update nm} { + 1 "UPDATE x1 SET a=5 FROM x1" x1 + 2 "UPDATE x1 AS grapes SET a=5 FROM x1 AS grapes" grapes + 3 "UPDATE x1 SET a=5 FROM x2, x1" x1 + 4 "UPDATE x1 AS grapes SET a=5 FROM x2, x1 AS grapes" grapes +} { + do_catchsql_test 5.$tn $update \ + "1 {target object/alias may not appear in FROM clause: $nm}" +} + +#-------------------------------------------------------------------------- +reset_db +do_execsql_test 6.0 { + CREATE TABLE t1(a); +} + +do_execsql_test 6.1 { + UPDATE t1 SET a = 1 FROM ( + SELECT * FROM t1 + ) +} {} +do_execsql_test 6.2 { + UPDATE t1 SET a = 1 FROM ( + SELECT * FROM t1 UNION ALL SELECT * FROM t1 + ) +} {} + +# 2022-03-21 +# https://sqlite.org/forum/forumpost/929168fdd6 +# +reset_db +do_execsql_test 7.0 { + CREATE TABLE t1(a); + INSERT INTO t1(a) VALUES(11),(22),(33),(44),(55); + CREATE VIEW t2(b,c) AS SELECT a, COUNT(*) OVER () FROM t1; + CREATE TABLE t3(x,y); + CREATE TRIGGER t2r1 INSTEAD OF UPDATE ON t2 BEGIN + INSERT INTO t3(x,y) VALUES(new.b,new.c); + END; + SELECT * FROM t2; +} {11 5 22 5 33 5 44 5 55 5} +do_execsql_test 7.1 { + UPDATE t2 SET c=t1.a FROM t1 WHERE t2.b=t1.a; + SELECT * FROM t3; +} {11 11 22 22 33 33 44 44 55 55} + + +finish_test diff --git a/test/upfrom3.test b/test/upfrom3.test new file mode 100644 index 0000000000..9936716b51 --- /dev/null +++ b/test/upfrom3.test @@ -0,0 +1,261 @@ +# 2020 July 14 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix upfrom3 + +# Test plan: +# +# 1.*: Test UPDATE ... FROM statements that modify IPK fields. And that +# modify "INTEGER PRIMARY KEY" fields on WITHOUT ROWID tables. +# +# 2.*: Test UPDATE ... FROM statements that modify PK fields of WITHOUT +# ROWID tables. +# +# 3.*: Test that UPDATE ... FROM statements are not confused if there +# are multiple tables of the same name in attached databases. +# +# 4.*: Tests for UPDATE ... FROM statements and foreign keys. +# + +foreach {tn wo} { + 1 "" + 2 "WITHOUT ROWID" +} { + reset_db + eval [string map [list %WO% $wo %TN% $tn] { + + do_execsql_test 1.%TN%.0 { + CREATE TABLE log(t TEXT); + CREATE TABLE t1(x INTEGER PRIMARY KEY, y, z UNIQUE) %WO%; + CREATE INDEX t1y ON t1(y); + + INSERT INTO t1 VALUES(1, 'i', 'one'); + INSERT INTO t1 VALUES(2, 'ii', 'two'); + INSERT INTO t1 VALUES(3, 'iii', 'three'); + INSERT INTO t1 VALUES(4, 'iv', 'four'); + } + + do_execsql_test 1.%TN%.1 { + CREATE TABLE x1(o, n); + INSERT INTO x1 VALUES(1, 11); + INSERT INTO x1 VALUES(2, 12); + INSERT INTO x1 VALUES(3, 13); + INSERT INTO x1 VALUES(4, 14); + UPDATE t1 SET x=n FROM x1 WHERE x=o; + SELECT x, y, z FROM t1 ORDER BY 1; + } { + 11 i one + 12 ii two + 13 iii three + 14 iv four + } + + do_test 1.%TN%.2 { db changes } 4 + + do_execsql_test 1.%TN%.3 { + INSERT INTO x1 VALUES(11, 21); + INSERT INTO x1 VALUES(12, 22); + INSERT INTO x1 VALUES(13, 23); + INSERT INTO x1 VALUES(14, 24); + + INSERT INTO x1 VALUES(21, 31); + INSERT INTO x1 VALUES(22, 32); + INSERT INTO x1 VALUES(23, 33); + INSERT INTO x1 VALUES(24, 34); + UPDATE t1 SET x=n FROM x1 WHERE x=o; + SELECT x, y, z FROM t1 ORDER BY 1; + } { + 21 i one + 22 ii two + 23 iii three + 24 iv four + } + + do_execsql_test 1.%TN%.4 { + UPDATE t1 SET x=n FROM x1 WHERE x=o; + SELECT x, y, z FROM t1 ORDER BY 1; + } { + 31 i one + 32 ii two + 33 iii three + 34 iv four + } + + do_execsql_test 1.%TN%.5 { + INSERT INTO x1 VALUES(31, 32); + INSERT INTO x1 VALUES(33, 34); + UPDATE OR REPLACE t1 SET x=n FROM x1 WHERE x=o; + SELECT x, y, z FROM t1 ORDER BY 1; + } { + 32 i one + 34 iii three + } + + do_execsql_test 1.%TN%.6 { + INSERT INTO t1 VALUES(33, 'ii', 'two'); + INSERT INTO t1 VALUES(35, 'iv', 'four'); + } + + do_execsql_test 1.%TN%.7 { + CREATE TABLE x2(o, n, zz); + INSERT INTO x2 VALUES(32, 41, 'four'); + INSERT INTO x2 VALUES(33, 42, 'three'); + UPDATE OR IGNORE t1 SET x=n, z=zz FROM x2 WHERE x=o; + SELECT x, y, z FROM t1 ORDER BY 1; + } { + 32 i one + 33 ii two + 34 iii three + 35 iv four + } + + do_execsql_test 1.%TN%.8 { + UPDATE OR REPLACE t1 SET x=n, z=zz FROM x2 WHERE x=o; + SELECT x, y, z FROM t1 ORDER BY 1; + } { + 41 i four + 42 ii three + } + + }] +} + +do_execsql_test 2.1.1 { + CREATE TABLE u1(a, b, c, PRIMARY KEY(b, c)) WITHOUT ROWID; + INSERT INTO u1 VALUES(0, 0, 0); + INSERT INTO u1 VALUES(1, 0, 1); + INSERT INTO u1 VALUES(2, 1, 0); + INSERT INTO u1 VALUES(3, 1, 1); +} + +do_execsql_test 2.1.2 { + CREATE TABLE map(f, t); + INSERT INTO map VALUES(0, 10); + INSERT INTO map VALUES(1, 11); + UPDATE u1 SET c=t FROM map WHERE c=f; + SELECT * FROM u1 ORDER BY a; +} { + 0 0 10 + 1 0 11 + 2 1 10 + 3 1 11 +} + +do_execsql_test 2.1.3 { + UPDATE u1 SET b=t FROM map WHERE b=f; + SELECT * FROM u1 ORDER BY a; +} { + 0 10 10 + 1 10 11 + 2 11 10 + 3 11 11 +} + +do_execsql_test 2.1.4 { + CREATE TABLE map2(o1, o2, n1, n2); + INSERT INTO map2 VALUES + (10, 10, 50, 50), (10, 11, 50, 60), + (11, 10, 60, 50), (11, 11, 60, 60); + UPDATE u1 SET b=n1, c=n2 FROM map2 WHERE b=o1 AND c=o2; + SELECT * FROM u1 ORDER BY a; +} { + 0 50 50 + 1 50 60 + 2 60 50 + 3 60 60 +} + +#------------------------------------------------------------------------- +foreach {tn wo} { + 1 "" + 2 "WITHOUT ROWID" +} { + reset_db + forcedelete test.db2 + eval [string map [list %WO% $wo %TN% $tn] { + do_execsql_test 3.$tn.1 { + CREATE TABLE g1(a, b, c, PRIMARY KEY(a, b)) %WO%; + INSERT INTO g1 VALUES(1, 1, 1); + + ATTACH 'test.db2' AS aux; + CREATE TABLE aux.g1(a, b, c, PRIMARY KEY(a, b)) %WO%; + INSERT INTO aux.g1 VALUES(10, 1, 10); + INSERT INTO aux.g1 VALUES(20, 2, 20); + INSERT INTO aux.g1 VALUES(30, 3, 30); + } + + do_execsql_test 3.$tn.2 { + UPDATE aux.g1 SET c=101 FROM main.g1; + } + do_execsql_test 3.$tn.3 { + SELECT * FROM aux.g1; + } {10 1 101 20 2 101 30 3 101} + + do_execsql_test 3.$tn.4 { + UPDATE g1 SET c=101 FROM g1 AS g2; + } + do_execsql_test 3.$tn.5 { + SELECT * FROM g1; + } {1 1 101} + }] +} + +#------------------------------------------------------------------------- +reset_db +foreach {tn wo} { + 1 "" + 2 "WITHOUT ROWID" +} { + reset_db + forcedelete test.db2 + eval [string map [list %WO% $wo %TN% $tn] { + + do_execsql_test 4.$tn.1 { + CREATE TABLE p1(a INTEGER PRIMARY KEY, b) %WO%; + CREATE TABLE c1(x PRIMARY KEY, y REFERENCES p1 ON UPDATE CASCADE) %WO%; + PRAGMA foreign_keys = 1; + + INSERT INTO p1 VALUES(1, 'one'); + INSERT INTO p1 VALUES(11, 'eleven'); + INSERT INTO p1 VALUES(111, 'eleventyone'); + + INSERT INTO c1 VALUES('a', 1); + INSERT INTO c1 VALUES('b', 11); + INSERT INTO c1 VALUES('c', 111); + } + + do_execsql_test 4.$tn.2 { + CREATE TABLE map(f, t); + INSERT INTO map VALUES('a', 111); + INSERT INTO map VALUES('c', 112); + } + + do_catchsql_test 4.$tn.3 { + UPDATE c1 SET y=t FROM map WHERE x=f; + } {1 {FOREIGN KEY constraint failed}} + + do_execsql_test 4.$tn.4 { + INSERT INTO map VALUES('eleven', 12); + INSERT INTO map VALUES('eleventyone', 112); + UPDATE p1 SET a=t FROM map WHERE b=f; + } + + do_execsql_test 4.$tn.5 { + SELECT * FROM c1 + } {a 1 b 12 c 112} + + }] +} + +finish_test diff --git a/test/upfrom4.test b/test/upfrom4.test new file mode 100644 index 0000000000..2228236280 --- /dev/null +++ b/test/upfrom4.test @@ -0,0 +1,156 @@ +# 2022-05-24 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix upfrom4 + +do_execsql_test 100 { + DROP TABLE IF EXISTS t5; + DROP TABLE IF EXISTS m1; + DROP TABLE IF EXISTS m2; + CREATE TABLE t5(a INTEGER PRIMARY KEY, b TEXT, c TEXT); + CREATE TABLE m1(x INTEGER PRIMARY KEY, y TEXT); + CREATE TABLE m2(u INTEGER PRIMARY KEY, v TEXT); + + INSERT INTO t5 VALUES(1, 'one', 'ONE'); + INSERT INTO t5 VALUES(2, 'two', 'TWO'); + INSERT INTO t5 VALUES(3, 'three', 'THREE'); + INSERT INTO t5 VALUES(4, 'four', 'FOUR'); + + INSERT INTO m1 VALUES(1, 'i'); + INSERT INTO m1 VALUES(2, 'ii'); + INSERT INTO m1 VALUES(3, 'iii'); + + INSERT INTO m2 VALUES(1, 'I'); + INSERT INTO m2 VALUES(3, 'II'); + INSERT INTO m2 VALUES(4, 'III'); + SELECT * FROM t5; +} {1 one ONE 2 two TWO 3 three THREE 4 four FOUR} + +do_execsql_test 110 { + BEGIN; + UPDATE t5 SET b=y, c=v FROM m1 LEFT JOIN m2 ON (x=u) WHERE x=a; + SELECT * FROM t5 ORDER BY a; + ROLLBACK; +} {1 i I 2 ii {} 3 iii II 4 four FOUR} + +do_execsql_test 120 { + BEGIN; + UPDATE t5 SET b=y, c=v FROM m2 RIGHT JOIN m1 ON (x=u) WHERE x=a; + SELECT * FROM t5 ORDER BY a; + ROLLBACK; +} {1 i I 2 ii {} 3 iii II 4 four FOUR} + + +reset_db +db null - +do_execsql_test 200 { + CREATE TABLE t1(a INT PRIMARY KEY, b INT, c INT); + INSERT INTO t1(a) VALUES(1),(2),(8),(19); + CREATE TABLE c1(x INTEGER PRIMARY KEY, b INT); + INSERT INTO c1(x,b) VALUES(1,1),(8,8),(17,17),(NULL,NULL); + CREATE TABLE c2(x INT,c INT); + INSERT INTO c2(x,c) VALUES(2,2),(8,8),(NULL,NULL); + CREATE TABLE dual(dummy TEXT); + INSERT INTO dual VALUES('X'); +} {} +do_execsql_test 210 { + BEGIN; + SELECT * FROM t1 ORDER BY a; + UPDATE t1 SET b=c1.b, c=c2.c + FROM dual, c1 NATURAL RIGHT JOIN c2 + WHERE x=a; + SELECT * FROM t1 ORDER BY a; + ROLLBACK; +} { + 1 - - + 2 - - + 8 - - + 19 - - + 1 - - + 2 - 2 + 8 8 8 + 19 - - +} +do_execsql_test 300 { + CREATE TABLE t2(x); + CREATE TRIGGER AFTER INSERT ON t2 BEGIN + UPDATE t1 SET b=c1.b, c=c2.c + FROM dual, c1 NATURAL RIGHT JOIN c2 + WHERE x=a; + END; +} {} +do_execsql_test 310 { + BEGIN; + SELECT * FROM t1 ORDER BY a; + INSERT INTO t2(x) VALUES(1); + SELECT * FROM t1 ORDER BY a; + ROLLBACK; +} { + 1 - - + 2 - - + 8 - - + 19 - - + 1 - - + 2 - 2 + 8 8 8 + 19 - - +} + +# 2022-05-26 dbsqlfuzz crash-9401d6ba699f1257d352a657de236286bf2b14da +# +reset_db +db null - +do_execsql_test 400 { + CREATE TABLE t2(x,y,z PRIMARY KEY) WITHOUT ROWID; + INSERT INTO t2 VALUES(89,-89,6); + CREATE TABLE t1(a INT,b TEXT,c TEXT,d REAL) STRICT; + INSERT INTO t1 VALUES(1,'xyz','def',4.5); + CREATE TRIGGER t1tr BEFORE UPDATE ON t1 BEGIN + INSERT INTO t1(a,b) VALUES(1000,'uvw'); + UPDATE t1 SET b=NULL FROM (SELECT CAST(a AS varchar) FROM t1 ORDER BY b) NATURAL LEFT FULL JOIN t1 AS text; + END; + UPDATE t1 SET b=b|100; + SELECT * FROM t1 ORDER BY a; +} { + 1 100 def 4.5 + 1000 - - - +} + +# Forum post https://sqlite.org/forum/forumpost/36ff78b2a3 +# +ifcapable update_delete_limit { + reset_db + do_execsql_test 500 { + CREATE TABLE t1(abc INT, def INT); + INSERT INTO t1 VALUES(0,0); + INSERT INTO t1 VALUES(0,0); + INSERT INTO t1 VALUES(0,0); + CREATE TABLE dual(dummy TEXT); + INSERT INTO dual(dummy) VALUES('X'); + } {} + + do_execsql_test 510 { + UPDATE t1 + SET (abc, def)=(SELECT x, 123) + FROM dual LEFT JOIN (SELECT 789 AS 'x' FROM dual) AS d2 + LIMIT 2 + } + + do_execsql_test 520 { + SELECT * FROM t1 + } {789 123 789 123 0 0} +} + + +finish_test diff --git a/test/upfromfault.test b/test/upfromfault.test new file mode 100644 index 0000000000..e876c071cb --- /dev/null +++ b/test/upfromfault.test @@ -0,0 +1,139 @@ +# 2020 April 29 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix upfromfault + +foreach {tn sql} { + 1 { + CREATE TABLE t1(x PRIMARY KEY, y, z UNIQUE); + CREATE INDEX t1y ON t1(y); + } + 2 { + CREATE TABLE t1(x PRIMARY KEY, y, z UNIQUE) WITHOUT ROWID; + CREATE INDEX t1y ON t1(y); + } + 3 { + CREATE TABLE t1(x, y, z UNIQUE, PRIMARY KEY(x,y)) WITHOUT ROWID; + } + 4 { + CREATE VIRTUAL TABLE t1 USING fts5(x, y, z); + } + 5 { + CREATE TABLE real(x, y, z); + CREATE VIEW t1 AS SELECT * FROM real; + CREATE TRIGGER t1_insert INSTEAD OF INSERT ON t1 BEGIN + INSERT INTO real VALUES(new.x, new.y, new.z); + END; + CREATE TRIGGER t1_update INSTEAD OF UPDATE ON t1 BEGIN + INSERT INTO log VALUES(old.z || '->' || new.z); + UPDATE real SET y=new.y, z=new.z WHERE x=old.x; + END; + } +} { +if {$tn<5} continue + reset_db + + ifcapable !fts5 { if {$tn==4} continue } + + execsql $sql + do_execsql_test 1.$tn.0 { + CREATE TABLE log(t TEXT); + + INSERT INTO t1 VALUES(1, 'i', 'one'); + INSERT INTO t1 VALUES(2, 'ii', 'two'); + INSERT INTO t1 VALUES(3, 'iii', 'three'); + INSERT INTO t1 VALUES(4, 'iv', 'four'); + } + if {$tn!=4 && $tn!=5} { + do_execsql_test 1.$tn.0b { + CREATE TRIGGER tr1 BEFORE UPDATE ON t1 BEGIN + INSERT INTO log VALUES(old.z || '->' || new.z); + END; + CREATE TRIGGER tr2 AFTER UPDATE ON t1 BEGIN + INSERT INTO log VALUES(old.y || '->' || new.y); + END; + } + } + + faultsim_save_and_close + + do_faultsim_test 1.$tn -prep { + faultsim_restore_and_reopen + execsql { SELECT * FROM t1 } + } -body { + execsql { + WITH data(k, v) AS ( + VALUES(3, 'thirty'), (1, 'ten') + ) + UPDATE t1 SET z=v FROM data WHERE x=k; + } + } -test { + faultsim_test_result {0 {}} {1 {vtable constructor failed: t1}} + if {$testrc==0} { + set res [execsql { SELECT * FROM t1 }] + if {$res!="1 i ten 2 ii two 3 iii thirty 4 iv four"} { + error "unexpected result: $res" + } + } + } +} + +reset_db +do_execsql_test 2.0 { + CREATE TABLE t1(a, b, c); + CREATE TABLE t2(x, y, z); +} +faultsim_save_and_close +do_faultsim_test 2.1 -prep { + faultsim_restore_and_reopen +} -body { + execsql { + CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN + UPDATE t2 SET x=a FROM t1 WHERE c=z; + END; + } +} -test { + faultsim_test_result {0 {}} +} + +faultsim_restore_and_reopen +do_execsql_test 2.2 { + CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN + UPDATE t1 SET a=x FROM t2 WHERE c=z; + END; + + INSERT INTO t2 VALUES(1, 1, 1); + INSERT INTO t2 VALUES(2, 2, 2); + INSERT INTO t2 VALUES(3, 3, 3); +} +faultsim_save_and_close + +do_faultsim_test 2.3 -prep { + faultsim_restore_and_reopen +} -body { + execsql { + INSERT INTO t1 VALUES(NULL, NULL, 1), (NULL, NULL, 3); + } +} -test { + faultsim_test_result {0 {}} + if {$testrc==0} { + set res [execsql { SELECT * FROM t1 }] + if {$res!="1 {} 1 3 {} 3"} { + error "unexpected result: $res" + } + } +} + + +finish_test diff --git a/test/upsert1.test b/test/upsert1.test new file mode 100644 index 0000000000..49168f840b --- /dev/null +++ b/test/upsert1.test @@ -0,0 +1,295 @@ +# 2018-04-12 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# Test cases for UPSERT + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix zipfile + +do_execsql_test upsert1-100 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT, c DEFAULT 0); + CREATE UNIQUE INDEX t1x1 ON t1(b); + INSERT INTO t1(a,b) VALUES(1,2) ON CONFLICT DO NOTHING; + INSERT INTO t1(a,b) VALUES(1,99),(99,2) ON CONFLICT DO NOTHING; + SELECT * FROM t1; +} {1 2 0} +do_execsql_test upsert1-101 { + DELETE FROM t1; + INSERT INTO t1(a,b) VALUES(2,3) ON CONFLICT(a) DO NOTHING; + INSERT INTO t1(a,b) VALUES(2,99) ON CONFLICT(a) DO NOTHING; + SELECT * FROM t1; +} {2 3 0} +do_execsql_test upsert1-102 { + DELETE FROM t1; + INSERT INTO t1(a,b) VALUES(3,4) ON CONFLICT(b) DO NOTHING; + INSERT INTO t1(a,b) VALUES(99,4) ON CONFLICT(b) DO NOTHING; + SELECT * FROM t1; +} {3 4 0} +do_catchsql_test upsert1-110 { + INSERT INTO t1(a,b) VALUES(5,6) ON CONFLICT(x) DO NOTHING; + SELECT * FROM t1; +} {1 {no such column: x}} +do_catchsql_test upsert1-120 { + INSERT INTO t1(a,b) VALUES(5,6) ON CONFLICT(c) DO NOTHING; + SELECT * FROM t1; +} {1 {ON CONFLICT clause does not match any PRIMARY KEY or UNIQUE constraint}} +breakpoint +do_catchsql_test upsert1-130 { + INSERT INTO t1(a,b) VALUES(5,6) ON CONFLICT(b COLLATE nocase) DO NOTHING; + SELECT * FROM t1; +} {1 {ON CONFLICT clause does not match any PRIMARY KEY or UNIQUE constraint}} +do_execsql_test upsert1-140 { + DELETE FROM t1; + INSERT INTO t1(a,b) VALUES(5,6) ON CONFLICT(b COLLATE binary) DO NOTHING; + SELECT * FROM t1; +} {5 6 0} + +do_catchsql_test upsert1-200 { + DROP TABLE t1; + CREATE TABLE t1(a INTEGER PRIMARY KEY, b INT, c DEFAULT 0); + CREATE UNIQUE INDEX t1x1 ON t1(a+b); + INSERT INTO t1(a,b) VALUES(7,8) ON CONFLICT(a+b) DO NOTHING; + INSERT INTO t1(a,b) VALUES(8,7),(9,6) ON CONFLICT(a+b) DO NOTHING; + SELECT * FROM t1; +} {0 {7 8 0}} +do_catchsql_test upsert1-201 { + INSERT INTO t1(a,b) VALUES(8,7),(9,6) ON CONFLICT(a) DO NOTHING; +} {1 {UNIQUE constraint failed: index 't1x1'}} +do_catchsql_test upsert1-210 { + DELETE FROM t1; + INSERT INTO t1(a,b) VALUES(9,10) ON CONFLICT(a+(+b)) DO NOTHING; + SELECT * FROM t1; +} {1 {ON CONFLICT clause does not match any PRIMARY KEY or UNIQUE constraint}} + +do_catchsql_test upsert1-300 { + DROP INDEX t1x1; + DELETE FROM t1; + CREATE UNIQUE INDEX t1x1 ON t1(b) WHERE b>10; + INSERT INTO t1(a,b) VALUES(1,2),(3,2) ON CONFLICT(b) DO NOTHING; + SELECT * FROM t1; +} {1 {ON CONFLICT clause does not match any PRIMARY KEY or UNIQUE constraint}} +do_catchsql_test upsert1-310 { + DELETE FROM t1; + INSERT INTO t1(a,b) VALUES(1,2),(3,2) ON CONFLICT(b) WHERE b!=10 DO NOTHING; + SELECT * FROM t1; +} {1 {ON CONFLICT clause does not match any PRIMARY KEY or UNIQUE constraint}} +do_execsql_test upsert1-320 { + DELETE FROM t1; + INSERT INTO t1(a,b) VALUES(1,2),(3,2),(4,20),(5,20) + ON CONFLICT(b) WHERE b>10 DO NOTHING; + SELECT *, 'x' FROM t1 ORDER BY b, a; +} {1 2 0 x 3 2 0 x 4 20 0 x} + +# Upsert works with count_changes=on; +do_execsql_test upsert1-400 { + DROP TABLE IF EXISTS t2; + CREATE TABLE t2(a TEXT UNIQUE, b INT DEFAULT 1); + INSERT INTO t2(a) VALUES('one'),('two'),('three'); + PRAGMA count_changes=ON; + INSERT INTO t2(a) VALUES('one'),('one'),('three'),('four') + ON CONFLICT(a) DO UPDATE SET b=b+1; +} {1} +do_execsql_test upsert1-410 { + PRAGMA count_changes=OFF; + SELECT a, b FROM t2 ORDER BY a; +} {four 1 one 3 three 2 two 1} + +# Problem found by AFL prior to any release +do_execsql_test upsert1-500 { + DROP TABLE t1; + CREATE TABLE t1(x INTEGER PRIMARY KEY, y INT UNIQUE); + INSERT INTO t1(x,y) SELECT 1,2 WHERE true + ON CONFLICT(x) DO UPDATE SET y=max(t1.y,excluded.y) AND true; + SELECT * FROM t1; +} {1 2} + +# 2018-07-11 +# Ticket https://sqlite.org/src/tktview/79cad5e4b2e219dd197242e9e5f4 +# UPSERT leads to a corrupt index. +# +do_execsql_test upsert1-600 { + DROP TABLE t1; + CREATE TABLE t1(b UNIQUE, a INT PRIMARY KEY) WITHOUT ROWID; + INSERT OR IGNORE INTO t1(a) VALUES('1') ON CONFLICT(a) DO NOTHING; + PRAGMA integrity_check; +} {ok} +do_execsql_test upsert1-610 { + DELETE FROM t1; + INSERT OR IGNORE INTO t1(a) VALUES('1'),(1) ON CONFLICT(a) DO NOTHING; + PRAGMA integrity_check; +} {ok} + +# 2018-08-14 +# Ticket https://sqlite.org/src/info/908f001483982c43 +# If there are multiple uniqueness contraints, the UPSERT should fire +# if the one constraint it targets fails, regardless of whether or not +# the other constraints pass or fail. In other words, the UPSERT constraint +# should be tested first. +# +do_execsql_test upsert1-700 { + DROP TABLE t1; + CREATE TABLE t1(a INTEGER PRIMARY KEY, b INT, c INT, d INT, e INT); + CREATE UNIQUE INDEX t1b ON t1(b); + CREATE UNIQUE INDEX t1e ON t1(e); + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,5); + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,33,44,5) + ON CONFLICT(e) DO UPDATE SET c=excluded.c; + SELECT * FROM t1; +} {1 2 33 4 5} +do_execsql_test upsert1-710 { + DELETE FROM t1; + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,5); + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,33,44,5) + ON CONFLICT(a) DO UPDATE SET c=excluded.c; + SELECT * FROM t1; +} {1 2 33 4 5} +do_execsql_test upsert1-720 { + DELETE FROM t1; + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,5); + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,33,44,5) + ON CONFLICT(b) DO UPDATE SET c=excluded.c; + SELECT * FROM t1; +} {1 2 33 4 5} +do_execsql_test upsert1-730 { + DROP TABLE t1; + CREATE TABLE t1(a INT, b INT, c INT, d INT, e INT); + CREATE UNIQUE INDEX t1a ON t1(a); + CREATE UNIQUE INDEX t1b ON t1(b); + CREATE UNIQUE INDEX t1e ON t1(e); + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,5); + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,33,44,5) + ON CONFLICT(e) DO UPDATE SET c=excluded.c; + SELECT * FROM t1; +} {1 2 33 4 5} +do_execsql_test upsert1-740 { + DELETE FROM t1; + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,5); + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,33,44,5) + ON CONFLICT(a) DO UPDATE SET c=excluded.c; + SELECT * FROM t1; +} {1 2 33 4 5} +do_execsql_test upsert1-750 { + DELETE FROM t1; + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,5); + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,33,44,5) + ON CONFLICT(b) DO UPDATE SET c=excluded.c; + SELECT * FROM t1; +} {1 2 33 4 5} +do_execsql_test upsert1-760 { + DROP TABLE t1; + CREATE TABLE t1(a INT PRIMARY KEY, b INT, c INT, d INT, e INT) WITHOUT ROWID; + CREATE UNIQUE INDEX t1a ON t1(a); + CREATE UNIQUE INDEX t1b ON t1(b); + CREATE UNIQUE INDEX t1e ON t1(e); + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,5); + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,33,44,5) + ON CONFLICT(e) DO UPDATE SET c=excluded.c; + SELECT * FROM t1; +} {1 2 33 4 5} +do_execsql_test upsert1-770 { + DELETE FROM t1; + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,5); + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,33,44,5) + ON CONFLICT(a) DO UPDATE SET c=excluded.c; + SELECT * FROM t1; +} {1 2 33 4 5} +do_execsql_test upsert1-780 { + DELETE FROM t1; + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,5); + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,33,44,5) + ON CONFLICT(b) DO UPDATE SET c=excluded.c; + SELECT * FROM t1; +} {1 2 33 4 5} + +# 2019-08-30 ticket https://sqlite.org/src/info/5a3dba8104421320 +do_execsql_test upsert1-800 { + DROP TABLE IF EXISTS t0; + CREATE TABLE t0(c0 REAL UNIQUE, c1); + CREATE UNIQUE INDEX test800i0 ON t0(0 || c1); + INSERT INTO t0(c0, c1) VALUES (1, 2), (2, 1); + INSERT INTO t0(c0) VALUES (1) ON CONFLICT(c0) DO UPDATE SET c1=excluded.c0; + PRAGMA integrity_check; + REINDEX; +} {ok} + +# 2019-12-06 gramfuzz find +sqlite3 db :memory: +do_execsql_test upsert1-900 { + CREATE VIEW t1(a) AS SELECT 1; + CREATE TRIGGER t1r1 INSTEAD OF INSERT ON t1 BEGIN + SELECT 2; + END; +} +do_catchsql_test upsert1-910 { + INSERT INTO t1 VALUES(3) ON CONFLICT(x) DO NOTHING; +} {1 {cannot UPSERT a view}} + +# 2019-12-26 ticket 7c13db5c3bf74001 +reset_db +do_catchsql_test upsert1-1000 { + CREATE TABLE t0(c0 PRIMARY KEY, c1, c2 UNIQUE) WITHOUT ROWID; + INSERT OR FAIL INTO t0(c2) VALUES (0), (NULL) + ON CONFLICT(c2) DO UPDATE SET c1 = c0; +} {1 {NOT NULL constraint failed: t0.c0}} + +# 2021-12-29 forum post https://sqlite.org/forum/forumpost/06b16b8b29f8c8c3 +# By Jingzhou Fu. When there is both an INTEGER PRIMARY KEY ON CONFLICT REPLACE +# and an upsert on a constraint other than the INTEGER PRIMARY KEY, the +# constraint checking logic generates invalid bytecode which might result +# in a NULL pointer dereference. +# +reset_db +do_execsql_test upsert1-1100 { + CREATE TABLE t1(a INTEGER PRIMARY KEY ON CONFLICT REPLACE, b UNIQUE); + INSERT INTO t1(b) VALUES(22); + INSERT INTO t1 VALUES(2,22) ON CONFLICT (b) DO NOTHING; + SELECT * FROM t1; +} {1 22} + +# 2023-08-17 dbsqlfuzz 9983e2c77634a8ccf33b5c91fa9982599de5f9e9 +# Bound parameters in the ON CONFLICT clause of an UPSERT. +# +reset_db +do_execsql_test upsert1-1200 { + CREATE TABLE t1(a INT, b INT); + CREATE UNIQUE INDEX t1x ON t1(b+3); +} +sqlite3_db_config db ENABLE_QPSG 1 +do_catchsql_test upsert1-1210 { + INSERT INTO t1(a,b) VALUES(1,2) ON CONFLICT(b+?1) DO NOTHING; +} {1 {ON CONFLICT clause does not match any PRIMARY KEY or UNIQUE constraint}} + +# 2024-04-11 https://sqlite.org/forum/forumpost/284955a3cd454a15 +# Incorrect value passed into a trigger that fires as the result of +# an upsert. +# +reset_db +do_execsql_test upsert1-1300 { + CREATE TABLE t1(x INT, y TEXT); + INSERT INTO t1 VALUES + (11, printf('%.9000c','a')), + (11, printf('%.9000c','a')), + (33, printf('%.9000c','b')), + (33, printf('%.9000c','b')); + CREATE TABLE t2(x INT UNIQUE, y TEXT); + CREATE TRIGGER r1 BEFORE UPDATE ON t2 BEGIN + SELECT raise(ABORT,'Incorrect old.y value passed to trigger!') + WHERE old.y != new.y; + /* ^^^ This trigger will fire and cause the ABORT if the problem has + ** not been fixed, or if there is a regression. */ + END; + INSERT INTO t2(x, y) SELECT x, y FROM t1 + WHERE true + ON CONFLICT (x) DO UPDATE SET y = excluded.y; +} {} + +finish_test diff --git a/test/upsert2.test b/test/upsert2.test new file mode 100644 index 0000000000..5cbc4656a4 --- /dev/null +++ b/test/upsert2.test @@ -0,0 +1,170 @@ +# 2018-04-17 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# Test cases for UPSERT + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix zipfile + +do_execsql_test upsert2-100 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b int, c DEFAULT 0); + INSERT INTO t1(a,b) VALUES(1,2),(3,4); + INSERT INTO t1(a,b) VALUES(1,8),(2,11),(3,1) + ON CONFLICT(a) DO UPDATE SET b=excluded.b, c=c+1 WHERE t1.b<excluded.b; + SELECT *, 'x' FROM t1 ORDER BY a; +} {1 8 1 x 2 11 0 x 3 4 0 x} +do_execsql_test upsert2-110 { + DROP TABLE t1; + CREATE TABLE t1(a INT PRIMARY KEY, b int, c DEFAULT 0) WITHOUT ROWID; + INSERT INTO t1(a,b) VALUES(1,2),(3,4); + INSERT INTO t1(a,b) VALUES(1,8),(2,11),(3,1) + ON CONFLICT(a) DO UPDATE SET b=excluded.b, c=c+1 WHERE t1.b<excluded.b; + SELECT *, 'x' FROM t1 ORDER BY a; +} {1 8 1 x 2 11 0 x 3 4 0 x} + +do_execsql_test upsert2-200 { + DROP TABLE t1; + CREATE TABLE t1(a INTEGER PRIMARY KEY, b int, c DEFAULT 0); + INSERT INTO t1(a,b) VALUES(1,2),(3,4); + WITH nx(a,b) AS (VALUES(1,8),(2,11),(3,1),(2,15),(1,4),(1,99)) + INSERT INTO t1(a,b) SELECT a, b FROM nx WHERE true + ON CONFLICT(a) DO UPDATE SET b=excluded.b, c=c+1 WHERE t1.b<excluded.b; + SELECT *, 'x' FROM t1 ORDER BY a; +} {1 99 2 x 2 15 1 x 3 4 0 x} +do_execsql_test upsert2-201 { + DELETE FROM t1; + INSERT INTO t1(a,b) VALUES(1,2),(3,4); + WITH nx(a,b) AS (VALUES(1,8),(2,11),(3,1),(2,15),(1,4),(1,99)) + INSERT INTO main.t1 AS t2(a,b) SELECT a, b FROM nx WHERE true + ON CONFLICT(a) DO UPDATE SET b=excluded.b, c=t2.c+1 WHERE t2.b<excluded.b; + SELECT *, 'x' FROM t1 ORDER BY a; +} {1 99 2 x 2 15 1 x 3 4 0 x} +do_catchsql_test upsert2-202 { + WITH nx(a,b) AS (VALUES(1,8),(2,11),(3,1),(2,15),(1,4),(1,99)) + INSERT INTO t1 AS t2(a,b) SELECT a, b FROM nx WHERE true + ON CONFLICT(a) DO UPDATE SET b=excluded.b, c=t1.c+1 WHERE t1.b<excluded.b; +} {1 {no such column: t1.c}} +do_execsql_test upsert2-210 { + DROP TABLE t1; + CREATE TABLE t1(a INT PRIMARY KEY, b int, c DEFAULT 0) WITHOUT ROWID; + INSERT INTO t1(a,b) VALUES(1,2),(3,4); + WITH nx(a,b) AS (VALUES(1,8),(2,11),(3,1),(2,15),(1,4),(1,99)) + INSERT INTO t1(a,b) SELECT a, b FROM nx WHERE true + ON CONFLICT(a) DO UPDATE SET b=excluded.b, c=c+1 WHERE t1.b<excluded.b; + SELECT *, 'x' FROM t1 ORDER BY a; +} {1 99 2 x 2 15 1 x 3 4 0 x} + +# On an ON CONFLICT DO UPDATE, the before-insert, before-update, and +# after-update triggers fire. +# +do_execsql_test upsert2-300 { + DROP TABLE t1; + CREATE TABLE t1(a INTEGER PRIMARY KEY, b int, c DEFAULT 0); + CREATE TABLE record(x TEXT, y TEXT); + CREATE TRIGGER r1 BEFORE INSERT ON t1 BEGIN + INSERT INTO record(x,y) + VALUES('before-insert',format('%d,%d,%d',new.a,new.b,new.c)); + END; + CREATE TRIGGER r2 AFTER INSERT ON t1 BEGIN + INSERT INTO record(x,y) + VALUES('after-insert',printf('%d,%d,%d',new.a,new.b,new.c)); + END; + CREATE TRIGGER r3 BEFORE UPDATE ON t1 BEGIN + INSERT INTO record(x,y) + VALUES('before-update',format('%d,%d,%d/%d,%d,%d', + old.a,old.b,old.c,new.a,new.b,new.c)); + END; + CREATE TRIGGER r4 AFTER UPDATE ON t1 BEGIN + INSERT INTO record(x,y) + VALUES('after-update',printf('%d,%d,%d/%d,%d,%d', + old.a,old.b,old.c,new.a,new.b,new.c)); + END; + INSERT INTO t1(a,b) VALUES(1,2); + DELETE FROM record; + INSERT INTO t1(a,b) VALUES(1,2) + ON CONFLICT(a) DO UPDATE SET c=t1.c+1; + SELECT * FROM record +} {before-insert 1,2,0 before-update 1,2,0/1,2,1 after-update 1,2,0/1,2,1} + +# On an ON CONFLICT DO NOTHING, only the before-insert trigger fires. +# +do_execsql_test upsert2-310 { + DELETE FROM record; + INSERT INTO t1(a,b) VALUES(1,2) ON CONFLICT DO NOTHING; + SELECT * FROM record; +} {before-insert 1,2,0} + +# With ON CONFLICT DO UPDATE and a failed WHERE, only the before-insert +# trigger fires. +# +do_execsql_test upsert2-320 { + DELETE FROM record; + INSERT INTO t1(a,b) VALUES(1,2) + ON CONFLICT(a) DO UPDATE SET c=c+1 WHERE c<0; + SELECT * FROM record; +} {before-insert 1,2,0} +do_execsql_test upsert2-321 { + SELECT * FROM t1; +} {1 2 1} + +# Trigger tests repeated for a WITHOUT ROWID table. +# +do_execsql_test upsert2-400 { + DROP TABLE t1; + CREATE TABLE t1(a INT PRIMARY KEY, b int, c DEFAULT 0) WITHOUT ROWID; + CREATE TRIGGER r1 BEFORE INSERT ON t1 BEGIN + INSERT INTO record(x,y) + VALUES('before-insert',format('%d,%d,%d',new.a,new.b,new.c)); + END; + CREATE TRIGGER r2 AFTER INSERT ON t1 BEGIN + INSERT INTO record(x,y) + VALUES('after-insert',printf('%d,%d,%d',new.a,new.b,new.c)); + END; + CREATE TRIGGER r3 BEFORE UPDATE ON t1 BEGIN + INSERT INTO record(x,y) + VALUES('before-update',format('%d,%d,%d/%d,%d,%d', + old.a,old.b,old.c,new.a,new.b,new.c)); + END; + CREATE TRIGGER r4 AFTER UPDATE ON t1 BEGIN + INSERT INTO record(x,y) + VALUES('after-update',printf('%d,%d,%d/%d,%d,%d', + old.a,old.b,old.c,new.a,new.b,new.c)); + END; + INSERT INTO t1(a,b) VALUES(1,2); + DELETE FROM record; + INSERT INTO t1(a,b) VALUES(1,2) + ON CONFLICT(a) DO UPDATE SET c=t1.c+1; + SELECT * FROM record +} {before-insert 1,2,0 before-update 1,2,0/1,2,1 after-update 1,2,0/1,2,1} + +# On an ON CONFLICT DO NOTHING, only the before-insert trigger fires. +# +do_execsql_test upsert2-410 { + DELETE FROM record; + INSERT INTO t1(a,b) VALUES(1,2) ON CONFLICT DO NOTHING; + SELECT * FROM record; +} {before-insert 1,2,0} + +# With ON CONFLICT DO UPDATE and a failed WHERE, only the before-insert +# trigger fires. +# +do_execsql_test upsert2-420 { + DELETE FROM record; + INSERT INTO t1(a,b) VALUES(1,2) + ON CONFLICT(a) DO UPDATE SET c=c+1 WHERE c<0; + SELECT * FROM record; +} {before-insert 1,2,0} +do_execsql_test upsert2-421 { + SELECT * FROM t1; +} {1 2 1} + +finish_test diff --git a/test/upsert3.test b/test/upsert3.test new file mode 100644 index 0000000000..b6242e9ff8 --- /dev/null +++ b/test/upsert3.test @@ -0,0 +1,58 @@ +# 2018-04-17 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# Test cases for UPSERT +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix zipfile + +do_execsql_test upsert3-100 { + CREATE TABLE t1(k int, v text); + CREATE UNIQUE INDEX x1 ON t1(k, v); +} {} +do_catchsql_test upsert3-110 { + INSERT INTO t1 VALUES(0,'abcdefghij') + ON CONFLICT(k) DO NOTHING; +} {1 {ON CONFLICT clause does not match any PRIMARY KEY or UNIQUE constraint}} +do_catchsql_test upsert3-120 { + INSERT INTO t1 VALUES(0,'abcdefghij') + ON CONFLICT(v) DO NOTHING; +} {1 {ON CONFLICT clause does not match any PRIMARY KEY or UNIQUE constraint}} + +do_execsql_test upsert3-130 { + INSERT INTO t1 VALUES(0, 'abcdefghij') + ON CONFLICT(k,v) DO NOTHING; + SELECT * FROM t1; +} {0 abcdefghij} +do_execsql_test upsert3-140 { + INSERT INTO t1 VALUES(0, 'abcdefghij') + ON CONFLICT(v,k) DO NOTHING; + SELECT * FROM t1; +} {0 abcdefghij} + +do_execsql_test upsert3-200 { + CREATE TABLE excluded(a INT, b INT, c INT DEFAULT 0); + CREATE UNIQUE INDEX excludedab ON excluded(a,b); + INSERT INTO excluded(a,b) VALUES(1,2),(1,2),(3,4),(1,2),(5,6),(3,4) + ON CONFLICT(b,a) DO UPDATE SET c=excluded.c+1; + SELECT *, 'x' FROM excluded ORDER BY a; +} {1 2 2 x 3 4 1 x 5 6 0 x} +do_execsql_test upsert3-210 { + INSERT INTO excluded AS base(a,b,c) VALUES(1,2,8),(1,2,3) + ON CONFLICT(b,a) DO UPDATE SET c=excluded.c+1 WHERE base.c<excluded.c; + SELECT *, 'x' FROM excluded ORDER BY a; +} {1 2 9 x 3 4 1 x 5 6 0 x} + + + +finish_test diff --git a/test/upsert4.test b/test/upsert4.test new file mode 100644 index 0000000000..c4bcc0329d --- /dev/null +++ b/test/upsert4.test @@ -0,0 +1,407 @@ +# 2018-04-17 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# Test cases for UPSERT + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix upsert4 + +foreach {tn sql} { + 1 { CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c UNIQUE) } + 2 { CREATE TABLE t1(a INT PRIMARY KEY, b, c UNIQUE) } + 3 { CREATE TABLE t1(a INT PRIMARY KEY, b, c UNIQUE) WITHOUT ROWID} +} { + reset_db + execsql $sql + + do_execsql_test 1.$tn.0 { + INSERT INTO t1 VALUES(1, NULL, 'one'); + INSERT INTO t1 VALUES(2, NULL, 'two'); + INSERT INTO t1 VALUES(3, NULL, 'three'); + } + + do_execsql_test 1.$tn.1 { + INSERT INTO t1 VALUES(1, NULL, 'xyz') ON CONFLICT DO NOTHING; + SELECT * FROM t1; + } { + 1 {} one 2 {} two 3 {} three + } + + do_execsql_test 1.$tn.2 { + INSERT INTO t1 VALUES(4, NULL, 'two') ON CONFLICT DO NOTHING; + SELECT * FROM t1; + } { + 1 {} one 2 {} two 3 {} three + } + + do_execsql_test 1.$tn.3 { + INSERT INTO t1 VALUES(4, NULL, 'two') ON CONFLICT (c) DO UPDATE SET b = 1; + SELECT * FROM t1; + } { + 1 {} one 2 1 two 3 {} three + } + + do_execsql_test 1.$tn.4 { + INSERT INTO t1 VALUES(2, NULL, 'zero') ON CONFLICT (a) DO UPDATE SET b=2; + SELECT * FROM t1; + } {1 {} one 2 2 two 3 {} three} + + do_catchsql_test 1.$tn.5 { + INSERT INTO t1 VALUES(2, NULL, 'zero') ON CONFLICT (a) + DO UPDATE SET c = 'one'; + } {1 {UNIQUE constraint failed: t1.c}} + + do_execsql_test 1.$tn.6 { + SELECT * FROM t1; + } {1 {} one 2 2 two 3 {} three} + + do_execsql_test 1.$tn.7 { + INSERT INTO t1 VALUES(2, NULL, 'zero') ON CONFLICT (a) + DO UPDATE SET (b, c) = (SELECT 'x', 'y'); + SELECT * FROM t1; + } {1 {} one 2 x y 3 {} three} + + do_execsql_test 1.$tn.8 { + INSERT INTO t1 VALUES(1, NULL, NULL) ON CONFLICT (a) + DO UPDATE SET (c, a) = ('four', 4); + SELECT * FROM t1 ORDER BY 1; + } {2 x y 3 {} three 4 {} four} +} + +#------------------------------------------------------------------------- +# Test target analysis. +# +set rtbl(0) {0 {}} +set rtbl(1) {/1 .*failed.*/} +set rtbl(2) {1 {ON CONFLICT clause does not match any PRIMARY KEY or UNIQUE constraint}} + +foreach {tn sql} { + 1 { + CREATE TABLE xyz(a INTEGER PRIMARY KEY, b, c, d); + CREATE UNIQUE INDEX xyz1 ON xyz(d, c, b COLLATE nocase); + } + + 2 { + CREATE TABLE xyz(a INT PRIMARY KEY, b, c, d); + CREATE UNIQUE INDEX xyz1 ON xyz(d, c, b COLLATE nocase); + } + + 3 { + CREATE TABLE xyz(a INT PRIMARY KEY, b, c, d) WITHOUT ROWID; + CREATE UNIQUE INDEX xyz1 ON xyz(d, c, b COLLATE nocase); + } +} { + reset_db + execsql $sql + do_execsql_test 2.$tn.1 { + INSERT INTO xyz VALUES(10, 1, 1, 'one'); + } + + + foreach {tn2 oc res} { + 1 "ON CONFLICT (b COLLATE nocase, c, d) DO NOTHING" 0 + 2 "ON CONFLICT (b, c, d) DO NOTHING" 0 + 3 "ON CONFLICT (b, c COLLATE nocase, d) DO NOTHING" 2 + 4 "ON CONFLICT (a) DO NOTHING" 1 + 5 "ON CONFLICT DO NOTHING" 0 + 6 "ON CONFLICT (b, c, d) WHERE a!=0 DO NOTHING" 0 + 7 "ON CONFLICT (d, c, c) WHERE a!=0 DO NOTHING" 2 + 8 "ON CONFLICT (b COLLATE nocase, c COLLATE nocase, d) DO NOTHING" 2 + 9 "ON CONFLICT (b, c, d) WHERE b==45 DO NOTHING" 0 + } { + + do_catchsql_test 2.$tn.2.$tn2 " + INSERT INTO xyz VALUES(11, 1, 1, 'one') $oc + " $rtbl($res) + } + + do_execsql_test 2.$tn.3 { + SELECT * FROM xyz; + } {10 1 1 one} +} + +foreach {tn sql} { + 1 { + CREATE TABLE abc(a INTEGER PRIMARY KEY, x, y); + CREATE UNIQUE INDEX abc1 ON abc(('x' || x) COLLATE nocase); + } + 2 { + CREATE TABLE abc(a INT PRIMARY KEY, x, y); + CREATE UNIQUE INDEX abc1 ON abc(('x' || x) COLLATE nocase); + } + 3 { + CREATE TABLE abc(a INT PRIMARY KEY, x, y) WITHOUT ROWID; + CREATE UNIQUE INDEX abc1 ON abc(('x' || x) COLLATE nocase); + } +} { + reset_db + execsql $sql + do_execsql_test 3.$tn.1 { + INSERT INTO abc VALUES(1, 'one', 'two'); + } + + foreach {tn2 oc res} { + 1 "ON CONFLICT DO NOTHING" 0 + 2 "ON CONFLICT ('x' || x) DO NOTHING" 0 + 3 "ON CONFLICT (('x' || x) COLLATE nocase) DO NOTHING" 0 + 4 "ON CONFLICT (('x' || x) COLLATE binary) DO NOTHING" 2 + 5 "ON CONFLICT (x || 'x') DO NOTHING" 2 + 6 "ON CONFLICT ((('x' || x))) DO NOTHING" 0 + } { + do_catchsql_test 3.$tn.2.$tn2 " + INSERT INTO abc VALUES(2, 'one', NULL) $oc; + " $rtbl($res) + } + + do_execsql_test 3.$tn.3 { + SELECT * FROM abc + } {1 one two} +} + +foreach {tn sql} { + 1 { + CREATE TABLE abc(a INTEGER PRIMARY KEY, x, y); + CREATE UNIQUE INDEX abc1 ON abc(x) WHERE y>0; + CREATE UNIQUE INDEX abc2 ON abc(y) WHERE x='xyz' COLLATE nocase; + } +} { + reset_db + execsql $sql + do_execsql_test 4.$tn.1 { + INSERT INTO abc VALUES(1, 'one', 1); + INSERT INTO abc VALUES(2, 'two', 2); + INSERT INTO abc VALUES(3, 'xyz', 3); + INSERT INTO abc VALUES(4, 'XYZ', 4); + } + + foreach {tn2 oc res} { + 1 "ON CONFLICT DO NOTHING" 0 + 2 "ON CONFLICT(x) WHERE y>0 DO NOTHING" 0 + 3 "ON CONFLICT(x) DO NOTHING" 2 + 4 "ON CONFLICT(x) WHERE y>=0 DO NOTHING" 2 + 5 "ON CONFLICT(y) WHERE x='xyz' COLLATE nocase DO NOTHING" 1 + } { + do_catchsql_test 4.$tn.2.$tn2 " + INSERT INTO abc VALUES(5, 'one', 10) $oc + " $rtbl($res) + } + + do_execsql_test 4.$tn.3 { + SELECT * FROM abc + } {1 one 1 2 two 2 3 xyz 3 4 XYZ 4} + + foreach {tn2 oc res} { + 1 "ON CONFLICT DO NOTHING" 0 + 2 "ON CONFLICT(y) WHERE x='xyz' COLLATE nocase DO NOTHING" 0 + 3 "ON CONFLICT(y) WHERE x='xyz' COLLATE binary DO NOTHING" 2 + 4 "ON CONFLICT(x) WHERE y>0 DO NOTHING" 1 + } { + do_catchsql_test 4.$tn.2.$tn2 " + INSERT INTO abc VALUES(5, 'xYz', 3) $oc + " $rtbl($res) + } +} + +do_catchsql_test 5.0 { + CREATE TABLE w1(a INT PRIMARY KEY, x, y); + CREATE UNIQUE INDEX w1expr ON w1(('x' || x)); + INSERT INTO w1 VALUES(2, 'one', NULL) + ON CONFLICT (('x' || x) COLLATE nocase) DO NOTHING; +} {1 {ON CONFLICT clause does not match any PRIMARY KEY or UNIQUE constraint}} + +#------------------------------------------------------------------------- +# Test that ON CONFLICT constraint processing occurs before any REPLACE +# constraint processing. +# +foreach {tn sql} { + 1 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b UNIQUE, c); + } + 2 { + CREATE TABLE t1(a INT PRIMARY KEY, b UNIQUE, c); + } + 3 { + CREATE TABLE t1(a INT PRIMARY KEY, b UNIQUE, c) WITHOUT ROWID; + } +} { + reset_db + execsql $sql + do_execsql_test 6.1.$tn { + INSERT INTO t1 VALUES(1, 1, 'one'); + INSERT INTO t1 VALUES(2, 2, 'two'); + INSERT OR REPLACE INTO t1 VALUES(1, 2, 'two') ON CONFLICT(b) DO NOTHING; + PRAGMA integrity_check; + } {ok} +} + +foreach {tn sql} { + 1 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b UNIQUE, c UNIQUE); + } +} { + reset_db + execsql $sql + + do_execsql_test 6.2.$tn.1 { + INSERT INTO t1 VALUES(1, 1, 1); + INSERT INTO t1 VALUES(2, 2, 2); + } + + do_execsql_test 6.2.$tn.2 { + INSERT OR REPLACE INTO t1 VALUES(3, 1, 1) ON CONFLICT(b) DO NOTHING; + SELECT * FROM t1; + PRAGMA integrity_check; + } {1 1 1 2 2 2 ok} + + do_execsql_test 6.2.$tn.3 { + INSERT OR REPLACE INTO t1 VALUES(3, 2, 2) ON CONFLICT(c) DO NOTHING; + SELECT * FROM t1; + PRAGMA integrity_check; + } {1 1 1 2 2 2 ok} + + do_execsql_test 6.2.$tn.2 { + INSERT OR REPLACE INTO t1 VALUES(3, 1, 1) ON CONFLICT(b) + DO UPDATE SET b=b||'x'; + SELECT * FROM t1; + PRAGMA integrity_check; + } {1 1x 1 2 2 2 ok} + + do_execsql_test 6.2.$tn.2 { + INSERT OR REPLACE INTO t1 VALUES(3, 2, 2) ON CONFLICT(c) + DO UPDATE SET c=c||'x'; + SELECT * FROM t1; + PRAGMA integrity_check; + } {1 1x 1 2 2 2x ok} +} + +#------------------------------------------------------------------------- +# Test references to "excluded". And using an alias in an INSERT +# statement. +# +foreach {tn sql} { + 1 { + CREATE TABLE t1(w, x, y, z, PRIMARY KEY(x, y)); + CREATE UNIQUE INDEX zz ON t1(z); + } + 2 { + CREATE TABLE t1(w, x, y, z, PRIMARY KEY(x, y)) WITHOUT ROWID; + CREATE UNIQUE INDEX zz ON t1(z); + } +} { + reset_db + execsql $sql + do_execsql_test 7.$tn.0 { + INSERT INTO t1 VALUES('a', 1, 1, 1); + INSERT INTO t1 VALUES('b', 2, 2, 2); + } + + do_execsql_test 7.$tn.1 { + INSERT INTO t1 VALUES('c', 3, 3, 1) ON CONFLICT(z) + DO UPDATE SET w = excluded.w; + SELECT * FROM t1; + } {c 1 1 1 b 2 2 2} + + do_execsql_test 7.$tn.2 { + INSERT INTO t1 VALUES('c', 2, 2, 3) ON CONFLICT(y, x) + DO UPDATE SET w = w||w; + SELECT * FROM t1; + } {c 1 1 1 bb 2 2 2} + + do_execsql_test 7.$tn.3 { + INSERT INTO t1 VALUES('c', 2, 2, 3) ON CONFLICT(y, x) + DO UPDATE SET w = w||t1.w; + SELECT * FROM t1; + } {c 1 1 1 bbbb 2 2 2} + + do_execsql_test 7.$tn.4 { + INSERT INTO t1 AS tbl VALUES('c', 2, 2, 3) ON CONFLICT(y, x) + DO UPDATE SET w = w||tbl.w; + SELECT * FROM t1; + } {c 1 1 1 bbbbbbbb 2 2 2} +} + +foreach {tn sql} { + 1 { + CREATE TABLE excluded(w, x INTEGER, 'a b', z, PRIMARY KEY(x, 'a b')); + CREATE UNIQUE INDEX zz ON excluded(z); + CREATE INDEX zz2 ON excluded(z); + } + 2 { + CREATE TABLE excluded(w, x, 'a b', z, PRIMARY KEY(x, 'a b')) WITHOUT ROWID; + CREATE UNIQUE INDEX zz ON excluded(z); + CREATE INDEX zz2 ON excluded(z); + } +} { + reset_db + execsql $sql + do_execsql_test 8.$tn.0 { + INSERT INTO excluded VALUES('a', 1, 1, 1); + INSERT INTO excluded VALUES('b', 2, 2, 2); + } + + # Note: An error in Postgres: "table reference "excluded" is ambiguous". + # + do_execsql_test 8.$tn.1 { + INSERT INTO excluded VALUES('hello', 1, 1, NULL) ON CONFLICT(x, "a b") + DO UPDATE SET w=excluded.w; + SELECT * FROM excluded; + } {a 1 1 1 b 2 2 2} + + do_execsql_test 8.$tn.2 { + INSERT INTO excluded AS x1 VALUES('hello', 1, 1, NULL) ON CONFLICT(x, [a b]) + DO UPDATE SET w=excluded.w; + SELECT * FROM excluded; + } {hello 1 1 1 b 2 2 2} + + do_execsql_test 8.$tn.3 { + INSERT INTO excluded AS x1 VALUES('hello', 1, 1, NULL) ON CONFLICT(x, [a b]) + DO UPDATE SET w=w||w WHERE excluded.w!='hello'; + SELECT * FROM excluded; + } {hello 1 1 1 b 2 2 2} + + do_execsql_test 8.$tn.4 { + INSERT INTO excluded AS x1 VALUES('hello', 1, 1, NULL) ON CONFLICT(x, [a b]) + DO UPDATE SET w=w||w WHERE excluded.x=1; + SELECT * FROM excluded; + } {hellohello 1 1 1 b 2 2 2} + + do_catchsql_test 8.$tn.5 { + INSERT INTO excluded AS x1 VALUES('hello', 1, 1, NULL) + ON CONFLICT(x, [a b]) WHERE y=1 + DO UPDATE SET w=w||w WHERE excluded.x=1; + } {1 {no such column: y}} +} + +#-------------------------------------------------------------------------- +# +do_execsql_test 9.0 { + CREATE TABLE v(x INTEGER); + CREATE TABLE hist(x INTEGER PRIMARY KEY, cnt INTEGER); + CREATE TRIGGER vt AFTER INSERT ON v BEGIN + INSERT INTO hist VALUES(new.x, 1) ON CONFLICT(x) DO + UPDATE SET cnt=cnt+1; + END; +} + +do_execsql_test 9.1 { + INSERT INTO v VALUES(1), (4), (1), (5), (5), (8), (9), (1); + SELECT * FROM hist; +} { + 1 3 + 4 1 + 5 2 + 8 1 + 9 1 +} + + +finish_test diff --git a/test/upsert5.test b/test/upsert5.test new file mode 100644 index 0000000000..e56e71d4b9 --- /dev/null +++ b/test/upsert5.test @@ -0,0 +1,453 @@ +# 2020-12-11 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# Test cases for generalized UPSERT + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix upsert5 + +foreach {tn sql} { + 1 { CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c UNIQUE, d UNIQUE, e UNIQUE) } + 2 { CREATE TABLE t1(a INT PRIMARY KEY, b, c UNIQUE, d UNIQUE, e UNIQUE) } + 3 { CREATE TABLE t1(a INT PRIMARY KEY, b, c UNIQUE, d UNIQUE, e UNIQUE) WITHOUT ROWID} + 4 { CREATE TABLE t1(e UNIQUE, d UNIQUE, c UNIQUE, a INTEGER PRIMARY KEY, b) } + 5 { CREATE TABLE t1(e UNIQUE, d UNIQUE, c UNIQUE, a INT PRIMARY KEY, b) } + 6 { CREATE TABLE t1(e UNIQUE, d UNIQUE, c UNIQUE, a INT PRIMARY KEY, b) WITHOUT ROWID} +} { + reset_db + execsql $sql + + do_execsql_test 1.$tn.100 { + DELETE FROM t1; + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,5); + INSERT INTO t1(a,b,c,d,e) VALUES(1,NULL,3,4,5) + ON CONFLICT(a) DO UPDATE SET b='a' + ON CONFLICT(c) DO UPDATE SET b='c' + ON CONFLICT(d) DO UPDATE SET b='d' + ON CONFLICT(e) DO UPDATE SET b='e'; + SELECT a,b,c,d,e FROM t1; + } {1 a 3 4 5} + do_execsql_test 1.$tn.101 { + DELETE FROM t1; + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,5); + INSERT INTO t1(a,b,c,d,e) VALUES(91,NULL,3,4,5) + ON CONFLICT(a) DO UPDATE SET b='a' + ON CONFLICT(c) DO UPDATE SET b='c' + ON CONFLICT(d) DO UPDATE SET b='d' + ON CONFLICT(e) DO UPDATE SET b='e'; + SELECT a,b,c,d,e FROM t1; + } {1 c 3 4 5} + do_execsql_test 1.$tn.102 { + DELETE FROM t1; + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,5); + INSERT INTO t1(a,b,c,d,e) VALUES(91,NULL,93,4,5) + ON CONFLICT(a) DO UPDATE SET b='a' + ON CONFLICT(c) DO UPDATE SET b='c' + ON CONFLICT(d) DO UPDATE SET b='d' + ON CONFLICT(e) DO UPDATE SET b='e'; + SELECT a,b,c,d,e FROM t1; + } {1 d 3 4 5} + do_execsql_test 1.$tn.103 { + DELETE FROM t1; + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,5); + INSERT INTO t1(a,b,c,d,e) VALUES(91,NULL,93,94,5) + ON CONFLICT(a) DO UPDATE SET b='a' + ON CONFLICT(c) DO UPDATE SET b='c' + ON CONFLICT(d) DO UPDATE SET b='d' + ON CONFLICT(e) DO UPDATE SET b='e'; + SELECT a,b,c,d,e FROM t1; + } {1 e 3 4 5} + do_execsql_test 1.$tn.200 { + DELETE FROM t1; + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,5); + INSERT INTO t1(a,b,c,d,e) VALUES(1,NULL,93,94,95) + ON CONFLICT(c) DO UPDATE SET b='c' + ON CONFLICT(a) DO UPDATE SET b='a' + ON CONFLICT(d) DO UPDATE SET b='d' + ON CONFLICT(e) DO UPDATE SET b='e'; + SELECT a,b,c,d,e FROM t1; + } {1 a 3 4 5} + do_execsql_test 1.$tn.201 { + DELETE FROM t1; + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,5); + INSERT INTO t1(a,b,c,d,e) VALUES(1,NULL,3,94,95) + ON CONFLICT(c) DO UPDATE SET b='c' + ON CONFLICT(a) DO UPDATE SET b='a' + ON CONFLICT(d) DO UPDATE SET b='d' + ON CONFLICT(e) DO UPDATE SET b='e'; + SELECT a,b,c,d,e FROM t1; + } {1 c 3 4 5} + do_execsql_test 1.$tn.202 { + DELETE FROM t1; + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,5); + INSERT INTO t1(a,b,c,d,e) VALUES(1,NULL,3,4,5) + ON CONFLICT(c) DO UPDATE SET b='c' + ON CONFLICT(a) DO UPDATE SET b='a' + ON CONFLICT(d) DO UPDATE SET b='d' + ON CONFLICT(e) DO UPDATE SET b='e'; + SELECT a,b,c,d,e FROM t1; + } {1 c 3 4 5} + do_execsql_test 1.$tn.203 { + DELETE FROM t1; + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,5); + INSERT INTO t1(a,b,c,d,e) VALUES(1,NULL,93,94,5) + ON CONFLICT(c) DO UPDATE SET b='c' + ON CONFLICT(a) DO UPDATE SET b='a' + ON CONFLICT(d) DO UPDATE SET b='d' + ON CONFLICT(e) DO UPDATE SET b='e'; + SELECT a,b,c,d,e FROM t1; + } {1 a 3 4 5} + do_execsql_test 1.$tn.204 { + DELETE FROM t1; + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,5); + INSERT INTO t1(a,b,c,d,e) VALUES(1,NULL,93,4,95) + ON CONFLICT(c) DO UPDATE SET b='c' + ON CONFLICT(a) DO UPDATE SET b='a' + ON CONFLICT(d) DO UPDATE SET b='d' + ON CONFLICT(e) DO UPDATE SET b='e'; + SELECT a,b,c,d,e FROM t1; + } {1 a 3 4 5} + do_execsql_test 1.$tn.210 { + DELETE FROM t1; + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,5); + INSERT INTO t1(a,b,c,d,e) VALUES(1,NULL,93,94,95) + ON CONFLICT(c) DO UPDATE SET b='c' + ON CONFLICT(d) DO UPDATE SET b='d' + ON CONFLICT(a) DO UPDATE SET b='a' + ON CONFLICT(e) DO UPDATE SET b='e'; + SELECT a,b,c,d,e FROM t1; + } {1 a 3 4 5} + do_execsql_test 1.$tn.211 { + DELETE FROM t1; + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,5); + INSERT INTO t1(a,b,c,d,e) VALUES(1,NULL,93,4,95) + ON CONFLICT(c) DO UPDATE SET b='c' + ON CONFLICT(d) DO UPDATE SET b='d' + ON CONFLICT(a) DO UPDATE SET b='a' + ON CONFLICT(e) DO UPDATE SET b='e'; + SELECT a,b,c,d,e FROM t1; + } {1 d 3 4 5} + do_execsql_test 1.$tn.212 { + DELETE FROM t1; + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,5); + INSERT INTO t1(a,b,c,d,e) VALUES(1,NULL,93,94,5) + ON CONFLICT(c) DO UPDATE SET b='c' + ON CONFLICT(d) DO UPDATE SET b='d' + ON CONFLICT(a) DO UPDATE SET b='a' + ON CONFLICT(e) DO UPDATE SET b='e'; + SELECT a,b,c,d,e FROM t1; + } {1 a 3 4 5} + do_execsql_test 1.$tn.213 { + DELETE FROM t1; + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,5); + INSERT INTO t1(a,b,c,d,e) VALUES(91,NULL,93,94,5) + ON CONFLICT(c) DO UPDATE SET b='c' + ON CONFLICT(d) DO UPDATE SET b='d' + ON CONFLICT(a) DO UPDATE SET b='a' + ON CONFLICT(e) DO UPDATE SET b='e'; + SELECT a,b,c,d,e FROM t1; + } {1 e 3 4 5} + do_execsql_test 1.$tn.214 { + DELETE FROM t1; + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,5); + INSERT INTO t1(a,b,c,d,e) VALUES(91,NULL,93,94,5) + ON CONFLICT(c) DO UPDATE SET b='c' + ON CONFLICT(d) DO UPDATE SET b='d' + ON CONFLICT(e) DO UPDATE SET b='e' + ON CONFLICT(a) DO UPDATE SET b='a'; + SELECT a,b,c,d,e FROM t1; + } {1 e 3 4 5} + do_execsql_test 1.$tn.215 { + DELETE FROM t1; + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,5); + INSERT INTO t1(a,b,c,d,e) VALUES(1,NULL,93,94,5) + ON CONFLICT(c) DO UPDATE SET b='c' + ON CONFLICT(d) DO UPDATE SET b='d' + ON CONFLICT(e) DO UPDATE SET b='e' + ON CONFLICT(a) DO UPDATE SET b='a'; + SELECT a,b,c,d,e FROM t1; + } {1 e 3 4 5} + do_execsql_test 1.$tn.216 { + DELETE FROM t1; + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,5); + INSERT INTO t1(a,b,c,d,e) VALUES(1,NULL,93,94,95) + ON CONFLICT(c) DO UPDATE SET b='c' + ON CONFLICT(d) DO UPDATE SET b='d' + ON CONFLICT(e) DO UPDATE SET b='e' + ON CONFLICT(a) DO UPDATE SET b='a'; + SELECT a,b,c,d,e FROM t1; + } {1 a 3 4 5} + + do_execsql_test 1.$tn.300 { + DELETE FROM t1; + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,5); + INSERT INTO t1(a,b,c,d,e) VALUES(1,NULL,93,94,95) + ON CONFLICT(c) DO UPDATE SET b='c' + ON CONFLICT(d) DO UPDATE SET b='d' + ON CONFLICT(a) DO UPDATE SET b='a1' + ON CONFLICT(a) DO UPDATE SET b='a2' + ON CONFLICT(a) DO UPDATE SET b='a3' + ON CONFLICT(a) DO UPDATE SET b='a4' + ON CONFLICT(a) DO UPDATE SET b='a5' + ON CONFLICT(e) DO UPDATE SET b='e'; + SELECT a,b,c,d,e FROM t1; + } {1 a1 3 4 5} + do_execsql_test 1.$tn.301 { + DELETE FROM t1; + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,5); + INSERT INTO t1(a,b,c,d,e) VALUES(91,NULL,93,94,5) + ON CONFLICT(c) DO UPDATE SET b='c' + ON CONFLICT(d) DO UPDATE SET b='d' + ON CONFLICT(a) DO UPDATE SET b='a1' + ON CONFLICT(a) DO UPDATE SET b='a2' + ON CONFLICT(a) DO UPDATE SET b='a3' + ON CONFLICT(a) DO UPDATE SET b='a4' + ON CONFLICT(a) DO UPDATE SET b='a5' + ON CONFLICT(e) DO UPDATE SET b='e'; + SELECT a,b,c,d,e FROM t1; + } {1 e 3 4 5} + + do_execsql_test 1.$tn.400 { + DELETE FROM t1; + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,5); + INSERT INTO t1(a,b,c,d,e) VALUES(1,NULL,93,94,95) + ON CONFLICT(c) DO UPDATE SET b='c' + ON CONFLICT(d) DO UPDATE SET b='d' + ON CONFLICT DO UPDATE set b='x'; + SELECT a,b,c,d,e FROM t1; + } {1 x 3 4 5} + do_execsql_test 1.$tn.401 { + DELETE FROM t1; + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,5); + INSERT INTO t1(a,b,c,d,e) VALUES(91,NULL,93,94,5) + ON CONFLICT(c) DO UPDATE SET b='c' + ON CONFLICT(d) DO UPDATE SET b='d' + ON CONFLICT DO UPDATE set b='x'; + SELECT a,b,c,d,e FROM t1; + } {1 x 3 4 5} + do_execsql_test 1.$tn.402 { + DELETE FROM t1; + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,5); + INSERT INTO t1(a,b,c,d,e) VALUES(1,NULL,93,94,95) + ON CONFLICT(c) DO UPDATE SET b='c' + ON CONFLICT(d) DO UPDATE SET b='d' + ON CONFLICT DO UPDATE set b='x'; + SELECT a,b,c,d,e FROM t1; + } {1 x 3 4 5} + do_execsql_test 1.$tn.403 { + DELETE FROM t1; + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,5); + INSERT INTO t1(a,b,c,d,e) VALUES(91,NULL,3,94,95) + ON CONFLICT(c) DO UPDATE SET b='c' + ON CONFLICT(d) DO UPDATE SET b='d' + ON CONFLICT DO UPDATE set b='x'; + SELECT a,b,c,d,e FROM t1; + } {1 c 3 4 5} + do_execsql_test 1.$tn.404 { + DELETE FROM t1; + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,5); + INSERT INTO t1(a,b,c,d,e) VALUES(91,NULL,3,4,95) + ON CONFLICT(c) DO UPDATE SET b='c' + ON CONFLICT(d) DO UPDATE SET b='d' + ON CONFLICT DO UPDATE set b='x'; + SELECT a,b,c,d,e FROM t1; + } {1 c 3 4 5} + do_execsql_test 1.$tn.405 { + DELETE FROM t1; + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,5); + INSERT INTO t1(a,b,c,d,e) VALUES(1,NULL,93,4,5) + ON CONFLICT(c) DO UPDATE SET b='c' + ON CONFLICT(d) DO UPDATE SET b='d' + ON CONFLICT DO UPDATE set b='x'; + SELECT a,b,c,d,e FROM t1; + } {1 d 3 4 5} + + do_execsql_test 1.$tn.410 { + DELETE FROM t1; + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,5); + INSERT INTO t1(a,b,c,d,e) VALUES(1,NULL,93,94,95) + ON CONFLICT DO UPDATE set b='x'; + SELECT a,b,c,d,e FROM t1; + } {1 x 3 4 5} + do_execsql_test 1.$tn.411 { + DELETE FROM t1; + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,5); + INSERT INTO t1(a,b,c,d,e) VALUES(91,NULL,93,94,5) + ON CONFLICT DO UPDATE set b='x'; + SELECT a,b,c,d,e FROM t1; + } {1 x 3 4 5} + do_execsql_test 1.$tn.412 { + DELETE FROM t1; + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,5); + INSERT INTO t1(a,b,c,d,e) VALUES(91,NULL,93,4,95) + ON CONFLICT DO UPDATE set b='x'; + SELECT a,b,c,d,e FROM t1; + } {1 x 3 4 5} + do_execsql_test 1.$tn.413 { + DELETE FROM t1; + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,5); + INSERT INTO t1(a,b,c,d,e) VALUES(91,NULL,3,94,95) + ON CONFLICT DO UPDATE set b='x'; + SELECT a,b,c,d,e FROM t1; + } {1 x 3 4 5} + + do_execsql_test 1.$tn.420 { + DELETE FROM t1; + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,5); + INSERT INTO t1(a,b,c,d,e) VALUES(1,NULL,93,94,95) + ON CONFLICT(c) DO NOTHING + ON CONFLICT(d) DO NOTHING + ON CONFLICT DO UPDATE set b='x'; + SELECT a,b,c,d,e FROM t1; + } {1 x 3 4 5} + do_execsql_test 1.$tn.421 { + DELETE FROM t1; + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,5); + INSERT INTO t1(a,b,c,d,e) VALUES(91,NULL,93,94,5) + ON CONFLICT(c) DO NOTHING + ON CONFLICT(d) DO NOTHING + ON CONFLICT DO UPDATE set b='x'; + SELECT a,b,c,d,e FROM t1; + } {1 x 3 4 5} + do_execsql_test 1.$tn.422 { + DELETE FROM t1; + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,5); + INSERT INTO t1(a,b,c,d,e) VALUES(91,NULL,93,4,95) + ON CONFLICT(c) DO NOTHING + ON CONFLICT(d) DO NOTHING + ON CONFLICT DO UPDATE set b='x'; + SELECT a,b,c,d,e FROM t1; + } {1 2 3 4 5} + do_execsql_test 1.$tn.423 { + DELETE FROM t1; + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,5); + INSERT INTO t1(a,b,c,d,e) VALUES(91,NULL,3,94,95) + ON CONFLICT(c) DO NOTHING + ON CONFLICT(d) DO NOTHING + ON CONFLICT DO UPDATE set b='x'; + SELECT a,b,c,d,e FROM t1; + } {1 2 3 4 5} + + do_execsql_test 1.$tn.500 { + DELETE FROM t1; + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,5); + INSERT INTO t1(a,b,c,d,e) VALUES(1,NULL,93,94,95) + ON CONFLICT(c) DO UPDATE SET b='c' + ON CONFLICT(d) DO UPDATE SET b='d' + ON CONFLICT DO NOTHING; + SELECT a,b,c,d,e FROM t1; + } {1 2 3 4 5} + do_execsql_test 1.$tn.501 { + DELETE FROM t1; + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,5); + INSERT INTO t1(a,b,c,d,e) VALUES(91,NULL,93,94,5) + ON CONFLICT(c) DO UPDATE SET b='c' + ON CONFLICT(d) DO UPDATE SET b='d' + ON CONFLICT DO NOTHING; + SELECT a,b,c,d,e FROM t1; + } {1 2 3 4 5} + do_execsql_test 1.$tn.502 { + DELETE FROM t1; + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,5); + INSERT INTO t1(a,b,c,d,e) VALUES(1,NULL,93,94,95) + ON CONFLICT(c) DO UPDATE SET b='c' + ON CONFLICT(d) DO UPDATE SET b='d' + ON CONFLICT DO NOTHING; + SELECT a,b,c,d,e FROM t1; + } {1 2 3 4 5} + do_execsql_test 1.$tn.503 { + DELETE FROM t1; + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,5); + INSERT INTO t1(a,b,c,d,e) VALUES(91,NULL,3,94,95) + ON CONFLICT(c) DO UPDATE SET b='c' + ON CONFLICT(d) DO UPDATE SET b='d' + ON CONFLICT DO NOTHING; + SELECT a,b,c,d,e FROM t1; + } {1 c 3 4 5} + do_execsql_test 1.$tn.504 { + DELETE FROM t1; + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,5); + INSERT INTO t1(a,b,c,d,e) VALUES(91,NULL,3,4,95) + ON CONFLICT(c) DO UPDATE SET b='c' + ON CONFLICT(d) DO UPDATE SET b='d' + ON CONFLICT DO NOTHING; + SELECT a,b,c,d,e FROM t1; + } {1 c 3 4 5} + do_execsql_test 1.$tn.505 { + DELETE FROM t1; + INSERT INTO t1(a,b,c,d,e) VALUES(1,2,3,4,5); + INSERT INTO t1(a,b,c,d,e) VALUES(1,NULL,93,4,5) + ON CONFLICT(c) DO UPDATE SET b='c' + ON CONFLICT(d) DO UPDATE SET b='d' + ON CONFLICT DO NOTHING; + SELECT a,b,c,d,e FROM t1; + } {1 d 3 4 5} + +} + +#-------------------------------------------------------------------------- +reset_db +do_execsql_test 2.0 { + CREATE TABLE t2(a, b, c REAL, d, e, PRIMARY KEY(a,b)) WITHOUT ROWID; + CREATE UNIQUE INDEX t2c ON t2(c); +} + +do_catchsql_test 2.1 { + INSERT INTO t2(a,b,c,e,d) VALUES(1,2,3,4,5) + ON CONFLICT(c) DO UPDATE SET b='' + ON CONFLICT((SELECT t2 FROM nosuchtable)) DO NOTHING; + +} {1 {no such table: nosuchtable}} + +# 2024-03-08 https://sqlite.org/forum/forumpost/919c6579c8 +# A redundant ON CONFLICT clause in an upsert can lead to +# index corruption. +# +reset_db +do_execsql_test 3.0 { + CREATE TABLE t1(aa INTEGER PRIMARY KEY, bb INT); + INSERT INTO t1 VALUES(11,22); + CREATE UNIQUE INDEX t1bb ON t1(bb); + REPLACE INTO t1 VALUES(11,33) + ON CONFLICT(bb) DO UPDATE SET aa = 44 + ON CONFLICT(bb) DO UPDATE SET aa = 44; + PRAGMA integrity_check; +} {ok} +do_execsql_test 3.1 { + SELECT * FROM t1 NOT INDEXED; +} {11 33} +do_execsql_test 3.2 { + SELECT * FROM t1 INDEXED BY t1bb; +} {11 33} +do_execsql_test 3.3 { + DROP TABLE t1; + CREATE TABLE t1(aa INTEGER PRIMARY KEY, bb INT, cc INT); + INSERT INTO t1 VALUES(10,21,32),(11,22,33),(12,23,34); + CREATE UNIQUE INDEX t1bb ON t1(bb); + CREATE UNIQUE INDEX t1cc ON t1(cc); + REPLACE INTO t1 VALUES(11,44,55) + ON CONFLICT(bb) DO UPDATE SET aa = 99 + ON CONFLICT(cc) DO UPDATE SET aa = 99 + ON CONFLICT(bb) DO UPDATE SET aa = 99; + PRAGMA integrity_check; +} {ok} +do_execsql_test 3.4 { + SELECT * FROM t1 NOT INDEXED ORDER BY +aa; +} {10 21 32 11 44 55 12 23 34} +do_execsql_test 3.5 { + SELECT * FROM t1 INDEXED BY t1bb ORDER BY +aa; +} {10 21 32 11 44 55 12 23 34} +do_execsql_test 3.6 { + SELECT * FROM t1 INDEXED BY t1cc ORDER BY +aa; +} {10 21 32 11 44 55 12 23 34} + +finish_test diff --git a/test/upsertfault.test b/test/upsertfault.test new file mode 100644 index 0000000000..52bb4a5e35 --- /dev/null +++ b/test/upsertfault.test @@ -0,0 +1,38 @@ +# 2018-04-17 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# Test cases for UPSERT + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix upsertfault + +do_execsql_test 1.0 { + CREATE TABLE t1(a PRIMARY KEY, b, c, d, UNIQUE(b, c)); + INSERT INTO t1 VALUES(1, 1, 1, 1); + INSERT INTO t1 VALUES(2, 2, 2, 2); +} +faultsim_save_and_close + +do_faultsim_test 1 -faults oom* -prep { + faultsim_restore_and_reopen + db eval { SELECT * FROM sqlite_master } +} -body { + execsql { + INSERT INTO t1 VALUES(3, 2, 2, NULL) ON CONFLICT(b, c) DO + UPDATE SET d=d+1; + } +} -test { + faultsim_test_result {0 {}} +} + + +finish_test diff --git a/test/uri.test b/test/uri.test index a0a88d2263..74da225acc 100644 --- a/test/uri.test +++ b/test/uri.test @@ -55,7 +55,11 @@ foreach {tn uri file} { ifcapable !curdir { if {$tn==3} break } - if {$tcl_platform(platform)=="windows"} { + ifcapable uri_00_error { + if {[string first %00 $uri]>=0} continue + } + + if {$tcl_platform(platform) eq "windows"} { # # NOTE: Due to limits on legal characters for file names imposed by # Windows, we must skip the final two tests here (i.e. the @@ -124,6 +128,10 @@ foreach {tn uri kvlist} { 14 http:test.db?hello&world {} } { + ifcapable uri_00_error { + if {[string first %00 $uri]>=0} continue + } + if {$tcl_platform(platform) == "windows" && $tn>12} { continue } @@ -186,6 +194,7 @@ foreach {tn mode create_ok write_ok readonly_ok} { } $A($readonly_ok) } +ifcapable shared_cache { set orig [sqlite3_enable_shared_cache] foreach {tn options sc_default is_shared} { 1 "" 1 1 @@ -215,6 +224,7 @@ foreach {tn options sc_default is_shared} { db2 close } +} ;# end ifcapable shared_cache do_test 4.3.1 { list [catch {sqlite3 db "file:test.db?mode=rc"} msg] $msg @@ -296,7 +306,7 @@ foreach {tn uri res} { 6 "file://x/PWD/test.db" {invalid uri authority: x} } { - if {$tcl_platform(platform)=="windows"} { + if {$tcl_platform(platform) eq "windows"} { set uri [string map [list PWD [string range [get_pwd] 3 end]] $uri] } else { set uri [string map [list PWD [string range [get_pwd] 1 end]] $uri] diff --git a/test/uri2.test b/test/uri2.test new file mode 100644 index 0000000000..52feb7b50f --- /dev/null +++ b/test/uri2.test @@ -0,0 +1,63 @@ +# 2016 October 26 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# Tests for SQLITE_ENABLE_URI_00_ERROR builds. + +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +ifcapable !uri_00_error { + finish_test + return +} + +set testprefix uri2 +db close +sqlite3_shutdown +sqlite3_config_uri 1 + +foreach {tn uri} { + 1 file:test.db%00trailing + 2 file:test.db?%00trailing=1 + 3 file:test.db?trailing=1%00 + 4 file:test.db?trailing=1&abc%00def + 5 file:test.db?trailing=1&abc%00def +} { + do_test 1.$tn.1 { + set rc [catch { sqlite3 db $uri } msg] + list $rc $msg + } {1 {unexpected %00 in uri}} + + do_test 1.$tn.2 { + set DB2 [sqlite3_open $uri] + sqlite3_errcode $DB2 + } {SQLITE_ERROR} + + catch { sqlite3_close $DB2 } + + do_test 1.$tn.2 { + sqlite3 db "" + catchsql { ATTACH $uri AS aux } + } {1 {unexpected %00 in uri}} + + do_test 1.$tn.3 { + sqlite3_errcode db + } {SQLITE_ERROR} + + catch { db close } +} + +reset_db +do_test 2.0 { + expr {[lsearch [execsql {PRAGMA compile_options}] ENABLE_URI_00_ERROR] >= 0} +} 1 + +finish_test diff --git a/test/userauth01.test b/test/userauth01.test deleted file mode 100644 index 644937b192..0000000000 --- a/test/userauth01.test +++ /dev/null @@ -1,257 +0,0 @@ -# 2014-09-10 -# -# The author disclaims copyright to this source code. In place of -# a legal notice, here is a blessing: -# -# May you do good and not evil. -# May you find forgiveness for yourself and forgive others. -# May you share freely, never taking more than you give. -# -#*********************************************************************** -# -# This file implements tests of the SQLITE_USER_AUTHENTICATION extension. -# - -set testdir [file dirname $argv0] -source $testdir/tester.tcl -set testprefix userauth01 - -ifcapable !userauth { - finish_test - return -} - -# Create a no-authentication-required database -# -do_execsql_test userauth01-1.0 { - CREATE TABLE t1(x); - INSERT INTO t1 VALUES(1),(2.5),('three'),(x'4444'),(NULL); - SELECT quote(x) FROM t1 ORDER BY x; - SELECT name FROM sqlite_master; -} {NULL 1 2.5 'three' X'4444' t1} - -# Calling sqlite3_user_authenticate() on a no-authentication-required -# database connection is a harmless no-op. -# -do_test userauth01-1.1 { - sqlite3_user_authenticate db alice pw-4-alice - execsql { - SELECT quote(x) FROM t1 ORDER BY x; - SELECT name FROM sqlite_master; - } -} {NULL 1 2.5 'three' X'4444' t1} - -# If sqlite3_user_add(D,U,P,N,A) is called on a no-authentication-required -# database and A is false, then the call fails with an SQLITE_AUTH error. -# -do_test userauth01-1.2 { - sqlite3_user_add db bob pw-4-bob 0 -} {SQLITE_AUTH} -do_test userauth01-1.3 { - execsql { - SELECT quote(x) FROM t1 ORDER BY x; - SELECT name FROM sqlite_master; - } -} {NULL 1 2.5 'three' X'4444' t1} - -# When called on a no-authentication-required -# database and when A is true, the sqlite3_user_add(D,U,P,N,A) routine -# converts the database into an authentication-required database and -# logs the database connection D in using user U with password P,N. -# -do_test userauth01-1.4 { - sqlite3_user_add db alice pw-4-alice 1 -} {SQLITE_OK} -do_test userauth01-1.5 { - execsql { - SELECT quote(x) FROM t1 ORDER BY x; - SELECT uname, isadmin FROM sqlite_user ORDER BY uname; - SELECT name FROM sqlite_master ORDER BY name; - } -} {NULL 1 2.5 'three' X'4444' alice 1 sqlite_user t1} - -# The sqlite3_user_add() interface can be used (by an admin user only) -# to create a new user. -# -do_test userauth01-1.6 { - sqlite3_user_add db bob pw-4-bob 0 - sqlite3_user_add db cindy pw-4-cindy 0 - sqlite3_user_add db david pw-4-david 0 - execsql { - SELECT uname, isadmin FROM sqlite_user ORDER BY uname; - } -} {alice 1 bob 0 cindy 0 david 0} - -# The sqlite_user table is inaccessible (unreadable and unwriteable) to -# non-admin users and is read-only for admin users. However, if the same -# -do_test userauth01-1.7 { - sqlite3 db2 test.db - sqlite3_user_authenticate db2 cindy pw-4-cindy - db2 eval { - SELECT quote(x) FROM t1 ORDER BY x; - SELECT name FROM sqlite_master ORDER BY name; - } -} {NULL 1 2.5 'three' X'4444' sqlite_user t1} -do_test userauth01-1.8 { - catchsql { - SELECT uname, isadmin FROM sqlite_user ORDER BY uname; - } db2 -} {1 {no such table: sqlite_user}} - -# Any user can change their own password. -# -do_test userauth01-1.9 { - sqlite3_user_change db2 cindy xyzzy-cindy 0 -} {SQLITE_OK} -do_test userauth01-1.10 { - sqlite3_user_authenticate db2 cindy pw-4-cindy -} {SQLITE_AUTH} -do_test userauth01-1.11 { - sqlite3_user_authenticate db2 cindy xyzzy-cindy -} {SQLITE_OK} -do_test userauth01-1.12 { - sqlite3_user_change db alice xyzzy-alice 1 -} {SQLITE_OK} -do_test userauth01-1.13 { - sqlite3_user_authenticate db alice pw-4-alice -} {SQLITE_AUTH} -do_test userauth01-1.14 { - sqlite3_user_authenticate db alice xyzzy-alice -} {SQLITE_OK} - -# No user may change their own admin privilege setting. -# -do_test userauth01-1.15 { - sqlite3_user_change db alice xyzzy-alice 0 -} {SQLITE_AUTH} -do_test userauth01-1.16 { - db eval {SELECT uname, isadmin FROM sqlite_user ORDER BY uname} -} {alice 1 bob 0 cindy 0 david 0} -do_test userauth01-1.17 { - sqlite3_user_change db2 cindy xyzzy-cindy 1 -} {SQLITE_AUTH} -do_test userauth01-1.18 { - db eval {SELECT uname, isadmin FROM sqlite_user ORDER BY uname} -} {alice 1 bob 0 cindy 0 david 0} - -# The sqlite3_user_change() interface can be used to change a users -# login credentials or admin privilege. -# -do_test userauth01-1.20 { - sqlite3_user_change db david xyzzy-david 1 -} {SQLITE_OK} -do_test userauth01-1.21 { - db eval {SELECT uname, isadmin FROM sqlite_user ORDER BY uname} -} {alice 1 bob 0 cindy 0 david 1} -do_test userauth01-1.22 { - sqlite3_user_authenticate db2 david xyzzy-david -} {SQLITE_OK} -do_test userauth01-1.23 { - db2 eval {SELECT uname, isadmin FROM sqlite_user ORDER BY uname} -} {alice 1 bob 0 cindy 0 david 1} -do_test userauth01-1.24 { - sqlite3_user_change db david pw-4-david 0 -} {SQLITE_OK} -do_test userauth01-1.25 { - sqlite3_user_authenticate db2 david pw-4-david -} {SQLITE_OK} -do_test userauth01-1.26 { - db eval {SELECT uname, isadmin FROM sqlite_user ORDER BY uname} -} {alice 1 bob 0 cindy 0 david 0} -do_test userauth01-1.27 { - catchsql {SELECT uname, isadmin FROM sqlite_user ORDER BY uname} db2 -} {1 {no such table: sqlite_user}} - -# Only an admin user can change another users login -# credentials or admin privilege setting. -# -do_test userauth01-1.30 { - sqlite3_user_change db2 bob xyzzy-bob 1 -} {SQLITE_AUTH} -do_test userauth01-1.31 { - db eval {SELECT uname, isadmin FROM sqlite_user ORDER BY uname} -} {alice 1 bob 0 cindy 0 david 0} - -# The sqlite3_user_delete() interface can be used (by an admin user only) -# to delete a user. -# -do_test userauth01-1.40 { - sqlite3_user_delete db bob -} {SQLITE_OK} -do_test userauth01-1.41 { - db eval {SELECT uname, isadmin FROM sqlite_user ORDER BY uname} -} {alice 1 cindy 0 david 0} -do_test userauth01-1.42 { - sqlite3_user_delete db2 cindy -} {SQLITE_AUTH} -do_test userauth01-1.43 { - sqlite3_user_delete db2 alice -} {SQLITE_AUTH} -do_test userauth01-1.44 { - db eval {SELECT uname, isadmin FROM sqlite_user ORDER BY uname} -} {alice 1 cindy 0 david 0} - -# The currently logged-in user cannot be deleted -# -do_test userauth01-1.50 { - sqlite3_user_delete db alice -} {SQLITE_AUTH} -do_test userauth01-1.51 { - db eval {SELECT uname, isadmin FROM sqlite_user ORDER BY uname} -} {alice 1 cindy 0 david 0} - -# When ATTACH-ing new database files to a connection, each newly attached -# database that is an authentication-required database is checked using -# the same username and password as supplied to the main database. If that -# check fails, then the ATTACH command fails with an SQLITE_AUTH error. -# -do_test userauth01-1.60 { - forcedelete test3.db - sqlite3 db3 test3.db - sqlite3_user_add db3 alice xyzzy-alice 1 -} {SQLITE_OK} -do_test userauth01-1.61 { - db3 eval { - CREATE TABLE t3(a,b,c); INSERT INTO t3 VALUES(1,2,3); - SELECT * FROM t3; - } -} {1 2 3} -do_test userauth01-1.62 { - db eval { - ATTACH 'test3.db' AS aux; - SELECT * FROM t1, t3 ORDER BY x LIMIT 1; - DETACH aux; - } -} {{} 1 2 3} -do_test userauth01-1.63 { - sqlite3_user_change db alice pw-4-alice 1 - sqlite3_user_authenticate db alice pw-4-alice - catchsql { - ATTACH 'test3.db' AS aux; - } -} {1 {unable to open database: test3.db}} -do_test userauth01-1.64 { - sqlite3_extended_errcode db -} {SQLITE_AUTH} -do_test userauth01-1.65 { - db eval {PRAGMA database_list} -} {~/test3.db/} - -# The sqlite3_set_authorizer() callback is modified to take a 7th parameter -# which is the username of the currently logged in user, or NULL for a -# no-authentication-required database. -# -proc auth {args} { - lappend ::authargs $args - return SQLITE_OK -} -do_test authuser01-2.1 { - unset -nocomplain ::authargs - db auth auth - db eval {SELECT x FROM t1} - set ::authargs -} {/SQLITE_SELECT {} {} {} {} alice/} - - -finish_test diff --git a/test/utf16align.test b/test/utf16align.test index f026d9575d..ef10bc659d 100644 --- a/test/utf16align.test +++ b/test/utf16align.test @@ -32,6 +32,7 @@ ifcapable !utf16 { do_test utf16align-1.0 { set unaligned_string_counter 0 add_alignment_test_collations [sqlite3_connection_pointer db] + sqlite3_db_config db SQLITE_DBCONFIG_DQS_DML 1 execsql { PRAGMA encoding=UTF16; CREATE TABLE t1( diff --git a/test/vacuum-into.test b/test/vacuum-into.test new file mode 100644 index 0000000000..c041ebe7a7 --- /dev/null +++ b/test/vacuum-into.test @@ -0,0 +1,220 @@ +# 2018-12-07 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is testing the VACUUM INTO statement. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +# If the VACUUM statement is disabled in the current build, skip all +# the tests in this file. +# +ifcapable {!vacuum} { + omit_test vacuum.test {Compiled with SQLITE_OMIT_VACUUM} + finish_test + return +} + +forcedelete out.db +do_execsql_test vacuum-into-100 { + CREATE TABLE t1( + a INTEGER PRIMARY KEY, + b ANY, + c INT AS (b+1), --- See "2024-04-09" block + CHECK( typeof(b)!='integer' OR b>a-5 ) --- comment below + ); + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<100) + INSERT INTO t1(a,b) SELECT x, randomblob(600) FROM c; + CREATE INDEX t1b ON t1(b); + DELETE FROM t1 WHERE a%2; + SELECT count(*), sum(a), sum(length(b)) FROM t1; +} {50 2550 30000} + +# Update 2024-04-09 for forum post eec177d68fe7fa2c. +# +# VACUUM INTO is sensitive to tables holding both generated columns +# and CHECK constraints. +# +# CHECK constraints are ignored for read-only databases in order to save +# memory (see check-in 34ddf02d3d21151b on 2014-05-21). But the xfer +# optimization normally only works if CHECK constraints match between the +# source and destination tables. So the xfer optimization was not +# working for VACUUM INTO when the source was a read-only database and the +# table held CHECK constraints. But if the table has generated columns, +# then the xfer optimization is required or else VACUUM will raise an +# error. +# +# Fix this by ignoring CHECK constraints when determining whether or not +# the xfer optimization can run while doing VACUUM. + +do_execsql_test vacuum-into-110 { + VACUUM main INTO 'out.db'; +} {} +sqlite3 db2 out.db +do_test vacuum-into-120 { + db2 eval {SELECT count(*), sum(a), sum(length(b)) FROM t1} +} {50 2550 30000} +do_catchsql_test vacuum-into-130 { + VACUUM INTO 'out.db'; +} {1 {output file already exists}} +forcedelete out2.db +do_catchsql_test vacuum-into-140 { + VACUUM INTO 'out2.db'; +} {0 {}} +do_catchsql_test vacuum-into-150 { + VACUUM INTO 'out2.db'; +} {1 {output file already exists}} + +do_catchsql_test vacuum-into-200 { + VACUUM main INTO ':memory:'; +} {0 {}} + +# The INTO argument can be an arbitrary expression. +# +do_execsql_test vacuum-into-300 { + CREATE TABLE t2(name TEXT); + INSERT INTO t2 VALUES(':memory:'); + VACUUM main INTO (SELECT name FROM t2); +} {} +do_catchsql_test vacuum-into-310 { + VACUUM INTO null; +} {1 {non-text filename}} +do_catchsql_test vacuum-into-320 { + VACUUM INTO x; +} {1 {no such column: x}} +do_catchsql_test vacuum-into-330 { + VACUUM INTO t1.nosuchcol; +} {1 {no such column: t1.nosuchcol}} +do_catchsql_test vacuum-into-340 { + VACUUM INTO main.t1.nosuchcol; +} {1 {no such column: main.t1.nosuchcol}} + +forcedelete test.db2 +db func target target +proc target {} { return "test.db2" } +do_test vacuum-into-410 { + execsql { VACUUM INTO target() } + file exists test.db2 +} 1 +do_catchsql_test vacuum-into-420 { + VACUUM INTO target2() +} {1 {no such function: target2}} + +# The ability to VACUUM INTO a read-only database +db close +if {$tcl_platform(platform) eq "windows"} { + file attributes test.db -readonly 1 +} else { + file attributes test.db -permissions 292 ;# 292 == 0444 +} +sqlite3 db test.db -readonly 1 +forcedelete test.db2 +do_execsql_test vacuum-into-500 { + VACUUM INTO 'test.db2'; +} +if {$tcl_platform(platform) eq "windows"} { + file attributes test.db -readonly 0 +} else { + file attributes test.db -permissions 420 ;# 420 = 0644 +} +sqlite3 db2 test.db2 +do_test vacuum-into-510 { + db2 eval {SELECT name FROM sqlite_master ORDER BY 1} +} {t1 t1b t2} +db2 close +db close + +# Change the page-size on a VACUUM INTO even if the original +# database is in WAL mode. +# +if {[wal_is_capable]} { + forcedelete test.db + forcedelete test.db2 + do_test vacuum-into-600 { + sqlite3 db test.db + db eval { + PRAGMA page_size=4096; + PRAGMA journal_mode=WAL; + CREATE TABLE t1(a); + INSERT INTO t1 VALUES(19); + CREATE INDEX t1a ON t1(a); + PRAGMA integrity_check; + } + } {wal ok} + do_execsql_test vacuum-into-610 { + PRAGMA page_size; + } {4096} + do_execsql_test vacuum-into-620 { + PRAGMA page_size=1024; + VACUUM INTO 'test.db2'; + } {} + do_test vacuum-into-630 { + sqlite3 db test.db2 + db eval { + PRAGMA page_size; + PRAGMA integrity_check; + } + } {1024 ok} +} + +#------------------------------------------------------------------------- + +testvfs tvfs -default 1 +tvfs filter xSync +tvfs script xSyncCb +proc xSyncCb {method file fileid flags} { + incr ::sync($flags) +} + +reset_db + +do_execsql_test vacuum-into-700 { + CREATE TABLE t1(a, b); + INSERT INTO t1 VALUES(1, 2); +} + +foreach {tn pragma res} { + 710 { + PRAGMA synchronous = normal + } {normal 2} + 720 { + PRAGMA synchronous = full + } {normal 3} + 730 { + PRAGMA synchronous = off + } {} + 740 { + PRAGMA synchronous = extra; + } {normal 3} + 750 { + PRAGMA fullfsync = 1; + PRAGMA synchronous = full; + } {full|dataonly 1 full 2} +} { + + forcedelete test.db2 + array unset ::sync + do_execsql_test vacuum-into-$tn.1 " + $pragma ; + VACUUM INTO 'test.db2' + " + + do_test vacuum-into-$tn.2 { + array get ::sync + } $res +} + +db close +tvfs delete + + +finish_test diff --git a/test/vacuum.test b/test/vacuum.test index 57429c29ea..82dd00d09c 100644 --- a/test/vacuum.test +++ b/test/vacuum.test @@ -401,4 +401,25 @@ do_test vacuum-10.1 { } {} do_test vacuum-10.2 { execsql VACUUM } {} +# Verify that VACUUM still works if ATTACH is disabled. +# +do_execsql_test vacuum-11.1 { + PRAGMA page_size=1024; + VACUUM; + PRAGMA page_size; +} {1024} +sqlite3_db_config db ATTACH_CREATE 0 +do_execsql_test vacuum-11.2 { + PRAGMA page_size=2048; + VACUUM; + PRAGMA page_size; +} {2048} +sqlite3_db_config db ATTACH_CREATE 1 +sqlite3_db_config db ATTACH_WRITE 0 +do_execsql_test vacuum-11.3 { + PRAGMA page_size=4096; + VACUUM; + PRAGMA page_size; +} {4096} + finish_test diff --git a/test/vacuum2.test b/test/vacuum2.test index 0350c8ec42..89fdb506c5 100644 --- a/test/vacuum2.test +++ b/test/vacuum2.test @@ -56,7 +56,7 @@ do_test vacuum2-2.1 { } hexio_get_int [hexio_read test.db 24 4] } [expr {[hexio_get_int [hexio_read test.db 24 4]]+3}] -do_test vacuum2-2.1 { +do_test vacuum2-2.2 { execsql { VACUUM } diff --git a/test/vacuum3.test b/test/vacuum3.test index 484a7d448a..e6a1c2b9ec 100644 --- a/test/vacuum3.test +++ b/test/vacuum3.test @@ -81,7 +81,17 @@ do_test vacuum3-2.1 { execsql { PRAGMA page_size = 1024; VACUUM; - ALTER TABLE t1 ADD COLUMN d; + } + ifcapable altertable { + execsql { ALTER TABLE t1 ADD COLUMN d; } + } else { + execsql { + DROP TABLE t1; + CREATE TABLE t1(a, b, c, d); + INSERT INTO t1 VALUES(1, 2, 3, NULL); + } + } + execsql { UPDATE t1 SET d = randomblob(1000); } file size test.db diff --git a/test/vacuum4.test b/test/vacuum4.test index 326d037276..bbf0dd4379 100644 --- a/test/vacuum4.test +++ b/test/vacuum4.test @@ -65,3 +65,5 @@ do_test vacuum4-1.1 { VACUUM; } } {} + +finish_test diff --git a/test/vacuum5.test b/test/vacuum5.test new file mode 100644 index 0000000000..f203fb87ba --- /dev/null +++ b/test/vacuum5.test @@ -0,0 +1,155 @@ +# 2016-08-19 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# This file implements a test for VACUUM on attached databases. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix vacuum5 + +# If the VACUUM statement is disabled in the current build, skip all +# the tests in this file. +# +ifcapable !vacuum { + finish_test + return +} + +forcedelete test2.db test3.db +do_execsql_test vacuum5-1.1 { + PRAGMA auto_vacuum = 0; + CREATE TABLE main.t1(a,b); + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<1000) + INSERT INTO t1(a,b) SELECT x, randomblob(1000) FROM c; + CREATE TEMP TABLE ttemp(x,y); + INSERT INTO ttemp SELECT * FROM t1; + ATTACH 'test2.db' AS x2; + ATTACH 'test3.db' AS x3; + CREATE TABLE x2.t2(c,d); + INSERT INTO t2 SELECT * FROM t1; + CREATE TABLE x3.t3(e,f); + INSERT INTO t3 SELECT * FROM t1; + DELETE FROM t1 WHERE (rowid%3)!=0; + DELETE FROM t2 WHERE (rowid%4)!=0; + DELETE FROM t3 WHERE (rowid%5)!=0; + PRAGMA main.integrity_check; + PRAGMA x2.integrity_check; + PRAGMA x3.integrity_check; +} {ok ok ok} +set size1 [file size test.db] +set size2 [file size test2.db] +set size3 [file size test3.db] + +do_execsql_test vacuum5-1.2.1 { + VACUUM main; +} {} +do_test vacuum5-1.2.2 { + expr {[file size test.db]<$size1} +} {1} +do_test vacuum5-1.2.3 { + file size test2.db +} $size2 +do_test vacuum5-1.2.4 { + file size test3.db +} $size3 +set size1 [file size test.db] +do_execsql_test vacuum-1.2.5 { + DELETE FROM t1; + PRAGMA main.integrity_check; +} {ok} + +do_execsql_test vacuum5-1.3.1 { + VACUUM x2; +} {} +do_test vacuum5-1.3.2 { + file size test.db +} $size1 +do_test vacuum5-1.3.3 { + expr {[file size test2.db]<$size2} +} 1 +do_test vacuum5-1.3.4 { + file size test3.db +} $size3 +set size2 [file size test2.db] +do_execsql_test vacuum-1.3.5 { + DELETE FROM t2; + PRAGMA x2.integrity_check; +} {ok} + +do_execsql_test vacuum5-1.4.1 { + VACUUM x3; +} {} +do_test vacuum5-1.3.2 { + file size test.db +} $size1 +do_test vacuum5-1.3.3 { + file size test2.db +} $size2 +do_test vacuum5-1.3.4 { + expr {[file size test3.db]<$size3} +} 1 + +# VACUUM is a no-op on the TEMP table +# +set sizeTemp [db one {PRAGMA temp.page_count}] +do_execsql_test vacuum5-1.4.1 { + VACUUM temp; +} {} +do_execsql_test vacuum5-1.4.2 { + PRAGMA temp.page_count; +} $sizeTemp + +do_catchsql_test vacuum5-2.0 { + VACUUM olaf; +} {1 {unknown database olaf}} + +#------------------------------------------------------------------------- +# Test that a temp file is opened as part of VACUUM. +# +if {$::TEMP_STORE<3 && [permutation]!="inmemory_journal"} { + db close + testvfs tvfs + tvfs filter xOpen + tvfs script open_cb + forcedelete test.db + + set ::openfiles [list] + proc open_cb {method args} { + lappend ::openfiles [file tail [lindex $args 0]] + } + sqlite3 db test.db -vfs tvfs + + do_execsql_test 3.0 { + PRAGMA temp_store = file; + PRAGMA page_size = 1024; + PRAGMA cache_size = 50; + CREATE TABLE t1(i INTEGER PRIMARY KEY, j UNIQUE); + WITH s(i) AS ( + VALUES(1) UNION ALL SELECT i+1 FROM s WHERE i<1000 + ) + INSERT INTO t1 SELECT NULL, randomblob(100) FROM s; + } + + do_execsql_test 3.1 { VACUUM } + + db close + tvfs delete + if {[atomic_batch_write test.db]==0} { + do_test 3.2 { + lrange $::openfiles 0 4 + } {test.db test.db-journal test.db-journal {} test.db-journal} + } +} + + + +finish_test diff --git a/test/vacuum6.test b/test/vacuum6.test new file mode 100644 index 0000000000..f80ff75462 --- /dev/null +++ b/test/vacuum6.test @@ -0,0 +1,113 @@ +# 2016-08-19 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# This file implements a test for VACUUM on attached databases. +# +# TESTRUNNER: slow + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix vacuum6 + +# If the VACUUM statement is disabled in the current build, skip all +# the tests in this file. +# +ifcapable !vacuum { + finish_test + return +} + + +do_execsql_test 1.0 { + CREATE TABLE t1(x INTEGER PRIMARY KEY, y); + INSERT INTO t1 VALUES(1, 1); +} {} + +do_execsql_test 1.1 { + VACUUM +} + +reset_db +do_execsql_test 1.2 { + CREATE TABLE t1(x,b); + CREATE INDEX x1 ON t1(x); + CREATE INDEX x2 ON t1(x); + CREATE INDEX x3 ON t1(x); + INSERT INTO t1 SELECT 2,''; + VACUUM; +} + +#------------------------------------------------------------------------- +# +reset_db +foreach {tn sz} {1 400 2 4000 3 9999} { + reset_db + do_execsql_test 2.$tn.1 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b); + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<100 + ) + INSERT INTO t1 SELECT i, randomblob($sz) FROM s; + } + + do_execsql_test 2.$tn.2 { + vacuum; + } + + do_execsql_test 2.$tn.3 { + PRAGMA integrity_check; + } {ok} +} + +reset_db +do_execsql_test 3.0 { + PRAGMA page_size = 1024; + CREATE TABLE t1(x INTEGER PRIMARY KEY, y); + INSERT INTO t1 VALUES(2, randomblob(1200)); +} {} +do_execsql_test 3.1 { + PRAGMA page_size = 512; + VACUUM; +} +do_execsql_test 3.2 { + PRAGMA integrity_check +} {ok} + +#------------------------------------------------------------------------- +# +reset_db +do_execsql_test 4.0 { + CREATE TABLE tx(a, b); + CREATE INDEX i1 ON tx(b); + WITH s(i) AS ( + SELECT 8000 UNION ALL SELECT i+1 FROM s WHERE i<10000 + ) + INSERT INTO tx SELECT i, randomblob(i) FROM s; + + SELECT sum(length(b)) FROM tx; +} {18009000} +foreach {tn pgsz av} { + 1 2048 0 + 2 1024 1 + 3 65536 0 + 4 8192 1 + 5 512 0 + 6 4096 1 +} { + do_execsql_test 4.1.$tn.1 " + PRAGMA page_size = $pgsz; + PRAGMA auto_vacuum = $av; + " + do_execsql_test 4.1.$tn.2 VACUUM + integrity_check 4.1.$tn.3 +} + +finish_test diff --git a/test/vacuummem.test b/test/vacuummem.test new file mode 100644 index 0000000000..9668b0ea90 --- /dev/null +++ b/test/vacuummem.test @@ -0,0 +1,74 @@ +# 2005 February 15 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is testing that the VACUUM statement correctly +# frees any memory used for a temporary cache. +# +# TESTRUNNER: slow + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix vacuummem + +if {[permutation]=="memsubsys1"} { + finish_test + return +} + +# If ENABLE_MEMORY_MANAGEMENT is defined, when VACUUM is run the temp db +# is able to borrow space from the main db (and it does, because the +# temp db is configure with a very small cache). When the VACUUM is +# finished and the temp db closed, all the page-cache memory currently +# assigned to the temp db is freed. If ENABLE_MEMORY_MANAGEMENT is defined +# this causes the total memory usage to drop much more than expected, +# causing tests in this file to fail. +# +ifcapable memorymanage { + finish_test + return +} + + +proc memory_used {} { + set stat [sqlite3_status SQLITE_STATUS_MEMORY_USED 1] + lindex $stat 1 +} + +do_execsql_test 1.0 { + PRAGMA cache_size = -2000; + CREATE TABLE t1(a, b, c); + + WITH r(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM r WHERE i<100000 + ) + INSERT INTO t1 SELECT randomblob(100),randomblob(100),randomblob(100) FROM r; + + CREATE INDEX t1a ON t1(a); + CREATE INDEX t1b ON t1(b); + CREATE INDEX t1c ON t1(c); +} +set ans "#/[memory_used]/" + +do_test 1.1 { memory_used } $ans + +do_execsql_test 1.2 VACUUM + +do_test 1.3 { memory_used } $ans + +do_execsql_test 1.4 { + SELECT count(*) FROM t1 WHERE +a IS NOT NULL +} {100000} + +do_test 1.5 { memory_used } $ans + + + +finish_test diff --git a/test/values.test b/test/values.test new file mode 100644 index 0000000000..c3c52ceb1e --- /dev/null +++ b/test/values.test @@ -0,0 +1,715 @@ +# 2024 March 3 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix values + + +do_execsql_test 1.0 { + CREATE TABLE x1(a, b, c); +} + + +explain_i { + INSERT INTO x1(a, b, c) VALUES(1, 1, 1), (2, 2, 2), (3, 3, 3), (4, 4, 4); +} +do_execsql_test 1.1.1 { + INSERT INTO x1 VALUES(1, 1, 1), (2, 2, 2), (3, 3, 3), (4, 4, 4); +} +do_execsql_test 1.1.2 { + SELECT * FROM x1; +} { + 1 1 1 + 2 2 2 + 3 3 3 + 4 4 4 +} + +do_execsql_test 1.2.0 { + DELETE FROM x1 +} +do_execsql_test 1.2.1 { + INSERT INTO x1 VALUES(1, 1, 1), (2, 2, 2), (3, 3, 3) UNION ALL SELECT 4, 4, 4; + SELECT * FROM x1; +} {1 1 1 2 2 2 3 3 3 4 4 4} + +sqlite3_limit db SQLITE_LIMIT_COMPOUND_SELECT 4 + +do_execsql_test 1.2.2 { + DELETE FROM x1; + INSERT INTO x1 + VALUES(1, 1, 1), (2, 2, 2), (3, 3, 3), (4, 4, 4), (5, 5, 5) + UNION ALL SELECT 6, 6, 6; + SELECT * FROM x1; +} {1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6} + +do_execsql_test 1.2.3 { + DELETE FROM x1; + INSERT INTO x1 + VALUES(1, 1, 1), (2, 2, 2), (3, 3, 3), (4, 4, 4) + UNION ALL SELECT 6, 6, 6; + SELECT * FROM x1; +} {1 1 1 2 2 2 3 3 3 4 4 4 6 6 6} + +do_execsql_test 1.2.4 { + DELETE FROM x1; + INSERT INTO x1 VALUES(1, 1, 1), (2, 2, 2), (3, 3, 3) UNION ALL SELECT 6, 6, 6; + SELECT * FROM x1; +} { + 1 1 1 + 2 2 2 + 3 3 3 + 6 6 6 +} + +set a 4 +set b 5 +set c 6 +do_execsql_test 1.2.5 { + DELETE FROM x1; + INSERT INTO x1 + VALUES(1, 1, 1), (2, 2, 2), (3, 3, 3), + (4, 4, $a), (5, 5, $b), (6, 6, $c) +} + +do_execsql_test 1.2.6 { + SELECT * FROM x1; +} { + 1 1 1 + 2 2 2 + 3 3 3 + 4 4 4 + 5 5 5 + 6 6 6 +} + +#------------------------------------------------------------------------- +# SQLITE_LIMIT_COMPOUND_SELECT set to 0. +# +reset_db + +do_execsql_test 2.0 { + CREATE TABLE x1(a, b, c); +} + +sqlite3_limit db SQLITE_LIMIT_COMPOUND_SELECT 3 + +do_catchsql_test 2.1.1 { + INSERT INTO x1 VALUES + (1, 1, 1), + (2, 2, 2), + (3, 3, 3), + (4, 4, 4), + (5, 5, 5), + (6, 6, 6), + (7, 7, 7), + (8, 8, 8), + (9, 9, 9), + (10, 10, 10, 10) +} {1 {all VALUES must have the same number of terms}} + +do_catchsql_test 2.1.2 { + INSERT INTO x1 VALUES + (1, 1, 1), + (2, 2, 2, 2), + (3, 3, 3), + (4, 4, 4), + (5, 5, 5), + (6, 6, 6), + (7, 7, 7), + (8, 8, 8), + (9, 9, 9), + (10, 10, 10) +} {1 {all VALUES must have the same number of terms}} + +sqlite3_limit db SQLITE_LIMIT_COMPOUND_SELECT 0 + +do_execsql_test 2.2 { + INSERT INTO x1 VALUES + (1, 1, 1), + (2, 2, 2), + (3, 3, 3), + (4, 4, 4), + (5, 5, 5), + (6, 6, 6), + (7, 7, 7), + (8, 8, 8), + (9, 9, 9), + (10, 10, 10) +} {} +do_execsql_test 2.3 { + INSERT INTO x1 VALUES + (1, 1, 1), + (2, 2, 2), + (3, 3, 3), + (4, 4, 4), + (5, 5, 5), + (6, 6, 6), + (7, 7, 7), + (8, 8, 8), + (9, 9, 9), + (10, 10, 10) + UNION ALL + SELECT 5, 12, 12 + ORDER BY 1 +} {} + +#------------------------------------------------------------------------- +reset_db + +do_execsql_test 3.0 { + CREATE TABLE y1(x, y); +} + +do_execsql_test 3.1.1 { + DELETE FROM y1; + INSERT INTO y1 VALUES(1, 2), (3, 4), (row_number() OVER (), 5); +} +do_execsql_test 3.1.2 { + SELECT * FROM y1; +} {1 2 3 4 1 5} +do_execsql_test 3.2.1 { + DELETE FROM y1; + INSERT INTO y1 VALUES(1, 2), (3, 4), (row_number() OVER (), 6) + , (row_number() OVER (), 7) +} +do_execsql_test 3.1.2 { + SELECT * FROM y1; +} {1 2 3 4 1 6 1 7} + +#------------------------------------------------------------------------- +reset_db + +do_execsql_test 4.0 { + CREATE TABLE x1(a PRIMARY KEY, b) WITHOUT ROWID; +} + +foreach {tn iLimit} {1 0 2 3} { + sqlite3_limit db SQLITE_LIMIT_COMPOUND_SELECT $iLimit + + do_execsql_test 4.1.1 { + DELETE FROM x1; + INSERT INTO x1 VALUES + (1, 1), + (2, (SELECT * FROM (VALUES('a'), ('b'), ('c'), ('d')) )) + } + do_execsql_test 4.1.2 { + SELECT * FROM x1 + } {1 1 2 a} + + do_execsql_test 4.2.1 { + DELETE FROM x1; + INSERT INTO x1 VALUES + (1, 1), + (2, 2), + (3, 3), + (4, 4), + (5, (SELECT * FROM (VALUES('a'), ('b'), ('c'), ('d')) )) + } + do_execsql_test 4.2.2 { + SELECT * FROM x1 + } {1 1 2 2 3 3 4 4 5 a} + + do_execsql_test 4.3.1 { + DELETE FROM x1; + INSERT INTO x1 VALUES + (1, (SELECT * FROM (VALUES('a'), ('b'), ('c'), ('d'), ('e')) )) + } + do_execsql_test 4.3.2 { + SELECT * FROM x1 + } {1 a} +} + +#------------------------------------------------------------------------ +reset_db + +do_execsql_test 5.0 { + CREATE VIEW v1 AS VALUES(1, 2, 3), (4, 5, 6), (7, 8, 9); +} +do_execsql_test 5.1 { + SELECT * FROM v1 +} {1 2 3 4 5 6 7 8 9} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 6.0 { + CREATE TABLE t1(x); + INSERT INTO t1 VALUES(1), (2); +} + +do_execsql_test 6.1 { + SELECT ( VALUES( x ), ( x ) ) FROM t1; +} {1 2} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 6.0 { + CREATE TABLE t1(x); + INSERT INTO t1 VALUES('x'), ('y'); +} + +do_execsql_test 6.1 { + SELECT * FROM t1, (VALUES(1), (2)) +} {x 1 x 2 y 1 y 2} + +do_execsql_test 6.2 { + VALUES(CAST(44 AS REAL)),(55); +} {44.0 55} + +#------------------------------------------------------------------------ +do_execsql_test 7.1 { + WITH x1(a, b) AS ( + VALUES(1, 2), ('a', 'b') + ) + SELECT * FROM x1 one, x1 two +} { + 1 2 1 2 + 1 2 a b + a b 1 2 + a b a b +} + +#------------------------------------------------------------------------- +reset_db + +set VVV { + ( VALUES('a', 'b'), ('c', 'd'), (123, NULL) ) +} +set VVV2 { + ( + SELECT 'a' AS column1, 'b' AS column2 + UNION ALL SELECT 'c', 'd' UNION ALL SELECT 123, NULL + ) +} + +do_execsql_test 8.0 { + CREATE TABLE t1(x); + INSERT INTO t1 VALUES('d'), (NULL), (123) +} +foreach {tn q res} { + 1 "SELECT * FROM t1 LEFT JOIN VVV" { + d a b d c d d 123 {} + {} a b {} c d {} 123 {} + 123 a b 123 c d 123 123 {} + } + + 2 "SELECT * FROM t1 LEFT JOIN VVV ON (column1=x)" { + d {} {} + {} {} {} + 123 123 {} + } + + 3 "SELECT * FROM t1 RIGHT JOIN VVV" { + d a b d c d d 123 {} + {} a b {} c d {} 123 {} + 123 a b 123 c d 123 123 {} + } + + 4 "SELECT * FROM t1 RIGHT JOIN VVV ON (column1=x)" { + 123 123 {} + {} a b + {} c d + } + + 5 "SELECT * FROM t1 FULL OUTER JOIN VVV ON (column1=x)" { + d {} {} + {} {} {} + 123 123 {} + {} a b + {} c d + } + + 6 "SELECT count(*) FROM VVV" { 3 } + + 7 "SELECT (SELECT column1 FROM VVV)" { a } + + 8 "SELECT * FROM VVV UNION ALL SELECT * FROM VVV" { + a b c d 123 {} + a b c d 123 {} + } + + 9 "SELECT * FROM VVV INTERSECT SELECT * FROM VVV" { + 123 {} a b c d + } + + 10 "SELECT * FROM VVV eXCEPT SELECT * FROM VVV" { } + + 11 "SELECT * FROM VVV eXCEPT SELECT 'a', 'b'" { 123 {} c d } + +} { + set q1 [string map [list VVV $VVV] $q] + set q2 [string map [list VVV $VVV2] $q] + set q3 "WITH VVV AS $VVV $q" + + do_execsql_test 8.1.$tn.1 $q1 $res + do_execsql_test 8.1.$tn.2 $q2 $res + do_execsql_test 8.1.$tn.3 $q3 $res +} + +#------------------------------------------------------------------------- +reset_db + +do_execsql_test 9.1 { + VALUES(456), (123), (NULL) UNION ALL SELECT 122 ORDER BY 1 +} { {} 122 123 456 } + +do_execsql_test 9.2 { + VALUES (1, 2), (3, 4), ( + ( SELECT column1 FROM ( VALUES (5, 6), (7, 8) ) ), + ( SELECT max(column2) FROM ( VALUES (5, 1), (7, 6) ) ) + ) +} { 1 2 3 4 5 6 } + +do_execsql_test 10.1 { + CREATE TABLE a2(a, b, c DEFAULT 'xyz'); +} +do_execsql_test 10.2 { + INSERT INTO a2(a) VALUES(3),(4); +} + +#------------------------------------------------------------------------- +reset_db +ifcapable fts3 { + do_execsql_test 11.0 { + CREATE VIRTUAL TABLE ft USING fts3(x); + } + do_execsql_test 11.1 { + INSERT INTO ft VALUES('one'), ('two'); + } +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 12.0 { + CREATE TABLE t1(a, b); +} +do_execsql_test 12.1 { + INSERT INTO t1 SELECT 1, 2 UNION ALL VALUES(3, 4), (5, 6); +} +do_execsql_test 12.2 { + SELECT * FROM t1 +} {1 2 3 4 5 6} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 13.0 { + CREATE TABLE t1(x); + INSERT INTO t1 VALUES('xyz'); + + SELECT ( + VALUES( (max(substr('abc', 1, 1), x)) ), + (123), + (456) + ) + FROM t1; +} {xyz} + +do_catchsql_test 13.1 { + VALUES(300), (zeroblob(300) OVER win); +} {1 {zeroblob() may not be used as a window function}} + +#-------------------------------------------------------------------------- +reset_db +do_execsql_test 14.1 { + PRAGMA encoding = utf16; + CREATE TABLE t1(a, b); +} {} + +db close +sqlite3 db test.db + +do_execsql_test 14.2 { + INSERT INTO t1 VALUES + (17, 'craft'), + (16, 'urtlek' IN(1,2,3)); +} + +#-------------------------------------------------------------------------- +# +reset_db +do_eqp_test 15.1 { + VALUES(1),(2),(3),(4),(5); +} { + QUERY PLAN + `--SCAN 5-ROW VALUES CLAUSE +} +do_execsql_test 15.2 { + CREATE TABLE t1(a,b); +} +do_eqp_test 15.3 { + INSERT INTO t1 VALUES + (1,2),(3,4),(7,8); +} { + QUERY PLAN + `--SCAN 3-ROW VALUES CLAUSE +} +do_eqp_test 15.4 { + INSERT INTO t1 VALUES + (1,2),(3,4),(7,8), + (5,row_number()OVER()); +} { + QUERY PLAN + `--COMPOUND QUERY + |--LEFT-MOST SUBQUERY + | `--SCAN 3-ROW VALUES CLAUSE + `--UNION ALL + |--CO-ROUTINE (subquery-xxxxxx) + | `--SCAN CONSTANT ROW + `--SCAN (subquery-xxxxxx) +} +do_eqp_test 15.5 { + SELECT * FROM (VALUES(1),(2),(3),(4),(5),(6)), (VALUES('a'),('b'),('c')); +} { + QUERY PLAN + |--SCAN 6-ROW VALUES CLAUSE + `--SCAN 3-ROW VALUES CLAUSE +} +do_execsql_test 15.6 { + CREATE TABLE t2(x,y); +} +do_eqp_test 15.7 { + SELECT * FROM t2 UNION ALL VALUES(1,2),(3,4),(5,6),(7,8); +} { + QUERY PLAN + `--COMPOUND QUERY + |--LEFT-MOST SUBQUERY + | `--SCAN t2 + `--UNION ALL + `--SCAN 4-ROW VALUES CLAUSE +} + +#-------------------------------------------------------------------------- +# The VALUES-as-coroutine optimization can be applied to later rows of +# a VALUES clause even if earlier rows do not qualify. +# +reset_db +do_execsql_test 16.1 { + CREATE TABLE t1(a,b); +} +do_execsql_test 16.2 { + BEGIN; + INSERT INTO t1 VALUES(1,2),(3,4),(5,6), + (7,row_number()OVER()), + (9,10), (11,12), (13,14), (15,16); + SELECT * FROM t1 ORDER BY a, b; + ROLLBACK; +} {1 2 3 4 5 6 7 1 9 10 11 12 13 14 15 16} +do_eqp_test 16.3 { + INSERT INTO t1 VALUES(1,2),(3,4),(5,6), + (7,row_number()OVER()), + (9,10), (11,12), (13,14), (15,16); +} { + QUERY PLAN + `--COMPOUND QUERY + |--LEFT-MOST SUBQUERY + | `--SCAN 3-ROW VALUES CLAUSE + |--UNION ALL + | |--CO-ROUTINE (subquery-xxxxxx) + | | `--SCAN CONSTANT ROW + | `--SCAN (subquery-xxxxxx) + `--UNION ALL + `--SCAN 4-ROW VALUES CLAUSE +} +do_execsql_test 16.4 { + BEGIN; + INSERT INTO t1 VALUES + (1,row_number()OVER()), + (2,3), (4,5), (6,7); + SELECT * FROM t1 ORDER BY a, b; + ROLLBACK; +} {1 1 2 3 4 5 6 7} +do_eqp_test 16.5 { + INSERT INTO t1 VALUES + (1,row_number()OVER()), + (2,3), (4,5), (6,7); +} { + QUERY PLAN + `--COMPOUND QUERY + |--LEFT-MOST SUBQUERY + | |--CO-ROUTINE (subquery-xxxxxx) + | | `--SCAN CONSTANT ROW + | `--SCAN (subquery-xxxxxx) + `--UNION ALL + `--SCAN 3-ROW VALUES CLAUSE +} +do_execsql_test 16.6 { + BEGIN; + INSERT INTO t1 VALUES + (1,2),(3,4), + (5,row_number()OVER()), + (7,8),(9,10),(11,12), + (13,row_number()OVER()), + (15,16),(17,18),(19,20),(21,22); + SELECT * FROM t1 ORDER BY a, b; + ROLLBACK; +} { 1 2 3 4 5 1 7 8 9 10 11 12 13 1 15 16 17 18 19 20 21 22} +do_eqp_test 16.7 { + INSERT INTO t1 VALUES + (1,2),(3,4), + (5,row_number()OVER()), + (7,8),(9,10),(11,12), + (13,row_number()OVER()), + (15,16),(17,18),(19,20),(21,22); +} { + QUERY PLAN + `--COMPOUND QUERY + |--LEFT-MOST SUBQUERY + | `--SCAN 2-ROW VALUES CLAUSE + |--UNION ALL + | |--CO-ROUTINE (subquery-xxxxxx) + | | `--SCAN CONSTANT ROW + | `--SCAN (subquery-xxxxxx) + |--UNION ALL + | `--SCAN 3-ROW VALUES CLAUSE + |--UNION ALL + | |--CO-ROUTINE (subquery-xxxxxx) + | | `--SCAN CONSTANT ROW + | `--SCAN (subquery-xxxxxx) + `--UNION ALL + `--SCAN 4-ROW VALUES CLAUSE +} + +#-------------------------------------------------------------------------- +# 2024-03-23 dbsqlfuzz crash-c2c5e7e08b7e489d270a26d895077a03f678c33b +# +do_execsql_test 17.1 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1 AS SELECT * FROM (VALUES(1,2), (3,4 IN (1,2,3))); +} + +do_execsql_test 17.2 { + SELECT * FROM t1 +} {1 2 3 0} + +# 2024-03-25 dbsqlfuzz crash-74cf7c9904360322a6c917e4934b127543d1cd51 +# +do_catchsql_test 18.1 { + DROP TABLE t1; + CREATE TABLE t1(x INTEGER PRIMARY KEY); + INSERT INTO t1 VALUES(RAISE(IGNORE)),(0); +} {1 {RAISE() may only be used within a trigger-program}} +do_catchsql_test 18.2 { + DROP TABLE t1; + CREATE TABLE t1(x INTEGER PRIMARY KEY, y, z); + CREATE TRIGGER r2 AFTER INSERT ON t1 BEGIN + INSERT INTO t1(y) VALUES(RAISE(IGNORE)),(0); + END; + INSERT INTO t1 VALUES(1,2,3); + SELECT * FROM t1; +} {0 {1 2 3}} +do_catchsql_test 18.3.1 { + DROP TABLE t1; + CREATE TABLE t1(x INTEGER PRIMARY KEY, y, z); + CREATE TRIGGER r2 AFTER INSERT ON t1 BEGIN + INSERT INTO t1(y) VALUES(RAISE(ABORT,'error 18.3')),(0); + END; + INSERT INTO t1 VALUES(1,2,3); +} {1 {error 18.3}} +do_execsql_test 18.3.2 { + SELECT * FROM t1; +} {} +do_catchsql_test 18.4.1 { + DROP TABLE t1; + CREATE TABLE t1(x INTEGER PRIMARY KEY, y, z); + CREATE TRIGGER r2 AFTER INSERT ON t1 BEGIN + INSERT INTO t1(y) VALUES(1),(RAISE(ABORT,'error 18.4')),(0); + END; + INSERT INTO t1 VALUES(1,2,3); +} {1 {error 18.4}} +do_execsql_test 18.4.2 { + SELECT * FROM t1; +} {} +do_catchsql_test 18.5.1 { + DROP TABLE t1; + CREATE TABLE t1(x INTEGER PRIMARY KEY, y, z); + CREATE TRIGGER r2 AFTER INSERT ON t1 BEGIN + INSERT INTO t1(y) VALUES(1), + (CASE WHEN new.z>7 THEN RAISE(ABORT,'error 18.5') ELSE 2 END); + END; + INSERT INTO t1 VALUES(1,2,3); + SELECT * FROM t1; +} {0 {1 2 3 2 1 {} 3 2 {}}} +do_catchsql_test 18.5.2 { + DELETE FROM t1; + INSERT INTO t1 VALUES(1,2,13); +} {1 {error 18.5}} +do_catchsql_test 18.5.3 { + SELECT * FROM t1; +} {0 {}} + +# 2024-04-18 dbsqlfuzz crash-bde3bf80aedf25afa56e2997a0545a314765d3f8 +# Verify that the VALUES expressions used as an argument to an outer +# join work correctly. +# +reset_db +db null NULL +do_execsql_test 19.1 { + CREATE TABLE t1(a INT, b INT); + INSERT INTO t1 VALUES(11,22); + SELECT * FROM t1 LEFT JOIN (VALUES(33,44),(55,66)) AS t2 ON a=b; +} {11 22 NULL NULL} +do_execsql_test 19.2 { + SELECT * FROM (VALUES(33,44),(55,66)) AS t2 RIGHT JOIN t1 ON a=b; +} {NULL NULL 11 22} +do_execsql_test 19.3 { + SELECT *, '|' FROM t1 FULL JOIN (VALUES(33,44),(55,66)) AS t2 ON a=b + ORDER BY +column1 +} {11 22 NULL NULL | NULL NULL 33 44 | NULL NULL 55 66 |} +do_execsql_test 19.4 { + SELECT *, '|' FROM (VALUES(33,44),(55,66)) AS t2 FULL JOIN t1 ON a=b + ORDER BY +column1 +} {NULL NULL 11 22 | 33 44 NULL NULL | 55 66 NULL NULL |} + +# 2024-04-21 dbsqlfuzz 6fd1ff3a64bef4a6c092e8d757548e95698b0df5 +# A continuation of the 2024-04-18 problem above. We have to create +# Pseudo-cursor that is always NULL on the viaCoroutine loop in case +# there are OP_Columns generated against it by the sub-WHERE clause. +# +db null N +do_execsql_test 19.5 { + DROP TABLE IF EXISTS t1; + DROP TABLE IF EXISTS t2; + DROP TABLE IF EXISTS t3; + CREATE TABLE t1(a,b); INSERT INTO t1 VALUES(1,2); + CREATE TABLE t2(column1,column2); INSERT INTO t2 VALUES(11,22),(33,44); + CREATE TABLE t3(d,e); INSERT INTO t3 VALUES(3,4); +} +do_execsql_test 19.6 { + -- output verify using PG 14.2 + SELECT * + FROM t1 CROSS JOIN t2 FULL JOIN t3 ON a=d + ORDER BY +d, +column1; +} {1 2 11 22 N N + 1 2 33 44 N N + N N N N 3 4} +do_execsql_test 19.7 { + SELECT * + FROM t1 CROSS JOIN (VALUES(11,22),(33,44)) FULL JOIN t3 ON a=d + ORDER BY +d, +column1; +} {1 2 11 22 N N + 1 2 33 44 N N + N N N N 3 4} +do_execsql_test 19.8 { + -- output verified using PG 14.2 + SELECT * + FROM t1 CROSS JOIN t2 FULL JOIN t3 ON a=d + WHERE column1 IS NULL; +} {N N N N 3 4} +do_execsql_test 19.9 { + SELECT * + FROM t1 CROSS JOIN (VALUES(11,22),(33,44)) FULL JOIN t3 ON a=d + WHERE column1 IS NULL; +} {N N N N 3 4} + +finish_test diff --git a/test/valuesfault.test b/test/valuesfault.test new file mode 100644 index 0000000000..bc5dddfb0e --- /dev/null +++ b/test/valuesfault.test @@ -0,0 +1,37 @@ +# 2024 March 3 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix valuesfault +source $testdir/malloc_common.tcl + + +do_execsql_test 1.0 { + CREATE TABLE x1(a, b, c); +} +faultsim_save_and_close + +do_faultsim_test 1 -prep { + faultsim_restore_and_reopen + sqlite3_limit db SQLITE_LIMIT_COMPOUND_SELECT 2 +} -body { + execsql { + INSERT INTO x1 VALUES(1, 1, 1), (2, 2, 2), (3, 3, 3), (4, 4, 4); + } +} -test { + faultsim_test_result {0 {}} +} + + +finish_test diff --git a/test/varint.test b/test/varint.test index 974e88f2a6..fd0ec0d415 100644 --- a/test/varint.test +++ b/test/varint.test @@ -30,3 +30,5 @@ foreach start {0 100 10000 1000000 0x10000000} { } } } + +finish_test diff --git a/test/view.test b/test/view.test index 235f8605ac..241742025c 100644 --- a/test/view.test +++ b/test/view.test @@ -38,6 +38,28 @@ do_test view-1.1 { SELECT * FROM v1 ORDER BY a; } } {1 2 4 5 7 8} +do_test view-1.1.100 { + db config enable_view off + catchsql { + SELECT * FROM v1 ORDER BY a; + } +} {1 {access to view "v1" prohibited}} +do_execsql_test view-1.1.101 { + CREATE TEMP VIEW v1temp AS SELECT a, b FROM t1; + SELECT * FROM v1temp ORDER BY a; +} {1 2 4 5 7 8} +do_test view-1.1.110 { + db config enable_view on + catchsql { + SELECT * FROM v1 ORDER BY a; + SELECT * FROM v1temp ORDER BY a; + } +} {0 {1 2 4 5 7 8 1 2 4 5 7 8}} +ifcapable vtab { + do_execsql_test view-1.1.120 { + SELECT name, type FROM pragma_table_list('v1'); + } {v1 view} +} do_test view-1.2 { catchsql { ROLLBACK; @@ -92,6 +114,26 @@ do_test view-1.8 { } } {2 3 5 6 8 9} +do_execsql_test view-1.10 { + CREATE TABLE t9(x INTEGER); + CREATE VIEW v9a AS SELECT x FROM t9; + CREATE VIEW v9b AS SELECT * FROM t9; + CREATE VIEW v9c(x) AS SELECT x FROM t9; + CREATE VIEW v9d(x) AS SELECT * FROM t9; +} {} +do_execsql_test view-1.11 { + PRAGMA table_info(v9a); +} {0 x INTEGER 0 {} 0} +do_execsql_test view-1.12 { + PRAGMA table_info(v9b); +} {0 x INTEGER 0 {} 0} +do_execsql_test view-1.13 { + PRAGMA table_info(v9c); +} {0 x INTEGER 0 {} 0} +do_execsql_test view-1.14 { + PRAGMA table_info(v9d); +} {0 x INTEGER 0 {} 0} + do_test view-2.1 { execsql { CREATE VIEW v2 AS SELECT * FROM t1 WHERE a>5 @@ -607,41 +649,6 @@ do_test view-20.1 { } } {} -# Ticket [d58ccbb3f1b]: Prevent Table.nRef overflow. -db close -sqlite3 db :memory: -do_test view-21.1 { - catchsql { - CREATE TABLE t1(x); - INSERT INTO t1 VALUES(5); - CREATE VIEW v1 AS SELECT x*2 FROM t1; - CREATE VIEW v2 AS SELECT * FROM v1 UNION SELECT * FROM v1; - CREATE VIEW v4 AS SELECT * FROM v2 UNION SELECT * FROM v2; - CREATE VIEW v8 AS SELECT * FROM v4 UNION SELECT * FROM v4; - CREATE VIEW v16 AS SELECT * FROM v8 UNION SELECT * FROM v8; - CREATE VIEW v32 AS SELECT * FROM v16 UNION SELECT * FROM v16; - CREATE VIEW v64 AS SELECT * FROM v32 UNION SELECT * FROM v32; - CREATE VIEW v128 AS SELECT * FROM v64 UNION SELECT * FROM v64; - CREATE VIEW v256 AS SELECT * FROM v128 UNION SELECT * FROM v128; - CREATE VIEW v512 AS SELECT * FROM v256 UNION SELECT * FROM v256; - CREATE VIEW v1024 AS SELECT * FROM v512 UNION SELECT * FROM v512; - CREATE VIEW v2048 AS SELECT * FROM v1024 UNION SELECT * FROM v1024; - CREATE VIEW v4096 AS SELECT * FROM v2048 UNION SELECT * FROM v2048; - CREATE VIEW v8192 AS SELECT * FROM v4096 UNION SELECT * FROM v4096; - CREATE VIEW v16384 AS SELECT * FROM v8192 UNION SELECT * FROM v8192; - CREATE VIEW v32768 AS SELECT * FROM v16384 UNION SELECT * FROM v16384; - SELECT * FROM v32768 UNION SELECT * FROM v32768; - } -} {1 {too many references to "v1": max 65535}} -ifcapable progress { - do_test view-21.2 { - db progress 1000 {expr 1} - catchsql { - SELECT * FROM v32768; - } - } {1 interrupted} -} - db close sqlite3 db :memory: do_execsql_test view-22.1 { @@ -654,5 +661,167 @@ do_test view-22.2 { lsort [array names x] } {{} * :1 :2} +do_test view-25.1 { + db eval { + CREATE TABLE t25 (x); + INSERT INTO t25 (x) VALUES (1); + ANALYZE; + } + proc authLogDelete {code arg1 arg2 arg3 arg4 args} { + if {$code=="SQLITE_DELETE" && [string match sqlite_stat* $arg1]} { + # lappend ::log [list $code $arg1 $arg2 $arg3 $arg4 $args] + lappend ::log [list $code $arg1 $arg2 $arg3 $arg4] + } + return SQLITE_OK + } + set log "" + db authorizer ::authLogDelete + db eval {DROP VIEW x1;} + set log +} {} + +set res [list {SQLITE_DELETE sqlite_stat1 {} main {}}] +ifcapable stat4 { lappend res {SQLITE_DELETE sqlite_stat4 {} main {}} } +do_test view-25.2 { + set log "" + db eval {DROP TABLE t25;} + set log +} $res + +#------------------------------------------------------------------------- +do_execsql_test view-26.0 { + CREATE TABLE t16(a, b, c UNIQUE); + INSERT INTO t16 VALUES(1, 1, 1); + INSERT INTO t16 VALUES(2, 2, 2); + INSERT INTO t16 VALUES(3, 3, 3); + CREATE VIEW v16 AS SELECT max(a) AS mx, min(b) AS mn FROM t16 GROUP BY c; + + SELECT * FROM v16 AS one, v16 AS two WHERE one.mx=1; +} { + 1 1 1 1 + 1 1 2 2 + 1 1 3 3 +} +do_execsql_test view-26.1 { + WITH v17(x,y) AS (SELECT max(a), min(b) FROM t16 GROUP BY c) + SELECT * FROM v17 AS one, v17 AS two WHERE one.x=1; +} { + 1 1 1 1 + 1 1 2 2 + 1 1 3 3 +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test view-27.0 { + CREATE TABLE t0(c0 TEXT, c1); + INSERT INTO t0(c0, c1) VALUES (-1, 0); + CREATE VIEW v0(c0, c1) AS SELECT t0.c0, AVG(t0.c1) FROM t0; +} + +do_execsql_test view-27.1 { + SELECT c0, typeof(c0), c1, typeof(c1) FROM v0; +} { + -1 text + 0.0 real +} + +do_execsql_test view-27.2 { SELECT c0<c1 FROM v0 } 1 +do_execsql_test view-27.3 { SELECT c1<c0 FROM v0 } 0 +do_execsql_test view-27.4 { + SELECT 1 FROM v0 WHERE c1<c0 +} {} +do_execsql_test view-27.5 { + SELECT 1 FROM v0 WHERE c0<c1 +} {1} + +do_execsql_test view-27.6 { + SELECT c0<c1 FROM (SELECT t0.c0 AS c0, AVG(t0.c1) AS c1 FROM t0) +} 1 +do_execsql_test view-27.7 { + SELECT c1<c0 FROM (SELECT t0.c0 AS c0, AVG(t0.c1) AS c1 FROM t0) +} 0 +do_execsql_test view-27.8 { + SELECT 1 FROM (SELECT t0.c0 AS c0, AVG(t0.c1) AS c1 FROM t0) WHERE c1<c0 +} {} +do_execsql_test view-27.9 { + SELECT 1 FROM (SELECT t0.c0 AS c0, AVG(t0.c1) AS c1 FROM t0) WHERE c0<c1 +} {1} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test view-28.0 { + CREATE TABLE t0(c0 TEXT); + CREATE VIEW v0(c0) AS SELECT t0.c0 FROM t0; + INSERT INTO t0(c0) VALUES ('0'); +} +do_execsql_test view-28.1 { + SELECT 0 IN (c0) FROM t0; +} {0} +do_execsql_test view-28.2 { + SELECT 0 IN (c0) FROM (SELECT c0 FROM t0); +} {0} + +#------------------------------------------------------------------------- +# 2020-10-26. https://sqlite.org/forum/forumpost/daa2c728cc +# +reset_db +do_catchsql_test view-29.0 { + CREATE TABLE t1(a,b,c); + CREATE VIEW IF NOT EXISTS IF AS SELECT null; +} {1 {malformed database schema (IF) - near "AS": syntax error}} +do_catchsql_test view-29.1 { + CREATE TABLE t2(c,d,e); + SELECT name FROM sqlite_schema ORDER BY name; +} {0 {t1 t2}} + +#------------------------------------------------------------------------- +# 2022-12-11. https://sqlite.org/src/info/679ed6a2 +# +# 2022-12-14 change: If the AS SELECT of a VIEW is a compound where +# the datatypes on each arm of the compound are different, then the +# datatype of the overall column is BLOB (ANY). +# +reset_db +do_execsql_test view-30.0 { + CREATE TABLE t0(a INT, b TEXT); + + INSERT INTO t0 VALUES(1,'one'); + + CREATE VIEW t1 AS SELECT a, b FROM t0 UNION ALL SELECT 2, 2; + CREATE VIEW t2(a,b) AS SELECT a, b FROM t0 UNION ALL SELECT 2, 2; +} + +ifcapable schema_pragmas { + do_execsql_test view-30.1 { + PRAGMA table_info = t1; + } { 0 a INT 0 {} 0 1 b BLOB 0 {} 0 } + do_execsql_test view-30.2 { + PRAGMA table_info = t2; + } { 0 a INT 0 {} 0 1 b BLOB 0 {} 0 } +} + +#----------------------------------------------------------------------- +# 2024-04-25 Trying to make type information on compound subqueries +# more predictable and rational. +# +reset_db +do_execsql_test view-31.1 { + CREATE TABLE x2(b TEXT); + CREATE TABLE x1(a TEXT); + INSERT INTO x1 VALUES('123'); + -- Two queries get the same result even though the order of terms + -- in the CTE is reversed + WITH c(x) AS ( SELECT b FROM x2 UNION SELECT 123 ) + SELECT count(*) FROM x1 WHERE a IN c; + WITH c(x) AS ( SELECT 123 UNION SELECT b FROM x2 ) + SELECT count(*) FROM x1 WHERE a IN c; +} {0 0} +do_execsql_test view-31.2 { + CREATE TABLE t3(a INTEGER, b TEXT); + INSERT INTO t3 VALUES(123, 123); + WITH s AS ( VALUES(123), (456) ) SELECT * FROM t3 WHERE b IN s; +} {123 123} + finish_test diff --git a/test/view2.test b/test/view2.test new file mode 100644 index 0000000000..e5df1437fe --- /dev/null +++ b/test/view2.test @@ -0,0 +1,49 @@ +# 2021 May 20 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is testing VIEW statements. +# +# $Id: view.test,v 1.39 2008/12/14 14:45:21 danielk1977 Exp $ +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +# Omit this entire file if the library is not configured with views enabled. +ifcapable !view { + finish_test + return +} +set testprefix view2 + +do_execsql_test 1.0 { + CREATE TABLE t1(x, y); + INSERT INTO t1 VALUES(1, 2); + CREATE VIEW v1 AS SELECT * FROM ( + WITH x1 AS (SELECT y, x FROM t1) + SELECT * FROM x1 + ); +} + +do_execsql_test 1.1 { + SELECT * FROM v1 +} {2 1} + +do_execsql_test 1.2 { + CREATE VIEW v3 AS SELECT * FROM main.t1; + WITH t1(a, b) AS ( SELECT 3, 4 ) SELECT * FROM v3; +} {1 2} + +breakpoint +do_execsql_test 1.3 { + CREATE VIEW v2 AS SELECT * FROM t1; + WITH t1(a, b) AS ( SELECT 3, 4 ) SELECT * FROM v2; +} {1 2} + +finish_test diff --git a/test/view3.test b/test/view3.test new file mode 100644 index 0000000000..60dd694bfd --- /dev/null +++ b/test/view3.test @@ -0,0 +1,60 @@ +# 2022 July 19 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is testing VIEW statements. +# +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +# Omit this entire file if the library is not configured with views enabled. +ifcapable !view { + finish_test + return +} +set testprefix view3 + +# Ticket [d58ccbb3f1b]: Prevent Table.nRef overflow. +db close +sqlite3 db :memory: +do_test 1.1 { + catchsql { + CREATE TABLE t1(x); + INSERT INTO t1 VALUES(5); + CREATE VIEW v1 AS SELECT x*2 FROM t1; + CREATE VIEW v2 AS SELECT * FROM v1 UNION SELECT * FROM v1; + CREATE VIEW v4 AS SELECT * FROM v2 UNION SELECT * FROM v2; + CREATE VIEW v8 AS SELECT * FROM v4 UNION SELECT * FROM v4; + CREATE VIEW v16 AS SELECT * FROM v8 UNION SELECT * FROM v8; + CREATE VIEW v32 AS SELECT * FROM v16 UNION SELECT * FROM v16; + CREATE VIEW v64 AS SELECT * FROM v32 UNION SELECT * FROM v32; + CREATE VIEW v128 AS SELECT * FROM v64 UNION SELECT * FROM v64; + CREATE VIEW v256 AS SELECT * FROM v128 UNION SELECT * FROM v128; + CREATE VIEW v512 AS SELECT * FROM v256 UNION SELECT * FROM v256; + CREATE VIEW v1024 AS SELECT * FROM v512 UNION SELECT * FROM v512; + CREATE VIEW v2048 AS SELECT * FROM v1024 UNION SELECT * FROM v1024; + CREATE VIEW v4096 AS SELECT * FROM v2048 UNION SELECT * FROM v2048; + CREATE VIEW v8192 AS SELECT * FROM v4096 UNION SELECT * FROM v4096; + CREATE VIEW v16384 AS SELECT * FROM v8192 UNION SELECT * FROM v8192; + CREATE VIEW v32768 AS SELECT * FROM v16384 UNION SELECT * FROM v16384; + SELECT * FROM v32768 UNION SELECT * FROM v32768; + } +} {1 {too many references to "v1": max 65535}} +ifcapable progress { + do_test 1.2 { + db progress 1000 {expr 1} + catchsql { + SELECT * FROM v32768; + } + } {1 interrupted} +} + + +finish_test diff --git a/test/vt02.c b/test/vt02.c new file mode 100644 index 0000000000..06fade0969 --- /dev/null +++ b/test/vt02.c @@ -0,0 +1,1024 @@ +/* +** This file implements an eponymous, read-only table-valued function +** (a virtual table) designed to be used for testing. We are not aware +** of any practical real-world use case for the virtual table. +** +** This virtual table originated in the TH3 test suite. It is still used +** there, but has now been copied into the public SQLite source tree and +** reused for a variety of testing purpose. The name "vt02" comes from the +** fact that there are many different testing virtual tables in TH3, of which +** this one is the second. +** +** ## SUBJECT TO CHANGE +** +** Because this virtual table is intended for testing, its interface is not +** guaranteed to be stable across releases. Future releases may contain +** changes in the vt02 design and interface. +** +** ## OVERVIEW +** +** The vt02 table-valued function has 10000 rows with 5 data columns. +** Column X contains all integer values between 0 and 9999 inclusive. +** Columns A, B, C, and D contain the individual base-10 digits associated +** with each X value: +** +** X A B C D +** ---- - - - - +** 0 0 0 0 0 +** 1 0 0 0 1 +** 2 0 0 0 2 +** ... +** 4998 4 9 9 8 +** 4999 4 9 9 9 +** 5000 5 0 0 0 +** ... +** 9995 9 9 9 5 +** 9996 9 9 9 6 +** 9997 9 9 9 7 +** +** The xBestIndex method recognizes a variety of equality constraints +** and attempts to optimize its output accordingly. +** +** x=... +** a=... +** a=... AND b=... +** a=... AND b=... AND c=... +** a=... AND b=... AND c=... AND d=... +** +** Various ORDER BY constraints are also recognized and consumed. The +** OFFSET constraint is recognized and consumed. +** +** ## TABLE-VALUED FUNCTION +** +** The vt02 virtual table is eponymous and has two hidden columns, meaning +** that it can functions a table-valued function. The two hidden columns +** are "flags" and "logtab", in that order. The "flags" column can be set +** to an integer where various bits enable or disable behaviors of the +** virtual table. The "logtab" can set to the name of an ordinary SQLite +** table into which is written information about each call to xBestIndex. +** +** The bits of "flags" are as follows: +** +** 0x01 Ignore the aConstraint[].usable flag. This might +** result in the xBestIndex method incorrectly using +** unusable entries in the aConstraint[] array, which +** should result in the SQLite core detecting and +** reporting that the virtual table is not behaving +** to spec. +** +** 0x02 Do not set the orderByConsumed flag, even if it +** could be set. +** +** 0x04 Do not consume the OFFSET constraint, if there is +** one. Instead, let the generated byte-code visit +** and ignore the first few columns of output. +** +** 0x08 Use sqlite3_mprintf() to allocate an idxStr string. +** The string is never used, but allocating it does +** test the idxStr deallocation logic inside of the +** SQLite core. +** +** 0x10 Cause the xBestIndex method to generate an idxNum +** that xFilter does not understand, thus causing +** the OP_VFilter opcode to raise an error. +** +** 0x20 Set the omit flag for all equality constraints on +** columns X, A, B, C, and D that are used to limit +** the search. +** +** 0x40 Add all constraints against X,A,B,C,D to the +** vector of results sent to xFilter. Only the first +** few are used, as required by idxNum. +** +** Because these flags take effect during xBestIndex, the RHS of the +** flag= constraint must be accessible. In other words, the RHS of flag= +** needs to be an integer literal, not another column of a join or a +** bound parameter. +** +** ## LOGGING OUTPUT +** +** If the "logtab" columns is set, then each call to the xBestIndex method +** inserts multiple rows into the table identified by "logtab". These +** rows collectively show the content of the sqlite3_index_info object and +** other context associated with the xBestIndex call. +** +** If the table named by "logtab" does not previously exist, it is created +** automatically. The schema for the logtab table is like this: +** +** CREATE TEMP TABLE vt02_log( +** bi INT, -- BestIndex call counter +** vn TEXT, -- Variable Name +** ix INT, -- Index or value +** cn TEXT, -- Column Name +** op INT, -- Opcode or "DESC" value +** ux INT, -- "Usable" flag +** ra BOOLEAN, -- Right-hand side Available. +** rhs ANY, -- Right-Hand Side value +** cs TEXT -- Collating Sequence for this constraint +** ); +** +** Because logging happens during xBestIindex, the RHS value of "logtab" must +** be known to xBestIndex, which means it must be a string literal, not a +** column in a join, or a bound parameter. +** +** ## VIRTUAL TABLE SCHEMA +** +** CREATE TABLE vt02( +** x INT, -- integer between 0 and 9999 inclusive +** a INT, -- The 1000s digit +** b INT, -- The 100s digit +** c INT, -- The 10s digit +** d INT, -- The 1s digit +** flags INT HIDDEN, -- Option flags +** logtab TEXT HIDDEN, -- Name of table into which to log xBestIndex +** ); +** +** ## COMPILING AND RUNNING +** +** This file can also be compiled separately as a loadable extension +** for SQLite (as long as the -DTH3_VERSION is not defined). To compile as a +** loadable extension do his: +** +** gcc -Wall -g -shared -fPIC -I. -DSQLITE_DEBUG vt02.c -o vt02.so +** +** Or on Windows: +** +** cl vt02.c -link -dll -out:vt02.dll +** +** Then load into the CLI using: +** +** .load ./vt02 sqlite3_vt02_init +** +** ## IDXNUM SUMMARY +** +** The xBestIndex method communicates the query plan to xFilter using +** the idxNum value, as follows: +** +** 0 unconstrained +** 1 X=argv[0] +** 2 A=argv[0] +** 3 A=argv[0], B=argv[1] +** 4 A=argv[0], B=argv[1], C=argv[2] +** 5 A=argv[0], B=argv[1], C=argv[2], D=argv[3] +** 6 A=argv[0], D IN argv[2] +** 7 A=argv[0], B=argv[2], D IN argv[3] +** 8 A=argv[0], B=argv[2], C=argv[3], D IN argv[4] +** 1x increment by 10 +** 2x increment by 100 +** 3x increment by 1000 +** 1xx Use offset provided by argv[N] +*/ +#ifndef TH3_VERSION + /* These bits for separate compilation as a loadable extension, only */ + #include "sqlite3ext.h" + SQLITE_EXTENSION_INIT1 + #include <stdlib.h> + #include <string.h> + #include <assert.h> +#endif + +/* Forward declarations */ +typedef struct vt02_vtab vt02_vtab; +typedef struct vt02_cur vt02_cur; + +/* +** The complete virtual table +*/ +struct vt02_vtab { + sqlite3_vtab parent; /* Base clase. Must be first. */ + sqlite3 *db; /* Database connection */ + int busy; /* Currently running xBestIndex */ +}; + +#define VT02_IGNORE_USABLE 0x0001 /* Ignore usable flags */ +#define VT02_NO_SORT_OPT 0x0002 /* Do not do any sorting optimizations */ +#define VT02_NO_OFFSET 0x0004 /* Omit the offset optimization */ +#define VT02_ALLOC_IDXSTR 0x0008 /* Alloate an idxStr */ +#define VT02_BAD_IDXNUM 0x0010 /* Generate an invalid idxNum */ + +/* +** A cursor +*/ +struct vt02_cur { + sqlite3_vtab_cursor parent; /* Base class. Must be first */ + sqlite3_int64 i; /* Current entry */ + sqlite3_int64 iEof; /* Indicate EOF when reaching this value */ + int iIncr; /* Amount by which to increment */ + unsigned int mD; /* Mask of allowed D-column values */ +}; + +/* The xConnect method */ +int vt02Connect( + sqlite3 *db, /* The database connection */ + void *pAux, /* Pointer to an alternative schema */ + int argc, /* Number of arguments */ + const char *const*argv, /* Text of the arguments */ + sqlite3_vtab **ppVTab, /* Write the new vtab here */ + char **pzErr /* Error message written here */ +){ + vt02_vtab *pVtab; + int rc; + const char *zSchema = (const char*)pAux; + static const char zDefaultSchema[] = + "CREATE TABLE x(x INT, a INT, b INT, c INT, d INT," + " flags INT HIDDEN, logtab TEXT HIDDEN);"; +#define VT02_COL_X 0 +#define VT02_COL_A 1 +#define VT02_COL_B 2 +#define VT02_COL_C 3 +#define VT02_COL_D 4 +#define VT02_COL_FLAGS 5 +#define VT02_COL_LOGTAB 6 +#define VT02_COL_NONE 7 + + pVtab = sqlite3_malloc( sizeof(*pVtab) ); + if( pVtab==0 ){ + *pzErr = sqlite3_mprintf("out of memory"); + return SQLITE_NOMEM; + } + memset(pVtab, 0, sizeof(*pVtab)); + pVtab->db = db; + rc = sqlite3_declare_vtab(db, zSchema ? zSchema : zDefaultSchema); + if( rc ){ + sqlite3_free(pVtab); + }else{ + *ppVTab = &pVtab->parent; + } + return rc; +} + +/* the xDisconnect method +*/ +int vt02Disconnect(sqlite3_vtab *pVTab){ + sqlite3_free(pVTab); + return SQLITE_OK; +} + +/* Put an error message into the zErrMsg string of the virtual table. +*/ +static void vt02ErrMsg(sqlite3_vtab *pVtab, const char *zFormat, ...){ + va_list ap; + sqlite3_free(pVtab->zErrMsg); + va_start(ap, zFormat); + pVtab->zErrMsg = sqlite3_vmprintf(zFormat, ap); + va_end(ap); +} + + +/* Open a cursor for scanning +*/ +static int vt02Open(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){ + vt02_cur *pCur; + pCur = sqlite3_malloc( sizeof(*pCur) ); + if( pCur==0 ){ + vt02ErrMsg(pVTab, "out of memory"); + return SQLITE_NOMEM; + } + *ppCursor = &pCur->parent; + pCur->i = -1; + return SQLITE_OK; +} + +/* Close a cursor +*/ +static int vt02Close(sqlite3_vtab_cursor *pCursor){ + vt02_cur *pCur = (vt02_cur*)pCursor; + sqlite3_free(pCur); + return SQLITE_OK; +} + +/* Return TRUE if we are at the end of the BVS and there are +** no more entries. +*/ +static int vt02Eof(sqlite3_vtab_cursor *pCursor){ + vt02_cur *pCur = (vt02_cur*)pCursor; + return pCur->i<0 || pCur->i>=pCur->iEof; +} + +/* Advance the cursor to the next row in the table +*/ +static int vt02Next(sqlite3_vtab_cursor *pCursor){ + vt02_cur *pCur = (vt02_cur*)pCursor; + do{ + pCur->i += pCur->iIncr; + if( pCur->i<0 ) pCur->i = pCur->iEof; + }while( (pCur->mD & (1<<(pCur->i%10)))==0 && pCur->i<pCur->iEof ); + return SQLITE_OK; +} + +/* Rewind a cursor back to the beginning of its scan. +** +** Scanning is always increasing. +** +** idxNum +** 0 unconstrained +** 1 X=argv[0] +** 2 A=argv[0] +** 3 A=argv[0], B=argv[1] +** 4 A=argv[0], B=argv[1], C=argv[2] +** 5 A=argv[0], B=argv[1], C=argv[2], D=argv[3] +** 6 A=argv[0], D IN argv[2] +** 7 A=argv[0], B=argv[2], D IN argv[3] +** 8 A=argv[0], B=argv[2], C=argv[3], D IN argv[4] +** 1x increment by 10 +** 2x increment by 100 +** 3x increment by 1000 +** 1xx Use offset provided by argv[N] +*/ +static int vt02Filter( + sqlite3_vtab_cursor *pCursor, /* The cursor to rewind */ + int idxNum, /* Search strategy */ + const char *idxStr, /* Not used */ + int argc, /* Not used */ + sqlite3_value **argv /* Not used */ +){ + vt02_cur *pCur = (vt02_cur*)pCursor; /* The vt02 cursor */ + int bUseOffset = 0; /* True to use OFFSET value */ + int iArg = 0; /* argv[] values used so far */ + int iOrigIdxNum = idxNum; /* Original value for idxNum */ + + pCur->iIncr = 1; + pCur->mD = 0x3ff; + if( idxNum>=100 ){ + bUseOffset = 1; + idxNum -= 100; + } + if( idxNum<0 || idxNum>38 ) goto vt02_bad_idxnum; + while( idxNum>=10 ){ + pCur->iIncr *= 10; + idxNum -= 10; + } + if( idxNum==0 ){ + pCur->i = 0; + pCur->iEof = 10000; + }else if( idxNum==1 ){ + pCur->i = sqlite3_value_int64(argv[0]); + if( pCur->i<0 ) pCur->i = -1; + if( pCur->i>9999 ) pCur->i = 10000; + pCur->iEof = pCur->i+1; + if( pCur->i<0 || pCur->i>9999 ) pCur->i = pCur->iEof; + }else if( idxNum>=2 && idxNum<=5 ){ + int i, e, m; + e = idxNum - 2; + assert( e<=argc-1 ); + pCur->i = 0; + for(m=1000, i=0; i<=e; i++, m /= 10){ + sqlite3_int64 v = sqlite3_value_int64(argv[iArg++]); + if( v<0 ) v = 0; + if( v>9 ) v = 9; + pCur->i += m*v; + pCur->iEof = pCur->i+m; + } + }else if( idxNum>=6 && idxNum<=8 ){ + int i, e, m, rc; + sqlite3_value *pIn, *pVal; + e = idxNum - 6; + assert( e<=argc-2 ); + pCur->i = 0; + for(m=1000, i=0; i<=e; i++, m /= 10){ + sqlite3_int64 v; + pVal = 0; + if( sqlite3_vtab_in_first(0, &pVal)!=SQLITE_MISUSE + || sqlite3_vtab_in_first(argv[iArg], &pVal)!=SQLITE_ERROR + ){ + vt02ErrMsg(pCursor->pVtab, + "unexpected success from sqlite3_vtab_in_first()"); + return SQLITE_ERROR; + } + v = sqlite3_value_int64(argv[iArg++]); + if( v<0 ) v = 0; + if( v>9 ) v = 9; + pCur->i += m*v; + pCur->iEof = pCur->i+m; + } + pCur->mD = 0; + pIn = argv[iArg++]; + assert( sqlite3_value_type(pIn)==SQLITE_NULL ); + for( rc = sqlite3_vtab_in_first(pIn, &pVal); + rc==SQLITE_OK && pVal!=0; + rc = sqlite3_vtab_in_next(pIn, &pVal) + ){ + int eType = sqlite3_value_numeric_type(pVal); + if( eType==SQLITE_FLOAT ){ + double r = sqlite3_value_double(pVal); + if( r<0.0 || r>9.0 || r!=(int)r ) continue; + }else if( eType!=SQLITE_INTEGER ){ + continue; + } + i = sqlite3_value_int(pVal); + if( i<0 || i>9 ) continue; + pCur->mD |= 1<<i; + } + if( rc!=SQLITE_OK && rc!=SQLITE_DONE ){ + vt02ErrMsg(pCursor->pVtab, "Error from sqlite3_vtab_in_first/next()"); + return rc; + } + }else{ + goto vt02_bad_idxnum; + } + if( bUseOffset ){ + int nSkip = sqlite3_value_int(argv[iArg]); + while( nSkip-- > 0 && pCur->i<pCur->iEof ) vt02Next(pCursor); + } + return SQLITE_OK; + +vt02_bad_idxnum: + vt02ErrMsg(pCursor->pVtab, "invalid idxNum for vt02: %d", iOrigIdxNum); + return SQLITE_ERROR; +} + +/* Return the Nth column of the current row. +*/ +static int vt02Column( + sqlite3_vtab_cursor *pCursor, + sqlite3_context *context, + int N +){ + vt02_cur *pCur = (vt02_cur*)pCursor; + int v = pCur->i; + if( N==VT02_COL_X ){ + sqlite3_result_int(context, v); + }else if( N>=VT02_COL_A && N<=VT02_COL_D ){ + static const int iDivisor[] = { 1, 1000, 100, 10, 1 }; + v = (v/iDivisor[N])%10; + sqlite3_result_int(context, v); + } + return SQLITE_OK; +} + +/* Return the rowid of the current row +*/ +static int vt02Rowid(sqlite3_vtab_cursor *pCursor, sqlite3_int64 *pRowid){ + vt02_cur *pCur = (vt02_cur*)pCursor; + *pRowid = pCur->i+1; + return SQLITE_OK; +} + +/************************************************************************* +** Logging Subsystem +** +** The sqlite3BestIndexLog() routine implements a logging system for +** xBestIndex calls. This code is portable to any virtual table. +** +** sqlite3BestIndexLog() is the main routine, sqlite3RunSql() is a +** helper routine used for running various SQL statements as part of +** creating the log. +** +** These two routines should be portable to other virtual tables. Simply +** extract this code and call sqlite3BestIndexLog() near the end of the +** xBestIndex method in cases where logging is desired. +*/ +/* +** Run SQL on behalf of sqlite3BestIndexLog. +** +** Construct the SQL using the zFormat string and subsequent arguments. +** Or if zFormat is NULL, take the SQL as the first argument after the +** zFormat. In either case, the dynamically allocated SQL string is +** freed after it has been run. If something goes wrong with the SQL, +** then an error is left in pVTab->zErrMsg. +*/ +static void sqlite3RunSql( + sqlite3 *db, /* Run the SQL on this database connection */ + sqlite3_vtab *pVTab, /* Report errors to this virtual table */ + const char *zFormat, /* Format string for SQL, or NULL */ + ... /* Arguments, according to the format string */ +){ + char *zSql; + + va_list ap; + va_start(ap, zFormat); + if( zFormat==0 ){ + zSql = va_arg(ap, char*); + }else{ + zSql = sqlite3_vmprintf(zFormat, ap); + } + va_end(ap); + if( zSql ){ + char *zErrMsg = 0; + (void)sqlite3_exec(db, zSql, 0, 0, &zErrMsg); + if( zErrMsg ){ + if( pVTab->zErrMsg==0 ){ + pVTab->zErrMsg = sqlite3_mprintf("%s in [%s]", zErrMsg, zSql); + } + sqlite3_free(zErrMsg); + } + sqlite3_free(zSql); + } +} + +/* +** Record information about each xBestIndex method call in a separate +** table: +** +** CREATE TEMP TABLE [log-table-name] ( +** bi INT, -- BestIndex call number +** vn TEXT, -- Variable Name +** ix INT, -- Index or value +** cn TEXT, -- Column Name +** op INT, -- Opcode or argvIndex +** ux INT, -- "usable" or "omit" flag +** rx BOOLEAN, -- True if has a RHS value +** rhs ANY, -- The RHS value +** cs TEXT, -- Collating Sequence +** inop BOOLEAN -- True if this is a batchable IN operator +** ); +** +** If an error occurs, leave an error message in pVTab->zErrMsg. +*/ +static void sqlite3BestIndexLog( + sqlite3_index_info *pInfo, /* The sqlite3_index_info object */ + const char *zLogTab, /* Log into this table */ + sqlite3 *db, /* Database connection containing zLogTab */ + const char **azColname, /* Names of columns in the virtual table */ + sqlite3_vtab *pVTab /* Record errors into this object */ +){ + int i, rc; + sqlite3_str *pStr; + int iBI; + + if( sqlite3_table_column_metadata(db,0,zLogTab,0,0,0,0,0,0) ){ + /* The log table does not previously exist. Create it. */ + sqlite3RunSql(db,pVTab, + "CREATE TABLE IF NOT EXISTS temp.\"%w\"(\n" + " bi INT, -- BestIndex call number\n" + " vn TEXT, -- Variable Name\n" + " ix INT, -- Index or value\n" + " cn TEXT, -- Column Name\n" + " op INT, -- Opcode or argvIndex\n" + " ux INT, -- usable for omit flag\n" + " rx BOOLEAN, -- Right-hand side value is available\n" + " rhs ANY, -- RHS value\n" + " cs TEXT, -- Collating Sequence\n" + " inop BOOLEAN -- IN operator capable of batch reads\n" + ");", zLogTab + ); + iBI = 1; + }else{ + /* The log table does already exist. We assume that it has the + ** correct schema and proceed to find the largest prior "bi" value. + ** If the schema is wrong, errors might result. The code is able + ** to deal with this. */ + sqlite3_stmt *pStmt; + char *zSql; + zSql = sqlite3_mprintf("SELECT max(bi) FROM temp.\"%w\"",zLogTab); + if( zSql==0 ){ + sqlite3_free(pVTab->zErrMsg); + pVTab->zErrMsg = sqlite3_mprintf("out of memory"); + return; + } + rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); + sqlite3_free(zSql); + if( rc ){ + sqlite3_free(pVTab->zErrMsg); + pVTab->zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(db)); + iBI = 0; + }else if( sqlite3_step(pStmt)==SQLITE_ROW ){ + iBI = sqlite3_column_int(pStmt, 0)+1; + }else{ + iBI = 1; + } + sqlite3_finalize(pStmt); + } + sqlite3RunSql(db,pVTab, + "INSERT INTO temp.\"%w\"(bi,vn,ix) VALUES(%d,'nConstraint',%d)" + "RETURNING iif(bi=%d,'ok',RAISE(ABORT,'wrong trigger'))", + /* The RETURNING clause checks to see that the returning trigger fired + ** for the correct INSERT in the case of nested INSERT RETURNINGs. */ + zLogTab, iBI, pInfo->nConstraint, iBI + ); + for(i=0; i<pInfo->nConstraint; i++){ + sqlite3_value *pVal; + char *zSql; + int iCol = pInfo->aConstraint[i].iColumn; + int op = pInfo->aConstraint[i].op; + const char *zCol; + if( op==SQLITE_INDEX_CONSTRAINT_LIMIT + || op==SQLITE_INDEX_CONSTRAINT_OFFSET + ){ + zCol = ""; + }else if( iCol<0 ){ + zCol = "rowid"; + }else{ + zCol = azColname[iCol]; + } + pStr = sqlite3_str_new(0); + sqlite3_str_appendf(pStr, + "INSERT INTO temp.\"%w\"(bi,vn,ix,cn,op,ux,rx,rhs,cs,inop)" + "VALUES(%d,'aConstraint',%d,%Q,%d,%d", + zLogTab, iBI, + i, + zCol, + op, + pInfo->aConstraint[i].usable); + pVal = 0; + rc = sqlite3_vtab_rhs_value(pInfo, i, &pVal); + assert( pVal!=0 || rc!=SQLITE_OK ); + if( rc==SQLITE_OK ){ + sqlite3_str_appendf(pStr,",1,?1"); + }else{ + sqlite3_str_appendf(pStr,",0,NULL"); + } + sqlite3_str_appendf(pStr,",%Q,%d)", + sqlite3_vtab_collation(pInfo,i), + sqlite3_vtab_in(pInfo,i,-1)); + zSql = sqlite3_str_finish(pStr); + if( zSql==0 ){ + if( pVTab->zErrMsg==0 ) pVTab->zErrMsg = sqlite3_mprintf("out of memory"); + }else{ + sqlite3_stmt *pStmt = 0; + rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); + if( rc ){ + if( pVTab->zErrMsg==0 ){ + pVTab->zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(db)); + } + }else{ + if( pVal ) sqlite3_bind_value(pStmt, 1, pVal); + sqlite3_step(pStmt); + rc = sqlite3_reset(pStmt); + if( rc && pVTab->zErrMsg==0 ){ + pVTab->zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(db)); + } + } + sqlite3_finalize(pStmt); + sqlite3_free(zSql); + } + } + sqlite3RunSql(db,pVTab, + "INSERT INTO temp.\"%w\"(bi,vn,ix) VALUES(%d,'nOrderBy',%d)", + zLogTab, iBI, pInfo->nOrderBy + ); + for(i=0; i<pInfo->nOrderBy; i++){ + int iCol = pInfo->aOrderBy[i].iColumn; + sqlite3RunSql(db,pVTab, + "INSERT INTO temp.\"%w\"(bi,vn,ix,cn,op)VALUES(%d,'aOrderBy',%d,%Q,%d)", + zLogTab, iBI, + i, + iCol>=0 ? azColname[iCol] : "rowid", + pInfo->aOrderBy[i].desc + ); + } + sqlite3RunSql(db,pVTab, + "INSERT INTO temp.\"%w\"(bi,vn,ix) VALUES(%d,'sqlite3_vtab_distinct',%d)", + zLogTab, iBI, sqlite3_vtab_distinct(pInfo) + ); + sqlite3RunSql(db,pVTab, + "INSERT INTO temp.\"%w\"(bi,vn,ix) VALUES(%d,'colUsed',%lld)", + zLogTab, iBI, pInfo->colUsed + ); + for(i=0; i<pInfo->nConstraint; i++){ + int iCol = pInfo->aConstraint[i].iColumn; + int op = pInfo->aConstraint[i].op; + const char *zCol; + if( op==SQLITE_INDEX_CONSTRAINT_LIMIT + || op==SQLITE_INDEX_CONSTRAINT_OFFSET + ){ + zCol = ""; + }else if( iCol<0 ){ + zCol = "rowid"; + }else{ + zCol = azColname[iCol]; + } + sqlite3RunSql(db,pVTab, + "INSERT INTO temp.\"%w\"(bi,vn,ix,cn,op,ux)" + "VALUES(%d,'aConstraintUsage',%d,%Q,%d,%d)", + zLogTab, iBI, + i, + zCol, + pInfo->aConstraintUsage[i].argvIndex, + pInfo->aConstraintUsage[i].omit + ); + } + sqlite3RunSql(db,pVTab, + "INSERT INTO temp.\"%w\"(bi,vn,ix)VALUES(%d,'idxNum',%d)", + zLogTab, iBI, pInfo->idxNum + ); + sqlite3RunSql(db,pVTab, + "INSERT INTO temp.\"%w\"(bi,vn,ix)VALUES(%d,'estimatedCost',%f)", + zLogTab, iBI, pInfo->estimatedCost + ); + sqlite3RunSql(db,pVTab, + "INSERT INTO temp.\"%w\"(bi,vn,ix)VALUES(%d,'estimatedRows',%lld)", + zLogTab, iBI, pInfo->estimatedRows + ); + if( pInfo->idxStr ){ + sqlite3RunSql(db,pVTab, + "INSERT INTO temp.\"%w\"(bi,vn,ix)VALUES(%d,'idxStr',%Q)", + zLogTab, iBI, pInfo->idxStr + ); + sqlite3RunSql(db,pVTab, + "INSERT INTO temp.\"%w\"(bi,vn,ix)VALUES(%d,'needToFreeIdxStr',%d)", + zLogTab, iBI, pInfo->needToFreeIdxStr + ); + } + if( pInfo->nOrderBy ){ + sqlite3RunSql(db,pVTab, + "INSERT INTO temp.\"%w\"(bi,vn,ix)VALUES(%d,'orderByConsumed',%d)", + zLogTab, iBI, pInfo->orderByConsumed + ); + } +} +/* +** End of Logging Subsystem +*****************************************************************************/ + + +/* Find an estimated cost of running a query against vt02. +*/ +static int vt02BestIndex(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){ + int i; /* Loop counter */ + int isEq[5]; /* Equality constraints on X, A, B, C, and D */ + int isUsed[5]; /* Other non-== cosntraints X, A, B, C, and D */ + int argvIndex = 0; /* Next available argv[] slot */ + int iOffset = -1; /* Constraint for OFFSET */ + void *pX = 0; /* idxStr value */ + int flags = 0; /* RHS value for flags= */ + const char *zLogTab = 0; /* RHS value for logtab= */ + int iFlagTerm = -1; /* Constraint term for flags= */ + int iLogTerm = -1; /* Constraint term for logtab= */ + int iIn = -1; /* Index of the IN constraint */ + vt02_vtab *pSelf; /* This virtual table */ + + pSelf = (vt02_vtab*)pVTab; + if( pSelf->busy ){ + vt02ErrMsg(pVTab, "recursive use of vt02 prohibited"); + return SQLITE_CONSTRAINT; + } + pSelf->busy++; + + + /* Do an initial scan for flags=N and logtab=TAB constraints with + ** usable RHS values */ + for(i=0; i<pInfo->nConstraint; i++){ + sqlite3_value *pVal; + if( !pInfo->aConstraint[i].usable ) continue; + if( pInfo->aConstraint[i].op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue; + switch( pInfo->aConstraint[i].iColumn ){ + case VT02_COL_FLAGS: + if( sqlite3_vtab_rhs_value(pInfo, i, &pVal)==SQLITE_OK + && sqlite3_value_type(pVal)==SQLITE_INTEGER + ){ + flags = sqlite3_value_int(pVal); + } + iFlagTerm = i; + break; + case VT02_COL_LOGTAB: + if( sqlite3_vtab_rhs_value(pInfo, i, &pVal)==SQLITE_OK + && sqlite3_value_type(pVal)==SQLITE_TEXT + ){ + zLogTab = (const char*)sqlite3_value_text(pVal); + } + iLogTerm = i; + break; + } + } + + /* Do a second scan to actually analyze the index information */ + memset(isEq, 0xff, sizeof(isEq)); + memset(isUsed, 0xff, sizeof(isUsed)); + for(i=0; i<pInfo->nConstraint; i++){ + int j = pInfo->aConstraint[i].iColumn; + if( j>=VT02_COL_FLAGS ) continue; + if( pInfo->aConstraint[i].usable==0 + && (flags & VT02_IGNORE_USABLE)==0 ) continue; + if( j<0 ) j = VT02_COL_X; + switch( pInfo->aConstraint[i].op ){ + case SQLITE_INDEX_CONSTRAINT_FUNCTION: + case SQLITE_INDEX_CONSTRAINT_EQ: + isEq[j] = i; + break; + case SQLITE_INDEX_CONSTRAINT_LT: + case SQLITE_INDEX_CONSTRAINT_LE: + case SQLITE_INDEX_CONSTRAINT_GT: + case SQLITE_INDEX_CONSTRAINT_GE: + isUsed[j] = i; + break; + case SQLITE_INDEX_CONSTRAINT_OFFSET: + iOffset = i; + break; + } + } + + /* Use the analysis to find an appropriate query plan */ + if( isEq[0]>=0 ){ + /* A constraint of X= takes priority */ + pInfo->estimatedCost = 1; + pInfo->aConstraintUsage[isEq[0]].argvIndex = ++argvIndex; + if( flags & 0x20 ) pInfo->aConstraintUsage[isEq[0]].omit = 1; + pInfo->idxNum = 1; + }else if( isEq[1]<0 ){ + /* If there is no X= nor A= then we have to do a full scan */ + pInfo->idxNum = 0; + pInfo->estimatedCost = 10000; + }else{ + int v = 1000; + pInfo->aConstraintUsage[isEq[1]].argvIndex = ++argvIndex; + if( flags & 0x20 ) pInfo->aConstraintUsage[isEq[1]].omit = 1; + for(i=2; i<=4 && isEq[i]>=0; i++){ + if( i==4 && sqlite3_vtab_in(pInfo, isEq[4], 0) ) break; + pInfo->aConstraintUsage[isEq[i]].argvIndex = ++argvIndex; + if( flags & 0x20 ) pInfo->aConstraintUsage[isEq[i]].omit = 1; + v /= 10; + } + pInfo->idxNum = i; + if( isEq[4]>=0 && sqlite3_vtab_in(pInfo,isEq[4],1) ){ + iIn = isEq[4]; + pInfo->aConstraintUsage[iIn].argvIndex = ++argvIndex; + if( flags & 0x20 ) pInfo->aConstraintUsage[iIn].omit = 1; + v /= 5; + i++; + pInfo->idxNum += 4; + } + pInfo->estimatedCost = v; + } + pInfo->estimatedRows = (sqlite3_int64)pInfo->estimatedCost; + + /* Attempt to consume the ORDER BY clause. Except, always leave + ** orderByConsumed set to 0 for vt02_no_sort_opt. In this way, + ** we can compare vt02 and vt02_no_sort_opt to ensure they get + ** the same answer. + */ + if( pInfo->nOrderBy>0 && (flags & VT02_NO_SORT_OPT)==0 ){ + if( pInfo->idxNum==1 ){ + /* There will only be one row of output. So it is always sorted. */ + pInfo->orderByConsumed = 1; + }else + if( pInfo->aOrderBy[0].iColumn<=0 + && pInfo->aOrderBy[0].desc==0 + ){ + /* First column of order by is X ascending */ + pInfo->orderByConsumed = 1; + }else + if( sqlite3_vtab_distinct(pInfo)>=1 ){ + unsigned int x = 0; + for(i=0; i<pInfo->nOrderBy; i++){ + int iCol = pInfo->aOrderBy[i].iColumn; + if( iCol<0 ) iCol = 0; + x |= 1<<iCol; + } + if( sqlite3_vtab_distinct(pInfo)==2 ){ + if( x==0x02 ){ + /* DISTINCT A */ + pInfo->idxNum += 30; + pInfo->orderByConsumed = 1; + }else if( x==0x06 ){ + /* DISTINCT A,B */ + pInfo->idxNum += 20; + pInfo->orderByConsumed = 1; + }else if( x==0x0e ){ + /* DISTINCT A,B,C */ + pInfo->idxNum += 10; + pInfo->orderByConsumed = 1; + }else if( x & 0x01 ){ + /* DISTINCT X */ + pInfo->orderByConsumed = 1; + }else if( x==0x1e ){ + /* DISTINCT A,B,C,D */ + pInfo->orderByConsumed = 1; + } + }else{ + if( x==0x02 ){ + /* GROUP BY A */ + pInfo->orderByConsumed = 1; + }else if( x==0x06 ){ + /* GROUP BY A,B */ + pInfo->orderByConsumed = 1; + }else if( x==0x0e ){ + /* GROUP BY A,B,C */ + pInfo->orderByConsumed = 1; + }else if( x & 0x01 ){ + /* GROUP BY X */ + pInfo->orderByConsumed = 1; + }else if( x==0x1e ){ + /* GROUP BY A,B,C,D */ + pInfo->orderByConsumed = 1; + } + } + } + } + + if( flags & VT02_ALLOC_IDXSTR ){ + pInfo->idxStr = sqlite3_mprintf("test"); + pInfo->needToFreeIdxStr = 1; + } + if( flags & VT02_BAD_IDXNUM ){ + pInfo->idxNum += 1000; + } + + if( iOffset>=0 ){ + pInfo->aConstraintUsage[iOffset].argvIndex = ++argvIndex; + if( (flags & VT02_NO_OFFSET)==0 + && (pInfo->nOrderBy==0 || pInfo->orderByConsumed) + ){ + pInfo->aConstraintUsage[iOffset].omit = 1; + pInfo->idxNum += 100; + } + } + + + /* Always omit flags= and logtab= constraints to prevent them from + ** interfering with the bytecode. Put them at the end of the argv[] + ** array to keep them out of the way. + */ + if( iFlagTerm>=0 ){ + pInfo->aConstraintUsage[iFlagTerm].omit = 1; + pInfo->aConstraintUsage[iFlagTerm].argvIndex = ++argvIndex; + } + if( iLogTerm>=0 ){ + pInfo->aConstraintUsage[iLogTerm].omit = 1; + pInfo->aConstraintUsage[iLogTerm].argvIndex = ++argvIndex; + } + + /* The 0x40 flag means add all usable constraints to the output set */ + if( flags & 0x40 ){ + for(i=0; i<pInfo->nConstraint; i++){ + if( pInfo->aConstraint[i].usable + && pInfo->aConstraintUsage[i].argvIndex==0 + ){ + pInfo->aConstraintUsage[i].argvIndex = ++argvIndex; + if( flags & 0x20 ) pInfo->aConstraintUsage[i].omit = 1; + } + } + } + + + /* Generate the log if requested */ + if( zLogTab ){ + static const char *azColname[] = { + "x", "a", "b", "c", "d", "flags", "logtab" + }; + sqlite3 *db = ((vt02_vtab*)pVTab)->db; + sqlite3BestIndexLog(pInfo, zLogTab, db, azColname, pVTab); + } + pSelf->busy--; + + /* Try to do a memory allocation solely for the purpose of causing + ** an error under OOM testing loops */ + pX = sqlite3_malloc(800); + if( pX==0 ) return SQLITE_NOMEM; + sqlite3_free(pX); + + return pVTab->zErrMsg!=0 ? SQLITE_ERROR : SQLITE_OK; +} + +/* This is the sqlite3_module definition for the the virtual table defined +** by this include file. +*/ +const sqlite3_module vt02Module = { + /* iVersion */ 2, + /* xCreate */ 0, /* This is an eponymous table */ + /* xConnect */ vt02Connect, + /* xBestIndex */ vt02BestIndex, + /* xDisconnect */ vt02Disconnect, + /* xDestroy */ vt02Disconnect, + /* xOpen */ vt02Open, + /* xClose */ vt02Close, + /* xFilter */ vt02Filter, + /* xNext */ vt02Next, + /* xEof */ vt02Eof, + /* xColumn */ vt02Column, + /* xRowid */ vt02Rowid, + /* xUpdate */ 0, + /* xBegin */ 0, + /* xSync */ 0, + /* xCommit */ 0, + /* xRollback */ 0, + /* xFindFunction */ 0, + /* xRename */ 0, + /* xSavepoint */ 0, + /* xRelease */ 0, + /* xRollbackTo */ 0, + /* xShadowName */ 0, + /* xIntegrity */ 0 +}; + +static void vt02CoreInit(sqlite3 *db){ + static const char zPkXSchema[] = + "CREATE TABLE x(x INT NOT NULL PRIMARY KEY, a INT, b INT, c INT, d INT," + " flags INT HIDDEN, logtab TEXT HIDDEN);"; + static const char zPkABCDSchema[] = + "CREATE TABLE x(x INT, a INT NOT NULL, b INT NOT NULL, c INT NOT NULL, " + "d INT NOT NULL, flags INT HIDDEN, logtab TEXT HIDDEN, " + "PRIMARY KEY(a,b,c,d));"; + sqlite3_create_module(db, "vt02", &vt02Module, 0); + sqlite3_create_module(db, "vt02pkx", &vt02Module, (void*)zPkXSchema); + sqlite3_create_module(db, "vt02pkabcd", &vt02Module, (void*)zPkABCDSchema); +} + +#ifdef TH3_VERSION +static void vt02_init(th3state *p, int iDb, char *zArg){ + vt02CoreInit(th3dbPointer(p, iDb)); +} +#else +#ifdef _WIN32 +__declspec(dllexport) +#endif +int sqlite3_vt02_init( + sqlite3 *db, + char **pzErrMsg, + const sqlite3_api_routines *pApi +){ + SQLITE_EXTENSION_INIT2(pApi); + vt02CoreInit(db); + return SQLITE_OK; +} +#endif /* TH3_VERSION */ diff --git a/test/vt100-a.sql b/test/vt100-a.sql new file mode 100644 index 0000000000..a0d3f46be7 --- /dev/null +++ b/test/vt100-a.sql @@ -0,0 +1,19 @@ +/* +** Run this script using the "sqlite3" command-line shell +** test test formatting of output text that contains +** vt100 escape sequences. +*/ +.mode box -escape off +CREATE TEMP TABLE t1(a,b,c); +INSERT INTO t1 VALUES + ('one','twotwotwo','thirty-three'), + (unistr('\u001b[91mRED\u001b[0m'),'fourfour','fifty-five'), + ('six','seven','eighty-eight'); +.print With -escape off +SELECT * FROM t1; +.mode box -escape ascii +.print With -escape ascii +SELECT * FROM t1; +.mode box -escape symbol +.print With -escape symbol +SELECT * FROM t1; diff --git a/test/vtab1.test b/test/vtab1.test index 6b6a0e2683..4b8fb9c700 100644 --- a/test/vtab1.test +++ b/test/vtab1.test @@ -875,6 +875,14 @@ do_test vtab1.7-13 { } } {} +# PRAGMA index_info and index_xinfo are no-ops on a virtual table +do_test vtab1.7-14 { + execsql { + PRAGMA index_info('echo_abc'); + PRAGMA index_xinfo('echo_abc'); + } +} {} + ifcapable attach { do_test vtab1.8-1 { set echo_module "" @@ -948,13 +956,13 @@ do_test vtab1.10-2 { sqlite3_declare_vtab $ptr {CREATE TABLE abc(a, b, c)} } msg] list $rc $msg -} {1 {library routine called out of sequence}} +} {1 {bad parameter or other API misuse}} do_test vtab1.10-3 { set ::echo_module_begin_fail r catchsql { INSERT INTO e VALUES(1, 2, 3); } -} {1 {SQL logic error or missing database}} +} {1 {SQL logic error}} do_test vtab1.10-4 { catch {execsql { EXPLAIN SELECT * FROM e WHERE rowid = 2; @@ -975,6 +983,7 @@ do_test vtab1.10-5 { proc match_func {args} {return ""} do_test vtab1.10-6 { set echo_module "" + sqlite_delete_function db match db function match match_func execsql { SELECT * FROM e WHERE match('pattern', rowid, 'pattern2'); @@ -1253,6 +1262,7 @@ ifcapable altertable { # characters that can be mistaken for printf() formatting directives. # do_test vtab1-17.1 { + sqlite3_db_config db DEFENSIVE 0 execsql { PRAGMA writable_schema = 1; INSERT INTO sqlite_master VALUES( @@ -1295,38 +1305,46 @@ do_execsql_test 18.1.0 { CREATE INDEX i6 ON t6(b, a); INSERT INTO t6 VALUES(1, 'Peter'); INSERT INTO t6 VALUES(2, 'Andrew'); - INSERT INTO t6 VALUES(3, 'James'); - INSERT INTO t6 VALUES(4, 'John'); + INSERT INTO t6 VALUES(3, '8James'); + INSERT INTO t6 VALUES(4, '8John'); INSERT INTO t6 VALUES(5, 'Phillip'); INSERT INTO t6 VALUES(6, 'Bartholomew'); CREATE VIRTUAL TABLE e6 USING echo(t6); } -foreach {tn sql res filter} { - 1.1 "SELECT a FROM e6 WHERE b>'James'" {4 1 5} - {xFilter {SELECT rowid, a, b FROM 't6' WHERE b > ?} James} - - 1.2 "SELECT a FROM e6 WHERE b>='J' AND b<'K'" {3 4} - {xFilter {SELECT rowid, a, b FROM 't6' WHERE b >= ? AND b < ?} J K} - - 1.3 "SELECT a FROM e6 WHERE b LIKE 'J%'" {3 4} - {xFilter {SELECT rowid, a, b FROM 't6' WHERE b like ?} J%} - - 1.4 "SELECT a FROM e6 WHERE b LIKE 'j%'" {3 4} - {xFilter {SELECT rowid, a, b FROM 't6' WHERE b like ?} j%} -} { - set echo_module {} - do_execsql_test 18.$tn.1 $sql $res - do_test 18.$tn.2 { lrange $::echo_module 2 end } $filter +ifcapable !icu { + foreach {tn sql res filter} { + 1.1 "SELECT a FROM e6 WHERE b>'8James'" {4 2 6 1 5} + {xFilter {SELECT rowid, a, b FROM 't6' WHERE b > ?} 8James} + + 1.2 "SELECT a FROM e6 WHERE b>='8' AND b<'9'" {3 4} + {xFilter {SELECT rowid, a, b FROM 't6' WHERE b >= ? AND b < ?} 8 9} + + 1.3 "SELECT a FROM e6 WHERE b LIKE '8J%'" {3 4} + {xFilter {SELECT rowid, a, b FROM 't6' WHERE b >= ? AND b < ? AND b like ?} 8J 8k 8J%} + + 1.4 "SELECT a FROM e6 WHERE b LIKE '8j%'" {3 4} + {xFilter {SELECT rowid, a, b FROM 't6' WHERE b >= ? AND b < ? AND b like ?} 8J 8k 8j%} + + 1.5 "SELECT a FROM e6 WHERE b LIKE '8%'" {3 4} + {xFilter {SELECT rowid, a, b FROM 't6' WHERE b like ?} 8%} + } { + set echo_module {} + do_execsql_test 18.$tn.1 $sql $res + do_test 18.$tn.2 { lrange $::echo_module 2 end } $filter + } } do_execsql_test 18.2.0 { PRAGMA case_sensitive_like = ON } foreach {tn sql res filter} { - 2.1 "SELECT a FROM e6 WHERE b LIKE 'J%'" {3 4} - {xFilter {SELECT rowid, a, b FROM 't6' WHERE b like ?} J%} + 2.1 "SELECT a FROM e6 WHERE b LIKE '8%'" {3 4} + {xFilter {SELECT rowid, a, b FROM 't6' WHERE b like ?} 8%} - 2.2 "SELECT a FROM e6 WHERE b LIKE 'j%'" {} - {xFilter {SELECT rowid, a, b FROM 't6' WHERE b like ?} j%} + 2.2 "SELECT a FROM e6 WHERE b LIKE '8j%'" {} + {xFilter {SELECT rowid, a, b FROM 't6' WHERE b >= ? AND b < ? AND b like ?} 8j 8k 8j%} + + 2.3 "SELECT a FROM e6 WHERE b LIKE '8J%'" {3 4} + {xFilter {SELECT rowid, a, b FROM 't6' WHERE b >= ? AND b < ? AND b like ?} 8J 8K 8J%} } { set echo_module {} do_execsql_test 18.$tn.1 $sql $res @@ -1335,7 +1353,7 @@ foreach {tn sql res filter} { do_execsql_test 18.2.x { PRAGMA case_sensitive_like = OFF } #------------------------------------------------------------------------- -# Test that an existing module may not be overridden. +# Test that it is ok to override and existing module. # do_test 19.1 { sqlite3 db2 test.db @@ -1343,7 +1361,7 @@ do_test 19.1 { } SQLITE_OK do_test 19.2 { register_echo_module [sqlite3_connection_pointer db2] -} SQLITE_MISUSE +} SQLITE_OK do_test 19.3 { db2 close } {} @@ -1540,4 +1558,36 @@ ifcapable fts3 { } } +# 2021-07-04 https://sqlite.org/forum/forumpost/16ca0e9f32 +# Yu Liang crash involving UPDATE on a virtual table with +# a duplicate column in a vector changeset and invoking the +# query flattener for UNION ALL. +# +reset_db +register_echo_module db +do_catchsql_test 25.0 { + CREATE TABLE t0(a); + CREATE VIRTUAL TABLE t1 USING echo(t0); + WITH t3(a) AS (SELECT * FROM t1 UNION ALL SELECT * FROM t1) + UPDATE t1 SET (a,a) = (SELECT 1, 0) FROM t3; +} {0 {}} + +#-------------------------------------------------------------------------- +# +reset_db +load_static_extension db wholenumber +do_execsql_test 26.1 { + CREATE VIRTUAL TABLE t1 USING wholenumber; + CREATE TABLE tx(a, b, c); +} +do_test 26.2 { + sqlite3 db2 test.db + db2 eval { CREATE TABLE ty(x, y) } + db2 close +} {} +do_execsql_test 26.3 { + SELECT value FROM t1 WHERE value<5 +} {1 2 3 4} + + finish_test diff --git a/test/vtab2.test b/test/vtab2.test index f0616513bd..7bd27a5fd0 100644 --- a/test/vtab2.test +++ b/test/vtab2.test @@ -60,7 +60,7 @@ do_test vtab2-2.1 { set ::abc 123 execsql { CREATE VIRTUAL TABLE vars USING tclvar; - SELECT * FROM vars WHERE name='abc'; + SELECT name, arrayname, value FROM vars WHERE name='abc'; } } [list abc "" 123] do_test vtab2-2.2 { @@ -68,7 +68,7 @@ do_test vtab2-2.2 { set A(2) 4 set A(3) 9 execsql { - SELECT * FROM vars WHERE name='A'; + SELECT name, arrayname, value FROM vars WHERE name='A'; } } [list A 1 1 A 2 4 A 3 9] unset -nocomplain result diff --git a/test/vtab6.test b/test/vtab6.test index f8e0935a28..1c220e11fe 100644 --- a/test/vtab6.test +++ b/test/vtab6.test @@ -223,11 +223,11 @@ do_test vtab6-2.2 { SELECT * FROM t2 NATURAL LEFT OUTER JOIN t1; } } {1 2 3 {} 2 3 4 1 3 4 5 2} -do_test vtab6-2.3 { - catchsql { - SELECT * FROM t1 NATURAL RIGHT OUTER JOIN t2; - } -} {1 {RIGHT and FULL OUTER JOINs are not currently supported}} +#do_test vtab6-2.3 { +# catchsql { +# SELECT * FROM t1 NATURAL RIGHT OUTER JOIN t2; +# } +#} {1 {RIGHT and FULL OUTER JOINs are not currently supported}} do_test vtab6-2.4 { execsql { SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.d @@ -263,7 +263,7 @@ do_test vtab6-3.3 { catchsql { SELECT * FROM t1 JOIN t2 ON t1.a=t2.b USING(b); } -} {1 {cannot have both ON and USING clauses in the same join}} +} {1 {near "USING": syntax error}} do_test vtab6-3.4 { catchsql { SELECT * FROM t1 JOIN t2 USING(a); @@ -277,16 +277,20 @@ do_test vtab6-3.6 { SELECT * FROM t1 JOIN t2 ON t3.a=t2.b; } } {1 {no such column: t3.a}} + +# EVIDENCE-OF: R-47973-48020 you cannot say "INNER OUTER JOIN", because +# that would be contradictory. do_test vtab6-3.7 { catchsql { SELECT * FROM t1 INNER OUTER JOIN t2; } -} {1 {unknown or unsupported join type: INNER OUTER}} +} {1 {unknown join type: INNER OUTER}} + do_test vtab6-3.7 { catchsql { SELECT * FROM t1 LEFT BOGUS JOIN t2; } -} {1 {unknown or unsupported join type: LEFT BOGUS}} +} {1 {unknown join type: LEFT BOGUS}} do_test vtab6-4.1 { execsql { @@ -351,6 +355,7 @@ do_test vtab6-4.10 { # A test for ticket #247. # do_test vtab6-7.1 { + sqlite3_db_config db SQLITE_DBCONFIG_DQS_DML 1 execsql { INSERT INTO t7 VALUES ("pa1", 1); INSERT INTO t7 VALUES ("pa2", NULL); @@ -566,12 +571,12 @@ do_test vtab6-11.4.1 { catchsql { SELECT a, b, c FROM ab NATURAL JOIN bc; } -} {1 {table ab: xBestIndex returned an invalid plan}} +} {1 {ab.xBestIndex malfunction}} do_test vtab6-11.4.2 { catchsql { SELECT a, b, c FROM bc NATURAL JOIN ab; } -} {1 {table bc: xBestIndex returned an invalid plan}} +} {1 {bc.xBestIndex malfunction}} unset ::echo_module_ignore_usable diff --git a/test/vtab7.test b/test/vtab7.test index 162bab5e87..8320993de7 100644 --- a/test/vtab7.test +++ b/test/vtab7.test @@ -162,7 +162,7 @@ ifcapable attach { # catchsql { # INSERT INTO abc2 VALUES(1, 2, 3); # } -# } {1 {library routine called out of sequence}} +# } {1 {bad parameter or other API misuse}} # These tests, vtab7-4.*, test that an SQLITE_LOCKED error is returned # if an attempt to write to a virtual module table or create a new diff --git a/test/vtabA.test b/test/vtabA.test index eddaa70d1f..4c9beae026 100644 --- a/test/vtabA.test +++ b/test/vtabA.test @@ -128,7 +128,7 @@ proc analyse_parse {columns decltype_list} { do_test vtabA-2.1 { analyse_parse {(a text, b integer hidden, c hidden)} {a b c} -} {a text integer {}} +} {a TEXT integer {}} do_test vtabA-2.2 { analyse_parse {(a hidden , b integerhidden, c hidden1)} {a b c} diff --git a/test/vtabE.test b/test/vtabE.test index aeb478e3e8..cbb6a1e30a 100644 --- a/test/vtabE.test +++ b/test/vtabE.test @@ -39,7 +39,9 @@ do_test vtabE-1 { CREATE VIRTUAL TABLE t1 USING tclvar; CREATE VIRTUAL TABLE t2 USING tclvar; CREATE TABLE t3(a INTEGER PRIMARY KEY, b); - SELECT t1.*, t2.*, abs(t3.b + abs(t2.value + abs(t1.value))) + SELECT t1.name, t1.arrayname, t1.value, + t2.name, t2.arrayname, t2.value, + abs(t3.b + abs(t2.value + abs(t1.value))) FROM t1 LEFT JOIN t2 ON t2.name = t1.arrayname LEFT JOIN t3 ON t3.a=t2.value WHERE t1.name = 'vtabE' diff --git a/test/vtabF.test b/test/vtabF.test index b9341ea597..7b11cb8c71 100644 --- a/test/vtabF.test +++ b/test/vtabF.test @@ -21,7 +21,7 @@ ifcapable !vtab||!schema_pragmas { finish_test ; return } # Register the echo module register_echo_module [sqlite3_connection_pointer db] -do_test vtabE-1.1 { +do_test vtabF-1.1 { execsql { CREATE TABLE t1(a, b); CREATE INDEX i1 ON t1(a); @@ -37,7 +37,7 @@ do_test vtabE-1.1 { SELECT b FROM t1 WHERE a IS NOT NULL; } } {110 111 112 113} -do_test vtabE-1.2 { +do_test vtabF-1.2 { execsql {SELECT b FROM tv1 WHERE a IS NOT NULL} } {110 111 112 113} diff --git a/test/vtabH.test b/test/vtabH.test index d16db13674..1496f49b53 100644 --- a/test/vtabH.test +++ b/test/vtabH.test @@ -30,22 +30,38 @@ do_execsql_test 1.0 { CREATE VIRTUAL TABLE e6 USING echo(t6); } -foreach {tn sql expect} { - 1 "SELECT * FROM e6 WHERE b LIKE 'abc'" { - xBestIndex {SELECT rowid, a, b FROM 't6' WHERE b like ?} - xFilter {SELECT rowid, a, b FROM 't6' WHERE b like ?} abc - } - - 2 "SELECT * FROM e6 WHERE b GLOB 'abc'" { - xBestIndex {SELECT rowid, a, b FROM 't6' WHERE b glob ?} - xFilter {SELECT rowid, a, b FROM 't6' WHERE b glob ?} abc +ifcapable !icu { + foreach {tn sql expect} { + 1 "SELECT * FROM e6 WHERE b LIKE '8abc'" { + xBestIndex + {SELECT rowid, a, b FROM 't6' WHERE b >= ? AND b < ? AND b like ?} + xFilter + {SELECT rowid, a, b FROM 't6' WHERE b >= ? AND b < ? AND b like ?} + 8ABC 8abd 8abc + } + + 2 "SELECT * FROM e6 WHERE b GLOB '8abc'" { + xBestIndex + {SELECT rowid, a, b FROM 't6' WHERE b >= ? AND b < ? AND b glob ?} + xFilter + {SELECT rowid, a, b FROM 't6' WHERE b >= ? AND b < ? AND b glob ?} + 8abc 8abd 8abc + } + 3 "SELECT * FROM e6 WHERE b LIKE '8e/'" { + xBestIndex {SELECT rowid, a, b FROM 't6' WHERE b like ?} + xFilter {SELECT rowid, a, b FROM 't6' WHERE b like ?} 8e/ + } + 4 "SELECT * FROM e6 WHERE b GLOB '8e/'" { + xBestIndex {SELECT rowid, a, b FROM 't6' WHERE b glob ?} + xFilter {SELECT rowid, a, b FROM 't6' WHERE b glob ?} 8e/ + } + } { + do_test 1.$tn { + set echo_module {} + execsql $sql + set ::echo_module + } [list {*}$expect] } -} { - do_test 1.$tn { - set echo_module {} - execsql $sql - set ::echo_module - } [list {*}$expect] } @@ -55,7 +71,7 @@ register_tclvar_module db set ::xyz 10 do_execsql_test 2.0 { CREATE VIRTUAL TABLE vars USING tclvar; - SELECT * FROM vars WHERE name = 'xyz'; + SELECT name, arrayname, value FROM vars WHERE name = 'xyz'; } {xyz {} 10} set x1 aback @@ -108,40 +124,79 @@ foreach ::tclvar_set_omit {0 1} { #------------------------------------------------------------------------- # -if {1} { - reset_db - register_fs_module db +if {$tcl_platform(platform) eq "windows"} { + set drive [string range [pwd] 0 1] + set ::env(fstreeDrive) $drive +} +reset_db +register_fs_module db +if {$tcl_platform(platform) ne "windows" || \ + [regexp -nocase -- {^[A-Z]:} $drive]} { do_execsql_test 3.0 { SELECT name FROM fsdir WHERE dir = '.' AND name = 'test.db'; SELECT name FROM fsdir WHERE dir = '.' AND name = '.' } {test.db .} + proc sort_files { names {nocase false} } { + if {$nocase && $::tcl_platform(platform) eq "windows"} { + return [lsort -nocase $names] + } else { + return [lsort $names] + } + } + proc list_root_files {} { if {$::tcl_platform(platform) eq "windows"} { - set res [list] - foreach name [glob -directory $::env(SystemDrive)/ -- *] { + set res [list]; set dir $::env(fstreeDrive)/; set names [list] + eval lappend names [glob -nocomplain -directory $dir -- *] + foreach name $names { if {[string index [file tail $name] 0] eq "."} continue + if {[file attributes $name -hidden]} continue + if {[file attributes $name -system]} continue lappend res $name } - return $res + return [sort_files $res true] } else { - return [string map {/ {}} [glob /*]] + return [sort_files [string map {/ {}} [glob -nocomplain -- /*]]] } } proc list_files { pattern } { if {$::tcl_platform(platform) eq "windows"} { - set res [list] - foreach name [glob -nocomplain $pattern] { + set res [list]; set names [list] + eval lappend names [glob -nocomplain -- $pattern] + foreach name $names { if {[string index [file tail $name] 0] eq "."} continue + if {[file attributes $name -hidden]} continue + if {[file attributes $name -system]} continue lappend res $name } - return $res + return [sort_files $res] } else { - return [glob -nocomplain $pattern] + return [sort_files [glob -nocomplain -- $pattern]] } } + # Read the first 5 entries from the root directory. Except, ignore + # files that contain the "$" character in their names as these are + # special files on some Windows platforms. + # + set res [list] + set root_files [list_root_files] + foreach p $root_files { + if {$::tcl_platform(platform) eq "windows"} { + if {![regexp {\$} $p]} {lappend res $p} + } else { + lappend res "/$p" + } + } + set num_root_files [llength $res] + do_test 3.1 { + sort_files [execsql { + SELECT path FROM fstree WHERE path NOT GLOB '*$*' LIMIT $num_root_files + }] true + } [sort_files $res true] + # Read all entries in the current directory. # proc contents {pattern} { @@ -158,7 +213,7 @@ if {1} { set res [contents $pwd] do_execsql_test 3.2 { SELECT path FROM fstree WHERE path GLOB $pwd ORDER BY 1 - } [lsort $res] + } [sort_files $res] # Add some sub-directories and files to the current directory. # @@ -177,24 +232,28 @@ if {1} { } {} set pwd [pwd] - do_execsql_test 3.5 { - SELECT path, size FROM fstree WHERE path GLOB $pwd || '/subdir/*' ORDER BY 1 - } [list \ - "$pwd/subdir/x1.txt" 143 \ - "$pwd/subdir/x2.txt" 153 \ - ] - do_execsql_test 3.6 { - SELECT path, size FROM fstree WHERE path LIKE $pwd || '/subdir/%' ORDER BY 1 - } [list \ - "$pwd/subdir/x1.txt" 143 \ - "$pwd/subdir/x2.txt" 153 \ - ] - do_execsql_test 3.7 { - SELECT sum(size) FROM fstree WHERE path LIKE $pwd || '/subdir/%' - } 296 - do_execsql_test 3.8 { - SELECT size FROM fstree WHERE path = $pwd || '/subdir/x1.txt' - } 143 + if {![string match {*[_%]*} $pwd]} { + do_execsql_test 3.5 { + SELECT path, size FROM fstree + WHERE path GLOB $pwd || '/subdir/*' ORDER BY 1 + } [list \ + "$pwd/subdir/x1.txt" 143 \ + "$pwd/subdir/x2.txt" 153 \ + ] + do_execsql_test 3.6 { + SELECT path, size FROM fstree + WHERE path LIKE $pwd || '/subdir/%' ORDER BY 1 + } [list \ + "$pwd/subdir/x1.txt" 143 \ + "$pwd/subdir/x2.txt" 153 \ + ] + do_execsql_test 3.7 { + SELECT sum(size) FROM fstree WHERE path LIKE $pwd || '/subdir/%' + } 296 + do_execsql_test 3.8 { + SELECT size FROM fstree WHERE path = $pwd || '/subdir/x1.txt' + } 143 + } } diff --git a/test/vtabJ.test b/test/vtabJ.test new file mode 100644 index 0000000000..123009f0e2 --- /dev/null +++ b/test/vtabJ.test @@ -0,0 +1,154 @@ +# 2017-08-10 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements tests of writing to WITHOUT ROWID virtual tables +# using the tclvar eponymous virtual table. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix vtabJ + +ifcapable !vtab { + finish_test + return +} + +register_tclvar_module db + +unset -nocomplain vtabJ +do_test 100 { + set vtabJ(1) this + set vtabJ(two) is + set vtabJ(3) {a test} + db eval { + SELECT fullname, value FROM tclvar WHERE name='vtabJ' ORDER BY fullname; + } +} {vtabJ(1) this vtabJ(3) {a test} vtabJ(two) is} + +do_execsql_test 110 { + INSERT INTO tclvar(fullname, value) + VALUES('vtabJ(4)',4),('vtabJ(five)',555); + SELECT fullname, value FROM tclvar WHERE name='vtabJ' ORDER BY fullname; +} {vtabJ(1) this vtabJ(3) {a test} vtabJ(4) 4 vtabJ(five) 555 vtabJ(two) is} +do_test 111 { + set res {} + foreach vname [lsort [array names vtabJ]] { + lappend res vtabJ($vname) $vtabJ($vname) + } + set res +} {vtabJ(1) this vtabJ(3) {a test} vtabJ(4) 4 vtabJ(five) 555 vtabJ(two) is} + +do_test 120 { + db eval { + INSERT INTO tclvar(fullname, value) VALUES('vtabJ(4)',444); + } + set vtabJ(4) +} {444} + +do_test 130 { + db eval { + INSERT INTO tclvar(fullname, value) VALUES('vtabJ(4)',NULL); + } + info exists vtabJ(4) +} {0} + +do_test 140 { + db eval { + UPDATE tclvar SET value=55 WHERE fullname='vtabJ(five)'; + } + set vtabJ(five) +} {55} + +do_test 150 { + db eval { + UPDATE tclvar SET fullname='vtabJ(5)' WHERE fullname='vtabJ(five)'; + } + set vtabJ(5) +} {55} +do_test 151 { + info exists vtabJ(five) +} {0} +do_test 152 { + set res {} + foreach vname [lsort [array names vtabJ]] { + lappend res vtabJ($vname) $vtabJ($vname) + } + set res +} {vtabJ(1) this vtabJ(3) {a test} vtabJ(5) 55 vtabJ(two) is} + +do_execsql_test 160 { + SELECT fullname FROM tclvar WHERE arrayname='two' +} {vtabJ(two)} +do_execsql_test 161 { + DELETE FROM tclvar WHERE arrayname='two'; + SELECT fullname, value FROM tclvar WHERE name='vtabJ' ORDER BY fullname; +} {vtabJ(1) this vtabJ(3) {a test} vtabJ(5) 55} +do_test 162 { + set res {} + foreach vname [lsort [array names vtabJ]] { + lappend res vtabJ($vname) $vtabJ($vname) + } + set res +} {vtabJ(1) this vtabJ(3) {a test} vtabJ(5) 55} + +# Try to trick the module into updating the same variable twice for a +# single UPDATE statement. +# +do_execsql_test 171 { + INSERT INTO tclvar(fullname, value) VALUES('xx', 'a'); + SELECT name, value FROM tclvar where name = 'xx'; +} {xx a} +do_execsql_test 172 { + UPDATE tclvar SET value = value || 't' + WHERE name = 'xx' OR name = 'x'||'x'; + SELECT name, value FROM tclvar where name = 'xx'; +} {xx at} +do_execsql_test 173 { + UPDATE tclvar SET value = value || 't' + WHERE name = 'xx' OR name BETWEEN 'xx' AND 'xx'; + SELECT name, value FROM tclvar where name = 'xx'; +} {xx att} + +do_execsql_test 181 { + DELETE FROM tclvar WHERE name BETWEEN 'xx' AND 'xx' OR name='xx'; + SELECT name, value FROM tclvar where name = 'xx'; +} {} + +#------------------------------------------------------------------------- + +do_execsql_test 200 { + CREATE TABLE var(k TEXT, v TEXT); + INSERT INTO var VALUES('testvar1', 10); + INSERT INTO var VALUES('testvar2', 20); + INSERT INTO var VALUES('testvar3', 30); +} + +do_test 210 { + foreach {testvar1 testvar2 testvar3} {1 2 3} {} + execsql { + UPDATE tclvar SET value = var.v FROM var WHERE name = var.k; + } + list $testvar1 $testvar2 $testvar3 +} {10 20 30} + +do_test 220 { + execsql { + CREATE TABLE nam(k TEXT, v TEXT); + INSERT INTO nam VALUES('testvar1', 'tv1'); + INSERT INTO nam VALUES('testvar2', 'tv2'); + INSERT INTO nam VALUES('testvar3', 'tv3'); + UPDATE tclvar SET fullname = nam.v FROM nam WHERE name = nam.k; + } + list $tv1 $tv2 $tv3 +} {10 20 30} + + +finish_test diff --git a/test/vtabK.test b/test/vtabK.test new file mode 100644 index 0000000000..07fe9c1312 --- /dev/null +++ b/test/vtabK.test @@ -0,0 +1,83 @@ +# 2020-09-24 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements tests for a strange scenario discovered by +# dbsqlfuzz (0ad6d441f9bf3dfc32626a9900bc1700495b16f9) in which a +# virtual table is named "sqlite_stat1". +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix vtabK + +ifcapable !vtab||!rtree||!fts5 { + finish_test + return +} + +do_execsql_test 100 { + CREATE TABLE t1(x); + INSERT INTO t1 VALUES(123); + PRAGMA writable_schema=ON; + CREATE VIRTUAL TABLE sqlite_stat1 USING fts5(a); + PRAGMA writable_schema=OFF; + CREATE VIRTUAL TABLE t3 USING fts5(b); + INSERT INTO t3 VALUES('this is a test'); +} +do_catchsql_test 110 { + CREATE VIRTUAL TABLE t2 USING rtree(id,x,y); +} {1 {no such column: stat}} +do_execsql_test 120 { + SELECT * FROM t1; +} {123} +do_execsql_test 130 { + INSERT INTO t3(b) VALUES('Four score and seven years ago'); + SELECT * FROM t3 WHERE t3 MATCH 'this'; +} {{this is a test}} +do_execsql_test 140 { + SELECT * FROM t3 WHERE t3 MATCH 'four seven'; +} {{Four score and seven years ago}} +do_execsql_test 150 { + INSERT INTO sqlite_stat1(a) + VALUES('We hold these truths to be self-evident...'); + SELECT * FROM sqlite_stat1; +} {{We hold these truths to be self-evident...}} +do_catchsql_test 160 { + ANALYZE; +} {1 {database disk image is malformed}} +do_execsql_test 170 { + PRAGMA integrity_check; +} {ok} + +# Follow-on dbsqlfuzz bc02a0cde82dee801a8d6f653d2831680f87dca1 +reset_db +do_execsql_test 200 { + CREATE TABLE t1(a); + INSERT INTO t1 VALUES('Ebed-malech'); + CREATE TABLE x(a); + PRAGMA writable_schema=ON; + CREATE VIRTUAL TABLE sqlite_stat1 USING fts5(a); +} {} +do_catchsql_test 210 { + CREATE VIRTUAL TABLE t2 USING rtree(id,x,y); +} {1 {no such column: stat}} +do_execsql_test 220 { + SELECT * FROM t1; +} {Ebed-malech} + +# Follow-on dbsqlfuzz a097eaad43c3c845b236126df92fb49b25449b0c +reset_db +do_catchsql_test 300 { + CREATE VIRTUAL TABLE t1 USING rtree(a,b,c); + CREATE TABLE t2(x); + ALTER TABLE t2 ADD d GENERATED ALWAYS AS (c IN (SELECT 1 FROM t1)) VIRTUAL; +} {1 {error in table t2 after add column: subqueries prohibited in generated columns}} + +finish_test diff --git a/test/vtabL.test b/test/vtabL.test new file mode 100644 index 0000000000..45528edcbd --- /dev/null +++ b/test/vtabL.test @@ -0,0 +1,75 @@ +# 2024-03-26 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix vtabL + +ifcapable !vtab { + finish_test + return +} + +register_tcl_module db + +proc vtab_command {method args} { + switch -- $method { + xConnect { + return $::create_table_sql + } + } + + return {} +} + +foreach {tn cts} { + 1 {SELECT 123} + 2 {SELECT 123, 456} + 3 {INSERT INTO t1 VALUES(5, 6)} + 4 {CREATE INDEX i1 ON t1(a)} + 5 {CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN SELECT 1; END;} + 6 {DROP TABLE nosuchtable} + 7 {DROP TABLE x1} + 8 {DROP TABLE t1} +} { + set ::create_table_sql $cts + do_catchsql_test 1.$tn { + CREATE VIRTUAL TABLE x1 USING tcl(vtab_command); + } {1 {declare_vtab: syntax error}} +} + +foreach {tn cts} { + 9 {CREATE TABLE xyz AS SELECT * FROM sqlite_schema} + 10 {CREATE TABLE xyz AS SELECT 1 AS 'col'} +} { + set ::create_table_sql $cts + do_catchsql_test 1.$tn { + CREATE VIRTUAL TABLE x1 USING tcl(vtab_command); + } {1 {declare_vtab: SQL logic error}} +} + +foreach {tn cts} { + 1 {CREATE TABLE IF NOT EXISTS t1(a, b)} + 2 {CREATE TABLE ""(a, b PRIMARY KEY) WITHOUT ROWID} +} { + set ::create_table_sql $cts + execsql { DROP TABLE IF EXISTS x1 } + do_execsql_test 2.$tn.1 { + CREATE VIRTUAL TABLE x1 USING tcl(vtab_command); + } + do_execsql_test 2.$tn.2 { + SELECT a, b FROM x1 + } +} + +finish_test + diff --git a/test/vtab_alter.test b/test/vtab_alter.test index eb50a28a5e..64053d1484 100644 --- a/test/vtab_alter.test +++ b/test/vtab_alter.test @@ -95,7 +95,7 @@ do_test vtab_alter-2.5 { do_test vtab_alter-3.1 { execsql { CREATE TABLE y_base(a, b, c) } catchsql { ALTER TABLE x RENAME TO y } -} {1 {SQL logic error or missing database}} +} {1 {SQL logic error}} do_test vtab_alter-3.2 { execsql { SELECT * FROM x } } {1 2 3} diff --git a/test/vtab_err.test b/test/vtab_err.test index 068386eb31..cfc5fc3b2b 100644 --- a/test/vtab_err.test +++ b/test/vtab_err.test @@ -20,7 +20,6 @@ ifcapable !vtab { } - unset -nocomplain echo_module_begin_fail do_ioerr_test vtab_err-1 -tclprep { register_echo_module [sqlite3_connection_pointer db] @@ -40,11 +39,6 @@ do_ioerr_test vtab_err-1 -tclprep { COMMIT; } -ifcapable !memdebug { - puts "Skipping vtab_err-2 tests: not compiled with -DSQLITE_MEMDEBUG..." - finish_test - return -} source $testdir/malloc_common.tcl @@ -68,4 +62,26 @@ do_malloc_test vtab_err-2 -tclprep { sqlite3_memdebug_fail -1 +reset_db +register_echo_module [sqlite3_connection_pointer db] +do_execsql_test vtab_err-3.0 { + CREATE TABLE r(a PRIMARY KEY, b, c); + CREATE VIRTUAL TABLE e USING echo(r); +} +faultsim_save_and_close + +do_faultsim_test vtab_err-3 -faults oom-t* -prep { + faultsim_restore_and_reopen + register_echo_module [sqlite3_connection_pointer db] +} -body { + execsql { + BEGIN; + CREATE TABLE xyz(x); + SELECT a FROM e; + COMMIT; + } +} -test { + faultsim_test_result {0 {}} +} + finish_test diff --git a/test/vtabdistinct.test b/test/vtabdistinct.test new file mode 100644 index 0000000000..e18e121e50 --- /dev/null +++ b/test/vtabdistinct.test @@ -0,0 +1,41 @@ +# 2022-01-21 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# This file implements tests for sqlite3_vtab_distinct() interface. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix vtabdistinct + +ifcapable !vtab { + finish_test + return +} +load_static_extension db qpvtab + +do_execsql_test 1.1 { + SELECT ix FROM qpvtab WHERE vn='sqlite3_vtab_distinct'; +} {0} +do_execsql_test 1.2 { + SELECT DISTINCT ix FROM qpvtab WHERE vn='sqlite3_vtab_distinct'; +} {2} +do_execsql_test 1.3 { + SELECT distinct vn, ix FROM qpvtab(3) + WHERE +vn IN ('sqlite3_vtab_distinct','nOrderBy'); +} {nOrderBy 2 sqlite3_vtab_distinct 2} +do_execsql_test 1.4 { + SELECT vn, ix FROM qpvtab + GROUP BY vn + HAVING vn='sqlite3_vtab_distinct'; +} {sqlite3_vtab_distinct 1} + +finish_test diff --git a/test/vtabdrop.test b/test/vtabdrop.test new file mode 100644 index 0000000000..1f9309e465 --- /dev/null +++ b/test/vtabdrop.test @@ -0,0 +1,127 @@ +# 2018 December 28 +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# The tests in this file test edge cases surrounding DROP TABLE on +# virtual tables. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +ifcapable !vtab { finish_test ; return } +source $testdir/fts3_common.tcl +source $testdir/malloc_common.tcl + +set testprefix vtabdrop + +#------------------------------------------------------------------------- +# Test that if a DROP TABLE is executed against an rtree table, but the +# xDestroy() call fails, the rtree table is not dropped, the sqlite_master +# table is not modified and the internal schema remains intact. +# +ifcapable rtree { + do_execsql_test 1.0 { + CREATE VIRTUAL TABLE rt USING rtree(id, x1, x2); + CREATE TABLE t1(x, y); + INSERT INTO t1 VALUES(1, 2); + } + + do_test 1.1 { + execsql { + BEGIN; + INSERT INTO t1 VALUES(3, 4); + } + db eval { SELECT * FROM t1 } { + catchsql { DROP TABLE rt } + } + execsql COMMIT + } {} + + do_execsql_test 1.2 { + SELECT name FROM sqlite_master ORDER BY 1; + SELECT * FROM t1; + SELECT * FROM rt; + } {rt rt_node rt_parent rt_rowid t1 1 2 3 4} + + db close + sqlite3 db test.db + + do_execsql_test 1.3 { + SELECT name FROM sqlite_master ORDER BY 1; + } {rt rt_node rt_parent rt_rowid t1} +} + +#------------------------------------------------------------------------- +# Same as tests 1.*, except with fts5 instead of rtree. +# +ifcapable fts5 { + reset_db + do_execsql_test 2.0 { + CREATE VIRTUAL TABLE ft USING fts5(x); + CREATE TABLE t1(x, y); + INSERT INTO t1 VALUES(1, 2); + } + + do_test 2.1 { + execsql { + BEGIN; + INSERT INTO t1 VALUES(3, 4); + } + db eval { SELECT * FROM t1 } { + catchsql { DROP TABLE ft } + } + execsql COMMIT + } {} + + do_execsql_test 2.2 { + SELECT name FROM sqlite_master ORDER BY 1; + } {ft ft_config ft_content ft_data ft_docsize ft_idx t1} + + db close + sqlite3 db test.db + + do_execsql_test 2.3 { + SELECT name FROM sqlite_master ORDER BY 1; + } {ft ft_config ft_content ft_data ft_docsize ft_idx t1} +} + +#------------------------------------------------------------------------- +# Same as tests 1.*, except with fts3 instead of rtree. +# +ifcapable fts3 { + reset_db + do_execsql_test 2.0 { + CREATE VIRTUAL TABLE ft USING fts3(x); + CREATE TABLE t1(x, y); + INSERT INTO t1 VALUES(1, 2); + } + + do_test 2.1 { + execsql { + BEGIN; + INSERT INTO t1 VALUES(3, 4); + } + db eval { SELECT * FROM t1 } { + catchsql { DROP TABLE ft } + } + execsql COMMIT + } {} + + do_execsql_test 2.2 { + SELECT name FROM sqlite_master ORDER BY 1; + } {ft ft_content ft_segdir ft_segments sqlite_autoindex_ft_segdir_1 t1} + + db close + sqlite3 db test.db + + do_execsql_test 2.3 { + SELECT name FROM sqlite_master ORDER BY 1; + } {ft ft_content ft_segdir ft_segments sqlite_autoindex_ft_segdir_1 t1} +} + +finish_test diff --git a/test/vtabrhs1.test b/test/vtabrhs1.test new file mode 100644 index 0000000000..0476a36f90 --- /dev/null +++ b/test/vtabrhs1.test @@ -0,0 +1,76 @@ +# 2022-01-20 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# This file implements tests for sqlite3_vtab_rhs_value() interface. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix vtabrhs1 + +ifcapable !vtab { + finish_test + return +} +load_static_extension db qpvtab + +# EVIDENCE-OF: R-60223-49197 When the sqlite3_vtab_rhs_value(P,J,V) +# interface is invoked from within the xBestIndex method of a virtual +# table implementation, with P being a copy of the sqlite3_index_info +# object pointer passed into xBestIndex and J being a 0-based index into +# P->aConstraint[], then this routine attempts to set *V to the value +# of the right-hand operand of that constraint if the right-hand operand +# is known. +# +do_execsql_test 1.1 { + SELECT rhs FROM qpvtab + WHERE cn='a' + AND a=12345 +} {12345} +do_execsql_test 1.2 { + SELECT rhs FROM qpvtab + WHERE cn='a' + AND a<>4.5 +} {4.5} +do_execsql_test 1.3 { + SELECT rhs FROM qpvtab + WHERE cn='a' + AND 'quokka' < a +} {'quokka'} +do_execsql_test 1.4 { + SELECT rhs FROM qpvtab + WHERE cn='a' + AND a IS NULL +} {{}} +do_execsql_test 1.5 { + SELECT rhs FROM qpvtab + WHERE cn='a' + AND a GLOB x'0123' +} {x'0123'} + +# EVIDENCE-OF: R-37799-62852 If the right-hand operand is not known, +# then *V is set to a NULL pointer. +# +do_execsql_test 2.1 { + SELECT typeof(rhs) FROM qpvtab WHERE cn='a' AND a=format('abc'); +} {null} +do_execsql_test 2.2 { + SELECT typeof(rhs) FROM qpvtab WHERE cn='a' AND a=?2 +} {null} + +# EVIDENCE-OF: R-14553-25174 When xBestIndex returns, the sqlite3_value +# object returned by sqlite3_vtab_rhs_value() is automatically +# deallocated. +# +# Where this not the case, the following "finish_test" statement would +# report a memory leak. +# +finish_test diff --git a/test/wal.test b/test/wal.test index 92b65e66ba..50988debe3 100644 --- a/test/wal.test +++ b/test/wal.test @@ -43,6 +43,7 @@ proc sqlite3_wal {args} { [lindex $args 0] eval { PRAGMA journal_mode = wal } [lindex $args 0] eval { PRAGMA synchronous = normal } [lindex $args 0] function blob blob + db timeout 1000 } proc log_deleted {logfile} { @@ -1218,7 +1219,7 @@ foreach {tn pgsz works} { set framehdr [binary format IIIIII $pg 5 22 23 $c1 $c2] set fd [open test.db-wal w] - fconfigure $fd -encoding binary -translation binary + fconfigure $fd -translation binary puts -nonewline $fd $walhdr puts -nonewline $fd $framehdr puts -nonewline $fd $framebody @@ -1297,51 +1298,53 @@ do_test wal-19.4 { # At one point, SQLite was failing to grow the mapping of the wal-index # file in step 3 and the checkpoint was corrupting the database file. # -do_test wal-20.1 { - catch {db close} - forcedelete test.db test.db-wal test.db-journal - sqlite3 db test.db - execsql { - PRAGMA journal_mode = WAL; - CREATE TABLE t1(x); - INSERT INTO t1 VALUES(randomblob(900)); - SELECT count(*) FROM t1; - } -} {wal 1} -do_test wal-20.2 { - set ::buddy [launch_testfixture] - testfixture $::buddy { +if {[permutation]!="unix-excl"} { + do_test wal-20.1 { + catch {db close} + forcedelete test.db test.db-wal test.db-journal sqlite3 db test.db - db transaction { db eval { - PRAGMA wal_autocheckpoint = 0; - INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 2 */ - INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 4 */ - INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 8 */ - INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 16 */ - INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 32 */ - INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 64 */ - INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 128 */ - INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 256 */ - INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 512 */ - INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 1024 */ - INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 2048 */ - INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 4096 */ - INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 8192 */ - INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 16384 */ - } } - } -} {0} -do_test wal-20.3 { - close $::buddy - execsql { PRAGMA wal_checkpoint } - execsql { SELECT count(*) FROM t1 } -} {16384} -do_test wal-20.4 { - db close - sqlite3 db test.db - execsql { SELECT count(*) FROM t1 } -} {16384} -integrity_check wal-20.5 + execsql { + PRAGMA journal_mode = WAL; + CREATE TABLE t1(x); + INSERT INTO t1 VALUES(randomblob(900)); + SELECT count(*) FROM t1; + } + } {wal 1} + do_test wal-20.2 { + set ::buddy [launch_testfixture] + testfixture $::buddy { + sqlite3 db test.db + db transaction { db eval { + PRAGMA wal_autocheckpoint = 0; + INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 2 */ + INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 4 */ + INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 8 */ + INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 16 */ + INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 32 */ + INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 64 */ + INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 128 */ + INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 256 */ + INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 512 */ + INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 1024 */ + INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 2048 */ + INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 4096 */ + INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 8192 */ + INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 16384 */ + } } + } + } {0} + do_test wal-20.3 { + close $::buddy + execsql { PRAGMA wal_checkpoint } + execsql { SELECT count(*) FROM t1 } + } {16384} + do_test wal-20.4 { + db close + sqlite3 db test.db + execsql { SELECT count(*) FROM t1 } + } {16384} + integrity_check wal-20.5 +} catch { db2 close } catch { db close } @@ -1378,6 +1381,7 @@ do_test wal-21.3 { #------------------------------------------------------------------------- # Test reading and writing of databases with different page-sizes. # +incr ::do_not_use_codec foreach pgsz {512 1024 2048 4096 8192 16384 32768 65536} { do_multiclient_test tn [string map [list %PGSZ% $pgsz] { do_test wal-22.%PGSZ%.$tn.1 { @@ -1398,6 +1402,7 @@ foreach pgsz {512 1024 2048 4096 8192 16384 32768 65536} { } {0} }] } +incr ::do_not_use_codec -1 #------------------------------------------------------------------------- # Test that when 1 or more pages are recovered from a WAL file, @@ -1510,5 +1515,36 @@ foreach mode {OFF MEMORY PERSIST DELETE TRUNCATE WAL} { db close } +# 2021-03-10 forum post https://sqlite.org/forum/forumpost/a006d86f72 +# +file delete test.db +sqlite3 db test.db +db eval {PRAGMA journal_mode=WAL} +for {set i 0} {$i<$SQLITE_MAX_ATTACHED} {incr i} { + do_test wal-26.1.$i { + file delete attached-$i.db + db eval "ATTACH 'attached-$i.db' AS a$i;" + db eval "PRAGMA a$i.journal_mode=WAL;" + db eval "CREATE TABLE a$i.t$i (x);" + db eval "INSERT INTO t$i VALUES(zeroblob(10000));" + db eval "DELETE FROM t$i;" + db eval "INSERT INTO t$i VALUES(randomblob(10000));" + expr {[file size attached-$i.db-wal]>10000} + } {1} +} +for {set i [expr {$SQLITE_MAX_ATTACHED-1}]} {$i>=0} {incr i -1} { + do_test wal-26.2.$i { + db eval "PRAGMA a$i.wal_checkpoint(TRUNCATE);" + file size attached-$i.db-wal + } {0} + for {set j 0} {$j<$i} {incr j} { + do_test wal-26.2.$i.$j { + expr {[file size attached-$j.db-wal]>10000} + } {1} + } +} +db close + + test_restore_config_pagecache finish_test diff --git a/test/wal2.test b/test/wal2.test index 9d45444d6a..064bed0b20 100644 --- a/test/wal2.test +++ b/test/wal2.test @@ -26,7 +26,7 @@ ifcapable !wal {finish_test ; return } set sqlite_sync_count 0 proc cond_incr_sync_count {adj} { global sqlite_sync_count - if {$::tcl_platform(platform) == "windows"} { + if {$::tcl_platform(os) eq "Windows NT"} { incr sqlite_sync_count $adj } { ifcapable !dirsync { @@ -35,43 +35,6 @@ proc cond_incr_sync_count {adj} { } } -proc set_tvfs_hdr {file args} { - - # Set $nHdr to the number of bytes in the wal-index header: - set nHdr 48 - set nInt [expr {$nHdr/4}] - - if {[llength $args]>2} { - error {wrong # args: should be "set_tvfs_hdr fileName ?val1? ?val2?"} - } - - set blob [tvfs shm $file] - if {$::tcl_platform(byteOrder)=="bigEndian"} {set fmt I} {set fmt i} - - if {[llength $args]} { - set ia [lindex $args 0] - set ib $ia - if {[llength $args]==2} { - set ib [lindex $args 1] - } - binary scan $blob a[expr $nHdr*2]a* dummy tail - set blob [binary format ${fmt}${nInt}${fmt}${nInt}a* $ia $ib $tail] - tvfs shm $file $blob - } - - binary scan $blob ${fmt}${nInt} ints - return $ints -} - -proc incr_tvfs_hdr {file idx incrval} { - set ints [set_tvfs_hdr $file] - set v [lindex $ints $idx] - incr v $incrval - lset ints $idx $v - set_tvfs_hdr $file $ints -} - - #------------------------------------------------------------------------- # Test case wal2-1.*: # @@ -122,8 +85,12 @@ do_test wal2-1.1 { } {4 10} set RECOVER [list \ - {0 1 lock exclusive} {1 7 lock exclusive} \ - {1 7 unlock exclusive} {0 1 unlock exclusive} \ + {0 1 lock exclusive} {1 2 lock exclusive} \ + {4 1 lock exclusive} {4 1 unlock exclusive} \ + {5 1 lock exclusive} {5 1 unlock exclusive} \ + {6 1 lock exclusive} {6 1 unlock exclusive} \ + {7 1 lock exclusive} {7 1 unlock exclusive} \ + {1 2 unlock exclusive} {0 1 unlock exclusive} \ ] set READ [list \ {4 1 lock shared} {4 1 unlock shared} \ @@ -393,8 +360,18 @@ tvfs delete set expected_locks [list] lappend expected_locks {1 1 lock exclusive} ;# Lock checkpoint lappend expected_locks {0 1 lock exclusive} ;# Lock writer -lappend expected_locks {2 6 lock exclusive} ;# Lock recovery & all aReadMark[] -lappend expected_locks {2 6 unlock exclusive} ;# Unlock recovery & aReadMark[] +lappend expected_locks {2 1 lock exclusive} ;# Lock recovery +# lappend expected_locks {4 4 lock exclusive} ;# Lock all aReadMark[] +lappend expected_locks {4 1 lock exclusive} ;# Lock aReadMark[1] +lappend expected_locks {4 1 unlock exclusive} ;# Unlock aReadMark[1] +lappend expected_locks {5 1 lock exclusive} +lappend expected_locks {5 1 unlock exclusive} +lappend expected_locks {6 1 lock exclusive} +lappend expected_locks {6 1 unlock exclusive} +lappend expected_locks {7 1 lock exclusive} +lappend expected_locks {7 1 unlock exclusive} +lappend expected_locks {2 1 unlock exclusive} ;# Unlock recovery +# lappend expected_locks {4 4 unlock exclusive} ;# Unlock all aReadMark[] lappend expected_locks {0 1 unlock exclusive} ;# Unlock writer lappend expected_locks {3 1 lock exclusive} ;# Lock aReadMark[0] lappend expected_locks {3 1 unlock exclusive} ;# Unlock aReadMark[0] @@ -582,15 +559,23 @@ do_test wal2-6.3.4 { BEGIN; INSERT INTO t1 VALUES('Groucho'); } - list [file exists test.db-wal] [file exists test.db-journal] -} {0 1} +} {} +if {[atomic_batch_write test.db]==0} { + do_test wal2-6.3.4.1 { + list [file exists test.db-wal] [file exists test.db-journal] + } {0 1} +} do_test wal2-6.3.5 { execsql { PRAGMA lock_status } } {main exclusive temp closed} do_test wal2-6.3.6 { execsql { COMMIT } - list [file exists test.db-wal] [file exists test.db-journal] -} {0 1} +} {} +if {[atomic_batch_write test.db]==0} { + do_test wal2-6.3.6.1 { + list [file exists test.db-wal] [file exists test.db-journal] + } {0 1} +} do_test wal2-6.3.7 { execsql { PRAGMA lock_status } } {main exclusive temp closed} @@ -615,8 +600,12 @@ do_test wal2-6.4.1 { } {} set RECOVERY { - {0 1 lock exclusive} {1 7 lock exclusive} - {1 7 unlock exclusive} {0 1 unlock exclusive} + {0 1 lock exclusive} {1 2 lock exclusive} + {4 1 lock exclusive} {4 1 unlock exclusive} + {5 1 lock exclusive} {5 1 unlock exclusive} + {6 1 lock exclusive} {6 1 unlock exclusive} + {7 1 lock exclusive} {7 1 unlock exclusive} + {1 2 unlock exclusive} {0 1 unlock exclusive} } set READMARK0_READ { {3 1 lock shared} {3 1 unlock shared} @@ -1049,7 +1038,7 @@ tvfs delete # the new files with the same file-system permissions as the database # file itself. Test this. # -if {$::tcl_platform(platform) == "unix"} { +if {$::tcl_platform(os) ne "Windows NT"} { faultsim_delete_and_reopen # Changed on 2012-02-13: umask is deliberately ignored for -wal files. #set umask [exec /bin/sh -c umask] @@ -1072,10 +1061,14 @@ if {$::tcl_platform(platform) == "unix"} { 3 00600 4 00755 } { - set effective [format %.5o [expr $permissions & ~$umask]] + if {$tcl_version>=9.0} { + set effective [format %.5d [expr $permissions & ~$umask]] + } else { + set effective [format %.5o [expr $permissions & ~$umask]] + } do_test wal2-12.2.$tn.1 { file attributes test.db -permissions $permissions - file attributes test.db -permissions + string map {o 0} [file attributes test.db -permissions] } $permissions do_test wal2-12.2.$tn.2 { list [file exists test.db-wal] [file exists test.db-shm] @@ -1086,7 +1079,8 @@ if {$::tcl_platform(platform) == "unix"} { list [file exists test.db-wal] [file exists test.db-shm] } {1 1} do_test wal2-12.2.$tn.4 { - list [file attr test.db-wal -perm] [file attr test.db-shm -perm] + set x [list [file attr test.db-wal -perm] [file attr test.db-shm -perm]] + string map {o 0} $x } [list $effective $effective] do_test wal2-12.2.$tn.5 { db close @@ -1100,7 +1094,7 @@ if {$::tcl_platform(platform) == "unix"} { # database, wal or shm files cannot be opened, or can only be opened # read-only. # -if {$::tcl_platform(platform) == "unix"} { +if {$::tcl_platform(os) ne "Windows NT"} { proc perm {} { set L [list] foreach f {test.db test.db-wal test.db-shm} { @@ -1128,7 +1122,7 @@ if {$::tcl_platform(platform) == "unix"} { foreach {tn db_perm wal_perm shm_perm can_open can_read can_write} { 2 00644 00644 00644 1 1 1 3 00644 00400 00644 1 1 0 - 4 00644 00644 00400 1 0 0 + 4 00644 00644 00400 1 1 0 5 00400 00644 00644 1 1 0 7 00644 00000 00644 1 0 0 @@ -1144,6 +1138,7 @@ if {$::tcl_platform(platform) == "unix"} { set L [file attr test.db -perm] lappend L [file attr test.db-wal -perm] lappend L [file attr test.db-shm -perm] + string map {o 0} $L } [list $db_perm $wal_perm $shm_perm] # If $can_open is true, then it should be possible to open a database @@ -1191,12 +1186,15 @@ if {$::tcl_platform(platform) == "unix"} { # foreach {tn sql reslist} { 1 { } {10 0 4 0 6 0} - 2 { PRAGMA checkpoint_fullfsync = 1 } {10 4 4 2 6 2} + 2 { PRAGMA checkpoint_fullfsync = 1 } {10 6 4 3 6 3} 3 { PRAGMA checkpoint_fullfsync = 0 } {10 0 4 0 6 0} } { + ifcapable default_ckptfullfsync { + if {[string trim $sql]==""} continue + } faultsim_delete_and_reopen - execsql {PRAGMA auto_vacuum = 0} + execsql {PRAGMA auto_vacuum = 0; PRAGMA synchronous = FULL;} execsql $sql do_execsql_test wal2-14.$tn.0 { PRAGMA page_size = 4096 } {} do_execsql_test wal2-14.$tn.1 { PRAGMA journal_mode = WAL } {wal} @@ -1258,8 +1256,8 @@ foreach {tn settings restart_sync commit_sync ckpt_sync} { 6 {0 1 full} {0 2} {0 1} {0 2} 7 {1 0 off} {0 0} {0 0} {0 0} - 8 {1 0 normal} {1 0} {0 0} {0 2} - 9 {1 0 full} {2 0} {1 0} {0 2} + 8 {1 0 normal} {0 1} {0 0} {0 2} + 9 {1 0 full} {1 1} {1 0} {0 2} 10 {1 1 off} {0 0} {0 0} {0 0} 11 {1 1 normal} {0 1} {0 0} {0 2} diff --git a/test/wal3.test b/test/wal3.test index da3d318773..cb28d0f0b9 100644 --- a/test/wal3.test +++ b/test/wal3.test @@ -12,6 +12,7 @@ # focus of this file is testing the operation of the library in # "PRAGMA journal_mode=WAL" mode. # +# TESTRUNNER: slow set testdir [file dirname $argv0] source $testdir/tester.tcl @@ -220,6 +221,7 @@ foreach {tn syncmode synccount} { sqlite3 db test.db -vfs T execsql "PRAGMA synchronous = $syncmode" + execsql "PRAGMA checkpoint_fullfsync = 0" execsql { PRAGMA journal_mode = WAL } execsql { CREATE TABLE filler(a,b,c); } diff --git a/test/wal5.test b/test/wal5.test index 360d9c911e..50c517286a 100644 --- a/test/wal5.test +++ b/test/wal5.test @@ -18,6 +18,7 @@ source $testdir/tester.tcl source $testdir/lock_common.tcl source $testdir/wal_common.tcl ifcapable !wal {finish_test ; return } +do_not_use_codec set testprefix wal5 @@ -140,13 +141,13 @@ foreach {testprefix do_wal_checkpoint} { do_test 1.$tn.7 { reopen_all list [db_page_count] [wal_page_count] $::nBusyHandler - } {7 0 0} + } [expr {[nonzero_reserved_bytes]?"/# # 0/":"7 0 0"}] do_test 1.$tn.8 { sql2 { BEGIN ; SELECT x FROM t1 } } {1 2 3 4 5} do_test 1.$tn.9 { sql1 { INSERT INTO t1 VALUES(6, zeroblob(1200)) } list [db_page_count] [wal_page_count] $::nBusyHandler - } {7 5 0} + } [expr {[nonzero_reserved_bytes]?"/# # #/":"7 5 0"}] do_test 1.$tn.10 { sql3 { BEGIN ; SELECT x FROM t1 } } {1 2 3 4 5 6} set ::busy_handler_script { @@ -157,7 +158,7 @@ foreach {testprefix do_wal_checkpoint} { do_test 1.$tn.11 { code1 { do_wal_checkpoint db -mode restart } list [db_page_count] [wal_page_count] $::nBusyHandler - } {10 5 8} + } [expr {[nonzero_reserved_bytes]?"/# # #/":"10 5 8"}] do_test 1.$tn.12 { set ::db_file_size } 10 } diff --git a/test/wal6.test b/test/wal6.test index 2574a64a84..081608cd30 100644 --- a/test/wal6.test +++ b/test/wal6.test @@ -46,7 +46,7 @@ foreach jmode $all_journal_modes { # Under Windows, you'll get an error trying to delete # a file this is already opened. Close the first connection # so the other tests work. -if {$tcl_platform(platform)=="windows"} { +if {$::tcl_platform(os) eq "Windows NT"} { if {$jmode=="persist" || $jmode=="truncate"} { db close } @@ -61,7 +61,7 @@ if {$tcl_platform(platform)=="windows"} { } db2 } {wal 1 2 3 4} -if {$tcl_platform(platform)=="windows"} { +if {$::tcl_platform(os) eq "Windows NT"} { if {$jmode=="persist" || $jmode=="truncate"} { sqlite3 db test.db } @@ -234,5 +234,36 @@ do_test 4.4.2 { catchsql { SELECT * FROM t2 } db2 } {1 {database disk image is malformed}} +#------------------------------------------------------------------------- +# Confirm that it is possible to get an SQLITE_BUSY_SNAPSHOT error from +# "BEGIN EXCLUSIVE" if the connection already has an open read-transaction. +# +db close +db2 close +reset_db +sqlite3 db2 test.db +do_execsql_test 5.1 { + PRAGMA journal_mode = wal; + CREATE TABLE t1(x, y); + INSERT INTO t1 VALUES(1, 2); + INSERT INTO t1 VALUES(3, 4); +} {wal} +do_test 5.2 { + set res [list] + db eval { + SELECT * FROM t1 + } { + if {$x==1} { + db2 eval { INSERT INTO t1 VALUES(5, 6) } + } + if {$x==3} { + set res [catchsql {BEGIN EXCLUSIVE}] + lappend res [sqlite3_extended_errcode db] + } + } + set res +} {1 {database is locked} SQLITE_BUSY_SNAPSHOT} + + finish_test diff --git a/test/wal64k.test b/test/wal64k.test index e962da128e..bacb14328a 100644 --- a/test/wal64k.test +++ b/test/wal64k.test @@ -19,10 +19,10 @@ set testprefix wal64k ifcapable !wal {finish_test ; return } -if {$tcl_platform(platform) != "unix"} { +if {[llength [info commands test_syscall]]==0} { finish_test return -} +} db close test_syscall pagesize 65536 @@ -46,6 +46,18 @@ do_test 1.2 { integrity_check 1.3 +db close +forcedelete test.db +sqlite3 db test.db -vfs unix-excl +do_execsql_test 2.1 { + PRAGMA page_size=512; + PRAGMA journal_mode=WAL; + CREATE TABLE t1(a,b); + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<8200) + INSERT INTO t1(a,b) SELECT x, zeroblob(300) FROM c; + PRAGMA integrity_check; +} {wal ok} + db close test_syscall pagesize -1 finish_test diff --git a/test/wal8.test b/test/wal8.test index 0682fce35b..3e5a0c2617 100644 --- a/test/wal8.test +++ b/test/wal8.test @@ -27,6 +27,7 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl set ::testprefix wal8 ifcapable !wal {finish_test ; return } +do_not_use_codec db close forcedelete test.db test.db-wal diff --git a/test/wal_common.tcl b/test/wal_common.tcl index 917ad598f6..d31131aa02 100644 --- a/test/wal_common.tcl +++ b/test/wal_common.tcl @@ -65,7 +65,6 @@ proc wal_set_walhdr {filename {intlist {}}} { set fd [open $filename r+] fconfigure $fd -translation binary - fconfigure $fd -encoding binary seek $fd 0 puts -nonewline $fd $blob close $fd @@ -73,7 +72,6 @@ proc wal_set_walhdr {filename {intlist {}}} { set fd [open $filename] fconfigure $fd -translation binary - fconfigure $fd -encoding binary set blob [read $fd 24] close $fd @@ -90,4 +88,42 @@ proc wal_fix_walindex_cksum {hdrvar} { lset hdr 11 $c2 } +# This command assumes that $file is the name of a database file opened +# in wal mode using a [testvfs] VFS. It returns a list of the 12 32-bit +# integers that make up the wal-index-header for the named file. +# +proc set_tvfs_hdr {file args} { + + # Set $nHdr to the number of bytes in the wal-index header: + set nHdr 48 + set nInt [expr {$nHdr/4}] + + if {[llength $args]>2} { + error {wrong # args: should be "set_tvfs_hdr fileName ?val1? ?val2?"} + } + + set blob [tvfs shm $file] + if {$::tcl_platform(byteOrder)=="bigEndian"} {set fmt I} {set fmt i} + if {[llength $args]} { + set ia [lindex $args 0] + set ib $ia + if {[llength $args]==2} { + set ib [lindex $args 1] + } + binary scan $blob a[expr $nHdr*2]a* dummy tail + set blob [binary format ${fmt}${nInt}${fmt}${nInt}a* $ia $ib $tail] + tvfs shm $file $blob + } + + binary scan $blob ${fmt}${nInt} ints + return $ints +} + +proc incr_tvfs_hdr {file idx incrval} { + set ints [set_tvfs_hdr $file] + set v [lindex $ints $idx] + incr v $incrval + lset ints $idx $v + set_tvfs_hdr $file $ints +} diff --git a/test/walbak.test b/test/walbak.test index 303a628a84..0e0f999534 100644 --- a/test/walbak.test +++ b/test/walbak.test @@ -127,6 +127,7 @@ do_test walbak-2.1 { } } {} do_test walbak-2.2 { + forcedelete abc.db db backup abc.db sqlite3 db2 abc.db string compare [sig db] [sig db2] @@ -239,6 +240,7 @@ foreach {tn setup} { } } { + if {$tn==4 && [sqlite3 -has-codec]} continue foreach f [glob -nocomplain test.db*] { forcedelete $f } eval $setup diff --git a/test/walblock.test b/test/walblock.test index 23167a8830..86a52b3f96 100644 --- a/test/walblock.test +++ b/test/walblock.test @@ -17,7 +17,7 @@ source $testdir/wal_common.tcl finish_test; return; # Feature currently not implemented. ifcapable !wal {finish_test ; return } -if {$::tcl_platform(platform)!="unix"} { finish_test ; return } +if {$::tcl_platform(platform) ne "unix"} { finish_test ; return } set testprefix walblock catch { db close } diff --git a/test/walckptnoop.test b/test/walckptnoop.test new file mode 100644 index 0000000000..7ff8e90b8f --- /dev/null +++ b/test/walckptnoop.test @@ -0,0 +1,110 @@ +# 2025 September 5 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is testing the operation of the library in +# "PRAGMA wal_checkpoint = noop" mode. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/lock_common.tcl +source $testdir/malloc_common.tcl +source $testdir/wal_common.tcl + +set testprefix walckptnoop + +ifcapable !wal {finish_test ; return } + +set VAL 123 + +proc myrand {} { + global VAL + + set A 1103515245 + set C 12345 + set M 2147483648 + + set VAL [expr {($A * $VAL + $C) % $M}] + return $VAL +} + +proc myrandomblob {n} { + set l [list] + for {set i 0} {$i < $n} {incr i} { + lappend l [expr [myrand] % 256] + } + binary format c* $l +} + +db func myrandomblob myrandomblob + + +do_execsql_test 1.0 { + PRAGMA page_size=1024; + PRAGMA auto_vacuum=NONE; + PRAGMA secure_delete=OFF; + VACUUM; + CREATE TABLE t1(x INTEGER PRIMARY KEY, y TEXT); + CREATE INDEX i1 ON t1(y); + PRAGMA journal_mode = wal; + + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<1000 + ) + INSERT INTO t1 SELECT NULL, hex(myrandomblob(64)) FROM s; +} {0 wal} + +do_execsql_test 1.1 { + PRAGMA wal_checkpoint = noop; +} {0 298 0} +do_execsql_test 1.2 { + PRAGMA wal_checkpoint = noop; +} {0 298 0} +do_execsql_test 1.3 { + PRAGMA wal_checkpoint = passive; +} {0 298 298} +do_execsql_test 1.4 { + PRAGMA wal_checkpoint = noop; +} {0 298 298} + +db_save_and_close +db_restore_and_reopen +do_execsql_test 1.5 { + PRAGMA wal_checkpoint = noop; +} {0 298 0} + +db close +sqlite3 db test.db +db eval { + PRAGMA auto_vacuum=NONE; + PRAGMA secure_delete=OFF; +} +do_execsql_test 1.6 { + PRAGMA wal_checkpoint = noop; +} {0 0 0} + +do_catchsql_test 1.7 { + BEGIN; + DELETE FROM t1; + PRAGMA wal_checkpoint = noop; +} {1 {database table is locked}} + +do_catchsql_test 1.8 { + COMMIT; + PRAGMA wal_checkpoint = noop; +} {0 {0 5 0}} + +do_execsql_test 1.9 { + PRAGMA journal_mode = delete; + PRAGMA wal_checkpoint = noop; +} {delete 0 -1 -1} + +finish_test diff --git a/test/walcksum.test b/test/walcksum.test index f3fc427115..0c9a7e55c0 100644 --- a/test/walcksum.test +++ b/test/walcksum.test @@ -16,13 +16,13 @@ source $testdir/lock_common.tcl source $testdir/wal_common.tcl ifcapable !wal {finish_test ; return } +set testprefix walcksum # Read and return the contents of file $filename. Treat the content as # binary data. # proc readfile {filename} { set fd [open $filename] - fconfigure $fd -encoding binary fconfigure $fd -translation binary set data [read $fd] close $fd @@ -59,7 +59,6 @@ proc log_checksum_write {filename iFrame endian} { set bin [binary format II $c1 $c2] set fd [open $filename r+] - fconfigure $fd -encoding binary fconfigure $fd -translation binary seek $fd $offset puts -nonewline $fd $bin @@ -114,7 +113,6 @@ proc log_checksum_writemagic {filename endian} { set val [expr {0x377f0682 | ($endian == "big" ? 1 : 0)}] set bin [binary format I $val] set fd [open $filename r+] - fconfigure $fd -encoding binary fconfigure $fd -translation binary puts -nonewline $fd $bin @@ -334,5 +332,152 @@ do_test walcksum-2.1 { catch { db close } catch { db2 close } +#------------------------------------------------------------------------- +# Test cases based on the bug reported at: +# +# <https://sqlite.org/forum/forumpost/b490f726db> +# +reset_db + +do_execsql_test 3.0 { + PRAGMA auto_vacuum = 0; + PRAGMA synchronous = NORMAL; + PRAGMA journal_mode = WAL; + PRAGMA cache_size = 1; + + CREATE TABLE t1 (i INTEGER PRIMARY KEY, b BLOB, t TEXT); + PRAGMA wal_checkpoint; + INSERT INTO t1 VALUES(1, randomblob(2048), 'one'); +} {wal 0 2 2} + +do_execsql_test 3.1 { + BEGIN; + INSERT INTO t1 VALUES(2, randomblob(2048), 'two'); + SAVEPOINT one; + INSERT INTO t1 VALUES(3, randomblob(2048), 'three'); + INSERT INTO t1 VALUES(4, randomblob(2048), 'four'); + INSERT INTO t1 VALUES(5, randomblob(2048), 'five'); + INSERT INTO t1 VALUES(6, randomblob(2048), 'six'); + INSERT INTO t1 VALUES(7, randomblob(2048), 'seven'); + + UPDATE t1 SET b=randomblob(2048) WHERE i=5; + UPDATE t1 SET b=randomblob(2048) WHERE i=6; + UPDATE t1 SET b=randomblob(2048) WHERE i=7; + ROLLBACK TO one; + INSERT INTO t1 VALUES(8, NULL, 'eight'); + COMMIT; +} {} + +do_execsql_test 3.2 { + SELECT i, t FROM t1 +} {1 one 2 two 8 eight} + +forcecopy test.db test2.db +forcecopy test.db-wal test2.db-wal + +sqlite3 db2 test2.db +do_test 1.3 { + execsql { + SELECT i, t FROM t1 + } db2 +} {1 one 2 two 8 eight} + +catch { db2 close } + +#------------------------------------------------------------------------- +reset_db + +do_execsql_test 4.0 { + PRAGMA auto_vacuum = 0; + PRAGMA synchronous = NORMAL; + PRAGMA journal_mode = WAL; + PRAGMA cache_size = 1; + + CREATE TABLE t1 (i INTEGER PRIMARY KEY, b BLOB, t TEXT); + PRAGMA wal_checkpoint; + INSERT INTO t1 VALUES(1, randomblob(2048), 'one'); +} {wal 0 2 2} + +do_execsql_test 4.1.1 { + SAVEPOINT one; + INSERT INTO t1 VALUES(2, randomblob(2048), 'two'); + INSERT INTO t1 VALUES(3, randomblob(2048), 'three'); + INSERT INTO t1 VALUES(4, randomblob(2048), 'four'); + INSERT INTO t1 VALUES(5, randomblob(2048), 'five'); + INSERT INTO t1 VALUES(6, randomblob(2048), 'six'); + INSERT INTO t1 VALUES(7, randomblob(2048), 'seven'); + + UPDATE t1 SET b=randomblob(2048) WHERE i=5; + UPDATE t1 SET b=randomblob(2048) WHERE i=6; + UPDATE t1 SET b=randomblob(2048) WHERE i=7; +} + +do_execsql_test 4.1.2 { + ROLLBACK TO one; + INSERT INTO t1 VALUES(8, NULL, 'eight'); + RELEASE one; +} {} + +do_execsql_test 4.2 { + SELECT i, t FROM t1 +} {1 one 8 eight} + +forcecopy test.db test2.db +forcecopy test.db-wal test2.db-wal + +sqlite3 db2 test2.db +do_test 4.3 { + execsql { + SELECT i, t FROM t1 + } db2 +} {1 one 8 eight} + +catch { db2 close } + +#------------------------------------------------------------------------- +reset_db + +do_execsql_test 5.0 { + PRAGMA auto_vacuum = 0; + PRAGMA synchronous = NORMAL; + PRAGMA journal_mode = WAL; + PRAGMA cache_size = 1; + + CREATE TABLE t1 (i INTEGER PRIMARY KEY, b BLOB, t TEXT); + INSERT INTO t1 VALUES(1, randomblob(2048), 'one'); + INSERT INTO t1 VALUES(2, randomblob(2048), 'two'); + INSERT INTO t1 VALUES(3, randomblob(2048), 'three'); + PRAGMA wal_checkpoint; +} {wal 0 14 14} + +do_execsql_test 5.1 { + BEGIN; + SELECT count(*) FROM t1; + SAVEPOINT one; + INSERT INTO t1 VALUES(4, randomblob(2048), 'four'); + INSERT INTO t1 VALUES(5, randomblob(2048), 'five'); + INSERT INTO t1 VALUES(6, randomblob(2048), 'six'); + INSERT INTO t1 VALUES(7, randomblob(2048), 'seven'); + ROLLBACK TO one; + INSERT INTO t1 VALUES(8, randomblob(2048), 'eight'); + INSERT INTO t1 VALUES(9, randomblob(2048), 'nine'); + COMMIT; +} {3} + +forcecopy test.db test2.db +forcecopy test.db-wal test2.db-wal + +sqlite3 db2 test2.db +do_test 5.2 { + execsql { + SELECT i, t FROM t1 + } db2 +} {1 one 2 two 3 three 8 eight 9 nine} +db2 close + +do_execsql_test 5.3 { + SELECT i, t FROM t1 +} {1 one 2 two 3 three 8 eight 9 nine} + finish_test diff --git a/test/walcrash4.test b/test/walcrash4.test new file mode 100644 index 0000000000..43292def42 --- /dev/null +++ b/test/walcrash4.test @@ -0,0 +1,85 @@ +# 2010 May 25 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/lock_common.tcl +source $testdir/wal_common.tcl +ifcapable !wal {finish_test ; return } +set testprefix walcrash4 +do_not_use_codec + +#------------------------------------------------------------------------- +# At one point, if "PRAGMA synchronous=full" is set and the platform +# does not support POWERSAFE_OVERWRITE, and the last frame written to +# the wal file in a transaction is aligned with a sector boundary, the +# xSync() call was omitted. +# +# The following test verifies that this has been fixed. +# +do_execsql_test 1.0 { + PRAGMA autovacuum = 0; + PRAGMA page_size = 1024; + PRAGMA journal_mode = wal; + PRAGMA main.synchronous = full; +} {wal} + +faultsim_save_and_close + +# The error message is different on unix and windows +# +if {$::tcl_platform(platform) eq "windows"} { + set msg "child killed: unknown signal" +} else { + set msg "child process exited abnormally" +} + +for {set nExtra 0} {$nExtra < 10} {incr nExtra} { + for {set i 0} {$i < 10} {incr i} { + do_test 1.nExtra=$nExtra.i=$i.1 { + faultsim_restore_and_reopen + + set fd [open crash.tcl w] + puts $fd [subst -nocommands { + sqlite3_crash_enable 1 + sqlite3_test_control_pending_byte $::sqlite_pending_byte + sqlite3 db test.db -vfs crash + db eval { + PRAGMA main.synchronous=FULL; + BEGIN; + CREATE TABLE t1(x UNIQUE); + } + for {set e 2} {[set e] < ($nExtra+2)} {incr e} { + db eval "CREATE TABLE t[set e] (x)" + } + db eval { + INSERT INTO t1 VALUES( randomblob(170000) ); + COMMIT; + } + sqlite3_crash_now + }] + close $fd + + set r [catch { exec [info nameofexec] crash.tcl >@stdout } msg] + list $r $msg + } "1 {$msg}" + + do_execsql_test 1.nExtra=$nExtra.i=$i.2 { + SELECT count(*) FROM t1; + PRAGMA integrity_check; + } {1 ok} + } +} + + +finish_test diff --git a/test/walfault.test b/test/walfault.test index 4e7064d53b..6cb760b258 100644 --- a/test/walfault.test +++ b/test/walfault.test @@ -552,7 +552,7 @@ do_faultsim_test walfault-14 -prep { #------------------------------------------------------------------------- # Test fault-handling when switching out of exclusive-locking mode. # -do_test walfault-14-pre { +do_test walfault-15-pre { faultsim_delete_and_reopen execsql { PRAGMA auto_vacuum = 0; @@ -565,7 +565,7 @@ do_test walfault-14-pre { } faultsim_save_and_close } {} -do_faultsim_test walfault-14 -prep { +do_faultsim_test walfault-15 -prep { faultsim_restore_and_reopen execsql { SELECT count(*) FROM abc; diff --git a/test/walfault2.test b/test/walfault2.test new file mode 100644 index 0000000000..a4c352be29 --- /dev/null +++ b/test/walfault2.test @@ -0,0 +1,91 @@ +# 2010 May 03 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is testing the operation of the library in +# "PRAGMA journal_mode=WAL" mode. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/malloc_common.tcl +source $testdir/lock_common.tcl + +ifcapable !wal {finish_test ; return } +set testprefix walfault2 + +#------------------------------------------------------------------------- +# Inject faults while truncating the wal file. +# +do_execsql_test 1.0 { + PRAGMA auto_vacuum = 0; + CREATE TABLE t1(a, b); + PRAGMA journal_mode = wal; + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s LIMIT 30 + ) + INSERT INTO t1 SELECT randomblob(400), randomblob(400) FROM s; +} {wal} +faultsim_save_and_close + +do_faultsim_test 1 -prep { + catch { db close } + faultsim_restore + sqlite3 db file:test.db?psow=0 -uri 1 + file_control_powersafe_overwrite db 0 + execsql { + PRAGMA wal_checkpoint; + PRAGMA journal_size_limit = 10000; + PRAGMA synchronous = full; + } +} -body { + execsql { INSERT INTO t1 VALUES(1,1) } +} -test { + faultsim_test_result {0 {}} +} + +#------------------------------------------------------------------------- +# Inject faults while rewriting checksums. +# +reset_db +do_execsql_test 2.0 { + PRAGMA auto_vacuum = 0; + CREATE TABLE t1(a, b); + PRAGMA journal_mode = wal; + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s LIMIT 30 + ) + INSERT INTO t1 SELECT randomblob(400), randomblob(400) FROM s; +} {wal} +faultsim_save_and_close + +do_faultsim_test 2 -prep { + faultsim_restore_and_reopen + execsql { + PRAGMA cache_size = 2; + BEGIN; + UPDATE t1 SET a=randomblob(400); + UPDATE t1 SET b=randomblob(400); + UPDATE t1 SET a=randomblob(400); + UPDATE t1 SET b=randomblob(400); + UPDATE t1 SET a=randomblob(400); + UPDATE t1 SET b=randomblob(400); + UPDATE t1 SET a=randomblob(400); + UPDATE t1 SET b=randomblob(400); + } +} -body { + execsql COMMIT +} -test { + faultsim_test_result {0 {}} +} + + + +finish_test diff --git a/test/walmode.test b/test/walmode.test index 4e14d54d4f..1c3325acf2 100644 --- a/test/walmode.test +++ b/test/walmode.test @@ -45,15 +45,17 @@ do_test walmode-1.2 { file size test.db } {1024} -set expected_sync_count 3 -if {$::tcl_platform(platform)!="windows"} { - ifcapable dirsync { - incr expected_sync_count +if {[atomic_batch_write test.db]==0} { + set expected_sync_count 3 + if {$::tcl_platform(os) ne "Windows NT"} { + ifcapable dirsync { + incr expected_sync_count + } } + do_test walmode-1.3 { + set sqlite_sync_count + } $expected_sync_count } -do_test walmode-1.3 { - set sqlite_sync_count -} $expected_sync_count do_test walmode-1.4 { file exists test.db-wal @@ -106,9 +108,11 @@ do_test walmode-4.1 { execsql { INSERT INTO t1 VALUES(1, 2) } execsql { PRAGMA journal_mode = persist } } {persist} -do_test walmode-4.2 { - list [file exists test.db-journal] [file exists test.db-wal] -} {1 0} +if {[atomic_batch_write test.db]==0} { + do_test walmode-4.2 { + list [file exists test.db-journal] [file exists test.db-wal] + } {1 0} +} do_test walmode-4.3 { execsql { SELECT * FROM t1 } } {1 2} @@ -117,9 +121,11 @@ do_test walmode-4.4 { sqlite3 db test.db execsql { SELECT * FROM t1 } } {1 2} -do_test walmode-4.5 { - list [file exists test.db-journal] [file exists test.db-wal] -} {1 0} +if {[atomic_batch_write test.db]==0} { + do_test walmode-4.5 { + list [file exists test.db-journal] [file exists test.db-wal] + } {1 0} +} # Test that nothing goes wrong if a connection is prevented from changing # from WAL to rollback mode because a second connection has the database diff --git a/test/walnoshm.test b/test/walnoshm.test index d4082178dd..f546c29b13 100644 --- a/test/walnoshm.test +++ b/test/walnoshm.test @@ -104,6 +104,7 @@ do_test 2.1.5 { } {exclusive delete a b c d e f g h} do_test 2.2.1 { + db2 close forcecopy test.db test2.db forcecopy test.db-wal test2.db-wal sqlite3 db3 test2.db -vfs tvfsshm diff --git a/test/walpersist.test b/test/walpersist.test index 692728dda4..73e6de13d4 100644 --- a/test/walpersist.test +++ b/test/walpersist.test @@ -121,6 +121,22 @@ do_test walpersist-3.4 { sqlite3 db test.db execsql { PRAGMA integrity_check } } {ok} - + +# 2023-05-07 https://sqlite.org/forum/forumpost/8130545bc6 +# +reset_db +do_test 4.1 { + db eval { + PRAGMA journal_mode=WAL; + CREATE TABLE t1(x); + } + file_control_persist_wal db 1 + db eval { + PRAGMA journal_mode=TRUNCATE; + PRAGMA journal_mode=MEMORY; + PRAGMA journal_mode=WAL; + PRAGMA journal_mode=PERSIST; + } +} {truncate memory wal persist} finish_test diff --git a/test/walprotocol.test b/test/walprotocol.test index d658de9d7c..a262cdd76d 100644 --- a/test/walprotocol.test +++ b/test/walprotocol.test @@ -52,24 +52,34 @@ do_test 1.1 { set ::locks [list] sqlite3 db test.db -vfs T execsql { SELECT * FROM x } - lrange $::locks 0 3 -} [list {0 1 lock exclusive} {1 7 lock exclusive} \ - {1 7 unlock exclusive} {0 1 unlock exclusive} \ + lrange $::locks 0 11 +} [list {0 1 lock exclusive} {1 2 lock exclusive} \ + {4 1 lock exclusive} {4 1 unlock exclusive} \ + {5 1 lock exclusive} {5 1 unlock exclusive} \ + {6 1 lock exclusive} {6 1 unlock exclusive} \ + {7 1 lock exclusive} {7 1 unlock exclusive} \ + {1 2 unlock exclusive} \ + {0 1 unlock exclusive} \ ] do_test 1.2 { db close set ::locks [list] sqlite3 db test.db -vfs T execsql { SELECT * FROM x } - lrange $::locks 0 3 -} [list {0 1 lock exclusive} {1 7 lock exclusive} \ - {1 7 unlock exclusive} {0 1 unlock exclusive} \ + lrange $::locks 0 11 +} [list {0 1 lock exclusive} {1 2 lock exclusive} \ + {4 1 lock exclusive} {4 1 unlock exclusive} \ + {5 1 lock exclusive} {5 1 unlock exclusive} \ + {6 1 lock exclusive} {6 1 unlock exclusive} \ + {7 1 lock exclusive} {7 1 unlock exclusive} \ + {1 2 unlock exclusive} \ + {0 1 unlock exclusive} \ ] proc lock_callback {method filename handle lock} { - if {$lock == "1 7 lock exclusive"} { return SQLITE_BUSY } + if {$lock == "1 2 lock exclusive"} { return SQLITE_BUSY } return SQLITE_OK } -puts "# Warning: This next test case causes SQLite to call xSlee(1) 100 times." +puts "# Warning: This next test case causes SQLite to call xSleep(1) 100 times." puts "# Normally this equates to a delay of roughly 10 seconds, but if SQLite" puts "# is built on unix without HAVE_USLEEP defined, it may be much longer." do_test 1.3 { @@ -90,6 +100,18 @@ do_test 1.4 { sqlite3 db test.db -vfs T catchsql { SELECT * FROM x } } {1 {locking protocol}} + +puts "# Warning: Third time!" +proc lock_callback {method filename handle lock} { + if {$lock == "4 4 lock exclusive"} { return SQLITE_BUSY } + return SQLITE_OK +} +do_test 1.5 { + db close + set ::locks [list] + sqlite3 db test.db -vfs T + catchsql { SELECT * FROM x } +} {0 z} db close T delete @@ -135,19 +157,20 @@ T filter xShmLock T script lock_callback proc lock_callback {method file handle spec} { - if {$spec == "1 7 unlock exclusive"} { + if {$spec == "1 2 unlock exclusive"} { T filter {} set ::r [catchsql { SELECT * FROM b } db2] } } sqlite3 db test.db sqlite3 db2 test.db +puts "# Warning: Another slow test!" do_test 2.5 { execsql { SELECT * FROM b } } {Tehran Qom Markazi Qazvin Gilan Ardabil} do_test 2.6 { set ::r -} {1 {locking protocol}} +} {0 {Tehran Qom Markazi Qazvin Gilan Ardabil}} db close db2 close @@ -157,18 +180,19 @@ sqlite3 db2 test.db T filter xShmLock T script lock_callback proc lock_callback {method file handle spec} { - if {$spec == "1 7 unlock exclusive"} { + if {$spec == "1 2 unlock exclusive"} { T filter {} set ::r [catchsql { SELECT * FROM b } db2] } } unset ::r +puts "# Warning: Last one!" do_test 2.7 { execsql { SELECT * FROM b } } {Tehran Qom Markazi Qazvin Gilan Ardabil} do_test 2.8 { set ::r -} {1 {locking protocol}} +} {0 {Tehran Qom Markazi Qazvin Gilan Ardabil}} db close db2 close diff --git a/test/walprotocol2.test b/test/walprotocol2.test new file mode 100644 index 0000000000..0792c9aae0 --- /dev/null +++ b/test/walprotocol2.test @@ -0,0 +1,97 @@ +# 2018 July 4 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/lock_common.tcl +source $testdir/wal_common.tcl +ifcapable !wal {finish_test ; return } + +set testprefix walprotocol2 + +#------------------------------------------------------------------------- +# When recovering the contents of a WAL file, a process obtains the WRITER +# lock, then locks all other bytes before commencing recovery. If it fails +# to lock all other bytes (because some other process is holding a read +# lock) it should retry up to 100 times. Then return SQLITE_PROTOCOL to the +# caller. Test this (test case 1.3). +# +# Also test the effect of hitting an SQLITE_BUSY while attempting to obtain +# the WRITER lock (should be the same). Test case 1.4. +# +do_execsql_test 1.0 { + PRAGMA journal_mode = wal; + CREATE TABLE x(y); + INSERT INTO x VALUES('z'); +} {wal} + +db close + +proc lock_callback {method filename handle lock} { + # puts "$method $filename $handle $lock" +} +testvfs T +T filter xShmLock +T script lock_callback + +sqlite3 db test.db -vfs T +sqlite3 db2 test.db -vfs T + +do_execsql_test 2.0 { + SELECT * FROM x; +} {z} +do_execsql_test -db db2 2.1 { + SELECT * FROM x; +} {z} + +#--------------------------------------------------------------- +# Attempt a "BEGIN EXCLUSIVE" using connection handle [db]. This +# causes SQLite to open a read transaction, then a write transaction. +# Rig the xShmLock() callback so that just before the EXCLUSIVE lock +# for the write transaction is taken, connection [db2] jumps in and +# modifies the database. This causes the "BEGIN EXCLUSIVE" to throw +# an SQLITE_BUSY_SNAPSHOT error. +# +proc lock_callback {method filename handle lock} { + if {$lock=="0 1 lock exclusive"} { + proc lock_callback {method filename handle lock} {} + db2 eval { INSERT INTO x VALUES('y') } + } +} +do_catchsql_test 2.2 { + BEGIN EXCLUSIVE; +} {1 {database is locked}} +do_test 2.3 { + sqlite3_extended_errcode db +} {SQLITE_BUSY} + +#--------------------------------------------------------------- +# Same again, but with a busy-handler. This time, following the +# SQLITE_BUSY_SNAPSHOT error the busy-handler is invoked and then the +# whole thing retried from the beginning. This time it succeeds. +# +proc lock_callback {method filename handle lock} { + if {$lock=="0 1 lock exclusive"} { + proc lock_callback {method filename handle lock} {} + db2 eval { INSERT INTO x VALUES('x') } + } +} +db timeout 10 +do_catchsql_test 2.4 { + BEGIN EXCLUSIVE; +} {0 {}} +do_execsql_test 2.5 { + SELECT * FROM x; + COMMIT; +} {z y x} + +finish_test diff --git a/test/walro.test b/test/walro.test index 6d920b1e24..a39b844d9d 100644 --- a/test/walro.test +++ b/test/walro.test @@ -19,7 +19,7 @@ set ::testprefix walro # These tests are only going to work on unix. # -if {$::tcl_platform(platform) != "unix"} { +if {$::tcl_platform(os) eq "Windows NT"} { finish_test return } @@ -101,10 +101,11 @@ do_multiclient_test tn { code1 { db close } list [file exists test.db-wal] [file exists test.db-shm] } {1 1} + do_test 1.2.2 { code1 { sqlite3 db file:test.db?readonly_shm=1 } - sql1 { SELECT * FROM t1 } - } {a b c d e f g h i j} + list [catch { sql1 { SELECT * FROM t1 } } msg] $msg + } {0 {a b c d e f g h i j}} do_test 1.2.3 { code1 { db close } @@ -113,10 +114,10 @@ do_multiclient_test tn { file attributes test.db-shm -permissions r--r--r-- code1 { sqlite3 db file:test.db?readonly_shm=1 } csql1 { SELECT * FROM t1 } - } {1 {attempt to write a readonly database}} + } {0 {a b c d e f g h i j}} do_test 1.2.4 { code1 { sqlite3_extended_errcode db } - } {SQLITE_READONLY_RECOVERY} + } {SQLITE_OK} do_test 1.2.5 { file attributes test.db-shm -permissions rw-r--r-- @@ -138,11 +139,15 @@ do_multiclient_test tn { # Now check that if the readonly_shm option is not supplied, or if it # is set to zero, it is not possible to connect to the database without # read-write access to the shm. + # + # UPDATE: os_unix.c now opens the *-shm file in readonly mode + # automatically. + # do_test 1.3.1 { code1 { db close } code1 { sqlite3 db test.db } csql1 { SELECT * FROM t1 } - } {1 {unable to open database file}} + } {0 {a b c d e f g h i j k l}} # Also test that if the -shm file can be opened for read/write access, # it is not if readonly_shm=1 is present in the URI. @@ -161,10 +166,10 @@ do_multiclient_test tn { file attributes test.db-shm -permissions r--r--r-- code1 { sqlite3 db file:test.db?readonly_shm=1 } csql1 { SELECT * FROM t1 } - } {1 {attempt to write a readonly database}} + } {0 {a b c d e f g h i j k l}} do_test 1.3.2.4 { code1 { sqlite3_extended_errcode db } - } {SQLITE_READONLY_RECOVERY} + } {SQLITE_OK} #----------------------------------------------------------------------- # Test cases 1.4.* check that checkpoints and log wraps don't prevent @@ -212,7 +217,7 @@ do_multiclient_test tn { INSERT INTO t2 SELECT x||y, y||x FROM t2; } file size test.db-wal - } {147800} + } [expr {[nonzero_reserved_bytes]?148848:147800}] do_test 1.4.4.2 { csql1 { SELECT * FROM t1 } } {0 {a b c d e f g h i j k l 1 2 3 4 5 6}} diff --git a/test/walro2.test b/test/walro2.test new file mode 100644 index 0000000000..1eb9d1c3ee --- /dev/null +++ b/test/walro2.test @@ -0,0 +1,411 @@ +# 2011 May 09 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# This file contains tests for using WAL databases in read-only mode. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/lock_common.tcl +source $testdir/wal_common.tcl +set ::testprefix walro2 + +# And only if the build is WAL-capable. +# +ifcapable !wal { + finish_test + return +} + +proc copy_to_test2 {bZeroShm} { + forcecopy test.db test.db2 + forcecopy test.db-wal test.db2-wal + if {$bZeroShm} { + forcedelete test.db2-shm + set fd [open test.db2-shm w] + seek $fd [expr [file size test.db-shm]-1] + puts -nonewline $fd "\0" + close $fd + } else { + forcecopy test.db-shm test.db2-shm + } +} + +# Most systems allocate the *-shm file in 32KB trunks. But on UNIX systems +# for which the getpagesize() call returns greater than 32K, the *-shm +# file is allocated in page-sized units (since you cannot mmap part of +# a page). The following code sets variable $MINSHMSZ to the smallest +# possible *-shm file (i.e. the greater of 32KB and the system page-size). +# +do_execsql_test 0.0 { + PRAGMA journal_mode = wal; + CREATE TABLE t1(x); +} {wal} +set MINSHMSZ [file size test.db-shm] +set dfltpgsz [db one {PRAGMA page_size}] + +foreach bZeroShm {0 1} { + for {set pgsz 512} {$pgsz<=65536} {set pgsz [expr {$pgsz*2}]} { + set TN [expr $bZeroShm+1]-$pgsz + do_multiclient_test tn { + + # Close all connections and delete the database. + # + code1 { db close } + code2 { db2 close } + code3 { db3 close } + forcedelete test.db + + # Do not run tests with the connections in the same process. + # + if {$tn==2} continue + + foreach c {code1 code2 code3} { + $c { + sqlite3_shutdown + sqlite3_config_uri 1 + } + } + + do_test $TN.1.1 { + code2 { sqlite3 db2 test.db } + sql2 "PRAGMA page_size=$::pgsz" + sql2 { + CREATE TABLE t1(x, y); + PRAGMA journal_mode = WAL; + INSERT INTO t1 VALUES('a', 'b'); + INSERT INTO t1 VALUES('c', 'd'); + } + file exists test.db-shm + } {1} + + do_test $TN.1.2.1 { + copy_to_test2 $bZeroShm + code1 { + sqlite3 db file:test.db2?readonly_shm=1 + } + + sql1 { SELECT * FROM t1 } + } {a b c d} + do_test $TN.1.2.2 { + sql1 { SELECT * FROM t1 } + } {a b c d} + + do_test $TN.1.3.1 { + code3 { sqlite3 db3 test.db2 } + sql3 { SELECT * FROM t1 } + } {a b c d} + + do_test $TN.1.3.2 { + sql1 { SELECT * FROM t1 } + } {a b c d} + + code1 { db close } + code2 { db2 close } + code3 { db3 close } + + do_test $TN.2.1 { + code2 { sqlite3 db2 test.db } + sql2 "PRAGMA page_size=$::pgsz;" + sql2 { + INSERT INTO t1 VALUES('e', 'f'); + INSERT INTO t1 VALUES('g', 'h'); + } + file exists test.db-shm + } {1} + + do_test $TN.2.2 { + copy_to_test2 $bZeroShm + code1 { + sqlite3 db file:test.db2?readonly_shm=1 + } + sql1 { + BEGIN; + SELECT * FROM t1; + } + } {a b c d e f g h} + + do_test $TN.2.3.1 { + code3 { sqlite3 db3 test.db2 } + sql3 { SELECT * FROM t1 } + } {a b c d e f g h} + do_test $TN.2.3.2 { + sql3 { INSERT INTO t1 VALUES('i', 'j') } + code3 { db3 close } + sql1 { COMMIT } + } {} + do_test $TN.2.3.3 { + sql1 { SELECT * FROM t1 } + } {a b c d e f g h i j} + + + #----------------------------------------------------------------------- + # 3.1.*: That a readonly_shm connection can read a database file if both + # the *-wal and *-shm files are zero bytes in size. + # + # 3.2.*: That it flushes the cache if, between transactions on a db with a + # zero byte *-wal file, some other connection modifies the db, then + # does "PRAGMA wal_checkpoint=truncate" to truncate the wal file + # back to zero bytes in size. + # + # 3.3.*: That, if between transactions some other process wraps the wal + # file, the readonly_shm client reruns recovery. + # + catch { code1 { db close } } + catch { code2 { db2 close } } + catch { code3 { db3 close } } + do_test $TN.3.1.0 { + list [file exists test.db-wal] [file exists test.db-shm] + } {0 0} + do_test $TN.3.1.1 { + close [open test.db-wal w] + close [open test.db-shm w] + code1 { + sqlite3 db file:test.db?readonly_shm=1 + } + sql1 { SELECT * FROM t1 } + } {a b c d e f g h} + + do_test $TN.3.2.0 { + list [file size test.db-wal] [file size test.db-shm] + } {0 0} + do_test $TN.3.2.1 { + code2 { sqlite3 db2 test.db } + sql2 { INSERT INTO t1 VALUES(1, 2) ; PRAGMA wal_checkpoint=truncate } + code2 { db2 close } + sql1 { SELECT * FROM t1 } + } {a b c d e f g h 1 2} + if {$pgsz!=$dfltpgsz} continue + do_test $TN.3.2.2 { + list [file size test.db-wal] [file size test.db-shm] + } [list 0 $MINSHMSZ] + do_test $TN.3.3.0 { + code2 { sqlite3 db2 test.db } + sql2 { + INSERT INTO t1 VALUES(3, 4); + INSERT INTO t1 VALUES(5, 6); + INSERT INTO t1 VALUES(7, 8); + INSERT INTO t1 VALUES(9, 10); + } + code2 { db2 close } + code1 { db close } + list [file size test.db-wal] [file size test.db-shm] + } [list [wal_file_size 4 1024] $MINSHMSZ] + do_test $TN.3.3.1 { + code1 { sqlite3 db file:test.db?readonly_shm=1 } + sql1 { SELECT * FROM t1 } + } {a b c d e f g h 1 2 3 4 5 6 7 8 9 10} + do_test $TN.3.3.2 { + code2 { sqlite3 db2 test.db } + sql2 { + PRAGMA wal_checkpoint; + DELETE FROM t1; + INSERT INTO t1 VALUES('i', 'ii'); + } + code2 { db2 close } + list [file size test.db-wal] [file size test.db-shm] + } [list [wal_file_size 4 1024] $MINSHMSZ] + do_test $TN.3.3.3 { + sql1 { SELECT * FROM t1 } + } {i ii} + + #----------------------------------------------------------------------- + # + # + catch { code1 { db close } } + catch { code2 { db2 close } } + catch { code3 { db3 close } } + + do_test $TN.4.0 { + code1 { forcedelete test.db } + code1 { sqlite3 db test.db } + sql1 { + PRAGMA journal_mode = wal; + CREATE TABLE t1(x); + INSERT INTO t1 VALUES('hello'); + INSERT INTO t1 VALUES('world'); + } + + copy_to_test2 $bZeroShm + + code1 { db close } + } {} + + do_test $TN.4.1.1 { + code2 { sqlite3 db2 file:test.db2?readonly_shm=1 } + sql2 { SELECT * FROM t1 } + } {hello world} + + do_test $TN.4.1.2 { + code3 { sqlite3 db3 test.db2 } + sql3 { + INSERT INTO t1 VALUES('!'); + PRAGMA wal_checkpoint = truncate; + } + code3 { db3 close } + } {} + do_test $TN.4.1.3 { + sql2 { SELECT * FROM t1 } + } {hello world !} + + catch { code1 { db close } } + catch { code2 { db2 close } } + catch { code3 { db3 close } } + + do_test $TN.4.2.1 { + code1 { sqlite3 db test.db } + sql1 { + INSERT INTO t1 VALUES('!'); + INSERT INTO t1 VALUES('!'); + + PRAGMA cache_size = 10; + CREATE TABLE t2(x); + + BEGIN; + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<500 + ) + INSERT INTO t2 SELECT randomblob(500) FROM s; + SELECT count(*) FROM t2; + } + } {500} + set sz [file size test.db-wal] + do_test $TN.4.2.2.(sz=$sz) { + expr {$sz>400000} + } {1} + do_test $TN.4.2.4 { + file_control_persist_wal db 1; db close + + copy_to_test2 $bZeroShm + code2 { sqlite3 db2 file:test.db2?readonly_shm=1 } + sql2 { + SELECT * FROM t1; + SELECT count(*) FROM t2; + } + } {hello world ! ! 0} + + #----------------------------------------------------------------------- + # + # + catch { code1 { db close } } + catch { code2 { db2 close } } + catch { code3 { db3 close } } + + do_test $TN.5.0 { + code1 { forcedelete test.db } + code1 { sqlite3 db test.db } + sql1 { + PRAGMA journal_mode = wal; + CREATE TABLE t1(x); + INSERT INTO t1 VALUES('hello'); + INSERT INTO t1 VALUES('world'); + INSERT INTO t1 VALUES('!'); + INSERT INTO t1 VALUES('world'); + INSERT INTO t1 VALUES('hello'); + } + + copy_to_test2 $bZeroShm + + code1 { db close } + } {} + + do_test $TN.5.1 { + code2 { sqlite3 db2 file:test.db2?readonly_shm=1 } + sql2 { + SELECT * FROM t1; + } + } {hello world ! world hello} + + do_test $TN.5.2 { + code1 { + proc handle_read {op args} { + if {$op=="xRead" && [file tail [lindex $args 0]]=="test.db2-wal"} { + set ::res2 [sql2 { SELECT * FROM t1 }] + } + puts "$msg xRead $args" + return "SQLITE_OK" + } + testvfs tvfs -fullshm 1 + + sqlite3 db file:test.db2?vfs=tvfs + db eval { SELECT * FROM sqlite_master } + + tvfs filter xRead + tvfs script handle_read + } + sql1 { + PRAGMA wal_checkpoint = truncate; + } + code1 { set ::res2 } + } {hello world ! world hello} + + do_test $TN.5.3 { + code1 { db close } + code1 { tvfs delete } + } {} + + #----------------------------------------------------------------------- + # + # + catch { code1 { db close } } + catch { code2 { db2 close } } + catch { code3 { db3 close } } + + do_test $TN.6.1 { + code1 { forcedelete test.db } + code1 { sqlite3 db test.db } + sql1 { + PRAGMA journal_mode = wal; + CREATE TABLE t1(x); + INSERT INTO t1 VALUES('hello'); + INSERT INTO t1 VALUES('world'); + INSERT INTO t1 VALUES('!'); + INSERT INTO t1 VALUES('world'); + INSERT INTO t1 VALUES('hello'); + } + + copy_to_test2 $bZeroShm + + code1 { db close } + } {} + + do_test $TN.6.2 { + code1 { + set ::nRem 5 + proc handle_read {op args} { + if {$op=="xRead" && [file tail [lindex $args 0]]=="test.db2-wal"} { + incr ::nRem -1 + if {$::nRem==0} { + code2 { sqlite3 db2 test.db2 } + sql2 { PRAGMA wal_checkpoint = truncate } + } + } + return "SQLITE_OK" + } + testvfs tvfs -fullshm 1 + + tvfs filter xRead + tvfs script handle_read + + sqlite3 db file:test.db2?readonly_shm=1&vfs=tvfs + db eval { SELECT * FROM t1 } + } + } {hello world ! world hello} + + do_test $TN.6.3 { + code1 { db close } + code1 { tvfs delete } + } {} + } + } ;# for pgsz +} ;# foreach bZeroShm + +finish_test diff --git a/test/walrofault.test b/test/walrofault.test new file mode 100644 index 0000000000..3e66e2d920 --- /dev/null +++ b/test/walrofault.test @@ -0,0 +1,60 @@ +# 2011 May 09 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# This file contains tests for using WAL databases in read-only mode. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/malloc_common.tcl +set ::testprefix walro2 + +# And only if the build is WAL-capable. +# +ifcapable !wal { + finish_test + return +} + +db close +sqlite3_shutdown +sqlite3_config_uri 1 +sqlite3 db test.db + +do_execsql_test 1.0 { + CREATE TABLE t1(b); + PRAGMA journal_mode = wal; + INSERT INTO t1 VALUES('hello'); + INSERT INTO t1 VALUES('world'); + INSERT INTO t1 VALUES('!'); + INSERT INTO t1 VALUES('world'); + INSERT INTO t1 VALUES('hello'); + PRAGMA cache_size = 10; + BEGIN; + WITH s(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<30 ) + INSERT INTO t1(b) SELECT randomblob(800) FROM s; +} {wal} +file_control_persist_wal db 1; db close +faultsim_save_and_close + +do_faultsim_test 1 -faults oom* -prep { + catch { db close } + faultsim_restore + sqlite3 db file:test.db?readonly_shm=1 +} -body { + execsql { SELECT * FROM t1 } +} -test { + faultsim_test_result {0 {hello world ! world hello}} +} + + + +finish_test diff --git a/test/walseh1.test b/test/walseh1.test new file mode 100644 index 0000000000..225b69f742 --- /dev/null +++ b/test/walseh1.test @@ -0,0 +1,148 @@ +# 2021 August 16 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/malloc_common.tcl +set testprefix walseh1 + +set ::seh_countdown 0 +set ::seh_errno 10 +proc seh_faultsim_callback {iFault} { + if {$iFault==650} { + incr ::seh_countdown -1 + if {$::seh_countdown==0} { return $::seh_errno } + } + return 0 +} + +proc seh_injectinstall {} { + sqlite3_test_control_fault_install seh_faultsim_callback +} +proc seh_injectuninstall {} { + sqlite3_test_control_fault_install +} +proc seh_injectstart {iFail} { + set ::seh_errno [expr 2+$iFail*10] + set ::seh_countdown $iFail +} +proc seh_injectstop {} { + set res [expr $::seh_countdown<=0] + set ::seh_countdown 0 + set res +} + +set FAULTSIM(seh) [list \ + -injectinstall seh_injectinstall \ + -injectstart seh_injectstart \ + -injectstop seh_injectstop \ + -injecterrlist {{1 {disk I/O error}}} \ + -injectuninstall seh_injectuninstall \ +] + +proc test_system_errno {db expect} { + set serrno [sqlite3_system_errno $db] + if {$serrno!=$expect} { + error "surprising system_errno. Expected $expect, got $serrno" + } +} + +do_execsql_test 1.0 { + PRAGMA journal_mode = wal; + CREATE TABLE t1(x, y); + INSERT INTO t1 VALUES(1, 2); + INSERT INTO t1 VALUES(3, 4); +} {wal} +faultsim_save_and_close + +do_faultsim_test 1 -faults seh -prep { + catch { db2 close } + faultsim_restore_and_reopen + execsql { SELECT * FROM sqlite_schema } + sqlite3 db2 test.db +} -body { + execsql { SELECT * FROM t1 } db2 +} -test { + faultsim_test_result {0 {1 2 3 4}} + if {$testrc} { test_system_errno db2 $::seh_errno } +} +catch { db2 close } + +faultsim_save_and_close + +do_faultsim_test 2 -faults seh -prep { + catch { db close } + faultsim_restore_and_reopen +} -body { + execsql { SELECT * FROM t1 } +} -test { + faultsim_test_result {0 {1 2 3 4}} + if {$testrc} { test_system_errno db $::seh_errno } +} + +do_faultsim_test 3 -faults seh -prep { + catch { db close } + faultsim_restore_and_reopen +} -body { + execsql { INSERT INTO t1 VALUES(5, 6) } + execsql { SELECT * FROM t1 } +} -test { + faultsim_test_result {0 {1 2 3 4 5 6}} + if {$testrc} { test_system_errno db $::seh_errno } +} + +do_faultsim_test 4 -faults seh -prep { + catch { db close } + faultsim_restore_and_reopen +} -body { + execsql { PRAGMA wal_checkpoint } + execsql { INSERT INTO t1 VALUES(7, 8) } + execsql { SELECT * FROM t1 } +} -test { + faultsim_test_result {0 {1 2 3 4 7 8}} + if {$testrc} { test_system_errno db $::seh_errno } +} +catch { db close } + +do_faultsim_test 5 -faults seh -prep { + catch { db close } + faultsim_restore_and_reopen + execsql { + PRAGMA cache_size = 5; + BEGIN; + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<50 + ) + INSERT INTO t1 SELECT randomblob(500), randomblob(500) FROM s; + } +} -body { + execsql ROLLBACK +} -test { + faultsim_test_result {0 {}} + if {$testrc} { test_system_errno db $::seh_errno } +} +catch { db close } + +do_faultsim_test 6 -faults seh -prep { + catch { db close } + faultsim_restore_and_reopen +} -body { + execsql { PRAGMA wal_checkpoint = TRUNCATE } + execsql { INSERT INTO t1 VALUES(7, 8) } + execsql { SELECT * FROM t1 } +} -test { + faultsim_test_result {0 {1 2 3 4 7 8}} + if {$testrc} { test_system_errno db $::seh_errno } +} +catch { db close } + +finish_test diff --git a/test/walsetlk.test b/test/walsetlk.test new file mode 100644 index 0000000000..969bcaf465 --- /dev/null +++ b/test/walsetlk.test @@ -0,0 +1,350 @@ +# 2020 May 06 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# TESTRUNNER: slow +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/lock_common.tcl +set testprefix walsetlk + +ifcapable !wal {finish_test ; return } +db timeout 1000 + +#------------------------------------------------------------------------- +# 1.*: Test that nothing goes wrong if recovery is forced while opening +# a write transaction or performing a checkpoint with blocking locks. +# + +do_execsql_test 1.0 { + CREATE TABLE t1(x, y); + PRAGMA journal_mode = wal; + INSERT INTO t1 VALUES(1, 2); + INSERT INTO t1 VALUES(3, 4); + INSERT INTO t1 VALUES(5, 6); + INSERT INTO t1 VALUES(7, 8); +} {wal} + +sqlite3 db2 test.db +db2 timeout 1000 + +do_execsql_test -db db2 1.1 { + SELECT * FROM t1 +} {1 2 3 4 5 6 7 8} + +set fd [open test.db-shm r+] +puts $fd "blahblahblahblah" +flush $fd + +do_execsql_test 1.2 { + BEGIN; + INSERT INTO t1 VALUES(9, 10); +} + +do_execsql_test -db db2 1.3 { + SELECT * FROM t1 +} {1 2 3 4 5 6 7 8} + +do_test 1.4 { + list [catch {db2 eval { BEGIN EXCLUSIVE }} msg] $msg +} {1 {database is locked}} + +do_execsql_test 1.5 { COMMIT } +do_execsql_test -db db2 1.6 { + SELECT * FROM t1 +} {1 2 3 4 5 6 7 8 9 10} + +puts $fd "blahblahblahblah" +flush $fd + +do_execsql_test -db db2 1.7 { + PRAGMA wal_checkpoint = TRUNCATE +} {0 0 0} + +do_test 1.8 { + file size test.db-wal +} 0 + +close $fd +db close +db2 close +#------------------------------------------------------------------------- + +do_multiclient_test tn { + + testvfs tvfs -fullshm 1 + db close + sqlite3 db test.db -vfs tvfs + tvfs script xSleep_callback + tvfs filter xSleep + + set ::sleep_count 0 + proc xSleep_callback {xSleep nMs} { + after [expr $nMs / 1000] + incr ::sleep_count + } + + do_test 2.$tn.1 { + sql1 { + PRAGMA journal_mode = wal; + CREATE TABLE t1(s, v); + INSERT INTO t1 VALUES(1, 2); + INSERT INTO t1 VALUES(3, 4); + INSERT INTO t1 VALUES(5, 6); + } + code1 { db timeout 1100 } + } {} + + do_test 2.$tn.2 { + sql2 { + BEGIN; + INSERT INTO t1 VALUES(7, 8); + } + } {} + + do_test 2.$tn.3 { + set us [lindex [time { catch {db eval "BEGIN EXCLUSIVE"} }] 0] + expr $us>1000000 && $us<4000000 + } {1} + + do_test 2.$tn.4 { + sql2 { COMMIT } + sql1 { SELECT * FROM t1 } + } {1 2 3 4 5 6 7 8} + + do_test 2.$tn.5 { + sql2 { + BEGIN; + INSERT INTO t1 VALUES(9, 10); + } + } {} + + do_test 2.$tn.6 { + set us [lindex [time { catch {db eval "PRAGMA wal_checkpoint=RESTART"} }] 0] + expr $us>1000000 && $us<4000000 + } {1} + + do_test 2.$tn.7 { + sql2 { + COMMIT; + BEGIN; + SELECT * FROM t1; + } + } {1 2 3 4 5 6 7 8 9 10} + + do_test 2.$tn.8 { + set us [lindex [time { catch {db eval "PRAGMA wal_checkpoint=RESTART"} }] 0] + expr $us>1000000 && $us<4000000 + } {1} + do_test 2.$tn.9 { + sql3 { + INSERT INTO t1 VALUES(11, 12); + } + sql2 { + COMMIT; + BEGIN; + SELECT * FROM t1; + } + sql3 { + INSERT INTO t1 VALUES(13, 14); + } + } {} + + do_test 2.$tn.10 { + set us [lindex [time { catch {db eval "PRAGMA wal_checkpoint=RESTART"} }] 0] + expr $us>1000000 && $us<4000000 + } {1} + + do_test 2.$tn.11 { + sql3 { + BEGIN; + SELECT * FROM t1; + } + sql1 { INSERT INTO t1 VALUES(15, 16); } + } {} + + do_test 2.$tn.12 { + set us [lindex [time { catch {db eval "PRAGMA wal_checkpoint=RESTART"} }] 0] + expr $us>1000000 && $us<4000000 + } {1} + + do_test 2.$tn.13 { + sql2 { + COMMIT; + BEGIN; + SELECT * FROM t1; + } + sql1 { INSERT INTO t1 VALUES(17, 18); } + } {} + + do_test 2.$tn.14 { + set us [lindex [time { catch {db eval "PRAGMA wal_checkpoint=RESTART"} }] 0] + expr $us>1000000 && $us<4000000 + } {1} + + db close + tvfs delete + + # Set bSleep to true if it is expected that the above used xSleep() to + # wait for locks. bSleep is true unless SQLITE_ENABLE_SETLK_TIMEOUT is + # set to 1 and either: + # + # * the OS is windows, or + # * the OS is unix and the tests were run with each connection + # in a separate process. + # + set bSleep 1 + if {$::sqlite_options(setlk_timeout)==1} { + if {$::tcl_platform(platform) eq "windows"} { + set bSleep 0 + } + if {$::tcl_platform(platform) eq "unix"} { + set bSleep [expr $tn==2] + } + } + + do_test 2.$tn.15.$bSleep { + expr $::sleep_count > 0 + } $bSleep +} + +#------------------------------------------------------------------------- +reset_db + +testvfs tvfs -fullshm 1 +tvfs script xSleep_callback +tvfs filter xSleep + +set ::sleep_count 0 +proc xSleep_callback {xSleep nMs} { + after [expr $nMs / 1000] + incr ::sleep_count + breakpoint +} + +sqlite3 db2 test.db -vfs tvfs +db2 timeout 1000 + +do_execsql_test 3.0 { + PRAGMA journal_mode = wal; + CREATE TABLE x1(x, y); + BEGIN; + INSERT INTO x1 VALUES(1, 2); +} {wal} + +do_execsql_test -db db2 3.1a { + SELECT * FROM x1 +} {} + +do_test 3.1b { + list [catch { db2 eval {BEGIN EXCLUSIVE} } msg] $msg +} {1 {database is locked}} + +# Set bExpect to true if calls to xSleep() are expected. Such calls are +# expected unless this is an SQLITE_ENABLE_SETLK_TIMEOUT=1 build. +set bExpect 1 +if {$tcl_platform(platform) eq "windows" && $::sqlite_options(setlk_timeout)==1} { + set bExpect 0 +} +do_test 3.2 { + expr {$::sleep_count > 0} +} $bExpect +set ::sleep_count 0 + +do_execsql_test 3.3 { + COMMIT; +} + +# Launch a non-blocking testfixture process to write-lock the +# database for 2000 ms. +testfixture_nb done { + sqlite3 db test.db + db eval { + BEGIN EXCLUSIVE; + INSERT INTO x1 VALUES(3, 4); + } + after 2000 + db eval { + COMMIT + } +} + +after 500 {set ok 1} +vwait ok + +db2 timeout 5000 +do_test 3.4 { + set t [lindex [time { db2 eval { BEGIN EXCLUSIVE } }] 0] + expr ($t>1000000) +} {1} + +# Set bExpect to true if calls to xSleep() are expected. Such calls are +# expected unless this is an SQLITE_ENABLE_SETLK_TIMEOUT=1 build. +set bExpect 1 +if {$::sqlite_options(setlk_timeout)==1} { + set bExpect 0 +} +do_test 3.5 { + expr {$::sleep_count > 0} +} $bExpect + +do_execsql_test -db db2 3.6 { + INSERT INTO x1 VALUES(5, 6); + COMMIT; + SELECT * FROM x1; +} {1 2 3 4 5 6} + +# Launch a non-blocking testfixture process to write-lock the +# database for 2000 ms. +testfixture_nb done { + sqlite3 db test.db + db eval { + BEGIN EXCLUSIVE; + INSERT INTO x1 VALUES(7, 8); + } + after 2000 + db eval { + COMMIT + } + db close +} + +after 500 {set ok 1} +vwait ok + +db2 timeout 0x7FFFFFFF +do_test 3.7 { + set t [lindex [time { db2 eval { BEGIN EXCLUSIVE } }] 0] + expr ($t>1000000) +} {1} + +# Set bExpect to true if calls to xSleep() are expected. Such calls are +# expected unless this is an SQLITE_ENABLE_SETLK_TIMEOUT=1 build. +set bExpect 1 +if {$::sqlite_options(setlk_timeout)==1} { + set bExpect 0 +} +do_test 3.8 { + expr {$::sleep_count > 0} +} $bExpect + +do_execsql_test -db db2 3.9 { + INSERT INTO x1 VALUES(9, 10); + COMMIT; + SELECT * FROM x1; +} {1 2 3 4 5 6 7 8 9 10} + +db2 close +tvfs delete + +finish_test + diff --git a/test/walsetlk2.test b/test/walsetlk2.test new file mode 100644 index 0000000000..7ffd8f03de --- /dev/null +++ b/test/walsetlk2.test @@ -0,0 +1,269 @@ +# 2025 Jan 24 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# TESTRUNNER: slow +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/lock_common.tcl +set testprefix walsetlk2 + +ifcapable !wal {finish_test ; return } +ifcapable !setlk_timeout {finish_test ; return } + +#------------------------------------------------------------------------- +# Check that xShmLock calls are as expected for write transactions in +# setlk mode. +# +reset_db + +do_execsql_test 1.0 { + PRAGMA journal_mode = wal; + CREATE TABLE t1(a, b, c); + INSERT INTO t1 VALUES(1, 2, 3); +} {wal} +db close + +testvfs tvfs +tvfs script xShmLock_callback +tvfs filter xShmLock + +set ::xshmlock [list] +proc xShmLock_callback {method path name detail} { + lappend ::xshmlock $detail +} + +sqlite3 db test.db -vfs tvfs +db timeout 1000 + +do_execsql_test 1.1 { + SELECT * FROM t1 +} {1 2 3} + +do_execsql_test 1.2 { + INSERT INTO t1 VALUES(4, 5, 6); +} + +set ::xshmlock [list] +do_execsql_test 1.3 { + INSERT INTO t1 VALUES(7, 8, 9); +} + +do_test 1.4 { + set ::xshmlock +} [list \ + {0 1 lock exclusive} \ + {4 1 lock exclusive} {4 1 unlock exclusive} \ + {4 1 lock shared} \ + {0 1 unlock exclusive} \ + {4 1 unlock shared} +] + +do_execsql_test 1.5.1 { SELECT * FROM t1 } {1 2 3 4 5 6 7 8 9} +set ::xshmlock [list] +do_execsql_test 1.5.2 { + INSERT INTO t1 VALUES(10, 11, 12); +} +do_test 1.5.3 { + set ::xshmlock +} [list \ + {0 1 lock exclusive} \ + {4 1 lock shared} \ + {0 1 unlock exclusive} \ + {4 1 unlock shared} +] + +db close +tvfs delete + +#------------------------------------------------------------------------- +# Check that if sqlite3_setlk_timeout() is used, blocking locks timeout +# but other operations do not use the retry mechanism. +# +reset_db +db close +sqlite3 db test.db -fullmutex 1 + +do_execsql_test 2.0 { + CREATE TABLE t1(a, b); + INSERT INTO t1 VALUES(1, 2), (3, 4); +} + +sqlite3_setlk_timeout db 2000 + +# Launch a non-blocking testfixture process to write-lock the +# database for 2000 ms. +testfixture_nb done { + sqlite3 db test.db + db eval { + BEGIN EXCLUSIVE; + INSERT INTO t1 VALUES(5, 6); + } + after 2000 + db eval { + COMMIT + } + db close +} + +after 500 {set ok 1} +vwait ok + +do_catchsql_test 2.1 { + INSERT INTO t1 VALUES(7, 8); +} {1 {database is locked}} + +sqlite3_busy_timeout db 2000 + +do_catchsql_test 2.2 { + INSERT INTO t1 VALUES(7, 8); +} {0 {}} + +do_execsql_test 2.3 { + SELECT * FROM t1 +} {1 2 3 4 5 6 7 8} + +do_execsql_test 2.4 { + PRAGMA journal_mode = wal; +} {wal} + +db close +sqlite3 db test.db + +do_execsql_test 2.5 { + INSERT INTO t1 VALUES(9, 10); +} + +if {$::sqlite_options(setlk_timeout)==1} { + +sqlite3_setlk_timeout db 2000 + +# Launch a non-blocking testfixture process to write-lock the +# database for 2000 ms. +testfixture_nb done { + sqlite3 db test.db + db eval { + BEGIN EXCLUSIVE; + INSERT INTO t1 VALUES(11, 12); + } + after 2000 + db eval { + COMMIT + } + db close +} + +after 500 {set ok 1} +vwait ok + +do_catchsql_test 2.6 { + INSERT INTO t1 VALUES(13, 14); +} {0 {}} + +do_execsql_test 2.7 { + SELECT * FROM t1 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14} + +} + + +#------------------------------------------------------------------------- +# Check that if sqlite3_setlk_timeout(-1) is called, blocking locks are +# enabled and last for a few seconds at least. Difficult to test that they +# really do block indefinitely. +# +reset_db + +if {$::sqlite_options(setlk_timeout)==1} { +do_execsql_test 3.0 { + PRAGMA journal_mode = wal; + CREATE TABLE t1(a INTEGER PRIMARY KEY, b); + INSERT INTO t1 VALUES(1, 'one'), (3, 'three'); +} {wal} + +sqlite3_setlk_timeout db -1 + +# Launch a non-blocking testfixture process to write-lock the +# database for 2000 ms. +testfixture_nb done { + sqlite3 db test.db + db eval { + BEGIN EXCLUSIVE; + INSERT INTO t1 VALUES(5, 'five'); + } + after 2000 + db eval { + COMMIT + } + db close +} + +after 500 {set ok 1} +vwait ok + +breakpoint +do_catchsql_test 3.1 { + INSERT INTO t1 VALUES(7, 'seven'); +} {0 {}} + +# Launch another non-blocking testfixture process to write-lock the +# database for 2000 ms. +testfixture_nb done { + sqlite3 db test.db + db eval { + BEGIN EXCLUSIVE; + INSERT INTO t1 VALUES(9, 'nine'); + } + after 2000 + db eval { + COMMIT + } + db close +} + +after 500 {set ok 1} +vwait ok + +do_catchsql_test 3.2 { + INSERT INTO t1 VALUES(9, 'ten'); +} {1 {UNIQUE constraint failed: t1.a}} + +do_execsql_test 3.3 { + SELECT * FROM t1 +} {1 one 3 three 5 five 7 seven 9 nine} + +db close + +# Launch another non-blocking testfixture process to write-lock the +# database for 2000 ms. +testfixture_nb done { + sqlite3 db test.db + db eval { + BEGIN EXCLUSIVE; + INSERT INTO t1 VALUES(11, 'eleven'); + } + after 2000 + db eval { + COMMIT + } +} + +sqlite3 db test.db +sqlite3_setlk_timeout db -1 +do_catchsql_test 3.4 { + INSERT INTO t1 VALUES(13, 'thirteen'); +} {0 {}} + +} + +finish_test + diff --git a/test/walsetlk3.test b/test/walsetlk3.test new file mode 100644 index 0000000000..b091b183d8 --- /dev/null +++ b/test/walsetlk3.test @@ -0,0 +1,135 @@ +# 2020 May 06 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# TESTRUNNER: slow +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/lock_common.tcl +set testprefix walsetlk3 + +ifcapable !wal {finish_test ; return } +ifcapable !setlk_timeout {finish_test ; return } + +do_execsql_test 1.0 { + CREATE TABLE t1(x, y); + PRAGMA journal_mode = wal; + INSERT INTO t1 VALUES(1, 2); + INSERT INTO t1 VALUES(3, 4); +} {wal} + +db close + +proc sql_block_on_close {sql} { + testfixture_nb done [string map [list %SQL% $sql] { + testvfs tvfs + tvfs script xWrite + tvfs filter xWrite + + set ::delay_done 0 + proc xWrite {method fname args} { + if {[file tail $fname]=="test.db" && $::delay_done==0} { + after 3000 + set ::delay_done 1 + } + return 0 + } + + sqlite3 db test.db -vfs tvfs + db eval {%SQL%} + db close + }] +} + +# Start a second process that writes to the db, then blocks within the +# [db close] holding an EXCLUSIVE on the db in order to checkpoint and +# delete the wal file. Then try to read the db. +# +# Without the SQLITE_SETLK_BLOCK_ON_CONNECT flag, this should fail with +# SQLITE_BUSY. +# +sql_block_on_close { + INSERT INTO t1 VALUES(5, 6); + INSERT INTO t1 VALUES(7, 8); +} +after 500 {set ok 1} +vwait ok +sqlite3 db test.db +sqlite3_setlk_timeout db 2000 +do_catchsql_test 1.1 { + SELECT * FROM t1 +} {1 {database is locked}} + +vwait ::done + +# But with SQLITE_SETLK_BLOCK_ON_CONNECT flag, it should succeed. +# +sql_block_on_close { + INSERT INTO t1 VALUES(9, 10); + INSERT INTO t1 VALUES(11, 12); +} +after 500 {set ok 1} +vwait ok +sqlite3 db test.db +sqlite3_setlk_timeout -block db 2000 +do_catchsql_test 1.2 { + SELECT * FROM t1 +} {0 {1 2 3 4 5 6 7 8 9 10 11 12}} + +vwait ::done + +#------------------------------------------------------------------------- +# Check that the SQLITE_SETLK_BLOCK_ON_CONNECT does not cause connections +# to block when taking a SHARED lock on a rollback mode database. +# +reset_db +do_execsql_test 2.1 { + CREATE TABLE x1(a); + INSERT INTO x1 VALUES(1), (2), (3); +} + +proc sql_block_on_write {sql} { + testfixture_nb done [string map [list %SQL% $sql] { + sqlite3 db test.db + db eval "BEGIN EXCLUSIVE" + db eval {%SQL%} + after 3000 + db eval COMMIT + db close + }] +} + +db close +sql_block_on_write { + INSERT INTO x1 VALUES(4); +} + +after 500 {set ok 1} +vwait ok + +sqlite3 db test.db +sqlite3_setlk_timeout -block db 2000 + +do_catchsql_test 2.2 { + SELECT * FROM x1 +} {1 {database is locked}} + +vwait ::done +after 500 {set ok 1} +vwait ok + +do_catchsql_test 2.3 { + SELECT * FROM x1 +} {0 {1 2 3 4}} + +finish_test + diff --git a/test/walsetlk_recover.test b/test/walsetlk_recover.test new file mode 100644 index 0000000000..1daece7470 --- /dev/null +++ b/test/walsetlk_recover.test @@ -0,0 +1,104 @@ +# 2025 May 30 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# TESTRUNNER: slow +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/lock_common.tcl +set testprefix walsetlk_recover + +ifcapable !wal {finish_test ; return } +# ifcapable !setlk_timeout {finish_test ; return } + +do_execsql_test 1.0 { + PRAGMA journal_mode = wal; + CREATE TABLE t1(a, b); + INSERT INTO t1 VALUES(1, 2); + INSERT INTO t1 VALUES(3, 4); + INSERT INTO t1 VALUES(5, 6); +} {wal} + +db_save_and_close +db_restore + +testfixture_nb myvar { + + testvfs tvfs -fullshm 1 + sqlite3 db test.db -vfs tvfs + tvfs script vfs_callback + tvfs filter xRead + + set ::done 0 + proc vfs_callback {method file args} { + if {$::done==0 && [string match *wal $file]} { + after 4000 + set ::done 1 + } + return "SQLITE_OK" + } + + db eval { + SELECT * FROM t1 + } + + db close +} + +# Give the [testfixture_nb] command time to start +after 1000 {set xyz 1} +vwait xyz + +testvfs tvfs -fullshm 1 +sqlite3 db test.db -vfs tvfs + +tvfs script sleep_callback +tvfs filter xSleep +set ::sleep_count 0 +proc sleep_callback {args} { + incr ::sleep_count +} + +sqlite3 db test.db -vfs tvfs +db timeout 500 +set tm [lindex [time { + catch { + db eval {SELECT * FROM t1} + } msg +}] 0] + +do_test 1.2 { set ::msg } {database is locked} +do_test 1.3.($::tm) { expr $::tm>400000 && $::tm<2000000 } 1 + +vwait myvar + +do_execsql_test 1.4 { + SELECT * FROM t1 +} {1 2 3 4 5 6} + +db close +tvfs delete + +# All SQLite builds should pass the tests above. SQLITE_ENABLE_SETLK_TIMEOUT=1 +# builds do so without calling the VFS xSleep method. +if {$::sqlite_options(setlk_timeout)==1} { + do_test 1.5.1 { + set ::sleep_count + } 0 +} else { + do_test 1.5.2 { + expr $::sleep_count>0 + } 1 +} + +finish_test + diff --git a/test/walsetlk_snapshot.test b/test/walsetlk_snapshot.test new file mode 100644 index 0000000000..e05ad69cce --- /dev/null +++ b/test/walsetlk_snapshot.test @@ -0,0 +1,109 @@ +# 2025 May 30 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# TESTRUNNER: slow +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/lock_common.tcl +set testprefix walsetlk_snapshot + +ifcapable !wal {finish_test ; return } +ifcapable !snapshot {finish_test; return} + +db close +testvfs tvfs -fullshm 1 +sqlite3 db test.db -vfs tvfs +tvfs script sleep_callback +tvfs filter xSleep +set ::sleep_count 0 +proc sleep_callback {args} { + incr ::sleep_count +} + +do_execsql_test 1.0 { + PRAGMA journal_mode = wal; + CREATE TABLE t1(a, b); + INSERT INTO t1 VALUES(1, 2); + INSERT INTO t1 VALUES(3, 4); + INSERT INTO t1 VALUES(5, 6); +} {wal} + +do_test 1.1 { + db eval BEGIN + set ::snap [sqlite3_snapshot_get db main] + db eval { + INSERT INTO t1 VALUES(7, 8); + COMMIT; + } +} {} + +testfixture_nb myvar { + + testvfs tvfs -fullshm 1 + sqlite3 db test.db -vfs tvfs + tvfs script vfs_callback + tvfs filter {xWrite} + + set ::done 0 + proc vfs_callback {args} { + if {$::done==0} { + after 4000 + set ::done 1 + } + return "SQLITE_OK" + } + + db eval { + PRAGMA wal_checkpoint; + } + + db close +} + +# Give the [testfixture_nb] command time to start +after 1000 {set xyz 1} +vwait xyz + +db timeout 500 +set tm [lindex [time { + catch { + db eval BEGIN + sqlite3_snapshot_open db main $::snap + } msg +}] 0] + +do_test 1.2 { set ::msg } {SQLITE_BUSY} +do_test 1.3.($::tm) { expr $::tm<2000000 } 1 + +do_execsql_test 1.4 { + SELECT * FROM t1 +} {1 2 3 4 5 6 7 8} + +sqlite3_snapshot_free $::snap + +vwait myvar + +# All SQLite builds should pass the tests above. SQLITE_ENABLE_SETLK_TIMEOUT=1 +# builds do so without calling the VFS xSleep method. +if {$::sqlite_options(setlk_timeout)==1} { + do_test 1.5.1 { + set ::sleep_count + } 0 +} else { + do_test 1.5.2 { + expr $::sleep_count>0 + } 1 +} + +finish_test + diff --git a/test/walshared.test b/test/walshared.test index fbbdeb4de3..ccac9484c6 100644 --- a/test/walshared.test +++ b/test/walshared.test @@ -16,7 +16,7 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl -ifcapable !wal {finish_test ; return } +ifcapable !wal||!shared_cache {finish_test ; return } db close set ::enable_shared_cache [sqlite3_enable_shared_cache 1] diff --git a/test/walslow.test b/test/walslow.test index 2a52a225d8..6a0f147a06 100644 --- a/test/walslow.test +++ b/test/walslow.test @@ -109,7 +109,6 @@ foreach incr {1 2 3 20 40 60 80 100 120 140 160 180 200 220 240 253 254 255} { forcecopy test.db-wal test2.db-wal set fd [open test2.db-wal r+] - fconfigure $fd -encoding binary fconfigure $fd -translation binary seek $fd $iOff diff --git a/test/walthread.test b/test/walthread.test index 6249ce11af..1a0c35af08 100644 --- a/test/walthread.test +++ b/test/walthread.test @@ -19,6 +19,7 @@ source $testdir/tester.tcl source $testdir/lock_common.tcl if {[run_thread_tests]==0} { finish_test ; return } ifcapable !wal { finish_test ; return } +set DBNAME wt-[expr {int(abs(rand()*100000000))}]-test.db set sqlite_walsummary_mmap_incr 64 @@ -74,7 +75,6 @@ proc lshift {lvar} { # -check SCRIPT Script to run after test. # proc do_thread_test {args} { - set A $args set P(testname) [lshift A] @@ -126,12 +126,12 @@ proc do_thread_test {args} { return } - puts "Running $P(testname) for $P(seconds) seconds..." + puts "Running $P(testname) for $P(seconds) seconds on $::DBNAME" catch { db close } - forcedelete test.db test.db-journal test.db-wal + forcedelete $::DBNAME $::DBNAME-journal $::DBNAME-wal - sqlite3 db test.db + sqlite3 db $::DBNAME eval $P(init) catch { db close } @@ -146,7 +146,7 @@ proc do_thread_test {args} { set E(nthread) $count set E(seconds) $P(seconds) " - set program [string map [list %TEST% $prg %VARS% $vars] { + set program [string map [list %TEST% $prg %VARS% $vars %DB% $::DBNAME] { %VARS% @@ -163,7 +163,7 @@ proc do_thread_test {args} { proc busyhandler {n} { usleep 10 ; return 0 } - sqlite3 db test.db + sqlite3 db %DB% db busy busyhandler db eval { SELECT randomblob($E(pid)*5) } @@ -202,7 +202,7 @@ proc do_thread_test {args} { } puts $report - sqlite3 db test.db + sqlite3 db $::DBNAME set res "" if {[catch $P(check) msg]} { set res $msg } do_test $P(testname).check [list set {} $res] "" @@ -327,59 +327,61 @@ do_thread_test2 walthread-1 -seconds $seconds(walthread-1) -init { # the number of write-transactions performed using a rollback journal. # For example, "192 w, 185 r". # -do_thread_test2 walthread-2 -seconds $seconds(walthread-2) -init { - execsql { CREATE TABLE t1(x INTEGER PRIMARY KEY, y UNIQUE) } -} -thread RB 2 { - - db close - set nRun 0 - set nDel 0 - while {[tt_continue]} { - sqlite3 db test.db - db busy busyhandler - db eval { SELECT * FROM sqlite_master } - catch { db eval { PRAGMA journal_mode = DELETE } } - db eval { - BEGIN; - INSERT INTO t1 VALUES(NULL, randomblob(100+$E(pid))); - } - incr nRun 1 - incr nDel [file exists test.db-journal] - if {[file exists test.db-journal] + [file exists test.db-wal] != 1} { - error "File-system looks bad..." - } - db eval COMMIT +if {[atomic_batch_write $::DBNAME]==0} { + do_thread_test2 walthread-2 -seconds $seconds(walthread-2) -init { + execsql { CREATE TABLE t1(x INTEGER PRIMARY KEY, y UNIQUE) } + } -thread RB 2 [string map [list %DB% $::DBNAME] { - integrity_check db close - } - list $nRun $nDel - set {} "[expr $nRun-$nDel] w, $nDel r" - -} -thread WAL 2 { - db close - set nRun 0 - set nDel 0 - while {[tt_continue]} { - sqlite3 db test.db - db busy busyhandler - db eval { SELECT * FROM sqlite_master } - catch { db eval { PRAGMA journal_mode = WAL } } - db eval { - BEGIN; - INSERT INTO t1 VALUES(NULL, randomblob(110+$E(pid))); - } - incr nRun 1 - incr nDel [file exists test.db-journal] - if {[file exists test.db-journal] + [file exists test.db-wal] != 1} { - error "File-system looks bad..." + set nRun 0 + set nDel 0 + while {[tt_continue]} { + sqlite3 db %DB% + db busy busyhandler + db eval { SELECT * FROM sqlite_master } + catch { db eval { PRAGMA journal_mode = DELETE } } + db eval { + BEGIN; + INSERT INTO t1 VALUES(NULL, randomblob(100+$E(pid))); + } + incr nRun 1 + incr nDel [file exists %DB%-journal] + if {[file exists %DB%-journal] + [file exists %DB%-wal] != 1} { + error "File-system looks bad..." + } + db eval COMMIT + + integrity_check + db close } - db eval COMMIT - - integrity_check + list $nRun $nDel + set {} "[expr $nRun-$nDel] w, $nDel r" + + }] -thread WAL 2 [string map [list %DB% $::DBNAME] { db close - } - set {} "[expr $nRun-$nDel] w, $nDel r" + set nRun 0 + set nDel 0 + while {[tt_continue]} { + sqlite3 db %DB% + db busy busyhandler + db eval { SELECT * FROM sqlite_master } + catch { db eval { PRAGMA journal_mode = WAL } } + db eval { + BEGIN; + INSERT INTO t1 VALUES(NULL, randomblob(110+$E(pid))); + } + incr nRun 1 + incr nDel [file exists %DB%-journal] + if {[file exists %DB%-journal] + [file exists %DB%-wal] != 1} { + error "File-system looks bad..." + } + db eval COMMIT + + integrity_check + db close + } + set {} "[expr $nRun-$nDel] w, $nDel r" + }] } do_thread_test walthread-3 -seconds $seconds(walthread-3) -init { @@ -508,14 +510,14 @@ do_thread_test walthread-5 -seconds $seconds(walthread-5) -init { COMMIT; } - forcecopy test.db-wal bak.db-wal - forcecopy test.db bak.db + forcecopy $::DBNAME-wal $::DBNAME-bak.db-wal + forcecopy $::DBNAME $::DBNAME-bak.db db close - forcecopy bak.db-wal test.db-wal - forcecopy bak.db test.db + forcecopy $::DBNAME-bak.db-wal $::DBNAME-wal + forcecopy $::DBNAME-bak.db $::DBNAME - if {[file size test.db-wal] < [log_file_size [expr 64*1024] 1024]} { + if {[file size $::DBNAME-wal] < [log_file_size [expr 64*1024] 1024]} { error "Somehow failed to create a large log file" } puts "Database with large log file recovered. Now running clients..." @@ -523,5 +525,8 @@ do_thread_test walthread-5 -seconds $seconds(walthread-5) -init { db eval { SELECT count(*) FROM t1 } } unset -nocomplain seconds +catch {db close} +forcedelete $::DBNAME $::DBNAME-journal $::DBNAME-wal $::DBNAME-shm +forcedelete $::DBNAME-bak.db-wal $::DBNAME-bak.db finish_test diff --git a/test/walvfs.test b/test/walvfs.test new file mode 100644 index 0000000000..d32cbd73f1 --- /dev/null +++ b/test/walvfs.test @@ -0,0 +1,437 @@ +# 2018 December 23 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is testing the operation of the library in +# "PRAGMA journal_mode=WAL" mode. +# +# TESTRUNNER: slow + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/lock_common.tcl +source $testdir/malloc_common.tcl +source $testdir/wal_common.tcl +set testprefix walvfs + +ifcapable !wal {finish_test ; return } + +db close +testvfs tvfs +tvfs script xSync +tvfs filter xSync +set ::sync_count 0 +proc xSync {method file args} { + if {[file tail $file]=="test.db-wal"} { + incr ::sync_count + } +} + + +#------------------------------------------------------------------------- +# Test that if IOCAP_SEQUENTIAL is set, the wal-header is not synced to +# disk immediately after it is written. +# +sqlite3 db test.db -vfs tvfs +do_execsql_test 1.0 { + PRAGMA auto_vacuum = 0; + PRAGMA journal_mode = wal; + PRAGMA synchronous = normal; + CREATE TABLE t1(a, b, c); + INSERT INTO t1 VALUES(1, 2, 3); + INSERT INTO t1 VALUES(4, 5, 6); + INSERT INTO t1 VALUES(7, 8, 9); + PRAGMA wal_checkpoint; +} {wal 0 5 5} + +set ::sync_count 0 +do_test 1.1 { + execsql { INSERT INTO t1 VALUES(10, 11, 12) } + set ::sync_count +} 1 + +db close +tvfs devchar sequential +sqlite3 db test.db -vfs tvfs +do_execsql_test 1.2 { + PRAGMA synchronous = normal; + INSERT INTO t1 VALUES(13, 14, 15); + INSERT INTO t1 VALUES(16, 17, 18); + PRAGMA wal_checkpoint; +} {0 4 4} + +set ::sync_count 0 +do_test 1.3 { + execsql { INSERT INTO t1 VALUES(10, 11, 12) } + set ::sync_count +} 0 + +#------------------------------------------------------------------------- +# Test that "PRAGMA journal_size_limit" works in wal mode. +# +reset_db +do_execsql_test 2.0 { + PRAGMA journal_size_limit = 10000; + CREATE TABLE t1(x); + PRAGMA journal_mode = wal; + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s LIMIT 20 + ) + INSERT INTO t1 SELECT randomblob(750) FROM s; +} {10000 wal} +do_test 2.1 { + expr [file size test.db-wal]>12000 +} {1} +do_test 2.2 { + execsql { + PRAGMA wal_checkpoint; + INSERT INTO t1 VALUES(randomblob(750)); + } + file size test.db-wal +} {10000} +do_test 2.3 { + execsql { + PRAGMA journal_size_limit = 8000; + PRAGMA wal_checkpoint; + INSERT INTO t1 VALUES(randomblob(750)); + } + file size test.db-wal +} {8000} + +#------------------------------------------------------------------------- +# Test that a checkpoint may be interrupted using sqlite3_interrupt(). +# And that the error code is SQLITE_NOMEM, not SQLITE_INTERRUPT, if +# an OOM error occurs just before the sqlite3_interrupt() call. +# +reset_db +db close +sqlite3 db test.db -vfs tvfs +tvfs filter {} + +do_execsql_test 3.0 { + CREATE TABLE t1(x); + PRAGMA journal_mode = wal; + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s LIMIT 20 + ) + INSERT INTO t1 SELECT randomblob(750) FROM s; +} {wal} + +tvfs filter xWrite +tvfs script xWrite +set ::cnt 2 +proc xWrite {method file args} { + if {[file tail $file]=="test.db"} { + incr ::cnt -1 + if {$::cnt==0} { + sqlite3_interrupt db + } + } + return SQLITE_OK +} + +do_catchsql_test 3.1 { + PRAGMA wal_checkpoint +} {1 interrupted} + +set ::cnt 2 +proc xWrite {method file args} { + if {[file tail $file]=="test.db"} { + incr ::cnt -1 + if {$::cnt==0} { + sqlite3_memdebug_fail 1 -repeat 0 + # For this test to pass, the following statement must call malloc() at + # least once. Even if the lookaside is enabled. + set ::xwrite_stmt_res [catchsql { SELECT hex(randomblob(4000)) }] + sqlite3_interrupt db + } + } + return SQLITE_OK +} + +set ::xwrite_stmt_res "" +do_catchsql_test 3.2 { + PRAGMA wal_checkpoint +} {1 {out of memory}} +do_test 3.2.2 { + set ::xwrite_stmt_res +} {1 {out of memory}} +unset ::xwrite_stmt_res + +#------------------------------------------------------------------------- +# +reset_db +db close +do_test 4.0 { + sqlite3 db test.db -vfs tvfs + execsql { + CREATE TABLE t1(x); + PRAGMA journal_mode = wal; + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s LIMIT 20 + ) + INSERT INTO t1 SELECT randomblob(750) FROM s; + } db +} {wal} +db close + +tvfs filter xShmMap +tvfs script xShmMap +proc xShmMap {method file args} { + return SQLITE_READONLY +} +sqlite3 db test.db -vfs tvfs +do_catchsql_test 4.1 { + SELECT count(*) FROM t1 +} {1 {attempt to write a readonly database}} + +set ::cnt 5 +tvfs filter {xShmMap xShmLock} +proc xShmMap {method file name args} { + switch -- $method { + xShmMap { return SQLITE_READONLY } + xShmLock { + if {$args == "{0 1 lock shared}"} { + incr ::cnt -1 + if {$::cnt>0} { return SQLITE_BUSY } + } + } + } + return SQLITE_OK +} +do_catchsql_test 4.2 { + SELECT count(*) FROM t1 +} {1 {attempt to write a readonly database}} + +#------------------------------------------------------------------------- +# +reset_db +db close +sqlite3 db test.db -vfs tvfs +tvfs filter {} +do_execsql_test 5.0 { + PRAGMA auto_vacuum = 0; + PRAGMA page_size = 1024; + CREATE TABLE t1(x); + PRAGMA journal_mode = wal; + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s LIMIT 20 + ) + INSERT INTO t1 SELECT randomblob(750) FROM s; +} {wal} + +do_execsql_test 5.1 { + SELECT count(*) FROM t1 +} {20} + +do_test 5.2 { + vfs_set_readmark db main 1 100 + vfs_set_readmark db main 2 100 + vfs_set_readmark db main 3 100 + vfs_set_readmark db main 4 100 +} {100} + +do_execsql_test 5.3 { + SELECT count(*) FROM t1 +} {20} + +do_test 5.3 { + list [vfs_set_readmark db main 1] \ + [vfs_set_readmark db main 2] \ + [vfs_set_readmark db main 3] \ + [vfs_set_readmark db main 4] +} {24 100 100 100} + +tvfs script xShmLock +tvfs filter xShmLock +set ::cnt 20 +proc xShmLock {args} { + incr ::cnt -1 + if {$::cnt>0} { return SQLITE_BUSY } + return SQLITE_OK +} + +do_test 5.4 { + vfs_set_readmark db main 1 100 + execsql { SELECT count(*) FROM t1 } +} {20} + +vfs_set_readmark db main 1 100 +vfs_set_readmark db main 2 100 +vfs_set_readmark db main 3 100 +vfs_set_readmark db main 4 100 + +tvfs script xShmMapLock +tvfs filter {xShmLock xShmMap} +proc xShmMapLock {method args} { + if {$method=="xShmMap"} { + return "SQLITE_READONLY" + } + return SQLITE_BUSY +} + +sqlite3 db2 test.db -vfs tvfs +breakpoint +do_test 5.5 { + list [catch { execsql { SELECT count(*) FROM t1 } db2 } msg] $msg +} {1 {attempt to write a readonly database}} + +tvfs filter {} +vfs_set_readmark db main 1 1 + +do_test 5.6 { + list [catch { execsql { SELECT count(*) FROM t1 } db2 } msg] $msg +} {0 20} +db2 close +db close + +#------------------------------------------------------------------------- +# Cause an SQLITE_PROTOCOL while attempting to restart the wal file. +# +reset_db +tvfs filter {} +db close +sqlite3 db test.db -vfs tvfs +do_execsql_test 6.0 { + PRAGMA auto_vacuum = 0; + PRAGMA page_size = 1024; + CREATE TABLE t1(x); + PRAGMA journal_mode = wal; + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s LIMIT 20 + ) + INSERT INTO t1 SELECT randomblob(750) FROM s; +} {wal} + +do_test 6.1 { + execsql { PRAGMA wal_checkpoint } + set {} {} +} {} + +tvfs filter xShmLock +tvfs script xShmLock +set ::flag 0 +proc xShmLock {method file handle spec} { + if {$::flag && [lrange $spec 2 end]=="lock shared"} { + return SQLITE_BUSY + } + if {$spec=="3 1 unlock shared"} { + set ::flag 1 + } + return SQLITE_OK +} + +puts "# WARNING: This next test takes around 12 seconds" +do_catchsql_test 6.2 { + INSERT INTO t1 VALUES(1); +} {1 {locking protocol}} + +#------------------------------------------------------------------------- +# Check that a checkpoint fails if it cannot get the CHECKPOINTER lock +# +reset_db +tvfs filter {} +db close +sqlite3 db test.db -vfs tvfs +do_execsql_test 7.0 { + PRAGMA auto_vacuum = 0; + PRAGMA page_size = 1024; + CREATE TABLE t1(x); + PRAGMA journal_mode = wal; + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s LIMIT 20 + ) + INSERT INTO t1 SELECT randomblob(750) FROM s; +} {wal} + +tvfs script xShmLock +tvfs filter xShmLock +proc xShmLock {method file handle spec} { + if {$spec=="1 1 lock exclusive"} { + return SQLITE_BUSY + } + return SQLITE_OK +} + +do_execsql_test 7.1 { + PRAGMA wal_checkpoint +} {1 -1 -1} + +#------------------------------------------------------------------------- +# Check that the page cache is correctly flushed if a checkpointer using +# a version 2 VFS makes a checkpoint with an out-of-date cache. +# +reset_db +testvfs tvfs2 -iversion 2 +db close +sqlite3 db test.db -vfs tvfs2 +do_execsql_test 8.0 { + PRAGMA auto_vacuum = 0; + PRAGMA page_size = 1024; + CREATE TABLE t1(x); + PRAGMA journal_mode = wal; + WITH s(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM s LIMIT 20 ) + INSERT INTO t1 SELECT randomblob(75) FROM s; +} {wal} + +do_execsql_test 8.1 { SELECT count(*) FROM t1 } {20} + +do_test 8.2 { + sqlite3 db2 test.db -vfs tvfs2 + execsql { + INSERT INTO t1 VALUES(randomblob(75)); + } db2 + db2 close +} {} + +do_execsql_test 8.3 { + PRAGMA wal_checkpoint; + SELECT count(*) FROM t1 +} {0 5 5 21} +db close +tvfs2 delete + +#------------------------------------------------------------------------- +reset_db +db close +sqlite3 db test.db -vfs tvfs +do_execsql_test 9.0 { + PRAGMA auto_vacuum = 0; + PRAGMA page_size = 1024; + CREATE TABLE t1(x); + PRAGMA journal_mode = wal; + WITH s(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM s LIMIT 20 ) + INSERT INTO t1 SELECT randomblob(75) FROM s; +} {wal} + +sqlite3 db2 test.db -vfs tvfs +tvfs filter {xShmMap xShmLock} +tvfs script xShmMap +proc xShmMap {method file handle args} { + switch -- $method { + xShmMap { + return "SQLITE_READONLY_CANTINIT" + } + xShmLock { + if {$args=="{3 1 lock shared}"} { + return "SQLITE_IOERR" + } + } + } +} + +do_test 9.1 { + catchsql { SELECT count(*) FROM t1 } db2 +} {1 {disk I/O error}} + +db close +db2 close +tvfs delete +finish_test diff --git a/test/where.test b/test/where.test index 0b5bb934d0..c377006fb9 100644 --- a/test/where.test +++ b/test/where.test @@ -11,7 +11,6 @@ # This file implements regression tests for SQLite library. The # focus of this file is testing the use of indices in WHERE clases. # -# $Id: where.test,v 1.50 2008/11/03 09:06:06 danielk1977 Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl @@ -70,10 +69,10 @@ do_test where-1.1.1b { } {3 121 10 3} do_eqp_test where-1.1.2 { SELECT x, y, w FROM t1 WHERE w=10 -} {*SEARCH TABLE t1 USING INDEX i1w (w=?)*} +} {*SEARCH t1 USING INDEX i1w (w=?)*} do_eqp_test where-1.1.2b { SELECT x, y, w FROM t1 WHERE w IS 10 -} {*SEARCH TABLE t1 USING INDEX i1w (w=?)*} +} {*SEARCH t1 USING INDEX i1w (w=?)*} do_test where-1.1.3 { db status step } {0} @@ -85,13 +84,13 @@ do_test where-1.1.5 { } {99} do_eqp_test where-1.1.6 { SELECT x, y, w FROM t1 WHERE +w=10 -} {*SCAN TABLE t1*} +} {*SCAN t1*} do_test where-1.1.7 { count {SELECT x, y, w AS abc FROM t1 WHERE abc=10} } {3 121 10 3} do_eqp_test where-1.1.8 { SELECT x, y, w AS abc FROM t1 WHERE abc=10 -} {*SEARCH TABLE t1 USING INDEX i1w (w=?)*} +} {*SEARCH t1 USING INDEX i1w (w=?)*} do_test where-1.1.9 { db status step } {0} @@ -118,22 +117,22 @@ do_test where-1.4.1b { } {11 3 144 3} do_eqp_test where-1.4.2 { SELECT w, x, y FROM t1 WHERE 11=w AND x>2 -} {*SEARCH TABLE t1 USING INDEX i1w (w=?)*} +} {*SEARCH t1 USING INDEX i1w (w=?)*} do_eqp_test where-1.4.2b { SELECT w, x, y FROM t1 WHERE 11 IS w AND x>2 -} {*SEARCH TABLE t1 USING INDEX i1w (w=?)*} +} {*SEARCH t1 USING INDEX i1w (w=?)*} do_test where-1.4.3 { count {SELECT w AS a, x AS b, y FROM t1 WHERE 11=a AND b>2} } {11 3 144 3} do_eqp_test where-1.4.4 { SELECT w AS a, x AS b, y FROM t1 WHERE 11=a AND b>2 -} {*SEARCH TABLE t1 USING INDEX i1w (w=?)*} +} {*SEARCH t1 USING INDEX i1w (w=?)*} do_test where-1.5 { count {SELECT x, y FROM t1 WHERE y<200 AND w=11 AND x>2} } {3 144 3} do_eqp_test where-1.5.2 { SELECT x, y FROM t1 WHERE y<200 AND w=11 AND x>2 -} {*SEARCH TABLE t1 USING INDEX i1w (w=?)*} +} {*SEARCH t1 USING INDEX i1w (w=?)*} do_test where-1.6 { count {SELECT x, y FROM t1 WHERE y<200 AND x>2 AND w=11} } {3 144 3} @@ -145,10 +144,10 @@ do_test where-1.8 { } {3 144 3} do_eqp_test where-1.8.2 { SELECT x, y FROM t1 WHERE w>10 AND y=144 AND x=3 -} {*SEARCH TABLE t1 USING INDEX i1xy (x=? AND y=?)*} +} {*SEARCH t1 USING INDEX i1xy (x=? AND y=?)*} do_eqp_test where-1.8.3 { SELECT x, y FROM t1 WHERE y=144 AND x=3 -} {*SEARCH TABLE t1 USING COVERING INDEX i1xy (x=? AND y=?)*} +} {*SEARCH t1 USING COVERING INDEX i1xy (x=? AND y=?)*} do_test where-1.9 { count {SELECT x, y FROM t1 WHERE y=144 AND w>10 AND x=3} } {3 144 3} @@ -546,6 +545,7 @@ do_test where-6.1 { CREATE INDEX t3acb ON t3(a,c,b); INSERT INTO t3 SELECT w, 101-w, y FROM t1; SELECT count(*), sum(a), sum(b), sum(c) FROM t3; + ANALYZE; } } {100 5050 5050 348550} do_test where-6.2 { @@ -573,11 +573,16 @@ do_test where-6.6 { SELECT * FROM t3 WHERE a>0 ORDER BY a LIMIT 3 } } {1 100 4 2 99 9 3 98 16 nosort} -do_test where-6.7 { +do_test where-6.7.1 { cksort { - SELECT * FROM t3 WHERE b>0 ORDER BY a LIMIT 3 + SELECT * FROM t3 WHERE b>0 ORDER BY a LIMIT 10 } -} {1 100 4 2 99 9 3 98 16 nosort} +} {/1 100 4 2 99 9 3 98 16 .* nosort/} +do_test where-6.7.2 { + cksort { + SELECT * FROM t3 WHERE b>0 ORDER BY a LIMIT 1 + } +} {1 100 4 nosort} ifcapable subquery { do_test where-6.8a { cksort { @@ -1343,16 +1348,25 @@ do_execsql_test where-18.1 { INSERT INTO t181 VALUES(1); SELECT DISTINCT a FROM t181 LEFT JOIN t182 ON a=b ORDER BY c IS NULL; } {1} +do_execsql_test where-18.1rj { + SELECT DISTINCT a FROM t182 RIGHT JOIN t181 ON a=b ORDER BY c IS NULL; +} {1} do_execsql_test where-18.2 { SELECT DISTINCT a FROM t181 LEFT JOIN t182 ON a=b ORDER BY +c; } {1} do_execsql_test where-18.3 { SELECT DISTINCT a FROM t181 LEFT JOIN t182 ON a=b ORDER BY c; } {1} +do_execsql_test where-18.3rj { + SELECT DISTINCT a FROM t182 RIGHT JOIN t181 ON a=b ORDER BY c; +} {1} do_execsql_test where-18.4 { INSERT INTO t181 VALUES(1),(1),(1),(1); SELECT DISTINCT a FROM t181 LEFT JOIN t182 ON a=b ORDER BY +c; } {1} +do_execsql_test where-18.4rj { + SELECT DISTINCT a FROM t182 RIGHT JOIN t181 ON a=b ORDER BY +c; +} {1} do_execsql_test where-18.5 { INSERT INTO t181 VALUES(2); SELECT DISTINCT a FROM t181 LEFT JOIN t182 ON a=b ORDER BY c IS NULL, +a; @@ -1362,5 +1376,332 @@ do_execsql_test where-18.6 { SELECT DISTINCT a FROM t181 LEFT JOIN t182 ON a=b ORDER BY +a, +c IS NULL; } {1 2} +# Make sure the OR optimization works on a JOIN +# +do_execsql_test where-19.0 { + CREATE TABLE t191(a INT UNIQUE NOT NULL, b INT UNIQUE NOT NULL,c,d); + CREATE INDEX t191a ON t1(a); + CREATE INDEX t191b ON t1(b); + CREATE TABLE t192(x INTEGER PRIMARY KEY,y INT, z INT); + + EXPLAIN QUERY PLAN + SELECT t191.rowid FROM t192, t191 WHERE (a=y OR b=y) AND x=?1; +} {/.* sqlite_autoindex_t191_1 .* sqlite_autoindex_t191_2 .*/} + +# 2018-04-24 ticket [https://sqlite.org/src/info/4ba5abf65c5b0f9a] +# Index on expressions leads to an incorrect answer for a LEFT JOIN +# +do_execsql_test where-20.0 { + CREATE TABLE t201(x); + CREATE TABLE t202(y, z); + INSERT INTO t201 VALUES('key'); + INSERT INTO t202 VALUES('key', -1); + CREATE INDEX t202i ON t202(y, ifnull(z, 0)); + SELECT count(*) FROM t201 LEFT JOIN t202 ON (x=y) WHERE ifnull(z, 0) >=0; +} {0} + +do_execsql_test where-21.0 { + CREATE TABLE t12(a, b, c); + CREATE TABLE t13(x); + CREATE INDEX t12ab ON t12(b, a); + CREATE INDEX t12ac ON t12(c, a); + + INSERT INTO t12 VALUES(4, 0, 1); + INSERT INTO t12 VALUES(4, 1, 0); + INSERT INTO t12 VALUES(5, 0, 1); + INSERT INTO t12 VALUES(5, 1, 0); + + INSERT INTO t13 VALUES(1), (2), (3), (4); +} +do_execsql_test where-21.1 { + SELECT * FROM t12 WHERE + a = (SELECT * FROM (SELECT count(*) FROM t13 LIMIT 5) ORDER BY 1 LIMIT 10) + AND (b=1 OR c=1); +} { + 4 1 0 + 4 0 1 +} + +# 2018-11-05: ticket [https://sqlite.org/src/tktview/65eb38f6e46de8c75e188a] +# Incorrect result in LEFT JOIN when STAT4 is enabled. +# +sqlite3 db :memory: +do_execsql_test where-22.1 { + CREATE TABLE t1(a INT); + CREATE INDEX t1a ON t1(a); + INSERT INTO t1(a) VALUES(NULL),(NULL),(42),(NULL),(NULL); + CREATE TABLE t2(dummy INT); + SELECT count(*) FROM t1 LEFT JOIN t2 ON a IS NOT NULL; +} {5} + +# 20190-02-22: A bug introduced by checkin +# https://sqlite.org/src/info/fa792714ae62fa98. +# +do_execsql_test where-23.0 { + DROP TABLE IF EXISTS t1; + DROP TABLE IF EXISTS t2; + CREATE TABLE t1(a INTEGER PRIMARY KEY); + INSERT INTO t1(a) VALUES(1),(2),(3); + CREATE TABLE t2(x INTEGER PRIMARY KEY, y INT); + INSERT INTO t2(y) VALUES(2),(3); + SELECT * FROM t1, t2 WHERE a=y AND y=3; +} {3 2 3} + +#------------------------------------------------------------------------- +# +reset_db +do_execsql_test where-24.0 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b); + INSERT INTO t1 VALUES(1, 'one'); + INSERT INTO t1 VALUES(2, 'two'); + INSERT INTO t1 VALUES(3, 'three'); + INSERT INTO t1 VALUES(4, 'four'); +} + +foreach {tn sql res} { + 1 "SELECT b FROM t1" {one two three four} + 2 "SELECT b FROM t1 WHERE a<4" {one two three} + 3 "SELECT b FROM t1 WHERE a>1" {two three four} + 4 "SELECT b FROM t1 WHERE a>1 AND a<4" {two three} + + 5 "SELECT b FROM t1 WHERE a>? AND a<4" {} + 6 "SELECT b FROM t1 WHERE a>1 AND a<?" {} + 7 "SELECT b FROM t1 WHERE a>? AND a<?" {} + + 7 "SELECT b FROM t1 WHERE a>=? AND a<=4" {} + 8 "SELECT b FROM t1 WHERE a>=1 AND a<=?" {} + 9 "SELECT b FROM t1 WHERE a>=? AND a<=?" {} +} { + set rev [list] + foreach r $res { set rev [concat $r $rev] } + + do_execsql_test where-24.$tn.1 "$sql" $res + do_execsql_test where-24.$tn.2 "$sql ORDER BY rowid" $res + do_execsql_test where-24.$tn.3 "$sql ORDER BY rowid DESC" $rev + + do_execsql_test where-24-$tn.4 " + BEGIN; + DELETE FROM t1; + $sql; + $sql ORDER BY rowid; + $sql ORDER BY rowid DESC; + ROLLBACK; + " +} + +#------------------------------------------------------------------------- +# +reset_db +do_execsql_test where-25.0 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c); + CREATE UNIQUE INDEX i1 ON t1(c); + INSERT INTO t1 VALUES(1, 'one', 'i'); + INSERT INTO t1 VALUES(2, 'two', 'ii'); + + CREATE TABLE t2(a INTEGER PRIMARY KEY, b, c); + CREATE UNIQUE INDEX i2 ON t2(c); + INSERT INTO t2 VALUES(1, 'one', 'i'); + INSERT INTO t2 VALUES(2, 'two', 'ii'); + INSERT INTO t2 VALUES(3, 'three', 'iii'); + + PRAGMA writable_schema = 1; + UPDATE sqlite_schema SET rootpage = ( + SELECT rootpage FROM sqlite_schema WHERE name = 'i2' + ) WHERE name = 'i1'; +} +db close +sqlite3 db test.db +do_catchsql_test where-25.1 { + DELETE FROM t1 WHERE c='iii' +} {1 {database disk image is malformed}} +do_catchsql_test where-25.2 { + INSERT INTO t1 VALUES(4, 'four', 'iii') + ON CONFLICT(c) DO UPDATE SET b=NULL +} {1 {database disk image is malformed}} + +reset_db +do_execsql_test where-25.3 { + CREATE TABLE t1(a PRIMARY KEY, b, c) WITHOUT ROWID; + CREATE UNIQUE INDEX i1 ON t1(c); + INSERT INTO t1 VALUES(1, 'one', 'i'); + INSERT INTO t1 VALUES(2, 'two', 'ii'); + + CREATE TABLE t2(a INTEGER PRIMARY KEY, b, c); + CREATE UNIQUE INDEX i2 ON t2(c); + INSERT INTO t2 VALUES(1, 'one', 'i'); + INSERT INTO t2 VALUES(2, 'two', 'ii'); + INSERT INTO t2 VALUES(3, 'three', 'iii'); + + PRAGMA writable_schema = 1; + UPDATE sqlite_schema SET rootpage = ( + SELECT rootpage FROM sqlite_schema WHERE name = 'i2' + ) WHERE name = 'i1'; +} +db close +sqlite3 db test.db +do_catchsql_test where-25.4 { + SELECT * FROM t1 WHERE c='iii' +} {0 {}} +do_catchsql_test where-25.5 { + INSERT INTO t1 VALUES(4, 'four', 'iii') + ON CONFLICT(c) DO UPDATE SET b=NULL +} {1 {corrupt database}} + +# 2019-08-21 Ticket https://sqlite.org/src/info/d9f584e936c7a8d0 +# +db close +sqlite3 db :memory: +do_execsql_test where-26.1 { + CREATE TABLE t0(c0 INTEGER PRIMARY KEY, c1 TEXT); + INSERT INTO t0(c0, c1) VALUES (1, 'a'); + CREATE TABLE t1(c0 INT PRIMARY KEY, c1 TEXT); + INSERT INTO t1(c0, c1) VALUES (1, 'a'); + SELECT * FROM t0 WHERE '-1' BETWEEN 0 AND t0.c0; +} {1 a} +do_execsql_test where-26.2 { + SELECT * FROM t1 WHERE '-1' BETWEEN 0 AND t1.c0; +} {1 a} +do_execsql_test where-26.3 { + SELECT * FROM t0 WHERE '-1'>=0 AND '-1'<=t0.c0; +} {1 a} +do_execsql_test where-26.4 { + SELECT * FROM t1 WHERE '-1'>=0 AND '-1'<=t1.c0; +} {1 a} +do_execsql_test where-26.5 { + SELECT '-1' BETWEEN 0 AND t0.c0 FROM t0; +} {1} +do_execsql_test where-26.6 { + SELECT '-1' BETWEEN 0 AND t1.c0 FROM t1; +} {1} +do_execsql_test where-26.7 { + SELECT '-1'>=0 AND '-1'<=t0.c0 FROM t0; +} {1} +do_execsql_test where-26.8 { + SELECT '-1'>=0 AND '-1'<=t1.c0 FROM t1; +} {1} + +# 2021-07-19 https://sqlite.org/forum/forumpost/2bdb86a068 +# Lose of precision when doing comparisons between integer and +# floating point values that are near 9223372036854775807 in the +# OP_SeekGE opcode (and similar). +# +# Valgrind documentation acknowledges that under valgrind, FP calculations +# may not be as accurate as on x86/amd64 hardware. This seems to be causing +# these tests to fail. +# +# https://valgrind.org/docs/manual/manual-core.html#manual-core.limits +# +if {[permutation]!="valgrind"} { + reset_db + do_execsql_test where-27.1 { + CREATE TABLE t1(a INTEGER PRIMARY KEY); + INSERT INTO t1(a) VALUES(9223372036854775807); + SELECT 1 FROM t1 WHERE a>=(9223372036854775807+1); + } {} + do_execsql_test where-27.2 { + SELECT a>=9223372036854775807+1 FROM t1; + } {0} +} + +# 2022-05-10 dbsqlfuzz 4c5e3e89bc251d28378be88233f531b84ec66901 +# +reset_db +do_execsql_test where-28.1 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b INT); + CREATE INDEX t1b ON t1(b,b,b,b,b,b,b,b,b,b,b,b,b); + INSERT INTO t1(a,b) VALUES(1,1),(15,2),(19,5); + UPDATE t1 SET b=999 WHERE a IN (SELECT 15) AND b IN (1,2); + SELECT * FROM t1; +} { + 1 1 + 15 999 + 19 5 +} + +# 2022-12-07 Yong Heng [https://sqlite.org/forum/forumpost/dfe8084751] +# +ifcapable vtab { + do_execsql_test where-29.1 { + SELECT DISTINCT 'xyz' FROM pragma_cache_size + WHERE rowid OR abs(0) + ORDER BY + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1; + } {xyz} +} + +# 2023-01-30 +# Tests case for the query planner performance issue reported by +# https://sqlite.org/forum/forumpost/1d571c0296 +# +# The fix was to adjust the cost of computing an automatic index for +# ephemeral tables, to help ensure that they are generated if they are +# needed. The test case below only looks at the query plan. But 12x +# improved performance has been verified by populating the "raw" table +# with 100K rows of random data and running actual speed tests. +# +do_test where-30.1 { + unset -nocomplain res + set res {} + db eval {CREATE TABLE raw(country,date,total,delta, UNIQUE(country,date));} + db eval { + EXPLAIN QUERY PLAN + WITH + -- Find the country and min/max date + init(country, date, fin) AS (SELECT country, min(date), max(date) + FROM raw WHERE total > 0 GROUP BY country), + + -- Generate the date stream for each country + src(country, date) AS (SELECT raw.country, raw.date + FROM raw JOIN init i on raw.country = i.country AND raw.date > i.date + ORDER BY raw.country, raw.date), + + -- Generate the x & y for each entry in the country/date stream + vals(country, date, x, y) AS (SELECT src.country, src.date, + julianday(raw.date) - julianday(src.date), log(delta+1) + FROM src JOIN raw on raw.country = src.country + AND raw.date > date(src.date,'-7 days') + AND raw.date <= src.date AND delta >= 0), + + -- Accumulate the data we need + sums(country, date, x2, x, n, xy, y) AS (SELECT country, date, + sum(x*x*1.0), sum(x*1.0), sum(1.0), sum(x*y*1.0), sum(y*1.0) + FROM vals GROUP BY 1, 2), + + -- use these to calculate to divisor for the inverse matrix + mult(country, date, m) AS (SELECT country, date, 1.0/(x2 * n - x * x) + FROM sums), + + -- Build the inverse matrix + inv(country, date, a,b,c,d) AS (SELECT mult.country, mult.date, n * m, + -x * m, -x * m, x2 * m + FROM mult JOIN sums on sums.country=mult.country + AND mult.date=sums.date), + + -- Calculate the coefficients for the least squares fit + fit(country, date, a, b) AS (SELECT inv.country, inv.date, + a * xy + b * y, c * xy + d * y + FROM inv + JOIN mult on mult.country = inv.country AND mult.date = inv.date + JOIN sums on sums.country = mult.country AND sums.date = mult.date + ) + SELECT *, nFin/nPrev - 1 AS growth, log(2)/log(nFin/nPrev) AS doubling + FROM (SELECT f.*, exp(b) - 1 AS nFin, exp(a* (-1) + b) - 1 AS nPrev + FROM fit f JOIN init i on i.country = f.country + AND f.date <= date(i.fin,'-3 days')) + WHERE nPrev > 0 AND nFin > 0; + } { + if {$parent!=0} continue + if {![string match SCAN* $detail]} continue + lappend res SCAN + } + set res +} {SCAN} +# ^^^^^^-- there should only be one top-level table scan in the query plan. finish_test diff --git a/test/where2.test b/test/where2.test index 434a7bcd9a..83740bffdf 100644 --- a/test/where2.test +++ b/test/where2.test @@ -76,10 +76,12 @@ proc queryplan {sql} { set eqp [execsql "EXPLAIN QUERY PLAN $sql"] # puts eqp=$eqp foreach {a b c x} $eqp { - if {[regexp { TABLE (\w+ AS )?(\w+) USING.* INDEX (\w+)\y} \ - $x all as tab idx]} { + if {[regexp {SCAN CONSTANT} $x]} { + # noop + } elseif {[regexp {(SCAN|SEARCH) (\w+ AS )?(\w+) USING.* INDEX (\w+)\y} \ + $x all ss as tab idx]} { lappend data $tab $idx - } elseif {[regexp { TABLE (\w+ AS )?(\w+)\y} $x all as tab]} { + } elseif {[regexp {(SCAN|SEARCH) (\w+ AS )?(\w+)\y} $x all ss as tab]} { lappend data $tab * } } @@ -753,7 +755,7 @@ do_execsql_test where2-12.1 { SELECT a.x, b.x FROM t12 AS a JOIN t12 AS b ON a.y=b.x WHERE (b.x=$abc OR b.y=$abc); -} {/.*SEARCH TABLE t12 AS b .*SEARCH TABLE t12 AS b .*/} +} {/SEARCH b .*SEARCH b /} } # Verify that all necessary OP_OpenRead opcodes occur in the OR optimization. @@ -765,4 +767,31 @@ do_execsql_test where2-13.1 { SELECT * FROM t13 WHERE (1=2 AND a=3) OR a=4; } {4 5} +# https://sqlite.org/src/info/5e3c886796e5512e (2016-03-09) +# Correlated subquery on the RHS of an IN operator +# +do_execsql_test where2-14.1 { + CREATE TABLE t14a(x INTEGER PRIMARY KEY); + INSERT INTO t14a(x) VALUES(1),(2),(3),(4); + CREATE TABLE t14b(y INTEGER PRIMARY KEY); + INSERT INTO t14b(y) VALUES(1); + SELECT x FROM t14a WHERE x NOT IN (SELECT x FROM t14b); +} {} + +# Demonstrate that the code removed by +# https://sqlite.org/src/info/2025-09-17T17:09:07Z is in fact +# necessary. Answer confirmed by PostgreSQL +# +do_execsql_test where2-15.1 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a INTEGER PRIMARY KEY,b INT); + INSERT INTO t1 VALUES(12,34),(56,78),(90,12); + DROP TABLE IF EXISTS t2; + CREATE TABLE t2(x INT UNIQUE); + INSERT INTO t2 VALUES(78),(12); + SELECT a,b,quote(x),'|' + FROM t1 LEFT JOIN (SELECT x FROM t2) AS s1 ON t1.b=s1.x + ORDER BY a,EXISTS(SELECT 1 FROM t1 LEFT JOIN (SELECT x AS y FROM t2) AS s2 ON t1.b=s2.y),x; +} {12 34 NULL | 56 78 78 | 90 12 12 |} + finish_test diff --git a/test/where3.test b/test/where3.test index c2804b5579..f574c0d55f 100644 --- a/test/where3.test +++ b/test/where3.test @@ -45,7 +45,7 @@ do_test where3-1.1 { } } {222 two 2 222 {} {}} -ifcapable explain { +ifcapable explain&&!cursorhints { do_test where3-1.1.1 { explain_no_trace {SELECT * FROM t1, t2 LEFT JOIN t3 ON q=x WHERE p=2 AND a=q} @@ -86,7 +86,7 @@ do_test where3-1.2 { } } {1 {Value for C1.1} {Value for C2.1} 2 {} {Value for C2.2} 3 {Value for C1.3} {Value for C2.3}} -ifcapable explain { +ifcapable explain&&!cursorhints { do_test where3-1.2.1 { explain_no_trace { SELECT parent1.parent1key, child1.value, child2.value @@ -111,10 +111,12 @@ proc queryplan {sql} { set eqp [execsql "EXPLAIN QUERY PLAN $sql"] # puts eqp=$eqp foreach {a b c x} $eqp { - if {[regexp { TABLE (\w+ AS )?(\w+) USING.* INDEX (\w+)\y} \ - $x all as tab idx]} { + if {[regexp {SCAN CONSTANT} $x]} { + # noop + } elseif {[regexp {(SCAN|SEARCH) (\w+ AS )?(\w+) USING.* INDEX (\w+)\y} \ + $x all ss as tab idx]} { lappend data $tab $idx - } elseif {[regexp { TABLE (\w+ AS )?(\w+)\y} $x all as tab]} { + } elseif {[regexp {(SCAN|SEARCH) (\w+ AS )?(\w+)\y} $x all ss as tab]} { lappend data $tab * } } @@ -235,17 +237,20 @@ do_execsql_test where3-3.0 { CREATE TABLE t302(x, y); INSERT INTO t302 VALUES(4,5); ANALYZE; - explain query plan SELECT * FROM t302, t301 WHERE t302.x=5 AND t301.a=t302.y; +} +do_eqp_test where3-3.0a { + SELECT * FROM t302, t301 WHERE t302.x=5 AND t301.a=t302.y; } { - 0 0 0 {SCAN TABLE t302} - 0 1 1 {SEARCH TABLE t301 USING INTEGER PRIMARY KEY (rowid=?)} + QUERY PLAN + |--SCAN t302 + `--SEARCH t301 USING INTEGER PRIMARY KEY (rowid=?) } -do_execsql_test where3-3.1 { - explain query plan +do_eqp_test where3-3.1 { SELECT * FROM t301, t302 WHERE t302.x=5 AND t301.a=t302.y; } { - 0 0 1 {SCAN TABLE t302} - 0 1 0 {SEARCH TABLE t301 USING INTEGER PRIMARY KEY (rowid=?)} + QUERY PLAN + |--SCAN t302 + `--SEARCH t301 USING INTEGER PRIMARY KEY (rowid=?) } do_execsql_test where3-3.2 { SELECT * FROM t301 WHERE c=3 AND a IS NULL; @@ -266,25 +271,25 @@ do_execsql_test where3-4.0 { EXPLAIN QUERY PLAN SELECT * FROM t400, t401, t402 WHERE t402.z GLOB 'abc*'; } { - 0 0 2 {SCAN TABLE t402} - 0 1 0 {SCAN TABLE t400} - 0 2 1 {SCAN TABLE t401} + 0 0 2 {SCAN t402} + 0 1 0 {SCAN t400} + 0 2 1 {SCAN t401} } do_execsql_test where3-4.1 { EXPLAIN QUERY PLAN SELECT * FROM t400, t401, t402 WHERE t401.r GLOB 'abc*'; } { - 0 0 1 {SCAN TABLE t401} - 0 1 0 {SCAN TABLE t400} - 0 2 2 {SCAN TABLE t402} + 0 0 1 {SCAN t401} + 0 1 0 {SCAN t400} + 0 2 2 {SCAN t402} } do_execsql_test where3-4.2 { EXPLAIN QUERY PLAN SELECT * FROM t400, t401, t402 WHERE t400.c GLOB 'abc*'; } { - 0 0 0 {SCAN TABLE t400} - 0 1 1 {SCAN TABLE t401} - 0 2 2 {SCAN TABLE t402} + 0 0 0 {SCAN t400} + 0 1 1 {SCAN t401} + 0 2 2 {SCAN t402} } } ;# endif @@ -308,8 +313,8 @@ do_execsql_test where3-5.0 { CREATE INDEX bbb_111 ON bbb (fk, type); CREATE INDEX bbb_222 ON bbb (parent, position); CREATE INDEX bbb_333 ON bbb (fk, lastModified); - - EXPLAIN QUERY PLAN +} +do_eqp_test where3-5.0a { SELECT bbb.title AS tag_title FROM aaa JOIN bbb ON bbb.id = aaa.parent WHERE aaa.fk = 'constant' @@ -317,12 +322,12 @@ do_execsql_test where3-5.0 { AND bbb.parent = 4 ORDER BY bbb.title COLLATE NOCASE ASC; } { - 0 0 0 {SEARCH TABLE aaa USING INDEX aaa_333 (fk=?)} - 0 1 1 {SEARCH TABLE bbb USING INTEGER PRIMARY KEY (rowid=?)} - 0 0 0 {USE TEMP B-TREE FOR ORDER BY} + QUERY PLAN + |--SEARCH aaa USING INDEX aaa_333 (fk=?) + |--SEARCH bbb USING INTEGER PRIMARY KEY (rowid=?) + `--USE TEMP B-TREE FOR ORDER BY } -do_execsql_test where3-5.1 { - EXPLAIN QUERY PLAN +do_eqp_test where3-5.1 { SELECT bbb.title AS tag_title FROM aaa JOIN aaa AS bbb ON bbb.id = aaa.parent WHERE aaa.fk = 'constant' @@ -330,12 +335,12 @@ do_execsql_test where3-5.1 { AND bbb.parent = 4 ORDER BY bbb.title COLLATE NOCASE ASC; } { - 0 0 0 {SEARCH TABLE aaa USING INDEX aaa_333 (fk=?)} - 0 1 1 {SEARCH TABLE aaa AS bbb USING INTEGER PRIMARY KEY (rowid=?)} - 0 0 0 {USE TEMP B-TREE FOR ORDER BY} + QUERY PLAN + |--SEARCH aaa USING INDEX aaa_333 (fk=?) + |--SEARCH bbb USING INTEGER PRIMARY KEY (rowid=?) + `--USE TEMP B-TREE FOR ORDER BY } -do_execsql_test where3-5.2 { - EXPLAIN QUERY PLAN +do_eqp_test where3-5.2 { SELECT bbb.title AS tag_title FROM bbb JOIN aaa ON bbb.id = aaa.parent WHERE aaa.fk = 'constant' @@ -343,12 +348,12 @@ do_execsql_test where3-5.2 { AND bbb.parent = 4 ORDER BY bbb.title COLLATE NOCASE ASC; } { - 0 0 1 {SEARCH TABLE aaa USING INDEX aaa_333 (fk=?)} - 0 1 0 {SEARCH TABLE bbb USING INTEGER PRIMARY KEY (rowid=?)} - 0 0 0 {USE TEMP B-TREE FOR ORDER BY} + QUERY PLAN + |--SEARCH aaa USING INDEX aaa_333 (fk=?) + |--SEARCH bbb USING INTEGER PRIMARY KEY (rowid=?) + `--USE TEMP B-TREE FOR ORDER BY } -do_execsql_test where3-5.3 { - EXPLAIN QUERY PLAN +do_eqp_test where3-5.3 { SELECT bbb.title AS tag_title FROM aaa AS bbb JOIN aaa ON bbb.id = aaa.parent WHERE aaa.fk = 'constant' @@ -356,9 +361,10 @@ do_execsql_test where3-5.3 { AND bbb.parent = 4 ORDER BY bbb.title COLLATE NOCASE ASC; } { - 0 0 1 {SEARCH TABLE aaa USING INDEX aaa_333 (fk=?)} - 0 1 0 {SEARCH TABLE aaa AS bbb USING INTEGER PRIMARY KEY (rowid=?)} - 0 0 0 {USE TEMP B-TREE FOR ORDER BY} + QUERY PLAN + |--SEARCH aaa USING INDEX aaa_333 (fk=?) + |--SEARCH bbb USING INTEGER PRIMARY KEY (rowid=?) + `--USE TEMP B-TREE FOR ORDER BY } # Name resolution with NATURAL JOIN and USING @@ -486,5 +492,27 @@ foreach disabled_opt {none omit-noop-join all} { } {123} } +# 2023-12-23 +# https://sqlite.org/forum/forumpost/2568d1f6e6 +# +# Index usage should be "x=? and y=?" - equality on both values. +# Not: "x=? AND y>?" - inequality on "y" +# +reset_db +do_execsql_test where3-8.1 { + CREATE TABLE t1(a,b,c,d); INSERT INTO t1 VALUES(1,2,3,4); + CREATE TABLE t2(x,y); INSERT INTO t2 VALUES(3,4); + CREATE INDEX t2xy ON t2(x,y); + SELECT 1 FROM t1 JOIN t2 ON x=c AND y=d WHERE d>0; +} 1 +do_eqp_test where3-8.2 { + SELECT 1 FROM t1 JOIN t2 ON x=c AND y=d WHERE d>0; +} { + QUERY PLAN + |--SCAN t1 + `--SEARCH t2 USING COVERING INDEX t2xy (x=? AND y=?) +} + + finish_test diff --git a/test/where7.test b/test/where7.test index 00cf5eb278..681684b809 100644 --- a/test/where7.test +++ b/test/where7.test @@ -47,18 +47,33 @@ do_test where7-1.1 { SELECT * FROM t1; } } {1 2 3 4 2 3 4 5 3 4 6 8 4 5 10 15 5 10 100 1000} -do_execsql_test where7-1.1.1 { - CREATE TABLE t(a); - CREATE INDEX ta ON t(a); - INSERT INTO t(a) VALUES(1),(2); - SELECT * FROM t ORDER BY a; - SELECT * FROM t WHERE a<2 OR a<3 ORDER BY a; - PRAGMA count_changes=ON; - DELETE FROM t WHERE a<2 OR a<3; - SELECT * FROM t; - PRAGMA count_changes=OFF; - DROP TABLE t; -} {1 2 1 2 2} +if {[permutation] != "no_optimization"} { + do_execsql_test where7-1.1.1 { + CREATE TABLE t(a); + CREATE INDEX ta ON t(a); + INSERT INTO t(a) VALUES(1),(2); + SELECT * FROM t ORDER BY a; + SELECT * FROM t WHERE a<2 OR a<3 ORDER BY a; + PRAGMA count_changes=ON; + DELETE FROM t WHERE a<2 OR a<3; + SELECT * FROM t; + PRAGMA count_changes=OFF; + DROP TABLE t; + } {1 2 1 2 2} +} else { + do_execsql_test where7-1.1.1-noopt { + CREATE TABLE t(a); + CREATE INDEX ta ON t(a); + INSERT INTO t(a) VALUES(1),(2); + SELECT * FROM t ORDER BY a; + SELECT * FROM t WHERE a<2 OR a<3 ORDER BY a; + PRAGMA count_changes=ON; + DELETE FROM t WHERE a<2 OR a<3; + SELECT * FROM t; + PRAGMA count_changes=OFF; + DROP TABLE t; + } {1 2 1 2 3} +} do_test where7-1.2 { count_steps { SELECT a FROM t1 WHERE b=3 OR c=6 ORDER BY a @@ -23341,8 +23356,8 @@ do_execsql_test where7-3.1 { CREATE INDEX t302_c3 on t302(c3); CREATE INDEX t302_c8_c3 on t302(c8, c3); CREATE INDEX t302_c5 on t302(c5); - - EXPLAIN QUERY PLAN +} +do_eqp_test where7-3.2 { SELECT t302.c1 FROM t302 JOIN t301 ON t302.c8 = +t301.c8 WHERE t302.c2 = 19571 @@ -23351,10 +23366,38 @@ do_execsql_test where7-3.1 { OR t301.c8 = 1407424651264000) ORDER BY t302.c5 LIMIT 200; } { - 0 0 1 {SEARCH TABLE t301 USING COVERING INDEX t301_c4 (c4=?)} - 0 0 1 {SEARCH TABLE t301 USING INTEGER PRIMARY KEY (rowid=?)} - 0 1 0 {SEARCH TABLE t302 USING INDEX t302_c8_c3 (c8=? AND c3>?)} - 0 0 0 {USE TEMP B-TREE FOR ORDER BY} + QUERY PLAN + |--MULTI-INDEX OR + | |--INDEX 1 + | | `--SEARCH t301 USING COVERING INDEX t301_c4 (c4=?) + | `--INDEX 2 + | `--SEARCH t301 USING INTEGER PRIMARY KEY (rowid=?) + |--SEARCH t302 USING INDEX t302_c8_c3 (c8=? AND c3>?) + `--USE TEMP B-TREE FOR ORDER BY } +# 2022-03-03 https://sqlite.org/forum/forumpost/36937b197273d403 +# +# In the multi-index OR, if there is an auxiliary WHERE clause term +# that includes a subquery and that subquery is pushed down into the +# OR-clause subqueries, WHERE subquery might get coded as a subroutine. +# In that case, the covering-index optimizer will attempt to change +# table-references into index-references. But it will do so for the +# index of the OR branch in which the subquery is coded. If the +# subquery subroutine is called from a different OR branch, the +# index might be different and the index-reference will no longer +# work. tag-20220303a +# +reset_db +do_execsql_test 4.1 { + CREATE TABLE t0(w); + INSERT INTO t0(w) VALUES(1); + CREATE TABLE t1(x INT, y INT PRIMARY KEY, z); + INSERT INTO t1 VALUES(0,111,222); + CREATE INDEX t1zxy ON t1(z,x,y); + SELECT y FROM t1 + WHERE (z=222 OR y=111) + AND (false OR EXISTS(SELECT 1 FROM t0 WHERE t1.y)); +} {111} + finish_test diff --git a/test/where8.test b/test/where8.test index 38214bc895..a8dbcfd9fe 100644 --- a/test/where8.test +++ b/test/where8.test @@ -16,6 +16,11 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl +if {[permutation]=="sorterref"} { + finish_test + return +} + # Test organization: # # where8-1.*: Tests to demonstrate simple cases work with a single table diff --git a/test/where9.test b/test/where9.test index d073074d43..4757881532 100644 --- a/test/where9.test +++ b/test/where9.test @@ -357,25 +357,31 @@ do_test where9-2.8 { ifcapable explain { - do_execsql_test where9-3.1 { - EXPLAIN QUERY PLAN + do_eqp_test where9-3.1 { SELECT t2.a FROM t1, t2 WHERE t1.a=80 AND ((t1.c=t2.c AND t1.d=t2.d) OR t1.f=t2.f) - } { - 0 0 0 {SEARCH TABLE t1 USING INTEGER PRIMARY KEY (rowid=?)} - 0 1 1 {SEARCH TABLE t2 USING INDEX t2d (d=?)} - 0 1 1 {SEARCH TABLE t2 USING COVERING INDEX t2f (f=?)} - } - do_execsql_test where9-3.2 { - EXPLAIN QUERY PLAN + } [string map {"\n " \n} { + QUERY PLAN + |--SEARCH t1 USING INTEGER PRIMARY KEY (rowid=?) + `--MULTI-INDEX OR + |--INDEX 1 + | `--SEARCH t2 USING INDEX t2d (d=?) + `--INDEX 3 + `--SEARCH t2 USING COVERING INDEX t2f (f=?) + }] + do_eqp_test where9-3.2 { SELECT coalesce(t2.a,9999) FROM t1 LEFT JOIN t2 ON (t1.c+1=t2.c AND t1.d=t2.d) OR (t1.f||'x')=t2.f WHERE t1.a=80 - } { - 0 0 0 {SEARCH TABLE t1 USING INTEGER PRIMARY KEY (rowid=?)} - 0 1 1 {SEARCH TABLE t2 USING INDEX t2d (d=?)} - 0 1 1 {SEARCH TABLE t2 USING COVERING INDEX t2f (f=?)} - } + } [string map {"\n " \n} { + QUERY PLAN + |--SEARCH t1 USING INTEGER PRIMARY KEY (rowid=?) + `--MULTI-INDEX OR + |--INDEX 1 + | `--SEARCH t2 USING INDEX t2d (d=?) LEFT-JOIN + `--INDEX 2 + `--SEARCH t2 USING COVERING INDEX t2f (f=?) LEFT-JOIN + }] } # Make sure that INDEXED BY and multi-index OR clauses play well with @@ -420,7 +426,7 @@ do_test where9-4.5 { AND (c=31031 OR d IS NULL) ORDER BY +a } -} {1 {no query solution}} +} {0 {92 93 97}} do_test where9-4.6 { count_steps { SELECT a FROM t1 NOT INDEXED @@ -436,7 +442,7 @@ do_test where9-4.7 { AND (c=31031 OR d IS NULL) ORDER BY +a } -} {1 {no query solution}} +} {0 {92 93 97}} do_test where9-4.8 { catchsql { SELECT a FROM t1 INDEXED BY t1d @@ -444,37 +450,35 @@ do_test where9-4.8 { AND (c=31031 OR d IS NULL) ORDER BY +a } -} {1 {no query solution}} +} {0 {92 93 97}} -ifcapable explain { - # The (c=31031 OR d IS NULL) clause is preferred over b>1000 because - # the former is an equality test which is expected to return fewer rows. - # - do_execsql_test where9-5.1 { - EXPLAIN QUERY PLAN SELECT a FROM t1 WHERE b>1000 AND (c=31031 OR d IS NULL) - } { - 0 0 0 {SEARCH TABLE t1 USING INDEX t1c (c=?)} - 0 0 0 {SEARCH TABLE t1 USING INDEX t1d (d=?)} - } - - # In contrast, b=1000 is preferred over any OR-clause. - # - do_execsql_test where9-5.2 { - EXPLAIN QUERY PLAN SELECT a FROM t1 WHERE b=1000 AND (c=31031 OR d IS NULL) - } { - 0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b=?)} - } - - # Likewise, inequalities in an AND are preferred over inequalities in - # an OR. - # - do_execsql_test where9-5.3 { - EXPLAIN QUERY PLAN SELECT a FROM t1 WHERE b>1000 AND (c>=31031 OR d IS NULL) - } { - 0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b>?)} - } +# The (c=31031 OR d IS NULL) clause is preferred over b>1000 because +# the former is an equality test which is expected to return fewer rows. +# +do_eqp_test where9-5.1 { + SELECT a FROM t1 WHERE b>1000 AND (c=31031 OR d IS NULL) +} { + QUERY PLAN + `--MULTI-INDEX OR + |--INDEX 1 + | `--SEARCH t1 USING INDEX t1c (c=?) + `--INDEX 2 + `--SEARCH t1 USING INDEX t1d (d=?) } +# In contrast, b=1000 is preferred over any OR-clause. +# +do_eqp_test where9-5.2 { + SELECT a FROM t1 WHERE b=1000 AND (c=31031 OR d IS NULL) +} {SEARCH t1 USING INDEX t1b (b=?)} + +# Likewise, inequalities in an AND are preferred over inequalities in +# an OR. +# +do_eqp_test where9-5.3 { + SELECT a FROM t1 WHERE b>1000 AND (c>=31031 OR d IS NULL) +} {SEARCH t1 USING INDEX t1b (b>?)} + ############################################################################ # Make sure OR-clauses work correctly on UPDATE and DELETE statements. @@ -772,7 +776,7 @@ do_test where9-6.8.1 { OR (b NOT NULL AND c IS NULL AND d NOT NULL) OR (b NOT NULL AND c NOT NULL AND d IS NULL) } -} {1 {no query solution}} +} {0 {}} do_test where9-6.8.2 { catchsql { UPDATE t1 INDEXED BY t1b SET a=a+100 @@ -780,10 +784,10 @@ do_test where9-6.8.2 { OR (b NOT NULL AND c IS NULL AND d NOT NULL) OR (b NOT NULL AND c NOT NULL AND d IS NULL) } -} {1 {no query solution}} +} {0 {}} set solution_possible 0 -ifcapable stat4||stat3 { +ifcapable stat4 { if {[permutation] != "no_optimization"} { set solution_possible 1 } } if $solution_possible { @@ -814,7 +818,7 @@ if $solution_possible { OR (b NOT NULL AND c IS NULL AND d NOT NULL) OR (b NOT NULL AND c NOT NULL AND d IS NULL) } - } {1 {no query solution}} + } {0 {}} do_test where9-6.8.4 { catchsql { DELETE FROM t1 INDEXED BY t1b @@ -822,7 +826,7 @@ if $solution_possible { OR (b NOT NULL AND c IS NULL AND d NOT NULL) OR (b NOT NULL AND c NOT NULL AND d IS NULL) } - } {1 {no query solution}} + } {0 {}} } ############################################################################ # Test cases where terms inside an OR series are combined with AND terms @@ -856,11 +860,6 @@ do_test where9-7.0 { INSERT INTO t6 SELECT * FROM t5; ANALYZE t5; } - ifcapable stat3 { - sqlite3 db2 test.db - db2 eval { DROP TABLE IF EXISTS sqlite_stat3 } - db2 close - } } {} do_test where9-7.1.1 { count_steps { @@ -983,6 +982,23 @@ do_test where9-10.2 { } } {1 {} 1} - +# dbsqlfuzz 9df1d53c24c4c96af0dae15ee764897af415ac76 +# The MULTI-INDEX OR processing evaluates the same WHERE-clause sub-expression +# twice. But if that sub-expression contains a UNION ALL SELECT statement +# subject to query flattening, the sub-expression might be transformed in a +# way that it can only be code-generated once. An assert() will fail on +# the second attempt to generate code from the same sub-expression. +# The solution is to make a copy of sub-expressions used by MULTI-INDEX OR +# +reset_db +do_execsql_test where9-11.1 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT); + CREATE TABLE t2_a(k INTEGER PRIMARY KEY, v TEXT); + CREATE TABLE t2_b(k INTEGER PRIMARY KEY, v TEXT); + CREATE VIEW t2 AS SELECT * FROM t2_a UNION ALL SELECT * FROM t2_b; + SELECT 1 FROM t1 JOIN t1 USING(a) + WHERE (a=1) + OR (a=2 AND (SELECT 4 FROM t2,(SELECT 5 FROM t1 ORDER BY a) WHERE a)); +} {} finish_test diff --git a/test/whereA.test b/test/whereA.test index 78826b111c..dff24d8c83 100644 --- a/test/whereA.test +++ b/test/whereA.test @@ -158,5 +158,32 @@ do_test whereA-4.6 { } } {2 1 1} +# Ticket https://sqlite.org/src/tktview/cb91bf4290c211 2017-08-01 +# Assertion fault following PRAGMA reverse_unordered_selects=ON. +# +do_execsql_test whereA-5.1 { + PRAGMA reverse_unordered_selects=on; + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a,b); + INSERT INTO t1 VALUES(1,2); + CREATE INDEX t1b ON t1(b); + SELECT a FROM t1 WHERE b=-99 OR b>1; +} {1} + +# 2020-10-02 OSSFuzz find for an issue introduced by a check-in +# on the previous day. +# +reset_db +do_execsql_test whereA-6.1 { + CREATE TABLE t1(a, b); + CREATE INDEX t1aa ON t1(a,a); + INSERT INTO t1 VALUES(1,2); + ANALYZE; + UPDATE sqlite_stat1 SET stat='27 3 3' WHERE idx='t1aa'; + ANALYZE sqlite_schema; + PRAGMA reverse_unordered_selects (1) ; + SELECT a FROM t1 WHERE a=1 OR a=2; +} {1} + finish_test diff --git a/test/whereD.test b/test/whereD.test index de0f9618d2..e727f47e20 100644 --- a/test/whereD.test +++ b/test/whereD.test @@ -337,4 +337,97 @@ do_searchcount_test 6.6.4 { SELECT c FROM x1 WHERE b=6 OR c=11 OR a=1 } {7 11 3 search 7} +# 2020-02-22 ticket aa4378693018aa99 +# In the OP_Column opcode, if a cursor is marked with OP_NullRow +# (because it is the right table of a LEFT JOIN that does not match) +# then do not substitute index cursors, as the index cursors do not +# have the VdbeCursor.nullRow flag set. +# +do_execsql_test 6.7 { + DROP TABLE IF EXISTS t1; + DROP TABLE IF EXISTS t2; + CREATE TABLE t1(a UNIQUE, b UNIQUE); + INSERT INTO t1(a,b) VALUES(null,2); + CREATE VIEW t2 AS SELECT * FROM t1 WHERE b<10 OR a<7 ORDER BY b; + SELECT t1.* FROM t1 LEFT JOIN t2 ON abs(t1.a)=abs(t2.b); +} {{} 2} + + +#------------------------------------------------------------------------- +# +do_execsql_test 7.0 { + CREATE TABLE y1(a, b); + CREATE TABLE y2(x, y); + CREATE INDEX y2xy ON y2(x, y); + INSERT INTO y1 VALUES(1, 1); + INSERT INTO y2 VALUES(3, 3); +} + +do_execsql_test 7.1 { + SELECT * FROM y1 LEFT JOIN y2 ON ((x=1 AND y=b) OR (x=2 AND y=b)) +} {1 1 {} {}} + +do_execsql_test 7.3 { + CREATE TABLE foo (Id INTEGER PRIMARY KEY, fa INTEGER, fb INTEGER); + CREATE TABLE bar (Id INTEGER PRIMARY KEY, ba INTEGER, bb INTEGER); + + INSERT INTO foo VALUES(1, 1, 1); + INSERT INTO foo VALUES(2, 1, 2); + INSERT INTO foo VALUES(3, 1, 3); + INSERT INTO foo VALUES(4, 1, 4); + INSERT INTO foo VALUES(5, 1, 5); + INSERT INTO foo VALUES(6, 1, 6); + INSERT INTO foo VALUES(7, 1, 7); + INSERT INTO foo VALUES(8, 1, 8); + INSERT INTO foo VALUES(9, 1, 9); + + INSERT INTO bar VALUES(NULL, 1, 1); + INSERT INTO bar VALUES(NULL, 2, 2); + INSERT INTO bar VALUES(NULL, 3, 3); + INSERT INTO bar VALUES(NULL, 1, 4); + INSERT INTO bar VALUES(NULL, 2, 5); + INSERT INTO bar VALUES(NULL, 3, 6); + INSERT INTO bar VALUES(NULL, 1, 7); + INSERT INTO bar VALUES(NULL, 2, 8); + INSERT INTO bar VALUES(NULL, 3, 9); +} + +do_execsql_test 7.4 { + SELECT + bar.Id, bar.ba, bar.bb, foo.fb + FROM foo LEFT JOIN bar + ON (bar.ba = 1 AND bar.bb = foo.fb) + OR (bar.ba = 5 AND bar.bb = foo.fb); +} { + 1 1 1 1 + {} {} {} 2 + {} {} {} 3 + 4 1 4 4 + {} {} {} 5 + {} {} {} 6 + 7 1 7 7 + {} {} {} 8 + {} {} {} 9 +} + +do_execsql_test 7.5 { + CREATE INDEX idx_bar ON bar(ba, bb); + SELECT + bar.Id, bar.ba, bar.bb, foo.fb + FROM foo LEFT JOIN bar + ON (bar.ba = 1 AND bar.bb = foo.fb) + OR (bar.ba = 5 AND bar.bb = foo.fb); +} { + 1 1 1 1 + {} {} {} 2 + {} {} {} 3 + 4 1 4 4 + {} {} {} 5 + {} {} {} 6 + 7 1 7 7 + {} {} {} 8 + {} {} {} 9 +} + + finish_test diff --git a/test/whereE.test b/test/whereE.test index a6b8f481b2..cd9f81d531 100644 --- a/test/whereE.test +++ b/test/whereE.test @@ -18,6 +18,12 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl set ::testprefix whereE +# If SQLITE_OMIT_ALTERTABLE is defined, omit this file. +ifcapable !altertable { + finish_test + return +} + do_execsql_test 1.1 { CREATE TABLE t1(a,b); INSERT INTO t1 VALUES(1,10), (2,20), (3,30), (2,22), (3, 33); @@ -47,16 +53,16 @@ do_execsql_test 1.1 { CREATE UNIQUE INDEX t2zx ON t2(z,x); EXPLAIN QUERY PLAN SELECT x FROM t1, t2 WHERE a=z AND c=x; -} {/.*SCAN TABLE t1.*SEARCH TABLE t2.*/} +} {/.*SCAN t1.*SEARCH t2.*/} do_execsql_test 1.2 { EXPLAIN QUERY PLAN SELECT x FROM t2, t1 WHERE a=z AND c=x; -} {/.*SCAN TABLE t1.*SEARCH TABLE t2.*/} +} {/.*SCAN t1.*SEARCH t2.*/} do_execsql_test 1.3 { ANALYZE; EXPLAIN QUERY PLAN SELECT x FROM t1, t2 WHERE a=z AND c=x; -} {/.*SCAN TABLE t1.*SEARCH TABLE t2.*/} +} {/.*SCAN t1.*SEARCH t2.*/} do_execsql_test 1.4 { EXPLAIN QUERY PLAN SELECT x FROM t2, t1 WHERE a=z AND c=x; -} {/.*SCAN TABLE t1.*SEARCH TABLE t2.*/} +} {/.*SCAN t1.*SEARCH t2.*/} finish_test diff --git a/test/whereF.test b/test/whereF.test index b9580bb196..ac7fd807b8 100644 --- a/test/whereF.test +++ b/test/whereF.test @@ -63,7 +63,7 @@ foreach {tn sql} { } { do_test 1.$tn { db eval "EXPLAIN QUERY PLAN $sql" - } {/.*SCAN TABLE t2\y.*SEARCH TABLE t1\y.*/} + } {/.*SCAN t2\y.*SEARCH t1\y.*/} } do_execsql_test 2.0 { @@ -84,7 +84,7 @@ foreach {tn sql} { } { do_test 2.$tn { db eval "EXPLAIN QUERY PLAN $sql" - } {/.*SCAN TABLE t2\y.*SEARCH TABLE t1\y.*/} + } {/.*SCAN t2\y.*SEARCH t1\y.*/} } do_execsql_test 3.0 { @@ -109,7 +109,7 @@ foreach {tn sql} { } { do_test 3.$tn { db eval "EXPLAIN QUERY PLAN $sql" - } {/.*SCAN TABLE t2\y.*SEARCH TABLE t1\y.*/} + } {/.*SCAN t2\y.*SEARCH t1\y.*/} } do_execsql_test 4.0 { @@ -119,4 +119,194 @@ do_execsql_test 4.0 { EXPLAIN QUERY PLAN SELECT rowid FROM t4 WHERE a=? AND b=?; } {/a=. AND b=./} +#------------------------------------------------------------------------- +# Test the following case: +# +# ... FROM t1, t2 WHERE ( +# t2.rowid = +t1.rowid OR (t2.f2 = t1.f1 AND t1.f1!=-1) +# ) +# +# where there is an index on t2(f2). The planner should use "t1" as the +# outer loop. The inner loop, on "t2", is an OR optimization. One pass +# for: +# +# t2.rowid = $1 +# +# and another for: +# +# t2.f2=$1 AND $1!=-1 +# +# the test is to ensure that on the second pass, the ($1!=-1) condition +# is tested before any seek operations are performed - i.e. outside of +# the loop through the f2=$1 range of the t2(f2) index. +# +reset_db +do_execsql_test 5.0 { + CREATE TABLE t1(f1); + CREATE TABLE t2(f2); + CREATE INDEX t2f ON t2(f2); + + INSERT INTO t1 VALUES(-1); + INSERT INTO t1 VALUES(-1); + INSERT INTO t1 VALUES(-1); + INSERT INTO t1 VALUES(-1); + + WITH w(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM w WHERE i<1000 + ) + INSERT INTO t2 SELECT -1 FROM w; +} + +do_execsql_test 5.1 { + SELECT count(*) FROM t1, t2 WHERE t2.rowid = +t1.rowid +} {4} +do_test 5.2 { expr [db status vmstep]<200 } 1 + +do_execsql_test 5.3 { + SELECT count(*) FROM t1, t2 WHERE ( + t2.rowid = +t1.rowid OR t2.f2 = t1.f1 + ) +} {4000} +do_test 5.4 { expr [db status vmstep]>1000 } 1 + +do_execsql_test 5.5 { + SELECT count(*) FROM t1, t2 WHERE ( + t2.rowid = +t1.rowid OR (t2.f2 = t1.f1 AND t1.f1!=-1) + ) +} {4} +do_test 5.6 { expr [db status vmstep]<200 } 1 + +# 2017-09-04 ticket b899b6042f97f52d +# Segfault on correlated subquery... +# +ifcapable json1&&vtab { + do_execsql_test 6.1 { + CREATE TABLE t6(x); + SELECT * FROM t6 WHERE 1 IN (SELECT value FROM json_each(x)); + } {} + + do_execsql_test 6.2 { + DROP TABLE t6; + CREATE TABLE t6(a,b,c); + INSERT INTO t6 VALUES + (0,null,'{"a":0,"b":[3,4,5],"c":{"x":4.5,"y":7.8}}'), + (1,null,'{"a":1,"b":[3,4,5],"c":{"x":4.5,"y":7.8}}'), + (2,null,'{"a":9,"b":[3,4,5],"c":{"x":4.5,"y":7.8}}'); + SELECT * FROM t6 + WHERE (EXISTS (SELECT 1 FROM json_each(t6.c) AS x WHERE x.value=1)); + } {1 {} {{"a":1,"b":[3,4,5],"c":{"x":4.5,"y":7.8}}}} + + # Another test case derived from a posting by Wout Mertens on the + # sqlite-users mailing list on 2017-10-04. + do_execsql_test 6.3 { + DROP TABLE IF EXISTS t; + CREATE TABLE t(json JSON); + SELECT * FROM t + WHERE(EXISTS(SELECT 1 FROM json_each(t.json,"$.foo") j + WHERE j.value = 'meep')); + } {} + do_execsql_test 6.4 { + INSERT INTO t VALUES('{"xyzzy":null}'); + INSERT INTO t VALUES('{"foo":"meep","other":12345}'); + INSERT INTO t VALUES('{"foo":"bingo","alt":5.25}'); + SELECT * FROM t + WHERE(EXISTS(SELECT 1 FROM json_each(t.json,"$.foo") j + WHERE j.value = 'meep')); + } {{{"foo":"meep","other":12345}}} +} + +# 2018-01-27 +# Ticket https://sqlite.org/src/tktview/ec32177c99ccac2b180fd3ea2083 +# Incorrect result when using the new OR clause factoring optimization +# +# This is the original test case as reported on the sqlite-users mailing +# list +# +do_execsql_test 7.1 { + DROP TABLE IF EXISTS cd; + CREATE TABLE cd ( cdid INTEGER PRIMARY KEY NOT NULL, genreid integer ); + CREATE INDEX cd_idx_genreid ON cd (genreid); + INSERT INTO cd ( cdid, genreid ) VALUES + ( 1, 1 ), + ( 2, NULL ), + ( 3, NULL ), + ( 4, NULL ), + ( 5, NULL ); + + SELECT cdid + FROM cd me + WHERE 2 > ( + SELECT COUNT( * ) + FROM cd rownum__emulation + WHERE + ( + me.genreid IS NOT NULL + AND + rownum__emulation.genreid IS NULL + ) + OR + ( + me.genreid IS NOT NULL + AND + rownum__emulation.genreid IS NOT NULL + AND + rownum__emulation.genreid < me.genreid + ) + OR + ( + ( me.genreid = rownum__emulation.genreid OR ( me.genreid IS NULL + AND rownum__emulation.genreid IS NULL ) ) + AND + rownum__emulation.cdid > me.cdid + ) + ); +} {4 5} + +# Simplified test cases from the ticket +# +do_execsql_test 7.2 { + DROP TABLE IF EXISTS t1; + DROP TABLE IF EXISTS t2; + CREATE TABLE t1(a INTEGER PRIMARY KEY, b); + INSERT INTO t1(a,b) VALUES(1,1); + CREATE TABLE t2(aa INTEGER PRIMARY KEY, bb); + INSERT INTO t2(aa,bb) VALUES(1,1),(2,NULL),(3,NULL); + SELECT ( + SELECT COUNT(*) FROM t2 + WHERE ( t1.b IS NOT NULL AND t2.bb IS NULL ) + OR ( t2.bb < t1.b ) + OR ( t1.b IS t2.bb AND t2.aa > t1.a ) + ) + FROM t1; +} {2} + +# The fix for ticket ec32177c99ccac2b180fd3ea2083 only makes a difference +# in the output when there is a TERM_VNULL entry in the WhereClause array. +# And TERM_VNULL entries are only generated when compiling with +# SQLITE_ENABLE_STAT4. Nevertheless, it is correct that TERM_VIRTUAL terms +# should not participate in the factoring optimization. In all cases other +# than TERM_VNULL, participation is harmless, but it does consume a few +# extra CPU cycles. +# +# The following test verifies that the TERM_VIRTUAL terms resulting from +# a GLOB operator do not appear anywhere in the generated code. This +# confirms that the problem is fixed, even on builds that omit STAT4. +# +do_execsql_test 7.3 { + DROP TABLE IF EXISTS t1; + DROP TABLE IF EXISTS t2; + CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT); + INSERT INTO t1(a,b) VALUES(1,'abcxyz'); + CREATE TABLE t2(aa INTEGER PRIMARY KEY, bb TEXT); + INSERT INTO t2(aa,bb) VALUES(1,'abc'),(2,'wxyz'),(3,'xyz'); + CREATE INDEX t2bb ON t2(bb); + EXPLAIN SELECT ( + SELECT COUNT(*) FROM t2 + WHERE ( t1.b GLOB 'a*z' AND t2.bb='xyz' ) + OR ( t2.bb = t1.b ) + OR ( t2.aa = t1.a ) + ) + FROM t1; +} {~/ (Lt|Ge) /} + finish_test diff --git a/test/whereG.test b/test/whereG.test index 110ed5dbd4..6ee8634817 100644 --- a/test/whereG.test +++ b/test/whereG.test @@ -66,7 +66,7 @@ do_eqp_test whereG-1.1 { WHERE unlikely(cname LIKE '%bach%') AND composer.cid=track.cid AND album.aid=track.aid; -} {/.*composer.*track.*album.*/} +} {composer*track*album} do_execsql_test whereG-1.2 { SELECT DISTINCT aname FROM album, composer, track @@ -156,16 +156,16 @@ do_execsql_test whereG-3.0 { } {} do_eqp_test whereG-3.1 { SELECT * FROM a, b WHERE b1=a1 AND a2=5; -} {/.*SCAN TABLE a.*SEARCH TABLE b USING INDEX .*b_1 .b1=..*/} +} {/.*SCAN a.*SEARCH b USING INDEX .*b_1 .b1=..*/} do_eqp_test whereG-3.2 { SELECT * FROM a, b WHERE a1=b1 AND a2=5; -} {/.*SCAN TABLE a.*SEARCH TABLE b USING INDEX .*b_1 .b1=..*/} +} {/.*SCAN a.*SEARCH b USING INDEX .*b_1 .b1=..*/} do_eqp_test whereG-3.3 { SELECT * FROM a, b WHERE a2=5 AND b1=a1; -} {/.*SCAN TABLE a.*SEARCH TABLE b USING INDEX .*b_1 .b1=..*/} +} {/.*SCAN a.*SEARCH b USING INDEX .*b_1 .b1=..*/} do_eqp_test whereG-3.4 { SELECT * FROM a, b WHERE a2=5 AND a1=b1; -} {/.*SCAN TABLE a.*SEARCH TABLE b USING INDEX .*b_1 .b1=..*/} +} {/.*SCAN a.*SEARCH b USING INDEX .*b_1 .b1=..*/} # Ticket [1e64dd782a126f48d78c43a664844a41d0e6334e]: # Incorrect result in a nested GROUP BY/DISTINCT due to the use of an OP_SCopy @@ -195,13 +195,13 @@ do_execsql_test 5.1 { } do_eqp_test 5.1.2 { SELECT * FROM t1 WHERE a>? -} {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (a>?)}} +} {SEARCH t1 USING INDEX i1 (a>?)} do_eqp_test 5.1.3 { SELECT * FROM t1 WHERE likelihood(a>?, 0.9) -} {0 0 0 {SCAN TABLE t1}} +} {SCAN t1} do_eqp_test 5.1.4 { SELECT * FROM t1 WHERE likely(a>?) -} {0 0 0 {SCAN TABLE t1}} +} {SCAN t1} do_test 5.2 { for {set i 0} {$i < 100} {incr i} { @@ -212,26 +212,32 @@ do_test 5.2 { } {} do_eqp_test 5.2.2 { SELECT * FROM t1 WHERE likelihood(b>?, 0.01) -} {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (ANY(a) AND b>?)}} +} {SEARCH t1 USING INDEX i1 (ANY(a) AND b>?)} do_eqp_test 5.2.3 { SELECT * FROM t1 WHERE likelihood(b>?, 0.9) -} {0 0 0 {SCAN TABLE t1}} +} {SCAN t1} do_eqp_test 5.2.4 { SELECT * FROM t1 WHERE likely(b>?) -} {0 0 0 {SCAN TABLE t1}} +} {SCAN t1} -do_eqp_test 5.3.1 { - SELECT * FROM t1 WHERE a=? -} {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (a=?)}} +ifcapable stat4 { + do_eqp_test 5.3.1.stat4 { + SELECT * FROM t1 WHERE a=? + } {SCAN t1} +} else { + do_eqp_test 5.3.1 { + SELECT * FROM t1 WHERE a=? + } {SEARCH t1 USING INDEX i1} +} do_eqp_test 5.3.2 { SELECT * FROM t1 WHERE likelihood(a=?, 0.9) -} {0 0 0 {SCAN TABLE t1}} +} {SCAN t1} do_eqp_test 5.3.3 { SELECT * FROM t1 WHERE likely(a=?) -} {0 0 0 {SCAN TABLE t1}} +} {SCAN t1} # 2015-06-18 -# Ticket [https://www.sqlite.org/see/tktview/472f0742a1868fb58862bc588ed70] +# Ticket [https://sqlite.org/see/tktview/472f0742a1868fb58862bc588ed70] # do_execsql_test 6.0 { DROP TABLE IF EXISTS t1; @@ -266,5 +272,131 @@ do_execsql_test 7.3 { SELECT coalesce(a,a), x FROM t1, t2 ORDER BY 1, 2; } {1 3 1 4 9 3 9 4} +# 2019-08-22 +# Ticket https://sqlite.org/src/info/7e07a3dbf5a8cd26 +# +do_execsql_test 8.1 { + DROP TABLE IF EXISTS t0; + CREATE TABLE t0 (c0); + INSERT INTO t0(c0) VALUES ('a'); + SELECT LIKELY(t0.rowid) <= '0' FROM t0; +} {1} +do_execsql_test 8.2 { + SELECT * FROM t0 WHERE LIKELY(t0.rowid) <= '0'; +} {a} +do_execsql_test 8.3 { + SELECT (t0.rowid) <= '0' FROM t0; +} {0} +do_execsql_test 8.4 { + SELECT * FROM t0 WHERE (t0.rowid) <= '0'; +} {} +do_execsql_test 8.5 { + SELECT unlikely(t0.rowid) <= '0', likelihood(t0.rowid,0.5) <= '0' FROM t0; +} {1 1} +do_execsql_test 8.6 { + SELECT * FROM t0 WHERE unlikely(t0.rowid) <= '0'; +} {a} +do_execsql_test 8.7 { + SELECT * FROM t0 WHERE likelihood(t0.rowid, 0.5) <= '0'; +} {a} +do_execsql_test 8.8 { + SELECT unlikely(t0.rowid <= '0'), + likely(t0.rowid <= '0'), + likelihood(t0.rowid <= '0',0.5) + FROM t0; +} {0 0 0} +do_execsql_test 8.9 { + SELECT * FROM t0 WHERE unlikely(t0.rowid <= '0'); +} {} +do_execsql_test 8.10 { + SELECT * FROM t0 WHERE likelihood(t0.rowid <= '0', 0.5); +} {} +# Forum https://sqlite.org/forum/forumpost/45ec3d9788 +reset_db +do_execsql_test 8.11 { + CREATE TABLE t1(c0 INT); + INSERT INTO t1(c0) VALUES (NULL); + CREATE INDEX i46 ON t1(CAST( (c0 IS TRUE) AS TEXT)); + CREATE VIEW v0(c2) AS SELECT CAST( (c0 IS TRUE) AS TEXT ) FROM t1; +} +do_execsql_test 8.12 { + SELECT quote(c0), quote(c2) FROM t1, v0 WHERE (0 < LIKELY(v0.c2)); +} {NULL '0'} +do_execsql_test 8.13 { + SELECT quote(c0), quote(c2) FROM t1, v0 WHERE (0 < LIKELY(v0.c2)) IS TRUE; +} {NULL '0'} + +# 2019-12-31: assertion fault discovered by Yongheng's fuzzer. +# Harmless memIsValid() due to the code generators failure to +# release the registers used by OP_ResultRow. +# +do_execsql_test 9.10 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a, b FLOAT); + INSERT INTO t1(a) VALUES(''),(NULL),('X'),(NULL); + SELECT coalesce(max(quote(a)),10) FROM t1 GROUP BY a; +} {NULL '' 'X'} + +# 2020-06-14: assert() changed back into testcase() +# ticket 9fb26d37cefaba40 +# +reset_db +do_execsql_test 10.1 { + CREATE TABLE a(b TEXT); INSERT INTO a VALUES(0),(4),(9); + CREATE TABLE c(d NUM); + CREATE VIEW f(g, h) AS SELECT b, 0 FROM a UNION SELECT d, d FROM c; + SELECT g = g FROM f GROUP BY h; +} {1} + +reset_db +do_execsql_test 11.0 { + CREATE TABLE t1(x PRIMARY KEY, y); + INSERT INTO t1 VALUES('AAA', 'BBB'); + + CREATE TABLE t2(z); + INSERT INTO t2 VALUES('t2'); + + CREATE TABLE t3(x PRIMARY KEY, y); + INSERT INTO t3 VALUES('AAA', 'AAA'); +} + +do_execsql_test 11.1.1 { + SELECT * FROM t1 JOIN t2 ON unlikely(x=y) AND y='AAA' +} +do_execsql_test 11.1.2 { + SELECT * FROM t1 JOIN t2 ON likely(x=y) AND y='AAA' +} +do_execsql_test 11.1.3 { + SELECT * FROM t1 JOIN t2 ON x=y AND y='AAA' +} + +do_execsql_test 11.2.1 { + SELECT * FROM t3 JOIN t2 ON unlikely(x=y) AND y='AAA' +} {AAA AAA t2} +do_execsql_test 11.2.2 { + SELECT * FROM t3 JOIN t2 ON likely(x=y) AND y='AAA' +} {AAA AAA t2} +do_execsql_test 11.2.3 { + SELECT * FROM t3 JOIN t2 ON x=y AND y='AAA' +} {AAA AAA t2} + +# 2021-06-14 forum https://sqlite.org/forum/forumpost/3b940c437a +# Affinity problem when a likely() function is used as a column in +# an index. +# +reset_db +do_execsql_test 12.0 { + CREATE TABLE t1(a REAL); + INSERT INTO t1(a) VALUES(123); + CREATE INDEX t1x1 ON t1(likely(a)); + SELECT typeof(likely(a)) FROM t1 NOT INDEXED; + SELECT typeof(likely(a)) FROM t1 INDEXED BY t1x1; +} {real real} +do_execsql_test 12.1 { + CREATE INDEX t1x2 ON t1(abs(a)); + SELECT typeof(abs(a)) FROM t1 NOT INDEXED; + SELECT typeof(abs(a)) FROM t1 INDEXED BY t1x2; +} {real real} + finish_test diff --git a/test/whereI.test b/test/whereI.test index 29b08549be..38e0148844 100644 --- a/test/whereI.test +++ b/test/whereI.test @@ -29,8 +29,12 @@ do_execsql_test 1.0 { do_eqp_test 1.1 { SELECT a FROM t1 WHERE b='b' OR c='x' } { - 0 0 0 {SEARCH TABLE t1 USING INDEX i1 (b=?)} - 0 0 0 {SEARCH TABLE t1 USING INDEX i2 (c=?)} + QUERY PLAN + `--MULTI-INDEX OR + |--INDEX 1 + | `--SEARCH t1 USING INDEX i1 (b=?) + `--INDEX 2 + `--SEARCH t1 USING INDEX i2 (c=?) } do_execsql_test 1.2 { @@ -57,8 +61,12 @@ do_execsql_test 2.0 { do_eqp_test 2.1 { SELECT a FROM t2 WHERE b='b' OR c='x' } { - 0 0 0 {SEARCH TABLE t2 USING INDEX i3 (b=?)} - 0 0 0 {SEARCH TABLE t2 USING INDEX i4 (c=?)} + QUERY PLAN + `--MULTI-INDEX OR + |--INDEX 1 + | `--SEARCH t2 USING INDEX i3 (b=?) + `--INDEX 2 + `--SEARCH t2 USING INDEX i4 (c=?) } do_execsql_test 2.2 { diff --git a/test/whereJ.test b/test/whereJ.test index 48924d0fcf..c31ab8c370 100644 --- a/test/whereJ.test +++ b/test/whereJ.test @@ -402,9 +402,7 @@ do_eqp_test 3.4 { a = 4 AND b BETWEEN 20 AND 80 -- Matches 80 rows AND c BETWEEN 150 AND 160 -- Matches 10 rows -} { - 0 0 0 {SEARCH TABLE t1 USING INDEX idx_c (c>? AND c<?)} -} +} {SEARCH t1 USING INDEX idx_c (c>? AND c<?)} # This one should use index "idx_ab". do_eqp_test 3.5 { @@ -412,9 +410,7 @@ do_eqp_test 3.5 { a = 5 AND b BETWEEN 20 AND 80 -- Matches 1 row AND c BETWEEN 150 AND 160 -- Matches 10 rows -} { - 0 0 0 {SEARCH TABLE t1 USING INDEX idx_ab (a=? AND b>? AND b<?)} -} +} {SEARCH t1 USING INDEX idx_ab (a=? AND b>? AND b<?)} ########################################################################################### @@ -637,7 +633,7 @@ do_execsql_test 4.2 { AND px.cx_id = cx.cx_id AND px.px_tid = 0 AND px.le_id = le.le_id; -} {/.*SCAN TABLE cx.*SEARCH TABLE px.*SEARCH TABLE le.*/} +} {/.*SCAN cx.*SEARCH px.*SEARCH le.*/} # The following test is derived from a performance problem reported from diff --git a/test/whereK.test b/test/whereK.test index 13c86508f9..060d470ff9 100644 --- a/test/whereK.test +++ b/test/whereK.test @@ -33,7 +33,7 @@ do_execsql_test 1.1 { do_execsql_test 1.1eqp { EXPLAIN QUERY PLAN SELECT a FROM t1 WHERE b>9 OR b=9 ORDER BY +a; -} {/SEARCH TABLE t1 USING INDEX t1bc/} +} {/SEARCH t1 USING INDEX t1bc/} do_execsql_test 1.2 { SELECT a FROM t1 WHERE b>8 OR (b=8 AND c>7) ORDER BY +a; @@ -41,7 +41,7 @@ do_execsql_test 1.2 { do_execsql_test 1.2eqp { EXPLAIN QUERY PLAN SELECT a FROM t1 WHERE b>8 OR (b=8 AND c>7) ORDER BY +a; -} {/SEARCH TABLE t1 USING INDEX t1bc/} +} {/SEARCH t1 USING INDEX t1bc/} do_execsql_test 1.3 { SELECT a FROM t1 WHERE (b=8 AND c>7) OR b>8 ORDER BY +a; @@ -49,7 +49,7 @@ do_execsql_test 1.3 { do_execsql_test 1.3eqp { EXPLAIN QUERY PLAN SELECT a FROM t1 WHERE (b=8 AND c>7) OR b>8 ORDER BY +a; -} {/SEARCH TABLE t1 USING INDEX t1bc/} +} {/SEARCH t1 USING INDEX t1bc/} do_execsql_test 1.4 { SELECT a FROM t1 WHERE (b=8 AND c>7) OR 8<b ORDER BY +a; @@ -57,7 +57,7 @@ do_execsql_test 1.4 { do_execsql_test 1.4eqp { EXPLAIN QUERY PLAN SELECT a FROM t1 WHERE (b=8 AND c>7) OR 8<b ORDER BY +a; -} {/SEARCH TABLE t1 USING INDEX t1bc/} +} {/SEARCH t1 USING INDEX t1bc/} do_execsql_test 1.5 { SELECT a FROM t1 WHERE (b=8 AND c>7) OR (b>8 AND c NOT IN (4,5,6)) @@ -67,6 +67,6 @@ do_execsql_test 1.5eqp { EXPLAIN QUERY PLAN SELECT a FROM t1 WHERE (b=8 AND c>7) OR (b>8 AND c NOT IN (4,5,6)) ORDER BY +a; -} {/SEARCH TABLE t1 USING INDEX t1bc/} +} {/SEARCH t1 USING INDEX t1bc/} finish_test diff --git a/test/whereL.test b/test/whereL.test new file mode 100644 index 0000000000..ffbae02b8d --- /dev/null +++ b/test/whereL.test @@ -0,0 +1,297 @@ +# 2018-07-26 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is testing the WHERE-clause constant propagation +# optimization. +# +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set ::testprefix whereL + +do_execsql_test 100 { + CREATE TABLE t1(a INT PRIMARY KEY, b, c, d, e); + CREATE TABLE t2(a INT PRIMARY KEY, f, g, h, i); + CREATE TABLE t3(a INT PRIMARY KEY, j, k, l, m); + CREATE VIEW v4 AS SELECT * FROM t2 UNION ALL SELECT * FROM t3; +} +do_eqp_test 110 { + SELECT * FROM t1, v4 WHERE t1.a=?1 AND v4.a=t1.a; +} { + QUERY PLAN + `--COMPOUND QUERY + |--LEFT-MOST SUBQUERY + | |--SEARCH t1 USING INDEX sqlite_autoindex_t1_1 (a=?) + | `--SEARCH t2 USING INDEX sqlite_autoindex_t2_1 (a=?) + `--UNION ALL + |--SEARCH t1 USING INDEX sqlite_autoindex_t1_1 (a=?) + `--SEARCH t3 USING INDEX sqlite_autoindex_t3_1 (a=?) +} + +# The scan of the t1 table goes first since that enables the ORDER BY +# sort to be omitted. This would not be possible without constant +# propagation because without it the t1 table would depend on t3. +# +do_eqp_test 120 { + SELECT * FROM t1, t2, t3 + WHERE t1.a=t2.a AND t2.a=t3.j AND t3.j=5 + ORDER BY t1.a; +} { + QUERY PLAN + |--SEARCH t1 USING INDEX sqlite_autoindex_t1_1 (a=?) + |--SEARCH t2 USING INDEX sqlite_autoindex_t2_1 (a=?) + `--SCAN t3 +} +do_eqp_test 121 { + SELECT * FROM t1, t2, t3 + WHERE t1.a=t2.a AND t2.a=t3.j AND t3.j=abs(5) + ORDER BY t1.a; +} { + QUERY PLAN + |--SEARCH t1 USING INDEX sqlite_autoindex_t1_1 (a=?) + |--SEARCH t2 USING INDEX sqlite_autoindex_t2_1 (a=?) + `--SCAN t3 +} + +# The sqlite3ExprIsConstant() routine does not believe that +# the expression "coalesce(5,random())" is constant. So the +# optimization does not apply in this case. +# +sqlite3_create_function db +do_eqp_test 122 { + SELECT * FROM t1, t2, t3 + WHERE t1.a=t2.a AND t2.a=t3.j AND t3.j=coalesce(5,random()) + ORDER BY t1.a; +} { + QUERY PLAN + |--SCAN t3 + |--SEARCH t1 USING INDEX sqlite_autoindex_t1_1 (a=?) + |--SEARCH t2 USING INDEX sqlite_autoindex_t2_1 (a=?) + `--USE TEMP B-TREE FOR ORDER BY +} + +# Constant propagation in the face of collating sequences: +# +do_execsql_test 200 { + CREATE TABLE c3(x COLLATE binary, y COLLATE nocase, z COLLATE binary); + CREATE INDEX c3x ON c3(x); + INSERT INTO c3 VALUES('ABC', 'ABC', 'abc'); + SELECT * FROM c3 WHERE x=y AND y=z AND z='abc'; +} {ABC ABC abc} + +# If the constants are blindly propagated, as shown in the following +# query, the wrong answer results: +# +do_execsql_test 201 { + SELECT * FROM c3 WHERE x='abc' AND y='abc' AND z='abc'; +} {} + +# Constant propagation caused an incorrect answer in the following +# query. (Reported by Bentley system on 2018-08-09.) +# +do_execsql_test 300 { + CREATE TABLE A(id INTEGER PRIMARY KEY, label TEXT); + CREATE TABLE B(id INTEGER PRIMARY KEY, label TEXT, Aid INTEGER); + CREATE TABLE C( + id INTEGER PRIMARY KEY, + xx INTEGER NOT NULL, + yy INTEGER, + zz INTEGER + ); + CREATE UNIQUE INDEX x2 ON C(yy); + CREATE UNIQUE INDEX x4 ON C(yy, zz); + INSERT INTO A(id) VALUES(1); + INSERT INTO B(id) VALUES(2); + INSERT INTO C(id,xx,yy,zz) VALUES(99,50,1,2); + SELECT 1 + FROM A, + (SELECT id,xx,yy,zz FROM C) subq, + B + WHERE A.id='1' + AND A.id=subq.yy + AND B.id=subq.zz; +} {1} +do_execsql_test 301 { + SELECT 1 + FROM A, + (SELECT id,xx,yy,zz FROM C) subq, + B + WHERE A.id=1 + AND A.id=subq.yy + AND B.id=subq.zz; +} {1} +do_execsql_test 302 { + SELECT 1 + FROM A, + (SELECT id,yy,zz FROM C) subq, + B + WHERE A.id='1' + AND A.id=subq.yy + AND B.id=subq.zz; +} {1} + +# 2018-10-25: Ticket [cf5ed20f] +# Incorrect join result with duplicate WHERE clause constraint. +# +do_execsql_test 400 { + CREATE TABLE x(a, b, c); + CREATE TABLE y(a, b); + INSERT INTO x VALUES (1, 0, 1); + INSERT INTO y VALUES (1, 2); + SELECT x.a FROM x JOIN y ON x.c = y.a WHERE x.b = 1 AND x.b = 1; +} {} + +# 2020-01-07: ticket 82ac75ba0093e5dc +# Incorrect join result due to mishandling of affinity in constant +# propagation. +# +reset_db +do_execsql_test 500 { + PRAGMA automatic_index=OFF; + CREATE TABLE t0(c0); + INSERT INTO t0 VALUES('0'); + CREATE VIEW v0(c0) AS SELECT CAST(0 AS INT) FROM t0; + SELECT 200, * FROM t0, v0 WHERE 0 = t0.c0 AND t0.c0 = v0.c0; +} {} +do_execsql_test 510 { + SELECT 200, * FROM t0, v0 WHERE t0.c0 = 0 AND t0.c0 = v0.c0; +} {} +do_execsql_test 520 { + SELECT 200, * FROM t0, v0 WHERE 0 = t0.c0 AND v0.c0 = t0.c0; +} {} +do_execsql_test 530 { + SELECT 200, * FROM t0, v0 WHERE t0.c0 = 0 AND v0.c0 = t0.c0; +} {} + +# 2020-02-13: ticket 1dcb4d44964846ad +# A problem introduced while making optimizations on the fixes above. +# +reset_db +do_execsql_test 600 { + CREATE TABLE t1(x TEXT); + CREATE TABLE t2(y TEXT); + INSERT INTO t1 VALUES('good'),('bad'); + INSERT INTO t2 VALUES('good'),('bad'); + SELECT * FROM t1 JOIN t2 ON x=y + WHERE x='good' AND y='good'; +} {good good} + +# 2020-04-24: Another test case for the previous (1dcb4d44964846ad) +# ticket. The test case comes from +# https://stackoverflow.com/questions/61399253/sqlite3-different-result-in-console-compared-to-python-script/ +# Output verified against postgresql. +# +do_execsql_test 610 { + CREATE TABLE tableA( + ID int, + RunYearMonth int + ); + INSERT INTO tableA VALUES(1,202003),(2,202003),(3,202003),(4,202004), + (5,202004),(6,202004),(7,202004),(8,202004); + CREATE TABLE tableB ( + ID int, + RunYearMonth int + ); + INSERT INTO tableB VALUES(1,202004),(2,202004),(3,202004),(4,202004), + (5,202004); + SELECT * + FROM ( + SELECT * + FROM tableA + WHERE RunYearMonth = 202004 + ) AS A + INNER JOIN ( + SELECT * + FROM tableB + WHERE RunYearMonth = 202004 + ) AS B + ON A.ID = B.ID + AND A.RunYearMonth = B.RunYearMonth; +} {4 202004 4 202004 5 202004 5 202004} + +# 2023-02-10 https://sqlite.org/forum/forumpost/0a539c76db3b9e29 +# The original constant propagation implementation caused a performance +# regression. Because "abs(v)" was rewritten into "abs(1)" it no longer +# matches the indexed column and the index is not used. +# +reset_db +do_execsql_test 700 { + CREATE TABLE t1(v INTEGER); + WITH RECURSIVE c(x) AS (VALUES(-10) UNION ALL SELECT x+1 FROM c WHERE x<10) + INSERT INTO t1(v) SELECT x FROM c; + CREATE INDEX idx ON t1( abs(v) ); + SELECT v FROM t1 WHERE abs(v)=1 and v=1; +} 1 +do_eqp_test 710 { + SELECT v FROM t1 WHERE abs(v)=1 and v=1; +} { + QUERY PLAN + `--SEARCH t1 USING INDEX idx (<expr>=?) +} + +# 2024-03-07 https://sqlite.org/forum/forumpost/ecdfc02339 +# A refinement is needed to the enhancements tested by the prior test case +# to avoid another problem with indexes on constant expressions. +# +reset_db +db null NULL +do_execsql_test 800 { + CREATE TABLE t0(c0, c1); + CREATE TABLE t1(c2); + CREATE INDEX i0 ON t1(NULL); + INSERT INTO t1(c2) VALUES (0.2); + CREATE VIEW v0(c3) AS SELECT DISTINCT c2 FROM t1; + SELECT * FROM v0 LEFT JOIN t0 ON c3<NULL LEFT JOIN t1 ON 1; +} {0.2 NULL NULL 0.2} +do_execsql_test 810 { + SELECT * FROM v0 LEFT JOIN t0 ON c3<NULL LEFT JOIN t1 ON 1 WHERE c2/0.1; +} {0.2 NULL NULL 0.2} + +#------------------------------------------------------------------------- +# 2025-04-10 https://sqlite.org/forum/forumpost/0109bca824 +reset_db + +do_execsql_test 900 { + SELECT * FROM (SELECT 1.0 AS abc) WHERE abc=1; +} {1.0} +do_execsql_test 910 { + SELECT * FROM (SELECT 1.0 AS abc) WHERE abc LIKE '1.0'; +} {1.0} +do_execsql_test 920 { + SELECT * FROM (SELECT 1.0 AS abc) WHERE abc=1 AND abc LIKE '1.0'; +} {1.0} + +do_execsql_test 930 { + CREATE TABLE IF NOT EXISTS t0 (c0 BLOB); + CREATE TABLE IF NOT EXISTS t1 (c0 INTEGER); + + INSERT INTO t1 VALUES ('1'); + INSERT INTO t0 VALUES (''), (''), ('2'); +} + +do_execsql_test 940 { + SELECT * + FROM (SELECT 0.0 AS col_0) as subQuery + LEFT JOIN t0 ON ((CASE '' + WHEN t0.c0 THEN subQuery.col_0 + ELSE (t0.c0) END) LIKE (((((subQuery.col_0)))))) + LEFT JOIN t1 ON ((subQuery.col_0) == (false)); +} {0.0 {} 1 0.0 {} 1} + +do_execsql_test 950 { + SELECT * + FROM (SELECT 0.0 AS col_0) as subQuery + LEFT JOIN t0 ON ((CASE '' + WHEN t0.c0 THEN subQuery.col_0 + ELSE (t0.c0) END) LIKE (((((subQuery.col_0)))))) + LEFT JOIN t1 ON ((subQuery.col_0) == (false)) WHERE t1.c0; +} {0.0 {} 1 0.0 {} 1} + +finish_test diff --git a/test/whereM.test b/test/whereM.test new file mode 100644 index 0000000000..83436893e8 --- /dev/null +++ b/test/whereM.test @@ -0,0 +1,112 @@ +# 2021 May 27 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# Tests focused on the "constant propagation" that occurs within the +# WHERE clause of a SELECT statemente. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/malloc_common.tcl +set testprefix whereM + + +do_execsql_test 1.0 { + CREATE TABLE t1(a, b INTEGER, c TEXT, d REAL, e BLOB); + INSERT INTO t1 VALUES(10.0, 10.0, 10.0, 10.0, 10.0); + SELECT * FROM t1; +} { + 10.0 10 10.0 10.0 10.0 +} + +do_execsql_test 1.1.1 { + SELECT a=10, a = '10.0', a LIKE '10.0' FROM t1; +} {1 0 1} +do_execsql_test 1.1.2 { + SELECT count(*) FROM t1 WHERE a=10 AND a = '10.0' +} {0} +do_execsql_test 1.1.3 { + SELECT count(*) FROM t1 WHERE a=10 AND a LIKE '10.0' +} {1} +do_execsql_test 1.1.4 { + SELECT count(*) FROM t1 WHERE a='10.0' AND a LIKE '10.0' +} {0} + +do_execsql_test 1.2.1 { + SELECT b=10, b = '10.0', b LIKE '10.0', b LIKE '10' FROM t1; +} {1 1 0 1} +do_execsql_test 1.2.2 { + SELECT count(*) FROM t1 WHERE b=10 AND b = '10.0' +} {1} +do_execsql_test 1.2.3 { + SELECT count(*) FROM t1 WHERE b=10 AND b LIKE '10.0' +} {0} +do_execsql_test 1.2.4 { + SELECT count(*) FROM t1 WHERE b='10.0' AND b LIKE '10.0' +} {0} +do_execsql_test 1.2.3 { + SELECT count(*) FROM t1 WHERE b=10 AND b LIKE '10' +} {1} +do_execsql_test 1.2.4 { + SELECT count(*) FROM t1 WHERE b='10.0' AND b LIKE '10' +} {1} + +do_execsql_test 1.3.1 { + SELECT c=10, c = 10.0, c = '10.0', c LIKE '10.0' FROM t1; +} {0 1 1 1} +do_execsql_test 1.3.2 { + SELECT count(*) FROM t1 WHERE c=10 AND c = '10.0' +} {0} +do_execsql_test 1.3.3 { + SELECT count(*) FROM t1 WHERE c=10 AND c LIKE '10.0' +} {0} +do_execsql_test 1.3.4 { + SELECT count(*) FROM t1 WHERE c='10.0' AND c LIKE '10.0' +} {1} +do_execsql_test 1.3.5 { + SELECT count(*) FROM t1 WHERE c=10.0 AND c = '10.0' +} {1} +do_execsql_test 1.3.6 { + SELECT count(*) FROM t1 WHERE c=10.0 AND c LIKE '10.0' +} {1} + +do_execsql_test 1.4.1 { + SELECT d=10, d = 10.0, d = '10.0', d LIKE '10.0', d LIKE '10' FROM t1; +} {1 1 1 1 0} +do_execsql_test 1.4.2 { + SELECT count(*) FROM t1 WHERE d=10 AND d = '10.0' +} {1} +do_execsql_test 1.4.3 { + SELECT count(*) FROM t1 WHERE d=10 AND d LIKE '10.0' +} {1} +do_execsql_test 1.4.4 { + SELECT count(*) FROM t1 WHERE d='10.0' AND d LIKE '10.0' +} {1} +do_execsql_test 1.4.5 { + SELECT count(*) FROM t1 WHERE d='10' AND d LIKE '10.0' +} {1} + +do_execsql_test 1.5.1 { + SELECT e=10, e = '10.0', e LIKE '10.0', e LIKE '10' FROM t1; +} {1 0 1 0} +do_execsql_test 1.5.2 { + SELECT count(*) FROM t1 WHERE e=10 AND e = '10.0' +} {0} +do_execsql_test 1.5.3 { + SELECT count(*) FROM t1 WHERE e=10 AND e LIKE '10.0' +} {1} +do_execsql_test 1.5.4 { + SELECT count(*) FROM t1 WHERE e='10.0' AND e LIKE '10.0' +} {0} +do_execsql_test 1.5.5 { + SELECT count(*) FROM t1 WHERE e=10.0 AND e LIKE '10.0' +} {1} + +finish_test diff --git a/test/whereN.test b/test/whereN.test new file mode 100644 index 0000000000..b9b889fa28 --- /dev/null +++ b/test/whereN.test @@ -0,0 +1,103 @@ +# 2024-04-02 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# Tests for the whereInterstageHeuristic() routine in the query planner. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix whereN + +# The following is a simplified and "sanitized" version of the original +# real-world query that brought the problem to light. +# +# The issue is a slow query. The answer is correct, but it was taking too +# much time, because it was doing a full table scan rather than an indexed +# lookup. +# +# The problem was that the query planner was overestimating the number of +# output rows. The estimated number of output rows is accurate if the +# DSNAME parameter is "ds-one". In that case, a large fraction of the rows +# in "violation" end up being output. The query planner correctly deduces +# that it is faster to do a full table scan of the large "violation" table +# to avoid the after-query sort that implements the ORDER BY clause. However, +# if the DSNAME is "ds-two", then only a few rows (about 6) are generated, +# and it is much much faster to do an indexed lookup of "violation" followed +# by a sort operation to implement ORDER BY +# +# The problem, of course, is that the query planner has no way of knowing +# in advance how many rows will be generated. The query planner tries to +# estimate a worst case, which is a large number of output rows, and it picks +# the best plan for that case. However, the plan choosen is very inefficient +# when the number of output rows is small. +# +# The whereInterstageHeuristic() routine in the query planner attempts to +# correct this by adjusting the query plan such that it avoids the very bad +# query plan for a small number of rows, at the expense of a slightly less +# efficient plan for a large number of rows. The large number of rows case +# is perhaps 5% slower with the revised plan, but the small number of +# rows case is around 100 times faster. That seems like a good tradeoff. +# +do_execsql_test 1.0 { + CREATE TABLE datasource(dsid INT, name TEXT); + INSERT INTO datasource VALUES(1,'ds-one'),(2,'ds-two'),(3,'ds-three'); + CREATE INDEX ds1 ON datasource(name, dsid); + + CREATE TABLE rule(rid INT, team_id INT, dsid INT); + WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<9) + INSERT INTO rule(rid,team_id,dsid) SELECT n, 1, 1 FROM c; + WITH RECURSIVE c(n) AS (VALUES(10) UNION ALL SELECT n+1 FROM c WHERE n<24) + INSERT INTO rule(rid,team_id,dsid) SELECT n, 2, 2 FROM c; + CREATE INDEX rule2 ON rule(dsid, rid); + + CREATE TABLE violation(vid INT, rid INT, vx BLOB); + /*** Uncomment to insert actual data + WITH src(rid, cnt) AS (VALUES(1,3586),(2,1343),(3,6505),(5,76230), + (6,740),(7,287794),(8,457),(12,1), + (14,1),(16,1),(17,1),(18,1),(19,1)) + INSERT INTO violation(vid, rid, vx) + SELECT rid*1000000+value, rid, randomblob(15) + FROM src, generate_series(1,cnt); + ***/ + CREATE INDEX v1 ON violation(rid, vid); + CREATE INDEX v2 ON violation(vid); + ANALYZE; + DELETE FROM sqlite_stat1; + DROP TABLE IF EXISTS sqlite_stat4; + INSERT INTO sqlite_stat1 VALUES + ('violation','v2','376661 1'), + ('violation','v1','376661 28974 1'), + ('rule','rule2','24 12 1'), + ('datasource','ds1','3 1 1'); + ANALYZE sqlite_schema; +} +set DSNAME ds-two ;# Only a few rows. Change to "ds-one" for many rows. +do_eqp_test 1.1 { + SELECT count(*), length(group_concat(vx)) FROM ( + SELECT V.* + FROM datasource DS, rule R, violation V + WHERE V.rid=R.rid + AND R.dsid=DS.dsid + AND DS.name=$DSNAME + ORDER BY V.vid desc + ); +} { + QUERY PLAN + |--CO-ROUTINE (subquery-xxxxxx) + | |--SEARCH DS USING COVERING INDEX ds1 (name=?) + | |--SEARCH R USING COVERING INDEX rule2 (dsid=?) + | |--SEARCH V USING INDEX v1 (rid=?) + | `--USE TEMP B-TREE FOR ORDER BY + `--SCAN (subquery-xxxxxx) +} +# ^^^^---- We want to see three SEARCH terms. No SCAN terms. +# The ORDER BY is implemented by a separate sorter pass. + +finish_test diff --git a/test/wherefault.test b/test/wherefault.test index 60330a92dd..7f07ad5f53 100644 --- a/test/wherefault.test +++ b/test/wherefault.test @@ -56,4 +56,28 @@ do_malloc_test 2 -tclprep { SELECT count(*) FROM t1 WHERE a BETWEEN 5 AND 995 OR b BETWEEN 5 AND 900000; } +reset_db +do_execsql_test 3.0 { + PRAGMA writable_schema = 1; + BEGIN TRANSACTION; + CREATE TABLE t1( + a INT AS (c*11), + b TEXT AS (substr(d,1,3)) STORED, + c INTEGEB PRIMARI KEY, d TEXT + ); + CREATE INDEX t1a ON t1(a); + COMMIT; +} +faultsim_save_and_close + +do_faultsim_test 3.1 -faults oom* -prep { + faultsim_restore_and_reopen +} -body { + execsql { + SELECT * FROM (SELECT a FROM t1 NATURAL JOIN t1 WHERE a IN (SELECT b FROM t1 ORDER BY b)) WHERE (SELECT a FROM t1 NATURAL JOIN (SELECT * FROM (SELECT a FROM t1 NATURAL JOIN t1 WHERE a IN (SELECT CASE b WHEN 82 THEN 207 WHEN 869 THEN 406 WHEN 85 THEN 83 WHEN 705 THEN 698 ELSE 1992229051 END%5 FROM t1 ORDER BY b)) WHERE (SELECT a FROM t1 NATURAL JOIN (SELECT b FROM t1 ORDER BY b) WHERE a IN (SELECT b FROM t1 ORDER BY b))) WHERE a ); + } +} -test { + faultsim_test_result {0 {}} +} + finish_test diff --git a/test/wherelfault.test b/test/wherelfault.test new file mode 100644 index 0000000000..cfb8c1cfb4 --- /dev/null +++ b/test/wherelfault.test @@ -0,0 +1,82 @@ +# 2008 October 6 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is testing fault-injection with the +# LIMIT ... OFFSET ... clause of UPDATE and DELETE statements. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/malloc_common.tcl +set testprefix wherelfault + +ifcapable !update_delete_limit { + finish_test + return +} + +do_execsql_test 1.0 { + CREATE TABLE t1(a, b); + INSERT INTO t1 VALUES(1, 'f'); + INSERT INTO t1 VALUES(2, 'e'); + INSERT INTO t1 VALUES(3, 'd'); + INSERT INTO t1 VALUES(4, 'c'); + INSERT INTO t1 VALUES(5, 'b'); + INSERT INTO t1 VALUES(6, 'a'); + + CREATE VIEW v1 AS SELECT a,b FROM t1; + CREATE TABLE log(op, a); + + CREATE TRIGGER v1del INSTEAD OF DELETE ON v1 BEGIN + INSERT INTO log VALUES('delete', old.a); + END; + + CREATE TRIGGER v1upd INSTEAD OF UPDATE ON v1 BEGIN + INSERT INTO log VALUES('update', old.a); + END; +} + +faultsim_save_and_close +do_faultsim_test 1.1 -prep { + faultsim_restore_and_reopen + db eval {SELECT * FROM sqlite_master} +} -body { + execsql { DELETE FROM v1 ORDER BY a LIMIT 3; } +} -test { + faultsim_test_result {0 {}} +} + +do_faultsim_test 1.2 -prep { + faultsim_restore_and_reopen + db eval {SELECT * FROM sqlite_master} +} -body { + execsql { UPDATE v1 SET b = 555 ORDER BY a LIMIT 3 } +} -test { + faultsim_test_result {0 {}} +} + +#------------------------------------------------------------------------- +sqlite3 db test.db +do_execsql_test 2.1.0 { + CREATE TABLE t2(a, b, c, PRIMARY KEY(a, b)) WITHOUT ROWID; +} +faultsim_save_and_close + +do_faultsim_test 2.1 -prep { + faultsim_restore_and_reopen + db eval {SELECT * FROM sqlite_master} +} -body { + execsql { DELETE FROM t2 WHERE c=? ORDER BY a DESC LIMIT 10 } +} -test { + faultsim_test_result {0 {}} +} + +finish_test diff --git a/test/wherelimit.test b/test/wherelimit.test index f0cfbb61e8..f6efab6682 100644 --- a/test/wherelimit.test +++ b/test/wherelimit.test @@ -38,6 +38,8 @@ proc create_test_data {size} { ifcapable {update_delete_limit} { + execsql { CREATE TABLE t1(x, y) } + # check syntax error support do_test wherelimit-0.1 { catchsql {DELETE FROM t1 ORDER BY x} @@ -50,12 +52,18 @@ ifcapable {update_delete_limit} { } {1 {ORDER BY without LIMIT on UPDATE}} # no AS on table sources + # + # UPDATE: As of version 3.24, AS clauses are allowed as part of + # UPDATE or DELETE statements. do_test wherelimit-0.4 { - catchsql {DELETE FROM t1 AS a WHERE x=1} - } {1 {near "AS": syntax error}} - do_test wherelimit-0.5 { + catchsql {DELETE FROM t1 AS a WHERE a.x=1} + } {0 {}} + do_test wherelimit-0.5.1 { catchsql {UPDATE t1 AS a SET y=1 WHERE x=1} - } {1 {near "AS": syntax error}} + } {0 {}} + do_test wherelimit-0.5.2 { + catchsql {UPDATE t1 AS a SET y=1 WHERE t1.x=1} + } {1 {no such column: t1.x}} # OFFSET w/o LIMIT do_test wherelimit-0.6 { @@ -65,6 +73,7 @@ ifcapable {update_delete_limit} { catchsql {UPDATE t1 SET y=1 WHERE x=1 OFFSET 2} } {1 {near "OFFSET": syntax error}} + execsql { DROP TABLE t1 } # check deletes w/o where clauses but with limit/offsets create_test_data 5 @@ -85,21 +94,31 @@ ifcapable {update_delete_limit} { execsql {DELETE FROM t1 ORDER BY x LIMIT 5} execsql {SELECT count(*) FROM t1} } {15} + create_test_data 4 + do_test wherelimit-1.3b { + # limit 5 + execsql {DELETE FROM t1 RETURNING x, y, '|' ORDER BY x, y LIMIT 5} + } {1 1 | 1 2 | 1 3 | 1 4 | 2 1 |} + do_test wherelimit-1.3c { + execsql {SELECT count(*) FROM t1} + } {11} do_test wherelimit-1.4 { # limit 5, offset 2 - execsql {DELETE FROM t1 ORDER BY x LIMIT 5 OFFSET 2} + execsql {DELETE FROM t1 RETURNING x, y, '|' ORDER BY x LIMIT 5 OFFSET 2} + } {2 4 | 3 1 | 3 2 | 3 3 | 3 4 |} + do_test wherelimit-1.4cnt { execsql {SELECT count(*) FROM t1} - } {10} + } {6} do_test wherelimit-1.5 { # limit 5, offset -2 execsql {DELETE FROM t1 ORDER BY x LIMIT 5 OFFSET -2} execsql {SELECT count(*) FROM t1} - } {5} + } {1} do_test wherelimit-1.6 { # limit -5 (no limit), offset 2 execsql {DELETE FROM t1 ORDER BY x LIMIT 2, -5} execsql {SELECT count(*) FROM t1} - } {2} + } {1} do_test wherelimit-1.7 { # limit 5, offset -2 (no offset) execsql {DELETE FROM t1 ORDER BY x LIMIT -2, 5} @@ -218,7 +237,9 @@ ifcapable {update_delete_limit} { } {11} create_test_data 6 do_test wherelimit-3.2 { - execsql {UPDATE t1 SET y=1 WHERE x=1 LIMIT 5} + execsql {UPDATE t1 SET y=1 WHERE x=1 RETURNING x, y, '|' LIMIT 5} + } {1 1 | 1 1 | 1 1 | 1 1 | 1 1 |} + do_test wherelimit-3.2cnt { execsql {SELECT count(*) FROM t1 WHERE y=1} } {10} do_test wherelimit-3.3 { @@ -279,6 +300,43 @@ ifcapable {update_delete_limit} { execsql {SELECT count(*) FROM t1 WHERE y=1} } {6} + # Cannot use a LIMIT for UPDATE or DELETE against a WITHOUT ROWID table + # or a VIEW. (We should fix this someday). + # + db close + sqlite3 db :memory: + do_execsql_test wherelimit-4.1 { + CREATE TABLE t1(a int); + INSERT INTO t1 VALUES(1); + INSERT INTO t1 VALUES(2); + INSERT INTO t1 VALUES(3); + CREATE TABLE t2(a int); + INSERT INTO t2 SELECT a+100 FROM t1; + CREATE VIEW tv(r,a) AS + SELECT rowid, a FROM t2 UNION ALL SELECT rowid, a FROM t1; + CREATE TRIGGER tv_del INSTEAD OF DELETE ON tv + BEGIN + DELETE FROM t1 WHERE rowid=old.r; + DELETE FROM t2 WHERE rowid=old.r; + END; + } {} + do_catchsql_test wherelimit-4.2 { + DELETE FROM tv WHERE 1 LIMIT 2; + } {0 {}} + do_catchsql_test wherelimit-4.3 { + DELETE FROM tv WHERE 1 ORDER BY a LIMIT 2; + } {0 {}} + do_execsql_test wherelimit-4.10 { + CREATE TABLE t3(a,b,c,d TEXT, PRIMARY KEY(a,b)) WITHOUT ROWID; + INSERT INTO t3(a,b,c,d) VALUES(1,2,3,4),(5,6,7,8),(9,10,11,12); + } {} + do_catchsql_test wherelimit-4.11 { + DELETE FROM t3 WHERE a=5 LIMIT 2; + } {0 {}} + do_execsql_test wherelimit-4.12 { + SELECT a,b,c,d FROM t3 ORDER BY 1; + } {1 2 3 4 9 10 11 12} + } finish_test diff --git a/test/wherelimit2.test b/test/wherelimit2.test new file mode 100644 index 0000000000..57288bf64c --- /dev/null +++ b/test/wherelimit2.test @@ -0,0 +1,333 @@ +# 2008 October 6 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is testing the LIMIT ... OFFSET ... clause +# of UPDATE and DELETE statements. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix wherelimit2 + +ifcapable !update_delete_limit { + finish_test + return +} + +#------------------------------------------------------------------------- +# Test with views and INSTEAD OF triggers. +# +do_execsql_test 1.0 { + CREATE TABLE t1(a, b); + INSERT INTO t1 VALUES(1, 'f'); + INSERT INTO t1 VALUES(2, 'e'); + INSERT INTO t1 VALUES(3, 'd'); + INSERT INTO t1 VALUES(4, 'c'); + INSERT INTO t1 VALUES(5, 'b'); + INSERT INTO t1 VALUES(6, 'a'); + + CREATE VIEW v1 AS SELECT a,b FROM t1; + CREATE TABLE log(op, a); + + CREATE TRIGGER v1del INSTEAD OF DELETE ON v1 BEGIN + INSERT INTO log VALUES('delete', old.a); + END; + + CREATE TRIGGER v1upd INSTEAD OF UPDATE ON v1 BEGIN + INSERT INTO log VALUES('update', old.a); + END; +} + +do_execsql_test 1.1 { + DELETE FROM v1 ORDER BY a LIMIT 3; + SELECT * FROM log; DELETE FROM log; +} { + delete 1 delete 2 delete 3 +} +do_execsql_test 1.2 { + DELETE FROM v1 ORDER BY b LIMIT 3; + SELECT * FROM log; DELETE FROM log; +} { + delete 6 delete 5 delete 4 +} +do_execsql_test 1.3 { + UPDATE v1 SET b = 555 ORDER BY a LIMIT 3; + SELECT * FROM log; DELETE FROM log; +} { + update 1 update 2 update 3 +} +do_execsql_test 1.4 { + UPDATE v1 SET b = 555 ORDER BY b LIMIT 3; + SELECT * FROM log; DELETE FROM log; +} { + update 6 update 5 update 4 +} + +#------------------------------------------------------------------------- +# Simple test using WITHOUT ROWID table. +# +do_execsql_test 2.1.0 { + CREATE TABLE t2(a, b, c, PRIMARY KEY(a, b)) WITHOUT ROWID; + INSERT INTO t2 VALUES(1, 1, 'h'); + INSERT INTO t2 VALUES(1, 2, 'g'); + INSERT INTO t2 VALUES(2, 1, 'f'); + INSERT INTO t2 VALUES(2, 2, 'e'); + INSERT INTO t2 VALUES(3, 1, 'd'); + INSERT INTO t2 VALUES(3, 2, 'c'); + INSERT INTO t2 VALUES(4, 1, 'b'); + INSERT INTO t2 VALUES(4, 2, 'a'); +} + +do_execsql_test 2.1.1 { + BEGIN; + DELETE FROM t2 WHERE b=1 ORDER BY c LIMIT 2; + SELECT c FROM t2 ORDER BY 1; + ROLLBACK; +} {a c e f g h} + +do_execsql_test 2.1.2 { + BEGIN; + UPDATE t2 SET c=NULL ORDER BY a, b DESC LIMIT 3 OFFSET 1; + SELECT a, b, c FROM t2; + ROLLBACK; +} { + 1 1 {} + 1 2 g + 2 1 {} + 2 2 {} + 3 1 d + 3 2 c + 4 1 b + 4 2 a +} + +do_execsql_test 2.2.0 { + DROP TABLE t2; + CREATE TABLE t2(a INTEGER PRIMARY KEY, b, c) WITHOUT ROWID; + INSERT INTO t2 VALUES(1, 1, 'h'); + INSERT INTO t2 VALUES(2, 2, 'g'); + INSERT INTO t2 VALUES(3, 1, 'f'); + INSERT INTO t2 VALUES(4, 2, 'e'); + INSERT INTO t2 VALUES(5, 1, 'd'); + INSERT INTO t2 VALUES(6, 2, 'c'); + INSERT INTO t2 VALUES(7, 1, 'b'); + INSERT INTO t2 VALUES(8, 2, 'a'); +} + +do_execsql_test 2.2.1 { + BEGIN; + DELETE FROM t2 WHERE b=1 ORDER BY c LIMIT 2; + SELECT c FROM t2 ORDER BY 1; + ROLLBACK; +} {a c e f g h} + +do_execsql_test 2.2.2 { + BEGIN; + UPDATE t2 SET c=NULL ORDER BY a DESC LIMIT 3 OFFSET 1; + SELECT a, b, c FROM t2; + ROLLBACK; +} { + 1 1 h + 2 2 g + 3 1 f + 4 2 e + 5 1 {} + 6 2 {} + 7 1 {} + 8 2 a +} + +#------------------------------------------------------------------------- +# Test using a virtual table +# +ifcapable fts5 { + do_execsql_test 3.0 { + CREATE VIRTUAL TABLE ft USING fts5(x); + INSERT INTO ft(rowid, x) VALUES(-45, 'a a'); + INSERT INTO ft(rowid, x) VALUES(12, 'a b'); + INSERT INTO ft(rowid, x) VALUES(444, 'a c'); + INSERT INTO ft(rowid, x) VALUES(12300, 'a d'); + INSERT INTO ft(rowid, x) VALUES(25400, 'a c'); + INSERT INTO ft(rowid, x) VALUES(25401, 'a b'); + INSERT INTO ft(rowid, x) VALUES(50000, 'a a'); + } + + do_execsql_test 3.1.1 { + BEGIN; + DELETE FROM ft ORDER BY rowid LIMIT 3; + SELECT x FROM ft; + ROLLBACK; + } {{a d} {a c} {a b} {a a}} + + do_execsql_test 3.1.2 { + BEGIN; + DELETE FROM ft WHERE ft MATCH 'a' ORDER BY rowid LIMIT 3; + SELECT x FROM ft; + ROLLBACK; + } {{a d} {a c} {a b} {a a}} + + do_execsql_test 3.1.3 { + BEGIN; + DELETE FROM ft WHERE ft MATCH 'b' ORDER BY rowid ASC LIMIT 1 OFFSET 1; + SELECT rowid FROM ft; + ROLLBACK; + } {-45 12 444 12300 25400 50000} + + do_execsql_test 3.2.1 { + BEGIN; + UPDATE ft SET x='hello' ORDER BY rowid LIMIT 2 OFFSET 2; + SELECT x FROM ft; + ROLLBACK; + } {{a a} {a b} hello hello {a c} {a b} {a a}} + + do_execsql_test 3.2.2 { + BEGIN; + UPDATE ft SET x='hello' WHERE ft MATCH 'a' + ORDER BY rowid DESC LIMIT 2 OFFSET 2; + SELECT x FROM ft; + ROLLBACK; + } {{a a} {a b} {a c} hello hello {a b} {a a}} +} ;# fts5 + +#------------------------------------------------------------------------- +# Test using INDEXED BY clauses. +# +do_execsql_test 4.0 { + CREATE TABLE x1(a INTEGER PRIMARY KEY, b, c, d); + CREATE INDEX x1bc ON x1(b, c); + INSERT INTO x1 VALUES(1,1,1,1); + INSERT INTO x1 VALUES(2,1,2,2); + INSERT INTO x1 VALUES(3,2,1,3); + INSERT INTO x1 VALUES(4,2,2,3); + INSERT INTO x1 VALUES(5,3,1,2); + INSERT INTO x1 VALUES(6,3,2,1); +} + +do_execsql_test 4.1 { + BEGIN; + DELETE FROM x1 ORDER BY a LIMIT 2; + SELECT a FROM x1; + ROLLBACK; +} {3 4 5 6} + +# 2020-06-03: Query planner improved so that a solution is possible. +# +#do_catchsql_test 4.2 { +# DELETE FROM x1 INDEXED BY x1bc WHERE d=3 LIMIT 1; +#} {1 {no query solution}} + +do_execsql_test 4.3 { + DELETE FROM x1 INDEXED BY x1bc WHERE b=3 LIMIT 1; + SELECT a FROM x1; +} {1 2 3 4 6} + +# 2020-06-03: Query planner improved so that a solution is possible. +# +#do_catchsql_test 4.4 { +# UPDATE x1 INDEXED BY x1bc SET d=5 WHERE d=3 LIMIT 1; +#} {1 {no query solution}} + +do_execsql_test 4.5 { + UPDATE x1 INDEXED BY x1bc SET d=5 WHERE b=2 LIMIT 1; + SELECT a, d FROM x1; +} {1 1 2 2 3 5 4 3 6 1} + +#------------------------------------------------------------------------- +# Test using object names that require quoting. +# +do_execsql_test 5.0 { + CREATE TABLE "x y"("a b" PRIMARY KEY, "c d") WITHOUT ROWID; + CREATE INDEX xycd ON "x y"("c d"); + + INSERT INTO "x y" VALUES('a', 'a'); + INSERT INTO "x y" VALUES('b', 'b'); + INSERT INTO "x y" VALUES('c', 'c'); + INSERT INTO "x y" VALUES('d', 'd'); + INSERT INTO "x y" VALUES('e', 'a'); + INSERT INTO "x y" VALUES('f', 'b'); + INSERT INTO "x y" VALUES('g', 'c'); + INSERT INTO "x y" VALUES('h', 'd'); +} + +do_execsql_test 5.1 { + BEGIN; + DELETE FROM "x y" WHERE "c d"!='e' ORDER BY "c d" LIMIT 2 OFFSET 2; + SELECT * FROM "x y" ORDER BY 1; + ROLLBACK; +} { + a a c c d d e a g c h d +} + +do_execsql_test 5.2 { + BEGIN; + UPDATE "x y" SET "c d"='e' WHERE "c d"!='e' ORDER BY "c d" LIMIT 2 OFFSET 2; + SELECT * FROM "x y" ORDER BY 1; + ROLLBACK; +} { + a a b e c c d d e a f e g c h d +} + +proc log {args} { lappend ::log {*}$args } +db func log log +do_execsql_test 5.3 { + CREATE VIEW "v w" AS SELECT * FROM "x y"; + CREATE TRIGGER tr1 INSTEAD OF DELETE ON "v w" BEGIN + SELECT log(old."a b", old."c d"); + END; + CREATE TRIGGER tr2 INSTEAD OF UPDATE ON "v w" BEGIN + SELECT log(new."a b", new."c d"); + END; +} + +do_test 5.4 { + set ::log {} + execsql { DELETE FROM "v w" ORDER BY "a b" LIMIT 3 } + set ::log +} {a a b b c c} + +do_test 5.5 { + set ::log {} + execsql { UPDATE "v w" SET "a b" = "a b" || 'x' ORDER BY "a b" LIMIT 5; } + set ::log +} {ax a bx b cx c dx d ex a} + +#----------------------------------------------------------------------- +reset_db +do_execsql_test 6.0 { + CREATE TABLE t2(x); + INSERT INTO t2(x) VALUES(1),(2),(3),(5),(8),(13); +} {} + +do_execsql_test 6.1 { + WITH t2 AS MATERIALIZED (VALUES(5)) + DELETE FROM t2 ORDER BY rank()OVER() LIMIT 2; +} + +do_execsql_test 6.2 { + SELECT * FROM t2; +} {3 5 8 13} + +#------------------------------------------------------------------------- + +do_execsql_test 7.0 { + CREATE TABLE t1(a INT); INSERT INTO t1(a) VALUES(0); +} {} + +do_execsql_test 7.1 { + WITH t1(b) AS (SELECT * FROM (SELECT * FROM (VALUES(2)))) + UPDATE t1 SET a=3 LIMIT 1; +} + +do_execsql_test 7.2 { + SELECT * FROM t1; +} {3} + +finish_test diff --git a/test/wherelimit3.test b/test/wherelimit3.test new file mode 100644 index 0000000000..dea3e97d86 --- /dev/null +++ b/test/wherelimit3.test @@ -0,0 +1,67 @@ +# 2024-06-06 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# Test cases for query plans using LIMIT +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix wherelimit3 + +do_execsql_test 1.0 { + CREATE TABLE t1(a INT, b INT); + WITH RECURSIVE c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<1000) + INSERT INTO t1 SELECT n, n FROM c; + CREATE INDEX t1a ON t1(a); + CREATE INDEX t1b ON t1(b); + ANALYZE; +} + +do_eqp_test 1.1 { + SELECT * FROM t1 WHERE a>=100 AND a<300 ORDER BY b LIMIT 5; +} { + QUERY PLAN + |--SEARCH t1 USING INDEX t1a (a>? AND a<?) + `--USE TEMP B-TREE FOR ORDER BY +} +ifcapable stat4 { + do_eqp_test 1.2 { + SELECT * FROM t1 WHERE a>=100 AND a<300 ORDER BY b LIMIT -1; + } { + QUERY PLAN + `--SCAN t1 USING INDEX t1b + } +} + +set N [expr 5] +do_eqp_test 1.3 { + SELECT * FROM t1 WHERE a>=100 AND a<300 ORDER BY b LIMIT $::N; +} { + QUERY PLAN + |--SEARCH t1 USING INDEX t1a (a>? AND a<?) + `--USE TEMP B-TREE FOR ORDER BY +} + +ifcapable stat4 { + set N [expr -1] + do_eqp_test 1.4 { + SELECT * FROM t1 WHERE a>=100 AND a<300 ORDER BY b LIMIT $::N; + } { + QUERY PLAN + `--SCAN t1 USING INDEX t1b + } +} + + + + + +finish_test diff --git a/test/widetab1.test b/test/widetab1.test new file mode 100644 index 0000000000..39523ce882 --- /dev/null +++ b/test/widetab1.test @@ -0,0 +1,156 @@ +# 2022-10-24 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements test cases for wide table (tables with more than +# 64 columns) and indexes that reference columns beyond the 63rd or 64th +# column. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix widetab1 + + +# In order to pick the better index in the following query, SQLite needs to +# be able to detect when an index that references later columns in a wide +# table is a covering index. +# +do_execsql_test 100 { + CREATE TABLE a( + a00, a01, a02, a03, a04, a05, a06, a07, a08, a09, + a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, + a20, a21, a22, a23, a24, a25, a26, a27, a28, a29, + a30, a31, a32, a33, a34, a35, a36, a37, a38, a39, + a40, a41, a42, a43, a44, a45, a46, a47, a48, a49, + a50, a51, a52, a53, a54, a55, a56, a57, a58, a59, + pd, bn, vb, bc, cn, ie, qm); + CREATE INDEX a1 on a(pd, bn, vb, bc, cn); -- preferred index + CREATE INDEX a2 on a(pd, bc, ie, qm); -- suboptimal index + CREATE TABLE b(bg, bc, bn, iv, ln, mg); + CREATE INDEX b1 on b(bn, iv, bg); +} +do_eqp_test 110 { + SELECT dc, count(cn) + FROM (SELECT coalesce(b.bg, a.bc) as dc, cn + FROM a LEFT JOIN b + ON a.bn = b.bn + AND CASE WHEN a.vb IS NOT NULL THEN 1 ELSE 0 END = b.iv + WHERE pd BETWEEN 0 AND 10) + GROUP BY dc; +} { + QUERY PLAN + |--SEARCH a USING COVERING INDEX a1 (pd>? AND pd<?) + |--SEARCH b USING COVERING INDEX b1 (bn=? AND iv=?) LEFT-JOIN + `--USE TEMP B-TREE FOR GROUP BY +} + +reset_db +do_execsql_test 200 { + CREATE TABLE t1( + c00,c01,c02,c03,c04,c05,c06,c07,c08,c09, + c10,c11,c12,c13,c14,c15,c16,c17,c18,c19, + c20,c21,c22,c23,c24,c25,c26,c27,c28,c29, + c30,c31,c32,c33,c34,c35,c36,c37,c38,c39, + c40,c41,c42,c43,c44,c45,c46,c47,c48,c49, + c50,c51,c52,c53,c54,c55,c56,c57,c58,c59, + c60,c61,c62,c63,c64,c65,c66,c67,c68,c69, + c70,c71,c72,c73,c74,c75,c76,c77,c78,c79, + c80,c81,c82,c83,c84,c85,c86,c87,c88,c89, + c90,c91,c92,c93,c94,c95,c96,c97,c98,c99, + a,b,c,d,e + ); + CREATE INDEX t1x1 on t1(c00,a,b, + c01,c02,c03,c04,c05,c06,c07,c08,c09, + c10,c11,c12,c13,c14,c15,c16,c17,c18,c19, + c20,c21,c22,c23,c24,c25,c26,c27,c28,c29, + c30,c31,c32,c33,c34,c35,c36,c37,c38,c39, + c40,c41,c42,c43,c44,c45,c46,c47,c48,c49, + c50,c51,c52,c53,c54,c55,c56,c57,c58,c59, + c60,c61,c62,c63,c64,c65,c66,c67,c68,c69, + c70,c71,c72,c73,c74,c75,c76,c77,c78,c79, + c80,c81,c82,c83,c84,c85,c86,c87,c88,c89, + c90,c91,c92,c93,c94,c00,c96,c97,c98,c99 + ); + CREATE INDEX t1cd ON t1(c,d); + CREATE INDEX t1x2 ON t1(c01,c02,c03,a,b); + WITH RECURSIVE c(x) AS (VALUES(0) UNION ALL SELECT x+1000 FROM c WHERE x<9000) + INSERT INTO t1 SELECT + x+00, x+01, x+02, x+03, x+04, x+05, x+06, x+07, x+08, x+09, + x+10, x+11, x+12, x+13, x+14, x+15, x+16, x+17, x+18, x+19, + x+20, x+21, x+22, x+23, x+24, x+25, x+26, x+27, x+28, x+29, + x+30, x+31, x+32, x+33, x+34, x+35, x+36, x+37, x+38, x+39, + x+40, x+41, x+42, x+43, x+44, x+45, x+46, x+47, x+48, x+49, + x+50, x+51, x+52, x+53, x+54, x+55, x+56, x+57, x+58, x+59, + x+60, x+61, x+62, x+63, x+64, x+65, x+66, x+67, x+68, x+69, + x+70, x+71, x+72, x+73, x+74, x+75, x+76, x+77, x+78, x+79, + x+80, x+81, x+82, x+83, x+84, x+85, x+86, x+87, x+88, x+89, + x+90, x+91, x+92, x+93, x+94, x+95, x+96, x+97, x+98, x+99, + x+100, x+101, x+102, x+103, x+104 FROM c; +} + +do_execsql_test 210 {SELECT sum(c62) FROM t1;} 45620 +do_execsql_test 220 {SELECT sum(c63) FROM t1;} 45630 +do_execsql_test 230 {SELECT sum(c64) FROM t1;} 45640 +do_execsql_test 240 {SELECT sum(c65) FROM t1;} 45650 + +do_execsql_test 300 { + BEGIN; + SELECT sum(c62) FROM t1; + UPDATE t1 SET c62=c62+1 WHERE c00=1000; + SELECT sum(c62) FROM t1; +} {45620 45621} +do_execsql_test 310 { + SELECT sum(c65) FROM t1; + UPDATE t1 SET c65=c65+1 WHERE c00=1000; + SELECT sum(c65) FROM t1; + ROLLBACK; +} {45650 45651} + +do_execsql_test 320 { + BEGIN; + SELECT count(*) FROM t1; + DELETE FROM t1 WHERE c=3102; + SELECT COUNT(*) FROM t1; + ROLLBACK; +} {10 9} +do_execsql_test 330 { + BEGIN; + SELECT count(*) FROM t1; + DELETE FROM t1 WHERE c=3102 AND d=3103; + SELECT COUNT(*) FROM t1; + ROLLBACK; +} {10 9} +do_execsql_test 340 { + BEGIN; + DELETE FROM t1 WHERE (c,d) IN (VALUES(3102,3103),(4102,4103),(5102,5103),(1,2)); + SELECT count(*) FROM t1; + ROLLBACK; +} {7} + +do_execsql_test 400 { + DROP INDEX t1cd; + DROP INDEX t1x1; + DROP INDEX t1x2; + CREATE INDEX t1x3 ON t1(c00,c05,c08); +} +do_execsql_test 410 {SELECT sum(c08) FROM t1 WHERE c00 IN (1000,5000);} 6016 +do_execsql_test 420 {SELECT sum(c63) FROM t1 WHERE c00 IN (1000,5000);} 6126 +do_execsql_test 430 {SELECT sum(c64) FROM t1 WHERE c00 IN (1000,5000);} 6128 + +do_execsql_test 500 { + DROP INDEX t1x3; + CREATE TABLE t2 AS SELECT * FROM t1; + CREATE INDEX t1x4 ON t1(c00, c62, a, b); + CREATE INDEX t2x4 ON t2(c01, c62, c63, b, c); + SELECT t1.b, t2.b FROM t1 JOIN t2 ON t2.c01=t1.c00+1 WHERE +t1.b<7000 + ORDER BY +t1.b; +} {101 101 1101 1101 2101 2101 3101 3101 4101 4101 5101 5101 6101 6101} + +finish_test diff --git a/test/wild001.test b/test/wild001.test deleted file mode 100644 index 7fe1404294..0000000000 --- a/test/wild001.test +++ /dev/null @@ -1,311 +0,0 @@ -# 2013-07-01 -# -# The author disclaims copyright to this source code. In place of -# a legal notice, here is a blessing: -# -# May you do good and not evil. -# May you find forgiveness for yourself and forgive others. -# May you share freely, never taking more than you give. -# -#*********************************************************************** -# -# This is a test case from content taken "from the wild". In this -# particular instance, the query was provided with permission by -# Elan Feingold on 2013-06-27. His message on the SQLite mailing list -# on that date reads: -# -#------------------------------------------------------------------------------ -# > Can you send (1) the schema (2) the query that is giving problems, and (3) -# > the content of the sqlite_stat1 table after you have run ANALYZE? If you -# > can combine all of the above into a script, that would be great! -# > -# > If you send (1..3) above and you give us written permission to include the -# > query in our test suite, that would be off-the-chain terrific. -# -# Please find items 1..3 in this file: http://www.plexapp.com/elan/sqlite_bug.txt -# -# You have our permission to include the query in your test suite. -# -# Thanks for an amazing product. -#----------------------------------------------------------------------------- -# -# This test case merely creates the schema and populates SQLITE_STAT1 and -# SQLITE_STAT3 then runs an EXPLAIN QUERY PLAN to ensure that the right plan -# is discovered. This test case may need to be adjusted for future revisions -# of the query planner manage to select a better query plan. The query plan -# shown here is known to be very fast with the original data. -# -# This test should work the same with and without SQLITE_ENABLE_STAT3 -# -############################################################################### - -set testdir [file dirname $argv0] -source $testdir/tester.tcl - -ifcapable !stat3 { - finish_test - return -} - -do_execsql_test wild001.01 { - CREATE TABLE "items" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "secid" integer, "parent_id" integer, "metadata_type" integer, "guid" varchar(255), "media_item_count" integer, "title" varchar(255), "title_sort" varchar(255) COLLATE NOCASE, "original_title" varchar(255), "studio" varchar(255), "rating" float, "rating_count" integer, "tagline" varchar(255), "summary" text, "trivia" text, "quotes" text, "content_rating" varchar(255), "content_rating_age" integer, "index" integer, "absolute_index" integer, "duration" integer, "user_thumb_url" varchar(255), "user_art_url" varchar(255), "user_banner_url" varchar(255), "user_music_url" varchar(255), "user_fields" varchar(255), "tags_genre" varchar(255), "tags_collection" varchar(255), "tags_director" varchar(255), "tags_writer" varchar(255), "tags_star" varchar(255), "originally_available_at" datetime, "available_at" datetime, "expires_at" datetime, "refreshed_at" datetime, "year" integer, "added_at" datetime, "created_at" datetime, "updated_at" datetime, "deleted_at" datetime, "tags_country" varchar(255), "extra_data" varchar(255), "hash" varchar(255)); - CREATE INDEX "i_secid" ON "items" ("secid" ); - CREATE INDEX "i_parent_id" ON "items" ("parent_id" ); - CREATE INDEX "i_created_at" ON "items" ("created_at" ); - CREATE INDEX "i_index" ON "items" ("index" ); - CREATE INDEX "i_title" ON "items" ("title" ); - CREATE INDEX "i_title_sort" ON "items" ("title_sort" ); - CREATE INDEX "i_guid" ON "items" ("guid" ); - CREATE INDEX "i_metadata_type" ON "items" ("metadata_type" ); - CREATE INDEX "i_deleted_at" ON "items" ("deleted_at" ); - CREATE INDEX "i_secid_ex1" ON "items" ("secid", "metadata_type", "added_at" ); - CREATE INDEX "i_hash" ON "items" ("hash" ); - CREATE TABLE "settings" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "account_id" integer, "guid" varchar(255), "rating" float, "view_offset" integer, "view_count" integer, "last_viewed_at" datetime, "created_at" datetime, "updated_at" datetime); - CREATE INDEX "s_account_id" ON "settings" ("account_id" ); - CREATE INDEX "s_guid" ON "settings" ("guid" ); - ANALYZE; - INSERT INTO sqlite_stat1 VALUES('settings','s_guid','4740 1'); - INSERT INTO sqlite_stat1 VALUES('settings','s_account_id','4740 4740'); - INSERT INTO sqlite_stat1 VALUES('items','i_hash','27316 2'); - INSERT INTO sqlite_stat1 VALUES('items','i_secid_ex1','27316 6829 4553 3'); - INSERT INTO sqlite_stat1 VALUES('items','i_deleted_at','27316 27316'); - INSERT INTO sqlite_stat1 VALUES('items','i_metadata_type','27316 6829'); - INSERT INTO sqlite_stat1 VALUES('items','i_guid','27316 2'); - INSERT INTO sqlite_stat1 VALUES('items','i_title_sort','27316 2'); - INSERT INTO sqlite_stat1 VALUES('items','i_title','27316 2'); - INSERT INTO sqlite_stat1 VALUES('items','i_index','27316 144'); - INSERT INTO sqlite_stat1 VALUES('items','i_created_at','27316 2'); - INSERT INTO sqlite_stat1 VALUES('items','i_parent_id','27316 15'); - INSERT INTO sqlite_stat1 VALUES('items','i_secid','27316 6829'); - INSERT INTO sqlite_stat3 VALUES('settings','s_guid',1,150,150,'com.plexapp.agents.thetvdb://153021/2/9?lang=en'); - INSERT INTO sqlite_stat3 VALUES('settings','s_guid',1,198,198,'com.plexapp.agents.thetvdb://194031/1/10?lang=en'); - INSERT INTO sqlite_stat3 VALUES('settings','s_guid',1,526,526,'com.plexapp.agents.thetvdb://71256/12/92?lang=en'); - INSERT INTO sqlite_stat3 VALUES('settings','s_guid',1,923,923,'com.plexapp.agents.thetvdb://71256/15/16?lang=en'); - INSERT INTO sqlite_stat3 VALUES('settings','s_guid',1,1008,1008,'com.plexapp.agents.thetvdb://71256/15/93?lang=en'); - INSERT INTO sqlite_stat3 VALUES('settings','s_guid',1,1053,1053,'com.plexapp.agents.thetvdb://71256/16/21?lang=en'); - INSERT INTO sqlite_stat3 VALUES('settings','s_guid',1,1068,1068,'com.plexapp.agents.thetvdb://71256/16/35?lang=en'); - INSERT INTO sqlite_stat3 VALUES('settings','s_guid',1,1235,1235,'com.plexapp.agents.thetvdb://71256/17/44?lang=en'); - INSERT INTO sqlite_stat3 VALUES('settings','s_guid',1,1255,1255,'com.plexapp.agents.thetvdb://71256/17/62?lang=en'); - INSERT INTO sqlite_stat3 VALUES('settings','s_guid',1,1573,1573,'com.plexapp.agents.thetvdb://71663/20/9?lang=en'); - INSERT INTO sqlite_stat3 VALUES('settings','s_guid',1,1580,1580,'com.plexapp.agents.thetvdb://71663/21/16?lang=en'); - INSERT INTO sqlite_stat3 VALUES('settings','s_guid',1,2000,2000,'com.plexapp.agents.thetvdb://73141/9/8?lang=en'); - INSERT INTO sqlite_stat3 VALUES('settings','s_guid',1,2107,2107,'com.plexapp.agents.thetvdb://73244/6/17?lang=en'); - INSERT INTO sqlite_stat3 VALUES('settings','s_guid',1,2256,2256,'com.plexapp.agents.thetvdb://74845/4/7?lang=en'); - INSERT INTO sqlite_stat3 VALUES('settings','s_guid',1,2408,2408,'com.plexapp.agents.thetvdb://75978/2/21?lang=en'); - INSERT INTO sqlite_stat3 VALUES('settings','s_guid',1,2634,2634,'com.plexapp.agents.thetvdb://79126/1/1?lang=en'); - INSERT INTO sqlite_stat3 VALUES('settings','s_guid',1,2962,2962,'com.plexapp.agents.thetvdb://79274/3/94?lang=en'); - INSERT INTO sqlite_stat3 VALUES('settings','s_guid',1,3160,3160,'com.plexapp.agents.thetvdb://79274/5/129?lang=en'); - INSERT INTO sqlite_stat3 VALUES('settings','s_guid',1,3161,3161,'com.plexapp.agents.thetvdb://79274/5/12?lang=en'); - INSERT INTO sqlite_stat3 VALUES('settings','s_guid',1,3688,3688,'com.plexapp.agents.thetvdb://79274/8/62?lang=en'); - INSERT INTO sqlite_stat3 VALUES('settings','s_guid',1,3714,3714,'com.plexapp.agents.thetvdb://79274/8/86?lang=en'); - INSERT INTO sqlite_stat3 VALUES('settings','s_guid',1,4002,4002,'com.plexapp.agents.thetvdb://79590/13/17?lang=en'); - INSERT INTO sqlite_stat3 VALUES('settings','s_guid',1,4215,4215,'com.plexapp.agents.thetvdb://80727/3/6?lang=en'); - INSERT INTO sqlite_stat3 VALUES('settings','s_guid',1,4381,4381,'com.plexapp.agents.thetvdb://83462/3/24?lang=en'); - INSERT INTO sqlite_stat3 VALUES('settings','s_account_id',4740,0,0,1); - INSERT INTO sqlite_stat3 VALUES('items','i_hash',1,1879,1879,'1113f632ccd52ec8b8d7ca3d6d56da4701e48018'); - INSERT INTO sqlite_stat3 VALUES('items','i_hash',1,2721,2721,'1936154b97bb5567163edaebc2806830ae419ccf'); - INSERT INTO sqlite_stat3 VALUES('items','i_hash',1,3035,3035,'1c122331d4b7bfa0dc2c003ab5fb4f7152b9987a'); - INSERT INTO sqlite_stat3 VALUES('items','i_hash',2,3393,3393,'1f81bdbc9acc3321dc592b1a109ca075731b549a'); - INSERT INTO sqlite_stat3 VALUES('items','i_hash',1,6071,6070,'393cf7713efb4519c7a3d1d5403f0d945d15a16a'); - INSERT INTO sqlite_stat3 VALUES('items','i_hash',1,7462,7461,'4677dd37011f8bd9ae7fbbdd3af6dcd8a5b4ab2d'); - INSERT INTO sqlite_stat3 VALUES('items','i_hash',2,8435,8434,'4ffa339485334e81a5e12e03a63b6508d76401cf'); - INSERT INTO sqlite_stat3 VALUES('items','i_hash',2,8716,8714,'52a093852e6599dd5004857b7ff5b5b82c7cdb25'); - INSERT INTO sqlite_stat3 VALUES('items','i_hash',1,9107,9104,'561183e39f866d97ec728e9ff16ac4ad01466111'); - INSERT INTO sqlite_stat3 VALUES('items','i_hash',2,10942,10939,'66e99b72e29610f49499ae09ee04a376210d1f08'); - INSERT INTO sqlite_stat3 VALUES('items','i_hash',1,12143,12139,'71f0602427e173dc2c551535f73fdb6885fe4302'); - INSERT INTO sqlite_stat3 VALUES('items','i_hash',2,14962,14958,'8ca8e4dfba696019830c19ab8a32c7ece9d8534b'); - INSERT INTO sqlite_stat3 VALUES('items','i_hash',1,15179,15174,'8ebf1a5cf33f8ada1fc5853ac06ac4d7e074f825'); - INSERT INTO sqlite_stat3 VALUES('items','i_hash',1,15375,15370,'908bc211bebdf21c79d2d2b54ebaa442ac1f5cae'); - INSERT INTO sqlite_stat3 VALUES('items','i_hash',1,18215,18210,'ab29e4e18ec5a14fef95aa713d69e31c045a22c1'); - INSERT INTO sqlite_stat3 VALUES('items','i_hash',1,18615,18610,'ae84c008cc0c338bf4f28d798a88575746452f6d'); - INSERT INTO sqlite_stat3 VALUES('items','i_hash',1,18649,18644,'aec7c901353e115aa5307e94018ba7507bec3a45'); - INSERT INTO sqlite_stat3 VALUES('items','i_hash',2,19517,19512,'b75025fbf2e9c504e3c1197ff1b69250402a31f8'); - INSERT INTO sqlite_stat3 VALUES('items','i_hash',1,21251,21245,'c7d32f0e3a8f3a0a3dbd00833833d2ccee62f0fd'); - INSERT INTO sqlite_stat3 VALUES('items','i_hash',2,23616,23610,'dd5ff61479a9bd4100de802515d9dcf72d46f07a'); - INSERT INTO sqlite_stat3 VALUES('items','i_hash',1,24287,24280,'e3db00034301b7555419d4ef6f64769298d5845e'); - INSERT INTO sqlite_stat3 VALUES('items','i_hash',1,24949,24942,'ea336abd197ecd7013854a25a4f4eb9dea7927c6'); - INSERT INTO sqlite_stat3 VALUES('items','i_hash',1,25574,25567,'f018ea5182ec3f32768ca1c3cefbf3ad160ec20b'); - INSERT INTO sqlite_stat3 VALUES('items','i_hash',2,26139,26132,'f53709a8d81c12cb0f4f8d58004a25dd063de67c'); - INSERT INTO sqlite_stat3 VALUES('items','i_secid_ex1',25167,0,0,2); - INSERT INTO sqlite_stat3 VALUES('items','i_secid_ex1',736,25167,1,3); - INSERT INTO sqlite_stat3 VALUES('items','i_secid_ex1',15,25903,2,4); - INSERT INTO sqlite_stat3 VALUES('items','i_secid_ex1',1398,25918,3,5); - INSERT INTO sqlite_stat3 VALUES('items','i_deleted_at',27316,0,0,NULL); - INSERT INTO sqlite_stat3 VALUES('items','i_metadata_type',2149,0,0,1); - INSERT INTO sqlite_stat3 VALUES('items','i_metadata_type',411,2149,1,2); - INSERT INTO sqlite_stat3 VALUES('items','i_metadata_type',1440,2560,2,3); - INSERT INTO sqlite_stat3 VALUES('items','i_metadata_type',23316,4000,3,4); - INSERT INTO sqlite_stat3 VALUES('items','i_guid',1,215,215,'com.plexapp.agents.imdb://tt0065702?lang=en'); - INSERT INTO sqlite_stat3 VALUES('items','i_guid',2,711,711,'com.plexapp.agents.imdb://tt0198781?lang=en'); - INSERT INTO sqlite_stat3 VALUES('items','i_guid',2,987,986,'com.plexapp.agents.imdb://tt0454876?lang=en'); - INSERT INTO sqlite_stat3 VALUES('items','i_guid',2,1004,1002,'com.plexapp.agents.imdb://tt0464154?lang=en'); - INSERT INTO sqlite_stat3 VALUES('items','i_guid',2,1056,1053,'com.plexapp.agents.imdb://tt0499549?lang=en'); - INSERT INTO sqlite_stat3 VALUES('items','i_guid',2,1120,1116,'com.plexapp.agents.imdb://tt0903624?lang=en'); - INSERT INTO sqlite_stat3 VALUES('items','i_guid',2,1250,1245,'com.plexapp.agents.imdb://tt1268799?lang=en'); - INSERT INTO sqlite_stat3 VALUES('items','i_guid',2,1270,1264,'com.plexapp.agents.imdb://tt1320261?lang=en'); - INSERT INTO sqlite_stat3 VALUES('items','i_guid',2,1376,1369,'com.plexapp.agents.imdb://tt1772341?lang=en'); - INSERT INTO sqlite_stat3 VALUES('items','i_guid',1,3035,3027,'com.plexapp.agents.thetvdb://153021/3/14?lang=en'); - INSERT INTO sqlite_stat3 VALUES('items','i_guid',1,6071,6063,'com.plexapp.agents.thetvdb://71173/1/18?lang=en'); - INSERT INTO sqlite_stat3 VALUES('items','i_guid',1,6342,6334,'com.plexapp.agents.thetvdb://71256/13/4?lang=en'); - INSERT INTO sqlite_stat3 VALUES('items','i_guid',1,9107,9099,'com.plexapp.agents.thetvdb://72389/2/19?lang=en'); - INSERT INTO sqlite_stat3 VALUES('items','i_guid',1,11740,11732,'com.plexapp.agents.thetvdb://73893/2/13?lang=en'); - INSERT INTO sqlite_stat3 VALUES('items','i_guid',1,12143,12135,'com.plexapp.agents.thetvdb://73976/4/23?lang=en'); - INSERT INTO sqlite_stat3 VALUES('items','i_guid',1,15179,15171,'com.plexapp.agents.thetvdb://75897/16/12?lang=en'); - INSERT INTO sqlite_stat3 VALUES('items','i_guid',1,17408,17400,'com.plexapp.agents.thetvdb://76808/2/16?lang=en'); - INSERT INTO sqlite_stat3 VALUES('items','i_guid',1,17984,17976,'com.plexapp.agents.thetvdb://77068/1/16?lang=en'); - INSERT INTO sqlite_stat3 VALUES('items','i_guid',1,18215,18207,'com.plexapp.agents.thetvdb://77259/1/1?lang=en'); - INSERT INTO sqlite_stat3 VALUES('items','i_guid',1,21251,21243,'com.plexapp.agents.thetvdb://78957/8/2?lang=en'); - INSERT INTO sqlite_stat3 VALUES('items','i_guid',1,24287,24279,'com.plexapp.agents.thetvdb://80337/5/8?lang=en'); - INSERT INTO sqlite_stat3 VALUES('items','i_guid',1,25513,25505,'com.plexapp.agents.thetvdb://82226/6?lang=en'); - INSERT INTO sqlite_stat3 VALUES('items','i_guid',1,25548,25540,'com.plexapp.agents.thetvdb://82339/2/10?lang=en'); - INSERT INTO sqlite_stat3 VALUES('items','i_guid',1,26770,26762,'com.plexapp.agents.thetvdb://86901/1/3?lang=en'); - INSERT INTO sqlite_stat3 VALUES('items','i_title_sort',1524,0,0,''); - INSERT INTO sqlite_stat3 VALUES('items','i_title_sort',2,3034,1391,'Attack of the Giant Squid'); - INSERT INTO sqlite_stat3 VALUES('items','i_title_sort',51,4742,2895,'Brad Sherwood'); - INSERT INTO sqlite_stat3 VALUES('items','i_title_sort',11,4912,2996,'Brian Williams'); - INSERT INTO sqlite_stat3 VALUES('items','i_title_sort',39,5847,3857,'Chip Esten'); - INSERT INTO sqlite_stat3 VALUES('items','i_title_sort',1,6071,4015,'Chuck Versus the DeLorean'); - INSERT INTO sqlite_stat3 VALUES('items','i_title_sort',12,7625,5436,'Denny Siegel'); - INSERT INTO sqlite_stat3 VALUES('items','i_title_sort',30,8924,6618,'Episode 1'); - INSERT INTO sqlite_stat3 VALUES('items','i_title_sort',29,9015,6629,'Episode 2'); - INSERT INTO sqlite_stat3 VALUES('items','i_title_sort',32,9082,6643,'Episode 3'); - INSERT INTO sqlite_stat3 VALUES('items','i_title_sort',28,9135,6654,'Episode 4'); - INSERT INTO sqlite_stat3 VALUES('items','i_title_sort',26,9183,6665,'Episode 5'); - INSERT INTO sqlite_stat3 VALUES('items','i_title_sort',27,9229,6677,'Episode 6'); - INSERT INTO sqlite_stat3 VALUES('items','i_title_sort',22,9266,6688,'Episode 7'); - INSERT INTO sqlite_stat3 VALUES('items','i_title_sort',20,9298,6699,'Episode 8'); - INSERT INTO sqlite_stat3 VALUES('items','i_title_sort',55,11750,8817,'Greg Proops'); - INSERT INTO sqlite_stat3 VALUES('items','i_title_sort',1,12143,9120,'Hardware Jungle'); - INSERT INTO sqlite_stat3 VALUES('items','i_title_sort',33,14712,11435,'Kathy Greenwood'); - INSERT INTO sqlite_stat3 VALUES('items','i_title_sort',3,15179,11840,'Last Call'); - INSERT INTO sqlite_stat3 VALUES('items','i_title_sort',1,18215,14601,'Nature or Nurture?'); - INSERT INTO sqlite_stat3 VALUES('items','i_title_sort',12,18241,14623,'Neil DeGrasse Tyson'); - INSERT INTO sqlite_stat3 VALUES('items','i_title_sort',68,19918,16144,'Pilot'); - INSERT INTO sqlite_stat3 VALUES('items','i_title_sort',7,21251,17298,'Reza Aslan'); - INSERT INTO sqlite_stat3 VALUES('items','i_title_sort',1,24287,20035,'Technoviking'); - INSERT INTO sqlite_stat3 VALUES('items','i_title',1524,0,0,''); - INSERT INTO sqlite_stat3 VALUES('items','i_title',1,3035,1429,'Anderson Can''t Dance'); - INSERT INTO sqlite_stat3 VALUES('items','i_title',51,4782,2991,'Brad Sherwood'); - INSERT INTO sqlite_stat3 VALUES('items','i_title',11,4936,3079,'Brian Williams'); - INSERT INTO sqlite_stat3 VALUES('items','i_title',39,5694,3783,'Chip Esten'); - INSERT INTO sqlite_stat3 VALUES('items','i_title',1,6071,4100,'Clive Warren'); - INSERT INTO sqlite_stat3 VALUES('items','i_title',12,7144,5078,'Denny Siegel'); - INSERT INTO sqlite_stat3 VALUES('items','i_title',30,8249,6097,'Episode 1'); - INSERT INTO sqlite_stat3 VALUES('items','i_title',29,8340,6108,'Episode 2'); - INSERT INTO sqlite_stat3 VALUES('items','i_title',32,8407,6122,'Episode 3'); - INSERT INTO sqlite_stat3 VALUES('items','i_title',28,8460,6133,'Episode 4'); - INSERT INTO sqlite_stat3 VALUES('items','i_title',26,8508,6144,'Episode 5'); - INSERT INTO sqlite_stat3 VALUES('items','i_title',27,8554,6156,'Episode 6'); - INSERT INTO sqlite_stat3 VALUES('items','i_title',22,8591,6167,'Episode 7'); - INSERT INTO sqlite_stat3 VALUES('items','i_title',20,8623,6178,'Episode 8'); - INSERT INTO sqlite_stat3 VALUES('items','i_title',1,9107,6537,'Fat Albert and the Cosby Kids'); - INSERT INTO sqlite_stat3 VALUES('items','i_title',55,10539,7843,'Greg Proops'); - INSERT INTO sqlite_stat3 VALUES('items','i_title',1,12143,9276,'Iron Age Remains'); - INSERT INTO sqlite_stat3 VALUES('items','i_title',33,13118,10143,'Kathy Greenwood'); - INSERT INTO sqlite_stat3 VALUES('items','i_title',1,15179,11972,'Mink'); - INSERT INTO sqlite_stat3 VALUES('items','i_title',68,17411,14035,'Pilot'); - INSERT INTO sqlite_stat3 VALUES('items','i_title',2,18214,14727,'Reflections'); - INSERT INTO sqlite_stat3 VALUES('items','i_title',4,21250,17481,'The Apartment'); - INSERT INTO sqlite_stat3 VALUES('items','i_title',1,24287,20283,'The Simpsons Already Did It'); - INSERT INTO sqlite_stat3 VALUES('items','i_index',4315,95,2,1); - INSERT INTO sqlite_stat3 VALUES('items','i_index',1553,4410,3,2); - INSERT INTO sqlite_stat3 VALUES('items','i_index',1485,5963,4,3); - INSERT INTO sqlite_stat3 VALUES('items','i_index',1414,7448,5,4); - INSERT INTO sqlite_stat3 VALUES('items','i_index',1367,8862,6,5); - INSERT INTO sqlite_stat3 VALUES('items','i_index',1328,10229,7,6); - INSERT INTO sqlite_stat3 VALUES('items','i_index',1161,11557,8,7); - INSERT INTO sqlite_stat3 VALUES('items','i_index',1108,12718,9,8); - INSERT INTO sqlite_stat3 VALUES('items','i_index',1033,13826,10,9); - INSERT INTO sqlite_stat3 VALUES('items','i_index',1014,14859,11,10); - INSERT INTO sqlite_stat3 VALUES('items','i_index',929,15873,12,11); - INSERT INTO sqlite_stat3 VALUES('items','i_index',906,16802,13,12); - INSERT INTO sqlite_stat3 VALUES('items','i_index',844,17708,14,13); - INSERT INTO sqlite_stat3 VALUES('items','i_index',690,18552,15,14); - INSERT INTO sqlite_stat3 VALUES('items','i_index',655,19242,16,15); - INSERT INTO sqlite_stat3 VALUES('items','i_index',625,19897,17,16); - INSERT INTO sqlite_stat3 VALUES('items','i_index',579,20522,18,17); - INSERT INTO sqlite_stat3 VALUES('items','i_index',555,21101,19,18); - INSERT INTO sqlite_stat3 VALUES('items','i_index',526,21656,20,19); - INSERT INTO sqlite_stat3 VALUES('items','i_index',501,22182,21,20); - INSERT INTO sqlite_stat3 VALUES('items','i_index',459,22683,22,21); - INSERT INTO sqlite_stat3 VALUES('items','i_index',439,23142,23,22); - INSERT INTO sqlite_stat3 VALUES('items','i_index',315,23581,24,23); - INSERT INTO sqlite_stat3 VALUES('items','i_index',192,24177,26,25); - INSERT INTO sqlite_stat3 VALUES('items','i_created_at',1851,0,0,NULL); - INSERT INTO sqlite_stat3 VALUES('items','i_created_at',373,1857,2,'2011-10-22 14:54:39'); - INSERT INTO sqlite_stat3 VALUES('items','i_created_at',595,2230,3,'2011-10-22 14:54:41'); - INSERT INTO sqlite_stat3 VALUES('items','i_created_at',337,2825,4,'2011-10-22 14:54:43'); - INSERT INTO sqlite_stat3 VALUES('items','i_created_at',361,3378,8,'2011-10-22 14:54:54'); - INSERT INTO sqlite_stat3 VALUES('items','i_created_at',160,3739,9,'2011-10-22 14:54:56'); - INSERT INTO sqlite_stat3 VALUES('items','i_created_at',315,4000,11,'2011-10-22 14:54:59'); - INSERT INTO sqlite_stat3 VALUES('items','i_created_at',321,4334,13,'2011-10-22 14:55:02'); - INSERT INTO sqlite_stat3 VALUES('items','i_created_at',1292,4723,16,'2011-10-22 14:55:06'); - INSERT INTO sqlite_stat3 VALUES('items','i_created_at',161,6015,17,'2011-10-22 14:55:07'); - INSERT INTO sqlite_stat3 VALUES('items','i_created_at',1,9107,2677,'2012-09-04 18:07:50'); - INSERT INTO sqlite_stat3 VALUES('items','i_created_at',313,9717,3270,'2012-10-18 16:50:21'); - INSERT INTO sqlite_stat3 VALUES('items','i_created_at',450,10030,3271,'2012-10-18 16:50:22'); - INSERT INTO sqlite_stat3 VALUES('items','i_created_at',389,10668,3275,'2012-10-18 16:50:26'); - INSERT INTO sqlite_stat3 VALUES('items','i_created_at',796,11057,3276,'2012-10-18 16:51:06'); - INSERT INTO sqlite_stat3 VALUES('items','i_created_at',161,12041,3280,'2012-10-19 19:52:37'); - INSERT INTO sqlite_stat3 VALUES('items','i_created_at',135,13281,4186,'2013-02-19 00:56:10'); - INSERT INTO sqlite_stat3 VALUES('items','i_created_at',1063,13416,4187,'2013-02-19 00:56:11'); - INSERT INTO sqlite_stat3 VALUES('items','i_created_at',797,14479,4188,'2013-02-19 00:56:13'); - INSERT INTO sqlite_stat3 VALUES('items','i_created_at',147,15276,4189,'2013-02-19 00:56:15'); - INSERT INTO sqlite_stat3 VALUES('items','i_created_at',346,15423,4190,'2013-02-19 00:56:16'); - INSERT INTO sqlite_stat3 VALUES('items','i_created_at',1,18215,6436,'2013-05-05 14:09:54'); - INSERT INTO sqlite_stat3 VALUES('items','i_created_at',2,21251,8122,'2013-05-24 15:25:45'); - INSERT INTO sqlite_stat3 VALUES('items','i_created_at',1,24287,11116,'2013-05-26 14:17:39'); - INSERT INTO sqlite_stat3 VALUES('items','i_parent_id',2560,0,0,NULL); - INSERT INTO sqlite_stat3 VALUES('items','i_parent_id',18,3022,31,2350); - INSERT INTO sqlite_stat3 VALUES('items','i_parent_id',10,6068,285,8150); - INSERT INTO sqlite_stat3 VALUES('items','i_parent_id',158,6346,315,8949); - INSERT INTO sqlite_stat3 VALUES('items','i_parent_id',34,9094,562,18831); - INSERT INTO sqlite_stat3 VALUES('items','i_parent_id',20,12139,794,22838); - INSERT INTO sqlite_stat3 VALUES('items','i_parent_id',134,14033,886,24739); - INSERT INTO sqlite_stat3 VALUES('items','i_parent_id',159,14167,887,24740); - INSERT INTO sqlite_stat3 VALUES('items','i_parent_id',161,14326,888,24741); - INSERT INTO sqlite_stat3 VALUES('items','i_parent_id',161,14487,889,24742); - INSERT INTO sqlite_stat3 VALUES('items','i_parent_id',124,14648,890,24743); - INSERT INTO sqlite_stat3 VALUES('items','i_parent_id',157,14772,891,24744); - INSERT INTO sqlite_stat3 VALUES('items','i_parent_id',126,15043,894,24747); - INSERT INTO sqlite_stat3 VALUES('items','i_parent_id',40,15169,895,24748); - INSERT INTO sqlite_stat3 VALUES('items','i_parent_id',161,15243,898,24753); - INSERT INTO sqlite_stat3 VALUES('items','i_parent_id',138,15404,899,24754); - INSERT INTO sqlite_stat3 VALUES('items','i_parent_id',160,15542,900,24755); - INSERT INTO sqlite_stat3 VALUES('items','i_parent_id',161,15702,901,24756); - INSERT INTO sqlite_stat3 VALUES('items','i_parent_id',161,15863,902,24757); - INSERT INTO sqlite_stat3 VALUES('items','i_parent_id',124,16024,903,24758); - INSERT INTO sqlite_stat3 VALUES('items','i_parent_id',155,16148,904,24759); - INSERT INTO sqlite_stat3 VALUES('items','i_parent_id',26,18208,1043,29704); - INSERT INTO sqlite_stat3 VALUES('items','i_parent_id',2,21251,1282,32952); - INSERT INTO sqlite_stat3 VALUES('items','i_parent_id',13,24279,1583,36068); - INSERT INTO sqlite_stat3 VALUES('items','i_secid',25167,0,0,2); - INSERT INTO sqlite_stat3 VALUES('items','i_secid',736,25167,1,3); - INSERT INTO sqlite_stat3 VALUES('items','i_secid',15,25903,2,4); - INSERT INTO sqlite_stat3 VALUES('items','i_secid',1398,25918,3,5); - ANALYZE sqlite_master; - - explain query plan - select items.title - from items - join items as child on child.parent_id=items.id - join items as grandchild on grandchild.parent_id=child.id - join settings - on settings.guid=grandchild.guid - and settings.account_id=1 - where items.metadata_type=2 - and items.secid=2 - and settings.last_viewed_at is not null - group by items.id - order by settings.last_viewed_at desc - limit 10; -} [list \ - 0 0 3 {SEARCH TABLE settings USING INDEX s_account_id (account_id=?)} \ - 0 1 2 {SEARCH TABLE items AS grandchild USING INDEX i_guid (guid=?)} \ - 0 2 1 {SEARCH TABLE items AS child USING INTEGER PRIMARY KEY (rowid=?)} \ - 0 3 0 {SEARCH TABLE items USING INTEGER PRIMARY KEY (rowid=?)} \ - 0 0 0 {USE TEMP B-TREE FOR GROUP BY} \ - 0 0 0 {USE TEMP B-TREE FOR ORDER BY}] - - -finish_test diff --git a/test/win32heap.test b/test/win32heap.test index b92f8040e6..b62126b772 100644 --- a/test/win32heap.test +++ b/test/win32heap.test @@ -9,11 +9,10 @@ # #*********************************************************************** # This file implements regression tests for SQLite library. The -# focus of this script is recovery from transient manditory locks -# that sometimes appear on database files due to anti-virus software. +# focus of this script is the Win32 heap implementation. # -if {$tcl_platform(platform)!="windows"} return +if {$tcl_platform(platform) ne "windows"} return set testdir [file dirname $argv0] source $testdir/tester.tcl diff --git a/test/win32lock.test b/test/win32lock.test index d1f3d1a06e..40ff5d5127 100644 --- a/test/win32lock.test +++ b/test/win32lock.test @@ -12,8 +12,10 @@ # focus of this script is recovery from transient manditory locks # that sometimes appear on database files due to anti-virus software. # +# TESTRUNNER: slow +# -if {$tcl_platform(platform)!="windows"} return +if {$tcl_platform(platform) ne "windows"} return set testdir [file dirname $argv0] source $testdir/tester.tcl diff --git a/test/win32longpath.test b/test/win32longpath.test index 9e9ed359c6..2545c55a16 100644 --- a/test/win32longpath.test +++ b/test/win32longpath.test @@ -13,7 +13,7 @@ # by the "win32-longpath" VFS. # -if {$tcl_platform(platform)!="windows"} return +if {$tcl_platform(platform) ne "windows"} return set testdir [file dirname $argv0] source $testdir/tester.tcl @@ -24,7 +24,8 @@ do_test 1.0 { } win32 db close -set path [file nativename [get_pwd]] +set rawPath [get_pwd] +set path [file nativename $rawPath] sqlite3 db [file join $path test.db] -vfs win32-longpath do_test 1.1 { @@ -45,16 +46,32 @@ do_test 1.2 { } {1 2 3 4} set longPath(1) \\\\?\\$path\\[pid] -make_win32_dir $longPath(1) +set uriPath(1a) %5C%5C%3F%5C$path\\[pid] +set uriPath(1b) %5C%5C%3F%5C$rawPath/[pid] + +file mkdir $longPath(1) set longPath(2) $longPath(1)\\[string repeat X 255] -make_win32_dir $longPath(2) +set uriPath(2a) $uriPath(1a)\\[string repeat X 255] +set uriPath(2b) $uriPath(1b)/[string repeat X 255] + +file mkdir $longPath(2) set longPath(3) $longPath(2)\\[string repeat Y 255] -make_win32_dir $longPath(3) +set uriPath(3a) $uriPath(2a)\\[string repeat Y 255] +set uriPath(3b) $uriPath(2b)/[string repeat Y 255] + +file mkdir $longPath(3) set fileName $longPath(3)\\test.db +set uri(1a) file:$uriPath(3a)\\test.db +set uri(1b) file:$uriPath(3b)/test.db +set uri(1c) file:///$uriPath(3a)\\test.db +set uri(1d) file:///$uriPath(3b)/test.db +set uri(1e) file://localhost/$uriPath(3a)\\test.db +set uri(1f) file://localhost/$uriPath(3b)/test.db + do_test 1.3 { list [catch {sqlite3 db2 [string range $fileName 4 end]} msg] $msg } {1 {unable to open database file}} @@ -75,7 +92,6 @@ do_test 1.4 { } {5 6 7 8} db3 close -# puts " Database exists \{[exists_win32_path $fileName]\}" sqlite3 db3 $fileName -vfs win32-longpath @@ -98,13 +114,38 @@ do_test 1.6 { } {5 6 7 8 9 10 11 12} db3 close -# puts " Database exists \{[exists_win32_path $fileName]\}" -do_delete_win32_file $fileName -# puts " Files remaining \{[find_win32_file $longPath(3)\\*]\}" +# winrt platforms do not handle paths with unix-style '/' directory separators. +# +set lUri [list 1a 1b 1c 1d 1e 1f] +ifcapable winrt { set lUri [list 1a 1c 1e] } -do_remove_win32_dir $longPath(3) -do_remove_win32_dir $longPath(2) -do_remove_win32_dir $longPath(1) +foreach tn $lUri { + sqlite3 db3 $uri($tn) -vfs win32-longpath -uri 1 -translatefilename 0 + + do_test 1.7.$tn { + db3 eval { + SELECT x FROM t1 ORDER BY x; + } + } {5 6 7 8 9 10 11 12} + + db3 close +} + +# These over-length file and directory names are difficult to delete. +# The "file delete -force" might not work, depending on the TCL build +# being used. So first try to delete using the windows rmdir command. +# +set fd [open cleanup.bat w] +puts $fd "rmdir /q /s $longPath(1)" +close $fd +if {[catch {exec cleanup.bat} msg]} { + puts "Command \[cleanup.bat\] returns $msg" +} + +file delete -force $fileName +file delete -force $longPath(3) +file delete -force $longPath(2) +file delete -force $longPath(1) finish_test diff --git a/test/win32nolock.test b/test/win32nolock.test new file mode 100644 index 0000000000..e6ff548b46 --- /dev/null +++ b/test/win32nolock.test @@ -0,0 +1,126 @@ +# 2016 July 8 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. +# + +if {$tcl_platform(os) ne "Windows NT"} return + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix win32nolock + +do_test win32nolock-1.0 { + sqlite3 db test.db + execsql { + CREATE TABLE t1(a, b); + BEGIN; + INSERT INTO t1 VALUES(1, 2); + } +} {} + +do_test win32nolock-1.1 { + execsql COMMIT + catchsql { SELECT * FROM t1 } +} {0 {1 2}} + +db close + +do_test win32nolock-1.2 { + sqlite3 db test.db -vfs win32-none + sqlite3 db2 test.db -vfs win32-none + execsql { PRAGMA mmap_size = 0 } db2 + execsql { + BEGIN; + INSERT INTO t1 VALUES(3, 4); + } +} {} + +do_test win32nolock-1.3 { + execsql { SELECT * FROM t1 } +} {1 2 3 4} + +do_test win32nolock-1.4 { + execsql { SELECT * FROM t1; } db2 +} {1 2} + +do_test win32nolock-1.5 { + execsql { + BEGIN; + SELECT * FROM t1; + } db2 +} {1 2} + +do_test win32nolock-1.6 { + execsql COMMIT + execsql {SELECT * FROM t1} db2 +} {1 2} + +ifcapable memorymanage { + do_test win32nolock-1.7 { + sqlite3_release_memory 1000000 + execsql {SELECT * FROM t1} db2 + } {1 2 3 4} +} + +do_test win32nolock-1.8 { + db close + db2 close +} {} + +do_test win32nolock-1.9.1 { + sqlite3 db test.db + sqlite3 db2 test.db + list [catchsql { BEGIN EXCLUSIVE; } db] \ + [catchsql { BEGIN EXCLUSIVE; } db2] +} {{0 {}} {1 {database is locked}}} + +do_test win32nolock-1.9.2 { + db close + db2 close +} {} + +do_test win32nolock-1.10.1 { + sqlite3 db test.db -vfs win32-none + sqlite3 db2 test.db + list [catchsql { BEGIN EXCLUSIVE; } db] \ + [catchsql { BEGIN EXCLUSIVE; } db2] +} {{0 {}} {0 {}}} + +do_test win32nolock-1.10.2 { + db close + db2 close +} {} + +do_test win32nolock-1.11.1 { + sqlite3 db test.db + sqlite3 db2 test.db -vfs win32-none + list [catchsql { BEGIN EXCLUSIVE; } db] \ + [catchsql { BEGIN EXCLUSIVE; } db2] +} {{0 {}} {0 {}}} + +do_test win32nolock-1.11.2 { + db close + db2 close +} {} + +do_test win32nolock-1.12.1 { + sqlite3 db test.db -vfs win32-none + sqlite3 db2 test.db -vfs win32-none + list [catchsql { BEGIN EXCLUSIVE; } db] \ + [catchsql { BEGIN EXCLUSIVE; } db2] +} {{0 {}} {0 {}}} + +do_test win32nolock-1.12.2 { + db close + db2 close +} {} + +finish_test diff --git a/test/window1.test b/test/window1.test new file mode 100644 index 0000000000..db9af62ac8 --- /dev/null +++ b/test/window1.test @@ -0,0 +1,2427 @@ +# 2018 May 8 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix window1 + +ifcapable !windowfunc { + finish_test + return +} + +do_execsql_test 1.0 { + CREATE TABLE t1(a, b, c, d); + INSERT INTO t1 VALUES(1, 2, 3, 4); + INSERT INTO t1 VALUES(5, 6, 7, 8); + INSERT INTO t1 VALUES(9, 10, 11, 12); +} + +do_execsql_test 1.1 { + SELECT sum(b) OVER () FROM t1 +} {18 18 18} + +do_execsql_test 1.2 { + SELECT a, sum(b) OVER () FROM t1 +} {1 18 5 18 9 18} + +do_execsql_test 1.3 { + SELECT a, 4 + sum(b) OVER () FROM t1 +} {1 22 5 22 9 22} + +do_execsql_test 1.4 { + SELECT a + 4 + sum(b) OVER () FROM t1 +} {23 27 31} + +do_execsql_test 1.5 { + SELECT a, sum(b) OVER (PARTITION BY c) FROM t1 +} {1 2 5 6 9 10} + +foreach {tn sql} { + 1 "SELECT sum(b) OVER () FROM t1" + 2 "SELECT sum(b) OVER (PARTITION BY c) FROM t1" + 3 "SELECT sum(b) OVER (ORDER BY c) FROM t1" + 4 "SELECT sum(b) OVER (PARTITION BY d ORDER BY c) FROM t1" + 5 "SELECT sum(b) FILTER (WHERE a>0) OVER (PARTITION BY d ORDER BY c) FROM t1" + 6 "SELECT sum(b) OVER (ORDER BY c RANGE UNBOUNDED PRECEDING) FROM t1" + 7 "SELECT sum(b) OVER (ORDER BY c ROWS 45 PRECEDING) FROM t1" + 8 "SELECT sum(b) OVER (ORDER BY c RANGE CURRENT ROW) FROM t1" + 9 "SELECT sum(b) OVER (ORDER BY c RANGE BETWEEN UNBOUNDED PRECEDING + AND CURRENT ROW) FROM t1" + 10 "SELECT sum(b) OVER (ORDER BY c ROWS BETWEEN UNBOUNDED PRECEDING + AND UNBOUNDED FOLLOWING) FROM t1" +} { + do_test 2.$tn { lindex [catchsql $sql] 0 } 0 +} + +foreach {tn sql} { + 1 "SELECT * FROM t1 WHERE sum(b) OVER ()" + 2 "SELECT * FROM t1 GROUP BY sum(b) OVER ()" + 3 "SELECT * FROM t1 GROUP BY a HAVING sum(b) OVER ()" +} { + do_catchsql_test 3.$tn $sql {1 {misuse of window function sum()}} +} + +do_execsql_test 4.0 { + CREATE TABLE t2(a, b, c); + INSERT INTO t2 VALUES(0, 0, 0); + INSERT INTO t2 VALUES(1, 1, 1); + INSERT INTO t2 VALUES(2, 0, 2); + INSERT INTO t2 VALUES(3, 1, 0); + INSERT INTO t2 VALUES(4, 0, 1); + INSERT INTO t2 VALUES(5, 1, 2); + INSERT INTO t2 VALUES(6, 0, 0); +} + +do_execsql_test 4.1 { + SELECT a, sum(a) OVER (PARTITION BY b) FROM t2; +} { + 0 12 2 12 4 12 6 12 1 9 3 9 5 9 +} + +do_execsql_test 4.2 { + SELECT a, sum(a) OVER (PARTITION BY b) FROM t2 ORDER BY a; +} { + 0 12 1 9 2 12 3 9 4 12 5 9 6 12 +} + +do_execsql_test 4.3 { + SELECT a, sum(a) OVER () FROM t2 ORDER BY a; +} { + 0 21 1 21 2 21 3 21 4 21 5 21 6 21 +} + +do_execsql_test 4.4 { + SELECT a, sum(a) OVER (ORDER BY a) FROM t2; +} { + 0 0 1 1 2 3 3 6 4 10 5 15 6 21 +} + +do_execsql_test 4.5 { + SELECT a, sum(a) OVER (PARTITION BY b ORDER BY a) FROM t2 ORDER BY a +} { + 0 0 1 1 2 2 3 4 4 6 5 9 6 12 +} + +do_execsql_test 4.6 { + SELECT a, sum(a) OVER (PARTITION BY c ORDER BY a) FROM t2 ORDER BY a +} { + 0 0 1 1 2 2 3 3 4 5 5 7 6 9 +} + +do_execsql_test 4.7 { + SELECT a, sum(a) OVER (PARTITION BY b ORDER BY a DESC) FROM t2 ORDER BY a +} { + 0 12 1 9 2 12 3 8 4 10 5 5 6 6 +} + +do_execsql_test 4.8 { + SELECT a, + sum(a) OVER (PARTITION BY b ORDER BY a DESC), + sum(a) OVER (PARTITION BY c ORDER BY a) + FROM t2 ORDER BY a +} { + 0 12 0 + 1 9 1 + 2 12 2 + 3 8 3 + 4 10 5 + 5 5 7 + 6 6 9 +} + +do_execsql_test 4.9 { + SELECT a, + sum(a) OVER (ORDER BY a), + avg(a) OVER (ORDER BY a) + FROM t2 ORDER BY a +} { + 0 0 0.0 + 1 1 0.5 + 2 3 1.0 + 3 6 1.5 + 4 10 2.0 + 5 15 2.5 + 6 21 3.0 +} + +do_execsql_test 4.10.1 { + SELECT a, + count() OVER (ORDER BY a DESC), + string_agg(a, '.') OVER (ORDER BY a DESC) + FROM t2 ORDER BY a DESC +} { + 6 1 6 + 5 2 6.5 + 4 3 6.5.4 + 3 4 6.5.4.3 + 2 5 6.5.4.3.2 + 1 6 6.5.4.3.2.1 + 0 7 6.5.4.3.2.1.0 +} + +do_execsql_test 4.10.2 { + SELECT a, + count(*) OVER (ORDER BY a DESC), + group_concat(a, '.') OVER (ORDER BY a DESC) + FROM t2 ORDER BY a DESC +} { + 6 1 6 + 5 2 6.5 + 4 3 6.5.4 + 3 4 6.5.4.3 + 2 5 6.5.4.3.2 + 1 6 6.5.4.3.2.1 + 0 7 6.5.4.3.2.1.0 +} + +do_catchsql_test 5.1 { + SELECT ntile(0) OVER (ORDER BY a) FROM t2; +} {1 {argument of ntile must be a positive integer}} +do_catchsql_test 5.2 { + SELECT ntile(-1) OVER (ORDER BY a) FROM t2; +} {1 {argument of ntile must be a positive integer}} +do_catchsql_test 5.3 { + SELECT ntile('zbc') OVER (ORDER BY a) FROM t2; +} {1 {argument of ntile must be a positive integer}} +do_execsql_test 5.4 { + CREATE TABLE t4(a, b); + SELECT ntile(1) OVER (ORDER BY a) FROM t4; +} {} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 6.1 { + CREATE TABLE t1(x); + INSERT INTO t1 VALUES(7), (6), (5), (4), (3), (2), (1); + + CREATE TABLE t2(x); + INSERT INTO t2 VALUES('b'), ('a'); + + SELECT x, count(*) OVER (ORDER BY x) FROM t1; +} {1 1 2 2 3 3 4 4 5 5 6 6 7 7} + +do_execsql_test 6.2 { + SELECT * FROM t2, (SELECT x, count(*) OVER (ORDER BY x) FROM t1) + ORDER BY 1, 2; +} { + a 1 1 a 2 2 a 3 3 a 4 4 a 5 5 a 6 6 a 7 7 + b 1 1 b 2 2 b 3 3 b 4 4 b 5 5 b 6 6 b 7 7 +} + +do_catchsql_test 6.3 { + SELECT x, lag(x) FILTER (WHERE (x%2)=0) OVER w FROM t1 + WINDOW w AS (ORDER BY x) +} {1 {FILTER clause may only be used with aggregate window functions}} + +#------------------------------------------------------------------------- +# Attempt to use a window function as an aggregate. And other errors. +# +reset_db +do_execsql_test 7.0 { + CREATE TABLE t1(x, y); + INSERT INTO t1 VALUES(1, 2); + INSERT INTO t1 VALUES(3, 4); + INSERT INTO t1 VALUES(5, 6); + INSERT INTO t1 VALUES(7, 8); + INSERT INTO t1 VALUES(9, 10); +} + +do_catchsql_test 7.1.1 { + SELECT nth_value(x, 1) FROM t1; +} {1 {misuse of window function nth_value()}} +do_catchsql_test 7.1.2 { + SELECT * FROM t1 WHERE nth_value(x, 1) OVER (ORDER BY y); +} {1 {misuse of window function nth_value()}} +do_catchsql_test 7.1.3 { + SELECT count(*) FROM t1 GROUP BY y HAVING nth_value(x, 1) OVER (ORDER BY y); +} {1 {misuse of window function nth_value()}} +do_catchsql_test 7.1.4 { + SELECT count(*) FROM t1 GROUP BY nth_value(x, 1) OVER (ORDER BY y); +} {1 {misuse of window function nth_value()}} +do_catchsql_test 7.1.5 { + SELECT count(*) FROM t1 LIMIT nth_value(x, 1) OVER (); +} {1 {no such column: x}} +do_catchsql_test 7.1.6 { + SELECT trim(x) OVER (ORDER BY y) FROM t1; +} {1 {trim() may not be used as a window function}} +do_catchsql_test 7.1.7 { + SELECT max(x) OVER abc FROM t1 WINDOW def AS (ORDER BY y); +} {1 {no such window: abc}} +do_catchsql_test 7.1.8 { + SELECT row_number(x) OVER () FROM t1 +} {1 {wrong number of arguments to function row_number()}} + +do_execsql_test 7.2 { + SELECT + lead(y) OVER win, + lead(y, 2) OVER win, + lead(y, 3, 'default') OVER win + FROM t1 + WINDOW win AS (ORDER BY x) +} { + 4 6 8 6 8 10 8 10 default 10 {} default {} {} default +} + +do_execsql_test 7.3 { + SELECT row_number() OVER (ORDER BY x) FROM t1 +} {1 2 3 4 5} + +do_execsql_test 7.4 { + SELECT + row_number() OVER win, + lead(x) OVER win + FROM t1 + WINDOW win AS (ORDER BY x ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) +} {1 3 2 5 3 7 4 9 5 {}} + +#------------------------------------------------------------------------- +# Attempt to use a window function in a view. +# +do_execsql_test 8.0 { + CREATE TABLE t3(a, b, c); + + WITH s(i) AS ( VALUES(1) UNION ALL SELECT i+1 FROM s WHERE i<6 ) + INSERT INTO t3 SELECT i, i, i FROM s; + + CREATE VIEW v1 AS SELECT + sum(b) OVER (ORDER BY c), + min(b) OVER (ORDER BY c), + max(b) OVER (ORDER BY c) + FROM t3; + + CREATE VIEW v2 AS SELECT + sum(b) OVER win, + min(b) OVER win, + max(b) OVER win + FROM t3 + WINDOW win AS (ORDER BY c); +} + +do_execsql_test 8.1.1 { + SELECT * FROM v1 +} {1 1 1 3 1 2 6 1 3 10 1 4 15 1 5 21 1 6} +do_execsql_test 8.1.2 { + SELECT * FROM v2 +} {1 1 1 3 1 2 6 1 3 10 1 4 15 1 5 21 1 6} + +db close +sqlite3 db test.db +do_execsql_test 8.2.1 { + SELECT * FROM v1 +} {1 1 1 3 1 2 6 1 3 10 1 4 15 1 5 21 1 6} +do_execsql_test 8.2.2 { + SELECT * FROM v2 +} {1 1 1 3 1 2 6 1 3 10 1 4 15 1 5 21 1 6} + +#------------------------------------------------------------------------- +# Attempt to use a window function in a trigger. +# +do_execsql_test 9.0 { + CREATE TABLE t4(x, y); + INSERT INTO t4 VALUES(1, 'g'); + INSERT INTO t4 VALUES(2, 'i'); + INSERT INTO t4 VALUES(3, 'l'); + INSERT INTO t4 VALUES(4, 'g'); + INSERT INTO t4 VALUES(5, 'a'); + + CREATE TABLE t5(x, y, m); + CREATE TRIGGER t4i AFTER INSERT ON t4 BEGIN + DELETE FROM t5; + INSERT INTO t5 + SELECT x, y, max(y) OVER xyz FROM t4 + WINDOW xyz AS (PARTITION BY (x%2) ORDER BY x); + END; +} + +do_execsql_test 9.1.1 { + SELECT x, y, max(y) OVER xyz FROM t4 + WINDOW xyz AS (PARTITION BY (x%2) ORDER BY x) ORDER BY 1 +} {1 g g 2 i i 3 l l 4 g i 5 a l} + +do_execsql_test 9.1.2 { + INSERT INTO t4 VALUES(6, 'm'); + SELECT x, y, max(y) OVER xyz FROM t4 + WINDOW xyz AS (PARTITION BY (x%2) ORDER BY x) ORDER BY 1 +} {1 g g 2 i i 3 l l 4 g i 5 a l 6 m m} + +do_execsql_test 9.1.3 { + SELECT * FROM t5 ORDER BY 1 +} {1 g g 2 i i 3 l l 4 g i 5 a l 6 m m} + +do_execsql_test 9.2 { + WITH aaa(x, y, z) AS ( + SELECT x, y, max(y) OVER xyz FROM t4 + WINDOW xyz AS (PARTITION BY (x%2) ORDER BY x) + ) + SELECT * FROM aaa ORDER BY 1; +} {1 g g 2 i i 3 l l 4 g i 5 a l 6 m m} + +do_execsql_test 9.3 { + WITH aaa(x, y, z) AS ( + SELECT x, y, max(y) OVER xyz FROM t4 + WINDOW xyz AS (ORDER BY x) + ) + SELECT *, min(z) OVER (ORDER BY x) FROM aaa ORDER BY 1; +} {1 g g g 2 i i g 3 l l g 4 g l g 5 a l g 6 m m g} + +do_catchsql_test 9.4 { + -- 2021-04-17 dbsqlfuzz d9cf66100064952b66951845dfab41de1c124611 + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a,b,c,d); + DROP TABLE IF EXISTS t2; + CREATE TABLE t2(x,y); + CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN + INSERT INTO t2(x,y) + SELECT a, max(d) OVER w1 FROM t1 + WINDOW w1 AS (PARTITION BY EXISTS(SELECT 1 FROM t1 WHERE c=?1) ); + END; +} {1 {trigger cannot use variables}} + +do_catchsql_test 9.4.2 { + CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN + INSERT INTO t1(a,b) + SELECT a, max(d) OVER w1 FROM t1 + WINDOW w1 AS ( + ORDER BY a ROWS BETWEEN ? PRECEDING AND UNBOUNDED FOLLOWING + ); + END; +} {1 {trigger cannot use variables}} +do_catchsql_test 9.4.3 { + CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN + INSERT INTO t1(a,b) + SELECT a, max(d) OVER w1 FROM t1 + WINDOW w1 AS ( + ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND ? FOLLOWING + ); + END; +} {1 {trigger cannot use variables}} + +#------------------------------------------------------------------------- +# +do_execsql_test 10.0 { + CREATE TABLE sales(emp TEXT PRIMARY KEY, region, total); + INSERT INTO sales VALUES + ('Alice', 'North', 34), + ('Frank', 'South', 22), + ('Charles', 'North', 45), + ('Darrell', 'South', 8), + ('Grant', 'South', 23), + ('Brad' , 'North', 22), + ('Elizabeth', 'South', 99), + ('Horace', 'East', 1); +} + +# Best two salespeople from each region +# +do_execsql_test 10.1 { + SELECT emp, region, total FROM ( + SELECT + emp, region, total, + row_number() OVER (PARTITION BY region ORDER BY total DESC) AS rank + FROM sales + ) WHERE rank<=2 ORDER BY region, total DESC +} { + Horace East 1 + Charles North 45 + Alice North 34 + Elizabeth South 99 + Grant South 23 +} + +do_execsql_test 10.2 { + SELECT emp, region, sum(total) OVER win FROM sales + WINDOW win AS (PARTITION BY region ORDER BY total) +} { + Horace East 1 + Brad North 22 + Alice North 56 + Charles North 101 + Darrell South 8 + Frank South 30 + Grant South 53 + Elizabeth South 152 +} + +do_execsql_test 10.3 { + SELECT emp, region, sum(total) OVER win FROM sales + WINDOW win AS (PARTITION BY region ORDER BY total) + LIMIT 5 +} { + Horace East 1 + Brad North 22 + Alice North 56 + Charles North 101 + Darrell South 8 +} + +do_execsql_test 10.4 { + SELECT emp, region, sum(total) OVER win FROM sales + WINDOW win AS (PARTITION BY region ORDER BY total) + LIMIT 5 OFFSET 2 +} { + Alice North 56 + Charles North 101 + Darrell South 8 + Frank South 30 + Grant South 53 +} + +do_execsql_test 10.5 { + SELECT emp, region, sum(total) OVER win FROM sales + WINDOW win AS ( + PARTITION BY region ORDER BY total + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + ) +} { + Horace East 1 + Brad North 101 + Alice North 79 + Charles North 45 + Darrell South 152 + Frank South 144 + Grant South 122 + Elizabeth South 99 +} + +do_execsql_test 10.6 { + SELECT emp, region, sum(total) OVER win FROM sales + WINDOW win AS ( + PARTITION BY region ORDER BY total + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + ) LIMIT 5 OFFSET 2 +} { + Alice North 79 + Charles North 45 + Darrell South 152 + Frank South 144 + Grant South 122 +} + +do_execsql_test 10.7 { + SELECT emp, region, ( + SELECT sum(total) OVER ( + ORDER BY total RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) || outer.emp FROM sales + ) FROM sales AS outer; +} { + Alice North 254Alice + Frank South 254Frank + Charles North 254Charles + Darrell South 254Darrell + Grant South 254Grant + Brad North 254Brad + Elizabeth South 254Elizabeth + Horace East 254Horace +} + +do_execsql_test 10.8 { + SELECT emp, region, ( + SELECT sum(total) FILTER (WHERE sales.emp!=outer.emp) OVER ( + ORDER BY total RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) FROM sales + ) FROM sales AS outer; +} { + Alice North 220 + Frank South 232 + Charles North 209 + Darrell South 246 + Grant South 231 + Brad North 232 + Elizabeth South 155 + Horace East 253 +} + +#------------------------------------------------------------------------- +# Check that it is not possible to use a window function in a CREATE INDEX +# statement. +# +do_execsql_test 11.0 { CREATE TABLE t6(a, b, c); } + +do_catchsql_test 11.1 { + CREATE INDEX t6i ON t6(a) WHERE sum(b) OVER (); +} {1 {misuse of window function sum()}} +do_catchsql_test 11.2 { + CREATE INDEX t6i ON t6(a) WHERE lead(b) OVER (); +} {1 {misuse of window function lead()}} + +do_catchsql_test 11.3 { + CREATE INDEX t6i ON t6(sum(b) OVER ()); +} {1 {misuse of window function sum()}} +do_catchsql_test 11.4 { + CREATE INDEX t6i ON t6(lead(b) OVER ()); +} {1 {misuse of window function lead()}} + +# 2018-09-17 ticket 510cde277783b5fb5de628393959849dff377eb3 +# Endless loop on a query with window functions and a limit +# +do_execsql_test 12.100 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(id INT, b VARCHAR, c VARCHAR); + INSERT INTO t1 VALUES(1, 'A', 'one'); + INSERT INTO t1 VALUES(2, 'B', 'two'); + INSERT INTO t1 VALUES(3, 'C', 'three'); + INSERT INTO t1 VALUES(4, 'D', 'one'); + INSERT INTO t1 VALUES(5, 'E', 'two'); + SELECT id, b, lead(c,1) OVER(ORDER BY c) AS x + FROM t1 WHERE id>1 + ORDER BY b LIMIT 1; +} {2 B two} +do_execsql_test 12.110 { + INSERT INTO t1 VALUES(6, 'F', 'three'); + INSERT INTO t1 VALUES(7, 'G', 'one'); + SELECT id, b, lead(c,1) OVER(ORDER BY c) AS x + FROM t1 WHERE id>1 + ORDER BY b LIMIT 2; +} {2 B two 3 C three} + +#------------------------------------------------------------------------- + +do_execsql_test 13.1 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a int, b int); + INSERT INTO t1 VALUES(1,11); + INSERT INTO t1 VALUES(2,12); +} + +do_execsql_test 13.2.1 { + SELECT a, rank() OVER(ORDER BY b) FROM t1; + SELECT a, rank() OVER(ORDER BY b DESC) FROM t1; +} { + 1 1 2 2 2 1 1 2 +} +do_execsql_test 13.2.2 { + SELECT a, rank() OVER(ORDER BY b) FROM t1 + UNION ALL + SELECT a, rank() OVER(ORDER BY b DESC) FROM t1; +} { + 1 1 2 2 2 1 1 2 +} +do_execsql_test 13.3 { + SELECT a, rank() OVER(ORDER BY b) FROM t1 + UNION + SELECT a, rank() OVER(ORDER BY b DESC) FROM t1; +} { + 1 1 1 2 2 1 2 2 +} + +do_execsql_test 13.4 { + SELECT a, rank() OVER(ORDER BY b) FROM t1 + EXCEPT + SELECT a, rank() OVER(ORDER BY b DESC) FROM t1; +} { + 1 1 2 2 +} + +do_execsql_test 13.5 { + SELECT a, rank() OVER(ORDER BY b) FROM t1 + INTERSECT + SELECT a, rank() OVER(ORDER BY b DESC) FROM t1; +} {} + +# 2018-12-06 +# https://sqlite.org/src/info/f09fcd17810f65f7 +# Assertion fault when window functions are used. +# +# Root cause is the query flattener invoking sqlite3ExprDup() on +# expressions that contain subqueries with window functions. The +# sqlite3ExprDup() routine is not making correctly initializing +# Select.pWin field of the subqueries. +# +sqlite3 db :memory: +do_execsql_test 14.0 { + SELECT * FROM( + SELECT * FROM (SELECT 1 AS c) WHERE c IN ( + SELECT (row_number() OVER()) FROM (VALUES (0)) + ) + ); +} {1} +do_execsql_test 14.1 { + CREATE TABLE t1(x); INSERT INTO t1(x) VALUES(12345); + CREATE TABLE t2(c); INSERT INTO t2(c) VALUES(1); + SELECT y, y+1, y+2 FROM ( + SELECT c IN ( + SELECT (row_number() OVER()) FROM t1 + ) AS y FROM t2 + ); +} {1 2 3} + +# 2018-12-31 +# https://sqlite.org/src/info/d0866b26f83e9c55 +# Window function in correlated subquery causes assertion fault +# +do_catchsql_test 15.0 { + WITH t(id, parent) AS ( + SELECT CAST(1 AS INT), CAST(NULL AS INT) + UNION ALL + SELECT 2, NULL + UNION ALL + SELECT 3, 1 + UNION ALL + SELECT 4, 1 + UNION ALL + SELECT 5, 2 + UNION ALL + SELECT 6, 2 + ), q AS ( + SELECT t.*, ROW_NUMBER() OVER (ORDER BY t.id) AS rn + FROM t + WHERE parent IS NULL + UNION ALL + SELECT t.*, ROW_NUMBER() OVER (ORDER BY t.id) AS rn + FROM q + JOIN t + ON t.parent = q.id + ) + SELECT * + FROM q; +} {1 {cannot use window functions in recursive queries}} +do_execsql_test 15.1 { + DROP TABLE IF EXISTS t1; + DROP TABLE IF EXISTS t2; + CREATE TABLE t1(x); + INSERT INTO t1 VALUES('a'), ('b'), ('c'); + CREATE TABLE t2(a, b); + INSERT INTO t2 VALUES('X', 1), ('X', 2), ('Y', 2), ('Y', 3); + SELECT x, ( + SELECT sum(b) + OVER (PARTITION BY a ROWS BETWEEN UNBOUNDED PRECEDING + AND UNBOUNDED FOLLOWING) + FROM t2 WHERE b<x + ) FROM t1; +} {a 3 b 3 c 3} + +do_execsql_test 15.2 { + SELECT( + WITH c AS( + VALUES(1) + ) SELECT '' FROM c,c + ) x WHERE x+x; +} {} + +#------------------------------------------------------------------------- + +do_execsql_test 16.0 { + CREATE TABLE t7(a,b); + INSERT INTO t7(rowid, a, b) VALUES + (1, 1, 3), + (2, 10, 4), + (3, 100, 2); +} + +do_execsql_test 16.1 { + SELECT rowid, sum(a) OVER (PARTITION BY b IN (SELECT rowid FROM t7)) FROM t7; +} { + 2 10 + 1 101 + 3 101 +} + +do_execsql_test 16.2 { + SELECT rowid, sum(a) OVER w1 FROM t7 + WINDOW w1 AS (PARTITION BY b IN (SELECT rowid FROM t7)); +} { + 2 10 + 1 101 + 3 101 +} + +#------------------------------------------------------------------------- +do_execsql_test 17.0 { + CREATE TABLE t8(a); + INSERT INTO t8 VALUES(1), (2), (3); +} + +do_execsql_test 17.1 { + SELECT +sum(0) OVER () ORDER BY +sum(0) OVER (); +} {0} + +do_execsql_test 17.2 { + select +sum(a) OVER () FROM t8 ORDER BY +sum(a) OVER () DESC; +} {6 6 6} + +do_execsql_test 17.3 { + SELECT 10+sum(a) OVER (ORDER BY a) + FROM t8 + ORDER BY 10+sum(a) OVER (ORDER BY a) DESC; +} {16 13 11} + + +#------------------------------------------------------------------------- +# Test error cases from chaining window definitions. +# +reset_db +do_execsql_test 18.0 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT, c TEXT, d INTEGER); + INSERT INTO t1 VALUES(1, 'odd', 'one', 1); + INSERT INTO t1 VALUES(2, 'even', 'two', 2); + INSERT INTO t1 VALUES(3, 'odd', 'three', 3); + INSERT INTO t1 VALUES(4, 'even', 'four', 4); + INSERT INTO t1 VALUES(5, 'odd', 'five', 5); + INSERT INTO t1 VALUES(6, 'even', 'six', 6); +} + +foreach {tn sql error} { + 1 { + SELECT c, sum(d) OVER win2 FROM t1 + WINDOW win1 AS (ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING), + win2 AS (win1 ORDER BY b) + } {cannot override frame specification of window: win1} + + 2 { + SELECT c, sum(d) OVER win2 FROM t1 + WINDOW win1 AS (), + win2 AS (win4 ORDER BY b) + } {no such window: win4} + + 3 { + SELECT c, sum(d) OVER win2 FROM t1 + WINDOW win1 AS (), + win2 AS (win1 PARTITION BY d) + } {cannot override PARTITION clause of window: win1} + + 4 { + SELECT c, sum(d) OVER win2 FROM t1 + WINDOW win1 AS (ORDER BY b), + win2 AS (win1 ORDER BY d) + } {cannot override ORDER BY clause of window: win1} +} { + do_catchsql_test 18.1.$tn $sql [list 1 $error] +} + +foreach {tn sql error} { + 1 { + SELECT c, sum(d) OVER (win1 ORDER BY b) FROM t1 + WINDOW win1 AS (ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) + } {cannot override frame specification of window: win1} + + 2 { + SELECT c, sum(d) OVER (win4 ORDER BY b) FROM t1 + WINDOW win1 AS () + } {no such window: win4} + + 3 { + SELECT c, sum(d) OVER (win1 PARTITION BY d) FROM t1 + WINDOW win1 AS () + } {cannot override PARTITION clause of window: win1} + + 4 { + SELECT c, sum(d) OVER (win1 ORDER BY d) FROM t1 + WINDOW win1 AS (ORDER BY b) + } {cannot override ORDER BY clause of window: win1} +} { + do_catchsql_test 18.2.$tn $sql [list 1 $error] +} + +do_execsql_test 18.3.1 { + SELECT string_agg(c, '.') OVER (PARTITION BY b ORDER BY c) + FROM t1 +} {four four.six four.six.two five five.one five.one.three} + +do_execsql_test 18.3.2 { + SELECT group_concat(c, '.') OVER (win1 ORDER BY c) + FROM t1 + WINDOW win1 AS (PARTITION BY b) +} {four four.six four.six.two five five.one five.one.three} + +do_execsql_test 18.3.3 { + SELECT string_agg(c, '.') OVER win2 + FROM t1 + WINDOW win1 AS (PARTITION BY b), + win2 AS (win1 ORDER BY c) +} {four four.six four.six.two five five.one five.one.three} + +do_execsql_test 18.3.4 { + SELECT group_concat(c, '.') OVER (win2) + FROM t1 + WINDOW win1 AS (PARTITION BY b), + win2 AS (win1 ORDER BY c) +} {four four.six four.six.two five five.one five.one.three} + +do_execsql_test 18.3.5 { + SELECT string_agg(c, '.') OVER win5 + FROM t1 + WINDOW win1 AS (PARTITION BY b), + win2 AS (win1), + win3 AS (win2), + win4 AS (win3), + win5 AS (win4 ORDER BY c) +} {four four.six four.six.two five five.one five.one.three} + +#------------------------------------------------------------------------- +# Test RANGE <expr> PRECEDING/FOLLOWING when there are string, blob +# and NULL values in the dataset. +# +reset_db +do_execsql_test 19.0 { + CREATE TABLE t1(a, b); + INSERT INTO t1 VALUES + (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), + ('a', 6), ('b', 7), ('c', 8), ('d', 9), ('e', 10); +} +do_execsql_test 19.1 { + SELECT a, sum(b) OVER (ORDER BY a) FROM t1; +} {1 1 2 3 3 6 4 10 5 15 a 21 b 28 c 36 d 45 e 55} + +do_execsql_test 19.2.1 { + SELECT a, sum(b) OVER ( + ORDER BY a RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING + ) FROM t1; +} {1 3 2 6 3 9 4 12 5 9 a 6 b 7 c 8 d 9 e 10} +do_execsql_test 19.2.2 { + SELECT a, sum(b) OVER ( + ORDER BY a DESC RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING + ) FROM t1 ORDER BY a ASC; +} {1 3 2 6 3 9 4 12 5 9 a 6 b 7 c 8 d 9 e 10} + +do_execsql_test 19.3.1 { + SELECT a, sum(b) OVER ( + ORDER BY a RANGE BETWEEN 2 PRECEDING AND 1 FOLLOWING + ) FROM t1; +} {1 3 2 6 3 10 4 14 5 12 a 6 b 7 c 8 d 9 e 10} +do_execsql_test 19.3.2 { + SELECT a, sum(b) OVER ( + ORDER BY a DESC RANGE BETWEEN 1 PRECEDING AND 2 FOLLOWING + ) FROM t1 ORDER BY a ASC; +} {1 3 2 6 3 10 4 14 5 12 a 6 b 7 c 8 d 9 e 10} + + +reset_db +do_execsql_test 20.0 { + CREATE TABLE t1(a, b); + INSERT INTO t1 VALUES + (NULL, 100), (NULL, 100), + (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), + ('a', 6), ('b', 7), ('c', 8), ('d', 9), ('e', 10); +} +do_execsql_test 20.1 { + SELECT a, sum(b) OVER (ORDER BY a) FROM t1; +} { + {} 200 {} 200 1 201 2 203 3 206 4 210 5 215 + a 221 b 228 c 236 d 245 e 255 +} + +do_execsql_test 20.2.1 { + SELECT a, sum(b) OVER ( + ORDER BY a RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING + ) FROM t1; +} {{} 200 {} 200 1 3 2 6 3 9 4 12 5 9 a 6 b 7 c 8 d 9 e 10} +do_execsql_test 20.2.2 { + SELECT a, sum(b) OVER ( + ORDER BY a DESC RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING + ) FROM t1 ORDER BY a ASC; +} {{} 200 {} 200 1 3 2 6 3 9 4 12 5 9 a 6 b 7 c 8 d 9 e 10} + +do_execsql_test 20.3.1 { + SELECT a, sum(b) OVER ( + ORDER BY a RANGE BETWEEN 2 PRECEDING AND 1 FOLLOWING + ) FROM t1; +} {{} 200 {} 200 1 3 2 6 3 10 4 14 5 12 a 6 b 7 c 8 d 9 e 10} +do_execsql_test 20.3.2 { + SELECT a, sum(b) OVER ( + ORDER BY a DESC RANGE BETWEEN 1 PRECEDING AND 2 FOLLOWING + ) FROM t1 ORDER BY a ASC; +} {{} 200 {} 200 1 3 2 6 3 10 4 14 5 12 a 6 b 7 c 8 d 9 e 10} + +#------------------------------------------------------------------------- +do_execsql_test 21.0 { + CREATE TABLE keyword_tab( + current, exclude, filter, following, groups, no, others, over, + partition, preceding, range, ties, unbounded, window + ); +} +do_execsql_test 21.1 { + SELECT + current, exclude, filter, following, groups, no, others, over, + partition, preceding, range, ties, unbounded, window + FROM keyword_tab +} + +#------------------------------------------------------------------------- +foreach {tn expr err} { + 1 4.5 0 + 2 NULL 1 + 3 0.0 0 + 4 0.1 0 + 5 -0.1 1 + 6 '' 1 + 7 '2.0' 0 + 8 '2.0x' 1 + 9 x'1234' 1 + 10 '1.2' 0 +} { + set res {0 1} + if {$err} {set res {1 {frame starting offset must be a non-negative number}} } + do_catchsql_test 22.$tn.1 " + WITH a(x, y) AS ( VALUES(1, 2) ) + SELECT sum(x) OVER ( + ORDER BY y RANGE BETWEEN $expr PRECEDING AND UNBOUNDED FOLLOWING + ) FROM a + " $res + + set res {0 1} + if {$err} {set res {1 {frame ending offset must be a non-negative number}} } + do_catchsql_test 22.$tn.2 " + WITH a(x, y) AS ( VALUES(1, 2) ) + SELECT sum(x) OVER ( + ORDER BY y RANGE BETWEEN UNBOUNDED PRECEDING AND $expr FOLLOWING + ) FROM a + " $res +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 23.0 { + CREATE TABLE t5(a, b, c); + CREATE INDEX t5ab ON t5(a, b); +} + +proc do_ordercount_test {tn sql nOrderBy} { + set plan [execsql "EXPLAIN QUERY PLAN $sql"] + uplevel [list do_test $tn [list regexp -all ORDER $plan] $nOrderBy] +} + +do_ordercount_test 23.1 { + SELECT + sum(c) OVER (ORDER BY a, b), + sum(c) OVER (PARTITION BY a ORDER BY b) + FROM t5 +} 0 + +do_ordercount_test 23.2 { + SELECT + sum(c) OVER (ORDER BY b, a), + sum(c) OVER (PARTITION BY b ORDER BY a) + FROM t5 +} 1 + +do_ordercount_test 23.3 { + SELECT + sum(c) OVER (ORDER BY b, a), + sum(c) OVER (ORDER BY c, b) + FROM t5 +} 2 + +do_ordercount_test 23.4 { + SELECT + sum(c) OVER (ORDER BY b ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + sum(c) OVER (ORDER BY b RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + sum(c) OVER (ORDER BY b GROUPS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM t5 +} 1 + +do_ordercount_test 23.5 { + SELECT + sum(c) OVER (ORDER BY b+1 ROWS UNBOUNDED PRECEDING), + sum(c) OVER (ORDER BY b+1 RANGE UNBOUNDED PRECEDING), + sum(c) OVER (ORDER BY b+1 GROUPS UNBOUNDED PRECEDING) + FROM t5 +} 1 + +do_ordercount_test 23.6 { + SELECT + sum(c) OVER (ORDER BY b+1 ROWS UNBOUNDED PRECEDING), + sum(c) OVER (ORDER BY b+2 RANGE UNBOUNDED PRECEDING), + sum(c) OVER (ORDER BY b+3 GROUPS UNBOUNDED PRECEDING) + FROM t5 +} 3 + +do_execsql_test 24.1 { + SELECT sum(44) OVER () +} {44} + +do_execsql_test 24.2 { + SELECT lead(44) OVER () +} {{}} + +#------------------------------------------------------------------------- +# +reset_db +do_execsql_test 25.0 { + CREATE TABLE t1 ( t1_id INTEGER PRIMARY KEY ); + CREATE TABLE t2 ( t2_id INTEGER PRIMARY KEY ); + CREATE TABLE t3 ( t3_id INTEGER PRIMARY KEY ); + + INSERT INTO t1 VALUES(1), (3), (5); + INSERT INTO t2 VALUES (3), (5); + INSERT INTO t3 VALUES(10), (11), (12); +} + +do_execsql_test 25.1 { + SELECT t1.* FROM t1, t2 WHERE + t1_id=t2_id AND t1_id IN ( + SELECT t1_id + row_number() OVER ( ORDER BY t1_id ) FROM t3 + ) +} + +do_execsql_test 25.2 { + SELECT t1.* FROM t1, t2 WHERE + t1_id=t2_id AND t1_id IN ( + SELECT row_number() OVER ( ORDER BY t1_id ) FROM t3 + ) +} {3} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 26.0 { + CREATE TABLE t1(x); + CREATE TABLE t2(c); +} + +do_execsql_test 26.1 { + SELECT ( SELECT row_number() OVER () FROM ( SELECT c FROM t1 ) ) FROM t2 +} {} + +do_execsql_test 26.2 { + INSERT INTO t1 VALUES(1), (2), (3), (4); + INSERT INTO t2 VALUES(2), (6), (8), (4); + SELECT c, c IN ( + SELECT row_number() OVER () FROM ( SELECT c FROM t1 ) + ) FROM t2 +} {2 1 6 0 8 0 4 1} + +do_execsql_test 26.3 { + DELETE FROM t1; + DELETE FROM t2; + + INSERT INTO t2 VALUES(1), (2), (3), (4); + INSERT INTO t1 VALUES(1), (1), (2), (3), (3), (3), (3), (4), (4); + + SELECT c, c IN ( + SELECT row_number() OVER () FROM ( SELECT 1 FROM t1 WHERE x=c ) + ) FROM t2 +} {1 1 2 0 3 1 4 0} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 27.0 { + CREATE TABLE t1(x); + INSERT INTO t1 VALUES(NULL), (1), (2), (3), (4), (5); +} +do_execsql_test 27.1 { + SELECT min(x) FROM t1; +} {1} +do_execsql_test 27.2 { + SELECT min(x) OVER win FROM t1 + WINDOW win AS (ORDER BY rowid ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) +} {1 1 1 2 3 4} + +#------------------------------------------------------------------------- + +reset_db +do_execsql_test 28.1.1 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b CHAR(1), c CHAR(2), d ANY); + INSERT INTO t1 VALUES (3, 'C', 'cc', 1.0); + INSERT INTO t1 VALUES (13,'M', 'cc', NULL); +} + +do_execsql_test 28.1.2 { + SELECT string_agg(b,'') OVER w1 FROM t1 + WINDOW w1 AS (ORDER BY a RANGE BETWEEN 3 PRECEDING AND 1 PRECEDING) +} { + {} {} +} + +do_execsql_test 28.2.1 { + CREATE TABLE t2(a TEXT, b INTEGER); + INSERT INTO t2 VALUES('A', NULL); + INSERT INTO t2 VALUES('B', NULL); +} + +do_execsql_test 28.2.1 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a INTEGER PRIMARY KEY, b CHAR(1), c CHAR(2), d ANY); + INSERT INTO t1 VALUES + (10,'J', 'cc', NULL), + (11,'K', 'cc', 'xyz'), + (13,'M', 'cc', NULL); +} + +do_execsql_test 28.2.2 { + SELECT a, b, c, quote(d), group_concat(b,'') OVER w1, '|' FROM t1 + WINDOW w1 AS + (ORDER BY d DESC RANGE BETWEEN 7.0 PRECEDING AND 2.5 PRECEDING) + ORDER BY c, d, a; +} { + 10 J cc NULL JM | + 13 M cc NULL JM | + 11 K cc 'xyz' K | +} + +#------------------------------------------------------------------------- +reset_db + +do_execsql_test 29.1 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a INTEGER PRIMARY KEY, b CHAR(1), c CHAR(2), d ANY); + INSERT INTO t1 VALUES + (1, 'A', 'aa', 2.5), + (2, 'B', 'bb', 3.75), + (3, 'C', 'cc', 1.0), + (4, 'D', 'cc', 8.25), + (5, 'E', 'bb', 6.5), + (6, 'F', 'aa', 6.5), + (7, 'G', 'aa', 6.0), + (8, 'H', 'bb', 9.0), + (9, 'I', 'aa', 3.75), + (10,'J', 'cc', NULL), + (11,'K', 'cc', 'xyz'), + (12,'L', 'cc', 'xyZ'), + (13,'M', 'cc', NULL); +} + +do_execsql_test 29.2 { + SELECT a, b, c, quote(d), group_concat(b,'') OVER w1, '|' FROM t1 + WINDOW w1 AS + (PARTITION BY c ORDER BY d DESC + RANGE BETWEEN 7.0 PRECEDING AND 2.5 PRECEDING) + ORDER BY c, d, a; +} { + 1 A aa 2.5 FG | + 9 I aa 3.75 F | + 7 G aa 6 {} | + 6 F aa 6.5 {} | + 2 B bb 3.75 HE | + 5 E bb 6.5 H | + 8 H bb 9 {} | + 10 J cc NULL JM | + 13 M cc NULL JM | + 3 C cc 1 {} | + 4 D cc 8.25 {} | + 12 L cc 'xyZ' L | + 11 K cc 'xyz' K | +} + +# 2019-07-18 +# Check-in [7ef7b23cbb1b9ace] (which was itself a fix for ticket +# https://sqlite.org/src/info/1be72aab9) introduced a new problem +# if the LHS of a BETWEEN operator is a WINDOW function. The problem +# was found by (the recently enhanced) dbsqlfuzz. +# +do_execsql_test 30.0 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a, b, c); + INSERT INTO t1 VALUES('BB','aa',399); + SELECT + count () OVER win1 NOT BETWEEN 'a' AND 'mmm', + count () OVER win3 + FROM t1 + WINDOW win1 AS (ORDER BY a GROUPS BETWEEN 4 PRECEDING AND 1 FOLLOWING + EXCLUDE CURRENT ROW), + win2 AS (PARTITION BY b ORDER BY a), + win3 AS (win2 RANGE BETWEEN 5.2 PRECEDING AND true PRECEDING ); +} {1 1} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 31.1 { + CREATE TABLE t1(a, b); + CREATE TABLE t2(c, d); + CREATE TABLE t3(e, f); + + INSERT INTO t1 VALUES(1, 1); + INSERT INTO t2 VALUES(1, 1); + INSERT INTO t3 VALUES(1, 1); +} + +do_execsql_test 31.2 { + SELECT d IN (SELECT sum(c) OVER (ORDER BY e+c) FROM t3) FROM ( + SELECT * FROM t2 + ); +} {1} + +do_execsql_test 31.3 { + SELECT d IN (SELECT sum(c) OVER (PARTITION BY d ORDER BY e+c) FROM t3) FROM ( + SELECT * FROM t2 + ); +} {1} + +do_catchsql_test 31.3 { + SELECT d IN ( + SELECT sum(c) OVER ( ROWS BETWEEN d FOLLOWING AND UNBOUNDED FOLLOWING) + FROM t3 + ) + FROM ( + SELECT * FROM t2 + ); +} {1 {frame starting offset must be a non-negative integer}} + +do_catchsql_test 31.3 { + SELECT d IN ( + SELECT sum(c) OVER ( ROWS BETWEEN CURRENT ROW AND c FOLLOWING) + FROM t3 + ) + FROM ( + SELECT * FROM t2 + ); +} {1 {frame ending offset must be a non-negative integer}} + +# 2019-11-16 chromium issue 1025467 +ifcapable altertable { + db close + sqlite3 db :memory: + do_catchsql_test 32.10 { + CREATE VIEW a AS SELECT NULL INTERSECT SELECT NULL ORDER BY s() OVER R; + CREATE TABLE a0 AS SELECT 0; + ALTER TABLE a0 RENAME TO S; + } {1 {error in view a: 1st ORDER BY term does not match any column in the result set}} +} + +reset_db +do_execsql_test 33.1 { + CREATE TABLE t1(aa, bb); + INSERT INTO t1 VALUES(1, 2); + INSERT INTO t1 VALUES(5, 6); + CREATE TABLE t2(x); + INSERT INTO t2 VALUES(1); +} +do_execsql_test 33.2 { + SELECT (SELECT DISTINCT sum(aa) OVER() FROM t1 ORDER BY 1), x FROM t2 + ORDER BY 1; +} {6 1} + +reset_db +do_execsql_test 34.1 { + CREATE TABLE t1(a,b,c); +} +do_execsql_test 34.2 { + SELECT avg(a) OVER ( + ORDER BY (SELECT sum(b) OVER () + FROM t1 ORDER BY ( + SELECT total(d) OVER (ORDER BY c) + FROM (SELECT 1 AS d) ORDER BY 1 + ) + ) + ) + FROM t1; +} + +#------------------------------------------------------------------------- +reset_db +do_catchsql_test 35.0 { + SELECT * WINDOW f AS () ORDER BY name COLLATE nocase; +} {1 {no tables specified}} + +do_catchsql_test 35.1 { + VALUES(1) INTERSECT SELECT * WINDOW f AS () ORDER BY x COLLATE nocase; +} {1 {no tables specified}} + +do_execsql_test 35.2 { + CREATE TABLE t1(x); + INSERT INTO t1 VALUES(1), (2), (3); + VALUES(1) INTERSECT + SELECT sum(x) OVER f FROM t1 WINDOW f AS (ORDER BY x) ORDER BY 1; +} {1} + +do_execsql_test 35.3 { + VALUES(8) EXCEPT + SELECT sum(x) OVER f FROM t1 WINDOW f AS (ORDER BY x) ORDER BY 1; +} {8} + +do_execsql_test 35.4 { + VALUES(1) UNION + SELECT sum(x) OVER f FROM t1 WINDOW f AS (ORDER BY x) ORDER BY 1; +} {1 3 6} + +# 2019-12-07 gramfuzz find +# +do_execsql_test 36.10 { + VALUES(count(*)OVER()); +} {1} +do_execsql_test 36.20 { + VALUES(count(*)OVER()),(2); +} {1 2} +do_execsql_test 36.30 { + VALUES(2),(count(*)OVER()); +} {2 1} +do_execsql_test 36.40 { + VALUES(2),(3),(count(*)OVER()),(4),(5); +} {2 3 1 4 5} + +# 2019-12-17 crash test case found by Yongheng and Rui +# See check-in 1ca0bd982ab1183b +# +reset_db +do_execsql_test 37.10 { + CREATE TABLE t0(a UNIQUE, b PRIMARY KEY); + CREATE VIEW v0(c) AS SELECT max((SELECT count(a)OVER(ORDER BY 1))) FROM t0; + SELECT c FROM v0 WHERE c BETWEEN 10 AND 20; +} {} +do_execsql_test 37.20 { + DROP VIEW v0; + CREATE VIEW v0(c) AS SELECT max((SELECT count(a)OVER(ORDER BY 1234))) FROM t0; + SELECT c FROM v0 WHERE c BETWEEN -10 AND 20; +} {} + +# 2019-12-20 mrigger reported problem with a FILTER clause on an aggregate +# in a join. +# +reset_db +do_catchsql_test 38.10 { + CREATE TABLE t0(c0); + CREATE TABLE t1(c0, c1 UNIQUE); + INSERT INTO t0(c0) VALUES(1); + INSERT INTO t1(c0,c1) VALUES(2,3); + SELECT COUNT(*) FROM t0, t1 WHERE (SELECT AVG(0) FILTER(WHERE t1.c1)); +} {1 {misuse of aggregate: AVG()}} +do_execsql_test 38.20 { + SELECT COUNT(*), AVG(1) FILTER(WHERE t1.c1) FROM t0, t1; +} {1 1.0} +do_catchsql_test 38.30 { + SELECT COUNT(*) FROM t0, t1 WHERE (SELECT AVG(1) FILTER(WHERE t1.c1)); +} {1 {misuse of aggregate: AVG()}} + +reset_db +do_execsql_test 39.1 { + CREATE TABLE t0(c0 UNIQUE); +} +do_execsql_test 39.2 { + SELECT FIRST_VALUE(0) OVER(); +} {0} +do_execsql_test 39.3 { + SELECT * FROM t0 WHERE(c0, 0) IN(SELECT FIRST_VALUE(0) OVER(), 0); +} +do_execsql_test 39.4 { + SELECT * FROM t0 WHERE (t0.c0, 1) IN(SELECT NTILE(1) OVER(), 0 FROM t0); +} + +ifcapable rtree { + # 2019-12-25 ticket d87336c81c7d0873 + # + reset_db + do_catchsql_test 40.1 { + CREATE VIRTUAL TABLE t0 USING rtree(c0, c1, c2); + SELECT * FROM t0 + WHERE ((0,0) IN (SELECT COUNT(*),LAG(5)OVER(PARTITION BY 0) FROM t0),0)<=(c1,0); + } {0 {}} +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 41.1 { + CREATE TABLE t1(a, b, c); + INSERT INTO t1 VALUES(NULL,'bb',355); + INSERT INTO t1 VALUES('CC','aa',158); + INSERT INTO t1 VALUES('GG','bb',929); + INSERT INTO t1 VALUES('FF','Rb',574); +} + +do_execsql_test 41.2 { + SELECT min(c) OVER ( + ORDER BY a RANGE BETWEEN 5.2 PRECEDING AND 0.1 PRECEDING + ) FROM t1 +} {355 158 574 929} + +do_execsql_test 41.2 { + SELECT min(c) OVER ( + ORDER BY a RANGE BETWEEN 5.2 PRECEDING AND 0.1 PRECEDING + ) << 100 FROM t1 +} {0 0 0 0} + +do_execsql_test 41.3 { + SELECT + min(c) OVER win3 << first_value(c) OVER win3, + min(c) OVER win3 << first_value(c) OVER win3 + FROM t1 + WINDOW win3 AS ( + PARTITION BY 6 ORDER BY a RANGE BETWEEN 5.2 PRECEDING AND 0.1 PRECEDING + ); +} {0 0 0 0 0 0 0 0} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 42.1 { + CREATE TABLE t1(a, b, c); + INSERT INTO t1 VALUES(1, 1, 1); + INSERT INTO t1 VALUES(2, 2, 2); +} +do_execsql_test 42.2 { + SELECT * FROM t1 WHERE (0, 0) IN ( SELECT count(*), 0 FROM t1 ) +} {} +do_execsql_test 42.3 { + SELECT * FROM t1 WHERE (2, 0) IN ( SELECT count(*), 0 FROM t1 ) +} {1 1 1 2 2 2} + +do_execsql_test 42.3 { + SELECT count(*), max(a) OVER () FROM t1 GROUP BY c; +} {1 2 1 2} + +do_execsql_test 42.4 { + SELECT sum(a), max(b) OVER () FROM t1; +} {3 1} + +do_execsql_test 42.5 { + CREATE TABLE t2(a, b); + INSERT INTO t2 VALUES('a', 1); + INSERT INTO t2 VALUES('a', 2); + INSERT INTO t2 VALUES('a', 3); + INSERT INTO t2 VALUES('b', 4); + INSERT INTO t2 VALUES('b', 5); + INSERT INTO t2 VALUES('b', 6); +} + +do_execsql_test 42.6 { + SELECT a, sum(b), sum( sum(b) ) OVER (ORDER BY a) FROM t2 GROUP BY a; +} {a 6 6 b 15 21} + +do_execsql_test 42.7 { + SELECT sum(b), sum( sum(b) ) OVER (ORDER BY a) FROM t2; +} {21 21} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 43.1.1 { + CREATE TABLE t1(x INTEGER PRIMARY KEY); + INSERT INTO t1 VALUES (10); +} +do_catchsql_test 43.1.2 { + SELECT count() OVER() AS m FROM t1 ORDER BY (SELECT m); +} {1 {misuse of aliased window function m}} + +reset_db +do_execsql_test 43.2.1 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b INTEGER); + INSERT INTO t1(a, b) VALUES(1, 10); -- 10 + INSERT INTO t1(a, b) VALUES(2, 15); -- 25 + INSERT INTO t1(a, b) VALUES(3, -5); -- 20 + INSERT INTO t1(a, b) VALUES(4, -5); -- 15 + INSERT INTO t1(a, b) VALUES(5, 20); -- 35 + INSERT INTO t1(a, b) VALUES(6, -11); -- 24 +} + +do_execsql_test 43.2.2 { + SELECT a, sum(b) OVER (ORDER BY a) AS abc FROM t1 ORDER BY 2 +} { + 1 10 4 15 3 20 6 24 2 25 5 35 +} + +do_execsql_test 43.2.3 { + SELECT a, sum(b) OVER (ORDER BY a) AS abc FROM t1 ORDER BY abc +} { + 1 10 4 15 3 20 6 24 2 25 5 35 +} + +do_execsql_test 43.2.4 { + SELECT a, sum(b) OVER (ORDER BY a) AS abc FROM t1 ORDER BY abc+5 +} { + 1 10 4 15 3 20 6 24 2 25 5 35 +} + +do_catchsql_test 43.2.5 { + SELECT a, sum(b) OVER (ORDER BY a) AS abc FROM t1 ORDER BY (SELECT abc) +} {1 {misuse of aliased window function abc}} + +do_catchsql_test 43.2.6 { + SELECT a, 1+sum(b) OVER (ORDER BY a) AS abc FROM t1 ORDER BY (SELECT abc) +} {1 {misuse of aliased window function abc}} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 44.1 { + CREATE TABLE t0(c0); +} + +do_catchsql_test 44.2.1 { + SELECT ntile(0) OVER (); +} {1 {argument of ntile must be a positive integer}} +do_catchsql_test 44.2.2 { + SELECT (0, 0) IN(SELECT MIN(c0), NTILE(0) OVER()) FROM t0; +} {1 {argument of ntile must be a positive integer}} + +do_execsql_test 44.3.1 { + SELECT ntile(1) OVER (); +} {1} +do_execsql_test 44.3.2 { + SELECT (0, 0) IN(SELECT MIN(c0), NTILE(1) OVER()) FROM t0; +} {0} + +do_execsql_test 44.4.2 { + INSERT INTO t0 VALUES(2), (1), (0); + SELECT (0, 1) IN(SELECT MIN(c0), NTILE(1) OVER()) FROM t0; +} {1} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 45.1 { + CREATE TABLE t0(x); + CREATE TABLE t1(a); + INSERT INTO t1 VALUES(1000); + INSERT INTO t1 VALUES(1000); + INSERT INTO t0 VALUES(10000); +} +do_execsql_test 45.2 { + SELECT * FROM ( + SELECT sum (a) OVER() FROM t1 UNION ALL SELECT x FROM t0 + ); +} {2000 2000 10000} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 46.1 { + CREATE TABLE t1 (a); + CREATE INDEX i1 ON t1(a); + + INSERT INTO t1 VALUES (10); +} + +do_execsql_test 46.2 { + SELECT (SELECT sum(a) OVER(ORDER BY a)) FROM t1 +} 10 + +do_execsql_test 46.3 { + SELECT * FROM t1 WHERE (SELECT sum(a) OVER(ORDER BY a)); +} 10 + +do_execsql_test 46.4 { + SELECT * FROM t1 NATURAL JOIN t1 + WHERE a=1 + OR ((SELECT sum(a)OVER(ORDER BY a)) AND a<=10) +} 10 + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 47.0 { + CREATE TABLE t1( + a, + e, + f, + g UNIQUE, + h UNIQUE + ); +} + +do_execsql_test 47.1 { + CREATE VIEW t2(k) AS + SELECT e FROM t1 WHERE g = 'abc' OR h BETWEEN 10 AND f; +} + +do_catchsql_test 47.2 { + SELECT 234 FROM t2 + WHERE k=1 + OR (SELECT k FROM t2 WHERE (SELECT sum(a) OVER() FROM t1 GROUP BY 1)); +} {1 {misuse of window function sum()}} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 48.0 { + CREATE TABLE t1(a); + INSERT INTO t1 VALUES(1); + INSERT INTO t1 VALUES(2); + INSERT INTO t1 VALUES(3); + SELECT (SELECT max(x)OVER(ORDER BY x) + min(x)OVER(ORDER BY x)) + FROM (SELECT (SELECT sum(a) FROM t1) AS x FROM t1); +} {12 12 12} + +do_execsql_test 48.1 { + SELECT (SELECT max(x)OVER(ORDER BY x) + min(x)OVER(ORDER BY x)) + FROM (SELECT (SELECT sum(a) FROM t1 GROUP BY a) AS x FROM t1); +} {2 2 2} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 49.1 { + CREATE TABLE t1 (a PRIMARY KEY); + INSERT INTO t1 VALUES(1); +} + +do_execsql_test 49.2 { + SELECT b AS c FROM ( + SELECT a AS b FROM ( + SELECT a FROM t1 WHERE a=1 OR (SELECT sum(a) OVER ()) + ) + WHERE b=1 OR b<10 + ) + WHERE c=1 OR c>=10; +} {1} + + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 50.0 { + CREATE TABLE t1 (a DOUBLE PRIMARY KEY); + INSERT INTO t1 VALUES(10.0); +} + +do_execsql_test 50.1 { + SELECT * FROM t1 WHERE a%1 OR (SELECT sum(a) OVER (ORDER BY a%2)) +} {10.0} + +do_execsql_test 50.2 { + SELECT * FROM ( + SELECT * FROM t1 WHERE a%1 OR (SELECT sum(a) OVER (ORDER BY a%2)) + ) + WHERE a=1 OR ( (SELECT sum(a) OVER (ORDER BY a%4)) AND a<=10 ) +} {10.0} + +do_execsql_test 50.3 { + SELECT a FROM ( + SELECT * FROM ( + SELECT * FROM t1 WHERE a%1 OR (SELECT sum(a) OVER (ORDER BY a%2)) + ) + WHERE a=1 OR ( (SELECT sum(a) OVER (ORDER BY a%4)) AND a<=10 ) + ) + WHERE a=1 OR a=10.0 +} {10.0} + +do_execsql_test 50.4 { + SELECT a FROM ( + SELECT * FROM ( + SELECT * FROM t1 WHERE a%1 OR (SELECT sum(a) OVER (ORDER BY a%2)) + ) + WHERE a=1 OR ( (SELECT sum(a) OVER (ORDER BY a%4)) AND a<=10 ) + ) + WHERE a=1 OR ((SELECT sum(a) OVER(ORDER BY a%8)) AND 10<=a) +} {10.0} + +do_execsql_test 50.5 { +SELECT * FROM (SELECT * FROM t1 NATURAL JOIN t1 WHERE a%1 OR ((SELECT sum(a)OVER(ORDER BY a)) AND a<=10)) NATURAL JOIN t1 WHERE a=1 OR ((SELECT sum((SELECT * FROM (SELECT * FROM (SELECT * FROM t1 NATURAL JOIN t1 WHERE a%1 OR ((SELECT sum(a)OVER(ORDER BY a)) AND a<=10)) NATURAL JOIN t1 WHERE a=1 OR ((SELECT sum((SELECT * FROM t1 NATURAL JOIN t1 WHERE a=1 OR ((SELECT sum(a)OVER(ORDER BY a)) AND a<=10)))OVER(ORDER BY a% 1 )) AND a<=10)) NATURAL JOIN t1 WHERE a=1 OR ((SELECT sum(a)OVER(ORDER BY a)) AND 10<=a)))OVER(ORDER BY a%5)) AND a<=10); +} {10.0} + +# 2020-04-03 ticket af4556bb5c285c08 +# +reset_db +do_catchsql_test 51.1 { + CREATE TABLE a(b, c); + SELECT c FROM a GROUP BY c + HAVING(SELECT(sum(b) OVER(ORDER BY b), + sum(b) OVER(PARTITION BY min(DISTINCT c), c ORDER BY b))); +} {1 {row value misused}} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 52.1 { + CREATE TABLE t1(a, b, c); + INSERT INTO t1 VALUES('AA','bb',356); + INSERT INTO t1 VALUES('CC','aa',158); + INSERT INTO t1 VALUES('BB','aa',399); + INSERT INTO t1 VALUES('FF','bb',938); +} + +do_execsql_test 52.2 { + SELECT + count() OVER win1, + sum(c) OVER win2, + first_value(c) OVER win2, + count(a) OVER (ORDER BY b) + FROM t1 + WINDOW + win1 AS (ORDER BY a), + win2 AS (PARTITION BY 6 ORDER BY a + RANGE BETWEEN 5 PRECEDING AND 0 PRECEDING ); +} { + 1 356 356 4 + 2 399 399 2 + 3 158 158 2 + 4 938 938 4 +} + +do_execsql_test 52.3 { +SELECT + count() OVER (), + sum(c) OVER win2, + first_value(c) OVER win2, + count(a) OVER (ORDER BY b) +FROM t1 +WINDOW + win1 AS (ORDER BY a), + win2 AS (PARTITION BY 6 COLLATE binary ORDER BY a + RANGE BETWEEN 5 PRECEDING AND 0 PRECEDING ); +} { + 4 356 356 4 + 4 399 399 2 + 4 158 158 2 + 4 938 938 4 +} + +do_execsql_test 52.4 { + SELECT + count() OVER win1, + sum(c) OVER win2, + first_value(c) OVER win2, + count(a) OVER (ORDER BY b) + FROM t1 + WINDOW + win1 AS (ORDER BY a), + win2 AS (PARTITION BY 6 COLLATE binary ORDER BY a + RANGE BETWEEN 5 PRECEDING AND 0 PRECEDING ); +} { + 1 356 356 4 + 2 399 399 2 + 3 158 158 2 + 4 938 938 4 +} + +# 2020-05-23 +# ticket 7a5279a25c57adf1 +# +reset_db +do_execsql_test 53.0 { + CREATE TABLE a(c UNIQUE); + INSERT INTO a VALUES(4),(0),(9),(-9); + SELECT a.c + FROM a + JOIN a AS b ON a.c=4 + JOIN a AS e ON a.c=e.c + WHERE a.c=(SELECT (SELECT coalesce(lead(2) OVER(),0) + sum(d.c)) + FROM a AS d + WHERE a.c); +} {4 4 4 4} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 54.1 { + CREATE TABLE t1(a VARCHAR(20), b FLOAT); + INSERT INTO t1 VALUES('1',10.0); +} + +do_catchsql_test 54.2 { + SELECT * FROM ( + SELECT sum(b) OVER() AS c FROM t1 + UNION + SELECT b AS c FROM t1 + ) WHERE c>10; +} {0 {}} + +do_execsql_test 54.3 { + INSERT INTO t1 VALUES('2',5.0); + INSERT INTO t1 VALUES('3',15.0); +} + +do_catchsql_test 54.4 { + SELECT * FROM ( + SELECT sum(b) OVER() AS c FROM t1 + UNION + SELECT b AS c FROM t1 + ) WHERE c>10; +} {0 {15.0 30.0}} + +# 2020-06-05 ticket c8d3b9f0a750a529 +reset_db +do_execsql_test 55.1 { + CREATE TABLE a(b); + SELECT + (SELECT b FROM a + GROUP BY b + HAVING (SELECT COUNT()OVER() + lead(b)OVER(ORDER BY SUM(DISTINCT b) + b)) + ) + FROM a + UNION + SELECT 99 + ORDER BY 1; +} {99} + +#------------------------------------------------------------------------ +reset_db +do_execsql_test 56.1 { + CREATE TABLE t1(a, b INTEGER); + CREATE TABLE t2(c, d); +} +do_catchsql_test 56.2 { + SELECT avg(b) FROM t1 + UNION ALL + SELECT min(c) OVER () FROM t2 + ORDER BY nosuchcolumn; +} {1 {1st ORDER BY term does not match any column in the result set}} + +reset_db +do_execsql_test 57.1 { + CREATE TABLE t4(a, b, c, d, e); +} + +do_catchsql_test 57.2 { + SELECT b FROM t4 + UNION + SELECT a FROM t4 + ORDER BY ( + SELECT sum(x) OVER() FROM ( + SELECT c AS x FROM t4 + UNION + SELECT d FROM t4 + ORDER BY (SELECT e FROM t4) + ) + ); +} {1 {1st ORDER BY term does not match any column in the result set}} + +# 2020-06-06 various dbsqlfuzz finds and +# ticket 0899cf62f597d7e7 +# +reset_db +do_execsql_test 57.1 { + CREATE TABLE t1(a, b, c); + INSERT INTO t1 VALUES(NULL,NULL,NULL); + SELECT + sum(a), + min(b) OVER (), + count(c) OVER (ORDER BY b) + FROM t1; +} {{} {} 0} +do_execsql_test 57.2 { + CREATE TABLE v0 ( v1 INTEGER PRIMARY KEY ) ; + INSERT INTO v0 VALUES ( 10 ) ; + SELECT DISTINCT v1, lead(v1) OVER() FROM v0 GROUP BY v1 ORDER BY 2; +} {10 {}} +do_catchsql_test 57.3 { + DROP TABLE t1; + CREATE TABLE t1(a); + INSERT INTO t1(a) VALUES(22); + CREATE TABLE t3(y); + INSERT INTO t3(y) VALUES(5),(11),(-9); + SELECT ( + SELECT max(y) OVER( ORDER BY (SELECT x FROM (SELECT sum(y) AS x FROM t1))) + ) + FROM t3; +} {0 5} + +# 2020-06-06 ticket 1f6f353b684fc708 +reset_db +do_execsql_test 58.1 { + CREATE TABLE a(a, b, c); + INSERT INTO a VALUES(1, 2, 3); + INSERT INTO a VALUES(4, 5, 6); + SELECT sum(345+b) OVER (ORDER BY b), + sum(avg(678)) OVER (ORDER BY c) FROM a; +} {347 678.0} + +# 2020-06-06 ticket e5504e987e419fb0 +do_catchsql_test 59.1 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(x INTEGER PRIMARY KEY); + INSERT INTO t1 VALUES (123); + SELECT + ntile( (SELECT sum(x)) ) OVER(ORDER BY x), + min(x) OVER(ORDER BY x) + FROM t1; +} {1 {misuse of aggregate: sum()}} + +# 2020-06-07 ticket f7d890858f361402 +do_execsql_test 60.1 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1 (x INTEGER PRIMARY KEY); + INSERT INTO t1 VALUES (99); + SELECT EXISTS(SELECT count(*) OVER() FROM t1 ORDER BY sum(x) OVER()); +} {1} + +# 2020-06-07 test case generated by dbsqlfuzz showing how an AggInfo +# object might be referenced after the sqlite3Select() call that created +# it returns. This proves the need to persist all AggInfo objects until +# the Parse object is destroyed. +# +reset_db +do_catchsql_test 61.1 { +CREATE TABLE t1(a); +INSERT INTO t1 VALUES(5),(NULL),('seventeen'); +SELECT (SELECT max(x)OVER(ORDER BY x) % min(x)OVER(ORDER BY CASE x WHEN 889 THEN x WHEN x THEN x END)) FROM (SELECT (SELECT sum(CAST(a IN(SELECT (SELECT max(x)OVER(ORDER BY CASE x WHEN 889 THEN 299 WHEN 863 THEN 863 END)) FROM (SELECT (SELECT sum(CAST((SELECT (SELECT max(x)OVER(ORDER BY x) / min(x)OVER(ORDER BY CASE x WHEN 889 THEN 299 WHEN -true THEN 863 END)) FROM (SELECT (SELECT sum(CAST(a IN(SELECT (SELECT max(x) & sum ( a )OVER(ORDER BY CASE x WHEN -8 THEN 299 WHEN 863 THEN 863 END)) FROM (SELECT (SELECT sum(CAST(a AS )) FROM t1) AS x FROM t1)) AS t1 )) FROM t1) AS x FROM t1)) AS x )) FROM t1) AS x FROM t1)) AS real)) FROM t1) AS x FROM t1); +} {0 {{} {} {}}} + +foreach tn {1 2} { + if {$tn==2} { optimization_control db query-flattener 0 } + do_catchsql_test 61.2.$tn { + SELECT + (SELECT max(x)OVER(ORDER BY x) / min(x) OVER() ) + FROM ( + SELECT (SELECT sum(a) FROM t1 ) AS x FROM t1 + ) + + } {0 {1.0 1.0 1.0}} +} + +reset_db +optimization_control db all 0 +do_execsql_test 61.3.0 { + CREATE TABLE t1(a); + CREATE TABLE t2(y); +} + +do_execsql_test 61.3.1 { + SELECT ( + SELECT count(a) OVER ( ORDER BY (SELECT sum(y) FROM t2) ) + + total(a) OVER() + ) + FROM t1 +} {} +do_execsql_test 61.4.2 { + SELECT ( + SELECT count(a) OVER ( ORDER BY sum(a) ) + + total(a) OVER() + ) + FROM t1 +} {0.0} + +do_catchsql_test 61.4.3 { + SELECT + sum(a) OVER ( ORDER BY a ) + FROM t1 + ORDER BY (SELECT sum(a) FROM t2) +} {1 {misuse of aggregate: sum()}} +do_execsql_test 61.4.4 { + SELECT + sum(a) OVER ( ORDER BY a ) + FROM t1 + ORDER BY (SELECT sum(y) FROM t2) +} + + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 62.1 { + CREATE TABLE t1(a VARCHAR(20), b FLOAT); + INSERT INTO t1 VALUES('1',10.0); +} + +do_execsql_test 62.2 { + SELECT * FROM ( + SELECT sum(b) OVER() AS c FROM t1 + UNION + SELECT b AS c FROM t1 + ) WHERE c>10; +} + +do_execsql_test 62.3 { + INSERT INTO t1 VALUES('2',5.0); + INSERT INTO t1 VALUES('3',15.0); +} + +do_execsql_test 62.4 { + SELECT * FROM ( + SELECT sum(b) OVER() AS c FROM t1 + UNION + SELECT b AS c FROM t1 + ) WHERE c>10; +} {15.0 30.0} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 63.1 { + CREATE TABLE t1(b, x); + CREATE TABLE t2(c, d); + CREATE TABLE t3(e, f); +} + +do_execsql_test 63.2 { + SELECT max(b) OVER( + ORDER BY SUM( + (SELECT c FROM t2 UNION SELECT x ORDER BY c) + ) + ) FROM t1; +} {{}} + +do_execsql_test 63.3 { + SELECT sum(b) over( + ORDER BY ( + SELECT max(b) OVER( + ORDER BY sum( + (SELECT x AS c UNION SELECT 1234 ORDER BY c) + ) + ) AS e + ORDER BY e + ) + ) + FROM t1; +} {{}} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 64.1 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b); + INSERT INTO t1 VALUES(1, 'abcd'); + INSERT INTO t1 VALUES(2, 'BCDE'); + INSERT INTO t1 VALUES(3, 'cdef'); + INSERT INTO t1 VALUES(4, 'DEFG'); +} + +do_execsql_test 64.2 { + SELECT rowid, max(b COLLATE nocase)||'' + FROM t1 + GROUP BY rowid + ORDER BY max(b COLLATE nocase)||''; +} {1 abcd 2 BCDE 3 cdef 4 DEFG} + +do_execsql_test 64.3 { + SELECT count() OVER (), rowid, max(b COLLATE nocase)||'' + FROM t1 + GROUP BY rowid + ORDER BY max(b COLLATE nocase)||''; +} {4 1 abcd 4 2 BCDE 4 3 cdef 4 4 DEFG} + +do_execsql_test 64.4 { + SELECT count() OVER (), rowid, max(b COLLATE nocase) + FROM t1 + GROUP BY rowid + ORDER BY max(b COLLATE nocase); +} {4 1 abcd 4 2 BCDE 4 3 cdef 4 4 DEFG} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 65.1 { + CREATE TABLE t1(c1); + INSERT INTO t1 VALUES('abcd'); +} +do_execsql_test 65.2 { + SELECT max(c1 COLLATE nocase) IN (SELECT 'aBCd') FROM t1; +} {1} + +do_execsql_test 65.3 { + SELECT + count() OVER (), + group_concat(c1 COLLATE nocase) IN (SELECT 'aBCd') FROM t1; +} {1 1} + +do_execsql_test 65.4 { + SELECT COUNT() OVER () LIKE lead(102030) OVER( + ORDER BY sum('abcdef' COLLATE nocase) IN (SELECT 54321) + ) + FROM t1; +} {{}} + +#------------------------------------------------------------------------- +reset_db + +do_execsql_test 66.1 { + CREATE TABLE t1(a INTEGER); + INSERT INTO t1 VALUES(3578824042033200656); + INSERT INTO t1 VALUES(3029012920382354029); +} + +foreach {tn spec} { + 1 "ORDER BY a RANGE BETWEEN 0.3 PRECEDING AND 10 FOLLOWING" + 2 "ORDER BY a RANGE BETWEEN 0.3 PRECEDING AND 0.1 PRECEDING" + 3 "ORDER BY a RANGE BETWEEN 0.3 FOLLOWING AND 10 FOLLOWING" + 4 "ORDER BY a DESC RANGE BETWEEN 0.3 PRECEDING AND 10 FOLLOWING" + 5 "ORDER BY a NULLS LAST RANGE BETWEEN 0.3 PRECEDING AND 10 FOLLOWING" + 6 "ORDER BY a RANGE BETWEEN 1.0 PRECEDING AND 2.0 PRECEDING" +} { + do_execsql_test 66.2.$tn " + SELECT total(a) OVER ( $spec ) FROM t1 ORDER BY a + " { + 3.02901292038235e+18 3.5788240420332e+18 + } +} + + +do_execsql_test 66.3 { + CREATE TABLE t2(a INTEGER); + INSERT INTO t2 VALUES(45); + INSERT INTO t2 VALUES(30); +} + +foreach {tn spec res} { + 1 "ORDER BY a RANGE BETWEEN 0.3 PRECEDING AND 10 FOLLOWING" {30.0 45.0} + 2 "ORDER BY a RANGE BETWEEN 0.3 PRECEDING AND 0.1 PRECEDING" {0.0 0.0} + 3 "ORDER BY a RANGE BETWEEN 0.3 FOLLOWING AND 10 FOLLOWING" {0.0 0.0} + 4 "ORDER BY a DESC RANGE BETWEEN 0.3 PRECEDING AND 10 FOLLOWING" {30.0 45.0} + 5 "ORDER BY a NULLS LAST RANGE BETWEEN 0.3 PRECEDING AND 10 FOLLOWING" {30.0 45.0} + 6 "ORDER BY a RANGE BETWEEN 1.0 PRECEDING AND 2.0 PRECEDING" {0.0 0.0} +} { + do_execsql_test 66.2.$tn " + SELECT total(a) OVER ( $spec ) FROM t2 ORDER BY a + " $res +} + + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 67.0 { + CREATE TABLE t1(a, b, c); + CREATE TABLE t2(a, b, c); +} + +do_catchsql_test 67.1 { + SELECT a,c,b FROM t1 INTERSECT SELECT a,b,c FROM t1 ORDER BY ( + SELECT nth_value(a,2) OVER w1 + WINDOW w1 AS ( ORDER BY ((SELECT 1 FROM v1)) ) + ) +} {1 {no such table: v1}} + +do_catchsql_test 67.2 { + SELECT a,c,b FROM t1 INTERSECT SELECT a,b,c FROM t1 ORDER BY ( + SELECT nth_value(a,2) OVER w1 + WINDOW w1 AS ( ORDER BY ((SELECT 1 FROM t2)) ) + ) +} {1 {1st ORDER BY term does not match any column in the result set}} + +# 2021-05-07 +# Do not allow aggregate functions in the ORDER BY clause even if +# there are window functions in the result set. +# Forum: /forumpost/540fdfef77 +# +reset_db +do_catchsql_test 68.0 { + CREATE TABLE t1(a,b); + INSERT INTO t1(a,b) VALUES(0,0),(1,1),(2,4),(3,9),(4,99); + SELECT rowid, a, b, sum(a)OVER() FROM t1 ORDER BY count(b); +} {1 {misuse of aggregate: count()}} + +# 2021-05-22 +# Forum https://sqlite.org/forum/forumpost/7e484e225c +# +reset_db +do_catchsql_test 69.0 { + CREATE TABLE t1(a,b); + CREATE INDEX t1ba ON t1(b,a); + SELECT * FROM t1 WHERE b = (SELECT b FROM t1 ORDER BY lead(b) OVER () AND sum(a)); +} {1 {misuse of aggregate: sum()}} +do_catchsql_test 69.1 { + SELECT * FROM t1 WHERE b >= (SELECT b FROM t1 ORDER BY lead(b) OVER () AND sum(a)); +} {1 {misuse of aggregate: sum()}} +do_catchsql_test 69.2 { + SELECT * FROM t1 WHERE b <= (SELECT b FROM t1 ORDER BY lead(b) OVER () AND sum(a)); +} {1 {misuse of aggregate: sum()}} + +# 2021-06-23 +# Forum https://sqlite.org/forum/forumpost/31e0432608 +# +reset_db +do_execsql_test 70.0 { + CREATE TABLE t1(a); +} +do_execsql_test 70.1 { + SELECT substr(a,4,lag(a,7) OVER(PARTITION BY 'cf23' ORDER BY 2)) AS ca0 FROM t1 ORDER BY ca0; +} +do_execsql_test 70.2 { + SELECT substr(a,4,lag(a,7) OVER(PARTITION BY 'cf23' ORDER BY likely(2))) AS ca0 FROM t1 ORDER BY ca0; +} + +# 2021-11-07 +# Bug report from Wang Ke +# https://sqlite.org/forum/forumpost/9ba4f60ff8 +reset_db +do_catchsql_test 71.0 { + CREATE TABLE t0(a); + SELECT a FROM t0, (SELECT a AS b FROM t0) + WHERE (a,1)=(SELECT 2,2 UNION SELECT sum(b),max(b) OVER(ORDER BY b) ORDER BY 2) + AND b=4 + ORDER BY b; +} {/1 {.*}/} + +do_execsql_test 72.1 { + CREATE TABLE dual(dummy); INSERT INTO dual VALUES('X'); + CREATE VIEW v1(x,y) AS SELECT RANK() OVER (PARTITION BY 0), SUM(0) FROM dual; + SELECT * FROM v1 WHERE true; +} {1 0} + +#------------------------------------------------------------------------- +reset_db + +do_execsql_test 72.0 { + CREATE TABLE t0(c0); + INSERT INTO t0(c0) VALUES (0); + CREATE VIEW v0(c0) AS SELECT TOTAL(0) OVER (PARTITION BY t0.c0) FROM t0; +} +do_execsql_test 72.1 { + SELECT COUNT(*) FROM ( + SELECT TOTAL(0) OVER (PARTITION BY t0.c0) FROM t0 + ) + WHERE ('1' IS NOT ('abcde' NOTNULL)); +} {1} + +# 2023-03-28 https://sqlite.org/forum/forumpost/dc3b92cfa0 (Song Liu) +# +reset_db +do_execsql_test 73.0 { + CREATE TABLE t1(a INT); + INSERT INTO t1(a) VALUES(1),(2),(4); + CREATE VIEW t2(b,c) AS SELECT * FROM t1 JOIN t1 A ORDER BY sum(0) OVER(PARTITION BY 0); + CREATE TRIGGER x1 INSTEAD OF UPDATE ON t2 BEGIN SELECT true; END; +} +do_execsql_test 73.1 { + SELECT * FROM t2; +} {1 1 1 2 1 4 2 1 2 2 2 4 4 1 4 2 4 4} +do_execsql_test 73.2 { + UPDATE t2 SET c=99 WHERE b=4 RETURNING *; +} {4 99 4 99 4 99} +do_execsql_test 73.3 { + SELECT *, nth_value(15,2) OVER() FROM t2, t1 WHERE b=4; +} { + 4 1 1 15 + 4 2 1 15 + 4 4 1 15 + 4 1 2 15 + 4 2 2 15 + 4 4 2 15 + 4 1 4 15 + 4 2 4 15 + 4 4 4 15 +} +do_execsql_test 73.4 { + UPDATE t2 SET c=nth_value(15,2) OVER() FROM (SELECT * FROM t1) WHERE b=4 RETURNING *; +} { + 4 15 + 4 15 + 4 15 + 4 15 + 4 15 + 4 15 + 4 15 + 4 15 + 4 15 +} +do_execsql_test 73.5 { + DROP TRIGGER x1; +} +do_catchsql_test 73.6 { + UPDATE t2 SET c=99 WHERE b=4 RETURNING *; +} {1 {cannot modify t2 because it is a view}} +do_catchsql_test 73.7 { + UPDATE t2 SET c=nth_value(15,2) OVER() FROM (SELECT * FROM t1) WHERE b=4 RETURNING *; +} {1 {cannot modify t2 because it is a view}} + +# 2023-03-28 https://sqlite.org/forum/forumpost/bad532820c +# +reset_db +do_execsql_test 74.0 { + CREATE TABLE t1 (a INT, b INT); + CREATE TABLE t2 (c INT, d INT); + CREATE INDEX idx ON t1(abs(a)); + INSERT INTO t1 VALUES(1,2),(3,4); + INSERT INTO t2 VALUES(5,6),(7,8); +} +do_execsql_test 74.1 { + SELECT ( + SELECT count( a ) FROM t2 LIMIT 1 + ) + FROM t1; +} {2} ;# Verified using PG 14.2 +do_execsql_test 74.2 { + SELECT ( + SELECT count( a+c ) FROM t2 LIMIT 1 + ) + FROM t1; +} {2 2} ;# verified on PG 14.2. Crashes PG 9.6! +do_execsql_test 74.3 { + SELECT ( + SELECT count( ( SELECT(sum(0) OVER(ORDER BY c, abs(a))) ) ) + FROM t2 GROUP BY c LIMIT 1 + ) + FROM t1; +} {1 1} ;# verified on PG 14.2 +do_execsql_test 74.4 { + /* Original test case reported in https://sqlite.org/forum/forumpost/bad532820c + CREATE TABLE v0 (c1); + CREATE INDEX i ON v0 (c1, c1=1); + SELECT 0 FROM v0 AS a1 + WHERE (SELECT count((SELECT(sum(0) OVER(PARTITION BY(c1), (a1.c1=1) )))) + FROM v0 + GROUP BY hex(0)) + AND a1.c1=0; +} {} + +# 2023-04-11 https://sqlite.org/forum/forumpost/6c5678e3da +# An ALWAYS() turns out to be sometimes false. +# +do_execsql_test 75.0 { + DROP TABLE t1; + CREATE TABLE t1(a INT, b INT); + CREATE INDEX t1x ON t1(a+b); +} +do_catchsql_test 75.1 { + SELECT count((SELECT count(a0.a+a0.b) ORDER BY sum(0) OVER (PARTITION BY 0))) + FROM t1 AS a0 JOIN t1 AS a1 + GROUP BY a1.a; +} {1 {misuse of aggregate: count()}} + +# 2023-04-13 https://sqlite.org/forum/forumpost/0d48347967 +reset_db +do_execsql_test 76.0 { + CREATE TABLE t1(a INT, b INT); + INSERT INTO t1(a,b) VALUES (111,222),(111,223),(118,229); + CREATE INDEX t1a ON t1(a); + CREATE TABLE t2(x INT); + INSERT INTO t2 VALUES (333),(444),(555); +} +do_execsql_test 76.1 { + SELECT c, (SELECT c + sum(1) OVER ()) AS "res" + FROM t2 LEFT JOIN (SELECT +a AS c FROM t1) AS v1 ON true + GROUP BY c + ORDER by c; +} {111 112 118 119} +# ^^^^^^^^^^^^^^^^^-- results verified against PG 14.2 + +do_execsql_test 76.2 { + CREATE TABLE t3(x); + CREATE TABLE t4(y); + INSERT INTO t3 VALUES(100), (200), (400); + INSERT INTO t4 VALUES(100), (300), (400); +} +do_execsql_test 76.3 { + SELECT (SELECT y+sum(0) OVER ()) FROM t3 LEFT JOIN t4 ON x=y; +} {100 {} 400} +do_execsql_test 76.4 { + SELECT (SELECT y+sum(0) OVER ()) FROM t3 LEFT JOIN t4 ON x=y GROUP BY x; +} {100 {} 400} +do_execsql_test 76.5 { + SELECT (SELECT max(y)+sum(0) OVER ()) FROM t3 LEFT JOIN t4 ON x=y GROUP BY x; +} {100 {} 400} + +# 2023-05-23 https://sqlite.org/forum/forumpost/fbfe330a20 +# +reset_db +do_execsql_test 77.1 { + CREATE TABLE t1(x INT); + CREATE INDEX t1x ON t1(likely(x)); + INSERT INTO t1 VALUES(1),(2),(4),(8); +} +do_execsql_test 77.2 { + SELECT max(~likely(x)) FILTER (WHERE true) FROM t1 INDEXED BY t1x GROUP BY x; +} {-2 -3 -5 -9} + +# 2024-05-23 https://sqlite.org/forum/forumpost/bf8f43aa522c2299 +# +# A bug in group_concat() when used as a window function, reported +# just hours after the 3.46.0 release, though first appearing +# in 3.28.0. +# +# When used as a window function, a group_concat() was not +# correctly distinguishing between NULL and empty-string for +# its return value. +# +do_execsql_test 78.1 { + SELECT quote(group_concat(x) OVER ()) FROM (SELECT '' AS x); +} '' +do_execsql_test 78.2 { + SELECT quote(group_concat(x) OVER ( + ORDER BY y RANGE BETWEEN 1 FOLLOWING AND 2 FOLLOWING + )) FROM (SELECT 'abc' AS x, 1 AS y); +} NULL + +#------------------------------------------------------------------------- +# Test that the following queries do not run for a very long time. +# +# https://sqlite.org/forum/forumpost/b1993c858f +# +do_execsql_test 79.0 { + CREATE TABLE t0 (c0 INTEGER ); + INSERT INTO t0 VALUES(1); + INSERT INTO t0 VALUES(2); + INSERT INTO t0 VALUES(3); +} + +do_execsql_test 79.1 { + SELECT COUNT(*) + OVER ( ROWS BETWEEN 0 FOLLOWING AND 100 FOLLOWING) + FROM t0; +} {3 2 1} + +do_execsql_test 79.2 { + SELECT COUNT(*) + OVER ( ORDER BY c0 RANGE BETWEEN 0 FOLLOWING AND 10_000_000_000 FOLLOWING ) + FROM t0; +} {3 2 1} + +do_execsql_test 79.3 { + SELECT sum(c0), COUNT(*) + OVER ( ORDER BY c0 RANGE BETWEEN 0 FOLLOWING AND 10_000_000_000 FOLLOWING ) + FROM t0; +} {6 1} + +finish_test diff --git a/test/window2.tcl b/test/window2.tcl new file mode 100644 index 0000000000..4c18b7970d --- /dev/null +++ b/test/window2.tcl @@ -0,0 +1,494 @@ +# 2018 May 19 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +source [file join [file dirname $argv0] pg_common.tcl] + +#========================================================================= + + +start_test window2 "2018 May 19" + +ifcapable !windowfunc + +execsql_test 1.0 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT, c TEXT, d INTEGER); + INSERT INTO t1 VALUES(1, 'odd', 'one', 1); + INSERT INTO t1 VALUES(2, 'even', 'two', 2); + INSERT INTO t1 VALUES(3, 'odd', 'three', 3); + INSERT INTO t1 VALUES(4, 'even', 'four', 4); + INSERT INTO t1 VALUES(5, 'odd', 'five', 5); + INSERT INTO t1 VALUES(6, 'even', 'six', 6); +} + +execsql_test 1.1 { + SELECT c, sum(d) OVER (PARTITION BY b ORDER BY c) FROM t1; +} + +execsql_test 1.2 { + SELECT sum(d) OVER () FROM t1; +} + +execsql_test 1.3 { + SELECT sum(d) OVER (PARTITION BY b) FROM t1; +} + +========== +execsql_test 2.1 { + SELECT a, sum(d) OVER ( + ORDER BY d + ROWS BETWEEN 1000 PRECEDING AND 1 FOLLOWING + ) FROM t1 +} +execsql_test 2.2 { + SELECT a, sum(d) OVER ( + ORDER BY d + ROWS BETWEEN 1000 PRECEDING AND 1000 FOLLOWING + ) FROM t1 +} +execsql_test 2.3 { + SELECT a, sum(d) OVER ( + ORDER BY d + ROWS BETWEEN 1 PRECEDING AND 1000 FOLLOWING + ) FROM t1 +} +execsql_test 2.4 { + SELECT a, sum(d) OVER ( + ORDER BY d + ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING + ) FROM t1 +} +execsql_test 2.5 { + SELECT a, sum(d) OVER ( + ORDER BY d + ROWS BETWEEN 1 PRECEDING AND 0 FOLLOWING + ) FROM t1 +} + +execsql_test 2.6 { + SELECT a, sum(d) OVER ( + PARTITION BY b + ORDER BY d + ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING + ) FROM t1 +} + +execsql_test 2.7 { + SELECT a, sum(d) OVER ( + PARTITION BY b + ORDER BY d + ROWS BETWEEN 0 PRECEDING AND 0 FOLLOWING + ) FROM t1 +} + +execsql_test 2.8 { + SELECT a, sum(d) OVER ( + ORDER BY d + ROWS BETWEEN CURRENT ROW AND 2 FOLLOWING + ) FROM t1 +} + +execsql_test 2.9 { + SELECT a, sum(d) OVER ( + ORDER BY d + ROWS BETWEEN UNBOUNDED PRECEDING AND 2 FOLLOWING + ) FROM t1 +} + +execsql_test 2.10 { + SELECT a, sum(d) OVER ( + ORDER BY d + ROWS BETWEEN CURRENT ROW AND 2 FOLLOWING + ) FROM t1 +} + +execsql_test 2.11 { + SELECT a, sum(d) OVER ( + ORDER BY d + ROWS BETWEEN 2 PRECEDING AND CURRENT ROW + ) FROM t1 +} + +execsql_test 2.13 { + SELECT a, sum(d) OVER ( + ORDER BY d + ROWS BETWEEN 2 PRECEDING AND UNBOUNDED FOLLOWING + ) FROM t1 +} + +execsql_test 2.14 { + SELECT a, sum(d) OVER ( + ORDER BY d + ROWS BETWEEN 3 PRECEDING AND 1 PRECEDING + ) FROM t1 +} + +execsql_test 2.15 { + SELECT a, sum(d) OVER ( + PARTITION BY b + ORDER BY d + ROWS BETWEEN 1 PRECEDING AND 0 PRECEDING + ) FROM t1 +} + +execsql_test 2.16 { + SELECT a, sum(d) OVER ( + PARTITION BY b + ORDER BY d + ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING + ) FROM t1 +} + +execsql_test 2.17 { + SELECT a, sum(d) OVER ( + PARTITION BY b + ORDER BY d + ROWS BETWEEN 1 PRECEDING AND 2 PRECEDING + ) FROM t1 +} + +execsql_test 2.18 { + SELECT a, sum(d) OVER ( + PARTITION BY b + ORDER BY d + ROWS BETWEEN UNBOUNDED PRECEDING AND 2 PRECEDING + ) FROM t1 +} + +execsql_test 2.19 { + SELECT a, sum(d) OVER ( + PARTITION BY b + ORDER BY d + ROWS BETWEEN 1 FOLLOWING AND 3 FOLLOWING + ) FROM t1 +} + +execsql_test 2.20 { + SELECT a, sum(d) OVER ( + ORDER BY d + ROWS BETWEEN 1 FOLLOWING AND 2 FOLLOWING + ) FROM t1 +} + +execsql_test 2.21 { + SELECT a, sum(d) OVER ( + ORDER BY d + ROWS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING + ) FROM t1 +} + +execsql_test 2.22 { + SELECT a, sum(d) OVER ( + PARTITION BY b + ORDER BY d + ROWS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING + ) FROM t1 +} + +execsql_test 2.23 { + SELECT a, sum(d) OVER ( + ORDER BY d + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + ) FROM t1 +} + +execsql_test 2.24 { + SELECT a, sum(d) OVER ( + PARTITION BY a%2 + ORDER BY d + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + ) FROM t1 +} + +execsql_test 2.25 { + SELECT a, sum(d) OVER ( + ORDER BY d + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) FROM t1 +} + +execsql_test 2.26 { + SELECT a, sum(d) OVER ( + PARTITION BY b + ORDER BY d + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) FROM t1 +} + +execsql_test 2.27 { + SELECT a, sum(d) OVER ( + ORDER BY d + ROWS BETWEEN CURRENT ROW AND CURRENT ROW + ) FROM t1 +} + +execsql_test 2.28 { + SELECT a, sum(d) OVER ( + PARTITION BY b + ORDER BY d + ROWS BETWEEN CURRENT ROW AND CURRENT ROW + ) FROM t1 +} + +execsql_test 2.29 { + SELECT a, sum(d) OVER ( + ORDER BY d + RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + ) FROM t1 +} +execsql_test 2.30 { + SELECT a, sum(d) OVER ( + ORDER BY b + RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + ) FROM t1 +} + +execsql_test 3.1 { + SELECT a, sum(d) OVER ( + PARTITION BY b ORDER BY d + RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + ) FROM t1 +} + +execsql_test 3.2 { + SELECT a, sum(d) OVER ( + ORDER BY b + RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + ) FROM t1 +} + +execsql_test 3.3 { + SELECT a, sum(d) OVER ( + ORDER BY d + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) FROM t1 +} + +execsql_test 3.4 { + SELECT a, sum(d) OVER ( + ORDER BY d/2 + ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW + ) FROM t1 +} + +#puts $::fd finish_test + +========== + +execsql_test 4.0 { + DROP TABLE IF EXISTS t2; + CREATE TABLE t2(a INTEGER PRIMARY KEY, b INTEGER); + INSERT INTO t2(a, b) VALUES + (1,0), (2,74), (3,41), (4,74), (5,23), (6,99), (7,26), (8,33), (9,2), + (10,89), (11,81), (12,96), (13,59), (14,38), (15,68), (16,39), (17,62), + (18,91), (19,46), (20,6), (21,99), (22,97), (23,27), (24,46), (25,78), + (26,54), (27,97), (28,8), (29,67), (30,29), (31,93), (32,84), (33,77), + (34,23), (35,16), (36,16), (37,93), (38,65), (39,35), (40,47), (41,7), + (42,86), (43,74), (44,61), (45,91), (46,85), (47,24), (48,85), (49,43), + (50,59), (51,12), (52,32), (53,56), (54,3), (55,91), (56,22), (57,90), + (58,55), (59,15), (60,28), (61,89), (62,25), (63,47), (64,1), (65,56), + (66,40), (67,43), (68,56), (69,16), (70,75), (71,36), (72,89), (73,98), + (74,76), (75,81), (76,4), (77,94), (78,42), (79,30), (80,78), (81,33), + (82,29), (83,53), (84,63), (85,2), (86,87), (87,37), (88,80), (89,84), + (90,72), (91,41), (92,9), (93,61), (94,73), (95,95), (96,65), (97,13), + (98,58), (99,96), (100,98), (101,1), (102,21), (103,74), (104,65), (105,35), + (106,5), (107,73), (108,11), (109,51), (110,87), (111,41), (112,12), (113,8), + (114,20), (115,31), (116,31), (117,15), (118,95), (119,22), (120,73), + (121,79), (122,88), (123,34), (124,8), (125,11), (126,49), (127,34), + (128,90), (129,59), (130,96), (131,60), (132,55), (133,75), (134,77), + (135,44), (136,2), (137,7), (138,85), (139,57), (140,74), (141,29), (142,70), + (143,59), (144,19), (145,39), (146,26), (147,26), (148,47), (149,80), + (150,90), (151,36), (152,58), (153,47), (154,9), (155,72), (156,72), (157,66), + (158,33), (159,93), (160,75), (161,64), (162,81), (163,9), (164,23), (165,37), + (166,13), (167,12), (168,14), (169,62), (170,91), (171,36), (172,91), + (173,33), (174,15), (175,34), (176,36), (177,99), (178,3), (179,95), (180,69), + (181,58), (182,52), (183,30), (184,50), (185,84), (186,10), (187,84), + (188,33), (189,21), (190,39), (191,44), (192,58), (193,30), (194,38), + (195,34), (196,83), (197,27), (198,82), (199,17), (200,7); +} + +execsql_test 4.1 { + SELECT a, sum(b) OVER ( + PARTITION BY (b%10) + ORDER BY b + ) FROM t2 ORDER BY a; +} + +execsql_test 4.2 { + SELECT a, sum(b) OVER ( + PARTITION BY (b%10) + ORDER BY b + RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW + ) FROM t2 ORDER BY a; +} + +execsql_test 4.3 { + SELECT b, sum(b) OVER ( + ORDER BY b + ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW + ) FROM t2 ORDER BY b; +} + +execsql_test 4.4 { + SELECT b, sum(b) OVER ( + ORDER BY b + RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) FROM t2 ORDER BY b; +} + +execsql_test 4.5 { + SELECT b, sum(b) OVER ( + ORDER BY b + RANGE BETWEEN CURRENT ROW AND CURRENT ROW + ) FROM t2 ORDER BY b; +} + +execsql_test 4.6.1 { + SELECT b, sum(b) OVER ( + RANGE BETWEEN CURRENT ROW AND CURRENT ROW + ) FROM t2 ORDER BY b; +} +execsql_test 4.6.2 { + SELECT b, sum(b) OVER () FROM t2 ORDER BY b; +} +execsql_test 4.6.3 { + SELECT b, sum(b) OVER ( + RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) FROM t2 ORDER BY b; +} +execsql_test 4.6.4 { + SELECT b, sum(b) OVER ( + RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + ) FROM t2 ORDER BY b; +} + +execsql_test 4.7.1 { + SELECT b, sum(b) OVER ( + ROWS BETWEEN CURRENT ROW AND CURRENT ROW + ) FROM t2 ORDER BY 1, 2; +} +execsql_test 4.7.2 { + SELECT b, sum(b) OVER ( + ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW + ) FROM t2 ORDER BY 1, 2; +} +execsql_test 4.7.3 { + SELECT b, sum(b) OVER ( + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) FROM t2 ORDER BY 1, 2; +} +execsql_test 4.7.4 { + SELECT b, sum(b) OVER ( + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + ) FROM t2 ORDER BY 1, 2; +} + +execsql_test 4.8.1 { + SELECT b, sum(b) OVER ( + ORDER BY a + ROWS BETWEEN CURRENT ROW AND CURRENT ROW + ) FROM t2 ORDER BY 1, 2; +} +execsql_test 4.8.2 { + SELECT b, sum(b) OVER ( + ORDER BY a + ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW + ) FROM t2 ORDER BY 1, 2; +} +execsql_test 4.8.3 { + SELECT b, sum(b) OVER ( + ORDER BY a + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) FROM t2 ORDER BY 1, 2; +} +execsql_test 4.8.4 { + SELECT b, sum(b) OVER ( + ORDER BY a + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + ) FROM t2 ORDER BY 1, 2; +} + +execsql_float_test 4.9 { + SELECT + rank() OVER win AS rank, + cume_dist() OVER win AS cume_dist FROM t1 + WINDOW win AS (ORDER BY 1); +} + +execsql_test 4.10 { + SELECT count(*) OVER (ORDER BY b) FROM t1 +} + +execsql_test 4.11 { + SELECT count(distinct a) FILTER (WHERE b='odd') FROM t1 +} + +========== + +execsql_test 5.0 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(x INTEGER, y INTEGER); + INSERT INTO t1 VALUES(10, 1); + INSERT INTO t1 VALUES(20, 2); + INSERT INTO t1 VALUES(3, 3); + INSERT INTO t1 VALUES(2, 4); + INSERT INTO t1 VALUES(1, 5); +} + +execsql_float_test 5.1 { + SELECT avg(x) OVER (ORDER BY y) AS z FROM t1 ORDER BY z; +} + +========== + +execsql_test 6.0 { + DROP TABLE IF EXISTS t0; + CREATE TABLE t0(c0 INTEGER UNIQUE); + INSERT INTO t0 VALUES(0); +} +execsql_test 6.1 { + SELECT DENSE_RANK() OVER(), LAG(0) OVER() FROM t0; +} +execsql_test 6.2 { + SELECT * FROM t0 WHERE + (0, t0.c0) IN (SELECT DENSE_RANK() OVER(), LAG(0) OVER() FROM t0); +} + +========== + +execsql_test 7.0 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a INTEGER, b INTEGER, c INTEGER); + INSERT INTO t1 VALUES(1, 1, 1); + INSERT INTO t1 VALUES(1, 2, 2); + INSERT INTO t1 VALUES(3, 3, 3); + INSERT INTO t1 VALUES(3, 4, 4); +} + +execsql_test 7.1 { + SELECT c, sum(c) OVER win1 FROM t1 + WINDOW win1 AS (ORDER BY b) +} + +execsql_test 7.2 { + SELECT c, sum(c) OVER win1 FROM t1 + WINDOW win1 AS (PARTITION BY 1 ORDER BY b) +} + +execsql_test 7.3 { + SELECT c, sum(c) OVER win1 FROM t1 + WINDOW win1 AS (ORDER BY 1) +} + +finish_test + + diff --git a/test/window2.test b/test/window2.test new file mode 100644 index 0000000000..e241d59644 --- /dev/null +++ b/test/window2.test @@ -0,0 +1,976 @@ +# 2018 May 19 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. +# + +#################################################### +# DO NOT EDIT! THIS FILE IS AUTOMATICALLY GENERATED! +#################################################### + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix window2 + +ifcapable !windowfunc { finish_test ; return } +do_execsql_test 1.0 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT, c TEXT, d INTEGER); + INSERT INTO t1 VALUES(1, 'odd', 'one', 1); + INSERT INTO t1 VALUES(2, 'even', 'two', 2); + INSERT INTO t1 VALUES(3, 'odd', 'three', 3); + INSERT INTO t1 VALUES(4, 'even', 'four', 4); + INSERT INTO t1 VALUES(5, 'odd', 'five', 5); + INSERT INTO t1 VALUES(6, 'even', 'six', 6); +} {} + +do_execsql_test 1.1 { + SELECT c, sum(d) OVER (PARTITION BY b ORDER BY c) FROM t1; +} {four 4 six 10 two 12 five 5 one 6 three 9} + +do_execsql_test 1.2 { + SELECT sum(d) OVER () FROM t1; +} {21 21 21 21 21 21} + +do_execsql_test 1.3 { + SELECT sum(d) OVER (PARTITION BY b) FROM t1; +} {12 12 12 9 9 9} + +#========================================================================== + +do_execsql_test 2.1 { + SELECT a, sum(d) OVER ( + ORDER BY d + ROWS BETWEEN 1000 PRECEDING AND 1 FOLLOWING + ) FROM t1 +} {1 3 2 6 3 10 4 15 5 21 6 21} + +do_execsql_test 2.2 { + SELECT a, sum(d) OVER ( + ORDER BY d + ROWS BETWEEN 1000 PRECEDING AND 1000 FOLLOWING + ) FROM t1 +} {1 21 2 21 3 21 4 21 5 21 6 21} + +do_execsql_test 2.3 { + SELECT a, sum(d) OVER ( + ORDER BY d + ROWS BETWEEN 1 PRECEDING AND 1000 FOLLOWING + ) FROM t1 +} {1 21 2 21 3 20 4 18 5 15 6 11} + +do_execsql_test 2.4 { + SELECT a, sum(d) OVER ( + ORDER BY d + ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING + ) FROM t1 +} {1 3 2 6 3 9 4 12 5 15 6 11} + +do_execsql_test 2.5 { + SELECT a, sum(d) OVER ( + ORDER BY d + ROWS BETWEEN 1 PRECEDING AND 0 FOLLOWING + ) FROM t1 +} {1 1 2 3 3 5 4 7 5 9 6 11} + +do_execsql_test 2.6 { + SELECT a, sum(d) OVER ( + PARTITION BY b + ORDER BY d + ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING + ) FROM t1 +} {2 6 4 12 6 10 1 4 3 9 5 8} + +do_execsql_test 2.7 { + SELECT a, sum(d) OVER ( + PARTITION BY b + ORDER BY d + ROWS BETWEEN 0 PRECEDING AND 0 FOLLOWING + ) FROM t1 +} {2 2 4 4 6 6 1 1 3 3 5 5} + +do_execsql_test 2.8 { + SELECT a, sum(d) OVER ( + ORDER BY d + ROWS BETWEEN CURRENT ROW AND 2 FOLLOWING + ) FROM t1 +} {1 6 2 9 3 12 4 15 5 11 6 6} + +do_execsql_test 2.9 { + SELECT a, sum(d) OVER ( + ORDER BY d + ROWS BETWEEN UNBOUNDED PRECEDING AND 2 FOLLOWING + ) FROM t1 +} {1 6 2 10 3 15 4 21 5 21 6 21} + +do_execsql_test 2.10 { + SELECT a, sum(d) OVER ( + ORDER BY d + ROWS BETWEEN CURRENT ROW AND 2 FOLLOWING + ) FROM t1 +} {1 6 2 9 3 12 4 15 5 11 6 6} + +do_execsql_test 2.11 { + SELECT a, sum(d) OVER ( + ORDER BY d + ROWS BETWEEN 2 PRECEDING AND CURRENT ROW + ) FROM t1 +} {1 1 2 3 3 6 4 9 5 12 6 15} + +do_execsql_test 2.13 { + SELECT a, sum(d) OVER ( + ORDER BY d + ROWS BETWEEN 2 PRECEDING AND UNBOUNDED FOLLOWING + ) FROM t1 +} {1 21 2 21 3 21 4 20 5 18 6 15} + +do_execsql_test 2.14 { + SELECT a, sum(d) OVER ( + ORDER BY d + ROWS BETWEEN 3 PRECEDING AND 1 PRECEDING + ) FROM t1 +} {1 {} 2 1 3 3 4 6 5 9 6 12} + +do_execsql_test 2.15 { + SELECT a, sum(d) OVER ( + PARTITION BY b + ORDER BY d + ROWS BETWEEN 1 PRECEDING AND 0 PRECEDING + ) FROM t1 +} {2 2 4 6 6 10 1 1 3 4 5 8} + +do_execsql_test 2.16 { + SELECT a, sum(d) OVER ( + PARTITION BY b + ORDER BY d + ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING + ) FROM t1 +} {2 {} 4 2 6 4 1 {} 3 1 5 3} + +do_execsql_test 2.17 { + SELECT a, sum(d) OVER ( + PARTITION BY b + ORDER BY d + ROWS BETWEEN 1 PRECEDING AND 2 PRECEDING + ) FROM t1 +} {2 {} 4 {} 6 {} 1 {} 3 {} 5 {}} + +do_execsql_test 2.18 { + SELECT a, sum(d) OVER ( + PARTITION BY b + ORDER BY d + ROWS BETWEEN UNBOUNDED PRECEDING AND 2 PRECEDING + ) FROM t1 +} {2 {} 4 {} 6 2 1 {} 3 {} 5 1} + +do_execsql_test 2.19 { + SELECT a, sum(d) OVER ( + PARTITION BY b + ORDER BY d + ROWS BETWEEN 1 FOLLOWING AND 3 FOLLOWING + ) FROM t1 +} {2 10 4 6 6 {} 1 8 3 5 5 {}} + +do_execsql_test 2.20 { + SELECT a, sum(d) OVER ( + ORDER BY d + ROWS BETWEEN 1 FOLLOWING AND 2 FOLLOWING + ) FROM t1 +} {1 5 2 7 3 9 4 11 5 6 6 {}} + +do_execsql_test 2.21 { + SELECT a, sum(d) OVER ( + ORDER BY d + ROWS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING + ) FROM t1 +} {1 20 2 18 3 15 4 11 5 6 6 {}} + +do_execsql_test 2.22 { + SELECT a, sum(d) OVER ( + PARTITION BY b + ORDER BY d + ROWS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING + ) FROM t1 +} {2 10 4 6 6 {} 1 8 3 5 5 {}} + +do_execsql_test 2.23 { + SELECT a, sum(d) OVER ( + ORDER BY d + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + ) FROM t1 +} {1 21 2 20 3 18 4 15 5 11 6 6} + +do_execsql_test 2.24 { + SELECT a, sum(d) OVER ( + PARTITION BY a%2 + ORDER BY d + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + ) FROM t1 +} {2 12 4 10 6 6 1 9 3 8 5 5} + +do_execsql_test 2.25 { + SELECT a, sum(d) OVER ( + ORDER BY d + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) FROM t1 +} {1 21 2 21 3 21 4 21 5 21 6 21} + +do_execsql_test 2.26 { + SELECT a, sum(d) OVER ( + PARTITION BY b + ORDER BY d + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) FROM t1 +} {2 12 4 12 6 12 1 9 3 9 5 9} + +do_execsql_test 2.27 { + SELECT a, sum(d) OVER ( + ORDER BY d + ROWS BETWEEN CURRENT ROW AND CURRENT ROW + ) FROM t1 +} {1 1 2 2 3 3 4 4 5 5 6 6} + +do_execsql_test 2.28 { + SELECT a, sum(d) OVER ( + PARTITION BY b + ORDER BY d + ROWS BETWEEN CURRENT ROW AND CURRENT ROW + ) FROM t1 +} {2 2 4 4 6 6 1 1 3 3 5 5} + +do_execsql_test 2.29 { + SELECT a, sum(d) OVER ( + ORDER BY d + RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + ) FROM t1 +} {1 21 2 20 3 18 4 15 5 11 6 6} + +do_execsql_test 2.30 { + SELECT a, sum(d) OVER ( + ORDER BY b + RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + ) FROM t1 +} {2 21 4 21 6 21 1 9 3 9 5 9} + +do_execsql_test 3.1 { + SELECT a, sum(d) OVER ( + PARTITION BY b ORDER BY d + RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + ) FROM t1 +} {2 12 4 10 6 6 1 9 3 8 5 5} + +do_execsql_test 3.2 { + SELECT a, sum(d) OVER ( + ORDER BY b + RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + ) FROM t1 +} {2 21 4 21 6 21 1 9 3 9 5 9} + +do_execsql_test 3.3 { + SELECT a, sum(d) OVER ( + ORDER BY d + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) FROM t1 +} {1 21 2 21 3 21 4 21 5 21 6 21} + +do_execsql_test 3.4 { + SELECT a, sum(d) OVER ( + ORDER BY d/2 + ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW + ) FROM t1 +} {1 1 2 3 3 6 4 10 5 15 6 21} + +#========================================================================== + +do_execsql_test 4.0 { + DROP TABLE IF EXISTS t2; + CREATE TABLE t2(a INTEGER PRIMARY KEY, b INTEGER); + INSERT INTO t2(a, b) VALUES + (1,0), (2,74), (3,41), (4,74), (5,23), (6,99), (7,26), (8,33), (9,2), + (10,89), (11,81), (12,96), (13,59), (14,38), (15,68), (16,39), (17,62), + (18,91), (19,46), (20,6), (21,99), (22,97), (23,27), (24,46), (25,78), + (26,54), (27,97), (28,8), (29,67), (30,29), (31,93), (32,84), (33,77), + (34,23), (35,16), (36,16), (37,93), (38,65), (39,35), (40,47), (41,7), + (42,86), (43,74), (44,61), (45,91), (46,85), (47,24), (48,85), (49,43), + (50,59), (51,12), (52,32), (53,56), (54,3), (55,91), (56,22), (57,90), + (58,55), (59,15), (60,28), (61,89), (62,25), (63,47), (64,1), (65,56), + (66,40), (67,43), (68,56), (69,16), (70,75), (71,36), (72,89), (73,98), + (74,76), (75,81), (76,4), (77,94), (78,42), (79,30), (80,78), (81,33), + (82,29), (83,53), (84,63), (85,2), (86,87), (87,37), (88,80), (89,84), + (90,72), (91,41), (92,9), (93,61), (94,73), (95,95), (96,65), (97,13), + (98,58), (99,96), (100,98), (101,1), (102,21), (103,74), (104,65), (105,35), + (106,5), (107,73), (108,11), (109,51), (110,87), (111,41), (112,12), (113,8), + (114,20), (115,31), (116,31), (117,15), (118,95), (119,22), (120,73), + (121,79), (122,88), (123,34), (124,8), (125,11), (126,49), (127,34), + (128,90), (129,59), (130,96), (131,60), (132,55), (133,75), (134,77), + (135,44), (136,2), (137,7), (138,85), (139,57), (140,74), (141,29), (142,70), + (143,59), (144,19), (145,39), (146,26), (147,26), (148,47), (149,80), + (150,90), (151,36), (152,58), (153,47), (154,9), (155,72), (156,72), (157,66), + (158,33), (159,93), (160,75), (161,64), (162,81), (163,9), (164,23), (165,37), + (166,13), (167,12), (168,14), (169,62), (170,91), (171,36), (172,91), + (173,33), (174,15), (175,34), (176,36), (177,99), (178,3), (179,95), (180,69), + (181,58), (182,52), (183,30), (184,50), (185,84), (186,10), (187,84), + (188,33), (189,21), (190,39), (191,44), (192,58), (193,30), (194,38), + (195,34), (196,83), (197,27), (198,82), (199,17), (200,7); +} {} + +do_execsql_test 4.1 { + SELECT a, sum(b) OVER ( + PARTITION BY (b%10) + ORDER BY b + ) FROM t2 ORDER BY a; +} {1 0 2 754 3 251 4 754 5 101 6 1247 7 132 8 266 9 6 10 950 + 11 667 12 1052 13 535 14 128 15 428 16 250 17 336 18 1122 + 19 368 20 6 21 1247 22 1000 23 92 24 368 25 584 26 320 + 27 1000 28 24 29 478 30 133 31 1049 32 1090 33 632 34 101 + 35 54 36 54 37 1049 38 450 39 145 40 354 41 21 42 764 + 43 754 44 424 45 1122 46 930 47 42 48 930 49 352 50 535 + 51 42 52 118 53 536 54 6 55 1122 56 86 57 770 58 255 59 50 + 60 52 61 950 62 75 63 354 64 2 65 536 66 160 67 352 68 536 + 69 54 70 675 71 276 72 950 73 868 74 678 75 667 76 4 + 77 1184 78 160 79 120 80 584 81 266 82 133 83 405 84 468 + 85 6 86 806 87 166 88 500 89 1090 90 552 91 251 92 27 + 93 424 94 687 95 1215 96 450 97 32 98 360 99 1052 100 868 + 101 2 102 66 103 754 104 450 105 145 106 5 107 687 108 24 + 109 302 110 806 111 251 112 42 113 24 114 30 115 128 116 128 + 117 50 118 1215 119 86 120 687 121 683 122 672 123 178 124 24 + 125 24 126 299 127 178 128 770 129 535 130 1052 131 270 + 132 255 133 675 134 632 135 266 136 6 137 21 138 930 139 411 + 140 754 141 133 142 340 143 535 144 46 145 250 146 132 + 147 132 148 354 149 500 150 770 151 276 152 360 153 354 + 154 27 155 552 156 552 157 602 158 266 159 1049 160 675 + 161 384 162 667 163 27 164 101 165 166 166 32 167 42 168 18 + 169 336 170 1122 171 276 172 1122 173 266 174 50 175 178 + 176 276 177 1247 178 6 179 1215 180 604 181 360 182 212 + 183 120 184 210 185 1090 186 10 187 1090 188 266 189 66 + 190 250 191 266 192 360 193 120 194 128 195 178 196 770 + 197 92 198 634 199 38 200 21} + +do_execsql_test 4.2 { + SELECT a, sum(b) OVER ( + PARTITION BY (b%10) + ORDER BY b + RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW + ) FROM t2 ORDER BY a; +} {1 0 2 754 3 251 4 754 5 101 6 1247 7 132 8 266 9 6 10 950 + 11 667 12 1052 13 535 14 128 15 428 16 250 17 336 18 1122 + 19 368 20 6 21 1247 22 1000 23 92 24 368 25 584 26 320 + 27 1000 28 24 29 478 30 133 31 1049 32 1090 33 632 34 101 + 35 54 36 54 37 1049 38 450 39 145 40 354 41 21 42 764 + 43 754 44 424 45 1122 46 930 47 42 48 930 49 352 50 535 + 51 42 52 118 53 536 54 6 55 1122 56 86 57 770 58 255 59 50 + 60 52 61 950 62 75 63 354 64 2 65 536 66 160 67 352 68 536 + 69 54 70 675 71 276 72 950 73 868 74 678 75 667 76 4 + 77 1184 78 160 79 120 80 584 81 266 82 133 83 405 84 468 + 85 6 86 806 87 166 88 500 89 1090 90 552 91 251 92 27 + 93 424 94 687 95 1215 96 450 97 32 98 360 99 1052 100 868 + 101 2 102 66 103 754 104 450 105 145 106 5 107 687 108 24 + 109 302 110 806 111 251 112 42 113 24 114 30 115 128 116 128 + 117 50 118 1215 119 86 120 687 121 683 122 672 123 178 124 24 + 125 24 126 299 127 178 128 770 129 535 130 1052 131 270 + 132 255 133 675 134 632 135 266 136 6 137 21 138 930 139 411 + 140 754 141 133 142 340 143 535 144 46 145 250 146 132 + 147 132 148 354 149 500 150 770 151 276 152 360 153 354 + 154 27 155 552 156 552 157 602 158 266 159 1049 160 675 + 161 384 162 667 163 27 164 101 165 166 166 32 167 42 168 18 + 169 336 170 1122 171 276 172 1122 173 266 174 50 175 178 + 176 276 177 1247 178 6 179 1215 180 604 181 360 182 212 + 183 120 184 210 185 1090 186 10 187 1090 188 266 189 66 + 190 250 191 266 192 360 193 120 194 128 195 178 196 770 + 197 92 198 634 199 38 200 21} + +do_execsql_test 4.3 { + SELECT b, sum(b) OVER ( + ORDER BY b + ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW + ) FROM t2 ORDER BY b; +} {0 0 1 1 1 2 2 4 2 6 2 8 3 11 3 14 4 18 5 23 6 29 7 36 + 7 43 7 50 8 58 8 66 8 74 9 83 9 92 9 101 10 111 11 122 + 11 133 12 145 12 157 12 169 13 182 13 195 14 209 15 224 + 15 239 15 254 16 270 16 286 16 302 17 319 19 338 20 358 + 21 379 21 400 22 422 22 444 23 467 23 490 23 513 24 537 + 25 562 26 588 26 614 26 640 27 667 27 694 28 722 29 751 + 29 780 29 809 30 839 30 869 30 899 31 930 31 961 32 993 + 33 1026 33 1059 33 1092 33 1125 33 1158 34 1192 34 1226 + 34 1260 34 1294 35 1329 35 1364 36 1400 36 1436 36 1472 + 36 1508 37 1545 37 1582 38 1620 38 1658 39 1697 39 1736 + 39 1775 40 1815 41 1856 41 1897 41 1938 42 1980 43 2023 + 43 2066 44 2110 44 2154 46 2200 46 2246 47 2293 47 2340 + 47 2387 47 2434 49 2483 50 2533 51 2584 52 2636 53 2689 + 54 2743 55 2798 55 2853 56 2909 56 2965 56 3021 57 3078 + 58 3136 58 3194 58 3252 58 3310 59 3369 59 3428 59 3487 + 59 3546 60 3606 61 3667 61 3728 62 3790 62 3852 63 3915 + 64 3979 65 4044 65 4109 65 4174 66 4240 67 4307 68 4375 + 69 4444 70 4514 72 4586 72 4658 72 4730 73 4803 73 4876 + 73 4949 74 5023 74 5097 74 5171 74 5245 74 5319 75 5394 + 75 5469 75 5544 76 5620 77 5697 77 5774 78 5852 78 5930 + 79 6009 80 6089 80 6169 81 6250 81 6331 81 6412 82 6494 + 83 6577 84 6661 84 6745 84 6829 84 6913 85 6998 85 7083 + 85 7168 86 7254 87 7341 87 7428 88 7516 89 7605 89 7694 + 89 7783 90 7873 90 7963 90 8053 91 8144 91 8235 91 8326 + 91 8417 91 8508 93 8601 93 8694 93 8787 94 8881 95 8976 + 95 9071 95 9166 96 9262 96 9358 96 9454 97 9551 97 9648 + 98 9746 98 9844 99 9943 99 10042 99 10141} + +do_execsql_test 4.4 { + SELECT b, sum(b) OVER ( + ORDER BY b + RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) FROM t2 ORDER BY b; +} {0 10141 1 10141 1 10141 2 10141 2 10141 2 10141 3 10141 + 3 10141 4 10141 5 10141 6 10141 7 10141 7 10141 7 10141 + 8 10141 8 10141 8 10141 9 10141 9 10141 9 10141 10 10141 + 11 10141 11 10141 12 10141 12 10141 12 10141 13 10141 13 10141 + 14 10141 15 10141 15 10141 15 10141 16 10141 16 10141 16 10141 + 17 10141 19 10141 20 10141 21 10141 21 10141 22 10141 22 10141 + 23 10141 23 10141 23 10141 24 10141 25 10141 26 10141 26 10141 + 26 10141 27 10141 27 10141 28 10141 29 10141 29 10141 29 10141 + 30 10141 30 10141 30 10141 31 10141 31 10141 32 10141 33 10141 + 33 10141 33 10141 33 10141 33 10141 34 10141 34 10141 34 10141 + 34 10141 35 10141 35 10141 36 10141 36 10141 36 10141 36 10141 + 37 10141 37 10141 38 10141 38 10141 39 10141 39 10141 39 10141 + 40 10141 41 10141 41 10141 41 10141 42 10141 43 10141 43 10141 + 44 10141 44 10141 46 10141 46 10141 47 10141 47 10141 47 10141 + 47 10141 49 10141 50 10141 51 10141 52 10141 53 10141 54 10141 + 55 10141 55 10141 56 10141 56 10141 56 10141 57 10141 58 10141 + 58 10141 58 10141 58 10141 59 10141 59 10141 59 10141 59 10141 + 60 10141 61 10141 61 10141 62 10141 62 10141 63 10141 64 10141 + 65 10141 65 10141 65 10141 66 10141 67 10141 68 10141 69 10141 + 70 10141 72 10141 72 10141 72 10141 73 10141 73 10141 73 10141 + 74 10141 74 10141 74 10141 74 10141 74 10141 75 10141 75 10141 + 75 10141 76 10141 77 10141 77 10141 78 10141 78 10141 79 10141 + 80 10141 80 10141 81 10141 81 10141 81 10141 82 10141 83 10141 + 84 10141 84 10141 84 10141 84 10141 85 10141 85 10141 85 10141 + 86 10141 87 10141 87 10141 88 10141 89 10141 89 10141 89 10141 + 90 10141 90 10141 90 10141 91 10141 91 10141 91 10141 91 10141 + 91 10141 93 10141 93 10141 93 10141 94 10141 95 10141 95 10141 + 95 10141 96 10141 96 10141 96 10141 97 10141 97 10141 98 10141 + 98 10141 99 10141 99 10141 99 10141} + +do_execsql_test 4.5 { + SELECT b, sum(b) OVER ( + ORDER BY b + RANGE BETWEEN CURRENT ROW AND CURRENT ROW + ) FROM t2 ORDER BY b; +} {0 0 1 2 1 2 2 6 2 6 2 6 3 6 3 6 4 4 5 5 6 6 7 21 + 7 21 7 21 8 24 8 24 8 24 9 27 9 27 9 27 10 10 11 22 + 11 22 12 36 12 36 12 36 13 26 13 26 14 14 15 45 15 45 + 15 45 16 48 16 48 16 48 17 17 19 19 20 20 21 42 21 42 + 22 44 22 44 23 69 23 69 23 69 24 24 25 25 26 78 26 78 + 26 78 27 54 27 54 28 28 29 87 29 87 29 87 30 90 30 90 + 30 90 31 62 31 62 32 32 33 165 33 165 33 165 33 165 33 165 + 34 136 34 136 34 136 34 136 35 70 35 70 36 144 36 144 + 36 144 36 144 37 74 37 74 38 76 38 76 39 117 39 117 39 117 + 40 40 41 123 41 123 41 123 42 42 43 86 43 86 44 88 44 88 + 46 92 46 92 47 188 47 188 47 188 47 188 49 49 50 50 51 51 + 52 52 53 53 54 54 55 110 55 110 56 168 56 168 56 168 57 57 + 58 232 58 232 58 232 58 232 59 236 59 236 59 236 59 236 + 60 60 61 122 61 122 62 124 62 124 63 63 64 64 65 195 65 195 + 65 195 66 66 67 67 68 68 69 69 70 70 72 216 72 216 72 216 + 73 219 73 219 73 219 74 370 74 370 74 370 74 370 74 370 + 75 225 75 225 75 225 76 76 77 154 77 154 78 156 78 156 + 79 79 80 160 80 160 81 243 81 243 81 243 82 82 83 83 84 336 + 84 336 84 336 84 336 85 255 85 255 85 255 86 86 87 174 + 87 174 88 88 89 267 89 267 89 267 90 270 90 270 90 270 + 91 455 91 455 91 455 91 455 91 455 93 279 93 279 93 279 + 94 94 95 285 95 285 95 285 96 288 96 288 96 288 97 194 + 97 194 98 196 98 196 99 297 99 297 99 297} + +do_execsql_test 4.6.1 { + SELECT b, sum(b) OVER ( + RANGE BETWEEN CURRENT ROW AND CURRENT ROW + ) FROM t2 ORDER BY b; +} {0 10141 1 10141 1 10141 2 10141 2 10141 2 10141 3 10141 + 3 10141 4 10141 5 10141 6 10141 7 10141 7 10141 7 10141 + 8 10141 8 10141 8 10141 9 10141 9 10141 9 10141 10 10141 + 11 10141 11 10141 12 10141 12 10141 12 10141 13 10141 13 10141 + 14 10141 15 10141 15 10141 15 10141 16 10141 16 10141 16 10141 + 17 10141 19 10141 20 10141 21 10141 21 10141 22 10141 22 10141 + 23 10141 23 10141 23 10141 24 10141 25 10141 26 10141 26 10141 + 26 10141 27 10141 27 10141 28 10141 29 10141 29 10141 29 10141 + 30 10141 30 10141 30 10141 31 10141 31 10141 32 10141 33 10141 + 33 10141 33 10141 33 10141 33 10141 34 10141 34 10141 34 10141 + 34 10141 35 10141 35 10141 36 10141 36 10141 36 10141 36 10141 + 37 10141 37 10141 38 10141 38 10141 39 10141 39 10141 39 10141 + 40 10141 41 10141 41 10141 41 10141 42 10141 43 10141 43 10141 + 44 10141 44 10141 46 10141 46 10141 47 10141 47 10141 47 10141 + 47 10141 49 10141 50 10141 51 10141 52 10141 53 10141 54 10141 + 55 10141 55 10141 56 10141 56 10141 56 10141 57 10141 58 10141 + 58 10141 58 10141 58 10141 59 10141 59 10141 59 10141 59 10141 + 60 10141 61 10141 61 10141 62 10141 62 10141 63 10141 64 10141 + 65 10141 65 10141 65 10141 66 10141 67 10141 68 10141 69 10141 + 70 10141 72 10141 72 10141 72 10141 73 10141 73 10141 73 10141 + 74 10141 74 10141 74 10141 74 10141 74 10141 75 10141 75 10141 + 75 10141 76 10141 77 10141 77 10141 78 10141 78 10141 79 10141 + 80 10141 80 10141 81 10141 81 10141 81 10141 82 10141 83 10141 + 84 10141 84 10141 84 10141 84 10141 85 10141 85 10141 85 10141 + 86 10141 87 10141 87 10141 88 10141 89 10141 89 10141 89 10141 + 90 10141 90 10141 90 10141 91 10141 91 10141 91 10141 91 10141 + 91 10141 93 10141 93 10141 93 10141 94 10141 95 10141 95 10141 + 95 10141 96 10141 96 10141 96 10141 97 10141 97 10141 98 10141 + 98 10141 99 10141 99 10141 99 10141} + +do_execsql_test 4.6.2 { + SELECT b, sum(b) OVER () FROM t2 ORDER BY b; +} {0 10141 1 10141 1 10141 2 10141 2 10141 2 10141 3 10141 + 3 10141 4 10141 5 10141 6 10141 7 10141 7 10141 7 10141 + 8 10141 8 10141 8 10141 9 10141 9 10141 9 10141 10 10141 + 11 10141 11 10141 12 10141 12 10141 12 10141 13 10141 13 10141 + 14 10141 15 10141 15 10141 15 10141 16 10141 16 10141 16 10141 + 17 10141 19 10141 20 10141 21 10141 21 10141 22 10141 22 10141 + 23 10141 23 10141 23 10141 24 10141 25 10141 26 10141 26 10141 + 26 10141 27 10141 27 10141 28 10141 29 10141 29 10141 29 10141 + 30 10141 30 10141 30 10141 31 10141 31 10141 32 10141 33 10141 + 33 10141 33 10141 33 10141 33 10141 34 10141 34 10141 34 10141 + 34 10141 35 10141 35 10141 36 10141 36 10141 36 10141 36 10141 + 37 10141 37 10141 38 10141 38 10141 39 10141 39 10141 39 10141 + 40 10141 41 10141 41 10141 41 10141 42 10141 43 10141 43 10141 + 44 10141 44 10141 46 10141 46 10141 47 10141 47 10141 47 10141 + 47 10141 49 10141 50 10141 51 10141 52 10141 53 10141 54 10141 + 55 10141 55 10141 56 10141 56 10141 56 10141 57 10141 58 10141 + 58 10141 58 10141 58 10141 59 10141 59 10141 59 10141 59 10141 + 60 10141 61 10141 61 10141 62 10141 62 10141 63 10141 64 10141 + 65 10141 65 10141 65 10141 66 10141 67 10141 68 10141 69 10141 + 70 10141 72 10141 72 10141 72 10141 73 10141 73 10141 73 10141 + 74 10141 74 10141 74 10141 74 10141 74 10141 75 10141 75 10141 + 75 10141 76 10141 77 10141 77 10141 78 10141 78 10141 79 10141 + 80 10141 80 10141 81 10141 81 10141 81 10141 82 10141 83 10141 + 84 10141 84 10141 84 10141 84 10141 85 10141 85 10141 85 10141 + 86 10141 87 10141 87 10141 88 10141 89 10141 89 10141 89 10141 + 90 10141 90 10141 90 10141 91 10141 91 10141 91 10141 91 10141 + 91 10141 93 10141 93 10141 93 10141 94 10141 95 10141 95 10141 + 95 10141 96 10141 96 10141 96 10141 97 10141 97 10141 98 10141 + 98 10141 99 10141 99 10141 99 10141} + +do_execsql_test 4.6.3 { + SELECT b, sum(b) OVER ( + RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) FROM t2 ORDER BY b; +} {0 10141 1 10141 1 10141 2 10141 2 10141 2 10141 3 10141 + 3 10141 4 10141 5 10141 6 10141 7 10141 7 10141 7 10141 + 8 10141 8 10141 8 10141 9 10141 9 10141 9 10141 10 10141 + 11 10141 11 10141 12 10141 12 10141 12 10141 13 10141 13 10141 + 14 10141 15 10141 15 10141 15 10141 16 10141 16 10141 16 10141 + 17 10141 19 10141 20 10141 21 10141 21 10141 22 10141 22 10141 + 23 10141 23 10141 23 10141 24 10141 25 10141 26 10141 26 10141 + 26 10141 27 10141 27 10141 28 10141 29 10141 29 10141 29 10141 + 30 10141 30 10141 30 10141 31 10141 31 10141 32 10141 33 10141 + 33 10141 33 10141 33 10141 33 10141 34 10141 34 10141 34 10141 + 34 10141 35 10141 35 10141 36 10141 36 10141 36 10141 36 10141 + 37 10141 37 10141 38 10141 38 10141 39 10141 39 10141 39 10141 + 40 10141 41 10141 41 10141 41 10141 42 10141 43 10141 43 10141 + 44 10141 44 10141 46 10141 46 10141 47 10141 47 10141 47 10141 + 47 10141 49 10141 50 10141 51 10141 52 10141 53 10141 54 10141 + 55 10141 55 10141 56 10141 56 10141 56 10141 57 10141 58 10141 + 58 10141 58 10141 58 10141 59 10141 59 10141 59 10141 59 10141 + 60 10141 61 10141 61 10141 62 10141 62 10141 63 10141 64 10141 + 65 10141 65 10141 65 10141 66 10141 67 10141 68 10141 69 10141 + 70 10141 72 10141 72 10141 72 10141 73 10141 73 10141 73 10141 + 74 10141 74 10141 74 10141 74 10141 74 10141 75 10141 75 10141 + 75 10141 76 10141 77 10141 77 10141 78 10141 78 10141 79 10141 + 80 10141 80 10141 81 10141 81 10141 81 10141 82 10141 83 10141 + 84 10141 84 10141 84 10141 84 10141 85 10141 85 10141 85 10141 + 86 10141 87 10141 87 10141 88 10141 89 10141 89 10141 89 10141 + 90 10141 90 10141 90 10141 91 10141 91 10141 91 10141 91 10141 + 91 10141 93 10141 93 10141 93 10141 94 10141 95 10141 95 10141 + 95 10141 96 10141 96 10141 96 10141 97 10141 97 10141 98 10141 + 98 10141 99 10141 99 10141 99 10141} + +do_execsql_test 4.6.4 { + SELECT b, sum(b) OVER ( + RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + ) FROM t2 ORDER BY b; +} {0 10141 1 10141 1 10141 2 10141 2 10141 2 10141 3 10141 + 3 10141 4 10141 5 10141 6 10141 7 10141 7 10141 7 10141 + 8 10141 8 10141 8 10141 9 10141 9 10141 9 10141 10 10141 + 11 10141 11 10141 12 10141 12 10141 12 10141 13 10141 13 10141 + 14 10141 15 10141 15 10141 15 10141 16 10141 16 10141 16 10141 + 17 10141 19 10141 20 10141 21 10141 21 10141 22 10141 22 10141 + 23 10141 23 10141 23 10141 24 10141 25 10141 26 10141 26 10141 + 26 10141 27 10141 27 10141 28 10141 29 10141 29 10141 29 10141 + 30 10141 30 10141 30 10141 31 10141 31 10141 32 10141 33 10141 + 33 10141 33 10141 33 10141 33 10141 34 10141 34 10141 34 10141 + 34 10141 35 10141 35 10141 36 10141 36 10141 36 10141 36 10141 + 37 10141 37 10141 38 10141 38 10141 39 10141 39 10141 39 10141 + 40 10141 41 10141 41 10141 41 10141 42 10141 43 10141 43 10141 + 44 10141 44 10141 46 10141 46 10141 47 10141 47 10141 47 10141 + 47 10141 49 10141 50 10141 51 10141 52 10141 53 10141 54 10141 + 55 10141 55 10141 56 10141 56 10141 56 10141 57 10141 58 10141 + 58 10141 58 10141 58 10141 59 10141 59 10141 59 10141 59 10141 + 60 10141 61 10141 61 10141 62 10141 62 10141 63 10141 64 10141 + 65 10141 65 10141 65 10141 66 10141 67 10141 68 10141 69 10141 + 70 10141 72 10141 72 10141 72 10141 73 10141 73 10141 73 10141 + 74 10141 74 10141 74 10141 74 10141 74 10141 75 10141 75 10141 + 75 10141 76 10141 77 10141 77 10141 78 10141 78 10141 79 10141 + 80 10141 80 10141 81 10141 81 10141 81 10141 82 10141 83 10141 + 84 10141 84 10141 84 10141 84 10141 85 10141 85 10141 85 10141 + 86 10141 87 10141 87 10141 88 10141 89 10141 89 10141 89 10141 + 90 10141 90 10141 90 10141 91 10141 91 10141 91 10141 91 10141 + 91 10141 93 10141 93 10141 93 10141 94 10141 95 10141 95 10141 + 95 10141 96 10141 96 10141 96 10141 97 10141 97 10141 98 10141 + 98 10141 99 10141 99 10141 99 10141} + +do_execsql_test 4.7.1 { + SELECT b, sum(b) OVER ( + ROWS BETWEEN CURRENT ROW AND CURRENT ROW + ) FROM t2 ORDER BY 1, 2; +} {0 0 1 1 1 1 2 2 2 2 2 2 3 3 3 3 4 4 5 5 6 6 7 7 7 7 + 7 7 8 8 8 8 8 8 9 9 9 9 9 9 10 10 11 11 11 11 12 12 + 12 12 12 12 13 13 13 13 14 14 15 15 15 15 15 15 16 16 + 16 16 16 16 17 17 19 19 20 20 21 21 21 21 22 22 22 22 + 23 23 23 23 23 23 24 24 25 25 26 26 26 26 26 26 27 27 + 27 27 28 28 29 29 29 29 29 29 30 30 30 30 30 30 31 31 + 31 31 32 32 33 33 33 33 33 33 33 33 33 33 34 34 34 34 + 34 34 34 34 35 35 35 35 36 36 36 36 36 36 36 36 37 37 + 37 37 38 38 38 38 39 39 39 39 39 39 40 40 41 41 41 41 + 41 41 42 42 43 43 43 43 44 44 44 44 46 46 46 46 47 47 + 47 47 47 47 47 47 49 49 50 50 51 51 52 52 53 53 54 54 + 55 55 55 55 56 56 56 56 56 56 57 57 58 58 58 58 58 58 + 58 58 59 59 59 59 59 59 59 59 60 60 61 61 61 61 62 62 + 62 62 63 63 64 64 65 65 65 65 65 65 66 66 67 67 68 68 + 69 69 70 70 72 72 72 72 72 72 73 73 73 73 73 73 74 74 + 74 74 74 74 74 74 74 74 75 75 75 75 75 75 76 76 77 77 + 77 77 78 78 78 78 79 79 80 80 80 80 81 81 81 81 81 81 + 82 82 83 83 84 84 84 84 84 84 84 84 85 85 85 85 85 85 + 86 86 87 87 87 87 88 88 89 89 89 89 89 89 90 90 90 90 + 90 90 91 91 91 91 91 91 91 91 91 91 93 93 93 93 93 93 + 94 94 95 95 95 95 95 95 96 96 96 96 96 96 97 97 97 97 + 98 98 98 98 99 99 99 99 99 99} + +do_execsql_test 4.7.2 { + SELECT b, sum(b) OVER ( + ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW + ) FROM t2 ORDER BY 1, 2; +} {0 0 1 3379 1 5443 2 372 2 4473 2 7074 3 2916 3 9096 4 4049 + 5 5643 6 1047 7 2205 7 7081 7 10141 8 1553 8 5926 8 6422 + 9 4883 9 7932 9 8497 10 9544 11 5727 11 6433 12 2825 12 5918 + 12 8582 13 5190 13 8570 14 8596 15 3189 15 6023 15 8924 + 16 1942 16 1958 16 3590 17 10134 19 7474 20 5946 21 5464 + 21 9682 22 3029 22 6140 23 212 23 1926 23 8520 24 2626 + 25 3331 26 337 26 7539 26 7565 27 1270 27 10035 28 3217 + 29 1649 29 4355 29 7326 30 4215 30 9400 30 9853 31 5977 + 31 6008 32 2857 33 370 33 4326 33 8175 33 8909 33 9661 + 34 6414 34 6516 34 8958 34 9925 35 2151 35 5638 36 3701 + 36 7818 36 8785 36 8994 37 4597 37 8557 38 735 38 9891 39 842 + 39 7513 39 9721 40 3475 41 115 41 4874 41 5906 42 4185 + 43 2754 43 3518 44 7072 44 9765 46 1041 46 1316 47 2198 + 47 3378 47 7612 47 7923 49 6482 50 9450 51 5778 52 9370 + 53 4408 54 1448 55 3174 55 6876 56 2913 56 3435 56 3574 + 57 7223 58 5248 58 7876 58 9318 58 9823 59 697 59 2813 + 59 6665 59 7455 60 6821 61 2426 61 4944 62 904 62 8658 + 63 4471 64 8407 65 2116 65 5177 65 5603 66 8142 67 1620 + 68 803 69 9260 70 7396 72 4833 72 8004 72 8076 73 5017 + 73 5716 73 6213 74 74 74 189 74 2365 74 5538 74 7297 75 3665 + 75 6951 75 8343 76 3964 77 1903 77 7028 78 1394 78 4293 + 79 6292 80 4677 80 7692 81 542 81 4045 81 8488 82 10117 + 83 10008 84 1826 84 4761 84 9534 84 9628 85 2602 85 2711 + 85 7166 86 2291 87 4560 87 5865 88 6380 89 461 89 3306 + 89 3790 90 3119 90 6606 90 7782 91 995 91 2517 91 3007 + 91 8749 91 8876 93 1742 93 2051 93 8268 94 4143 95 5112 + 95 6118 95 9191 96 638 96 5344 96 6761 97 1243 97 1545 + 98 3888 98 5442 99 311 99 1146 99 9093} + +do_execsql_test 4.7.3 { + SELECT b, sum(b) OVER ( + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) FROM t2 ORDER BY 1, 2; +} {0 10141 1 10141 1 10141 2 10141 2 10141 2 10141 3 10141 + 3 10141 4 10141 5 10141 6 10141 7 10141 7 10141 7 10141 + 8 10141 8 10141 8 10141 9 10141 9 10141 9 10141 10 10141 + 11 10141 11 10141 12 10141 12 10141 12 10141 13 10141 13 10141 + 14 10141 15 10141 15 10141 15 10141 16 10141 16 10141 16 10141 + 17 10141 19 10141 20 10141 21 10141 21 10141 22 10141 22 10141 + 23 10141 23 10141 23 10141 24 10141 25 10141 26 10141 26 10141 + 26 10141 27 10141 27 10141 28 10141 29 10141 29 10141 29 10141 + 30 10141 30 10141 30 10141 31 10141 31 10141 32 10141 33 10141 + 33 10141 33 10141 33 10141 33 10141 34 10141 34 10141 34 10141 + 34 10141 35 10141 35 10141 36 10141 36 10141 36 10141 36 10141 + 37 10141 37 10141 38 10141 38 10141 39 10141 39 10141 39 10141 + 40 10141 41 10141 41 10141 41 10141 42 10141 43 10141 43 10141 + 44 10141 44 10141 46 10141 46 10141 47 10141 47 10141 47 10141 + 47 10141 49 10141 50 10141 51 10141 52 10141 53 10141 54 10141 + 55 10141 55 10141 56 10141 56 10141 56 10141 57 10141 58 10141 + 58 10141 58 10141 58 10141 59 10141 59 10141 59 10141 59 10141 + 60 10141 61 10141 61 10141 62 10141 62 10141 63 10141 64 10141 + 65 10141 65 10141 65 10141 66 10141 67 10141 68 10141 69 10141 + 70 10141 72 10141 72 10141 72 10141 73 10141 73 10141 73 10141 + 74 10141 74 10141 74 10141 74 10141 74 10141 75 10141 75 10141 + 75 10141 76 10141 77 10141 77 10141 78 10141 78 10141 79 10141 + 80 10141 80 10141 81 10141 81 10141 81 10141 82 10141 83 10141 + 84 10141 84 10141 84 10141 84 10141 85 10141 85 10141 85 10141 + 86 10141 87 10141 87 10141 88 10141 89 10141 89 10141 89 10141 + 90 10141 90 10141 90 10141 91 10141 91 10141 91 10141 91 10141 + 91 10141 93 10141 93 10141 93 10141 94 10141 95 10141 95 10141 + 95 10141 96 10141 96 10141 96 10141 97 10141 97 10141 98 10141 + 98 10141 99 10141 99 10141 99 10141} + +do_execsql_test 4.7.4 { + SELECT b, sum(b) OVER ( + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + ) FROM t2 ORDER BY 1, 2; +} {0 10141 1 4699 1 6763 2 3069 2 5670 2 9771 3 1048 3 7228 + 4 6096 5 4503 6 9100 7 7 7 3067 7 7943 8 3727 8 4223 8 8596 + 9 1653 9 2218 9 5267 10 607 11 3719 11 4425 12 1571 12 4235 + 12 7328 13 1584 13 4964 14 1559 15 1232 15 4133 15 6967 + 16 6567 16 8199 16 8215 17 24 19 2686 20 4215 21 480 21 4698 + 22 4023 22 7134 23 1644 23 8238 23 9952 24 7539 25 6835 + 26 2602 26 2628 26 9830 27 133 27 8898 28 6952 29 2844 + 29 5815 29 8521 30 318 30 771 30 5956 31 4164 31 4195 32 7316 + 33 513 33 1265 33 1999 33 5848 33 9804 34 250 34 1217 34 3659 + 34 3761 35 4538 35 8025 36 1183 36 1392 36 2359 36 6476 + 37 1621 37 5581 38 288 38 9444 39 459 39 2667 39 9338 40 6706 + 41 4276 41 5308 41 10067 42 5998 43 6666 43 7430 44 420 + 44 3113 46 8871 46 9146 47 2265 47 2576 47 6810 47 7990 + 49 3708 50 741 51 4414 52 823 53 5786 54 8747 55 3320 55 7022 + 56 6623 56 6762 56 7284 57 2975 58 376 58 881 58 2323 58 4951 + 59 2745 59 3535 59 7387 59 9503 60 3380 61 5258 61 7776 + 62 1545 62 9299 63 5733 64 1798 65 4603 65 5029 65 8090 + 66 2065 67 8588 68 9406 69 950 70 2815 72 2137 72 2209 + 72 5380 73 4001 73 4498 73 5197 74 2918 74 4677 74 7850 + 74 10026 74 10141 75 1873 75 3265 75 6551 76 6253 77 3190 + 77 8315 78 5926 78 8825 79 3928 80 2529 80 5544 81 1734 + 81 6177 81 9680 82 106 83 216 84 597 84 691 84 5464 84 8399 + 85 3060 85 7515 85 7624 86 7936 87 4363 87 5668 88 3849 + 89 6440 89 6924 89 9769 90 2449 90 3625 90 7112 91 1356 + 91 1483 91 7225 91 7715 91 9237 93 1966 93 8183 93 8492 + 94 6092 95 1045 95 4118 95 5124 96 3476 96 4893 96 9599 + 97 8693 97 8995 98 4797 98 6351 99 1147 99 9094 99 9929} + +do_execsql_test 4.8.1 { + SELECT b, sum(b) OVER ( + ORDER BY a + ROWS BETWEEN CURRENT ROW AND CURRENT ROW + ) FROM t2 ORDER BY 1, 2; +} {0 0 1 1 1 1 2 2 2 2 2 2 3 3 3 3 4 4 5 5 6 6 7 7 7 7 + 7 7 8 8 8 8 8 8 9 9 9 9 9 9 10 10 11 11 11 11 12 12 + 12 12 12 12 13 13 13 13 14 14 15 15 15 15 15 15 16 16 + 16 16 16 16 17 17 19 19 20 20 21 21 21 21 22 22 22 22 + 23 23 23 23 23 23 24 24 25 25 26 26 26 26 26 26 27 27 + 27 27 28 28 29 29 29 29 29 29 30 30 30 30 30 30 31 31 + 31 31 32 32 33 33 33 33 33 33 33 33 33 33 34 34 34 34 + 34 34 34 34 35 35 35 35 36 36 36 36 36 36 36 36 37 37 + 37 37 38 38 38 38 39 39 39 39 39 39 40 40 41 41 41 41 + 41 41 42 42 43 43 43 43 44 44 44 44 46 46 46 46 47 47 + 47 47 47 47 47 47 49 49 50 50 51 51 52 52 53 53 54 54 + 55 55 55 55 56 56 56 56 56 56 57 57 58 58 58 58 58 58 + 58 58 59 59 59 59 59 59 59 59 60 60 61 61 61 61 62 62 + 62 62 63 63 64 64 65 65 65 65 65 65 66 66 67 67 68 68 + 69 69 70 70 72 72 72 72 72 72 73 73 73 73 73 73 74 74 + 74 74 74 74 74 74 74 74 75 75 75 75 75 75 76 76 77 77 + 77 77 78 78 78 78 79 79 80 80 80 80 81 81 81 81 81 81 + 82 82 83 83 84 84 84 84 84 84 84 84 85 85 85 85 85 85 + 86 86 87 87 87 87 88 88 89 89 89 89 89 89 90 90 90 90 + 90 90 91 91 91 91 91 91 91 91 91 91 93 93 93 93 93 93 + 94 94 95 95 95 95 95 95 96 96 96 96 96 96 97 97 97 97 + 98 98 98 98 99 99 99 99 99 99} + +do_execsql_test 4.8.2 { + SELECT b, sum(b) OVER ( + ORDER BY a + ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW + ) FROM t2 ORDER BY 1, 2; +} {0 0 1 3379 1 5443 2 372 2 4473 2 7074 3 2916 3 9096 4 4049 + 5 5643 6 1047 7 2205 7 7081 7 10141 8 1553 8 5926 8 6422 + 9 4883 9 7932 9 8497 10 9544 11 5727 11 6433 12 2825 12 5918 + 12 8582 13 5190 13 8570 14 8596 15 3189 15 6023 15 8924 + 16 1942 16 1958 16 3590 17 10134 19 7474 20 5946 21 5464 + 21 9682 22 3029 22 6140 23 212 23 1926 23 8520 24 2626 + 25 3331 26 337 26 7539 26 7565 27 1270 27 10035 28 3217 + 29 1649 29 4355 29 7326 30 4215 30 9400 30 9853 31 5977 + 31 6008 32 2857 33 370 33 4326 33 8175 33 8909 33 9661 + 34 6414 34 6516 34 8958 34 9925 35 2151 35 5638 36 3701 + 36 7818 36 8785 36 8994 37 4597 37 8557 38 735 38 9891 39 842 + 39 7513 39 9721 40 3475 41 115 41 4874 41 5906 42 4185 + 43 2754 43 3518 44 7072 44 9765 46 1041 46 1316 47 2198 + 47 3378 47 7612 47 7923 49 6482 50 9450 51 5778 52 9370 + 53 4408 54 1448 55 3174 55 6876 56 2913 56 3435 56 3574 + 57 7223 58 5248 58 7876 58 9318 58 9823 59 697 59 2813 + 59 6665 59 7455 60 6821 61 2426 61 4944 62 904 62 8658 + 63 4471 64 8407 65 2116 65 5177 65 5603 66 8142 67 1620 + 68 803 69 9260 70 7396 72 4833 72 8004 72 8076 73 5017 + 73 5716 73 6213 74 74 74 189 74 2365 74 5538 74 7297 75 3665 + 75 6951 75 8343 76 3964 77 1903 77 7028 78 1394 78 4293 + 79 6292 80 4677 80 7692 81 542 81 4045 81 8488 82 10117 + 83 10008 84 1826 84 4761 84 9534 84 9628 85 2602 85 2711 + 85 7166 86 2291 87 4560 87 5865 88 6380 89 461 89 3306 + 89 3790 90 3119 90 6606 90 7782 91 995 91 2517 91 3007 + 91 8749 91 8876 93 1742 93 2051 93 8268 94 4143 95 5112 + 95 6118 95 9191 96 638 96 5344 96 6761 97 1243 97 1545 + 98 3888 98 5442 99 311 99 1146 99 9093} + +do_execsql_test 4.8.3 { + SELECT b, sum(b) OVER ( + ORDER BY a + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) FROM t2 ORDER BY 1, 2; +} {0 10141 1 10141 1 10141 2 10141 2 10141 2 10141 3 10141 + 3 10141 4 10141 5 10141 6 10141 7 10141 7 10141 7 10141 + 8 10141 8 10141 8 10141 9 10141 9 10141 9 10141 10 10141 + 11 10141 11 10141 12 10141 12 10141 12 10141 13 10141 13 10141 + 14 10141 15 10141 15 10141 15 10141 16 10141 16 10141 16 10141 + 17 10141 19 10141 20 10141 21 10141 21 10141 22 10141 22 10141 + 23 10141 23 10141 23 10141 24 10141 25 10141 26 10141 26 10141 + 26 10141 27 10141 27 10141 28 10141 29 10141 29 10141 29 10141 + 30 10141 30 10141 30 10141 31 10141 31 10141 32 10141 33 10141 + 33 10141 33 10141 33 10141 33 10141 34 10141 34 10141 34 10141 + 34 10141 35 10141 35 10141 36 10141 36 10141 36 10141 36 10141 + 37 10141 37 10141 38 10141 38 10141 39 10141 39 10141 39 10141 + 40 10141 41 10141 41 10141 41 10141 42 10141 43 10141 43 10141 + 44 10141 44 10141 46 10141 46 10141 47 10141 47 10141 47 10141 + 47 10141 49 10141 50 10141 51 10141 52 10141 53 10141 54 10141 + 55 10141 55 10141 56 10141 56 10141 56 10141 57 10141 58 10141 + 58 10141 58 10141 58 10141 59 10141 59 10141 59 10141 59 10141 + 60 10141 61 10141 61 10141 62 10141 62 10141 63 10141 64 10141 + 65 10141 65 10141 65 10141 66 10141 67 10141 68 10141 69 10141 + 70 10141 72 10141 72 10141 72 10141 73 10141 73 10141 73 10141 + 74 10141 74 10141 74 10141 74 10141 74 10141 75 10141 75 10141 + 75 10141 76 10141 77 10141 77 10141 78 10141 78 10141 79 10141 + 80 10141 80 10141 81 10141 81 10141 81 10141 82 10141 83 10141 + 84 10141 84 10141 84 10141 84 10141 85 10141 85 10141 85 10141 + 86 10141 87 10141 87 10141 88 10141 89 10141 89 10141 89 10141 + 90 10141 90 10141 90 10141 91 10141 91 10141 91 10141 91 10141 + 91 10141 93 10141 93 10141 93 10141 94 10141 95 10141 95 10141 + 95 10141 96 10141 96 10141 96 10141 97 10141 97 10141 98 10141 + 98 10141 99 10141 99 10141 99 10141} + +do_execsql_test 4.8.4 { + SELECT b, sum(b) OVER ( + ORDER BY a + ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + ) FROM t2 ORDER BY 1, 2; +} {0 10141 1 4699 1 6763 2 3069 2 5670 2 9771 3 1048 3 7228 + 4 6096 5 4503 6 9100 7 7 7 3067 7 7943 8 3727 8 4223 8 8596 + 9 1653 9 2218 9 5267 10 607 11 3719 11 4425 12 1571 12 4235 + 12 7328 13 1584 13 4964 14 1559 15 1232 15 4133 15 6967 + 16 6567 16 8199 16 8215 17 24 19 2686 20 4215 21 480 21 4698 + 22 4023 22 7134 23 1644 23 8238 23 9952 24 7539 25 6835 + 26 2602 26 2628 26 9830 27 133 27 8898 28 6952 29 2844 + 29 5815 29 8521 30 318 30 771 30 5956 31 4164 31 4195 32 7316 + 33 513 33 1265 33 1999 33 5848 33 9804 34 250 34 1217 34 3659 + 34 3761 35 4538 35 8025 36 1183 36 1392 36 2359 36 6476 + 37 1621 37 5581 38 288 38 9444 39 459 39 2667 39 9338 40 6706 + 41 4276 41 5308 41 10067 42 5998 43 6666 43 7430 44 420 + 44 3113 46 8871 46 9146 47 2265 47 2576 47 6810 47 7990 + 49 3708 50 741 51 4414 52 823 53 5786 54 8747 55 3320 55 7022 + 56 6623 56 6762 56 7284 57 2975 58 376 58 881 58 2323 58 4951 + 59 2745 59 3535 59 7387 59 9503 60 3380 61 5258 61 7776 + 62 1545 62 9299 63 5733 64 1798 65 4603 65 5029 65 8090 + 66 2065 67 8588 68 9406 69 950 70 2815 72 2137 72 2209 + 72 5380 73 4001 73 4498 73 5197 74 2918 74 4677 74 7850 + 74 10026 74 10141 75 1873 75 3265 75 6551 76 6253 77 3190 + 77 8315 78 5926 78 8825 79 3928 80 2529 80 5544 81 1734 + 81 6177 81 9680 82 106 83 216 84 597 84 691 84 5464 84 8399 + 85 3060 85 7515 85 7624 86 7936 87 4363 87 5668 88 3849 + 89 6440 89 6924 89 9769 90 2449 90 3625 90 7112 91 1356 + 91 1483 91 7225 91 7715 91 9237 93 1966 93 8183 93 8492 + 94 6092 95 1045 95 4118 95 5124 96 3476 96 4893 96 9599 + 97 8693 97 8995 98 4797 98 6351 99 1147 99 9094 99 9929} + + +do_test 4.9 { + set myres {} + foreach r [db eval {SELECT + rank() OVER win AS rank, + cume_dist() OVER win AS cume_dist FROM t1 + WINDOW win AS (ORDER BY 1);}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + +do_execsql_test 4.10 { + SELECT count(*) OVER (ORDER BY b) FROM t1 +} {3 3 3 6 6 6} + +do_execsql_test 4.11 { + SELECT count(distinct a) FILTER (WHERE b='odd') FROM t1 +} {3} + +#========================================================================== + +do_execsql_test 5.0 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(x INTEGER, y INTEGER); + INSERT INTO t1 VALUES(10, 1); + INSERT INTO t1 VALUES(20, 2); + INSERT INTO t1 VALUES(3, 3); + INSERT INTO t1 VALUES(2, 4); + INSERT INTO t1 VALUES(1, 5); +} {} + + +do_test 5.1 { + set myres {} + foreach r [db eval {SELECT avg(x) OVER (ORDER BY y) AS z FROM t1 ORDER BY z;}] { + lappend myres [format %.4f [set r]] + } + set res2 {7.2000 8.7500 10.0000 11.0000 15.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + +#========================================================================== + +do_execsql_test 6.0 { + DROP TABLE IF EXISTS t0; + CREATE TABLE t0(c0 INTEGER UNIQUE); + INSERT INTO t0 VALUES(0); +} {} + +do_execsql_test 6.1 { + SELECT DENSE_RANK() OVER(), LAG(0) OVER() FROM t0; +} {1 {}} + +do_execsql_test 6.2 { + SELECT * FROM t0 WHERE + (0, t0.c0) IN (SELECT DENSE_RANK() OVER(), LAG(0) OVER() FROM t0); +} {} + +#========================================================================== + +do_execsql_test 7.0 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a INTEGER, b INTEGER, c INTEGER); + INSERT INTO t1 VALUES(1, 1, 1); + INSERT INTO t1 VALUES(1, 2, 2); + INSERT INTO t1 VALUES(3, 3, 3); + INSERT INTO t1 VALUES(3, 4, 4); +} {} + +do_execsql_test 7.1 { + SELECT c, sum(c) OVER win1 FROM t1 + WINDOW win1 AS (ORDER BY b) +} {1 1 2 3 3 6 4 10} + +do_execsql_test 7.2 { + SELECT c, sum(c) OVER win1 FROM t1 + WINDOW win1 AS (PARTITION BY 1 ORDER BY b) +} {1 1 2 3 3 6 4 10} + +do_execsql_test 7.3 { + SELECT c, sum(c) OVER win1 FROM t1 + WINDOW win1 AS (ORDER BY 1) +} {1 10 2 10 3 10 4 10} + +finish_test diff --git a/test/window3.tcl b/test/window3.tcl new file mode 100644 index 0000000000..f2c596c6fe --- /dev/null +++ b/test/window3.tcl @@ -0,0 +1,363 @@ +# 2018 May 19 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +source [file join [file dirname $argv0] pg_common.tcl] + +#========================================================================= + +start_test window3 "2018 May 31" +ifcapable !windowfunc + +execsql_test 1.0 { + DROP TABLE IF EXISTS t2; + CREATE TABLE t2(a INTEGER PRIMARY KEY, b INTEGER); + INSERT INTO t2(a, b) VALUES + (10,89), (11,81), (12,96), (13,59), (14,38), (15,68), (16,39), (17,62), + (18,91), (19,46), (20,6), (21,99), (22,97), (23,27), (24,46), (25,78), + (26,54), (27,97), (28,8), (29,67), (30,29), (31,93), (32,84), (33,77), + (34,23), (35,16), (36,16), (37,93), (38,65), (39,35), (40,47), (41,7), + (42,86), (43,74), (44,61), (45,91), (46,85), (47,24), (48,85), (49,43), + (50,59), (51,12), (52,32), (53,56), (54,3), (55,91), (56,22), (57,90), + (58,55), (59,15), (60,28), (61,89), (62,25), (63,47), (64,1), (65,56), + (66,40), (67,43), (68,56), (69,16), (70,75), (71,36), (72,89), (73,98), + (74,76), (75,81), (76,4), (77,94), (78,42), (79,30), (80,78), (81,33), + (82,29), (83,53), (84,63), (85,2), (86,87), (87,37), (88,80), (89,84), + (90,72), (91,41), (92,9), (93,61), (94,73), (95,95), (96,65), (97,13), + (98,58), (99,96), (100,98), (101,1), (102,21), (103,74), (104,65), (105,35), + (106,5), (107,73), (108,11), (109,51), (110,87), (111,41), (112,12), (113,8), + (114,20), (115,31), (116,31), (117,15), (118,95), (119,22), (120,73), + (121,79), (122,88), (123,34), (124,8), (125,11), (126,49), (127,34), + (128,90), (129,59), (130,96), (131,60), (132,55), (133,75), (134,77), + (135,44), (136,2), (137,7), (138,85), (139,57), (140,74), (141,29), (142,70), + (143,59), (144,19), (145,39), (146,26), (147,26), (148,47), (149,80), + (150,90), (151,36), (152,58), (153,47), (154,9), (155,72), (156,72), (157,66), + (158,33), (159,93), (160,75), (161,64), (162,81), (163,9), (164,23), (165,37), + (166,13), (167,12), (168,14), (169,62), (170,91), (171,36), (172,91), + (173,33), (174,15), (175,34), (176,36), (177,99), (178,3), (179,95), (180,69), + (181,58), (182,52), (183,30), (184,50), (185,84), (186,10), (187,84), + (188,33), (189,21), (190,39), (191,44), (192,58), (193,30), (194,38), + (195,34), (196,83), (197,27), (198,82), (199,17), (200,7); +} + +execsql_test 1.1 { + SELECT max(b) OVER ( + ORDER BY a + ) FROM t2 +} + +foreach {tn window} { + 1 "RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW" + 2 "RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING" + 3 "RANGE BETWEEN CURRENT ROW AND CURRENT ROW" + 4 "RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING" + 5 "ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING" + 6 "ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING" + 7 "ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW" + 8 "ROWS BETWEEN 4 PRECEDING AND CURRENT ROW" + 9 "ROWS BETWEEN CURRENT ROW AND CURRENT ROW" + 10 "ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING" + 11 "ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING" + 12 "ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING" + 13 "ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING" + 14 "ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING" + 15 "ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING" + 16 "ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING" + 17 "ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING" + + 18 "ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW" + 19 "ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES" + 20 "ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP" + +} { + execsql_test 1.$tn.2.1 "SELECT max(b) OVER ( ORDER BY a $window ) FROM t2" + execsql_test 1.$tn.2.2 "SELECT min(b) OVER ( ORDER BY a $window ) FROM t2" + + execsql_test 1.$tn.3.1 " + SELECT row_number() OVER ( ORDER BY a $window ) FROM t2 + " + execsql_test 1.$tn.3.2 " + SELECT row_number() OVER ( PARTITION BY b%10 ORDER BY a $window ) FROM t2 + " + execsql_test 1.$tn.3.3 " + SELECT row_number() OVER ( $window ) FROM t2 + " + + execsql_test 1.$tn.4.1 " + SELECT dense_rank() OVER ( ORDER BY a $window ) FROM t2 + " + execsql_test 1.$tn.4.2 " + SELECT dense_rank() OVER ( PARTITION BY b%10 ORDER BY a $window ) FROM t2 + " + execsql_test 1.$tn.4.3 " + SELECT dense_rank() OVER ( ORDER BY b $window ) FROM t2 + " + execsql_test 1.$tn.4.4 " + SELECT dense_rank() OVER ( PARTITION BY b%10 ORDER BY b $window ) FROM t2 + " + execsql_test 1.$tn.4.5 " + SELECT dense_rank() OVER ( ORDER BY b%10 $window ) FROM t2 + " + execsql_test 1.$tn.4.6 " + SELECT dense_rank() OVER ( PARTITION BY b%2 ORDER BY b%10 $window ) FROM t2 + " + + execsql_test 1.$tn.5.1 " + SELECT rank() OVER ( ORDER BY a $window ) FROM t2 + " + execsql_test 1.$tn.5.2 " + SELECT rank() OVER ( PARTITION BY b%10 ORDER BY a $window ) FROM t2 + " + execsql_test 1.$tn.5.3 " + SELECT rank() OVER ( ORDER BY b $window ) FROM t2 + " + execsql_test 1.$tn.5.4 " + SELECT rank() OVER ( PARTITION BY b%10 ORDER BY b $window ) FROM t2 + " + execsql_test 1.$tn.5.5 " + SELECT rank() OVER ( ORDER BY b%10 $window ) FROM t2 + " + execsql_test 1.$tn.5.6 " + SELECT rank() OVER ( PARTITION BY b%2 ORDER BY b%10 $window ) FROM t2 + " + + execsql_test 1.$tn.6.1 " + SELECT + row_number() OVER ( PARTITION BY b%2 ORDER BY b%10 $window ), + rank() OVER ( PARTITION BY b%2 ORDER BY b%10 $window ), + dense_rank() OVER ( PARTITION BY b%2 ORDER BY b%10 $window ) + FROM t2 + " + + execsql_float_test 1.$tn.7.1 " + SELECT percent_rank() OVER ( ORDER BY a $window ) FROM t2 + " + execsql_float_test 1.$tn.7.2 " + SELECT percent_rank() OVER ( PARTITION BY b%10 ORDER BY a $window ) FROM t2 + " + execsql_float_test 1.$tn.7.3 " + SELECT percent_rank() OVER ( ORDER BY b $window ) FROM t2 + " + execsql_float_test 1.$tn.7.4 " + SELECT percent_rank() OVER ( PARTITION BY b%10 ORDER BY b $window ) FROM t2 + " + execsql_float_test 1.$tn.7.5 " + SELECT percent_rank() OVER ( ORDER BY b%10 $window ) FROM t2 + " + execsql_float_test 1.$tn.7.6 " + SELECT percent_rank() OVER (PARTITION BY b%2 ORDER BY b%10 $window) FROM t2 + " + + execsql_float_test 1.$tn.8.1 " + SELECT cume_dist() OVER ( ORDER BY a $window ) FROM t2 + " + execsql_float_test 1.$tn.8.2 " + SELECT cume_dist() OVER ( PARTITION BY b%10 ORDER BY a $window ) FROM t2 + " + execsql_float_test 1.$tn.8.3 " + SELECT cume_dist() OVER ( ORDER BY b $window ) FROM t2 + " + execsql_float_test 1.$tn.8.4 " + SELECT cume_dist() OVER ( PARTITION BY b%10 ORDER BY b $window ) FROM t2 + " + execsql_float_test 1.$tn.8.5 " + SELECT cume_dist() OVER ( ORDER BY b%10 $window ) FROM t2 + " + execsql_float_test 1.$tn.8.6 " + SELECT cume_dist() OVER ( PARTITION BY b%2 ORDER BY b%10 $window ) FROM t2 + " + + execsql_float_test 1.$tn.8.1 " + SELECT ntile(100) OVER ( ORDER BY a $window ) FROM t2 + " + execsql_float_test 1.$tn.8.2 " + SELECT ntile(101) OVER ( PARTITION BY b%10 ORDER BY a $window ) FROM t2 + " + execsql_float_test 1.$tn.8.3 " + SELECT ntile(102) OVER ( ORDER BY b,a $window ) FROM t2 + " + execsql_float_test 1.$tn.8.4 " + SELECT ntile(103) OVER ( PARTITION BY b%10 ORDER BY b,a $window ) FROM t2 + " + execsql_float_test 1.$tn.8.5 " + SELECT ntile(104) OVER ( ORDER BY b%10,a $window ) FROM t2 + " + execsql_float_test 1.$tn.8.6 " + SELECT ntile(105) OVER (PARTITION BY b%2,a ORDER BY b%10 $window) FROM t2 + " + execsql_float_test 1.$tn.8.7 " + SELECT ntile(105) OVER ( $window ) FROM t2 + " + + execsql_test 1.$tn.9.1 " + SELECT last_value(a+b) OVER ( ORDER BY a $window ) FROM t2 + " + execsql_test 1.$tn.9.2 " + SELECT last_value(a+b) OVER ( PARTITION BY b%10 ORDER BY a $window ) FROM t2 + " + execsql_test 1.$tn.9.3 " + SELECT last_value(a+b) OVER ( ORDER BY b,a $window ) FROM t2 + " + execsql_test 1.$tn.9.4 " + SELECT last_value(a+b) OVER ( PARTITION BY b%10 ORDER BY b,a $window ) FROM t2 + " + execsql_test 1.$tn.9.5 " + SELECT last_value(a+b) OVER ( ORDER BY b%10,a $window ) FROM t2 + " + execsql_test 1.$tn.9.6 " + SELECT last_value(a+b) OVER (PARTITION BY b%2,a ORDER BY b%10 $window) FROM t2 + " + + execsql_test 1.$tn.10.1 " + SELECT nth_value(b,b+1) OVER (ORDER BY a $window) FROM t2 + " + execsql_test 1.$tn.10.2 " + SELECT nth_value(b,b+1) OVER (PARTITION BY b%10 ORDER BY a $window) FROM t2 + " + execsql_test 1.$tn.10.3 " + SELECT nth_value(b,b+1) OVER ( ORDER BY b,a $window ) FROM t2 + " + execsql_test 1.$tn.10.4 " + SELECT nth_value(b,b+1) OVER ( PARTITION BY b%10 ORDER BY b,a $window ) FROM t2 + " + execsql_test 1.$tn.10.5 " + SELECT nth_value(b,b+1) OVER ( ORDER BY b%10,a $window ) FROM t2 + " + execsql_test 1.$tn.10.6 " + SELECT nth_value(b,b+1) OVER (PARTITION BY b%2,a ORDER BY b%10 $window) FROM t2 + " + + execsql_test 1.$tn.11.1 " + SELECT first_value(b) OVER (ORDER BY a $window) FROM t2 + " + execsql_test 1.$tn.11.2 " + SELECT first_value(b) OVER (PARTITION BY b%10 ORDER BY a $window) FROM t2 + " + execsql_test 1.$tn.11.3 " + SELECT first_value(b) OVER ( ORDER BY b,a $window ) FROM t2 + " + execsql_test 1.$tn.11.4 " + SELECT first_value(b) OVER ( PARTITION BY b%10 ORDER BY b,a $window ) FROM t2 + " + execsql_test 1.$tn.11.5 " + SELECT first_value(b) OVER ( ORDER BY b%10,a $window ) FROM t2 + " + execsql_test 1.$tn.11.6 " + SELECT first_value(b) OVER (PARTITION BY b%2,a ORDER BY b%10 $window) FROM t2 + " + + execsql_test 1.$tn.12.1 " + SELECT lead(b,b) OVER (ORDER BY a $window) FROM t2 + " + execsql_test 1.$tn.12.2 " + SELECT lead(b,b) OVER (PARTITION BY b%10 ORDER BY a $window) FROM t2 + " + execsql_test 1.$tn.12.3 " + SELECT lead(b,b) OVER ( ORDER BY b,a $window ) FROM t2 + " + execsql_test 1.$tn.12.4 " + SELECT lead(b,b) OVER ( PARTITION BY b%10 ORDER BY b,a $window ) FROM t2 + " + execsql_test 1.$tn.12.5 " + SELECT lead(b,b) OVER ( ORDER BY b%10,a $window ) FROM t2 + " + execsql_test 1.$tn.12.6 " + SELECT lead(b,b) OVER (PARTITION BY b%2,a ORDER BY b%10 $window) FROM t2 + " + + execsql_test 1.$tn.13.1 " + SELECT lag(b,b) OVER (ORDER BY a $window) FROM t2 + " + execsql_test 1.$tn.13.2 " + SELECT lag(b,b) OVER (PARTITION BY b%10 ORDER BY a $window) FROM t2 + " + execsql_test 1.$tn.13.3 " + SELECT lag(b,b) OVER ( ORDER BY b,a $window ) FROM t2 + " + execsql_test 1.$tn.13.4 " + SELECT lag(b,b) OVER ( PARTITION BY b%10 ORDER BY b,a $window ) FROM t2 + " + execsql_test 1.$tn.13.5 " + SELECT lag(b,b) OVER ( ORDER BY b%10,a $window ) FROM t2 + " + execsql_test 1.$tn.13.6 " + SELECT lag(b,b) OVER (PARTITION BY b%2,a ORDER BY b%10 $window) FROM t2 + " + + execsql_test 1.$tn.14.1 " + SELECT string_agg(CAST(b AS TEXT), '.') OVER (ORDER BY a $window) FROM t2 + " + execsql_test 1.$tn.14.2 " + SELECT string_agg(CAST(b AS TEXT), '.') OVER (PARTITION BY b%10 ORDER BY a $window) FROM t2 + " + execsql_test 1.$tn.14.3 " + SELECT string_agg(CAST(b AS TEXT), '.') OVER ( ORDER BY b,a $window ) FROM t2 + " + execsql_test 1.$tn.14.4 " + SELECT string_agg(CAST(b AS TEXT), '.') OVER ( PARTITION BY b%10 ORDER BY b,a $window ) FROM t2 + " + execsql_test 1.$tn.14.5 " + SELECT string_agg(CAST(b AS TEXT), '.') OVER ( ORDER BY b%10,a $window ) FROM t2 + " + execsql_test 1.$tn.14.6 " + SELECT string_agg(CAST(b AS TEXT), '.') OVER (PARTITION BY b%2,a ORDER BY b%10 $window) FROM t2 + " + + execsql_test 1.$tn.14.7 " + SELECT string_agg(CAST(b AS TEXT), '.') OVER (win1 ORDER BY b%10 $window) + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a) + ORDER BY 1 + " + + execsql_test 1.$tn.14.8 " + SELECT string_agg(CAST(b AS TEXT), '.') OVER (win1 $window) + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a ORDER BY b%10) + ORDER BY 1 + " + + execsql_test 1.$tn.14.9 " + SELECT string_agg(CAST(b AS TEXT), '.') OVER win2 + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a ORDER BY b%10), + win2 AS (win1 $window) + ORDER BY 1 + " + + execsql_test 1.$tn.15.1 " + SELECT count(*) OVER win, string_agg(CAST(b AS TEXT), '.') + FILTER (WHERE a%2=0) OVER win FROM t2 + WINDOW win AS (ORDER BY a $window) + " + + execsql_test 1.$tn.15.2 " + SELECT count(*) OVER win, string_agg(CAST(b AS TEXT), '.') + FILTER (WHERE 0=1) OVER win FROM t2 + WINDOW win AS (ORDER BY a $window) + " + + execsql_test 1.$tn.15.3 " + SELECT count(*) OVER win, string_agg(CAST(b AS TEXT), '.') + FILTER (WHERE 1=0) OVER win FROM t2 + WINDOW win AS (PARTITION BY (a%10) ORDER BY a $window) + " + + execsql_test 1.$tn.15.4 " + SELECT count(*) OVER win, string_agg(CAST(b AS TEXT), '.') + FILTER (WHERE a%2=0) OVER win FROM t2 + WINDOW win AS (PARTITION BY (a%10) ORDER BY a $window) + " + +} + +finish_test + diff --git a/test/window3.test b/test/window3.test new file mode 100644 index 0000000000..1893b539d3 --- /dev/null +++ b/test/window3.test @@ -0,0 +1,40613 @@ +# 2018 May 31 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. +# + +#################################################### +# DO NOT EDIT! THIS FILE IS AUTOMATICALLY GENERATED! +#################################################### + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix window3 + +ifcapable !windowfunc { finish_test ; return } +do_execsql_test 1.0 { + DROP TABLE IF EXISTS t2; + CREATE TABLE t2(a INTEGER PRIMARY KEY, b INTEGER); + INSERT INTO t2(a, b) VALUES + (10,89), (11,81), (12,96), (13,59), (14,38), (15,68), (16,39), (17,62), + (18,91), (19,46), (20,6), (21,99), (22,97), (23,27), (24,46), (25,78), + (26,54), (27,97), (28,8), (29,67), (30,29), (31,93), (32,84), (33,77), + (34,23), (35,16), (36,16), (37,93), (38,65), (39,35), (40,47), (41,7), + (42,86), (43,74), (44,61), (45,91), (46,85), (47,24), (48,85), (49,43), + (50,59), (51,12), (52,32), (53,56), (54,3), (55,91), (56,22), (57,90), + (58,55), (59,15), (60,28), (61,89), (62,25), (63,47), (64,1), (65,56), + (66,40), (67,43), (68,56), (69,16), (70,75), (71,36), (72,89), (73,98), + (74,76), (75,81), (76,4), (77,94), (78,42), (79,30), (80,78), (81,33), + (82,29), (83,53), (84,63), (85,2), (86,87), (87,37), (88,80), (89,84), + (90,72), (91,41), (92,9), (93,61), (94,73), (95,95), (96,65), (97,13), + (98,58), (99,96), (100,98), (101,1), (102,21), (103,74), (104,65), (105,35), + (106,5), (107,73), (108,11), (109,51), (110,87), (111,41), (112,12), (113,8), + (114,20), (115,31), (116,31), (117,15), (118,95), (119,22), (120,73), + (121,79), (122,88), (123,34), (124,8), (125,11), (126,49), (127,34), + (128,90), (129,59), (130,96), (131,60), (132,55), (133,75), (134,77), + (135,44), (136,2), (137,7), (138,85), (139,57), (140,74), (141,29), (142,70), + (143,59), (144,19), (145,39), (146,26), (147,26), (148,47), (149,80), + (150,90), (151,36), (152,58), (153,47), (154,9), (155,72), (156,72), (157,66), + (158,33), (159,93), (160,75), (161,64), (162,81), (163,9), (164,23), (165,37), + (166,13), (167,12), (168,14), (169,62), (170,91), (171,36), (172,91), + (173,33), (174,15), (175,34), (176,36), (177,99), (178,3), (179,95), (180,69), + (181,58), (182,52), (183,30), (184,50), (185,84), (186,10), (187,84), + (188,33), (189,21), (190,39), (191,44), (192,58), (193,30), (194,38), + (195,34), (196,83), (197,27), (198,82), (199,17), (200,7); +} {} + +do_execsql_test 1.1 { + SELECT max(b) OVER ( + ORDER BY a + ) FROM t2 +} {89 89 96 96 96 96 96 96 96 96 96 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99} + +do_execsql_test 1.1.2.1 { + SELECT max(b) OVER ( ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {89 89 96 96 96 96 96 96 96 96 96 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99} + +do_execsql_test 1.1.2.2 { + SELECT min(b) OVER ( ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {89 81 81 59 38 38 38 38 38 38 6 6 6 6 6 6 6 + 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 + 6 6 6 6 6 6 6 6 3 3 3 3 3 3 3 3 3 3 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1} + +do_execsql_test 1.1.3.1 { + SELECT row_number() OVER ( ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.1.3.2 { + SELECT row_number() OVER ( PARTITION BY b%10 ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.1.3.3 { + SELECT row_number() OVER ( RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.1.4.1 { + SELECT dense_rank() OVER ( ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.1.4.2 { + SELECT dense_rank() OVER ( PARTITION BY b%10 ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.1.4.3 { + SELECT dense_rank() OVER ( ORDER BY b RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {1 1 2 2 3 3 4 5 6 7 7 7 8 8 8 9 9 9 10 + 11 11 12 12 12 13 13 14 15 15 15 16 16 16 17 + 18 19 20 20 21 21 22 22 23 24 25 25 26 26 27 + 28 28 28 29 29 29 30 30 31 32 32 32 32 33 33 + 33 33 34 34 35 35 35 35 36 36 37 37 38 38 38 + 39 40 40 41 42 42 43 43 44 44 45 45 45 45 46 + 47 48 49 50 51 52 52 53 53 53 54 55 55 55 55 + 56 56 56 56 57 58 58 59 59 60 61 62 62 62 63 + 64 65 66 67 68 68 68 69 69 69 70 70 70 71 71 + 71 72 73 73 74 74 75 76 76 77 77 77 78 79 80 + 80 80 80 81 81 81 82 83 83 84 85 85 85 86 86 + 86 87 87 87 87 87 88 88 88 89 90 90 90 91 91 + 91 92 92 93 93 94 94} + +do_execsql_test 1.1.4.4 { + SELECT dense_rank() OVER ( PARTITION BY b%10 ORDER BY b RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {1 2 3 3 3 4 5 6 7 8 8 9 9 9 1 1 2 2 3 + 3 4 4 5 5 6 7 7 8 8 8 9 9 9 9 9 1 1 2 + 2 2 3 3 4 5 6 7 7 8 8 8 9 1 1 2 2 3 3 + 4 4 4 4 5 5 6 7 8 8 8 9 10 10 10 1 2 3 + 4 4 4 4 5 5 6 7 8 8 8 9 9 9 9 10 1 2 2 + 2 3 4 4 5 5 6 6 6 7 7 7 8 8 8 9 9 9 1 + 2 2 2 3 3 4 4 4 4 5 5 6 6 6 7 8 9 10 10 + 10 1 1 1 2 3 3 4 4 5 5 5 5 6 7 8 8 9 9 + 10 10 1 1 1 2 3 3 4 4 4 4 5 6 6 7 8 8 1 + 1 1 2 3 3 3 4 4 4 5 6 6 6 6 7 8 9 9 9 + 10 10} + +do_execsql_test 1.1.4.5 { + SELECT dense_rank() OVER ( ORDER BY b%10 RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 + 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 + 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 + 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 + 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 + 8 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 10 10 + 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 + 10 10 10 10 10} + +do_execsql_test 1.1.4.6 { + SELECT dense_rank() OVER ( PARTITION BY b%2 ORDER BY b%10 RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 + 5 5 5 5 5 5 5 5 5 5 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 + 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 + 5} + +do_execsql_test 1.1.5.1 { + SELECT rank() OVER ( ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.1.5.2 { + SELECT rank() OVER ( PARTITION BY b%10 ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.1.5.3 { + SELECT rank() OVER ( ORDER BY b RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {1 1 3 3 5 5 7 8 9 10 10 10 13 13 13 16 16 + 16 19 20 20 22 22 22 25 25 27 28 28 28 31 31 + 31 34 35 36 37 37 39 39 41 41 43 44 45 45 47 + 47 49 50 50 50 53 53 53 56 56 58 59 59 59 59 + 63 63 63 63 67 67 69 69 69 69 73 73 75 75 77 + 77 77 80 81 81 83 84 84 86 86 88 88 90 90 90 + 90 94 95 96 97 98 99 100 100 102 102 102 105 106 + 106 106 106 110 110 110 110 114 115 115 117 117 119 + 120 121 121 121 124 125 126 127 128 129 129 129 132 + 132 132 135 135 135 138 138 138 141 142 142 144 144 + 146 147 147 149 149 149 152 153 154 154 154 154 158 + 158 158 161 162 162 164 165 165 165 168 168 168 171 + 171 171 171 171 176 176 176 179 180 180 180 183 183 + 183 186 186 188 188 190 190} + +do_execsql_test 1.1.5.4 { + SELECT rank() OVER ( PARTITION BY b%10 ORDER BY b RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {1 2 3 3 3 6 7 8 9 10 10 12 12 12 1 1 3 3 + 5 5 7 7 9 9 11 12 12 14 14 14 17 17 17 17 + 17 1 1 3 3 3 6 6 8 9 10 11 11 13 13 13 16 + 1 1 3 3 5 5 7 7 7 7 11 11 13 14 15 15 15 + 18 19 19 19 1 2 3 4 4 4 4 8 8 10 11 12 12 + 12 15 15 15 15 19 1 2 2 2 5 6 6 8 8 10 10 + 10 13 13 13 16 16 16 19 19 19 1 2 2 2 5 5 7 + 7 7 7 11 11 13 13 13 16 17 18 19 19 19 1 1 + 1 4 5 5 7 7 9 9 9 9 13 14 15 15 17 17 19 + 19 1 1 1 4 5 5 7 7 7 7 11 12 12 14 15 15 + 1 1 1 4 5 5 5 8 8 8 11 12 12 12 12 16 17 + 18 18 18 21 21} + +do_execsql_test 1.1.5.5 { + SELECT rank() OVER ( ORDER BY b%10 RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 15 15 15 15 + 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 + 15 15 36 36 36 36 36 36 36 36 36 36 36 36 36 + 36 36 36 52 52 52 52 52 52 52 52 52 52 52 52 + 52 52 52 52 52 52 52 52 52 73 73 73 73 73 73 + 73 73 73 73 73 73 73 73 73 73 73 73 73 92 92 + 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 + 92 92 92 92 113 113 113 113 113 113 113 113 113 + 113 113 113 113 113 113 113 113 113 113 113 113 134 + 134 134 134 134 134 134 134 134 134 134 134 134 134 + 134 134 134 134 134 134 154 154 154 154 154 154 154 + 154 154 154 154 154 154 154 154 154 170 170 170 170 + 170 170 170 170 170 170 170 170 170 170 170 170 170 + 170 170 170 170 170} + +do_execsql_test 1.1.5.6 { + SELECT rank() OVER ( PARTITION BY b%2 ORDER BY b%10 RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 15 15 15 15 + 15 15 15 15 15 15 15 15 15 15 15 15 31 31 31 + 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 + 31 50 50 50 50 50 50 50 50 50 50 50 50 50 50 + 50 50 50 50 50 50 50 71 71 71 71 71 71 71 71 + 71 71 71 71 71 71 71 71 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 22 22 22 22 22 22 + 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 + 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 + 43 43 43 43 43 43 64 64 64 64 64 64 64 64 64 + 64 64 64 64 64 64 64 64 64 64 64 84 84 84 84 + 84 84 84 84 84 84 84 84 84 84 84 84 84 84 84 + 84 84 84} + +do_execsql_test 1.1.6.1 { + SELECT + row_number() OVER ( PARTITION BY b%2 ORDER BY b%10 RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ), + rank() OVER ( PARTITION BY b%2 ORDER BY b%10 RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ), + dense_rank() OVER ( PARTITION BY b%2 ORDER BY b%10 RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) + FROM t2 +} {1 1 1 2 1 1 3 1 1 4 1 1 5 1 1 6 1 1 7 1 1 8 1 1 9 1 1 + 10 1 1 11 1 1 12 1 1 13 1 1 14 1 1 15 15 2 16 15 2 17 15 2 + 18 15 2 19 15 2 20 15 2 21 15 2 22 15 2 23 15 2 24 15 2 + 25 15 2 26 15 2 27 15 2 28 15 2 29 15 2 30 15 2 31 31 3 + 32 31 3 33 31 3 34 31 3 35 31 3 36 31 3 37 31 3 38 31 3 + 39 31 3 40 31 3 41 31 3 42 31 3 43 31 3 44 31 3 45 31 3 + 46 31 3 47 31 3 48 31 3 49 31 3 50 50 4 51 50 4 52 50 4 + 53 50 4 54 50 4 55 50 4 56 50 4 57 50 4 58 50 4 59 50 4 + 60 50 4 61 50 4 62 50 4 63 50 4 64 50 4 65 50 4 66 50 4 + 67 50 4 68 50 4 69 50 4 70 50 4 71 71 5 72 71 5 73 71 5 + 74 71 5 75 71 5 76 71 5 77 71 5 78 71 5 79 71 5 80 71 5 + 81 71 5 82 71 5 83 71 5 84 71 5 85 71 5 86 71 5 1 1 1 2 1 1 + 3 1 1 4 1 1 5 1 1 6 1 1 7 1 1 8 1 1 9 1 1 10 1 1 11 1 1 + 12 1 1 13 1 1 14 1 1 15 1 1 16 1 1 17 1 1 18 1 1 19 1 1 + 20 1 1 21 1 1 22 22 2 23 22 2 24 22 2 25 22 2 26 22 2 27 22 2 + 28 22 2 29 22 2 30 22 2 31 22 2 32 22 2 33 22 2 34 22 2 + 35 22 2 36 22 2 37 22 2 38 22 2 39 22 2 40 22 2 41 22 2 + 42 22 2 43 43 3 44 43 3 45 43 3 46 43 3 47 43 3 48 43 3 + 49 43 3 50 43 3 51 43 3 52 43 3 53 43 3 54 43 3 55 43 3 + 56 43 3 57 43 3 58 43 3 59 43 3 60 43 3 61 43 3 62 43 3 + 63 43 3 64 64 4 65 64 4 66 64 4 67 64 4 68 64 4 69 64 4 + 70 64 4 71 64 4 72 64 4 73 64 4 74 64 4 75 64 4 76 64 4 + 77 64 4 78 64 4 79 64 4 80 64 4 81 64 4 82 64 4 83 64 4 + 84 84 5 85 84 5 86 84 5 87 84 5 88 84 5 89 84 5 90 84 5 + 91 84 5 92 84 5 93 84 5 94 84 5 95 84 5 96 84 5 97 84 5 + 98 84 5 99 84 5 100 84 5 101 84 5 102 84 5 103 84 5 104 84 5 + 105 84 5} + + +do_test 1.1.7.1 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0053 0.0105 0.0158 0.0211 0.0263 0.0316 0.0368 0.0421 0.0474 0.0526 0.0579 0.0632 0.0684 0.0737 0.0789 0.0842 0.0895 0.0947 0.1000 0.1053 0.1105 0.1158 0.1211 0.1263 0.1316 0.1368 0.1421 0.1474 0.1526 0.1579 0.1632 0.1684 0.1737 0.1789 0.1842 0.1895 0.1947 0.2000 0.2053 0.2105 0.2158 0.2211 0.2263 0.2316 0.2368 0.2421 0.2474 0.2526 0.2579 0.2632 0.2684 0.2737 0.2789 0.2842 0.2895 0.2947 0.3000 0.3053 0.3105 0.3158 0.3211 0.3263 0.3316 0.3368 0.3421 0.3474 0.3526 0.3579 0.3632 0.3684 0.3737 0.3789 0.3842 0.3895 0.3947 0.4000 0.4053 0.4105 0.4158 0.4211 0.4263 0.4316 0.4368 0.4421 0.4474 0.4526 0.4579 0.4632 0.4684 0.4737 0.4789 0.4842 0.4895 0.4947 0.5000 0.5053 0.5105 0.5158 0.5211 0.5263 0.5316 0.5368 0.5421 0.5474 0.5526 0.5579 0.5632 0.5684 0.5737 0.5789 0.5842 0.5895 0.5947 0.6000 0.6053 0.6105 0.6158 0.6211 0.6263 0.6316 0.6368 0.6421 0.6474 0.6526 0.6579 0.6632 0.6684 0.6737 0.6789 0.6842 0.6895 0.6947 0.7000 0.7053 0.7105 0.7158 0.7211 0.7263 0.7316 0.7368 0.7421 0.7474 0.7526 0.7579 0.7632 0.7684 0.7737 0.7789 0.7842 0.7895 0.7947 0.8000 0.8053 0.8105 0.8158 0.8211 0.8263 0.8316 0.8368 0.8421 0.8474 0.8526 0.8579 0.8632 0.8684 0.8737 0.8789 0.8842 0.8895 0.8947 0.9000 0.9053 0.9105 0.9158 0.9211 0.9263 0.9316 0.9368 0.9421 0.9474 0.9526 0.9579 0.9632 0.9684 0.9737 0.9789 0.9842 0.9895 0.9947 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.1.7.2 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( PARTITION BY b%10 ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0769 0.1538 0.2308 0.3077 0.3846 0.4615 0.5385 0.6154 0.6923 0.7692 0.8462 0.9231 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0667 0.1333 0.2000 0.2667 0.3333 0.4000 0.4667 0.5333 0.6000 0.6667 0.7333 0.8000 0.8667 0.9333 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0556 0.1111 0.1667 0.2222 0.2778 0.3333 0.3889 0.4444 0.5000 0.5556 0.6111 0.6667 0.7222 0.7778 0.8333 0.8889 0.9444 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0526 0.1053 0.1579 0.2105 0.2632 0.3158 0.3684 0.4211 0.4737 0.5263 0.5789 0.6316 0.6842 0.7368 0.7895 0.8421 0.8947 0.9474 1.0000 0.0000 0.0667 0.1333 0.2000 0.2667 0.3333 0.4000 0.4667 0.5333 0.6000 0.6667 0.7333 0.8000 0.8667 0.9333 1.0000 0.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.1.7.3 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY b RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0105 0.0105 0.0211 0.0211 0.0316 0.0368 0.0421 0.0474 0.0474 0.0474 0.0632 0.0632 0.0632 0.0789 0.0789 0.0789 0.0947 0.1000 0.1000 0.1105 0.1105 0.1105 0.1263 0.1263 0.1368 0.1421 0.1421 0.1421 0.1579 0.1579 0.1579 0.1737 0.1789 0.1842 0.1895 0.1895 0.2000 0.2000 0.2105 0.2105 0.2211 0.2263 0.2316 0.2316 0.2421 0.2421 0.2526 0.2579 0.2579 0.2579 0.2737 0.2737 0.2737 0.2895 0.2895 0.3000 0.3053 0.3053 0.3053 0.3053 0.3263 0.3263 0.3263 0.3263 0.3474 0.3474 0.3579 0.3579 0.3579 0.3579 0.3789 0.3789 0.3895 0.3895 0.4000 0.4000 0.4000 0.4158 0.4211 0.4211 0.4316 0.4368 0.4368 0.4474 0.4474 0.4579 0.4579 0.4684 0.4684 0.4684 0.4684 0.4895 0.4947 0.5000 0.5053 0.5105 0.5158 0.5211 0.5211 0.5316 0.5316 0.5316 0.5474 0.5526 0.5526 0.5526 0.5526 0.5737 0.5737 0.5737 0.5737 0.5947 0.6000 0.6000 0.6105 0.6105 0.6211 0.6263 0.6316 0.6316 0.6316 0.6474 0.6526 0.6579 0.6632 0.6684 0.6737 0.6737 0.6737 0.6895 0.6895 0.6895 0.7053 0.7053 0.7053 0.7211 0.7211 0.7211 0.7368 0.7421 0.7421 0.7526 0.7526 0.7632 0.7684 0.7684 0.7789 0.7789 0.7789 0.7947 0.8000 0.8053 0.8053 0.8053 0.8053 0.8263 0.8263 0.8263 0.8421 0.8474 0.8474 0.8579 0.8632 0.8632 0.8632 0.8789 0.8789 0.8789 0.8947 0.8947 0.8947 0.8947 0.8947 0.9211 0.9211 0.9211 0.9368 0.9421 0.9421 0.9421 0.9579 0.9579 0.9579 0.9737 0.9737 0.9842 0.9842 0.9947 0.9947} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.1.7.4 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( PARTITION BY b%10 ORDER BY b RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0769 0.1538 0.1538 0.1538 0.3846 0.4615 0.5385 0.6154 0.6923 0.6923 0.8462 0.8462 0.8462 0.0000 0.0000 0.1000 0.1000 0.2000 0.2000 0.3000 0.3000 0.4000 0.4000 0.5000 0.5500 0.5500 0.6500 0.6500 0.6500 0.8000 0.8000 0.8000 0.8000 0.8000 0.0000 0.0000 0.1333 0.1333 0.1333 0.3333 0.3333 0.4667 0.5333 0.6000 0.6667 0.6667 0.8000 0.8000 0.8000 1.0000 0.0000 0.0000 0.1000 0.1000 0.2000 0.2000 0.3000 0.3000 0.3000 0.3000 0.5000 0.5000 0.6000 0.6500 0.7000 0.7000 0.7000 0.8500 0.9000 0.9000 0.9000 0.0000 0.0556 0.1111 0.1667 0.1667 0.1667 0.1667 0.3889 0.3889 0.5000 0.5556 0.6111 0.6111 0.6111 0.7778 0.7778 0.7778 0.7778 1.0000 0.0000 0.0500 0.0500 0.0500 0.2000 0.2500 0.2500 0.3500 0.3500 0.4500 0.4500 0.4500 0.6000 0.6000 0.6000 0.7500 0.7500 0.7500 0.9000 0.9000 0.9000 0.0000 0.0500 0.0500 0.0500 0.2000 0.2000 0.3000 0.3000 0.3000 0.3000 0.5000 0.5000 0.6000 0.6000 0.6000 0.7500 0.8000 0.8500 0.9000 0.9000 0.9000 0.0000 0.0000 0.0000 0.1579 0.2105 0.2105 0.3158 0.3158 0.4211 0.4211 0.4211 0.4211 0.6316 0.6842 0.7368 0.7368 0.8421 0.8421 0.9474 0.9474 0.0000 0.0000 0.0000 0.2000 0.2667 0.2667 0.4000 0.4000 0.4000 0.4000 0.6667 0.7333 0.7333 0.8667 0.9333 0.9333 0.0000 0.0000 0.0000 0.1429 0.1905 0.1905 0.1905 0.3333 0.3333 0.3333 0.4762 0.5238 0.5238 0.5238 0.5238 0.7143 0.7619 0.8095 0.8095 0.8095 0.9524 0.9524} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.1.7.5 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY b%10 RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.1.7.6 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER (PARTITION BY b%2 ORDER BY b%10 RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.1.8.1 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0052 0.0105 0.0157 0.0209 0.0262 0.0314 0.0366 0.0419 0.0471 0.0524 0.0576 0.0628 0.0681 0.0733 0.0785 0.0838 0.0890 0.0942 0.0995 0.1047 0.1099 0.1152 0.1204 0.1257 0.1309 0.1361 0.1414 0.1466 0.1518 0.1571 0.1623 0.1675 0.1728 0.1780 0.1832 0.1885 0.1937 0.1990 0.2042 0.2094 0.2147 0.2199 0.2251 0.2304 0.2356 0.2408 0.2461 0.2513 0.2565 0.2618 0.2670 0.2723 0.2775 0.2827 0.2880 0.2932 0.2984 0.3037 0.3089 0.3141 0.3194 0.3246 0.3298 0.3351 0.3403 0.3455 0.3508 0.3560 0.3613 0.3665 0.3717 0.3770 0.3822 0.3874 0.3927 0.3979 0.4031 0.4084 0.4136 0.4188 0.4241 0.4293 0.4346 0.4398 0.4450 0.4503 0.4555 0.4607 0.4660 0.4712 0.4764 0.4817 0.4869 0.4921 0.4974 0.5026 0.5079 0.5131 0.5183 0.5236 0.5288 0.5340 0.5393 0.5445 0.5497 0.5550 0.5602 0.5654 0.5707 0.5759 0.5812 0.5864 0.5916 0.5969 0.6021 0.6073 0.6126 0.6178 0.6230 0.6283 0.6335 0.6387 0.6440 0.6492 0.6545 0.6597 0.6649 0.6702 0.6754 0.6806 0.6859 0.6911 0.6963 0.7016 0.7068 0.7120 0.7173 0.7225 0.7277 0.7330 0.7382 0.7435 0.7487 0.7539 0.7592 0.7644 0.7696 0.7749 0.7801 0.7853 0.7906 0.7958 0.8010 0.8063 0.8115 0.8168 0.8220 0.8272 0.8325 0.8377 0.8429 0.8482 0.8534 0.8586 0.8639 0.8691 0.8743 0.8796 0.8848 0.8901 0.8953 0.9005 0.9058 0.9110 0.9162 0.9215 0.9267 0.9319 0.9372 0.9424 0.9476 0.9529 0.9581 0.9634 0.9686 0.9738 0.9791 0.9843 0.9895 0.9948 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.1.8.2 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%10 ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0714 0.1429 0.2143 0.2857 0.3571 0.4286 0.5000 0.5714 0.6429 0.7143 0.7857 0.8571 0.9286 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0625 0.1250 0.1875 0.2500 0.3125 0.3750 0.4375 0.5000 0.5625 0.6250 0.6875 0.7500 0.8125 0.8750 0.9375 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0526 0.1053 0.1579 0.2105 0.2632 0.3158 0.3684 0.4211 0.4737 0.5263 0.5789 0.6316 0.6842 0.7368 0.7895 0.8421 0.8947 0.9474 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0625 0.1250 0.1875 0.2500 0.3125 0.3750 0.4375 0.5000 0.5625 0.6250 0.6875 0.7500 0.8125 0.8750 0.9375 1.0000 0.0455 0.0909 0.1364 0.1818 0.2273 0.2727 0.3182 0.3636 0.4091 0.4545 0.5000 0.5455 0.5909 0.6364 0.6818 0.7273 0.7727 0.8182 0.8636 0.9091 0.9545 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.1.8.3 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY b RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0105 0.0105 0.0209 0.0209 0.0314 0.0314 0.0366 0.0419 0.0471 0.0628 0.0628 0.0628 0.0785 0.0785 0.0785 0.0942 0.0942 0.0942 0.0995 0.1099 0.1099 0.1257 0.1257 0.1257 0.1361 0.1361 0.1414 0.1571 0.1571 0.1571 0.1728 0.1728 0.1728 0.1780 0.1832 0.1885 0.1990 0.1990 0.2094 0.2094 0.2199 0.2199 0.2251 0.2304 0.2408 0.2408 0.2513 0.2513 0.2565 0.2723 0.2723 0.2723 0.2880 0.2880 0.2880 0.2984 0.2984 0.3037 0.3246 0.3246 0.3246 0.3246 0.3455 0.3455 0.3455 0.3455 0.3560 0.3560 0.3770 0.3770 0.3770 0.3770 0.3874 0.3874 0.3979 0.3979 0.4136 0.4136 0.4136 0.4188 0.4293 0.4293 0.4346 0.4450 0.4450 0.4555 0.4555 0.4660 0.4660 0.4869 0.4869 0.4869 0.4869 0.4921 0.4974 0.5026 0.5079 0.5131 0.5183 0.5288 0.5288 0.5445 0.5445 0.5445 0.5497 0.5707 0.5707 0.5707 0.5707 0.5916 0.5916 0.5916 0.5916 0.5969 0.6073 0.6073 0.6178 0.6178 0.6230 0.6283 0.6440 0.6440 0.6440 0.6492 0.6545 0.6597 0.6649 0.6702 0.6859 0.6859 0.6859 0.7016 0.7016 0.7016 0.7173 0.7173 0.7173 0.7330 0.7330 0.7330 0.7382 0.7487 0.7487 0.7592 0.7592 0.7644 0.7749 0.7749 0.7906 0.7906 0.7906 0.7958 0.8010 0.8220 0.8220 0.8220 0.8220 0.8377 0.8377 0.8377 0.8429 0.8534 0.8534 0.8586 0.8743 0.8743 0.8743 0.8901 0.8901 0.8901 0.9162 0.9162 0.9162 0.9162 0.9162 0.9319 0.9319 0.9319 0.9372 0.9529 0.9529 0.9529 0.9686 0.9686 0.9686 0.9791 0.9791 0.9895 0.9895 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.1.8.4 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%10 ORDER BY b RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0714 0.1429 0.3571 0.3571 0.3571 0.4286 0.5000 0.5714 0.6429 0.7857 0.7857 1.0000 1.0000 1.0000 0.0952 0.0952 0.1905 0.1905 0.2857 0.2857 0.3810 0.3810 0.4762 0.4762 0.5238 0.6190 0.6190 0.7619 0.7619 0.7619 1.0000 1.0000 1.0000 1.0000 1.0000 0.1250 0.1250 0.3125 0.3125 0.3125 0.4375 0.4375 0.5000 0.5625 0.6250 0.7500 0.7500 0.9375 0.9375 0.9375 1.0000 0.0952 0.0952 0.1905 0.1905 0.2857 0.2857 0.4762 0.4762 0.4762 0.4762 0.5714 0.5714 0.6190 0.6667 0.8095 0.8095 0.8095 0.8571 1.0000 1.0000 1.0000 0.0526 0.1053 0.1579 0.3684 0.3684 0.3684 0.3684 0.4737 0.4737 0.5263 0.5789 0.7368 0.7368 0.7368 0.9474 0.9474 0.9474 0.9474 1.0000 0.0476 0.1905 0.1905 0.1905 0.2381 0.3333 0.3333 0.4286 0.4286 0.5714 0.5714 0.5714 0.7143 0.7143 0.7143 0.8571 0.8571 0.8571 1.0000 1.0000 1.0000 0.0476 0.1905 0.1905 0.1905 0.2857 0.2857 0.4762 0.4762 0.4762 0.4762 0.5714 0.5714 0.7143 0.7143 0.7143 0.7619 0.8095 0.8571 1.0000 1.0000 1.0000 0.1500 0.1500 0.1500 0.2000 0.3000 0.3000 0.4000 0.4000 0.6000 0.6000 0.6000 0.6000 0.6500 0.7000 0.8000 0.8000 0.9000 0.9000 1.0000 1.0000 0.1875 0.1875 0.1875 0.2500 0.3750 0.3750 0.6250 0.6250 0.6250 0.6250 0.6875 0.8125 0.8125 0.8750 1.0000 1.0000 0.1364 0.1364 0.1364 0.1818 0.3182 0.3182 0.3182 0.4545 0.4545 0.4545 0.5000 0.6818 0.6818 0.6818 0.6818 0.7273 0.7727 0.9091 0.9091 0.9091 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.1.8.5 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY b%10 RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.1.8.6 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%2 ORDER BY b%10 RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.1.8.1 { + set myres {} + foreach r [db eval {SELECT ntile(100) OVER ( ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 88.0000 89.0000 89.0000 90.0000 90.0000 91.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.1.8.2 { + set myres {} + foreach r [db eval {SELECT ntile(101) OVER ( PARTITION BY b%10 ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 22.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.1.8.3 { + set myres {} + foreach r [db eval {SELECT ntile(102) OVER ( ORDER BY b,a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 88.0000 89.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.1.8.4 { + set myres {} + foreach r [db eval {SELECT ntile(103) OVER ( PARTITION BY b%10 ORDER BY b,a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 22.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.1.8.5 { + set myres {} + foreach r [db eval {SELECT ntile(104) OVER ( ORDER BY b%10,a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000 103.0000 104.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.1.8.6 { + set myres {} + foreach r [db eval {SELECT ntile(105) OVER (PARTITION BY b%2,a ORDER BY b%10 RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.1.8.7 { + set myres {} + foreach r [db eval {SELECT ntile(105) OVER ( RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 88.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000 103.0000 104.0000 105.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + +do_execsql_test 1.1.9.1 { + SELECT last_value(a+b) OVER ( ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {99 92 108 72 52 83 55 79 109 65 26 120 119 50 + 70 103 80 124 36 96 59 124 116 110 57 51 52 130 + 103 74 87 48 128 117 105 136 131 71 133 92 109 63 + 84 109 57 146 78 147 113 74 88 150 87 110 65 121 + 106 110 124 85 145 107 161 171 150 156 80 171 120 + 109 158 114 111 136 147 87 173 124 168 173 162 132 + 101 154 167 190 161 110 156 195 198 102 123 177 169 + 140 111 180 119 160 197 152 124 121 134 146 147 132 + 213 141 193 200 210 157 132 136 175 161 218 188 226 + 191 187 208 211 179 138 144 223 196 214 170 212 202 + 163 184 172 173 195 229 240 187 210 200 163 227 228 + 223 191 252 235 225 243 172 187 202 179 179 182 231 + 261 207 263 206 189 209 212 276 181 274 249 239 234 + 213 234 269 196 271 221 210 229 235 250 223 232 229 + 279 224 280 216 207} + +do_execsql_test 1.1.9.2 { + SELECT last_value(a+b) OVER ( PARTITION BY b%10 ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {147 106 109 168 134 218 191 212 229 240 213 234 196 + 223 92 109 105 136 146 65 156 132 154 102 123 119 + 160 152 146 147 136 243 261 263 210 79 63 84 78 + 120 87 162 124 141 138 227 228 179 231 234 280 124 + 57 130 92 57 110 114 136 147 167 110 180 193 191 + 252 187 179 206 181 221 279 80 116 117 71 80 171 + 173 177 157 161 179 214 225 182 209 269 271 235 229 + 103 74 131 133 113 74 87 145 190 161 169 140 111 + 132 213 187 208 223 235 189 274 108 65 26 70 51 + 52 128 109 121 124 85 107 150 195 226 172 173 187 + 223 207 212 119 50 124 96 110 87 48 110 173 124 + 197 211 144 196 195 200 202 224 216 207 52 83 103 + 36 88 171 158 156 198 121 210 132 210 239 250 232 + 99 72 55 120 59 109 150 161 111 101 200 175 188 + 170 202 163 184 163 172 276 249 229} + +do_execsql_test 1.1.9.3 { + SELECT last_value(a+b) OVER ( ORDER BY b,a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {65 102 87 138 57 181 80 111 26 48 144 207 36 121 + 132 101 163 172 196 119 136 63 124 179 110 179 182 + 74 132 189 51 52 85 216 163 134 123 210 78 141 57 + 187 71 87 172 173 50 224 88 59 111 170 109 213 + 223 146 147 84 114 191 206 221 157 161 209 229 74 + 140 107 187 207 212 124 202 52 232 55 184 229 106 + 132 152 120 92 110 179 235 65 70 87 110 195 200 + 175 234 160 234 136 80 113 187 109 121 124 196 156 + 210 239 250 72 109 188 202 191 105 154 79 231 147 + 225 103 161 169 223 96 83 249 212 162 227 228 167 + 180 193 117 177 214 145 208 235 150 110 211 103 158 + 200 168 229 92 156 243 280 279 116 173 269 271 131 + 133 223 128 173 197 210 99 150 161 147 218 240 109 + 136 146 261 263 124 130 252 171 190 213 274 108 195 + 226 119 124 171 198 120 276} + +do_execsql_test 1.1.9.4 { + SELECT last_value(a+b) OVER ( PARTITION BY b%10 ORDER BY b,a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {196 134 109 213 223 106 234 191 212 168 229 147 218 + 240 65 102 119 136 123 210 146 147 132 152 160 105 + 154 92 156 243 109 136 146 261 263 87 138 63 124 + 179 78 141 84 120 234 79 231 162 227 228 280 57 + 181 110 179 57 187 114 191 206 221 92 110 136 147 + 167 180 193 279 124 130 252 80 182 71 157 161 209 + 229 179 235 80 225 117 177 214 116 173 269 271 171 + 111 74 132 189 87 74 140 113 187 103 161 169 145 + 208 235 131 133 223 190 213 274 26 51 52 85 172 + 173 107 187 207 212 65 70 109 121 124 223 150 128 + 108 195 226 48 144 207 216 50 224 124 202 87 110 + 195 200 196 96 110 211 173 197 119 124 36 121 132 + 88 52 232 156 210 239 250 83 103 158 210 171 198 + 101 163 172 163 59 111 170 55 184 229 175 72 109 + 188 202 249 200 99 150 161 120 276} + +do_execsql_test 1.1.9.5 { + SELECT last_value(a+b) OVER ( ORDER BY b%10,a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {147 106 109 168 134 218 191 212 229 240 213 234 196 + 223 92 109 105 136 146 65 156 132 154 102 123 119 + 160 152 146 147 136 243 261 263 210 79 63 84 78 + 120 87 162 124 141 138 227 228 179 231 234 280 124 + 57 130 92 57 110 114 136 147 167 110 180 193 191 + 252 187 179 206 181 221 279 80 116 117 71 80 171 + 173 177 157 161 179 214 225 182 209 269 271 235 229 + 103 74 131 133 113 74 87 145 190 161 169 140 111 + 132 213 187 208 223 235 189 274 108 65 26 70 51 + 52 128 109 121 124 85 107 150 195 226 172 173 187 + 223 207 212 119 50 124 96 110 87 48 110 173 124 + 197 211 144 196 195 200 202 224 216 207 52 83 103 + 36 88 171 158 156 198 121 210 132 210 239 250 232 + 99 72 55 120 59 109 150 161 111 101 200 175 188 + 170 202 163 184 163 172 276 249 229} + +do_execsql_test 1.1.9.6 { + SELECT last_value(a+b) OVER (PARTITION BY b%2,a ORDER BY b%10 RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM t2 +} {108 52 83 79 65 26 70 103 80 36 116 51 52 128 + 117 71 63 84 109 78 147 88 121 106 124 85 107 171 + 150 80 171 120 109 158 87 168 173 162 156 195 198 + 177 124 121 134 141 210 157 132 161 218 226 191 179 + 138 214 212 172 173 229 240 187 210 227 228 223 225 + 179 182 231 207 209 212 239 234 213 234 269 196 271 + 235 250 223 232 229 280 99 92 72 55 109 120 119 + 50 124 96 59 124 110 57 130 103 74 87 48 105 136 + 131 133 92 109 57 146 113 74 150 87 110 65 110 + 145 161 156 114 111 136 147 173 124 132 101 154 167 + 190 161 110 102 123 169 140 111 180 119 160 197 152 + 146 147 132 213 193 200 136 175 188 187 208 211 144 + 223 196 170 202 163 184 195 200 163 191 252 235 243 + 172 187 202 179 261 263 206 189 276 181 274 249 221 + 210 229 279 224 216 207} + +do_execsql_test 1.1.10.1 { + SELECT nth_value(b,b+1) OVER (ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} 39 {} {} {} {} + {} {} {} 91 {} {} {} {} {} 77 54 54 {} {} {} + {} 62 {} {} {} {} {} 23 {} {} {} 97 86 {} 59 + {} 84 {} {} 78 65 {} 16 90 81 {} 59 56 40 54 + {} 85 {} {} {} {} 38 {} 32 47 {} 74 35 47 98 + 96 {} 24 {} {} 29 12 46 36 53 {} 81 27 56 {} + {} 81 93 63 81 91 68 53 99 89 13 12 97 91 29 + 7 7 78 35 84 53 84 58 61 91 99 15 61 98 16 5 + 75 56 2 37 3 96 62 95 43 63 35 78 16 67 43 16 + 16 90 72 98 85 56 90 46 29 29 4 74 74 2 76 41 + 46 77 24 27 97 46 89 1 85 1 74 78 61 85 51 59 + 35 30 56 25 47 28 73 6 73 74 93 43 3 56 47 85 + 61 61 93 9 97 62} + +do_execsql_test 1.1.10.2 { + SELECT nth_value(b,b+1) OVER (PARTITION BY b%10 ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} 30 {} {} + {} {} {} {} 91 {} {} {} 91 {} 11 {} {} {} {} + 11 {} {} {} {} {} {} {} {} {} 32 {} {} {} 32 + {} {} 12 {} {} {} {} {} {} {} 43 {} {} {} {} + {} {} {} {} {} {} {} 33 {} 43 {} {} {} {} {} + {} 4 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 15 {} + {} {} {} {} {} 55 {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} 47 {} {} {} {} + {} 27 47 {} {} {} {} {} {} {} {} {} 98 {} 98 + {} {} {} {} {} {} {} {} {} {} {} {} {} 9 {} + {} {} {} {} {} {} 9 9 {} {} {}} + +do_execsql_test 1.1.10.3 { + SELECT nth_value(b,b+1) OVER ( ORDER BY b,a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {{} 1 2 2 2 2 3 3 4 5 5 5 6 6 6 7 7 7 7 + 7 7 8 8 8 8 8 8 9 9 9 9 9 9 9 11 11 12 + 12 12 12 12 12 13 13 14 14 15 15 15 15 15 15 + 16 16 16 16 16 16 17 17 17 17 19 19 19 19 20 + 20 21 21 21 21 21 21 22 22 22 22 22 23 23 23 + 24 25 25 26 26 27 27 27 27 27 27 29 29 29 30 + 30 30 31 31 31 31 31 32 33 33 33 33 33 33 33 + 33 33 33 33 34 34 34 34 34 34 34 35 35 36 36 + 36 37 37 37 37 37 37 38 38 38 38 38 38 39 39 + 39 39 39 40 41 41 41 41 41 42 43 43 43 43 43 + 44 44 44 44 46 46 46 47 47 47 47 47 47 47 47 + 47 47 47 49 49 49 50 51 51 51 52 52 52 53 53 + 54 54 55 55} + +do_execsql_test 1.1.10.4 { + SELECT nth_value(b,b+1) OVER ( PARTITION BY b%10 ORDER BY b,a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + 1 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.1.10.5 { + SELECT nth_value(b,b+1) OVER ( ORDER BY b%10,a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} 30 {} {} + {} {} {} {} 40 {} {} {} 40 41 50 {} {} {} {} + 50 {} {} {} 41 {} 10 91 61 {} 30 {} 10 61 30 + {} {} 10 {} {} {} {} 1 {} 22 80 22 91 93 {} + {} 30 {} {} 91 {} 1 30 91 80 91 {} 43 {} 74 + 21 20 {} {} 74 21 21 2 74 33 81 21 64 64 2 21 + 93 62 14 14 3 91 11 24 55 93 93 62 90 91 55 3 + 24 14 24 91 55 15 72 60 72 61 61 34 43 43 43 + 61 12 4 15 15 51 51 12 23 12 12 25 41 25 13 + 94 12 70 12 84 32 84 94 70 33 12 12 32 41 91 + 70 22 33 84 80 31 75 84 53 75 80 84 80 53 53 + 53 22 44 63 42 95 31 63 44 44 31 90 74 52 63 + 31 63 1 42 90 90 95 3 42} + +do_execsql_test 1.1.10.6 { + SELECT nth_value(b,b+1) OVER (PARTITION BY b%2,a ORDER BY b%10 RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.1.11.1 { + SELECT first_value(b) OVER (ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM t2 +} {89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89} + +do_execsql_test 1.1.11.2 { + SELECT first_value(b) OVER (PARTITION BY b%10 ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM t2 +} {90 90 90 90 90 90 90 90 90 90 90 90 90 90 81 + 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 + 81 81 81 81 81 62 62 62 62 62 62 62 62 62 62 + 62 62 62 62 62 62 93 93 93 93 93 93 93 93 93 + 93 93 93 93 93 93 93 93 93 93 93 93 54 54 54 + 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 + 54 65 65 65 65 65 65 65 65 65 65 65 65 65 65 + 65 65 65 65 65 65 65 96 96 96 96 96 96 96 96 + 96 96 96 96 96 96 96 96 96 96 96 96 96 97 97 + 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 + 97 97 97 38 38 38 38 38 38 38 38 38 38 38 38 + 38 38 38 38 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89} + +do_execsql_test 1.1.11.3 { + SELECT first_value(b) OVER ( ORDER BY b,a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1} + +do_execsql_test 1.1.11.4 { + SELECT first_value(b) OVER ( PARTITION BY b%10 ORDER BY b,a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {10 10 10 10 10 10 10 10 10 10 10 10 10 10 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 + 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 + 5 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 + 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 + 7 7 7 7 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 + 8 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 + 9 9 9 9} + +do_execsql_test 1.1.11.5 { + SELECT first_value(b) OVER ( ORDER BY b%10,a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90} + +do_execsql_test 1.1.11.6 { + SELECT first_value(b) OVER (PARTITION BY b%2,a ORDER BY b%10 RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM t2 +} {96 38 68 62 46 6 46 78 54 8 84 16 16 86 74 24 + 12 32 56 22 90 28 56 40 56 16 36 98 76 4 94 + 42 30 78 2 80 84 72 58 96 98 74 12 8 20 22 88 + 34 8 34 90 96 60 44 2 74 70 26 26 80 90 36 58 + 72 72 66 64 12 14 62 36 34 36 58 52 30 50 84 + 10 84 44 58 30 38 34 82 89 81 59 39 91 99 97 + 27 97 67 29 93 77 23 93 65 35 47 7 61 91 85 + 85 43 59 3 91 55 15 89 25 47 1 43 75 89 81 33 + 29 53 63 87 37 41 9 61 73 95 65 13 1 21 65 35 + 5 73 11 51 87 41 31 31 15 95 73 79 11 49 59 + 55 75 77 7 85 57 29 59 19 39 47 47 9 33 93 75 + 81 9 23 37 13 91 91 33 15 99 3 95 69 33 21 39 + 83 27 17 7} + +do_execsql_test 1.1.12.1 { + SELECT lead(b,b) OVER (ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM t2 +} {96 9 11 89 32 53 91 30 51 56 54 73 22 59 75 + 74 78 8 16 65 15 8 31 87 90 12 32 96 74 76 37 + 85 90 15 35 2 60 36 75 9 51 47 63 51 90 26 42 + 26 8 76 80 90 37 87 56 79 5 87 8 2 39 73 64 + 36 90 72 78 36 73 51 33 20 41 2 26 37 33 8 14 + 33 81 55 1 9 12 39 64 87 72 34 82 21 34 99 62 + 74 41 69 22 75 27 58 8 79 77 26 26 55 {} 29 + 30 7 {} 66 55 2 34 64 {} 33 {} 44 84 {} {} 95 + 85 19 {} 83 {} 91 {} {} 9 50 91 33 34 {} {} + 84 {} 7 9 {} {} {} 44 {} {} {} {} 91 84 {} 95 + 95 52 {} {} {} {} {} 21 {} {} {} 58 {} {} {} + {} {} {} {} 83 {} {} {} {} {} {} {} {} {} {} + {} {} {} {}} + +do_execsql_test 1.1.12.2 { + SELECT lead(b,b) OVER (PARTITION BY b%10 ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 81 {} {} {} 21 {} {} {} {} {} {} + {} {} {} {} {} {} 62 {} {} {} 12 {} {} {} 72 + {} {} {} {} {} {} {} {} {} {} 53 {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 34 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} 95 {} {} {} {} {} {} 85 {} + {} {} {} {} {} {} {} {} {} 56 {} 36 {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 57 {} {} {} {} {} 7 {} {} {} {} + {} {} {} {} {} {} 8 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 9 {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.1.12.3 { + SELECT lead(b,b) OVER ( ORDER BY b,a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {1 2 3 3 5 6 7 8 8 9 9 10 11 12 12 13 13 + 14 15 16 16 17 19 20 21 22 23 24 25 26 27 27 + 28 29 30 31 32 33 33 33 34 34 35 36 36 36 37 + 38 39 39 40 41 42 43 43 44 46 47 47 47 49 50 + 52 53 54 55 56 56 57 58 58 58 59 59 59 60 61 + 62 62 64 65 65 67 69 70 72 72 73 74 74 75 75 + 75 77 78 80 81 81 83 84 84 85 85 85 87 88 89 + 89 89 90 90 91 91 91 93 93 94 95 95 96 97 97 + 98 99 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.1.12.4 { + SELECT lead(b,b) OVER ( PARTITION BY b%10 ORDER BY b,a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {80 {} {} {} {} {} {} {} {} {} {} {} {} {} 1 + 11 81 81 {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} 12 12 72 82 {} {} {} {} {} {} + {} {} {} {} {} {} 13 23 73 73 {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} 34 84 {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 35 85 85 95 {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} 36 86 96 96 {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 37 47 + 47 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} 58 58 68 {} {} {} {} {} {} {} {} {} + {} {} {} {} 39 49 59 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.1.12.5 { + SELECT lead(b,b) OVER ( ORDER BY b%10,a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {34 72 91 74 21 55 23 94 84 95 2 13 61 22 55 + 55 94 85 75 81 65 73 74 21 72 12 94 33 63 73 + 72 96 36 76 3 25 62 3 73 34 12 46 43 93 72 16 + 86 63 15 65 36 77 24 57 25 53 95 34 95 16 97 + 74 97 67 25 98 44 34 65 54 5 68 96 28 47 95 + 34 39 8 38 6 46 96 28 47 95 56 39 99 97 76 8 + 26 9 79 27 95 16 29 {} 58 58 77 85 56 {} 98 + 29 {} 19 96 {} {} 78 56 98 36 97 {} 89 89 29 + 47 78 {} {} {} 38 68 58 {} 58 38 {} 98 {} {} + {} 39 57 9 {} 79 {} {} 7 {} {} {} 9 29 38 78 + {} {} {} 8 39 {} {} {} {} 59 {} 99 {} {} {} + {} {} {} {} {} {} {} {} {} {} 9 {} {} {} {} + {} {} {} {} {} {} {} {}} + +do_execsql_test 1.1.12.6 { + SELECT lead(b,b) OVER (PARTITION BY b%2,a ORDER BY b%10 RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.1.13.1 { + SELECT lag(b,b) OVER (ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} 38 {} {} {} {} + {} {} {} 6 {} {} {} {} {} 81 46 6 {} {} {} {} + 23 {} {} {} {} {} 27 {} {} {} 35 6 {} 12 {} + 23 {} {} 61 84 {} 93 39 47 {} 54 46 96 56 {} + 16 {} {} {} {} 89 {} 16 43 {} 85 56 29 99 53 + {} 59 {} {} 91 59 53 84 99 {} 93 63 47 {} {} + 98 33 67 35 75 1 23 13 55 27 75 98 35 73 63 2 + 21 27 13 24 86 23 84 31 20 94 61 65 75 23 36 + 94 55 90 41 77 96 56 29 40 12 89 63 11 5 73 + 79 1 16 28 31 73 5 39 53 63 41 11 40 2 13 33 + 9 29 90 47 72 9 73 30 44 33 74 93 29 74 42 34 + 63 41 34 96 47 77 1 36 74 72 14 36 26 77 9 72 + 64 8 91 31 52 30} + +do_execsql_test 1.1.13.2 { + SELECT lag(b,b) OVER (PARTITION BY b%10 ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} 30 {} {} + {} {} {} {} 91 {} {} {} 61 {} 81 {} {} {} {} + 1 {} {} {} {} {} {} {} {} {} 22 {} {} {} 12 + {} {} 62 {} {} {} {} {} {} {} 23 {} {} {} {} + {} {} {} {} {} {} {} 43 {} 23 {} {} {} {} {} + {} 54 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 75 {} + {} {} {} {} {} 55 {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} 47 {} {} {} {} + {} 27 7 {} {} {} {} {} {} {} {} {} 68 {} 8 {} + {} {} {} {} {} {} {} {} {} {} {} {} 89 {} {} + {} {} {} {} {} 29 9 {} {} {}} + +do_execsql_test 1.1.13.3 { + SELECT lag(b,b) OVER ( ORDER BY b,a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {{} 1 1 1 1 2 2 2 2 2 2 3 3 3 4 4 5 6 6 + 6 7 7 7 7 7 8 8 8 8 8 8 9 9 9 9 9 9 9 + 9 9 9 10 10 10 10 11 11 11 11 11 12 12 12 12 + 13 13 13 13 13 14 15 15 15 15 16 16 16 16 16 + 17 19 20 20 21 21 21 21 22 22 22 22 23 23 23 + 23 23 24 23 24 24 25 26 26 26 26 26 26 26 26 + 26 26 26 27 27 27 27 28 29 29 29 29 30 30 30 + 30 30 30 31 31 31 31 31 32 32 32 32 32 32 31 + 32 33 33 33 33 33 33 34 34 34 34 34 34 34 34 + 35 35 35 35 35 36 36 36 36 36 36 36 37 37 37 + 38 38 38 38 38 38 39 39 39 39 40 40 41 41 42 + 43 42 43 43 43 43 44 44 44 46 46 46 47 47 47 + 47 47} + +do_execsql_test 1.1.13.4 { + SELECT lag(b,b) OVER ( PARTITION BY b%10 ORDER BY b,a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + 1 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.1.13.5 { + SELECT lag(b,b) OVER ( ORDER BY b%10,a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} 30 {} {} + {} {} {} {} 91 {} {} {} 61 80 81 {} {} {} {} + 1 {} {} {} 30 {} 21 90 61 {} 22 {} 11 41 12 + {} {} 62 {} {} {} {} 31 {} 50 23 30 21 90 {} + {} 62 {} {} 81 {} 22 43 62 23 32 {} 91 {} 90 + 93 54 {} {} 90 72 12 22 90 81 83 23 80 20 72 + 43 51 33 80 90 2 34 54 1 20 62 12 13 75 44 30 + 93 91 1 21 55 61 61 13 85 3 65 65 91 73 33 93 + 55 84 62 31 11 65 35 85 33 55 15 12 75 22 3 + 73 65 36 85 43 95 43 13 47 44 65 65 96 36 27 + 7 46 34 94 47 36 73 34 35 73 68 24 8 75 85 75 + 66 34 95 36 84 77 46 34 84 47 89 65 36 16 38 + 76 58 57 29 9 44 56 17} + +do_execsql_test 1.1.13.6 { + SELECT lag(b,b) OVER (PARTITION BY b%2,a ORDER BY b%10 RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.1.14.1 { + SELECT string_agg(CAST(b AS TEXT), '.') OVER (ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM t2 +} {89 89.81 89.81.96 89.81.96.59 89.81.96.59.38 89.81.96.59.38.68 + 89.81.96.59.38.68.39 89.81.96.59.38.68.39.62 89.81.96.59.38.68.39.62.91 + 89.81.96.59.38.68.39.62.91.46 89.81.96.59.38.68.39.62.91.46.6 + 89.81.96.59.38.68.39.62.91.46.6.99 89.81.96.59.38.68.39.62.91.46.6.99.97 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7} + +do_execsql_test 1.1.14.2 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (PARTITION BY b%10 ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM t2 +} {90 90.40 90.40.30 90.40.30.80 90.40.30.80.20 90.40.30.80.20.90 + 90.40.30.80.20.90.60 90.40.30.80.20.90.60.70 90.40.30.80.20.90.60.70.80 + 90.40.30.80.20.90.60.70.80.90 90.40.30.80.20.90.60.70.80.90.30 + 90.40.30.80.20.90.60.70.80.90.30.50 + 90.40.30.80.20.90.60.70.80.90.30.50.10 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30 81 81.91 81.91.61 + 81.91.61.91 81.91.61.91.91 81.91.61.91.91.1 81.91.61.91.91.1.81 + 81.91.61.91.91.1.81.41 81.91.61.91.91.1.81.41.61 + 81.91.61.91.91.1.81.41.61.1 81.91.61.91.91.1.81.41.61.1.21 + 81.91.61.91.91.1.81.41.61.1.21.11 81.91.61.91.91.1.81.41.61.1.21.11.51 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 62 62.12 + 62.12.32 62.12.32.22 62.12.32.22.42 62.12.32.22.42.2 + 62.12.32.22.42.2.72 62.12.32.22.42.2.72.12 62.12.32.22.42.2.72.12.22 + 62.12.32.22.42.2.72.12.22.2 62.12.32.22.42.2.72.12.22.2.72 + 62.12.32.22.42.2.72.12.22.2.72.72 62.12.32.22.42.2.72.12.22.2.72.72.12 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 93 93.23 93.23.93 + 93.23.93.43 93.23.93.43.3 93.23.93.43.3.43 93.23.93.43.3.43.33 + 93.23.93.43.3.43.33.53 93.23.93.43.3.43.33.53.63 + 93.23.93.43.3.43.33.53.63.73 93.23.93.43.3.43.33.53.63.73.13 + 93.23.93.43.3.43.33.53.63.73.13.73 93.23.93.43.3.43.33.53.63.73.13.73.73 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 54 54.84 + 54.84.74 54.84.74.24 54.84.74.24.4 54.84.74.24.4.94 + 54.84.74.24.4.94.84 54.84.74.24.4.94.84.74 54.84.74.24.4.94.84.74.34 + 54.84.74.24.4.94.84.74.34.34 54.84.74.24.4.94.84.74.34.34.44 + 54.84.74.24.4.94.84.74.34.34.44.74 54.84.74.24.4.94.84.74.34.34.44.74.64 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 65 65.35 + 65.35.85 65.35.85.85 65.35.85.85.55 65.35.85.85.55.15 + 65.35.85.85.55.15.25 65.35.85.85.55.15.25.75 65.35.85.85.55.15.25.75.95 + 65.35.85.85.55.15.25.75.95.65 65.35.85.85.55.15.25.75.95.65.65 + 65.35.85.85.55.15.25.75.95.65.65.35 65.35.85.85.55.15.25.75.95.65.65.35.5 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 96 96.46 + 96.46.6 96.46.6.46 96.46.6.46.16 96.46.6.46.16.16 + 96.46.6.46.16.16.86 96.46.6.46.16.16.86.56 96.46.6.46.16.16.86.56.56 + 96.46.6.46.16.16.86.56.56.56 96.46.6.46.16.16.86.56.56.56.16 + 96.46.6.46.16.16.86.56.56.56.16.36 96.46.6.46.16.16.86.56.56.56.16.36.76 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 97 97.27 + 97.27.97 97.27.97.67 97.27.97.67.77 97.27.97.67.77.47 + 97.27.97.67.77.47.7 97.27.97.67.77.47.7.47 97.27.97.67.77.47.7.47.87 + 97.27.97.67.77.47.7.47.87.37 97.27.97.67.77.47.7.47.87.37.87 + 97.27.97.67.77.47.7.47.87.37.87.77 97.27.97.67.77.47.7.47.87.37.87.77.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 38 38.68 + 38.68.78 38.68.78.8 38.68.78.8.28 38.68.78.8.28.98 + 38.68.78.8.28.98.78 38.68.78.8.28.98.78.58 38.68.78.8.28.98.78.58.98 + 38.68.78.8.28.98.78.58.98.8 38.68.78.8.28.98.78.58.98.8.88 + 38.68.78.8.28.98.78.58.98.8.88.8 38.68.78.8.28.98.78.58.98.8.88.8.58 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 89 89.59 89.59.39 + 89.59.39.99 89.59.39.99.29 89.59.39.99.29.59 89.59.39.99.29.59.89 + 89.59.39.99.29.59.89.89 89.59.39.99.29.59.89.89.29 + 89.59.39.99.29.59.89.89.29.9 89.59.39.99.29.59.89.89.29.9.79 + 89.59.39.99.29.59.89.89.29.9.79.49 89.59.39.99.29.59.89.89.29.9.79.49.59 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39} + +do_execsql_test 1.1.14.3 { + SELECT string_agg(CAST(b AS TEXT), '.') OVER ( ORDER BY b,a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {1 1.1 1.1.2 1.1.2.2 1.1.2.2.3 1.1.2.2.3.3 1.1.2.2.3.3.4 + 1.1.2.2.3.3.4.5 1.1.2.2.3.3.4.5.6 1.1.2.2.3.3.4.5.6.7 + 1.1.2.2.3.3.4.5.6.7.7 1.1.2.2.3.3.4.5.6.7.7.7 1.1.2.2.3.3.4.5.6.7.7.7.8 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99} + +do_execsql_test 1.1.14.4 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( PARTITION BY b%10 ORDER BY b,a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {10 10.20 10.20.30 10.20.30.30 10.20.30.30.30 10.20.30.30.30.40 + 10.20.30.30.30.40.50 10.20.30.30.30.40.50.60 10.20.30.30.30.40.50.60.70 + 10.20.30.30.30.40.50.60.70.80 10.20.30.30.30.40.50.60.70.80.80 + 10.20.30.30.30.40.50.60.70.80.80.90 + 10.20.30.30.30.40.50.60.70.80.80.90.90 + 10.20.30.30.30.40.50.60.70.80.80.90.90.90 1 1.1 1.1.11 1.1.11.11 + 1.1.11.11.21 1.1.11.11.21.21 1.1.11.11.21.21.31 1.1.11.11.21.21.31.31 + 1.1.11.11.21.21.31.31.41 1.1.11.11.21.21.31.31.41.41 + 1.1.11.11.21.21.31.31.41.41.51 1.1.11.11.21.21.31.31.41.41.51.61 + 1.1.11.11.21.21.31.31.41.41.51.61.61 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 2 2.2 + 2.2.12 2.2.12.12 2.2.12.12.12 2.2.12.12.12.22 2.2.12.12.12.22.22 + 2.2.12.12.12.22.22.32 2.2.12.12.12.22.22.32.42 + 2.2.12.12.12.22.22.32.42.52 2.2.12.12.12.22.22.32.42.52.62 + 2.2.12.12.12.22.22.32.42.52.62.62 2.2.12.12.12.22.22.32.42.52.62.62.72 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 3 3.3 3.3.13 + 3.3.13.13 3.3.13.13.23 3.3.13.13.23.23 3.3.13.13.23.23.33 + 3.3.13.13.23.23.33.33 3.3.13.13.23.23.33.33.33 + 3.3.13.13.23.23.33.33.33.33 3.3.13.13.23.23.33.33.33.33.43 + 3.3.13.13.23.23.33.33.33.33.43.43 3.3.13.13.23.23.33.33.33.33.43.43.53 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 4 4.14 + 4.14.24 4.14.24.34 4.14.24.34.34 4.14.24.34.34.34 + 4.14.24.34.34.34.34 4.14.24.34.34.34.34.44 4.14.24.34.34.34.34.44.44 + 4.14.24.34.34.34.34.44.44.54 4.14.24.34.34.34.34.44.44.54.64 + 4.14.24.34.34.34.34.44.44.54.64.74 4.14.24.34.34.34.34.44.44.54.64.74.74 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 5 5.15 + 5.15.15 5.15.15.15 5.15.15.15.25 5.15.15.15.25.35 + 5.15.15.15.25.35.35 5.15.15.15.25.35.35.55 5.15.15.15.25.35.35.55.55 + 5.15.15.15.25.35.35.55.55.65 5.15.15.15.25.35.35.55.55.65.65 + 5.15.15.15.25.35.35.55.55.65.65.65 5.15.15.15.25.35.35.55.55.65.65.65.75 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 6 6.16 + 6.16.16 6.16.16.16 6.16.16.16.26 6.16.16.16.26.26 + 6.16.16.16.26.26.36 6.16.16.16.26.26.36.36 6.16.16.16.26.26.36.36.36 + 6.16.16.16.26.26.36.36.36.36 6.16.16.16.26.26.36.36.36.36.46 + 6.16.16.16.26.26.36.36.36.36.46.46 6.16.16.16.26.26.36.36.36.36.46.46.56 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 7 7.7 + 7.7.7 7.7.7.17 7.7.7.17.27 7.7.7.17.27.27 7.7.7.17.27.27.37 + 7.7.7.17.27.27.37.37 7.7.7.17.27.27.37.37.47 7.7.7.17.27.27.37.37.47.47 + 7.7.7.17.27.27.37.37.47.47.47 7.7.7.17.27.27.37.37.47.47.47.47 + 7.7.7.17.27.27.37.37.47.47.47.47.57 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 8 8.8 8.8.8 + 8.8.8.28 8.8.8.28.38 8.8.8.28.38.38 8.8.8.28.38.38.58 + 8.8.8.28.38.38.58.58 8.8.8.28.38.38.58.58.58 8.8.8.28.38.38.58.58.58.58 + 8.8.8.28.38.38.58.58.58.58.68 8.8.8.28.38.38.58.58.58.58.68.78 + 8.8.8.28.38.38.58.58.58.58.68.78.78 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 9 9.9 9.9.9 9.9.9.19 + 9.9.9.19.29 9.9.9.19.29.29 9.9.9.19.29.29.29 9.9.9.19.29.29.29.39 + 9.9.9.19.29.29.29.39.39 9.9.9.19.29.29.29.39.39.39 + 9.9.9.19.29.29.29.39.39.39.49 9.9.9.19.29.29.29.39.39.39.49.59 + 9.9.9.19.29.29.29.39.39.39.49.59.59 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99} + +do_execsql_test 1.1.14.5 { + SELECT string_agg(CAST(b AS TEXT), '.') OVER ( ORDER BY b%10,a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {90 90.40 90.40.30 90.40.30.80 90.40.30.80.20 90.40.30.80.20.90 + 90.40.30.80.20.90.60 90.40.30.80.20.90.60.70 90.40.30.80.20.90.60.70.80 + 90.40.30.80.20.90.60.70.80.90 90.40.30.80.20.90.60.70.80.90.30 + 90.40.30.80.20.90.60.70.80.90.30.50 + 90.40.30.80.20.90.60.70.80.90.30.50.10 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39} + +do_execsql_test 1.1.14.6 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (PARTITION BY b%2,a ORDER BY b%10 RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM t2 +} {96 38 68 62 46 6 46 78 54 8 84 16 16 86 74 24 + 12 32 56 22 90 28 56 40 56 16 36 98 76 4 94 + 42 30 78 2 80 84 72 58 96 98 74 12 8 20 22 88 + 34 8 34 90 96 60 44 2 74 70 26 26 80 90 36 58 + 72 72 66 64 12 14 62 36 34 36 58 52 30 50 84 + 10 84 44 58 30 38 34 82 89 81 59 39 91 99 97 + 27 97 67 29 93 77 23 93 65 35 47 7 61 91 85 + 85 43 59 3 91 55 15 89 25 47 1 43 75 89 81 33 + 29 53 63 87 37 41 9 61 73 95 65 13 1 21 65 35 + 5 73 11 51 87 41 31 31 15 95 73 79 11 49 59 + 55 75 77 7 85 57 29 59 19 39 47 47 9 33 93 75 + 81 9 23 37 13 91 91 33 15 99 3 95 69 33 21 39 + 83 27 17 7} + +do_execsql_test 1.1.14.7 { + SELECT string_agg(CAST(b AS TEXT), '.') OVER (win1 ORDER BY b%10 RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a) + ORDER BY 1 +} {1 1 10 11 11 12 12 12 13 13 14 15 15 15 16 16 + 16 17 19 2 2 20 21 21 22 22 23 23 24 25 26 26 + 27 27 28 29 29 29 3 3 30 30 30 31 31 32 33 33 + 33 33 34 34 34 34 35 35 36 36 36 36 37 37 38 + 38 39 39 39 4 40 41 41 42 43 43 44 44 46 46 + 47 47 47 47 49 5 50 51 52 53 54 55 55 56 56 + 56 57 58 58 58 58 59 59 59 59 6 60 61 61 62 + 62 63 64 65 65 65 66 67 68 69 7 7 7 70 72 72 + 72 73 73 73 74 74 74 75 75 75 76 77 77 78 78 + 79 8 8 8 80 80 81 81 81 82 83 84 84 84 84 85 + 85 85 86 87 87 88 89 89 89 9 9 9 90 90 90 91 + 91 91 91 91 93 93 93 94 95 95 95 96 96 96 97 + 97 98 98 99 99} + +do_execsql_test 1.1.14.8 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (win1 RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a ORDER BY b%10) + ORDER BY 1 +} {1 1 10 11 11 12 12 12 13 13 14 15 15 15 16 16 + 16 17 19 2 2 20 21 21 22 22 23 23 24 25 26 26 + 27 27 28 29 29 29 3 3 30 30 30 31 31 32 33 33 + 33 33 34 34 34 34 35 35 36 36 36 36 37 37 38 + 38 39 39 39 4 40 41 41 42 43 43 44 44 46 46 + 47 47 47 47 49 5 50 51 52 53 54 55 55 56 56 + 56 57 58 58 58 58 59 59 59 59 6 60 61 61 62 + 62 63 64 65 65 65 66 67 68 69 7 7 7 70 72 72 + 72 73 73 73 74 74 74 75 75 75 76 77 77 78 78 + 79 8 8 8 80 80 81 81 81 82 83 84 84 84 84 85 + 85 85 86 87 87 88 89 89 89 9 9 9 90 90 90 91 + 91 91 91 91 93 93 93 94 95 95 95 96 96 96 97 + 97 98 98 99 99} + +do_execsql_test 1.1.14.9 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER win2 + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a ORDER BY b%10), + win2 AS (win1 RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + ORDER BY 1 +} {1 1 10 11 11 12 12 12 13 13 14 15 15 15 16 16 + 16 17 19 2 2 20 21 21 22 22 23 23 24 25 26 26 + 27 27 28 29 29 29 3 3 30 30 30 31 31 32 33 33 + 33 33 34 34 34 34 35 35 36 36 36 36 37 37 38 + 38 39 39 39 4 40 41 41 42 43 43 44 44 46 46 + 47 47 47 47 49 5 50 51 52 53 54 55 55 56 56 + 56 57 58 58 58 58 59 59 59 59 6 60 61 61 62 + 62 63 64 65 65 65 66 67 68 69 7 7 7 70 72 72 + 72 73 73 73 74 74 74 75 75 75 76 77 77 78 78 + 79 8 8 8 80 80 81 81 81 82 83 84 84 84 84 85 + 85 85 86 87 87 88 89 89 89 9 9 9 90 90 90 91 + 91 91 91 91 93 93 93 94 95 95 95 96 96 96 97 + 97 98 98 99 99} + +do_execsql_test 1.1.15.1 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE a%2=0) OVER win FROM t2 + WINDOW win AS (ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) +} {1 89 2 89 3 89.96 4 89.96 5 89.96.38 6 89.96.38 7 89.96.38.39 + 8 89.96.38.39 9 89.96.38.39.91 10 89.96.38.39.91 11 89.96.38.39.91.6 + 12 89.96.38.39.91.6 13 89.96.38.39.91.6.97 14 89.96.38.39.91.6.97 + 15 89.96.38.39.91.6.97.46 16 89.96.38.39.91.6.97.46 + 17 89.96.38.39.91.6.97.46.54 18 89.96.38.39.91.6.97.46.54 + 19 89.96.38.39.91.6.97.46.54.8 20 89.96.38.39.91.6.97.46.54.8 + 21 89.96.38.39.91.6.97.46.54.8.29 22 89.96.38.39.91.6.97.46.54.8.29 + 23 89.96.38.39.91.6.97.46.54.8.29.84 24 89.96.38.39.91.6.97.46.54.8.29.84 + 25 89.96.38.39.91.6.97.46.54.8.29.84.23 + 26 89.96.38.39.91.6.97.46.54.8.29.84.23 + 27 89.96.38.39.91.6.97.46.54.8.29.84.23.16 + 28 89.96.38.39.91.6.97.46.54.8.29.84.23.16 + 29 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65 + 30 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65 + 31 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47 + 32 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47 + 33 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86 + 34 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86 + 35 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61 + 36 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61 + 37 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85 + 38 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85 + 39 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85 + 40 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85 + 41 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59 + 42 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59 + 43 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32 + 44 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32 + 45 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3 + 46 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3 + 47 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22 + 48 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22 + 49 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55 + 50 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55 + 51 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28 + 52 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28 + 53 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25 + 54 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25 + 55 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1 + 56 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1 + 57 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40 + 58 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40 + 59 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56 + 60 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56 + 61 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75 + 62 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75 + 63 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89 + 64 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89 + 65 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76 + 66 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76 + 67 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4 + 68 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4 + 69 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42 + 70 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42 + 71 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78 + 72 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78 + 73 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29 + 74 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29 + 75 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63 + 76 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63 + 77 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87 + 78 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87 + 79 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80 + 80 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80 + 81 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72 + 82 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72 + 83 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9 + 84 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9 + 85 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73 + 86 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73 + 87 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65 + 88 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65 + 89 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58 + 90 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58 + 91 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98 + 92 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98 + 93 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21 + 94 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21 + 95 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65 + 96 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65 + 97 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5 + 98 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5 + 99 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11 + 100 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11 + 101 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87 + 102 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87 + 103 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12 + 104 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12 + 105 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20 + 106 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20 + 107 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31 + 108 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31 + 109 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95 + 110 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95 + 111 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73 + 112 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73 + 113 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88 + 114 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88 + 115 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8 + 116 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8 + 117 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49 + 118 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49 + 119 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90 + 120 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90 + 121 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96 + 122 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96 + 123 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55 + 124 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55 + 125 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77 + 126 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77 + 127 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2 + 128 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2 + 129 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85 + 130 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85 + 131 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74 + 132 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74 + 133 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70 + 134 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70 + 135 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19 + 136 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19 + 137 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26 + 138 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26 + 139 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47 + 140 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47 + 141 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90 + 142 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90 + 143 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58 + 144 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58 + 145 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9 + 146 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9 + 147 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72 + 148 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72 + 149 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33 + 150 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33 + 151 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75 + 152 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75 + 153 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81 + 154 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81 + 155 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23 + 156 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23 + 157 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13 + 158 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13 + 159 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14 + 160 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14 + 161 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91 + 162 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91 + 163 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91 + 164 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91 + 165 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15 + 166 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15 + 167 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36 + 168 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36 + 169 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3 + 170 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3 + 171 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69 + 172 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69 + 173 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52 + 174 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52 + 175 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50 + 176 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50 + 177 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10 + 178 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10 + 179 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33 + 180 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33 + 181 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39 + 182 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39 + 183 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58 + 184 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58 + 185 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38 + 186 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38 + 187 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83 + 188 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83 + 189 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82 + 190 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7} + +do_execsql_test 1.1.15.2 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE 0=1) OVER win FROM t2 + WINDOW win AS (ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) +} {1 {} 2 {} 3 {} 4 {} 5 {} 6 {} 7 {} 8 {} 9 {} 10 {} + 11 {} 12 {} 13 {} 14 {} 15 {} 16 {} 17 {} 18 {} 19 {} + 20 {} 21 {} 22 {} 23 {} 24 {} 25 {} 26 {} 27 {} 28 {} + 29 {} 30 {} 31 {} 32 {} 33 {} 34 {} 35 {} 36 {} 37 {} + 38 {} 39 {} 40 {} 41 {} 42 {} 43 {} 44 {} 45 {} 46 {} + 47 {} 48 {} 49 {} 50 {} 51 {} 52 {} 53 {} 54 {} 55 {} + 56 {} 57 {} 58 {} 59 {} 60 {} 61 {} 62 {} 63 {} 64 {} + 65 {} 66 {} 67 {} 68 {} 69 {} 70 {} 71 {} 72 {} 73 {} + 74 {} 75 {} 76 {} 77 {} 78 {} 79 {} 80 {} 81 {} 82 {} + 83 {} 84 {} 85 {} 86 {} 87 {} 88 {} 89 {} 90 {} 91 {} + 92 {} 93 {} 94 {} 95 {} 96 {} 97 {} 98 {} 99 {} 100 {} + 101 {} 102 {} 103 {} 104 {} 105 {} 106 {} 107 {} 108 {} + 109 {} 110 {} 111 {} 112 {} 113 {} 114 {} 115 {} 116 {} + 117 {} 118 {} 119 {} 120 {} 121 {} 122 {} 123 {} 124 {} + 125 {} 126 {} 127 {} 128 {} 129 {} 130 {} 131 {} 132 {} + 133 {} 134 {} 135 {} 136 {} 137 {} 138 {} 139 {} 140 {} + 141 {} 142 {} 143 {} 144 {} 145 {} 146 {} 147 {} 148 {} + 149 {} 150 {} 151 {} 152 {} 153 {} 154 {} 155 {} 156 {} + 157 {} 158 {} 159 {} 160 {} 161 {} 162 {} 163 {} 164 {} + 165 {} 166 {} 167 {} 168 {} 169 {} 170 {} 171 {} 172 {} + 173 {} 174 {} 175 {} 176 {} 177 {} 178 {} 179 {} 180 {} + 181 {} 182 {} 183 {} 184 {} 185 {} 186 {} 187 {} 188 {} + 189 {} 190 {} 191 {}} + +do_execsql_test 1.1.15.3 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE 1=0) OVER win FROM t2 + WINDOW win AS (PARTITION BY (a%10) ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) +} {1 {} 2 {} 3 {} 4 {} 5 {} 6 {} 7 {} 8 {} 9 {} 10 {} + 11 {} 12 {} 13 {} 14 {} 15 {} 16 {} 17 {} 18 {} 19 {} + 20 {} 1 {} 2 {} 3 {} 4 {} 5 {} 6 {} 7 {} 8 {} 9 {} + 10 {} 11 {} 12 {} 13 {} 14 {} 15 {} 16 {} 17 {} 18 {} + 19 {} 1 {} 2 {} 3 {} 4 {} 5 {} 6 {} 7 {} 8 {} 9 {} + 10 {} 11 {} 12 {} 13 {} 14 {} 15 {} 16 {} 17 {} 18 {} + 19 {} 1 {} 2 {} 3 {} 4 {} 5 {} 6 {} 7 {} 8 {} 9 {} + 10 {} 11 {} 12 {} 13 {} 14 {} 15 {} 16 {} 17 {} 18 {} + 19 {} 1 {} 2 {} 3 {} 4 {} 5 {} 6 {} 7 {} 8 {} 9 {} + 10 {} 11 {} 12 {} 13 {} 14 {} 15 {} 16 {} 17 {} 18 {} + 19 {} 1 {} 2 {} 3 {} 4 {} 5 {} 6 {} 7 {} 8 {} 9 {} + 10 {} 11 {} 12 {} 13 {} 14 {} 15 {} 16 {} 17 {} 18 {} + 19 {} 1 {} 2 {} 3 {} 4 {} 5 {} 6 {} 7 {} 8 {} 9 {} + 10 {} 11 {} 12 {} 13 {} 14 {} 15 {} 16 {} 17 {} 18 {} + 19 {} 1 {} 2 {} 3 {} 4 {} 5 {} 6 {} 7 {} 8 {} 9 {} + 10 {} 11 {} 12 {} 13 {} 14 {} 15 {} 16 {} 17 {} 18 {} + 19 {} 1 {} 2 {} 3 {} 4 {} 5 {} 6 {} 7 {} 8 {} 9 {} + 10 {} 11 {} 12 {} 13 {} 14 {} 15 {} 16 {} 17 {} 18 {} + 19 {} 1 {} 2 {} 3 {} 4 {} 5 {} 6 {} 7 {} 8 {} 9 {} + 10 {} 11 {} 12 {} 13 {} 14 {} 15 {} 16 {} 17 {} 18 {} + 19 {}} + +do_execsql_test 1.1.15.4 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE a%2=0) OVER win FROM t2 + WINDOW win AS (PARTITION BY (a%10) ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) +} {1 89 2 89.6 3 89.6.29 4 89.6.29.47 5 89.6.29.47.59 + 6 89.6.29.47.59.28 7 89.6.29.47.59.28.75 8 89.6.29.47.59.28.75.78 + 9 89.6.29.47.59.28.75.78.72 10 89.6.29.47.59.28.75.78.72.98 + 11 89.6.29.47.59.28.75.78.72.98.87 12 89.6.29.47.59.28.75.78.72.98.87.73 + 13 89.6.29.47.59.28.75.78.72.98.87.73.96 + 14 89.6.29.47.59.28.75.78.72.98.87.73.96.74 + 15 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90 + 16 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75 + 17 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91 + 18 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69 + 19 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39 + 20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 1 {} 2 {} + 3 {} 4 {} 5 {} 6 {} 7 {} 8 {} 9 {} 10 {} 11 {} 12 {} + 13 {} 14 {} 15 {} 16 {} 17 {} 18 {} 19 {} 1 96 2 96.97 + 3 96.97.84 4 96.97.84.86 5 96.97.84.86.32 6 96.97.84.86.32.25 + 7 96.97.84.86.32.25.89 8 96.97.84.86.32.25.89.29 + 9 96.97.84.86.32.25.89.29.9 10 96.97.84.86.32.25.89.29.9.21 + 11 96.97.84.86.32.25.89.29.9.21.12 12 96.97.84.86.32.25.89.29.9.21.12.88 + 13 96.97.84.86.32.25.89.29.9.21.12.88.55 + 14 96.97.84.86.32.25.89.29.9.21.12.88.55.70 + 15 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58 + 16 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81 + 17 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91 + 18 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52 + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 1 {} 2 {} + 3 {} 4 {} 5 {} 6 {} 7 {} 8 {} 9 {} 10 {} 11 {} 12 {} + 13 {} 14 {} 15 {} 16 {} 17 {} 18 {} 19 {} 1 38 2 38.46 + 3 38.46.23 4 38.46.23.61 5 38.46.23.61.3 6 38.46.23.61.3.1 + 7 38.46.23.61.3.1.76 8 38.46.23.61.3.1.76.63 9 38.46.23.61.3.1.76.63.73 + 10 38.46.23.61.3.1.76.63.73.65 11 38.46.23.61.3.1.76.63.73.65.20 + 12 38.46.23.61.3.1.76.63.73.65.20.8 + 13 38.46.23.61.3.1.76.63.73.65.20.8.77 + 14 38.46.23.61.3.1.76.63.73.65.20.8.77.19 + 15 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9 + 16 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23 + 17 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15 + 18 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50 + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 1 {} 2 {} + 3 {} 4 {} 5 {} 6 {} 7 {} 8 {} 9 {} 10 {} 11 {} 12 {} + 13 {} 14 {} 15 {} 16 {} 17 {} 18 {} 19 {} 1 39 2 39.54 + 3 39.54.16 4 39.54.16.85 5 39.54.16.85.22 6 39.54.16.85.22.40 + 7 39.54.16.85.22.40.4 8 39.54.16.85.22.40.4.87 + 9 39.54.16.85.22.40.4.87.65 10 39.54.16.85.22.40.4.87.65.5 + 11 39.54.16.85.22.40.4.87.65.5.31 12 39.54.16.85.22.40.4.87.65.5.31.49 + 13 39.54.16.85.22.40.4.87.65.5.31.49.2 + 14 39.54.16.85.22.40.4.87.65.5.31.49.2.26 + 15 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72 + 16 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13 + 17 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36 + 18 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10 + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 1 {} 2 {} + 3 {} 4 {} 5 {} 6 {} 7 {} 8 {} 9 {} 10 {} 11 {} 12 {} + 13 {} 14 {} 15 {} 16 {} 17 {} 18 {} 19 {} 1 91 2 91.8 + 3 91.8.65 4 91.8.65.85 5 91.8.65.85.55 6 91.8.65.85.55.56 + 7 91.8.65.85.55.56.42 8 91.8.65.85.55.56.42.80 + 9 91.8.65.85.55.56.42.80.58 10 91.8.65.85.55.56.42.80.58.11 + 11 91.8.65.85.55.56.42.80.58.11.95 12 91.8.65.85.55.56.42.80.58.11.95.90 + 13 91.8.65.85.55.56.42.80.58.11.95.90.85 + 14 91.8.65.85.55.56.42.80.58.11.95.90.85.47 + 15 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33 + 16 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14 + 17 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3 + 18 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33 + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 1 {} 2 {} + 3 {} 4 {} 5 {} 6 {} 7 {} 8 {} 9 {} 10 {} 11 {} 12 {} + 13 {} 14 {} 15 {} 16 {} 17 {} 18 {} 19 {}} + +do_execsql_test 1.2.2.1 { + SELECT max(b) OVER ( ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99} + +do_execsql_test 1.2.2.2 { + SELECT min(b) OVER ( ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1} + +do_execsql_test 1.2.3.1 { + SELECT row_number() OVER ( ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.2.3.2 { + SELECT row_number() OVER ( PARTITION BY b%10 ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.2.3.3 { + SELECT row_number() OVER ( RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.2.4.1 { + SELECT dense_rank() OVER ( ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.2.4.2 { + SELECT dense_rank() OVER ( PARTITION BY b%10 ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.2.4.3 { + SELECT dense_rank() OVER ( ORDER BY b RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 1 2 2 3 3 4 5 6 7 7 7 8 8 8 9 9 9 10 + 11 11 12 12 12 13 13 14 15 15 15 16 16 16 17 + 18 19 20 20 21 21 22 22 23 24 25 25 26 26 27 + 28 28 28 29 29 29 30 30 31 32 32 32 32 33 33 + 33 33 34 34 35 35 35 35 36 36 37 37 38 38 38 + 39 40 40 41 42 42 43 43 44 44 45 45 45 45 46 + 47 48 49 50 51 52 52 53 53 53 54 55 55 55 55 + 56 56 56 56 57 58 58 59 59 60 61 62 62 62 63 + 64 65 66 67 68 68 68 69 69 69 70 70 70 71 71 + 71 72 73 73 74 74 75 76 76 77 77 77 78 79 80 + 80 80 80 81 81 81 82 83 83 84 85 85 85 86 86 + 86 87 87 87 87 87 88 88 88 89 90 90 90 91 91 + 91 92 92 93 93 94 94} + +do_execsql_test 1.2.4.4 { + SELECT dense_rank() OVER ( PARTITION BY b%10 ORDER BY b RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 3 3 4 5 6 7 8 8 9 9 9 1 1 2 2 3 + 3 4 4 5 5 6 7 7 8 8 8 9 9 9 9 9 1 1 2 + 2 2 3 3 4 5 6 7 7 8 8 8 9 1 1 2 2 3 3 + 4 4 4 4 5 5 6 7 8 8 8 9 10 10 10 1 2 3 + 4 4 4 4 5 5 6 7 8 8 8 9 9 9 9 10 1 2 2 + 2 3 4 4 5 5 6 6 6 7 7 7 8 8 8 9 9 9 1 + 2 2 2 3 3 4 4 4 4 5 5 6 6 6 7 8 9 10 10 + 10 1 1 1 2 3 3 4 4 5 5 5 5 6 7 8 8 9 9 + 10 10 1 1 1 2 3 3 4 4 4 4 5 6 6 7 8 8 1 + 1 1 2 3 3 3 4 4 4 5 6 6 6 6 7 8 9 9 9 + 10 10} + +do_execsql_test 1.2.4.5 { + SELECT dense_rank() OVER ( ORDER BY b%10 RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 + 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 + 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 + 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 + 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 + 8 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 10 10 + 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 + 10 10 10 10 10} + +do_execsql_test 1.2.4.6 { + SELECT dense_rank() OVER ( PARTITION BY b%2 ORDER BY b%10 RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 + 5 5 5 5 5 5 5 5 5 5 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 + 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 + 5} + +do_execsql_test 1.2.5.1 { + SELECT rank() OVER ( ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.2.5.2 { + SELECT rank() OVER ( PARTITION BY b%10 ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.2.5.3 { + SELECT rank() OVER ( ORDER BY b RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 1 3 3 5 5 7 8 9 10 10 10 13 13 13 16 16 + 16 19 20 20 22 22 22 25 25 27 28 28 28 31 31 + 31 34 35 36 37 37 39 39 41 41 43 44 45 45 47 + 47 49 50 50 50 53 53 53 56 56 58 59 59 59 59 + 63 63 63 63 67 67 69 69 69 69 73 73 75 75 77 + 77 77 80 81 81 83 84 84 86 86 88 88 90 90 90 + 90 94 95 96 97 98 99 100 100 102 102 102 105 106 + 106 106 106 110 110 110 110 114 115 115 117 117 119 + 120 121 121 121 124 125 126 127 128 129 129 129 132 + 132 132 135 135 135 138 138 138 141 142 142 144 144 + 146 147 147 149 149 149 152 153 154 154 154 154 158 + 158 158 161 162 162 164 165 165 165 168 168 168 171 + 171 171 171 171 176 176 176 179 180 180 180 183 183 + 183 186 186 188 188 190 190} + +do_execsql_test 1.2.5.4 { + SELECT rank() OVER ( PARTITION BY b%10 ORDER BY b RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 3 3 6 7 8 9 10 10 12 12 12 1 1 3 3 + 5 5 7 7 9 9 11 12 12 14 14 14 17 17 17 17 + 17 1 1 3 3 3 6 6 8 9 10 11 11 13 13 13 16 + 1 1 3 3 5 5 7 7 7 7 11 11 13 14 15 15 15 + 18 19 19 19 1 2 3 4 4 4 4 8 8 10 11 12 12 + 12 15 15 15 15 19 1 2 2 2 5 6 6 8 8 10 10 + 10 13 13 13 16 16 16 19 19 19 1 2 2 2 5 5 7 + 7 7 7 11 11 13 13 13 16 17 18 19 19 19 1 1 + 1 4 5 5 7 7 9 9 9 9 13 14 15 15 17 17 19 + 19 1 1 1 4 5 5 7 7 7 7 11 12 12 14 15 15 + 1 1 1 4 5 5 5 8 8 8 11 12 12 12 12 16 17 + 18 18 18 21 21} + +do_execsql_test 1.2.5.5 { + SELECT rank() OVER ( ORDER BY b%10 RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 15 15 15 15 + 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 + 15 15 36 36 36 36 36 36 36 36 36 36 36 36 36 + 36 36 36 52 52 52 52 52 52 52 52 52 52 52 52 + 52 52 52 52 52 52 52 52 52 73 73 73 73 73 73 + 73 73 73 73 73 73 73 73 73 73 73 73 73 92 92 + 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 + 92 92 92 92 113 113 113 113 113 113 113 113 113 + 113 113 113 113 113 113 113 113 113 113 113 113 134 + 134 134 134 134 134 134 134 134 134 134 134 134 134 + 134 134 134 134 134 134 154 154 154 154 154 154 154 + 154 154 154 154 154 154 154 154 154 170 170 170 170 + 170 170 170 170 170 170 170 170 170 170 170 170 170 + 170 170 170 170 170} + +do_execsql_test 1.2.5.6 { + SELECT rank() OVER ( PARTITION BY b%2 ORDER BY b%10 RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 15 15 15 15 + 15 15 15 15 15 15 15 15 15 15 15 15 31 31 31 + 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 + 31 50 50 50 50 50 50 50 50 50 50 50 50 50 50 + 50 50 50 50 50 50 50 71 71 71 71 71 71 71 71 + 71 71 71 71 71 71 71 71 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 22 22 22 22 22 22 + 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 + 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 + 43 43 43 43 43 43 64 64 64 64 64 64 64 64 64 + 64 64 64 64 64 64 64 64 64 64 64 84 84 84 84 + 84 84 84 84 84 84 84 84 84 84 84 84 84 84 84 + 84 84 84} + +do_execsql_test 1.2.6.1 { + SELECT + row_number() OVER ( PARTITION BY b%2 ORDER BY b%10 RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ), + rank() OVER ( PARTITION BY b%2 ORDER BY b%10 RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ), + dense_rank() OVER ( PARTITION BY b%2 ORDER BY b%10 RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) + FROM t2 +} {1 1 1 2 1 1 3 1 1 4 1 1 5 1 1 6 1 1 7 1 1 8 1 1 9 1 1 + 10 1 1 11 1 1 12 1 1 13 1 1 14 1 1 15 15 2 16 15 2 17 15 2 + 18 15 2 19 15 2 20 15 2 21 15 2 22 15 2 23 15 2 24 15 2 + 25 15 2 26 15 2 27 15 2 28 15 2 29 15 2 30 15 2 31 31 3 + 32 31 3 33 31 3 34 31 3 35 31 3 36 31 3 37 31 3 38 31 3 + 39 31 3 40 31 3 41 31 3 42 31 3 43 31 3 44 31 3 45 31 3 + 46 31 3 47 31 3 48 31 3 49 31 3 50 50 4 51 50 4 52 50 4 + 53 50 4 54 50 4 55 50 4 56 50 4 57 50 4 58 50 4 59 50 4 + 60 50 4 61 50 4 62 50 4 63 50 4 64 50 4 65 50 4 66 50 4 + 67 50 4 68 50 4 69 50 4 70 50 4 71 71 5 72 71 5 73 71 5 + 74 71 5 75 71 5 76 71 5 77 71 5 78 71 5 79 71 5 80 71 5 + 81 71 5 82 71 5 83 71 5 84 71 5 85 71 5 86 71 5 1 1 1 2 1 1 + 3 1 1 4 1 1 5 1 1 6 1 1 7 1 1 8 1 1 9 1 1 10 1 1 11 1 1 + 12 1 1 13 1 1 14 1 1 15 1 1 16 1 1 17 1 1 18 1 1 19 1 1 + 20 1 1 21 1 1 22 22 2 23 22 2 24 22 2 25 22 2 26 22 2 27 22 2 + 28 22 2 29 22 2 30 22 2 31 22 2 32 22 2 33 22 2 34 22 2 + 35 22 2 36 22 2 37 22 2 38 22 2 39 22 2 40 22 2 41 22 2 + 42 22 2 43 43 3 44 43 3 45 43 3 46 43 3 47 43 3 48 43 3 + 49 43 3 50 43 3 51 43 3 52 43 3 53 43 3 54 43 3 55 43 3 + 56 43 3 57 43 3 58 43 3 59 43 3 60 43 3 61 43 3 62 43 3 + 63 43 3 64 64 4 65 64 4 66 64 4 67 64 4 68 64 4 69 64 4 + 70 64 4 71 64 4 72 64 4 73 64 4 74 64 4 75 64 4 76 64 4 + 77 64 4 78 64 4 79 64 4 80 64 4 81 64 4 82 64 4 83 64 4 + 84 84 5 85 84 5 86 84 5 87 84 5 88 84 5 89 84 5 90 84 5 + 91 84 5 92 84 5 93 84 5 94 84 5 95 84 5 96 84 5 97 84 5 + 98 84 5 99 84 5 100 84 5 101 84 5 102 84 5 103 84 5 104 84 5 + 105 84 5} + + +do_test 1.2.7.1 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0053 0.0105 0.0158 0.0211 0.0263 0.0316 0.0368 0.0421 0.0474 0.0526 0.0579 0.0632 0.0684 0.0737 0.0789 0.0842 0.0895 0.0947 0.1000 0.1053 0.1105 0.1158 0.1211 0.1263 0.1316 0.1368 0.1421 0.1474 0.1526 0.1579 0.1632 0.1684 0.1737 0.1789 0.1842 0.1895 0.1947 0.2000 0.2053 0.2105 0.2158 0.2211 0.2263 0.2316 0.2368 0.2421 0.2474 0.2526 0.2579 0.2632 0.2684 0.2737 0.2789 0.2842 0.2895 0.2947 0.3000 0.3053 0.3105 0.3158 0.3211 0.3263 0.3316 0.3368 0.3421 0.3474 0.3526 0.3579 0.3632 0.3684 0.3737 0.3789 0.3842 0.3895 0.3947 0.4000 0.4053 0.4105 0.4158 0.4211 0.4263 0.4316 0.4368 0.4421 0.4474 0.4526 0.4579 0.4632 0.4684 0.4737 0.4789 0.4842 0.4895 0.4947 0.5000 0.5053 0.5105 0.5158 0.5211 0.5263 0.5316 0.5368 0.5421 0.5474 0.5526 0.5579 0.5632 0.5684 0.5737 0.5789 0.5842 0.5895 0.5947 0.6000 0.6053 0.6105 0.6158 0.6211 0.6263 0.6316 0.6368 0.6421 0.6474 0.6526 0.6579 0.6632 0.6684 0.6737 0.6789 0.6842 0.6895 0.6947 0.7000 0.7053 0.7105 0.7158 0.7211 0.7263 0.7316 0.7368 0.7421 0.7474 0.7526 0.7579 0.7632 0.7684 0.7737 0.7789 0.7842 0.7895 0.7947 0.8000 0.8053 0.8105 0.8158 0.8211 0.8263 0.8316 0.8368 0.8421 0.8474 0.8526 0.8579 0.8632 0.8684 0.8737 0.8789 0.8842 0.8895 0.8947 0.9000 0.9053 0.9105 0.9158 0.9211 0.9263 0.9316 0.9368 0.9421 0.9474 0.9526 0.9579 0.9632 0.9684 0.9737 0.9789 0.9842 0.9895 0.9947 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.2.7.2 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( PARTITION BY b%10 ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0769 0.1538 0.2308 0.3077 0.3846 0.4615 0.5385 0.6154 0.6923 0.7692 0.8462 0.9231 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0667 0.1333 0.2000 0.2667 0.3333 0.4000 0.4667 0.5333 0.6000 0.6667 0.7333 0.8000 0.8667 0.9333 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0556 0.1111 0.1667 0.2222 0.2778 0.3333 0.3889 0.4444 0.5000 0.5556 0.6111 0.6667 0.7222 0.7778 0.8333 0.8889 0.9444 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0526 0.1053 0.1579 0.2105 0.2632 0.3158 0.3684 0.4211 0.4737 0.5263 0.5789 0.6316 0.6842 0.7368 0.7895 0.8421 0.8947 0.9474 1.0000 0.0000 0.0667 0.1333 0.2000 0.2667 0.3333 0.4000 0.4667 0.5333 0.6000 0.6667 0.7333 0.8000 0.8667 0.9333 1.0000 0.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.2.7.3 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY b RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0105 0.0105 0.0211 0.0211 0.0316 0.0368 0.0421 0.0474 0.0474 0.0474 0.0632 0.0632 0.0632 0.0789 0.0789 0.0789 0.0947 0.1000 0.1000 0.1105 0.1105 0.1105 0.1263 0.1263 0.1368 0.1421 0.1421 0.1421 0.1579 0.1579 0.1579 0.1737 0.1789 0.1842 0.1895 0.1895 0.2000 0.2000 0.2105 0.2105 0.2211 0.2263 0.2316 0.2316 0.2421 0.2421 0.2526 0.2579 0.2579 0.2579 0.2737 0.2737 0.2737 0.2895 0.2895 0.3000 0.3053 0.3053 0.3053 0.3053 0.3263 0.3263 0.3263 0.3263 0.3474 0.3474 0.3579 0.3579 0.3579 0.3579 0.3789 0.3789 0.3895 0.3895 0.4000 0.4000 0.4000 0.4158 0.4211 0.4211 0.4316 0.4368 0.4368 0.4474 0.4474 0.4579 0.4579 0.4684 0.4684 0.4684 0.4684 0.4895 0.4947 0.5000 0.5053 0.5105 0.5158 0.5211 0.5211 0.5316 0.5316 0.5316 0.5474 0.5526 0.5526 0.5526 0.5526 0.5737 0.5737 0.5737 0.5737 0.5947 0.6000 0.6000 0.6105 0.6105 0.6211 0.6263 0.6316 0.6316 0.6316 0.6474 0.6526 0.6579 0.6632 0.6684 0.6737 0.6737 0.6737 0.6895 0.6895 0.6895 0.7053 0.7053 0.7053 0.7211 0.7211 0.7211 0.7368 0.7421 0.7421 0.7526 0.7526 0.7632 0.7684 0.7684 0.7789 0.7789 0.7789 0.7947 0.8000 0.8053 0.8053 0.8053 0.8053 0.8263 0.8263 0.8263 0.8421 0.8474 0.8474 0.8579 0.8632 0.8632 0.8632 0.8789 0.8789 0.8789 0.8947 0.8947 0.8947 0.8947 0.8947 0.9211 0.9211 0.9211 0.9368 0.9421 0.9421 0.9421 0.9579 0.9579 0.9579 0.9737 0.9737 0.9842 0.9842 0.9947 0.9947} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.2.7.4 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( PARTITION BY b%10 ORDER BY b RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0769 0.1538 0.1538 0.1538 0.3846 0.4615 0.5385 0.6154 0.6923 0.6923 0.8462 0.8462 0.8462 0.0000 0.0000 0.1000 0.1000 0.2000 0.2000 0.3000 0.3000 0.4000 0.4000 0.5000 0.5500 0.5500 0.6500 0.6500 0.6500 0.8000 0.8000 0.8000 0.8000 0.8000 0.0000 0.0000 0.1333 0.1333 0.1333 0.3333 0.3333 0.4667 0.5333 0.6000 0.6667 0.6667 0.8000 0.8000 0.8000 1.0000 0.0000 0.0000 0.1000 0.1000 0.2000 0.2000 0.3000 0.3000 0.3000 0.3000 0.5000 0.5000 0.6000 0.6500 0.7000 0.7000 0.7000 0.8500 0.9000 0.9000 0.9000 0.0000 0.0556 0.1111 0.1667 0.1667 0.1667 0.1667 0.3889 0.3889 0.5000 0.5556 0.6111 0.6111 0.6111 0.7778 0.7778 0.7778 0.7778 1.0000 0.0000 0.0500 0.0500 0.0500 0.2000 0.2500 0.2500 0.3500 0.3500 0.4500 0.4500 0.4500 0.6000 0.6000 0.6000 0.7500 0.7500 0.7500 0.9000 0.9000 0.9000 0.0000 0.0500 0.0500 0.0500 0.2000 0.2000 0.3000 0.3000 0.3000 0.3000 0.5000 0.5000 0.6000 0.6000 0.6000 0.7500 0.8000 0.8500 0.9000 0.9000 0.9000 0.0000 0.0000 0.0000 0.1579 0.2105 0.2105 0.3158 0.3158 0.4211 0.4211 0.4211 0.4211 0.6316 0.6842 0.7368 0.7368 0.8421 0.8421 0.9474 0.9474 0.0000 0.0000 0.0000 0.2000 0.2667 0.2667 0.4000 0.4000 0.4000 0.4000 0.6667 0.7333 0.7333 0.8667 0.9333 0.9333 0.0000 0.0000 0.0000 0.1429 0.1905 0.1905 0.1905 0.3333 0.3333 0.3333 0.4762 0.5238 0.5238 0.5238 0.5238 0.7143 0.7619 0.8095 0.8095 0.8095 0.9524 0.9524} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.2.7.5 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY b%10 RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.2.7.6 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER (PARTITION BY b%2 ORDER BY b%10 RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.2.8.1 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0052 0.0105 0.0157 0.0209 0.0262 0.0314 0.0366 0.0419 0.0471 0.0524 0.0576 0.0628 0.0681 0.0733 0.0785 0.0838 0.0890 0.0942 0.0995 0.1047 0.1099 0.1152 0.1204 0.1257 0.1309 0.1361 0.1414 0.1466 0.1518 0.1571 0.1623 0.1675 0.1728 0.1780 0.1832 0.1885 0.1937 0.1990 0.2042 0.2094 0.2147 0.2199 0.2251 0.2304 0.2356 0.2408 0.2461 0.2513 0.2565 0.2618 0.2670 0.2723 0.2775 0.2827 0.2880 0.2932 0.2984 0.3037 0.3089 0.3141 0.3194 0.3246 0.3298 0.3351 0.3403 0.3455 0.3508 0.3560 0.3613 0.3665 0.3717 0.3770 0.3822 0.3874 0.3927 0.3979 0.4031 0.4084 0.4136 0.4188 0.4241 0.4293 0.4346 0.4398 0.4450 0.4503 0.4555 0.4607 0.4660 0.4712 0.4764 0.4817 0.4869 0.4921 0.4974 0.5026 0.5079 0.5131 0.5183 0.5236 0.5288 0.5340 0.5393 0.5445 0.5497 0.5550 0.5602 0.5654 0.5707 0.5759 0.5812 0.5864 0.5916 0.5969 0.6021 0.6073 0.6126 0.6178 0.6230 0.6283 0.6335 0.6387 0.6440 0.6492 0.6545 0.6597 0.6649 0.6702 0.6754 0.6806 0.6859 0.6911 0.6963 0.7016 0.7068 0.7120 0.7173 0.7225 0.7277 0.7330 0.7382 0.7435 0.7487 0.7539 0.7592 0.7644 0.7696 0.7749 0.7801 0.7853 0.7906 0.7958 0.8010 0.8063 0.8115 0.8168 0.8220 0.8272 0.8325 0.8377 0.8429 0.8482 0.8534 0.8586 0.8639 0.8691 0.8743 0.8796 0.8848 0.8901 0.8953 0.9005 0.9058 0.9110 0.9162 0.9215 0.9267 0.9319 0.9372 0.9424 0.9476 0.9529 0.9581 0.9634 0.9686 0.9738 0.9791 0.9843 0.9895 0.9948 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.2.8.2 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%10 ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0714 0.1429 0.2143 0.2857 0.3571 0.4286 0.5000 0.5714 0.6429 0.7143 0.7857 0.8571 0.9286 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0625 0.1250 0.1875 0.2500 0.3125 0.3750 0.4375 0.5000 0.5625 0.6250 0.6875 0.7500 0.8125 0.8750 0.9375 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0526 0.1053 0.1579 0.2105 0.2632 0.3158 0.3684 0.4211 0.4737 0.5263 0.5789 0.6316 0.6842 0.7368 0.7895 0.8421 0.8947 0.9474 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0625 0.1250 0.1875 0.2500 0.3125 0.3750 0.4375 0.5000 0.5625 0.6250 0.6875 0.7500 0.8125 0.8750 0.9375 1.0000 0.0455 0.0909 0.1364 0.1818 0.2273 0.2727 0.3182 0.3636 0.4091 0.4545 0.5000 0.5455 0.5909 0.6364 0.6818 0.7273 0.7727 0.8182 0.8636 0.9091 0.9545 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.2.8.3 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY b RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0105 0.0105 0.0209 0.0209 0.0314 0.0314 0.0366 0.0419 0.0471 0.0628 0.0628 0.0628 0.0785 0.0785 0.0785 0.0942 0.0942 0.0942 0.0995 0.1099 0.1099 0.1257 0.1257 0.1257 0.1361 0.1361 0.1414 0.1571 0.1571 0.1571 0.1728 0.1728 0.1728 0.1780 0.1832 0.1885 0.1990 0.1990 0.2094 0.2094 0.2199 0.2199 0.2251 0.2304 0.2408 0.2408 0.2513 0.2513 0.2565 0.2723 0.2723 0.2723 0.2880 0.2880 0.2880 0.2984 0.2984 0.3037 0.3246 0.3246 0.3246 0.3246 0.3455 0.3455 0.3455 0.3455 0.3560 0.3560 0.3770 0.3770 0.3770 0.3770 0.3874 0.3874 0.3979 0.3979 0.4136 0.4136 0.4136 0.4188 0.4293 0.4293 0.4346 0.4450 0.4450 0.4555 0.4555 0.4660 0.4660 0.4869 0.4869 0.4869 0.4869 0.4921 0.4974 0.5026 0.5079 0.5131 0.5183 0.5288 0.5288 0.5445 0.5445 0.5445 0.5497 0.5707 0.5707 0.5707 0.5707 0.5916 0.5916 0.5916 0.5916 0.5969 0.6073 0.6073 0.6178 0.6178 0.6230 0.6283 0.6440 0.6440 0.6440 0.6492 0.6545 0.6597 0.6649 0.6702 0.6859 0.6859 0.6859 0.7016 0.7016 0.7016 0.7173 0.7173 0.7173 0.7330 0.7330 0.7330 0.7382 0.7487 0.7487 0.7592 0.7592 0.7644 0.7749 0.7749 0.7906 0.7906 0.7906 0.7958 0.8010 0.8220 0.8220 0.8220 0.8220 0.8377 0.8377 0.8377 0.8429 0.8534 0.8534 0.8586 0.8743 0.8743 0.8743 0.8901 0.8901 0.8901 0.9162 0.9162 0.9162 0.9162 0.9162 0.9319 0.9319 0.9319 0.9372 0.9529 0.9529 0.9529 0.9686 0.9686 0.9686 0.9791 0.9791 0.9895 0.9895 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.2.8.4 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%10 ORDER BY b RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0714 0.1429 0.3571 0.3571 0.3571 0.4286 0.5000 0.5714 0.6429 0.7857 0.7857 1.0000 1.0000 1.0000 0.0952 0.0952 0.1905 0.1905 0.2857 0.2857 0.3810 0.3810 0.4762 0.4762 0.5238 0.6190 0.6190 0.7619 0.7619 0.7619 1.0000 1.0000 1.0000 1.0000 1.0000 0.1250 0.1250 0.3125 0.3125 0.3125 0.4375 0.4375 0.5000 0.5625 0.6250 0.7500 0.7500 0.9375 0.9375 0.9375 1.0000 0.0952 0.0952 0.1905 0.1905 0.2857 0.2857 0.4762 0.4762 0.4762 0.4762 0.5714 0.5714 0.6190 0.6667 0.8095 0.8095 0.8095 0.8571 1.0000 1.0000 1.0000 0.0526 0.1053 0.1579 0.3684 0.3684 0.3684 0.3684 0.4737 0.4737 0.5263 0.5789 0.7368 0.7368 0.7368 0.9474 0.9474 0.9474 0.9474 1.0000 0.0476 0.1905 0.1905 0.1905 0.2381 0.3333 0.3333 0.4286 0.4286 0.5714 0.5714 0.5714 0.7143 0.7143 0.7143 0.8571 0.8571 0.8571 1.0000 1.0000 1.0000 0.0476 0.1905 0.1905 0.1905 0.2857 0.2857 0.4762 0.4762 0.4762 0.4762 0.5714 0.5714 0.7143 0.7143 0.7143 0.7619 0.8095 0.8571 1.0000 1.0000 1.0000 0.1500 0.1500 0.1500 0.2000 0.3000 0.3000 0.4000 0.4000 0.6000 0.6000 0.6000 0.6000 0.6500 0.7000 0.8000 0.8000 0.9000 0.9000 1.0000 1.0000 0.1875 0.1875 0.1875 0.2500 0.3750 0.3750 0.6250 0.6250 0.6250 0.6250 0.6875 0.8125 0.8125 0.8750 1.0000 1.0000 0.1364 0.1364 0.1364 0.1818 0.3182 0.3182 0.3182 0.4545 0.4545 0.4545 0.5000 0.6818 0.6818 0.6818 0.6818 0.7273 0.7727 0.9091 0.9091 0.9091 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.2.8.5 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY b%10 RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.2.8.6 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%2 ORDER BY b%10 RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.2.8.1 { + set myres {} + foreach r [db eval {SELECT ntile(100) OVER ( ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 88.0000 89.0000 89.0000 90.0000 90.0000 91.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.2.8.2 { + set myres {} + foreach r [db eval {SELECT ntile(101) OVER ( PARTITION BY b%10 ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 22.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.2.8.3 { + set myres {} + foreach r [db eval {SELECT ntile(102) OVER ( ORDER BY b,a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 88.0000 89.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.2.8.4 { + set myres {} + foreach r [db eval {SELECT ntile(103) OVER ( PARTITION BY b%10 ORDER BY b,a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 22.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.2.8.5 { + set myres {} + foreach r [db eval {SELECT ntile(104) OVER ( ORDER BY b%10,a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000 103.0000 104.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.2.8.6 { + set myres {} + foreach r [db eval {SELECT ntile(105) OVER (PARTITION BY b%2,a ORDER BY b%10 RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.2.8.7 { + set myres {} + foreach r [db eval {SELECT ntile(105) OVER ( RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 88.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000 103.0000 104.0000 105.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + +do_execsql_test 1.2.9.1 { + SELECT last_value(a+b) OVER ( ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207} + +do_execsql_test 1.2.9.2 { + SELECT last_value(a+b) OVER ( PARTITION BY b%10 ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {223 223 223 223 223 223 223 223 223 223 223 223 223 + 223 210 210 210 210 210 210 210 210 210 210 210 210 + 210 210 210 210 210 210 210 210 210 280 280 280 280 + 280 280 280 280 280 280 280 280 280 280 280 280 279 + 279 279 279 279 279 279 279 279 279 279 279 279 279 + 279 279 279 279 279 279 279 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 274 274 274 274 274 274 274 274 274 274 274 274 274 + 274 274 274 274 274 274 274 274 212 212 212 212 212 + 212 212 212 212 212 212 212 212 212 212 212 212 212 + 212 212 212 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 232 232 232 + 232 232 232 232 232 232 232 232 232 232 232 232 232 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229} + +do_execsql_test 1.2.9.3 { + SELECT last_value(a+b) OVER ( ORDER BY b,a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276} + +do_execsql_test 1.2.9.4 { + SELECT last_value(a+b) OVER ( PARTITION BY b%10 ORDER BY b,a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {240 240 240 240 240 240 240 240 240 240 240 240 240 + 240 263 263 263 263 263 263 263 263 263 263 263 263 + 263 263 263 263 263 263 263 263 263 280 280 280 280 + 280 280 280 280 280 280 280 280 280 280 280 280 252 + 252 252 252 252 252 252 252 252 252 252 252 252 252 + 252 252 252 252 252 252 252 171 171 171 171 171 171 + 171 171 171 171 171 171 171 171 171 171 171 171 171 + 274 274 274 274 274 274 274 274 274 274 274 274 274 + 274 274 274 274 274 274 274 274 226 226 226 226 226 + 226 226 226 226 226 226 226 226 226 226 226 226 226 + 226 226 226 124 124 124 124 124 124 124 124 124 124 + 124 124 124 124 124 124 124 124 124 124 198 198 198 + 198 198 198 198 198 198 198 198 198 198 198 198 198 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276} + +do_execsql_test 1.2.9.5 { + SELECT last_value(a+b) OVER ( ORDER BY b%10,a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229} + +do_execsql_test 1.2.9.6 { + SELECT last_value(a+b) OVER (PARTITION BY b%2,a ORDER BY b%10 RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM t2 +} {108 52 83 79 65 26 70 103 80 36 116 51 52 128 + 117 71 63 84 109 78 147 88 121 106 124 85 107 171 + 150 80 171 120 109 158 87 168 173 162 156 195 198 + 177 124 121 134 141 210 157 132 161 218 226 191 179 + 138 214 212 172 173 229 240 187 210 227 228 223 225 + 179 182 231 207 209 212 239 234 213 234 269 196 271 + 235 250 223 232 229 280 99 92 72 55 109 120 119 + 50 124 96 59 124 110 57 130 103 74 87 48 105 136 + 131 133 92 109 57 146 113 74 150 87 110 65 110 + 145 161 156 114 111 136 147 173 124 132 101 154 167 + 190 161 110 102 123 169 140 111 180 119 160 197 152 + 146 147 132 213 193 200 136 175 188 187 208 211 144 + 223 196 170 202 163 184 195 200 163 191 252 235 243 + 172 187 202 179 261 263 206 189 276 181 274 249 221 + 210 229 279 224 216 207} + +do_execsql_test 1.2.10.1 { + SELECT nth_value(b,b+1) OVER (ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM t2 +} {96 41 5 16 85 42 43 89 1 22 39 51 73 93 22 80 + 1 73 91 94 35 74 73 37 77 54 54 74 81 91 90 + 62 65 63 36 1 95 23 95 56 16 97 86 40 59 1 84 + 98 56 78 65 96 16 90 81 40 59 56 40 54 2 85 + 96 11 87 41 38 65 32 47 80 74 35 47 98 96 13 + 24 72 73 29 12 46 36 53 35 81 27 56 5 11 81 + 93 63 81 91 68 53 99 89 13 12 97 91 29 7 7 78 + 35 84 53 84 58 61 91 99 15 61 98 16 5 75 56 2 + 37 3 96 62 95 43 63 35 78 16 67 43 16 16 90 + 72 98 85 56 90 46 29 29 4 74 74 2 76 41 46 77 + 24 27 97 46 89 1 85 1 74 78 61 85 51 59 35 30 + 56 25 47 28 73 6 73 74 93 43 3 56 47 85 61 61 + 93 9 97 62} + +do_execsql_test 1.2.10.2 { + SELECT nth_value(b,b+1) OVER (PARTITION BY b%10 ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} 30 {} {} + {} {} {} {} 91 {} {} {} 91 {} 11 {} {} {} {} + 11 {} {} {} {} {} 12 {} {} {} 32 {} 12 {} 32 + {} {} 12 {} {} {} {} {} {} {} 43 {} {} {} {} + {} 33 {} {} {} {} {} 33 {} 43 {} {} {} {} {} + {} 4 {} {} {} {} {} {} {} {} 34 {} {} {} {} + {} {} {} {} {} {} 55 {} {} {} {} {} {} 15 55 + {} {} {} {} {} 55 {} {} {} 86 {} 26 26 {} {} + {} {} 26 {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 47 {} {} {} {} {} 47 {} {} {} {} + {} 27 47 {} {} {} 98 {} {} {} {} {} 98 {} 98 + {} {} {} {} {} {} {} {} {} {} {} {} {} 9 {} + {} {} {} {} 99 {} 9 9 {} {} {}} + +do_execsql_test 1.2.10.3 { + SELECT nth_value(b,b+1) OVER ( ORDER BY b,a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 1 2 2 2 2 3 3 4 5 5 5 6 6 6 7 7 7 7 + 7 7 8 8 8 8 8 8 9 9 9 9 9 9 9 11 11 12 + 12 12 12 12 12 13 13 14 14 15 15 15 15 15 15 + 16 16 16 16 16 16 17 17 17 17 19 19 19 19 20 + 20 21 21 21 21 21 21 22 22 22 22 22 23 23 23 + 24 25 25 26 26 27 27 27 27 27 27 29 29 29 30 + 30 30 31 31 31 31 31 32 33 33 33 33 33 33 33 + 33 33 33 33 34 34 34 34 34 34 34 35 35 36 36 + 36 37 37 37 37 37 37 38 38 38 38 38 38 39 39 + 39 39 39 40 41 41 41 41 41 42 43 43 43 43 43 + 44 44 44 44 46 46 46 47 47 47 47 47 47 47 47 + 47 47 47 49 49 49 50 51 51 51 52 52 52 53 53 + 54 54 55 55} + +do_execsql_test 1.2.10.4 { + SELECT nth_value(b,b+1) OVER ( PARTITION BY b%10 ORDER BY b,a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {80 {} {} {} {} {} {} {} {} {} {} {} {} {} 1 1 + 61 61 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 12 12 72 72 72 {} {} {} {} {} {} + {} {} {} {} {} 13 13 63 63 {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} 34 84 {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + 35 85 85 85 {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} 36 76 76 76 {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} 37 37 37 + 87 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} 58 58 58 {} {} {} {} {} {} {} {} {} {} + {} {} {} 39 39 39 89 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.2.10.5 { + SELECT nth_value(b,b+1) OVER ( ORDER BY b%10,a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {34 2 11 34 81 34 73 33 34 34 11 82 30 11 34 + 65 13 65 65 40 34 72 13 40 41 50 93 72 81 81 + 50 34 65 65 41 73 10 91 61 12 30 54 10 61 30 + 54 54 10 73 23 44 85 1 85 22 80 22 91 93 73 + 84 30 84 84 91 85 1 30 91 80 91 74 43 64 74 + 21 20 85 64 74 21 21 2 74 33 81 21 64 64 2 21 + 93 62 14 14 3 91 11 24 55 93 93 62 90 91 55 3 + 24 14 24 91 55 15 72 60 72 61 61 34 43 43 43 + 61 12 4 15 15 51 51 12 23 12 12 25 41 25 13 + 94 12 70 12 84 32 84 94 70 33 12 12 32 41 91 + 70 22 33 84 80 31 75 84 53 75 80 84 80 53 53 + 53 22 44 63 42 95 31 63 44 44 31 90 74 52 63 + 31 63 1 42 90 90 95 3 42} + +do_execsql_test 1.2.10.6 { + SELECT nth_value(b,b+1) OVER (PARTITION BY b%2,a ORDER BY b%10 RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.2.11.1 { + SELECT first_value(b) OVER (ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM t2 +} {89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89} + +do_execsql_test 1.2.11.2 { + SELECT first_value(b) OVER (PARTITION BY b%10 ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM t2 +} {90 90 90 90 90 90 90 90 90 90 90 90 90 90 81 + 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 + 81 81 81 81 81 62 62 62 62 62 62 62 62 62 62 + 62 62 62 62 62 62 93 93 93 93 93 93 93 93 93 + 93 93 93 93 93 93 93 93 93 93 93 93 54 54 54 + 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 + 54 65 65 65 65 65 65 65 65 65 65 65 65 65 65 + 65 65 65 65 65 65 65 96 96 96 96 96 96 96 96 + 96 96 96 96 96 96 96 96 96 96 96 96 96 97 97 + 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 + 97 97 97 38 38 38 38 38 38 38 38 38 38 38 38 + 38 38 38 38 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89} + +do_execsql_test 1.2.11.3 { + SELECT first_value(b) OVER ( ORDER BY b,a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1} + +do_execsql_test 1.2.11.4 { + SELECT first_value(b) OVER ( PARTITION BY b%10 ORDER BY b,a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {10 10 10 10 10 10 10 10 10 10 10 10 10 10 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 + 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 + 5 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 + 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 + 7 7 7 7 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 + 8 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 + 9 9 9 9} + +do_execsql_test 1.2.11.5 { + SELECT first_value(b) OVER ( ORDER BY b%10,a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90} + +do_execsql_test 1.2.11.6 { + SELECT first_value(b) OVER (PARTITION BY b%2,a ORDER BY b%10 RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM t2 +} {96 38 68 62 46 6 46 78 54 8 84 16 16 86 74 24 + 12 32 56 22 90 28 56 40 56 16 36 98 76 4 94 + 42 30 78 2 80 84 72 58 96 98 74 12 8 20 22 88 + 34 8 34 90 96 60 44 2 74 70 26 26 80 90 36 58 + 72 72 66 64 12 14 62 36 34 36 58 52 30 50 84 + 10 84 44 58 30 38 34 82 89 81 59 39 91 99 97 + 27 97 67 29 93 77 23 93 65 35 47 7 61 91 85 + 85 43 59 3 91 55 15 89 25 47 1 43 75 89 81 33 + 29 53 63 87 37 41 9 61 73 95 65 13 1 21 65 35 + 5 73 11 51 87 41 31 31 15 95 73 79 11 49 59 + 55 75 77 7 85 57 29 59 19 39 47 47 9 33 93 75 + 81 9 23 37 13 91 91 33 15 99 3 95 69 33 21 39 + 83 27 17 7} + +do_execsql_test 1.2.12.1 { + SELECT lead(b,b) OVER (ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM t2 +} {96 9 11 89 32 53 91 30 51 56 54 73 22 59 75 + 74 78 8 16 65 15 8 31 87 90 12 32 96 74 76 37 + 85 90 15 35 2 60 36 75 9 51 47 63 51 90 26 42 + 26 8 76 80 90 37 87 56 79 5 87 8 2 39 73 64 + 36 90 72 78 36 73 51 33 20 41 2 26 37 33 8 14 + 33 81 55 1 9 12 39 64 87 72 34 82 21 34 99 62 + 74 41 69 22 75 27 58 8 79 77 26 26 55 {} 29 + 30 7 {} 66 55 2 34 64 {} 33 {} 44 84 {} {} 95 + 85 19 {} 83 {} 91 {} {} 9 50 91 33 34 {} {} + 84 {} 7 9 {} {} {} 44 {} {} {} {} 91 84 {} 95 + 95 52 {} {} {} {} {} 21 {} {} {} 58 {} {} {} + {} {} {} {} 83 {} {} {} {} {} {} {} {} {} {} + {} {} {} {}} + +do_execsql_test 1.2.12.2 { + SELECT lead(b,b) OVER (PARTITION BY b%10 ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 81 {} {} {} 21 {} {} {} {} {} {} + {} {} {} {} {} {} 62 {} {} {} 12 {} {} {} 72 + {} {} {} {} {} {} {} {} {} {} 53 {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 34 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} 95 {} {} {} {} {} {} 85 {} + {} {} {} {} {} {} {} {} {} 56 {} 36 {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 57 {} {} {} {} {} 7 {} {} {} {} + {} {} {} {} {} {} 8 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 9 {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.2.12.3 { + SELECT lead(b,b) OVER ( ORDER BY b,a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 3 5 6 7 8 8 9 9 10 11 12 12 13 13 + 14 15 16 16 17 19 20 21 22 23 24 25 26 27 27 + 28 29 30 31 32 33 33 33 34 34 35 36 36 36 37 + 38 39 39 40 41 42 43 43 44 46 47 47 47 49 50 + 52 53 54 55 56 56 57 58 58 58 59 59 59 60 61 + 62 62 64 65 65 67 69 70 72 72 73 74 74 75 75 + 75 77 78 80 81 81 83 84 84 85 85 85 87 88 89 + 89 89 90 90 91 91 91 93 93 94 95 95 96 97 97 + 98 99 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.2.12.4 { + SELECT lead(b,b) OVER ( PARTITION BY b%10 ORDER BY b,a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {80 {} {} {} {} {} {} {} {} {} {} {} {} {} 1 + 11 81 81 {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} 12 12 72 82 {} {} {} {} {} {} + {} {} {} {} {} {} 13 23 73 73 {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} 34 84 {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 35 85 85 95 {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} 36 86 96 96 {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 37 47 + 47 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} 58 58 68 {} {} {} {} {} {} {} {} {} + {} {} {} {} 39 49 59 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.2.12.5 { + SELECT lead(b,b) OVER ( ORDER BY b%10,a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {34 72 91 74 21 55 23 94 84 95 2 13 61 22 55 + 55 94 85 75 81 65 73 74 21 72 12 94 33 63 73 + 72 96 36 76 3 25 62 3 73 34 12 46 43 93 72 16 + 86 63 15 65 36 77 24 57 25 53 95 34 95 16 97 + 74 97 67 25 98 44 34 65 54 5 68 96 28 47 95 + 34 39 8 38 6 46 96 28 47 95 56 39 99 97 76 8 + 26 9 79 27 95 16 29 {} 58 58 77 85 56 {} 98 + 29 {} 19 96 {} {} 78 56 98 36 97 {} 89 89 29 + 47 78 {} {} {} 38 68 58 {} 58 38 {} 98 {} {} + {} 39 57 9 {} 79 {} {} 7 {} {} {} 9 29 38 78 + {} {} {} 8 39 {} {} {} {} 59 {} 99 {} {} {} + {} {} {} {} {} {} {} {} {} {} 9 {} {} {} {} + {} {} {} {} {} {} {} {}} + +do_execsql_test 1.2.12.6 { + SELECT lead(b,b) OVER (PARTITION BY b%2,a ORDER BY b%10 RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.2.13.1 { + SELECT lag(b,b) OVER (ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} 38 {} {} {} {} + {} {} {} 6 {} {} {} {} {} 81 46 6 {} {} {} {} + 23 {} {} {} {} {} 27 {} {} {} 35 6 {} 12 {} + 23 {} {} 61 84 {} 93 39 47 {} 54 46 96 56 {} + 16 {} {} {} {} 89 {} 16 43 {} 85 56 29 99 53 + {} 59 {} {} 91 59 53 84 99 {} 93 63 47 {} {} + 98 33 67 35 75 1 23 13 55 27 75 98 35 73 63 2 + 21 27 13 24 86 23 84 31 20 94 61 65 75 23 36 + 94 55 90 41 77 96 56 29 40 12 89 63 11 5 73 + 79 1 16 28 31 73 5 39 53 63 41 11 40 2 13 33 + 9 29 90 47 72 9 73 30 44 33 74 93 29 74 42 34 + 63 41 34 96 47 77 1 36 74 72 14 36 26 77 9 72 + 64 8 91 31 52 30} + +do_execsql_test 1.2.13.2 { + SELECT lag(b,b) OVER (PARTITION BY b%10 ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} 30 {} {} + {} {} {} {} 91 {} {} {} 61 {} 81 {} {} {} {} + 1 {} {} {} {} {} {} {} {} {} 22 {} {} {} 12 + {} {} 62 {} {} {} {} {} {} {} 23 {} {} {} {} + {} {} {} {} {} {} {} 43 {} 23 {} {} {} {} {} + {} 54 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 75 {} + {} {} {} {} {} 55 {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} 47 {} {} {} {} + {} 27 7 {} {} {} {} {} {} {} {} {} 68 {} 8 {} + {} {} {} {} {} {} {} {} {} {} {} {} 89 {} {} + {} {} {} {} {} 29 9 {} {} {}} + +do_execsql_test 1.2.13.3 { + SELECT lag(b,b) OVER ( ORDER BY b,a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {{} 1 1 1 1 2 2 2 2 2 2 3 3 3 4 4 5 6 6 + 6 7 7 7 7 7 8 8 8 8 8 8 9 9 9 9 9 9 9 + 9 9 9 10 10 10 10 11 11 11 11 11 12 12 12 12 + 13 13 13 13 13 14 15 15 15 15 16 16 16 16 16 + 17 19 20 20 21 21 21 21 22 22 22 22 23 23 23 + 23 23 24 23 24 24 25 26 26 26 26 26 26 26 26 + 26 26 26 27 27 27 27 28 29 29 29 29 30 30 30 + 30 30 30 31 31 31 31 31 32 32 32 32 32 32 31 + 32 33 33 33 33 33 33 34 34 34 34 34 34 34 34 + 35 35 35 35 35 36 36 36 36 36 36 36 37 37 37 + 38 38 38 38 38 38 39 39 39 39 40 40 41 41 42 + 43 42 43 43 43 43 44 44 44 46 46 46 47 47 47 + 47 47} + +do_execsql_test 1.2.13.4 { + SELECT lag(b,b) OVER ( PARTITION BY b%10 ORDER BY b,a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + 1 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.2.13.5 { + SELECT lag(b,b) OVER ( ORDER BY b%10,a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} 30 {} {} + {} {} {} {} 91 {} {} {} 61 80 81 {} {} {} {} + 1 {} {} {} 30 {} 21 90 61 {} 22 {} 11 41 12 + {} {} 62 {} {} {} {} 31 {} 50 23 30 21 90 {} + {} 62 {} {} 81 {} 22 43 62 23 32 {} 91 {} 90 + 93 54 {} {} 90 72 12 22 90 81 83 23 80 20 72 + 43 51 33 80 90 2 34 54 1 20 62 12 13 75 44 30 + 93 91 1 21 55 61 61 13 85 3 65 65 91 73 33 93 + 55 84 62 31 11 65 35 85 33 55 15 12 75 22 3 + 73 65 36 85 43 95 43 13 47 44 65 65 96 36 27 + 7 46 34 94 47 36 73 34 35 73 68 24 8 75 85 75 + 66 34 95 36 84 77 46 34 84 47 89 65 36 16 38 + 76 58 57 29 9 44 56 17} + +do_execsql_test 1.2.13.6 { + SELECT lag(b,b) OVER (PARTITION BY b%2,a ORDER BY b%10 RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.2.14.1 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM t2 +} {89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7} + +do_execsql_test 1.2.14.2 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (PARTITION BY b%10 ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM t2 +} {90.40.30.80.20.90.60.70.80.90.30.50.10.30 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39} + +do_execsql_test 1.2.14.3 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( ORDER BY b,a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99} + +do_execsql_test 1.2.14.4 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( PARTITION BY b%10 ORDER BY b,a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {10.20.30.30.30.40.50.60.70.80.80.90.90.90 + 10.20.30.30.30.40.50.60.70.80.80.90.90.90 + 10.20.30.30.30.40.50.60.70.80.80.90.90.90 + 10.20.30.30.30.40.50.60.70.80.80.90.90.90 + 10.20.30.30.30.40.50.60.70.80.80.90.90.90 + 10.20.30.30.30.40.50.60.70.80.80.90.90.90 + 10.20.30.30.30.40.50.60.70.80.80.90.90.90 + 10.20.30.30.30.40.50.60.70.80.80.90.90.90 + 10.20.30.30.30.40.50.60.70.80.80.90.90.90 + 10.20.30.30.30.40.50.60.70.80.80.90.90.90 + 10.20.30.30.30.40.50.60.70.80.80.90.90.90 + 10.20.30.30.30.40.50.60.70.80.80.90.90.90 + 10.20.30.30.30.40.50.60.70.80.80.90.90.90 + 10.20.30.30.30.40.50.60.70.80.80.90.90.90 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99} + +do_execsql_test 1.2.14.5 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( ORDER BY b%10,a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39} + +do_execsql_test 1.2.14.6 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (PARTITION BY b%2,a ORDER BY b%10 RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM t2 +} {96 38 68 62 46 6 46 78 54 8 84 16 16 86 74 24 + 12 32 56 22 90 28 56 40 56 16 36 98 76 4 94 + 42 30 78 2 80 84 72 58 96 98 74 12 8 20 22 88 + 34 8 34 90 96 60 44 2 74 70 26 26 80 90 36 58 + 72 72 66 64 12 14 62 36 34 36 58 52 30 50 84 + 10 84 44 58 30 38 34 82 89 81 59 39 91 99 97 + 27 97 67 29 93 77 23 93 65 35 47 7 61 91 85 + 85 43 59 3 91 55 15 89 25 47 1 43 75 89 81 33 + 29 53 63 87 37 41 9 61 73 95 65 13 1 21 65 35 + 5 73 11 51 87 41 31 31 15 95 73 79 11 49 59 + 55 75 77 7 85 57 29 59 19 39 47 47 9 33 93 75 + 81 9 23 37 13 91 91 33 15 99 3 95 69 33 21 39 + 83 27 17 7} + +do_execsql_test 1.2.14.7 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (win1 ORDER BY b%10 RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a) + ORDER BY 1 +} {1 1 10 11 11 12 12 12 13 13 14 15 15 15 16 16 + 16 17 19 2 2 20 21 21 22 22 23 23 24 25 26 26 + 27 27 28 29 29 29 3 3 30 30 30 31 31 32 33 33 + 33 33 34 34 34 34 35 35 36 36 36 36 37 37 38 + 38 39 39 39 4 40 41 41 42 43 43 44 44 46 46 + 47 47 47 47 49 5 50 51 52 53 54 55 55 56 56 + 56 57 58 58 58 58 59 59 59 59 6 60 61 61 62 + 62 63 64 65 65 65 66 67 68 69 7 7 7 70 72 72 + 72 73 73 73 74 74 74 75 75 75 76 77 77 78 78 + 79 8 8 8 80 80 81 81 81 82 83 84 84 84 84 85 + 85 85 86 87 87 88 89 89 89 9 9 9 90 90 90 91 + 91 91 91 91 93 93 93 94 95 95 95 96 96 96 97 + 97 98 98 99 99} + +do_execsql_test 1.2.14.8 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (win1 RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a ORDER BY b%10) + ORDER BY 1 +} {1 1 10 11 11 12 12 12 13 13 14 15 15 15 16 16 + 16 17 19 2 2 20 21 21 22 22 23 23 24 25 26 26 + 27 27 28 29 29 29 3 3 30 30 30 31 31 32 33 33 + 33 33 34 34 34 34 35 35 36 36 36 36 37 37 38 + 38 39 39 39 4 40 41 41 42 43 43 44 44 46 46 + 47 47 47 47 49 5 50 51 52 53 54 55 55 56 56 + 56 57 58 58 58 58 59 59 59 59 6 60 61 61 62 + 62 63 64 65 65 65 66 67 68 69 7 7 7 70 72 72 + 72 73 73 73 74 74 74 75 75 75 76 77 77 78 78 + 79 8 8 8 80 80 81 81 81 82 83 84 84 84 84 85 + 85 85 86 87 87 88 89 89 89 9 9 9 90 90 90 91 + 91 91 91 91 93 93 93 94 95 95 95 96 96 96 97 + 97 98 98 99 99} + +do_execsql_test 1.2.14.9 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER win2 + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a ORDER BY b%10), + win2 AS (win1 RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) + ORDER BY 1 +} {1 1 10 11 11 12 12 12 13 13 14 15 15 15 16 16 + 16 17 19 2 2 20 21 21 22 22 23 23 24 25 26 26 + 27 27 28 29 29 29 3 3 30 30 30 31 31 32 33 33 + 33 33 34 34 34 34 35 35 36 36 36 36 37 37 38 + 38 39 39 39 4 40 41 41 42 43 43 44 44 46 46 + 47 47 47 47 49 5 50 51 52 53 54 55 55 56 56 + 56 57 58 58 58 58 59 59 59 59 6 60 61 61 62 + 62 63 64 65 65 65 66 67 68 69 7 7 7 70 72 72 + 72 73 73 73 74 74 74 75 75 75 76 77 77 78 78 + 79 8 8 8 80 80 81 81 81 82 83 84 84 84 84 85 + 85 85 86 87 87 88 89 89 89 9 9 9 90 90 90 91 + 91 91 91 91 93 93 93 94 95 95 95 96 96 96 97 + 97 98 98 99 99} + +do_execsql_test 1.2.15.1 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE a%2=0) OVER win FROM t2 + WINDOW win AS (ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) +} {191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7} + +do_execsql_test 1.2.15.2 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE 0=1) OVER win FROM t2 + WINDOW win AS (ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) +} {191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} + 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} + 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} + 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} + 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} + 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} + 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} + 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} + 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} + 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} + 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} + 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} + 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} + 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} + 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} + 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} + 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} + 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} + 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} + 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} + 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} + 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} + 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} + 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {}} + +do_execsql_test 1.2.15.3 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE 1=0) OVER win FROM t2 + WINDOW win AS (PARTITION BY (a%10) ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) +} {20 {} 20 {} 20 {} 20 {} 20 {} 20 {} 20 {} 20 {} 20 {} + 20 {} 20 {} 20 {} 20 {} 20 {} 20 {} 20 {} 20 {} 20 {} + 20 {} 20 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 19 {}} + +do_execsql_test 1.2.15.4 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE a%2=0) OVER win FROM t2 + WINDOW win AS (PARTITION BY (a%10) ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) +} {20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {}} + +do_execsql_test 1.3.2.1 { + SELECT max(b) OVER ( ORDER BY a RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {89 81 96 59 38 68 39 62 91 46 6 99 97 27 46 + 78 54 97 8 67 29 93 84 77 23 16 16 93 65 35 + 47 7 86 74 61 91 85 24 85 43 59 12 32 56 3 91 + 22 90 55 15 28 89 25 47 1 56 40 43 56 16 75 + 36 89 98 76 81 4 94 42 30 78 33 29 53 63 2 87 + 37 80 84 72 41 9 61 73 95 65 13 58 96 98 1 21 + 74 65 35 5 73 11 51 87 41 12 8 20 31 31 15 95 + 22 73 79 88 34 8 11 49 34 90 59 96 60 55 75 + 77 44 2 7 85 57 74 29 70 59 19 39 26 26 47 80 + 90 36 58 47 9 72 72 66 33 93 75 64 81 9 23 37 + 13 12 14 62 91 36 91 33 15 34 36 99 3 95 69 + 58 52 30 50 84 10 84 33 21 39 44 58 30 38 34 + 83 27 82 17 7} + +do_execsql_test 1.3.2.2 { + SELECT min(b) OVER ( ORDER BY a RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {89 81 96 59 38 68 39 62 91 46 6 99 97 27 46 + 78 54 97 8 67 29 93 84 77 23 16 16 93 65 35 + 47 7 86 74 61 91 85 24 85 43 59 12 32 56 3 91 + 22 90 55 15 28 89 25 47 1 56 40 43 56 16 75 + 36 89 98 76 81 4 94 42 30 78 33 29 53 63 2 87 + 37 80 84 72 41 9 61 73 95 65 13 58 96 98 1 21 + 74 65 35 5 73 11 51 87 41 12 8 20 31 31 15 95 + 22 73 79 88 34 8 11 49 34 90 59 96 60 55 75 + 77 44 2 7 85 57 74 29 70 59 19 39 26 26 47 80 + 90 36 58 47 9 72 72 66 33 93 75 64 81 9 23 37 + 13 12 14 62 91 36 91 33 15 34 36 99 3 95 69 + 58 52 30 50 84 10 84 33 21 39 44 58 30 38 34 + 83 27 82 17 7} + +do_execsql_test 1.3.3.1 { + SELECT row_number() OVER ( ORDER BY a RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.3.3.2 { + SELECT row_number() OVER ( PARTITION BY b%10 ORDER BY a RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.3.3.3 { + SELECT row_number() OVER ( RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.3.4.1 { + SELECT dense_rank() OVER ( ORDER BY a RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.3.4.2 { + SELECT dense_rank() OVER ( PARTITION BY b%10 ORDER BY a RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.3.4.3 { + SELECT dense_rank() OVER ( ORDER BY b RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {1 1 2 2 3 3 4 5 6 7 7 7 8 8 8 9 9 9 10 + 11 11 12 12 12 13 13 14 15 15 15 16 16 16 17 + 18 19 20 20 21 21 22 22 23 24 25 25 26 26 27 + 28 28 28 29 29 29 30 30 31 32 32 32 32 33 33 + 33 33 34 34 35 35 35 35 36 36 37 37 38 38 38 + 39 40 40 41 42 42 43 43 44 44 45 45 45 45 46 + 47 48 49 50 51 52 52 53 53 53 54 55 55 55 55 + 56 56 56 56 57 58 58 59 59 60 61 62 62 62 63 + 64 65 66 67 68 68 68 69 69 69 70 70 70 71 71 + 71 72 73 73 74 74 75 76 76 77 77 77 78 79 80 + 80 80 80 81 81 81 82 83 83 84 85 85 85 86 86 + 86 87 87 87 87 87 88 88 88 89 90 90 90 91 91 + 91 92 92 93 93 94 94} + +do_execsql_test 1.3.4.4 { + SELECT dense_rank() OVER ( PARTITION BY b%10 ORDER BY b RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {1 2 3 3 3 4 5 6 7 8 8 9 9 9 1 1 2 2 3 + 3 4 4 5 5 6 7 7 8 8 8 9 9 9 9 9 1 1 2 + 2 2 3 3 4 5 6 7 7 8 8 8 9 1 1 2 2 3 3 + 4 4 4 4 5 5 6 7 8 8 8 9 10 10 10 1 2 3 + 4 4 4 4 5 5 6 7 8 8 8 9 9 9 9 10 1 2 2 + 2 3 4 4 5 5 6 6 6 7 7 7 8 8 8 9 9 9 1 + 2 2 2 3 3 4 4 4 4 5 5 6 6 6 7 8 9 10 10 + 10 1 1 1 2 3 3 4 4 5 5 5 5 6 7 8 8 9 9 + 10 10 1 1 1 2 3 3 4 4 4 4 5 6 6 7 8 8 1 + 1 1 2 3 3 3 4 4 4 5 6 6 6 6 7 8 9 9 9 + 10 10} + +do_execsql_test 1.3.4.5 { + SELECT dense_rank() OVER ( ORDER BY b%10 RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 + 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 + 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 + 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 + 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 + 8 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 10 10 + 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 + 10 10 10 10 10} + +do_execsql_test 1.3.4.6 { + SELECT dense_rank() OVER ( PARTITION BY b%2 ORDER BY b%10 RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 + 5 5 5 5 5 5 5 5 5 5 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 + 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 + 5} + +do_execsql_test 1.3.5.1 { + SELECT rank() OVER ( ORDER BY a RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.3.5.2 { + SELECT rank() OVER ( PARTITION BY b%10 ORDER BY a RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.3.5.3 { + SELECT rank() OVER ( ORDER BY b RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {1 1 3 3 5 5 7 8 9 10 10 10 13 13 13 16 16 + 16 19 20 20 22 22 22 25 25 27 28 28 28 31 31 + 31 34 35 36 37 37 39 39 41 41 43 44 45 45 47 + 47 49 50 50 50 53 53 53 56 56 58 59 59 59 59 + 63 63 63 63 67 67 69 69 69 69 73 73 75 75 77 + 77 77 80 81 81 83 84 84 86 86 88 88 90 90 90 + 90 94 95 96 97 98 99 100 100 102 102 102 105 106 + 106 106 106 110 110 110 110 114 115 115 117 117 119 + 120 121 121 121 124 125 126 127 128 129 129 129 132 + 132 132 135 135 135 138 138 138 141 142 142 144 144 + 146 147 147 149 149 149 152 153 154 154 154 154 158 + 158 158 161 162 162 164 165 165 165 168 168 168 171 + 171 171 171 171 176 176 176 179 180 180 180 183 183 + 183 186 186 188 188 190 190} + +do_execsql_test 1.3.5.4 { + SELECT rank() OVER ( PARTITION BY b%10 ORDER BY b RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {1 2 3 3 3 6 7 8 9 10 10 12 12 12 1 1 3 3 + 5 5 7 7 9 9 11 12 12 14 14 14 17 17 17 17 + 17 1 1 3 3 3 6 6 8 9 10 11 11 13 13 13 16 + 1 1 3 3 5 5 7 7 7 7 11 11 13 14 15 15 15 + 18 19 19 19 1 2 3 4 4 4 4 8 8 10 11 12 12 + 12 15 15 15 15 19 1 2 2 2 5 6 6 8 8 10 10 + 10 13 13 13 16 16 16 19 19 19 1 2 2 2 5 5 7 + 7 7 7 11 11 13 13 13 16 17 18 19 19 19 1 1 + 1 4 5 5 7 7 9 9 9 9 13 14 15 15 17 17 19 + 19 1 1 1 4 5 5 7 7 7 7 11 12 12 14 15 15 + 1 1 1 4 5 5 5 8 8 8 11 12 12 12 12 16 17 + 18 18 18 21 21} + +do_execsql_test 1.3.5.5 { + SELECT rank() OVER ( ORDER BY b%10 RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 15 15 15 15 + 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 + 15 15 36 36 36 36 36 36 36 36 36 36 36 36 36 + 36 36 36 52 52 52 52 52 52 52 52 52 52 52 52 + 52 52 52 52 52 52 52 52 52 73 73 73 73 73 73 + 73 73 73 73 73 73 73 73 73 73 73 73 73 92 92 + 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 + 92 92 92 92 113 113 113 113 113 113 113 113 113 + 113 113 113 113 113 113 113 113 113 113 113 113 134 + 134 134 134 134 134 134 134 134 134 134 134 134 134 + 134 134 134 134 134 134 154 154 154 154 154 154 154 + 154 154 154 154 154 154 154 154 154 170 170 170 170 + 170 170 170 170 170 170 170 170 170 170 170 170 170 + 170 170 170 170 170} + +do_execsql_test 1.3.5.6 { + SELECT rank() OVER ( PARTITION BY b%2 ORDER BY b%10 RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 15 15 15 15 + 15 15 15 15 15 15 15 15 15 15 15 15 31 31 31 + 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 + 31 50 50 50 50 50 50 50 50 50 50 50 50 50 50 + 50 50 50 50 50 50 50 71 71 71 71 71 71 71 71 + 71 71 71 71 71 71 71 71 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 22 22 22 22 22 22 + 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 + 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 + 43 43 43 43 43 43 64 64 64 64 64 64 64 64 64 + 64 64 64 64 64 64 64 64 64 64 64 84 84 84 84 + 84 84 84 84 84 84 84 84 84 84 84 84 84 84 84 + 84 84 84} + +do_execsql_test 1.3.6.1 { + SELECT + row_number() OVER ( PARTITION BY b%2 ORDER BY b%10 RANGE BETWEEN CURRENT ROW AND CURRENT ROW ), + rank() OVER ( PARTITION BY b%2 ORDER BY b%10 RANGE BETWEEN CURRENT ROW AND CURRENT ROW ), + dense_rank() OVER ( PARTITION BY b%2 ORDER BY b%10 RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) + FROM t2 +} {1 1 1 2 1 1 3 1 1 4 1 1 5 1 1 6 1 1 7 1 1 8 1 1 9 1 1 + 10 1 1 11 1 1 12 1 1 13 1 1 14 1 1 15 15 2 16 15 2 17 15 2 + 18 15 2 19 15 2 20 15 2 21 15 2 22 15 2 23 15 2 24 15 2 + 25 15 2 26 15 2 27 15 2 28 15 2 29 15 2 30 15 2 31 31 3 + 32 31 3 33 31 3 34 31 3 35 31 3 36 31 3 37 31 3 38 31 3 + 39 31 3 40 31 3 41 31 3 42 31 3 43 31 3 44 31 3 45 31 3 + 46 31 3 47 31 3 48 31 3 49 31 3 50 50 4 51 50 4 52 50 4 + 53 50 4 54 50 4 55 50 4 56 50 4 57 50 4 58 50 4 59 50 4 + 60 50 4 61 50 4 62 50 4 63 50 4 64 50 4 65 50 4 66 50 4 + 67 50 4 68 50 4 69 50 4 70 50 4 71 71 5 72 71 5 73 71 5 + 74 71 5 75 71 5 76 71 5 77 71 5 78 71 5 79 71 5 80 71 5 + 81 71 5 82 71 5 83 71 5 84 71 5 85 71 5 86 71 5 1 1 1 2 1 1 + 3 1 1 4 1 1 5 1 1 6 1 1 7 1 1 8 1 1 9 1 1 10 1 1 11 1 1 + 12 1 1 13 1 1 14 1 1 15 1 1 16 1 1 17 1 1 18 1 1 19 1 1 + 20 1 1 21 1 1 22 22 2 23 22 2 24 22 2 25 22 2 26 22 2 27 22 2 + 28 22 2 29 22 2 30 22 2 31 22 2 32 22 2 33 22 2 34 22 2 + 35 22 2 36 22 2 37 22 2 38 22 2 39 22 2 40 22 2 41 22 2 + 42 22 2 43 43 3 44 43 3 45 43 3 46 43 3 47 43 3 48 43 3 + 49 43 3 50 43 3 51 43 3 52 43 3 53 43 3 54 43 3 55 43 3 + 56 43 3 57 43 3 58 43 3 59 43 3 60 43 3 61 43 3 62 43 3 + 63 43 3 64 64 4 65 64 4 66 64 4 67 64 4 68 64 4 69 64 4 + 70 64 4 71 64 4 72 64 4 73 64 4 74 64 4 75 64 4 76 64 4 + 77 64 4 78 64 4 79 64 4 80 64 4 81 64 4 82 64 4 83 64 4 + 84 84 5 85 84 5 86 84 5 87 84 5 88 84 5 89 84 5 90 84 5 + 91 84 5 92 84 5 93 84 5 94 84 5 95 84 5 96 84 5 97 84 5 + 98 84 5 99 84 5 100 84 5 101 84 5 102 84 5 103 84 5 104 84 5 + 105 84 5} + + +do_test 1.3.7.1 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY a RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0053 0.0105 0.0158 0.0211 0.0263 0.0316 0.0368 0.0421 0.0474 0.0526 0.0579 0.0632 0.0684 0.0737 0.0789 0.0842 0.0895 0.0947 0.1000 0.1053 0.1105 0.1158 0.1211 0.1263 0.1316 0.1368 0.1421 0.1474 0.1526 0.1579 0.1632 0.1684 0.1737 0.1789 0.1842 0.1895 0.1947 0.2000 0.2053 0.2105 0.2158 0.2211 0.2263 0.2316 0.2368 0.2421 0.2474 0.2526 0.2579 0.2632 0.2684 0.2737 0.2789 0.2842 0.2895 0.2947 0.3000 0.3053 0.3105 0.3158 0.3211 0.3263 0.3316 0.3368 0.3421 0.3474 0.3526 0.3579 0.3632 0.3684 0.3737 0.3789 0.3842 0.3895 0.3947 0.4000 0.4053 0.4105 0.4158 0.4211 0.4263 0.4316 0.4368 0.4421 0.4474 0.4526 0.4579 0.4632 0.4684 0.4737 0.4789 0.4842 0.4895 0.4947 0.5000 0.5053 0.5105 0.5158 0.5211 0.5263 0.5316 0.5368 0.5421 0.5474 0.5526 0.5579 0.5632 0.5684 0.5737 0.5789 0.5842 0.5895 0.5947 0.6000 0.6053 0.6105 0.6158 0.6211 0.6263 0.6316 0.6368 0.6421 0.6474 0.6526 0.6579 0.6632 0.6684 0.6737 0.6789 0.6842 0.6895 0.6947 0.7000 0.7053 0.7105 0.7158 0.7211 0.7263 0.7316 0.7368 0.7421 0.7474 0.7526 0.7579 0.7632 0.7684 0.7737 0.7789 0.7842 0.7895 0.7947 0.8000 0.8053 0.8105 0.8158 0.8211 0.8263 0.8316 0.8368 0.8421 0.8474 0.8526 0.8579 0.8632 0.8684 0.8737 0.8789 0.8842 0.8895 0.8947 0.9000 0.9053 0.9105 0.9158 0.9211 0.9263 0.9316 0.9368 0.9421 0.9474 0.9526 0.9579 0.9632 0.9684 0.9737 0.9789 0.9842 0.9895 0.9947 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.3.7.2 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( PARTITION BY b%10 ORDER BY a RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0769 0.1538 0.2308 0.3077 0.3846 0.4615 0.5385 0.6154 0.6923 0.7692 0.8462 0.9231 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0667 0.1333 0.2000 0.2667 0.3333 0.4000 0.4667 0.5333 0.6000 0.6667 0.7333 0.8000 0.8667 0.9333 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0556 0.1111 0.1667 0.2222 0.2778 0.3333 0.3889 0.4444 0.5000 0.5556 0.6111 0.6667 0.7222 0.7778 0.8333 0.8889 0.9444 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0526 0.1053 0.1579 0.2105 0.2632 0.3158 0.3684 0.4211 0.4737 0.5263 0.5789 0.6316 0.6842 0.7368 0.7895 0.8421 0.8947 0.9474 1.0000 0.0000 0.0667 0.1333 0.2000 0.2667 0.3333 0.4000 0.4667 0.5333 0.6000 0.6667 0.7333 0.8000 0.8667 0.9333 1.0000 0.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.3.7.3 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY b RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0105 0.0105 0.0211 0.0211 0.0316 0.0368 0.0421 0.0474 0.0474 0.0474 0.0632 0.0632 0.0632 0.0789 0.0789 0.0789 0.0947 0.1000 0.1000 0.1105 0.1105 0.1105 0.1263 0.1263 0.1368 0.1421 0.1421 0.1421 0.1579 0.1579 0.1579 0.1737 0.1789 0.1842 0.1895 0.1895 0.2000 0.2000 0.2105 0.2105 0.2211 0.2263 0.2316 0.2316 0.2421 0.2421 0.2526 0.2579 0.2579 0.2579 0.2737 0.2737 0.2737 0.2895 0.2895 0.3000 0.3053 0.3053 0.3053 0.3053 0.3263 0.3263 0.3263 0.3263 0.3474 0.3474 0.3579 0.3579 0.3579 0.3579 0.3789 0.3789 0.3895 0.3895 0.4000 0.4000 0.4000 0.4158 0.4211 0.4211 0.4316 0.4368 0.4368 0.4474 0.4474 0.4579 0.4579 0.4684 0.4684 0.4684 0.4684 0.4895 0.4947 0.5000 0.5053 0.5105 0.5158 0.5211 0.5211 0.5316 0.5316 0.5316 0.5474 0.5526 0.5526 0.5526 0.5526 0.5737 0.5737 0.5737 0.5737 0.5947 0.6000 0.6000 0.6105 0.6105 0.6211 0.6263 0.6316 0.6316 0.6316 0.6474 0.6526 0.6579 0.6632 0.6684 0.6737 0.6737 0.6737 0.6895 0.6895 0.6895 0.7053 0.7053 0.7053 0.7211 0.7211 0.7211 0.7368 0.7421 0.7421 0.7526 0.7526 0.7632 0.7684 0.7684 0.7789 0.7789 0.7789 0.7947 0.8000 0.8053 0.8053 0.8053 0.8053 0.8263 0.8263 0.8263 0.8421 0.8474 0.8474 0.8579 0.8632 0.8632 0.8632 0.8789 0.8789 0.8789 0.8947 0.8947 0.8947 0.8947 0.8947 0.9211 0.9211 0.9211 0.9368 0.9421 0.9421 0.9421 0.9579 0.9579 0.9579 0.9737 0.9737 0.9842 0.9842 0.9947 0.9947} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.3.7.4 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( PARTITION BY b%10 ORDER BY b RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0769 0.1538 0.1538 0.1538 0.3846 0.4615 0.5385 0.6154 0.6923 0.6923 0.8462 0.8462 0.8462 0.0000 0.0000 0.1000 0.1000 0.2000 0.2000 0.3000 0.3000 0.4000 0.4000 0.5000 0.5500 0.5500 0.6500 0.6500 0.6500 0.8000 0.8000 0.8000 0.8000 0.8000 0.0000 0.0000 0.1333 0.1333 0.1333 0.3333 0.3333 0.4667 0.5333 0.6000 0.6667 0.6667 0.8000 0.8000 0.8000 1.0000 0.0000 0.0000 0.1000 0.1000 0.2000 0.2000 0.3000 0.3000 0.3000 0.3000 0.5000 0.5000 0.6000 0.6500 0.7000 0.7000 0.7000 0.8500 0.9000 0.9000 0.9000 0.0000 0.0556 0.1111 0.1667 0.1667 0.1667 0.1667 0.3889 0.3889 0.5000 0.5556 0.6111 0.6111 0.6111 0.7778 0.7778 0.7778 0.7778 1.0000 0.0000 0.0500 0.0500 0.0500 0.2000 0.2500 0.2500 0.3500 0.3500 0.4500 0.4500 0.4500 0.6000 0.6000 0.6000 0.7500 0.7500 0.7500 0.9000 0.9000 0.9000 0.0000 0.0500 0.0500 0.0500 0.2000 0.2000 0.3000 0.3000 0.3000 0.3000 0.5000 0.5000 0.6000 0.6000 0.6000 0.7500 0.8000 0.8500 0.9000 0.9000 0.9000 0.0000 0.0000 0.0000 0.1579 0.2105 0.2105 0.3158 0.3158 0.4211 0.4211 0.4211 0.4211 0.6316 0.6842 0.7368 0.7368 0.8421 0.8421 0.9474 0.9474 0.0000 0.0000 0.0000 0.2000 0.2667 0.2667 0.4000 0.4000 0.4000 0.4000 0.6667 0.7333 0.7333 0.8667 0.9333 0.9333 0.0000 0.0000 0.0000 0.1429 0.1905 0.1905 0.1905 0.3333 0.3333 0.3333 0.4762 0.5238 0.5238 0.5238 0.5238 0.7143 0.7619 0.8095 0.8095 0.8095 0.9524 0.9524} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.3.7.5 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY b%10 RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.3.7.6 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER (PARTITION BY b%2 ORDER BY b%10 RANGE BETWEEN CURRENT ROW AND CURRENT ROW) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.3.8.1 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY a RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0052 0.0105 0.0157 0.0209 0.0262 0.0314 0.0366 0.0419 0.0471 0.0524 0.0576 0.0628 0.0681 0.0733 0.0785 0.0838 0.0890 0.0942 0.0995 0.1047 0.1099 0.1152 0.1204 0.1257 0.1309 0.1361 0.1414 0.1466 0.1518 0.1571 0.1623 0.1675 0.1728 0.1780 0.1832 0.1885 0.1937 0.1990 0.2042 0.2094 0.2147 0.2199 0.2251 0.2304 0.2356 0.2408 0.2461 0.2513 0.2565 0.2618 0.2670 0.2723 0.2775 0.2827 0.2880 0.2932 0.2984 0.3037 0.3089 0.3141 0.3194 0.3246 0.3298 0.3351 0.3403 0.3455 0.3508 0.3560 0.3613 0.3665 0.3717 0.3770 0.3822 0.3874 0.3927 0.3979 0.4031 0.4084 0.4136 0.4188 0.4241 0.4293 0.4346 0.4398 0.4450 0.4503 0.4555 0.4607 0.4660 0.4712 0.4764 0.4817 0.4869 0.4921 0.4974 0.5026 0.5079 0.5131 0.5183 0.5236 0.5288 0.5340 0.5393 0.5445 0.5497 0.5550 0.5602 0.5654 0.5707 0.5759 0.5812 0.5864 0.5916 0.5969 0.6021 0.6073 0.6126 0.6178 0.6230 0.6283 0.6335 0.6387 0.6440 0.6492 0.6545 0.6597 0.6649 0.6702 0.6754 0.6806 0.6859 0.6911 0.6963 0.7016 0.7068 0.7120 0.7173 0.7225 0.7277 0.7330 0.7382 0.7435 0.7487 0.7539 0.7592 0.7644 0.7696 0.7749 0.7801 0.7853 0.7906 0.7958 0.8010 0.8063 0.8115 0.8168 0.8220 0.8272 0.8325 0.8377 0.8429 0.8482 0.8534 0.8586 0.8639 0.8691 0.8743 0.8796 0.8848 0.8901 0.8953 0.9005 0.9058 0.9110 0.9162 0.9215 0.9267 0.9319 0.9372 0.9424 0.9476 0.9529 0.9581 0.9634 0.9686 0.9738 0.9791 0.9843 0.9895 0.9948 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.3.8.2 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%10 ORDER BY a RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0714 0.1429 0.2143 0.2857 0.3571 0.4286 0.5000 0.5714 0.6429 0.7143 0.7857 0.8571 0.9286 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0625 0.1250 0.1875 0.2500 0.3125 0.3750 0.4375 0.5000 0.5625 0.6250 0.6875 0.7500 0.8125 0.8750 0.9375 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0526 0.1053 0.1579 0.2105 0.2632 0.3158 0.3684 0.4211 0.4737 0.5263 0.5789 0.6316 0.6842 0.7368 0.7895 0.8421 0.8947 0.9474 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0625 0.1250 0.1875 0.2500 0.3125 0.3750 0.4375 0.5000 0.5625 0.6250 0.6875 0.7500 0.8125 0.8750 0.9375 1.0000 0.0455 0.0909 0.1364 0.1818 0.2273 0.2727 0.3182 0.3636 0.4091 0.4545 0.5000 0.5455 0.5909 0.6364 0.6818 0.7273 0.7727 0.8182 0.8636 0.9091 0.9545 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.3.8.3 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY b RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0105 0.0105 0.0209 0.0209 0.0314 0.0314 0.0366 0.0419 0.0471 0.0628 0.0628 0.0628 0.0785 0.0785 0.0785 0.0942 0.0942 0.0942 0.0995 0.1099 0.1099 0.1257 0.1257 0.1257 0.1361 0.1361 0.1414 0.1571 0.1571 0.1571 0.1728 0.1728 0.1728 0.1780 0.1832 0.1885 0.1990 0.1990 0.2094 0.2094 0.2199 0.2199 0.2251 0.2304 0.2408 0.2408 0.2513 0.2513 0.2565 0.2723 0.2723 0.2723 0.2880 0.2880 0.2880 0.2984 0.2984 0.3037 0.3246 0.3246 0.3246 0.3246 0.3455 0.3455 0.3455 0.3455 0.3560 0.3560 0.3770 0.3770 0.3770 0.3770 0.3874 0.3874 0.3979 0.3979 0.4136 0.4136 0.4136 0.4188 0.4293 0.4293 0.4346 0.4450 0.4450 0.4555 0.4555 0.4660 0.4660 0.4869 0.4869 0.4869 0.4869 0.4921 0.4974 0.5026 0.5079 0.5131 0.5183 0.5288 0.5288 0.5445 0.5445 0.5445 0.5497 0.5707 0.5707 0.5707 0.5707 0.5916 0.5916 0.5916 0.5916 0.5969 0.6073 0.6073 0.6178 0.6178 0.6230 0.6283 0.6440 0.6440 0.6440 0.6492 0.6545 0.6597 0.6649 0.6702 0.6859 0.6859 0.6859 0.7016 0.7016 0.7016 0.7173 0.7173 0.7173 0.7330 0.7330 0.7330 0.7382 0.7487 0.7487 0.7592 0.7592 0.7644 0.7749 0.7749 0.7906 0.7906 0.7906 0.7958 0.8010 0.8220 0.8220 0.8220 0.8220 0.8377 0.8377 0.8377 0.8429 0.8534 0.8534 0.8586 0.8743 0.8743 0.8743 0.8901 0.8901 0.8901 0.9162 0.9162 0.9162 0.9162 0.9162 0.9319 0.9319 0.9319 0.9372 0.9529 0.9529 0.9529 0.9686 0.9686 0.9686 0.9791 0.9791 0.9895 0.9895 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.3.8.4 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%10 ORDER BY b RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0714 0.1429 0.3571 0.3571 0.3571 0.4286 0.5000 0.5714 0.6429 0.7857 0.7857 1.0000 1.0000 1.0000 0.0952 0.0952 0.1905 0.1905 0.2857 0.2857 0.3810 0.3810 0.4762 0.4762 0.5238 0.6190 0.6190 0.7619 0.7619 0.7619 1.0000 1.0000 1.0000 1.0000 1.0000 0.1250 0.1250 0.3125 0.3125 0.3125 0.4375 0.4375 0.5000 0.5625 0.6250 0.7500 0.7500 0.9375 0.9375 0.9375 1.0000 0.0952 0.0952 0.1905 0.1905 0.2857 0.2857 0.4762 0.4762 0.4762 0.4762 0.5714 0.5714 0.6190 0.6667 0.8095 0.8095 0.8095 0.8571 1.0000 1.0000 1.0000 0.0526 0.1053 0.1579 0.3684 0.3684 0.3684 0.3684 0.4737 0.4737 0.5263 0.5789 0.7368 0.7368 0.7368 0.9474 0.9474 0.9474 0.9474 1.0000 0.0476 0.1905 0.1905 0.1905 0.2381 0.3333 0.3333 0.4286 0.4286 0.5714 0.5714 0.5714 0.7143 0.7143 0.7143 0.8571 0.8571 0.8571 1.0000 1.0000 1.0000 0.0476 0.1905 0.1905 0.1905 0.2857 0.2857 0.4762 0.4762 0.4762 0.4762 0.5714 0.5714 0.7143 0.7143 0.7143 0.7619 0.8095 0.8571 1.0000 1.0000 1.0000 0.1500 0.1500 0.1500 0.2000 0.3000 0.3000 0.4000 0.4000 0.6000 0.6000 0.6000 0.6000 0.6500 0.7000 0.8000 0.8000 0.9000 0.9000 1.0000 1.0000 0.1875 0.1875 0.1875 0.2500 0.3750 0.3750 0.6250 0.6250 0.6250 0.6250 0.6875 0.8125 0.8125 0.8750 1.0000 1.0000 0.1364 0.1364 0.1364 0.1818 0.3182 0.3182 0.3182 0.4545 0.4545 0.4545 0.5000 0.6818 0.6818 0.6818 0.6818 0.7273 0.7727 0.9091 0.9091 0.9091 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.3.8.5 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY b%10 RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.3.8.6 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%2 ORDER BY b%10 RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.3.8.1 { + set myres {} + foreach r [db eval {SELECT ntile(100) OVER ( ORDER BY a RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 88.0000 89.0000 89.0000 90.0000 90.0000 91.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.3.8.2 { + set myres {} + foreach r [db eval {SELECT ntile(101) OVER ( PARTITION BY b%10 ORDER BY a RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 22.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.3.8.3 { + set myres {} + foreach r [db eval {SELECT ntile(102) OVER ( ORDER BY b,a RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 88.0000 89.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.3.8.4 { + set myres {} + foreach r [db eval {SELECT ntile(103) OVER ( PARTITION BY b%10 ORDER BY b,a RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 22.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.3.8.5 { + set myres {} + foreach r [db eval {SELECT ntile(104) OVER ( ORDER BY b%10,a RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000 103.0000 104.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.3.8.6 { + set myres {} + foreach r [db eval {SELECT ntile(105) OVER (PARTITION BY b%2,a ORDER BY b%10 RANGE BETWEEN CURRENT ROW AND CURRENT ROW) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.3.8.7 { + set myres {} + foreach r [db eval {SELECT ntile(105) OVER ( RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 88.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000 103.0000 104.0000 105.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + +do_execsql_test 1.3.9.1 { + SELECT last_value(a+b) OVER ( ORDER BY a RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {99 92 108 72 52 83 55 79 109 65 26 120 119 50 + 70 103 80 124 36 96 59 124 116 110 57 51 52 130 + 103 74 87 48 128 117 105 136 131 71 133 92 109 63 + 84 109 57 146 78 147 113 74 88 150 87 110 65 121 + 106 110 124 85 145 107 161 171 150 156 80 171 120 + 109 158 114 111 136 147 87 173 124 168 173 162 132 + 101 154 167 190 161 110 156 195 198 102 123 177 169 + 140 111 180 119 160 197 152 124 121 134 146 147 132 + 213 141 193 200 210 157 132 136 175 161 218 188 226 + 191 187 208 211 179 138 144 223 196 214 170 212 202 + 163 184 172 173 195 229 240 187 210 200 163 227 228 + 223 191 252 235 225 243 172 187 202 179 179 182 231 + 261 207 263 206 189 209 212 276 181 274 249 239 234 + 213 234 269 196 271 221 210 229 235 250 223 232 229 + 279 224 280 216 207} + +do_execsql_test 1.3.9.2 { + SELECT last_value(a+b) OVER ( PARTITION BY b%10 ORDER BY a RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {147 106 109 168 134 218 191 212 229 240 213 234 196 + 223 92 109 105 136 146 65 156 132 154 102 123 119 + 160 152 146 147 136 243 261 263 210 79 63 84 78 + 120 87 162 124 141 138 227 228 179 231 234 280 124 + 57 130 92 57 110 114 136 147 167 110 180 193 191 + 252 187 179 206 181 221 279 80 116 117 71 80 171 + 173 177 157 161 179 214 225 182 209 269 271 235 229 + 103 74 131 133 113 74 87 145 190 161 169 140 111 + 132 213 187 208 223 235 189 274 108 65 26 70 51 + 52 128 109 121 124 85 107 150 195 226 172 173 187 + 223 207 212 119 50 124 96 110 87 48 110 173 124 + 197 211 144 196 195 200 202 224 216 207 52 83 103 + 36 88 171 158 156 198 121 210 132 210 239 250 232 + 99 72 55 120 59 109 150 161 111 101 200 175 188 + 170 202 163 184 163 172 276 249 229} + +do_execsql_test 1.3.9.3 { + SELECT last_value(a+b) OVER ( ORDER BY b,a RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {65 102 87 138 57 181 80 111 26 48 144 207 36 121 + 132 101 163 172 196 119 136 63 124 179 110 179 182 + 74 132 189 51 52 85 216 163 134 123 210 78 141 57 + 187 71 87 172 173 50 224 88 59 111 170 109 213 + 223 146 147 84 114 191 206 221 157 161 209 229 74 + 140 107 187 207 212 124 202 52 232 55 184 229 106 + 132 152 120 92 110 179 235 65 70 87 110 195 200 + 175 234 160 234 136 80 113 187 109 121 124 196 156 + 210 239 250 72 109 188 202 191 105 154 79 231 147 + 225 103 161 169 223 96 83 249 212 162 227 228 167 + 180 193 117 177 214 145 208 235 150 110 211 103 158 + 200 168 229 92 156 243 280 279 116 173 269 271 131 + 133 223 128 173 197 210 99 150 161 147 218 240 109 + 136 146 261 263 124 130 252 171 190 213 274 108 195 + 226 119 124 171 198 120 276} + +do_execsql_test 1.3.9.4 { + SELECT last_value(a+b) OVER ( PARTITION BY b%10 ORDER BY b,a RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {196 134 109 213 223 106 234 191 212 168 229 147 218 + 240 65 102 119 136 123 210 146 147 132 152 160 105 + 154 92 156 243 109 136 146 261 263 87 138 63 124 + 179 78 141 84 120 234 79 231 162 227 228 280 57 + 181 110 179 57 187 114 191 206 221 92 110 136 147 + 167 180 193 279 124 130 252 80 182 71 157 161 209 + 229 179 235 80 225 117 177 214 116 173 269 271 171 + 111 74 132 189 87 74 140 113 187 103 161 169 145 + 208 235 131 133 223 190 213 274 26 51 52 85 172 + 173 107 187 207 212 65 70 109 121 124 223 150 128 + 108 195 226 48 144 207 216 50 224 124 202 87 110 + 195 200 196 96 110 211 173 197 119 124 36 121 132 + 88 52 232 156 210 239 250 83 103 158 210 171 198 + 101 163 172 163 59 111 170 55 184 229 175 72 109 + 188 202 249 200 99 150 161 120 276} + +do_execsql_test 1.3.9.5 { + SELECT last_value(a+b) OVER ( ORDER BY b%10,a RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {147 106 109 168 134 218 191 212 229 240 213 234 196 + 223 92 109 105 136 146 65 156 132 154 102 123 119 + 160 152 146 147 136 243 261 263 210 79 63 84 78 + 120 87 162 124 141 138 227 228 179 231 234 280 124 + 57 130 92 57 110 114 136 147 167 110 180 193 191 + 252 187 179 206 181 221 279 80 116 117 71 80 171 + 173 177 157 161 179 214 225 182 209 269 271 235 229 + 103 74 131 133 113 74 87 145 190 161 169 140 111 + 132 213 187 208 223 235 189 274 108 65 26 70 51 + 52 128 109 121 124 85 107 150 195 226 172 173 187 + 223 207 212 119 50 124 96 110 87 48 110 173 124 + 197 211 144 196 195 200 202 224 216 207 52 83 103 + 36 88 171 158 156 198 121 210 132 210 239 250 232 + 99 72 55 120 59 109 150 161 111 101 200 175 188 + 170 202 163 184 163 172 276 249 229} + +do_execsql_test 1.3.9.6 { + SELECT last_value(a+b) OVER (PARTITION BY b%2,a ORDER BY b%10 RANGE BETWEEN CURRENT ROW AND CURRENT ROW) FROM t2 +} {108 52 83 79 65 26 70 103 80 36 116 51 52 128 + 117 71 63 84 109 78 147 88 121 106 124 85 107 171 + 150 80 171 120 109 158 87 168 173 162 156 195 198 + 177 124 121 134 141 210 157 132 161 218 226 191 179 + 138 214 212 172 173 229 240 187 210 227 228 223 225 + 179 182 231 207 209 212 239 234 213 234 269 196 271 + 235 250 223 232 229 280 99 92 72 55 109 120 119 + 50 124 96 59 124 110 57 130 103 74 87 48 105 136 + 131 133 92 109 57 146 113 74 150 87 110 65 110 + 145 161 156 114 111 136 147 173 124 132 101 154 167 + 190 161 110 102 123 169 140 111 180 119 160 197 152 + 146 147 132 213 193 200 136 175 188 187 208 211 144 + 223 196 170 202 163 184 195 200 163 191 252 235 243 + 172 187 202 179 261 263 206 189 276 181 274 249 221 + 210 229 279 224 216 207} + +do_execsql_test 1.3.10.1 { + SELECT nth_value(b,b+1) OVER (ORDER BY a RANGE BETWEEN CURRENT ROW AND CURRENT ROW) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.3.10.2 { + SELECT nth_value(b,b+1) OVER (PARTITION BY b%10 ORDER BY a RANGE BETWEEN CURRENT ROW AND CURRENT ROW) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.3.10.3 { + SELECT nth_value(b,b+1) OVER ( ORDER BY b,a RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.3.10.4 { + SELECT nth_value(b,b+1) OVER ( PARTITION BY b%10 ORDER BY b,a RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.3.10.5 { + SELECT nth_value(b,b+1) OVER ( ORDER BY b%10,a RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.3.10.6 { + SELECT nth_value(b,b+1) OVER (PARTITION BY b%2,a ORDER BY b%10 RANGE BETWEEN CURRENT ROW AND CURRENT ROW) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.3.11.1 { + SELECT first_value(b) OVER (ORDER BY a RANGE BETWEEN CURRENT ROW AND CURRENT ROW) FROM t2 +} {89 81 96 59 38 68 39 62 91 46 6 99 97 27 46 + 78 54 97 8 67 29 93 84 77 23 16 16 93 65 35 + 47 7 86 74 61 91 85 24 85 43 59 12 32 56 3 91 + 22 90 55 15 28 89 25 47 1 56 40 43 56 16 75 + 36 89 98 76 81 4 94 42 30 78 33 29 53 63 2 87 + 37 80 84 72 41 9 61 73 95 65 13 58 96 98 1 21 + 74 65 35 5 73 11 51 87 41 12 8 20 31 31 15 95 + 22 73 79 88 34 8 11 49 34 90 59 96 60 55 75 + 77 44 2 7 85 57 74 29 70 59 19 39 26 26 47 80 + 90 36 58 47 9 72 72 66 33 93 75 64 81 9 23 37 + 13 12 14 62 91 36 91 33 15 34 36 99 3 95 69 + 58 52 30 50 84 10 84 33 21 39 44 58 30 38 34 + 83 27 82 17 7} + +do_execsql_test 1.3.11.2 { + SELECT first_value(b) OVER (PARTITION BY b%10 ORDER BY a RANGE BETWEEN CURRENT ROW AND CURRENT ROW) FROM t2 +} {90 40 30 80 20 90 60 70 80 90 30 50 10 30 81 + 91 61 91 91 1 81 41 61 1 21 11 51 41 31 31 11 + 81 91 91 21 62 12 32 22 42 2 72 12 22 2 72 72 + 12 62 52 82 93 23 93 43 3 43 33 53 63 73 13 + 73 73 33 93 23 13 33 3 33 83 54 84 74 24 4 94 + 84 74 34 34 44 74 64 14 34 84 84 44 34 65 35 + 85 85 55 15 25 75 95 65 65 35 5 15 95 55 75 + 85 75 15 95 96 46 6 46 16 16 86 56 56 56 16 + 36 76 96 96 26 26 36 66 36 36 97 27 97 67 77 + 47 7 47 87 37 87 77 7 57 47 47 37 27 17 7 38 + 68 78 8 28 98 78 58 98 8 88 8 58 58 58 38 89 + 59 39 99 29 59 89 89 29 9 79 49 59 29 59 19 + 39 9 9 99 69 39} + +do_execsql_test 1.3.11.3 { + SELECT first_value(b) OVER ( ORDER BY b,a RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {1 1 2 2 3 3 4 5 6 7 7 7 8 8 8 9 9 9 10 + 11 11 12 12 12 13 13 14 15 15 15 16 16 16 17 + 19 20 21 21 22 22 23 23 24 25 26 26 27 27 28 + 29 29 29 30 30 30 31 31 32 33 33 33 33 34 34 + 34 34 35 35 36 36 36 36 37 37 38 38 39 39 39 + 40 41 41 42 43 43 44 44 46 46 47 47 47 47 49 + 50 51 52 53 54 55 55 56 56 56 57 58 58 58 58 + 59 59 59 59 60 61 61 62 62 63 64 65 65 65 66 + 67 68 69 70 72 72 72 73 73 73 74 74 74 75 75 + 75 76 77 77 78 78 79 80 80 81 81 81 82 83 84 + 84 84 84 85 85 85 86 87 87 88 89 89 89 90 90 + 90 91 91 91 91 91 93 93 93 94 95 95 95 96 96 + 96 97 97 98 98 99 99} + +do_execsql_test 1.3.11.4 { + SELECT first_value(b) OVER ( PARTITION BY b%10 ORDER BY b,a RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {10 20 30 30 30 40 50 60 70 80 80 90 90 90 1 1 + 11 11 21 21 31 31 41 41 51 61 61 81 81 81 91 + 91 91 91 91 2 2 12 12 12 22 22 32 42 52 62 62 + 72 72 72 82 3 3 13 13 23 23 33 33 33 33 43 43 + 53 63 73 73 73 83 93 93 93 4 14 24 34 34 34 + 34 44 44 54 64 74 74 74 84 84 84 84 94 5 15 + 15 15 25 35 35 55 55 65 65 65 75 75 75 85 85 + 85 95 95 95 6 16 16 16 26 26 36 36 36 36 46 + 46 56 56 56 66 76 86 96 96 96 7 7 7 17 27 27 + 37 37 47 47 47 47 57 67 77 77 87 87 97 97 8 8 + 8 28 38 38 58 58 58 58 68 78 78 88 98 98 9 9 + 9 19 29 29 29 39 39 39 49 59 59 59 59 69 79 + 89 89 89 99 99} + +do_execsql_test 1.3.11.5 { + SELECT first_value(b) OVER ( ORDER BY b%10,a RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {90 40 30 80 20 90 60 70 80 90 30 50 10 30 81 + 91 61 91 91 1 81 41 61 1 21 11 51 41 31 31 11 + 81 91 91 21 62 12 32 22 42 2 72 12 22 2 72 72 + 12 62 52 82 93 23 93 43 3 43 33 53 63 73 13 + 73 73 33 93 23 13 33 3 33 83 54 84 74 24 4 94 + 84 74 34 34 44 74 64 14 34 84 84 44 34 65 35 + 85 85 55 15 25 75 95 65 65 35 5 15 95 55 75 + 85 75 15 95 96 46 6 46 16 16 86 56 56 56 16 + 36 76 96 96 26 26 36 66 36 36 97 27 97 67 77 + 47 7 47 87 37 87 77 7 57 47 47 37 27 17 7 38 + 68 78 8 28 98 78 58 98 8 88 8 58 58 58 38 89 + 59 39 99 29 59 89 89 29 9 79 49 59 29 59 19 + 39 9 9 99 69 39} + +do_execsql_test 1.3.11.6 { + SELECT first_value(b) OVER (PARTITION BY b%2,a ORDER BY b%10 RANGE BETWEEN CURRENT ROW AND CURRENT ROW) FROM t2 +} {96 38 68 62 46 6 46 78 54 8 84 16 16 86 74 24 + 12 32 56 22 90 28 56 40 56 16 36 98 76 4 94 + 42 30 78 2 80 84 72 58 96 98 74 12 8 20 22 88 + 34 8 34 90 96 60 44 2 74 70 26 26 80 90 36 58 + 72 72 66 64 12 14 62 36 34 36 58 52 30 50 84 + 10 84 44 58 30 38 34 82 89 81 59 39 91 99 97 + 27 97 67 29 93 77 23 93 65 35 47 7 61 91 85 + 85 43 59 3 91 55 15 89 25 47 1 43 75 89 81 33 + 29 53 63 87 37 41 9 61 73 95 65 13 1 21 65 35 + 5 73 11 51 87 41 31 31 15 95 73 79 11 49 59 + 55 75 77 7 85 57 29 59 19 39 47 47 9 33 93 75 + 81 9 23 37 13 91 91 33 15 99 3 95 69 33 21 39 + 83 27 17 7} + +do_execsql_test 1.3.12.1 { + SELECT lead(b,b) OVER (ORDER BY a RANGE BETWEEN CURRENT ROW AND CURRENT ROW) FROM t2 +} {96 9 11 89 32 53 91 30 51 56 54 73 22 59 75 + 74 78 8 16 65 15 8 31 87 90 12 32 96 74 76 37 + 85 90 15 35 2 60 36 75 9 51 47 63 51 90 26 42 + 26 8 76 80 90 37 87 56 79 5 87 8 2 39 73 64 + 36 90 72 78 36 73 51 33 20 41 2 26 37 33 8 14 + 33 81 55 1 9 12 39 64 87 72 34 82 21 34 99 62 + 74 41 69 22 75 27 58 8 79 77 26 26 55 {} 29 + 30 7 {} 66 55 2 34 64 {} 33 {} 44 84 {} {} 95 + 85 19 {} 83 {} 91 {} {} 9 50 91 33 34 {} {} + 84 {} 7 9 {} {} {} 44 {} {} {} {} 91 84 {} 95 + 95 52 {} {} {} {} {} 21 {} {} {} 58 {} {} {} + {} {} {} {} 83 {} {} {} {} {} {} {} {} {} {} + {} {} {} {}} + +do_execsql_test 1.3.12.2 { + SELECT lead(b,b) OVER (PARTITION BY b%10 ORDER BY a RANGE BETWEEN CURRENT ROW AND CURRENT ROW) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 81 {} {} {} 21 {} {} {} {} {} {} + {} {} {} {} {} {} 62 {} {} {} 12 {} {} {} 72 + {} {} {} {} {} {} {} {} {} {} 53 {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 34 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} 95 {} {} {} {} {} {} 85 {} + {} {} {} {} {} {} {} {} {} 56 {} 36 {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 57 {} {} {} {} {} 7 {} {} {} {} + {} {} {} {} {} {} 8 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 9 {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.3.12.3 { + SELECT lead(b,b) OVER ( ORDER BY b,a RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {1 2 3 3 5 6 7 8 8 9 9 10 11 12 12 13 13 + 14 15 16 16 17 19 20 21 22 23 24 25 26 27 27 + 28 29 30 31 32 33 33 33 34 34 35 36 36 36 37 + 38 39 39 40 41 42 43 43 44 46 47 47 47 49 50 + 52 53 54 55 56 56 57 58 58 58 59 59 59 60 61 + 62 62 64 65 65 67 69 70 72 72 73 74 74 75 75 + 75 77 78 80 81 81 83 84 84 85 85 85 87 88 89 + 89 89 90 90 91 91 91 93 93 94 95 95 96 97 97 + 98 99 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.3.12.4 { + SELECT lead(b,b) OVER ( PARTITION BY b%10 ORDER BY b,a RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {80 {} {} {} {} {} {} {} {} {} {} {} {} {} 1 + 11 81 81 {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} 12 12 72 82 {} {} {} {} {} {} + {} {} {} {} {} {} 13 23 73 73 {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} 34 84 {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 35 85 85 95 {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} 36 86 96 96 {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 37 47 + 47 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} 58 58 68 {} {} {} {} {} {} {} {} {} + {} {} {} {} 39 49 59 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.3.12.5 { + SELECT lead(b,b) OVER ( ORDER BY b%10,a RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {34 72 91 74 21 55 23 94 84 95 2 13 61 22 55 + 55 94 85 75 81 65 73 74 21 72 12 94 33 63 73 + 72 96 36 76 3 25 62 3 73 34 12 46 43 93 72 16 + 86 63 15 65 36 77 24 57 25 53 95 34 95 16 97 + 74 97 67 25 98 44 34 65 54 5 68 96 28 47 95 + 34 39 8 38 6 46 96 28 47 95 56 39 99 97 76 8 + 26 9 79 27 95 16 29 {} 58 58 77 85 56 {} 98 + 29 {} 19 96 {} {} 78 56 98 36 97 {} 89 89 29 + 47 78 {} {} {} 38 68 58 {} 58 38 {} 98 {} {} + {} 39 57 9 {} 79 {} {} 7 {} {} {} 9 29 38 78 + {} {} {} 8 39 {} {} {} {} 59 {} 99 {} {} {} + {} {} {} {} {} {} {} {} {} {} 9 {} {} {} {} + {} {} {} {} {} {} {} {}} + +do_execsql_test 1.3.12.6 { + SELECT lead(b,b) OVER (PARTITION BY b%2,a ORDER BY b%10 RANGE BETWEEN CURRENT ROW AND CURRENT ROW) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.3.13.1 { + SELECT lag(b,b) OVER (ORDER BY a RANGE BETWEEN CURRENT ROW AND CURRENT ROW) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} 38 {} {} {} {} + {} {} {} 6 {} {} {} {} {} 81 46 6 {} {} {} {} + 23 {} {} {} {} {} 27 {} {} {} 35 6 {} 12 {} + 23 {} {} 61 84 {} 93 39 47 {} 54 46 96 56 {} + 16 {} {} {} {} 89 {} 16 43 {} 85 56 29 99 53 + {} 59 {} {} 91 59 53 84 99 {} 93 63 47 {} {} + 98 33 67 35 75 1 23 13 55 27 75 98 35 73 63 2 + 21 27 13 24 86 23 84 31 20 94 61 65 75 23 36 + 94 55 90 41 77 96 56 29 40 12 89 63 11 5 73 + 79 1 16 28 31 73 5 39 53 63 41 11 40 2 13 33 + 9 29 90 47 72 9 73 30 44 33 74 93 29 74 42 34 + 63 41 34 96 47 77 1 36 74 72 14 36 26 77 9 72 + 64 8 91 31 52 30} + +do_execsql_test 1.3.13.2 { + SELECT lag(b,b) OVER (PARTITION BY b%10 ORDER BY a RANGE BETWEEN CURRENT ROW AND CURRENT ROW) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} 30 {} {} + {} {} {} {} 91 {} {} {} 61 {} 81 {} {} {} {} + 1 {} {} {} {} {} {} {} {} {} 22 {} {} {} 12 + {} {} 62 {} {} {} {} {} {} {} 23 {} {} {} {} + {} {} {} {} {} {} {} 43 {} 23 {} {} {} {} {} + {} 54 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 75 {} + {} {} {} {} {} 55 {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} 47 {} {} {} {} + {} 27 7 {} {} {} {} {} {} {} {} {} 68 {} 8 {} + {} {} {} {} {} {} {} {} {} {} {} {} 89 {} {} + {} {} {} {} {} 29 9 {} {} {}} + +do_execsql_test 1.3.13.3 { + SELECT lag(b,b) OVER ( ORDER BY b,a RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {{} 1 1 1 1 2 2 2 2 2 2 3 3 3 4 4 5 6 6 + 6 7 7 7 7 7 8 8 8 8 8 8 9 9 9 9 9 9 9 + 9 9 9 10 10 10 10 11 11 11 11 11 12 12 12 12 + 13 13 13 13 13 14 15 15 15 15 16 16 16 16 16 + 17 19 20 20 21 21 21 21 22 22 22 22 23 23 23 + 23 23 24 23 24 24 25 26 26 26 26 26 26 26 26 + 26 26 26 27 27 27 27 28 29 29 29 29 30 30 30 + 30 30 30 31 31 31 31 31 32 32 32 32 32 32 31 + 32 33 33 33 33 33 33 34 34 34 34 34 34 34 34 + 35 35 35 35 35 36 36 36 36 36 36 36 37 37 37 + 38 38 38 38 38 38 39 39 39 39 40 40 41 41 42 + 43 42 43 43 43 43 44 44 44 46 46 46 47 47 47 + 47 47} + +do_execsql_test 1.3.13.4 { + SELECT lag(b,b) OVER ( PARTITION BY b%10 ORDER BY b,a RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + 1 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.3.13.5 { + SELECT lag(b,b) OVER ( ORDER BY b%10,a RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} 30 {} {} + {} {} {} {} 91 {} {} {} 61 80 81 {} {} {} {} + 1 {} {} {} 30 {} 21 90 61 {} 22 {} 11 41 12 + {} {} 62 {} {} {} {} 31 {} 50 23 30 21 90 {} + {} 62 {} {} 81 {} 22 43 62 23 32 {} 91 {} 90 + 93 54 {} {} 90 72 12 22 90 81 83 23 80 20 72 + 43 51 33 80 90 2 34 54 1 20 62 12 13 75 44 30 + 93 91 1 21 55 61 61 13 85 3 65 65 91 73 33 93 + 55 84 62 31 11 65 35 85 33 55 15 12 75 22 3 + 73 65 36 85 43 95 43 13 47 44 65 65 96 36 27 + 7 46 34 94 47 36 73 34 35 73 68 24 8 75 85 75 + 66 34 95 36 84 77 46 34 84 47 89 65 36 16 38 + 76 58 57 29 9 44 56 17} + +do_execsql_test 1.3.13.6 { + SELECT lag(b,b) OVER (PARTITION BY b%2,a ORDER BY b%10 RANGE BETWEEN CURRENT ROW AND CURRENT ROW) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.3.14.1 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (ORDER BY a RANGE BETWEEN CURRENT ROW AND CURRENT ROW) FROM t2 +} {89 81 96 59 38 68 39 62 91 46 6 99 97 27 46 + 78 54 97 8 67 29 93 84 77 23 16 16 93 65 35 + 47 7 86 74 61 91 85 24 85 43 59 12 32 56 3 91 + 22 90 55 15 28 89 25 47 1 56 40 43 56 16 75 + 36 89 98 76 81 4 94 42 30 78 33 29 53 63 2 87 + 37 80 84 72 41 9 61 73 95 65 13 58 96 98 1 21 + 74 65 35 5 73 11 51 87 41 12 8 20 31 31 15 95 + 22 73 79 88 34 8 11 49 34 90 59 96 60 55 75 + 77 44 2 7 85 57 74 29 70 59 19 39 26 26 47 80 + 90 36 58 47 9 72 72 66 33 93 75 64 81 9 23 37 + 13 12 14 62 91 36 91 33 15 34 36 99 3 95 69 + 58 52 30 50 84 10 84 33 21 39 44 58 30 38 34 + 83 27 82 17 7} + +do_execsql_test 1.3.14.2 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (PARTITION BY b%10 ORDER BY a RANGE BETWEEN CURRENT ROW AND CURRENT ROW) FROM t2 +} {90 40 30 80 20 90 60 70 80 90 30 50 10 30 81 + 91 61 91 91 1 81 41 61 1 21 11 51 41 31 31 11 + 81 91 91 21 62 12 32 22 42 2 72 12 22 2 72 72 + 12 62 52 82 93 23 93 43 3 43 33 53 63 73 13 + 73 73 33 93 23 13 33 3 33 83 54 84 74 24 4 94 + 84 74 34 34 44 74 64 14 34 84 84 44 34 65 35 + 85 85 55 15 25 75 95 65 65 35 5 15 95 55 75 + 85 75 15 95 96 46 6 46 16 16 86 56 56 56 16 + 36 76 96 96 26 26 36 66 36 36 97 27 97 67 77 + 47 7 47 87 37 87 77 7 57 47 47 37 27 17 7 38 + 68 78 8 28 98 78 58 98 8 88 8 58 58 58 38 89 + 59 39 99 29 59 89 89 29 9 79 49 59 29 59 19 + 39 9 9 99 69 39} + +do_execsql_test 1.3.14.3 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( ORDER BY b,a RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {1 1 2 2 3 3 4 5 6 7 7 7 8 8 8 9 9 9 10 + 11 11 12 12 12 13 13 14 15 15 15 16 16 16 17 + 19 20 21 21 22 22 23 23 24 25 26 26 27 27 28 + 29 29 29 30 30 30 31 31 32 33 33 33 33 34 34 + 34 34 35 35 36 36 36 36 37 37 38 38 39 39 39 + 40 41 41 42 43 43 44 44 46 46 47 47 47 47 49 + 50 51 52 53 54 55 55 56 56 56 57 58 58 58 58 + 59 59 59 59 60 61 61 62 62 63 64 65 65 65 66 + 67 68 69 70 72 72 72 73 73 73 74 74 74 75 75 + 75 76 77 77 78 78 79 80 80 81 81 81 82 83 84 + 84 84 84 85 85 85 86 87 87 88 89 89 89 90 90 + 90 91 91 91 91 91 93 93 93 94 95 95 95 96 96 + 96 97 97 98 98 99 99} + +do_execsql_test 1.3.14.4 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( PARTITION BY b%10 ORDER BY b,a RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {10 20 30 30 30 40 50 60 70 80 80 90 90 90 1 1 + 11 11 21 21 31 31 41 41 51 61 61 81 81 81 91 + 91 91 91 91 2 2 12 12 12 22 22 32 42 52 62 62 + 72 72 72 82 3 3 13 13 23 23 33 33 33 33 43 43 + 53 63 73 73 73 83 93 93 93 4 14 24 34 34 34 + 34 44 44 54 64 74 74 74 84 84 84 84 94 5 15 + 15 15 25 35 35 55 55 65 65 65 75 75 75 85 85 + 85 95 95 95 6 16 16 16 26 26 36 36 36 36 46 + 46 56 56 56 66 76 86 96 96 96 7 7 7 17 27 27 + 37 37 47 47 47 47 57 67 77 77 87 87 97 97 8 8 + 8 28 38 38 58 58 58 58 68 78 78 88 98 98 9 9 + 9 19 29 29 29 39 39 39 49 59 59 59 59 69 79 + 89 89 89 99 99} + +do_execsql_test 1.3.14.5 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( ORDER BY b%10,a RANGE BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {90 40 30 80 20 90 60 70 80 90 30 50 10 30 81 + 91 61 91 91 1 81 41 61 1 21 11 51 41 31 31 11 + 81 91 91 21 62 12 32 22 42 2 72 12 22 2 72 72 + 12 62 52 82 93 23 93 43 3 43 33 53 63 73 13 + 73 73 33 93 23 13 33 3 33 83 54 84 74 24 4 94 + 84 74 34 34 44 74 64 14 34 84 84 44 34 65 35 + 85 85 55 15 25 75 95 65 65 35 5 15 95 55 75 + 85 75 15 95 96 46 6 46 16 16 86 56 56 56 16 + 36 76 96 96 26 26 36 66 36 36 97 27 97 67 77 + 47 7 47 87 37 87 77 7 57 47 47 37 27 17 7 38 + 68 78 8 28 98 78 58 98 8 88 8 58 58 58 38 89 + 59 39 99 29 59 89 89 29 9 79 49 59 29 59 19 + 39 9 9 99 69 39} + +do_execsql_test 1.3.14.6 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (PARTITION BY b%2,a ORDER BY b%10 RANGE BETWEEN CURRENT ROW AND CURRENT ROW) FROM t2 +} {96 38 68 62 46 6 46 78 54 8 84 16 16 86 74 24 + 12 32 56 22 90 28 56 40 56 16 36 98 76 4 94 + 42 30 78 2 80 84 72 58 96 98 74 12 8 20 22 88 + 34 8 34 90 96 60 44 2 74 70 26 26 80 90 36 58 + 72 72 66 64 12 14 62 36 34 36 58 52 30 50 84 + 10 84 44 58 30 38 34 82 89 81 59 39 91 99 97 + 27 97 67 29 93 77 23 93 65 35 47 7 61 91 85 + 85 43 59 3 91 55 15 89 25 47 1 43 75 89 81 33 + 29 53 63 87 37 41 9 61 73 95 65 13 1 21 65 35 + 5 73 11 51 87 41 31 31 15 95 73 79 11 49 59 + 55 75 77 7 85 57 29 59 19 39 47 47 9 33 93 75 + 81 9 23 37 13 91 91 33 15 99 3 95 69 33 21 39 + 83 27 17 7} + +do_execsql_test 1.3.14.7 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (win1 ORDER BY b%10 RANGE BETWEEN CURRENT ROW AND CURRENT ROW) + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a) + ORDER BY 1 +} {1 1 10 11 11 12 12 12 13 13 14 15 15 15 16 16 + 16 17 19 2 2 20 21 21 22 22 23 23 24 25 26 26 + 27 27 28 29 29 29 3 3 30 30 30 31 31 32 33 33 + 33 33 34 34 34 34 35 35 36 36 36 36 37 37 38 + 38 39 39 39 4 40 41 41 42 43 43 44 44 46 46 + 47 47 47 47 49 5 50 51 52 53 54 55 55 56 56 + 56 57 58 58 58 58 59 59 59 59 6 60 61 61 62 + 62 63 64 65 65 65 66 67 68 69 7 7 7 70 72 72 + 72 73 73 73 74 74 74 75 75 75 76 77 77 78 78 + 79 8 8 8 80 80 81 81 81 82 83 84 84 84 84 85 + 85 85 86 87 87 88 89 89 89 9 9 9 90 90 90 91 + 91 91 91 91 93 93 93 94 95 95 95 96 96 96 97 + 97 98 98 99 99} + +do_execsql_test 1.3.14.8 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (win1 RANGE BETWEEN CURRENT ROW AND CURRENT ROW) + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a ORDER BY b%10) + ORDER BY 1 +} {1 1 10 11 11 12 12 12 13 13 14 15 15 15 16 16 + 16 17 19 2 2 20 21 21 22 22 23 23 24 25 26 26 + 27 27 28 29 29 29 3 3 30 30 30 31 31 32 33 33 + 33 33 34 34 34 34 35 35 36 36 36 36 37 37 38 + 38 39 39 39 4 40 41 41 42 43 43 44 44 46 46 + 47 47 47 47 49 5 50 51 52 53 54 55 55 56 56 + 56 57 58 58 58 58 59 59 59 59 6 60 61 61 62 + 62 63 64 65 65 65 66 67 68 69 7 7 7 70 72 72 + 72 73 73 73 74 74 74 75 75 75 76 77 77 78 78 + 79 8 8 8 80 80 81 81 81 82 83 84 84 84 84 85 + 85 85 86 87 87 88 89 89 89 9 9 9 90 90 90 91 + 91 91 91 91 93 93 93 94 95 95 95 96 96 96 97 + 97 98 98 99 99} + +do_execsql_test 1.3.14.9 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER win2 + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a ORDER BY b%10), + win2 AS (win1 RANGE BETWEEN CURRENT ROW AND CURRENT ROW) + ORDER BY 1 +} {1 1 10 11 11 12 12 12 13 13 14 15 15 15 16 16 + 16 17 19 2 2 20 21 21 22 22 23 23 24 25 26 26 + 27 27 28 29 29 29 3 3 30 30 30 31 31 32 33 33 + 33 33 34 34 34 34 35 35 36 36 36 36 37 37 38 + 38 39 39 39 4 40 41 41 42 43 43 44 44 46 46 + 47 47 47 47 49 5 50 51 52 53 54 55 55 56 56 + 56 57 58 58 58 58 59 59 59 59 6 60 61 61 62 + 62 63 64 65 65 65 66 67 68 69 7 7 7 70 72 72 + 72 73 73 73 74 74 74 75 75 75 76 77 77 78 78 + 79 8 8 8 80 80 81 81 81 82 83 84 84 84 84 85 + 85 85 86 87 87 88 89 89 89 9 9 9 90 90 90 91 + 91 91 91 91 93 93 93 94 95 95 95 96 96 96 97 + 97 98 98 99 99} + +do_execsql_test 1.3.15.1 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE a%2=0) OVER win FROM t2 + WINDOW win AS (ORDER BY a RANGE BETWEEN CURRENT ROW AND CURRENT ROW) +} {1 89 1 {} 1 96 1 {} 1 38 1 {} 1 39 1 {} 1 91 1 {} 1 6 + 1 {} 1 97 1 {} 1 46 1 {} 1 54 1 {} 1 8 1 {} 1 29 1 {} + 1 84 1 {} 1 23 1 {} 1 16 1 {} 1 65 1 {} 1 47 1 {} 1 86 + 1 {} 1 61 1 {} 1 85 1 {} 1 85 1 {} 1 59 1 {} 1 32 1 {} + 1 3 1 {} 1 22 1 {} 1 55 1 {} 1 28 1 {} 1 25 1 {} 1 1 + 1 {} 1 40 1 {} 1 56 1 {} 1 75 1 {} 1 89 1 {} 1 76 1 {} + 1 4 1 {} 1 42 1 {} 1 78 1 {} 1 29 1 {} 1 63 1 {} 1 87 + 1 {} 1 80 1 {} 1 72 1 {} 1 9 1 {} 1 73 1 {} 1 65 1 {} + 1 58 1 {} 1 98 1 {} 1 21 1 {} 1 65 1 {} 1 5 1 {} 1 11 + 1 {} 1 87 1 {} 1 12 1 {} 1 20 1 {} 1 31 1 {} 1 95 1 {} + 1 73 1 {} 1 88 1 {} 1 8 1 {} 1 49 1 {} 1 90 1 {} 1 96 + 1 {} 1 55 1 {} 1 77 1 {} 1 2 1 {} 1 85 1 {} 1 74 1 {} + 1 70 1 {} 1 19 1 {} 1 26 1 {} 1 47 1 {} 1 90 1 {} 1 58 + 1 {} 1 9 1 {} 1 72 1 {} 1 33 1 {} 1 75 1 {} 1 81 1 {} + 1 23 1 {} 1 13 1 {} 1 14 1 {} 1 91 1 {} 1 91 1 {} 1 15 + 1 {} 1 36 1 {} 1 3 1 {} 1 69 1 {} 1 52 1 {} 1 50 1 {} + 1 10 1 {} 1 33 1 {} 1 39 1 {} 1 58 1 {} 1 38 1 {} 1 83 + 1 {} 1 82 1 {} 1 7} + +do_execsql_test 1.3.15.2 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE 0=1) OVER win FROM t2 + WINDOW win AS (ORDER BY a RANGE BETWEEN CURRENT ROW AND CURRENT ROW) +} {1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {}} + +do_execsql_test 1.3.15.3 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE 1=0) OVER win FROM t2 + WINDOW win AS (PARTITION BY (a%10) ORDER BY a RANGE BETWEEN CURRENT ROW AND CURRENT ROW) +} {1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {}} + +do_execsql_test 1.3.15.4 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE a%2=0) OVER win FROM t2 + WINDOW win AS (PARTITION BY (a%10) ORDER BY a RANGE BETWEEN CURRENT ROW AND CURRENT ROW) +} {1 89 1 6 1 29 1 47 1 59 1 28 1 75 1 78 1 72 1 98 1 87 + 1 73 1 96 1 74 1 90 1 75 1 91 1 69 1 39 1 7 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 96 1 97 1 84 1 86 1 32 + 1 25 1 89 1 29 1 9 1 21 1 12 1 88 1 55 1 70 1 58 1 81 + 1 91 1 52 1 58 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 38 1 46 1 23 1 61 1 3 1 1 1 76 1 63 1 73 1 65 1 20 + 1 8 1 77 1 19 1 9 1 23 1 15 1 50 1 38 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 39 1 54 1 16 1 85 1 22 1 40 + 1 4 1 87 1 65 1 5 1 31 1 49 1 2 1 26 1 72 1 13 1 36 + 1 10 1 83 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 91 + 1 8 1 65 1 85 1 55 1 56 1 42 1 80 1 58 1 11 1 95 1 90 + 1 85 1 47 1 33 1 14 1 3 1 33 1 82 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {}} + +do_execsql_test 1.4.2.1 { + SELECT max(b) OVER ( ORDER BY a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 95 95 84 84 84 84 84 84 84 84 83 83 + 83 83 83 83 83 83 83 82 82 17 7} + +do_execsql_test 1.4.2.2 { + SELECT min(b) OVER ( ORDER BY a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 7 7 + 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 + 7} + +do_execsql_test 1.4.3.1 { + SELECT row_number() OVER ( ORDER BY a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.4.3.2 { + SELECT row_number() OVER ( PARTITION BY b%10 ORDER BY a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.4.3.3 { + SELECT row_number() OVER ( RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.4.4.1 { + SELECT dense_rank() OVER ( ORDER BY a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.4.4.2 { + SELECT dense_rank() OVER ( PARTITION BY b%10 ORDER BY a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.4.4.3 { + SELECT dense_rank() OVER ( ORDER BY b RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 1 2 2 3 3 4 5 6 7 7 7 8 8 8 9 9 9 10 + 11 11 12 12 12 13 13 14 15 15 15 16 16 16 17 + 18 19 20 20 21 21 22 22 23 24 25 25 26 26 27 + 28 28 28 29 29 29 30 30 31 32 32 32 32 33 33 + 33 33 34 34 35 35 35 35 36 36 37 37 38 38 38 + 39 40 40 41 42 42 43 43 44 44 45 45 45 45 46 + 47 48 49 50 51 52 52 53 53 53 54 55 55 55 55 + 56 56 56 56 57 58 58 59 59 60 61 62 62 62 63 + 64 65 66 67 68 68 68 69 69 69 70 70 70 71 71 + 71 72 73 73 74 74 75 76 76 77 77 77 78 79 80 + 80 80 80 81 81 81 82 83 83 84 85 85 85 86 86 + 86 87 87 87 87 87 88 88 88 89 90 90 90 91 91 + 91 92 92 93 93 94 94} + +do_execsql_test 1.4.4.4 { + SELECT dense_rank() OVER ( PARTITION BY b%10 ORDER BY b RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 3 3 4 5 6 7 8 8 9 9 9 1 1 2 2 3 + 3 4 4 5 5 6 7 7 8 8 8 9 9 9 9 9 1 1 2 + 2 2 3 3 4 5 6 7 7 8 8 8 9 1 1 2 2 3 3 + 4 4 4 4 5 5 6 7 8 8 8 9 10 10 10 1 2 3 + 4 4 4 4 5 5 6 7 8 8 8 9 9 9 9 10 1 2 2 + 2 3 4 4 5 5 6 6 6 7 7 7 8 8 8 9 9 9 1 + 2 2 2 3 3 4 4 4 4 5 5 6 6 6 7 8 9 10 10 + 10 1 1 1 2 3 3 4 4 5 5 5 5 6 7 8 8 9 9 + 10 10 1 1 1 2 3 3 4 4 4 4 5 6 6 7 8 8 1 + 1 1 2 3 3 3 4 4 4 5 6 6 6 6 7 8 9 9 9 + 10 10} + +do_execsql_test 1.4.4.5 { + SELECT dense_rank() OVER ( ORDER BY b%10 RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 + 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 + 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 + 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 + 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 + 8 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 10 10 + 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 + 10 10 10 10 10} + +do_execsql_test 1.4.4.6 { + SELECT dense_rank() OVER ( PARTITION BY b%2 ORDER BY b%10 RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 + 5 5 5 5 5 5 5 5 5 5 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 + 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 + 5} + +do_execsql_test 1.4.5.1 { + SELECT rank() OVER ( ORDER BY a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.4.5.2 { + SELECT rank() OVER ( PARTITION BY b%10 ORDER BY a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.4.5.3 { + SELECT rank() OVER ( ORDER BY b RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 1 3 3 5 5 7 8 9 10 10 10 13 13 13 16 16 + 16 19 20 20 22 22 22 25 25 27 28 28 28 31 31 + 31 34 35 36 37 37 39 39 41 41 43 44 45 45 47 + 47 49 50 50 50 53 53 53 56 56 58 59 59 59 59 + 63 63 63 63 67 67 69 69 69 69 73 73 75 75 77 + 77 77 80 81 81 83 84 84 86 86 88 88 90 90 90 + 90 94 95 96 97 98 99 100 100 102 102 102 105 106 + 106 106 106 110 110 110 110 114 115 115 117 117 119 + 120 121 121 121 124 125 126 127 128 129 129 129 132 + 132 132 135 135 135 138 138 138 141 142 142 144 144 + 146 147 147 149 149 149 152 153 154 154 154 154 158 + 158 158 161 162 162 164 165 165 165 168 168 168 171 + 171 171 171 171 176 176 176 179 180 180 180 183 183 + 183 186 186 188 188 190 190} + +do_execsql_test 1.4.5.4 { + SELECT rank() OVER ( PARTITION BY b%10 ORDER BY b RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 3 3 6 7 8 9 10 10 12 12 12 1 1 3 3 + 5 5 7 7 9 9 11 12 12 14 14 14 17 17 17 17 + 17 1 1 3 3 3 6 6 8 9 10 11 11 13 13 13 16 + 1 1 3 3 5 5 7 7 7 7 11 11 13 14 15 15 15 + 18 19 19 19 1 2 3 4 4 4 4 8 8 10 11 12 12 + 12 15 15 15 15 19 1 2 2 2 5 6 6 8 8 10 10 + 10 13 13 13 16 16 16 19 19 19 1 2 2 2 5 5 7 + 7 7 7 11 11 13 13 13 16 17 18 19 19 19 1 1 + 1 4 5 5 7 7 9 9 9 9 13 14 15 15 17 17 19 + 19 1 1 1 4 5 5 7 7 7 7 11 12 12 14 15 15 + 1 1 1 4 5 5 5 8 8 8 11 12 12 12 12 16 17 + 18 18 18 21 21} + +do_execsql_test 1.4.5.5 { + SELECT rank() OVER ( ORDER BY b%10 RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 15 15 15 15 + 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 + 15 15 36 36 36 36 36 36 36 36 36 36 36 36 36 + 36 36 36 52 52 52 52 52 52 52 52 52 52 52 52 + 52 52 52 52 52 52 52 52 52 73 73 73 73 73 73 + 73 73 73 73 73 73 73 73 73 73 73 73 73 92 92 + 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 + 92 92 92 92 113 113 113 113 113 113 113 113 113 + 113 113 113 113 113 113 113 113 113 113 113 113 134 + 134 134 134 134 134 134 134 134 134 134 134 134 134 + 134 134 134 134 134 134 154 154 154 154 154 154 154 + 154 154 154 154 154 154 154 154 154 170 170 170 170 + 170 170 170 170 170 170 170 170 170 170 170 170 170 + 170 170 170 170 170} + +do_execsql_test 1.4.5.6 { + SELECT rank() OVER ( PARTITION BY b%2 ORDER BY b%10 RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 15 15 15 15 + 15 15 15 15 15 15 15 15 15 15 15 15 31 31 31 + 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 + 31 50 50 50 50 50 50 50 50 50 50 50 50 50 50 + 50 50 50 50 50 50 50 71 71 71 71 71 71 71 71 + 71 71 71 71 71 71 71 71 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 22 22 22 22 22 22 + 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 + 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 + 43 43 43 43 43 43 64 64 64 64 64 64 64 64 64 + 64 64 64 64 64 64 64 64 64 64 64 84 84 84 84 + 84 84 84 84 84 84 84 84 84 84 84 84 84 84 84 + 84 84 84} + +do_execsql_test 1.4.6.1 { + SELECT + row_number() OVER ( PARTITION BY b%2 ORDER BY b%10 RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ), + rank() OVER ( PARTITION BY b%2 ORDER BY b%10 RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ), + dense_rank() OVER ( PARTITION BY b%2 ORDER BY b%10 RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) + FROM t2 +} {1 1 1 2 1 1 3 1 1 4 1 1 5 1 1 6 1 1 7 1 1 8 1 1 9 1 1 + 10 1 1 11 1 1 12 1 1 13 1 1 14 1 1 15 15 2 16 15 2 17 15 2 + 18 15 2 19 15 2 20 15 2 21 15 2 22 15 2 23 15 2 24 15 2 + 25 15 2 26 15 2 27 15 2 28 15 2 29 15 2 30 15 2 31 31 3 + 32 31 3 33 31 3 34 31 3 35 31 3 36 31 3 37 31 3 38 31 3 + 39 31 3 40 31 3 41 31 3 42 31 3 43 31 3 44 31 3 45 31 3 + 46 31 3 47 31 3 48 31 3 49 31 3 50 50 4 51 50 4 52 50 4 + 53 50 4 54 50 4 55 50 4 56 50 4 57 50 4 58 50 4 59 50 4 + 60 50 4 61 50 4 62 50 4 63 50 4 64 50 4 65 50 4 66 50 4 + 67 50 4 68 50 4 69 50 4 70 50 4 71 71 5 72 71 5 73 71 5 + 74 71 5 75 71 5 76 71 5 77 71 5 78 71 5 79 71 5 80 71 5 + 81 71 5 82 71 5 83 71 5 84 71 5 85 71 5 86 71 5 1 1 1 2 1 1 + 3 1 1 4 1 1 5 1 1 6 1 1 7 1 1 8 1 1 9 1 1 10 1 1 11 1 1 + 12 1 1 13 1 1 14 1 1 15 1 1 16 1 1 17 1 1 18 1 1 19 1 1 + 20 1 1 21 1 1 22 22 2 23 22 2 24 22 2 25 22 2 26 22 2 27 22 2 + 28 22 2 29 22 2 30 22 2 31 22 2 32 22 2 33 22 2 34 22 2 + 35 22 2 36 22 2 37 22 2 38 22 2 39 22 2 40 22 2 41 22 2 + 42 22 2 43 43 3 44 43 3 45 43 3 46 43 3 47 43 3 48 43 3 + 49 43 3 50 43 3 51 43 3 52 43 3 53 43 3 54 43 3 55 43 3 + 56 43 3 57 43 3 58 43 3 59 43 3 60 43 3 61 43 3 62 43 3 + 63 43 3 64 64 4 65 64 4 66 64 4 67 64 4 68 64 4 69 64 4 + 70 64 4 71 64 4 72 64 4 73 64 4 74 64 4 75 64 4 76 64 4 + 77 64 4 78 64 4 79 64 4 80 64 4 81 64 4 82 64 4 83 64 4 + 84 84 5 85 84 5 86 84 5 87 84 5 88 84 5 89 84 5 90 84 5 + 91 84 5 92 84 5 93 84 5 94 84 5 95 84 5 96 84 5 97 84 5 + 98 84 5 99 84 5 100 84 5 101 84 5 102 84 5 103 84 5 104 84 5 + 105 84 5} + + +do_test 1.4.7.1 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0053 0.0105 0.0158 0.0211 0.0263 0.0316 0.0368 0.0421 0.0474 0.0526 0.0579 0.0632 0.0684 0.0737 0.0789 0.0842 0.0895 0.0947 0.1000 0.1053 0.1105 0.1158 0.1211 0.1263 0.1316 0.1368 0.1421 0.1474 0.1526 0.1579 0.1632 0.1684 0.1737 0.1789 0.1842 0.1895 0.1947 0.2000 0.2053 0.2105 0.2158 0.2211 0.2263 0.2316 0.2368 0.2421 0.2474 0.2526 0.2579 0.2632 0.2684 0.2737 0.2789 0.2842 0.2895 0.2947 0.3000 0.3053 0.3105 0.3158 0.3211 0.3263 0.3316 0.3368 0.3421 0.3474 0.3526 0.3579 0.3632 0.3684 0.3737 0.3789 0.3842 0.3895 0.3947 0.4000 0.4053 0.4105 0.4158 0.4211 0.4263 0.4316 0.4368 0.4421 0.4474 0.4526 0.4579 0.4632 0.4684 0.4737 0.4789 0.4842 0.4895 0.4947 0.5000 0.5053 0.5105 0.5158 0.5211 0.5263 0.5316 0.5368 0.5421 0.5474 0.5526 0.5579 0.5632 0.5684 0.5737 0.5789 0.5842 0.5895 0.5947 0.6000 0.6053 0.6105 0.6158 0.6211 0.6263 0.6316 0.6368 0.6421 0.6474 0.6526 0.6579 0.6632 0.6684 0.6737 0.6789 0.6842 0.6895 0.6947 0.7000 0.7053 0.7105 0.7158 0.7211 0.7263 0.7316 0.7368 0.7421 0.7474 0.7526 0.7579 0.7632 0.7684 0.7737 0.7789 0.7842 0.7895 0.7947 0.8000 0.8053 0.8105 0.8158 0.8211 0.8263 0.8316 0.8368 0.8421 0.8474 0.8526 0.8579 0.8632 0.8684 0.8737 0.8789 0.8842 0.8895 0.8947 0.9000 0.9053 0.9105 0.9158 0.9211 0.9263 0.9316 0.9368 0.9421 0.9474 0.9526 0.9579 0.9632 0.9684 0.9737 0.9789 0.9842 0.9895 0.9947 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.4.7.2 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( PARTITION BY b%10 ORDER BY a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0769 0.1538 0.2308 0.3077 0.3846 0.4615 0.5385 0.6154 0.6923 0.7692 0.8462 0.9231 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0667 0.1333 0.2000 0.2667 0.3333 0.4000 0.4667 0.5333 0.6000 0.6667 0.7333 0.8000 0.8667 0.9333 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0556 0.1111 0.1667 0.2222 0.2778 0.3333 0.3889 0.4444 0.5000 0.5556 0.6111 0.6667 0.7222 0.7778 0.8333 0.8889 0.9444 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0526 0.1053 0.1579 0.2105 0.2632 0.3158 0.3684 0.4211 0.4737 0.5263 0.5789 0.6316 0.6842 0.7368 0.7895 0.8421 0.8947 0.9474 1.0000 0.0000 0.0667 0.1333 0.2000 0.2667 0.3333 0.4000 0.4667 0.5333 0.6000 0.6667 0.7333 0.8000 0.8667 0.9333 1.0000 0.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.4.7.3 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY b RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0105 0.0105 0.0211 0.0211 0.0316 0.0368 0.0421 0.0474 0.0474 0.0474 0.0632 0.0632 0.0632 0.0789 0.0789 0.0789 0.0947 0.1000 0.1000 0.1105 0.1105 0.1105 0.1263 0.1263 0.1368 0.1421 0.1421 0.1421 0.1579 0.1579 0.1579 0.1737 0.1789 0.1842 0.1895 0.1895 0.2000 0.2000 0.2105 0.2105 0.2211 0.2263 0.2316 0.2316 0.2421 0.2421 0.2526 0.2579 0.2579 0.2579 0.2737 0.2737 0.2737 0.2895 0.2895 0.3000 0.3053 0.3053 0.3053 0.3053 0.3263 0.3263 0.3263 0.3263 0.3474 0.3474 0.3579 0.3579 0.3579 0.3579 0.3789 0.3789 0.3895 0.3895 0.4000 0.4000 0.4000 0.4158 0.4211 0.4211 0.4316 0.4368 0.4368 0.4474 0.4474 0.4579 0.4579 0.4684 0.4684 0.4684 0.4684 0.4895 0.4947 0.5000 0.5053 0.5105 0.5158 0.5211 0.5211 0.5316 0.5316 0.5316 0.5474 0.5526 0.5526 0.5526 0.5526 0.5737 0.5737 0.5737 0.5737 0.5947 0.6000 0.6000 0.6105 0.6105 0.6211 0.6263 0.6316 0.6316 0.6316 0.6474 0.6526 0.6579 0.6632 0.6684 0.6737 0.6737 0.6737 0.6895 0.6895 0.6895 0.7053 0.7053 0.7053 0.7211 0.7211 0.7211 0.7368 0.7421 0.7421 0.7526 0.7526 0.7632 0.7684 0.7684 0.7789 0.7789 0.7789 0.7947 0.8000 0.8053 0.8053 0.8053 0.8053 0.8263 0.8263 0.8263 0.8421 0.8474 0.8474 0.8579 0.8632 0.8632 0.8632 0.8789 0.8789 0.8789 0.8947 0.8947 0.8947 0.8947 0.8947 0.9211 0.9211 0.9211 0.9368 0.9421 0.9421 0.9421 0.9579 0.9579 0.9579 0.9737 0.9737 0.9842 0.9842 0.9947 0.9947} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.4.7.4 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( PARTITION BY b%10 ORDER BY b RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0769 0.1538 0.1538 0.1538 0.3846 0.4615 0.5385 0.6154 0.6923 0.6923 0.8462 0.8462 0.8462 0.0000 0.0000 0.1000 0.1000 0.2000 0.2000 0.3000 0.3000 0.4000 0.4000 0.5000 0.5500 0.5500 0.6500 0.6500 0.6500 0.8000 0.8000 0.8000 0.8000 0.8000 0.0000 0.0000 0.1333 0.1333 0.1333 0.3333 0.3333 0.4667 0.5333 0.6000 0.6667 0.6667 0.8000 0.8000 0.8000 1.0000 0.0000 0.0000 0.1000 0.1000 0.2000 0.2000 0.3000 0.3000 0.3000 0.3000 0.5000 0.5000 0.6000 0.6500 0.7000 0.7000 0.7000 0.8500 0.9000 0.9000 0.9000 0.0000 0.0556 0.1111 0.1667 0.1667 0.1667 0.1667 0.3889 0.3889 0.5000 0.5556 0.6111 0.6111 0.6111 0.7778 0.7778 0.7778 0.7778 1.0000 0.0000 0.0500 0.0500 0.0500 0.2000 0.2500 0.2500 0.3500 0.3500 0.4500 0.4500 0.4500 0.6000 0.6000 0.6000 0.7500 0.7500 0.7500 0.9000 0.9000 0.9000 0.0000 0.0500 0.0500 0.0500 0.2000 0.2000 0.3000 0.3000 0.3000 0.3000 0.5000 0.5000 0.6000 0.6000 0.6000 0.7500 0.8000 0.8500 0.9000 0.9000 0.9000 0.0000 0.0000 0.0000 0.1579 0.2105 0.2105 0.3158 0.3158 0.4211 0.4211 0.4211 0.4211 0.6316 0.6842 0.7368 0.7368 0.8421 0.8421 0.9474 0.9474 0.0000 0.0000 0.0000 0.2000 0.2667 0.2667 0.4000 0.4000 0.4000 0.4000 0.6667 0.7333 0.7333 0.8667 0.9333 0.9333 0.0000 0.0000 0.0000 0.1429 0.1905 0.1905 0.1905 0.3333 0.3333 0.3333 0.4762 0.5238 0.5238 0.5238 0.5238 0.7143 0.7619 0.8095 0.8095 0.8095 0.9524 0.9524} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.4.7.5 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY b%10 RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.4.7.6 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER (PARTITION BY b%2 ORDER BY b%10 RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.4.8.1 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0052 0.0105 0.0157 0.0209 0.0262 0.0314 0.0366 0.0419 0.0471 0.0524 0.0576 0.0628 0.0681 0.0733 0.0785 0.0838 0.0890 0.0942 0.0995 0.1047 0.1099 0.1152 0.1204 0.1257 0.1309 0.1361 0.1414 0.1466 0.1518 0.1571 0.1623 0.1675 0.1728 0.1780 0.1832 0.1885 0.1937 0.1990 0.2042 0.2094 0.2147 0.2199 0.2251 0.2304 0.2356 0.2408 0.2461 0.2513 0.2565 0.2618 0.2670 0.2723 0.2775 0.2827 0.2880 0.2932 0.2984 0.3037 0.3089 0.3141 0.3194 0.3246 0.3298 0.3351 0.3403 0.3455 0.3508 0.3560 0.3613 0.3665 0.3717 0.3770 0.3822 0.3874 0.3927 0.3979 0.4031 0.4084 0.4136 0.4188 0.4241 0.4293 0.4346 0.4398 0.4450 0.4503 0.4555 0.4607 0.4660 0.4712 0.4764 0.4817 0.4869 0.4921 0.4974 0.5026 0.5079 0.5131 0.5183 0.5236 0.5288 0.5340 0.5393 0.5445 0.5497 0.5550 0.5602 0.5654 0.5707 0.5759 0.5812 0.5864 0.5916 0.5969 0.6021 0.6073 0.6126 0.6178 0.6230 0.6283 0.6335 0.6387 0.6440 0.6492 0.6545 0.6597 0.6649 0.6702 0.6754 0.6806 0.6859 0.6911 0.6963 0.7016 0.7068 0.7120 0.7173 0.7225 0.7277 0.7330 0.7382 0.7435 0.7487 0.7539 0.7592 0.7644 0.7696 0.7749 0.7801 0.7853 0.7906 0.7958 0.8010 0.8063 0.8115 0.8168 0.8220 0.8272 0.8325 0.8377 0.8429 0.8482 0.8534 0.8586 0.8639 0.8691 0.8743 0.8796 0.8848 0.8901 0.8953 0.9005 0.9058 0.9110 0.9162 0.9215 0.9267 0.9319 0.9372 0.9424 0.9476 0.9529 0.9581 0.9634 0.9686 0.9738 0.9791 0.9843 0.9895 0.9948 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.4.8.2 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%10 ORDER BY a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0714 0.1429 0.2143 0.2857 0.3571 0.4286 0.5000 0.5714 0.6429 0.7143 0.7857 0.8571 0.9286 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0625 0.1250 0.1875 0.2500 0.3125 0.3750 0.4375 0.5000 0.5625 0.6250 0.6875 0.7500 0.8125 0.8750 0.9375 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0526 0.1053 0.1579 0.2105 0.2632 0.3158 0.3684 0.4211 0.4737 0.5263 0.5789 0.6316 0.6842 0.7368 0.7895 0.8421 0.8947 0.9474 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0625 0.1250 0.1875 0.2500 0.3125 0.3750 0.4375 0.5000 0.5625 0.6250 0.6875 0.7500 0.8125 0.8750 0.9375 1.0000 0.0455 0.0909 0.1364 0.1818 0.2273 0.2727 0.3182 0.3636 0.4091 0.4545 0.5000 0.5455 0.5909 0.6364 0.6818 0.7273 0.7727 0.8182 0.8636 0.9091 0.9545 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.4.8.3 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY b RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0105 0.0105 0.0209 0.0209 0.0314 0.0314 0.0366 0.0419 0.0471 0.0628 0.0628 0.0628 0.0785 0.0785 0.0785 0.0942 0.0942 0.0942 0.0995 0.1099 0.1099 0.1257 0.1257 0.1257 0.1361 0.1361 0.1414 0.1571 0.1571 0.1571 0.1728 0.1728 0.1728 0.1780 0.1832 0.1885 0.1990 0.1990 0.2094 0.2094 0.2199 0.2199 0.2251 0.2304 0.2408 0.2408 0.2513 0.2513 0.2565 0.2723 0.2723 0.2723 0.2880 0.2880 0.2880 0.2984 0.2984 0.3037 0.3246 0.3246 0.3246 0.3246 0.3455 0.3455 0.3455 0.3455 0.3560 0.3560 0.3770 0.3770 0.3770 0.3770 0.3874 0.3874 0.3979 0.3979 0.4136 0.4136 0.4136 0.4188 0.4293 0.4293 0.4346 0.4450 0.4450 0.4555 0.4555 0.4660 0.4660 0.4869 0.4869 0.4869 0.4869 0.4921 0.4974 0.5026 0.5079 0.5131 0.5183 0.5288 0.5288 0.5445 0.5445 0.5445 0.5497 0.5707 0.5707 0.5707 0.5707 0.5916 0.5916 0.5916 0.5916 0.5969 0.6073 0.6073 0.6178 0.6178 0.6230 0.6283 0.6440 0.6440 0.6440 0.6492 0.6545 0.6597 0.6649 0.6702 0.6859 0.6859 0.6859 0.7016 0.7016 0.7016 0.7173 0.7173 0.7173 0.7330 0.7330 0.7330 0.7382 0.7487 0.7487 0.7592 0.7592 0.7644 0.7749 0.7749 0.7906 0.7906 0.7906 0.7958 0.8010 0.8220 0.8220 0.8220 0.8220 0.8377 0.8377 0.8377 0.8429 0.8534 0.8534 0.8586 0.8743 0.8743 0.8743 0.8901 0.8901 0.8901 0.9162 0.9162 0.9162 0.9162 0.9162 0.9319 0.9319 0.9319 0.9372 0.9529 0.9529 0.9529 0.9686 0.9686 0.9686 0.9791 0.9791 0.9895 0.9895 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.4.8.4 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%10 ORDER BY b RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0714 0.1429 0.3571 0.3571 0.3571 0.4286 0.5000 0.5714 0.6429 0.7857 0.7857 1.0000 1.0000 1.0000 0.0952 0.0952 0.1905 0.1905 0.2857 0.2857 0.3810 0.3810 0.4762 0.4762 0.5238 0.6190 0.6190 0.7619 0.7619 0.7619 1.0000 1.0000 1.0000 1.0000 1.0000 0.1250 0.1250 0.3125 0.3125 0.3125 0.4375 0.4375 0.5000 0.5625 0.6250 0.7500 0.7500 0.9375 0.9375 0.9375 1.0000 0.0952 0.0952 0.1905 0.1905 0.2857 0.2857 0.4762 0.4762 0.4762 0.4762 0.5714 0.5714 0.6190 0.6667 0.8095 0.8095 0.8095 0.8571 1.0000 1.0000 1.0000 0.0526 0.1053 0.1579 0.3684 0.3684 0.3684 0.3684 0.4737 0.4737 0.5263 0.5789 0.7368 0.7368 0.7368 0.9474 0.9474 0.9474 0.9474 1.0000 0.0476 0.1905 0.1905 0.1905 0.2381 0.3333 0.3333 0.4286 0.4286 0.5714 0.5714 0.5714 0.7143 0.7143 0.7143 0.8571 0.8571 0.8571 1.0000 1.0000 1.0000 0.0476 0.1905 0.1905 0.1905 0.2857 0.2857 0.4762 0.4762 0.4762 0.4762 0.5714 0.5714 0.7143 0.7143 0.7143 0.7619 0.8095 0.8571 1.0000 1.0000 1.0000 0.1500 0.1500 0.1500 0.2000 0.3000 0.3000 0.4000 0.4000 0.6000 0.6000 0.6000 0.6000 0.6500 0.7000 0.8000 0.8000 0.9000 0.9000 1.0000 1.0000 0.1875 0.1875 0.1875 0.2500 0.3750 0.3750 0.6250 0.6250 0.6250 0.6250 0.6875 0.8125 0.8125 0.8750 1.0000 1.0000 0.1364 0.1364 0.1364 0.1818 0.3182 0.3182 0.3182 0.4545 0.4545 0.4545 0.5000 0.6818 0.6818 0.6818 0.6818 0.7273 0.7727 0.9091 0.9091 0.9091 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.4.8.5 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY b%10 RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.4.8.6 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%2 ORDER BY b%10 RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.4.8.1 { + set myres {} + foreach r [db eval {SELECT ntile(100) OVER ( ORDER BY a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 88.0000 89.0000 89.0000 90.0000 90.0000 91.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.4.8.2 { + set myres {} + foreach r [db eval {SELECT ntile(101) OVER ( PARTITION BY b%10 ORDER BY a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 22.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.4.8.3 { + set myres {} + foreach r [db eval {SELECT ntile(102) OVER ( ORDER BY b,a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 88.0000 89.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.4.8.4 { + set myres {} + foreach r [db eval {SELECT ntile(103) OVER ( PARTITION BY b%10 ORDER BY b,a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 22.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.4.8.5 { + set myres {} + foreach r [db eval {SELECT ntile(104) OVER ( ORDER BY b%10,a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000 103.0000 104.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.4.8.6 { + set myres {} + foreach r [db eval {SELECT ntile(105) OVER (PARTITION BY b%2,a ORDER BY b%10 RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.4.8.7 { + set myres {} + foreach r [db eval {SELECT ntile(105) OVER ( RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 88.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000 103.0000 104.0000 105.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + +do_execsql_test 1.4.9.1 { + SELECT last_value(a+b) OVER ( ORDER BY a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207} + +do_execsql_test 1.4.9.2 { + SELECT last_value(a+b) OVER ( PARTITION BY b%10 ORDER BY a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {223 223 223 223 223 223 223 223 223 223 223 223 223 + 223 210 210 210 210 210 210 210 210 210 210 210 210 + 210 210 210 210 210 210 210 210 210 280 280 280 280 + 280 280 280 280 280 280 280 280 280 280 280 280 279 + 279 279 279 279 279 279 279 279 279 279 279 279 279 + 279 279 279 279 279 279 279 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 274 274 274 274 274 274 274 274 274 274 274 274 274 + 274 274 274 274 274 274 274 274 212 212 212 212 212 + 212 212 212 212 212 212 212 212 212 212 212 212 212 + 212 212 212 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 232 232 232 + 232 232 232 232 232 232 232 232 232 232 232 232 232 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229} + +do_execsql_test 1.4.9.3 { + SELECT last_value(a+b) OVER ( ORDER BY b,a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276} + +do_execsql_test 1.4.9.4 { + SELECT last_value(a+b) OVER ( PARTITION BY b%10 ORDER BY b,a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {240 240 240 240 240 240 240 240 240 240 240 240 240 + 240 263 263 263 263 263 263 263 263 263 263 263 263 + 263 263 263 263 263 263 263 263 263 280 280 280 280 + 280 280 280 280 280 280 280 280 280 280 280 280 252 + 252 252 252 252 252 252 252 252 252 252 252 252 252 + 252 252 252 252 252 252 252 171 171 171 171 171 171 + 171 171 171 171 171 171 171 171 171 171 171 171 171 + 274 274 274 274 274 274 274 274 274 274 274 274 274 + 274 274 274 274 274 274 274 274 226 226 226 226 226 + 226 226 226 226 226 226 226 226 226 226 226 226 226 + 226 226 226 124 124 124 124 124 124 124 124 124 124 + 124 124 124 124 124 124 124 124 124 124 198 198 198 + 198 198 198 198 198 198 198 198 198 198 198 198 198 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276} + +do_execsql_test 1.4.9.5 { + SELECT last_value(a+b) OVER ( ORDER BY b%10,a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229} + +do_execsql_test 1.4.9.6 { + SELECT last_value(a+b) OVER (PARTITION BY b%2,a ORDER BY b%10 RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM t2 +} {108 52 83 79 65 26 70 103 80 36 116 51 52 128 + 117 71 63 84 109 78 147 88 121 106 124 85 107 171 + 150 80 171 120 109 158 87 168 173 162 156 195 198 + 177 124 121 134 141 210 157 132 161 218 226 191 179 + 138 214 212 172 173 229 240 187 210 227 228 223 225 + 179 182 231 207 209 212 239 234 213 234 269 196 271 + 235 250 223 232 229 280 99 92 72 55 109 120 119 + 50 124 96 59 124 110 57 130 103 74 87 48 105 136 + 131 133 92 109 57 146 113 74 150 87 110 65 110 + 145 161 156 114 111 136 147 173 124 132 101 154 167 + 190 161 110 102 123 169 140 111 180 119 160 197 152 + 146 147 132 213 193 200 136 175 188 187 208 211 144 + 223 196 170 202 163 184 195 200 163 191 252 235 243 + 172 187 202 179 261 263 206 189 276 181 274 249 221 + 210 229 279 224 216 207} + +do_execsql_test 1.4.10.1 { + SELECT nth_value(b,b+1) OVER (ORDER BY a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM t2 +} {96 9 11 89 32 53 91 30 51 56 54 73 22 59 75 + 74 78 8 16 65 15 8 31 87 90 12 32 96 74 76 37 + 85 90 15 35 2 60 36 75 9 51 47 63 51 90 26 42 + 26 8 76 80 90 37 87 56 79 5 87 8 2 39 73 64 + 36 90 72 78 36 73 51 33 20 41 2 26 37 33 8 14 + 33 81 55 1 9 12 39 64 87 72 34 82 21 34 99 62 + 74 41 69 22 75 27 58 8 79 77 26 26 55 {} 29 + 30 7 {} 66 55 2 34 64 {} 33 {} 44 84 {} {} 95 + 85 19 {} 83 {} 91 {} {} 9 50 91 33 34 {} {} + 84 {} 7 9 {} {} {} 44 {} {} {} {} 91 84 {} 95 + 95 52 {} {} {} {} {} 21 {} {} {} 58 {} {} {} + {} {} {} {} 83 {} {} {} {} {} {} {} {} {} {} + {} {} {} {}} + +do_execsql_test 1.4.10.2 { + SELECT nth_value(b,b+1) OVER (PARTITION BY b%10 ORDER BY a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 81 {} {} {} 21 {} {} {} {} {} {} + {} {} {} {} {} {} 62 {} {} {} 12 {} {} {} 72 + {} {} {} {} {} {} {} {} {} {} 53 {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 34 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} 95 {} {} {} {} {} {} 85 {} + {} {} {} {} {} {} {} {} {} 56 {} 36 {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 57 {} {} {} {} {} 7 {} {} {} {} + {} {} {} {} {} {} 8 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 9 {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.4.10.3 { + SELECT nth_value(b,b+1) OVER ( ORDER BY b,a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 3 5 6 7 8 8 9 9 10 11 12 12 13 13 + 14 15 16 16 17 19 20 21 22 23 24 25 26 27 27 + 28 29 30 31 32 33 33 33 34 34 35 36 36 36 37 + 38 39 39 40 41 42 43 43 44 46 47 47 47 49 50 + 52 53 54 55 56 56 57 58 58 58 59 59 59 60 61 + 62 62 64 65 65 67 69 70 72 72 73 74 74 75 75 + 75 77 78 80 81 81 83 84 84 85 85 85 87 88 89 + 89 89 90 90 91 91 91 93 93 94 95 95 96 97 97 + 98 99 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.4.10.4 { + SELECT nth_value(b,b+1) OVER ( PARTITION BY b%10 ORDER BY b,a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {80 {} {} {} {} {} {} {} {} {} {} {} {} {} 1 + 11 81 81 {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} 12 12 72 82 {} {} {} {} {} {} + {} {} {} {} {} {} 13 23 73 73 {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} 34 84 {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 35 85 85 95 {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} 36 86 96 96 {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 37 47 + 47 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} 58 58 68 {} {} {} {} {} {} {} {} {} + {} {} {} {} 39 49 59 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.4.10.5 { + SELECT nth_value(b,b+1) OVER ( ORDER BY b%10,a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {34 72 91 74 21 55 23 94 84 95 2 13 61 22 55 + 55 94 85 75 81 65 73 74 21 72 12 94 33 63 73 + 72 96 36 76 3 25 62 3 73 34 12 46 43 93 72 16 + 86 63 15 65 36 77 24 57 25 53 95 34 95 16 97 + 74 97 67 25 98 44 34 65 54 5 68 96 28 47 95 + 34 39 8 38 6 46 96 28 47 95 56 39 99 97 76 8 + 26 9 79 27 95 16 29 {} 58 58 77 85 56 {} 98 + 29 {} 19 96 {} {} 78 56 98 36 97 {} 89 89 29 + 47 78 {} {} {} 38 68 58 {} 58 38 {} 98 {} {} + {} 39 57 9 {} 79 {} {} 7 {} {} {} 9 29 38 78 + {} {} {} 8 39 {} {} {} {} 59 {} 99 {} {} {} + {} {} {} {} {} {} {} {} {} {} 9 {} {} {} {} + {} {} {} {} {} {} {} {}} + +do_execsql_test 1.4.10.6 { + SELECT nth_value(b,b+1) OVER (PARTITION BY b%2,a ORDER BY b%10 RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.4.11.1 { + SELECT first_value(b) OVER (ORDER BY a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM t2 +} {89 81 96 59 38 68 39 62 91 46 6 99 97 27 46 + 78 54 97 8 67 29 93 84 77 23 16 16 93 65 35 + 47 7 86 74 61 91 85 24 85 43 59 12 32 56 3 91 + 22 90 55 15 28 89 25 47 1 56 40 43 56 16 75 + 36 89 98 76 81 4 94 42 30 78 33 29 53 63 2 87 + 37 80 84 72 41 9 61 73 95 65 13 58 96 98 1 21 + 74 65 35 5 73 11 51 87 41 12 8 20 31 31 15 95 + 22 73 79 88 34 8 11 49 34 90 59 96 60 55 75 + 77 44 2 7 85 57 74 29 70 59 19 39 26 26 47 80 + 90 36 58 47 9 72 72 66 33 93 75 64 81 9 23 37 + 13 12 14 62 91 36 91 33 15 34 36 99 3 95 69 + 58 52 30 50 84 10 84 33 21 39 44 58 30 38 34 + 83 27 82 17 7} + +do_execsql_test 1.4.11.2 { + SELECT first_value(b) OVER (PARTITION BY b%10 ORDER BY a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM t2 +} {90 40 30 80 20 90 60 70 80 90 30 50 10 30 81 + 91 61 91 91 1 81 41 61 1 21 11 51 41 31 31 11 + 81 91 91 21 62 12 32 22 42 2 72 12 22 2 72 72 + 12 62 52 82 93 23 93 43 3 43 33 53 63 73 13 + 73 73 33 93 23 13 33 3 33 83 54 84 74 24 4 94 + 84 74 34 34 44 74 64 14 34 84 84 44 34 65 35 + 85 85 55 15 25 75 95 65 65 35 5 15 95 55 75 + 85 75 15 95 96 46 6 46 16 16 86 56 56 56 16 + 36 76 96 96 26 26 36 66 36 36 97 27 97 67 77 + 47 7 47 87 37 87 77 7 57 47 47 37 27 17 7 38 + 68 78 8 28 98 78 58 98 8 88 8 58 58 58 38 89 + 59 39 99 29 59 89 89 29 9 79 49 59 29 59 19 + 39 9 9 99 69 39} + +do_execsql_test 1.4.11.3 { + SELECT first_value(b) OVER ( ORDER BY b,a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 1 2 2 3 3 4 5 6 7 7 7 8 8 8 9 9 9 10 + 11 11 12 12 12 13 13 14 15 15 15 16 16 16 17 + 19 20 21 21 22 22 23 23 24 25 26 26 27 27 28 + 29 29 29 30 30 30 31 31 32 33 33 33 33 34 34 + 34 34 35 35 36 36 36 36 37 37 38 38 39 39 39 + 40 41 41 42 43 43 44 44 46 46 47 47 47 47 49 + 50 51 52 53 54 55 55 56 56 56 57 58 58 58 58 + 59 59 59 59 60 61 61 62 62 63 64 65 65 65 66 + 67 68 69 70 72 72 72 73 73 73 74 74 74 75 75 + 75 76 77 77 78 78 79 80 80 81 81 81 82 83 84 + 84 84 84 85 85 85 86 87 87 88 89 89 89 90 90 + 90 91 91 91 91 91 93 93 93 94 95 95 95 96 96 + 96 97 97 98 98 99 99} + +do_execsql_test 1.4.11.4 { + SELECT first_value(b) OVER ( PARTITION BY b%10 ORDER BY b,a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {10 20 30 30 30 40 50 60 70 80 80 90 90 90 1 1 + 11 11 21 21 31 31 41 41 51 61 61 81 81 81 91 + 91 91 91 91 2 2 12 12 12 22 22 32 42 52 62 62 + 72 72 72 82 3 3 13 13 23 23 33 33 33 33 43 43 + 53 63 73 73 73 83 93 93 93 4 14 24 34 34 34 + 34 44 44 54 64 74 74 74 84 84 84 84 94 5 15 + 15 15 25 35 35 55 55 65 65 65 75 75 75 85 85 + 85 95 95 95 6 16 16 16 26 26 36 36 36 36 46 + 46 56 56 56 66 76 86 96 96 96 7 7 7 17 27 27 + 37 37 47 47 47 47 57 67 77 77 87 87 97 97 8 8 + 8 28 38 38 58 58 58 58 68 78 78 88 98 98 9 9 + 9 19 29 29 29 39 39 39 49 59 59 59 59 69 79 + 89 89 89 99 99} + +do_execsql_test 1.4.11.5 { + SELECT first_value(b) OVER ( ORDER BY b%10,a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {90 40 30 80 20 90 60 70 80 90 30 50 10 30 81 + 91 61 91 91 1 81 41 61 1 21 11 51 41 31 31 11 + 81 91 91 21 62 12 32 22 42 2 72 12 22 2 72 72 + 12 62 52 82 93 23 93 43 3 43 33 53 63 73 13 + 73 73 33 93 23 13 33 3 33 83 54 84 74 24 4 94 + 84 74 34 34 44 74 64 14 34 84 84 44 34 65 35 + 85 85 55 15 25 75 95 65 65 35 5 15 95 55 75 + 85 75 15 95 96 46 6 46 16 16 86 56 56 56 16 + 36 76 96 96 26 26 36 66 36 36 97 27 97 67 77 + 47 7 47 87 37 87 77 7 57 47 47 37 27 17 7 38 + 68 78 8 28 98 78 58 98 8 88 8 58 58 58 38 89 + 59 39 99 29 59 89 89 29 9 79 49 59 29 59 19 + 39 9 9 99 69 39} + +do_execsql_test 1.4.11.6 { + SELECT first_value(b) OVER (PARTITION BY b%2,a ORDER BY b%10 RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM t2 +} {96 38 68 62 46 6 46 78 54 8 84 16 16 86 74 24 + 12 32 56 22 90 28 56 40 56 16 36 98 76 4 94 + 42 30 78 2 80 84 72 58 96 98 74 12 8 20 22 88 + 34 8 34 90 96 60 44 2 74 70 26 26 80 90 36 58 + 72 72 66 64 12 14 62 36 34 36 58 52 30 50 84 + 10 84 44 58 30 38 34 82 89 81 59 39 91 99 97 + 27 97 67 29 93 77 23 93 65 35 47 7 61 91 85 + 85 43 59 3 91 55 15 89 25 47 1 43 75 89 81 33 + 29 53 63 87 37 41 9 61 73 95 65 13 1 21 65 35 + 5 73 11 51 87 41 31 31 15 95 73 79 11 49 59 + 55 75 77 7 85 57 29 59 19 39 47 47 9 33 93 75 + 81 9 23 37 13 91 91 33 15 99 3 95 69 33 21 39 + 83 27 17 7} + +do_execsql_test 1.4.12.1 { + SELECT lead(b,b) OVER (ORDER BY a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM t2 +} {96 9 11 89 32 53 91 30 51 56 54 73 22 59 75 + 74 78 8 16 65 15 8 31 87 90 12 32 96 74 76 37 + 85 90 15 35 2 60 36 75 9 51 47 63 51 90 26 42 + 26 8 76 80 90 37 87 56 79 5 87 8 2 39 73 64 + 36 90 72 78 36 73 51 33 20 41 2 26 37 33 8 14 + 33 81 55 1 9 12 39 64 87 72 34 82 21 34 99 62 + 74 41 69 22 75 27 58 8 79 77 26 26 55 {} 29 + 30 7 {} 66 55 2 34 64 {} 33 {} 44 84 {} {} 95 + 85 19 {} 83 {} 91 {} {} 9 50 91 33 34 {} {} + 84 {} 7 9 {} {} {} 44 {} {} {} {} 91 84 {} 95 + 95 52 {} {} {} {} {} 21 {} {} {} 58 {} {} {} + {} {} {} {} 83 {} {} {} {} {} {} {} {} {} {} + {} {} {} {}} + +do_execsql_test 1.4.12.2 { + SELECT lead(b,b) OVER (PARTITION BY b%10 ORDER BY a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 81 {} {} {} 21 {} {} {} {} {} {} + {} {} {} {} {} {} 62 {} {} {} 12 {} {} {} 72 + {} {} {} {} {} {} {} {} {} {} 53 {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 34 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} 95 {} {} {} {} {} {} 85 {} + {} {} {} {} {} {} {} {} {} 56 {} 36 {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 57 {} {} {} {} {} 7 {} {} {} {} + {} {} {} {} {} {} 8 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 9 {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.4.12.3 { + SELECT lead(b,b) OVER ( ORDER BY b,a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 3 5 6 7 8 8 9 9 10 11 12 12 13 13 + 14 15 16 16 17 19 20 21 22 23 24 25 26 27 27 + 28 29 30 31 32 33 33 33 34 34 35 36 36 36 37 + 38 39 39 40 41 42 43 43 44 46 47 47 47 49 50 + 52 53 54 55 56 56 57 58 58 58 59 59 59 60 61 + 62 62 64 65 65 67 69 70 72 72 73 74 74 75 75 + 75 77 78 80 81 81 83 84 84 85 85 85 87 88 89 + 89 89 90 90 91 91 91 93 93 94 95 95 96 97 97 + 98 99 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.4.12.4 { + SELECT lead(b,b) OVER ( PARTITION BY b%10 ORDER BY b,a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {80 {} {} {} {} {} {} {} {} {} {} {} {} {} 1 + 11 81 81 {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} 12 12 72 82 {} {} {} {} {} {} + {} {} {} {} {} {} 13 23 73 73 {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} 34 84 {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 35 85 85 95 {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} 36 86 96 96 {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 37 47 + 47 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} 58 58 68 {} {} {} {} {} {} {} {} {} + {} {} {} {} 39 49 59 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.4.12.5 { + SELECT lead(b,b) OVER ( ORDER BY b%10,a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {34 72 91 74 21 55 23 94 84 95 2 13 61 22 55 + 55 94 85 75 81 65 73 74 21 72 12 94 33 63 73 + 72 96 36 76 3 25 62 3 73 34 12 46 43 93 72 16 + 86 63 15 65 36 77 24 57 25 53 95 34 95 16 97 + 74 97 67 25 98 44 34 65 54 5 68 96 28 47 95 + 34 39 8 38 6 46 96 28 47 95 56 39 99 97 76 8 + 26 9 79 27 95 16 29 {} 58 58 77 85 56 {} 98 + 29 {} 19 96 {} {} 78 56 98 36 97 {} 89 89 29 + 47 78 {} {} {} 38 68 58 {} 58 38 {} 98 {} {} + {} 39 57 9 {} 79 {} {} 7 {} {} {} 9 29 38 78 + {} {} {} 8 39 {} {} {} {} 59 {} 99 {} {} {} + {} {} {} {} {} {} {} {} {} {} 9 {} {} {} {} + {} {} {} {} {} {} {} {}} + +do_execsql_test 1.4.12.6 { + SELECT lead(b,b) OVER (PARTITION BY b%2,a ORDER BY b%10 RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.4.13.1 { + SELECT lag(b,b) OVER (ORDER BY a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} 38 {} {} {} {} + {} {} {} 6 {} {} {} {} {} 81 46 6 {} {} {} {} + 23 {} {} {} {} {} 27 {} {} {} 35 6 {} 12 {} + 23 {} {} 61 84 {} 93 39 47 {} 54 46 96 56 {} + 16 {} {} {} {} 89 {} 16 43 {} 85 56 29 99 53 + {} 59 {} {} 91 59 53 84 99 {} 93 63 47 {} {} + 98 33 67 35 75 1 23 13 55 27 75 98 35 73 63 2 + 21 27 13 24 86 23 84 31 20 94 61 65 75 23 36 + 94 55 90 41 77 96 56 29 40 12 89 63 11 5 73 + 79 1 16 28 31 73 5 39 53 63 41 11 40 2 13 33 + 9 29 90 47 72 9 73 30 44 33 74 93 29 74 42 34 + 63 41 34 96 47 77 1 36 74 72 14 36 26 77 9 72 + 64 8 91 31 52 30} + +do_execsql_test 1.4.13.2 { + SELECT lag(b,b) OVER (PARTITION BY b%10 ORDER BY a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} 30 {} {} + {} {} {} {} 91 {} {} {} 61 {} 81 {} {} {} {} + 1 {} {} {} {} {} {} {} {} {} 22 {} {} {} 12 + {} {} 62 {} {} {} {} {} {} {} 23 {} {} {} {} + {} {} {} {} {} {} {} 43 {} 23 {} {} {} {} {} + {} 54 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 75 {} + {} {} {} {} {} 55 {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} 47 {} {} {} {} + {} 27 7 {} {} {} {} {} {} {} {} {} 68 {} 8 {} + {} {} {} {} {} {} {} {} {} {} {} {} 89 {} {} + {} {} {} {} {} 29 9 {} {} {}} + +do_execsql_test 1.4.13.3 { + SELECT lag(b,b) OVER ( ORDER BY b,a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {{} 1 1 1 1 2 2 2 2 2 2 3 3 3 4 4 5 6 6 + 6 7 7 7 7 7 8 8 8 8 8 8 9 9 9 9 9 9 9 + 9 9 9 10 10 10 10 11 11 11 11 11 12 12 12 12 + 13 13 13 13 13 14 15 15 15 15 16 16 16 16 16 + 17 19 20 20 21 21 21 21 22 22 22 22 23 23 23 + 23 23 24 23 24 24 25 26 26 26 26 26 26 26 26 + 26 26 26 27 27 27 27 28 29 29 29 29 30 30 30 + 30 30 30 31 31 31 31 31 32 32 32 32 32 32 31 + 32 33 33 33 33 33 33 34 34 34 34 34 34 34 34 + 35 35 35 35 35 36 36 36 36 36 36 36 37 37 37 + 38 38 38 38 38 38 39 39 39 39 40 40 41 41 42 + 43 42 43 43 43 43 44 44 44 46 46 46 47 47 47 + 47 47} + +do_execsql_test 1.4.13.4 { + SELECT lag(b,b) OVER ( PARTITION BY b%10 ORDER BY b,a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + 1 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.4.13.5 { + SELECT lag(b,b) OVER ( ORDER BY b%10,a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} 30 {} {} + {} {} {} {} 91 {} {} {} 61 80 81 {} {} {} {} + 1 {} {} {} 30 {} 21 90 61 {} 22 {} 11 41 12 + {} {} 62 {} {} {} {} 31 {} 50 23 30 21 90 {} + {} 62 {} {} 81 {} 22 43 62 23 32 {} 91 {} 90 + 93 54 {} {} 90 72 12 22 90 81 83 23 80 20 72 + 43 51 33 80 90 2 34 54 1 20 62 12 13 75 44 30 + 93 91 1 21 55 61 61 13 85 3 65 65 91 73 33 93 + 55 84 62 31 11 65 35 85 33 55 15 12 75 22 3 + 73 65 36 85 43 95 43 13 47 44 65 65 96 36 27 + 7 46 34 94 47 36 73 34 35 73 68 24 8 75 85 75 + 66 34 95 36 84 77 46 34 84 47 89 65 36 16 38 + 76 58 57 29 9 44 56 17} + +do_execsql_test 1.4.13.6 { + SELECT lag(b,b) OVER (PARTITION BY b%2,a ORDER BY b%10 RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.4.14.1 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (ORDER BY a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM t2 +} {89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 33.21.39.44.58.30.38.34.83.27.82.17.7 21.39.44.58.30.38.34.83.27.82.17.7 + 39.44.58.30.38.34.83.27.82.17.7 44.58.30.38.34.83.27.82.17.7 + 58.30.38.34.83.27.82.17.7 30.38.34.83.27.82.17.7 38.34.83.27.82.17.7 + 34.83.27.82.17.7 83.27.82.17.7 27.82.17.7 82.17.7 17.7 7} + +do_execsql_test 1.4.14.2 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (PARTITION BY b%10 ORDER BY a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM t2 +} {90.40.30.80.20.90.60.70.80.90.30.50.10.30 + 40.30.80.20.90.60.70.80.90.30.50.10.30 + 30.80.20.90.60.70.80.90.30.50.10.30 80.20.90.60.70.80.90.30.50.10.30 + 20.90.60.70.80.90.30.50.10.30 90.60.70.80.90.30.50.10.30 + 60.70.80.90.30.50.10.30 70.80.90.30.50.10.30 80.90.30.50.10.30 + 90.30.50.10.30 30.50.10.30 50.10.30 10.30 30 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 61.1.21.11.51.41.31.31.11.81.91.91.21 1.21.11.51.41.31.31.11.81.91.91.21 + 21.11.51.41.31.31.11.81.91.91.21 11.51.41.31.31.11.81.91.91.21 + 51.41.31.31.11.81.91.91.21 41.31.31.11.81.91.91.21 31.31.11.81.91.91.21 + 31.11.81.91.91.21 11.81.91.91.21 81.91.91.21 91.91.21 91.21 21 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 22.42.2.72.12.22.2.72.72.12.62.52.82 42.2.72.12.22.2.72.72.12.62.52.82 + 2.72.12.22.2.72.72.12.62.52.82 72.12.22.2.72.72.12.62.52.82 + 12.22.2.72.72.12.62.52.82 22.2.72.72.12.62.52.82 2.72.72.12.62.52.82 + 72.72.12.62.52.82 72.12.62.52.82 12.62.52.82 62.52.82 52.82 82 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 63.73.13.73.73.33.93.23.13.33.3.33.83 73.13.73.73.33.93.23.13.33.3.33.83 + 13.73.73.33.93.23.13.33.3.33.83 73.73.33.93.23.13.33.3.33.83 + 73.33.93.23.13.33.3.33.83 33.93.23.13.33.3.33.83 93.23.13.33.3.33.83 + 23.13.33.3.33.83 13.33.3.33.83 33.3.33.83 3.33.83 33.83 83 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 84.74.34.34.44.74.64.14.34.84.84.44.34 + 74.34.34.44.74.64.14.34.84.84.44.34 34.34.44.74.64.14.34.84.84.44.34 + 34.44.74.64.14.34.84.84.44.34 44.74.64.14.34.84.84.44.34 + 74.64.14.34.84.84.44.34 64.14.34.84.84.44.34 14.34.84.84.44.34 + 34.84.84.44.34 84.84.44.34 84.44.34 44.34 34 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 95.65.65.35.5.15.95.55.75.85.75.15.95 65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.5.15.95.55.75.85.75.15.95 35.5.15.95.55.75.85.75.15.95 + 5.15.95.55.75.85.75.15.95 15.95.55.75.85.75.15.95 95.55.75.85.75.15.95 + 55.75.85.75.15.95 75.85.75.15.95 85.75.15.95 75.15.95 15.95 95 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 56.56.16.36.76.96.96.26.26.36.66.36.36 + 56.16.36.76.96.96.26.26.36.66.36.36 16.36.76.96.96.26.26.36.66.36.36 + 36.76.96.96.26.26.36.66.36.36 76.96.96.26.26.36.66.36.36 + 96.96.26.26.36.66.36.36 96.26.26.36.66.36.36 26.26.36.66.36.36 + 26.36.66.36.36 36.66.36.36 66.36.36 36.36 36 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 47.87.37.87.77.7.57.47.47.37.27.17.7 87.37.87.77.7.57.47.47.37.27.17.7 + 37.87.77.7.57.47.47.37.27.17.7 87.77.7.57.47.47.37.27.17.7 + 77.7.57.47.47.37.27.17.7 7.57.47.47.37.27.17.7 57.47.47.37.27.17.7 + 47.47.37.27.17.7 47.37.27.17.7 37.27.17.7 27.17.7 17.7 7 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 8.28.98.78.58.98.8.88.8.58.58.58.38 28.98.78.58.98.8.88.8.58.58.58.38 + 98.78.58.98.8.88.8.58.58.58.38 78.58.98.8.88.8.58.58.58.38 + 58.98.8.88.8.58.58.58.38 98.8.88.8.58.58.58.38 8.88.8.58.58.58.38 + 88.8.58.58.58.38 8.58.58.58.38 58.58.58.38 58.58.38 58.38 38 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 9.79.49.59.29.59.19.39.9.9.99.69.39 79.49.59.29.59.19.39.9.9.99.69.39 + 49.59.29.59.19.39.9.9.99.69.39 59.29.59.19.39.9.9.99.69.39 + 29.59.19.39.9.9.99.69.39 59.19.39.9.9.99.69.39 19.39.9.9.99.69.39 + 39.9.9.99.69.39 9.9.99.69.39 9.99.69.39 99.69.39 69.39 39} + +do_execsql_test 1.4.14.3 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( ORDER BY b,a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 94.95.95.95.96.96.96.97.97.98.98.99.99 + 95.95.95.96.96.96.97.97.98.98.99.99 95.95.96.96.96.97.97.98.98.99.99 + 95.96.96.96.97.97.98.98.99.99 96.96.96.97.97.98.98.99.99 + 96.96.97.97.98.98.99.99 96.97.97.98.98.99.99 97.97.98.98.99.99 + 97.98.98.99.99 98.98.99.99 98.99.99 99.99 99} + +do_execsql_test 1.4.14.4 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( PARTITION BY b%10 ORDER BY b,a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {10.20.30.30.30.40.50.60.70.80.80.90.90.90 + 20.30.30.30.40.50.60.70.80.80.90.90.90 + 30.30.30.40.50.60.70.80.80.90.90.90 30.30.40.50.60.70.80.80.90.90.90 + 30.40.50.60.70.80.80.90.90.90 40.50.60.70.80.80.90.90.90 + 50.60.70.80.80.90.90.90 60.70.80.80.90.90.90 70.80.80.90.90.90 + 80.80.90.90.90 80.90.90.90 90.90.90 90.90 90 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 41.41.51.61.61.81.81.81.91.91.91.91.91 + 41.51.61.61.81.81.81.91.91.91.91.91 51.61.61.81.81.81.91.91.91.91.91 + 61.61.81.81.81.91.91.91.91.91 61.81.81.81.91.91.91.91.91 + 81.81.81.91.91.91.91.91 81.81.91.91.91.91.91 81.91.91.91.91.91 + 91.91.91.91.91 91.91.91.91 91.91.91 91.91 91 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 12.12.22.22.32.42.52.62.62.72.72.72.82 + 12.22.22.32.42.52.62.62.72.72.72.82 22.22.32.42.52.62.62.72.72.72.82 + 22.32.42.52.62.62.72.72.72.82 32.42.52.62.62.72.72.72.82 + 42.52.62.62.72.72.72.82 52.62.62.72.72.72.82 62.62.72.72.72.82 + 62.72.72.72.82 72.72.72.82 72.72.82 72.82 82 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 33.33.43.43.53.63.73.73.73.83.93.93.93 + 33.43.43.53.63.73.73.73.83.93.93.93 43.43.53.63.73.73.73.83.93.93.93 + 43.53.63.73.73.73.83.93.93.93 53.63.73.73.73.83.93.93.93 + 63.73.73.73.83.93.93.93 73.73.73.83.93.93.93 73.73.83.93.93.93 + 73.83.93.93.93 83.93.93.93 93.93.93 93.93 93 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 34.44.44.54.64.74.74.74.84.84.84.84.94 + 44.44.54.64.74.74.74.84.84.84.84.94 44.54.64.74.74.74.84.84.84.84.94 + 54.64.74.74.74.84.84.84.84.94 64.74.74.74.84.84.84.84.94 + 74.74.74.84.84.84.84.94 74.74.84.84.84.84.94 74.84.84.84.84.94 + 84.84.84.84.94 84.84.84.94 84.84.94 84.94 94 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 55.65.65.65.75.75.75.85.85.85.95.95.95 + 65.65.65.75.75.75.85.85.85.95.95.95 65.65.75.75.75.85.85.85.95.95.95 + 65.75.75.75.85.85.85.95.95.95 75.75.75.85.85.85.95.95.95 + 75.75.85.85.85.95.95.95 75.85.85.85.95.95.95 85.85.85.95.95.95 + 85.85.95.95.95 85.95.95.95 95.95.95 95.95 95 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 36.36.46.46.56.56.56.66.76.86.96.96.96 + 36.46.46.56.56.56.66.76.86.96.96.96 46.46.56.56.56.66.76.86.96.96.96 + 46.56.56.56.66.76.86.96.96.96 56.56.56.66.76.86.96.96.96 + 56.56.66.76.86.96.96.96 56.66.76.86.96.96.96 66.76.86.96.96.96 + 76.86.96.96.96 86.96.96.96 96.96.96 96.96 96 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 37.47.47.47.47.57.67.77.77.87.87.97.97 + 47.47.47.47.57.67.77.77.87.87.97.97 47.47.47.57.67.77.77.87.87.97.97 + 47.47.57.67.77.77.87.87.97.97 47.57.67.77.77.87.87.97.97 + 57.67.77.77.87.87.97.97 67.77.77.87.87.97.97 77.77.87.87.97.97 + 77.87.87.97.97 87.87.97.97 87.97.97 97.97 97 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 28.38.38.58.58.58.58.68.78.78.88.98.98 + 38.38.58.58.58.58.68.78.78.88.98.98 38.58.58.58.58.68.78.78.88.98.98 + 58.58.58.58.68.78.78.88.98.98 58.58.58.68.78.78.88.98.98 + 58.58.68.78.78.88.98.98 58.68.78.78.88.98.98 68.78.78.88.98.98 + 78.78.88.98.98 78.88.98.98 88.98.98 98.98 98 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 39.49.59.59.59.59.69.79.89.89.89.99.99 + 49.59.59.59.59.69.79.89.89.89.99.99 59.59.59.59.69.79.89.89.89.99.99 + 59.59.59.69.79.89.89.89.99.99 59.59.69.79.89.89.89.99.99 + 59.69.79.89.89.89.99.99 69.79.89.89.89.99.99 79.89.89.89.99.99 + 89.89.89.99.99 89.89.99.99 89.99.99 99.99 99} + +do_execsql_test 1.4.14.5 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( ORDER BY b%10,a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 9.79.49.59.29.59.19.39.9.9.99.69.39 79.49.59.29.59.19.39.9.9.99.69.39 + 49.59.29.59.19.39.9.9.99.69.39 59.29.59.19.39.9.9.99.69.39 + 29.59.19.39.9.9.99.69.39 59.19.39.9.9.99.69.39 19.39.9.9.99.69.39 + 39.9.9.99.69.39 9.9.99.69.39 9.99.69.39 99.69.39 69.39 39} + +do_execsql_test 1.4.14.6 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (PARTITION BY b%2,a ORDER BY b%10 RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM t2 +} {96 38 68 62 46 6 46 78 54 8 84 16 16 86 74 24 + 12 32 56 22 90 28 56 40 56 16 36 98 76 4 94 + 42 30 78 2 80 84 72 58 96 98 74 12 8 20 22 88 + 34 8 34 90 96 60 44 2 74 70 26 26 80 90 36 58 + 72 72 66 64 12 14 62 36 34 36 58 52 30 50 84 + 10 84 44 58 30 38 34 82 89 81 59 39 91 99 97 + 27 97 67 29 93 77 23 93 65 35 47 7 61 91 85 + 85 43 59 3 91 55 15 89 25 47 1 43 75 89 81 33 + 29 53 63 87 37 41 9 61 73 95 65 13 1 21 65 35 + 5 73 11 51 87 41 31 31 15 95 73 79 11 49 59 + 55 75 77 7 85 57 29 59 19 39 47 47 9 33 93 75 + 81 9 23 37 13 91 91 33 15 99 3 95 69 33 21 39 + 83 27 17 7} + +do_execsql_test 1.4.14.7 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (win1 ORDER BY b%10 RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a) + ORDER BY 1 +} {1 1 10 11 11 12 12 12 13 13 14 15 15 15 16 16 + 16 17 19 2 2 20 21 21 22 22 23 23 24 25 26 26 + 27 27 28 29 29 29 3 3 30 30 30 31 31 32 33 33 + 33 33 34 34 34 34 35 35 36 36 36 36 37 37 38 + 38 39 39 39 4 40 41 41 42 43 43 44 44 46 46 + 47 47 47 47 49 5 50 51 52 53 54 55 55 56 56 + 56 57 58 58 58 58 59 59 59 59 6 60 61 61 62 + 62 63 64 65 65 65 66 67 68 69 7 7 7 70 72 72 + 72 73 73 73 74 74 74 75 75 75 76 77 77 78 78 + 79 8 8 8 80 80 81 81 81 82 83 84 84 84 84 85 + 85 85 86 87 87 88 89 89 89 9 9 9 90 90 90 91 + 91 91 91 91 93 93 93 94 95 95 95 96 96 96 97 + 97 98 98 99 99} + +do_execsql_test 1.4.14.8 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (win1 RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a ORDER BY b%10) + ORDER BY 1 +} {1 1 10 11 11 12 12 12 13 13 14 15 15 15 16 16 + 16 17 19 2 2 20 21 21 22 22 23 23 24 25 26 26 + 27 27 28 29 29 29 3 3 30 30 30 31 31 32 33 33 + 33 33 34 34 34 34 35 35 36 36 36 36 37 37 38 + 38 39 39 39 4 40 41 41 42 43 43 44 44 46 46 + 47 47 47 47 49 5 50 51 52 53 54 55 55 56 56 + 56 57 58 58 58 58 59 59 59 59 6 60 61 61 62 + 62 63 64 65 65 65 66 67 68 69 7 7 7 70 72 72 + 72 73 73 73 74 74 74 75 75 75 76 77 77 78 78 + 79 8 8 8 80 80 81 81 81 82 83 84 84 84 84 85 + 85 85 86 87 87 88 89 89 89 9 9 9 90 90 90 91 + 91 91 91 91 93 93 93 94 95 95 95 96 96 96 97 + 97 98 98 99 99} + +do_execsql_test 1.4.14.9 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER win2 + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a ORDER BY b%10), + win2 AS (win1 RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) + ORDER BY 1 +} {1 1 10 11 11 12 12 12 13 13 14 15 15 15 16 16 + 16 17 19 2 2 20 21 21 22 22 23 23 24 25 26 26 + 27 27 28 29 29 29 3 3 30 30 30 31 31 32 33 33 + 33 33 34 34 34 34 35 35 36 36 36 36 37 37 38 + 38 39 39 39 4 40 41 41 42 43 43 44 44 46 46 + 47 47 47 47 49 5 50 51 52 53 54 55 55 56 56 + 56 57 58 58 58 58 59 59 59 59 6 60 61 61 62 + 62 63 64 65 65 65 66 67 68 69 7 7 7 70 72 72 + 72 73 73 73 74 74 74 75 75 75 76 77 77 78 78 + 79 8 8 8 80 80 81 81 81 82 83 84 84 84 84 85 + 85 85 86 87 87 88 89 89 89 9 9 9 90 90 90 91 + 91 91 91 91 93 93 93 94 95 95 95 96 96 96 97 + 97 98 98 99 99} + +do_execsql_test 1.4.15.1 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE a%2=0) OVER win FROM t2 + WINDOW win AS (ORDER BY a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) +} {191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 190 96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 189 96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 188 38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 187 38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 186 39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 185 39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 184 91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 183 91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 182 6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 181 6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 180 97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 179 97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 178 46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 177 46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 176 54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 175 54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 174 8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 173 8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 172 29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 171 29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 170 84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 169 84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 168 23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 167 23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 166 16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 165 16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 164 65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 163 65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 162 47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 161 47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 160 86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 159 86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 158 61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 157 61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 156 85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 155 85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 154 85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 153 85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 152 59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 151 59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 150 32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 149 32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 148 3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 147 3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 146 22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 145 22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 144 55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 143 55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 142 28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 141 28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 140 25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 139 25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 138 1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 137 1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 136 40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 135 40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 134 56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 133 56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 132 75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 131 75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 130 89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 129 89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 128 76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 127 76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 126 4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 125 4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 124 42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 123 42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 122 78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 121 78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 120 29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 119 29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 118 63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 117 63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 116 87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 115 87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 114 80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 113 80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 112 72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 111 72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 110 9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 109 9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 108 73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 107 73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 106 65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 105 65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 104 58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 103 58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 102 98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 101 98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 100 21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 99 21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 98 65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 97 65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 96 5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 95 5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 94 11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 93 11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 92 87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 91 87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 90 12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 89 12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 88 20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 87 20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 86 31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 85 31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 84 95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 83 95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 82 73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 81 73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 80 88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 79 88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 78 8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 77 8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 76 49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 75 49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 74 90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 73 90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 72 96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 71 96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 70 55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 69 55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 68 77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 67 77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 66 2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 65 2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 64 85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 63 85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 62 74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 61 74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 60 70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 59 70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 58 19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 57 19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 56 26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 55 26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 54 47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 53 47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 52 90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 51 90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 50 58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 49 58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 48 9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 47 9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 46 72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 45 72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 44 33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 43 33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 42 75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 41 75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 40 81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 39 81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 38 23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 37 23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 36 13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 35 13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 34 14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 33 14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 32 91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 31 91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 30 91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 29 91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 28 15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 27 15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 26 36.3.69.52.50.10.33.39.58.38.83.82.7 + 25 36.3.69.52.50.10.33.39.58.38.83.82.7 + 24 3.69.52.50.10.33.39.58.38.83.82.7 23 3.69.52.50.10.33.39.58.38.83.82.7 + 22 69.52.50.10.33.39.58.38.83.82.7 21 69.52.50.10.33.39.58.38.83.82.7 + 20 52.50.10.33.39.58.38.83.82.7 19 52.50.10.33.39.58.38.83.82.7 + 18 50.10.33.39.58.38.83.82.7 17 50.10.33.39.58.38.83.82.7 + 16 10.33.39.58.38.83.82.7 15 10.33.39.58.38.83.82.7 + 14 33.39.58.38.83.82.7 13 33.39.58.38.83.82.7 12 39.58.38.83.82.7 + 11 39.58.38.83.82.7 10 58.38.83.82.7 9 58.38.83.82.7 8 38.83.82.7 + 7 38.83.82.7 6 83.82.7 5 83.82.7 4 82.7 3 82.7 2 7 1 7} + +do_execsql_test 1.4.15.2 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE 0=1) OVER win FROM t2 + WINDOW win AS (ORDER BY a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) +} {191 {} 190 {} 189 {} 188 {} 187 {} 186 {} 185 {} 184 {} + 183 {} 182 {} 181 {} 180 {} 179 {} 178 {} 177 {} 176 {} + 175 {} 174 {} 173 {} 172 {} 171 {} 170 {} 169 {} 168 {} + 167 {} 166 {} 165 {} 164 {} 163 {} 162 {} 161 {} 160 {} + 159 {} 158 {} 157 {} 156 {} 155 {} 154 {} 153 {} 152 {} + 151 {} 150 {} 149 {} 148 {} 147 {} 146 {} 145 {} 144 {} + 143 {} 142 {} 141 {} 140 {} 139 {} 138 {} 137 {} 136 {} + 135 {} 134 {} 133 {} 132 {} 131 {} 130 {} 129 {} 128 {} + 127 {} 126 {} 125 {} 124 {} 123 {} 122 {} 121 {} 120 {} + 119 {} 118 {} 117 {} 116 {} 115 {} 114 {} 113 {} 112 {} + 111 {} 110 {} 109 {} 108 {} 107 {} 106 {} 105 {} 104 {} + 103 {} 102 {} 101 {} 100 {} 99 {} 98 {} 97 {} 96 {} 95 {} + 94 {} 93 {} 92 {} 91 {} 90 {} 89 {} 88 {} 87 {} 86 {} + 85 {} 84 {} 83 {} 82 {} 81 {} 80 {} 79 {} 78 {} 77 {} + 76 {} 75 {} 74 {} 73 {} 72 {} 71 {} 70 {} 69 {} 68 {} + 67 {} 66 {} 65 {} 64 {} 63 {} 62 {} 61 {} 60 {} 59 {} + 58 {} 57 {} 56 {} 55 {} 54 {} 53 {} 52 {} 51 {} 50 {} + 49 {} 48 {} 47 {} 46 {} 45 {} 44 {} 43 {} 42 {} 41 {} + 40 {} 39 {} 38 {} 37 {} 36 {} 35 {} 34 {} 33 {} 32 {} + 31 {} 30 {} 29 {} 28 {} 27 {} 26 {} 25 {} 24 {} 23 {} + 22 {} 21 {} 20 {} 19 {} 18 {} 17 {} 16 {} 15 {} 14 {} + 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} 5 {} 4 {} + 3 {} 2 {} 1 {}} + +do_execsql_test 1.4.15.3 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE 1=0) OVER win FROM t2 + WINDOW win AS (PARTITION BY (a%10) ORDER BY a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) +} {20 {} 19 {} 18 {} 17 {} 16 {} 15 {} 14 {} 13 {} 12 {} + 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} 5 {} 4 {} 3 {} 2 {} + 1 {} 19 {} 18 {} 17 {} 16 {} 15 {} 14 {} 13 {} 12 {} + 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} 5 {} 4 {} 3 {} 2 {} + 1 {} 19 {} 18 {} 17 {} 16 {} 15 {} 14 {} 13 {} 12 {} + 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} 5 {} 4 {} 3 {} 2 {} + 1 {} 19 {} 18 {} 17 {} 16 {} 15 {} 14 {} 13 {} 12 {} + 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} 5 {} 4 {} 3 {} 2 {} + 1 {} 19 {} 18 {} 17 {} 16 {} 15 {} 14 {} 13 {} 12 {} + 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} 5 {} 4 {} 3 {} 2 {} + 1 {} 19 {} 18 {} 17 {} 16 {} 15 {} 14 {} 13 {} 12 {} + 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} 5 {} 4 {} 3 {} 2 {} + 1 {} 19 {} 18 {} 17 {} 16 {} 15 {} 14 {} 13 {} 12 {} + 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} 5 {} 4 {} 3 {} 2 {} + 1 {} 19 {} 18 {} 17 {} 16 {} 15 {} 14 {} 13 {} 12 {} + 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} 5 {} 4 {} 3 {} 2 {} + 1 {} 19 {} 18 {} 17 {} 16 {} 15 {} 14 {} 13 {} 12 {} + 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} 5 {} 4 {} 3 {} 2 {} + 1 {} 19 {} 18 {} 17 {} 16 {} 15 {} 14 {} 13 {} 12 {} + 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} 5 {} 4 {} 3 {} 2 {} + 1 {}} + +do_execsql_test 1.4.15.4 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE a%2=0) OVER win FROM t2 + WINDOW win AS (PARTITION BY (a%10) ORDER BY a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) +} {20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 19 6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 18 29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 17 47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 16 59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 15 28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 14 75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 13 78.72.98.87.73.96.74.90.75.91.69.39.7 + 12 72.98.87.73.96.74.90.75.91.69.39.7 11 98.87.73.96.74.90.75.91.69.39.7 + 10 87.73.96.74.90.75.91.69.39.7 9 73.96.74.90.75.91.69.39.7 + 8 96.74.90.75.91.69.39.7 7 74.90.75.91.69.39.7 6 90.75.91.69.39.7 + 5 75.91.69.39.7 4 91.69.39.7 3 69.39.7 2 39.7 1 7 19 {} 18 {} + 17 {} 16 {} 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} + 7 {} 6 {} 5 {} 4 {} 3 {} 2 {} 1 {} + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 18 97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 17 84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 16 86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 15 32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 14 25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 13 89.29.9.21.12.88.55.70.58.81.91.52.58 + 12 29.9.21.12.88.55.70.58.81.91.52.58 11 9.21.12.88.55.70.58.81.91.52.58 + 10 21.12.88.55.70.58.81.91.52.58 9 12.88.55.70.58.81.91.52.58 + 8 88.55.70.58.81.91.52.58 7 55.70.58.81.91.52.58 6 70.58.81.91.52.58 + 5 58.81.91.52.58 4 81.91.52.58 3 91.52.58 2 52.58 1 58 19 {} + 18 {} 17 {} 16 {} 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} + 9 {} 8 {} 7 {} 6 {} 5 {} 4 {} 3 {} 2 {} 1 {} + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 18 46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 17 23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 16 61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 15 3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 14 1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 13 76.63.73.65.20.8.77.19.9.23.15.50.38 + 12 63.73.65.20.8.77.19.9.23.15.50.38 11 73.65.20.8.77.19.9.23.15.50.38 + 10 65.20.8.77.19.9.23.15.50.38 9 20.8.77.19.9.23.15.50.38 + 8 8.77.19.9.23.15.50.38 7 77.19.9.23.15.50.38 6 19.9.23.15.50.38 + 5 9.23.15.50.38 4 23.15.50.38 3 15.50.38 2 50.38 1 38 19 {} + 18 {} 17 {} 16 {} 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} + 9 {} 8 {} 7 {} 6 {} 5 {} 4 {} 3 {} 2 {} 1 {} + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 18 54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 17 16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 16 85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 15 22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 14 40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 13 4.87.65.5.31.49.2.26.72.13.36.10.83 + 12 87.65.5.31.49.2.26.72.13.36.10.83 11 65.5.31.49.2.26.72.13.36.10.83 + 10 5.31.49.2.26.72.13.36.10.83 9 31.49.2.26.72.13.36.10.83 + 8 49.2.26.72.13.36.10.83 7 2.26.72.13.36.10.83 6 26.72.13.36.10.83 + 5 72.13.36.10.83 4 13.36.10.83 3 36.10.83 2 10.83 1 83 19 {} + 18 {} 17 {} 16 {} 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} + 9 {} 8 {} 7 {} 6 {} 5 {} 4 {} 3 {} 2 {} 1 {} + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 18 8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 17 65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 16 85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 15 55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 14 56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 13 42.80.58.11.95.90.85.47.33.14.3.33.82 + 12 80.58.11.95.90.85.47.33.14.3.33.82 11 58.11.95.90.85.47.33.14.3.33.82 + 10 11.95.90.85.47.33.14.3.33.82 9 95.90.85.47.33.14.3.33.82 + 8 90.85.47.33.14.3.33.82 7 85.47.33.14.3.33.82 6 47.33.14.3.33.82 + 5 33.14.3.33.82 4 14.3.33.82 3 3.33.82 2 33.82 1 82 19 {} 18 {} + 17 {} 16 {} 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} + 7 {} 6 {} 5 {} 4 {} 3 {} 2 {} 1 {}} + +do_execsql_test 1.5.2.1 { + SELECT max(b) OVER ( ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2 +} {{} {} {} {} 89 89 96 96 96 96 96 96 96 96 96 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99} + +do_execsql_test 1.5.2.2 { + SELECT min(b) OVER ( ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2 +} {{} {} {} {} 89 81 81 59 38 38 38 38 38 38 6 6 + 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 + 6 6 6 6 6 6 6 6 6 6 6 6 6 3 3 3 3 3 3 + 3 3 3 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1} + +do_execsql_test 1.5.3.1 { + SELECT row_number() OVER ( ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.5.3.2 { + SELECT row_number() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.5.3.3 { + SELECT row_number() OVER ( ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.5.4.1 { + SELECT dense_rank() OVER ( ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.5.4.2 { + SELECT dense_rank() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.5.4.3 { + SELECT dense_rank() OVER ( ORDER BY b ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2 +} {1 1 2 2 3 3 4 5 6 7 7 7 8 8 8 9 9 9 10 + 11 11 12 12 12 13 13 14 15 15 15 16 16 16 17 + 18 19 20 20 21 21 22 22 23 24 25 25 26 26 27 + 28 28 28 29 29 29 30 30 31 32 32 32 32 33 33 + 33 33 34 34 35 35 35 35 36 36 37 37 38 38 38 + 39 40 40 41 42 42 43 43 44 44 45 45 45 45 46 + 47 48 49 50 51 52 52 53 53 53 54 55 55 55 55 + 56 56 56 56 57 58 58 59 59 60 61 62 62 62 63 + 64 65 66 67 68 68 68 69 69 69 70 70 70 71 71 + 71 72 73 73 74 74 75 76 76 77 77 77 78 79 80 + 80 80 80 81 81 81 82 83 83 84 85 85 85 86 86 + 86 87 87 87 87 87 88 88 88 89 90 90 90 91 91 + 91 92 92 93 93 94 94} + +do_execsql_test 1.5.4.4 { + SELECT dense_rank() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2 +} {1 2 3 3 3 4 5 6 7 8 8 9 9 9 1 1 2 2 3 + 3 4 4 5 5 6 7 7 8 8 8 9 9 9 9 9 1 1 2 + 2 2 3 3 4 5 6 7 7 8 8 8 9 1 1 2 2 3 3 + 4 4 4 4 5 5 6 7 8 8 8 9 10 10 10 1 2 3 + 4 4 4 4 5 5 6 7 8 8 8 9 9 9 9 10 1 2 2 + 2 3 4 4 5 5 6 6 6 7 7 7 8 8 8 9 9 9 1 + 2 2 2 3 3 4 4 4 4 5 5 6 6 6 7 8 9 10 10 + 10 1 1 1 2 3 3 4 4 5 5 5 5 6 7 8 8 9 9 + 10 10 1 1 1 2 3 3 4 4 4 4 5 6 6 7 8 8 1 + 1 1 2 3 3 3 4 4 4 5 6 6 6 6 7 8 9 9 9 + 10 10} + +do_execsql_test 1.5.4.5 { + SELECT dense_rank() OVER ( ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 + 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 + 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 + 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 + 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 + 8 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 10 10 + 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 + 10 10 10 10 10} + +do_execsql_test 1.5.4.6 { + SELECT dense_rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 + 5 5 5 5 5 5 5 5 5 5 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 + 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 + 5} + +do_execsql_test 1.5.5.1 { + SELECT rank() OVER ( ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.5.5.2 { + SELECT rank() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.5.5.3 { + SELECT rank() OVER ( ORDER BY b ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2 +} {1 1 3 3 5 5 7 8 9 10 10 10 13 13 13 16 16 + 16 19 20 20 22 22 22 25 25 27 28 28 28 31 31 + 31 34 35 36 37 37 39 39 41 41 43 44 45 45 47 + 47 49 50 50 50 53 53 53 56 56 58 59 59 59 59 + 63 63 63 63 67 67 69 69 69 69 73 73 75 75 77 + 77 77 80 81 81 83 84 84 86 86 88 88 90 90 90 + 90 94 95 96 97 98 99 100 100 102 102 102 105 106 + 106 106 106 110 110 110 110 114 115 115 117 117 119 + 120 121 121 121 124 125 126 127 128 129 129 129 132 + 132 132 135 135 135 138 138 138 141 142 142 144 144 + 146 147 147 149 149 149 152 153 154 154 154 154 158 + 158 158 161 162 162 164 165 165 165 168 168 168 171 + 171 171 171 171 176 176 176 179 180 180 180 183 183 + 183 186 186 188 188 190 190} + +do_execsql_test 1.5.5.4 { + SELECT rank() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2 +} {1 2 3 3 3 6 7 8 9 10 10 12 12 12 1 1 3 3 + 5 5 7 7 9 9 11 12 12 14 14 14 17 17 17 17 + 17 1 1 3 3 3 6 6 8 9 10 11 11 13 13 13 16 + 1 1 3 3 5 5 7 7 7 7 11 11 13 14 15 15 15 + 18 19 19 19 1 2 3 4 4 4 4 8 8 10 11 12 12 + 12 15 15 15 15 19 1 2 2 2 5 6 6 8 8 10 10 + 10 13 13 13 16 16 16 19 19 19 1 2 2 2 5 5 7 + 7 7 7 11 11 13 13 13 16 17 18 19 19 19 1 1 + 1 4 5 5 7 7 9 9 9 9 13 14 15 15 17 17 19 + 19 1 1 1 4 5 5 7 7 7 7 11 12 12 14 15 15 + 1 1 1 4 5 5 5 8 8 8 11 12 12 12 12 16 17 + 18 18 18 21 21} + +do_execsql_test 1.5.5.5 { + SELECT rank() OVER ( ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 15 15 15 15 + 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 + 15 15 36 36 36 36 36 36 36 36 36 36 36 36 36 + 36 36 36 52 52 52 52 52 52 52 52 52 52 52 52 + 52 52 52 52 52 52 52 52 52 73 73 73 73 73 73 + 73 73 73 73 73 73 73 73 73 73 73 73 73 92 92 + 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 + 92 92 92 92 113 113 113 113 113 113 113 113 113 + 113 113 113 113 113 113 113 113 113 113 113 113 134 + 134 134 134 134 134 134 134 134 134 134 134 134 134 + 134 134 134 134 134 134 154 154 154 154 154 154 154 + 154 154 154 154 154 154 154 154 154 170 170 170 170 + 170 170 170 170 170 170 170 170 170 170 170 170 170 + 170 170 170 170 170} + +do_execsql_test 1.5.5.6 { + SELECT rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 15 15 15 15 + 15 15 15 15 15 15 15 15 15 15 15 15 31 31 31 + 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 + 31 50 50 50 50 50 50 50 50 50 50 50 50 50 50 + 50 50 50 50 50 50 50 71 71 71 71 71 71 71 71 + 71 71 71 71 71 71 71 71 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 22 22 22 22 22 22 + 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 + 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 + 43 43 43 43 43 43 64 64 64 64 64 64 64 64 64 + 64 64 64 64 64 64 64 64 64 64 64 84 84 84 84 + 84 84 84 84 84 84 84 84 84 84 84 84 84 84 84 + 84 84 84} + +do_execsql_test 1.5.6.1 { + SELECT + row_number() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ), + rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ), + dense_rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) + FROM t2 +} {1 1 1 2 1 1 3 1 1 4 1 1 5 1 1 6 1 1 7 1 1 8 1 1 9 1 1 + 10 1 1 11 1 1 12 1 1 13 1 1 14 1 1 15 15 2 16 15 2 17 15 2 + 18 15 2 19 15 2 20 15 2 21 15 2 22 15 2 23 15 2 24 15 2 + 25 15 2 26 15 2 27 15 2 28 15 2 29 15 2 30 15 2 31 31 3 + 32 31 3 33 31 3 34 31 3 35 31 3 36 31 3 37 31 3 38 31 3 + 39 31 3 40 31 3 41 31 3 42 31 3 43 31 3 44 31 3 45 31 3 + 46 31 3 47 31 3 48 31 3 49 31 3 50 50 4 51 50 4 52 50 4 + 53 50 4 54 50 4 55 50 4 56 50 4 57 50 4 58 50 4 59 50 4 + 60 50 4 61 50 4 62 50 4 63 50 4 64 50 4 65 50 4 66 50 4 + 67 50 4 68 50 4 69 50 4 70 50 4 71 71 5 72 71 5 73 71 5 + 74 71 5 75 71 5 76 71 5 77 71 5 78 71 5 79 71 5 80 71 5 + 81 71 5 82 71 5 83 71 5 84 71 5 85 71 5 86 71 5 1 1 1 2 1 1 + 3 1 1 4 1 1 5 1 1 6 1 1 7 1 1 8 1 1 9 1 1 10 1 1 11 1 1 + 12 1 1 13 1 1 14 1 1 15 1 1 16 1 1 17 1 1 18 1 1 19 1 1 + 20 1 1 21 1 1 22 22 2 23 22 2 24 22 2 25 22 2 26 22 2 27 22 2 + 28 22 2 29 22 2 30 22 2 31 22 2 32 22 2 33 22 2 34 22 2 + 35 22 2 36 22 2 37 22 2 38 22 2 39 22 2 40 22 2 41 22 2 + 42 22 2 43 43 3 44 43 3 45 43 3 46 43 3 47 43 3 48 43 3 + 49 43 3 50 43 3 51 43 3 52 43 3 53 43 3 54 43 3 55 43 3 + 56 43 3 57 43 3 58 43 3 59 43 3 60 43 3 61 43 3 62 43 3 + 63 43 3 64 64 4 65 64 4 66 64 4 67 64 4 68 64 4 69 64 4 + 70 64 4 71 64 4 72 64 4 73 64 4 74 64 4 75 64 4 76 64 4 + 77 64 4 78 64 4 79 64 4 80 64 4 81 64 4 82 64 4 83 64 4 + 84 84 5 85 84 5 86 84 5 87 84 5 88 84 5 89 84 5 90 84 5 + 91 84 5 92 84 5 93 84 5 94 84 5 95 84 5 96 84 5 97 84 5 + 98 84 5 99 84 5 100 84 5 101 84 5 102 84 5 103 84 5 104 84 5 + 105 84 5} + + +do_test 1.5.7.1 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0053 0.0105 0.0158 0.0211 0.0263 0.0316 0.0368 0.0421 0.0474 0.0526 0.0579 0.0632 0.0684 0.0737 0.0789 0.0842 0.0895 0.0947 0.1000 0.1053 0.1105 0.1158 0.1211 0.1263 0.1316 0.1368 0.1421 0.1474 0.1526 0.1579 0.1632 0.1684 0.1737 0.1789 0.1842 0.1895 0.1947 0.2000 0.2053 0.2105 0.2158 0.2211 0.2263 0.2316 0.2368 0.2421 0.2474 0.2526 0.2579 0.2632 0.2684 0.2737 0.2789 0.2842 0.2895 0.2947 0.3000 0.3053 0.3105 0.3158 0.3211 0.3263 0.3316 0.3368 0.3421 0.3474 0.3526 0.3579 0.3632 0.3684 0.3737 0.3789 0.3842 0.3895 0.3947 0.4000 0.4053 0.4105 0.4158 0.4211 0.4263 0.4316 0.4368 0.4421 0.4474 0.4526 0.4579 0.4632 0.4684 0.4737 0.4789 0.4842 0.4895 0.4947 0.5000 0.5053 0.5105 0.5158 0.5211 0.5263 0.5316 0.5368 0.5421 0.5474 0.5526 0.5579 0.5632 0.5684 0.5737 0.5789 0.5842 0.5895 0.5947 0.6000 0.6053 0.6105 0.6158 0.6211 0.6263 0.6316 0.6368 0.6421 0.6474 0.6526 0.6579 0.6632 0.6684 0.6737 0.6789 0.6842 0.6895 0.6947 0.7000 0.7053 0.7105 0.7158 0.7211 0.7263 0.7316 0.7368 0.7421 0.7474 0.7526 0.7579 0.7632 0.7684 0.7737 0.7789 0.7842 0.7895 0.7947 0.8000 0.8053 0.8105 0.8158 0.8211 0.8263 0.8316 0.8368 0.8421 0.8474 0.8526 0.8579 0.8632 0.8684 0.8737 0.8789 0.8842 0.8895 0.8947 0.9000 0.9053 0.9105 0.9158 0.9211 0.9263 0.9316 0.9368 0.9421 0.9474 0.9526 0.9579 0.9632 0.9684 0.9737 0.9789 0.9842 0.9895 0.9947 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.5.7.2 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0769 0.1538 0.2308 0.3077 0.3846 0.4615 0.5385 0.6154 0.6923 0.7692 0.8462 0.9231 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0667 0.1333 0.2000 0.2667 0.3333 0.4000 0.4667 0.5333 0.6000 0.6667 0.7333 0.8000 0.8667 0.9333 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0556 0.1111 0.1667 0.2222 0.2778 0.3333 0.3889 0.4444 0.5000 0.5556 0.6111 0.6667 0.7222 0.7778 0.8333 0.8889 0.9444 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0526 0.1053 0.1579 0.2105 0.2632 0.3158 0.3684 0.4211 0.4737 0.5263 0.5789 0.6316 0.6842 0.7368 0.7895 0.8421 0.8947 0.9474 1.0000 0.0000 0.0667 0.1333 0.2000 0.2667 0.3333 0.4000 0.4667 0.5333 0.6000 0.6667 0.7333 0.8000 0.8667 0.9333 1.0000 0.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.5.7.3 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY b ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0105 0.0105 0.0211 0.0211 0.0316 0.0368 0.0421 0.0474 0.0474 0.0474 0.0632 0.0632 0.0632 0.0789 0.0789 0.0789 0.0947 0.1000 0.1000 0.1105 0.1105 0.1105 0.1263 0.1263 0.1368 0.1421 0.1421 0.1421 0.1579 0.1579 0.1579 0.1737 0.1789 0.1842 0.1895 0.1895 0.2000 0.2000 0.2105 0.2105 0.2211 0.2263 0.2316 0.2316 0.2421 0.2421 0.2526 0.2579 0.2579 0.2579 0.2737 0.2737 0.2737 0.2895 0.2895 0.3000 0.3053 0.3053 0.3053 0.3053 0.3263 0.3263 0.3263 0.3263 0.3474 0.3474 0.3579 0.3579 0.3579 0.3579 0.3789 0.3789 0.3895 0.3895 0.4000 0.4000 0.4000 0.4158 0.4211 0.4211 0.4316 0.4368 0.4368 0.4474 0.4474 0.4579 0.4579 0.4684 0.4684 0.4684 0.4684 0.4895 0.4947 0.5000 0.5053 0.5105 0.5158 0.5211 0.5211 0.5316 0.5316 0.5316 0.5474 0.5526 0.5526 0.5526 0.5526 0.5737 0.5737 0.5737 0.5737 0.5947 0.6000 0.6000 0.6105 0.6105 0.6211 0.6263 0.6316 0.6316 0.6316 0.6474 0.6526 0.6579 0.6632 0.6684 0.6737 0.6737 0.6737 0.6895 0.6895 0.6895 0.7053 0.7053 0.7053 0.7211 0.7211 0.7211 0.7368 0.7421 0.7421 0.7526 0.7526 0.7632 0.7684 0.7684 0.7789 0.7789 0.7789 0.7947 0.8000 0.8053 0.8053 0.8053 0.8053 0.8263 0.8263 0.8263 0.8421 0.8474 0.8474 0.8579 0.8632 0.8632 0.8632 0.8789 0.8789 0.8789 0.8947 0.8947 0.8947 0.8947 0.8947 0.9211 0.9211 0.9211 0.9368 0.9421 0.9421 0.9421 0.9579 0.9579 0.9579 0.9737 0.9737 0.9842 0.9842 0.9947 0.9947} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.5.7.4 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0769 0.1538 0.1538 0.1538 0.3846 0.4615 0.5385 0.6154 0.6923 0.6923 0.8462 0.8462 0.8462 0.0000 0.0000 0.1000 0.1000 0.2000 0.2000 0.3000 0.3000 0.4000 0.4000 0.5000 0.5500 0.5500 0.6500 0.6500 0.6500 0.8000 0.8000 0.8000 0.8000 0.8000 0.0000 0.0000 0.1333 0.1333 0.1333 0.3333 0.3333 0.4667 0.5333 0.6000 0.6667 0.6667 0.8000 0.8000 0.8000 1.0000 0.0000 0.0000 0.1000 0.1000 0.2000 0.2000 0.3000 0.3000 0.3000 0.3000 0.5000 0.5000 0.6000 0.6500 0.7000 0.7000 0.7000 0.8500 0.9000 0.9000 0.9000 0.0000 0.0556 0.1111 0.1667 0.1667 0.1667 0.1667 0.3889 0.3889 0.5000 0.5556 0.6111 0.6111 0.6111 0.7778 0.7778 0.7778 0.7778 1.0000 0.0000 0.0500 0.0500 0.0500 0.2000 0.2500 0.2500 0.3500 0.3500 0.4500 0.4500 0.4500 0.6000 0.6000 0.6000 0.7500 0.7500 0.7500 0.9000 0.9000 0.9000 0.0000 0.0500 0.0500 0.0500 0.2000 0.2000 0.3000 0.3000 0.3000 0.3000 0.5000 0.5000 0.6000 0.6000 0.6000 0.7500 0.8000 0.8500 0.9000 0.9000 0.9000 0.0000 0.0000 0.0000 0.1579 0.2105 0.2105 0.3158 0.3158 0.4211 0.4211 0.4211 0.4211 0.6316 0.6842 0.7368 0.7368 0.8421 0.8421 0.9474 0.9474 0.0000 0.0000 0.0000 0.2000 0.2667 0.2667 0.4000 0.4000 0.4000 0.4000 0.6667 0.7333 0.7333 0.8667 0.9333 0.9333 0.0000 0.0000 0.0000 0.1429 0.1905 0.1905 0.1905 0.3333 0.3333 0.3333 0.4762 0.5238 0.5238 0.5238 0.5238 0.7143 0.7619 0.8095 0.8095 0.8095 0.9524 0.9524} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.5.7.5 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.5.7.6 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER (PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.5.8.1 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0052 0.0105 0.0157 0.0209 0.0262 0.0314 0.0366 0.0419 0.0471 0.0524 0.0576 0.0628 0.0681 0.0733 0.0785 0.0838 0.0890 0.0942 0.0995 0.1047 0.1099 0.1152 0.1204 0.1257 0.1309 0.1361 0.1414 0.1466 0.1518 0.1571 0.1623 0.1675 0.1728 0.1780 0.1832 0.1885 0.1937 0.1990 0.2042 0.2094 0.2147 0.2199 0.2251 0.2304 0.2356 0.2408 0.2461 0.2513 0.2565 0.2618 0.2670 0.2723 0.2775 0.2827 0.2880 0.2932 0.2984 0.3037 0.3089 0.3141 0.3194 0.3246 0.3298 0.3351 0.3403 0.3455 0.3508 0.3560 0.3613 0.3665 0.3717 0.3770 0.3822 0.3874 0.3927 0.3979 0.4031 0.4084 0.4136 0.4188 0.4241 0.4293 0.4346 0.4398 0.4450 0.4503 0.4555 0.4607 0.4660 0.4712 0.4764 0.4817 0.4869 0.4921 0.4974 0.5026 0.5079 0.5131 0.5183 0.5236 0.5288 0.5340 0.5393 0.5445 0.5497 0.5550 0.5602 0.5654 0.5707 0.5759 0.5812 0.5864 0.5916 0.5969 0.6021 0.6073 0.6126 0.6178 0.6230 0.6283 0.6335 0.6387 0.6440 0.6492 0.6545 0.6597 0.6649 0.6702 0.6754 0.6806 0.6859 0.6911 0.6963 0.7016 0.7068 0.7120 0.7173 0.7225 0.7277 0.7330 0.7382 0.7435 0.7487 0.7539 0.7592 0.7644 0.7696 0.7749 0.7801 0.7853 0.7906 0.7958 0.8010 0.8063 0.8115 0.8168 0.8220 0.8272 0.8325 0.8377 0.8429 0.8482 0.8534 0.8586 0.8639 0.8691 0.8743 0.8796 0.8848 0.8901 0.8953 0.9005 0.9058 0.9110 0.9162 0.9215 0.9267 0.9319 0.9372 0.9424 0.9476 0.9529 0.9581 0.9634 0.9686 0.9738 0.9791 0.9843 0.9895 0.9948 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.5.8.2 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0714 0.1429 0.2143 0.2857 0.3571 0.4286 0.5000 0.5714 0.6429 0.7143 0.7857 0.8571 0.9286 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0625 0.1250 0.1875 0.2500 0.3125 0.3750 0.4375 0.5000 0.5625 0.6250 0.6875 0.7500 0.8125 0.8750 0.9375 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0526 0.1053 0.1579 0.2105 0.2632 0.3158 0.3684 0.4211 0.4737 0.5263 0.5789 0.6316 0.6842 0.7368 0.7895 0.8421 0.8947 0.9474 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0625 0.1250 0.1875 0.2500 0.3125 0.3750 0.4375 0.5000 0.5625 0.6250 0.6875 0.7500 0.8125 0.8750 0.9375 1.0000 0.0455 0.0909 0.1364 0.1818 0.2273 0.2727 0.3182 0.3636 0.4091 0.4545 0.5000 0.5455 0.5909 0.6364 0.6818 0.7273 0.7727 0.8182 0.8636 0.9091 0.9545 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.5.8.3 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY b ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0105 0.0105 0.0209 0.0209 0.0314 0.0314 0.0366 0.0419 0.0471 0.0628 0.0628 0.0628 0.0785 0.0785 0.0785 0.0942 0.0942 0.0942 0.0995 0.1099 0.1099 0.1257 0.1257 0.1257 0.1361 0.1361 0.1414 0.1571 0.1571 0.1571 0.1728 0.1728 0.1728 0.1780 0.1832 0.1885 0.1990 0.1990 0.2094 0.2094 0.2199 0.2199 0.2251 0.2304 0.2408 0.2408 0.2513 0.2513 0.2565 0.2723 0.2723 0.2723 0.2880 0.2880 0.2880 0.2984 0.2984 0.3037 0.3246 0.3246 0.3246 0.3246 0.3455 0.3455 0.3455 0.3455 0.3560 0.3560 0.3770 0.3770 0.3770 0.3770 0.3874 0.3874 0.3979 0.3979 0.4136 0.4136 0.4136 0.4188 0.4293 0.4293 0.4346 0.4450 0.4450 0.4555 0.4555 0.4660 0.4660 0.4869 0.4869 0.4869 0.4869 0.4921 0.4974 0.5026 0.5079 0.5131 0.5183 0.5288 0.5288 0.5445 0.5445 0.5445 0.5497 0.5707 0.5707 0.5707 0.5707 0.5916 0.5916 0.5916 0.5916 0.5969 0.6073 0.6073 0.6178 0.6178 0.6230 0.6283 0.6440 0.6440 0.6440 0.6492 0.6545 0.6597 0.6649 0.6702 0.6859 0.6859 0.6859 0.7016 0.7016 0.7016 0.7173 0.7173 0.7173 0.7330 0.7330 0.7330 0.7382 0.7487 0.7487 0.7592 0.7592 0.7644 0.7749 0.7749 0.7906 0.7906 0.7906 0.7958 0.8010 0.8220 0.8220 0.8220 0.8220 0.8377 0.8377 0.8377 0.8429 0.8534 0.8534 0.8586 0.8743 0.8743 0.8743 0.8901 0.8901 0.8901 0.9162 0.9162 0.9162 0.9162 0.9162 0.9319 0.9319 0.9319 0.9372 0.9529 0.9529 0.9529 0.9686 0.9686 0.9686 0.9791 0.9791 0.9895 0.9895 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.5.8.4 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0714 0.1429 0.3571 0.3571 0.3571 0.4286 0.5000 0.5714 0.6429 0.7857 0.7857 1.0000 1.0000 1.0000 0.0952 0.0952 0.1905 0.1905 0.2857 0.2857 0.3810 0.3810 0.4762 0.4762 0.5238 0.6190 0.6190 0.7619 0.7619 0.7619 1.0000 1.0000 1.0000 1.0000 1.0000 0.1250 0.1250 0.3125 0.3125 0.3125 0.4375 0.4375 0.5000 0.5625 0.6250 0.7500 0.7500 0.9375 0.9375 0.9375 1.0000 0.0952 0.0952 0.1905 0.1905 0.2857 0.2857 0.4762 0.4762 0.4762 0.4762 0.5714 0.5714 0.6190 0.6667 0.8095 0.8095 0.8095 0.8571 1.0000 1.0000 1.0000 0.0526 0.1053 0.1579 0.3684 0.3684 0.3684 0.3684 0.4737 0.4737 0.5263 0.5789 0.7368 0.7368 0.7368 0.9474 0.9474 0.9474 0.9474 1.0000 0.0476 0.1905 0.1905 0.1905 0.2381 0.3333 0.3333 0.4286 0.4286 0.5714 0.5714 0.5714 0.7143 0.7143 0.7143 0.8571 0.8571 0.8571 1.0000 1.0000 1.0000 0.0476 0.1905 0.1905 0.1905 0.2857 0.2857 0.4762 0.4762 0.4762 0.4762 0.5714 0.5714 0.7143 0.7143 0.7143 0.7619 0.8095 0.8571 1.0000 1.0000 1.0000 0.1500 0.1500 0.1500 0.2000 0.3000 0.3000 0.4000 0.4000 0.6000 0.6000 0.6000 0.6000 0.6500 0.7000 0.8000 0.8000 0.9000 0.9000 1.0000 1.0000 0.1875 0.1875 0.1875 0.2500 0.3750 0.3750 0.6250 0.6250 0.6250 0.6250 0.6875 0.8125 0.8125 0.8750 1.0000 1.0000 0.1364 0.1364 0.1364 0.1818 0.3182 0.3182 0.3182 0.4545 0.4545 0.4545 0.5000 0.6818 0.6818 0.6818 0.6818 0.7273 0.7727 0.9091 0.9091 0.9091 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.5.8.5 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.5.8.6 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.5.8.1 { + set myres {} + foreach r [db eval {SELECT ntile(100) OVER ( ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 88.0000 89.0000 89.0000 90.0000 90.0000 91.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.5.8.2 { + set myres {} + foreach r [db eval {SELECT ntile(101) OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 22.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.5.8.3 { + set myres {} + foreach r [db eval {SELECT ntile(102) OVER ( ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 88.0000 89.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.5.8.4 { + set myres {} + foreach r [db eval {SELECT ntile(103) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 22.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.5.8.5 { + set myres {} + foreach r [db eval {SELECT ntile(104) OVER ( ORDER BY b%10,a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000 103.0000 104.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.5.8.6 { + set myres {} + foreach r [db eval {SELECT ntile(105) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.5.8.7 { + set myres {} + foreach r [db eval {SELECT ntile(105) OVER ( ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 88.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000 103.0000 104.0000 105.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + +do_execsql_test 1.5.9.1 { + SELECT last_value(a+b) OVER ( ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2 +} {{} {} {} {} 99 92 108 72 52 83 55 79 109 65 26 + 120 119 50 70 103 80 124 36 96 59 124 116 110 57 + 51 52 130 103 74 87 48 128 117 105 136 131 71 133 + 92 109 63 84 109 57 146 78 147 113 74 88 150 87 + 110 65 121 106 110 124 85 145 107 161 171 150 156 + 80 171 120 109 158 114 111 136 147 87 173 124 168 + 173 162 132 101 154 167 190 161 110 156 195 198 102 + 123 177 169 140 111 180 119 160 197 152 124 121 134 + 146 147 132 213 141 193 200 210 157 132 136 175 161 + 218 188 226 191 187 208 211 179 138 144 223 196 214 + 170 212 202 163 184 172 173 195 229 240 187 210 200 + 163 227 228 223 191 252 235 225 243 172 187 202 179 + 179 182 231 261 207 263 206 189 209 212 276 181 274 + 249 239 234 213 234 269 196 271 221 210 229 235 250 + 223 232 229 279} + +do_execsql_test 1.5.9.2 { + SELECT last_value(a+b) OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2 +} {{} {} {} {} 147 106 109 168 134 218 191 212 229 + 240 {} {} {} {} 92 109 105 136 146 65 156 132 154 + 102 123 119 160 152 146 147 136 {} {} {} {} 79 63 + 84 78 120 87 162 124 141 138 227 228 {} {} {} {} + 124 57 130 92 57 110 114 136 147 167 110 180 193 + 191 252 187 179 {} {} {} {} 80 116 117 71 80 171 + 173 177 157 161 179 214 225 182 209 {} {} {} {} + 103 74 131 133 113 74 87 145 190 161 169 140 111 + 132 213 187 208 {} {} {} {} 108 65 26 70 51 52 + 128 109 121 124 85 107 150 195 226 172 173 {} {} + {} {} 119 50 124 96 110 87 48 110 173 124 197 211 + 144 196 195 200 {} {} {} {} 52 83 103 36 88 171 + 158 156 198 121 210 132 {} {} {} {} 99 72 55 120 + 59 109 150 161 111 101 200 175 188 170 202 163 184 + 163} + +do_execsql_test 1.5.9.3 { + SELECT last_value(a+b) OVER ( ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2 +} {{} {} {} {} 65 102 87 138 57 181 80 111 26 48 + 144 207 36 121 132 101 163 172 196 119 136 63 124 + 179 110 179 182 74 132 189 51 52 85 216 163 134 + 123 210 78 141 57 187 71 87 172 173 50 224 88 59 + 111 170 109 213 223 146 147 84 114 191 206 221 157 + 161 209 229 74 140 107 187 207 212 124 202 52 232 + 55 184 229 106 132 152 120 92 110 179 235 65 70 + 87 110 195 200 175 234 160 234 136 80 113 187 109 + 121 124 196 156 210 239 250 72 109 188 202 191 105 + 154 79 231 147 225 103 161 169 223 96 83 249 212 + 162 227 228 167 180 193 117 177 214 145 208 235 150 + 110 211 103 158 200 168 229 92 156 243 280 279 116 + 173 269 271 131 133 223 128 173 197 210 99 150 161 + 147 218 240 109 136 146 261 263 124 130 252 171 190 + 213 274 108 195 226 119 124} + +do_execsql_test 1.5.9.4 { + SELECT last_value(a+b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2 +} {{} {} {} {} 196 134 109 213 223 106 234 191 212 + 168 {} {} {} {} 65 102 119 136 123 210 146 147 + 132 152 160 105 154 92 156 243 109 {} {} {} {} 87 + 138 63 124 179 78 141 84 120 234 79 231 {} {} {} + {} 57 181 110 179 57 187 114 191 206 221 92 110 + 136 147 167 180 193 {} {} {} {} 80 182 71 157 161 + 209 229 179 235 80 225 117 177 214 116 {} {} {} + {} 111 74 132 189 87 74 140 113 187 103 161 169 + 145 208 235 131 133 {} {} {} {} 26 51 52 85 172 + 173 107 187 207 212 65 70 109 121 124 223 150 {} + {} {} {} 48 144 207 216 50 224 124 202 87 110 195 + 200 196 96 110 211 {} {} {} {} 36 121 132 88 52 + 232 156 210 239 250 83 103 {} {} {} {} 101 163 + 172 163 59 111 170 55 184 229 175 72 109 188 202 + 249 200 99} + +do_execsql_test 1.5.9.5 { + SELECT last_value(a+b) OVER ( ORDER BY b%10,a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2 +} {{} {} {} {} 147 106 109 168 134 218 191 212 229 + 240 213 234 196 223 92 109 105 136 146 65 156 132 + 154 102 123 119 160 152 146 147 136 243 261 263 210 + 79 63 84 78 120 87 162 124 141 138 227 228 179 + 231 234 280 124 57 130 92 57 110 114 136 147 167 + 110 180 193 191 252 187 179 206 181 221 279 80 116 + 117 71 80 171 173 177 157 161 179 214 225 182 209 + 269 271 235 229 103 74 131 133 113 74 87 145 190 + 161 169 140 111 132 213 187 208 223 235 189 274 108 + 65 26 70 51 52 128 109 121 124 85 107 150 195 226 + 172 173 187 223 207 212 119 50 124 96 110 87 48 + 110 173 124 197 211 144 196 195 200 202 224 216 207 + 52 83 103 36 88 171 158 156 198 121 210 132 210 + 239 250 232 99 72 55 120 59 109 150 161 111 101 + 200 175 188 170 202 163 184 163} + +do_execsql_test 1.5.9.6 { + SELECT last_value(a+b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.5.10.1 { + SELECT nth_value(b,b+1) OVER (ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} 39 {} {} {} {} + {} {} {} 91 {} {} {} {} {} {} 54 54 {} {} {} + {} 62 {} {} {} {} {} 23 {} {} {} 97 86 {} 59 + {} 84 {} {} 78 65 {} 16 90 81 {} 59 56 {} 54 + {} 85 {} {} {} {} 38 {} 32 47 {} 74 35 47 98 + 96 {} 24 {} {} 29 12 46 36 53 {} 81 27 56 {} + {} 81 93 63 81 91 68 53 99 89 13 12 97 91 29 + 7 7 78 35 84 53 84 58 61 91 99 15 61 98 16 5 + 75 56 2 37 3 96 62 95 43 63 35 78 16 67 43 16 + 16 90 72 98 85 56 90 46 29 29 4 74 74 2 76 41 + 46 77 24 27 97 46 89 1 85 1 74 78 61 85 51 59 + 35 30 56 25 47 28 73 6 73 74 93 43 3 56 47 85 + 61 61 93 9 97 62} + +do_execsql_test 1.5.10.2 { + SELECT nth_value(b,b+1) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 91 {} {} {} 91 {} {} {} {} {} {} + 11 {} {} {} {} {} {} {} {} {} {} {} {} {} 32 + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} 43 {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 15 {} + {} {} {} {} {} 55 {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} 47 {} {} {} {} + {} {} 47 {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} 9 9 {} {} {}} + +do_execsql_test 1.5.10.3 { + SELECT nth_value(b,b+1) OVER ( ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} 5 6 6 6 7 + 7 7 7 7 7 8 8 8 8 8 8 9 9 9 9 9 9 9 11 + 11 12 12 12 12 12 12 13 13 14 14 15 15 15 15 + 15 15 16 16 16 16 16 16 17 17 17 17 19 19 19 + 19 20 20 21 21 21 21 21 21 22 22 22 22 22 23 + 23 23 24 25 25 26 26 27 27 27 27 27 27 29 29 + 29 30 30 30 31 31 31 31 31 32 33 33 33 33 33 + 33 33 33 33 33 33 34 34 34 34 34 34 34 35 35 + 36 36 36 37 37 37 37 37 37 38 38 38 38 38 38 + 39 39 39 39 39 40 41 41 41 41 41 42 43 43 43 + 43 43 44 44 44 44 46 46 46 47 47 47 47 47 47 + 47 47 47 47 47 49 49 49 50 51 51 51 52 52 52 + 53 53 54 54 55 55} + +do_execsql_test 1.5.10.4 { + SELECT nth_value(b,b+1) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.5.10.5 { + SELECT nth_value(b,b+1) OVER ( ORDER BY b%10,a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 40 {} {} {} 40 {} 50 {} {} {} {} + 50 {} {} {} 41 {} 10 91 61 {} 30 {} 10 61 30 + {} {} 10 {} {} {} {} 1 {} 22 80 22 91 93 {} + {} 30 {} {} 91 {} 1 30 91 80 91 {} 43 {} {} + 21 20 {} {} 74 21 21 2 74 33 81 21 {} 64 2 21 + 93 62 14 14 3 91 11 24 55 93 93 62 90 91 55 3 + 24 14 24 91 55 15 72 60 72 61 61 34 43 43 43 + 61 12 4 15 15 51 51 12 23 12 12 25 41 25 13 + 94 12 70 12 84 32 84 94 70 33 12 12 32 41 91 + 70 22 33 84 80 31 75 84 53 75 80 84 80 53 53 + 53 22 44 63 42 95 31 63 44 44 31 90 74 52 63 + 31 63 1 42 90 90 95 3 42} + +do_execsql_test 1.5.10.6 { + SELECT nth_value(b,b+1) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.5.11.1 { + SELECT first_value(b) OVER (ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING) FROM t2 +} {{} {} {} {} 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89} + +do_execsql_test 1.5.11.2 { + SELECT first_value(b) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING) FROM t2 +} {{} {} {} {} 90 90 90 90 90 90 90 90 90 90 {} + {} {} {} 81 81 81 81 81 81 81 81 81 81 81 81 + 81 81 81 81 81 {} {} {} {} 62 62 62 62 62 62 + 62 62 62 62 62 62 {} {} {} {} 93 93 93 93 93 + 93 93 93 93 93 93 93 93 93 93 93 93 {} {} {} + {} 54 54 54 54 54 54 54 54 54 54 54 54 54 54 + 54 {} {} {} {} 65 65 65 65 65 65 65 65 65 65 + 65 65 65 65 65 65 65 {} {} {} {} 96 96 96 96 + 96 96 96 96 96 96 96 96 96 96 96 96 96 {} {} + {} {} 97 97 97 97 97 97 97 97 97 97 97 97 97 + 97 97 97 {} {} {} {} 38 38 38 38 38 38 38 38 + 38 38 38 38 {} {} {} {} 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89} + +do_execsql_test 1.5.11.3 { + SELECT first_value(b) OVER ( ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2 +} {{} {} {} {} 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1} + +do_execsql_test 1.5.11.4 { + SELECT first_value(b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2 +} {{} {} {} {} 10 10 10 10 10 10 10 10 10 10 {} + {} {} {} 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 {} {} {} {} 2 2 2 2 2 2 2 2 2 2 2 2 + {} {} {} {} 3 3 3 3 3 3 3 3 3 3 3 3 3 3 + 3 3 3 {} {} {} {} 4 4 4 4 4 4 4 4 4 4 4 + 4 4 4 4 {} {} {} {} 5 5 5 5 5 5 5 5 5 5 + 5 5 5 5 5 5 5 {} {} {} {} 6 6 6 6 6 6 6 + 6 6 6 6 6 6 6 6 6 6 {} {} {} {} 7 7 7 7 + 7 7 7 7 7 7 7 7 7 7 7 7 {} {} {} {} 8 8 + 8 8 8 8 8 8 8 8 8 8 {} {} {} {} 9 9 9 9 + 9 9 9 9 9 9 9 9 9 9 9 9 9 9} + +do_execsql_test 1.5.11.5 { + SELECT first_value(b) OVER ( ORDER BY b%10,a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2 +} {{} {} {} {} 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90} + +do_execsql_test 1.5.11.6 { + SELECT first_value(b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.5.12.1 { + SELECT lead(b,b) OVER (ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING) FROM t2 +} {96 9 11 89 32 53 91 30 51 56 54 73 22 59 75 + 74 78 8 16 65 15 8 31 87 90 12 32 96 74 76 37 + 85 90 15 35 2 60 36 75 9 51 47 63 51 90 26 42 + 26 8 76 80 90 37 87 56 79 5 87 8 2 39 73 64 + 36 90 72 78 36 73 51 33 20 41 2 26 37 33 8 14 + 33 81 55 1 9 12 39 64 87 72 34 82 21 34 99 62 + 74 41 69 22 75 27 58 8 79 77 26 26 55 {} 29 + 30 7 {} 66 55 2 34 64 {} 33 {} 44 84 {} {} 95 + 85 19 {} 83 {} 91 {} {} 9 50 91 33 34 {} {} + 84 {} 7 9 {} {} {} 44 {} {} {} {} 91 84 {} 95 + 95 52 {} {} {} {} {} 21 {} {} {} 58 {} {} {} + {} {} {} {} 83 {} {} {} {} {} {} {} {} {} {} + {} {} {} {}} + +do_execsql_test 1.5.12.2 { + SELECT lead(b,b) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 81 {} {} {} 21 {} {} {} {} {} {} + {} {} {} {} {} {} 62 {} {} {} 12 {} {} {} 72 + {} {} {} {} {} {} {} {} {} {} 53 {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 34 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} 95 {} {} {} {} {} {} 85 {} + {} {} {} {} {} {} {} {} {} 56 {} 36 {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 57 {} {} {} {} {} 7 {} {} {} {} + {} {} {} {} {} {} 8 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 9 {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.5.12.3 { + SELECT lead(b,b) OVER ( ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2 +} {1 2 3 3 5 6 7 8 8 9 9 10 11 12 12 13 13 + 14 15 16 16 17 19 20 21 22 23 24 25 26 27 27 + 28 29 30 31 32 33 33 33 34 34 35 36 36 36 37 + 38 39 39 40 41 42 43 43 44 46 47 47 47 49 50 + 52 53 54 55 56 56 57 58 58 58 59 59 59 60 61 + 62 62 64 65 65 67 69 70 72 72 73 74 74 75 75 + 75 77 78 80 81 81 83 84 84 85 85 85 87 88 89 + 89 89 90 90 91 91 91 93 93 94 95 95 96 97 97 + 98 99 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.5.12.4 { + SELECT lead(b,b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2 +} {80 {} {} {} {} {} {} {} {} {} {} {} {} {} 1 + 11 81 81 {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} 12 12 72 82 {} {} {} {} {} {} + {} {} {} {} {} {} 13 23 73 73 {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} 34 84 {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 35 85 85 95 {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} 36 86 96 96 {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 37 47 + 47 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} 58 58 68 {} {} {} {} {} {} {} {} {} + {} {} {} {} 39 49 59 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.5.12.5 { + SELECT lead(b,b) OVER ( ORDER BY b%10,a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2 +} {34 72 91 74 21 55 23 94 84 95 2 13 61 22 55 + 55 94 85 75 81 65 73 74 21 72 12 94 33 63 73 + 72 96 36 76 3 25 62 3 73 34 12 46 43 93 72 16 + 86 63 15 65 36 77 24 57 25 53 95 34 95 16 97 + 74 97 67 25 98 44 34 65 54 5 68 96 28 47 95 + 34 39 8 38 6 46 96 28 47 95 56 39 99 97 76 8 + 26 9 79 27 95 16 29 {} 58 58 77 85 56 {} 98 + 29 {} 19 96 {} {} 78 56 98 36 97 {} 89 89 29 + 47 78 {} {} {} 38 68 58 {} 58 38 {} 98 {} {} + {} 39 57 9 {} 79 {} {} 7 {} {} {} 9 29 38 78 + {} {} {} 8 39 {} {} {} {} 59 {} 99 {} {} {} + {} {} {} {} {} {} {} {} {} {} 9 {} {} {} {} + {} {} {} {} {} {} {} {}} + +do_execsql_test 1.5.12.6 { + SELECT lead(b,b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.5.13.1 { + SELECT lag(b,b) OVER (ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} 38 {} {} {} {} + {} {} {} 6 {} {} {} {} {} 81 46 6 {} {} {} {} + 23 {} {} {} {} {} 27 {} {} {} 35 6 {} 12 {} + 23 {} {} 61 84 {} 93 39 47 {} 54 46 96 56 {} + 16 {} {} {} {} 89 {} 16 43 {} 85 56 29 99 53 + {} 59 {} {} 91 59 53 84 99 {} 93 63 47 {} {} + 98 33 67 35 75 1 23 13 55 27 75 98 35 73 63 2 + 21 27 13 24 86 23 84 31 20 94 61 65 75 23 36 + 94 55 90 41 77 96 56 29 40 12 89 63 11 5 73 + 79 1 16 28 31 73 5 39 53 63 41 11 40 2 13 33 + 9 29 90 47 72 9 73 30 44 33 74 93 29 74 42 34 + 63 41 34 96 47 77 1 36 74 72 14 36 26 77 9 72 + 64 8 91 31 52 30} + +do_execsql_test 1.5.13.2 { + SELECT lag(b,b) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} 30 {} {} + {} {} {} {} 91 {} {} {} 61 {} 81 {} {} {} {} + 1 {} {} {} {} {} {} {} {} {} 22 {} {} {} 12 + {} {} 62 {} {} {} {} {} {} {} 23 {} {} {} {} + {} {} {} {} {} {} {} 43 {} 23 {} {} {} {} {} + {} 54 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 75 {} + {} {} {} {} {} 55 {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} 47 {} {} {} {} + {} 27 7 {} {} {} {} {} {} {} {} {} 68 {} 8 {} + {} {} {} {} {} {} {} {} {} {} {} {} 89 {} {} + {} {} {} {} {} 29 9 {} {} {}} + +do_execsql_test 1.5.13.3 { + SELECT lag(b,b) OVER ( ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2 +} {{} 1 1 1 1 2 2 2 2 2 2 3 3 3 4 4 5 6 6 + 6 7 7 7 7 7 8 8 8 8 8 8 9 9 9 9 9 9 9 + 9 9 9 10 10 10 10 11 11 11 11 11 12 12 12 12 + 13 13 13 13 13 14 15 15 15 15 16 16 16 16 16 + 17 19 20 20 21 21 21 21 22 22 22 22 23 23 23 + 23 23 24 23 24 24 25 26 26 26 26 26 26 26 26 + 26 26 26 27 27 27 27 28 29 29 29 29 30 30 30 + 30 30 30 31 31 31 31 31 32 32 32 32 32 32 31 + 32 33 33 33 33 33 33 34 34 34 34 34 34 34 34 + 35 35 35 35 35 36 36 36 36 36 36 36 37 37 37 + 38 38 38 38 38 38 39 39 39 39 40 40 41 41 42 + 43 42 43 43 43 43 44 44 44 46 46 46 47 47 47 + 47 47} + +do_execsql_test 1.5.13.4 { + SELECT lag(b,b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + 1 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.5.13.5 { + SELECT lag(b,b) OVER ( ORDER BY b%10,a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} 30 {} {} + {} {} {} {} 91 {} {} {} 61 80 81 {} {} {} {} + 1 {} {} {} 30 {} 21 90 61 {} 22 {} 11 41 12 + {} {} 62 {} {} {} {} 31 {} 50 23 30 21 90 {} + {} 62 {} {} 81 {} 22 43 62 23 32 {} 91 {} 90 + 93 54 {} {} 90 72 12 22 90 81 83 23 80 20 72 + 43 51 33 80 90 2 34 54 1 20 62 12 13 75 44 30 + 93 91 1 21 55 61 61 13 85 3 65 65 91 73 33 93 + 55 84 62 31 11 65 35 85 33 55 15 12 75 22 3 + 73 65 36 85 43 95 43 13 47 44 65 65 96 36 27 + 7 46 34 94 47 36 73 34 35 73 68 24 8 75 85 75 + 66 34 95 36 84 77 46 34 84 47 89 65 36 16 38 + 76 58 57 29 9 44 56 17} + +do_execsql_test 1.5.13.6 { + SELECT lag(b,b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.5.14.1 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING) FROM t2 +} {{} {} {} {} 89 89.81 89.81.96 89.81.96.59 89.81.96.59.38 + 89.81.96.59.38.68 89.81.96.59.38.68.39 89.81.96.59.38.68.39.62 + 89.81.96.59.38.68.39.62.91 89.81.96.59.38.68.39.62.91.46 + 89.81.96.59.38.68.39.62.91.46.6 89.81.96.59.38.68.39.62.91.46.6.99 + 89.81.96.59.38.68.39.62.91.46.6.99.97 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83} + +do_execsql_test 1.5.14.2 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING) FROM t2 +} {{} {} {} {} 90 90.40 90.40.30 90.40.30.80 90.40.30.80.20 + 90.40.30.80.20.90 90.40.30.80.20.90.60 90.40.30.80.20.90.60.70 + 90.40.30.80.20.90.60.70.80 90.40.30.80.20.90.60.70.80.90 {} {} {} + {} 81 81.91 81.91.61 81.91.61.91 81.91.61.91.91 + 81.91.61.91.91.1 81.91.61.91.91.1.81 81.91.61.91.91.1.81.41 + 81.91.61.91.91.1.81.41.61 81.91.61.91.91.1.81.41.61.1 + 81.91.61.91.91.1.81.41.61.1.21 81.91.61.91.91.1.81.41.61.1.21.11 + 81.91.61.91.91.1.81.41.61.1.21.11.51 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11 {} {} {} {} 62 + 62.12 62.12.32 62.12.32.22 62.12.32.22.42 62.12.32.22.42.2 + 62.12.32.22.42.2.72 62.12.32.22.42.2.72.12 62.12.32.22.42.2.72.12.22 + 62.12.32.22.42.2.72.12.22.2 62.12.32.22.42.2.72.12.22.2.72 + 62.12.32.22.42.2.72.12.22.2.72.72 {} {} {} {} 93 93.23 + 93.23.93 93.23.93.43 93.23.93.43.3 93.23.93.43.3.43 + 93.23.93.43.3.43.33 93.23.93.43.3.43.33.53 93.23.93.43.3.43.33.53.63 + 93.23.93.43.3.43.33.53.63.73 93.23.93.43.3.43.33.53.63.73.13 + 93.23.93.43.3.43.33.53.63.73.13.73 93.23.93.43.3.43.33.53.63.73.13.73.73 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13 {} {} {} {} 54 + 54.84 54.84.74 54.84.74.24 54.84.74.24.4 54.84.74.24.4.94 + 54.84.74.24.4.94.84 54.84.74.24.4.94.84.74 54.84.74.24.4.94.84.74.34 + 54.84.74.24.4.94.84.74.34.34 54.84.74.24.4.94.84.74.34.34.44 + 54.84.74.24.4.94.84.74.34.34.44.74 54.84.74.24.4.94.84.74.34.34.44.74.64 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34 {} {} {} {} 65 + 65.35 65.35.85 65.35.85.85 65.35.85.85.55 65.35.85.85.55.15 + 65.35.85.85.55.15.25 65.35.85.85.55.15.25.75 65.35.85.85.55.15.25.75.95 + 65.35.85.85.55.15.25.75.95.65 65.35.85.85.55.15.25.75.95.65.65 + 65.35.85.85.55.15.25.75.95.65.65.35 65.35.85.85.55.15.25.75.95.65.65.35.5 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75 {} {} {} {} 96 + 96.46 96.46.6 96.46.6.46 96.46.6.46.16 96.46.6.46.16.16 + 96.46.6.46.16.16.86 96.46.6.46.16.16.86.56 96.46.6.46.16.16.86.56.56 + 96.46.6.46.16.16.86.56.56.56 96.46.6.46.16.16.86.56.56.56.16 + 96.46.6.46.16.16.86.56.56.56.16.36 96.46.6.46.16.16.86.56.56.56.16.36.76 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26 {} {} {} {} 97 + 97.27 97.27.97 97.27.97.67 97.27.97.67.77 97.27.97.67.77.47 + 97.27.97.67.77.47.7 97.27.97.67.77.47.7.47 97.27.97.67.77.47.7.47.87 + 97.27.97.67.77.47.7.47.87.37 97.27.97.67.77.47.7.47.87.37.87 + 97.27.97.67.77.47.7.47.87.37.87.77 97.27.97.67.77.47.7.47.87.37.87.77.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47 {} {} {} {} 38 + 38.68 38.68.78 38.68.78.8 38.68.78.8.28 38.68.78.8.28.98 + 38.68.78.8.28.98.78 38.68.78.8.28.98.78.58 38.68.78.8.28.98.78.58.98 + 38.68.78.8.28.98.78.58.98.8 38.68.78.8.28.98.78.58.98.8.88 + 38.68.78.8.28.98.78.58.98.8.88.8 {} {} {} {} 89 89.59 + 89.59.39 89.59.39.99 89.59.39.99.29 89.59.39.99.29.59 + 89.59.39.99.29.59.89 89.59.39.99.29.59.89.89 89.59.39.99.29.59.89.89.29 + 89.59.39.99.29.59.89.89.29.9 89.59.39.99.29.59.89.89.29.9.79 + 89.59.39.99.29.59.89.89.29.9.79.49 89.59.39.99.29.59.89.89.29.9.79.49.59 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9} + +do_execsql_test 1.5.14.3 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2 +} {{} {} {} {} 1 1.1 1.1.2 1.1.2.2 1.1.2.2.3 1.1.2.2.3.3 + 1.1.2.2.3.3.4 1.1.2.2.3.3.4.5 1.1.2.2.3.3.4.5.6 1.1.2.2.3.3.4.5.6.7 + 1.1.2.2.3.3.4.5.6.7.7 1.1.2.2.3.3.4.5.6.7.7.7 1.1.2.2.3.3.4.5.6.7.7.7.8 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97} + +do_execsql_test 1.5.14.4 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2 +} {{} {} {} {} 10 10.20 10.20.30 10.20.30.30 10.20.30.30.30 + 10.20.30.30.30.40 10.20.30.30.30.40.50 10.20.30.30.30.40.50.60 + 10.20.30.30.30.40.50.60.70 10.20.30.30.30.40.50.60.70.80 {} {} {} + {} 1 1.1 1.1.11 1.1.11.11 1.1.11.11.21 1.1.11.11.21.21 + 1.1.11.11.21.21.31 1.1.11.11.21.21.31.31 1.1.11.11.21.21.31.31.41 + 1.1.11.11.21.21.31.31.41.41 1.1.11.11.21.21.31.31.41.41.51 + 1.1.11.11.21.21.31.31.41.41.51.61 1.1.11.11.21.21.31.31.41.41.51.61.61 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91 {} {} {} {} 2 + 2.2 2.2.12 2.2.12.12 2.2.12.12.12 2.2.12.12.12.22 + 2.2.12.12.12.22.22 2.2.12.12.12.22.22.32 2.2.12.12.12.22.22.32.42 + 2.2.12.12.12.22.22.32.42.52 2.2.12.12.12.22.22.32.42.52.62 + 2.2.12.12.12.22.22.32.42.52.62.62 {} {} {} {} 3 3.3 3.3.13 + 3.3.13.13 3.3.13.13.23 3.3.13.13.23.23 3.3.13.13.23.23.33 + 3.3.13.13.23.23.33.33 3.3.13.13.23.23.33.33.33 + 3.3.13.13.23.23.33.33.33.33 3.3.13.13.23.23.33.33.33.33.43 + 3.3.13.13.23.23.33.33.33.33.43.43 3.3.13.13.23.23.33.33.33.33.43.43.53 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73 {} {} {} {} 4 + 4.14 4.14.24 4.14.24.34 4.14.24.34.34 4.14.24.34.34.34 + 4.14.24.34.34.34.34 4.14.24.34.34.34.34.44 4.14.24.34.34.34.34.44.44 + 4.14.24.34.34.34.34.44.44.54 4.14.24.34.34.34.34.44.44.54.64 + 4.14.24.34.34.34.34.44.44.54.64.74 4.14.24.34.34.34.34.44.44.54.64.74.74 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84 {} {} {} {} 5 5.15 + 5.15.15 5.15.15.15 5.15.15.15.25 5.15.15.15.25.35 + 5.15.15.15.25.35.35 5.15.15.15.25.35.35.55 5.15.15.15.25.35.35.55.55 + 5.15.15.15.25.35.35.55.55.65 5.15.15.15.25.35.35.55.55.65.65 + 5.15.15.15.25.35.35.55.55.65.65.65 5.15.15.15.25.35.35.55.55.65.65.65.75 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85 {} {} {} {} 6 + 6.16 6.16.16 6.16.16.16 6.16.16.16.26 6.16.16.16.26.26 + 6.16.16.16.26.26.36 6.16.16.16.26.26.36.36 6.16.16.16.26.26.36.36.36 + 6.16.16.16.26.26.36.36.36.36 6.16.16.16.26.26.36.36.36.36.46 + 6.16.16.16.26.26.36.36.36.36.46.46 6.16.16.16.26.26.36.36.36.36.46.46.56 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76 {} {} {} {} 7 + 7.7 7.7.7 7.7.7.17 7.7.7.17.27 7.7.7.17.27.27 7.7.7.17.27.27.37 + 7.7.7.17.27.27.37.37 7.7.7.17.27.27.37.37.47 7.7.7.17.27.27.37.37.47.47 + 7.7.7.17.27.27.37.37.47.47.47 7.7.7.17.27.27.37.37.47.47.47.47 + 7.7.7.17.27.27.37.37.47.47.47.47.57 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77 {} {} {} {} 8 8.8 + 8.8.8 8.8.8.28 8.8.8.28.38 8.8.8.28.38.38 8.8.8.28.38.38.58 + 8.8.8.28.38.38.58.58 8.8.8.28.38.38.58.58.58 8.8.8.28.38.38.58.58.58.58 + 8.8.8.28.38.38.58.58.58.58.68 8.8.8.28.38.38.58.58.58.58.68.78 {} {} + {} {} 9 9.9 9.9.9 9.9.9.19 9.9.9.19.29 9.9.9.19.29.29 + 9.9.9.19.29.29.29 9.9.9.19.29.29.29.39 9.9.9.19.29.29.29.39.39 + 9.9.9.19.29.29.29.39.39.39 9.9.9.19.29.29.29.39.39.39.49 + 9.9.9.19.29.29.29.39.39.39.49.59 9.9.9.19.29.29.29.39.39.39.49.59.59 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89} + +do_execsql_test 1.5.14.5 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( ORDER BY b%10,a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING ) FROM t2 +} {{} {} {} {} 90 90.40 90.40.30 90.40.30.80 90.40.30.80.20 + 90.40.30.80.20.90 90.40.30.80.20.90.60 90.40.30.80.20.90.60.70 + 90.40.30.80.20.90.60.70.80 90.40.30.80.20.90.60.70.80.90 + 90.40.30.80.20.90.60.70.80.90.30 90.40.30.80.20.90.60.70.80.90.30.50 + 90.40.30.80.20.90.60.70.80.90.30.50.10 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9} + +do_execsql_test 1.5.14.6 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.5.14.7 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (win1 ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING) + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a) + ORDER BY 1 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.5.14.8 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (win1 ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING) + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a ORDER BY b%10) + ORDER BY 1 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.5.14.9 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER win2 + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a ORDER BY b%10), + win2 AS (win1 ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING) + ORDER BY 1 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.5.15.1 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE a%2=0) OVER win FROM t2 + WINDOW win AS (ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING) +} {0 {} 0 {} 0 {} 0 {} 1 89 2 89 3 89.96 4 89.96 5 89.96.38 + 6 89.96.38 7 89.96.38.39 8 89.96.38.39 9 89.96.38.39.91 + 10 89.96.38.39.91 11 89.96.38.39.91.6 12 89.96.38.39.91.6 + 13 89.96.38.39.91.6.97 14 89.96.38.39.91.6.97 15 89.96.38.39.91.6.97.46 + 16 89.96.38.39.91.6.97.46 17 89.96.38.39.91.6.97.46.54 + 18 89.96.38.39.91.6.97.46.54 19 89.96.38.39.91.6.97.46.54.8 + 20 89.96.38.39.91.6.97.46.54.8 21 89.96.38.39.91.6.97.46.54.8.29 + 22 89.96.38.39.91.6.97.46.54.8.29 23 89.96.38.39.91.6.97.46.54.8.29.84 + 24 89.96.38.39.91.6.97.46.54.8.29.84 + 25 89.96.38.39.91.6.97.46.54.8.29.84.23 + 26 89.96.38.39.91.6.97.46.54.8.29.84.23 + 27 89.96.38.39.91.6.97.46.54.8.29.84.23.16 + 28 89.96.38.39.91.6.97.46.54.8.29.84.23.16 + 29 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65 + 30 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65 + 31 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47 + 32 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47 + 33 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86 + 34 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86 + 35 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61 + 36 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61 + 37 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85 + 38 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85 + 39 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85 + 40 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85 + 41 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59 + 42 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59 + 43 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32 + 44 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32 + 45 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3 + 46 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3 + 47 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22 + 48 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22 + 49 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55 + 50 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55 + 51 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28 + 52 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28 + 53 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25 + 54 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25 + 55 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1 + 56 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1 + 57 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40 + 58 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40 + 59 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56 + 60 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56 + 61 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75 + 62 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75 + 63 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89 + 64 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89 + 65 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76 + 66 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76 + 67 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4 + 68 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4 + 69 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42 + 70 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42 + 71 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78 + 72 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78 + 73 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29 + 74 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29 + 75 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63 + 76 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63 + 77 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87 + 78 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87 + 79 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80 + 80 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80 + 81 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72 + 82 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72 + 83 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9 + 84 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9 + 85 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73 + 86 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73 + 87 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65 + 88 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65 + 89 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58 + 90 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58 + 91 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98 + 92 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98 + 93 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21 + 94 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21 + 95 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65 + 96 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65 + 97 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5 + 98 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5 + 99 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11 + 100 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11 + 101 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87 + 102 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87 + 103 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12 + 104 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12 + 105 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20 + 106 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20 + 107 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31 + 108 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31 + 109 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95 + 110 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95 + 111 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73 + 112 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73 + 113 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88 + 114 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88 + 115 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8 + 116 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8 + 117 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49 + 118 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49 + 119 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90 + 120 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90 + 121 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96 + 122 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96 + 123 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55 + 124 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55 + 125 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77 + 126 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77 + 127 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2 + 128 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2 + 129 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85 + 130 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85 + 131 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74 + 132 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74 + 133 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70 + 134 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70 + 135 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19 + 136 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19 + 137 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26 + 138 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26 + 139 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47 + 140 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47 + 141 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90 + 142 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90 + 143 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58 + 144 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58 + 145 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9 + 146 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9 + 147 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72 + 148 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72 + 149 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33 + 150 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33 + 151 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75 + 152 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75 + 153 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81 + 154 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81 + 155 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23 + 156 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23 + 157 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13 + 158 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13 + 159 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14 + 160 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14 + 161 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91 + 162 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91 + 163 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91 + 164 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91 + 165 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15 + 166 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15 + 167 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36 + 168 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36 + 169 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3 + 170 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3 + 171 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69 + 172 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69 + 173 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52 + 174 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52 + 175 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50 + 176 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50 + 177 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10 + 178 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10 + 179 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33 + 180 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33 + 181 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39 + 182 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39 + 183 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58 + 184 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58 + 185 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38 + 186 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38 + 187 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83} + +do_execsql_test 1.5.15.2 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE 0=1) OVER win FROM t2 + WINDOW win AS (ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING) +} {0 {} 0 {} 0 {} 0 {} 1 {} 2 {} 3 {} 4 {} 5 {} 6 {} 7 {} + 8 {} 9 {} 10 {} 11 {} 12 {} 13 {} 14 {} 15 {} 16 {} 17 {} + 18 {} 19 {} 20 {} 21 {} 22 {} 23 {} 24 {} 25 {} 26 {} + 27 {} 28 {} 29 {} 30 {} 31 {} 32 {} 33 {} 34 {} 35 {} + 36 {} 37 {} 38 {} 39 {} 40 {} 41 {} 42 {} 43 {} 44 {} + 45 {} 46 {} 47 {} 48 {} 49 {} 50 {} 51 {} 52 {} 53 {} + 54 {} 55 {} 56 {} 57 {} 58 {} 59 {} 60 {} 61 {} 62 {} + 63 {} 64 {} 65 {} 66 {} 67 {} 68 {} 69 {} 70 {} 71 {} + 72 {} 73 {} 74 {} 75 {} 76 {} 77 {} 78 {} 79 {} 80 {} + 81 {} 82 {} 83 {} 84 {} 85 {} 86 {} 87 {} 88 {} 89 {} + 90 {} 91 {} 92 {} 93 {} 94 {} 95 {} 96 {} 97 {} 98 {} + 99 {} 100 {} 101 {} 102 {} 103 {} 104 {} 105 {} 106 {} + 107 {} 108 {} 109 {} 110 {} 111 {} 112 {} 113 {} 114 {} + 115 {} 116 {} 117 {} 118 {} 119 {} 120 {} 121 {} 122 {} + 123 {} 124 {} 125 {} 126 {} 127 {} 128 {} 129 {} 130 {} + 131 {} 132 {} 133 {} 134 {} 135 {} 136 {} 137 {} 138 {} + 139 {} 140 {} 141 {} 142 {} 143 {} 144 {} 145 {} 146 {} + 147 {} 148 {} 149 {} 150 {} 151 {} 152 {} 153 {} 154 {} + 155 {} 156 {} 157 {} 158 {} 159 {} 160 {} 161 {} 162 {} + 163 {} 164 {} 165 {} 166 {} 167 {} 168 {} 169 {} 170 {} + 171 {} 172 {} 173 {} 174 {} 175 {} 176 {} 177 {} 178 {} + 179 {} 180 {} 181 {} 182 {} 183 {} 184 {} 185 {} 186 {} + 187 {}} + +do_execsql_test 1.5.15.3 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE 1=0) OVER win FROM t2 + WINDOW win AS (PARTITION BY (a%10) ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING) +} {0 {} 0 {} 0 {} 0 {} 1 {} 2 {} 3 {} 4 {} 5 {} 6 {} 7 {} + 8 {} 9 {} 10 {} 11 {} 12 {} 13 {} 14 {} 15 {} 16 {} 0 {} + 0 {} 0 {} 0 {} 1 {} 2 {} 3 {} 4 {} 5 {} 6 {} 7 {} 8 {} + 9 {} 10 {} 11 {} 12 {} 13 {} 14 {} 15 {} 0 {} 0 {} 0 {} + 0 {} 1 {} 2 {} 3 {} 4 {} 5 {} 6 {} 7 {} 8 {} 9 {} 10 {} + 11 {} 12 {} 13 {} 14 {} 15 {} 0 {} 0 {} 0 {} 0 {} 1 {} + 2 {} 3 {} 4 {} 5 {} 6 {} 7 {} 8 {} 9 {} 10 {} 11 {} + 12 {} 13 {} 14 {} 15 {} 0 {} 0 {} 0 {} 0 {} 1 {} 2 {} + 3 {} 4 {} 5 {} 6 {} 7 {} 8 {} 9 {} 10 {} 11 {} 12 {} + 13 {} 14 {} 15 {} 0 {} 0 {} 0 {} 0 {} 1 {} 2 {} 3 {} + 4 {} 5 {} 6 {} 7 {} 8 {} 9 {} 10 {} 11 {} 12 {} 13 {} + 14 {} 15 {} 0 {} 0 {} 0 {} 0 {} 1 {} 2 {} 3 {} 4 {} + 5 {} 6 {} 7 {} 8 {} 9 {} 10 {} 11 {} 12 {} 13 {} 14 {} + 15 {} 0 {} 0 {} 0 {} 0 {} 1 {} 2 {} 3 {} 4 {} 5 {} 6 {} + 7 {} 8 {} 9 {} 10 {} 11 {} 12 {} 13 {} 14 {} 15 {} 0 {} + 0 {} 0 {} 0 {} 1 {} 2 {} 3 {} 4 {} 5 {} 6 {} 7 {} 8 {} + 9 {} 10 {} 11 {} 12 {} 13 {} 14 {} 15 {} 0 {} 0 {} 0 {} + 0 {} 1 {} 2 {} 3 {} 4 {} 5 {} 6 {} 7 {} 8 {} 9 {} 10 {} + 11 {} 12 {} 13 {} 14 {} 15 {}} + +do_execsql_test 1.5.15.4 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE a%2=0) OVER win FROM t2 + WINDOW win AS (PARTITION BY (a%10) ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 PRECEDING) +} {0 {} 0 {} 0 {} 0 {} 1 89 2 89.6 3 89.6.29 4 89.6.29.47 + 5 89.6.29.47.59 6 89.6.29.47.59.28 7 89.6.29.47.59.28.75 + 8 89.6.29.47.59.28.75.78 9 89.6.29.47.59.28.75.78.72 + 10 89.6.29.47.59.28.75.78.72.98 11 89.6.29.47.59.28.75.78.72.98.87 + 12 89.6.29.47.59.28.75.78.72.98.87.73 + 13 89.6.29.47.59.28.75.78.72.98.87.73.96 + 14 89.6.29.47.59.28.75.78.72.98.87.73.96.74 + 15 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90 + 16 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75 0 {} 0 {} 0 {} + 0 {} 1 {} 2 {} 3 {} 4 {} 5 {} 6 {} 7 {} 8 {} 9 {} 10 {} + 11 {} 12 {} 13 {} 14 {} 15 {} 0 {} 0 {} 0 {} 0 {} 1 96 + 2 96.97 3 96.97.84 4 96.97.84.86 5 96.97.84.86.32 + 6 96.97.84.86.32.25 7 96.97.84.86.32.25.89 8 96.97.84.86.32.25.89.29 + 9 96.97.84.86.32.25.89.29.9 10 96.97.84.86.32.25.89.29.9.21 + 11 96.97.84.86.32.25.89.29.9.21.12 12 96.97.84.86.32.25.89.29.9.21.12.88 + 13 96.97.84.86.32.25.89.29.9.21.12.88.55 + 14 96.97.84.86.32.25.89.29.9.21.12.88.55.70 + 15 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58 0 {} 0 {} 0 {} 0 {} + 1 {} 2 {} 3 {} 4 {} 5 {} 6 {} 7 {} 8 {} 9 {} 10 {} + 11 {} 12 {} 13 {} 14 {} 15 {} 0 {} 0 {} 0 {} 0 {} 1 38 + 2 38.46 3 38.46.23 4 38.46.23.61 5 38.46.23.61.3 6 38.46.23.61.3.1 + 7 38.46.23.61.3.1.76 8 38.46.23.61.3.1.76.63 9 38.46.23.61.3.1.76.63.73 + 10 38.46.23.61.3.1.76.63.73.65 11 38.46.23.61.3.1.76.63.73.65.20 + 12 38.46.23.61.3.1.76.63.73.65.20.8 + 13 38.46.23.61.3.1.76.63.73.65.20.8.77 + 14 38.46.23.61.3.1.76.63.73.65.20.8.77.19 + 15 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9 0 {} 0 {} 0 {} 0 {} + 1 {} 2 {} 3 {} 4 {} 5 {} 6 {} 7 {} 8 {} 9 {} 10 {} + 11 {} 12 {} 13 {} 14 {} 15 {} 0 {} 0 {} 0 {} 0 {} 1 39 + 2 39.54 3 39.54.16 4 39.54.16.85 5 39.54.16.85.22 + 6 39.54.16.85.22.40 7 39.54.16.85.22.40.4 8 39.54.16.85.22.40.4.87 + 9 39.54.16.85.22.40.4.87.65 10 39.54.16.85.22.40.4.87.65.5 + 11 39.54.16.85.22.40.4.87.65.5.31 12 39.54.16.85.22.40.4.87.65.5.31.49 + 13 39.54.16.85.22.40.4.87.65.5.31.49.2 + 14 39.54.16.85.22.40.4.87.65.5.31.49.2.26 + 15 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72 0 {} 0 {} 0 {} 0 {} + 1 {} 2 {} 3 {} 4 {} 5 {} 6 {} 7 {} 8 {} 9 {} 10 {} + 11 {} 12 {} 13 {} 14 {} 15 {} 0 {} 0 {} 0 {} 0 {} 1 91 + 2 91.8 3 91.8.65 4 91.8.65.85 5 91.8.65.85.55 6 91.8.65.85.55.56 + 7 91.8.65.85.55.56.42 8 91.8.65.85.55.56.42.80 + 9 91.8.65.85.55.56.42.80.58 10 91.8.65.85.55.56.42.80.58.11 + 11 91.8.65.85.55.56.42.80.58.11.95 12 91.8.65.85.55.56.42.80.58.11.95.90 + 13 91.8.65.85.55.56.42.80.58.11.95.90.85 + 14 91.8.65.85.55.56.42.80.58.11.95.90.85.47 + 15 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33 0 {} 0 {} 0 {} 0 {} + 1 {} 2 {} 3 {} 4 {} 5 {} 6 {} 7 {} 8 {} 9 {} 10 {} + 11 {} 12 {} 13 {} 14 {} 15 {}} + +do_execsql_test 1.6.2.1 { + SELECT max(b) OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2 +} {{} {} 89 89 96 96 96 68 68 68 91 91 91 99 99 + 99 97 78 78 97 97 97 67 93 93 93 84 77 23 93 + 93 93 65 47 86 86 86 91 91 91 85 85 85 59 59 + 56 56 91 91 91 90 90 55 89 89 89 47 56 56 56 + 56 56 75 75 89 98 98 98 81 94 94 94 78 78 78 + 53 63 63 87 87 87 84 84 84 72 61 73 95 95 95 + 65 96 98 98 98 74 74 74 65 73 73 73 87 87 87 + 41 20 31 31 31 95 95 95 79 88 88 88 34 49 49 + 90 90 96 96 96 75 77 77 77 44 85 85 85 74 74 + 70 70 59 39 39 47 80 90 90 90 58 58 72 72 72 + 72 93 93 93 81 81 81 37 37 37 14 62 91 91 91 + 91 91 34 36 99 99 99 95 95 69 58 52 84 84 84 + 84 84 39 44 58 58 58 38 83 83 83} + +do_execsql_test 1.6.2.2 { + SELECT min(b) OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2 +} {{} {} 89 81 81 59 38 38 38 39 39 46 6 6 6 27 + 27 27 46 54 8 8 8 29 29 77 23 16 16 16 16 35 + 35 7 7 7 61 61 61 24 24 24 43 12 12 12 3 3 + 3 22 22 15 15 15 25 25 1 1 1 40 40 16 16 16 + 36 36 76 76 4 4 4 30 30 30 29 29 29 2 2 2 + 37 37 72 41 9 9 9 61 65 13 13 13 58 1 1 1 + 21 35 5 5 5 11 11 41 12 8 8 8 20 15 15 15 + 22 22 73 34 8 8 8 11 34 34 59 59 55 55 55 44 + 2 2 2 7 57 29 29 29 19 19 19 26 26 26 47 36 + 36 36 9 9 9 66 33 33 33 64 64 9 9 9 13 12 + 12 12 14 36 36 33 15 15 15 34 3 3 3 58 52 30 + 30 30 10 10 10 21 21 21 39 30 30 30 34 27 27} + +do_execsql_test 1.6.3.1 { + SELECT row_number() OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.6.3.2 { + SELECT row_number() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.6.3.3 { + SELECT row_number() OVER ( ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.6.4.1 { + SELECT dense_rank() OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.6.4.2 { + SELECT dense_rank() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.6.4.3 { + SELECT dense_rank() OVER ( ORDER BY b ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2 +} {1 1 2 2 3 3 4 5 6 7 7 7 8 8 8 9 9 9 10 + 11 11 12 12 12 13 13 14 15 15 15 16 16 16 17 + 18 19 20 20 21 21 22 22 23 24 25 25 26 26 27 + 28 28 28 29 29 29 30 30 31 32 32 32 32 33 33 + 33 33 34 34 35 35 35 35 36 36 37 37 38 38 38 + 39 40 40 41 42 42 43 43 44 44 45 45 45 45 46 + 47 48 49 50 51 52 52 53 53 53 54 55 55 55 55 + 56 56 56 56 57 58 58 59 59 60 61 62 62 62 63 + 64 65 66 67 68 68 68 69 69 69 70 70 70 71 71 + 71 72 73 73 74 74 75 76 76 77 77 77 78 79 80 + 80 80 80 81 81 81 82 83 83 84 85 85 85 86 86 + 86 87 87 87 87 87 88 88 88 89 90 90 90 91 91 + 91 92 92 93 93 94 94} + +do_execsql_test 1.6.4.4 { + SELECT dense_rank() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2 +} {1 2 3 3 3 4 5 6 7 8 8 9 9 9 1 1 2 2 3 + 3 4 4 5 5 6 7 7 8 8 8 9 9 9 9 9 1 1 2 + 2 2 3 3 4 5 6 7 7 8 8 8 9 1 1 2 2 3 3 + 4 4 4 4 5 5 6 7 8 8 8 9 10 10 10 1 2 3 + 4 4 4 4 5 5 6 7 8 8 8 9 9 9 9 10 1 2 2 + 2 3 4 4 5 5 6 6 6 7 7 7 8 8 8 9 9 9 1 + 2 2 2 3 3 4 4 4 4 5 5 6 6 6 7 8 9 10 10 + 10 1 1 1 2 3 3 4 4 5 5 5 5 6 7 8 8 9 9 + 10 10 1 1 1 2 3 3 4 4 4 4 5 6 6 7 8 8 1 + 1 1 2 3 3 3 4 4 4 5 6 6 6 6 7 8 9 9 9 + 10 10} + +do_execsql_test 1.6.4.5 { + SELECT dense_rank() OVER ( ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 + 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 + 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 + 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 + 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 + 8 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 10 10 + 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 + 10 10 10 10 10} + +do_execsql_test 1.6.4.6 { + SELECT dense_rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 + 5 5 5 5 5 5 5 5 5 5 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 + 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 + 5} + +do_execsql_test 1.6.5.1 { + SELECT rank() OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.6.5.2 { + SELECT rank() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.6.5.3 { + SELECT rank() OVER ( ORDER BY b ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2 +} {1 1 3 3 5 5 7 8 9 10 10 10 13 13 13 16 16 + 16 19 20 20 22 22 22 25 25 27 28 28 28 31 31 + 31 34 35 36 37 37 39 39 41 41 43 44 45 45 47 + 47 49 50 50 50 53 53 53 56 56 58 59 59 59 59 + 63 63 63 63 67 67 69 69 69 69 73 73 75 75 77 + 77 77 80 81 81 83 84 84 86 86 88 88 90 90 90 + 90 94 95 96 97 98 99 100 100 102 102 102 105 106 + 106 106 106 110 110 110 110 114 115 115 117 117 119 + 120 121 121 121 124 125 126 127 128 129 129 129 132 + 132 132 135 135 135 138 138 138 141 142 142 144 144 + 146 147 147 149 149 149 152 153 154 154 154 154 158 + 158 158 161 162 162 164 165 165 165 168 168 168 171 + 171 171 171 171 176 176 176 179 180 180 180 183 183 + 183 186 186 188 188 190 190} + +do_execsql_test 1.6.5.4 { + SELECT rank() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2 +} {1 2 3 3 3 6 7 8 9 10 10 12 12 12 1 1 3 3 + 5 5 7 7 9 9 11 12 12 14 14 14 17 17 17 17 + 17 1 1 3 3 3 6 6 8 9 10 11 11 13 13 13 16 + 1 1 3 3 5 5 7 7 7 7 11 11 13 14 15 15 15 + 18 19 19 19 1 2 3 4 4 4 4 8 8 10 11 12 12 + 12 15 15 15 15 19 1 2 2 2 5 6 6 8 8 10 10 + 10 13 13 13 16 16 16 19 19 19 1 2 2 2 5 5 7 + 7 7 7 11 11 13 13 13 16 17 18 19 19 19 1 1 + 1 4 5 5 7 7 9 9 9 9 13 14 15 15 17 17 19 + 19 1 1 1 4 5 5 7 7 7 7 11 12 12 14 15 15 + 1 1 1 4 5 5 5 8 8 8 11 12 12 12 12 16 17 + 18 18 18 21 21} + +do_execsql_test 1.6.5.5 { + SELECT rank() OVER ( ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 15 15 15 15 + 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 + 15 15 36 36 36 36 36 36 36 36 36 36 36 36 36 + 36 36 36 52 52 52 52 52 52 52 52 52 52 52 52 + 52 52 52 52 52 52 52 52 52 73 73 73 73 73 73 + 73 73 73 73 73 73 73 73 73 73 73 73 73 92 92 + 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 + 92 92 92 92 113 113 113 113 113 113 113 113 113 + 113 113 113 113 113 113 113 113 113 113 113 113 134 + 134 134 134 134 134 134 134 134 134 134 134 134 134 + 134 134 134 134 134 134 154 154 154 154 154 154 154 + 154 154 154 154 154 154 154 154 154 170 170 170 170 + 170 170 170 170 170 170 170 170 170 170 170 170 170 + 170 170 170 170 170} + +do_execsql_test 1.6.5.6 { + SELECT rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 15 15 15 15 + 15 15 15 15 15 15 15 15 15 15 15 15 31 31 31 + 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 + 31 50 50 50 50 50 50 50 50 50 50 50 50 50 50 + 50 50 50 50 50 50 50 71 71 71 71 71 71 71 71 + 71 71 71 71 71 71 71 71 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 22 22 22 22 22 22 + 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 + 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 + 43 43 43 43 43 43 64 64 64 64 64 64 64 64 64 + 64 64 64 64 64 64 64 64 64 64 64 84 84 84 84 + 84 84 84 84 84 84 84 84 84 84 84 84 84 84 84 + 84 84 84} + +do_execsql_test 1.6.6.1 { + SELECT + row_number() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ), + rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ), + dense_rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) + FROM t2 +} {1 1 1 2 1 1 3 1 1 4 1 1 5 1 1 6 1 1 7 1 1 8 1 1 9 1 1 + 10 1 1 11 1 1 12 1 1 13 1 1 14 1 1 15 15 2 16 15 2 17 15 2 + 18 15 2 19 15 2 20 15 2 21 15 2 22 15 2 23 15 2 24 15 2 + 25 15 2 26 15 2 27 15 2 28 15 2 29 15 2 30 15 2 31 31 3 + 32 31 3 33 31 3 34 31 3 35 31 3 36 31 3 37 31 3 38 31 3 + 39 31 3 40 31 3 41 31 3 42 31 3 43 31 3 44 31 3 45 31 3 + 46 31 3 47 31 3 48 31 3 49 31 3 50 50 4 51 50 4 52 50 4 + 53 50 4 54 50 4 55 50 4 56 50 4 57 50 4 58 50 4 59 50 4 + 60 50 4 61 50 4 62 50 4 63 50 4 64 50 4 65 50 4 66 50 4 + 67 50 4 68 50 4 69 50 4 70 50 4 71 71 5 72 71 5 73 71 5 + 74 71 5 75 71 5 76 71 5 77 71 5 78 71 5 79 71 5 80 71 5 + 81 71 5 82 71 5 83 71 5 84 71 5 85 71 5 86 71 5 1 1 1 2 1 1 + 3 1 1 4 1 1 5 1 1 6 1 1 7 1 1 8 1 1 9 1 1 10 1 1 11 1 1 + 12 1 1 13 1 1 14 1 1 15 1 1 16 1 1 17 1 1 18 1 1 19 1 1 + 20 1 1 21 1 1 22 22 2 23 22 2 24 22 2 25 22 2 26 22 2 27 22 2 + 28 22 2 29 22 2 30 22 2 31 22 2 32 22 2 33 22 2 34 22 2 + 35 22 2 36 22 2 37 22 2 38 22 2 39 22 2 40 22 2 41 22 2 + 42 22 2 43 43 3 44 43 3 45 43 3 46 43 3 47 43 3 48 43 3 + 49 43 3 50 43 3 51 43 3 52 43 3 53 43 3 54 43 3 55 43 3 + 56 43 3 57 43 3 58 43 3 59 43 3 60 43 3 61 43 3 62 43 3 + 63 43 3 64 64 4 65 64 4 66 64 4 67 64 4 68 64 4 69 64 4 + 70 64 4 71 64 4 72 64 4 73 64 4 74 64 4 75 64 4 76 64 4 + 77 64 4 78 64 4 79 64 4 80 64 4 81 64 4 82 64 4 83 64 4 + 84 84 5 85 84 5 86 84 5 87 84 5 88 84 5 89 84 5 90 84 5 + 91 84 5 92 84 5 93 84 5 94 84 5 95 84 5 96 84 5 97 84 5 + 98 84 5 99 84 5 100 84 5 101 84 5 102 84 5 103 84 5 104 84 5 + 105 84 5} + + +do_test 1.6.7.1 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0053 0.0105 0.0158 0.0211 0.0263 0.0316 0.0368 0.0421 0.0474 0.0526 0.0579 0.0632 0.0684 0.0737 0.0789 0.0842 0.0895 0.0947 0.1000 0.1053 0.1105 0.1158 0.1211 0.1263 0.1316 0.1368 0.1421 0.1474 0.1526 0.1579 0.1632 0.1684 0.1737 0.1789 0.1842 0.1895 0.1947 0.2000 0.2053 0.2105 0.2158 0.2211 0.2263 0.2316 0.2368 0.2421 0.2474 0.2526 0.2579 0.2632 0.2684 0.2737 0.2789 0.2842 0.2895 0.2947 0.3000 0.3053 0.3105 0.3158 0.3211 0.3263 0.3316 0.3368 0.3421 0.3474 0.3526 0.3579 0.3632 0.3684 0.3737 0.3789 0.3842 0.3895 0.3947 0.4000 0.4053 0.4105 0.4158 0.4211 0.4263 0.4316 0.4368 0.4421 0.4474 0.4526 0.4579 0.4632 0.4684 0.4737 0.4789 0.4842 0.4895 0.4947 0.5000 0.5053 0.5105 0.5158 0.5211 0.5263 0.5316 0.5368 0.5421 0.5474 0.5526 0.5579 0.5632 0.5684 0.5737 0.5789 0.5842 0.5895 0.5947 0.6000 0.6053 0.6105 0.6158 0.6211 0.6263 0.6316 0.6368 0.6421 0.6474 0.6526 0.6579 0.6632 0.6684 0.6737 0.6789 0.6842 0.6895 0.6947 0.7000 0.7053 0.7105 0.7158 0.7211 0.7263 0.7316 0.7368 0.7421 0.7474 0.7526 0.7579 0.7632 0.7684 0.7737 0.7789 0.7842 0.7895 0.7947 0.8000 0.8053 0.8105 0.8158 0.8211 0.8263 0.8316 0.8368 0.8421 0.8474 0.8526 0.8579 0.8632 0.8684 0.8737 0.8789 0.8842 0.8895 0.8947 0.9000 0.9053 0.9105 0.9158 0.9211 0.9263 0.9316 0.9368 0.9421 0.9474 0.9526 0.9579 0.9632 0.9684 0.9737 0.9789 0.9842 0.9895 0.9947 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.6.7.2 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0769 0.1538 0.2308 0.3077 0.3846 0.4615 0.5385 0.6154 0.6923 0.7692 0.8462 0.9231 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0667 0.1333 0.2000 0.2667 0.3333 0.4000 0.4667 0.5333 0.6000 0.6667 0.7333 0.8000 0.8667 0.9333 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0556 0.1111 0.1667 0.2222 0.2778 0.3333 0.3889 0.4444 0.5000 0.5556 0.6111 0.6667 0.7222 0.7778 0.8333 0.8889 0.9444 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0526 0.1053 0.1579 0.2105 0.2632 0.3158 0.3684 0.4211 0.4737 0.5263 0.5789 0.6316 0.6842 0.7368 0.7895 0.8421 0.8947 0.9474 1.0000 0.0000 0.0667 0.1333 0.2000 0.2667 0.3333 0.4000 0.4667 0.5333 0.6000 0.6667 0.7333 0.8000 0.8667 0.9333 1.0000 0.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.6.7.3 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY b ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0105 0.0105 0.0211 0.0211 0.0316 0.0368 0.0421 0.0474 0.0474 0.0474 0.0632 0.0632 0.0632 0.0789 0.0789 0.0789 0.0947 0.1000 0.1000 0.1105 0.1105 0.1105 0.1263 0.1263 0.1368 0.1421 0.1421 0.1421 0.1579 0.1579 0.1579 0.1737 0.1789 0.1842 0.1895 0.1895 0.2000 0.2000 0.2105 0.2105 0.2211 0.2263 0.2316 0.2316 0.2421 0.2421 0.2526 0.2579 0.2579 0.2579 0.2737 0.2737 0.2737 0.2895 0.2895 0.3000 0.3053 0.3053 0.3053 0.3053 0.3263 0.3263 0.3263 0.3263 0.3474 0.3474 0.3579 0.3579 0.3579 0.3579 0.3789 0.3789 0.3895 0.3895 0.4000 0.4000 0.4000 0.4158 0.4211 0.4211 0.4316 0.4368 0.4368 0.4474 0.4474 0.4579 0.4579 0.4684 0.4684 0.4684 0.4684 0.4895 0.4947 0.5000 0.5053 0.5105 0.5158 0.5211 0.5211 0.5316 0.5316 0.5316 0.5474 0.5526 0.5526 0.5526 0.5526 0.5737 0.5737 0.5737 0.5737 0.5947 0.6000 0.6000 0.6105 0.6105 0.6211 0.6263 0.6316 0.6316 0.6316 0.6474 0.6526 0.6579 0.6632 0.6684 0.6737 0.6737 0.6737 0.6895 0.6895 0.6895 0.7053 0.7053 0.7053 0.7211 0.7211 0.7211 0.7368 0.7421 0.7421 0.7526 0.7526 0.7632 0.7684 0.7684 0.7789 0.7789 0.7789 0.7947 0.8000 0.8053 0.8053 0.8053 0.8053 0.8263 0.8263 0.8263 0.8421 0.8474 0.8474 0.8579 0.8632 0.8632 0.8632 0.8789 0.8789 0.8789 0.8947 0.8947 0.8947 0.8947 0.8947 0.9211 0.9211 0.9211 0.9368 0.9421 0.9421 0.9421 0.9579 0.9579 0.9579 0.9737 0.9737 0.9842 0.9842 0.9947 0.9947} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.6.7.4 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0769 0.1538 0.1538 0.1538 0.3846 0.4615 0.5385 0.6154 0.6923 0.6923 0.8462 0.8462 0.8462 0.0000 0.0000 0.1000 0.1000 0.2000 0.2000 0.3000 0.3000 0.4000 0.4000 0.5000 0.5500 0.5500 0.6500 0.6500 0.6500 0.8000 0.8000 0.8000 0.8000 0.8000 0.0000 0.0000 0.1333 0.1333 0.1333 0.3333 0.3333 0.4667 0.5333 0.6000 0.6667 0.6667 0.8000 0.8000 0.8000 1.0000 0.0000 0.0000 0.1000 0.1000 0.2000 0.2000 0.3000 0.3000 0.3000 0.3000 0.5000 0.5000 0.6000 0.6500 0.7000 0.7000 0.7000 0.8500 0.9000 0.9000 0.9000 0.0000 0.0556 0.1111 0.1667 0.1667 0.1667 0.1667 0.3889 0.3889 0.5000 0.5556 0.6111 0.6111 0.6111 0.7778 0.7778 0.7778 0.7778 1.0000 0.0000 0.0500 0.0500 0.0500 0.2000 0.2500 0.2500 0.3500 0.3500 0.4500 0.4500 0.4500 0.6000 0.6000 0.6000 0.7500 0.7500 0.7500 0.9000 0.9000 0.9000 0.0000 0.0500 0.0500 0.0500 0.2000 0.2000 0.3000 0.3000 0.3000 0.3000 0.5000 0.5000 0.6000 0.6000 0.6000 0.7500 0.8000 0.8500 0.9000 0.9000 0.9000 0.0000 0.0000 0.0000 0.1579 0.2105 0.2105 0.3158 0.3158 0.4211 0.4211 0.4211 0.4211 0.6316 0.6842 0.7368 0.7368 0.8421 0.8421 0.9474 0.9474 0.0000 0.0000 0.0000 0.2000 0.2667 0.2667 0.4000 0.4000 0.4000 0.4000 0.6667 0.7333 0.7333 0.8667 0.9333 0.9333 0.0000 0.0000 0.0000 0.1429 0.1905 0.1905 0.1905 0.3333 0.3333 0.3333 0.4762 0.5238 0.5238 0.5238 0.5238 0.7143 0.7619 0.8095 0.8095 0.8095 0.9524 0.9524} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.6.7.5 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.6.7.6 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER (PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.6.8.1 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0052 0.0105 0.0157 0.0209 0.0262 0.0314 0.0366 0.0419 0.0471 0.0524 0.0576 0.0628 0.0681 0.0733 0.0785 0.0838 0.0890 0.0942 0.0995 0.1047 0.1099 0.1152 0.1204 0.1257 0.1309 0.1361 0.1414 0.1466 0.1518 0.1571 0.1623 0.1675 0.1728 0.1780 0.1832 0.1885 0.1937 0.1990 0.2042 0.2094 0.2147 0.2199 0.2251 0.2304 0.2356 0.2408 0.2461 0.2513 0.2565 0.2618 0.2670 0.2723 0.2775 0.2827 0.2880 0.2932 0.2984 0.3037 0.3089 0.3141 0.3194 0.3246 0.3298 0.3351 0.3403 0.3455 0.3508 0.3560 0.3613 0.3665 0.3717 0.3770 0.3822 0.3874 0.3927 0.3979 0.4031 0.4084 0.4136 0.4188 0.4241 0.4293 0.4346 0.4398 0.4450 0.4503 0.4555 0.4607 0.4660 0.4712 0.4764 0.4817 0.4869 0.4921 0.4974 0.5026 0.5079 0.5131 0.5183 0.5236 0.5288 0.5340 0.5393 0.5445 0.5497 0.5550 0.5602 0.5654 0.5707 0.5759 0.5812 0.5864 0.5916 0.5969 0.6021 0.6073 0.6126 0.6178 0.6230 0.6283 0.6335 0.6387 0.6440 0.6492 0.6545 0.6597 0.6649 0.6702 0.6754 0.6806 0.6859 0.6911 0.6963 0.7016 0.7068 0.7120 0.7173 0.7225 0.7277 0.7330 0.7382 0.7435 0.7487 0.7539 0.7592 0.7644 0.7696 0.7749 0.7801 0.7853 0.7906 0.7958 0.8010 0.8063 0.8115 0.8168 0.8220 0.8272 0.8325 0.8377 0.8429 0.8482 0.8534 0.8586 0.8639 0.8691 0.8743 0.8796 0.8848 0.8901 0.8953 0.9005 0.9058 0.9110 0.9162 0.9215 0.9267 0.9319 0.9372 0.9424 0.9476 0.9529 0.9581 0.9634 0.9686 0.9738 0.9791 0.9843 0.9895 0.9948 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.6.8.2 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0714 0.1429 0.2143 0.2857 0.3571 0.4286 0.5000 0.5714 0.6429 0.7143 0.7857 0.8571 0.9286 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0625 0.1250 0.1875 0.2500 0.3125 0.3750 0.4375 0.5000 0.5625 0.6250 0.6875 0.7500 0.8125 0.8750 0.9375 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0526 0.1053 0.1579 0.2105 0.2632 0.3158 0.3684 0.4211 0.4737 0.5263 0.5789 0.6316 0.6842 0.7368 0.7895 0.8421 0.8947 0.9474 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0625 0.1250 0.1875 0.2500 0.3125 0.3750 0.4375 0.5000 0.5625 0.6250 0.6875 0.7500 0.8125 0.8750 0.9375 1.0000 0.0455 0.0909 0.1364 0.1818 0.2273 0.2727 0.3182 0.3636 0.4091 0.4545 0.5000 0.5455 0.5909 0.6364 0.6818 0.7273 0.7727 0.8182 0.8636 0.9091 0.9545 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.6.8.3 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY b ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0105 0.0105 0.0209 0.0209 0.0314 0.0314 0.0366 0.0419 0.0471 0.0628 0.0628 0.0628 0.0785 0.0785 0.0785 0.0942 0.0942 0.0942 0.0995 0.1099 0.1099 0.1257 0.1257 0.1257 0.1361 0.1361 0.1414 0.1571 0.1571 0.1571 0.1728 0.1728 0.1728 0.1780 0.1832 0.1885 0.1990 0.1990 0.2094 0.2094 0.2199 0.2199 0.2251 0.2304 0.2408 0.2408 0.2513 0.2513 0.2565 0.2723 0.2723 0.2723 0.2880 0.2880 0.2880 0.2984 0.2984 0.3037 0.3246 0.3246 0.3246 0.3246 0.3455 0.3455 0.3455 0.3455 0.3560 0.3560 0.3770 0.3770 0.3770 0.3770 0.3874 0.3874 0.3979 0.3979 0.4136 0.4136 0.4136 0.4188 0.4293 0.4293 0.4346 0.4450 0.4450 0.4555 0.4555 0.4660 0.4660 0.4869 0.4869 0.4869 0.4869 0.4921 0.4974 0.5026 0.5079 0.5131 0.5183 0.5288 0.5288 0.5445 0.5445 0.5445 0.5497 0.5707 0.5707 0.5707 0.5707 0.5916 0.5916 0.5916 0.5916 0.5969 0.6073 0.6073 0.6178 0.6178 0.6230 0.6283 0.6440 0.6440 0.6440 0.6492 0.6545 0.6597 0.6649 0.6702 0.6859 0.6859 0.6859 0.7016 0.7016 0.7016 0.7173 0.7173 0.7173 0.7330 0.7330 0.7330 0.7382 0.7487 0.7487 0.7592 0.7592 0.7644 0.7749 0.7749 0.7906 0.7906 0.7906 0.7958 0.8010 0.8220 0.8220 0.8220 0.8220 0.8377 0.8377 0.8377 0.8429 0.8534 0.8534 0.8586 0.8743 0.8743 0.8743 0.8901 0.8901 0.8901 0.9162 0.9162 0.9162 0.9162 0.9162 0.9319 0.9319 0.9319 0.9372 0.9529 0.9529 0.9529 0.9686 0.9686 0.9686 0.9791 0.9791 0.9895 0.9895 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.6.8.4 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0714 0.1429 0.3571 0.3571 0.3571 0.4286 0.5000 0.5714 0.6429 0.7857 0.7857 1.0000 1.0000 1.0000 0.0952 0.0952 0.1905 0.1905 0.2857 0.2857 0.3810 0.3810 0.4762 0.4762 0.5238 0.6190 0.6190 0.7619 0.7619 0.7619 1.0000 1.0000 1.0000 1.0000 1.0000 0.1250 0.1250 0.3125 0.3125 0.3125 0.4375 0.4375 0.5000 0.5625 0.6250 0.7500 0.7500 0.9375 0.9375 0.9375 1.0000 0.0952 0.0952 0.1905 0.1905 0.2857 0.2857 0.4762 0.4762 0.4762 0.4762 0.5714 0.5714 0.6190 0.6667 0.8095 0.8095 0.8095 0.8571 1.0000 1.0000 1.0000 0.0526 0.1053 0.1579 0.3684 0.3684 0.3684 0.3684 0.4737 0.4737 0.5263 0.5789 0.7368 0.7368 0.7368 0.9474 0.9474 0.9474 0.9474 1.0000 0.0476 0.1905 0.1905 0.1905 0.2381 0.3333 0.3333 0.4286 0.4286 0.5714 0.5714 0.5714 0.7143 0.7143 0.7143 0.8571 0.8571 0.8571 1.0000 1.0000 1.0000 0.0476 0.1905 0.1905 0.1905 0.2857 0.2857 0.4762 0.4762 0.4762 0.4762 0.5714 0.5714 0.7143 0.7143 0.7143 0.7619 0.8095 0.8571 1.0000 1.0000 1.0000 0.1500 0.1500 0.1500 0.2000 0.3000 0.3000 0.4000 0.4000 0.6000 0.6000 0.6000 0.6000 0.6500 0.7000 0.8000 0.8000 0.9000 0.9000 1.0000 1.0000 0.1875 0.1875 0.1875 0.2500 0.3750 0.3750 0.6250 0.6250 0.6250 0.6250 0.6875 0.8125 0.8125 0.8750 1.0000 1.0000 0.1364 0.1364 0.1364 0.1818 0.3182 0.3182 0.3182 0.4545 0.4545 0.4545 0.5000 0.6818 0.6818 0.6818 0.6818 0.7273 0.7727 0.9091 0.9091 0.9091 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.6.8.5 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.6.8.6 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.6.8.1 { + set myres {} + foreach r [db eval {SELECT ntile(100) OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 88.0000 89.0000 89.0000 90.0000 90.0000 91.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.6.8.2 { + set myres {} + foreach r [db eval {SELECT ntile(101) OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 22.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.6.8.3 { + set myres {} + foreach r [db eval {SELECT ntile(102) OVER ( ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 88.0000 89.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.6.8.4 { + set myres {} + foreach r [db eval {SELECT ntile(103) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 22.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.6.8.5 { + set myres {} + foreach r [db eval {SELECT ntile(104) OVER ( ORDER BY b%10,a ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000 103.0000 104.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.6.8.6 { + set myres {} + foreach r [db eval {SELECT ntile(105) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.6.8.7 { + set myres {} + foreach r [db eval {SELECT ntile(105) OVER ( ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 88.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000 103.0000 104.0000 105.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + +do_execsql_test 1.6.9.1 { + SELECT last_value(a+b) OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2 +} {{} {} 99 92 108 72 52 83 55 79 109 65 26 120 + 119 50 70 103 80 124 36 96 59 124 116 110 57 51 + 52 130 103 74 87 48 128 117 105 136 131 71 133 92 + 109 63 84 109 57 146 78 147 113 74 88 150 87 110 + 65 121 106 110 124 85 145 107 161 171 150 156 80 + 171 120 109 158 114 111 136 147 87 173 124 168 173 + 162 132 101 154 167 190 161 110 156 195 198 102 123 + 177 169 140 111 180 119 160 197 152 124 121 134 146 + 147 132 213 141 193 200 210 157 132 136 175 161 218 + 188 226 191 187 208 211 179 138 144 223 196 214 170 + 212 202 163 184 172 173 195 229 240 187 210 200 163 + 227 228 223 191 252 235 225 243 172 187 202 179 179 + 182 231 261 207 263 206 189 209 212 276 181 274 249 + 239 234 213 234 269 196 271 221 210 229 235 250 223 + 232 229 279 224 280} + +do_execsql_test 1.6.9.2 { + SELECT last_value(a+b) OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2 +} {{} {} 147 106 109 168 134 218 191 212 229 240 213 + 234 {} {} 92 109 105 136 146 65 156 132 154 102 + 123 119 160 152 146 147 136 243 261 {} {} 79 63 + 84 78 120 87 162 124 141 138 227 228 179 231 {} + {} 124 57 130 92 57 110 114 136 147 167 110 180 + 193 191 252 187 179 206 181 {} {} 80 116 117 71 + 80 171 173 177 157 161 179 214 225 182 209 269 271 + {} {} 103 74 131 133 113 74 87 145 190 161 169 + 140 111 132 213 187 208 223 235 {} {} 108 65 26 + 70 51 52 128 109 121 124 85 107 150 195 226 172 + 173 187 223 {} {} 119 50 124 96 110 87 48 110 173 + 124 197 211 144 196 195 200 202 224 {} {} 52 83 + 103 36 88 171 158 156 198 121 210 132 210 239 {} + {} 99 72 55 120 59 109 150 161 111 101 200 175 + 188 170 202 163 184 163 172 276} + +do_execsql_test 1.6.9.3 { + SELECT last_value(a+b) OVER ( ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2 +} {{} {} 65 102 87 138 57 181 80 111 26 48 144 207 + 36 121 132 101 163 172 196 119 136 63 124 179 110 + 179 182 74 132 189 51 52 85 216 163 134 123 210 + 78 141 57 187 71 87 172 173 50 224 88 59 111 170 + 109 213 223 146 147 84 114 191 206 221 157 161 209 + 229 74 140 107 187 207 212 124 202 52 232 55 184 + 229 106 132 152 120 92 110 179 235 65 70 87 110 + 195 200 175 234 160 234 136 80 113 187 109 121 124 + 196 156 210 239 250 72 109 188 202 191 105 154 79 + 231 147 225 103 161 169 223 96 83 249 212 162 227 + 228 167 180 193 117 177 214 145 208 235 150 110 211 + 103 158 200 168 229 92 156 243 280 279 116 173 269 + 271 131 133 223 128 173 197 210 99 150 161 147 218 + 240 109 136 146 261 263 124 130 252 171 190 213 274 + 108 195 226 119 124 171 198} + +do_execsql_test 1.6.9.4 { + SELECT last_value(a+b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2 +} {{} {} 196 134 109 213 223 106 234 191 212 168 229 + 147 {} {} 65 102 119 136 123 210 146 147 132 152 + 160 105 154 92 156 243 109 136 146 {} {} 87 138 + 63 124 179 78 141 84 120 234 79 231 162 227 {} {} + 57 181 110 179 57 187 114 191 206 221 92 110 136 + 147 167 180 193 279 124 {} {} 80 182 71 157 161 + 209 229 179 235 80 225 117 177 214 116 173 269 {} + {} 111 74 132 189 87 74 140 113 187 103 161 169 + 145 208 235 131 133 223 190 {} {} 26 51 52 85 172 + 173 107 187 207 212 65 70 109 121 124 223 150 128 + 108 {} {} 48 144 207 216 50 224 124 202 87 110 + 195 200 196 96 110 211 173 197 {} {} 36 121 132 + 88 52 232 156 210 239 250 83 103 158 210 {} {} + 101 163 172 163 59 111 170 55 184 229 175 72 109 + 188 202 249 200 99 150 161} + +do_execsql_test 1.6.9.5 { + SELECT last_value(a+b) OVER ( ORDER BY b%10,a ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2 +} {{} {} 147 106 109 168 134 218 191 212 229 240 213 + 234 196 223 92 109 105 136 146 65 156 132 154 102 + 123 119 160 152 146 147 136 243 261 263 210 79 63 + 84 78 120 87 162 124 141 138 227 228 179 231 234 + 280 124 57 130 92 57 110 114 136 147 167 110 180 + 193 191 252 187 179 206 181 221 279 80 116 117 71 + 80 171 173 177 157 161 179 214 225 182 209 269 271 + 235 229 103 74 131 133 113 74 87 145 190 161 169 + 140 111 132 213 187 208 223 235 189 274 108 65 26 + 70 51 52 128 109 121 124 85 107 150 195 226 172 + 173 187 223 207 212 119 50 124 96 110 87 48 110 + 173 124 197 211 144 196 195 200 202 224 216 207 52 + 83 103 36 88 171 158 156 198 121 210 132 210 239 + 250 232 99 72 55 120 59 109 150 161 111 101 200 + 175 188 170 202 163 184 163 172 276} + +do_execsql_test 1.6.9.6 { + SELECT last_value(a+b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.6.10.1 { + SELECT nth_value(b,b+1) OVER (ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} 89 {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + 53 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 58 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} 77 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.6.10.2 { + SELECT nth_value(b,b+1) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 61 {} {} {} 81 {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} 22 {} {} {} 12 + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.6.10.3 { + SELECT nth_value(b,b+1) OVER ( ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.6.10.4 { + SELECT nth_value(b,b+1) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.6.10.5 { + SELECT nth_value(b,b+1) OVER ( ORDER BY b%10,a ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 61 {} {} {} 81 {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} 22 {} {} {} 12 + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.6.10.6 { + SELECT nth_value(b,b+1) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.6.11.1 { + SELECT first_value(b) OVER (ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING) FROM t2 +} {{} {} 89 89 89 81 96 59 38 68 39 62 91 46 6 + 99 97 27 46 78 54 97 8 67 29 93 84 77 23 16 + 16 93 65 35 47 7 86 74 61 91 85 24 85 43 59 + 12 32 56 3 91 22 90 55 15 28 89 25 47 1 56 40 + 43 56 16 75 36 89 98 76 81 4 94 42 30 78 33 + 29 53 63 2 87 37 80 84 72 41 9 61 73 95 65 13 + 58 96 98 1 21 74 65 35 5 73 11 51 87 41 12 8 + 20 31 31 15 95 22 73 79 88 34 8 11 49 34 90 + 59 96 60 55 75 77 44 2 7 85 57 74 29 70 59 19 + 39 26 26 47 80 90 36 58 47 9 72 72 66 33 93 + 75 64 81 9 23 37 13 12 14 62 91 36 91 33 15 + 34 36 99 3 95 69 58 52 30 50 84 10 84 33 21 + 39 44 58 30 38 34 83} + +do_execsql_test 1.6.11.2 { + SELECT first_value(b) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING) FROM t2 +} {{} {} 90 90 90 40 30 80 20 90 60 70 80 90 {} + {} 81 81 81 91 61 91 91 1 81 41 61 1 21 11 51 + 41 31 31 11 {} {} 62 62 62 12 32 22 42 2 72 + 12 22 2 72 72 {} {} 93 93 93 23 93 43 3 43 33 + 53 63 73 13 73 73 33 93 23 13 {} {} 54 54 54 + 84 74 24 4 94 84 74 34 34 44 74 64 14 34 {} + {} 65 65 65 35 85 85 55 15 25 75 95 65 65 35 + 5 15 95 55 75 {} {} 96 96 96 46 6 46 16 16 86 + 56 56 56 16 36 76 96 96 26 26 {} {} 97 97 97 + 27 97 67 77 47 7 47 87 37 87 77 7 57 47 47 {} + {} 38 38 38 68 78 8 28 98 78 58 98 8 88 8 {} + {} 89 89 89 59 39 99 29 59 89 89 29 9 79 49 + 59 29 59 19 39 9} + +do_execsql_test 1.6.11.3 { + SELECT first_value(b) OVER ( ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2 +} {{} {} 1 1 1 1 2 2 3 3 4 5 6 7 7 7 8 8 8 + 9 9 9 10 11 11 12 12 12 13 13 14 15 15 15 16 + 16 16 17 19 20 21 21 22 22 23 23 24 25 26 26 + 27 27 28 29 29 29 30 30 30 31 31 32 33 33 33 + 33 34 34 34 34 35 35 36 36 36 36 37 37 38 38 + 39 39 39 40 41 41 42 43 43 44 44 46 46 47 47 + 47 47 49 50 51 52 53 54 55 55 56 56 56 57 58 + 58 58 58 59 59 59 59 60 61 61 62 62 63 64 65 + 65 65 66 67 68 69 70 72 72 72 73 73 73 74 74 + 74 75 75 75 76 77 77 78 78 79 80 80 81 81 81 + 82 83 84 84 84 84 85 85 85 86 87 87 88 89 89 + 89 90 90 90 91 91 91 91 91 93 93 93 94 95 95 + 95 96 96 96 97 97} + +do_execsql_test 1.6.11.4 { + SELECT first_value(b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2 +} {{} {} 10 10 10 20 30 30 30 40 50 60 70 80 {} + {} 1 1 1 1 11 11 21 21 31 31 41 41 51 61 61 + 81 81 81 91 {} {} 2 2 2 2 12 12 12 22 22 32 + 42 52 62 62 {} {} 3 3 3 3 13 13 23 23 33 33 + 33 33 43 43 53 63 73 73 73 {} {} 4 4 4 14 24 + 34 34 34 34 44 44 54 64 74 74 74 84 {} {} 5 5 + 5 15 15 15 25 35 35 55 55 65 65 65 75 75 75 + 85 85 {} {} 6 6 6 16 16 16 26 26 36 36 36 36 + 46 46 56 56 56 66 76 {} {} 7 7 7 7 7 17 27 + 27 37 37 47 47 47 47 57 67 77 77 {} {} 8 8 8 + 8 8 28 38 38 58 58 58 58 68 78 {} {} 9 9 9 + 9 9 19 29 29 29 39 39 39 49 59 59 59 59 69 79 + 89} + +do_execsql_test 1.6.11.5 { + SELECT first_value(b) OVER ( ORDER BY b%10,a ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2 +} {{} {} 90 90 90 40 30 80 20 90 60 70 80 90 30 + 50 10 30 81 91 61 91 91 1 81 41 61 1 21 11 51 + 41 31 31 11 81 91 91 21 62 12 32 22 42 2 72 + 12 22 2 72 72 12 62 52 82 93 23 93 43 3 43 33 + 53 63 73 13 73 73 33 93 23 13 33 3 33 83 54 + 84 74 24 4 94 84 74 34 34 44 74 64 14 34 84 + 84 44 34 65 35 85 85 55 15 25 75 95 65 65 35 + 5 15 95 55 75 85 75 15 95 96 46 6 46 16 16 86 + 56 56 56 16 36 76 96 96 26 26 36 66 36 36 97 + 27 97 67 77 47 7 47 87 37 87 77 7 57 47 47 37 + 27 17 7 38 68 78 8 28 98 78 58 98 8 88 8 58 + 58 58 38 89 59 39 99 29 59 89 89 29 9 79 49 + 59 29 59 19 39 9} + +do_execsql_test 1.6.11.6 { + SELECT first_value(b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.6.12.1 { + SELECT lead(b,b) OVER (ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING) FROM t2 +} {96 9 11 89 32 53 91 30 51 56 54 73 22 59 75 + 74 78 8 16 65 15 8 31 87 90 12 32 96 74 76 37 + 85 90 15 35 2 60 36 75 9 51 47 63 51 90 26 42 + 26 8 76 80 90 37 87 56 79 5 87 8 2 39 73 64 + 36 90 72 78 36 73 51 33 20 41 2 26 37 33 8 14 + 33 81 55 1 9 12 39 64 87 72 34 82 21 34 99 62 + 74 41 69 22 75 27 58 8 79 77 26 26 55 {} 29 + 30 7 {} 66 55 2 34 64 {} 33 {} 44 84 {} {} 95 + 85 19 {} 83 {} 91 {} {} 9 50 91 33 34 {} {} + 84 {} 7 9 {} {} {} 44 {} {} {} {} 91 84 {} 95 + 95 52 {} {} {} {} {} 21 {} {} {} 58 {} {} {} + {} {} {} {} 83 {} {} {} {} {} {} {} {} {} {} + {} {} {} {}} + +do_execsql_test 1.6.12.2 { + SELECT lead(b,b) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 81 {} {} {} 21 {} {} {} {} {} {} + {} {} {} {} {} {} 62 {} {} {} 12 {} {} {} 72 + {} {} {} {} {} {} {} {} {} {} 53 {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 34 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} 95 {} {} {} {} {} {} 85 {} + {} {} {} {} {} {} {} {} {} 56 {} 36 {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 57 {} {} {} {} {} 7 {} {} {} {} + {} {} {} {} {} {} 8 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 9 {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.6.12.3 { + SELECT lead(b,b) OVER ( ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2 +} {1 2 3 3 5 6 7 8 8 9 9 10 11 12 12 13 13 + 14 15 16 16 17 19 20 21 22 23 24 25 26 27 27 + 28 29 30 31 32 33 33 33 34 34 35 36 36 36 37 + 38 39 39 40 41 42 43 43 44 46 47 47 47 49 50 + 52 53 54 55 56 56 57 58 58 58 59 59 59 60 61 + 62 62 64 65 65 67 69 70 72 72 73 74 74 75 75 + 75 77 78 80 81 81 83 84 84 85 85 85 87 88 89 + 89 89 90 90 91 91 91 93 93 94 95 95 96 97 97 + 98 99 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.6.12.4 { + SELECT lead(b,b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2 +} {80 {} {} {} {} {} {} {} {} {} {} {} {} {} 1 + 11 81 81 {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} 12 12 72 82 {} {} {} {} {} {} + {} {} {} {} {} {} 13 23 73 73 {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} 34 84 {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 35 85 85 95 {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} 36 86 96 96 {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 37 47 + 47 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} 58 58 68 {} {} {} {} {} {} {} {} {} + {} {} {} {} 39 49 59 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.6.12.5 { + SELECT lead(b,b) OVER ( ORDER BY b%10,a ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2 +} {34 72 91 74 21 55 23 94 84 95 2 13 61 22 55 + 55 94 85 75 81 65 73 74 21 72 12 94 33 63 73 + 72 96 36 76 3 25 62 3 73 34 12 46 43 93 72 16 + 86 63 15 65 36 77 24 57 25 53 95 34 95 16 97 + 74 97 67 25 98 44 34 65 54 5 68 96 28 47 95 + 34 39 8 38 6 46 96 28 47 95 56 39 99 97 76 8 + 26 9 79 27 95 16 29 {} 58 58 77 85 56 {} 98 + 29 {} 19 96 {} {} 78 56 98 36 97 {} 89 89 29 + 47 78 {} {} {} 38 68 58 {} 58 38 {} 98 {} {} + {} 39 57 9 {} 79 {} {} 7 {} {} {} 9 29 38 78 + {} {} {} 8 39 {} {} {} {} 59 {} 99 {} {} {} + {} {} {} {} {} {} {} {} {} {} 9 {} {} {} {} + {} {} {} {} {} {} {} {}} + +do_execsql_test 1.6.12.6 { + SELECT lead(b,b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.6.13.1 { + SELECT lag(b,b) OVER (ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} 38 {} {} {} {} + {} {} {} 6 {} {} {} {} {} 81 46 6 {} {} {} {} + 23 {} {} {} {} {} 27 {} {} {} 35 6 {} 12 {} + 23 {} {} 61 84 {} 93 39 47 {} 54 46 96 56 {} + 16 {} {} {} {} 89 {} 16 43 {} 85 56 29 99 53 + {} 59 {} {} 91 59 53 84 99 {} 93 63 47 {} {} + 98 33 67 35 75 1 23 13 55 27 75 98 35 73 63 2 + 21 27 13 24 86 23 84 31 20 94 61 65 75 23 36 + 94 55 90 41 77 96 56 29 40 12 89 63 11 5 73 + 79 1 16 28 31 73 5 39 53 63 41 11 40 2 13 33 + 9 29 90 47 72 9 73 30 44 33 74 93 29 74 42 34 + 63 41 34 96 47 77 1 36 74 72 14 36 26 77 9 72 + 64 8 91 31 52 30} + +do_execsql_test 1.6.13.2 { + SELECT lag(b,b) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} 30 {} {} + {} {} {} {} 91 {} {} {} 61 {} 81 {} {} {} {} + 1 {} {} {} {} {} {} {} {} {} 22 {} {} {} 12 + {} {} 62 {} {} {} {} {} {} {} 23 {} {} {} {} + {} {} {} {} {} {} {} 43 {} 23 {} {} {} {} {} + {} 54 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 75 {} + {} {} {} {} {} 55 {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} 47 {} {} {} {} + {} 27 7 {} {} {} {} {} {} {} {} {} 68 {} 8 {} + {} {} {} {} {} {} {} {} {} {} {} {} 89 {} {} + {} {} {} {} {} 29 9 {} {} {}} + +do_execsql_test 1.6.13.3 { + SELECT lag(b,b) OVER ( ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2 +} {{} 1 1 1 1 2 2 2 2 2 2 3 3 3 4 4 5 6 6 + 6 7 7 7 7 7 8 8 8 8 8 8 9 9 9 9 9 9 9 + 9 9 9 10 10 10 10 11 11 11 11 11 12 12 12 12 + 13 13 13 13 13 14 15 15 15 15 16 16 16 16 16 + 17 19 20 20 21 21 21 21 22 22 22 22 23 23 23 + 23 23 24 23 24 24 25 26 26 26 26 26 26 26 26 + 26 26 26 27 27 27 27 28 29 29 29 29 30 30 30 + 30 30 30 31 31 31 31 31 32 32 32 32 32 32 31 + 32 33 33 33 33 33 33 34 34 34 34 34 34 34 34 + 35 35 35 35 35 36 36 36 36 36 36 36 37 37 37 + 38 38 38 38 38 38 39 39 39 39 40 40 41 41 42 + 43 42 43 43 43 43 44 44 44 46 46 46 47 47 47 + 47 47} + +do_execsql_test 1.6.13.4 { + SELECT lag(b,b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + 1 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.6.13.5 { + SELECT lag(b,b) OVER ( ORDER BY b%10,a ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} 30 {} {} + {} {} {} {} 91 {} {} {} 61 80 81 {} {} {} {} + 1 {} {} {} 30 {} 21 90 61 {} 22 {} 11 41 12 + {} {} 62 {} {} {} {} 31 {} 50 23 30 21 90 {} + {} 62 {} {} 81 {} 22 43 62 23 32 {} 91 {} 90 + 93 54 {} {} 90 72 12 22 90 81 83 23 80 20 72 + 43 51 33 80 90 2 34 54 1 20 62 12 13 75 44 30 + 93 91 1 21 55 61 61 13 85 3 65 65 91 73 33 93 + 55 84 62 31 11 65 35 85 33 55 15 12 75 22 3 + 73 65 36 85 43 95 43 13 47 44 65 65 96 36 27 + 7 46 34 94 47 36 73 34 35 73 68 24 8 75 85 75 + 66 34 95 36 84 77 46 34 84 47 89 65 36 16 38 + 76 58 57 29 9 44 56 17} + +do_execsql_test 1.6.13.6 { + SELECT lag(b,b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.6.14.1 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING) FROM t2 +} {{} {} 89 89.81 89.81.96 81.96.59 96.59.38 59.38.68 38.68.39 + 68.39.62 39.62.91 62.91.46 91.46.6 46.6.99 6.99.97 99.97.27 + 97.27.46 27.46.78 46.78.54 78.54.97 54.97.8 97.8.67 8.67.29 + 67.29.93 29.93.84 93.84.77 84.77.23 77.23.16 23.16.16 16.16.93 + 16.93.65 93.65.35 65.35.47 35.47.7 47.7.86 7.86.74 86.74.61 + 74.61.91 61.91.85 91.85.24 85.24.85 24.85.43 85.43.59 43.59.12 + 59.12.32 12.32.56 32.56.3 56.3.91 3.91.22 91.22.90 22.90.55 + 90.55.15 55.15.28 15.28.89 28.89.25 89.25.47 25.47.1 47.1.56 + 1.56.40 56.40.43 40.43.56 43.56.16 56.16.75 16.75.36 75.36.89 + 36.89.98 89.98.76 98.76.81 76.81.4 81.4.94 4.94.42 94.42.30 + 42.30.78 30.78.33 78.33.29 33.29.53 29.53.63 53.63.2 63.2.87 + 2.87.37 87.37.80 37.80.84 80.84.72 84.72.41 72.41.9 41.9.61 + 9.61.73 61.73.95 73.95.65 95.65.13 65.13.58 13.58.96 58.96.98 + 96.98.1 98.1.21 1.21.74 21.74.65 74.65.35 65.35.5 35.5.73 + 5.73.11 73.11.51 11.51.87 51.87.41 87.41.12 41.12.8 12.8.20 + 8.20.31 20.31.31 31.31.15 31.15.95 15.95.22 95.22.73 22.73.79 + 73.79.88 79.88.34 88.34.8 34.8.11 8.11.49 11.49.34 49.34.90 + 34.90.59 90.59.96 59.96.60 96.60.55 60.55.75 55.75.77 75.77.44 + 77.44.2 44.2.7 2.7.85 7.85.57 85.57.74 57.74.29 74.29.70 + 29.70.59 70.59.19 59.19.39 19.39.26 39.26.26 26.26.47 26.47.80 + 47.80.90 80.90.36 90.36.58 36.58.47 58.47.9 47.9.72 9.72.72 + 72.72.66 72.66.33 66.33.93 33.93.75 93.75.64 75.64.81 64.81.9 + 81.9.23 9.23.37 23.37.13 37.13.12 13.12.14 12.14.62 14.62.91 + 62.91.36 91.36.91 36.91.33 91.33.15 33.15.34 15.34.36 34.36.99 + 36.99.3 99.3.95 3.95.69 95.69.58 69.58.52 58.52.30 52.30.50 + 30.50.84 50.84.10 84.10.84 10.84.33 84.33.21 33.21.39 21.39.44 + 39.44.58 44.58.30 58.30.38 30.38.34 38.34.83 34.83.27 83.27.82} + +do_execsql_test 1.6.14.2 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING) FROM t2 +} {{} {} 90 90.40 90.40.30 40.30.80 30.80.20 80.20.90 20.90.60 + 90.60.70 60.70.80 70.80.90 80.90.30 90.30.50 {} {} 81 81.91 + 81.91.61 91.61.91 61.91.91 91.91.1 91.1.81 1.81.41 81.41.61 + 41.61.1 61.1.21 1.21.11 21.11.51 11.51.41 51.41.31 41.31.31 + 31.31.11 31.11.81 11.81.91 {} {} 62 62.12 62.12.32 12.32.22 + 32.22.42 22.42.2 42.2.72 2.72.12 72.12.22 12.22.2 22.2.72 + 2.72.72 72.72.12 72.12.62 {} {} 93 93.23 93.23.93 23.93.43 + 93.43.3 43.3.43 3.43.33 43.33.53 33.53.63 53.63.73 63.73.13 + 73.13.73 13.73.73 73.73.33 73.33.93 33.93.23 93.23.13 23.13.33 + 13.33.3 {} {} 54 54.84 54.84.74 84.74.24 74.24.4 24.4.94 + 4.94.84 94.84.74 84.74.34 74.34.34 34.34.44 34.44.74 44.74.64 + 74.64.14 64.14.34 14.34.84 34.84.84 {} {} 65 65.35 65.35.85 + 35.85.85 85.85.55 85.55.15 55.15.25 15.25.75 25.75.95 75.95.65 + 95.65.65 65.65.35 65.35.5 35.5.15 5.15.95 15.95.55 95.55.75 + 55.75.85 75.85.75 {} {} 96 96.46 96.46.6 46.6.46 6.46.16 + 46.16.16 16.16.86 16.86.56 86.56.56 56.56.56 56.56.16 56.16.36 + 16.36.76 36.76.96 76.96.96 96.96.26 96.26.26 26.26.36 26.36.66 + {} {} 97 97.27 97.27.97 27.97.67 97.67.77 67.77.47 77.47.7 + 47.7.47 7.47.87 47.87.37 87.37.87 37.87.77 87.77.7 77.7.57 + 7.57.47 57.47.47 47.47.37 47.37.27 {} {} 38 38.68 38.68.78 + 68.78.8 78.8.28 8.28.98 28.98.78 98.78.58 78.58.98 58.98.8 + 98.8.88 8.88.8 88.8.58 8.58.58 {} {} 89 89.59 89.59.39 + 59.39.99 39.99.29 99.29.59 29.59.89 59.89.89 89.89.29 89.29.9 + 29.9.79 9.79.49 79.49.59 49.59.29 59.29.59 29.59.19 59.19.39 + 19.39.9 39.9.9 9.9.99} + +do_execsql_test 1.6.14.3 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2 +} {{} {} 1 1.1 1.1.2 1.2.2 2.2.3 2.3.3 3.3.4 3.4.5 4.5.6 + 5.6.7 6.7.7 7.7.7 7.7.8 7.8.8 8.8.8 8.8.9 8.9.9 9.9.9 + 9.9.10 9.10.11 10.11.11 11.11.12 11.12.12 12.12.12 12.12.13 + 12.13.13 13.13.14 13.14.15 14.15.15 15.15.15 15.15.16 15.16.16 + 16.16.16 16.16.17 16.17.19 17.19.20 19.20.21 20.21.21 21.21.22 + 21.22.22 22.22.23 22.23.23 23.23.24 23.24.25 24.25.26 25.26.26 + 26.26.27 26.27.27 27.27.28 27.28.29 28.29.29 29.29.29 29.29.30 + 29.30.30 30.30.30 30.30.31 30.31.31 31.31.32 31.32.33 32.33.33 + 33.33.33 33.33.33 33.33.34 33.34.34 34.34.34 34.34.34 34.34.35 + 34.35.35 35.35.36 35.36.36 36.36.36 36.36.36 36.36.37 36.37.37 + 37.37.38 37.38.38 38.38.39 38.39.39 39.39.39 39.39.40 39.40.41 + 40.41.41 41.41.42 41.42.43 42.43.43 43.43.44 43.44.44 44.44.46 + 44.46.46 46.46.47 46.47.47 47.47.47 47.47.47 47.47.49 47.49.50 + 49.50.51 50.51.52 51.52.53 52.53.54 53.54.55 54.55.55 55.55.56 + 55.56.56 56.56.56 56.56.57 56.57.58 57.58.58 58.58.58 58.58.58 + 58.58.59 58.59.59 59.59.59 59.59.59 59.59.60 59.60.61 60.61.61 + 61.61.62 61.62.62 62.62.63 62.63.64 63.64.65 64.65.65 65.65.65 + 65.65.66 65.66.67 66.67.68 67.68.69 68.69.70 69.70.72 70.72.72 + 72.72.72 72.72.73 72.73.73 73.73.73 73.73.74 73.74.74 74.74.74 + 74.74.75 74.75.75 75.75.75 75.75.76 75.76.77 76.77.77 77.77.78 + 77.78.78 78.78.79 78.79.80 79.80.80 80.80.81 80.81.81 81.81.81 + 81.81.82 81.82.83 82.83.84 83.84.84 84.84.84 84.84.84 84.84.85 + 84.85.85 85.85.85 85.85.86 85.86.87 86.87.87 87.87.88 87.88.89 + 88.89.89 89.89.89 89.89.90 89.90.90 90.90.90 90.90.91 90.91.91 + 91.91.91 91.91.91 91.91.91 91.91.93 91.93.93 93.93.93 93.93.94 + 93.94.95 94.95.95 95.95.95 95.95.96 95.96.96 96.96.96 96.96.97 + 96.97.97 97.97.98 97.98.98} + +do_execsql_test 1.6.14.4 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2 +} {{} {} 10 10.20 10.20.30 20.30.30 30.30.30 30.30.40 30.40.50 + 40.50.60 50.60.70 60.70.80 70.80.80 80.80.90 {} {} 1 1.1 + 1.1.11 1.11.11 11.11.21 11.21.21 21.21.31 21.31.31 31.31.41 + 31.41.41 41.41.51 41.51.61 51.61.61 61.61.81 61.81.81 81.81.81 + 81.81.91 81.91.91 91.91.91 {} {} 2 2.2 2.2.12 2.12.12 + 12.12.12 12.12.22 12.22.22 22.22.32 22.32.42 32.42.52 42.52.62 + 52.62.62 62.62.72 62.72.72 {} {} 3 3.3 3.3.13 3.13.13 + 13.13.23 13.23.23 23.23.33 23.33.33 33.33.33 33.33.33 33.33.43 + 33.43.43 43.43.53 43.53.63 53.63.73 63.73.73 73.73.73 73.73.83 + 73.83.93 {} {} 4 4.14 4.14.24 14.24.34 24.34.34 34.34.34 + 34.34.34 34.34.44 34.44.44 44.44.54 44.54.64 54.64.74 64.74.74 + 74.74.74 74.74.84 74.84.84 84.84.84 {} {} 5 5.15 5.15.15 + 15.15.15 15.15.25 15.25.35 25.35.35 35.35.55 35.55.55 55.55.65 + 55.65.65 65.65.65 65.65.75 65.75.75 75.75.75 75.75.85 75.85.85 + 85.85.85 85.85.95 {} {} 6 6.16 6.16.16 16.16.16 16.16.26 + 16.26.26 26.26.36 26.36.36 36.36.36 36.36.36 36.36.46 36.46.46 + 46.46.56 46.56.56 56.56.56 56.56.66 56.66.76 66.76.86 76.86.96 + {} {} 7 7.7 7.7.7 7.7.17 7.17.27 17.27.27 27.27.37 + 27.37.37 37.37.47 37.47.47 47.47.47 47.47.47 47.47.57 47.57.67 + 57.67.77 67.77.77 77.77.87 77.87.87 {} {} 8 8.8 8.8.8 + 8.8.28 8.28.38 28.38.38 38.38.58 38.58.58 58.58.58 58.58.58 + 58.58.68 58.68.78 68.78.78 78.78.88 {} {} 9 9.9 9.9.9 + 9.9.19 9.19.29 19.29.29 29.29.29 29.29.39 29.39.39 39.39.39 + 39.39.49 39.49.59 49.59.59 59.59.59 59.59.59 59.59.69 59.69.79 + 69.79.89 79.89.89 89.89.89} + +do_execsql_test 1.6.14.5 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( ORDER BY b%10,a ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING ) FROM t2 +} {{} {} 90 90.40 90.40.30 40.30.80 30.80.20 80.20.90 20.90.60 + 90.60.70 60.70.80 70.80.90 80.90.30 90.30.50 30.50.10 50.10.30 + 10.30.81 30.81.91 81.91.61 91.61.91 61.91.91 91.91.1 91.1.81 + 1.81.41 81.41.61 41.61.1 61.1.21 1.21.11 21.11.51 11.51.41 + 51.41.31 41.31.31 31.31.11 31.11.81 11.81.91 81.91.91 91.91.21 + 91.21.62 21.62.12 62.12.32 12.32.22 32.22.42 22.42.2 42.2.72 + 2.72.12 72.12.22 12.22.2 22.2.72 2.72.72 72.72.12 72.12.62 + 12.62.52 62.52.82 52.82.93 82.93.23 93.23.93 23.93.43 93.43.3 + 43.3.43 3.43.33 43.33.53 33.53.63 53.63.73 63.73.13 73.13.73 + 13.73.73 73.73.33 73.33.93 33.93.23 93.23.13 23.13.33 13.33.3 + 33.3.33 3.33.83 33.83.54 83.54.84 54.84.74 84.74.24 74.24.4 + 24.4.94 4.94.84 94.84.74 84.74.34 74.34.34 34.34.44 34.44.74 + 44.74.64 74.64.14 64.14.34 14.34.84 34.84.84 84.84.44 84.44.34 + 44.34.65 34.65.35 65.35.85 35.85.85 85.85.55 85.55.15 55.15.25 + 15.25.75 25.75.95 75.95.65 95.65.65 65.65.35 65.35.5 35.5.15 + 5.15.95 15.95.55 95.55.75 55.75.85 75.85.75 85.75.15 75.15.95 + 15.95.96 95.96.46 96.46.6 46.6.46 6.46.16 46.16.16 16.16.86 + 16.86.56 86.56.56 56.56.56 56.56.16 56.16.36 16.36.76 36.76.96 + 76.96.96 96.96.26 96.26.26 26.26.36 26.36.66 36.66.36 66.36.36 + 36.36.97 36.97.27 97.27.97 27.97.67 97.67.77 67.77.47 77.47.7 + 47.7.47 7.47.87 47.87.37 87.37.87 37.87.77 87.77.7 77.7.57 + 7.57.47 57.47.47 47.47.37 47.37.27 37.27.17 27.17.7 17.7.38 + 7.38.68 38.68.78 68.78.8 78.8.28 8.28.98 28.98.78 98.78.58 + 78.58.98 58.98.8 98.8.88 8.88.8 88.8.58 8.58.58 58.58.58 + 58.58.38 58.38.89 38.89.59 89.59.39 59.39.99 39.99.29 99.29.59 + 29.59.89 59.89.89 89.89.29 89.29.9 29.9.79 9.79.49 79.49.59 + 49.59.29 59.29.59 29.59.19 59.19.39 19.39.9 39.9.9 9.9.99} + +do_execsql_test 1.6.14.6 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.6.14.7 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (win1 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING) + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a) + ORDER BY 1 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.6.14.8 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (win1 ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING) + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a ORDER BY b%10) + ORDER BY 1 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.6.14.9 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER win2 + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a ORDER BY b%10), + win2 AS (win1 ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING) + ORDER BY 1 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.6.15.1 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE a%2=0) OVER win FROM t2 + WINDOW win AS (ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING) +} {0 {} 0 {} 1 89 2 89 3 89.96 3 96 3 96.38 3 38 3 38.39 + 3 39 3 39.91 3 91 3 91.6 3 6 3 6.97 3 97 3 97.46 3 46 + 3 46.54 3 54 3 54.8 3 8 3 8.29 3 29 3 29.84 3 84 3 84.23 + 3 23 3 23.16 3 16 3 16.65 3 65 3 65.47 3 47 3 47.86 3 86 + 3 86.61 3 61 3 61.85 3 85 3 85.85 3 85 3 85.59 3 59 3 59.32 + 3 32 3 32.3 3 3 3 3.22 3 22 3 22.55 3 55 3 55.28 3 28 + 3 28.25 3 25 3 25.1 3 1 3 1.40 3 40 3 40.56 3 56 3 56.75 + 3 75 3 75.89 3 89 3 89.76 3 76 3 76.4 3 4 3 4.42 3 42 + 3 42.78 3 78 3 78.29 3 29 3 29.63 3 63 3 63.87 3 87 3 87.80 + 3 80 3 80.72 3 72 3 72.9 3 9 3 9.73 3 73 3 73.65 3 65 + 3 65.58 3 58 3 58.98 3 98 3 98.21 3 21 3 21.65 3 65 3 65.5 + 3 5 3 5.11 3 11 3 11.87 3 87 3 87.12 3 12 3 12.20 3 20 + 3 20.31 3 31 3 31.95 3 95 3 95.73 3 73 3 73.88 3 88 3 88.8 + 3 8 3 8.49 3 49 3 49.90 3 90 3 90.96 3 96 3 96.55 3 55 + 3 55.77 3 77 3 77.2 3 2 3 2.85 3 85 3 85.74 3 74 3 74.70 + 3 70 3 70.19 3 19 3 19.26 3 26 3 26.47 3 47 3 47.90 3 90 + 3 90.58 3 58 3 58.9 3 9 3 9.72 3 72 3 72.33 3 33 3 33.75 + 3 75 3 75.81 3 81 3 81.23 3 23 3 23.13 3 13 3 13.14 3 14 + 3 14.91 3 91 3 91.91 3 91 3 91.15 3 15 3 15.36 3 36 3 36.3 + 3 3 3 3.69 3 69 3 69.52 3 52 3 52.50 3 50 3 50.10 3 10 + 3 10.33 3 33 3 33.39 3 39 3 39.58 3 58 3 58.38 3 38 3 38.83 + 3 83 3 83.82} + +do_execsql_test 1.6.15.2 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE 0=1) OVER win FROM t2 + WINDOW win AS (ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING) +} {0 {} 0 {} 1 {} 2 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {}} + +do_execsql_test 1.6.15.3 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE 1=0) OVER win FROM t2 + WINDOW win AS (PARTITION BY (a%10) ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING) +} {0 {} 0 {} 1 {} 2 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 0 {} 0 {} + 1 {} 2 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 0 {} 0 {} 1 {} 2 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 0 {} 0 {} 1 {} 2 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 0 {} 0 {} 1 {} 2 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 0 {} 0 {} 1 {} + 2 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 0 {} 0 {} 1 {} 2 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 0 {} 0 {} 1 {} 2 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 0 {} + 0 {} 1 {} 2 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 0 {} 0 {} 1 {} 2 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {}} + +do_execsql_test 1.6.15.4 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE a%2=0) OVER win FROM t2 + WINDOW win AS (PARTITION BY (a%10) ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 PRECEDING) +} {0 {} 0 {} 1 89 2 89.6 3 89.6.29 3 6.29.47 3 29.47.59 + 3 47.59.28 3 59.28.75 3 28.75.78 3 75.78.72 3 78.72.98 3 72.98.87 + 3 98.87.73 3 87.73.96 3 73.96.74 3 96.74.90 3 74.90.75 3 90.75.91 + 3 75.91.69 0 {} 0 {} 1 {} 2 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 0 {} + 0 {} 1 96 2 96.97 3 96.97.84 3 97.84.86 3 84.86.32 3 86.32.25 + 3 32.25.89 3 25.89.29 3 89.29.9 3 29.9.21 3 9.21.12 3 21.12.88 + 3 12.88.55 3 88.55.70 3 55.70.58 3 70.58.81 3 58.81.91 0 {} + 0 {} 1 {} 2 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 0 {} 0 {} 1 38 + 2 38.46 3 38.46.23 3 46.23.61 3 23.61.3 3 61.3.1 3 3.1.76 + 3 1.76.63 3 76.63.73 3 63.73.65 3 73.65.20 3 65.20.8 3 20.8.77 + 3 8.77.19 3 77.19.9 3 19.9.23 3 9.23.15 0 {} 0 {} 1 {} 2 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 0 {} 0 {} 1 39 2 39.54 3 39.54.16 + 3 54.16.85 3 16.85.22 3 85.22.40 3 22.40.4 3 40.4.87 3 4.87.65 + 3 87.65.5 3 65.5.31 3 5.31.49 3 31.49.2 3 49.2.26 3 2.26.72 + 3 26.72.13 3 72.13.36 0 {} 0 {} 1 {} 2 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 0 {} 0 {} 1 91 2 91.8 3 91.8.65 3 8.65.85 3 65.85.55 + 3 85.55.56 3 55.56.42 3 56.42.80 3 42.80.58 3 80.58.11 3 58.11.95 + 3 11.95.90 3 95.90.85 3 90.85.47 3 85.47.33 3 47.33.14 3 33.14.3 + 0 {} 0 {} 1 {} 2 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {}} + +do_execsql_test 1.7.2.1 { + SELECT max(b) OVER ( ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {89 89 96 96 96 96 96 96 96 96 96 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99} + +do_execsql_test 1.7.2.2 { + SELECT min(b) OVER ( ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {89 81 81 59 38 38 38 38 38 38 6 6 6 6 6 6 6 + 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 + 6 6 6 6 6 6 6 6 3 3 3 3 3 3 3 3 3 3 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1} + +do_execsql_test 1.7.3.1 { + SELECT row_number() OVER ( ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.7.3.2 { + SELECT row_number() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.7.3.3 { + SELECT row_number() OVER ( ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.7.4.1 { + SELECT dense_rank() OVER ( ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.7.4.2 { + SELECT dense_rank() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.7.4.3 { + SELECT dense_rank() OVER ( ORDER BY b ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {1 1 2 2 3 3 4 5 6 7 7 7 8 8 8 9 9 9 10 + 11 11 12 12 12 13 13 14 15 15 15 16 16 16 17 + 18 19 20 20 21 21 22 22 23 24 25 25 26 26 27 + 28 28 28 29 29 29 30 30 31 32 32 32 32 33 33 + 33 33 34 34 35 35 35 35 36 36 37 37 38 38 38 + 39 40 40 41 42 42 43 43 44 44 45 45 45 45 46 + 47 48 49 50 51 52 52 53 53 53 54 55 55 55 55 + 56 56 56 56 57 58 58 59 59 60 61 62 62 62 63 + 64 65 66 67 68 68 68 69 69 69 70 70 70 71 71 + 71 72 73 73 74 74 75 76 76 77 77 77 78 79 80 + 80 80 80 81 81 81 82 83 83 84 85 85 85 86 86 + 86 87 87 87 87 87 88 88 88 89 90 90 90 91 91 + 91 92 92 93 93 94 94} + +do_execsql_test 1.7.4.4 { + SELECT dense_rank() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {1 2 3 3 3 4 5 6 7 8 8 9 9 9 1 1 2 2 3 + 3 4 4 5 5 6 7 7 8 8 8 9 9 9 9 9 1 1 2 + 2 2 3 3 4 5 6 7 7 8 8 8 9 1 1 2 2 3 3 + 4 4 4 4 5 5 6 7 8 8 8 9 10 10 10 1 2 3 + 4 4 4 4 5 5 6 7 8 8 8 9 9 9 9 10 1 2 2 + 2 3 4 4 5 5 6 6 6 7 7 7 8 8 8 9 9 9 1 + 2 2 2 3 3 4 4 4 4 5 5 6 6 6 7 8 9 10 10 + 10 1 1 1 2 3 3 4 4 5 5 5 5 6 7 8 8 9 9 + 10 10 1 1 1 2 3 3 4 4 4 4 5 6 6 7 8 8 1 + 1 1 2 3 3 3 4 4 4 5 6 6 6 6 7 8 9 9 9 + 10 10} + +do_execsql_test 1.7.4.5 { + SELECT dense_rank() OVER ( ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 + 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 + 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 + 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 + 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 + 8 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 10 10 + 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 + 10 10 10 10 10} + +do_execsql_test 1.7.4.6 { + SELECT dense_rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 + 5 5 5 5 5 5 5 5 5 5 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 + 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 + 5} + +do_execsql_test 1.7.5.1 { + SELECT rank() OVER ( ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.7.5.2 { + SELECT rank() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.7.5.3 { + SELECT rank() OVER ( ORDER BY b ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {1 1 3 3 5 5 7 8 9 10 10 10 13 13 13 16 16 + 16 19 20 20 22 22 22 25 25 27 28 28 28 31 31 + 31 34 35 36 37 37 39 39 41 41 43 44 45 45 47 + 47 49 50 50 50 53 53 53 56 56 58 59 59 59 59 + 63 63 63 63 67 67 69 69 69 69 73 73 75 75 77 + 77 77 80 81 81 83 84 84 86 86 88 88 90 90 90 + 90 94 95 96 97 98 99 100 100 102 102 102 105 106 + 106 106 106 110 110 110 110 114 115 115 117 117 119 + 120 121 121 121 124 125 126 127 128 129 129 129 132 + 132 132 135 135 135 138 138 138 141 142 142 144 144 + 146 147 147 149 149 149 152 153 154 154 154 154 158 + 158 158 161 162 162 164 165 165 165 168 168 168 171 + 171 171 171 171 176 176 176 179 180 180 180 183 183 + 183 186 186 188 188 190 190} + +do_execsql_test 1.7.5.4 { + SELECT rank() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {1 2 3 3 3 6 7 8 9 10 10 12 12 12 1 1 3 3 + 5 5 7 7 9 9 11 12 12 14 14 14 17 17 17 17 + 17 1 1 3 3 3 6 6 8 9 10 11 11 13 13 13 16 + 1 1 3 3 5 5 7 7 7 7 11 11 13 14 15 15 15 + 18 19 19 19 1 2 3 4 4 4 4 8 8 10 11 12 12 + 12 15 15 15 15 19 1 2 2 2 5 6 6 8 8 10 10 + 10 13 13 13 16 16 16 19 19 19 1 2 2 2 5 5 7 + 7 7 7 11 11 13 13 13 16 17 18 19 19 19 1 1 + 1 4 5 5 7 7 9 9 9 9 13 14 15 15 17 17 19 + 19 1 1 1 4 5 5 7 7 7 7 11 12 12 14 15 15 + 1 1 1 4 5 5 5 8 8 8 11 12 12 12 12 16 17 + 18 18 18 21 21} + +do_execsql_test 1.7.5.5 { + SELECT rank() OVER ( ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 15 15 15 15 + 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 + 15 15 36 36 36 36 36 36 36 36 36 36 36 36 36 + 36 36 36 52 52 52 52 52 52 52 52 52 52 52 52 + 52 52 52 52 52 52 52 52 52 73 73 73 73 73 73 + 73 73 73 73 73 73 73 73 73 73 73 73 73 92 92 + 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 + 92 92 92 92 113 113 113 113 113 113 113 113 113 + 113 113 113 113 113 113 113 113 113 113 113 113 134 + 134 134 134 134 134 134 134 134 134 134 134 134 134 + 134 134 134 134 134 134 154 154 154 154 154 154 154 + 154 154 154 154 154 154 154 154 154 170 170 170 170 + 170 170 170 170 170 170 170 170 170 170 170 170 170 + 170 170 170 170 170} + +do_execsql_test 1.7.5.6 { + SELECT rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 15 15 15 15 + 15 15 15 15 15 15 15 15 15 15 15 15 31 31 31 + 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 + 31 50 50 50 50 50 50 50 50 50 50 50 50 50 50 + 50 50 50 50 50 50 50 71 71 71 71 71 71 71 71 + 71 71 71 71 71 71 71 71 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 22 22 22 22 22 22 + 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 + 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 + 43 43 43 43 43 43 64 64 64 64 64 64 64 64 64 + 64 64 64 64 64 64 64 64 64 64 64 84 84 84 84 + 84 84 84 84 84 84 84 84 84 84 84 84 84 84 84 + 84 84 84} + +do_execsql_test 1.7.6.1 { + SELECT + row_number() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ), + rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ), + dense_rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) + FROM t2 +} {1 1 1 2 1 1 3 1 1 4 1 1 5 1 1 6 1 1 7 1 1 8 1 1 9 1 1 + 10 1 1 11 1 1 12 1 1 13 1 1 14 1 1 15 15 2 16 15 2 17 15 2 + 18 15 2 19 15 2 20 15 2 21 15 2 22 15 2 23 15 2 24 15 2 + 25 15 2 26 15 2 27 15 2 28 15 2 29 15 2 30 15 2 31 31 3 + 32 31 3 33 31 3 34 31 3 35 31 3 36 31 3 37 31 3 38 31 3 + 39 31 3 40 31 3 41 31 3 42 31 3 43 31 3 44 31 3 45 31 3 + 46 31 3 47 31 3 48 31 3 49 31 3 50 50 4 51 50 4 52 50 4 + 53 50 4 54 50 4 55 50 4 56 50 4 57 50 4 58 50 4 59 50 4 + 60 50 4 61 50 4 62 50 4 63 50 4 64 50 4 65 50 4 66 50 4 + 67 50 4 68 50 4 69 50 4 70 50 4 71 71 5 72 71 5 73 71 5 + 74 71 5 75 71 5 76 71 5 77 71 5 78 71 5 79 71 5 80 71 5 + 81 71 5 82 71 5 83 71 5 84 71 5 85 71 5 86 71 5 1 1 1 2 1 1 + 3 1 1 4 1 1 5 1 1 6 1 1 7 1 1 8 1 1 9 1 1 10 1 1 11 1 1 + 12 1 1 13 1 1 14 1 1 15 1 1 16 1 1 17 1 1 18 1 1 19 1 1 + 20 1 1 21 1 1 22 22 2 23 22 2 24 22 2 25 22 2 26 22 2 27 22 2 + 28 22 2 29 22 2 30 22 2 31 22 2 32 22 2 33 22 2 34 22 2 + 35 22 2 36 22 2 37 22 2 38 22 2 39 22 2 40 22 2 41 22 2 + 42 22 2 43 43 3 44 43 3 45 43 3 46 43 3 47 43 3 48 43 3 + 49 43 3 50 43 3 51 43 3 52 43 3 53 43 3 54 43 3 55 43 3 + 56 43 3 57 43 3 58 43 3 59 43 3 60 43 3 61 43 3 62 43 3 + 63 43 3 64 64 4 65 64 4 66 64 4 67 64 4 68 64 4 69 64 4 + 70 64 4 71 64 4 72 64 4 73 64 4 74 64 4 75 64 4 76 64 4 + 77 64 4 78 64 4 79 64 4 80 64 4 81 64 4 82 64 4 83 64 4 + 84 84 5 85 84 5 86 84 5 87 84 5 88 84 5 89 84 5 90 84 5 + 91 84 5 92 84 5 93 84 5 94 84 5 95 84 5 96 84 5 97 84 5 + 98 84 5 99 84 5 100 84 5 101 84 5 102 84 5 103 84 5 104 84 5 + 105 84 5} + + +do_test 1.7.7.1 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0053 0.0105 0.0158 0.0211 0.0263 0.0316 0.0368 0.0421 0.0474 0.0526 0.0579 0.0632 0.0684 0.0737 0.0789 0.0842 0.0895 0.0947 0.1000 0.1053 0.1105 0.1158 0.1211 0.1263 0.1316 0.1368 0.1421 0.1474 0.1526 0.1579 0.1632 0.1684 0.1737 0.1789 0.1842 0.1895 0.1947 0.2000 0.2053 0.2105 0.2158 0.2211 0.2263 0.2316 0.2368 0.2421 0.2474 0.2526 0.2579 0.2632 0.2684 0.2737 0.2789 0.2842 0.2895 0.2947 0.3000 0.3053 0.3105 0.3158 0.3211 0.3263 0.3316 0.3368 0.3421 0.3474 0.3526 0.3579 0.3632 0.3684 0.3737 0.3789 0.3842 0.3895 0.3947 0.4000 0.4053 0.4105 0.4158 0.4211 0.4263 0.4316 0.4368 0.4421 0.4474 0.4526 0.4579 0.4632 0.4684 0.4737 0.4789 0.4842 0.4895 0.4947 0.5000 0.5053 0.5105 0.5158 0.5211 0.5263 0.5316 0.5368 0.5421 0.5474 0.5526 0.5579 0.5632 0.5684 0.5737 0.5789 0.5842 0.5895 0.5947 0.6000 0.6053 0.6105 0.6158 0.6211 0.6263 0.6316 0.6368 0.6421 0.6474 0.6526 0.6579 0.6632 0.6684 0.6737 0.6789 0.6842 0.6895 0.6947 0.7000 0.7053 0.7105 0.7158 0.7211 0.7263 0.7316 0.7368 0.7421 0.7474 0.7526 0.7579 0.7632 0.7684 0.7737 0.7789 0.7842 0.7895 0.7947 0.8000 0.8053 0.8105 0.8158 0.8211 0.8263 0.8316 0.8368 0.8421 0.8474 0.8526 0.8579 0.8632 0.8684 0.8737 0.8789 0.8842 0.8895 0.8947 0.9000 0.9053 0.9105 0.9158 0.9211 0.9263 0.9316 0.9368 0.9421 0.9474 0.9526 0.9579 0.9632 0.9684 0.9737 0.9789 0.9842 0.9895 0.9947 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.7.7.2 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0769 0.1538 0.2308 0.3077 0.3846 0.4615 0.5385 0.6154 0.6923 0.7692 0.8462 0.9231 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0667 0.1333 0.2000 0.2667 0.3333 0.4000 0.4667 0.5333 0.6000 0.6667 0.7333 0.8000 0.8667 0.9333 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0556 0.1111 0.1667 0.2222 0.2778 0.3333 0.3889 0.4444 0.5000 0.5556 0.6111 0.6667 0.7222 0.7778 0.8333 0.8889 0.9444 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0526 0.1053 0.1579 0.2105 0.2632 0.3158 0.3684 0.4211 0.4737 0.5263 0.5789 0.6316 0.6842 0.7368 0.7895 0.8421 0.8947 0.9474 1.0000 0.0000 0.0667 0.1333 0.2000 0.2667 0.3333 0.4000 0.4667 0.5333 0.6000 0.6667 0.7333 0.8000 0.8667 0.9333 1.0000 0.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.7.7.3 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY b ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0105 0.0105 0.0211 0.0211 0.0316 0.0368 0.0421 0.0474 0.0474 0.0474 0.0632 0.0632 0.0632 0.0789 0.0789 0.0789 0.0947 0.1000 0.1000 0.1105 0.1105 0.1105 0.1263 0.1263 0.1368 0.1421 0.1421 0.1421 0.1579 0.1579 0.1579 0.1737 0.1789 0.1842 0.1895 0.1895 0.2000 0.2000 0.2105 0.2105 0.2211 0.2263 0.2316 0.2316 0.2421 0.2421 0.2526 0.2579 0.2579 0.2579 0.2737 0.2737 0.2737 0.2895 0.2895 0.3000 0.3053 0.3053 0.3053 0.3053 0.3263 0.3263 0.3263 0.3263 0.3474 0.3474 0.3579 0.3579 0.3579 0.3579 0.3789 0.3789 0.3895 0.3895 0.4000 0.4000 0.4000 0.4158 0.4211 0.4211 0.4316 0.4368 0.4368 0.4474 0.4474 0.4579 0.4579 0.4684 0.4684 0.4684 0.4684 0.4895 0.4947 0.5000 0.5053 0.5105 0.5158 0.5211 0.5211 0.5316 0.5316 0.5316 0.5474 0.5526 0.5526 0.5526 0.5526 0.5737 0.5737 0.5737 0.5737 0.5947 0.6000 0.6000 0.6105 0.6105 0.6211 0.6263 0.6316 0.6316 0.6316 0.6474 0.6526 0.6579 0.6632 0.6684 0.6737 0.6737 0.6737 0.6895 0.6895 0.6895 0.7053 0.7053 0.7053 0.7211 0.7211 0.7211 0.7368 0.7421 0.7421 0.7526 0.7526 0.7632 0.7684 0.7684 0.7789 0.7789 0.7789 0.7947 0.8000 0.8053 0.8053 0.8053 0.8053 0.8263 0.8263 0.8263 0.8421 0.8474 0.8474 0.8579 0.8632 0.8632 0.8632 0.8789 0.8789 0.8789 0.8947 0.8947 0.8947 0.8947 0.8947 0.9211 0.9211 0.9211 0.9368 0.9421 0.9421 0.9421 0.9579 0.9579 0.9579 0.9737 0.9737 0.9842 0.9842 0.9947 0.9947} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.7.7.4 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0769 0.1538 0.1538 0.1538 0.3846 0.4615 0.5385 0.6154 0.6923 0.6923 0.8462 0.8462 0.8462 0.0000 0.0000 0.1000 0.1000 0.2000 0.2000 0.3000 0.3000 0.4000 0.4000 0.5000 0.5500 0.5500 0.6500 0.6500 0.6500 0.8000 0.8000 0.8000 0.8000 0.8000 0.0000 0.0000 0.1333 0.1333 0.1333 0.3333 0.3333 0.4667 0.5333 0.6000 0.6667 0.6667 0.8000 0.8000 0.8000 1.0000 0.0000 0.0000 0.1000 0.1000 0.2000 0.2000 0.3000 0.3000 0.3000 0.3000 0.5000 0.5000 0.6000 0.6500 0.7000 0.7000 0.7000 0.8500 0.9000 0.9000 0.9000 0.0000 0.0556 0.1111 0.1667 0.1667 0.1667 0.1667 0.3889 0.3889 0.5000 0.5556 0.6111 0.6111 0.6111 0.7778 0.7778 0.7778 0.7778 1.0000 0.0000 0.0500 0.0500 0.0500 0.2000 0.2500 0.2500 0.3500 0.3500 0.4500 0.4500 0.4500 0.6000 0.6000 0.6000 0.7500 0.7500 0.7500 0.9000 0.9000 0.9000 0.0000 0.0500 0.0500 0.0500 0.2000 0.2000 0.3000 0.3000 0.3000 0.3000 0.5000 0.5000 0.6000 0.6000 0.6000 0.7500 0.8000 0.8500 0.9000 0.9000 0.9000 0.0000 0.0000 0.0000 0.1579 0.2105 0.2105 0.3158 0.3158 0.4211 0.4211 0.4211 0.4211 0.6316 0.6842 0.7368 0.7368 0.8421 0.8421 0.9474 0.9474 0.0000 0.0000 0.0000 0.2000 0.2667 0.2667 0.4000 0.4000 0.4000 0.4000 0.6667 0.7333 0.7333 0.8667 0.9333 0.9333 0.0000 0.0000 0.0000 0.1429 0.1905 0.1905 0.1905 0.3333 0.3333 0.3333 0.4762 0.5238 0.5238 0.5238 0.5238 0.7143 0.7619 0.8095 0.8095 0.8095 0.9524 0.9524} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.7.7.5 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.7.7.6 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER (PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.7.8.1 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0052 0.0105 0.0157 0.0209 0.0262 0.0314 0.0366 0.0419 0.0471 0.0524 0.0576 0.0628 0.0681 0.0733 0.0785 0.0838 0.0890 0.0942 0.0995 0.1047 0.1099 0.1152 0.1204 0.1257 0.1309 0.1361 0.1414 0.1466 0.1518 0.1571 0.1623 0.1675 0.1728 0.1780 0.1832 0.1885 0.1937 0.1990 0.2042 0.2094 0.2147 0.2199 0.2251 0.2304 0.2356 0.2408 0.2461 0.2513 0.2565 0.2618 0.2670 0.2723 0.2775 0.2827 0.2880 0.2932 0.2984 0.3037 0.3089 0.3141 0.3194 0.3246 0.3298 0.3351 0.3403 0.3455 0.3508 0.3560 0.3613 0.3665 0.3717 0.3770 0.3822 0.3874 0.3927 0.3979 0.4031 0.4084 0.4136 0.4188 0.4241 0.4293 0.4346 0.4398 0.4450 0.4503 0.4555 0.4607 0.4660 0.4712 0.4764 0.4817 0.4869 0.4921 0.4974 0.5026 0.5079 0.5131 0.5183 0.5236 0.5288 0.5340 0.5393 0.5445 0.5497 0.5550 0.5602 0.5654 0.5707 0.5759 0.5812 0.5864 0.5916 0.5969 0.6021 0.6073 0.6126 0.6178 0.6230 0.6283 0.6335 0.6387 0.6440 0.6492 0.6545 0.6597 0.6649 0.6702 0.6754 0.6806 0.6859 0.6911 0.6963 0.7016 0.7068 0.7120 0.7173 0.7225 0.7277 0.7330 0.7382 0.7435 0.7487 0.7539 0.7592 0.7644 0.7696 0.7749 0.7801 0.7853 0.7906 0.7958 0.8010 0.8063 0.8115 0.8168 0.8220 0.8272 0.8325 0.8377 0.8429 0.8482 0.8534 0.8586 0.8639 0.8691 0.8743 0.8796 0.8848 0.8901 0.8953 0.9005 0.9058 0.9110 0.9162 0.9215 0.9267 0.9319 0.9372 0.9424 0.9476 0.9529 0.9581 0.9634 0.9686 0.9738 0.9791 0.9843 0.9895 0.9948 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.7.8.2 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0714 0.1429 0.2143 0.2857 0.3571 0.4286 0.5000 0.5714 0.6429 0.7143 0.7857 0.8571 0.9286 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0625 0.1250 0.1875 0.2500 0.3125 0.3750 0.4375 0.5000 0.5625 0.6250 0.6875 0.7500 0.8125 0.8750 0.9375 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0526 0.1053 0.1579 0.2105 0.2632 0.3158 0.3684 0.4211 0.4737 0.5263 0.5789 0.6316 0.6842 0.7368 0.7895 0.8421 0.8947 0.9474 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0625 0.1250 0.1875 0.2500 0.3125 0.3750 0.4375 0.5000 0.5625 0.6250 0.6875 0.7500 0.8125 0.8750 0.9375 1.0000 0.0455 0.0909 0.1364 0.1818 0.2273 0.2727 0.3182 0.3636 0.4091 0.4545 0.5000 0.5455 0.5909 0.6364 0.6818 0.7273 0.7727 0.8182 0.8636 0.9091 0.9545 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.7.8.3 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY b ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0105 0.0105 0.0209 0.0209 0.0314 0.0314 0.0366 0.0419 0.0471 0.0628 0.0628 0.0628 0.0785 0.0785 0.0785 0.0942 0.0942 0.0942 0.0995 0.1099 0.1099 0.1257 0.1257 0.1257 0.1361 0.1361 0.1414 0.1571 0.1571 0.1571 0.1728 0.1728 0.1728 0.1780 0.1832 0.1885 0.1990 0.1990 0.2094 0.2094 0.2199 0.2199 0.2251 0.2304 0.2408 0.2408 0.2513 0.2513 0.2565 0.2723 0.2723 0.2723 0.2880 0.2880 0.2880 0.2984 0.2984 0.3037 0.3246 0.3246 0.3246 0.3246 0.3455 0.3455 0.3455 0.3455 0.3560 0.3560 0.3770 0.3770 0.3770 0.3770 0.3874 0.3874 0.3979 0.3979 0.4136 0.4136 0.4136 0.4188 0.4293 0.4293 0.4346 0.4450 0.4450 0.4555 0.4555 0.4660 0.4660 0.4869 0.4869 0.4869 0.4869 0.4921 0.4974 0.5026 0.5079 0.5131 0.5183 0.5288 0.5288 0.5445 0.5445 0.5445 0.5497 0.5707 0.5707 0.5707 0.5707 0.5916 0.5916 0.5916 0.5916 0.5969 0.6073 0.6073 0.6178 0.6178 0.6230 0.6283 0.6440 0.6440 0.6440 0.6492 0.6545 0.6597 0.6649 0.6702 0.6859 0.6859 0.6859 0.7016 0.7016 0.7016 0.7173 0.7173 0.7173 0.7330 0.7330 0.7330 0.7382 0.7487 0.7487 0.7592 0.7592 0.7644 0.7749 0.7749 0.7906 0.7906 0.7906 0.7958 0.8010 0.8220 0.8220 0.8220 0.8220 0.8377 0.8377 0.8377 0.8429 0.8534 0.8534 0.8586 0.8743 0.8743 0.8743 0.8901 0.8901 0.8901 0.9162 0.9162 0.9162 0.9162 0.9162 0.9319 0.9319 0.9319 0.9372 0.9529 0.9529 0.9529 0.9686 0.9686 0.9686 0.9791 0.9791 0.9895 0.9895 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.7.8.4 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0714 0.1429 0.3571 0.3571 0.3571 0.4286 0.5000 0.5714 0.6429 0.7857 0.7857 1.0000 1.0000 1.0000 0.0952 0.0952 0.1905 0.1905 0.2857 0.2857 0.3810 0.3810 0.4762 0.4762 0.5238 0.6190 0.6190 0.7619 0.7619 0.7619 1.0000 1.0000 1.0000 1.0000 1.0000 0.1250 0.1250 0.3125 0.3125 0.3125 0.4375 0.4375 0.5000 0.5625 0.6250 0.7500 0.7500 0.9375 0.9375 0.9375 1.0000 0.0952 0.0952 0.1905 0.1905 0.2857 0.2857 0.4762 0.4762 0.4762 0.4762 0.5714 0.5714 0.6190 0.6667 0.8095 0.8095 0.8095 0.8571 1.0000 1.0000 1.0000 0.0526 0.1053 0.1579 0.3684 0.3684 0.3684 0.3684 0.4737 0.4737 0.5263 0.5789 0.7368 0.7368 0.7368 0.9474 0.9474 0.9474 0.9474 1.0000 0.0476 0.1905 0.1905 0.1905 0.2381 0.3333 0.3333 0.4286 0.4286 0.5714 0.5714 0.5714 0.7143 0.7143 0.7143 0.8571 0.8571 0.8571 1.0000 1.0000 1.0000 0.0476 0.1905 0.1905 0.1905 0.2857 0.2857 0.4762 0.4762 0.4762 0.4762 0.5714 0.5714 0.7143 0.7143 0.7143 0.7619 0.8095 0.8571 1.0000 1.0000 1.0000 0.1500 0.1500 0.1500 0.2000 0.3000 0.3000 0.4000 0.4000 0.6000 0.6000 0.6000 0.6000 0.6500 0.7000 0.8000 0.8000 0.9000 0.9000 1.0000 1.0000 0.1875 0.1875 0.1875 0.2500 0.3750 0.3750 0.6250 0.6250 0.6250 0.6250 0.6875 0.8125 0.8125 0.8750 1.0000 1.0000 0.1364 0.1364 0.1364 0.1818 0.3182 0.3182 0.3182 0.4545 0.4545 0.4545 0.5000 0.6818 0.6818 0.6818 0.6818 0.7273 0.7727 0.9091 0.9091 0.9091 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.7.8.5 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.7.8.6 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.7.8.1 { + set myres {} + foreach r [db eval {SELECT ntile(100) OVER ( ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 88.0000 89.0000 89.0000 90.0000 90.0000 91.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.7.8.2 { + set myres {} + foreach r [db eval {SELECT ntile(101) OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 22.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.7.8.3 { + set myres {} + foreach r [db eval {SELECT ntile(102) OVER ( ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 88.0000 89.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.7.8.4 { + set myres {} + foreach r [db eval {SELECT ntile(103) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 22.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.7.8.5 { + set myres {} + foreach r [db eval {SELECT ntile(104) OVER ( ORDER BY b%10,a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000 103.0000 104.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.7.8.6 { + set myres {} + foreach r [db eval {SELECT ntile(105) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.7.8.7 { + set myres {} + foreach r [db eval {SELECT ntile(105) OVER ( ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 88.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000 103.0000 104.0000 105.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + +do_execsql_test 1.7.9.1 { + SELECT last_value(a+b) OVER ( ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {99 92 108 72 52 83 55 79 109 65 26 120 119 50 + 70 103 80 124 36 96 59 124 116 110 57 51 52 130 + 103 74 87 48 128 117 105 136 131 71 133 92 109 63 + 84 109 57 146 78 147 113 74 88 150 87 110 65 121 + 106 110 124 85 145 107 161 171 150 156 80 171 120 + 109 158 114 111 136 147 87 173 124 168 173 162 132 + 101 154 167 190 161 110 156 195 198 102 123 177 169 + 140 111 180 119 160 197 152 124 121 134 146 147 132 + 213 141 193 200 210 157 132 136 175 161 218 188 226 + 191 187 208 211 179 138 144 223 196 214 170 212 202 + 163 184 172 173 195 229 240 187 210 200 163 227 228 + 223 191 252 235 225 243 172 187 202 179 179 182 231 + 261 207 263 206 189 209 212 276 181 274 249 239 234 + 213 234 269 196 271 221 210 229 235 250 223 232 229 + 279 224 280 216 207} + +do_execsql_test 1.7.9.2 { + SELECT last_value(a+b) OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {147 106 109 168 134 218 191 212 229 240 213 234 196 + 223 92 109 105 136 146 65 156 132 154 102 123 119 + 160 152 146 147 136 243 261 263 210 79 63 84 78 + 120 87 162 124 141 138 227 228 179 231 234 280 124 + 57 130 92 57 110 114 136 147 167 110 180 193 191 + 252 187 179 206 181 221 279 80 116 117 71 80 171 + 173 177 157 161 179 214 225 182 209 269 271 235 229 + 103 74 131 133 113 74 87 145 190 161 169 140 111 + 132 213 187 208 223 235 189 274 108 65 26 70 51 + 52 128 109 121 124 85 107 150 195 226 172 173 187 + 223 207 212 119 50 124 96 110 87 48 110 173 124 + 197 211 144 196 195 200 202 224 216 207 52 83 103 + 36 88 171 158 156 198 121 210 132 210 239 250 232 + 99 72 55 120 59 109 150 161 111 101 200 175 188 + 170 202 163 184 163 172 276 249 229} + +do_execsql_test 1.7.9.3 { + SELECT last_value(a+b) OVER ( ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {65 102 87 138 57 181 80 111 26 48 144 207 36 121 + 132 101 163 172 196 119 136 63 124 179 110 179 182 + 74 132 189 51 52 85 216 163 134 123 210 78 141 57 + 187 71 87 172 173 50 224 88 59 111 170 109 213 + 223 146 147 84 114 191 206 221 157 161 209 229 74 + 140 107 187 207 212 124 202 52 232 55 184 229 106 + 132 152 120 92 110 179 235 65 70 87 110 195 200 + 175 234 160 234 136 80 113 187 109 121 124 196 156 + 210 239 250 72 109 188 202 191 105 154 79 231 147 + 225 103 161 169 223 96 83 249 212 162 227 228 167 + 180 193 117 177 214 145 208 235 150 110 211 103 158 + 200 168 229 92 156 243 280 279 116 173 269 271 131 + 133 223 128 173 197 210 99 150 161 147 218 240 109 + 136 146 261 263 124 130 252 171 190 213 274 108 195 + 226 119 124 171 198 120 276} + +do_execsql_test 1.7.9.4 { + SELECT last_value(a+b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {196 134 109 213 223 106 234 191 212 168 229 147 218 + 240 65 102 119 136 123 210 146 147 132 152 160 105 + 154 92 156 243 109 136 146 261 263 87 138 63 124 + 179 78 141 84 120 234 79 231 162 227 228 280 57 + 181 110 179 57 187 114 191 206 221 92 110 136 147 + 167 180 193 279 124 130 252 80 182 71 157 161 209 + 229 179 235 80 225 117 177 214 116 173 269 271 171 + 111 74 132 189 87 74 140 113 187 103 161 169 145 + 208 235 131 133 223 190 213 274 26 51 52 85 172 + 173 107 187 207 212 65 70 109 121 124 223 150 128 + 108 195 226 48 144 207 216 50 224 124 202 87 110 + 195 200 196 96 110 211 173 197 119 124 36 121 132 + 88 52 232 156 210 239 250 83 103 158 210 171 198 + 101 163 172 163 59 111 170 55 184 229 175 72 109 + 188 202 249 200 99 150 161 120 276} + +do_execsql_test 1.7.9.5 { + SELECT last_value(a+b) OVER ( ORDER BY b%10,a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {147 106 109 168 134 218 191 212 229 240 213 234 196 + 223 92 109 105 136 146 65 156 132 154 102 123 119 + 160 152 146 147 136 243 261 263 210 79 63 84 78 + 120 87 162 124 141 138 227 228 179 231 234 280 124 + 57 130 92 57 110 114 136 147 167 110 180 193 191 + 252 187 179 206 181 221 279 80 116 117 71 80 171 + 173 177 157 161 179 214 225 182 209 269 271 235 229 + 103 74 131 133 113 74 87 145 190 161 169 140 111 + 132 213 187 208 223 235 189 274 108 65 26 70 51 + 52 128 109 121 124 85 107 150 195 226 172 173 187 + 223 207 212 119 50 124 96 110 87 48 110 173 124 + 197 211 144 196 195 200 202 224 216 207 52 83 103 + 36 88 171 158 156 198 121 210 132 210 239 250 232 + 99 72 55 120 59 109 150 161 111 101 200 175 188 + 170 202 163 184 163 172 276 249 229} + +do_execsql_test 1.7.9.6 { + SELECT last_value(a+b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM t2 +} {108 52 83 79 65 26 70 103 80 36 116 51 52 128 + 117 71 63 84 109 78 147 88 121 106 124 85 107 171 + 150 80 171 120 109 158 87 168 173 162 156 195 198 + 177 124 121 134 141 210 157 132 161 218 226 191 179 + 138 214 212 172 173 229 240 187 210 227 228 223 225 + 179 182 231 207 209 212 239 234 213 234 269 196 271 + 235 250 223 232 229 280 99 92 72 55 109 120 119 + 50 124 96 59 124 110 57 130 103 74 87 48 105 136 + 131 133 92 109 57 146 113 74 150 87 110 65 110 + 145 161 156 114 111 136 147 173 124 132 101 154 167 + 190 161 110 102 123 169 140 111 180 119 160 197 152 + 146 147 132 213 193 200 136 175 188 187 208 211 144 + 223 196 170 202 163 184 195 200 163 191 252 235 243 + 172 187 202 179 261 263 206 189 276 181 274 249 221 + 210 229 279 224 216 207} + +do_execsql_test 1.7.10.1 { + SELECT nth_value(b,b+1) OVER (ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} 39 {} {} {} {} + {} {} {} 91 {} {} {} {} {} 77 54 54 {} {} {} + {} 62 {} {} {} {} {} 23 {} {} {} 97 86 {} 59 + {} 84 {} {} 78 65 {} 16 90 81 {} 59 56 40 54 + {} 85 {} {} {} {} 38 {} 32 47 {} 74 35 47 98 + 96 {} 24 {} {} 29 12 46 36 53 {} 81 27 56 {} + {} 81 93 63 81 91 68 53 99 89 13 12 97 91 29 + 7 7 78 35 84 53 84 58 61 91 99 15 61 98 16 5 + 75 56 2 37 3 96 62 95 43 63 35 78 16 67 43 16 + 16 90 72 98 85 56 90 46 29 29 4 74 74 2 76 41 + 46 77 24 27 97 46 89 1 85 1 74 78 61 85 51 59 + 35 30 56 25 47 28 73 6 73 74 93 43 3 56 47 85 + 61 61 93 9 97 62} + +do_execsql_test 1.7.10.2 { + SELECT nth_value(b,b+1) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} 30 {} {} + {} {} {} {} 91 {} {} {} 91 {} 11 {} {} {} {} + 11 {} {} {} {} {} {} {} {} {} 32 {} {} {} 32 + {} {} 12 {} {} {} {} {} {} {} 43 {} {} {} {} + {} {} {} {} {} {} {} 33 {} 43 {} {} {} {} {} + {} 4 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 15 {} + {} {} {} {} {} 55 {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} 47 {} {} {} {} + {} 27 47 {} {} {} {} {} {} {} {} {} 98 {} 98 + {} {} {} {} {} {} {} {} {} {} {} {} {} 9 {} + {} {} {} {} {} {} 9 9 {} {} {}} + +do_execsql_test 1.7.10.3 { + SELECT nth_value(b,b+1) OVER ( ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {{} 1 2 2 2 2 3 3 4 5 5 5 6 6 6 7 7 7 7 + 7 7 8 8 8 8 8 8 9 9 9 9 9 9 9 11 11 12 + 12 12 12 12 12 13 13 14 14 15 15 15 15 15 15 + 16 16 16 16 16 16 17 17 17 17 19 19 19 19 20 + 20 21 21 21 21 21 21 22 22 22 22 22 23 23 23 + 24 25 25 26 26 27 27 27 27 27 27 29 29 29 30 + 30 30 31 31 31 31 31 32 33 33 33 33 33 33 33 + 33 33 33 33 34 34 34 34 34 34 34 35 35 36 36 + 36 37 37 37 37 37 37 38 38 38 38 38 38 39 39 + 39 39 39 40 41 41 41 41 41 42 43 43 43 43 43 + 44 44 44 44 46 46 46 47 47 47 47 47 47 47 47 + 47 47 47 49 49 49 50 51 51 51 52 52 52 53 53 + 54 54 55 55} + +do_execsql_test 1.7.10.4 { + SELECT nth_value(b,b+1) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + 1 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.7.10.5 { + SELECT nth_value(b,b+1) OVER ( ORDER BY b%10,a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} 30 {} {} + {} {} {} {} 40 {} {} {} 40 41 50 {} {} {} {} + 50 {} {} {} 41 {} 10 91 61 {} 30 {} 10 61 30 + {} {} 10 {} {} {} {} 1 {} 22 80 22 91 93 {} + {} 30 {} {} 91 {} 1 30 91 80 91 {} 43 {} 74 + 21 20 {} {} 74 21 21 2 74 33 81 21 64 64 2 21 + 93 62 14 14 3 91 11 24 55 93 93 62 90 91 55 3 + 24 14 24 91 55 15 72 60 72 61 61 34 43 43 43 + 61 12 4 15 15 51 51 12 23 12 12 25 41 25 13 + 94 12 70 12 84 32 84 94 70 33 12 12 32 41 91 + 70 22 33 84 80 31 75 84 53 75 80 84 80 53 53 + 53 22 44 63 42 95 31 63 44 44 31 90 74 52 63 + 31 63 1 42 90 90 95 3 42} + +do_execsql_test 1.7.10.6 { + SELECT nth_value(b,b+1) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.7.11.1 { + SELECT first_value(b) OVER (ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM t2 +} {89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89} + +do_execsql_test 1.7.11.2 { + SELECT first_value(b) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM t2 +} {90 90 90 90 90 90 90 90 90 90 90 90 90 90 81 + 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 + 81 81 81 81 81 62 62 62 62 62 62 62 62 62 62 + 62 62 62 62 62 62 93 93 93 93 93 93 93 93 93 + 93 93 93 93 93 93 93 93 93 93 93 93 54 54 54 + 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 + 54 65 65 65 65 65 65 65 65 65 65 65 65 65 65 + 65 65 65 65 65 65 65 96 96 96 96 96 96 96 96 + 96 96 96 96 96 96 96 96 96 96 96 96 96 97 97 + 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 + 97 97 97 38 38 38 38 38 38 38 38 38 38 38 38 + 38 38 38 38 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89} + +do_execsql_test 1.7.11.3 { + SELECT first_value(b) OVER ( ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1} + +do_execsql_test 1.7.11.4 { + SELECT first_value(b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {10 10 10 10 10 10 10 10 10 10 10 10 10 10 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 + 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 + 5 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 + 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 + 7 7 7 7 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 + 8 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 + 9 9 9 9} + +do_execsql_test 1.7.11.5 { + SELECT first_value(b) OVER ( ORDER BY b%10,a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90} + +do_execsql_test 1.7.11.6 { + SELECT first_value(b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM t2 +} {96 38 68 62 46 6 46 78 54 8 84 16 16 86 74 24 + 12 32 56 22 90 28 56 40 56 16 36 98 76 4 94 + 42 30 78 2 80 84 72 58 96 98 74 12 8 20 22 88 + 34 8 34 90 96 60 44 2 74 70 26 26 80 90 36 58 + 72 72 66 64 12 14 62 36 34 36 58 52 30 50 84 + 10 84 44 58 30 38 34 82 89 81 59 39 91 99 97 + 27 97 67 29 93 77 23 93 65 35 47 7 61 91 85 + 85 43 59 3 91 55 15 89 25 47 1 43 75 89 81 33 + 29 53 63 87 37 41 9 61 73 95 65 13 1 21 65 35 + 5 73 11 51 87 41 31 31 15 95 73 79 11 49 59 + 55 75 77 7 85 57 29 59 19 39 47 47 9 33 93 75 + 81 9 23 37 13 91 91 33 15 99 3 95 69 33 21 39 + 83 27 17 7} + +do_execsql_test 1.7.12.1 { + SELECT lead(b,b) OVER (ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM t2 +} {96 9 11 89 32 53 91 30 51 56 54 73 22 59 75 + 74 78 8 16 65 15 8 31 87 90 12 32 96 74 76 37 + 85 90 15 35 2 60 36 75 9 51 47 63 51 90 26 42 + 26 8 76 80 90 37 87 56 79 5 87 8 2 39 73 64 + 36 90 72 78 36 73 51 33 20 41 2 26 37 33 8 14 + 33 81 55 1 9 12 39 64 87 72 34 82 21 34 99 62 + 74 41 69 22 75 27 58 8 79 77 26 26 55 {} 29 + 30 7 {} 66 55 2 34 64 {} 33 {} 44 84 {} {} 95 + 85 19 {} 83 {} 91 {} {} 9 50 91 33 34 {} {} + 84 {} 7 9 {} {} {} 44 {} {} {} {} 91 84 {} 95 + 95 52 {} {} {} {} {} 21 {} {} {} 58 {} {} {} + {} {} {} {} 83 {} {} {} {} {} {} {} {} {} {} + {} {} {} {}} + +do_execsql_test 1.7.12.2 { + SELECT lead(b,b) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 81 {} {} {} 21 {} {} {} {} {} {} + {} {} {} {} {} {} 62 {} {} {} 12 {} {} {} 72 + {} {} {} {} {} {} {} {} {} {} 53 {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 34 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} 95 {} {} {} {} {} {} 85 {} + {} {} {} {} {} {} {} {} {} 56 {} 36 {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 57 {} {} {} {} {} 7 {} {} {} {} + {} {} {} {} {} {} 8 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 9 {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.7.12.3 { + SELECT lead(b,b) OVER ( ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {1 2 3 3 5 6 7 8 8 9 9 10 11 12 12 13 13 + 14 15 16 16 17 19 20 21 22 23 24 25 26 27 27 + 28 29 30 31 32 33 33 33 34 34 35 36 36 36 37 + 38 39 39 40 41 42 43 43 44 46 47 47 47 49 50 + 52 53 54 55 56 56 57 58 58 58 59 59 59 60 61 + 62 62 64 65 65 67 69 70 72 72 73 74 74 75 75 + 75 77 78 80 81 81 83 84 84 85 85 85 87 88 89 + 89 89 90 90 91 91 91 93 93 94 95 95 96 97 97 + 98 99 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.7.12.4 { + SELECT lead(b,b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {80 {} {} {} {} {} {} {} {} {} {} {} {} {} 1 + 11 81 81 {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} 12 12 72 82 {} {} {} {} {} {} + {} {} {} {} {} {} 13 23 73 73 {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} 34 84 {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 35 85 85 95 {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} 36 86 96 96 {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 37 47 + 47 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} 58 58 68 {} {} {} {} {} {} {} {} {} + {} {} {} {} 39 49 59 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.7.12.5 { + SELECT lead(b,b) OVER ( ORDER BY b%10,a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {34 72 91 74 21 55 23 94 84 95 2 13 61 22 55 + 55 94 85 75 81 65 73 74 21 72 12 94 33 63 73 + 72 96 36 76 3 25 62 3 73 34 12 46 43 93 72 16 + 86 63 15 65 36 77 24 57 25 53 95 34 95 16 97 + 74 97 67 25 98 44 34 65 54 5 68 96 28 47 95 + 34 39 8 38 6 46 96 28 47 95 56 39 99 97 76 8 + 26 9 79 27 95 16 29 {} 58 58 77 85 56 {} 98 + 29 {} 19 96 {} {} 78 56 98 36 97 {} 89 89 29 + 47 78 {} {} {} 38 68 58 {} 58 38 {} 98 {} {} + {} 39 57 9 {} 79 {} {} 7 {} {} {} 9 29 38 78 + {} {} {} 8 39 {} {} {} {} 59 {} 99 {} {} {} + {} {} {} {} {} {} {} {} {} {} 9 {} {} {} {} + {} {} {} {} {} {} {} {}} + +do_execsql_test 1.7.12.6 { + SELECT lead(b,b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.7.13.1 { + SELECT lag(b,b) OVER (ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} 38 {} {} {} {} + {} {} {} 6 {} {} {} {} {} 81 46 6 {} {} {} {} + 23 {} {} {} {} {} 27 {} {} {} 35 6 {} 12 {} + 23 {} {} 61 84 {} 93 39 47 {} 54 46 96 56 {} + 16 {} {} {} {} 89 {} 16 43 {} 85 56 29 99 53 + {} 59 {} {} 91 59 53 84 99 {} 93 63 47 {} {} + 98 33 67 35 75 1 23 13 55 27 75 98 35 73 63 2 + 21 27 13 24 86 23 84 31 20 94 61 65 75 23 36 + 94 55 90 41 77 96 56 29 40 12 89 63 11 5 73 + 79 1 16 28 31 73 5 39 53 63 41 11 40 2 13 33 + 9 29 90 47 72 9 73 30 44 33 74 93 29 74 42 34 + 63 41 34 96 47 77 1 36 74 72 14 36 26 77 9 72 + 64 8 91 31 52 30} + +do_execsql_test 1.7.13.2 { + SELECT lag(b,b) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} 30 {} {} + {} {} {} {} 91 {} {} {} 61 {} 81 {} {} {} {} + 1 {} {} {} {} {} {} {} {} {} 22 {} {} {} 12 + {} {} 62 {} {} {} {} {} {} {} 23 {} {} {} {} + {} {} {} {} {} {} {} 43 {} 23 {} {} {} {} {} + {} 54 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 75 {} + {} {} {} {} {} 55 {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} 47 {} {} {} {} + {} 27 7 {} {} {} {} {} {} {} {} {} 68 {} 8 {} + {} {} {} {} {} {} {} {} {} {} {} {} 89 {} {} + {} {} {} {} {} 29 9 {} {} {}} + +do_execsql_test 1.7.13.3 { + SELECT lag(b,b) OVER ( ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {{} 1 1 1 1 2 2 2 2 2 2 3 3 3 4 4 5 6 6 + 6 7 7 7 7 7 8 8 8 8 8 8 9 9 9 9 9 9 9 + 9 9 9 10 10 10 10 11 11 11 11 11 12 12 12 12 + 13 13 13 13 13 14 15 15 15 15 16 16 16 16 16 + 17 19 20 20 21 21 21 21 22 22 22 22 23 23 23 + 23 23 24 23 24 24 25 26 26 26 26 26 26 26 26 + 26 26 26 27 27 27 27 28 29 29 29 29 30 30 30 + 30 30 30 31 31 31 31 31 32 32 32 32 32 32 31 + 32 33 33 33 33 33 33 34 34 34 34 34 34 34 34 + 35 35 35 35 35 36 36 36 36 36 36 36 37 37 37 + 38 38 38 38 38 38 39 39 39 39 40 40 41 41 42 + 43 42 43 43 43 43 44 44 44 46 46 46 47 47 47 + 47 47} + +do_execsql_test 1.7.13.4 { + SELECT lag(b,b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + 1 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.7.13.5 { + SELECT lag(b,b) OVER ( ORDER BY b%10,a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} 30 {} {} + {} {} {} {} 91 {} {} {} 61 80 81 {} {} {} {} + 1 {} {} {} 30 {} 21 90 61 {} 22 {} 11 41 12 + {} {} 62 {} {} {} {} 31 {} 50 23 30 21 90 {} + {} 62 {} {} 81 {} 22 43 62 23 32 {} 91 {} 90 + 93 54 {} {} 90 72 12 22 90 81 83 23 80 20 72 + 43 51 33 80 90 2 34 54 1 20 62 12 13 75 44 30 + 93 91 1 21 55 61 61 13 85 3 65 65 91 73 33 93 + 55 84 62 31 11 65 35 85 33 55 15 12 75 22 3 + 73 65 36 85 43 95 43 13 47 44 65 65 96 36 27 + 7 46 34 94 47 36 73 34 35 73 68 24 8 75 85 75 + 66 34 95 36 84 77 46 34 84 47 89 65 36 16 38 + 76 58 57 29 9 44 56 17} + +do_execsql_test 1.7.13.6 { + SELECT lag(b,b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.7.14.1 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM t2 +} {89 89.81 89.81.96 89.81.96.59 89.81.96.59.38 89.81.96.59.38.68 + 89.81.96.59.38.68.39 89.81.96.59.38.68.39.62 89.81.96.59.38.68.39.62.91 + 89.81.96.59.38.68.39.62.91.46 89.81.96.59.38.68.39.62.91.46.6 + 89.81.96.59.38.68.39.62.91.46.6.99 89.81.96.59.38.68.39.62.91.46.6.99.97 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7} + +do_execsql_test 1.7.14.2 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM t2 +} {90 90.40 90.40.30 90.40.30.80 90.40.30.80.20 90.40.30.80.20.90 + 90.40.30.80.20.90.60 90.40.30.80.20.90.60.70 90.40.30.80.20.90.60.70.80 + 90.40.30.80.20.90.60.70.80.90 90.40.30.80.20.90.60.70.80.90.30 + 90.40.30.80.20.90.60.70.80.90.30.50 + 90.40.30.80.20.90.60.70.80.90.30.50.10 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30 81 81.91 81.91.61 + 81.91.61.91 81.91.61.91.91 81.91.61.91.91.1 81.91.61.91.91.1.81 + 81.91.61.91.91.1.81.41 81.91.61.91.91.1.81.41.61 + 81.91.61.91.91.1.81.41.61.1 81.91.61.91.91.1.81.41.61.1.21 + 81.91.61.91.91.1.81.41.61.1.21.11 81.91.61.91.91.1.81.41.61.1.21.11.51 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 62 62.12 + 62.12.32 62.12.32.22 62.12.32.22.42 62.12.32.22.42.2 + 62.12.32.22.42.2.72 62.12.32.22.42.2.72.12 62.12.32.22.42.2.72.12.22 + 62.12.32.22.42.2.72.12.22.2 62.12.32.22.42.2.72.12.22.2.72 + 62.12.32.22.42.2.72.12.22.2.72.72 62.12.32.22.42.2.72.12.22.2.72.72.12 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 93 93.23 93.23.93 + 93.23.93.43 93.23.93.43.3 93.23.93.43.3.43 93.23.93.43.3.43.33 + 93.23.93.43.3.43.33.53 93.23.93.43.3.43.33.53.63 + 93.23.93.43.3.43.33.53.63.73 93.23.93.43.3.43.33.53.63.73.13 + 93.23.93.43.3.43.33.53.63.73.13.73 93.23.93.43.3.43.33.53.63.73.13.73.73 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 54 54.84 + 54.84.74 54.84.74.24 54.84.74.24.4 54.84.74.24.4.94 + 54.84.74.24.4.94.84 54.84.74.24.4.94.84.74 54.84.74.24.4.94.84.74.34 + 54.84.74.24.4.94.84.74.34.34 54.84.74.24.4.94.84.74.34.34.44 + 54.84.74.24.4.94.84.74.34.34.44.74 54.84.74.24.4.94.84.74.34.34.44.74.64 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 65 65.35 + 65.35.85 65.35.85.85 65.35.85.85.55 65.35.85.85.55.15 + 65.35.85.85.55.15.25 65.35.85.85.55.15.25.75 65.35.85.85.55.15.25.75.95 + 65.35.85.85.55.15.25.75.95.65 65.35.85.85.55.15.25.75.95.65.65 + 65.35.85.85.55.15.25.75.95.65.65.35 65.35.85.85.55.15.25.75.95.65.65.35.5 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 96 96.46 + 96.46.6 96.46.6.46 96.46.6.46.16 96.46.6.46.16.16 + 96.46.6.46.16.16.86 96.46.6.46.16.16.86.56 96.46.6.46.16.16.86.56.56 + 96.46.6.46.16.16.86.56.56.56 96.46.6.46.16.16.86.56.56.56.16 + 96.46.6.46.16.16.86.56.56.56.16.36 96.46.6.46.16.16.86.56.56.56.16.36.76 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 97 97.27 + 97.27.97 97.27.97.67 97.27.97.67.77 97.27.97.67.77.47 + 97.27.97.67.77.47.7 97.27.97.67.77.47.7.47 97.27.97.67.77.47.7.47.87 + 97.27.97.67.77.47.7.47.87.37 97.27.97.67.77.47.7.47.87.37.87 + 97.27.97.67.77.47.7.47.87.37.87.77 97.27.97.67.77.47.7.47.87.37.87.77.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 38 38.68 + 38.68.78 38.68.78.8 38.68.78.8.28 38.68.78.8.28.98 + 38.68.78.8.28.98.78 38.68.78.8.28.98.78.58 38.68.78.8.28.98.78.58.98 + 38.68.78.8.28.98.78.58.98.8 38.68.78.8.28.98.78.58.98.8.88 + 38.68.78.8.28.98.78.58.98.8.88.8 38.68.78.8.28.98.78.58.98.8.88.8.58 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 89 89.59 89.59.39 + 89.59.39.99 89.59.39.99.29 89.59.39.99.29.59 89.59.39.99.29.59.89 + 89.59.39.99.29.59.89.89 89.59.39.99.29.59.89.89.29 + 89.59.39.99.29.59.89.89.29.9 89.59.39.99.29.59.89.89.29.9.79 + 89.59.39.99.29.59.89.89.29.9.79.49 89.59.39.99.29.59.89.89.29.9.79.49.59 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39} + +do_execsql_test 1.7.14.3 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {1 1.1 1.1.2 1.1.2.2 1.1.2.2.3 1.1.2.2.3.3 1.1.2.2.3.3.4 + 1.1.2.2.3.3.4.5 1.1.2.2.3.3.4.5.6 1.1.2.2.3.3.4.5.6.7 + 1.1.2.2.3.3.4.5.6.7.7 1.1.2.2.3.3.4.5.6.7.7.7 1.1.2.2.3.3.4.5.6.7.7.7.8 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99} + +do_execsql_test 1.7.14.4 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {10 10.20 10.20.30 10.20.30.30 10.20.30.30.30 10.20.30.30.30.40 + 10.20.30.30.30.40.50 10.20.30.30.30.40.50.60 10.20.30.30.30.40.50.60.70 + 10.20.30.30.30.40.50.60.70.80 10.20.30.30.30.40.50.60.70.80.80 + 10.20.30.30.30.40.50.60.70.80.80.90 + 10.20.30.30.30.40.50.60.70.80.80.90.90 + 10.20.30.30.30.40.50.60.70.80.80.90.90.90 1 1.1 1.1.11 1.1.11.11 + 1.1.11.11.21 1.1.11.11.21.21 1.1.11.11.21.21.31 1.1.11.11.21.21.31.31 + 1.1.11.11.21.21.31.31.41 1.1.11.11.21.21.31.31.41.41 + 1.1.11.11.21.21.31.31.41.41.51 1.1.11.11.21.21.31.31.41.41.51.61 + 1.1.11.11.21.21.31.31.41.41.51.61.61 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 2 2.2 + 2.2.12 2.2.12.12 2.2.12.12.12 2.2.12.12.12.22 2.2.12.12.12.22.22 + 2.2.12.12.12.22.22.32 2.2.12.12.12.22.22.32.42 + 2.2.12.12.12.22.22.32.42.52 2.2.12.12.12.22.22.32.42.52.62 + 2.2.12.12.12.22.22.32.42.52.62.62 2.2.12.12.12.22.22.32.42.52.62.62.72 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 3 3.3 3.3.13 + 3.3.13.13 3.3.13.13.23 3.3.13.13.23.23 3.3.13.13.23.23.33 + 3.3.13.13.23.23.33.33 3.3.13.13.23.23.33.33.33 + 3.3.13.13.23.23.33.33.33.33 3.3.13.13.23.23.33.33.33.33.43 + 3.3.13.13.23.23.33.33.33.33.43.43 3.3.13.13.23.23.33.33.33.33.43.43.53 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 4 4.14 + 4.14.24 4.14.24.34 4.14.24.34.34 4.14.24.34.34.34 + 4.14.24.34.34.34.34 4.14.24.34.34.34.34.44 4.14.24.34.34.34.34.44.44 + 4.14.24.34.34.34.34.44.44.54 4.14.24.34.34.34.34.44.44.54.64 + 4.14.24.34.34.34.34.44.44.54.64.74 4.14.24.34.34.34.34.44.44.54.64.74.74 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 5 5.15 + 5.15.15 5.15.15.15 5.15.15.15.25 5.15.15.15.25.35 + 5.15.15.15.25.35.35 5.15.15.15.25.35.35.55 5.15.15.15.25.35.35.55.55 + 5.15.15.15.25.35.35.55.55.65 5.15.15.15.25.35.35.55.55.65.65 + 5.15.15.15.25.35.35.55.55.65.65.65 5.15.15.15.25.35.35.55.55.65.65.65.75 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 6 6.16 + 6.16.16 6.16.16.16 6.16.16.16.26 6.16.16.16.26.26 + 6.16.16.16.26.26.36 6.16.16.16.26.26.36.36 6.16.16.16.26.26.36.36.36 + 6.16.16.16.26.26.36.36.36.36 6.16.16.16.26.26.36.36.36.36.46 + 6.16.16.16.26.26.36.36.36.36.46.46 6.16.16.16.26.26.36.36.36.36.46.46.56 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 7 7.7 + 7.7.7 7.7.7.17 7.7.7.17.27 7.7.7.17.27.27 7.7.7.17.27.27.37 + 7.7.7.17.27.27.37.37 7.7.7.17.27.27.37.37.47 7.7.7.17.27.27.37.37.47.47 + 7.7.7.17.27.27.37.37.47.47.47 7.7.7.17.27.27.37.37.47.47.47.47 + 7.7.7.17.27.27.37.37.47.47.47.47.57 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 8 8.8 8.8.8 + 8.8.8.28 8.8.8.28.38 8.8.8.28.38.38 8.8.8.28.38.38.58 + 8.8.8.28.38.38.58.58 8.8.8.28.38.38.58.58.58 8.8.8.28.38.38.58.58.58.58 + 8.8.8.28.38.38.58.58.58.58.68 8.8.8.28.38.38.58.58.58.58.68.78 + 8.8.8.28.38.38.58.58.58.58.68.78.78 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 9 9.9 9.9.9 9.9.9.19 + 9.9.9.19.29 9.9.9.19.29.29 9.9.9.19.29.29.29 9.9.9.19.29.29.29.39 + 9.9.9.19.29.29.29.39.39 9.9.9.19.29.29.29.39.39.39 + 9.9.9.19.29.29.29.39.39.39.49 9.9.9.19.29.29.29.39.39.39.49.59 + 9.9.9.19.29.29.29.39.39.39.49.59.59 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99} + +do_execsql_test 1.7.14.5 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( ORDER BY b%10,a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t2 +} {90 90.40 90.40.30 90.40.30.80 90.40.30.80.20 90.40.30.80.20.90 + 90.40.30.80.20.90.60 90.40.30.80.20.90.60.70 90.40.30.80.20.90.60.70.80 + 90.40.30.80.20.90.60.70.80.90 90.40.30.80.20.90.60.70.80.90.30 + 90.40.30.80.20.90.60.70.80.90.30.50 + 90.40.30.80.20.90.60.70.80.90.30.50.10 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39} + +do_execsql_test 1.7.14.6 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM t2 +} {96 38 68 62 46 6 46 78 54 8 84 16 16 86 74 24 + 12 32 56 22 90 28 56 40 56 16 36 98 76 4 94 + 42 30 78 2 80 84 72 58 96 98 74 12 8 20 22 88 + 34 8 34 90 96 60 44 2 74 70 26 26 80 90 36 58 + 72 72 66 64 12 14 62 36 34 36 58 52 30 50 84 + 10 84 44 58 30 38 34 82 89 81 59 39 91 99 97 + 27 97 67 29 93 77 23 93 65 35 47 7 61 91 85 + 85 43 59 3 91 55 15 89 25 47 1 43 75 89 81 33 + 29 53 63 87 37 41 9 61 73 95 65 13 1 21 65 35 + 5 73 11 51 87 41 31 31 15 95 73 79 11 49 59 + 55 75 77 7 85 57 29 59 19 39 47 47 9 33 93 75 + 81 9 23 37 13 91 91 33 15 99 3 95 69 33 21 39 + 83 27 17 7} + +do_execsql_test 1.7.14.7 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (win1 ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a) + ORDER BY 1 +} {1 1 10 11 11 12 12 12 13 13 14 15 15 15 16 16 + 16 17 19 2 2 20 21 21 22 22 23 23 24 25 26 26 + 27 27 28 29 29 29 3 3 30 30 30 31 31 32 33 33 + 33 33 34 34 34 34 35 35 36 36 36 36 37 37 38 + 38 39 39 39 4 40 41 41 42 43 43 44 44 46 46 + 47 47 47 47 49 5 50 51 52 53 54 55 55 56 56 + 56 57 58 58 58 58 59 59 59 59 6 60 61 61 62 + 62 63 64 65 65 65 66 67 68 69 7 7 7 70 72 72 + 72 73 73 73 74 74 74 75 75 75 76 77 77 78 78 + 79 8 8 8 80 80 81 81 81 82 83 84 84 84 84 85 + 85 85 86 87 87 88 89 89 89 9 9 9 90 90 90 91 + 91 91 91 91 93 93 93 94 95 95 95 96 96 96 97 + 97 98 98 99 99} + +do_execsql_test 1.7.14.8 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (win1 ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a ORDER BY b%10) + ORDER BY 1 +} {1 1 10 11 11 12 12 12 13 13 14 15 15 15 16 16 + 16 17 19 2 2 20 21 21 22 22 23 23 24 25 26 26 + 27 27 28 29 29 29 3 3 30 30 30 31 31 32 33 33 + 33 33 34 34 34 34 35 35 36 36 36 36 37 37 38 + 38 39 39 39 4 40 41 41 42 43 43 44 44 46 46 + 47 47 47 47 49 5 50 51 52 53 54 55 55 56 56 + 56 57 58 58 58 58 59 59 59 59 6 60 61 61 62 + 62 63 64 65 65 65 66 67 68 69 7 7 7 70 72 72 + 72 73 73 73 74 74 74 75 75 75 76 77 77 78 78 + 79 8 8 8 80 80 81 81 81 82 83 84 84 84 84 85 + 85 85 86 87 87 88 89 89 89 9 9 9 90 90 90 91 + 91 91 91 91 93 93 93 94 95 95 95 96 96 96 97 + 97 98 98 99 99} + +do_execsql_test 1.7.14.9 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER win2 + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a ORDER BY b%10), + win2 AS (win1 ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + ORDER BY 1 +} {1 1 10 11 11 12 12 12 13 13 14 15 15 15 16 16 + 16 17 19 2 2 20 21 21 22 22 23 23 24 25 26 26 + 27 27 28 29 29 29 3 3 30 30 30 31 31 32 33 33 + 33 33 34 34 34 34 35 35 36 36 36 36 37 37 38 + 38 39 39 39 4 40 41 41 42 43 43 44 44 46 46 + 47 47 47 47 49 5 50 51 52 53 54 55 55 56 56 + 56 57 58 58 58 58 59 59 59 59 6 60 61 61 62 + 62 63 64 65 65 65 66 67 68 69 7 7 7 70 72 72 + 72 73 73 73 74 74 74 75 75 75 76 77 77 78 78 + 79 8 8 8 80 80 81 81 81 82 83 84 84 84 84 85 + 85 85 86 87 87 88 89 89 89 9 9 9 90 90 90 91 + 91 91 91 91 93 93 93 94 95 95 95 96 96 96 97 + 97 98 98 99 99} + +do_execsql_test 1.7.15.1 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE a%2=0) OVER win FROM t2 + WINDOW win AS (ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) +} {1 89 2 89 3 89.96 4 89.96 5 89.96.38 6 89.96.38 7 89.96.38.39 + 8 89.96.38.39 9 89.96.38.39.91 10 89.96.38.39.91 11 89.96.38.39.91.6 + 12 89.96.38.39.91.6 13 89.96.38.39.91.6.97 14 89.96.38.39.91.6.97 + 15 89.96.38.39.91.6.97.46 16 89.96.38.39.91.6.97.46 + 17 89.96.38.39.91.6.97.46.54 18 89.96.38.39.91.6.97.46.54 + 19 89.96.38.39.91.6.97.46.54.8 20 89.96.38.39.91.6.97.46.54.8 + 21 89.96.38.39.91.6.97.46.54.8.29 22 89.96.38.39.91.6.97.46.54.8.29 + 23 89.96.38.39.91.6.97.46.54.8.29.84 24 89.96.38.39.91.6.97.46.54.8.29.84 + 25 89.96.38.39.91.6.97.46.54.8.29.84.23 + 26 89.96.38.39.91.6.97.46.54.8.29.84.23 + 27 89.96.38.39.91.6.97.46.54.8.29.84.23.16 + 28 89.96.38.39.91.6.97.46.54.8.29.84.23.16 + 29 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65 + 30 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65 + 31 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47 + 32 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47 + 33 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86 + 34 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86 + 35 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61 + 36 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61 + 37 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85 + 38 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85 + 39 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85 + 40 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85 + 41 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59 + 42 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59 + 43 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32 + 44 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32 + 45 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3 + 46 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3 + 47 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22 + 48 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22 + 49 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55 + 50 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55 + 51 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28 + 52 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28 + 53 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25 + 54 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25 + 55 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1 + 56 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1 + 57 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40 + 58 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40 + 59 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56 + 60 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56 + 61 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75 + 62 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75 + 63 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89 + 64 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89 + 65 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76 + 66 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76 + 67 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4 + 68 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4 + 69 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42 + 70 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42 + 71 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78 + 72 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78 + 73 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29 + 74 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29 + 75 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63 + 76 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63 + 77 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87 + 78 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87 + 79 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80 + 80 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80 + 81 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72 + 82 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72 + 83 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9 + 84 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9 + 85 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73 + 86 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73 + 87 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65 + 88 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65 + 89 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58 + 90 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58 + 91 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98 + 92 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98 + 93 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21 + 94 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21 + 95 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65 + 96 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65 + 97 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5 + 98 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5 + 99 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11 + 100 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11 + 101 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87 + 102 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87 + 103 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12 + 104 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12 + 105 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20 + 106 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20 + 107 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31 + 108 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31 + 109 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95 + 110 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95 + 111 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73 + 112 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73 + 113 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88 + 114 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88 + 115 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8 + 116 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8 + 117 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49 + 118 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49 + 119 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90 + 120 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90 + 121 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96 + 122 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96 + 123 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55 + 124 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55 + 125 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77 + 126 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77 + 127 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2 + 128 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2 + 129 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85 + 130 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85 + 131 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74 + 132 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74 + 133 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70 + 134 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70 + 135 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19 + 136 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19 + 137 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26 + 138 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26 + 139 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47 + 140 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47 + 141 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90 + 142 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90 + 143 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58 + 144 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58 + 145 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9 + 146 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9 + 147 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72 + 148 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72 + 149 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33 + 150 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33 + 151 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75 + 152 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75 + 153 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81 + 154 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81 + 155 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23 + 156 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23 + 157 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13 + 158 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13 + 159 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14 + 160 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14 + 161 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91 + 162 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91 + 163 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91 + 164 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91 + 165 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15 + 166 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15 + 167 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36 + 168 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36 + 169 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3 + 170 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3 + 171 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69 + 172 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69 + 173 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52 + 174 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52 + 175 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50 + 176 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50 + 177 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10 + 178 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10 + 179 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33 + 180 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33 + 181 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39 + 182 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39 + 183 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58 + 184 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58 + 185 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38 + 186 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38 + 187 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83 + 188 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83 + 189 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82 + 190 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7} + +do_execsql_test 1.7.15.2 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE 0=1) OVER win FROM t2 + WINDOW win AS (ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) +} {1 {} 2 {} 3 {} 4 {} 5 {} 6 {} 7 {} 8 {} 9 {} 10 {} + 11 {} 12 {} 13 {} 14 {} 15 {} 16 {} 17 {} 18 {} 19 {} + 20 {} 21 {} 22 {} 23 {} 24 {} 25 {} 26 {} 27 {} 28 {} + 29 {} 30 {} 31 {} 32 {} 33 {} 34 {} 35 {} 36 {} 37 {} + 38 {} 39 {} 40 {} 41 {} 42 {} 43 {} 44 {} 45 {} 46 {} + 47 {} 48 {} 49 {} 50 {} 51 {} 52 {} 53 {} 54 {} 55 {} + 56 {} 57 {} 58 {} 59 {} 60 {} 61 {} 62 {} 63 {} 64 {} + 65 {} 66 {} 67 {} 68 {} 69 {} 70 {} 71 {} 72 {} 73 {} + 74 {} 75 {} 76 {} 77 {} 78 {} 79 {} 80 {} 81 {} 82 {} + 83 {} 84 {} 85 {} 86 {} 87 {} 88 {} 89 {} 90 {} 91 {} + 92 {} 93 {} 94 {} 95 {} 96 {} 97 {} 98 {} 99 {} 100 {} + 101 {} 102 {} 103 {} 104 {} 105 {} 106 {} 107 {} 108 {} + 109 {} 110 {} 111 {} 112 {} 113 {} 114 {} 115 {} 116 {} + 117 {} 118 {} 119 {} 120 {} 121 {} 122 {} 123 {} 124 {} + 125 {} 126 {} 127 {} 128 {} 129 {} 130 {} 131 {} 132 {} + 133 {} 134 {} 135 {} 136 {} 137 {} 138 {} 139 {} 140 {} + 141 {} 142 {} 143 {} 144 {} 145 {} 146 {} 147 {} 148 {} + 149 {} 150 {} 151 {} 152 {} 153 {} 154 {} 155 {} 156 {} + 157 {} 158 {} 159 {} 160 {} 161 {} 162 {} 163 {} 164 {} + 165 {} 166 {} 167 {} 168 {} 169 {} 170 {} 171 {} 172 {} + 173 {} 174 {} 175 {} 176 {} 177 {} 178 {} 179 {} 180 {} + 181 {} 182 {} 183 {} 184 {} 185 {} 186 {} 187 {} 188 {} + 189 {} 190 {} 191 {}} + +do_execsql_test 1.7.15.3 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE 1=0) OVER win FROM t2 + WINDOW win AS (PARTITION BY (a%10) ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) +} {1 {} 2 {} 3 {} 4 {} 5 {} 6 {} 7 {} 8 {} 9 {} 10 {} + 11 {} 12 {} 13 {} 14 {} 15 {} 16 {} 17 {} 18 {} 19 {} + 20 {} 1 {} 2 {} 3 {} 4 {} 5 {} 6 {} 7 {} 8 {} 9 {} + 10 {} 11 {} 12 {} 13 {} 14 {} 15 {} 16 {} 17 {} 18 {} + 19 {} 1 {} 2 {} 3 {} 4 {} 5 {} 6 {} 7 {} 8 {} 9 {} + 10 {} 11 {} 12 {} 13 {} 14 {} 15 {} 16 {} 17 {} 18 {} + 19 {} 1 {} 2 {} 3 {} 4 {} 5 {} 6 {} 7 {} 8 {} 9 {} + 10 {} 11 {} 12 {} 13 {} 14 {} 15 {} 16 {} 17 {} 18 {} + 19 {} 1 {} 2 {} 3 {} 4 {} 5 {} 6 {} 7 {} 8 {} 9 {} + 10 {} 11 {} 12 {} 13 {} 14 {} 15 {} 16 {} 17 {} 18 {} + 19 {} 1 {} 2 {} 3 {} 4 {} 5 {} 6 {} 7 {} 8 {} 9 {} + 10 {} 11 {} 12 {} 13 {} 14 {} 15 {} 16 {} 17 {} 18 {} + 19 {} 1 {} 2 {} 3 {} 4 {} 5 {} 6 {} 7 {} 8 {} 9 {} + 10 {} 11 {} 12 {} 13 {} 14 {} 15 {} 16 {} 17 {} 18 {} + 19 {} 1 {} 2 {} 3 {} 4 {} 5 {} 6 {} 7 {} 8 {} 9 {} + 10 {} 11 {} 12 {} 13 {} 14 {} 15 {} 16 {} 17 {} 18 {} + 19 {} 1 {} 2 {} 3 {} 4 {} 5 {} 6 {} 7 {} 8 {} 9 {} + 10 {} 11 {} 12 {} 13 {} 14 {} 15 {} 16 {} 17 {} 18 {} + 19 {} 1 {} 2 {} 3 {} 4 {} 5 {} 6 {} 7 {} 8 {} 9 {} + 10 {} 11 {} 12 {} 13 {} 14 {} 15 {} 16 {} 17 {} 18 {} + 19 {}} + +do_execsql_test 1.7.15.4 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE a%2=0) OVER win FROM t2 + WINDOW win AS (PARTITION BY (a%10) ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) +} {1 89 2 89.6 3 89.6.29 4 89.6.29.47 5 89.6.29.47.59 + 6 89.6.29.47.59.28 7 89.6.29.47.59.28.75 8 89.6.29.47.59.28.75.78 + 9 89.6.29.47.59.28.75.78.72 10 89.6.29.47.59.28.75.78.72.98 + 11 89.6.29.47.59.28.75.78.72.98.87 12 89.6.29.47.59.28.75.78.72.98.87.73 + 13 89.6.29.47.59.28.75.78.72.98.87.73.96 + 14 89.6.29.47.59.28.75.78.72.98.87.73.96.74 + 15 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90 + 16 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75 + 17 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91 + 18 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69 + 19 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39 + 20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 1 {} 2 {} + 3 {} 4 {} 5 {} 6 {} 7 {} 8 {} 9 {} 10 {} 11 {} 12 {} + 13 {} 14 {} 15 {} 16 {} 17 {} 18 {} 19 {} 1 96 2 96.97 + 3 96.97.84 4 96.97.84.86 5 96.97.84.86.32 6 96.97.84.86.32.25 + 7 96.97.84.86.32.25.89 8 96.97.84.86.32.25.89.29 + 9 96.97.84.86.32.25.89.29.9 10 96.97.84.86.32.25.89.29.9.21 + 11 96.97.84.86.32.25.89.29.9.21.12 12 96.97.84.86.32.25.89.29.9.21.12.88 + 13 96.97.84.86.32.25.89.29.9.21.12.88.55 + 14 96.97.84.86.32.25.89.29.9.21.12.88.55.70 + 15 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58 + 16 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81 + 17 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91 + 18 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52 + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 1 {} 2 {} + 3 {} 4 {} 5 {} 6 {} 7 {} 8 {} 9 {} 10 {} 11 {} 12 {} + 13 {} 14 {} 15 {} 16 {} 17 {} 18 {} 19 {} 1 38 2 38.46 + 3 38.46.23 4 38.46.23.61 5 38.46.23.61.3 6 38.46.23.61.3.1 + 7 38.46.23.61.3.1.76 8 38.46.23.61.3.1.76.63 9 38.46.23.61.3.1.76.63.73 + 10 38.46.23.61.3.1.76.63.73.65 11 38.46.23.61.3.1.76.63.73.65.20 + 12 38.46.23.61.3.1.76.63.73.65.20.8 + 13 38.46.23.61.3.1.76.63.73.65.20.8.77 + 14 38.46.23.61.3.1.76.63.73.65.20.8.77.19 + 15 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9 + 16 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23 + 17 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15 + 18 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50 + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 1 {} 2 {} + 3 {} 4 {} 5 {} 6 {} 7 {} 8 {} 9 {} 10 {} 11 {} 12 {} + 13 {} 14 {} 15 {} 16 {} 17 {} 18 {} 19 {} 1 39 2 39.54 + 3 39.54.16 4 39.54.16.85 5 39.54.16.85.22 6 39.54.16.85.22.40 + 7 39.54.16.85.22.40.4 8 39.54.16.85.22.40.4.87 + 9 39.54.16.85.22.40.4.87.65 10 39.54.16.85.22.40.4.87.65.5 + 11 39.54.16.85.22.40.4.87.65.5.31 12 39.54.16.85.22.40.4.87.65.5.31.49 + 13 39.54.16.85.22.40.4.87.65.5.31.49.2 + 14 39.54.16.85.22.40.4.87.65.5.31.49.2.26 + 15 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72 + 16 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13 + 17 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36 + 18 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10 + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 1 {} 2 {} + 3 {} 4 {} 5 {} 6 {} 7 {} 8 {} 9 {} 10 {} 11 {} 12 {} + 13 {} 14 {} 15 {} 16 {} 17 {} 18 {} 19 {} 1 91 2 91.8 + 3 91.8.65 4 91.8.65.85 5 91.8.65.85.55 6 91.8.65.85.55.56 + 7 91.8.65.85.55.56.42 8 91.8.65.85.55.56.42.80 + 9 91.8.65.85.55.56.42.80.58 10 91.8.65.85.55.56.42.80.58.11 + 11 91.8.65.85.55.56.42.80.58.11.95 12 91.8.65.85.55.56.42.80.58.11.95.90 + 13 91.8.65.85.55.56.42.80.58.11.95.90.85 + 14 91.8.65.85.55.56.42.80.58.11.95.90.85.47 + 15 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33 + 16 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14 + 17 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3 + 18 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33 + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 1 {} 2 {} + 3 {} 4 {} 5 {} 6 {} 7 {} 8 {} 9 {} 10 {} 11 {} 12 {} + 13 {} 14 {} 15 {} 16 {} 17 {} 18 {} 19 {}} + +do_execsql_test 1.8.2.1 { + SELECT max(b) OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2 +} {89 89 96 96 96 96 96 68 91 91 91 99 99 99 99 + 99 97 97 97 97 97 97 93 93 93 93 84 93 93 93 + 93 93 86 86 86 91 91 91 91 91 85 85 85 59 59 + 91 91 91 91 91 90 90 89 89 89 89 56 56 56 56 + 75 75 89 98 98 98 98 98 94 94 94 94 78 78 78 + 63 87 87 87 87 87 84 84 84 73 95 95 95 95 96 + 98 98 98 98 98 74 74 74 73 73 87 87 87 87 87 + 41 31 31 95 95 95 95 95 88 88 88 88 49 90 90 + 96 96 96 96 96 77 77 77 85 85 85 85 85 74 74 + 70 70 59 47 80 90 90 90 90 90 72 72 72 72 93 + 93 93 93 93 81 81 81 37 37 62 91 91 91 91 91 + 91 91 99 99 99 99 99 95 95 69 84 84 84 84 84 + 84 84 58 58 58 58 83 83 83 83 83} + +do_execsql_test 1.8.2.2 { + SELECT min(b) OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2 +} {89 81 81 59 38 38 38 38 38 39 6 6 6 6 6 27 + 27 27 8 8 8 8 8 29 23 16 16 16 16 16 16 7 7 + 7 7 7 61 24 24 24 24 12 12 12 3 3 3 3 3 15 + 15 15 15 15 1 1 1 1 1 16 16 16 16 16 36 36 + 4 4 4 4 4 30 29 29 29 2 2 2 2 2 37 37 9 9 + 9 9 9 13 13 13 13 1 1 1 1 1 5 5 5 5 5 11 + 11 8 8 8 8 8 15 15 15 15 22 22 8 8 8 8 8 + 11 34 34 55 55 55 44 2 2 2 2 2 7 29 29 19 + 19 19 19 19 26 26 26 36 36 9 9 9 9 9 33 33 + 33 33 9 9 9 9 9 12 12 12 12 14 33 15 15 15 + 15 3 3 3 3 3 30 30 30 10 10 10 10 10 21 21 + 21 30 30 30 27 27 17 7} + +do_execsql_test 1.8.3.1 { + SELECT row_number() OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.8.3.2 { + SELECT row_number() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.8.3.3 { + SELECT row_number() OVER ( ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.8.4.1 { + SELECT dense_rank() OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.8.4.2 { + SELECT dense_rank() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.8.4.3 { + SELECT dense_rank() OVER ( ORDER BY b ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2 +} {1 1 2 2 3 3 4 5 6 7 7 7 8 8 8 9 9 9 10 + 11 11 12 12 12 13 13 14 15 15 15 16 16 16 17 + 18 19 20 20 21 21 22 22 23 24 25 25 26 26 27 + 28 28 28 29 29 29 30 30 31 32 32 32 32 33 33 + 33 33 34 34 35 35 35 35 36 36 37 37 38 38 38 + 39 40 40 41 42 42 43 43 44 44 45 45 45 45 46 + 47 48 49 50 51 52 52 53 53 53 54 55 55 55 55 + 56 56 56 56 57 58 58 59 59 60 61 62 62 62 63 + 64 65 66 67 68 68 68 69 69 69 70 70 70 71 71 + 71 72 73 73 74 74 75 76 76 77 77 77 78 79 80 + 80 80 80 81 81 81 82 83 83 84 85 85 85 86 86 + 86 87 87 87 87 87 88 88 88 89 90 90 90 91 91 + 91 92 92 93 93 94 94} + +do_execsql_test 1.8.4.4 { + SELECT dense_rank() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2 +} {1 2 3 3 3 4 5 6 7 8 8 9 9 9 1 1 2 2 3 + 3 4 4 5 5 6 7 7 8 8 8 9 9 9 9 9 1 1 2 + 2 2 3 3 4 5 6 7 7 8 8 8 9 1 1 2 2 3 3 + 4 4 4 4 5 5 6 7 8 8 8 9 10 10 10 1 2 3 + 4 4 4 4 5 5 6 7 8 8 8 9 9 9 9 10 1 2 2 + 2 3 4 4 5 5 6 6 6 7 7 7 8 8 8 9 9 9 1 + 2 2 2 3 3 4 4 4 4 5 5 6 6 6 7 8 9 10 10 + 10 1 1 1 2 3 3 4 4 5 5 5 5 6 7 8 8 9 9 + 10 10 1 1 1 2 3 3 4 4 4 4 5 6 6 7 8 8 1 + 1 1 2 3 3 3 4 4 4 5 6 6 6 6 7 8 9 9 9 + 10 10} + +do_execsql_test 1.8.4.5 { + SELECT dense_rank() OVER ( ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 + 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 + 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 + 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 + 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 + 8 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 10 10 + 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 + 10 10 10 10 10} + +do_execsql_test 1.8.4.6 { + SELECT dense_rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 + 5 5 5 5 5 5 5 5 5 5 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 + 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 + 5} + +do_execsql_test 1.8.5.1 { + SELECT rank() OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.8.5.2 { + SELECT rank() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.8.5.3 { + SELECT rank() OVER ( ORDER BY b ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2 +} {1 1 3 3 5 5 7 8 9 10 10 10 13 13 13 16 16 + 16 19 20 20 22 22 22 25 25 27 28 28 28 31 31 + 31 34 35 36 37 37 39 39 41 41 43 44 45 45 47 + 47 49 50 50 50 53 53 53 56 56 58 59 59 59 59 + 63 63 63 63 67 67 69 69 69 69 73 73 75 75 77 + 77 77 80 81 81 83 84 84 86 86 88 88 90 90 90 + 90 94 95 96 97 98 99 100 100 102 102 102 105 106 + 106 106 106 110 110 110 110 114 115 115 117 117 119 + 120 121 121 121 124 125 126 127 128 129 129 129 132 + 132 132 135 135 135 138 138 138 141 142 142 144 144 + 146 147 147 149 149 149 152 153 154 154 154 154 158 + 158 158 161 162 162 164 165 165 165 168 168 168 171 + 171 171 171 171 176 176 176 179 180 180 180 183 183 + 183 186 186 188 188 190 190} + +do_execsql_test 1.8.5.4 { + SELECT rank() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2 +} {1 2 3 3 3 6 7 8 9 10 10 12 12 12 1 1 3 3 + 5 5 7 7 9 9 11 12 12 14 14 14 17 17 17 17 + 17 1 1 3 3 3 6 6 8 9 10 11 11 13 13 13 16 + 1 1 3 3 5 5 7 7 7 7 11 11 13 14 15 15 15 + 18 19 19 19 1 2 3 4 4 4 4 8 8 10 11 12 12 + 12 15 15 15 15 19 1 2 2 2 5 6 6 8 8 10 10 + 10 13 13 13 16 16 16 19 19 19 1 2 2 2 5 5 7 + 7 7 7 11 11 13 13 13 16 17 18 19 19 19 1 1 + 1 4 5 5 7 7 9 9 9 9 13 14 15 15 17 17 19 + 19 1 1 1 4 5 5 7 7 7 7 11 12 12 14 15 15 + 1 1 1 4 5 5 5 8 8 8 11 12 12 12 12 16 17 + 18 18 18 21 21} + +do_execsql_test 1.8.5.5 { + SELECT rank() OVER ( ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 15 15 15 15 + 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 + 15 15 36 36 36 36 36 36 36 36 36 36 36 36 36 + 36 36 36 52 52 52 52 52 52 52 52 52 52 52 52 + 52 52 52 52 52 52 52 52 52 73 73 73 73 73 73 + 73 73 73 73 73 73 73 73 73 73 73 73 73 92 92 + 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 + 92 92 92 92 113 113 113 113 113 113 113 113 113 + 113 113 113 113 113 113 113 113 113 113 113 113 134 + 134 134 134 134 134 134 134 134 134 134 134 134 134 + 134 134 134 134 134 134 154 154 154 154 154 154 154 + 154 154 154 154 154 154 154 154 154 170 170 170 170 + 170 170 170 170 170 170 170 170 170 170 170 170 170 + 170 170 170 170 170} + +do_execsql_test 1.8.5.6 { + SELECT rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 15 15 15 15 + 15 15 15 15 15 15 15 15 15 15 15 15 31 31 31 + 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 + 31 50 50 50 50 50 50 50 50 50 50 50 50 50 50 + 50 50 50 50 50 50 50 71 71 71 71 71 71 71 71 + 71 71 71 71 71 71 71 71 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 22 22 22 22 22 22 + 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 + 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 + 43 43 43 43 43 43 64 64 64 64 64 64 64 64 64 + 64 64 64 64 64 64 64 64 64 64 64 84 84 84 84 + 84 84 84 84 84 84 84 84 84 84 84 84 84 84 84 + 84 84 84} + +do_execsql_test 1.8.6.1 { + SELECT + row_number() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ), + rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ), + dense_rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) + FROM t2 +} {1 1 1 2 1 1 3 1 1 4 1 1 5 1 1 6 1 1 7 1 1 8 1 1 9 1 1 + 10 1 1 11 1 1 12 1 1 13 1 1 14 1 1 15 15 2 16 15 2 17 15 2 + 18 15 2 19 15 2 20 15 2 21 15 2 22 15 2 23 15 2 24 15 2 + 25 15 2 26 15 2 27 15 2 28 15 2 29 15 2 30 15 2 31 31 3 + 32 31 3 33 31 3 34 31 3 35 31 3 36 31 3 37 31 3 38 31 3 + 39 31 3 40 31 3 41 31 3 42 31 3 43 31 3 44 31 3 45 31 3 + 46 31 3 47 31 3 48 31 3 49 31 3 50 50 4 51 50 4 52 50 4 + 53 50 4 54 50 4 55 50 4 56 50 4 57 50 4 58 50 4 59 50 4 + 60 50 4 61 50 4 62 50 4 63 50 4 64 50 4 65 50 4 66 50 4 + 67 50 4 68 50 4 69 50 4 70 50 4 71 71 5 72 71 5 73 71 5 + 74 71 5 75 71 5 76 71 5 77 71 5 78 71 5 79 71 5 80 71 5 + 81 71 5 82 71 5 83 71 5 84 71 5 85 71 5 86 71 5 1 1 1 2 1 1 + 3 1 1 4 1 1 5 1 1 6 1 1 7 1 1 8 1 1 9 1 1 10 1 1 11 1 1 + 12 1 1 13 1 1 14 1 1 15 1 1 16 1 1 17 1 1 18 1 1 19 1 1 + 20 1 1 21 1 1 22 22 2 23 22 2 24 22 2 25 22 2 26 22 2 27 22 2 + 28 22 2 29 22 2 30 22 2 31 22 2 32 22 2 33 22 2 34 22 2 + 35 22 2 36 22 2 37 22 2 38 22 2 39 22 2 40 22 2 41 22 2 + 42 22 2 43 43 3 44 43 3 45 43 3 46 43 3 47 43 3 48 43 3 + 49 43 3 50 43 3 51 43 3 52 43 3 53 43 3 54 43 3 55 43 3 + 56 43 3 57 43 3 58 43 3 59 43 3 60 43 3 61 43 3 62 43 3 + 63 43 3 64 64 4 65 64 4 66 64 4 67 64 4 68 64 4 69 64 4 + 70 64 4 71 64 4 72 64 4 73 64 4 74 64 4 75 64 4 76 64 4 + 77 64 4 78 64 4 79 64 4 80 64 4 81 64 4 82 64 4 83 64 4 + 84 84 5 85 84 5 86 84 5 87 84 5 88 84 5 89 84 5 90 84 5 + 91 84 5 92 84 5 93 84 5 94 84 5 95 84 5 96 84 5 97 84 5 + 98 84 5 99 84 5 100 84 5 101 84 5 102 84 5 103 84 5 104 84 5 + 105 84 5} + + +do_test 1.8.7.1 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0053 0.0105 0.0158 0.0211 0.0263 0.0316 0.0368 0.0421 0.0474 0.0526 0.0579 0.0632 0.0684 0.0737 0.0789 0.0842 0.0895 0.0947 0.1000 0.1053 0.1105 0.1158 0.1211 0.1263 0.1316 0.1368 0.1421 0.1474 0.1526 0.1579 0.1632 0.1684 0.1737 0.1789 0.1842 0.1895 0.1947 0.2000 0.2053 0.2105 0.2158 0.2211 0.2263 0.2316 0.2368 0.2421 0.2474 0.2526 0.2579 0.2632 0.2684 0.2737 0.2789 0.2842 0.2895 0.2947 0.3000 0.3053 0.3105 0.3158 0.3211 0.3263 0.3316 0.3368 0.3421 0.3474 0.3526 0.3579 0.3632 0.3684 0.3737 0.3789 0.3842 0.3895 0.3947 0.4000 0.4053 0.4105 0.4158 0.4211 0.4263 0.4316 0.4368 0.4421 0.4474 0.4526 0.4579 0.4632 0.4684 0.4737 0.4789 0.4842 0.4895 0.4947 0.5000 0.5053 0.5105 0.5158 0.5211 0.5263 0.5316 0.5368 0.5421 0.5474 0.5526 0.5579 0.5632 0.5684 0.5737 0.5789 0.5842 0.5895 0.5947 0.6000 0.6053 0.6105 0.6158 0.6211 0.6263 0.6316 0.6368 0.6421 0.6474 0.6526 0.6579 0.6632 0.6684 0.6737 0.6789 0.6842 0.6895 0.6947 0.7000 0.7053 0.7105 0.7158 0.7211 0.7263 0.7316 0.7368 0.7421 0.7474 0.7526 0.7579 0.7632 0.7684 0.7737 0.7789 0.7842 0.7895 0.7947 0.8000 0.8053 0.8105 0.8158 0.8211 0.8263 0.8316 0.8368 0.8421 0.8474 0.8526 0.8579 0.8632 0.8684 0.8737 0.8789 0.8842 0.8895 0.8947 0.9000 0.9053 0.9105 0.9158 0.9211 0.9263 0.9316 0.9368 0.9421 0.9474 0.9526 0.9579 0.9632 0.9684 0.9737 0.9789 0.9842 0.9895 0.9947 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.8.7.2 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0769 0.1538 0.2308 0.3077 0.3846 0.4615 0.5385 0.6154 0.6923 0.7692 0.8462 0.9231 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0667 0.1333 0.2000 0.2667 0.3333 0.4000 0.4667 0.5333 0.6000 0.6667 0.7333 0.8000 0.8667 0.9333 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0556 0.1111 0.1667 0.2222 0.2778 0.3333 0.3889 0.4444 0.5000 0.5556 0.6111 0.6667 0.7222 0.7778 0.8333 0.8889 0.9444 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0526 0.1053 0.1579 0.2105 0.2632 0.3158 0.3684 0.4211 0.4737 0.5263 0.5789 0.6316 0.6842 0.7368 0.7895 0.8421 0.8947 0.9474 1.0000 0.0000 0.0667 0.1333 0.2000 0.2667 0.3333 0.4000 0.4667 0.5333 0.6000 0.6667 0.7333 0.8000 0.8667 0.9333 1.0000 0.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.8.7.3 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY b ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0105 0.0105 0.0211 0.0211 0.0316 0.0368 0.0421 0.0474 0.0474 0.0474 0.0632 0.0632 0.0632 0.0789 0.0789 0.0789 0.0947 0.1000 0.1000 0.1105 0.1105 0.1105 0.1263 0.1263 0.1368 0.1421 0.1421 0.1421 0.1579 0.1579 0.1579 0.1737 0.1789 0.1842 0.1895 0.1895 0.2000 0.2000 0.2105 0.2105 0.2211 0.2263 0.2316 0.2316 0.2421 0.2421 0.2526 0.2579 0.2579 0.2579 0.2737 0.2737 0.2737 0.2895 0.2895 0.3000 0.3053 0.3053 0.3053 0.3053 0.3263 0.3263 0.3263 0.3263 0.3474 0.3474 0.3579 0.3579 0.3579 0.3579 0.3789 0.3789 0.3895 0.3895 0.4000 0.4000 0.4000 0.4158 0.4211 0.4211 0.4316 0.4368 0.4368 0.4474 0.4474 0.4579 0.4579 0.4684 0.4684 0.4684 0.4684 0.4895 0.4947 0.5000 0.5053 0.5105 0.5158 0.5211 0.5211 0.5316 0.5316 0.5316 0.5474 0.5526 0.5526 0.5526 0.5526 0.5737 0.5737 0.5737 0.5737 0.5947 0.6000 0.6000 0.6105 0.6105 0.6211 0.6263 0.6316 0.6316 0.6316 0.6474 0.6526 0.6579 0.6632 0.6684 0.6737 0.6737 0.6737 0.6895 0.6895 0.6895 0.7053 0.7053 0.7053 0.7211 0.7211 0.7211 0.7368 0.7421 0.7421 0.7526 0.7526 0.7632 0.7684 0.7684 0.7789 0.7789 0.7789 0.7947 0.8000 0.8053 0.8053 0.8053 0.8053 0.8263 0.8263 0.8263 0.8421 0.8474 0.8474 0.8579 0.8632 0.8632 0.8632 0.8789 0.8789 0.8789 0.8947 0.8947 0.8947 0.8947 0.8947 0.9211 0.9211 0.9211 0.9368 0.9421 0.9421 0.9421 0.9579 0.9579 0.9579 0.9737 0.9737 0.9842 0.9842 0.9947 0.9947} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.8.7.4 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0769 0.1538 0.1538 0.1538 0.3846 0.4615 0.5385 0.6154 0.6923 0.6923 0.8462 0.8462 0.8462 0.0000 0.0000 0.1000 0.1000 0.2000 0.2000 0.3000 0.3000 0.4000 0.4000 0.5000 0.5500 0.5500 0.6500 0.6500 0.6500 0.8000 0.8000 0.8000 0.8000 0.8000 0.0000 0.0000 0.1333 0.1333 0.1333 0.3333 0.3333 0.4667 0.5333 0.6000 0.6667 0.6667 0.8000 0.8000 0.8000 1.0000 0.0000 0.0000 0.1000 0.1000 0.2000 0.2000 0.3000 0.3000 0.3000 0.3000 0.5000 0.5000 0.6000 0.6500 0.7000 0.7000 0.7000 0.8500 0.9000 0.9000 0.9000 0.0000 0.0556 0.1111 0.1667 0.1667 0.1667 0.1667 0.3889 0.3889 0.5000 0.5556 0.6111 0.6111 0.6111 0.7778 0.7778 0.7778 0.7778 1.0000 0.0000 0.0500 0.0500 0.0500 0.2000 0.2500 0.2500 0.3500 0.3500 0.4500 0.4500 0.4500 0.6000 0.6000 0.6000 0.7500 0.7500 0.7500 0.9000 0.9000 0.9000 0.0000 0.0500 0.0500 0.0500 0.2000 0.2000 0.3000 0.3000 0.3000 0.3000 0.5000 0.5000 0.6000 0.6000 0.6000 0.7500 0.8000 0.8500 0.9000 0.9000 0.9000 0.0000 0.0000 0.0000 0.1579 0.2105 0.2105 0.3158 0.3158 0.4211 0.4211 0.4211 0.4211 0.6316 0.6842 0.7368 0.7368 0.8421 0.8421 0.9474 0.9474 0.0000 0.0000 0.0000 0.2000 0.2667 0.2667 0.4000 0.4000 0.4000 0.4000 0.6667 0.7333 0.7333 0.8667 0.9333 0.9333 0.0000 0.0000 0.0000 0.1429 0.1905 0.1905 0.1905 0.3333 0.3333 0.3333 0.4762 0.5238 0.5238 0.5238 0.5238 0.7143 0.7619 0.8095 0.8095 0.8095 0.9524 0.9524} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.8.7.5 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.8.7.6 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER (PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND CURRENT ROW) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.8.8.1 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0052 0.0105 0.0157 0.0209 0.0262 0.0314 0.0366 0.0419 0.0471 0.0524 0.0576 0.0628 0.0681 0.0733 0.0785 0.0838 0.0890 0.0942 0.0995 0.1047 0.1099 0.1152 0.1204 0.1257 0.1309 0.1361 0.1414 0.1466 0.1518 0.1571 0.1623 0.1675 0.1728 0.1780 0.1832 0.1885 0.1937 0.1990 0.2042 0.2094 0.2147 0.2199 0.2251 0.2304 0.2356 0.2408 0.2461 0.2513 0.2565 0.2618 0.2670 0.2723 0.2775 0.2827 0.2880 0.2932 0.2984 0.3037 0.3089 0.3141 0.3194 0.3246 0.3298 0.3351 0.3403 0.3455 0.3508 0.3560 0.3613 0.3665 0.3717 0.3770 0.3822 0.3874 0.3927 0.3979 0.4031 0.4084 0.4136 0.4188 0.4241 0.4293 0.4346 0.4398 0.4450 0.4503 0.4555 0.4607 0.4660 0.4712 0.4764 0.4817 0.4869 0.4921 0.4974 0.5026 0.5079 0.5131 0.5183 0.5236 0.5288 0.5340 0.5393 0.5445 0.5497 0.5550 0.5602 0.5654 0.5707 0.5759 0.5812 0.5864 0.5916 0.5969 0.6021 0.6073 0.6126 0.6178 0.6230 0.6283 0.6335 0.6387 0.6440 0.6492 0.6545 0.6597 0.6649 0.6702 0.6754 0.6806 0.6859 0.6911 0.6963 0.7016 0.7068 0.7120 0.7173 0.7225 0.7277 0.7330 0.7382 0.7435 0.7487 0.7539 0.7592 0.7644 0.7696 0.7749 0.7801 0.7853 0.7906 0.7958 0.8010 0.8063 0.8115 0.8168 0.8220 0.8272 0.8325 0.8377 0.8429 0.8482 0.8534 0.8586 0.8639 0.8691 0.8743 0.8796 0.8848 0.8901 0.8953 0.9005 0.9058 0.9110 0.9162 0.9215 0.9267 0.9319 0.9372 0.9424 0.9476 0.9529 0.9581 0.9634 0.9686 0.9738 0.9791 0.9843 0.9895 0.9948 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.8.8.2 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0714 0.1429 0.2143 0.2857 0.3571 0.4286 0.5000 0.5714 0.6429 0.7143 0.7857 0.8571 0.9286 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0625 0.1250 0.1875 0.2500 0.3125 0.3750 0.4375 0.5000 0.5625 0.6250 0.6875 0.7500 0.8125 0.8750 0.9375 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0526 0.1053 0.1579 0.2105 0.2632 0.3158 0.3684 0.4211 0.4737 0.5263 0.5789 0.6316 0.6842 0.7368 0.7895 0.8421 0.8947 0.9474 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0625 0.1250 0.1875 0.2500 0.3125 0.3750 0.4375 0.5000 0.5625 0.6250 0.6875 0.7500 0.8125 0.8750 0.9375 1.0000 0.0455 0.0909 0.1364 0.1818 0.2273 0.2727 0.3182 0.3636 0.4091 0.4545 0.5000 0.5455 0.5909 0.6364 0.6818 0.7273 0.7727 0.8182 0.8636 0.9091 0.9545 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.8.8.3 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY b ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0105 0.0105 0.0209 0.0209 0.0314 0.0314 0.0366 0.0419 0.0471 0.0628 0.0628 0.0628 0.0785 0.0785 0.0785 0.0942 0.0942 0.0942 0.0995 0.1099 0.1099 0.1257 0.1257 0.1257 0.1361 0.1361 0.1414 0.1571 0.1571 0.1571 0.1728 0.1728 0.1728 0.1780 0.1832 0.1885 0.1990 0.1990 0.2094 0.2094 0.2199 0.2199 0.2251 0.2304 0.2408 0.2408 0.2513 0.2513 0.2565 0.2723 0.2723 0.2723 0.2880 0.2880 0.2880 0.2984 0.2984 0.3037 0.3246 0.3246 0.3246 0.3246 0.3455 0.3455 0.3455 0.3455 0.3560 0.3560 0.3770 0.3770 0.3770 0.3770 0.3874 0.3874 0.3979 0.3979 0.4136 0.4136 0.4136 0.4188 0.4293 0.4293 0.4346 0.4450 0.4450 0.4555 0.4555 0.4660 0.4660 0.4869 0.4869 0.4869 0.4869 0.4921 0.4974 0.5026 0.5079 0.5131 0.5183 0.5288 0.5288 0.5445 0.5445 0.5445 0.5497 0.5707 0.5707 0.5707 0.5707 0.5916 0.5916 0.5916 0.5916 0.5969 0.6073 0.6073 0.6178 0.6178 0.6230 0.6283 0.6440 0.6440 0.6440 0.6492 0.6545 0.6597 0.6649 0.6702 0.6859 0.6859 0.6859 0.7016 0.7016 0.7016 0.7173 0.7173 0.7173 0.7330 0.7330 0.7330 0.7382 0.7487 0.7487 0.7592 0.7592 0.7644 0.7749 0.7749 0.7906 0.7906 0.7906 0.7958 0.8010 0.8220 0.8220 0.8220 0.8220 0.8377 0.8377 0.8377 0.8429 0.8534 0.8534 0.8586 0.8743 0.8743 0.8743 0.8901 0.8901 0.8901 0.9162 0.9162 0.9162 0.9162 0.9162 0.9319 0.9319 0.9319 0.9372 0.9529 0.9529 0.9529 0.9686 0.9686 0.9686 0.9791 0.9791 0.9895 0.9895 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.8.8.4 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0714 0.1429 0.3571 0.3571 0.3571 0.4286 0.5000 0.5714 0.6429 0.7857 0.7857 1.0000 1.0000 1.0000 0.0952 0.0952 0.1905 0.1905 0.2857 0.2857 0.3810 0.3810 0.4762 0.4762 0.5238 0.6190 0.6190 0.7619 0.7619 0.7619 1.0000 1.0000 1.0000 1.0000 1.0000 0.1250 0.1250 0.3125 0.3125 0.3125 0.4375 0.4375 0.5000 0.5625 0.6250 0.7500 0.7500 0.9375 0.9375 0.9375 1.0000 0.0952 0.0952 0.1905 0.1905 0.2857 0.2857 0.4762 0.4762 0.4762 0.4762 0.5714 0.5714 0.6190 0.6667 0.8095 0.8095 0.8095 0.8571 1.0000 1.0000 1.0000 0.0526 0.1053 0.1579 0.3684 0.3684 0.3684 0.3684 0.4737 0.4737 0.5263 0.5789 0.7368 0.7368 0.7368 0.9474 0.9474 0.9474 0.9474 1.0000 0.0476 0.1905 0.1905 0.1905 0.2381 0.3333 0.3333 0.4286 0.4286 0.5714 0.5714 0.5714 0.7143 0.7143 0.7143 0.8571 0.8571 0.8571 1.0000 1.0000 1.0000 0.0476 0.1905 0.1905 0.1905 0.2857 0.2857 0.4762 0.4762 0.4762 0.4762 0.5714 0.5714 0.7143 0.7143 0.7143 0.7619 0.8095 0.8571 1.0000 1.0000 1.0000 0.1500 0.1500 0.1500 0.2000 0.3000 0.3000 0.4000 0.4000 0.6000 0.6000 0.6000 0.6000 0.6500 0.7000 0.8000 0.8000 0.9000 0.9000 1.0000 1.0000 0.1875 0.1875 0.1875 0.2500 0.3750 0.3750 0.6250 0.6250 0.6250 0.6250 0.6875 0.8125 0.8125 0.8750 1.0000 1.0000 0.1364 0.1364 0.1364 0.1818 0.3182 0.3182 0.3182 0.4545 0.4545 0.4545 0.5000 0.6818 0.6818 0.6818 0.6818 0.7273 0.7727 0.9091 0.9091 0.9091 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.8.8.5 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.8.8.6 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.8.8.1 { + set myres {} + foreach r [db eval {SELECT ntile(100) OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 88.0000 89.0000 89.0000 90.0000 90.0000 91.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.8.8.2 { + set myres {} + foreach r [db eval {SELECT ntile(101) OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 22.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.8.8.3 { + set myres {} + foreach r [db eval {SELECT ntile(102) OVER ( ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 88.0000 89.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.8.8.4 { + set myres {} + foreach r [db eval {SELECT ntile(103) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 22.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.8.8.5 { + set myres {} + foreach r [db eval {SELECT ntile(104) OVER ( ORDER BY b%10,a ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000 103.0000 104.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.8.8.6 { + set myres {} + foreach r [db eval {SELECT ntile(105) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND CURRENT ROW) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.8.8.7 { + set myres {} + foreach r [db eval {SELECT ntile(105) OVER ( ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 88.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000 103.0000 104.0000 105.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + +do_execsql_test 1.8.9.1 { + SELECT last_value(a+b) OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2 +} {99 92 108 72 52 83 55 79 109 65 26 120 119 50 + 70 103 80 124 36 96 59 124 116 110 57 51 52 130 + 103 74 87 48 128 117 105 136 131 71 133 92 109 63 + 84 109 57 146 78 147 113 74 88 150 87 110 65 121 + 106 110 124 85 145 107 161 171 150 156 80 171 120 + 109 158 114 111 136 147 87 173 124 168 173 162 132 + 101 154 167 190 161 110 156 195 198 102 123 177 169 + 140 111 180 119 160 197 152 124 121 134 146 147 132 + 213 141 193 200 210 157 132 136 175 161 218 188 226 + 191 187 208 211 179 138 144 223 196 214 170 212 202 + 163 184 172 173 195 229 240 187 210 200 163 227 228 + 223 191 252 235 225 243 172 187 202 179 179 182 231 + 261 207 263 206 189 209 212 276 181 274 249 239 234 + 213 234 269 196 271 221 210 229 235 250 223 232 229 + 279 224 280 216 207} + +do_execsql_test 1.8.9.2 { + SELECT last_value(a+b) OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2 +} {147 106 109 168 134 218 191 212 229 240 213 234 196 + 223 92 109 105 136 146 65 156 132 154 102 123 119 + 160 152 146 147 136 243 261 263 210 79 63 84 78 + 120 87 162 124 141 138 227 228 179 231 234 280 124 + 57 130 92 57 110 114 136 147 167 110 180 193 191 + 252 187 179 206 181 221 279 80 116 117 71 80 171 + 173 177 157 161 179 214 225 182 209 269 271 235 229 + 103 74 131 133 113 74 87 145 190 161 169 140 111 + 132 213 187 208 223 235 189 274 108 65 26 70 51 + 52 128 109 121 124 85 107 150 195 226 172 173 187 + 223 207 212 119 50 124 96 110 87 48 110 173 124 + 197 211 144 196 195 200 202 224 216 207 52 83 103 + 36 88 171 158 156 198 121 210 132 210 239 250 232 + 99 72 55 120 59 109 150 161 111 101 200 175 188 + 170 202 163 184 163 172 276 249 229} + +do_execsql_test 1.8.9.3 { + SELECT last_value(a+b) OVER ( ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2 +} {65 102 87 138 57 181 80 111 26 48 144 207 36 121 + 132 101 163 172 196 119 136 63 124 179 110 179 182 + 74 132 189 51 52 85 216 163 134 123 210 78 141 57 + 187 71 87 172 173 50 224 88 59 111 170 109 213 + 223 146 147 84 114 191 206 221 157 161 209 229 74 + 140 107 187 207 212 124 202 52 232 55 184 229 106 + 132 152 120 92 110 179 235 65 70 87 110 195 200 + 175 234 160 234 136 80 113 187 109 121 124 196 156 + 210 239 250 72 109 188 202 191 105 154 79 231 147 + 225 103 161 169 223 96 83 249 212 162 227 228 167 + 180 193 117 177 214 145 208 235 150 110 211 103 158 + 200 168 229 92 156 243 280 279 116 173 269 271 131 + 133 223 128 173 197 210 99 150 161 147 218 240 109 + 136 146 261 263 124 130 252 171 190 213 274 108 195 + 226 119 124 171 198 120 276} + +do_execsql_test 1.8.9.4 { + SELECT last_value(a+b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2 +} {196 134 109 213 223 106 234 191 212 168 229 147 218 + 240 65 102 119 136 123 210 146 147 132 152 160 105 + 154 92 156 243 109 136 146 261 263 87 138 63 124 + 179 78 141 84 120 234 79 231 162 227 228 280 57 + 181 110 179 57 187 114 191 206 221 92 110 136 147 + 167 180 193 279 124 130 252 80 182 71 157 161 209 + 229 179 235 80 225 117 177 214 116 173 269 271 171 + 111 74 132 189 87 74 140 113 187 103 161 169 145 + 208 235 131 133 223 190 213 274 26 51 52 85 172 + 173 107 187 207 212 65 70 109 121 124 223 150 128 + 108 195 226 48 144 207 216 50 224 124 202 87 110 + 195 200 196 96 110 211 173 197 119 124 36 121 132 + 88 52 232 156 210 239 250 83 103 158 210 171 198 + 101 163 172 163 59 111 170 55 184 229 175 72 109 + 188 202 249 200 99 150 161 120 276} + +do_execsql_test 1.8.9.5 { + SELECT last_value(a+b) OVER ( ORDER BY b%10,a ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2 +} {147 106 109 168 134 218 191 212 229 240 213 234 196 + 223 92 109 105 136 146 65 156 132 154 102 123 119 + 160 152 146 147 136 243 261 263 210 79 63 84 78 + 120 87 162 124 141 138 227 228 179 231 234 280 124 + 57 130 92 57 110 114 136 147 167 110 180 193 191 + 252 187 179 206 181 221 279 80 116 117 71 80 171 + 173 177 157 161 179 214 225 182 209 269 271 235 229 + 103 74 131 133 113 74 87 145 190 161 169 140 111 + 132 213 187 208 223 235 189 274 108 65 26 70 51 + 52 128 109 121 124 85 107 150 195 226 172 173 187 + 223 207 212 119 50 124 96 110 87 48 110 173 124 + 197 211 144 196 195 200 202 224 216 207 52 83 103 + 36 88 171 158 156 198 121 210 132 210 239 250 232 + 99 72 55 120 59 109 150 161 111 101 200 175 188 + 170 202 163 184 163 172 276 249 229} + +do_execsql_test 1.8.9.6 { + SELECT last_value(a+b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND CURRENT ROW) FROM t2 +} {108 52 83 79 65 26 70 103 80 36 116 51 52 128 + 117 71 63 84 109 78 147 88 121 106 124 85 107 171 + 150 80 171 120 109 158 87 168 173 162 156 195 198 + 177 124 121 134 141 210 157 132 161 218 226 191 179 + 138 214 212 172 173 229 240 187 210 227 228 223 225 + 179 182 231 207 209 212 239 234 213 234 269 196 271 + 235 250 223 232 229 280 99 92 72 55 109 120 119 + 50 124 96 59 124 110 57 130 103 74 87 48 105 136 + 131 133 92 109 57 146 113 74 150 87 110 65 110 + 145 161 156 114 111 136 147 173 124 132 101 154 167 + 190 161 110 102 123 169 140 111 180 119 160 197 152 + 146 147 132 213 193 200 136 175 188 187 208 211 144 + 223 196 170 202 163 184 195 200 163 191 252 235 243 + 172 187 202 179 261 263 206 189 276 181 274 249 221 + 210 229 279 224 216 207} + +do_execsql_test 1.8.10.1 { + SELECT nth_value(b,b+1) OVER (ORDER BY a ROWS BETWEEN 4 PRECEDING AND CURRENT ROW) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} 56 + {} {} {} {} {} {} {} {} {} 89 {} {} {} {} {} + {} {} {} {} {} {} 4 {} {} {} {} {} {} {} {} + 53 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 58 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} 77 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} 99 {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.8.10.2 { + SELECT nth_value(b,b+1) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND CURRENT ROW) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 61 {} {} {} 81 {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} 22 {} {} {} 12 + {} {} {} {} {} {} {} {} {} {} 43 {} {} {} {} + {} {} {} {} {} {} {} {} {} 33 {} {} {} {} {} + {} 4 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.8.10.3 { + SELECT nth_value(b,b+1) OVER ( ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2 +} {{} 1 2 2 2 3 4 {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.8.10.4 { + SELECT nth_value(b,b+1) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + 1 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.8.10.5 { + SELECT nth_value(b,b+1) OVER ( ORDER BY b%10,a ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 61 {} {} {} 81 {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} 22 {} {} {} 12 + {} {} {} {} {} {} {} {} {} {} 43 {} {} {} {} + {} {} {} {} {} {} {} {} {} 33 {} {} {} {} {} + {} 4 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.8.10.6 { + SELECT nth_value(b,b+1) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND CURRENT ROW) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.8.11.1 { + SELECT first_value(b) OVER (ORDER BY a ROWS BETWEEN 4 PRECEDING AND CURRENT ROW) FROM t2 +} {89 89 89 89 89 81 96 59 38 68 39 62 91 46 6 + 99 97 27 46 78 54 97 8 67 29 93 84 77 23 16 + 16 93 65 35 47 7 86 74 61 91 85 24 85 43 59 + 12 32 56 3 91 22 90 55 15 28 89 25 47 1 56 40 + 43 56 16 75 36 89 98 76 81 4 94 42 30 78 33 + 29 53 63 2 87 37 80 84 72 41 9 61 73 95 65 13 + 58 96 98 1 21 74 65 35 5 73 11 51 87 41 12 8 + 20 31 31 15 95 22 73 79 88 34 8 11 49 34 90 + 59 96 60 55 75 77 44 2 7 85 57 74 29 70 59 19 + 39 26 26 47 80 90 36 58 47 9 72 72 66 33 93 + 75 64 81 9 23 37 13 12 14 62 91 36 91 33 15 + 34 36 99 3 95 69 58 52 30 50 84 10 84 33 21 + 39 44 58 30 38 34 83} + +do_execsql_test 1.8.11.2 { + SELECT first_value(b) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND CURRENT ROW) FROM t2 +} {90 90 90 90 90 40 30 80 20 90 60 70 80 90 81 + 81 81 81 81 91 61 91 91 1 81 41 61 1 21 11 51 + 41 31 31 11 62 62 62 62 62 12 32 22 42 2 72 + 12 22 2 72 72 93 93 93 93 93 23 93 43 3 43 33 + 53 63 73 13 73 73 33 93 23 13 54 54 54 54 54 + 84 74 24 4 94 84 74 34 34 44 74 64 14 34 65 + 65 65 65 65 35 85 85 55 15 25 75 95 65 65 35 + 5 15 95 55 75 96 96 96 96 96 46 6 46 16 16 86 + 56 56 56 16 36 76 96 96 26 26 97 97 97 97 97 + 27 97 67 77 47 7 47 87 37 87 77 7 57 47 47 38 + 38 38 38 38 68 78 8 28 98 78 58 98 8 88 8 89 + 89 89 89 89 59 39 99 29 59 89 89 29 9 79 49 + 59 29 59 19 39 9} + +do_execsql_test 1.8.11.3 { + SELECT first_value(b) OVER ( ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2 +} {1 1 1 1 1 1 2 2 3 3 4 5 6 7 7 7 8 8 8 + 9 9 9 10 11 11 12 12 12 13 13 14 15 15 15 16 + 16 16 17 19 20 21 21 22 22 23 23 24 25 26 26 + 27 27 28 29 29 29 30 30 30 31 31 32 33 33 33 + 33 34 34 34 34 35 35 36 36 36 36 37 37 38 38 + 39 39 39 40 41 41 42 43 43 44 44 46 46 47 47 + 47 47 49 50 51 52 53 54 55 55 56 56 56 57 58 + 58 58 58 59 59 59 59 60 61 61 62 62 63 64 65 + 65 65 66 67 68 69 70 72 72 72 73 73 73 74 74 + 74 75 75 75 76 77 77 78 78 79 80 80 81 81 81 + 82 83 84 84 84 84 85 85 85 86 87 87 88 89 89 + 89 90 90 90 91 91 91 91 91 93 93 93 94 95 95 + 95 96 96 96 97 97} + +do_execsql_test 1.8.11.4 { + SELECT first_value(b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2 +} {10 10 10 10 10 20 30 30 30 40 50 60 70 80 1 1 + 1 1 1 1 11 11 21 21 31 31 41 41 51 61 61 81 + 81 81 91 2 2 2 2 2 2 12 12 12 22 22 32 42 + 52 62 62 3 3 3 3 3 3 13 13 23 23 33 33 33 + 33 43 43 53 63 73 73 73 4 4 4 4 4 14 24 34 + 34 34 34 44 44 54 64 74 74 74 84 5 5 5 5 5 + 15 15 15 25 35 35 55 55 65 65 65 75 75 75 85 + 85 6 6 6 6 6 16 16 16 26 26 36 36 36 36 46 + 46 56 56 56 66 76 7 7 7 7 7 7 7 17 27 27 37 + 37 47 47 47 47 57 67 77 77 8 8 8 8 8 8 8 28 + 38 38 58 58 58 58 68 78 9 9 9 9 9 9 9 19 29 + 29 29 39 39 39 49 59 59 59 59 69 79 89} + +do_execsql_test 1.8.11.5 { + SELECT first_value(b) OVER ( ORDER BY b%10,a ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2 +} {90 90 90 90 90 40 30 80 20 90 60 70 80 90 30 + 50 10 30 81 91 61 91 91 1 81 41 61 1 21 11 51 + 41 31 31 11 81 91 91 21 62 12 32 22 42 2 72 + 12 22 2 72 72 12 62 52 82 93 23 93 43 3 43 33 + 53 63 73 13 73 73 33 93 23 13 33 3 33 83 54 + 84 74 24 4 94 84 74 34 34 44 74 64 14 34 84 + 84 44 34 65 35 85 85 55 15 25 75 95 65 65 35 + 5 15 95 55 75 85 75 15 95 96 46 6 46 16 16 86 + 56 56 56 16 36 76 96 96 26 26 36 66 36 36 97 + 27 97 67 77 47 7 47 87 37 87 77 7 57 47 47 37 + 27 17 7 38 68 78 8 28 98 78 58 98 8 88 8 58 + 58 58 38 89 59 39 99 29 59 89 89 29 9 79 49 + 59 29 59 19 39 9} + +do_execsql_test 1.8.11.6 { + SELECT first_value(b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND CURRENT ROW) FROM t2 +} {96 38 68 62 46 6 46 78 54 8 84 16 16 86 74 24 + 12 32 56 22 90 28 56 40 56 16 36 98 76 4 94 + 42 30 78 2 80 84 72 58 96 98 74 12 8 20 22 88 + 34 8 34 90 96 60 44 2 74 70 26 26 80 90 36 58 + 72 72 66 64 12 14 62 36 34 36 58 52 30 50 84 + 10 84 44 58 30 38 34 82 89 81 59 39 91 99 97 + 27 97 67 29 93 77 23 93 65 35 47 7 61 91 85 + 85 43 59 3 91 55 15 89 25 47 1 43 75 89 81 33 + 29 53 63 87 37 41 9 61 73 95 65 13 1 21 65 35 + 5 73 11 51 87 41 31 31 15 95 73 79 11 49 59 + 55 75 77 7 85 57 29 59 19 39 47 47 9 33 93 75 + 81 9 23 37 13 91 91 33 15 99 3 95 69 33 21 39 + 83 27 17 7} + +do_execsql_test 1.8.12.1 { + SELECT lead(b,b) OVER (ORDER BY a ROWS BETWEEN 4 PRECEDING AND CURRENT ROW) FROM t2 +} {96 9 11 89 32 53 91 30 51 56 54 73 22 59 75 + 74 78 8 16 65 15 8 31 87 90 12 32 96 74 76 37 + 85 90 15 35 2 60 36 75 9 51 47 63 51 90 26 42 + 26 8 76 80 90 37 87 56 79 5 87 8 2 39 73 64 + 36 90 72 78 36 73 51 33 20 41 2 26 37 33 8 14 + 33 81 55 1 9 12 39 64 87 72 34 82 21 34 99 62 + 74 41 69 22 75 27 58 8 79 77 26 26 55 {} 29 + 30 7 {} 66 55 2 34 64 {} 33 {} 44 84 {} {} 95 + 85 19 {} 83 {} 91 {} {} 9 50 91 33 34 {} {} + 84 {} 7 9 {} {} {} 44 {} {} {} {} 91 84 {} 95 + 95 52 {} {} {} {} {} 21 {} {} {} 58 {} {} {} + {} {} {} {} 83 {} {} {} {} {} {} {} {} {} {} + {} {} {} {}} + +do_execsql_test 1.8.12.2 { + SELECT lead(b,b) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND CURRENT ROW) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 81 {} {} {} 21 {} {} {} {} {} {} + {} {} {} {} {} {} 62 {} {} {} 12 {} {} {} 72 + {} {} {} {} {} {} {} {} {} {} 53 {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 34 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} 95 {} {} {} {} {} {} 85 {} + {} {} {} {} {} {} {} {} {} 56 {} 36 {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 57 {} {} {} {} {} 7 {} {} {} {} + {} {} {} {} {} {} 8 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 9 {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.8.12.3 { + SELECT lead(b,b) OVER ( ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2 +} {1 2 3 3 5 6 7 8 8 9 9 10 11 12 12 13 13 + 14 15 16 16 17 19 20 21 22 23 24 25 26 27 27 + 28 29 30 31 32 33 33 33 34 34 35 36 36 36 37 + 38 39 39 40 41 42 43 43 44 46 47 47 47 49 50 + 52 53 54 55 56 56 57 58 58 58 59 59 59 60 61 + 62 62 64 65 65 67 69 70 72 72 73 74 74 75 75 + 75 77 78 80 81 81 83 84 84 85 85 85 87 88 89 + 89 89 90 90 91 91 91 93 93 94 95 95 96 97 97 + 98 99 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.8.12.4 { + SELECT lead(b,b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2 +} {80 {} {} {} {} {} {} {} {} {} {} {} {} {} 1 + 11 81 81 {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} 12 12 72 82 {} {} {} {} {} {} + {} {} {} {} {} {} 13 23 73 73 {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} 34 84 {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 35 85 85 95 {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} 36 86 96 96 {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 37 47 + 47 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} 58 58 68 {} {} {} {} {} {} {} {} {} + {} {} {} {} 39 49 59 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.8.12.5 { + SELECT lead(b,b) OVER ( ORDER BY b%10,a ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2 +} {34 72 91 74 21 55 23 94 84 95 2 13 61 22 55 + 55 94 85 75 81 65 73 74 21 72 12 94 33 63 73 + 72 96 36 76 3 25 62 3 73 34 12 46 43 93 72 16 + 86 63 15 65 36 77 24 57 25 53 95 34 95 16 97 + 74 97 67 25 98 44 34 65 54 5 68 96 28 47 95 + 34 39 8 38 6 46 96 28 47 95 56 39 99 97 76 8 + 26 9 79 27 95 16 29 {} 58 58 77 85 56 {} 98 + 29 {} 19 96 {} {} 78 56 98 36 97 {} 89 89 29 + 47 78 {} {} {} 38 68 58 {} 58 38 {} 98 {} {} + {} 39 57 9 {} 79 {} {} 7 {} {} {} 9 29 38 78 + {} {} {} 8 39 {} {} {} {} 59 {} 99 {} {} {} + {} {} {} {} {} {} {} {} {} {} 9 {} {} {} {} + {} {} {} {} {} {} {} {}} + +do_execsql_test 1.8.12.6 { + SELECT lead(b,b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND CURRENT ROW) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.8.13.1 { + SELECT lag(b,b) OVER (ORDER BY a ROWS BETWEEN 4 PRECEDING AND CURRENT ROW) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} 38 {} {} {} {} + {} {} {} 6 {} {} {} {} {} 81 46 6 {} {} {} {} + 23 {} {} {} {} {} 27 {} {} {} 35 6 {} 12 {} + 23 {} {} 61 84 {} 93 39 47 {} 54 46 96 56 {} + 16 {} {} {} {} 89 {} 16 43 {} 85 56 29 99 53 + {} 59 {} {} 91 59 53 84 99 {} 93 63 47 {} {} + 98 33 67 35 75 1 23 13 55 27 75 98 35 73 63 2 + 21 27 13 24 86 23 84 31 20 94 61 65 75 23 36 + 94 55 90 41 77 96 56 29 40 12 89 63 11 5 73 + 79 1 16 28 31 73 5 39 53 63 41 11 40 2 13 33 + 9 29 90 47 72 9 73 30 44 33 74 93 29 74 42 34 + 63 41 34 96 47 77 1 36 74 72 14 36 26 77 9 72 + 64 8 91 31 52 30} + +do_execsql_test 1.8.13.2 { + SELECT lag(b,b) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND CURRENT ROW) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} 30 {} {} + {} {} {} {} 91 {} {} {} 61 {} 81 {} {} {} {} + 1 {} {} {} {} {} {} {} {} {} 22 {} {} {} 12 + {} {} 62 {} {} {} {} {} {} {} 23 {} {} {} {} + {} {} {} {} {} {} {} 43 {} 23 {} {} {} {} {} + {} 54 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 75 {} + {} {} {} {} {} 55 {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} 47 {} {} {} {} + {} 27 7 {} {} {} {} {} {} {} {} {} 68 {} 8 {} + {} {} {} {} {} {} {} {} {} {} {} {} 89 {} {} + {} {} {} {} {} 29 9 {} {} {}} + +do_execsql_test 1.8.13.3 { + SELECT lag(b,b) OVER ( ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2 +} {{} 1 1 1 1 2 2 2 2 2 2 3 3 3 4 4 5 6 6 + 6 7 7 7 7 7 8 8 8 8 8 8 9 9 9 9 9 9 9 + 9 9 9 10 10 10 10 11 11 11 11 11 12 12 12 12 + 13 13 13 13 13 14 15 15 15 15 16 16 16 16 16 + 17 19 20 20 21 21 21 21 22 22 22 22 23 23 23 + 23 23 24 23 24 24 25 26 26 26 26 26 26 26 26 + 26 26 26 27 27 27 27 28 29 29 29 29 30 30 30 + 30 30 30 31 31 31 31 31 32 32 32 32 32 32 31 + 32 33 33 33 33 33 33 34 34 34 34 34 34 34 34 + 35 35 35 35 35 36 36 36 36 36 36 36 37 37 37 + 38 38 38 38 38 38 39 39 39 39 40 40 41 41 42 + 43 42 43 43 43 43 44 44 44 46 46 46 47 47 47 + 47 47} + +do_execsql_test 1.8.13.4 { + SELECT lag(b,b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + 1 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.8.13.5 { + SELECT lag(b,b) OVER ( ORDER BY b%10,a ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} 30 {} {} + {} {} {} {} 91 {} {} {} 61 80 81 {} {} {} {} + 1 {} {} {} 30 {} 21 90 61 {} 22 {} 11 41 12 + {} {} 62 {} {} {} {} 31 {} 50 23 30 21 90 {} + {} 62 {} {} 81 {} 22 43 62 23 32 {} 91 {} 90 + 93 54 {} {} 90 72 12 22 90 81 83 23 80 20 72 + 43 51 33 80 90 2 34 54 1 20 62 12 13 75 44 30 + 93 91 1 21 55 61 61 13 85 3 65 65 91 73 33 93 + 55 84 62 31 11 65 35 85 33 55 15 12 75 22 3 + 73 65 36 85 43 95 43 13 47 44 65 65 96 36 27 + 7 46 34 94 47 36 73 34 35 73 68 24 8 75 85 75 + 66 34 95 36 84 77 46 34 84 47 89 65 36 16 38 + 76 58 57 29 9 44 56 17} + +do_execsql_test 1.8.13.6 { + SELECT lag(b,b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND CURRENT ROW) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.8.14.1 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (ORDER BY a ROWS BETWEEN 4 PRECEDING AND CURRENT ROW) FROM t2 +} {89 89.81 89.81.96 89.81.96.59 89.81.96.59.38 81.96.59.38.68 + 96.59.38.68.39 59.38.68.39.62 38.68.39.62.91 68.39.62.91.46 + 39.62.91.46.6 62.91.46.6.99 91.46.6.99.97 46.6.99.97.27 + 6.99.97.27.46 99.97.27.46.78 97.27.46.78.54 27.46.78.54.97 + 46.78.54.97.8 78.54.97.8.67 54.97.8.67.29 97.8.67.29.93 + 8.67.29.93.84 67.29.93.84.77 29.93.84.77.23 93.84.77.23.16 + 84.77.23.16.16 77.23.16.16.93 23.16.16.93.65 16.16.93.65.35 + 16.93.65.35.47 93.65.35.47.7 65.35.47.7.86 35.47.7.86.74 + 47.7.86.74.61 7.86.74.61.91 86.74.61.91.85 74.61.91.85.24 + 61.91.85.24.85 91.85.24.85.43 85.24.85.43.59 24.85.43.59.12 + 85.43.59.12.32 43.59.12.32.56 59.12.32.56.3 12.32.56.3.91 + 32.56.3.91.22 56.3.91.22.90 3.91.22.90.55 91.22.90.55.15 + 22.90.55.15.28 90.55.15.28.89 55.15.28.89.25 15.28.89.25.47 + 28.89.25.47.1 89.25.47.1.56 25.47.1.56.40 47.1.56.40.43 + 1.56.40.43.56 56.40.43.56.16 40.43.56.16.75 43.56.16.75.36 + 56.16.75.36.89 16.75.36.89.98 75.36.89.98.76 36.89.98.76.81 + 89.98.76.81.4 98.76.81.4.94 76.81.4.94.42 81.4.94.42.30 + 4.94.42.30.78 94.42.30.78.33 42.30.78.33.29 30.78.33.29.53 + 78.33.29.53.63 33.29.53.63.2 29.53.63.2.87 53.63.2.87.37 + 63.2.87.37.80 2.87.37.80.84 87.37.80.84.72 37.80.84.72.41 + 80.84.72.41.9 84.72.41.9.61 72.41.9.61.73 41.9.61.73.95 + 9.61.73.95.65 61.73.95.65.13 73.95.65.13.58 95.65.13.58.96 + 65.13.58.96.98 13.58.96.98.1 58.96.98.1.21 96.98.1.21.74 + 98.1.21.74.65 1.21.74.65.35 21.74.65.35.5 74.65.35.5.73 + 65.35.5.73.11 35.5.73.11.51 5.73.11.51.87 73.11.51.87.41 + 11.51.87.41.12 51.87.41.12.8 87.41.12.8.20 41.12.8.20.31 + 12.8.20.31.31 8.20.31.31.15 20.31.31.15.95 31.31.15.95.22 + 31.15.95.22.73 15.95.22.73.79 95.22.73.79.88 22.73.79.88.34 + 73.79.88.34.8 79.88.34.8.11 88.34.8.11.49 34.8.11.49.34 + 8.11.49.34.90 11.49.34.90.59 49.34.90.59.96 34.90.59.96.60 + 90.59.96.60.55 59.96.60.55.75 96.60.55.75.77 60.55.75.77.44 + 55.75.77.44.2 75.77.44.2.7 77.44.2.7.85 44.2.7.85.57 2.7.85.57.74 + 7.85.57.74.29 85.57.74.29.70 57.74.29.70.59 74.29.70.59.19 + 29.70.59.19.39 70.59.19.39.26 59.19.39.26.26 19.39.26.26.47 + 39.26.26.47.80 26.26.47.80.90 26.47.80.90.36 47.80.90.36.58 + 80.90.36.58.47 90.36.58.47.9 36.58.47.9.72 58.47.9.72.72 + 47.9.72.72.66 9.72.72.66.33 72.72.66.33.93 72.66.33.93.75 + 66.33.93.75.64 33.93.75.64.81 93.75.64.81.9 75.64.81.9.23 + 64.81.9.23.37 81.9.23.37.13 9.23.37.13.12 23.37.13.12.14 + 37.13.12.14.62 13.12.14.62.91 12.14.62.91.36 14.62.91.36.91 + 62.91.36.91.33 91.36.91.33.15 36.91.33.15.34 91.33.15.34.36 + 33.15.34.36.99 15.34.36.99.3 34.36.99.3.95 36.99.3.95.69 + 99.3.95.69.58 3.95.69.58.52 95.69.58.52.30 69.58.52.30.50 + 58.52.30.50.84 52.30.50.84.10 30.50.84.10.84 50.84.10.84.33 + 84.10.84.33.21 10.84.33.21.39 84.33.21.39.44 33.21.39.44.58 + 21.39.44.58.30 39.44.58.30.38 44.58.30.38.34 58.30.38.34.83 + 30.38.34.83.27 38.34.83.27.82 34.83.27.82.17 83.27.82.17.7} + +do_execsql_test 1.8.14.2 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND CURRENT ROW) FROM t2 +} {90 90.40 90.40.30 90.40.30.80 90.40.30.80.20 40.30.80.20.90 + 30.80.20.90.60 80.20.90.60.70 20.90.60.70.80 90.60.70.80.90 + 60.70.80.90.30 70.80.90.30.50 80.90.30.50.10 90.30.50.10.30 81 + 81.91 81.91.61 81.91.61.91 81.91.61.91.91 91.61.91.91.1 + 61.91.91.1.81 91.91.1.81.41 91.1.81.41.61 1.81.41.61.1 + 81.41.61.1.21 41.61.1.21.11 61.1.21.11.51 1.21.11.51.41 + 21.11.51.41.31 11.51.41.31.31 51.41.31.31.11 41.31.31.11.81 + 31.31.11.81.91 31.11.81.91.91 11.81.91.91.21 62 62.12 62.12.32 + 62.12.32.22 62.12.32.22.42 12.32.22.42.2 32.22.42.2.72 + 22.42.2.72.12 42.2.72.12.22 2.72.12.22.2 72.12.22.2.72 + 12.22.2.72.72 22.2.72.72.12 2.72.72.12.62 72.72.12.62.52 + 72.12.62.52.82 93 93.23 93.23.93 93.23.93.43 93.23.93.43.3 + 23.93.43.3.43 93.43.3.43.33 43.3.43.33.53 3.43.33.53.63 + 43.33.53.63.73 33.53.63.73.13 53.63.73.13.73 63.73.13.73.73 + 73.13.73.73.33 13.73.73.33.93 73.73.33.93.23 73.33.93.23.13 + 33.93.23.13.33 93.23.13.33.3 23.13.33.3.33 13.33.3.33.83 54 54.84 + 54.84.74 54.84.74.24 54.84.74.24.4 84.74.24.4.94 74.24.4.94.84 + 24.4.94.84.74 4.94.84.74.34 94.84.74.34.34 84.74.34.34.44 + 74.34.34.44.74 34.34.44.74.64 34.44.74.64.14 44.74.64.14.34 + 74.64.14.34.84 64.14.34.84.84 14.34.84.84.44 34.84.84.44.34 65 + 65.35 65.35.85 65.35.85.85 65.35.85.85.55 35.85.85.55.15 + 85.85.55.15.25 85.55.15.25.75 55.15.25.75.95 15.25.75.95.65 + 25.75.95.65.65 75.95.65.65.35 95.65.65.35.5 65.65.35.5.15 + 65.35.5.15.95 35.5.15.95.55 5.15.95.55.75 15.95.55.75.85 + 95.55.75.85.75 55.75.85.75.15 75.85.75.15.95 96 96.46 96.46.6 + 96.46.6.46 96.46.6.46.16 46.6.46.16.16 6.46.16.16.86 46.16.16.86.56 + 16.16.86.56.56 16.86.56.56.56 86.56.56.56.16 56.56.56.16.36 + 56.56.16.36.76 56.16.36.76.96 16.36.76.96.96 36.76.96.96.26 + 76.96.96.26.26 96.96.26.26.36 96.26.26.36.66 26.26.36.66.36 + 26.36.66.36.36 97 97.27 97.27.97 97.27.97.67 97.27.97.67.77 + 27.97.67.77.47 97.67.77.47.7 67.77.47.7.47 77.47.7.47.87 + 47.7.47.87.37 7.47.87.37.87 47.87.37.87.77 87.37.87.77.7 + 37.87.77.7.57 87.77.7.57.47 77.7.57.47.47 7.57.47.47.37 + 57.47.47.37.27 47.47.37.27.17 47.37.27.17.7 38 38.68 38.68.78 + 38.68.78.8 38.68.78.8.28 68.78.8.28.98 78.8.28.98.78 8.28.98.78.58 + 28.98.78.58.98 98.78.58.98.8 78.58.98.8.88 58.98.8.88.8 + 98.8.88.8.58 8.88.8.58.58 88.8.58.58.58 8.58.58.58.38 89 89.59 + 89.59.39 89.59.39.99 89.59.39.99.29 59.39.99.29.59 39.99.29.59.89 + 99.29.59.89.89 29.59.89.89.29 59.89.89.29.9 89.89.29.9.79 + 89.29.9.79.49 29.9.79.49.59 9.79.49.59.29 79.49.59.29.59 + 49.59.29.59.19 59.29.59.19.39 29.59.19.39.9 59.19.39.9.9 + 19.39.9.9.99 39.9.9.99.69 9.9.99.69.39} + +do_execsql_test 1.8.14.3 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2 +} {1 1.1 1.1.2 1.1.2.2 1.1.2.2.3 1.2.2.3.3 2.2.3.3.4 2.3.3.4.5 + 3.3.4.5.6 3.4.5.6.7 4.5.6.7.7 5.6.7.7.7 6.7.7.7.8 7.7.7.8.8 + 7.7.8.8.8 7.8.8.8.9 8.8.8.9.9 8.8.9.9.9 8.9.9.9.10 9.9.9.10.11 + 9.9.10.11.11 9.10.11.11.12 10.11.11.12.12 11.11.12.12.12 + 11.12.12.12.13 12.12.12.13.13 12.12.13.13.14 12.13.13.14.15 + 13.13.14.15.15 13.14.15.15.15 14.15.15.15.16 15.15.15.16.16 + 15.15.16.16.16 15.16.16.16.17 16.16.16.17.19 16.16.17.19.20 + 16.17.19.20.21 17.19.20.21.21 19.20.21.21.22 20.21.21.22.22 + 21.21.22.22.23 21.22.22.23.23 22.22.23.23.24 22.23.23.24.25 + 23.23.24.25.26 23.24.25.26.26 24.25.26.26.27 25.26.26.27.27 + 26.26.27.27.28 26.27.27.28.29 27.27.28.29.29 27.28.29.29.29 + 28.29.29.29.30 29.29.29.30.30 29.29.30.30.30 29.30.30.30.31 + 30.30.30.31.31 30.30.31.31.32 30.31.31.32.33 31.31.32.33.33 + 31.32.33.33.33 32.33.33.33.33 33.33.33.33.34 33.33.33.34.34 + 33.33.34.34.34 33.34.34.34.34 34.34.34.34.35 34.34.34.35.35 + 34.34.35.35.36 34.35.35.36.36 35.35.36.36.36 35.36.36.36.36 + 36.36.36.36.37 36.36.36.37.37 36.36.37.37.38 36.37.37.38.38 + 37.37.38.38.39 37.38.38.39.39 38.38.39.39.39 38.39.39.39.40 + 39.39.39.40.41 39.39.40.41.41 39.40.41.41.42 40.41.41.42.43 + 41.41.42.43.43 41.42.43.43.44 42.43.43.44.44 43.43.44.44.46 + 43.44.44.46.46 44.44.46.46.47 44.46.46.47.47 46.46.47.47.47 + 46.47.47.47.47 47.47.47.47.49 47.47.47.49.50 47.47.49.50.51 + 47.49.50.51.52 49.50.51.52.53 50.51.52.53.54 51.52.53.54.55 + 52.53.54.55.55 53.54.55.55.56 54.55.55.56.56 55.55.56.56.56 + 55.56.56.56.57 56.56.56.57.58 56.56.57.58.58 56.57.58.58.58 + 57.58.58.58.58 58.58.58.58.59 58.58.58.59.59 58.58.59.59.59 + 58.59.59.59.59 59.59.59.59.60 59.59.59.60.61 59.59.60.61.61 + 59.60.61.61.62 60.61.61.62.62 61.61.62.62.63 61.62.62.63.64 + 62.62.63.64.65 62.63.64.65.65 63.64.65.65.65 64.65.65.65.66 + 65.65.65.66.67 65.65.66.67.68 65.66.67.68.69 66.67.68.69.70 + 67.68.69.70.72 68.69.70.72.72 69.70.72.72.72 70.72.72.72.73 + 72.72.72.73.73 72.72.73.73.73 72.73.73.73.74 73.73.73.74.74 + 73.73.74.74.74 73.74.74.74.75 74.74.74.75.75 74.74.75.75.75 + 74.75.75.75.76 75.75.75.76.77 75.75.76.77.77 75.76.77.77.78 + 76.77.77.78.78 77.77.78.78.79 77.78.78.79.80 78.78.79.80.80 + 78.79.80.80.81 79.80.80.81.81 80.80.81.81.81 80.81.81.81.82 + 81.81.81.82.83 81.81.82.83.84 81.82.83.84.84 82.83.84.84.84 + 83.84.84.84.84 84.84.84.84.85 84.84.84.85.85 84.84.85.85.85 + 84.85.85.85.86 85.85.85.86.87 85.85.86.87.87 85.86.87.87.88 + 86.87.87.88.89 87.87.88.89.89 87.88.89.89.89 88.89.89.89.90 + 89.89.89.90.90 89.89.90.90.90 89.90.90.90.91 90.90.90.91.91 + 90.90.91.91.91 90.91.91.91.91 91.91.91.91.91 91.91.91.91.93 + 91.91.91.93.93 91.91.93.93.93 91.93.93.93.94 93.93.93.94.95 + 93.93.94.95.95 93.94.95.95.95 94.95.95.95.96 95.95.95.96.96 + 95.95.96.96.96 95.96.96.96.97 96.96.96.97.97 96.96.97.97.98 + 96.97.97.98.98 97.97.98.98.99 97.98.98.99.99} + +do_execsql_test 1.8.14.4 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2 +} {10 10.20 10.20.30 10.20.30.30 10.20.30.30.30 20.30.30.30.40 + 30.30.30.40.50 30.30.40.50.60 30.40.50.60.70 40.50.60.70.80 + 50.60.70.80.80 60.70.80.80.90 70.80.80.90.90 80.80.90.90.90 1 1.1 + 1.1.11 1.1.11.11 1.1.11.11.21 1.11.11.21.21 11.11.21.21.31 + 11.21.21.31.31 21.21.31.31.41 21.31.31.41.41 31.31.41.41.51 + 31.41.41.51.61 41.41.51.61.61 41.51.61.61.81 51.61.61.81.81 + 61.61.81.81.81 61.81.81.81.91 81.81.81.91.91 81.81.91.91.91 + 81.91.91.91.91 91.91.91.91.91 2 2.2 2.2.12 2.2.12.12 + 2.2.12.12.12 2.12.12.12.22 12.12.12.22.22 12.12.22.22.32 + 12.22.22.32.42 22.22.32.42.52 22.32.42.52.62 32.42.52.62.62 + 42.52.62.62.72 52.62.62.72.72 62.62.72.72.72 62.72.72.72.82 3 3.3 + 3.3.13 3.3.13.13 3.3.13.13.23 3.13.13.23.23 13.13.23.23.33 + 13.23.23.33.33 23.23.33.33.33 23.33.33.33.33 33.33.33.33.43 + 33.33.33.43.43 33.33.43.43.53 33.43.43.53.63 43.43.53.63.73 + 43.53.63.73.73 53.63.73.73.73 63.73.73.73.83 73.73.73.83.93 + 73.73.83.93.93 73.83.93.93.93 4 4.14 4.14.24 4.14.24.34 + 4.14.24.34.34 14.24.34.34.34 24.34.34.34.34 34.34.34.34.44 + 34.34.34.44.44 34.34.44.44.54 34.44.44.54.64 44.44.54.64.74 + 44.54.64.74.74 54.64.74.74.74 64.74.74.74.84 74.74.74.84.84 + 74.74.84.84.84 74.84.84.84.84 84.84.84.84.94 5 5.15 5.15.15 + 5.15.15.15 5.15.15.15.25 15.15.15.25.35 15.15.25.35.35 + 15.25.35.35.55 25.35.35.55.55 35.35.55.55.65 35.55.55.65.65 + 55.55.65.65.65 55.65.65.65.75 65.65.65.75.75 65.65.75.75.75 + 65.75.75.75.85 75.75.75.85.85 75.75.85.85.85 75.85.85.85.95 + 85.85.85.95.95 85.85.95.95.95 6 6.16 6.16.16 6.16.16.16 + 6.16.16.16.26 16.16.16.26.26 16.16.26.26.36 16.26.26.36.36 + 26.26.36.36.36 26.36.36.36.36 36.36.36.36.46 36.36.36.46.46 + 36.36.46.46.56 36.46.46.56.56 46.46.56.56.56 46.56.56.56.66 + 56.56.56.66.76 56.56.66.76.86 56.66.76.86.96 66.76.86.96.96 + 76.86.96.96.96 7 7.7 7.7.7 7.7.7.17 7.7.7.17.27 7.7.17.27.27 + 7.17.27.27.37 17.27.27.37.37 27.27.37.37.47 27.37.37.47.47 + 37.37.47.47.47 37.47.47.47.47 47.47.47.47.57 47.47.47.57.67 + 47.47.57.67.77 47.57.67.77.77 57.67.77.77.87 67.77.77.87.87 + 77.77.87.87.97 77.87.87.97.97 8 8.8 8.8.8 8.8.8.28 8.8.8.28.38 + 8.8.28.38.38 8.28.38.38.58 28.38.38.58.58 38.38.58.58.58 + 38.58.58.58.58 58.58.58.58.68 58.58.58.68.78 58.58.68.78.78 + 58.68.78.78.88 68.78.78.88.98 78.78.88.98.98 9 9.9 9.9.9 + 9.9.9.19 9.9.9.19.29 9.9.19.29.29 9.19.29.29.29 19.29.29.29.39 + 29.29.29.39.39 29.29.39.39.39 29.39.39.39.49 39.39.39.49.59 + 39.39.49.59.59 39.49.59.59.59 49.59.59.59.59 59.59.59.59.69 + 59.59.59.69.79 59.59.69.79.89 59.69.79.89.89 69.79.89.89.89 + 79.89.89.89.99 89.89.89.99.99} + +do_execsql_test 1.8.14.5 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( ORDER BY b%10,a ROWS BETWEEN 4 PRECEDING AND CURRENT ROW ) FROM t2 +} {90 90.40 90.40.30 90.40.30.80 90.40.30.80.20 40.30.80.20.90 + 30.80.20.90.60 80.20.90.60.70 20.90.60.70.80 90.60.70.80.90 + 60.70.80.90.30 70.80.90.30.50 80.90.30.50.10 90.30.50.10.30 + 30.50.10.30.81 50.10.30.81.91 10.30.81.91.61 30.81.91.61.91 + 81.91.61.91.91 91.61.91.91.1 61.91.91.1.81 91.91.1.81.41 + 91.1.81.41.61 1.81.41.61.1 81.41.61.1.21 41.61.1.21.11 + 61.1.21.11.51 1.21.11.51.41 21.11.51.41.31 11.51.41.31.31 + 51.41.31.31.11 41.31.31.11.81 31.31.11.81.91 31.11.81.91.91 + 11.81.91.91.21 81.91.91.21.62 91.91.21.62.12 91.21.62.12.32 + 21.62.12.32.22 62.12.32.22.42 12.32.22.42.2 32.22.42.2.72 + 22.42.2.72.12 42.2.72.12.22 2.72.12.22.2 72.12.22.2.72 + 12.22.2.72.72 22.2.72.72.12 2.72.72.12.62 72.72.12.62.52 + 72.12.62.52.82 12.62.52.82.93 62.52.82.93.23 52.82.93.23.93 + 82.93.23.93.43 93.23.93.43.3 23.93.43.3.43 93.43.3.43.33 + 43.3.43.33.53 3.43.33.53.63 43.33.53.63.73 33.53.63.73.13 + 53.63.73.13.73 63.73.13.73.73 73.13.73.73.33 13.73.73.33.93 + 73.73.33.93.23 73.33.93.23.13 33.93.23.13.33 93.23.13.33.3 + 23.13.33.3.33 13.33.3.33.83 33.3.33.83.54 3.33.83.54.84 + 33.83.54.84.74 83.54.84.74.24 54.84.74.24.4 84.74.24.4.94 + 74.24.4.94.84 24.4.94.84.74 4.94.84.74.34 94.84.74.34.34 + 84.74.34.34.44 74.34.34.44.74 34.34.44.74.64 34.44.74.64.14 + 44.74.64.14.34 74.64.14.34.84 64.14.34.84.84 14.34.84.84.44 + 34.84.84.44.34 84.84.44.34.65 84.44.34.65.35 44.34.65.35.85 + 34.65.35.85.85 65.35.85.85.55 35.85.85.55.15 85.85.55.15.25 + 85.55.15.25.75 55.15.25.75.95 15.25.75.95.65 25.75.95.65.65 + 75.95.65.65.35 95.65.65.35.5 65.65.35.5.15 65.35.5.15.95 + 35.5.15.95.55 5.15.95.55.75 15.95.55.75.85 95.55.75.85.75 + 55.75.85.75.15 75.85.75.15.95 85.75.15.95.96 75.15.95.96.46 + 15.95.96.46.6 95.96.46.6.46 96.46.6.46.16 46.6.46.16.16 + 6.46.16.16.86 46.16.16.86.56 16.16.86.56.56 16.86.56.56.56 + 86.56.56.56.16 56.56.56.16.36 56.56.16.36.76 56.16.36.76.96 + 16.36.76.96.96 36.76.96.96.26 76.96.96.26.26 96.96.26.26.36 + 96.26.26.36.66 26.26.36.66.36 26.36.66.36.36 36.66.36.36.97 + 66.36.36.97.27 36.36.97.27.97 36.97.27.97.67 97.27.97.67.77 + 27.97.67.77.47 97.67.77.47.7 67.77.47.7.47 77.47.7.47.87 + 47.7.47.87.37 7.47.87.37.87 47.87.37.87.77 87.37.87.77.7 + 37.87.77.7.57 87.77.7.57.47 77.7.57.47.47 7.57.47.47.37 + 57.47.47.37.27 47.47.37.27.17 47.37.27.17.7 37.27.17.7.38 + 27.17.7.38.68 17.7.38.68.78 7.38.68.78.8 38.68.78.8.28 + 68.78.8.28.98 78.8.28.98.78 8.28.98.78.58 28.98.78.58.98 + 98.78.58.98.8 78.58.98.8.88 58.98.8.88.8 98.8.88.8.58 8.88.8.58.58 + 88.8.58.58.58 8.58.58.58.38 58.58.58.38.89 58.58.38.89.59 + 58.38.89.59.39 38.89.59.39.99 89.59.39.99.29 59.39.99.29.59 + 39.99.29.59.89 99.29.59.89.89 29.59.89.89.29 59.89.89.29.9 + 89.89.29.9.79 89.29.9.79.49 29.9.79.49.59 9.79.49.59.29 + 79.49.59.29.59 49.59.29.59.19 59.29.59.19.39 29.59.19.39.9 + 59.19.39.9.9 19.39.9.9.99 39.9.9.99.69 9.9.99.69.39} + +do_execsql_test 1.8.14.6 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND CURRENT ROW) FROM t2 +} {96 38 68 62 46 6 46 78 54 8 84 16 16 86 74 24 + 12 32 56 22 90 28 56 40 56 16 36 98 76 4 94 + 42 30 78 2 80 84 72 58 96 98 74 12 8 20 22 88 + 34 8 34 90 96 60 44 2 74 70 26 26 80 90 36 58 + 72 72 66 64 12 14 62 36 34 36 58 52 30 50 84 + 10 84 44 58 30 38 34 82 89 81 59 39 91 99 97 + 27 97 67 29 93 77 23 93 65 35 47 7 61 91 85 + 85 43 59 3 91 55 15 89 25 47 1 43 75 89 81 33 + 29 53 63 87 37 41 9 61 73 95 65 13 1 21 65 35 + 5 73 11 51 87 41 31 31 15 95 73 79 11 49 59 + 55 75 77 7 85 57 29 59 19 39 47 47 9 33 93 75 + 81 9 23 37 13 91 91 33 15 99 3 95 69 33 21 39 + 83 27 17 7} + +do_execsql_test 1.8.14.7 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (win1 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND CURRENT ROW) + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a) + ORDER BY 1 +} {1 1 10 11 11 12 12 12 13 13 14 15 15 15 16 16 + 16 17 19 2 2 20 21 21 22 22 23 23 24 25 26 26 + 27 27 28 29 29 29 3 3 30 30 30 31 31 32 33 33 + 33 33 34 34 34 34 35 35 36 36 36 36 37 37 38 + 38 39 39 39 4 40 41 41 42 43 43 44 44 46 46 + 47 47 47 47 49 5 50 51 52 53 54 55 55 56 56 + 56 57 58 58 58 58 59 59 59 59 6 60 61 61 62 + 62 63 64 65 65 65 66 67 68 69 7 7 7 70 72 72 + 72 73 73 73 74 74 74 75 75 75 76 77 77 78 78 + 79 8 8 8 80 80 81 81 81 82 83 84 84 84 84 85 + 85 85 86 87 87 88 89 89 89 9 9 9 90 90 90 91 + 91 91 91 91 93 93 93 94 95 95 95 96 96 96 97 + 97 98 98 99 99} + +do_execsql_test 1.8.14.8 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (win1 ROWS BETWEEN 4 PRECEDING AND CURRENT ROW) + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a ORDER BY b%10) + ORDER BY 1 +} {1 1 10 11 11 12 12 12 13 13 14 15 15 15 16 16 + 16 17 19 2 2 20 21 21 22 22 23 23 24 25 26 26 + 27 27 28 29 29 29 3 3 30 30 30 31 31 32 33 33 + 33 33 34 34 34 34 35 35 36 36 36 36 37 37 38 + 38 39 39 39 4 40 41 41 42 43 43 44 44 46 46 + 47 47 47 47 49 5 50 51 52 53 54 55 55 56 56 + 56 57 58 58 58 58 59 59 59 59 6 60 61 61 62 + 62 63 64 65 65 65 66 67 68 69 7 7 7 70 72 72 + 72 73 73 73 74 74 74 75 75 75 76 77 77 78 78 + 79 8 8 8 80 80 81 81 81 82 83 84 84 84 84 85 + 85 85 86 87 87 88 89 89 89 9 9 9 90 90 90 91 + 91 91 91 91 93 93 93 94 95 95 95 96 96 96 97 + 97 98 98 99 99} + +do_execsql_test 1.8.14.9 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER win2 + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a ORDER BY b%10), + win2 AS (win1 ROWS BETWEEN 4 PRECEDING AND CURRENT ROW) + ORDER BY 1 +} {1 1 10 11 11 12 12 12 13 13 14 15 15 15 16 16 + 16 17 19 2 2 20 21 21 22 22 23 23 24 25 26 26 + 27 27 28 29 29 29 3 3 30 30 30 31 31 32 33 33 + 33 33 34 34 34 34 35 35 36 36 36 36 37 37 38 + 38 39 39 39 4 40 41 41 42 43 43 44 44 46 46 + 47 47 47 47 49 5 50 51 52 53 54 55 55 56 56 + 56 57 58 58 58 58 59 59 59 59 6 60 61 61 62 + 62 63 64 65 65 65 66 67 68 69 7 7 7 70 72 72 + 72 73 73 73 74 74 74 75 75 75 76 77 77 78 78 + 79 8 8 8 80 80 81 81 81 82 83 84 84 84 84 85 + 85 85 86 87 87 88 89 89 89 9 9 9 90 90 90 91 + 91 91 91 91 93 93 93 94 95 95 95 96 96 96 97 + 97 98 98 99 99} + +do_execsql_test 1.8.15.1 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE a%2=0) OVER win FROM t2 + WINDOW win AS (ORDER BY a ROWS BETWEEN 4 PRECEDING AND CURRENT ROW) +} {1 89 2 89 3 89.96 4 89.96 5 89.96.38 5 96.38 5 96.38.39 + 5 38.39 5 38.39.91 5 39.91 5 39.91.6 5 91.6 5 91.6.97 5 6.97 + 5 6.97.46 5 97.46 5 97.46.54 5 46.54 5 46.54.8 5 54.8 5 54.8.29 + 5 8.29 5 8.29.84 5 29.84 5 29.84.23 5 84.23 5 84.23.16 5 23.16 + 5 23.16.65 5 16.65 5 16.65.47 5 65.47 5 65.47.86 5 47.86 + 5 47.86.61 5 86.61 5 86.61.85 5 61.85 5 61.85.85 5 85.85 + 5 85.85.59 5 85.59 5 85.59.32 5 59.32 5 59.32.3 5 32.3 + 5 32.3.22 5 3.22 5 3.22.55 5 22.55 5 22.55.28 5 55.28 + 5 55.28.25 5 28.25 5 28.25.1 5 25.1 5 25.1.40 5 1.40 5 1.40.56 + 5 40.56 5 40.56.75 5 56.75 5 56.75.89 5 75.89 5 75.89.76 + 5 89.76 5 89.76.4 5 76.4 5 76.4.42 5 4.42 5 4.42.78 5 42.78 + 5 42.78.29 5 78.29 5 78.29.63 5 29.63 5 29.63.87 5 63.87 + 5 63.87.80 5 87.80 5 87.80.72 5 80.72 5 80.72.9 5 72.9 + 5 72.9.73 5 9.73 5 9.73.65 5 73.65 5 73.65.58 5 65.58 + 5 65.58.98 5 58.98 5 58.98.21 5 98.21 5 98.21.65 5 21.65 + 5 21.65.5 5 65.5 5 65.5.11 5 5.11 5 5.11.87 5 11.87 5 11.87.12 + 5 87.12 5 87.12.20 5 12.20 5 12.20.31 5 20.31 5 20.31.95 + 5 31.95 5 31.95.73 5 95.73 5 95.73.88 5 73.88 5 73.88.8 5 88.8 + 5 88.8.49 5 8.49 5 8.49.90 5 49.90 5 49.90.96 5 90.96 + 5 90.96.55 5 96.55 5 96.55.77 5 55.77 5 55.77.2 5 77.2 + 5 77.2.85 5 2.85 5 2.85.74 5 85.74 5 85.74.70 5 74.70 + 5 74.70.19 5 70.19 5 70.19.26 5 19.26 5 19.26.47 5 26.47 + 5 26.47.90 5 47.90 5 47.90.58 5 90.58 5 90.58.9 5 58.9 + 5 58.9.72 5 9.72 5 9.72.33 5 72.33 5 72.33.75 5 33.75 + 5 33.75.81 5 75.81 5 75.81.23 5 81.23 5 81.23.13 5 23.13 + 5 23.13.14 5 13.14 5 13.14.91 5 14.91 5 14.91.91 5 91.91 + 5 91.91.15 5 91.15 5 91.15.36 5 15.36 5 15.36.3 5 36.3 + 5 36.3.69 5 3.69 5 3.69.52 5 69.52 5 69.52.50 5 52.50 + 5 52.50.10 5 50.10 5 50.10.33 5 10.33 5 10.33.39 5 33.39 + 5 33.39.58 5 39.58 5 39.58.38 5 58.38 5 58.38.83 5 38.83 + 5 38.83.82 5 83.82 5 83.82.7} + +do_execsql_test 1.8.15.2 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE 0=1) OVER win FROM t2 + WINDOW win AS (ORDER BY a ROWS BETWEEN 4 PRECEDING AND CURRENT ROW) +} {1 {} 2 {} 3 {} 4 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {}} + +do_execsql_test 1.8.15.3 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE 1=0) OVER win FROM t2 + WINDOW win AS (PARTITION BY (a%10) ORDER BY a ROWS BETWEEN 4 PRECEDING AND CURRENT ROW) +} {1 {} 2 {} 3 {} 4 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 1 {} 2 {} + 3 {} 4 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 1 {} 2 {} 3 {} 4 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 1 {} 2 {} 3 {} 4 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 1 {} 2 {} 3 {} 4 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 1 {} 2 {} 3 {} + 4 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 1 {} 2 {} 3 {} 4 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 1 {} 2 {} 3 {} 4 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 1 {} + 2 {} 3 {} 4 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 1 {} 2 {} 3 {} 4 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {}} + +do_execsql_test 1.8.15.4 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE a%2=0) OVER win FROM t2 + WINDOW win AS (PARTITION BY (a%10) ORDER BY a ROWS BETWEEN 4 PRECEDING AND CURRENT ROW) +} {1 89 2 89.6 3 89.6.29 4 89.6.29.47 5 89.6.29.47.59 + 5 6.29.47.59.28 5 29.47.59.28.75 5 47.59.28.75.78 5 59.28.75.78.72 + 5 28.75.78.72.98 5 75.78.72.98.87 5 78.72.98.87.73 5 72.98.87.73.96 + 5 98.87.73.96.74 5 87.73.96.74.90 5 73.96.74.90.75 5 96.74.90.75.91 + 5 74.90.75.91.69 5 90.75.91.69.39 5 75.91.69.39.7 1 {} 2 {} 3 {} + 4 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 1 96 2 96.97 3 96.97.84 + 4 96.97.84.86 5 96.97.84.86.32 5 97.84.86.32.25 5 84.86.32.25.89 + 5 86.32.25.89.29 5 32.25.89.29.9 5 25.89.29.9.21 5 89.29.9.21.12 + 5 29.9.21.12.88 5 9.21.12.88.55 5 21.12.88.55.70 5 12.88.55.70.58 + 5 88.55.70.58.81 5 55.70.58.81.91 5 70.58.81.91.52 5 58.81.91.52.58 + 1 {} 2 {} 3 {} 4 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 1 38 2 38.46 + 3 38.46.23 4 38.46.23.61 5 38.46.23.61.3 5 46.23.61.3.1 + 5 23.61.3.1.76 5 61.3.1.76.63 5 3.1.76.63.73 5 1.76.63.73.65 + 5 76.63.73.65.20 5 63.73.65.20.8 5 73.65.20.8.77 5 65.20.8.77.19 + 5 20.8.77.19.9 5 8.77.19.9.23 5 77.19.9.23.15 5 19.9.23.15.50 + 5 9.23.15.50.38 1 {} 2 {} 3 {} 4 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 1 39 2 39.54 3 39.54.16 4 39.54.16.85 5 39.54.16.85.22 + 5 54.16.85.22.40 5 16.85.22.40.4 5 85.22.40.4.87 5 22.40.4.87.65 + 5 40.4.87.65.5 5 4.87.65.5.31 5 87.65.5.31.49 5 65.5.31.49.2 + 5 5.31.49.2.26 5 31.49.2.26.72 5 49.2.26.72.13 5 2.26.72.13.36 + 5 26.72.13.36.10 5 72.13.36.10.83 1 {} 2 {} 3 {} 4 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 1 91 2 91.8 3 91.8.65 4 91.8.65.85 + 5 91.8.65.85.55 5 8.65.85.55.56 5 65.85.55.56.42 5 85.55.56.42.80 + 5 55.56.42.80.58 5 56.42.80.58.11 5 42.80.58.11.95 5 80.58.11.95.90 + 5 58.11.95.90.85 5 11.95.90.85.47 5 95.90.85.47.33 5 90.85.47.33.14 + 5 85.47.33.14.3 5 47.33.14.3.33 5 33.14.3.33.82 1 {} 2 {} 3 {} + 4 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {}} + +do_execsql_test 1.9.2.1 { + SELECT max(b) OVER ( ORDER BY a ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {89 81 96 59 38 68 39 62 91 46 6 99 97 27 46 + 78 54 97 8 67 29 93 84 77 23 16 16 93 65 35 + 47 7 86 74 61 91 85 24 85 43 59 12 32 56 3 91 + 22 90 55 15 28 89 25 47 1 56 40 43 56 16 75 + 36 89 98 76 81 4 94 42 30 78 33 29 53 63 2 87 + 37 80 84 72 41 9 61 73 95 65 13 58 96 98 1 21 + 74 65 35 5 73 11 51 87 41 12 8 20 31 31 15 95 + 22 73 79 88 34 8 11 49 34 90 59 96 60 55 75 + 77 44 2 7 85 57 74 29 70 59 19 39 26 26 47 80 + 90 36 58 47 9 72 72 66 33 93 75 64 81 9 23 37 + 13 12 14 62 91 36 91 33 15 34 36 99 3 95 69 + 58 52 30 50 84 10 84 33 21 39 44 58 30 38 34 + 83 27 82 17 7} + +do_execsql_test 1.9.2.2 { + SELECT min(b) OVER ( ORDER BY a ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {89 81 96 59 38 68 39 62 91 46 6 99 97 27 46 + 78 54 97 8 67 29 93 84 77 23 16 16 93 65 35 + 47 7 86 74 61 91 85 24 85 43 59 12 32 56 3 91 + 22 90 55 15 28 89 25 47 1 56 40 43 56 16 75 + 36 89 98 76 81 4 94 42 30 78 33 29 53 63 2 87 + 37 80 84 72 41 9 61 73 95 65 13 58 96 98 1 21 + 74 65 35 5 73 11 51 87 41 12 8 20 31 31 15 95 + 22 73 79 88 34 8 11 49 34 90 59 96 60 55 75 + 77 44 2 7 85 57 74 29 70 59 19 39 26 26 47 80 + 90 36 58 47 9 72 72 66 33 93 75 64 81 9 23 37 + 13 12 14 62 91 36 91 33 15 34 36 99 3 95 69 + 58 52 30 50 84 10 84 33 21 39 44 58 30 38 34 + 83 27 82 17 7} + +do_execsql_test 1.9.3.1 { + SELECT row_number() OVER ( ORDER BY a ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.9.3.2 { + SELECT row_number() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.9.3.3 { + SELECT row_number() OVER ( ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.9.4.1 { + SELECT dense_rank() OVER ( ORDER BY a ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.9.4.2 { + SELECT dense_rank() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.9.4.3 { + SELECT dense_rank() OVER ( ORDER BY b ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {1 1 2 2 3 3 4 5 6 7 7 7 8 8 8 9 9 9 10 + 11 11 12 12 12 13 13 14 15 15 15 16 16 16 17 + 18 19 20 20 21 21 22 22 23 24 25 25 26 26 27 + 28 28 28 29 29 29 30 30 31 32 32 32 32 33 33 + 33 33 34 34 35 35 35 35 36 36 37 37 38 38 38 + 39 40 40 41 42 42 43 43 44 44 45 45 45 45 46 + 47 48 49 50 51 52 52 53 53 53 54 55 55 55 55 + 56 56 56 56 57 58 58 59 59 60 61 62 62 62 63 + 64 65 66 67 68 68 68 69 69 69 70 70 70 71 71 + 71 72 73 73 74 74 75 76 76 77 77 77 78 79 80 + 80 80 80 81 81 81 82 83 83 84 85 85 85 86 86 + 86 87 87 87 87 87 88 88 88 89 90 90 90 91 91 + 91 92 92 93 93 94 94} + +do_execsql_test 1.9.4.4 { + SELECT dense_rank() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {1 2 3 3 3 4 5 6 7 8 8 9 9 9 1 1 2 2 3 + 3 4 4 5 5 6 7 7 8 8 8 9 9 9 9 9 1 1 2 + 2 2 3 3 4 5 6 7 7 8 8 8 9 1 1 2 2 3 3 + 4 4 4 4 5 5 6 7 8 8 8 9 10 10 10 1 2 3 + 4 4 4 4 5 5 6 7 8 8 8 9 9 9 9 10 1 2 2 + 2 3 4 4 5 5 6 6 6 7 7 7 8 8 8 9 9 9 1 + 2 2 2 3 3 4 4 4 4 5 5 6 6 6 7 8 9 10 10 + 10 1 1 1 2 3 3 4 4 5 5 5 5 6 7 8 8 9 9 + 10 10 1 1 1 2 3 3 4 4 4 4 5 6 6 7 8 8 1 + 1 1 2 3 3 3 4 4 4 5 6 6 6 6 7 8 9 9 9 + 10 10} + +do_execsql_test 1.9.4.5 { + SELECT dense_rank() OVER ( ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 + 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 + 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 + 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 + 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 + 8 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 10 10 + 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 + 10 10 10 10 10} + +do_execsql_test 1.9.4.6 { + SELECT dense_rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 + 5 5 5 5 5 5 5 5 5 5 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 + 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 + 5} + +do_execsql_test 1.9.5.1 { + SELECT rank() OVER ( ORDER BY a ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.9.5.2 { + SELECT rank() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.9.5.3 { + SELECT rank() OVER ( ORDER BY b ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {1 1 3 3 5 5 7 8 9 10 10 10 13 13 13 16 16 + 16 19 20 20 22 22 22 25 25 27 28 28 28 31 31 + 31 34 35 36 37 37 39 39 41 41 43 44 45 45 47 + 47 49 50 50 50 53 53 53 56 56 58 59 59 59 59 + 63 63 63 63 67 67 69 69 69 69 73 73 75 75 77 + 77 77 80 81 81 83 84 84 86 86 88 88 90 90 90 + 90 94 95 96 97 98 99 100 100 102 102 102 105 106 + 106 106 106 110 110 110 110 114 115 115 117 117 119 + 120 121 121 121 124 125 126 127 128 129 129 129 132 + 132 132 135 135 135 138 138 138 141 142 142 144 144 + 146 147 147 149 149 149 152 153 154 154 154 154 158 + 158 158 161 162 162 164 165 165 165 168 168 168 171 + 171 171 171 171 176 176 176 179 180 180 180 183 183 + 183 186 186 188 188 190 190} + +do_execsql_test 1.9.5.4 { + SELECT rank() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {1 2 3 3 3 6 7 8 9 10 10 12 12 12 1 1 3 3 + 5 5 7 7 9 9 11 12 12 14 14 14 17 17 17 17 + 17 1 1 3 3 3 6 6 8 9 10 11 11 13 13 13 16 + 1 1 3 3 5 5 7 7 7 7 11 11 13 14 15 15 15 + 18 19 19 19 1 2 3 4 4 4 4 8 8 10 11 12 12 + 12 15 15 15 15 19 1 2 2 2 5 6 6 8 8 10 10 + 10 13 13 13 16 16 16 19 19 19 1 2 2 2 5 5 7 + 7 7 7 11 11 13 13 13 16 17 18 19 19 19 1 1 + 1 4 5 5 7 7 9 9 9 9 13 14 15 15 17 17 19 + 19 1 1 1 4 5 5 7 7 7 7 11 12 12 14 15 15 + 1 1 1 4 5 5 5 8 8 8 11 12 12 12 12 16 17 + 18 18 18 21 21} + +do_execsql_test 1.9.5.5 { + SELECT rank() OVER ( ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 15 15 15 15 + 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 + 15 15 36 36 36 36 36 36 36 36 36 36 36 36 36 + 36 36 36 52 52 52 52 52 52 52 52 52 52 52 52 + 52 52 52 52 52 52 52 52 52 73 73 73 73 73 73 + 73 73 73 73 73 73 73 73 73 73 73 73 73 92 92 + 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 + 92 92 92 92 113 113 113 113 113 113 113 113 113 + 113 113 113 113 113 113 113 113 113 113 113 113 134 + 134 134 134 134 134 134 134 134 134 134 134 134 134 + 134 134 134 134 134 134 154 154 154 154 154 154 154 + 154 154 154 154 154 154 154 154 154 170 170 170 170 + 170 170 170 170 170 170 170 170 170 170 170 170 170 + 170 170 170 170 170} + +do_execsql_test 1.9.5.6 { + SELECT rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 15 15 15 15 + 15 15 15 15 15 15 15 15 15 15 15 15 31 31 31 + 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 + 31 50 50 50 50 50 50 50 50 50 50 50 50 50 50 + 50 50 50 50 50 50 50 71 71 71 71 71 71 71 71 + 71 71 71 71 71 71 71 71 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 22 22 22 22 22 22 + 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 + 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 + 43 43 43 43 43 43 64 64 64 64 64 64 64 64 64 + 64 64 64 64 64 64 64 64 64 64 64 84 84 84 84 + 84 84 84 84 84 84 84 84 84 84 84 84 84 84 84 + 84 84 84} + +do_execsql_test 1.9.6.1 { + SELECT + row_number() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND CURRENT ROW ), + rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND CURRENT ROW ), + dense_rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) + FROM t2 +} {1 1 1 2 1 1 3 1 1 4 1 1 5 1 1 6 1 1 7 1 1 8 1 1 9 1 1 + 10 1 1 11 1 1 12 1 1 13 1 1 14 1 1 15 15 2 16 15 2 17 15 2 + 18 15 2 19 15 2 20 15 2 21 15 2 22 15 2 23 15 2 24 15 2 + 25 15 2 26 15 2 27 15 2 28 15 2 29 15 2 30 15 2 31 31 3 + 32 31 3 33 31 3 34 31 3 35 31 3 36 31 3 37 31 3 38 31 3 + 39 31 3 40 31 3 41 31 3 42 31 3 43 31 3 44 31 3 45 31 3 + 46 31 3 47 31 3 48 31 3 49 31 3 50 50 4 51 50 4 52 50 4 + 53 50 4 54 50 4 55 50 4 56 50 4 57 50 4 58 50 4 59 50 4 + 60 50 4 61 50 4 62 50 4 63 50 4 64 50 4 65 50 4 66 50 4 + 67 50 4 68 50 4 69 50 4 70 50 4 71 71 5 72 71 5 73 71 5 + 74 71 5 75 71 5 76 71 5 77 71 5 78 71 5 79 71 5 80 71 5 + 81 71 5 82 71 5 83 71 5 84 71 5 85 71 5 86 71 5 1 1 1 2 1 1 + 3 1 1 4 1 1 5 1 1 6 1 1 7 1 1 8 1 1 9 1 1 10 1 1 11 1 1 + 12 1 1 13 1 1 14 1 1 15 1 1 16 1 1 17 1 1 18 1 1 19 1 1 + 20 1 1 21 1 1 22 22 2 23 22 2 24 22 2 25 22 2 26 22 2 27 22 2 + 28 22 2 29 22 2 30 22 2 31 22 2 32 22 2 33 22 2 34 22 2 + 35 22 2 36 22 2 37 22 2 38 22 2 39 22 2 40 22 2 41 22 2 + 42 22 2 43 43 3 44 43 3 45 43 3 46 43 3 47 43 3 48 43 3 + 49 43 3 50 43 3 51 43 3 52 43 3 53 43 3 54 43 3 55 43 3 + 56 43 3 57 43 3 58 43 3 59 43 3 60 43 3 61 43 3 62 43 3 + 63 43 3 64 64 4 65 64 4 66 64 4 67 64 4 68 64 4 69 64 4 + 70 64 4 71 64 4 72 64 4 73 64 4 74 64 4 75 64 4 76 64 4 + 77 64 4 78 64 4 79 64 4 80 64 4 81 64 4 82 64 4 83 64 4 + 84 84 5 85 84 5 86 84 5 87 84 5 88 84 5 89 84 5 90 84 5 + 91 84 5 92 84 5 93 84 5 94 84 5 95 84 5 96 84 5 97 84 5 + 98 84 5 99 84 5 100 84 5 101 84 5 102 84 5 103 84 5 104 84 5 + 105 84 5} + + +do_test 1.9.7.1 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY a ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0053 0.0105 0.0158 0.0211 0.0263 0.0316 0.0368 0.0421 0.0474 0.0526 0.0579 0.0632 0.0684 0.0737 0.0789 0.0842 0.0895 0.0947 0.1000 0.1053 0.1105 0.1158 0.1211 0.1263 0.1316 0.1368 0.1421 0.1474 0.1526 0.1579 0.1632 0.1684 0.1737 0.1789 0.1842 0.1895 0.1947 0.2000 0.2053 0.2105 0.2158 0.2211 0.2263 0.2316 0.2368 0.2421 0.2474 0.2526 0.2579 0.2632 0.2684 0.2737 0.2789 0.2842 0.2895 0.2947 0.3000 0.3053 0.3105 0.3158 0.3211 0.3263 0.3316 0.3368 0.3421 0.3474 0.3526 0.3579 0.3632 0.3684 0.3737 0.3789 0.3842 0.3895 0.3947 0.4000 0.4053 0.4105 0.4158 0.4211 0.4263 0.4316 0.4368 0.4421 0.4474 0.4526 0.4579 0.4632 0.4684 0.4737 0.4789 0.4842 0.4895 0.4947 0.5000 0.5053 0.5105 0.5158 0.5211 0.5263 0.5316 0.5368 0.5421 0.5474 0.5526 0.5579 0.5632 0.5684 0.5737 0.5789 0.5842 0.5895 0.5947 0.6000 0.6053 0.6105 0.6158 0.6211 0.6263 0.6316 0.6368 0.6421 0.6474 0.6526 0.6579 0.6632 0.6684 0.6737 0.6789 0.6842 0.6895 0.6947 0.7000 0.7053 0.7105 0.7158 0.7211 0.7263 0.7316 0.7368 0.7421 0.7474 0.7526 0.7579 0.7632 0.7684 0.7737 0.7789 0.7842 0.7895 0.7947 0.8000 0.8053 0.8105 0.8158 0.8211 0.8263 0.8316 0.8368 0.8421 0.8474 0.8526 0.8579 0.8632 0.8684 0.8737 0.8789 0.8842 0.8895 0.8947 0.9000 0.9053 0.9105 0.9158 0.9211 0.9263 0.9316 0.9368 0.9421 0.9474 0.9526 0.9579 0.9632 0.9684 0.9737 0.9789 0.9842 0.9895 0.9947 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.9.7.2 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0769 0.1538 0.2308 0.3077 0.3846 0.4615 0.5385 0.6154 0.6923 0.7692 0.8462 0.9231 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0667 0.1333 0.2000 0.2667 0.3333 0.4000 0.4667 0.5333 0.6000 0.6667 0.7333 0.8000 0.8667 0.9333 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0556 0.1111 0.1667 0.2222 0.2778 0.3333 0.3889 0.4444 0.5000 0.5556 0.6111 0.6667 0.7222 0.7778 0.8333 0.8889 0.9444 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0526 0.1053 0.1579 0.2105 0.2632 0.3158 0.3684 0.4211 0.4737 0.5263 0.5789 0.6316 0.6842 0.7368 0.7895 0.8421 0.8947 0.9474 1.0000 0.0000 0.0667 0.1333 0.2000 0.2667 0.3333 0.4000 0.4667 0.5333 0.6000 0.6667 0.7333 0.8000 0.8667 0.9333 1.0000 0.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.9.7.3 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY b ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0105 0.0105 0.0211 0.0211 0.0316 0.0368 0.0421 0.0474 0.0474 0.0474 0.0632 0.0632 0.0632 0.0789 0.0789 0.0789 0.0947 0.1000 0.1000 0.1105 0.1105 0.1105 0.1263 0.1263 0.1368 0.1421 0.1421 0.1421 0.1579 0.1579 0.1579 0.1737 0.1789 0.1842 0.1895 0.1895 0.2000 0.2000 0.2105 0.2105 0.2211 0.2263 0.2316 0.2316 0.2421 0.2421 0.2526 0.2579 0.2579 0.2579 0.2737 0.2737 0.2737 0.2895 0.2895 0.3000 0.3053 0.3053 0.3053 0.3053 0.3263 0.3263 0.3263 0.3263 0.3474 0.3474 0.3579 0.3579 0.3579 0.3579 0.3789 0.3789 0.3895 0.3895 0.4000 0.4000 0.4000 0.4158 0.4211 0.4211 0.4316 0.4368 0.4368 0.4474 0.4474 0.4579 0.4579 0.4684 0.4684 0.4684 0.4684 0.4895 0.4947 0.5000 0.5053 0.5105 0.5158 0.5211 0.5211 0.5316 0.5316 0.5316 0.5474 0.5526 0.5526 0.5526 0.5526 0.5737 0.5737 0.5737 0.5737 0.5947 0.6000 0.6000 0.6105 0.6105 0.6211 0.6263 0.6316 0.6316 0.6316 0.6474 0.6526 0.6579 0.6632 0.6684 0.6737 0.6737 0.6737 0.6895 0.6895 0.6895 0.7053 0.7053 0.7053 0.7211 0.7211 0.7211 0.7368 0.7421 0.7421 0.7526 0.7526 0.7632 0.7684 0.7684 0.7789 0.7789 0.7789 0.7947 0.8000 0.8053 0.8053 0.8053 0.8053 0.8263 0.8263 0.8263 0.8421 0.8474 0.8474 0.8579 0.8632 0.8632 0.8632 0.8789 0.8789 0.8789 0.8947 0.8947 0.8947 0.8947 0.8947 0.9211 0.9211 0.9211 0.9368 0.9421 0.9421 0.9421 0.9579 0.9579 0.9579 0.9737 0.9737 0.9842 0.9842 0.9947 0.9947} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.9.7.4 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0769 0.1538 0.1538 0.1538 0.3846 0.4615 0.5385 0.6154 0.6923 0.6923 0.8462 0.8462 0.8462 0.0000 0.0000 0.1000 0.1000 0.2000 0.2000 0.3000 0.3000 0.4000 0.4000 0.5000 0.5500 0.5500 0.6500 0.6500 0.6500 0.8000 0.8000 0.8000 0.8000 0.8000 0.0000 0.0000 0.1333 0.1333 0.1333 0.3333 0.3333 0.4667 0.5333 0.6000 0.6667 0.6667 0.8000 0.8000 0.8000 1.0000 0.0000 0.0000 0.1000 0.1000 0.2000 0.2000 0.3000 0.3000 0.3000 0.3000 0.5000 0.5000 0.6000 0.6500 0.7000 0.7000 0.7000 0.8500 0.9000 0.9000 0.9000 0.0000 0.0556 0.1111 0.1667 0.1667 0.1667 0.1667 0.3889 0.3889 0.5000 0.5556 0.6111 0.6111 0.6111 0.7778 0.7778 0.7778 0.7778 1.0000 0.0000 0.0500 0.0500 0.0500 0.2000 0.2500 0.2500 0.3500 0.3500 0.4500 0.4500 0.4500 0.6000 0.6000 0.6000 0.7500 0.7500 0.7500 0.9000 0.9000 0.9000 0.0000 0.0500 0.0500 0.0500 0.2000 0.2000 0.3000 0.3000 0.3000 0.3000 0.5000 0.5000 0.6000 0.6000 0.6000 0.7500 0.8000 0.8500 0.9000 0.9000 0.9000 0.0000 0.0000 0.0000 0.1579 0.2105 0.2105 0.3158 0.3158 0.4211 0.4211 0.4211 0.4211 0.6316 0.6842 0.7368 0.7368 0.8421 0.8421 0.9474 0.9474 0.0000 0.0000 0.0000 0.2000 0.2667 0.2667 0.4000 0.4000 0.4000 0.4000 0.6667 0.7333 0.7333 0.8667 0.9333 0.9333 0.0000 0.0000 0.0000 0.1429 0.1905 0.1905 0.1905 0.3333 0.3333 0.3333 0.4762 0.5238 0.5238 0.5238 0.5238 0.7143 0.7619 0.8095 0.8095 0.8095 0.9524 0.9524} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.9.7.5 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.9.7.6 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER (PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND CURRENT ROW) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.9.8.1 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY a ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0052 0.0105 0.0157 0.0209 0.0262 0.0314 0.0366 0.0419 0.0471 0.0524 0.0576 0.0628 0.0681 0.0733 0.0785 0.0838 0.0890 0.0942 0.0995 0.1047 0.1099 0.1152 0.1204 0.1257 0.1309 0.1361 0.1414 0.1466 0.1518 0.1571 0.1623 0.1675 0.1728 0.1780 0.1832 0.1885 0.1937 0.1990 0.2042 0.2094 0.2147 0.2199 0.2251 0.2304 0.2356 0.2408 0.2461 0.2513 0.2565 0.2618 0.2670 0.2723 0.2775 0.2827 0.2880 0.2932 0.2984 0.3037 0.3089 0.3141 0.3194 0.3246 0.3298 0.3351 0.3403 0.3455 0.3508 0.3560 0.3613 0.3665 0.3717 0.3770 0.3822 0.3874 0.3927 0.3979 0.4031 0.4084 0.4136 0.4188 0.4241 0.4293 0.4346 0.4398 0.4450 0.4503 0.4555 0.4607 0.4660 0.4712 0.4764 0.4817 0.4869 0.4921 0.4974 0.5026 0.5079 0.5131 0.5183 0.5236 0.5288 0.5340 0.5393 0.5445 0.5497 0.5550 0.5602 0.5654 0.5707 0.5759 0.5812 0.5864 0.5916 0.5969 0.6021 0.6073 0.6126 0.6178 0.6230 0.6283 0.6335 0.6387 0.6440 0.6492 0.6545 0.6597 0.6649 0.6702 0.6754 0.6806 0.6859 0.6911 0.6963 0.7016 0.7068 0.7120 0.7173 0.7225 0.7277 0.7330 0.7382 0.7435 0.7487 0.7539 0.7592 0.7644 0.7696 0.7749 0.7801 0.7853 0.7906 0.7958 0.8010 0.8063 0.8115 0.8168 0.8220 0.8272 0.8325 0.8377 0.8429 0.8482 0.8534 0.8586 0.8639 0.8691 0.8743 0.8796 0.8848 0.8901 0.8953 0.9005 0.9058 0.9110 0.9162 0.9215 0.9267 0.9319 0.9372 0.9424 0.9476 0.9529 0.9581 0.9634 0.9686 0.9738 0.9791 0.9843 0.9895 0.9948 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.9.8.2 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0714 0.1429 0.2143 0.2857 0.3571 0.4286 0.5000 0.5714 0.6429 0.7143 0.7857 0.8571 0.9286 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0625 0.1250 0.1875 0.2500 0.3125 0.3750 0.4375 0.5000 0.5625 0.6250 0.6875 0.7500 0.8125 0.8750 0.9375 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0526 0.1053 0.1579 0.2105 0.2632 0.3158 0.3684 0.4211 0.4737 0.5263 0.5789 0.6316 0.6842 0.7368 0.7895 0.8421 0.8947 0.9474 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0625 0.1250 0.1875 0.2500 0.3125 0.3750 0.4375 0.5000 0.5625 0.6250 0.6875 0.7500 0.8125 0.8750 0.9375 1.0000 0.0455 0.0909 0.1364 0.1818 0.2273 0.2727 0.3182 0.3636 0.4091 0.4545 0.5000 0.5455 0.5909 0.6364 0.6818 0.7273 0.7727 0.8182 0.8636 0.9091 0.9545 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.9.8.3 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY b ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0105 0.0105 0.0209 0.0209 0.0314 0.0314 0.0366 0.0419 0.0471 0.0628 0.0628 0.0628 0.0785 0.0785 0.0785 0.0942 0.0942 0.0942 0.0995 0.1099 0.1099 0.1257 0.1257 0.1257 0.1361 0.1361 0.1414 0.1571 0.1571 0.1571 0.1728 0.1728 0.1728 0.1780 0.1832 0.1885 0.1990 0.1990 0.2094 0.2094 0.2199 0.2199 0.2251 0.2304 0.2408 0.2408 0.2513 0.2513 0.2565 0.2723 0.2723 0.2723 0.2880 0.2880 0.2880 0.2984 0.2984 0.3037 0.3246 0.3246 0.3246 0.3246 0.3455 0.3455 0.3455 0.3455 0.3560 0.3560 0.3770 0.3770 0.3770 0.3770 0.3874 0.3874 0.3979 0.3979 0.4136 0.4136 0.4136 0.4188 0.4293 0.4293 0.4346 0.4450 0.4450 0.4555 0.4555 0.4660 0.4660 0.4869 0.4869 0.4869 0.4869 0.4921 0.4974 0.5026 0.5079 0.5131 0.5183 0.5288 0.5288 0.5445 0.5445 0.5445 0.5497 0.5707 0.5707 0.5707 0.5707 0.5916 0.5916 0.5916 0.5916 0.5969 0.6073 0.6073 0.6178 0.6178 0.6230 0.6283 0.6440 0.6440 0.6440 0.6492 0.6545 0.6597 0.6649 0.6702 0.6859 0.6859 0.6859 0.7016 0.7016 0.7016 0.7173 0.7173 0.7173 0.7330 0.7330 0.7330 0.7382 0.7487 0.7487 0.7592 0.7592 0.7644 0.7749 0.7749 0.7906 0.7906 0.7906 0.7958 0.8010 0.8220 0.8220 0.8220 0.8220 0.8377 0.8377 0.8377 0.8429 0.8534 0.8534 0.8586 0.8743 0.8743 0.8743 0.8901 0.8901 0.8901 0.9162 0.9162 0.9162 0.9162 0.9162 0.9319 0.9319 0.9319 0.9372 0.9529 0.9529 0.9529 0.9686 0.9686 0.9686 0.9791 0.9791 0.9895 0.9895 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.9.8.4 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0714 0.1429 0.3571 0.3571 0.3571 0.4286 0.5000 0.5714 0.6429 0.7857 0.7857 1.0000 1.0000 1.0000 0.0952 0.0952 0.1905 0.1905 0.2857 0.2857 0.3810 0.3810 0.4762 0.4762 0.5238 0.6190 0.6190 0.7619 0.7619 0.7619 1.0000 1.0000 1.0000 1.0000 1.0000 0.1250 0.1250 0.3125 0.3125 0.3125 0.4375 0.4375 0.5000 0.5625 0.6250 0.7500 0.7500 0.9375 0.9375 0.9375 1.0000 0.0952 0.0952 0.1905 0.1905 0.2857 0.2857 0.4762 0.4762 0.4762 0.4762 0.5714 0.5714 0.6190 0.6667 0.8095 0.8095 0.8095 0.8571 1.0000 1.0000 1.0000 0.0526 0.1053 0.1579 0.3684 0.3684 0.3684 0.3684 0.4737 0.4737 0.5263 0.5789 0.7368 0.7368 0.7368 0.9474 0.9474 0.9474 0.9474 1.0000 0.0476 0.1905 0.1905 0.1905 0.2381 0.3333 0.3333 0.4286 0.4286 0.5714 0.5714 0.5714 0.7143 0.7143 0.7143 0.8571 0.8571 0.8571 1.0000 1.0000 1.0000 0.0476 0.1905 0.1905 0.1905 0.2857 0.2857 0.4762 0.4762 0.4762 0.4762 0.5714 0.5714 0.7143 0.7143 0.7143 0.7619 0.8095 0.8571 1.0000 1.0000 1.0000 0.1500 0.1500 0.1500 0.2000 0.3000 0.3000 0.4000 0.4000 0.6000 0.6000 0.6000 0.6000 0.6500 0.7000 0.8000 0.8000 0.9000 0.9000 1.0000 1.0000 0.1875 0.1875 0.1875 0.2500 0.3750 0.3750 0.6250 0.6250 0.6250 0.6250 0.6875 0.8125 0.8125 0.8750 1.0000 1.0000 0.1364 0.1364 0.1364 0.1818 0.3182 0.3182 0.3182 0.4545 0.4545 0.4545 0.5000 0.6818 0.6818 0.6818 0.6818 0.7273 0.7727 0.9091 0.9091 0.9091 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.9.8.5 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.9.8.6 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.9.8.1 { + set myres {} + foreach r [db eval {SELECT ntile(100) OVER ( ORDER BY a ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 88.0000 89.0000 89.0000 90.0000 90.0000 91.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.9.8.2 { + set myres {} + foreach r [db eval {SELECT ntile(101) OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 22.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.9.8.3 { + set myres {} + foreach r [db eval {SELECT ntile(102) OVER ( ORDER BY b,a ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 88.0000 89.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.9.8.4 { + set myres {} + foreach r [db eval {SELECT ntile(103) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 22.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.9.8.5 { + set myres {} + foreach r [db eval {SELECT ntile(104) OVER ( ORDER BY b%10,a ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000 103.0000 104.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.9.8.6 { + set myres {} + foreach r [db eval {SELECT ntile(105) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND CURRENT ROW) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.9.8.7 { + set myres {} + foreach r [db eval {SELECT ntile(105) OVER ( ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 88.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000 103.0000 104.0000 105.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + +do_execsql_test 1.9.9.1 { + SELECT last_value(a+b) OVER ( ORDER BY a ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {99 92 108 72 52 83 55 79 109 65 26 120 119 50 + 70 103 80 124 36 96 59 124 116 110 57 51 52 130 + 103 74 87 48 128 117 105 136 131 71 133 92 109 63 + 84 109 57 146 78 147 113 74 88 150 87 110 65 121 + 106 110 124 85 145 107 161 171 150 156 80 171 120 + 109 158 114 111 136 147 87 173 124 168 173 162 132 + 101 154 167 190 161 110 156 195 198 102 123 177 169 + 140 111 180 119 160 197 152 124 121 134 146 147 132 + 213 141 193 200 210 157 132 136 175 161 218 188 226 + 191 187 208 211 179 138 144 223 196 214 170 212 202 + 163 184 172 173 195 229 240 187 210 200 163 227 228 + 223 191 252 235 225 243 172 187 202 179 179 182 231 + 261 207 263 206 189 209 212 276 181 274 249 239 234 + 213 234 269 196 271 221 210 229 235 250 223 232 229 + 279 224 280 216 207} + +do_execsql_test 1.9.9.2 { + SELECT last_value(a+b) OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {147 106 109 168 134 218 191 212 229 240 213 234 196 + 223 92 109 105 136 146 65 156 132 154 102 123 119 + 160 152 146 147 136 243 261 263 210 79 63 84 78 + 120 87 162 124 141 138 227 228 179 231 234 280 124 + 57 130 92 57 110 114 136 147 167 110 180 193 191 + 252 187 179 206 181 221 279 80 116 117 71 80 171 + 173 177 157 161 179 214 225 182 209 269 271 235 229 + 103 74 131 133 113 74 87 145 190 161 169 140 111 + 132 213 187 208 223 235 189 274 108 65 26 70 51 + 52 128 109 121 124 85 107 150 195 226 172 173 187 + 223 207 212 119 50 124 96 110 87 48 110 173 124 + 197 211 144 196 195 200 202 224 216 207 52 83 103 + 36 88 171 158 156 198 121 210 132 210 239 250 232 + 99 72 55 120 59 109 150 161 111 101 200 175 188 + 170 202 163 184 163 172 276 249 229} + +do_execsql_test 1.9.9.3 { + SELECT last_value(a+b) OVER ( ORDER BY b,a ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {65 102 87 138 57 181 80 111 26 48 144 207 36 121 + 132 101 163 172 196 119 136 63 124 179 110 179 182 + 74 132 189 51 52 85 216 163 134 123 210 78 141 57 + 187 71 87 172 173 50 224 88 59 111 170 109 213 + 223 146 147 84 114 191 206 221 157 161 209 229 74 + 140 107 187 207 212 124 202 52 232 55 184 229 106 + 132 152 120 92 110 179 235 65 70 87 110 195 200 + 175 234 160 234 136 80 113 187 109 121 124 196 156 + 210 239 250 72 109 188 202 191 105 154 79 231 147 + 225 103 161 169 223 96 83 249 212 162 227 228 167 + 180 193 117 177 214 145 208 235 150 110 211 103 158 + 200 168 229 92 156 243 280 279 116 173 269 271 131 + 133 223 128 173 197 210 99 150 161 147 218 240 109 + 136 146 261 263 124 130 252 171 190 213 274 108 195 + 226 119 124 171 198 120 276} + +do_execsql_test 1.9.9.4 { + SELECT last_value(a+b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {196 134 109 213 223 106 234 191 212 168 229 147 218 + 240 65 102 119 136 123 210 146 147 132 152 160 105 + 154 92 156 243 109 136 146 261 263 87 138 63 124 + 179 78 141 84 120 234 79 231 162 227 228 280 57 + 181 110 179 57 187 114 191 206 221 92 110 136 147 + 167 180 193 279 124 130 252 80 182 71 157 161 209 + 229 179 235 80 225 117 177 214 116 173 269 271 171 + 111 74 132 189 87 74 140 113 187 103 161 169 145 + 208 235 131 133 223 190 213 274 26 51 52 85 172 + 173 107 187 207 212 65 70 109 121 124 223 150 128 + 108 195 226 48 144 207 216 50 224 124 202 87 110 + 195 200 196 96 110 211 173 197 119 124 36 121 132 + 88 52 232 156 210 239 250 83 103 158 210 171 198 + 101 163 172 163 59 111 170 55 184 229 175 72 109 + 188 202 249 200 99 150 161 120 276} + +do_execsql_test 1.9.9.5 { + SELECT last_value(a+b) OVER ( ORDER BY b%10,a ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {147 106 109 168 134 218 191 212 229 240 213 234 196 + 223 92 109 105 136 146 65 156 132 154 102 123 119 + 160 152 146 147 136 243 261 263 210 79 63 84 78 + 120 87 162 124 141 138 227 228 179 231 234 280 124 + 57 130 92 57 110 114 136 147 167 110 180 193 191 + 252 187 179 206 181 221 279 80 116 117 71 80 171 + 173 177 157 161 179 214 225 182 209 269 271 235 229 + 103 74 131 133 113 74 87 145 190 161 169 140 111 + 132 213 187 208 223 235 189 274 108 65 26 70 51 + 52 128 109 121 124 85 107 150 195 226 172 173 187 + 223 207 212 119 50 124 96 110 87 48 110 173 124 + 197 211 144 196 195 200 202 224 216 207 52 83 103 + 36 88 171 158 156 198 121 210 132 210 239 250 232 + 99 72 55 120 59 109 150 161 111 101 200 175 188 + 170 202 163 184 163 172 276 249 229} + +do_execsql_test 1.9.9.6 { + SELECT last_value(a+b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND CURRENT ROW) FROM t2 +} {108 52 83 79 65 26 70 103 80 36 116 51 52 128 + 117 71 63 84 109 78 147 88 121 106 124 85 107 171 + 150 80 171 120 109 158 87 168 173 162 156 195 198 + 177 124 121 134 141 210 157 132 161 218 226 191 179 + 138 214 212 172 173 229 240 187 210 227 228 223 225 + 179 182 231 207 209 212 239 234 213 234 269 196 271 + 235 250 223 232 229 280 99 92 72 55 109 120 119 + 50 124 96 59 124 110 57 130 103 74 87 48 105 136 + 131 133 92 109 57 146 113 74 150 87 110 65 110 + 145 161 156 114 111 136 147 173 124 132 101 154 167 + 190 161 110 102 123 169 140 111 180 119 160 197 152 + 146 147 132 213 193 200 136 175 188 187 208 211 144 + 223 196 170 202 163 184 195 200 163 191 252 235 243 + 172 187 202 179 261 263 206 189 276 181 274 249 221 + 210 229 279 224 216 207} + +do_execsql_test 1.9.10.1 { + SELECT nth_value(b,b+1) OVER (ORDER BY a ROWS BETWEEN CURRENT ROW AND CURRENT ROW) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.9.10.2 { + SELECT nth_value(b,b+1) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN CURRENT ROW AND CURRENT ROW) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.9.10.3 { + SELECT nth_value(b,b+1) OVER ( ORDER BY b,a ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.9.10.4 { + SELECT nth_value(b,b+1) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.9.10.5 { + SELECT nth_value(b,b+1) OVER ( ORDER BY b%10,a ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.9.10.6 { + SELECT nth_value(b,b+1) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND CURRENT ROW) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.9.11.1 { + SELECT first_value(b) OVER (ORDER BY a ROWS BETWEEN CURRENT ROW AND CURRENT ROW) FROM t2 +} {89 81 96 59 38 68 39 62 91 46 6 99 97 27 46 + 78 54 97 8 67 29 93 84 77 23 16 16 93 65 35 + 47 7 86 74 61 91 85 24 85 43 59 12 32 56 3 91 + 22 90 55 15 28 89 25 47 1 56 40 43 56 16 75 + 36 89 98 76 81 4 94 42 30 78 33 29 53 63 2 87 + 37 80 84 72 41 9 61 73 95 65 13 58 96 98 1 21 + 74 65 35 5 73 11 51 87 41 12 8 20 31 31 15 95 + 22 73 79 88 34 8 11 49 34 90 59 96 60 55 75 + 77 44 2 7 85 57 74 29 70 59 19 39 26 26 47 80 + 90 36 58 47 9 72 72 66 33 93 75 64 81 9 23 37 + 13 12 14 62 91 36 91 33 15 34 36 99 3 95 69 + 58 52 30 50 84 10 84 33 21 39 44 58 30 38 34 + 83 27 82 17 7} + +do_execsql_test 1.9.11.2 { + SELECT first_value(b) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN CURRENT ROW AND CURRENT ROW) FROM t2 +} {90 40 30 80 20 90 60 70 80 90 30 50 10 30 81 + 91 61 91 91 1 81 41 61 1 21 11 51 41 31 31 11 + 81 91 91 21 62 12 32 22 42 2 72 12 22 2 72 72 + 12 62 52 82 93 23 93 43 3 43 33 53 63 73 13 + 73 73 33 93 23 13 33 3 33 83 54 84 74 24 4 94 + 84 74 34 34 44 74 64 14 34 84 84 44 34 65 35 + 85 85 55 15 25 75 95 65 65 35 5 15 95 55 75 + 85 75 15 95 96 46 6 46 16 16 86 56 56 56 16 + 36 76 96 96 26 26 36 66 36 36 97 27 97 67 77 + 47 7 47 87 37 87 77 7 57 47 47 37 27 17 7 38 + 68 78 8 28 98 78 58 98 8 88 8 58 58 58 38 89 + 59 39 99 29 59 89 89 29 9 79 49 59 29 59 19 + 39 9 9 99 69 39} + +do_execsql_test 1.9.11.3 { + SELECT first_value(b) OVER ( ORDER BY b,a ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {1 1 2 2 3 3 4 5 6 7 7 7 8 8 8 9 9 9 10 + 11 11 12 12 12 13 13 14 15 15 15 16 16 16 17 + 19 20 21 21 22 22 23 23 24 25 26 26 27 27 28 + 29 29 29 30 30 30 31 31 32 33 33 33 33 34 34 + 34 34 35 35 36 36 36 36 37 37 38 38 39 39 39 + 40 41 41 42 43 43 44 44 46 46 47 47 47 47 49 + 50 51 52 53 54 55 55 56 56 56 57 58 58 58 58 + 59 59 59 59 60 61 61 62 62 63 64 65 65 65 66 + 67 68 69 70 72 72 72 73 73 73 74 74 74 75 75 + 75 76 77 77 78 78 79 80 80 81 81 81 82 83 84 + 84 84 84 85 85 85 86 87 87 88 89 89 89 90 90 + 90 91 91 91 91 91 93 93 93 94 95 95 95 96 96 + 96 97 97 98 98 99 99} + +do_execsql_test 1.9.11.4 { + SELECT first_value(b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {10 20 30 30 30 40 50 60 70 80 80 90 90 90 1 1 + 11 11 21 21 31 31 41 41 51 61 61 81 81 81 91 + 91 91 91 91 2 2 12 12 12 22 22 32 42 52 62 62 + 72 72 72 82 3 3 13 13 23 23 33 33 33 33 43 43 + 53 63 73 73 73 83 93 93 93 4 14 24 34 34 34 + 34 44 44 54 64 74 74 74 84 84 84 84 94 5 15 + 15 15 25 35 35 55 55 65 65 65 75 75 75 85 85 + 85 95 95 95 6 16 16 16 26 26 36 36 36 36 46 + 46 56 56 56 66 76 86 96 96 96 7 7 7 17 27 27 + 37 37 47 47 47 47 57 67 77 77 87 87 97 97 8 8 + 8 28 38 38 58 58 58 58 68 78 78 88 98 98 9 9 + 9 19 29 29 29 39 39 39 49 59 59 59 59 69 79 + 89 89 89 99 99} + +do_execsql_test 1.9.11.5 { + SELECT first_value(b) OVER ( ORDER BY b%10,a ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {90 40 30 80 20 90 60 70 80 90 30 50 10 30 81 + 91 61 91 91 1 81 41 61 1 21 11 51 41 31 31 11 + 81 91 91 21 62 12 32 22 42 2 72 12 22 2 72 72 + 12 62 52 82 93 23 93 43 3 43 33 53 63 73 13 + 73 73 33 93 23 13 33 3 33 83 54 84 74 24 4 94 + 84 74 34 34 44 74 64 14 34 84 84 44 34 65 35 + 85 85 55 15 25 75 95 65 65 35 5 15 95 55 75 + 85 75 15 95 96 46 6 46 16 16 86 56 56 56 16 + 36 76 96 96 26 26 36 66 36 36 97 27 97 67 77 + 47 7 47 87 37 87 77 7 57 47 47 37 27 17 7 38 + 68 78 8 28 98 78 58 98 8 88 8 58 58 58 38 89 + 59 39 99 29 59 89 89 29 9 79 49 59 29 59 19 + 39 9 9 99 69 39} + +do_execsql_test 1.9.11.6 { + SELECT first_value(b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND CURRENT ROW) FROM t2 +} {96 38 68 62 46 6 46 78 54 8 84 16 16 86 74 24 + 12 32 56 22 90 28 56 40 56 16 36 98 76 4 94 + 42 30 78 2 80 84 72 58 96 98 74 12 8 20 22 88 + 34 8 34 90 96 60 44 2 74 70 26 26 80 90 36 58 + 72 72 66 64 12 14 62 36 34 36 58 52 30 50 84 + 10 84 44 58 30 38 34 82 89 81 59 39 91 99 97 + 27 97 67 29 93 77 23 93 65 35 47 7 61 91 85 + 85 43 59 3 91 55 15 89 25 47 1 43 75 89 81 33 + 29 53 63 87 37 41 9 61 73 95 65 13 1 21 65 35 + 5 73 11 51 87 41 31 31 15 95 73 79 11 49 59 + 55 75 77 7 85 57 29 59 19 39 47 47 9 33 93 75 + 81 9 23 37 13 91 91 33 15 99 3 95 69 33 21 39 + 83 27 17 7} + +do_execsql_test 1.9.12.1 { + SELECT lead(b,b) OVER (ORDER BY a ROWS BETWEEN CURRENT ROW AND CURRENT ROW) FROM t2 +} {96 9 11 89 32 53 91 30 51 56 54 73 22 59 75 + 74 78 8 16 65 15 8 31 87 90 12 32 96 74 76 37 + 85 90 15 35 2 60 36 75 9 51 47 63 51 90 26 42 + 26 8 76 80 90 37 87 56 79 5 87 8 2 39 73 64 + 36 90 72 78 36 73 51 33 20 41 2 26 37 33 8 14 + 33 81 55 1 9 12 39 64 87 72 34 82 21 34 99 62 + 74 41 69 22 75 27 58 8 79 77 26 26 55 {} 29 + 30 7 {} 66 55 2 34 64 {} 33 {} 44 84 {} {} 95 + 85 19 {} 83 {} 91 {} {} 9 50 91 33 34 {} {} + 84 {} 7 9 {} {} {} 44 {} {} {} {} 91 84 {} 95 + 95 52 {} {} {} {} {} 21 {} {} {} 58 {} {} {} + {} {} {} {} 83 {} {} {} {} {} {} {} {} {} {} + {} {} {} {}} + +do_execsql_test 1.9.12.2 { + SELECT lead(b,b) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN CURRENT ROW AND CURRENT ROW) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 81 {} {} {} 21 {} {} {} {} {} {} + {} {} {} {} {} {} 62 {} {} {} 12 {} {} {} 72 + {} {} {} {} {} {} {} {} {} {} 53 {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 34 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} 95 {} {} {} {} {} {} 85 {} + {} {} {} {} {} {} {} {} {} 56 {} 36 {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 57 {} {} {} {} {} 7 {} {} {} {} + {} {} {} {} {} {} 8 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 9 {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.9.12.3 { + SELECT lead(b,b) OVER ( ORDER BY b,a ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {1 2 3 3 5 6 7 8 8 9 9 10 11 12 12 13 13 + 14 15 16 16 17 19 20 21 22 23 24 25 26 27 27 + 28 29 30 31 32 33 33 33 34 34 35 36 36 36 37 + 38 39 39 40 41 42 43 43 44 46 47 47 47 49 50 + 52 53 54 55 56 56 57 58 58 58 59 59 59 60 61 + 62 62 64 65 65 67 69 70 72 72 73 74 74 75 75 + 75 77 78 80 81 81 83 84 84 85 85 85 87 88 89 + 89 89 90 90 91 91 91 93 93 94 95 95 96 97 97 + 98 99 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.9.12.4 { + SELECT lead(b,b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {80 {} {} {} {} {} {} {} {} {} {} {} {} {} 1 + 11 81 81 {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} 12 12 72 82 {} {} {} {} {} {} + {} {} {} {} {} {} 13 23 73 73 {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} 34 84 {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 35 85 85 95 {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} 36 86 96 96 {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 37 47 + 47 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} 58 58 68 {} {} {} {} {} {} {} {} {} + {} {} {} {} 39 49 59 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.9.12.5 { + SELECT lead(b,b) OVER ( ORDER BY b%10,a ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {34 72 91 74 21 55 23 94 84 95 2 13 61 22 55 + 55 94 85 75 81 65 73 74 21 72 12 94 33 63 73 + 72 96 36 76 3 25 62 3 73 34 12 46 43 93 72 16 + 86 63 15 65 36 77 24 57 25 53 95 34 95 16 97 + 74 97 67 25 98 44 34 65 54 5 68 96 28 47 95 + 34 39 8 38 6 46 96 28 47 95 56 39 99 97 76 8 + 26 9 79 27 95 16 29 {} 58 58 77 85 56 {} 98 + 29 {} 19 96 {} {} 78 56 98 36 97 {} 89 89 29 + 47 78 {} {} {} 38 68 58 {} 58 38 {} 98 {} {} + {} 39 57 9 {} 79 {} {} 7 {} {} {} 9 29 38 78 + {} {} {} 8 39 {} {} {} {} 59 {} 99 {} {} {} + {} {} {} {} {} {} {} {} {} {} 9 {} {} {} {} + {} {} {} {} {} {} {} {}} + +do_execsql_test 1.9.12.6 { + SELECT lead(b,b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND CURRENT ROW) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.9.13.1 { + SELECT lag(b,b) OVER (ORDER BY a ROWS BETWEEN CURRENT ROW AND CURRENT ROW) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} 38 {} {} {} {} + {} {} {} 6 {} {} {} {} {} 81 46 6 {} {} {} {} + 23 {} {} {} {} {} 27 {} {} {} 35 6 {} 12 {} + 23 {} {} 61 84 {} 93 39 47 {} 54 46 96 56 {} + 16 {} {} {} {} 89 {} 16 43 {} 85 56 29 99 53 + {} 59 {} {} 91 59 53 84 99 {} 93 63 47 {} {} + 98 33 67 35 75 1 23 13 55 27 75 98 35 73 63 2 + 21 27 13 24 86 23 84 31 20 94 61 65 75 23 36 + 94 55 90 41 77 96 56 29 40 12 89 63 11 5 73 + 79 1 16 28 31 73 5 39 53 63 41 11 40 2 13 33 + 9 29 90 47 72 9 73 30 44 33 74 93 29 74 42 34 + 63 41 34 96 47 77 1 36 74 72 14 36 26 77 9 72 + 64 8 91 31 52 30} + +do_execsql_test 1.9.13.2 { + SELECT lag(b,b) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN CURRENT ROW AND CURRENT ROW) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} 30 {} {} + {} {} {} {} 91 {} {} {} 61 {} 81 {} {} {} {} + 1 {} {} {} {} {} {} {} {} {} 22 {} {} {} 12 + {} {} 62 {} {} {} {} {} {} {} 23 {} {} {} {} + {} {} {} {} {} {} {} 43 {} 23 {} {} {} {} {} + {} 54 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 75 {} + {} {} {} {} {} 55 {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} 47 {} {} {} {} + {} 27 7 {} {} {} {} {} {} {} {} {} 68 {} 8 {} + {} {} {} {} {} {} {} {} {} {} {} {} 89 {} {} + {} {} {} {} {} 29 9 {} {} {}} + +do_execsql_test 1.9.13.3 { + SELECT lag(b,b) OVER ( ORDER BY b,a ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {{} 1 1 1 1 2 2 2 2 2 2 3 3 3 4 4 5 6 6 + 6 7 7 7 7 7 8 8 8 8 8 8 9 9 9 9 9 9 9 + 9 9 9 10 10 10 10 11 11 11 11 11 12 12 12 12 + 13 13 13 13 13 14 15 15 15 15 16 16 16 16 16 + 17 19 20 20 21 21 21 21 22 22 22 22 23 23 23 + 23 23 24 23 24 24 25 26 26 26 26 26 26 26 26 + 26 26 26 27 27 27 27 28 29 29 29 29 30 30 30 + 30 30 30 31 31 31 31 31 32 32 32 32 32 32 31 + 32 33 33 33 33 33 33 34 34 34 34 34 34 34 34 + 35 35 35 35 35 36 36 36 36 36 36 36 37 37 37 + 38 38 38 38 38 38 39 39 39 39 40 40 41 41 42 + 43 42 43 43 43 43 44 44 44 46 46 46 47 47 47 + 47 47} + +do_execsql_test 1.9.13.4 { + SELECT lag(b,b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + 1 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.9.13.5 { + SELECT lag(b,b) OVER ( ORDER BY b%10,a ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} 30 {} {} + {} {} {} {} 91 {} {} {} 61 80 81 {} {} {} {} + 1 {} {} {} 30 {} 21 90 61 {} 22 {} 11 41 12 + {} {} 62 {} {} {} {} 31 {} 50 23 30 21 90 {} + {} 62 {} {} 81 {} 22 43 62 23 32 {} 91 {} 90 + 93 54 {} {} 90 72 12 22 90 81 83 23 80 20 72 + 43 51 33 80 90 2 34 54 1 20 62 12 13 75 44 30 + 93 91 1 21 55 61 61 13 85 3 65 65 91 73 33 93 + 55 84 62 31 11 65 35 85 33 55 15 12 75 22 3 + 73 65 36 85 43 95 43 13 47 44 65 65 96 36 27 + 7 46 34 94 47 36 73 34 35 73 68 24 8 75 85 75 + 66 34 95 36 84 77 46 34 84 47 89 65 36 16 38 + 76 58 57 29 9 44 56 17} + +do_execsql_test 1.9.13.6 { + SELECT lag(b,b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND CURRENT ROW) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.9.14.1 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (ORDER BY a ROWS BETWEEN CURRENT ROW AND CURRENT ROW) FROM t2 +} {89 81 96 59 38 68 39 62 91 46 6 99 97 27 46 + 78 54 97 8 67 29 93 84 77 23 16 16 93 65 35 + 47 7 86 74 61 91 85 24 85 43 59 12 32 56 3 91 + 22 90 55 15 28 89 25 47 1 56 40 43 56 16 75 + 36 89 98 76 81 4 94 42 30 78 33 29 53 63 2 87 + 37 80 84 72 41 9 61 73 95 65 13 58 96 98 1 21 + 74 65 35 5 73 11 51 87 41 12 8 20 31 31 15 95 + 22 73 79 88 34 8 11 49 34 90 59 96 60 55 75 + 77 44 2 7 85 57 74 29 70 59 19 39 26 26 47 80 + 90 36 58 47 9 72 72 66 33 93 75 64 81 9 23 37 + 13 12 14 62 91 36 91 33 15 34 36 99 3 95 69 + 58 52 30 50 84 10 84 33 21 39 44 58 30 38 34 + 83 27 82 17 7} + +do_execsql_test 1.9.14.2 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN CURRENT ROW AND CURRENT ROW) FROM t2 +} {90 40 30 80 20 90 60 70 80 90 30 50 10 30 81 + 91 61 91 91 1 81 41 61 1 21 11 51 41 31 31 11 + 81 91 91 21 62 12 32 22 42 2 72 12 22 2 72 72 + 12 62 52 82 93 23 93 43 3 43 33 53 63 73 13 + 73 73 33 93 23 13 33 3 33 83 54 84 74 24 4 94 + 84 74 34 34 44 74 64 14 34 84 84 44 34 65 35 + 85 85 55 15 25 75 95 65 65 35 5 15 95 55 75 + 85 75 15 95 96 46 6 46 16 16 86 56 56 56 16 + 36 76 96 96 26 26 36 66 36 36 97 27 97 67 77 + 47 7 47 87 37 87 77 7 57 47 47 37 27 17 7 38 + 68 78 8 28 98 78 58 98 8 88 8 58 58 58 38 89 + 59 39 99 29 59 89 89 29 9 79 49 59 29 59 19 + 39 9 9 99 69 39} + +do_execsql_test 1.9.14.3 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( ORDER BY b,a ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {1 1 2 2 3 3 4 5 6 7 7 7 8 8 8 9 9 9 10 + 11 11 12 12 12 13 13 14 15 15 15 16 16 16 17 + 19 20 21 21 22 22 23 23 24 25 26 26 27 27 28 + 29 29 29 30 30 30 31 31 32 33 33 33 33 34 34 + 34 34 35 35 36 36 36 36 37 37 38 38 39 39 39 + 40 41 41 42 43 43 44 44 46 46 47 47 47 47 49 + 50 51 52 53 54 55 55 56 56 56 57 58 58 58 58 + 59 59 59 59 60 61 61 62 62 63 64 65 65 65 66 + 67 68 69 70 72 72 72 73 73 73 74 74 74 75 75 + 75 76 77 77 78 78 79 80 80 81 81 81 82 83 84 + 84 84 84 85 85 85 86 87 87 88 89 89 89 90 90 + 90 91 91 91 91 91 93 93 93 94 95 95 95 96 96 + 96 97 97 98 98 99 99} + +do_execsql_test 1.9.14.4 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {10 20 30 30 30 40 50 60 70 80 80 90 90 90 1 1 + 11 11 21 21 31 31 41 41 51 61 61 81 81 81 91 + 91 91 91 91 2 2 12 12 12 22 22 32 42 52 62 62 + 72 72 72 82 3 3 13 13 23 23 33 33 33 33 43 43 + 53 63 73 73 73 83 93 93 93 4 14 24 34 34 34 + 34 44 44 54 64 74 74 74 84 84 84 84 94 5 15 + 15 15 25 35 35 55 55 65 65 65 75 75 75 85 85 + 85 95 95 95 6 16 16 16 26 26 36 36 36 36 46 + 46 56 56 56 66 76 86 96 96 96 7 7 7 17 27 27 + 37 37 47 47 47 47 57 67 77 77 87 87 97 97 8 8 + 8 28 38 38 58 58 58 58 68 78 78 88 98 98 9 9 + 9 19 29 29 29 39 39 39 49 59 59 59 59 69 79 + 89 89 89 99 99} + +do_execsql_test 1.9.14.5 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( ORDER BY b%10,a ROWS BETWEEN CURRENT ROW AND CURRENT ROW ) FROM t2 +} {90 40 30 80 20 90 60 70 80 90 30 50 10 30 81 + 91 61 91 91 1 81 41 61 1 21 11 51 41 31 31 11 + 81 91 91 21 62 12 32 22 42 2 72 12 22 2 72 72 + 12 62 52 82 93 23 93 43 3 43 33 53 63 73 13 + 73 73 33 93 23 13 33 3 33 83 54 84 74 24 4 94 + 84 74 34 34 44 74 64 14 34 84 84 44 34 65 35 + 85 85 55 15 25 75 95 65 65 35 5 15 95 55 75 + 85 75 15 95 96 46 6 46 16 16 86 56 56 56 16 + 36 76 96 96 26 26 36 66 36 36 97 27 97 67 77 + 47 7 47 87 37 87 77 7 57 47 47 37 27 17 7 38 + 68 78 8 28 98 78 58 98 8 88 8 58 58 58 38 89 + 59 39 99 29 59 89 89 29 9 79 49 59 29 59 19 + 39 9 9 99 69 39} + +do_execsql_test 1.9.14.6 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND CURRENT ROW) FROM t2 +} {96 38 68 62 46 6 46 78 54 8 84 16 16 86 74 24 + 12 32 56 22 90 28 56 40 56 16 36 98 76 4 94 + 42 30 78 2 80 84 72 58 96 98 74 12 8 20 22 88 + 34 8 34 90 96 60 44 2 74 70 26 26 80 90 36 58 + 72 72 66 64 12 14 62 36 34 36 58 52 30 50 84 + 10 84 44 58 30 38 34 82 89 81 59 39 91 99 97 + 27 97 67 29 93 77 23 93 65 35 47 7 61 91 85 + 85 43 59 3 91 55 15 89 25 47 1 43 75 89 81 33 + 29 53 63 87 37 41 9 61 73 95 65 13 1 21 65 35 + 5 73 11 51 87 41 31 31 15 95 73 79 11 49 59 + 55 75 77 7 85 57 29 59 19 39 47 47 9 33 93 75 + 81 9 23 37 13 91 91 33 15 99 3 95 69 33 21 39 + 83 27 17 7} + +do_execsql_test 1.9.14.7 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (win1 ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND CURRENT ROW) + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a) + ORDER BY 1 +} {1 1 10 11 11 12 12 12 13 13 14 15 15 15 16 16 + 16 17 19 2 2 20 21 21 22 22 23 23 24 25 26 26 + 27 27 28 29 29 29 3 3 30 30 30 31 31 32 33 33 + 33 33 34 34 34 34 35 35 36 36 36 36 37 37 38 + 38 39 39 39 4 40 41 41 42 43 43 44 44 46 46 + 47 47 47 47 49 5 50 51 52 53 54 55 55 56 56 + 56 57 58 58 58 58 59 59 59 59 6 60 61 61 62 + 62 63 64 65 65 65 66 67 68 69 7 7 7 70 72 72 + 72 73 73 73 74 74 74 75 75 75 76 77 77 78 78 + 79 8 8 8 80 80 81 81 81 82 83 84 84 84 84 85 + 85 85 86 87 87 88 89 89 89 9 9 9 90 90 90 91 + 91 91 91 91 93 93 93 94 95 95 95 96 96 96 97 + 97 98 98 99 99} + +do_execsql_test 1.9.14.8 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (win1 ROWS BETWEEN CURRENT ROW AND CURRENT ROW) + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a ORDER BY b%10) + ORDER BY 1 +} {1 1 10 11 11 12 12 12 13 13 14 15 15 15 16 16 + 16 17 19 2 2 20 21 21 22 22 23 23 24 25 26 26 + 27 27 28 29 29 29 3 3 30 30 30 31 31 32 33 33 + 33 33 34 34 34 34 35 35 36 36 36 36 37 37 38 + 38 39 39 39 4 40 41 41 42 43 43 44 44 46 46 + 47 47 47 47 49 5 50 51 52 53 54 55 55 56 56 + 56 57 58 58 58 58 59 59 59 59 6 60 61 61 62 + 62 63 64 65 65 65 66 67 68 69 7 7 7 70 72 72 + 72 73 73 73 74 74 74 75 75 75 76 77 77 78 78 + 79 8 8 8 80 80 81 81 81 82 83 84 84 84 84 85 + 85 85 86 87 87 88 89 89 89 9 9 9 90 90 90 91 + 91 91 91 91 93 93 93 94 95 95 95 96 96 96 97 + 97 98 98 99 99} + +do_execsql_test 1.9.14.9 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER win2 + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a ORDER BY b%10), + win2 AS (win1 ROWS BETWEEN CURRENT ROW AND CURRENT ROW) + ORDER BY 1 +} {1 1 10 11 11 12 12 12 13 13 14 15 15 15 16 16 + 16 17 19 2 2 20 21 21 22 22 23 23 24 25 26 26 + 27 27 28 29 29 29 3 3 30 30 30 31 31 32 33 33 + 33 33 34 34 34 34 35 35 36 36 36 36 37 37 38 + 38 39 39 39 4 40 41 41 42 43 43 44 44 46 46 + 47 47 47 47 49 5 50 51 52 53 54 55 55 56 56 + 56 57 58 58 58 58 59 59 59 59 6 60 61 61 62 + 62 63 64 65 65 65 66 67 68 69 7 7 7 70 72 72 + 72 73 73 73 74 74 74 75 75 75 76 77 77 78 78 + 79 8 8 8 80 80 81 81 81 82 83 84 84 84 84 85 + 85 85 86 87 87 88 89 89 89 9 9 9 90 90 90 91 + 91 91 91 91 93 93 93 94 95 95 95 96 96 96 97 + 97 98 98 99 99} + +do_execsql_test 1.9.15.1 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE a%2=0) OVER win FROM t2 + WINDOW win AS (ORDER BY a ROWS BETWEEN CURRENT ROW AND CURRENT ROW) +} {1 89 1 {} 1 96 1 {} 1 38 1 {} 1 39 1 {} 1 91 1 {} 1 6 + 1 {} 1 97 1 {} 1 46 1 {} 1 54 1 {} 1 8 1 {} 1 29 1 {} + 1 84 1 {} 1 23 1 {} 1 16 1 {} 1 65 1 {} 1 47 1 {} 1 86 + 1 {} 1 61 1 {} 1 85 1 {} 1 85 1 {} 1 59 1 {} 1 32 1 {} + 1 3 1 {} 1 22 1 {} 1 55 1 {} 1 28 1 {} 1 25 1 {} 1 1 + 1 {} 1 40 1 {} 1 56 1 {} 1 75 1 {} 1 89 1 {} 1 76 1 {} + 1 4 1 {} 1 42 1 {} 1 78 1 {} 1 29 1 {} 1 63 1 {} 1 87 + 1 {} 1 80 1 {} 1 72 1 {} 1 9 1 {} 1 73 1 {} 1 65 1 {} + 1 58 1 {} 1 98 1 {} 1 21 1 {} 1 65 1 {} 1 5 1 {} 1 11 + 1 {} 1 87 1 {} 1 12 1 {} 1 20 1 {} 1 31 1 {} 1 95 1 {} + 1 73 1 {} 1 88 1 {} 1 8 1 {} 1 49 1 {} 1 90 1 {} 1 96 + 1 {} 1 55 1 {} 1 77 1 {} 1 2 1 {} 1 85 1 {} 1 74 1 {} + 1 70 1 {} 1 19 1 {} 1 26 1 {} 1 47 1 {} 1 90 1 {} 1 58 + 1 {} 1 9 1 {} 1 72 1 {} 1 33 1 {} 1 75 1 {} 1 81 1 {} + 1 23 1 {} 1 13 1 {} 1 14 1 {} 1 91 1 {} 1 91 1 {} 1 15 + 1 {} 1 36 1 {} 1 3 1 {} 1 69 1 {} 1 52 1 {} 1 50 1 {} + 1 10 1 {} 1 33 1 {} 1 39 1 {} 1 58 1 {} 1 38 1 {} 1 83 + 1 {} 1 82 1 {} 1 7} + +do_execsql_test 1.9.15.2 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE 0=1) OVER win FROM t2 + WINDOW win AS (ORDER BY a ROWS BETWEEN CURRENT ROW AND CURRENT ROW) +} {1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {}} + +do_execsql_test 1.9.15.3 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE 1=0) OVER win FROM t2 + WINDOW win AS (PARTITION BY (a%10) ORDER BY a ROWS BETWEEN CURRENT ROW AND CURRENT ROW) +} {1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {}} + +do_execsql_test 1.9.15.4 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE a%2=0) OVER win FROM t2 + WINDOW win AS (PARTITION BY (a%10) ORDER BY a ROWS BETWEEN CURRENT ROW AND CURRENT ROW) +} {1 89 1 6 1 29 1 47 1 59 1 28 1 75 1 78 1 72 1 98 1 87 + 1 73 1 96 1 74 1 90 1 75 1 91 1 69 1 39 1 7 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 96 1 97 1 84 1 86 1 32 + 1 25 1 89 1 29 1 9 1 21 1 12 1 88 1 55 1 70 1 58 1 81 + 1 91 1 52 1 58 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 38 1 46 1 23 1 61 1 3 1 1 1 76 1 63 1 73 1 65 1 20 + 1 8 1 77 1 19 1 9 1 23 1 15 1 50 1 38 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 39 1 54 1 16 1 85 1 22 1 40 + 1 4 1 87 1 65 1 5 1 31 1 49 1 2 1 26 1 72 1 13 1 36 + 1 10 1 83 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 91 + 1 8 1 65 1 85 1 55 1 56 1 42 1 80 1 58 1 11 1 95 1 90 + 1 85 1 47 1 33 1 14 1 3 1 33 1 82 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} 1 {} + 1 {} 1 {} 1 {} 1 {}} + +do_execsql_test 1.10.2.1 { + SELECT max(b) OVER ( ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2 +} {96 96 96 96 96 96 96 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99} + +do_execsql_test 1.10.2.2 { + SELECT min(b) OVER ( ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2 +} {38 38 38 38 38 38 6 6 6 6 6 6 6 6 6 6 6 6 + 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 + 6 6 6 3 3 3 3 3 3 3 3 3 3 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1} + +do_execsql_test 1.10.3.1 { + SELECT row_number() OVER ( ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.10.3.2 { + SELECT row_number() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.10.3.3 { + SELECT row_number() OVER ( ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.10.4.1 { + SELECT dense_rank() OVER ( ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.10.4.2 { + SELECT dense_rank() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.10.4.3 { + SELECT dense_rank() OVER ( ORDER BY b ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2 +} {1 1 2 2 3 3 4 5 6 7 7 7 8 8 8 9 9 9 10 + 11 11 12 12 12 13 13 14 15 15 15 16 16 16 17 + 18 19 20 20 21 21 22 22 23 24 25 25 26 26 27 + 28 28 28 29 29 29 30 30 31 32 32 32 32 33 33 + 33 33 34 34 35 35 35 35 36 36 37 37 38 38 38 + 39 40 40 41 42 42 43 43 44 44 45 45 45 45 46 + 47 48 49 50 51 52 52 53 53 53 54 55 55 55 55 + 56 56 56 56 57 58 58 59 59 60 61 62 62 62 63 + 64 65 66 67 68 68 68 69 69 69 70 70 70 71 71 + 71 72 73 73 74 74 75 76 76 77 77 77 78 79 80 + 80 80 80 81 81 81 82 83 83 84 85 85 85 86 86 + 86 87 87 87 87 87 88 88 88 89 90 90 90 91 91 + 91 92 92 93 93 94 94} + +do_execsql_test 1.10.4.4 { + SELECT dense_rank() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2 +} {1 2 3 3 3 4 5 6 7 8 8 9 9 9 1 1 2 2 3 + 3 4 4 5 5 6 7 7 8 8 8 9 9 9 9 9 1 1 2 + 2 2 3 3 4 5 6 7 7 8 8 8 9 1 1 2 2 3 3 + 4 4 4 4 5 5 6 7 8 8 8 9 10 10 10 1 2 3 + 4 4 4 4 5 5 6 7 8 8 8 9 9 9 9 10 1 2 2 + 2 3 4 4 5 5 6 6 6 7 7 7 8 8 8 9 9 9 1 + 2 2 2 3 3 4 4 4 4 5 5 6 6 6 7 8 9 10 10 + 10 1 1 1 2 3 3 4 4 5 5 5 5 6 7 8 8 9 9 + 10 10 1 1 1 2 3 3 4 4 4 4 5 6 6 7 8 8 1 + 1 1 2 3 3 3 4 4 4 5 6 6 6 6 7 8 9 9 9 + 10 10} + +do_execsql_test 1.10.4.5 { + SELECT dense_rank() OVER ( ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 + 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 + 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 + 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 + 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 + 8 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 10 10 + 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 + 10 10 10 10 10} + +do_execsql_test 1.10.4.6 { + SELECT dense_rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 + 5 5 5 5 5 5 5 5 5 5 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 + 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 + 5} + +do_execsql_test 1.10.5.1 { + SELECT rank() OVER ( ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.10.5.2 { + SELECT rank() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.10.5.3 { + SELECT rank() OVER ( ORDER BY b ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2 +} {1 1 3 3 5 5 7 8 9 10 10 10 13 13 13 16 16 + 16 19 20 20 22 22 22 25 25 27 28 28 28 31 31 + 31 34 35 36 37 37 39 39 41 41 43 44 45 45 47 + 47 49 50 50 50 53 53 53 56 56 58 59 59 59 59 + 63 63 63 63 67 67 69 69 69 69 73 73 75 75 77 + 77 77 80 81 81 83 84 84 86 86 88 88 90 90 90 + 90 94 95 96 97 98 99 100 100 102 102 102 105 106 + 106 106 106 110 110 110 110 114 115 115 117 117 119 + 120 121 121 121 124 125 126 127 128 129 129 129 132 + 132 132 135 135 135 138 138 138 141 142 142 144 144 + 146 147 147 149 149 149 152 153 154 154 154 154 158 + 158 158 161 162 162 164 165 165 165 168 168 168 171 + 171 171 171 171 176 176 176 179 180 180 180 183 183 + 183 186 186 188 188 190 190} + +do_execsql_test 1.10.5.4 { + SELECT rank() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2 +} {1 2 3 3 3 6 7 8 9 10 10 12 12 12 1 1 3 3 + 5 5 7 7 9 9 11 12 12 14 14 14 17 17 17 17 + 17 1 1 3 3 3 6 6 8 9 10 11 11 13 13 13 16 + 1 1 3 3 5 5 7 7 7 7 11 11 13 14 15 15 15 + 18 19 19 19 1 2 3 4 4 4 4 8 8 10 11 12 12 + 12 15 15 15 15 19 1 2 2 2 5 6 6 8 8 10 10 + 10 13 13 13 16 16 16 19 19 19 1 2 2 2 5 5 7 + 7 7 7 11 11 13 13 13 16 17 18 19 19 19 1 1 + 1 4 5 5 7 7 9 9 9 9 13 14 15 15 17 17 19 + 19 1 1 1 4 5 5 7 7 7 7 11 12 12 14 15 15 + 1 1 1 4 5 5 5 8 8 8 11 12 12 12 12 16 17 + 18 18 18 21 21} + +do_execsql_test 1.10.5.5 { + SELECT rank() OVER ( ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 15 15 15 15 + 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 + 15 15 36 36 36 36 36 36 36 36 36 36 36 36 36 + 36 36 36 52 52 52 52 52 52 52 52 52 52 52 52 + 52 52 52 52 52 52 52 52 52 73 73 73 73 73 73 + 73 73 73 73 73 73 73 73 73 73 73 73 73 92 92 + 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 + 92 92 92 92 113 113 113 113 113 113 113 113 113 + 113 113 113 113 113 113 113 113 113 113 113 113 134 + 134 134 134 134 134 134 134 134 134 134 134 134 134 + 134 134 134 134 134 134 154 154 154 154 154 154 154 + 154 154 154 154 154 154 154 154 154 170 170 170 170 + 170 170 170 170 170 170 170 170 170 170 170 170 170 + 170 170 170 170 170} + +do_execsql_test 1.10.5.6 { + SELECT rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 15 15 15 15 + 15 15 15 15 15 15 15 15 15 15 15 15 31 31 31 + 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 + 31 50 50 50 50 50 50 50 50 50 50 50 50 50 50 + 50 50 50 50 50 50 50 71 71 71 71 71 71 71 71 + 71 71 71 71 71 71 71 71 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 22 22 22 22 22 22 + 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 + 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 + 43 43 43 43 43 43 64 64 64 64 64 64 64 64 64 + 64 64 64 64 64 64 64 64 64 64 64 84 84 84 84 + 84 84 84 84 84 84 84 84 84 84 84 84 84 84 84 + 84 84 84} + +do_execsql_test 1.10.6.1 { + SELECT + row_number() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ), + rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ), + dense_rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) + FROM t2 +} {1 1 1 2 1 1 3 1 1 4 1 1 5 1 1 6 1 1 7 1 1 8 1 1 9 1 1 + 10 1 1 11 1 1 12 1 1 13 1 1 14 1 1 15 15 2 16 15 2 17 15 2 + 18 15 2 19 15 2 20 15 2 21 15 2 22 15 2 23 15 2 24 15 2 + 25 15 2 26 15 2 27 15 2 28 15 2 29 15 2 30 15 2 31 31 3 + 32 31 3 33 31 3 34 31 3 35 31 3 36 31 3 37 31 3 38 31 3 + 39 31 3 40 31 3 41 31 3 42 31 3 43 31 3 44 31 3 45 31 3 + 46 31 3 47 31 3 48 31 3 49 31 3 50 50 4 51 50 4 52 50 4 + 53 50 4 54 50 4 55 50 4 56 50 4 57 50 4 58 50 4 59 50 4 + 60 50 4 61 50 4 62 50 4 63 50 4 64 50 4 65 50 4 66 50 4 + 67 50 4 68 50 4 69 50 4 70 50 4 71 71 5 72 71 5 73 71 5 + 74 71 5 75 71 5 76 71 5 77 71 5 78 71 5 79 71 5 80 71 5 + 81 71 5 82 71 5 83 71 5 84 71 5 85 71 5 86 71 5 1 1 1 2 1 1 + 3 1 1 4 1 1 5 1 1 6 1 1 7 1 1 8 1 1 9 1 1 10 1 1 11 1 1 + 12 1 1 13 1 1 14 1 1 15 1 1 16 1 1 17 1 1 18 1 1 19 1 1 + 20 1 1 21 1 1 22 22 2 23 22 2 24 22 2 25 22 2 26 22 2 27 22 2 + 28 22 2 29 22 2 30 22 2 31 22 2 32 22 2 33 22 2 34 22 2 + 35 22 2 36 22 2 37 22 2 38 22 2 39 22 2 40 22 2 41 22 2 + 42 22 2 43 43 3 44 43 3 45 43 3 46 43 3 47 43 3 48 43 3 + 49 43 3 50 43 3 51 43 3 52 43 3 53 43 3 54 43 3 55 43 3 + 56 43 3 57 43 3 58 43 3 59 43 3 60 43 3 61 43 3 62 43 3 + 63 43 3 64 64 4 65 64 4 66 64 4 67 64 4 68 64 4 69 64 4 + 70 64 4 71 64 4 72 64 4 73 64 4 74 64 4 75 64 4 76 64 4 + 77 64 4 78 64 4 79 64 4 80 64 4 81 64 4 82 64 4 83 64 4 + 84 84 5 85 84 5 86 84 5 87 84 5 88 84 5 89 84 5 90 84 5 + 91 84 5 92 84 5 93 84 5 94 84 5 95 84 5 96 84 5 97 84 5 + 98 84 5 99 84 5 100 84 5 101 84 5 102 84 5 103 84 5 104 84 5 + 105 84 5} + + +do_test 1.10.7.1 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0053 0.0105 0.0158 0.0211 0.0263 0.0316 0.0368 0.0421 0.0474 0.0526 0.0579 0.0632 0.0684 0.0737 0.0789 0.0842 0.0895 0.0947 0.1000 0.1053 0.1105 0.1158 0.1211 0.1263 0.1316 0.1368 0.1421 0.1474 0.1526 0.1579 0.1632 0.1684 0.1737 0.1789 0.1842 0.1895 0.1947 0.2000 0.2053 0.2105 0.2158 0.2211 0.2263 0.2316 0.2368 0.2421 0.2474 0.2526 0.2579 0.2632 0.2684 0.2737 0.2789 0.2842 0.2895 0.2947 0.3000 0.3053 0.3105 0.3158 0.3211 0.3263 0.3316 0.3368 0.3421 0.3474 0.3526 0.3579 0.3632 0.3684 0.3737 0.3789 0.3842 0.3895 0.3947 0.4000 0.4053 0.4105 0.4158 0.4211 0.4263 0.4316 0.4368 0.4421 0.4474 0.4526 0.4579 0.4632 0.4684 0.4737 0.4789 0.4842 0.4895 0.4947 0.5000 0.5053 0.5105 0.5158 0.5211 0.5263 0.5316 0.5368 0.5421 0.5474 0.5526 0.5579 0.5632 0.5684 0.5737 0.5789 0.5842 0.5895 0.5947 0.6000 0.6053 0.6105 0.6158 0.6211 0.6263 0.6316 0.6368 0.6421 0.6474 0.6526 0.6579 0.6632 0.6684 0.6737 0.6789 0.6842 0.6895 0.6947 0.7000 0.7053 0.7105 0.7158 0.7211 0.7263 0.7316 0.7368 0.7421 0.7474 0.7526 0.7579 0.7632 0.7684 0.7737 0.7789 0.7842 0.7895 0.7947 0.8000 0.8053 0.8105 0.8158 0.8211 0.8263 0.8316 0.8368 0.8421 0.8474 0.8526 0.8579 0.8632 0.8684 0.8737 0.8789 0.8842 0.8895 0.8947 0.9000 0.9053 0.9105 0.9158 0.9211 0.9263 0.9316 0.9368 0.9421 0.9474 0.9526 0.9579 0.9632 0.9684 0.9737 0.9789 0.9842 0.9895 0.9947 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.10.7.2 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0769 0.1538 0.2308 0.3077 0.3846 0.4615 0.5385 0.6154 0.6923 0.7692 0.8462 0.9231 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0667 0.1333 0.2000 0.2667 0.3333 0.4000 0.4667 0.5333 0.6000 0.6667 0.7333 0.8000 0.8667 0.9333 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0556 0.1111 0.1667 0.2222 0.2778 0.3333 0.3889 0.4444 0.5000 0.5556 0.6111 0.6667 0.7222 0.7778 0.8333 0.8889 0.9444 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0526 0.1053 0.1579 0.2105 0.2632 0.3158 0.3684 0.4211 0.4737 0.5263 0.5789 0.6316 0.6842 0.7368 0.7895 0.8421 0.8947 0.9474 1.0000 0.0000 0.0667 0.1333 0.2000 0.2667 0.3333 0.4000 0.4667 0.5333 0.6000 0.6667 0.7333 0.8000 0.8667 0.9333 1.0000 0.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.10.7.3 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY b ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0105 0.0105 0.0211 0.0211 0.0316 0.0368 0.0421 0.0474 0.0474 0.0474 0.0632 0.0632 0.0632 0.0789 0.0789 0.0789 0.0947 0.1000 0.1000 0.1105 0.1105 0.1105 0.1263 0.1263 0.1368 0.1421 0.1421 0.1421 0.1579 0.1579 0.1579 0.1737 0.1789 0.1842 0.1895 0.1895 0.2000 0.2000 0.2105 0.2105 0.2211 0.2263 0.2316 0.2316 0.2421 0.2421 0.2526 0.2579 0.2579 0.2579 0.2737 0.2737 0.2737 0.2895 0.2895 0.3000 0.3053 0.3053 0.3053 0.3053 0.3263 0.3263 0.3263 0.3263 0.3474 0.3474 0.3579 0.3579 0.3579 0.3579 0.3789 0.3789 0.3895 0.3895 0.4000 0.4000 0.4000 0.4158 0.4211 0.4211 0.4316 0.4368 0.4368 0.4474 0.4474 0.4579 0.4579 0.4684 0.4684 0.4684 0.4684 0.4895 0.4947 0.5000 0.5053 0.5105 0.5158 0.5211 0.5211 0.5316 0.5316 0.5316 0.5474 0.5526 0.5526 0.5526 0.5526 0.5737 0.5737 0.5737 0.5737 0.5947 0.6000 0.6000 0.6105 0.6105 0.6211 0.6263 0.6316 0.6316 0.6316 0.6474 0.6526 0.6579 0.6632 0.6684 0.6737 0.6737 0.6737 0.6895 0.6895 0.6895 0.7053 0.7053 0.7053 0.7211 0.7211 0.7211 0.7368 0.7421 0.7421 0.7526 0.7526 0.7632 0.7684 0.7684 0.7789 0.7789 0.7789 0.7947 0.8000 0.8053 0.8053 0.8053 0.8053 0.8263 0.8263 0.8263 0.8421 0.8474 0.8474 0.8579 0.8632 0.8632 0.8632 0.8789 0.8789 0.8789 0.8947 0.8947 0.8947 0.8947 0.8947 0.9211 0.9211 0.9211 0.9368 0.9421 0.9421 0.9421 0.9579 0.9579 0.9579 0.9737 0.9737 0.9842 0.9842 0.9947 0.9947} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.10.7.4 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0769 0.1538 0.1538 0.1538 0.3846 0.4615 0.5385 0.6154 0.6923 0.6923 0.8462 0.8462 0.8462 0.0000 0.0000 0.1000 0.1000 0.2000 0.2000 0.3000 0.3000 0.4000 0.4000 0.5000 0.5500 0.5500 0.6500 0.6500 0.6500 0.8000 0.8000 0.8000 0.8000 0.8000 0.0000 0.0000 0.1333 0.1333 0.1333 0.3333 0.3333 0.4667 0.5333 0.6000 0.6667 0.6667 0.8000 0.8000 0.8000 1.0000 0.0000 0.0000 0.1000 0.1000 0.2000 0.2000 0.3000 0.3000 0.3000 0.3000 0.5000 0.5000 0.6000 0.6500 0.7000 0.7000 0.7000 0.8500 0.9000 0.9000 0.9000 0.0000 0.0556 0.1111 0.1667 0.1667 0.1667 0.1667 0.3889 0.3889 0.5000 0.5556 0.6111 0.6111 0.6111 0.7778 0.7778 0.7778 0.7778 1.0000 0.0000 0.0500 0.0500 0.0500 0.2000 0.2500 0.2500 0.3500 0.3500 0.4500 0.4500 0.4500 0.6000 0.6000 0.6000 0.7500 0.7500 0.7500 0.9000 0.9000 0.9000 0.0000 0.0500 0.0500 0.0500 0.2000 0.2000 0.3000 0.3000 0.3000 0.3000 0.5000 0.5000 0.6000 0.6000 0.6000 0.7500 0.8000 0.8500 0.9000 0.9000 0.9000 0.0000 0.0000 0.0000 0.1579 0.2105 0.2105 0.3158 0.3158 0.4211 0.4211 0.4211 0.4211 0.6316 0.6842 0.7368 0.7368 0.8421 0.8421 0.9474 0.9474 0.0000 0.0000 0.0000 0.2000 0.2667 0.2667 0.4000 0.4000 0.4000 0.4000 0.6667 0.7333 0.7333 0.8667 0.9333 0.9333 0.0000 0.0000 0.0000 0.1429 0.1905 0.1905 0.1905 0.3333 0.3333 0.3333 0.4762 0.5238 0.5238 0.5238 0.5238 0.7143 0.7619 0.8095 0.8095 0.8095 0.9524 0.9524} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.10.7.5 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.10.7.6 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER (PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.10.8.1 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0052 0.0105 0.0157 0.0209 0.0262 0.0314 0.0366 0.0419 0.0471 0.0524 0.0576 0.0628 0.0681 0.0733 0.0785 0.0838 0.0890 0.0942 0.0995 0.1047 0.1099 0.1152 0.1204 0.1257 0.1309 0.1361 0.1414 0.1466 0.1518 0.1571 0.1623 0.1675 0.1728 0.1780 0.1832 0.1885 0.1937 0.1990 0.2042 0.2094 0.2147 0.2199 0.2251 0.2304 0.2356 0.2408 0.2461 0.2513 0.2565 0.2618 0.2670 0.2723 0.2775 0.2827 0.2880 0.2932 0.2984 0.3037 0.3089 0.3141 0.3194 0.3246 0.3298 0.3351 0.3403 0.3455 0.3508 0.3560 0.3613 0.3665 0.3717 0.3770 0.3822 0.3874 0.3927 0.3979 0.4031 0.4084 0.4136 0.4188 0.4241 0.4293 0.4346 0.4398 0.4450 0.4503 0.4555 0.4607 0.4660 0.4712 0.4764 0.4817 0.4869 0.4921 0.4974 0.5026 0.5079 0.5131 0.5183 0.5236 0.5288 0.5340 0.5393 0.5445 0.5497 0.5550 0.5602 0.5654 0.5707 0.5759 0.5812 0.5864 0.5916 0.5969 0.6021 0.6073 0.6126 0.6178 0.6230 0.6283 0.6335 0.6387 0.6440 0.6492 0.6545 0.6597 0.6649 0.6702 0.6754 0.6806 0.6859 0.6911 0.6963 0.7016 0.7068 0.7120 0.7173 0.7225 0.7277 0.7330 0.7382 0.7435 0.7487 0.7539 0.7592 0.7644 0.7696 0.7749 0.7801 0.7853 0.7906 0.7958 0.8010 0.8063 0.8115 0.8168 0.8220 0.8272 0.8325 0.8377 0.8429 0.8482 0.8534 0.8586 0.8639 0.8691 0.8743 0.8796 0.8848 0.8901 0.8953 0.9005 0.9058 0.9110 0.9162 0.9215 0.9267 0.9319 0.9372 0.9424 0.9476 0.9529 0.9581 0.9634 0.9686 0.9738 0.9791 0.9843 0.9895 0.9948 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.10.8.2 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0714 0.1429 0.2143 0.2857 0.3571 0.4286 0.5000 0.5714 0.6429 0.7143 0.7857 0.8571 0.9286 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0625 0.1250 0.1875 0.2500 0.3125 0.3750 0.4375 0.5000 0.5625 0.6250 0.6875 0.7500 0.8125 0.8750 0.9375 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0526 0.1053 0.1579 0.2105 0.2632 0.3158 0.3684 0.4211 0.4737 0.5263 0.5789 0.6316 0.6842 0.7368 0.7895 0.8421 0.8947 0.9474 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0625 0.1250 0.1875 0.2500 0.3125 0.3750 0.4375 0.5000 0.5625 0.6250 0.6875 0.7500 0.8125 0.8750 0.9375 1.0000 0.0455 0.0909 0.1364 0.1818 0.2273 0.2727 0.3182 0.3636 0.4091 0.4545 0.5000 0.5455 0.5909 0.6364 0.6818 0.7273 0.7727 0.8182 0.8636 0.9091 0.9545 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.10.8.3 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY b ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0105 0.0105 0.0209 0.0209 0.0314 0.0314 0.0366 0.0419 0.0471 0.0628 0.0628 0.0628 0.0785 0.0785 0.0785 0.0942 0.0942 0.0942 0.0995 0.1099 0.1099 0.1257 0.1257 0.1257 0.1361 0.1361 0.1414 0.1571 0.1571 0.1571 0.1728 0.1728 0.1728 0.1780 0.1832 0.1885 0.1990 0.1990 0.2094 0.2094 0.2199 0.2199 0.2251 0.2304 0.2408 0.2408 0.2513 0.2513 0.2565 0.2723 0.2723 0.2723 0.2880 0.2880 0.2880 0.2984 0.2984 0.3037 0.3246 0.3246 0.3246 0.3246 0.3455 0.3455 0.3455 0.3455 0.3560 0.3560 0.3770 0.3770 0.3770 0.3770 0.3874 0.3874 0.3979 0.3979 0.4136 0.4136 0.4136 0.4188 0.4293 0.4293 0.4346 0.4450 0.4450 0.4555 0.4555 0.4660 0.4660 0.4869 0.4869 0.4869 0.4869 0.4921 0.4974 0.5026 0.5079 0.5131 0.5183 0.5288 0.5288 0.5445 0.5445 0.5445 0.5497 0.5707 0.5707 0.5707 0.5707 0.5916 0.5916 0.5916 0.5916 0.5969 0.6073 0.6073 0.6178 0.6178 0.6230 0.6283 0.6440 0.6440 0.6440 0.6492 0.6545 0.6597 0.6649 0.6702 0.6859 0.6859 0.6859 0.7016 0.7016 0.7016 0.7173 0.7173 0.7173 0.7330 0.7330 0.7330 0.7382 0.7487 0.7487 0.7592 0.7592 0.7644 0.7749 0.7749 0.7906 0.7906 0.7906 0.7958 0.8010 0.8220 0.8220 0.8220 0.8220 0.8377 0.8377 0.8377 0.8429 0.8534 0.8534 0.8586 0.8743 0.8743 0.8743 0.8901 0.8901 0.8901 0.9162 0.9162 0.9162 0.9162 0.9162 0.9319 0.9319 0.9319 0.9372 0.9529 0.9529 0.9529 0.9686 0.9686 0.9686 0.9791 0.9791 0.9895 0.9895 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.10.8.4 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0714 0.1429 0.3571 0.3571 0.3571 0.4286 0.5000 0.5714 0.6429 0.7857 0.7857 1.0000 1.0000 1.0000 0.0952 0.0952 0.1905 0.1905 0.2857 0.2857 0.3810 0.3810 0.4762 0.4762 0.5238 0.6190 0.6190 0.7619 0.7619 0.7619 1.0000 1.0000 1.0000 1.0000 1.0000 0.1250 0.1250 0.3125 0.3125 0.3125 0.4375 0.4375 0.5000 0.5625 0.6250 0.7500 0.7500 0.9375 0.9375 0.9375 1.0000 0.0952 0.0952 0.1905 0.1905 0.2857 0.2857 0.4762 0.4762 0.4762 0.4762 0.5714 0.5714 0.6190 0.6667 0.8095 0.8095 0.8095 0.8571 1.0000 1.0000 1.0000 0.0526 0.1053 0.1579 0.3684 0.3684 0.3684 0.3684 0.4737 0.4737 0.5263 0.5789 0.7368 0.7368 0.7368 0.9474 0.9474 0.9474 0.9474 1.0000 0.0476 0.1905 0.1905 0.1905 0.2381 0.3333 0.3333 0.4286 0.4286 0.5714 0.5714 0.5714 0.7143 0.7143 0.7143 0.8571 0.8571 0.8571 1.0000 1.0000 1.0000 0.0476 0.1905 0.1905 0.1905 0.2857 0.2857 0.4762 0.4762 0.4762 0.4762 0.5714 0.5714 0.7143 0.7143 0.7143 0.7619 0.8095 0.8571 1.0000 1.0000 1.0000 0.1500 0.1500 0.1500 0.2000 0.3000 0.3000 0.4000 0.4000 0.6000 0.6000 0.6000 0.6000 0.6500 0.7000 0.8000 0.8000 0.9000 0.9000 1.0000 1.0000 0.1875 0.1875 0.1875 0.2500 0.3750 0.3750 0.6250 0.6250 0.6250 0.6250 0.6875 0.8125 0.8125 0.8750 1.0000 1.0000 0.1364 0.1364 0.1364 0.1818 0.3182 0.3182 0.3182 0.4545 0.4545 0.4545 0.5000 0.6818 0.6818 0.6818 0.6818 0.7273 0.7727 0.9091 0.9091 0.9091 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.10.8.5 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.10.8.6 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.10.8.1 { + set myres {} + foreach r [db eval {SELECT ntile(100) OVER ( ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 88.0000 89.0000 89.0000 90.0000 90.0000 91.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.10.8.2 { + set myres {} + foreach r [db eval {SELECT ntile(101) OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 22.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.10.8.3 { + set myres {} + foreach r [db eval {SELECT ntile(102) OVER ( ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 88.0000 89.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.10.8.4 { + set myres {} + foreach r [db eval {SELECT ntile(103) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 22.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.10.8.5 { + set myres {} + foreach r [db eval {SELECT ntile(104) OVER ( ORDER BY b%10,a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000 103.0000 104.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.10.8.6 { + set myres {} + foreach r [db eval {SELECT ntile(105) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.10.8.7 { + set myres {} + foreach r [db eval {SELECT ntile(105) OVER ( ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 88.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000 103.0000 104.0000 105.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + +do_execsql_test 1.10.9.1 { + SELECT last_value(a+b) OVER ( ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2 +} {52 83 55 79 109 65 26 120 119 50 70 103 80 124 + 36 96 59 124 116 110 57 51 52 130 103 74 87 48 + 128 117 105 136 131 71 133 92 109 63 84 109 57 + 146 78 147 113 74 88 150 87 110 65 121 106 110 + 124 85 145 107 161 171 150 156 80 171 120 109 158 + 114 111 136 147 87 173 124 168 173 162 132 101 154 + 167 190 161 110 156 195 198 102 123 177 169 140 111 + 180 119 160 197 152 124 121 134 146 147 132 213 141 + 193 200 210 157 132 136 175 161 218 188 226 191 187 + 208 211 179 138 144 223 196 214 170 212 202 163 184 + 172 173 195 229 240 187 210 200 163 227 228 223 191 + 252 235 225 243 172 187 202 179 179 182 231 261 207 + 263 206 189 209 212 276 181 274 249 239 234 213 234 + 269 196 271 221 210 229 235 250 223 232 229 279 224 + 280 216 207 207 207 207 207} + +do_execsql_test 1.10.9.2 { + SELECT last_value(a+b) OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2 +} {134 218 191 212 229 240 213 234 196 223 223 223 223 + 223 146 65 156 132 154 102 123 119 160 152 146 147 + 136 243 261 263 210 210 210 210 210 120 87 162 124 + 141 138 227 228 179 231 234 280 280 280 280 280 57 + 110 114 136 147 167 110 180 193 191 252 187 179 206 + 181 221 279 279 279 279 279 80 171 173 177 157 161 + 179 214 225 182 209 269 271 235 229 229 229 229 229 + 113 74 87 145 190 161 169 140 111 132 213 187 208 + 223 235 189 274 274 274 274 274 51 52 128 109 121 + 124 85 107 150 195 226 172 173 187 223 207 212 212 + 212 212 212 110 87 48 110 173 124 197 211 144 196 + 195 200 202 224 216 207 207 207 207 207 88 171 158 + 156 198 121 210 132 210 239 250 232 232 232 232 232 + 59 109 150 161 111 101 200 175 188 170 202 163 184 + 163 172 276 249 229 229 229 229 229} + +do_execsql_test 1.10.9.3 { + SELECT last_value(a+b) OVER ( ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2 +} {57 181 80 111 26 48 144 207 36 121 132 101 163 + 172 196 119 136 63 124 179 110 179 182 74 132 189 + 51 52 85 216 163 134 123 210 78 141 57 187 71 87 + 172 173 50 224 88 59 111 170 109 213 223 146 147 + 84 114 191 206 221 157 161 209 229 74 140 107 187 + 207 212 124 202 52 232 55 184 229 106 132 152 120 + 92 110 179 235 65 70 87 110 195 200 175 234 160 + 234 136 80 113 187 109 121 124 196 156 210 239 250 + 72 109 188 202 191 105 154 79 231 147 225 103 161 + 169 223 96 83 249 212 162 227 228 167 180 193 117 + 177 214 145 208 235 150 110 211 103 158 200 168 229 + 92 156 243 280 279 116 173 269 271 131 133 223 128 + 173 197 210 99 150 161 147 218 240 109 136 146 261 + 263 124 130 252 171 190 213 274 108 195 226 119 124 + 171 198 120 276 276 276 276 276} + +do_execsql_test 1.10.9.4 { + SELECT last_value(a+b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2 +} {223 106 234 191 212 168 229 147 218 240 240 240 240 + 240 123 210 146 147 132 152 160 105 154 92 156 243 + 109 136 146 261 263 263 263 263 263 179 78 141 84 + 120 234 79 231 162 227 228 280 280 280 280 280 57 + 187 114 191 206 221 92 110 136 147 167 180 193 279 + 124 130 252 252 252 252 252 161 209 229 179 235 80 + 225 117 177 214 116 173 269 271 171 171 171 171 171 + 87 74 140 113 187 103 161 169 145 208 235 131 133 + 223 190 213 274 274 274 274 274 172 173 107 187 207 + 212 65 70 109 121 124 223 150 128 108 195 226 226 + 226 226 226 50 224 124 202 87 110 195 200 196 96 + 110 211 173 197 119 124 124 124 124 124 52 232 156 + 210 239 250 83 103 158 210 171 198 198 198 198 198 + 59 111 170 55 184 229 175 72 109 188 202 249 200 + 99 150 161 120 276 276 276 276 276} + +do_execsql_test 1.10.9.5 { + SELECT last_value(a+b) OVER ( ORDER BY b%10,a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2 +} {134 218 191 212 229 240 213 234 196 223 92 109 105 + 136 146 65 156 132 154 102 123 119 160 152 146 147 + 136 243 261 263 210 79 63 84 78 120 87 162 124 + 141 138 227 228 179 231 234 280 124 57 130 92 57 + 110 114 136 147 167 110 180 193 191 252 187 179 206 + 181 221 279 80 116 117 71 80 171 173 177 157 161 + 179 214 225 182 209 269 271 235 229 103 74 131 133 + 113 74 87 145 190 161 169 140 111 132 213 187 208 + 223 235 189 274 108 65 26 70 51 52 128 109 121 + 124 85 107 150 195 226 172 173 187 223 207 212 119 + 50 124 96 110 87 48 110 173 124 197 211 144 196 + 195 200 202 224 216 207 52 83 103 36 88 171 158 + 156 198 121 210 132 210 239 250 232 99 72 55 120 + 59 109 150 161 111 101 200 175 188 170 202 163 184 + 163 172 276 249 229 229 229 229 229} + +do_execsql_test 1.10.9.6 { + SELECT last_value(a+b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING) FROM t2 +} {108 52 83 79 65 26 70 103 80 36 116 51 52 128 + 117 71 63 84 109 78 147 88 121 106 124 85 107 171 + 150 80 171 120 109 158 87 168 173 162 156 195 198 + 177 124 121 134 141 210 157 132 161 218 226 191 179 + 138 214 212 172 173 229 240 187 210 227 228 223 225 + 179 182 231 207 209 212 239 234 213 234 269 196 271 + 235 250 223 232 229 280 99 92 72 55 109 120 119 + 50 124 96 59 124 110 57 130 103 74 87 48 105 136 + 131 133 92 109 57 146 113 74 150 87 110 65 110 + 145 161 156 114 111 136 147 173 124 132 101 154 167 + 190 161 110 102 123 169 140 111 180 119 160 197 152 + 146 147 132 213 193 200 136 175 188 187 208 211 144 + 223 196 170 202 163 184 195 200 163 191 252 235 243 + 172 187 202 179 261 263 206 189 276 181 274 249 221 + 210 229 279 224 216 207} + +do_execsql_test 1.10.10.1 { + SELECT nth_value(b,b+1) OVER (ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} 39 {} {} {} {} + {} {} {} 91 {} {} {} {} {} 77 54 54 {} {} {} + {} 62 {} {} {} {} {} 23 {} 56 {} 97 86 {} 59 + {} 84 {} {} 78 65 {} 16 90 81 40 59 56 40 54 + {} 85 {} {} {} {} 38 {} 32 47 {} 74 35 47 98 + 96 {} 24 72 {} 29 12 46 36 53 {} 81 27 56 {} + {} 81 93 63 81 91 68 53 99 89 13 12 97 91 29 + 7 7 78 35 84 53 84 58 61 91 99 15 61 98 16 5 + 75 56 2 37 3 96 62 95 43 63 35 78 16 67 43 16 + 16 90 72 98 85 56 90 46 29 29 4 74 74 2 76 41 + 46 77 24 27 97 46 89 1 85 1 74 78 61 85 51 59 + 35 30 56 25 47 28 73 6 73 74 93 43 3 56 47 85 + 61 61 93 9 97 62} + +do_execsql_test 1.10.10.2 { + SELECT nth_value(b,b+1) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} 30 {} {} + {} {} {} {} 91 {} {} {} 91 {} 11 {} {} {} {} + 11 {} {} {} {} {} {} {} {} {} 32 {} {} {} 32 + {} {} 12 {} {} {} {} {} {} {} 43 {} {} {} {} + {} 33 {} {} {} {} {} 33 {} 43 {} {} {} {} {} + {} 4 {} {} {} {} {} {} {} {} 34 {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 15 55 + {} {} {} {} {} 55 {} {} {} 86 {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 47 {} {} {} {} {} 47 {} {} {} {} + {} 27 47 {} {} {} {} {} {} {} {} {} 98 {} 98 + {} {} {} {} {} {} {} {} {} {} {} {} {} 9 {} + {} {} {} {} 99 {} 9 9 {} {} {}} + +do_execsql_test 1.10.10.3 { + SELECT nth_value(b,b+1) OVER ( ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2 +} {1 1 2 2 2 2 3 3 4 5 5 5 6 6 6 7 7 7 7 + 7 7 8 8 8 8 8 8 9 9 9 9 9 9 9 11 11 12 + 12 12 12 12 12 13 13 14 14 15 15 15 15 15 15 + 16 16 16 16 16 16 17 17 17 17 19 19 19 19 20 + 20 21 21 21 21 21 21 22 22 22 22 22 23 23 23 + 24 25 25 26 26 27 27 27 27 27 27 29 29 29 30 + 30 30 31 31 31 31 31 32 33 33 33 33 33 33 33 + 33 33 33 33 34 34 34 34 34 34 34 35 35 36 36 + 36 37 37 37 37 37 37 38 38 38 38 38 38 39 39 + 39 39 39 40 41 41 41 41 41 42 43 43 43 43 43 + 44 44 44 44 46 46 46 47 47 47 47 47 47 47 47 + 47 47 47 49 49 49 50 51 51 51 52 52 52 53 53 + 54 54 55 55} + +do_execsql_test 1.10.10.4 { + SELECT nth_value(b,b+1) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} 1 1 + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 12 12 {} {} {} {} {} {} {} {} {} + {} {} {} {} {} 13 13 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} 34 {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.10.10.5 { + SELECT nth_value(b,b+1) OVER ( ORDER BY b%10,a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} 30 {} {} + {} {} {} {} 40 {} {} {} 40 41 50 {} {} 81 81 + 50 {} {} {} 41 {} 10 91 61 12 30 {} 10 61 30 + {} {} 10 {} 23 {} {} 1 {} 22 80 22 91 93 73 + {} 30 {} {} 91 {} 1 30 91 80 91 {} 43 {} 74 + 21 20 {} {} 74 21 21 2 74 33 81 21 64 64 2 21 + 93 62 14 14 3 91 11 24 55 93 93 62 90 91 55 3 + 24 14 24 91 55 15 72 60 72 61 61 34 43 43 43 + 61 12 4 15 15 51 51 12 23 12 12 25 41 25 13 + 94 12 70 12 84 32 84 94 70 33 12 12 32 41 91 + 70 22 33 84 80 31 75 84 53 75 80 84 80 53 53 + 53 22 44 63 42 95 31 63 44 44 31 90 74 52 63 + 31 63 1 42 90 90 95 3 42} + +do_execsql_test 1.10.10.6 { + SELECT nth_value(b,b+1) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.10.11.1 { + SELECT first_value(b) OVER (ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING) FROM t2 +} {89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89} + +do_execsql_test 1.10.11.2 { + SELECT first_value(b) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING) FROM t2 +} {90 90 90 90 90 90 90 90 90 90 90 90 90 90 81 + 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 + 81 81 81 81 81 62 62 62 62 62 62 62 62 62 62 + 62 62 62 62 62 62 93 93 93 93 93 93 93 93 93 + 93 93 93 93 93 93 93 93 93 93 93 93 54 54 54 + 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 + 54 65 65 65 65 65 65 65 65 65 65 65 65 65 65 + 65 65 65 65 65 65 65 96 96 96 96 96 96 96 96 + 96 96 96 96 96 96 96 96 96 96 96 96 96 97 97 + 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 + 97 97 97 38 38 38 38 38 38 38 38 38 38 38 38 + 38 38 38 38 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89} + +do_execsql_test 1.10.11.3 { + SELECT first_value(b) OVER ( ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1} + +do_execsql_test 1.10.11.4 { + SELECT first_value(b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2 +} {10 10 10 10 10 10 10 10 10 10 10 10 10 10 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 + 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 + 5 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 + 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 + 7 7 7 7 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 + 8 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 + 9 9 9 9} + +do_execsql_test 1.10.11.5 { + SELECT first_value(b) OVER ( ORDER BY b%10,a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2 +} {90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90} + +do_execsql_test 1.10.11.6 { + SELECT first_value(b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING) FROM t2 +} {96 38 68 62 46 6 46 78 54 8 84 16 16 86 74 24 + 12 32 56 22 90 28 56 40 56 16 36 98 76 4 94 + 42 30 78 2 80 84 72 58 96 98 74 12 8 20 22 88 + 34 8 34 90 96 60 44 2 74 70 26 26 80 90 36 58 + 72 72 66 64 12 14 62 36 34 36 58 52 30 50 84 + 10 84 44 58 30 38 34 82 89 81 59 39 91 99 97 + 27 97 67 29 93 77 23 93 65 35 47 7 61 91 85 + 85 43 59 3 91 55 15 89 25 47 1 43 75 89 81 33 + 29 53 63 87 37 41 9 61 73 95 65 13 1 21 65 35 + 5 73 11 51 87 41 31 31 15 95 73 79 11 49 59 + 55 75 77 7 85 57 29 59 19 39 47 47 9 33 93 75 + 81 9 23 37 13 91 91 33 15 99 3 95 69 33 21 39 + 83 27 17 7} + +do_execsql_test 1.10.12.1 { + SELECT lead(b,b) OVER (ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING) FROM t2 +} {96 9 11 89 32 53 91 30 51 56 54 73 22 59 75 + 74 78 8 16 65 15 8 31 87 90 12 32 96 74 76 37 + 85 90 15 35 2 60 36 75 9 51 47 63 51 90 26 42 + 26 8 76 80 90 37 87 56 79 5 87 8 2 39 73 64 + 36 90 72 78 36 73 51 33 20 41 2 26 37 33 8 14 + 33 81 55 1 9 12 39 64 87 72 34 82 21 34 99 62 + 74 41 69 22 75 27 58 8 79 77 26 26 55 {} 29 + 30 7 {} 66 55 2 34 64 {} 33 {} 44 84 {} {} 95 + 85 19 {} 83 {} 91 {} {} 9 50 91 33 34 {} {} + 84 {} 7 9 {} {} {} 44 {} {} {} {} 91 84 {} 95 + 95 52 {} {} {} {} {} 21 {} {} {} 58 {} {} {} + {} {} {} {} 83 {} {} {} {} {} {} {} {} {} {} + {} {} {} {}} + +do_execsql_test 1.10.12.2 { + SELECT lead(b,b) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 81 {} {} {} 21 {} {} {} {} {} {} + {} {} {} {} {} {} 62 {} {} {} 12 {} {} {} 72 + {} {} {} {} {} {} {} {} {} {} 53 {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 34 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} 95 {} {} {} {} {} {} 85 {} + {} {} {} {} {} {} {} {} {} 56 {} 36 {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 57 {} {} {} {} {} 7 {} {} {} {} + {} {} {} {} {} {} 8 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 9 {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.10.12.3 { + SELECT lead(b,b) OVER ( ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2 +} {1 2 3 3 5 6 7 8 8 9 9 10 11 12 12 13 13 + 14 15 16 16 17 19 20 21 22 23 24 25 26 27 27 + 28 29 30 31 32 33 33 33 34 34 35 36 36 36 37 + 38 39 39 40 41 42 43 43 44 46 47 47 47 49 50 + 52 53 54 55 56 56 57 58 58 58 59 59 59 60 61 + 62 62 64 65 65 67 69 70 72 72 73 74 74 75 75 + 75 77 78 80 81 81 83 84 84 85 85 85 87 88 89 + 89 89 90 90 91 91 91 93 93 94 95 95 96 97 97 + 98 99 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.10.12.4 { + SELECT lead(b,b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2 +} {80 {} {} {} {} {} {} {} {} {} {} {} {} {} 1 + 11 81 81 {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} 12 12 72 82 {} {} {} {} {} {} + {} {} {} {} {} {} 13 23 73 73 {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} 34 84 {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 35 85 85 95 {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} 36 86 96 96 {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 37 47 + 47 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} 58 58 68 {} {} {} {} {} {} {} {} {} + {} {} {} {} 39 49 59 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.10.12.5 { + SELECT lead(b,b) OVER ( ORDER BY b%10,a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2 +} {34 72 91 74 21 55 23 94 84 95 2 13 61 22 55 + 55 94 85 75 81 65 73 74 21 72 12 94 33 63 73 + 72 96 36 76 3 25 62 3 73 34 12 46 43 93 72 16 + 86 63 15 65 36 77 24 57 25 53 95 34 95 16 97 + 74 97 67 25 98 44 34 65 54 5 68 96 28 47 95 + 34 39 8 38 6 46 96 28 47 95 56 39 99 97 76 8 + 26 9 79 27 95 16 29 {} 58 58 77 85 56 {} 98 + 29 {} 19 96 {} {} 78 56 98 36 97 {} 89 89 29 + 47 78 {} {} {} 38 68 58 {} 58 38 {} 98 {} {} + {} 39 57 9 {} 79 {} {} 7 {} {} {} 9 29 38 78 + {} {} {} 8 39 {} {} {} {} 59 {} 99 {} {} {} + {} {} {} {} {} {} {} {} {} {} 9 {} {} {} {} + {} {} {} {} {} {} {} {}} + +do_execsql_test 1.10.12.6 { + SELECT lead(b,b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.10.13.1 { + SELECT lag(b,b) OVER (ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} 38 {} {} {} {} + {} {} {} 6 {} {} {} {} {} 81 46 6 {} {} {} {} + 23 {} {} {} {} {} 27 {} {} {} 35 6 {} 12 {} + 23 {} {} 61 84 {} 93 39 47 {} 54 46 96 56 {} + 16 {} {} {} {} 89 {} 16 43 {} 85 56 29 99 53 + {} 59 {} {} 91 59 53 84 99 {} 93 63 47 {} {} + 98 33 67 35 75 1 23 13 55 27 75 98 35 73 63 2 + 21 27 13 24 86 23 84 31 20 94 61 65 75 23 36 + 94 55 90 41 77 96 56 29 40 12 89 63 11 5 73 + 79 1 16 28 31 73 5 39 53 63 41 11 40 2 13 33 + 9 29 90 47 72 9 73 30 44 33 74 93 29 74 42 34 + 63 41 34 96 47 77 1 36 74 72 14 36 26 77 9 72 + 64 8 91 31 52 30} + +do_execsql_test 1.10.13.2 { + SELECT lag(b,b) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} 30 {} {} + {} {} {} {} 91 {} {} {} 61 {} 81 {} {} {} {} + 1 {} {} {} {} {} {} {} {} {} 22 {} {} {} 12 + {} {} 62 {} {} {} {} {} {} {} 23 {} {} {} {} + {} {} {} {} {} {} {} 43 {} 23 {} {} {} {} {} + {} 54 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 75 {} + {} {} {} {} {} 55 {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} 47 {} {} {} {} + {} 27 7 {} {} {} {} {} {} {} {} {} 68 {} 8 {} + {} {} {} {} {} {} {} {} {} {} {} {} 89 {} {} + {} {} {} {} {} 29 9 {} {} {}} + +do_execsql_test 1.10.13.3 { + SELECT lag(b,b) OVER ( ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2 +} {{} 1 1 1 1 2 2 2 2 2 2 3 3 3 4 4 5 6 6 + 6 7 7 7 7 7 8 8 8 8 8 8 9 9 9 9 9 9 9 + 9 9 9 10 10 10 10 11 11 11 11 11 12 12 12 12 + 13 13 13 13 13 14 15 15 15 15 16 16 16 16 16 + 17 19 20 20 21 21 21 21 22 22 22 22 23 23 23 + 23 23 24 23 24 24 25 26 26 26 26 26 26 26 26 + 26 26 26 27 27 27 27 28 29 29 29 29 30 30 30 + 30 30 30 31 31 31 31 31 32 32 32 32 32 32 31 + 32 33 33 33 33 33 33 34 34 34 34 34 34 34 34 + 35 35 35 35 35 36 36 36 36 36 36 36 37 37 37 + 38 38 38 38 38 38 39 39 39 39 40 40 41 41 42 + 43 42 43 43 43 43 44 44 44 46 46 46 47 47 47 + 47 47} + +do_execsql_test 1.10.13.4 { + SELECT lag(b,b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + 1 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.10.13.5 { + SELECT lag(b,b) OVER ( ORDER BY b%10,a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} 30 {} {} + {} {} {} {} 91 {} {} {} 61 80 81 {} {} {} {} + 1 {} {} {} 30 {} 21 90 61 {} 22 {} 11 41 12 + {} {} 62 {} {} {} {} 31 {} 50 23 30 21 90 {} + {} 62 {} {} 81 {} 22 43 62 23 32 {} 91 {} 90 + 93 54 {} {} 90 72 12 22 90 81 83 23 80 20 72 + 43 51 33 80 90 2 34 54 1 20 62 12 13 75 44 30 + 93 91 1 21 55 61 61 13 85 3 65 65 91 73 33 93 + 55 84 62 31 11 65 35 85 33 55 15 12 75 22 3 + 73 65 36 85 43 95 43 13 47 44 65 65 96 36 27 + 7 46 34 94 47 36 73 34 35 73 68 24 8 75 85 75 + 66 34 95 36 84 77 46 34 84 47 89 65 36 16 38 + 76 58 57 29 9 44 56 17} + +do_execsql_test 1.10.13.6 { + SELECT lag(b,b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.10.14.1 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING) FROM t2 +} {89.81.96.59.38 89.81.96.59.38.68 89.81.96.59.38.68.39 + 89.81.96.59.38.68.39.62 89.81.96.59.38.68.39.62.91 + 89.81.96.59.38.68.39.62.91.46 89.81.96.59.38.68.39.62.91.46.6 + 89.81.96.59.38.68.39.62.91.46.6.99 89.81.96.59.38.68.39.62.91.46.6.99.97 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7} + +do_execsql_test 1.10.14.2 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING) FROM t2 +} {90.40.30.80.20 90.40.30.80.20.90 90.40.30.80.20.90.60 + 90.40.30.80.20.90.60.70 90.40.30.80.20.90.60.70.80 + 90.40.30.80.20.90.60.70.80.90 90.40.30.80.20.90.60.70.80.90.30 + 90.40.30.80.20.90.60.70.80.90.30.50 + 90.40.30.80.20.90.60.70.80.90.30.50.10 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30 81.91.61.91.91 + 81.91.61.91.91.1 81.91.61.91.91.1.81 81.91.61.91.91.1.81.41 + 81.91.61.91.91.1.81.41.61 81.91.61.91.91.1.81.41.61.1 + 81.91.61.91.91.1.81.41.61.1.21 81.91.61.91.91.1.81.41.61.1.21.11 + 81.91.61.91.91.1.81.41.61.1.21.11.51 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 62.12.32.22.42 62.12.32.22.42.2 62.12.32.22.42.2.72 + 62.12.32.22.42.2.72.12 62.12.32.22.42.2.72.12.22 + 62.12.32.22.42.2.72.12.22.2 62.12.32.22.42.2.72.12.22.2.72 + 62.12.32.22.42.2.72.12.22.2.72.72 62.12.32.22.42.2.72.12.22.2.72.72.12 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 93.23.93.43.3 + 93.23.93.43.3.43 93.23.93.43.3.43.33 93.23.93.43.3.43.33.53 + 93.23.93.43.3.43.33.53.63 93.23.93.43.3.43.33.53.63.73 + 93.23.93.43.3.43.33.53.63.73.13 93.23.93.43.3.43.33.53.63.73.13.73 + 93.23.93.43.3.43.33.53.63.73.13.73.73 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 54.84.74.24.4 54.84.74.24.4.94 54.84.74.24.4.94.84 + 54.84.74.24.4.94.84.74 54.84.74.24.4.94.84.74.34 + 54.84.74.24.4.94.84.74.34.34 54.84.74.24.4.94.84.74.34.34.44 + 54.84.74.24.4.94.84.74.34.34.44.74 54.84.74.24.4.94.84.74.34.34.44.74.64 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 65.35.85.85.55 + 65.35.85.85.55.15 65.35.85.85.55.15.25 65.35.85.85.55.15.25.75 + 65.35.85.85.55.15.25.75.95 65.35.85.85.55.15.25.75.95.65 + 65.35.85.85.55.15.25.75.95.65.65 65.35.85.85.55.15.25.75.95.65.65.35 + 65.35.85.85.55.15.25.75.95.65.65.35.5 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 96.46.6.46.16 96.46.6.46.16.16 96.46.6.46.16.16.86 + 96.46.6.46.16.16.86.56 96.46.6.46.16.16.86.56.56 + 96.46.6.46.16.16.86.56.56.56 96.46.6.46.16.16.86.56.56.56.16 + 96.46.6.46.16.16.86.56.56.56.16.36 96.46.6.46.16.16.86.56.56.56.16.36.76 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 97.27.97.67.77 97.27.97.67.77.47 97.27.97.67.77.47.7 + 97.27.97.67.77.47.7.47 97.27.97.67.77.47.7.47.87 + 97.27.97.67.77.47.7.47.87.37 97.27.97.67.77.47.7.47.87.37.87 + 97.27.97.67.77.47.7.47.87.37.87.77 97.27.97.67.77.47.7.47.87.37.87.77.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 38.68.78.8.28 + 38.68.78.8.28.98 38.68.78.8.28.98.78 38.68.78.8.28.98.78.58 + 38.68.78.8.28.98.78.58.98 38.68.78.8.28.98.78.58.98.8 + 38.68.78.8.28.98.78.58.98.8.88 38.68.78.8.28.98.78.58.98.8.88.8 + 38.68.78.8.28.98.78.58.98.8.88.8.58 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 89.59.39.99.29 + 89.59.39.99.29.59 89.59.39.99.29.59.89 89.59.39.99.29.59.89.89 + 89.59.39.99.29.59.89.89.29 89.59.39.99.29.59.89.89.29.9 + 89.59.39.99.29.59.89.89.29.9.79 89.59.39.99.29.59.89.89.29.9.79.49 + 89.59.39.99.29.59.89.89.29.9.79.49.59 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39} + +do_execsql_test 1.10.14.3 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2 +} {1.1.2.2.3 1.1.2.2.3.3 1.1.2.2.3.3.4 1.1.2.2.3.3.4.5 + 1.1.2.2.3.3.4.5.6 1.1.2.2.3.3.4.5.6.7 1.1.2.2.3.3.4.5.6.7.7 + 1.1.2.2.3.3.4.5.6.7.7.7 1.1.2.2.3.3.4.5.6.7.7.7.8 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99} + +do_execsql_test 1.10.14.4 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2 +} {10.20.30.30.30 10.20.30.30.30.40 10.20.30.30.30.40.50 + 10.20.30.30.30.40.50.60 10.20.30.30.30.40.50.60.70 + 10.20.30.30.30.40.50.60.70.80 10.20.30.30.30.40.50.60.70.80.80 + 10.20.30.30.30.40.50.60.70.80.80.90 + 10.20.30.30.30.40.50.60.70.80.80.90.90 + 10.20.30.30.30.40.50.60.70.80.80.90.90.90 + 10.20.30.30.30.40.50.60.70.80.80.90.90.90 + 10.20.30.30.30.40.50.60.70.80.80.90.90.90 + 10.20.30.30.30.40.50.60.70.80.80.90.90.90 + 10.20.30.30.30.40.50.60.70.80.80.90.90.90 1.1.11.11.21 1.1.11.11.21.21 + 1.1.11.11.21.21.31 1.1.11.11.21.21.31.31 1.1.11.11.21.21.31.31.41 + 1.1.11.11.21.21.31.31.41.41 1.1.11.11.21.21.31.31.41.41.51 + 1.1.11.11.21.21.31.31.41.41.51.61 1.1.11.11.21.21.31.31.41.41.51.61.61 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 2.2.12.12.12 + 2.2.12.12.12.22 2.2.12.12.12.22.22 2.2.12.12.12.22.22.32 + 2.2.12.12.12.22.22.32.42 2.2.12.12.12.22.22.32.42.52 + 2.2.12.12.12.22.22.32.42.52.62 2.2.12.12.12.22.22.32.42.52.62.62 + 2.2.12.12.12.22.22.32.42.52.62.62.72 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 3.3.13.13.23 + 3.3.13.13.23.23 3.3.13.13.23.23.33 3.3.13.13.23.23.33.33 + 3.3.13.13.23.23.33.33.33 3.3.13.13.23.23.33.33.33.33 + 3.3.13.13.23.23.33.33.33.33.43 3.3.13.13.23.23.33.33.33.33.43.43 + 3.3.13.13.23.23.33.33.33.33.43.43.53 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 4.14.24.34.34 4.14.24.34.34.34 4.14.24.34.34.34.34 + 4.14.24.34.34.34.34.44 4.14.24.34.34.34.34.44.44 + 4.14.24.34.34.34.34.44.44.54 4.14.24.34.34.34.34.44.44.54.64 + 4.14.24.34.34.34.34.44.44.54.64.74 4.14.24.34.34.34.34.44.44.54.64.74.74 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 5.15.15.15.25 + 5.15.15.15.25.35 5.15.15.15.25.35.35 5.15.15.15.25.35.35.55 + 5.15.15.15.25.35.35.55.55 5.15.15.15.25.35.35.55.55.65 + 5.15.15.15.25.35.35.55.55.65.65 5.15.15.15.25.35.35.55.55.65.65.65 + 5.15.15.15.25.35.35.55.55.65.65.65.75 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 6.16.16.16.26 6.16.16.16.26.26 6.16.16.16.26.26.36 + 6.16.16.16.26.26.36.36 6.16.16.16.26.26.36.36.36 + 6.16.16.16.26.26.36.36.36.36 6.16.16.16.26.26.36.36.36.36.46 + 6.16.16.16.26.26.36.36.36.36.46.46 6.16.16.16.26.26.36.36.36.36.46.46.56 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 7.7.7.17.27 + 7.7.7.17.27.27 7.7.7.17.27.27.37 7.7.7.17.27.27.37.37 + 7.7.7.17.27.27.37.37.47 7.7.7.17.27.27.37.37.47.47 + 7.7.7.17.27.27.37.37.47.47.47 7.7.7.17.27.27.37.37.47.47.47.47 + 7.7.7.17.27.27.37.37.47.47.47.47.57 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 8.8.8.28.38 + 8.8.8.28.38.38 8.8.8.28.38.38.58 8.8.8.28.38.38.58.58 + 8.8.8.28.38.38.58.58.58 8.8.8.28.38.38.58.58.58.58 + 8.8.8.28.38.38.58.58.58.58.68 8.8.8.28.38.38.58.58.58.58.68.78 + 8.8.8.28.38.38.58.58.58.58.68.78.78 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 9.9.9.19.29 9.9.9.19.29.29 + 9.9.9.19.29.29.29 9.9.9.19.29.29.29.39 9.9.9.19.29.29.29.39.39 + 9.9.9.19.29.29.29.39.39.39 9.9.9.19.29.29.29.39.39.39.49 + 9.9.9.19.29.29.29.39.39.39.49.59 9.9.9.19.29.29.29.39.39.39.49.59.59 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99} + +do_execsql_test 1.10.14.5 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( ORDER BY b%10,a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING ) FROM t2 +} {90.40.30.80.20 90.40.30.80.20.90 90.40.30.80.20.90.60 + 90.40.30.80.20.90.60.70 90.40.30.80.20.90.60.70.80 + 90.40.30.80.20.90.60.70.80.90 90.40.30.80.20.90.60.70.80.90.30 + 90.40.30.80.20.90.60.70.80.90.30.50 + 90.40.30.80.20.90.60.70.80.90.30.50.10 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39} + +do_execsql_test 1.10.14.6 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING) FROM t2 +} {96 38 68 62 46 6 46 78 54 8 84 16 16 86 74 24 + 12 32 56 22 90 28 56 40 56 16 36 98 76 4 94 + 42 30 78 2 80 84 72 58 96 98 74 12 8 20 22 88 + 34 8 34 90 96 60 44 2 74 70 26 26 80 90 36 58 + 72 72 66 64 12 14 62 36 34 36 58 52 30 50 84 + 10 84 44 58 30 38 34 82 89 81 59 39 91 99 97 + 27 97 67 29 93 77 23 93 65 35 47 7 61 91 85 + 85 43 59 3 91 55 15 89 25 47 1 43 75 89 81 33 + 29 53 63 87 37 41 9 61 73 95 65 13 1 21 65 35 + 5 73 11 51 87 41 31 31 15 95 73 79 11 49 59 + 55 75 77 7 85 57 29 59 19 39 47 47 9 33 93 75 + 81 9 23 37 13 91 91 33 15 99 3 95 69 33 21 39 + 83 27 17 7} + +do_execsql_test 1.10.14.7 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (win1 ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING) + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a) + ORDER BY 1 +} {1 1 10 11 11 12 12 12 13 13 14 15 15 15 16 16 + 16 17 19 2 2 20 21 21 22 22 23 23 24 25 26 26 + 27 27 28 29 29 29 3 3 30 30 30 31 31 32 33 33 + 33 33 34 34 34 34 35 35 36 36 36 36 37 37 38 + 38 39 39 39 4 40 41 41 42 43 43 44 44 46 46 + 47 47 47 47 49 5 50 51 52 53 54 55 55 56 56 + 56 57 58 58 58 58 59 59 59 59 6 60 61 61 62 + 62 63 64 65 65 65 66 67 68 69 7 7 7 70 72 72 + 72 73 73 73 74 74 74 75 75 75 76 77 77 78 78 + 79 8 8 8 80 80 81 81 81 82 83 84 84 84 84 85 + 85 85 86 87 87 88 89 89 89 9 9 9 90 90 90 91 + 91 91 91 91 93 93 93 94 95 95 95 96 96 96 97 + 97 98 98 99 99} + +do_execsql_test 1.10.14.8 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (win1 ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING) + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a ORDER BY b%10) + ORDER BY 1 +} {1 1 10 11 11 12 12 12 13 13 14 15 15 15 16 16 + 16 17 19 2 2 20 21 21 22 22 23 23 24 25 26 26 + 27 27 28 29 29 29 3 3 30 30 30 31 31 32 33 33 + 33 33 34 34 34 34 35 35 36 36 36 36 37 37 38 + 38 39 39 39 4 40 41 41 42 43 43 44 44 46 46 + 47 47 47 47 49 5 50 51 52 53 54 55 55 56 56 + 56 57 58 58 58 58 59 59 59 59 6 60 61 61 62 + 62 63 64 65 65 65 66 67 68 69 7 7 7 70 72 72 + 72 73 73 73 74 74 74 75 75 75 76 77 77 78 78 + 79 8 8 8 80 80 81 81 81 82 83 84 84 84 84 85 + 85 85 86 87 87 88 89 89 89 9 9 9 90 90 90 91 + 91 91 91 91 93 93 93 94 95 95 95 96 96 96 97 + 97 98 98 99 99} + +do_execsql_test 1.10.14.9 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER win2 + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a ORDER BY b%10), + win2 AS (win1 ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING) + ORDER BY 1 +} {1 1 10 11 11 12 12 12 13 13 14 15 15 15 16 16 + 16 17 19 2 2 20 21 21 22 22 23 23 24 25 26 26 + 27 27 28 29 29 29 3 3 30 30 30 31 31 32 33 33 + 33 33 34 34 34 34 35 35 36 36 36 36 37 37 38 + 38 39 39 39 4 40 41 41 42 43 43 44 44 46 46 + 47 47 47 47 49 5 50 51 52 53 54 55 55 56 56 + 56 57 58 58 58 58 59 59 59 59 6 60 61 61 62 + 62 63 64 65 65 65 66 67 68 69 7 7 7 70 72 72 + 72 73 73 73 74 74 74 75 75 75 76 77 77 78 78 + 79 8 8 8 80 80 81 81 81 82 83 84 84 84 84 85 + 85 85 86 87 87 88 89 89 89 9 9 9 90 90 90 91 + 91 91 91 91 93 93 93 94 95 95 95 96 96 96 97 + 97 98 98 99 99} + +do_execsql_test 1.10.15.1 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE a%2=0) OVER win FROM t2 + WINDOW win AS (ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING) +} {5 89.96.38 6 89.96.38 7 89.96.38.39 8 89.96.38.39 9 89.96.38.39.91 + 10 89.96.38.39.91 11 89.96.38.39.91.6 12 89.96.38.39.91.6 + 13 89.96.38.39.91.6.97 14 89.96.38.39.91.6.97 15 89.96.38.39.91.6.97.46 + 16 89.96.38.39.91.6.97.46 17 89.96.38.39.91.6.97.46.54 + 18 89.96.38.39.91.6.97.46.54 19 89.96.38.39.91.6.97.46.54.8 + 20 89.96.38.39.91.6.97.46.54.8 21 89.96.38.39.91.6.97.46.54.8.29 + 22 89.96.38.39.91.6.97.46.54.8.29 23 89.96.38.39.91.6.97.46.54.8.29.84 + 24 89.96.38.39.91.6.97.46.54.8.29.84 + 25 89.96.38.39.91.6.97.46.54.8.29.84.23 + 26 89.96.38.39.91.6.97.46.54.8.29.84.23 + 27 89.96.38.39.91.6.97.46.54.8.29.84.23.16 + 28 89.96.38.39.91.6.97.46.54.8.29.84.23.16 + 29 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65 + 30 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65 + 31 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47 + 32 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47 + 33 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86 + 34 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86 + 35 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61 + 36 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61 + 37 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85 + 38 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85 + 39 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85 + 40 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85 + 41 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59 + 42 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59 + 43 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32 + 44 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32 + 45 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3 + 46 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3 + 47 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22 + 48 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22 + 49 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55 + 50 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55 + 51 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28 + 52 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28 + 53 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25 + 54 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25 + 55 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1 + 56 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1 + 57 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40 + 58 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40 + 59 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56 + 60 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56 + 61 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75 + 62 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75 + 63 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89 + 64 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89 + 65 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76 + 66 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76 + 67 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4 + 68 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4 + 69 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42 + 70 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42 + 71 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78 + 72 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78 + 73 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29 + 74 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29 + 75 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63 + 76 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63 + 77 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87 + 78 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87 + 79 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80 + 80 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80 + 81 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72 + 82 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72 + 83 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9 + 84 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9 + 85 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73 + 86 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73 + 87 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65 + 88 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65 + 89 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58 + 90 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58 + 91 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98 + 92 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98 + 93 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21 + 94 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21 + 95 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65 + 96 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65 + 97 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5 + 98 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5 + 99 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11 + 100 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11 + 101 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87 + 102 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87 + 103 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12 + 104 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12 + 105 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20 + 106 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20 + 107 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31 + 108 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31 + 109 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95 + 110 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95 + 111 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73 + 112 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73 + 113 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88 + 114 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88 + 115 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8 + 116 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8 + 117 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49 + 118 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49 + 119 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90 + 120 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90 + 121 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96 + 122 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96 + 123 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55 + 124 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55 + 125 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77 + 126 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77 + 127 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2 + 128 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2 + 129 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85 + 130 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85 + 131 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74 + 132 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74 + 133 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70 + 134 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70 + 135 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19 + 136 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19 + 137 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26 + 138 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26 + 139 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47 + 140 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47 + 141 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90 + 142 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90 + 143 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58 + 144 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58 + 145 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9 + 146 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9 + 147 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72 + 148 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72 + 149 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33 + 150 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33 + 151 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75 + 152 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75 + 153 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81 + 154 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81 + 155 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23 + 156 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23 + 157 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13 + 158 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13 + 159 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14 + 160 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14 + 161 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91 + 162 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91 + 163 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91 + 164 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91 + 165 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15 + 166 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15 + 167 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36 + 168 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36 + 169 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3 + 170 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3 + 171 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69 + 172 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69 + 173 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52 + 174 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52 + 175 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50 + 176 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50 + 177 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10 + 178 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10 + 179 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33 + 180 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33 + 181 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39 + 182 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39 + 183 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58 + 184 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58 + 185 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38 + 186 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38 + 187 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83 + 188 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83 + 189 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82 + 190 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7} + +do_execsql_test 1.10.15.2 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE 0=1) OVER win FROM t2 + WINDOW win AS (ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING) +} {5 {} 6 {} 7 {} 8 {} 9 {} 10 {} 11 {} 12 {} 13 {} 14 {} + 15 {} 16 {} 17 {} 18 {} 19 {} 20 {} 21 {} 22 {} 23 {} + 24 {} 25 {} 26 {} 27 {} 28 {} 29 {} 30 {} 31 {} 32 {} + 33 {} 34 {} 35 {} 36 {} 37 {} 38 {} 39 {} 40 {} 41 {} + 42 {} 43 {} 44 {} 45 {} 46 {} 47 {} 48 {} 49 {} 50 {} + 51 {} 52 {} 53 {} 54 {} 55 {} 56 {} 57 {} 58 {} 59 {} + 60 {} 61 {} 62 {} 63 {} 64 {} 65 {} 66 {} 67 {} 68 {} + 69 {} 70 {} 71 {} 72 {} 73 {} 74 {} 75 {} 76 {} 77 {} + 78 {} 79 {} 80 {} 81 {} 82 {} 83 {} 84 {} 85 {} 86 {} + 87 {} 88 {} 89 {} 90 {} 91 {} 92 {} 93 {} 94 {} 95 {} + 96 {} 97 {} 98 {} 99 {} 100 {} 101 {} 102 {} 103 {} 104 {} + 105 {} 106 {} 107 {} 108 {} 109 {} 110 {} 111 {} 112 {} + 113 {} 114 {} 115 {} 116 {} 117 {} 118 {} 119 {} 120 {} + 121 {} 122 {} 123 {} 124 {} 125 {} 126 {} 127 {} 128 {} + 129 {} 130 {} 131 {} 132 {} 133 {} 134 {} 135 {} 136 {} + 137 {} 138 {} 139 {} 140 {} 141 {} 142 {} 143 {} 144 {} + 145 {} 146 {} 147 {} 148 {} 149 {} 150 {} 151 {} 152 {} + 153 {} 154 {} 155 {} 156 {} 157 {} 158 {} 159 {} 160 {} + 161 {} 162 {} 163 {} 164 {} 165 {} 166 {} 167 {} 168 {} + 169 {} 170 {} 171 {} 172 {} 173 {} 174 {} 175 {} 176 {} + 177 {} 178 {} 179 {} 180 {} 181 {} 182 {} 183 {} 184 {} + 185 {} 186 {} 187 {} 188 {} 189 {} 190 {} 191 {} 191 {} + 191 {} 191 {} 191 {}} + +do_execsql_test 1.10.15.3 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE 1=0) OVER win FROM t2 + WINDOW win AS (PARTITION BY (a%10) ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING) +} {5 {} 6 {} 7 {} 8 {} 9 {} 10 {} 11 {} 12 {} 13 {} 14 {} + 15 {} 16 {} 17 {} 18 {} 19 {} 20 {} 20 {} 20 {} 20 {} + 20 {} 5 {} 6 {} 7 {} 8 {} 9 {} 10 {} 11 {} 12 {} 13 {} + 14 {} 15 {} 16 {} 17 {} 18 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 5 {} 6 {} 7 {} 8 {} 9 {} 10 {} 11 {} 12 {} 13 {} + 14 {} 15 {} 16 {} 17 {} 18 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 5 {} 6 {} 7 {} 8 {} 9 {} 10 {} 11 {} 12 {} 13 {} + 14 {} 15 {} 16 {} 17 {} 18 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 5 {} 6 {} 7 {} 8 {} 9 {} 10 {} 11 {} 12 {} 13 {} + 14 {} 15 {} 16 {} 17 {} 18 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 5 {} 6 {} 7 {} 8 {} 9 {} 10 {} 11 {} 12 {} 13 {} + 14 {} 15 {} 16 {} 17 {} 18 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 5 {} 6 {} 7 {} 8 {} 9 {} 10 {} 11 {} 12 {} 13 {} + 14 {} 15 {} 16 {} 17 {} 18 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 5 {} 6 {} 7 {} 8 {} 9 {} 10 {} 11 {} 12 {} 13 {} + 14 {} 15 {} 16 {} 17 {} 18 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 5 {} 6 {} 7 {} 8 {} 9 {} 10 {} 11 {} 12 {} 13 {} + 14 {} 15 {} 16 {} 17 {} 18 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 5 {} 6 {} 7 {} 8 {} 9 {} 10 {} 11 {} 12 {} 13 {} + 14 {} 15 {} 16 {} 17 {} 18 {} 19 {} 19 {} 19 {} 19 {} + 19 {}} + +do_execsql_test 1.10.15.4 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE a%2=0) OVER win FROM t2 + WINDOW win AS (PARTITION BY (a%10) ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 4 FOLLOWING) +} {5 89.6.29.47.59 6 89.6.29.47.59.28 7 89.6.29.47.59.28.75 + 8 89.6.29.47.59.28.75.78 9 89.6.29.47.59.28.75.78.72 + 10 89.6.29.47.59.28.75.78.72.98 11 89.6.29.47.59.28.75.78.72.98.87 + 12 89.6.29.47.59.28.75.78.72.98.87.73 + 13 89.6.29.47.59.28.75.78.72.98.87.73.96 + 14 89.6.29.47.59.28.75.78.72.98.87.73.96.74 + 15 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90 + 16 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75 + 17 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91 + 18 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69 + 19 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39 + 20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 5 {} 6 {} + 7 {} 8 {} 9 {} 10 {} 11 {} 12 {} 13 {} 14 {} 15 {} 16 {} + 17 {} 18 {} 19 {} 19 {} 19 {} 19 {} 19 {} 5 96.97.84.86.32 + 6 96.97.84.86.32.25 7 96.97.84.86.32.25.89 8 96.97.84.86.32.25.89.29 + 9 96.97.84.86.32.25.89.29.9 10 96.97.84.86.32.25.89.29.9.21 + 11 96.97.84.86.32.25.89.29.9.21.12 12 96.97.84.86.32.25.89.29.9.21.12.88 + 13 96.97.84.86.32.25.89.29.9.21.12.88.55 + 14 96.97.84.86.32.25.89.29.9.21.12.88.55.70 + 15 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58 + 16 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81 + 17 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91 + 18 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52 + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 5 {} 6 {} + 7 {} 8 {} 9 {} 10 {} 11 {} 12 {} 13 {} 14 {} 15 {} 16 {} + 17 {} 18 {} 19 {} 19 {} 19 {} 19 {} 19 {} 5 38.46.23.61.3 + 6 38.46.23.61.3.1 7 38.46.23.61.3.1.76 8 38.46.23.61.3.1.76.63 + 9 38.46.23.61.3.1.76.63.73 10 38.46.23.61.3.1.76.63.73.65 + 11 38.46.23.61.3.1.76.63.73.65.20 12 38.46.23.61.3.1.76.63.73.65.20.8 + 13 38.46.23.61.3.1.76.63.73.65.20.8.77 + 14 38.46.23.61.3.1.76.63.73.65.20.8.77.19 + 15 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9 + 16 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23 + 17 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15 + 18 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50 + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 5 {} 6 {} + 7 {} 8 {} 9 {} 10 {} 11 {} 12 {} 13 {} 14 {} 15 {} 16 {} + 17 {} 18 {} 19 {} 19 {} 19 {} 19 {} 19 {} 5 39.54.16.85.22 + 6 39.54.16.85.22.40 7 39.54.16.85.22.40.4 8 39.54.16.85.22.40.4.87 + 9 39.54.16.85.22.40.4.87.65 10 39.54.16.85.22.40.4.87.65.5 + 11 39.54.16.85.22.40.4.87.65.5.31 12 39.54.16.85.22.40.4.87.65.5.31.49 + 13 39.54.16.85.22.40.4.87.65.5.31.49.2 + 14 39.54.16.85.22.40.4.87.65.5.31.49.2.26 + 15 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72 + 16 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13 + 17 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36 + 18 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10 + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 5 {} 6 {} + 7 {} 8 {} 9 {} 10 {} 11 {} 12 {} 13 {} 14 {} 15 {} 16 {} + 17 {} 18 {} 19 {} 19 {} 19 {} 19 {} 19 {} 5 91.8.65.85.55 + 6 91.8.65.85.55.56 7 91.8.65.85.55.56.42 8 91.8.65.85.55.56.42.80 + 9 91.8.65.85.55.56.42.80.58 10 91.8.65.85.55.56.42.80.58.11 + 11 91.8.65.85.55.56.42.80.58.11.95 12 91.8.65.85.55.56.42.80.58.11.95.90 + 13 91.8.65.85.55.56.42.80.58.11.95.90.85 + 14 91.8.65.85.55.56.42.80.58.11.95.90.85.47 + 15 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33 + 16 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14 + 17 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3 + 18 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33 + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 5 {} 6 {} + 7 {} 8 {} 9 {} 10 {} 11 {} 12 {} 13 {} 14 {} 15 {} 16 {} + 17 {} 18 {} 19 {} 19 {} 19 {} 19 {} 19 {}} + +do_execsql_test 1.11.2.1 { + SELECT max(b) OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2 +} {96 96 96 96 96 96 96 91 91 99 99 99 99 99 99 + 99 97 97 97 97 97 97 93 93 93 93 93 93 93 93 + 93 93 86 91 91 91 91 91 91 91 85 85 85 91 91 + 91 91 91 91 91 90 90 89 89 89 89 56 56 75 75 + 89 98 98 98 98 98 98 98 94 94 94 94 78 78 87 + 87 87 87 87 87 87 84 84 95 95 95 95 96 98 98 + 98 98 98 98 98 74 74 74 87 87 87 87 87 87 87 + 41 95 95 95 95 95 95 95 88 88 88 90 90 96 96 + 96 96 96 96 96 77 85 85 85 85 85 85 85 74 74 + 70 70 80 90 90 90 90 90 90 90 72 72 93 93 93 + 93 93 93 93 81 81 81 62 91 91 91 91 91 91 91 + 99 99 99 99 99 99 99 95 95 84 84 84 84 84 84 + 84 84 58 58 83 83 83 83 83 83 83} + +do_execsql_test 1.11.2.2 { + SELECT min(b) OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2 +} {81 59 38 38 38 38 38 38 6 6 6 6 6 6 6 27 8 + 8 8 8 8 8 8 16 16 16 16 16 16 7 7 7 7 7 7 + 7 24 24 24 12 12 12 3 3 3 3 3 3 3 15 15 15 + 1 1 1 1 1 1 1 16 16 16 16 16 4 4 4 4 4 4 + 4 29 29 2 2 2 2 2 2 2 9 9 9 9 9 9 9 13 + 13 1 1 1 1 1 1 1 5 5 5 5 5 8 8 8 8 8 8 + 8 15 15 15 15 8 8 8 8 8 8 8 11 34 34 55 44 + 2 2 2 2 2 2 2 7 19 19 19 19 19 19 19 26 26 + 26 9 9 9 9 9 9 9 33 33 9 9 9 9 9 9 9 12 + 12 12 12 14 15 15 15 3 3 3 3 3 3 3 30 10 10 + 10 10 10 10 10 21 21 21 30 27 27 17 7 7 7} + +do_execsql_test 1.11.3.1 { + SELECT row_number() OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.11.3.2 { + SELECT row_number() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.11.3.3 { + SELECT row_number() OVER ( ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.11.4.1 { + SELECT dense_rank() OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.11.4.2 { + SELECT dense_rank() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.11.4.3 { + SELECT dense_rank() OVER ( ORDER BY b ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2 +} {1 1 2 2 3 3 4 5 6 7 7 7 8 8 8 9 9 9 10 + 11 11 12 12 12 13 13 14 15 15 15 16 16 16 17 + 18 19 20 20 21 21 22 22 23 24 25 25 26 26 27 + 28 28 28 29 29 29 30 30 31 32 32 32 32 33 33 + 33 33 34 34 35 35 35 35 36 36 37 37 38 38 38 + 39 40 40 41 42 42 43 43 44 44 45 45 45 45 46 + 47 48 49 50 51 52 52 53 53 53 54 55 55 55 55 + 56 56 56 56 57 58 58 59 59 60 61 62 62 62 63 + 64 65 66 67 68 68 68 69 69 69 70 70 70 71 71 + 71 72 73 73 74 74 75 76 76 77 77 77 78 79 80 + 80 80 80 81 81 81 82 83 83 84 85 85 85 86 86 + 86 87 87 87 87 87 88 88 88 89 90 90 90 91 91 + 91 92 92 93 93 94 94} + +do_execsql_test 1.11.4.4 { + SELECT dense_rank() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2 +} {1 2 3 3 3 4 5 6 7 8 8 9 9 9 1 1 2 2 3 + 3 4 4 5 5 6 7 7 8 8 8 9 9 9 9 9 1 1 2 + 2 2 3 3 4 5 6 7 7 8 8 8 9 1 1 2 2 3 3 + 4 4 4 4 5 5 6 7 8 8 8 9 10 10 10 1 2 3 + 4 4 4 4 5 5 6 7 8 8 8 9 9 9 9 10 1 2 2 + 2 3 4 4 5 5 6 6 6 7 7 7 8 8 8 9 9 9 1 + 2 2 2 3 3 4 4 4 4 5 5 6 6 6 7 8 9 10 10 + 10 1 1 1 2 3 3 4 4 5 5 5 5 6 7 8 8 9 9 + 10 10 1 1 1 2 3 3 4 4 4 4 5 6 6 7 8 8 1 + 1 1 2 3 3 3 4 4 4 5 6 6 6 6 7 8 9 9 9 + 10 10} + +do_execsql_test 1.11.4.5 { + SELECT dense_rank() OVER ( ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 + 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 + 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 + 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 + 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 + 8 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 10 10 + 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 + 10 10 10 10 10} + +do_execsql_test 1.11.4.6 { + SELECT dense_rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 + 5 5 5 5 5 5 5 5 5 5 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 + 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 + 5} + +do_execsql_test 1.11.5.1 { + SELECT rank() OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.11.5.2 { + SELECT rank() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.11.5.3 { + SELECT rank() OVER ( ORDER BY b ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2 +} {1 1 3 3 5 5 7 8 9 10 10 10 13 13 13 16 16 + 16 19 20 20 22 22 22 25 25 27 28 28 28 31 31 + 31 34 35 36 37 37 39 39 41 41 43 44 45 45 47 + 47 49 50 50 50 53 53 53 56 56 58 59 59 59 59 + 63 63 63 63 67 67 69 69 69 69 73 73 75 75 77 + 77 77 80 81 81 83 84 84 86 86 88 88 90 90 90 + 90 94 95 96 97 98 99 100 100 102 102 102 105 106 + 106 106 106 110 110 110 110 114 115 115 117 117 119 + 120 121 121 121 124 125 126 127 128 129 129 129 132 + 132 132 135 135 135 138 138 138 141 142 142 144 144 + 146 147 147 149 149 149 152 153 154 154 154 154 158 + 158 158 161 162 162 164 165 165 165 168 168 168 171 + 171 171 171 171 176 176 176 179 180 180 180 183 183 + 183 186 186 188 188 190 190} + +do_execsql_test 1.11.5.4 { + SELECT rank() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2 +} {1 2 3 3 3 6 7 8 9 10 10 12 12 12 1 1 3 3 + 5 5 7 7 9 9 11 12 12 14 14 14 17 17 17 17 + 17 1 1 3 3 3 6 6 8 9 10 11 11 13 13 13 16 + 1 1 3 3 5 5 7 7 7 7 11 11 13 14 15 15 15 + 18 19 19 19 1 2 3 4 4 4 4 8 8 10 11 12 12 + 12 15 15 15 15 19 1 2 2 2 5 6 6 8 8 10 10 + 10 13 13 13 16 16 16 19 19 19 1 2 2 2 5 5 7 + 7 7 7 11 11 13 13 13 16 17 18 19 19 19 1 1 + 1 4 5 5 7 7 9 9 9 9 13 14 15 15 17 17 19 + 19 1 1 1 4 5 5 7 7 7 7 11 12 12 14 15 15 + 1 1 1 4 5 5 5 8 8 8 11 12 12 12 12 16 17 + 18 18 18 21 21} + +do_execsql_test 1.11.5.5 { + SELECT rank() OVER ( ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 15 15 15 15 + 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 + 15 15 36 36 36 36 36 36 36 36 36 36 36 36 36 + 36 36 36 52 52 52 52 52 52 52 52 52 52 52 52 + 52 52 52 52 52 52 52 52 52 73 73 73 73 73 73 + 73 73 73 73 73 73 73 73 73 73 73 73 73 92 92 + 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 + 92 92 92 92 113 113 113 113 113 113 113 113 113 + 113 113 113 113 113 113 113 113 113 113 113 113 134 + 134 134 134 134 134 134 134 134 134 134 134 134 134 + 134 134 134 134 134 134 154 154 154 154 154 154 154 + 154 154 154 154 154 154 154 154 154 170 170 170 170 + 170 170 170 170 170 170 170 170 170 170 170 170 170 + 170 170 170 170 170} + +do_execsql_test 1.11.5.6 { + SELECT rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 15 15 15 15 + 15 15 15 15 15 15 15 15 15 15 15 15 31 31 31 + 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 + 31 50 50 50 50 50 50 50 50 50 50 50 50 50 50 + 50 50 50 50 50 50 50 71 71 71 71 71 71 71 71 + 71 71 71 71 71 71 71 71 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 22 22 22 22 22 22 + 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 + 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 + 43 43 43 43 43 43 64 64 64 64 64 64 64 64 64 + 64 64 64 64 64 64 64 64 64 64 64 84 84 84 84 + 84 84 84 84 84 84 84 84 84 84 84 84 84 84 84 + 84 84 84} + +do_execsql_test 1.11.6.1 { + SELECT + row_number() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ), + rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ), + dense_rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) + FROM t2 +} {1 1 1 2 1 1 3 1 1 4 1 1 5 1 1 6 1 1 7 1 1 8 1 1 9 1 1 + 10 1 1 11 1 1 12 1 1 13 1 1 14 1 1 15 15 2 16 15 2 17 15 2 + 18 15 2 19 15 2 20 15 2 21 15 2 22 15 2 23 15 2 24 15 2 + 25 15 2 26 15 2 27 15 2 28 15 2 29 15 2 30 15 2 31 31 3 + 32 31 3 33 31 3 34 31 3 35 31 3 36 31 3 37 31 3 38 31 3 + 39 31 3 40 31 3 41 31 3 42 31 3 43 31 3 44 31 3 45 31 3 + 46 31 3 47 31 3 48 31 3 49 31 3 50 50 4 51 50 4 52 50 4 + 53 50 4 54 50 4 55 50 4 56 50 4 57 50 4 58 50 4 59 50 4 + 60 50 4 61 50 4 62 50 4 63 50 4 64 50 4 65 50 4 66 50 4 + 67 50 4 68 50 4 69 50 4 70 50 4 71 71 5 72 71 5 73 71 5 + 74 71 5 75 71 5 76 71 5 77 71 5 78 71 5 79 71 5 80 71 5 + 81 71 5 82 71 5 83 71 5 84 71 5 85 71 5 86 71 5 1 1 1 2 1 1 + 3 1 1 4 1 1 5 1 1 6 1 1 7 1 1 8 1 1 9 1 1 10 1 1 11 1 1 + 12 1 1 13 1 1 14 1 1 15 1 1 16 1 1 17 1 1 18 1 1 19 1 1 + 20 1 1 21 1 1 22 22 2 23 22 2 24 22 2 25 22 2 26 22 2 27 22 2 + 28 22 2 29 22 2 30 22 2 31 22 2 32 22 2 33 22 2 34 22 2 + 35 22 2 36 22 2 37 22 2 38 22 2 39 22 2 40 22 2 41 22 2 + 42 22 2 43 43 3 44 43 3 45 43 3 46 43 3 47 43 3 48 43 3 + 49 43 3 50 43 3 51 43 3 52 43 3 53 43 3 54 43 3 55 43 3 + 56 43 3 57 43 3 58 43 3 59 43 3 60 43 3 61 43 3 62 43 3 + 63 43 3 64 64 4 65 64 4 66 64 4 67 64 4 68 64 4 69 64 4 + 70 64 4 71 64 4 72 64 4 73 64 4 74 64 4 75 64 4 76 64 4 + 77 64 4 78 64 4 79 64 4 80 64 4 81 64 4 82 64 4 83 64 4 + 84 84 5 85 84 5 86 84 5 87 84 5 88 84 5 89 84 5 90 84 5 + 91 84 5 92 84 5 93 84 5 94 84 5 95 84 5 96 84 5 97 84 5 + 98 84 5 99 84 5 100 84 5 101 84 5 102 84 5 103 84 5 104 84 5 + 105 84 5} + + +do_test 1.11.7.1 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0053 0.0105 0.0158 0.0211 0.0263 0.0316 0.0368 0.0421 0.0474 0.0526 0.0579 0.0632 0.0684 0.0737 0.0789 0.0842 0.0895 0.0947 0.1000 0.1053 0.1105 0.1158 0.1211 0.1263 0.1316 0.1368 0.1421 0.1474 0.1526 0.1579 0.1632 0.1684 0.1737 0.1789 0.1842 0.1895 0.1947 0.2000 0.2053 0.2105 0.2158 0.2211 0.2263 0.2316 0.2368 0.2421 0.2474 0.2526 0.2579 0.2632 0.2684 0.2737 0.2789 0.2842 0.2895 0.2947 0.3000 0.3053 0.3105 0.3158 0.3211 0.3263 0.3316 0.3368 0.3421 0.3474 0.3526 0.3579 0.3632 0.3684 0.3737 0.3789 0.3842 0.3895 0.3947 0.4000 0.4053 0.4105 0.4158 0.4211 0.4263 0.4316 0.4368 0.4421 0.4474 0.4526 0.4579 0.4632 0.4684 0.4737 0.4789 0.4842 0.4895 0.4947 0.5000 0.5053 0.5105 0.5158 0.5211 0.5263 0.5316 0.5368 0.5421 0.5474 0.5526 0.5579 0.5632 0.5684 0.5737 0.5789 0.5842 0.5895 0.5947 0.6000 0.6053 0.6105 0.6158 0.6211 0.6263 0.6316 0.6368 0.6421 0.6474 0.6526 0.6579 0.6632 0.6684 0.6737 0.6789 0.6842 0.6895 0.6947 0.7000 0.7053 0.7105 0.7158 0.7211 0.7263 0.7316 0.7368 0.7421 0.7474 0.7526 0.7579 0.7632 0.7684 0.7737 0.7789 0.7842 0.7895 0.7947 0.8000 0.8053 0.8105 0.8158 0.8211 0.8263 0.8316 0.8368 0.8421 0.8474 0.8526 0.8579 0.8632 0.8684 0.8737 0.8789 0.8842 0.8895 0.8947 0.9000 0.9053 0.9105 0.9158 0.9211 0.9263 0.9316 0.9368 0.9421 0.9474 0.9526 0.9579 0.9632 0.9684 0.9737 0.9789 0.9842 0.9895 0.9947 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.11.7.2 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0769 0.1538 0.2308 0.3077 0.3846 0.4615 0.5385 0.6154 0.6923 0.7692 0.8462 0.9231 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0667 0.1333 0.2000 0.2667 0.3333 0.4000 0.4667 0.5333 0.6000 0.6667 0.7333 0.8000 0.8667 0.9333 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0556 0.1111 0.1667 0.2222 0.2778 0.3333 0.3889 0.4444 0.5000 0.5556 0.6111 0.6667 0.7222 0.7778 0.8333 0.8889 0.9444 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0526 0.1053 0.1579 0.2105 0.2632 0.3158 0.3684 0.4211 0.4737 0.5263 0.5789 0.6316 0.6842 0.7368 0.7895 0.8421 0.8947 0.9474 1.0000 0.0000 0.0667 0.1333 0.2000 0.2667 0.3333 0.4000 0.4667 0.5333 0.6000 0.6667 0.7333 0.8000 0.8667 0.9333 1.0000 0.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.11.7.3 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY b ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0105 0.0105 0.0211 0.0211 0.0316 0.0368 0.0421 0.0474 0.0474 0.0474 0.0632 0.0632 0.0632 0.0789 0.0789 0.0789 0.0947 0.1000 0.1000 0.1105 0.1105 0.1105 0.1263 0.1263 0.1368 0.1421 0.1421 0.1421 0.1579 0.1579 0.1579 0.1737 0.1789 0.1842 0.1895 0.1895 0.2000 0.2000 0.2105 0.2105 0.2211 0.2263 0.2316 0.2316 0.2421 0.2421 0.2526 0.2579 0.2579 0.2579 0.2737 0.2737 0.2737 0.2895 0.2895 0.3000 0.3053 0.3053 0.3053 0.3053 0.3263 0.3263 0.3263 0.3263 0.3474 0.3474 0.3579 0.3579 0.3579 0.3579 0.3789 0.3789 0.3895 0.3895 0.4000 0.4000 0.4000 0.4158 0.4211 0.4211 0.4316 0.4368 0.4368 0.4474 0.4474 0.4579 0.4579 0.4684 0.4684 0.4684 0.4684 0.4895 0.4947 0.5000 0.5053 0.5105 0.5158 0.5211 0.5211 0.5316 0.5316 0.5316 0.5474 0.5526 0.5526 0.5526 0.5526 0.5737 0.5737 0.5737 0.5737 0.5947 0.6000 0.6000 0.6105 0.6105 0.6211 0.6263 0.6316 0.6316 0.6316 0.6474 0.6526 0.6579 0.6632 0.6684 0.6737 0.6737 0.6737 0.6895 0.6895 0.6895 0.7053 0.7053 0.7053 0.7211 0.7211 0.7211 0.7368 0.7421 0.7421 0.7526 0.7526 0.7632 0.7684 0.7684 0.7789 0.7789 0.7789 0.7947 0.8000 0.8053 0.8053 0.8053 0.8053 0.8263 0.8263 0.8263 0.8421 0.8474 0.8474 0.8579 0.8632 0.8632 0.8632 0.8789 0.8789 0.8789 0.8947 0.8947 0.8947 0.8947 0.8947 0.9211 0.9211 0.9211 0.9368 0.9421 0.9421 0.9421 0.9579 0.9579 0.9579 0.9737 0.9737 0.9842 0.9842 0.9947 0.9947} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.11.7.4 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0769 0.1538 0.1538 0.1538 0.3846 0.4615 0.5385 0.6154 0.6923 0.6923 0.8462 0.8462 0.8462 0.0000 0.0000 0.1000 0.1000 0.2000 0.2000 0.3000 0.3000 0.4000 0.4000 0.5000 0.5500 0.5500 0.6500 0.6500 0.6500 0.8000 0.8000 0.8000 0.8000 0.8000 0.0000 0.0000 0.1333 0.1333 0.1333 0.3333 0.3333 0.4667 0.5333 0.6000 0.6667 0.6667 0.8000 0.8000 0.8000 1.0000 0.0000 0.0000 0.1000 0.1000 0.2000 0.2000 0.3000 0.3000 0.3000 0.3000 0.5000 0.5000 0.6000 0.6500 0.7000 0.7000 0.7000 0.8500 0.9000 0.9000 0.9000 0.0000 0.0556 0.1111 0.1667 0.1667 0.1667 0.1667 0.3889 0.3889 0.5000 0.5556 0.6111 0.6111 0.6111 0.7778 0.7778 0.7778 0.7778 1.0000 0.0000 0.0500 0.0500 0.0500 0.2000 0.2500 0.2500 0.3500 0.3500 0.4500 0.4500 0.4500 0.6000 0.6000 0.6000 0.7500 0.7500 0.7500 0.9000 0.9000 0.9000 0.0000 0.0500 0.0500 0.0500 0.2000 0.2000 0.3000 0.3000 0.3000 0.3000 0.5000 0.5000 0.6000 0.6000 0.6000 0.7500 0.8000 0.8500 0.9000 0.9000 0.9000 0.0000 0.0000 0.0000 0.1579 0.2105 0.2105 0.3158 0.3158 0.4211 0.4211 0.4211 0.4211 0.6316 0.6842 0.7368 0.7368 0.8421 0.8421 0.9474 0.9474 0.0000 0.0000 0.0000 0.2000 0.2667 0.2667 0.4000 0.4000 0.4000 0.4000 0.6667 0.7333 0.7333 0.8667 0.9333 0.9333 0.0000 0.0000 0.0000 0.1429 0.1905 0.1905 0.1905 0.3333 0.3333 0.3333 0.4762 0.5238 0.5238 0.5238 0.5238 0.7143 0.7619 0.8095 0.8095 0.8095 0.9524 0.9524} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.11.7.5 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.11.7.6 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER (PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.11.8.1 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0052 0.0105 0.0157 0.0209 0.0262 0.0314 0.0366 0.0419 0.0471 0.0524 0.0576 0.0628 0.0681 0.0733 0.0785 0.0838 0.0890 0.0942 0.0995 0.1047 0.1099 0.1152 0.1204 0.1257 0.1309 0.1361 0.1414 0.1466 0.1518 0.1571 0.1623 0.1675 0.1728 0.1780 0.1832 0.1885 0.1937 0.1990 0.2042 0.2094 0.2147 0.2199 0.2251 0.2304 0.2356 0.2408 0.2461 0.2513 0.2565 0.2618 0.2670 0.2723 0.2775 0.2827 0.2880 0.2932 0.2984 0.3037 0.3089 0.3141 0.3194 0.3246 0.3298 0.3351 0.3403 0.3455 0.3508 0.3560 0.3613 0.3665 0.3717 0.3770 0.3822 0.3874 0.3927 0.3979 0.4031 0.4084 0.4136 0.4188 0.4241 0.4293 0.4346 0.4398 0.4450 0.4503 0.4555 0.4607 0.4660 0.4712 0.4764 0.4817 0.4869 0.4921 0.4974 0.5026 0.5079 0.5131 0.5183 0.5236 0.5288 0.5340 0.5393 0.5445 0.5497 0.5550 0.5602 0.5654 0.5707 0.5759 0.5812 0.5864 0.5916 0.5969 0.6021 0.6073 0.6126 0.6178 0.6230 0.6283 0.6335 0.6387 0.6440 0.6492 0.6545 0.6597 0.6649 0.6702 0.6754 0.6806 0.6859 0.6911 0.6963 0.7016 0.7068 0.7120 0.7173 0.7225 0.7277 0.7330 0.7382 0.7435 0.7487 0.7539 0.7592 0.7644 0.7696 0.7749 0.7801 0.7853 0.7906 0.7958 0.8010 0.8063 0.8115 0.8168 0.8220 0.8272 0.8325 0.8377 0.8429 0.8482 0.8534 0.8586 0.8639 0.8691 0.8743 0.8796 0.8848 0.8901 0.8953 0.9005 0.9058 0.9110 0.9162 0.9215 0.9267 0.9319 0.9372 0.9424 0.9476 0.9529 0.9581 0.9634 0.9686 0.9738 0.9791 0.9843 0.9895 0.9948 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.11.8.2 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0714 0.1429 0.2143 0.2857 0.3571 0.4286 0.5000 0.5714 0.6429 0.7143 0.7857 0.8571 0.9286 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0625 0.1250 0.1875 0.2500 0.3125 0.3750 0.4375 0.5000 0.5625 0.6250 0.6875 0.7500 0.8125 0.8750 0.9375 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0526 0.1053 0.1579 0.2105 0.2632 0.3158 0.3684 0.4211 0.4737 0.5263 0.5789 0.6316 0.6842 0.7368 0.7895 0.8421 0.8947 0.9474 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0625 0.1250 0.1875 0.2500 0.3125 0.3750 0.4375 0.5000 0.5625 0.6250 0.6875 0.7500 0.8125 0.8750 0.9375 1.0000 0.0455 0.0909 0.1364 0.1818 0.2273 0.2727 0.3182 0.3636 0.4091 0.4545 0.5000 0.5455 0.5909 0.6364 0.6818 0.7273 0.7727 0.8182 0.8636 0.9091 0.9545 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.11.8.3 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY b ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0105 0.0105 0.0209 0.0209 0.0314 0.0314 0.0366 0.0419 0.0471 0.0628 0.0628 0.0628 0.0785 0.0785 0.0785 0.0942 0.0942 0.0942 0.0995 0.1099 0.1099 0.1257 0.1257 0.1257 0.1361 0.1361 0.1414 0.1571 0.1571 0.1571 0.1728 0.1728 0.1728 0.1780 0.1832 0.1885 0.1990 0.1990 0.2094 0.2094 0.2199 0.2199 0.2251 0.2304 0.2408 0.2408 0.2513 0.2513 0.2565 0.2723 0.2723 0.2723 0.2880 0.2880 0.2880 0.2984 0.2984 0.3037 0.3246 0.3246 0.3246 0.3246 0.3455 0.3455 0.3455 0.3455 0.3560 0.3560 0.3770 0.3770 0.3770 0.3770 0.3874 0.3874 0.3979 0.3979 0.4136 0.4136 0.4136 0.4188 0.4293 0.4293 0.4346 0.4450 0.4450 0.4555 0.4555 0.4660 0.4660 0.4869 0.4869 0.4869 0.4869 0.4921 0.4974 0.5026 0.5079 0.5131 0.5183 0.5288 0.5288 0.5445 0.5445 0.5445 0.5497 0.5707 0.5707 0.5707 0.5707 0.5916 0.5916 0.5916 0.5916 0.5969 0.6073 0.6073 0.6178 0.6178 0.6230 0.6283 0.6440 0.6440 0.6440 0.6492 0.6545 0.6597 0.6649 0.6702 0.6859 0.6859 0.6859 0.7016 0.7016 0.7016 0.7173 0.7173 0.7173 0.7330 0.7330 0.7330 0.7382 0.7487 0.7487 0.7592 0.7592 0.7644 0.7749 0.7749 0.7906 0.7906 0.7906 0.7958 0.8010 0.8220 0.8220 0.8220 0.8220 0.8377 0.8377 0.8377 0.8429 0.8534 0.8534 0.8586 0.8743 0.8743 0.8743 0.8901 0.8901 0.8901 0.9162 0.9162 0.9162 0.9162 0.9162 0.9319 0.9319 0.9319 0.9372 0.9529 0.9529 0.9529 0.9686 0.9686 0.9686 0.9791 0.9791 0.9895 0.9895 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.11.8.4 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0714 0.1429 0.3571 0.3571 0.3571 0.4286 0.5000 0.5714 0.6429 0.7857 0.7857 1.0000 1.0000 1.0000 0.0952 0.0952 0.1905 0.1905 0.2857 0.2857 0.3810 0.3810 0.4762 0.4762 0.5238 0.6190 0.6190 0.7619 0.7619 0.7619 1.0000 1.0000 1.0000 1.0000 1.0000 0.1250 0.1250 0.3125 0.3125 0.3125 0.4375 0.4375 0.5000 0.5625 0.6250 0.7500 0.7500 0.9375 0.9375 0.9375 1.0000 0.0952 0.0952 0.1905 0.1905 0.2857 0.2857 0.4762 0.4762 0.4762 0.4762 0.5714 0.5714 0.6190 0.6667 0.8095 0.8095 0.8095 0.8571 1.0000 1.0000 1.0000 0.0526 0.1053 0.1579 0.3684 0.3684 0.3684 0.3684 0.4737 0.4737 0.5263 0.5789 0.7368 0.7368 0.7368 0.9474 0.9474 0.9474 0.9474 1.0000 0.0476 0.1905 0.1905 0.1905 0.2381 0.3333 0.3333 0.4286 0.4286 0.5714 0.5714 0.5714 0.7143 0.7143 0.7143 0.8571 0.8571 0.8571 1.0000 1.0000 1.0000 0.0476 0.1905 0.1905 0.1905 0.2857 0.2857 0.4762 0.4762 0.4762 0.4762 0.5714 0.5714 0.7143 0.7143 0.7143 0.7619 0.8095 0.8571 1.0000 1.0000 1.0000 0.1500 0.1500 0.1500 0.2000 0.3000 0.3000 0.4000 0.4000 0.6000 0.6000 0.6000 0.6000 0.6500 0.7000 0.8000 0.8000 0.9000 0.9000 1.0000 1.0000 0.1875 0.1875 0.1875 0.2500 0.3750 0.3750 0.6250 0.6250 0.6250 0.6250 0.6875 0.8125 0.8125 0.8750 1.0000 1.0000 0.1364 0.1364 0.1364 0.1818 0.3182 0.3182 0.3182 0.4545 0.4545 0.4545 0.5000 0.6818 0.6818 0.6818 0.6818 0.7273 0.7727 0.9091 0.9091 0.9091 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.11.8.5 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.11.8.6 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.11.8.1 { + set myres {} + foreach r [db eval {SELECT ntile(100) OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 88.0000 89.0000 89.0000 90.0000 90.0000 91.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.11.8.2 { + set myres {} + foreach r [db eval {SELECT ntile(101) OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 22.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.11.8.3 { + set myres {} + foreach r [db eval {SELECT ntile(102) OVER ( ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 88.0000 89.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.11.8.4 { + set myres {} + foreach r [db eval {SELECT ntile(103) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 22.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.11.8.5 { + set myres {} + foreach r [db eval {SELECT ntile(104) OVER ( ORDER BY b%10,a ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000 103.0000 104.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.11.8.6 { + set myres {} + foreach r [db eval {SELECT ntile(105) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.11.8.7 { + set myres {} + foreach r [db eval {SELECT ntile(105) OVER ( ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 88.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000 103.0000 104.0000 105.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + +do_execsql_test 1.11.9.1 { + SELECT last_value(a+b) OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2 +} {108 72 52 83 55 79 109 65 26 120 119 50 70 103 + 80 124 36 96 59 124 116 110 57 51 52 130 103 74 + 87 48 128 117 105 136 131 71 133 92 109 63 84 109 + 57 146 78 147 113 74 88 150 87 110 65 121 106 110 + 124 85 145 107 161 171 150 156 80 171 120 109 158 + 114 111 136 147 87 173 124 168 173 162 132 101 154 + 167 190 161 110 156 195 198 102 123 177 169 140 111 + 180 119 160 197 152 124 121 134 146 147 132 213 141 + 193 200 210 157 132 136 175 161 218 188 226 191 187 + 208 211 179 138 144 223 196 214 170 212 202 163 184 + 172 173 195 229 240 187 210 200 163 227 228 223 191 + 252 235 225 243 172 187 202 179 179 182 231 261 207 + 263 206 189 209 212 276 181 274 249 239 234 213 234 + 269 196 271 221 210 229 235 250 223 232 229 279 224 + 280 216 207 207 207} + +do_execsql_test 1.11.9.2 { + SELECT last_value(a+b) OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2 +} {109 168 134 218 191 212 229 240 213 234 196 223 223 + 223 105 136 146 65 156 132 154 102 123 119 160 152 + 146 147 136 243 261 263 210 210 210 84 78 120 87 + 162 124 141 138 227 228 179 231 234 280 280 280 130 + 92 57 110 114 136 147 167 110 180 193 191 252 187 + 179 206 181 221 279 279 279 117 71 80 171 173 177 + 157 161 179 214 225 182 209 269 271 235 229 229 229 + 131 133 113 74 87 145 190 161 169 140 111 132 213 + 187 208 223 235 189 274 274 274 26 70 51 52 128 + 109 121 124 85 107 150 195 226 172 173 187 223 207 + 212 212 212 124 96 110 87 48 110 173 124 197 211 + 144 196 195 200 202 224 216 207 207 207 103 36 88 + 171 158 156 198 121 210 132 210 239 250 232 232 232 + 55 120 59 109 150 161 111 101 200 175 188 170 202 + 163 184 163 172 276 249 229 229 229} + +do_execsql_test 1.11.9.3 { + SELECT last_value(a+b) OVER ( ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2 +} {87 138 57 181 80 111 26 48 144 207 36 121 132 101 + 163 172 196 119 136 63 124 179 110 179 182 74 132 + 189 51 52 85 216 163 134 123 210 78 141 57 187 71 + 87 172 173 50 224 88 59 111 170 109 213 223 146 + 147 84 114 191 206 221 157 161 209 229 74 140 107 + 187 207 212 124 202 52 232 55 184 229 106 132 152 + 120 92 110 179 235 65 70 87 110 195 200 175 234 + 160 234 136 80 113 187 109 121 124 196 156 210 239 + 250 72 109 188 202 191 105 154 79 231 147 225 103 + 161 169 223 96 83 249 212 162 227 228 167 180 193 + 117 177 214 145 208 235 150 110 211 103 158 200 168 + 229 92 156 243 280 279 116 173 269 271 131 133 223 + 128 173 197 210 99 150 161 147 218 240 109 136 146 + 261 263 124 130 252 171 190 213 274 108 195 226 119 + 124 171 198 120 276 276 276} + +do_execsql_test 1.11.9.4 { + SELECT last_value(a+b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2 +} {109 213 223 106 234 191 212 168 229 147 218 240 240 + 240 119 136 123 210 146 147 132 152 160 105 154 92 + 156 243 109 136 146 261 263 263 263 63 124 179 78 + 141 84 120 234 79 231 162 227 228 280 280 280 110 + 179 57 187 114 191 206 221 92 110 136 147 167 180 + 193 279 124 130 252 252 252 71 157 161 209 229 179 + 235 80 225 117 177 214 116 173 269 271 171 171 171 + 132 189 87 74 140 113 187 103 161 169 145 208 235 + 131 133 223 190 213 274 274 274 52 85 172 173 107 + 187 207 212 65 70 109 121 124 223 150 128 108 195 + 226 226 226 207 216 50 224 124 202 87 110 195 200 + 196 96 110 211 173 197 119 124 124 124 132 88 52 + 232 156 210 239 250 83 103 158 210 171 198 198 198 + 172 163 59 111 170 55 184 229 175 72 109 188 202 + 249 200 99 150 161 120 276 276 276} + +do_execsql_test 1.11.9.5 { + SELECT last_value(a+b) OVER ( ORDER BY b%10,a ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2 +} {109 168 134 218 191 212 229 240 213 234 196 223 92 + 109 105 136 146 65 156 132 154 102 123 119 160 152 + 146 147 136 243 261 263 210 79 63 84 78 120 87 + 162 124 141 138 227 228 179 231 234 280 124 57 130 + 92 57 110 114 136 147 167 110 180 193 191 252 187 + 179 206 181 221 279 80 116 117 71 80 171 173 177 + 157 161 179 214 225 182 209 269 271 235 229 103 74 + 131 133 113 74 87 145 190 161 169 140 111 132 213 + 187 208 223 235 189 274 108 65 26 70 51 52 128 + 109 121 124 85 107 150 195 226 172 173 187 223 207 + 212 119 50 124 96 110 87 48 110 173 124 197 211 + 144 196 195 200 202 224 216 207 52 83 103 36 88 + 171 158 156 198 121 210 132 210 239 250 232 99 72 + 55 120 59 109 150 161 111 101 200 175 188 170 202 + 163 184 163 172 276 249 229 229 229} + +do_execsql_test 1.11.9.6 { + SELECT last_value(a+b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING) FROM t2 +} {108 52 83 79 65 26 70 103 80 36 116 51 52 128 + 117 71 63 84 109 78 147 88 121 106 124 85 107 171 + 150 80 171 120 109 158 87 168 173 162 156 195 198 + 177 124 121 134 141 210 157 132 161 218 226 191 179 + 138 214 212 172 173 229 240 187 210 227 228 223 225 + 179 182 231 207 209 212 239 234 213 234 269 196 271 + 235 250 223 232 229 280 99 92 72 55 109 120 119 + 50 124 96 59 124 110 57 130 103 74 87 48 105 136 + 131 133 92 109 57 146 113 74 150 87 110 65 110 + 145 161 156 114 111 136 147 173 124 132 101 154 167 + 190 161 110 102 123 169 140 111 180 119 160 197 152 + 146 147 132 213 193 200 136 175 188 187 208 211 144 + 223 196 170 202 163 184 195 200 163 191 252 235 243 + 172 187 202 179 261 263 206 189 276 181 274 249 221 + 210 229 279 224 216 207} + +do_execsql_test 1.11.10.1 { + SELECT nth_value(b,b+1) OVER (ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} 97 {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} 56 + {} {} {} {} {} {} {} {} {} 89 {} {} {} {} {} + {} {} {} {} {} {} 4 {} {} {} {} {} {} {} {} + 53 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 58 {} {} {} {} 73 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} 77 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} 99 {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.11.10.2 { + SELECT nth_value(b,b+1) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 61 {} {} {} 81 {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} 22 {} {} {} 12 + {} {} {} {} {} {} {} {} {} {} 43 {} {} {} {} + {} {} {} {} {} {} {} {} {} 33 {} {} {} {} {} + {} 4 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 15 {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.11.10.3 { + SELECT nth_value(b,b+1) OVER ( ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2 +} {1 1 2 2 2 3 4 6 7 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.11.10.4 { + SELECT nth_value(b,b+1) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} 1 1 + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 12 12 {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} 13 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.11.10.5 { + SELECT nth_value(b,b+1) OVER ( ORDER BY b%10,a ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 61 {} {} {} 81 {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} 22 {} {} {} 12 + {} {} {} {} {} {} {} {} {} {} 43 {} {} {} {} + {} {} {} {} {} {} {} {} {} 33 {} {} {} {} {} + {} 4 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 15 {} + {} {} {} {} {} {} {} {} {} 16 {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.11.10.6 { + SELECT nth_value(b,b+1) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.11.11.1 { + SELECT first_value(b) OVER (ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING) FROM t2 +} {89 89 89 89 89 81 96 59 38 68 39 62 91 46 6 + 99 97 27 46 78 54 97 8 67 29 93 84 77 23 16 + 16 93 65 35 47 7 86 74 61 91 85 24 85 43 59 + 12 32 56 3 91 22 90 55 15 28 89 25 47 1 56 40 + 43 56 16 75 36 89 98 76 81 4 94 42 30 78 33 + 29 53 63 2 87 37 80 84 72 41 9 61 73 95 65 13 + 58 96 98 1 21 74 65 35 5 73 11 51 87 41 12 8 + 20 31 31 15 95 22 73 79 88 34 8 11 49 34 90 + 59 96 60 55 75 77 44 2 7 85 57 74 29 70 59 19 + 39 26 26 47 80 90 36 58 47 9 72 72 66 33 93 + 75 64 81 9 23 37 13 12 14 62 91 36 91 33 15 + 34 36 99 3 95 69 58 52 30 50 84 10 84 33 21 + 39 44 58 30 38 34 83} + +do_execsql_test 1.11.11.2 { + SELECT first_value(b) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING) FROM t2 +} {90 90 90 90 90 40 30 80 20 90 60 70 80 90 81 + 81 81 81 81 91 61 91 91 1 81 41 61 1 21 11 51 + 41 31 31 11 62 62 62 62 62 12 32 22 42 2 72 + 12 22 2 72 72 93 93 93 93 93 23 93 43 3 43 33 + 53 63 73 13 73 73 33 93 23 13 54 54 54 54 54 + 84 74 24 4 94 84 74 34 34 44 74 64 14 34 65 + 65 65 65 65 35 85 85 55 15 25 75 95 65 65 35 + 5 15 95 55 75 96 96 96 96 96 46 6 46 16 16 86 + 56 56 56 16 36 76 96 96 26 26 97 97 97 97 97 + 27 97 67 77 47 7 47 87 37 87 77 7 57 47 47 38 + 38 38 38 38 68 78 8 28 98 78 58 98 8 88 8 89 + 89 89 89 89 59 39 99 29 59 89 89 29 9 79 49 + 59 29 59 19 39 9} + +do_execsql_test 1.11.11.3 { + SELECT first_value(b) OVER ( ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2 +} {1 1 1 1 1 1 2 2 3 3 4 5 6 7 7 7 8 8 8 + 9 9 9 10 11 11 12 12 12 13 13 14 15 15 15 16 + 16 16 17 19 20 21 21 22 22 23 23 24 25 26 26 + 27 27 28 29 29 29 30 30 30 31 31 32 33 33 33 + 33 34 34 34 34 35 35 36 36 36 36 37 37 38 38 + 39 39 39 40 41 41 42 43 43 44 44 46 46 47 47 + 47 47 49 50 51 52 53 54 55 55 56 56 56 57 58 + 58 58 58 59 59 59 59 60 61 61 62 62 63 64 65 + 65 65 66 67 68 69 70 72 72 72 73 73 73 74 74 + 74 75 75 75 76 77 77 78 78 79 80 80 81 81 81 + 82 83 84 84 84 84 85 85 85 86 87 87 88 89 89 + 89 90 90 90 91 91 91 91 91 93 93 93 94 95 95 + 95 96 96 96 97 97} + +do_execsql_test 1.11.11.4 { + SELECT first_value(b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2 +} {10 10 10 10 10 20 30 30 30 40 50 60 70 80 1 1 + 1 1 1 1 11 11 21 21 31 31 41 41 51 61 61 81 + 81 81 91 2 2 2 2 2 2 12 12 12 22 22 32 42 + 52 62 62 3 3 3 3 3 3 13 13 23 23 33 33 33 + 33 43 43 53 63 73 73 73 4 4 4 4 4 14 24 34 + 34 34 34 44 44 54 64 74 74 74 84 5 5 5 5 5 + 15 15 15 25 35 35 55 55 65 65 65 75 75 75 85 + 85 6 6 6 6 6 16 16 16 26 26 36 36 36 36 46 + 46 56 56 56 66 76 7 7 7 7 7 7 7 17 27 27 37 + 37 47 47 47 47 57 67 77 77 8 8 8 8 8 8 8 28 + 38 38 58 58 58 58 68 78 9 9 9 9 9 9 9 19 29 + 29 29 39 39 39 49 59 59 59 59 69 79 89} + +do_execsql_test 1.11.11.5 { + SELECT first_value(b) OVER ( ORDER BY b%10,a ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2 +} {90 90 90 90 90 40 30 80 20 90 60 70 80 90 30 + 50 10 30 81 91 61 91 91 1 81 41 61 1 21 11 51 + 41 31 31 11 81 91 91 21 62 12 32 22 42 2 72 + 12 22 2 72 72 12 62 52 82 93 23 93 43 3 43 33 + 53 63 73 13 73 73 33 93 23 13 33 3 33 83 54 + 84 74 24 4 94 84 74 34 34 44 74 64 14 34 84 + 84 44 34 65 35 85 85 55 15 25 75 95 65 65 35 + 5 15 95 55 75 85 75 15 95 96 46 6 46 16 16 86 + 56 56 56 16 36 76 96 96 26 26 36 66 36 36 97 + 27 97 67 77 47 7 47 87 37 87 77 7 57 47 47 37 + 27 17 7 38 68 78 8 28 98 78 58 98 8 88 8 58 + 58 58 38 89 59 39 99 29 59 89 89 29 9 79 49 + 59 29 59 19 39 9} + +do_execsql_test 1.11.11.6 { + SELECT first_value(b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING) FROM t2 +} {96 38 68 62 46 6 46 78 54 8 84 16 16 86 74 24 + 12 32 56 22 90 28 56 40 56 16 36 98 76 4 94 + 42 30 78 2 80 84 72 58 96 98 74 12 8 20 22 88 + 34 8 34 90 96 60 44 2 74 70 26 26 80 90 36 58 + 72 72 66 64 12 14 62 36 34 36 58 52 30 50 84 + 10 84 44 58 30 38 34 82 89 81 59 39 91 99 97 + 27 97 67 29 93 77 23 93 65 35 47 7 61 91 85 + 85 43 59 3 91 55 15 89 25 47 1 43 75 89 81 33 + 29 53 63 87 37 41 9 61 73 95 65 13 1 21 65 35 + 5 73 11 51 87 41 31 31 15 95 73 79 11 49 59 + 55 75 77 7 85 57 29 59 19 39 47 47 9 33 93 75 + 81 9 23 37 13 91 91 33 15 99 3 95 69 33 21 39 + 83 27 17 7} + +do_execsql_test 1.11.12.1 { + SELECT lead(b,b) OVER (ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING) FROM t2 +} {96 9 11 89 32 53 91 30 51 56 54 73 22 59 75 + 74 78 8 16 65 15 8 31 87 90 12 32 96 74 76 37 + 85 90 15 35 2 60 36 75 9 51 47 63 51 90 26 42 + 26 8 76 80 90 37 87 56 79 5 87 8 2 39 73 64 + 36 90 72 78 36 73 51 33 20 41 2 26 37 33 8 14 + 33 81 55 1 9 12 39 64 87 72 34 82 21 34 99 62 + 74 41 69 22 75 27 58 8 79 77 26 26 55 {} 29 + 30 7 {} 66 55 2 34 64 {} 33 {} 44 84 {} {} 95 + 85 19 {} 83 {} 91 {} {} 9 50 91 33 34 {} {} + 84 {} 7 9 {} {} {} 44 {} {} {} {} 91 84 {} 95 + 95 52 {} {} {} {} {} 21 {} {} {} 58 {} {} {} + {} {} {} {} 83 {} {} {} {} {} {} {} {} {} {} + {} {} {} {}} + +do_execsql_test 1.11.12.2 { + SELECT lead(b,b) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 81 {} {} {} 21 {} {} {} {} {} {} + {} {} {} {} {} {} 62 {} {} {} 12 {} {} {} 72 + {} {} {} {} {} {} {} {} {} {} 53 {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 34 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} 95 {} {} {} {} {} {} 85 {} + {} {} {} {} {} {} {} {} {} 56 {} 36 {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 57 {} {} {} {} {} 7 {} {} {} {} + {} {} {} {} {} {} 8 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 9 {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.11.12.3 { + SELECT lead(b,b) OVER ( ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2 +} {1 2 3 3 5 6 7 8 8 9 9 10 11 12 12 13 13 + 14 15 16 16 17 19 20 21 22 23 24 25 26 27 27 + 28 29 30 31 32 33 33 33 34 34 35 36 36 36 37 + 38 39 39 40 41 42 43 43 44 46 47 47 47 49 50 + 52 53 54 55 56 56 57 58 58 58 59 59 59 60 61 + 62 62 64 65 65 67 69 70 72 72 73 74 74 75 75 + 75 77 78 80 81 81 83 84 84 85 85 85 87 88 89 + 89 89 90 90 91 91 91 93 93 94 95 95 96 97 97 + 98 99 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.11.12.4 { + SELECT lead(b,b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2 +} {80 {} {} {} {} {} {} {} {} {} {} {} {} {} 1 + 11 81 81 {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} 12 12 72 82 {} {} {} {} {} {} + {} {} {} {} {} {} 13 23 73 73 {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} 34 84 {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 35 85 85 95 {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} 36 86 96 96 {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 37 47 + 47 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} 58 58 68 {} {} {} {} {} {} {} {} {} + {} {} {} {} 39 49 59 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.11.12.5 { + SELECT lead(b,b) OVER ( ORDER BY b%10,a ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2 +} {34 72 91 74 21 55 23 94 84 95 2 13 61 22 55 + 55 94 85 75 81 65 73 74 21 72 12 94 33 63 73 + 72 96 36 76 3 25 62 3 73 34 12 46 43 93 72 16 + 86 63 15 65 36 77 24 57 25 53 95 34 95 16 97 + 74 97 67 25 98 44 34 65 54 5 68 96 28 47 95 + 34 39 8 38 6 46 96 28 47 95 56 39 99 97 76 8 + 26 9 79 27 95 16 29 {} 58 58 77 85 56 {} 98 + 29 {} 19 96 {} {} 78 56 98 36 97 {} 89 89 29 + 47 78 {} {} {} 38 68 58 {} 58 38 {} 98 {} {} + {} 39 57 9 {} 79 {} {} 7 {} {} {} 9 29 38 78 + {} {} {} 8 39 {} {} {} {} 59 {} 99 {} {} {} + {} {} {} {} {} {} {} {} {} {} 9 {} {} {} {} + {} {} {} {} {} {} {} {}} + +do_execsql_test 1.11.12.6 { + SELECT lead(b,b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.11.13.1 { + SELECT lag(b,b) OVER (ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} 38 {} {} {} {} + {} {} {} 6 {} {} {} {} {} 81 46 6 {} {} {} {} + 23 {} {} {} {} {} 27 {} {} {} 35 6 {} 12 {} + 23 {} {} 61 84 {} 93 39 47 {} 54 46 96 56 {} + 16 {} {} {} {} 89 {} 16 43 {} 85 56 29 99 53 + {} 59 {} {} 91 59 53 84 99 {} 93 63 47 {} {} + 98 33 67 35 75 1 23 13 55 27 75 98 35 73 63 2 + 21 27 13 24 86 23 84 31 20 94 61 65 75 23 36 + 94 55 90 41 77 96 56 29 40 12 89 63 11 5 73 + 79 1 16 28 31 73 5 39 53 63 41 11 40 2 13 33 + 9 29 90 47 72 9 73 30 44 33 74 93 29 74 42 34 + 63 41 34 96 47 77 1 36 74 72 14 36 26 77 9 72 + 64 8 91 31 52 30} + +do_execsql_test 1.11.13.2 { + SELECT lag(b,b) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} 30 {} {} + {} {} {} {} 91 {} {} {} 61 {} 81 {} {} {} {} + 1 {} {} {} {} {} {} {} {} {} 22 {} {} {} 12 + {} {} 62 {} {} {} {} {} {} {} 23 {} {} {} {} + {} {} {} {} {} {} {} 43 {} 23 {} {} {} {} {} + {} 54 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 75 {} + {} {} {} {} {} 55 {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} 47 {} {} {} {} + {} 27 7 {} {} {} {} {} {} {} {} {} 68 {} 8 {} + {} {} {} {} {} {} {} {} {} {} {} {} 89 {} {} + {} {} {} {} {} 29 9 {} {} {}} + +do_execsql_test 1.11.13.3 { + SELECT lag(b,b) OVER ( ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2 +} {{} 1 1 1 1 2 2 2 2 2 2 3 3 3 4 4 5 6 6 + 6 7 7 7 7 7 8 8 8 8 8 8 9 9 9 9 9 9 9 + 9 9 9 10 10 10 10 11 11 11 11 11 12 12 12 12 + 13 13 13 13 13 14 15 15 15 15 16 16 16 16 16 + 17 19 20 20 21 21 21 21 22 22 22 22 23 23 23 + 23 23 24 23 24 24 25 26 26 26 26 26 26 26 26 + 26 26 26 27 27 27 27 28 29 29 29 29 30 30 30 + 30 30 30 31 31 31 31 31 32 32 32 32 32 32 31 + 32 33 33 33 33 33 33 34 34 34 34 34 34 34 34 + 35 35 35 35 35 36 36 36 36 36 36 36 37 37 37 + 38 38 38 38 38 38 39 39 39 39 40 40 41 41 42 + 43 42 43 43 43 43 44 44 44 46 46 46 47 47 47 + 47 47} + +do_execsql_test 1.11.13.4 { + SELECT lag(b,b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + 1 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.11.13.5 { + SELECT lag(b,b) OVER ( ORDER BY b%10,a ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} 30 {} {} + {} {} {} {} 91 {} {} {} 61 80 81 {} {} {} {} + 1 {} {} {} 30 {} 21 90 61 {} 22 {} 11 41 12 + {} {} 62 {} {} {} {} 31 {} 50 23 30 21 90 {} + {} 62 {} {} 81 {} 22 43 62 23 32 {} 91 {} 90 + 93 54 {} {} 90 72 12 22 90 81 83 23 80 20 72 + 43 51 33 80 90 2 34 54 1 20 62 12 13 75 44 30 + 93 91 1 21 55 61 61 13 85 3 65 65 91 73 33 93 + 55 84 62 31 11 65 35 85 33 55 15 12 75 22 3 + 73 65 36 85 43 95 43 13 47 44 65 65 96 36 27 + 7 46 34 94 47 36 73 34 35 73 68 24 8 75 85 75 + 66 34 95 36 84 77 46 34 84 47 89 65 36 16 38 + 76 58 57 29 9 44 56 17} + +do_execsql_test 1.11.13.6 { + SELECT lag(b,b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.11.14.1 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING) FROM t2 +} {89.81.96 89.81.96.59 89.81.96.59.38 89.81.96.59.38.68 + 89.81.96.59.38.68.39 81.96.59.38.68.39.62 96.59.38.68.39.62.91 + 59.38.68.39.62.91.46 38.68.39.62.91.46.6 68.39.62.91.46.6.99 + 39.62.91.46.6.99.97 62.91.46.6.99.97.27 91.46.6.99.97.27.46 + 46.6.99.97.27.46.78 6.99.97.27.46.78.54 99.97.27.46.78.54.97 + 97.27.46.78.54.97.8 27.46.78.54.97.8.67 46.78.54.97.8.67.29 + 78.54.97.8.67.29.93 54.97.8.67.29.93.84 97.8.67.29.93.84.77 + 8.67.29.93.84.77.23 67.29.93.84.77.23.16 29.93.84.77.23.16.16 + 93.84.77.23.16.16.93 84.77.23.16.16.93.65 77.23.16.16.93.65.35 + 23.16.16.93.65.35.47 16.16.93.65.35.47.7 16.93.65.35.47.7.86 + 93.65.35.47.7.86.74 65.35.47.7.86.74.61 35.47.7.86.74.61.91 + 47.7.86.74.61.91.85 7.86.74.61.91.85.24 86.74.61.91.85.24.85 + 74.61.91.85.24.85.43 61.91.85.24.85.43.59 91.85.24.85.43.59.12 + 85.24.85.43.59.12.32 24.85.43.59.12.32.56 85.43.59.12.32.56.3 + 43.59.12.32.56.3.91 59.12.32.56.3.91.22 12.32.56.3.91.22.90 + 32.56.3.91.22.90.55 56.3.91.22.90.55.15 3.91.22.90.55.15.28 + 91.22.90.55.15.28.89 22.90.55.15.28.89.25 90.55.15.28.89.25.47 + 55.15.28.89.25.47.1 15.28.89.25.47.1.56 28.89.25.47.1.56.40 + 89.25.47.1.56.40.43 25.47.1.56.40.43.56 47.1.56.40.43.56.16 + 1.56.40.43.56.16.75 56.40.43.56.16.75.36 40.43.56.16.75.36.89 + 43.56.16.75.36.89.98 56.16.75.36.89.98.76 16.75.36.89.98.76.81 + 75.36.89.98.76.81.4 36.89.98.76.81.4.94 89.98.76.81.4.94.42 + 98.76.81.4.94.42.30 76.81.4.94.42.30.78 81.4.94.42.30.78.33 + 4.94.42.30.78.33.29 94.42.30.78.33.29.53 42.30.78.33.29.53.63 + 30.78.33.29.53.63.2 78.33.29.53.63.2.87 33.29.53.63.2.87.37 + 29.53.63.2.87.37.80 53.63.2.87.37.80.84 63.2.87.37.80.84.72 + 2.87.37.80.84.72.41 87.37.80.84.72.41.9 37.80.84.72.41.9.61 + 80.84.72.41.9.61.73 84.72.41.9.61.73.95 72.41.9.61.73.95.65 + 41.9.61.73.95.65.13 9.61.73.95.65.13.58 61.73.95.65.13.58.96 + 73.95.65.13.58.96.98 95.65.13.58.96.98.1 65.13.58.96.98.1.21 + 13.58.96.98.1.21.74 58.96.98.1.21.74.65 96.98.1.21.74.65.35 + 98.1.21.74.65.35.5 1.21.74.65.35.5.73 21.74.65.35.5.73.11 + 74.65.35.5.73.11.51 65.35.5.73.11.51.87 35.5.73.11.51.87.41 + 5.73.11.51.87.41.12 73.11.51.87.41.12.8 11.51.87.41.12.8.20 + 51.87.41.12.8.20.31 87.41.12.8.20.31.31 41.12.8.20.31.31.15 + 12.8.20.31.31.15.95 8.20.31.31.15.95.22 20.31.31.15.95.22.73 + 31.31.15.95.22.73.79 31.15.95.22.73.79.88 15.95.22.73.79.88.34 + 95.22.73.79.88.34.8 22.73.79.88.34.8.11 73.79.88.34.8.11.49 + 79.88.34.8.11.49.34 88.34.8.11.49.34.90 34.8.11.49.34.90.59 + 8.11.49.34.90.59.96 11.49.34.90.59.96.60 49.34.90.59.96.60.55 + 34.90.59.96.60.55.75 90.59.96.60.55.75.77 59.96.60.55.75.77.44 + 96.60.55.75.77.44.2 60.55.75.77.44.2.7 55.75.77.44.2.7.85 + 75.77.44.2.7.85.57 77.44.2.7.85.57.74 44.2.7.85.57.74.29 + 2.7.85.57.74.29.70 7.85.57.74.29.70.59 85.57.74.29.70.59.19 + 57.74.29.70.59.19.39 74.29.70.59.19.39.26 29.70.59.19.39.26.26 + 70.59.19.39.26.26.47 59.19.39.26.26.47.80 19.39.26.26.47.80.90 + 39.26.26.47.80.90.36 26.26.47.80.90.36.58 26.47.80.90.36.58.47 + 47.80.90.36.58.47.9 80.90.36.58.47.9.72 90.36.58.47.9.72.72 + 36.58.47.9.72.72.66 58.47.9.72.72.66.33 47.9.72.72.66.33.93 + 9.72.72.66.33.93.75 72.72.66.33.93.75.64 72.66.33.93.75.64.81 + 66.33.93.75.64.81.9 33.93.75.64.81.9.23 93.75.64.81.9.23.37 + 75.64.81.9.23.37.13 64.81.9.23.37.13.12 81.9.23.37.13.12.14 + 9.23.37.13.12.14.62 23.37.13.12.14.62.91 37.13.12.14.62.91.36 + 13.12.14.62.91.36.91 12.14.62.91.36.91.33 14.62.91.36.91.33.15 + 62.91.36.91.33.15.34 91.36.91.33.15.34.36 36.91.33.15.34.36.99 + 91.33.15.34.36.99.3 33.15.34.36.99.3.95 15.34.36.99.3.95.69 + 34.36.99.3.95.69.58 36.99.3.95.69.58.52 99.3.95.69.58.52.30 + 3.95.69.58.52.30.50 95.69.58.52.30.50.84 69.58.52.30.50.84.10 + 58.52.30.50.84.10.84 52.30.50.84.10.84.33 30.50.84.10.84.33.21 + 50.84.10.84.33.21.39 84.10.84.33.21.39.44 10.84.33.21.39.44.58 + 84.33.21.39.44.58.30 33.21.39.44.58.30.38 21.39.44.58.30.38.34 + 39.44.58.30.38.34.83 44.58.30.38.34.83.27 58.30.38.34.83.27.82 + 30.38.34.83.27.82.17 38.34.83.27.82.17.7 34.83.27.82.17.7 + 83.27.82.17.7} + +do_execsql_test 1.11.14.2 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING) FROM t2 +} {90.40.30 90.40.30.80 90.40.30.80.20 90.40.30.80.20.90 + 90.40.30.80.20.90.60 40.30.80.20.90.60.70 30.80.20.90.60.70.80 + 80.20.90.60.70.80.90 20.90.60.70.80.90.30 90.60.70.80.90.30.50 + 60.70.80.90.30.50.10 70.80.90.30.50.10.30 80.90.30.50.10.30 + 90.30.50.10.30 81.91.61 81.91.61.91 81.91.61.91.91 81.91.61.91.91.1 + 81.91.61.91.91.1.81 91.61.91.91.1.81.41 61.91.91.1.81.41.61 + 91.91.1.81.41.61.1 91.1.81.41.61.1.21 1.81.41.61.1.21.11 + 81.41.61.1.21.11.51 41.61.1.21.11.51.41 61.1.21.11.51.41.31 + 1.21.11.51.41.31.31 21.11.51.41.31.31.11 11.51.41.31.31.11.81 + 51.41.31.31.11.81.91 41.31.31.11.81.91.91 31.31.11.81.91.91.21 + 31.11.81.91.91.21 11.81.91.91.21 62.12.32 62.12.32.22 + 62.12.32.22.42 62.12.32.22.42.2 62.12.32.22.42.2.72 + 12.32.22.42.2.72.12 32.22.42.2.72.12.22 22.42.2.72.12.22.2 + 42.2.72.12.22.2.72 2.72.12.22.2.72.72 72.12.22.2.72.72.12 + 12.22.2.72.72.12.62 22.2.72.72.12.62.52 2.72.72.12.62.52.82 + 72.72.12.62.52.82 72.12.62.52.82 93.23.93 93.23.93.43 93.23.93.43.3 + 93.23.93.43.3.43 93.23.93.43.3.43.33 23.93.43.3.43.33.53 + 93.43.3.43.33.53.63 43.3.43.33.53.63.73 3.43.33.53.63.73.13 + 43.33.53.63.73.13.73 33.53.63.73.13.73.73 53.63.73.13.73.73.33 + 63.73.13.73.73.33.93 73.13.73.73.33.93.23 13.73.73.33.93.23.13 + 73.73.33.93.23.13.33 73.33.93.23.13.33.3 33.93.23.13.33.3.33 + 93.23.13.33.3.33.83 23.13.33.3.33.83 13.33.3.33.83 54.84.74 + 54.84.74.24 54.84.74.24.4 54.84.74.24.4.94 54.84.74.24.4.94.84 + 84.74.24.4.94.84.74 74.24.4.94.84.74.34 24.4.94.84.74.34.34 + 4.94.84.74.34.34.44 94.84.74.34.34.44.74 84.74.34.34.44.74.64 + 74.34.34.44.74.64.14 34.34.44.74.64.14.34 34.44.74.64.14.34.84 + 44.74.64.14.34.84.84 74.64.14.34.84.84.44 64.14.34.84.84.44.34 + 14.34.84.84.44.34 34.84.84.44.34 65.35.85 65.35.85.85 + 65.35.85.85.55 65.35.85.85.55.15 65.35.85.85.55.15.25 + 35.85.85.55.15.25.75 85.85.55.15.25.75.95 85.55.15.25.75.95.65 + 55.15.25.75.95.65.65 15.25.75.95.65.65.35 25.75.95.65.65.35.5 + 75.95.65.65.35.5.15 95.65.65.35.5.15.95 65.65.35.5.15.95.55 + 65.35.5.15.95.55.75 35.5.15.95.55.75.85 5.15.95.55.75.85.75 + 15.95.55.75.85.75.15 95.55.75.85.75.15.95 55.75.85.75.15.95 + 75.85.75.15.95 96.46.6 96.46.6.46 96.46.6.46.16 96.46.6.46.16.16 + 96.46.6.46.16.16.86 46.6.46.16.16.86.56 6.46.16.16.86.56.56 + 46.16.16.86.56.56.56 16.16.86.56.56.56.16 16.86.56.56.56.16.36 + 86.56.56.56.16.36.76 56.56.56.16.36.76.96 56.56.16.36.76.96.96 + 56.16.36.76.96.96.26 16.36.76.96.96.26.26 36.76.96.96.26.26.36 + 76.96.96.26.26.36.66 96.96.26.26.36.66.36 96.26.26.36.66.36.36 + 26.26.36.66.36.36 26.36.66.36.36 97.27.97 97.27.97.67 + 97.27.97.67.77 97.27.97.67.77.47 97.27.97.67.77.47.7 + 27.97.67.77.47.7.47 97.67.77.47.7.47.87 67.77.47.7.47.87.37 + 77.47.7.47.87.37.87 47.7.47.87.37.87.77 7.47.87.37.87.77.7 + 47.87.37.87.77.7.57 87.37.87.77.7.57.47 37.87.77.7.57.47.47 + 87.77.7.57.47.47.37 77.7.57.47.47.37.27 7.57.47.47.37.27.17 + 57.47.47.37.27.17.7 47.47.37.27.17.7 47.37.27.17.7 38.68.78 + 38.68.78.8 38.68.78.8.28 38.68.78.8.28.98 38.68.78.8.28.98.78 + 68.78.8.28.98.78.58 78.8.28.98.78.58.98 8.28.98.78.58.98.8 + 28.98.78.58.98.8.88 98.78.58.98.8.88.8 78.58.98.8.88.8.58 + 58.98.8.88.8.58.58 98.8.88.8.58.58.58 8.88.8.58.58.58.38 + 88.8.58.58.58.38 8.58.58.58.38 89.59.39 89.59.39.99 89.59.39.99.29 + 89.59.39.99.29.59 89.59.39.99.29.59.89 59.39.99.29.59.89.89 + 39.99.29.59.89.89.29 99.29.59.89.89.29.9 29.59.89.89.29.9.79 + 59.89.89.29.9.79.49 89.89.29.9.79.49.59 89.29.9.79.49.59.29 + 29.9.79.49.59.29.59 9.79.49.59.29.59.19 79.49.59.29.59.19.39 + 49.59.29.59.19.39.9 59.29.59.19.39.9.9 29.59.19.39.9.9.99 + 59.19.39.9.9.99.69 19.39.9.9.99.69.39 39.9.9.99.69.39 9.9.99.69.39} + +do_execsql_test 1.11.14.3 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2 +} {1.1.2 1.1.2.2 1.1.2.2.3 1.1.2.2.3.3 1.1.2.2.3.3.4 1.2.2.3.3.4.5 + 2.2.3.3.4.5.6 2.3.3.4.5.6.7 3.3.4.5.6.7.7 3.4.5.6.7.7.7 + 4.5.6.7.7.7.8 5.6.7.7.7.8.8 6.7.7.7.8.8.8 7.7.7.8.8.8.9 + 7.7.8.8.8.9.9 7.8.8.8.9.9.9 8.8.8.9.9.9.10 8.8.9.9.9.10.11 + 8.9.9.9.10.11.11 9.9.9.10.11.11.12 9.9.10.11.11.12.12 + 9.10.11.11.12.12.12 10.11.11.12.12.12.13 11.11.12.12.12.13.13 + 11.12.12.12.13.13.14 12.12.12.13.13.14.15 12.12.13.13.14.15.15 + 12.13.13.14.15.15.15 13.13.14.15.15.15.16 13.14.15.15.15.16.16 + 14.15.15.15.16.16.16 15.15.15.16.16.16.17 15.15.16.16.16.17.19 + 15.16.16.16.17.19.20 16.16.16.17.19.20.21 16.16.17.19.20.21.21 + 16.17.19.20.21.21.22 17.19.20.21.21.22.22 19.20.21.21.22.22.23 + 20.21.21.22.22.23.23 21.21.22.22.23.23.24 21.22.22.23.23.24.25 + 22.22.23.23.24.25.26 22.23.23.24.25.26.26 23.23.24.25.26.26.27 + 23.24.25.26.26.27.27 24.25.26.26.27.27.28 25.26.26.27.27.28.29 + 26.26.27.27.28.29.29 26.27.27.28.29.29.29 27.27.28.29.29.29.30 + 27.28.29.29.29.30.30 28.29.29.29.30.30.30 29.29.29.30.30.30.31 + 29.29.30.30.30.31.31 29.30.30.30.31.31.32 30.30.30.31.31.32.33 + 30.30.31.31.32.33.33 30.31.31.32.33.33.33 31.31.32.33.33.33.33 + 31.32.33.33.33.33.34 32.33.33.33.33.34.34 33.33.33.33.34.34.34 + 33.33.33.34.34.34.34 33.33.34.34.34.34.35 33.34.34.34.34.35.35 + 34.34.34.34.35.35.36 34.34.34.35.35.36.36 34.34.35.35.36.36.36 + 34.35.35.36.36.36.36 35.35.36.36.36.36.37 35.36.36.36.36.37.37 + 36.36.36.36.37.37.38 36.36.36.37.37.38.38 36.36.37.37.38.38.39 + 36.37.37.38.38.39.39 37.37.38.38.39.39.39 37.38.38.39.39.39.40 + 38.38.39.39.39.40.41 38.39.39.39.40.41.41 39.39.39.40.41.41.42 + 39.39.40.41.41.42.43 39.40.41.41.42.43.43 40.41.41.42.43.43.44 + 41.41.42.43.43.44.44 41.42.43.43.44.44.46 42.43.43.44.44.46.46 + 43.43.44.44.46.46.47 43.44.44.46.46.47.47 44.44.46.46.47.47.47 + 44.46.46.47.47.47.47 46.46.47.47.47.47.49 46.47.47.47.47.49.50 + 47.47.47.47.49.50.51 47.47.47.49.50.51.52 47.47.49.50.51.52.53 + 47.49.50.51.52.53.54 49.50.51.52.53.54.55 50.51.52.53.54.55.55 + 51.52.53.54.55.55.56 52.53.54.55.55.56.56 53.54.55.55.56.56.56 + 54.55.55.56.56.56.57 55.55.56.56.56.57.58 55.56.56.56.57.58.58 + 56.56.56.57.58.58.58 56.56.57.58.58.58.58 56.57.58.58.58.58.59 + 57.58.58.58.58.59.59 58.58.58.58.59.59.59 58.58.58.59.59.59.59 + 58.58.59.59.59.59.60 58.59.59.59.59.60.61 59.59.59.59.60.61.61 + 59.59.59.60.61.61.62 59.59.60.61.61.62.62 59.60.61.61.62.62.63 + 60.61.61.62.62.63.64 61.61.62.62.63.64.65 61.62.62.63.64.65.65 + 62.62.63.64.65.65.65 62.63.64.65.65.65.66 63.64.65.65.65.66.67 + 64.65.65.65.66.67.68 65.65.65.66.67.68.69 65.65.66.67.68.69.70 + 65.66.67.68.69.70.72 66.67.68.69.70.72.72 67.68.69.70.72.72.72 + 68.69.70.72.72.72.73 69.70.72.72.72.73.73 70.72.72.72.73.73.73 + 72.72.72.73.73.73.74 72.72.73.73.73.74.74 72.73.73.73.74.74.74 + 73.73.73.74.74.74.75 73.73.74.74.74.75.75 73.74.74.74.75.75.75 + 74.74.74.75.75.75.76 74.74.75.75.75.76.77 74.75.75.75.76.77.77 + 75.75.75.76.77.77.78 75.75.76.77.77.78.78 75.76.77.77.78.78.79 + 76.77.77.78.78.79.80 77.77.78.78.79.80.80 77.78.78.79.80.80.81 + 78.78.79.80.80.81.81 78.79.80.80.81.81.81 79.80.80.81.81.81.82 + 80.80.81.81.81.82.83 80.81.81.81.82.83.84 81.81.81.82.83.84.84 + 81.81.82.83.84.84.84 81.82.83.84.84.84.84 82.83.84.84.84.84.85 + 83.84.84.84.84.85.85 84.84.84.84.85.85.85 84.84.84.85.85.85.86 + 84.84.85.85.85.86.87 84.85.85.85.86.87.87 85.85.85.86.87.87.88 + 85.85.86.87.87.88.89 85.86.87.87.88.89.89 86.87.87.88.89.89.89 + 87.87.88.89.89.89.90 87.88.89.89.89.90.90 88.89.89.89.90.90.90 + 89.89.89.90.90.90.91 89.89.90.90.90.91.91 89.90.90.90.91.91.91 + 90.90.90.91.91.91.91 90.90.91.91.91.91.91 90.91.91.91.91.91.93 + 91.91.91.91.91.93.93 91.91.91.91.93.93.93 91.91.91.93.93.93.94 + 91.91.93.93.93.94.95 91.93.93.93.94.95.95 93.93.93.94.95.95.95 + 93.93.94.95.95.95.96 93.94.95.95.95.96.96 94.95.95.95.96.96.96 + 95.95.95.96.96.96.97 95.95.96.96.96.97.97 95.96.96.96.97.97.98 + 96.96.96.97.97.98.98 96.96.97.97.98.98.99 96.97.97.98.98.99.99 + 97.97.98.98.99.99 97.98.98.99.99} + +do_execsql_test 1.11.14.4 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2 +} {10.20.30 10.20.30.30 10.20.30.30.30 10.20.30.30.30.40 + 10.20.30.30.30.40.50 20.30.30.30.40.50.60 30.30.30.40.50.60.70 + 30.30.40.50.60.70.80 30.40.50.60.70.80.80 40.50.60.70.80.80.90 + 50.60.70.80.80.90.90 60.70.80.80.90.90.90 70.80.80.90.90.90 + 80.80.90.90.90 1.1.11 1.1.11.11 1.1.11.11.21 1.1.11.11.21.21 + 1.1.11.11.21.21.31 1.11.11.21.21.31.31 11.11.21.21.31.31.41 + 11.21.21.31.31.41.41 21.21.31.31.41.41.51 21.31.31.41.41.51.61 + 31.31.41.41.51.61.61 31.41.41.51.61.61.81 41.41.51.61.61.81.81 + 41.51.61.61.81.81.81 51.61.61.81.81.81.91 61.61.81.81.81.91.91 + 61.81.81.81.91.91.91 81.81.81.91.91.91.91 81.81.91.91.91.91.91 + 81.91.91.91.91.91 91.91.91.91.91 2.2.12 2.2.12.12 2.2.12.12.12 + 2.2.12.12.12.22 2.2.12.12.12.22.22 2.12.12.12.22.22.32 + 12.12.12.22.22.32.42 12.12.22.22.32.42.52 12.22.22.32.42.52.62 + 22.22.32.42.52.62.62 22.32.42.52.62.62.72 32.42.52.62.62.72.72 + 42.52.62.62.72.72.72 52.62.62.72.72.72.82 62.62.72.72.72.82 + 62.72.72.72.82 3.3.13 3.3.13.13 3.3.13.13.23 3.3.13.13.23.23 + 3.3.13.13.23.23.33 3.13.13.23.23.33.33 13.13.23.23.33.33.33 + 13.23.23.33.33.33.33 23.23.33.33.33.33.43 23.33.33.33.33.43.43 + 33.33.33.33.43.43.53 33.33.33.43.43.53.63 33.33.43.43.53.63.73 + 33.43.43.53.63.73.73 43.43.53.63.73.73.73 43.53.63.73.73.73.83 + 53.63.73.73.73.83.93 63.73.73.73.83.93.93 73.73.73.83.93.93.93 + 73.73.83.93.93.93 73.83.93.93.93 4.14.24 4.14.24.34 4.14.24.34.34 + 4.14.24.34.34.34 4.14.24.34.34.34.34 14.24.34.34.34.34.44 + 24.34.34.34.34.44.44 34.34.34.34.44.44.54 34.34.34.44.44.54.64 + 34.34.44.44.54.64.74 34.44.44.54.64.74.74 44.44.54.64.74.74.74 + 44.54.64.74.74.74.84 54.64.74.74.74.84.84 64.74.74.74.84.84.84 + 74.74.74.84.84.84.84 74.74.84.84.84.84.94 74.84.84.84.84.94 + 84.84.84.84.94 5.15.15 5.15.15.15 5.15.15.15.25 5.15.15.15.25.35 + 5.15.15.15.25.35.35 15.15.15.25.35.35.55 15.15.25.35.35.55.55 + 15.25.35.35.55.55.65 25.35.35.55.55.65.65 35.35.55.55.65.65.65 + 35.55.55.65.65.65.75 55.55.65.65.65.75.75 55.65.65.65.75.75.75 + 65.65.65.75.75.75.85 65.65.75.75.75.85.85 65.75.75.75.85.85.85 + 75.75.75.85.85.85.95 75.75.85.85.85.95.95 75.85.85.85.95.95.95 + 85.85.85.95.95.95 85.85.95.95.95 6.16.16 6.16.16.16 6.16.16.16.26 + 6.16.16.16.26.26 6.16.16.16.26.26.36 16.16.16.26.26.36.36 + 16.16.26.26.36.36.36 16.26.26.36.36.36.36 26.26.36.36.36.36.46 + 26.36.36.36.36.46.46 36.36.36.36.46.46.56 36.36.36.46.46.56.56 + 36.36.46.46.56.56.56 36.46.46.56.56.56.66 46.46.56.56.56.66.76 + 46.56.56.56.66.76.86 56.56.56.66.76.86.96 56.56.66.76.86.96.96 + 56.66.76.86.96.96.96 66.76.86.96.96.96 76.86.96.96.96 7.7.7 + 7.7.7.17 7.7.7.17.27 7.7.7.17.27.27 7.7.7.17.27.27.37 + 7.7.17.27.27.37.37 7.17.27.27.37.37.47 17.27.27.37.37.47.47 + 27.27.37.37.47.47.47 27.37.37.47.47.47.47 37.37.47.47.47.47.57 + 37.47.47.47.47.57.67 47.47.47.47.57.67.77 47.47.47.57.67.77.77 + 47.47.57.67.77.77.87 47.57.67.77.77.87.87 57.67.77.77.87.87.97 + 67.77.77.87.87.97.97 77.77.87.87.97.97 77.87.87.97.97 8.8.8 + 8.8.8.28 8.8.8.28.38 8.8.8.28.38.38 8.8.8.28.38.38.58 + 8.8.28.38.38.58.58 8.28.38.38.58.58.58 28.38.38.58.58.58.58 + 38.38.58.58.58.58.68 38.58.58.58.58.68.78 58.58.58.58.68.78.78 + 58.58.58.68.78.78.88 58.58.68.78.78.88.98 58.68.78.78.88.98.98 + 68.78.78.88.98.98 78.78.88.98.98 9.9.9 9.9.9.19 9.9.9.19.29 + 9.9.9.19.29.29 9.9.9.19.29.29.29 9.9.19.29.29.29.39 + 9.19.29.29.29.39.39 19.29.29.29.39.39.39 29.29.29.39.39.39.49 + 29.29.39.39.39.49.59 29.39.39.39.49.59.59 39.39.39.49.59.59.59 + 39.39.49.59.59.59.59 39.49.59.59.59.59.69 49.59.59.59.59.69.79 + 59.59.59.59.69.79.89 59.59.59.69.79.89.89 59.59.69.79.89.89.89 + 59.69.79.89.89.89.99 69.79.89.89.89.99.99 79.89.89.89.99.99 + 89.89.89.99.99} + +do_execsql_test 1.11.14.5 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( ORDER BY b%10,a ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING ) FROM t2 +} {90.40.30 90.40.30.80 90.40.30.80.20 90.40.30.80.20.90 + 90.40.30.80.20.90.60 40.30.80.20.90.60.70 30.80.20.90.60.70.80 + 80.20.90.60.70.80.90 20.90.60.70.80.90.30 90.60.70.80.90.30.50 + 60.70.80.90.30.50.10 70.80.90.30.50.10.30 80.90.30.50.10.30.81 + 90.30.50.10.30.81.91 30.50.10.30.81.91.61 50.10.30.81.91.61.91 + 10.30.81.91.61.91.91 30.81.91.61.91.91.1 81.91.61.91.91.1.81 + 91.61.91.91.1.81.41 61.91.91.1.81.41.61 91.91.1.81.41.61.1 + 91.1.81.41.61.1.21 1.81.41.61.1.21.11 81.41.61.1.21.11.51 + 41.61.1.21.11.51.41 61.1.21.11.51.41.31 1.21.11.51.41.31.31 + 21.11.51.41.31.31.11 11.51.41.31.31.11.81 51.41.31.31.11.81.91 + 41.31.31.11.81.91.91 31.31.11.81.91.91.21 31.11.81.91.91.21.62 + 11.81.91.91.21.62.12 81.91.91.21.62.12.32 91.91.21.62.12.32.22 + 91.21.62.12.32.22.42 21.62.12.32.22.42.2 62.12.32.22.42.2.72 + 12.32.22.42.2.72.12 32.22.42.2.72.12.22 22.42.2.72.12.22.2 + 42.2.72.12.22.2.72 2.72.12.22.2.72.72 72.12.22.2.72.72.12 + 12.22.2.72.72.12.62 22.2.72.72.12.62.52 2.72.72.12.62.52.82 + 72.72.12.62.52.82.93 72.12.62.52.82.93.23 12.62.52.82.93.23.93 + 62.52.82.93.23.93.43 52.82.93.23.93.43.3 82.93.23.93.43.3.43 + 93.23.93.43.3.43.33 23.93.43.3.43.33.53 93.43.3.43.33.53.63 + 43.3.43.33.53.63.73 3.43.33.53.63.73.13 43.33.53.63.73.13.73 + 33.53.63.73.13.73.73 53.63.73.13.73.73.33 63.73.13.73.73.33.93 + 73.13.73.73.33.93.23 13.73.73.33.93.23.13 73.73.33.93.23.13.33 + 73.33.93.23.13.33.3 33.93.23.13.33.3.33 93.23.13.33.3.33.83 + 23.13.33.3.33.83.54 13.33.3.33.83.54.84 33.3.33.83.54.84.74 + 3.33.83.54.84.74.24 33.83.54.84.74.24.4 83.54.84.74.24.4.94 + 54.84.74.24.4.94.84 84.74.24.4.94.84.74 74.24.4.94.84.74.34 + 24.4.94.84.74.34.34 4.94.84.74.34.34.44 94.84.74.34.34.44.74 + 84.74.34.34.44.74.64 74.34.34.44.74.64.14 34.34.44.74.64.14.34 + 34.44.74.64.14.34.84 44.74.64.14.34.84.84 74.64.14.34.84.84.44 + 64.14.34.84.84.44.34 14.34.84.84.44.34.65 34.84.84.44.34.65.35 + 84.84.44.34.65.35.85 84.44.34.65.35.85.85 44.34.65.35.85.85.55 + 34.65.35.85.85.55.15 65.35.85.85.55.15.25 35.85.85.55.15.25.75 + 85.85.55.15.25.75.95 85.55.15.25.75.95.65 55.15.25.75.95.65.65 + 15.25.75.95.65.65.35 25.75.95.65.65.35.5 75.95.65.65.35.5.15 + 95.65.65.35.5.15.95 65.65.35.5.15.95.55 65.35.5.15.95.55.75 + 35.5.15.95.55.75.85 5.15.95.55.75.85.75 15.95.55.75.85.75.15 + 95.55.75.85.75.15.95 55.75.85.75.15.95.96 75.85.75.15.95.96.46 + 85.75.15.95.96.46.6 75.15.95.96.46.6.46 15.95.96.46.6.46.16 + 95.96.46.6.46.16.16 96.46.6.46.16.16.86 46.6.46.16.16.86.56 + 6.46.16.16.86.56.56 46.16.16.86.56.56.56 16.16.86.56.56.56.16 + 16.86.56.56.56.16.36 86.56.56.56.16.36.76 56.56.56.16.36.76.96 + 56.56.16.36.76.96.96 56.16.36.76.96.96.26 16.36.76.96.96.26.26 + 36.76.96.96.26.26.36 76.96.96.26.26.36.66 96.96.26.26.36.66.36 + 96.26.26.36.66.36.36 26.26.36.66.36.36.97 26.36.66.36.36.97.27 + 36.66.36.36.97.27.97 66.36.36.97.27.97.67 36.36.97.27.97.67.77 + 36.97.27.97.67.77.47 97.27.97.67.77.47.7 27.97.67.77.47.7.47 + 97.67.77.47.7.47.87 67.77.47.7.47.87.37 77.47.7.47.87.37.87 + 47.7.47.87.37.87.77 7.47.87.37.87.77.7 47.87.37.87.77.7.57 + 87.37.87.77.7.57.47 37.87.77.7.57.47.47 87.77.7.57.47.47.37 + 77.7.57.47.47.37.27 7.57.47.47.37.27.17 57.47.47.37.27.17.7 + 47.47.37.27.17.7.38 47.37.27.17.7.38.68 37.27.17.7.38.68.78 + 27.17.7.38.68.78.8 17.7.38.68.78.8.28 7.38.68.78.8.28.98 + 38.68.78.8.28.98.78 68.78.8.28.98.78.58 78.8.28.98.78.58.98 + 8.28.98.78.58.98.8 28.98.78.58.98.8.88 98.78.58.98.8.88.8 + 78.58.98.8.88.8.58 58.98.8.88.8.58.58 98.8.88.8.58.58.58 + 8.88.8.58.58.58.38 88.8.58.58.58.38.89 8.58.58.58.38.89.59 + 58.58.58.38.89.59.39 58.58.38.89.59.39.99 58.38.89.59.39.99.29 + 38.89.59.39.99.29.59 89.59.39.99.29.59.89 59.39.99.29.59.89.89 + 39.99.29.59.89.89.29 99.29.59.89.89.29.9 29.59.89.89.29.9.79 + 59.89.89.29.9.79.49 89.89.29.9.79.49.59 89.29.9.79.49.59.29 + 29.9.79.49.59.29.59 9.79.49.59.29.59.19 79.49.59.29.59.19.39 + 49.59.29.59.19.39.9 59.29.59.19.39.9.9 29.59.19.39.9.9.99 + 59.19.39.9.9.99.69 19.39.9.9.99.69.39 39.9.9.99.69.39 9.9.99.69.39} + +do_execsql_test 1.11.14.6 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING) FROM t2 +} {96 38 68 62 46 6 46 78 54 8 84 16 16 86 74 24 + 12 32 56 22 90 28 56 40 56 16 36 98 76 4 94 + 42 30 78 2 80 84 72 58 96 98 74 12 8 20 22 88 + 34 8 34 90 96 60 44 2 74 70 26 26 80 90 36 58 + 72 72 66 64 12 14 62 36 34 36 58 52 30 50 84 + 10 84 44 58 30 38 34 82 89 81 59 39 91 99 97 + 27 97 67 29 93 77 23 93 65 35 47 7 61 91 85 + 85 43 59 3 91 55 15 89 25 47 1 43 75 89 81 33 + 29 53 63 87 37 41 9 61 73 95 65 13 1 21 65 35 + 5 73 11 51 87 41 31 31 15 95 73 79 11 49 59 + 55 75 77 7 85 57 29 59 19 39 47 47 9 33 93 75 + 81 9 23 37 13 91 91 33 15 99 3 95 69 33 21 39 + 83 27 17 7} + +do_execsql_test 1.11.14.7 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (win1 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING) + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a) + ORDER BY 1 +} {1 1 10 11 11 12 12 12 13 13 14 15 15 15 16 16 + 16 17 19 2 2 20 21 21 22 22 23 23 24 25 26 26 + 27 27 28 29 29 29 3 3 30 30 30 31 31 32 33 33 + 33 33 34 34 34 34 35 35 36 36 36 36 37 37 38 + 38 39 39 39 4 40 41 41 42 43 43 44 44 46 46 + 47 47 47 47 49 5 50 51 52 53 54 55 55 56 56 + 56 57 58 58 58 58 59 59 59 59 6 60 61 61 62 + 62 63 64 65 65 65 66 67 68 69 7 7 7 70 72 72 + 72 73 73 73 74 74 74 75 75 75 76 77 77 78 78 + 79 8 8 8 80 80 81 81 81 82 83 84 84 84 84 85 + 85 85 86 87 87 88 89 89 89 9 9 9 90 90 90 91 + 91 91 91 91 93 93 93 94 95 95 95 96 96 96 97 + 97 98 98 99 99} + +do_execsql_test 1.11.14.8 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (win1 ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING) + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a ORDER BY b%10) + ORDER BY 1 +} {1 1 10 11 11 12 12 12 13 13 14 15 15 15 16 16 + 16 17 19 2 2 20 21 21 22 22 23 23 24 25 26 26 + 27 27 28 29 29 29 3 3 30 30 30 31 31 32 33 33 + 33 33 34 34 34 34 35 35 36 36 36 36 37 37 38 + 38 39 39 39 4 40 41 41 42 43 43 44 44 46 46 + 47 47 47 47 49 5 50 51 52 53 54 55 55 56 56 + 56 57 58 58 58 58 59 59 59 59 6 60 61 61 62 + 62 63 64 65 65 65 66 67 68 69 7 7 7 70 72 72 + 72 73 73 73 74 74 74 75 75 75 76 77 77 78 78 + 79 8 8 8 80 80 81 81 81 82 83 84 84 84 84 85 + 85 85 86 87 87 88 89 89 89 9 9 9 90 90 90 91 + 91 91 91 91 93 93 93 94 95 95 95 96 96 96 97 + 97 98 98 99 99} + +do_execsql_test 1.11.14.9 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER win2 + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a ORDER BY b%10), + win2 AS (win1 ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING) + ORDER BY 1 +} {1 1 10 11 11 12 12 12 13 13 14 15 15 15 16 16 + 16 17 19 2 2 20 21 21 22 22 23 23 24 25 26 26 + 27 27 28 29 29 29 3 3 30 30 30 31 31 32 33 33 + 33 33 34 34 34 34 35 35 36 36 36 36 37 37 38 + 38 39 39 39 4 40 41 41 42 43 43 44 44 46 46 + 47 47 47 47 49 5 50 51 52 53 54 55 55 56 56 + 56 57 58 58 58 58 59 59 59 59 6 60 61 61 62 + 62 63 64 65 65 65 66 67 68 69 7 7 7 70 72 72 + 72 73 73 73 74 74 74 75 75 75 76 77 77 78 78 + 79 8 8 8 80 80 81 81 81 82 83 84 84 84 84 85 + 85 85 86 87 87 88 89 89 89 9 9 9 90 90 90 91 + 91 91 91 91 93 93 93 94 95 95 95 96 96 96 97 + 97 98 98 99 99} + +do_execsql_test 1.11.15.1 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE a%2=0) OVER win FROM t2 + WINDOW win AS (ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING) +} {3 89.96 4 89.96 5 89.96.38 6 89.96.38 7 89.96.38.39 7 96.38.39 + 7 96.38.39.91 7 38.39.91 7 38.39.91.6 7 39.91.6 7 39.91.6.97 + 7 91.6.97 7 91.6.97.46 7 6.97.46 7 6.97.46.54 7 97.46.54 + 7 97.46.54.8 7 46.54.8 7 46.54.8.29 7 54.8.29 7 54.8.29.84 + 7 8.29.84 7 8.29.84.23 7 29.84.23 7 29.84.23.16 7 84.23.16 + 7 84.23.16.65 7 23.16.65 7 23.16.65.47 7 16.65.47 7 16.65.47.86 + 7 65.47.86 7 65.47.86.61 7 47.86.61 7 47.86.61.85 7 86.61.85 + 7 86.61.85.85 7 61.85.85 7 61.85.85.59 7 85.85.59 7 85.85.59.32 + 7 85.59.32 7 85.59.32.3 7 59.32.3 7 59.32.3.22 7 32.3.22 + 7 32.3.22.55 7 3.22.55 7 3.22.55.28 7 22.55.28 7 22.55.28.25 + 7 55.28.25 7 55.28.25.1 7 28.25.1 7 28.25.1.40 7 25.1.40 + 7 25.1.40.56 7 1.40.56 7 1.40.56.75 7 40.56.75 7 40.56.75.89 + 7 56.75.89 7 56.75.89.76 7 75.89.76 7 75.89.76.4 7 89.76.4 + 7 89.76.4.42 7 76.4.42 7 76.4.42.78 7 4.42.78 7 4.42.78.29 + 7 42.78.29 7 42.78.29.63 7 78.29.63 7 78.29.63.87 7 29.63.87 + 7 29.63.87.80 7 63.87.80 7 63.87.80.72 7 87.80.72 7 87.80.72.9 + 7 80.72.9 7 80.72.9.73 7 72.9.73 7 72.9.73.65 7 9.73.65 + 7 9.73.65.58 7 73.65.58 7 73.65.58.98 7 65.58.98 7 65.58.98.21 + 7 58.98.21 7 58.98.21.65 7 98.21.65 7 98.21.65.5 7 21.65.5 + 7 21.65.5.11 7 65.5.11 7 65.5.11.87 7 5.11.87 7 5.11.87.12 + 7 11.87.12 7 11.87.12.20 7 87.12.20 7 87.12.20.31 7 12.20.31 + 7 12.20.31.95 7 20.31.95 7 20.31.95.73 7 31.95.73 7 31.95.73.88 + 7 95.73.88 7 95.73.88.8 7 73.88.8 7 73.88.8.49 7 88.8.49 + 7 88.8.49.90 7 8.49.90 7 8.49.90.96 7 49.90.96 7 49.90.96.55 + 7 90.96.55 7 90.96.55.77 7 96.55.77 7 96.55.77.2 7 55.77.2 + 7 55.77.2.85 7 77.2.85 7 77.2.85.74 7 2.85.74 7 2.85.74.70 + 7 85.74.70 7 85.74.70.19 7 74.70.19 7 74.70.19.26 7 70.19.26 + 7 70.19.26.47 7 19.26.47 7 19.26.47.90 7 26.47.90 7 26.47.90.58 + 7 47.90.58 7 47.90.58.9 7 90.58.9 7 90.58.9.72 7 58.9.72 + 7 58.9.72.33 7 9.72.33 7 9.72.33.75 7 72.33.75 7 72.33.75.81 + 7 33.75.81 7 33.75.81.23 7 75.81.23 7 75.81.23.13 7 81.23.13 + 7 81.23.13.14 7 23.13.14 7 23.13.14.91 7 13.14.91 7 13.14.91.91 + 7 14.91.91 7 14.91.91.15 7 91.91.15 7 91.91.15.36 7 91.15.36 + 7 91.15.36.3 7 15.36.3 7 15.36.3.69 7 36.3.69 7 36.3.69.52 + 7 3.69.52 7 3.69.52.50 7 69.52.50 7 69.52.50.10 7 52.50.10 + 7 52.50.10.33 7 50.10.33 7 50.10.33.39 7 10.33.39 7 10.33.39.58 + 7 33.39.58 7 33.39.58.38 7 39.58.38 7 39.58.38.83 7 58.38.83 + 7 58.38.83.82 7 38.83.82 7 38.83.82.7 6 83.82.7 5 83.82.7} + +do_execsql_test 1.11.15.2 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE 0=1) OVER win FROM t2 + WINDOW win AS (ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING) +} {3 {} 4 {} 5 {} 6 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} + 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} + 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} + 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} + 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} + 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} + 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} + 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} + 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} + 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} + 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} + 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} + 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} + 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} + 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} + 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} + 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} + 7 {} 7 {} 6 {} 5 {}} + +do_execsql_test 1.11.15.3 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE 1=0) OVER win FROM t2 + WINDOW win AS (PARTITION BY (a%10) ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING) +} {3 {} 4 {} 5 {} 6 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} + 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 6 {} 5 {} 3 {} 4 {} + 5 {} 6 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} + 7 {} 7 {} 7 {} 7 {} 6 {} 5 {} 3 {} 4 {} 5 {} 6 {} 7 {} + 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} + 7 {} 6 {} 5 {} 3 {} 4 {} 5 {} 6 {} 7 {} 7 {} 7 {} 7 {} + 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 6 {} 5 {} + 3 {} 4 {} 5 {} 6 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} + 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 6 {} 5 {} 3 {} 4 {} 5 {} + 6 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} + 7 {} 7 {} 7 {} 6 {} 5 {} 3 {} 4 {} 5 {} 6 {} 7 {} 7 {} + 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} + 6 {} 5 {} 3 {} 4 {} 5 {} 6 {} 7 {} 7 {} 7 {} 7 {} 7 {} + 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 6 {} 5 {} 3 {} + 4 {} 5 {} 6 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} + 7 {} 7 {} 7 {} 7 {} 7 {} 6 {} 5 {} 3 {} 4 {} 5 {} 6 {} + 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} + 7 {} 7 {} 6 {} 5 {}} + +do_execsql_test 1.11.15.4 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE a%2=0) OVER win FROM t2 + WINDOW win AS (PARTITION BY (a%10) ORDER BY a ROWS BETWEEN 4 PRECEDING AND 2 FOLLOWING) +} {3 89.6.29 4 89.6.29.47 5 89.6.29.47.59 6 89.6.29.47.59.28 + 7 89.6.29.47.59.28.75 7 6.29.47.59.28.75.78 7 29.47.59.28.75.78.72 + 7 47.59.28.75.78.72.98 7 59.28.75.78.72.98.87 7 28.75.78.72.98.87.73 + 7 75.78.72.98.87.73.96 7 78.72.98.87.73.96.74 7 72.98.87.73.96.74.90 + 7 98.87.73.96.74.90.75 7 87.73.96.74.90.75.91 7 73.96.74.90.75.91.69 + 7 96.74.90.75.91.69.39 7 74.90.75.91.69.39.7 6 90.75.91.69.39.7 + 5 75.91.69.39.7 3 {} 4 {} 5 {} 6 {} 7 {} 7 {} 7 {} 7 {} + 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 6 {} 5 {} + 3 96.97.84 4 96.97.84.86 5 96.97.84.86.32 6 96.97.84.86.32.25 + 7 96.97.84.86.32.25.89 7 97.84.86.32.25.89.29 7 84.86.32.25.89.29.9 + 7 86.32.25.89.29.9.21 7 32.25.89.29.9.21.12 7 25.89.29.9.21.12.88 + 7 89.29.9.21.12.88.55 7 29.9.21.12.88.55.70 7 9.21.12.88.55.70.58 + 7 21.12.88.55.70.58.81 7 12.88.55.70.58.81.91 7 88.55.70.58.81.91.52 + 7 55.70.58.81.91.52.58 6 70.58.81.91.52.58 5 58.81.91.52.58 3 {} + 4 {} 5 {} 6 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} + 7 {} 7 {} 7 {} 7 {} 7 {} 6 {} 5 {} 3 38.46.23 4 38.46.23.61 + 5 38.46.23.61.3 6 38.46.23.61.3.1 7 38.46.23.61.3.1.76 + 7 46.23.61.3.1.76.63 7 23.61.3.1.76.63.73 7 61.3.1.76.63.73.65 + 7 3.1.76.63.73.65.20 7 1.76.63.73.65.20.8 7 76.63.73.65.20.8.77 + 7 63.73.65.20.8.77.19 7 73.65.20.8.77.19.9 7 65.20.8.77.19.9.23 + 7 20.8.77.19.9.23.15 7 8.77.19.9.23.15.50 7 77.19.9.23.15.50.38 + 6 19.9.23.15.50.38 5 9.23.15.50.38 3 {} 4 {} 5 {} 6 {} 7 {} + 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} + 7 {} 6 {} 5 {} 3 39.54.16 4 39.54.16.85 5 39.54.16.85.22 + 6 39.54.16.85.22.40 7 39.54.16.85.22.40.4 7 54.16.85.22.40.4.87 + 7 16.85.22.40.4.87.65 7 85.22.40.4.87.65.5 7 22.40.4.87.65.5.31 + 7 40.4.87.65.5.31.49 7 4.87.65.5.31.49.2 7 87.65.5.31.49.2.26 + 7 65.5.31.49.2.26.72 7 5.31.49.2.26.72.13 7 31.49.2.26.72.13.36 + 7 49.2.26.72.13.36.10 7 2.26.72.13.36.10.83 6 26.72.13.36.10.83 + 5 72.13.36.10.83 3 {} 4 {} 5 {} 6 {} 7 {} 7 {} 7 {} 7 {} + 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 6 {} 5 {} + 3 91.8.65 4 91.8.65.85 5 91.8.65.85.55 6 91.8.65.85.55.56 + 7 91.8.65.85.55.56.42 7 8.65.85.55.56.42.80 7 65.85.55.56.42.80.58 + 7 85.55.56.42.80.58.11 7 55.56.42.80.58.11.95 7 56.42.80.58.11.95.90 + 7 42.80.58.11.95.90.85 7 80.58.11.95.90.85.47 7 58.11.95.90.85.47.33 + 7 11.95.90.85.47.33.14 7 95.90.85.47.33.14.3 7 90.85.47.33.14.3.33 + 7 85.47.33.14.3.33.82 6 47.33.14.3.33.82 5 33.14.3.33.82 3 {} 4 {} + 5 {} 6 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} 7 {} + 7 {} 7 {} 7 {} 7 {} 6 {} 5 {}} + +do_execsql_test 1.12.2.1 { + SELECT max(b) OVER ( ORDER BY a ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2 +} {96 96 96 68 91 91 91 99 99 99 99 99 97 97 97 + 97 97 97 93 93 93 93 84 93 93 93 93 93 86 86 + 86 91 91 91 91 91 85 85 85 59 59 91 91 91 91 + 91 90 90 89 89 89 89 56 56 56 56 75 75 89 98 + 98 98 98 98 94 94 94 94 78 78 78 63 87 87 87 + 87 87 84 84 84 73 95 95 95 95 96 98 98 98 98 + 98 74 74 74 73 73 87 87 87 87 87 41 31 31 95 + 95 95 95 95 88 88 88 88 49 90 90 96 96 96 96 + 96 77 77 77 85 85 85 85 85 74 74 70 70 59 47 + 80 90 90 90 90 90 72 72 72 72 93 93 93 93 93 + 81 81 81 37 37 62 91 91 91 91 91 91 91 99 99 + 99 99 99 95 95 69 84 84 84 84 84 84 84 58 58 + 58 58 83 83 83 83 83 82 82 17 7} + +do_execsql_test 1.12.2.2 { + SELECT min(b) OVER ( ORDER BY a ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2 +} {38 38 38 38 38 39 6 6 6 6 6 27 27 27 8 8 8 + 8 8 29 23 16 16 16 16 16 16 7 7 7 7 7 61 24 + 24 24 24 12 12 12 3 3 3 3 3 15 15 15 15 15 + 1 1 1 1 1 16 16 16 16 16 36 36 4 4 4 4 4 + 30 29 29 29 2 2 2 2 2 37 37 9 9 9 9 9 13 + 13 13 13 1 1 1 1 1 5 5 5 5 5 11 11 8 8 8 + 8 8 15 15 15 15 22 22 8 8 8 8 8 11 34 34 55 + 55 55 44 2 2 2 2 2 7 29 29 19 19 19 19 19 + 26 26 26 36 36 9 9 9 9 9 33 33 33 33 9 9 9 + 9 9 12 12 12 12 14 33 15 15 15 15 3 3 3 3 3 + 30 30 30 10 10 10 10 10 21 21 21 30 30 30 27 + 27 17 7 7 7 7 7} + +do_execsql_test 1.12.3.1 { + SELECT row_number() OVER ( ORDER BY a ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.12.3.2 { + SELECT row_number() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.12.3.3 { + SELECT row_number() OVER ( ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.12.4.1 { + SELECT dense_rank() OVER ( ORDER BY a ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.12.4.2 { + SELECT dense_rank() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.12.4.3 { + SELECT dense_rank() OVER ( ORDER BY b ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2 +} {1 1 2 2 3 3 4 5 6 7 7 7 8 8 8 9 9 9 10 + 11 11 12 12 12 13 13 14 15 15 15 16 16 16 17 + 18 19 20 20 21 21 22 22 23 24 25 25 26 26 27 + 28 28 28 29 29 29 30 30 31 32 32 32 32 33 33 + 33 33 34 34 35 35 35 35 36 36 37 37 38 38 38 + 39 40 40 41 42 42 43 43 44 44 45 45 45 45 46 + 47 48 49 50 51 52 52 53 53 53 54 55 55 55 55 + 56 56 56 56 57 58 58 59 59 60 61 62 62 62 63 + 64 65 66 67 68 68 68 69 69 69 70 70 70 71 71 + 71 72 73 73 74 74 75 76 76 77 77 77 78 79 80 + 80 80 80 81 81 81 82 83 83 84 85 85 85 86 86 + 86 87 87 87 87 87 88 88 88 89 90 90 90 91 91 + 91 92 92 93 93 94 94} + +do_execsql_test 1.12.4.4 { + SELECT dense_rank() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2 +} {1 2 3 3 3 4 5 6 7 8 8 9 9 9 1 1 2 2 3 + 3 4 4 5 5 6 7 7 8 8 8 9 9 9 9 9 1 1 2 + 2 2 3 3 4 5 6 7 7 8 8 8 9 1 1 2 2 3 3 + 4 4 4 4 5 5 6 7 8 8 8 9 10 10 10 1 2 3 + 4 4 4 4 5 5 6 7 8 8 8 9 9 9 9 10 1 2 2 + 2 3 4 4 5 5 6 6 6 7 7 7 8 8 8 9 9 9 1 + 2 2 2 3 3 4 4 4 4 5 5 6 6 6 7 8 9 10 10 + 10 1 1 1 2 3 3 4 4 5 5 5 5 6 7 8 8 9 9 + 10 10 1 1 1 2 3 3 4 4 4 4 5 6 6 7 8 8 1 + 1 1 2 3 3 3 4 4 4 5 6 6 6 6 7 8 9 9 9 + 10 10} + +do_execsql_test 1.12.4.5 { + SELECT dense_rank() OVER ( ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 + 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 + 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 + 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 + 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 + 8 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 10 10 + 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 + 10 10 10 10 10} + +do_execsql_test 1.12.4.6 { + SELECT dense_rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 + 5 5 5 5 5 5 5 5 5 5 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 + 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 + 5} + +do_execsql_test 1.12.5.1 { + SELECT rank() OVER ( ORDER BY a ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.12.5.2 { + SELECT rank() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.12.5.3 { + SELECT rank() OVER ( ORDER BY b ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2 +} {1 1 3 3 5 5 7 8 9 10 10 10 13 13 13 16 16 + 16 19 20 20 22 22 22 25 25 27 28 28 28 31 31 + 31 34 35 36 37 37 39 39 41 41 43 44 45 45 47 + 47 49 50 50 50 53 53 53 56 56 58 59 59 59 59 + 63 63 63 63 67 67 69 69 69 69 73 73 75 75 77 + 77 77 80 81 81 83 84 84 86 86 88 88 90 90 90 + 90 94 95 96 97 98 99 100 100 102 102 102 105 106 + 106 106 106 110 110 110 110 114 115 115 117 117 119 + 120 121 121 121 124 125 126 127 128 129 129 129 132 + 132 132 135 135 135 138 138 138 141 142 142 144 144 + 146 147 147 149 149 149 152 153 154 154 154 154 158 + 158 158 161 162 162 164 165 165 165 168 168 168 171 + 171 171 171 171 176 176 176 179 180 180 180 183 183 + 183 186 186 188 188 190 190} + +do_execsql_test 1.12.5.4 { + SELECT rank() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2 +} {1 2 3 3 3 6 7 8 9 10 10 12 12 12 1 1 3 3 + 5 5 7 7 9 9 11 12 12 14 14 14 17 17 17 17 + 17 1 1 3 3 3 6 6 8 9 10 11 11 13 13 13 16 + 1 1 3 3 5 5 7 7 7 7 11 11 13 14 15 15 15 + 18 19 19 19 1 2 3 4 4 4 4 8 8 10 11 12 12 + 12 15 15 15 15 19 1 2 2 2 5 6 6 8 8 10 10 + 10 13 13 13 16 16 16 19 19 19 1 2 2 2 5 5 7 + 7 7 7 11 11 13 13 13 16 17 18 19 19 19 1 1 + 1 4 5 5 7 7 9 9 9 9 13 14 15 15 17 17 19 + 19 1 1 1 4 5 5 7 7 7 7 11 12 12 14 15 15 + 1 1 1 4 5 5 5 8 8 8 11 12 12 12 12 16 17 + 18 18 18 21 21} + +do_execsql_test 1.12.5.5 { + SELECT rank() OVER ( ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 15 15 15 15 + 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 + 15 15 36 36 36 36 36 36 36 36 36 36 36 36 36 + 36 36 36 52 52 52 52 52 52 52 52 52 52 52 52 + 52 52 52 52 52 52 52 52 52 73 73 73 73 73 73 + 73 73 73 73 73 73 73 73 73 73 73 73 73 92 92 + 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 + 92 92 92 92 113 113 113 113 113 113 113 113 113 + 113 113 113 113 113 113 113 113 113 113 113 113 134 + 134 134 134 134 134 134 134 134 134 134 134 134 134 + 134 134 134 134 134 134 154 154 154 154 154 154 154 + 154 154 154 154 154 154 154 154 154 170 170 170 170 + 170 170 170 170 170 170 170 170 170 170 170 170 170 + 170 170 170 170 170} + +do_execsql_test 1.12.5.6 { + SELECT rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 15 15 15 15 + 15 15 15 15 15 15 15 15 15 15 15 15 31 31 31 + 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 + 31 50 50 50 50 50 50 50 50 50 50 50 50 50 50 + 50 50 50 50 50 50 50 71 71 71 71 71 71 71 71 + 71 71 71 71 71 71 71 71 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 22 22 22 22 22 22 + 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 + 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 + 43 43 43 43 43 43 64 64 64 64 64 64 64 64 64 + 64 64 64 64 64 64 64 64 64 64 64 84 84 84 84 + 84 84 84 84 84 84 84 84 84 84 84 84 84 84 84 + 84 84 84} + +do_execsql_test 1.12.6.1 { + SELECT + row_number() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ), + rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ), + dense_rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) + FROM t2 +} {1 1 1 2 1 1 3 1 1 4 1 1 5 1 1 6 1 1 7 1 1 8 1 1 9 1 1 + 10 1 1 11 1 1 12 1 1 13 1 1 14 1 1 15 15 2 16 15 2 17 15 2 + 18 15 2 19 15 2 20 15 2 21 15 2 22 15 2 23 15 2 24 15 2 + 25 15 2 26 15 2 27 15 2 28 15 2 29 15 2 30 15 2 31 31 3 + 32 31 3 33 31 3 34 31 3 35 31 3 36 31 3 37 31 3 38 31 3 + 39 31 3 40 31 3 41 31 3 42 31 3 43 31 3 44 31 3 45 31 3 + 46 31 3 47 31 3 48 31 3 49 31 3 50 50 4 51 50 4 52 50 4 + 53 50 4 54 50 4 55 50 4 56 50 4 57 50 4 58 50 4 59 50 4 + 60 50 4 61 50 4 62 50 4 63 50 4 64 50 4 65 50 4 66 50 4 + 67 50 4 68 50 4 69 50 4 70 50 4 71 71 5 72 71 5 73 71 5 + 74 71 5 75 71 5 76 71 5 77 71 5 78 71 5 79 71 5 80 71 5 + 81 71 5 82 71 5 83 71 5 84 71 5 85 71 5 86 71 5 1 1 1 2 1 1 + 3 1 1 4 1 1 5 1 1 6 1 1 7 1 1 8 1 1 9 1 1 10 1 1 11 1 1 + 12 1 1 13 1 1 14 1 1 15 1 1 16 1 1 17 1 1 18 1 1 19 1 1 + 20 1 1 21 1 1 22 22 2 23 22 2 24 22 2 25 22 2 26 22 2 27 22 2 + 28 22 2 29 22 2 30 22 2 31 22 2 32 22 2 33 22 2 34 22 2 + 35 22 2 36 22 2 37 22 2 38 22 2 39 22 2 40 22 2 41 22 2 + 42 22 2 43 43 3 44 43 3 45 43 3 46 43 3 47 43 3 48 43 3 + 49 43 3 50 43 3 51 43 3 52 43 3 53 43 3 54 43 3 55 43 3 + 56 43 3 57 43 3 58 43 3 59 43 3 60 43 3 61 43 3 62 43 3 + 63 43 3 64 64 4 65 64 4 66 64 4 67 64 4 68 64 4 69 64 4 + 70 64 4 71 64 4 72 64 4 73 64 4 74 64 4 75 64 4 76 64 4 + 77 64 4 78 64 4 79 64 4 80 64 4 81 64 4 82 64 4 83 64 4 + 84 84 5 85 84 5 86 84 5 87 84 5 88 84 5 89 84 5 90 84 5 + 91 84 5 92 84 5 93 84 5 94 84 5 95 84 5 96 84 5 97 84 5 + 98 84 5 99 84 5 100 84 5 101 84 5 102 84 5 103 84 5 104 84 5 + 105 84 5} + + +do_test 1.12.7.1 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY a ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0053 0.0105 0.0158 0.0211 0.0263 0.0316 0.0368 0.0421 0.0474 0.0526 0.0579 0.0632 0.0684 0.0737 0.0789 0.0842 0.0895 0.0947 0.1000 0.1053 0.1105 0.1158 0.1211 0.1263 0.1316 0.1368 0.1421 0.1474 0.1526 0.1579 0.1632 0.1684 0.1737 0.1789 0.1842 0.1895 0.1947 0.2000 0.2053 0.2105 0.2158 0.2211 0.2263 0.2316 0.2368 0.2421 0.2474 0.2526 0.2579 0.2632 0.2684 0.2737 0.2789 0.2842 0.2895 0.2947 0.3000 0.3053 0.3105 0.3158 0.3211 0.3263 0.3316 0.3368 0.3421 0.3474 0.3526 0.3579 0.3632 0.3684 0.3737 0.3789 0.3842 0.3895 0.3947 0.4000 0.4053 0.4105 0.4158 0.4211 0.4263 0.4316 0.4368 0.4421 0.4474 0.4526 0.4579 0.4632 0.4684 0.4737 0.4789 0.4842 0.4895 0.4947 0.5000 0.5053 0.5105 0.5158 0.5211 0.5263 0.5316 0.5368 0.5421 0.5474 0.5526 0.5579 0.5632 0.5684 0.5737 0.5789 0.5842 0.5895 0.5947 0.6000 0.6053 0.6105 0.6158 0.6211 0.6263 0.6316 0.6368 0.6421 0.6474 0.6526 0.6579 0.6632 0.6684 0.6737 0.6789 0.6842 0.6895 0.6947 0.7000 0.7053 0.7105 0.7158 0.7211 0.7263 0.7316 0.7368 0.7421 0.7474 0.7526 0.7579 0.7632 0.7684 0.7737 0.7789 0.7842 0.7895 0.7947 0.8000 0.8053 0.8105 0.8158 0.8211 0.8263 0.8316 0.8368 0.8421 0.8474 0.8526 0.8579 0.8632 0.8684 0.8737 0.8789 0.8842 0.8895 0.8947 0.9000 0.9053 0.9105 0.9158 0.9211 0.9263 0.9316 0.9368 0.9421 0.9474 0.9526 0.9579 0.9632 0.9684 0.9737 0.9789 0.9842 0.9895 0.9947 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.12.7.2 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0769 0.1538 0.2308 0.3077 0.3846 0.4615 0.5385 0.6154 0.6923 0.7692 0.8462 0.9231 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0667 0.1333 0.2000 0.2667 0.3333 0.4000 0.4667 0.5333 0.6000 0.6667 0.7333 0.8000 0.8667 0.9333 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0556 0.1111 0.1667 0.2222 0.2778 0.3333 0.3889 0.4444 0.5000 0.5556 0.6111 0.6667 0.7222 0.7778 0.8333 0.8889 0.9444 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0526 0.1053 0.1579 0.2105 0.2632 0.3158 0.3684 0.4211 0.4737 0.5263 0.5789 0.6316 0.6842 0.7368 0.7895 0.8421 0.8947 0.9474 1.0000 0.0000 0.0667 0.1333 0.2000 0.2667 0.3333 0.4000 0.4667 0.5333 0.6000 0.6667 0.7333 0.8000 0.8667 0.9333 1.0000 0.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.12.7.3 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY b ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0105 0.0105 0.0211 0.0211 0.0316 0.0368 0.0421 0.0474 0.0474 0.0474 0.0632 0.0632 0.0632 0.0789 0.0789 0.0789 0.0947 0.1000 0.1000 0.1105 0.1105 0.1105 0.1263 0.1263 0.1368 0.1421 0.1421 0.1421 0.1579 0.1579 0.1579 0.1737 0.1789 0.1842 0.1895 0.1895 0.2000 0.2000 0.2105 0.2105 0.2211 0.2263 0.2316 0.2316 0.2421 0.2421 0.2526 0.2579 0.2579 0.2579 0.2737 0.2737 0.2737 0.2895 0.2895 0.3000 0.3053 0.3053 0.3053 0.3053 0.3263 0.3263 0.3263 0.3263 0.3474 0.3474 0.3579 0.3579 0.3579 0.3579 0.3789 0.3789 0.3895 0.3895 0.4000 0.4000 0.4000 0.4158 0.4211 0.4211 0.4316 0.4368 0.4368 0.4474 0.4474 0.4579 0.4579 0.4684 0.4684 0.4684 0.4684 0.4895 0.4947 0.5000 0.5053 0.5105 0.5158 0.5211 0.5211 0.5316 0.5316 0.5316 0.5474 0.5526 0.5526 0.5526 0.5526 0.5737 0.5737 0.5737 0.5737 0.5947 0.6000 0.6000 0.6105 0.6105 0.6211 0.6263 0.6316 0.6316 0.6316 0.6474 0.6526 0.6579 0.6632 0.6684 0.6737 0.6737 0.6737 0.6895 0.6895 0.6895 0.7053 0.7053 0.7053 0.7211 0.7211 0.7211 0.7368 0.7421 0.7421 0.7526 0.7526 0.7632 0.7684 0.7684 0.7789 0.7789 0.7789 0.7947 0.8000 0.8053 0.8053 0.8053 0.8053 0.8263 0.8263 0.8263 0.8421 0.8474 0.8474 0.8579 0.8632 0.8632 0.8632 0.8789 0.8789 0.8789 0.8947 0.8947 0.8947 0.8947 0.8947 0.9211 0.9211 0.9211 0.9368 0.9421 0.9421 0.9421 0.9579 0.9579 0.9579 0.9737 0.9737 0.9842 0.9842 0.9947 0.9947} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.12.7.4 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0769 0.1538 0.1538 0.1538 0.3846 0.4615 0.5385 0.6154 0.6923 0.6923 0.8462 0.8462 0.8462 0.0000 0.0000 0.1000 0.1000 0.2000 0.2000 0.3000 0.3000 0.4000 0.4000 0.5000 0.5500 0.5500 0.6500 0.6500 0.6500 0.8000 0.8000 0.8000 0.8000 0.8000 0.0000 0.0000 0.1333 0.1333 0.1333 0.3333 0.3333 0.4667 0.5333 0.6000 0.6667 0.6667 0.8000 0.8000 0.8000 1.0000 0.0000 0.0000 0.1000 0.1000 0.2000 0.2000 0.3000 0.3000 0.3000 0.3000 0.5000 0.5000 0.6000 0.6500 0.7000 0.7000 0.7000 0.8500 0.9000 0.9000 0.9000 0.0000 0.0556 0.1111 0.1667 0.1667 0.1667 0.1667 0.3889 0.3889 0.5000 0.5556 0.6111 0.6111 0.6111 0.7778 0.7778 0.7778 0.7778 1.0000 0.0000 0.0500 0.0500 0.0500 0.2000 0.2500 0.2500 0.3500 0.3500 0.4500 0.4500 0.4500 0.6000 0.6000 0.6000 0.7500 0.7500 0.7500 0.9000 0.9000 0.9000 0.0000 0.0500 0.0500 0.0500 0.2000 0.2000 0.3000 0.3000 0.3000 0.3000 0.5000 0.5000 0.6000 0.6000 0.6000 0.7500 0.8000 0.8500 0.9000 0.9000 0.9000 0.0000 0.0000 0.0000 0.1579 0.2105 0.2105 0.3158 0.3158 0.4211 0.4211 0.4211 0.4211 0.6316 0.6842 0.7368 0.7368 0.8421 0.8421 0.9474 0.9474 0.0000 0.0000 0.0000 0.2000 0.2667 0.2667 0.4000 0.4000 0.4000 0.4000 0.6667 0.7333 0.7333 0.8667 0.9333 0.9333 0.0000 0.0000 0.0000 0.1429 0.1905 0.1905 0.1905 0.3333 0.3333 0.3333 0.4762 0.5238 0.5238 0.5238 0.5238 0.7143 0.7619 0.8095 0.8095 0.8095 0.9524 0.9524} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.12.7.5 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.12.7.6 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER (PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.12.8.1 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY a ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0052 0.0105 0.0157 0.0209 0.0262 0.0314 0.0366 0.0419 0.0471 0.0524 0.0576 0.0628 0.0681 0.0733 0.0785 0.0838 0.0890 0.0942 0.0995 0.1047 0.1099 0.1152 0.1204 0.1257 0.1309 0.1361 0.1414 0.1466 0.1518 0.1571 0.1623 0.1675 0.1728 0.1780 0.1832 0.1885 0.1937 0.1990 0.2042 0.2094 0.2147 0.2199 0.2251 0.2304 0.2356 0.2408 0.2461 0.2513 0.2565 0.2618 0.2670 0.2723 0.2775 0.2827 0.2880 0.2932 0.2984 0.3037 0.3089 0.3141 0.3194 0.3246 0.3298 0.3351 0.3403 0.3455 0.3508 0.3560 0.3613 0.3665 0.3717 0.3770 0.3822 0.3874 0.3927 0.3979 0.4031 0.4084 0.4136 0.4188 0.4241 0.4293 0.4346 0.4398 0.4450 0.4503 0.4555 0.4607 0.4660 0.4712 0.4764 0.4817 0.4869 0.4921 0.4974 0.5026 0.5079 0.5131 0.5183 0.5236 0.5288 0.5340 0.5393 0.5445 0.5497 0.5550 0.5602 0.5654 0.5707 0.5759 0.5812 0.5864 0.5916 0.5969 0.6021 0.6073 0.6126 0.6178 0.6230 0.6283 0.6335 0.6387 0.6440 0.6492 0.6545 0.6597 0.6649 0.6702 0.6754 0.6806 0.6859 0.6911 0.6963 0.7016 0.7068 0.7120 0.7173 0.7225 0.7277 0.7330 0.7382 0.7435 0.7487 0.7539 0.7592 0.7644 0.7696 0.7749 0.7801 0.7853 0.7906 0.7958 0.8010 0.8063 0.8115 0.8168 0.8220 0.8272 0.8325 0.8377 0.8429 0.8482 0.8534 0.8586 0.8639 0.8691 0.8743 0.8796 0.8848 0.8901 0.8953 0.9005 0.9058 0.9110 0.9162 0.9215 0.9267 0.9319 0.9372 0.9424 0.9476 0.9529 0.9581 0.9634 0.9686 0.9738 0.9791 0.9843 0.9895 0.9948 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.12.8.2 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0714 0.1429 0.2143 0.2857 0.3571 0.4286 0.5000 0.5714 0.6429 0.7143 0.7857 0.8571 0.9286 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0625 0.1250 0.1875 0.2500 0.3125 0.3750 0.4375 0.5000 0.5625 0.6250 0.6875 0.7500 0.8125 0.8750 0.9375 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0526 0.1053 0.1579 0.2105 0.2632 0.3158 0.3684 0.4211 0.4737 0.5263 0.5789 0.6316 0.6842 0.7368 0.7895 0.8421 0.8947 0.9474 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0625 0.1250 0.1875 0.2500 0.3125 0.3750 0.4375 0.5000 0.5625 0.6250 0.6875 0.7500 0.8125 0.8750 0.9375 1.0000 0.0455 0.0909 0.1364 0.1818 0.2273 0.2727 0.3182 0.3636 0.4091 0.4545 0.5000 0.5455 0.5909 0.6364 0.6818 0.7273 0.7727 0.8182 0.8636 0.9091 0.9545 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.12.8.3 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY b ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0105 0.0105 0.0209 0.0209 0.0314 0.0314 0.0366 0.0419 0.0471 0.0628 0.0628 0.0628 0.0785 0.0785 0.0785 0.0942 0.0942 0.0942 0.0995 0.1099 0.1099 0.1257 0.1257 0.1257 0.1361 0.1361 0.1414 0.1571 0.1571 0.1571 0.1728 0.1728 0.1728 0.1780 0.1832 0.1885 0.1990 0.1990 0.2094 0.2094 0.2199 0.2199 0.2251 0.2304 0.2408 0.2408 0.2513 0.2513 0.2565 0.2723 0.2723 0.2723 0.2880 0.2880 0.2880 0.2984 0.2984 0.3037 0.3246 0.3246 0.3246 0.3246 0.3455 0.3455 0.3455 0.3455 0.3560 0.3560 0.3770 0.3770 0.3770 0.3770 0.3874 0.3874 0.3979 0.3979 0.4136 0.4136 0.4136 0.4188 0.4293 0.4293 0.4346 0.4450 0.4450 0.4555 0.4555 0.4660 0.4660 0.4869 0.4869 0.4869 0.4869 0.4921 0.4974 0.5026 0.5079 0.5131 0.5183 0.5288 0.5288 0.5445 0.5445 0.5445 0.5497 0.5707 0.5707 0.5707 0.5707 0.5916 0.5916 0.5916 0.5916 0.5969 0.6073 0.6073 0.6178 0.6178 0.6230 0.6283 0.6440 0.6440 0.6440 0.6492 0.6545 0.6597 0.6649 0.6702 0.6859 0.6859 0.6859 0.7016 0.7016 0.7016 0.7173 0.7173 0.7173 0.7330 0.7330 0.7330 0.7382 0.7487 0.7487 0.7592 0.7592 0.7644 0.7749 0.7749 0.7906 0.7906 0.7906 0.7958 0.8010 0.8220 0.8220 0.8220 0.8220 0.8377 0.8377 0.8377 0.8429 0.8534 0.8534 0.8586 0.8743 0.8743 0.8743 0.8901 0.8901 0.8901 0.9162 0.9162 0.9162 0.9162 0.9162 0.9319 0.9319 0.9319 0.9372 0.9529 0.9529 0.9529 0.9686 0.9686 0.9686 0.9791 0.9791 0.9895 0.9895 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.12.8.4 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0714 0.1429 0.3571 0.3571 0.3571 0.4286 0.5000 0.5714 0.6429 0.7857 0.7857 1.0000 1.0000 1.0000 0.0952 0.0952 0.1905 0.1905 0.2857 0.2857 0.3810 0.3810 0.4762 0.4762 0.5238 0.6190 0.6190 0.7619 0.7619 0.7619 1.0000 1.0000 1.0000 1.0000 1.0000 0.1250 0.1250 0.3125 0.3125 0.3125 0.4375 0.4375 0.5000 0.5625 0.6250 0.7500 0.7500 0.9375 0.9375 0.9375 1.0000 0.0952 0.0952 0.1905 0.1905 0.2857 0.2857 0.4762 0.4762 0.4762 0.4762 0.5714 0.5714 0.6190 0.6667 0.8095 0.8095 0.8095 0.8571 1.0000 1.0000 1.0000 0.0526 0.1053 0.1579 0.3684 0.3684 0.3684 0.3684 0.4737 0.4737 0.5263 0.5789 0.7368 0.7368 0.7368 0.9474 0.9474 0.9474 0.9474 1.0000 0.0476 0.1905 0.1905 0.1905 0.2381 0.3333 0.3333 0.4286 0.4286 0.5714 0.5714 0.5714 0.7143 0.7143 0.7143 0.8571 0.8571 0.8571 1.0000 1.0000 1.0000 0.0476 0.1905 0.1905 0.1905 0.2857 0.2857 0.4762 0.4762 0.4762 0.4762 0.5714 0.5714 0.7143 0.7143 0.7143 0.7619 0.8095 0.8571 1.0000 1.0000 1.0000 0.1500 0.1500 0.1500 0.2000 0.3000 0.3000 0.4000 0.4000 0.6000 0.6000 0.6000 0.6000 0.6500 0.7000 0.8000 0.8000 0.9000 0.9000 1.0000 1.0000 0.1875 0.1875 0.1875 0.2500 0.3750 0.3750 0.6250 0.6250 0.6250 0.6250 0.6875 0.8125 0.8125 0.8750 1.0000 1.0000 0.1364 0.1364 0.1364 0.1818 0.3182 0.3182 0.3182 0.4545 0.4545 0.4545 0.5000 0.6818 0.6818 0.6818 0.6818 0.7273 0.7727 0.9091 0.9091 0.9091 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.12.8.5 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.12.8.6 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.12.8.1 { + set myres {} + foreach r [db eval {SELECT ntile(100) OVER ( ORDER BY a ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 88.0000 89.0000 89.0000 90.0000 90.0000 91.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.12.8.2 { + set myres {} + foreach r [db eval {SELECT ntile(101) OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 22.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.12.8.3 { + set myres {} + foreach r [db eval {SELECT ntile(102) OVER ( ORDER BY b,a ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 88.0000 89.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.12.8.4 { + set myres {} + foreach r [db eval {SELECT ntile(103) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 22.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.12.8.5 { + set myres {} + foreach r [db eval {SELECT ntile(104) OVER ( ORDER BY b%10,a ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000 103.0000 104.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.12.8.6 { + set myres {} + foreach r [db eval {SELECT ntile(105) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.12.8.7 { + set myres {} + foreach r [db eval {SELECT ntile(105) OVER ( ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 88.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000 103.0000 104.0000 105.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + +do_execsql_test 1.12.9.1 { + SELECT last_value(a+b) OVER ( ORDER BY a ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2 +} {52 83 55 79 109 65 26 120 119 50 70 103 80 124 + 36 96 59 124 116 110 57 51 52 130 103 74 87 48 + 128 117 105 136 131 71 133 92 109 63 84 109 57 + 146 78 147 113 74 88 150 87 110 65 121 106 110 + 124 85 145 107 161 171 150 156 80 171 120 109 158 + 114 111 136 147 87 173 124 168 173 162 132 101 154 + 167 190 161 110 156 195 198 102 123 177 169 140 111 + 180 119 160 197 152 124 121 134 146 147 132 213 141 + 193 200 210 157 132 136 175 161 218 188 226 191 187 + 208 211 179 138 144 223 196 214 170 212 202 163 184 + 172 173 195 229 240 187 210 200 163 227 228 223 191 + 252 235 225 243 172 187 202 179 179 182 231 261 207 + 263 206 189 209 212 276 181 274 249 239 234 213 234 + 269 196 271 221 210 229 235 250 223 232 229 279 224 + 280 216 207 207 207 207 207} + +do_execsql_test 1.12.9.2 { + SELECT last_value(a+b) OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2 +} {134 218 191 212 229 240 213 234 196 223 223 223 223 + 223 146 65 156 132 154 102 123 119 160 152 146 147 + 136 243 261 263 210 210 210 210 210 120 87 162 124 + 141 138 227 228 179 231 234 280 280 280 280 280 57 + 110 114 136 147 167 110 180 193 191 252 187 179 206 + 181 221 279 279 279 279 279 80 171 173 177 157 161 + 179 214 225 182 209 269 271 235 229 229 229 229 229 + 113 74 87 145 190 161 169 140 111 132 213 187 208 + 223 235 189 274 274 274 274 274 51 52 128 109 121 + 124 85 107 150 195 226 172 173 187 223 207 212 212 + 212 212 212 110 87 48 110 173 124 197 211 144 196 + 195 200 202 224 216 207 207 207 207 207 88 171 158 + 156 198 121 210 132 210 239 250 232 232 232 232 232 + 59 109 150 161 111 101 200 175 188 170 202 163 184 + 163 172 276 249 229 229 229 229 229} + +do_execsql_test 1.12.9.3 { + SELECT last_value(a+b) OVER ( ORDER BY b,a ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2 +} {57 181 80 111 26 48 144 207 36 121 132 101 163 + 172 196 119 136 63 124 179 110 179 182 74 132 189 + 51 52 85 216 163 134 123 210 78 141 57 187 71 87 + 172 173 50 224 88 59 111 170 109 213 223 146 147 + 84 114 191 206 221 157 161 209 229 74 140 107 187 + 207 212 124 202 52 232 55 184 229 106 132 152 120 + 92 110 179 235 65 70 87 110 195 200 175 234 160 + 234 136 80 113 187 109 121 124 196 156 210 239 250 + 72 109 188 202 191 105 154 79 231 147 225 103 161 + 169 223 96 83 249 212 162 227 228 167 180 193 117 + 177 214 145 208 235 150 110 211 103 158 200 168 229 + 92 156 243 280 279 116 173 269 271 131 133 223 128 + 173 197 210 99 150 161 147 218 240 109 136 146 261 + 263 124 130 252 171 190 213 274 108 195 226 119 124 + 171 198 120 276 276 276 276 276} + +do_execsql_test 1.12.9.4 { + SELECT last_value(a+b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2 +} {223 106 234 191 212 168 229 147 218 240 240 240 240 + 240 123 210 146 147 132 152 160 105 154 92 156 243 + 109 136 146 261 263 263 263 263 263 179 78 141 84 + 120 234 79 231 162 227 228 280 280 280 280 280 57 + 187 114 191 206 221 92 110 136 147 167 180 193 279 + 124 130 252 252 252 252 252 161 209 229 179 235 80 + 225 117 177 214 116 173 269 271 171 171 171 171 171 + 87 74 140 113 187 103 161 169 145 208 235 131 133 + 223 190 213 274 274 274 274 274 172 173 107 187 207 + 212 65 70 109 121 124 223 150 128 108 195 226 226 + 226 226 226 50 224 124 202 87 110 195 200 196 96 + 110 211 173 197 119 124 124 124 124 124 52 232 156 + 210 239 250 83 103 158 210 171 198 198 198 198 198 + 59 111 170 55 184 229 175 72 109 188 202 249 200 + 99 150 161 120 276 276 276 276 276} + +do_execsql_test 1.12.9.5 { + SELECT last_value(a+b) OVER ( ORDER BY b%10,a ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2 +} {134 218 191 212 229 240 213 234 196 223 92 109 105 + 136 146 65 156 132 154 102 123 119 160 152 146 147 + 136 243 261 263 210 79 63 84 78 120 87 162 124 + 141 138 227 228 179 231 234 280 124 57 130 92 57 + 110 114 136 147 167 110 180 193 191 252 187 179 206 + 181 221 279 80 116 117 71 80 171 173 177 157 161 + 179 214 225 182 209 269 271 235 229 103 74 131 133 + 113 74 87 145 190 161 169 140 111 132 213 187 208 + 223 235 189 274 108 65 26 70 51 52 128 109 121 + 124 85 107 150 195 226 172 173 187 223 207 212 119 + 50 124 96 110 87 48 110 173 124 197 211 144 196 + 195 200 202 224 216 207 52 83 103 36 88 171 158 + 156 198 121 210 132 210 239 250 232 99 72 55 120 + 59 109 150 161 111 101 200 175 188 170 202 163 184 + 163 172 276 249 229 229 229 229 229} + +do_execsql_test 1.12.9.6 { + SELECT last_value(a+b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING) FROM t2 +} {108 52 83 79 65 26 70 103 80 36 116 51 52 128 + 117 71 63 84 109 78 147 88 121 106 124 85 107 171 + 150 80 171 120 109 158 87 168 173 162 156 195 198 + 177 124 121 134 141 210 157 132 161 218 226 191 179 + 138 214 212 172 173 229 240 187 210 227 228 223 225 + 179 182 231 207 209 212 239 234 213 234 269 196 271 + 235 250 223 232 229 280 99 92 72 55 109 120 119 + 50 124 96 59 124 110 57 130 103 74 87 48 105 136 + 131 133 92 109 57 146 113 74 150 87 110 65 110 + 145 161 156 114 111 136 147 173 124 132 101 154 167 + 190 161 110 102 123 169 140 111 180 119 160 197 152 + 146 147 132 213 193 200 136 175 188 187 208 211 144 + 223 196 170 202 163 184 195 200 163 191 252 235 243 + 172 187 202 179 261 263 206 189 276 181 274 249 221 + 210 229 279 224 216 207} + +do_execsql_test 1.12.10.1 { + SELECT nth_value(b,b+1) OVER (ORDER BY a ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} 90 + {} {} {} {} {} {} {} {} {} 56 {} {} {} {} {} + {} {} {} {} {} {} 78 {} {} {} {} {} {} {} {} + 37 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 21 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} 85 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} 58 {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.12.10.2 { + SELECT nth_value(b,b+1) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 81 {} {} {} 21 {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} 12 {} {} {} 72 + {} {} {} {} {} {} {} {} {} {} 53 {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 34 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.12.10.3 { + SELECT nth_value(b,b+1) OVER ( ORDER BY b,a ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2 +} {1 2 3 3 5 6 7 {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.12.10.4 { + SELECT nth_value(b,b+1) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} 1 + 11 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} 12 12 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} 13 23 {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} 34 {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.12.10.5 { + SELECT nth_value(b,b+1) OVER ( ORDER BY b%10,a ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 81 {} {} {} 21 {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} 12 {} {} {} 72 + {} {} {} {} {} {} {} {} {} {} 53 {} {} {} {} + {} {} {} {} {} {} {} {} {} 54 {} {} {} {} {} + {} 34 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.12.10.6 { + SELECT nth_value(b,b+1) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.12.11.1 { + SELECT first_value(b) OVER (ORDER BY a ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING) FROM t2 +} {89 81 96 59 38 68 39 62 91 46 6 99 97 27 46 + 78 54 97 8 67 29 93 84 77 23 16 16 93 65 35 + 47 7 86 74 61 91 85 24 85 43 59 12 32 56 3 91 + 22 90 55 15 28 89 25 47 1 56 40 43 56 16 75 + 36 89 98 76 81 4 94 42 30 78 33 29 53 63 2 87 + 37 80 84 72 41 9 61 73 95 65 13 58 96 98 1 21 + 74 65 35 5 73 11 51 87 41 12 8 20 31 31 15 95 + 22 73 79 88 34 8 11 49 34 90 59 96 60 55 75 + 77 44 2 7 85 57 74 29 70 59 19 39 26 26 47 80 + 90 36 58 47 9 72 72 66 33 93 75 64 81 9 23 37 + 13 12 14 62 91 36 91 33 15 34 36 99 3 95 69 + 58 52 30 50 84 10 84 33 21 39 44 58 30 38 34 + 83 27 82 17 7} + +do_execsql_test 1.12.11.2 { + SELECT first_value(b) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING) FROM t2 +} {90 40 30 80 20 90 60 70 80 90 30 50 10 30 81 + 91 61 91 91 1 81 41 61 1 21 11 51 41 31 31 11 + 81 91 91 21 62 12 32 22 42 2 72 12 22 2 72 72 + 12 62 52 82 93 23 93 43 3 43 33 53 63 73 13 + 73 73 33 93 23 13 33 3 33 83 54 84 74 24 4 94 + 84 74 34 34 44 74 64 14 34 84 84 44 34 65 35 + 85 85 55 15 25 75 95 65 65 35 5 15 95 55 75 + 85 75 15 95 96 46 6 46 16 16 86 56 56 56 16 + 36 76 96 96 26 26 36 66 36 36 97 27 97 67 77 + 47 7 47 87 37 87 77 7 57 47 47 37 27 17 7 38 + 68 78 8 28 98 78 58 98 8 88 8 58 58 58 38 89 + 59 39 99 29 59 89 89 29 9 79 49 59 29 59 19 + 39 9 9 99 69 39} + +do_execsql_test 1.12.11.3 { + SELECT first_value(b) OVER ( ORDER BY b,a ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2 +} {1 1 2 2 3 3 4 5 6 7 7 7 8 8 8 9 9 9 10 + 11 11 12 12 12 13 13 14 15 15 15 16 16 16 17 + 19 20 21 21 22 22 23 23 24 25 26 26 27 27 28 + 29 29 29 30 30 30 31 31 32 33 33 33 33 34 34 + 34 34 35 35 36 36 36 36 37 37 38 38 39 39 39 + 40 41 41 42 43 43 44 44 46 46 47 47 47 47 49 + 50 51 52 53 54 55 55 56 56 56 57 58 58 58 58 + 59 59 59 59 60 61 61 62 62 63 64 65 65 65 66 + 67 68 69 70 72 72 72 73 73 73 74 74 74 75 75 + 75 76 77 77 78 78 79 80 80 81 81 81 82 83 84 + 84 84 84 85 85 85 86 87 87 88 89 89 89 90 90 + 90 91 91 91 91 91 93 93 93 94 95 95 95 96 96 + 96 97 97 98 98 99 99} + +do_execsql_test 1.12.11.4 { + SELECT first_value(b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2 +} {10 20 30 30 30 40 50 60 70 80 80 90 90 90 1 1 + 11 11 21 21 31 31 41 41 51 61 61 81 81 81 91 + 91 91 91 91 2 2 12 12 12 22 22 32 42 52 62 62 + 72 72 72 82 3 3 13 13 23 23 33 33 33 33 43 43 + 53 63 73 73 73 83 93 93 93 4 14 24 34 34 34 + 34 44 44 54 64 74 74 74 84 84 84 84 94 5 15 + 15 15 25 35 35 55 55 65 65 65 75 75 75 85 85 + 85 95 95 95 6 16 16 16 26 26 36 36 36 36 46 + 46 56 56 56 66 76 86 96 96 96 7 7 7 17 27 27 + 37 37 47 47 47 47 57 67 77 77 87 87 97 97 8 8 + 8 28 38 38 58 58 58 58 68 78 78 88 98 98 9 9 + 9 19 29 29 29 39 39 39 49 59 59 59 59 69 79 + 89 89 89 99 99} + +do_execsql_test 1.12.11.5 { + SELECT first_value(b) OVER ( ORDER BY b%10,a ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2 +} {90 40 30 80 20 90 60 70 80 90 30 50 10 30 81 + 91 61 91 91 1 81 41 61 1 21 11 51 41 31 31 11 + 81 91 91 21 62 12 32 22 42 2 72 12 22 2 72 72 + 12 62 52 82 93 23 93 43 3 43 33 53 63 73 13 + 73 73 33 93 23 13 33 3 33 83 54 84 74 24 4 94 + 84 74 34 34 44 74 64 14 34 84 84 44 34 65 35 + 85 85 55 15 25 75 95 65 65 35 5 15 95 55 75 + 85 75 15 95 96 46 6 46 16 16 86 56 56 56 16 + 36 76 96 96 26 26 36 66 36 36 97 27 97 67 77 + 47 7 47 87 37 87 77 7 57 47 47 37 27 17 7 38 + 68 78 8 28 98 78 58 98 8 88 8 58 58 58 38 89 + 59 39 99 29 59 89 89 29 9 79 49 59 29 59 19 + 39 9 9 99 69 39} + +do_execsql_test 1.12.11.6 { + SELECT first_value(b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING) FROM t2 +} {96 38 68 62 46 6 46 78 54 8 84 16 16 86 74 24 + 12 32 56 22 90 28 56 40 56 16 36 98 76 4 94 + 42 30 78 2 80 84 72 58 96 98 74 12 8 20 22 88 + 34 8 34 90 96 60 44 2 74 70 26 26 80 90 36 58 + 72 72 66 64 12 14 62 36 34 36 58 52 30 50 84 + 10 84 44 58 30 38 34 82 89 81 59 39 91 99 97 + 27 97 67 29 93 77 23 93 65 35 47 7 61 91 85 + 85 43 59 3 91 55 15 89 25 47 1 43 75 89 81 33 + 29 53 63 87 37 41 9 61 73 95 65 13 1 21 65 35 + 5 73 11 51 87 41 31 31 15 95 73 79 11 49 59 + 55 75 77 7 85 57 29 59 19 39 47 47 9 33 93 75 + 81 9 23 37 13 91 91 33 15 99 3 95 69 33 21 39 + 83 27 17 7} + +do_execsql_test 1.12.12.1 { + SELECT lead(b,b) OVER (ORDER BY a ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING) FROM t2 +} {96 9 11 89 32 53 91 30 51 56 54 73 22 59 75 + 74 78 8 16 65 15 8 31 87 90 12 32 96 74 76 37 + 85 90 15 35 2 60 36 75 9 51 47 63 51 90 26 42 + 26 8 76 80 90 37 87 56 79 5 87 8 2 39 73 64 + 36 90 72 78 36 73 51 33 20 41 2 26 37 33 8 14 + 33 81 55 1 9 12 39 64 87 72 34 82 21 34 99 62 + 74 41 69 22 75 27 58 8 79 77 26 26 55 {} 29 + 30 7 {} 66 55 2 34 64 {} 33 {} 44 84 {} {} 95 + 85 19 {} 83 {} 91 {} {} 9 50 91 33 34 {} {} + 84 {} 7 9 {} {} {} 44 {} {} {} {} 91 84 {} 95 + 95 52 {} {} {} {} {} 21 {} {} {} 58 {} {} {} + {} {} {} {} 83 {} {} {} {} {} {} {} {} {} {} + {} {} {} {}} + +do_execsql_test 1.12.12.2 { + SELECT lead(b,b) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 81 {} {} {} 21 {} {} {} {} {} {} + {} {} {} {} {} {} 62 {} {} {} 12 {} {} {} 72 + {} {} {} {} {} {} {} {} {} {} 53 {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 34 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} 95 {} {} {} {} {} {} 85 {} + {} {} {} {} {} {} {} {} {} 56 {} 36 {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 57 {} {} {} {} {} 7 {} {} {} {} + {} {} {} {} {} {} 8 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 9 {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.12.12.3 { + SELECT lead(b,b) OVER ( ORDER BY b,a ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2 +} {1 2 3 3 5 6 7 8 8 9 9 10 11 12 12 13 13 + 14 15 16 16 17 19 20 21 22 23 24 25 26 27 27 + 28 29 30 31 32 33 33 33 34 34 35 36 36 36 37 + 38 39 39 40 41 42 43 43 44 46 47 47 47 49 50 + 52 53 54 55 56 56 57 58 58 58 59 59 59 60 61 + 62 62 64 65 65 67 69 70 72 72 73 74 74 75 75 + 75 77 78 80 81 81 83 84 84 85 85 85 87 88 89 + 89 89 90 90 91 91 91 93 93 94 95 95 96 97 97 + 98 99 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.12.12.4 { + SELECT lead(b,b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2 +} {80 {} {} {} {} {} {} {} {} {} {} {} {} {} 1 + 11 81 81 {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} 12 12 72 82 {} {} {} {} {} {} + {} {} {} {} {} {} 13 23 73 73 {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} 34 84 {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 35 85 85 95 {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} 36 86 96 96 {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 37 47 + 47 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} 58 58 68 {} {} {} {} {} {} {} {} {} + {} {} {} {} 39 49 59 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.12.12.5 { + SELECT lead(b,b) OVER ( ORDER BY b%10,a ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2 +} {34 72 91 74 21 55 23 94 84 95 2 13 61 22 55 + 55 94 85 75 81 65 73 74 21 72 12 94 33 63 73 + 72 96 36 76 3 25 62 3 73 34 12 46 43 93 72 16 + 86 63 15 65 36 77 24 57 25 53 95 34 95 16 97 + 74 97 67 25 98 44 34 65 54 5 68 96 28 47 95 + 34 39 8 38 6 46 96 28 47 95 56 39 99 97 76 8 + 26 9 79 27 95 16 29 {} 58 58 77 85 56 {} 98 + 29 {} 19 96 {} {} 78 56 98 36 97 {} 89 89 29 + 47 78 {} {} {} 38 68 58 {} 58 38 {} 98 {} {} + {} 39 57 9 {} 79 {} {} 7 {} {} {} 9 29 38 78 + {} {} {} 8 39 {} {} {} {} 59 {} 99 {} {} {} + {} {} {} {} {} {} {} {} {} {} 9 {} {} {} {} + {} {} {} {} {} {} {} {}} + +do_execsql_test 1.12.12.6 { + SELECT lead(b,b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.12.13.1 { + SELECT lag(b,b) OVER (ORDER BY a ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} 38 {} {} {} {} + {} {} {} 6 {} {} {} {} {} 81 46 6 {} {} {} {} + 23 {} {} {} {} {} 27 {} {} {} 35 6 {} 12 {} + 23 {} {} 61 84 {} 93 39 47 {} 54 46 96 56 {} + 16 {} {} {} {} 89 {} 16 43 {} 85 56 29 99 53 + {} 59 {} {} 91 59 53 84 99 {} 93 63 47 {} {} + 98 33 67 35 75 1 23 13 55 27 75 98 35 73 63 2 + 21 27 13 24 86 23 84 31 20 94 61 65 75 23 36 + 94 55 90 41 77 96 56 29 40 12 89 63 11 5 73 + 79 1 16 28 31 73 5 39 53 63 41 11 40 2 13 33 + 9 29 90 47 72 9 73 30 44 33 74 93 29 74 42 34 + 63 41 34 96 47 77 1 36 74 72 14 36 26 77 9 72 + 64 8 91 31 52 30} + +do_execsql_test 1.12.13.2 { + SELECT lag(b,b) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} 30 {} {} + {} {} {} {} 91 {} {} {} 61 {} 81 {} {} {} {} + 1 {} {} {} {} {} {} {} {} {} 22 {} {} {} 12 + {} {} 62 {} {} {} {} {} {} {} 23 {} {} {} {} + {} {} {} {} {} {} {} 43 {} 23 {} {} {} {} {} + {} 54 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 75 {} + {} {} {} {} {} 55 {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} 47 {} {} {} {} + {} 27 7 {} {} {} {} {} {} {} {} {} 68 {} 8 {} + {} {} {} {} {} {} {} {} {} {} {} {} 89 {} {} + {} {} {} {} {} 29 9 {} {} {}} + +do_execsql_test 1.12.13.3 { + SELECT lag(b,b) OVER ( ORDER BY b,a ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2 +} {{} 1 1 1 1 2 2 2 2 2 2 3 3 3 4 4 5 6 6 + 6 7 7 7 7 7 8 8 8 8 8 8 9 9 9 9 9 9 9 + 9 9 9 10 10 10 10 11 11 11 11 11 12 12 12 12 + 13 13 13 13 13 14 15 15 15 15 16 16 16 16 16 + 17 19 20 20 21 21 21 21 22 22 22 22 23 23 23 + 23 23 24 23 24 24 25 26 26 26 26 26 26 26 26 + 26 26 26 27 27 27 27 28 29 29 29 29 30 30 30 + 30 30 30 31 31 31 31 31 32 32 32 32 32 32 31 + 32 33 33 33 33 33 33 34 34 34 34 34 34 34 34 + 35 35 35 35 35 36 36 36 36 36 36 36 37 37 37 + 38 38 38 38 38 38 39 39 39 39 40 40 41 41 42 + 43 42 43 43 43 43 44 44 44 46 46 46 47 47 47 + 47 47} + +do_execsql_test 1.12.13.4 { + SELECT lag(b,b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + 1 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.12.13.5 { + SELECT lag(b,b) OVER ( ORDER BY b%10,a ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} 30 {} {} + {} {} {} {} 91 {} {} {} 61 80 81 {} {} {} {} + 1 {} {} {} 30 {} 21 90 61 {} 22 {} 11 41 12 + {} {} 62 {} {} {} {} 31 {} 50 23 30 21 90 {} + {} 62 {} {} 81 {} 22 43 62 23 32 {} 91 {} 90 + 93 54 {} {} 90 72 12 22 90 81 83 23 80 20 72 + 43 51 33 80 90 2 34 54 1 20 62 12 13 75 44 30 + 93 91 1 21 55 61 61 13 85 3 65 65 91 73 33 93 + 55 84 62 31 11 65 35 85 33 55 15 12 75 22 3 + 73 65 36 85 43 95 43 13 47 44 65 65 96 36 27 + 7 46 34 94 47 36 73 34 35 73 68 24 8 75 85 75 + 66 34 95 36 84 77 46 34 84 47 89 65 36 16 38 + 76 58 57 29 9 44 56 17} + +do_execsql_test 1.12.13.6 { + SELECT lag(b,b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.12.14.1 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (ORDER BY a ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING) FROM t2 +} {89.81.96.59.38 81.96.59.38.68 96.59.38.68.39 59.38.68.39.62 + 38.68.39.62.91 68.39.62.91.46 39.62.91.46.6 62.91.46.6.99 + 91.46.6.99.97 46.6.99.97.27 6.99.97.27.46 99.97.27.46.78 + 97.27.46.78.54 27.46.78.54.97 46.78.54.97.8 78.54.97.8.67 + 54.97.8.67.29 97.8.67.29.93 8.67.29.93.84 67.29.93.84.77 + 29.93.84.77.23 93.84.77.23.16 84.77.23.16.16 77.23.16.16.93 + 23.16.16.93.65 16.16.93.65.35 16.93.65.35.47 93.65.35.47.7 + 65.35.47.7.86 35.47.7.86.74 47.7.86.74.61 7.86.74.61.91 + 86.74.61.91.85 74.61.91.85.24 61.91.85.24.85 91.85.24.85.43 + 85.24.85.43.59 24.85.43.59.12 85.43.59.12.32 43.59.12.32.56 + 59.12.32.56.3 12.32.56.3.91 32.56.3.91.22 56.3.91.22.90 + 3.91.22.90.55 91.22.90.55.15 22.90.55.15.28 90.55.15.28.89 + 55.15.28.89.25 15.28.89.25.47 28.89.25.47.1 89.25.47.1.56 + 25.47.1.56.40 47.1.56.40.43 1.56.40.43.56 56.40.43.56.16 + 40.43.56.16.75 43.56.16.75.36 56.16.75.36.89 16.75.36.89.98 + 75.36.89.98.76 36.89.98.76.81 89.98.76.81.4 98.76.81.4.94 + 76.81.4.94.42 81.4.94.42.30 4.94.42.30.78 94.42.30.78.33 + 42.30.78.33.29 30.78.33.29.53 78.33.29.53.63 33.29.53.63.2 + 29.53.63.2.87 53.63.2.87.37 63.2.87.37.80 2.87.37.80.84 + 87.37.80.84.72 37.80.84.72.41 80.84.72.41.9 84.72.41.9.61 + 72.41.9.61.73 41.9.61.73.95 9.61.73.95.65 61.73.95.65.13 + 73.95.65.13.58 95.65.13.58.96 65.13.58.96.98 13.58.96.98.1 + 58.96.98.1.21 96.98.1.21.74 98.1.21.74.65 1.21.74.65.35 + 21.74.65.35.5 74.65.35.5.73 65.35.5.73.11 35.5.73.11.51 + 5.73.11.51.87 73.11.51.87.41 11.51.87.41.12 51.87.41.12.8 + 87.41.12.8.20 41.12.8.20.31 12.8.20.31.31 8.20.31.31.15 + 20.31.31.15.95 31.31.15.95.22 31.15.95.22.73 15.95.22.73.79 + 95.22.73.79.88 22.73.79.88.34 73.79.88.34.8 79.88.34.8.11 + 88.34.8.11.49 34.8.11.49.34 8.11.49.34.90 11.49.34.90.59 + 49.34.90.59.96 34.90.59.96.60 90.59.96.60.55 59.96.60.55.75 + 96.60.55.75.77 60.55.75.77.44 55.75.77.44.2 75.77.44.2.7 + 77.44.2.7.85 44.2.7.85.57 2.7.85.57.74 7.85.57.74.29 85.57.74.29.70 + 57.74.29.70.59 74.29.70.59.19 29.70.59.19.39 70.59.19.39.26 + 59.19.39.26.26 19.39.26.26.47 39.26.26.47.80 26.26.47.80.90 + 26.47.80.90.36 47.80.90.36.58 80.90.36.58.47 90.36.58.47.9 + 36.58.47.9.72 58.47.9.72.72 47.9.72.72.66 9.72.72.66.33 + 72.72.66.33.93 72.66.33.93.75 66.33.93.75.64 33.93.75.64.81 + 93.75.64.81.9 75.64.81.9.23 64.81.9.23.37 81.9.23.37.13 + 9.23.37.13.12 23.37.13.12.14 37.13.12.14.62 13.12.14.62.91 + 12.14.62.91.36 14.62.91.36.91 62.91.36.91.33 91.36.91.33.15 + 36.91.33.15.34 91.33.15.34.36 33.15.34.36.99 15.34.36.99.3 + 34.36.99.3.95 36.99.3.95.69 99.3.95.69.58 3.95.69.58.52 + 95.69.58.52.30 69.58.52.30.50 58.52.30.50.84 52.30.50.84.10 + 30.50.84.10.84 50.84.10.84.33 84.10.84.33.21 10.84.33.21.39 + 84.33.21.39.44 33.21.39.44.58 21.39.44.58.30 39.44.58.30.38 + 44.58.30.38.34 58.30.38.34.83 30.38.34.83.27 38.34.83.27.82 + 34.83.27.82.17 83.27.82.17.7 27.82.17.7 82.17.7 17.7 7} + +do_execsql_test 1.12.14.2 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING) FROM t2 +} {90.40.30.80.20 40.30.80.20.90 30.80.20.90.60 80.20.90.60.70 + 20.90.60.70.80 90.60.70.80.90 60.70.80.90.30 70.80.90.30.50 + 80.90.30.50.10 90.30.50.10.30 30.50.10.30 50.10.30 10.30 30 + 81.91.61.91.91 91.61.91.91.1 61.91.91.1.81 91.91.1.81.41 + 91.1.81.41.61 1.81.41.61.1 81.41.61.1.21 41.61.1.21.11 + 61.1.21.11.51 1.21.11.51.41 21.11.51.41.31 11.51.41.31.31 + 51.41.31.31.11 41.31.31.11.81 31.31.11.81.91 31.11.81.91.91 + 11.81.91.91.21 81.91.91.21 91.91.21 91.21 21 62.12.32.22.42 + 12.32.22.42.2 32.22.42.2.72 22.42.2.72.12 42.2.72.12.22 + 2.72.12.22.2 72.12.22.2.72 12.22.2.72.72 22.2.72.72.12 + 2.72.72.12.62 72.72.12.62.52 72.12.62.52.82 12.62.52.82 62.52.82 + 52.82 82 93.23.93.43.3 23.93.43.3.43 93.43.3.43.33 43.3.43.33.53 + 3.43.33.53.63 43.33.53.63.73 33.53.63.73.13 53.63.73.13.73 + 63.73.13.73.73 73.13.73.73.33 13.73.73.33.93 73.73.33.93.23 + 73.33.93.23.13 33.93.23.13.33 93.23.13.33.3 23.13.33.3.33 + 13.33.3.33.83 33.3.33.83 3.33.83 33.83 83 54.84.74.24.4 + 84.74.24.4.94 74.24.4.94.84 24.4.94.84.74 4.94.84.74.34 + 94.84.74.34.34 84.74.34.34.44 74.34.34.44.74 34.34.44.74.64 + 34.44.74.64.14 44.74.64.14.34 74.64.14.34.84 64.14.34.84.84 + 14.34.84.84.44 34.84.84.44.34 84.84.44.34 84.44.34 44.34 34 + 65.35.85.85.55 35.85.85.55.15 85.85.55.15.25 85.55.15.25.75 + 55.15.25.75.95 15.25.75.95.65 25.75.95.65.65 75.95.65.65.35 + 95.65.65.35.5 65.65.35.5.15 65.35.5.15.95 35.5.15.95.55 + 5.15.95.55.75 15.95.55.75.85 95.55.75.85.75 55.75.85.75.15 + 75.85.75.15.95 85.75.15.95 75.15.95 15.95 95 96.46.6.46.16 + 46.6.46.16.16 6.46.16.16.86 46.16.16.86.56 16.16.86.56.56 + 16.86.56.56.56 86.56.56.56.16 56.56.56.16.36 56.56.16.36.76 + 56.16.36.76.96 16.36.76.96.96 36.76.96.96.26 76.96.96.26.26 + 96.96.26.26.36 96.26.26.36.66 26.26.36.66.36 26.36.66.36.36 + 36.66.36.36 66.36.36 36.36 36 97.27.97.67.77 27.97.67.77.47 + 97.67.77.47.7 67.77.47.7.47 77.47.7.47.87 47.7.47.87.37 + 7.47.87.37.87 47.87.37.87.77 87.37.87.77.7 37.87.77.7.57 + 87.77.7.57.47 77.7.57.47.47 7.57.47.47.37 57.47.47.37.27 + 47.47.37.27.17 47.37.27.17.7 37.27.17.7 27.17.7 17.7 7 + 38.68.78.8.28 68.78.8.28.98 78.8.28.98.78 8.28.98.78.58 + 28.98.78.58.98 98.78.58.98.8 78.58.98.8.88 58.98.8.88.8 + 98.8.88.8.58 8.88.8.58.58 88.8.58.58.58 8.58.58.58.38 58.58.58.38 + 58.58.38 58.38 38 89.59.39.99.29 59.39.99.29.59 39.99.29.59.89 + 99.29.59.89.89 29.59.89.89.29 59.89.89.29.9 89.89.29.9.79 + 89.29.9.79.49 29.9.79.49.59 9.79.49.59.29 79.49.59.29.59 + 49.59.29.59.19 59.29.59.19.39 29.59.19.39.9 59.19.39.9.9 + 19.39.9.9.99 39.9.9.99.69 9.9.99.69.39 9.99.69.39 99.69.39 69.39 + 39} + +do_execsql_test 1.12.14.3 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( ORDER BY b,a ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2 +} {1.1.2.2.3 1.2.2.3.3 2.2.3.3.4 2.3.3.4.5 3.3.4.5.6 3.4.5.6.7 + 4.5.6.7.7 5.6.7.7.7 6.7.7.7.8 7.7.7.8.8 7.7.8.8.8 7.8.8.8.9 + 8.8.8.9.9 8.8.9.9.9 8.9.9.9.10 9.9.9.10.11 9.9.10.11.11 + 9.10.11.11.12 10.11.11.12.12 11.11.12.12.12 11.12.12.12.13 + 12.12.12.13.13 12.12.13.13.14 12.13.13.14.15 13.13.14.15.15 + 13.14.15.15.15 14.15.15.15.16 15.15.15.16.16 15.15.16.16.16 + 15.16.16.16.17 16.16.16.17.19 16.16.17.19.20 16.17.19.20.21 + 17.19.20.21.21 19.20.21.21.22 20.21.21.22.22 21.21.22.22.23 + 21.22.22.23.23 22.22.23.23.24 22.23.23.24.25 23.23.24.25.26 + 23.24.25.26.26 24.25.26.26.27 25.26.26.27.27 26.26.27.27.28 + 26.27.27.28.29 27.27.28.29.29 27.28.29.29.29 28.29.29.29.30 + 29.29.29.30.30 29.29.30.30.30 29.30.30.30.31 30.30.30.31.31 + 30.30.31.31.32 30.31.31.32.33 31.31.32.33.33 31.32.33.33.33 + 32.33.33.33.33 33.33.33.33.34 33.33.33.34.34 33.33.34.34.34 + 33.34.34.34.34 34.34.34.34.35 34.34.34.35.35 34.34.35.35.36 + 34.35.35.36.36 35.35.36.36.36 35.36.36.36.36 36.36.36.36.37 + 36.36.36.37.37 36.36.37.37.38 36.37.37.38.38 37.37.38.38.39 + 37.38.38.39.39 38.38.39.39.39 38.39.39.39.40 39.39.39.40.41 + 39.39.40.41.41 39.40.41.41.42 40.41.41.42.43 41.41.42.43.43 + 41.42.43.43.44 42.43.43.44.44 43.43.44.44.46 43.44.44.46.46 + 44.44.46.46.47 44.46.46.47.47 46.46.47.47.47 46.47.47.47.47 + 47.47.47.47.49 47.47.47.49.50 47.47.49.50.51 47.49.50.51.52 + 49.50.51.52.53 50.51.52.53.54 51.52.53.54.55 52.53.54.55.55 + 53.54.55.55.56 54.55.55.56.56 55.55.56.56.56 55.56.56.56.57 + 56.56.56.57.58 56.56.57.58.58 56.57.58.58.58 57.58.58.58.58 + 58.58.58.58.59 58.58.58.59.59 58.58.59.59.59 58.59.59.59.59 + 59.59.59.59.60 59.59.59.60.61 59.59.60.61.61 59.60.61.61.62 + 60.61.61.62.62 61.61.62.62.63 61.62.62.63.64 62.62.63.64.65 + 62.63.64.65.65 63.64.65.65.65 64.65.65.65.66 65.65.65.66.67 + 65.65.66.67.68 65.66.67.68.69 66.67.68.69.70 67.68.69.70.72 + 68.69.70.72.72 69.70.72.72.72 70.72.72.72.73 72.72.72.73.73 + 72.72.73.73.73 72.73.73.73.74 73.73.73.74.74 73.73.74.74.74 + 73.74.74.74.75 74.74.74.75.75 74.74.75.75.75 74.75.75.75.76 + 75.75.75.76.77 75.75.76.77.77 75.76.77.77.78 76.77.77.78.78 + 77.77.78.78.79 77.78.78.79.80 78.78.79.80.80 78.79.80.80.81 + 79.80.80.81.81 80.80.81.81.81 80.81.81.81.82 81.81.81.82.83 + 81.81.82.83.84 81.82.83.84.84 82.83.84.84.84 83.84.84.84.84 + 84.84.84.84.85 84.84.84.85.85 84.84.85.85.85 84.85.85.85.86 + 85.85.85.86.87 85.85.86.87.87 85.86.87.87.88 86.87.87.88.89 + 87.87.88.89.89 87.88.89.89.89 88.89.89.89.90 89.89.89.90.90 + 89.89.90.90.90 89.90.90.90.91 90.90.90.91.91 90.90.91.91.91 + 90.91.91.91.91 91.91.91.91.91 91.91.91.91.93 91.91.91.93.93 + 91.91.93.93.93 91.93.93.93.94 93.93.93.94.95 93.93.94.95.95 + 93.94.95.95.95 94.95.95.95.96 95.95.95.96.96 95.95.96.96.96 + 95.96.96.96.97 96.96.96.97.97 96.96.97.97.98 96.97.97.98.98 + 97.97.98.98.99 97.98.98.99.99 98.98.99.99 98.99.99 99.99 99} + +do_execsql_test 1.12.14.4 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2 +} {10.20.30.30.30 20.30.30.30.40 30.30.30.40.50 30.30.40.50.60 + 30.40.50.60.70 40.50.60.70.80 50.60.70.80.80 60.70.80.80.90 + 70.80.80.90.90 80.80.90.90.90 80.90.90.90 90.90.90 90.90 90 + 1.1.11.11.21 1.11.11.21.21 11.11.21.21.31 11.21.21.31.31 + 21.21.31.31.41 21.31.31.41.41 31.31.41.41.51 31.41.41.51.61 + 41.41.51.61.61 41.51.61.61.81 51.61.61.81.81 61.61.81.81.81 + 61.81.81.81.91 81.81.81.91.91 81.81.91.91.91 81.91.91.91.91 + 91.91.91.91.91 91.91.91.91 91.91.91 91.91 91 2.2.12.12.12 + 2.12.12.12.22 12.12.12.22.22 12.12.22.22.32 12.22.22.32.42 + 22.22.32.42.52 22.32.42.52.62 32.42.52.62.62 42.52.62.62.72 + 52.62.62.72.72 62.62.72.72.72 62.72.72.72.82 72.72.72.82 72.72.82 + 72.82 82 3.3.13.13.23 3.13.13.23.23 13.13.23.23.33 13.23.23.33.33 + 23.23.33.33.33 23.33.33.33.33 33.33.33.33.43 33.33.33.43.43 + 33.33.43.43.53 33.43.43.53.63 43.43.53.63.73 43.53.63.73.73 + 53.63.73.73.73 63.73.73.73.83 73.73.73.83.93 73.73.83.93.93 + 73.83.93.93.93 83.93.93.93 93.93.93 93.93 93 4.14.24.34.34 + 14.24.34.34.34 24.34.34.34.34 34.34.34.34.44 34.34.34.44.44 + 34.34.44.44.54 34.44.44.54.64 44.44.54.64.74 44.54.64.74.74 + 54.64.74.74.74 64.74.74.74.84 74.74.74.84.84 74.74.84.84.84 + 74.84.84.84.84 84.84.84.84.94 84.84.84.94 84.84.94 84.94 94 + 5.15.15.15.25 15.15.15.25.35 15.15.25.35.35 15.25.35.35.55 + 25.35.35.55.55 35.35.55.55.65 35.55.55.65.65 55.55.65.65.65 + 55.65.65.65.75 65.65.65.75.75 65.65.75.75.75 65.75.75.75.85 + 75.75.75.85.85 75.75.85.85.85 75.85.85.85.95 85.85.85.95.95 + 85.85.95.95.95 85.95.95.95 95.95.95 95.95 95 6.16.16.16.26 + 16.16.16.26.26 16.16.26.26.36 16.26.26.36.36 26.26.36.36.36 + 26.36.36.36.36 36.36.36.36.46 36.36.36.46.46 36.36.46.46.56 + 36.46.46.56.56 46.46.56.56.56 46.56.56.56.66 56.56.56.66.76 + 56.56.66.76.86 56.66.76.86.96 66.76.86.96.96 76.86.96.96.96 + 86.96.96.96 96.96.96 96.96 96 7.7.7.17.27 7.7.17.27.27 + 7.17.27.27.37 17.27.27.37.37 27.27.37.37.47 27.37.37.47.47 + 37.37.47.47.47 37.47.47.47.47 47.47.47.47.57 47.47.47.57.67 + 47.47.57.67.77 47.57.67.77.77 57.67.77.77.87 67.77.77.87.87 + 77.77.87.87.97 77.87.87.97.97 87.87.97.97 87.97.97 97.97 97 + 8.8.8.28.38 8.8.28.38.38 8.28.38.38.58 28.38.38.58.58 + 38.38.58.58.58 38.58.58.58.58 58.58.58.58.68 58.58.58.68.78 + 58.58.68.78.78 58.68.78.78.88 68.78.78.88.98 78.78.88.98.98 + 78.88.98.98 88.98.98 98.98 98 9.9.9.19.29 9.9.19.29.29 + 9.19.29.29.29 19.29.29.29.39 29.29.29.39.39 29.29.39.39.39 + 29.39.39.39.49 39.39.39.49.59 39.39.49.59.59 39.49.59.59.59 + 49.59.59.59.59 59.59.59.59.69 59.59.59.69.79 59.59.69.79.89 + 59.69.79.89.89 69.79.89.89.89 79.89.89.89.99 89.89.89.99.99 + 89.89.99.99 89.99.99 99.99 99} + +do_execsql_test 1.12.14.5 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( ORDER BY b%10,a ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING ) FROM t2 +} {90.40.30.80.20 40.30.80.20.90 30.80.20.90.60 80.20.90.60.70 + 20.90.60.70.80 90.60.70.80.90 60.70.80.90.30 70.80.90.30.50 + 80.90.30.50.10 90.30.50.10.30 30.50.10.30.81 50.10.30.81.91 + 10.30.81.91.61 30.81.91.61.91 81.91.61.91.91 91.61.91.91.1 + 61.91.91.1.81 91.91.1.81.41 91.1.81.41.61 1.81.41.61.1 + 81.41.61.1.21 41.61.1.21.11 61.1.21.11.51 1.21.11.51.41 + 21.11.51.41.31 11.51.41.31.31 51.41.31.31.11 41.31.31.11.81 + 31.31.11.81.91 31.11.81.91.91 11.81.91.91.21 81.91.91.21.62 + 91.91.21.62.12 91.21.62.12.32 21.62.12.32.22 62.12.32.22.42 + 12.32.22.42.2 32.22.42.2.72 22.42.2.72.12 42.2.72.12.22 + 2.72.12.22.2 72.12.22.2.72 12.22.2.72.72 22.2.72.72.12 + 2.72.72.12.62 72.72.12.62.52 72.12.62.52.82 12.62.52.82.93 + 62.52.82.93.23 52.82.93.23.93 82.93.23.93.43 93.23.93.43.3 + 23.93.43.3.43 93.43.3.43.33 43.3.43.33.53 3.43.33.53.63 + 43.33.53.63.73 33.53.63.73.13 53.63.73.13.73 63.73.13.73.73 + 73.13.73.73.33 13.73.73.33.93 73.73.33.93.23 73.33.93.23.13 + 33.93.23.13.33 93.23.13.33.3 23.13.33.3.33 13.33.3.33.83 + 33.3.33.83.54 3.33.83.54.84 33.83.54.84.74 83.54.84.74.24 + 54.84.74.24.4 84.74.24.4.94 74.24.4.94.84 24.4.94.84.74 + 4.94.84.74.34 94.84.74.34.34 84.74.34.34.44 74.34.34.44.74 + 34.34.44.74.64 34.44.74.64.14 44.74.64.14.34 74.64.14.34.84 + 64.14.34.84.84 14.34.84.84.44 34.84.84.44.34 84.84.44.34.65 + 84.44.34.65.35 44.34.65.35.85 34.65.35.85.85 65.35.85.85.55 + 35.85.85.55.15 85.85.55.15.25 85.55.15.25.75 55.15.25.75.95 + 15.25.75.95.65 25.75.95.65.65 75.95.65.65.35 95.65.65.35.5 + 65.65.35.5.15 65.35.5.15.95 35.5.15.95.55 5.15.95.55.75 + 15.95.55.75.85 95.55.75.85.75 55.75.85.75.15 75.85.75.15.95 + 85.75.15.95.96 75.15.95.96.46 15.95.96.46.6 95.96.46.6.46 + 96.46.6.46.16 46.6.46.16.16 6.46.16.16.86 46.16.16.86.56 + 16.16.86.56.56 16.86.56.56.56 86.56.56.56.16 56.56.56.16.36 + 56.56.16.36.76 56.16.36.76.96 16.36.76.96.96 36.76.96.96.26 + 76.96.96.26.26 96.96.26.26.36 96.26.26.36.66 26.26.36.66.36 + 26.36.66.36.36 36.66.36.36.97 66.36.36.97.27 36.36.97.27.97 + 36.97.27.97.67 97.27.97.67.77 27.97.67.77.47 97.67.77.47.7 + 67.77.47.7.47 77.47.7.47.87 47.7.47.87.37 7.47.87.37.87 + 47.87.37.87.77 87.37.87.77.7 37.87.77.7.57 87.77.7.57.47 + 77.7.57.47.47 7.57.47.47.37 57.47.47.37.27 47.47.37.27.17 + 47.37.27.17.7 37.27.17.7.38 27.17.7.38.68 17.7.38.68.78 + 7.38.68.78.8 38.68.78.8.28 68.78.8.28.98 78.8.28.98.78 + 8.28.98.78.58 28.98.78.58.98 98.78.58.98.8 78.58.98.8.88 + 58.98.8.88.8 98.8.88.8.58 8.88.8.58.58 88.8.58.58.58 8.58.58.58.38 + 58.58.58.38.89 58.58.38.89.59 58.38.89.59.39 38.89.59.39.99 + 89.59.39.99.29 59.39.99.29.59 39.99.29.59.89 99.29.59.89.89 + 29.59.89.89.29 59.89.89.29.9 89.89.29.9.79 89.29.9.79.49 + 29.9.79.49.59 9.79.49.59.29 79.49.59.29.59 49.59.29.59.19 + 59.29.59.19.39 29.59.19.39.9 59.19.39.9.9 19.39.9.9.99 39.9.9.99.69 + 9.9.99.69.39 9.99.69.39 99.69.39 69.39 39} + +do_execsql_test 1.12.14.6 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING) FROM t2 +} {96 38 68 62 46 6 46 78 54 8 84 16 16 86 74 24 + 12 32 56 22 90 28 56 40 56 16 36 98 76 4 94 + 42 30 78 2 80 84 72 58 96 98 74 12 8 20 22 88 + 34 8 34 90 96 60 44 2 74 70 26 26 80 90 36 58 + 72 72 66 64 12 14 62 36 34 36 58 52 30 50 84 + 10 84 44 58 30 38 34 82 89 81 59 39 91 99 97 + 27 97 67 29 93 77 23 93 65 35 47 7 61 91 85 + 85 43 59 3 91 55 15 89 25 47 1 43 75 89 81 33 + 29 53 63 87 37 41 9 61 73 95 65 13 1 21 65 35 + 5 73 11 51 87 41 31 31 15 95 73 79 11 49 59 + 55 75 77 7 85 57 29 59 19 39 47 47 9 33 93 75 + 81 9 23 37 13 91 91 33 15 99 3 95 69 33 21 39 + 83 27 17 7} + +do_execsql_test 1.12.14.7 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (win1 ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING) + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a) + ORDER BY 1 +} {1 1 10 11 11 12 12 12 13 13 14 15 15 15 16 16 + 16 17 19 2 2 20 21 21 22 22 23 23 24 25 26 26 + 27 27 28 29 29 29 3 3 30 30 30 31 31 32 33 33 + 33 33 34 34 34 34 35 35 36 36 36 36 37 37 38 + 38 39 39 39 4 40 41 41 42 43 43 44 44 46 46 + 47 47 47 47 49 5 50 51 52 53 54 55 55 56 56 + 56 57 58 58 58 58 59 59 59 59 6 60 61 61 62 + 62 63 64 65 65 65 66 67 68 69 7 7 7 70 72 72 + 72 73 73 73 74 74 74 75 75 75 76 77 77 78 78 + 79 8 8 8 80 80 81 81 81 82 83 84 84 84 84 85 + 85 85 86 87 87 88 89 89 89 9 9 9 90 90 90 91 + 91 91 91 91 93 93 93 94 95 95 95 96 96 96 97 + 97 98 98 99 99} + +do_execsql_test 1.12.14.8 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (win1 ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING) + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a ORDER BY b%10) + ORDER BY 1 +} {1 1 10 11 11 12 12 12 13 13 14 15 15 15 16 16 + 16 17 19 2 2 20 21 21 22 22 23 23 24 25 26 26 + 27 27 28 29 29 29 3 3 30 30 30 31 31 32 33 33 + 33 33 34 34 34 34 35 35 36 36 36 36 37 37 38 + 38 39 39 39 4 40 41 41 42 43 43 44 44 46 46 + 47 47 47 47 49 5 50 51 52 53 54 55 55 56 56 + 56 57 58 58 58 58 59 59 59 59 6 60 61 61 62 + 62 63 64 65 65 65 66 67 68 69 7 7 7 70 72 72 + 72 73 73 73 74 74 74 75 75 75 76 77 77 78 78 + 79 8 8 8 80 80 81 81 81 82 83 84 84 84 84 85 + 85 85 86 87 87 88 89 89 89 9 9 9 90 90 90 91 + 91 91 91 91 93 93 93 94 95 95 95 96 96 96 97 + 97 98 98 99 99} + +do_execsql_test 1.12.14.9 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER win2 + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a ORDER BY b%10), + win2 AS (win1 ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING) + ORDER BY 1 +} {1 1 10 11 11 12 12 12 13 13 14 15 15 15 16 16 + 16 17 19 2 2 20 21 21 22 22 23 23 24 25 26 26 + 27 27 28 29 29 29 3 3 30 30 30 31 31 32 33 33 + 33 33 34 34 34 34 35 35 36 36 36 36 37 37 38 + 38 39 39 39 4 40 41 41 42 43 43 44 44 46 46 + 47 47 47 47 49 5 50 51 52 53 54 55 55 56 56 + 56 57 58 58 58 58 59 59 59 59 6 60 61 61 62 + 62 63 64 65 65 65 66 67 68 69 7 7 7 70 72 72 + 72 73 73 73 74 74 74 75 75 75 76 77 77 78 78 + 79 8 8 8 80 80 81 81 81 82 83 84 84 84 84 85 + 85 85 86 87 87 88 89 89 89 9 9 9 90 90 90 91 + 91 91 91 91 93 93 93 94 95 95 95 96 96 96 97 + 97 98 98 99 99} + +do_execsql_test 1.12.15.1 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE a%2=0) OVER win FROM t2 + WINDOW win AS (ORDER BY a ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING) +} {5 89.96.38 5 96.38 5 96.38.39 5 38.39 5 38.39.91 5 39.91 + 5 39.91.6 5 91.6 5 91.6.97 5 6.97 5 6.97.46 5 97.46 5 97.46.54 + 5 46.54 5 46.54.8 5 54.8 5 54.8.29 5 8.29 5 8.29.84 5 29.84 + 5 29.84.23 5 84.23 5 84.23.16 5 23.16 5 23.16.65 5 16.65 + 5 16.65.47 5 65.47 5 65.47.86 5 47.86 5 47.86.61 5 86.61 + 5 86.61.85 5 61.85 5 61.85.85 5 85.85 5 85.85.59 5 85.59 + 5 85.59.32 5 59.32 5 59.32.3 5 32.3 5 32.3.22 5 3.22 5 3.22.55 + 5 22.55 5 22.55.28 5 55.28 5 55.28.25 5 28.25 5 28.25.1 5 25.1 + 5 25.1.40 5 1.40 5 1.40.56 5 40.56 5 40.56.75 5 56.75 + 5 56.75.89 5 75.89 5 75.89.76 5 89.76 5 89.76.4 5 76.4 + 5 76.4.42 5 4.42 5 4.42.78 5 42.78 5 42.78.29 5 78.29 + 5 78.29.63 5 29.63 5 29.63.87 5 63.87 5 63.87.80 5 87.80 + 5 87.80.72 5 80.72 5 80.72.9 5 72.9 5 72.9.73 5 9.73 5 9.73.65 + 5 73.65 5 73.65.58 5 65.58 5 65.58.98 5 58.98 5 58.98.21 + 5 98.21 5 98.21.65 5 21.65 5 21.65.5 5 65.5 5 65.5.11 5 5.11 + 5 5.11.87 5 11.87 5 11.87.12 5 87.12 5 87.12.20 5 12.20 + 5 12.20.31 5 20.31 5 20.31.95 5 31.95 5 31.95.73 5 95.73 + 5 95.73.88 5 73.88 5 73.88.8 5 88.8 5 88.8.49 5 8.49 5 8.49.90 + 5 49.90 5 49.90.96 5 90.96 5 90.96.55 5 96.55 5 96.55.77 + 5 55.77 5 55.77.2 5 77.2 5 77.2.85 5 2.85 5 2.85.74 5 85.74 + 5 85.74.70 5 74.70 5 74.70.19 5 70.19 5 70.19.26 5 19.26 + 5 19.26.47 5 26.47 5 26.47.90 5 47.90 5 47.90.58 5 90.58 + 5 90.58.9 5 58.9 5 58.9.72 5 9.72 5 9.72.33 5 72.33 5 72.33.75 + 5 33.75 5 33.75.81 5 75.81 5 75.81.23 5 81.23 5 81.23.13 + 5 23.13 5 23.13.14 5 13.14 5 13.14.91 5 14.91 5 14.91.91 + 5 91.91 5 91.91.15 5 91.15 5 91.15.36 5 15.36 5 15.36.3 5 36.3 + 5 36.3.69 5 3.69 5 3.69.52 5 69.52 5 69.52.50 5 52.50 + 5 52.50.10 5 50.10 5 50.10.33 5 10.33 5 10.33.39 5 33.39 + 5 33.39.58 5 39.58 5 39.58.38 5 58.38 5 58.38.83 5 38.83 + 5 38.83.82 5 83.82 5 83.82.7 4 82.7 3 82.7 2 7 1 7} + +do_execsql_test 1.12.15.2 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE 0=1) OVER win FROM t2 + WINDOW win AS (ORDER BY a ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING) +} {5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 4 {} 3 {} 2 {} 1 {}} + +do_execsql_test 1.12.15.3 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE 1=0) OVER win FROM t2 + WINDOW win AS (PARTITION BY (a%10) ORDER BY a ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING) +} {5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 4 {} 3 {} 2 {} 1 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 4 {} 3 {} 2 {} 1 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 4 {} + 3 {} 2 {} 1 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 4 {} 3 {} 2 {} 1 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 4 {} 3 {} 2 {} 1 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 4 {} 3 {} 2 {} 1 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 4 {} 3 {} + 2 {} 1 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 4 {} 3 {} 2 {} 1 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 4 {} 3 {} 2 {} 1 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 4 {} 3 {} 2 {} 1 {}} + +do_execsql_test 1.12.15.4 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE a%2=0) OVER win FROM t2 + WINDOW win AS (PARTITION BY (a%10) ORDER BY a ROWS BETWEEN CURRENT ROW AND 4 FOLLOWING) +} {5 89.6.29.47.59 5 6.29.47.59.28 5 29.47.59.28.75 5 47.59.28.75.78 + 5 59.28.75.78.72 5 28.75.78.72.98 5 75.78.72.98.87 5 78.72.98.87.73 + 5 72.98.87.73.96 5 98.87.73.96.74 5 87.73.96.74.90 5 73.96.74.90.75 + 5 96.74.90.75.91 5 74.90.75.91.69 5 90.75.91.69.39 5 75.91.69.39.7 + 4 91.69.39.7 3 69.39.7 2 39.7 1 7 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 4 {} + 3 {} 2 {} 1 {} 5 96.97.84.86.32 5 97.84.86.32.25 5 84.86.32.25.89 + 5 86.32.25.89.29 5 32.25.89.29.9 5 25.89.29.9.21 5 89.29.9.21.12 + 5 29.9.21.12.88 5 9.21.12.88.55 5 21.12.88.55.70 5 12.88.55.70.58 + 5 88.55.70.58.81 5 55.70.58.81.91 5 70.58.81.91.52 5 58.81.91.52.58 + 4 81.91.52.58 3 91.52.58 2 52.58 1 58 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 4 {} 3 {} 2 {} 1 {} 5 38.46.23.61.3 5 46.23.61.3.1 + 5 23.61.3.1.76 5 61.3.1.76.63 5 3.1.76.63.73 5 1.76.63.73.65 + 5 76.63.73.65.20 5 63.73.65.20.8 5 73.65.20.8.77 5 65.20.8.77.19 + 5 20.8.77.19.9 5 8.77.19.9.23 5 77.19.9.23.15 5 19.9.23.15.50 + 5 9.23.15.50.38 4 23.15.50.38 3 15.50.38 2 50.38 1 38 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 4 {} 3 {} 2 {} 1 {} 5 39.54.16.85.22 + 5 54.16.85.22.40 5 16.85.22.40.4 5 85.22.40.4.87 5 22.40.4.87.65 + 5 40.4.87.65.5 5 4.87.65.5.31 5 87.65.5.31.49 5 65.5.31.49.2 + 5 5.31.49.2.26 5 31.49.2.26.72 5 49.2.26.72.13 5 2.26.72.13.36 + 5 26.72.13.36.10 5 72.13.36.10.83 4 13.36.10.83 3 36.10.83 2 10.83 + 1 83 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 4 {} 3 {} 2 {} 1 {} + 5 91.8.65.85.55 5 8.65.85.55.56 5 65.85.55.56.42 5 85.55.56.42.80 + 5 55.56.42.80.58 5 56.42.80.58.11 5 42.80.58.11.95 5 80.58.11.95.90 + 5 58.11.95.90.85 5 11.95.90.85.47 5 95.90.85.47.33 5 90.85.47.33.14 + 5 85.47.33.14.3 5 47.33.14.3.33 5 33.14.3.33.82 4 14.3.33.82 + 3 3.33.82 2 33.82 1 82 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} + 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 5 {} 4 {} 3 {} 2 {} + 1 {}} + +do_execsql_test 1.13.2.1 { + SELECT max(b) OVER ( ORDER BY a ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2 +} {96 68 68 68 91 91 91 99 99 99 97 78 78 97 97 + 97 67 93 93 93 84 77 23 93 93 93 65 47 86 86 + 86 91 91 91 85 85 85 59 59 56 56 91 91 91 90 + 90 55 89 89 89 47 56 56 56 56 56 75 75 89 98 + 98 98 81 94 94 94 78 78 78 53 63 63 87 87 87 + 84 84 84 72 61 73 95 95 95 65 96 98 98 98 74 + 74 74 65 73 73 73 87 87 87 41 20 31 31 31 95 + 95 95 79 88 88 88 34 49 49 90 90 96 96 96 75 + 77 77 77 44 85 85 85 74 74 70 70 59 39 39 47 + 80 90 90 90 58 58 72 72 72 72 93 93 93 81 81 + 81 37 37 37 14 62 91 91 91 91 91 34 36 99 99 + 99 95 95 69 58 52 84 84 84 84 84 39 44 58 58 + 58 38 83 83 83 82 82 17 7 {} {}} + +do_execsql_test 1.13.2.2 { + SELECT min(b) OVER ( ORDER BY a ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2 +} {38 38 38 39 39 46 6 6 6 27 27 27 46 54 8 8 + 8 29 29 77 23 16 16 16 16 35 35 7 7 7 61 61 + 61 24 24 24 43 12 12 12 3 3 3 22 22 15 15 15 + 25 25 1 1 1 40 40 16 16 16 36 36 76 76 4 4 + 4 30 30 30 29 29 29 2 2 2 37 37 72 41 9 9 9 + 61 65 13 13 13 58 1 1 1 21 35 5 5 5 11 11 + 41 12 8 8 8 20 15 15 15 22 22 73 34 8 8 8 + 11 34 34 59 59 55 55 55 44 2 2 2 7 57 29 29 + 29 19 19 19 26 26 26 47 36 36 36 9 9 9 66 33 + 33 33 64 64 9 9 9 13 12 12 12 14 36 36 33 15 + 15 15 34 3 3 3 58 52 30 30 30 10 10 10 21 21 + 21 39 30 30 30 34 27 27 17 7 7 7 {} {}} + +do_execsql_test 1.13.3.1 { + SELECT row_number() OVER ( ORDER BY a ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.13.3.2 { + SELECT row_number() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.13.3.3 { + SELECT row_number() OVER ( ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.13.4.1 { + SELECT dense_rank() OVER ( ORDER BY a ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.13.4.2 { + SELECT dense_rank() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.13.4.3 { + SELECT dense_rank() OVER ( ORDER BY b ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2 +} {1 1 2 2 3 3 4 5 6 7 7 7 8 8 8 9 9 9 10 + 11 11 12 12 12 13 13 14 15 15 15 16 16 16 17 + 18 19 20 20 21 21 22 22 23 24 25 25 26 26 27 + 28 28 28 29 29 29 30 30 31 32 32 32 32 33 33 + 33 33 34 34 35 35 35 35 36 36 37 37 38 38 38 + 39 40 40 41 42 42 43 43 44 44 45 45 45 45 46 + 47 48 49 50 51 52 52 53 53 53 54 55 55 55 55 + 56 56 56 56 57 58 58 59 59 60 61 62 62 62 63 + 64 65 66 67 68 68 68 69 69 69 70 70 70 71 71 + 71 72 73 73 74 74 75 76 76 77 77 77 78 79 80 + 80 80 80 81 81 81 82 83 83 84 85 85 85 86 86 + 86 87 87 87 87 87 88 88 88 89 90 90 90 91 91 + 91 92 92 93 93 94 94} + +do_execsql_test 1.13.4.4 { + SELECT dense_rank() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2 +} {1 2 3 3 3 4 5 6 7 8 8 9 9 9 1 1 2 2 3 + 3 4 4 5 5 6 7 7 8 8 8 9 9 9 9 9 1 1 2 + 2 2 3 3 4 5 6 7 7 8 8 8 9 1 1 2 2 3 3 + 4 4 4 4 5 5 6 7 8 8 8 9 10 10 10 1 2 3 + 4 4 4 4 5 5 6 7 8 8 8 9 9 9 9 10 1 2 2 + 2 3 4 4 5 5 6 6 6 7 7 7 8 8 8 9 9 9 1 + 2 2 2 3 3 4 4 4 4 5 5 6 6 6 7 8 9 10 10 + 10 1 1 1 2 3 3 4 4 5 5 5 5 6 7 8 8 9 9 + 10 10 1 1 1 2 3 3 4 4 4 4 5 6 6 7 8 8 1 + 1 1 2 3 3 3 4 4 4 5 6 6 6 6 7 8 9 9 9 + 10 10} + +do_execsql_test 1.13.4.5 { + SELECT dense_rank() OVER ( ORDER BY b%10 ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 + 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 + 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 + 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 + 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 + 8 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 10 10 + 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 + 10 10 10 10 10} + +do_execsql_test 1.13.4.6 { + SELECT dense_rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 + 5 5 5 5 5 5 5 5 5 5 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 + 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 + 5} + +do_execsql_test 1.13.5.1 { + SELECT rank() OVER ( ORDER BY a ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.13.5.2 { + SELECT rank() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.13.5.3 { + SELECT rank() OVER ( ORDER BY b ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2 +} {1 1 3 3 5 5 7 8 9 10 10 10 13 13 13 16 16 + 16 19 20 20 22 22 22 25 25 27 28 28 28 31 31 + 31 34 35 36 37 37 39 39 41 41 43 44 45 45 47 + 47 49 50 50 50 53 53 53 56 56 58 59 59 59 59 + 63 63 63 63 67 67 69 69 69 69 73 73 75 75 77 + 77 77 80 81 81 83 84 84 86 86 88 88 90 90 90 + 90 94 95 96 97 98 99 100 100 102 102 102 105 106 + 106 106 106 110 110 110 110 114 115 115 117 117 119 + 120 121 121 121 124 125 126 127 128 129 129 129 132 + 132 132 135 135 135 138 138 138 141 142 142 144 144 + 146 147 147 149 149 149 152 153 154 154 154 154 158 + 158 158 161 162 162 164 165 165 165 168 168 168 171 + 171 171 171 171 176 176 176 179 180 180 180 183 183 + 183 186 186 188 188 190 190} + +do_execsql_test 1.13.5.4 { + SELECT rank() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2 +} {1 2 3 3 3 6 7 8 9 10 10 12 12 12 1 1 3 3 + 5 5 7 7 9 9 11 12 12 14 14 14 17 17 17 17 + 17 1 1 3 3 3 6 6 8 9 10 11 11 13 13 13 16 + 1 1 3 3 5 5 7 7 7 7 11 11 13 14 15 15 15 + 18 19 19 19 1 2 3 4 4 4 4 8 8 10 11 12 12 + 12 15 15 15 15 19 1 2 2 2 5 6 6 8 8 10 10 + 10 13 13 13 16 16 16 19 19 19 1 2 2 2 5 5 7 + 7 7 7 11 11 13 13 13 16 17 18 19 19 19 1 1 + 1 4 5 5 7 7 9 9 9 9 13 14 15 15 17 17 19 + 19 1 1 1 4 5 5 7 7 7 7 11 12 12 14 15 15 + 1 1 1 4 5 5 5 8 8 8 11 12 12 12 12 16 17 + 18 18 18 21 21} + +do_execsql_test 1.13.5.5 { + SELECT rank() OVER ( ORDER BY b%10 ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 15 15 15 15 + 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 + 15 15 36 36 36 36 36 36 36 36 36 36 36 36 36 + 36 36 36 52 52 52 52 52 52 52 52 52 52 52 52 + 52 52 52 52 52 52 52 52 52 73 73 73 73 73 73 + 73 73 73 73 73 73 73 73 73 73 73 73 73 92 92 + 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 + 92 92 92 92 113 113 113 113 113 113 113 113 113 + 113 113 113 113 113 113 113 113 113 113 113 113 134 + 134 134 134 134 134 134 134 134 134 134 134 134 134 + 134 134 134 134 134 134 154 154 154 154 154 154 154 + 154 154 154 154 154 154 154 154 154 170 170 170 170 + 170 170 170 170 170 170 170 170 170 170 170 170 170 + 170 170 170 170 170} + +do_execsql_test 1.13.5.6 { + SELECT rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 15 15 15 15 + 15 15 15 15 15 15 15 15 15 15 15 15 31 31 31 + 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 + 31 50 50 50 50 50 50 50 50 50 50 50 50 50 50 + 50 50 50 50 50 50 50 71 71 71 71 71 71 71 71 + 71 71 71 71 71 71 71 71 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 22 22 22 22 22 22 + 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 + 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 + 43 43 43 43 43 43 64 64 64 64 64 64 64 64 64 + 64 64 64 64 64 64 64 64 64 64 64 84 84 84 84 + 84 84 84 84 84 84 84 84 84 84 84 84 84 84 84 + 84 84 84} + +do_execsql_test 1.13.6.1 { + SELECT + row_number() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ), + rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ), + dense_rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) + FROM t2 +} {1 1 1 2 1 1 3 1 1 4 1 1 5 1 1 6 1 1 7 1 1 8 1 1 9 1 1 + 10 1 1 11 1 1 12 1 1 13 1 1 14 1 1 15 15 2 16 15 2 17 15 2 + 18 15 2 19 15 2 20 15 2 21 15 2 22 15 2 23 15 2 24 15 2 + 25 15 2 26 15 2 27 15 2 28 15 2 29 15 2 30 15 2 31 31 3 + 32 31 3 33 31 3 34 31 3 35 31 3 36 31 3 37 31 3 38 31 3 + 39 31 3 40 31 3 41 31 3 42 31 3 43 31 3 44 31 3 45 31 3 + 46 31 3 47 31 3 48 31 3 49 31 3 50 50 4 51 50 4 52 50 4 + 53 50 4 54 50 4 55 50 4 56 50 4 57 50 4 58 50 4 59 50 4 + 60 50 4 61 50 4 62 50 4 63 50 4 64 50 4 65 50 4 66 50 4 + 67 50 4 68 50 4 69 50 4 70 50 4 71 71 5 72 71 5 73 71 5 + 74 71 5 75 71 5 76 71 5 77 71 5 78 71 5 79 71 5 80 71 5 + 81 71 5 82 71 5 83 71 5 84 71 5 85 71 5 86 71 5 1 1 1 2 1 1 + 3 1 1 4 1 1 5 1 1 6 1 1 7 1 1 8 1 1 9 1 1 10 1 1 11 1 1 + 12 1 1 13 1 1 14 1 1 15 1 1 16 1 1 17 1 1 18 1 1 19 1 1 + 20 1 1 21 1 1 22 22 2 23 22 2 24 22 2 25 22 2 26 22 2 27 22 2 + 28 22 2 29 22 2 30 22 2 31 22 2 32 22 2 33 22 2 34 22 2 + 35 22 2 36 22 2 37 22 2 38 22 2 39 22 2 40 22 2 41 22 2 + 42 22 2 43 43 3 44 43 3 45 43 3 46 43 3 47 43 3 48 43 3 + 49 43 3 50 43 3 51 43 3 52 43 3 53 43 3 54 43 3 55 43 3 + 56 43 3 57 43 3 58 43 3 59 43 3 60 43 3 61 43 3 62 43 3 + 63 43 3 64 64 4 65 64 4 66 64 4 67 64 4 68 64 4 69 64 4 + 70 64 4 71 64 4 72 64 4 73 64 4 74 64 4 75 64 4 76 64 4 + 77 64 4 78 64 4 79 64 4 80 64 4 81 64 4 82 64 4 83 64 4 + 84 84 5 85 84 5 86 84 5 87 84 5 88 84 5 89 84 5 90 84 5 + 91 84 5 92 84 5 93 84 5 94 84 5 95 84 5 96 84 5 97 84 5 + 98 84 5 99 84 5 100 84 5 101 84 5 102 84 5 103 84 5 104 84 5 + 105 84 5} + + +do_test 1.13.7.1 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY a ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0053 0.0105 0.0158 0.0211 0.0263 0.0316 0.0368 0.0421 0.0474 0.0526 0.0579 0.0632 0.0684 0.0737 0.0789 0.0842 0.0895 0.0947 0.1000 0.1053 0.1105 0.1158 0.1211 0.1263 0.1316 0.1368 0.1421 0.1474 0.1526 0.1579 0.1632 0.1684 0.1737 0.1789 0.1842 0.1895 0.1947 0.2000 0.2053 0.2105 0.2158 0.2211 0.2263 0.2316 0.2368 0.2421 0.2474 0.2526 0.2579 0.2632 0.2684 0.2737 0.2789 0.2842 0.2895 0.2947 0.3000 0.3053 0.3105 0.3158 0.3211 0.3263 0.3316 0.3368 0.3421 0.3474 0.3526 0.3579 0.3632 0.3684 0.3737 0.3789 0.3842 0.3895 0.3947 0.4000 0.4053 0.4105 0.4158 0.4211 0.4263 0.4316 0.4368 0.4421 0.4474 0.4526 0.4579 0.4632 0.4684 0.4737 0.4789 0.4842 0.4895 0.4947 0.5000 0.5053 0.5105 0.5158 0.5211 0.5263 0.5316 0.5368 0.5421 0.5474 0.5526 0.5579 0.5632 0.5684 0.5737 0.5789 0.5842 0.5895 0.5947 0.6000 0.6053 0.6105 0.6158 0.6211 0.6263 0.6316 0.6368 0.6421 0.6474 0.6526 0.6579 0.6632 0.6684 0.6737 0.6789 0.6842 0.6895 0.6947 0.7000 0.7053 0.7105 0.7158 0.7211 0.7263 0.7316 0.7368 0.7421 0.7474 0.7526 0.7579 0.7632 0.7684 0.7737 0.7789 0.7842 0.7895 0.7947 0.8000 0.8053 0.8105 0.8158 0.8211 0.8263 0.8316 0.8368 0.8421 0.8474 0.8526 0.8579 0.8632 0.8684 0.8737 0.8789 0.8842 0.8895 0.8947 0.9000 0.9053 0.9105 0.9158 0.9211 0.9263 0.9316 0.9368 0.9421 0.9474 0.9526 0.9579 0.9632 0.9684 0.9737 0.9789 0.9842 0.9895 0.9947 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.13.7.2 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0769 0.1538 0.2308 0.3077 0.3846 0.4615 0.5385 0.6154 0.6923 0.7692 0.8462 0.9231 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0667 0.1333 0.2000 0.2667 0.3333 0.4000 0.4667 0.5333 0.6000 0.6667 0.7333 0.8000 0.8667 0.9333 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0556 0.1111 0.1667 0.2222 0.2778 0.3333 0.3889 0.4444 0.5000 0.5556 0.6111 0.6667 0.7222 0.7778 0.8333 0.8889 0.9444 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0526 0.1053 0.1579 0.2105 0.2632 0.3158 0.3684 0.4211 0.4737 0.5263 0.5789 0.6316 0.6842 0.7368 0.7895 0.8421 0.8947 0.9474 1.0000 0.0000 0.0667 0.1333 0.2000 0.2667 0.3333 0.4000 0.4667 0.5333 0.6000 0.6667 0.7333 0.8000 0.8667 0.9333 1.0000 0.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.13.7.3 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY b ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0105 0.0105 0.0211 0.0211 0.0316 0.0368 0.0421 0.0474 0.0474 0.0474 0.0632 0.0632 0.0632 0.0789 0.0789 0.0789 0.0947 0.1000 0.1000 0.1105 0.1105 0.1105 0.1263 0.1263 0.1368 0.1421 0.1421 0.1421 0.1579 0.1579 0.1579 0.1737 0.1789 0.1842 0.1895 0.1895 0.2000 0.2000 0.2105 0.2105 0.2211 0.2263 0.2316 0.2316 0.2421 0.2421 0.2526 0.2579 0.2579 0.2579 0.2737 0.2737 0.2737 0.2895 0.2895 0.3000 0.3053 0.3053 0.3053 0.3053 0.3263 0.3263 0.3263 0.3263 0.3474 0.3474 0.3579 0.3579 0.3579 0.3579 0.3789 0.3789 0.3895 0.3895 0.4000 0.4000 0.4000 0.4158 0.4211 0.4211 0.4316 0.4368 0.4368 0.4474 0.4474 0.4579 0.4579 0.4684 0.4684 0.4684 0.4684 0.4895 0.4947 0.5000 0.5053 0.5105 0.5158 0.5211 0.5211 0.5316 0.5316 0.5316 0.5474 0.5526 0.5526 0.5526 0.5526 0.5737 0.5737 0.5737 0.5737 0.5947 0.6000 0.6000 0.6105 0.6105 0.6211 0.6263 0.6316 0.6316 0.6316 0.6474 0.6526 0.6579 0.6632 0.6684 0.6737 0.6737 0.6737 0.6895 0.6895 0.6895 0.7053 0.7053 0.7053 0.7211 0.7211 0.7211 0.7368 0.7421 0.7421 0.7526 0.7526 0.7632 0.7684 0.7684 0.7789 0.7789 0.7789 0.7947 0.8000 0.8053 0.8053 0.8053 0.8053 0.8263 0.8263 0.8263 0.8421 0.8474 0.8474 0.8579 0.8632 0.8632 0.8632 0.8789 0.8789 0.8789 0.8947 0.8947 0.8947 0.8947 0.8947 0.9211 0.9211 0.9211 0.9368 0.9421 0.9421 0.9421 0.9579 0.9579 0.9579 0.9737 0.9737 0.9842 0.9842 0.9947 0.9947} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.13.7.4 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0769 0.1538 0.1538 0.1538 0.3846 0.4615 0.5385 0.6154 0.6923 0.6923 0.8462 0.8462 0.8462 0.0000 0.0000 0.1000 0.1000 0.2000 0.2000 0.3000 0.3000 0.4000 0.4000 0.5000 0.5500 0.5500 0.6500 0.6500 0.6500 0.8000 0.8000 0.8000 0.8000 0.8000 0.0000 0.0000 0.1333 0.1333 0.1333 0.3333 0.3333 0.4667 0.5333 0.6000 0.6667 0.6667 0.8000 0.8000 0.8000 1.0000 0.0000 0.0000 0.1000 0.1000 0.2000 0.2000 0.3000 0.3000 0.3000 0.3000 0.5000 0.5000 0.6000 0.6500 0.7000 0.7000 0.7000 0.8500 0.9000 0.9000 0.9000 0.0000 0.0556 0.1111 0.1667 0.1667 0.1667 0.1667 0.3889 0.3889 0.5000 0.5556 0.6111 0.6111 0.6111 0.7778 0.7778 0.7778 0.7778 1.0000 0.0000 0.0500 0.0500 0.0500 0.2000 0.2500 0.2500 0.3500 0.3500 0.4500 0.4500 0.4500 0.6000 0.6000 0.6000 0.7500 0.7500 0.7500 0.9000 0.9000 0.9000 0.0000 0.0500 0.0500 0.0500 0.2000 0.2000 0.3000 0.3000 0.3000 0.3000 0.5000 0.5000 0.6000 0.6000 0.6000 0.7500 0.8000 0.8500 0.9000 0.9000 0.9000 0.0000 0.0000 0.0000 0.1579 0.2105 0.2105 0.3158 0.3158 0.4211 0.4211 0.4211 0.4211 0.6316 0.6842 0.7368 0.7368 0.8421 0.8421 0.9474 0.9474 0.0000 0.0000 0.0000 0.2000 0.2667 0.2667 0.4000 0.4000 0.4000 0.4000 0.6667 0.7333 0.7333 0.8667 0.9333 0.9333 0.0000 0.0000 0.0000 0.1429 0.1905 0.1905 0.1905 0.3333 0.3333 0.3333 0.4762 0.5238 0.5238 0.5238 0.5238 0.7143 0.7619 0.8095 0.8095 0.8095 0.9524 0.9524} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.13.7.5 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY b%10 ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.13.7.6 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER (PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.13.8.1 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY a ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0052 0.0105 0.0157 0.0209 0.0262 0.0314 0.0366 0.0419 0.0471 0.0524 0.0576 0.0628 0.0681 0.0733 0.0785 0.0838 0.0890 0.0942 0.0995 0.1047 0.1099 0.1152 0.1204 0.1257 0.1309 0.1361 0.1414 0.1466 0.1518 0.1571 0.1623 0.1675 0.1728 0.1780 0.1832 0.1885 0.1937 0.1990 0.2042 0.2094 0.2147 0.2199 0.2251 0.2304 0.2356 0.2408 0.2461 0.2513 0.2565 0.2618 0.2670 0.2723 0.2775 0.2827 0.2880 0.2932 0.2984 0.3037 0.3089 0.3141 0.3194 0.3246 0.3298 0.3351 0.3403 0.3455 0.3508 0.3560 0.3613 0.3665 0.3717 0.3770 0.3822 0.3874 0.3927 0.3979 0.4031 0.4084 0.4136 0.4188 0.4241 0.4293 0.4346 0.4398 0.4450 0.4503 0.4555 0.4607 0.4660 0.4712 0.4764 0.4817 0.4869 0.4921 0.4974 0.5026 0.5079 0.5131 0.5183 0.5236 0.5288 0.5340 0.5393 0.5445 0.5497 0.5550 0.5602 0.5654 0.5707 0.5759 0.5812 0.5864 0.5916 0.5969 0.6021 0.6073 0.6126 0.6178 0.6230 0.6283 0.6335 0.6387 0.6440 0.6492 0.6545 0.6597 0.6649 0.6702 0.6754 0.6806 0.6859 0.6911 0.6963 0.7016 0.7068 0.7120 0.7173 0.7225 0.7277 0.7330 0.7382 0.7435 0.7487 0.7539 0.7592 0.7644 0.7696 0.7749 0.7801 0.7853 0.7906 0.7958 0.8010 0.8063 0.8115 0.8168 0.8220 0.8272 0.8325 0.8377 0.8429 0.8482 0.8534 0.8586 0.8639 0.8691 0.8743 0.8796 0.8848 0.8901 0.8953 0.9005 0.9058 0.9110 0.9162 0.9215 0.9267 0.9319 0.9372 0.9424 0.9476 0.9529 0.9581 0.9634 0.9686 0.9738 0.9791 0.9843 0.9895 0.9948 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.13.8.2 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0714 0.1429 0.2143 0.2857 0.3571 0.4286 0.5000 0.5714 0.6429 0.7143 0.7857 0.8571 0.9286 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0625 0.1250 0.1875 0.2500 0.3125 0.3750 0.4375 0.5000 0.5625 0.6250 0.6875 0.7500 0.8125 0.8750 0.9375 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0526 0.1053 0.1579 0.2105 0.2632 0.3158 0.3684 0.4211 0.4737 0.5263 0.5789 0.6316 0.6842 0.7368 0.7895 0.8421 0.8947 0.9474 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0625 0.1250 0.1875 0.2500 0.3125 0.3750 0.4375 0.5000 0.5625 0.6250 0.6875 0.7500 0.8125 0.8750 0.9375 1.0000 0.0455 0.0909 0.1364 0.1818 0.2273 0.2727 0.3182 0.3636 0.4091 0.4545 0.5000 0.5455 0.5909 0.6364 0.6818 0.7273 0.7727 0.8182 0.8636 0.9091 0.9545 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.13.8.3 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY b ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0105 0.0105 0.0209 0.0209 0.0314 0.0314 0.0366 0.0419 0.0471 0.0628 0.0628 0.0628 0.0785 0.0785 0.0785 0.0942 0.0942 0.0942 0.0995 0.1099 0.1099 0.1257 0.1257 0.1257 0.1361 0.1361 0.1414 0.1571 0.1571 0.1571 0.1728 0.1728 0.1728 0.1780 0.1832 0.1885 0.1990 0.1990 0.2094 0.2094 0.2199 0.2199 0.2251 0.2304 0.2408 0.2408 0.2513 0.2513 0.2565 0.2723 0.2723 0.2723 0.2880 0.2880 0.2880 0.2984 0.2984 0.3037 0.3246 0.3246 0.3246 0.3246 0.3455 0.3455 0.3455 0.3455 0.3560 0.3560 0.3770 0.3770 0.3770 0.3770 0.3874 0.3874 0.3979 0.3979 0.4136 0.4136 0.4136 0.4188 0.4293 0.4293 0.4346 0.4450 0.4450 0.4555 0.4555 0.4660 0.4660 0.4869 0.4869 0.4869 0.4869 0.4921 0.4974 0.5026 0.5079 0.5131 0.5183 0.5288 0.5288 0.5445 0.5445 0.5445 0.5497 0.5707 0.5707 0.5707 0.5707 0.5916 0.5916 0.5916 0.5916 0.5969 0.6073 0.6073 0.6178 0.6178 0.6230 0.6283 0.6440 0.6440 0.6440 0.6492 0.6545 0.6597 0.6649 0.6702 0.6859 0.6859 0.6859 0.7016 0.7016 0.7016 0.7173 0.7173 0.7173 0.7330 0.7330 0.7330 0.7382 0.7487 0.7487 0.7592 0.7592 0.7644 0.7749 0.7749 0.7906 0.7906 0.7906 0.7958 0.8010 0.8220 0.8220 0.8220 0.8220 0.8377 0.8377 0.8377 0.8429 0.8534 0.8534 0.8586 0.8743 0.8743 0.8743 0.8901 0.8901 0.8901 0.9162 0.9162 0.9162 0.9162 0.9162 0.9319 0.9319 0.9319 0.9372 0.9529 0.9529 0.9529 0.9686 0.9686 0.9686 0.9791 0.9791 0.9895 0.9895 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.13.8.4 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0714 0.1429 0.3571 0.3571 0.3571 0.4286 0.5000 0.5714 0.6429 0.7857 0.7857 1.0000 1.0000 1.0000 0.0952 0.0952 0.1905 0.1905 0.2857 0.2857 0.3810 0.3810 0.4762 0.4762 0.5238 0.6190 0.6190 0.7619 0.7619 0.7619 1.0000 1.0000 1.0000 1.0000 1.0000 0.1250 0.1250 0.3125 0.3125 0.3125 0.4375 0.4375 0.5000 0.5625 0.6250 0.7500 0.7500 0.9375 0.9375 0.9375 1.0000 0.0952 0.0952 0.1905 0.1905 0.2857 0.2857 0.4762 0.4762 0.4762 0.4762 0.5714 0.5714 0.6190 0.6667 0.8095 0.8095 0.8095 0.8571 1.0000 1.0000 1.0000 0.0526 0.1053 0.1579 0.3684 0.3684 0.3684 0.3684 0.4737 0.4737 0.5263 0.5789 0.7368 0.7368 0.7368 0.9474 0.9474 0.9474 0.9474 1.0000 0.0476 0.1905 0.1905 0.1905 0.2381 0.3333 0.3333 0.4286 0.4286 0.5714 0.5714 0.5714 0.7143 0.7143 0.7143 0.8571 0.8571 0.8571 1.0000 1.0000 1.0000 0.0476 0.1905 0.1905 0.1905 0.2857 0.2857 0.4762 0.4762 0.4762 0.4762 0.5714 0.5714 0.7143 0.7143 0.7143 0.7619 0.8095 0.8571 1.0000 1.0000 1.0000 0.1500 0.1500 0.1500 0.2000 0.3000 0.3000 0.4000 0.4000 0.6000 0.6000 0.6000 0.6000 0.6500 0.7000 0.8000 0.8000 0.9000 0.9000 1.0000 1.0000 0.1875 0.1875 0.1875 0.2500 0.3750 0.3750 0.6250 0.6250 0.6250 0.6250 0.6875 0.8125 0.8125 0.8750 1.0000 1.0000 0.1364 0.1364 0.1364 0.1818 0.3182 0.3182 0.3182 0.4545 0.4545 0.4545 0.5000 0.6818 0.6818 0.6818 0.6818 0.7273 0.7727 0.9091 0.9091 0.9091 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.13.8.5 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY b%10 ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.13.8.6 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.13.8.1 { + set myres {} + foreach r [db eval {SELECT ntile(100) OVER ( ORDER BY a ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 88.0000 89.0000 89.0000 90.0000 90.0000 91.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.13.8.2 { + set myres {} + foreach r [db eval {SELECT ntile(101) OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 22.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.13.8.3 { + set myres {} + foreach r [db eval {SELECT ntile(102) OVER ( ORDER BY b,a ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 88.0000 89.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.13.8.4 { + set myres {} + foreach r [db eval {SELECT ntile(103) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 22.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.13.8.5 { + set myres {} + foreach r [db eval {SELECT ntile(104) OVER ( ORDER BY b%10,a ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000 103.0000 104.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.13.8.6 { + set myres {} + foreach r [db eval {SELECT ntile(105) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.13.8.7 { + set myres {} + foreach r [db eval {SELECT ntile(105) OVER ( ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 88.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000 103.0000 104.0000 105.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + +do_execsql_test 1.13.9.1 { + SELECT last_value(a+b) OVER ( ORDER BY a ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2 +} {52 83 55 79 109 65 26 120 119 50 70 103 80 124 + 36 96 59 124 116 110 57 51 52 130 103 74 87 48 + 128 117 105 136 131 71 133 92 109 63 84 109 57 + 146 78 147 113 74 88 150 87 110 65 121 106 110 + 124 85 145 107 161 171 150 156 80 171 120 109 158 + 114 111 136 147 87 173 124 168 173 162 132 101 154 + 167 190 161 110 156 195 198 102 123 177 169 140 111 + 180 119 160 197 152 124 121 134 146 147 132 213 141 + 193 200 210 157 132 136 175 161 218 188 226 191 187 + 208 211 179 138 144 223 196 214 170 212 202 163 184 + 172 173 195 229 240 187 210 200 163 227 228 223 191 + 252 235 225 243 172 187 202 179 179 182 231 261 207 + 263 206 189 209 212 276 181 274 249 239 234 213 234 + 269 196 271 221 210 229 235 250 223 232 229 279 224 + 280 216 207 207 207 {} {}} + +do_execsql_test 1.13.9.2 { + SELECT last_value(a+b) OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2 +} {134 218 191 212 229 240 213 234 196 223 223 223 {} + {} 146 65 156 132 154 102 123 119 160 152 146 147 + 136 243 261 263 210 210 210 {} {} 120 87 162 124 + 141 138 227 228 179 231 234 280 280 280 {} {} 57 + 110 114 136 147 167 110 180 193 191 252 187 179 206 + 181 221 279 279 279 {} {} 80 171 173 177 157 161 + 179 214 225 182 209 269 271 235 229 229 229 {} {} + 113 74 87 145 190 161 169 140 111 132 213 187 208 + 223 235 189 274 274 274 {} {} 51 52 128 109 121 + 124 85 107 150 195 226 172 173 187 223 207 212 212 + 212 {} {} 110 87 48 110 173 124 197 211 144 196 + 195 200 202 224 216 207 207 207 {} {} 88 171 158 + 156 198 121 210 132 210 239 250 232 232 232 {} {} + 59 109 150 161 111 101 200 175 188 170 202 163 184 + 163 172 276 249 229 229 229 {} {}} + +do_execsql_test 1.13.9.3 { + SELECT last_value(a+b) OVER ( ORDER BY b,a ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2 +} {57 181 80 111 26 48 144 207 36 121 132 101 163 + 172 196 119 136 63 124 179 110 179 182 74 132 189 + 51 52 85 216 163 134 123 210 78 141 57 187 71 87 + 172 173 50 224 88 59 111 170 109 213 223 146 147 + 84 114 191 206 221 157 161 209 229 74 140 107 187 + 207 212 124 202 52 232 55 184 229 106 132 152 120 + 92 110 179 235 65 70 87 110 195 200 175 234 160 + 234 136 80 113 187 109 121 124 196 156 210 239 250 + 72 109 188 202 191 105 154 79 231 147 225 103 161 + 169 223 96 83 249 212 162 227 228 167 180 193 117 + 177 214 145 208 235 150 110 211 103 158 200 168 229 + 92 156 243 280 279 116 173 269 271 131 133 223 128 + 173 197 210 99 150 161 147 218 240 109 136 146 261 + 263 124 130 252 171 190 213 274 108 195 226 119 124 + 171 198 120 276 276 276 {} {}} + +do_execsql_test 1.13.9.4 { + SELECT last_value(a+b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2 +} {223 106 234 191 212 168 229 147 218 240 240 240 {} + {} 123 210 146 147 132 152 160 105 154 92 156 243 + 109 136 146 261 263 263 263 {} {} 179 78 141 84 + 120 234 79 231 162 227 228 280 280 280 {} {} 57 + 187 114 191 206 221 92 110 136 147 167 180 193 279 + 124 130 252 252 252 {} {} 161 209 229 179 235 80 + 225 117 177 214 116 173 269 271 171 171 171 {} {} + 87 74 140 113 187 103 161 169 145 208 235 131 133 + 223 190 213 274 274 274 {} {} 172 173 107 187 207 + 212 65 70 109 121 124 223 150 128 108 195 226 226 + 226 {} {} 50 224 124 202 87 110 195 200 196 96 + 110 211 173 197 119 124 124 124 {} {} 52 232 156 + 210 239 250 83 103 158 210 171 198 198 198 {} {} + 59 111 170 55 184 229 175 72 109 188 202 249 200 + 99 150 161 120 276 276 276 {} {}} + +do_execsql_test 1.13.9.5 { + SELECT last_value(a+b) OVER ( ORDER BY b%10,a ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2 +} {134 218 191 212 229 240 213 234 196 223 92 109 105 + 136 146 65 156 132 154 102 123 119 160 152 146 147 + 136 243 261 263 210 79 63 84 78 120 87 162 124 + 141 138 227 228 179 231 234 280 124 57 130 92 57 + 110 114 136 147 167 110 180 193 191 252 187 179 206 + 181 221 279 80 116 117 71 80 171 173 177 157 161 + 179 214 225 182 209 269 271 235 229 103 74 131 133 + 113 74 87 145 190 161 169 140 111 132 213 187 208 + 223 235 189 274 108 65 26 70 51 52 128 109 121 + 124 85 107 150 195 226 172 173 187 223 207 212 119 + 50 124 96 110 87 48 110 173 124 197 211 144 196 + 195 200 202 224 216 207 52 83 103 36 88 171 158 + 156 198 121 210 132 210 239 250 232 99 72 55 120 + 59 109 150 161 111 101 200 175 188 170 202 163 184 + 163 172 276 249 229 229 229 {} {}} + +do_execsql_test 1.13.9.6 { + SELECT last_value(a+b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.13.10.1 { + SELECT nth_value(b,b+1) OVER (ORDER BY a ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} 43 {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + 84 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 65 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} 74 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.13.10.2 { + SELECT nth_value(b,b+1) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 61 {} {} {} 51 {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} 2 {} {} {} 62 + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.13.10.3 { + SELECT nth_value(b,b+1) OVER ( ORDER BY b,a ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2 +} {2 3 4 5 {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.13.10.4 { + SELECT nth_value(b,b+1) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} 11 + 21 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} 12 22 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.13.10.5 { + SELECT nth_value(b,b+1) OVER ( ORDER BY b%10,a ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 61 {} {} {} 51 {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} 2 {} {} {} 62 + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.13.10.6 { + SELECT nth_value(b,b+1) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.13.11.1 { + SELECT first_value(b) OVER (ORDER BY a ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING) FROM t2 +} {96 59 38 68 39 62 91 46 6 99 97 27 46 78 54 + 97 8 67 29 93 84 77 23 16 16 93 65 35 47 7 86 + 74 61 91 85 24 85 43 59 12 32 56 3 91 22 90 + 55 15 28 89 25 47 1 56 40 43 56 16 75 36 89 + 98 76 81 4 94 42 30 78 33 29 53 63 2 87 37 80 + 84 72 41 9 61 73 95 65 13 58 96 98 1 21 74 65 + 35 5 73 11 51 87 41 12 8 20 31 31 15 95 22 73 + 79 88 34 8 11 49 34 90 59 96 60 55 75 77 44 2 + 7 85 57 74 29 70 59 19 39 26 26 47 80 90 36 + 58 47 9 72 72 66 33 93 75 64 81 9 23 37 13 12 + 14 62 91 36 91 33 15 34 36 99 3 95 69 58 52 + 30 50 84 10 84 33 21 39 44 58 30 38 34 83 27 + 82 17 7 {} {}} + +do_execsql_test 1.13.11.2 { + SELECT first_value(b) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING) FROM t2 +} {30 80 20 90 60 70 80 90 30 50 10 30 {} {} 61 + 91 91 1 81 41 61 1 21 11 51 41 31 31 11 81 91 + 91 21 {} {} 32 22 42 2 72 12 22 2 72 72 12 62 + 52 82 {} {} 93 43 3 43 33 53 63 73 13 73 73 + 33 93 23 13 33 3 33 83 {} {} 74 24 4 94 84 74 + 34 34 44 74 64 14 34 84 84 44 34 {} {} 85 85 + 55 15 25 75 95 65 65 35 5 15 95 55 75 85 75 + 15 95 {} {} 6 46 16 16 86 56 56 56 16 36 76 + 96 96 26 26 36 66 36 36 {} {} 97 67 77 47 7 + 47 87 37 87 77 7 57 47 47 37 27 17 7 {} {} 78 + 8 28 98 78 58 98 8 88 8 58 58 58 38 {} {} 39 + 99 29 59 89 89 29 9 79 49 59 29 59 19 39 9 9 + 99 69 39 {} {}} + +do_execsql_test 1.13.11.3 { + SELECT first_value(b) OVER ( ORDER BY b,a ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2 +} {2 2 3 3 4 5 6 7 7 7 8 8 8 9 9 9 10 11 + 11 12 12 12 13 13 14 15 15 15 16 16 16 17 19 + 20 21 21 22 22 23 23 24 25 26 26 27 27 28 29 + 29 29 30 30 30 31 31 32 33 33 33 33 34 34 34 + 34 35 35 36 36 36 36 37 37 38 38 39 39 39 40 + 41 41 42 43 43 44 44 46 46 47 47 47 47 49 50 + 51 52 53 54 55 55 56 56 56 57 58 58 58 58 59 + 59 59 59 60 61 61 62 62 63 64 65 65 65 66 67 + 68 69 70 72 72 72 73 73 73 74 74 74 75 75 75 + 76 77 77 78 78 79 80 80 81 81 81 82 83 84 84 + 84 84 85 85 85 86 87 87 88 89 89 89 90 90 90 + 91 91 91 91 91 93 93 93 94 95 95 95 96 96 96 + 97 97 98 98 99 99 {} {}} + +do_execsql_test 1.13.11.4 { + SELECT first_value(b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2 +} {30 30 30 40 50 60 70 80 80 90 90 90 {} {} 11 + 11 21 21 31 31 41 41 51 61 61 81 81 81 91 91 + 91 91 91 {} {} 12 12 12 22 22 32 42 52 62 62 + 72 72 72 82 {} {} 13 13 23 23 33 33 33 33 43 + 43 53 63 73 73 73 83 93 93 93 {} {} 24 34 34 + 34 34 44 44 54 64 74 74 74 84 84 84 84 94 {} + {} 15 15 25 35 35 55 55 65 65 65 75 75 75 85 + 85 85 95 95 95 {} {} 16 16 26 26 36 36 36 36 + 46 46 56 56 56 66 76 86 96 96 96 {} {} 7 17 + 27 27 37 37 47 47 47 47 57 67 77 77 87 87 97 + 97 {} {} 8 28 38 38 58 58 58 58 68 78 78 88 + 98 98 {} {} 9 19 29 29 29 39 39 39 49 59 59 + 59 59 69 79 89 89 89 99 99 {} {}} + +do_execsql_test 1.13.11.5 { + SELECT first_value(b) OVER ( ORDER BY b%10,a ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2 +} {30 80 20 90 60 70 80 90 30 50 10 30 81 91 61 + 91 91 1 81 41 61 1 21 11 51 41 31 31 11 81 91 + 91 21 62 12 32 22 42 2 72 12 22 2 72 72 12 62 + 52 82 93 23 93 43 3 43 33 53 63 73 13 73 73 + 33 93 23 13 33 3 33 83 54 84 74 24 4 94 84 74 + 34 34 44 74 64 14 34 84 84 44 34 65 35 85 85 + 55 15 25 75 95 65 65 35 5 15 95 55 75 85 75 + 15 95 96 46 6 46 16 16 86 56 56 56 16 36 76 + 96 96 26 26 36 66 36 36 97 27 97 67 77 47 7 + 47 87 37 87 77 7 57 47 47 37 27 17 7 38 68 78 + 8 28 98 78 58 98 8 88 8 58 58 58 38 89 59 39 + 99 29 59 89 89 29 9 79 49 59 29 59 19 39 9 9 + 99 69 39 {} {}} + +do_execsql_test 1.13.11.6 { + SELECT first_value(b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.13.12.1 { + SELECT lead(b,b) OVER (ORDER BY a ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING) FROM t2 +} {96 9 11 89 32 53 91 30 51 56 54 73 22 59 75 + 74 78 8 16 65 15 8 31 87 90 12 32 96 74 76 37 + 85 90 15 35 2 60 36 75 9 51 47 63 51 90 26 42 + 26 8 76 80 90 37 87 56 79 5 87 8 2 39 73 64 + 36 90 72 78 36 73 51 33 20 41 2 26 37 33 8 14 + 33 81 55 1 9 12 39 64 87 72 34 82 21 34 99 62 + 74 41 69 22 75 27 58 8 79 77 26 26 55 {} 29 + 30 7 {} 66 55 2 34 64 {} 33 {} 44 84 {} {} 95 + 85 19 {} 83 {} 91 {} {} 9 50 91 33 34 {} {} + 84 {} 7 9 {} {} {} 44 {} {} {} {} 91 84 {} 95 + 95 52 {} {} {} {} {} 21 {} {} {} 58 {} {} {} + {} {} {} {} 83 {} {} {} {} {} {} {} {} {} {} + {} {} {} {}} + +do_execsql_test 1.13.12.2 { + SELECT lead(b,b) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 81 {} {} {} 21 {} {} {} {} {} {} + {} {} {} {} {} {} 62 {} {} {} 12 {} {} {} 72 + {} {} {} {} {} {} {} {} {} {} 53 {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 34 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} 95 {} {} {} {} {} {} 85 {} + {} {} {} {} {} {} {} {} {} 56 {} 36 {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 57 {} {} {} {} {} 7 {} {} {} {} + {} {} {} {} {} {} 8 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 9 {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.13.12.3 { + SELECT lead(b,b) OVER ( ORDER BY b,a ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2 +} {1 2 3 3 5 6 7 8 8 9 9 10 11 12 12 13 13 + 14 15 16 16 17 19 20 21 22 23 24 25 26 27 27 + 28 29 30 31 32 33 33 33 34 34 35 36 36 36 37 + 38 39 39 40 41 42 43 43 44 46 47 47 47 49 50 + 52 53 54 55 56 56 57 58 58 58 59 59 59 60 61 + 62 62 64 65 65 67 69 70 72 72 73 74 74 75 75 + 75 77 78 80 81 81 83 84 84 85 85 85 87 88 89 + 89 89 90 90 91 91 91 93 93 94 95 95 96 97 97 + 98 99 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.13.12.4 { + SELECT lead(b,b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2 +} {80 {} {} {} {} {} {} {} {} {} {} {} {} {} 1 + 11 81 81 {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} 12 12 72 82 {} {} {} {} {} {} + {} {} {} {} {} {} 13 23 73 73 {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} 34 84 {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 35 85 85 95 {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} 36 86 96 96 {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 37 47 + 47 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} 58 58 68 {} {} {} {} {} {} {} {} {} + {} {} {} {} 39 49 59 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.13.12.5 { + SELECT lead(b,b) OVER ( ORDER BY b%10,a ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2 +} {34 72 91 74 21 55 23 94 84 95 2 13 61 22 55 + 55 94 85 75 81 65 73 74 21 72 12 94 33 63 73 + 72 96 36 76 3 25 62 3 73 34 12 46 43 93 72 16 + 86 63 15 65 36 77 24 57 25 53 95 34 95 16 97 + 74 97 67 25 98 44 34 65 54 5 68 96 28 47 95 + 34 39 8 38 6 46 96 28 47 95 56 39 99 97 76 8 + 26 9 79 27 95 16 29 {} 58 58 77 85 56 {} 98 + 29 {} 19 96 {} {} 78 56 98 36 97 {} 89 89 29 + 47 78 {} {} {} 38 68 58 {} 58 38 {} 98 {} {} + {} 39 57 9 {} 79 {} {} 7 {} {} {} 9 29 38 78 + {} {} {} 8 39 {} {} {} {} 59 {} 99 {} {} {} + {} {} {} {} {} {} {} {} {} {} 9 {} {} {} {} + {} {} {} {} {} {} {} {}} + +do_execsql_test 1.13.12.6 { + SELECT lead(b,b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.13.13.1 { + SELECT lag(b,b) OVER (ORDER BY a ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} 38 {} {} {} {} + {} {} {} 6 {} {} {} {} {} 81 46 6 {} {} {} {} + 23 {} {} {} {} {} 27 {} {} {} 35 6 {} 12 {} + 23 {} {} 61 84 {} 93 39 47 {} 54 46 96 56 {} + 16 {} {} {} {} 89 {} 16 43 {} 85 56 29 99 53 + {} 59 {} {} 91 59 53 84 99 {} 93 63 47 {} {} + 98 33 67 35 75 1 23 13 55 27 75 98 35 73 63 2 + 21 27 13 24 86 23 84 31 20 94 61 65 75 23 36 + 94 55 90 41 77 96 56 29 40 12 89 63 11 5 73 + 79 1 16 28 31 73 5 39 53 63 41 11 40 2 13 33 + 9 29 90 47 72 9 73 30 44 33 74 93 29 74 42 34 + 63 41 34 96 47 77 1 36 74 72 14 36 26 77 9 72 + 64 8 91 31 52 30} + +do_execsql_test 1.13.13.2 { + SELECT lag(b,b) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} 30 {} {} + {} {} {} {} 91 {} {} {} 61 {} 81 {} {} {} {} + 1 {} {} {} {} {} {} {} {} {} 22 {} {} {} 12 + {} {} 62 {} {} {} {} {} {} {} 23 {} {} {} {} + {} {} {} {} {} {} {} 43 {} 23 {} {} {} {} {} + {} 54 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 75 {} + {} {} {} {} {} 55 {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} 47 {} {} {} {} + {} 27 7 {} {} {} {} {} {} {} {} {} 68 {} 8 {} + {} {} {} {} {} {} {} {} {} {} {} {} 89 {} {} + {} {} {} {} {} 29 9 {} {} {}} + +do_execsql_test 1.13.13.3 { + SELECT lag(b,b) OVER ( ORDER BY b,a ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2 +} {{} 1 1 1 1 2 2 2 2 2 2 3 3 3 4 4 5 6 6 + 6 7 7 7 7 7 8 8 8 8 8 8 9 9 9 9 9 9 9 + 9 9 9 10 10 10 10 11 11 11 11 11 12 12 12 12 + 13 13 13 13 13 14 15 15 15 15 16 16 16 16 16 + 17 19 20 20 21 21 21 21 22 22 22 22 23 23 23 + 23 23 24 23 24 24 25 26 26 26 26 26 26 26 26 + 26 26 26 27 27 27 27 28 29 29 29 29 30 30 30 + 30 30 30 31 31 31 31 31 32 32 32 32 32 32 31 + 32 33 33 33 33 33 33 34 34 34 34 34 34 34 34 + 35 35 35 35 35 36 36 36 36 36 36 36 37 37 37 + 38 38 38 38 38 38 39 39 39 39 40 40 41 41 42 + 43 42 43 43 43 43 44 44 44 46 46 46 47 47 47 + 47 47} + +do_execsql_test 1.13.13.4 { + SELECT lag(b,b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + 1 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.13.13.5 { + SELECT lag(b,b) OVER ( ORDER BY b%10,a ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} 30 {} {} + {} {} {} {} 91 {} {} {} 61 80 81 {} {} {} {} + 1 {} {} {} 30 {} 21 90 61 {} 22 {} 11 41 12 + {} {} 62 {} {} {} {} 31 {} 50 23 30 21 90 {} + {} 62 {} {} 81 {} 22 43 62 23 32 {} 91 {} 90 + 93 54 {} {} 90 72 12 22 90 81 83 23 80 20 72 + 43 51 33 80 90 2 34 54 1 20 62 12 13 75 44 30 + 93 91 1 21 55 61 61 13 85 3 65 65 91 73 33 93 + 55 84 62 31 11 65 35 85 33 55 15 12 75 22 3 + 73 65 36 85 43 95 43 13 47 44 65 65 96 36 27 + 7 46 34 94 47 36 73 34 35 73 68 24 8 75 85 75 + 66 34 95 36 84 77 46 34 84 47 89 65 36 16 38 + 76 58 57 29 9 44 56 17} + +do_execsql_test 1.13.13.6 { + SELECT lag(b,b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.13.14.1 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (ORDER BY a ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING) FROM t2 +} {96.59.38 59.38.68 38.68.39 68.39.62 39.62.91 62.91.46 91.46.6 + 46.6.99 6.99.97 99.97.27 97.27.46 27.46.78 46.78.54 78.54.97 + 54.97.8 97.8.67 8.67.29 67.29.93 29.93.84 93.84.77 84.77.23 + 77.23.16 23.16.16 16.16.93 16.93.65 93.65.35 65.35.47 35.47.7 + 47.7.86 7.86.74 86.74.61 74.61.91 61.91.85 91.85.24 85.24.85 + 24.85.43 85.43.59 43.59.12 59.12.32 12.32.56 32.56.3 56.3.91 + 3.91.22 91.22.90 22.90.55 90.55.15 55.15.28 15.28.89 28.89.25 + 89.25.47 25.47.1 47.1.56 1.56.40 56.40.43 40.43.56 43.56.16 + 56.16.75 16.75.36 75.36.89 36.89.98 89.98.76 98.76.81 76.81.4 + 81.4.94 4.94.42 94.42.30 42.30.78 30.78.33 78.33.29 33.29.53 + 29.53.63 53.63.2 63.2.87 2.87.37 87.37.80 37.80.84 80.84.72 + 84.72.41 72.41.9 41.9.61 9.61.73 61.73.95 73.95.65 95.65.13 + 65.13.58 13.58.96 58.96.98 96.98.1 98.1.21 1.21.74 21.74.65 + 74.65.35 65.35.5 35.5.73 5.73.11 73.11.51 11.51.87 51.87.41 + 87.41.12 41.12.8 12.8.20 8.20.31 20.31.31 31.31.15 31.15.95 + 15.95.22 95.22.73 22.73.79 73.79.88 79.88.34 88.34.8 34.8.11 + 8.11.49 11.49.34 49.34.90 34.90.59 90.59.96 59.96.60 96.60.55 + 60.55.75 55.75.77 75.77.44 77.44.2 44.2.7 2.7.85 7.85.57 + 85.57.74 57.74.29 74.29.70 29.70.59 70.59.19 59.19.39 19.39.26 + 39.26.26 26.26.47 26.47.80 47.80.90 80.90.36 90.36.58 36.58.47 + 58.47.9 47.9.72 9.72.72 72.72.66 72.66.33 66.33.93 33.93.75 + 93.75.64 75.64.81 64.81.9 81.9.23 9.23.37 23.37.13 37.13.12 + 13.12.14 12.14.62 14.62.91 62.91.36 91.36.91 36.91.33 91.33.15 + 33.15.34 15.34.36 34.36.99 36.99.3 99.3.95 3.95.69 95.69.58 + 69.58.52 58.52.30 52.30.50 30.50.84 50.84.10 84.10.84 10.84.33 + 84.33.21 33.21.39 21.39.44 39.44.58 44.58.30 58.30.38 30.38.34 + 38.34.83 34.83.27 83.27.82 27.82.17 82.17.7 17.7 7 {} {}} + +do_execsql_test 1.13.14.2 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING) FROM t2 +} {30.80.20 80.20.90 20.90.60 90.60.70 60.70.80 70.80.90 80.90.30 + 90.30.50 30.50.10 50.10.30 10.30 30 {} {} 61.91.91 91.91.1 + 91.1.81 1.81.41 81.41.61 41.61.1 61.1.21 1.21.11 21.11.51 + 11.51.41 51.41.31 41.31.31 31.31.11 31.11.81 11.81.91 81.91.91 + 91.91.21 91.21 21 {} {} 32.22.42 22.42.2 42.2.72 2.72.12 + 72.12.22 12.22.2 22.2.72 2.72.72 72.72.12 72.12.62 12.62.52 + 62.52.82 52.82 82 {} {} 93.43.3 43.3.43 3.43.33 43.33.53 + 33.53.63 53.63.73 63.73.13 73.13.73 13.73.73 73.73.33 73.33.93 + 33.93.23 93.23.13 23.13.33 13.33.3 33.3.33 3.33.83 33.83 83 + {} {} 74.24.4 24.4.94 4.94.84 94.84.74 84.74.34 74.34.34 + 34.34.44 34.44.74 44.74.64 74.64.14 64.14.34 14.34.84 34.84.84 + 84.84.44 84.44.34 44.34 34 {} {} 85.85.55 85.55.15 55.15.25 + 15.25.75 25.75.95 75.95.65 95.65.65 65.65.35 65.35.5 35.5.15 + 5.15.95 15.95.55 95.55.75 55.75.85 75.85.75 85.75.15 75.15.95 + 15.95 95 {} {} 6.46.16 46.16.16 16.16.86 16.86.56 86.56.56 + 56.56.56 56.56.16 56.16.36 16.36.76 36.76.96 76.96.96 96.96.26 + 96.26.26 26.26.36 26.36.66 36.66.36 66.36.36 36.36 36 {} {} + 97.67.77 67.77.47 77.47.7 47.7.47 7.47.87 47.87.37 87.37.87 + 37.87.77 87.77.7 77.7.57 7.57.47 57.47.47 47.47.37 47.37.27 + 37.27.17 27.17.7 17.7 7 {} {} 78.8.28 8.28.98 28.98.78 + 98.78.58 78.58.98 58.98.8 98.8.88 8.88.8 88.8.58 8.58.58 + 58.58.58 58.58.38 58.38 38 {} {} 39.99.29 99.29.59 29.59.89 + 59.89.89 89.89.29 89.29.9 29.9.79 9.79.49 79.49.59 49.59.29 + 59.29.59 29.59.19 59.19.39 19.39.9 39.9.9 9.9.99 9.99.69 + 99.69.39 69.39 39 {} {}} + +do_execsql_test 1.13.14.3 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( ORDER BY b,a ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2 +} {2.2.3 2.3.3 3.3.4 3.4.5 4.5.6 5.6.7 6.7.7 7.7.7 7.7.8 + 7.8.8 8.8.8 8.8.9 8.9.9 9.9.9 9.9.10 9.10.11 10.11.11 + 11.11.12 11.12.12 12.12.12 12.12.13 12.13.13 13.13.14 13.14.15 + 14.15.15 15.15.15 15.15.16 15.16.16 16.16.16 16.16.17 16.17.19 + 17.19.20 19.20.21 20.21.21 21.21.22 21.22.22 22.22.23 22.23.23 + 23.23.24 23.24.25 24.25.26 25.26.26 26.26.27 26.27.27 27.27.28 + 27.28.29 28.29.29 29.29.29 29.29.30 29.30.30 30.30.30 30.30.31 + 30.31.31 31.31.32 31.32.33 32.33.33 33.33.33 33.33.33 33.33.34 + 33.34.34 34.34.34 34.34.34 34.34.35 34.35.35 35.35.36 35.36.36 + 36.36.36 36.36.36 36.36.37 36.37.37 37.37.38 37.38.38 38.38.39 + 38.39.39 39.39.39 39.39.40 39.40.41 40.41.41 41.41.42 41.42.43 + 42.43.43 43.43.44 43.44.44 44.44.46 44.46.46 46.46.47 46.47.47 + 47.47.47 47.47.47 47.47.49 47.49.50 49.50.51 50.51.52 51.52.53 + 52.53.54 53.54.55 54.55.55 55.55.56 55.56.56 56.56.56 56.56.57 + 56.57.58 57.58.58 58.58.58 58.58.58 58.58.59 58.59.59 59.59.59 + 59.59.59 59.59.60 59.60.61 60.61.61 61.61.62 61.62.62 62.62.63 + 62.63.64 63.64.65 64.65.65 65.65.65 65.65.66 65.66.67 66.67.68 + 67.68.69 68.69.70 69.70.72 70.72.72 72.72.72 72.72.73 72.73.73 + 73.73.73 73.73.74 73.74.74 74.74.74 74.74.75 74.75.75 75.75.75 + 75.75.76 75.76.77 76.77.77 77.77.78 77.78.78 78.78.79 78.79.80 + 79.80.80 80.80.81 80.81.81 81.81.81 81.81.82 81.82.83 82.83.84 + 83.84.84 84.84.84 84.84.84 84.84.85 84.85.85 85.85.85 85.85.86 + 85.86.87 86.87.87 87.87.88 87.88.89 88.89.89 89.89.89 89.89.90 + 89.90.90 90.90.90 90.90.91 90.91.91 91.91.91 91.91.91 91.91.91 + 91.91.93 91.93.93 93.93.93 93.93.94 93.94.95 94.95.95 95.95.95 + 95.95.96 95.96.96 96.96.96 96.96.97 96.97.97 97.97.98 97.98.98 + 98.98.99 98.99.99 99.99 99 {} {}} + +do_execsql_test 1.13.14.4 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2 +} {30.30.30 30.30.40 30.40.50 40.50.60 50.60.70 60.70.80 70.80.80 + 80.80.90 80.90.90 90.90.90 90.90 90 {} {} 11.11.21 11.21.21 + 21.21.31 21.31.31 31.31.41 31.41.41 41.41.51 41.51.61 51.61.61 + 61.61.81 61.81.81 81.81.81 81.81.91 81.91.91 91.91.91 91.91.91 + 91.91.91 91.91 91 {} {} 12.12.12 12.12.22 12.22.22 22.22.32 + 22.32.42 32.42.52 42.52.62 52.62.62 62.62.72 62.72.72 72.72.72 + 72.72.82 72.82 82 {} {} 13.13.23 13.23.23 23.23.33 23.33.33 + 33.33.33 33.33.33 33.33.43 33.43.43 43.43.53 43.53.63 53.63.73 + 63.73.73 73.73.73 73.73.83 73.83.93 83.93.93 93.93.93 93.93 + 93 {} {} 24.34.34 34.34.34 34.34.34 34.34.44 34.44.44 + 44.44.54 44.54.64 54.64.74 64.74.74 74.74.74 74.74.84 74.84.84 + 84.84.84 84.84.84 84.84.94 84.94 94 {} {} 15.15.25 15.25.35 + 25.35.35 35.35.55 35.55.55 55.55.65 55.65.65 65.65.65 65.65.75 + 65.75.75 75.75.75 75.75.85 75.85.85 85.85.85 85.85.95 85.95.95 + 95.95.95 95.95 95 {} {} 16.16.26 16.26.26 26.26.36 26.36.36 + 36.36.36 36.36.36 36.36.46 36.46.46 46.46.56 46.56.56 56.56.56 + 56.56.66 56.66.76 66.76.86 76.86.96 86.96.96 96.96.96 96.96 + 96 {} {} 7.17.27 17.27.27 27.27.37 27.37.37 37.37.47 + 37.47.47 47.47.47 47.47.47 47.47.57 47.57.67 57.67.77 67.77.77 + 77.77.87 77.87.87 87.87.97 87.97.97 97.97 97 {} {} 8.28.38 + 28.38.38 38.38.58 38.58.58 58.58.58 58.58.58 58.58.68 58.68.78 + 68.78.78 78.78.88 78.88.98 88.98.98 98.98 98 {} {} 9.19.29 + 19.29.29 29.29.29 29.29.39 29.39.39 39.39.39 39.39.49 39.49.59 + 49.59.59 59.59.59 59.59.59 59.59.69 59.69.79 69.79.89 79.89.89 + 89.89.89 89.89.99 89.99.99 99.99 99 {} {}} + +do_execsql_test 1.13.14.5 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( ORDER BY b%10,a ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING ) FROM t2 +} {30.80.20 80.20.90 20.90.60 90.60.70 60.70.80 70.80.90 80.90.30 + 90.30.50 30.50.10 50.10.30 10.30.81 30.81.91 81.91.61 91.61.91 + 61.91.91 91.91.1 91.1.81 1.81.41 81.41.61 41.61.1 61.1.21 + 1.21.11 21.11.51 11.51.41 51.41.31 41.31.31 31.31.11 31.11.81 + 11.81.91 81.91.91 91.91.21 91.21.62 21.62.12 62.12.32 12.32.22 + 32.22.42 22.42.2 42.2.72 2.72.12 72.12.22 12.22.2 22.2.72 + 2.72.72 72.72.12 72.12.62 12.62.52 62.52.82 52.82.93 82.93.23 + 93.23.93 23.93.43 93.43.3 43.3.43 3.43.33 43.33.53 33.53.63 + 53.63.73 63.73.13 73.13.73 13.73.73 73.73.33 73.33.93 33.93.23 + 93.23.13 23.13.33 13.33.3 33.3.33 3.33.83 33.83.54 83.54.84 + 54.84.74 84.74.24 74.24.4 24.4.94 4.94.84 94.84.74 84.74.34 + 74.34.34 34.34.44 34.44.74 44.74.64 74.64.14 64.14.34 14.34.84 + 34.84.84 84.84.44 84.44.34 44.34.65 34.65.35 65.35.85 35.85.85 + 85.85.55 85.55.15 55.15.25 15.25.75 25.75.95 75.95.65 95.65.65 + 65.65.35 65.35.5 35.5.15 5.15.95 15.95.55 95.55.75 55.75.85 + 75.85.75 85.75.15 75.15.95 15.95.96 95.96.46 96.46.6 46.6.46 + 6.46.16 46.16.16 16.16.86 16.86.56 86.56.56 56.56.56 56.56.16 + 56.16.36 16.36.76 36.76.96 76.96.96 96.96.26 96.26.26 26.26.36 + 26.36.66 36.66.36 66.36.36 36.36.97 36.97.27 97.27.97 27.97.67 + 97.67.77 67.77.47 77.47.7 47.7.47 7.47.87 47.87.37 87.37.87 + 37.87.77 87.77.7 77.7.57 7.57.47 57.47.47 47.47.37 47.37.27 + 37.27.17 27.17.7 17.7.38 7.38.68 38.68.78 68.78.8 78.8.28 + 8.28.98 28.98.78 98.78.58 78.58.98 58.98.8 98.8.88 8.88.8 + 88.8.58 8.58.58 58.58.58 58.58.38 58.38.89 38.89.59 89.59.39 + 59.39.99 39.99.29 99.29.59 29.59.89 59.89.89 89.89.29 89.29.9 + 29.9.79 9.79.49 79.49.59 49.59.29 59.29.59 29.59.19 59.19.39 + 19.39.9 39.9.9 9.9.99 9.99.69 99.69.39 69.39 39 {} {}} + +do_execsql_test 1.13.14.6 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.13.14.7 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (win1 ORDER BY b%10 ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING) + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a) + ORDER BY 1 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.13.14.8 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (win1 ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING) + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a ORDER BY b%10) + ORDER BY 1 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.13.14.9 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER win2 + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a ORDER BY b%10), + win2 AS (win1 ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING) + ORDER BY 1 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.13.15.1 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE a%2=0) OVER win FROM t2 + WINDOW win AS (ORDER BY a ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING) +} {3 96.38 3 38 3 38.39 3 39 3 39.91 3 91 3 91.6 3 6 3 6.97 + 3 97 3 97.46 3 46 3 46.54 3 54 3 54.8 3 8 3 8.29 3 29 + 3 29.84 3 84 3 84.23 3 23 3 23.16 3 16 3 16.65 3 65 3 65.47 + 3 47 3 47.86 3 86 3 86.61 3 61 3 61.85 3 85 3 85.85 3 85 + 3 85.59 3 59 3 59.32 3 32 3 32.3 3 3 3 3.22 3 22 3 22.55 + 3 55 3 55.28 3 28 3 28.25 3 25 3 25.1 3 1 3 1.40 3 40 + 3 40.56 3 56 3 56.75 3 75 3 75.89 3 89 3 89.76 3 76 3 76.4 + 3 4 3 4.42 3 42 3 42.78 3 78 3 78.29 3 29 3 29.63 3 63 + 3 63.87 3 87 3 87.80 3 80 3 80.72 3 72 3 72.9 3 9 3 9.73 + 3 73 3 73.65 3 65 3 65.58 3 58 3 58.98 3 98 3 98.21 3 21 + 3 21.65 3 65 3 65.5 3 5 3 5.11 3 11 3 11.87 3 87 3 87.12 + 3 12 3 12.20 3 20 3 20.31 3 31 3 31.95 3 95 3 95.73 3 73 + 3 73.88 3 88 3 88.8 3 8 3 8.49 3 49 3 49.90 3 90 3 90.96 + 3 96 3 96.55 3 55 3 55.77 3 77 3 77.2 3 2 3 2.85 3 85 + 3 85.74 3 74 3 74.70 3 70 3 70.19 3 19 3 19.26 3 26 3 26.47 + 3 47 3 47.90 3 90 3 90.58 3 58 3 58.9 3 9 3 9.72 3 72 + 3 72.33 3 33 3 33.75 3 75 3 75.81 3 81 3 81.23 3 23 3 23.13 + 3 13 3 13.14 3 14 3 14.91 3 91 3 91.91 3 91 3 91.15 3 15 + 3 15.36 3 36 3 36.3 3 3 3 3.69 3 69 3 69.52 3 52 3 52.50 + 3 50 3 50.10 3 10 3 10.33 3 33 3 33.39 3 39 3 39.58 3 58 + 3 58.38 3 38 3 38.83 3 83 3 83.82 3 82 3 82.7 2 7 1 7 + 0 {} 0 {}} + +do_execsql_test 1.13.15.2 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE 0=1) OVER win FROM t2 + WINDOW win AS (ORDER BY a ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING) +} {3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 2 {} 1 {} 0 {} 0 {}} + +do_execsql_test 1.13.15.3 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE 1=0) OVER win FROM t2 + WINDOW win AS (PARTITION BY (a%10) ORDER BY a ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING) +} {3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 2 {} 1 {} 0 {} 0 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 2 {} 1 {} 0 {} 0 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 2 {} + 1 {} 0 {} 0 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 2 {} 1 {} 0 {} 0 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 2 {} 1 {} 0 {} 0 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 2 {} 1 {} 0 {} 0 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 2 {} 1 {} + 0 {} 0 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 2 {} 1 {} 0 {} 0 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 2 {} 1 {} 0 {} 0 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 2 {} 1 {} 0 {} 0 {}} + +do_execsql_test 1.13.15.4 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE a%2=0) OVER win FROM t2 + WINDOW win AS (PARTITION BY (a%10) ORDER BY a ROWS BETWEEN 2 FOLLOWING AND 4 FOLLOWING) +} {3 29.47.59 3 47.59.28 3 59.28.75 3 28.75.78 3 75.78.72 3 78.72.98 + 3 72.98.87 3 98.87.73 3 87.73.96 3 73.96.74 3 96.74.90 3 74.90.75 + 3 90.75.91 3 75.91.69 3 91.69.39 3 69.39.7 2 39.7 1 7 0 {} + 0 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 2 {} 1 {} 0 {} 0 {} 3 84.86.32 + 3 86.32.25 3 32.25.89 3 25.89.29 3 89.29.9 3 29.9.21 3 9.21.12 + 3 21.12.88 3 12.88.55 3 88.55.70 3 55.70.58 3 70.58.81 3 58.81.91 + 3 81.91.52 3 91.52.58 2 52.58 1 58 0 {} 0 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 2 {} 1 {} 0 {} 0 {} 3 23.61.3 3 61.3.1 3 3.1.76 + 3 1.76.63 3 76.63.73 3 63.73.65 3 73.65.20 3 65.20.8 3 20.8.77 + 3 8.77.19 3 77.19.9 3 19.9.23 3 9.23.15 3 23.15.50 3 15.50.38 + 2 50.38 1 38 0 {} 0 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 2 {} 1 {} + 0 {} 0 {} 3 16.85.22 3 85.22.40 3 22.40.4 3 40.4.87 3 4.87.65 + 3 87.65.5 3 65.5.31 3 5.31.49 3 31.49.2 3 49.2.26 3 2.26.72 + 3 26.72.13 3 72.13.36 3 13.36.10 3 36.10.83 2 10.83 1 83 0 {} + 0 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 2 {} 1 {} 0 {} 0 {} 3 65.85.55 + 3 85.55.56 3 55.56.42 3 56.42.80 3 42.80.58 3 80.58.11 3 58.11.95 + 3 11.95.90 3 95.90.85 3 90.85.47 3 85.47.33 3 47.33.14 3 33.14.3 + 3 14.3.33 3 3.33.82 2 33.82 1 82 0 {} 0 {} 3 {} 3 {} 3 {} + 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} 3 {} + 3 {} 2 {} 1 {} 0 {} 0 {}} + +do_execsql_test 1.14.2.1 { + SELECT max(b) OVER ( ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99} + +do_execsql_test 1.14.2.2 { + SELECT min(b) OVER ( ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1} + +do_execsql_test 1.14.3.1 { + SELECT row_number() OVER ( ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.14.3.2 { + SELECT row_number() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.14.3.3 { + SELECT row_number() OVER ( ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.14.4.1 { + SELECT dense_rank() OVER ( ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.14.4.2 { + SELECT dense_rank() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.14.4.3 { + SELECT dense_rank() OVER ( ORDER BY b ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 1 2 2 3 3 4 5 6 7 7 7 8 8 8 9 9 9 10 + 11 11 12 12 12 13 13 14 15 15 15 16 16 16 17 + 18 19 20 20 21 21 22 22 23 24 25 25 26 26 27 + 28 28 28 29 29 29 30 30 31 32 32 32 32 33 33 + 33 33 34 34 35 35 35 35 36 36 37 37 38 38 38 + 39 40 40 41 42 42 43 43 44 44 45 45 45 45 46 + 47 48 49 50 51 52 52 53 53 53 54 55 55 55 55 + 56 56 56 56 57 58 58 59 59 60 61 62 62 62 63 + 64 65 66 67 68 68 68 69 69 69 70 70 70 71 71 + 71 72 73 73 74 74 75 76 76 77 77 77 78 79 80 + 80 80 80 81 81 81 82 83 83 84 85 85 85 86 86 + 86 87 87 87 87 87 88 88 88 89 90 90 90 91 91 + 91 92 92 93 93 94 94} + +do_execsql_test 1.14.4.4 { + SELECT dense_rank() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 3 3 4 5 6 7 8 8 9 9 9 1 1 2 2 3 + 3 4 4 5 5 6 7 7 8 8 8 9 9 9 9 9 1 1 2 + 2 2 3 3 4 5 6 7 7 8 8 8 9 1 1 2 2 3 3 + 4 4 4 4 5 5 6 7 8 8 8 9 10 10 10 1 2 3 + 4 4 4 4 5 5 6 7 8 8 8 9 9 9 9 10 1 2 2 + 2 3 4 4 5 5 6 6 6 7 7 7 8 8 8 9 9 9 1 + 2 2 2 3 3 4 4 4 4 5 5 6 6 6 7 8 9 10 10 + 10 1 1 1 2 3 3 4 4 5 5 5 5 6 7 8 8 9 9 + 10 10 1 1 1 2 3 3 4 4 4 4 5 6 6 7 8 8 1 + 1 1 2 3 3 3 4 4 4 5 6 6 6 6 7 8 9 9 9 + 10 10} + +do_execsql_test 1.14.4.5 { + SELECT dense_rank() OVER ( ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 + 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 + 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 + 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 + 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 + 8 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 10 10 + 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 + 10 10 10 10 10} + +do_execsql_test 1.14.4.6 { + SELECT dense_rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 + 5 5 5 5 5 5 5 5 5 5 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 + 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 + 5} + +do_execsql_test 1.14.5.1 { + SELECT rank() OVER ( ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.14.5.2 { + SELECT rank() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.14.5.3 { + SELECT rank() OVER ( ORDER BY b ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 1 3 3 5 5 7 8 9 10 10 10 13 13 13 16 16 + 16 19 20 20 22 22 22 25 25 27 28 28 28 31 31 + 31 34 35 36 37 37 39 39 41 41 43 44 45 45 47 + 47 49 50 50 50 53 53 53 56 56 58 59 59 59 59 + 63 63 63 63 67 67 69 69 69 69 73 73 75 75 77 + 77 77 80 81 81 83 84 84 86 86 88 88 90 90 90 + 90 94 95 96 97 98 99 100 100 102 102 102 105 106 + 106 106 106 110 110 110 110 114 115 115 117 117 119 + 120 121 121 121 124 125 126 127 128 129 129 129 132 + 132 132 135 135 135 138 138 138 141 142 142 144 144 + 146 147 147 149 149 149 152 153 154 154 154 154 158 + 158 158 161 162 162 164 165 165 165 168 168 168 171 + 171 171 171 171 176 176 176 179 180 180 180 183 183 + 183 186 186 188 188 190 190} + +do_execsql_test 1.14.5.4 { + SELECT rank() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 3 3 6 7 8 9 10 10 12 12 12 1 1 3 3 + 5 5 7 7 9 9 11 12 12 14 14 14 17 17 17 17 + 17 1 1 3 3 3 6 6 8 9 10 11 11 13 13 13 16 + 1 1 3 3 5 5 7 7 7 7 11 11 13 14 15 15 15 + 18 19 19 19 1 2 3 4 4 4 4 8 8 10 11 12 12 + 12 15 15 15 15 19 1 2 2 2 5 6 6 8 8 10 10 + 10 13 13 13 16 16 16 19 19 19 1 2 2 2 5 5 7 + 7 7 7 11 11 13 13 13 16 17 18 19 19 19 1 1 + 1 4 5 5 7 7 9 9 9 9 13 14 15 15 17 17 19 + 19 1 1 1 4 5 5 7 7 7 7 11 12 12 14 15 15 + 1 1 1 4 5 5 5 8 8 8 11 12 12 12 12 16 17 + 18 18 18 21 21} + +do_execsql_test 1.14.5.5 { + SELECT rank() OVER ( ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 15 15 15 15 + 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 + 15 15 36 36 36 36 36 36 36 36 36 36 36 36 36 + 36 36 36 52 52 52 52 52 52 52 52 52 52 52 52 + 52 52 52 52 52 52 52 52 52 73 73 73 73 73 73 + 73 73 73 73 73 73 73 73 73 73 73 73 73 92 92 + 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 + 92 92 92 92 113 113 113 113 113 113 113 113 113 + 113 113 113 113 113 113 113 113 113 113 113 113 134 + 134 134 134 134 134 134 134 134 134 134 134 134 134 + 134 134 134 134 134 134 154 154 154 154 154 154 154 + 154 154 154 154 154 154 154 154 154 170 170 170 170 + 170 170 170 170 170 170 170 170 170 170 170 170 170 + 170 170 170 170 170} + +do_execsql_test 1.14.5.6 { + SELECT rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 15 15 15 15 + 15 15 15 15 15 15 15 15 15 15 15 15 31 31 31 + 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 + 31 50 50 50 50 50 50 50 50 50 50 50 50 50 50 + 50 50 50 50 50 50 50 71 71 71 71 71 71 71 71 + 71 71 71 71 71 71 71 71 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 22 22 22 22 22 22 + 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 + 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 + 43 43 43 43 43 43 64 64 64 64 64 64 64 64 64 + 64 64 64 64 64 64 64 64 64 64 64 84 84 84 84 + 84 84 84 84 84 84 84 84 84 84 84 84 84 84 84 + 84 84 84} + +do_execsql_test 1.14.6.1 { + SELECT + row_number() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ), + rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ), + dense_rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) + FROM t2 +} {1 1 1 2 1 1 3 1 1 4 1 1 5 1 1 6 1 1 7 1 1 8 1 1 9 1 1 + 10 1 1 11 1 1 12 1 1 13 1 1 14 1 1 15 15 2 16 15 2 17 15 2 + 18 15 2 19 15 2 20 15 2 21 15 2 22 15 2 23 15 2 24 15 2 + 25 15 2 26 15 2 27 15 2 28 15 2 29 15 2 30 15 2 31 31 3 + 32 31 3 33 31 3 34 31 3 35 31 3 36 31 3 37 31 3 38 31 3 + 39 31 3 40 31 3 41 31 3 42 31 3 43 31 3 44 31 3 45 31 3 + 46 31 3 47 31 3 48 31 3 49 31 3 50 50 4 51 50 4 52 50 4 + 53 50 4 54 50 4 55 50 4 56 50 4 57 50 4 58 50 4 59 50 4 + 60 50 4 61 50 4 62 50 4 63 50 4 64 50 4 65 50 4 66 50 4 + 67 50 4 68 50 4 69 50 4 70 50 4 71 71 5 72 71 5 73 71 5 + 74 71 5 75 71 5 76 71 5 77 71 5 78 71 5 79 71 5 80 71 5 + 81 71 5 82 71 5 83 71 5 84 71 5 85 71 5 86 71 5 1 1 1 2 1 1 + 3 1 1 4 1 1 5 1 1 6 1 1 7 1 1 8 1 1 9 1 1 10 1 1 11 1 1 + 12 1 1 13 1 1 14 1 1 15 1 1 16 1 1 17 1 1 18 1 1 19 1 1 + 20 1 1 21 1 1 22 22 2 23 22 2 24 22 2 25 22 2 26 22 2 27 22 2 + 28 22 2 29 22 2 30 22 2 31 22 2 32 22 2 33 22 2 34 22 2 + 35 22 2 36 22 2 37 22 2 38 22 2 39 22 2 40 22 2 41 22 2 + 42 22 2 43 43 3 44 43 3 45 43 3 46 43 3 47 43 3 48 43 3 + 49 43 3 50 43 3 51 43 3 52 43 3 53 43 3 54 43 3 55 43 3 + 56 43 3 57 43 3 58 43 3 59 43 3 60 43 3 61 43 3 62 43 3 + 63 43 3 64 64 4 65 64 4 66 64 4 67 64 4 68 64 4 69 64 4 + 70 64 4 71 64 4 72 64 4 73 64 4 74 64 4 75 64 4 76 64 4 + 77 64 4 78 64 4 79 64 4 80 64 4 81 64 4 82 64 4 83 64 4 + 84 84 5 85 84 5 86 84 5 87 84 5 88 84 5 89 84 5 90 84 5 + 91 84 5 92 84 5 93 84 5 94 84 5 95 84 5 96 84 5 97 84 5 + 98 84 5 99 84 5 100 84 5 101 84 5 102 84 5 103 84 5 104 84 5 + 105 84 5} + + +do_test 1.14.7.1 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0053 0.0105 0.0158 0.0211 0.0263 0.0316 0.0368 0.0421 0.0474 0.0526 0.0579 0.0632 0.0684 0.0737 0.0789 0.0842 0.0895 0.0947 0.1000 0.1053 0.1105 0.1158 0.1211 0.1263 0.1316 0.1368 0.1421 0.1474 0.1526 0.1579 0.1632 0.1684 0.1737 0.1789 0.1842 0.1895 0.1947 0.2000 0.2053 0.2105 0.2158 0.2211 0.2263 0.2316 0.2368 0.2421 0.2474 0.2526 0.2579 0.2632 0.2684 0.2737 0.2789 0.2842 0.2895 0.2947 0.3000 0.3053 0.3105 0.3158 0.3211 0.3263 0.3316 0.3368 0.3421 0.3474 0.3526 0.3579 0.3632 0.3684 0.3737 0.3789 0.3842 0.3895 0.3947 0.4000 0.4053 0.4105 0.4158 0.4211 0.4263 0.4316 0.4368 0.4421 0.4474 0.4526 0.4579 0.4632 0.4684 0.4737 0.4789 0.4842 0.4895 0.4947 0.5000 0.5053 0.5105 0.5158 0.5211 0.5263 0.5316 0.5368 0.5421 0.5474 0.5526 0.5579 0.5632 0.5684 0.5737 0.5789 0.5842 0.5895 0.5947 0.6000 0.6053 0.6105 0.6158 0.6211 0.6263 0.6316 0.6368 0.6421 0.6474 0.6526 0.6579 0.6632 0.6684 0.6737 0.6789 0.6842 0.6895 0.6947 0.7000 0.7053 0.7105 0.7158 0.7211 0.7263 0.7316 0.7368 0.7421 0.7474 0.7526 0.7579 0.7632 0.7684 0.7737 0.7789 0.7842 0.7895 0.7947 0.8000 0.8053 0.8105 0.8158 0.8211 0.8263 0.8316 0.8368 0.8421 0.8474 0.8526 0.8579 0.8632 0.8684 0.8737 0.8789 0.8842 0.8895 0.8947 0.9000 0.9053 0.9105 0.9158 0.9211 0.9263 0.9316 0.9368 0.9421 0.9474 0.9526 0.9579 0.9632 0.9684 0.9737 0.9789 0.9842 0.9895 0.9947 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.14.7.2 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0769 0.1538 0.2308 0.3077 0.3846 0.4615 0.5385 0.6154 0.6923 0.7692 0.8462 0.9231 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0667 0.1333 0.2000 0.2667 0.3333 0.4000 0.4667 0.5333 0.6000 0.6667 0.7333 0.8000 0.8667 0.9333 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0556 0.1111 0.1667 0.2222 0.2778 0.3333 0.3889 0.4444 0.5000 0.5556 0.6111 0.6667 0.7222 0.7778 0.8333 0.8889 0.9444 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0526 0.1053 0.1579 0.2105 0.2632 0.3158 0.3684 0.4211 0.4737 0.5263 0.5789 0.6316 0.6842 0.7368 0.7895 0.8421 0.8947 0.9474 1.0000 0.0000 0.0667 0.1333 0.2000 0.2667 0.3333 0.4000 0.4667 0.5333 0.6000 0.6667 0.7333 0.8000 0.8667 0.9333 1.0000 0.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.14.7.3 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY b ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0105 0.0105 0.0211 0.0211 0.0316 0.0368 0.0421 0.0474 0.0474 0.0474 0.0632 0.0632 0.0632 0.0789 0.0789 0.0789 0.0947 0.1000 0.1000 0.1105 0.1105 0.1105 0.1263 0.1263 0.1368 0.1421 0.1421 0.1421 0.1579 0.1579 0.1579 0.1737 0.1789 0.1842 0.1895 0.1895 0.2000 0.2000 0.2105 0.2105 0.2211 0.2263 0.2316 0.2316 0.2421 0.2421 0.2526 0.2579 0.2579 0.2579 0.2737 0.2737 0.2737 0.2895 0.2895 0.3000 0.3053 0.3053 0.3053 0.3053 0.3263 0.3263 0.3263 0.3263 0.3474 0.3474 0.3579 0.3579 0.3579 0.3579 0.3789 0.3789 0.3895 0.3895 0.4000 0.4000 0.4000 0.4158 0.4211 0.4211 0.4316 0.4368 0.4368 0.4474 0.4474 0.4579 0.4579 0.4684 0.4684 0.4684 0.4684 0.4895 0.4947 0.5000 0.5053 0.5105 0.5158 0.5211 0.5211 0.5316 0.5316 0.5316 0.5474 0.5526 0.5526 0.5526 0.5526 0.5737 0.5737 0.5737 0.5737 0.5947 0.6000 0.6000 0.6105 0.6105 0.6211 0.6263 0.6316 0.6316 0.6316 0.6474 0.6526 0.6579 0.6632 0.6684 0.6737 0.6737 0.6737 0.6895 0.6895 0.6895 0.7053 0.7053 0.7053 0.7211 0.7211 0.7211 0.7368 0.7421 0.7421 0.7526 0.7526 0.7632 0.7684 0.7684 0.7789 0.7789 0.7789 0.7947 0.8000 0.8053 0.8053 0.8053 0.8053 0.8263 0.8263 0.8263 0.8421 0.8474 0.8474 0.8579 0.8632 0.8632 0.8632 0.8789 0.8789 0.8789 0.8947 0.8947 0.8947 0.8947 0.8947 0.9211 0.9211 0.9211 0.9368 0.9421 0.9421 0.9421 0.9579 0.9579 0.9579 0.9737 0.9737 0.9842 0.9842 0.9947 0.9947} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.14.7.4 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0769 0.1538 0.1538 0.1538 0.3846 0.4615 0.5385 0.6154 0.6923 0.6923 0.8462 0.8462 0.8462 0.0000 0.0000 0.1000 0.1000 0.2000 0.2000 0.3000 0.3000 0.4000 0.4000 0.5000 0.5500 0.5500 0.6500 0.6500 0.6500 0.8000 0.8000 0.8000 0.8000 0.8000 0.0000 0.0000 0.1333 0.1333 0.1333 0.3333 0.3333 0.4667 0.5333 0.6000 0.6667 0.6667 0.8000 0.8000 0.8000 1.0000 0.0000 0.0000 0.1000 0.1000 0.2000 0.2000 0.3000 0.3000 0.3000 0.3000 0.5000 0.5000 0.6000 0.6500 0.7000 0.7000 0.7000 0.8500 0.9000 0.9000 0.9000 0.0000 0.0556 0.1111 0.1667 0.1667 0.1667 0.1667 0.3889 0.3889 0.5000 0.5556 0.6111 0.6111 0.6111 0.7778 0.7778 0.7778 0.7778 1.0000 0.0000 0.0500 0.0500 0.0500 0.2000 0.2500 0.2500 0.3500 0.3500 0.4500 0.4500 0.4500 0.6000 0.6000 0.6000 0.7500 0.7500 0.7500 0.9000 0.9000 0.9000 0.0000 0.0500 0.0500 0.0500 0.2000 0.2000 0.3000 0.3000 0.3000 0.3000 0.5000 0.5000 0.6000 0.6000 0.6000 0.7500 0.8000 0.8500 0.9000 0.9000 0.9000 0.0000 0.0000 0.0000 0.1579 0.2105 0.2105 0.3158 0.3158 0.4211 0.4211 0.4211 0.4211 0.6316 0.6842 0.7368 0.7368 0.8421 0.8421 0.9474 0.9474 0.0000 0.0000 0.0000 0.2000 0.2667 0.2667 0.4000 0.4000 0.4000 0.4000 0.6667 0.7333 0.7333 0.8667 0.9333 0.9333 0.0000 0.0000 0.0000 0.1429 0.1905 0.1905 0.1905 0.3333 0.3333 0.3333 0.4762 0.5238 0.5238 0.5238 0.5238 0.7143 0.7619 0.8095 0.8095 0.8095 0.9524 0.9524} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.14.7.5 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.14.7.6 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER (PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.14.8.1 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0052 0.0105 0.0157 0.0209 0.0262 0.0314 0.0366 0.0419 0.0471 0.0524 0.0576 0.0628 0.0681 0.0733 0.0785 0.0838 0.0890 0.0942 0.0995 0.1047 0.1099 0.1152 0.1204 0.1257 0.1309 0.1361 0.1414 0.1466 0.1518 0.1571 0.1623 0.1675 0.1728 0.1780 0.1832 0.1885 0.1937 0.1990 0.2042 0.2094 0.2147 0.2199 0.2251 0.2304 0.2356 0.2408 0.2461 0.2513 0.2565 0.2618 0.2670 0.2723 0.2775 0.2827 0.2880 0.2932 0.2984 0.3037 0.3089 0.3141 0.3194 0.3246 0.3298 0.3351 0.3403 0.3455 0.3508 0.3560 0.3613 0.3665 0.3717 0.3770 0.3822 0.3874 0.3927 0.3979 0.4031 0.4084 0.4136 0.4188 0.4241 0.4293 0.4346 0.4398 0.4450 0.4503 0.4555 0.4607 0.4660 0.4712 0.4764 0.4817 0.4869 0.4921 0.4974 0.5026 0.5079 0.5131 0.5183 0.5236 0.5288 0.5340 0.5393 0.5445 0.5497 0.5550 0.5602 0.5654 0.5707 0.5759 0.5812 0.5864 0.5916 0.5969 0.6021 0.6073 0.6126 0.6178 0.6230 0.6283 0.6335 0.6387 0.6440 0.6492 0.6545 0.6597 0.6649 0.6702 0.6754 0.6806 0.6859 0.6911 0.6963 0.7016 0.7068 0.7120 0.7173 0.7225 0.7277 0.7330 0.7382 0.7435 0.7487 0.7539 0.7592 0.7644 0.7696 0.7749 0.7801 0.7853 0.7906 0.7958 0.8010 0.8063 0.8115 0.8168 0.8220 0.8272 0.8325 0.8377 0.8429 0.8482 0.8534 0.8586 0.8639 0.8691 0.8743 0.8796 0.8848 0.8901 0.8953 0.9005 0.9058 0.9110 0.9162 0.9215 0.9267 0.9319 0.9372 0.9424 0.9476 0.9529 0.9581 0.9634 0.9686 0.9738 0.9791 0.9843 0.9895 0.9948 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.14.8.2 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0714 0.1429 0.2143 0.2857 0.3571 0.4286 0.5000 0.5714 0.6429 0.7143 0.7857 0.8571 0.9286 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0625 0.1250 0.1875 0.2500 0.3125 0.3750 0.4375 0.5000 0.5625 0.6250 0.6875 0.7500 0.8125 0.8750 0.9375 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0526 0.1053 0.1579 0.2105 0.2632 0.3158 0.3684 0.4211 0.4737 0.5263 0.5789 0.6316 0.6842 0.7368 0.7895 0.8421 0.8947 0.9474 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0625 0.1250 0.1875 0.2500 0.3125 0.3750 0.4375 0.5000 0.5625 0.6250 0.6875 0.7500 0.8125 0.8750 0.9375 1.0000 0.0455 0.0909 0.1364 0.1818 0.2273 0.2727 0.3182 0.3636 0.4091 0.4545 0.5000 0.5455 0.5909 0.6364 0.6818 0.7273 0.7727 0.8182 0.8636 0.9091 0.9545 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.14.8.3 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY b ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0105 0.0105 0.0209 0.0209 0.0314 0.0314 0.0366 0.0419 0.0471 0.0628 0.0628 0.0628 0.0785 0.0785 0.0785 0.0942 0.0942 0.0942 0.0995 0.1099 0.1099 0.1257 0.1257 0.1257 0.1361 0.1361 0.1414 0.1571 0.1571 0.1571 0.1728 0.1728 0.1728 0.1780 0.1832 0.1885 0.1990 0.1990 0.2094 0.2094 0.2199 0.2199 0.2251 0.2304 0.2408 0.2408 0.2513 0.2513 0.2565 0.2723 0.2723 0.2723 0.2880 0.2880 0.2880 0.2984 0.2984 0.3037 0.3246 0.3246 0.3246 0.3246 0.3455 0.3455 0.3455 0.3455 0.3560 0.3560 0.3770 0.3770 0.3770 0.3770 0.3874 0.3874 0.3979 0.3979 0.4136 0.4136 0.4136 0.4188 0.4293 0.4293 0.4346 0.4450 0.4450 0.4555 0.4555 0.4660 0.4660 0.4869 0.4869 0.4869 0.4869 0.4921 0.4974 0.5026 0.5079 0.5131 0.5183 0.5288 0.5288 0.5445 0.5445 0.5445 0.5497 0.5707 0.5707 0.5707 0.5707 0.5916 0.5916 0.5916 0.5916 0.5969 0.6073 0.6073 0.6178 0.6178 0.6230 0.6283 0.6440 0.6440 0.6440 0.6492 0.6545 0.6597 0.6649 0.6702 0.6859 0.6859 0.6859 0.7016 0.7016 0.7016 0.7173 0.7173 0.7173 0.7330 0.7330 0.7330 0.7382 0.7487 0.7487 0.7592 0.7592 0.7644 0.7749 0.7749 0.7906 0.7906 0.7906 0.7958 0.8010 0.8220 0.8220 0.8220 0.8220 0.8377 0.8377 0.8377 0.8429 0.8534 0.8534 0.8586 0.8743 0.8743 0.8743 0.8901 0.8901 0.8901 0.9162 0.9162 0.9162 0.9162 0.9162 0.9319 0.9319 0.9319 0.9372 0.9529 0.9529 0.9529 0.9686 0.9686 0.9686 0.9791 0.9791 0.9895 0.9895 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.14.8.4 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0714 0.1429 0.3571 0.3571 0.3571 0.4286 0.5000 0.5714 0.6429 0.7857 0.7857 1.0000 1.0000 1.0000 0.0952 0.0952 0.1905 0.1905 0.2857 0.2857 0.3810 0.3810 0.4762 0.4762 0.5238 0.6190 0.6190 0.7619 0.7619 0.7619 1.0000 1.0000 1.0000 1.0000 1.0000 0.1250 0.1250 0.3125 0.3125 0.3125 0.4375 0.4375 0.5000 0.5625 0.6250 0.7500 0.7500 0.9375 0.9375 0.9375 1.0000 0.0952 0.0952 0.1905 0.1905 0.2857 0.2857 0.4762 0.4762 0.4762 0.4762 0.5714 0.5714 0.6190 0.6667 0.8095 0.8095 0.8095 0.8571 1.0000 1.0000 1.0000 0.0526 0.1053 0.1579 0.3684 0.3684 0.3684 0.3684 0.4737 0.4737 0.5263 0.5789 0.7368 0.7368 0.7368 0.9474 0.9474 0.9474 0.9474 1.0000 0.0476 0.1905 0.1905 0.1905 0.2381 0.3333 0.3333 0.4286 0.4286 0.5714 0.5714 0.5714 0.7143 0.7143 0.7143 0.8571 0.8571 0.8571 1.0000 1.0000 1.0000 0.0476 0.1905 0.1905 0.1905 0.2857 0.2857 0.4762 0.4762 0.4762 0.4762 0.5714 0.5714 0.7143 0.7143 0.7143 0.7619 0.8095 0.8571 1.0000 1.0000 1.0000 0.1500 0.1500 0.1500 0.2000 0.3000 0.3000 0.4000 0.4000 0.6000 0.6000 0.6000 0.6000 0.6500 0.7000 0.8000 0.8000 0.9000 0.9000 1.0000 1.0000 0.1875 0.1875 0.1875 0.2500 0.3750 0.3750 0.6250 0.6250 0.6250 0.6250 0.6875 0.8125 0.8125 0.8750 1.0000 1.0000 0.1364 0.1364 0.1364 0.1818 0.3182 0.3182 0.3182 0.4545 0.4545 0.4545 0.5000 0.6818 0.6818 0.6818 0.6818 0.7273 0.7727 0.9091 0.9091 0.9091 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.14.8.5 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.14.8.6 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.14.8.1 { + set myres {} + foreach r [db eval {SELECT ntile(100) OVER ( ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 88.0000 89.0000 89.0000 90.0000 90.0000 91.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.14.8.2 { + set myres {} + foreach r [db eval {SELECT ntile(101) OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 22.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.14.8.3 { + set myres {} + foreach r [db eval {SELECT ntile(102) OVER ( ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 88.0000 89.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.14.8.4 { + set myres {} + foreach r [db eval {SELECT ntile(103) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 22.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.14.8.5 { + set myres {} + foreach r [db eval {SELECT ntile(104) OVER ( ORDER BY b%10,a ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000 103.0000 104.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.14.8.6 { + set myres {} + foreach r [db eval {SELECT ntile(105) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.14.8.7 { + set myres {} + foreach r [db eval {SELECT ntile(105) OVER ( ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 88.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000 103.0000 104.0000 105.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + +do_execsql_test 1.14.9.1 { + SELECT last_value(a+b) OVER ( ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207} + +do_execsql_test 1.14.9.2 { + SELECT last_value(a+b) OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {223 223 223 223 223 223 223 223 223 223 223 223 223 + 223 210 210 210 210 210 210 210 210 210 210 210 210 + 210 210 210 210 210 210 210 210 210 280 280 280 280 + 280 280 280 280 280 280 280 280 280 280 280 280 279 + 279 279 279 279 279 279 279 279 279 279 279 279 279 + 279 279 279 279 279 279 279 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 274 274 274 274 274 274 274 274 274 274 274 274 274 + 274 274 274 274 274 274 274 274 212 212 212 212 212 + 212 212 212 212 212 212 212 212 212 212 212 212 212 + 212 212 212 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 232 232 232 + 232 232 232 232 232 232 232 232 232 232 232 232 232 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229} + +do_execsql_test 1.14.9.3 { + SELECT last_value(a+b) OVER ( ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276} + +do_execsql_test 1.14.9.4 { + SELECT last_value(a+b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {240 240 240 240 240 240 240 240 240 240 240 240 240 + 240 263 263 263 263 263 263 263 263 263 263 263 263 + 263 263 263 263 263 263 263 263 263 280 280 280 280 + 280 280 280 280 280 280 280 280 280 280 280 280 252 + 252 252 252 252 252 252 252 252 252 252 252 252 252 + 252 252 252 252 252 252 252 171 171 171 171 171 171 + 171 171 171 171 171 171 171 171 171 171 171 171 171 + 274 274 274 274 274 274 274 274 274 274 274 274 274 + 274 274 274 274 274 274 274 274 226 226 226 226 226 + 226 226 226 226 226 226 226 226 226 226 226 226 226 + 226 226 226 124 124 124 124 124 124 124 124 124 124 + 124 124 124 124 124 124 124 124 124 124 198 198 198 + 198 198 198 198 198 198 198 198 198 198 198 198 198 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276} + +do_execsql_test 1.14.9.5 { + SELECT last_value(a+b) OVER ( ORDER BY b%10,a ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229} + +do_execsql_test 1.14.9.6 { + SELECT last_value(a+b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM t2 +} {108 52 83 79 65 26 70 103 80 36 116 51 52 128 + 117 71 63 84 109 78 147 88 121 106 124 85 107 171 + 150 80 171 120 109 158 87 168 173 162 156 195 198 + 177 124 121 134 141 210 157 132 161 218 226 191 179 + 138 214 212 172 173 229 240 187 210 227 228 223 225 + 179 182 231 207 209 212 239 234 213 234 269 196 271 + 235 250 223 232 229 280 99 92 72 55 109 120 119 + 50 124 96 59 124 110 57 130 103 74 87 48 105 136 + 131 133 92 109 57 146 113 74 150 87 110 65 110 + 145 161 156 114 111 136 147 173 124 132 101 154 167 + 190 161 110 102 123 169 140 111 180 119 160 197 152 + 146 147 132 213 193 200 136 175 188 187 208 211 144 + 223 196 170 202 163 184 195 200 163 191 252 235 243 + 172 187 202 179 261 263 206 189 276 181 274 249 221 + 210 229 279 224 216 207} + +do_execsql_test 1.14.10.1 { + SELECT nth_value(b,b+1) OVER (ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM t2 +} {96 41 5 16 85 42 43 89 1 22 39 51 73 93 22 80 + 1 73 91 94 35 74 73 37 77 54 54 74 81 91 90 + 62 65 63 36 1 95 23 95 56 16 97 86 40 59 1 84 + 98 56 78 65 96 16 90 81 40 59 56 40 54 2 85 + 96 11 87 41 38 65 32 47 80 74 35 47 98 96 13 + 24 72 73 29 12 46 36 53 35 81 27 56 5 11 81 + 93 63 81 91 68 53 99 89 13 12 97 91 29 7 7 78 + 35 84 53 84 58 61 91 99 15 61 98 16 5 75 56 2 + 37 3 96 62 95 43 63 35 78 16 67 43 16 16 90 + 72 98 85 56 90 46 29 29 4 74 74 2 76 41 46 77 + 24 27 97 46 89 1 85 1 74 78 61 85 51 59 35 30 + 56 25 47 28 73 6 73 74 93 43 3 56 47 85 61 61 + 93 9 97 62} + +do_execsql_test 1.14.10.2 { + SELECT nth_value(b,b+1) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} 30 {} {} + {} {} {} {} 91 {} {} {} 91 {} 11 {} {} {} {} + 11 {} {} {} {} {} 12 {} {} {} 32 {} 12 {} 32 + {} {} 12 {} {} {} {} {} {} {} 43 {} {} {} {} + {} 33 {} {} {} {} {} 33 {} 43 {} {} {} {} {} + {} 4 {} {} {} {} {} {} {} {} 34 {} {} {} {} + {} {} {} {} {} {} 55 {} {} {} {} {} {} 15 55 + {} {} {} {} {} 55 {} {} {} 86 {} 26 26 {} {} + {} {} 26 {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 47 {} {} {} {} {} 47 {} {} {} {} + {} 27 47 {} {} {} 98 {} {} {} {} {} 98 {} 98 + {} {} {} {} {} {} {} {} {} {} {} {} {} 9 {} + {} {} {} {} 99 {} 9 9 {} {} {}} + +do_execsql_test 1.14.10.3 { + SELECT nth_value(b,b+1) OVER ( ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 1 2 2 2 2 3 3 4 5 5 5 6 6 6 7 7 7 7 + 7 7 8 8 8 8 8 8 9 9 9 9 9 9 9 11 11 12 + 12 12 12 12 12 13 13 14 14 15 15 15 15 15 15 + 16 16 16 16 16 16 17 17 17 17 19 19 19 19 20 + 20 21 21 21 21 21 21 22 22 22 22 22 23 23 23 + 24 25 25 26 26 27 27 27 27 27 27 29 29 29 30 + 30 30 31 31 31 31 31 32 33 33 33 33 33 33 33 + 33 33 33 33 34 34 34 34 34 34 34 35 35 36 36 + 36 37 37 37 37 37 37 38 38 38 38 38 38 39 39 + 39 39 39 40 41 41 41 41 41 42 43 43 43 43 43 + 44 44 44 44 46 46 46 47 47 47 47 47 47 47 47 + 47 47 47 49 49 49 50 51 51 51 52 52 52 53 53 + 54 54 55 55} + +do_execsql_test 1.14.10.4 { + SELECT nth_value(b,b+1) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {80 {} {} {} {} {} {} {} {} {} {} {} {} {} 1 1 + 61 61 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 12 12 72 72 72 {} {} {} {} {} {} + {} {} {} {} {} 13 13 63 63 {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} 34 84 {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + 35 85 85 85 {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} 36 76 76 76 {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} 37 37 37 + 87 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} 58 58 58 {} {} {} {} {} {} {} {} {} {} + {} {} {} 39 39 39 89 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.14.10.5 { + SELECT nth_value(b,b+1) OVER ( ORDER BY b%10,a ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {34 2 11 34 81 34 73 33 34 34 11 82 30 11 34 + 65 13 65 65 40 34 72 13 40 41 50 93 72 81 81 + 50 34 65 65 41 73 10 91 61 12 30 54 10 61 30 + 54 54 10 73 23 44 85 1 85 22 80 22 91 93 73 + 84 30 84 84 91 85 1 30 91 80 91 74 43 64 74 + 21 20 85 64 74 21 21 2 74 33 81 21 64 64 2 21 + 93 62 14 14 3 91 11 24 55 93 93 62 90 91 55 3 + 24 14 24 91 55 15 72 60 72 61 61 34 43 43 43 + 61 12 4 15 15 51 51 12 23 12 12 25 41 25 13 + 94 12 70 12 84 32 84 94 70 33 12 12 32 41 91 + 70 22 33 84 80 31 75 84 53 75 80 84 80 53 53 + 53 22 44 63 42 95 31 63 44 44 31 90 74 52 63 + 31 63 1 42 90 90 95 3 42} + +do_execsql_test 1.14.10.6 { + SELECT nth_value(b,b+1) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.14.11.1 { + SELECT first_value(b) OVER (ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM t2 +} {89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89} + +do_execsql_test 1.14.11.2 { + SELECT first_value(b) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM t2 +} {90 90 90 90 90 90 90 90 90 90 90 90 90 90 81 + 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 + 81 81 81 81 81 62 62 62 62 62 62 62 62 62 62 + 62 62 62 62 62 62 93 93 93 93 93 93 93 93 93 + 93 93 93 93 93 93 93 93 93 93 93 93 54 54 54 + 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 + 54 65 65 65 65 65 65 65 65 65 65 65 65 65 65 + 65 65 65 65 65 65 65 96 96 96 96 96 96 96 96 + 96 96 96 96 96 96 96 96 96 96 96 96 96 97 97 + 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 + 97 97 97 38 38 38 38 38 38 38 38 38 38 38 38 + 38 38 38 38 89 89 89 89 89 89 89 89 89 89 89 + 89 89 89 89 89 89 89 89 89 89 89} + +do_execsql_test 1.14.11.3 { + SELECT first_value(b) OVER ( ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1} + +do_execsql_test 1.14.11.4 { + SELECT first_value(b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {10 10 10 10 10 10 10 10 10 10 10 10 10 10 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 + 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 + 5 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 + 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 + 7 7 7 7 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 + 8 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 + 9 9 9 9} + +do_execsql_test 1.14.11.5 { + SELECT first_value(b) OVER ( ORDER BY b%10,a ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 + 90 90 90 90 90 90 90 90 90 90 90} + +do_execsql_test 1.14.11.6 { + SELECT first_value(b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM t2 +} {96 38 68 62 46 6 46 78 54 8 84 16 16 86 74 24 + 12 32 56 22 90 28 56 40 56 16 36 98 76 4 94 + 42 30 78 2 80 84 72 58 96 98 74 12 8 20 22 88 + 34 8 34 90 96 60 44 2 74 70 26 26 80 90 36 58 + 72 72 66 64 12 14 62 36 34 36 58 52 30 50 84 + 10 84 44 58 30 38 34 82 89 81 59 39 91 99 97 + 27 97 67 29 93 77 23 93 65 35 47 7 61 91 85 + 85 43 59 3 91 55 15 89 25 47 1 43 75 89 81 33 + 29 53 63 87 37 41 9 61 73 95 65 13 1 21 65 35 + 5 73 11 51 87 41 31 31 15 95 73 79 11 49 59 + 55 75 77 7 85 57 29 59 19 39 47 47 9 33 93 75 + 81 9 23 37 13 91 91 33 15 99 3 95 69 33 21 39 + 83 27 17 7} + +do_execsql_test 1.14.12.1 { + SELECT lead(b,b) OVER (ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM t2 +} {96 9 11 89 32 53 91 30 51 56 54 73 22 59 75 + 74 78 8 16 65 15 8 31 87 90 12 32 96 74 76 37 + 85 90 15 35 2 60 36 75 9 51 47 63 51 90 26 42 + 26 8 76 80 90 37 87 56 79 5 87 8 2 39 73 64 + 36 90 72 78 36 73 51 33 20 41 2 26 37 33 8 14 + 33 81 55 1 9 12 39 64 87 72 34 82 21 34 99 62 + 74 41 69 22 75 27 58 8 79 77 26 26 55 {} 29 + 30 7 {} 66 55 2 34 64 {} 33 {} 44 84 {} {} 95 + 85 19 {} 83 {} 91 {} {} 9 50 91 33 34 {} {} + 84 {} 7 9 {} {} {} 44 {} {} {} {} 91 84 {} 95 + 95 52 {} {} {} {} {} 21 {} {} {} 58 {} {} {} + {} {} {} {} 83 {} {} {} {} {} {} {} {} {} {} + {} {} {} {}} + +do_execsql_test 1.14.12.2 { + SELECT lead(b,b) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 81 {} {} {} 21 {} {} {} {} {} {} + {} {} {} {} {} {} 62 {} {} {} 12 {} {} {} 72 + {} {} {} {} {} {} {} {} {} {} 53 {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 34 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} 95 {} {} {} {} {} {} 85 {} + {} {} {} {} {} {} {} {} {} 56 {} 36 {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 57 {} {} {} {} {} 7 {} {} {} {} + {} {} {} {} {} {} 8 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 9 {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.14.12.3 { + SELECT lead(b,b) OVER ( ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 3 5 6 7 8 8 9 9 10 11 12 12 13 13 + 14 15 16 16 17 19 20 21 22 23 24 25 26 27 27 + 28 29 30 31 32 33 33 33 34 34 35 36 36 36 37 + 38 39 39 40 41 42 43 43 44 46 47 47 47 49 50 + 52 53 54 55 56 56 57 58 58 58 59 59 59 60 61 + 62 62 64 65 65 67 69 70 72 72 73 74 74 75 75 + 75 77 78 80 81 81 83 84 84 85 85 85 87 88 89 + 89 89 90 90 91 91 91 93 93 94 95 95 96 97 97 + 98 99 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.14.12.4 { + SELECT lead(b,b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {80 {} {} {} {} {} {} {} {} {} {} {} {} {} 1 + 11 81 81 {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} 12 12 72 82 {} {} {} {} {} {} + {} {} {} {} {} {} 13 23 73 73 {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} 34 84 {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 35 85 85 95 {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} 36 86 96 96 {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 37 47 + 47 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} 58 58 68 {} {} {} {} {} {} {} {} {} + {} {} {} {} 39 49 59 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.14.12.5 { + SELECT lead(b,b) OVER ( ORDER BY b%10,a ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {34 72 91 74 21 55 23 94 84 95 2 13 61 22 55 + 55 94 85 75 81 65 73 74 21 72 12 94 33 63 73 + 72 96 36 76 3 25 62 3 73 34 12 46 43 93 72 16 + 86 63 15 65 36 77 24 57 25 53 95 34 95 16 97 + 74 97 67 25 98 44 34 65 54 5 68 96 28 47 95 + 34 39 8 38 6 46 96 28 47 95 56 39 99 97 76 8 + 26 9 79 27 95 16 29 {} 58 58 77 85 56 {} 98 + 29 {} 19 96 {} {} 78 56 98 36 97 {} 89 89 29 + 47 78 {} {} {} 38 68 58 {} 58 38 {} 98 {} {} + {} 39 57 9 {} 79 {} {} 7 {} {} {} 9 29 38 78 + {} {} {} 8 39 {} {} {} {} 59 {} 99 {} {} {} + {} {} {} {} {} {} {} {} {} {} 9 {} {} {} {} + {} {} {} {} {} {} {} {}} + +do_execsql_test 1.14.12.6 { + SELECT lead(b,b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.14.13.1 { + SELECT lag(b,b) OVER (ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} 38 {} {} {} {} + {} {} {} 6 {} {} {} {} {} 81 46 6 {} {} {} {} + 23 {} {} {} {} {} 27 {} {} {} 35 6 {} 12 {} + 23 {} {} 61 84 {} 93 39 47 {} 54 46 96 56 {} + 16 {} {} {} {} 89 {} 16 43 {} 85 56 29 99 53 + {} 59 {} {} 91 59 53 84 99 {} 93 63 47 {} {} + 98 33 67 35 75 1 23 13 55 27 75 98 35 73 63 2 + 21 27 13 24 86 23 84 31 20 94 61 65 75 23 36 + 94 55 90 41 77 96 56 29 40 12 89 63 11 5 73 + 79 1 16 28 31 73 5 39 53 63 41 11 40 2 13 33 + 9 29 90 47 72 9 73 30 44 33 74 93 29 74 42 34 + 63 41 34 96 47 77 1 36 74 72 14 36 26 77 9 72 + 64 8 91 31 52 30} + +do_execsql_test 1.14.13.2 { + SELECT lag(b,b) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} 30 {} {} + {} {} {} {} 91 {} {} {} 61 {} 81 {} {} {} {} + 1 {} {} {} {} {} {} {} {} {} 22 {} {} {} 12 + {} {} 62 {} {} {} {} {} {} {} 23 {} {} {} {} + {} {} {} {} {} {} {} 43 {} 23 {} {} {} {} {} + {} 54 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 75 {} + {} {} {} {} {} 55 {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} 47 {} {} {} {} + {} 27 7 {} {} {} {} {} {} {} {} {} 68 {} 8 {} + {} {} {} {} {} {} {} {} {} {} {} {} 89 {} {} + {} {} {} {} {} 29 9 {} {} {}} + +do_execsql_test 1.14.13.3 { + SELECT lag(b,b) OVER ( ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {{} 1 1 1 1 2 2 2 2 2 2 3 3 3 4 4 5 6 6 + 6 7 7 7 7 7 8 8 8 8 8 8 9 9 9 9 9 9 9 + 9 9 9 10 10 10 10 11 11 11 11 11 12 12 12 12 + 13 13 13 13 13 14 15 15 15 15 16 16 16 16 16 + 17 19 20 20 21 21 21 21 22 22 22 22 23 23 23 + 23 23 24 23 24 24 25 26 26 26 26 26 26 26 26 + 26 26 26 27 27 27 27 28 29 29 29 29 30 30 30 + 30 30 30 31 31 31 31 31 32 32 32 32 32 32 31 + 32 33 33 33 33 33 33 34 34 34 34 34 34 34 34 + 35 35 35 35 35 36 36 36 36 36 36 36 37 37 37 + 38 38 38 38 38 38 39 39 39 39 40 40 41 41 42 + 43 42 43 43 43 43 44 44 44 46 46 46 47 47 47 + 47 47} + +do_execsql_test 1.14.13.4 { + SELECT lag(b,b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + 1 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.14.13.5 { + SELECT lag(b,b) OVER ( ORDER BY b%10,a ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} 30 {} {} + {} {} {} {} 91 {} {} {} 61 80 81 {} {} {} {} + 1 {} {} {} 30 {} 21 90 61 {} 22 {} 11 41 12 + {} {} 62 {} {} {} {} 31 {} 50 23 30 21 90 {} + {} 62 {} {} 81 {} 22 43 62 23 32 {} 91 {} 90 + 93 54 {} {} 90 72 12 22 90 81 83 23 80 20 72 + 43 51 33 80 90 2 34 54 1 20 62 12 13 75 44 30 + 93 91 1 21 55 61 61 13 85 3 65 65 91 73 33 93 + 55 84 62 31 11 65 35 85 33 55 15 12 75 22 3 + 73 65 36 85 43 95 43 13 47 44 65 65 96 36 27 + 7 46 34 94 47 36 73 34 35 73 68 24 8 75 85 75 + 66 34 95 36 84 77 46 34 84 47 89 65 36 16 38 + 76 58 57 29 9 44 56 17} + +do_execsql_test 1.14.13.6 { + SELECT lag(b,b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.14.14.1 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM t2 +} {89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7} + +do_execsql_test 1.14.14.2 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM t2 +} {90.40.30.80.20.90.60.70.80.90.30.50.10.30 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39} + +do_execsql_test 1.14.14.3 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99} + +do_execsql_test 1.14.14.4 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {10.20.30.30.30.40.50.60.70.80.80.90.90.90 + 10.20.30.30.30.40.50.60.70.80.80.90.90.90 + 10.20.30.30.30.40.50.60.70.80.80.90.90.90 + 10.20.30.30.30.40.50.60.70.80.80.90.90.90 + 10.20.30.30.30.40.50.60.70.80.80.90.90.90 + 10.20.30.30.30.40.50.60.70.80.80.90.90.90 + 10.20.30.30.30.40.50.60.70.80.80.90.90.90 + 10.20.30.30.30.40.50.60.70.80.80.90.90.90 + 10.20.30.30.30.40.50.60.70.80.80.90.90.90 + 10.20.30.30.30.40.50.60.70.80.80.90.90.90 + 10.20.30.30.30.40.50.60.70.80.80.90.90.90 + 10.20.30.30.30.40.50.60.70.80.80.90.90.90 + 10.20.30.30.30.40.50.60.70.80.80.90.90.90 + 10.20.30.30.30.40.50.60.70.80.80.90.90.90 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99} + +do_execsql_test 1.14.14.5 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( ORDER BY b%10,a ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39} + +do_execsql_test 1.14.14.6 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM t2 +} {96 38 68 62 46 6 46 78 54 8 84 16 16 86 74 24 + 12 32 56 22 90 28 56 40 56 16 36 98 76 4 94 + 42 30 78 2 80 84 72 58 96 98 74 12 8 20 22 88 + 34 8 34 90 96 60 44 2 74 70 26 26 80 90 36 58 + 72 72 66 64 12 14 62 36 34 36 58 52 30 50 84 + 10 84 44 58 30 38 34 82 89 81 59 39 91 99 97 + 27 97 67 29 93 77 23 93 65 35 47 7 61 91 85 + 85 43 59 3 91 55 15 89 25 47 1 43 75 89 81 33 + 29 53 63 87 37 41 9 61 73 95 65 13 1 21 65 35 + 5 73 11 51 87 41 31 31 15 95 73 79 11 49 59 + 55 75 77 7 85 57 29 59 19 39 47 47 9 33 93 75 + 81 9 23 37 13 91 91 33 15 99 3 95 69 33 21 39 + 83 27 17 7} + +do_execsql_test 1.14.14.7 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (win1 ORDER BY b%10 ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a) + ORDER BY 1 +} {1 1 10 11 11 12 12 12 13 13 14 15 15 15 16 16 + 16 17 19 2 2 20 21 21 22 22 23 23 24 25 26 26 + 27 27 28 29 29 29 3 3 30 30 30 31 31 32 33 33 + 33 33 34 34 34 34 35 35 36 36 36 36 37 37 38 + 38 39 39 39 4 40 41 41 42 43 43 44 44 46 46 + 47 47 47 47 49 5 50 51 52 53 54 55 55 56 56 + 56 57 58 58 58 58 59 59 59 59 6 60 61 61 62 + 62 63 64 65 65 65 66 67 68 69 7 7 7 70 72 72 + 72 73 73 73 74 74 74 75 75 75 76 77 77 78 78 + 79 8 8 8 80 80 81 81 81 82 83 84 84 84 84 85 + 85 85 86 87 87 88 89 89 89 9 9 9 90 90 90 91 + 91 91 91 91 93 93 93 94 95 95 95 96 96 96 97 + 97 98 98 99 99} + +do_execsql_test 1.14.14.8 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (win1 ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a ORDER BY b%10) + ORDER BY 1 +} {1 1 10 11 11 12 12 12 13 13 14 15 15 15 16 16 + 16 17 19 2 2 20 21 21 22 22 23 23 24 25 26 26 + 27 27 28 29 29 29 3 3 30 30 30 31 31 32 33 33 + 33 33 34 34 34 34 35 35 36 36 36 36 37 37 38 + 38 39 39 39 4 40 41 41 42 43 43 44 44 46 46 + 47 47 47 47 49 5 50 51 52 53 54 55 55 56 56 + 56 57 58 58 58 58 59 59 59 59 6 60 61 61 62 + 62 63 64 65 65 65 66 67 68 69 7 7 7 70 72 72 + 72 73 73 73 74 74 74 75 75 75 76 77 77 78 78 + 79 8 8 8 80 80 81 81 81 82 83 84 84 84 84 85 + 85 85 86 87 87 88 89 89 89 9 9 9 90 90 90 91 + 91 91 91 91 93 93 93 94 95 95 95 96 96 96 97 + 97 98 98 99 99} + +do_execsql_test 1.14.14.9 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER win2 + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a ORDER BY b%10), + win2 AS (win1 ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) + ORDER BY 1 +} {1 1 10 11 11 12 12 12 13 13 14 15 15 15 16 16 + 16 17 19 2 2 20 21 21 22 22 23 23 24 25 26 26 + 27 27 28 29 29 29 3 3 30 30 30 31 31 32 33 33 + 33 33 34 34 34 34 35 35 36 36 36 36 37 37 38 + 38 39 39 39 4 40 41 41 42 43 43 44 44 46 46 + 47 47 47 47 49 5 50 51 52 53 54 55 55 56 56 + 56 57 58 58 58 58 59 59 59 59 6 60 61 61 62 + 62 63 64 65 65 65 66 67 68 69 7 7 7 70 72 72 + 72 73 73 73 74 74 74 75 75 75 76 77 77 78 78 + 79 8 8 8 80 80 81 81 81 82 83 84 84 84 84 85 + 85 85 86 87 87 88 89 89 89 9 9 9 90 90 90 91 + 91 91 91 91 93 93 93 94 95 95 95 96 96 96 97 + 97 98 98 99 99} + +do_execsql_test 1.14.15.1 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE a%2=0) OVER win FROM t2 + WINDOW win AS (ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) +} {191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7} + +do_execsql_test 1.14.15.2 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE 0=1) OVER win FROM t2 + WINDOW win AS (ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) +} {191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} + 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} + 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} + 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} + 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} + 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} + 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} + 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} + 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} + 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} + 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} + 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} + 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} + 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} + 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} + 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} + 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} + 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} + 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} + 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} + 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} + 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} + 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} + 191 {} 191 {} 191 {} 191 {} 191 {} 191 {} 191 {}} + +do_execsql_test 1.14.15.3 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE 1=0) OVER win FROM t2 + WINDOW win AS (PARTITION BY (a%10) ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) +} {20 {} 20 {} 20 {} 20 {} 20 {} 20 {} 20 {} 20 {} 20 {} + 20 {} 20 {} 20 {} 20 {} 20 {} 20 {} 20 {} 20 {} 20 {} + 20 {} 20 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 19 {}} + +do_execsql_test 1.14.15.4 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE a%2=0) OVER win FROM t2 + WINDOW win AS (PARTITION BY (a%10) ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) +} {20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} + 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {} 19 {}} + +do_execsql_test 1.15.2.1 { + SELECT max(b) OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 95 95 84 84 84 84 84 84 + 84 84 83 83 83 83 83 83 83 83 83} + +do_execsql_test 1.15.2.2 { + SELECT min(b) OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 + 3 3 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 + 7} + +do_execsql_test 1.15.3.1 { + SELECT row_number() OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.15.3.2 { + SELECT row_number() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.15.3.3 { + SELECT row_number() OVER ( ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.15.4.1 { + SELECT dense_rank() OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.15.4.2 { + SELECT dense_rank() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.15.4.3 { + SELECT dense_rank() OVER ( ORDER BY b ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 1 2 2 3 3 4 5 6 7 7 7 8 8 8 9 9 9 10 + 11 11 12 12 12 13 13 14 15 15 15 16 16 16 17 + 18 19 20 20 21 21 22 22 23 24 25 25 26 26 27 + 28 28 28 29 29 29 30 30 31 32 32 32 32 33 33 + 33 33 34 34 35 35 35 35 36 36 37 37 38 38 38 + 39 40 40 41 42 42 43 43 44 44 45 45 45 45 46 + 47 48 49 50 51 52 52 53 53 53 54 55 55 55 55 + 56 56 56 56 57 58 58 59 59 60 61 62 62 62 63 + 64 65 66 67 68 68 68 69 69 69 70 70 70 71 71 + 71 72 73 73 74 74 75 76 76 77 77 77 78 79 80 + 80 80 80 81 81 81 82 83 83 84 85 85 85 86 86 + 86 87 87 87 87 87 88 88 88 89 90 90 90 91 91 + 91 92 92 93 93 94 94} + +do_execsql_test 1.15.4.4 { + SELECT dense_rank() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 3 3 4 5 6 7 8 8 9 9 9 1 1 2 2 3 + 3 4 4 5 5 6 7 7 8 8 8 9 9 9 9 9 1 1 2 + 2 2 3 3 4 5 6 7 7 8 8 8 9 1 1 2 2 3 3 + 4 4 4 4 5 5 6 7 8 8 8 9 10 10 10 1 2 3 + 4 4 4 4 5 5 6 7 8 8 8 9 9 9 9 10 1 2 2 + 2 3 4 4 5 5 6 6 6 7 7 7 8 8 8 9 9 9 1 + 2 2 2 3 3 4 4 4 4 5 5 6 6 6 7 8 9 10 10 + 10 1 1 1 2 3 3 4 4 5 5 5 5 6 7 8 8 9 9 + 10 10 1 1 1 2 3 3 4 4 4 4 5 6 6 7 8 8 1 + 1 1 2 3 3 3 4 4 4 5 6 6 6 6 7 8 9 9 9 + 10 10} + +do_execsql_test 1.15.4.5 { + SELECT dense_rank() OVER ( ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 + 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 + 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 + 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 + 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 + 8 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 10 10 + 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 + 10 10 10 10 10} + +do_execsql_test 1.15.4.6 { + SELECT dense_rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 + 5 5 5 5 5 5 5 5 5 5 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 + 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 + 5} + +do_execsql_test 1.15.5.1 { + SELECT rank() OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.15.5.2 { + SELECT rank() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.15.5.3 { + SELECT rank() OVER ( ORDER BY b ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 1 3 3 5 5 7 8 9 10 10 10 13 13 13 16 16 + 16 19 20 20 22 22 22 25 25 27 28 28 28 31 31 + 31 34 35 36 37 37 39 39 41 41 43 44 45 45 47 + 47 49 50 50 50 53 53 53 56 56 58 59 59 59 59 + 63 63 63 63 67 67 69 69 69 69 73 73 75 75 77 + 77 77 80 81 81 83 84 84 86 86 88 88 90 90 90 + 90 94 95 96 97 98 99 100 100 102 102 102 105 106 + 106 106 106 110 110 110 110 114 115 115 117 117 119 + 120 121 121 121 124 125 126 127 128 129 129 129 132 + 132 132 135 135 135 138 138 138 141 142 142 144 144 + 146 147 147 149 149 149 152 153 154 154 154 154 158 + 158 158 161 162 162 164 165 165 165 168 168 168 171 + 171 171 171 171 176 176 176 179 180 180 180 183 183 + 183 186 186 188 188 190 190} + +do_execsql_test 1.15.5.4 { + SELECT rank() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 3 3 6 7 8 9 10 10 12 12 12 1 1 3 3 + 5 5 7 7 9 9 11 12 12 14 14 14 17 17 17 17 + 17 1 1 3 3 3 6 6 8 9 10 11 11 13 13 13 16 + 1 1 3 3 5 5 7 7 7 7 11 11 13 14 15 15 15 + 18 19 19 19 1 2 3 4 4 4 4 8 8 10 11 12 12 + 12 15 15 15 15 19 1 2 2 2 5 6 6 8 8 10 10 + 10 13 13 13 16 16 16 19 19 19 1 2 2 2 5 5 7 + 7 7 7 11 11 13 13 13 16 17 18 19 19 19 1 1 + 1 4 5 5 7 7 9 9 9 9 13 14 15 15 17 17 19 + 19 1 1 1 4 5 5 7 7 7 7 11 12 12 14 15 15 + 1 1 1 4 5 5 5 8 8 8 11 12 12 12 12 16 17 + 18 18 18 21 21} + +do_execsql_test 1.15.5.5 { + SELECT rank() OVER ( ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 15 15 15 15 + 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 + 15 15 36 36 36 36 36 36 36 36 36 36 36 36 36 + 36 36 36 52 52 52 52 52 52 52 52 52 52 52 52 + 52 52 52 52 52 52 52 52 52 73 73 73 73 73 73 + 73 73 73 73 73 73 73 73 73 73 73 73 73 92 92 + 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 + 92 92 92 92 113 113 113 113 113 113 113 113 113 + 113 113 113 113 113 113 113 113 113 113 113 113 134 + 134 134 134 134 134 134 134 134 134 134 134 134 134 + 134 134 134 134 134 134 154 154 154 154 154 154 154 + 154 154 154 154 154 154 154 154 154 170 170 170 170 + 170 170 170 170 170 170 170 170 170 170 170 170 170 + 170 170 170 170 170} + +do_execsql_test 1.15.5.6 { + SELECT rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 15 15 15 15 + 15 15 15 15 15 15 15 15 15 15 15 15 31 31 31 + 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 + 31 50 50 50 50 50 50 50 50 50 50 50 50 50 50 + 50 50 50 50 50 50 50 71 71 71 71 71 71 71 71 + 71 71 71 71 71 71 71 71 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 22 22 22 22 22 22 + 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 + 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 + 43 43 43 43 43 43 64 64 64 64 64 64 64 64 64 + 64 64 64 64 64 64 64 64 64 64 64 84 84 84 84 + 84 84 84 84 84 84 84 84 84 84 84 84 84 84 84 + 84 84 84} + +do_execsql_test 1.15.6.1 { + SELECT + row_number() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ), + rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ), + dense_rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) + FROM t2 +} {1 1 1 2 1 1 3 1 1 4 1 1 5 1 1 6 1 1 7 1 1 8 1 1 9 1 1 + 10 1 1 11 1 1 12 1 1 13 1 1 14 1 1 15 15 2 16 15 2 17 15 2 + 18 15 2 19 15 2 20 15 2 21 15 2 22 15 2 23 15 2 24 15 2 + 25 15 2 26 15 2 27 15 2 28 15 2 29 15 2 30 15 2 31 31 3 + 32 31 3 33 31 3 34 31 3 35 31 3 36 31 3 37 31 3 38 31 3 + 39 31 3 40 31 3 41 31 3 42 31 3 43 31 3 44 31 3 45 31 3 + 46 31 3 47 31 3 48 31 3 49 31 3 50 50 4 51 50 4 52 50 4 + 53 50 4 54 50 4 55 50 4 56 50 4 57 50 4 58 50 4 59 50 4 + 60 50 4 61 50 4 62 50 4 63 50 4 64 50 4 65 50 4 66 50 4 + 67 50 4 68 50 4 69 50 4 70 50 4 71 71 5 72 71 5 73 71 5 + 74 71 5 75 71 5 76 71 5 77 71 5 78 71 5 79 71 5 80 71 5 + 81 71 5 82 71 5 83 71 5 84 71 5 85 71 5 86 71 5 1 1 1 2 1 1 + 3 1 1 4 1 1 5 1 1 6 1 1 7 1 1 8 1 1 9 1 1 10 1 1 11 1 1 + 12 1 1 13 1 1 14 1 1 15 1 1 16 1 1 17 1 1 18 1 1 19 1 1 + 20 1 1 21 1 1 22 22 2 23 22 2 24 22 2 25 22 2 26 22 2 27 22 2 + 28 22 2 29 22 2 30 22 2 31 22 2 32 22 2 33 22 2 34 22 2 + 35 22 2 36 22 2 37 22 2 38 22 2 39 22 2 40 22 2 41 22 2 + 42 22 2 43 43 3 44 43 3 45 43 3 46 43 3 47 43 3 48 43 3 + 49 43 3 50 43 3 51 43 3 52 43 3 53 43 3 54 43 3 55 43 3 + 56 43 3 57 43 3 58 43 3 59 43 3 60 43 3 61 43 3 62 43 3 + 63 43 3 64 64 4 65 64 4 66 64 4 67 64 4 68 64 4 69 64 4 + 70 64 4 71 64 4 72 64 4 73 64 4 74 64 4 75 64 4 76 64 4 + 77 64 4 78 64 4 79 64 4 80 64 4 81 64 4 82 64 4 83 64 4 + 84 84 5 85 84 5 86 84 5 87 84 5 88 84 5 89 84 5 90 84 5 + 91 84 5 92 84 5 93 84 5 94 84 5 95 84 5 96 84 5 97 84 5 + 98 84 5 99 84 5 100 84 5 101 84 5 102 84 5 103 84 5 104 84 5 + 105 84 5} + + +do_test 1.15.7.1 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0053 0.0105 0.0158 0.0211 0.0263 0.0316 0.0368 0.0421 0.0474 0.0526 0.0579 0.0632 0.0684 0.0737 0.0789 0.0842 0.0895 0.0947 0.1000 0.1053 0.1105 0.1158 0.1211 0.1263 0.1316 0.1368 0.1421 0.1474 0.1526 0.1579 0.1632 0.1684 0.1737 0.1789 0.1842 0.1895 0.1947 0.2000 0.2053 0.2105 0.2158 0.2211 0.2263 0.2316 0.2368 0.2421 0.2474 0.2526 0.2579 0.2632 0.2684 0.2737 0.2789 0.2842 0.2895 0.2947 0.3000 0.3053 0.3105 0.3158 0.3211 0.3263 0.3316 0.3368 0.3421 0.3474 0.3526 0.3579 0.3632 0.3684 0.3737 0.3789 0.3842 0.3895 0.3947 0.4000 0.4053 0.4105 0.4158 0.4211 0.4263 0.4316 0.4368 0.4421 0.4474 0.4526 0.4579 0.4632 0.4684 0.4737 0.4789 0.4842 0.4895 0.4947 0.5000 0.5053 0.5105 0.5158 0.5211 0.5263 0.5316 0.5368 0.5421 0.5474 0.5526 0.5579 0.5632 0.5684 0.5737 0.5789 0.5842 0.5895 0.5947 0.6000 0.6053 0.6105 0.6158 0.6211 0.6263 0.6316 0.6368 0.6421 0.6474 0.6526 0.6579 0.6632 0.6684 0.6737 0.6789 0.6842 0.6895 0.6947 0.7000 0.7053 0.7105 0.7158 0.7211 0.7263 0.7316 0.7368 0.7421 0.7474 0.7526 0.7579 0.7632 0.7684 0.7737 0.7789 0.7842 0.7895 0.7947 0.8000 0.8053 0.8105 0.8158 0.8211 0.8263 0.8316 0.8368 0.8421 0.8474 0.8526 0.8579 0.8632 0.8684 0.8737 0.8789 0.8842 0.8895 0.8947 0.9000 0.9053 0.9105 0.9158 0.9211 0.9263 0.9316 0.9368 0.9421 0.9474 0.9526 0.9579 0.9632 0.9684 0.9737 0.9789 0.9842 0.9895 0.9947 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.15.7.2 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0769 0.1538 0.2308 0.3077 0.3846 0.4615 0.5385 0.6154 0.6923 0.7692 0.8462 0.9231 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0667 0.1333 0.2000 0.2667 0.3333 0.4000 0.4667 0.5333 0.6000 0.6667 0.7333 0.8000 0.8667 0.9333 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0556 0.1111 0.1667 0.2222 0.2778 0.3333 0.3889 0.4444 0.5000 0.5556 0.6111 0.6667 0.7222 0.7778 0.8333 0.8889 0.9444 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0526 0.1053 0.1579 0.2105 0.2632 0.3158 0.3684 0.4211 0.4737 0.5263 0.5789 0.6316 0.6842 0.7368 0.7895 0.8421 0.8947 0.9474 1.0000 0.0000 0.0667 0.1333 0.2000 0.2667 0.3333 0.4000 0.4667 0.5333 0.6000 0.6667 0.7333 0.8000 0.8667 0.9333 1.0000 0.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.15.7.3 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY b ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0105 0.0105 0.0211 0.0211 0.0316 0.0368 0.0421 0.0474 0.0474 0.0474 0.0632 0.0632 0.0632 0.0789 0.0789 0.0789 0.0947 0.1000 0.1000 0.1105 0.1105 0.1105 0.1263 0.1263 0.1368 0.1421 0.1421 0.1421 0.1579 0.1579 0.1579 0.1737 0.1789 0.1842 0.1895 0.1895 0.2000 0.2000 0.2105 0.2105 0.2211 0.2263 0.2316 0.2316 0.2421 0.2421 0.2526 0.2579 0.2579 0.2579 0.2737 0.2737 0.2737 0.2895 0.2895 0.3000 0.3053 0.3053 0.3053 0.3053 0.3263 0.3263 0.3263 0.3263 0.3474 0.3474 0.3579 0.3579 0.3579 0.3579 0.3789 0.3789 0.3895 0.3895 0.4000 0.4000 0.4000 0.4158 0.4211 0.4211 0.4316 0.4368 0.4368 0.4474 0.4474 0.4579 0.4579 0.4684 0.4684 0.4684 0.4684 0.4895 0.4947 0.5000 0.5053 0.5105 0.5158 0.5211 0.5211 0.5316 0.5316 0.5316 0.5474 0.5526 0.5526 0.5526 0.5526 0.5737 0.5737 0.5737 0.5737 0.5947 0.6000 0.6000 0.6105 0.6105 0.6211 0.6263 0.6316 0.6316 0.6316 0.6474 0.6526 0.6579 0.6632 0.6684 0.6737 0.6737 0.6737 0.6895 0.6895 0.6895 0.7053 0.7053 0.7053 0.7211 0.7211 0.7211 0.7368 0.7421 0.7421 0.7526 0.7526 0.7632 0.7684 0.7684 0.7789 0.7789 0.7789 0.7947 0.8000 0.8053 0.8053 0.8053 0.8053 0.8263 0.8263 0.8263 0.8421 0.8474 0.8474 0.8579 0.8632 0.8632 0.8632 0.8789 0.8789 0.8789 0.8947 0.8947 0.8947 0.8947 0.8947 0.9211 0.9211 0.9211 0.9368 0.9421 0.9421 0.9421 0.9579 0.9579 0.9579 0.9737 0.9737 0.9842 0.9842 0.9947 0.9947} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.15.7.4 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0769 0.1538 0.1538 0.1538 0.3846 0.4615 0.5385 0.6154 0.6923 0.6923 0.8462 0.8462 0.8462 0.0000 0.0000 0.1000 0.1000 0.2000 0.2000 0.3000 0.3000 0.4000 0.4000 0.5000 0.5500 0.5500 0.6500 0.6500 0.6500 0.8000 0.8000 0.8000 0.8000 0.8000 0.0000 0.0000 0.1333 0.1333 0.1333 0.3333 0.3333 0.4667 0.5333 0.6000 0.6667 0.6667 0.8000 0.8000 0.8000 1.0000 0.0000 0.0000 0.1000 0.1000 0.2000 0.2000 0.3000 0.3000 0.3000 0.3000 0.5000 0.5000 0.6000 0.6500 0.7000 0.7000 0.7000 0.8500 0.9000 0.9000 0.9000 0.0000 0.0556 0.1111 0.1667 0.1667 0.1667 0.1667 0.3889 0.3889 0.5000 0.5556 0.6111 0.6111 0.6111 0.7778 0.7778 0.7778 0.7778 1.0000 0.0000 0.0500 0.0500 0.0500 0.2000 0.2500 0.2500 0.3500 0.3500 0.4500 0.4500 0.4500 0.6000 0.6000 0.6000 0.7500 0.7500 0.7500 0.9000 0.9000 0.9000 0.0000 0.0500 0.0500 0.0500 0.2000 0.2000 0.3000 0.3000 0.3000 0.3000 0.5000 0.5000 0.6000 0.6000 0.6000 0.7500 0.8000 0.8500 0.9000 0.9000 0.9000 0.0000 0.0000 0.0000 0.1579 0.2105 0.2105 0.3158 0.3158 0.4211 0.4211 0.4211 0.4211 0.6316 0.6842 0.7368 0.7368 0.8421 0.8421 0.9474 0.9474 0.0000 0.0000 0.0000 0.2000 0.2667 0.2667 0.4000 0.4000 0.4000 0.4000 0.6667 0.7333 0.7333 0.8667 0.9333 0.9333 0.0000 0.0000 0.0000 0.1429 0.1905 0.1905 0.1905 0.3333 0.3333 0.3333 0.4762 0.5238 0.5238 0.5238 0.5238 0.7143 0.7619 0.8095 0.8095 0.8095 0.9524 0.9524} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.15.7.5 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.15.7.6 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER (PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.15.8.1 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0052 0.0105 0.0157 0.0209 0.0262 0.0314 0.0366 0.0419 0.0471 0.0524 0.0576 0.0628 0.0681 0.0733 0.0785 0.0838 0.0890 0.0942 0.0995 0.1047 0.1099 0.1152 0.1204 0.1257 0.1309 0.1361 0.1414 0.1466 0.1518 0.1571 0.1623 0.1675 0.1728 0.1780 0.1832 0.1885 0.1937 0.1990 0.2042 0.2094 0.2147 0.2199 0.2251 0.2304 0.2356 0.2408 0.2461 0.2513 0.2565 0.2618 0.2670 0.2723 0.2775 0.2827 0.2880 0.2932 0.2984 0.3037 0.3089 0.3141 0.3194 0.3246 0.3298 0.3351 0.3403 0.3455 0.3508 0.3560 0.3613 0.3665 0.3717 0.3770 0.3822 0.3874 0.3927 0.3979 0.4031 0.4084 0.4136 0.4188 0.4241 0.4293 0.4346 0.4398 0.4450 0.4503 0.4555 0.4607 0.4660 0.4712 0.4764 0.4817 0.4869 0.4921 0.4974 0.5026 0.5079 0.5131 0.5183 0.5236 0.5288 0.5340 0.5393 0.5445 0.5497 0.5550 0.5602 0.5654 0.5707 0.5759 0.5812 0.5864 0.5916 0.5969 0.6021 0.6073 0.6126 0.6178 0.6230 0.6283 0.6335 0.6387 0.6440 0.6492 0.6545 0.6597 0.6649 0.6702 0.6754 0.6806 0.6859 0.6911 0.6963 0.7016 0.7068 0.7120 0.7173 0.7225 0.7277 0.7330 0.7382 0.7435 0.7487 0.7539 0.7592 0.7644 0.7696 0.7749 0.7801 0.7853 0.7906 0.7958 0.8010 0.8063 0.8115 0.8168 0.8220 0.8272 0.8325 0.8377 0.8429 0.8482 0.8534 0.8586 0.8639 0.8691 0.8743 0.8796 0.8848 0.8901 0.8953 0.9005 0.9058 0.9110 0.9162 0.9215 0.9267 0.9319 0.9372 0.9424 0.9476 0.9529 0.9581 0.9634 0.9686 0.9738 0.9791 0.9843 0.9895 0.9948 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.15.8.2 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0714 0.1429 0.2143 0.2857 0.3571 0.4286 0.5000 0.5714 0.6429 0.7143 0.7857 0.8571 0.9286 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0625 0.1250 0.1875 0.2500 0.3125 0.3750 0.4375 0.5000 0.5625 0.6250 0.6875 0.7500 0.8125 0.8750 0.9375 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0526 0.1053 0.1579 0.2105 0.2632 0.3158 0.3684 0.4211 0.4737 0.5263 0.5789 0.6316 0.6842 0.7368 0.7895 0.8421 0.8947 0.9474 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0625 0.1250 0.1875 0.2500 0.3125 0.3750 0.4375 0.5000 0.5625 0.6250 0.6875 0.7500 0.8125 0.8750 0.9375 1.0000 0.0455 0.0909 0.1364 0.1818 0.2273 0.2727 0.3182 0.3636 0.4091 0.4545 0.5000 0.5455 0.5909 0.6364 0.6818 0.7273 0.7727 0.8182 0.8636 0.9091 0.9545 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.15.8.3 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY b ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0105 0.0105 0.0209 0.0209 0.0314 0.0314 0.0366 0.0419 0.0471 0.0628 0.0628 0.0628 0.0785 0.0785 0.0785 0.0942 0.0942 0.0942 0.0995 0.1099 0.1099 0.1257 0.1257 0.1257 0.1361 0.1361 0.1414 0.1571 0.1571 0.1571 0.1728 0.1728 0.1728 0.1780 0.1832 0.1885 0.1990 0.1990 0.2094 0.2094 0.2199 0.2199 0.2251 0.2304 0.2408 0.2408 0.2513 0.2513 0.2565 0.2723 0.2723 0.2723 0.2880 0.2880 0.2880 0.2984 0.2984 0.3037 0.3246 0.3246 0.3246 0.3246 0.3455 0.3455 0.3455 0.3455 0.3560 0.3560 0.3770 0.3770 0.3770 0.3770 0.3874 0.3874 0.3979 0.3979 0.4136 0.4136 0.4136 0.4188 0.4293 0.4293 0.4346 0.4450 0.4450 0.4555 0.4555 0.4660 0.4660 0.4869 0.4869 0.4869 0.4869 0.4921 0.4974 0.5026 0.5079 0.5131 0.5183 0.5288 0.5288 0.5445 0.5445 0.5445 0.5497 0.5707 0.5707 0.5707 0.5707 0.5916 0.5916 0.5916 0.5916 0.5969 0.6073 0.6073 0.6178 0.6178 0.6230 0.6283 0.6440 0.6440 0.6440 0.6492 0.6545 0.6597 0.6649 0.6702 0.6859 0.6859 0.6859 0.7016 0.7016 0.7016 0.7173 0.7173 0.7173 0.7330 0.7330 0.7330 0.7382 0.7487 0.7487 0.7592 0.7592 0.7644 0.7749 0.7749 0.7906 0.7906 0.7906 0.7958 0.8010 0.8220 0.8220 0.8220 0.8220 0.8377 0.8377 0.8377 0.8429 0.8534 0.8534 0.8586 0.8743 0.8743 0.8743 0.8901 0.8901 0.8901 0.9162 0.9162 0.9162 0.9162 0.9162 0.9319 0.9319 0.9319 0.9372 0.9529 0.9529 0.9529 0.9686 0.9686 0.9686 0.9791 0.9791 0.9895 0.9895 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.15.8.4 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0714 0.1429 0.3571 0.3571 0.3571 0.4286 0.5000 0.5714 0.6429 0.7857 0.7857 1.0000 1.0000 1.0000 0.0952 0.0952 0.1905 0.1905 0.2857 0.2857 0.3810 0.3810 0.4762 0.4762 0.5238 0.6190 0.6190 0.7619 0.7619 0.7619 1.0000 1.0000 1.0000 1.0000 1.0000 0.1250 0.1250 0.3125 0.3125 0.3125 0.4375 0.4375 0.5000 0.5625 0.6250 0.7500 0.7500 0.9375 0.9375 0.9375 1.0000 0.0952 0.0952 0.1905 0.1905 0.2857 0.2857 0.4762 0.4762 0.4762 0.4762 0.5714 0.5714 0.6190 0.6667 0.8095 0.8095 0.8095 0.8571 1.0000 1.0000 1.0000 0.0526 0.1053 0.1579 0.3684 0.3684 0.3684 0.3684 0.4737 0.4737 0.5263 0.5789 0.7368 0.7368 0.7368 0.9474 0.9474 0.9474 0.9474 1.0000 0.0476 0.1905 0.1905 0.1905 0.2381 0.3333 0.3333 0.4286 0.4286 0.5714 0.5714 0.5714 0.7143 0.7143 0.7143 0.8571 0.8571 0.8571 1.0000 1.0000 1.0000 0.0476 0.1905 0.1905 0.1905 0.2857 0.2857 0.4762 0.4762 0.4762 0.4762 0.5714 0.5714 0.7143 0.7143 0.7143 0.7619 0.8095 0.8571 1.0000 1.0000 1.0000 0.1500 0.1500 0.1500 0.2000 0.3000 0.3000 0.4000 0.4000 0.6000 0.6000 0.6000 0.6000 0.6500 0.7000 0.8000 0.8000 0.9000 0.9000 1.0000 1.0000 0.1875 0.1875 0.1875 0.2500 0.3750 0.3750 0.6250 0.6250 0.6250 0.6250 0.6875 0.8125 0.8125 0.8750 1.0000 1.0000 0.1364 0.1364 0.1364 0.1818 0.3182 0.3182 0.3182 0.4545 0.4545 0.4545 0.5000 0.6818 0.6818 0.6818 0.6818 0.7273 0.7727 0.9091 0.9091 0.9091 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.15.8.5 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.15.8.6 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.15.8.1 { + set myres {} + foreach r [db eval {SELECT ntile(100) OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 88.0000 89.0000 89.0000 90.0000 90.0000 91.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.15.8.2 { + set myres {} + foreach r [db eval {SELECT ntile(101) OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 22.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.15.8.3 { + set myres {} + foreach r [db eval {SELECT ntile(102) OVER ( ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 88.0000 89.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.15.8.4 { + set myres {} + foreach r [db eval {SELECT ntile(103) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 22.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.15.8.5 { + set myres {} + foreach r [db eval {SELECT ntile(104) OVER ( ORDER BY b%10,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000 103.0000 104.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.15.8.6 { + set myres {} + foreach r [db eval {SELECT ntile(105) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.15.8.7 { + set myres {} + foreach r [db eval {SELECT ntile(105) OVER ( ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 88.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000 103.0000 104.0000 105.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + +do_execsql_test 1.15.9.1 { + SELECT last_value(a+b) OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207} + +do_execsql_test 1.15.9.2 { + SELECT last_value(a+b) OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {223 223 223 223 223 223 223 223 223 223 223 223 223 + 223 210 210 210 210 210 210 210 210 210 210 210 210 + 210 210 210 210 210 210 210 210 210 280 280 280 280 + 280 280 280 280 280 280 280 280 280 280 280 280 279 + 279 279 279 279 279 279 279 279 279 279 279 279 279 + 279 279 279 279 279 279 279 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 274 274 274 274 274 274 274 274 274 274 274 274 274 + 274 274 274 274 274 274 274 274 212 212 212 212 212 + 212 212 212 212 212 212 212 212 212 212 212 212 212 + 212 212 212 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 232 232 232 + 232 232 232 232 232 232 232 232 232 232 232 232 232 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229} + +do_execsql_test 1.15.9.3 { + SELECT last_value(a+b) OVER ( ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276} + +do_execsql_test 1.15.9.4 { + SELECT last_value(a+b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {240 240 240 240 240 240 240 240 240 240 240 240 240 + 240 263 263 263 263 263 263 263 263 263 263 263 263 + 263 263 263 263 263 263 263 263 263 280 280 280 280 + 280 280 280 280 280 280 280 280 280 280 280 280 252 + 252 252 252 252 252 252 252 252 252 252 252 252 252 + 252 252 252 252 252 252 252 171 171 171 171 171 171 + 171 171 171 171 171 171 171 171 171 171 171 171 171 + 274 274 274 274 274 274 274 274 274 274 274 274 274 + 274 274 274 274 274 274 274 274 226 226 226 226 226 + 226 226 226 226 226 226 226 226 226 226 226 226 226 + 226 226 226 124 124 124 124 124 124 124 124 124 124 + 124 124 124 124 124 124 124 124 124 124 198 198 198 + 198 198 198 198 198 198 198 198 198 198 198 198 198 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276} + +do_execsql_test 1.15.9.5 { + SELECT last_value(a+b) OVER ( ORDER BY b%10,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229} + +do_execsql_test 1.15.9.6 { + SELECT last_value(a+b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING) FROM t2 +} {108 52 83 79 65 26 70 103 80 36 116 51 52 128 + 117 71 63 84 109 78 147 88 121 106 124 85 107 171 + 150 80 171 120 109 158 87 168 173 162 156 195 198 + 177 124 121 134 141 210 157 132 161 218 226 191 179 + 138 214 212 172 173 229 240 187 210 227 228 223 225 + 179 182 231 207 209 212 239 234 213 234 269 196 271 + 235 250 223 232 229 280 99 92 72 55 109 120 119 + 50 124 96 59 124 110 57 130 103 74 87 48 105 136 + 131 133 92 109 57 146 113 74 150 87 110 65 110 + 145 161 156 114 111 136 147 173 124 132 101 154 167 + 190 161 110 102 123 169 140 111 180 119 160 197 152 + 146 147 132 213 193 200 136 175 188 187 208 211 144 + 223 196 170 202 163 184 195 200 163 191 252 235 243 + 172 187 202 179 261 263 206 189 276 181 274 249 221 + 210 229 279 224 216 207} + +do_execsql_test 1.15.10.1 { + SELECT nth_value(b,b+1) OVER (ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING) FROM t2 +} {96 41 5 16 85 30 12 81 35 89 97 31 31 85 40 + 96 4 73 84 9 91 73 12 5 56 24 85 49 96 75 53 + 61 8 8 1 55 34 43 59 80 35 15 78 35 56 70 76 + 59 51 75 63 26 53 5 89 15 21 5 73 33 29 74 66 + 12 26 58 4 12 31 35 9 87 73 55 59 53 62 73 23 + 62 33 90 13 90 9 10 66 5 58 44 38 58 22 33 37 + 2 73 36 31 72 30 47 73 15 96 70 59 90 {} 7 21 + 83 {} 47 90 55 36 66 {} 50 {} 84 30 {} {} 34 + 77 74 {} 58 {} 13 {} 82 93 69 14 62 44 {} {} + 30 {} 83 93 {} {} {} 84 {} {} {} {} 14 30 82 + 34 34 3 {} {} {} {} {} 84 {} {} {} 99 {} {} + {} {} {} {} {} 58 {} {} {} {} {} {} {} {} {} + {} {} {} {} {}} + +do_execsql_test 1.15.10.2 { + SELECT nth_value(b,b+1) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 61 {} {} {} 81 {} 91 {} {} {} {} + {} {} {} {} {} {} 12 {} {} {} 22 {} 82 {} 12 + {} {} {} {} {} {} {} {} {} {} 43 {} {} {} {} + {} 33 {} {} {} {} {} {} {} 33 {} {} {} {} {} + {} 4 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} 75 {} {} {} {} {} {} 15 {} + {} {} {} {} {} {} {} {} {} 86 {} 26 36 {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 37 {} {} {} {} {} 47 {} {} {} {} + {} {} {} {} {} {} 98 {} {} {} {} {} 58 {} 38 + {} {} {} {} {} {} {} {} {} {} {} {} {} 59 {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.15.10.3 { + SELECT nth_value(b,b+1) OVER ( ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 1 2 2 2 3 4 6 7 8 8 8 9 9 10 11 12 12 + 13 14 15 15 16 16 17 19 21 22 22 23 24 25 26 + 27 29 29 30 30 31 32 33 33 34 34 35 35 36 36 + 37 38 38 39 39 40 41 42 43 44 46 46 47 47 47 + 49 50 51 53 54 55 56 56 56 58 58 58 59 59 59 + 60 61 62 63 65 65 66 68 69 72 72 73 73 74 74 + 75 76 77 78 80 81 81 82 84 84 84 85 85 86 87 + 87 89 89 89 90 90 91 91 91 93 93 95 95 96 96 + 97 98 99 {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {}} + +do_execsql_test 1.15.10.4 { + SELECT nth_value(b,b+1) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {80 {} {} {} {} {} {} {} {} {} {} {} {} {} 1 1 + 61 61 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 12 12 72 72 72 {} {} {} {} {} {} + {} {} {} {} {} 13 13 63 63 {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} 34 84 {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + 35 85 85 85 {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} 36 76 76 76 {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} 37 37 37 + 87 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} 58 58 58 {} {} {} {} {} {} {} {} {} {} + {} {} {} 39 39 39 89 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.15.10.5 { + SELECT nth_value(b,b+1) OVER ( ORDER BY b%10,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {34 2 11 34 81 65 73 84 64 55 12 33 91 42 65 + 35 84 15 95 61 25 53 74 81 72 91 84 33 3 43 + 32 85 56 56 93 85 2 93 43 94 22 75 82 13 12 + 46 6 3 55 25 26 47 83 37 85 43 55 34 75 86 36 + 33 36 36 85 68 14 4 25 33 95 27 16 38 77 55 4 + 58 98 37 15 95 16 38 77 55 16 58 38 36 56 7 + 36 59 89 57 75 86 89 39 98 8 97 15 46 {} 28 9 + 69 49 56 {} {} 78 16 28 26 36 {} 39 99 29 27 + 78 {} {} {} 37 27 98 {} 88 8 {} 28 {} {} {} + 59 37 59 {} 89 {} {} 47 {} 39 {} 29 29 8 78 9 + {} {} 58 59 {} {} {} {} 58 {} 38 {} {} {} {} + {} {} {} {} {} {} {} {} {} 59 {} {} {} {} {} + {} {} {} {} {} {} {}} + +do_execsql_test 1.15.10.6 { + SELECT nth_value(b,b+1) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.15.11.1 { + SELECT first_value(b) OVER (ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING) FROM t2 +} {89 89 89 89 89 81 96 59 38 68 39 62 91 46 6 + 99 97 27 46 78 54 97 8 67 29 93 84 77 23 16 + 16 93 65 35 47 7 86 74 61 91 85 24 85 43 59 + 12 32 56 3 91 22 90 55 15 28 89 25 47 1 56 40 + 43 56 16 75 36 89 98 76 81 4 94 42 30 78 33 + 29 53 63 2 87 37 80 84 72 41 9 61 73 95 65 13 + 58 96 98 1 21 74 65 35 5 73 11 51 87 41 12 8 + 20 31 31 15 95 22 73 79 88 34 8 11 49 34 90 + 59 96 60 55 75 77 44 2 7 85 57 74 29 70 59 19 + 39 26 26 47 80 90 36 58 47 9 72 72 66 33 93 + 75 64 81 9 23 37 13 12 14 62 91 36 91 33 15 + 34 36 99 3 95 69 58 52 30 50 84 10 84 33 21 + 39 44 58 30 38 34 83} + +do_execsql_test 1.15.11.2 { + SELECT first_value(b) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING) FROM t2 +} {90 90 90 90 90 40 30 80 20 90 60 70 80 90 81 + 81 81 81 81 91 61 91 91 1 81 41 61 1 21 11 51 + 41 31 31 11 62 62 62 62 62 12 32 22 42 2 72 + 12 22 2 72 72 93 93 93 93 93 23 93 43 3 43 33 + 53 63 73 13 73 73 33 93 23 13 54 54 54 54 54 + 84 74 24 4 94 84 74 34 34 44 74 64 14 34 65 + 65 65 65 65 35 85 85 55 15 25 75 95 65 65 35 + 5 15 95 55 75 96 96 96 96 96 46 6 46 16 16 86 + 56 56 56 16 36 76 96 96 26 26 97 97 97 97 97 + 27 97 67 77 47 7 47 87 37 87 77 7 57 47 47 38 + 38 38 38 38 68 78 8 28 98 78 58 98 8 88 8 89 + 89 89 89 89 59 39 99 29 59 89 89 29 9 79 49 + 59 29 59 19 39 9} + +do_execsql_test 1.15.11.3 { + SELECT first_value(b) OVER ( ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 1 1 1 1 1 2 2 3 3 4 5 6 7 7 7 8 8 8 + 9 9 9 10 11 11 12 12 12 13 13 14 15 15 15 16 + 16 16 17 19 20 21 21 22 22 23 23 24 25 26 26 + 27 27 28 29 29 29 30 30 30 31 31 32 33 33 33 + 33 34 34 34 34 35 35 36 36 36 36 37 37 38 38 + 39 39 39 40 41 41 42 43 43 44 44 46 46 47 47 + 47 47 49 50 51 52 53 54 55 55 56 56 56 57 58 + 58 58 58 59 59 59 59 60 61 61 62 62 63 64 65 + 65 65 66 67 68 69 70 72 72 72 73 73 73 74 74 + 74 75 75 75 76 77 77 78 78 79 80 80 81 81 81 + 82 83 84 84 84 84 85 85 85 86 87 87 88 89 89 + 89 90 90 90 91 91 91 91 91 93 93 93 94 95 95 + 95 96 96 96 97 97} + +do_execsql_test 1.15.11.4 { + SELECT first_value(b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {10 10 10 10 10 20 30 30 30 40 50 60 70 80 1 1 + 1 1 1 1 11 11 21 21 31 31 41 41 51 61 61 81 + 81 81 91 2 2 2 2 2 2 12 12 12 22 22 32 42 + 52 62 62 3 3 3 3 3 3 13 13 23 23 33 33 33 + 33 43 43 53 63 73 73 73 4 4 4 4 4 14 24 34 + 34 34 34 44 44 54 64 74 74 74 84 5 5 5 5 5 + 15 15 15 25 35 35 55 55 65 65 65 75 75 75 85 + 85 6 6 6 6 6 16 16 16 26 26 36 36 36 36 46 + 46 56 56 56 66 76 7 7 7 7 7 7 7 17 27 27 37 + 37 47 47 47 47 57 67 77 77 8 8 8 8 8 8 8 28 + 38 38 58 58 58 58 68 78 9 9 9 9 9 9 9 19 29 + 29 29 39 39 39 49 59 59 59 59 69 79 89} + +do_execsql_test 1.15.11.5 { + SELECT first_value(b) OVER ( ORDER BY b%10,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {90 90 90 90 90 40 30 80 20 90 60 70 80 90 30 + 50 10 30 81 91 61 91 91 1 81 41 61 1 21 11 51 + 41 31 31 11 81 91 91 21 62 12 32 22 42 2 72 + 12 22 2 72 72 12 62 52 82 93 23 93 43 3 43 33 + 53 63 73 13 73 73 33 93 23 13 33 3 33 83 54 + 84 74 24 4 94 84 74 34 34 44 74 64 14 34 84 + 84 44 34 65 35 85 85 55 15 25 75 95 65 65 35 + 5 15 95 55 75 85 75 15 95 96 46 6 46 16 16 86 + 56 56 56 16 36 76 96 96 26 26 36 66 36 36 97 + 27 97 67 77 47 7 47 87 37 87 77 7 57 47 47 37 + 27 17 7 38 68 78 8 28 98 78 58 98 8 88 8 58 + 58 58 38 89 59 39 99 29 59 89 89 29 9 79 49 + 59 29 59 19 39 9} + +do_execsql_test 1.15.11.6 { + SELECT first_value(b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING) FROM t2 +} {96 38 68 62 46 6 46 78 54 8 84 16 16 86 74 24 + 12 32 56 22 90 28 56 40 56 16 36 98 76 4 94 + 42 30 78 2 80 84 72 58 96 98 74 12 8 20 22 88 + 34 8 34 90 96 60 44 2 74 70 26 26 80 90 36 58 + 72 72 66 64 12 14 62 36 34 36 58 52 30 50 84 + 10 84 44 58 30 38 34 82 89 81 59 39 91 99 97 + 27 97 67 29 93 77 23 93 65 35 47 7 61 91 85 + 85 43 59 3 91 55 15 89 25 47 1 43 75 89 81 33 + 29 53 63 87 37 41 9 61 73 95 65 13 1 21 65 35 + 5 73 11 51 87 41 31 31 15 95 73 79 11 49 59 + 55 75 77 7 85 57 29 59 19 39 47 47 9 33 93 75 + 81 9 23 37 13 91 91 33 15 99 3 95 69 33 21 39 + 83 27 17 7} + +do_execsql_test 1.15.12.1 { + SELECT lead(b,b) OVER (ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING) FROM t2 +} {96 9 11 89 32 53 91 30 51 56 54 73 22 59 75 + 74 78 8 16 65 15 8 31 87 90 12 32 96 74 76 37 + 85 90 15 35 2 60 36 75 9 51 47 63 51 90 26 42 + 26 8 76 80 90 37 87 56 79 5 87 8 2 39 73 64 + 36 90 72 78 36 73 51 33 20 41 2 26 37 33 8 14 + 33 81 55 1 9 12 39 64 87 72 34 82 21 34 99 62 + 74 41 69 22 75 27 58 8 79 77 26 26 55 {} 29 + 30 7 {} 66 55 2 34 64 {} 33 {} 44 84 {} {} 95 + 85 19 {} 83 {} 91 {} {} 9 50 91 33 34 {} {} + 84 {} 7 9 {} {} {} 44 {} {} {} {} 91 84 {} 95 + 95 52 {} {} {} {} {} 21 {} {} {} 58 {} {} {} + {} {} {} {} 83 {} {} {} {} {} {} {} {} {} {} + {} {} {} {}} + +do_execsql_test 1.15.12.2 { + SELECT lead(b,b) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 81 {} {} {} 21 {} {} {} {} {} {} + {} {} {} {} {} {} 62 {} {} {} 12 {} {} {} 72 + {} {} {} {} {} {} {} {} {} {} 53 {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 34 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} 95 {} {} {} {} {} {} 85 {} + {} {} {} {} {} {} {} {} {} 56 {} 36 {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 57 {} {} {} {} {} 7 {} {} {} {} + {} {} {} {} {} {} 8 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 9 {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.15.12.3 { + SELECT lead(b,b) OVER ( ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 3 5 6 7 8 8 9 9 10 11 12 12 13 13 + 14 15 16 16 17 19 20 21 22 23 24 25 26 27 27 + 28 29 30 31 32 33 33 33 34 34 35 36 36 36 37 + 38 39 39 40 41 42 43 43 44 46 47 47 47 49 50 + 52 53 54 55 56 56 57 58 58 58 59 59 59 60 61 + 62 62 64 65 65 67 69 70 72 72 73 74 74 75 75 + 75 77 78 80 81 81 83 84 84 85 85 85 87 88 89 + 89 89 90 90 91 91 91 93 93 94 95 95 96 97 97 + 98 99 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.15.12.4 { + SELECT lead(b,b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {80 {} {} {} {} {} {} {} {} {} {} {} {} {} 1 + 11 81 81 {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} 12 12 72 82 {} {} {} {} {} {} + {} {} {} {} {} {} 13 23 73 73 {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} 34 84 {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 35 85 85 95 {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} 36 86 96 96 {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 37 47 + 47 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} 58 58 68 {} {} {} {} {} {} {} {} {} + {} {} {} {} 39 49 59 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.15.12.5 { + SELECT lead(b,b) OVER ( ORDER BY b%10,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {34 72 91 74 21 55 23 94 84 95 2 13 61 22 55 + 55 94 85 75 81 65 73 74 21 72 12 94 33 63 73 + 72 96 36 76 3 25 62 3 73 34 12 46 43 93 72 16 + 86 63 15 65 36 77 24 57 25 53 95 34 95 16 97 + 74 97 67 25 98 44 34 65 54 5 68 96 28 47 95 + 34 39 8 38 6 46 96 28 47 95 56 39 99 97 76 8 + 26 9 79 27 95 16 29 {} 58 58 77 85 56 {} 98 + 29 {} 19 96 {} {} 78 56 98 36 97 {} 89 89 29 + 47 78 {} {} {} 38 68 58 {} 58 38 {} 98 {} {} + {} 39 57 9 {} 79 {} {} 7 {} {} {} 9 29 38 78 + {} {} {} 8 39 {} {} {} {} 59 {} 99 {} {} {} + {} {} {} {} {} {} {} {} {} {} 9 {} {} {} {} + {} {} {} {} {} {} {} {}} + +do_execsql_test 1.15.12.6 { + SELECT lead(b,b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.15.13.1 { + SELECT lag(b,b) OVER (ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} 38 {} {} {} {} + {} {} {} 6 {} {} {} {} {} 81 46 6 {} {} {} {} + 23 {} {} {} {} {} 27 {} {} {} 35 6 {} 12 {} + 23 {} {} 61 84 {} 93 39 47 {} 54 46 96 56 {} + 16 {} {} {} {} 89 {} 16 43 {} 85 56 29 99 53 + {} 59 {} {} 91 59 53 84 99 {} 93 63 47 {} {} + 98 33 67 35 75 1 23 13 55 27 75 98 35 73 63 2 + 21 27 13 24 86 23 84 31 20 94 61 65 75 23 36 + 94 55 90 41 77 96 56 29 40 12 89 63 11 5 73 + 79 1 16 28 31 73 5 39 53 63 41 11 40 2 13 33 + 9 29 90 47 72 9 73 30 44 33 74 93 29 74 42 34 + 63 41 34 96 47 77 1 36 74 72 14 36 26 77 9 72 + 64 8 91 31 52 30} + +do_execsql_test 1.15.13.2 { + SELECT lag(b,b) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} 30 {} {} + {} {} {} {} 91 {} {} {} 61 {} 81 {} {} {} {} + 1 {} {} {} {} {} {} {} {} {} 22 {} {} {} 12 + {} {} 62 {} {} {} {} {} {} {} 23 {} {} {} {} + {} {} {} {} {} {} {} 43 {} 23 {} {} {} {} {} + {} 54 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 75 {} + {} {} {} {} {} 55 {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} 47 {} {} {} {} + {} 27 7 {} {} {} {} {} {} {} {} {} 68 {} 8 {} + {} {} {} {} {} {} {} {} {} {} {} {} 89 {} {} + {} {} {} {} {} 29 9 {} {} {}} + +do_execsql_test 1.15.13.3 { + SELECT lag(b,b) OVER ( ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {{} 1 1 1 1 2 2 2 2 2 2 3 3 3 4 4 5 6 6 + 6 7 7 7 7 7 8 8 8 8 8 8 9 9 9 9 9 9 9 + 9 9 9 10 10 10 10 11 11 11 11 11 12 12 12 12 + 13 13 13 13 13 14 15 15 15 15 16 16 16 16 16 + 17 19 20 20 21 21 21 21 22 22 22 22 23 23 23 + 23 23 24 23 24 24 25 26 26 26 26 26 26 26 26 + 26 26 26 27 27 27 27 28 29 29 29 29 30 30 30 + 30 30 30 31 31 31 31 31 32 32 32 32 32 32 31 + 32 33 33 33 33 33 33 34 34 34 34 34 34 34 34 + 35 35 35 35 35 36 36 36 36 36 36 36 37 37 37 + 38 38 38 38 38 38 39 39 39 39 40 40 41 41 42 + 43 42 43 43 43 43 44 44 44 46 46 46 47 47 47 + 47 47} + +do_execsql_test 1.15.13.4 { + SELECT lag(b,b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + 1 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.15.13.5 { + SELECT lag(b,b) OVER ( ORDER BY b%10,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} 30 {} {} + {} {} {} {} 91 {} {} {} 61 80 81 {} {} {} {} + 1 {} {} {} 30 {} 21 90 61 {} 22 {} 11 41 12 + {} {} 62 {} {} {} {} 31 {} 50 23 30 21 90 {} + {} 62 {} {} 81 {} 22 43 62 23 32 {} 91 {} 90 + 93 54 {} {} 90 72 12 22 90 81 83 23 80 20 72 + 43 51 33 80 90 2 34 54 1 20 62 12 13 75 44 30 + 93 91 1 21 55 61 61 13 85 3 65 65 91 73 33 93 + 55 84 62 31 11 65 35 85 33 55 15 12 75 22 3 + 73 65 36 85 43 95 43 13 47 44 65 65 96 36 27 + 7 46 34 94 47 36 73 34 35 73 68 24 8 75 85 75 + 66 34 95 36 84 77 46 34 84 47 89 65 36 16 38 + 76 58 57 29 9 44 56 17} + +do_execsql_test 1.15.13.6 { + SELECT lag(b,b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.15.14.1 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING) FROM t2 +} {89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 33.21.39.44.58.30.38.34.83.27.82.17.7 21.39.44.58.30.38.34.83.27.82.17.7 + 39.44.58.30.38.34.83.27.82.17.7 44.58.30.38.34.83.27.82.17.7 + 58.30.38.34.83.27.82.17.7 30.38.34.83.27.82.17.7 38.34.83.27.82.17.7 + 34.83.27.82.17.7 83.27.82.17.7} + +do_execsql_test 1.15.14.2 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING) FROM t2 +} {90.40.30.80.20.90.60.70.80.90.30.50.10.30 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30 + 40.30.80.20.90.60.70.80.90.30.50.10.30 + 30.80.20.90.60.70.80.90.30.50.10.30 80.20.90.60.70.80.90.30.50.10.30 + 20.90.60.70.80.90.30.50.10.30 90.60.70.80.90.30.50.10.30 + 60.70.80.90.30.50.10.30 70.80.90.30.50.10.30 80.90.30.50.10.30 + 90.30.50.10.30 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 61.1.21.11.51.41.31.31.11.81.91.91.21 1.21.11.51.41.31.31.11.81.91.91.21 + 21.11.51.41.31.31.11.81.91.91.21 11.51.41.31.31.11.81.91.91.21 + 51.41.31.31.11.81.91.91.21 41.31.31.11.81.91.91.21 31.31.11.81.91.91.21 + 31.11.81.91.91.21 11.81.91.91.21 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 22.42.2.72.12.22.2.72.72.12.62.52.82 42.2.72.12.22.2.72.72.12.62.52.82 + 2.72.12.22.2.72.72.12.62.52.82 72.12.22.2.72.72.12.62.52.82 + 12.22.2.72.72.12.62.52.82 22.2.72.72.12.62.52.82 2.72.72.12.62.52.82 + 72.72.12.62.52.82 72.12.62.52.82 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 63.73.13.73.73.33.93.23.13.33.3.33.83 73.13.73.73.33.93.23.13.33.3.33.83 + 13.73.73.33.93.23.13.33.3.33.83 73.73.33.93.23.13.33.3.33.83 + 73.33.93.23.13.33.3.33.83 33.93.23.13.33.3.33.83 93.23.13.33.3.33.83 + 23.13.33.3.33.83 13.33.3.33.83 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 84.74.34.34.44.74.64.14.34.84.84.44.34 + 74.34.34.44.74.64.14.34.84.84.44.34 34.34.44.74.64.14.34.84.84.44.34 + 34.44.74.64.14.34.84.84.44.34 44.74.64.14.34.84.84.44.34 + 74.64.14.34.84.84.44.34 64.14.34.84.84.44.34 14.34.84.84.44.34 + 34.84.84.44.34 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 95.65.65.35.5.15.95.55.75.85.75.15.95 65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.5.15.95.55.75.85.75.15.95 35.5.15.95.55.75.85.75.15.95 + 5.15.95.55.75.85.75.15.95 15.95.55.75.85.75.15.95 95.55.75.85.75.15.95 + 55.75.85.75.15.95 75.85.75.15.95 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 56.56.16.36.76.96.96.26.26.36.66.36.36 + 56.16.36.76.96.96.26.26.36.66.36.36 16.36.76.96.96.26.26.36.66.36.36 + 36.76.96.96.26.26.36.66.36.36 76.96.96.26.26.36.66.36.36 + 96.96.26.26.36.66.36.36 96.26.26.36.66.36.36 26.26.36.66.36.36 + 26.36.66.36.36 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 47.87.37.87.77.7.57.47.47.37.27.17.7 87.37.87.77.7.57.47.47.37.27.17.7 + 37.87.77.7.57.47.47.37.27.17.7 87.77.7.57.47.47.37.27.17.7 + 77.7.57.47.47.37.27.17.7 7.57.47.47.37.27.17.7 57.47.47.37.27.17.7 + 47.47.37.27.17.7 47.37.27.17.7 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 8.28.98.78.58.98.8.88.8.58.58.58.38 28.98.78.58.98.8.88.8.58.58.58.38 + 98.78.58.98.8.88.8.58.58.58.38 78.58.98.8.88.8.58.58.58.38 + 58.98.8.88.8.58.58.58.38 98.8.88.8.58.58.58.38 8.88.8.58.58.58.38 + 88.8.58.58.58.38 8.58.58.58.38 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 9.79.49.59.29.59.19.39.9.9.99.69.39 79.49.59.29.59.19.39.9.9.99.69.39 + 49.59.29.59.19.39.9.9.99.69.39 59.29.59.19.39.9.9.99.69.39 + 29.59.19.39.9.9.99.69.39 59.19.39.9.9.99.69.39 19.39.9.9.99.69.39 + 39.9.9.99.69.39 9.9.99.69.39} + +do_execsql_test 1.15.14.3 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 94.95.95.95.96.96.96.97.97.98.98.99.99 + 95.95.95.96.96.96.97.97.98.98.99.99 95.95.96.96.96.97.97.98.98.99.99 + 95.96.96.96.97.97.98.98.99.99 96.96.96.97.97.98.98.99.99 + 96.96.97.97.98.98.99.99 96.97.97.98.98.99.99 97.97.98.98.99.99 + 97.98.98.99.99} + +do_execsql_test 1.15.14.4 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {10.20.30.30.30.40.50.60.70.80.80.90.90.90 + 10.20.30.30.30.40.50.60.70.80.80.90.90.90 + 10.20.30.30.30.40.50.60.70.80.80.90.90.90 + 10.20.30.30.30.40.50.60.70.80.80.90.90.90 + 10.20.30.30.30.40.50.60.70.80.80.90.90.90 + 20.30.30.30.40.50.60.70.80.80.90.90.90 + 30.30.30.40.50.60.70.80.80.90.90.90 30.30.40.50.60.70.80.80.90.90.90 + 30.40.50.60.70.80.80.90.90.90 40.50.60.70.80.80.90.90.90 + 50.60.70.80.80.90.90.90 60.70.80.80.90.90.90 70.80.80.90.90.90 + 80.80.90.90.90 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 41.41.51.61.61.81.81.81.91.91.91.91.91 + 41.51.61.61.81.81.81.91.91.91.91.91 51.61.61.81.81.81.91.91.91.91.91 + 61.61.81.81.81.91.91.91.91.91 61.81.81.81.91.91.91.91.91 + 81.81.81.91.91.91.91.91 81.81.91.91.91.91.91 81.91.91.91.91.91 + 91.91.91.91.91 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 12.12.22.22.32.42.52.62.62.72.72.72.82 + 12.22.22.32.42.52.62.62.72.72.72.82 22.22.32.42.52.62.62.72.72.72.82 + 22.32.42.52.62.62.72.72.72.82 32.42.52.62.62.72.72.72.82 + 42.52.62.62.72.72.72.82 52.62.62.72.72.72.82 62.62.72.72.72.82 + 62.72.72.72.82 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 33.33.43.43.53.63.73.73.73.83.93.93.93 + 33.43.43.53.63.73.73.73.83.93.93.93 43.43.53.63.73.73.73.83.93.93.93 + 43.53.63.73.73.73.83.93.93.93 53.63.73.73.73.83.93.93.93 + 63.73.73.73.83.93.93.93 73.73.73.83.93.93.93 73.73.83.93.93.93 + 73.83.93.93.93 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 34.44.44.54.64.74.74.74.84.84.84.84.94 + 44.44.54.64.74.74.74.84.84.84.84.94 44.54.64.74.74.74.84.84.84.84.94 + 54.64.74.74.74.84.84.84.84.94 64.74.74.74.84.84.84.84.94 + 74.74.74.84.84.84.84.94 74.74.84.84.84.84.94 74.84.84.84.84.94 + 84.84.84.84.94 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 55.65.65.65.75.75.75.85.85.85.95.95.95 + 65.65.65.75.75.75.85.85.85.95.95.95 65.65.75.75.75.85.85.85.95.95.95 + 65.75.75.75.85.85.85.95.95.95 75.75.75.85.85.85.95.95.95 + 75.75.85.85.85.95.95.95 75.85.85.85.95.95.95 85.85.85.95.95.95 + 85.85.95.95.95 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 36.36.46.46.56.56.56.66.76.86.96.96.96 + 36.46.46.56.56.56.66.76.86.96.96.96 46.46.56.56.56.66.76.86.96.96.96 + 46.56.56.56.66.76.86.96.96.96 56.56.56.66.76.86.96.96.96 + 56.56.66.76.86.96.96.96 56.66.76.86.96.96.96 66.76.86.96.96.96 + 76.86.96.96.96 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 37.47.47.47.47.57.67.77.77.87.87.97.97 + 47.47.47.47.57.67.77.77.87.87.97.97 47.47.47.57.67.77.77.87.87.97.97 + 47.47.57.67.77.77.87.87.97.97 47.57.67.77.77.87.87.97.97 + 57.67.77.77.87.87.97.97 67.77.77.87.87.97.97 77.77.87.87.97.97 + 77.87.87.97.97 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 28.38.38.58.58.58.58.68.78.78.88.98.98 + 38.38.58.58.58.58.68.78.78.88.98.98 38.58.58.58.58.68.78.78.88.98.98 + 58.58.58.58.68.78.78.88.98.98 58.58.58.68.78.78.88.98.98 + 58.58.68.78.78.88.98.98 58.68.78.78.88.98.98 68.78.78.88.98.98 + 78.78.88.98.98 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 39.49.59.59.59.59.69.79.89.89.89.99.99 + 49.59.59.59.59.69.79.89.89.89.99.99 59.59.59.59.69.79.89.89.89.99.99 + 59.59.59.69.79.89.89.89.99.99 59.59.69.79.89.89.89.99.99 + 59.69.79.89.89.89.99.99 69.79.89.89.89.99.99 79.89.89.89.99.99 + 89.89.89.99.99} + +do_execsql_test 1.15.14.5 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( ORDER BY b%10,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t2 +} {90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 9.79.49.59.29.59.19.39.9.9.99.69.39 79.49.59.29.59.19.39.9.9.99.69.39 + 49.59.29.59.19.39.9.9.99.69.39 59.29.59.19.39.9.9.99.69.39 + 29.59.19.39.9.9.99.69.39 59.19.39.9.9.99.69.39 19.39.9.9.99.69.39 + 39.9.9.99.69.39 9.9.99.69.39} + +do_execsql_test 1.15.14.6 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING) FROM t2 +} {96 38 68 62 46 6 46 78 54 8 84 16 16 86 74 24 + 12 32 56 22 90 28 56 40 56 16 36 98 76 4 94 + 42 30 78 2 80 84 72 58 96 98 74 12 8 20 22 88 + 34 8 34 90 96 60 44 2 74 70 26 26 80 90 36 58 + 72 72 66 64 12 14 62 36 34 36 58 52 30 50 84 + 10 84 44 58 30 38 34 82 89 81 59 39 91 99 97 + 27 97 67 29 93 77 23 93 65 35 47 7 61 91 85 + 85 43 59 3 91 55 15 89 25 47 1 43 75 89 81 33 + 29 53 63 87 37 41 9 61 73 95 65 13 1 21 65 35 + 5 73 11 51 87 41 31 31 15 95 73 79 11 49 59 + 55 75 77 7 85 57 29 59 19 39 47 47 9 33 93 75 + 81 9 23 37 13 91 91 33 15 99 3 95 69 33 21 39 + 83 27 17 7} + +do_execsql_test 1.15.14.7 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (win1 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING) + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a) + ORDER BY 1 +} {1 1 10 11 11 12 12 12 13 13 14 15 15 15 16 16 + 16 17 19 2 2 20 21 21 22 22 23 23 24 25 26 26 + 27 27 28 29 29 29 3 3 30 30 30 31 31 32 33 33 + 33 33 34 34 34 34 35 35 36 36 36 36 37 37 38 + 38 39 39 39 4 40 41 41 42 43 43 44 44 46 46 + 47 47 47 47 49 5 50 51 52 53 54 55 55 56 56 + 56 57 58 58 58 58 59 59 59 59 6 60 61 61 62 + 62 63 64 65 65 65 66 67 68 69 7 7 7 70 72 72 + 72 73 73 73 74 74 74 75 75 75 76 77 77 78 78 + 79 8 8 8 80 80 81 81 81 82 83 84 84 84 84 85 + 85 85 86 87 87 88 89 89 89 9 9 9 90 90 90 91 + 91 91 91 91 93 93 93 94 95 95 95 96 96 96 97 + 97 98 98 99 99} + +do_execsql_test 1.15.14.8 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (win1 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING) + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a ORDER BY b%10) + ORDER BY 1 +} {1 1 10 11 11 12 12 12 13 13 14 15 15 15 16 16 + 16 17 19 2 2 20 21 21 22 22 23 23 24 25 26 26 + 27 27 28 29 29 29 3 3 30 30 30 31 31 32 33 33 + 33 33 34 34 34 34 35 35 36 36 36 36 37 37 38 + 38 39 39 39 4 40 41 41 42 43 43 44 44 46 46 + 47 47 47 47 49 5 50 51 52 53 54 55 55 56 56 + 56 57 58 58 58 58 59 59 59 59 6 60 61 61 62 + 62 63 64 65 65 65 66 67 68 69 7 7 7 70 72 72 + 72 73 73 73 74 74 74 75 75 75 76 77 77 78 78 + 79 8 8 8 80 80 81 81 81 82 83 84 84 84 84 85 + 85 85 86 87 87 88 89 89 89 9 9 9 90 90 90 91 + 91 91 91 91 93 93 93 94 95 95 95 96 96 96 97 + 97 98 98 99 99} + +do_execsql_test 1.15.14.9 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER win2 + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a ORDER BY b%10), + win2 AS (win1 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING) + ORDER BY 1 +} {1 1 10 11 11 12 12 12 13 13 14 15 15 15 16 16 + 16 17 19 2 2 20 21 21 22 22 23 23 24 25 26 26 + 27 27 28 29 29 29 3 3 30 30 30 31 31 32 33 33 + 33 33 34 34 34 34 35 35 36 36 36 36 37 37 38 + 38 39 39 39 4 40 41 41 42 43 43 44 44 46 46 + 47 47 47 47 49 5 50 51 52 53 54 55 55 56 56 + 56 57 58 58 58 58 59 59 59 59 6 60 61 61 62 + 62 63 64 65 65 65 66 67 68 69 7 7 7 70 72 72 + 72 73 73 73 74 74 74 75 75 75 76 77 77 78 78 + 79 8 8 8 80 80 81 81 81 82 83 84 84 84 84 85 + 85 85 86 87 87 88 89 89 89 9 9 9 90 90 90 91 + 91 91 91 91 93 93 93 94 95 95 95 96 96 96 97 + 97 98 98 99 99} + +do_execsql_test 1.15.15.1 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE a%2=0) OVER win FROM t2 + WINDOW win AS (ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING) +} {191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 190 96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 189 96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 188 38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 187 38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 186 39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 185 39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 184 91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 183 91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 182 6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 181 6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 180 97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 179 97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 178 46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 177 46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 176 54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 175 54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 174 8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 173 8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 172 29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 171 29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 170 84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 169 84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 168 23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 167 23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 166 16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 165 16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 164 65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 163 65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 162 47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 161 47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 160 86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 159 86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 158 61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 157 61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 156 85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 155 85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 154 85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 153 85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 152 59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 151 59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 150 32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 149 32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 148 3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 147 3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 146 22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 145 22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 144 55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 143 55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 142 28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 141 28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 140 25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 139 25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 138 1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 137 1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 136 40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 135 40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 134 56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 133 56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 132 75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 131 75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 130 89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 129 89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 128 76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 127 76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 126 4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 125 4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 124 42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 123 42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 122 78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 121 78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 120 29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 119 29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 118 63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 117 63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 116 87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 115 87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 114 80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 113 80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 112 72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 111 72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 110 9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 109 9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 108 73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 107 73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 106 65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 105 65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 104 58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 103 58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 102 98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 101 98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 100 21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 99 21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 98 65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 97 65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 96 5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 95 5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 94 11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 93 11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 92 87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 91 87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 90 12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 89 12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 88 20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 87 20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 86 31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 85 31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 84 95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 83 95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 82 73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 81 73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 80 88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 79 88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 78 8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 77 8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 76 49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 75 49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 74 90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 73 90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 72 96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 71 96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 70 55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 69 55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 68 77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 67 77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 66 2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 65 2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 64 85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 63 85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 62 74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 61 74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 60 70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 59 70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 58 19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 57 19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 56 26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 55 26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 54 47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 53 47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 52 90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 51 90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 50 58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 49 58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 48 9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 47 9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 46 72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 45 72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 44 33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 43 33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 42 75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 41 75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 40 81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 39 81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 38 23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 37 23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 36 13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 35 13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 34 14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 33 14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 32 91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 31 91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 30 91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 29 91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 28 15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 27 15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 26 36.3.69.52.50.10.33.39.58.38.83.82.7 + 25 36.3.69.52.50.10.33.39.58.38.83.82.7 + 24 3.69.52.50.10.33.39.58.38.83.82.7 23 3.69.52.50.10.33.39.58.38.83.82.7 + 22 69.52.50.10.33.39.58.38.83.82.7 21 69.52.50.10.33.39.58.38.83.82.7 + 20 52.50.10.33.39.58.38.83.82.7 19 52.50.10.33.39.58.38.83.82.7 + 18 50.10.33.39.58.38.83.82.7 17 50.10.33.39.58.38.83.82.7 + 16 10.33.39.58.38.83.82.7 15 10.33.39.58.38.83.82.7 + 14 33.39.58.38.83.82.7 13 33.39.58.38.83.82.7 12 39.58.38.83.82.7 + 11 39.58.38.83.82.7 10 58.38.83.82.7 9 58.38.83.82.7 8 38.83.82.7 + 7 38.83.82.7 6 83.82.7 5 83.82.7} + +do_execsql_test 1.15.15.2 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE 0=1) OVER win FROM t2 + WINDOW win AS (ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING) +} {191 {} 191 {} 191 {} 191 {} 191 {} 190 {} 189 {} 188 {} + 187 {} 186 {} 185 {} 184 {} 183 {} 182 {} 181 {} 180 {} + 179 {} 178 {} 177 {} 176 {} 175 {} 174 {} 173 {} 172 {} + 171 {} 170 {} 169 {} 168 {} 167 {} 166 {} 165 {} 164 {} + 163 {} 162 {} 161 {} 160 {} 159 {} 158 {} 157 {} 156 {} + 155 {} 154 {} 153 {} 152 {} 151 {} 150 {} 149 {} 148 {} + 147 {} 146 {} 145 {} 144 {} 143 {} 142 {} 141 {} 140 {} + 139 {} 138 {} 137 {} 136 {} 135 {} 134 {} 133 {} 132 {} + 131 {} 130 {} 129 {} 128 {} 127 {} 126 {} 125 {} 124 {} + 123 {} 122 {} 121 {} 120 {} 119 {} 118 {} 117 {} 116 {} + 115 {} 114 {} 113 {} 112 {} 111 {} 110 {} 109 {} 108 {} + 107 {} 106 {} 105 {} 104 {} 103 {} 102 {} 101 {} 100 {} + 99 {} 98 {} 97 {} 96 {} 95 {} 94 {} 93 {} 92 {} 91 {} + 90 {} 89 {} 88 {} 87 {} 86 {} 85 {} 84 {} 83 {} 82 {} + 81 {} 80 {} 79 {} 78 {} 77 {} 76 {} 75 {} 74 {} 73 {} + 72 {} 71 {} 70 {} 69 {} 68 {} 67 {} 66 {} 65 {} 64 {} + 63 {} 62 {} 61 {} 60 {} 59 {} 58 {} 57 {} 56 {} 55 {} + 54 {} 53 {} 52 {} 51 {} 50 {} 49 {} 48 {} 47 {} 46 {} + 45 {} 44 {} 43 {} 42 {} 41 {} 40 {} 39 {} 38 {} 37 {} + 36 {} 35 {} 34 {} 33 {} 32 {} 31 {} 30 {} 29 {} 28 {} + 27 {} 26 {} 25 {} 24 {} 23 {} 22 {} 21 {} 20 {} 19 {} + 18 {} 17 {} 16 {} 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} + 9 {} 8 {} 7 {} 6 {} 5 {}} + +do_execsql_test 1.15.15.3 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE 1=0) OVER win FROM t2 + WINDOW win AS (PARTITION BY (a%10) ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING) +} {20 {} 20 {} 20 {} 20 {} 20 {} 19 {} 18 {} 17 {} 16 {} + 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} + 5 {} 19 {} 19 {} 19 {} 19 {} 19 {} 18 {} 17 {} 16 {} + 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} + 5 {} 19 {} 19 {} 19 {} 19 {} 19 {} 18 {} 17 {} 16 {} + 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} + 5 {} 19 {} 19 {} 19 {} 19 {} 19 {} 18 {} 17 {} 16 {} + 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} + 5 {} 19 {} 19 {} 19 {} 19 {} 19 {} 18 {} 17 {} 16 {} + 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} + 5 {} 19 {} 19 {} 19 {} 19 {} 19 {} 18 {} 17 {} 16 {} + 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} + 5 {} 19 {} 19 {} 19 {} 19 {} 19 {} 18 {} 17 {} 16 {} + 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} + 5 {} 19 {} 19 {} 19 {} 19 {} 19 {} 18 {} 17 {} 16 {} + 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} + 5 {} 19 {} 19 {} 19 {} 19 {} 19 {} 18 {} 17 {} 16 {} + 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} + 5 {} 19 {} 19 {} 19 {} 19 {} 19 {} 18 {} 17 {} 16 {} + 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} + 5 {}} + +do_execsql_test 1.15.15.4 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE a%2=0) OVER win FROM t2 + WINDOW win AS (PARTITION BY (a%10) ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING) +} {20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 19 6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 18 29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 17 47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 16 59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 15 28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 14 75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 13 78.72.98.87.73.96.74.90.75.91.69.39.7 + 12 72.98.87.73.96.74.90.75.91.69.39.7 11 98.87.73.96.74.90.75.91.69.39.7 + 10 87.73.96.74.90.75.91.69.39.7 9 73.96.74.90.75.91.69.39.7 + 8 96.74.90.75.91.69.39.7 7 74.90.75.91.69.39.7 6 90.75.91.69.39.7 + 5 75.91.69.39.7 19 {} 19 {} 19 {} 19 {} 19 {} 18 {} 17 {} + 16 {} 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} + 6 {} 5 {} 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 18 97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 17 84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 16 86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 15 32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 14 25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 13 89.29.9.21.12.88.55.70.58.81.91.52.58 + 12 29.9.21.12.88.55.70.58.81.91.52.58 11 9.21.12.88.55.70.58.81.91.52.58 + 10 21.12.88.55.70.58.81.91.52.58 9 12.88.55.70.58.81.91.52.58 + 8 88.55.70.58.81.91.52.58 7 55.70.58.81.91.52.58 6 70.58.81.91.52.58 + 5 58.81.91.52.58 19 {} 19 {} 19 {} 19 {} 19 {} 18 {} 17 {} + 16 {} 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} + 6 {} 5 {} 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 18 46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 17 23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 16 61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 15 3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 14 1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 13 76.63.73.65.20.8.77.19.9.23.15.50.38 + 12 63.73.65.20.8.77.19.9.23.15.50.38 11 73.65.20.8.77.19.9.23.15.50.38 + 10 65.20.8.77.19.9.23.15.50.38 9 20.8.77.19.9.23.15.50.38 + 8 8.77.19.9.23.15.50.38 7 77.19.9.23.15.50.38 6 19.9.23.15.50.38 + 5 9.23.15.50.38 19 {} 19 {} 19 {} 19 {} 19 {} 18 {} 17 {} + 16 {} 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} + 6 {} 5 {} 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 18 54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 17 16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 16 85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 15 22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 14 40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 13 4.87.65.5.31.49.2.26.72.13.36.10.83 + 12 87.65.5.31.49.2.26.72.13.36.10.83 11 65.5.31.49.2.26.72.13.36.10.83 + 10 5.31.49.2.26.72.13.36.10.83 9 31.49.2.26.72.13.36.10.83 + 8 49.2.26.72.13.36.10.83 7 2.26.72.13.36.10.83 6 26.72.13.36.10.83 + 5 72.13.36.10.83 19 {} 19 {} 19 {} 19 {} 19 {} 18 {} 17 {} + 16 {} 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} + 6 {} 5 {} 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 18 8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 17 65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 16 85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 15 55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 14 56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 13 42.80.58.11.95.90.85.47.33.14.3.33.82 + 12 80.58.11.95.90.85.47.33.14.3.33.82 11 58.11.95.90.85.47.33.14.3.33.82 + 10 11.95.90.85.47.33.14.3.33.82 9 95.90.85.47.33.14.3.33.82 + 8 90.85.47.33.14.3.33.82 7 85.47.33.14.3.33.82 6 47.33.14.3.33.82 + 5 33.14.3.33.82 19 {} 19 {} 19 {} 19 {} 19 {} 18 {} 17 {} + 16 {} 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} + 6 {} 5 {}} + +do_execsql_test 1.16.2.1 { + SELECT max(b) OVER ( ORDER BY a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 95 95 84 84 84 84 84 84 84 84 83 83 + 83 83 83 83 83 83 83 82 82 17 7} + +do_execsql_test 1.16.2.2 { + SELECT min(b) OVER ( ORDER BY a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 7 7 + 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 + 7} + +do_execsql_test 1.16.3.1 { + SELECT row_number() OVER ( ORDER BY a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.16.3.2 { + SELECT row_number() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.16.3.3 { + SELECT row_number() OVER ( ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.16.4.1 { + SELECT dense_rank() OVER ( ORDER BY a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.16.4.2 { + SELECT dense_rank() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.16.4.3 { + SELECT dense_rank() OVER ( ORDER BY b ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 1 2 2 3 3 4 5 6 7 7 7 8 8 8 9 9 9 10 + 11 11 12 12 12 13 13 14 15 15 15 16 16 16 17 + 18 19 20 20 21 21 22 22 23 24 25 25 26 26 27 + 28 28 28 29 29 29 30 30 31 32 32 32 32 33 33 + 33 33 34 34 35 35 35 35 36 36 37 37 38 38 38 + 39 40 40 41 42 42 43 43 44 44 45 45 45 45 46 + 47 48 49 50 51 52 52 53 53 53 54 55 55 55 55 + 56 56 56 56 57 58 58 59 59 60 61 62 62 62 63 + 64 65 66 67 68 68 68 69 69 69 70 70 70 71 71 + 71 72 73 73 74 74 75 76 76 77 77 77 78 79 80 + 80 80 80 81 81 81 82 83 83 84 85 85 85 86 86 + 86 87 87 87 87 87 88 88 88 89 90 90 90 91 91 + 91 92 92 93 93 94 94} + +do_execsql_test 1.16.4.4 { + SELECT dense_rank() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 3 3 4 5 6 7 8 8 9 9 9 1 1 2 2 3 + 3 4 4 5 5 6 7 7 8 8 8 9 9 9 9 9 1 1 2 + 2 2 3 3 4 5 6 7 7 8 8 8 9 1 1 2 2 3 3 + 4 4 4 4 5 5 6 7 8 8 8 9 10 10 10 1 2 3 + 4 4 4 4 5 5 6 7 8 8 8 9 9 9 9 10 1 2 2 + 2 3 4 4 5 5 6 6 6 7 7 7 8 8 8 9 9 9 1 + 2 2 2 3 3 4 4 4 4 5 5 6 6 6 7 8 9 10 10 + 10 1 1 1 2 3 3 4 4 5 5 5 5 6 7 8 8 9 9 + 10 10 1 1 1 2 3 3 4 4 4 4 5 6 6 7 8 8 1 + 1 1 2 3 3 3 4 4 4 5 6 6 6 6 7 8 9 9 9 + 10 10} + +do_execsql_test 1.16.4.5 { + SELECT dense_rank() OVER ( ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 + 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 + 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 + 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 + 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 + 8 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 10 10 + 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 + 10 10 10 10 10} + +do_execsql_test 1.16.4.6 { + SELECT dense_rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 + 5 5 5 5 5 5 5 5 5 5 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 + 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 + 5} + +do_execsql_test 1.16.5.1 { + SELECT rank() OVER ( ORDER BY a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.16.5.2 { + SELECT rank() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.16.5.3 { + SELECT rank() OVER ( ORDER BY b ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 1 3 3 5 5 7 8 9 10 10 10 13 13 13 16 16 + 16 19 20 20 22 22 22 25 25 27 28 28 28 31 31 + 31 34 35 36 37 37 39 39 41 41 43 44 45 45 47 + 47 49 50 50 50 53 53 53 56 56 58 59 59 59 59 + 63 63 63 63 67 67 69 69 69 69 73 73 75 75 77 + 77 77 80 81 81 83 84 84 86 86 88 88 90 90 90 + 90 94 95 96 97 98 99 100 100 102 102 102 105 106 + 106 106 106 110 110 110 110 114 115 115 117 117 119 + 120 121 121 121 124 125 126 127 128 129 129 129 132 + 132 132 135 135 135 138 138 138 141 142 142 144 144 + 146 147 147 149 149 149 152 153 154 154 154 154 158 + 158 158 161 162 162 164 165 165 165 168 168 168 171 + 171 171 171 171 176 176 176 179 180 180 180 183 183 + 183 186 186 188 188 190 190} + +do_execsql_test 1.16.5.4 { + SELECT rank() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 3 3 6 7 8 9 10 10 12 12 12 1 1 3 3 + 5 5 7 7 9 9 11 12 12 14 14 14 17 17 17 17 + 17 1 1 3 3 3 6 6 8 9 10 11 11 13 13 13 16 + 1 1 3 3 5 5 7 7 7 7 11 11 13 14 15 15 15 + 18 19 19 19 1 2 3 4 4 4 4 8 8 10 11 12 12 + 12 15 15 15 15 19 1 2 2 2 5 6 6 8 8 10 10 + 10 13 13 13 16 16 16 19 19 19 1 2 2 2 5 5 7 + 7 7 7 11 11 13 13 13 16 17 18 19 19 19 1 1 + 1 4 5 5 7 7 9 9 9 9 13 14 15 15 17 17 19 + 19 1 1 1 4 5 5 7 7 7 7 11 12 12 14 15 15 + 1 1 1 4 5 5 5 8 8 8 11 12 12 12 12 16 17 + 18 18 18 21 21} + +do_execsql_test 1.16.5.5 { + SELECT rank() OVER ( ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 15 15 15 15 + 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 + 15 15 36 36 36 36 36 36 36 36 36 36 36 36 36 + 36 36 36 52 52 52 52 52 52 52 52 52 52 52 52 + 52 52 52 52 52 52 52 52 52 73 73 73 73 73 73 + 73 73 73 73 73 73 73 73 73 73 73 73 73 92 92 + 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 + 92 92 92 92 113 113 113 113 113 113 113 113 113 + 113 113 113 113 113 113 113 113 113 113 113 113 134 + 134 134 134 134 134 134 134 134 134 134 134 134 134 + 134 134 134 134 134 134 154 154 154 154 154 154 154 + 154 154 154 154 154 154 154 154 154 170 170 170 170 + 170 170 170 170 170 170 170 170 170 170 170 170 170 + 170 170 170 170 170} + +do_execsql_test 1.16.5.6 { + SELECT rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 15 15 15 15 + 15 15 15 15 15 15 15 15 15 15 15 15 31 31 31 + 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 + 31 50 50 50 50 50 50 50 50 50 50 50 50 50 50 + 50 50 50 50 50 50 50 71 71 71 71 71 71 71 71 + 71 71 71 71 71 71 71 71 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 22 22 22 22 22 22 + 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 + 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 + 43 43 43 43 43 43 64 64 64 64 64 64 64 64 64 + 64 64 64 64 64 64 64 64 64 64 64 84 84 84 84 + 84 84 84 84 84 84 84 84 84 84 84 84 84 84 84 + 84 84 84} + +do_execsql_test 1.16.6.1 { + SELECT + row_number() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ), + rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ), + dense_rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) + FROM t2 +} {1 1 1 2 1 1 3 1 1 4 1 1 5 1 1 6 1 1 7 1 1 8 1 1 9 1 1 + 10 1 1 11 1 1 12 1 1 13 1 1 14 1 1 15 15 2 16 15 2 17 15 2 + 18 15 2 19 15 2 20 15 2 21 15 2 22 15 2 23 15 2 24 15 2 + 25 15 2 26 15 2 27 15 2 28 15 2 29 15 2 30 15 2 31 31 3 + 32 31 3 33 31 3 34 31 3 35 31 3 36 31 3 37 31 3 38 31 3 + 39 31 3 40 31 3 41 31 3 42 31 3 43 31 3 44 31 3 45 31 3 + 46 31 3 47 31 3 48 31 3 49 31 3 50 50 4 51 50 4 52 50 4 + 53 50 4 54 50 4 55 50 4 56 50 4 57 50 4 58 50 4 59 50 4 + 60 50 4 61 50 4 62 50 4 63 50 4 64 50 4 65 50 4 66 50 4 + 67 50 4 68 50 4 69 50 4 70 50 4 71 71 5 72 71 5 73 71 5 + 74 71 5 75 71 5 76 71 5 77 71 5 78 71 5 79 71 5 80 71 5 + 81 71 5 82 71 5 83 71 5 84 71 5 85 71 5 86 71 5 1 1 1 2 1 1 + 3 1 1 4 1 1 5 1 1 6 1 1 7 1 1 8 1 1 9 1 1 10 1 1 11 1 1 + 12 1 1 13 1 1 14 1 1 15 1 1 16 1 1 17 1 1 18 1 1 19 1 1 + 20 1 1 21 1 1 22 22 2 23 22 2 24 22 2 25 22 2 26 22 2 27 22 2 + 28 22 2 29 22 2 30 22 2 31 22 2 32 22 2 33 22 2 34 22 2 + 35 22 2 36 22 2 37 22 2 38 22 2 39 22 2 40 22 2 41 22 2 + 42 22 2 43 43 3 44 43 3 45 43 3 46 43 3 47 43 3 48 43 3 + 49 43 3 50 43 3 51 43 3 52 43 3 53 43 3 54 43 3 55 43 3 + 56 43 3 57 43 3 58 43 3 59 43 3 60 43 3 61 43 3 62 43 3 + 63 43 3 64 64 4 65 64 4 66 64 4 67 64 4 68 64 4 69 64 4 + 70 64 4 71 64 4 72 64 4 73 64 4 74 64 4 75 64 4 76 64 4 + 77 64 4 78 64 4 79 64 4 80 64 4 81 64 4 82 64 4 83 64 4 + 84 84 5 85 84 5 86 84 5 87 84 5 88 84 5 89 84 5 90 84 5 + 91 84 5 92 84 5 93 84 5 94 84 5 95 84 5 96 84 5 97 84 5 + 98 84 5 99 84 5 100 84 5 101 84 5 102 84 5 103 84 5 104 84 5 + 105 84 5} + + +do_test 1.16.7.1 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0053 0.0105 0.0158 0.0211 0.0263 0.0316 0.0368 0.0421 0.0474 0.0526 0.0579 0.0632 0.0684 0.0737 0.0789 0.0842 0.0895 0.0947 0.1000 0.1053 0.1105 0.1158 0.1211 0.1263 0.1316 0.1368 0.1421 0.1474 0.1526 0.1579 0.1632 0.1684 0.1737 0.1789 0.1842 0.1895 0.1947 0.2000 0.2053 0.2105 0.2158 0.2211 0.2263 0.2316 0.2368 0.2421 0.2474 0.2526 0.2579 0.2632 0.2684 0.2737 0.2789 0.2842 0.2895 0.2947 0.3000 0.3053 0.3105 0.3158 0.3211 0.3263 0.3316 0.3368 0.3421 0.3474 0.3526 0.3579 0.3632 0.3684 0.3737 0.3789 0.3842 0.3895 0.3947 0.4000 0.4053 0.4105 0.4158 0.4211 0.4263 0.4316 0.4368 0.4421 0.4474 0.4526 0.4579 0.4632 0.4684 0.4737 0.4789 0.4842 0.4895 0.4947 0.5000 0.5053 0.5105 0.5158 0.5211 0.5263 0.5316 0.5368 0.5421 0.5474 0.5526 0.5579 0.5632 0.5684 0.5737 0.5789 0.5842 0.5895 0.5947 0.6000 0.6053 0.6105 0.6158 0.6211 0.6263 0.6316 0.6368 0.6421 0.6474 0.6526 0.6579 0.6632 0.6684 0.6737 0.6789 0.6842 0.6895 0.6947 0.7000 0.7053 0.7105 0.7158 0.7211 0.7263 0.7316 0.7368 0.7421 0.7474 0.7526 0.7579 0.7632 0.7684 0.7737 0.7789 0.7842 0.7895 0.7947 0.8000 0.8053 0.8105 0.8158 0.8211 0.8263 0.8316 0.8368 0.8421 0.8474 0.8526 0.8579 0.8632 0.8684 0.8737 0.8789 0.8842 0.8895 0.8947 0.9000 0.9053 0.9105 0.9158 0.9211 0.9263 0.9316 0.9368 0.9421 0.9474 0.9526 0.9579 0.9632 0.9684 0.9737 0.9789 0.9842 0.9895 0.9947 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.16.7.2 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0769 0.1538 0.2308 0.3077 0.3846 0.4615 0.5385 0.6154 0.6923 0.7692 0.8462 0.9231 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0667 0.1333 0.2000 0.2667 0.3333 0.4000 0.4667 0.5333 0.6000 0.6667 0.7333 0.8000 0.8667 0.9333 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0556 0.1111 0.1667 0.2222 0.2778 0.3333 0.3889 0.4444 0.5000 0.5556 0.6111 0.6667 0.7222 0.7778 0.8333 0.8889 0.9444 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0526 0.1053 0.1579 0.2105 0.2632 0.3158 0.3684 0.4211 0.4737 0.5263 0.5789 0.6316 0.6842 0.7368 0.7895 0.8421 0.8947 0.9474 1.0000 0.0000 0.0667 0.1333 0.2000 0.2667 0.3333 0.4000 0.4667 0.5333 0.6000 0.6667 0.7333 0.8000 0.8667 0.9333 1.0000 0.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.16.7.3 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY b ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0105 0.0105 0.0211 0.0211 0.0316 0.0368 0.0421 0.0474 0.0474 0.0474 0.0632 0.0632 0.0632 0.0789 0.0789 0.0789 0.0947 0.1000 0.1000 0.1105 0.1105 0.1105 0.1263 0.1263 0.1368 0.1421 0.1421 0.1421 0.1579 0.1579 0.1579 0.1737 0.1789 0.1842 0.1895 0.1895 0.2000 0.2000 0.2105 0.2105 0.2211 0.2263 0.2316 0.2316 0.2421 0.2421 0.2526 0.2579 0.2579 0.2579 0.2737 0.2737 0.2737 0.2895 0.2895 0.3000 0.3053 0.3053 0.3053 0.3053 0.3263 0.3263 0.3263 0.3263 0.3474 0.3474 0.3579 0.3579 0.3579 0.3579 0.3789 0.3789 0.3895 0.3895 0.4000 0.4000 0.4000 0.4158 0.4211 0.4211 0.4316 0.4368 0.4368 0.4474 0.4474 0.4579 0.4579 0.4684 0.4684 0.4684 0.4684 0.4895 0.4947 0.5000 0.5053 0.5105 0.5158 0.5211 0.5211 0.5316 0.5316 0.5316 0.5474 0.5526 0.5526 0.5526 0.5526 0.5737 0.5737 0.5737 0.5737 0.5947 0.6000 0.6000 0.6105 0.6105 0.6211 0.6263 0.6316 0.6316 0.6316 0.6474 0.6526 0.6579 0.6632 0.6684 0.6737 0.6737 0.6737 0.6895 0.6895 0.6895 0.7053 0.7053 0.7053 0.7211 0.7211 0.7211 0.7368 0.7421 0.7421 0.7526 0.7526 0.7632 0.7684 0.7684 0.7789 0.7789 0.7789 0.7947 0.8000 0.8053 0.8053 0.8053 0.8053 0.8263 0.8263 0.8263 0.8421 0.8474 0.8474 0.8579 0.8632 0.8632 0.8632 0.8789 0.8789 0.8789 0.8947 0.8947 0.8947 0.8947 0.8947 0.9211 0.9211 0.9211 0.9368 0.9421 0.9421 0.9421 0.9579 0.9579 0.9579 0.9737 0.9737 0.9842 0.9842 0.9947 0.9947} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.16.7.4 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0769 0.1538 0.1538 0.1538 0.3846 0.4615 0.5385 0.6154 0.6923 0.6923 0.8462 0.8462 0.8462 0.0000 0.0000 0.1000 0.1000 0.2000 0.2000 0.3000 0.3000 0.4000 0.4000 0.5000 0.5500 0.5500 0.6500 0.6500 0.6500 0.8000 0.8000 0.8000 0.8000 0.8000 0.0000 0.0000 0.1333 0.1333 0.1333 0.3333 0.3333 0.4667 0.5333 0.6000 0.6667 0.6667 0.8000 0.8000 0.8000 1.0000 0.0000 0.0000 0.1000 0.1000 0.2000 0.2000 0.3000 0.3000 0.3000 0.3000 0.5000 0.5000 0.6000 0.6500 0.7000 0.7000 0.7000 0.8500 0.9000 0.9000 0.9000 0.0000 0.0556 0.1111 0.1667 0.1667 0.1667 0.1667 0.3889 0.3889 0.5000 0.5556 0.6111 0.6111 0.6111 0.7778 0.7778 0.7778 0.7778 1.0000 0.0000 0.0500 0.0500 0.0500 0.2000 0.2500 0.2500 0.3500 0.3500 0.4500 0.4500 0.4500 0.6000 0.6000 0.6000 0.7500 0.7500 0.7500 0.9000 0.9000 0.9000 0.0000 0.0500 0.0500 0.0500 0.2000 0.2000 0.3000 0.3000 0.3000 0.3000 0.5000 0.5000 0.6000 0.6000 0.6000 0.7500 0.8000 0.8500 0.9000 0.9000 0.9000 0.0000 0.0000 0.0000 0.1579 0.2105 0.2105 0.3158 0.3158 0.4211 0.4211 0.4211 0.4211 0.6316 0.6842 0.7368 0.7368 0.8421 0.8421 0.9474 0.9474 0.0000 0.0000 0.0000 0.2000 0.2667 0.2667 0.4000 0.4000 0.4000 0.4000 0.6667 0.7333 0.7333 0.8667 0.9333 0.9333 0.0000 0.0000 0.0000 0.1429 0.1905 0.1905 0.1905 0.3333 0.3333 0.3333 0.4762 0.5238 0.5238 0.5238 0.5238 0.7143 0.7619 0.8095 0.8095 0.8095 0.9524 0.9524} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.16.7.5 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.16.7.6 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER (PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.16.8.1 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0052 0.0105 0.0157 0.0209 0.0262 0.0314 0.0366 0.0419 0.0471 0.0524 0.0576 0.0628 0.0681 0.0733 0.0785 0.0838 0.0890 0.0942 0.0995 0.1047 0.1099 0.1152 0.1204 0.1257 0.1309 0.1361 0.1414 0.1466 0.1518 0.1571 0.1623 0.1675 0.1728 0.1780 0.1832 0.1885 0.1937 0.1990 0.2042 0.2094 0.2147 0.2199 0.2251 0.2304 0.2356 0.2408 0.2461 0.2513 0.2565 0.2618 0.2670 0.2723 0.2775 0.2827 0.2880 0.2932 0.2984 0.3037 0.3089 0.3141 0.3194 0.3246 0.3298 0.3351 0.3403 0.3455 0.3508 0.3560 0.3613 0.3665 0.3717 0.3770 0.3822 0.3874 0.3927 0.3979 0.4031 0.4084 0.4136 0.4188 0.4241 0.4293 0.4346 0.4398 0.4450 0.4503 0.4555 0.4607 0.4660 0.4712 0.4764 0.4817 0.4869 0.4921 0.4974 0.5026 0.5079 0.5131 0.5183 0.5236 0.5288 0.5340 0.5393 0.5445 0.5497 0.5550 0.5602 0.5654 0.5707 0.5759 0.5812 0.5864 0.5916 0.5969 0.6021 0.6073 0.6126 0.6178 0.6230 0.6283 0.6335 0.6387 0.6440 0.6492 0.6545 0.6597 0.6649 0.6702 0.6754 0.6806 0.6859 0.6911 0.6963 0.7016 0.7068 0.7120 0.7173 0.7225 0.7277 0.7330 0.7382 0.7435 0.7487 0.7539 0.7592 0.7644 0.7696 0.7749 0.7801 0.7853 0.7906 0.7958 0.8010 0.8063 0.8115 0.8168 0.8220 0.8272 0.8325 0.8377 0.8429 0.8482 0.8534 0.8586 0.8639 0.8691 0.8743 0.8796 0.8848 0.8901 0.8953 0.9005 0.9058 0.9110 0.9162 0.9215 0.9267 0.9319 0.9372 0.9424 0.9476 0.9529 0.9581 0.9634 0.9686 0.9738 0.9791 0.9843 0.9895 0.9948 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.16.8.2 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0714 0.1429 0.2143 0.2857 0.3571 0.4286 0.5000 0.5714 0.6429 0.7143 0.7857 0.8571 0.9286 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0625 0.1250 0.1875 0.2500 0.3125 0.3750 0.4375 0.5000 0.5625 0.6250 0.6875 0.7500 0.8125 0.8750 0.9375 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0526 0.1053 0.1579 0.2105 0.2632 0.3158 0.3684 0.4211 0.4737 0.5263 0.5789 0.6316 0.6842 0.7368 0.7895 0.8421 0.8947 0.9474 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0625 0.1250 0.1875 0.2500 0.3125 0.3750 0.4375 0.5000 0.5625 0.6250 0.6875 0.7500 0.8125 0.8750 0.9375 1.0000 0.0455 0.0909 0.1364 0.1818 0.2273 0.2727 0.3182 0.3636 0.4091 0.4545 0.5000 0.5455 0.5909 0.6364 0.6818 0.7273 0.7727 0.8182 0.8636 0.9091 0.9545 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.16.8.3 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY b ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0105 0.0105 0.0209 0.0209 0.0314 0.0314 0.0366 0.0419 0.0471 0.0628 0.0628 0.0628 0.0785 0.0785 0.0785 0.0942 0.0942 0.0942 0.0995 0.1099 0.1099 0.1257 0.1257 0.1257 0.1361 0.1361 0.1414 0.1571 0.1571 0.1571 0.1728 0.1728 0.1728 0.1780 0.1832 0.1885 0.1990 0.1990 0.2094 0.2094 0.2199 0.2199 0.2251 0.2304 0.2408 0.2408 0.2513 0.2513 0.2565 0.2723 0.2723 0.2723 0.2880 0.2880 0.2880 0.2984 0.2984 0.3037 0.3246 0.3246 0.3246 0.3246 0.3455 0.3455 0.3455 0.3455 0.3560 0.3560 0.3770 0.3770 0.3770 0.3770 0.3874 0.3874 0.3979 0.3979 0.4136 0.4136 0.4136 0.4188 0.4293 0.4293 0.4346 0.4450 0.4450 0.4555 0.4555 0.4660 0.4660 0.4869 0.4869 0.4869 0.4869 0.4921 0.4974 0.5026 0.5079 0.5131 0.5183 0.5288 0.5288 0.5445 0.5445 0.5445 0.5497 0.5707 0.5707 0.5707 0.5707 0.5916 0.5916 0.5916 0.5916 0.5969 0.6073 0.6073 0.6178 0.6178 0.6230 0.6283 0.6440 0.6440 0.6440 0.6492 0.6545 0.6597 0.6649 0.6702 0.6859 0.6859 0.6859 0.7016 0.7016 0.7016 0.7173 0.7173 0.7173 0.7330 0.7330 0.7330 0.7382 0.7487 0.7487 0.7592 0.7592 0.7644 0.7749 0.7749 0.7906 0.7906 0.7906 0.7958 0.8010 0.8220 0.8220 0.8220 0.8220 0.8377 0.8377 0.8377 0.8429 0.8534 0.8534 0.8586 0.8743 0.8743 0.8743 0.8901 0.8901 0.8901 0.9162 0.9162 0.9162 0.9162 0.9162 0.9319 0.9319 0.9319 0.9372 0.9529 0.9529 0.9529 0.9686 0.9686 0.9686 0.9791 0.9791 0.9895 0.9895 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.16.8.4 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0714 0.1429 0.3571 0.3571 0.3571 0.4286 0.5000 0.5714 0.6429 0.7857 0.7857 1.0000 1.0000 1.0000 0.0952 0.0952 0.1905 0.1905 0.2857 0.2857 0.3810 0.3810 0.4762 0.4762 0.5238 0.6190 0.6190 0.7619 0.7619 0.7619 1.0000 1.0000 1.0000 1.0000 1.0000 0.1250 0.1250 0.3125 0.3125 0.3125 0.4375 0.4375 0.5000 0.5625 0.6250 0.7500 0.7500 0.9375 0.9375 0.9375 1.0000 0.0952 0.0952 0.1905 0.1905 0.2857 0.2857 0.4762 0.4762 0.4762 0.4762 0.5714 0.5714 0.6190 0.6667 0.8095 0.8095 0.8095 0.8571 1.0000 1.0000 1.0000 0.0526 0.1053 0.1579 0.3684 0.3684 0.3684 0.3684 0.4737 0.4737 0.5263 0.5789 0.7368 0.7368 0.7368 0.9474 0.9474 0.9474 0.9474 1.0000 0.0476 0.1905 0.1905 0.1905 0.2381 0.3333 0.3333 0.4286 0.4286 0.5714 0.5714 0.5714 0.7143 0.7143 0.7143 0.8571 0.8571 0.8571 1.0000 1.0000 1.0000 0.0476 0.1905 0.1905 0.1905 0.2857 0.2857 0.4762 0.4762 0.4762 0.4762 0.5714 0.5714 0.7143 0.7143 0.7143 0.7619 0.8095 0.8571 1.0000 1.0000 1.0000 0.1500 0.1500 0.1500 0.2000 0.3000 0.3000 0.4000 0.4000 0.6000 0.6000 0.6000 0.6000 0.6500 0.7000 0.8000 0.8000 0.9000 0.9000 1.0000 1.0000 0.1875 0.1875 0.1875 0.2500 0.3750 0.3750 0.6250 0.6250 0.6250 0.6250 0.6875 0.8125 0.8125 0.8750 1.0000 1.0000 0.1364 0.1364 0.1364 0.1818 0.3182 0.3182 0.3182 0.4545 0.4545 0.4545 0.5000 0.6818 0.6818 0.6818 0.6818 0.7273 0.7727 0.9091 0.9091 0.9091 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.16.8.5 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.16.8.6 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.16.8.1 { + set myres {} + foreach r [db eval {SELECT ntile(100) OVER ( ORDER BY a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 88.0000 89.0000 89.0000 90.0000 90.0000 91.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.16.8.2 { + set myres {} + foreach r [db eval {SELECT ntile(101) OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 22.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.16.8.3 { + set myres {} + foreach r [db eval {SELECT ntile(102) OVER ( ORDER BY b,a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 88.0000 89.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.16.8.4 { + set myres {} + foreach r [db eval {SELECT ntile(103) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 22.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.16.8.5 { + set myres {} + foreach r [db eval {SELECT ntile(104) OVER ( ORDER BY b%10,a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000 103.0000 104.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.16.8.6 { + set myres {} + foreach r [db eval {SELECT ntile(105) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.16.8.7 { + set myres {} + foreach r [db eval {SELECT ntile(105) OVER ( ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 88.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000 103.0000 104.0000 105.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + +do_execsql_test 1.16.9.1 { + SELECT last_value(a+b) OVER ( ORDER BY a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207} + +do_execsql_test 1.16.9.2 { + SELECT last_value(a+b) OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {223 223 223 223 223 223 223 223 223 223 223 223 223 + 223 210 210 210 210 210 210 210 210 210 210 210 210 + 210 210 210 210 210 210 210 210 210 280 280 280 280 + 280 280 280 280 280 280 280 280 280 280 280 280 279 + 279 279 279 279 279 279 279 279 279 279 279 279 279 + 279 279 279 279 279 279 279 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 274 274 274 274 274 274 274 274 274 274 274 274 274 + 274 274 274 274 274 274 274 274 212 212 212 212 212 + 212 212 212 212 212 212 212 212 212 212 212 212 212 + 212 212 212 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 232 232 232 + 232 232 232 232 232 232 232 232 232 232 232 232 232 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229} + +do_execsql_test 1.16.9.3 { + SELECT last_value(a+b) OVER ( ORDER BY b,a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276} + +do_execsql_test 1.16.9.4 { + SELECT last_value(a+b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {240 240 240 240 240 240 240 240 240 240 240 240 240 + 240 263 263 263 263 263 263 263 263 263 263 263 263 + 263 263 263 263 263 263 263 263 263 280 280 280 280 + 280 280 280 280 280 280 280 280 280 280 280 280 252 + 252 252 252 252 252 252 252 252 252 252 252 252 252 + 252 252 252 252 252 252 252 171 171 171 171 171 171 + 171 171 171 171 171 171 171 171 171 171 171 171 171 + 274 274 274 274 274 274 274 274 274 274 274 274 274 + 274 274 274 274 274 274 274 274 226 226 226 226 226 + 226 226 226 226 226 226 226 226 226 226 226 226 226 + 226 226 226 124 124 124 124 124 124 124 124 124 124 + 124 124 124 124 124 124 124 124 124 124 198 198 198 + 198 198 198 198 198 198 198 198 198 198 198 198 198 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276} + +do_execsql_test 1.16.9.5 { + SELECT last_value(a+b) OVER ( ORDER BY b%10,a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229} + +do_execsql_test 1.16.9.6 { + SELECT last_value(a+b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM t2 +} {108 52 83 79 65 26 70 103 80 36 116 51 52 128 + 117 71 63 84 109 78 147 88 121 106 124 85 107 171 + 150 80 171 120 109 158 87 168 173 162 156 195 198 + 177 124 121 134 141 210 157 132 161 218 226 191 179 + 138 214 212 172 173 229 240 187 210 227 228 223 225 + 179 182 231 207 209 212 239 234 213 234 269 196 271 + 235 250 223 232 229 280 99 92 72 55 109 120 119 + 50 124 96 59 124 110 57 130 103 74 87 48 105 136 + 131 133 92 109 57 146 113 74 150 87 110 65 110 + 145 161 156 114 111 136 147 173 124 132 101 154 167 + 190 161 110 102 123 169 140 111 180 119 160 197 152 + 146 147 132 213 193 200 136 175 188 187 208 211 144 + 223 196 170 202 163 184 195 200 163 191 252 235 243 + 172 187 202 179 261 263 206 189 276 181 274 249 221 + 210 229 279 224 216 207} + +do_execsql_test 1.16.10.1 { + SELECT nth_value(b,b+1) OVER (ORDER BY a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM t2 +} {96 9 11 89 32 53 91 30 51 56 54 73 22 59 75 + 74 78 8 16 65 15 8 31 87 90 12 32 96 74 76 37 + 85 90 15 35 2 60 36 75 9 51 47 63 51 90 26 42 + 26 8 76 80 90 37 87 56 79 5 87 8 2 39 73 64 + 36 90 72 78 36 73 51 33 20 41 2 26 37 33 8 14 + 33 81 55 1 9 12 39 64 87 72 34 82 21 34 99 62 + 74 41 69 22 75 27 58 8 79 77 26 26 55 {} 29 + 30 7 {} 66 55 2 34 64 {} 33 {} 44 84 {} {} 95 + 85 19 {} 83 {} 91 {} {} 9 50 91 33 34 {} {} + 84 {} 7 9 {} {} {} 44 {} {} {} {} 91 84 {} 95 + 95 52 {} {} {} {} {} 21 {} {} {} 58 {} {} {} + {} {} {} {} 83 {} {} {} {} {} {} {} {} {} {} + {} {} {} {}} + +do_execsql_test 1.16.10.2 { + SELECT nth_value(b,b+1) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 81 {} {} {} 21 {} {} {} {} {} {} + {} {} {} {} {} {} 62 {} {} {} 12 {} {} {} 72 + {} {} {} {} {} {} {} {} {} {} 53 {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 34 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} 95 {} {} {} {} {} {} 85 {} + {} {} {} {} {} {} {} {} {} 56 {} 36 {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 57 {} {} {} {} {} 7 {} {} {} {} + {} {} {} {} {} {} 8 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 9 {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.16.10.3 { + SELECT nth_value(b,b+1) OVER ( ORDER BY b,a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 3 5 6 7 8 8 9 9 10 11 12 12 13 13 + 14 15 16 16 17 19 20 21 22 23 24 25 26 27 27 + 28 29 30 31 32 33 33 33 34 34 35 36 36 36 37 + 38 39 39 40 41 42 43 43 44 46 47 47 47 49 50 + 52 53 54 55 56 56 57 58 58 58 59 59 59 60 61 + 62 62 64 65 65 67 69 70 72 72 73 74 74 75 75 + 75 77 78 80 81 81 83 84 84 85 85 85 87 88 89 + 89 89 90 90 91 91 91 93 93 94 95 95 96 97 97 + 98 99 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.16.10.4 { + SELECT nth_value(b,b+1) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {80 {} {} {} {} {} {} {} {} {} {} {} {} {} 1 + 11 81 81 {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} 12 12 72 82 {} {} {} {} {} {} + {} {} {} {} {} {} 13 23 73 73 {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} 34 84 {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 35 85 85 95 {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} 36 86 96 96 {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 37 47 + 47 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} 58 58 68 {} {} {} {} {} {} {} {} {} + {} {} {} {} 39 49 59 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.16.10.5 { + SELECT nth_value(b,b+1) OVER ( ORDER BY b%10,a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {34 72 91 74 21 55 23 94 84 95 2 13 61 22 55 + 55 94 85 75 81 65 73 74 21 72 12 94 33 63 73 + 72 96 36 76 3 25 62 3 73 34 12 46 43 93 72 16 + 86 63 15 65 36 77 24 57 25 53 95 34 95 16 97 + 74 97 67 25 98 44 34 65 54 5 68 96 28 47 95 + 34 39 8 38 6 46 96 28 47 95 56 39 99 97 76 8 + 26 9 79 27 95 16 29 {} 58 58 77 85 56 {} 98 + 29 {} 19 96 {} {} 78 56 98 36 97 {} 89 89 29 + 47 78 {} {} {} 38 68 58 {} 58 38 {} 98 {} {} + {} 39 57 9 {} 79 {} {} 7 {} {} {} 9 29 38 78 + {} {} {} 8 39 {} {} {} {} 59 {} 99 {} {} {} + {} {} {} {} {} {} {} {} {} {} 9 {} {} {} {} + {} {} {} {} {} {} {} {}} + +do_execsql_test 1.16.10.6 { + SELECT nth_value(b,b+1) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.16.11.1 { + SELECT first_value(b) OVER (ORDER BY a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM t2 +} {89 81 96 59 38 68 39 62 91 46 6 99 97 27 46 + 78 54 97 8 67 29 93 84 77 23 16 16 93 65 35 + 47 7 86 74 61 91 85 24 85 43 59 12 32 56 3 91 + 22 90 55 15 28 89 25 47 1 56 40 43 56 16 75 + 36 89 98 76 81 4 94 42 30 78 33 29 53 63 2 87 + 37 80 84 72 41 9 61 73 95 65 13 58 96 98 1 21 + 74 65 35 5 73 11 51 87 41 12 8 20 31 31 15 95 + 22 73 79 88 34 8 11 49 34 90 59 96 60 55 75 + 77 44 2 7 85 57 74 29 70 59 19 39 26 26 47 80 + 90 36 58 47 9 72 72 66 33 93 75 64 81 9 23 37 + 13 12 14 62 91 36 91 33 15 34 36 99 3 95 69 + 58 52 30 50 84 10 84 33 21 39 44 58 30 38 34 + 83 27 82 17 7} + +do_execsql_test 1.16.11.2 { + SELECT first_value(b) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM t2 +} {90 40 30 80 20 90 60 70 80 90 30 50 10 30 81 + 91 61 91 91 1 81 41 61 1 21 11 51 41 31 31 11 + 81 91 91 21 62 12 32 22 42 2 72 12 22 2 72 72 + 12 62 52 82 93 23 93 43 3 43 33 53 63 73 13 + 73 73 33 93 23 13 33 3 33 83 54 84 74 24 4 94 + 84 74 34 34 44 74 64 14 34 84 84 44 34 65 35 + 85 85 55 15 25 75 95 65 65 35 5 15 95 55 75 + 85 75 15 95 96 46 6 46 16 16 86 56 56 56 16 + 36 76 96 96 26 26 36 66 36 36 97 27 97 67 77 + 47 7 47 87 37 87 77 7 57 47 47 37 27 17 7 38 + 68 78 8 28 98 78 58 98 8 88 8 58 58 58 38 89 + 59 39 99 29 59 89 89 29 9 79 49 59 29 59 19 + 39 9 9 99 69 39} + +do_execsql_test 1.16.11.3 { + SELECT first_value(b) OVER ( ORDER BY b,a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 1 2 2 3 3 4 5 6 7 7 7 8 8 8 9 9 9 10 + 11 11 12 12 12 13 13 14 15 15 15 16 16 16 17 + 19 20 21 21 22 22 23 23 24 25 26 26 27 27 28 + 29 29 29 30 30 30 31 31 32 33 33 33 33 34 34 + 34 34 35 35 36 36 36 36 37 37 38 38 39 39 39 + 40 41 41 42 43 43 44 44 46 46 47 47 47 47 49 + 50 51 52 53 54 55 55 56 56 56 57 58 58 58 58 + 59 59 59 59 60 61 61 62 62 63 64 65 65 65 66 + 67 68 69 70 72 72 72 73 73 73 74 74 74 75 75 + 75 76 77 77 78 78 79 80 80 81 81 81 82 83 84 + 84 84 84 85 85 85 86 87 87 88 89 89 89 90 90 + 90 91 91 91 91 91 93 93 93 94 95 95 95 96 96 + 96 97 97 98 98 99 99} + +do_execsql_test 1.16.11.4 { + SELECT first_value(b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {10 20 30 30 30 40 50 60 70 80 80 90 90 90 1 1 + 11 11 21 21 31 31 41 41 51 61 61 81 81 81 91 + 91 91 91 91 2 2 12 12 12 22 22 32 42 52 62 62 + 72 72 72 82 3 3 13 13 23 23 33 33 33 33 43 43 + 53 63 73 73 73 83 93 93 93 4 14 24 34 34 34 + 34 44 44 54 64 74 74 74 84 84 84 84 94 5 15 + 15 15 25 35 35 55 55 65 65 65 75 75 75 85 85 + 85 95 95 95 6 16 16 16 26 26 36 36 36 36 46 + 46 56 56 56 66 76 86 96 96 96 7 7 7 17 27 27 + 37 37 47 47 47 47 57 67 77 77 87 87 97 97 8 8 + 8 28 38 38 58 58 58 58 68 78 78 88 98 98 9 9 + 9 19 29 29 29 39 39 39 49 59 59 59 59 69 79 + 89 89 89 99 99} + +do_execsql_test 1.16.11.5 { + SELECT first_value(b) OVER ( ORDER BY b%10,a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {90 40 30 80 20 90 60 70 80 90 30 50 10 30 81 + 91 61 91 91 1 81 41 61 1 21 11 51 41 31 31 11 + 81 91 91 21 62 12 32 22 42 2 72 12 22 2 72 72 + 12 62 52 82 93 23 93 43 3 43 33 53 63 73 13 + 73 73 33 93 23 13 33 3 33 83 54 84 74 24 4 94 + 84 74 34 34 44 74 64 14 34 84 84 44 34 65 35 + 85 85 55 15 25 75 95 65 65 35 5 15 95 55 75 + 85 75 15 95 96 46 6 46 16 16 86 56 56 56 16 + 36 76 96 96 26 26 36 66 36 36 97 27 97 67 77 + 47 7 47 87 37 87 77 7 57 47 47 37 27 17 7 38 + 68 78 8 28 98 78 58 98 8 88 8 58 58 58 38 89 + 59 39 99 29 59 89 89 29 9 79 49 59 29 59 19 + 39 9 9 99 69 39} + +do_execsql_test 1.16.11.6 { + SELECT first_value(b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM t2 +} {96 38 68 62 46 6 46 78 54 8 84 16 16 86 74 24 + 12 32 56 22 90 28 56 40 56 16 36 98 76 4 94 + 42 30 78 2 80 84 72 58 96 98 74 12 8 20 22 88 + 34 8 34 90 96 60 44 2 74 70 26 26 80 90 36 58 + 72 72 66 64 12 14 62 36 34 36 58 52 30 50 84 + 10 84 44 58 30 38 34 82 89 81 59 39 91 99 97 + 27 97 67 29 93 77 23 93 65 35 47 7 61 91 85 + 85 43 59 3 91 55 15 89 25 47 1 43 75 89 81 33 + 29 53 63 87 37 41 9 61 73 95 65 13 1 21 65 35 + 5 73 11 51 87 41 31 31 15 95 73 79 11 49 59 + 55 75 77 7 85 57 29 59 19 39 47 47 9 33 93 75 + 81 9 23 37 13 91 91 33 15 99 3 95 69 33 21 39 + 83 27 17 7} + +do_execsql_test 1.16.12.1 { + SELECT lead(b,b) OVER (ORDER BY a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM t2 +} {96 9 11 89 32 53 91 30 51 56 54 73 22 59 75 + 74 78 8 16 65 15 8 31 87 90 12 32 96 74 76 37 + 85 90 15 35 2 60 36 75 9 51 47 63 51 90 26 42 + 26 8 76 80 90 37 87 56 79 5 87 8 2 39 73 64 + 36 90 72 78 36 73 51 33 20 41 2 26 37 33 8 14 + 33 81 55 1 9 12 39 64 87 72 34 82 21 34 99 62 + 74 41 69 22 75 27 58 8 79 77 26 26 55 {} 29 + 30 7 {} 66 55 2 34 64 {} 33 {} 44 84 {} {} 95 + 85 19 {} 83 {} 91 {} {} 9 50 91 33 34 {} {} + 84 {} 7 9 {} {} {} 44 {} {} {} {} 91 84 {} 95 + 95 52 {} {} {} {} {} 21 {} {} {} 58 {} {} {} + {} {} {} {} 83 {} {} {} {} {} {} {} {} {} {} + {} {} {} {}} + +do_execsql_test 1.16.12.2 { + SELECT lead(b,b) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 81 {} {} {} 21 {} {} {} {} {} {} + {} {} {} {} {} {} 62 {} {} {} 12 {} {} {} 72 + {} {} {} {} {} {} {} {} {} {} 53 {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 34 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} 95 {} {} {} {} {} {} 85 {} + {} {} {} {} {} {} {} {} {} 56 {} 36 {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 57 {} {} {} {} {} 7 {} {} {} {} + {} {} {} {} {} {} 8 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 9 {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.16.12.3 { + SELECT lead(b,b) OVER ( ORDER BY b,a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 3 5 6 7 8 8 9 9 10 11 12 12 13 13 + 14 15 16 16 17 19 20 21 22 23 24 25 26 27 27 + 28 29 30 31 32 33 33 33 34 34 35 36 36 36 37 + 38 39 39 40 41 42 43 43 44 46 47 47 47 49 50 + 52 53 54 55 56 56 57 58 58 58 59 59 59 60 61 + 62 62 64 65 65 67 69 70 72 72 73 74 74 75 75 + 75 77 78 80 81 81 83 84 84 85 85 85 87 88 89 + 89 89 90 90 91 91 91 93 93 94 95 95 96 97 97 + 98 99 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.16.12.4 { + SELECT lead(b,b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {80 {} {} {} {} {} {} {} {} {} {} {} {} {} 1 + 11 81 81 {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} 12 12 72 82 {} {} {} {} {} {} + {} {} {} {} {} {} 13 23 73 73 {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} 34 84 {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 35 85 85 95 {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} 36 86 96 96 {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 37 47 + 47 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} 58 58 68 {} {} {} {} {} {} {} {} {} + {} {} {} {} 39 49 59 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.16.12.5 { + SELECT lead(b,b) OVER ( ORDER BY b%10,a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {34 72 91 74 21 55 23 94 84 95 2 13 61 22 55 + 55 94 85 75 81 65 73 74 21 72 12 94 33 63 73 + 72 96 36 76 3 25 62 3 73 34 12 46 43 93 72 16 + 86 63 15 65 36 77 24 57 25 53 95 34 95 16 97 + 74 97 67 25 98 44 34 65 54 5 68 96 28 47 95 + 34 39 8 38 6 46 96 28 47 95 56 39 99 97 76 8 + 26 9 79 27 95 16 29 {} 58 58 77 85 56 {} 98 + 29 {} 19 96 {} {} 78 56 98 36 97 {} 89 89 29 + 47 78 {} {} {} 38 68 58 {} 58 38 {} 98 {} {} + {} 39 57 9 {} 79 {} {} 7 {} {} {} 9 29 38 78 + {} {} {} 8 39 {} {} {} {} 59 {} 99 {} {} {} + {} {} {} {} {} {} {} {} {} {} 9 {} {} {} {} + {} {} {} {} {} {} {} {}} + +do_execsql_test 1.16.12.6 { + SELECT lead(b,b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.16.13.1 { + SELECT lag(b,b) OVER (ORDER BY a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} 38 {} {} {} {} + {} {} {} 6 {} {} {} {} {} 81 46 6 {} {} {} {} + 23 {} {} {} {} {} 27 {} {} {} 35 6 {} 12 {} + 23 {} {} 61 84 {} 93 39 47 {} 54 46 96 56 {} + 16 {} {} {} {} 89 {} 16 43 {} 85 56 29 99 53 + {} 59 {} {} 91 59 53 84 99 {} 93 63 47 {} {} + 98 33 67 35 75 1 23 13 55 27 75 98 35 73 63 2 + 21 27 13 24 86 23 84 31 20 94 61 65 75 23 36 + 94 55 90 41 77 96 56 29 40 12 89 63 11 5 73 + 79 1 16 28 31 73 5 39 53 63 41 11 40 2 13 33 + 9 29 90 47 72 9 73 30 44 33 74 93 29 74 42 34 + 63 41 34 96 47 77 1 36 74 72 14 36 26 77 9 72 + 64 8 91 31 52 30} + +do_execsql_test 1.16.13.2 { + SELECT lag(b,b) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} 30 {} {} + {} {} {} {} 91 {} {} {} 61 {} 81 {} {} {} {} + 1 {} {} {} {} {} {} {} {} {} 22 {} {} {} 12 + {} {} 62 {} {} {} {} {} {} {} 23 {} {} {} {} + {} {} {} {} {} {} {} 43 {} 23 {} {} {} {} {} + {} 54 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 75 {} + {} {} {} {} {} 55 {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} 47 {} {} {} {} + {} 27 7 {} {} {} {} {} {} {} {} {} 68 {} 8 {} + {} {} {} {} {} {} {} {} {} {} {} {} 89 {} {} + {} {} {} {} {} 29 9 {} {} {}} + +do_execsql_test 1.16.13.3 { + SELECT lag(b,b) OVER ( ORDER BY b,a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {{} 1 1 1 1 2 2 2 2 2 2 3 3 3 4 4 5 6 6 + 6 7 7 7 7 7 8 8 8 8 8 8 9 9 9 9 9 9 9 + 9 9 9 10 10 10 10 11 11 11 11 11 12 12 12 12 + 13 13 13 13 13 14 15 15 15 15 16 16 16 16 16 + 17 19 20 20 21 21 21 21 22 22 22 22 23 23 23 + 23 23 24 23 24 24 25 26 26 26 26 26 26 26 26 + 26 26 26 27 27 27 27 28 29 29 29 29 30 30 30 + 30 30 30 31 31 31 31 31 32 32 32 32 32 32 31 + 32 33 33 33 33 33 33 34 34 34 34 34 34 34 34 + 35 35 35 35 35 36 36 36 36 36 36 36 37 37 37 + 38 38 38 38 38 38 39 39 39 39 40 40 41 41 42 + 43 42 43 43 43 43 44 44 44 46 46 46 47 47 47 + 47 47} + +do_execsql_test 1.16.13.4 { + SELECT lag(b,b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + 1 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.16.13.5 { + SELECT lag(b,b) OVER ( ORDER BY b%10,a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} 30 {} {} + {} {} {} {} 91 {} {} {} 61 80 81 {} {} {} {} + 1 {} {} {} 30 {} 21 90 61 {} 22 {} 11 41 12 + {} {} 62 {} {} {} {} 31 {} 50 23 30 21 90 {} + {} 62 {} {} 81 {} 22 43 62 23 32 {} 91 {} 90 + 93 54 {} {} 90 72 12 22 90 81 83 23 80 20 72 + 43 51 33 80 90 2 34 54 1 20 62 12 13 75 44 30 + 93 91 1 21 55 61 61 13 85 3 65 65 91 73 33 93 + 55 84 62 31 11 65 35 85 33 55 15 12 75 22 3 + 73 65 36 85 43 95 43 13 47 44 65 65 96 36 27 + 7 46 34 94 47 36 73 34 35 73 68 24 8 75 85 75 + 66 34 95 36 84 77 46 34 84 47 89 65 36 16 38 + 76 58 57 29 9 44 56 17} + +do_execsql_test 1.16.13.6 { + SELECT lag(b,b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.16.14.1 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (ORDER BY a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM t2 +} {89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 33.21.39.44.58.30.38.34.83.27.82.17.7 21.39.44.58.30.38.34.83.27.82.17.7 + 39.44.58.30.38.34.83.27.82.17.7 44.58.30.38.34.83.27.82.17.7 + 58.30.38.34.83.27.82.17.7 30.38.34.83.27.82.17.7 38.34.83.27.82.17.7 + 34.83.27.82.17.7 83.27.82.17.7 27.82.17.7 82.17.7 17.7 7} + +do_execsql_test 1.16.14.2 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM t2 +} {90.40.30.80.20.90.60.70.80.90.30.50.10.30 + 40.30.80.20.90.60.70.80.90.30.50.10.30 + 30.80.20.90.60.70.80.90.30.50.10.30 80.20.90.60.70.80.90.30.50.10.30 + 20.90.60.70.80.90.30.50.10.30 90.60.70.80.90.30.50.10.30 + 60.70.80.90.30.50.10.30 70.80.90.30.50.10.30 80.90.30.50.10.30 + 90.30.50.10.30 30.50.10.30 50.10.30 10.30 30 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 61.1.21.11.51.41.31.31.11.81.91.91.21 1.21.11.51.41.31.31.11.81.91.91.21 + 21.11.51.41.31.31.11.81.91.91.21 11.51.41.31.31.11.81.91.91.21 + 51.41.31.31.11.81.91.91.21 41.31.31.11.81.91.91.21 31.31.11.81.91.91.21 + 31.11.81.91.91.21 11.81.91.91.21 81.91.91.21 91.91.21 91.21 21 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 22.42.2.72.12.22.2.72.72.12.62.52.82 42.2.72.12.22.2.72.72.12.62.52.82 + 2.72.12.22.2.72.72.12.62.52.82 72.12.22.2.72.72.12.62.52.82 + 12.22.2.72.72.12.62.52.82 22.2.72.72.12.62.52.82 2.72.72.12.62.52.82 + 72.72.12.62.52.82 72.12.62.52.82 12.62.52.82 62.52.82 52.82 82 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 63.73.13.73.73.33.93.23.13.33.3.33.83 73.13.73.73.33.93.23.13.33.3.33.83 + 13.73.73.33.93.23.13.33.3.33.83 73.73.33.93.23.13.33.3.33.83 + 73.33.93.23.13.33.3.33.83 33.93.23.13.33.3.33.83 93.23.13.33.3.33.83 + 23.13.33.3.33.83 13.33.3.33.83 33.3.33.83 3.33.83 33.83 83 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 84.74.34.34.44.74.64.14.34.84.84.44.34 + 74.34.34.44.74.64.14.34.84.84.44.34 34.34.44.74.64.14.34.84.84.44.34 + 34.44.74.64.14.34.84.84.44.34 44.74.64.14.34.84.84.44.34 + 74.64.14.34.84.84.44.34 64.14.34.84.84.44.34 14.34.84.84.44.34 + 34.84.84.44.34 84.84.44.34 84.44.34 44.34 34 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 95.65.65.35.5.15.95.55.75.85.75.15.95 65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.5.15.95.55.75.85.75.15.95 35.5.15.95.55.75.85.75.15.95 + 5.15.95.55.75.85.75.15.95 15.95.55.75.85.75.15.95 95.55.75.85.75.15.95 + 55.75.85.75.15.95 75.85.75.15.95 85.75.15.95 75.15.95 15.95 95 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 56.56.16.36.76.96.96.26.26.36.66.36.36 + 56.16.36.76.96.96.26.26.36.66.36.36 16.36.76.96.96.26.26.36.66.36.36 + 36.76.96.96.26.26.36.66.36.36 76.96.96.26.26.36.66.36.36 + 96.96.26.26.36.66.36.36 96.26.26.36.66.36.36 26.26.36.66.36.36 + 26.36.66.36.36 36.66.36.36 66.36.36 36.36 36 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 47.87.37.87.77.7.57.47.47.37.27.17.7 87.37.87.77.7.57.47.47.37.27.17.7 + 37.87.77.7.57.47.47.37.27.17.7 87.77.7.57.47.47.37.27.17.7 + 77.7.57.47.47.37.27.17.7 7.57.47.47.37.27.17.7 57.47.47.37.27.17.7 + 47.47.37.27.17.7 47.37.27.17.7 37.27.17.7 27.17.7 17.7 7 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 8.28.98.78.58.98.8.88.8.58.58.58.38 28.98.78.58.98.8.88.8.58.58.58.38 + 98.78.58.98.8.88.8.58.58.58.38 78.58.98.8.88.8.58.58.58.38 + 58.98.8.88.8.58.58.58.38 98.8.88.8.58.58.58.38 8.88.8.58.58.58.38 + 88.8.58.58.58.38 8.58.58.58.38 58.58.58.38 58.58.38 58.38 38 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 9.79.49.59.29.59.19.39.9.9.99.69.39 79.49.59.29.59.19.39.9.9.99.69.39 + 49.59.29.59.19.39.9.9.99.69.39 59.29.59.19.39.9.9.99.69.39 + 29.59.19.39.9.9.99.69.39 59.19.39.9.9.99.69.39 19.39.9.9.99.69.39 + 39.9.9.99.69.39 9.9.99.69.39 9.99.69.39 99.69.39 69.39 39} + +do_execsql_test 1.16.14.3 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( ORDER BY b,a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 94.95.95.95.96.96.96.97.97.98.98.99.99 + 95.95.95.96.96.96.97.97.98.98.99.99 95.95.96.96.96.97.97.98.98.99.99 + 95.96.96.96.97.97.98.98.99.99 96.96.96.97.97.98.98.99.99 + 96.96.97.97.98.98.99.99 96.97.97.98.98.99.99 97.97.98.98.99.99 + 97.98.98.99.99 98.98.99.99 98.99.99 99.99 99} + +do_execsql_test 1.16.14.4 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {10.20.30.30.30.40.50.60.70.80.80.90.90.90 + 20.30.30.30.40.50.60.70.80.80.90.90.90 + 30.30.30.40.50.60.70.80.80.90.90.90 30.30.40.50.60.70.80.80.90.90.90 + 30.40.50.60.70.80.80.90.90.90 40.50.60.70.80.80.90.90.90 + 50.60.70.80.80.90.90.90 60.70.80.80.90.90.90 70.80.80.90.90.90 + 80.80.90.90.90 80.90.90.90 90.90.90 90.90 90 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 41.41.51.61.61.81.81.81.91.91.91.91.91 + 41.51.61.61.81.81.81.91.91.91.91.91 51.61.61.81.81.81.91.91.91.91.91 + 61.61.81.81.81.91.91.91.91.91 61.81.81.81.91.91.91.91.91 + 81.81.81.91.91.91.91.91 81.81.91.91.91.91.91 81.91.91.91.91.91 + 91.91.91.91.91 91.91.91.91 91.91.91 91.91 91 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 12.12.22.22.32.42.52.62.62.72.72.72.82 + 12.22.22.32.42.52.62.62.72.72.72.82 22.22.32.42.52.62.62.72.72.72.82 + 22.32.42.52.62.62.72.72.72.82 32.42.52.62.62.72.72.72.82 + 42.52.62.62.72.72.72.82 52.62.62.72.72.72.82 62.62.72.72.72.82 + 62.72.72.72.82 72.72.72.82 72.72.82 72.82 82 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 33.33.43.43.53.63.73.73.73.83.93.93.93 + 33.43.43.53.63.73.73.73.83.93.93.93 43.43.53.63.73.73.73.83.93.93.93 + 43.53.63.73.73.73.83.93.93.93 53.63.73.73.73.83.93.93.93 + 63.73.73.73.83.93.93.93 73.73.73.83.93.93.93 73.73.83.93.93.93 + 73.83.93.93.93 83.93.93.93 93.93.93 93.93 93 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 34.44.44.54.64.74.74.74.84.84.84.84.94 + 44.44.54.64.74.74.74.84.84.84.84.94 44.54.64.74.74.74.84.84.84.84.94 + 54.64.74.74.74.84.84.84.84.94 64.74.74.74.84.84.84.84.94 + 74.74.74.84.84.84.84.94 74.74.84.84.84.84.94 74.84.84.84.84.94 + 84.84.84.84.94 84.84.84.94 84.84.94 84.94 94 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 55.65.65.65.75.75.75.85.85.85.95.95.95 + 65.65.65.75.75.75.85.85.85.95.95.95 65.65.75.75.75.85.85.85.95.95.95 + 65.75.75.75.85.85.85.95.95.95 75.75.75.85.85.85.95.95.95 + 75.75.85.85.85.95.95.95 75.85.85.85.95.95.95 85.85.85.95.95.95 + 85.85.95.95.95 85.95.95.95 95.95.95 95.95 95 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 36.36.46.46.56.56.56.66.76.86.96.96.96 + 36.46.46.56.56.56.66.76.86.96.96.96 46.46.56.56.56.66.76.86.96.96.96 + 46.56.56.56.66.76.86.96.96.96 56.56.56.66.76.86.96.96.96 + 56.56.66.76.86.96.96.96 56.66.76.86.96.96.96 66.76.86.96.96.96 + 76.86.96.96.96 86.96.96.96 96.96.96 96.96 96 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 37.47.47.47.47.57.67.77.77.87.87.97.97 + 47.47.47.47.57.67.77.77.87.87.97.97 47.47.47.57.67.77.77.87.87.97.97 + 47.47.57.67.77.77.87.87.97.97 47.57.67.77.77.87.87.97.97 + 57.67.77.77.87.87.97.97 67.77.77.87.87.97.97 77.77.87.87.97.97 + 77.87.87.97.97 87.87.97.97 87.97.97 97.97 97 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 28.38.38.58.58.58.58.68.78.78.88.98.98 + 38.38.58.58.58.58.68.78.78.88.98.98 38.58.58.58.58.68.78.78.88.98.98 + 58.58.58.58.68.78.78.88.98.98 58.58.58.68.78.78.88.98.98 + 58.58.68.78.78.88.98.98 58.68.78.78.88.98.98 68.78.78.88.98.98 + 78.78.88.98.98 78.88.98.98 88.98.98 98.98 98 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 39.49.59.59.59.59.69.79.89.89.89.99.99 + 49.59.59.59.59.69.79.89.89.89.99.99 59.59.59.59.69.79.89.89.89.99.99 + 59.59.59.69.79.89.89.89.99.99 59.59.69.79.89.89.89.99.99 + 59.69.79.89.89.89.99.99 69.79.89.89.89.99.99 79.89.89.89.99.99 + 89.89.89.99.99 89.89.99.99 89.99.99 99.99 99} + +do_execsql_test 1.16.14.5 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( ORDER BY b%10,a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t2 +} {90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 9.79.49.59.29.59.19.39.9.9.99.69.39 79.49.59.29.59.19.39.9.9.99.69.39 + 49.59.29.59.19.39.9.9.99.69.39 59.29.59.19.39.9.9.99.69.39 + 29.59.19.39.9.9.99.69.39 59.19.39.9.9.99.69.39 19.39.9.9.99.69.39 + 39.9.9.99.69.39 9.9.99.69.39 9.99.69.39 99.69.39 69.39 39} + +do_execsql_test 1.16.14.6 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM t2 +} {96 38 68 62 46 6 46 78 54 8 84 16 16 86 74 24 + 12 32 56 22 90 28 56 40 56 16 36 98 76 4 94 + 42 30 78 2 80 84 72 58 96 98 74 12 8 20 22 88 + 34 8 34 90 96 60 44 2 74 70 26 26 80 90 36 58 + 72 72 66 64 12 14 62 36 34 36 58 52 30 50 84 + 10 84 44 58 30 38 34 82 89 81 59 39 91 99 97 + 27 97 67 29 93 77 23 93 65 35 47 7 61 91 85 + 85 43 59 3 91 55 15 89 25 47 1 43 75 89 81 33 + 29 53 63 87 37 41 9 61 73 95 65 13 1 21 65 35 + 5 73 11 51 87 41 31 31 15 95 73 79 11 49 59 + 55 75 77 7 85 57 29 59 19 39 47 47 9 33 93 75 + 81 9 23 37 13 91 91 33 15 99 3 95 69 33 21 39 + 83 27 17 7} + +do_execsql_test 1.16.14.7 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (win1 ORDER BY b%10 ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a) + ORDER BY 1 +} {1 1 10 11 11 12 12 12 13 13 14 15 15 15 16 16 + 16 17 19 2 2 20 21 21 22 22 23 23 24 25 26 26 + 27 27 28 29 29 29 3 3 30 30 30 31 31 32 33 33 + 33 33 34 34 34 34 35 35 36 36 36 36 37 37 38 + 38 39 39 39 4 40 41 41 42 43 43 44 44 46 46 + 47 47 47 47 49 5 50 51 52 53 54 55 55 56 56 + 56 57 58 58 58 58 59 59 59 59 6 60 61 61 62 + 62 63 64 65 65 65 66 67 68 69 7 7 7 70 72 72 + 72 73 73 73 74 74 74 75 75 75 76 77 77 78 78 + 79 8 8 8 80 80 81 81 81 82 83 84 84 84 84 85 + 85 85 86 87 87 88 89 89 89 9 9 9 90 90 90 91 + 91 91 91 91 93 93 93 94 95 95 95 96 96 96 97 + 97 98 98 99 99} + +do_execsql_test 1.16.14.8 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (win1 ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a ORDER BY b%10) + ORDER BY 1 +} {1 1 10 11 11 12 12 12 13 13 14 15 15 15 16 16 + 16 17 19 2 2 20 21 21 22 22 23 23 24 25 26 26 + 27 27 28 29 29 29 3 3 30 30 30 31 31 32 33 33 + 33 33 34 34 34 34 35 35 36 36 36 36 37 37 38 + 38 39 39 39 4 40 41 41 42 43 43 44 44 46 46 + 47 47 47 47 49 5 50 51 52 53 54 55 55 56 56 + 56 57 58 58 58 58 59 59 59 59 6 60 61 61 62 + 62 63 64 65 65 65 66 67 68 69 7 7 7 70 72 72 + 72 73 73 73 74 74 74 75 75 75 76 77 77 78 78 + 79 8 8 8 80 80 81 81 81 82 83 84 84 84 84 85 + 85 85 86 87 87 88 89 89 89 9 9 9 90 90 90 91 + 91 91 91 91 93 93 93 94 95 95 95 96 96 96 97 + 97 98 98 99 99} + +do_execsql_test 1.16.14.9 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER win2 + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a ORDER BY b%10), + win2 AS (win1 ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) + ORDER BY 1 +} {1 1 10 11 11 12 12 12 13 13 14 15 15 15 16 16 + 16 17 19 2 2 20 21 21 22 22 23 23 24 25 26 26 + 27 27 28 29 29 29 3 3 30 30 30 31 31 32 33 33 + 33 33 34 34 34 34 35 35 36 36 36 36 37 37 38 + 38 39 39 39 4 40 41 41 42 43 43 44 44 46 46 + 47 47 47 47 49 5 50 51 52 53 54 55 55 56 56 + 56 57 58 58 58 58 59 59 59 59 6 60 61 61 62 + 62 63 64 65 65 65 66 67 68 69 7 7 7 70 72 72 + 72 73 73 73 74 74 74 75 75 75 76 77 77 78 78 + 79 8 8 8 80 80 81 81 81 82 83 84 84 84 84 85 + 85 85 86 87 87 88 89 89 89 9 9 9 90 90 90 91 + 91 91 91 91 93 93 93 94 95 95 95 96 96 96 97 + 97 98 98 99 99} + +do_execsql_test 1.16.15.1 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE a%2=0) OVER win FROM t2 + WINDOW win AS (ORDER BY a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) +} {191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 190 96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 189 96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 188 38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 187 38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 186 39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 185 39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 184 91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 183 91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 182 6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 181 6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 180 97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 179 97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 178 46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 177 46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 176 54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 175 54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 174 8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 173 8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 172 29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 171 29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 170 84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 169 84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 168 23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 167 23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 166 16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 165 16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 164 65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 163 65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 162 47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 161 47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 160 86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 159 86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 158 61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 157 61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 156 85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 155 85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 154 85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 153 85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 152 59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 151 59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 150 32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 149 32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 148 3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 147 3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 146 22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 145 22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 144 55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 143 55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 142 28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 141 28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 140 25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 139 25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 138 1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 137 1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 136 40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 135 40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 134 56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 133 56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 132 75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 131 75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 130 89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 129 89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 128 76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 127 76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 126 4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 125 4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 124 42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 123 42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 122 78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 121 78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 120 29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 119 29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 118 63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 117 63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 116 87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 115 87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 114 80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 113 80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 112 72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 111 72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 110 9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 109 9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 108 73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 107 73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 106 65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 105 65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 104 58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 103 58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 102 98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 101 98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 100 21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 99 21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 98 65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 97 65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 96 5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 95 5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 94 11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 93 11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 92 87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 91 87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 90 12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 89 12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 88 20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 87 20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 86 31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 85 31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 84 95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 83 95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 82 73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 81 73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 80 88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 79 88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 78 8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 77 8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 76 49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 75 49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 74 90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 73 90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 72 96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 71 96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 70 55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 69 55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 68 77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 67 77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 66 2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 65 2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 64 85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 63 85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 62 74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 61 74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 60 70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 59 70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 58 19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 57 19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 56 26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 55 26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 54 47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 53 47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 52 90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 51 90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 50 58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 49 58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 48 9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 47 9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 46 72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 45 72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 44 33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 43 33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 42 75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 41 75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 40 81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 39 81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 38 23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 37 23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 36 13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 35 13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 34 14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 33 14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 32 91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 31 91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 30 91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 29 91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 28 15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 27 15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 26 36.3.69.52.50.10.33.39.58.38.83.82.7 + 25 36.3.69.52.50.10.33.39.58.38.83.82.7 + 24 3.69.52.50.10.33.39.58.38.83.82.7 23 3.69.52.50.10.33.39.58.38.83.82.7 + 22 69.52.50.10.33.39.58.38.83.82.7 21 69.52.50.10.33.39.58.38.83.82.7 + 20 52.50.10.33.39.58.38.83.82.7 19 52.50.10.33.39.58.38.83.82.7 + 18 50.10.33.39.58.38.83.82.7 17 50.10.33.39.58.38.83.82.7 + 16 10.33.39.58.38.83.82.7 15 10.33.39.58.38.83.82.7 + 14 33.39.58.38.83.82.7 13 33.39.58.38.83.82.7 12 39.58.38.83.82.7 + 11 39.58.38.83.82.7 10 58.38.83.82.7 9 58.38.83.82.7 8 38.83.82.7 + 7 38.83.82.7 6 83.82.7 5 83.82.7 4 82.7 3 82.7 2 7 1 7} + +do_execsql_test 1.16.15.2 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE 0=1) OVER win FROM t2 + WINDOW win AS (ORDER BY a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) +} {191 {} 190 {} 189 {} 188 {} 187 {} 186 {} 185 {} 184 {} + 183 {} 182 {} 181 {} 180 {} 179 {} 178 {} 177 {} 176 {} + 175 {} 174 {} 173 {} 172 {} 171 {} 170 {} 169 {} 168 {} + 167 {} 166 {} 165 {} 164 {} 163 {} 162 {} 161 {} 160 {} + 159 {} 158 {} 157 {} 156 {} 155 {} 154 {} 153 {} 152 {} + 151 {} 150 {} 149 {} 148 {} 147 {} 146 {} 145 {} 144 {} + 143 {} 142 {} 141 {} 140 {} 139 {} 138 {} 137 {} 136 {} + 135 {} 134 {} 133 {} 132 {} 131 {} 130 {} 129 {} 128 {} + 127 {} 126 {} 125 {} 124 {} 123 {} 122 {} 121 {} 120 {} + 119 {} 118 {} 117 {} 116 {} 115 {} 114 {} 113 {} 112 {} + 111 {} 110 {} 109 {} 108 {} 107 {} 106 {} 105 {} 104 {} + 103 {} 102 {} 101 {} 100 {} 99 {} 98 {} 97 {} 96 {} 95 {} + 94 {} 93 {} 92 {} 91 {} 90 {} 89 {} 88 {} 87 {} 86 {} + 85 {} 84 {} 83 {} 82 {} 81 {} 80 {} 79 {} 78 {} 77 {} + 76 {} 75 {} 74 {} 73 {} 72 {} 71 {} 70 {} 69 {} 68 {} + 67 {} 66 {} 65 {} 64 {} 63 {} 62 {} 61 {} 60 {} 59 {} + 58 {} 57 {} 56 {} 55 {} 54 {} 53 {} 52 {} 51 {} 50 {} + 49 {} 48 {} 47 {} 46 {} 45 {} 44 {} 43 {} 42 {} 41 {} + 40 {} 39 {} 38 {} 37 {} 36 {} 35 {} 34 {} 33 {} 32 {} + 31 {} 30 {} 29 {} 28 {} 27 {} 26 {} 25 {} 24 {} 23 {} + 22 {} 21 {} 20 {} 19 {} 18 {} 17 {} 16 {} 15 {} 14 {} + 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} 5 {} 4 {} + 3 {} 2 {} 1 {}} + +do_execsql_test 1.16.15.3 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE 1=0) OVER win FROM t2 + WINDOW win AS (PARTITION BY (a%10) ORDER BY a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) +} {20 {} 19 {} 18 {} 17 {} 16 {} 15 {} 14 {} 13 {} 12 {} + 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} 5 {} 4 {} 3 {} 2 {} + 1 {} 19 {} 18 {} 17 {} 16 {} 15 {} 14 {} 13 {} 12 {} + 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} 5 {} 4 {} 3 {} 2 {} + 1 {} 19 {} 18 {} 17 {} 16 {} 15 {} 14 {} 13 {} 12 {} + 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} 5 {} 4 {} 3 {} 2 {} + 1 {} 19 {} 18 {} 17 {} 16 {} 15 {} 14 {} 13 {} 12 {} + 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} 5 {} 4 {} 3 {} 2 {} + 1 {} 19 {} 18 {} 17 {} 16 {} 15 {} 14 {} 13 {} 12 {} + 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} 5 {} 4 {} 3 {} 2 {} + 1 {} 19 {} 18 {} 17 {} 16 {} 15 {} 14 {} 13 {} 12 {} + 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} 5 {} 4 {} 3 {} 2 {} + 1 {} 19 {} 18 {} 17 {} 16 {} 15 {} 14 {} 13 {} 12 {} + 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} 5 {} 4 {} 3 {} 2 {} + 1 {} 19 {} 18 {} 17 {} 16 {} 15 {} 14 {} 13 {} 12 {} + 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} 5 {} 4 {} 3 {} 2 {} + 1 {} 19 {} 18 {} 17 {} 16 {} 15 {} 14 {} 13 {} 12 {} + 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} 5 {} 4 {} 3 {} 2 {} + 1 {} 19 {} 18 {} 17 {} 16 {} 15 {} 14 {} 13 {} 12 {} + 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} 5 {} 4 {} 3 {} 2 {} + 1 {}} + +do_execsql_test 1.16.15.4 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE a%2=0) OVER win FROM t2 + WINDOW win AS (PARTITION BY (a%10) ORDER BY a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) +} {20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 19 6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 18 29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 17 47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 16 59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 15 28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 14 75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 13 78.72.98.87.73.96.74.90.75.91.69.39.7 + 12 72.98.87.73.96.74.90.75.91.69.39.7 11 98.87.73.96.74.90.75.91.69.39.7 + 10 87.73.96.74.90.75.91.69.39.7 9 73.96.74.90.75.91.69.39.7 + 8 96.74.90.75.91.69.39.7 7 74.90.75.91.69.39.7 6 90.75.91.69.39.7 + 5 75.91.69.39.7 4 91.69.39.7 3 69.39.7 2 39.7 1 7 19 {} 18 {} + 17 {} 16 {} 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} + 7 {} 6 {} 5 {} 4 {} 3 {} 2 {} 1 {} + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 18 97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 17 84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 16 86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 15 32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 14 25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 13 89.29.9.21.12.88.55.70.58.81.91.52.58 + 12 29.9.21.12.88.55.70.58.81.91.52.58 11 9.21.12.88.55.70.58.81.91.52.58 + 10 21.12.88.55.70.58.81.91.52.58 9 12.88.55.70.58.81.91.52.58 + 8 88.55.70.58.81.91.52.58 7 55.70.58.81.91.52.58 6 70.58.81.91.52.58 + 5 58.81.91.52.58 4 81.91.52.58 3 91.52.58 2 52.58 1 58 19 {} + 18 {} 17 {} 16 {} 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} + 9 {} 8 {} 7 {} 6 {} 5 {} 4 {} 3 {} 2 {} 1 {} + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 18 46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 17 23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 16 61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 15 3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 14 1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 13 76.63.73.65.20.8.77.19.9.23.15.50.38 + 12 63.73.65.20.8.77.19.9.23.15.50.38 11 73.65.20.8.77.19.9.23.15.50.38 + 10 65.20.8.77.19.9.23.15.50.38 9 20.8.77.19.9.23.15.50.38 + 8 8.77.19.9.23.15.50.38 7 77.19.9.23.15.50.38 6 19.9.23.15.50.38 + 5 9.23.15.50.38 4 23.15.50.38 3 15.50.38 2 50.38 1 38 19 {} + 18 {} 17 {} 16 {} 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} + 9 {} 8 {} 7 {} 6 {} 5 {} 4 {} 3 {} 2 {} 1 {} + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 18 54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 17 16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 16 85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 15 22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 14 40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 13 4.87.65.5.31.49.2.26.72.13.36.10.83 + 12 87.65.5.31.49.2.26.72.13.36.10.83 11 65.5.31.49.2.26.72.13.36.10.83 + 10 5.31.49.2.26.72.13.36.10.83 9 31.49.2.26.72.13.36.10.83 + 8 49.2.26.72.13.36.10.83 7 2.26.72.13.36.10.83 6 26.72.13.36.10.83 + 5 72.13.36.10.83 4 13.36.10.83 3 36.10.83 2 10.83 1 83 19 {} + 18 {} 17 {} 16 {} 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} + 9 {} 8 {} 7 {} 6 {} 5 {} 4 {} 3 {} 2 {} 1 {} + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 18 8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 17 65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 16 85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 15 55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 14 56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 13 42.80.58.11.95.90.85.47.33.14.3.33.82 + 12 80.58.11.95.90.85.47.33.14.3.33.82 11 58.11.95.90.85.47.33.14.3.33.82 + 10 11.95.90.85.47.33.14.3.33.82 9 95.90.85.47.33.14.3.33.82 + 8 90.85.47.33.14.3.33.82 7 85.47.33.14.3.33.82 6 47.33.14.3.33.82 + 5 33.14.3.33.82 4 14.3.33.82 3 3.33.82 2 33.82 1 82 19 {} 18 {} + 17 {} 16 {} 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} + 7 {} 6 {} 5 {} 4 {} 3 {} 2 {} 1 {}} + +do_execsql_test 1.17.2.1 { + SELECT max(b) OVER ( ORDER BY a ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2 +} {99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 95 + 95 84 84 84 84 84 84 84 84 83 83 83 83 83 83 + 83 83 83 82 82 17 7 {} {} {} {}} + +do_execsql_test 1.17.2.2 { + SELECT min(b) OVER ( ORDER BY a ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 7 7 7 7 7 7 + 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 {} {} + {} {}} + +do_execsql_test 1.17.3.1 { + SELECT row_number() OVER ( ORDER BY a ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.17.3.2 { + SELECT row_number() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.17.3.3 { + SELECT row_number() OVER ( ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.17.4.1 { + SELECT dense_rank() OVER ( ORDER BY a ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.17.4.2 { + SELECT dense_rank() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.17.4.3 { + SELECT dense_rank() OVER ( ORDER BY b ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 1 2 2 3 3 4 5 6 7 7 7 8 8 8 9 9 9 10 + 11 11 12 12 12 13 13 14 15 15 15 16 16 16 17 + 18 19 20 20 21 21 22 22 23 24 25 25 26 26 27 + 28 28 28 29 29 29 30 30 31 32 32 32 32 33 33 + 33 33 34 34 35 35 35 35 36 36 37 37 38 38 38 + 39 40 40 41 42 42 43 43 44 44 45 45 45 45 46 + 47 48 49 50 51 52 52 53 53 53 54 55 55 55 55 + 56 56 56 56 57 58 58 59 59 60 61 62 62 62 63 + 64 65 66 67 68 68 68 69 69 69 70 70 70 71 71 + 71 72 73 73 74 74 75 76 76 77 77 77 78 79 80 + 80 80 80 81 81 81 82 83 83 84 85 85 85 86 86 + 86 87 87 87 87 87 88 88 88 89 90 90 90 91 91 + 91 92 92 93 93 94 94} + +do_execsql_test 1.17.4.4 { + SELECT dense_rank() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 3 3 4 5 6 7 8 8 9 9 9 1 1 2 2 3 + 3 4 4 5 5 6 7 7 8 8 8 9 9 9 9 9 1 1 2 + 2 2 3 3 4 5 6 7 7 8 8 8 9 1 1 2 2 3 3 + 4 4 4 4 5 5 6 7 8 8 8 9 10 10 10 1 2 3 + 4 4 4 4 5 5 6 7 8 8 8 9 9 9 9 10 1 2 2 + 2 3 4 4 5 5 6 6 6 7 7 7 8 8 8 9 9 9 1 + 2 2 2 3 3 4 4 4 4 5 5 6 6 6 7 8 9 10 10 + 10 1 1 1 2 3 3 4 4 5 5 5 5 6 7 8 8 9 9 + 10 10 1 1 1 2 3 3 4 4 4 4 5 6 6 7 8 8 1 + 1 1 2 3 3 3 4 4 4 5 6 6 6 6 7 8 9 9 9 + 10 10} + +do_execsql_test 1.17.4.5 { + SELECT dense_rank() OVER ( ORDER BY b%10 ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 + 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 + 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 + 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 + 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 + 8 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 10 10 + 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 + 10 10 10 10 10} + +do_execsql_test 1.17.4.6 { + SELECT dense_rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 + 5 5 5 5 5 5 5 5 5 5 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 + 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 + 5} + +do_execsql_test 1.17.5.1 { + SELECT rank() OVER ( ORDER BY a ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.17.5.2 { + SELECT rank() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.17.5.3 { + SELECT rank() OVER ( ORDER BY b ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 1 3 3 5 5 7 8 9 10 10 10 13 13 13 16 16 + 16 19 20 20 22 22 22 25 25 27 28 28 28 31 31 + 31 34 35 36 37 37 39 39 41 41 43 44 45 45 47 + 47 49 50 50 50 53 53 53 56 56 58 59 59 59 59 + 63 63 63 63 67 67 69 69 69 69 73 73 75 75 77 + 77 77 80 81 81 83 84 84 86 86 88 88 90 90 90 + 90 94 95 96 97 98 99 100 100 102 102 102 105 106 + 106 106 106 110 110 110 110 114 115 115 117 117 119 + 120 121 121 121 124 125 126 127 128 129 129 129 132 + 132 132 135 135 135 138 138 138 141 142 142 144 144 + 146 147 147 149 149 149 152 153 154 154 154 154 158 + 158 158 161 162 162 164 165 165 165 168 168 168 171 + 171 171 171 171 176 176 176 179 180 180 180 183 183 + 183 186 186 188 188 190 190} + +do_execsql_test 1.17.5.4 { + SELECT rank() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 3 3 6 7 8 9 10 10 12 12 12 1 1 3 3 + 5 5 7 7 9 9 11 12 12 14 14 14 17 17 17 17 + 17 1 1 3 3 3 6 6 8 9 10 11 11 13 13 13 16 + 1 1 3 3 5 5 7 7 7 7 11 11 13 14 15 15 15 + 18 19 19 19 1 2 3 4 4 4 4 8 8 10 11 12 12 + 12 15 15 15 15 19 1 2 2 2 5 6 6 8 8 10 10 + 10 13 13 13 16 16 16 19 19 19 1 2 2 2 5 5 7 + 7 7 7 11 11 13 13 13 16 17 18 19 19 19 1 1 + 1 4 5 5 7 7 9 9 9 9 13 14 15 15 17 17 19 + 19 1 1 1 4 5 5 7 7 7 7 11 12 12 14 15 15 + 1 1 1 4 5 5 5 8 8 8 11 12 12 12 12 16 17 + 18 18 18 21 21} + +do_execsql_test 1.17.5.5 { + SELECT rank() OVER ( ORDER BY b%10 ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 15 15 15 15 + 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 + 15 15 36 36 36 36 36 36 36 36 36 36 36 36 36 + 36 36 36 52 52 52 52 52 52 52 52 52 52 52 52 + 52 52 52 52 52 52 52 52 52 73 73 73 73 73 73 + 73 73 73 73 73 73 73 73 73 73 73 73 73 92 92 + 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 + 92 92 92 92 113 113 113 113 113 113 113 113 113 + 113 113 113 113 113 113 113 113 113 113 113 113 134 + 134 134 134 134 134 134 134 134 134 134 134 134 134 + 134 134 134 134 134 134 154 154 154 154 154 154 154 + 154 154 154 154 154 154 154 154 154 170 170 170 170 + 170 170 170 170 170 170 170 170 170 170 170 170 170 + 170 170 170 170 170} + +do_execsql_test 1.17.5.6 { + SELECT rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 15 15 15 15 + 15 15 15 15 15 15 15 15 15 15 15 15 31 31 31 + 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 + 31 50 50 50 50 50 50 50 50 50 50 50 50 50 50 + 50 50 50 50 50 50 50 71 71 71 71 71 71 71 71 + 71 71 71 71 71 71 71 71 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 22 22 22 22 22 22 + 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 + 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 + 43 43 43 43 43 43 64 64 64 64 64 64 64 64 64 + 64 64 64 64 64 64 64 64 64 64 64 84 84 84 84 + 84 84 84 84 84 84 84 84 84 84 84 84 84 84 84 + 84 84 84} + +do_execsql_test 1.17.6.1 { + SELECT + row_number() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ), + rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ), + dense_rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) + FROM t2 +} {1 1 1 2 1 1 3 1 1 4 1 1 5 1 1 6 1 1 7 1 1 8 1 1 9 1 1 + 10 1 1 11 1 1 12 1 1 13 1 1 14 1 1 15 15 2 16 15 2 17 15 2 + 18 15 2 19 15 2 20 15 2 21 15 2 22 15 2 23 15 2 24 15 2 + 25 15 2 26 15 2 27 15 2 28 15 2 29 15 2 30 15 2 31 31 3 + 32 31 3 33 31 3 34 31 3 35 31 3 36 31 3 37 31 3 38 31 3 + 39 31 3 40 31 3 41 31 3 42 31 3 43 31 3 44 31 3 45 31 3 + 46 31 3 47 31 3 48 31 3 49 31 3 50 50 4 51 50 4 52 50 4 + 53 50 4 54 50 4 55 50 4 56 50 4 57 50 4 58 50 4 59 50 4 + 60 50 4 61 50 4 62 50 4 63 50 4 64 50 4 65 50 4 66 50 4 + 67 50 4 68 50 4 69 50 4 70 50 4 71 71 5 72 71 5 73 71 5 + 74 71 5 75 71 5 76 71 5 77 71 5 78 71 5 79 71 5 80 71 5 + 81 71 5 82 71 5 83 71 5 84 71 5 85 71 5 86 71 5 1 1 1 2 1 1 + 3 1 1 4 1 1 5 1 1 6 1 1 7 1 1 8 1 1 9 1 1 10 1 1 11 1 1 + 12 1 1 13 1 1 14 1 1 15 1 1 16 1 1 17 1 1 18 1 1 19 1 1 + 20 1 1 21 1 1 22 22 2 23 22 2 24 22 2 25 22 2 26 22 2 27 22 2 + 28 22 2 29 22 2 30 22 2 31 22 2 32 22 2 33 22 2 34 22 2 + 35 22 2 36 22 2 37 22 2 38 22 2 39 22 2 40 22 2 41 22 2 + 42 22 2 43 43 3 44 43 3 45 43 3 46 43 3 47 43 3 48 43 3 + 49 43 3 50 43 3 51 43 3 52 43 3 53 43 3 54 43 3 55 43 3 + 56 43 3 57 43 3 58 43 3 59 43 3 60 43 3 61 43 3 62 43 3 + 63 43 3 64 64 4 65 64 4 66 64 4 67 64 4 68 64 4 69 64 4 + 70 64 4 71 64 4 72 64 4 73 64 4 74 64 4 75 64 4 76 64 4 + 77 64 4 78 64 4 79 64 4 80 64 4 81 64 4 82 64 4 83 64 4 + 84 84 5 85 84 5 86 84 5 87 84 5 88 84 5 89 84 5 90 84 5 + 91 84 5 92 84 5 93 84 5 94 84 5 95 84 5 96 84 5 97 84 5 + 98 84 5 99 84 5 100 84 5 101 84 5 102 84 5 103 84 5 104 84 5 + 105 84 5} + + +do_test 1.17.7.1 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY a ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0053 0.0105 0.0158 0.0211 0.0263 0.0316 0.0368 0.0421 0.0474 0.0526 0.0579 0.0632 0.0684 0.0737 0.0789 0.0842 0.0895 0.0947 0.1000 0.1053 0.1105 0.1158 0.1211 0.1263 0.1316 0.1368 0.1421 0.1474 0.1526 0.1579 0.1632 0.1684 0.1737 0.1789 0.1842 0.1895 0.1947 0.2000 0.2053 0.2105 0.2158 0.2211 0.2263 0.2316 0.2368 0.2421 0.2474 0.2526 0.2579 0.2632 0.2684 0.2737 0.2789 0.2842 0.2895 0.2947 0.3000 0.3053 0.3105 0.3158 0.3211 0.3263 0.3316 0.3368 0.3421 0.3474 0.3526 0.3579 0.3632 0.3684 0.3737 0.3789 0.3842 0.3895 0.3947 0.4000 0.4053 0.4105 0.4158 0.4211 0.4263 0.4316 0.4368 0.4421 0.4474 0.4526 0.4579 0.4632 0.4684 0.4737 0.4789 0.4842 0.4895 0.4947 0.5000 0.5053 0.5105 0.5158 0.5211 0.5263 0.5316 0.5368 0.5421 0.5474 0.5526 0.5579 0.5632 0.5684 0.5737 0.5789 0.5842 0.5895 0.5947 0.6000 0.6053 0.6105 0.6158 0.6211 0.6263 0.6316 0.6368 0.6421 0.6474 0.6526 0.6579 0.6632 0.6684 0.6737 0.6789 0.6842 0.6895 0.6947 0.7000 0.7053 0.7105 0.7158 0.7211 0.7263 0.7316 0.7368 0.7421 0.7474 0.7526 0.7579 0.7632 0.7684 0.7737 0.7789 0.7842 0.7895 0.7947 0.8000 0.8053 0.8105 0.8158 0.8211 0.8263 0.8316 0.8368 0.8421 0.8474 0.8526 0.8579 0.8632 0.8684 0.8737 0.8789 0.8842 0.8895 0.8947 0.9000 0.9053 0.9105 0.9158 0.9211 0.9263 0.9316 0.9368 0.9421 0.9474 0.9526 0.9579 0.9632 0.9684 0.9737 0.9789 0.9842 0.9895 0.9947 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.17.7.2 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0769 0.1538 0.2308 0.3077 0.3846 0.4615 0.5385 0.6154 0.6923 0.7692 0.8462 0.9231 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0667 0.1333 0.2000 0.2667 0.3333 0.4000 0.4667 0.5333 0.6000 0.6667 0.7333 0.8000 0.8667 0.9333 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0556 0.1111 0.1667 0.2222 0.2778 0.3333 0.3889 0.4444 0.5000 0.5556 0.6111 0.6667 0.7222 0.7778 0.8333 0.8889 0.9444 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0526 0.1053 0.1579 0.2105 0.2632 0.3158 0.3684 0.4211 0.4737 0.5263 0.5789 0.6316 0.6842 0.7368 0.7895 0.8421 0.8947 0.9474 1.0000 0.0000 0.0667 0.1333 0.2000 0.2667 0.3333 0.4000 0.4667 0.5333 0.6000 0.6667 0.7333 0.8000 0.8667 0.9333 1.0000 0.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.17.7.3 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY b ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0105 0.0105 0.0211 0.0211 0.0316 0.0368 0.0421 0.0474 0.0474 0.0474 0.0632 0.0632 0.0632 0.0789 0.0789 0.0789 0.0947 0.1000 0.1000 0.1105 0.1105 0.1105 0.1263 0.1263 0.1368 0.1421 0.1421 0.1421 0.1579 0.1579 0.1579 0.1737 0.1789 0.1842 0.1895 0.1895 0.2000 0.2000 0.2105 0.2105 0.2211 0.2263 0.2316 0.2316 0.2421 0.2421 0.2526 0.2579 0.2579 0.2579 0.2737 0.2737 0.2737 0.2895 0.2895 0.3000 0.3053 0.3053 0.3053 0.3053 0.3263 0.3263 0.3263 0.3263 0.3474 0.3474 0.3579 0.3579 0.3579 0.3579 0.3789 0.3789 0.3895 0.3895 0.4000 0.4000 0.4000 0.4158 0.4211 0.4211 0.4316 0.4368 0.4368 0.4474 0.4474 0.4579 0.4579 0.4684 0.4684 0.4684 0.4684 0.4895 0.4947 0.5000 0.5053 0.5105 0.5158 0.5211 0.5211 0.5316 0.5316 0.5316 0.5474 0.5526 0.5526 0.5526 0.5526 0.5737 0.5737 0.5737 0.5737 0.5947 0.6000 0.6000 0.6105 0.6105 0.6211 0.6263 0.6316 0.6316 0.6316 0.6474 0.6526 0.6579 0.6632 0.6684 0.6737 0.6737 0.6737 0.6895 0.6895 0.6895 0.7053 0.7053 0.7053 0.7211 0.7211 0.7211 0.7368 0.7421 0.7421 0.7526 0.7526 0.7632 0.7684 0.7684 0.7789 0.7789 0.7789 0.7947 0.8000 0.8053 0.8053 0.8053 0.8053 0.8263 0.8263 0.8263 0.8421 0.8474 0.8474 0.8579 0.8632 0.8632 0.8632 0.8789 0.8789 0.8789 0.8947 0.8947 0.8947 0.8947 0.8947 0.9211 0.9211 0.9211 0.9368 0.9421 0.9421 0.9421 0.9579 0.9579 0.9579 0.9737 0.9737 0.9842 0.9842 0.9947 0.9947} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.17.7.4 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0769 0.1538 0.1538 0.1538 0.3846 0.4615 0.5385 0.6154 0.6923 0.6923 0.8462 0.8462 0.8462 0.0000 0.0000 0.1000 0.1000 0.2000 0.2000 0.3000 0.3000 0.4000 0.4000 0.5000 0.5500 0.5500 0.6500 0.6500 0.6500 0.8000 0.8000 0.8000 0.8000 0.8000 0.0000 0.0000 0.1333 0.1333 0.1333 0.3333 0.3333 0.4667 0.5333 0.6000 0.6667 0.6667 0.8000 0.8000 0.8000 1.0000 0.0000 0.0000 0.1000 0.1000 0.2000 0.2000 0.3000 0.3000 0.3000 0.3000 0.5000 0.5000 0.6000 0.6500 0.7000 0.7000 0.7000 0.8500 0.9000 0.9000 0.9000 0.0000 0.0556 0.1111 0.1667 0.1667 0.1667 0.1667 0.3889 0.3889 0.5000 0.5556 0.6111 0.6111 0.6111 0.7778 0.7778 0.7778 0.7778 1.0000 0.0000 0.0500 0.0500 0.0500 0.2000 0.2500 0.2500 0.3500 0.3500 0.4500 0.4500 0.4500 0.6000 0.6000 0.6000 0.7500 0.7500 0.7500 0.9000 0.9000 0.9000 0.0000 0.0500 0.0500 0.0500 0.2000 0.2000 0.3000 0.3000 0.3000 0.3000 0.5000 0.5000 0.6000 0.6000 0.6000 0.7500 0.8000 0.8500 0.9000 0.9000 0.9000 0.0000 0.0000 0.0000 0.1579 0.2105 0.2105 0.3158 0.3158 0.4211 0.4211 0.4211 0.4211 0.6316 0.6842 0.7368 0.7368 0.8421 0.8421 0.9474 0.9474 0.0000 0.0000 0.0000 0.2000 0.2667 0.2667 0.4000 0.4000 0.4000 0.4000 0.6667 0.7333 0.7333 0.8667 0.9333 0.9333 0.0000 0.0000 0.0000 0.1429 0.1905 0.1905 0.1905 0.3333 0.3333 0.3333 0.4762 0.5238 0.5238 0.5238 0.5238 0.7143 0.7619 0.8095 0.8095 0.8095 0.9524 0.9524} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.17.7.5 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY b%10 ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.17.7.6 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER (PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.17.8.1 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY a ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0052 0.0105 0.0157 0.0209 0.0262 0.0314 0.0366 0.0419 0.0471 0.0524 0.0576 0.0628 0.0681 0.0733 0.0785 0.0838 0.0890 0.0942 0.0995 0.1047 0.1099 0.1152 0.1204 0.1257 0.1309 0.1361 0.1414 0.1466 0.1518 0.1571 0.1623 0.1675 0.1728 0.1780 0.1832 0.1885 0.1937 0.1990 0.2042 0.2094 0.2147 0.2199 0.2251 0.2304 0.2356 0.2408 0.2461 0.2513 0.2565 0.2618 0.2670 0.2723 0.2775 0.2827 0.2880 0.2932 0.2984 0.3037 0.3089 0.3141 0.3194 0.3246 0.3298 0.3351 0.3403 0.3455 0.3508 0.3560 0.3613 0.3665 0.3717 0.3770 0.3822 0.3874 0.3927 0.3979 0.4031 0.4084 0.4136 0.4188 0.4241 0.4293 0.4346 0.4398 0.4450 0.4503 0.4555 0.4607 0.4660 0.4712 0.4764 0.4817 0.4869 0.4921 0.4974 0.5026 0.5079 0.5131 0.5183 0.5236 0.5288 0.5340 0.5393 0.5445 0.5497 0.5550 0.5602 0.5654 0.5707 0.5759 0.5812 0.5864 0.5916 0.5969 0.6021 0.6073 0.6126 0.6178 0.6230 0.6283 0.6335 0.6387 0.6440 0.6492 0.6545 0.6597 0.6649 0.6702 0.6754 0.6806 0.6859 0.6911 0.6963 0.7016 0.7068 0.7120 0.7173 0.7225 0.7277 0.7330 0.7382 0.7435 0.7487 0.7539 0.7592 0.7644 0.7696 0.7749 0.7801 0.7853 0.7906 0.7958 0.8010 0.8063 0.8115 0.8168 0.8220 0.8272 0.8325 0.8377 0.8429 0.8482 0.8534 0.8586 0.8639 0.8691 0.8743 0.8796 0.8848 0.8901 0.8953 0.9005 0.9058 0.9110 0.9162 0.9215 0.9267 0.9319 0.9372 0.9424 0.9476 0.9529 0.9581 0.9634 0.9686 0.9738 0.9791 0.9843 0.9895 0.9948 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.17.8.2 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0714 0.1429 0.2143 0.2857 0.3571 0.4286 0.5000 0.5714 0.6429 0.7143 0.7857 0.8571 0.9286 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0625 0.1250 0.1875 0.2500 0.3125 0.3750 0.4375 0.5000 0.5625 0.6250 0.6875 0.7500 0.8125 0.8750 0.9375 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0526 0.1053 0.1579 0.2105 0.2632 0.3158 0.3684 0.4211 0.4737 0.5263 0.5789 0.6316 0.6842 0.7368 0.7895 0.8421 0.8947 0.9474 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0625 0.1250 0.1875 0.2500 0.3125 0.3750 0.4375 0.5000 0.5625 0.6250 0.6875 0.7500 0.8125 0.8750 0.9375 1.0000 0.0455 0.0909 0.1364 0.1818 0.2273 0.2727 0.3182 0.3636 0.4091 0.4545 0.5000 0.5455 0.5909 0.6364 0.6818 0.7273 0.7727 0.8182 0.8636 0.9091 0.9545 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.17.8.3 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY b ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0105 0.0105 0.0209 0.0209 0.0314 0.0314 0.0366 0.0419 0.0471 0.0628 0.0628 0.0628 0.0785 0.0785 0.0785 0.0942 0.0942 0.0942 0.0995 0.1099 0.1099 0.1257 0.1257 0.1257 0.1361 0.1361 0.1414 0.1571 0.1571 0.1571 0.1728 0.1728 0.1728 0.1780 0.1832 0.1885 0.1990 0.1990 0.2094 0.2094 0.2199 0.2199 0.2251 0.2304 0.2408 0.2408 0.2513 0.2513 0.2565 0.2723 0.2723 0.2723 0.2880 0.2880 0.2880 0.2984 0.2984 0.3037 0.3246 0.3246 0.3246 0.3246 0.3455 0.3455 0.3455 0.3455 0.3560 0.3560 0.3770 0.3770 0.3770 0.3770 0.3874 0.3874 0.3979 0.3979 0.4136 0.4136 0.4136 0.4188 0.4293 0.4293 0.4346 0.4450 0.4450 0.4555 0.4555 0.4660 0.4660 0.4869 0.4869 0.4869 0.4869 0.4921 0.4974 0.5026 0.5079 0.5131 0.5183 0.5288 0.5288 0.5445 0.5445 0.5445 0.5497 0.5707 0.5707 0.5707 0.5707 0.5916 0.5916 0.5916 0.5916 0.5969 0.6073 0.6073 0.6178 0.6178 0.6230 0.6283 0.6440 0.6440 0.6440 0.6492 0.6545 0.6597 0.6649 0.6702 0.6859 0.6859 0.6859 0.7016 0.7016 0.7016 0.7173 0.7173 0.7173 0.7330 0.7330 0.7330 0.7382 0.7487 0.7487 0.7592 0.7592 0.7644 0.7749 0.7749 0.7906 0.7906 0.7906 0.7958 0.8010 0.8220 0.8220 0.8220 0.8220 0.8377 0.8377 0.8377 0.8429 0.8534 0.8534 0.8586 0.8743 0.8743 0.8743 0.8901 0.8901 0.8901 0.9162 0.9162 0.9162 0.9162 0.9162 0.9319 0.9319 0.9319 0.9372 0.9529 0.9529 0.9529 0.9686 0.9686 0.9686 0.9791 0.9791 0.9895 0.9895 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.17.8.4 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0714 0.1429 0.3571 0.3571 0.3571 0.4286 0.5000 0.5714 0.6429 0.7857 0.7857 1.0000 1.0000 1.0000 0.0952 0.0952 0.1905 0.1905 0.2857 0.2857 0.3810 0.3810 0.4762 0.4762 0.5238 0.6190 0.6190 0.7619 0.7619 0.7619 1.0000 1.0000 1.0000 1.0000 1.0000 0.1250 0.1250 0.3125 0.3125 0.3125 0.4375 0.4375 0.5000 0.5625 0.6250 0.7500 0.7500 0.9375 0.9375 0.9375 1.0000 0.0952 0.0952 0.1905 0.1905 0.2857 0.2857 0.4762 0.4762 0.4762 0.4762 0.5714 0.5714 0.6190 0.6667 0.8095 0.8095 0.8095 0.8571 1.0000 1.0000 1.0000 0.0526 0.1053 0.1579 0.3684 0.3684 0.3684 0.3684 0.4737 0.4737 0.5263 0.5789 0.7368 0.7368 0.7368 0.9474 0.9474 0.9474 0.9474 1.0000 0.0476 0.1905 0.1905 0.1905 0.2381 0.3333 0.3333 0.4286 0.4286 0.5714 0.5714 0.5714 0.7143 0.7143 0.7143 0.8571 0.8571 0.8571 1.0000 1.0000 1.0000 0.0476 0.1905 0.1905 0.1905 0.2857 0.2857 0.4762 0.4762 0.4762 0.4762 0.5714 0.5714 0.7143 0.7143 0.7143 0.7619 0.8095 0.8571 1.0000 1.0000 1.0000 0.1500 0.1500 0.1500 0.2000 0.3000 0.3000 0.4000 0.4000 0.6000 0.6000 0.6000 0.6000 0.6500 0.7000 0.8000 0.8000 0.9000 0.9000 1.0000 1.0000 0.1875 0.1875 0.1875 0.2500 0.3750 0.3750 0.6250 0.6250 0.6250 0.6250 0.6875 0.8125 0.8125 0.8750 1.0000 1.0000 0.1364 0.1364 0.1364 0.1818 0.3182 0.3182 0.3182 0.4545 0.4545 0.4545 0.5000 0.6818 0.6818 0.6818 0.6818 0.7273 0.7727 0.9091 0.9091 0.9091 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.17.8.5 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY b%10 ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.17.8.6 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.17.8.1 { + set myres {} + foreach r [db eval {SELECT ntile(100) OVER ( ORDER BY a ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 88.0000 89.0000 89.0000 90.0000 90.0000 91.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.17.8.2 { + set myres {} + foreach r [db eval {SELECT ntile(101) OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 22.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.17.8.3 { + set myres {} + foreach r [db eval {SELECT ntile(102) OVER ( ORDER BY b,a ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 88.0000 89.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.17.8.4 { + set myres {} + foreach r [db eval {SELECT ntile(103) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 22.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.17.8.5 { + set myres {} + foreach r [db eval {SELECT ntile(104) OVER ( ORDER BY b%10,a ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000 103.0000 104.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.17.8.6 { + set myres {} + foreach r [db eval {SELECT ntile(105) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.17.8.7 { + set myres {} + foreach r [db eval {SELECT ntile(105) OVER ( ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 88.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000 103.0000 104.0000 105.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + +do_execsql_test 1.17.9.1 { + SELECT last_value(a+b) OVER ( ORDER BY a ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2 +} {207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 {} {} {} {}} + +do_execsql_test 1.17.9.2 { + SELECT last_value(a+b) OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2 +} {223 223 223 223 223 223 223 223 223 223 {} {} {} + {} 210 210 210 210 210 210 210 210 210 210 210 210 + 210 210 210 210 210 {} {} {} {} 280 280 280 280 + 280 280 280 280 280 280 280 280 {} {} {} {} 279 + 279 279 279 279 279 279 279 279 279 279 279 279 279 + 279 279 279 {} {} {} {} 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 {} {} {} {} + 274 274 274 274 274 274 274 274 274 274 274 274 274 + 274 274 274 274 {} {} {} {} 212 212 212 212 212 + 212 212 212 212 212 212 212 212 212 212 212 212 {} + {} {} {} 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 {} {} {} {} 232 232 232 + 232 232 232 232 232 232 232 232 232 {} {} {} {} + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 {} {} {} {}} + +do_execsql_test 1.17.9.3 { + SELECT last_value(a+b) OVER ( ORDER BY b,a ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2 +} {276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 {} {} {} {}} + +do_execsql_test 1.17.9.4 { + SELECT last_value(a+b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2 +} {240 240 240 240 240 240 240 240 240 240 {} {} {} + {} 263 263 263 263 263 263 263 263 263 263 263 263 + 263 263 263 263 263 {} {} {} {} 280 280 280 280 + 280 280 280 280 280 280 280 280 {} {} {} {} 252 + 252 252 252 252 252 252 252 252 252 252 252 252 252 + 252 252 252 {} {} {} {} 171 171 171 171 171 171 + 171 171 171 171 171 171 171 171 171 {} {} {} {} + 274 274 274 274 274 274 274 274 274 274 274 274 274 + 274 274 274 274 {} {} {} {} 226 226 226 226 226 + 226 226 226 226 226 226 226 226 226 226 226 226 {} + {} {} {} 124 124 124 124 124 124 124 124 124 124 + 124 124 124 124 124 124 {} {} {} {} 198 198 198 + 198 198 198 198 198 198 198 198 198 {} {} {} {} + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 {} {} {} {}} + +do_execsql_test 1.17.9.5 { + SELECT last_value(a+b) OVER ( ORDER BY b%10,a ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2 +} {229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 {} {} {} {}} + +do_execsql_test 1.17.9.6 { + SELECT last_value(a+b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.17.10.1 { + SELECT nth_value(b,b+1) OVER (ORDER BY a ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING) FROM t2 +} {74 65 12 4 22 37 15 53 8 16 29 8 34 3 76 73 + 63 90 47 98 47 90 73 20 89 91 22 77 73 42 41 + 32 55 79 51 74 44 81 7 65 8 43 80 8 89 90 29 + 36 15 42 9 9 41 20 16 11 87 20 90 84 80 41 37 + 34 9 75 63 34 8 8 81 95 31 74 36 41 99 90 91 + 99 13 2 35 33 36 38 37 20 75 17 {} 5 34 58 33 + 19 31 50 34 23 {} 72 90 11 85 90 36 2 {} 39 + 27 {} {} 64 2 74 95 37 {} 58 {} 34 44 {} {} + 30 70 47 {} 7 {} 15 {} {} 12 33 36 99 17 {} + {} 44 {} {} 12 {} {} {} 34 {} {} {} {} 36 44 + {} 30 30 10 {} {} {} {} {} 30 {} {} {} 84 {} + {} {} {} {} {} {} 7 {} {} {} {} {} {} {} {} + {} {} {} {} {} {}} + +do_execsql_test 1.17.10.2 { + SELECT nth_value(b,b+1) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 21 {} {} {} 31 {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} 72 {} {} {} 82 + {} {} {} {} {} {} {} {} {} {} 73 {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 64 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} 76 {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 27 {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} 38 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.17.10.3 { + SELECT nth_value(b,b+1) OVER ( ORDER BY b,a ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2 +} {3 4 6 7 7 8 8 9 10 11 12 12 13 13 14 15 15 + 16 16 19 20 21 22 22 23 24 26 27 27 28 29 29 + 30 30 32 33 33 34 34 34 35 36 36 37 38 38 39 + 39 41 42 43 43 44 46 46 47 47 49 51 52 53 54 + 55 56 56 56 58 58 58 59 59 59 60 61 62 62 64 + 65 65 66 68 69 72 72 73 73 74 75 75 76 77 77 + 78 80 81 81 83 84 84 85 85 87 87 88 89 90 90 + 90 91 91 91 91 93 93 95 95 96 96 97 98 99 99 + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.17.10.4 { + SELECT nth_value(b,b+1) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} 21 + 31 91 91 {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} 22 32 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} 33 33 93 93 {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} 44 {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 65 95 {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} 46 {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 47 57 + 67 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} 78 88 98 {} {} {} {} {} {} {} {} {} + {} {} {} {} 59 59 69 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.17.10.5 { + SELECT nth_value(b,b+1) OVER ( ORDER BY b%10,a ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2 +} {85 72 12 84 31 95 33 34 35 5 2 93 51 12 95 15 + 34 96 46 21 95 23 84 31 52 2 34 54 73 33 72 + 16 26 26 63 65 23 84 33 14 72 16 53 3 82 56 + 16 73 6 95 67 47 74 27 65 73 5 85 46 96 77 84 + 7 47 65 8 85 64 95 4 75 98 66 98 7 5 64 89 + 58 28 86 56 66 98 7 5 76 89 89 77 26 58 36 29 + 59 68 46 96 29 {} 89 59 87 96 36 {} 58 9 {} + 99 36 {} {} 88 76 58 67 77 {} 79 49 59 37 88 + {} {} {} 28 98 89 {} 39 99 {} 58 {} {} {} 69 + 27 {} {} 59 {} {} 8 {} {} {} 39 59 99 88 {} + {} {} 38 69 {} {} {} {} 59 {} 89 {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {}} + +do_execsql_test 1.17.10.6 { + SELECT nth_value(b,b+1) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.17.11.1 { + SELECT first_value(b) OVER (ORDER BY a ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING) FROM t2 +} {38 68 39 62 91 46 6 99 97 27 46 78 54 97 8 67 + 29 93 84 77 23 16 16 93 65 35 47 7 86 74 61 + 91 85 24 85 43 59 12 32 56 3 91 22 90 55 15 + 28 89 25 47 1 56 40 43 56 16 75 36 89 98 76 + 81 4 94 42 30 78 33 29 53 63 2 87 37 80 84 72 + 41 9 61 73 95 65 13 58 96 98 1 21 74 65 35 5 + 73 11 51 87 41 12 8 20 31 31 15 95 22 73 79 + 88 34 8 11 49 34 90 59 96 60 55 75 77 44 2 7 + 85 57 74 29 70 59 19 39 26 26 47 80 90 36 58 + 47 9 72 72 66 33 93 75 64 81 9 23 37 13 12 14 + 62 91 36 91 33 15 34 36 99 3 95 69 58 52 30 + 50 84 10 84 33 21 39 44 58 30 38 34 83 27 82 + 17 7 {} {} {} {}} + +do_execsql_test 1.17.11.2 { + SELECT first_value(b) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING) FROM t2 +} {20 90 60 70 80 90 30 50 10 30 {} {} {} {} 91 + 1 81 41 61 1 21 11 51 41 31 31 11 81 91 91 21 + {} {} {} {} 42 2 72 12 22 2 72 72 12 62 52 82 + {} {} {} {} 3 43 33 53 63 73 13 73 73 33 93 + 23 13 33 3 33 83 {} {} {} {} 4 94 84 74 34 34 + 44 74 64 14 34 84 84 44 34 {} {} {} {} 55 15 + 25 75 95 65 65 35 5 15 95 55 75 85 75 15 95 + {} {} {} {} 16 16 86 56 56 56 16 36 76 96 96 + 26 26 36 66 36 36 {} {} {} {} 77 47 7 47 87 + 37 87 77 7 57 47 47 37 27 17 7 {} {} {} {} 28 + 98 78 58 98 8 88 8 58 58 58 38 {} {} {} {} 29 + 59 89 89 29 9 79 49 59 29 59 19 39 9 9 99 69 + 39 {} {} {} {}} + +do_execsql_test 1.17.11.3 { + SELECT first_value(b) OVER ( ORDER BY b,a ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2 +} {3 3 4 5 6 7 7 7 8 8 8 9 9 9 10 11 11 12 + 12 12 13 13 14 15 15 15 16 16 16 17 19 20 21 + 21 22 22 23 23 24 25 26 26 27 27 28 29 29 29 + 30 30 30 31 31 32 33 33 33 33 34 34 34 34 35 + 35 36 36 36 36 37 37 38 38 39 39 39 40 41 41 + 42 43 43 44 44 46 46 47 47 47 47 49 50 51 52 + 53 54 55 55 56 56 56 57 58 58 58 58 59 59 59 + 59 60 61 61 62 62 63 64 65 65 65 66 67 68 69 + 70 72 72 72 73 73 73 74 74 74 75 75 75 76 77 + 77 78 78 79 80 80 81 81 81 82 83 84 84 84 84 + 85 85 85 86 87 87 88 89 89 89 90 90 90 91 91 + 91 91 91 93 93 93 94 95 95 95 96 96 96 97 97 + 98 98 99 99 {} {} {} {}} + +do_execsql_test 1.17.11.4 { + SELECT first_value(b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2 +} {30 40 50 60 70 80 80 90 90 90 {} {} {} {} 21 + 21 31 31 41 41 51 61 61 81 81 81 91 91 91 91 + 91 {} {} {} {} 12 22 22 32 42 52 62 62 72 72 + 72 82 {} {} {} {} 23 23 33 33 33 33 43 43 53 + 63 73 73 73 83 93 93 93 {} {} {} {} 34 34 34 + 44 44 54 64 74 74 74 84 84 84 84 94 {} {} {} + {} 25 35 35 55 55 65 65 65 75 75 75 85 85 85 + 95 95 95 {} {} {} {} 26 26 36 36 36 36 46 46 + 56 56 56 66 76 86 96 96 96 {} {} {} {} 27 27 + 37 37 47 47 47 47 57 67 77 77 87 87 97 97 {} + {} {} {} 38 38 58 58 58 58 68 78 78 88 98 98 + {} {} {} {} 29 29 29 39 39 39 49 59 59 59 59 + 69 79 89 89 89 99 99 {} {} {} {}} + +do_execsql_test 1.17.11.5 { + SELECT first_value(b) OVER ( ORDER BY b%10,a ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2 +} {20 90 60 70 80 90 30 50 10 30 81 91 61 91 91 + 1 81 41 61 1 21 11 51 41 31 31 11 81 91 91 21 + 62 12 32 22 42 2 72 12 22 2 72 72 12 62 52 82 + 93 23 93 43 3 43 33 53 63 73 13 73 73 33 93 + 23 13 33 3 33 83 54 84 74 24 4 94 84 74 34 34 + 44 74 64 14 34 84 84 44 34 65 35 85 85 55 15 + 25 75 95 65 65 35 5 15 95 55 75 85 75 15 95 + 96 46 6 46 16 16 86 56 56 56 16 36 76 96 96 + 26 26 36 66 36 36 97 27 97 67 77 47 7 47 87 + 37 87 77 7 57 47 47 37 27 17 7 38 68 78 8 28 + 98 78 58 98 8 88 8 58 58 58 38 89 59 39 99 29 + 59 89 89 29 9 79 49 59 29 59 19 39 9 9 99 69 + 39 {} {} {} {}} + +do_execsql_test 1.17.11.6 { + SELECT first_value(b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.17.12.1 { + SELECT lead(b,b) OVER (ORDER BY a ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING) FROM t2 +} {96 9 11 89 32 53 91 30 51 56 54 73 22 59 75 + 74 78 8 16 65 15 8 31 87 90 12 32 96 74 76 37 + 85 90 15 35 2 60 36 75 9 51 47 63 51 90 26 42 + 26 8 76 80 90 37 87 56 79 5 87 8 2 39 73 64 + 36 90 72 78 36 73 51 33 20 41 2 26 37 33 8 14 + 33 81 55 1 9 12 39 64 87 72 34 82 21 34 99 62 + 74 41 69 22 75 27 58 8 79 77 26 26 55 {} 29 + 30 7 {} 66 55 2 34 64 {} 33 {} 44 84 {} {} 95 + 85 19 {} 83 {} 91 {} {} 9 50 91 33 34 {} {} + 84 {} 7 9 {} {} {} 44 {} {} {} {} 91 84 {} 95 + 95 52 {} {} {} {} {} 21 {} {} {} 58 {} {} {} + {} {} {} {} 83 {} {} {} {} {} {} {} {} {} {} + {} {} {} {}} + +do_execsql_test 1.17.12.2 { + SELECT lead(b,b) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 81 {} {} {} 21 {} {} {} {} {} {} + {} {} {} {} {} {} 62 {} {} {} 12 {} {} {} 72 + {} {} {} {} {} {} {} {} {} {} 53 {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 34 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} 95 {} {} {} {} {} {} 85 {} + {} {} {} {} {} {} {} {} {} 56 {} 36 {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 57 {} {} {} {} {} 7 {} {} {} {} + {} {} {} {} {} {} 8 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 9 {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.17.12.3 { + SELECT lead(b,b) OVER ( ORDER BY b,a ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2 +} {1 2 3 3 5 6 7 8 8 9 9 10 11 12 12 13 13 + 14 15 16 16 17 19 20 21 22 23 24 25 26 27 27 + 28 29 30 31 32 33 33 33 34 34 35 36 36 36 37 + 38 39 39 40 41 42 43 43 44 46 47 47 47 49 50 + 52 53 54 55 56 56 57 58 58 58 59 59 59 60 61 + 62 62 64 65 65 67 69 70 72 72 73 74 74 75 75 + 75 77 78 80 81 81 83 84 84 85 85 85 87 88 89 + 89 89 90 90 91 91 91 93 93 94 95 95 96 97 97 + 98 99 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.17.12.4 { + SELECT lead(b,b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2 +} {80 {} {} {} {} {} {} {} {} {} {} {} {} {} 1 + 11 81 81 {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} 12 12 72 82 {} {} {} {} {} {} + {} {} {} {} {} {} 13 23 73 73 {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} 34 84 {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 35 85 85 95 {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} 36 86 96 96 {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 37 47 + 47 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} 58 58 68 {} {} {} {} {} {} {} {} {} + {} {} {} {} 39 49 59 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.17.12.5 { + SELECT lead(b,b) OVER ( ORDER BY b%10,a ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2 +} {34 72 91 74 21 55 23 94 84 95 2 13 61 22 55 + 55 94 85 75 81 65 73 74 21 72 12 94 33 63 73 + 72 96 36 76 3 25 62 3 73 34 12 46 43 93 72 16 + 86 63 15 65 36 77 24 57 25 53 95 34 95 16 97 + 74 97 67 25 98 44 34 65 54 5 68 96 28 47 95 + 34 39 8 38 6 46 96 28 47 95 56 39 99 97 76 8 + 26 9 79 27 95 16 29 {} 58 58 77 85 56 {} 98 + 29 {} 19 96 {} {} 78 56 98 36 97 {} 89 89 29 + 47 78 {} {} {} 38 68 58 {} 58 38 {} 98 {} {} + {} 39 57 9 {} 79 {} {} 7 {} {} {} 9 29 38 78 + {} {} {} 8 39 {} {} {} {} 59 {} 99 {} {} {} + {} {} {} {} {} {} {} {} {} {} 9 {} {} {} {} + {} {} {} {} {} {} {} {}} + +do_execsql_test 1.17.12.6 { + SELECT lead(b,b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.17.13.1 { + SELECT lag(b,b) OVER (ORDER BY a ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} 38 {} {} {} {} + {} {} {} 6 {} {} {} {} {} 81 46 6 {} {} {} {} + 23 {} {} {} {} {} 27 {} {} {} 35 6 {} 12 {} + 23 {} {} 61 84 {} 93 39 47 {} 54 46 96 56 {} + 16 {} {} {} {} 89 {} 16 43 {} 85 56 29 99 53 + {} 59 {} {} 91 59 53 84 99 {} 93 63 47 {} {} + 98 33 67 35 75 1 23 13 55 27 75 98 35 73 63 2 + 21 27 13 24 86 23 84 31 20 94 61 65 75 23 36 + 94 55 90 41 77 96 56 29 40 12 89 63 11 5 73 + 79 1 16 28 31 73 5 39 53 63 41 11 40 2 13 33 + 9 29 90 47 72 9 73 30 44 33 74 93 29 74 42 34 + 63 41 34 96 47 77 1 36 74 72 14 36 26 77 9 72 + 64 8 91 31 52 30} + +do_execsql_test 1.17.13.2 { + SELECT lag(b,b) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} 30 {} {} + {} {} {} {} 91 {} {} {} 61 {} 81 {} {} {} {} + 1 {} {} {} {} {} {} {} {} {} 22 {} {} {} 12 + {} {} 62 {} {} {} {} {} {} {} 23 {} {} {} {} + {} {} {} {} {} {} {} 43 {} 23 {} {} {} {} {} + {} 54 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 75 {} + {} {} {} {} {} 55 {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} 47 {} {} {} {} + {} 27 7 {} {} {} {} {} {} {} {} {} 68 {} 8 {} + {} {} {} {} {} {} {} {} {} {} {} {} 89 {} {} + {} {} {} {} {} 29 9 {} {} {}} + +do_execsql_test 1.17.13.3 { + SELECT lag(b,b) OVER ( ORDER BY b,a ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2 +} {{} 1 1 1 1 2 2 2 2 2 2 3 3 3 4 4 5 6 6 + 6 7 7 7 7 7 8 8 8 8 8 8 9 9 9 9 9 9 9 + 9 9 9 10 10 10 10 11 11 11 11 11 12 12 12 12 + 13 13 13 13 13 14 15 15 15 15 16 16 16 16 16 + 17 19 20 20 21 21 21 21 22 22 22 22 23 23 23 + 23 23 24 23 24 24 25 26 26 26 26 26 26 26 26 + 26 26 26 27 27 27 27 28 29 29 29 29 30 30 30 + 30 30 30 31 31 31 31 31 32 32 32 32 32 32 31 + 32 33 33 33 33 33 33 34 34 34 34 34 34 34 34 + 35 35 35 35 35 36 36 36 36 36 36 36 37 37 37 + 38 38 38 38 38 38 39 39 39 39 40 40 41 41 42 + 43 42 43 43 43 43 44 44 44 46 46 46 47 47 47 + 47 47} + +do_execsql_test 1.17.13.4 { + SELECT lag(b,b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + 1 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.17.13.5 { + SELECT lag(b,b) OVER ( ORDER BY b%10,a ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} 30 {} {} + {} {} {} {} 91 {} {} {} 61 80 81 {} {} {} {} + 1 {} {} {} 30 {} 21 90 61 {} 22 {} 11 41 12 + {} {} 62 {} {} {} {} 31 {} 50 23 30 21 90 {} + {} 62 {} {} 81 {} 22 43 62 23 32 {} 91 {} 90 + 93 54 {} {} 90 72 12 22 90 81 83 23 80 20 72 + 43 51 33 80 90 2 34 54 1 20 62 12 13 75 44 30 + 93 91 1 21 55 61 61 13 85 3 65 65 91 73 33 93 + 55 84 62 31 11 65 35 85 33 55 15 12 75 22 3 + 73 65 36 85 43 95 43 13 47 44 65 65 96 36 27 + 7 46 34 94 47 36 73 34 35 73 68 24 8 75 85 75 + 66 34 95 36 84 77 46 34 84 47 89 65 36 16 38 + 76 58 57 29 9 44 56 17} + +do_execsql_test 1.17.13.6 { + SELECT lag(b,b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.17.14.1 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (ORDER BY a ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING) FROM t2 +} {38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 33.21.39.44.58.30.38.34.83.27.82.17.7 21.39.44.58.30.38.34.83.27.82.17.7 + 39.44.58.30.38.34.83.27.82.17.7 44.58.30.38.34.83.27.82.17.7 + 58.30.38.34.83.27.82.17.7 30.38.34.83.27.82.17.7 38.34.83.27.82.17.7 + 34.83.27.82.17.7 83.27.82.17.7 27.82.17.7 82.17.7 17.7 7 {} + {} {} {}} + +do_execsql_test 1.17.14.2 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING) FROM t2 +} {20.90.60.70.80.90.30.50.10.30 90.60.70.80.90.30.50.10.30 + 60.70.80.90.30.50.10.30 70.80.90.30.50.10.30 80.90.30.50.10.30 + 90.30.50.10.30 30.50.10.30 50.10.30 10.30 30 {} {} {} {} + 91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 61.1.21.11.51.41.31.31.11.81.91.91.21 1.21.11.51.41.31.31.11.81.91.91.21 + 21.11.51.41.31.31.11.81.91.91.21 11.51.41.31.31.11.81.91.91.21 + 51.41.31.31.11.81.91.91.21 41.31.31.11.81.91.91.21 31.31.11.81.91.91.21 + 31.11.81.91.91.21 11.81.91.91.21 81.91.91.21 91.91.21 91.21 21 + {} {} {} {} 42.2.72.12.22.2.72.72.12.62.52.82 + 2.72.12.22.2.72.72.12.62.52.82 72.12.22.2.72.72.12.62.52.82 + 12.22.2.72.72.12.62.52.82 22.2.72.72.12.62.52.82 2.72.72.12.62.52.82 + 72.72.12.62.52.82 72.12.62.52.82 12.62.52.82 62.52.82 52.82 82 + {} {} {} {} 3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 63.73.13.73.73.33.93.23.13.33.3.33.83 73.13.73.73.33.93.23.13.33.3.33.83 + 13.73.73.33.93.23.13.33.3.33.83 73.73.33.93.23.13.33.3.33.83 + 73.33.93.23.13.33.3.33.83 33.93.23.13.33.3.33.83 93.23.13.33.3.33.83 + 23.13.33.3.33.83 13.33.3.33.83 33.3.33.83 3.33.83 33.83 83 {} + {} {} {} 4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 84.74.34.34.44.74.64.14.34.84.84.44.34 + 74.34.34.44.74.64.14.34.84.84.44.34 34.34.44.74.64.14.34.84.84.44.34 + 34.44.74.64.14.34.84.84.44.34 44.74.64.14.34.84.84.44.34 + 74.64.14.34.84.84.44.34 64.14.34.84.84.44.34 14.34.84.84.44.34 + 34.84.84.44.34 84.84.44.34 84.44.34 44.34 34 {} {} {} {} + 55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 95.65.65.35.5.15.95.55.75.85.75.15.95 65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.5.15.95.55.75.85.75.15.95 35.5.15.95.55.75.85.75.15.95 + 5.15.95.55.75.85.75.15.95 15.95.55.75.85.75.15.95 95.55.75.85.75.15.95 + 55.75.85.75.15.95 75.85.75.15.95 85.75.15.95 75.15.95 15.95 95 + {} {} {} {} 16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 56.56.16.36.76.96.96.26.26.36.66.36.36 + 56.16.36.76.96.96.26.26.36.66.36.36 16.36.76.96.96.26.26.36.66.36.36 + 36.76.96.96.26.26.36.66.36.36 76.96.96.26.26.36.66.36.36 + 96.96.26.26.36.66.36.36 96.26.26.36.66.36.36 26.26.36.66.36.36 + 26.36.66.36.36 36.66.36.36 66.36.36 36.36 36 {} {} {} {} + 77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 47.87.37.87.77.7.57.47.47.37.27.17.7 87.37.87.77.7.57.47.47.37.27.17.7 + 37.87.77.7.57.47.47.37.27.17.7 87.77.7.57.47.47.37.27.17.7 + 77.7.57.47.47.37.27.17.7 7.57.47.47.37.27.17.7 57.47.47.37.27.17.7 + 47.47.37.27.17.7 47.37.27.17.7 37.27.17.7 27.17.7 17.7 7 {} + {} {} {} 28.98.78.58.98.8.88.8.58.58.58.38 + 98.78.58.98.8.88.8.58.58.58.38 78.58.98.8.88.8.58.58.58.38 + 58.98.8.88.8.58.58.58.38 98.8.88.8.58.58.58.38 8.88.8.58.58.58.38 + 88.8.58.58.58.38 8.58.58.58.38 58.58.58.38 58.58.38 58.38 38 {} + {} {} {} 29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 9.79.49.59.29.59.19.39.9.9.99.69.39 79.49.59.29.59.19.39.9.9.99.69.39 + 49.59.29.59.19.39.9.9.99.69.39 59.29.59.19.39.9.9.99.69.39 + 29.59.19.39.9.9.99.69.39 59.19.39.9.9.99.69.39 19.39.9.9.99.69.39 + 39.9.9.99.69.39 9.9.99.69.39 9.99.69.39 99.69.39 69.39 39 {} + {} {} {}} + +do_execsql_test 1.17.14.3 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( ORDER BY b,a ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2 +} {3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 94.95.95.95.96.96.96.97.97.98.98.99.99 + 95.95.95.96.96.96.97.97.98.98.99.99 95.95.96.96.96.97.97.98.98.99.99 + 95.96.96.96.97.97.98.98.99.99 96.96.96.97.97.98.98.99.99 + 96.96.97.97.98.98.99.99 96.97.97.98.98.99.99 97.97.98.98.99.99 + 97.98.98.99.99 98.98.99.99 98.99.99 99.99 99 {} {} {} {}} + +do_execsql_test 1.17.14.4 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2 +} {30.40.50.60.70.80.80.90.90.90 40.50.60.70.80.80.90.90.90 + 50.60.70.80.80.90.90.90 60.70.80.80.90.90.90 70.80.80.90.90.90 + 80.80.90.90.90 80.90.90.90 90.90.90 90.90 90 {} {} {} {} + 21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 41.41.51.61.61.81.81.81.91.91.91.91.91 + 41.51.61.61.81.81.81.91.91.91.91.91 51.61.61.81.81.81.91.91.91.91.91 + 61.61.81.81.81.91.91.91.91.91 61.81.81.81.91.91.91.91.91 + 81.81.81.91.91.91.91.91 81.81.91.91.91.91.91 81.91.91.91.91.91 + 91.91.91.91.91 91.91.91.91 91.91.91 91.91 91 {} {} {} {} + 12.22.22.32.42.52.62.62.72.72.72.82 22.22.32.42.52.62.62.72.72.72.82 + 22.32.42.52.62.62.72.72.72.82 32.42.52.62.62.72.72.72.82 + 42.52.62.62.72.72.72.82 52.62.62.72.72.72.82 62.62.72.72.72.82 + 62.72.72.72.82 72.72.72.82 72.72.82 72.82 82 {} {} {} {} + 23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 33.33.43.43.53.63.73.73.73.83.93.93.93 + 33.43.43.53.63.73.73.73.83.93.93.93 43.43.53.63.73.73.73.83.93.93.93 + 43.53.63.73.73.73.83.93.93.93 53.63.73.73.73.83.93.93.93 + 63.73.73.73.83.93.93.93 73.73.73.83.93.93.93 73.73.83.93.93.93 + 73.83.93.93.93 83.93.93.93 93.93.93 93.93 93 {} {} {} {} + 34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 34.44.44.54.64.74.74.74.84.84.84.84.94 + 44.44.54.64.74.74.74.84.84.84.84.94 44.54.64.74.74.74.84.84.84.84.94 + 54.64.74.74.74.84.84.84.84.94 64.74.74.74.84.84.84.84.94 + 74.74.74.84.84.84.84.94 74.74.84.84.84.84.94 74.84.84.84.84.94 + 84.84.84.84.94 84.84.84.94 84.84.94 84.94 94 {} {} {} {} + 25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 55.65.65.65.75.75.75.85.85.85.95.95.95 + 65.65.65.75.75.75.85.85.85.95.95.95 65.65.75.75.75.85.85.85.95.95.95 + 65.75.75.75.85.85.85.95.95.95 75.75.75.85.85.85.95.95.95 + 75.75.85.85.85.95.95.95 75.85.85.85.95.95.95 85.85.85.95.95.95 + 85.85.95.95.95 85.95.95.95 95.95.95 95.95 95 {} {} {} {} + 26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 36.36.46.46.56.56.56.66.76.86.96.96.96 + 36.46.46.56.56.56.66.76.86.96.96.96 46.46.56.56.56.66.76.86.96.96.96 + 46.56.56.56.66.76.86.96.96.96 56.56.56.66.76.86.96.96.96 + 56.56.66.76.86.96.96.96 56.66.76.86.96.96.96 66.76.86.96.96.96 + 76.86.96.96.96 86.96.96.96 96.96.96 96.96 96 {} {} {} {} + 27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 37.47.47.47.47.57.67.77.77.87.87.97.97 + 47.47.47.47.57.67.77.77.87.87.97.97 47.47.47.57.67.77.77.87.87.97.97 + 47.47.57.67.77.77.87.87.97.97 47.57.67.77.77.87.87.97.97 + 57.67.77.77.87.87.97.97 67.77.77.87.87.97.97 77.77.87.87.97.97 + 77.87.87.97.97 87.87.97.97 87.97.97 97.97 97 {} {} {} {} + 38.38.58.58.58.58.68.78.78.88.98.98 38.58.58.58.58.68.78.78.88.98.98 + 58.58.58.58.68.78.78.88.98.98 58.58.58.68.78.78.88.98.98 + 58.58.68.78.78.88.98.98 58.68.78.78.88.98.98 68.78.78.88.98.98 + 78.78.88.98.98 78.88.98.98 88.98.98 98.98 98 {} {} {} {} + 29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 39.49.59.59.59.59.69.79.89.89.89.99.99 + 49.59.59.59.59.69.79.89.89.89.99.99 59.59.59.59.69.79.89.89.89.99.99 + 59.59.59.69.79.89.89.89.99.99 59.59.69.79.89.89.89.99.99 + 59.69.79.89.89.89.99.99 69.79.89.89.89.99.99 79.89.89.89.99.99 + 89.89.89.99.99 89.89.99.99 89.99.99 99.99 99 {} {} {} {}} + +do_execsql_test 1.17.14.5 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( ORDER BY b%10,a ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t2 +} {20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 9.79.49.59.29.59.19.39.9.9.99.69.39 79.49.59.29.59.19.39.9.9.99.69.39 + 49.59.29.59.19.39.9.9.99.69.39 59.29.59.19.39.9.9.99.69.39 + 29.59.19.39.9.9.99.69.39 59.19.39.9.9.99.69.39 19.39.9.9.99.69.39 + 39.9.9.99.69.39 9.9.99.69.39 9.99.69.39 99.69.39 69.39 39 {} + {} {} {}} + +do_execsql_test 1.17.14.6 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.17.14.7 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (win1 ORDER BY b%10 ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING) + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a) + ORDER BY 1 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.17.14.8 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (win1 ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING) + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a ORDER BY b%10) + ORDER BY 1 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.17.14.9 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER win2 + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a ORDER BY b%10), + win2 AS (win1 ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING) + ORDER BY 1 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.17.15.1 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE a%2=0) OVER win FROM t2 + WINDOW win AS (ORDER BY a ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING) +} {187 38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 186 39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 185 39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 184 91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 183 91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 182 6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 181 6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 180 97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 179 97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 178 46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 177 46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 176 54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 175 54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 174 8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 173 8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 172 29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 171 29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 170 84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 169 84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 168 23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 167 23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 166 16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 165 16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 164 65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 163 65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 162 47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 161 47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 160 86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 159 86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 158 61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 157 61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 156 85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 155 85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 154 85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 153 85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 152 59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 151 59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 150 32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 149 32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 148 3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 147 3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 146 22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 145 22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 144 55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 143 55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 142 28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 141 28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 140 25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 139 25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 138 1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 137 1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 136 40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 135 40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 134 56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 133 56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 132 75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 131 75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 130 89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 129 89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 128 76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 127 76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 126 4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 125 4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 124 42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 123 42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 122 78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 121 78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 120 29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 119 29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 118 63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 117 63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 116 87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 115 87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 114 80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 113 80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 112 72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 111 72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 110 9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 109 9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 108 73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 107 73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 106 65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 105 65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 104 58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 103 58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 102 98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 101 98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 100 21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 99 21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 98 65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 97 65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 96 5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 95 5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 94 11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 93 11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 92 87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 91 87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 90 12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 89 12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 88 20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 87 20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 86 31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 85 31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 84 95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 83 95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 82 73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 81 73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 80 88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 79 88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 78 8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 77 8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 76 49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 75 49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 74 90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 73 90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 72 96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 71 96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 70 55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 69 55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 68 77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 67 77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 66 2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 65 2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 64 85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 63 85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 62 74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 61 74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 60 70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 59 70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 58 19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 57 19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 56 26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 55 26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 54 47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 53 47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 52 90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 51 90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 50 58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 49 58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 48 9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 47 9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 46 72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 45 72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 44 33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 43 33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 42 75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 41 75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 40 81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 39 81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 38 23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 37 23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 36 13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 35 13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 34 14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 33 14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 32 91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 31 91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 30 91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 29 91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 28 15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 27 15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 26 36.3.69.52.50.10.33.39.58.38.83.82.7 + 25 36.3.69.52.50.10.33.39.58.38.83.82.7 + 24 3.69.52.50.10.33.39.58.38.83.82.7 23 3.69.52.50.10.33.39.58.38.83.82.7 + 22 69.52.50.10.33.39.58.38.83.82.7 21 69.52.50.10.33.39.58.38.83.82.7 + 20 52.50.10.33.39.58.38.83.82.7 19 52.50.10.33.39.58.38.83.82.7 + 18 50.10.33.39.58.38.83.82.7 17 50.10.33.39.58.38.83.82.7 + 16 10.33.39.58.38.83.82.7 15 10.33.39.58.38.83.82.7 + 14 33.39.58.38.83.82.7 13 33.39.58.38.83.82.7 12 39.58.38.83.82.7 + 11 39.58.38.83.82.7 10 58.38.83.82.7 9 58.38.83.82.7 8 38.83.82.7 + 7 38.83.82.7 6 83.82.7 5 83.82.7 4 82.7 3 82.7 2 7 1 7 0 {} + 0 {} 0 {} 0 {}} + +do_execsql_test 1.17.15.2 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE 0=1) OVER win FROM t2 + WINDOW win AS (ORDER BY a ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING) +} {187 {} 186 {} 185 {} 184 {} 183 {} 182 {} 181 {} 180 {} + 179 {} 178 {} 177 {} 176 {} 175 {} 174 {} 173 {} 172 {} + 171 {} 170 {} 169 {} 168 {} 167 {} 166 {} 165 {} 164 {} + 163 {} 162 {} 161 {} 160 {} 159 {} 158 {} 157 {} 156 {} + 155 {} 154 {} 153 {} 152 {} 151 {} 150 {} 149 {} 148 {} + 147 {} 146 {} 145 {} 144 {} 143 {} 142 {} 141 {} 140 {} + 139 {} 138 {} 137 {} 136 {} 135 {} 134 {} 133 {} 132 {} + 131 {} 130 {} 129 {} 128 {} 127 {} 126 {} 125 {} 124 {} + 123 {} 122 {} 121 {} 120 {} 119 {} 118 {} 117 {} 116 {} + 115 {} 114 {} 113 {} 112 {} 111 {} 110 {} 109 {} 108 {} + 107 {} 106 {} 105 {} 104 {} 103 {} 102 {} 101 {} 100 {} + 99 {} 98 {} 97 {} 96 {} 95 {} 94 {} 93 {} 92 {} 91 {} + 90 {} 89 {} 88 {} 87 {} 86 {} 85 {} 84 {} 83 {} 82 {} + 81 {} 80 {} 79 {} 78 {} 77 {} 76 {} 75 {} 74 {} 73 {} + 72 {} 71 {} 70 {} 69 {} 68 {} 67 {} 66 {} 65 {} 64 {} + 63 {} 62 {} 61 {} 60 {} 59 {} 58 {} 57 {} 56 {} 55 {} + 54 {} 53 {} 52 {} 51 {} 50 {} 49 {} 48 {} 47 {} 46 {} + 45 {} 44 {} 43 {} 42 {} 41 {} 40 {} 39 {} 38 {} 37 {} + 36 {} 35 {} 34 {} 33 {} 32 {} 31 {} 30 {} 29 {} 28 {} + 27 {} 26 {} 25 {} 24 {} 23 {} 22 {} 21 {} 20 {} 19 {} + 18 {} 17 {} 16 {} 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} + 9 {} 8 {} 7 {} 6 {} 5 {} 4 {} 3 {} 2 {} 1 {} 0 {} 0 {} + 0 {} 0 {}} + +do_execsql_test 1.17.15.3 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE 1=0) OVER win FROM t2 + WINDOW win AS (PARTITION BY (a%10) ORDER BY a ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING) +} {16 {} 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} + 6 {} 5 {} 4 {} 3 {} 2 {} 1 {} 0 {} 0 {} 0 {} 0 {} 15 {} + 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} 5 {} + 4 {} 3 {} 2 {} 1 {} 0 {} 0 {} 0 {} 0 {} 15 {} 14 {} + 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} 5 {} 4 {} + 3 {} 2 {} 1 {} 0 {} 0 {} 0 {} 0 {} 15 {} 14 {} 13 {} + 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} 5 {} 4 {} 3 {} + 2 {} 1 {} 0 {} 0 {} 0 {} 0 {} 15 {} 14 {} 13 {} 12 {} + 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} 5 {} 4 {} 3 {} 2 {} + 1 {} 0 {} 0 {} 0 {} 0 {} 15 {} 14 {} 13 {} 12 {} 11 {} + 10 {} 9 {} 8 {} 7 {} 6 {} 5 {} 4 {} 3 {} 2 {} 1 {} 0 {} + 0 {} 0 {} 0 {} 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} + 8 {} 7 {} 6 {} 5 {} 4 {} 3 {} 2 {} 1 {} 0 {} 0 {} 0 {} + 0 {} 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} + 6 {} 5 {} 4 {} 3 {} 2 {} 1 {} 0 {} 0 {} 0 {} 0 {} 15 {} + 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} 5 {} + 4 {} 3 {} 2 {} 1 {} 0 {} 0 {} 0 {} 0 {} 15 {} 14 {} + 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} 5 {} 4 {} + 3 {} 2 {} 1 {} 0 {} 0 {} 0 {} 0 {}} + +do_execsql_test 1.17.15.4 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE a%2=0) OVER win FROM t2 + WINDOW win AS (PARTITION BY (a%10) ORDER BY a ROWS BETWEEN 4 FOLLOWING AND UNBOUNDED FOLLOWING) +} {16 59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 15 28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 14 75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 13 78.72.98.87.73.96.74.90.75.91.69.39.7 + 12 72.98.87.73.96.74.90.75.91.69.39.7 11 98.87.73.96.74.90.75.91.69.39.7 + 10 87.73.96.74.90.75.91.69.39.7 9 73.96.74.90.75.91.69.39.7 + 8 96.74.90.75.91.69.39.7 7 74.90.75.91.69.39.7 6 90.75.91.69.39.7 + 5 75.91.69.39.7 4 91.69.39.7 3 69.39.7 2 39.7 1 7 0 {} 0 {} + 0 {} 0 {} 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} + 7 {} 6 {} 5 {} 4 {} 3 {} 2 {} 1 {} 0 {} 0 {} 0 {} 0 {} + 15 32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 14 25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 13 89.29.9.21.12.88.55.70.58.81.91.52.58 + 12 29.9.21.12.88.55.70.58.81.91.52.58 11 9.21.12.88.55.70.58.81.91.52.58 + 10 21.12.88.55.70.58.81.91.52.58 9 12.88.55.70.58.81.91.52.58 + 8 88.55.70.58.81.91.52.58 7 55.70.58.81.91.52.58 6 70.58.81.91.52.58 + 5 58.81.91.52.58 4 81.91.52.58 3 91.52.58 2 52.58 1 58 0 {} + 0 {} 0 {} 0 {} 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} + 8 {} 7 {} 6 {} 5 {} 4 {} 3 {} 2 {} 1 {} 0 {} 0 {} 0 {} + 0 {} 15 3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 14 1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 13 76.63.73.65.20.8.77.19.9.23.15.50.38 + 12 63.73.65.20.8.77.19.9.23.15.50.38 11 73.65.20.8.77.19.9.23.15.50.38 + 10 65.20.8.77.19.9.23.15.50.38 9 20.8.77.19.9.23.15.50.38 + 8 8.77.19.9.23.15.50.38 7 77.19.9.23.15.50.38 6 19.9.23.15.50.38 + 5 9.23.15.50.38 4 23.15.50.38 3 15.50.38 2 50.38 1 38 0 {} 0 {} + 0 {} 0 {} 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} + 7 {} 6 {} 5 {} 4 {} 3 {} 2 {} 1 {} 0 {} 0 {} 0 {} 0 {} + 15 22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 14 40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 13 4.87.65.5.31.49.2.26.72.13.36.10.83 + 12 87.65.5.31.49.2.26.72.13.36.10.83 11 65.5.31.49.2.26.72.13.36.10.83 + 10 5.31.49.2.26.72.13.36.10.83 9 31.49.2.26.72.13.36.10.83 + 8 49.2.26.72.13.36.10.83 7 2.26.72.13.36.10.83 6 26.72.13.36.10.83 + 5 72.13.36.10.83 4 13.36.10.83 3 36.10.83 2 10.83 1 83 0 {} + 0 {} 0 {} 0 {} 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} + 8 {} 7 {} 6 {} 5 {} 4 {} 3 {} 2 {} 1 {} 0 {} 0 {} 0 {} + 0 {} 15 55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 14 56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 13 42.80.58.11.95.90.85.47.33.14.3.33.82 + 12 80.58.11.95.90.85.47.33.14.3.33.82 11 58.11.95.90.85.47.33.14.3.33.82 + 10 11.95.90.85.47.33.14.3.33.82 9 95.90.85.47.33.14.3.33.82 + 8 90.85.47.33.14.3.33.82 7 85.47.33.14.3.33.82 6 47.33.14.3.33.82 + 5 33.14.3.33.82 4 14.3.33.82 3 3.33.82 2 33.82 1 82 0 {} 0 {} + 0 {} 0 {} 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} + 7 {} 6 {} 5 {} 4 {} 3 {} 2 {} 1 {} 0 {} 0 {} 0 {} 0 {}} + +do_execsql_test 1.18.2.1 { + SELECT max(b) OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2 +} {99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 95 99 99 99 99 95 95 84 84 84 84 84 84 + 84 84 83 83 83 83 82 83 83 83 83} + +do_execsql_test 1.18.2.2 { + SELECT min(b) OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 + 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 2 3 2 2 2 2 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 7 3 3 + 3 3 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 + 17} + +do_execsql_test 1.18.3.1 { + SELECT row_number() OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.18.3.2 { + SELECT row_number() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.18.3.3 { + SELECT row_number() OVER ( ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.18.4.1 { + SELECT dense_rank() OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.18.4.2 { + SELECT dense_rank() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.18.4.3 { + SELECT dense_rank() OVER ( ORDER BY b ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2 +} {1 1 2 2 3 3 4 5 6 7 7 7 8 8 8 9 9 9 10 + 11 11 12 12 12 13 13 14 15 15 15 16 16 16 17 + 18 19 20 20 21 21 22 22 23 24 25 25 26 26 27 + 28 28 28 29 29 29 30 30 31 32 32 32 32 33 33 + 33 33 34 34 35 35 35 35 36 36 37 37 38 38 38 + 39 40 40 41 42 42 43 43 44 44 45 45 45 45 46 + 47 48 49 50 51 52 52 53 53 53 54 55 55 55 55 + 56 56 56 56 57 58 58 59 59 60 61 62 62 62 63 + 64 65 66 67 68 68 68 69 69 69 70 70 70 71 71 + 71 72 73 73 74 74 75 76 76 77 77 77 78 79 80 + 80 80 80 81 81 81 82 83 83 84 85 85 85 86 86 + 86 87 87 87 87 87 88 88 88 89 90 90 90 91 91 + 91 92 92 93 93 94 94} + +do_execsql_test 1.18.4.4 { + SELECT dense_rank() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2 +} {1 2 3 3 3 4 5 6 7 8 8 9 9 9 1 1 2 2 3 + 3 4 4 5 5 6 7 7 8 8 8 9 9 9 9 9 1 1 2 + 2 2 3 3 4 5 6 7 7 8 8 8 9 1 1 2 2 3 3 + 4 4 4 4 5 5 6 7 8 8 8 9 10 10 10 1 2 3 + 4 4 4 4 5 5 6 7 8 8 8 9 9 9 9 10 1 2 2 + 2 3 4 4 5 5 6 6 6 7 7 7 8 8 8 9 9 9 1 + 2 2 2 3 3 4 4 4 4 5 5 6 6 6 7 8 9 10 10 + 10 1 1 1 2 3 3 4 4 5 5 5 5 6 7 8 8 9 9 + 10 10 1 1 1 2 3 3 4 4 4 4 5 6 6 7 8 8 1 + 1 1 2 3 3 3 4 4 4 5 6 6 6 6 7 8 9 9 9 + 10 10} + +do_execsql_test 1.18.4.5 { + SELECT dense_rank() OVER ( ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 + 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 + 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 + 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 + 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 + 8 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 10 10 + 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 + 10 10 10 10 10} + +do_execsql_test 1.18.4.6 { + SELECT dense_rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 + 5 5 5 5 5 5 5 5 5 5 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 + 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 + 5} + +do_execsql_test 1.18.5.1 { + SELECT rank() OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.18.5.2 { + SELECT rank() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.18.5.3 { + SELECT rank() OVER ( ORDER BY b ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2 +} {1 1 3 3 5 5 7 8 9 10 10 10 13 13 13 16 16 + 16 19 20 20 22 22 22 25 25 27 28 28 28 31 31 + 31 34 35 36 37 37 39 39 41 41 43 44 45 45 47 + 47 49 50 50 50 53 53 53 56 56 58 59 59 59 59 + 63 63 63 63 67 67 69 69 69 69 73 73 75 75 77 + 77 77 80 81 81 83 84 84 86 86 88 88 90 90 90 + 90 94 95 96 97 98 99 100 100 102 102 102 105 106 + 106 106 106 110 110 110 110 114 115 115 117 117 119 + 120 121 121 121 124 125 126 127 128 129 129 129 132 + 132 132 135 135 135 138 138 138 141 142 142 144 144 + 146 147 147 149 149 149 152 153 154 154 154 154 158 + 158 158 161 162 162 164 165 165 165 168 168 168 171 + 171 171 171 171 176 176 176 179 180 180 180 183 183 + 183 186 186 188 188 190 190} + +do_execsql_test 1.18.5.4 { + SELECT rank() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2 +} {1 2 3 3 3 6 7 8 9 10 10 12 12 12 1 1 3 3 + 5 5 7 7 9 9 11 12 12 14 14 14 17 17 17 17 + 17 1 1 3 3 3 6 6 8 9 10 11 11 13 13 13 16 + 1 1 3 3 5 5 7 7 7 7 11 11 13 14 15 15 15 + 18 19 19 19 1 2 3 4 4 4 4 8 8 10 11 12 12 + 12 15 15 15 15 19 1 2 2 2 5 6 6 8 8 10 10 + 10 13 13 13 16 16 16 19 19 19 1 2 2 2 5 5 7 + 7 7 7 11 11 13 13 13 16 17 18 19 19 19 1 1 + 1 4 5 5 7 7 9 9 9 9 13 14 15 15 17 17 19 + 19 1 1 1 4 5 5 7 7 7 7 11 12 12 14 15 15 + 1 1 1 4 5 5 5 8 8 8 11 12 12 12 12 16 17 + 18 18 18 21 21} + +do_execsql_test 1.18.5.5 { + SELECT rank() OVER ( ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 15 15 15 15 + 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 + 15 15 36 36 36 36 36 36 36 36 36 36 36 36 36 + 36 36 36 52 52 52 52 52 52 52 52 52 52 52 52 + 52 52 52 52 52 52 52 52 52 73 73 73 73 73 73 + 73 73 73 73 73 73 73 73 73 73 73 73 73 92 92 + 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 + 92 92 92 92 113 113 113 113 113 113 113 113 113 + 113 113 113 113 113 113 113 113 113 113 113 113 134 + 134 134 134 134 134 134 134 134 134 134 134 134 134 + 134 134 134 134 134 134 154 154 154 154 154 154 154 + 154 154 154 154 154 154 154 154 154 170 170 170 170 + 170 170 170 170 170 170 170 170 170 170 170 170 170 + 170 170 170 170 170} + +do_execsql_test 1.18.5.6 { + SELECT rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 15 15 15 15 + 15 15 15 15 15 15 15 15 15 15 15 15 31 31 31 + 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 + 31 50 50 50 50 50 50 50 50 50 50 50 50 50 50 + 50 50 50 50 50 50 50 71 71 71 71 71 71 71 71 + 71 71 71 71 71 71 71 71 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 22 22 22 22 22 22 + 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 + 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 + 43 43 43 43 43 43 64 64 64 64 64 64 64 64 64 + 64 64 64 64 64 64 64 64 64 64 64 84 84 84 84 + 84 84 84 84 84 84 84 84 84 84 84 84 84 84 84 + 84 84 84} + +do_execsql_test 1.18.6.1 { + SELECT + row_number() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ), + rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ), + dense_rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) + FROM t2 +} {1 1 1 2 1 1 3 1 1 4 1 1 5 1 1 6 1 1 7 1 1 8 1 1 9 1 1 + 10 1 1 11 1 1 12 1 1 13 1 1 14 1 1 15 15 2 16 15 2 17 15 2 + 18 15 2 19 15 2 20 15 2 21 15 2 22 15 2 23 15 2 24 15 2 + 25 15 2 26 15 2 27 15 2 28 15 2 29 15 2 30 15 2 31 31 3 + 32 31 3 33 31 3 34 31 3 35 31 3 36 31 3 37 31 3 38 31 3 + 39 31 3 40 31 3 41 31 3 42 31 3 43 31 3 44 31 3 45 31 3 + 46 31 3 47 31 3 48 31 3 49 31 3 50 50 4 51 50 4 52 50 4 + 53 50 4 54 50 4 55 50 4 56 50 4 57 50 4 58 50 4 59 50 4 + 60 50 4 61 50 4 62 50 4 63 50 4 64 50 4 65 50 4 66 50 4 + 67 50 4 68 50 4 69 50 4 70 50 4 71 71 5 72 71 5 73 71 5 + 74 71 5 75 71 5 76 71 5 77 71 5 78 71 5 79 71 5 80 71 5 + 81 71 5 82 71 5 83 71 5 84 71 5 85 71 5 86 71 5 1 1 1 2 1 1 + 3 1 1 4 1 1 5 1 1 6 1 1 7 1 1 8 1 1 9 1 1 10 1 1 11 1 1 + 12 1 1 13 1 1 14 1 1 15 1 1 16 1 1 17 1 1 18 1 1 19 1 1 + 20 1 1 21 1 1 22 22 2 23 22 2 24 22 2 25 22 2 26 22 2 27 22 2 + 28 22 2 29 22 2 30 22 2 31 22 2 32 22 2 33 22 2 34 22 2 + 35 22 2 36 22 2 37 22 2 38 22 2 39 22 2 40 22 2 41 22 2 + 42 22 2 43 43 3 44 43 3 45 43 3 46 43 3 47 43 3 48 43 3 + 49 43 3 50 43 3 51 43 3 52 43 3 53 43 3 54 43 3 55 43 3 + 56 43 3 57 43 3 58 43 3 59 43 3 60 43 3 61 43 3 62 43 3 + 63 43 3 64 64 4 65 64 4 66 64 4 67 64 4 68 64 4 69 64 4 + 70 64 4 71 64 4 72 64 4 73 64 4 74 64 4 75 64 4 76 64 4 + 77 64 4 78 64 4 79 64 4 80 64 4 81 64 4 82 64 4 83 64 4 + 84 84 5 85 84 5 86 84 5 87 84 5 88 84 5 89 84 5 90 84 5 + 91 84 5 92 84 5 93 84 5 94 84 5 95 84 5 96 84 5 97 84 5 + 98 84 5 99 84 5 100 84 5 101 84 5 102 84 5 103 84 5 104 84 5 + 105 84 5} + + +do_test 1.18.7.1 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0053 0.0105 0.0158 0.0211 0.0263 0.0316 0.0368 0.0421 0.0474 0.0526 0.0579 0.0632 0.0684 0.0737 0.0789 0.0842 0.0895 0.0947 0.1000 0.1053 0.1105 0.1158 0.1211 0.1263 0.1316 0.1368 0.1421 0.1474 0.1526 0.1579 0.1632 0.1684 0.1737 0.1789 0.1842 0.1895 0.1947 0.2000 0.2053 0.2105 0.2158 0.2211 0.2263 0.2316 0.2368 0.2421 0.2474 0.2526 0.2579 0.2632 0.2684 0.2737 0.2789 0.2842 0.2895 0.2947 0.3000 0.3053 0.3105 0.3158 0.3211 0.3263 0.3316 0.3368 0.3421 0.3474 0.3526 0.3579 0.3632 0.3684 0.3737 0.3789 0.3842 0.3895 0.3947 0.4000 0.4053 0.4105 0.4158 0.4211 0.4263 0.4316 0.4368 0.4421 0.4474 0.4526 0.4579 0.4632 0.4684 0.4737 0.4789 0.4842 0.4895 0.4947 0.5000 0.5053 0.5105 0.5158 0.5211 0.5263 0.5316 0.5368 0.5421 0.5474 0.5526 0.5579 0.5632 0.5684 0.5737 0.5789 0.5842 0.5895 0.5947 0.6000 0.6053 0.6105 0.6158 0.6211 0.6263 0.6316 0.6368 0.6421 0.6474 0.6526 0.6579 0.6632 0.6684 0.6737 0.6789 0.6842 0.6895 0.6947 0.7000 0.7053 0.7105 0.7158 0.7211 0.7263 0.7316 0.7368 0.7421 0.7474 0.7526 0.7579 0.7632 0.7684 0.7737 0.7789 0.7842 0.7895 0.7947 0.8000 0.8053 0.8105 0.8158 0.8211 0.8263 0.8316 0.8368 0.8421 0.8474 0.8526 0.8579 0.8632 0.8684 0.8737 0.8789 0.8842 0.8895 0.8947 0.9000 0.9053 0.9105 0.9158 0.9211 0.9263 0.9316 0.9368 0.9421 0.9474 0.9526 0.9579 0.9632 0.9684 0.9737 0.9789 0.9842 0.9895 0.9947 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.18.7.2 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0769 0.1538 0.2308 0.3077 0.3846 0.4615 0.5385 0.6154 0.6923 0.7692 0.8462 0.9231 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0667 0.1333 0.2000 0.2667 0.3333 0.4000 0.4667 0.5333 0.6000 0.6667 0.7333 0.8000 0.8667 0.9333 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0556 0.1111 0.1667 0.2222 0.2778 0.3333 0.3889 0.4444 0.5000 0.5556 0.6111 0.6667 0.7222 0.7778 0.8333 0.8889 0.9444 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0526 0.1053 0.1579 0.2105 0.2632 0.3158 0.3684 0.4211 0.4737 0.5263 0.5789 0.6316 0.6842 0.7368 0.7895 0.8421 0.8947 0.9474 1.0000 0.0000 0.0667 0.1333 0.2000 0.2667 0.3333 0.4000 0.4667 0.5333 0.6000 0.6667 0.7333 0.8000 0.8667 0.9333 1.0000 0.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.18.7.3 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY b ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0105 0.0105 0.0211 0.0211 0.0316 0.0368 0.0421 0.0474 0.0474 0.0474 0.0632 0.0632 0.0632 0.0789 0.0789 0.0789 0.0947 0.1000 0.1000 0.1105 0.1105 0.1105 0.1263 0.1263 0.1368 0.1421 0.1421 0.1421 0.1579 0.1579 0.1579 0.1737 0.1789 0.1842 0.1895 0.1895 0.2000 0.2000 0.2105 0.2105 0.2211 0.2263 0.2316 0.2316 0.2421 0.2421 0.2526 0.2579 0.2579 0.2579 0.2737 0.2737 0.2737 0.2895 0.2895 0.3000 0.3053 0.3053 0.3053 0.3053 0.3263 0.3263 0.3263 0.3263 0.3474 0.3474 0.3579 0.3579 0.3579 0.3579 0.3789 0.3789 0.3895 0.3895 0.4000 0.4000 0.4000 0.4158 0.4211 0.4211 0.4316 0.4368 0.4368 0.4474 0.4474 0.4579 0.4579 0.4684 0.4684 0.4684 0.4684 0.4895 0.4947 0.5000 0.5053 0.5105 0.5158 0.5211 0.5211 0.5316 0.5316 0.5316 0.5474 0.5526 0.5526 0.5526 0.5526 0.5737 0.5737 0.5737 0.5737 0.5947 0.6000 0.6000 0.6105 0.6105 0.6211 0.6263 0.6316 0.6316 0.6316 0.6474 0.6526 0.6579 0.6632 0.6684 0.6737 0.6737 0.6737 0.6895 0.6895 0.6895 0.7053 0.7053 0.7053 0.7211 0.7211 0.7211 0.7368 0.7421 0.7421 0.7526 0.7526 0.7632 0.7684 0.7684 0.7789 0.7789 0.7789 0.7947 0.8000 0.8053 0.8053 0.8053 0.8053 0.8263 0.8263 0.8263 0.8421 0.8474 0.8474 0.8579 0.8632 0.8632 0.8632 0.8789 0.8789 0.8789 0.8947 0.8947 0.8947 0.8947 0.8947 0.9211 0.9211 0.9211 0.9368 0.9421 0.9421 0.9421 0.9579 0.9579 0.9579 0.9737 0.9737 0.9842 0.9842 0.9947 0.9947} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.18.7.4 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0769 0.1538 0.1538 0.1538 0.3846 0.4615 0.5385 0.6154 0.6923 0.6923 0.8462 0.8462 0.8462 0.0000 0.0000 0.1000 0.1000 0.2000 0.2000 0.3000 0.3000 0.4000 0.4000 0.5000 0.5500 0.5500 0.6500 0.6500 0.6500 0.8000 0.8000 0.8000 0.8000 0.8000 0.0000 0.0000 0.1333 0.1333 0.1333 0.3333 0.3333 0.4667 0.5333 0.6000 0.6667 0.6667 0.8000 0.8000 0.8000 1.0000 0.0000 0.0000 0.1000 0.1000 0.2000 0.2000 0.3000 0.3000 0.3000 0.3000 0.5000 0.5000 0.6000 0.6500 0.7000 0.7000 0.7000 0.8500 0.9000 0.9000 0.9000 0.0000 0.0556 0.1111 0.1667 0.1667 0.1667 0.1667 0.3889 0.3889 0.5000 0.5556 0.6111 0.6111 0.6111 0.7778 0.7778 0.7778 0.7778 1.0000 0.0000 0.0500 0.0500 0.0500 0.2000 0.2500 0.2500 0.3500 0.3500 0.4500 0.4500 0.4500 0.6000 0.6000 0.6000 0.7500 0.7500 0.7500 0.9000 0.9000 0.9000 0.0000 0.0500 0.0500 0.0500 0.2000 0.2000 0.3000 0.3000 0.3000 0.3000 0.5000 0.5000 0.6000 0.6000 0.6000 0.7500 0.8000 0.8500 0.9000 0.9000 0.9000 0.0000 0.0000 0.0000 0.1579 0.2105 0.2105 0.3158 0.3158 0.4211 0.4211 0.4211 0.4211 0.6316 0.6842 0.7368 0.7368 0.8421 0.8421 0.9474 0.9474 0.0000 0.0000 0.0000 0.2000 0.2667 0.2667 0.4000 0.4000 0.4000 0.4000 0.6667 0.7333 0.7333 0.8667 0.9333 0.9333 0.0000 0.0000 0.0000 0.1429 0.1905 0.1905 0.1905 0.3333 0.3333 0.3333 0.4762 0.5238 0.5238 0.5238 0.5238 0.7143 0.7619 0.8095 0.8095 0.8095 0.9524 0.9524} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.18.7.5 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.18.7.6 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER (PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.18.8.1 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0052 0.0105 0.0157 0.0209 0.0262 0.0314 0.0366 0.0419 0.0471 0.0524 0.0576 0.0628 0.0681 0.0733 0.0785 0.0838 0.0890 0.0942 0.0995 0.1047 0.1099 0.1152 0.1204 0.1257 0.1309 0.1361 0.1414 0.1466 0.1518 0.1571 0.1623 0.1675 0.1728 0.1780 0.1832 0.1885 0.1937 0.1990 0.2042 0.2094 0.2147 0.2199 0.2251 0.2304 0.2356 0.2408 0.2461 0.2513 0.2565 0.2618 0.2670 0.2723 0.2775 0.2827 0.2880 0.2932 0.2984 0.3037 0.3089 0.3141 0.3194 0.3246 0.3298 0.3351 0.3403 0.3455 0.3508 0.3560 0.3613 0.3665 0.3717 0.3770 0.3822 0.3874 0.3927 0.3979 0.4031 0.4084 0.4136 0.4188 0.4241 0.4293 0.4346 0.4398 0.4450 0.4503 0.4555 0.4607 0.4660 0.4712 0.4764 0.4817 0.4869 0.4921 0.4974 0.5026 0.5079 0.5131 0.5183 0.5236 0.5288 0.5340 0.5393 0.5445 0.5497 0.5550 0.5602 0.5654 0.5707 0.5759 0.5812 0.5864 0.5916 0.5969 0.6021 0.6073 0.6126 0.6178 0.6230 0.6283 0.6335 0.6387 0.6440 0.6492 0.6545 0.6597 0.6649 0.6702 0.6754 0.6806 0.6859 0.6911 0.6963 0.7016 0.7068 0.7120 0.7173 0.7225 0.7277 0.7330 0.7382 0.7435 0.7487 0.7539 0.7592 0.7644 0.7696 0.7749 0.7801 0.7853 0.7906 0.7958 0.8010 0.8063 0.8115 0.8168 0.8220 0.8272 0.8325 0.8377 0.8429 0.8482 0.8534 0.8586 0.8639 0.8691 0.8743 0.8796 0.8848 0.8901 0.8953 0.9005 0.9058 0.9110 0.9162 0.9215 0.9267 0.9319 0.9372 0.9424 0.9476 0.9529 0.9581 0.9634 0.9686 0.9738 0.9791 0.9843 0.9895 0.9948 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.18.8.2 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0714 0.1429 0.2143 0.2857 0.3571 0.4286 0.5000 0.5714 0.6429 0.7143 0.7857 0.8571 0.9286 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0625 0.1250 0.1875 0.2500 0.3125 0.3750 0.4375 0.5000 0.5625 0.6250 0.6875 0.7500 0.8125 0.8750 0.9375 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0526 0.1053 0.1579 0.2105 0.2632 0.3158 0.3684 0.4211 0.4737 0.5263 0.5789 0.6316 0.6842 0.7368 0.7895 0.8421 0.8947 0.9474 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0625 0.1250 0.1875 0.2500 0.3125 0.3750 0.4375 0.5000 0.5625 0.6250 0.6875 0.7500 0.8125 0.8750 0.9375 1.0000 0.0455 0.0909 0.1364 0.1818 0.2273 0.2727 0.3182 0.3636 0.4091 0.4545 0.5000 0.5455 0.5909 0.6364 0.6818 0.7273 0.7727 0.8182 0.8636 0.9091 0.9545 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.18.8.3 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY b ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0105 0.0105 0.0209 0.0209 0.0314 0.0314 0.0366 0.0419 0.0471 0.0628 0.0628 0.0628 0.0785 0.0785 0.0785 0.0942 0.0942 0.0942 0.0995 0.1099 0.1099 0.1257 0.1257 0.1257 0.1361 0.1361 0.1414 0.1571 0.1571 0.1571 0.1728 0.1728 0.1728 0.1780 0.1832 0.1885 0.1990 0.1990 0.2094 0.2094 0.2199 0.2199 0.2251 0.2304 0.2408 0.2408 0.2513 0.2513 0.2565 0.2723 0.2723 0.2723 0.2880 0.2880 0.2880 0.2984 0.2984 0.3037 0.3246 0.3246 0.3246 0.3246 0.3455 0.3455 0.3455 0.3455 0.3560 0.3560 0.3770 0.3770 0.3770 0.3770 0.3874 0.3874 0.3979 0.3979 0.4136 0.4136 0.4136 0.4188 0.4293 0.4293 0.4346 0.4450 0.4450 0.4555 0.4555 0.4660 0.4660 0.4869 0.4869 0.4869 0.4869 0.4921 0.4974 0.5026 0.5079 0.5131 0.5183 0.5288 0.5288 0.5445 0.5445 0.5445 0.5497 0.5707 0.5707 0.5707 0.5707 0.5916 0.5916 0.5916 0.5916 0.5969 0.6073 0.6073 0.6178 0.6178 0.6230 0.6283 0.6440 0.6440 0.6440 0.6492 0.6545 0.6597 0.6649 0.6702 0.6859 0.6859 0.6859 0.7016 0.7016 0.7016 0.7173 0.7173 0.7173 0.7330 0.7330 0.7330 0.7382 0.7487 0.7487 0.7592 0.7592 0.7644 0.7749 0.7749 0.7906 0.7906 0.7906 0.7958 0.8010 0.8220 0.8220 0.8220 0.8220 0.8377 0.8377 0.8377 0.8429 0.8534 0.8534 0.8586 0.8743 0.8743 0.8743 0.8901 0.8901 0.8901 0.9162 0.9162 0.9162 0.9162 0.9162 0.9319 0.9319 0.9319 0.9372 0.9529 0.9529 0.9529 0.9686 0.9686 0.9686 0.9791 0.9791 0.9895 0.9895 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.18.8.4 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0714 0.1429 0.3571 0.3571 0.3571 0.4286 0.5000 0.5714 0.6429 0.7857 0.7857 1.0000 1.0000 1.0000 0.0952 0.0952 0.1905 0.1905 0.2857 0.2857 0.3810 0.3810 0.4762 0.4762 0.5238 0.6190 0.6190 0.7619 0.7619 0.7619 1.0000 1.0000 1.0000 1.0000 1.0000 0.1250 0.1250 0.3125 0.3125 0.3125 0.4375 0.4375 0.5000 0.5625 0.6250 0.7500 0.7500 0.9375 0.9375 0.9375 1.0000 0.0952 0.0952 0.1905 0.1905 0.2857 0.2857 0.4762 0.4762 0.4762 0.4762 0.5714 0.5714 0.6190 0.6667 0.8095 0.8095 0.8095 0.8571 1.0000 1.0000 1.0000 0.0526 0.1053 0.1579 0.3684 0.3684 0.3684 0.3684 0.4737 0.4737 0.5263 0.5789 0.7368 0.7368 0.7368 0.9474 0.9474 0.9474 0.9474 1.0000 0.0476 0.1905 0.1905 0.1905 0.2381 0.3333 0.3333 0.4286 0.4286 0.5714 0.5714 0.5714 0.7143 0.7143 0.7143 0.8571 0.8571 0.8571 1.0000 1.0000 1.0000 0.0476 0.1905 0.1905 0.1905 0.2857 0.2857 0.4762 0.4762 0.4762 0.4762 0.5714 0.5714 0.7143 0.7143 0.7143 0.7619 0.8095 0.8571 1.0000 1.0000 1.0000 0.1500 0.1500 0.1500 0.2000 0.3000 0.3000 0.4000 0.4000 0.6000 0.6000 0.6000 0.6000 0.6500 0.7000 0.8000 0.8000 0.9000 0.9000 1.0000 1.0000 0.1875 0.1875 0.1875 0.2500 0.3750 0.3750 0.6250 0.6250 0.6250 0.6250 0.6875 0.8125 0.8125 0.8750 1.0000 1.0000 0.1364 0.1364 0.1364 0.1818 0.3182 0.3182 0.3182 0.4545 0.4545 0.4545 0.5000 0.6818 0.6818 0.6818 0.6818 0.7273 0.7727 0.9091 0.9091 0.9091 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.18.8.5 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.18.8.6 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.18.8.1 { + set myres {} + foreach r [db eval {SELECT ntile(100) OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 88.0000 89.0000 89.0000 90.0000 90.0000 91.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.18.8.2 { + set myres {} + foreach r [db eval {SELECT ntile(101) OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 22.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.18.8.3 { + set myres {} + foreach r [db eval {SELECT ntile(102) OVER ( ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 88.0000 89.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.18.8.4 { + set myres {} + foreach r [db eval {SELECT ntile(103) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 22.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.18.8.5 { + set myres {} + foreach r [db eval {SELECT ntile(104) OVER ( ORDER BY b%10,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000 103.0000 104.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.18.8.6 { + set myres {} + foreach r [db eval {SELECT ntile(105) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.18.8.7 { + set myres {} + foreach r [db eval {SELECT ntile(105) OVER ( ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 88.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000 103.0000 104.0000 105.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + +do_execsql_test 1.18.9.1 { + SELECT last_value(a+b) OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2 +} {207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 216} + +do_execsql_test 1.18.9.2 { + SELECT last_value(a+b) OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2 +} {223 223 223 223 223 223 223 223 223 223 223 223 223 + 196 210 210 210 210 210 210 210 210 210 210 210 210 + 210 210 210 210 210 210 210 210 263 280 280 280 280 + 280 280 280 280 280 280 280 280 280 280 280 234 279 + 279 279 279 279 279 279 279 279 279 279 279 279 279 + 279 279 279 279 279 279 221 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 235 + 274 274 274 274 274 274 274 274 274 274 274 274 274 + 274 274 274 274 274 274 274 189 212 212 212 212 212 + 212 212 212 212 212 212 212 212 212 212 212 212 212 + 212 212 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 216 232 232 232 + 232 232 232 232 232 232 232 232 232 232 232 232 250 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 249} + +do_execsql_test 1.18.9.3 { + SELECT last_value(a+b) OVER ( ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2 +} {276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 120} + +do_execsql_test 1.18.9.4 { + SELECT last_value(a+b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2 +} {240 240 240 240 240 240 240 240 240 240 240 240 240 + 218 263 263 263 263 263 263 263 263 263 263 263 263 + 263 263 263 263 263 263 263 263 261 280 280 280 280 + 280 280 280 280 280 280 280 280 280 280 280 228 252 + 252 252 252 252 252 252 252 252 252 252 252 252 252 + 252 252 252 252 252 252 130 171 171 171 171 171 171 + 171 171 171 171 171 171 171 171 171 171 171 171 271 + 274 274 274 274 274 274 274 274 274 274 274 274 274 + 274 274 274 274 274 274 274 213 226 226 226 226 226 + 226 226 226 226 226 226 226 226 226 226 226 226 226 + 226 226 195 124 124 124 124 124 124 124 124 124 124 + 124 124 124 124 124 124 124 124 124 119 198 198 198 + 198 198 198 198 198 198 198 198 198 198 198 198 171 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 120} + +do_execsql_test 1.18.9.5 { + SELECT last_value(a+b) OVER ( ORDER BY b%10,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2 +} {229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 249} + +do_execsql_test 1.18.9.6 { + SELECT last_value(a+b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.18.10.1 { + SELECT nth_value(b,b+1) OVER (ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW) FROM t2 +} {98 9 73 75 43 78 32 4 5 25 27 15 31 24 43 98 + 94 79 77 61 22 79 8 73 3 85 43 34 98 36 63 91 + 11 20 21 75 90 56 96 84 5 28 33 5 56 59 81 19 + 87 36 2 26 63 73 89 95 74 73 79 29 70 65 33 + 14 26 47 94 14 15 5 72 41 11 75 19 53 91 79 + 37 91 93 59 58 36 23 84 33 73 47 58 34 58 73 + 15 13 7 11 99 31 66 38 80 79 95 60 59 19 59 + {} 85 39 27 {} 9 59 75 91 33 {} 84 {} 33 50 + {} {} 36 77 29 {} 30 {} 12 {} 17 75 58 62 91 + 58 {} {} 50 {} 27 75 {} {} {} 33 {} {} {} {} + 62 50 17 36 36 95 {} {} {} {} {} 10 {} {} {} + 99 {} {} {} {} {} {} {} 30 {} {} {} {} {} {} + {} {} {} {} {} {} {} {}} + +do_execsql_test 1.18.10.2 { + SELECT nth_value(b,b+1) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 61 {} {} {} 81 {} 91 {} {} {} {} + {} {} {} {} {} {} 62 {} {} {} 22 {} {} {} 12 + {} {} {} {} {} {} {} {} {} {} 43 {} {} {} {} + {} 83 {} {} {} {} {} {} {} 33 {} {} {} {} {} + {} 94 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} 85 {} {} {} {} {} {} 95 {} + {} {} {} {} {} {} {} {} {} 56 {} 36 66 {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 87 {} {} {} {} {} 37 {} {} {} {} + {} {} {} {} {} {} 8 {} {} {} {} {} 58 {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 19 {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.18.10.3 { + SELECT nth_value(b,b+1) OVER ( ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2 +} {2 2 2 2 2 3 5 7 7 8 8 9 9 10 11 12 12 12 + 13 15 15 16 16 16 19 20 21 22 23 23 25 26 26 + 27 29 30 30 31 32 33 33 33 34 34 35 36 36 36 + 37 38 39 39 40 41 41 43 43 44 46 47 47 47 49 + 50 51 52 54 55 56 56 56 57 58 58 59 59 59 60 + 61 62 63 64 65 66 67 69 70 72 73 73 74 74 74 + 75 77 78 79 80 81 82 83 84 84 84 85 86 87 87 + 88 89 89 90 90 91 91 91 93 93 94 95 96 96 96 + 97 98 99 {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {}} + +do_execsql_test 1.18.10.4 { + SELECT nth_value(b,b+1) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2 +} {90 {} {} {} {} {} {} {} {} {} {} {} {} {} 11 + 11 61 61 {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} 12 12 72 72 72 {} {} {} {} {} + {} {} {} {} {} {} 23 23 73 73 {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} 34 84 {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 35 85 85 85 {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} 36 86 86 86 {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 47 47 + 47 97 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} 58 58 58 {} {} {} {} {} {} {} {} {} + {} {} {} {} 49 49 49 99 {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.18.10.5 { + SELECT nth_value(b,b+1) OVER ( ORDER BY b%10,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2 +} {65 72 81 34 41 35 73 74 14 15 32 53 1 2 35 5 + 74 95 55 61 75 63 34 81 12 91 74 93 43 33 22 + 75 56 56 23 85 72 23 33 84 22 15 93 73 12 6 + 46 43 75 75 36 87 54 87 85 43 15 84 85 56 66 + 83 36 97 85 78 34 94 75 33 65 17 36 68 7 15 + 94 38 78 27 95 96 36 68 7 15 16 38 89 66 56 + 38 76 89 89 47 85 56 59 {} 8 88 27 95 16 {} + 98 79 39 59 16 {} {} 8 16 98 36 66 {} 99 29 + 59 97 8 {} {} {} 27 17 8 {} 8 58 {} 98 {} {} + {} 29 87 19 {} 89 {} {} 37 {} {} {} 59 59 58 + 8 99 {} {} 98 29 {} {} {} {} 58 {} 89 {} {} + {} {} {} {} {} {} {} {} {} {} {} 19 {} {} {} + {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.18.10.6 { + SELECT nth_value(b,b+1) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.18.11.1 { + SELECT first_value(b) OVER (ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW) FROM t2 +} {81 89 89 89 89 81 96 59 38 68 39 62 91 46 6 + 99 97 27 46 78 54 97 8 67 29 93 84 77 23 16 + 16 93 65 35 47 7 86 74 61 91 85 24 85 43 59 + 12 32 56 3 91 22 90 55 15 28 89 25 47 1 56 40 + 43 56 16 75 36 89 98 76 81 4 94 42 30 78 33 + 29 53 63 2 87 37 80 84 72 41 9 61 73 95 65 13 + 58 96 98 1 21 74 65 35 5 73 11 51 87 41 12 8 + 20 31 31 15 95 22 73 79 88 34 8 11 49 34 90 + 59 96 60 55 75 77 44 2 7 85 57 74 29 70 59 19 + 39 26 26 47 80 90 36 58 47 9 72 72 66 33 93 + 75 64 81 9 23 37 13 12 14 62 91 36 91 33 15 + 34 36 99 3 95 69 58 52 30 50 84 10 84 33 21 + 39 44 58 30 38 34 83} + +do_execsql_test 1.18.11.2 { + SELECT first_value(b) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW) FROM t2 +} {40 90 90 90 90 40 30 80 20 90 60 70 80 90 91 + 81 81 81 81 91 61 91 91 1 81 41 61 1 21 11 51 + 41 31 31 11 12 62 62 62 62 12 32 22 42 2 72 + 12 22 2 72 72 23 93 93 93 93 23 93 43 3 43 33 + 53 63 73 13 73 73 33 93 23 13 84 54 54 54 54 + 84 74 24 4 94 84 74 34 34 44 74 64 14 34 35 + 65 65 65 65 35 85 85 55 15 25 75 95 65 65 35 + 5 15 95 55 75 46 96 96 96 96 46 6 46 16 16 86 + 56 56 56 16 36 76 96 96 26 26 27 97 97 97 97 + 27 97 67 77 47 7 47 87 37 87 77 7 57 47 47 68 + 38 38 38 38 68 78 8 28 98 78 58 98 8 88 8 59 + 89 89 89 89 59 39 99 29 59 89 89 29 9 79 49 + 59 29 59 19 39 9} + +do_execsql_test 1.18.11.3 { + SELECT first_value(b) OVER ( ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2 +} {1 1 1 1 1 1 2 2 3 3 4 5 6 7 7 7 8 8 8 + 9 9 9 10 11 11 12 12 12 13 13 14 15 15 15 16 + 16 16 17 19 20 21 21 22 22 23 23 24 25 26 26 + 27 27 28 29 29 29 30 30 30 31 31 32 33 33 33 + 33 34 34 34 34 35 35 36 36 36 36 37 37 38 38 + 39 39 39 40 41 41 42 43 43 44 44 46 46 47 47 + 47 47 49 50 51 52 53 54 55 55 56 56 56 57 58 + 58 58 58 59 59 59 59 60 61 61 62 62 63 64 65 + 65 65 66 67 68 69 70 72 72 72 73 73 73 74 74 + 74 75 75 75 76 77 77 78 78 79 80 80 81 81 81 + 82 83 84 84 84 84 85 85 85 86 87 87 88 89 89 + 89 90 90 90 91 91 91 91 91 93 93 93 94 95 95 + 95 96 96 96 97 97} + +do_execsql_test 1.18.11.4 { + SELECT first_value(b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2 +} {20 10 10 10 10 20 30 30 30 40 50 60 70 80 1 1 + 1 1 1 1 11 11 21 21 31 31 41 41 51 61 61 81 + 81 81 91 2 2 2 2 2 2 12 12 12 22 22 32 42 + 52 62 62 3 3 3 3 3 3 13 13 23 23 33 33 33 + 33 43 43 53 63 73 73 73 14 4 4 4 4 14 24 34 + 34 34 34 44 44 54 64 74 74 74 84 15 5 5 5 5 + 15 15 15 25 35 35 55 55 65 65 65 75 75 75 85 + 85 16 6 6 6 6 16 16 16 26 26 36 36 36 36 46 + 46 56 56 56 66 76 7 7 7 7 7 7 7 17 27 27 37 + 37 47 47 47 47 57 67 77 77 8 8 8 8 8 8 8 28 + 38 38 58 58 58 58 68 78 9 9 9 9 9 9 9 19 29 + 29 29 39 39 39 49 59 59 59 59 69 79 89} + +do_execsql_test 1.18.11.5 { + SELECT first_value(b) OVER ( ORDER BY b%10,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2 +} {40 90 90 90 90 40 30 80 20 90 60 70 80 90 30 + 50 10 30 81 91 61 91 91 1 81 41 61 1 21 11 51 + 41 31 31 11 81 91 91 21 62 12 32 22 42 2 72 + 12 22 2 72 72 12 62 52 82 93 23 93 43 3 43 33 + 53 63 73 13 73 73 33 93 23 13 33 3 33 83 54 + 84 74 24 4 94 84 74 34 34 44 74 64 14 34 84 + 84 44 34 65 35 85 85 55 15 25 75 95 65 65 35 + 5 15 95 55 75 85 75 15 95 96 46 6 46 16 16 86 + 56 56 56 16 36 76 96 96 26 26 36 66 36 36 97 + 27 97 67 77 47 7 47 87 37 87 77 7 57 47 47 37 + 27 17 7 38 68 78 8 28 98 78 58 98 8 88 8 58 + 58 58 38 89 59 39 99 29 59 89 89 29 9 79 49 + 59 29 59 19 39 9} + +do_execsql_test 1.18.11.6 { + SELECT first_value(b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.18.12.1 { + SELECT lead(b,b) OVER (ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW) FROM t2 +} {96 9 11 89 32 53 91 30 51 56 54 73 22 59 75 + 74 78 8 16 65 15 8 31 87 90 12 32 96 74 76 37 + 85 90 15 35 2 60 36 75 9 51 47 63 51 90 26 42 + 26 8 76 80 90 37 87 56 79 5 87 8 2 39 73 64 + 36 90 72 78 36 73 51 33 20 41 2 26 37 33 8 14 + 33 81 55 1 9 12 39 64 87 72 34 82 21 34 99 62 + 74 41 69 22 75 27 58 8 79 77 26 26 55 {} 29 + 30 7 {} 66 55 2 34 64 {} 33 {} 44 84 {} {} 95 + 85 19 {} 83 {} 91 {} {} 9 50 91 33 34 {} {} + 84 {} 7 9 {} {} {} 44 {} {} {} {} 91 84 {} 95 + 95 52 {} {} {} {} {} 21 {} {} {} 58 {} {} {} + {} {} {} {} 83 {} {} {} {} {} {} {} {} {} {} + {} {} {} {}} + +do_execsql_test 1.18.12.2 { + SELECT lead(b,b) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 81 {} {} {} 21 {} {} {} {} {} {} + {} {} {} {} {} {} 62 {} {} {} 12 {} {} {} 72 + {} {} {} {} {} {} {} {} {} {} 53 {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 34 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} 95 {} {} {} {} {} {} 85 {} + {} {} {} {} {} {} {} {} {} 56 {} 36 {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 57 {} {} {} {} {} 7 {} {} {} {} + {} {} {} {} {} {} 8 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 9 {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.18.12.3 { + SELECT lead(b,b) OVER ( ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2 +} {1 2 3 3 5 6 7 8 8 9 9 10 11 12 12 13 13 + 14 15 16 16 17 19 20 21 22 23 24 25 26 27 27 + 28 29 30 31 32 33 33 33 34 34 35 36 36 36 37 + 38 39 39 40 41 42 43 43 44 46 47 47 47 49 50 + 52 53 54 55 56 56 57 58 58 58 59 59 59 60 61 + 62 62 64 65 65 67 69 70 72 72 73 74 74 75 75 + 75 77 78 80 81 81 83 84 84 85 85 85 87 88 89 + 89 89 90 90 91 91 91 93 93 94 95 95 96 97 97 + 98 99 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.18.12.4 { + SELECT lead(b,b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2 +} {80 {} {} {} {} {} {} {} {} {} {} {} {} {} 1 + 11 81 81 {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} 12 12 72 82 {} {} {} {} {} {} + {} {} {} {} {} {} 13 23 73 73 {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} 34 84 {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 35 85 85 95 {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} 36 86 96 96 {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 37 47 + 47 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} 58 58 68 {} {} {} {} {} {} {} {} {} + {} {} {} {} 39 49 59 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.18.12.5 { + SELECT lead(b,b) OVER ( ORDER BY b%10,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2 +} {34 72 91 74 21 55 23 94 84 95 2 13 61 22 55 + 55 94 85 75 81 65 73 74 21 72 12 94 33 63 73 + 72 96 36 76 3 25 62 3 73 34 12 46 43 93 72 16 + 86 63 15 65 36 77 24 57 25 53 95 34 95 16 97 + 74 97 67 25 98 44 34 65 54 5 68 96 28 47 95 + 34 39 8 38 6 46 96 28 47 95 56 39 99 97 76 8 + 26 9 79 27 95 16 29 {} 58 58 77 85 56 {} 98 + 29 {} 19 96 {} {} 78 56 98 36 97 {} 89 89 29 + 47 78 {} {} {} 38 68 58 {} 58 38 {} 98 {} {} + {} 39 57 9 {} 79 {} {} 7 {} {} {} 9 29 38 78 + {} {} {} 8 39 {} {} {} {} 59 {} 99 {} {} {} + {} {} {} {} {} {} {} {} {} {} 9 {} {} {} {} + {} {} {} {} {} {} {} {}} + +do_execsql_test 1.18.12.6 { + SELECT lead(b,b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.18.13.1 { + SELECT lag(b,b) OVER (ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} 38 {} {} {} {} + {} {} {} 6 {} {} {} {} {} 81 46 6 {} {} {} {} + 23 {} {} {} {} {} 27 {} {} {} 35 6 {} 12 {} + 23 {} {} 61 84 {} 93 39 47 {} 54 46 96 56 {} + 16 {} {} {} {} 89 {} 16 43 {} 85 56 29 99 53 + {} 59 {} {} 91 59 53 84 99 {} 93 63 47 {} {} + 98 33 67 35 75 1 23 13 55 27 75 98 35 73 63 2 + 21 27 13 24 86 23 84 31 20 94 61 65 75 23 36 + 94 55 90 41 77 96 56 29 40 12 89 63 11 5 73 + 79 1 16 28 31 73 5 39 53 63 41 11 40 2 13 33 + 9 29 90 47 72 9 73 30 44 33 74 93 29 74 42 34 + 63 41 34 96 47 77 1 36 74 72 14 36 26 77 9 72 + 64 8 91 31 52 30} + +do_execsql_test 1.18.13.2 { + SELECT lag(b,b) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} 30 {} {} + {} {} {} {} 91 {} {} {} 61 {} 81 {} {} {} {} + 1 {} {} {} {} {} {} {} {} {} 22 {} {} {} 12 + {} {} 62 {} {} {} {} {} {} {} 23 {} {} {} {} + {} {} {} {} {} {} {} 43 {} 23 {} {} {} {} {} + {} 54 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 75 {} + {} {} {} {} {} 55 {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} 47 {} {} {} {} + {} 27 7 {} {} {} {} {} {} {} {} {} 68 {} 8 {} + {} {} {} {} {} {} {} {} {} {} {} {} 89 {} {} + {} {} {} {} {} 29 9 {} {} {}} + +do_execsql_test 1.18.13.3 { + SELECT lag(b,b) OVER ( ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2 +} {{} 1 1 1 1 2 2 2 2 2 2 3 3 3 4 4 5 6 6 + 6 7 7 7 7 7 8 8 8 8 8 8 9 9 9 9 9 9 9 + 9 9 9 10 10 10 10 11 11 11 11 11 12 12 12 12 + 13 13 13 13 13 14 15 15 15 15 16 16 16 16 16 + 17 19 20 20 21 21 21 21 22 22 22 22 23 23 23 + 23 23 24 23 24 24 25 26 26 26 26 26 26 26 26 + 26 26 26 27 27 27 27 28 29 29 29 29 30 30 30 + 30 30 30 31 31 31 31 31 32 32 32 32 32 32 31 + 32 33 33 33 33 33 33 34 34 34 34 34 34 34 34 + 35 35 35 35 35 36 36 36 36 36 36 36 37 37 37 + 38 38 38 38 38 38 39 39 39 39 40 40 41 41 42 + 43 42 43 43 43 43 44 44 44 46 46 46 47 47 47 + 47 47} + +do_execsql_test 1.18.13.4 { + SELECT lag(b,b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + 1 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.18.13.5 { + SELECT lag(b,b) OVER ( ORDER BY b%10,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} 30 {} {} + {} {} {} {} 91 {} {} {} 61 80 81 {} {} {} {} + 1 {} {} {} 30 {} 21 90 61 {} 22 {} 11 41 12 + {} {} 62 {} {} {} {} 31 {} 50 23 30 21 90 {} + {} 62 {} {} 81 {} 22 43 62 23 32 {} 91 {} 90 + 93 54 {} {} 90 72 12 22 90 81 83 23 80 20 72 + 43 51 33 80 90 2 34 54 1 20 62 12 13 75 44 30 + 93 91 1 21 55 61 61 13 85 3 65 65 91 73 33 93 + 55 84 62 31 11 65 35 85 33 55 15 12 75 22 3 + 73 65 36 85 43 95 43 13 47 44 65 65 96 36 27 + 7 46 34 94 47 36 73 34 35 73 68 24 8 75 85 75 + 66 34 95 36 84 77 46 34 84 47 89 65 36 16 38 + 76 58 57 29 9 44 56 17} + +do_execsql_test 1.18.13.6 { + SELECT lag(b,b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.18.14.1 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW) FROM t2 +} {81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 81.96.59.38.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 96.59.38.68.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 59.38.68.39.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 38.68.39.62.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 68.39.62.91.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 39.62.91.46.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 62.91.46.6.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 91.46.6.99.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 46.6.99.97.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 6.99.97.27.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 99.97.27.46.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 97.27.46.78.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 27.46.78.54.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 46.78.54.97.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 78.54.97.8.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 54.97.8.67.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 97.8.67.29.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 8.67.29.93.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 67.29.93.84.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 29.93.84.77.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 93.84.77.23.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 84.77.23.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 77.23.16.16.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 23.16.16.93.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 16.16.93.65.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 16.93.65.35.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 93.65.35.47.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 65.35.47.7.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 35.47.7.86.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 47.7.86.74.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 7.86.74.61.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 86.74.61.91.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 74.61.91.85.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 61.91.85.24.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 91.85.24.85.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 85.24.85.43.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 24.85.43.59.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 85.43.59.12.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 43.59.12.32.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 59.12.32.56.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 12.32.56.3.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 32.56.3.91.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 56.3.91.22.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 3.91.22.90.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 91.22.90.55.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 22.90.55.15.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 90.55.15.28.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 55.15.28.89.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 15.28.89.25.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 28.89.25.47.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.25.47.1.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 25.47.1.56.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 47.1.56.40.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 1.56.40.43.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 56.40.43.56.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 40.43.56.16.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 43.56.16.75.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 56.16.75.36.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 16.75.36.89.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 75.36.89.98.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 36.89.98.76.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.98.76.81.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 98.76.81.4.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 76.81.4.94.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 81.4.94.42.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 4.94.42.30.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 94.42.30.78.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 42.30.78.33.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 30.78.33.29.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 78.33.29.53.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 33.29.53.63.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 29.53.63.2.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 53.63.2.87.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 63.2.87.37.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 2.87.37.80.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 87.37.80.84.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 37.80.84.72.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 80.84.72.41.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 84.72.41.9.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 72.41.9.61.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 41.9.61.73.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 9.61.73.95.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 61.73.95.65.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 73.95.65.13.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 95.65.13.58.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 65.13.58.96.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 13.58.96.98.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 58.96.98.1.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 96.98.1.21.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 98.1.21.74.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 1.21.74.65.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 21.74.65.35.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 74.65.35.5.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 65.35.5.73.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 35.5.73.11.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 5.73.11.51.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 73.11.51.87.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 11.51.87.41.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 51.87.41.12.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 87.41.12.8.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 41.12.8.20.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 12.8.20.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 8.20.31.31.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 20.31.31.15.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 31.31.15.95.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 31.15.95.22.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 15.95.22.73.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 95.22.73.79.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 22.73.79.88.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 73.79.88.34.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 79.88.34.8.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 88.34.8.11.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 34.8.11.49.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 8.11.49.34.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 11.49.34.90.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 49.34.90.59.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 34.90.59.96.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 90.59.96.60.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 59.96.60.55.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 96.60.55.75.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 60.55.75.77.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 55.75.77.44.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 75.77.44.2.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 77.44.2.7.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 44.2.7.85.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 2.7.85.57.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 7.85.57.74.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 85.57.74.29.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 57.74.29.70.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 74.29.70.59.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 29.70.59.19.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 70.59.19.39.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 59.19.39.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 19.39.26.26.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 39.26.26.47.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 26.26.47.80.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 26.47.80.90.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 47.80.90.36.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 80.90.36.58.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 90.36.58.47.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 36.58.47.9.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 58.47.9.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 47.9.72.72.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 9.72.72.66.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 72.72.66.33.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 72.66.33.93.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 66.33.93.75.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 33.93.75.64.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 93.75.64.81.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 75.64.81.9.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 64.81.9.23.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 81.9.23.37.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 9.23.37.13.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 23.37.13.12.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 37.13.12.14.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 13.12.14.62.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 12.14.62.91.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 14.62.91.36.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 62.91.36.91.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 91.36.91.33.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 36.91.33.15.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 91.33.15.34.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 33.15.34.36.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 15.34.36.99.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 34.36.99.3.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 36.99.3.95.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 99.3.95.69.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 3.95.69.58.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 95.69.58.52.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 69.58.52.30.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 58.52.30.50.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 52.30.50.84.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 30.50.84.10.33.21.39.44.58.30.38.34.83.27.82.17.7 + 50.84.10.84.21.39.44.58.30.38.34.83.27.82.17.7 + 84.10.84.33.39.44.58.30.38.34.83.27.82.17.7 + 10.84.33.21.44.58.30.38.34.83.27.82.17.7 + 84.33.21.39.58.30.38.34.83.27.82.17.7 33.21.39.44.30.38.34.83.27.82.17.7 + 21.39.44.58.38.34.83.27.82.17.7 39.44.58.30.34.83.27.82.17.7 + 44.58.30.38.83.27.82.17.7 58.30.38.34.27.82.17.7 30.38.34.83.82.17.7 + 38.34.83.27.17.7 34.83.27.82.7 83.27.82.17} + +do_execsql_test 1.18.14.2 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW) FROM t2 +} {40.30.80.20.90.60.70.80.90.30.50.10.30 + 90.30.80.20.90.60.70.80.90.30.50.10.30 + 90.40.80.20.90.60.70.80.90.30.50.10.30 + 90.40.30.20.90.60.70.80.90.30.50.10.30 + 90.40.30.80.90.60.70.80.90.30.50.10.30 + 40.30.80.20.60.70.80.90.30.50.10.30 30.80.20.90.70.80.90.30.50.10.30 + 80.20.90.60.80.90.30.50.10.30 20.90.60.70.90.30.50.10.30 + 90.60.70.80.30.50.10.30 60.70.80.90.50.10.30 70.80.90.30.10.30 + 80.90.30.50.30 90.30.50.10 + 91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 91.61.91.91.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 61.91.91.1.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 91.91.1.81.61.1.21.11.51.41.31.31.11.81.91.91.21 + 91.1.81.41.1.21.11.51.41.31.31.11.81.91.91.21 + 1.81.41.61.21.11.51.41.31.31.11.81.91.91.21 + 81.41.61.1.11.51.41.31.31.11.81.91.91.21 + 41.61.1.21.51.41.31.31.11.81.91.91.21 61.1.21.11.41.31.31.11.81.91.91.21 + 1.21.11.51.31.31.11.81.91.91.21 21.11.51.41.31.11.81.91.91.21 + 11.51.41.31.11.81.91.91.21 51.41.31.31.81.91.91.21 41.31.31.11.91.91.21 + 31.31.11.81.91.21 31.11.81.91.21 11.81.91.91 + 12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 62.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 62.12.22.42.2.72.12.22.2.72.72.12.62.52.82 + 62.12.32.42.2.72.12.22.2.72.72.12.62.52.82 + 62.12.32.22.2.72.12.22.2.72.72.12.62.52.82 + 12.32.22.42.72.12.22.2.72.72.12.62.52.82 + 32.22.42.2.12.22.2.72.72.12.62.52.82 22.42.2.72.22.2.72.72.12.62.52.82 + 42.2.72.12.2.72.72.12.62.52.82 2.72.12.22.72.72.12.62.52.82 + 72.12.22.2.72.12.62.52.82 12.22.2.72.12.62.52.82 22.2.72.72.62.52.82 + 2.72.72.12.52.82 72.72.12.62.82 72.12.62.52 + 23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 23.93.43.3.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.43.3.43.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 43.3.43.33.63.73.13.73.73.33.93.23.13.33.3.33.83 + 3.43.33.53.73.13.73.73.33.93.23.13.33.3.33.83 + 43.33.53.63.13.73.73.33.93.23.13.33.3.33.83 + 33.53.63.73.73.73.33.93.23.13.33.3.33.83 + 53.63.73.13.73.33.93.23.13.33.3.33.83 63.73.13.73.33.93.23.13.33.3.33.83 + 73.13.73.73.93.23.13.33.3.33.83 13.73.73.33.23.13.33.3.33.83 + 73.73.33.93.13.33.3.33.83 73.33.93.23.33.3.33.83 33.93.23.13.3.33.83 + 93.23.13.33.33.83 23.13.33.3.83 13.33.3.33 + 84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.74.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.74.24.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 84.74.24.4.84.74.34.34.44.74.64.14.34.84.84.44.34 + 74.24.4.94.74.34.34.44.74.64.14.34.84.84.44.34 + 24.4.94.84.34.34.44.74.64.14.34.84.84.44.34 + 4.94.84.74.34.44.74.64.14.34.84.84.44.34 + 94.84.74.34.44.74.64.14.34.84.84.44.34 + 84.74.34.34.74.64.14.34.84.84.44.34 74.34.34.44.64.14.34.84.84.44.34 + 34.34.44.74.14.34.84.84.44.34 34.44.74.64.34.84.84.44.34 + 44.74.64.14.84.84.44.34 74.64.14.34.84.44.34 64.14.34.84.44.34 + 14.34.84.84.34 34.84.84.44 + 35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 35.85.85.55.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 85.85.55.15.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 85.55.15.25.95.65.65.35.5.15.95.55.75.85.75.15.95 + 55.15.25.75.65.65.35.5.15.95.55.75.85.75.15.95 + 15.25.75.95.65.35.5.15.95.55.75.85.75.15.95 + 25.75.95.65.35.5.15.95.55.75.85.75.15.95 + 75.95.65.65.5.15.95.55.75.85.75.15.95 95.65.65.35.15.95.55.75.85.75.15.95 + 65.65.35.5.95.55.75.85.75.15.95 65.35.5.15.55.75.85.75.15.95 + 35.5.15.95.75.85.75.15.95 5.15.95.55.85.75.15.95 15.95.55.75.75.15.95 + 95.55.75.85.15.95 55.75.85.75.95 75.85.75.15 + 46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 46.6.46.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 6.46.16.16.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 46.16.16.86.56.56.16.36.76.96.96.26.26.36.66.36.36 + 16.16.86.56.56.16.36.76.96.96.26.26.36.66.36.36 + 16.86.56.56.16.36.76.96.96.26.26.36.66.36.36 + 86.56.56.56.36.76.96.96.26.26.36.66.36.36 + 56.56.56.16.76.96.96.26.26.36.66.36.36 + 56.56.16.36.96.96.26.26.36.66.36.36 56.16.36.76.96.26.26.36.66.36.36 + 16.36.76.96.26.26.36.66.36.36 36.76.96.96.26.36.66.36.36 + 76.96.96.26.36.66.36.36 96.96.26.26.66.36.36 96.26.26.36.36.36 + 26.26.36.66.36 26.36.66.36 + 27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.67.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 27.97.67.77.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.67.77.47.47.87.37.87.77.7.57.47.47.37.27.17.7 + 67.77.47.7.87.37.87.77.7.57.47.47.37.27.17.7 + 77.47.7.47.37.87.77.7.57.47.47.37.27.17.7 + 47.7.47.87.87.77.7.57.47.47.37.27.17.7 + 7.47.87.37.77.7.57.47.47.37.27.17.7 47.87.37.87.7.57.47.47.37.27.17.7 + 87.37.87.77.57.47.47.37.27.17.7 37.87.77.7.47.47.37.27.17.7 + 87.77.7.57.47.37.27.17.7 77.7.57.47.37.27.17.7 7.57.47.47.27.17.7 + 57.47.47.37.17.7 47.47.37.27.7 47.37.27.17 + 68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 38.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 38.68.8.28.98.78.58.98.8.88.8.58.58.58.38 + 38.68.78.28.98.78.58.98.8.88.8.58.58.58.38 + 38.68.78.8.98.78.58.98.8.88.8.58.58.58.38 + 68.78.8.28.78.58.98.8.88.8.58.58.58.38 + 78.8.28.98.58.98.8.88.8.58.58.58.38 8.28.98.78.98.8.88.8.58.58.58.38 + 28.98.78.58.8.88.8.58.58.58.38 98.78.58.98.88.8.58.58.58.38 + 78.58.98.8.8.58.58.58.38 58.98.8.88.58.58.58.38 98.8.88.8.58.58.38 + 8.88.8.58.58.38 88.8.58.58.38 8.58.58.58 + 59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 59.39.99.29.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 39.99.29.59.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 99.29.59.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 29.59.89.89.9.79.49.59.29.59.19.39.9.9.99.69.39 + 59.89.89.29.79.49.59.29.59.19.39.9.9.99.69.39 + 89.89.29.9.49.59.29.59.19.39.9.9.99.69.39 + 89.29.9.79.59.29.59.19.39.9.9.99.69.39 + 29.9.79.49.29.59.19.39.9.9.99.69.39 9.79.49.59.59.19.39.9.9.99.69.39 + 79.49.59.29.19.39.9.9.99.69.39 49.59.29.59.39.9.9.99.69.39 + 59.29.59.19.9.9.99.69.39 29.59.19.39.9.99.69.39 59.19.39.9.99.69.39 + 19.39.9.9.69.39 39.9.9.99.39 9.9.99.69} + +do_execsql_test 1.18.14.3 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2 +} {1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.2.2.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 2.2.3.3.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 2.3.3.4.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 3.3.4.5.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 3.4.5.6.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 4.5.6.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 5.6.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 6.7.7.7.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 7.7.7.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 7.7.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 7.8.8.8.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 8.8.8.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 8.8.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 8.9.9.9.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 9.9.9.10.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 9.9.10.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 9.10.11.11.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 10.11.11.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 11.11.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 11.12.12.12.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 12.12.12.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 12.12.13.13.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 12.13.13.14.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 13.13.14.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 13.14.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 14.15.15.15.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 15.15.15.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 15.15.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 15.16.16.16.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 16.16.16.17.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 16.16.17.19.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 16.17.19.20.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 17.19.20.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 19.20.21.21.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 20.21.21.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 21.21.22.22.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 21.22.22.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 22.22.23.23.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 22.23.23.24.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 23.23.24.25.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 23.24.25.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 24.25.26.26.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 25.26.26.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 26.26.27.27.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 26.27.27.28.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 27.27.28.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 27.28.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 28.29.29.29.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 29.29.29.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 29.29.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 29.30.30.30.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 30.30.30.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 30.30.31.31.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 30.31.31.32.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 31.31.32.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 31.32.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 32.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 33.33.33.33.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 33.33.33.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 33.33.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 33.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 34.34.34.34.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 34.34.34.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 34.34.35.35.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 34.35.35.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 35.35.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 35.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 36.36.36.36.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 36.36.36.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 36.36.37.37.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 36.37.37.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 37.37.38.38.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 37.38.38.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 38.38.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 38.39.39.39.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 39.39.39.40.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 39.39.40.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 39.40.41.41.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 40.41.41.42.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 41.41.42.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 41.42.43.43.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 42.43.43.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 43.43.44.44.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 43.44.44.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 44.44.46.46.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 44.46.46.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 46.46.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 46.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 47.47.47.47.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 47.47.47.49.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 47.47.49.50.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 47.49.50.51.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 49.50.51.52.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 50.51.52.53.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 51.52.53.54.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 52.53.54.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 53.54.55.55.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 54.55.55.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 55.55.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 55.56.56.56.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 56.56.56.57.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 56.56.57.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 56.57.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 57.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 58.58.58.58.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 58.58.58.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 58.58.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 58.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 59.59.59.59.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 59.59.59.60.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 59.59.60.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 59.60.61.61.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 60.61.61.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 61.61.62.62.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 61.62.62.63.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 62.62.63.64.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 62.63.64.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 63.64.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 64.65.65.65.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 65.65.65.66.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 65.65.66.67.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 65.66.67.68.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 66.67.68.69.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 67.68.69.70.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 68.69.70.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 69.70.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 70.72.72.72.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 72.72.72.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 72.72.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 72.73.73.73.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 73.73.73.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 73.73.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 73.74.74.74.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 74.74.74.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 74.74.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 74.75.75.75.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 75.75.75.76.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 75.75.76.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 75.76.77.77.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 76.77.77.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 77.77.78.78.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 77.78.78.79.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 78.78.79.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 78.79.80.80.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 79.80.80.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 80.80.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 80.81.81.81.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 81.81.81.82.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 81.81.82.83.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 81.82.83.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 82.83.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 83.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 84.84.84.84.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 84.84.84.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 84.84.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 84.85.85.85.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 85.85.85.86.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 85.85.86.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 85.86.87.87.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 86.87.87.88.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 87.87.88.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 87.88.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 88.89.89.89.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 89.89.89.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 89.89.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 89.90.90.90.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 90.90.90.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 90.90.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 90.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 91.91.91.91.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 91.91.91.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 91.91.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 91.93.93.93.95.95.95.96.96.96.97.97.98.98.99.99 + 93.93.93.94.95.95.96.96.96.97.97.98.98.99.99 + 93.93.94.95.95.96.96.96.97.97.98.98.99.99 + 93.94.95.95.96.96.96.97.97.98.98.99.99 + 94.95.95.95.96.96.97.97.98.98.99.99 95.95.95.96.96.97.97.98.98.99.99 + 95.95.96.96.97.97.98.98.99.99 95.96.96.96.97.98.98.99.99 + 96.96.96.97.98.98.99.99 96.96.97.97.98.99.99 96.97.97.98.99.99 + 97.97.98.98.99 97.98.98.99} + +do_execsql_test 1.18.14.4 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2 +} {20.30.30.30.40.50.60.70.80.80.90.90.90 + 10.30.30.30.40.50.60.70.80.80.90.90.90 + 10.20.30.30.40.50.60.70.80.80.90.90.90 + 10.20.30.30.40.50.60.70.80.80.90.90.90 + 10.20.30.30.40.50.60.70.80.80.90.90.90 + 20.30.30.30.50.60.70.80.80.90.90.90 30.30.30.40.60.70.80.80.90.90.90 + 30.30.40.50.70.80.80.90.90.90 30.40.50.60.80.80.90.90.90 + 40.50.60.70.80.90.90.90 50.60.70.80.90.90.90 60.70.80.80.90.90 + 70.80.80.90.90 80.80.90.90 + 1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.11.11.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 11.11.21.21.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 11.21.21.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 21.21.31.31.41.51.61.61.81.81.81.91.91.91.91.91 + 21.31.31.41.51.61.61.81.81.81.91.91.91.91.91 + 31.31.41.41.61.61.81.81.81.91.91.91.91.91 + 31.41.41.51.61.81.81.81.91.91.91.91.91 + 41.41.51.61.81.81.81.91.91.91.91.91 41.51.61.61.81.81.91.91.91.91.91 + 51.61.61.81.81.91.91.91.91.91 61.61.81.81.91.91.91.91.91 + 61.81.81.81.91.91.91.91 81.81.81.91.91.91.91 81.81.91.91.91.91 + 81.91.91.91.91 91.91.91.91 2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.2.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.2.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.2.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.12.12.12.22.32.42.52.62.62.72.72.72.82 + 12.12.12.22.32.42.52.62.62.72.72.72.82 + 12.12.22.22.42.52.62.62.72.72.72.82 12.22.22.32.52.62.62.72.72.72.82 + 22.22.32.42.62.62.72.72.72.82 22.32.42.52.62.72.72.72.82 + 32.42.52.62.72.72.72.82 42.52.62.62.72.72.82 52.62.62.72.72.82 + 62.62.72.72.82 62.72.72.72 + 3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.13.13.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 13.13.23.23.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 13.23.23.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 23.23.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 23.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 33.33.33.33.43.53.63.73.73.73.83.93.93.93 + 33.33.33.43.53.63.73.73.73.83.93.93.93 + 33.33.43.43.63.73.73.73.83.93.93.93 33.43.43.53.73.73.73.83.93.93.93 + 43.43.53.63.73.73.83.93.93.93 43.53.63.73.73.83.93.93.93 + 53.63.73.73.83.93.93.93 63.73.73.73.93.93.93 73.73.73.83.93.93 + 73.73.83.93.93 73.83.93.93 + 14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.24.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.24.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 14.24.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 24.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 34.34.34.34.44.54.64.74.74.74.84.84.84.84.94 + 34.34.34.44.54.64.74.74.74.84.84.84.84.94 + 34.34.44.44.64.74.74.74.84.84.84.84.94 + 34.44.44.54.74.74.74.84.84.84.84.94 44.44.54.64.74.74.84.84.84.84.94 + 44.54.64.74.74.84.84.84.84.94 54.64.74.74.84.84.84.84.94 + 64.74.74.74.84.84.84.94 74.74.74.84.84.84.94 74.74.84.84.84.94 + 74.84.84.84.94 84.84.84.84 + 15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 15.15.15.25.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 15.15.25.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 15.25.35.35.55.65.65.65.75.75.75.85.85.85.95.95.95 + 25.35.35.55.65.65.65.75.75.75.85.85.85.95.95.95 + 35.35.55.55.65.65.75.75.75.85.85.85.95.95.95 + 35.55.55.65.65.75.75.75.85.85.85.95.95.95 + 55.55.65.65.75.75.75.85.85.85.95.95.95 + 55.65.65.65.75.75.85.85.85.95.95.95 65.65.65.75.75.85.85.85.95.95.95 + 65.65.75.75.85.85.85.95.95.95 65.75.75.75.85.85.95.95.95 + 75.75.75.85.85.95.95.95 75.75.85.85.95.95.95 75.85.85.85.95.95 + 85.85.85.95.95 85.85.95.95 + 16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 16.16.16.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 16.16.26.26.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 16.26.26.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 26.26.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 26.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 36.36.36.36.46.56.56.56.66.76.86.96.96.96 + 36.36.36.46.56.56.56.66.76.86.96.96.96 + 36.36.46.46.56.56.66.76.86.96.96.96 36.46.46.56.56.66.76.86.96.96.96 + 46.46.56.56.66.76.86.96.96.96 46.56.56.56.76.86.96.96.96 + 56.56.56.66.86.96.96.96 56.56.66.76.96.96.96 56.66.76.86.96.96 + 66.76.86.96.96 76.86.96.96 + 7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.17.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.17.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.17.27.27.37.47.47.47.47.57.67.77.77.87.87.97.97 + 17.27.27.37.47.47.47.47.57.67.77.77.87.87.97.97 + 27.27.37.37.47.47.47.57.67.77.77.87.87.97.97 + 27.37.37.47.47.47.57.67.77.77.87.87.97.97 + 37.37.47.47.47.57.67.77.77.87.87.97.97 + 37.47.47.47.57.67.77.77.87.87.97.97 47.47.47.47.67.77.77.87.87.97.97 + 47.47.47.57.77.77.87.87.97.97 47.47.57.67.77.87.87.97.97 + 47.57.67.77.87.87.97.97 57.67.77.77.87.97.97 67.77.77.87.97.97 + 77.77.87.87.97 77.87.87.97 8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.8.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.8.28.38.58.58.58.58.68.78.78.88.98.98 + 8.8.28.38.58.58.58.58.68.78.78.88.98.98 + 8.28.38.38.58.58.58.68.78.78.88.98.98 28.38.38.58.58.58.68.78.78.88.98.98 + 38.38.58.58.58.68.78.78.88.98.98 38.58.58.58.68.78.78.88.98.98 + 58.58.58.58.78.78.88.98.98 58.58.58.68.78.88.98.98 58.58.68.78.88.98.98 + 58.68.78.78.98.98 68.78.78.88.98 78.78.88.98 + 9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.19.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.19.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 19.29.29.29.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 29.29.29.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 29.29.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 29.39.39.39.59.59.59.59.69.79.89.89.89.99.99 + 39.39.39.49.59.59.59.69.79.89.89.89.99.99 + 39.39.49.59.59.59.69.79.89.89.89.99.99 + 39.49.59.59.59.69.79.89.89.89.99.99 49.59.59.59.69.79.89.89.89.99.99 + 59.59.59.59.79.89.89.89.99.99 59.59.59.69.89.89.89.99.99 + 59.59.69.79.89.89.99.99 59.69.79.89.89.99.99 69.79.89.89.99.99 + 79.89.89.89.99 89.89.89.99} + +do_execsql_test 1.18.14.5 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( ORDER BY b%10,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) FROM t2 +} {40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 40.30.80.20.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 30.80.20.90.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 80.20.90.60.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 20.90.60.70.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.60.70.80.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 60.70.80.90.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 70.80.90.30.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 80.90.30.50.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.30.50.10.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 30.50.10.30.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 50.10.30.81.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 10.30.81.91.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 30.81.91.61.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 81.91.61.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 91.61.91.91.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 61.91.91.1.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 91.91.1.81.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 91.1.81.41.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 1.81.41.61.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 81.41.61.1.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 41.61.1.21.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 61.1.21.11.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 1.21.11.51.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 21.11.51.41.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 11.51.41.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 51.41.31.31.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 41.31.31.11.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 31.31.11.81.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 31.11.81.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 11.81.91.91.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 81.91.91.21.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 91.91.21.62.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 91.21.62.12.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 21.62.12.32.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 62.12.32.22.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 12.32.22.42.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 32.22.42.2.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 22.42.2.72.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 42.2.72.12.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 2.72.12.22.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 72.12.22.2.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 12.22.2.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 22.2.72.72.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 2.72.72.12.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 72.72.12.62.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 72.12.62.52.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 12.62.52.82.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 62.52.82.93.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 52.82.93.23.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 82.93.23.93.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 93.23.93.43.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 23.93.43.3.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 93.43.3.43.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 43.3.43.33.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 3.43.33.53.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 43.33.53.63.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 33.53.63.73.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 53.63.73.13.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 63.73.13.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 73.13.73.73.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 13.73.73.33.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 73.73.33.93.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 73.33.93.23.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 33.93.23.13.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 93.23.13.33.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 23.13.33.3.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 13.33.3.33.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 33.3.33.83.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 3.33.83.54.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 33.83.54.84.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 83.54.84.74.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 54.84.74.24.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 84.74.24.4.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 74.24.4.94.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 24.4.94.84.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 4.94.84.74.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 94.84.74.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 84.74.34.34.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 74.34.34.44.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 34.34.44.74.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 34.44.74.64.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 44.74.64.14.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 74.64.14.34.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 64.14.34.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 14.34.84.84.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 34.84.84.44.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 84.84.44.34.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 84.44.34.65.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 44.34.65.35.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 34.65.35.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 65.35.85.85.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 35.85.85.55.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 85.85.55.15.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 85.55.15.25.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 55.15.25.75.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 15.25.75.95.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 25.75.95.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 75.95.65.65.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 95.65.65.35.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 65.65.35.5.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 65.35.5.15.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 35.5.15.95.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 5.15.95.55.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 15.95.55.75.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 95.55.75.85.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 55.75.85.75.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 75.85.75.15.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 85.75.15.95.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 75.15.95.96.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 15.95.96.46.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 95.96.46.6.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 96.46.6.46.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 46.6.46.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 6.46.16.16.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 46.16.16.86.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 16.16.86.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 16.86.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 86.56.56.56.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 56.56.56.16.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 56.56.16.36.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 56.16.36.76.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 16.36.76.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 36.76.96.96.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 76.96.96.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 96.96.26.26.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 96.26.26.36.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 26.26.36.66.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 26.36.66.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 36.66.36.36.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 66.36.36.97.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 36.36.97.27.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 36.97.27.97.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 97.27.97.67.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 27.97.67.77.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 97.67.77.47.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 67.77.47.7.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 77.47.7.47.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 47.7.47.87.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 7.47.87.37.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 47.87.37.87.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 87.37.87.77.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 37.87.77.7.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 87.77.7.57.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 77.7.57.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 7.57.47.47.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 57.47.47.37.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 47.47.37.27.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 47.37.27.17.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 37.27.17.7.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 27.17.7.38.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 17.7.38.68.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 7.38.68.78.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 38.68.78.8.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 68.78.8.28.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 78.8.28.98.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 8.28.98.78.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 28.98.78.58.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 98.78.58.98.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 78.58.98.8.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 58.98.8.88.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 98.8.88.8.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 8.88.8.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 88.8.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 8.58.58.58.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 58.58.58.38.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 58.58.38.89.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 58.38.89.59.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 38.89.59.39.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 59.39.99.29.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 39.99.29.59.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 99.29.59.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 29.59.89.89.9.79.49.59.29.59.19.39.9.9.99.69.39 + 59.89.89.29.79.49.59.29.59.19.39.9.9.99.69.39 + 89.89.29.9.49.59.29.59.19.39.9.9.99.69.39 + 89.29.9.79.59.29.59.19.39.9.9.99.69.39 + 29.9.79.49.29.59.19.39.9.9.99.69.39 9.79.49.59.59.19.39.9.9.99.69.39 + 79.49.59.29.19.39.9.9.99.69.39 49.59.29.59.39.9.9.99.69.39 + 59.29.59.19.9.9.99.69.39 29.59.19.39.9.99.69.39 59.19.39.9.99.69.39 + 19.39.9.9.69.39 39.9.9.99.39 9.9.99.69} + +do_execsql_test 1.18.14.6 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.18.14.7 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (win1 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW) + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a) + ORDER BY 1 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.18.14.8 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (win1 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW) + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a ORDER BY b%10) + ORDER BY 1 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.18.14.9 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER win2 + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a ORDER BY b%10), + win2 AS (win1 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW) + ORDER BY 1 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.18.15.1 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE a%2=0) OVER win FROM t2 + WINDOW win AS (ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW) +} {190 96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 190 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 190 89.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 190 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 190 89.96.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 189 96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 188 96.38.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 187 38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 186 38.39.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 185 39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 184 39.91.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 183 91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 182 91.6.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 181 6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 180 6.97.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 179 97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 178 97.46.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 177 46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 176 46.54.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 175 54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 174 54.8.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 173 8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 172 8.29.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 171 29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 170 29.84.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 169 84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 168 84.23.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 167 23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 166 23.16.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 165 16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 164 16.65.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 163 65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 162 65.47.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 161 47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 160 47.86.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 159 86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 158 86.61.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 157 61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 156 61.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 155 85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 154 85.85.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 153 85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 152 85.59.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 151 59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 150 59.32.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 149 32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 148 32.3.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 147 3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 146 3.22.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 145 22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 144 22.55.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 143 55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 142 55.28.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 141 28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 140 28.25.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 139 25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 138 25.1.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 137 1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 136 1.40.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 135 40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 134 40.56.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 133 56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 132 56.75.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 131 75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 130 75.89.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 129 89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 128 89.76.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 127 76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 126 76.4.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 125 4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 124 4.42.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 123 42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 122 42.78.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 121 78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 120 78.29.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 119 29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 118 29.63.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 117 63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 116 63.87.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 115 87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 114 87.80.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 113 80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 112 80.72.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 111 72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 110 72.9.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 109 9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 108 9.73.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 107 73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 106 73.65.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 105 65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 104 65.58.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 103 58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 102 58.98.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 101 98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 100 98.21.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 99 21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 98 21.65.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 97 65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 96 65.5.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 95 5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 94 5.11.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 93 11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 92 11.87.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 91 87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 90 87.12.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 89 12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 88 12.20.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 87 20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 86 20.31.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 85 31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 84 31.95.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 83 95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 82 95.73.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 81 73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 80 73.88.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 79 88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 78 88.8.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 77 8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 76 8.49.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 75 49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 74 49.90.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 73 90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 72 90.96.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 71 96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 70 96.55.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 69 55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 68 55.77.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 67 77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 66 77.2.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 65 2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 64 2.85.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 63 85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 62 85.74.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 61 74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 60 74.70.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 59 70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 58 70.19.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 57 19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 56 19.26.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 55 26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 54 26.47.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 53 47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 52 47.90.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 51 90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 50 90.58.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 49 58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 48 58.9.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 47 9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 46 9.72.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 45 72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 44 72.33.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 43 33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 42 33.75.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 41 75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 40 75.81.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 39 81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 38 81.23.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 37 23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 36 23.13.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 35 13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 34 13.14.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 33 14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 32 14.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 31 91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 30 91.91.36.3.69.52.50.10.33.39.58.38.83.82.7 + 29 91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 28 91.15.3.69.52.50.10.33.39.58.38.83.82.7 + 27 15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 26 15.36.69.52.50.10.33.39.58.38.83.82.7 + 25 36.3.69.52.50.10.33.39.58.38.83.82.7 + 24 36.3.52.50.10.33.39.58.38.83.82.7 23 3.69.52.50.10.33.39.58.38.83.82.7 + 22 3.69.50.10.33.39.58.38.83.82.7 21 69.52.50.10.33.39.58.38.83.82.7 + 20 69.52.10.33.39.58.38.83.82.7 19 52.50.10.33.39.58.38.83.82.7 + 18 52.50.33.39.58.38.83.82.7 17 50.10.33.39.58.38.83.82.7 + 16 50.10.39.58.38.83.82.7 15 10.33.39.58.38.83.82.7 + 14 10.33.58.38.83.82.7 13 33.39.58.38.83.82.7 12 33.39.38.83.82.7 + 11 39.58.38.83.82.7 10 39.58.83.82.7 9 58.38.83.82.7 8 58.38.82.7 + 7 38.83.82.7 6 38.83.7 5 83.82.7 4 83.82} + +do_execsql_test 1.18.15.2 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE 0=1) OVER win FROM t2 + WINDOW win AS (ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW) +} {190 {} 190 {} 190 {} 190 {} 190 {} 189 {} 188 {} 187 {} + 186 {} 185 {} 184 {} 183 {} 182 {} 181 {} 180 {} 179 {} + 178 {} 177 {} 176 {} 175 {} 174 {} 173 {} 172 {} 171 {} + 170 {} 169 {} 168 {} 167 {} 166 {} 165 {} 164 {} 163 {} + 162 {} 161 {} 160 {} 159 {} 158 {} 157 {} 156 {} 155 {} + 154 {} 153 {} 152 {} 151 {} 150 {} 149 {} 148 {} 147 {} + 146 {} 145 {} 144 {} 143 {} 142 {} 141 {} 140 {} 139 {} + 138 {} 137 {} 136 {} 135 {} 134 {} 133 {} 132 {} 131 {} + 130 {} 129 {} 128 {} 127 {} 126 {} 125 {} 124 {} 123 {} + 122 {} 121 {} 120 {} 119 {} 118 {} 117 {} 116 {} 115 {} + 114 {} 113 {} 112 {} 111 {} 110 {} 109 {} 108 {} 107 {} + 106 {} 105 {} 104 {} 103 {} 102 {} 101 {} 100 {} 99 {} + 98 {} 97 {} 96 {} 95 {} 94 {} 93 {} 92 {} 91 {} 90 {} + 89 {} 88 {} 87 {} 86 {} 85 {} 84 {} 83 {} 82 {} 81 {} + 80 {} 79 {} 78 {} 77 {} 76 {} 75 {} 74 {} 73 {} 72 {} + 71 {} 70 {} 69 {} 68 {} 67 {} 66 {} 65 {} 64 {} 63 {} + 62 {} 61 {} 60 {} 59 {} 58 {} 57 {} 56 {} 55 {} 54 {} + 53 {} 52 {} 51 {} 50 {} 49 {} 48 {} 47 {} 46 {} 45 {} + 44 {} 43 {} 42 {} 41 {} 40 {} 39 {} 38 {} 37 {} 36 {} + 35 {} 34 {} 33 {} 32 {} 31 {} 30 {} 29 {} 28 {} 27 {} + 26 {} 25 {} 24 {} 23 {} 22 {} 21 {} 20 {} 19 {} 18 {} + 17 {} 16 {} 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} + 7 {} 6 {} 5 {} 4 {}} + +do_execsql_test 1.18.15.3 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE 1=0) OVER win FROM t2 + WINDOW win AS (PARTITION BY (a%10) ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW) +} {19 {} 19 {} 19 {} 19 {} 19 {} 18 {} 17 {} 16 {} 15 {} + 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} 5 {} + 4 {} 18 {} 18 {} 18 {} 18 {} 18 {} 17 {} 16 {} 15 {} + 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} 5 {} + 4 {} 18 {} 18 {} 18 {} 18 {} 18 {} 17 {} 16 {} 15 {} + 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} 5 {} + 4 {} 18 {} 18 {} 18 {} 18 {} 18 {} 17 {} 16 {} 15 {} + 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} 5 {} + 4 {} 18 {} 18 {} 18 {} 18 {} 18 {} 17 {} 16 {} 15 {} + 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} 5 {} + 4 {} 18 {} 18 {} 18 {} 18 {} 18 {} 17 {} 16 {} 15 {} + 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} 5 {} + 4 {} 18 {} 18 {} 18 {} 18 {} 18 {} 17 {} 16 {} 15 {} + 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} 5 {} + 4 {} 18 {} 18 {} 18 {} 18 {} 18 {} 17 {} 16 {} 15 {} + 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} 5 {} + 4 {} 18 {} 18 {} 18 {} 18 {} 18 {} 17 {} 16 {} 15 {} + 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} 5 {} + 4 {} 18 {} 18 {} 18 {} 18 {} 18 {} 17 {} 16 {} 15 {} + 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} 5 {} + 4 {}} + +do_execsql_test 1.18.15.4 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE a%2=0) OVER win FROM t2 + WINDOW win AS (PARTITION BY (a%10) ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW) +} {19 6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 19 89.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 19 89.6.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 19 89.6.29.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 19 89.6.29.47.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 18 6.29.47.59.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 17 29.47.59.28.78.72.98.87.73.96.74.90.75.91.69.39.7 + 16 47.59.28.75.72.98.87.73.96.74.90.75.91.69.39.7 + 15 59.28.75.78.98.87.73.96.74.90.75.91.69.39.7 + 14 28.75.78.72.87.73.96.74.90.75.91.69.39.7 + 13 75.78.72.98.73.96.74.90.75.91.69.39.7 + 12 78.72.98.87.96.74.90.75.91.69.39.7 11 72.98.87.73.74.90.75.91.69.39.7 + 10 98.87.73.96.90.75.91.69.39.7 9 87.73.96.74.75.91.69.39.7 + 8 73.96.74.90.91.69.39.7 7 96.74.90.75.69.39.7 6 74.90.75.91.39.7 + 5 90.75.91.69.7 4 75.91.69.39 18 {} 18 {} 18 {} 18 {} 18 {} + 17 {} 16 {} 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} + 7 {} 6 {} 5 {} 4 {} + 18 97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 18 96.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 18 96.97.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 18 96.97.84.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 18 96.97.84.86.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 17 97.84.86.32.89.29.9.21.12.88.55.70.58.81.91.52.58 + 16 84.86.32.25.29.9.21.12.88.55.70.58.81.91.52.58 + 15 86.32.25.89.9.21.12.88.55.70.58.81.91.52.58 + 14 32.25.89.29.21.12.88.55.70.58.81.91.52.58 + 13 25.89.29.9.12.88.55.70.58.81.91.52.58 + 12 89.29.9.21.88.55.70.58.81.91.52.58 11 29.9.21.12.55.70.58.81.91.52.58 + 10 9.21.12.88.70.58.81.91.52.58 9 21.12.88.55.58.81.91.52.58 + 8 12.88.55.70.81.91.52.58 7 88.55.70.58.91.52.58 6 55.70.58.81.52.58 + 5 70.58.81.91.58 4 58.81.91.52 18 {} 18 {} 18 {} 18 {} 18 {} + 17 {} 16 {} 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} + 7 {} 6 {} 5 {} 4 {} + 18 46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 18 38.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 18 38.46.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 18 38.46.23.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 18 38.46.23.61.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 17 46.23.61.3.76.63.73.65.20.8.77.19.9.23.15.50.38 + 16 23.61.3.1.63.73.65.20.8.77.19.9.23.15.50.38 + 15 61.3.1.76.73.65.20.8.77.19.9.23.15.50.38 + 14 3.1.76.63.65.20.8.77.19.9.23.15.50.38 + 13 1.76.63.73.20.8.77.19.9.23.15.50.38 + 12 76.63.73.65.8.77.19.9.23.15.50.38 11 63.73.65.20.77.19.9.23.15.50.38 + 10 73.65.20.8.19.9.23.15.50.38 9 65.20.8.77.9.23.15.50.38 + 8 20.8.77.19.23.15.50.38 7 8.77.19.9.15.50.38 6 77.19.9.23.50.38 + 5 19.9.23.15.38 4 9.23.15.50 18 {} 18 {} 18 {} 18 {} 18 {} + 17 {} 16 {} 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} + 7 {} 6 {} 5 {} 4 {} + 18 54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 18 39.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 18 39.54.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 18 39.54.16.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 18 39.54.16.85.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 17 54.16.85.22.4.87.65.5.31.49.2.26.72.13.36.10.83 + 16 16.85.22.40.87.65.5.31.49.2.26.72.13.36.10.83 + 15 85.22.40.4.65.5.31.49.2.26.72.13.36.10.83 + 14 22.40.4.87.5.31.49.2.26.72.13.36.10.83 + 13 40.4.87.65.31.49.2.26.72.13.36.10.83 + 12 4.87.65.5.49.2.26.72.13.36.10.83 11 87.65.5.31.2.26.72.13.36.10.83 + 10 65.5.31.49.26.72.13.36.10.83 9 5.31.49.2.72.13.36.10.83 + 8 31.49.2.26.13.36.10.83 7 49.2.26.72.36.10.83 6 2.26.72.13.10.83 + 5 26.72.13.36.83 4 72.13.36.10 18 {} 18 {} 18 {} 18 {} 18 {} + 17 {} 16 {} 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} + 7 {} 6 {} 5 {} 4 {} + 18 8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 18 91.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 18 91.8.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 18 91.8.65.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 18 91.8.65.85.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 17 8.65.85.55.42.80.58.11.95.90.85.47.33.14.3.33.82 + 16 65.85.55.56.80.58.11.95.90.85.47.33.14.3.33.82 + 15 85.55.56.42.58.11.95.90.85.47.33.14.3.33.82 + 14 55.56.42.80.11.95.90.85.47.33.14.3.33.82 + 13 56.42.80.58.95.90.85.47.33.14.3.33.82 + 12 42.80.58.11.90.85.47.33.14.3.33.82 11 80.58.11.95.85.47.33.14.3.33.82 + 10 58.11.95.90.47.33.14.3.33.82 9 11.95.90.85.33.14.3.33.82 + 8 95.90.85.47.14.3.33.82 7 90.85.47.33.3.33.82 6 85.47.33.14.33.82 + 5 47.33.14.3.82 4 33.14.3.33 18 {} 18 {} 18 {} 18 {} 18 {} + 17 {} 16 {} 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} + 7 {} 6 {} 5 {} 4 {}} + +do_execsql_test 1.19.2.1 { + SELECT max(b) OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2 +} {99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 95 95 84 84 84 84 84 84 + 84 84 83 83 83 83 83 83 83 83 83} + +do_execsql_test 1.19.2.2 { + SELECT min(b) OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 + 3 3 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 + 7} + +do_execsql_test 1.19.3.1 { + SELECT row_number() OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.19.3.2 { + SELECT row_number() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.19.3.3 { + SELECT row_number() OVER ( ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.19.4.1 { + SELECT dense_rank() OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.19.4.2 { + SELECT dense_rank() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.19.4.3 { + SELECT dense_rank() OVER ( ORDER BY b ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2 +} {1 1 2 2 3 3 4 5 6 7 7 7 8 8 8 9 9 9 10 + 11 11 12 12 12 13 13 14 15 15 15 16 16 16 17 + 18 19 20 20 21 21 22 22 23 24 25 25 26 26 27 + 28 28 28 29 29 29 30 30 31 32 32 32 32 33 33 + 33 33 34 34 35 35 35 35 36 36 37 37 38 38 38 + 39 40 40 41 42 42 43 43 44 44 45 45 45 45 46 + 47 48 49 50 51 52 52 53 53 53 54 55 55 55 55 + 56 56 56 56 57 58 58 59 59 60 61 62 62 62 63 + 64 65 66 67 68 68 68 69 69 69 70 70 70 71 71 + 71 72 73 73 74 74 75 76 76 77 77 77 78 79 80 + 80 80 80 81 81 81 82 83 83 84 85 85 85 86 86 + 86 87 87 87 87 87 88 88 88 89 90 90 90 91 91 + 91 92 92 93 93 94 94} + +do_execsql_test 1.19.4.4 { + SELECT dense_rank() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2 +} {1 2 3 3 3 4 5 6 7 8 8 9 9 9 1 1 2 2 3 + 3 4 4 5 5 6 7 7 8 8 8 9 9 9 9 9 1 1 2 + 2 2 3 3 4 5 6 7 7 8 8 8 9 1 1 2 2 3 3 + 4 4 4 4 5 5 6 7 8 8 8 9 10 10 10 1 2 3 + 4 4 4 4 5 5 6 7 8 8 8 9 9 9 9 10 1 2 2 + 2 3 4 4 5 5 6 6 6 7 7 7 8 8 8 9 9 9 1 + 2 2 2 3 3 4 4 4 4 5 5 6 6 6 7 8 9 10 10 + 10 1 1 1 2 3 3 4 4 5 5 5 5 6 7 8 8 9 9 + 10 10 1 1 1 2 3 3 4 4 4 4 5 6 6 7 8 8 1 + 1 1 2 3 3 3 4 4 4 5 6 6 6 6 7 8 9 9 9 + 10 10} + +do_execsql_test 1.19.4.5 { + SELECT dense_rank() OVER ( ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 + 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 + 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 + 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 + 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 + 8 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 10 10 + 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 + 10 10 10 10 10} + +do_execsql_test 1.19.4.6 { + SELECT dense_rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 + 5 5 5 5 5 5 5 5 5 5 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 + 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 + 5} + +do_execsql_test 1.19.5.1 { + SELECT rank() OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.19.5.2 { + SELECT rank() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.19.5.3 { + SELECT rank() OVER ( ORDER BY b ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2 +} {1 1 3 3 5 5 7 8 9 10 10 10 13 13 13 16 16 + 16 19 20 20 22 22 22 25 25 27 28 28 28 31 31 + 31 34 35 36 37 37 39 39 41 41 43 44 45 45 47 + 47 49 50 50 50 53 53 53 56 56 58 59 59 59 59 + 63 63 63 63 67 67 69 69 69 69 73 73 75 75 77 + 77 77 80 81 81 83 84 84 86 86 88 88 90 90 90 + 90 94 95 96 97 98 99 100 100 102 102 102 105 106 + 106 106 106 110 110 110 110 114 115 115 117 117 119 + 120 121 121 121 124 125 126 127 128 129 129 129 132 + 132 132 135 135 135 138 138 138 141 142 142 144 144 + 146 147 147 149 149 149 152 153 154 154 154 154 158 + 158 158 161 162 162 164 165 165 165 168 168 168 171 + 171 171 171 171 176 176 176 179 180 180 180 183 183 + 183 186 186 188 188 190 190} + +do_execsql_test 1.19.5.4 { + SELECT rank() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2 +} {1 2 3 3 3 6 7 8 9 10 10 12 12 12 1 1 3 3 + 5 5 7 7 9 9 11 12 12 14 14 14 17 17 17 17 + 17 1 1 3 3 3 6 6 8 9 10 11 11 13 13 13 16 + 1 1 3 3 5 5 7 7 7 7 11 11 13 14 15 15 15 + 18 19 19 19 1 2 3 4 4 4 4 8 8 10 11 12 12 + 12 15 15 15 15 19 1 2 2 2 5 6 6 8 8 10 10 + 10 13 13 13 16 16 16 19 19 19 1 2 2 2 5 5 7 + 7 7 7 11 11 13 13 13 16 17 18 19 19 19 1 1 + 1 4 5 5 7 7 9 9 9 9 13 14 15 15 17 17 19 + 19 1 1 1 4 5 5 7 7 7 7 11 12 12 14 15 15 + 1 1 1 4 5 5 5 8 8 8 11 12 12 12 12 16 17 + 18 18 18 21 21} + +do_execsql_test 1.19.5.5 { + SELECT rank() OVER ( ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 15 15 15 15 + 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 + 15 15 36 36 36 36 36 36 36 36 36 36 36 36 36 + 36 36 36 52 52 52 52 52 52 52 52 52 52 52 52 + 52 52 52 52 52 52 52 52 52 73 73 73 73 73 73 + 73 73 73 73 73 73 73 73 73 73 73 73 73 92 92 + 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 + 92 92 92 92 113 113 113 113 113 113 113 113 113 + 113 113 113 113 113 113 113 113 113 113 113 113 134 + 134 134 134 134 134 134 134 134 134 134 134 134 134 + 134 134 134 134 134 134 154 154 154 154 154 154 154 + 154 154 154 154 154 154 154 154 154 170 170 170 170 + 170 170 170 170 170 170 170 170 170 170 170 170 170 + 170 170 170 170 170} + +do_execsql_test 1.19.5.6 { + SELECT rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 15 15 15 15 + 15 15 15 15 15 15 15 15 15 15 15 15 31 31 31 + 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 + 31 50 50 50 50 50 50 50 50 50 50 50 50 50 50 + 50 50 50 50 50 50 50 71 71 71 71 71 71 71 71 + 71 71 71 71 71 71 71 71 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 22 22 22 22 22 22 + 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 + 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 + 43 43 43 43 43 43 64 64 64 64 64 64 64 64 64 + 64 64 64 64 64 64 64 64 64 64 64 84 84 84 84 + 84 84 84 84 84 84 84 84 84 84 84 84 84 84 84 + 84 84 84} + +do_execsql_test 1.19.6.1 { + SELECT + row_number() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ), + rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ), + dense_rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) + FROM t2 +} {1 1 1 2 1 1 3 1 1 4 1 1 5 1 1 6 1 1 7 1 1 8 1 1 9 1 1 + 10 1 1 11 1 1 12 1 1 13 1 1 14 1 1 15 15 2 16 15 2 17 15 2 + 18 15 2 19 15 2 20 15 2 21 15 2 22 15 2 23 15 2 24 15 2 + 25 15 2 26 15 2 27 15 2 28 15 2 29 15 2 30 15 2 31 31 3 + 32 31 3 33 31 3 34 31 3 35 31 3 36 31 3 37 31 3 38 31 3 + 39 31 3 40 31 3 41 31 3 42 31 3 43 31 3 44 31 3 45 31 3 + 46 31 3 47 31 3 48 31 3 49 31 3 50 50 4 51 50 4 52 50 4 + 53 50 4 54 50 4 55 50 4 56 50 4 57 50 4 58 50 4 59 50 4 + 60 50 4 61 50 4 62 50 4 63 50 4 64 50 4 65 50 4 66 50 4 + 67 50 4 68 50 4 69 50 4 70 50 4 71 71 5 72 71 5 73 71 5 + 74 71 5 75 71 5 76 71 5 77 71 5 78 71 5 79 71 5 80 71 5 + 81 71 5 82 71 5 83 71 5 84 71 5 85 71 5 86 71 5 1 1 1 2 1 1 + 3 1 1 4 1 1 5 1 1 6 1 1 7 1 1 8 1 1 9 1 1 10 1 1 11 1 1 + 12 1 1 13 1 1 14 1 1 15 1 1 16 1 1 17 1 1 18 1 1 19 1 1 + 20 1 1 21 1 1 22 22 2 23 22 2 24 22 2 25 22 2 26 22 2 27 22 2 + 28 22 2 29 22 2 30 22 2 31 22 2 32 22 2 33 22 2 34 22 2 + 35 22 2 36 22 2 37 22 2 38 22 2 39 22 2 40 22 2 41 22 2 + 42 22 2 43 43 3 44 43 3 45 43 3 46 43 3 47 43 3 48 43 3 + 49 43 3 50 43 3 51 43 3 52 43 3 53 43 3 54 43 3 55 43 3 + 56 43 3 57 43 3 58 43 3 59 43 3 60 43 3 61 43 3 62 43 3 + 63 43 3 64 64 4 65 64 4 66 64 4 67 64 4 68 64 4 69 64 4 + 70 64 4 71 64 4 72 64 4 73 64 4 74 64 4 75 64 4 76 64 4 + 77 64 4 78 64 4 79 64 4 80 64 4 81 64 4 82 64 4 83 64 4 + 84 84 5 85 84 5 86 84 5 87 84 5 88 84 5 89 84 5 90 84 5 + 91 84 5 92 84 5 93 84 5 94 84 5 95 84 5 96 84 5 97 84 5 + 98 84 5 99 84 5 100 84 5 101 84 5 102 84 5 103 84 5 104 84 5 + 105 84 5} + + +do_test 1.19.7.1 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0053 0.0105 0.0158 0.0211 0.0263 0.0316 0.0368 0.0421 0.0474 0.0526 0.0579 0.0632 0.0684 0.0737 0.0789 0.0842 0.0895 0.0947 0.1000 0.1053 0.1105 0.1158 0.1211 0.1263 0.1316 0.1368 0.1421 0.1474 0.1526 0.1579 0.1632 0.1684 0.1737 0.1789 0.1842 0.1895 0.1947 0.2000 0.2053 0.2105 0.2158 0.2211 0.2263 0.2316 0.2368 0.2421 0.2474 0.2526 0.2579 0.2632 0.2684 0.2737 0.2789 0.2842 0.2895 0.2947 0.3000 0.3053 0.3105 0.3158 0.3211 0.3263 0.3316 0.3368 0.3421 0.3474 0.3526 0.3579 0.3632 0.3684 0.3737 0.3789 0.3842 0.3895 0.3947 0.4000 0.4053 0.4105 0.4158 0.4211 0.4263 0.4316 0.4368 0.4421 0.4474 0.4526 0.4579 0.4632 0.4684 0.4737 0.4789 0.4842 0.4895 0.4947 0.5000 0.5053 0.5105 0.5158 0.5211 0.5263 0.5316 0.5368 0.5421 0.5474 0.5526 0.5579 0.5632 0.5684 0.5737 0.5789 0.5842 0.5895 0.5947 0.6000 0.6053 0.6105 0.6158 0.6211 0.6263 0.6316 0.6368 0.6421 0.6474 0.6526 0.6579 0.6632 0.6684 0.6737 0.6789 0.6842 0.6895 0.6947 0.7000 0.7053 0.7105 0.7158 0.7211 0.7263 0.7316 0.7368 0.7421 0.7474 0.7526 0.7579 0.7632 0.7684 0.7737 0.7789 0.7842 0.7895 0.7947 0.8000 0.8053 0.8105 0.8158 0.8211 0.8263 0.8316 0.8368 0.8421 0.8474 0.8526 0.8579 0.8632 0.8684 0.8737 0.8789 0.8842 0.8895 0.8947 0.9000 0.9053 0.9105 0.9158 0.9211 0.9263 0.9316 0.9368 0.9421 0.9474 0.9526 0.9579 0.9632 0.9684 0.9737 0.9789 0.9842 0.9895 0.9947 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.19.7.2 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0769 0.1538 0.2308 0.3077 0.3846 0.4615 0.5385 0.6154 0.6923 0.7692 0.8462 0.9231 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0667 0.1333 0.2000 0.2667 0.3333 0.4000 0.4667 0.5333 0.6000 0.6667 0.7333 0.8000 0.8667 0.9333 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0556 0.1111 0.1667 0.2222 0.2778 0.3333 0.3889 0.4444 0.5000 0.5556 0.6111 0.6667 0.7222 0.7778 0.8333 0.8889 0.9444 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0526 0.1053 0.1579 0.2105 0.2632 0.3158 0.3684 0.4211 0.4737 0.5263 0.5789 0.6316 0.6842 0.7368 0.7895 0.8421 0.8947 0.9474 1.0000 0.0000 0.0667 0.1333 0.2000 0.2667 0.3333 0.4000 0.4667 0.5333 0.6000 0.6667 0.7333 0.8000 0.8667 0.9333 1.0000 0.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.19.7.3 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY b ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0105 0.0105 0.0211 0.0211 0.0316 0.0368 0.0421 0.0474 0.0474 0.0474 0.0632 0.0632 0.0632 0.0789 0.0789 0.0789 0.0947 0.1000 0.1000 0.1105 0.1105 0.1105 0.1263 0.1263 0.1368 0.1421 0.1421 0.1421 0.1579 0.1579 0.1579 0.1737 0.1789 0.1842 0.1895 0.1895 0.2000 0.2000 0.2105 0.2105 0.2211 0.2263 0.2316 0.2316 0.2421 0.2421 0.2526 0.2579 0.2579 0.2579 0.2737 0.2737 0.2737 0.2895 0.2895 0.3000 0.3053 0.3053 0.3053 0.3053 0.3263 0.3263 0.3263 0.3263 0.3474 0.3474 0.3579 0.3579 0.3579 0.3579 0.3789 0.3789 0.3895 0.3895 0.4000 0.4000 0.4000 0.4158 0.4211 0.4211 0.4316 0.4368 0.4368 0.4474 0.4474 0.4579 0.4579 0.4684 0.4684 0.4684 0.4684 0.4895 0.4947 0.5000 0.5053 0.5105 0.5158 0.5211 0.5211 0.5316 0.5316 0.5316 0.5474 0.5526 0.5526 0.5526 0.5526 0.5737 0.5737 0.5737 0.5737 0.5947 0.6000 0.6000 0.6105 0.6105 0.6211 0.6263 0.6316 0.6316 0.6316 0.6474 0.6526 0.6579 0.6632 0.6684 0.6737 0.6737 0.6737 0.6895 0.6895 0.6895 0.7053 0.7053 0.7053 0.7211 0.7211 0.7211 0.7368 0.7421 0.7421 0.7526 0.7526 0.7632 0.7684 0.7684 0.7789 0.7789 0.7789 0.7947 0.8000 0.8053 0.8053 0.8053 0.8053 0.8263 0.8263 0.8263 0.8421 0.8474 0.8474 0.8579 0.8632 0.8632 0.8632 0.8789 0.8789 0.8789 0.8947 0.8947 0.8947 0.8947 0.8947 0.9211 0.9211 0.9211 0.9368 0.9421 0.9421 0.9421 0.9579 0.9579 0.9579 0.9737 0.9737 0.9842 0.9842 0.9947 0.9947} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.19.7.4 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0769 0.1538 0.1538 0.1538 0.3846 0.4615 0.5385 0.6154 0.6923 0.6923 0.8462 0.8462 0.8462 0.0000 0.0000 0.1000 0.1000 0.2000 0.2000 0.3000 0.3000 0.4000 0.4000 0.5000 0.5500 0.5500 0.6500 0.6500 0.6500 0.8000 0.8000 0.8000 0.8000 0.8000 0.0000 0.0000 0.1333 0.1333 0.1333 0.3333 0.3333 0.4667 0.5333 0.6000 0.6667 0.6667 0.8000 0.8000 0.8000 1.0000 0.0000 0.0000 0.1000 0.1000 0.2000 0.2000 0.3000 0.3000 0.3000 0.3000 0.5000 0.5000 0.6000 0.6500 0.7000 0.7000 0.7000 0.8500 0.9000 0.9000 0.9000 0.0000 0.0556 0.1111 0.1667 0.1667 0.1667 0.1667 0.3889 0.3889 0.5000 0.5556 0.6111 0.6111 0.6111 0.7778 0.7778 0.7778 0.7778 1.0000 0.0000 0.0500 0.0500 0.0500 0.2000 0.2500 0.2500 0.3500 0.3500 0.4500 0.4500 0.4500 0.6000 0.6000 0.6000 0.7500 0.7500 0.7500 0.9000 0.9000 0.9000 0.0000 0.0500 0.0500 0.0500 0.2000 0.2000 0.3000 0.3000 0.3000 0.3000 0.5000 0.5000 0.6000 0.6000 0.6000 0.7500 0.8000 0.8500 0.9000 0.9000 0.9000 0.0000 0.0000 0.0000 0.1579 0.2105 0.2105 0.3158 0.3158 0.4211 0.4211 0.4211 0.4211 0.6316 0.6842 0.7368 0.7368 0.8421 0.8421 0.9474 0.9474 0.0000 0.0000 0.0000 0.2000 0.2667 0.2667 0.4000 0.4000 0.4000 0.4000 0.6667 0.7333 0.7333 0.8667 0.9333 0.9333 0.0000 0.0000 0.0000 0.1429 0.1905 0.1905 0.1905 0.3333 0.3333 0.3333 0.4762 0.5238 0.5238 0.5238 0.5238 0.7143 0.7619 0.8095 0.8095 0.8095 0.9524 0.9524} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.19.7.5 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.19.7.6 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER (PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.19.8.1 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0052 0.0105 0.0157 0.0209 0.0262 0.0314 0.0366 0.0419 0.0471 0.0524 0.0576 0.0628 0.0681 0.0733 0.0785 0.0838 0.0890 0.0942 0.0995 0.1047 0.1099 0.1152 0.1204 0.1257 0.1309 0.1361 0.1414 0.1466 0.1518 0.1571 0.1623 0.1675 0.1728 0.1780 0.1832 0.1885 0.1937 0.1990 0.2042 0.2094 0.2147 0.2199 0.2251 0.2304 0.2356 0.2408 0.2461 0.2513 0.2565 0.2618 0.2670 0.2723 0.2775 0.2827 0.2880 0.2932 0.2984 0.3037 0.3089 0.3141 0.3194 0.3246 0.3298 0.3351 0.3403 0.3455 0.3508 0.3560 0.3613 0.3665 0.3717 0.3770 0.3822 0.3874 0.3927 0.3979 0.4031 0.4084 0.4136 0.4188 0.4241 0.4293 0.4346 0.4398 0.4450 0.4503 0.4555 0.4607 0.4660 0.4712 0.4764 0.4817 0.4869 0.4921 0.4974 0.5026 0.5079 0.5131 0.5183 0.5236 0.5288 0.5340 0.5393 0.5445 0.5497 0.5550 0.5602 0.5654 0.5707 0.5759 0.5812 0.5864 0.5916 0.5969 0.6021 0.6073 0.6126 0.6178 0.6230 0.6283 0.6335 0.6387 0.6440 0.6492 0.6545 0.6597 0.6649 0.6702 0.6754 0.6806 0.6859 0.6911 0.6963 0.7016 0.7068 0.7120 0.7173 0.7225 0.7277 0.7330 0.7382 0.7435 0.7487 0.7539 0.7592 0.7644 0.7696 0.7749 0.7801 0.7853 0.7906 0.7958 0.8010 0.8063 0.8115 0.8168 0.8220 0.8272 0.8325 0.8377 0.8429 0.8482 0.8534 0.8586 0.8639 0.8691 0.8743 0.8796 0.8848 0.8901 0.8953 0.9005 0.9058 0.9110 0.9162 0.9215 0.9267 0.9319 0.9372 0.9424 0.9476 0.9529 0.9581 0.9634 0.9686 0.9738 0.9791 0.9843 0.9895 0.9948 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.19.8.2 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0714 0.1429 0.2143 0.2857 0.3571 0.4286 0.5000 0.5714 0.6429 0.7143 0.7857 0.8571 0.9286 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0625 0.1250 0.1875 0.2500 0.3125 0.3750 0.4375 0.5000 0.5625 0.6250 0.6875 0.7500 0.8125 0.8750 0.9375 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0526 0.1053 0.1579 0.2105 0.2632 0.3158 0.3684 0.4211 0.4737 0.5263 0.5789 0.6316 0.6842 0.7368 0.7895 0.8421 0.8947 0.9474 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0625 0.1250 0.1875 0.2500 0.3125 0.3750 0.4375 0.5000 0.5625 0.6250 0.6875 0.7500 0.8125 0.8750 0.9375 1.0000 0.0455 0.0909 0.1364 0.1818 0.2273 0.2727 0.3182 0.3636 0.4091 0.4545 0.5000 0.5455 0.5909 0.6364 0.6818 0.7273 0.7727 0.8182 0.8636 0.9091 0.9545 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.19.8.3 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY b ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0105 0.0105 0.0209 0.0209 0.0314 0.0314 0.0366 0.0419 0.0471 0.0628 0.0628 0.0628 0.0785 0.0785 0.0785 0.0942 0.0942 0.0942 0.0995 0.1099 0.1099 0.1257 0.1257 0.1257 0.1361 0.1361 0.1414 0.1571 0.1571 0.1571 0.1728 0.1728 0.1728 0.1780 0.1832 0.1885 0.1990 0.1990 0.2094 0.2094 0.2199 0.2199 0.2251 0.2304 0.2408 0.2408 0.2513 0.2513 0.2565 0.2723 0.2723 0.2723 0.2880 0.2880 0.2880 0.2984 0.2984 0.3037 0.3246 0.3246 0.3246 0.3246 0.3455 0.3455 0.3455 0.3455 0.3560 0.3560 0.3770 0.3770 0.3770 0.3770 0.3874 0.3874 0.3979 0.3979 0.4136 0.4136 0.4136 0.4188 0.4293 0.4293 0.4346 0.4450 0.4450 0.4555 0.4555 0.4660 0.4660 0.4869 0.4869 0.4869 0.4869 0.4921 0.4974 0.5026 0.5079 0.5131 0.5183 0.5288 0.5288 0.5445 0.5445 0.5445 0.5497 0.5707 0.5707 0.5707 0.5707 0.5916 0.5916 0.5916 0.5916 0.5969 0.6073 0.6073 0.6178 0.6178 0.6230 0.6283 0.6440 0.6440 0.6440 0.6492 0.6545 0.6597 0.6649 0.6702 0.6859 0.6859 0.6859 0.7016 0.7016 0.7016 0.7173 0.7173 0.7173 0.7330 0.7330 0.7330 0.7382 0.7487 0.7487 0.7592 0.7592 0.7644 0.7749 0.7749 0.7906 0.7906 0.7906 0.7958 0.8010 0.8220 0.8220 0.8220 0.8220 0.8377 0.8377 0.8377 0.8429 0.8534 0.8534 0.8586 0.8743 0.8743 0.8743 0.8901 0.8901 0.8901 0.9162 0.9162 0.9162 0.9162 0.9162 0.9319 0.9319 0.9319 0.9372 0.9529 0.9529 0.9529 0.9686 0.9686 0.9686 0.9791 0.9791 0.9895 0.9895 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.19.8.4 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0714 0.1429 0.3571 0.3571 0.3571 0.4286 0.5000 0.5714 0.6429 0.7857 0.7857 1.0000 1.0000 1.0000 0.0952 0.0952 0.1905 0.1905 0.2857 0.2857 0.3810 0.3810 0.4762 0.4762 0.5238 0.6190 0.6190 0.7619 0.7619 0.7619 1.0000 1.0000 1.0000 1.0000 1.0000 0.1250 0.1250 0.3125 0.3125 0.3125 0.4375 0.4375 0.5000 0.5625 0.6250 0.7500 0.7500 0.9375 0.9375 0.9375 1.0000 0.0952 0.0952 0.1905 0.1905 0.2857 0.2857 0.4762 0.4762 0.4762 0.4762 0.5714 0.5714 0.6190 0.6667 0.8095 0.8095 0.8095 0.8571 1.0000 1.0000 1.0000 0.0526 0.1053 0.1579 0.3684 0.3684 0.3684 0.3684 0.4737 0.4737 0.5263 0.5789 0.7368 0.7368 0.7368 0.9474 0.9474 0.9474 0.9474 1.0000 0.0476 0.1905 0.1905 0.1905 0.2381 0.3333 0.3333 0.4286 0.4286 0.5714 0.5714 0.5714 0.7143 0.7143 0.7143 0.8571 0.8571 0.8571 1.0000 1.0000 1.0000 0.0476 0.1905 0.1905 0.1905 0.2857 0.2857 0.4762 0.4762 0.4762 0.4762 0.5714 0.5714 0.7143 0.7143 0.7143 0.7619 0.8095 0.8571 1.0000 1.0000 1.0000 0.1500 0.1500 0.1500 0.2000 0.3000 0.3000 0.4000 0.4000 0.6000 0.6000 0.6000 0.6000 0.6500 0.7000 0.8000 0.8000 0.9000 0.9000 1.0000 1.0000 0.1875 0.1875 0.1875 0.2500 0.3750 0.3750 0.6250 0.6250 0.6250 0.6250 0.6875 0.8125 0.8125 0.8750 1.0000 1.0000 0.1364 0.1364 0.1364 0.1818 0.3182 0.3182 0.3182 0.4545 0.4545 0.4545 0.5000 0.6818 0.6818 0.6818 0.6818 0.7273 0.7727 0.9091 0.9091 0.9091 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.19.8.5 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.19.8.6 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.19.8.1 { + set myres {} + foreach r [db eval {SELECT ntile(100) OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 88.0000 89.0000 89.0000 90.0000 90.0000 91.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.19.8.2 { + set myres {} + foreach r [db eval {SELECT ntile(101) OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 22.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.19.8.3 { + set myres {} + foreach r [db eval {SELECT ntile(102) OVER ( ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 88.0000 89.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.19.8.4 { + set myres {} + foreach r [db eval {SELECT ntile(103) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 22.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.19.8.5 { + set myres {} + foreach r [db eval {SELECT ntile(104) OVER ( ORDER BY b%10,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000 103.0000 104.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.19.8.6 { + set myres {} + foreach r [db eval {SELECT ntile(105) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.19.8.7 { + set myres {} + foreach r [db eval {SELECT ntile(105) OVER ( ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 88.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000 103.0000 104.0000 105.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + +do_execsql_test 1.19.9.1 { + SELECT last_value(a+b) OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2 +} {207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207} + +do_execsql_test 1.19.9.2 { + SELECT last_value(a+b) OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2 +} {223 223 223 223 223 223 223 223 223 223 223 223 223 + 223 210 210 210 210 210 210 210 210 210 210 210 210 + 210 210 210 210 210 210 210 210 210 280 280 280 280 + 280 280 280 280 280 280 280 280 280 280 280 280 279 + 279 279 279 279 279 279 279 279 279 279 279 279 279 + 279 279 279 279 279 279 279 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 274 274 274 274 274 274 274 274 274 274 274 274 274 + 274 274 274 274 274 274 274 274 212 212 212 212 212 + 212 212 212 212 212 212 212 212 212 212 212 212 212 + 212 212 212 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 232 232 232 + 232 232 232 232 232 232 232 232 232 232 232 232 232 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229} + +do_execsql_test 1.19.9.3 { + SELECT last_value(a+b) OVER ( ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2 +} {276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276} + +do_execsql_test 1.19.9.4 { + SELECT last_value(a+b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2 +} {240 240 240 240 240 240 240 240 240 240 240 240 240 + 240 263 263 263 263 263 263 263 263 263 263 263 263 + 263 263 263 263 263 263 263 263 263 280 280 280 280 + 280 280 280 280 280 280 280 280 280 280 280 280 252 + 252 252 252 252 252 252 252 252 252 252 252 252 252 + 252 252 252 252 252 252 252 171 171 171 171 171 171 + 171 171 171 171 171 171 171 171 171 171 171 171 171 + 274 274 274 274 274 274 274 274 274 274 274 274 274 + 274 274 274 274 274 274 274 274 226 226 226 226 226 + 226 226 226 226 226 226 226 226 226 226 226 226 226 + 226 226 226 124 124 124 124 124 124 124 124 124 124 + 124 124 124 124 124 124 124 124 124 124 198 198 198 + 198 198 198 198 198 198 198 198 198 198 198 198 198 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276} + +do_execsql_test 1.19.9.5 { + SELECT last_value(a+b) OVER ( ORDER BY b%10,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2 +} {229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229} + +do_execsql_test 1.19.9.6 { + SELECT last_value(a+b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES) FROM t2 +} {108 52 83 79 65 26 70 103 80 36 116 51 52 128 + 117 71 63 84 109 78 147 88 121 106 124 85 107 171 + 150 80 171 120 109 158 87 168 173 162 156 195 198 + 177 124 121 134 141 210 157 132 161 218 226 191 179 + 138 214 212 172 173 229 240 187 210 227 228 223 225 + 179 182 231 207 209 212 239 234 213 234 269 196 271 + 235 250 223 232 229 280 99 92 72 55 109 120 119 + 50 124 96 59 124 110 57 130 103 74 87 48 105 136 + 131 133 92 109 57 146 113 74 150 87 110 65 110 + 145 161 156 114 111 136 147 173 124 132 101 154 167 + 190 161 110 102 123 169 140 111 180 119 160 197 152 + 146 147 132 213 193 200 136 175 188 187 208 211 144 + 223 196 170 202 163 184 195 200 163 191 252 235 243 + 172 187 202 179 261 263 206 189 276 181 274 249 221 + 210 229 279 224 216 207} + +do_execsql_test 1.19.10.1 { + SELECT nth_value(b,b+1) OVER (ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES) FROM t2 +} {96 41 5 16 85 30 12 81 35 89 97 31 31 85 40 + 96 4 73 84 9 91 73 12 5 56 24 85 49 96 75 53 + 61 8 8 1 55 34 43 59 80 35 15 78 35 56 70 76 + 59 51 75 63 26 53 5 89 15 21 5 73 33 29 74 66 + 12 26 58 4 12 31 35 9 87 73 55 59 53 62 73 23 + 62 33 90 13 90 9 10 66 5 58 44 38 58 22 33 37 + 2 73 36 31 72 30 47 73 15 96 70 59 90 {} 7 21 + 83 {} 47 90 55 36 66 {} 50 {} 84 30 {} {} 34 + 77 74 {} 58 {} 13 {} 82 93 69 14 62 44 {} {} + 30 {} 83 93 {} {} {} 84 {} {} {} {} 14 30 82 + 34 34 3 {} {} {} {} {} 84 {} {} {} 99 {} {} + {} {} {} {} {} 58 {} {} {} {} {} {} {} {} {} + {} {} {} {} {}} + +do_execsql_test 1.19.10.2 { + SELECT nth_value(b,b+1) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 61 {} {} {} 81 {} 91 {} {} {} {} + {} {} {} {} {} {} 12 {} {} {} 22 {} 82 {} 12 + {} {} {} {} {} {} {} {} {} {} 43 {} {} {} {} + {} 33 {} {} {} {} {} {} {} 33 {} {} {} {} {} + {} 4 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} 75 {} {} {} {} {} {} 15 {} + {} {} {} {} {} {} {} {} {} 86 {} 26 36 {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 37 {} {} {} {} {} 47 {} {} {} {} + {} {} {} {} {} {} 98 {} {} {} {} {} 58 {} 38 + {} {} {} {} {} {} {} {} {} {} {} {} {} 59 {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.19.10.3 { + SELECT nth_value(b,b+1) OVER ( ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2 +} {1 1 2 2 2 3 4 6 7 8 8 8 9 9 10 11 12 12 + 13 14 15 15 16 16 17 19 21 22 22 23 24 25 26 + 27 29 29 30 30 31 32 33 33 34 34 35 35 36 36 + 37 38 38 39 39 40 41 42 43 44 46 46 47 47 47 + 49 50 51 53 54 55 56 56 56 58 58 58 59 59 59 + 60 61 62 63 65 65 66 68 69 72 72 73 73 74 74 + 75 76 77 78 80 81 81 82 84 84 84 85 85 86 87 + 87 89 89 89 90 90 91 91 91 93 93 95 95 96 96 + 97 98 99 {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {}} + +do_execsql_test 1.19.10.4 { + SELECT nth_value(b,b+1) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2 +} {80 {} {} {} {} {} {} {} {} {} {} {} {} {} 1 1 + 61 61 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 12 12 72 72 72 {} {} {} {} {} {} + {} {} {} {} {} 13 13 63 63 {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} 34 84 {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + 35 85 85 85 {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} 36 76 76 76 {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} 37 37 37 + 87 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} 58 58 58 {} {} {} {} {} {} {} {} {} {} + {} {} {} 39 39 39 89 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.19.10.5 { + SELECT nth_value(b,b+1) OVER ( ORDER BY b%10,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2 +} {34 2 11 34 81 65 73 84 64 55 12 33 91 42 65 + 35 84 15 95 61 25 53 74 81 72 91 84 33 3 43 + 32 85 56 56 93 85 2 93 43 94 22 75 82 13 12 + 46 6 3 55 25 26 47 83 37 85 43 55 34 75 86 36 + 33 36 36 85 68 14 4 25 33 95 27 16 38 77 55 4 + 58 98 37 15 95 16 38 77 55 16 58 38 36 56 7 + 36 59 89 57 75 86 89 39 98 8 97 15 46 {} 28 9 + 69 49 56 {} {} 78 16 28 26 36 {} 39 99 29 27 + 78 {} {} {} 37 27 98 {} 88 8 {} 28 {} {} {} + 59 37 59 {} 89 {} {} 47 {} 39 {} 29 29 8 78 9 + {} {} 58 59 {} {} {} {} 58 {} 38 {} {} {} {} + {} {} {} {} {} {} {} {} {} 59 {} {} {} {} {} + {} {} {} {} {} {} {}} + +do_execsql_test 1.19.10.6 { + SELECT nth_value(b,b+1) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.19.11.1 { + SELECT first_value(b) OVER (ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES) FROM t2 +} {89 89 89 89 89 81 96 59 38 68 39 62 91 46 6 + 99 97 27 46 78 54 97 8 67 29 93 84 77 23 16 + 16 93 65 35 47 7 86 74 61 91 85 24 85 43 59 + 12 32 56 3 91 22 90 55 15 28 89 25 47 1 56 40 + 43 56 16 75 36 89 98 76 81 4 94 42 30 78 33 + 29 53 63 2 87 37 80 84 72 41 9 61 73 95 65 13 + 58 96 98 1 21 74 65 35 5 73 11 51 87 41 12 8 + 20 31 31 15 95 22 73 79 88 34 8 11 49 34 90 + 59 96 60 55 75 77 44 2 7 85 57 74 29 70 59 19 + 39 26 26 47 80 90 36 58 47 9 72 72 66 33 93 + 75 64 81 9 23 37 13 12 14 62 91 36 91 33 15 + 34 36 99 3 95 69 58 52 30 50 84 10 84 33 21 + 39 44 58 30 38 34 83} + +do_execsql_test 1.19.11.2 { + SELECT first_value(b) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES) FROM t2 +} {90 90 90 90 90 40 30 80 20 90 60 70 80 90 81 + 81 81 81 81 91 61 91 91 1 81 41 61 1 21 11 51 + 41 31 31 11 62 62 62 62 62 12 32 22 42 2 72 + 12 22 2 72 72 93 93 93 93 93 23 93 43 3 43 33 + 53 63 73 13 73 73 33 93 23 13 54 54 54 54 54 + 84 74 24 4 94 84 74 34 34 44 74 64 14 34 65 + 65 65 65 65 35 85 85 55 15 25 75 95 65 65 35 + 5 15 95 55 75 96 96 96 96 96 46 6 46 16 16 86 + 56 56 56 16 36 76 96 96 26 26 97 97 97 97 97 + 27 97 67 77 47 7 47 87 37 87 77 7 57 47 47 38 + 38 38 38 38 68 78 8 28 98 78 58 98 8 88 8 89 + 89 89 89 89 59 39 99 29 59 89 89 29 9 79 49 + 59 29 59 19 39 9} + +do_execsql_test 1.19.11.3 { + SELECT first_value(b) OVER ( ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2 +} {1 1 1 1 1 1 2 2 3 3 4 5 6 7 7 7 8 8 8 + 9 9 9 10 11 11 12 12 12 13 13 14 15 15 15 16 + 16 16 17 19 20 21 21 22 22 23 23 24 25 26 26 + 27 27 28 29 29 29 30 30 30 31 31 32 33 33 33 + 33 34 34 34 34 35 35 36 36 36 36 37 37 38 38 + 39 39 39 40 41 41 42 43 43 44 44 46 46 47 47 + 47 47 49 50 51 52 53 54 55 55 56 56 56 57 58 + 58 58 58 59 59 59 59 60 61 61 62 62 63 64 65 + 65 65 66 67 68 69 70 72 72 72 73 73 73 74 74 + 74 75 75 75 76 77 77 78 78 79 80 80 81 81 81 + 82 83 84 84 84 84 85 85 85 86 87 87 88 89 89 + 89 90 90 90 91 91 91 91 91 93 93 93 94 95 95 + 95 96 96 96 97 97} + +do_execsql_test 1.19.11.4 { + SELECT first_value(b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2 +} {10 10 10 10 10 20 30 30 30 40 50 60 70 80 1 1 + 1 1 1 1 11 11 21 21 31 31 41 41 51 61 61 81 + 81 81 91 2 2 2 2 2 2 12 12 12 22 22 32 42 + 52 62 62 3 3 3 3 3 3 13 13 23 23 33 33 33 + 33 43 43 53 63 73 73 73 4 4 4 4 4 14 24 34 + 34 34 34 44 44 54 64 74 74 74 84 5 5 5 5 5 + 15 15 15 25 35 35 55 55 65 65 65 75 75 75 85 + 85 6 6 6 6 6 16 16 16 26 26 36 36 36 36 46 + 46 56 56 56 66 76 7 7 7 7 7 7 7 17 27 27 37 + 37 47 47 47 47 57 67 77 77 8 8 8 8 8 8 8 28 + 38 38 58 58 58 58 68 78 9 9 9 9 9 9 9 19 29 + 29 29 39 39 39 49 59 59 59 59 69 79 89} + +do_execsql_test 1.19.11.5 { + SELECT first_value(b) OVER ( ORDER BY b%10,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2 +} {90 90 90 90 90 40 30 80 20 90 60 70 80 90 30 + 50 10 30 81 91 61 91 91 1 81 41 61 1 21 11 51 + 41 31 31 11 81 91 91 21 62 12 32 22 42 2 72 + 12 22 2 72 72 12 62 52 82 93 23 93 43 3 43 33 + 53 63 73 13 73 73 33 93 23 13 33 3 33 83 54 + 84 74 24 4 94 84 74 34 34 44 74 64 14 34 84 + 84 44 34 65 35 85 85 55 15 25 75 95 65 65 35 + 5 15 95 55 75 85 75 15 95 96 46 6 46 16 16 86 + 56 56 56 16 36 76 96 96 26 26 36 66 36 36 97 + 27 97 67 77 47 7 47 87 37 87 77 7 57 47 47 37 + 27 17 7 38 68 78 8 28 98 78 58 98 8 88 8 58 + 58 58 38 89 59 39 99 29 59 89 89 29 9 79 49 + 59 29 59 19 39 9} + +do_execsql_test 1.19.11.6 { + SELECT first_value(b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES) FROM t2 +} {96 38 68 62 46 6 46 78 54 8 84 16 16 86 74 24 + 12 32 56 22 90 28 56 40 56 16 36 98 76 4 94 + 42 30 78 2 80 84 72 58 96 98 74 12 8 20 22 88 + 34 8 34 90 96 60 44 2 74 70 26 26 80 90 36 58 + 72 72 66 64 12 14 62 36 34 36 58 52 30 50 84 + 10 84 44 58 30 38 34 82 89 81 59 39 91 99 97 + 27 97 67 29 93 77 23 93 65 35 47 7 61 91 85 + 85 43 59 3 91 55 15 89 25 47 1 43 75 89 81 33 + 29 53 63 87 37 41 9 61 73 95 65 13 1 21 65 35 + 5 73 11 51 87 41 31 31 15 95 73 79 11 49 59 + 55 75 77 7 85 57 29 59 19 39 47 47 9 33 93 75 + 81 9 23 37 13 91 91 33 15 99 3 95 69 33 21 39 + 83 27 17 7} + +do_execsql_test 1.19.12.1 { + SELECT lead(b,b) OVER (ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES) FROM t2 +} {96 9 11 89 32 53 91 30 51 56 54 73 22 59 75 + 74 78 8 16 65 15 8 31 87 90 12 32 96 74 76 37 + 85 90 15 35 2 60 36 75 9 51 47 63 51 90 26 42 + 26 8 76 80 90 37 87 56 79 5 87 8 2 39 73 64 + 36 90 72 78 36 73 51 33 20 41 2 26 37 33 8 14 + 33 81 55 1 9 12 39 64 87 72 34 82 21 34 99 62 + 74 41 69 22 75 27 58 8 79 77 26 26 55 {} 29 + 30 7 {} 66 55 2 34 64 {} 33 {} 44 84 {} {} 95 + 85 19 {} 83 {} 91 {} {} 9 50 91 33 34 {} {} + 84 {} 7 9 {} {} {} 44 {} {} {} {} 91 84 {} 95 + 95 52 {} {} {} {} {} 21 {} {} {} 58 {} {} {} + {} {} {} {} 83 {} {} {} {} {} {} {} {} {} {} + {} {} {} {}} + +do_execsql_test 1.19.12.2 { + SELECT lead(b,b) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 81 {} {} {} 21 {} {} {} {} {} {} + {} {} {} {} {} {} 62 {} {} {} 12 {} {} {} 72 + {} {} {} {} {} {} {} {} {} {} 53 {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 34 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} 95 {} {} {} {} {} {} 85 {} + {} {} {} {} {} {} {} {} {} 56 {} 36 {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 57 {} {} {} {} {} 7 {} {} {} {} + {} {} {} {} {} {} 8 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 9 {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.19.12.3 { + SELECT lead(b,b) OVER ( ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2 +} {1 2 3 3 5 6 7 8 8 9 9 10 11 12 12 13 13 + 14 15 16 16 17 19 20 21 22 23 24 25 26 27 27 + 28 29 30 31 32 33 33 33 34 34 35 36 36 36 37 + 38 39 39 40 41 42 43 43 44 46 47 47 47 49 50 + 52 53 54 55 56 56 57 58 58 58 59 59 59 60 61 + 62 62 64 65 65 67 69 70 72 72 73 74 74 75 75 + 75 77 78 80 81 81 83 84 84 85 85 85 87 88 89 + 89 89 90 90 91 91 91 93 93 94 95 95 96 97 97 + 98 99 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.19.12.4 { + SELECT lead(b,b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2 +} {80 {} {} {} {} {} {} {} {} {} {} {} {} {} 1 + 11 81 81 {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} 12 12 72 82 {} {} {} {} {} {} + {} {} {} {} {} {} 13 23 73 73 {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} 34 84 {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 35 85 85 95 {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} 36 86 96 96 {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 37 47 + 47 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} 58 58 68 {} {} {} {} {} {} {} {} {} + {} {} {} {} 39 49 59 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.19.12.5 { + SELECT lead(b,b) OVER ( ORDER BY b%10,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2 +} {34 72 91 74 21 55 23 94 84 95 2 13 61 22 55 + 55 94 85 75 81 65 73 74 21 72 12 94 33 63 73 + 72 96 36 76 3 25 62 3 73 34 12 46 43 93 72 16 + 86 63 15 65 36 77 24 57 25 53 95 34 95 16 97 + 74 97 67 25 98 44 34 65 54 5 68 96 28 47 95 + 34 39 8 38 6 46 96 28 47 95 56 39 99 97 76 8 + 26 9 79 27 95 16 29 {} 58 58 77 85 56 {} 98 + 29 {} 19 96 {} {} 78 56 98 36 97 {} 89 89 29 + 47 78 {} {} {} 38 68 58 {} 58 38 {} 98 {} {} + {} 39 57 9 {} 79 {} {} 7 {} {} {} 9 29 38 78 + {} {} {} 8 39 {} {} {} {} 59 {} 99 {} {} {} + {} {} {} {} {} {} {} {} {} {} 9 {} {} {} {} + {} {} {} {} {} {} {} {}} + +do_execsql_test 1.19.12.6 { + SELECT lead(b,b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.19.13.1 { + SELECT lag(b,b) OVER (ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} 38 {} {} {} {} + {} {} {} 6 {} {} {} {} {} 81 46 6 {} {} {} {} + 23 {} {} {} {} {} 27 {} {} {} 35 6 {} 12 {} + 23 {} {} 61 84 {} 93 39 47 {} 54 46 96 56 {} + 16 {} {} {} {} 89 {} 16 43 {} 85 56 29 99 53 + {} 59 {} {} 91 59 53 84 99 {} 93 63 47 {} {} + 98 33 67 35 75 1 23 13 55 27 75 98 35 73 63 2 + 21 27 13 24 86 23 84 31 20 94 61 65 75 23 36 + 94 55 90 41 77 96 56 29 40 12 89 63 11 5 73 + 79 1 16 28 31 73 5 39 53 63 41 11 40 2 13 33 + 9 29 90 47 72 9 73 30 44 33 74 93 29 74 42 34 + 63 41 34 96 47 77 1 36 74 72 14 36 26 77 9 72 + 64 8 91 31 52 30} + +do_execsql_test 1.19.13.2 { + SELECT lag(b,b) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} 30 {} {} + {} {} {} {} 91 {} {} {} 61 {} 81 {} {} {} {} + 1 {} {} {} {} {} {} {} {} {} 22 {} {} {} 12 + {} {} 62 {} {} {} {} {} {} {} 23 {} {} {} {} + {} {} {} {} {} {} {} 43 {} 23 {} {} {} {} {} + {} 54 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 75 {} + {} {} {} {} {} 55 {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} 47 {} {} {} {} + {} 27 7 {} {} {} {} {} {} {} {} {} 68 {} 8 {} + {} {} {} {} {} {} {} {} {} {} {} {} 89 {} {} + {} {} {} {} {} 29 9 {} {} {}} + +do_execsql_test 1.19.13.3 { + SELECT lag(b,b) OVER ( ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2 +} {{} 1 1 1 1 2 2 2 2 2 2 3 3 3 4 4 5 6 6 + 6 7 7 7 7 7 8 8 8 8 8 8 9 9 9 9 9 9 9 + 9 9 9 10 10 10 10 11 11 11 11 11 12 12 12 12 + 13 13 13 13 13 14 15 15 15 15 16 16 16 16 16 + 17 19 20 20 21 21 21 21 22 22 22 22 23 23 23 + 23 23 24 23 24 24 25 26 26 26 26 26 26 26 26 + 26 26 26 27 27 27 27 28 29 29 29 29 30 30 30 + 30 30 30 31 31 31 31 31 32 32 32 32 32 32 31 + 32 33 33 33 33 33 33 34 34 34 34 34 34 34 34 + 35 35 35 35 35 36 36 36 36 36 36 36 37 37 37 + 38 38 38 38 38 38 39 39 39 39 40 40 41 41 42 + 43 42 43 43 43 43 44 44 44 46 46 46 47 47 47 + 47 47} + +do_execsql_test 1.19.13.4 { + SELECT lag(b,b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + 1 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.19.13.5 { + SELECT lag(b,b) OVER ( ORDER BY b%10,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} 30 {} {} + {} {} {} {} 91 {} {} {} 61 80 81 {} {} {} {} + 1 {} {} {} 30 {} 21 90 61 {} 22 {} 11 41 12 + {} {} 62 {} {} {} {} 31 {} 50 23 30 21 90 {} + {} 62 {} {} 81 {} 22 43 62 23 32 {} 91 {} 90 + 93 54 {} {} 90 72 12 22 90 81 83 23 80 20 72 + 43 51 33 80 90 2 34 54 1 20 62 12 13 75 44 30 + 93 91 1 21 55 61 61 13 85 3 65 65 91 73 33 93 + 55 84 62 31 11 65 35 85 33 55 15 12 75 22 3 + 73 65 36 85 43 95 43 13 47 44 65 65 96 36 27 + 7 46 34 94 47 36 73 34 35 73 68 24 8 75 85 75 + 66 34 95 36 84 77 46 34 84 47 89 65 36 16 38 + 76 58 57 29 9 44 56 17} + +do_execsql_test 1.19.13.6 { + SELECT lag(b,b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.19.14.1 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES) FROM t2 +} {89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 33.21.39.44.58.30.38.34.83.27.82.17.7 21.39.44.58.30.38.34.83.27.82.17.7 + 39.44.58.30.38.34.83.27.82.17.7 44.58.30.38.34.83.27.82.17.7 + 58.30.38.34.83.27.82.17.7 30.38.34.83.27.82.17.7 38.34.83.27.82.17.7 + 34.83.27.82.17.7 83.27.82.17.7} + +do_execsql_test 1.19.14.2 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES) FROM t2 +} {90.40.30.80.20.90.60.70.80.90.30.50.10.30 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30 + 40.30.80.20.90.60.70.80.90.30.50.10.30 + 30.80.20.90.60.70.80.90.30.50.10.30 80.20.90.60.70.80.90.30.50.10.30 + 20.90.60.70.80.90.30.50.10.30 90.60.70.80.90.30.50.10.30 + 60.70.80.90.30.50.10.30 70.80.90.30.50.10.30 80.90.30.50.10.30 + 90.30.50.10.30 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 61.1.21.11.51.41.31.31.11.81.91.91.21 1.21.11.51.41.31.31.11.81.91.91.21 + 21.11.51.41.31.31.11.81.91.91.21 11.51.41.31.31.11.81.91.91.21 + 51.41.31.31.11.81.91.91.21 41.31.31.11.81.91.91.21 31.31.11.81.91.91.21 + 31.11.81.91.91.21 11.81.91.91.21 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 22.42.2.72.12.22.2.72.72.12.62.52.82 42.2.72.12.22.2.72.72.12.62.52.82 + 2.72.12.22.2.72.72.12.62.52.82 72.12.22.2.72.72.12.62.52.82 + 12.22.2.72.72.12.62.52.82 22.2.72.72.12.62.52.82 2.72.72.12.62.52.82 + 72.72.12.62.52.82 72.12.62.52.82 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 63.73.13.73.73.33.93.23.13.33.3.33.83 73.13.73.73.33.93.23.13.33.3.33.83 + 13.73.73.33.93.23.13.33.3.33.83 73.73.33.93.23.13.33.3.33.83 + 73.33.93.23.13.33.3.33.83 33.93.23.13.33.3.33.83 93.23.13.33.3.33.83 + 23.13.33.3.33.83 13.33.3.33.83 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 84.74.34.34.44.74.64.14.34.84.84.44.34 + 74.34.34.44.74.64.14.34.84.84.44.34 34.34.44.74.64.14.34.84.84.44.34 + 34.44.74.64.14.34.84.84.44.34 44.74.64.14.34.84.84.44.34 + 74.64.14.34.84.84.44.34 64.14.34.84.84.44.34 14.34.84.84.44.34 + 34.84.84.44.34 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 95.65.65.35.5.15.95.55.75.85.75.15.95 65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.5.15.95.55.75.85.75.15.95 35.5.15.95.55.75.85.75.15.95 + 5.15.95.55.75.85.75.15.95 15.95.55.75.85.75.15.95 95.55.75.85.75.15.95 + 55.75.85.75.15.95 75.85.75.15.95 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 56.56.16.36.76.96.96.26.26.36.66.36.36 + 56.16.36.76.96.96.26.26.36.66.36.36 16.36.76.96.96.26.26.36.66.36.36 + 36.76.96.96.26.26.36.66.36.36 76.96.96.26.26.36.66.36.36 + 96.96.26.26.36.66.36.36 96.26.26.36.66.36.36 26.26.36.66.36.36 + 26.36.66.36.36 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 47.87.37.87.77.7.57.47.47.37.27.17.7 87.37.87.77.7.57.47.47.37.27.17.7 + 37.87.77.7.57.47.47.37.27.17.7 87.77.7.57.47.47.37.27.17.7 + 77.7.57.47.47.37.27.17.7 7.57.47.47.37.27.17.7 57.47.47.37.27.17.7 + 47.47.37.27.17.7 47.37.27.17.7 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 8.28.98.78.58.98.8.88.8.58.58.58.38 28.98.78.58.98.8.88.8.58.58.58.38 + 98.78.58.98.8.88.8.58.58.58.38 78.58.98.8.88.8.58.58.58.38 + 58.98.8.88.8.58.58.58.38 98.8.88.8.58.58.58.38 8.88.8.58.58.58.38 + 88.8.58.58.58.38 8.58.58.58.38 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 9.79.49.59.29.59.19.39.9.9.99.69.39 79.49.59.29.59.19.39.9.9.99.69.39 + 49.59.29.59.19.39.9.9.99.69.39 59.29.59.19.39.9.9.99.69.39 + 29.59.19.39.9.9.99.69.39 59.19.39.9.9.99.69.39 19.39.9.9.99.69.39 + 39.9.9.99.69.39 9.9.99.69.39} + +do_execsql_test 1.19.14.3 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2 +} {1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 94.95.95.95.96.96.96.97.97.98.98.99.99 + 95.95.95.96.96.96.97.97.98.98.99.99 95.95.96.96.96.97.97.98.98.99.99 + 95.96.96.96.97.97.98.98.99.99 96.96.96.97.97.98.98.99.99 + 96.96.97.97.98.98.99.99 96.97.97.98.98.99.99 97.97.98.98.99.99 + 97.98.98.99.99} + +do_execsql_test 1.19.14.4 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2 +} {10.20.30.30.30.40.50.60.70.80.80.90.90.90 + 10.20.30.30.30.40.50.60.70.80.80.90.90.90 + 10.20.30.30.30.40.50.60.70.80.80.90.90.90 + 10.20.30.30.30.40.50.60.70.80.80.90.90.90 + 10.20.30.30.30.40.50.60.70.80.80.90.90.90 + 20.30.30.30.40.50.60.70.80.80.90.90.90 + 30.30.30.40.50.60.70.80.80.90.90.90 30.30.40.50.60.70.80.80.90.90.90 + 30.40.50.60.70.80.80.90.90.90 40.50.60.70.80.80.90.90.90 + 50.60.70.80.80.90.90.90 60.70.80.80.90.90.90 70.80.80.90.90.90 + 80.80.90.90.90 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 41.41.51.61.61.81.81.81.91.91.91.91.91 + 41.51.61.61.81.81.81.91.91.91.91.91 51.61.61.81.81.81.91.91.91.91.91 + 61.61.81.81.81.91.91.91.91.91 61.81.81.81.91.91.91.91.91 + 81.81.81.91.91.91.91.91 81.81.91.91.91.91.91 81.91.91.91.91.91 + 91.91.91.91.91 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 12.12.22.22.32.42.52.62.62.72.72.72.82 + 12.22.22.32.42.52.62.62.72.72.72.82 22.22.32.42.52.62.62.72.72.72.82 + 22.32.42.52.62.62.72.72.72.82 32.42.52.62.62.72.72.72.82 + 42.52.62.62.72.72.72.82 52.62.62.72.72.72.82 62.62.72.72.72.82 + 62.72.72.72.82 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 33.33.43.43.53.63.73.73.73.83.93.93.93 + 33.43.43.53.63.73.73.73.83.93.93.93 43.43.53.63.73.73.73.83.93.93.93 + 43.53.63.73.73.73.83.93.93.93 53.63.73.73.73.83.93.93.93 + 63.73.73.73.83.93.93.93 73.73.73.83.93.93.93 73.73.83.93.93.93 + 73.83.93.93.93 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 34.44.44.54.64.74.74.74.84.84.84.84.94 + 44.44.54.64.74.74.74.84.84.84.84.94 44.54.64.74.74.74.84.84.84.84.94 + 54.64.74.74.74.84.84.84.84.94 64.74.74.74.84.84.84.84.94 + 74.74.74.84.84.84.84.94 74.74.84.84.84.84.94 74.84.84.84.84.94 + 84.84.84.84.94 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 55.65.65.65.75.75.75.85.85.85.95.95.95 + 65.65.65.75.75.75.85.85.85.95.95.95 65.65.75.75.75.85.85.85.95.95.95 + 65.75.75.75.85.85.85.95.95.95 75.75.75.85.85.85.95.95.95 + 75.75.85.85.85.95.95.95 75.85.85.85.95.95.95 85.85.85.95.95.95 + 85.85.95.95.95 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 36.36.46.46.56.56.56.66.76.86.96.96.96 + 36.46.46.56.56.56.66.76.86.96.96.96 46.46.56.56.56.66.76.86.96.96.96 + 46.56.56.56.66.76.86.96.96.96 56.56.56.66.76.86.96.96.96 + 56.56.66.76.86.96.96.96 56.66.76.86.96.96.96 66.76.86.96.96.96 + 76.86.96.96.96 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 37.47.47.47.47.57.67.77.77.87.87.97.97 + 47.47.47.47.57.67.77.77.87.87.97.97 47.47.47.57.67.77.77.87.87.97.97 + 47.47.57.67.77.77.87.87.97.97 47.57.67.77.77.87.87.97.97 + 57.67.77.77.87.87.97.97 67.77.77.87.87.97.97 77.77.87.87.97.97 + 77.87.87.97.97 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 28.38.38.58.58.58.58.68.78.78.88.98.98 + 38.38.58.58.58.58.68.78.78.88.98.98 38.58.58.58.58.68.78.78.88.98.98 + 58.58.58.58.68.78.78.88.98.98 58.58.58.68.78.78.88.98.98 + 58.58.68.78.78.88.98.98 58.68.78.78.88.98.98 68.78.78.88.98.98 + 78.78.88.98.98 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 39.49.59.59.59.59.69.79.89.89.89.99.99 + 49.59.59.59.59.69.79.89.89.89.99.99 59.59.59.59.69.79.89.89.89.99.99 + 59.59.59.69.79.89.89.89.99.99 59.59.69.79.89.89.89.99.99 + 59.69.79.89.89.89.99.99 69.79.89.89.89.99.99 79.89.89.89.99.99 + 89.89.89.99.99} + +do_execsql_test 1.19.14.5 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( ORDER BY b%10,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) FROM t2 +} {90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 9.79.49.59.29.59.19.39.9.9.99.69.39 79.49.59.29.59.19.39.9.9.99.69.39 + 49.59.29.59.19.39.9.9.99.69.39 59.29.59.19.39.9.9.99.69.39 + 29.59.19.39.9.9.99.69.39 59.19.39.9.9.99.69.39 19.39.9.9.99.69.39 + 39.9.9.99.69.39 9.9.99.69.39} + +do_execsql_test 1.19.14.6 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES) FROM t2 +} {96 38 68 62 46 6 46 78 54 8 84 16 16 86 74 24 + 12 32 56 22 90 28 56 40 56 16 36 98 76 4 94 + 42 30 78 2 80 84 72 58 96 98 74 12 8 20 22 88 + 34 8 34 90 96 60 44 2 74 70 26 26 80 90 36 58 + 72 72 66 64 12 14 62 36 34 36 58 52 30 50 84 + 10 84 44 58 30 38 34 82 89 81 59 39 91 99 97 + 27 97 67 29 93 77 23 93 65 35 47 7 61 91 85 + 85 43 59 3 91 55 15 89 25 47 1 43 75 89 81 33 + 29 53 63 87 37 41 9 61 73 95 65 13 1 21 65 35 + 5 73 11 51 87 41 31 31 15 95 73 79 11 49 59 + 55 75 77 7 85 57 29 59 19 39 47 47 9 33 93 75 + 81 9 23 37 13 91 91 33 15 99 3 95 69 33 21 39 + 83 27 17 7} + +do_execsql_test 1.19.14.7 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (win1 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES) + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a) + ORDER BY 1 +} {1 1 10 11 11 12 12 12 13 13 14 15 15 15 16 16 + 16 17 19 2 2 20 21 21 22 22 23 23 24 25 26 26 + 27 27 28 29 29 29 3 3 30 30 30 31 31 32 33 33 + 33 33 34 34 34 34 35 35 36 36 36 36 37 37 38 + 38 39 39 39 4 40 41 41 42 43 43 44 44 46 46 + 47 47 47 47 49 5 50 51 52 53 54 55 55 56 56 + 56 57 58 58 58 58 59 59 59 59 6 60 61 61 62 + 62 63 64 65 65 65 66 67 68 69 7 7 7 70 72 72 + 72 73 73 73 74 74 74 75 75 75 76 77 77 78 78 + 79 8 8 8 80 80 81 81 81 82 83 84 84 84 84 85 + 85 85 86 87 87 88 89 89 89 9 9 9 90 90 90 91 + 91 91 91 91 93 93 93 94 95 95 95 96 96 96 97 + 97 98 98 99 99} + +do_execsql_test 1.19.14.8 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (win1 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES) + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a ORDER BY b%10) + ORDER BY 1 +} {1 1 10 11 11 12 12 12 13 13 14 15 15 15 16 16 + 16 17 19 2 2 20 21 21 22 22 23 23 24 25 26 26 + 27 27 28 29 29 29 3 3 30 30 30 31 31 32 33 33 + 33 33 34 34 34 34 35 35 36 36 36 36 37 37 38 + 38 39 39 39 4 40 41 41 42 43 43 44 44 46 46 + 47 47 47 47 49 5 50 51 52 53 54 55 55 56 56 + 56 57 58 58 58 58 59 59 59 59 6 60 61 61 62 + 62 63 64 65 65 65 66 67 68 69 7 7 7 70 72 72 + 72 73 73 73 74 74 74 75 75 75 76 77 77 78 78 + 79 8 8 8 80 80 81 81 81 82 83 84 84 84 84 85 + 85 85 86 87 87 88 89 89 89 9 9 9 90 90 90 91 + 91 91 91 91 93 93 93 94 95 95 95 96 96 96 97 + 97 98 98 99 99} + +do_execsql_test 1.19.14.9 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER win2 + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a ORDER BY b%10), + win2 AS (win1 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES) + ORDER BY 1 +} {1 1 10 11 11 12 12 12 13 13 14 15 15 15 16 16 + 16 17 19 2 2 20 21 21 22 22 23 23 24 25 26 26 + 27 27 28 29 29 29 3 3 30 30 30 31 31 32 33 33 + 33 33 34 34 34 34 35 35 36 36 36 36 37 37 38 + 38 39 39 39 4 40 41 41 42 43 43 44 44 46 46 + 47 47 47 47 49 5 50 51 52 53 54 55 55 56 56 + 56 57 58 58 58 58 59 59 59 59 6 60 61 61 62 + 62 63 64 65 65 65 66 67 68 69 7 7 7 70 72 72 + 72 73 73 73 74 74 74 75 75 75 76 77 77 78 78 + 79 8 8 8 80 80 81 81 81 82 83 84 84 84 84 85 + 85 85 86 87 87 88 89 89 89 9 9 9 90 90 90 91 + 91 91 91 91 93 93 93 94 95 95 95 96 96 96 97 + 97 98 98 99 99} + +do_execsql_test 1.19.15.1 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE a%2=0) OVER win FROM t2 + WINDOW win AS (ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES) +} {191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 191 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 190 96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 189 96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 188 38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 187 38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 186 39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 185 39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 184 91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 183 91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 182 6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 181 6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 180 97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 179 97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 178 46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 177 46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 176 54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 175 54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 174 8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 173 8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 172 29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 171 29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 170 84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 169 84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 168 23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 167 23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 166 16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 165 16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 164 65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 163 65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 162 47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 161 47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 160 86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 159 86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 158 61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 157 61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 156 85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 155 85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 154 85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 153 85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 152 59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 151 59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 150 32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 149 32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 148 3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 147 3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 146 22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 145 22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 144 55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 143 55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 142 28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 141 28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 140 25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 139 25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 138 1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 137 1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 136 40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 135 40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 134 56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 133 56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 132 75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 131 75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 130 89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 129 89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 128 76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 127 76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 126 4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 125 4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 124 42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 123 42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 122 78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 121 78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 120 29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 119 29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 118 63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 117 63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 116 87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 115 87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 114 80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 113 80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 112 72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 111 72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 110 9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 109 9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 108 73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 107 73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 106 65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 105 65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 104 58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 103 58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 102 98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 101 98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 100 21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 99 21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 98 65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 97 65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 96 5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 95 5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 94 11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 93 11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 92 87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 91 87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 90 12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 89 12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 88 20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 87 20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 86 31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 85 31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 84 95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 83 95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 82 73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 81 73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 80 88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 79 88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 78 8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 77 8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 76 49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 75 49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 74 90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 73 90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 72 96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 71 96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 70 55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 69 55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 68 77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 67 77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 66 2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 65 2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 64 85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 63 85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 62 74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 61 74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 60 70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 59 70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 58 19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 57 19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 56 26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 55 26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 54 47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 53 47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 52 90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 51 90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 50 58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 49 58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 48 9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 47 9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 46 72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 45 72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 44 33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 43 33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 42 75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 41 75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 40 81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 39 81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 38 23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 37 23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 36 13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 35 13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 34 14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 33 14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 32 91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 31 91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 30 91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 29 91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 28 15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 27 15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 26 36.3.69.52.50.10.33.39.58.38.83.82.7 + 25 36.3.69.52.50.10.33.39.58.38.83.82.7 + 24 3.69.52.50.10.33.39.58.38.83.82.7 23 3.69.52.50.10.33.39.58.38.83.82.7 + 22 69.52.50.10.33.39.58.38.83.82.7 21 69.52.50.10.33.39.58.38.83.82.7 + 20 52.50.10.33.39.58.38.83.82.7 19 52.50.10.33.39.58.38.83.82.7 + 18 50.10.33.39.58.38.83.82.7 17 50.10.33.39.58.38.83.82.7 + 16 10.33.39.58.38.83.82.7 15 10.33.39.58.38.83.82.7 + 14 33.39.58.38.83.82.7 13 33.39.58.38.83.82.7 12 39.58.38.83.82.7 + 11 39.58.38.83.82.7 10 58.38.83.82.7 9 58.38.83.82.7 8 38.83.82.7 + 7 38.83.82.7 6 83.82.7 5 83.82.7} + +do_execsql_test 1.19.15.2 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE 0=1) OVER win FROM t2 + WINDOW win AS (ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES) +} {191 {} 191 {} 191 {} 191 {} 191 {} 190 {} 189 {} 188 {} + 187 {} 186 {} 185 {} 184 {} 183 {} 182 {} 181 {} 180 {} + 179 {} 178 {} 177 {} 176 {} 175 {} 174 {} 173 {} 172 {} + 171 {} 170 {} 169 {} 168 {} 167 {} 166 {} 165 {} 164 {} + 163 {} 162 {} 161 {} 160 {} 159 {} 158 {} 157 {} 156 {} + 155 {} 154 {} 153 {} 152 {} 151 {} 150 {} 149 {} 148 {} + 147 {} 146 {} 145 {} 144 {} 143 {} 142 {} 141 {} 140 {} + 139 {} 138 {} 137 {} 136 {} 135 {} 134 {} 133 {} 132 {} + 131 {} 130 {} 129 {} 128 {} 127 {} 126 {} 125 {} 124 {} + 123 {} 122 {} 121 {} 120 {} 119 {} 118 {} 117 {} 116 {} + 115 {} 114 {} 113 {} 112 {} 111 {} 110 {} 109 {} 108 {} + 107 {} 106 {} 105 {} 104 {} 103 {} 102 {} 101 {} 100 {} + 99 {} 98 {} 97 {} 96 {} 95 {} 94 {} 93 {} 92 {} 91 {} + 90 {} 89 {} 88 {} 87 {} 86 {} 85 {} 84 {} 83 {} 82 {} + 81 {} 80 {} 79 {} 78 {} 77 {} 76 {} 75 {} 74 {} 73 {} + 72 {} 71 {} 70 {} 69 {} 68 {} 67 {} 66 {} 65 {} 64 {} + 63 {} 62 {} 61 {} 60 {} 59 {} 58 {} 57 {} 56 {} 55 {} + 54 {} 53 {} 52 {} 51 {} 50 {} 49 {} 48 {} 47 {} 46 {} + 45 {} 44 {} 43 {} 42 {} 41 {} 40 {} 39 {} 38 {} 37 {} + 36 {} 35 {} 34 {} 33 {} 32 {} 31 {} 30 {} 29 {} 28 {} + 27 {} 26 {} 25 {} 24 {} 23 {} 22 {} 21 {} 20 {} 19 {} + 18 {} 17 {} 16 {} 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} + 9 {} 8 {} 7 {} 6 {} 5 {}} + +do_execsql_test 1.19.15.3 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE 1=0) OVER win FROM t2 + WINDOW win AS (PARTITION BY (a%10) ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES) +} {20 {} 20 {} 20 {} 20 {} 20 {} 19 {} 18 {} 17 {} 16 {} + 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} + 5 {} 19 {} 19 {} 19 {} 19 {} 19 {} 18 {} 17 {} 16 {} + 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} + 5 {} 19 {} 19 {} 19 {} 19 {} 19 {} 18 {} 17 {} 16 {} + 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} + 5 {} 19 {} 19 {} 19 {} 19 {} 19 {} 18 {} 17 {} 16 {} + 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} + 5 {} 19 {} 19 {} 19 {} 19 {} 19 {} 18 {} 17 {} 16 {} + 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} + 5 {} 19 {} 19 {} 19 {} 19 {} 19 {} 18 {} 17 {} 16 {} + 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} + 5 {} 19 {} 19 {} 19 {} 19 {} 19 {} 18 {} 17 {} 16 {} + 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} + 5 {} 19 {} 19 {} 19 {} 19 {} 19 {} 18 {} 17 {} 16 {} + 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} + 5 {} 19 {} 19 {} 19 {} 19 {} 19 {} 18 {} 17 {} 16 {} + 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} + 5 {} 19 {} 19 {} 19 {} 19 {} 19 {} 18 {} 17 {} 16 {} + 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} + 5 {}} + +do_execsql_test 1.19.15.4 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE a%2=0) OVER win FROM t2 + WINDOW win AS (PARTITION BY (a%10) ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES) +} {20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 20 89.6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 19 6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 18 29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 17 47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 16 59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 15 28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 14 75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 13 78.72.98.87.73.96.74.90.75.91.69.39.7 + 12 72.98.87.73.96.74.90.75.91.69.39.7 11 98.87.73.96.74.90.75.91.69.39.7 + 10 87.73.96.74.90.75.91.69.39.7 9 73.96.74.90.75.91.69.39.7 + 8 96.74.90.75.91.69.39.7 7 74.90.75.91.69.39.7 6 90.75.91.69.39.7 + 5 75.91.69.39.7 19 {} 19 {} 19 {} 19 {} 19 {} 18 {} 17 {} + 16 {} 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} + 6 {} 5 {} 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 19 96.97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 18 97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 17 84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 16 86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 15 32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 14 25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 13 89.29.9.21.12.88.55.70.58.81.91.52.58 + 12 29.9.21.12.88.55.70.58.81.91.52.58 11 9.21.12.88.55.70.58.81.91.52.58 + 10 21.12.88.55.70.58.81.91.52.58 9 12.88.55.70.58.81.91.52.58 + 8 88.55.70.58.81.91.52.58 7 55.70.58.81.91.52.58 6 70.58.81.91.52.58 + 5 58.81.91.52.58 19 {} 19 {} 19 {} 19 {} 19 {} 18 {} 17 {} + 16 {} 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} + 6 {} 5 {} 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 19 38.46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 18 46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 17 23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 16 61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 15 3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 14 1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 13 76.63.73.65.20.8.77.19.9.23.15.50.38 + 12 63.73.65.20.8.77.19.9.23.15.50.38 11 73.65.20.8.77.19.9.23.15.50.38 + 10 65.20.8.77.19.9.23.15.50.38 9 20.8.77.19.9.23.15.50.38 + 8 8.77.19.9.23.15.50.38 7 77.19.9.23.15.50.38 6 19.9.23.15.50.38 + 5 9.23.15.50.38 19 {} 19 {} 19 {} 19 {} 19 {} 18 {} 17 {} + 16 {} 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} + 6 {} 5 {} 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 19 39.54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 18 54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 17 16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 16 85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 15 22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 14 40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 13 4.87.65.5.31.49.2.26.72.13.36.10.83 + 12 87.65.5.31.49.2.26.72.13.36.10.83 11 65.5.31.49.2.26.72.13.36.10.83 + 10 5.31.49.2.26.72.13.36.10.83 9 31.49.2.26.72.13.36.10.83 + 8 49.2.26.72.13.36.10.83 7 2.26.72.13.36.10.83 6 26.72.13.36.10.83 + 5 72.13.36.10.83 19 {} 19 {} 19 {} 19 {} 19 {} 18 {} 17 {} + 16 {} 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} + 6 {} 5 {} 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 19 91.8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 18 8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 17 65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 16 85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 15 55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 14 56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 13 42.80.58.11.95.90.85.47.33.14.3.33.82 + 12 80.58.11.95.90.85.47.33.14.3.33.82 11 58.11.95.90.85.47.33.14.3.33.82 + 10 11.95.90.85.47.33.14.3.33.82 9 95.90.85.47.33.14.3.33.82 + 8 90.85.47.33.14.3.33.82 7 85.47.33.14.3.33.82 6 47.33.14.3.33.82 + 5 33.14.3.33.82 19 {} 19 {} 19 {} 19 {} 19 {} 18 {} 17 {} + 16 {} 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} + 6 {} 5 {}} + +do_execsql_test 1.20.2.1 { + SELECT max(b) OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2 +} {99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 + 99 99 95 99 99 99 99 95 95 84 84 84 84 84 84 + 84 84 83 83 83 83 82 83 83 83 83} + +do_execsql_test 1.20.2.2 { + SELECT min(b) OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 + 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 2 3 2 2 2 2 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 7 3 3 + 3 3 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 + 17} + +do_execsql_test 1.20.3.1 { + SELECT row_number() OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.20.3.2 { + SELECT row_number() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.20.3.3 { + SELECT row_number() OVER ( ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.20.4.1 { + SELECT dense_rank() OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.20.4.2 { + SELECT dense_rank() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.20.4.3 { + SELECT dense_rank() OVER ( ORDER BY b ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2 +} {1 1 2 2 3 3 4 5 6 7 7 7 8 8 8 9 9 9 10 + 11 11 12 12 12 13 13 14 15 15 15 16 16 16 17 + 18 19 20 20 21 21 22 22 23 24 25 25 26 26 27 + 28 28 28 29 29 29 30 30 31 32 32 32 32 33 33 + 33 33 34 34 35 35 35 35 36 36 37 37 38 38 38 + 39 40 40 41 42 42 43 43 44 44 45 45 45 45 46 + 47 48 49 50 51 52 52 53 53 53 54 55 55 55 55 + 56 56 56 56 57 58 58 59 59 60 61 62 62 62 63 + 64 65 66 67 68 68 68 69 69 69 70 70 70 71 71 + 71 72 73 73 74 74 75 76 76 77 77 77 78 79 80 + 80 80 80 81 81 81 82 83 83 84 85 85 85 86 86 + 86 87 87 87 87 87 88 88 88 89 90 90 90 91 91 + 91 92 92 93 93 94 94} + +do_execsql_test 1.20.4.4 { + SELECT dense_rank() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2 +} {1 2 3 3 3 4 5 6 7 8 8 9 9 9 1 1 2 2 3 + 3 4 4 5 5 6 7 7 8 8 8 9 9 9 9 9 1 1 2 + 2 2 3 3 4 5 6 7 7 8 8 8 9 1 1 2 2 3 3 + 4 4 4 4 5 5 6 7 8 8 8 9 10 10 10 1 2 3 + 4 4 4 4 5 5 6 7 8 8 8 9 9 9 9 10 1 2 2 + 2 3 4 4 5 5 6 6 6 7 7 7 8 8 8 9 9 9 1 + 2 2 2 3 3 4 4 4 4 5 5 6 6 6 7 8 9 10 10 + 10 1 1 1 2 3 3 4 4 5 5 5 5 6 7 8 8 9 9 + 10 10 1 1 1 2 3 3 4 4 4 4 5 6 6 7 8 8 1 + 1 1 2 3 3 3 4 4 4 5 6 6 6 6 7 8 9 9 9 + 10 10} + +do_execsql_test 1.20.4.5 { + SELECT dense_rank() OVER ( ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 + 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 + 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 + 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 + 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 + 8 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 10 10 + 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 + 10 10 10 10 10} + +do_execsql_test 1.20.4.6 { + SELECT dense_rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 + 5 5 5 5 5 5 5 5 5 5 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 + 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 + 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 + 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 + 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 + 5} + +do_execsql_test 1.20.5.1 { + SELECT rank() OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 + 93 94 95 96 97 98 99 100 101 102 103 104 105 106 + 107 108 109 110 111 112 113 114 115 116 117 118 119 + 120 121 122 123 124 125 126 127 128 129 130 131 132 + 133 134 135 136 137 138 139 140 141 142 143 144 145 + 146 147 148 149 150 151 152 153 154 155 156 157 158 + 159 160 161 162 163 164 165 166 167 168 169 170 171 + 172 173 174 175 176 177 178 179 180 181 182 183 184 + 185 186 187 188 189 190 191} + +do_execsql_test 1.20.5.2 { + SELECT rank() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 + 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 + 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 + 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 + 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 + 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 + 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22} + +do_execsql_test 1.20.5.3 { + SELECT rank() OVER ( ORDER BY b ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2 +} {1 1 3 3 5 5 7 8 9 10 10 10 13 13 13 16 16 + 16 19 20 20 22 22 22 25 25 27 28 28 28 31 31 + 31 34 35 36 37 37 39 39 41 41 43 44 45 45 47 + 47 49 50 50 50 53 53 53 56 56 58 59 59 59 59 + 63 63 63 63 67 67 69 69 69 69 73 73 75 75 77 + 77 77 80 81 81 83 84 84 86 86 88 88 90 90 90 + 90 94 95 96 97 98 99 100 100 102 102 102 105 106 + 106 106 106 110 110 110 110 114 115 115 117 117 119 + 120 121 121 121 124 125 126 127 128 129 129 129 132 + 132 132 135 135 135 138 138 138 141 142 142 144 144 + 146 147 147 149 149 149 152 153 154 154 154 154 158 + 158 158 161 162 162 164 165 165 165 168 168 168 171 + 171 171 171 171 176 176 176 179 180 180 180 183 183 + 183 186 186 188 188 190 190} + +do_execsql_test 1.20.5.4 { + SELECT rank() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2 +} {1 2 3 3 3 6 7 8 9 10 10 12 12 12 1 1 3 3 + 5 5 7 7 9 9 11 12 12 14 14 14 17 17 17 17 + 17 1 1 3 3 3 6 6 8 9 10 11 11 13 13 13 16 + 1 1 3 3 5 5 7 7 7 7 11 11 13 14 15 15 15 + 18 19 19 19 1 2 3 4 4 4 4 8 8 10 11 12 12 + 12 15 15 15 15 19 1 2 2 2 5 6 6 8 8 10 10 + 10 13 13 13 16 16 16 19 19 19 1 2 2 2 5 5 7 + 7 7 7 11 11 13 13 13 16 17 18 19 19 19 1 1 + 1 4 5 5 7 7 9 9 9 9 13 14 15 15 17 17 19 + 19 1 1 1 4 5 5 7 7 7 7 11 12 12 14 15 15 + 1 1 1 4 5 5 5 8 8 8 11 12 12 12 12 16 17 + 18 18 18 21 21} + +do_execsql_test 1.20.5.5 { + SELECT rank() OVER ( ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 15 15 15 15 + 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 + 15 15 36 36 36 36 36 36 36 36 36 36 36 36 36 + 36 36 36 52 52 52 52 52 52 52 52 52 52 52 52 + 52 52 52 52 52 52 52 52 52 73 73 73 73 73 73 + 73 73 73 73 73 73 73 73 73 73 73 73 73 92 92 + 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 + 92 92 92 92 113 113 113 113 113 113 113 113 113 + 113 113 113 113 113 113 113 113 113 113 113 113 134 + 134 134 134 134 134 134 134 134 134 134 134 134 134 + 134 134 134 134 134 134 154 154 154 154 154 154 154 + 154 154 154 154 154 154 154 154 154 170 170 170 170 + 170 170 170 170 170 170 170 170 170 170 170 170 170 + 170 170 170 170 170} + +do_execsql_test 1.20.5.6 { + SELECT rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2 +} {1 1 1 1 1 1 1 1 1 1 1 1 1 1 15 15 15 15 + 15 15 15 15 15 15 15 15 15 15 15 15 31 31 31 + 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 + 31 50 50 50 50 50 50 50 50 50 50 50 50 50 50 + 50 50 50 50 50 50 50 71 71 71 71 71 71 71 71 + 71 71 71 71 71 71 71 71 1 1 1 1 1 1 1 1 1 + 1 1 1 1 1 1 1 1 1 1 1 1 22 22 22 22 22 22 + 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 + 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 + 43 43 43 43 43 43 64 64 64 64 64 64 64 64 64 + 64 64 64 64 64 64 64 64 64 64 64 84 84 84 84 + 84 84 84 84 84 84 84 84 84 84 84 84 84 84 84 + 84 84 84} + +do_execsql_test 1.20.6.1 { + SELECT + row_number() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ), + rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ), + dense_rank() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) + FROM t2 +} {1 1 1 2 1 1 3 1 1 4 1 1 5 1 1 6 1 1 7 1 1 8 1 1 9 1 1 + 10 1 1 11 1 1 12 1 1 13 1 1 14 1 1 15 15 2 16 15 2 17 15 2 + 18 15 2 19 15 2 20 15 2 21 15 2 22 15 2 23 15 2 24 15 2 + 25 15 2 26 15 2 27 15 2 28 15 2 29 15 2 30 15 2 31 31 3 + 32 31 3 33 31 3 34 31 3 35 31 3 36 31 3 37 31 3 38 31 3 + 39 31 3 40 31 3 41 31 3 42 31 3 43 31 3 44 31 3 45 31 3 + 46 31 3 47 31 3 48 31 3 49 31 3 50 50 4 51 50 4 52 50 4 + 53 50 4 54 50 4 55 50 4 56 50 4 57 50 4 58 50 4 59 50 4 + 60 50 4 61 50 4 62 50 4 63 50 4 64 50 4 65 50 4 66 50 4 + 67 50 4 68 50 4 69 50 4 70 50 4 71 71 5 72 71 5 73 71 5 + 74 71 5 75 71 5 76 71 5 77 71 5 78 71 5 79 71 5 80 71 5 + 81 71 5 82 71 5 83 71 5 84 71 5 85 71 5 86 71 5 1 1 1 2 1 1 + 3 1 1 4 1 1 5 1 1 6 1 1 7 1 1 8 1 1 9 1 1 10 1 1 11 1 1 + 12 1 1 13 1 1 14 1 1 15 1 1 16 1 1 17 1 1 18 1 1 19 1 1 + 20 1 1 21 1 1 22 22 2 23 22 2 24 22 2 25 22 2 26 22 2 27 22 2 + 28 22 2 29 22 2 30 22 2 31 22 2 32 22 2 33 22 2 34 22 2 + 35 22 2 36 22 2 37 22 2 38 22 2 39 22 2 40 22 2 41 22 2 + 42 22 2 43 43 3 44 43 3 45 43 3 46 43 3 47 43 3 48 43 3 + 49 43 3 50 43 3 51 43 3 52 43 3 53 43 3 54 43 3 55 43 3 + 56 43 3 57 43 3 58 43 3 59 43 3 60 43 3 61 43 3 62 43 3 + 63 43 3 64 64 4 65 64 4 66 64 4 67 64 4 68 64 4 69 64 4 + 70 64 4 71 64 4 72 64 4 73 64 4 74 64 4 75 64 4 76 64 4 + 77 64 4 78 64 4 79 64 4 80 64 4 81 64 4 82 64 4 83 64 4 + 84 84 5 85 84 5 86 84 5 87 84 5 88 84 5 89 84 5 90 84 5 + 91 84 5 92 84 5 93 84 5 94 84 5 95 84 5 96 84 5 97 84 5 + 98 84 5 99 84 5 100 84 5 101 84 5 102 84 5 103 84 5 104 84 5 + 105 84 5} + + +do_test 1.20.7.1 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0053 0.0105 0.0158 0.0211 0.0263 0.0316 0.0368 0.0421 0.0474 0.0526 0.0579 0.0632 0.0684 0.0737 0.0789 0.0842 0.0895 0.0947 0.1000 0.1053 0.1105 0.1158 0.1211 0.1263 0.1316 0.1368 0.1421 0.1474 0.1526 0.1579 0.1632 0.1684 0.1737 0.1789 0.1842 0.1895 0.1947 0.2000 0.2053 0.2105 0.2158 0.2211 0.2263 0.2316 0.2368 0.2421 0.2474 0.2526 0.2579 0.2632 0.2684 0.2737 0.2789 0.2842 0.2895 0.2947 0.3000 0.3053 0.3105 0.3158 0.3211 0.3263 0.3316 0.3368 0.3421 0.3474 0.3526 0.3579 0.3632 0.3684 0.3737 0.3789 0.3842 0.3895 0.3947 0.4000 0.4053 0.4105 0.4158 0.4211 0.4263 0.4316 0.4368 0.4421 0.4474 0.4526 0.4579 0.4632 0.4684 0.4737 0.4789 0.4842 0.4895 0.4947 0.5000 0.5053 0.5105 0.5158 0.5211 0.5263 0.5316 0.5368 0.5421 0.5474 0.5526 0.5579 0.5632 0.5684 0.5737 0.5789 0.5842 0.5895 0.5947 0.6000 0.6053 0.6105 0.6158 0.6211 0.6263 0.6316 0.6368 0.6421 0.6474 0.6526 0.6579 0.6632 0.6684 0.6737 0.6789 0.6842 0.6895 0.6947 0.7000 0.7053 0.7105 0.7158 0.7211 0.7263 0.7316 0.7368 0.7421 0.7474 0.7526 0.7579 0.7632 0.7684 0.7737 0.7789 0.7842 0.7895 0.7947 0.8000 0.8053 0.8105 0.8158 0.8211 0.8263 0.8316 0.8368 0.8421 0.8474 0.8526 0.8579 0.8632 0.8684 0.8737 0.8789 0.8842 0.8895 0.8947 0.9000 0.9053 0.9105 0.9158 0.9211 0.9263 0.9316 0.9368 0.9421 0.9474 0.9526 0.9579 0.9632 0.9684 0.9737 0.9789 0.9842 0.9895 0.9947 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.20.7.2 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0769 0.1538 0.2308 0.3077 0.3846 0.4615 0.5385 0.6154 0.6923 0.7692 0.8462 0.9231 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0667 0.1333 0.2000 0.2667 0.3333 0.4000 0.4667 0.5333 0.6000 0.6667 0.7333 0.8000 0.8667 0.9333 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0556 0.1111 0.1667 0.2222 0.2778 0.3333 0.3889 0.4444 0.5000 0.5556 0.6111 0.6667 0.7222 0.7778 0.8333 0.8889 0.9444 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0000 0.0526 0.1053 0.1579 0.2105 0.2632 0.3158 0.3684 0.4211 0.4737 0.5263 0.5789 0.6316 0.6842 0.7368 0.7895 0.8421 0.8947 0.9474 1.0000 0.0000 0.0667 0.1333 0.2000 0.2667 0.3333 0.4000 0.4667 0.5333 0.6000 0.6667 0.7333 0.8000 0.8667 0.9333 1.0000 0.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.20.7.3 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY b ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0105 0.0105 0.0211 0.0211 0.0316 0.0368 0.0421 0.0474 0.0474 0.0474 0.0632 0.0632 0.0632 0.0789 0.0789 0.0789 0.0947 0.1000 0.1000 0.1105 0.1105 0.1105 0.1263 0.1263 0.1368 0.1421 0.1421 0.1421 0.1579 0.1579 0.1579 0.1737 0.1789 0.1842 0.1895 0.1895 0.2000 0.2000 0.2105 0.2105 0.2211 0.2263 0.2316 0.2316 0.2421 0.2421 0.2526 0.2579 0.2579 0.2579 0.2737 0.2737 0.2737 0.2895 0.2895 0.3000 0.3053 0.3053 0.3053 0.3053 0.3263 0.3263 0.3263 0.3263 0.3474 0.3474 0.3579 0.3579 0.3579 0.3579 0.3789 0.3789 0.3895 0.3895 0.4000 0.4000 0.4000 0.4158 0.4211 0.4211 0.4316 0.4368 0.4368 0.4474 0.4474 0.4579 0.4579 0.4684 0.4684 0.4684 0.4684 0.4895 0.4947 0.5000 0.5053 0.5105 0.5158 0.5211 0.5211 0.5316 0.5316 0.5316 0.5474 0.5526 0.5526 0.5526 0.5526 0.5737 0.5737 0.5737 0.5737 0.5947 0.6000 0.6000 0.6105 0.6105 0.6211 0.6263 0.6316 0.6316 0.6316 0.6474 0.6526 0.6579 0.6632 0.6684 0.6737 0.6737 0.6737 0.6895 0.6895 0.6895 0.7053 0.7053 0.7053 0.7211 0.7211 0.7211 0.7368 0.7421 0.7421 0.7526 0.7526 0.7632 0.7684 0.7684 0.7789 0.7789 0.7789 0.7947 0.8000 0.8053 0.8053 0.8053 0.8053 0.8263 0.8263 0.8263 0.8421 0.8474 0.8474 0.8579 0.8632 0.8632 0.8632 0.8789 0.8789 0.8789 0.8947 0.8947 0.8947 0.8947 0.8947 0.9211 0.9211 0.9211 0.9368 0.9421 0.9421 0.9421 0.9579 0.9579 0.9579 0.9737 0.9737 0.9842 0.9842 0.9947 0.9947} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.20.7.4 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0769 0.1538 0.1538 0.1538 0.3846 0.4615 0.5385 0.6154 0.6923 0.6923 0.8462 0.8462 0.8462 0.0000 0.0000 0.1000 0.1000 0.2000 0.2000 0.3000 0.3000 0.4000 0.4000 0.5000 0.5500 0.5500 0.6500 0.6500 0.6500 0.8000 0.8000 0.8000 0.8000 0.8000 0.0000 0.0000 0.1333 0.1333 0.1333 0.3333 0.3333 0.4667 0.5333 0.6000 0.6667 0.6667 0.8000 0.8000 0.8000 1.0000 0.0000 0.0000 0.1000 0.1000 0.2000 0.2000 0.3000 0.3000 0.3000 0.3000 0.5000 0.5000 0.6000 0.6500 0.7000 0.7000 0.7000 0.8500 0.9000 0.9000 0.9000 0.0000 0.0556 0.1111 0.1667 0.1667 0.1667 0.1667 0.3889 0.3889 0.5000 0.5556 0.6111 0.6111 0.6111 0.7778 0.7778 0.7778 0.7778 1.0000 0.0000 0.0500 0.0500 0.0500 0.2000 0.2500 0.2500 0.3500 0.3500 0.4500 0.4500 0.4500 0.6000 0.6000 0.6000 0.7500 0.7500 0.7500 0.9000 0.9000 0.9000 0.0000 0.0500 0.0500 0.0500 0.2000 0.2000 0.3000 0.3000 0.3000 0.3000 0.5000 0.5000 0.6000 0.6000 0.6000 0.7500 0.8000 0.8500 0.9000 0.9000 0.9000 0.0000 0.0000 0.0000 0.1579 0.2105 0.2105 0.3158 0.3158 0.4211 0.4211 0.4211 0.4211 0.6316 0.6842 0.7368 0.7368 0.8421 0.8421 0.9474 0.9474 0.0000 0.0000 0.0000 0.2000 0.2667 0.2667 0.4000 0.4000 0.4000 0.4000 0.6667 0.7333 0.7333 0.8667 0.9333 0.9333 0.0000 0.0000 0.0000 0.1429 0.1905 0.1905 0.1905 0.3333 0.3333 0.3333 0.4762 0.5238 0.5238 0.5238 0.5238 0.7143 0.7619 0.8095 0.8095 0.8095 0.9524 0.9524} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.20.7.5 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER ( ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.0737 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.1842 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.2684 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.3789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.4789 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.5895 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.7000 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8053 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895 0.8895} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.20.7.6 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER (PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.1647 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.3529 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.5765 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.8235 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.2019 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.4038 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.6058 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981 0.7981} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.20.8.1 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0052 0.0105 0.0157 0.0209 0.0262 0.0314 0.0366 0.0419 0.0471 0.0524 0.0576 0.0628 0.0681 0.0733 0.0785 0.0838 0.0890 0.0942 0.0995 0.1047 0.1099 0.1152 0.1204 0.1257 0.1309 0.1361 0.1414 0.1466 0.1518 0.1571 0.1623 0.1675 0.1728 0.1780 0.1832 0.1885 0.1937 0.1990 0.2042 0.2094 0.2147 0.2199 0.2251 0.2304 0.2356 0.2408 0.2461 0.2513 0.2565 0.2618 0.2670 0.2723 0.2775 0.2827 0.2880 0.2932 0.2984 0.3037 0.3089 0.3141 0.3194 0.3246 0.3298 0.3351 0.3403 0.3455 0.3508 0.3560 0.3613 0.3665 0.3717 0.3770 0.3822 0.3874 0.3927 0.3979 0.4031 0.4084 0.4136 0.4188 0.4241 0.4293 0.4346 0.4398 0.4450 0.4503 0.4555 0.4607 0.4660 0.4712 0.4764 0.4817 0.4869 0.4921 0.4974 0.5026 0.5079 0.5131 0.5183 0.5236 0.5288 0.5340 0.5393 0.5445 0.5497 0.5550 0.5602 0.5654 0.5707 0.5759 0.5812 0.5864 0.5916 0.5969 0.6021 0.6073 0.6126 0.6178 0.6230 0.6283 0.6335 0.6387 0.6440 0.6492 0.6545 0.6597 0.6649 0.6702 0.6754 0.6806 0.6859 0.6911 0.6963 0.7016 0.7068 0.7120 0.7173 0.7225 0.7277 0.7330 0.7382 0.7435 0.7487 0.7539 0.7592 0.7644 0.7696 0.7749 0.7801 0.7853 0.7906 0.7958 0.8010 0.8063 0.8115 0.8168 0.8220 0.8272 0.8325 0.8377 0.8429 0.8482 0.8534 0.8586 0.8639 0.8691 0.8743 0.8796 0.8848 0.8901 0.8953 0.9005 0.9058 0.9110 0.9162 0.9215 0.9267 0.9319 0.9372 0.9424 0.9476 0.9529 0.9581 0.9634 0.9686 0.9738 0.9791 0.9843 0.9895 0.9948 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.20.8.2 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0714 0.1429 0.2143 0.2857 0.3571 0.4286 0.5000 0.5714 0.6429 0.7143 0.7857 0.8571 0.9286 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0625 0.1250 0.1875 0.2500 0.3125 0.3750 0.4375 0.5000 0.5625 0.6250 0.6875 0.7500 0.8125 0.8750 0.9375 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0526 0.1053 0.1579 0.2105 0.2632 0.3158 0.3684 0.4211 0.4737 0.5263 0.5789 0.6316 0.6842 0.7368 0.7895 0.8421 0.8947 0.9474 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0476 0.0952 0.1429 0.1905 0.2381 0.2857 0.3333 0.3810 0.4286 0.4762 0.5238 0.5714 0.6190 0.6667 0.7143 0.7619 0.8095 0.8571 0.9048 0.9524 1.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 0.0625 0.1250 0.1875 0.2500 0.3125 0.3750 0.4375 0.5000 0.5625 0.6250 0.6875 0.7500 0.8125 0.8750 0.9375 1.0000 0.0455 0.0909 0.1364 0.1818 0.2273 0.2727 0.3182 0.3636 0.4091 0.4545 0.5000 0.5455 0.5909 0.6364 0.6818 0.7273 0.7727 0.8182 0.8636 0.9091 0.9545 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.20.8.3 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY b ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0105 0.0105 0.0209 0.0209 0.0314 0.0314 0.0366 0.0419 0.0471 0.0628 0.0628 0.0628 0.0785 0.0785 0.0785 0.0942 0.0942 0.0942 0.0995 0.1099 0.1099 0.1257 0.1257 0.1257 0.1361 0.1361 0.1414 0.1571 0.1571 0.1571 0.1728 0.1728 0.1728 0.1780 0.1832 0.1885 0.1990 0.1990 0.2094 0.2094 0.2199 0.2199 0.2251 0.2304 0.2408 0.2408 0.2513 0.2513 0.2565 0.2723 0.2723 0.2723 0.2880 0.2880 0.2880 0.2984 0.2984 0.3037 0.3246 0.3246 0.3246 0.3246 0.3455 0.3455 0.3455 0.3455 0.3560 0.3560 0.3770 0.3770 0.3770 0.3770 0.3874 0.3874 0.3979 0.3979 0.4136 0.4136 0.4136 0.4188 0.4293 0.4293 0.4346 0.4450 0.4450 0.4555 0.4555 0.4660 0.4660 0.4869 0.4869 0.4869 0.4869 0.4921 0.4974 0.5026 0.5079 0.5131 0.5183 0.5288 0.5288 0.5445 0.5445 0.5445 0.5497 0.5707 0.5707 0.5707 0.5707 0.5916 0.5916 0.5916 0.5916 0.5969 0.6073 0.6073 0.6178 0.6178 0.6230 0.6283 0.6440 0.6440 0.6440 0.6492 0.6545 0.6597 0.6649 0.6702 0.6859 0.6859 0.6859 0.7016 0.7016 0.7016 0.7173 0.7173 0.7173 0.7330 0.7330 0.7330 0.7382 0.7487 0.7487 0.7592 0.7592 0.7644 0.7749 0.7749 0.7906 0.7906 0.7906 0.7958 0.8010 0.8220 0.8220 0.8220 0.8220 0.8377 0.8377 0.8377 0.8429 0.8534 0.8534 0.8586 0.8743 0.8743 0.8743 0.8901 0.8901 0.8901 0.9162 0.9162 0.9162 0.9162 0.9162 0.9319 0.9319 0.9319 0.9372 0.9529 0.9529 0.9529 0.9686 0.9686 0.9686 0.9791 0.9791 0.9895 0.9895 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.20.8.4 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%10 ORDER BY b ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0714 0.1429 0.3571 0.3571 0.3571 0.4286 0.5000 0.5714 0.6429 0.7857 0.7857 1.0000 1.0000 1.0000 0.0952 0.0952 0.1905 0.1905 0.2857 0.2857 0.3810 0.3810 0.4762 0.4762 0.5238 0.6190 0.6190 0.7619 0.7619 0.7619 1.0000 1.0000 1.0000 1.0000 1.0000 0.1250 0.1250 0.3125 0.3125 0.3125 0.4375 0.4375 0.5000 0.5625 0.6250 0.7500 0.7500 0.9375 0.9375 0.9375 1.0000 0.0952 0.0952 0.1905 0.1905 0.2857 0.2857 0.4762 0.4762 0.4762 0.4762 0.5714 0.5714 0.6190 0.6667 0.8095 0.8095 0.8095 0.8571 1.0000 1.0000 1.0000 0.0526 0.1053 0.1579 0.3684 0.3684 0.3684 0.3684 0.4737 0.4737 0.5263 0.5789 0.7368 0.7368 0.7368 0.9474 0.9474 0.9474 0.9474 1.0000 0.0476 0.1905 0.1905 0.1905 0.2381 0.3333 0.3333 0.4286 0.4286 0.5714 0.5714 0.5714 0.7143 0.7143 0.7143 0.8571 0.8571 0.8571 1.0000 1.0000 1.0000 0.0476 0.1905 0.1905 0.1905 0.2857 0.2857 0.4762 0.4762 0.4762 0.4762 0.5714 0.5714 0.7143 0.7143 0.7143 0.7619 0.8095 0.8571 1.0000 1.0000 1.0000 0.1500 0.1500 0.1500 0.2000 0.3000 0.3000 0.4000 0.4000 0.6000 0.6000 0.6000 0.6000 0.6500 0.7000 0.8000 0.8000 0.9000 0.9000 1.0000 1.0000 0.1875 0.1875 0.1875 0.2500 0.3750 0.3750 0.6250 0.6250 0.6250 0.6250 0.6875 0.8125 0.8125 0.8750 1.0000 1.0000 0.1364 0.1364 0.1364 0.1818 0.3182 0.3182 0.3182 0.4545 0.4545 0.4545 0.5000 0.6818 0.6818 0.6818 0.6818 0.7273 0.7727 0.9091 0.9091 0.9091 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.20.8.5 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.0733 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.1832 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.2670 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.3770 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.4764 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.5864 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.6963 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8010 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 0.8848 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.20.8.6 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER ( PARTITION BY b%2 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.1628 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.3488 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.5698 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 0.8140 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.2000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.6000 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 0.7905 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.20.8.1 { + set myres {} + foreach r [db eval {SELECT ntile(100) OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 88.0000 89.0000 89.0000 90.0000 90.0000 91.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.20.8.2 { + set myres {} + foreach r [db eval {SELECT ntile(101) OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 22.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.20.8.3 { + set myres {} + foreach r [db eval {SELECT ntile(102) OVER ( ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 88.0000 89.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.20.8.4 { + set myres {} + foreach r [db eval {SELECT ntile(103) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 22.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.20.8.5 { + set myres {} + foreach r [db eval {SELECT ntile(104) OVER ( ORDER BY b%10,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 87.0000 88.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000 103.0000 104.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.20.8.6 { + set myres {} + foreach r [db eval {SELECT ntile(105) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 1.20.8.7 { + set myres {} + foreach r [db eval {SELECT ntile(105) OVER ( ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 2.0000 2.0000 3.0000 3.0000 4.0000 4.0000 5.0000 5.0000 6.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 10.0000 11.0000 11.0000 12.0000 12.0000 13.0000 13.0000 14.0000 14.0000 15.0000 15.0000 16.0000 16.0000 17.0000 17.0000 18.0000 18.0000 19.0000 19.0000 20.0000 20.0000 21.0000 21.0000 22.0000 22.0000 23.0000 23.0000 24.0000 24.0000 25.0000 25.0000 26.0000 26.0000 27.0000 27.0000 28.0000 28.0000 29.0000 29.0000 30.0000 30.0000 31.0000 31.0000 32.0000 32.0000 33.0000 33.0000 34.0000 34.0000 35.0000 35.0000 36.0000 36.0000 37.0000 37.0000 38.0000 38.0000 39.0000 39.0000 40.0000 40.0000 41.0000 41.0000 42.0000 42.0000 43.0000 43.0000 44.0000 44.0000 45.0000 45.0000 46.0000 46.0000 47.0000 47.0000 48.0000 48.0000 49.0000 49.0000 50.0000 50.0000 51.0000 51.0000 52.0000 52.0000 53.0000 53.0000 54.0000 54.0000 55.0000 55.0000 56.0000 56.0000 57.0000 57.0000 58.0000 58.0000 59.0000 59.0000 60.0000 60.0000 61.0000 61.0000 62.0000 62.0000 63.0000 63.0000 64.0000 64.0000 65.0000 65.0000 66.0000 66.0000 67.0000 67.0000 68.0000 68.0000 69.0000 69.0000 70.0000 70.0000 71.0000 71.0000 72.0000 72.0000 73.0000 73.0000 74.0000 74.0000 75.0000 75.0000 76.0000 76.0000 77.0000 77.0000 78.0000 78.0000 79.0000 79.0000 80.0000 80.0000 81.0000 81.0000 82.0000 82.0000 83.0000 83.0000 84.0000 84.0000 85.0000 85.0000 86.0000 86.0000 87.0000 88.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000 103.0000 104.0000 105.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + +do_execsql_test 1.20.9.1 { + SELECT last_value(a+b) OVER ( ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2 +} {207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 216} + +do_execsql_test 1.20.9.2 { + SELECT last_value(a+b) OVER ( PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2 +} {223 223 223 223 223 223 223 223 223 223 223 223 223 + 196 210 210 210 210 210 210 210 210 210 210 210 210 + 210 210 210 210 210 210 210 210 263 280 280 280 280 + 280 280 280 280 280 280 280 280 280 280 280 234 279 + 279 279 279 279 279 279 279 279 279 279 279 279 279 + 279 279 279 279 279 279 221 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 235 + 274 274 274 274 274 274 274 274 274 274 274 274 274 + 274 274 274 274 274 274 274 189 212 212 212 212 212 + 212 212 212 212 212 212 212 212 212 212 212 212 212 + 212 212 207 207 207 207 207 207 207 207 207 207 207 + 207 207 207 207 207 207 207 207 207 216 232 232 232 + 232 232 232 232 232 232 232 232 232 232 232 232 250 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 249} + +do_execsql_test 1.20.9.3 { + SELECT last_value(a+b) OVER ( ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2 +} {276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 120} + +do_execsql_test 1.20.9.4 { + SELECT last_value(a+b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2 +} {240 240 240 240 240 240 240 240 240 240 240 240 240 + 218 263 263 263 263 263 263 263 263 263 263 263 263 + 263 263 263 263 263 263 263 263 261 280 280 280 280 + 280 280 280 280 280 280 280 280 280 280 280 228 252 + 252 252 252 252 252 252 252 252 252 252 252 252 252 + 252 252 252 252 252 252 130 171 171 171 171 171 171 + 171 171 171 171 171 171 171 171 171 171 171 171 271 + 274 274 274 274 274 274 274 274 274 274 274 274 274 + 274 274 274 274 274 274 274 213 226 226 226 226 226 + 226 226 226 226 226 226 226 226 226 226 226 226 226 + 226 226 195 124 124 124 124 124 124 124 124 124 124 + 124 124 124 124 124 124 124 124 124 119 198 198 198 + 198 198 198 198 198 198 198 198 198 198 198 198 171 + 276 276 276 276 276 276 276 276 276 276 276 276 276 + 276 276 276 276 276 276 276 276 120} + +do_execsql_test 1.20.9.5 { + SELECT last_value(a+b) OVER ( ORDER BY b%10,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2 +} {229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 229 229 229 229 229 + 229 229 229 229 229 229 229 229 249} + +do_execsql_test 1.20.9.6 { + SELECT last_value(a+b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.20.10.1 { + SELECT nth_value(b,b+1) OVER (ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP) FROM t2 +} {98 9 73 75 43 78 32 4 5 25 27 15 31 24 43 98 + 94 79 77 61 22 79 8 73 3 85 43 34 98 36 63 91 + 11 20 21 75 90 56 96 84 5 28 33 5 56 59 81 19 + 87 36 2 26 63 73 89 95 74 73 79 29 70 65 33 + 14 26 47 94 14 15 5 72 41 11 75 19 53 91 79 + 37 91 93 59 58 36 23 84 33 73 47 58 34 58 73 + 15 13 7 11 99 31 66 38 80 79 95 60 59 19 59 + {} 85 39 27 {} 9 59 75 91 33 {} 84 {} 33 50 + {} {} 36 77 29 {} 30 {} 12 {} 17 75 58 62 91 + 58 {} {} 50 {} 27 75 {} {} {} 33 {} {} {} {} + 62 50 17 36 36 95 {} {} {} {} {} 10 {} {} {} + 99 {} {} {} {} {} {} {} 30 {} {} {} {} {} {} + {} {} {} {} {} {} {} {}} + +do_execsql_test 1.20.10.2 { + SELECT nth_value(b,b+1) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 61 {} {} {} 81 {} 91 {} {} {} {} + {} {} {} {} {} {} 62 {} {} {} 22 {} {} {} 12 + {} {} {} {} {} {} {} {} {} {} 43 {} {} {} {} + {} 83 {} {} {} {} {} {} {} 33 {} {} {} {} {} + {} 94 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} 85 {} {} {} {} {} {} 95 {} + {} {} {} {} {} {} {} {} {} 56 {} 36 66 {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 87 {} {} {} {} {} 37 {} {} {} {} + {} {} {} {} {} {} 8 {} {} {} {} {} 58 {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 19 {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.20.10.3 { + SELECT nth_value(b,b+1) OVER ( ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2 +} {2 2 2 2 2 3 5 7 7 8 8 9 9 10 11 12 12 12 + 13 15 15 16 16 16 19 20 21 22 23 23 25 26 26 + 27 29 30 30 31 32 33 33 33 34 34 35 36 36 36 + 37 38 39 39 40 41 41 43 43 44 46 47 47 47 49 + 50 51 52 54 55 56 56 56 57 58 58 59 59 59 60 + 61 62 63 64 65 66 67 69 70 72 73 73 74 74 74 + 75 77 78 79 80 81 82 83 84 84 84 85 86 87 87 + 88 89 89 90 90 91 91 91 93 93 94 95 96 96 96 + 97 98 99 {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {}} + +do_execsql_test 1.20.10.4 { + SELECT nth_value(b,b+1) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2 +} {90 {} {} {} {} {} {} {} {} {} {} {} {} {} 11 + 11 61 61 {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} 12 12 72 72 72 {} {} {} {} {} + {} {} {} {} {} {} 23 23 73 73 {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} 34 84 {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 35 85 85 85 {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} 36 86 86 86 {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 47 47 + 47 97 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} 58 58 58 {} {} {} {} {} {} {} {} {} + {} {} {} {} 49 49 49 99 {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.20.10.5 { + SELECT nth_value(b,b+1) OVER ( ORDER BY b%10,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2 +} {65 72 81 34 41 35 73 74 14 15 32 53 1 2 35 5 + 74 95 55 61 75 63 34 81 12 91 74 93 43 33 22 + 75 56 56 23 85 72 23 33 84 22 15 93 73 12 6 + 46 43 75 75 36 87 54 87 85 43 15 84 85 56 66 + 83 36 97 85 78 34 94 75 33 65 17 36 68 7 15 + 94 38 78 27 95 96 36 68 7 15 16 38 89 66 56 + 38 76 89 89 47 85 56 59 {} 8 88 27 95 16 {} + 98 79 39 59 16 {} {} 8 16 98 36 66 {} 99 29 + 59 97 8 {} {} {} 27 17 8 {} 8 58 {} 98 {} {} + {} 29 87 19 {} 89 {} {} 37 {} {} {} 59 59 58 + 8 99 {} {} 98 29 {} {} {} {} 58 {} 89 {} {} + {} {} {} {} {} {} {} {} {} {} {} 19 {} {} {} + {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.20.10.6 { + SELECT nth_value(b,b+1) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.20.11.1 { + SELECT first_value(b) OVER (ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP) FROM t2 +} {81 89 89 89 89 81 96 59 38 68 39 62 91 46 6 + 99 97 27 46 78 54 97 8 67 29 93 84 77 23 16 + 16 93 65 35 47 7 86 74 61 91 85 24 85 43 59 + 12 32 56 3 91 22 90 55 15 28 89 25 47 1 56 40 + 43 56 16 75 36 89 98 76 81 4 94 42 30 78 33 + 29 53 63 2 87 37 80 84 72 41 9 61 73 95 65 13 + 58 96 98 1 21 74 65 35 5 73 11 51 87 41 12 8 + 20 31 31 15 95 22 73 79 88 34 8 11 49 34 90 + 59 96 60 55 75 77 44 2 7 85 57 74 29 70 59 19 + 39 26 26 47 80 90 36 58 47 9 72 72 66 33 93 + 75 64 81 9 23 37 13 12 14 62 91 36 91 33 15 + 34 36 99 3 95 69 58 52 30 50 84 10 84 33 21 + 39 44 58 30 38 34 83} + +do_execsql_test 1.20.11.2 { + SELECT first_value(b) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP) FROM t2 +} {40 90 90 90 90 40 30 80 20 90 60 70 80 90 91 + 81 81 81 81 91 61 91 91 1 81 41 61 1 21 11 51 + 41 31 31 11 12 62 62 62 62 12 32 22 42 2 72 + 12 22 2 72 72 23 93 93 93 93 23 93 43 3 43 33 + 53 63 73 13 73 73 33 93 23 13 84 54 54 54 54 + 84 74 24 4 94 84 74 34 34 44 74 64 14 34 35 + 65 65 65 65 35 85 85 55 15 25 75 95 65 65 35 + 5 15 95 55 75 46 96 96 96 96 46 6 46 16 16 86 + 56 56 56 16 36 76 96 96 26 26 27 97 97 97 97 + 27 97 67 77 47 7 47 87 37 87 77 7 57 47 47 68 + 38 38 38 38 68 78 8 28 98 78 58 98 8 88 8 59 + 89 89 89 89 59 39 99 29 59 89 89 29 9 79 49 + 59 29 59 19 39 9} + +do_execsql_test 1.20.11.3 { + SELECT first_value(b) OVER ( ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2 +} {1 1 1 1 1 1 2 2 3 3 4 5 6 7 7 7 8 8 8 + 9 9 9 10 11 11 12 12 12 13 13 14 15 15 15 16 + 16 16 17 19 20 21 21 22 22 23 23 24 25 26 26 + 27 27 28 29 29 29 30 30 30 31 31 32 33 33 33 + 33 34 34 34 34 35 35 36 36 36 36 37 37 38 38 + 39 39 39 40 41 41 42 43 43 44 44 46 46 47 47 + 47 47 49 50 51 52 53 54 55 55 56 56 56 57 58 + 58 58 58 59 59 59 59 60 61 61 62 62 63 64 65 + 65 65 66 67 68 69 70 72 72 72 73 73 73 74 74 + 74 75 75 75 76 77 77 78 78 79 80 80 81 81 81 + 82 83 84 84 84 84 85 85 85 86 87 87 88 89 89 + 89 90 90 90 91 91 91 91 91 93 93 93 94 95 95 + 95 96 96 96 97 97} + +do_execsql_test 1.20.11.4 { + SELECT first_value(b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2 +} {20 10 10 10 10 20 30 30 30 40 50 60 70 80 1 1 + 1 1 1 1 11 11 21 21 31 31 41 41 51 61 61 81 + 81 81 91 2 2 2 2 2 2 12 12 12 22 22 32 42 + 52 62 62 3 3 3 3 3 3 13 13 23 23 33 33 33 + 33 43 43 53 63 73 73 73 14 4 4 4 4 14 24 34 + 34 34 34 44 44 54 64 74 74 74 84 15 5 5 5 5 + 15 15 15 25 35 35 55 55 65 65 65 75 75 75 85 + 85 16 6 6 6 6 16 16 16 26 26 36 36 36 36 46 + 46 56 56 56 66 76 7 7 7 7 7 7 7 17 27 27 37 + 37 47 47 47 47 57 67 77 77 8 8 8 8 8 8 8 28 + 38 38 58 58 58 58 68 78 9 9 9 9 9 9 9 19 29 + 29 29 39 39 39 49 59 59 59 59 69 79 89} + +do_execsql_test 1.20.11.5 { + SELECT first_value(b) OVER ( ORDER BY b%10,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2 +} {40 90 90 90 90 40 30 80 20 90 60 70 80 90 30 + 50 10 30 81 91 61 91 91 1 81 41 61 1 21 11 51 + 41 31 31 11 81 91 91 21 62 12 32 22 42 2 72 + 12 22 2 72 72 12 62 52 82 93 23 93 43 3 43 33 + 53 63 73 13 73 73 33 93 23 13 33 3 33 83 54 + 84 74 24 4 94 84 74 34 34 44 74 64 14 34 84 + 84 44 34 65 35 85 85 55 15 25 75 95 65 65 35 + 5 15 95 55 75 85 75 15 95 96 46 6 46 16 16 86 + 56 56 56 16 36 76 96 96 26 26 36 66 36 36 97 + 27 97 67 77 47 7 47 87 37 87 77 7 57 47 47 37 + 27 17 7 38 68 78 8 28 98 78 58 98 8 88 8 58 + 58 58 38 89 59 39 99 29 59 89 89 29 9 79 49 + 59 29 59 19 39 9} + +do_execsql_test 1.20.11.6 { + SELECT first_value(b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.20.12.1 { + SELECT lead(b,b) OVER (ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP) FROM t2 +} {96 9 11 89 32 53 91 30 51 56 54 73 22 59 75 + 74 78 8 16 65 15 8 31 87 90 12 32 96 74 76 37 + 85 90 15 35 2 60 36 75 9 51 47 63 51 90 26 42 + 26 8 76 80 90 37 87 56 79 5 87 8 2 39 73 64 + 36 90 72 78 36 73 51 33 20 41 2 26 37 33 8 14 + 33 81 55 1 9 12 39 64 87 72 34 82 21 34 99 62 + 74 41 69 22 75 27 58 8 79 77 26 26 55 {} 29 + 30 7 {} 66 55 2 34 64 {} 33 {} 44 84 {} {} 95 + 85 19 {} 83 {} 91 {} {} 9 50 91 33 34 {} {} + 84 {} 7 9 {} {} {} 44 {} {} {} {} 91 84 {} 95 + 95 52 {} {} {} {} {} 21 {} {} {} 58 {} {} {} + {} {} {} {} 83 {} {} {} {} {} {} {} {} {} {} + {} {} {} {}} + +do_execsql_test 1.20.12.2 { + SELECT lead(b,b) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 81 {} {} {} 21 {} {} {} {} {} {} + {} {} {} {} {} {} 62 {} {} {} 12 {} {} {} 72 + {} {} {} {} {} {} {} {} {} {} 53 {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 34 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} 95 {} {} {} {} {} {} 85 {} + {} {} {} {} {} {} {} {} {} 56 {} 36 {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} 57 {} {} {} {} {} 7 {} {} {} {} + {} {} {} {} {} {} 8 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 9 {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.20.12.3 { + SELECT lead(b,b) OVER ( ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2 +} {1 2 3 3 5 6 7 8 8 9 9 10 11 12 12 13 13 + 14 15 16 16 17 19 20 21 22 23 24 25 26 27 27 + 28 29 30 31 32 33 33 33 34 34 35 36 36 36 37 + 38 39 39 40 41 42 43 43 44 46 47 47 47 49 50 + 52 53 54 55 56 56 57 58 58 58 59 59 59 60 61 + 62 62 64 65 65 67 69 70 72 72 73 74 74 75 75 + 75 77 78 80 81 81 83 84 84 85 85 85 87 88 89 + 89 89 90 90 91 91 91 93 93 94 95 95 96 97 97 + 98 99 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.20.12.4 { + SELECT lead(b,b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2 +} {80 {} {} {} {} {} {} {} {} {} {} {} {} {} 1 + 11 81 81 {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} 12 12 72 82 {} {} {} {} {} {} + {} {} {} {} {} {} 13 23 73 73 {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} 34 84 {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} 35 85 85 95 {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} 36 86 96 96 {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 37 47 + 47 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} 58 58 68 {} {} {} {} {} {} {} {} {} + {} {} {} {} 39 49 59 {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.20.12.5 { + SELECT lead(b,b) OVER ( ORDER BY b%10,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2 +} {34 72 91 74 21 55 23 94 84 95 2 13 61 22 55 + 55 94 85 75 81 65 73 74 21 72 12 94 33 63 73 + 72 96 36 76 3 25 62 3 73 34 12 46 43 93 72 16 + 86 63 15 65 36 77 24 57 25 53 95 34 95 16 97 + 74 97 67 25 98 44 34 65 54 5 68 96 28 47 95 + 34 39 8 38 6 46 96 28 47 95 56 39 99 97 76 8 + 26 9 79 27 95 16 29 {} 58 58 77 85 56 {} 98 + 29 {} 19 96 {} {} 78 56 98 36 97 {} 89 89 29 + 47 78 {} {} {} 38 68 58 {} 58 38 {} 98 {} {} + {} 39 57 9 {} 79 {} {} 7 {} {} {} 9 29 38 78 + {} {} {} 8 39 {} {} {} {} 59 {} 99 {} {} {} + {} {} {} {} {} {} {} {} {} {} 9 {} {} {} {} + {} {} {} {} {} {} {} {}} + +do_execsql_test 1.20.12.6 { + SELECT lead(b,b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.20.13.1 { + SELECT lag(b,b) OVER (ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} 38 {} {} {} {} + {} {} {} 6 {} {} {} {} {} 81 46 6 {} {} {} {} + 23 {} {} {} {} {} 27 {} {} {} 35 6 {} 12 {} + 23 {} {} 61 84 {} 93 39 47 {} 54 46 96 56 {} + 16 {} {} {} {} 89 {} 16 43 {} 85 56 29 99 53 + {} 59 {} {} 91 59 53 84 99 {} 93 63 47 {} {} + 98 33 67 35 75 1 23 13 55 27 75 98 35 73 63 2 + 21 27 13 24 86 23 84 31 20 94 61 65 75 23 36 + 94 55 90 41 77 96 56 29 40 12 89 63 11 5 73 + 79 1 16 28 31 73 5 39 53 63 41 11 40 2 13 33 + 9 29 90 47 72 9 73 30 44 33 74 93 29 74 42 34 + 63 41 34 96 47 77 1 36 74 72 14 36 26 77 9 72 + 64 8 91 31 52 30} + +do_execsql_test 1.20.13.2 { + SELECT lag(b,b) OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} 30 {} {} + {} {} {} {} 91 {} {} {} 61 {} 81 {} {} {} {} + 1 {} {} {} {} {} {} {} {} {} 22 {} {} {} 12 + {} {} 62 {} {} {} {} {} {} {} 23 {} {} {} {} + {} {} {} {} {} {} {} 43 {} 23 {} {} {} {} {} + {} 54 {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} 75 {} + {} {} {} {} {} 55 {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} 47 {} {} {} {} + {} 27 7 {} {} {} {} {} {} {} {} {} 68 {} 8 {} + {} {} {} {} {} {} {} {} {} {} {} {} 89 {} {} + {} {} {} {} {} 29 9 {} {} {}} + +do_execsql_test 1.20.13.3 { + SELECT lag(b,b) OVER ( ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2 +} {{} 1 1 1 1 2 2 2 2 2 2 3 3 3 4 4 5 6 6 + 6 7 7 7 7 7 8 8 8 8 8 8 9 9 9 9 9 9 9 + 9 9 9 10 10 10 10 11 11 11 11 11 12 12 12 12 + 13 13 13 13 13 14 15 15 15 15 16 16 16 16 16 + 17 19 20 20 21 21 21 21 22 22 22 22 23 23 23 + 23 23 24 23 24 24 25 26 26 26 26 26 26 26 26 + 26 26 26 27 27 27 27 28 29 29 29 29 30 30 30 + 30 30 30 31 31 31 31 31 32 32 32 32 32 32 31 + 32 33 33 33 33 33 33 34 34 34 34 34 34 34 34 + 35 35 35 35 35 36 36 36 36 36 36 36 37 37 37 + 38 38 38 38 38 38 39 39 39 39 40 40 41 41 42 + 43 42 43 43 43 43 44 44 44 46 46 46 47 47 47 + 47 47} + +do_execsql_test 1.20.13.4 { + SELECT lag(b,b) OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + 1 {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.20.13.5 { + SELECT lag(b,b) OVER ( ORDER BY b%10,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} 30 {} {} + {} {} {} {} 91 {} {} {} 61 80 81 {} {} {} {} + 1 {} {} {} 30 {} 21 90 61 {} 22 {} 11 41 12 + {} {} 62 {} {} {} {} 31 {} 50 23 30 21 90 {} + {} 62 {} {} 81 {} 22 43 62 23 32 {} 91 {} 90 + 93 54 {} {} 90 72 12 22 90 81 83 23 80 20 72 + 43 51 33 80 90 2 34 54 1 20 62 12 13 75 44 30 + 93 91 1 21 55 61 61 13 85 3 65 65 91 73 33 93 + 55 84 62 31 11 65 35 85 33 55 15 12 75 22 3 + 73 65 36 85 43 95 43 13 47 44 65 65 96 36 27 + 7 46 34 94 47 36 73 34 35 73 68 24 8 75 85 75 + 66 34 95 36 84 77 46 34 84 47 89 65 36 16 38 + 76 58 57 29 9 44 56 17} + +do_execsql_test 1.20.13.6 { + SELECT lag(b,b) OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.20.14.1 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP) FROM t2 +} {81.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.96.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.59.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.38.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.81.96.59.68.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 81.96.59.38.39.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 96.59.38.68.62.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 59.38.68.39.91.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 38.68.39.62.46.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 68.39.62.91.6.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 39.62.91.46.99.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 62.91.46.6.97.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 91.46.6.99.27.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 46.6.99.97.46.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 6.99.97.27.78.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 99.97.27.46.54.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 97.27.46.78.97.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 27.46.78.54.8.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 46.78.54.97.67.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 78.54.97.8.29.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 54.97.8.67.93.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 97.8.67.29.84.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 8.67.29.93.77.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 67.29.93.84.23.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 29.93.84.77.16.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 93.84.77.23.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 84.77.23.16.93.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 77.23.16.16.65.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 23.16.16.93.35.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 16.16.93.65.47.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 16.93.65.35.7.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 93.65.35.47.86.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 65.35.47.7.74.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 35.47.7.86.61.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 47.7.86.74.91.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 7.86.74.61.85.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 86.74.61.91.24.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 74.61.91.85.85.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 61.91.85.24.43.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 91.85.24.85.59.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 85.24.85.43.12.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 24.85.43.59.32.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 85.43.59.12.56.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 43.59.12.32.3.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 59.12.32.56.91.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 12.32.56.3.22.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 32.56.3.91.90.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 56.3.91.22.55.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 3.91.22.90.15.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 91.22.90.55.28.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 22.90.55.15.89.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 90.55.15.28.25.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 55.15.28.89.47.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 15.28.89.25.1.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 28.89.25.47.56.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.25.47.1.40.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 25.47.1.56.43.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 47.1.56.40.56.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 1.56.40.43.16.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 56.40.43.56.75.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 40.43.56.16.36.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 43.56.16.75.89.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 56.16.75.36.98.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 16.75.36.89.76.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 75.36.89.98.81.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 36.89.98.76.4.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 89.98.76.81.94.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 98.76.81.4.42.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 76.81.4.94.30.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 81.4.94.42.78.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 4.94.42.30.33.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 94.42.30.78.29.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 42.30.78.33.53.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 30.78.33.29.63.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 78.33.29.53.2.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 33.29.53.63.87.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 29.53.63.2.37.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 53.63.2.87.80.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 63.2.87.37.84.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 2.87.37.80.72.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 87.37.80.84.41.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 37.80.84.72.9.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 80.84.72.41.61.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 84.72.41.9.73.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 72.41.9.61.95.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 41.9.61.73.65.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 9.61.73.95.13.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 61.73.95.65.58.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 73.95.65.13.96.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 95.65.13.58.98.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 65.13.58.96.1.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 13.58.96.98.21.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 58.96.98.1.74.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 96.98.1.21.65.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 98.1.21.74.35.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 1.21.74.65.5.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 21.74.65.35.73.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 74.65.35.5.11.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 65.35.5.73.51.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 35.5.73.11.87.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 5.73.11.51.41.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 73.11.51.87.12.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 11.51.87.41.8.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 51.87.41.12.20.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 87.41.12.8.31.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 41.12.8.20.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 12.8.20.31.15.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 8.20.31.31.95.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 20.31.31.15.22.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 31.31.15.95.73.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 31.15.95.22.79.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 15.95.22.73.88.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 95.22.73.79.34.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 22.73.79.88.8.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 73.79.88.34.11.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 79.88.34.8.49.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 88.34.8.11.34.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 34.8.11.49.90.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 8.11.49.34.59.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 11.49.34.90.96.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 49.34.90.59.60.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 34.90.59.96.55.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 90.59.96.60.75.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 59.96.60.55.77.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 96.60.55.75.44.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 60.55.75.77.2.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 55.75.77.44.7.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 75.77.44.2.85.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 77.44.2.7.57.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 44.2.7.85.74.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 2.7.85.57.29.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 7.85.57.74.70.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 85.57.74.29.59.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 57.74.29.70.19.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 74.29.70.59.39.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 29.70.59.19.26.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 70.59.19.39.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 59.19.39.26.47.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 19.39.26.26.80.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 39.26.26.47.90.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 26.26.47.80.36.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 26.47.80.90.58.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 47.80.90.36.47.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 80.90.36.58.9.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 90.36.58.47.72.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 36.58.47.9.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 58.47.9.72.66.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 47.9.72.72.33.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 9.72.72.66.93.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 72.72.66.33.75.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 72.66.33.93.64.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 66.33.93.75.81.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 33.93.75.64.9.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 93.75.64.81.23.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 75.64.81.9.37.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 64.81.9.23.13.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 81.9.23.37.12.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 9.23.37.13.14.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 23.37.13.12.62.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 37.13.12.14.91.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 13.12.14.62.36.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 12.14.62.91.91.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 14.62.91.36.33.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 62.91.36.91.15.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 91.36.91.33.34.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 36.91.33.15.36.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 91.33.15.34.99.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 33.15.34.36.3.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 15.34.36.99.95.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 34.36.99.3.69.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 36.99.3.95.58.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 99.3.95.69.52.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 3.95.69.58.30.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 95.69.58.52.50.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 69.58.52.30.84.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 58.52.30.50.10.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 52.30.50.84.84.33.21.39.44.58.30.38.34.83.27.82.17.7 + 30.50.84.10.33.21.39.44.58.30.38.34.83.27.82.17.7 + 50.84.10.84.21.39.44.58.30.38.34.83.27.82.17.7 + 84.10.84.33.39.44.58.30.38.34.83.27.82.17.7 + 10.84.33.21.44.58.30.38.34.83.27.82.17.7 + 84.33.21.39.58.30.38.34.83.27.82.17.7 33.21.39.44.30.38.34.83.27.82.17.7 + 21.39.44.58.38.34.83.27.82.17.7 39.44.58.30.34.83.27.82.17.7 + 44.58.30.38.83.27.82.17.7 58.30.38.34.27.82.17.7 30.38.34.83.82.17.7 + 38.34.83.27.17.7 34.83.27.82.7 83.27.82.17} + +do_execsql_test 1.20.14.2 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (PARTITION BY b%10 ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP) FROM t2 +} {40.30.80.20.90.60.70.80.90.30.50.10.30 + 90.30.80.20.90.60.70.80.90.30.50.10.30 + 90.40.80.20.90.60.70.80.90.30.50.10.30 + 90.40.30.20.90.60.70.80.90.30.50.10.30 + 90.40.30.80.90.60.70.80.90.30.50.10.30 + 40.30.80.20.60.70.80.90.30.50.10.30 30.80.20.90.70.80.90.30.50.10.30 + 80.20.90.60.80.90.30.50.10.30 20.90.60.70.90.30.50.10.30 + 90.60.70.80.30.50.10.30 60.70.80.90.50.10.30 70.80.90.30.10.30 + 80.90.30.50.30 90.30.50.10 + 91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 81.91.61.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 91.61.91.91.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 61.91.91.1.41.61.1.21.11.51.41.31.31.11.81.91.91.21 + 91.91.1.81.61.1.21.11.51.41.31.31.11.81.91.91.21 + 91.1.81.41.1.21.11.51.41.31.31.11.81.91.91.21 + 1.81.41.61.21.11.51.41.31.31.11.81.91.91.21 + 81.41.61.1.11.51.41.31.31.11.81.91.91.21 + 41.61.1.21.51.41.31.31.11.81.91.91.21 61.1.21.11.41.31.31.11.81.91.91.21 + 1.21.11.51.31.31.11.81.91.91.21 21.11.51.41.31.11.81.91.91.21 + 11.51.41.31.11.81.91.91.21 51.41.31.31.81.91.91.21 41.31.31.11.91.91.21 + 31.31.11.81.91.21 31.11.81.91.21 11.81.91.91 + 12.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 62.32.22.42.2.72.12.22.2.72.72.12.62.52.82 + 62.12.22.42.2.72.12.22.2.72.72.12.62.52.82 + 62.12.32.42.2.72.12.22.2.72.72.12.62.52.82 + 62.12.32.22.2.72.12.22.2.72.72.12.62.52.82 + 12.32.22.42.72.12.22.2.72.72.12.62.52.82 + 32.22.42.2.12.22.2.72.72.12.62.52.82 22.42.2.72.22.2.72.72.12.62.52.82 + 42.2.72.12.2.72.72.12.62.52.82 2.72.12.22.72.72.12.62.52.82 + 72.12.22.2.72.12.62.52.82 12.22.2.72.12.62.52.82 22.2.72.72.62.52.82 + 2.72.72.12.52.82 72.72.12.62.82 72.12.62.52 + 23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.23.93.43.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 23.93.43.3.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 93.43.3.43.53.63.73.13.73.73.33.93.23.13.33.3.33.83 + 43.3.43.33.63.73.13.73.73.33.93.23.13.33.3.33.83 + 3.43.33.53.73.13.73.73.33.93.23.13.33.3.33.83 + 43.33.53.63.13.73.73.33.93.23.13.33.3.33.83 + 33.53.63.73.73.73.33.93.23.13.33.3.33.83 + 53.63.73.13.73.33.93.23.13.33.3.33.83 63.73.13.73.33.93.23.13.33.3.33.83 + 73.13.73.73.93.23.13.33.3.33.83 13.73.73.33.23.13.33.3.33.83 + 73.73.33.93.13.33.3.33.83 73.33.93.23.33.3.33.83 33.93.23.13.3.33.83 + 93.23.13.33.33.83 23.13.33.3.83 13.33.3.33 + 84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.74.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 54.84.74.24.94.84.74.34.34.44.74.64.14.34.84.84.44.34 + 84.74.24.4.84.74.34.34.44.74.64.14.34.84.84.44.34 + 74.24.4.94.74.34.34.44.74.64.14.34.84.84.44.34 + 24.4.94.84.34.34.44.74.64.14.34.84.84.44.34 + 4.94.84.74.34.44.74.64.14.34.84.84.44.34 + 94.84.74.34.44.74.64.14.34.84.84.44.34 + 84.74.34.34.74.64.14.34.84.84.44.34 74.34.34.44.64.14.34.84.84.44.34 + 34.34.44.74.14.34.84.84.44.34 34.44.74.64.34.84.84.44.34 + 44.74.64.14.84.84.44.34 74.64.14.34.84.44.34 64.14.34.84.44.34 + 14.34.84.84.34 34.84.84.44 + 35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 65.35.85.85.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 35.85.85.55.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 85.85.55.15.75.95.65.65.35.5.15.95.55.75.85.75.15.95 + 85.55.15.25.95.65.65.35.5.15.95.55.75.85.75.15.95 + 55.15.25.75.65.65.35.5.15.95.55.75.85.75.15.95 + 15.25.75.95.65.35.5.15.95.55.75.85.75.15.95 + 25.75.95.65.35.5.15.95.55.75.85.75.15.95 + 75.95.65.65.5.15.95.55.75.85.75.15.95 95.65.65.35.15.95.55.75.85.75.15.95 + 65.65.35.5.95.55.75.85.75.15.95 65.35.5.15.55.75.85.75.15.95 + 35.5.15.95.75.85.75.15.95 5.15.95.55.85.75.15.95 15.95.55.75.75.15.95 + 95.55.75.85.15.95 55.75.85.75.95 75.85.75.15 + 46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 96.46.6.46.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 46.6.46.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 6.46.16.16.56.56.56.16.36.76.96.96.26.26.36.66.36.36 + 46.16.16.86.56.56.16.36.76.96.96.26.26.36.66.36.36 + 16.16.86.56.56.16.36.76.96.96.26.26.36.66.36.36 + 16.86.56.56.16.36.76.96.96.26.26.36.66.36.36 + 86.56.56.56.36.76.96.96.26.26.36.66.36.36 + 56.56.56.16.76.96.96.26.26.36.66.36.36 + 56.56.16.36.96.96.26.26.36.66.36.36 56.16.36.76.96.26.26.36.66.36.36 + 16.36.76.96.26.26.36.66.36.36 36.76.96.96.26.36.66.36.36 + 76.96.96.26.36.66.36.36 96.96.26.26.66.36.36 96.26.26.36.36.36 + 26.26.36.66.36 26.36.66.36 + 27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.27.97.67.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 27.97.67.77.7.47.87.37.87.77.7.57.47.47.37.27.17.7 + 97.67.77.47.47.87.37.87.77.7.57.47.47.37.27.17.7 + 67.77.47.7.87.37.87.77.7.57.47.47.37.27.17.7 + 77.47.7.47.37.87.77.7.57.47.47.37.27.17.7 + 47.7.47.87.87.77.7.57.47.47.37.27.17.7 + 7.47.87.37.77.7.57.47.47.37.27.17.7 47.87.37.87.7.57.47.47.37.27.17.7 + 87.37.87.77.57.47.47.37.27.17.7 37.87.77.7.47.47.37.27.17.7 + 87.77.7.57.47.37.27.17.7 77.7.57.47.37.27.17.7 7.57.47.47.27.17.7 + 57.47.47.37.17.7 47.47.37.27.7 47.37.27.17 + 68.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 38.78.8.28.98.78.58.98.8.88.8.58.58.58.38 + 38.68.8.28.98.78.58.98.8.88.8.58.58.58.38 + 38.68.78.28.98.78.58.98.8.88.8.58.58.58.38 + 38.68.78.8.98.78.58.98.8.88.8.58.58.58.38 + 68.78.8.28.78.58.98.8.88.8.58.58.58.38 + 78.8.28.98.58.98.8.88.8.58.58.58.38 8.28.98.78.98.8.88.8.58.58.58.38 + 28.98.78.58.8.88.8.58.58.58.38 98.78.58.98.88.8.58.58.58.38 + 78.58.98.8.8.58.58.58.38 58.98.8.88.58.58.58.38 98.8.88.8.58.58.38 + 8.88.8.58.58.38 88.8.58.58.38 8.58.58.58 + 59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 59.39.99.29.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 39.99.29.59.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 99.29.59.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 29.59.89.89.9.79.49.59.29.59.19.39.9.9.99.69.39 + 59.89.89.29.79.49.59.29.59.19.39.9.9.99.69.39 + 89.89.29.9.49.59.29.59.19.39.9.9.99.69.39 + 89.29.9.79.59.29.59.19.39.9.9.99.69.39 + 29.9.79.49.29.59.19.39.9.9.99.69.39 9.79.49.59.59.19.39.9.9.99.69.39 + 79.49.59.29.19.39.9.9.99.69.39 49.59.29.59.39.9.9.99.69.39 + 59.29.59.19.9.9.99.69.39 29.59.19.39.9.99.69.39 59.19.39.9.99.69.39 + 19.39.9.9.69.39 39.9.9.99.39 9.9.99.69} + +do_execsql_test 1.20.14.3 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2 +} {1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.2.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.3.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.1.2.2.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 1.2.2.3.4.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 2.2.3.3.5.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 2.3.3.4.6.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 3.3.4.5.7.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 3.4.5.6.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 4.5.6.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 5.6.7.7.8.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 6.7.7.7.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 7.7.7.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 7.7.8.8.9.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 7.8.8.8.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 8.8.8.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 8.8.9.9.10.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 8.9.9.9.11.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 9.9.9.10.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 9.9.10.11.12.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 9.10.11.11.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 10.11.11.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 11.11.12.12.13.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 11.12.12.12.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 12.12.12.13.14.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 12.12.13.13.15.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 12.13.13.14.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 13.13.14.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 13.14.15.15.16.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 14.15.15.15.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 15.15.15.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 15.15.16.16.17.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 15.16.16.16.19.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 16.16.16.17.20.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 16.16.17.19.21.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 16.17.19.20.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 17.19.20.21.22.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 19.20.21.21.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 20.21.21.22.23.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 21.21.22.22.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 21.22.22.23.24.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 22.22.23.23.25.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 22.23.23.24.26.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 23.23.24.25.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 23.24.25.26.27.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 24.25.26.26.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 25.26.26.27.28.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 26.26.27.27.29.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 26.27.27.28.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 27.27.28.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 27.28.29.29.30.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 28.29.29.29.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 29.29.29.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 29.29.30.30.31.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 29.30.30.30.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 30.30.30.31.32.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 30.30.31.31.33.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 30.31.31.32.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 31.31.32.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 31.32.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 32.33.33.33.34.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 33.33.33.33.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 33.33.33.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 33.33.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 33.34.34.34.35.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 34.34.34.34.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 34.34.34.35.36.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 34.34.35.35.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 34.35.35.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 35.35.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 35.36.36.36.37.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 36.36.36.36.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 36.36.36.37.38.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 36.36.37.37.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 36.37.37.38.39.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 37.37.38.38.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 37.38.38.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 38.38.39.39.40.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 38.39.39.39.41.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 39.39.39.40.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 39.39.40.41.42.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 39.40.41.41.43.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 40.41.41.42.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 41.41.42.43.44.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 41.42.43.43.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 42.43.43.44.46.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 43.43.44.44.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 43.44.44.46.47.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 44.44.46.46.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 44.46.46.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 46.46.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 46.47.47.47.49.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 47.47.47.47.50.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 47.47.47.49.51.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 47.47.49.50.52.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 47.49.50.51.53.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 49.50.51.52.54.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 50.51.52.53.55.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 51.52.53.54.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 52.53.54.55.56.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 53.54.55.55.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 54.55.55.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 55.55.56.56.57.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 55.56.56.56.58.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 56.56.56.57.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 56.56.57.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 56.57.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 57.58.58.58.59.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 58.58.58.58.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 58.58.58.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 58.58.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 58.59.59.59.60.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 59.59.59.59.61.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 59.59.59.60.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 59.59.60.61.62.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 59.60.61.61.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 60.61.61.62.63.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 61.61.62.62.64.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 61.62.62.63.65.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 62.62.63.64.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 62.63.64.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 63.64.65.65.66.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 64.65.65.65.67.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 65.65.65.66.68.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 65.65.66.67.69.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 65.66.67.68.70.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 66.67.68.69.72.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 67.68.69.70.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 68.69.70.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 69.70.72.72.73.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 70.72.72.72.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 72.72.72.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 72.72.73.73.74.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 72.73.73.73.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 73.73.73.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 73.73.74.74.75.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 73.74.74.74.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 74.74.74.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 74.74.75.75.76.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 74.75.75.75.77.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 75.75.75.76.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 75.75.76.77.78.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 75.76.77.77.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 76.77.77.78.79.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 77.77.78.78.80.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 77.78.78.79.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 78.78.79.80.81.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 78.79.80.80.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 79.80.80.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 80.80.81.81.82.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 80.81.81.81.83.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 81.81.81.82.84.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 81.81.82.83.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 81.82.83.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 82.83.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 83.84.84.84.85.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 84.84.84.84.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 84.84.84.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 84.84.85.85.86.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 84.85.85.85.87.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 85.85.85.86.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 85.85.86.87.88.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 85.86.87.87.89.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 86.87.87.88.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 87.87.88.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 87.88.89.89.90.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 88.89.89.89.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 89.89.89.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 89.89.90.90.91.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 89.90.90.90.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 90.90.90.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 90.90.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 90.91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 91.91.91.91.93.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 91.91.91.91.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 91.91.91.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 91.91.93.93.94.95.95.95.96.96.96.97.97.98.98.99.99 + 91.93.93.93.95.95.95.96.96.96.97.97.98.98.99.99 + 93.93.93.94.95.95.96.96.96.97.97.98.98.99.99 + 93.93.94.95.95.96.96.96.97.97.98.98.99.99 + 93.94.95.95.96.96.96.97.97.98.98.99.99 + 94.95.95.95.96.96.97.97.98.98.99.99 95.95.95.96.96.97.97.98.98.99.99 + 95.95.96.96.97.97.98.98.99.99 95.96.96.96.97.98.98.99.99 + 96.96.96.97.98.98.99.99 96.96.97.97.98.99.99 96.97.97.98.99.99 + 97.97.98.98.99 97.98.98.99} + +do_execsql_test 1.20.14.4 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( PARTITION BY b%10 ORDER BY b,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2 +} {20.30.30.30.40.50.60.70.80.80.90.90.90 + 10.30.30.30.40.50.60.70.80.80.90.90.90 + 10.20.30.30.40.50.60.70.80.80.90.90.90 + 10.20.30.30.40.50.60.70.80.80.90.90.90 + 10.20.30.30.40.50.60.70.80.80.90.90.90 + 20.30.30.30.50.60.70.80.80.90.90.90 30.30.30.40.60.70.80.80.90.90.90 + 30.30.40.50.70.80.80.90.90.90 30.40.50.60.80.80.90.90.90 + 40.50.60.70.80.90.90.90 50.60.70.80.90.90.90 60.70.80.80.90.90 + 70.80.80.90.90 80.80.90.90 + 1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.11.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.21.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.1.11.11.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 1.11.11.21.31.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 11.11.21.21.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 11.21.21.31.41.41.51.61.61.81.81.81.91.91.91.91.91 + 21.21.31.31.41.51.61.61.81.81.81.91.91.91.91.91 + 21.31.31.41.51.61.61.81.81.81.91.91.91.91.91 + 31.31.41.41.61.61.81.81.81.91.91.91.91.91 + 31.41.41.51.61.81.81.81.91.91.91.91.91 + 41.41.51.61.81.81.81.91.91.91.91.91 41.51.61.61.81.81.91.91.91.91.91 + 51.61.61.81.81.91.91.91.91.91 61.61.81.81.91.91.91.91.91 + 61.81.81.81.91.91.91.91 81.81.81.91.91.91.91 81.81.91.91.91.91 + 81.91.91.91.91 91.91.91.91 2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.12.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.2.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.2.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.2.12.12.22.22.32.42.52.62.62.72.72.72.82 + 2.12.12.12.22.32.42.52.62.62.72.72.72.82 + 12.12.12.22.32.42.52.62.62.72.72.72.82 + 12.12.22.22.42.52.62.62.72.72.72.82 12.22.22.32.52.62.62.72.72.72.82 + 22.22.32.42.62.62.72.72.72.82 22.32.42.52.62.72.72.72.82 + 32.42.52.62.72.72.72.82 42.52.62.62.72.72.82 52.62.62.72.72.82 + 62.62.72.72.82 62.72.72.72 + 3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.13.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.23.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.3.13.13.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 3.13.13.23.33.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 13.13.23.23.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 13.23.23.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 23.23.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 23.33.33.33.43.43.53.63.73.73.73.83.93.93.93 + 33.33.33.33.43.53.63.73.73.73.83.93.93.93 + 33.33.33.43.53.63.73.73.73.83.93.93.93 + 33.33.43.43.63.73.73.73.83.93.93.93 33.43.43.53.73.73.73.83.93.93.93 + 43.43.53.63.73.73.83.93.93.93 43.53.63.73.73.83.93.93.93 + 53.63.73.73.83.93.93.93 63.73.73.73.93.93.93 73.73.73.83.93.93 + 73.73.83.93.93 73.83.93.93 + 14.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.24.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.34.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.24.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 4.14.24.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 14.24.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 24.34.34.34.44.44.54.64.74.74.74.84.84.84.84.94 + 34.34.34.34.44.54.64.74.74.74.84.84.84.84.94 + 34.34.34.44.54.64.74.74.74.84.84.84.84.94 + 34.34.44.44.64.74.74.74.84.84.84.84.94 + 34.44.44.54.74.74.74.84.84.84.84.94 44.44.54.64.74.74.84.84.84.84.94 + 44.54.64.74.74.84.84.84.84.94 54.64.74.74.84.84.84.84.94 + 64.74.74.74.84.84.84.94 74.74.74.84.84.84.94 74.74.84.84.84.94 + 74.84.84.84.94 84.84.84.84 + 15.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.25.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 5.15.15.15.35.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 15.15.15.25.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 15.15.25.35.55.55.65.65.65.75.75.75.85.85.85.95.95.95 + 15.25.35.35.55.65.65.65.75.75.75.85.85.85.95.95.95 + 25.35.35.55.65.65.65.75.75.75.85.85.85.95.95.95 + 35.35.55.55.65.65.75.75.75.85.85.85.95.95.95 + 35.55.55.65.65.75.75.75.85.85.85.95.95.95 + 55.55.65.65.75.75.75.85.85.85.95.95.95 + 55.65.65.65.75.75.85.85.85.95.95.95 65.65.65.75.75.85.85.85.95.95.95 + 65.65.75.75.85.85.85.95.95.95 65.75.75.75.85.85.95.95.95 + 75.75.75.85.85.95.95.95 75.75.85.85.95.95.95 75.85.85.85.95.95 + 85.85.85.95.95 85.85.95.95 + 16.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.26.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 6.16.16.16.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 16.16.16.26.36.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 16.16.26.26.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 16.26.26.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 26.26.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 26.36.36.36.46.46.56.56.56.66.76.86.96.96.96 + 36.36.36.36.46.56.56.56.66.76.86.96.96.96 + 36.36.36.46.56.56.56.66.76.86.96.96.96 + 36.36.46.46.56.56.66.76.86.96.96.96 36.46.46.56.56.66.76.86.96.96.96 + 46.46.56.56.66.76.86.96.96.96 46.56.56.56.76.86.96.96.96 + 56.56.56.66.86.96.96.96 56.56.66.76.96.96.96 56.66.76.86.96.96 + 66.76.86.96.96 76.86.96.96 + 7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.17.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.27.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.7.17.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.7.17.27.37.37.47.47.47.47.57.67.77.77.87.87.97.97 + 7.17.27.27.37.47.47.47.47.57.67.77.77.87.87.97.97 + 17.27.27.37.47.47.47.47.57.67.77.77.87.87.97.97 + 27.27.37.37.47.47.47.57.67.77.77.87.87.97.97 + 27.37.37.47.47.47.57.67.77.77.87.87.97.97 + 37.37.47.47.47.57.67.77.77.87.87.97.97 + 37.47.47.47.57.67.77.77.87.87.97.97 47.47.47.47.67.77.77.87.87.97.97 + 47.47.47.57.77.77.87.87.97.97 47.47.57.67.77.87.87.97.97 + 47.57.67.77.87.87.97.97 57.67.77.77.87.97.97 67.77.77.87.97.97 + 77.77.87.87.97 77.87.87.97 8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.28.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.8.38.38.58.58.58.58.68.78.78.88.98.98 + 8.8.8.28.38.58.58.58.58.68.78.78.88.98.98 + 8.8.28.38.58.58.58.58.68.78.78.88.98.98 + 8.28.38.38.58.58.58.68.78.78.88.98.98 28.38.38.58.58.58.68.78.78.88.98.98 + 38.38.58.58.58.68.78.78.88.98.98 38.58.58.58.68.78.78.88.98.98 + 58.58.58.58.78.78.88.98.98 58.58.58.68.78.88.98.98 58.58.68.78.88.98.98 + 58.68.78.78.98.98 68.78.78.88.98 78.78.88.98 + 9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.19.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.29.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.9.19.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.9.19.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 9.19.29.29.39.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 19.29.29.29.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 29.29.29.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 29.29.39.39.49.59.59.59.59.69.79.89.89.89.99.99 + 29.39.39.39.59.59.59.59.69.79.89.89.89.99.99 + 39.39.39.49.59.59.59.69.79.89.89.89.99.99 + 39.39.49.59.59.59.69.79.89.89.89.99.99 + 39.49.59.59.59.69.79.89.89.89.99.99 49.59.59.59.69.79.89.89.89.99.99 + 59.59.59.59.79.89.89.89.99.99 59.59.59.69.89.89.89.99.99 + 59.59.69.79.89.89.99.99 59.69.79.89.89.99.99 69.79.89.89.99.99 + 79.89.89.89.99 89.89.89.99} + +do_execsql_test 1.20.14.5 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER ( ORDER BY b%10,a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) FROM t2 +} {40.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.30.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.80.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.20.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.40.30.80.90.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 40.30.80.20.60.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 30.80.20.90.70.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 80.20.90.60.80.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 20.90.60.70.90.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.60.70.80.30.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 60.70.80.90.50.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 70.80.90.30.10.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 80.90.30.50.30.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 90.30.50.10.81.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 30.50.10.30.91.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 50.10.30.81.61.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 10.30.81.91.91.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 30.81.91.61.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 81.91.61.91.1.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 91.61.91.91.81.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 61.91.91.1.41.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 91.91.1.81.61.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 91.1.81.41.1.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 1.81.41.61.21.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 81.41.61.1.11.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 41.61.1.21.51.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 61.1.21.11.41.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 1.21.11.51.31.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 21.11.51.41.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 11.51.41.31.11.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 51.41.31.31.81.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 41.31.31.11.91.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 31.31.11.81.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 31.11.81.91.21.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 11.81.91.91.62.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 81.91.91.21.12.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 91.91.21.62.32.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 91.21.62.12.22.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 21.62.12.32.42.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 62.12.32.22.2.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 12.32.22.42.72.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 32.22.42.2.12.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 22.42.2.72.22.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 42.2.72.12.2.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 2.72.12.22.72.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 72.12.22.2.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 12.22.2.72.12.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 22.2.72.72.62.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 2.72.72.12.52.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 72.72.12.62.82.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 72.12.62.52.93.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 12.62.52.82.23.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 62.52.82.93.93.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 52.82.93.23.43.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 82.93.23.93.3.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 93.23.93.43.43.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 23.93.43.3.33.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 93.43.3.43.53.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 43.3.43.33.63.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 3.43.33.53.73.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 43.33.53.63.13.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 33.53.63.73.73.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 53.63.73.13.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 63.73.13.73.33.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 73.13.73.73.93.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 13.73.73.33.23.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 73.73.33.93.13.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 73.33.93.23.33.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 33.93.23.13.3.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 93.23.13.33.33.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 23.13.33.3.83.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 13.33.3.33.54.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 33.3.33.83.84.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 3.33.83.54.74.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 33.83.54.84.24.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 83.54.84.74.4.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 54.84.74.24.94.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 84.74.24.4.84.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 74.24.4.94.74.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 24.4.94.84.34.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 4.94.84.74.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 94.84.74.34.44.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 84.74.34.34.74.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 74.34.34.44.64.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 34.34.44.74.14.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 34.44.74.64.34.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 44.74.64.14.84.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 74.64.14.34.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 64.14.34.84.44.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 14.34.84.84.34.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 34.84.84.44.65.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 84.84.44.34.35.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 84.44.34.65.85.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 44.34.65.35.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 34.65.35.85.55.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 65.35.85.85.15.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 35.85.85.55.25.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 85.85.55.15.75.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 85.55.15.25.95.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 55.15.25.75.65.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 15.25.75.95.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 25.75.95.65.35.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 75.95.65.65.5.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 95.65.65.35.15.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 65.65.35.5.95.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 65.35.5.15.55.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 35.5.15.95.75.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 5.15.95.55.85.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 15.95.55.75.75.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 95.55.75.85.15.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 55.75.85.75.95.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 75.85.75.15.96.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 85.75.15.95.46.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 75.15.95.96.6.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 15.95.96.46.46.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 95.96.46.6.16.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 96.46.6.46.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 46.6.46.16.86.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 6.46.16.16.56.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 46.16.16.86.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 16.16.86.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 16.86.56.56.16.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 86.56.56.56.36.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 56.56.56.16.76.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 56.56.16.36.96.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 56.16.36.76.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 16.36.76.96.26.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 36.76.96.96.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 76.96.96.26.36.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 96.96.26.26.66.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 96.26.26.36.36.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 26.26.36.66.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 26.36.66.36.97.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 36.66.36.36.27.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 66.36.36.97.97.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 36.36.97.27.67.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 36.97.27.97.77.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 97.27.97.67.47.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 27.97.67.77.7.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 97.67.77.47.47.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 67.77.47.7.87.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 77.47.7.47.37.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 47.7.47.87.87.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 7.47.87.37.77.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 47.87.37.87.7.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 87.37.87.77.57.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 37.87.77.7.47.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 87.77.7.57.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 77.7.57.47.37.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 7.57.47.47.27.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 57.47.47.37.17.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 47.47.37.27.7.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 47.37.27.17.38.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 37.27.17.7.68.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 27.17.7.38.78.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 17.7.38.68.8.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 7.38.68.78.28.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 38.68.78.8.98.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 68.78.8.28.78.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 78.8.28.98.58.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 8.28.98.78.98.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 28.98.78.58.8.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 98.78.58.98.88.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 78.58.98.8.8.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 58.98.8.88.58.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 98.8.88.8.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 8.88.8.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 88.8.58.58.38.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 8.58.58.58.89.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 58.58.58.38.59.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 58.58.38.89.39.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 58.38.89.59.99.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 38.89.59.39.29.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 89.59.39.99.59.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 59.39.99.29.89.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 39.99.29.59.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 99.29.59.89.29.9.79.49.59.29.59.19.39.9.9.99.69.39 + 29.59.89.89.9.79.49.59.29.59.19.39.9.9.99.69.39 + 59.89.89.29.79.49.59.29.59.19.39.9.9.99.69.39 + 89.89.29.9.49.59.29.59.19.39.9.9.99.69.39 + 89.29.9.79.59.29.59.19.39.9.9.99.69.39 + 29.9.79.49.29.59.19.39.9.9.99.69.39 9.79.49.59.59.19.39.9.9.99.69.39 + 79.49.59.29.19.39.9.9.99.69.39 49.59.29.59.39.9.9.99.69.39 + 59.29.59.19.9.9.99.69.39 29.59.19.39.9.99.69.39 59.19.39.9.99.69.39 + 19.39.9.9.69.39 39.9.9.99.39 9.9.99.69} + +do_execsql_test 1.20.14.6 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (PARTITION BY b%2,a ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP) FROM t2 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.20.14.7 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (win1 ORDER BY b%10 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP) + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a) + ORDER BY 1 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.20.14.8 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER (win1 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP) + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a ORDER BY b%10) + ORDER BY 1 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.20.14.9 { + SELECT group_concat(CAST(b AS TEXT), '.') OVER win2 + FROM t2 + WINDOW win1 AS (PARTITION BY b%2,a ORDER BY b%10), + win2 AS (win1 ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP) + ORDER BY 1 +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} + {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 1.20.15.1 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE a%2=0) OVER win FROM t2 + WINDOW win AS (ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP) +} {190 96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 190 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 190 89.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 190 89.96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 190 89.96.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 189 96.38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 188 96.38.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 187 38.39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 186 38.39.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 185 39.91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 184 39.91.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 183 91.6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 182 91.6.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 181 6.97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 180 6.97.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 179 97.46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 178 97.46.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 177 46.54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 176 46.54.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 175 54.8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 174 54.8.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 173 8.29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 172 8.29.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 171 29.84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 170 29.84.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 169 84.23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 168 84.23.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 167 23.16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 166 23.16.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 165 16.65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 164 16.65.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 163 65.47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 162 65.47.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 161 47.86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 160 47.86.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 159 86.61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 158 86.61.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 157 61.85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 156 61.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 155 85.85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 154 85.85.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 153 85.59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 152 85.59.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 151 59.32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 150 59.32.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 149 32.3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 148 32.3.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 147 3.22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 146 3.22.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 145 22.55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 144 22.55.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 143 55.28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 142 55.28.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 141 28.25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 140 28.25.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 139 25.1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 138 25.1.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 137 1.40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 136 1.40.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 135 40.56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 134 40.56.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 133 56.75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 132 56.75.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 131 75.89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 130 75.89.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 129 89.76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 128 89.76.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 127 76.4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 126 76.4.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 125 4.42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 124 4.42.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 123 42.78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 122 42.78.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 121 78.29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 120 78.29.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 119 29.63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 118 29.63.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 117 63.87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 116 63.87.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 115 87.80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 114 87.80.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 113 80.72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 112 80.72.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 111 72.9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 110 72.9.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 109 9.73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 108 9.73.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 107 73.65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 106 73.65.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 105 65.58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 104 65.58.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 103 58.98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 102 58.98.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 101 98.21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 100 98.21.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 99 21.65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 98 21.65.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 97 65.5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 96 65.5.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 95 5.11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 94 5.11.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 93 11.87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 92 11.87.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 91 87.12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 90 87.12.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 89 12.20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 88 12.20.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 87 20.31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 86 20.31.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 85 31.95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 84 31.95.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 83 95.73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 82 95.73.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 81 73.88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 80 73.88.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 79 88.8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 78 88.8.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 77 8.49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 76 8.49.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 75 49.90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 74 49.90.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 73 90.96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 72 90.96.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 71 96.55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 70 96.55.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 69 55.77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 68 55.77.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 67 77.2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 66 77.2.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 65 2.85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 64 2.85.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 63 85.74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 62 85.74.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 61 74.70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 60 74.70.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 59 70.19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 58 70.19.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 57 19.26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 56 19.26.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 55 26.47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 54 26.47.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 53 47.90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 52 47.90.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 51 90.58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 50 90.58.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 49 58.9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 48 58.9.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 47 9.72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 46 9.72.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 45 72.33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 44 72.33.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 43 33.75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 42 33.75.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 41 75.81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 40 75.81.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 39 81.23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 38 81.23.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 37 23.13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 36 23.13.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 35 13.14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 34 13.14.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 33 14.91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 32 14.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 31 91.91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 30 91.91.36.3.69.52.50.10.33.39.58.38.83.82.7 + 29 91.15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 28 91.15.3.69.52.50.10.33.39.58.38.83.82.7 + 27 15.36.3.69.52.50.10.33.39.58.38.83.82.7 + 26 15.36.69.52.50.10.33.39.58.38.83.82.7 + 25 36.3.69.52.50.10.33.39.58.38.83.82.7 + 24 36.3.52.50.10.33.39.58.38.83.82.7 23 3.69.52.50.10.33.39.58.38.83.82.7 + 22 3.69.50.10.33.39.58.38.83.82.7 21 69.52.50.10.33.39.58.38.83.82.7 + 20 69.52.10.33.39.58.38.83.82.7 19 52.50.10.33.39.58.38.83.82.7 + 18 52.50.33.39.58.38.83.82.7 17 50.10.33.39.58.38.83.82.7 + 16 50.10.39.58.38.83.82.7 15 10.33.39.58.38.83.82.7 + 14 10.33.58.38.83.82.7 13 33.39.58.38.83.82.7 12 33.39.38.83.82.7 + 11 39.58.38.83.82.7 10 39.58.83.82.7 9 58.38.83.82.7 8 58.38.82.7 + 7 38.83.82.7 6 38.83.7 5 83.82.7 4 83.82} + +do_execsql_test 1.20.15.2 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE 0=1) OVER win FROM t2 + WINDOW win AS (ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP) +} {190 {} 190 {} 190 {} 190 {} 190 {} 189 {} 188 {} 187 {} + 186 {} 185 {} 184 {} 183 {} 182 {} 181 {} 180 {} 179 {} + 178 {} 177 {} 176 {} 175 {} 174 {} 173 {} 172 {} 171 {} + 170 {} 169 {} 168 {} 167 {} 166 {} 165 {} 164 {} 163 {} + 162 {} 161 {} 160 {} 159 {} 158 {} 157 {} 156 {} 155 {} + 154 {} 153 {} 152 {} 151 {} 150 {} 149 {} 148 {} 147 {} + 146 {} 145 {} 144 {} 143 {} 142 {} 141 {} 140 {} 139 {} + 138 {} 137 {} 136 {} 135 {} 134 {} 133 {} 132 {} 131 {} + 130 {} 129 {} 128 {} 127 {} 126 {} 125 {} 124 {} 123 {} + 122 {} 121 {} 120 {} 119 {} 118 {} 117 {} 116 {} 115 {} + 114 {} 113 {} 112 {} 111 {} 110 {} 109 {} 108 {} 107 {} + 106 {} 105 {} 104 {} 103 {} 102 {} 101 {} 100 {} 99 {} + 98 {} 97 {} 96 {} 95 {} 94 {} 93 {} 92 {} 91 {} 90 {} + 89 {} 88 {} 87 {} 86 {} 85 {} 84 {} 83 {} 82 {} 81 {} + 80 {} 79 {} 78 {} 77 {} 76 {} 75 {} 74 {} 73 {} 72 {} + 71 {} 70 {} 69 {} 68 {} 67 {} 66 {} 65 {} 64 {} 63 {} + 62 {} 61 {} 60 {} 59 {} 58 {} 57 {} 56 {} 55 {} 54 {} + 53 {} 52 {} 51 {} 50 {} 49 {} 48 {} 47 {} 46 {} 45 {} + 44 {} 43 {} 42 {} 41 {} 40 {} 39 {} 38 {} 37 {} 36 {} + 35 {} 34 {} 33 {} 32 {} 31 {} 30 {} 29 {} 28 {} 27 {} + 26 {} 25 {} 24 {} 23 {} 22 {} 21 {} 20 {} 19 {} 18 {} + 17 {} 16 {} 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} + 7 {} 6 {} 5 {} 4 {}} + +do_execsql_test 1.20.15.3 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE 1=0) OVER win FROM t2 + WINDOW win AS (PARTITION BY (a%10) ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP) +} {19 {} 19 {} 19 {} 19 {} 19 {} 18 {} 17 {} 16 {} 15 {} + 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} 5 {} + 4 {} 18 {} 18 {} 18 {} 18 {} 18 {} 17 {} 16 {} 15 {} + 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} 5 {} + 4 {} 18 {} 18 {} 18 {} 18 {} 18 {} 17 {} 16 {} 15 {} + 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} 5 {} + 4 {} 18 {} 18 {} 18 {} 18 {} 18 {} 17 {} 16 {} 15 {} + 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} 5 {} + 4 {} 18 {} 18 {} 18 {} 18 {} 18 {} 17 {} 16 {} 15 {} + 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} 5 {} + 4 {} 18 {} 18 {} 18 {} 18 {} 18 {} 17 {} 16 {} 15 {} + 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} 5 {} + 4 {} 18 {} 18 {} 18 {} 18 {} 18 {} 17 {} 16 {} 15 {} + 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} 5 {} + 4 {} 18 {} 18 {} 18 {} 18 {} 18 {} 17 {} 16 {} 15 {} + 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} 5 {} + 4 {} 18 {} 18 {} 18 {} 18 {} 18 {} 17 {} 16 {} 15 {} + 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} 5 {} + 4 {} 18 {} 18 {} 18 {} 18 {} 18 {} 17 {} 16 {} 15 {} + 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} 7 {} 6 {} 5 {} + 4 {}} + +do_execsql_test 1.20.15.4 { + SELECT count(*) OVER win, group_concat(CAST(b AS TEXT), '.') + FILTER (WHERE a%2=0) OVER win FROM t2 + WINDOW win AS (PARTITION BY (a%10) ORDER BY a ROWS BETWEEN 4 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP) +} {19 6.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 19 89.29.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 19 89.6.47.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 19 89.6.29.59.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 19 89.6.29.47.28.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 18 6.29.47.59.75.78.72.98.87.73.96.74.90.75.91.69.39.7 + 17 29.47.59.28.78.72.98.87.73.96.74.90.75.91.69.39.7 + 16 47.59.28.75.72.98.87.73.96.74.90.75.91.69.39.7 + 15 59.28.75.78.98.87.73.96.74.90.75.91.69.39.7 + 14 28.75.78.72.87.73.96.74.90.75.91.69.39.7 + 13 75.78.72.98.73.96.74.90.75.91.69.39.7 + 12 78.72.98.87.96.74.90.75.91.69.39.7 11 72.98.87.73.74.90.75.91.69.39.7 + 10 98.87.73.96.90.75.91.69.39.7 9 87.73.96.74.75.91.69.39.7 + 8 73.96.74.90.91.69.39.7 7 96.74.90.75.69.39.7 6 74.90.75.91.39.7 + 5 90.75.91.69.7 4 75.91.69.39 18 {} 18 {} 18 {} 18 {} 18 {} + 17 {} 16 {} 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} + 7 {} 6 {} 5 {} 4 {} + 18 97.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 18 96.84.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 18 96.97.86.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 18 96.97.84.32.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 18 96.97.84.86.25.89.29.9.21.12.88.55.70.58.81.91.52.58 + 17 97.84.86.32.89.29.9.21.12.88.55.70.58.81.91.52.58 + 16 84.86.32.25.29.9.21.12.88.55.70.58.81.91.52.58 + 15 86.32.25.89.9.21.12.88.55.70.58.81.91.52.58 + 14 32.25.89.29.21.12.88.55.70.58.81.91.52.58 + 13 25.89.29.9.12.88.55.70.58.81.91.52.58 + 12 89.29.9.21.88.55.70.58.81.91.52.58 11 29.9.21.12.55.70.58.81.91.52.58 + 10 9.21.12.88.70.58.81.91.52.58 9 21.12.88.55.58.81.91.52.58 + 8 12.88.55.70.81.91.52.58 7 88.55.70.58.91.52.58 6 55.70.58.81.52.58 + 5 70.58.81.91.58 4 58.81.91.52 18 {} 18 {} 18 {} 18 {} 18 {} + 17 {} 16 {} 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} + 7 {} 6 {} 5 {} 4 {} + 18 46.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 18 38.23.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 18 38.46.61.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 18 38.46.23.3.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 18 38.46.23.61.1.76.63.73.65.20.8.77.19.9.23.15.50.38 + 17 46.23.61.3.76.63.73.65.20.8.77.19.9.23.15.50.38 + 16 23.61.3.1.63.73.65.20.8.77.19.9.23.15.50.38 + 15 61.3.1.76.73.65.20.8.77.19.9.23.15.50.38 + 14 3.1.76.63.65.20.8.77.19.9.23.15.50.38 + 13 1.76.63.73.20.8.77.19.9.23.15.50.38 + 12 76.63.73.65.8.77.19.9.23.15.50.38 11 63.73.65.20.77.19.9.23.15.50.38 + 10 73.65.20.8.19.9.23.15.50.38 9 65.20.8.77.9.23.15.50.38 + 8 20.8.77.19.23.15.50.38 7 8.77.19.9.15.50.38 6 77.19.9.23.50.38 + 5 19.9.23.15.38 4 9.23.15.50 18 {} 18 {} 18 {} 18 {} 18 {} + 17 {} 16 {} 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} + 7 {} 6 {} 5 {} 4 {} + 18 54.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 18 39.16.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 18 39.54.85.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 18 39.54.16.22.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 18 39.54.16.85.40.4.87.65.5.31.49.2.26.72.13.36.10.83 + 17 54.16.85.22.4.87.65.5.31.49.2.26.72.13.36.10.83 + 16 16.85.22.40.87.65.5.31.49.2.26.72.13.36.10.83 + 15 85.22.40.4.65.5.31.49.2.26.72.13.36.10.83 + 14 22.40.4.87.5.31.49.2.26.72.13.36.10.83 + 13 40.4.87.65.31.49.2.26.72.13.36.10.83 + 12 4.87.65.5.49.2.26.72.13.36.10.83 11 87.65.5.31.2.26.72.13.36.10.83 + 10 65.5.31.49.26.72.13.36.10.83 9 5.31.49.2.72.13.36.10.83 + 8 31.49.2.26.13.36.10.83 7 49.2.26.72.36.10.83 6 2.26.72.13.10.83 + 5 26.72.13.36.83 4 72.13.36.10 18 {} 18 {} 18 {} 18 {} 18 {} + 17 {} 16 {} 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} + 7 {} 6 {} 5 {} 4 {} + 18 8.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 18 91.65.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 18 91.8.85.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 18 91.8.65.55.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 18 91.8.65.85.56.42.80.58.11.95.90.85.47.33.14.3.33.82 + 17 8.65.85.55.42.80.58.11.95.90.85.47.33.14.3.33.82 + 16 65.85.55.56.80.58.11.95.90.85.47.33.14.3.33.82 + 15 85.55.56.42.58.11.95.90.85.47.33.14.3.33.82 + 14 55.56.42.80.11.95.90.85.47.33.14.3.33.82 + 13 56.42.80.58.95.90.85.47.33.14.3.33.82 + 12 42.80.58.11.90.85.47.33.14.3.33.82 11 80.58.11.95.85.47.33.14.3.33.82 + 10 58.11.95.90.47.33.14.3.33.82 9 11.95.90.85.33.14.3.33.82 + 8 95.90.85.47.14.3.33.82 7 90.85.47.33.3.33.82 6 85.47.33.14.33.82 + 5 47.33.14.3.82 4 33.14.3.33 18 {} 18 {} 18 {} 18 {} 18 {} + 17 {} 16 {} 15 {} 14 {} 13 {} 12 {} 11 {} 10 {} 9 {} 8 {} + 7 {} 6 {} 5 {} 4 {}} + +finish_test diff --git a/test/window4.tcl b/test/window4.tcl new file mode 100644 index 0000000000..0b91d768a4 --- /dev/null +++ b/test/window4.tcl @@ -0,0 +1,424 @@ +## 2018 May 19 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +source [file join [file dirname $argv0] pg_common.tcl] + +#========================================================================= + +start_test window4 "2018 June 04" +ifcapable !windowfunc + +execsql_test 1.0 { + DROP TABLE IF EXISTS t3; + CREATE TABLE t3(a TEXT PRIMARY KEY); + INSERT INTO t3 VALUES('a'), ('b'), ('c'), ('d'), ('e'); + INSERT INTO t3 VALUES('f'), ('g'), ('h'), ('i'), ('j'); +} + +for {set i 1} {$i < 20} {incr i} { + execsql_test 1.$i "SELECT a, ntile($i) OVER (ORDER BY a) FROM t3" +} + +execsql_test 2.0 { + DROP TABLE IF EXISTS t4; + CREATE TABLE t4(a INTEGER PRIMARY KEY, b TEXT, c INTEGER); + INSERT INTO t4 VALUES(1, 'A', 9); + INSERT INTO t4 VALUES(2, 'B', 3); + INSERT INTO t4 VALUES(3, 'C', 2); + INSERT INTO t4 VALUES(4, 'D', 10); + INSERT INTO t4 VALUES(5, 'E', 5); + INSERT INTO t4 VALUES(6, 'F', 1); + INSERT INTO t4 VALUES(7, 'G', 1); + INSERT INTO t4 VALUES(8, 'H', 2); + INSERT INTO t4 VALUES(9, 'I', 10); + INSERT INTO t4 VALUES(10, 'J', 4); +} + +execsql_test 2.1 { + SELECT a, nth_value(b, c) OVER (ORDER BY a) FROM t4 +} + +execsql_test 2.2.1 { + SELECT a, lead(b) OVER (ORDER BY a) FROM t4 +} +execsql_test 2.2.2 { + SELECT a, lead(b, 2) OVER (ORDER BY a) FROM t4 +} +execsql_test 2.2.3 { + SELECT a, lead(b, 3, 'abc') OVER (ORDER BY a) FROM t4 +} + +execsql_test 2.3.1 { + SELECT a, lag(b) OVER (ORDER BY a) FROM t4 +} +execsql_test 2.3.2 { + SELECT a, lag(b, 2) OVER (ORDER BY a) FROM t4 +} +execsql_test 2.3.3 { + SELECT a, lag(b, 3, 'abc') OVER (ORDER BY a) FROM t4 +} + +execsql_test 2.4.1 { + SELECT string_agg(b, '.') OVER ( + ORDER BY a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + ) FROM t4 +} + +execsql_test 3.0 { + DROP TABLE IF EXISTS t5; + CREATE TABLE t5(a INTEGER PRIMARY KEY, b TEXT, c TEXT, d INTEGER); + INSERT INTO t5 VALUES(1, 'A', 'one', 5); + INSERT INTO t5 VALUES(2, 'B', 'two', 4); + INSERT INTO t5 VALUES(3, 'A', 'three', 3); + INSERT INTO t5 VALUES(4, 'B', 'four', 2); + INSERT INTO t5 VALUES(5, 'A', 'five', 1); +} + +execsql_test 3.1 { + SELECT a, nth_value(c, d) OVER (ORDER BY b) FROM t5 +} + +execsql_test 3.2 { + SELECT a, nth_value(c, d) OVER (PARTITION BY b ORDER BY a) FROM t5 +} + +execsql_test 3.3 { + SELECT a, count(*) OVER abc, count(*) OVER def FROM t5 + WINDOW abc AS (ORDER BY a), + def AS (ORDER BY a DESC) + ORDER BY a; +} + +execsql_test 3.4 { + SELECT a, max(a) FILTER (WHERE (a%2)=0) OVER w FROM t5 + WINDOW w AS (ORDER BY a) +} + +execsql_test 3.5.1 { + SELECT a, max(c) OVER (ORDER BY a ROWS BETWEEN 1 PRECEDING AND 2 PRECEDING) + FROM t5 +} +execsql_test 3.5.2 { + SELECT a, max(c) OVER (ORDER BY a ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING) + FROM t5 +} +execsql_test 3.5.3 { + SELECT a, max(c) OVER (ORDER BY a ROWS BETWEEN 0 PRECEDING AND 0 PRECEDING) + FROM t5 +} + +execsql_test 3.6.1 { + SELECT a, max(c) OVER (ORDER BY a ROWS BETWEEN 2 FOLLOWING AND 1 FOLLOWING) + FROM t5 +} +execsql_test 3.6.2 { + SELECT a, max(c) OVER (ORDER BY a ROWS BETWEEN 1 FOLLOWING AND 1 FOLLOWING) + FROM t5 +} +execsql_test 3.6.3 { + SELECT a, max(c) OVER (ORDER BY a ROWS BETWEEN 0 FOLLOWING AND 0 FOLLOWING) + FROM t5 +} + +========== + +execsql_test 4.0 { + DROP TABLE IF EXISTS ttt; + CREATE TABLE ttt(a INTEGER PRIMARY KEY, b INTEGER, c INTEGER); + INSERT INTO ttt VALUES(1, 1, 1); + INSERT INTO ttt VALUES(2, 2, 2); + INSERT INTO ttt VALUES(3, 3, 3); + + INSERT INTO ttt VALUES(4, 1, 2); + INSERT INTO ttt VALUES(5, 2, 3); + INSERT INTO ttt VALUES(6, 3, 4); + + INSERT INTO ttt VALUES(7, 1, 3); + INSERT INTO ttt VALUES(8, 2, 4); + INSERT INTO ttt VALUES(9, 3, 5); +} + +execsql_test 4.1 { + SELECT max(c), max(b) OVER (ORDER BY b) FROM ttt GROUP BY b; +} + +execsql_test 4.2 { + SELECT max(b) OVER (ORDER BY max(c)) FROM ttt GROUP BY b; +} + +execsql_test 4.3 { + SELECT abs(max(b) OVER (ORDER BY b)) FROM ttt GROUP BY b; +} + +execsql_test 4.4 { + SELECT sum(b) OVER ( + ORDER BY a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + ) FROM ttt; +} + +set lPart [list "PARTITION BY b" "PARTITION BY b, a" "" "PARTITION BY a"] +set lOrder [list "ORDER BY a" "ORDER BY a DESC" "" "ORDER BY b, a"] +set lRange { + "RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW" + "RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING" + "RANGE BETWEEN CURRENT ROW AND CURRENT ROW" + "RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING" +} + +set lRows { + "ROWS BETWEEN 3 PRECEDING AND 1 FOLLOWING" + "ROWS BETWEEN 3 PRECEDING AND 2 FOLLOWING" + "ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING" + "ROWS BETWEEN 0 PRECEDING AND 1 PRECEDING" + "ROWS BETWEEN 1 FOLLOWING AND 500 FOLLOWING" +} + +set tn 1 +set SQL { + SELECT max(c) OVER ($p1 $o1 $r1), + min(c) OVER ($p2 $o2 $r2) + FROM ttt ORDER BY a +} +set SQL2 { + SELECT sum(c) OVER ($p1 $o1 $r1), + sum(c) OVER ($p2 $o2 $r2) + FROM ttt ORDER BY a +} + +set o1 [lindex $lOrder 0] +set o2 [lindex $lOrder 0] +set r1 [lindex $lRange 0] +set r2 [lindex $lRange 0] +foreach p1 $lPart { foreach p2 $lPart { + execsql_test 4.5.$tn.1 [subst $SQL] + execsql_test 4.5.$tn.2 [subst $SQL2] + incr tn +}} + +set o1 [lindex $lOrder 0] +set o2 [lindex $lOrder 0] +set p1 [lindex $lPart 0] +set p2 [lindex $lPart 0] +foreach r1 $lRange { foreach r2 $lRange { + execsql_test 4.5.$tn.1 [subst $SQL] + execsql_test 4.5.$tn.2 [subst $SQL2] + incr tn +}} +foreach r1 $lRows { foreach r2 $lRows { + execsql_test 4.5.$tn.1 [subst $SQL] + execsql_test 4.5.$tn.2 [subst $SQL2] + incr tn +}} + +set r1 [lindex $lRange 0] +set r2 [lindex $lRange 0] +set p1 [lindex $lPart 0] +set p2 [lindex $lPart 0] +foreach o1 $lOrder { foreach o2 $lOrder { + execsql_test 4.5.$tn.1 [subst $SQL] + execsql_test 4.5.$tn.2 [subst $SQL2] + incr tn +}} + +========== + +execsql_test 7.0 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(x INTEGER, y INTEGER); + INSERT INTO t1 VALUES(1, 2); + INSERT INTO t1 VALUES(3, 4); + INSERT INTO t1 VALUES(5, 6); + INSERT INTO t1 VALUES(7, 8); + INSERT INTO t1 VALUES(9, 10); +} + +execsql_test 7.1 { + SELECT lead(y) OVER win FROM t1 + WINDOW win AS (ORDER BY x) +} + +execsql_test 7.2 { + SELECT lead(y, 2) OVER win FROM t1 + WINDOW win AS (ORDER BY x) +} + +execsql_test 7.3 { + SELECT lead(y, 3, -1) OVER win FROM t1 + WINDOW win AS (ORDER BY x) +} + +execsql_test 7.4 { + SELECT + lead(y) OVER win, lead(y) OVER win + FROM t1 + WINDOW win AS (ORDER BY x) +} + +execsql_test 7.5 { + SELECT + lead(y) OVER win, + lead(y, 2) OVER win, + lead(y, 3, -1) OVER win + FROM t1 + WINDOW win AS (ORDER BY x) +} + +========== + +execsql_test 8.0 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a INTEGER, b INTEGER, c INTEGER, d INTEGER); + INSERT INTO t1 VALUES(1, 2, 3, 4); + INSERT INTO t1 VALUES(5, 6, 7, 8); + INSERT INTO t1 VALUES(9, 10, 11, 12); +} + +execsql_test 8.1 { + SELECT row_number() OVER win, + nth_value(d,2) OVER win, + lead(d) OVER win + FROM t1 + WINDOW win AS (ORDER BY a) +} + +execsql_test 8.2 { + SELECT row_number() OVER win, + rank() OVER win, + dense_rank() OVER win, + ntile(2) OVER win, + first_value(d) OVER win, + last_value(d) OVER win, + nth_value(d,2) OVER win, + lead(d) OVER win, + lag(d) OVER win, + max(d) OVER win, + min(d) OVER win + FROM t1 + WINDOW win AS (ORDER BY a) +} + +========== + +execsql_test 9.0 { + DROP TABLE IF EXISTS t2; + CREATE TABLE t2(x INTEGER); + INSERT INTO t2 VALUES(1), (1), (1), (4), (4), (6), (7); +} + +execsql_test 9.1 { + SELECT rank() OVER () FROM t2 +} +execsql_test 9.2 { + SELECT dense_rank() OVER (PARTITION BY x) FROM t2 +} +execsql_float_test 9.3 { + SELECT x, percent_rank() OVER (PARTITION BY x ORDER BY x) FROM t2 +} + +execsql_test 9.4 { + SELECT x, rank() OVER (ORDER BY x) FROM t2 ORDER BY 1,2 +} + +execsql_test 9.5 { + SELECT DISTINCT x, rank() OVER (ORDER BY x) FROM t2 ORDER BY 1,2 +} + +execsql_float_test 9.6 { + SELECT percent_rank() OVER () FROM t1 +} + +execsql_float_test 9.7 { + SELECT cume_dist() OVER () FROM t1 +} + +execsql_test 10.0 { + DROP TABLE IF EXISTS t7; + CREATE TABLE t7(id INTEGER PRIMARY KEY, a INTEGER, b INTEGER); + INSERT INTO t7(id, a, b) VALUES + (1, 1, 2), (2, 1, NULL), (3, 1, 4), + (4, 3, NULL), (5, 3, 8), (6, 3, 1); +} +execsql_test 10.1 { + SELECT id, min(b) OVER (PARTITION BY a ORDER BY id) FROM t7; +} + +execsql_test 10.2 { + SELECT id, lead(b, -1) OVER (PARTITION BY a ORDER BY id) FROM t7; +} +execsql_test 10.3 { + SELECT id, lag(b, -1) OVER (PARTITION BY a ORDER BY id) FROM t7; +} + +execsql_test 11.0 { + DROP VIEW IF EXISTS v8; + DROP TABLE IF EXISTS t8; + CREATE TABLE t8(t INT, total INT); + INSERT INTO t8 VALUES(0,2); + INSERT INTO t8 VALUES(5,1); + INSERT INTO t8 VALUES(10,1); +} + +execsql_test 11.1 { + SELECT NTILE(256) OVER (ORDER BY total) - 1 AS nt FROM t8; +} + +execsql_test 11.2 { + CREATE VIEW v8 AS SELECT NTILE(256) OVER (ORDER BY total) - 1 AS nt FROM t8; +} + +execsql_test 11.3 { + SELECT * FROM v8; +} + +execsql_test 11.4 { + SELECT * FROM ( + SELECT NTILE(256) OVER (ORDER BY total) - 1 AS nt FROM t8 + ) sub; +} + +execsql_test 11.5 { + SELECT sum( min(t) ) OVER () FROM t8 GROUP BY total; +} +execsql_test 11.5 { + SELECT sum( max(t) ) OVER () FROM t8 GROUP BY total; +} + +execsql_test 11.7 { + SELECT sum( min(t) ) OVER () FROM t8; +} +execsql_test 11.8 { + SELECT sum( max(t) ) OVER () FROM t8; +} + +execsql_test 12.0 { + DROP TABLE IF EXISTS t2; + CREATE TABLE t2(a INTEGER); + INSERT INTO t2 VALUES(1), (2), (3); +} + +execsql_test 12.1 { + SELECT (SELECT min(a) OVER ()) FROM t2 +} + +execsql_float_test 12.2 { + SELECT (SELECT avg(a)) FROM t2 ORDER BY 1 +} + +execsql_float_test 12.3 { + SELECT + (SELECT avg(a) UNION SELECT min(a) OVER ()) + FROM t2 GROUP BY a + ORDER BY 1 +} + +finish_test + diff --git a/test/window4.test b/test/window4.test new file mode 100644 index 0000000000..4194f75355 --- /dev/null +++ b/test/window4.test @@ -0,0 +1,1390 @@ +# 2018 June 04 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. +# + +#################################################### +# DO NOT EDIT! THIS FILE IS AUTOMATICALLY GENERATED! +#################################################### + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix window4 + +ifcapable !windowfunc { finish_test ; return } +do_execsql_test 1.0 { + DROP TABLE IF EXISTS t3; + CREATE TABLE t3(a TEXT PRIMARY KEY); + INSERT INTO t3 VALUES('a'), ('b'), ('c'), ('d'), ('e'); + INSERT INTO t3 VALUES('f'), ('g'), ('h'), ('i'), ('j'); +} {} + +do_execsql_test 1.1 { + SELECT a, ntile(1) OVER (ORDER BY a) FROM t3 +} {a 1 b 1 c 1 d 1 e 1 f 1 g 1 h 1 i 1 j 1} + +do_execsql_test 1.2 { + SELECT a, ntile(2) OVER (ORDER BY a) FROM t3 +} {a 1 b 1 c 1 d 1 e 1 f 2 g 2 h 2 i 2 j 2} + +do_execsql_test 1.3 { + SELECT a, ntile(3) OVER (ORDER BY a) FROM t3 +} {a 1 b 1 c 1 d 1 e 2 f 2 g 2 h 3 i 3 j 3} + +do_execsql_test 1.4 { + SELECT a, ntile(4) OVER (ORDER BY a) FROM t3 +} {a 1 b 1 c 1 d 2 e 2 f 2 g 3 h 3 i 4 j 4} + +do_execsql_test 1.5 { + SELECT a, ntile(5) OVER (ORDER BY a) FROM t3 +} {a 1 b 1 c 2 d 2 e 3 f 3 g 4 h 4 i 5 j 5} + +do_execsql_test 1.6 { + SELECT a, ntile(6) OVER (ORDER BY a) FROM t3 +} {a 1 b 1 c 2 d 2 e 3 f 3 g 4 h 4 i 5 j 6} + +do_execsql_test 1.7 { + SELECT a, ntile(7) OVER (ORDER BY a) FROM t3 +} {a 1 b 1 c 2 d 2 e 3 f 3 g 4 h 5 i 6 j 7} + +do_execsql_test 1.8 { + SELECT a, ntile(8) OVER (ORDER BY a) FROM t3 +} {a 1 b 1 c 2 d 2 e 3 f 4 g 5 h 6 i 7 j 8} + +do_execsql_test 1.9 { + SELECT a, ntile(9) OVER (ORDER BY a) FROM t3 +} {a 1 b 1 c 2 d 3 e 4 f 5 g 6 h 7 i 8 j 9} + +do_execsql_test 1.10 { + SELECT a, ntile(10) OVER (ORDER BY a) FROM t3 +} {a 1 b 2 c 3 d 4 e 5 f 6 g 7 h 8 i 9 j 10} + +do_execsql_test 1.11 { + SELECT a, ntile(11) OVER (ORDER BY a) FROM t3 +} {a 1 b 2 c 3 d 4 e 5 f 6 g 7 h 8 i 9 j 10} + +do_execsql_test 1.12 { + SELECT a, ntile(12) OVER (ORDER BY a) FROM t3 +} {a 1 b 2 c 3 d 4 e 5 f 6 g 7 h 8 i 9 j 10} + +do_execsql_test 1.13 { + SELECT a, ntile(13) OVER (ORDER BY a) FROM t3 +} {a 1 b 2 c 3 d 4 e 5 f 6 g 7 h 8 i 9 j 10} + +do_execsql_test 1.14 { + SELECT a, ntile(14) OVER (ORDER BY a) FROM t3 +} {a 1 b 2 c 3 d 4 e 5 f 6 g 7 h 8 i 9 j 10} + +do_execsql_test 1.15 { + SELECT a, ntile(15) OVER (ORDER BY a) FROM t3 +} {a 1 b 2 c 3 d 4 e 5 f 6 g 7 h 8 i 9 j 10} + +do_execsql_test 1.16 { + SELECT a, ntile(16) OVER (ORDER BY a) FROM t3 +} {a 1 b 2 c 3 d 4 e 5 f 6 g 7 h 8 i 9 j 10} + +do_execsql_test 1.17 { + SELECT a, ntile(17) OVER (ORDER BY a) FROM t3 +} {a 1 b 2 c 3 d 4 e 5 f 6 g 7 h 8 i 9 j 10} + +do_execsql_test 1.18 { + SELECT a, ntile(18) OVER (ORDER BY a) FROM t3 +} {a 1 b 2 c 3 d 4 e 5 f 6 g 7 h 8 i 9 j 10} + +do_execsql_test 1.19 { + SELECT a, ntile(19) OVER (ORDER BY a) FROM t3 +} {a 1 b 2 c 3 d 4 e 5 f 6 g 7 h 8 i 9 j 10} + +do_execsql_test 2.0 { + DROP TABLE IF EXISTS t4; + CREATE TABLE t4(a INTEGER PRIMARY KEY, b TEXT, c INTEGER); + INSERT INTO t4 VALUES(1, 'A', 9); + INSERT INTO t4 VALUES(2, 'B', 3); + INSERT INTO t4 VALUES(3, 'C', 2); + INSERT INTO t4 VALUES(4, 'D', 10); + INSERT INTO t4 VALUES(5, 'E', 5); + INSERT INTO t4 VALUES(6, 'F', 1); + INSERT INTO t4 VALUES(7, 'G', 1); + INSERT INTO t4 VALUES(8, 'H', 2); + INSERT INTO t4 VALUES(9, 'I', 10); + INSERT INTO t4 VALUES(10, 'J', 4); +} {} + +do_execsql_test 2.1 { + SELECT a, nth_value(b, c) OVER (ORDER BY a) FROM t4 +} {1 {} 2 {} 3 B 4 {} 5 E 6 A 7 A 8 B 9 {} 10 D} + +do_execsql_test 2.2.1 { + SELECT a, lead(b) OVER (ORDER BY a) FROM t4 +} {1 B 2 C 3 D 4 E 5 F 6 G 7 H 8 I 9 J 10 {}} + +do_execsql_test 2.2.2 { + SELECT a, lead(b, 2) OVER (ORDER BY a) FROM t4 +} {1 C 2 D 3 E 4 F 5 G 6 H 7 I 8 J 9 {} 10 {}} + +do_execsql_test 2.2.3 { + SELECT a, lead(b, 3, 'abc') OVER (ORDER BY a) FROM t4 +} {1 D 2 E 3 F 4 G 5 H 6 I 7 J 8 abc 9 abc 10 abc} + +do_execsql_test 2.3.1 { + SELECT a, lag(b) OVER (ORDER BY a) FROM t4 +} {1 {} 2 A 3 B 4 C 5 D 6 E 7 F 8 G 9 H 10 I} + +do_execsql_test 2.3.2 { + SELECT a, lag(b, 2) OVER (ORDER BY a) FROM t4 +} {1 {} 2 {} 3 A 4 B 5 C 6 D 7 E 8 F 9 G 10 H} + +do_execsql_test 2.3.3 { + SELECT a, lag(b, 3, 'abc') OVER (ORDER BY a) FROM t4 +} {1 abc 2 abc 3 abc 4 A 5 B 6 C 7 D 8 E 9 F 10 G} + +do_execsql_test 2.4.1 { + SELECT group_concat(b, '.') OVER ( + ORDER BY a ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + ) FROM t4 +} {A.B.C.D.E.F.G.H.I.J B.C.D.E.F.G.H.I.J C.D.E.F.G.H.I.J D.E.F.G.H.I.J + E.F.G.H.I.J F.G.H.I.J G.H.I.J H.I.J I.J J} + +do_execsql_test 3.0 { + DROP TABLE IF EXISTS t5; + CREATE TABLE t5(a INTEGER PRIMARY KEY, b TEXT, c TEXT, d INTEGER); + INSERT INTO t5 VALUES(1, 'A', 'one', 5); + INSERT INTO t5 VALUES(2, 'B', 'two', 4); + INSERT INTO t5 VALUES(3, 'A', 'three', 3); + INSERT INTO t5 VALUES(4, 'B', 'four', 2); + INSERT INTO t5 VALUES(5, 'A', 'five', 1); +} {} + +do_execsql_test 3.1 { + SELECT a, nth_value(c, d) OVER (ORDER BY b) FROM t5 +} {1 {} 3 five 5 one 2 two 4 three} + +do_execsql_test 3.2 { + SELECT a, nth_value(c, d) OVER (PARTITION BY b ORDER BY a) FROM t5 +} {1 {} 3 {} 5 one 2 {} 4 four} + +do_execsql_test 3.3 { + SELECT a, count(*) OVER abc, count(*) OVER def FROM t5 + WINDOW abc AS (ORDER BY a), + def AS (ORDER BY a DESC) + ORDER BY a; +} {1 1 5 2 2 4 3 3 3 4 4 2 5 5 1} + +do_execsql_test 3.4 { + SELECT a, max(a) FILTER (WHERE (a%2)=0) OVER w FROM t5 + WINDOW w AS (ORDER BY a) +} {1 {} 2 2 3 2 4 4 5 4} + +do_execsql_test 3.5.1 { + SELECT a, max(c) OVER (ORDER BY a ROWS BETWEEN 1 PRECEDING AND 2 PRECEDING) + FROM t5 +} {1 {} 2 {} 3 {} 4 {} 5 {}} + +do_execsql_test 3.5.2 { + SELECT a, max(c) OVER (ORDER BY a ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING) + FROM t5 +} {1 {} 2 one 3 two 4 three 5 four} + +do_execsql_test 3.5.3 { + SELECT a, max(c) OVER (ORDER BY a ROWS BETWEEN 0 PRECEDING AND 0 PRECEDING) + FROM t5 +} {1 one 2 two 3 three 4 four 5 five} + +do_execsql_test 3.6.1 { + SELECT a, max(c) OVER (ORDER BY a ROWS BETWEEN 2 FOLLOWING AND 1 FOLLOWING) + FROM t5 +} {1 {} 2 {} 3 {} 4 {} 5 {}} + +do_execsql_test 3.6.2 { + SELECT a, max(c) OVER (ORDER BY a ROWS BETWEEN 1 FOLLOWING AND 1 FOLLOWING) + FROM t5 +} {1 two 2 three 3 four 4 five 5 {}} + +do_execsql_test 3.6.3 { + SELECT a, max(c) OVER (ORDER BY a ROWS BETWEEN 0 FOLLOWING AND 0 FOLLOWING) + FROM t5 +} {1 one 2 two 3 three 4 four 5 five} + +#========================================================================== + +do_execsql_test 4.0 { + DROP TABLE IF EXISTS ttt; + CREATE TABLE ttt(a INTEGER PRIMARY KEY, b INTEGER, c INTEGER); + INSERT INTO ttt VALUES(1, 1, 1); + INSERT INTO ttt VALUES(2, 2, 2); + INSERT INTO ttt VALUES(3, 3, 3); + + INSERT INTO ttt VALUES(4, 1, 2); + INSERT INTO ttt VALUES(5, 2, 3); + INSERT INTO ttt VALUES(6, 3, 4); + + INSERT INTO ttt VALUES(7, 1, 3); + INSERT INTO ttt VALUES(8, 2, 4); + INSERT INTO ttt VALUES(9, 3, 5); +} {} + +do_execsql_test 4.1 { + SELECT max(c), max(b) OVER (ORDER BY b) FROM ttt GROUP BY b; +} {3 1 4 2 5 3} + +do_execsql_test 4.2 { + SELECT max(b) OVER (ORDER BY max(c)) FROM ttt GROUP BY b; +} {1 2 3} + +do_execsql_test 4.3 { + SELECT abs(max(b) OVER (ORDER BY b)) FROM ttt GROUP BY b; +} {1 2 3} + +do_execsql_test 4.4 { + SELECT sum(b) OVER ( + ORDER BY a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING + ) FROM ttt; +} {18 17 15 12 11 9 6 5 3} + +do_execsql_test 4.5.1.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + min(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 1 2 2 3 3 2 1 3 2 4 3 3 1 4 2 5 3} + +do_execsql_test 4.5.1.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + sum(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 1 2 2 3 3 3 3 5 5 7 7 6 6 9 9 12 12} + +do_execsql_test 4.5.2.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + min(c) OVER (PARTITION BY b, a ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 1 2 2 3 3 2 2 3 3 4 4 3 3 4 4 5 5} + +do_execsql_test 4.5.2.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + sum(c) OVER (PARTITION BY b, a ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 1 2 2 3 3 3 2 5 3 7 4 6 3 9 4 12 5} + +do_execsql_test 4.5.3.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + min(c) OVER ( ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 1 2 1 3 1 2 1 3 1 4 1 3 1 4 1 5 1} + +do_execsql_test 4.5.3.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + sum(c) OVER ( ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 1 2 3 3 6 3 8 5 11 7 15 6 18 9 22 12 27} + +do_execsql_test 4.5.4.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + min(c) OVER (PARTITION BY a ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 1 2 2 3 3 2 2 3 3 4 4 3 3 4 4 5 5} + +do_execsql_test 4.5.4.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + sum(c) OVER (PARTITION BY a ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 1 2 2 3 3 3 2 5 3 7 4 6 3 9 4 12 5} + +do_execsql_test 4.5.5.1 { + SELECT max(c) OVER (PARTITION BY b, a ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + min(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 1 2 2 3 3 2 1 3 2 4 3 3 1 4 2 5 3} + +do_execsql_test 4.5.5.2 { + SELECT sum(c) OVER (PARTITION BY b, a ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + sum(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 1 2 2 3 3 2 3 3 5 4 7 3 6 4 9 5 12} + +do_execsql_test 4.5.6.1 { + SELECT max(c) OVER (PARTITION BY b, a ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + min(c) OVER (PARTITION BY b, a ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 1 2 2 3 3 2 2 3 3 4 4 3 3 4 4 5 5} + +do_execsql_test 4.5.6.2 { + SELECT sum(c) OVER (PARTITION BY b, a ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + sum(c) OVER (PARTITION BY b, a ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 1 2 2 3 3 2 2 3 3 4 4 3 3 4 4 5 5} + +do_execsql_test 4.5.7.1 { + SELECT max(c) OVER (PARTITION BY b, a ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + min(c) OVER ( ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 1 2 1 3 1 2 1 3 1 4 1 3 1 4 1 5 1} + +do_execsql_test 4.5.7.2 { + SELECT sum(c) OVER (PARTITION BY b, a ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + sum(c) OVER ( ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 1 2 3 3 6 2 8 3 11 4 15 3 18 4 22 5 27} + +do_execsql_test 4.5.8.1 { + SELECT max(c) OVER (PARTITION BY b, a ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + min(c) OVER (PARTITION BY a ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 1 2 2 3 3 2 2 3 3 4 4 3 3 4 4 5 5} + +do_execsql_test 4.5.8.2 { + SELECT sum(c) OVER (PARTITION BY b, a ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + sum(c) OVER (PARTITION BY a ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 1 2 2 3 3 2 2 3 3 4 4 3 3 4 4 5 5} + +do_execsql_test 4.5.9.1 { + SELECT max(c) OVER ( ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + min(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 1 2 2 3 3 3 1 3 2 4 3 4 1 4 2 5 3} + +do_execsql_test 4.5.9.2 { + SELECT sum(c) OVER ( ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + sum(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 1 3 2 6 3 8 3 11 5 15 7 18 6 22 9 27 12} + +do_execsql_test 4.5.10.1 { + SELECT max(c) OVER ( ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + min(c) OVER (PARTITION BY b, a ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 1 2 2 3 3 3 2 3 3 4 4 4 3 4 4 5 5} + +do_execsql_test 4.5.10.2 { + SELECT sum(c) OVER ( ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + sum(c) OVER (PARTITION BY b, a ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 1 3 2 6 3 8 2 11 3 15 4 18 3 22 4 27 5} + +do_execsql_test 4.5.11.1 { + SELECT max(c) OVER ( ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + min(c) OVER ( ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 1 2 1 3 1 3 1 3 1 4 1 4 1 4 1 5 1} + +do_execsql_test 4.5.11.2 { + SELECT sum(c) OVER ( ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + sum(c) OVER ( ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 1 3 3 6 6 8 8 11 11 15 15 18 18 22 22 27 27} + +do_execsql_test 4.5.12.1 { + SELECT max(c) OVER ( ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + min(c) OVER (PARTITION BY a ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 1 2 2 3 3 3 2 3 3 4 4 4 3 4 4 5 5} + +do_execsql_test 4.5.12.2 { + SELECT sum(c) OVER ( ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + sum(c) OVER (PARTITION BY a ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 1 3 2 6 3 8 2 11 3 15 4 18 3 22 4 27 5} + +do_execsql_test 4.5.13.1 { + SELECT max(c) OVER (PARTITION BY a ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + min(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 1 2 2 3 3 2 1 3 2 4 3 3 1 4 2 5 3} + +do_execsql_test 4.5.13.2 { + SELECT sum(c) OVER (PARTITION BY a ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + sum(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 1 2 2 3 3 2 3 3 5 4 7 3 6 4 9 5 12} + +do_execsql_test 4.5.14.1 { + SELECT max(c) OVER (PARTITION BY a ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + min(c) OVER (PARTITION BY b, a ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 1 2 2 3 3 2 2 3 3 4 4 3 3 4 4 5 5} + +do_execsql_test 4.5.14.2 { + SELECT sum(c) OVER (PARTITION BY a ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + sum(c) OVER (PARTITION BY b, a ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 1 2 2 3 3 2 2 3 3 4 4 3 3 4 4 5 5} + +do_execsql_test 4.5.15.1 { + SELECT max(c) OVER (PARTITION BY a ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + min(c) OVER ( ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 1 2 1 3 1 2 1 3 1 4 1 3 1 4 1 5 1} + +do_execsql_test 4.5.15.2 { + SELECT sum(c) OVER (PARTITION BY a ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + sum(c) OVER ( ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 1 2 3 3 6 2 8 3 11 4 15 3 18 4 22 5 27} + +do_execsql_test 4.5.16.1 { + SELECT max(c) OVER (PARTITION BY a ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + min(c) OVER (PARTITION BY a ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 1 2 2 3 3 2 2 3 3 4 4 3 3 4 4 5 5} + +do_execsql_test 4.5.16.2 { + SELECT sum(c) OVER (PARTITION BY a ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + sum(c) OVER (PARTITION BY a ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 1 2 2 3 3 2 2 3 3 4 4 3 3 4 4 5 5} + +do_execsql_test 4.5.17.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + min(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 1 2 2 3 3 2 1 3 2 4 3 3 1 4 2 5 3} + +do_execsql_test 4.5.17.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + sum(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 1 2 2 3 3 3 3 5 5 7 7 6 6 9 9 12 12} + +do_execsql_test 4.5.18.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + min(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) + FROM ttt ORDER BY a +} {1 1 2 2 3 3 2 1 3 2 4 3 3 1 4 2 5 3} + +do_execsql_test 4.5.18.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + sum(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) + FROM ttt ORDER BY a +} {1 6 2 9 3 12 3 6 5 9 7 12 6 6 9 9 12 12} + +do_execsql_test 4.5.19.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + min(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN CURRENT ROW AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 1 2 2 3 3 2 2 3 3 4 4 3 3 4 4 5 5} + +do_execsql_test 4.5.19.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + sum(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN CURRENT ROW AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 1 2 2 3 3 3 2 5 3 7 4 6 3 9 4 12 5} + +do_execsql_test 4.5.20.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + min(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) + FROM ttt ORDER BY a +} {1 1 2 2 3 3 2 2 3 3 4 4 3 3 4 4 5 5} + +do_execsql_test 4.5.20.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + sum(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) + FROM ttt ORDER BY a +} {1 6 2 9 3 12 3 5 5 7 7 9 6 3 9 4 12 5} + +do_execsql_test 4.5.21.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING), + min(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {3 1 4 2 5 3 3 1 4 2 5 3 3 1 4 2 5 3} + +do_execsql_test 4.5.21.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING), + sum(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {6 1 9 2 12 3 6 3 9 5 12 7 6 6 9 9 12 12} + +do_execsql_test 4.5.22.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING), + min(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) + FROM ttt ORDER BY a +} {3 1 4 2 5 3 3 1 4 2 5 3 3 1 4 2 5 3} + +do_execsql_test 4.5.22.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING), + sum(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) + FROM ttt ORDER BY a +} {6 6 9 9 12 12 6 6 9 9 12 12 6 6 9 9 12 12} + +do_execsql_test 4.5.23.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING), + min(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN CURRENT ROW AND CURRENT ROW) + FROM ttt ORDER BY a +} {3 1 4 2 5 3 3 2 4 3 5 4 3 3 4 4 5 5} + +do_execsql_test 4.5.23.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING), + sum(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN CURRENT ROW AND CURRENT ROW) + FROM ttt ORDER BY a +} {6 1 9 2 12 3 6 2 9 3 12 4 6 3 9 4 12 5} + +do_execsql_test 4.5.24.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING), + min(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) + FROM ttt ORDER BY a +} {3 1 4 2 5 3 3 2 4 3 5 4 3 3 4 4 5 5} + +do_execsql_test 4.5.24.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING), + sum(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) + FROM ttt ORDER BY a +} {6 6 9 9 12 12 6 5 9 7 12 9 6 3 9 4 12 5} + +do_execsql_test 4.5.25.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN CURRENT ROW AND CURRENT ROW), + min(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 1 2 2 3 3 2 1 3 2 4 3 3 1 4 2 5 3} + +do_execsql_test 4.5.25.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN CURRENT ROW AND CURRENT ROW), + sum(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 1 2 2 3 3 2 3 3 5 4 7 3 6 4 9 5 12} + +do_execsql_test 4.5.26.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN CURRENT ROW AND CURRENT ROW), + min(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) + FROM ttt ORDER BY a +} {1 1 2 2 3 3 2 1 3 2 4 3 3 1 4 2 5 3} + +do_execsql_test 4.5.26.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN CURRENT ROW AND CURRENT ROW), + sum(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) + FROM ttt ORDER BY a +} {1 6 2 9 3 12 2 6 3 9 4 12 3 6 4 9 5 12} + +do_execsql_test 4.5.27.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN CURRENT ROW AND CURRENT ROW), + min(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN CURRENT ROW AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 1 2 2 3 3 2 2 3 3 4 4 3 3 4 4 5 5} + +do_execsql_test 4.5.27.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN CURRENT ROW AND CURRENT ROW), + sum(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN CURRENT ROW AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 1 2 2 3 3 2 2 3 3 4 4 3 3 4 4 5 5} + +do_execsql_test 4.5.28.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN CURRENT ROW AND CURRENT ROW), + min(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) + FROM ttt ORDER BY a +} {1 1 2 2 3 3 2 2 3 3 4 4 3 3 4 4 5 5} + +do_execsql_test 4.5.28.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN CURRENT ROW AND CURRENT ROW), + sum(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) + FROM ttt ORDER BY a +} {1 6 2 9 3 12 2 5 3 7 4 9 3 3 4 4 5 5} + +do_execsql_test 4.5.29.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING), + min(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {3 1 4 2 5 3 3 1 4 2 5 3 3 1 4 2 5 3} + +do_execsql_test 4.5.29.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING), + sum(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {6 1 9 2 12 3 5 3 7 5 9 7 3 6 4 9 5 12} + +do_execsql_test 4.5.30.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING), + min(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) + FROM ttt ORDER BY a +} {3 1 4 2 5 3 3 1 4 2 5 3 3 1 4 2 5 3} + +do_execsql_test 4.5.30.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING), + sum(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) + FROM ttt ORDER BY a +} {6 6 9 9 12 12 5 6 7 9 9 12 3 6 4 9 5 12} + +do_execsql_test 4.5.31.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING), + min(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN CURRENT ROW AND CURRENT ROW) + FROM ttt ORDER BY a +} {3 1 4 2 5 3 3 2 4 3 5 4 3 3 4 4 5 5} + +do_execsql_test 4.5.31.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING), + sum(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN CURRENT ROW AND CURRENT ROW) + FROM ttt ORDER BY a +} {6 1 9 2 12 3 5 2 7 3 9 4 3 3 4 4 5 5} + +do_execsql_test 4.5.32.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING), + min(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) + FROM ttt ORDER BY a +} {3 1 4 2 5 3 3 2 4 3 5 4 3 3 4 4 5 5} + +do_execsql_test 4.5.32.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING), + sum(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) + FROM ttt ORDER BY a +} {6 6 9 9 12 12 5 5 7 7 9 9 3 3 4 4 5 5} + +do_execsql_test 4.5.33.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 3 PRECEDING AND 1 FOLLOWING), + min(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 3 PRECEDING AND 1 FOLLOWING) + FROM ttt ORDER BY a +} {2 1 3 2 4 3 3 1 4 2 5 3 3 1 4 2 5 3} + +do_execsql_test 4.5.33.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 3 PRECEDING AND 1 FOLLOWING), + sum(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 3 PRECEDING AND 1 FOLLOWING) + FROM ttt ORDER BY a +} {3 3 5 5 7 7 6 6 9 9 12 12 6 6 9 9 12 12} + +do_execsql_test 4.5.34.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 3 PRECEDING AND 1 FOLLOWING), + min(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 3 PRECEDING AND 2 FOLLOWING) + FROM ttt ORDER BY a +} {2 1 3 2 4 3 3 1 4 2 5 3 3 1 4 2 5 3} + +do_execsql_test 4.5.34.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 3 PRECEDING AND 1 FOLLOWING), + sum(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 3 PRECEDING AND 2 FOLLOWING) + FROM ttt ORDER BY a +} {3 6 5 9 7 12 6 6 9 9 12 12 6 6 9 9 12 12} + +do_execsql_test 4.5.35.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 3 PRECEDING AND 1 FOLLOWING), + min(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING) + FROM ttt ORDER BY a +} {2 {} 3 {} 4 {} 3 1 4 2 5 3 3 2 4 3 5 4} + +do_execsql_test 4.5.35.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 3 PRECEDING AND 1 FOLLOWING), + sum(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING) + FROM ttt ORDER BY a +} {3 {} 5 {} 7 {} 6 1 9 2 12 3 6 2 9 3 12 4} + +do_execsql_test 4.5.36.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 3 PRECEDING AND 1 FOLLOWING), + min(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 0 PRECEDING AND 1 PRECEDING) + FROM ttt ORDER BY a +} {2 {} 3 {} 4 {} 3 {} 4 {} 5 {} 3 {} 4 {} 5 {}} + +do_execsql_test 4.5.36.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 3 PRECEDING AND 1 FOLLOWING), + sum(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 0 PRECEDING AND 1 PRECEDING) + FROM ttt ORDER BY a +} {3 {} 5 {} 7 {} 6 {} 9 {} 12 {} 6 {} 9 {} 12 {}} + +do_execsql_test 4.5.37.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 3 PRECEDING AND 1 FOLLOWING), + min(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 1 FOLLOWING AND 500 FOLLOWING) + FROM ttt ORDER BY a +} {2 2 3 3 4 4 3 3 4 4 5 5 3 {} 4 {} 5 {}} + +do_execsql_test 4.5.37.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 3 PRECEDING AND 1 FOLLOWING), + sum(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 1 FOLLOWING AND 500 FOLLOWING) + FROM ttt ORDER BY a +} {3 5 5 7 7 9 6 3 9 4 12 5 6 {} 9 {} 12 {}} + +do_execsql_test 4.5.38.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 3 PRECEDING AND 2 FOLLOWING), + min(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 3 PRECEDING AND 1 FOLLOWING) + FROM ttt ORDER BY a +} {3 1 4 2 5 3 3 1 4 2 5 3 3 1 4 2 5 3} + +do_execsql_test 4.5.38.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 3 PRECEDING AND 2 FOLLOWING), + sum(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 3 PRECEDING AND 1 FOLLOWING) + FROM ttt ORDER BY a +} {6 3 9 5 12 7 6 6 9 9 12 12 6 6 9 9 12 12} + +do_execsql_test 4.5.39.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 3 PRECEDING AND 2 FOLLOWING), + min(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 3 PRECEDING AND 2 FOLLOWING) + FROM ttt ORDER BY a +} {3 1 4 2 5 3 3 1 4 2 5 3 3 1 4 2 5 3} + +do_execsql_test 4.5.39.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 3 PRECEDING AND 2 FOLLOWING), + sum(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 3 PRECEDING AND 2 FOLLOWING) + FROM ttt ORDER BY a +} {6 6 9 9 12 12 6 6 9 9 12 12 6 6 9 9 12 12} + +do_execsql_test 4.5.40.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 3 PRECEDING AND 2 FOLLOWING), + min(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING) + FROM ttt ORDER BY a +} {3 {} 4 {} 5 {} 3 1 4 2 5 3 3 2 4 3 5 4} + +do_execsql_test 4.5.40.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 3 PRECEDING AND 2 FOLLOWING), + sum(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING) + FROM ttt ORDER BY a +} {6 {} 9 {} 12 {} 6 1 9 2 12 3 6 2 9 3 12 4} + +do_execsql_test 4.5.41.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 3 PRECEDING AND 2 FOLLOWING), + min(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 0 PRECEDING AND 1 PRECEDING) + FROM ttt ORDER BY a +} {3 {} 4 {} 5 {} 3 {} 4 {} 5 {} 3 {} 4 {} 5 {}} + +do_execsql_test 4.5.41.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 3 PRECEDING AND 2 FOLLOWING), + sum(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 0 PRECEDING AND 1 PRECEDING) + FROM ttt ORDER BY a +} {6 {} 9 {} 12 {} 6 {} 9 {} 12 {} 6 {} 9 {} 12 {}} + +do_execsql_test 4.5.42.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 3 PRECEDING AND 2 FOLLOWING), + min(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 1 FOLLOWING AND 500 FOLLOWING) + FROM ttt ORDER BY a +} {3 2 4 3 5 4 3 3 4 4 5 5 3 {} 4 {} 5 {}} + +do_execsql_test 4.5.42.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 3 PRECEDING AND 2 FOLLOWING), + sum(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 1 FOLLOWING AND 500 FOLLOWING) + FROM ttt ORDER BY a +} {6 5 9 7 12 9 6 3 9 4 12 5 6 {} 9 {} 12 {}} + +do_execsql_test 4.5.43.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING), + min(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 3 PRECEDING AND 1 FOLLOWING) + FROM ttt ORDER BY a +} {{} 1 {} 2 {} 3 1 1 2 2 3 3 2 1 3 2 4 3} + +do_execsql_test 4.5.43.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING), + sum(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 3 PRECEDING AND 1 FOLLOWING) + FROM ttt ORDER BY a +} {{} 3 {} 5 {} 7 1 6 2 9 3 12 2 6 3 9 4 12} + +do_execsql_test 4.5.44.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING), + min(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 3 PRECEDING AND 2 FOLLOWING) + FROM ttt ORDER BY a +} {{} 1 {} 2 {} 3 1 1 2 2 3 3 2 1 3 2 4 3} + +do_execsql_test 4.5.44.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING), + sum(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 3 PRECEDING AND 2 FOLLOWING) + FROM ttt ORDER BY a +} {{} 6 {} 9 {} 12 1 6 2 9 3 12 2 6 3 9 4 12} + +do_execsql_test 4.5.45.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING), + min(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING) + FROM ttt ORDER BY a +} {{} {} {} {} {} {} 1 1 2 2 3 3 2 2 3 3 4 4} + +do_execsql_test 4.5.45.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING), + sum(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING) + FROM ttt ORDER BY a +} {{} {} {} {} {} {} 1 1 2 2 3 3 2 2 3 3 4 4} + +do_execsql_test 4.5.46.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING), + min(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 0 PRECEDING AND 1 PRECEDING) + FROM ttt ORDER BY a +} {{} {} {} {} {} {} 1 {} 2 {} 3 {} 2 {} 3 {} 4 {}} + +do_execsql_test 4.5.46.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING), + sum(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 0 PRECEDING AND 1 PRECEDING) + FROM ttt ORDER BY a +} {{} {} {} {} {} {} 1 {} 2 {} 3 {} 2 {} 3 {} 4 {}} + +do_execsql_test 4.5.47.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING), + min(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 1 FOLLOWING AND 500 FOLLOWING) + FROM ttt ORDER BY a +} {{} 2 {} 3 {} 4 1 3 2 4 3 5 2 {} 3 {} 4 {}} + +do_execsql_test 4.5.47.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING), + sum(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 1 FOLLOWING AND 500 FOLLOWING) + FROM ttt ORDER BY a +} {{} 5 {} 7 {} 9 1 3 2 4 3 5 2 {} 3 {} 4 {}} + +do_execsql_test 4.5.48.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 0 PRECEDING AND 1 PRECEDING), + min(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 3 PRECEDING AND 1 FOLLOWING) + FROM ttt ORDER BY a +} {{} 1 {} 2 {} 3 {} 1 {} 2 {} 3 {} 1 {} 2 {} 3} + +do_execsql_test 4.5.48.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 0 PRECEDING AND 1 PRECEDING), + sum(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 3 PRECEDING AND 1 FOLLOWING) + FROM ttt ORDER BY a +} {{} 3 {} 5 {} 7 {} 6 {} 9 {} 12 {} 6 {} 9 {} 12} + +do_execsql_test 4.5.49.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 0 PRECEDING AND 1 PRECEDING), + min(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 3 PRECEDING AND 2 FOLLOWING) + FROM ttt ORDER BY a +} {{} 1 {} 2 {} 3 {} 1 {} 2 {} 3 {} 1 {} 2 {} 3} + +do_execsql_test 4.5.49.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 0 PRECEDING AND 1 PRECEDING), + sum(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 3 PRECEDING AND 2 FOLLOWING) + FROM ttt ORDER BY a +} {{} 6 {} 9 {} 12 {} 6 {} 9 {} 12 {} 6 {} 9 {} 12} + +do_execsql_test 4.5.50.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 0 PRECEDING AND 1 PRECEDING), + min(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING) + FROM ttt ORDER BY a +} {{} {} {} {} {} {} {} 1 {} 2 {} 3 {} 2 {} 3 {} 4} + +do_execsql_test 4.5.50.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 0 PRECEDING AND 1 PRECEDING), + sum(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING) + FROM ttt ORDER BY a +} {{} {} {} {} {} {} {} 1 {} 2 {} 3 {} 2 {} 3 {} 4} + +do_execsql_test 4.5.51.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 0 PRECEDING AND 1 PRECEDING), + min(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 0 PRECEDING AND 1 PRECEDING) + FROM ttt ORDER BY a +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 4.5.51.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 0 PRECEDING AND 1 PRECEDING), + sum(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 0 PRECEDING AND 1 PRECEDING) + FROM ttt ORDER BY a +} {{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}} + +do_execsql_test 4.5.52.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 0 PRECEDING AND 1 PRECEDING), + min(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 1 FOLLOWING AND 500 FOLLOWING) + FROM ttt ORDER BY a +} {{} 2 {} 3 {} 4 {} 3 {} 4 {} 5 {} {} {} {} {} {}} + +do_execsql_test 4.5.52.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 0 PRECEDING AND 1 PRECEDING), + sum(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 1 FOLLOWING AND 500 FOLLOWING) + FROM ttt ORDER BY a +} {{} 5 {} 7 {} 9 {} 3 {} 4 {} 5 {} {} {} {} {} {}} + +do_execsql_test 4.5.53.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 1 FOLLOWING AND 500 FOLLOWING), + min(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 3 PRECEDING AND 1 FOLLOWING) + FROM ttt ORDER BY a +} {3 1 4 2 5 3 3 1 4 2 5 3 {} 1 {} 2 {} 3} + +do_execsql_test 4.5.53.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 1 FOLLOWING AND 500 FOLLOWING), + sum(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 3 PRECEDING AND 1 FOLLOWING) + FROM ttt ORDER BY a +} {5 3 7 5 9 7 3 6 4 9 5 12 {} 6 {} 9 {} 12} + +do_execsql_test 4.5.54.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 1 FOLLOWING AND 500 FOLLOWING), + min(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 3 PRECEDING AND 2 FOLLOWING) + FROM ttt ORDER BY a +} {3 1 4 2 5 3 3 1 4 2 5 3 {} 1 {} 2 {} 3} + +do_execsql_test 4.5.54.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 1 FOLLOWING AND 500 FOLLOWING), + sum(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 3 PRECEDING AND 2 FOLLOWING) + FROM ttt ORDER BY a +} {5 6 7 9 9 12 3 6 4 9 5 12 {} 6 {} 9 {} 12} + +do_execsql_test 4.5.55.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 1 FOLLOWING AND 500 FOLLOWING), + min(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING) + FROM ttt ORDER BY a +} {3 {} 4 {} 5 {} 3 1 4 2 5 3 {} 2 {} 3 {} 4} + +do_execsql_test 4.5.55.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 1 FOLLOWING AND 500 FOLLOWING), + sum(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING) + FROM ttt ORDER BY a +} {5 {} 7 {} 9 {} 3 1 4 2 5 3 {} 2 {} 3 {} 4} + +do_execsql_test 4.5.56.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 1 FOLLOWING AND 500 FOLLOWING), + min(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 0 PRECEDING AND 1 PRECEDING) + FROM ttt ORDER BY a +} {3 {} 4 {} 5 {} 3 {} 4 {} 5 {} {} {} {} {} {} {}} + +do_execsql_test 4.5.56.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 1 FOLLOWING AND 500 FOLLOWING), + sum(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 0 PRECEDING AND 1 PRECEDING) + FROM ttt ORDER BY a +} {5 {} 7 {} 9 {} 3 {} 4 {} 5 {} {} {} {} {} {} {}} + +do_execsql_test 4.5.57.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 1 FOLLOWING AND 500 FOLLOWING), + min(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 1 FOLLOWING AND 500 FOLLOWING) + FROM ttt ORDER BY a +} {3 2 4 3 5 4 3 3 4 4 5 5 {} {} {} {} {} {}} + +do_execsql_test 4.5.57.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 1 FOLLOWING AND 500 FOLLOWING), + sum(c) OVER (PARTITION BY b ORDER BY a ROWS BETWEEN 1 FOLLOWING AND 500 FOLLOWING) + FROM ttt ORDER BY a +} {5 5 7 7 9 9 3 3 4 4 5 5 {} {} {} {} {} {}} + +do_execsql_test 4.5.58.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + min(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 1 2 2 3 3 2 1 3 2 4 3 3 1 4 2 5 3} + +do_execsql_test 4.5.58.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + sum(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 1 2 2 3 3 3 3 5 5 7 7 6 6 9 9 12 12} + +do_execsql_test 4.5.59.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + min(c) OVER (PARTITION BY b ORDER BY a DESC RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 1 2 2 3 3 2 2 3 3 4 4 3 3 4 4 5 5} + +do_execsql_test 4.5.59.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + sum(c) OVER (PARTITION BY b ORDER BY a DESC RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 6 2 9 3 12 3 5 5 7 7 9 6 3 9 4 12 5} + +do_execsql_test 4.5.60.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + min(c) OVER (PARTITION BY b RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 1 2 2 3 3 2 1 3 2 4 3 3 1 4 2 5 3} + +do_execsql_test 4.5.60.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + sum(c) OVER (PARTITION BY b RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 6 2 9 3 12 3 6 5 9 7 12 6 6 9 9 12 12} + +do_execsql_test 4.5.61.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + min(c) OVER (PARTITION BY b ORDER BY b, a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 1 2 2 3 3 2 1 3 2 4 3 3 1 4 2 5 3} + +do_execsql_test 4.5.61.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + sum(c) OVER (PARTITION BY b ORDER BY b, a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 1 2 2 3 3 3 3 5 5 7 7 6 6 9 9 12 12} + +do_execsql_test 4.5.62.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY a DESC RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + min(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {3 1 4 2 5 3 3 1 4 2 5 3 3 1 4 2 5 3} + +do_execsql_test 4.5.62.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY a DESC RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + sum(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {6 1 9 2 12 3 5 3 7 5 9 7 3 6 4 9 5 12} + +do_execsql_test 4.5.63.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY a DESC RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + min(c) OVER (PARTITION BY b ORDER BY a DESC RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {3 1 4 2 5 3 3 2 4 3 5 4 3 3 4 4 5 5} + +do_execsql_test 4.5.63.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY a DESC RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + sum(c) OVER (PARTITION BY b ORDER BY a DESC RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {6 6 9 9 12 12 5 5 7 7 9 9 3 3 4 4 5 5} + +do_execsql_test 4.5.64.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY a DESC RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + min(c) OVER (PARTITION BY b RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {3 1 4 2 5 3 3 1 4 2 5 3 3 1 4 2 5 3} + +do_execsql_test 4.5.64.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY a DESC RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + sum(c) OVER (PARTITION BY b RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {6 6 9 9 12 12 5 6 7 9 9 12 3 6 4 9 5 12} + +do_execsql_test 4.5.65.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY a DESC RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + min(c) OVER (PARTITION BY b ORDER BY b, a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {3 1 4 2 5 3 3 1 4 2 5 3 3 1 4 2 5 3} + +do_execsql_test 4.5.65.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY a DESC RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + sum(c) OVER (PARTITION BY b ORDER BY b, a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {6 1 9 2 12 3 5 3 7 5 9 7 3 6 4 9 5 12} + +do_execsql_test 4.5.66.1 { + SELECT max(c) OVER (PARTITION BY b RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + min(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {3 1 4 2 5 3 3 1 4 2 5 3 3 1 4 2 5 3} + +do_execsql_test 4.5.66.2 { + SELECT sum(c) OVER (PARTITION BY b RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + sum(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {6 1 9 2 12 3 6 3 9 5 12 7 6 6 9 9 12 12} + +do_execsql_test 4.5.67.1 { + SELECT max(c) OVER (PARTITION BY b RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + min(c) OVER (PARTITION BY b ORDER BY a DESC RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {3 1 4 2 5 3 3 2 4 3 5 4 3 3 4 4 5 5} + +do_execsql_test 4.5.67.2 { + SELECT sum(c) OVER (PARTITION BY b RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + sum(c) OVER (PARTITION BY b ORDER BY a DESC RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {6 6 9 9 12 12 6 5 9 7 12 9 6 3 9 4 12 5} + +do_execsql_test 4.5.68.1 { + SELECT max(c) OVER (PARTITION BY b RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + min(c) OVER (PARTITION BY b RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {3 1 4 2 5 3 3 1 4 2 5 3 3 1 4 2 5 3} + +do_execsql_test 4.5.68.2 { + SELECT sum(c) OVER (PARTITION BY b RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + sum(c) OVER (PARTITION BY b RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {6 6 9 9 12 12 6 6 9 9 12 12 6 6 9 9 12 12} + +do_execsql_test 4.5.69.1 { + SELECT max(c) OVER (PARTITION BY b RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + min(c) OVER (PARTITION BY b ORDER BY b, a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {3 1 4 2 5 3 3 1 4 2 5 3 3 1 4 2 5 3} + +do_execsql_test 4.5.69.2 { + SELECT sum(c) OVER (PARTITION BY b RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + sum(c) OVER (PARTITION BY b ORDER BY b, a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {6 1 9 2 12 3 6 3 9 5 12 7 6 6 9 9 12 12} + +do_execsql_test 4.5.70.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY b, a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + min(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 1 2 2 3 3 2 1 3 2 4 3 3 1 4 2 5 3} + +do_execsql_test 4.5.70.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY b, a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + sum(c) OVER (PARTITION BY b ORDER BY a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 1 2 2 3 3 3 3 5 5 7 7 6 6 9 9 12 12} + +do_execsql_test 4.5.71.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY b, a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + min(c) OVER (PARTITION BY b ORDER BY a DESC RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 1 2 2 3 3 2 2 3 3 4 4 3 3 4 4 5 5} + +do_execsql_test 4.5.71.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY b, a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + sum(c) OVER (PARTITION BY b ORDER BY a DESC RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 6 2 9 3 12 3 5 5 7 7 9 6 3 9 4 12 5} + +do_execsql_test 4.5.72.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY b, a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + min(c) OVER (PARTITION BY b RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 1 2 2 3 3 2 1 3 2 4 3 3 1 4 2 5 3} + +do_execsql_test 4.5.72.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY b, a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + sum(c) OVER (PARTITION BY b RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 6 2 9 3 12 3 6 5 9 7 12 6 6 9 9 12 12} + +do_execsql_test 4.5.73.1 { + SELECT max(c) OVER (PARTITION BY b ORDER BY b, a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + min(c) OVER (PARTITION BY b ORDER BY b, a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 1 2 2 3 3 2 1 3 2 4 3 3 1 4 2 5 3} + +do_execsql_test 4.5.73.2 { + SELECT sum(c) OVER (PARTITION BY b ORDER BY b, a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), + sum(c) OVER (PARTITION BY b ORDER BY b, a RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + FROM ttt ORDER BY a +} {1 1 2 2 3 3 3 3 5 5 7 7 6 6 9 9 12 12} + +#========================================================================== + +do_execsql_test 7.0 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(x INTEGER, y INTEGER); + INSERT INTO t1 VALUES(1, 2); + INSERT INTO t1 VALUES(3, 4); + INSERT INTO t1 VALUES(5, 6); + INSERT INTO t1 VALUES(7, 8); + INSERT INTO t1 VALUES(9, 10); +} {} + +do_execsql_test 7.1 { + SELECT lead(y) OVER win FROM t1 + WINDOW win AS (ORDER BY x) +} {4 6 8 10 {}} + +do_execsql_test 7.2 { + SELECT lead(y, 2) OVER win FROM t1 + WINDOW win AS (ORDER BY x) +} {6 8 10 {} {}} + +do_execsql_test 7.3 { + SELECT lead(y, 3, -1) OVER win FROM t1 + WINDOW win AS (ORDER BY x) +} {8 10 -1 -1 -1} + +do_execsql_test 7.4 { + SELECT + lead(y) OVER win, lead(y) OVER win + FROM t1 + WINDOW win AS (ORDER BY x) +} {4 4 6 6 8 8 10 10 {} {}} + +do_execsql_test 7.5 { + SELECT + lead(y) OVER win, + lead(y, 2) OVER win, + lead(y, 3, -1) OVER win + FROM t1 + WINDOW win AS (ORDER BY x) +} {4 6 8 6 8 10 8 10 -1 10 {} -1 {} {} -1} + +#========================================================================== + +do_execsql_test 8.0 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a INTEGER, b INTEGER, c INTEGER, d INTEGER); + INSERT INTO t1 VALUES(1, 2, 3, 4); + INSERT INTO t1 VALUES(5, 6, 7, 8); + INSERT INTO t1 VALUES(9, 10, 11, 12); +} {} + +do_execsql_test 8.1 { + SELECT row_number() OVER win, + nth_value(d,2) OVER win, + lead(d) OVER win + FROM t1 + WINDOW win AS (ORDER BY a) +} {1 {} 8 2 8 12 3 8 {}} + +do_execsql_test 8.2 { + SELECT row_number() OVER win, + rank() OVER win, + dense_rank() OVER win, + ntile(2) OVER win, + first_value(d) OVER win, + last_value(d) OVER win, + nth_value(d,2) OVER win, + lead(d) OVER win, + lag(d) OVER win, + max(d) OVER win, + min(d) OVER win + FROM t1 + WINDOW win AS (ORDER BY a) +} {1 1 1 1 4 4 {} 8 {} 4 4 2 2 2 1 4 8 8 12 4 8 4 3 3 3 2 4 12 8 {} 8 12 4} + +#========================================================================== + +do_execsql_test 9.0 { + DROP TABLE IF EXISTS t2; + CREATE TABLE t2(x INTEGER); + INSERT INTO t2 VALUES(1), (1), (1), (4), (4), (6), (7); +} {} + +do_execsql_test 9.1 { + SELECT rank() OVER () FROM t2 +} {1 1 1 1 1 1 1} + +do_execsql_test 9.2 { + SELECT dense_rank() OVER (PARTITION BY x) FROM t2 +} {1 1 1 1 1 1 1} + + +do_test 9.3 { + set myres {} + foreach r [db eval {SELECT x, percent_rank() OVER (PARTITION BY x ORDER BY x) FROM t2}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 0.0000 1.0000 0.0000 1.0000 0.0000 4.0000 0.0000 4.0000 0.0000 6.0000 0.0000 7.0000 0.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + +do_execsql_test 9.4 { + SELECT x, rank() OVER (ORDER BY x) FROM t2 ORDER BY 1,2 +} {1 1 1 1 1 1 4 4 4 4 6 6 7 7} + +do_execsql_test 9.5 { + SELECT DISTINCT x, rank() OVER (ORDER BY x) FROM t2 ORDER BY 1,2 +} {1 1 4 4 6 6 7 7} + + +do_test 9.6 { + set myres {} + foreach r [db eval {SELECT percent_rank() OVER () FROM t1}] { + lappend myres [format %.4f [set r]] + } + set res2 {0.0000 0.0000 0.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 9.7 { + set myres {} + foreach r [db eval {SELECT cume_dist() OVER () FROM t1}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 1.0000 1.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + +do_execsql_test 10.0 { + DROP TABLE IF EXISTS t7; + CREATE TABLE t7(id INTEGER PRIMARY KEY, a INTEGER, b INTEGER); + INSERT INTO t7(id, a, b) VALUES + (1, 1, 2), (2, 1, NULL), (3, 1, 4), + (4, 3, NULL), (5, 3, 8), (6, 3, 1); +} {} + +do_execsql_test 10.1 { + SELECT id, min(b) OVER (PARTITION BY a ORDER BY id) FROM t7; +} {1 2 2 2 3 2 4 {} 5 8 6 1} + +do_execsql_test 10.2 { + SELECT id, lead(b, -1) OVER (PARTITION BY a ORDER BY id) FROM t7; +} {1 {} 2 2 3 {} 4 {} 5 {} 6 8} + +do_execsql_test 10.3 { + SELECT id, lag(b, -1) OVER (PARTITION BY a ORDER BY id) FROM t7; +} {1 {} 2 4 3 {} 4 8 5 1 6 {}} + +do_execsql_test 11.0 { + DROP VIEW IF EXISTS v8; + DROP TABLE IF EXISTS t8; + CREATE TABLE t8(t INT, total INT); + INSERT INTO t8 VALUES(0,2); + INSERT INTO t8 VALUES(5,1); + INSERT INTO t8 VALUES(10,1); +} {} + +do_execsql_test 11.1 { + SELECT NTILE(256) OVER (ORDER BY total) - 1 AS nt FROM t8; +} {0 1 2} + +do_execsql_test 11.2 { + CREATE VIEW v8 AS SELECT NTILE(256) OVER (ORDER BY total) - 1 AS nt FROM t8; +} {} + +do_execsql_test 11.3 { + SELECT * FROM v8; +} {0 1 2} + +do_execsql_test 11.4 { + SELECT * FROM ( + SELECT NTILE(256) OVER (ORDER BY total) - 1 AS nt FROM t8 + ) sub; +} {0 1 2} + +do_execsql_test 11.5 { + SELECT sum( min(t) ) OVER () FROM t8 GROUP BY total; +} {5 5} + +do_execsql_test 11.5 { + SELECT sum( max(t) ) OVER () FROM t8 GROUP BY total; +} {10 10} + +do_execsql_test 11.7 { + SELECT sum( min(t) ) OVER () FROM t8; +} {0} + +do_execsql_test 11.8 { + SELECT sum( max(t) ) OVER () FROM t8; +} {10} + +do_execsql_test 12.0 { + DROP TABLE IF EXISTS t2; + CREATE TABLE t2(a INTEGER); + INSERT INTO t2 VALUES(1), (2), (3); +} {} + +do_execsql_test 12.1 { + SELECT (SELECT min(a) OVER ()) FROM t2 +} {1 2 3} + + +do_test 12.2 { + set myres {} + foreach r [db eval {SELECT (SELECT avg(a)) FROM t2 ORDER BY 1}] { + lappend myres [format %.4f [set r]] + } + set res2 {2.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + + +do_test 12.3 { + set myres {} + foreach r [db eval {SELECT + (SELECT avg(a) UNION SELECT min(a) OVER ()) + FROM t2 GROUP BY a + ORDER BY 1}] { + lappend myres [format %.4f [set r]] + } + set res2 {1.0000 2.0000 3.0000} + set i 0 + foreach r [set myres] r2 [set res2] { + if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} { + error "list element [set i] does not match: got=[set r] expected=[set r2]" + } + incr i + } + set {} {} +} {} + +finish_test diff --git a/test/window5.test b/test/window5.test new file mode 100644 index 0000000000..890d6704b4 --- /dev/null +++ b/test/window5.test @@ -0,0 +1,96 @@ +# 2018 May 8 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. Specifically, +# it tests the sqlite3_create_window_function() API. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix window5 + +ifcapable !windowfunc { + finish_test + return +} + +proc m_step {ctx val} { + lappend ctx $val + return $ctx +} +proc m_value {ctx} { + set lSort [lsort $ctx] + + set nVal [llength $lSort] + set n [expr $nVal/2] + + if {($nVal % 2)==0 && $nVal>0} { + set a [lindex $lSort $n] + set b [lindex $lSort $n-1] + if {($a+$b) % 2} { + set ret [expr ($a+$b)/2.0] + } else { + set ret [expr ($a+$b)/2] + } + } else { + set ret [lindex $lSort $n] + } + return $ret +} +proc m_inverse {ctx val} { + set ctx [lrange $ctx 1 end] + return $ctx +} +proc w_value {ctx} { + lsort $ctx +} + +sqlite3_create_window_function db median m_step m_value m_value m_inverse +sqlite3_create_window_function db win m_step w_value w_value m_inverse + +do_test 0.0 { + test_create_window_function_misuse db +} {} + +do_execsql_test 1.0 { + CREATE TABLE t1(a, b); + INSERT INTO t1 VALUES(4, 'a'); + INSERT INTO t1 VALUES(6, 'b'); + INSERT INTO t1 VALUES(1, 'c'); + INSERT INTO t1 VALUES(5, 'd'); + INSERT INTO t1 VALUES(2, 'e'); + INSERT INTO t1 VALUES(3, 'f'); +} + +do_execsql_test 1.1 { + SELECT win(a) OVER (ORDER BY b), median(a) OVER (ORDER BY b) FROM t1; +} {4 4 {4 6} 5 {1 4 6} 4 {1 4 5 6} 4.5 {1 2 4 5 6} 4 {1 2 3 4 5 6} 3.5} + +test_create_sumint db +do_execsql_test 2.0 { + SELECT sumint(a) OVER (ORDER BY rowid) FROM t1 ORDER BY rowid; +} {4 10 11 16 18 21} + +do_execsql_test 2.1 { + SELECT sumint(a) OVER (ORDER BY rowid ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) FROM t1 ORDER BY rowid; +} {10 11 12 8 10 5} + +test_override_sum db +do_catchsql_test 3.0 { + SELECT sum(a) OVER + (ORDER BY b ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) + FROM t1; +} {1 {sum() may not be used as a window function}} +do_execsql_test 3.1 { + SELECT sum(a) FROM t1; +} {21} + + +finish_test diff --git a/test/window6.test b/test/window6.test new file mode 100644 index 0000000000..b5e677208f --- /dev/null +++ b/test/window6.test @@ -0,0 +1,371 @@ +# 2018 May 8 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. Specifically, +# it tests the sqlite3_create_window_function() API. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix window6 + +ifcapable !windowfunc { + finish_test + return +} + +set setup { + CREATE TABLE %t1(%x, %y %typename); + INSERT INTO %t1 VALUES(1, 'a'); + INSERT INTO %t1 VALUES(2, 'b'); + INSERT INTO %t1 VALUES(3, 'c'); + INSERT INTO %t1 VALUES(4, 'd'); + INSERT INTO %t1 VALUES(5, 'e'); +} + +foreach {tn vars} { + 1 {} + 2 { set A(%t1) over } + 3 { set A(%x) over } + 4 { + set A(%alias) over + set A(%x) following + set A(%y) over + } + 5 { + set A(%t1) over + set A(%x) following + set A(%y) preceding + set A(%w) current + set A(%alias) filter + set A(%typename) window + } + + 6 { + set A(%x) window + } +} { + set A(%t1) t1 + set A(%x) x + set A(%y) y + set A(%w) w + set A(%alias) alias + set A(%typename) integer + eval $vars + + set MAP [array get A] + set setup_sql [string map $MAP $setup] + reset_db + execsql $setup_sql + + do_execsql_test 1.$tn.1 [string map $MAP { + SELECT group_concat(%x, '.') OVER (ORDER BY %y) FROM %t1 + }] {1 1.2 1.2.3 1.2.3.4 1.2.3.4.5} + + do_execsql_test 1.$tn.2 [string map $MAP { + SELECT sum(%x) OVER %w FROM %t1 WINDOW %w AS (ORDER BY %y) + }] {1 3 6 10 15} + + do_execsql_test 1.$tn.3 [string map $MAP { + SELECT sum(%alias.%x) OVER %w FROM %t1 %alias WINDOW %w AS (ORDER BY %y) + }] {1 3 6 10 15} + + do_execsql_test 1.$tn.4 [string map $MAP { + SELECT sum(%x) %alias FROM %t1 + }] {15} +} + + +proc winproc {args} { return "window: $args" } +db func window winproc +do_execsql_test 2.0 { + SELECT window('hello world'); +} {{window: {hello world}}} + +proc wincmp {a b} { string compare $b $a } +db collate window wincmp +do_execsql_test 3.0 { + CREATE TABLE window(x COLLATE window); + INSERT INTO window VALUES('bob'), ('alice'), ('cate'); + SELECT * FROM window ORDER BY x COLLATE window; +} {cate bob alice} +do_execsql_test 3.1 { + DROP TABLE window; + CREATE TABLE x1(x); + INSERT INTO x1 VALUES('bob'), ('alice'), ('cate'); + CREATE INDEX window ON x1(x COLLATE window); + SELECT * FROM x1 ORDER BY x COLLATE window; +} {cate bob alice} + + +do_execsql_test 4.0 { CREATE TABLE t4(x, y); } + +# do_execsql_test 4.1 { PRAGMA parser_trace = 1 } +do_execsql_test 4.1 { + SELECT * FROM t4 window, t4; +} + +#------------------------------------------------------------------------- +reset_db + +do_execsql_test 5.0 { + CREATE TABLE over(x, over); + CREATE TABLE window(x, window); + INSERT INTO over VALUES(1, 2), (3, 4), (5, 6); + INSERT INTO window VALUES(1, 2), (3, 4), (5, 6); + SELECT sum(x) over FROM over +} {9} + +do_execsql_test 5.1 { + SELECT sum(x) over over FROM over WINDOW over AS () +} {9 9 9} + +do_execsql_test 5.2 { + SELECT sum(over) over over over FROM over over WINDOW over AS (ORDER BY over) +} {2 6 12} + +do_execsql_test 5.3 { + SELECT sum(over) over over over FROM over over WINDOW over AS (ORDER BY over); +} {2 6 12} + +do_execsql_test 5.4 { + SELECT sum(window) OVER window window FROM window window window window AS (ORDER BY window); +} {2 6 12} + +do_execsql_test 5.5 { + SELECT count(*) OVER win FROM over + WINDOW win AS (ORDER BY x ROWS BETWEEN +2 FOLLOWING AND +3 FOLLOWING) +} {1 0 0} + +#------------------------------------------------------------------------- +# + +ifcapable !icu { + sqlite3_db_config db SQLITE_DBCONFIG_DQS_DML 1 + do_execsql_test 6.0 { + SELECT LIKE('!', '', '!') x WHERE x; + } {} + do_execsql_test 6.1 { + SELECT LIKE("!","","!")""WHeRE""; + } {} + do_catchsql_test 6.2 { + SELECT LIKE("!","","!")""window""; + } {1 {near "window": syntax error}} +} + +reset_db +do_execsql_test 7.0 { + CREATE TABLE t1(x TEXT); + CREATE INDEX i1 ON t1(x COLLATE nocase); + INSERT INTO t1 VALUES(''); +} + +ifcapable !icu { + do_execsql_test 7.1 { + SELECT count(*) FROM t1 WHERE x LIKE '!' ESCAPE '!'; + } {0} +} + +#------------------------------------------------------------------------- +# +do_execsql_test 8.0 { + CREATE TABLE IF NOT EXISTS "sample" ( + "id" INTEGER NOT NULL PRIMARY KEY, + "counter" INTEGER NOT NULL, + "value" REAL NOT NULL + ); + + INSERT INTO "sample" (counter, value) + VALUES (1, 10.), (1, 20.), (2, 1.), (2, 3.), (3, 100.); +} + +do_execsql_test 8.1 { + SELECT "counter", "value", RANK() OVER w AS "rank" + FROM "sample" + WINDOW w AS (PARTITION BY "counter" ORDER BY "value" DESC) + ORDER BY "counter", RANK() OVER w +} { + 1 20.0 1 1 10.0 2 2 3.0 1 2 1.0 2 3 100.0 1 +} + +do_execsql_test 8.2 { + SELECT "counter", "value", SUM("value") OVER + (ORDER BY "id" ROWS 2 PRECEDING) + FROM "sample" + ORDER BY "id" +} { + 1 10.0 10.0 1 20.0 30.0 2 1.0 31.0 2 3.0 24.0 3 100.0 104.0 +} + +do_execsql_test 8.3 { + SELECT SUM("value") OVER + (ORDER BY "id" ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) + FROM "sample" + ORDER BY "id" +} { + 10.0 30.0 31.0 24.0 104.0 +} + +do_execsql_test 9.0 { + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<5) + SELECT x, group_concat(x) OVER (ORDER BY x ROWS 2 PRECEDING) + FROM c; +} { + 1 1 2 1,2 3 1,2,3 4 2,3,4 5 3,4,5 +} +#do_catchsql_test 9.1 { +# WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<5) +# SELECT x, group_concat(x) OVER (ORDER BY x RANGE 2 PRECEDING) +# FROM c; +#} {1 {RANGE must use only UNBOUNDED or CURRENT ROW}} +# +#do_catchsql_test 9.2 { +# WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<5) +# SELECT x, group_concat(x) OVER (ORDER BY x RANGE BETWEEN UNBOUNDED PRECEDING AND 2 FOLLOWING) +# FROM c; +#} {1 {RANGE must use only UNBOUNDED or CURRENT ROW}} + +do_catchsql_test 9.3 { + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<5) + SELECT count(DISTINCT x) OVER (ORDER BY x) FROM c; +} {1 {DISTINCT is not supported for window functions}} + +do_catchsql_test 9.4 { + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<5) + SELECT count() OVER (ORDER BY x RANGE UNBOUNDED FOLLOWING) FROM c; +} {1 {near "FOLLOWING": syntax error}} + +do_catchsql_test 9.5 { + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<5) + SELECT count() OVER (ORDER BY x RANGE BETWEEN UNBOUNDED FOLLOWING AND UNBOUNDED FOLLOWING) FROM c; +} {1 {near "FOLLOWING": syntax error}} + +do_catchsql_test 9.6 { + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<5) + SELECT count() OVER (ORDER BY x RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED PRECEDING) FROM c; +} {1 {near "PRECEDING": syntax error}} + +foreach {tn frame} { + 1 "BETWEEN CURRENT ROW AND 4 PRECEDING" + 2 "4 FOLLOWING" + 3 "BETWEEN 4 FOLLOWING AND CURRENT ROW" + 4 "BETWEEN 4 FOLLOWING AND 2 PRECEDING" +} { + do_catchsql_test 9.7.$tn " + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<5) + SELECT count() OVER ( + ORDER BY x ROWS $frame + ) FROM c; + " {1 {unsupported frame specification}} +} + +do_catchsql_test 9.8.1 { + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<5) + SELECT count() OVER ( + ORDER BY x ROWS BETWEEN a PRECEDING AND 2 FOLLOWING + ) FROM c; +} {1 {frame starting offset must be a non-negative integer}} +do_catchsql_test 9.8.2 { + WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<5) + SELECT count() OVER ( + ORDER BY x ROWS BETWEEN 2 PRECEDING AND a FOLLOWING + ) FROM c; +} {1 {frame ending offset must be a non-negative integer}} + +do_execsql_test 10.0 { + WITH t1(a,b) AS (VALUES(1,2)) + SELECT count() FILTER (where b<>5) OVER w1 + FROM t1 + WINDOW w1 AS (ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING); +} {1} + +foreach {tn stmt} { + 1 "SELECT nth_value(b, 0) OVER (ORDER BY a) FROM t1" + 2 "SELECT nth_value(b, -1) OVER (ORDER BY a) FROM t1" + 3 "SELECT nth_value(b, '4ab') OVER (ORDER BY a) FROM t1" + 4 "SELECT nth_value(b, NULL) OVER (ORDER BY a) FROM t1" + 5 "SELECT nth_value(b, 8.5) OVER (ORDER BY a) FROM t1" +} { + do_catchsql_test 10.1.$tn " + WITH t1(a,b) AS ( VALUES(1, 2), (2, 3), (3, 4) ) + $stmt + " {1 {second argument to nth_value must be a positive integer}} +} + +foreach {tn stmt res} { + 1 "SELECT nth_value(b, 1) OVER (ORDER BY a) FROM t1" {2 2 2} + 2 "SELECT nth_value(b, 2) OVER (ORDER BY a) FROM t1" {{} 3 3} + 3 "SELECT nth_value(b, '2') OVER (ORDER BY a) FROM t1" {{} 3 3} + 4 "SELECT nth_value(b, 2.0) OVER (ORDER BY a) FROM t1" {{} 3 3} + 5 "SELECT nth_value(b, '2.0') OVER (ORDER BY a) FROM t1" {{} 3 3} + 6 "SELECT nth_value(b, 10000000) OVER (ORDER BY a) FROM t1" {{} {} {}} +} { + do_execsql_test 10.2.$tn " + WITH t1(a,b) AS ( VALUES(1, 2), (2, 3), (3, 4) ) + $stmt + " $res +} + + +#------------------------------------------------------------------------- +# +reset_db +do_execsql_test 11.0 { + CREATE TABLE t1(a INT); + INSERT INTO t1 VALUES(10),(15),(20),(20),(25),(30),(30),(50); + CREATE TABLE t3(x INT, y VARCHAR); + INSERT INTO t3(x,y) VALUES(10,'ten'),('15','fifteen'),(30,'thirty'); +} + +do_execsql_test 11.1 { + SELECT a, (SELECT y FROM t3 WHERE x=a) FROM t1 ORDER BY a; +} { + 10 ten 15 fifteen 20 {} 20 {} 25 {} 30 thirty 30 thirty 50 {} +} + +do_execsql_test 11.2 { + SELECT a, (SELECT y FROM t3 WHERE x=a), sum(a) OVER (ORDER BY a) + FROM t1 ORDER BY a; +} { + 10 ten 10 15 fifteen 25 20 {} 65 20 {} 65 + 25 {} 90 30 thirty 150 30 thirty 150 50 {} 200 +} + +do_execsql_test 11.3.1 { + SELECT a, sum(a) OVER win FROM t1 + WINDOW win AS (ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) +} { + 10 10 15 25 20 45 20 65 25 90 30 120 30 150 50 200 +} +do_execsql_test 11.3.2 { + SELECT a, sum(a) OVER win FROM t1 + WINDOW win AS (ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 0 FOLLOWING) +} { + 10 10 15 25 20 45 20 65 25 90 30 120 30 150 50 200 +} +do_execsql_test 11.3.3 { + SELECT a, sum(a) OVER win FROM t1 + WINDOW win AS (ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 0 PRECEDING) +} { + 10 10 15 25 20 45 20 65 25 90 30 120 30 150 50 200 +} + +do_execsql_test 11.4.1 { + SELECT y, group_concat(y, '.') OVER win FROM t3 + WINDOW win AS ( + ORDER BY y RANGE BETWEEN UNBOUNDED PRECEDING AND 10 PRECEDING + ); +} { + fifteen fifteen + ten fifteen.ten + thirty fifteen.ten.thirty +} + +finish_test diff --git a/test/window7.tcl b/test/window7.tcl new file mode 100644 index 0000000000..56fc60dd7f --- /dev/null +++ b/test/window7.tcl @@ -0,0 +1,91 @@ +# 2018 May 19 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +source [file join [file dirname $argv0] pg_common.tcl] + +#========================================================================= + +start_test window7 "2019 March 01" +ifcapable !windowfunc + +execsql_test 1.0 { + DROP TABLE IF EXISTS t3; + CREATE TABLE t3(a INTEGER, b INTEGER); + INSERT INTO t3 VALUES + (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8, 8), + (9, 9), (0, 10), (1, 11), (2, 12), (3, 13), (4, 14), (5, 15), (6, 16), + (7, 17), (8, 18), (9, 19), (0, 20), (1, 21), (2, 22), (3, 23), (4, 24), + (5, 25), (6, 26), (7, 27), (8, 28), (9, 29), (0, 30), (1, 31), (2, 32), + (3, 33), (4, 34), (5, 35), (6, 36), (7, 37), (8, 38), (9, 39), (0, 40), + (1, 41), (2, 42), (3, 43), (4, 44), (5, 45), (6, 46), (7, 47), (8, 48), + (9, 49), (0, 50), (1, 51), (2, 52), (3, 53), (4, 54), (5, 55), (6, 56), + (7, 57), (8, 58), (9, 59), (0, 60), (1, 61), (2, 62), (3, 63), (4, 64), + (5, 65), (6, 66), (7, 67), (8, 68), (9, 69), (0, 70), (1, 71), (2, 72), + (3, 73), (4, 74), (5, 75), (6, 76), (7, 77), (8, 78), (9, 79), (0, 80), + (1, 81), (2, 82), (3, 83), (4, 84), (5, 85), (6, 86), (7, 87), (8, 88), + (9, 89), (0, 90), (1, 91), (2, 92), (3, 93), (4, 94), (5, 95), (6, 96), + (7, 97), (8, 98), (9, 99), (0, 100); +} + +execsql_test 1.1 { + SELECT a, sum(b) FROM t3 GROUP BY a ORDER BY 1; +} + +execsql_test 1.2 { + SELECT a, sum(b) OVER ( + ORDER BY a GROUPS BETWEEN CURRENT ROW AND CURRENT ROW + ) FROM t3 ORDER BY 1; +} + +execsql_test 1.3 { + SELECT a, sum(b) OVER ( + ORDER BY a GROUPS BETWEEN 0 PRECEDING AND 0 FOLLOWING + ) FROM t3 ORDER BY 1; +} + +execsql_test 1.4 { + SELECT a, sum(b) OVER ( + ORDER BY a GROUPS BETWEEN 2 PRECEDING AND 2 FOLLOWING + ) FROM t3 ORDER BY 1; +} + +execsql_test 1.5 { + SELECT a, sum(b) OVER ( + ORDER BY a RANGE BETWEEN 0 PRECEDING AND 0 FOLLOWING + ) FROM t3 ORDER BY 1; +} + +execsql_test 1.6 { + SELECT a, sum(b) OVER ( + ORDER BY a RANGE BETWEEN 2 PRECEDING AND 2 FOLLOWING + ) FROM t3 ORDER BY 1; +} + +execsql_test 1.7 { + SELECT a, sum(b) OVER ( + ORDER BY a RANGE BETWEEN 2 PRECEDING AND 1 FOLLOWING + ) FROM t3 ORDER BY 1; +} + +execsql_test 1.8.1 { + SELECT a, sum(b) OVER ( + ORDER BY a RANGE BETWEEN 0 PRECEDING AND 1 FOLLOWING + ) FROM t3 ORDER BY 1; +} +execsql_test 1.8.2 { + SELECT a, sum(b) OVER ( + ORDER BY a DESC RANGE BETWEEN 0 PRECEDING AND 1 FOLLOWING + ) FROM t3 ORDER BY 1; +} + +finish_test + diff --git a/test/window7.test b/test/window7.test new file mode 100644 index 0000000000..441e2b126a --- /dev/null +++ b/test/window7.test @@ -0,0 +1,186 @@ +# 2019 March 01 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. +# + +#################################################### +# DO NOT EDIT! THIS FILE IS AUTOMATICALLY GENERATED! +#################################################### + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix window7 + +ifcapable !windowfunc { finish_test ; return } +do_execsql_test 1.0 { + DROP TABLE IF EXISTS t3; + CREATE TABLE t3(a INTEGER, b INTEGER); + INSERT INTO t3 VALUES + (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8, 8), + (9, 9), (0, 10), (1, 11), (2, 12), (3, 13), (4, 14), (5, 15), (6, 16), + (7, 17), (8, 18), (9, 19), (0, 20), (1, 21), (2, 22), (3, 23), (4, 24), + (5, 25), (6, 26), (7, 27), (8, 28), (9, 29), (0, 30), (1, 31), (2, 32), + (3, 33), (4, 34), (5, 35), (6, 36), (7, 37), (8, 38), (9, 39), (0, 40), + (1, 41), (2, 42), (3, 43), (4, 44), (5, 45), (6, 46), (7, 47), (8, 48), + (9, 49), (0, 50), (1, 51), (2, 52), (3, 53), (4, 54), (5, 55), (6, 56), + (7, 57), (8, 58), (9, 59), (0, 60), (1, 61), (2, 62), (3, 63), (4, 64), + (5, 65), (6, 66), (7, 67), (8, 68), (9, 69), (0, 70), (1, 71), (2, 72), + (3, 73), (4, 74), (5, 75), (6, 76), (7, 77), (8, 78), (9, 79), (0, 80), + (1, 81), (2, 82), (3, 83), (4, 84), (5, 85), (6, 86), (7, 87), (8, 88), + (9, 89), (0, 90), (1, 91), (2, 92), (3, 93), (4, 94), (5, 95), (6, 96), + (7, 97), (8, 98), (9, 99), (0, 100); +} {} + +do_execsql_test 1.1 { + SELECT a, sum(b) FROM t3 GROUP BY a ORDER BY 1; +} {0 550 1 460 2 470 3 480 4 490 5 500 6 510 7 520 8 530 + 9 540} + +do_execsql_test 1.2 { + SELECT a, sum(b) OVER ( + ORDER BY a GROUPS BETWEEN CURRENT ROW AND CURRENT ROW + ) FROM t3 ORDER BY 1; +} {0 550 0 550 0 550 0 550 0 550 0 550 0 550 0 550 0 550 + 0 550 1 460 1 460 1 460 1 460 1 460 1 460 1 460 1 460 + 1 460 1 460 2 470 2 470 2 470 2 470 2 470 2 470 2 470 + 2 470 2 470 2 470 3 480 3 480 3 480 3 480 3 480 3 480 + 3 480 3 480 3 480 3 480 4 490 4 490 4 490 4 490 4 490 + 4 490 4 490 4 490 4 490 4 490 5 500 5 500 5 500 5 500 + 5 500 5 500 5 500 5 500 5 500 5 500 6 510 6 510 6 510 + 6 510 6 510 6 510 6 510 6 510 6 510 6 510 7 520 7 520 + 7 520 7 520 7 520 7 520 7 520 7 520 7 520 7 520 8 530 + 8 530 8 530 8 530 8 530 8 530 8 530 8 530 8 530 8 530 + 9 540 9 540 9 540 9 540 9 540 9 540 9 540 9 540 9 540 + 9 540} + +do_execsql_test 1.3 { + SELECT a, sum(b) OVER ( + ORDER BY a GROUPS BETWEEN 0 PRECEDING AND 0 FOLLOWING + ) FROM t3 ORDER BY 1; +} {0 550 0 550 0 550 0 550 0 550 0 550 0 550 0 550 0 550 + 0 550 1 460 1 460 1 460 1 460 1 460 1 460 1 460 1 460 + 1 460 1 460 2 470 2 470 2 470 2 470 2 470 2 470 2 470 + 2 470 2 470 2 470 3 480 3 480 3 480 3 480 3 480 3 480 + 3 480 3 480 3 480 3 480 4 490 4 490 4 490 4 490 4 490 + 4 490 4 490 4 490 4 490 4 490 5 500 5 500 5 500 5 500 + 5 500 5 500 5 500 5 500 5 500 5 500 6 510 6 510 6 510 + 6 510 6 510 6 510 6 510 6 510 6 510 6 510 7 520 7 520 + 7 520 7 520 7 520 7 520 7 520 7 520 7 520 7 520 8 530 + 8 530 8 530 8 530 8 530 8 530 8 530 8 530 8 530 8 530 + 9 540 9 540 9 540 9 540 9 540 9 540 9 540 9 540 9 540 + 9 540} + +do_execsql_test 1.4 { + SELECT a, sum(b) OVER ( + ORDER BY a GROUPS BETWEEN 2 PRECEDING AND 2 FOLLOWING + ) FROM t3 ORDER BY 1; +} {0 1480 0 1480 0 1480 0 1480 0 1480 0 1480 0 1480 0 1480 + 0 1480 0 1480 1 1960 1 1960 1 1960 1 1960 1 1960 1 1960 + 1 1960 1 1960 1 1960 1 1960 2 2450 2 2450 2 2450 2 2450 + 2 2450 2 2450 2 2450 2 2450 2 2450 2 2450 3 2400 3 2400 + 3 2400 3 2400 3 2400 3 2400 3 2400 3 2400 3 2400 3 2400 + 4 2450 4 2450 4 2450 4 2450 4 2450 4 2450 4 2450 4 2450 + 4 2450 4 2450 5 2500 5 2500 5 2500 5 2500 5 2500 5 2500 + 5 2500 5 2500 5 2500 5 2500 6 2550 6 2550 6 2550 6 2550 + 6 2550 6 2550 6 2550 6 2550 6 2550 6 2550 7 2600 7 2600 + 7 2600 7 2600 7 2600 7 2600 7 2600 7 2600 7 2600 7 2600 + 8 2100 8 2100 8 2100 8 2100 8 2100 8 2100 8 2100 8 2100 + 8 2100 8 2100 9 1590 9 1590 9 1590 9 1590 9 1590 9 1590 + 9 1590 9 1590 9 1590 9 1590} + +do_execsql_test 1.5 { + SELECT a, sum(b) OVER ( + ORDER BY a RANGE BETWEEN 0 PRECEDING AND 0 FOLLOWING + ) FROM t3 ORDER BY 1; +} {0 550 0 550 0 550 0 550 0 550 0 550 0 550 0 550 0 550 + 0 550 1 460 1 460 1 460 1 460 1 460 1 460 1 460 1 460 + 1 460 1 460 2 470 2 470 2 470 2 470 2 470 2 470 2 470 + 2 470 2 470 2 470 3 480 3 480 3 480 3 480 3 480 3 480 + 3 480 3 480 3 480 3 480 4 490 4 490 4 490 4 490 4 490 + 4 490 4 490 4 490 4 490 4 490 5 500 5 500 5 500 5 500 + 5 500 5 500 5 500 5 500 5 500 5 500 6 510 6 510 6 510 + 6 510 6 510 6 510 6 510 6 510 6 510 6 510 7 520 7 520 + 7 520 7 520 7 520 7 520 7 520 7 520 7 520 7 520 8 530 + 8 530 8 530 8 530 8 530 8 530 8 530 8 530 8 530 8 530 + 9 540 9 540 9 540 9 540 9 540 9 540 9 540 9 540 9 540 + 9 540} + +do_execsql_test 1.6 { + SELECT a, sum(b) OVER ( + ORDER BY a RANGE BETWEEN 2 PRECEDING AND 2 FOLLOWING + ) FROM t3 ORDER BY 1; +} {0 1480 0 1480 0 1480 0 1480 0 1480 0 1480 0 1480 0 1480 + 0 1480 0 1480 1 1960 1 1960 1 1960 1 1960 1 1960 1 1960 + 1 1960 1 1960 1 1960 1 1960 2 2450 2 2450 2 2450 2 2450 + 2 2450 2 2450 2 2450 2 2450 2 2450 2 2450 3 2400 3 2400 + 3 2400 3 2400 3 2400 3 2400 3 2400 3 2400 3 2400 3 2400 + 4 2450 4 2450 4 2450 4 2450 4 2450 4 2450 4 2450 4 2450 + 4 2450 4 2450 5 2500 5 2500 5 2500 5 2500 5 2500 5 2500 + 5 2500 5 2500 5 2500 5 2500 6 2550 6 2550 6 2550 6 2550 + 6 2550 6 2550 6 2550 6 2550 6 2550 6 2550 7 2600 7 2600 + 7 2600 7 2600 7 2600 7 2600 7 2600 7 2600 7 2600 7 2600 + 8 2100 8 2100 8 2100 8 2100 8 2100 8 2100 8 2100 8 2100 + 8 2100 8 2100 9 1590 9 1590 9 1590 9 1590 9 1590 9 1590 + 9 1590 9 1590 9 1590 9 1590} + +do_execsql_test 1.7 { + SELECT a, sum(b) OVER ( + ORDER BY a RANGE BETWEEN 2 PRECEDING AND 1 FOLLOWING + ) FROM t3 ORDER BY 1; +} {0 1010 0 1010 0 1010 0 1010 0 1010 0 1010 0 1010 0 1010 + 0 1010 0 1010 1 1480 1 1480 1 1480 1 1480 1 1480 1 1480 + 1 1480 1 1480 1 1480 1 1480 2 1960 2 1960 2 1960 2 1960 + 2 1960 2 1960 2 1960 2 1960 2 1960 2 1960 3 1900 3 1900 + 3 1900 3 1900 3 1900 3 1900 3 1900 3 1900 3 1900 3 1900 + 4 1940 4 1940 4 1940 4 1940 4 1940 4 1940 4 1940 4 1940 + 4 1940 4 1940 5 1980 5 1980 5 1980 5 1980 5 1980 5 1980 + 5 1980 5 1980 5 1980 5 1980 6 2020 6 2020 6 2020 6 2020 + 6 2020 6 2020 6 2020 6 2020 6 2020 6 2020 7 2060 7 2060 + 7 2060 7 2060 7 2060 7 2060 7 2060 7 2060 7 2060 7 2060 + 8 2100 8 2100 8 2100 8 2100 8 2100 8 2100 8 2100 8 2100 + 8 2100 8 2100 9 1590 9 1590 9 1590 9 1590 9 1590 9 1590 + 9 1590 9 1590 9 1590 9 1590} + +do_execsql_test 1.8.1 { + SELECT a, sum(b) OVER ( + ORDER BY a RANGE BETWEEN 0 PRECEDING AND 1 FOLLOWING + ) FROM t3 ORDER BY 1; +} {0 1010 0 1010 0 1010 0 1010 0 1010 0 1010 0 1010 0 1010 + 0 1010 0 1010 1 930 1 930 1 930 1 930 1 930 1 930 1 930 + 1 930 1 930 1 930 2 950 2 950 2 950 2 950 2 950 2 950 + 2 950 2 950 2 950 2 950 3 970 3 970 3 970 3 970 3 970 + 3 970 3 970 3 970 3 970 3 970 4 990 4 990 4 990 4 990 + 4 990 4 990 4 990 4 990 4 990 4 990 5 1010 5 1010 5 1010 + 5 1010 5 1010 5 1010 5 1010 5 1010 5 1010 5 1010 6 1030 + 6 1030 6 1030 6 1030 6 1030 6 1030 6 1030 6 1030 6 1030 + 6 1030 7 1050 7 1050 7 1050 7 1050 7 1050 7 1050 7 1050 + 7 1050 7 1050 7 1050 8 1070 8 1070 8 1070 8 1070 8 1070 + 8 1070 8 1070 8 1070 8 1070 8 1070 9 540 9 540 9 540 9 540 + 9 540 9 540 9 540 9 540 9 540 9 540} + +do_execsql_test 1.8.2 { + SELECT a, sum(b) OVER ( + ORDER BY a DESC RANGE BETWEEN 0 PRECEDING AND 1 FOLLOWING + ) FROM t3 ORDER BY 1; +} {0 550 0 550 0 550 0 550 0 550 0 550 0 550 0 550 0 550 + 0 550 1 1010 1 1010 1 1010 1 1010 1 1010 1 1010 1 1010 + 1 1010 1 1010 1 1010 2 930 2 930 2 930 2 930 2 930 2 930 + 2 930 2 930 2 930 2 930 3 950 3 950 3 950 3 950 3 950 + 3 950 3 950 3 950 3 950 3 950 4 970 4 970 4 970 4 970 + 4 970 4 970 4 970 4 970 4 970 4 970 5 990 5 990 5 990 + 5 990 5 990 5 990 5 990 5 990 5 990 5 990 6 1010 6 1010 + 6 1010 6 1010 6 1010 6 1010 6 1010 6 1010 6 1010 6 1010 + 7 1030 7 1030 7 1030 7 1030 7 1030 7 1030 7 1030 7 1030 + 7 1030 7 1030 8 1050 8 1050 8 1050 8 1050 8 1050 8 1050 + 8 1050 8 1050 8 1050 8 1050 9 1070 9 1070 9 1070 9 1070 + 9 1070 9 1070 9 1070 9 1070 9 1070 9 1070} + +finish_test diff --git a/test/window8.tcl b/test/window8.tcl new file mode 100644 index 0000000000..fcb18249ae --- /dev/null +++ b/test/window8.tcl @@ -0,0 +1,557 @@ +# 2018 May 19 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +source [file join [file dirname $argv0] pg_common.tcl] + +#========================================================================= + +start_test window8 "2019 March 01" +ifcapable !windowfunc + +execsql_test 1.0 { + DROP TABLE IF EXISTS t3; + CREATE TABLE t3(a TEXT, b TEXT, c INTEGER); + INSERT INTO t3 VALUES + ('HH', 'bb', 355), ('CC', 'aa', 158), ('BB', 'aa', 399), + ('FF', 'bb', 938), ('HH', 'aa', 480), ('FF', 'bb', 870), + ('JJ', 'aa', 768), ('JJ', 'aa', 899), ('GG', 'bb', 929), + ('II', 'bb', 421), ('GG', 'bb', 844), ('FF', 'bb', 574), + ('CC', 'bb', 822), ('GG', 'bb', 938), ('BB', 'aa', 660), + ('HH', 'aa', 979), ('BB', 'bb', 792), ('DD', 'aa', 845), + ('JJ', 'bb', 354), ('FF', 'bb', 295), ('JJ', 'aa', 234), + ('BB', 'bb', 840), ('AA', 'aa', 934), ('EE', 'aa', 113), + ('AA', 'bb', 309), ('BB', 'aa', 412), ('AA', 'aa', 911), + ('AA', 'bb', 572), ('II', 'aa', 398), ('II', 'bb', 250), + ('II', 'aa', 652), ('BB', 'bb', 633), ('AA', 'aa', 239), + ('FF', 'aa', 670), ('BB', 'bb', 705), ('HH', 'bb', 963), + ('CC', 'bb', 346), ('II', 'bb', 671), ('BB', 'aa', 247), + ('AA', 'aa', 223), ('GG', 'aa', 480), ('HH', 'aa', 790), + ('FF', 'aa', 208), ('BB', 'bb', 711), ('EE', 'aa', 777), + ('DD', 'bb', 716), ('CC', 'aa', 759), ('CC', 'aa', 430), + ('CC', 'aa', 607), ('DD', 'bb', 794), ('GG', 'aa', 148), + ('GG', 'aa', 634), ('JJ', 'bb', 257), ('DD', 'bb', 959), + ('FF', 'bb', 726), ('BB', 'aa', 762), ('JJ', 'bb', 336), + ('GG', 'aa', 335), ('HH', 'bb', 330), ('GG', 'bb', 160), + ('JJ', 'bb', 839), ('FF', 'aa', 618), ('BB', 'aa', 393), + ('EE', 'bb', 629), ('FF', 'aa', 667), ('AA', 'bb', 870), + ('FF', 'bb', 102), ('JJ', 'aa', 113), ('DD', 'aa', 224), + ('AA', 'bb', 627), ('HH', 'bb', 730), ('II', 'bb', 443), + ('HH', 'bb', 133), ('EE', 'bb', 252), ('II', 'bb', 805), + ('BB', 'bb', 786), ('EE', 'bb', 768), ('HH', 'bb', 683), + ('DD', 'bb', 238), ('DD', 'aa', 256); +} + +foreach {tn frame} { + 1 { GROUPS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING } + 2 { GROUPS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW } + 3 { GROUPS BETWEEN UNBOUNDED PRECEDING AND 1 FOLLOWING } + 4 { GROUPS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING } + 5 { GROUPS BETWEEN 1 PRECEDING AND 2 PRECEDING } + 6 { GROUPS BETWEEN 2 PRECEDING AND 1 PRECEDING } + 7 { GROUPS BETWEEN 3 PRECEDING AND 1 PRECEDING } + 8 { GROUPS BETWEEN 3 PRECEDING AND 0 PRECEDING } + 9 { GROUPS BETWEEN 2 PRECEDING AND CURRENT ROW } + 10 { GROUPS BETWEEN 3 PRECEDING AND 0 FOLLOWING } + 11 { GROUPS BETWEEN 2 PRECEDING AND UNBOUNDED FOLLOWING } + 12 { GROUPS BETWEEN CURRENT ROW AND 0 FOLLOWING } + 13 { GROUPS BETWEEN CURRENT ROW AND 1 FOLLOWING } + 14 { GROUPS BETWEEN CURRENT ROW AND 100 FOLLOWING } + 15 { GROUPS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING } + 16 { GROUPS BETWEEN 0 FOLLOWING AND 0 FOLLOWING } + 17 { GROUPS BETWEEN 1 FOLLOWING AND 0 FOLLOWING } + 18 { GROUPS BETWEEN 1 FOLLOWING AND 5 FOLLOWING } + 19 { GROUPS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING } + +} { + execsql_test 1.$tn.1 " + SELECT a, b, sum(c) OVER (ORDER BY a $frame) FROM t3 ORDER BY 1, 2, 3; + " + execsql_test 1.$tn.2 " + SELECT a, b, sum(c) OVER (ORDER BY a,b $frame) FROM t3 ORDER BY 1, 2, 3; + " + execsql_test 1.$tn.3 " + SELECT a, b, rank() OVER (ORDER BY a $frame) FROM t3 ORDER BY 1, 2, 3; + " + execsql_test 1.$tn.4 " + SELECT a, b, max(c) OVER (ORDER BY a,b $frame) FROM t3 ORDER BY 1, 2, 3; + " + execsql_test 1.$tn.5 " + SELECT a, b, min(c) OVER (ORDER BY a,b $frame) FROM t3 ORDER BY 1, 2, 3; + " + + set f2 "$frame EXCLUDE CURRENT ROW" + + execsql_test 1.$tn.6 " + SELECT a, b, sum(c) OVER (ORDER BY a $f2) FROM t3 ORDER BY 1, 2, 3; + " + execsql_test 1.$tn.7 " + SELECT a, b, sum(c) OVER (ORDER BY a,b $f2) FROM t3 ORDER BY 1, 2, 3; + " + + execsql_test 1.$tn.8 " + SELECT a, b, + sum(c) OVER (ORDER BY a $f2), + sum(c) OVER (ORDER BY a $frame), + sum(c) OVER (ORDER BY a,b $f2), + sum(c) OVER (ORDER BY a,b $frame) + FROM t3 ORDER BY 1, 2, 3; + " +} + + +foreach {tn ex} { + 1 { EXCLUDE NO OTHERS } + 2 { EXCLUDE CURRENT ROW } + 3 { EXCLUDE GROUP } + 4 { EXCLUDE TIES } +} { + execsql_test 2.$tn.1 " + SELECT row_number() OVER win + FROM t3 + WINDOW win AS ( + ORDER BY c, b, a + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING $ex + ) + " + + execsql_test 2.$tn.2 " + SELECT nth_value(c, 14) OVER win + FROM t3 + WINDOW win AS ( + ORDER BY c, b, a + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING $ex + ) + " + + execsql_test 2.$tn.3 " + SELECT min(c) OVER win, max(c) OVER win, sum(c) OVER win FROM t3 + WINDOW win AS ( + ORDER BY c, b, a + ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW $ex + ) ORDER BY a, b, c; + " +} + +========== + +execsql_test 3.0 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a REAL, b INTEGER); + INSERT INTO t1 VALUES + (5, 10), (10, 20), (13, 26), (13, 26), + (15, 30), (20, 40), (22,80), (30, 90); +} + +foreach {tn frame} { + 1 { ORDER BY a RANGE BETWEEN 5 PRECEDING AND 5 FOLLOWING } + 2 { ORDER BY a RANGE BETWEEN 10 PRECEDING AND 5 PRECEDING } + 3 { ORDER BY a RANGE BETWEEN 2 FOLLOWING AND 3 FOLLOWING } + 4 { ORDER BY a DESC RANGE BETWEEN 5 PRECEDING AND 5 FOLLOWING } + 5 { ORDER BY a DESC RANGE BETWEEN 10 PRECEDING AND 5 PRECEDING } + 6 { ORDER BY a DESC RANGE BETWEEN 2 FOLLOWING AND 3 FOLLOWING } + + 7 { ORDER BY a RANGE BETWEEN 5.1 PRECEDING AND 5.3 FOLLOWING } + 8 { ORDER BY a RANGE BETWEEN 10.2 PRECEDING AND 5.4 PRECEDING } + 9 { ORDER BY a RANGE BETWEEN 2.6 FOLLOWING AND 3.5 FOLLOWING } + 10 { ORDER BY a DESC RANGE BETWEEN 5.7 PRECEDING AND 5.8 FOLLOWING } + 11 { ORDER BY a DESC RANGE BETWEEN UNBOUNDED PRECEDING AND 5.9 PRECEDING } + 12 { ORDER BY a DESC RANGE BETWEEN 2.1 FOLLOWING AND UNBOUNDED FOLLOWING } + 13 { ORDER BY a RANGE 5.1 PRECEDING } +} { + execsql_test 3.$tn " + SELECT CAST(a AS INTEGER), sum(b) OVER win FROM t1 WINDOW win AS ($frame) + " +} + +========== + +execsql_test 4.0 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a INTEGER, b INTEGER); + INSERT INTO t1 VALUES + (NULL, 1), (NULL, 2), (NULL, 3), (10, 4), (10, 5); +} + +execsql_test 4.1.1 { + SELECT sum(b) OVER ( + ORDER BY a RANGE BETWEEN 5 PRECEDING AND 10 FOLLOWING + ) FROM t1 ORDER BY 1; +} +execsql_test 4.1.2 { + SELECT sum(b) OVER ( + ORDER BY a DESC RANGE BETWEEN 5 PRECEDING AND 10 FOLLOWING + ) FROM t1 ORDER BY 1; +} + +execsql_test 4.2.1 { + SELECT sum(b) OVER ( + ORDER BY a RANGE BETWEEN 5 FOLLOWING AND 10 FOLLOWING + ) FROM t1 ORDER BY 1 NULLS FIRST; +} +execsql_test 4.2.2 { + SELECT sum(b) OVER ( + ORDER BY a RANGE BETWEEN 5 FOLLOWING AND 10 FOLLOWING + ) FROM t1 ORDER BY 1 NULLS LAST; +} + +execsql_test 4.2.3 { + SELECT sum(b) OVER ( + ORDER BY a DESC RANGE BETWEEN 5 FOLLOWING AND 10 FOLLOWING + ) FROM t1 ORDER BY 1 NULLS FIRST; +} +execsql_test 4.2.4 { + SELECT sum(b) OVER ( + ORDER BY a DESC RANGE BETWEEN 5 FOLLOWING AND 10 FOLLOWING + ) FROM t1 ORDER BY 1 NULLS LAST; +} + +execsql_test 4.3.1 { + SELECT sum(b) OVER ( + ORDER BY a NULLS FIRST RANGE BETWEEN UNBOUNDED PRECEDING AND 10 FOLLOWING + ) FROM t1 ORDER BY 1 NULLS FIRST; +} +execsql_test 4.3.2 { + SELECT sum(b) OVER ( + ORDER BY a NULLS LAST RANGE BETWEEN UNBOUNDED PRECEDING AND 10 FOLLOWING + ) FROM t1 ORDER BY 1 NULLS LAST; +} + +execsql_test 4.4.1 { + SELECT sum(b) OVER ( + ORDER BY a NULLS FIRST ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING + ) FROM t1 ORDER BY 1 NULLS FIRST; +} +execsql_test 4.4.2 { + SELECT sum(b) OVER ( + ORDER BY a NULLS LAST ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING + ) FROM t1 ORDER BY 1 NULLS LAST; +} + +execsql_test 4.4.3 { + SELECT sum(b) OVER ( + ORDER BY a DESC NULLS LAST ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING + ) FROM t1 ORDER BY 1 NULLS FIRST; +} +execsql_test 4.4.4 { + SELECT sum(b) OVER ( + ORDER BY a DESC NULLS LAST ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING + ) FROM t1 ORDER BY 1 NULLS LAST; +} + +execsql_test 4.5.1 { + SELECT sum(b) OVER ( + ORDER BY a ASC NULLS LAST RANGE BETWEEN UNBOUNDED PRECEDING AND 10 FOLLOWING + ) FROM t1 ORDER BY 1 NULLS LAST; +} +execsql_test 4.5.2 { + SELECT sum(b) OVER ( + ORDER BY a DESC NULLS FIRST RANGE + BETWEEN UNBOUNDED PRECEDING AND 10 FOLLOWING + ) FROM t1 ORDER BY 1 NULLS LAST; +} + +========== + +execsql_test 5.0 { + INSERT INTO t3 VALUES + (NULL, 'bb', 355), (NULL, 'cc', 158), (NULL, 'aa', 399), + ('JJ', NULL, 839), ('FF', NULL, 618), ('BB', NULL, 393), + (NULL, 'bb', 629), (NULL, NULL, 667), (NULL, NULL, 870); +} + +foreach {tn ex} { + 1 { EXCLUDE NO OTHERS } + 2 { EXCLUDE CURRENT ROW } + 3 { EXCLUDE GROUP } + 4 { EXCLUDE TIES } +} { + foreach {tn2 frame} { + 1 { RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING } + 2 { ORDER BY a NULLS FIRST + RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING } + 3 { PARTITION BY coalesce(a, '') + RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING } + 4 { ORDER BY a NULLS FIRST GROUPS 6 PRECEDING } + 5 { ORDER BY c NULLS FIRST RANGE BETWEEN 6 PRECEDING AND 7 FOLLOWING } + 6 { ORDER BY c NULLS FIRST RANGE BETWEEN 0 PRECEDING AND 0 FOLLOWING } + 7 { ORDER BY c NULLS FIRST, b NULLS FIRST, a NULLS FIRST + ROWS BETWEEN 6 PRECEDING AND UNBOUNDED FOLLOWING } + + 8 { RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING } + 9 { ORDER BY a NULLS LAST + RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING } + 10 { PARTITION BY coalesce(a, '') + RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING } + 11 { ORDER BY a NULLS LAST GROUPS 6 PRECEDING } + 12 { ORDER BY c NULLS LAST RANGE BETWEEN 6 PRECEDING AND 7 FOLLOWING } + 13 { ORDER BY c NULLS LAST RANGE BETWEEN 0 PRECEDING AND 0 FOLLOWING } + 14 { ORDER BY c NULLS LAST, b NULLS LAST, a NULLS LAST + ROWS BETWEEN 6 PRECEDING AND UNBOUNDED FOLLOWING } + } { + execsql_test 5.$tn.$tn2.1 " + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( $frame $ex ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST + " + + execsql_test 5.$tn.$tn2.2 " + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( $frame $ex ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST + " + } +} + +========== + +execsql_test 6.0 { + DROP TABLE IF EXISTS t2; + CREATE TABLE t2(a TEXT, b INTEGER); + INSERT INTO t2 VALUES('A', NULL); + INSERT INTO t2 VALUES('B', NULL); + INSERT INTO t2 VALUES('C', 1); +} + +execsql_test 6.1 { + SELECT string_agg(a, '.') OVER ( + ORDER BY b NULLS FIRST RANGE BETWEEN 7 PRECEDING AND 2 PRECEDING + ) + FROM t2 +} + +execsql_test 6.2 { + SELECT string_agg(a, '.') OVER ( + ORDER BY b DESC NULLS LAST RANGE BETWEEN 7 PRECEDING AND 2 PRECEDING + ) + FROM t2 +} + +========== + +execsql_test 7.0 { + DROP TABLE IF EXISTS t2; + CREATE TABLE t2(a INTEGER, b INTEGER); + + INSERT INTO t2 VALUES(1, 65); + INSERT INTO t2 VALUES(2, NULL); + INSERT INTO t2 VALUES(3, NULL); + INSERT INTO t2 VALUES(4, NULL); + INSERT INTO t2 VALUES(5, 66); + INSERT INTO t2 VALUES(6, 67); +} + +foreach {tn f ex} { + 1 sum "" + 2 min "" + 3 sum "EXCLUDE CURRENT ROW" + 4 max "EXCLUDE CURRENT ROW" +} { +execsql_test 7.$tn.1 " + SELECT $f (a) OVER win FROM t2 + WINDOW win AS ( + ORDER BY b NULLS LAST RANGE BETWEEN 6 FOLLOWING AND UNBOUNDED FOLLOWING + ); +" +execsql_test 7.$tn.2 " + SELECT $f (a) OVER win FROM t2 + WINDOW win AS ( + ORDER BY b NULLS LAST RANGE BETWEEN 1 PRECEDING AND 2 PRECEDING + ); +" +execsql_test 7.$tn.3 " + SELECT $f (a) OVER win FROM t2 + WINDOW win AS ( + ORDER BY b NULLS LAST RANGE BETWEEN 2 FOLLOWING AND 1 FOLLOWING + ); +" +execsql_test 7.$tn.4 " + SELECT $f (a) OVER win FROM t2 + WINDOW win AS ( + ORDER BY b NULLS FIRST RANGE BETWEEN 1 PRECEDING AND 2 PRECEDING + ); +" +execsql_test 7.$tn.5 " + SELECT $f (a) OVER win FROM t2 + WINDOW win AS ( + ORDER BY b NULLS FIRST RANGE BETWEEN 2 FOLLOWING AND 1 FOLLOWING + ); +" + +execsql_test 7.$tn.6 " + SELECT $f (a) OVER win FROM t2 + WINDOW win AS ( + ORDER BY b NULLS LAST RANGE BETWEEN 1000 PRECEDING AND 2 PRECEDING + ); +" +execsql_test 7.$tn.7 " + SELECT $f (a) OVER win FROM t2 + WINDOW win AS ( + ORDER BY b NULLS LAST RANGE BETWEEN 2000 FOLLOWING AND 1000 FOLLOWING + ); +" +execsql_test 7.$tn.8 " + SELECT $f (a) OVER win FROM t2 + WINDOW win AS ( + ORDER BY b NULLS FIRST RANGE BETWEEN 1000 PRECEDING AND 2000 PRECEDING + ); +" +execsql_test 7.$tn.9 " + SELECT $f (a) OVER win FROM t2 + WINDOW win AS ( + ORDER BY b NULLS FIRST RANGE BETWEEN 2000 FOLLOWING AND 1000 FOLLOWING + ); +" +} + +========== + +execsql_test 8.0 { + DROP TABLE IF EXISTS tx; + CREATE TABLE tx(a INTEGER PRIMARY KEY); + INSERT INTO tx VALUES(1), (2), (3), (4), (5), (6); + + DROP TABLE IF EXISTS map; + CREATE TABLE map(v INTEGER PRIMARY KEY, t TEXT); + INSERT INTO map VALUES + (1, 'odd'), (2, 'even'), (3, 'odd'), + (4, 'even'), (5, 'odd'), (6, 'even'); +} + +execsql_test 8.1 { + SELECT sum(a) OVER ( + PARTITION BY ( + SELECT t FROM map WHERE v=a + ) ORDER BY a + ) FROM tx; +} + +execsql_test 8.2 { + SELECT sum(a) OVER win FROM tx + WINDOW win AS ( + PARTITION BY ( + SELECT t FROM map WHERE v=a + ) ORDER BY a + ); +} + +execsql_test 8.3 { + WITH map2 AS ( + SELECT * FROM map + ) + SELECT sum(a) OVER ( + PARTITION BY ( + SELECT t FROM map2 WHERE v=a + ) ORDER BY a + ) FROM tx; +} + +execsql_test 8.4 { + WITH map2 AS ( + SELECT * FROM map + ) + SELECT sum(a) OVER win FROM tx + WINDOW win AS ( + PARTITION BY ( + SELECT t FROM map2 WHERE v=a + ) ORDER BY a + ); +} + +========== + +execsql_test 9.1 { + DROP TABLE IF EXISTS t1; + DROP TABLE IF EXISTS t2; + CREATE TABLE t1(a INTEGER); + CREATE TABLE t2(y INTEGER); +} + +execsql_test 9.2 { + SELECT ( + SELECT max(a) OVER ( ORDER BY (SELECT sum(a) FROM t1) ) + + min(a) OVER() + ) + FROM t1 +} + +========== + +execsql_test 10.0 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a INTEGER, b INTEGER); + INSERT INTO t1 VALUES (10, 1), + (20, -1), + (5, 2), + (15, 0), + (25, 3); +} + +execsql_test 10.1 { + SELECT + a, b, MIN(a) FILTER(WHERE b > 0) OVER win + FROM t1 + WINDOW win AS (ORDER BY a ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING); +} + +execsql_test 10.2 { + SELECT + a, b, MIN(a) FILTER(WHERE b > 0) OVER win + FROM t1 + WINDOW win AS (); +} + +execsql_test 10.3 { + SELECT + a, b, MIN(a) FILTER(WHERE b > 0) OVER win + FROM t1 + WINDOW win AS (ORDER BY a); +} + +execsql_test 10.4 { + SELECT + a, b, MIN(a) OVER win + FROM t1 + WINDOW win AS (ORDER BY a ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING); +} + +========== + +execsql_test 11.0 { + DROP TABLE IF EXISTS t2; + CREATE TABLE t2(a INTEGER, b INTEGER); + INSERT INTO t2 VALUES(1, 12); + INSERT INTO t2 VALUES(2, 10); + INSERT INTO t2 VALUES(3, 15); + INSERT INTO t2 VALUES(4, 22); + INSERT INTO t2 VALUES(5, 1); + INSERT INTO t2 VALUES(6, 4); + INSERT INTO t2 VALUES(7, 7); + INSERT INTO t2 VALUES(8, 6); + INSERT INTO t2 VALUES(9, 22); + INSERT INTO t2 VALUES(10, 2); +} + +execsql_test 11.1 { + SELECT a, min(b) FILTER (WHERE a%2 != 0) OVER win + FROM t2 + WINDOW win AS (ORDER BY a ROWS BETWEEN 2 PRECEDING AND 2 FOLLOWING); +} + +finish_test + + diff --git a/test/window8.test b/test/window8.test new file mode 100644 index 0000000000..b1b28f1c2f --- /dev/null +++ b/test/window8.test @@ -0,0 +1,6606 @@ +# 2019 March 01 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. +# + +#################################################### +# DO NOT EDIT! THIS FILE IS AUTOMATICALLY GENERATED! +#################################################### + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix window8 + +ifcapable !windowfunc { finish_test ; return } +do_execsql_test 1.0 { + DROP TABLE IF EXISTS t3; + CREATE TABLE t3(a TEXT, b TEXT, c INTEGER); + INSERT INTO t3 VALUES + ('HH', 'bb', 355), ('CC', 'aa', 158), ('BB', 'aa', 399), + ('FF', 'bb', 938), ('HH', 'aa', 480), ('FF', 'bb', 870), + ('JJ', 'aa', 768), ('JJ', 'aa', 899), ('GG', 'bb', 929), + ('II', 'bb', 421), ('GG', 'bb', 844), ('FF', 'bb', 574), + ('CC', 'bb', 822), ('GG', 'bb', 938), ('BB', 'aa', 660), + ('HH', 'aa', 979), ('BB', 'bb', 792), ('DD', 'aa', 845), + ('JJ', 'bb', 354), ('FF', 'bb', 295), ('JJ', 'aa', 234), + ('BB', 'bb', 840), ('AA', 'aa', 934), ('EE', 'aa', 113), + ('AA', 'bb', 309), ('BB', 'aa', 412), ('AA', 'aa', 911), + ('AA', 'bb', 572), ('II', 'aa', 398), ('II', 'bb', 250), + ('II', 'aa', 652), ('BB', 'bb', 633), ('AA', 'aa', 239), + ('FF', 'aa', 670), ('BB', 'bb', 705), ('HH', 'bb', 963), + ('CC', 'bb', 346), ('II', 'bb', 671), ('BB', 'aa', 247), + ('AA', 'aa', 223), ('GG', 'aa', 480), ('HH', 'aa', 790), + ('FF', 'aa', 208), ('BB', 'bb', 711), ('EE', 'aa', 777), + ('DD', 'bb', 716), ('CC', 'aa', 759), ('CC', 'aa', 430), + ('CC', 'aa', 607), ('DD', 'bb', 794), ('GG', 'aa', 148), + ('GG', 'aa', 634), ('JJ', 'bb', 257), ('DD', 'bb', 959), + ('FF', 'bb', 726), ('BB', 'aa', 762), ('JJ', 'bb', 336), + ('GG', 'aa', 335), ('HH', 'bb', 330), ('GG', 'bb', 160), + ('JJ', 'bb', 839), ('FF', 'aa', 618), ('BB', 'aa', 393), + ('EE', 'bb', 629), ('FF', 'aa', 667), ('AA', 'bb', 870), + ('FF', 'bb', 102), ('JJ', 'aa', 113), ('DD', 'aa', 224), + ('AA', 'bb', 627), ('HH', 'bb', 730), ('II', 'bb', 443), + ('HH', 'bb', 133), ('EE', 'bb', 252), ('II', 'bb', 805), + ('BB', 'bb', 786), ('EE', 'bb', 768), ('HH', 'bb', 683), + ('DD', 'bb', 238), ('DD', 'aa', 256); +} {} + +do_execsql_test 1.1.1 { + SELECT a, b, sum(c) OVER (ORDER BY a GROUPS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa {} AA aa {} AA aa {} AA aa {} AA bb {} AA bb {} AA bb {} + AA bb {} BB aa 4685 BB aa 4685 BB aa 4685 BB aa 4685 BB aa 4685 + BB aa 4685 BB bb 4685 BB bb 4685 BB bb 4685 BB bb 4685 BB bb 4685 + BB bb 4685 CC aa 12025 CC aa 12025 CC aa 12025 CC aa 12025 + CC bb 12025 CC bb 12025 DD aa 15147 DD aa 15147 DD aa 15147 + DD bb 15147 DD bb 15147 DD bb 15147 DD bb 15147 EE aa 19179 + EE aa 19179 EE bb 19179 EE bb 19179 EE bb 19179 FF aa 21718 + FF aa 21718 FF aa 21718 FF aa 21718 FF bb 21718 FF bb 21718 + FF bb 21718 FF bb 21718 FF bb 21718 FF bb 21718 GG aa 27386 + GG aa 27386 GG aa 27386 GG aa 27386 GG bb 27386 GG bb 27386 + GG bb 27386 GG bb 27386 HH aa 31854 HH aa 31854 HH aa 31854 + HH bb 31854 HH bb 31854 HH bb 31854 HH bb 31854 HH bb 31854 + HH bb 31854 II aa 37297 II aa 37297 II bb 37297 II bb 37297 + II bb 37297 II bb 37297 II bb 37297 JJ aa 40937 JJ aa 40937 + JJ aa 40937 JJ aa 40937 JJ bb 40937 JJ bb 40937 JJ bb 40937 + JJ bb 40937} + +do_execsql_test 1.1.2 { + SELECT a, b, sum(c) OVER (ORDER BY a,b GROUPS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa {} AA aa {} AA aa {} AA aa {} AA bb 2307 AA bb 2307 + AA bb 2307 AA bb 2307 BB aa 4685 BB aa 4685 BB aa 4685 BB aa 4685 + BB aa 4685 BB aa 4685 BB bb 7558 BB bb 7558 BB bb 7558 BB bb 7558 + BB bb 7558 BB bb 7558 CC aa 12025 CC aa 12025 CC aa 12025 + CC aa 12025 CC bb 13979 CC bb 13979 DD aa 15147 DD aa 15147 + DD aa 15147 DD bb 16472 DD bb 16472 DD bb 16472 DD bb 16472 + EE aa 19179 EE aa 19179 EE bb 20069 EE bb 20069 EE bb 20069 + FF aa 21718 FF aa 21718 FF aa 21718 FF aa 21718 FF bb 23881 + FF bb 23881 FF bb 23881 FF bb 23881 FF bb 23881 FF bb 23881 + GG aa 27386 GG aa 27386 GG aa 27386 GG aa 27386 GG bb 28983 + GG bb 28983 GG bb 28983 GG bb 28983 HH aa 31854 HH aa 31854 + HH aa 31854 HH bb 34103 HH bb 34103 HH bb 34103 HH bb 34103 + HH bb 34103 HH bb 34103 II aa 37297 II aa 37297 II bb 38347 + II bb 38347 II bb 38347 II bb 38347 II bb 38347 JJ aa 40937 + JJ aa 40937 JJ aa 40937 JJ aa 40937 JJ bb 42951 JJ bb 42951 + JJ bb 42951 JJ bb 42951} + +do_execsql_test 1.1.3 { + SELECT a, b, rank() OVER (ORDER BY a GROUPS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 1 AA aa 1 AA aa 1 AA aa 1 AA bb 1 AA bb 1 AA bb 1 + AA bb 1 BB aa 9 BB aa 9 BB aa 9 BB aa 9 BB aa 9 BB aa 9 + BB bb 9 BB bb 9 BB bb 9 BB bb 9 BB bb 9 BB bb 9 CC aa 21 + CC aa 21 CC aa 21 CC aa 21 CC bb 21 CC bb 21 DD aa 27 DD aa 27 + DD aa 27 DD bb 27 DD bb 27 DD bb 27 DD bb 27 EE aa 34 EE aa 34 + EE bb 34 EE bb 34 EE bb 34 FF aa 39 FF aa 39 FF aa 39 FF aa 39 + FF bb 39 FF bb 39 FF bb 39 FF bb 39 FF bb 39 FF bb 39 GG aa 49 + GG aa 49 GG aa 49 GG aa 49 GG bb 49 GG bb 49 GG bb 49 GG bb 49 + HH aa 57 HH aa 57 HH aa 57 HH bb 57 HH bb 57 HH bb 57 HH bb 57 + HH bb 57 HH bb 57 II aa 66 II aa 66 II bb 66 II bb 66 II bb 66 + II bb 66 II bb 66 JJ aa 73 JJ aa 73 JJ aa 73 JJ aa 73 JJ bb 73 + JJ bb 73 JJ bb 73 JJ bb 73} + +do_execsql_test 1.1.4 { + SELECT a, b, max(c) OVER (ORDER BY a,b GROUPS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa {} AA aa {} AA aa {} AA aa {} AA bb 934 AA bb 934 + AA bb 934 AA bb 934 BB aa 934 BB aa 934 BB aa 934 BB aa 934 + BB aa 934 BB aa 934 BB bb 934 BB bb 934 BB bb 934 BB bb 934 + BB bb 934 BB bb 934 CC aa 934 CC aa 934 CC aa 934 CC aa 934 + CC bb 934 CC bb 934 DD aa 934 DD aa 934 DD aa 934 DD bb 934 + DD bb 934 DD bb 934 DD bb 934 EE aa 959 EE aa 959 EE bb 959 + EE bb 959 EE bb 959 FF aa 959 FF aa 959 FF aa 959 FF aa 959 + FF bb 959 FF bb 959 FF bb 959 FF bb 959 FF bb 959 FF bb 959 + GG aa 959 GG aa 959 GG aa 959 GG aa 959 GG bb 959 GG bb 959 + GG bb 959 GG bb 959 HH aa 959 HH aa 959 HH aa 959 HH bb 979 + HH bb 979 HH bb 979 HH bb 979 HH bb 979 HH bb 979 II aa 979 + II aa 979 II bb 979 II bb 979 II bb 979 II bb 979 II bb 979 + JJ aa 979 JJ aa 979 JJ aa 979 JJ aa 979 JJ bb 979 JJ bb 979 + JJ bb 979 JJ bb 979} + +do_execsql_test 1.1.5 { + SELECT a, b, min(c) OVER (ORDER BY a,b GROUPS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa {} AA aa {} AA aa {} AA aa {} AA bb 223 AA bb 223 + AA bb 223 AA bb 223 BB aa 223 BB aa 223 BB aa 223 BB aa 223 + BB aa 223 BB aa 223 BB bb 223 BB bb 223 BB bb 223 BB bb 223 + BB bb 223 BB bb 223 CC aa 223 CC aa 223 CC aa 223 CC aa 223 + CC bb 158 CC bb 158 DD aa 158 DD aa 158 DD aa 158 DD bb 158 + DD bb 158 DD bb 158 DD bb 158 EE aa 158 EE aa 158 EE bb 113 + EE bb 113 EE bb 113 FF aa 113 FF aa 113 FF aa 113 FF aa 113 + FF bb 113 FF bb 113 FF bb 113 FF bb 113 FF bb 113 FF bb 113 + GG aa 102 GG aa 102 GG aa 102 GG aa 102 GG bb 102 GG bb 102 + GG bb 102 GG bb 102 HH aa 102 HH aa 102 HH aa 102 HH bb 102 + HH bb 102 HH bb 102 HH bb 102 HH bb 102 HH bb 102 II aa 102 + II aa 102 II bb 102 II bb 102 II bb 102 II bb 102 II bb 102 + JJ aa 102 JJ aa 102 JJ aa 102 JJ aa 102 JJ bb 102 JJ bb 102 + JJ bb 102 JJ bb 102} + +do_execsql_test 1.1.6 { + SELECT a, b, sum(c) OVER (ORDER BY a GROUPS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING EXCLUDE CURRENT ROW) FROM t3 ORDER BY 1, 2, 3; +} {AA aa {} AA aa {} AA aa {} AA aa {} AA bb {} AA bb {} AA bb {} + AA bb {} BB aa 4685 BB aa 4685 BB aa 4685 BB aa 4685 BB aa 4685 + BB aa 4685 BB bb 4685 BB bb 4685 BB bb 4685 BB bb 4685 BB bb 4685 + BB bb 4685 CC aa 12025 CC aa 12025 CC aa 12025 CC aa 12025 + CC bb 12025 CC bb 12025 DD aa 15147 DD aa 15147 DD aa 15147 + DD bb 15147 DD bb 15147 DD bb 15147 DD bb 15147 EE aa 19179 + EE aa 19179 EE bb 19179 EE bb 19179 EE bb 19179 FF aa 21718 + FF aa 21718 FF aa 21718 FF aa 21718 FF bb 21718 FF bb 21718 + FF bb 21718 FF bb 21718 FF bb 21718 FF bb 21718 GG aa 27386 + GG aa 27386 GG aa 27386 GG aa 27386 GG bb 27386 GG bb 27386 + GG bb 27386 GG bb 27386 HH aa 31854 HH aa 31854 HH aa 31854 + HH bb 31854 HH bb 31854 HH bb 31854 HH bb 31854 HH bb 31854 + HH bb 31854 II aa 37297 II aa 37297 II bb 37297 II bb 37297 + II bb 37297 II bb 37297 II bb 37297 JJ aa 40937 JJ aa 40937 + JJ aa 40937 JJ aa 40937 JJ bb 40937 JJ bb 40937 JJ bb 40937 + JJ bb 40937} + +do_execsql_test 1.1.7 { + SELECT a, b, sum(c) OVER (ORDER BY a,b GROUPS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING EXCLUDE CURRENT ROW) FROM t3 ORDER BY 1, 2, 3; +} {AA aa {} AA aa {} AA aa {} AA aa {} AA bb 2307 AA bb 2307 + AA bb 2307 AA bb 2307 BB aa 4685 BB aa 4685 BB aa 4685 BB aa 4685 + BB aa 4685 BB aa 4685 BB bb 7558 BB bb 7558 BB bb 7558 BB bb 7558 + BB bb 7558 BB bb 7558 CC aa 12025 CC aa 12025 CC aa 12025 + CC aa 12025 CC bb 13979 CC bb 13979 DD aa 15147 DD aa 15147 + DD aa 15147 DD bb 16472 DD bb 16472 DD bb 16472 DD bb 16472 + EE aa 19179 EE aa 19179 EE bb 20069 EE bb 20069 EE bb 20069 + FF aa 21718 FF aa 21718 FF aa 21718 FF aa 21718 FF bb 23881 + FF bb 23881 FF bb 23881 FF bb 23881 FF bb 23881 FF bb 23881 + GG aa 27386 GG aa 27386 GG aa 27386 GG aa 27386 GG bb 28983 + GG bb 28983 GG bb 28983 GG bb 28983 HH aa 31854 HH aa 31854 + HH aa 31854 HH bb 34103 HH bb 34103 HH bb 34103 HH bb 34103 + HH bb 34103 HH bb 34103 II aa 37297 II aa 37297 II bb 38347 + II bb 38347 II bb 38347 II bb 38347 II bb 38347 JJ aa 40937 + JJ aa 40937 JJ aa 40937 JJ aa 40937 JJ bb 42951 JJ bb 42951 + JJ bb 42951 JJ bb 42951} + +do_execsql_test 1.1.8 { + SELECT a, b, + sum(c) OVER (ORDER BY a GROUPS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING EXCLUDE CURRENT ROW), + sum(c) OVER (ORDER BY a GROUPS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING ), + sum(c) OVER (ORDER BY a,b GROUPS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING EXCLUDE CURRENT ROW), + sum(c) OVER (ORDER BY a,b GROUPS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING ) + FROM t3 ORDER BY 1, 2, 3; +} {AA aa {} {} {} {} AA aa {} {} {} {} AA aa {} {} {} {} + AA aa {} {} {} {} AA bb {} {} 2307 2307 AA bb {} {} 2307 2307 + AA bb {} {} 2307 2307 AA bb {} {} 2307 2307 BB aa 4685 4685 4685 4685 + BB aa 4685 4685 4685 4685 BB aa 4685 4685 4685 4685 + BB aa 4685 4685 4685 4685 BB aa 4685 4685 4685 4685 + BB aa 4685 4685 4685 4685 BB bb 4685 4685 7558 7558 + BB bb 4685 4685 7558 7558 BB bb 4685 4685 7558 7558 + BB bb 4685 4685 7558 7558 BB bb 4685 4685 7558 7558 + BB bb 4685 4685 7558 7558 CC aa 12025 12025 12025 12025 + CC aa 12025 12025 12025 12025 CC aa 12025 12025 12025 12025 + CC aa 12025 12025 12025 12025 CC bb 12025 12025 13979 13979 + CC bb 12025 12025 13979 13979 DD aa 15147 15147 15147 15147 + DD aa 15147 15147 15147 15147 DD aa 15147 15147 15147 15147 + DD bb 15147 15147 16472 16472 DD bb 15147 15147 16472 16472 + DD bb 15147 15147 16472 16472 DD bb 15147 15147 16472 16472 + EE aa 19179 19179 19179 19179 EE aa 19179 19179 19179 19179 + EE bb 19179 19179 20069 20069 EE bb 19179 19179 20069 20069 + EE bb 19179 19179 20069 20069 FF aa 21718 21718 21718 21718 + FF aa 21718 21718 21718 21718 FF aa 21718 21718 21718 21718 + FF aa 21718 21718 21718 21718 FF bb 21718 21718 23881 23881 + FF bb 21718 21718 23881 23881 FF bb 21718 21718 23881 23881 + FF bb 21718 21718 23881 23881 FF bb 21718 21718 23881 23881 + FF bb 21718 21718 23881 23881 GG aa 27386 27386 27386 27386 + GG aa 27386 27386 27386 27386 GG aa 27386 27386 27386 27386 + GG aa 27386 27386 27386 27386 GG bb 27386 27386 28983 28983 + GG bb 27386 27386 28983 28983 GG bb 27386 27386 28983 28983 + GG bb 27386 27386 28983 28983 HH aa 31854 31854 31854 31854 + HH aa 31854 31854 31854 31854 HH aa 31854 31854 31854 31854 + HH bb 31854 31854 34103 34103 HH bb 31854 31854 34103 34103 + HH bb 31854 31854 34103 34103 HH bb 31854 31854 34103 34103 + HH bb 31854 31854 34103 34103 HH bb 31854 31854 34103 34103 + II aa 37297 37297 37297 37297 II aa 37297 37297 37297 37297 + II bb 37297 37297 38347 38347 II bb 37297 37297 38347 38347 + II bb 37297 37297 38347 38347 II bb 37297 37297 38347 38347 + II bb 37297 37297 38347 38347 JJ aa 40937 40937 40937 40937 + JJ aa 40937 40937 40937 40937 JJ aa 40937 40937 40937 40937 + JJ aa 40937 40937 40937 40937 JJ bb 40937 40937 42951 42951 + JJ bb 40937 40937 42951 42951 JJ bb 40937 40937 42951 42951 + JJ bb 40937 40937 42951 42951} + +do_execsql_test 1.2.1 { + SELECT a, b, sum(c) OVER (ORDER BY a GROUPS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 4685 AA aa 4685 AA aa 4685 AA aa 4685 AA bb 4685 AA bb 4685 + AA bb 4685 AA bb 4685 BB aa 12025 BB aa 12025 BB aa 12025 + BB aa 12025 BB aa 12025 BB aa 12025 BB bb 12025 BB bb 12025 + BB bb 12025 BB bb 12025 BB bb 12025 BB bb 12025 CC aa 15147 + CC aa 15147 CC aa 15147 CC aa 15147 CC bb 15147 CC bb 15147 + DD aa 19179 DD aa 19179 DD aa 19179 DD bb 19179 DD bb 19179 + DD bb 19179 DD bb 19179 EE aa 21718 EE aa 21718 EE bb 21718 + EE bb 21718 EE bb 21718 FF aa 27386 FF aa 27386 FF aa 27386 + FF aa 27386 FF bb 27386 FF bb 27386 FF bb 27386 FF bb 27386 + FF bb 27386 FF bb 27386 GG aa 31854 GG aa 31854 GG aa 31854 + GG aa 31854 GG bb 31854 GG bb 31854 GG bb 31854 GG bb 31854 + HH aa 37297 HH aa 37297 HH aa 37297 HH bb 37297 HH bb 37297 + HH bb 37297 HH bb 37297 HH bb 37297 HH bb 37297 II aa 40937 + II aa 40937 II bb 40937 II bb 40937 II bb 40937 II bb 40937 + II bb 40937 JJ aa 44737 JJ aa 44737 JJ aa 44737 JJ aa 44737 + JJ bb 44737 JJ bb 44737 JJ bb 44737 JJ bb 44737} + +do_execsql_test 1.2.2 { + SELECT a, b, sum(c) OVER (ORDER BY a,b GROUPS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 2307 AA aa 2307 AA aa 2307 AA aa 2307 AA bb 4685 AA bb 4685 + AA bb 4685 AA bb 4685 BB aa 7558 BB aa 7558 BB aa 7558 BB aa 7558 + BB aa 7558 BB aa 7558 BB bb 12025 BB bb 12025 BB bb 12025 + BB bb 12025 BB bb 12025 BB bb 12025 CC aa 13979 CC aa 13979 + CC aa 13979 CC aa 13979 CC bb 15147 CC bb 15147 DD aa 16472 + DD aa 16472 DD aa 16472 DD bb 19179 DD bb 19179 DD bb 19179 + DD bb 19179 EE aa 20069 EE aa 20069 EE bb 21718 EE bb 21718 + EE bb 21718 FF aa 23881 FF aa 23881 FF aa 23881 FF aa 23881 + FF bb 27386 FF bb 27386 FF bb 27386 FF bb 27386 FF bb 27386 + FF bb 27386 GG aa 28983 GG aa 28983 GG aa 28983 GG aa 28983 + GG bb 31854 GG bb 31854 GG bb 31854 GG bb 31854 HH aa 34103 + HH aa 34103 HH aa 34103 HH bb 37297 HH bb 37297 HH bb 37297 + HH bb 37297 HH bb 37297 HH bb 37297 II aa 38347 II aa 38347 + II bb 40937 II bb 40937 II bb 40937 II bb 40937 II bb 40937 + JJ aa 42951 JJ aa 42951 JJ aa 42951 JJ aa 42951 JJ bb 44737 + JJ bb 44737 JJ bb 44737 JJ bb 44737} + +do_execsql_test 1.2.3 { + SELECT a, b, rank() OVER (ORDER BY a GROUPS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 1 AA aa 1 AA aa 1 AA aa 1 AA bb 1 AA bb 1 AA bb 1 + AA bb 1 BB aa 9 BB aa 9 BB aa 9 BB aa 9 BB aa 9 BB aa 9 + BB bb 9 BB bb 9 BB bb 9 BB bb 9 BB bb 9 BB bb 9 CC aa 21 + CC aa 21 CC aa 21 CC aa 21 CC bb 21 CC bb 21 DD aa 27 DD aa 27 + DD aa 27 DD bb 27 DD bb 27 DD bb 27 DD bb 27 EE aa 34 EE aa 34 + EE bb 34 EE bb 34 EE bb 34 FF aa 39 FF aa 39 FF aa 39 FF aa 39 + FF bb 39 FF bb 39 FF bb 39 FF bb 39 FF bb 39 FF bb 39 GG aa 49 + GG aa 49 GG aa 49 GG aa 49 GG bb 49 GG bb 49 GG bb 49 GG bb 49 + HH aa 57 HH aa 57 HH aa 57 HH bb 57 HH bb 57 HH bb 57 HH bb 57 + HH bb 57 HH bb 57 II aa 66 II aa 66 II bb 66 II bb 66 II bb 66 + II bb 66 II bb 66 JJ aa 73 JJ aa 73 JJ aa 73 JJ aa 73 JJ bb 73 + JJ bb 73 JJ bb 73 JJ bb 73} + +do_execsql_test 1.2.4 { + SELECT a, b, max(c) OVER (ORDER BY a,b GROUPS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 934 AA aa 934 AA aa 934 AA aa 934 AA bb 934 AA bb 934 + AA bb 934 AA bb 934 BB aa 934 BB aa 934 BB aa 934 BB aa 934 + BB aa 934 BB aa 934 BB bb 934 BB bb 934 BB bb 934 BB bb 934 + BB bb 934 BB bb 934 CC aa 934 CC aa 934 CC aa 934 CC aa 934 + CC bb 934 CC bb 934 DD aa 934 DD aa 934 DD aa 934 DD bb 959 + DD bb 959 DD bb 959 DD bb 959 EE aa 959 EE aa 959 EE bb 959 + EE bb 959 EE bb 959 FF aa 959 FF aa 959 FF aa 959 FF aa 959 + FF bb 959 FF bb 959 FF bb 959 FF bb 959 FF bb 959 FF bb 959 + GG aa 959 GG aa 959 GG aa 959 GG aa 959 GG bb 959 GG bb 959 + GG bb 959 GG bb 959 HH aa 979 HH aa 979 HH aa 979 HH bb 979 + HH bb 979 HH bb 979 HH bb 979 HH bb 979 HH bb 979 II aa 979 + II aa 979 II bb 979 II bb 979 II bb 979 II bb 979 II bb 979 + JJ aa 979 JJ aa 979 JJ aa 979 JJ aa 979 JJ bb 979 JJ bb 979 + JJ bb 979 JJ bb 979} + +do_execsql_test 1.2.5 { + SELECT a, b, min(c) OVER (ORDER BY a,b GROUPS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 223 AA aa 223 AA aa 223 AA aa 223 AA bb 223 AA bb 223 + AA bb 223 AA bb 223 BB aa 223 BB aa 223 BB aa 223 BB aa 223 + BB aa 223 BB aa 223 BB bb 223 BB bb 223 BB bb 223 BB bb 223 + BB bb 223 BB bb 223 CC aa 158 CC aa 158 CC aa 158 CC aa 158 + CC bb 158 CC bb 158 DD aa 158 DD aa 158 DD aa 158 DD bb 158 + DD bb 158 DD bb 158 DD bb 158 EE aa 113 EE aa 113 EE bb 113 + EE bb 113 EE bb 113 FF aa 113 FF aa 113 FF aa 113 FF aa 113 + FF bb 102 FF bb 102 FF bb 102 FF bb 102 FF bb 102 FF bb 102 + GG aa 102 GG aa 102 GG aa 102 GG aa 102 GG bb 102 GG bb 102 + GG bb 102 GG bb 102 HH aa 102 HH aa 102 HH aa 102 HH bb 102 + HH bb 102 HH bb 102 HH bb 102 HH bb 102 HH bb 102 II aa 102 + II aa 102 II bb 102 II bb 102 II bb 102 II bb 102 II bb 102 + JJ aa 102 JJ aa 102 JJ aa 102 JJ aa 102 JJ bb 102 JJ bb 102 + JJ bb 102 JJ bb 102} + +do_execsql_test 1.2.6 { + SELECT a, b, sum(c) OVER (ORDER BY a GROUPS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW EXCLUDE CURRENT ROW) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 3751 AA aa 3774 AA aa 4446 AA aa 4462 AA bb 3815 AA bb 4058 + AA bb 4113 AA bb 4376 BB aa 11263 BB aa 11365 BB aa 11613 + BB aa 11626 BB aa 11632 BB aa 11778 BB bb 11185 BB bb 11233 + BB bb 11239 BB bb 11314 BB bb 11320 BB bb 11392 CC aa 14388 + CC aa 14540 CC aa 14717 CC aa 14989 CC bb 14325 CC bb 14801 + DD aa 18334 DD aa 18923 DD aa 18955 DD bb 18220 DD bb 18385 + DD bb 18463 DD bb 18941 EE aa 20941 EE aa 21605 EE bb 20950 + EE bb 21089 EE bb 21466 FF aa 26716 FF aa 26719 FF aa 26768 + FF aa 27178 FF bb 26448 FF bb 26516 FF bb 26660 FF bb 26812 + FF bb 27091 FF bb 27284 GG aa 31220 GG aa 31374 GG aa 31519 + GG aa 31706 GG bb 30916 GG bb 30925 GG bb 31010 GG bb 31694 + HH aa 36318 HH aa 36507 HH aa 36817 HH bb 36334 HH bb 36567 + HH bb 36614 HH bb 36942 HH bb 36967 HH bb 37164 II aa 40285 + II aa 40539 II bb 40132 II bb 40266 II bb 40494 II bb 40516 + II bb 40687 JJ aa 43838 JJ aa 43969 JJ aa 44503 JJ aa 44624 + JJ bb 43898 JJ bb 44383 JJ bb 44401 JJ bb 44480} + +do_execsql_test 1.2.7 { + SELECT a, b, sum(c) OVER (ORDER BY a,b GROUPS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW EXCLUDE CURRENT ROW) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 1373 AA aa 1396 AA aa 2068 AA aa 2084 AA bb 3815 AA bb 4058 + AA bb 4113 AA bb 4376 BB aa 6796 BB aa 6898 BB aa 7146 BB aa 7159 + BB aa 7165 BB aa 7311 BB bb 11185 BB bb 11233 BB bb 11239 + BB bb 11314 BB bb 11320 BB bb 11392 CC aa 13220 CC aa 13372 + CC aa 13549 CC aa 13821 CC bb 14325 CC bb 14801 DD aa 15627 + DD aa 16216 DD aa 16248 DD bb 18220 DD bb 18385 DD bb 18463 + DD bb 18941 EE aa 19292 EE aa 19956 EE bb 20950 EE bb 21089 + EE bb 21466 FF aa 23211 FF aa 23214 FF aa 23263 FF aa 23673 + FF bb 26448 FF bb 26516 FF bb 26660 FF bb 26812 FF bb 27091 + FF bb 27284 GG aa 28349 GG aa 28503 GG aa 28648 GG aa 28835 + GG bb 30916 GG bb 30925 GG bb 31010 GG bb 31694 HH aa 33124 + HH aa 33313 HH aa 33623 HH bb 36334 HH bb 36567 HH bb 36614 + HH bb 36942 HH bb 36967 HH bb 37164 II aa 37695 II aa 37949 + II bb 40132 II bb 40266 II bb 40494 II bb 40516 II bb 40687 + JJ aa 42052 JJ aa 42183 JJ aa 42717 JJ aa 42838 JJ bb 43898 + JJ bb 44383 JJ bb 44401 JJ bb 44480} + +do_execsql_test 1.2.8 { + SELECT a, b, + sum(c) OVER (ORDER BY a GROUPS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW EXCLUDE CURRENT ROW), + sum(c) OVER (ORDER BY a GROUPS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ), + sum(c) OVER (ORDER BY a,b GROUPS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW EXCLUDE CURRENT ROW), + sum(c) OVER (ORDER BY a,b GROUPS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) + FROM t3 ORDER BY 1, 2, 3; +} {AA aa 3751 4685 1373 2307 AA aa 3774 4685 1396 2307 + AA aa 4446 4685 2068 2307 AA aa 4462 4685 2084 2307 + AA bb 3815 4685 3815 4685 AA bb 4058 4685 4058 4685 + AA bb 4113 4685 4113 4685 AA bb 4376 4685 4376 4685 + BB aa 11263 12025 6796 7558 BB aa 11365 12025 6898 7558 + BB aa 11613 12025 7146 7558 BB aa 11626 12025 7159 7558 + BB aa 11632 12025 7165 7558 BB aa 11778 12025 7311 7558 + BB bb 11185 12025 11185 12025 BB bb 11233 12025 11233 12025 + BB bb 11239 12025 11239 12025 BB bb 11314 12025 11314 12025 + BB bb 11320 12025 11320 12025 BB bb 11392 12025 11392 12025 + CC aa 14388 15147 13220 13979 CC aa 14540 15147 13372 13979 + CC aa 14717 15147 13549 13979 CC aa 14989 15147 13821 13979 + CC bb 14325 15147 14325 15147 CC bb 14801 15147 14801 15147 + DD aa 18334 19179 15627 16472 DD aa 18923 19179 16216 16472 + DD aa 18955 19179 16248 16472 DD bb 18220 19179 18220 19179 + DD bb 18385 19179 18385 19179 DD bb 18463 19179 18463 19179 + DD bb 18941 19179 18941 19179 EE aa 20941 21718 19292 20069 + EE aa 21605 21718 19956 20069 EE bb 20950 21718 20950 21718 + EE bb 21089 21718 21089 21718 EE bb 21466 21718 21466 21718 + FF aa 26716 27386 23211 23881 FF aa 26719 27386 23214 23881 + FF aa 26768 27386 23263 23881 FF aa 27178 27386 23673 23881 + FF bb 26448 27386 26448 27386 FF bb 26516 27386 26516 27386 + FF bb 26660 27386 26660 27386 FF bb 26812 27386 26812 27386 + FF bb 27091 27386 27091 27386 FF bb 27284 27386 27284 27386 + GG aa 31220 31854 28349 28983 GG aa 31374 31854 28503 28983 + GG aa 31519 31854 28648 28983 GG aa 31706 31854 28835 28983 + GG bb 30916 31854 30916 31854 GG bb 30925 31854 30925 31854 + GG bb 31010 31854 31010 31854 GG bb 31694 31854 31694 31854 + HH aa 36318 37297 33124 34103 HH aa 36507 37297 33313 34103 + HH aa 36817 37297 33623 34103 HH bb 36334 37297 36334 37297 + HH bb 36567 37297 36567 37297 HH bb 36614 37297 36614 37297 + HH bb 36942 37297 36942 37297 HH bb 36967 37297 36967 37297 + HH bb 37164 37297 37164 37297 II aa 40285 40937 37695 38347 + II aa 40539 40937 37949 38347 II bb 40132 40937 40132 40937 + II bb 40266 40937 40266 40937 II bb 40494 40937 40494 40937 + II bb 40516 40937 40516 40937 II bb 40687 40937 40687 40937 + JJ aa 43838 44737 42052 42951 JJ aa 43969 44737 42183 42951 + JJ aa 44503 44737 42717 42951 JJ aa 44624 44737 42838 42951 + JJ bb 43898 44737 43898 44737 JJ bb 44383 44737 44383 44737 + JJ bb 44401 44737 44401 44737 JJ bb 44480 44737 44480 44737} + +do_execsql_test 1.3.1 { + SELECT a, b, sum(c) OVER (ORDER BY a GROUPS BETWEEN UNBOUNDED PRECEDING AND 1 FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 12025 AA aa 12025 AA aa 12025 AA aa 12025 AA bb 12025 + AA bb 12025 AA bb 12025 AA bb 12025 BB aa 15147 BB aa 15147 + BB aa 15147 BB aa 15147 BB aa 15147 BB aa 15147 BB bb 15147 + BB bb 15147 BB bb 15147 BB bb 15147 BB bb 15147 BB bb 15147 + CC aa 19179 CC aa 19179 CC aa 19179 CC aa 19179 CC bb 19179 + CC bb 19179 DD aa 21718 DD aa 21718 DD aa 21718 DD bb 21718 + DD bb 21718 DD bb 21718 DD bb 21718 EE aa 27386 EE aa 27386 + EE bb 27386 EE bb 27386 EE bb 27386 FF aa 31854 FF aa 31854 + FF aa 31854 FF aa 31854 FF bb 31854 FF bb 31854 FF bb 31854 + FF bb 31854 FF bb 31854 FF bb 31854 GG aa 37297 GG aa 37297 + GG aa 37297 GG aa 37297 GG bb 37297 GG bb 37297 GG bb 37297 + GG bb 37297 HH aa 40937 HH aa 40937 HH aa 40937 HH bb 40937 + HH bb 40937 HH bb 40937 HH bb 40937 HH bb 40937 HH bb 40937 + II aa 44737 II aa 44737 II bb 44737 II bb 44737 II bb 44737 + II bb 44737 II bb 44737 JJ aa 44737 JJ aa 44737 JJ aa 44737 + JJ aa 44737 JJ bb 44737 JJ bb 44737 JJ bb 44737 JJ bb 44737} + +do_execsql_test 1.3.2 { + SELECT a, b, sum(c) OVER (ORDER BY a,b GROUPS BETWEEN UNBOUNDED PRECEDING AND 1 FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 4685 AA aa 4685 AA aa 4685 AA aa 4685 AA bb 7558 AA bb 7558 + AA bb 7558 AA bb 7558 BB aa 12025 BB aa 12025 BB aa 12025 + BB aa 12025 BB aa 12025 BB aa 12025 BB bb 13979 BB bb 13979 + BB bb 13979 BB bb 13979 BB bb 13979 BB bb 13979 CC aa 15147 + CC aa 15147 CC aa 15147 CC aa 15147 CC bb 16472 CC bb 16472 + DD aa 19179 DD aa 19179 DD aa 19179 DD bb 20069 DD bb 20069 + DD bb 20069 DD bb 20069 EE aa 21718 EE aa 21718 EE bb 23881 + EE bb 23881 EE bb 23881 FF aa 27386 FF aa 27386 FF aa 27386 + FF aa 27386 FF bb 28983 FF bb 28983 FF bb 28983 FF bb 28983 + FF bb 28983 FF bb 28983 GG aa 31854 GG aa 31854 GG aa 31854 + GG aa 31854 GG bb 34103 GG bb 34103 GG bb 34103 GG bb 34103 + HH aa 37297 HH aa 37297 HH aa 37297 HH bb 38347 HH bb 38347 + HH bb 38347 HH bb 38347 HH bb 38347 HH bb 38347 II aa 40937 + II aa 40937 II bb 42951 II bb 42951 II bb 42951 II bb 42951 + II bb 42951 JJ aa 44737 JJ aa 44737 JJ aa 44737 JJ aa 44737 + JJ bb 44737 JJ bb 44737 JJ bb 44737 JJ bb 44737} + +do_execsql_test 1.3.3 { + SELECT a, b, rank() OVER (ORDER BY a GROUPS BETWEEN UNBOUNDED PRECEDING AND 1 FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 1 AA aa 1 AA aa 1 AA aa 1 AA bb 1 AA bb 1 AA bb 1 + AA bb 1 BB aa 9 BB aa 9 BB aa 9 BB aa 9 BB aa 9 BB aa 9 + BB bb 9 BB bb 9 BB bb 9 BB bb 9 BB bb 9 BB bb 9 CC aa 21 + CC aa 21 CC aa 21 CC aa 21 CC bb 21 CC bb 21 DD aa 27 DD aa 27 + DD aa 27 DD bb 27 DD bb 27 DD bb 27 DD bb 27 EE aa 34 EE aa 34 + EE bb 34 EE bb 34 EE bb 34 FF aa 39 FF aa 39 FF aa 39 FF aa 39 + FF bb 39 FF bb 39 FF bb 39 FF bb 39 FF bb 39 FF bb 39 GG aa 49 + GG aa 49 GG aa 49 GG aa 49 GG bb 49 GG bb 49 GG bb 49 GG bb 49 + HH aa 57 HH aa 57 HH aa 57 HH bb 57 HH bb 57 HH bb 57 HH bb 57 + HH bb 57 HH bb 57 II aa 66 II aa 66 II bb 66 II bb 66 II bb 66 + II bb 66 II bb 66 JJ aa 73 JJ aa 73 JJ aa 73 JJ aa 73 JJ bb 73 + JJ bb 73 JJ bb 73 JJ bb 73} + +do_execsql_test 1.3.4 { + SELECT a, b, max(c) OVER (ORDER BY a,b GROUPS BETWEEN UNBOUNDED PRECEDING AND 1 FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 934 AA aa 934 AA aa 934 AA aa 934 AA bb 934 AA bb 934 + AA bb 934 AA bb 934 BB aa 934 BB aa 934 BB aa 934 BB aa 934 + BB aa 934 BB aa 934 BB bb 934 BB bb 934 BB bb 934 BB bb 934 + BB bb 934 BB bb 934 CC aa 934 CC aa 934 CC aa 934 CC aa 934 + CC bb 934 CC bb 934 DD aa 959 DD aa 959 DD aa 959 DD bb 959 + DD bb 959 DD bb 959 DD bb 959 EE aa 959 EE aa 959 EE bb 959 + EE bb 959 EE bb 959 FF aa 959 FF aa 959 FF aa 959 FF aa 959 + FF bb 959 FF bb 959 FF bb 959 FF bb 959 FF bb 959 FF bb 959 + GG aa 959 GG aa 959 GG aa 959 GG aa 959 GG bb 979 GG bb 979 + GG bb 979 GG bb 979 HH aa 979 HH aa 979 HH aa 979 HH bb 979 + HH bb 979 HH bb 979 HH bb 979 HH bb 979 HH bb 979 II aa 979 + II aa 979 II bb 979 II bb 979 II bb 979 II bb 979 II bb 979 + JJ aa 979 JJ aa 979 JJ aa 979 JJ aa 979 JJ bb 979 JJ bb 979 + JJ bb 979 JJ bb 979} + +do_execsql_test 1.3.5 { + SELECT a, b, min(c) OVER (ORDER BY a,b GROUPS BETWEEN UNBOUNDED PRECEDING AND 1 FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 223 AA aa 223 AA aa 223 AA aa 223 AA bb 223 AA bb 223 + AA bb 223 AA bb 223 BB aa 223 BB aa 223 BB aa 223 BB aa 223 + BB aa 223 BB aa 223 BB bb 158 BB bb 158 BB bb 158 BB bb 158 + BB bb 158 BB bb 158 CC aa 158 CC aa 158 CC aa 158 CC aa 158 + CC bb 158 CC bb 158 DD aa 158 DD aa 158 DD aa 158 DD bb 113 + DD bb 113 DD bb 113 DD bb 113 EE aa 113 EE aa 113 EE bb 113 + EE bb 113 EE bb 113 FF aa 102 FF aa 102 FF aa 102 FF aa 102 + FF bb 102 FF bb 102 FF bb 102 FF bb 102 FF bb 102 FF bb 102 + GG aa 102 GG aa 102 GG aa 102 GG aa 102 GG bb 102 GG bb 102 + GG bb 102 GG bb 102 HH aa 102 HH aa 102 HH aa 102 HH bb 102 + HH bb 102 HH bb 102 HH bb 102 HH bb 102 HH bb 102 II aa 102 + II aa 102 II bb 102 II bb 102 II bb 102 II bb 102 II bb 102 + JJ aa 102 JJ aa 102 JJ aa 102 JJ aa 102 JJ bb 102 JJ bb 102 + JJ bb 102 JJ bb 102} + +do_execsql_test 1.3.6 { + SELECT a, b, sum(c) OVER (ORDER BY a GROUPS BETWEEN UNBOUNDED PRECEDING AND 1 FOLLOWING EXCLUDE CURRENT ROW) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 11091 AA aa 11114 AA aa 11786 AA aa 11802 AA bb 11155 + AA bb 11398 AA bb 11453 AA bb 11716 BB aa 14385 BB aa 14487 + BB aa 14735 BB aa 14748 BB aa 14754 BB aa 14900 BB bb 14307 + BB bb 14355 BB bb 14361 BB bb 14436 BB bb 14442 BB bb 14514 + CC aa 18420 CC aa 18572 CC aa 18749 CC aa 19021 CC bb 18357 + CC bb 18833 DD aa 20873 DD aa 21462 DD aa 21494 DD bb 20759 + DD bb 20924 DD bb 21002 DD bb 21480 EE aa 26609 EE aa 27273 + EE bb 26618 EE bb 26757 EE bb 27134 FF aa 31184 FF aa 31187 + FF aa 31236 FF aa 31646 FF bb 30916 FF bb 30984 FF bb 31128 + FF bb 31280 FF bb 31559 FF bb 31752 GG aa 36663 GG aa 36817 + GG aa 36962 GG aa 37149 GG bb 36359 GG bb 36368 GG bb 36453 + GG bb 37137 HH aa 39958 HH aa 40147 HH aa 40457 HH bb 39974 + HH bb 40207 HH bb 40254 HH bb 40582 HH bb 40607 HH bb 40804 + II aa 44085 II aa 44339 II bb 43932 II bb 44066 II bb 44294 + II bb 44316 II bb 44487 JJ aa 43838 JJ aa 43969 JJ aa 44503 + JJ aa 44624 JJ bb 43898 JJ bb 44383 JJ bb 44401 JJ bb 44480} + +do_execsql_test 1.3.7 { + SELECT a, b, sum(c) OVER (ORDER BY a,b GROUPS BETWEEN UNBOUNDED PRECEDING AND 1 FOLLOWING EXCLUDE CURRENT ROW) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 3751 AA aa 3774 AA aa 4446 AA aa 4462 AA bb 6688 AA bb 6931 + AA bb 6986 AA bb 7249 BB aa 11263 BB aa 11365 BB aa 11613 + BB aa 11626 BB aa 11632 BB aa 11778 BB bb 13139 BB bb 13187 + BB bb 13193 BB bb 13268 BB bb 13274 BB bb 13346 CC aa 14388 + CC aa 14540 CC aa 14717 CC aa 14989 CC bb 15650 CC bb 16126 + DD aa 18334 DD aa 18923 DD aa 18955 DD bb 19110 DD bb 19275 + DD bb 19353 DD bb 19831 EE aa 20941 EE aa 21605 EE bb 23113 + EE bb 23252 EE bb 23629 FF aa 26716 FF aa 26719 FF aa 26768 + FF aa 27178 FF bb 28045 FF bb 28113 FF bb 28257 FF bb 28409 + FF bb 28688 FF bb 28881 GG aa 31220 GG aa 31374 GG aa 31519 + GG aa 31706 GG bb 33165 GG bb 33174 GG bb 33259 GG bb 33943 + HH aa 36318 HH aa 36507 HH aa 36817 HH bb 37384 HH bb 37617 + HH bb 37664 HH bb 37992 HH bb 38017 HH bb 38214 II aa 40285 + II aa 40539 II bb 42146 II bb 42280 II bb 42508 II bb 42530 + II bb 42701 JJ aa 43838 JJ aa 43969 JJ aa 44503 JJ aa 44624 + JJ bb 43898 JJ bb 44383 JJ bb 44401 JJ bb 44480} + +do_execsql_test 1.3.8 { + SELECT a, b, + sum(c) OVER (ORDER BY a GROUPS BETWEEN UNBOUNDED PRECEDING AND 1 FOLLOWING EXCLUDE CURRENT ROW), + sum(c) OVER (ORDER BY a GROUPS BETWEEN UNBOUNDED PRECEDING AND 1 FOLLOWING ), + sum(c) OVER (ORDER BY a,b GROUPS BETWEEN UNBOUNDED PRECEDING AND 1 FOLLOWING EXCLUDE CURRENT ROW), + sum(c) OVER (ORDER BY a,b GROUPS BETWEEN UNBOUNDED PRECEDING AND 1 FOLLOWING ) + FROM t3 ORDER BY 1, 2, 3; +} {AA aa 11091 12025 3751 4685 AA aa 11114 12025 3774 4685 + AA aa 11786 12025 4446 4685 AA aa 11802 12025 4462 4685 + AA bb 11155 12025 6688 7558 AA bb 11398 12025 6931 7558 + AA bb 11453 12025 6986 7558 AA bb 11716 12025 7249 7558 + BB aa 14385 15147 11263 12025 BB aa 14487 15147 11365 12025 + BB aa 14735 15147 11613 12025 BB aa 14748 15147 11626 12025 + BB aa 14754 15147 11632 12025 BB aa 14900 15147 11778 12025 + BB bb 14307 15147 13139 13979 BB bb 14355 15147 13187 13979 + BB bb 14361 15147 13193 13979 BB bb 14436 15147 13268 13979 + BB bb 14442 15147 13274 13979 BB bb 14514 15147 13346 13979 + CC aa 18420 19179 14388 15147 CC aa 18572 19179 14540 15147 + CC aa 18749 19179 14717 15147 CC aa 19021 19179 14989 15147 + CC bb 18357 19179 15650 16472 CC bb 18833 19179 16126 16472 + DD aa 20873 21718 18334 19179 DD aa 21462 21718 18923 19179 + DD aa 21494 21718 18955 19179 DD bb 20759 21718 19110 20069 + DD bb 20924 21718 19275 20069 DD bb 21002 21718 19353 20069 + DD bb 21480 21718 19831 20069 EE aa 26609 27386 20941 21718 + EE aa 27273 27386 21605 21718 EE bb 26618 27386 23113 23881 + EE bb 26757 27386 23252 23881 EE bb 27134 27386 23629 23881 + FF aa 31184 31854 26716 27386 FF aa 31187 31854 26719 27386 + FF aa 31236 31854 26768 27386 FF aa 31646 31854 27178 27386 + FF bb 30916 31854 28045 28983 FF bb 30984 31854 28113 28983 + FF bb 31128 31854 28257 28983 FF bb 31280 31854 28409 28983 + FF bb 31559 31854 28688 28983 FF bb 31752 31854 28881 28983 + GG aa 36663 37297 31220 31854 GG aa 36817 37297 31374 31854 + GG aa 36962 37297 31519 31854 GG aa 37149 37297 31706 31854 + GG bb 36359 37297 33165 34103 GG bb 36368 37297 33174 34103 + GG bb 36453 37297 33259 34103 GG bb 37137 37297 33943 34103 + HH aa 39958 40937 36318 37297 HH aa 40147 40937 36507 37297 + HH aa 40457 40937 36817 37297 HH bb 39974 40937 37384 38347 + HH bb 40207 40937 37617 38347 HH bb 40254 40937 37664 38347 + HH bb 40582 40937 37992 38347 HH bb 40607 40937 38017 38347 + HH bb 40804 40937 38214 38347 II aa 44085 44737 40285 40937 + II aa 44339 44737 40539 40937 II bb 43932 44737 42146 42951 + II bb 44066 44737 42280 42951 II bb 44294 44737 42508 42951 + II bb 44316 44737 42530 42951 II bb 44487 44737 42701 42951 + JJ aa 43838 44737 43838 44737 JJ aa 43969 44737 43969 44737 + JJ aa 44503 44737 44503 44737 JJ aa 44624 44737 44624 44737 + JJ bb 43898 44737 43898 44737 JJ bb 44383 44737 44383 44737 + JJ bb 44401 44737 44401 44737 JJ bb 44480 44737 44480 44737} + +do_execsql_test 1.4.1 { + SELECT a, b, sum(c) OVER (ORDER BY a GROUPS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 44737 AA aa 44737 AA aa 44737 AA aa 44737 AA bb 44737 + AA bb 44737 AA bb 44737 AA bb 44737 BB aa 44737 BB aa 44737 + BB aa 44737 BB aa 44737 BB aa 44737 BB aa 44737 BB bb 44737 + BB bb 44737 BB bb 44737 BB bb 44737 BB bb 44737 BB bb 44737 + CC aa 44737 CC aa 44737 CC aa 44737 CC aa 44737 CC bb 44737 + CC bb 44737 DD aa 44737 DD aa 44737 DD aa 44737 DD bb 44737 + DD bb 44737 DD bb 44737 DD bb 44737 EE aa 44737 EE aa 44737 + EE bb 44737 EE bb 44737 EE bb 44737 FF aa 44737 FF aa 44737 + FF aa 44737 FF aa 44737 FF bb 44737 FF bb 44737 FF bb 44737 + FF bb 44737 FF bb 44737 FF bb 44737 GG aa 44737 GG aa 44737 + GG aa 44737 GG aa 44737 GG bb 44737 GG bb 44737 GG bb 44737 + GG bb 44737 HH aa 44737 HH aa 44737 HH aa 44737 HH bb 44737 + HH bb 44737 HH bb 44737 HH bb 44737 HH bb 44737 HH bb 44737 + II aa 44737 II aa 44737 II bb 44737 II bb 44737 II bb 44737 + II bb 44737 II bb 44737 JJ aa 44737 JJ aa 44737 JJ aa 44737 + JJ aa 44737 JJ bb 44737 JJ bb 44737 JJ bb 44737 JJ bb 44737} + +do_execsql_test 1.4.2 { + SELECT a, b, sum(c) OVER (ORDER BY a,b GROUPS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 44737 AA aa 44737 AA aa 44737 AA aa 44737 AA bb 44737 + AA bb 44737 AA bb 44737 AA bb 44737 BB aa 44737 BB aa 44737 + BB aa 44737 BB aa 44737 BB aa 44737 BB aa 44737 BB bb 44737 + BB bb 44737 BB bb 44737 BB bb 44737 BB bb 44737 BB bb 44737 + CC aa 44737 CC aa 44737 CC aa 44737 CC aa 44737 CC bb 44737 + CC bb 44737 DD aa 44737 DD aa 44737 DD aa 44737 DD bb 44737 + DD bb 44737 DD bb 44737 DD bb 44737 EE aa 44737 EE aa 44737 + EE bb 44737 EE bb 44737 EE bb 44737 FF aa 44737 FF aa 44737 + FF aa 44737 FF aa 44737 FF bb 44737 FF bb 44737 FF bb 44737 + FF bb 44737 FF bb 44737 FF bb 44737 GG aa 44737 GG aa 44737 + GG aa 44737 GG aa 44737 GG bb 44737 GG bb 44737 GG bb 44737 + GG bb 44737 HH aa 44737 HH aa 44737 HH aa 44737 HH bb 44737 + HH bb 44737 HH bb 44737 HH bb 44737 HH bb 44737 HH bb 44737 + II aa 44737 II aa 44737 II bb 44737 II bb 44737 II bb 44737 + II bb 44737 II bb 44737 JJ aa 44737 JJ aa 44737 JJ aa 44737 + JJ aa 44737 JJ bb 44737 JJ bb 44737 JJ bb 44737 JJ bb 44737} + +do_execsql_test 1.4.3 { + SELECT a, b, rank() OVER (ORDER BY a GROUPS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 1 AA aa 1 AA aa 1 AA aa 1 AA bb 1 AA bb 1 AA bb 1 + AA bb 1 BB aa 9 BB aa 9 BB aa 9 BB aa 9 BB aa 9 BB aa 9 + BB bb 9 BB bb 9 BB bb 9 BB bb 9 BB bb 9 BB bb 9 CC aa 21 + CC aa 21 CC aa 21 CC aa 21 CC bb 21 CC bb 21 DD aa 27 DD aa 27 + DD aa 27 DD bb 27 DD bb 27 DD bb 27 DD bb 27 EE aa 34 EE aa 34 + EE bb 34 EE bb 34 EE bb 34 FF aa 39 FF aa 39 FF aa 39 FF aa 39 + FF bb 39 FF bb 39 FF bb 39 FF bb 39 FF bb 39 FF bb 39 GG aa 49 + GG aa 49 GG aa 49 GG aa 49 GG bb 49 GG bb 49 GG bb 49 GG bb 49 + HH aa 57 HH aa 57 HH aa 57 HH bb 57 HH bb 57 HH bb 57 HH bb 57 + HH bb 57 HH bb 57 II aa 66 II aa 66 II bb 66 II bb 66 II bb 66 + II bb 66 II bb 66 JJ aa 73 JJ aa 73 JJ aa 73 JJ aa 73 JJ bb 73 + JJ bb 73 JJ bb 73 JJ bb 73} + +do_execsql_test 1.4.4 { + SELECT a, b, max(c) OVER (ORDER BY a,b GROUPS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 979 AA aa 979 AA aa 979 AA aa 979 AA bb 979 AA bb 979 + AA bb 979 AA bb 979 BB aa 979 BB aa 979 BB aa 979 BB aa 979 + BB aa 979 BB aa 979 BB bb 979 BB bb 979 BB bb 979 BB bb 979 + BB bb 979 BB bb 979 CC aa 979 CC aa 979 CC aa 979 CC aa 979 + CC bb 979 CC bb 979 DD aa 979 DD aa 979 DD aa 979 DD bb 979 + DD bb 979 DD bb 979 DD bb 979 EE aa 979 EE aa 979 EE bb 979 + EE bb 979 EE bb 979 FF aa 979 FF aa 979 FF aa 979 FF aa 979 + FF bb 979 FF bb 979 FF bb 979 FF bb 979 FF bb 979 FF bb 979 + GG aa 979 GG aa 979 GG aa 979 GG aa 979 GG bb 979 GG bb 979 + GG bb 979 GG bb 979 HH aa 979 HH aa 979 HH aa 979 HH bb 979 + HH bb 979 HH bb 979 HH bb 979 HH bb 979 HH bb 979 II aa 979 + II aa 979 II bb 979 II bb 979 II bb 979 II bb 979 II bb 979 + JJ aa 979 JJ aa 979 JJ aa 979 JJ aa 979 JJ bb 979 JJ bb 979 + JJ bb 979 JJ bb 979} + +do_execsql_test 1.4.5 { + SELECT a, b, min(c) OVER (ORDER BY a,b GROUPS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 102 AA aa 102 AA aa 102 AA aa 102 AA bb 102 AA bb 102 + AA bb 102 AA bb 102 BB aa 102 BB aa 102 BB aa 102 BB aa 102 + BB aa 102 BB aa 102 BB bb 102 BB bb 102 BB bb 102 BB bb 102 + BB bb 102 BB bb 102 CC aa 102 CC aa 102 CC aa 102 CC aa 102 + CC bb 102 CC bb 102 DD aa 102 DD aa 102 DD aa 102 DD bb 102 + DD bb 102 DD bb 102 DD bb 102 EE aa 102 EE aa 102 EE bb 102 + EE bb 102 EE bb 102 FF aa 102 FF aa 102 FF aa 102 FF aa 102 + FF bb 102 FF bb 102 FF bb 102 FF bb 102 FF bb 102 FF bb 102 + GG aa 102 GG aa 102 GG aa 102 GG aa 102 GG bb 102 GG bb 102 + GG bb 102 GG bb 102 HH aa 102 HH aa 102 HH aa 102 HH bb 102 + HH bb 102 HH bb 102 HH bb 102 HH bb 102 HH bb 102 II aa 102 + II aa 102 II bb 102 II bb 102 II bb 102 II bb 102 II bb 102 + JJ aa 102 JJ aa 102 JJ aa 102 JJ aa 102 JJ bb 102 JJ bb 102 + JJ bb 102 JJ bb 102} + +do_execsql_test 1.4.6 { + SELECT a, b, sum(c) OVER (ORDER BY a GROUPS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 43803 AA aa 43826 AA aa 44498 AA aa 44514 AA bb 43867 + AA bb 44110 AA bb 44165 AA bb 44428 BB aa 43975 BB aa 44077 + BB aa 44325 BB aa 44338 BB aa 44344 BB aa 44490 BB bb 43897 + BB bb 43945 BB bb 43951 BB bb 44026 BB bb 44032 BB bb 44104 + CC aa 43978 CC aa 44130 CC aa 44307 CC aa 44579 CC bb 43915 + CC bb 44391 DD aa 43892 DD aa 44481 DD aa 44513 DD bb 43778 + DD bb 43943 DD bb 44021 DD bb 44499 EE aa 43960 EE aa 44624 + EE bb 43969 EE bb 44108 EE bb 44485 FF aa 44067 FF aa 44070 + FF aa 44119 FF aa 44529 FF bb 43799 FF bb 43867 FF bb 44011 + FF bb 44163 FF bb 44442 FF bb 44635 GG aa 44103 GG aa 44257 + GG aa 44402 GG aa 44589 GG bb 43799 GG bb 43808 GG bb 43893 + GG bb 44577 HH aa 43758 HH aa 43947 HH aa 44257 HH bb 43774 + HH bb 44007 HH bb 44054 HH bb 44382 HH bb 44407 HH bb 44604 + II aa 44085 II aa 44339 II bb 43932 II bb 44066 II bb 44294 + II bb 44316 II bb 44487 JJ aa 43838 JJ aa 43969 JJ aa 44503 + JJ aa 44624 JJ bb 43898 JJ bb 44383 JJ bb 44401 JJ bb 44480} + +do_execsql_test 1.4.7 { + SELECT a, b, sum(c) OVER (ORDER BY a,b GROUPS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 43803 AA aa 43826 AA aa 44498 AA aa 44514 AA bb 43867 + AA bb 44110 AA bb 44165 AA bb 44428 BB aa 43975 BB aa 44077 + BB aa 44325 BB aa 44338 BB aa 44344 BB aa 44490 BB bb 43897 + BB bb 43945 BB bb 43951 BB bb 44026 BB bb 44032 BB bb 44104 + CC aa 43978 CC aa 44130 CC aa 44307 CC aa 44579 CC bb 43915 + CC bb 44391 DD aa 43892 DD aa 44481 DD aa 44513 DD bb 43778 + DD bb 43943 DD bb 44021 DD bb 44499 EE aa 43960 EE aa 44624 + EE bb 43969 EE bb 44108 EE bb 44485 FF aa 44067 FF aa 44070 + FF aa 44119 FF aa 44529 FF bb 43799 FF bb 43867 FF bb 44011 + FF bb 44163 FF bb 44442 FF bb 44635 GG aa 44103 GG aa 44257 + GG aa 44402 GG aa 44589 GG bb 43799 GG bb 43808 GG bb 43893 + GG bb 44577 HH aa 43758 HH aa 43947 HH aa 44257 HH bb 43774 + HH bb 44007 HH bb 44054 HH bb 44382 HH bb 44407 HH bb 44604 + II aa 44085 II aa 44339 II bb 43932 II bb 44066 II bb 44294 + II bb 44316 II bb 44487 JJ aa 43838 JJ aa 43969 JJ aa 44503 + JJ aa 44624 JJ bb 43898 JJ bb 44383 JJ bb 44401 JJ bb 44480} + +do_execsql_test 1.4.8 { + SELECT a, b, + sum(c) OVER (ORDER BY a GROUPS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW), + sum(c) OVER (ORDER BY a GROUPS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ), + sum(c) OVER (ORDER BY a,b GROUPS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW), + sum(c) OVER (ORDER BY a,b GROUPS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) + FROM t3 ORDER BY 1, 2, 3; +} {AA aa 43803 44737 43803 44737 AA aa 43826 44737 43826 44737 + AA aa 44498 44737 44498 44737 AA aa 44514 44737 44514 44737 + AA bb 43867 44737 43867 44737 AA bb 44110 44737 44110 44737 + AA bb 44165 44737 44165 44737 AA bb 44428 44737 44428 44737 + BB aa 43975 44737 43975 44737 BB aa 44077 44737 44077 44737 + BB aa 44325 44737 44325 44737 BB aa 44338 44737 44338 44737 + BB aa 44344 44737 44344 44737 BB aa 44490 44737 44490 44737 + BB bb 43897 44737 43897 44737 BB bb 43945 44737 43945 44737 + BB bb 43951 44737 43951 44737 BB bb 44026 44737 44026 44737 + BB bb 44032 44737 44032 44737 BB bb 44104 44737 44104 44737 + CC aa 43978 44737 43978 44737 CC aa 44130 44737 44130 44737 + CC aa 44307 44737 44307 44737 CC aa 44579 44737 44579 44737 + CC bb 43915 44737 43915 44737 CC bb 44391 44737 44391 44737 + DD aa 43892 44737 43892 44737 DD aa 44481 44737 44481 44737 + DD aa 44513 44737 44513 44737 DD bb 43778 44737 43778 44737 + DD bb 43943 44737 43943 44737 DD bb 44021 44737 44021 44737 + DD bb 44499 44737 44499 44737 EE aa 43960 44737 43960 44737 + EE aa 44624 44737 44624 44737 EE bb 43969 44737 43969 44737 + EE bb 44108 44737 44108 44737 EE bb 44485 44737 44485 44737 + FF aa 44067 44737 44067 44737 FF aa 44070 44737 44070 44737 + FF aa 44119 44737 44119 44737 FF aa 44529 44737 44529 44737 + FF bb 43799 44737 43799 44737 FF bb 43867 44737 43867 44737 + FF bb 44011 44737 44011 44737 FF bb 44163 44737 44163 44737 + FF bb 44442 44737 44442 44737 FF bb 44635 44737 44635 44737 + GG aa 44103 44737 44103 44737 GG aa 44257 44737 44257 44737 + GG aa 44402 44737 44402 44737 GG aa 44589 44737 44589 44737 + GG bb 43799 44737 43799 44737 GG bb 43808 44737 43808 44737 + GG bb 43893 44737 43893 44737 GG bb 44577 44737 44577 44737 + HH aa 43758 44737 43758 44737 HH aa 43947 44737 43947 44737 + HH aa 44257 44737 44257 44737 HH bb 43774 44737 43774 44737 + HH bb 44007 44737 44007 44737 HH bb 44054 44737 44054 44737 + HH bb 44382 44737 44382 44737 HH bb 44407 44737 44407 44737 + HH bb 44604 44737 44604 44737 II aa 44085 44737 44085 44737 + II aa 44339 44737 44339 44737 II bb 43932 44737 43932 44737 + II bb 44066 44737 44066 44737 II bb 44294 44737 44294 44737 + II bb 44316 44737 44316 44737 II bb 44487 44737 44487 44737 + JJ aa 43838 44737 43838 44737 JJ aa 43969 44737 43969 44737 + JJ aa 44503 44737 44503 44737 JJ aa 44624 44737 44624 44737 + JJ bb 43898 44737 43898 44737 JJ bb 44383 44737 44383 44737 + JJ bb 44401 44737 44401 44737 JJ bb 44480 44737 44480 44737} + +do_execsql_test 1.5.1 { + SELECT a, b, sum(c) OVER (ORDER BY a GROUPS BETWEEN 1 PRECEDING AND 2 PRECEDING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa {} AA aa {} AA aa {} AA aa {} AA bb {} AA bb {} AA bb {} + AA bb {} BB aa {} BB aa {} BB aa {} BB aa {} BB aa {} BB aa {} + BB bb {} BB bb {} BB bb {} BB bb {} BB bb {} BB bb {} CC aa {} + CC aa {} CC aa {} CC aa {} CC bb {} CC bb {} DD aa {} DD aa {} + DD aa {} DD bb {} DD bb {} DD bb {} DD bb {} EE aa {} EE aa {} + EE bb {} EE bb {} EE bb {} FF aa {} FF aa {} FF aa {} FF aa {} + FF bb {} FF bb {} FF bb {} FF bb {} FF bb {} FF bb {} GG aa {} + GG aa {} GG aa {} GG aa {} GG bb {} GG bb {} GG bb {} GG bb {} + HH aa {} HH aa {} HH aa {} HH bb {} HH bb {} HH bb {} HH bb {} + HH bb {} HH bb {} II aa {} II aa {} II bb {} II bb {} II bb {} + II bb {} II bb {} JJ aa {} JJ aa {} JJ aa {} JJ aa {} JJ bb {} + JJ bb {} JJ bb {} JJ bb {}} + +do_execsql_test 1.5.2 { + SELECT a, b, sum(c) OVER (ORDER BY a,b GROUPS BETWEEN 1 PRECEDING AND 2 PRECEDING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa {} AA aa {} AA aa {} AA aa {} AA bb {} AA bb {} AA bb {} + AA bb {} BB aa {} BB aa {} BB aa {} BB aa {} BB aa {} BB aa {} + BB bb {} BB bb {} BB bb {} BB bb {} BB bb {} BB bb {} CC aa {} + CC aa {} CC aa {} CC aa {} CC bb {} CC bb {} DD aa {} DD aa {} + DD aa {} DD bb {} DD bb {} DD bb {} DD bb {} EE aa {} EE aa {} + EE bb {} EE bb {} EE bb {} FF aa {} FF aa {} FF aa {} FF aa {} + FF bb {} FF bb {} FF bb {} FF bb {} FF bb {} FF bb {} GG aa {} + GG aa {} GG aa {} GG aa {} GG bb {} GG bb {} GG bb {} GG bb {} + HH aa {} HH aa {} HH aa {} HH bb {} HH bb {} HH bb {} HH bb {} + HH bb {} HH bb {} II aa {} II aa {} II bb {} II bb {} II bb {} + II bb {} II bb {} JJ aa {} JJ aa {} JJ aa {} JJ aa {} JJ bb {} + JJ bb {} JJ bb {} JJ bb {}} + +do_execsql_test 1.5.3 { + SELECT a, b, rank() OVER (ORDER BY a GROUPS BETWEEN 1 PRECEDING AND 2 PRECEDING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 1 AA aa 1 AA aa 1 AA aa 1 AA bb 1 AA bb 1 AA bb 1 + AA bb 1 BB aa 9 BB aa 9 BB aa 9 BB aa 9 BB aa 9 BB aa 9 + BB bb 9 BB bb 9 BB bb 9 BB bb 9 BB bb 9 BB bb 9 CC aa 21 + CC aa 21 CC aa 21 CC aa 21 CC bb 21 CC bb 21 DD aa 27 DD aa 27 + DD aa 27 DD bb 27 DD bb 27 DD bb 27 DD bb 27 EE aa 34 EE aa 34 + EE bb 34 EE bb 34 EE bb 34 FF aa 39 FF aa 39 FF aa 39 FF aa 39 + FF bb 39 FF bb 39 FF bb 39 FF bb 39 FF bb 39 FF bb 39 GG aa 49 + GG aa 49 GG aa 49 GG aa 49 GG bb 49 GG bb 49 GG bb 49 GG bb 49 + HH aa 57 HH aa 57 HH aa 57 HH bb 57 HH bb 57 HH bb 57 HH bb 57 + HH bb 57 HH bb 57 II aa 66 II aa 66 II bb 66 II bb 66 II bb 66 + II bb 66 II bb 66 JJ aa 73 JJ aa 73 JJ aa 73 JJ aa 73 JJ bb 73 + JJ bb 73 JJ bb 73 JJ bb 73} + +do_execsql_test 1.5.4 { + SELECT a, b, max(c) OVER (ORDER BY a,b GROUPS BETWEEN 1 PRECEDING AND 2 PRECEDING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa {} AA aa {} AA aa {} AA aa {} AA bb {} AA bb {} AA bb {} + AA bb {} BB aa {} BB aa {} BB aa {} BB aa {} BB aa {} BB aa {} + BB bb {} BB bb {} BB bb {} BB bb {} BB bb {} BB bb {} CC aa {} + CC aa {} CC aa {} CC aa {} CC bb {} CC bb {} DD aa {} DD aa {} + DD aa {} DD bb {} DD bb {} DD bb {} DD bb {} EE aa {} EE aa {} + EE bb {} EE bb {} EE bb {} FF aa {} FF aa {} FF aa {} FF aa {} + FF bb {} FF bb {} FF bb {} FF bb {} FF bb {} FF bb {} GG aa {} + GG aa {} GG aa {} GG aa {} GG bb {} GG bb {} GG bb {} GG bb {} + HH aa {} HH aa {} HH aa {} HH bb {} HH bb {} HH bb {} HH bb {} + HH bb {} HH bb {} II aa {} II aa {} II bb {} II bb {} II bb {} + II bb {} II bb {} JJ aa {} JJ aa {} JJ aa {} JJ aa {} JJ bb {} + JJ bb {} JJ bb {} JJ bb {}} + +do_execsql_test 1.5.5 { + SELECT a, b, min(c) OVER (ORDER BY a,b GROUPS BETWEEN 1 PRECEDING AND 2 PRECEDING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa {} AA aa {} AA aa {} AA aa {} AA bb {} AA bb {} AA bb {} + AA bb {} BB aa {} BB aa {} BB aa {} BB aa {} BB aa {} BB aa {} + BB bb {} BB bb {} BB bb {} BB bb {} BB bb {} BB bb {} CC aa {} + CC aa {} CC aa {} CC aa {} CC bb {} CC bb {} DD aa {} DD aa {} + DD aa {} DD bb {} DD bb {} DD bb {} DD bb {} EE aa {} EE aa {} + EE bb {} EE bb {} EE bb {} FF aa {} FF aa {} FF aa {} FF aa {} + FF bb {} FF bb {} FF bb {} FF bb {} FF bb {} FF bb {} GG aa {} + GG aa {} GG aa {} GG aa {} GG bb {} GG bb {} GG bb {} GG bb {} + HH aa {} HH aa {} HH aa {} HH bb {} HH bb {} HH bb {} HH bb {} + HH bb {} HH bb {} II aa {} II aa {} II bb {} II bb {} II bb {} + II bb {} II bb {} JJ aa {} JJ aa {} JJ aa {} JJ aa {} JJ bb {} + JJ bb {} JJ bb {} JJ bb {}} + +do_execsql_test 1.5.6 { + SELECT a, b, sum(c) OVER (ORDER BY a GROUPS BETWEEN 1 PRECEDING AND 2 PRECEDING EXCLUDE CURRENT ROW) FROM t3 ORDER BY 1, 2, 3; +} {AA aa {} AA aa {} AA aa {} AA aa {} AA bb {} AA bb {} AA bb {} + AA bb {} BB aa {} BB aa {} BB aa {} BB aa {} BB aa {} BB aa {} + BB bb {} BB bb {} BB bb {} BB bb {} BB bb {} BB bb {} CC aa {} + CC aa {} CC aa {} CC aa {} CC bb {} CC bb {} DD aa {} DD aa {} + DD aa {} DD bb {} DD bb {} DD bb {} DD bb {} EE aa {} EE aa {} + EE bb {} EE bb {} EE bb {} FF aa {} FF aa {} FF aa {} FF aa {} + FF bb {} FF bb {} FF bb {} FF bb {} FF bb {} FF bb {} GG aa {} + GG aa {} GG aa {} GG aa {} GG bb {} GG bb {} GG bb {} GG bb {} + HH aa {} HH aa {} HH aa {} HH bb {} HH bb {} HH bb {} HH bb {} + HH bb {} HH bb {} II aa {} II aa {} II bb {} II bb {} II bb {} + II bb {} II bb {} JJ aa {} JJ aa {} JJ aa {} JJ aa {} JJ bb {} + JJ bb {} JJ bb {} JJ bb {}} + +do_execsql_test 1.5.7 { + SELECT a, b, sum(c) OVER (ORDER BY a,b GROUPS BETWEEN 1 PRECEDING AND 2 PRECEDING EXCLUDE CURRENT ROW) FROM t3 ORDER BY 1, 2, 3; +} {AA aa {} AA aa {} AA aa {} AA aa {} AA bb {} AA bb {} AA bb {} + AA bb {} BB aa {} BB aa {} BB aa {} BB aa {} BB aa {} BB aa {} + BB bb {} BB bb {} BB bb {} BB bb {} BB bb {} BB bb {} CC aa {} + CC aa {} CC aa {} CC aa {} CC bb {} CC bb {} DD aa {} DD aa {} + DD aa {} DD bb {} DD bb {} DD bb {} DD bb {} EE aa {} EE aa {} + EE bb {} EE bb {} EE bb {} FF aa {} FF aa {} FF aa {} FF aa {} + FF bb {} FF bb {} FF bb {} FF bb {} FF bb {} FF bb {} GG aa {} + GG aa {} GG aa {} GG aa {} GG bb {} GG bb {} GG bb {} GG bb {} + HH aa {} HH aa {} HH aa {} HH bb {} HH bb {} HH bb {} HH bb {} + HH bb {} HH bb {} II aa {} II aa {} II bb {} II bb {} II bb {} + II bb {} II bb {} JJ aa {} JJ aa {} JJ aa {} JJ aa {} JJ bb {} + JJ bb {} JJ bb {} JJ bb {}} + +do_execsql_test 1.5.8 { + SELECT a, b, + sum(c) OVER (ORDER BY a GROUPS BETWEEN 1 PRECEDING AND 2 PRECEDING EXCLUDE CURRENT ROW), + sum(c) OVER (ORDER BY a GROUPS BETWEEN 1 PRECEDING AND 2 PRECEDING ), + sum(c) OVER (ORDER BY a,b GROUPS BETWEEN 1 PRECEDING AND 2 PRECEDING EXCLUDE CURRENT ROW), + sum(c) OVER (ORDER BY a,b GROUPS BETWEEN 1 PRECEDING AND 2 PRECEDING ) + FROM t3 ORDER BY 1, 2, 3; +} {AA aa {} {} {} {} AA aa {} {} {} {} AA aa {} {} {} {} + AA aa {} {} {} {} AA bb {} {} {} {} AA bb {} {} {} {} + AA bb {} {} {} {} AA bb {} {} {} {} BB aa {} {} {} {} + BB aa {} {} {} {} BB aa {} {} {} {} BB aa {} {} {} {} + BB aa {} {} {} {} BB aa {} {} {} {} BB bb {} {} {} {} + BB bb {} {} {} {} BB bb {} {} {} {} BB bb {} {} {} {} + BB bb {} {} {} {} BB bb {} {} {} {} CC aa {} {} {} {} + CC aa {} {} {} {} CC aa {} {} {} {} CC aa {} {} {} {} + CC bb {} {} {} {} CC bb {} {} {} {} DD aa {} {} {} {} + DD aa {} {} {} {} DD aa {} {} {} {} DD bb {} {} {} {} + DD bb {} {} {} {} DD bb {} {} {} {} DD bb {} {} {} {} + EE aa {} {} {} {} EE aa {} {} {} {} EE bb {} {} {} {} + EE bb {} {} {} {} EE bb {} {} {} {} FF aa {} {} {} {} + FF aa {} {} {} {} FF aa {} {} {} {} FF aa {} {} {} {} + FF bb {} {} {} {} FF bb {} {} {} {} FF bb {} {} {} {} + FF bb {} {} {} {} FF bb {} {} {} {} FF bb {} {} {} {} + GG aa {} {} {} {} GG aa {} {} {} {} GG aa {} {} {} {} + GG aa {} {} {} {} GG bb {} {} {} {} GG bb {} {} {} {} + GG bb {} {} {} {} GG bb {} {} {} {} HH aa {} {} {} {} + HH aa {} {} {} {} HH aa {} {} {} {} HH bb {} {} {} {} + HH bb {} {} {} {} HH bb {} {} {} {} HH bb {} {} {} {} + HH bb {} {} {} {} HH bb {} {} {} {} II aa {} {} {} {} + II aa {} {} {} {} II bb {} {} {} {} II bb {} {} {} {} + II bb {} {} {} {} II bb {} {} {} {} II bb {} {} {} {} + JJ aa {} {} {} {} JJ aa {} {} {} {} JJ aa {} {} {} {} + JJ aa {} {} {} {} JJ bb {} {} {} {} JJ bb {} {} {} {} + JJ bb {} {} {} {} JJ bb {} {} {} {}} + +do_execsql_test 1.6.1 { + SELECT a, b, sum(c) OVER (ORDER BY a GROUPS BETWEEN 2 PRECEDING AND 1 PRECEDING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa {} AA aa {} AA aa {} AA aa {} AA bb {} AA bb {} AA bb {} + AA bb {} BB aa 4685 BB aa 4685 BB aa 4685 BB aa 4685 BB aa 4685 + BB aa 4685 BB bb 4685 BB bb 4685 BB bb 4685 BB bb 4685 BB bb 4685 + BB bb 4685 CC aa 12025 CC aa 12025 CC aa 12025 CC aa 12025 + CC bb 12025 CC bb 12025 DD aa 10462 DD aa 10462 DD aa 10462 + DD bb 10462 DD bb 10462 DD bb 10462 DD bb 10462 EE aa 7154 + EE aa 7154 EE bb 7154 EE bb 7154 EE bb 7154 FF aa 6571 FF aa 6571 + FF aa 6571 FF aa 6571 FF bb 6571 FF bb 6571 FF bb 6571 FF bb 6571 + FF bb 6571 FF bb 6571 GG aa 8207 GG aa 8207 GG aa 8207 GG aa 8207 + GG bb 8207 GG bb 8207 GG bb 8207 GG bb 8207 HH aa 10136 + HH aa 10136 HH aa 10136 HH bb 10136 HH bb 10136 HH bb 10136 + HH bb 10136 HH bb 10136 HH bb 10136 II aa 9911 II aa 9911 + II bb 9911 II bb 9911 II bb 9911 II bb 9911 II bb 9911 JJ aa 9083 + JJ aa 9083 JJ aa 9083 JJ aa 9083 JJ bb 9083 JJ bb 9083 JJ bb 9083 + JJ bb 9083} + +do_execsql_test 1.6.2 { + SELECT a, b, sum(c) OVER (ORDER BY a,b GROUPS BETWEEN 2 PRECEDING AND 1 PRECEDING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa {} AA aa {} AA aa {} AA aa {} AA bb 2307 AA bb 2307 + AA bb 2307 AA bb 2307 BB aa 4685 BB aa 4685 BB aa 4685 BB aa 4685 + BB aa 4685 BB aa 4685 BB bb 5251 BB bb 5251 BB bb 5251 BB bb 5251 + BB bb 5251 BB bb 5251 CC aa 7340 CC aa 7340 CC aa 7340 CC aa 7340 + CC bb 6421 CC bb 6421 DD aa 3122 DD aa 3122 DD aa 3122 DD bb 2493 + DD bb 2493 DD bb 2493 DD bb 2493 EE aa 4032 EE aa 4032 EE bb 3597 + EE bb 3597 EE bb 3597 FF aa 2539 FF aa 2539 FF aa 2539 FF aa 2539 + FF bb 3812 FF bb 3812 FF bb 3812 FF bb 3812 FF bb 3812 FF bb 3812 + GG aa 5668 GG aa 5668 GG aa 5668 GG aa 5668 GG bb 5102 GG bb 5102 + GG bb 5102 GG bb 5102 HH aa 4468 HH aa 4468 HH aa 4468 HH bb 5120 + HH bb 5120 HH bb 5120 HH bb 5120 HH bb 5120 HH bb 5120 II aa 5443 + II aa 5443 II bb 4244 II bb 4244 II bb 4244 II bb 4244 II bb 4244 + JJ aa 3640 JJ aa 3640 JJ aa 3640 JJ aa 3640 JJ bb 4604 JJ bb 4604 + JJ bb 4604 JJ bb 4604} + +do_execsql_test 1.6.3 { + SELECT a, b, rank() OVER (ORDER BY a GROUPS BETWEEN 2 PRECEDING AND 1 PRECEDING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 1 AA aa 1 AA aa 1 AA aa 1 AA bb 1 AA bb 1 AA bb 1 + AA bb 1 BB aa 9 BB aa 9 BB aa 9 BB aa 9 BB aa 9 BB aa 9 + BB bb 9 BB bb 9 BB bb 9 BB bb 9 BB bb 9 BB bb 9 CC aa 21 + CC aa 21 CC aa 21 CC aa 21 CC bb 21 CC bb 21 DD aa 27 DD aa 27 + DD aa 27 DD bb 27 DD bb 27 DD bb 27 DD bb 27 EE aa 34 EE aa 34 + EE bb 34 EE bb 34 EE bb 34 FF aa 39 FF aa 39 FF aa 39 FF aa 39 + FF bb 39 FF bb 39 FF bb 39 FF bb 39 FF bb 39 FF bb 39 GG aa 49 + GG aa 49 GG aa 49 GG aa 49 GG bb 49 GG bb 49 GG bb 49 GG bb 49 + HH aa 57 HH aa 57 HH aa 57 HH bb 57 HH bb 57 HH bb 57 HH bb 57 + HH bb 57 HH bb 57 II aa 66 II aa 66 II bb 66 II bb 66 II bb 66 + II bb 66 II bb 66 JJ aa 73 JJ aa 73 JJ aa 73 JJ aa 73 JJ bb 73 + JJ bb 73 JJ bb 73 JJ bb 73} + +do_execsql_test 1.6.4 { + SELECT a, b, max(c) OVER (ORDER BY a,b GROUPS BETWEEN 2 PRECEDING AND 1 PRECEDING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa {} AA aa {} AA aa {} AA aa {} AA bb 934 AA bb 934 + AA bb 934 AA bb 934 BB aa 934 BB aa 934 BB aa 934 BB aa 934 + BB aa 934 BB aa 934 BB bb 870 BB bb 870 BB bb 870 BB bb 870 + BB bb 870 BB bb 870 CC aa 840 CC aa 840 CC aa 840 CC aa 840 + CC bb 840 CC bb 840 DD aa 822 DD aa 822 DD aa 822 DD bb 845 + DD bb 845 DD bb 845 DD bb 845 EE aa 959 EE aa 959 EE bb 959 + EE bb 959 EE bb 959 FF aa 777 FF aa 777 FF aa 777 FF aa 777 + FF bb 768 FF bb 768 FF bb 768 FF bb 768 FF bb 768 FF bb 768 + GG aa 938 GG aa 938 GG aa 938 GG aa 938 GG bb 938 GG bb 938 + GG bb 938 GG bb 938 HH aa 938 HH aa 938 HH aa 938 HH bb 979 + HH bb 979 HH bb 979 HH bb 979 HH bb 979 HH bb 979 II aa 979 + II aa 979 II bb 963 II bb 963 II bb 963 II bb 963 II bb 963 + JJ aa 805 JJ aa 805 JJ aa 805 JJ aa 805 JJ bb 899 JJ bb 899 + JJ bb 899 JJ bb 899} + +do_execsql_test 1.6.5 { + SELECT a, b, min(c) OVER (ORDER BY a,b GROUPS BETWEEN 2 PRECEDING AND 1 PRECEDING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa {} AA aa {} AA aa {} AA aa {} AA bb 223 AA bb 223 + AA bb 223 AA bb 223 BB aa 223 BB aa 223 BB aa 223 BB aa 223 + BB aa 223 BB aa 223 BB bb 247 BB bb 247 BB bb 247 BB bb 247 + BB bb 247 BB bb 247 CC aa 247 CC aa 247 CC aa 247 CC aa 247 + CC bb 158 CC bb 158 DD aa 158 DD aa 158 DD aa 158 DD bb 224 + DD bb 224 DD bb 224 DD bb 224 EE aa 224 EE aa 224 EE bb 113 + EE bb 113 EE bb 113 FF aa 113 FF aa 113 FF aa 113 FF aa 113 + FF bb 208 FF bb 208 FF bb 208 FF bb 208 FF bb 208 FF bb 208 + GG aa 102 GG aa 102 GG aa 102 GG aa 102 GG bb 102 GG bb 102 + GG bb 102 GG bb 102 HH aa 148 HH aa 148 HH aa 148 HH bb 160 + HH bb 160 HH bb 160 HH bb 160 HH bb 160 HH bb 160 II aa 133 + II aa 133 II bb 133 II bb 133 II bb 133 II bb 133 II bb 133 + JJ aa 250 JJ aa 250 JJ aa 250 JJ aa 250 JJ bb 113 JJ bb 113 + JJ bb 113 JJ bb 113} + +do_execsql_test 1.6.6 { + SELECT a, b, sum(c) OVER (ORDER BY a GROUPS BETWEEN 2 PRECEDING AND 1 PRECEDING EXCLUDE CURRENT ROW) FROM t3 ORDER BY 1, 2, 3; +} {AA aa {} AA aa {} AA aa {} AA aa {} AA bb {} AA bb {} AA bb {} + AA bb {} BB aa 4685 BB aa 4685 BB aa 4685 BB aa 4685 BB aa 4685 + BB aa 4685 BB bb 4685 BB bb 4685 BB bb 4685 BB bb 4685 BB bb 4685 + BB bb 4685 CC aa 12025 CC aa 12025 CC aa 12025 CC aa 12025 + CC bb 12025 CC bb 12025 DD aa 10462 DD aa 10462 DD aa 10462 + DD bb 10462 DD bb 10462 DD bb 10462 DD bb 10462 EE aa 7154 + EE aa 7154 EE bb 7154 EE bb 7154 EE bb 7154 FF aa 6571 FF aa 6571 + FF aa 6571 FF aa 6571 FF bb 6571 FF bb 6571 FF bb 6571 FF bb 6571 + FF bb 6571 FF bb 6571 GG aa 8207 GG aa 8207 GG aa 8207 GG aa 8207 + GG bb 8207 GG bb 8207 GG bb 8207 GG bb 8207 HH aa 10136 + HH aa 10136 HH aa 10136 HH bb 10136 HH bb 10136 HH bb 10136 + HH bb 10136 HH bb 10136 HH bb 10136 II aa 9911 II aa 9911 + II bb 9911 II bb 9911 II bb 9911 II bb 9911 II bb 9911 JJ aa 9083 + JJ aa 9083 JJ aa 9083 JJ aa 9083 JJ bb 9083 JJ bb 9083 JJ bb 9083 + JJ bb 9083} + +do_execsql_test 1.6.7 { + SELECT a, b, sum(c) OVER (ORDER BY a,b GROUPS BETWEEN 2 PRECEDING AND 1 PRECEDING EXCLUDE CURRENT ROW) FROM t3 ORDER BY 1, 2, 3; +} {AA aa {} AA aa {} AA aa {} AA aa {} AA bb 2307 AA bb 2307 + AA bb 2307 AA bb 2307 BB aa 4685 BB aa 4685 BB aa 4685 BB aa 4685 + BB aa 4685 BB aa 4685 BB bb 5251 BB bb 5251 BB bb 5251 BB bb 5251 + BB bb 5251 BB bb 5251 CC aa 7340 CC aa 7340 CC aa 7340 CC aa 7340 + CC bb 6421 CC bb 6421 DD aa 3122 DD aa 3122 DD aa 3122 DD bb 2493 + DD bb 2493 DD bb 2493 DD bb 2493 EE aa 4032 EE aa 4032 EE bb 3597 + EE bb 3597 EE bb 3597 FF aa 2539 FF aa 2539 FF aa 2539 FF aa 2539 + FF bb 3812 FF bb 3812 FF bb 3812 FF bb 3812 FF bb 3812 FF bb 3812 + GG aa 5668 GG aa 5668 GG aa 5668 GG aa 5668 GG bb 5102 GG bb 5102 + GG bb 5102 GG bb 5102 HH aa 4468 HH aa 4468 HH aa 4468 HH bb 5120 + HH bb 5120 HH bb 5120 HH bb 5120 HH bb 5120 HH bb 5120 II aa 5443 + II aa 5443 II bb 4244 II bb 4244 II bb 4244 II bb 4244 II bb 4244 + JJ aa 3640 JJ aa 3640 JJ aa 3640 JJ aa 3640 JJ bb 4604 JJ bb 4604 + JJ bb 4604 JJ bb 4604} + +do_execsql_test 1.6.8 { + SELECT a, b, + sum(c) OVER (ORDER BY a GROUPS BETWEEN 2 PRECEDING AND 1 PRECEDING EXCLUDE CURRENT ROW), + sum(c) OVER (ORDER BY a GROUPS BETWEEN 2 PRECEDING AND 1 PRECEDING ), + sum(c) OVER (ORDER BY a,b GROUPS BETWEEN 2 PRECEDING AND 1 PRECEDING EXCLUDE CURRENT ROW), + sum(c) OVER (ORDER BY a,b GROUPS BETWEEN 2 PRECEDING AND 1 PRECEDING ) + FROM t3 ORDER BY 1, 2, 3; +} {AA aa {} {} {} {} AA aa {} {} {} {} AA aa {} {} {} {} + AA aa {} {} {} {} AA bb {} {} 2307 2307 AA bb {} {} 2307 2307 + AA bb {} {} 2307 2307 AA bb {} {} 2307 2307 BB aa 4685 4685 4685 4685 + BB aa 4685 4685 4685 4685 BB aa 4685 4685 4685 4685 + BB aa 4685 4685 4685 4685 BB aa 4685 4685 4685 4685 + BB aa 4685 4685 4685 4685 BB bb 4685 4685 5251 5251 + BB bb 4685 4685 5251 5251 BB bb 4685 4685 5251 5251 + BB bb 4685 4685 5251 5251 BB bb 4685 4685 5251 5251 + BB bb 4685 4685 5251 5251 CC aa 12025 12025 7340 7340 + CC aa 12025 12025 7340 7340 CC aa 12025 12025 7340 7340 + CC aa 12025 12025 7340 7340 CC bb 12025 12025 6421 6421 + CC bb 12025 12025 6421 6421 DD aa 10462 10462 3122 3122 + DD aa 10462 10462 3122 3122 DD aa 10462 10462 3122 3122 + DD bb 10462 10462 2493 2493 DD bb 10462 10462 2493 2493 + DD bb 10462 10462 2493 2493 DD bb 10462 10462 2493 2493 + EE aa 7154 7154 4032 4032 EE aa 7154 7154 4032 4032 + EE bb 7154 7154 3597 3597 EE bb 7154 7154 3597 3597 + EE bb 7154 7154 3597 3597 FF aa 6571 6571 2539 2539 + FF aa 6571 6571 2539 2539 FF aa 6571 6571 2539 2539 + FF aa 6571 6571 2539 2539 FF bb 6571 6571 3812 3812 + FF bb 6571 6571 3812 3812 FF bb 6571 6571 3812 3812 + FF bb 6571 6571 3812 3812 FF bb 6571 6571 3812 3812 + FF bb 6571 6571 3812 3812 GG aa 8207 8207 5668 5668 + GG aa 8207 8207 5668 5668 GG aa 8207 8207 5668 5668 + GG aa 8207 8207 5668 5668 GG bb 8207 8207 5102 5102 + GG bb 8207 8207 5102 5102 GG bb 8207 8207 5102 5102 + GG bb 8207 8207 5102 5102 HH aa 10136 10136 4468 4468 + HH aa 10136 10136 4468 4468 HH aa 10136 10136 4468 4468 + HH bb 10136 10136 5120 5120 HH bb 10136 10136 5120 5120 + HH bb 10136 10136 5120 5120 HH bb 10136 10136 5120 5120 + HH bb 10136 10136 5120 5120 HH bb 10136 10136 5120 5120 + II aa 9911 9911 5443 5443 II aa 9911 9911 5443 5443 + II bb 9911 9911 4244 4244 II bb 9911 9911 4244 4244 + II bb 9911 9911 4244 4244 II bb 9911 9911 4244 4244 + II bb 9911 9911 4244 4244 JJ aa 9083 9083 3640 3640 + JJ aa 9083 9083 3640 3640 JJ aa 9083 9083 3640 3640 + JJ aa 9083 9083 3640 3640 JJ bb 9083 9083 4604 4604 + JJ bb 9083 9083 4604 4604 JJ bb 9083 9083 4604 4604 + JJ bb 9083 9083 4604 4604} + +do_execsql_test 1.7.1 { + SELECT a, b, sum(c) OVER (ORDER BY a GROUPS BETWEEN 3 PRECEDING AND 1 PRECEDING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa {} AA aa {} AA aa {} AA aa {} AA bb {} AA bb {} AA bb {} + AA bb {} BB aa 4685 BB aa 4685 BB aa 4685 BB aa 4685 BB aa 4685 + BB aa 4685 BB bb 4685 BB bb 4685 BB bb 4685 BB bb 4685 BB bb 4685 + BB bb 4685 CC aa 12025 CC aa 12025 CC aa 12025 CC aa 12025 + CC bb 12025 CC bb 12025 DD aa 15147 DD aa 15147 DD aa 15147 + DD bb 15147 DD bb 15147 DD bb 15147 DD bb 15147 EE aa 14494 + EE aa 14494 EE bb 14494 EE bb 14494 EE bb 14494 FF aa 9693 + FF aa 9693 FF aa 9693 FF aa 9693 FF bb 9693 FF bb 9693 FF bb 9693 + FF bb 9693 FF bb 9693 FF bb 9693 GG aa 12239 GG aa 12239 + GG aa 12239 GG aa 12239 GG bb 12239 GG bb 12239 GG bb 12239 + GG bb 12239 HH aa 12675 HH aa 12675 HH aa 12675 HH bb 12675 + HH bb 12675 HH bb 12675 HH bb 12675 HH bb 12675 HH bb 12675 + II aa 15579 II aa 15579 II bb 15579 II bb 15579 II bb 15579 + II bb 15579 II bb 15579 JJ aa 13551 JJ aa 13551 JJ aa 13551 + JJ aa 13551 JJ bb 13551 JJ bb 13551 JJ bb 13551 JJ bb 13551} + +do_execsql_test 1.7.2 { + SELECT a, b, sum(c) OVER (ORDER BY a,b GROUPS BETWEEN 3 PRECEDING AND 1 PRECEDING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa {} AA aa {} AA aa {} AA aa {} AA bb 2307 AA bb 2307 + AA bb 2307 AA bb 2307 BB aa 4685 BB aa 4685 BB aa 4685 BB aa 4685 + BB aa 4685 BB aa 4685 BB bb 7558 BB bb 7558 BB bb 7558 BB bb 7558 + BB bb 7558 BB bb 7558 CC aa 9718 CC aa 9718 CC aa 9718 CC aa 9718 + CC bb 9294 CC bb 9294 DD aa 7589 DD aa 7589 DD aa 7589 DD bb 4447 + DD bb 4447 DD bb 4447 DD bb 4447 EE aa 5200 EE aa 5200 EE bb 4922 + EE bb 4922 EE bb 4922 FF aa 5246 FF aa 5246 FF aa 5246 FF aa 5246 + FF bb 4702 FF bb 4702 FF bb 4702 FF bb 4702 FF bb 4702 FF bb 4702 + GG aa 7317 GG aa 7317 GG aa 7317 GG aa 7317 GG bb 7265 GG bb 7265 + GG bb 7265 GG bb 7265 HH aa 7973 HH aa 7973 HH aa 7973 HH bb 6717 + HH bb 6717 HH bb 6717 HH bb 6717 HH bb 6717 HH bb 6717 II aa 8314 + II aa 8314 II bb 6493 II bb 6493 II bb 6493 II bb 6493 II bb 6493 + JJ aa 6834 JJ aa 6834 JJ aa 6834 JJ aa 6834 JJ bb 5654 JJ bb 5654 + JJ bb 5654 JJ bb 5654} + +do_execsql_test 1.7.3 { + SELECT a, b, rank() OVER (ORDER BY a GROUPS BETWEEN 3 PRECEDING AND 1 PRECEDING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 1 AA aa 1 AA aa 1 AA aa 1 AA bb 1 AA bb 1 AA bb 1 + AA bb 1 BB aa 9 BB aa 9 BB aa 9 BB aa 9 BB aa 9 BB aa 9 + BB bb 9 BB bb 9 BB bb 9 BB bb 9 BB bb 9 BB bb 9 CC aa 21 + CC aa 21 CC aa 21 CC aa 21 CC bb 21 CC bb 21 DD aa 27 DD aa 27 + DD aa 27 DD bb 27 DD bb 27 DD bb 27 DD bb 27 EE aa 34 EE aa 34 + EE bb 34 EE bb 34 EE bb 34 FF aa 39 FF aa 39 FF aa 39 FF aa 39 + FF bb 39 FF bb 39 FF bb 39 FF bb 39 FF bb 39 FF bb 39 GG aa 49 + GG aa 49 GG aa 49 GG aa 49 GG bb 49 GG bb 49 GG bb 49 GG bb 49 + HH aa 57 HH aa 57 HH aa 57 HH bb 57 HH bb 57 HH bb 57 HH bb 57 + HH bb 57 HH bb 57 II aa 66 II aa 66 II bb 66 II bb 66 II bb 66 + II bb 66 II bb 66 JJ aa 73 JJ aa 73 JJ aa 73 JJ aa 73 JJ bb 73 + JJ bb 73 JJ bb 73 JJ bb 73} + +do_execsql_test 1.7.4 { + SELECT a, b, max(c) OVER (ORDER BY a,b GROUPS BETWEEN 3 PRECEDING AND 1 PRECEDING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa {} AA aa {} AA aa {} AA aa {} AA bb 934 AA bb 934 + AA bb 934 AA bb 934 BB aa 934 BB aa 934 BB aa 934 BB aa 934 + BB aa 934 BB aa 934 BB bb 934 BB bb 934 BB bb 934 BB bb 934 + BB bb 934 BB bb 934 CC aa 870 CC aa 870 CC aa 870 CC aa 870 + CC bb 840 CC bb 840 DD aa 840 DD aa 840 DD aa 840 DD bb 845 + DD bb 845 DD bb 845 DD bb 845 EE aa 959 EE aa 959 EE bb 959 + EE bb 959 EE bb 959 FF aa 959 FF aa 959 FF aa 959 FF aa 959 + FF bb 777 FF bb 777 FF bb 777 FF bb 777 FF bb 777 FF bb 777 + GG aa 938 GG aa 938 GG aa 938 GG aa 938 GG bb 938 GG bb 938 + GG bb 938 GG bb 938 HH aa 938 HH aa 938 HH aa 938 HH bb 979 + HH bb 979 HH bb 979 HH bb 979 HH bb 979 HH bb 979 II aa 979 + II aa 979 II bb 979 II bb 979 II bb 979 II bb 979 II bb 979 + JJ aa 963 JJ aa 963 JJ aa 963 JJ aa 963 JJ bb 899 JJ bb 899 + JJ bb 899 JJ bb 899} + +do_execsql_test 1.7.5 { + SELECT a, b, min(c) OVER (ORDER BY a,b GROUPS BETWEEN 3 PRECEDING AND 1 PRECEDING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa {} AA aa {} AA aa {} AA aa {} AA bb 223 AA bb 223 + AA bb 223 AA bb 223 BB aa 223 BB aa 223 BB aa 223 BB aa 223 + BB aa 223 BB aa 223 BB bb 223 BB bb 223 BB bb 223 BB bb 223 + BB bb 223 BB bb 223 CC aa 247 CC aa 247 CC aa 247 CC aa 247 + CC bb 158 CC bb 158 DD aa 158 DD aa 158 DD aa 158 DD bb 158 + DD bb 158 DD bb 158 DD bb 158 EE aa 224 EE aa 224 EE bb 113 + EE bb 113 EE bb 113 FF aa 113 FF aa 113 FF aa 113 FF aa 113 + FF bb 113 FF bb 113 FF bb 113 FF bb 113 FF bb 113 FF bb 113 + GG aa 102 GG aa 102 GG aa 102 GG aa 102 GG bb 102 GG bb 102 + GG bb 102 GG bb 102 HH aa 102 HH aa 102 HH aa 102 HH bb 148 + HH bb 148 HH bb 148 HH bb 148 HH bb 148 HH bb 148 II aa 133 + II aa 133 II bb 133 II bb 133 II bb 133 II bb 133 II bb 133 + JJ aa 133 JJ aa 133 JJ aa 133 JJ aa 133 JJ bb 113 JJ bb 113 + JJ bb 113 JJ bb 113} + +do_execsql_test 1.7.6 { + SELECT a, b, sum(c) OVER (ORDER BY a GROUPS BETWEEN 3 PRECEDING AND 1 PRECEDING EXCLUDE CURRENT ROW) FROM t3 ORDER BY 1, 2, 3; +} {AA aa {} AA aa {} AA aa {} AA aa {} AA bb {} AA bb {} AA bb {} + AA bb {} BB aa 4685 BB aa 4685 BB aa 4685 BB aa 4685 BB aa 4685 + BB aa 4685 BB bb 4685 BB bb 4685 BB bb 4685 BB bb 4685 BB bb 4685 + BB bb 4685 CC aa 12025 CC aa 12025 CC aa 12025 CC aa 12025 + CC bb 12025 CC bb 12025 DD aa 15147 DD aa 15147 DD aa 15147 + DD bb 15147 DD bb 15147 DD bb 15147 DD bb 15147 EE aa 14494 + EE aa 14494 EE bb 14494 EE bb 14494 EE bb 14494 FF aa 9693 + FF aa 9693 FF aa 9693 FF aa 9693 FF bb 9693 FF bb 9693 FF bb 9693 + FF bb 9693 FF bb 9693 FF bb 9693 GG aa 12239 GG aa 12239 + GG aa 12239 GG aa 12239 GG bb 12239 GG bb 12239 GG bb 12239 + GG bb 12239 HH aa 12675 HH aa 12675 HH aa 12675 HH bb 12675 + HH bb 12675 HH bb 12675 HH bb 12675 HH bb 12675 HH bb 12675 + II aa 15579 II aa 15579 II bb 15579 II bb 15579 II bb 15579 + II bb 15579 II bb 15579 JJ aa 13551 JJ aa 13551 JJ aa 13551 + JJ aa 13551 JJ bb 13551 JJ bb 13551 JJ bb 13551 JJ bb 13551} + +do_execsql_test 1.7.7 { + SELECT a, b, sum(c) OVER (ORDER BY a,b GROUPS BETWEEN 3 PRECEDING AND 1 PRECEDING EXCLUDE CURRENT ROW) FROM t3 ORDER BY 1, 2, 3; +} {AA aa {} AA aa {} AA aa {} AA aa {} AA bb 2307 AA bb 2307 + AA bb 2307 AA bb 2307 BB aa 4685 BB aa 4685 BB aa 4685 BB aa 4685 + BB aa 4685 BB aa 4685 BB bb 7558 BB bb 7558 BB bb 7558 BB bb 7558 + BB bb 7558 BB bb 7558 CC aa 9718 CC aa 9718 CC aa 9718 CC aa 9718 + CC bb 9294 CC bb 9294 DD aa 7589 DD aa 7589 DD aa 7589 DD bb 4447 + DD bb 4447 DD bb 4447 DD bb 4447 EE aa 5200 EE aa 5200 EE bb 4922 + EE bb 4922 EE bb 4922 FF aa 5246 FF aa 5246 FF aa 5246 FF aa 5246 + FF bb 4702 FF bb 4702 FF bb 4702 FF bb 4702 FF bb 4702 FF bb 4702 + GG aa 7317 GG aa 7317 GG aa 7317 GG aa 7317 GG bb 7265 GG bb 7265 + GG bb 7265 GG bb 7265 HH aa 7973 HH aa 7973 HH aa 7973 HH bb 6717 + HH bb 6717 HH bb 6717 HH bb 6717 HH bb 6717 HH bb 6717 II aa 8314 + II aa 8314 II bb 6493 II bb 6493 II bb 6493 II bb 6493 II bb 6493 + JJ aa 6834 JJ aa 6834 JJ aa 6834 JJ aa 6834 JJ bb 5654 JJ bb 5654 + JJ bb 5654 JJ bb 5654} + +do_execsql_test 1.7.8 { + SELECT a, b, + sum(c) OVER (ORDER BY a GROUPS BETWEEN 3 PRECEDING AND 1 PRECEDING EXCLUDE CURRENT ROW), + sum(c) OVER (ORDER BY a GROUPS BETWEEN 3 PRECEDING AND 1 PRECEDING ), + sum(c) OVER (ORDER BY a,b GROUPS BETWEEN 3 PRECEDING AND 1 PRECEDING EXCLUDE CURRENT ROW), + sum(c) OVER (ORDER BY a,b GROUPS BETWEEN 3 PRECEDING AND 1 PRECEDING ) + FROM t3 ORDER BY 1, 2, 3; +} {AA aa {} {} {} {} AA aa {} {} {} {} AA aa {} {} {} {} + AA aa {} {} {} {} AA bb {} {} 2307 2307 AA bb {} {} 2307 2307 + AA bb {} {} 2307 2307 AA bb {} {} 2307 2307 BB aa 4685 4685 4685 4685 + BB aa 4685 4685 4685 4685 BB aa 4685 4685 4685 4685 + BB aa 4685 4685 4685 4685 BB aa 4685 4685 4685 4685 + BB aa 4685 4685 4685 4685 BB bb 4685 4685 7558 7558 + BB bb 4685 4685 7558 7558 BB bb 4685 4685 7558 7558 + BB bb 4685 4685 7558 7558 BB bb 4685 4685 7558 7558 + BB bb 4685 4685 7558 7558 CC aa 12025 12025 9718 9718 + CC aa 12025 12025 9718 9718 CC aa 12025 12025 9718 9718 + CC aa 12025 12025 9718 9718 CC bb 12025 12025 9294 9294 + CC bb 12025 12025 9294 9294 DD aa 15147 15147 7589 7589 + DD aa 15147 15147 7589 7589 DD aa 15147 15147 7589 7589 + DD bb 15147 15147 4447 4447 DD bb 15147 15147 4447 4447 + DD bb 15147 15147 4447 4447 DD bb 15147 15147 4447 4447 + EE aa 14494 14494 5200 5200 EE aa 14494 14494 5200 5200 + EE bb 14494 14494 4922 4922 EE bb 14494 14494 4922 4922 + EE bb 14494 14494 4922 4922 FF aa 9693 9693 5246 5246 + FF aa 9693 9693 5246 5246 FF aa 9693 9693 5246 5246 + FF aa 9693 9693 5246 5246 FF bb 9693 9693 4702 4702 + FF bb 9693 9693 4702 4702 FF bb 9693 9693 4702 4702 + FF bb 9693 9693 4702 4702 FF bb 9693 9693 4702 4702 + FF bb 9693 9693 4702 4702 GG aa 12239 12239 7317 7317 + GG aa 12239 12239 7317 7317 GG aa 12239 12239 7317 7317 + GG aa 12239 12239 7317 7317 GG bb 12239 12239 7265 7265 + GG bb 12239 12239 7265 7265 GG bb 12239 12239 7265 7265 + GG bb 12239 12239 7265 7265 HH aa 12675 12675 7973 7973 + HH aa 12675 12675 7973 7973 HH aa 12675 12675 7973 7973 + HH bb 12675 12675 6717 6717 HH bb 12675 12675 6717 6717 + HH bb 12675 12675 6717 6717 HH bb 12675 12675 6717 6717 + HH bb 12675 12675 6717 6717 HH bb 12675 12675 6717 6717 + II aa 15579 15579 8314 8314 II aa 15579 15579 8314 8314 + II bb 15579 15579 6493 6493 II bb 15579 15579 6493 6493 + II bb 15579 15579 6493 6493 II bb 15579 15579 6493 6493 + II bb 15579 15579 6493 6493 JJ aa 13551 13551 6834 6834 + JJ aa 13551 13551 6834 6834 JJ aa 13551 13551 6834 6834 + JJ aa 13551 13551 6834 6834 JJ bb 13551 13551 5654 5654 + JJ bb 13551 13551 5654 5654 JJ bb 13551 13551 5654 5654 + JJ bb 13551 13551 5654 5654} + +do_execsql_test 1.8.1 { + SELECT a, b, sum(c) OVER (ORDER BY a GROUPS BETWEEN 3 PRECEDING AND 0 PRECEDING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 4685 AA aa 4685 AA aa 4685 AA aa 4685 AA bb 4685 AA bb 4685 + AA bb 4685 AA bb 4685 BB aa 12025 BB aa 12025 BB aa 12025 + BB aa 12025 BB aa 12025 BB aa 12025 BB bb 12025 BB bb 12025 + BB bb 12025 BB bb 12025 BB bb 12025 BB bb 12025 CC aa 15147 + CC aa 15147 CC aa 15147 CC aa 15147 CC bb 15147 CC bb 15147 + DD aa 19179 DD aa 19179 DD aa 19179 DD bb 19179 DD bb 19179 + DD bb 19179 DD bb 19179 EE aa 17033 EE aa 17033 EE bb 17033 + EE bb 17033 EE bb 17033 FF aa 15361 FF aa 15361 FF aa 15361 + FF aa 15361 FF bb 15361 FF bb 15361 FF bb 15361 FF bb 15361 + FF bb 15361 FF bb 15361 GG aa 16707 GG aa 16707 GG aa 16707 + GG aa 16707 GG bb 16707 GG bb 16707 GG bb 16707 GG bb 16707 + HH aa 18118 HH aa 18118 HH aa 18118 HH bb 18118 HH bb 18118 + HH bb 18118 HH bb 18118 HH bb 18118 HH bb 18118 II aa 19219 + II aa 19219 II bb 19219 II bb 19219 II bb 19219 II bb 19219 + II bb 19219 JJ aa 17351 JJ aa 17351 JJ aa 17351 JJ aa 17351 + JJ bb 17351 JJ bb 17351 JJ bb 17351 JJ bb 17351} + +do_execsql_test 1.8.2 { + SELECT a, b, sum(c) OVER (ORDER BY a,b GROUPS BETWEEN 3 PRECEDING AND 0 PRECEDING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 2307 AA aa 2307 AA aa 2307 AA aa 2307 AA bb 4685 AA bb 4685 + AA bb 4685 AA bb 4685 BB aa 7558 BB aa 7558 BB aa 7558 BB aa 7558 + BB aa 7558 BB aa 7558 BB bb 12025 BB bb 12025 BB bb 12025 + BB bb 12025 BB bb 12025 BB bb 12025 CC aa 11672 CC aa 11672 + CC aa 11672 CC aa 11672 CC bb 10462 CC bb 10462 DD aa 8914 + DD aa 8914 DD aa 8914 DD bb 7154 DD bb 7154 DD bb 7154 DD bb 7154 + EE aa 6090 EE aa 6090 EE bb 6571 EE bb 6571 EE bb 6571 FF aa 7409 + FF aa 7409 FF aa 7409 FF aa 7409 FF bb 8207 FF bb 8207 FF bb 8207 + FF bb 8207 FF bb 8207 FF bb 8207 GG aa 8914 GG aa 8914 GG aa 8914 + GG aa 8914 GG bb 10136 GG bb 10136 GG bb 10136 GG bb 10136 + HH aa 10222 HH aa 10222 HH aa 10222 HH bb 9911 HH bb 9911 + HH bb 9911 HH bb 9911 HH bb 9911 HH bb 9911 II aa 9364 II aa 9364 + II bb 9083 II bb 9083 II bb 9083 II bb 9083 II bb 9083 JJ aa 8848 + JJ aa 8848 JJ aa 8848 JJ aa 8848 JJ bb 7440 JJ bb 7440 JJ bb 7440 + JJ bb 7440} + +do_execsql_test 1.8.3 { + SELECT a, b, rank() OVER (ORDER BY a GROUPS BETWEEN 3 PRECEDING AND 0 PRECEDING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 1 AA aa 1 AA aa 1 AA aa 1 AA bb 1 AA bb 1 AA bb 1 + AA bb 1 BB aa 9 BB aa 9 BB aa 9 BB aa 9 BB aa 9 BB aa 9 + BB bb 9 BB bb 9 BB bb 9 BB bb 9 BB bb 9 BB bb 9 CC aa 21 + CC aa 21 CC aa 21 CC aa 21 CC bb 21 CC bb 21 DD aa 27 DD aa 27 + DD aa 27 DD bb 27 DD bb 27 DD bb 27 DD bb 27 EE aa 34 EE aa 34 + EE bb 34 EE bb 34 EE bb 34 FF aa 39 FF aa 39 FF aa 39 FF aa 39 + FF bb 39 FF bb 39 FF bb 39 FF bb 39 FF bb 39 FF bb 39 GG aa 49 + GG aa 49 GG aa 49 GG aa 49 GG bb 49 GG bb 49 GG bb 49 GG bb 49 + HH aa 57 HH aa 57 HH aa 57 HH bb 57 HH bb 57 HH bb 57 HH bb 57 + HH bb 57 HH bb 57 II aa 66 II aa 66 II bb 66 II bb 66 II bb 66 + II bb 66 II bb 66 JJ aa 73 JJ aa 73 JJ aa 73 JJ aa 73 JJ bb 73 + JJ bb 73 JJ bb 73 JJ bb 73} + +do_execsql_test 1.8.4 { + SELECT a, b, max(c) OVER (ORDER BY a,b GROUPS BETWEEN 3 PRECEDING AND 0 PRECEDING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 934 AA aa 934 AA aa 934 AA aa 934 AA bb 934 AA bb 934 + AA bb 934 AA bb 934 BB aa 934 BB aa 934 BB aa 934 BB aa 934 + BB aa 934 BB aa 934 BB bb 934 BB bb 934 BB bb 934 BB bb 934 + BB bb 934 BB bb 934 CC aa 870 CC aa 870 CC aa 870 CC aa 870 + CC bb 840 CC bb 840 DD aa 845 DD aa 845 DD aa 845 DD bb 959 + DD bb 959 DD bb 959 DD bb 959 EE aa 959 EE aa 959 EE bb 959 + EE bb 959 EE bb 959 FF aa 959 FF aa 959 FF aa 959 FF aa 959 + FF bb 938 FF bb 938 FF bb 938 FF bb 938 FF bb 938 FF bb 938 + GG aa 938 GG aa 938 GG aa 938 GG aa 938 GG bb 938 GG bb 938 + GG bb 938 GG bb 938 HH aa 979 HH aa 979 HH aa 979 HH bb 979 + HH bb 979 HH bb 979 HH bb 979 HH bb 979 HH bb 979 II aa 979 + II aa 979 II bb 979 II bb 979 II bb 979 II bb 979 II bb 979 + JJ aa 963 JJ aa 963 JJ aa 963 JJ aa 963 JJ bb 899 JJ bb 899 + JJ bb 899 JJ bb 899} + +do_execsql_test 1.8.5 { + SELECT a, b, min(c) OVER (ORDER BY a,b GROUPS BETWEEN 3 PRECEDING AND 0 PRECEDING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 223 AA aa 223 AA aa 223 AA aa 223 AA bb 223 AA bb 223 + AA bb 223 AA bb 223 BB aa 223 BB aa 223 BB aa 223 BB aa 223 + BB aa 223 BB aa 223 BB bb 223 BB bb 223 BB bb 223 BB bb 223 + BB bb 223 BB bb 223 CC aa 158 CC aa 158 CC aa 158 CC aa 158 + CC bb 158 CC bb 158 DD aa 158 DD aa 158 DD aa 158 DD bb 158 + DD bb 158 DD bb 158 DD bb 158 EE aa 113 EE aa 113 EE bb 113 + EE bb 113 EE bb 113 FF aa 113 FF aa 113 FF aa 113 FF aa 113 + FF bb 102 FF bb 102 FF bb 102 FF bb 102 FF bb 102 FF bb 102 + GG aa 102 GG aa 102 GG aa 102 GG aa 102 GG bb 102 GG bb 102 + GG bb 102 GG bb 102 HH aa 102 HH aa 102 HH aa 102 HH bb 133 + HH bb 133 HH bb 133 HH bb 133 HH bb 133 HH bb 133 II aa 133 + II aa 133 II bb 133 II bb 133 II bb 133 II bb 133 II bb 133 + JJ aa 113 JJ aa 113 JJ aa 113 JJ aa 113 JJ bb 113 JJ bb 113 + JJ bb 113 JJ bb 113} + +do_execsql_test 1.8.6 { + SELECT a, b, sum(c) OVER (ORDER BY a GROUPS BETWEEN 3 PRECEDING AND 0 PRECEDING EXCLUDE CURRENT ROW) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 3751 AA aa 3774 AA aa 4446 AA aa 4462 AA bb 3815 AA bb 4058 + AA bb 4113 AA bb 4376 BB aa 11263 BB aa 11365 BB aa 11613 + BB aa 11626 BB aa 11632 BB aa 11778 BB bb 11185 BB bb 11233 + BB bb 11239 BB bb 11314 BB bb 11320 BB bb 11392 CC aa 14388 + CC aa 14540 CC aa 14717 CC aa 14989 CC bb 14325 CC bb 14801 + DD aa 18334 DD aa 18923 DD aa 18955 DD bb 18220 DD bb 18385 + DD bb 18463 DD bb 18941 EE aa 16256 EE aa 16920 EE bb 16265 + EE bb 16404 EE bb 16781 FF aa 14691 FF aa 14694 FF aa 14743 + FF aa 15153 FF bb 14423 FF bb 14491 FF bb 14635 FF bb 14787 + FF bb 15066 FF bb 15259 GG aa 16073 GG aa 16227 GG aa 16372 + GG aa 16559 GG bb 15769 GG bb 15778 GG bb 15863 GG bb 16547 + HH aa 17139 HH aa 17328 HH aa 17638 HH bb 17155 HH bb 17388 + HH bb 17435 HH bb 17763 HH bb 17788 HH bb 17985 II aa 18567 + II aa 18821 II bb 18414 II bb 18548 II bb 18776 II bb 18798 + II bb 18969 JJ aa 16452 JJ aa 16583 JJ aa 17117 JJ aa 17238 + JJ bb 16512 JJ bb 16997 JJ bb 17015 JJ bb 17094} + +do_execsql_test 1.8.7 { + SELECT a, b, sum(c) OVER (ORDER BY a,b GROUPS BETWEEN 3 PRECEDING AND 0 PRECEDING EXCLUDE CURRENT ROW) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 1373 AA aa 1396 AA aa 2068 AA aa 2084 AA bb 3815 AA bb 4058 + AA bb 4113 AA bb 4376 BB aa 6796 BB aa 6898 BB aa 7146 BB aa 7159 + BB aa 7165 BB aa 7311 BB bb 11185 BB bb 11233 BB bb 11239 + BB bb 11314 BB bb 11320 BB bb 11392 CC aa 10913 CC aa 11065 + CC aa 11242 CC aa 11514 CC bb 9640 CC bb 10116 DD aa 8069 + DD aa 8658 DD aa 8690 DD bb 6195 DD bb 6360 DD bb 6438 DD bb 6916 + EE aa 5313 EE aa 5977 EE bb 5803 EE bb 5942 EE bb 6319 FF aa 6739 + FF aa 6742 FF aa 6791 FF aa 7201 FF bb 7269 FF bb 7337 FF bb 7481 + FF bb 7633 FF bb 7912 FF bb 8105 GG aa 8280 GG aa 8434 GG aa 8579 + GG aa 8766 GG bb 9198 GG bb 9207 GG bb 9292 GG bb 9976 HH aa 9243 + HH aa 9432 HH aa 9742 HH bb 8948 HH bb 9181 HH bb 9228 HH bb 9556 + HH bb 9581 HH bb 9778 II aa 8712 II aa 8966 II bb 8278 II bb 8412 + II bb 8640 II bb 8662 II bb 8833 JJ aa 7949 JJ aa 8080 JJ aa 8614 + JJ aa 8735 JJ bb 6601 JJ bb 7086 JJ bb 7104 JJ bb 7183} + +do_execsql_test 1.8.8 { + SELECT a, b, + sum(c) OVER (ORDER BY a GROUPS BETWEEN 3 PRECEDING AND 0 PRECEDING EXCLUDE CURRENT ROW), + sum(c) OVER (ORDER BY a GROUPS BETWEEN 3 PRECEDING AND 0 PRECEDING ), + sum(c) OVER (ORDER BY a,b GROUPS BETWEEN 3 PRECEDING AND 0 PRECEDING EXCLUDE CURRENT ROW), + sum(c) OVER (ORDER BY a,b GROUPS BETWEEN 3 PRECEDING AND 0 PRECEDING ) + FROM t3 ORDER BY 1, 2, 3; +} {AA aa 3751 4685 1373 2307 AA aa 3774 4685 1396 2307 + AA aa 4446 4685 2068 2307 AA aa 4462 4685 2084 2307 + AA bb 3815 4685 3815 4685 AA bb 4058 4685 4058 4685 + AA bb 4113 4685 4113 4685 AA bb 4376 4685 4376 4685 + BB aa 11263 12025 6796 7558 BB aa 11365 12025 6898 7558 + BB aa 11613 12025 7146 7558 BB aa 11626 12025 7159 7558 + BB aa 11632 12025 7165 7558 BB aa 11778 12025 7311 7558 + BB bb 11185 12025 11185 12025 BB bb 11233 12025 11233 12025 + BB bb 11239 12025 11239 12025 BB bb 11314 12025 11314 12025 + BB bb 11320 12025 11320 12025 BB bb 11392 12025 11392 12025 + CC aa 14388 15147 10913 11672 CC aa 14540 15147 11065 11672 + CC aa 14717 15147 11242 11672 CC aa 14989 15147 11514 11672 + CC bb 14325 15147 9640 10462 CC bb 14801 15147 10116 10462 + DD aa 18334 19179 8069 8914 DD aa 18923 19179 8658 8914 + DD aa 18955 19179 8690 8914 DD bb 18220 19179 6195 7154 + DD bb 18385 19179 6360 7154 DD bb 18463 19179 6438 7154 + DD bb 18941 19179 6916 7154 EE aa 16256 17033 5313 6090 + EE aa 16920 17033 5977 6090 EE bb 16265 17033 5803 6571 + EE bb 16404 17033 5942 6571 EE bb 16781 17033 6319 6571 + FF aa 14691 15361 6739 7409 FF aa 14694 15361 6742 7409 + FF aa 14743 15361 6791 7409 FF aa 15153 15361 7201 7409 + FF bb 14423 15361 7269 8207 FF bb 14491 15361 7337 8207 + FF bb 14635 15361 7481 8207 FF bb 14787 15361 7633 8207 + FF bb 15066 15361 7912 8207 FF bb 15259 15361 8105 8207 + GG aa 16073 16707 8280 8914 GG aa 16227 16707 8434 8914 + GG aa 16372 16707 8579 8914 GG aa 16559 16707 8766 8914 + GG bb 15769 16707 9198 10136 GG bb 15778 16707 9207 10136 + GG bb 15863 16707 9292 10136 GG bb 16547 16707 9976 10136 + HH aa 17139 18118 9243 10222 HH aa 17328 18118 9432 10222 + HH aa 17638 18118 9742 10222 HH bb 17155 18118 8948 9911 + HH bb 17388 18118 9181 9911 HH bb 17435 18118 9228 9911 + HH bb 17763 18118 9556 9911 HH bb 17788 18118 9581 9911 + HH bb 17985 18118 9778 9911 II aa 18567 19219 8712 9364 + II aa 18821 19219 8966 9364 II bb 18414 19219 8278 9083 + II bb 18548 19219 8412 9083 II bb 18776 19219 8640 9083 + II bb 18798 19219 8662 9083 II bb 18969 19219 8833 9083 + JJ aa 16452 17351 7949 8848 JJ aa 16583 17351 8080 8848 + JJ aa 17117 17351 8614 8848 JJ aa 17238 17351 8735 8848 + JJ bb 16512 17351 6601 7440 JJ bb 16997 17351 7086 7440 + JJ bb 17015 17351 7104 7440 JJ bb 17094 17351 7183 7440} + +do_execsql_test 1.9.1 { + SELECT a, b, sum(c) OVER (ORDER BY a GROUPS BETWEEN 2 PRECEDING AND CURRENT ROW ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 4685 AA aa 4685 AA aa 4685 AA aa 4685 AA bb 4685 AA bb 4685 + AA bb 4685 AA bb 4685 BB aa 12025 BB aa 12025 BB aa 12025 + BB aa 12025 BB aa 12025 BB aa 12025 BB bb 12025 BB bb 12025 + BB bb 12025 BB bb 12025 BB bb 12025 BB bb 12025 CC aa 15147 + CC aa 15147 CC aa 15147 CC aa 15147 CC bb 15147 CC bb 15147 + DD aa 14494 DD aa 14494 DD aa 14494 DD bb 14494 DD bb 14494 + DD bb 14494 DD bb 14494 EE aa 9693 EE aa 9693 EE bb 9693 + EE bb 9693 EE bb 9693 FF aa 12239 FF aa 12239 FF aa 12239 + FF aa 12239 FF bb 12239 FF bb 12239 FF bb 12239 FF bb 12239 + FF bb 12239 FF bb 12239 GG aa 12675 GG aa 12675 GG aa 12675 + GG aa 12675 GG bb 12675 GG bb 12675 GG bb 12675 GG bb 12675 + HH aa 15579 HH aa 15579 HH aa 15579 HH bb 15579 HH bb 15579 + HH bb 15579 HH bb 15579 HH bb 15579 HH bb 15579 II aa 13551 + II aa 13551 II bb 13551 II bb 13551 II bb 13551 II bb 13551 + II bb 13551 JJ aa 12883 JJ aa 12883 JJ aa 12883 JJ aa 12883 + JJ bb 12883 JJ bb 12883 JJ bb 12883 JJ bb 12883} + +do_execsql_test 1.9.2 { + SELECT a, b, sum(c) OVER (ORDER BY a,b GROUPS BETWEEN 2 PRECEDING AND CURRENT ROW ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 2307 AA aa 2307 AA aa 2307 AA aa 2307 AA bb 4685 AA bb 4685 + AA bb 4685 AA bb 4685 BB aa 7558 BB aa 7558 BB aa 7558 BB aa 7558 + BB aa 7558 BB aa 7558 BB bb 9718 BB bb 9718 BB bb 9718 BB bb 9718 + BB bb 9718 BB bb 9718 CC aa 9294 CC aa 9294 CC aa 9294 CC aa 9294 + CC bb 7589 CC bb 7589 DD aa 4447 DD aa 4447 DD aa 4447 DD bb 5200 + DD bb 5200 DD bb 5200 DD bb 5200 EE aa 4922 EE aa 4922 EE bb 5246 + EE bb 5246 EE bb 5246 FF aa 4702 FF aa 4702 FF aa 4702 FF aa 4702 + FF bb 7317 FF bb 7317 FF bb 7317 FF bb 7317 FF bb 7317 FF bb 7317 + GG aa 7265 GG aa 7265 GG aa 7265 GG aa 7265 GG bb 7973 GG bb 7973 + GG bb 7973 GG bb 7973 HH aa 6717 HH aa 6717 HH aa 6717 HH bb 8314 + HH bb 8314 HH bb 8314 HH bb 8314 HH bb 8314 HH bb 8314 II aa 6493 + II aa 6493 II bb 6834 II bb 6834 II bb 6834 II bb 6834 II bb 6834 + JJ aa 5654 JJ aa 5654 JJ aa 5654 JJ aa 5654 JJ bb 6390 JJ bb 6390 + JJ bb 6390 JJ bb 6390} + +do_execsql_test 1.9.3 { + SELECT a, b, rank() OVER (ORDER BY a GROUPS BETWEEN 2 PRECEDING AND CURRENT ROW ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 1 AA aa 1 AA aa 1 AA aa 1 AA bb 1 AA bb 1 AA bb 1 + AA bb 1 BB aa 9 BB aa 9 BB aa 9 BB aa 9 BB aa 9 BB aa 9 + BB bb 9 BB bb 9 BB bb 9 BB bb 9 BB bb 9 BB bb 9 CC aa 21 + CC aa 21 CC aa 21 CC aa 21 CC bb 21 CC bb 21 DD aa 27 DD aa 27 + DD aa 27 DD bb 27 DD bb 27 DD bb 27 DD bb 27 EE aa 34 EE aa 34 + EE bb 34 EE bb 34 EE bb 34 FF aa 39 FF aa 39 FF aa 39 FF aa 39 + FF bb 39 FF bb 39 FF bb 39 FF bb 39 FF bb 39 FF bb 39 GG aa 49 + GG aa 49 GG aa 49 GG aa 49 GG bb 49 GG bb 49 GG bb 49 GG bb 49 + HH aa 57 HH aa 57 HH aa 57 HH bb 57 HH bb 57 HH bb 57 HH bb 57 + HH bb 57 HH bb 57 II aa 66 II aa 66 II bb 66 II bb 66 II bb 66 + II bb 66 II bb 66 JJ aa 73 JJ aa 73 JJ aa 73 JJ aa 73 JJ bb 73 + JJ bb 73 JJ bb 73 JJ bb 73} + +do_execsql_test 1.9.4 { + SELECT a, b, max(c) OVER (ORDER BY a,b GROUPS BETWEEN 2 PRECEDING AND CURRENT ROW ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 934 AA aa 934 AA aa 934 AA aa 934 AA bb 934 AA bb 934 + AA bb 934 AA bb 934 BB aa 934 BB aa 934 BB aa 934 BB aa 934 + BB aa 934 BB aa 934 BB bb 870 BB bb 870 BB bb 870 BB bb 870 + BB bb 870 BB bb 870 CC aa 840 CC aa 840 CC aa 840 CC aa 840 + CC bb 840 CC bb 840 DD aa 845 DD aa 845 DD aa 845 DD bb 959 + DD bb 959 DD bb 959 DD bb 959 EE aa 959 EE aa 959 EE bb 959 + EE bb 959 EE bb 959 FF aa 777 FF aa 777 FF aa 777 FF aa 777 + FF bb 938 FF bb 938 FF bb 938 FF bb 938 FF bb 938 FF bb 938 + GG aa 938 GG aa 938 GG aa 938 GG aa 938 GG bb 938 GG bb 938 + GG bb 938 GG bb 938 HH aa 979 HH aa 979 HH aa 979 HH bb 979 + HH bb 979 HH bb 979 HH bb 979 HH bb 979 HH bb 979 II aa 979 + II aa 979 II bb 963 II bb 963 II bb 963 II bb 963 II bb 963 + JJ aa 899 JJ aa 899 JJ aa 899 JJ aa 899 JJ bb 899 JJ bb 899 + JJ bb 899 JJ bb 899} + +do_execsql_test 1.9.5 { + SELECT a, b, min(c) OVER (ORDER BY a,b GROUPS BETWEEN 2 PRECEDING AND CURRENT ROW ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 223 AA aa 223 AA aa 223 AA aa 223 AA bb 223 AA bb 223 + AA bb 223 AA bb 223 BB aa 223 BB aa 223 BB aa 223 BB aa 223 + BB aa 223 BB aa 223 BB bb 247 BB bb 247 BB bb 247 BB bb 247 + BB bb 247 BB bb 247 CC aa 158 CC aa 158 CC aa 158 CC aa 158 + CC bb 158 CC bb 158 DD aa 158 DD aa 158 DD aa 158 DD bb 224 + DD bb 224 DD bb 224 DD bb 224 EE aa 113 EE aa 113 EE bb 113 + EE bb 113 EE bb 113 FF aa 113 FF aa 113 FF aa 113 FF aa 113 + FF bb 102 FF bb 102 FF bb 102 FF bb 102 FF bb 102 FF bb 102 + GG aa 102 GG aa 102 GG aa 102 GG aa 102 GG bb 102 GG bb 102 + GG bb 102 GG bb 102 HH aa 148 HH aa 148 HH aa 148 HH bb 133 + HH bb 133 HH bb 133 HH bb 133 HH bb 133 HH bb 133 II aa 133 + II aa 133 II bb 133 II bb 133 II bb 133 II bb 133 II bb 133 + JJ aa 113 JJ aa 113 JJ aa 113 JJ aa 113 JJ bb 113 JJ bb 113 + JJ bb 113 JJ bb 113} + +do_execsql_test 1.9.6 { + SELECT a, b, sum(c) OVER (ORDER BY a GROUPS BETWEEN 2 PRECEDING AND CURRENT ROW EXCLUDE CURRENT ROW) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 3751 AA aa 3774 AA aa 4446 AA aa 4462 AA bb 3815 AA bb 4058 + AA bb 4113 AA bb 4376 BB aa 11263 BB aa 11365 BB aa 11613 + BB aa 11626 BB aa 11632 BB aa 11778 BB bb 11185 BB bb 11233 + BB bb 11239 BB bb 11314 BB bb 11320 BB bb 11392 CC aa 14388 + CC aa 14540 CC aa 14717 CC aa 14989 CC bb 14325 CC bb 14801 + DD aa 13649 DD aa 14238 DD aa 14270 DD bb 13535 DD bb 13700 + DD bb 13778 DD bb 14256 EE aa 8916 EE aa 9580 EE bb 8925 + EE bb 9064 EE bb 9441 FF aa 11569 FF aa 11572 FF aa 11621 + FF aa 12031 FF bb 11301 FF bb 11369 FF bb 11513 FF bb 11665 + FF bb 11944 FF bb 12137 GG aa 12041 GG aa 12195 GG aa 12340 + GG aa 12527 GG bb 11737 GG bb 11746 GG bb 11831 GG bb 12515 + HH aa 14600 HH aa 14789 HH aa 15099 HH bb 14616 HH bb 14849 + HH bb 14896 HH bb 15224 HH bb 15249 HH bb 15446 II aa 12899 + II aa 13153 II bb 12746 II bb 12880 II bb 13108 II bb 13130 + II bb 13301 JJ aa 11984 JJ aa 12115 JJ aa 12649 JJ aa 12770 + JJ bb 12044 JJ bb 12529 JJ bb 12547 JJ bb 12626} + +do_execsql_test 1.9.7 { + SELECT a, b, sum(c) OVER (ORDER BY a,b GROUPS BETWEEN 2 PRECEDING AND CURRENT ROW EXCLUDE CURRENT ROW) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 1373 AA aa 1396 AA aa 2068 AA aa 2084 AA bb 3815 AA bb 4058 + AA bb 4113 AA bb 4376 BB aa 6796 BB aa 6898 BB aa 7146 BB aa 7159 + BB aa 7165 BB aa 7311 BB bb 8878 BB bb 8926 BB bb 8932 BB bb 9007 + BB bb 9013 BB bb 9085 CC aa 8535 CC aa 8687 CC aa 8864 CC aa 9136 + CC bb 6767 CC bb 7243 DD aa 3602 DD aa 4191 DD aa 4223 DD bb 4241 + DD bb 4406 DD bb 4484 DD bb 4962 EE aa 4145 EE aa 4809 EE bb 4478 + EE bb 4617 EE bb 4994 FF aa 4032 FF aa 4035 FF aa 4084 FF aa 4494 + FF bb 6379 FF bb 6447 FF bb 6591 FF bb 6743 FF bb 7022 FF bb 7215 + GG aa 6631 GG aa 6785 GG aa 6930 GG aa 7117 GG bb 7035 GG bb 7044 + GG bb 7129 GG bb 7813 HH aa 5738 HH aa 5927 HH aa 6237 HH bb 7351 + HH bb 7584 HH bb 7631 HH bb 7959 HH bb 7984 HH bb 8181 II aa 5841 + II aa 6095 II bb 6029 II bb 6163 II bb 6391 II bb 6413 II bb 6584 + JJ aa 4755 JJ aa 4886 JJ aa 5420 JJ aa 5541 JJ bb 5551 JJ bb 6036 + JJ bb 6054 JJ bb 6133} + +do_execsql_test 1.9.8 { + SELECT a, b, + sum(c) OVER (ORDER BY a GROUPS BETWEEN 2 PRECEDING AND CURRENT ROW EXCLUDE CURRENT ROW), + sum(c) OVER (ORDER BY a GROUPS BETWEEN 2 PRECEDING AND CURRENT ROW ), + sum(c) OVER (ORDER BY a,b GROUPS BETWEEN 2 PRECEDING AND CURRENT ROW EXCLUDE CURRENT ROW), + sum(c) OVER (ORDER BY a,b GROUPS BETWEEN 2 PRECEDING AND CURRENT ROW ) + FROM t3 ORDER BY 1, 2, 3; +} {AA aa 3751 4685 1373 2307 AA aa 3774 4685 1396 2307 + AA aa 4446 4685 2068 2307 AA aa 4462 4685 2084 2307 + AA bb 3815 4685 3815 4685 AA bb 4058 4685 4058 4685 + AA bb 4113 4685 4113 4685 AA bb 4376 4685 4376 4685 + BB aa 11263 12025 6796 7558 BB aa 11365 12025 6898 7558 + BB aa 11613 12025 7146 7558 BB aa 11626 12025 7159 7558 + BB aa 11632 12025 7165 7558 BB aa 11778 12025 7311 7558 + BB bb 11185 12025 8878 9718 BB bb 11233 12025 8926 9718 + BB bb 11239 12025 8932 9718 BB bb 11314 12025 9007 9718 + BB bb 11320 12025 9013 9718 BB bb 11392 12025 9085 9718 + CC aa 14388 15147 8535 9294 CC aa 14540 15147 8687 9294 + CC aa 14717 15147 8864 9294 CC aa 14989 15147 9136 9294 + CC bb 14325 15147 6767 7589 CC bb 14801 15147 7243 7589 + DD aa 13649 14494 3602 4447 DD aa 14238 14494 4191 4447 + DD aa 14270 14494 4223 4447 DD bb 13535 14494 4241 5200 + DD bb 13700 14494 4406 5200 DD bb 13778 14494 4484 5200 + DD bb 14256 14494 4962 5200 EE aa 8916 9693 4145 4922 + EE aa 9580 9693 4809 4922 EE bb 8925 9693 4478 5246 + EE bb 9064 9693 4617 5246 EE bb 9441 9693 4994 5246 + FF aa 11569 12239 4032 4702 FF aa 11572 12239 4035 4702 + FF aa 11621 12239 4084 4702 FF aa 12031 12239 4494 4702 + FF bb 11301 12239 6379 7317 FF bb 11369 12239 6447 7317 + FF bb 11513 12239 6591 7317 FF bb 11665 12239 6743 7317 + FF bb 11944 12239 7022 7317 FF bb 12137 12239 7215 7317 + GG aa 12041 12675 6631 7265 GG aa 12195 12675 6785 7265 + GG aa 12340 12675 6930 7265 GG aa 12527 12675 7117 7265 + GG bb 11737 12675 7035 7973 GG bb 11746 12675 7044 7973 + GG bb 11831 12675 7129 7973 GG bb 12515 12675 7813 7973 + HH aa 14600 15579 5738 6717 HH aa 14789 15579 5927 6717 + HH aa 15099 15579 6237 6717 HH bb 14616 15579 7351 8314 + HH bb 14849 15579 7584 8314 HH bb 14896 15579 7631 8314 + HH bb 15224 15579 7959 8314 HH bb 15249 15579 7984 8314 + HH bb 15446 15579 8181 8314 II aa 12899 13551 5841 6493 + II aa 13153 13551 6095 6493 II bb 12746 13551 6029 6834 + II bb 12880 13551 6163 6834 II bb 13108 13551 6391 6834 + II bb 13130 13551 6413 6834 II bb 13301 13551 6584 6834 + JJ aa 11984 12883 4755 5654 JJ aa 12115 12883 4886 5654 + JJ aa 12649 12883 5420 5654 JJ aa 12770 12883 5541 5654 + JJ bb 12044 12883 5551 6390 JJ bb 12529 12883 6036 6390 + JJ bb 12547 12883 6054 6390 JJ bb 12626 12883 6133 6390} + +do_execsql_test 1.10.1 { + SELECT a, b, sum(c) OVER (ORDER BY a GROUPS BETWEEN 3 PRECEDING AND 0 FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 4685 AA aa 4685 AA aa 4685 AA aa 4685 AA bb 4685 AA bb 4685 + AA bb 4685 AA bb 4685 BB aa 12025 BB aa 12025 BB aa 12025 + BB aa 12025 BB aa 12025 BB aa 12025 BB bb 12025 BB bb 12025 + BB bb 12025 BB bb 12025 BB bb 12025 BB bb 12025 CC aa 15147 + CC aa 15147 CC aa 15147 CC aa 15147 CC bb 15147 CC bb 15147 + DD aa 19179 DD aa 19179 DD aa 19179 DD bb 19179 DD bb 19179 + DD bb 19179 DD bb 19179 EE aa 17033 EE aa 17033 EE bb 17033 + EE bb 17033 EE bb 17033 FF aa 15361 FF aa 15361 FF aa 15361 + FF aa 15361 FF bb 15361 FF bb 15361 FF bb 15361 FF bb 15361 + FF bb 15361 FF bb 15361 GG aa 16707 GG aa 16707 GG aa 16707 + GG aa 16707 GG bb 16707 GG bb 16707 GG bb 16707 GG bb 16707 + HH aa 18118 HH aa 18118 HH aa 18118 HH bb 18118 HH bb 18118 + HH bb 18118 HH bb 18118 HH bb 18118 HH bb 18118 II aa 19219 + II aa 19219 II bb 19219 II bb 19219 II bb 19219 II bb 19219 + II bb 19219 JJ aa 17351 JJ aa 17351 JJ aa 17351 JJ aa 17351 + JJ bb 17351 JJ bb 17351 JJ bb 17351 JJ bb 17351} + +do_execsql_test 1.10.2 { + SELECT a, b, sum(c) OVER (ORDER BY a,b GROUPS BETWEEN 3 PRECEDING AND 0 FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 2307 AA aa 2307 AA aa 2307 AA aa 2307 AA bb 4685 AA bb 4685 + AA bb 4685 AA bb 4685 BB aa 7558 BB aa 7558 BB aa 7558 BB aa 7558 + BB aa 7558 BB aa 7558 BB bb 12025 BB bb 12025 BB bb 12025 + BB bb 12025 BB bb 12025 BB bb 12025 CC aa 11672 CC aa 11672 + CC aa 11672 CC aa 11672 CC bb 10462 CC bb 10462 DD aa 8914 + DD aa 8914 DD aa 8914 DD bb 7154 DD bb 7154 DD bb 7154 DD bb 7154 + EE aa 6090 EE aa 6090 EE bb 6571 EE bb 6571 EE bb 6571 FF aa 7409 + FF aa 7409 FF aa 7409 FF aa 7409 FF bb 8207 FF bb 8207 FF bb 8207 + FF bb 8207 FF bb 8207 FF bb 8207 GG aa 8914 GG aa 8914 GG aa 8914 + GG aa 8914 GG bb 10136 GG bb 10136 GG bb 10136 GG bb 10136 + HH aa 10222 HH aa 10222 HH aa 10222 HH bb 9911 HH bb 9911 + HH bb 9911 HH bb 9911 HH bb 9911 HH bb 9911 II aa 9364 II aa 9364 + II bb 9083 II bb 9083 II bb 9083 II bb 9083 II bb 9083 JJ aa 8848 + JJ aa 8848 JJ aa 8848 JJ aa 8848 JJ bb 7440 JJ bb 7440 JJ bb 7440 + JJ bb 7440} + +do_execsql_test 1.10.3 { + SELECT a, b, rank() OVER (ORDER BY a GROUPS BETWEEN 3 PRECEDING AND 0 FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 1 AA aa 1 AA aa 1 AA aa 1 AA bb 1 AA bb 1 AA bb 1 + AA bb 1 BB aa 9 BB aa 9 BB aa 9 BB aa 9 BB aa 9 BB aa 9 + BB bb 9 BB bb 9 BB bb 9 BB bb 9 BB bb 9 BB bb 9 CC aa 21 + CC aa 21 CC aa 21 CC aa 21 CC bb 21 CC bb 21 DD aa 27 DD aa 27 + DD aa 27 DD bb 27 DD bb 27 DD bb 27 DD bb 27 EE aa 34 EE aa 34 + EE bb 34 EE bb 34 EE bb 34 FF aa 39 FF aa 39 FF aa 39 FF aa 39 + FF bb 39 FF bb 39 FF bb 39 FF bb 39 FF bb 39 FF bb 39 GG aa 49 + GG aa 49 GG aa 49 GG aa 49 GG bb 49 GG bb 49 GG bb 49 GG bb 49 + HH aa 57 HH aa 57 HH aa 57 HH bb 57 HH bb 57 HH bb 57 HH bb 57 + HH bb 57 HH bb 57 II aa 66 II aa 66 II bb 66 II bb 66 II bb 66 + II bb 66 II bb 66 JJ aa 73 JJ aa 73 JJ aa 73 JJ aa 73 JJ bb 73 + JJ bb 73 JJ bb 73 JJ bb 73} + +do_execsql_test 1.10.4 { + SELECT a, b, max(c) OVER (ORDER BY a,b GROUPS BETWEEN 3 PRECEDING AND 0 FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 934 AA aa 934 AA aa 934 AA aa 934 AA bb 934 AA bb 934 + AA bb 934 AA bb 934 BB aa 934 BB aa 934 BB aa 934 BB aa 934 + BB aa 934 BB aa 934 BB bb 934 BB bb 934 BB bb 934 BB bb 934 + BB bb 934 BB bb 934 CC aa 870 CC aa 870 CC aa 870 CC aa 870 + CC bb 840 CC bb 840 DD aa 845 DD aa 845 DD aa 845 DD bb 959 + DD bb 959 DD bb 959 DD bb 959 EE aa 959 EE aa 959 EE bb 959 + EE bb 959 EE bb 959 FF aa 959 FF aa 959 FF aa 959 FF aa 959 + FF bb 938 FF bb 938 FF bb 938 FF bb 938 FF bb 938 FF bb 938 + GG aa 938 GG aa 938 GG aa 938 GG aa 938 GG bb 938 GG bb 938 + GG bb 938 GG bb 938 HH aa 979 HH aa 979 HH aa 979 HH bb 979 + HH bb 979 HH bb 979 HH bb 979 HH bb 979 HH bb 979 II aa 979 + II aa 979 II bb 979 II bb 979 II bb 979 II bb 979 II bb 979 + JJ aa 963 JJ aa 963 JJ aa 963 JJ aa 963 JJ bb 899 JJ bb 899 + JJ bb 899 JJ bb 899} + +do_execsql_test 1.10.5 { + SELECT a, b, min(c) OVER (ORDER BY a,b GROUPS BETWEEN 3 PRECEDING AND 0 FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 223 AA aa 223 AA aa 223 AA aa 223 AA bb 223 AA bb 223 + AA bb 223 AA bb 223 BB aa 223 BB aa 223 BB aa 223 BB aa 223 + BB aa 223 BB aa 223 BB bb 223 BB bb 223 BB bb 223 BB bb 223 + BB bb 223 BB bb 223 CC aa 158 CC aa 158 CC aa 158 CC aa 158 + CC bb 158 CC bb 158 DD aa 158 DD aa 158 DD aa 158 DD bb 158 + DD bb 158 DD bb 158 DD bb 158 EE aa 113 EE aa 113 EE bb 113 + EE bb 113 EE bb 113 FF aa 113 FF aa 113 FF aa 113 FF aa 113 + FF bb 102 FF bb 102 FF bb 102 FF bb 102 FF bb 102 FF bb 102 + GG aa 102 GG aa 102 GG aa 102 GG aa 102 GG bb 102 GG bb 102 + GG bb 102 GG bb 102 HH aa 102 HH aa 102 HH aa 102 HH bb 133 + HH bb 133 HH bb 133 HH bb 133 HH bb 133 HH bb 133 II aa 133 + II aa 133 II bb 133 II bb 133 II bb 133 II bb 133 II bb 133 + JJ aa 113 JJ aa 113 JJ aa 113 JJ aa 113 JJ bb 113 JJ bb 113 + JJ bb 113 JJ bb 113} + +do_execsql_test 1.10.6 { + SELECT a, b, sum(c) OVER (ORDER BY a GROUPS BETWEEN 3 PRECEDING AND 0 FOLLOWING EXCLUDE CURRENT ROW) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 3751 AA aa 3774 AA aa 4446 AA aa 4462 AA bb 3815 AA bb 4058 + AA bb 4113 AA bb 4376 BB aa 11263 BB aa 11365 BB aa 11613 + BB aa 11626 BB aa 11632 BB aa 11778 BB bb 11185 BB bb 11233 + BB bb 11239 BB bb 11314 BB bb 11320 BB bb 11392 CC aa 14388 + CC aa 14540 CC aa 14717 CC aa 14989 CC bb 14325 CC bb 14801 + DD aa 18334 DD aa 18923 DD aa 18955 DD bb 18220 DD bb 18385 + DD bb 18463 DD bb 18941 EE aa 16256 EE aa 16920 EE bb 16265 + EE bb 16404 EE bb 16781 FF aa 14691 FF aa 14694 FF aa 14743 + FF aa 15153 FF bb 14423 FF bb 14491 FF bb 14635 FF bb 14787 + FF bb 15066 FF bb 15259 GG aa 16073 GG aa 16227 GG aa 16372 + GG aa 16559 GG bb 15769 GG bb 15778 GG bb 15863 GG bb 16547 + HH aa 17139 HH aa 17328 HH aa 17638 HH bb 17155 HH bb 17388 + HH bb 17435 HH bb 17763 HH bb 17788 HH bb 17985 II aa 18567 + II aa 18821 II bb 18414 II bb 18548 II bb 18776 II bb 18798 + II bb 18969 JJ aa 16452 JJ aa 16583 JJ aa 17117 JJ aa 17238 + JJ bb 16512 JJ bb 16997 JJ bb 17015 JJ bb 17094} + +do_execsql_test 1.10.7 { + SELECT a, b, sum(c) OVER (ORDER BY a,b GROUPS BETWEEN 3 PRECEDING AND 0 FOLLOWING EXCLUDE CURRENT ROW) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 1373 AA aa 1396 AA aa 2068 AA aa 2084 AA bb 3815 AA bb 4058 + AA bb 4113 AA bb 4376 BB aa 6796 BB aa 6898 BB aa 7146 BB aa 7159 + BB aa 7165 BB aa 7311 BB bb 11185 BB bb 11233 BB bb 11239 + BB bb 11314 BB bb 11320 BB bb 11392 CC aa 10913 CC aa 11065 + CC aa 11242 CC aa 11514 CC bb 9640 CC bb 10116 DD aa 8069 + DD aa 8658 DD aa 8690 DD bb 6195 DD bb 6360 DD bb 6438 DD bb 6916 + EE aa 5313 EE aa 5977 EE bb 5803 EE bb 5942 EE bb 6319 FF aa 6739 + FF aa 6742 FF aa 6791 FF aa 7201 FF bb 7269 FF bb 7337 FF bb 7481 + FF bb 7633 FF bb 7912 FF bb 8105 GG aa 8280 GG aa 8434 GG aa 8579 + GG aa 8766 GG bb 9198 GG bb 9207 GG bb 9292 GG bb 9976 HH aa 9243 + HH aa 9432 HH aa 9742 HH bb 8948 HH bb 9181 HH bb 9228 HH bb 9556 + HH bb 9581 HH bb 9778 II aa 8712 II aa 8966 II bb 8278 II bb 8412 + II bb 8640 II bb 8662 II bb 8833 JJ aa 7949 JJ aa 8080 JJ aa 8614 + JJ aa 8735 JJ bb 6601 JJ bb 7086 JJ bb 7104 JJ bb 7183} + +do_execsql_test 1.10.8 { + SELECT a, b, + sum(c) OVER (ORDER BY a GROUPS BETWEEN 3 PRECEDING AND 0 FOLLOWING EXCLUDE CURRENT ROW), + sum(c) OVER (ORDER BY a GROUPS BETWEEN 3 PRECEDING AND 0 FOLLOWING ), + sum(c) OVER (ORDER BY a,b GROUPS BETWEEN 3 PRECEDING AND 0 FOLLOWING EXCLUDE CURRENT ROW), + sum(c) OVER (ORDER BY a,b GROUPS BETWEEN 3 PRECEDING AND 0 FOLLOWING ) + FROM t3 ORDER BY 1, 2, 3; +} {AA aa 3751 4685 1373 2307 AA aa 3774 4685 1396 2307 + AA aa 4446 4685 2068 2307 AA aa 4462 4685 2084 2307 + AA bb 3815 4685 3815 4685 AA bb 4058 4685 4058 4685 + AA bb 4113 4685 4113 4685 AA bb 4376 4685 4376 4685 + BB aa 11263 12025 6796 7558 BB aa 11365 12025 6898 7558 + BB aa 11613 12025 7146 7558 BB aa 11626 12025 7159 7558 + BB aa 11632 12025 7165 7558 BB aa 11778 12025 7311 7558 + BB bb 11185 12025 11185 12025 BB bb 11233 12025 11233 12025 + BB bb 11239 12025 11239 12025 BB bb 11314 12025 11314 12025 + BB bb 11320 12025 11320 12025 BB bb 11392 12025 11392 12025 + CC aa 14388 15147 10913 11672 CC aa 14540 15147 11065 11672 + CC aa 14717 15147 11242 11672 CC aa 14989 15147 11514 11672 + CC bb 14325 15147 9640 10462 CC bb 14801 15147 10116 10462 + DD aa 18334 19179 8069 8914 DD aa 18923 19179 8658 8914 + DD aa 18955 19179 8690 8914 DD bb 18220 19179 6195 7154 + DD bb 18385 19179 6360 7154 DD bb 18463 19179 6438 7154 + DD bb 18941 19179 6916 7154 EE aa 16256 17033 5313 6090 + EE aa 16920 17033 5977 6090 EE bb 16265 17033 5803 6571 + EE bb 16404 17033 5942 6571 EE bb 16781 17033 6319 6571 + FF aa 14691 15361 6739 7409 FF aa 14694 15361 6742 7409 + FF aa 14743 15361 6791 7409 FF aa 15153 15361 7201 7409 + FF bb 14423 15361 7269 8207 FF bb 14491 15361 7337 8207 + FF bb 14635 15361 7481 8207 FF bb 14787 15361 7633 8207 + FF bb 15066 15361 7912 8207 FF bb 15259 15361 8105 8207 + GG aa 16073 16707 8280 8914 GG aa 16227 16707 8434 8914 + GG aa 16372 16707 8579 8914 GG aa 16559 16707 8766 8914 + GG bb 15769 16707 9198 10136 GG bb 15778 16707 9207 10136 + GG bb 15863 16707 9292 10136 GG bb 16547 16707 9976 10136 + HH aa 17139 18118 9243 10222 HH aa 17328 18118 9432 10222 + HH aa 17638 18118 9742 10222 HH bb 17155 18118 8948 9911 + HH bb 17388 18118 9181 9911 HH bb 17435 18118 9228 9911 + HH bb 17763 18118 9556 9911 HH bb 17788 18118 9581 9911 + HH bb 17985 18118 9778 9911 II aa 18567 19219 8712 9364 + II aa 18821 19219 8966 9364 II bb 18414 19219 8278 9083 + II bb 18548 19219 8412 9083 II bb 18776 19219 8640 9083 + II bb 18798 19219 8662 9083 II bb 18969 19219 8833 9083 + JJ aa 16452 17351 7949 8848 JJ aa 16583 17351 8080 8848 + JJ aa 17117 17351 8614 8848 JJ aa 17238 17351 8735 8848 + JJ bb 16512 17351 6601 7440 JJ bb 16997 17351 7086 7440 + JJ bb 17015 17351 7104 7440 JJ bb 17094 17351 7183 7440} + +do_execsql_test 1.11.1 { + SELECT a, b, sum(c) OVER (ORDER BY a GROUPS BETWEEN 2 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 44737 AA aa 44737 AA aa 44737 AA aa 44737 AA bb 44737 + AA bb 44737 AA bb 44737 AA bb 44737 BB aa 44737 BB aa 44737 + BB aa 44737 BB aa 44737 BB aa 44737 BB aa 44737 BB bb 44737 + BB bb 44737 BB bb 44737 BB bb 44737 BB bb 44737 BB bb 44737 + CC aa 44737 CC aa 44737 CC aa 44737 CC aa 44737 CC bb 44737 + CC bb 44737 DD aa 40052 DD aa 40052 DD aa 40052 DD bb 40052 + DD bb 40052 DD bb 40052 DD bb 40052 EE aa 32712 EE aa 32712 + EE bb 32712 EE bb 32712 EE bb 32712 FF aa 29590 FF aa 29590 + FF aa 29590 FF aa 29590 FF bb 29590 FF bb 29590 FF bb 29590 + FF bb 29590 FF bb 29590 FF bb 29590 GG aa 25558 GG aa 25558 + GG aa 25558 GG aa 25558 GG bb 25558 GG bb 25558 GG bb 25558 + GG bb 25558 HH aa 23019 HH aa 23019 HH aa 23019 HH bb 23019 + HH bb 23019 HH bb 23019 HH bb 23019 HH bb 23019 HH bb 23019 + II aa 17351 II aa 17351 II bb 17351 II bb 17351 II bb 17351 + II bb 17351 II bb 17351 JJ aa 12883 JJ aa 12883 JJ aa 12883 + JJ aa 12883 JJ bb 12883 JJ bb 12883 JJ bb 12883 JJ bb 12883} + +do_execsql_test 1.11.2 { + SELECT a, b, sum(c) OVER (ORDER BY a,b GROUPS BETWEEN 2 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 44737 AA aa 44737 AA aa 44737 AA aa 44737 AA bb 44737 + AA bb 44737 AA bb 44737 AA bb 44737 BB aa 44737 BB aa 44737 + BB aa 44737 BB aa 44737 BB aa 44737 BB aa 44737 BB bb 42430 + BB bb 42430 BB bb 42430 BB bb 42430 BB bb 42430 BB bb 42430 + CC aa 40052 CC aa 40052 CC aa 40052 CC aa 40052 CC bb 37179 + CC bb 37179 DD aa 32712 DD aa 32712 DD aa 32712 DD bb 30758 + DD bb 30758 DD bb 30758 DD bb 30758 EE aa 29590 EE aa 29590 + EE bb 28265 EE bb 28265 EE bb 28265 FF aa 25558 FF aa 25558 + FF aa 25558 FF aa 25558 FF bb 24668 FF bb 24668 FF bb 24668 + FF bb 24668 FF bb 24668 FF bb 24668 GG aa 23019 GG aa 23019 + GG aa 23019 GG aa 23019 GG bb 20856 GG bb 20856 GG bb 20856 + GG bb 20856 HH aa 17351 HH aa 17351 HH aa 17351 HH bb 15754 + HH bb 15754 HH bb 15754 HH bb 15754 HH bb 15754 HH bb 15754 + II aa 12883 II aa 12883 II bb 10634 II bb 10634 II bb 10634 + II bb 10634 II bb 10634 JJ aa 7440 JJ aa 7440 JJ aa 7440 + JJ aa 7440 JJ bb 6390 JJ bb 6390 JJ bb 6390 JJ bb 6390} + +do_execsql_test 1.11.3 { + SELECT a, b, rank() OVER (ORDER BY a GROUPS BETWEEN 2 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 1 AA aa 1 AA aa 1 AA aa 1 AA bb 1 AA bb 1 AA bb 1 + AA bb 1 BB aa 9 BB aa 9 BB aa 9 BB aa 9 BB aa 9 BB aa 9 + BB bb 9 BB bb 9 BB bb 9 BB bb 9 BB bb 9 BB bb 9 CC aa 21 + CC aa 21 CC aa 21 CC aa 21 CC bb 21 CC bb 21 DD aa 27 DD aa 27 + DD aa 27 DD bb 27 DD bb 27 DD bb 27 DD bb 27 EE aa 34 EE aa 34 + EE bb 34 EE bb 34 EE bb 34 FF aa 39 FF aa 39 FF aa 39 FF aa 39 + FF bb 39 FF bb 39 FF bb 39 FF bb 39 FF bb 39 FF bb 39 GG aa 49 + GG aa 49 GG aa 49 GG aa 49 GG bb 49 GG bb 49 GG bb 49 GG bb 49 + HH aa 57 HH aa 57 HH aa 57 HH bb 57 HH bb 57 HH bb 57 HH bb 57 + HH bb 57 HH bb 57 II aa 66 II aa 66 II bb 66 II bb 66 II bb 66 + II bb 66 II bb 66 JJ aa 73 JJ aa 73 JJ aa 73 JJ aa 73 JJ bb 73 + JJ bb 73 JJ bb 73 JJ bb 73} + +do_execsql_test 1.11.4 { + SELECT a, b, max(c) OVER (ORDER BY a,b GROUPS BETWEEN 2 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 979 AA aa 979 AA aa 979 AA aa 979 AA bb 979 AA bb 979 + AA bb 979 AA bb 979 BB aa 979 BB aa 979 BB aa 979 BB aa 979 + BB aa 979 BB aa 979 BB bb 979 BB bb 979 BB bb 979 BB bb 979 + BB bb 979 BB bb 979 CC aa 979 CC aa 979 CC aa 979 CC aa 979 + CC bb 979 CC bb 979 DD aa 979 DD aa 979 DD aa 979 DD bb 979 + DD bb 979 DD bb 979 DD bb 979 EE aa 979 EE aa 979 EE bb 979 + EE bb 979 EE bb 979 FF aa 979 FF aa 979 FF aa 979 FF aa 979 + FF bb 979 FF bb 979 FF bb 979 FF bb 979 FF bb 979 FF bb 979 + GG aa 979 GG aa 979 GG aa 979 GG aa 979 GG bb 979 GG bb 979 + GG bb 979 GG bb 979 HH aa 979 HH aa 979 HH aa 979 HH bb 979 + HH bb 979 HH bb 979 HH bb 979 HH bb 979 HH bb 979 II aa 979 + II aa 979 II bb 963 II bb 963 II bb 963 II bb 963 II bb 963 + JJ aa 899 JJ aa 899 JJ aa 899 JJ aa 899 JJ bb 899 JJ bb 899 + JJ bb 899 JJ bb 899} + +do_execsql_test 1.11.5 { + SELECT a, b, min(c) OVER (ORDER BY a,b GROUPS BETWEEN 2 PRECEDING AND UNBOUNDED FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 102 AA aa 102 AA aa 102 AA aa 102 AA bb 102 AA bb 102 + AA bb 102 AA bb 102 BB aa 102 BB aa 102 BB aa 102 BB aa 102 + BB aa 102 BB aa 102 BB bb 102 BB bb 102 BB bb 102 BB bb 102 + BB bb 102 BB bb 102 CC aa 102 CC aa 102 CC aa 102 CC aa 102 + CC bb 102 CC bb 102 DD aa 102 DD aa 102 DD aa 102 DD bb 102 + DD bb 102 DD bb 102 DD bb 102 EE aa 102 EE aa 102 EE bb 102 + EE bb 102 EE bb 102 FF aa 102 FF aa 102 FF aa 102 FF aa 102 + FF bb 102 FF bb 102 FF bb 102 FF bb 102 FF bb 102 FF bb 102 + GG aa 102 GG aa 102 GG aa 102 GG aa 102 GG bb 102 GG bb 102 + GG bb 102 GG bb 102 HH aa 113 HH aa 113 HH aa 113 HH bb 113 + HH bb 113 HH bb 113 HH bb 113 HH bb 113 HH bb 113 II aa 113 + II aa 113 II bb 113 II bb 113 II bb 113 II bb 113 II bb 113 + JJ aa 113 JJ aa 113 JJ aa 113 JJ aa 113 JJ bb 113 JJ bb 113 + JJ bb 113 JJ bb 113} + +do_execsql_test 1.11.6 { + SELECT a, b, sum(c) OVER (ORDER BY a GROUPS BETWEEN 2 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 43803 AA aa 43826 AA aa 44498 AA aa 44514 AA bb 43867 + AA bb 44110 AA bb 44165 AA bb 44428 BB aa 43975 BB aa 44077 + BB aa 44325 BB aa 44338 BB aa 44344 BB aa 44490 BB bb 43897 + BB bb 43945 BB bb 43951 BB bb 44026 BB bb 44032 BB bb 44104 + CC aa 43978 CC aa 44130 CC aa 44307 CC aa 44579 CC bb 43915 + CC bb 44391 DD aa 39207 DD aa 39796 DD aa 39828 DD bb 39093 + DD bb 39258 DD bb 39336 DD bb 39814 EE aa 31935 EE aa 32599 + EE bb 31944 EE bb 32083 EE bb 32460 FF aa 28920 FF aa 28923 + FF aa 28972 FF aa 29382 FF bb 28652 FF bb 28720 FF bb 28864 + FF bb 29016 FF bb 29295 FF bb 29488 GG aa 24924 GG aa 25078 + GG aa 25223 GG aa 25410 GG bb 24620 GG bb 24629 GG bb 24714 + GG bb 25398 HH aa 22040 HH aa 22229 HH aa 22539 HH bb 22056 + HH bb 22289 HH bb 22336 HH bb 22664 HH bb 22689 HH bb 22886 + II aa 16699 II aa 16953 II bb 16546 II bb 16680 II bb 16908 + II bb 16930 II bb 17101 JJ aa 11984 JJ aa 12115 JJ aa 12649 + JJ aa 12770 JJ bb 12044 JJ bb 12529 JJ bb 12547 JJ bb 12626} + +do_execsql_test 1.11.7 { + SELECT a, b, sum(c) OVER (ORDER BY a,b GROUPS BETWEEN 2 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 43803 AA aa 43826 AA aa 44498 AA aa 44514 AA bb 43867 + AA bb 44110 AA bb 44165 AA bb 44428 BB aa 43975 BB aa 44077 + BB aa 44325 BB aa 44338 BB aa 44344 BB aa 44490 BB bb 41590 + BB bb 41638 BB bb 41644 BB bb 41719 BB bb 41725 BB bb 41797 + CC aa 39293 CC aa 39445 CC aa 39622 CC aa 39894 CC bb 36357 + CC bb 36833 DD aa 31867 DD aa 32456 DD aa 32488 DD bb 29799 + DD bb 29964 DD bb 30042 DD bb 30520 EE aa 28813 EE aa 29477 + EE bb 27497 EE bb 27636 EE bb 28013 FF aa 24888 FF aa 24891 + FF aa 24940 FF aa 25350 FF bb 23730 FF bb 23798 FF bb 23942 + FF bb 24094 FF bb 24373 FF bb 24566 GG aa 22385 GG aa 22539 + GG aa 22684 GG aa 22871 GG bb 19918 GG bb 19927 GG bb 20012 + GG bb 20696 HH aa 16372 HH aa 16561 HH aa 16871 HH bb 14791 + HH bb 15024 HH bb 15071 HH bb 15399 HH bb 15424 HH bb 15621 + II aa 12231 II aa 12485 II bb 9829 II bb 9963 II bb 10191 + II bb 10213 II bb 10384 JJ aa 6541 JJ aa 6672 JJ aa 7206 + JJ aa 7327 JJ bb 5551 JJ bb 6036 JJ bb 6054 JJ bb 6133} + +do_execsql_test 1.11.8 { + SELECT a, b, + sum(c) OVER (ORDER BY a GROUPS BETWEEN 2 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW), + sum(c) OVER (ORDER BY a GROUPS BETWEEN 2 PRECEDING AND UNBOUNDED FOLLOWING ), + sum(c) OVER (ORDER BY a,b GROUPS BETWEEN 2 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW), + sum(c) OVER (ORDER BY a,b GROUPS BETWEEN 2 PRECEDING AND UNBOUNDED FOLLOWING ) + FROM t3 ORDER BY 1, 2, 3; +} {AA aa 43803 44737 43803 44737 AA aa 43826 44737 43826 44737 + AA aa 44498 44737 44498 44737 AA aa 44514 44737 44514 44737 + AA bb 43867 44737 43867 44737 AA bb 44110 44737 44110 44737 + AA bb 44165 44737 44165 44737 AA bb 44428 44737 44428 44737 + BB aa 43975 44737 43975 44737 BB aa 44077 44737 44077 44737 + BB aa 44325 44737 44325 44737 BB aa 44338 44737 44338 44737 + BB aa 44344 44737 44344 44737 BB aa 44490 44737 44490 44737 + BB bb 43897 44737 41590 42430 BB bb 43945 44737 41638 42430 + BB bb 43951 44737 41644 42430 BB bb 44026 44737 41719 42430 + BB bb 44032 44737 41725 42430 BB bb 44104 44737 41797 42430 + CC aa 43978 44737 39293 40052 CC aa 44130 44737 39445 40052 + CC aa 44307 44737 39622 40052 CC aa 44579 44737 39894 40052 + CC bb 43915 44737 36357 37179 CC bb 44391 44737 36833 37179 + DD aa 39207 40052 31867 32712 DD aa 39796 40052 32456 32712 + DD aa 39828 40052 32488 32712 DD bb 39093 40052 29799 30758 + DD bb 39258 40052 29964 30758 DD bb 39336 40052 30042 30758 + DD bb 39814 40052 30520 30758 EE aa 31935 32712 28813 29590 + EE aa 32599 32712 29477 29590 EE bb 31944 32712 27497 28265 + EE bb 32083 32712 27636 28265 EE bb 32460 32712 28013 28265 + FF aa 28920 29590 24888 25558 FF aa 28923 29590 24891 25558 + FF aa 28972 29590 24940 25558 FF aa 29382 29590 25350 25558 + FF bb 28652 29590 23730 24668 FF bb 28720 29590 23798 24668 + FF bb 28864 29590 23942 24668 FF bb 29016 29590 24094 24668 + FF bb 29295 29590 24373 24668 FF bb 29488 29590 24566 24668 + GG aa 24924 25558 22385 23019 GG aa 25078 25558 22539 23019 + GG aa 25223 25558 22684 23019 GG aa 25410 25558 22871 23019 + GG bb 24620 25558 19918 20856 GG bb 24629 25558 19927 20856 + GG bb 24714 25558 20012 20856 GG bb 25398 25558 20696 20856 + HH aa 22040 23019 16372 17351 HH aa 22229 23019 16561 17351 + HH aa 22539 23019 16871 17351 HH bb 22056 23019 14791 15754 + HH bb 22289 23019 15024 15754 HH bb 22336 23019 15071 15754 + HH bb 22664 23019 15399 15754 HH bb 22689 23019 15424 15754 + HH bb 22886 23019 15621 15754 II aa 16699 17351 12231 12883 + II aa 16953 17351 12485 12883 II bb 16546 17351 9829 10634 + II bb 16680 17351 9963 10634 II bb 16908 17351 10191 10634 + II bb 16930 17351 10213 10634 II bb 17101 17351 10384 10634 + JJ aa 11984 12883 6541 7440 JJ aa 12115 12883 6672 7440 + JJ aa 12649 12883 7206 7440 JJ aa 12770 12883 7327 7440 + JJ bb 12044 12883 5551 6390 JJ bb 12529 12883 6036 6390 + JJ bb 12547 12883 6054 6390 JJ bb 12626 12883 6133 6390} + +do_execsql_test 1.12.1 { + SELECT a, b, sum(c) OVER (ORDER BY a GROUPS BETWEEN CURRENT ROW AND 0 FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 4685 AA aa 4685 AA aa 4685 AA aa 4685 AA bb 4685 AA bb 4685 + AA bb 4685 AA bb 4685 BB aa 7340 BB aa 7340 BB aa 7340 BB aa 7340 + BB aa 7340 BB aa 7340 BB bb 7340 BB bb 7340 BB bb 7340 BB bb 7340 + BB bb 7340 BB bb 7340 CC aa 3122 CC aa 3122 CC aa 3122 CC aa 3122 + CC bb 3122 CC bb 3122 DD aa 4032 DD aa 4032 DD aa 4032 DD bb 4032 + DD bb 4032 DD bb 4032 DD bb 4032 EE aa 2539 EE aa 2539 EE bb 2539 + EE bb 2539 EE bb 2539 FF aa 5668 FF aa 5668 FF aa 5668 FF aa 5668 + FF bb 5668 FF bb 5668 FF bb 5668 FF bb 5668 FF bb 5668 FF bb 5668 + GG aa 4468 GG aa 4468 GG aa 4468 GG aa 4468 GG bb 4468 GG bb 4468 + GG bb 4468 GG bb 4468 HH aa 5443 HH aa 5443 HH aa 5443 HH bb 5443 + HH bb 5443 HH bb 5443 HH bb 5443 HH bb 5443 HH bb 5443 II aa 3640 + II aa 3640 II bb 3640 II bb 3640 II bb 3640 II bb 3640 II bb 3640 + JJ aa 3800 JJ aa 3800 JJ aa 3800 JJ aa 3800 JJ bb 3800 JJ bb 3800 + JJ bb 3800 JJ bb 3800} + +do_execsql_test 1.12.2 { + SELECT a, b, sum(c) OVER (ORDER BY a,b GROUPS BETWEEN CURRENT ROW AND 0 FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 2307 AA aa 2307 AA aa 2307 AA aa 2307 AA bb 2378 AA bb 2378 + AA bb 2378 AA bb 2378 BB aa 2873 BB aa 2873 BB aa 2873 BB aa 2873 + BB aa 2873 BB aa 2873 BB bb 4467 BB bb 4467 BB bb 4467 BB bb 4467 + BB bb 4467 BB bb 4467 CC aa 1954 CC aa 1954 CC aa 1954 CC aa 1954 + CC bb 1168 CC bb 1168 DD aa 1325 DD aa 1325 DD aa 1325 DD bb 2707 + DD bb 2707 DD bb 2707 DD bb 2707 EE aa 890 EE aa 890 EE bb 1649 + EE bb 1649 EE bb 1649 FF aa 2163 FF aa 2163 FF aa 2163 FF aa 2163 + FF bb 3505 FF bb 3505 FF bb 3505 FF bb 3505 FF bb 3505 FF bb 3505 + GG aa 1597 GG aa 1597 GG aa 1597 GG aa 1597 GG bb 2871 GG bb 2871 + GG bb 2871 GG bb 2871 HH aa 2249 HH aa 2249 HH aa 2249 HH bb 3194 + HH bb 3194 HH bb 3194 HH bb 3194 HH bb 3194 HH bb 3194 II aa 1050 + II aa 1050 II bb 2590 II bb 2590 II bb 2590 II bb 2590 II bb 2590 + JJ aa 2014 JJ aa 2014 JJ aa 2014 JJ aa 2014 JJ bb 1786 JJ bb 1786 + JJ bb 1786 JJ bb 1786} + +do_execsql_test 1.12.3 { + SELECT a, b, rank() OVER (ORDER BY a GROUPS BETWEEN CURRENT ROW AND 0 FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 1 AA aa 1 AA aa 1 AA aa 1 AA bb 1 AA bb 1 AA bb 1 + AA bb 1 BB aa 9 BB aa 9 BB aa 9 BB aa 9 BB aa 9 BB aa 9 + BB bb 9 BB bb 9 BB bb 9 BB bb 9 BB bb 9 BB bb 9 CC aa 21 + CC aa 21 CC aa 21 CC aa 21 CC bb 21 CC bb 21 DD aa 27 DD aa 27 + DD aa 27 DD bb 27 DD bb 27 DD bb 27 DD bb 27 EE aa 34 EE aa 34 + EE bb 34 EE bb 34 EE bb 34 FF aa 39 FF aa 39 FF aa 39 FF aa 39 + FF bb 39 FF bb 39 FF bb 39 FF bb 39 FF bb 39 FF bb 39 GG aa 49 + GG aa 49 GG aa 49 GG aa 49 GG bb 49 GG bb 49 GG bb 49 GG bb 49 + HH aa 57 HH aa 57 HH aa 57 HH bb 57 HH bb 57 HH bb 57 HH bb 57 + HH bb 57 HH bb 57 II aa 66 II aa 66 II bb 66 II bb 66 II bb 66 + II bb 66 II bb 66 JJ aa 73 JJ aa 73 JJ aa 73 JJ aa 73 JJ bb 73 + JJ bb 73 JJ bb 73 JJ bb 73} + +do_execsql_test 1.12.4 { + SELECT a, b, max(c) OVER (ORDER BY a,b GROUPS BETWEEN CURRENT ROW AND 0 FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 934 AA aa 934 AA aa 934 AA aa 934 AA bb 870 AA bb 870 + AA bb 870 AA bb 870 BB aa 762 BB aa 762 BB aa 762 BB aa 762 + BB aa 762 BB aa 762 BB bb 840 BB bb 840 BB bb 840 BB bb 840 + BB bb 840 BB bb 840 CC aa 759 CC aa 759 CC aa 759 CC aa 759 + CC bb 822 CC bb 822 DD aa 845 DD aa 845 DD aa 845 DD bb 959 + DD bb 959 DD bb 959 DD bb 959 EE aa 777 EE aa 777 EE bb 768 + EE bb 768 EE bb 768 FF aa 670 FF aa 670 FF aa 670 FF aa 670 + FF bb 938 FF bb 938 FF bb 938 FF bb 938 FF bb 938 FF bb 938 + GG aa 634 GG aa 634 GG aa 634 GG aa 634 GG bb 938 GG bb 938 + GG bb 938 GG bb 938 HH aa 979 HH aa 979 HH aa 979 HH bb 963 + HH bb 963 HH bb 963 HH bb 963 HH bb 963 HH bb 963 II aa 652 + II aa 652 II bb 805 II bb 805 II bb 805 II bb 805 II bb 805 + JJ aa 899 JJ aa 899 JJ aa 899 JJ aa 899 JJ bb 839 JJ bb 839 + JJ bb 839 JJ bb 839} + +do_execsql_test 1.12.5 { + SELECT a, b, min(c) OVER (ORDER BY a,b GROUPS BETWEEN CURRENT ROW AND 0 FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 223 AA aa 223 AA aa 223 AA aa 223 AA bb 309 AA bb 309 + AA bb 309 AA bb 309 BB aa 247 BB aa 247 BB aa 247 BB aa 247 + BB aa 247 BB aa 247 BB bb 633 BB bb 633 BB bb 633 BB bb 633 + BB bb 633 BB bb 633 CC aa 158 CC aa 158 CC aa 158 CC aa 158 + CC bb 346 CC bb 346 DD aa 224 DD aa 224 DD aa 224 DD bb 238 + DD bb 238 DD bb 238 DD bb 238 EE aa 113 EE aa 113 EE bb 252 + EE bb 252 EE bb 252 FF aa 208 FF aa 208 FF aa 208 FF aa 208 + FF bb 102 FF bb 102 FF bb 102 FF bb 102 FF bb 102 FF bb 102 + GG aa 148 GG aa 148 GG aa 148 GG aa 148 GG bb 160 GG bb 160 + GG bb 160 GG bb 160 HH aa 480 HH aa 480 HH aa 480 HH bb 133 + HH bb 133 HH bb 133 HH bb 133 HH bb 133 HH bb 133 II aa 398 + II aa 398 II bb 250 II bb 250 II bb 250 II bb 250 II bb 250 + JJ aa 113 JJ aa 113 JJ aa 113 JJ aa 113 JJ bb 257 JJ bb 257 + JJ bb 257 JJ bb 257} + +do_execsql_test 1.12.6 { + SELECT a, b, sum(c) OVER (ORDER BY a GROUPS BETWEEN CURRENT ROW AND 0 FOLLOWING EXCLUDE CURRENT ROW) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 3751 AA aa 3774 AA aa 4446 AA aa 4462 AA bb 3815 AA bb 4058 + AA bb 4113 AA bb 4376 BB aa 6578 BB aa 6680 BB aa 6928 BB aa 6941 + BB aa 6947 BB aa 7093 BB bb 6500 BB bb 6548 BB bb 6554 BB bb 6629 + BB bb 6635 BB bb 6707 CC aa 2363 CC aa 2515 CC aa 2692 CC aa 2964 + CC bb 2300 CC bb 2776 DD aa 3187 DD aa 3776 DD aa 3808 DD bb 3073 + DD bb 3238 DD bb 3316 DD bb 3794 EE aa 1762 EE aa 2426 EE bb 1771 + EE bb 1910 EE bb 2287 FF aa 4998 FF aa 5001 FF aa 5050 FF aa 5460 + FF bb 4730 FF bb 4798 FF bb 4942 FF bb 5094 FF bb 5373 FF bb 5566 + GG aa 3834 GG aa 3988 GG aa 4133 GG aa 4320 GG bb 3530 GG bb 3539 + GG bb 3624 GG bb 4308 HH aa 4464 HH aa 4653 HH aa 4963 HH bb 4480 + HH bb 4713 HH bb 4760 HH bb 5088 HH bb 5113 HH bb 5310 II aa 2988 + II aa 3242 II bb 2835 II bb 2969 II bb 3197 II bb 3219 II bb 3390 + JJ aa 2901 JJ aa 3032 JJ aa 3566 JJ aa 3687 JJ bb 2961 JJ bb 3446 + JJ bb 3464 JJ bb 3543} + +do_execsql_test 1.12.7 { + SELECT a, b, sum(c) OVER (ORDER BY a,b GROUPS BETWEEN CURRENT ROW AND 0 FOLLOWING EXCLUDE CURRENT ROW) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 1373 AA aa 1396 AA aa 2068 AA aa 2084 AA bb 1508 AA bb 1751 + AA bb 1806 AA bb 2069 BB aa 2111 BB aa 2213 BB aa 2461 BB aa 2474 + BB aa 2480 BB aa 2626 BB bb 3627 BB bb 3675 BB bb 3681 BB bb 3756 + BB bb 3762 BB bb 3834 CC aa 1195 CC aa 1347 CC aa 1524 CC aa 1796 + CC bb 346 CC bb 822 DD aa 480 DD aa 1069 DD aa 1101 DD bb 1748 + DD bb 1913 DD bb 1991 DD bb 2469 EE aa 113 EE aa 777 EE bb 881 + EE bb 1020 EE bb 1397 FF aa 1493 FF aa 1496 FF aa 1545 FF aa 1955 + FF bb 2567 FF bb 2635 FF bb 2779 FF bb 2931 FF bb 3210 FF bb 3403 + GG aa 963 GG aa 1117 GG aa 1262 GG aa 1449 GG bb 1933 GG bb 1942 + GG bb 2027 GG bb 2711 HH aa 1270 HH aa 1459 HH aa 1769 HH bb 2231 + HH bb 2464 HH bb 2511 HH bb 2839 HH bb 2864 HH bb 3061 II aa 398 + II aa 652 II bb 1785 II bb 1919 II bb 2147 II bb 2169 II bb 2340 + JJ aa 1115 JJ aa 1246 JJ aa 1780 JJ aa 1901 JJ bb 947 JJ bb 1432 + JJ bb 1450 JJ bb 1529} + +do_execsql_test 1.12.8 { + SELECT a, b, + sum(c) OVER (ORDER BY a GROUPS BETWEEN CURRENT ROW AND 0 FOLLOWING EXCLUDE CURRENT ROW), + sum(c) OVER (ORDER BY a GROUPS BETWEEN CURRENT ROW AND 0 FOLLOWING ), + sum(c) OVER (ORDER BY a,b GROUPS BETWEEN CURRENT ROW AND 0 FOLLOWING EXCLUDE CURRENT ROW), + sum(c) OVER (ORDER BY a,b GROUPS BETWEEN CURRENT ROW AND 0 FOLLOWING ) + FROM t3 ORDER BY 1, 2, 3; +} {AA aa 3751 4685 1373 2307 AA aa 3774 4685 1396 2307 + AA aa 4446 4685 2068 2307 AA aa 4462 4685 2084 2307 + AA bb 3815 4685 1508 2378 AA bb 4058 4685 1751 2378 + AA bb 4113 4685 1806 2378 AA bb 4376 4685 2069 2378 + BB aa 6578 7340 2111 2873 BB aa 6680 7340 2213 2873 + BB aa 6928 7340 2461 2873 BB aa 6941 7340 2474 2873 + BB aa 6947 7340 2480 2873 BB aa 7093 7340 2626 2873 + BB bb 6500 7340 3627 4467 BB bb 6548 7340 3675 4467 + BB bb 6554 7340 3681 4467 BB bb 6629 7340 3756 4467 + BB bb 6635 7340 3762 4467 BB bb 6707 7340 3834 4467 + CC aa 2363 3122 1195 1954 CC aa 2515 3122 1347 1954 + CC aa 2692 3122 1524 1954 CC aa 2964 3122 1796 1954 + CC bb 2300 3122 346 1168 CC bb 2776 3122 822 1168 + DD aa 3187 4032 480 1325 DD aa 3776 4032 1069 1325 + DD aa 3808 4032 1101 1325 DD bb 3073 4032 1748 2707 + DD bb 3238 4032 1913 2707 DD bb 3316 4032 1991 2707 + DD bb 3794 4032 2469 2707 EE aa 1762 2539 113 890 + EE aa 2426 2539 777 890 EE bb 1771 2539 881 1649 + EE bb 1910 2539 1020 1649 EE bb 2287 2539 1397 1649 + FF aa 4998 5668 1493 2163 FF aa 5001 5668 1496 2163 + FF aa 5050 5668 1545 2163 FF aa 5460 5668 1955 2163 + FF bb 4730 5668 2567 3505 FF bb 4798 5668 2635 3505 + FF bb 4942 5668 2779 3505 FF bb 5094 5668 2931 3505 + FF bb 5373 5668 3210 3505 FF bb 5566 5668 3403 3505 + GG aa 3834 4468 963 1597 GG aa 3988 4468 1117 1597 + GG aa 4133 4468 1262 1597 GG aa 4320 4468 1449 1597 + GG bb 3530 4468 1933 2871 GG bb 3539 4468 1942 2871 + GG bb 3624 4468 2027 2871 GG bb 4308 4468 2711 2871 + HH aa 4464 5443 1270 2249 HH aa 4653 5443 1459 2249 + HH aa 4963 5443 1769 2249 HH bb 4480 5443 2231 3194 + HH bb 4713 5443 2464 3194 HH bb 4760 5443 2511 3194 + HH bb 5088 5443 2839 3194 HH bb 5113 5443 2864 3194 + HH bb 5310 5443 3061 3194 II aa 2988 3640 398 1050 + II aa 3242 3640 652 1050 II bb 2835 3640 1785 2590 + II bb 2969 3640 1919 2590 II bb 3197 3640 2147 2590 + II bb 3219 3640 2169 2590 II bb 3390 3640 2340 2590 + JJ aa 2901 3800 1115 2014 JJ aa 3032 3800 1246 2014 + JJ aa 3566 3800 1780 2014 JJ aa 3687 3800 1901 2014 + JJ bb 2961 3800 947 1786 JJ bb 3446 3800 1432 1786 + JJ bb 3464 3800 1450 1786 JJ bb 3543 3800 1529 1786} + +do_execsql_test 1.13.1 { + SELECT a, b, sum(c) OVER (ORDER BY a GROUPS BETWEEN CURRENT ROW AND 1 FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 12025 AA aa 12025 AA aa 12025 AA aa 12025 AA bb 12025 + AA bb 12025 AA bb 12025 AA bb 12025 BB aa 10462 BB aa 10462 + BB aa 10462 BB aa 10462 BB aa 10462 BB aa 10462 BB bb 10462 + BB bb 10462 BB bb 10462 BB bb 10462 BB bb 10462 BB bb 10462 + CC aa 7154 CC aa 7154 CC aa 7154 CC aa 7154 CC bb 7154 CC bb 7154 + DD aa 6571 DD aa 6571 DD aa 6571 DD bb 6571 DD bb 6571 DD bb 6571 + DD bb 6571 EE aa 8207 EE aa 8207 EE bb 8207 EE bb 8207 EE bb 8207 + FF aa 10136 FF aa 10136 FF aa 10136 FF aa 10136 FF bb 10136 + FF bb 10136 FF bb 10136 FF bb 10136 FF bb 10136 FF bb 10136 + GG aa 9911 GG aa 9911 GG aa 9911 GG aa 9911 GG bb 9911 GG bb 9911 + GG bb 9911 GG bb 9911 HH aa 9083 HH aa 9083 HH aa 9083 HH bb 9083 + HH bb 9083 HH bb 9083 HH bb 9083 HH bb 9083 HH bb 9083 II aa 7440 + II aa 7440 II bb 7440 II bb 7440 II bb 7440 II bb 7440 II bb 7440 + JJ aa 3800 JJ aa 3800 JJ aa 3800 JJ aa 3800 JJ bb 3800 JJ bb 3800 + JJ bb 3800 JJ bb 3800} + +do_execsql_test 1.13.2 { + SELECT a, b, sum(c) OVER (ORDER BY a,b GROUPS BETWEEN CURRENT ROW AND 1 FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 4685 AA aa 4685 AA aa 4685 AA aa 4685 AA bb 5251 AA bb 5251 + AA bb 5251 AA bb 5251 BB aa 7340 BB aa 7340 BB aa 7340 BB aa 7340 + BB aa 7340 BB aa 7340 BB bb 6421 BB bb 6421 BB bb 6421 BB bb 6421 + BB bb 6421 BB bb 6421 CC aa 3122 CC aa 3122 CC aa 3122 CC aa 3122 + CC bb 2493 CC bb 2493 DD aa 4032 DD aa 4032 DD aa 4032 DD bb 3597 + DD bb 3597 DD bb 3597 DD bb 3597 EE aa 2539 EE aa 2539 EE bb 3812 + EE bb 3812 EE bb 3812 FF aa 5668 FF aa 5668 FF aa 5668 FF aa 5668 + FF bb 5102 FF bb 5102 FF bb 5102 FF bb 5102 FF bb 5102 FF bb 5102 + GG aa 4468 GG aa 4468 GG aa 4468 GG aa 4468 GG bb 5120 GG bb 5120 + GG bb 5120 GG bb 5120 HH aa 5443 HH aa 5443 HH aa 5443 HH bb 4244 + HH bb 4244 HH bb 4244 HH bb 4244 HH bb 4244 HH bb 4244 II aa 3640 + II aa 3640 II bb 4604 II bb 4604 II bb 4604 II bb 4604 II bb 4604 + JJ aa 3800 JJ aa 3800 JJ aa 3800 JJ aa 3800 JJ bb 1786 JJ bb 1786 + JJ bb 1786 JJ bb 1786} + +do_execsql_test 1.13.3 { + SELECT a, b, rank() OVER (ORDER BY a GROUPS BETWEEN CURRENT ROW AND 1 FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 1 AA aa 1 AA aa 1 AA aa 1 AA bb 1 AA bb 1 AA bb 1 + AA bb 1 BB aa 9 BB aa 9 BB aa 9 BB aa 9 BB aa 9 BB aa 9 + BB bb 9 BB bb 9 BB bb 9 BB bb 9 BB bb 9 BB bb 9 CC aa 21 + CC aa 21 CC aa 21 CC aa 21 CC bb 21 CC bb 21 DD aa 27 DD aa 27 + DD aa 27 DD bb 27 DD bb 27 DD bb 27 DD bb 27 EE aa 34 EE aa 34 + EE bb 34 EE bb 34 EE bb 34 FF aa 39 FF aa 39 FF aa 39 FF aa 39 + FF bb 39 FF bb 39 FF bb 39 FF bb 39 FF bb 39 FF bb 39 GG aa 49 + GG aa 49 GG aa 49 GG aa 49 GG bb 49 GG bb 49 GG bb 49 GG bb 49 + HH aa 57 HH aa 57 HH aa 57 HH bb 57 HH bb 57 HH bb 57 HH bb 57 + HH bb 57 HH bb 57 II aa 66 II aa 66 II bb 66 II bb 66 II bb 66 + II bb 66 II bb 66 JJ aa 73 JJ aa 73 JJ aa 73 JJ aa 73 JJ bb 73 + JJ bb 73 JJ bb 73 JJ bb 73} + +do_execsql_test 1.13.4 { + SELECT a, b, max(c) OVER (ORDER BY a,b GROUPS BETWEEN CURRENT ROW AND 1 FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 934 AA aa 934 AA aa 934 AA aa 934 AA bb 870 AA bb 870 + AA bb 870 AA bb 870 BB aa 840 BB aa 840 BB aa 840 BB aa 840 + BB aa 840 BB aa 840 BB bb 840 BB bb 840 BB bb 840 BB bb 840 + BB bb 840 BB bb 840 CC aa 822 CC aa 822 CC aa 822 CC aa 822 + CC bb 845 CC bb 845 DD aa 959 DD aa 959 DD aa 959 DD bb 959 + DD bb 959 DD bb 959 DD bb 959 EE aa 777 EE aa 777 EE bb 768 + EE bb 768 EE bb 768 FF aa 938 FF aa 938 FF aa 938 FF aa 938 + FF bb 938 FF bb 938 FF bb 938 FF bb 938 FF bb 938 FF bb 938 + GG aa 938 GG aa 938 GG aa 938 GG aa 938 GG bb 979 GG bb 979 + GG bb 979 GG bb 979 HH aa 979 HH aa 979 HH aa 979 HH bb 963 + HH bb 963 HH bb 963 HH bb 963 HH bb 963 HH bb 963 II aa 805 + II aa 805 II bb 899 II bb 899 II bb 899 II bb 899 II bb 899 + JJ aa 899 JJ aa 899 JJ aa 899 JJ aa 899 JJ bb 839 JJ bb 839 + JJ bb 839 JJ bb 839} + +do_execsql_test 1.13.5 { + SELECT a, b, min(c) OVER (ORDER BY a,b GROUPS BETWEEN CURRENT ROW AND 1 FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 223 AA aa 223 AA aa 223 AA aa 223 AA bb 247 AA bb 247 + AA bb 247 AA bb 247 BB aa 247 BB aa 247 BB aa 247 BB aa 247 + BB aa 247 BB aa 247 BB bb 158 BB bb 158 BB bb 158 BB bb 158 + BB bb 158 BB bb 158 CC aa 158 CC aa 158 CC aa 158 CC aa 158 + CC bb 224 CC bb 224 DD aa 224 DD aa 224 DD aa 224 DD bb 113 + DD bb 113 DD bb 113 DD bb 113 EE aa 113 EE aa 113 EE bb 208 + EE bb 208 EE bb 208 FF aa 102 FF aa 102 FF aa 102 FF aa 102 + FF bb 102 FF bb 102 FF bb 102 FF bb 102 FF bb 102 FF bb 102 + GG aa 148 GG aa 148 GG aa 148 GG aa 148 GG bb 160 GG bb 160 + GG bb 160 GG bb 160 HH aa 133 HH aa 133 HH aa 133 HH bb 133 + HH bb 133 HH bb 133 HH bb 133 HH bb 133 HH bb 133 II aa 250 + II aa 250 II bb 113 II bb 113 II bb 113 II bb 113 II bb 113 + JJ aa 113 JJ aa 113 JJ aa 113 JJ aa 113 JJ bb 257 JJ bb 257 + JJ bb 257 JJ bb 257} + +do_execsql_test 1.13.6 { + SELECT a, b, sum(c) OVER (ORDER BY a GROUPS BETWEEN CURRENT ROW AND 1 FOLLOWING EXCLUDE CURRENT ROW) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 11091 AA aa 11114 AA aa 11786 AA aa 11802 AA bb 11155 + AA bb 11398 AA bb 11453 AA bb 11716 BB aa 9700 BB aa 9802 + BB aa 10050 BB aa 10063 BB aa 10069 BB aa 10215 BB bb 9622 + BB bb 9670 BB bb 9676 BB bb 9751 BB bb 9757 BB bb 9829 CC aa 6395 + CC aa 6547 CC aa 6724 CC aa 6996 CC bb 6332 CC bb 6808 DD aa 5726 + DD aa 6315 DD aa 6347 DD bb 5612 DD bb 5777 DD bb 5855 DD bb 6333 + EE aa 7430 EE aa 8094 EE bb 7439 EE bb 7578 EE bb 7955 FF aa 9466 + FF aa 9469 FF aa 9518 FF aa 9928 FF bb 9198 FF bb 9266 FF bb 9410 + FF bb 9562 FF bb 9841 FF bb 10034 GG aa 9277 GG aa 9431 + GG aa 9576 GG aa 9763 GG bb 8973 GG bb 8982 GG bb 9067 GG bb 9751 + HH aa 8104 HH aa 8293 HH aa 8603 HH bb 8120 HH bb 8353 HH bb 8400 + HH bb 8728 HH bb 8753 HH bb 8950 II aa 6788 II aa 7042 II bb 6635 + II bb 6769 II bb 6997 II bb 7019 II bb 7190 JJ aa 2901 JJ aa 3032 + JJ aa 3566 JJ aa 3687 JJ bb 2961 JJ bb 3446 JJ bb 3464 JJ bb 3543} + +do_execsql_test 1.13.7 { + SELECT a, b, sum(c) OVER (ORDER BY a,b GROUPS BETWEEN CURRENT ROW AND 1 FOLLOWING EXCLUDE CURRENT ROW) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 3751 AA aa 3774 AA aa 4446 AA aa 4462 AA bb 4381 AA bb 4624 + AA bb 4679 AA bb 4942 BB aa 6578 BB aa 6680 BB aa 6928 BB aa 6941 + BB aa 6947 BB aa 7093 BB bb 5581 BB bb 5629 BB bb 5635 BB bb 5710 + BB bb 5716 BB bb 5788 CC aa 2363 CC aa 2515 CC aa 2692 CC aa 2964 + CC bb 1671 CC bb 2147 DD aa 3187 DD aa 3776 DD aa 3808 DD bb 2638 + DD bb 2803 DD bb 2881 DD bb 3359 EE aa 1762 EE aa 2426 EE bb 3044 + EE bb 3183 EE bb 3560 FF aa 4998 FF aa 5001 FF aa 5050 FF aa 5460 + FF bb 4164 FF bb 4232 FF bb 4376 FF bb 4528 FF bb 4807 FF bb 5000 + GG aa 3834 GG aa 3988 GG aa 4133 GG aa 4320 GG bb 4182 GG bb 4191 + GG bb 4276 GG bb 4960 HH aa 4464 HH aa 4653 HH aa 4963 HH bb 3281 + HH bb 3514 HH bb 3561 HH bb 3889 HH bb 3914 HH bb 4111 II aa 2988 + II aa 3242 II bb 3799 II bb 3933 II bb 4161 II bb 4183 II bb 4354 + JJ aa 2901 JJ aa 3032 JJ aa 3566 JJ aa 3687 JJ bb 947 JJ bb 1432 + JJ bb 1450 JJ bb 1529} + +do_execsql_test 1.13.8 { + SELECT a, b, + sum(c) OVER (ORDER BY a GROUPS BETWEEN CURRENT ROW AND 1 FOLLOWING EXCLUDE CURRENT ROW), + sum(c) OVER (ORDER BY a GROUPS BETWEEN CURRENT ROW AND 1 FOLLOWING ), + sum(c) OVER (ORDER BY a,b GROUPS BETWEEN CURRENT ROW AND 1 FOLLOWING EXCLUDE CURRENT ROW), + sum(c) OVER (ORDER BY a,b GROUPS BETWEEN CURRENT ROW AND 1 FOLLOWING ) + FROM t3 ORDER BY 1, 2, 3; +} {AA aa 11091 12025 3751 4685 AA aa 11114 12025 3774 4685 + AA aa 11786 12025 4446 4685 AA aa 11802 12025 4462 4685 + AA bb 11155 12025 4381 5251 AA bb 11398 12025 4624 5251 + AA bb 11453 12025 4679 5251 AA bb 11716 12025 4942 5251 + BB aa 9700 10462 6578 7340 BB aa 9802 10462 6680 7340 + BB aa 10050 10462 6928 7340 BB aa 10063 10462 6941 7340 + BB aa 10069 10462 6947 7340 BB aa 10215 10462 7093 7340 + BB bb 9622 10462 5581 6421 BB bb 9670 10462 5629 6421 + BB bb 9676 10462 5635 6421 BB bb 9751 10462 5710 6421 + BB bb 9757 10462 5716 6421 BB bb 9829 10462 5788 6421 + CC aa 6395 7154 2363 3122 CC aa 6547 7154 2515 3122 + CC aa 6724 7154 2692 3122 CC aa 6996 7154 2964 3122 + CC bb 6332 7154 1671 2493 CC bb 6808 7154 2147 2493 + DD aa 5726 6571 3187 4032 DD aa 6315 6571 3776 4032 + DD aa 6347 6571 3808 4032 DD bb 5612 6571 2638 3597 + DD bb 5777 6571 2803 3597 DD bb 5855 6571 2881 3597 + DD bb 6333 6571 3359 3597 EE aa 7430 8207 1762 2539 + EE aa 8094 8207 2426 2539 EE bb 7439 8207 3044 3812 + EE bb 7578 8207 3183 3812 EE bb 7955 8207 3560 3812 + FF aa 9466 10136 4998 5668 FF aa 9469 10136 5001 5668 + FF aa 9518 10136 5050 5668 FF aa 9928 10136 5460 5668 + FF bb 9198 10136 4164 5102 FF bb 9266 10136 4232 5102 + FF bb 9410 10136 4376 5102 FF bb 9562 10136 4528 5102 + FF bb 9841 10136 4807 5102 FF bb 10034 10136 5000 5102 + GG aa 9277 9911 3834 4468 GG aa 9431 9911 3988 4468 + GG aa 9576 9911 4133 4468 GG aa 9763 9911 4320 4468 + GG bb 8973 9911 4182 5120 GG bb 8982 9911 4191 5120 + GG bb 9067 9911 4276 5120 GG bb 9751 9911 4960 5120 + HH aa 8104 9083 4464 5443 HH aa 8293 9083 4653 5443 + HH aa 8603 9083 4963 5443 HH bb 8120 9083 3281 4244 + HH bb 8353 9083 3514 4244 HH bb 8400 9083 3561 4244 + HH bb 8728 9083 3889 4244 HH bb 8753 9083 3914 4244 + HH bb 8950 9083 4111 4244 II aa 6788 7440 2988 3640 + II aa 7042 7440 3242 3640 II bb 6635 7440 3799 4604 + II bb 6769 7440 3933 4604 II bb 6997 7440 4161 4604 + II bb 7019 7440 4183 4604 II bb 7190 7440 4354 4604 + JJ aa 2901 3800 2901 3800 JJ aa 3032 3800 3032 3800 + JJ aa 3566 3800 3566 3800 JJ aa 3687 3800 3687 3800 + JJ bb 2961 3800 947 1786 JJ bb 3446 3800 1432 1786 + JJ bb 3464 3800 1450 1786 JJ bb 3543 3800 1529 1786} + +do_execsql_test 1.14.1 { + SELECT a, b, sum(c) OVER (ORDER BY a GROUPS BETWEEN CURRENT ROW AND 100 FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 44737 AA aa 44737 AA aa 44737 AA aa 44737 AA bb 44737 + AA bb 44737 AA bb 44737 AA bb 44737 BB aa 40052 BB aa 40052 + BB aa 40052 BB aa 40052 BB aa 40052 BB aa 40052 BB bb 40052 + BB bb 40052 BB bb 40052 BB bb 40052 BB bb 40052 BB bb 40052 + CC aa 32712 CC aa 32712 CC aa 32712 CC aa 32712 CC bb 32712 + CC bb 32712 DD aa 29590 DD aa 29590 DD aa 29590 DD bb 29590 + DD bb 29590 DD bb 29590 DD bb 29590 EE aa 25558 EE aa 25558 + EE bb 25558 EE bb 25558 EE bb 25558 FF aa 23019 FF aa 23019 + FF aa 23019 FF aa 23019 FF bb 23019 FF bb 23019 FF bb 23019 + FF bb 23019 FF bb 23019 FF bb 23019 GG aa 17351 GG aa 17351 + GG aa 17351 GG aa 17351 GG bb 17351 GG bb 17351 GG bb 17351 + GG bb 17351 HH aa 12883 HH aa 12883 HH aa 12883 HH bb 12883 + HH bb 12883 HH bb 12883 HH bb 12883 HH bb 12883 HH bb 12883 + II aa 7440 II aa 7440 II bb 7440 II bb 7440 II bb 7440 II bb 7440 + II bb 7440 JJ aa 3800 JJ aa 3800 JJ aa 3800 JJ aa 3800 JJ bb 3800 + JJ bb 3800 JJ bb 3800 JJ bb 3800} + +do_execsql_test 1.14.2 { + SELECT a, b, sum(c) OVER (ORDER BY a,b GROUPS BETWEEN CURRENT ROW AND 100 FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 44737 AA aa 44737 AA aa 44737 AA aa 44737 AA bb 42430 + AA bb 42430 AA bb 42430 AA bb 42430 BB aa 40052 BB aa 40052 + BB aa 40052 BB aa 40052 BB aa 40052 BB aa 40052 BB bb 37179 + BB bb 37179 BB bb 37179 BB bb 37179 BB bb 37179 BB bb 37179 + CC aa 32712 CC aa 32712 CC aa 32712 CC aa 32712 CC bb 30758 + CC bb 30758 DD aa 29590 DD aa 29590 DD aa 29590 DD bb 28265 + DD bb 28265 DD bb 28265 DD bb 28265 EE aa 25558 EE aa 25558 + EE bb 24668 EE bb 24668 EE bb 24668 FF aa 23019 FF aa 23019 + FF aa 23019 FF aa 23019 FF bb 20856 FF bb 20856 FF bb 20856 + FF bb 20856 FF bb 20856 FF bb 20856 GG aa 17351 GG aa 17351 + GG aa 17351 GG aa 17351 GG bb 15754 GG bb 15754 GG bb 15754 + GG bb 15754 HH aa 12883 HH aa 12883 HH aa 12883 HH bb 10634 + HH bb 10634 HH bb 10634 HH bb 10634 HH bb 10634 HH bb 10634 + II aa 7440 II aa 7440 II bb 6390 II bb 6390 II bb 6390 II bb 6390 + II bb 6390 JJ aa 3800 JJ aa 3800 JJ aa 3800 JJ aa 3800 JJ bb 1786 + JJ bb 1786 JJ bb 1786 JJ bb 1786} + +do_execsql_test 1.14.3 { + SELECT a, b, rank() OVER (ORDER BY a GROUPS BETWEEN CURRENT ROW AND 100 FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 1 AA aa 1 AA aa 1 AA aa 1 AA bb 1 AA bb 1 AA bb 1 + AA bb 1 BB aa 9 BB aa 9 BB aa 9 BB aa 9 BB aa 9 BB aa 9 + BB bb 9 BB bb 9 BB bb 9 BB bb 9 BB bb 9 BB bb 9 CC aa 21 + CC aa 21 CC aa 21 CC aa 21 CC bb 21 CC bb 21 DD aa 27 DD aa 27 + DD aa 27 DD bb 27 DD bb 27 DD bb 27 DD bb 27 EE aa 34 EE aa 34 + EE bb 34 EE bb 34 EE bb 34 FF aa 39 FF aa 39 FF aa 39 FF aa 39 + FF bb 39 FF bb 39 FF bb 39 FF bb 39 FF bb 39 FF bb 39 GG aa 49 + GG aa 49 GG aa 49 GG aa 49 GG bb 49 GG bb 49 GG bb 49 GG bb 49 + HH aa 57 HH aa 57 HH aa 57 HH bb 57 HH bb 57 HH bb 57 HH bb 57 + HH bb 57 HH bb 57 II aa 66 II aa 66 II bb 66 II bb 66 II bb 66 + II bb 66 II bb 66 JJ aa 73 JJ aa 73 JJ aa 73 JJ aa 73 JJ bb 73 + JJ bb 73 JJ bb 73 JJ bb 73} + +do_execsql_test 1.14.4 { + SELECT a, b, max(c) OVER (ORDER BY a,b GROUPS BETWEEN CURRENT ROW AND 100 FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 979 AA aa 979 AA aa 979 AA aa 979 AA bb 979 AA bb 979 + AA bb 979 AA bb 979 BB aa 979 BB aa 979 BB aa 979 BB aa 979 + BB aa 979 BB aa 979 BB bb 979 BB bb 979 BB bb 979 BB bb 979 + BB bb 979 BB bb 979 CC aa 979 CC aa 979 CC aa 979 CC aa 979 + CC bb 979 CC bb 979 DD aa 979 DD aa 979 DD aa 979 DD bb 979 + DD bb 979 DD bb 979 DD bb 979 EE aa 979 EE aa 979 EE bb 979 + EE bb 979 EE bb 979 FF aa 979 FF aa 979 FF aa 979 FF aa 979 + FF bb 979 FF bb 979 FF bb 979 FF bb 979 FF bb 979 FF bb 979 + GG aa 979 GG aa 979 GG aa 979 GG aa 979 GG bb 979 GG bb 979 + GG bb 979 GG bb 979 HH aa 979 HH aa 979 HH aa 979 HH bb 963 + HH bb 963 HH bb 963 HH bb 963 HH bb 963 HH bb 963 II aa 899 + II aa 899 II bb 899 II bb 899 II bb 899 II bb 899 II bb 899 + JJ aa 899 JJ aa 899 JJ aa 899 JJ aa 899 JJ bb 839 JJ bb 839 + JJ bb 839 JJ bb 839} + +do_execsql_test 1.14.5 { + SELECT a, b, min(c) OVER (ORDER BY a,b GROUPS BETWEEN CURRENT ROW AND 100 FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 102 AA aa 102 AA aa 102 AA aa 102 AA bb 102 AA bb 102 + AA bb 102 AA bb 102 BB aa 102 BB aa 102 BB aa 102 BB aa 102 + BB aa 102 BB aa 102 BB bb 102 BB bb 102 BB bb 102 BB bb 102 + BB bb 102 BB bb 102 CC aa 102 CC aa 102 CC aa 102 CC aa 102 + CC bb 102 CC bb 102 DD aa 102 DD aa 102 DD aa 102 DD bb 102 + DD bb 102 DD bb 102 DD bb 102 EE aa 102 EE aa 102 EE bb 102 + EE bb 102 EE bb 102 FF aa 102 FF aa 102 FF aa 102 FF aa 102 + FF bb 102 FF bb 102 FF bb 102 FF bb 102 FF bb 102 FF bb 102 + GG aa 113 GG aa 113 GG aa 113 GG aa 113 GG bb 113 GG bb 113 + GG bb 113 GG bb 113 HH aa 113 HH aa 113 HH aa 113 HH bb 113 + HH bb 113 HH bb 113 HH bb 113 HH bb 113 HH bb 113 II aa 113 + II aa 113 II bb 113 II bb 113 II bb 113 II bb 113 II bb 113 + JJ aa 113 JJ aa 113 JJ aa 113 JJ aa 113 JJ bb 257 JJ bb 257 + JJ bb 257 JJ bb 257} + +do_execsql_test 1.14.6 { + SELECT a, b, sum(c) OVER (ORDER BY a GROUPS BETWEEN CURRENT ROW AND 100 FOLLOWING EXCLUDE CURRENT ROW) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 43803 AA aa 43826 AA aa 44498 AA aa 44514 AA bb 43867 + AA bb 44110 AA bb 44165 AA bb 44428 BB aa 39290 BB aa 39392 + BB aa 39640 BB aa 39653 BB aa 39659 BB aa 39805 BB bb 39212 + BB bb 39260 BB bb 39266 BB bb 39341 BB bb 39347 BB bb 39419 + CC aa 31953 CC aa 32105 CC aa 32282 CC aa 32554 CC bb 31890 + CC bb 32366 DD aa 28745 DD aa 29334 DD aa 29366 DD bb 28631 + DD bb 28796 DD bb 28874 DD bb 29352 EE aa 24781 EE aa 25445 + EE bb 24790 EE bb 24929 EE bb 25306 FF aa 22349 FF aa 22352 + FF aa 22401 FF aa 22811 FF bb 22081 FF bb 22149 FF bb 22293 + FF bb 22445 FF bb 22724 FF bb 22917 GG aa 16717 GG aa 16871 + GG aa 17016 GG aa 17203 GG bb 16413 GG bb 16422 GG bb 16507 + GG bb 17191 HH aa 11904 HH aa 12093 HH aa 12403 HH bb 11920 + HH bb 12153 HH bb 12200 HH bb 12528 HH bb 12553 HH bb 12750 + II aa 6788 II aa 7042 II bb 6635 II bb 6769 II bb 6997 II bb 7019 + II bb 7190 JJ aa 2901 JJ aa 3032 JJ aa 3566 JJ aa 3687 JJ bb 2961 + JJ bb 3446 JJ bb 3464 JJ bb 3543} + +do_execsql_test 1.14.7 { + SELECT a, b, sum(c) OVER (ORDER BY a,b GROUPS BETWEEN CURRENT ROW AND 100 FOLLOWING EXCLUDE CURRENT ROW) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 43803 AA aa 43826 AA aa 44498 AA aa 44514 AA bb 41560 + AA bb 41803 AA bb 41858 AA bb 42121 BB aa 39290 BB aa 39392 + BB aa 39640 BB aa 39653 BB aa 39659 BB aa 39805 BB bb 36339 + BB bb 36387 BB bb 36393 BB bb 36468 BB bb 36474 BB bb 36546 + CC aa 31953 CC aa 32105 CC aa 32282 CC aa 32554 CC bb 29936 + CC bb 30412 DD aa 28745 DD aa 29334 DD aa 29366 DD bb 27306 + DD bb 27471 DD bb 27549 DD bb 28027 EE aa 24781 EE aa 25445 + EE bb 23900 EE bb 24039 EE bb 24416 FF aa 22349 FF aa 22352 + FF aa 22401 FF aa 22811 FF bb 19918 FF bb 19986 FF bb 20130 + FF bb 20282 FF bb 20561 FF bb 20754 GG aa 16717 GG aa 16871 + GG aa 17016 GG aa 17203 GG bb 14816 GG bb 14825 GG bb 14910 + GG bb 15594 HH aa 11904 HH aa 12093 HH aa 12403 HH bb 9671 + HH bb 9904 HH bb 9951 HH bb 10279 HH bb 10304 HH bb 10501 + II aa 6788 II aa 7042 II bb 5585 II bb 5719 II bb 5947 II bb 5969 + II bb 6140 JJ aa 2901 JJ aa 3032 JJ aa 3566 JJ aa 3687 JJ bb 947 + JJ bb 1432 JJ bb 1450 JJ bb 1529} + +do_execsql_test 1.14.8 { + SELECT a, b, + sum(c) OVER (ORDER BY a GROUPS BETWEEN CURRENT ROW AND 100 FOLLOWING EXCLUDE CURRENT ROW), + sum(c) OVER (ORDER BY a GROUPS BETWEEN CURRENT ROW AND 100 FOLLOWING ), + sum(c) OVER (ORDER BY a,b GROUPS BETWEEN CURRENT ROW AND 100 FOLLOWING EXCLUDE CURRENT ROW), + sum(c) OVER (ORDER BY a,b GROUPS BETWEEN CURRENT ROW AND 100 FOLLOWING ) + FROM t3 ORDER BY 1, 2, 3; +} {AA aa 43803 44737 43803 44737 AA aa 43826 44737 43826 44737 + AA aa 44498 44737 44498 44737 AA aa 44514 44737 44514 44737 + AA bb 43867 44737 41560 42430 AA bb 44110 44737 41803 42430 + AA bb 44165 44737 41858 42430 AA bb 44428 44737 42121 42430 + BB aa 39290 40052 39290 40052 BB aa 39392 40052 39392 40052 + BB aa 39640 40052 39640 40052 BB aa 39653 40052 39653 40052 + BB aa 39659 40052 39659 40052 BB aa 39805 40052 39805 40052 + BB bb 39212 40052 36339 37179 BB bb 39260 40052 36387 37179 + BB bb 39266 40052 36393 37179 BB bb 39341 40052 36468 37179 + BB bb 39347 40052 36474 37179 BB bb 39419 40052 36546 37179 + CC aa 31953 32712 31953 32712 CC aa 32105 32712 32105 32712 + CC aa 32282 32712 32282 32712 CC aa 32554 32712 32554 32712 + CC bb 31890 32712 29936 30758 CC bb 32366 32712 30412 30758 + DD aa 28745 29590 28745 29590 DD aa 29334 29590 29334 29590 + DD aa 29366 29590 29366 29590 DD bb 28631 29590 27306 28265 + DD bb 28796 29590 27471 28265 DD bb 28874 29590 27549 28265 + DD bb 29352 29590 28027 28265 EE aa 24781 25558 24781 25558 + EE aa 25445 25558 25445 25558 EE bb 24790 25558 23900 24668 + EE bb 24929 25558 24039 24668 EE bb 25306 25558 24416 24668 + FF aa 22349 23019 22349 23019 FF aa 22352 23019 22352 23019 + FF aa 22401 23019 22401 23019 FF aa 22811 23019 22811 23019 + FF bb 22081 23019 19918 20856 FF bb 22149 23019 19986 20856 + FF bb 22293 23019 20130 20856 FF bb 22445 23019 20282 20856 + FF bb 22724 23019 20561 20856 FF bb 22917 23019 20754 20856 + GG aa 16717 17351 16717 17351 GG aa 16871 17351 16871 17351 + GG aa 17016 17351 17016 17351 GG aa 17203 17351 17203 17351 + GG bb 16413 17351 14816 15754 GG bb 16422 17351 14825 15754 + GG bb 16507 17351 14910 15754 GG bb 17191 17351 15594 15754 + HH aa 11904 12883 11904 12883 HH aa 12093 12883 12093 12883 + HH aa 12403 12883 12403 12883 HH bb 11920 12883 9671 10634 + HH bb 12153 12883 9904 10634 HH bb 12200 12883 9951 10634 + HH bb 12528 12883 10279 10634 HH bb 12553 12883 10304 10634 + HH bb 12750 12883 10501 10634 II aa 6788 7440 6788 7440 + II aa 7042 7440 7042 7440 II bb 6635 7440 5585 6390 + II bb 6769 7440 5719 6390 II bb 6997 7440 5947 6390 + II bb 7019 7440 5969 6390 II bb 7190 7440 6140 6390 + JJ aa 2901 3800 2901 3800 JJ aa 3032 3800 3032 3800 + JJ aa 3566 3800 3566 3800 JJ aa 3687 3800 3687 3800 + JJ bb 2961 3800 947 1786 JJ bb 3446 3800 1432 1786 + JJ bb 3464 3800 1450 1786 JJ bb 3543 3800 1529 1786} + +do_execsql_test 1.15.1 { + SELECT a, b, sum(c) OVER (ORDER BY a GROUPS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 44737 AA aa 44737 AA aa 44737 AA aa 44737 AA bb 44737 + AA bb 44737 AA bb 44737 AA bb 44737 BB aa 40052 BB aa 40052 + BB aa 40052 BB aa 40052 BB aa 40052 BB aa 40052 BB bb 40052 + BB bb 40052 BB bb 40052 BB bb 40052 BB bb 40052 BB bb 40052 + CC aa 32712 CC aa 32712 CC aa 32712 CC aa 32712 CC bb 32712 + CC bb 32712 DD aa 29590 DD aa 29590 DD aa 29590 DD bb 29590 + DD bb 29590 DD bb 29590 DD bb 29590 EE aa 25558 EE aa 25558 + EE bb 25558 EE bb 25558 EE bb 25558 FF aa 23019 FF aa 23019 + FF aa 23019 FF aa 23019 FF bb 23019 FF bb 23019 FF bb 23019 + FF bb 23019 FF bb 23019 FF bb 23019 GG aa 17351 GG aa 17351 + GG aa 17351 GG aa 17351 GG bb 17351 GG bb 17351 GG bb 17351 + GG bb 17351 HH aa 12883 HH aa 12883 HH aa 12883 HH bb 12883 + HH bb 12883 HH bb 12883 HH bb 12883 HH bb 12883 HH bb 12883 + II aa 7440 II aa 7440 II bb 7440 II bb 7440 II bb 7440 II bb 7440 + II bb 7440 JJ aa 3800 JJ aa 3800 JJ aa 3800 JJ aa 3800 JJ bb 3800 + JJ bb 3800 JJ bb 3800 JJ bb 3800} + +do_execsql_test 1.15.2 { + SELECT a, b, sum(c) OVER (ORDER BY a,b GROUPS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 44737 AA aa 44737 AA aa 44737 AA aa 44737 AA bb 42430 + AA bb 42430 AA bb 42430 AA bb 42430 BB aa 40052 BB aa 40052 + BB aa 40052 BB aa 40052 BB aa 40052 BB aa 40052 BB bb 37179 + BB bb 37179 BB bb 37179 BB bb 37179 BB bb 37179 BB bb 37179 + CC aa 32712 CC aa 32712 CC aa 32712 CC aa 32712 CC bb 30758 + CC bb 30758 DD aa 29590 DD aa 29590 DD aa 29590 DD bb 28265 + DD bb 28265 DD bb 28265 DD bb 28265 EE aa 25558 EE aa 25558 + EE bb 24668 EE bb 24668 EE bb 24668 FF aa 23019 FF aa 23019 + FF aa 23019 FF aa 23019 FF bb 20856 FF bb 20856 FF bb 20856 + FF bb 20856 FF bb 20856 FF bb 20856 GG aa 17351 GG aa 17351 + GG aa 17351 GG aa 17351 GG bb 15754 GG bb 15754 GG bb 15754 + GG bb 15754 HH aa 12883 HH aa 12883 HH aa 12883 HH bb 10634 + HH bb 10634 HH bb 10634 HH bb 10634 HH bb 10634 HH bb 10634 + II aa 7440 II aa 7440 II bb 6390 II bb 6390 II bb 6390 II bb 6390 + II bb 6390 JJ aa 3800 JJ aa 3800 JJ aa 3800 JJ aa 3800 JJ bb 1786 + JJ bb 1786 JJ bb 1786 JJ bb 1786} + +do_execsql_test 1.15.3 { + SELECT a, b, rank() OVER (ORDER BY a GROUPS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 1 AA aa 1 AA aa 1 AA aa 1 AA bb 1 AA bb 1 AA bb 1 + AA bb 1 BB aa 9 BB aa 9 BB aa 9 BB aa 9 BB aa 9 BB aa 9 + BB bb 9 BB bb 9 BB bb 9 BB bb 9 BB bb 9 BB bb 9 CC aa 21 + CC aa 21 CC aa 21 CC aa 21 CC bb 21 CC bb 21 DD aa 27 DD aa 27 + DD aa 27 DD bb 27 DD bb 27 DD bb 27 DD bb 27 EE aa 34 EE aa 34 + EE bb 34 EE bb 34 EE bb 34 FF aa 39 FF aa 39 FF aa 39 FF aa 39 + FF bb 39 FF bb 39 FF bb 39 FF bb 39 FF bb 39 FF bb 39 GG aa 49 + GG aa 49 GG aa 49 GG aa 49 GG bb 49 GG bb 49 GG bb 49 GG bb 49 + HH aa 57 HH aa 57 HH aa 57 HH bb 57 HH bb 57 HH bb 57 HH bb 57 + HH bb 57 HH bb 57 II aa 66 II aa 66 II bb 66 II bb 66 II bb 66 + II bb 66 II bb 66 JJ aa 73 JJ aa 73 JJ aa 73 JJ aa 73 JJ bb 73 + JJ bb 73 JJ bb 73 JJ bb 73} + +do_execsql_test 1.15.4 { + SELECT a, b, max(c) OVER (ORDER BY a,b GROUPS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 979 AA aa 979 AA aa 979 AA aa 979 AA bb 979 AA bb 979 + AA bb 979 AA bb 979 BB aa 979 BB aa 979 BB aa 979 BB aa 979 + BB aa 979 BB aa 979 BB bb 979 BB bb 979 BB bb 979 BB bb 979 + BB bb 979 BB bb 979 CC aa 979 CC aa 979 CC aa 979 CC aa 979 + CC bb 979 CC bb 979 DD aa 979 DD aa 979 DD aa 979 DD bb 979 + DD bb 979 DD bb 979 DD bb 979 EE aa 979 EE aa 979 EE bb 979 + EE bb 979 EE bb 979 FF aa 979 FF aa 979 FF aa 979 FF aa 979 + FF bb 979 FF bb 979 FF bb 979 FF bb 979 FF bb 979 FF bb 979 + GG aa 979 GG aa 979 GG aa 979 GG aa 979 GG bb 979 GG bb 979 + GG bb 979 GG bb 979 HH aa 979 HH aa 979 HH aa 979 HH bb 963 + HH bb 963 HH bb 963 HH bb 963 HH bb 963 HH bb 963 II aa 899 + II aa 899 II bb 899 II bb 899 II bb 899 II bb 899 II bb 899 + JJ aa 899 JJ aa 899 JJ aa 899 JJ aa 899 JJ bb 839 JJ bb 839 + JJ bb 839 JJ bb 839} + +do_execsql_test 1.15.5 { + SELECT a, b, min(c) OVER (ORDER BY a,b GROUPS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 102 AA aa 102 AA aa 102 AA aa 102 AA bb 102 AA bb 102 + AA bb 102 AA bb 102 BB aa 102 BB aa 102 BB aa 102 BB aa 102 + BB aa 102 BB aa 102 BB bb 102 BB bb 102 BB bb 102 BB bb 102 + BB bb 102 BB bb 102 CC aa 102 CC aa 102 CC aa 102 CC aa 102 + CC bb 102 CC bb 102 DD aa 102 DD aa 102 DD aa 102 DD bb 102 + DD bb 102 DD bb 102 DD bb 102 EE aa 102 EE aa 102 EE bb 102 + EE bb 102 EE bb 102 FF aa 102 FF aa 102 FF aa 102 FF aa 102 + FF bb 102 FF bb 102 FF bb 102 FF bb 102 FF bb 102 FF bb 102 + GG aa 113 GG aa 113 GG aa 113 GG aa 113 GG bb 113 GG bb 113 + GG bb 113 GG bb 113 HH aa 113 HH aa 113 HH aa 113 HH bb 113 + HH bb 113 HH bb 113 HH bb 113 HH bb 113 HH bb 113 II aa 113 + II aa 113 II bb 113 II bb 113 II bb 113 II bb 113 II bb 113 + JJ aa 113 JJ aa 113 JJ aa 113 JJ aa 113 JJ bb 257 JJ bb 257 + JJ bb 257 JJ bb 257} + +do_execsql_test 1.15.6 { + SELECT a, b, sum(c) OVER (ORDER BY a GROUPS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 43803 AA aa 43826 AA aa 44498 AA aa 44514 AA bb 43867 + AA bb 44110 AA bb 44165 AA bb 44428 BB aa 39290 BB aa 39392 + BB aa 39640 BB aa 39653 BB aa 39659 BB aa 39805 BB bb 39212 + BB bb 39260 BB bb 39266 BB bb 39341 BB bb 39347 BB bb 39419 + CC aa 31953 CC aa 32105 CC aa 32282 CC aa 32554 CC bb 31890 + CC bb 32366 DD aa 28745 DD aa 29334 DD aa 29366 DD bb 28631 + DD bb 28796 DD bb 28874 DD bb 29352 EE aa 24781 EE aa 25445 + EE bb 24790 EE bb 24929 EE bb 25306 FF aa 22349 FF aa 22352 + FF aa 22401 FF aa 22811 FF bb 22081 FF bb 22149 FF bb 22293 + FF bb 22445 FF bb 22724 FF bb 22917 GG aa 16717 GG aa 16871 + GG aa 17016 GG aa 17203 GG bb 16413 GG bb 16422 GG bb 16507 + GG bb 17191 HH aa 11904 HH aa 12093 HH aa 12403 HH bb 11920 + HH bb 12153 HH bb 12200 HH bb 12528 HH bb 12553 HH bb 12750 + II aa 6788 II aa 7042 II bb 6635 II bb 6769 II bb 6997 II bb 7019 + II bb 7190 JJ aa 2901 JJ aa 3032 JJ aa 3566 JJ aa 3687 JJ bb 2961 + JJ bb 3446 JJ bb 3464 JJ bb 3543} + +do_execsql_test 1.15.7 { + SELECT a, b, sum(c) OVER (ORDER BY a,b GROUPS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 43803 AA aa 43826 AA aa 44498 AA aa 44514 AA bb 41560 + AA bb 41803 AA bb 41858 AA bb 42121 BB aa 39290 BB aa 39392 + BB aa 39640 BB aa 39653 BB aa 39659 BB aa 39805 BB bb 36339 + BB bb 36387 BB bb 36393 BB bb 36468 BB bb 36474 BB bb 36546 + CC aa 31953 CC aa 32105 CC aa 32282 CC aa 32554 CC bb 29936 + CC bb 30412 DD aa 28745 DD aa 29334 DD aa 29366 DD bb 27306 + DD bb 27471 DD bb 27549 DD bb 28027 EE aa 24781 EE aa 25445 + EE bb 23900 EE bb 24039 EE bb 24416 FF aa 22349 FF aa 22352 + FF aa 22401 FF aa 22811 FF bb 19918 FF bb 19986 FF bb 20130 + FF bb 20282 FF bb 20561 FF bb 20754 GG aa 16717 GG aa 16871 + GG aa 17016 GG aa 17203 GG bb 14816 GG bb 14825 GG bb 14910 + GG bb 15594 HH aa 11904 HH aa 12093 HH aa 12403 HH bb 9671 + HH bb 9904 HH bb 9951 HH bb 10279 HH bb 10304 HH bb 10501 + II aa 6788 II aa 7042 II bb 5585 II bb 5719 II bb 5947 II bb 5969 + II bb 6140 JJ aa 2901 JJ aa 3032 JJ aa 3566 JJ aa 3687 JJ bb 947 + JJ bb 1432 JJ bb 1450 JJ bb 1529} + +do_execsql_test 1.15.8 { + SELECT a, b, + sum(c) OVER (ORDER BY a GROUPS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW), + sum(c) OVER (ORDER BY a GROUPS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ), + sum(c) OVER (ORDER BY a,b GROUPS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW), + sum(c) OVER (ORDER BY a,b GROUPS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) + FROM t3 ORDER BY 1, 2, 3; +} {AA aa 43803 44737 43803 44737 AA aa 43826 44737 43826 44737 + AA aa 44498 44737 44498 44737 AA aa 44514 44737 44514 44737 + AA bb 43867 44737 41560 42430 AA bb 44110 44737 41803 42430 + AA bb 44165 44737 41858 42430 AA bb 44428 44737 42121 42430 + BB aa 39290 40052 39290 40052 BB aa 39392 40052 39392 40052 + BB aa 39640 40052 39640 40052 BB aa 39653 40052 39653 40052 + BB aa 39659 40052 39659 40052 BB aa 39805 40052 39805 40052 + BB bb 39212 40052 36339 37179 BB bb 39260 40052 36387 37179 + BB bb 39266 40052 36393 37179 BB bb 39341 40052 36468 37179 + BB bb 39347 40052 36474 37179 BB bb 39419 40052 36546 37179 + CC aa 31953 32712 31953 32712 CC aa 32105 32712 32105 32712 + CC aa 32282 32712 32282 32712 CC aa 32554 32712 32554 32712 + CC bb 31890 32712 29936 30758 CC bb 32366 32712 30412 30758 + DD aa 28745 29590 28745 29590 DD aa 29334 29590 29334 29590 + DD aa 29366 29590 29366 29590 DD bb 28631 29590 27306 28265 + DD bb 28796 29590 27471 28265 DD bb 28874 29590 27549 28265 + DD bb 29352 29590 28027 28265 EE aa 24781 25558 24781 25558 + EE aa 25445 25558 25445 25558 EE bb 24790 25558 23900 24668 + EE bb 24929 25558 24039 24668 EE bb 25306 25558 24416 24668 + FF aa 22349 23019 22349 23019 FF aa 22352 23019 22352 23019 + FF aa 22401 23019 22401 23019 FF aa 22811 23019 22811 23019 + FF bb 22081 23019 19918 20856 FF bb 22149 23019 19986 20856 + FF bb 22293 23019 20130 20856 FF bb 22445 23019 20282 20856 + FF bb 22724 23019 20561 20856 FF bb 22917 23019 20754 20856 + GG aa 16717 17351 16717 17351 GG aa 16871 17351 16871 17351 + GG aa 17016 17351 17016 17351 GG aa 17203 17351 17203 17351 + GG bb 16413 17351 14816 15754 GG bb 16422 17351 14825 15754 + GG bb 16507 17351 14910 15754 GG bb 17191 17351 15594 15754 + HH aa 11904 12883 11904 12883 HH aa 12093 12883 12093 12883 + HH aa 12403 12883 12403 12883 HH bb 11920 12883 9671 10634 + HH bb 12153 12883 9904 10634 HH bb 12200 12883 9951 10634 + HH bb 12528 12883 10279 10634 HH bb 12553 12883 10304 10634 + HH bb 12750 12883 10501 10634 II aa 6788 7440 6788 7440 + II aa 7042 7440 7042 7440 II bb 6635 7440 5585 6390 + II bb 6769 7440 5719 6390 II bb 6997 7440 5947 6390 + II bb 7019 7440 5969 6390 II bb 7190 7440 6140 6390 + JJ aa 2901 3800 2901 3800 JJ aa 3032 3800 3032 3800 + JJ aa 3566 3800 3566 3800 JJ aa 3687 3800 3687 3800 + JJ bb 2961 3800 947 1786 JJ bb 3446 3800 1432 1786 + JJ bb 3464 3800 1450 1786 JJ bb 3543 3800 1529 1786} + +do_execsql_test 1.16.1 { + SELECT a, b, sum(c) OVER (ORDER BY a GROUPS BETWEEN 0 FOLLOWING AND 0 FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 4685 AA aa 4685 AA aa 4685 AA aa 4685 AA bb 4685 AA bb 4685 + AA bb 4685 AA bb 4685 BB aa 7340 BB aa 7340 BB aa 7340 BB aa 7340 + BB aa 7340 BB aa 7340 BB bb 7340 BB bb 7340 BB bb 7340 BB bb 7340 + BB bb 7340 BB bb 7340 CC aa 3122 CC aa 3122 CC aa 3122 CC aa 3122 + CC bb 3122 CC bb 3122 DD aa 4032 DD aa 4032 DD aa 4032 DD bb 4032 + DD bb 4032 DD bb 4032 DD bb 4032 EE aa 2539 EE aa 2539 EE bb 2539 + EE bb 2539 EE bb 2539 FF aa 5668 FF aa 5668 FF aa 5668 FF aa 5668 + FF bb 5668 FF bb 5668 FF bb 5668 FF bb 5668 FF bb 5668 FF bb 5668 + GG aa 4468 GG aa 4468 GG aa 4468 GG aa 4468 GG bb 4468 GG bb 4468 + GG bb 4468 GG bb 4468 HH aa 5443 HH aa 5443 HH aa 5443 HH bb 5443 + HH bb 5443 HH bb 5443 HH bb 5443 HH bb 5443 HH bb 5443 II aa 3640 + II aa 3640 II bb 3640 II bb 3640 II bb 3640 II bb 3640 II bb 3640 + JJ aa 3800 JJ aa 3800 JJ aa 3800 JJ aa 3800 JJ bb 3800 JJ bb 3800 + JJ bb 3800 JJ bb 3800} + +do_execsql_test 1.16.2 { + SELECT a, b, sum(c) OVER (ORDER BY a,b GROUPS BETWEEN 0 FOLLOWING AND 0 FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 2307 AA aa 2307 AA aa 2307 AA aa 2307 AA bb 2378 AA bb 2378 + AA bb 2378 AA bb 2378 BB aa 2873 BB aa 2873 BB aa 2873 BB aa 2873 + BB aa 2873 BB aa 2873 BB bb 4467 BB bb 4467 BB bb 4467 BB bb 4467 + BB bb 4467 BB bb 4467 CC aa 1954 CC aa 1954 CC aa 1954 CC aa 1954 + CC bb 1168 CC bb 1168 DD aa 1325 DD aa 1325 DD aa 1325 DD bb 2707 + DD bb 2707 DD bb 2707 DD bb 2707 EE aa 890 EE aa 890 EE bb 1649 + EE bb 1649 EE bb 1649 FF aa 2163 FF aa 2163 FF aa 2163 FF aa 2163 + FF bb 3505 FF bb 3505 FF bb 3505 FF bb 3505 FF bb 3505 FF bb 3505 + GG aa 1597 GG aa 1597 GG aa 1597 GG aa 1597 GG bb 2871 GG bb 2871 + GG bb 2871 GG bb 2871 HH aa 2249 HH aa 2249 HH aa 2249 HH bb 3194 + HH bb 3194 HH bb 3194 HH bb 3194 HH bb 3194 HH bb 3194 II aa 1050 + II aa 1050 II bb 2590 II bb 2590 II bb 2590 II bb 2590 II bb 2590 + JJ aa 2014 JJ aa 2014 JJ aa 2014 JJ aa 2014 JJ bb 1786 JJ bb 1786 + JJ bb 1786 JJ bb 1786} + +do_execsql_test 1.16.3 { + SELECT a, b, rank() OVER (ORDER BY a GROUPS BETWEEN 0 FOLLOWING AND 0 FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 1 AA aa 1 AA aa 1 AA aa 1 AA bb 1 AA bb 1 AA bb 1 + AA bb 1 BB aa 9 BB aa 9 BB aa 9 BB aa 9 BB aa 9 BB aa 9 + BB bb 9 BB bb 9 BB bb 9 BB bb 9 BB bb 9 BB bb 9 CC aa 21 + CC aa 21 CC aa 21 CC aa 21 CC bb 21 CC bb 21 DD aa 27 DD aa 27 + DD aa 27 DD bb 27 DD bb 27 DD bb 27 DD bb 27 EE aa 34 EE aa 34 + EE bb 34 EE bb 34 EE bb 34 FF aa 39 FF aa 39 FF aa 39 FF aa 39 + FF bb 39 FF bb 39 FF bb 39 FF bb 39 FF bb 39 FF bb 39 GG aa 49 + GG aa 49 GG aa 49 GG aa 49 GG bb 49 GG bb 49 GG bb 49 GG bb 49 + HH aa 57 HH aa 57 HH aa 57 HH bb 57 HH bb 57 HH bb 57 HH bb 57 + HH bb 57 HH bb 57 II aa 66 II aa 66 II bb 66 II bb 66 II bb 66 + II bb 66 II bb 66 JJ aa 73 JJ aa 73 JJ aa 73 JJ aa 73 JJ bb 73 + JJ bb 73 JJ bb 73 JJ bb 73} + +do_execsql_test 1.16.4 { + SELECT a, b, max(c) OVER (ORDER BY a,b GROUPS BETWEEN 0 FOLLOWING AND 0 FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 934 AA aa 934 AA aa 934 AA aa 934 AA bb 870 AA bb 870 + AA bb 870 AA bb 870 BB aa 762 BB aa 762 BB aa 762 BB aa 762 + BB aa 762 BB aa 762 BB bb 840 BB bb 840 BB bb 840 BB bb 840 + BB bb 840 BB bb 840 CC aa 759 CC aa 759 CC aa 759 CC aa 759 + CC bb 822 CC bb 822 DD aa 845 DD aa 845 DD aa 845 DD bb 959 + DD bb 959 DD bb 959 DD bb 959 EE aa 777 EE aa 777 EE bb 768 + EE bb 768 EE bb 768 FF aa 670 FF aa 670 FF aa 670 FF aa 670 + FF bb 938 FF bb 938 FF bb 938 FF bb 938 FF bb 938 FF bb 938 + GG aa 634 GG aa 634 GG aa 634 GG aa 634 GG bb 938 GG bb 938 + GG bb 938 GG bb 938 HH aa 979 HH aa 979 HH aa 979 HH bb 963 + HH bb 963 HH bb 963 HH bb 963 HH bb 963 HH bb 963 II aa 652 + II aa 652 II bb 805 II bb 805 II bb 805 II bb 805 II bb 805 + JJ aa 899 JJ aa 899 JJ aa 899 JJ aa 899 JJ bb 839 JJ bb 839 + JJ bb 839 JJ bb 839} + +do_execsql_test 1.16.5 { + SELECT a, b, min(c) OVER (ORDER BY a,b GROUPS BETWEEN 0 FOLLOWING AND 0 FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 223 AA aa 223 AA aa 223 AA aa 223 AA bb 309 AA bb 309 + AA bb 309 AA bb 309 BB aa 247 BB aa 247 BB aa 247 BB aa 247 + BB aa 247 BB aa 247 BB bb 633 BB bb 633 BB bb 633 BB bb 633 + BB bb 633 BB bb 633 CC aa 158 CC aa 158 CC aa 158 CC aa 158 + CC bb 346 CC bb 346 DD aa 224 DD aa 224 DD aa 224 DD bb 238 + DD bb 238 DD bb 238 DD bb 238 EE aa 113 EE aa 113 EE bb 252 + EE bb 252 EE bb 252 FF aa 208 FF aa 208 FF aa 208 FF aa 208 + FF bb 102 FF bb 102 FF bb 102 FF bb 102 FF bb 102 FF bb 102 + GG aa 148 GG aa 148 GG aa 148 GG aa 148 GG bb 160 GG bb 160 + GG bb 160 GG bb 160 HH aa 480 HH aa 480 HH aa 480 HH bb 133 + HH bb 133 HH bb 133 HH bb 133 HH bb 133 HH bb 133 II aa 398 + II aa 398 II bb 250 II bb 250 II bb 250 II bb 250 II bb 250 + JJ aa 113 JJ aa 113 JJ aa 113 JJ aa 113 JJ bb 257 JJ bb 257 + JJ bb 257 JJ bb 257} + +do_execsql_test 1.16.6 { + SELECT a, b, sum(c) OVER (ORDER BY a GROUPS BETWEEN 0 FOLLOWING AND 0 FOLLOWING EXCLUDE CURRENT ROW) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 3751 AA aa 3774 AA aa 4446 AA aa 4462 AA bb 3815 AA bb 4058 + AA bb 4113 AA bb 4376 BB aa 6578 BB aa 6680 BB aa 6928 BB aa 6941 + BB aa 6947 BB aa 7093 BB bb 6500 BB bb 6548 BB bb 6554 BB bb 6629 + BB bb 6635 BB bb 6707 CC aa 2363 CC aa 2515 CC aa 2692 CC aa 2964 + CC bb 2300 CC bb 2776 DD aa 3187 DD aa 3776 DD aa 3808 DD bb 3073 + DD bb 3238 DD bb 3316 DD bb 3794 EE aa 1762 EE aa 2426 EE bb 1771 + EE bb 1910 EE bb 2287 FF aa 4998 FF aa 5001 FF aa 5050 FF aa 5460 + FF bb 4730 FF bb 4798 FF bb 4942 FF bb 5094 FF bb 5373 FF bb 5566 + GG aa 3834 GG aa 3988 GG aa 4133 GG aa 4320 GG bb 3530 GG bb 3539 + GG bb 3624 GG bb 4308 HH aa 4464 HH aa 4653 HH aa 4963 HH bb 4480 + HH bb 4713 HH bb 4760 HH bb 5088 HH bb 5113 HH bb 5310 II aa 2988 + II aa 3242 II bb 2835 II bb 2969 II bb 3197 II bb 3219 II bb 3390 + JJ aa 2901 JJ aa 3032 JJ aa 3566 JJ aa 3687 JJ bb 2961 JJ bb 3446 + JJ bb 3464 JJ bb 3543} + +do_execsql_test 1.16.7 { + SELECT a, b, sum(c) OVER (ORDER BY a,b GROUPS BETWEEN 0 FOLLOWING AND 0 FOLLOWING EXCLUDE CURRENT ROW) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 1373 AA aa 1396 AA aa 2068 AA aa 2084 AA bb 1508 AA bb 1751 + AA bb 1806 AA bb 2069 BB aa 2111 BB aa 2213 BB aa 2461 BB aa 2474 + BB aa 2480 BB aa 2626 BB bb 3627 BB bb 3675 BB bb 3681 BB bb 3756 + BB bb 3762 BB bb 3834 CC aa 1195 CC aa 1347 CC aa 1524 CC aa 1796 + CC bb 346 CC bb 822 DD aa 480 DD aa 1069 DD aa 1101 DD bb 1748 + DD bb 1913 DD bb 1991 DD bb 2469 EE aa 113 EE aa 777 EE bb 881 + EE bb 1020 EE bb 1397 FF aa 1493 FF aa 1496 FF aa 1545 FF aa 1955 + FF bb 2567 FF bb 2635 FF bb 2779 FF bb 2931 FF bb 3210 FF bb 3403 + GG aa 963 GG aa 1117 GG aa 1262 GG aa 1449 GG bb 1933 GG bb 1942 + GG bb 2027 GG bb 2711 HH aa 1270 HH aa 1459 HH aa 1769 HH bb 2231 + HH bb 2464 HH bb 2511 HH bb 2839 HH bb 2864 HH bb 3061 II aa 398 + II aa 652 II bb 1785 II bb 1919 II bb 2147 II bb 2169 II bb 2340 + JJ aa 1115 JJ aa 1246 JJ aa 1780 JJ aa 1901 JJ bb 947 JJ bb 1432 + JJ bb 1450 JJ bb 1529} + +do_execsql_test 1.16.8 { + SELECT a, b, + sum(c) OVER (ORDER BY a GROUPS BETWEEN 0 FOLLOWING AND 0 FOLLOWING EXCLUDE CURRENT ROW), + sum(c) OVER (ORDER BY a GROUPS BETWEEN 0 FOLLOWING AND 0 FOLLOWING ), + sum(c) OVER (ORDER BY a,b GROUPS BETWEEN 0 FOLLOWING AND 0 FOLLOWING EXCLUDE CURRENT ROW), + sum(c) OVER (ORDER BY a,b GROUPS BETWEEN 0 FOLLOWING AND 0 FOLLOWING ) + FROM t3 ORDER BY 1, 2, 3; +} {AA aa 3751 4685 1373 2307 AA aa 3774 4685 1396 2307 + AA aa 4446 4685 2068 2307 AA aa 4462 4685 2084 2307 + AA bb 3815 4685 1508 2378 AA bb 4058 4685 1751 2378 + AA bb 4113 4685 1806 2378 AA bb 4376 4685 2069 2378 + BB aa 6578 7340 2111 2873 BB aa 6680 7340 2213 2873 + BB aa 6928 7340 2461 2873 BB aa 6941 7340 2474 2873 + BB aa 6947 7340 2480 2873 BB aa 7093 7340 2626 2873 + BB bb 6500 7340 3627 4467 BB bb 6548 7340 3675 4467 + BB bb 6554 7340 3681 4467 BB bb 6629 7340 3756 4467 + BB bb 6635 7340 3762 4467 BB bb 6707 7340 3834 4467 + CC aa 2363 3122 1195 1954 CC aa 2515 3122 1347 1954 + CC aa 2692 3122 1524 1954 CC aa 2964 3122 1796 1954 + CC bb 2300 3122 346 1168 CC bb 2776 3122 822 1168 + DD aa 3187 4032 480 1325 DD aa 3776 4032 1069 1325 + DD aa 3808 4032 1101 1325 DD bb 3073 4032 1748 2707 + DD bb 3238 4032 1913 2707 DD bb 3316 4032 1991 2707 + DD bb 3794 4032 2469 2707 EE aa 1762 2539 113 890 + EE aa 2426 2539 777 890 EE bb 1771 2539 881 1649 + EE bb 1910 2539 1020 1649 EE bb 2287 2539 1397 1649 + FF aa 4998 5668 1493 2163 FF aa 5001 5668 1496 2163 + FF aa 5050 5668 1545 2163 FF aa 5460 5668 1955 2163 + FF bb 4730 5668 2567 3505 FF bb 4798 5668 2635 3505 + FF bb 4942 5668 2779 3505 FF bb 5094 5668 2931 3505 + FF bb 5373 5668 3210 3505 FF bb 5566 5668 3403 3505 + GG aa 3834 4468 963 1597 GG aa 3988 4468 1117 1597 + GG aa 4133 4468 1262 1597 GG aa 4320 4468 1449 1597 + GG bb 3530 4468 1933 2871 GG bb 3539 4468 1942 2871 + GG bb 3624 4468 2027 2871 GG bb 4308 4468 2711 2871 + HH aa 4464 5443 1270 2249 HH aa 4653 5443 1459 2249 + HH aa 4963 5443 1769 2249 HH bb 4480 5443 2231 3194 + HH bb 4713 5443 2464 3194 HH bb 4760 5443 2511 3194 + HH bb 5088 5443 2839 3194 HH bb 5113 5443 2864 3194 + HH bb 5310 5443 3061 3194 II aa 2988 3640 398 1050 + II aa 3242 3640 652 1050 II bb 2835 3640 1785 2590 + II bb 2969 3640 1919 2590 II bb 3197 3640 2147 2590 + II bb 3219 3640 2169 2590 II bb 3390 3640 2340 2590 + JJ aa 2901 3800 1115 2014 JJ aa 3032 3800 1246 2014 + JJ aa 3566 3800 1780 2014 JJ aa 3687 3800 1901 2014 + JJ bb 2961 3800 947 1786 JJ bb 3446 3800 1432 1786 + JJ bb 3464 3800 1450 1786 JJ bb 3543 3800 1529 1786} + +do_execsql_test 1.17.1 { + SELECT a, b, sum(c) OVER (ORDER BY a GROUPS BETWEEN 1 FOLLOWING AND 0 FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa {} AA aa {} AA aa {} AA aa {} AA bb {} AA bb {} AA bb {} + AA bb {} BB aa {} BB aa {} BB aa {} BB aa {} BB aa {} BB aa {} + BB bb {} BB bb {} BB bb {} BB bb {} BB bb {} BB bb {} CC aa {} + CC aa {} CC aa {} CC aa {} CC bb {} CC bb {} DD aa {} DD aa {} + DD aa {} DD bb {} DD bb {} DD bb {} DD bb {} EE aa {} EE aa {} + EE bb {} EE bb {} EE bb {} FF aa {} FF aa {} FF aa {} FF aa {} + FF bb {} FF bb {} FF bb {} FF bb {} FF bb {} FF bb {} GG aa {} + GG aa {} GG aa {} GG aa {} GG bb {} GG bb {} GG bb {} GG bb {} + HH aa {} HH aa {} HH aa {} HH bb {} HH bb {} HH bb {} HH bb {} + HH bb {} HH bb {} II aa {} II aa {} II bb {} II bb {} II bb {} + II bb {} II bb {} JJ aa {} JJ aa {} JJ aa {} JJ aa {} JJ bb {} + JJ bb {} JJ bb {} JJ bb {}} + +do_execsql_test 1.17.2 { + SELECT a, b, sum(c) OVER (ORDER BY a,b GROUPS BETWEEN 1 FOLLOWING AND 0 FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa {} AA aa {} AA aa {} AA aa {} AA bb {} AA bb {} AA bb {} + AA bb {} BB aa {} BB aa {} BB aa {} BB aa {} BB aa {} BB aa {} + BB bb {} BB bb {} BB bb {} BB bb {} BB bb {} BB bb {} CC aa {} + CC aa {} CC aa {} CC aa {} CC bb {} CC bb {} DD aa {} DD aa {} + DD aa {} DD bb {} DD bb {} DD bb {} DD bb {} EE aa {} EE aa {} + EE bb {} EE bb {} EE bb {} FF aa {} FF aa {} FF aa {} FF aa {} + FF bb {} FF bb {} FF bb {} FF bb {} FF bb {} FF bb {} GG aa {} + GG aa {} GG aa {} GG aa {} GG bb {} GG bb {} GG bb {} GG bb {} + HH aa {} HH aa {} HH aa {} HH bb {} HH bb {} HH bb {} HH bb {} + HH bb {} HH bb {} II aa {} II aa {} II bb {} II bb {} II bb {} + II bb {} II bb {} JJ aa {} JJ aa {} JJ aa {} JJ aa {} JJ bb {} + JJ bb {} JJ bb {} JJ bb {}} + +do_execsql_test 1.17.3 { + SELECT a, b, rank() OVER (ORDER BY a GROUPS BETWEEN 1 FOLLOWING AND 0 FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 1 AA aa 1 AA aa 1 AA aa 1 AA bb 1 AA bb 1 AA bb 1 + AA bb 1 BB aa 9 BB aa 9 BB aa 9 BB aa 9 BB aa 9 BB aa 9 + BB bb 9 BB bb 9 BB bb 9 BB bb 9 BB bb 9 BB bb 9 CC aa 21 + CC aa 21 CC aa 21 CC aa 21 CC bb 21 CC bb 21 DD aa 27 DD aa 27 + DD aa 27 DD bb 27 DD bb 27 DD bb 27 DD bb 27 EE aa 34 EE aa 34 + EE bb 34 EE bb 34 EE bb 34 FF aa 39 FF aa 39 FF aa 39 FF aa 39 + FF bb 39 FF bb 39 FF bb 39 FF bb 39 FF bb 39 FF bb 39 GG aa 49 + GG aa 49 GG aa 49 GG aa 49 GG bb 49 GG bb 49 GG bb 49 GG bb 49 + HH aa 57 HH aa 57 HH aa 57 HH bb 57 HH bb 57 HH bb 57 HH bb 57 + HH bb 57 HH bb 57 II aa 66 II aa 66 II bb 66 II bb 66 II bb 66 + II bb 66 II bb 66 JJ aa 73 JJ aa 73 JJ aa 73 JJ aa 73 JJ bb 73 + JJ bb 73 JJ bb 73 JJ bb 73} + +do_execsql_test 1.17.4 { + SELECT a, b, max(c) OVER (ORDER BY a,b GROUPS BETWEEN 1 FOLLOWING AND 0 FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa {} AA aa {} AA aa {} AA aa {} AA bb {} AA bb {} AA bb {} + AA bb {} BB aa {} BB aa {} BB aa {} BB aa {} BB aa {} BB aa {} + BB bb {} BB bb {} BB bb {} BB bb {} BB bb {} BB bb {} CC aa {} + CC aa {} CC aa {} CC aa {} CC bb {} CC bb {} DD aa {} DD aa {} + DD aa {} DD bb {} DD bb {} DD bb {} DD bb {} EE aa {} EE aa {} + EE bb {} EE bb {} EE bb {} FF aa {} FF aa {} FF aa {} FF aa {} + FF bb {} FF bb {} FF bb {} FF bb {} FF bb {} FF bb {} GG aa {} + GG aa {} GG aa {} GG aa {} GG bb {} GG bb {} GG bb {} GG bb {} + HH aa {} HH aa {} HH aa {} HH bb {} HH bb {} HH bb {} HH bb {} + HH bb {} HH bb {} II aa {} II aa {} II bb {} II bb {} II bb {} + II bb {} II bb {} JJ aa {} JJ aa {} JJ aa {} JJ aa {} JJ bb {} + JJ bb {} JJ bb {} JJ bb {}} + +do_execsql_test 1.17.5 { + SELECT a, b, min(c) OVER (ORDER BY a,b GROUPS BETWEEN 1 FOLLOWING AND 0 FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa {} AA aa {} AA aa {} AA aa {} AA bb {} AA bb {} AA bb {} + AA bb {} BB aa {} BB aa {} BB aa {} BB aa {} BB aa {} BB aa {} + BB bb {} BB bb {} BB bb {} BB bb {} BB bb {} BB bb {} CC aa {} + CC aa {} CC aa {} CC aa {} CC bb {} CC bb {} DD aa {} DD aa {} + DD aa {} DD bb {} DD bb {} DD bb {} DD bb {} EE aa {} EE aa {} + EE bb {} EE bb {} EE bb {} FF aa {} FF aa {} FF aa {} FF aa {} + FF bb {} FF bb {} FF bb {} FF bb {} FF bb {} FF bb {} GG aa {} + GG aa {} GG aa {} GG aa {} GG bb {} GG bb {} GG bb {} GG bb {} + HH aa {} HH aa {} HH aa {} HH bb {} HH bb {} HH bb {} HH bb {} + HH bb {} HH bb {} II aa {} II aa {} II bb {} II bb {} II bb {} + II bb {} II bb {} JJ aa {} JJ aa {} JJ aa {} JJ aa {} JJ bb {} + JJ bb {} JJ bb {} JJ bb {}} + +do_execsql_test 1.17.6 { + SELECT a, b, sum(c) OVER (ORDER BY a GROUPS BETWEEN 1 FOLLOWING AND 0 FOLLOWING EXCLUDE CURRENT ROW) FROM t3 ORDER BY 1, 2, 3; +} {AA aa {} AA aa {} AA aa {} AA aa {} AA bb {} AA bb {} AA bb {} + AA bb {} BB aa {} BB aa {} BB aa {} BB aa {} BB aa {} BB aa {} + BB bb {} BB bb {} BB bb {} BB bb {} BB bb {} BB bb {} CC aa {} + CC aa {} CC aa {} CC aa {} CC bb {} CC bb {} DD aa {} DD aa {} + DD aa {} DD bb {} DD bb {} DD bb {} DD bb {} EE aa {} EE aa {} + EE bb {} EE bb {} EE bb {} FF aa {} FF aa {} FF aa {} FF aa {} + FF bb {} FF bb {} FF bb {} FF bb {} FF bb {} FF bb {} GG aa {} + GG aa {} GG aa {} GG aa {} GG bb {} GG bb {} GG bb {} GG bb {} + HH aa {} HH aa {} HH aa {} HH bb {} HH bb {} HH bb {} HH bb {} + HH bb {} HH bb {} II aa {} II aa {} II bb {} II bb {} II bb {} + II bb {} II bb {} JJ aa {} JJ aa {} JJ aa {} JJ aa {} JJ bb {} + JJ bb {} JJ bb {} JJ bb {}} + +do_execsql_test 1.17.7 { + SELECT a, b, sum(c) OVER (ORDER BY a,b GROUPS BETWEEN 1 FOLLOWING AND 0 FOLLOWING EXCLUDE CURRENT ROW) FROM t3 ORDER BY 1, 2, 3; +} {AA aa {} AA aa {} AA aa {} AA aa {} AA bb {} AA bb {} AA bb {} + AA bb {} BB aa {} BB aa {} BB aa {} BB aa {} BB aa {} BB aa {} + BB bb {} BB bb {} BB bb {} BB bb {} BB bb {} BB bb {} CC aa {} + CC aa {} CC aa {} CC aa {} CC bb {} CC bb {} DD aa {} DD aa {} + DD aa {} DD bb {} DD bb {} DD bb {} DD bb {} EE aa {} EE aa {} + EE bb {} EE bb {} EE bb {} FF aa {} FF aa {} FF aa {} FF aa {} + FF bb {} FF bb {} FF bb {} FF bb {} FF bb {} FF bb {} GG aa {} + GG aa {} GG aa {} GG aa {} GG bb {} GG bb {} GG bb {} GG bb {} + HH aa {} HH aa {} HH aa {} HH bb {} HH bb {} HH bb {} HH bb {} + HH bb {} HH bb {} II aa {} II aa {} II bb {} II bb {} II bb {} + II bb {} II bb {} JJ aa {} JJ aa {} JJ aa {} JJ aa {} JJ bb {} + JJ bb {} JJ bb {} JJ bb {}} + +do_execsql_test 1.17.8 { + SELECT a, b, + sum(c) OVER (ORDER BY a GROUPS BETWEEN 1 FOLLOWING AND 0 FOLLOWING EXCLUDE CURRENT ROW), + sum(c) OVER (ORDER BY a GROUPS BETWEEN 1 FOLLOWING AND 0 FOLLOWING ), + sum(c) OVER (ORDER BY a,b GROUPS BETWEEN 1 FOLLOWING AND 0 FOLLOWING EXCLUDE CURRENT ROW), + sum(c) OVER (ORDER BY a,b GROUPS BETWEEN 1 FOLLOWING AND 0 FOLLOWING ) + FROM t3 ORDER BY 1, 2, 3; +} {AA aa {} {} {} {} AA aa {} {} {} {} AA aa {} {} {} {} + AA aa {} {} {} {} AA bb {} {} {} {} AA bb {} {} {} {} + AA bb {} {} {} {} AA bb {} {} {} {} BB aa {} {} {} {} + BB aa {} {} {} {} BB aa {} {} {} {} BB aa {} {} {} {} + BB aa {} {} {} {} BB aa {} {} {} {} BB bb {} {} {} {} + BB bb {} {} {} {} BB bb {} {} {} {} BB bb {} {} {} {} + BB bb {} {} {} {} BB bb {} {} {} {} CC aa {} {} {} {} + CC aa {} {} {} {} CC aa {} {} {} {} CC aa {} {} {} {} + CC bb {} {} {} {} CC bb {} {} {} {} DD aa {} {} {} {} + DD aa {} {} {} {} DD aa {} {} {} {} DD bb {} {} {} {} + DD bb {} {} {} {} DD bb {} {} {} {} DD bb {} {} {} {} + EE aa {} {} {} {} EE aa {} {} {} {} EE bb {} {} {} {} + EE bb {} {} {} {} EE bb {} {} {} {} FF aa {} {} {} {} + FF aa {} {} {} {} FF aa {} {} {} {} FF aa {} {} {} {} + FF bb {} {} {} {} FF bb {} {} {} {} FF bb {} {} {} {} + FF bb {} {} {} {} FF bb {} {} {} {} FF bb {} {} {} {} + GG aa {} {} {} {} GG aa {} {} {} {} GG aa {} {} {} {} + GG aa {} {} {} {} GG bb {} {} {} {} GG bb {} {} {} {} + GG bb {} {} {} {} GG bb {} {} {} {} HH aa {} {} {} {} + HH aa {} {} {} {} HH aa {} {} {} {} HH bb {} {} {} {} + HH bb {} {} {} {} HH bb {} {} {} {} HH bb {} {} {} {} + HH bb {} {} {} {} HH bb {} {} {} {} II aa {} {} {} {} + II aa {} {} {} {} II bb {} {} {} {} II bb {} {} {} {} + II bb {} {} {} {} II bb {} {} {} {} II bb {} {} {} {} + JJ aa {} {} {} {} JJ aa {} {} {} {} JJ aa {} {} {} {} + JJ aa {} {} {} {} JJ bb {} {} {} {} JJ bb {} {} {} {} + JJ bb {} {} {} {} JJ bb {} {} {} {}} + +do_execsql_test 1.18.1 { + SELECT a, b, sum(c) OVER (ORDER BY a GROUPS BETWEEN 1 FOLLOWING AND 5 FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 22701 AA aa 22701 AA aa 22701 AA aa 22701 AA bb 22701 + AA bb 22701 AA bb 22701 AA bb 22701 BB aa 19829 BB aa 19829 + BB aa 19829 BB aa 19829 BB aa 19829 BB aa 19829 BB bb 19829 + BB bb 19829 BB bb 19829 BB bb 19829 BB bb 19829 BB bb 19829 + CC aa 22150 CC aa 22150 CC aa 22150 CC aa 22150 CC bb 22150 + CC bb 22150 DD aa 21758 DD aa 21758 DD aa 21758 DD bb 21758 + DD bb 21758 DD bb 21758 DD bb 21758 EE aa 23019 EE aa 23019 + EE bb 23019 EE bb 23019 EE bb 23019 FF aa 17351 FF aa 17351 + FF aa 17351 FF aa 17351 FF bb 17351 FF bb 17351 FF bb 17351 + FF bb 17351 FF bb 17351 FF bb 17351 GG aa 12883 GG aa 12883 + GG aa 12883 GG aa 12883 GG bb 12883 GG bb 12883 GG bb 12883 + GG bb 12883 HH aa 7440 HH aa 7440 HH aa 7440 HH bb 7440 + HH bb 7440 HH bb 7440 HH bb 7440 HH bb 7440 HH bb 7440 II aa 3800 + II aa 3800 II bb 3800 II bb 3800 II bb 3800 II bb 3800 II bb 3800 + JJ aa {} JJ aa {} JJ aa {} JJ aa {} JJ bb {} JJ bb {} JJ bb {} + JJ bb {}} + +do_execsql_test 1.18.2 { + SELECT a, b, sum(c) OVER (ORDER BY a,b GROUPS BETWEEN 1 FOLLOWING AND 5 FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 12840 AA aa 12840 AA aa 12840 AA aa 12840 AA bb 11787 + AA bb 11787 AA bb 11787 AA bb 11787 BB aa 11621 BB aa 11621 + BB aa 11621 BB aa 11621 BB aa 11621 BB aa 11621 BB bb 8044 + BB bb 8044 BB bb 8044 BB bb 8044 BB bb 8044 BB bb 8044 CC aa 7739 + CC aa 7739 CC aa 7739 CC aa 7739 CC bb 8734 CC bb 8734 + DD aa 10914 DD aa 10914 DD aa 10914 DD bb 9804 DD bb 9804 + DD bb 9804 DD bb 9804 EE aa 11785 EE aa 11785 EE bb 12385 + EE bb 12385 EE bb 12385 FF aa 13416 FF aa 13416 FF aa 13416 + FF aa 13416 FF bb 10961 FF bb 10961 FF bb 10961 FF bb 10961 + FF bb 10961 FF bb 10961 GG aa 11954 GG aa 11954 GG aa 11954 + GG aa 11954 GG bb 11097 GG bb 11097 GG bb 11097 GG bb 11097 + HH aa 10634 HH aa 10634 HH aa 10634 HH bb 7440 HH bb 7440 + HH bb 7440 HH bb 7440 HH bb 7440 HH bb 7440 II aa 6390 II aa 6390 + II bb 3800 II bb 3800 II bb 3800 II bb 3800 II bb 3800 JJ aa 1786 + JJ aa 1786 JJ aa 1786 JJ aa 1786 JJ bb {} JJ bb {} JJ bb {} + JJ bb {}} + +do_execsql_test 1.18.3 { + SELECT a, b, rank() OVER (ORDER BY a GROUPS BETWEEN 1 FOLLOWING AND 5 FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 1 AA aa 1 AA aa 1 AA aa 1 AA bb 1 AA bb 1 AA bb 1 + AA bb 1 BB aa 9 BB aa 9 BB aa 9 BB aa 9 BB aa 9 BB aa 9 + BB bb 9 BB bb 9 BB bb 9 BB bb 9 BB bb 9 BB bb 9 CC aa 21 + CC aa 21 CC aa 21 CC aa 21 CC bb 21 CC bb 21 DD aa 27 DD aa 27 + DD aa 27 DD bb 27 DD bb 27 DD bb 27 DD bb 27 EE aa 34 EE aa 34 + EE bb 34 EE bb 34 EE bb 34 FF aa 39 FF aa 39 FF aa 39 FF aa 39 + FF bb 39 FF bb 39 FF bb 39 FF bb 39 FF bb 39 FF bb 39 GG aa 49 + GG aa 49 GG aa 49 GG aa 49 GG bb 49 GG bb 49 GG bb 49 GG bb 49 + HH aa 57 HH aa 57 HH aa 57 HH bb 57 HH bb 57 HH bb 57 HH bb 57 + HH bb 57 HH bb 57 II aa 66 II aa 66 II bb 66 II bb 66 II bb 66 + II bb 66 II bb 66 JJ aa 73 JJ aa 73 JJ aa 73 JJ aa 73 JJ bb 73 + JJ bb 73 JJ bb 73 JJ bb 73} + +do_execsql_test 1.18.4 { + SELECT a, b, max(c) OVER (ORDER BY a,b GROUPS BETWEEN 1 FOLLOWING AND 5 FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 870 AA aa 870 AA aa 870 AA aa 870 AA bb 845 AA bb 845 + AA bb 845 AA bb 845 BB aa 959 BB aa 959 BB aa 959 BB aa 959 + BB aa 959 BB aa 959 BB bb 959 BB bb 959 BB bb 959 BB bb 959 + BB bb 959 BB bb 959 CC aa 959 CC aa 959 CC aa 959 CC aa 959 + CC bb 959 CC bb 959 DD aa 959 DD aa 959 DD aa 959 DD bb 938 + DD bb 938 DD bb 938 DD bb 938 EE aa 938 EE aa 938 EE bb 979 + EE bb 979 EE bb 979 FF aa 979 FF aa 979 FF aa 979 FF aa 979 + FF bb 979 FF bb 979 FF bb 979 FF bb 979 FF bb 979 FF bb 979 + GG aa 979 GG aa 979 GG aa 979 GG aa 979 GG bb 979 GG bb 979 + GG bb 979 GG bb 979 HH aa 963 HH aa 963 HH aa 963 HH bb 899 + HH bb 899 HH bb 899 HH bb 899 HH bb 899 HH bb 899 II aa 899 + II aa 899 II bb 899 II bb 899 II bb 899 II bb 899 II bb 899 + JJ aa 839 JJ aa 839 JJ aa 839 JJ aa 839 JJ bb {} JJ bb {} + JJ bb {} JJ bb {}} + +do_execsql_test 1.18.5 { + SELECT a, b, min(c) OVER (ORDER BY a,b GROUPS BETWEEN 1 FOLLOWING AND 5 FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 158 AA aa 158 AA aa 158 AA aa 158 AA bb 158 AA bb 158 + AA bb 158 AA bb 158 BB aa 158 BB aa 158 BB aa 158 BB aa 158 + BB aa 158 BB aa 158 BB bb 113 BB bb 113 BB bb 113 BB bb 113 + BB bb 113 BB bb 113 CC aa 113 CC aa 113 CC aa 113 CC aa 113 + CC bb 113 CC bb 113 DD aa 102 DD aa 102 DD aa 102 DD bb 102 + DD bb 102 DD bb 102 DD bb 102 EE aa 102 EE aa 102 EE bb 102 + EE bb 102 EE bb 102 FF aa 102 FF aa 102 FF aa 102 FF aa 102 + FF bb 133 FF bb 133 FF bb 133 FF bb 133 FF bb 133 FF bb 133 + GG aa 133 GG aa 133 GG aa 133 GG aa 133 GG bb 113 GG bb 113 + GG bb 113 GG bb 113 HH aa 113 HH aa 113 HH aa 113 HH bb 113 + HH bb 113 HH bb 113 HH bb 113 HH bb 113 HH bb 113 II aa 113 + II aa 113 II bb 113 II bb 113 II bb 113 II bb 113 II bb 113 + JJ aa 257 JJ aa 257 JJ aa 257 JJ aa 257 JJ bb {} JJ bb {} + JJ bb {} JJ bb {}} + +do_execsql_test 1.18.6 { + SELECT a, b, sum(c) OVER (ORDER BY a GROUPS BETWEEN 1 FOLLOWING AND 5 FOLLOWING EXCLUDE CURRENT ROW) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 22701 AA aa 22701 AA aa 22701 AA aa 22701 AA bb 22701 + AA bb 22701 AA bb 22701 AA bb 22701 BB aa 19829 BB aa 19829 + BB aa 19829 BB aa 19829 BB aa 19829 BB aa 19829 BB bb 19829 + BB bb 19829 BB bb 19829 BB bb 19829 BB bb 19829 BB bb 19829 + CC aa 22150 CC aa 22150 CC aa 22150 CC aa 22150 CC bb 22150 + CC bb 22150 DD aa 21758 DD aa 21758 DD aa 21758 DD bb 21758 + DD bb 21758 DD bb 21758 DD bb 21758 EE aa 23019 EE aa 23019 + EE bb 23019 EE bb 23019 EE bb 23019 FF aa 17351 FF aa 17351 + FF aa 17351 FF aa 17351 FF bb 17351 FF bb 17351 FF bb 17351 + FF bb 17351 FF bb 17351 FF bb 17351 GG aa 12883 GG aa 12883 + GG aa 12883 GG aa 12883 GG bb 12883 GG bb 12883 GG bb 12883 + GG bb 12883 HH aa 7440 HH aa 7440 HH aa 7440 HH bb 7440 + HH bb 7440 HH bb 7440 HH bb 7440 HH bb 7440 HH bb 7440 II aa 3800 + II aa 3800 II bb 3800 II bb 3800 II bb 3800 II bb 3800 II bb 3800 + JJ aa {} JJ aa {} JJ aa {} JJ aa {} JJ bb {} JJ bb {} JJ bb {} + JJ bb {}} + +do_execsql_test 1.18.7 { + SELECT a, b, sum(c) OVER (ORDER BY a,b GROUPS BETWEEN 1 FOLLOWING AND 5 FOLLOWING EXCLUDE CURRENT ROW) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 12840 AA aa 12840 AA aa 12840 AA aa 12840 AA bb 11787 + AA bb 11787 AA bb 11787 AA bb 11787 BB aa 11621 BB aa 11621 + BB aa 11621 BB aa 11621 BB aa 11621 BB aa 11621 BB bb 8044 + BB bb 8044 BB bb 8044 BB bb 8044 BB bb 8044 BB bb 8044 CC aa 7739 + CC aa 7739 CC aa 7739 CC aa 7739 CC bb 8734 CC bb 8734 + DD aa 10914 DD aa 10914 DD aa 10914 DD bb 9804 DD bb 9804 + DD bb 9804 DD bb 9804 EE aa 11785 EE aa 11785 EE bb 12385 + EE bb 12385 EE bb 12385 FF aa 13416 FF aa 13416 FF aa 13416 + FF aa 13416 FF bb 10961 FF bb 10961 FF bb 10961 FF bb 10961 + FF bb 10961 FF bb 10961 GG aa 11954 GG aa 11954 GG aa 11954 + GG aa 11954 GG bb 11097 GG bb 11097 GG bb 11097 GG bb 11097 + HH aa 10634 HH aa 10634 HH aa 10634 HH bb 7440 HH bb 7440 + HH bb 7440 HH bb 7440 HH bb 7440 HH bb 7440 II aa 6390 II aa 6390 + II bb 3800 II bb 3800 II bb 3800 II bb 3800 II bb 3800 JJ aa 1786 + JJ aa 1786 JJ aa 1786 JJ aa 1786 JJ bb {} JJ bb {} JJ bb {} + JJ bb {}} + +do_execsql_test 1.18.8 { + SELECT a, b, + sum(c) OVER (ORDER BY a GROUPS BETWEEN 1 FOLLOWING AND 5 FOLLOWING EXCLUDE CURRENT ROW), + sum(c) OVER (ORDER BY a GROUPS BETWEEN 1 FOLLOWING AND 5 FOLLOWING ), + sum(c) OVER (ORDER BY a,b GROUPS BETWEEN 1 FOLLOWING AND 5 FOLLOWING EXCLUDE CURRENT ROW), + sum(c) OVER (ORDER BY a,b GROUPS BETWEEN 1 FOLLOWING AND 5 FOLLOWING ) + FROM t3 ORDER BY 1, 2, 3; +} {AA aa 22701 22701 12840 12840 AA aa 22701 22701 12840 12840 + AA aa 22701 22701 12840 12840 AA aa 22701 22701 12840 12840 + AA bb 22701 22701 11787 11787 AA bb 22701 22701 11787 11787 + AA bb 22701 22701 11787 11787 AA bb 22701 22701 11787 11787 + BB aa 19829 19829 11621 11621 BB aa 19829 19829 11621 11621 + BB aa 19829 19829 11621 11621 BB aa 19829 19829 11621 11621 + BB aa 19829 19829 11621 11621 BB aa 19829 19829 11621 11621 + BB bb 19829 19829 8044 8044 BB bb 19829 19829 8044 8044 + BB bb 19829 19829 8044 8044 BB bb 19829 19829 8044 8044 + BB bb 19829 19829 8044 8044 BB bb 19829 19829 8044 8044 + CC aa 22150 22150 7739 7739 CC aa 22150 22150 7739 7739 + CC aa 22150 22150 7739 7739 CC aa 22150 22150 7739 7739 + CC bb 22150 22150 8734 8734 CC bb 22150 22150 8734 8734 + DD aa 21758 21758 10914 10914 DD aa 21758 21758 10914 10914 + DD aa 21758 21758 10914 10914 DD bb 21758 21758 9804 9804 + DD bb 21758 21758 9804 9804 DD bb 21758 21758 9804 9804 + DD bb 21758 21758 9804 9804 EE aa 23019 23019 11785 11785 + EE aa 23019 23019 11785 11785 EE bb 23019 23019 12385 12385 + EE bb 23019 23019 12385 12385 EE bb 23019 23019 12385 12385 + FF aa 17351 17351 13416 13416 FF aa 17351 17351 13416 13416 + FF aa 17351 17351 13416 13416 FF aa 17351 17351 13416 13416 + FF bb 17351 17351 10961 10961 FF bb 17351 17351 10961 10961 + FF bb 17351 17351 10961 10961 FF bb 17351 17351 10961 10961 + FF bb 17351 17351 10961 10961 FF bb 17351 17351 10961 10961 + GG aa 12883 12883 11954 11954 GG aa 12883 12883 11954 11954 + GG aa 12883 12883 11954 11954 GG aa 12883 12883 11954 11954 + GG bb 12883 12883 11097 11097 GG bb 12883 12883 11097 11097 + GG bb 12883 12883 11097 11097 GG bb 12883 12883 11097 11097 + HH aa 7440 7440 10634 10634 HH aa 7440 7440 10634 10634 + HH aa 7440 7440 10634 10634 HH bb 7440 7440 7440 7440 + HH bb 7440 7440 7440 7440 HH bb 7440 7440 7440 7440 + HH bb 7440 7440 7440 7440 HH bb 7440 7440 7440 7440 + HH bb 7440 7440 7440 7440 II aa 3800 3800 6390 6390 + II aa 3800 3800 6390 6390 II bb 3800 3800 3800 3800 + II bb 3800 3800 3800 3800 II bb 3800 3800 3800 3800 + II bb 3800 3800 3800 3800 II bb 3800 3800 3800 3800 + JJ aa {} {} 1786 1786 JJ aa {} {} 1786 1786 JJ aa {} {} 1786 1786 + JJ aa {} {} 1786 1786 JJ bb {} {} {} {} JJ bb {} {} {} {} + JJ bb {} {} {} {} JJ bb {} {} {} {}} + +do_execsql_test 1.19.1 { + SELECT a, b, sum(c) OVER (ORDER BY a GROUPS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 40052 AA aa 40052 AA aa 40052 AA aa 40052 AA bb 40052 + AA bb 40052 AA bb 40052 AA bb 40052 BB aa 32712 BB aa 32712 + BB aa 32712 BB aa 32712 BB aa 32712 BB aa 32712 BB bb 32712 + BB bb 32712 BB bb 32712 BB bb 32712 BB bb 32712 BB bb 32712 + CC aa 29590 CC aa 29590 CC aa 29590 CC aa 29590 CC bb 29590 + CC bb 29590 DD aa 25558 DD aa 25558 DD aa 25558 DD bb 25558 + DD bb 25558 DD bb 25558 DD bb 25558 EE aa 23019 EE aa 23019 + EE bb 23019 EE bb 23019 EE bb 23019 FF aa 17351 FF aa 17351 + FF aa 17351 FF aa 17351 FF bb 17351 FF bb 17351 FF bb 17351 + FF bb 17351 FF bb 17351 FF bb 17351 GG aa 12883 GG aa 12883 + GG aa 12883 GG aa 12883 GG bb 12883 GG bb 12883 GG bb 12883 + GG bb 12883 HH aa 7440 HH aa 7440 HH aa 7440 HH bb 7440 + HH bb 7440 HH bb 7440 HH bb 7440 HH bb 7440 HH bb 7440 II aa 3800 + II aa 3800 II bb 3800 II bb 3800 II bb 3800 II bb 3800 II bb 3800 + JJ aa {} JJ aa {} JJ aa {} JJ aa {} JJ bb {} JJ bb {} JJ bb {} + JJ bb {}} + +do_execsql_test 1.19.2 { + SELECT a, b, sum(c) OVER (ORDER BY a,b GROUPS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 42430 AA aa 42430 AA aa 42430 AA aa 42430 AA bb 40052 + AA bb 40052 AA bb 40052 AA bb 40052 BB aa 37179 BB aa 37179 + BB aa 37179 BB aa 37179 BB aa 37179 BB aa 37179 BB bb 32712 + BB bb 32712 BB bb 32712 BB bb 32712 BB bb 32712 BB bb 32712 + CC aa 30758 CC aa 30758 CC aa 30758 CC aa 30758 CC bb 29590 + CC bb 29590 DD aa 28265 DD aa 28265 DD aa 28265 DD bb 25558 + DD bb 25558 DD bb 25558 DD bb 25558 EE aa 24668 EE aa 24668 + EE bb 23019 EE bb 23019 EE bb 23019 FF aa 20856 FF aa 20856 + FF aa 20856 FF aa 20856 FF bb 17351 FF bb 17351 FF bb 17351 + FF bb 17351 FF bb 17351 FF bb 17351 GG aa 15754 GG aa 15754 + GG aa 15754 GG aa 15754 GG bb 12883 GG bb 12883 GG bb 12883 + GG bb 12883 HH aa 10634 HH aa 10634 HH aa 10634 HH bb 7440 + HH bb 7440 HH bb 7440 HH bb 7440 HH bb 7440 HH bb 7440 II aa 6390 + II aa 6390 II bb 3800 II bb 3800 II bb 3800 II bb 3800 II bb 3800 + JJ aa 1786 JJ aa 1786 JJ aa 1786 JJ aa 1786 JJ bb {} JJ bb {} + JJ bb {} JJ bb {}} + +do_execsql_test 1.19.3 { + SELECT a, b, rank() OVER (ORDER BY a GROUPS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 1 AA aa 1 AA aa 1 AA aa 1 AA bb 1 AA bb 1 AA bb 1 + AA bb 1 BB aa 9 BB aa 9 BB aa 9 BB aa 9 BB aa 9 BB aa 9 + BB bb 9 BB bb 9 BB bb 9 BB bb 9 BB bb 9 BB bb 9 CC aa 21 + CC aa 21 CC aa 21 CC aa 21 CC bb 21 CC bb 21 DD aa 27 DD aa 27 + DD aa 27 DD bb 27 DD bb 27 DD bb 27 DD bb 27 EE aa 34 EE aa 34 + EE bb 34 EE bb 34 EE bb 34 FF aa 39 FF aa 39 FF aa 39 FF aa 39 + FF bb 39 FF bb 39 FF bb 39 FF bb 39 FF bb 39 FF bb 39 GG aa 49 + GG aa 49 GG aa 49 GG aa 49 GG bb 49 GG bb 49 GG bb 49 GG bb 49 + HH aa 57 HH aa 57 HH aa 57 HH bb 57 HH bb 57 HH bb 57 HH bb 57 + HH bb 57 HH bb 57 II aa 66 II aa 66 II bb 66 II bb 66 II bb 66 + II bb 66 II bb 66 JJ aa 73 JJ aa 73 JJ aa 73 JJ aa 73 JJ bb 73 + JJ bb 73 JJ bb 73 JJ bb 73} + +do_execsql_test 1.19.4 { + SELECT a, b, max(c) OVER (ORDER BY a,b GROUPS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 979 AA aa 979 AA aa 979 AA aa 979 AA bb 979 AA bb 979 + AA bb 979 AA bb 979 BB aa 979 BB aa 979 BB aa 979 BB aa 979 + BB aa 979 BB aa 979 BB bb 979 BB bb 979 BB bb 979 BB bb 979 + BB bb 979 BB bb 979 CC aa 979 CC aa 979 CC aa 979 CC aa 979 + CC bb 979 CC bb 979 DD aa 979 DD aa 979 DD aa 979 DD bb 979 + DD bb 979 DD bb 979 DD bb 979 EE aa 979 EE aa 979 EE bb 979 + EE bb 979 EE bb 979 FF aa 979 FF aa 979 FF aa 979 FF aa 979 + FF bb 979 FF bb 979 FF bb 979 FF bb 979 FF bb 979 FF bb 979 + GG aa 979 GG aa 979 GG aa 979 GG aa 979 GG bb 979 GG bb 979 + GG bb 979 GG bb 979 HH aa 963 HH aa 963 HH aa 963 HH bb 899 + HH bb 899 HH bb 899 HH bb 899 HH bb 899 HH bb 899 II aa 899 + II aa 899 II bb 899 II bb 899 II bb 899 II bb 899 II bb 899 + JJ aa 839 JJ aa 839 JJ aa 839 JJ aa 839 JJ bb {} JJ bb {} + JJ bb {} JJ bb {}} + +do_execsql_test 1.19.5 { + SELECT a, b, min(c) OVER (ORDER BY a,b GROUPS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING ) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 102 AA aa 102 AA aa 102 AA aa 102 AA bb 102 AA bb 102 + AA bb 102 AA bb 102 BB aa 102 BB aa 102 BB aa 102 BB aa 102 + BB aa 102 BB aa 102 BB bb 102 BB bb 102 BB bb 102 BB bb 102 + BB bb 102 BB bb 102 CC aa 102 CC aa 102 CC aa 102 CC aa 102 + CC bb 102 CC bb 102 DD aa 102 DD aa 102 DD aa 102 DD bb 102 + DD bb 102 DD bb 102 DD bb 102 EE aa 102 EE aa 102 EE bb 102 + EE bb 102 EE bb 102 FF aa 102 FF aa 102 FF aa 102 FF aa 102 + FF bb 113 FF bb 113 FF bb 113 FF bb 113 FF bb 113 FF bb 113 + GG aa 113 GG aa 113 GG aa 113 GG aa 113 GG bb 113 GG bb 113 + GG bb 113 GG bb 113 HH aa 113 HH aa 113 HH aa 113 HH bb 113 + HH bb 113 HH bb 113 HH bb 113 HH bb 113 HH bb 113 II aa 113 + II aa 113 II bb 113 II bb 113 II bb 113 II bb 113 II bb 113 + JJ aa 257 JJ aa 257 JJ aa 257 JJ aa 257 JJ bb {} JJ bb {} + JJ bb {} JJ bb {}} + +do_execsql_test 1.19.6 { + SELECT a, b, sum(c) OVER (ORDER BY a GROUPS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 40052 AA aa 40052 AA aa 40052 AA aa 40052 AA bb 40052 + AA bb 40052 AA bb 40052 AA bb 40052 BB aa 32712 BB aa 32712 + BB aa 32712 BB aa 32712 BB aa 32712 BB aa 32712 BB bb 32712 + BB bb 32712 BB bb 32712 BB bb 32712 BB bb 32712 BB bb 32712 + CC aa 29590 CC aa 29590 CC aa 29590 CC aa 29590 CC bb 29590 + CC bb 29590 DD aa 25558 DD aa 25558 DD aa 25558 DD bb 25558 + DD bb 25558 DD bb 25558 DD bb 25558 EE aa 23019 EE aa 23019 + EE bb 23019 EE bb 23019 EE bb 23019 FF aa 17351 FF aa 17351 + FF aa 17351 FF aa 17351 FF bb 17351 FF bb 17351 FF bb 17351 + FF bb 17351 FF bb 17351 FF bb 17351 GG aa 12883 GG aa 12883 + GG aa 12883 GG aa 12883 GG bb 12883 GG bb 12883 GG bb 12883 + GG bb 12883 HH aa 7440 HH aa 7440 HH aa 7440 HH bb 7440 + HH bb 7440 HH bb 7440 HH bb 7440 HH bb 7440 HH bb 7440 II aa 3800 + II aa 3800 II bb 3800 II bb 3800 II bb 3800 II bb 3800 II bb 3800 + JJ aa {} JJ aa {} JJ aa {} JJ aa {} JJ bb {} JJ bb {} JJ bb {} + JJ bb {}} + +do_execsql_test 1.19.7 { + SELECT a, b, sum(c) OVER (ORDER BY a,b GROUPS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW) FROM t3 ORDER BY 1, 2, 3; +} {AA aa 42430 AA aa 42430 AA aa 42430 AA aa 42430 AA bb 40052 + AA bb 40052 AA bb 40052 AA bb 40052 BB aa 37179 BB aa 37179 + BB aa 37179 BB aa 37179 BB aa 37179 BB aa 37179 BB bb 32712 + BB bb 32712 BB bb 32712 BB bb 32712 BB bb 32712 BB bb 32712 + CC aa 30758 CC aa 30758 CC aa 30758 CC aa 30758 CC bb 29590 + CC bb 29590 DD aa 28265 DD aa 28265 DD aa 28265 DD bb 25558 + DD bb 25558 DD bb 25558 DD bb 25558 EE aa 24668 EE aa 24668 + EE bb 23019 EE bb 23019 EE bb 23019 FF aa 20856 FF aa 20856 + FF aa 20856 FF aa 20856 FF bb 17351 FF bb 17351 FF bb 17351 + FF bb 17351 FF bb 17351 FF bb 17351 GG aa 15754 GG aa 15754 + GG aa 15754 GG aa 15754 GG bb 12883 GG bb 12883 GG bb 12883 + GG bb 12883 HH aa 10634 HH aa 10634 HH aa 10634 HH bb 7440 + HH bb 7440 HH bb 7440 HH bb 7440 HH bb 7440 HH bb 7440 II aa 6390 + II aa 6390 II bb 3800 II bb 3800 II bb 3800 II bb 3800 II bb 3800 + JJ aa 1786 JJ aa 1786 JJ aa 1786 JJ aa 1786 JJ bb {} JJ bb {} + JJ bb {} JJ bb {}} + +do_execsql_test 1.19.8 { + SELECT a, b, + sum(c) OVER (ORDER BY a GROUPS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW), + sum(c) OVER (ORDER BY a GROUPS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING ), + sum(c) OVER (ORDER BY a,b GROUPS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW), + sum(c) OVER (ORDER BY a,b GROUPS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING ) + FROM t3 ORDER BY 1, 2, 3; +} {AA aa 40052 40052 42430 42430 AA aa 40052 40052 42430 42430 + AA aa 40052 40052 42430 42430 AA aa 40052 40052 42430 42430 + AA bb 40052 40052 40052 40052 AA bb 40052 40052 40052 40052 + AA bb 40052 40052 40052 40052 AA bb 40052 40052 40052 40052 + BB aa 32712 32712 37179 37179 BB aa 32712 32712 37179 37179 + BB aa 32712 32712 37179 37179 BB aa 32712 32712 37179 37179 + BB aa 32712 32712 37179 37179 BB aa 32712 32712 37179 37179 + BB bb 32712 32712 32712 32712 BB bb 32712 32712 32712 32712 + BB bb 32712 32712 32712 32712 BB bb 32712 32712 32712 32712 + BB bb 32712 32712 32712 32712 BB bb 32712 32712 32712 32712 + CC aa 29590 29590 30758 30758 CC aa 29590 29590 30758 30758 + CC aa 29590 29590 30758 30758 CC aa 29590 29590 30758 30758 + CC bb 29590 29590 29590 29590 CC bb 29590 29590 29590 29590 + DD aa 25558 25558 28265 28265 DD aa 25558 25558 28265 28265 + DD aa 25558 25558 28265 28265 DD bb 25558 25558 25558 25558 + DD bb 25558 25558 25558 25558 DD bb 25558 25558 25558 25558 + DD bb 25558 25558 25558 25558 EE aa 23019 23019 24668 24668 + EE aa 23019 23019 24668 24668 EE bb 23019 23019 23019 23019 + EE bb 23019 23019 23019 23019 EE bb 23019 23019 23019 23019 + FF aa 17351 17351 20856 20856 FF aa 17351 17351 20856 20856 + FF aa 17351 17351 20856 20856 FF aa 17351 17351 20856 20856 + FF bb 17351 17351 17351 17351 FF bb 17351 17351 17351 17351 + FF bb 17351 17351 17351 17351 FF bb 17351 17351 17351 17351 + FF bb 17351 17351 17351 17351 FF bb 17351 17351 17351 17351 + GG aa 12883 12883 15754 15754 GG aa 12883 12883 15754 15754 + GG aa 12883 12883 15754 15754 GG aa 12883 12883 15754 15754 + GG bb 12883 12883 12883 12883 GG bb 12883 12883 12883 12883 + GG bb 12883 12883 12883 12883 GG bb 12883 12883 12883 12883 + HH aa 7440 7440 10634 10634 HH aa 7440 7440 10634 10634 + HH aa 7440 7440 10634 10634 HH bb 7440 7440 7440 7440 + HH bb 7440 7440 7440 7440 HH bb 7440 7440 7440 7440 + HH bb 7440 7440 7440 7440 HH bb 7440 7440 7440 7440 + HH bb 7440 7440 7440 7440 II aa 3800 3800 6390 6390 + II aa 3800 3800 6390 6390 II bb 3800 3800 3800 3800 + II bb 3800 3800 3800 3800 II bb 3800 3800 3800 3800 + II bb 3800 3800 3800 3800 II bb 3800 3800 3800 3800 + JJ aa {} {} 1786 1786 JJ aa {} {} 1786 1786 JJ aa {} {} 1786 1786 + JJ aa {} {} 1786 1786 JJ bb {} {} {} {} JJ bb {} {} {} {} + JJ bb {} {} {} {} JJ bb {} {} {} {}} + +do_execsql_test 2.1.1 { + SELECT row_number() OVER win + FROM t3 + WINDOW win AS ( + ORDER BY c, b, a + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE NO OTHERS + ) +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80} + +do_execsql_test 2.1.2 { + SELECT nth_value(c, 14) OVER win + FROM t3 + WINDOW win AS ( + ORDER BY c, b, a + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE NO OTHERS + ) +} {247 247 247 247 247 247 247 247 247 247 247 247 247 + 247 247 247 247 247 247 247 247 247 247 247 247 247 + 247 247 247 247 247 247 247 247 247 247 247 247 247 + 247 247 247 247 247 247 247 247 247 247 247 247 247 + 247 247 247 247 247 247 247 247 247 247 247 247 247 + 247 247 247 247 247 247 247 247 247 247 247 247 247 + 247 247} + +do_execsql_test 2.1.3 { + SELECT min(c) OVER win, max(c) OVER win, sum(c) OVER win FROM t3 + WINDOW win AS ( + ORDER BY c, b, a + ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW EXCLUDE NO OTHERS + ) ORDER BY a, b, c; +} {102 223 1358 102 239 2293 102 911 38097 102 934 39960 102 309 4159 + 102 572 10643 102 627 13069 102 870 35417 102 247 2540 102 393 6608 + 102 399 7405 102 412 7817 102 660 16277 102 762 24077 102 633 14331 + 102 705 19673 102 711 20384 102 786 27176 102 792 28758 + 102 840 32858 102 158 767 102 430 8668 102 607 11824 102 759 23315 + 102 346 5506 102 822 31179 102 224 1582 102 256 3298 102 845 34547 + 102 238 2054 102 716 21100 102 794 29552 102 959 42795 102 113 215 + 102 777 26390 102 252 3042 102 629 13698 102 768 25613 102 208 1135 + 102 618 12442 102 667 16944 102 670 17614 102 102 102 102 295 3850 + 102 574 11217 102 726 21826 102 870 36287 102 938 40898 102 148 609 + 102 335 4824 102 480 9591 102 634 14965 102 160 927 102 844 33702 + 102 929 39026 102 938 41836 102 480 10071 102 790 27966 + 102 979 44737 102 133 461 102 330 4489 102 355 6215 102 683 18968 + 102 730 22556 102 963 43758 102 398 7006 102 652 15617 102 250 2790 + 102 421 8238 102 443 9111 102 671 18285 102 805 30357 102 113 328 + 102 234 1816 102 768 24845 102 899 37186 102 257 3555 102 336 5160 + 102 354 5860 102 839 32018} + +do_execsql_test 2.2.1 { + SELECT row_number() OVER win + FROM t3 + WINDOW win AS ( + ORDER BY c, b, a + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW + ) +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80} + +do_execsql_test 2.2.2 { + SELECT nth_value(c, 14) OVER win + FROM t3 + WINDOW win AS ( + ORDER BY c, b, a + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW + ) +} {250 250 250 250 250 250 250 250 250 250 250 250 250 + 250 247 247 247 247 247 247 247 247 247 247 247 247 + 247 247 247 247 247 247 247 247 247 247 247 247 247 + 247 247 247 247 247 247 247 247 247 247 247 247 247 + 247 247 247 247 247 247 247 247 247 247 247 247 247 + 247 247 247 247 247 247 247 247 247 247 247 247 247 + 247 247} + +do_execsql_test 2.2.3 { + SELECT min(c) OVER win, max(c) OVER win, sum(c) OVER win FROM t3 + WINDOW win AS ( + ORDER BY c, b, a + ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW EXCLUDE CURRENT ROW + ) ORDER BY a, b, c; +} {102 208 1135 102 238 2054 102 899 37186 102 929 39026 102 295 3850 + 102 480 10071 102 618 12442 102 845 34547 102 239 2293 102 355 6215 + 102 398 7006 102 399 7405 102 652 15617 102 759 23315 102 629 13698 + 102 683 18968 102 705 19673 102 777 26390 102 790 27966 + 102 839 32018 102 148 609 102 421 8238 102 574 11217 102 730 22556 + 102 336 5160 102 805 30357 102 223 1358 102 252 3042 102 844 33702 + 102 234 1816 102 711 20384 102 792 28758 102 938 41836 102 102 102 + 102 768 25613 102 250 2790 102 627 13069 102 768 24845 102 160 927 + 102 607 11824 102 660 16277 102 667 16944 {} {} {} 102 257 3555 + 102 572 10643 102 716 21100 102 870 35417 102 934 39960 102 133 461 + 102 330 4489 102 443 9111 102 633 14331 102 158 767 102 840 32858 + 102 911 38097 102 938 40898 102 480 9591 102 786 27176 + 102 963 43758 102 113 328 102 309 4159 102 354 5860 102 671 18285 + 102 726 21826 102 959 42795 102 393 6608 102 634 14965 102 247 2540 + 102 412 7817 102 430 8668 102 670 17614 102 794 29552 102 113 215 + 102 224 1582 102 762 24077 102 870 36287 102 256 3298 102 335 4824 + 102 346 5506 102 822 31179} + +do_execsql_test 2.3.1 { + SELECT row_number() OVER win + FROM t3 + WINDOW win AS ( + ORDER BY c, b, a + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP + ) +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80} + +do_execsql_test 2.3.2 { + SELECT nth_value(c, 14) OVER win + FROM t3 + WINDOW win AS ( + ORDER BY c, b, a + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP + ) +} {250 250 250 250 250 250 250 250 250 250 250 250 250 + 250 247 247 247 247 247 247 247 247 247 247 247 247 + 247 247 247 247 247 247 247 247 247 247 247 247 247 + 247 247 247 247 247 247 247 247 247 247 247 247 247 + 247 247 247 247 247 247 247 247 247 247 247 247 247 + 247 247 247 247 247 247 247 247 247 247 247 247 247 + 247 247} + +do_execsql_test 2.3.3 { + SELECT min(c) OVER win, max(c) OVER win, sum(c) OVER win FROM t3 + WINDOW win AS ( + ORDER BY c, b, a + ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW EXCLUDE GROUP + ) ORDER BY a, b, c; +} {102 208 1135 102 238 2054 102 899 37186 102 929 39026 102 295 3850 + 102 480 10071 102 618 12442 102 845 34547 102 239 2293 102 355 6215 + 102 398 7006 102 399 7405 102 652 15617 102 759 23315 102 629 13698 + 102 683 18968 102 705 19673 102 777 26390 102 790 27966 + 102 839 32018 102 148 609 102 421 8238 102 574 11217 102 730 22556 + 102 336 5160 102 805 30357 102 223 1358 102 252 3042 102 844 33702 + 102 234 1816 102 711 20384 102 792 28758 102 938 41836 102 102 102 + 102 768 25613 102 250 2790 102 627 13069 102 768 24845 102 160 927 + 102 607 11824 102 660 16277 102 667 16944 {} {} {} 102 257 3555 + 102 572 10643 102 716 21100 102 870 35417 102 934 39960 102 133 461 + 102 330 4489 102 443 9111 102 633 14331 102 158 767 102 840 32858 + 102 911 38097 102 938 40898 102 480 9591 102 786 27176 + 102 963 43758 102 113 328 102 309 4159 102 354 5860 102 671 18285 + 102 726 21826 102 959 42795 102 393 6608 102 634 14965 102 247 2540 + 102 412 7817 102 430 8668 102 670 17614 102 794 29552 102 113 215 + 102 224 1582 102 762 24077 102 870 36287 102 256 3298 102 335 4824 + 102 346 5506 102 822 31179} + +do_execsql_test 2.4.1 { + SELECT row_number() OVER win + FROM t3 + WINDOW win AS ( + ORDER BY c, b, a + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES + ) +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 + 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 + 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 + 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 + 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 + 78 79 80} + +do_execsql_test 2.4.2 { + SELECT nth_value(c, 14) OVER win + FROM t3 + WINDOW win AS ( + ORDER BY c, b, a + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES + ) +} {247 247 247 247 247 247 247 247 247 247 247 247 247 + 247 247 247 247 247 247 247 247 247 247 247 247 247 + 247 247 247 247 247 247 247 247 247 247 247 247 247 + 247 247 247 247 247 247 247 247 247 247 247 247 247 + 247 247 247 247 247 247 247 247 247 247 247 247 247 + 247 247 247 247 247 247 247 247 247 247 247 247 247 + 247 247} + +do_execsql_test 2.4.3 { + SELECT min(c) OVER win, max(c) OVER win, sum(c) OVER win FROM t3 + WINDOW win AS ( + ORDER BY c, b, a + ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW EXCLUDE TIES + ) ORDER BY a, b, c; +} {102 223 1358 102 239 2293 102 911 38097 102 934 39960 102 309 4159 + 102 572 10643 102 627 13069 102 870 35417 102 247 2540 102 393 6608 + 102 399 7405 102 412 7817 102 660 16277 102 762 24077 102 633 14331 + 102 705 19673 102 711 20384 102 786 27176 102 792 28758 + 102 840 32858 102 158 767 102 430 8668 102 607 11824 102 759 23315 + 102 346 5506 102 822 31179 102 224 1582 102 256 3298 102 845 34547 + 102 238 2054 102 716 21100 102 794 29552 102 959 42795 102 113 215 + 102 777 26390 102 252 3042 102 629 13698 102 768 25613 102 208 1135 + 102 618 12442 102 667 16944 102 670 17614 102 102 102 102 295 3850 + 102 574 11217 102 726 21826 102 870 36287 102 938 40898 102 148 609 + 102 335 4824 102 480 9591 102 634 14965 102 160 927 102 844 33702 + 102 929 39026 102 938 41836 102 480 10071 102 790 27966 + 102 979 44737 102 133 461 102 330 4489 102 355 6215 102 683 18968 + 102 730 22556 102 963 43758 102 398 7006 102 652 15617 102 250 2790 + 102 421 8238 102 443 9111 102 671 18285 102 805 30357 102 113 328 + 102 234 1816 102 768 24845 102 899 37186 102 257 3555 102 336 5160 + 102 354 5860 102 839 32018} + +#========================================================================== + +do_execsql_test 3.0 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a REAL, b INTEGER); + INSERT INTO t1 VALUES + (5, 10), (10, 20), (13, 26), (13, 26), + (15, 30), (20, 40), (22,80), (30, 90); +} {} + +do_execsql_test 3.1 { + SELECT CAST(a AS INTEGER), sum(b) OVER win FROM t1 WINDOW win AS ( ORDER BY a RANGE BETWEEN 5 PRECEDING AND 5 FOLLOWING ) +} {5 30 10 112 13 102 13 102 15 142 20 150 22 120 30 90} + +do_execsql_test 3.2 { + SELECT CAST(a AS INTEGER), sum(b) OVER win FROM t1 WINDOW win AS ( ORDER BY a RANGE BETWEEN 10 PRECEDING AND 5 PRECEDING ) +} {5 {} 10 10 13 10 13 10 15 30 20 102 22 82 30 120} + +do_execsql_test 3.3 { + SELECT CAST(a AS INTEGER), sum(b) OVER win FROM t1 WINDOW win AS ( ORDER BY a RANGE BETWEEN 2 FOLLOWING AND 3 FOLLOWING ) +} {5 {} 10 52 13 30 13 30 15 {} 20 80 22 {} 30 {}} + +do_execsql_test 3.4 { + SELECT CAST(a AS INTEGER), sum(b) OVER win FROM t1 WINDOW win AS ( ORDER BY a DESC RANGE BETWEEN 5 PRECEDING AND 5 FOLLOWING ) +} {30 90 22 120 20 150 15 142 13 102 13 102 10 112 5 30} + +do_execsql_test 3.5 { + SELECT CAST(a AS INTEGER), sum(b) OVER win FROM t1 WINDOW win AS ( ORDER BY a DESC RANGE BETWEEN 10 PRECEDING AND 5 PRECEDING ) +} {30 {} 22 90 20 90 15 120 13 120 13 120 10 70 5 102} + +do_execsql_test 3.6 { + SELECT CAST(a AS INTEGER), sum(b) OVER win FROM t1 WINDOW win AS ( ORDER BY a DESC RANGE BETWEEN 2 FOLLOWING AND 3 FOLLOWING ) +} {30 {} 22 40 20 {} 15 52 13 20 13 20 10 {} 5 {}} + +do_execsql_test 3.7 { + SELECT CAST(a AS INTEGER), sum(b) OVER win FROM t1 WINDOW win AS ( ORDER BY a RANGE BETWEEN 5.1 PRECEDING AND 5.3 FOLLOWING ) +} {5 30 10 112 13 102 13 102 15 142 20 150 22 120 30 90} + +do_execsql_test 3.8 { + SELECT CAST(a AS INTEGER), sum(b) OVER win FROM t1 WINDOW win AS ( ORDER BY a RANGE BETWEEN 10.2 PRECEDING AND 5.4 PRECEDING ) +} {5 {} 10 {} 13 10 13 10 15 10 20 72 22 82 30 120} + +do_execsql_test 3.9 { + SELECT CAST(a AS INTEGER), sum(b) OVER win FROM t1 WINDOW win AS ( ORDER BY a RANGE BETWEEN 2.6 FOLLOWING AND 3.5 FOLLOWING ) +} {5 {} 10 52 13 {} 13 {} 15 {} 20 {} 22 {} 30 {}} + +do_execsql_test 3.10 { + SELECT CAST(a AS INTEGER), sum(b) OVER win FROM t1 WINDOW win AS ( ORDER BY a DESC RANGE BETWEEN 5.7 PRECEDING AND 5.8 FOLLOWING ) +} {30 90 22 120 20 150 15 142 13 102 13 102 10 112 5 30} + +do_execsql_test 3.11 { + SELECT CAST(a AS INTEGER), sum(b) OVER win FROM t1 WINDOW win AS ( ORDER BY a DESC RANGE BETWEEN UNBOUNDED PRECEDING AND 5.9 PRECEDING ) +} {30 {} 22 90 20 90 15 170 13 210 13 210 10 210 5 292} + +do_execsql_test 3.12 { + SELECT CAST(a AS INTEGER), sum(b) OVER win FROM t1 WINDOW win AS ( ORDER BY a DESC RANGE BETWEEN 2.1 FOLLOWING AND UNBOUNDED FOLLOWING ) +} {30 232 22 112 20 112 15 30 13 30 13 30 10 10 5 {}} + +do_execsql_test 3.13 { + SELECT CAST(a AS INTEGER), sum(b) OVER win FROM t1 WINDOW win AS ( ORDER BY a RANGE 5.1 PRECEDING ) +} {5 10 10 30 13 72 13 72 15 102 20 70 22 120 30 90} + +#========================================================================== + +do_execsql_test 4.0 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a INTEGER, b INTEGER); + INSERT INTO t1 VALUES + (NULL, 1), (NULL, 2), (NULL, 3), (10, 4), (10, 5); +} {} + +do_execsql_test 4.1.1 { + SELECT sum(b) OVER ( + ORDER BY a RANGE BETWEEN 5 PRECEDING AND 10 FOLLOWING + ) FROM t1 ORDER BY 1; +} {6 6 6 9 9} + +do_execsql_test 4.1.2 { + SELECT sum(b) OVER ( + ORDER BY a DESC RANGE BETWEEN 5 PRECEDING AND 10 FOLLOWING + ) FROM t1 ORDER BY 1; +} {6 6 6 9 9} + +do_execsql_test 4.2.1 { + SELECT sum(b) OVER ( + ORDER BY a RANGE BETWEEN 5 FOLLOWING AND 10 FOLLOWING + ) FROM t1 ORDER BY 1 NULLS FIRST; +} {{} {} 6 6 6} + +do_execsql_test 4.2.2 { + SELECT sum(b) OVER ( + ORDER BY a RANGE BETWEEN 5 FOLLOWING AND 10 FOLLOWING + ) FROM t1 ORDER BY 1 NULLS LAST; +} {6 6 6 {} {}} + +do_execsql_test 4.2.3 { + SELECT sum(b) OVER ( + ORDER BY a DESC RANGE BETWEEN 5 FOLLOWING AND 10 FOLLOWING + ) FROM t1 ORDER BY 1 NULLS FIRST; +} {{} {} 6 6 6} + +do_execsql_test 4.2.4 { + SELECT sum(b) OVER ( + ORDER BY a DESC RANGE BETWEEN 5 FOLLOWING AND 10 FOLLOWING + ) FROM t1 ORDER BY 1 NULLS LAST; +} {6 6 6 {} {}} + +do_execsql_test 4.3.1 { + SELECT sum(b) OVER ( + ORDER BY a NULLS FIRST RANGE BETWEEN UNBOUNDED PRECEDING AND 10 FOLLOWING + ) FROM t1 ORDER BY 1 NULLS FIRST; +} {6 6 6 15 15} + +do_execsql_test 4.3.2 { + SELECT sum(b) OVER ( + ORDER BY a NULLS LAST RANGE BETWEEN UNBOUNDED PRECEDING AND 10 FOLLOWING + ) FROM t1 ORDER BY 1 NULLS LAST; +} {9 9 15 15 15} + +do_execsql_test 4.4.1 { + SELECT sum(b) OVER ( + ORDER BY a NULLS FIRST ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING + ) FROM t1 ORDER BY 1 NULLS FIRST; +} {3 6 9 9 12} + +do_execsql_test 4.4.2 { + SELECT sum(b) OVER ( + ORDER BY a NULLS LAST ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING + ) FROM t1 ORDER BY 1 NULLS LAST; +} {5 6 8 9 10} + +do_execsql_test 4.4.3 { + SELECT sum(b) OVER ( + ORDER BY a DESC NULLS LAST ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING + ) FROM t1 ORDER BY 1 NULLS FIRST; +} {5 6 8 9 10} + +do_execsql_test 4.4.4 { + SELECT sum(b) OVER ( + ORDER BY a DESC NULLS LAST ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING + ) FROM t1 ORDER BY 1 NULLS LAST; +} {5 6 8 9 10} + +do_execsql_test 4.5.1 { + SELECT sum(b) OVER ( + ORDER BY a ASC NULLS LAST RANGE BETWEEN UNBOUNDED PRECEDING AND 10 FOLLOWING + ) FROM t1 ORDER BY 1 NULLS LAST; +} {9 9 15 15 15} + +do_execsql_test 4.5.2 { + SELECT sum(b) OVER ( + ORDER BY a DESC NULLS FIRST RANGE + BETWEEN UNBOUNDED PRECEDING AND 10 FOLLOWING + ) FROM t1 ORDER BY 1 NULLS LAST; +} {6 6 6 15 15} + +#========================================================================== + +do_execsql_test 5.0 { + INSERT INTO t3 VALUES + (NULL, 'bb', 355), (NULL, 'cc', 158), (NULL, 'aa', 399), + ('JJ', NULL, 839), ('FF', NULL, 618), ('BB', NULL, 393), + (NULL, 'bb', 629), (NULL, NULL, 667), (NULL, NULL, 870); +} {} + +do_execsql_test 5.1.1.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE NO OTHERS ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 + 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 + 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 + 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 + 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 + 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 + 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 + 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 + 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 + 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 + 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 + 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 + 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 + 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 + 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83} + +do_execsql_test 5.1.1.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE NO OTHERS ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 + 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 + 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 + 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 + 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 + 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 + 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 + 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 + 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 + 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 + 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 + 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 + 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 + 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 + 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1} + +do_execsql_test 5.1.2.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( ORDER BY a NULLS FIRST + RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE NO OTHERS ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {899 113 9 899 113 9 899 113 9 899 113 9 899 113 9 899 113 9 + 899 113 9 899 113 9 899 113 9 899 113 16 899 113 16 899 113 16 + 899 113 16 899 113 16 899 113 16 899 113 16 979 102 44 979 102 44 + 979 102 44 979 102 44 979 102 44 979 102 44 979 102 44 979 102 44 + 979 102 44 979 102 44 979 102 44 979 102 49 979 102 49 979 102 49 + 979 102 49 979 102 49 979 102 56 979 102 56 979 102 56 979 102 56 + 979 102 56 979 102 56 979 102 56 979 102 62 979 102 62 979 102 62 + 979 102 62 979 102 62 979 102 62 979 102 75 979 102 75 979 102 75 + 979 102 75 979 102 75 979 102 75 979 102 75 979 102 75 979 102 75 + 979 102 75 979 102 75 979 102 75 979 102 75 979 102 83 979 102 83 + 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 + 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 + 979 113 25 979 113 25 979 113 25 979 113 25 979 113 25 979 113 25 + 979 113 25 979 113 25 979 113 25 979 113 33 979 113 33 979 113 33 + 979 113 33 979 113 33 979 113 33 979 113 33 979 113 33} + +do_execsql_test 5.1.2.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( ORDER BY a NULLS FIRST + RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE NO OTHERS ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {2947 81 11 2947 81 11 2947 81 11 2947 81 11 2947 81 11 2947 81 11 + 2947 81 11 2947 81 11 2947 81 11 5287 74 10 5287 74 10 5287 74 10 + 5287 74 10 5287 74 10 5287 74 10 5287 74 10 8400 65 9 8400 65 9 + 8400 65 9 8400 65 9 8400 65 9 8400 65 9 8400 65 9 8400 65 9 + 8400 65 9 9664 57 8 9664 57 8 9664 57 8 9664 57 8 9664 57 8 + 9664 57 8 9664 57 8 9664 57 8 10626 46 7 10626 46 7 10626 46 7 + 10626 46 7 10626 46 7 10626 46 7 10626 46 7 10626 46 7 10626 46 7 + 10626 46 7 10626 46 7 12145 41 6 12145 41 6 12145 41 6 12145 41 6 + 12145 41 6 13949 34 5 13949 34 5 13949 34 5 13949 34 5 13949 34 5 + 13949 34 5 13949 34 5 15315 28 4 15315 28 4 15315 28 4 15315 28 4 + 15315 28 4 15315 28 4 18796 15 3 18796 15 3 18796 15 3 18796 15 3 + 18796 15 3 18796 15 3 18796 15 3 18796 15 3 18796 15 3 18796 15 3 + 18796 15 3 18796 15 3 18796 15 3 21105 7 2 21105 7 2 21105 7 2 + 21105 7 2 21105 7 2 21105 7 2 21105 7 2 21105 7 2 23155 1 1 + 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1} + +do_execsql_test 5.1.3.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( PARTITION BY coalesce(a, '') + RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE NO OTHERS ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {777 113 5 777 113 5 777 113 5 777 113 5 777 113 5 805 250 7 + 805 250 7 805 250 7 805 250 7 805 250 7 805 250 7 805 250 7 + 822 158 6 822 158 6 822 158 6 822 158 6 822 158 6 822 158 6 + 840 247 13 840 247 13 840 247 13 840 247 13 840 247 13 840 247 13 + 840 247 13 840 247 13 840 247 13 840 247 13 840 247 13 840 247 13 + 840 247 13 870 158 0 870 158 0 870 158 0 870 158 0 870 158 0 + 870 158 0 899 113 9 899 113 9 899 113 9 899 113 9 899 113 9 + 899 113 9 899 113 9 899 113 9 899 113 9 934 223 8 934 223 8 + 934 223 8 934 223 8 934 223 8 934 223 8 934 223 8 934 223 8 + 938 102 11 938 102 11 938 102 11 938 102 11 938 102 11 938 102 11 + 938 102 11 938 102 11 938 102 11 938 102 11 938 102 11 938 148 8 + 938 148 8 938 148 8 938 148 8 938 148 8 938 148 8 938 148 8 + 938 148 8 959 224 7 959 224 7 959 224 7 959 224 7 959 224 7 + 959 224 7 959 224 7 979 133 9 979 133 9 979 133 9 979 133 9 + 979 133 9 979 133 9 979 133 9 979 133 9 979 133 9} + +do_execsql_test 5.1.3.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( PARTITION BY coalesce(a, '') + RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE NO OTHERS ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {962 1 1 962 1 1 962 1 1 962 1 1 962 1 1 962 1 1 962 1 1 + 962 1 1 962 1 1 962 1 1 962 1 1 1264 1 1 1264 1 1 1264 1 1 + 1264 1 1 1264 1 1 1264 1 1 1264 1 1 1264 1 1 1366 1 1 1366 1 1 + 1366 1 1 1366 1 1 1366 1 1 1366 1 1 1519 1 1 1519 1 1 1519 1 1 + 1519 1 1 1519 1 1 1804 1 1 1804 1 1 1804 1 1 1804 1 1 1804 1 1 + 1804 1 1 1804 1 1 2050 1 1 2050 1 1 2050 1 1 2050 1 1 2050 1 1 + 2050 1 1 2309 1 1 2309 1 1 2309 1 1 2309 1 1 2309 1 1 2309 1 1 + 2309 1 1 2309 1 1 2340 1 1 2340 1 1 2340 1 1 2340 1 1 2340 1 1 + 2340 1 1 2340 1 1 2947 1 1 2947 1 1 2947 1 1 2947 1 1 2947 1 1 + 2947 1 1 2947 1 1 2947 1 1 2947 1 1 3113 1 1 3113 1 1 3113 1 1 + 3113 1 1 3113 1 1 3113 1 1 3113 1 1 3113 1 1 3113 1 1 3481 1 1 + 3481 1 1 3481 1 1 3481 1 1 3481 1 1 3481 1 1 3481 1 1 3481 1 1 + 3481 1 1 3481 1 1 3481 1 1 3481 1 1 3481 1 1} + +do_execsql_test 5.1.4.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( ORDER BY a NULLS FIRST GROUPS 6 PRECEDING EXCLUDE NO OTHERS ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {870 158 0 870 158 0 870 158 0 870 158 0 870 158 0 870 158 0 + 934 158 8 934 158 8 934 158 8 934 158 8 934 158 8 934 158 8 + 934 158 8 934 158 8 934 158 21 934 158 21 934 158 21 934 158 21 + 934 158 21 934 158 21 934 158 21 934 158 21 934 158 21 934 158 21 + 934 158 21 934 158 21 934 158 21 934 158 27 934 158 27 934 158 27 + 934 158 27 934 158 27 934 158 27 959 102 50 959 102 50 959 102 50 + 959 102 50 959 102 50 959 102 50 959 102 50 959 102 50 959 102 50 + 959 102 50 959 102 50 959 102 58 959 102 58 959 102 58 959 102 58 + 959 102 58 959 102 58 959 102 58 959 102 58 959 113 39 959 113 39 + 959 113 39 959 113 39 959 113 39 959 158 34 959 158 34 959 158 34 + 959 158 34 959 158 34 959 158 34 959 158 34 979 102 53 979 102 53 + 979 102 53 979 102 53 979 102 53 979 102 53 979 102 53 979 102 56 + 979 102 56 979 102 56 979 102 56 979 102 56 979 102 56 979 102 56 + 979 102 56 979 102 56 979 102 59 979 102 59 979 102 59 979 102 59 + 979 102 59 979 102 59 979 102 59 979 102 59 979 102 59} + +do_execsql_test 5.1.4.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( ORDER BY a NULLS FIRST GROUPS 6 PRECEDING EXCLUDE NO OTHERS ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {2050 1 1 2050 1 1 2050 1 1 2050 1 1 2050 1 1 2050 1 1 4359 7 2 + 4359 7 2 4359 7 2 4359 7 2 4359 7 2 4359 7 2 4359 7 2 4359 7 2 + 7840 15 3 7840 15 3 7840 15 3 7840 15 3 7840 15 3 7840 15 3 + 7840 15 3 7840 15 3 7840 15 3 7840 15 3 7840 15 3 7840 15 3 + 7840 15 3 9206 28 4 9206 28 4 9206 28 4 9206 28 4 9206 28 4 + 9206 28 4 11010 34 5 11010 34 5 11010 34 5 11010 34 5 11010 34 5 + 11010 34 5 11010 34 5 12368 74 10 12368 74 10 12368 74 10 + 12368 74 10 12368 74 10 12368 74 10 12368 74 10 12529 41 6 + 12529 41 6 12529 41 6 12529 41 6 12529 41 6 12705 57 8 12705 57 8 + 12705 57 8 12705 57 8 12705 57 8 12705 57 8 12705 57 8 12705 57 8 + 13491 46 7 13491 46 7 13491 46 7 13491 46 7 13491 46 7 13491 46 7 + 13491 46 7 13491 46 7 13491 46 7 13491 46 7 13491 46 7 13509 65 9 + 13509 65 9 13509 65 9 13509 65 9 13509 65 9 13509 65 9 13509 65 9 + 13509 65 9 13509 65 9 13949 81 11 13949 81 11 13949 81 11 + 13949 81 11 13949 81 11 13949 81 11 13949 81 11 13949 81 11 + 13949 81 11} + +do_execsql_test 5.1.5.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( ORDER BY c NULLS FIRST RANGE BETWEEN 6 PRECEDING AND 7 FOLLOWING EXCLUDE NO OTHERS ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {102 102 1 113 113 2 113 113 2 133 133 1 148 148 1 160 158 2 + 160 158 2 160 158 2 208 208 1 224 223 2 224 223 2 239 234 3 + 239 234 3 239 234 3 252 247 3 257 247 5 257 247 5 257 250 4 + 257 252 3 295 295 1 309 309 1 336 330 3 336 330 3 336 330 3 + 346 346 1 355 354 2 355 354 2 355 354 2 399 393 4 399 393 4 + 399 393 4 399 393 4 399 393 4 412 412 1 421 421 1 430 430 1 + 443 443 1 480 480 2 480 480 2 574 572 2 574 572 2 607 607 1 + 618 618 2 618 618 2 634 627 4 634 627 4 634 627 4 634 627 4 + 634 629 3 652 652 1 667 660 2 671 667 3 671 667 3 671 667 3 + 671 667 3 683 683 1 711 705 2 716 705 3 716 711 2 730 726 2 + 730 726 2 762 759 2 768 759 4 768 762 3 768 762 3 777 777 1 + 792 786 3 794 786 4 794 786 4 794 790 3 805 805 1 822 822 1 + 845 839 5 845 839 5 845 839 5 845 839 5 845 839 5 870 870 2 + 870 870 2 870 870 2 899 899 1 911 911 1 934 929 2 938 929 4 + 938 934 3 938 934 3 963 959 2 963 959 2 979 979 1} + +do_execsql_test 5.1.5.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( ORDER BY c NULLS FIRST RANGE BETWEEN 6 PRECEDING AND 7 FOLLOWING EXCLUDE NO OTHERS ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {{} 1 1 {} 5 4 {} 6 5 {} 6 5 {} 8 6 {} 9 7 {} 25 23 {} 34 29 + {} 36 31 {} 38 33 {} 38 33 {} 40 34 {} 41 35 {} 43 37 {} 43 37 + {} 50 42 {} 60 51 {} 61 52 {} 64 55 {} 64 55 {} 67 57 {} 68 58 + {} 69 59 {} 70 60 {} 72 62 {} 78 67 {} 78 67 {} 78 67 {} 85 72 + {} 85 72 133 4 3 223 10 8 223 11 9 226 2 2 226 2 2 239 12 10 + 239 13 11 239 14 12 247 15 13 257 18 16 257 19 17 295 20 18 + 309 21 19 335 22 20 335 23 21 335 24 22 421 35 30 443 37 32 + 504 16 14 504 17 15 607 42 36 683 56 47 710 26 24 710 27 25 + 710 27 25 711 59 50 759 62 53 759 63 54 777 66 56 805 71 61 + 899 81 68 911 82 69 929 83 70 929 84 71 979 89 75 1334 51 43 + 1416 57 48 1416 58 49 1584 29 26 1584 29 26 1584 31 27 1584 32 28 + 1584 32 28 1891 49 41 1922 87 73 1922 88 74 2005 52 44 2005 52 44 + 2005 54 45 2005 55 46 2518 45 38 2518 46 39 2518 46 39 2518 48 40 + 2523 73 63 2523 73 63 2523 75 64 2523 76 65 2523 77 66} + +do_execsql_test 5.1.6.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( ORDER BY c NULLS FIRST RANGE BETWEEN 0 PRECEDING AND 0 FOLLOWING EXCLUDE NO OTHERS ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {102 102 1 113 113 2 113 113 2 133 133 1 148 148 1 158 158 1 + 158 158 1 160 160 1 208 208 1 223 223 1 224 224 1 234 234 1 + 238 238 1 239 239 1 247 247 1 250 250 1 252 252 1 256 256 1 + 257 257 1 295 295 1 309 309 1 330 330 1 335 335 1 336 336 1 + 346 346 1 354 354 1 355 355 1 355 355 1 393 393 2 393 393 2 + 398 398 1 399 399 1 399 399 1 412 412 1 421 421 1 430 430 1 + 443 443 1 480 480 2 480 480 2 572 572 1 574 574 1 607 607 1 + 618 618 2 618 618 2 627 627 1 629 629 1 629 629 1 633 633 1 + 634 634 1 652 652 1 660 660 1 667 667 1 667 667 1 670 670 1 + 671 671 1 683 683 1 705 705 1 711 711 1 716 716 1 726 726 1 + 730 730 1 759 759 1 762 762 1 768 768 2 768 768 2 777 777 1 + 786 786 1 790 790 1 792 792 1 794 794 1 805 805 1 822 822 1 + 839 839 2 839 839 2 840 840 1 844 844 1 845 845 1 870 870 2 + 870 870 2 870 870 2 899 899 1 911 911 1 929 929 1 934 934 1 + 938 938 2 938 938 2 959 959 1 963 963 1 979 979 1} + +do_execsql_test 5.1.6.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( ORDER BY c NULLS FIRST RANGE BETWEEN 0 PRECEDING AND 0 FOLLOWING EXCLUDE NO OTHERS ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {{} 1 1 {} 5 4 {} 6 5 {} 6 5 {} 8 6 {} 9 7 {} 11 9 {} 12 10 + {} 13 11 {} 16 14 {} 17 15 {} 18 16 {} 22 20 {} 24 22 {} 25 23 + {} 26 24 {} 31 27 {} 34 29 {} 36 31 {} 38 33 {} 38 33 {} 40 34 + {} 41 35 {} 43 37 {} 43 37 {} 49 41 {} 50 42 {} 51 43 {} 54 45 + {} 59 50 {} 60 51 {} 61 52 {} 63 54 {} 64 55 {} 64 55 {} 67 57 + {} 68 58 {} 69 59 {} 70 60 {} 72 62 {} 75 64 {} 76 65 {} 78 67 + {} 78 67 {} 78 67 {} 84 71 {} 85 72 {} 85 72 133 4 3 223 10 8 + 226 2 2 226 2 2 239 14 12 247 15 13 257 19 17 295 20 18 + 309 21 19 335 23 21 421 35 30 443 37 32 607 42 36 627 45 38 + 633 48 40 671 55 46 683 56 47 705 57 48 710 27 25 710 27 25 + 711 58 49 759 62 53 777 66 56 786 29 26 786 29 26 798 32 28 + 798 32 28 805 71 61 845 77 66 899 81 68 911 82 69 929 83 70 + 959 87 73 963 88 74 979 89 75 1258 46 39 1258 46 39 1334 52 44 + 1334 52 44 1678 73 63 1678 73 63} + +do_execsql_test 5.1.7.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( ORDER BY c NULLS FIRST, b NULLS FIRST, a NULLS FIRST + ROWS BETWEEN 6 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE NO OTHERS ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 + 979 102 83 979 113 81 979 113 82 979 133 80 979 148 79 979 158 77 + 979 158 78 979 160 77 979 208 76 979 223 75 979 224 74 979 234 73 + 979 238 72 979 239 71 979 247 70 979 250 69 979 252 68 979 256 67 + 979 257 66 979 295 65 979 309 64 979 330 63 979 335 62 979 336 61 + 979 346 60 979 354 59 979 355 58 979 355 58 979 393 56 979 393 57 + 979 398 55 979 399 54 979 399 54 979 412 53 979 421 52 979 430 51 + 979 443 50 979 480 48 979 480 49 979 572 47 979 574 46 979 607 45 + 979 618 43 979 618 44 979 627 42 979 629 41 979 629 41 979 633 40 + 979 634 39 979 652 38 979 660 37 979 667 36 979 667 36 979 670 35 + 979 671 34 979 683 33 979 705 32 979 711 31 979 716 30 979 726 29 + 979 730 28 979 759 27 979 762 26 979 768 24 979 768 25 979 777 23 + 979 786 22 979 790 21 979 792 20 979 794 19 979 805 18 979 822 17 + 979 839 15 979 839 16 979 840 14 979 844 13 979 845 12 979 870 10 + 979 870 11 979 870 11 979 899 9 979 911 8 979 929 7} + +do_execsql_test 5.1.7.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( ORDER BY c NULLS FIRST, b NULLS FIRST, a NULLS FIRST + ROWS BETWEEN 6 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE NO OTHERS ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {3830 89 89 4741 88 88 5640 84 84 5640 85 85 5640 86 86 5640 87 87 + 6485 81 81 6485 82 82 6485 83 83 7324 80 80 8163 78 78 8163 79 79 + 8968 73 73 8968 74 74 8968 75 75 8968 76 76 8968 77 77 9745 69 69 + 9745 70 70 9745 71 71 9745 72 72 10504 65 65 10504 66 66 + 10504 67 67 10504 68 68 11215 64 64 11920 63 63 12603 62 62 + 13274 60 60 13274 61 61 13941 59 59 14608 55 55 14608 56 56 + 14608 57 57 14608 58 58 15241 54 54 15870 53 53 16499 52 52 + 17126 49 49 17126 50 50 17126 51 51 17733 44 44 17733 45 45 + 17733 46 46 17733 47 47 17733 48 48 18176 42 42 18176 43 43 + 18597 40 40 18597 41 41 18996 39 39 19395 37 37 19395 38 38 + 19788 36 36 20181 35 35 20536 34 34 20891 30 30 20891 31 31 + 20891 32 32 20891 33 33 21226 28 28 21226 29 29 21535 27 27 + 21830 26 26 22087 22 22 22087 23 23 22087 24 24 22087 25 25 + 22334 21 21 22573 17 17 22573 18 18 22573 19 19 22573 20 20 + 22796 11 11 22796 12 12 22796 13 13 22796 14 14 22796 15 15 + 22796 16 16 22929 10 10 23042 9 9 23155 1 1 23155 2 2 23155 3 3 + 23155 4 4 23155 5 5 23155 6 6 23155 7 7 23155 8 8} + +do_execsql_test 5.1.8.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE NO OTHERS ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 + 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 + 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 + 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 + 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 + 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 + 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 + 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 + 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 + 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 + 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 + 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 + 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 + 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 + 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83} + +do_execsql_test 5.1.8.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE NO OTHERS ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 + 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 + 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 + 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 + 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 + 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 + 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 + 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 + 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 + 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 + 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 + 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 + 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 + 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 + 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1} + +do_execsql_test 5.1.9.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( ORDER BY a NULLS LAST + RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE NO OTHERS ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {870 158 0 870 158 0 870 158 0 870 158 0 870 158 0 870 158 0 + 899 113 9 899 113 9 899 113 9 899 113 9 899 113 9 899 113 9 + 899 113 9 899 113 9 899 113 9 899 113 16 899 113 16 899 113 16 + 899 113 16 899 113 16 899 113 16 899 113 16 979 102 44 979 102 44 + 979 102 44 979 102 44 979 102 44 979 102 44 979 102 44 979 102 44 + 979 102 44 979 102 44 979 102 44 979 102 49 979 102 49 979 102 49 + 979 102 49 979 102 49 979 102 56 979 102 56 979 102 56 979 102 56 + 979 102 56 979 102 56 979 102 56 979 102 62 979 102 62 979 102 62 + 979 102 62 979 102 62 979 102 62 979 102 75 979 102 75 979 102 75 + 979 102 75 979 102 75 979 102 75 979 102 75 979 102 75 979 102 75 + 979 102 75 979 102 75 979 102 75 979 102 75 979 102 83 979 102 83 + 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 + 979 113 25 979 113 25 979 113 25 979 113 25 979 113 25 979 113 25 + 979 113 25 979 113 25 979 113 25 979 113 33 979 113 33 979 113 33 + 979 113 33 979 113 33 979 113 33 979 113 33 979 113 33} + +do_execsql_test 5.1.9.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( ORDER BY a NULLS LAST + RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE NO OTHERS ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {2050 84 11 2050 84 11 2050 84 11 2050 84 11 2050 84 11 2050 84 11 + 4997 75 10 4997 75 10 4997 75 10 4997 75 10 4997 75 10 4997 75 10 + 4997 75 10 4997 75 10 4997 75 10 7337 68 9 7337 68 9 7337 68 9 + 7337 68 9 7337 68 9 7337 68 9 7337 68 9 10450 59 8 10450 59 8 + 10450 59 8 10450 59 8 10450 59 8 10450 59 8 10450 59 8 10450 59 8 + 10450 59 8 11714 51 7 11714 51 7 11714 51 7 11714 51 7 11714 51 7 + 11714 51 7 11714 51 7 11714 51 7 12676 40 6 12676 40 6 12676 40 6 + 12676 40 6 12676 40 6 12676 40 6 12676 40 6 12676 40 6 12676 40 6 + 12676 40 6 12676 40 6 14195 35 5 14195 35 5 14195 35 5 14195 35 5 + 14195 35 5 15999 28 4 15999 28 4 15999 28 4 15999 28 4 15999 28 4 + 15999 28 4 15999 28 4 17365 22 3 17365 22 3 17365 22 3 17365 22 3 + 17365 22 3 17365 22 3 20846 9 2 20846 9 2 20846 9 2 20846 9 2 + 20846 9 2 20846 9 2 20846 9 2 20846 9 2 20846 9 2 20846 9 2 + 20846 9 2 20846 9 2 20846 9 2 23155 1 1 23155 1 1 23155 1 1 + 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1} + +do_execsql_test 5.1.10.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( PARTITION BY coalesce(a, '') + RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE NO OTHERS ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {777 113 5 777 113 5 777 113 5 777 113 5 777 113 5 805 250 7 + 805 250 7 805 250 7 805 250 7 805 250 7 805 250 7 805 250 7 + 822 158 6 822 158 6 822 158 6 822 158 6 822 158 6 822 158 6 + 840 247 13 840 247 13 840 247 13 840 247 13 840 247 13 840 247 13 + 840 247 13 840 247 13 840 247 13 840 247 13 840 247 13 840 247 13 + 840 247 13 870 158 0 870 158 0 870 158 0 870 158 0 870 158 0 + 870 158 0 899 113 9 899 113 9 899 113 9 899 113 9 899 113 9 + 899 113 9 899 113 9 899 113 9 899 113 9 934 223 8 934 223 8 + 934 223 8 934 223 8 934 223 8 934 223 8 934 223 8 934 223 8 + 938 102 11 938 102 11 938 102 11 938 102 11 938 102 11 938 102 11 + 938 102 11 938 102 11 938 102 11 938 102 11 938 102 11 938 148 8 + 938 148 8 938 148 8 938 148 8 938 148 8 938 148 8 938 148 8 + 938 148 8 959 224 7 959 224 7 959 224 7 959 224 7 959 224 7 + 959 224 7 959 224 7 979 133 9 979 133 9 979 133 9 979 133 9 + 979 133 9 979 133 9 979 133 9 979 133 9 979 133 9} + +do_execsql_test 5.1.10.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( PARTITION BY coalesce(a, '') + RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE NO OTHERS ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {962 1 1 962 1 1 962 1 1 962 1 1 962 1 1 962 1 1 962 1 1 + 962 1 1 962 1 1 962 1 1 962 1 1 1264 1 1 1264 1 1 1264 1 1 + 1264 1 1 1264 1 1 1264 1 1 1264 1 1 1264 1 1 1366 1 1 1366 1 1 + 1366 1 1 1366 1 1 1366 1 1 1366 1 1 1519 1 1 1519 1 1 1519 1 1 + 1519 1 1 1519 1 1 1804 1 1 1804 1 1 1804 1 1 1804 1 1 1804 1 1 + 1804 1 1 1804 1 1 2050 1 1 2050 1 1 2050 1 1 2050 1 1 2050 1 1 + 2050 1 1 2309 1 1 2309 1 1 2309 1 1 2309 1 1 2309 1 1 2309 1 1 + 2309 1 1 2309 1 1 2340 1 1 2340 1 1 2340 1 1 2340 1 1 2340 1 1 + 2340 1 1 2340 1 1 2947 1 1 2947 1 1 2947 1 1 2947 1 1 2947 1 1 + 2947 1 1 2947 1 1 2947 1 1 2947 1 1 3113 1 1 3113 1 1 3113 1 1 + 3113 1 1 3113 1 1 3113 1 1 3113 1 1 3113 1 1 3113 1 1 3481 1 1 + 3481 1 1 3481 1 1 3481 1 1 3481 1 1 3481 1 1 3481 1 1 3481 1 1 + 3481 1 1 3481 1 1 3481 1 1 3481 1 1 3481 1 1} + +do_execsql_test 5.1.11.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( ORDER BY a NULLS LAST GROUPS 6 PRECEDING EXCLUDE NO OTHERS ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {934 158 27 934 158 27 934 158 27 934 158 27 934 158 27 934 158 27 + 934 223 8 934 223 8 934 223 8 934 223 8 934 223 8 934 223 8 + 934 223 8 934 223 8 934 223 21 934 223 21 934 223 21 934 223 21 + 934 223 21 934 223 21 934 223 21 934 223 21 934 223 21 934 223 21 + 934 223 21 934 223 21 934 223 21 959 102 50 959 102 50 959 102 50 + 959 102 50 959 102 50 959 102 50 959 102 50 959 102 50 959 102 50 + 959 102 50 959 102 50 959 102 58 959 102 58 959 102 58 959 102 58 + 959 102 58 959 102 58 959 102 58 959 102 58 959 113 39 959 113 39 + 959 113 39 959 113 39 959 113 39 959 158 34 959 158 34 959 158 34 + 959 158 34 959 158 34 959 158 34 959 158 34 979 102 49 979 102 49 + 979 102 49 979 102 49 979 102 49 979 102 49 979 102 53 979 102 53 + 979 102 53 979 102 53 979 102 53 979 102 53 979 102 53 979 102 56 + 979 102 56 979 102 56 979 102 56 979 102 56 979 102 56 979 102 56 + 979 102 56 979 102 56 979 102 59 979 102 59 979 102 59 979 102 59 + 979 102 59 979 102 59 979 102 59 979 102 59 979 102 59} + +do_execsql_test 5.1.11.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( ORDER BY a NULLS LAST GROUPS 6 PRECEDING EXCLUDE NO OTHERS ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {2309 1 1 2309 1 1 2309 1 1 2309 1 1 2309 1 1 2309 1 1 2309 1 1 + 2309 1 1 5790 9 2 5790 9 2 5790 9 2 5790 9 2 5790 9 2 5790 9 2 + 5790 9 2 5790 9 2 5790 9 2 5790 9 2 5790 9 2 5790 9 2 5790 9 2 + 7156 22 3 7156 22 3 7156 22 3 7156 22 3 7156 22 3 7156 22 3 + 8960 28 4 8960 28 4 8960 28 4 8960 28 4 8960 28 4 8960 28 4 + 8960 28 4 10479 35 5 10479 35 5 10479 35 5 10479 35 5 10479 35 5 + 11441 40 6 11441 40 6 11441 40 6 11441 40 6 11441 40 6 11441 40 6 + 11441 40 6 11441 40 6 11441 40 6 11441 40 6 11441 40 6 12368 68 9 + 12368 68 9 12368 68 9 12368 68 9 12368 68 9 12368 68 9 12368 68 9 + 12705 51 7 12705 51 7 12705 51 7 12705 51 7 12705 51 7 12705 51 7 + 12705 51 7 12705 51 7 13509 59 8 13509 59 8 13509 59 8 13509 59 8 + 13509 59 8 13509 59 8 13509 59 8 13509 59 8 13509 59 8 + 13949 75 10 13949 75 10 13949 75 10 13949 75 10 13949 75 10 + 13949 75 10 13949 75 10 13949 75 10 13949 75 10 14195 84 11 + 14195 84 11 14195 84 11 14195 84 11 14195 84 11 14195 84 11} + +do_execsql_test 5.1.12.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( ORDER BY c NULLS LAST RANGE BETWEEN 6 PRECEDING AND 7 FOLLOWING EXCLUDE NO OTHERS ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {102 102 1 113 113 2 113 113 2 133 133 1 148 148 1 160 158 2 + 160 158 2 160 158 2 208 208 1 224 223 2 224 223 2 239 234 3 + 239 234 3 239 234 3 252 247 3 257 247 5 257 247 5 257 250 4 + 257 252 3 295 295 1 309 309 1 336 330 3 336 330 3 336 330 3 + 346 346 1 355 354 2 355 354 2 355 354 2 399 393 4 399 393 4 + 399 393 4 399 393 4 399 393 4 412 412 1 421 421 1 430 430 1 + 443 443 1 480 480 2 480 480 2 574 572 2 574 572 2 607 607 1 + 618 618 2 618 618 2 634 627 4 634 627 4 634 627 4 634 627 4 + 634 629 3 652 652 1 667 660 2 671 667 3 671 667 3 671 667 3 + 671 667 3 683 683 1 711 705 2 716 705 3 716 711 2 730 726 2 + 730 726 2 762 759 2 768 759 4 768 762 3 768 762 3 777 777 1 + 792 786 3 794 786 4 794 786 4 794 790 3 805 805 1 822 822 1 + 845 839 5 845 839 5 845 839 5 845 839 5 845 839 5 870 870 2 + 870 870 2 870 870 2 899 899 1 911 911 1 934 929 2 938 929 4 + 938 934 3 938 934 3 963 959 2 963 959 2 979 979 1} + +do_execsql_test 5.1.12.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( ORDER BY c NULLS LAST RANGE BETWEEN 6 PRECEDING AND 7 FOLLOWING EXCLUDE NO OTHERS ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {{} 1 1 {} 5 4 {} 6 5 {} 6 5 {} 8 6 {} 9 7 {} 25 23 {} 34 29 + {} 36 31 {} 38 33 {} 38 33 {} 40 34 {} 41 35 {} 43 37 {} 43 37 + {} 50 42 {} 60 51 {} 61 52 {} 64 55 {} 64 55 {} 67 57 {} 68 58 + {} 69 59 {} 70 60 {} 72 62 {} 78 67 {} 78 67 {} 78 67 {} 85 72 + {} 85 72 133 4 3 223 10 8 223 11 9 226 2 2 226 2 2 239 12 10 + 239 13 11 239 14 12 247 15 13 257 18 16 257 19 17 295 20 18 + 309 21 19 335 22 20 335 23 21 335 24 22 421 35 30 443 37 32 + 504 16 14 504 17 15 607 42 36 683 56 47 710 26 24 710 27 25 + 710 27 25 711 59 50 759 62 53 759 63 54 777 66 56 805 71 61 + 899 81 68 911 82 69 929 83 70 929 84 71 979 89 75 1334 51 43 + 1416 57 48 1416 58 49 1584 29 26 1584 29 26 1584 31 27 1584 32 28 + 1584 32 28 1891 49 41 1922 87 73 1922 88 74 2005 52 44 2005 52 44 + 2005 54 45 2005 55 46 2518 45 38 2518 46 39 2518 46 39 2518 48 40 + 2523 73 63 2523 73 63 2523 75 64 2523 76 65 2523 77 66} + +do_execsql_test 5.1.13.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( ORDER BY c NULLS LAST RANGE BETWEEN 0 PRECEDING AND 0 FOLLOWING EXCLUDE NO OTHERS ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {102 102 1 113 113 2 113 113 2 133 133 1 148 148 1 158 158 1 + 158 158 1 160 160 1 208 208 1 223 223 1 224 224 1 234 234 1 + 238 238 1 239 239 1 247 247 1 250 250 1 252 252 1 256 256 1 + 257 257 1 295 295 1 309 309 1 330 330 1 335 335 1 336 336 1 + 346 346 1 354 354 1 355 355 1 355 355 1 393 393 2 393 393 2 + 398 398 1 399 399 1 399 399 1 412 412 1 421 421 1 430 430 1 + 443 443 1 480 480 2 480 480 2 572 572 1 574 574 1 607 607 1 + 618 618 2 618 618 2 627 627 1 629 629 1 629 629 1 633 633 1 + 634 634 1 652 652 1 660 660 1 667 667 1 667 667 1 670 670 1 + 671 671 1 683 683 1 705 705 1 711 711 1 716 716 1 726 726 1 + 730 730 1 759 759 1 762 762 1 768 768 2 768 768 2 777 777 1 + 786 786 1 790 790 1 792 792 1 794 794 1 805 805 1 822 822 1 + 839 839 2 839 839 2 840 840 1 844 844 1 845 845 1 870 870 2 + 870 870 2 870 870 2 899 899 1 911 911 1 929 929 1 934 934 1 + 938 938 2 938 938 2 959 959 1 963 963 1 979 979 1} + +do_execsql_test 5.1.13.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( ORDER BY c NULLS LAST RANGE BETWEEN 0 PRECEDING AND 0 FOLLOWING EXCLUDE NO OTHERS ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {{} 1 1 {} 5 4 {} 6 5 {} 6 5 {} 8 6 {} 9 7 {} 11 9 {} 12 10 + {} 13 11 {} 16 14 {} 17 15 {} 18 16 {} 22 20 {} 24 22 {} 25 23 + {} 26 24 {} 31 27 {} 34 29 {} 36 31 {} 38 33 {} 38 33 {} 40 34 + {} 41 35 {} 43 37 {} 43 37 {} 49 41 {} 50 42 {} 51 43 {} 54 45 + {} 59 50 {} 60 51 {} 61 52 {} 63 54 {} 64 55 {} 64 55 {} 67 57 + {} 68 58 {} 69 59 {} 70 60 {} 72 62 {} 75 64 {} 76 65 {} 78 67 + {} 78 67 {} 78 67 {} 84 71 {} 85 72 {} 85 72 133 4 3 223 10 8 + 226 2 2 226 2 2 239 14 12 247 15 13 257 19 17 295 20 18 + 309 21 19 335 23 21 421 35 30 443 37 32 607 42 36 627 45 38 + 633 48 40 671 55 46 683 56 47 705 57 48 710 27 25 710 27 25 + 711 58 49 759 62 53 777 66 56 786 29 26 786 29 26 798 32 28 + 798 32 28 805 71 61 845 77 66 899 81 68 911 82 69 929 83 70 + 959 87 73 963 88 74 979 89 75 1258 46 39 1258 46 39 1334 52 44 + 1334 52 44 1678 73 63 1678 73 63} + +do_execsql_test 5.1.14.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( ORDER BY c NULLS LAST, b NULLS LAST, a NULLS LAST + ROWS BETWEEN 6 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE NO OTHERS ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 + 979 102 83 979 113 81 979 113 82 979 133 80 979 148 79 979 158 77 + 979 158 78 979 160 77 979 208 76 979 223 75 979 224 74 979 234 73 + 979 238 72 979 239 71 979 247 70 979 250 69 979 252 68 979 256 67 + 979 257 66 979 295 65 979 309 64 979 330 63 979 335 62 979 336 61 + 979 346 60 979 354 59 979 355 57 979 355 58 979 393 56 979 393 57 + 979 398 55 979 399 53 979 399 54 979 412 53 979 421 52 979 430 51 + 979 443 50 979 480 48 979 480 49 979 572 47 979 574 46 979 607 45 + 979 618 43 979 618 44 979 627 42 979 629 40 979 629 41 979 633 40 + 979 634 39 979 652 38 979 660 37 979 667 35 979 667 36 979 670 35 + 979 671 34 979 683 33 979 705 32 979 711 31 979 716 30 979 726 29 + 979 730 28 979 759 27 979 762 26 979 768 24 979 768 25 979 777 23 + 979 786 22 979 790 21 979 792 20 979 794 19 979 805 18 979 822 17 + 979 839 15 979 839 16 979 840 14 979 844 13 979 845 12 979 870 9 + 979 870 10 979 870 11 979 899 9 979 911 8 979 929 7} + +do_execsql_test 5.1.14.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( ORDER BY c NULLS LAST, b NULLS LAST, a NULLS LAST + ROWS BETWEEN 6 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE NO OTHERS ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {3830 89 89 4741 88 88 5640 84 84 5640 85 85 5640 86 86 5640 87 87 + 6485 81 81 6485 82 82 6485 83 83 7324 80 80 8163 78 78 8163 79 79 + 8968 73 73 8968 74 74 8968 75 75 8968 76 76 8968 77 77 9745 69 69 + 9745 70 70 9745 71 71 9745 72 72 10504 65 65 10504 66 66 + 10504 67 67 10504 68 68 11215 64 64 11920 63 63 12603 62 62 + 13274 60 60 13274 61 61 13941 59 59 14608 55 55 14608 56 56 + 14608 57 57 14608 58 58 15241 54 54 15870 53 53 16499 52 52 + 17126 49 49 17126 50 50 17126 51 51 17733 44 44 17733 45 45 + 17733 46 46 17733 47 47 17733 48 48 18176 42 42 18176 43 43 + 18597 40 40 18597 41 41 18996 39 39 19395 37 37 19395 38 38 + 19788 36 36 20181 35 35 20536 34 34 20891 30 30 20891 31 31 + 20891 32 32 20891 33 33 21226 28 28 21226 29 29 21535 27 27 + 21830 26 26 22087 22 22 22087 23 23 22087 24 24 22087 25 25 + 22334 21 21 22573 17 17 22573 18 18 22573 19 19 22573 20 20 + 22796 11 11 22796 12 12 22796 13 13 22796 14 14 22796 15 15 + 22796 16 16 22929 10 10 23042 9 9 23155 1 1 23155 2 2 23155 3 3 + 23155 4 4 23155 5 5 23155 6 6 23155 7 7 23155 8 8} + +do_execsql_test 5.2.1.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {963 102 82 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 + 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 + 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 + 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 + 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 + 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 + 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 + 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 + 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 + 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 + 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 + 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 + 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 + 979 102 82 979 102 82 979 102 82 979 102 82 979 102 83 979 102 83 + 979 102 83 979 102 83 979 102 83 979 102 83 979 113 82} + +do_execsql_test 5.2.1.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {22176 1 1 22192 1 1 22196 1 1 22226 1 1 22244 1 1 22256 1 1 + 22310 1 1 22316 1 1 22316 1 1 22350 1 1 22378 1 1 22396 1 1 + 22444 1 1 22450 1 1 22472 1 1 22484 1 1 22488 1 1 22488 1 1 + 22522 1 1 22526 1 1 22526 1 1 22528 1 1 22548 1 1 22712 1 1 + 22734 1 1 22756 1 1 22756 1 1 22762 1 1 22762 1 1 22800 1 1 + 22800 1 1 22820 1 1 22846 1 1 22860 1 1 22898 1 1 22908 1 1 + 22916 1 1 22932 1 1 23022 1 1 23042 1 1 23042 1 1 23155 1 1 + 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 + 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 + 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 + 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 + 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 + 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 + 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 + 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1} + +do_execsql_test 5.2.2.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( ORDER BY a NULLS FIRST + RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {839 113 8 899 113 8 899 113 8 899 113 8 899 113 8 899 113 8 + 899 113 8 899 113 8 899 113 15 899 113 15 899 113 15 899 113 15 + 899 113 15 899 113 15 899 113 15 899 234 8 963 113 24 979 102 43 + 979 102 43 979 102 43 979 102 43 979 102 43 979 102 43 979 102 43 + 979 102 43 979 102 43 979 102 43 979 102 48 979 102 48 979 102 48 + 979 102 48 979 102 48 979 102 55 979 102 55 979 102 55 979 102 55 + 979 102 55 979 102 55 979 102 55 979 102 61 979 102 61 979 102 61 + 979 102 61 979 102 61 979 102 61 979 102 74 979 102 74 979 102 74 + 979 102 74 979 102 74 979 102 74 979 102 74 979 102 74 979 102 74 + 979 102 74 979 102 74 979 102 74 979 102 74 979 102 82 979 102 82 + 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 + 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 + 979 113 24 979 113 24 979 113 24 979 113 24 979 113 24 979 113 24 + 979 113 24 979 113 24 979 113 32 979 113 32 979 113 32 979 113 32 + 979 113 32 979 113 32 979 113 32 979 113 32 979 113 43} + +do_execsql_test 5.2.2.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( ORDER BY a NULLS FIRST + RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {2048 81 11 2108 81 11 2108 81 11 2690 81 11 2834 81 11 2947 81 11 + 2947 81 11 2947 81 11 2947 81 11 4482 74 10 4616 74 10 4844 74 10 + 4866 74 10 5287 74 10 5287 74 10 5287 74 10 7421 65 9 7437 65 9 + 7717 65 9 8045 65 9 8267 65 9 8400 65 9 8400 65 9 8400 65 9 + 8400 65 9 8735 57 8 9329 57 8 9664 57 8 9664 57 8 9664 57 8 + 9664 57 8 9664 57 8 9664 57 8 9959 46 7 10331 46 7 10626 46 7 + 10626 46 7 10626 46 7 10626 46 7 10626 46 7 10626 46 7 10626 46 7 + 10626 46 7 10626 46 7 11368 41 6 11516 41 6 12032 41 6 12145 41 6 + 12145 41 6 12990 34 5 13104 34 5 13949 34 5 13949 34 5 13949 34 5 + 13949 34 5 13949 34 5 14556 28 4 14708 28 4 15315 28 4 15315 28 4 + 15315 28 4 15315 28 4 18085 15 3 18091 15 3 18163 15 3 18397 15 3 + 18403 15 3 18403 15 3 18549 15 3 18796 15 3 18796 15 3 18796 15 3 + 18796 15 3 18796 15 3 18796 15 3 20194 7 2 20478 7 2 20796 7 2 + 20866 7 2 20882 7 2 21105 7 2 21105 7 2 21105 7 2 22488 1 1 + 22526 1 1 22756 1 1 22800 1 1 23155 1 1 23155 1 1} + +do_execsql_test 5.2.3.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( PARTITION BY coalesce(a, '') + RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {667 158 0 671 250 6 759 158 5 768 113 4 777 113 4 777 113 4 + 777 113 4 777 252 4 792 247 12 805 250 6 805 250 6 805 250 6 + 805 250 6 805 250 6 805 398 6 822 158 5 822 158 5 822 158 5 + 822 158 5 822 346 5 839 113 8 840 247 12 840 247 12 840 247 12 + 840 247 12 840 247 12 840 247 12 840 247 12 840 247 12 840 247 12 + 840 247 12 840 247 12 840 393 12 845 224 6 870 102 10 870 158 0 + 870 158 0 870 158 0 870 158 0 870 355 0 899 113 8 899 113 8 + 899 113 8 899 113 8 899 113 8 899 113 8 899 113 8 899 234 8 + 911 223 7 929 148 7 934 223 7 934 223 7 934 223 7 934 223 7 + 934 223 7 934 223 7 934 239 7 938 102 10 938 102 10 938 102 10 + 938 102 10 938 102 10 938 102 10 938 102 10 938 102 10 938 102 10 + 938 148 7 938 148 7 938 148 7 938 148 7 938 148 7 938 148 7 + 938 160 7 938 208 10 959 224 6 959 224 6 959 224 6 959 224 6 + 959 224 6 959 238 6 963 133 8 979 133 8 979 133 8 979 133 8 + 979 133 8 979 133 8 979 133 8 979 133 8 979 330 8} + +do_execsql_test 5.2.3.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( PARTITION BY coalesce(a, '') + RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {295 1 1 335 1 1 607 1 1 667 1 1 742 1 1 759 1 1 845 1 1 + 890 1 1 929 1 1 959 1 1 962 1 1 962 1 1 962 1 1 962 1 1 + 962 1 1 962 1 1 962 1 1 962 1 1 962 1 1 1264 1 1 1264 1 1 + 1264 1 1 1264 1 1 1264 1 1 1264 1 1 1366 1 1 1366 1 1 1366 1 1 + 1366 1 1 1383 1 1 1398 1 1 1406 1 1 1421 1 1 1519 1 1 1519 1 1 + 1535 1 1 1651 1 1 1669 1 1 1682 1 1 1695 1 1 1804 1 1 1804 1 1 + 1804 1 1 1804 1 1 1804 1 1 1897 1 1 1919 1 1 2000 1 1 2048 1 1 + 2050 1 1 2050 1 1 2070 1 1 2086 1 1 2108 1 1 2108 1 1 2134 1 1 + 2150 1 1 2309 1 1 2309 1 1 2309 1 1 2340 1 1 2340 1 1 2340 1 1 + 2430 1 1 2690 1 1 2758 1 1 2770 1 1 2776 1 1 2834 1 1 2848 1 1 + 2947 1 1 2947 1 1 2947 1 1 2947 1 1 2980 1 1 3082 1 1 3088 1 1 + 3088 1 1 3113 1 1 3113 1 1 3113 1 1 3113 1 1 3234 1 1 3481 1 1 + 3481 1 1 3481 1 1 3481 1 1 3481 1 1 3481 1 1} + +do_execsql_test 5.2.4.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( ORDER BY a NULLS FIRST GROUPS 6 PRECEDING EXCLUDE CURRENT ROW ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {667 158 0 870 158 0 870 158 0 870 158 0 870 158 0 870 355 0 + 911 158 7 934 158 7 934 158 7 934 158 7 934 158 7 934 158 7 + 934 158 7 934 158 7 934 158 20 934 158 20 934 158 20 934 158 20 + 934 158 20 934 158 20 934 158 20 934 158 20 934 158 20 934 158 20 + 934 158 20 934 158 20 934 158 20 934 158 26 934 158 26 934 158 26 + 934 158 26 934 158 26 934 158 26 934 158 33 959 102 49 959 102 49 + 959 102 49 959 102 49 959 102 49 959 102 49 959 102 49 959 102 49 + 959 102 49 959 102 49 959 102 57 959 102 57 959 102 57 959 102 57 + 959 102 57 959 102 57 959 102 57 959 102 57 959 113 38 959 113 38 + 959 113 38 959 113 38 959 113 49 959 158 33 959 158 33 959 158 33 + 959 158 33 959 158 33 959 158 33 959 158 38 963 102 58 979 102 52 + 979 102 52 979 102 52 979 102 52 979 102 52 979 102 52 979 102 52 + 979 102 55 979 102 55 979 102 55 979 102 55 979 102 55 979 102 55 + 979 102 55 979 102 55 979 102 55 979 102 58 979 102 58 979 102 58 + 979 102 58 979 102 58 979 102 58 979 102 58 979 102 58} + +do_execsql_test 5.2.4.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( ORDER BY a NULLS FIRST GROUPS 6 PRECEDING EXCLUDE CURRENT ROW ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {1383 1 1 1421 1 1 1651 1 1 1695 1 1 2050 1 1 2050 1 1 3448 7 2 + 3732 7 2 4050 7 2 4120 7 2 4136 7 2 4359 7 2 4359 7 2 4359 7 2 + 7129 15 3 7135 15 3 7207 15 3 7441 15 3 7447 15 3 7447 15 3 + 7593 15 3 7840 15 3 7840 15 3 7840 15 3 7840 15 3 7840 15 3 + 7840 15 3 8447 28 4 8599 28 4 9206 28 4 9206 28 4 9206 28 4 + 9206 28 4 10051 34 5 10165 34 5 11010 34 5 11010 34 5 11010 34 5 + 11010 34 5 11010 34 5 11563 74 10 11697 74 10 11752 41 6 + 11776 57 8 11900 41 6 11925 74 10 11947 74 10 12368 74 10 + 12368 74 10 12368 74 10 12370 57 8 12416 41 6 12529 41 6 + 12529 41 6 12530 65 9 12546 65 9 12705 57 8 12705 57 8 12705 57 8 + 12705 57 8 12705 57 8 12705 57 8 12824 46 7 12826 65 9 + 13050 81 11 13110 81 11 13110 81 11 13154 65 9 13196 46 7 + 13376 65 9 13491 46 7 13491 46 7 13491 46 7 13491 46 7 13491 46 7 + 13491 46 7 13491 46 7 13491 46 7 13491 46 7 13509 65 9 13509 65 9 + 13509 65 9 13509 65 9 13692 81 11 13836 81 11 13949 81 11 + 13949 81 11 13949 81 11 13949 81 11} + +do_execsql_test 5.2.5.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( ORDER BY c NULLS FIRST RANGE BETWEEN 6 PRECEDING AND 7 FOLLOWING EXCLUDE CURRENT ROW ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {{} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 113 113 1 + 113 113 1 158 158 1 160 158 1 160 158 2 223 223 1 224 224 1 + 238 234 2 239 234 2 239 238 2 252 250 2 256 252 2 257 247 4 + 257 247 4 257 250 3 335 330 2 336 330 2 336 335 2 355 354 1 + 355 354 2 355 355 1 399 393 3 399 393 3 399 393 3 399 393 3 + 399 393 4 480 480 1 480 480 1 572 572 1 574 574 1 618 618 1 + 618 618 1 633 629 2 634 627 3 634 627 3 634 627 4 634 629 3 + 667 667 1 670 667 2 671 667 2 671 667 2 671 667 3 711 711 1 + 711 711 1 716 705 2 726 726 1 730 730 1 762 762 1 768 759 3 + 768 762 2 768 762 2 792 790 2 792 790 2 794 786 3 794 786 3 + 844 839 4 845 839 4 845 839 4 845 839 4 845 839 4 870 870 1 + 870 870 1 870 870 2 934 934 1 938 929 3 938 934 2 938 934 2 + 959 959 1 963 963 1} + +do_execsql_test 5.2.5.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( ORDER BY c NULLS FIRST RANGE BETWEEN 6 PRECEDING AND 7 FOLLOWING EXCLUDE CURRENT ROW ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {{} 1 1 {} 4 3 {} 5 4 {} 6 5 {} 6 5 {} 8 6 {} 9 7 {} 10 8 + {} 14 12 {} 15 13 {} 19 17 {} 20 18 {} 21 19 {} 23 21 {} 25 23 + {} 34 29 {} 35 30 {} 36 31 {} 37 32 {} 38 33 {} 38 33 {} 40 34 + {} 41 35 {} 42 36 {} 43 37 {} 43 37 {} 50 42 {} 56 47 {} 60 51 + {} 61 52 {} 62 53 {} 64 55 {} 64 55 {} 66 56 {} 67 57 {} 68 58 + {} 69 59 {} 70 60 {} 71 61 {} 72 62 {} 78 67 {} 78 67 {} 78 67 + {} 81 68 {} 82 69 {} 83 70 {} 85 72 {} 85 72 {} 89 75 113 2 2 + 113 2 2 223 11 9 239 12 10 239 13 11 257 18 16 335 22 20 + 335 24 22 355 27 25 355 27 25 504 16 14 504 17 15 705 58 49 + 710 26 24 711 57 48 711 59 50 759 63 54 929 84 71 959 88 74 + 963 87 73 1185 32 28 1185 32 28 1191 29 26 1191 29 26 1334 51 43 + 1334 55 46 1338 52 44 1338 52 44 1584 31 27 1678 77 66 1684 73 63 + 1684 73 63 1885 48 40 1889 46 39 1889 46 39 1891 45 38 1891 49 41 + 2005 54 45 2523 75 64 2523 76 65} + +do_execsql_test 5.2.6.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( ORDER BY c NULLS FIRST RANGE BETWEEN 0 PRECEDING AND 0 FOLLOWING EXCLUDE CURRENT ROW ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {{} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 113 113 1 + 113 113 1 158 158 0 158 158 1 355 355 0 355 355 1 393 393 1 + 393 393 1 399 399 0 399 399 1 480 480 1 480 480 1 618 618 1 + 618 618 1 629 629 0 629 629 1 667 667 0 667 667 1 768 768 1 + 768 768 1 839 839 1 839 839 1 870 870 1 870 870 1 870 870 2 + 938 938 1 938 938 1} + +do_execsql_test 5.2.6.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( ORDER BY c NULLS FIRST RANGE BETWEEN 0 PRECEDING AND 0 FOLLOWING EXCLUDE CURRENT ROW ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {{} 1 1 {} 4 3 {} 5 4 {} 6 5 {} 6 5 {} 8 6 {} 9 7 {} 10 8 + {} 11 9 {} 12 10 {} 13 11 {} 14 12 {} 15 13 {} 16 14 {} 17 15 + {} 18 16 {} 19 17 {} 20 18 {} 21 19 {} 22 20 {} 23 21 {} 24 22 + {} 25 23 {} 26 24 {} 31 27 {} 34 29 {} 35 30 {} 36 31 {} 37 32 + {} 38 33 {} 38 33 {} 40 34 {} 41 35 {} 42 36 {} 43 37 {} 43 37 + {} 45 38 {} 48 40 {} 49 41 {} 50 42 {} 51 43 {} 54 45 {} 55 46 + {} 56 47 {} 57 48 {} 58 49 {} 59 50 {} 60 51 {} 61 52 {} 62 53 + {} 63 54 {} 64 55 {} 64 55 {} 66 56 {} 67 57 {} 68 58 {} 69 59 + {} 70 60 {} 71 61 {} 72 62 {} 75 64 {} 76 65 {} 77 66 {} 78 67 + {} 78 67 {} 78 67 {} 81 68 {} 82 69 {} 83 70 {} 84 71 {} 85 72 + {} 85 72 {} 87 73 {} 88 74 {} 89 75 113 2 2 113 2 2 355 27 25 + 355 27 25 393 29 26 393 29 26 399 32 28 399 32 28 629 46 39 + 629 46 39 667 52 44 667 52 44 839 73 63 839 73 63} + +do_execsql_test 5.2.7.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( ORDER BY c NULLS FIRST, b NULLS FIRST, a NULLS FIRST + ROWS BETWEEN 6 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {963 929 6 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 + 979 102 83 979 113 80 979 113 81 979 113 82 979 133 79 979 148 78 + 979 158 76 979 158 77 979 160 76 979 208 75 979 223 74 979 224 73 + 979 234 72 979 238 71 979 239 70 979 247 69 979 250 68 979 252 67 + 979 256 66 979 257 65 979 295 64 979 309 64 979 330 62 979 335 61 + 979 336 60 979 346 59 979 354 59 979 355 57 979 355 57 979 393 55 + 979 393 56 979 398 54 979 399 53 979 399 53 979 412 52 979 421 51 + 979 430 50 979 443 49 979 480 47 979 480 48 979 572 47 979 574 45 + 979 607 44 979 618 42 979 618 43 979 627 41 979 629 40 979 629 41 + 979 633 39 979 634 38 979 652 37 979 660 36 979 667 35 979 667 35 + 979 670 34 979 671 33 979 683 32 979 705 31 979 711 30 979 716 29 + 979 726 28 979 730 27 979 759 26 979 762 25 979 768 23 979 768 24 + 979 777 22 979 786 21 979 790 20 979 792 19 979 794 18 979 805 17 + 979 822 17 979 839 14 979 839 15 979 840 13 979 844 12 979 845 11 + 979 870 9 979 870 10 979 870 10 979 899 8 979 911 7} + +do_execsql_test 5.2.7.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( ORDER BY c NULLS FIRST, b NULLS FIRST, a NULLS FIRST + ROWS BETWEEN 6 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {2851 89 89 3778 88 88 4681 87 87 5556 83 83 5574 82 82 5586 81 81 + 5640 84 84 5640 85 85 5640 86 86 7324 80 80 8123 77 77 8129 73 73 + 8129 74 74 8163 78 78 8163 79 79 8940 71 71 8968 75 75 8968 76 76 + 9727 66 66 9745 69 69 9745 70 70 9745 72 72 10504 65 65 + 10504 67 67 10504 68 68 11215 64 64 11844 62 62 11920 63 63 + 13274 60 60 13274 61 61 13897 58 58 13903 57 57 13925 56 56 + 13937 55 55 13941 59 59 15203 53 53 15241 54 54 15832 52 52 + 17100 48 48 17104 46 46 17104 47 47 17106 45 45 17126 49 49 + 17126 50 50 17126 51 51 17569 42 42 17733 44 44 18176 43 43 + 18597 40 40 18597 41 41 18952 37 37 18996 39 39 19395 38 38 + 19760 35 35 19788 36 36 20492 32 32 20492 33 33 20498 30 30 + 20536 34 34 20833 29 29 20871 28 28 20891 31 31 21180 27 27 + 21752 23 23 21830 26 26 22025 21 21 22087 22 22 22087 24 24 + 22087 25 25 22278 20 20 22316 19 19 22549 15 15 22557 14 14 + 22573 17 17 22573 18 18 22706 10 10 22796 11 11 22796 12 12 + 22796 13 13 22796 16 16 23022 4 4 23042 2 2 23042 3 3 23042 9 9 + 23155 1 1 23155 5 5 23155 6 6 23155 7 7 23155 8 8} + +do_execsql_test 5.2.8.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {963 102 82 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 + 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 + 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 + 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 + 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 + 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 + 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 + 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 + 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 + 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 + 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 + 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 + 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 + 979 102 82 979 102 82 979 102 82 979 102 82 979 102 83 979 102 83 + 979 102 83 979 102 83 979 102 83 979 102 83 979 113 82} + +do_execsql_test 5.2.8.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {22176 1 1 22192 1 1 22196 1 1 22226 1 1 22244 1 1 22256 1 1 + 22310 1 1 22316 1 1 22316 1 1 22350 1 1 22378 1 1 22396 1 1 + 22444 1 1 22450 1 1 22472 1 1 22484 1 1 22488 1 1 22488 1 1 + 22522 1 1 22526 1 1 22526 1 1 22528 1 1 22548 1 1 22712 1 1 + 22734 1 1 22756 1 1 22756 1 1 22762 1 1 22762 1 1 22800 1 1 + 22800 1 1 22820 1 1 22846 1 1 22860 1 1 22898 1 1 22908 1 1 + 22916 1 1 22932 1 1 23022 1 1 23042 1 1 23042 1 1 23155 1 1 + 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 + 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 + 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 + 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 + 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 + 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 + 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1 + 23155 1 1 23155 1 1 23155 1 1 23155 1 1 23155 1 1} + +do_execsql_test 5.2.9.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( ORDER BY a NULLS LAST + RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {667 158 0 870 113 8 870 158 0 870 158 0 870 158 0 870 158 0 + 870 355 0 899 113 8 899 113 8 899 113 8 899 113 8 899 113 8 + 899 113 8 899 113 8 899 113 15 899 113 15 899 113 15 899 113 15 + 899 113 15 899 113 15 899 113 15 899 158 8 963 113 24 979 102 43 + 979 102 43 979 102 43 979 102 43 979 102 43 979 102 43 979 102 43 + 979 102 43 979 102 43 979 102 43 979 102 48 979 102 48 979 102 48 + 979 102 48 979 102 48 979 102 55 979 102 55 979 102 55 979 102 55 + 979 102 55 979 102 55 979 102 55 979 102 61 979 102 61 979 102 61 + 979 102 61 979 102 61 979 102 61 979 102 74 979 102 74 979 102 74 + 979 102 74 979 102 74 979 102 74 979 102 74 979 102 74 979 102 74 + 979 102 74 979 102 74 979 102 74 979 102 74 979 102 82 979 102 82 + 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 + 979 113 24 979 113 24 979 113 24 979 113 24 979 113 24 979 113 24 + 979 113 24 979 113 24 979 113 32 979 113 32 979 113 32 979 113 32 + 979 113 32 979 113 32 979 113 32 979 113 32 979 113 43} + +do_execsql_test 5.2.9.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( ORDER BY a NULLS LAST + RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {1383 84 11 1421 84 11 1651 84 11 1695 84 11 2050 84 11 2050 84 11 + 4098 75 10 4158 75 10 4158 75 10 4740 75 10 4884 75 10 4997 75 10 + 4997 75 10 4997 75 10 4997 75 10 6532 68 9 6666 68 9 6894 68 9 + 6916 68 9 7337 68 9 7337 68 9 7337 68 9 9471 59 8 9487 59 8 + 9767 59 8 10095 59 8 10317 59 8 10450 59 8 10450 59 8 10450 59 8 + 10450 59 8 10785 51 7 11379 51 7 11714 51 7 11714 51 7 11714 51 7 + 11714 51 7 11714 51 7 11714 51 7 12009 40 6 12381 40 6 12676 40 6 + 12676 40 6 12676 40 6 12676 40 6 12676 40 6 12676 40 6 12676 40 6 + 12676 40 6 12676 40 6 13418 35 5 13566 35 5 14082 35 5 14195 35 5 + 14195 35 5 15040 28 4 15154 28 4 15999 28 4 15999 28 4 15999 28 4 + 15999 28 4 15999 28 4 16606 22 3 16758 22 3 17365 22 3 17365 22 3 + 17365 22 3 17365 22 3 20135 9 2 20141 9 2 20213 9 2 20447 9 2 + 20453 9 2 20453 9 2 20599 9 2 20846 9 2 20846 9 2 20846 9 2 + 20846 9 2 20846 9 2 20846 9 2 22244 1 1 22528 1 1 22846 1 1 + 22916 1 1 22932 1 1 23155 1 1 23155 1 1 23155 1 1} + +do_execsql_test 5.2.10.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( PARTITION BY coalesce(a, '') + RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {667 158 0 671 250 6 759 158 5 768 113 4 777 113 4 777 113 4 + 777 113 4 777 252 4 792 247 12 805 250 6 805 250 6 805 250 6 + 805 250 6 805 250 6 805 398 6 822 158 5 822 158 5 822 158 5 + 822 158 5 822 346 5 839 113 8 840 247 12 840 247 12 840 247 12 + 840 247 12 840 247 12 840 247 12 840 247 12 840 247 12 840 247 12 + 840 247 12 840 247 12 840 393 12 845 224 6 870 102 10 870 158 0 + 870 158 0 870 158 0 870 158 0 870 355 0 899 113 8 899 113 8 + 899 113 8 899 113 8 899 113 8 899 113 8 899 113 8 899 234 8 + 911 223 7 929 148 7 934 223 7 934 223 7 934 223 7 934 223 7 + 934 223 7 934 223 7 934 239 7 938 102 10 938 102 10 938 102 10 + 938 102 10 938 102 10 938 102 10 938 102 10 938 102 10 938 102 10 + 938 148 7 938 148 7 938 148 7 938 148 7 938 148 7 938 148 7 + 938 160 7 938 208 10 959 224 6 959 224 6 959 224 6 959 224 6 + 959 224 6 959 238 6 963 133 8 979 133 8 979 133 8 979 133 8 + 979 133 8 979 133 8 979 133 8 979 133 8 979 330 8} + +do_execsql_test 5.2.10.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( PARTITION BY coalesce(a, '') + RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {295 1 1 335 1 1 607 1 1 667 1 1 742 1 1 759 1 1 845 1 1 + 890 1 1 929 1 1 959 1 1 962 1 1 962 1 1 962 1 1 962 1 1 + 962 1 1 962 1 1 962 1 1 962 1 1 962 1 1 1264 1 1 1264 1 1 + 1264 1 1 1264 1 1 1264 1 1 1264 1 1 1366 1 1 1366 1 1 1366 1 1 + 1366 1 1 1383 1 1 1398 1 1 1406 1 1 1421 1 1 1519 1 1 1519 1 1 + 1535 1 1 1651 1 1 1669 1 1 1682 1 1 1695 1 1 1804 1 1 1804 1 1 + 1804 1 1 1804 1 1 1804 1 1 1897 1 1 1919 1 1 2000 1 1 2048 1 1 + 2050 1 1 2050 1 1 2070 1 1 2086 1 1 2108 1 1 2108 1 1 2134 1 1 + 2150 1 1 2309 1 1 2309 1 1 2309 1 1 2340 1 1 2340 1 1 2340 1 1 + 2430 1 1 2690 1 1 2758 1 1 2770 1 1 2776 1 1 2834 1 1 2848 1 1 + 2947 1 1 2947 1 1 2947 1 1 2947 1 1 2980 1 1 3082 1 1 3088 1 1 + 3088 1 1 3113 1 1 3113 1 1 3113 1 1 3113 1 1 3234 1 1 3481 1 1 + 3481 1 1 3481 1 1 3481 1 1 3481 1 1 3481 1 1} + +do_execsql_test 5.2.11.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( ORDER BY a NULLS LAST GROUPS 6 PRECEDING EXCLUDE CURRENT ROW ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {911 223 7 934 158 26 934 158 26 934 158 26 934 158 26 934 158 26 + 934 158 33 934 223 7 934 223 7 934 223 7 934 223 7 934 223 7 + 934 223 7 934 223 20 934 223 20 934 223 20 934 223 20 934 223 20 + 934 223 20 934 223 20 934 223 20 934 223 20 934 223 20 934 223 20 + 934 223 20 934 223 20 934 223 26 934 239 7 959 102 49 959 102 49 + 959 102 49 959 102 49 959 102 49 959 102 49 959 102 49 959 102 49 + 959 102 49 959 102 49 959 102 57 959 102 57 959 102 57 959 102 57 + 959 102 57 959 102 57 959 102 57 959 102 57 959 113 38 959 113 38 + 959 113 38 959 113 38 959 113 49 959 158 33 959 158 33 959 158 33 + 959 158 33 959 158 33 959 158 33 959 158 38 963 102 58 979 102 49 + 979 102 49 979 102 49 979 102 49 979 102 49 979 102 49 979 102 52 + 979 102 52 979 102 52 979 102 52 979 102 52 979 102 52 979 102 52 + 979 102 55 979 102 55 979 102 55 979 102 55 979 102 55 979 102 55 + 979 102 55 979 102 55 979 102 55 979 102 58 979 102 58 979 102 58 + 979 102 58 979 102 58 979 102 58 979 102 58 979 102 58} + +do_execsql_test 5.2.11.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( ORDER BY a NULLS LAST GROUPS 6 PRECEDING EXCLUDE CURRENT ROW ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {1398 1 1 1682 1 1 2000 1 1 2070 1 1 2086 1 1 2309 1 1 2309 1 1 + 2309 1 1 5079 9 2 5085 9 2 5157 9 2 5391 9 2 5397 9 2 5397 9 2 + 5543 9 2 5790 9 2 5790 9 2 5790 9 2 5790 9 2 5790 9 2 5790 9 2 + 6397 22 3 6549 22 3 7156 22 3 7156 22 3 7156 22 3 7156 22 3 + 8001 28 4 8115 28 4 8960 28 4 8960 28 4 8960 28 4 8960 28 4 + 8960 28 4 9702 35 5 9850 35 5 10366 35 5 10479 35 5 10479 35 5 + 10774 40 6 11146 40 6 11441 40 6 11441 40 6 11441 40 6 11441 40 6 + 11441 40 6 11441 40 6 11441 40 6 11441 40 6 11441 40 6 11563 68 9 + 11697 68 9 11776 51 7 11925 68 9 11947 68 9 12368 68 9 12368 68 9 + 12368 68 9 12370 51 7 12530 59 8 12546 59 8 12705 51 7 12705 51 7 + 12705 51 7 12705 51 7 12705 51 7 12705 51 7 12826 59 8 + 13050 75 10 13110 75 10 13110 75 10 13154 59 8 13376 59 8 + 13509 59 8 13509 59 8 13509 59 8 13509 59 8 13528 84 11 + 13566 84 11 13692 75 10 13796 84 11 13836 75 10 13840 84 11 + 13949 75 10 13949 75 10 13949 75 10 13949 75 10 14195 84 11 + 14195 84 11} + +do_execsql_test 5.2.12.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( ORDER BY c NULLS LAST RANGE BETWEEN 6 PRECEDING AND 7 FOLLOWING EXCLUDE CURRENT ROW ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {{} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 113 113 1 + 113 113 1 158 158 1 160 158 1 160 158 2 223 223 1 224 224 1 + 238 234 2 239 234 2 239 238 2 252 250 2 256 252 2 257 247 4 + 257 247 4 257 250 3 335 330 2 336 330 2 336 335 2 355 354 1 + 355 354 2 355 355 1 399 393 3 399 393 3 399 393 3 399 393 3 + 399 393 4 480 480 1 480 480 1 572 572 1 574 574 1 618 618 1 + 618 618 1 633 629 2 634 627 3 634 627 3 634 627 4 634 629 3 + 667 667 1 670 667 2 671 667 2 671 667 2 671 667 3 711 711 1 + 711 711 1 716 705 2 726 726 1 730 730 1 762 762 1 768 759 3 + 768 762 2 768 762 2 792 790 2 792 790 2 794 786 3 794 786 3 + 844 839 4 845 839 4 845 839 4 845 839 4 845 839 4 870 870 1 + 870 870 1 870 870 2 934 934 1 938 929 3 938 934 2 938 934 2 + 959 959 1 963 963 1} + +do_execsql_test 5.2.12.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( ORDER BY c NULLS LAST RANGE BETWEEN 6 PRECEDING AND 7 FOLLOWING EXCLUDE CURRENT ROW ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {{} 1 1 {} 4 3 {} 5 4 {} 6 5 {} 6 5 {} 8 6 {} 9 7 {} 10 8 + {} 14 12 {} 15 13 {} 19 17 {} 20 18 {} 21 19 {} 23 21 {} 25 23 + {} 34 29 {} 35 30 {} 36 31 {} 37 32 {} 38 33 {} 38 33 {} 40 34 + {} 41 35 {} 42 36 {} 43 37 {} 43 37 {} 50 42 {} 56 47 {} 60 51 + {} 61 52 {} 62 53 {} 64 55 {} 64 55 {} 66 56 {} 67 57 {} 68 58 + {} 69 59 {} 70 60 {} 71 61 {} 72 62 {} 78 67 {} 78 67 {} 78 67 + {} 81 68 {} 82 69 {} 83 70 {} 85 72 {} 85 72 {} 89 75 113 2 2 + 113 2 2 223 11 9 239 12 10 239 13 11 257 18 16 335 22 20 + 335 24 22 355 27 25 355 27 25 504 16 14 504 17 15 705 58 49 + 710 26 24 711 57 48 711 59 50 759 63 54 929 84 71 959 88 74 + 963 87 73 1185 32 28 1185 32 28 1191 29 26 1191 29 26 1334 51 43 + 1334 55 46 1338 52 44 1338 52 44 1584 31 27 1678 77 66 1684 73 63 + 1684 73 63 1885 48 40 1889 46 39 1889 46 39 1891 45 38 1891 49 41 + 2005 54 45 2523 75 64 2523 76 65} + +do_execsql_test 5.2.13.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( ORDER BY c NULLS LAST RANGE BETWEEN 0 PRECEDING AND 0 FOLLOWING EXCLUDE CURRENT ROW ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {{} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 113 113 1 + 113 113 1 158 158 0 158 158 1 355 355 0 355 355 1 393 393 1 + 393 393 1 399 399 0 399 399 1 480 480 1 480 480 1 618 618 1 + 618 618 1 629 629 0 629 629 1 667 667 0 667 667 1 768 768 1 + 768 768 1 839 839 1 839 839 1 870 870 1 870 870 1 870 870 2 + 938 938 1 938 938 1} + +do_execsql_test 5.2.13.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( ORDER BY c NULLS LAST RANGE BETWEEN 0 PRECEDING AND 0 FOLLOWING EXCLUDE CURRENT ROW ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {{} 1 1 {} 4 3 {} 5 4 {} 6 5 {} 6 5 {} 8 6 {} 9 7 {} 10 8 + {} 11 9 {} 12 10 {} 13 11 {} 14 12 {} 15 13 {} 16 14 {} 17 15 + {} 18 16 {} 19 17 {} 20 18 {} 21 19 {} 22 20 {} 23 21 {} 24 22 + {} 25 23 {} 26 24 {} 31 27 {} 34 29 {} 35 30 {} 36 31 {} 37 32 + {} 38 33 {} 38 33 {} 40 34 {} 41 35 {} 42 36 {} 43 37 {} 43 37 + {} 45 38 {} 48 40 {} 49 41 {} 50 42 {} 51 43 {} 54 45 {} 55 46 + {} 56 47 {} 57 48 {} 58 49 {} 59 50 {} 60 51 {} 61 52 {} 62 53 + {} 63 54 {} 64 55 {} 64 55 {} 66 56 {} 67 57 {} 68 58 {} 69 59 + {} 70 60 {} 71 61 {} 72 62 {} 75 64 {} 76 65 {} 77 66 {} 78 67 + {} 78 67 {} 78 67 {} 81 68 {} 82 69 {} 83 70 {} 84 71 {} 85 72 + {} 85 72 {} 87 73 {} 88 74 {} 89 75 113 2 2 113 2 2 355 27 25 + 355 27 25 393 29 26 393 29 26 399 32 28 399 32 28 629 46 39 + 629 46 39 667 52 44 667 52 44 839 73 63 839 73 63} + +do_execsql_test 5.2.14.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( ORDER BY c NULLS LAST, b NULLS LAST, a NULLS LAST + ROWS BETWEEN 6 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {963 929 6 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 + 979 102 83 979 113 80 979 113 81 979 113 82 979 133 79 979 148 78 + 979 158 76 979 158 77 979 160 76 979 208 75 979 223 74 979 224 73 + 979 234 72 979 238 71 979 239 70 979 247 69 979 250 68 979 252 67 + 979 256 66 979 257 65 979 295 64 979 309 63 979 330 63 979 335 61 + 979 336 60 979 346 59 979 354 58 979 355 56 979 355 58 979 393 55 + 979 393 56 979 398 54 979 399 52 979 399 53 979 412 52 979 421 51 + 979 430 50 979 443 49 979 480 47 979 480 48 979 572 46 979 574 46 + 979 607 44 979 618 42 979 618 43 979 627 41 979 629 40 979 629 40 + 979 633 39 979 634 38 979 652 37 979 660 36 979 667 34 979 667 35 + 979 670 34 979 671 33 979 683 32 979 705 31 979 711 30 979 716 29 + 979 726 28 979 730 27 979 759 26 979 762 25 979 768 23 979 768 24 + 979 777 22 979 786 21 979 790 20 979 792 19 979 794 18 979 805 17 + 979 822 16 979 839 15 979 839 15 979 840 13 979 844 12 979 845 11 + 979 870 8 979 870 9 979 870 10 979 899 8 979 911 7} + +do_execsql_test 5.2.14.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( ORDER BY c NULLS LAST, b NULLS LAST, a NULLS LAST + ROWS BETWEEN 6 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {2851 89 89 3778 88 88 4681 87 87 5556 83 83 5574 82 82 5586 81 81 + 5640 84 84 5640 85 85 5640 86 86 7324 80 80 8123 77 77 8129 73 73 + 8129 74 74 8163 78 78 8163 79 79 8940 71 71 8968 75 75 8968 76 76 + 9727 66 66 9745 69 69 9745 70 70 9745 72 72 10504 65 65 + 10504 67 67 10504 68 68 11215 64 64 11844 62 62 11920 63 63 + 13274 60 60 13274 61 61 13897 58 58 13903 57 57 13925 56 56 + 13937 55 55 13941 59 59 15203 53 53 15241 54 54 15832 52 52 + 17100 48 48 17104 46 46 17104 47 47 17106 45 45 17126 49 49 + 17126 50 50 17126 51 51 17569 42 42 17733 44 44 18176 43 43 + 18597 40 40 18597 41 41 18952 37 37 18996 39 39 19395 38 38 + 19760 35 35 19788 36 36 20492 32 32 20492 33 33 20498 30 30 + 20536 34 34 20833 29 29 20871 28 28 20891 31 31 21180 27 27 + 21752 23 23 21830 26 26 22025 21 21 22087 22 22 22087 24 24 + 22087 25 25 22278 20 20 22316 19 19 22549 15 15 22557 14 14 + 22573 17 17 22573 18 18 22706 10 10 22796 11 11 22796 12 12 + 22796 13 13 22796 16 16 23022 4 4 23042 2 2 23042 3 3 23042 9 9 + 23155 1 1 23155 5 5 23155 6 6 23155 7 7 23155 8 8} + +do_execsql_test 5.3.1.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {{} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0} + +do_execsql_test 5.3.1.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {{} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1} + +do_execsql_test 5.3.2.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( ORDER BY a NULLS FIRST + RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {{} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 899 113 9 899 113 9 899 113 9 899 113 9 + 899 113 9 899 113 9 899 113 9 899 113 16 899 113 16 899 113 16 + 899 113 16 899 113 16 899 113 16 899 113 16 899 113 16 899 113 16 + 979 102 44 979 102 44 979 102 44 979 102 44 979 102 44 979 102 49 + 979 102 49 979 102 49 979 102 49 979 102 49 979 102 49 979 102 49 + 979 102 56 979 102 56 979 102 56 979 102 56 979 102 56 979 102 56 + 979 102 62 979 102 62 979 102 62 979 102 62 979 102 62 979 102 62 + 979 102 62 979 102 62 979 102 62 979 102 62 979 102 62 979 102 62 + 979 102 62 979 102 75 979 102 75 979 102 75 979 102 75 979 102 75 + 979 102 75 979 102 75 979 102 75 979 102 83 979 102 83 979 102 83 + 979 102 83 979 102 83 979 102 83 979 113 25 979 113 25 979 113 25 + 979 113 25 979 113 25 979 113 25 979 113 25 979 113 25 979 113 33 + 979 113 33 979 113 33 979 113 33 979 113 33 979 113 33 979 113 33 + 979 113 33 979 113 33 979 113 33 979 113 33} + +do_execsql_test 5.3.2.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( ORDER BY a NULLS FIRST + RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {{} 81 11 {} 81 11 {} 81 11 {} 81 11 {} 81 11 {} 81 11 {} 81 11 + {} 81 11 {} 81 11 2947 74 10 2947 74 10 2947 74 10 2947 74 10 + 2947 74 10 2947 74 10 2947 74 10 5287 65 9 5287 65 9 5287 65 9 + 5287 65 9 5287 65 9 5287 65 9 5287 65 9 5287 65 9 5287 65 9 + 8400 57 8 8400 57 8 8400 57 8 8400 57 8 8400 57 8 8400 57 8 + 8400 57 8 8400 57 8 9664 46 7 9664 46 7 9664 46 7 9664 46 7 + 9664 46 7 9664 46 7 9664 46 7 9664 46 7 9664 46 7 9664 46 7 + 9664 46 7 10626 41 6 10626 41 6 10626 41 6 10626 41 6 10626 41 6 + 12145 34 5 12145 34 5 12145 34 5 12145 34 5 12145 34 5 12145 34 5 + 12145 34 5 13949 28 4 13949 28 4 13949 28 4 13949 28 4 13949 28 4 + 13949 28 4 15315 15 3 15315 15 3 15315 15 3 15315 15 3 15315 15 3 + 15315 15 3 15315 15 3 15315 15 3 15315 15 3 15315 15 3 15315 15 3 + 15315 15 3 15315 15 3 18796 7 2 18796 7 2 18796 7 2 18796 7 2 + 18796 7 2 18796 7 2 18796 7 2 18796 7 2 21105 1 1 21105 1 1 + 21105 1 1 21105 1 1 21105 1 1 21105 1 1} + +do_execsql_test 5.3.3.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( PARTITION BY coalesce(a, '') + RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {{} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0} + +do_execsql_test 5.3.3.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( PARTITION BY coalesce(a, '') + RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {{} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1} + +do_execsql_test 5.3.4.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( ORDER BY a NULLS FIRST GROUPS 6 PRECEDING EXCLUDE GROUP ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {{} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 870 158 0 + 870 158 0 870 158 0 870 158 0 870 158 0 870 158 0 870 158 0 + 870 158 0 934 158 8 934 158 8 934 158 8 934 158 8 934 158 8 + 934 158 8 934 158 8 934 158 8 934 158 8 934 158 8 934 158 8 + 934 158 8 934 158 8 934 158 21 934 158 21 934 158 21 934 158 21 + 934 158 21 934 158 21 934 158 27 934 158 27 934 158 27 934 158 27 + 934 158 27 934 158 27 934 158 27 959 102 50 959 102 50 959 102 50 + 959 102 50 959 102 50 959 102 50 959 102 50 959 102 50 959 102 50 + 959 102 50 959 102 50 959 102 50 959 102 50 959 102 50 959 102 50 + 959 102 50 959 102 50 959 113 39 959 113 39 959 113 39 959 113 39 + 959 113 39 959 113 39 959 113 39 959 113 39 959 113 39 959 113 39 + 959 113 39 959 158 34 959 158 34 959 158 34 959 158 34 959 158 34 + 979 102 46 979 102 46 979 102 46 979 102 46 979 102 46 979 102 46 + 979 102 46 979 102 47 979 102 47 979 102 47 979 102 47 979 102 47 + 979 102 47 979 102 47 979 102 47 979 102 47} + +do_execsql_test 5.3.4.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( ORDER BY a NULLS FIRST GROUPS 6 PRECEDING EXCLUDE GROUP ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {{} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 2050 7 2 2050 7 2 + 2050 7 2 2050 7 2 2050 7 2 2050 7 2 2050 7 2 2050 7 2 4359 15 3 + 4359 15 3 4359 15 3 4359 15 3 4359 15 3 4359 15 3 4359 15 3 + 4359 15 3 4359 15 3 4359 15 3 4359 15 3 4359 15 3 4359 15 3 + 7840 28 4 7840 28 4 7840 28 4 7840 28 4 7840 28 4 7840 28 4 + 9206 34 5 9206 34 5 9206 34 5 9206 34 5 9206 34 5 9206 34 5 + 9206 34 5 10028 74 10 10028 74 10 10028 74 10 10028 74 10 + 10028 74 10 10028 74 10 10028 74 10 10396 65 9 10396 65 9 + 10396 65 9 10396 65 9 10396 65 9 10396 65 9 10396 65 9 10396 65 9 + 10396 65 9 11002 81 11 11002 81 11 11002 81 11 11002 81 11 + 11002 81 11 11002 81 11 11002 81 11 11002 81 11 11002 81 11 + 11010 41 6 11010 41 6 11010 41 6 11010 41 6 11010 41 6 11441 57 8 + 11441 57 8 11441 57 8 11441 57 8 11441 57 8 11441 57 8 11441 57 8 + 11441 57 8 12529 46 7 12529 46 7 12529 46 7 12529 46 7 12529 46 7 + 12529 46 7 12529 46 7 12529 46 7 12529 46 7 12529 46 7 12529 46 7} + +do_execsql_test 5.3.5.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( ORDER BY c NULLS FIRST RANGE BETWEEN 6 PRECEDING AND 7 FOLLOWING EXCLUDE GROUP ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {{} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 158 158 1 160 160 1 160 160 1 223 223 1 224 224 1 + 238 234 2 239 234 2 239 238 2 252 250 2 256 252 2 257 247 4 + 257 247 4 257 250 3 335 330 2 336 330 2 336 335 2 354 354 1 + 354 354 1 355 355 1 398 393 3 398 393 3 399 393 3 399 398 2 + 399 398 2 572 572 1 574 574 1 633 629 2 634 627 3 634 627 3 + 634 627 3 634 629 3 667 667 1 670 667 2 671 667 2 671 670 2 + 671 670 2 711 711 1 711 711 1 716 705 2 726 726 1 730 730 1 + 762 762 1 762 762 1 762 762 1 768 759 3 792 790 2 792 790 2 + 794 786 3 794 786 3 844 839 4 845 839 4 845 839 4 845 840 3 + 845 840 3 934 934 1 934 934 1 934 934 1 938 929 3 959 959 1 + 963 963 1} + +do_execsql_test 5.3.5.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( ORDER BY c NULLS FIRST RANGE BETWEEN 6 PRECEDING AND 7 FOLLOWING EXCLUDE GROUP ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {{} 1 1 {} 2 2 {} 2 2 {} 4 3 {} 5 4 {} 6 5 {} 6 5 {} 8 6 + {} 9 7 {} 10 8 {} 14 12 {} 15 13 {} 19 17 {} 20 18 {} 21 19 + {} 23 21 {} 25 23 {} 27 25 {} 27 25 {} 34 29 {} 35 30 {} 36 31 + {} 37 32 {} 38 33 {} 38 33 {} 40 34 {} 41 35 {} 42 36 {} 43 37 + {} 43 37 {} 50 42 {} 56 47 {} 60 51 {} 61 52 {} 62 53 {} 64 55 + {} 64 55 {} 66 56 {} 67 57 {} 68 58 {} 69 59 {} 70 60 {} 71 61 + {} 72 62 {} 78 67 {} 78 67 {} 78 67 {} 81 68 {} 82 69 {} 83 70 + {} 85 72 {} 85 72 {} 89 75 223 11 9 239 12 10 239 13 11 + 257 18 16 335 22 20 335 24 22 504 16 14 504 17 15 671 52 44 + 671 52 44 705 58 49 710 26 24 711 57 48 711 59 50 759 63 54 + 786 32 28 786 32 28 798 29 26 798 29 26 845 73 63 845 73 63 + 929 84 71 959 88 74 963 87 73 1260 46 39 1260 46 39 1334 51 43 + 1334 55 46 1584 31 27 1678 77 66 1885 48 40 1891 45 38 1891 49 41 + 2005 54 45 2523 75 64 2523 76 65} + +do_execsql_test 5.3.6.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( ORDER BY c NULLS FIRST RANGE BETWEEN 0 PRECEDING AND 0 FOLLOWING EXCLUDE GROUP ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {{} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0} + +do_execsql_test 5.3.6.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( ORDER BY c NULLS FIRST RANGE BETWEEN 0 PRECEDING AND 0 FOLLOWING EXCLUDE GROUP ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {{} 1 1 {} 2 2 {} 2 2 {} 4 3 {} 5 4 {} 6 5 {} 6 5 {} 8 6 + {} 9 7 {} 10 8 {} 11 9 {} 12 10 {} 13 11 {} 14 12 {} 15 13 + {} 16 14 {} 17 15 {} 18 16 {} 19 17 {} 20 18 {} 21 19 {} 22 20 + {} 23 21 {} 24 22 {} 25 23 {} 26 24 {} 27 25 {} 27 25 {} 29 26 + {} 29 26 {} 31 27 {} 32 28 {} 32 28 {} 34 29 {} 35 30 {} 36 31 + {} 37 32 {} 38 33 {} 38 33 {} 40 34 {} 41 35 {} 42 36 {} 43 37 + {} 43 37 {} 45 38 {} 46 39 {} 46 39 {} 48 40 {} 49 41 {} 50 42 + {} 51 43 {} 52 44 {} 52 44 {} 54 45 {} 55 46 {} 56 47 {} 57 48 + {} 58 49 {} 59 50 {} 60 51 {} 61 52 {} 62 53 {} 63 54 {} 64 55 + {} 64 55 {} 66 56 {} 67 57 {} 68 58 {} 69 59 {} 70 60 {} 71 61 + {} 72 62 {} 73 63 {} 73 63 {} 75 64 {} 76 65 {} 77 66 {} 78 67 + {} 78 67 {} 78 67 {} 81 68 {} 82 69 {} 83 70 {} 84 71 {} 85 72 + {} 85 72 {} 87 73 {} 88 74 {} 89 75} + +do_execsql_test 5.3.7.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( ORDER BY c NULLS FIRST, b NULLS FIRST, a NULLS FIRST + ROWS BETWEEN 6 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {963 929 6 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 + 979 102 83 979 113 80 979 113 81 979 113 82 979 133 79 979 148 78 + 979 158 76 979 158 77 979 160 76 979 208 75 979 223 74 979 224 73 + 979 234 72 979 238 71 979 239 70 979 247 69 979 250 68 979 252 67 + 979 256 66 979 257 65 979 295 64 979 309 64 979 330 62 979 335 61 + 979 336 60 979 346 59 979 354 59 979 355 57 979 355 57 979 393 55 + 979 393 56 979 398 54 979 399 53 979 399 53 979 412 52 979 421 51 + 979 430 50 979 443 49 979 480 47 979 480 48 979 572 47 979 574 45 + 979 607 44 979 618 42 979 618 43 979 627 41 979 629 40 979 629 41 + 979 633 39 979 634 38 979 652 37 979 660 36 979 667 35 979 667 35 + 979 670 34 979 671 33 979 683 32 979 705 31 979 711 30 979 716 29 + 979 726 28 979 730 27 979 759 26 979 762 25 979 768 23 979 768 24 + 979 777 22 979 786 21 979 790 20 979 792 19 979 794 18 979 805 17 + 979 822 17 979 839 14 979 839 15 979 840 13 979 844 12 979 845 11 + 979 870 9 979 870 10 979 870 10 979 899 8 979 911 7} + +do_execsql_test 5.3.7.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( ORDER BY c NULLS FIRST, b NULLS FIRST, a NULLS FIRST + ROWS BETWEEN 6 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {2851 89 89 3778 88 88 4681 87 87 5556 83 83 5574 82 82 5586 81 81 + 5640 84 84 5640 85 85 5640 86 86 7324 80 80 8123 77 77 8129 73 73 + 8129 74 74 8163 78 78 8163 79 79 8940 71 71 8968 75 75 8968 76 76 + 9727 66 66 9745 69 69 9745 70 70 9745 72 72 10504 65 65 + 10504 67 67 10504 68 68 11215 64 64 11844 62 62 11920 63 63 + 13274 60 60 13274 61 61 13897 58 58 13903 57 57 13925 56 56 + 13937 55 55 13941 59 59 15203 53 53 15241 54 54 15832 52 52 + 17100 48 48 17104 46 46 17104 47 47 17106 45 45 17126 49 49 + 17126 50 50 17126 51 51 17569 42 42 17733 44 44 18176 43 43 + 18597 40 40 18597 41 41 18952 37 37 18996 39 39 19395 38 38 + 19760 35 35 19788 36 36 20492 32 32 20492 33 33 20498 30 30 + 20536 34 34 20833 29 29 20871 28 28 20891 31 31 21180 27 27 + 21752 23 23 21830 26 26 22025 21 21 22087 22 22 22087 24 24 + 22087 25 25 22278 20 20 22316 19 19 22549 15 15 22557 14 14 + 22573 17 17 22573 18 18 22706 10 10 22796 11 11 22796 12 12 + 22796 13 13 22796 16 16 23022 4 4 23042 2 2 23042 3 3 23042 9 9 + 23155 1 1 23155 5 5 23155 6 6 23155 7 7 23155 8 8} + +do_execsql_test 5.3.8.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {{} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0} + +do_execsql_test 5.3.8.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {{} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1} + +do_execsql_test 5.3.9.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( ORDER BY a NULLS LAST + RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {{} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 870 158 0 + 870 158 0 870 158 0 870 158 0 870 158 0 870 158 0 870 158 0 + 870 158 0 870 158 0 899 113 9 899 113 9 899 113 9 899 113 9 + 899 113 9 899 113 9 899 113 9 899 113 16 899 113 16 899 113 16 + 899 113 16 899 113 16 899 113 16 899 113 16 899 113 16 899 113 16 + 979 102 44 979 102 44 979 102 44 979 102 44 979 102 44 979 102 49 + 979 102 49 979 102 49 979 102 49 979 102 49 979 102 49 979 102 49 + 979 102 56 979 102 56 979 102 56 979 102 56 979 102 56 979 102 56 + 979 102 62 979 102 62 979 102 62 979 102 62 979 102 62 979 102 62 + 979 102 62 979 102 62 979 102 62 979 102 62 979 102 62 979 102 62 + 979 102 62 979 102 75 979 102 75 979 102 75 979 102 75 979 102 75 + 979 102 75 979 102 75 979 102 75 979 113 25 979 113 25 979 113 25 + 979 113 25 979 113 25 979 113 25 979 113 25 979 113 25 979 113 33 + 979 113 33 979 113 33 979 113 33 979 113 33 979 113 33 979 113 33 + 979 113 33 979 113 33 979 113 33 979 113 33} + +do_execsql_test 5.3.9.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( ORDER BY a NULLS LAST + RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {{} 84 11 {} 84 11 {} 84 11 {} 84 11 {} 84 11 {} 84 11 + 2050 75 10 2050 75 10 2050 75 10 2050 75 10 2050 75 10 2050 75 10 + 2050 75 10 2050 75 10 2050 75 10 4997 68 9 4997 68 9 4997 68 9 + 4997 68 9 4997 68 9 4997 68 9 4997 68 9 7337 59 8 7337 59 8 + 7337 59 8 7337 59 8 7337 59 8 7337 59 8 7337 59 8 7337 59 8 + 7337 59 8 10450 51 7 10450 51 7 10450 51 7 10450 51 7 10450 51 7 + 10450 51 7 10450 51 7 10450 51 7 11714 40 6 11714 40 6 11714 40 6 + 11714 40 6 11714 40 6 11714 40 6 11714 40 6 11714 40 6 11714 40 6 + 11714 40 6 11714 40 6 12676 35 5 12676 35 5 12676 35 5 12676 35 5 + 12676 35 5 14195 28 4 14195 28 4 14195 28 4 14195 28 4 14195 28 4 + 14195 28 4 14195 28 4 15999 22 3 15999 22 3 15999 22 3 15999 22 3 + 15999 22 3 15999 22 3 17365 9 2 17365 9 2 17365 9 2 17365 9 2 + 17365 9 2 17365 9 2 17365 9 2 17365 9 2 17365 9 2 17365 9 2 + 17365 9 2 17365 9 2 17365 9 2 20846 1 1 20846 1 1 20846 1 1 + 20846 1 1 20846 1 1 20846 1 1 20846 1 1 20846 1 1} + +do_execsql_test 5.3.10.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( PARTITION BY coalesce(a, '') + RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {{} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0} + +do_execsql_test 5.3.10.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( PARTITION BY coalesce(a, '') + RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {{} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1} + +do_execsql_test 5.3.11.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( ORDER BY a NULLS LAST GROUPS 6 PRECEDING EXCLUDE GROUP ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {{} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 934 158 27 934 158 27 934 158 27 934 158 27 934 158 27 + 934 158 27 934 158 27 934 223 8 934 223 8 934 223 8 934 223 8 + 934 223 8 934 223 8 934 223 8 934 223 8 934 223 8 934 223 8 + 934 223 8 934 223 8 934 223 8 934 223 21 934 223 21 934 223 21 + 934 223 21 934 223 21 934 223 21 959 102 50 959 102 50 959 102 50 + 959 102 50 959 102 50 959 102 50 959 102 50 959 102 50 959 102 50 + 959 102 50 959 102 50 959 102 50 959 102 50 959 102 50 959 102 50 + 959 102 50 959 102 50 959 113 39 959 113 39 959 113 39 959 113 39 + 959 113 39 959 113 39 959 113 39 959 113 39 959 113 39 959 113 39 + 959 113 39 959 158 34 959 158 34 959 158 34 959 158 34 959 158 34 + 979 102 46 979 102 46 979 102 46 979 102 46 979 102 46 979 102 46 + 979 102 46 979 102 47 979 102 47 979 102 47 979 102 47 979 102 47 + 979 102 47 979 102 47 979 102 47 979 102 47 979 102 49 979 102 49 + 979 102 49 979 102 49 979 102 49 979 102 49} + +do_execsql_test 5.3.11.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( ORDER BY a NULLS LAST GROUPS 6 PRECEDING EXCLUDE GROUP ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {{} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + 2309 9 2 2309 9 2 2309 9 2 2309 9 2 2309 9 2 2309 9 2 2309 9 2 + 2309 9 2 2309 9 2 2309 9 2 2309 9 2 2309 9 2 2309 9 2 5790 22 3 + 5790 22 3 5790 22 3 5790 22 3 5790 22 3 5790 22 3 7156 28 4 + 7156 28 4 7156 28 4 7156 28 4 7156 28 4 7156 28 4 7156 28 4 + 8960 35 5 8960 35 5 8960 35 5 8960 35 5 8960 35 5 10028 68 9 + 10028 68 9 10028 68 9 10028 68 9 10028 68 9 10028 68 9 10028 68 9 + 10396 59 8 10396 59 8 10396 59 8 10396 59 8 10396 59 8 10396 59 8 + 10396 59 8 10396 59 8 10396 59 8 10479 40 6 10479 40 6 10479 40 6 + 10479 40 6 10479 40 6 10479 40 6 10479 40 6 10479 40 6 10479 40 6 + 10479 40 6 10479 40 6 11002 75 10 11002 75 10 11002 75 10 + 11002 75 10 11002 75 10 11002 75 10 11002 75 10 11002 75 10 + 11002 75 10 11441 51 7 11441 51 7 11441 51 7 11441 51 7 + 11441 51 7 11441 51 7 11441 51 7 11441 51 7 12145 84 11 + 12145 84 11 12145 84 11 12145 84 11 12145 84 11 12145 84 11} + +do_execsql_test 5.3.12.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( ORDER BY c NULLS LAST RANGE BETWEEN 6 PRECEDING AND 7 FOLLOWING EXCLUDE GROUP ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {{} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 158 158 1 160 160 1 160 160 1 223 223 1 224 224 1 + 238 234 2 239 234 2 239 238 2 252 250 2 256 252 2 257 247 4 + 257 247 4 257 250 3 335 330 2 336 330 2 336 335 2 354 354 1 + 354 354 1 355 355 1 398 393 3 398 393 3 399 393 3 399 398 2 + 399 398 2 572 572 1 574 574 1 633 629 2 634 627 3 634 627 3 + 634 627 3 634 629 3 667 667 1 670 667 2 671 667 2 671 670 2 + 671 670 2 711 711 1 711 711 1 716 705 2 726 726 1 730 730 1 + 762 762 1 762 762 1 762 762 1 768 759 3 792 790 2 792 790 2 + 794 786 3 794 786 3 844 839 4 845 839 4 845 839 4 845 840 3 + 845 840 3 934 934 1 934 934 1 934 934 1 938 929 3 959 959 1 + 963 963 1} + +do_execsql_test 5.3.12.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( ORDER BY c NULLS LAST RANGE BETWEEN 6 PRECEDING AND 7 FOLLOWING EXCLUDE GROUP ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {{} 1 1 {} 2 2 {} 2 2 {} 4 3 {} 5 4 {} 6 5 {} 6 5 {} 8 6 + {} 9 7 {} 10 8 {} 14 12 {} 15 13 {} 19 17 {} 20 18 {} 21 19 + {} 23 21 {} 25 23 {} 27 25 {} 27 25 {} 34 29 {} 35 30 {} 36 31 + {} 37 32 {} 38 33 {} 38 33 {} 40 34 {} 41 35 {} 42 36 {} 43 37 + {} 43 37 {} 50 42 {} 56 47 {} 60 51 {} 61 52 {} 62 53 {} 64 55 + {} 64 55 {} 66 56 {} 67 57 {} 68 58 {} 69 59 {} 70 60 {} 71 61 + {} 72 62 {} 78 67 {} 78 67 {} 78 67 {} 81 68 {} 82 69 {} 83 70 + {} 85 72 {} 85 72 {} 89 75 223 11 9 239 12 10 239 13 11 + 257 18 16 335 22 20 335 24 22 504 16 14 504 17 15 671 52 44 + 671 52 44 705 58 49 710 26 24 711 57 48 711 59 50 759 63 54 + 786 32 28 786 32 28 798 29 26 798 29 26 845 73 63 845 73 63 + 929 84 71 959 88 74 963 87 73 1260 46 39 1260 46 39 1334 51 43 + 1334 55 46 1584 31 27 1678 77 66 1885 48 40 1891 45 38 1891 49 41 + 2005 54 45 2523 75 64 2523 76 65} + +do_execsql_test 5.3.13.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( ORDER BY c NULLS LAST RANGE BETWEEN 0 PRECEDING AND 0 FOLLOWING EXCLUDE GROUP ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {{} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0 + {} {} 0 {} {} 0 {} {} 0 {} {} 0 {} {} 0} + +do_execsql_test 5.3.13.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( ORDER BY c NULLS LAST RANGE BETWEEN 0 PRECEDING AND 0 FOLLOWING EXCLUDE GROUP ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {{} 1 1 {} 2 2 {} 2 2 {} 4 3 {} 5 4 {} 6 5 {} 6 5 {} 8 6 + {} 9 7 {} 10 8 {} 11 9 {} 12 10 {} 13 11 {} 14 12 {} 15 13 + {} 16 14 {} 17 15 {} 18 16 {} 19 17 {} 20 18 {} 21 19 {} 22 20 + {} 23 21 {} 24 22 {} 25 23 {} 26 24 {} 27 25 {} 27 25 {} 29 26 + {} 29 26 {} 31 27 {} 32 28 {} 32 28 {} 34 29 {} 35 30 {} 36 31 + {} 37 32 {} 38 33 {} 38 33 {} 40 34 {} 41 35 {} 42 36 {} 43 37 + {} 43 37 {} 45 38 {} 46 39 {} 46 39 {} 48 40 {} 49 41 {} 50 42 + {} 51 43 {} 52 44 {} 52 44 {} 54 45 {} 55 46 {} 56 47 {} 57 48 + {} 58 49 {} 59 50 {} 60 51 {} 61 52 {} 62 53 {} 63 54 {} 64 55 + {} 64 55 {} 66 56 {} 67 57 {} 68 58 {} 69 59 {} 70 60 {} 71 61 + {} 72 62 {} 73 63 {} 73 63 {} 75 64 {} 76 65 {} 77 66 {} 78 67 + {} 78 67 {} 78 67 {} 81 68 {} 82 69 {} 83 70 {} 84 71 {} 85 72 + {} 85 72 {} 87 73 {} 88 74 {} 89 75} + +do_execsql_test 5.3.14.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( ORDER BY c NULLS LAST, b NULLS LAST, a NULLS LAST + ROWS BETWEEN 6 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {963 929 6 979 102 82 979 102 82 979 102 82 979 102 82 979 102 82 + 979 102 83 979 113 80 979 113 81 979 113 82 979 133 79 979 148 78 + 979 158 76 979 158 77 979 160 76 979 208 75 979 223 74 979 224 73 + 979 234 72 979 238 71 979 239 70 979 247 69 979 250 68 979 252 67 + 979 256 66 979 257 65 979 295 64 979 309 63 979 330 63 979 335 61 + 979 336 60 979 346 59 979 354 58 979 355 56 979 355 58 979 393 55 + 979 393 56 979 398 54 979 399 52 979 399 53 979 412 52 979 421 51 + 979 430 50 979 443 49 979 480 47 979 480 48 979 572 46 979 574 46 + 979 607 44 979 618 42 979 618 43 979 627 41 979 629 40 979 629 40 + 979 633 39 979 634 38 979 652 37 979 660 36 979 667 34 979 667 35 + 979 670 34 979 671 33 979 683 32 979 705 31 979 711 30 979 716 29 + 979 726 28 979 730 27 979 759 26 979 762 25 979 768 23 979 768 24 + 979 777 22 979 786 21 979 790 20 979 792 19 979 794 18 979 805 17 + 979 822 16 979 839 15 979 839 15 979 840 13 979 844 12 979 845 11 + 979 870 8 979 870 9 979 870 10 979 899 8 979 911 7} + +do_execsql_test 5.3.14.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( ORDER BY c NULLS LAST, b NULLS LAST, a NULLS LAST + ROWS BETWEEN 6 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {2851 89 89 3778 88 88 4681 87 87 5556 83 83 5574 82 82 5586 81 81 + 5640 84 84 5640 85 85 5640 86 86 7324 80 80 8123 77 77 8129 73 73 + 8129 74 74 8163 78 78 8163 79 79 8940 71 71 8968 75 75 8968 76 76 + 9727 66 66 9745 69 69 9745 70 70 9745 72 72 10504 65 65 + 10504 67 67 10504 68 68 11215 64 64 11844 62 62 11920 63 63 + 13274 60 60 13274 61 61 13897 58 58 13903 57 57 13925 56 56 + 13937 55 55 13941 59 59 15203 53 53 15241 54 54 15832 52 52 + 17100 48 48 17104 46 46 17104 47 47 17106 45 45 17126 49 49 + 17126 50 50 17126 51 51 17569 42 42 17733 44 44 18176 43 43 + 18597 40 40 18597 41 41 18952 37 37 18996 39 39 19395 38 38 + 19760 35 35 19788 36 36 20492 32 32 20492 33 33 20498 30 30 + 20536 34 34 20833 29 29 20871 28 28 20891 31 31 21180 27 27 + 21752 23 23 21830 26 26 22025 21 21 22087 22 22 22087 24 24 + 22087 25 25 22278 20 20 22316 19 19 22549 15 15 22557 14 14 + 22573 17 17 22573 18 18 22706 10 10 22796 11 11 22796 12 12 + 22796 13 13 22796 16 16 23022 4 4 23042 2 2 23042 3 3 23042 9 9 + 23155 1 1 23155 5 5 23155 6 6 23155 7 7 23155 8 8} + +do_execsql_test 5.4.1.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE TIES ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {102 102 1 113 113 1 113 113 1 133 133 1 148 148 1 158 158 0 + 158 158 1 160 160 1 208 208 1 223 223 1 224 224 1 234 234 1 + 238 238 1 239 239 1 247 247 1 250 250 1 252 252 1 256 256 1 + 257 257 1 295 295 1 309 309 1 330 330 1 335 335 1 336 336 1 + 346 346 1 354 354 1 355 355 0 355 355 1 393 393 1 393 393 1 + 398 398 1 399 399 0 399 399 1 412 412 1 421 421 1 430 430 1 + 443 443 1 480 480 1 480 480 1 572 572 1 574 574 1 607 607 1 + 618 618 1 618 618 1 627 627 1 629 629 0 629 629 1 633 633 1 + 634 634 1 652 652 1 660 660 1 667 667 0 667 667 1 670 670 1 + 671 671 1 683 683 1 705 705 1 711 711 1 716 716 1 726 726 1 + 730 730 1 759 759 1 762 762 1 768 768 1 768 768 1 777 777 1 + 786 786 1 790 790 1 792 792 1 794 794 1 805 805 1 822 822 1 + 839 839 1 839 839 1 840 840 1 844 844 1 845 845 1 870 870 0 + 870 870 1 870 870 1 899 899 1 911 911 1 929 929 1 934 934 1 + 938 938 1 938 938 1 959 959 1 963 963 1 979 979 1} + +do_execsql_test 5.4.1.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE TIES ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {{} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + 113 1 1 113 1 1 133 1 1 223 1 1 239 1 1 247 1 1 257 1 1 + 295 1 1 309 1 1 335 1 1 355 1 1 355 1 1 393 1 1 393 1 1 + 399 1 1 399 1 1 421 1 1 443 1 1 607 1 1 627 1 1 629 1 1 + 629 1 1 633 1 1 667 1 1 667 1 1 671 1 1 683 1 1 705 1 1 + 711 1 1 759 1 1 777 1 1 805 1 1 839 1 1 839 1 1 845 1 1 + 899 1 1 911 1 1 929 1 1 959 1 1 963 1 1 979 1 1} + +do_execsql_test 5.4.2.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( ORDER BY a NULLS FIRST + RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE TIES ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {113 113 1 234 234 1 257 257 1 336 336 1 354 354 1 768 768 1 + 839 839 1 839 839 1 899 113 10 899 113 10 899 113 10 899 113 10 + 899 113 10 899 113 10 899 113 10 899 113 17 899 113 17 899 113 17 + 899 113 17 899 113 17 899 113 17 899 113 17 899 899 1 963 113 17 + 979 102 34 979 102 45 979 102 45 979 102 45 979 102 45 979 102 45 + 979 102 50 979 102 50 979 102 50 979 102 50 979 102 50 979 102 50 + 979 102 50 979 102 57 979 102 57 979 102 57 979 102 57 979 102 57 + 979 102 57 979 102 63 979 102 63 979 102 63 979 102 63 979 102 63 + 979 102 63 979 102 63 979 102 63 979 102 63 979 102 63 979 102 63 + 979 102 63 979 102 63 979 102 76 979 102 76 979 102 76 979 102 76 + 979 102 76 979 102 76 979 102 76 979 102 76 979 102 83 979 102 83 + 979 102 83 979 102 83 979 102 83 979 102 83 979 113 17 979 113 26 + 979 113 26 979 113 26 979 113 26 979 113 26 979 113 26 979 113 26 + 979 113 26 979 113 34 979 113 34 979 113 34 979 113 34 979 113 34 + 979 113 34 979 113 34 979 113 34 979 113 34 979 113 34} + +do_execsql_test 5.4.2.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( ORDER BY a NULLS FIRST + RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE TIES ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {{} 81 11 {} 81 11 {} 81 11 {} 81 11 113 81 11 257 81 11 + 839 81 11 839 81 11 899 81 11 2947 74 10 2947 74 10 2947 74 10 + 3368 74 10 3390 74 10 3618 74 10 3752 74 10 5287 65 9 5287 65 9 + 5287 65 9 5287 65 9 5420 65 9 5642 65 9 5970 65 9 6250 65 9 + 6266 65 9 8400 57 8 8400 57 8 8400 57 8 8400 57 8 8400 57 8 + 8400 57 8 8735 57 8 9329 57 8 9664 46 7 9664 46 7 9664 46 7 + 9664 46 7 9664 46 7 9664 46 7 9664 46 7 9664 46 7 9664 46 7 + 9959 46 7 10331 46 7 10626 41 6 10626 41 6 10739 41 6 11255 41 6 + 11403 41 6 12145 34 5 12145 34 5 12145 34 5 12145 34 5 12145 34 5 + 12990 34 5 13104 34 5 13949 28 4 13949 28 4 13949 28 4 13949 28 4 + 14556 28 4 14708 28 4 15315 15 3 15315 15 3 15315 15 3 15315 15 3 + 15315 15 3 15315 15 3 15562 15 3 15708 15 3 15708 15 3 15714 15 3 + 15948 15 3 16020 15 3 16026 15 3 18796 7 2 18796 7 2 18796 7 2 + 19019 7 2 19035 7 2 19105 7 2 19423 7 2 19707 7 2 21105 1 1 + 21105 1 1 21460 1 1 21504 1 1 21734 1 1 21772 1 1} + +do_execsql_test 5.4.3.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( PARTITION BY coalesce(a, '') + RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE TIES ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {102 102 1 113 113 1 113 113 1 133 133 1 148 148 1 158 158 0 + 158 158 1 160 160 1 208 208 1 223 223 1 224 224 1 234 234 1 + 238 238 1 239 239 1 247 247 1 250 250 1 252 252 1 256 256 1 + 257 257 1 295 295 1 309 309 1 330 330 1 335 335 1 336 336 1 + 346 346 1 354 354 1 355 355 0 355 355 1 393 393 1 393 393 1 + 398 398 1 399 399 0 399 399 1 412 412 1 421 421 1 430 430 1 + 443 443 1 480 480 1 480 480 1 572 572 1 574 574 1 607 607 1 + 618 618 1 618 618 1 627 627 1 629 629 0 629 629 1 633 633 1 + 634 634 1 652 652 1 660 660 1 667 667 0 667 667 1 670 670 1 + 671 671 1 683 683 1 705 705 1 711 711 1 716 716 1 726 726 1 + 730 730 1 759 759 1 762 762 1 768 768 1 768 768 1 777 777 1 + 786 786 1 790 790 1 792 792 1 794 794 1 805 805 1 822 822 1 + 839 839 1 839 839 1 840 840 1 844 844 1 845 845 1 870 870 0 + 870 870 1 870 870 1 899 899 1 911 911 1 929 929 1 934 934 1 + 938 938 1 938 938 1 959 959 1 963 963 1 979 979 1} + +do_execsql_test 5.4.3.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( PARTITION BY coalesce(a, '') + RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE TIES ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {{} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + 113 1 1 113 1 1 133 1 1 223 1 1 239 1 1 247 1 1 257 1 1 + 295 1 1 309 1 1 335 1 1 355 1 1 355 1 1 393 1 1 393 1 1 + 399 1 1 399 1 1 421 1 1 443 1 1 607 1 1 627 1 1 629 1 1 + 629 1 1 633 1 1 667 1 1 667 1 1 671 1 1 683 1 1 705 1 1 + 711 1 1 759 1 1 777 1 1 805 1 1 839 1 1 839 1 1 845 1 1 + 899 1 1 911 1 1 929 1 1 959 1 1 963 1 1 979 1 1} + +do_execsql_test 5.4.4.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( ORDER BY a NULLS FIRST GROUPS 6 PRECEDING EXCLUDE TIES ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {158 158 0 355 355 0 399 399 0 629 629 0 667 667 0 870 158 1 + 870 158 1 870 158 1 870 158 1 870 158 1 870 158 1 870 870 0 + 911 158 1 934 158 1 934 158 9 934 158 9 934 158 9 934 158 9 + 934 158 9 934 158 9 934 158 9 934 158 9 934 158 9 934 158 9 + 934 158 9 934 158 9 934 158 9 934 158 22 934 158 22 934 158 22 + 934 158 22 934 158 22 934 158 22 934 158 28 934 158 28 934 158 28 + 934 158 28 934 158 28 934 158 28 959 102 40 959 102 51 959 102 51 + 959 102 51 959 102 51 959 102 51 959 102 51 959 102 51 959 102 51 + 959 102 51 959 102 51 959 102 51 959 102 51 959 102 51 959 102 51 + 959 102 51 959 113 35 959 113 40 959 113 40 959 113 40 959 113 40 + 959 113 40 959 113 40 959 113 40 959 113 40 959 113 40 959 113 40 + 959 158 28 959 158 35 959 158 35 959 158 35 959 158 35 963 102 51 + 979 102 47 979 102 47 979 102 47 979 102 47 979 102 47 979 102 47 + 979 102 47 979 102 48 979 102 48 979 102 48 979 102 48 979 102 48 + 979 102 48 979 102 48 979 102 48 979 102 48 979 102 51} + +do_execsql_test 5.4.4.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( ORDER BY a NULLS FIRST GROUPS 6 PRECEDING EXCLUDE TIES ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {{} 1 1 {} 1 1 355 1 1 399 1 1 629 1 1 667 1 1 2050 7 2 + 2050 7 2 2050 7 2 2273 7 2 2289 7 2 2359 7 2 2677 7 2 2961 7 2 + 4359 15 3 4359 15 3 4359 15 3 4359 15 3 4359 15 3 4359 15 3 + 4606 15 3 4752 15 3 4752 15 3 4758 15 3 4992 15 3 5064 15 3 + 5070 15 3 7840 28 4 7840 28 4 7840 28 4 7840 28 4 8447 28 4 + 8599 28 4 9206 34 5 9206 34 5 9206 34 5 9206 34 5 9206 34 5 + 10028 74 10 10028 74 10 10028 74 10 10051 34 5 10165 34 5 + 10396 65 9 10396 65 9 10396 65 9 10396 65 9 10449 74 10 + 10471 74 10 10529 65 9 10699 74 10 10751 65 9 10833 74 10 + 11002 81 11 11002 81 11 11002 81 11 11002 81 11 11010 41 6 + 11010 41 6 11079 65 9 11115 81 11 11123 41 6 11259 81 11 + 11359 65 9 11375 65 9 11441 57 8 11441 57 8 11441 57 8 11441 57 8 + 11441 57 8 11441 57 8 11639 41 6 11776 57 8 11787 41 6 + 11841 81 11 11841 81 11 11901 81 11 12370 57 8 12529 46 7 + 12529 46 7 12529 46 7 12529 46 7 12529 46 7 12529 46 7 12529 46 7 + 12529 46 7 12529 46 7 12824 46 7 13196 46 7} + +do_execsql_test 5.4.5.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( ORDER BY c NULLS FIRST RANGE BETWEEN 6 PRECEDING AND 7 FOLLOWING EXCLUDE TIES ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {102 102 1 113 113 1 113 113 1 133 133 1 148 148 1 160 158 1 + 160 158 2 160 158 2 208 208 1 224 223 2 224 223 2 239 234 3 + 239 234 3 239 234 3 252 247 3 257 247 5 257 247 5 257 250 4 + 257 252 3 295 295 1 309 309 1 336 330 3 336 330 3 336 330 3 + 346 346 1 355 354 1 355 354 2 355 354 2 399 393 3 399 393 3 + 399 393 3 399 393 4 399 393 4 412 412 1 421 421 1 430 430 1 + 443 443 1 480 480 1 480 480 1 574 572 2 574 572 2 607 607 1 + 618 618 1 618 618 1 634 627 3 634 627 4 634 627 4 634 627 4 + 634 629 3 652 652 1 667 660 2 671 667 2 671 667 3 671 667 3 + 671 667 3 683 683 1 711 705 2 716 705 3 716 711 2 730 726 2 + 730 726 2 762 759 2 768 759 4 768 762 2 768 762 2 777 777 1 + 792 786 3 794 786 4 794 786 4 794 790 3 805 805 1 822 822 1 + 845 839 4 845 839 4 845 839 5 845 839 5 845 839 5 870 870 0 + 870 870 1 870 870 1 899 899 1 911 911 1 934 929 2 938 929 4 + 938 934 2 938 934 2 963 959 2 963 959 2 979 979 1} + +do_execsql_test 5.4.5.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( ORDER BY c NULLS FIRST RANGE BETWEEN 6 PRECEDING AND 7 FOLLOWING EXCLUDE TIES ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {{} 1 1 {} 5 4 {} 6 5 {} 6 5 {} 8 6 {} 9 7 {} 25 23 {} 34 29 + {} 36 31 {} 38 33 {} 38 33 {} 40 34 {} 41 35 {} 43 37 {} 43 37 + {} 50 42 {} 60 51 {} 61 52 {} 64 55 {} 64 55 {} 67 57 {} 68 58 + {} 69 59 {} 70 60 {} 72 62 {} 78 67 {} 78 67 {} 78 67 {} 85 72 + {} 85 72 113 2 2 113 2 2 133 4 3 223 10 8 223 11 9 239 12 10 + 239 13 11 239 14 12 247 15 13 257 18 16 257 19 17 295 20 18 + 309 21 19 335 22 20 335 23 21 335 24 22 355 27 25 355 27 25 + 421 35 30 443 37 32 504 16 14 504 17 15 607 42 36 683 56 47 + 710 26 24 711 59 50 759 62 53 759 63 54 777 66 56 805 71 61 + 899 81 68 911 82 69 929 83 70 929 84 71 979 89 75 1185 32 28 + 1185 32 28 1191 29 26 1191 29 26 1334 51 43 1338 52 44 1338 52 44 + 1416 57 48 1416 58 49 1584 31 27 1684 73 63 1684 73 63 1889 46 39 + 1889 46 39 1891 49 41 1922 87 73 1922 88 74 2005 54 45 2005 55 46 + 2518 45 38 2518 48 40 2523 75 64 2523 76 65 2523 77 66} + +do_execsql_test 5.4.6.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( ORDER BY c NULLS FIRST RANGE BETWEEN 0 PRECEDING AND 0 FOLLOWING EXCLUDE TIES ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {102 102 1 113 113 1 113 113 1 133 133 1 148 148 1 158 158 0 + 158 158 1 160 160 1 208 208 1 223 223 1 224 224 1 234 234 1 + 238 238 1 239 239 1 247 247 1 250 250 1 252 252 1 256 256 1 + 257 257 1 295 295 1 309 309 1 330 330 1 335 335 1 336 336 1 + 346 346 1 354 354 1 355 355 0 355 355 1 393 393 1 393 393 1 + 398 398 1 399 399 0 399 399 1 412 412 1 421 421 1 430 430 1 + 443 443 1 480 480 1 480 480 1 572 572 1 574 574 1 607 607 1 + 618 618 1 618 618 1 627 627 1 629 629 0 629 629 1 633 633 1 + 634 634 1 652 652 1 660 660 1 667 667 0 667 667 1 670 670 1 + 671 671 1 683 683 1 705 705 1 711 711 1 716 716 1 726 726 1 + 730 730 1 759 759 1 762 762 1 768 768 1 768 768 1 777 777 1 + 786 786 1 790 790 1 792 792 1 794 794 1 805 805 1 822 822 1 + 839 839 1 839 839 1 840 840 1 844 844 1 845 845 1 870 870 0 + 870 870 1 870 870 1 899 899 1 911 911 1 929 929 1 934 934 1 + 938 938 1 938 938 1 959 959 1 963 963 1 979 979 1} + +do_execsql_test 5.4.6.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( ORDER BY c NULLS FIRST RANGE BETWEEN 0 PRECEDING AND 0 FOLLOWING EXCLUDE TIES ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {{} 1 1 {} 5 4 {} 6 5 {} 6 5 {} 8 6 {} 9 7 {} 11 9 {} 12 10 + {} 13 11 {} 16 14 {} 17 15 {} 18 16 {} 22 20 {} 24 22 {} 25 23 + {} 26 24 {} 31 27 {} 34 29 {} 36 31 {} 38 33 {} 38 33 {} 40 34 + {} 41 35 {} 43 37 {} 43 37 {} 49 41 {} 50 42 {} 51 43 {} 54 45 + {} 59 50 {} 60 51 {} 61 52 {} 63 54 {} 64 55 {} 64 55 {} 67 57 + {} 68 58 {} 69 59 {} 70 60 {} 72 62 {} 75 64 {} 76 65 {} 78 67 + {} 78 67 {} 78 67 {} 84 71 {} 85 72 {} 85 72 113 2 2 113 2 2 + 133 4 3 223 10 8 239 14 12 247 15 13 257 19 17 295 20 18 + 309 21 19 335 23 21 355 27 25 355 27 25 393 29 26 393 29 26 + 399 32 28 399 32 28 421 35 30 443 37 32 607 42 36 627 45 38 + 629 46 39 629 46 39 633 48 40 667 52 44 667 52 44 671 55 46 + 683 56 47 705 57 48 711 58 49 759 62 53 777 66 56 805 71 61 + 839 73 63 839 73 63 845 77 66 899 81 68 911 82 69 929 83 70 + 959 87 73 963 88 74 979 89 75} + +do_execsql_test 5.4.7.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( ORDER BY c NULLS FIRST, b NULLS FIRST, a NULLS FIRST + ROWS BETWEEN 6 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 + 979 102 83 979 113 81 979 113 82 979 133 80 979 148 79 979 158 77 + 979 158 78 979 160 77 979 208 76 979 223 75 979 224 74 979 234 73 + 979 238 72 979 239 71 979 247 70 979 250 69 979 252 68 979 256 67 + 979 257 66 979 295 65 979 309 64 979 330 63 979 335 62 979 336 61 + 979 346 60 979 354 59 979 355 58 979 355 58 979 393 56 979 393 57 + 979 398 55 979 399 54 979 399 54 979 412 53 979 421 52 979 430 51 + 979 443 50 979 480 48 979 480 49 979 572 47 979 574 46 979 607 45 + 979 618 43 979 618 44 979 627 42 979 629 41 979 629 41 979 633 40 + 979 634 39 979 652 38 979 660 37 979 667 36 979 667 36 979 670 35 + 979 671 34 979 683 33 979 705 32 979 711 31 979 716 30 979 726 29 + 979 730 28 979 759 27 979 762 26 979 768 24 979 768 25 979 777 23 + 979 786 22 979 790 21 979 792 20 979 794 19 979 805 18 979 822 17 + 979 839 15 979 839 16 979 840 14 979 844 13 979 845 12 979 870 10 + 979 870 11 979 870 11 979 899 9 979 911 8 979 929 7} + +do_execsql_test 5.4.7.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( ORDER BY c NULLS FIRST, b NULLS FIRST, a NULLS FIRST + ROWS BETWEEN 6 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {3830 89 89 4741 88 88 5640 84 84 5640 85 85 5640 86 86 5640 87 87 + 6485 81 81 6485 82 82 6485 83 83 7324 80 80 8163 78 78 8163 79 79 + 8968 73 73 8968 74 74 8968 75 75 8968 76 76 8968 77 77 9745 69 69 + 9745 70 70 9745 71 71 9745 72 72 10504 65 65 10504 66 66 + 10504 67 67 10504 68 68 11215 64 64 11920 63 63 12603 62 62 + 13274 60 60 13274 61 61 13941 59 59 14608 55 55 14608 56 56 + 14608 57 57 14608 58 58 15241 54 54 15870 53 53 16499 52 52 + 17126 49 49 17126 50 50 17126 51 51 17733 44 44 17733 45 45 + 17733 46 46 17733 47 47 17733 48 48 18176 42 42 18176 43 43 + 18597 40 40 18597 41 41 18996 39 39 19395 37 37 19395 38 38 + 19788 36 36 20181 35 35 20536 34 34 20891 30 30 20891 31 31 + 20891 32 32 20891 33 33 21226 28 28 21226 29 29 21535 27 27 + 21830 26 26 22087 22 22 22087 23 23 22087 24 24 22087 25 25 + 22334 21 21 22573 17 17 22573 18 18 22573 19 19 22573 20 20 + 22796 11 11 22796 12 12 22796 13 13 22796 14 14 22796 15 15 + 22796 16 16 22929 10 10 23042 9 9 23155 1 1 23155 2 2 23155 3 3 + 23155 4 4 23155 5 5 23155 6 6 23155 7 7 23155 8 8} + +do_execsql_test 5.4.8.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE TIES ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {102 102 1 113 113 1 113 113 1 133 133 1 148 148 1 158 158 0 + 158 158 1 160 160 1 208 208 1 223 223 1 224 224 1 234 234 1 + 238 238 1 239 239 1 247 247 1 250 250 1 252 252 1 256 256 1 + 257 257 1 295 295 1 309 309 1 330 330 1 335 335 1 336 336 1 + 346 346 1 354 354 1 355 355 0 355 355 1 393 393 1 393 393 1 + 398 398 1 399 399 0 399 399 1 412 412 1 421 421 1 430 430 1 + 443 443 1 480 480 1 480 480 1 572 572 1 574 574 1 607 607 1 + 618 618 1 618 618 1 627 627 1 629 629 0 629 629 1 633 633 1 + 634 634 1 652 652 1 660 660 1 667 667 0 667 667 1 670 670 1 + 671 671 1 683 683 1 705 705 1 711 711 1 716 716 1 726 726 1 + 730 730 1 759 759 1 762 762 1 768 768 1 768 768 1 777 777 1 + 786 786 1 790 790 1 792 792 1 794 794 1 805 805 1 822 822 1 + 839 839 1 839 839 1 840 840 1 844 844 1 845 845 1 870 870 0 + 870 870 1 870 870 1 899 899 1 911 911 1 929 929 1 934 934 1 + 938 938 1 938 938 1 959 959 1 963 963 1 979 979 1} + +do_execsql_test 5.4.8.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE TIES ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {{} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + 113 1 1 113 1 1 133 1 1 223 1 1 239 1 1 247 1 1 257 1 1 + 295 1 1 309 1 1 335 1 1 355 1 1 355 1 1 393 1 1 393 1 1 + 399 1 1 399 1 1 421 1 1 443 1 1 607 1 1 627 1 1 629 1 1 + 629 1 1 633 1 1 667 1 1 667 1 1 671 1 1 683 1 1 705 1 1 + 711 1 1 759 1 1 777 1 1 805 1 1 839 1 1 839 1 1 845 1 1 + 899 1 1 911 1 1 929 1 1 959 1 1 963 1 1 979 1 1} + +do_execsql_test 5.4.9.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( ORDER BY a NULLS LAST + RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE TIES ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {158 158 0 355 355 0 399 399 0 629 629 0 667 667 0 870 113 1 + 870 158 1 870 158 1 870 158 1 870 158 1 870 158 1 870 158 1 + 870 158 1 870 870 0 899 113 10 899 113 10 899 113 10 899 113 10 + 899 113 10 899 113 10 899 113 10 899 113 17 899 113 17 899 113 17 + 899 113 17 899 113 17 899 113 17 899 113 17 899 158 1 963 113 17 + 979 102 34 979 102 45 979 102 45 979 102 45 979 102 45 979 102 45 + 979 102 50 979 102 50 979 102 50 979 102 50 979 102 50 979 102 50 + 979 102 50 979 102 57 979 102 57 979 102 57 979 102 57 979 102 57 + 979 102 57 979 102 63 979 102 63 979 102 63 979 102 63 979 102 63 + 979 102 63 979 102 63 979 102 63 979 102 63 979 102 63 979 102 63 + 979 102 63 979 102 63 979 102 76 979 102 76 979 102 76 979 102 76 + 979 102 76 979 102 76 979 102 76 979 102 76 979 113 17 979 113 26 + 979 113 26 979 113 26 979 113 26 979 113 26 979 113 26 979 113 26 + 979 113 26 979 113 34 979 113 34 979 113 34 979 113 34 979 113 34 + 979 113 34 979 113 34 979 113 34 979 113 34 979 113 34} + +do_execsql_test 5.4.9.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( ORDER BY a NULLS LAST + RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE TIES ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {{} 84 11 {} 84 11 355 84 11 399 84 11 629 84 11 667 84 11 + 2050 75 10 2050 75 10 2050 75 10 2050 75 10 2163 75 10 2307 75 10 + 2889 75 10 2889 75 10 2949 75 10 4997 68 9 4997 68 9 4997 68 9 + 5418 68 9 5440 68 9 5668 68 9 5802 68 9 7337 59 8 7337 59 8 + 7337 59 8 7337 59 8 7470 59 8 7692 59 8 8020 59 8 8300 59 8 + 8316 59 8 10450 51 7 10450 51 7 10450 51 7 10450 51 7 10450 51 7 + 10450 51 7 10785 51 7 11379 51 7 11714 40 6 11714 40 6 11714 40 6 + 11714 40 6 11714 40 6 11714 40 6 11714 40 6 11714 40 6 11714 40 6 + 12009 40 6 12381 40 6 12676 35 5 12676 35 5 12789 35 5 13305 35 5 + 13453 35 5 14195 28 4 14195 28 4 14195 28 4 14195 28 4 14195 28 4 + 15040 28 4 15154 28 4 15999 22 3 15999 22 3 15999 22 3 15999 22 3 + 16606 22 3 16758 22 3 17365 9 2 17365 9 2 17365 9 2 17365 9 2 + 17365 9 2 17365 9 2 17612 9 2 17758 9 2 17758 9 2 17764 9 2 + 17998 9 2 18070 9 2 18076 9 2 20846 1 1 20846 1 1 20846 1 1 + 21069 1 1 21085 1 1 21155 1 1 21473 1 1 21757 1 1} + +do_execsql_test 5.4.10.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( PARTITION BY coalesce(a, '') + RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE TIES ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {102 102 1 113 113 1 113 113 1 133 133 1 148 148 1 158 158 0 + 158 158 1 160 160 1 208 208 1 223 223 1 224 224 1 234 234 1 + 238 238 1 239 239 1 247 247 1 250 250 1 252 252 1 256 256 1 + 257 257 1 295 295 1 309 309 1 330 330 1 335 335 1 336 336 1 + 346 346 1 354 354 1 355 355 0 355 355 1 393 393 1 393 393 1 + 398 398 1 399 399 0 399 399 1 412 412 1 421 421 1 430 430 1 + 443 443 1 480 480 1 480 480 1 572 572 1 574 574 1 607 607 1 + 618 618 1 618 618 1 627 627 1 629 629 0 629 629 1 633 633 1 + 634 634 1 652 652 1 660 660 1 667 667 0 667 667 1 670 670 1 + 671 671 1 683 683 1 705 705 1 711 711 1 716 716 1 726 726 1 + 730 730 1 759 759 1 762 762 1 768 768 1 768 768 1 777 777 1 + 786 786 1 790 790 1 792 792 1 794 794 1 805 805 1 822 822 1 + 839 839 1 839 839 1 840 840 1 844 844 1 845 845 1 870 870 0 + 870 870 1 870 870 1 899 899 1 911 911 1 929 929 1 934 934 1 + 938 938 1 938 938 1 959 959 1 963 963 1 979 979 1} + +do_execsql_test 5.4.10.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( PARTITION BY coalesce(a, '') + RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE TIES ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {{} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 {} 1 1 + 113 1 1 113 1 1 133 1 1 223 1 1 239 1 1 247 1 1 257 1 1 + 295 1 1 309 1 1 335 1 1 355 1 1 355 1 1 393 1 1 393 1 1 + 399 1 1 399 1 1 421 1 1 443 1 1 607 1 1 627 1 1 629 1 1 + 629 1 1 633 1 1 667 1 1 667 1 1 671 1 1 683 1 1 705 1 1 + 711 1 1 759 1 1 777 1 1 805 1 1 839 1 1 839 1 1 845 1 1 + 899 1 1 911 1 1 929 1 1 959 1 1 963 1 1 979 1 1} + +do_execsql_test 5.4.11.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( ORDER BY a NULLS LAST GROUPS 6 PRECEDING EXCLUDE TIES ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {223 223 1 239 239 1 309 309 1 572 572 1 627 627 1 870 870 1 + 911 911 1 934 158 22 934 158 28 934 158 28 934 158 28 934 158 28 + 934 158 28 934 158 28 934 223 9 934 223 9 934 223 9 934 223 9 + 934 223 9 934 223 9 934 223 9 934 223 9 934 223 9 934 223 9 + 934 223 9 934 223 9 934 223 9 934 223 22 934 223 22 934 223 22 + 934 223 22 934 223 22 934 934 1 959 102 40 959 102 51 959 102 51 + 959 102 51 959 102 51 959 102 51 959 102 51 959 102 51 959 102 51 + 959 102 51 959 102 51 959 102 51 959 102 51 959 102 51 959 102 51 + 959 102 51 959 113 35 959 113 40 959 113 40 959 113 40 959 113 40 + 959 113 40 959 113 40 959 113 40 959 113 40 959 113 40 959 113 40 + 959 158 28 959 158 35 959 158 35 959 158 35 959 158 35 963 102 51 + 979 102 47 979 102 47 979 102 47 979 102 47 979 102 47 979 102 47 + 979 102 47 979 102 48 979 102 48 979 102 48 979 102 48 979 102 48 + 979 102 48 979 102 48 979 102 48 979 102 48 979 102 49 979 102 49 + 979 102 49 979 102 49 979 102 49 979 102 49 979 102 51} + +do_execsql_test 5.4.11.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( ORDER BY a NULLS LAST GROUPS 6 PRECEDING EXCLUDE TIES ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {{} 1 1 {} 1 1 {} 1 1 223 1 1 239 1 1 309 1 1 627 1 1 911 1 1 + 2309 9 2 2309 9 2 2309 9 2 2309 9 2 2309 9 2 2309 9 2 2556 9 2 + 2702 9 2 2702 9 2 2708 9 2 2942 9 2 3014 9 2 3020 9 2 5790 22 3 + 5790 22 3 5790 22 3 5790 22 3 6397 22 3 6549 22 3 7156 28 4 + 7156 28 4 7156 28 4 7156 28 4 7156 28 4 8001 28 4 8115 28 4 + 8960 35 5 8960 35 5 9073 35 5 9589 35 5 9737 35 5 10028 68 9 + 10028 68 9 10028 68 9 10396 59 8 10396 59 8 10396 59 8 10396 59 8 + 10449 68 9 10471 68 9 10479 40 6 10479 40 6 10479 40 6 10479 40 6 + 10479 40 6 10479 40 6 10479 40 6 10479 40 6 10479 40 6 10529 59 8 + 10699 68 9 10751 59 8 10774 40 6 10833 68 9 11002 75 10 + 11002 75 10 11002 75 10 11002 75 10 11079 59 8 11115 75 10 + 11146 40 6 11259 75 10 11359 59 8 11375 59 8 11441 51 7 + 11441 51 7 11441 51 7 11441 51 7 11441 51 7 11441 51 7 11776 51 7 + 11841 75 10 11841 75 10 11901 75 10 12145 84 11 12145 84 11 + 12370 51 7 12500 84 11 12544 84 11 12774 84 11 12812 84 11} + +do_execsql_test 5.4.12.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( ORDER BY c NULLS LAST RANGE BETWEEN 6 PRECEDING AND 7 FOLLOWING EXCLUDE TIES ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {102 102 1 113 113 1 113 113 1 133 133 1 148 148 1 160 158 1 + 160 158 2 160 158 2 208 208 1 224 223 2 224 223 2 239 234 3 + 239 234 3 239 234 3 252 247 3 257 247 5 257 247 5 257 250 4 + 257 252 3 295 295 1 309 309 1 336 330 3 336 330 3 336 330 3 + 346 346 1 355 354 1 355 354 2 355 354 2 399 393 3 399 393 3 + 399 393 3 399 393 4 399 393 4 412 412 1 421 421 1 430 430 1 + 443 443 1 480 480 1 480 480 1 574 572 2 574 572 2 607 607 1 + 618 618 1 618 618 1 634 627 3 634 627 4 634 627 4 634 627 4 + 634 629 3 652 652 1 667 660 2 671 667 2 671 667 3 671 667 3 + 671 667 3 683 683 1 711 705 2 716 705 3 716 711 2 730 726 2 + 730 726 2 762 759 2 768 759 4 768 762 2 768 762 2 777 777 1 + 792 786 3 794 786 4 794 786 4 794 790 3 805 805 1 822 822 1 + 845 839 4 845 839 4 845 839 5 845 839 5 845 839 5 870 870 0 + 870 870 1 870 870 1 899 899 1 911 911 1 934 929 2 938 929 4 + 938 934 2 938 934 2 963 959 2 963 959 2 979 979 1} + +do_execsql_test 5.4.12.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( ORDER BY c NULLS LAST RANGE BETWEEN 6 PRECEDING AND 7 FOLLOWING EXCLUDE TIES ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {{} 1 1 {} 5 4 {} 6 5 {} 6 5 {} 8 6 {} 9 7 {} 25 23 {} 34 29 + {} 36 31 {} 38 33 {} 38 33 {} 40 34 {} 41 35 {} 43 37 {} 43 37 + {} 50 42 {} 60 51 {} 61 52 {} 64 55 {} 64 55 {} 67 57 {} 68 58 + {} 69 59 {} 70 60 {} 72 62 {} 78 67 {} 78 67 {} 78 67 {} 85 72 + {} 85 72 113 2 2 113 2 2 133 4 3 223 10 8 223 11 9 239 12 10 + 239 13 11 239 14 12 247 15 13 257 18 16 257 19 17 295 20 18 + 309 21 19 335 22 20 335 23 21 335 24 22 355 27 25 355 27 25 + 421 35 30 443 37 32 504 16 14 504 17 15 607 42 36 683 56 47 + 710 26 24 711 59 50 759 62 53 759 63 54 777 66 56 805 71 61 + 899 81 68 911 82 69 929 83 70 929 84 71 979 89 75 1185 32 28 + 1185 32 28 1191 29 26 1191 29 26 1334 51 43 1338 52 44 1338 52 44 + 1416 57 48 1416 58 49 1584 31 27 1684 73 63 1684 73 63 1889 46 39 + 1889 46 39 1891 49 41 1922 87 73 1922 88 74 2005 54 45 2005 55 46 + 2518 45 38 2518 48 40 2523 75 64 2523 76 65 2523 77 66} + +do_execsql_test 5.4.13.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( ORDER BY c NULLS LAST RANGE BETWEEN 0 PRECEDING AND 0 FOLLOWING EXCLUDE TIES ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {102 102 1 113 113 1 113 113 1 133 133 1 148 148 1 158 158 0 + 158 158 1 160 160 1 208 208 1 223 223 1 224 224 1 234 234 1 + 238 238 1 239 239 1 247 247 1 250 250 1 252 252 1 256 256 1 + 257 257 1 295 295 1 309 309 1 330 330 1 335 335 1 336 336 1 + 346 346 1 354 354 1 355 355 0 355 355 1 393 393 1 393 393 1 + 398 398 1 399 399 0 399 399 1 412 412 1 421 421 1 430 430 1 + 443 443 1 480 480 1 480 480 1 572 572 1 574 574 1 607 607 1 + 618 618 1 618 618 1 627 627 1 629 629 0 629 629 1 633 633 1 + 634 634 1 652 652 1 660 660 1 667 667 0 667 667 1 670 670 1 + 671 671 1 683 683 1 705 705 1 711 711 1 716 716 1 726 726 1 + 730 730 1 759 759 1 762 762 1 768 768 1 768 768 1 777 777 1 + 786 786 1 790 790 1 792 792 1 794 794 1 805 805 1 822 822 1 + 839 839 1 839 839 1 840 840 1 844 844 1 845 845 1 870 870 0 + 870 870 1 870 870 1 899 899 1 911 911 1 929 929 1 934 934 1 + 938 938 1 938 938 1 959 959 1 963 963 1 979 979 1} + +do_execsql_test 5.4.13.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( ORDER BY c NULLS LAST RANGE BETWEEN 0 PRECEDING AND 0 FOLLOWING EXCLUDE TIES ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {{} 1 1 {} 5 4 {} 6 5 {} 6 5 {} 8 6 {} 9 7 {} 11 9 {} 12 10 + {} 13 11 {} 16 14 {} 17 15 {} 18 16 {} 22 20 {} 24 22 {} 25 23 + {} 26 24 {} 31 27 {} 34 29 {} 36 31 {} 38 33 {} 38 33 {} 40 34 + {} 41 35 {} 43 37 {} 43 37 {} 49 41 {} 50 42 {} 51 43 {} 54 45 + {} 59 50 {} 60 51 {} 61 52 {} 63 54 {} 64 55 {} 64 55 {} 67 57 + {} 68 58 {} 69 59 {} 70 60 {} 72 62 {} 75 64 {} 76 65 {} 78 67 + {} 78 67 {} 78 67 {} 84 71 {} 85 72 {} 85 72 113 2 2 113 2 2 + 133 4 3 223 10 8 239 14 12 247 15 13 257 19 17 295 20 18 + 309 21 19 335 23 21 355 27 25 355 27 25 393 29 26 393 29 26 + 399 32 28 399 32 28 421 35 30 443 37 32 607 42 36 627 45 38 + 629 46 39 629 46 39 633 48 40 667 52 44 667 52 44 671 55 46 + 683 56 47 705 57 48 711 58 49 759 62 53 777 66 56 805 71 61 + 839 73 63 839 73 63 845 77 66 899 81 68 911 82 69 929 83 70 + 959 87 73 963 88 74 979 89 75} + +do_execsql_test 5.4.14.1 { + SELECT max(c) OVER win, + min(c) OVER win, + count(a) OVER win + FROM t3 + WINDOW win AS ( ORDER BY c NULLS LAST, b NULLS LAST, a NULLS LAST + ROWS BETWEEN 6 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 979 102 83 + 979 102 83 979 113 81 979 113 82 979 133 80 979 148 79 979 158 77 + 979 158 78 979 160 77 979 208 76 979 223 75 979 224 74 979 234 73 + 979 238 72 979 239 71 979 247 70 979 250 69 979 252 68 979 256 67 + 979 257 66 979 295 65 979 309 64 979 330 63 979 335 62 979 336 61 + 979 346 60 979 354 59 979 355 57 979 355 58 979 393 56 979 393 57 + 979 398 55 979 399 53 979 399 54 979 412 53 979 421 52 979 430 51 + 979 443 50 979 480 48 979 480 49 979 572 47 979 574 46 979 607 45 + 979 618 43 979 618 44 979 627 42 979 629 40 979 629 41 979 633 40 + 979 634 39 979 652 38 979 660 37 979 667 35 979 667 36 979 670 35 + 979 671 34 979 683 33 979 705 32 979 711 31 979 716 30 979 726 29 + 979 730 28 979 759 27 979 762 26 979 768 24 979 768 25 979 777 23 + 979 786 22 979 790 21 979 792 20 979 794 19 979 805 18 979 822 17 + 979 839 15 979 839 16 979 840 14 979 844 13 979 845 12 979 870 9 + 979 870 10 979 870 11 979 899 9 979 911 8 979 929 7} + +do_execsql_test 5.4.14.2 { + SELECT sum(c) FILTER (WHERE (c%2)!=0) OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t3 + WINDOW win AS ( ORDER BY c NULLS LAST, b NULLS LAST, a NULLS LAST + ROWS BETWEEN 6 PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES ) + ORDER BY 1 NULLS FIRST, 2 NULLS FIRST, 3 NULLS FIRST +} {3830 89 89 4741 88 88 5640 84 84 5640 85 85 5640 86 86 5640 87 87 + 6485 81 81 6485 82 82 6485 83 83 7324 80 80 8163 78 78 8163 79 79 + 8968 73 73 8968 74 74 8968 75 75 8968 76 76 8968 77 77 9745 69 69 + 9745 70 70 9745 71 71 9745 72 72 10504 65 65 10504 66 66 + 10504 67 67 10504 68 68 11215 64 64 11920 63 63 12603 62 62 + 13274 60 60 13274 61 61 13941 59 59 14608 55 55 14608 56 56 + 14608 57 57 14608 58 58 15241 54 54 15870 53 53 16499 52 52 + 17126 49 49 17126 50 50 17126 51 51 17733 44 44 17733 45 45 + 17733 46 46 17733 47 47 17733 48 48 18176 42 42 18176 43 43 + 18597 40 40 18597 41 41 18996 39 39 19395 37 37 19395 38 38 + 19788 36 36 20181 35 35 20536 34 34 20891 30 30 20891 31 31 + 20891 32 32 20891 33 33 21226 28 28 21226 29 29 21535 27 27 + 21830 26 26 22087 22 22 22087 23 23 22087 24 24 22087 25 25 + 22334 21 21 22573 17 17 22573 18 18 22573 19 19 22573 20 20 + 22796 11 11 22796 12 12 22796 13 13 22796 14 14 22796 15 15 + 22796 16 16 22929 10 10 23042 9 9 23155 1 1 23155 2 2 23155 3 3 + 23155 4 4 23155 5 5 23155 6 6 23155 7 7 23155 8 8} + +#========================================================================== + +do_execsql_test 6.0 { + DROP TABLE IF EXISTS t2; + CREATE TABLE t2(a TEXT, b INTEGER); + INSERT INTO t2 VALUES('A', NULL); + INSERT INTO t2 VALUES('B', NULL); + INSERT INTO t2 VALUES('C', 1); +} {} + +do_execsql_test 6.1 { + SELECT group_concat(a, '.') OVER ( + ORDER BY b NULLS FIRST RANGE BETWEEN 7 PRECEDING AND 2 PRECEDING + ) + FROM t2 +} {A.B A.B {}} + +do_execsql_test 6.2 { + SELECT group_concat(a, '.') OVER ( + ORDER BY b DESC NULLS LAST RANGE BETWEEN 7 PRECEDING AND 2 PRECEDING + ) + FROM t2 +} {{} A.B A.B} + +#========================================================================== + +do_execsql_test 7.0 { + DROP TABLE IF EXISTS t2; + CREATE TABLE t2(a INTEGER, b INTEGER); + + INSERT INTO t2 VALUES(1, 65); + INSERT INTO t2 VALUES(2, NULL); + INSERT INTO t2 VALUES(3, NULL); + INSERT INTO t2 VALUES(4, NULL); + INSERT INTO t2 VALUES(5, 66); + INSERT INTO t2 VALUES(6, 67); +} {} + +do_execsql_test 7.1.1 { + SELECT sum (a) OVER win FROM t2 + WINDOW win AS ( + ORDER BY b NULLS LAST RANGE BETWEEN 6 FOLLOWING AND UNBOUNDED FOLLOWING + ); +} {9 9 9 9 9 9} + +do_execsql_test 7.1.2 { + SELECT sum (a) OVER win FROM t2 + WINDOW win AS ( + ORDER BY b NULLS LAST RANGE BETWEEN 1 PRECEDING AND 2 PRECEDING + ); +} {{} {} {} 9 9 9} + +do_execsql_test 7.1.3 { + SELECT sum (a) OVER win FROM t2 + WINDOW win AS ( + ORDER BY b NULLS LAST RANGE BETWEEN 2 FOLLOWING AND 1 FOLLOWING + ); +} {{} {} {} 9 9 9} + +do_execsql_test 7.1.4 { + SELECT sum (a) OVER win FROM t2 + WINDOW win AS ( + ORDER BY b NULLS FIRST RANGE BETWEEN 1 PRECEDING AND 2 PRECEDING + ); +} {9 9 9 {} {} {}} + +do_execsql_test 7.1.5 { + SELECT sum (a) OVER win FROM t2 + WINDOW win AS ( + ORDER BY b NULLS FIRST RANGE BETWEEN 2 FOLLOWING AND 1 FOLLOWING + ); +} {9 9 9 {} {} {}} + +do_execsql_test 7.1.6 { + SELECT sum (a) OVER win FROM t2 + WINDOW win AS ( + ORDER BY b NULLS LAST RANGE BETWEEN 1000 PRECEDING AND 2 PRECEDING + ); +} {{} {} 1 9 9 9} + +do_execsql_test 7.1.7 { + SELECT sum (a) OVER win FROM t2 + WINDOW win AS ( + ORDER BY b NULLS LAST RANGE BETWEEN 2000 FOLLOWING AND 1000 FOLLOWING + ); +} {{} {} {} 9 9 9} + +do_execsql_test 7.1.8 { + SELECT sum (a) OVER win FROM t2 + WINDOW win AS ( + ORDER BY b NULLS FIRST RANGE BETWEEN 1000 PRECEDING AND 2000 PRECEDING + ); +} {9 9 9 {} {} {}} + +do_execsql_test 7.1.9 { + SELECT sum (a) OVER win FROM t2 + WINDOW win AS ( + ORDER BY b NULLS FIRST RANGE BETWEEN 2000 FOLLOWING AND 1000 FOLLOWING + ); +} {9 9 9 {} {} {}} + +do_execsql_test 7.2.1 { + SELECT min (a) OVER win FROM t2 + WINDOW win AS ( + ORDER BY b NULLS LAST RANGE BETWEEN 6 FOLLOWING AND UNBOUNDED FOLLOWING + ); +} {2 2 2 2 2 2} + +do_execsql_test 7.2.2 { + SELECT min (a) OVER win FROM t2 + WINDOW win AS ( + ORDER BY b NULLS LAST RANGE BETWEEN 1 PRECEDING AND 2 PRECEDING + ); +} {{} {} {} 2 2 2} + +do_execsql_test 7.2.3 { + SELECT min (a) OVER win FROM t2 + WINDOW win AS ( + ORDER BY b NULLS LAST RANGE BETWEEN 2 FOLLOWING AND 1 FOLLOWING + ); +} {{} {} {} 2 2 2} + +do_execsql_test 7.2.4 { + SELECT min (a) OVER win FROM t2 + WINDOW win AS ( + ORDER BY b NULLS FIRST RANGE BETWEEN 1 PRECEDING AND 2 PRECEDING + ); +} {2 2 2 {} {} {}} + +do_execsql_test 7.2.5 { + SELECT min (a) OVER win FROM t2 + WINDOW win AS ( + ORDER BY b NULLS FIRST RANGE BETWEEN 2 FOLLOWING AND 1 FOLLOWING + ); +} {2 2 2 {} {} {}} + +do_execsql_test 7.2.6 { + SELECT min (a) OVER win FROM t2 + WINDOW win AS ( + ORDER BY b NULLS LAST RANGE BETWEEN 1000 PRECEDING AND 2 PRECEDING + ); +} {{} {} 1 2 2 2} + +do_execsql_test 7.2.7 { + SELECT min (a) OVER win FROM t2 + WINDOW win AS ( + ORDER BY b NULLS LAST RANGE BETWEEN 2000 FOLLOWING AND 1000 FOLLOWING + ); +} {{} {} {} 2 2 2} + +do_execsql_test 7.2.8 { + SELECT min (a) OVER win FROM t2 + WINDOW win AS ( + ORDER BY b NULLS FIRST RANGE BETWEEN 1000 PRECEDING AND 2000 PRECEDING + ); +} {2 2 2 {} {} {}} + +do_execsql_test 7.2.9 { + SELECT min (a) OVER win FROM t2 + WINDOW win AS ( + ORDER BY b NULLS FIRST RANGE BETWEEN 2000 FOLLOWING AND 1000 FOLLOWING + ); +} {2 2 2 {} {} {}} + +do_execsql_test 7.3.1 { + SELECT sum (a) OVER win FROM t2 + WINDOW win AS ( + ORDER BY b NULLS LAST RANGE BETWEEN 6 FOLLOWING AND UNBOUNDED FOLLOWING + ); +} {9 9 9 9 9 9} + +do_execsql_test 7.3.2 { + SELECT sum (a) OVER win FROM t2 + WINDOW win AS ( + ORDER BY b NULLS LAST RANGE BETWEEN 1 PRECEDING AND 2 PRECEDING + ); +} {{} {} {} 9 9 9} + +do_execsql_test 7.3.3 { + SELECT sum (a) OVER win FROM t2 + WINDOW win AS ( + ORDER BY b NULLS LAST RANGE BETWEEN 2 FOLLOWING AND 1 FOLLOWING + ); +} {{} {} {} 9 9 9} + +do_execsql_test 7.3.4 { + SELECT sum (a) OVER win FROM t2 + WINDOW win AS ( + ORDER BY b NULLS FIRST RANGE BETWEEN 1 PRECEDING AND 2 PRECEDING + ); +} {9 9 9 {} {} {}} + +do_execsql_test 7.3.5 { + SELECT sum (a) OVER win FROM t2 + WINDOW win AS ( + ORDER BY b NULLS FIRST RANGE BETWEEN 2 FOLLOWING AND 1 FOLLOWING + ); +} {9 9 9 {} {} {}} + +do_execsql_test 7.3.6 { + SELECT sum (a) OVER win FROM t2 + WINDOW win AS ( + ORDER BY b NULLS LAST RANGE BETWEEN 1000 PRECEDING AND 2 PRECEDING + ); +} {{} {} 1 9 9 9} + +do_execsql_test 7.3.7 { + SELECT sum (a) OVER win FROM t2 + WINDOW win AS ( + ORDER BY b NULLS LAST RANGE BETWEEN 2000 FOLLOWING AND 1000 FOLLOWING + ); +} {{} {} {} 9 9 9} + +do_execsql_test 7.3.8 { + SELECT sum (a) OVER win FROM t2 + WINDOW win AS ( + ORDER BY b NULLS FIRST RANGE BETWEEN 1000 PRECEDING AND 2000 PRECEDING + ); +} {9 9 9 {} {} {}} + +do_execsql_test 7.3.9 { + SELECT sum (a) OVER win FROM t2 + WINDOW win AS ( + ORDER BY b NULLS FIRST RANGE BETWEEN 2000 FOLLOWING AND 1000 FOLLOWING + ); +} {9 9 9 {} {} {}} + +do_execsql_test 7.4.1 { + SELECT max (a) OVER win FROM t2 + WINDOW win AS ( + ORDER BY b NULLS LAST RANGE BETWEEN 6 FOLLOWING AND UNBOUNDED FOLLOWING + ); +} {4 4 4 4 4 4} + +do_execsql_test 7.4.2 { + SELECT max (a) OVER win FROM t2 + WINDOW win AS ( + ORDER BY b NULLS LAST RANGE BETWEEN 1 PRECEDING AND 2 PRECEDING + ); +} {{} {} {} 4 4 4} + +do_execsql_test 7.4.3 { + SELECT max (a) OVER win FROM t2 + WINDOW win AS ( + ORDER BY b NULLS LAST RANGE BETWEEN 2 FOLLOWING AND 1 FOLLOWING + ); +} {{} {} {} 4 4 4} + +do_execsql_test 7.4.4 { + SELECT max (a) OVER win FROM t2 + WINDOW win AS ( + ORDER BY b NULLS FIRST RANGE BETWEEN 1 PRECEDING AND 2 PRECEDING + ); +} {4 4 4 {} {} {}} + +do_execsql_test 7.4.5 { + SELECT max (a) OVER win FROM t2 + WINDOW win AS ( + ORDER BY b NULLS FIRST RANGE BETWEEN 2 FOLLOWING AND 1 FOLLOWING + ); +} {4 4 4 {} {} {}} + +do_execsql_test 7.4.6 { + SELECT max (a) OVER win FROM t2 + WINDOW win AS ( + ORDER BY b NULLS LAST RANGE BETWEEN 1000 PRECEDING AND 2 PRECEDING + ); +} {{} {} 1 4 4 4} + +do_execsql_test 7.4.7 { + SELECT max (a) OVER win FROM t2 + WINDOW win AS ( + ORDER BY b NULLS LAST RANGE BETWEEN 2000 FOLLOWING AND 1000 FOLLOWING + ); +} {{} {} {} 4 4 4} + +do_execsql_test 7.4.8 { + SELECT max (a) OVER win FROM t2 + WINDOW win AS ( + ORDER BY b NULLS FIRST RANGE BETWEEN 1000 PRECEDING AND 2000 PRECEDING + ); +} {4 4 4 {} {} {}} + +do_execsql_test 7.4.9 { + SELECT max (a) OVER win FROM t2 + WINDOW win AS ( + ORDER BY b NULLS FIRST RANGE BETWEEN 2000 FOLLOWING AND 1000 FOLLOWING + ); +} {4 4 4 {} {} {}} + +#========================================================================== + +do_execsql_test 8.0 { + DROP TABLE IF EXISTS tx; + CREATE TABLE tx(a INTEGER PRIMARY KEY); + INSERT INTO tx VALUES(1), (2), (3), (4), (5), (6); + + DROP TABLE IF EXISTS map; + CREATE TABLE map(v INTEGER PRIMARY KEY, t TEXT); + INSERT INTO map VALUES + (1, 'odd'), (2, 'even'), (3, 'odd'), + (4, 'even'), (5, 'odd'), (6, 'even'); +} {} + +do_execsql_test 8.1 { + SELECT sum(a) OVER ( + PARTITION BY ( + SELECT t FROM map WHERE v=a + ) ORDER BY a + ) FROM tx; +} {2 6 12 1 4 9} + +do_execsql_test 8.2 { + SELECT sum(a) OVER win FROM tx + WINDOW win AS ( + PARTITION BY ( + SELECT t FROM map WHERE v=a + ) ORDER BY a + ); +} {2 6 12 1 4 9} + +do_execsql_test 8.3 { + WITH map2 AS ( + SELECT * FROM map + ) + SELECT sum(a) OVER ( + PARTITION BY ( + SELECT t FROM map2 WHERE v=a + ) ORDER BY a + ) FROM tx; +} {2 6 12 1 4 9} + +do_execsql_test 8.4 { + WITH map2 AS ( + SELECT * FROM map + ) + SELECT sum(a) OVER win FROM tx + WINDOW win AS ( + PARTITION BY ( + SELECT t FROM map2 WHERE v=a + ) ORDER BY a + ); +} {2 6 12 1 4 9} + +#========================================================================== + +do_execsql_test 9.1 { + DROP TABLE IF EXISTS t1; + DROP TABLE IF EXISTS t2; + CREATE TABLE t1(a INTEGER); + CREATE TABLE t2(y INTEGER); +} {} + +do_execsql_test 9.2 { + SELECT ( + SELECT max(a) OVER ( ORDER BY (SELECT sum(a) FROM t1) ) + + min(a) OVER() + ) + FROM t1 +} {} + +#========================================================================== + +do_execsql_test 10.0 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a INTEGER, b INTEGER); + INSERT INTO t1 VALUES (10, 1), + (20, -1), + (5, 2), + (15, 0), + (25, 3); +} {} + +do_execsql_test 10.1 { + SELECT + a, b, MIN(a) FILTER(WHERE b > 0) OVER win + FROM t1 + WINDOW win AS (ORDER BY a ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING); +} {5 2 5 10 1 5 15 0 10 20 -1 25 25 3 25} + +do_execsql_test 10.2 { + SELECT + a, b, MIN(a) FILTER(WHERE b > 0) OVER win + FROM t1 + WINDOW win AS (); +} {10 1 5 20 -1 5 5 2 5 15 0 5 25 3 5} + +do_execsql_test 10.3 { + SELECT + a, b, MIN(a) FILTER(WHERE b > 0) OVER win + FROM t1 + WINDOW win AS (ORDER BY a); +} {5 2 5 10 1 5 15 0 5 20 -1 5 25 3 5} + +do_execsql_test 10.4 { + SELECT + a, b, MIN(a) OVER win + FROM t1 + WINDOW win AS (ORDER BY a ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING); +} {5 2 5 10 1 5 15 0 10 20 -1 15 25 3 20} + +#========================================================================== + +do_execsql_test 11.0 { + DROP TABLE IF EXISTS t2; + CREATE TABLE t2(a INTEGER, b INTEGER); + INSERT INTO t2 VALUES(1, 12); + INSERT INTO t2 VALUES(2, 10); + INSERT INTO t2 VALUES(3, 15); + INSERT INTO t2 VALUES(4, 22); + INSERT INTO t2 VALUES(5, 1); + INSERT INTO t2 VALUES(6, 4); + INSERT INTO t2 VALUES(7, 7); + INSERT INTO t2 VALUES(8, 6); + INSERT INTO t2 VALUES(9, 22); + INSERT INTO t2 VALUES(10, 2); +} {} + +do_execsql_test 11.1 { + SELECT a, min(b) FILTER (WHERE a%2 != 0) OVER win + FROM t2 + WINDOW win AS (ORDER BY a ROWS BETWEEN 2 PRECEDING AND 2 FOLLOWING); +} {1 12 2 12 3 1 4 1 5 1 6 1 7 1 8 7 9 7 10 22} + +finish_test diff --git a/test/window9.test b/test/window9.test new file mode 100644 index 0000000000..0548624f82 --- /dev/null +++ b/test/window9.test @@ -0,0 +1,343 @@ +# 2019 June 8 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix window9 + +ifcapable !windowfunc { + finish_test + return +} + +do_execsql_test 1.0 { + CREATE TABLE fruits( + name TEXT COLLATE NOCASE, + color TEXT COLLATE NOCASE + ); +} + +do_execsql_test 1.1 { + INSERT INTO fruits (name, color) VALUES ('apple', 'RED'); + INSERT INTO fruits (name, color) VALUES ('APPLE', 'yellow'); + INSERT INTO fruits (name, color) VALUES ('pear', 'YELLOW'); + INSERT INTO fruits (name, color) VALUES ('PEAR', 'green'); +} + +do_execsql_test 1.2 { + SELECT name, color, dense_rank() OVER (ORDER BY name) FROM fruits; +} { + apple RED 1 + APPLE yellow 1 + pear YELLOW 2 + PEAR green 2 +} + +do_execsql_test 1.3 { + SELECT name, color, + dense_rank() OVER (PARTITION BY name ORDER BY color) + FROM fruits; +} { + apple RED 1 + APPLE yellow 2 + PEAR green 1 + pear YELLOW 2 +} + +do_execsql_test 1.4 { + SELECT name, color, + dense_rank() OVER (ORDER BY name), + dense_rank() OVER (PARTITION BY name ORDER BY color) + FROM fruits; +} { + apple RED 1 1 + APPLE yellow 1 2 + PEAR green 2 1 + pear YELLOW 2 2 +} + +do_execsql_test 1.5 { + SELECT name, color, + dense_rank() OVER (ORDER BY name), + dense_rank() OVER (PARTITION BY name ORDER BY color) + FROM fruits ORDER BY color; +} { + PEAR green 2 1 + apple RED 1 1 + APPLE yellow 1 2 + pear YELLOW 2 2 +} + +do_execsql_test 2.0 { + CREATE TABLE t1(a BLOB, b INTEGER, c COLLATE nocase); + INSERT INTO t1 VALUES(1, 2, 'abc'); + INSERT INTO t1 VALUES(3, 4, 'ABC'); +} + +do_execsql_test 2.1.1 { + SELECT c=='Abc' FROM t1 +} {1 1} +do_execsql_test 2.1.2 { + SELECT c=='Abc', rank() OVER (ORDER BY b) FROM t1 +} {1 1 1 2} + +do_execsql_test 2.2.1 { + SELECT b=='2' FROM t1 +} {1 0} +do_execsql_test 2.2.2 { + SELECT b=='2', rank() OVER (ORDER BY a) FROM t1 +} {1 1 0 2} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 3.0 { + CREATE TABLE t1(a); + CREATE TABLE t2(a,b,c); +} + +do_execsql_test 3.1 { + SELECT EXISTS(SELECT 1 FROM t1 ORDER BY sum(a) OVER ()) FROM t1; +} + +do_execsql_test 3.2 { + SELECT sum(a) OVER () FROM t2 + ORDER BY EXISTS(SELECT 1 FROM t2 ORDER BY sum(a) OVER ()); +} + +do_catchsql_test 3.3 { + SELECT a, sum(a) OVER (ORDER BY a DESC) FROM t2 + ORDER BY EXISTS( + SELECT 1 FROM t2 ORDER BY sum(a) OVER (ORDER BY a) + ) OVER (ORDER BY a); +} {1 {near "OVER": syntax error}} + +do_catchsql_test 3.4 { + SELECT y, y+1, y+2 FROM ( + SELECT c IN ( + SELECT min(a) OVER (), + (abs(row_number() OVER())+22)/19, + max(a) OVER () FROM t1 + ) AS y FROM t2 + ); +} {1 {sub-select returns 3 columns - expected 1}} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 4.0 { + CREATE TABLE t1(a, b TEXT); + INSERT INTO t1 VALUES('A', 1), ('A', 2), ('2', 1), ('2', 2); +} + +do_execsql_test 4.1.1 { + SELECT b, b=count(*), '1,2' FROM t1 GROUP BY b; +} {1 0 1,2 2 1 1,2} +do_execsql_test 4.1.2 { + SELECT b, b=count(*), group_concat(b) OVER () FROM t1 GROUP BY b; +} {1 0 1,2 2 1 1,2} + +#-------------------------------------------------------------------------- +reset_db +do_execsql_test 5.0 { + CREATE TABLE t1(a, b, c, d, e); + CREATE INDEX i1 ON t1(a, b, c, d, e); +} + +foreach {tn sql} { + 1 { + SELECT + sum(e) OVER (), + sum(e) OVER (ORDER BY a), + sum(e) OVER (PARTITION BY a ORDER BY b), + sum(e) OVER (PARTITION BY a, b ORDER BY c), + sum(e) OVER (PARTITION BY a, b, c ORDER BY d) + FROM t1; + } + 2 { + SELECT sum(e) OVER (PARTITION BY a ORDER BY b) FROM t1 ORDER BY a; + } +} { + do_test 5.1.$tn { + execsql "EXPLAIN QUERY PLAN $sql" + } {~/ORDER/} +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 6.0 { + CREATE TABLE t0(c0); + INSERT INTO t0(c0) VALUES (0); +} + +do_execsql_test 6.1 { + SELECT * FROM t0 WHERE + EXISTS ( + SELECT MIN(c0) OVER (), CUME_DIST() OVER () FROM t0 + ) >=1 AND + EXISTS ( + SELECT MIN(c0) OVER (), CUME_DIST() OVER () FROM t0 + ) <=1; +} {0} + +do_execsql_test 6.2 { + SELECT * FROM t0 WHERE EXISTS ( + SELECT MIN(c0) OVER (), CUME_DIST() OVER () FROM t0 + ) + BETWEEN 1 AND 1; +} {0} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 7.0 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(x, y); + INSERT INTO t1 VALUES(10, 1); + INSERT INTO t1 VALUES(20, 2); + INSERT INTO t1 VALUES(3, 3); + INSERT INTO t1 VALUES(2, 4); + INSERT INTO t1 VALUES(1, 5); +} {} + + +do_execsql_test 7.1 { + SELECT avg(x) OVER (ORDER BY y) AS z FROM t1 ORDER BY z +} { + 7.2 8.75 10.0 11.0 15.0 +} + +do_execsql_test 7.2 { + SELECT avg(x) OVER (ORDER BY y) z FROM t1 ORDER BY (z IS y); +} { + 10.0 15.0 11.0 8.75 7.2 +} + +do_execsql_test 7.3 { + SELECT avg(x) OVER (ORDER BY y) z FROM t1 ORDER BY (y IS z); +} { + 10.0 15.0 11.0 8.75 7.2 +} + +do_execsql_test 7.4 { + SELECT avg(x) OVER (ORDER BY y) z FROM t1 ORDER BY z + 0.0; +} { + 7.2 8.75 10.0 11.0 15.0 +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 8.1.1 { + CREATE TABLE t1(a, b); + INSERT INTO t1 VALUES(1, 2), (3, 4); + SELECT min( sum(a) ) OVER () FROM t1; +} {4} + +do_execsql_test 8.1.2 { + SELECT min( sum(a) ) OVER () FROM t1 GROUP BY a; +} {1 1} + +do_execsql_test 8.2 { + CREATE VIEW v1 AS + SELECT 0 AS x + UNION + SELECT count() OVER() FROM (SELECT 0) + ORDER BY 1 + ; +} + +do_catchsql_test 8.3 { + SELECT min( max((SELECT x FROM v1)) ) OVER() +} {0 0} + +do_execsql_test 8.4 { + SELECT( + SELECT x UNION + SELECT sum( avg((SELECT x FROM v1)) ) OVER() + ) + FROM v1; +} {0.0 0.0} + +#-------------------------------------------------------------------------- +reset_db +do_execsql_test 9.0 { + CREATE TABLE t1(a, b, c); + INSERT INTO t1 VALUES(NULL,'bb',356); + INSERT INTO t1 VALUES('CB','aa',158); + INSERT INTO t1 VALUES('BB','aa',399); + INSERT INTO t1 VALUES('FF','bb',938); +} + +do_catchsql_test 9.1 { + SELECT sum(c) OVER ( + ORDER BY c RANGE BETWEEN 0 PRECEDING AND '-700' PRECEDING + ) + FROM t1 +} {1 {frame ending offset must be a non-negative number}} + +#-------------------------------------------------------------------------- +reset_db + +do_execsql_test 10.0 { + CREATE TABLE t1(a, b); + INSERT INTO t1 VALUES(1, 'a'); + INSERT INTO t1 VALUES(2, 'b'); + INSERT INTO t1 VALUES(3, 'c'); + INSERT INTO t1 VALUES(4, 'd'); + INSERT INTO t1 VALUES(5, 'e'); + INSERT INTO t1 VALUES(6, 'f'); +} + +do_execsql_test 10.1 { + SELECT a, min(b) OVER win + FROM t1 + WINDOW win AS (ORDER BY a ROWS BETWEEN 2 PRECEDING AND 1 FOLLOWING) +} { + 1 a + 2 a + 3 a + 4 b + 5 c + 6 d +} + +do_execsql_test 10.2 { + SELECT a, min(b) FILTER (WHERE a%2) OVER win + FROM t1 + WINDOW win AS (ORDER BY a ROWS BETWEEN 2 PRECEDING AND 1 FOLLOWING) +} { + 1 a + 2 a + 3 a + 4 c + 5 c + 6 e +} + +do_execsql_test 10.3 { + SELECT a, min(b) FILTER (WHERE (a%2)=0) OVER win + FROM t1 + WINDOW win AS (ORDER BY a ROWS BETWEEN 2 PRECEDING AND 1 FOLLOWING) +} { + 1 b + 2 b + 3 b + 4 b + 5 d + 6 d +} + +do_catchsql_test 10.4 { + SELECT a, nth_value(b, 1) FILTER (WHERE (a%2)=0) OVER win + FROM t1 + WINDOW win AS (ORDER BY a ROWS BETWEEN 2 PRECEDING AND 1 FOLLOWING) +} {1 {FILTER clause may only be used with aggregate window functions}} + +finish_test diff --git a/test/windowA.test b/test/windowA.test new file mode 100644 index 0000000000..e94ae57e43 --- /dev/null +++ b/test/windowA.test @@ -0,0 +1,309 @@ +# 2019-08-30 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# Test cases for RANGE BETWEEN and especially with NULLS LAST +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix windowA + +ifcapable !windowfunc { + finish_test + return +} + +do_execsql_test 1.0 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b CHAR(1), d FLOAT); + INSERT INTO t1 VALUES + (1, 'A', 5.4), + (2, 'B', 5.55), + (3, 'C', 8.0), + (4, 'D', 10.25), + (5, 'E', 10.26), + (6, 'N', NULL), + (7, 'N', NULL); +} {} + +do_execsql_test 1.1 { + SELECT a, b, quote(d), group_concat(b,'') OVER w1 FROM t1 + WINDOW w1 AS + (ORDER BY d DESC NULLS LAST + RANGE BETWEEN 2.50 PRECEDING AND 2.25 FOLLOWING) + ORDER BY +d DESC NULLS LAST, +a; +} [list \ + 5 E 10.26 ED \ + 4 D 10.25 EDC \ + 3 C 8.0 EDC \ + 2 B 5.55 CBA \ + 1 A 5.4 BA \ + 6 N NULL NN \ + 7 N NULL NN \ +] + +do_execsql_test 1.2 { + SELECT a, b, quote(d), group_concat(b,'') OVER w1 FROM t1 + WINDOW w1 AS + (ORDER BY d DESC NULLS FIRST + RANGE BETWEEN 2.50 PRECEDING AND 2.25 FOLLOWING) + ORDER BY +d DESC NULLS FIRST, +a; +} [list \ + 6 N NULL NN \ + 7 N NULL NN \ + 5 E 10.26 ED \ + 4 D 10.25 EDC \ + 3 C 8.0 EDC \ + 2 B 5.55 CBA \ + 1 A 5.4 BA \ +] + +do_execsql_test 1.3 { + SELECT a, b, quote(d), group_concat(b,'') OVER w1 FROM t1 + WINDOW w1 AS + (ORDER BY d DESC NULLS LAST + RANGE BETWEEN 2.50 PRECEDING AND UNBOUNDED FOLLOWING) + ORDER BY +d DESC NULLS LAST, +a; +} [list \ + 5 E 10.26 EDCBANN \ + 4 D 10.25 EDCBANN \ + 3 C 8.0 EDCBANN \ + 2 B 5.55 CBANN \ + 1 A 5.4 BANN \ + 6 N NULL NN \ + 7 N NULL NN \ +] + +do_execsql_test 1.4 { + SELECT a, b, quote(d), group_concat(b,'') OVER w1 FROM t1 + WINDOW w1 AS + (ORDER BY d DESC NULLS FIRST + RANGE BETWEEN 2.50 PRECEDING AND UNBOUNDED FOLLOWING) + ORDER BY +d DESC NULLS FIRST, +a; +} [list \ + 6 N NULL NNEDCBA \ + 7 N NULL NNEDCBA \ + 5 E 10.26 EDCBA \ + 4 D 10.25 EDCBA \ + 3 C 8.0 EDCBA \ + 2 B 5.55 CBA \ + 1 A 5.4 BA \ +] + +do_execsql_test 1.5 { + SELECT a, b, quote(d), group_concat(b,'') OVER w1 FROM t1 + WINDOW w1 AS + (ORDER BY d DESC NULLS LAST + RANGE BETWEEN 2.50 PRECEDING AND CURRENT ROW) + ORDER BY +d DESC NULLS LAST, +a; +} [list \ + 5 E 10.26 E \ + 4 D 10.25 ED \ + 3 C 8.0 EDC \ + 2 B 5.55 CB \ + 1 A 5.4 BA \ + 6 N NULL NN \ + 7 N NULL NN \ +] + +do_execsql_test 1.6 { + SELECT a, b, quote(d), group_concat(b,'') OVER w1 FROM t1 + WINDOW w1 AS + (ORDER BY d DESC NULLS FIRST + RANGE BETWEEN 2.50 PRECEDING AND CURRENT ROW) + ORDER BY +d DESC NULLS FIRST, +a; +} [list \ + 6 N NULL NN \ + 7 N NULL NN \ + 5 E 10.26 E \ + 4 D 10.25 ED \ + 3 C 8.0 EDC \ + 2 B 5.55 CB \ + 1 A 5.4 BA \ +] + +do_execsql_test 2.1 { + SELECT a, b, quote(d), group_concat(b,'') OVER w1 FROM t1 + WINDOW w1 AS + (ORDER BY d DESC NULLS LAST + RANGE BETWEEN UNBOUNDED PRECEDING AND 2.25 FOLLOWING) + ORDER BY +d DESC NULLS LAST, +a; +} [list \ + 5 E 10.26 ED \ + 4 D 10.25 EDC \ + 3 C 8.0 EDC \ + 2 B 5.55 EDCBA \ + 1 A 5.4 EDCBA \ + 6 N NULL EDCBANN \ + 7 N NULL EDCBANN \ +] + +do_execsql_test 2.2 { + SELECT a, b, quote(d), group_concat(b,'') OVER w1 FROM t1 + WINDOW w1 AS + (ORDER BY d DESC NULLS FIRST + RANGE BETWEEN UNBOUNDED PRECEDING AND 2.25 FOLLOWING) + ORDER BY +d DESC NULLS FIRST, +a; +} [list \ + 6 N NULL NN \ + 7 N NULL NN \ + 5 E 10.26 NNED \ + 4 D 10.25 NNEDC \ + 3 C 8.0 NNEDC \ + 2 B 5.55 NNEDCBA \ + 1 A 5.4 NNEDCBA \ +] + +do_execsql_test 2.3 { + SELECT a, b, quote(d), group_concat(b,'') OVER w1 FROM t1 + WINDOW w1 AS + (ORDER BY d DESC NULLS LAST + RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) + ORDER BY +d DESC NULLS LAST, +a; +} [list \ + 5 E 10.26 EDCBANN \ + 4 D 10.25 EDCBANN \ + 3 C 8.0 EDCBANN \ + 2 B 5.55 EDCBANN \ + 1 A 5.4 EDCBANN \ + 6 N NULL EDCBANN \ + 7 N NULL EDCBANN \ +] + +do_execsql_test 2.4 { + SELECT a, b, quote(d), group_concat(b,'') OVER w1 FROM t1 + WINDOW w1 AS + (ORDER BY d DESC NULLS FIRST + RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) + ORDER BY +d DESC NULLS FIRST, +a; +} [list \ + 6 N NULL NNEDCBA \ + 7 N NULL NNEDCBA \ + 5 E 10.26 NNEDCBA \ + 4 D 10.25 NNEDCBA \ + 3 C 8.0 NNEDCBA \ + 2 B 5.55 NNEDCBA \ + 1 A 5.4 NNEDCBA \ +] + +do_execsql_test 2.5 { + SELECT a, b, quote(d), group_concat(b,'') OVER w1 FROM t1 + WINDOW w1 AS + (ORDER BY d DESC NULLS LAST + RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + ORDER BY +d DESC NULLS LAST, +a; +} [list \ + 5 E 10.26 E \ + 4 D 10.25 ED \ + 3 C 8.0 EDC \ + 2 B 5.55 EDCB \ + 1 A 5.4 EDCBA \ + 6 N NULL EDCBANN \ + 7 N NULL EDCBANN \ +] + +do_execsql_test 2.6 { + SELECT a, b, quote(d), group_concat(b,'') OVER w1 FROM t1 + WINDOW w1 AS + (ORDER BY d DESC NULLS FIRST + RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + ORDER BY +d DESC NULLS FIRST, +a; +} [list \ + 6 N NULL NN \ + 7 N NULL NN \ + 5 E 10.26 NNE \ + 4 D 10.25 NNED \ + 3 C 8.0 NNEDC \ + 2 B 5.55 NNEDCB \ + 1 A 5.4 NNEDCBA \ +] + + +do_execsql_test 3.1 { + SELECT a, b, quote(d), group_concat(b,'') OVER w1 FROM t1 + WINDOW w1 AS + (ORDER BY d DESC NULLS LAST + RANGE BETWEEN CURRENT ROW AND 2.25 FOLLOWING) + ORDER BY +d DESC NULLS LAST, +a; +} [list \ + 5 E 10.26 ED \ + 4 D 10.25 DC \ + 3 C 8.0 C \ + 2 B 5.55 BA \ + 1 A 5.4 A \ + 6 N NULL NN \ + 7 N NULL NN \ +] + +do_execsql_test 3.2 { + SELECT a, b, quote(d), group_concat(b,'') OVER w1 FROM t1 + WINDOW w1 AS + (ORDER BY d DESC NULLS FIRST + RANGE BETWEEN CURRENT ROW AND 2.25 FOLLOWING) + ORDER BY +d DESC NULLS FIRST, +a; +} [list \ + 6 N NULL NN \ + 7 N NULL NN \ + 5 E 10.26 ED \ + 4 D 10.25 DC \ + 3 C 8.0 C \ + 2 B 5.55 BA \ + 1 A 5.4 A \ +] + +do_execsql_test 3.3 { + SELECT a, b, quote(d), group_concat(b,'') OVER w1 FROM t1 + WINDOW w1 AS + (ORDER BY d DESC NULLS LAST + RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) + ORDER BY +d DESC NULLS LAST, +a; +} [list \ + 5 E 10.26 EDCBANN \ + 4 D 10.25 DCBANN \ + 3 C 8.0 CBANN \ + 2 B 5.55 BANN \ + 1 A 5.4 ANN \ + 6 N NULL NN \ + 7 N NULL NN \ +] + +do_execsql_test 3.4 { + SELECT a, b, quote(d), group_concat(b,'') OVER w1 FROM t1 + WINDOW w1 AS + (ORDER BY d DESC NULLS FIRST + RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) + ORDER BY +d DESC NULLS FIRST, +a; +} [list \ + 6 N NULL NNEDCBA \ + 7 N NULL NNEDCBA \ + 5 E 10.26 EDCBA \ + 4 D 10.25 DCBA \ + 3 C 8.0 CBA \ + 2 B 5.55 BA \ + 1 A 5.4 A \ +] + +do_execsql_test 4.0 { + SELECT a, b, quote(d), group_concat(b,'') OVER w1 FROM t1 + WINDOW w1 AS + (ORDER BY d DESC NULLS FIRST + RANGE BETWEEN 2.50 PRECEDING AND 0.5 PRECEDING) + ORDER BY +d DESC NULLS FIRST, +a; +} [list \ + 6 N NULL NN \ + 7 N NULL NN \ + 5 E 10.26 {} \ + 4 D 10.25 {} \ + 3 C 8.0 ED \ + 2 B 5.55 C \ + 1 A 5.4 {} \ +] + + +finish_test diff --git a/test/windowB.test b/test/windowB.test new file mode 100644 index 0000000000..32193a378a --- /dev/null +++ b/test/windowB.test @@ -0,0 +1,486 @@ +# 2019-08-30 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# Test cases for RANGE BETWEEN and especially with NULLS LAST +# and for varying separator handling by group_concat(). +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix windowB + +ifcapable !windowfunc { + finish_test + return +} + +do_execsql_test 1.0 { + CREATE TABLE t1(a, b); + INSERT INTO t1 VALUES(NULL, 1); + INSERT INTO t1 VALUES(NULL, 2); + INSERT INTO t1 VALUES(NULL, 3); +} {} + +foreach {tn win} { + 1 { ORDER BY a RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING } + 2 { ORDER BY a NULLS LAST RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING } + 3 { ORDER BY a DESC RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING } + 4 { ORDER BY a DESC NULLS FIRST RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING } + + 5 { ORDER BY a NULLS LAST RANGE BETWEEN 1 FOLLOWING AND 2 FOLLOWING } + 6 { ORDER BY a DESC NULLS FIRST RANGE BETWEEN 1 FOLLOWING AND 2 FOLLOWING } + + 7 { ORDER BY a NULLS LAST RANGE BETWEEN 2 PRECEDING AND 1 PRECEDING } + 8 { ORDER BY a DESC NULLS FIRST RANGE BETWEEN 2 PRECEDING AND 1 PRECEDING } +} { + do_execsql_test 1.$tn " + SELECT sum(b) OVER win FROM t1 + WINDOW win AS ( $win ) + " {6 6 6} +} + +do_execsql_test 1.2 { + SELECT sum(b) OVER win FROM t1 + WINDOW win AS ( + ORDER BY a DESC NULLS FIRST RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING + ) +} {6 6 6} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 2.0 { + CREATE TABLE t1(a, b); + INSERT INTO t1 VALUES(1, NULL); + INSERT INTO t1 VALUES(2, 45); + INSERT INTO t1 VALUES(3, 66.2); + INSERT INTO t1 VALUES(4, 'hello world'); + INSERT INTO t1 VALUES(5, 'hello world'); + INSERT INTO t1 VALUES(6, X'1234'); + INSERT INTO t1 VALUES(7, X'1234'); + INSERT INTO t1 VALUES(8, NULL); +} + +foreach {tn win} { + 1 "ORDER BY b RANGE BETWEEN 1 PRECEDING AND 2 PRECEDING" + 2 "ORDER BY b RANGE BETWEEN 2 FOLLOWING AND 2 FOLLOWING" + 3 "ORDER BY b NULLS LAST RANGE BETWEEN 1 PRECEDING AND 2 PRECEDING" + 4 "ORDER BY b NULLS LAST RANGE BETWEEN 2 FOLLOWING AND 2 FOLLOWING" +} { + do_execsql_test 2.1.$tn " + SELECT a, sum(a) OVER win FROM t1 + WINDOW win AS ( $win ) + ORDER BY 1 + " {1 9 2 {} 3 {} 4 9 5 9 6 13 7 13 8 9} +} + +#------------------------------------------------------------------------- +ifcapable json1 { + reset_db + do_execsql_test 3.0 { + CREATE TABLE testjson(id INTEGER PRIMARY KEY, j TEXT, x TEXT); + INSERT INTO testjson VALUES(1, '{"a":1}', 'a'); + INSERT INTO testjson VALUES(2, '{"b":2}', 'b'); + INSERT INTO testjson VALUES(3, '{"c":3}', 'c'); + INSERT INTO testjson VALUES(4, '{"d":4}', 'd'); + } + + do_execsql_test 3.1 { + SELECT json_group_array(json(j)) FROM testjson; + } { + {[{"a":1},{"b":2},{"c":3},{"d":4}]} + } + + do_execsql_test 3.2 { + SELECT json_group_array(json(j)) OVER (ORDER BY id) FROM testjson; + } { + {[{"a":1}]} + {[{"a":1},{"b":2}]} + {[{"a":1},{"b":2},{"c":3}]} + {[{"a":1},{"b":2},{"c":3},{"d":4}]} + } + + do_execsql_test 3.3 { + SELECT json_group_array(json(j)) OVER ( + ORDER BY id RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW + EXCLUDE TIES + ) FROM testjson; + } { + {[{"a":1}]} + {[{"a":1},{"b":2}]} + {[{"a":1},{"b":2},{"c":3}]} + {[{"a":1},{"b":2},{"c":3},{"d":4}]} + } + + do_execsql_test 3.4 { + SELECT json_group_array(json(j)) OVER ( + ORDER BY id ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING + ) FROM testjson; + } { + {[{"a":1},{"b":2}]} + {[{"a":1},{"b":2},{"c":3}]} + {[{"b":2},{"c":3},{"d":4}]} + {[{"c":3},{"d":4}]} + } + + do_execsql_test 3.5 { + SELECT json_group_array(json(j)) OVER ( + ORDER BY id ROWS BETWEEN 2 PRECEDING AND 1 PRECEDING + ) FROM testjson; + } { + {[]} + {[{"a":1}]} + {[{"a":1},{"b":2}]} + {[{"b":2},{"c":3}]} + } + + do_execsql_test 3.5a { + UPDATE testjson SET j = replace(j,char(125),',"e":9'||char(125)); + SELECT j FROM testjson; + } { + {{"a":1,"e":9}} + {{"b":2,"e":9}} + {{"c":3,"e":9}} + {{"d":4,"e":9}} + } + do_execsql_test 3.5b { + SELECT group_concat(x,'') OVER ( + ORDER BY id ROWS BETWEEN 1 FOLLOWING AND 2 FOLLOWING + ) FROM testjson ORDER BY id; + } {bc cd d {}} + do_execsql_test 3.5c { + SELECT json_group_array(json(j)) OVER ( + ORDER BY id ROWS BETWEEN 1 FOLLOWING AND 2 FOLLOWING + ) FROM testjson; + } { + {[{"b":2,"e":9},{"c":3,"e":9}]} + {[{"c":3,"e":9},{"d":4,"e":9}]} + {[{"d":4,"e":9}]} + {[]} + } + do_execsql_test 3.5d { + SELECT json_group_object(x,json(j)) OVER ( + ORDER BY id ROWS BETWEEN 1 FOLLOWING AND 2 FOLLOWING + ) FROM testjson; + } { + {{"b":{"b":2,"e":9},"c":{"c":3,"e":9}}} + {{"c":{"c":3,"e":9},"d":{"d":4,"e":9}}} + {{"d":{"d":4,"e":9}}} + {{}} + } + + do_execsql_test 3.7b { + SELECT group_concat(x,'') FILTER (WHERE id!=2) OVER ( + ORDER BY id ROWS BETWEEN 2 PRECEDING AND 1 PRECEDING + ) FROM testjson; + } {{} a a c} + + do_execsql_test 3.7c { + SELECT json_group_array(json(j)) FILTER (WHERE id!=2) OVER ( + ORDER BY id ROWS BETWEEN 2 PRECEDING AND 1 PRECEDING + ) FROM testjson + } { + {[]} + {[{"a":1,"e":9}]} + {[{"a":1,"e":9}]} + {[{"c":3,"e":9}]} + } + do_execsql_test 3.7d { + SELECT json_group_object(x,json(j)) FILTER (WHERE id!=2) OVER ( + ORDER BY id ROWS BETWEEN 2 PRECEDING AND 1 PRECEDING + ) FROM testjson + } { + {{}} + {{"a":{"a":1,"e":9}}} + {{"a":{"a":1,"e":9}}} + {{"c":{"c":3,"e":9}}} + } +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 4.0 { + CREATE TABLE x(a); + INSERT INTO x VALUES(1); + INSERT INTO x VALUES(2); +} + +do_execsql_test 4.1 { + WITH y AS ( + SELECT Row_Number() OVER (win) FROM x WINDOW win AS (PARTITION BY a) + ) + SELECT * FROM y; +} { + 1 1 +} + +do_catchsql_test 4.2 { + WITH y AS ( + SELECT Row_Number() OVER (win) FROM x WINDOW win AS (PARTITION + BY fake_column)) + SELECT * FROM y; +} {1 {no such column: fake_column}} + +do_catchsql_test 4.3 { + SELECT 1 WINDOW win AS (PARTITION BY fake_column); +} {0 1} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 5.0 { + CREATE TABLE t1(a, c); + CREATE INDEX i1 ON t1(a); + + INSERT INTO t1 VALUES(0, 421); + INSERT INTO t1 VALUES(1, 844); + INSERT INTO t1 VALUES(2, 1001); +} + +do_execsql_test 5.1 { + SELECT a, sum(c) OVER ( + ORDER BY a RANGE BETWEEN 0 PRECEDING AND 3 PRECEDING + ) FROM t1; +} {0 {} 1 {} 2 {}} + +do_execsql_test 5.2 { + INSERT INTO t1 VALUES(NULL, 123); + INSERT INTO t1 VALUES(NULL, 111); + INSERT INTO t1 VALUES('xyz', 222); + INSERT INTO t1 VALUES('xyz', 333); + + SELECT a, sum(c) OVER ( + ORDER BY a RANGE BETWEEN 0 PRECEDING AND 3 PRECEDING + ) FROM t1; +} {{} 234 {} 234 0 {} 1 {} 2 {} xyz 555 xyz 555} + +do_execsql_test 5.3 { + SELECT a, sum(c) OVER ( + ORDER BY a RANGE BETWEEN 2 FOLLOWING AND 0 FOLLOWING + ) FROM t1; +} {{} 234 {} 234 0 {} 1 {} 2 {} xyz 555 xyz 555} + +do_execsql_test 5.4 { + SELECT a, sum(c) OVER ( + ORDER BY a RANGE BETWEEN 0 PRECEDING AND 3 PRECEDING EXCLUDE NO OTHERS + ) FROM t1; +} {{} 234 {} 234 0 {} 1 {} 2 {} xyz 555 xyz 555} + +do_execsql_test 5.5 { + SELECT a, sum(c) OVER ( + ORDER BY a RANGE BETWEEN 2 FOLLOWING AND 0 FOLLOWING EXCLUDE NO OTHERS + ) FROM t1; +} {{} 234 {} 234 0 {} 1 {} 2 {} xyz 555 xyz 555} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 6.0 { + CREATE TABLE t1(a, c); + CREATE INDEX i1 ON t1(a); + + INSERT INTO t1 VALUES(7, 997); + INSERT INTO t1 VALUES(8, 997); + INSERT INTO t1 VALUES('abc', 1001); +} +do_execsql_test 6.1 { + SELECT a, sum(c) OVER ( + ORDER BY a RANGE BETWEEN 2 FOLLOWING AND 0 FOLLOWING + ) FROM t1; +} {7 {} 8 {} abc 1001} +do_execsql_test 6.2 { + SELECT a, sum(c) OVER ( + ORDER BY a RANGE BETWEEN 2 FOLLOWING AND 0 FOLLOWING EXCLUDE NO OTHERS + ) FROM t1; +} {7 {} 8 {} abc 1001} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 7.0 { + CREATE TABLE t1(a, c); + CREATE INDEX i1 ON t1(a); + + INSERT INTO t1 VALUES(NULL, 46); + INSERT INTO t1 VALUES(NULL, 45); + INSERT INTO t1 VALUES(7, 997); + INSERT INTO t1 VALUES(7, 1000); + INSERT INTO t1 VALUES(8, 997); + INSERT INTO t1 VALUES(8, 1000); + INSERT INTO t1 VALUES('abc', 1001); + INSERT INTO t1 VALUES('abc', 1004); + INSERT INTO t1 VALUES('xyz', 3333); +} + +do_execsql_test 7.1 { + SELECT a, max(c) OVER ( + ORDER BY a RANGE BETWEEN 2 FOLLOWING AND 0 FOLLOWING + ) FROM t1; +} {{} 46 {} 46 7 {} 7 {} 8 {} 8 {} abc 1004 abc 1004 xyz 3333} +do_execsql_test 7.2 { + SELECT a, min(c) OVER ( + ORDER BY a RANGE BETWEEN 2 FOLLOWING AND 0 FOLLOWING + ) FROM t1; +} {{} 45 {} 45 7 {} 7 {} 8 {} 8 {} abc 1001 abc 1001 xyz 3333} + +do_execsql_test 7.3 { + SELECT a, max(c) OVER ( + ORDER BY a RANGE BETWEEN 0 PRECEDING AND 2 PRECEDING + ) FROM t1; +} {{} 46 {} 46 7 {} 7 {} 8 {} 8 {} abc 1004 abc 1004 xyz 3333} +do_execsql_test 7.4 { + SELECT a, min(c) OVER ( + ORDER BY a RANGE BETWEEN 0 PRECEDING AND 2 PRECEDING + ) FROM t1; +} {{} 45 {} 45 7 {} 7 {} 8 {} 8 {} abc 1001 abc 1001 xyz 3333} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 8.0 { + BEGIN TRANSACTION; + CREATE TABLE t1(a, c); + INSERT INTO t1 VALUES('aa', 111); + INSERT INTO t1 VALUES('BB', 660); + INSERT INTO t1 VALUES('CC', 938); + INSERT INTO t1 VALUES('dd', 979); + COMMIT; + + CREATE INDEX i1 ON t1(a COLLATE nocase); +} + +do_execsql_test 8.1 { + SELECT sum(c) OVER + (ORDER BY a COLLATE nocase RANGE BETWEEN 10.0 PRECEDING AND 5.0 PRECEDING) + FROM t1; +} {111 660 938 979} + +do_execsql_test 9.0 { + CREATE TABLE seps(x); + INSERT INTO seps(x) VALUES ('1'), ('22'), ('333'), ('4444'); + SELECT group_concat('-', x) + OVER ( ORDER BY x ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING ) + FROM seps; +} {-22- -22-333- -333-4444- -4444-} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 10.1 { + CREATE TABLE t1(i INTEGER PRIMARY KEY, v); + INSERT INTO t1 VALUES( 1, 'one' ); + INSERT INTO t1 VALUES( 2, 'two' ); +} + +do_execsql_test 10.2 { + SELECT + json_group_array( v ) OVER w, + json_group_array( v ) OVER w + FROM t1 + window w as ( + range between unbounded preceding and unbounded following + ) +} { + {["one","two"]} + {["one","two"]} + {["one","two"]} + {["one","two"]} +} + +do_execsql_test 10.3 { + SELECT + group_concat( v ) OVER w, + json_group_array( v ) OVER w, + json_group_array( v ) OVER w, + group_concat( v ) OVER w + FROM t1 + window w as ( + range between unbounded preceding and unbounded following + ) +} { + one,two + {["one","two"]} + {["one","two"]} + one,two + + one,two + {["one","two"]} + {["one","two"]} + one,two +} + +ifcapable json1&&vtab { +if {[permutation]!="no_optimization"} { + + do_execsql_test 11.0 { + SELECT value FROM json_each('[1,2,3,4,5]'); + } {1 2 3 4 5} + + do_execsql_test 11.1 { + SELECT key, value FROM json_each('[1,2,3,4,5]'); + } {0 1 1 2 2 3 3 4 4 5} + do_execsql_test 11.2 { + SELECT rowid, value FROM json_each('[1,2,3,4,5]'); + } {0 1 1 2 2 3 3 4 4 5} + + do_execsql_test 11.3 { + SELECT sum(value) OVER (ORDER BY rowid) FROM json_each('[1,2,3,4,5]') + } {1 3 6 10 15} + + do_execsql_test 11.4 { + SELECT sum(value) OVER ( + ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW + ) FROM json_each('[1,2,3,4,5]') + } {1 3 6 10 15} + + do_eqp_test 11.5 { + SELECT sum(value) OVER ( + ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW + ) FROM json_each('[1,2,3,4,5]') + } { + QUERY PLAN + |--CO-ROUTINE (subquery-xxxxxx) + | `--SCAN json_each VIRTUAL TABLE INDEX 1: + `--SCAN (subquery-xxxxxx) + } + + do_eqp_test 11.6 { + SELECT sum(value) OVER (ORDER BY rowid) FROM json_each('[1,2,3,4,5]') + } { + QUERY PLAN + |--CO-ROUTINE (subquery-xxxxxx) + | `--SCAN json_each VIRTUAL TABLE INDEX 1: + `--SCAN (subquery-xxxxxx) + } + + do_eqp_test 11.8 { + SELECT sum(value) OVER (ORDER BY rowid DESC) FROM json_each('[1,2,3,4,5]') + } { + QUERY PLAN + |--CO-ROUTINE (subquery-xxxxxx) + | |--SCAN json_each VIRTUAL TABLE INDEX 1: + | `--USE TEMP B-TREE FOR ORDER BY + `--SCAN (subquery-xxxxxx) + } + + do_execsql_test 11.9 { + SELECT sum(value) OVER (ORDER BY rowid DESC) FROM json_each('[1,2,3,4,5]') + } {5 9 12 14 15} + + do_execsql_test 11.10 { + SELECT sum(value) OVER (ORDER BY value ASC) FROM json_each('[2,1,4,3,5]') + } {1 3 6 10 15} + do_eqp_test 11.11 { + SELECT sum(value) OVER (ORDER BY value ASC) FROM json_each('[2,1,4,3,5]') + } { + QUERY PLAN + |--CO-ROUTINE (subquery-xxxxxx) + | |--SCAN json_each VIRTUAL TABLE INDEX 1: + | `--USE TEMP B-TREE FOR ORDER BY + `--SCAN (subquery-xxxxxx) + } +}} + +finish_test + diff --git a/test/windowC.test b/test/windowC.test new file mode 100644 index 0000000000..013876f9b3 --- /dev/null +++ b/test/windowC.test @@ -0,0 +1,85 @@ +# 2021-09-29 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# Test cases for varying separator handling by group_concat(). +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix windowC + +ifcapable !windowfunc { + finish_test + return +} + +do_execsql_test 1.0 { + CREATE TABLE x1(i INTEGER PRIMARY KEY, x); +} + +foreach {tn bBlob seps} { + 1 0 {a b c def g} + 2 0 {abcdefg {} {} abcdefg} + 3 0 {a bc def ghij klmno pqrstu} + 4 1 {a bc def ghij klmno pqrstu} + 5 1 {, , , , , , , , , , , , ....... , ,} +} { + foreach type {text blob} { + do_test 1.$type.$tn.1 { + execsql { DELETE FROM x1 } + foreach s $seps { + if {$type=="text"} { + execsql {INSERT INTO x1 VALUES(NULL, $s)} + } else { + execsql {INSERT INTO x1 VALUES(NULL, CAST ($s AS blob))} + } + } + } {} + + foreach {tn2 win} { + 1 "ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING" + 2 "ROWS BETWEEN 2 PRECEDING AND CURRENT ROW" + 3 "ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING" + } { + do_test 1.$type.$tn.2.$tn2 { + db eval " + SELECT group_concat('val', x) OVER ( ORDER BY i $win ) AS val FROM x1 + " { + if {[string range $val 0 2]!="val" + || [string range $val end-2 end]!="val" + } { + error "unexpected return value: $val" + } + } + } {} + } + } +} + +# 2021-10-12 dbsqlfuzz 6c31db077a14149a7b22a1069294bdb068be8a96 +# +reset_db +do_execsql_test 2.0 { + PRAGMA encoding=UTF16le; + WITH separator(x) AS (VALUES(',a,'),(',bc,')), + value(y) AS (VALUES(1),(x'5585d09013455178cd11ce4a')) + SELECT group_concat(y,x) OVER (ORDER BY x ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING) + FROM separator, value; +} {{} 1 蕕郐䔓硑ᇍ䫎 1} +reset_db +do_execsql_test 2.1 { + PRAGMA encoding=UTF16be; + WITH separator(x) AS (VALUES(',a,'),(',bc,')), + value(y) AS (VALUES(1),(x'5585d09013455178cd11ce4a')) + SELECT group_concat(y,x) OVER (ORDER BY x ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING) + FROM separator, value; +} {{} 1 喅킐ፅ典촑칊 1} + +finish_test diff --git a/test/windowD.test b/test/windowD.test new file mode 100644 index 0000000000..f2ec36a443 --- /dev/null +++ b/test/windowD.test @@ -0,0 +1,94 @@ +# 2022 June 2 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix windowD + + +do_execsql_test 1.0 { + CREATE TABLE t0(c0 TEXT); + CREATE VIEW v0(c0, c1) + AS SELECT CUME_DIST() OVER (PARTITION BY t0.c0), TRUE FROM t0; + INSERT INTO t0 VALUES ('x'); +} + +do_execsql_test 1.1 { + SELECT ('500') IS (v0.c1) FROM v0; +} { + 0 +} + +do_execsql_test 1.2 { + SELECT (('500') IS (v0.c1)) FROM v0, t0; +} { + 0 +} + +do_execsql_test 1.2 { + SELECT (('500') IS (v0.c1)) IS FALSE FROM v0; +} { + 1 +} + +do_execsql_test 1.3 { + SELECT * FROM v0; +} { + 1.0 1 +} + +do_execsql_test 1.4 { + SELECT * FROM v0 WHERE ('500' IS v0.c1) IS FALSE; +} { + 1.0 1 +} + +#------------------------------------------------------------------------- + +reset_db +do_execsql_test 2.0 { + CREATE TABLE t1(x); + INSERT INTO t1 VALUES('value'); + CREATE VIEW v1(a, b, c, d) AS SELECT 1, 2, TRUE, FALSE FROM t1; +} + +do_execsql_test 2.1 { + SELECT 500 IS a, 500 IS b, 500 IS c, 500 IS d FROM v1 +} {0 0 0 0} + +do_execsql_test 2.2 { + SELECT * FROM v1 WHERE 500 IS c; +} {} + +do_execsql_test 2.3 { + SELECT * FROM v1 WHERE 500 IS d; +} {} + +do_execsql_test 2.4 { + CREATE VIEW v2 AS SELECT max(x) OVER () AS a, TRUE AS c FROM t1; +} + +do_execsql_test 2.5 { + SELECT 500 IS c FROM v2; +} 0 + +do_execsql_test 2.6 { + SELECT * FROM v2 WHERE 500 IS c; +} {} + + + + + + +finish_test + diff --git a/test/windowE.test b/test/windowE.test new file mode 100644 index 0000000000..1cb67f56b0 --- /dev/null +++ b/test/windowE.test @@ -0,0 +1,110 @@ +# 2022 October 18 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix windowE + +proc custom {a b} { return [string compare $a $b] } +db collate custom custom + +do_execsql_test 1.0 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT COLLATE custom); + INSERT INTO t1 VALUES(1, 'one'); + INSERT INTO t1 VALUES(2, 'two'); + INSERT INTO t1 VALUES(3, 'three'); + INSERT INTO t1 VALUES(4, 'four'); + INSERT INTO t1 VALUES(5, 'five'); + INSERT INTO t1 VALUES(6, 'six'); + CREATE INDEX t1b ON t1(b); +} + +do_execsql_test 1.1 { + SELECT * FROM t1 +} { + 1 one 2 two 3 three 4 four 5 five 6 six +} + +do_execsql_test 1.2 { + SELECT group_concat(a,',') OVER win FROM t1 + WINDOW win AS ( + ORDER BY b RANGE BETWEEN 1 PRECEDING AND 2 PRECEDING + ) +} { + 5 4 1 6 3 2 +} + +proc custom {a b} { return [string compare $b $a] } + +do_execsql_test 1.3 { + SELECT group_concat(a,',') OVER win FROM t1 + WINDOW win AS ( + ORDER BY b RANGE BETWEEN 1 PRECEDING AND 2 PRECEDING + ) +} { + 5 5,4 5,4,1 5,4,1,6 5,4,1,6,3 5,4,1,6,3,2 +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 2.0 { + CREATE TABLE t1(x); +} + +sqlite3_create_aggregate db + +breakpoint +do_catchsql_test 2.1 { + SELECT min(x) OVER w1 FROM t1 + WINDOW w1 AS (PARTITION BY x_count(x) OVER w1); +} {1 {x_count() may not be used as a window function}} + +do_catchsql_test 2.2 { + SELECT min(x) FILTER (WHERE x_count(x) OVER w1) OVER w1 FROM t1 + WINDOW w1 AS (PARTITION BY x OVER w1); +} {1 {near "OVER": syntax error}} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 3.0 { + BEGIN TRANSACTION; + CREATE TABLE t2(c1 INT, c2 REAL); + INSERT INTO t2 VALUES + (447,0.0), (448,0.0), (449,0.0), (452,0.0), (453,0.0), (454,0.0), (455,0.0), + (456,0.0), (459,0.0), (460,0.0), (462,0.0), (463,0.0), (466,0.0), (467,0.0), + (468,0.0), (469,0.0), (470,0.0), (473,0.0), (474,0.0), (475,0.0), (476,0.0), + (477,0.0), (480,0.0), (481,0.0), (482,0.0), (483,0.0), (484,0.0), (487,0.0), + (488,0.0), (489,0.0), (490,0.0), (491,0.0), (494,0.0), (495,0.0), (496,0.0), + (497,0.0), (498,0.0), (501,0.0), (502,0.0), (503,0.0), (504,0.0), (505,0.0), + (508,0.0), (509,0.0), (510,0.0), (511,0.0), (512,0.0), (515,0.0), (516,0.0), + (517,0.0), (518,0.0), (519,0.0), (522,0.0), (523,0.0), (524,0.0), (525,0.0), + (526,0.0), (529,0.0), (530,0.0), (531,0.0), (532,0.0), (533,0.0), (536,0.0), + (537,1.0), (538,0.0), (539,0.0), (540,0.0), (543,0.0), (544,0.0); + COMMIT; +} + +do_execsql_test 3.1 { + select c1, max(c2) over (order by c1 range 366.0 preceding) from t2; +} { + 447 0.0 448 0.0 449 0.0 452 0.0 453 0.0 454 0.0 455 0.0 456 0.0 459 0.0 + 460 0.0 462 0.0 463 0.0 466 0.0 467 0.0 468 0.0 469 0.0 470 0.0 473 0.0 + 474 0.0 475 0.0 476 0.0 477 0.0 480 0.0 481 0.0 482 0.0 483 0.0 484 0.0 + 487 0.0 488 0.0 489 0.0 490 0.0 491 0.0 494 0.0 495 0.0 496 0.0 497 0.0 + 498 0.0 501 0.0 502 0.0 503 0.0 504 0.0 505 0.0 508 0.0 509 0.0 510 0.0 + 511 0.0 512 0.0 515 0.0 516 0.0 517 0.0 518 0.0 519 0.0 522 0.0 523 0.0 + 524 0.0 525 0.0 526 0.0 529 0.0 530 0.0 531 0.0 532 0.0 533 0.0 536 0.0 + 537 1.0 538 1.0 539 1.0 540 1.0 543 1.0 544 1.0 +} + + +finish_test + diff --git a/test/windowerr.tcl b/test/windowerr.tcl new file mode 100644 index 0000000000..294e68dc18 --- /dev/null +++ b/test/windowerr.tcl @@ -0,0 +1,72 @@ +# 2018 May 19 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +source [file join [file dirname $argv0] pg_common.tcl] + +#========================================================================= + +start_test windowerr "2019 March 01" +ifcapable !windowfunc + +execsql_test 1.0 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a INTEGER, b INTEGER); + INSERT INTO t1 VALUES(1, 1); + INSERT INTO t1 VALUES(2, 2); + INSERT INTO t1 VALUES(3, 3); + INSERT INTO t1 VALUES(4, 4); + INSERT INTO t1 VALUES(5, 5); +} + +foreach {tn frame} { + 1 "ORDER BY a ROWS BETWEEN -1 PRECEDING AND 1 FOLLOWING" + 2 "ORDER BY a ROWS BETWEEN 1 PRECEDING AND -1 FOLLOWING" + + 3 "ORDER BY a RANGE BETWEEN -1 PRECEDING AND 1 FOLLOWING" + 4 "ORDER BY a RANGE BETWEEN 1 PRECEDING AND -1 FOLLOWING" + + 5 "ORDER BY a GROUPS BETWEEN -1 PRECEDING AND 1 FOLLOWING" + 6 "ORDER BY a GROUPS BETWEEN 1 PRECEDING AND -1 FOLLOWING" + + 7 "ORDER BY a,b RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING" + + 8 "PARTITION BY a RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING" +} { + errorsql_test 1.$tn " + SELECT a, sum(b) OVER ( + $frame + ) FROM t1 ORDER BY 1 + " +} +errorsql_test 2.1 { + SELECT sum( sum(a) OVER () ) FROM t1; +} + +errorsql_test 2.2 { + SELECT sum(a) OVER () AS xyz FROM t1 ORDER BY sum(xyz); +} + +errorsql_test 3.0 { + SELECT sum(a) OVER win FROM t1 + WINDOW win AS (ROWS BETWEEN 'hello' PRECEDING AND 10 FOLLOWING) +} +errorsql_test 3.2 { + SELECT sum(a) OVER win FROM t1 + WINDOW win AS (ROWS BETWEEN 10 PRECEDING AND x'ABCD' FOLLOWING) +} + +errorsql_test 3.3 { + SELECT row_number(a) OVER () FROM t1; +} + +finish_test + diff --git a/test/windowerr.test b/test/windowerr.test new file mode 100644 index 0000000000..40d994de85 --- /dev/null +++ b/test/windowerr.test @@ -0,0 +1,116 @@ +# 2019 March 01 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. +# + +#################################################### +# DO NOT EDIT! THIS FILE IS AUTOMATICALLY GENERATED! +#################################################### + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix windowerr + +ifcapable !windowfunc { finish_test ; return } +do_execsql_test 1.0 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a INTEGER, b INTEGER); + INSERT INTO t1 VALUES(1, 1); + INSERT INTO t1 VALUES(2, 2); + INSERT INTO t1 VALUES(3, 3); + INSERT INTO t1 VALUES(4, 4); + INSERT INTO t1 VALUES(5, 5); +} {} + +# PG says ERROR: frame starting offset must not be negative +do_test 1.1 { catch { execsql { + SELECT a, sum(b) OVER ( + ORDER BY a ROWS BETWEEN -1 PRECEDING AND 1 FOLLOWING + ) FROM t1 ORDER BY 1 +} } } 1 + +# PG says ERROR: frame ending offset must not be negative +do_test 1.2 { catch { execsql { + SELECT a, sum(b) OVER ( + ORDER BY a ROWS BETWEEN 1 PRECEDING AND -1 FOLLOWING + ) FROM t1 ORDER BY 1 +} } } 1 + +# PG says ERROR: invalid preceding or following size in window function +do_test 1.3 { catch { execsql { + SELECT a, sum(b) OVER ( + ORDER BY a RANGE BETWEEN -1 PRECEDING AND 1 FOLLOWING + ) FROM t1 ORDER BY 1 +} } } 1 + +# PG says ERROR: invalid preceding or following size in window function +do_test 1.4 { catch { execsql { + SELECT a, sum(b) OVER ( + ORDER BY a RANGE BETWEEN 1 PRECEDING AND -1 FOLLOWING + ) FROM t1 ORDER BY 1 +} } } 1 + +# PG says ERROR: frame starting offset must not be negative +do_test 1.5 { catch { execsql { + SELECT a, sum(b) OVER ( + ORDER BY a GROUPS BETWEEN -1 PRECEDING AND 1 FOLLOWING + ) FROM t1 ORDER BY 1 +} } } 1 + +# PG says ERROR: frame ending offset must not be negative +do_test 1.6 { catch { execsql { + SELECT a, sum(b) OVER ( + ORDER BY a GROUPS BETWEEN 1 PRECEDING AND -1 FOLLOWING + ) FROM t1 ORDER BY 1 +} } } 1 + +# PG says ERROR: RANGE with offset PRECEDING/FOLLOWING requires exactly one ORDER BY column +do_test 1.7 { catch { execsql { + SELECT a, sum(b) OVER ( + ORDER BY a,b RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING + ) FROM t1 ORDER BY 1 +} } } 1 + +# PG says ERROR: RANGE with offset PRECEDING/FOLLOWING requires exactly one ORDER BY column +do_test 1.8 { catch { execsql { + SELECT a, sum(b) OVER ( + PARTITION BY a RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING + ) FROM t1 ORDER BY 1 +} } } 1 + +# PG says ERROR: aggregate function calls cannot contain window function calls +do_test 2.1 { catch { execsql { + SELECT sum( sum(a) OVER () ) FROM t1; +} } } 1 + +# PG says ERROR: column "xyz" does not exist +do_test 2.2 { catch { execsql { + SELECT sum(a) OVER () AS xyz FROM t1 ORDER BY sum(xyz); +} } } 1 + +# PG says ERROR: invalid input syntax for integer: "hello" +do_test 3.0 { catch { execsql { + SELECT sum(a) OVER win FROM t1 + WINDOW win AS (ROWS BETWEEN 'hello' PRECEDING AND 10 FOLLOWING) +} } } 1 + +# PG says ERROR: argument of ROWS must be type bigint, not type bit +do_test 3.2 { catch { execsql { + SELECT sum(a) OVER win FROM t1 + WINDOW win AS (ROWS BETWEEN 10 PRECEDING AND x'ABCD' FOLLOWING) +} } } 1 + +# PG says ERROR: function row_number(integer) does not exist +do_test 3.3 { catch { execsql { + SELECT row_number(a) OVER () FROM t1; +} } } 1 + +finish_test diff --git a/test/windowfault.test b/test/windowfault.test new file mode 100644 index 0000000000..5340ce08fd --- /dev/null +++ b/test/windowfault.test @@ -0,0 +1,334 @@ +# 2018 May 8 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix windowfault + +ifcapable !windowfunc { + finish_test + return +} + +do_execsql_test 1.0 { + CREATE TABLE t1(a, b, c, d); + INSERT INTO t1 VALUES(1, 2, 3, 4); + INSERT INTO t1 VALUES(5, 6, 7, 8); + INSERT INTO t1 VALUES(9, 10, 11, 12); +} +faultsim_save_and_close + +do_faultsim_test 1 -start 1 -faults oom-t* -prep { + faultsim_restore_and_reopen +} -body { + execsql { + SELECT row_number() OVER win, + rank() OVER win, + dense_rank() OVER win, + ntile(2) OVER win, + first_value(d) OVER win, + last_value(d) OVER win, + nth_value(d,2) OVER win, + lead(d) OVER win, + lag(d) OVER win, + max(d) OVER win, + min(d) OVER win + FROM t1 + WINDOW win AS (ORDER BY a) + } +} -test { + faultsim_test_result {0 {1 1 1 1 4 4 {} 8 {} 4 4 2 2 2 1 4 8 8 12 4 8 4 3 3 3 2 4 12 8 {} 8 12 4}} +} + +do_faultsim_test 1.1 -faults oom-t* -prep { + faultsim_restore_and_reopen +} -body { + execsql { + SELECT row_number() OVER win, + rank() OVER win, + dense_rank() OVER win + FROM t1 + WINDOW win AS (PARTITION BY c<7 ORDER BY a) + } +} -test { + faultsim_test_result {0 {1 1 1 2 2 2 1 1 1}} +} + +do_faultsim_test 1.2 -faults oom-t* -prep { + faultsim_restore_and_reopen +} -body { + execsql { + SELECT ntile(105) + OVER ( RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) + FROM t1 + } +} -test { + faultsim_test_result {0 {1 2 3}} +} + +do_faultsim_test 2 -start 1 -faults oom-* -prep { + faultsim_restore_and_reopen +} -body { + execsql { + SELECT round(percent_rank() OVER win, 2), + round(cume_dist() OVER win, 2) + FROM t1 + WINDOW win AS (ORDER BY a) + } +} -test { + faultsim_test_result {0 {0.0 0.33 0.5 0.67 1.0 1.0}} +} + +do_faultsim_test 3 -faults oom-* -prep { + faultsim_restore_and_reopen +} -body { + execsql { + SELECT min(d) OVER win, max(d) OVER win + FROM t1 + WINDOW win AS (ORDER BY a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) + } +} -test { + faultsim_test_result {0 {4 12 8 12 12 12}} +} + +do_faultsim_test 4 -faults oom-* -prep { + faultsim_restore_and_reopen +} -body { + execsql { + CREATE VIEW aaa AS + SELECT min(d) OVER w, max(d) OVER w + FROM t1 + WINDOW w AS (ORDER BY a RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING); + SELECT * FROM aaa; + } +} -test { + faultsim_test_result {0 {4 12 8 12 12 12}} +} + +do_faultsim_test 5 -start 1 -faults oom-* -prep { + faultsim_restore_and_reopen +} -body { + execsql { + SELECT last_value(a) OVER win1, + last_value(a) OVER win2 + FROM t1 + WINDOW win1 AS (ORDER BY a ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING), + win2 AS (ORDER BY a) + } +} -test { + faultsim_test_result {0 {5 1 9 5 9 9}} +} + +do_faultsim_test 6 -faults oom-* -prep { + faultsim_restore_and_reopen +} -body { + execsql { + SELECT percent_rank() OVER (), cume_dist() OVER () FROM t1 + } +} -test { + faultsim_test_result {0 {0.0 1.0 0.0 1.0 0.0 1.0}} +} + +do_faultsim_test 7 -faults oom-* -prep { + faultsim_restore_and_reopen +} -body { + execsql { + SELECT percent_rank() OVER (), cume_dist() OVER () FROM t1 + } +} -test { + faultsim_test_result {0 {0.0 1.0 0.0 1.0 0.0 1.0}} +} + +do_faultsim_test 8 -faults oom-t* -prep { + faultsim_restore_and_reopen +} -body { + execsql { + SELECT a, sum(b) OVER win1 FROM t1 + WINDOW win1 AS (PARTITION BY a ), + win2 AS (PARTITION BY b ) + ORDER BY a; + } +} -test { + faultsim_test_result {0 {1 2 5 6 9 10}} +} + +#------------------------------------------------------------------------- +# The following test causes a cursor in REQURESEEK state to be passed +# to sqlite3BtreeDelete(). An error is simulated within the seek operation +# to restore the cursors position. +# +reset_db +set big [string repeat x 900] +do_execsql_test 9.0 { + PRAGMA page_size = 512; + PRAGMA cache_size = 2; + CREATE TABLE t(x INTEGER PRIMARY KEY, y TEXT); + WITH s(i) AS ( + VALUES(1) UNION ALL SELECT i+1 FROM s WHERE i<1900 + ) + INSERT INTO t(y) SELECT $big FROM s; +} +db close + +testvfs tvfs -default 1 +tvfs script vfs_callback +tvfs filter xRead + +sqlite3 db test.db +proc vfs_callback {method file args} { + if {$file=="" && [info exists ::tmp_read_fail]} { + incr ::tmp_read_fail -1 + if {$::tmp_read_fail<=0} { + return "SQLITE_IOERR" + } + } + return "SQLITE_OK" +} + +set FAULTSIM(tmpread) [list \ + -injectstart tmpread_injectstart \ + -injectstop tmpread_injectstop \ + -injecterrlist {{1 {disk I/O error}}} \ +] +proc tmpread_injectstart {iFail} { + set ::tmp_read_fail $iFail +} +proc tmpread_injectstop {} { + set ret [expr $::tmp_read_fail<=0] + unset -nocomplain ::tmp_read_fail + return $ret +} + +set L [db eval {SELECT 0.0 FROM t}] +do_faultsim_test 9 -end 25 -faults tmpread -body { + execsql { + SELECT sum(y) OVER win FROM t + WINDOW win AS ( + ORDER BY x ROWS BETWEEN UNBOUNDED PRECEDING AND 1800 FOLLOWING + ) + } +} -test { + faultsim_test_result [list 0 $::L] +} + +catch {db close} +tvfs delete + +reset_db +do_execsql_test 10.0 { + CREATE TABLE t1(a, b, c, d); + CREATE TABLE t2(a, b, c, d); +} + +do_faultsim_test 10 -faults oom* -prep { +} -body { + execsql { + SELECT row_number() OVER win + FROM t1 + WINDOW win AS ( + ORDER BY ( + SELECT percent_rank() OVER win2 FROM t2 + WINDOW win2 AS (ORDER BY a) + ) + ) + } +} -test { + faultsim_test_result {0 {}} +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 11.0 { + DROP TABLE IF EXISTS t0; + CREATE TABLE t0(c0 INTEGER UNIQUE); + INSERT INTO t0 VALUES(0); +} {} + +do_faultsim_test 11.1 -faults oom* -prep { +} -body { + execsql { + SELECT * FROM t0 WHERE + (0, t0.c0) IN (SELECT DENSE_RANK() OVER(), LAG(0) OVER() FROM t0); + } +} -test { + faultsim_test_result {0 {}} +} + +do_faultsim_test 11.2 -faults oom* -prep { +} -body { + execsql { + VALUES(false),(current_date collate binary) + intersect + values(count() not like group_concat(cast(cast(0e00 as text) as integer) <= NULL || 0.4e-0 || 0x8 & true ) over () collate rtrim); + } +} -test { + faultsim_test_result {0 {}} +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 12.0 { + CREATE TABLE t1(a, b, c); +} {} +do_faultsim_test 12 -faults oom* -prep { +} -body { + execsql { + WITH v(a, b, row_number) AS ( + SELECT a, b, row_number() OVER (PARTITION BY a COLLATE nocase ORDER BY b) FROM t1 + ) + SELECT * FROM v WHERE a=2 + } +} -test { + faultsim_test_result {0 {}} +} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 13.0 { + CREATE TABLE t1(id INTEGER PRIMARY KEY, a, b); + INSERT INTO t1 VALUES(1, '1', 'a'); + INSERT INTO t1 VALUES(2, '22', 'b'); + INSERT INTO t1 VALUES(3, '333', 'c'); + INSERT INTO t1 VALUES(4, '4444', 'dddd'); + INSERT INTO t1 VALUES(5, '55555', 'e'); + INSERT INTO t1 VALUES(6, '666666', 'f'); + INSERT INTO t1 VALUES(7, '7777777', 'gggggggggg'); +} {} + +set queryres [list {*}{ + 1b22 + 1b22c333 + 22c333dddd4444 + 333dddd4444e55555 + 4444e55555f666666 + 55555f666666gggggggggg7777777 + 666666gggggggggg7777777 +}] +do_execsql_test 13.1 { + SELECT group_concat(a, b) OVER ( + ORDER BY id RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING + ) FROM t1 +} $queryres + +do_faultsim_test 13 -faults oom* -prep { +} -body { + execsql { + SELECT group_concat(a, b) OVER ( + ORDER BY id RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING + ) FROM t1 + } +} -test { + faultsim_test_result [list 0 $::queryres] +} + +finish_test diff --git a/test/windowpushd.test b/test/windowpushd.test new file mode 100644 index 0000000000..4bfb2c523d --- /dev/null +++ b/test/windowpushd.test @@ -0,0 +1,240 @@ +# 2021 February 23 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is testing the push-down optimization when +# WHERE constraints are pushed down into a sub-query that uses +# window functions. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix windowpushd + +do_execsql_test 1.0 { + CREATE TABLE t1(id INTEGER PRIMARY KEY, grp_id); + CREATE INDEX i1 ON t1(grp_id); + CREATE VIEW lll AS SELECT + row_number() OVER (PARTITION BY grp_id), + grp_id, id + FROM t1 +} + +do_execsql_test 1.1 { + INSERT INTO t1 VALUES + (1, 2), (2, 3), (3, 3), (4, 1), (5, 1), + (6, 1), (7, 1), (8, 1), (9, 3), (10, 3), + (11, 2), (12, 3), (13, 3), (14, 2), (15, 1), + (16, 2), (17, 1), (18, 2), (19, 3), (20, 2) +} + +do_execsql_test 1.2 { + SELECT * FROM lll +} { + 1 1 4 2 1 5 3 1 6 4 1 7 5 1 8 6 1 15 7 1 17 + 1 2 1 2 2 11 3 2 14 4 2 16 5 2 18 6 2 20 + 1 3 2 2 3 3 3 3 9 4 3 10 5 3 12 6 3 13 7 3 19 +} + +do_execsql_test 1.3 { + SELECT * FROM lll WHERE grp_id=2 +} { + 1 2 1 2 2 11 3 2 14 4 2 16 5 2 18 6 2 20 +} + +do_eqp_test 1.4 { + SELECT * FROM lll WHERE grp_id=2 +} {SEARCH t1 USING COVERING INDEX i1 (grp_id=?)} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 2.0 { + CREATE TABLE t1(a, b, c, d); + INSERT INTO t1 VALUES('A', 'C', 1, 0.1); + INSERT INTO t1 VALUES('A', 'D', 2, 0.2); + INSERT INTO t1 VALUES('A', 'E', 3, 0.3); + INSERT INTO t1 VALUES('A', 'C', 4, 0.4); + INSERT INTO t1 VALUES('B', 'D', 5, 0.5); + INSERT INTO t1 VALUES('B', 'E', 6, 0.6); + INSERT INTO t1 VALUES('B', 'C', 7, 0.7); + INSERT INTO t1 VALUES('B', 'D', 8, 0.8); + INSERT INTO t1 VALUES('C', 'E', 9, 0.9); + INSERT INTO t1 VALUES('C', 'C', 10, 1.0); + INSERT INTO t1 VALUES('C', 'D', 11, 1.1); + INSERT INTO t1 VALUES('C', 'E', 12, 1.2); + + CREATE INDEX i1 ON t1(a); + CREATE INDEX i2 ON t1(b); + + CREATE VIEW v1 AS SELECT a, c, max(c) OVER (PARTITION BY a) FROM t1; + + CREATE VIEW v2 AS SELECT a, c, + max(c) OVER (PARTITION BY a), + row_number() OVER () + FROM t1; + + CREATE VIEW v3 AS SELECT b, d, + max(d) OVER (PARTITION BY b), + row_number() OVER (PARTITION BY b) + FROM t1; + + CREATE TABLE t2(x, y, z); + INSERT INTO t2 VALUES('W', 3, 1); + INSERT INTO t2 VALUES('W', 2, 2); + INSERT INTO t2 VALUES('X', 1, 4); + INSERT INTO t2 VALUES('X', 5, 7); + INSERT INTO t2 VALUES('Y', 1, 9); + INSERT INTO t2 VALUES('Y', 4, 2); + INSERT INTO t2 VALUES('Z', 3, 3); + INSERT INTO t2 VALUES('Z', 3, 4); +} + +foreach tn {0 1} { + if {$tn} { + optimization_control db all on + } else { + optimization_control db push-down off + } + + do_execsql_test 2.$tn.1.1 { + SELECT * FROM v1; + } { + A 1 4 A 2 4 A 3 4 A 4 4 + B 5 8 B 6 8 B 7 8 B 8 8 + C 9 12 C 10 12 C 11 12 C 12 12 + } + + do_execsql_test 2.$tn.1.2 { + SELECT * FROM v1 WHERE a IN ('A', 'B'); + } { + A 1 4 A 2 4 A 3 4 A 4 4 + B 5 8 B 6 8 B 7 8 B 8 8 + } + + do_execsql_test 2.$tn.1.3 { + SELECT * FROM v1 WHERE a IS 'C' + } { + C 9 12 C 10 12 C 11 12 C 12 12 + } + + if {$tn==1} { + do_eqp_test 2.$tn.1.4 { + SELECT * FROM v1 WHERE a IN ('A', 'B'); + } {USING INDEX i1 (a=?)} + + do_eqp_test 2.$tn.1.5 { + SELECT * FROM v1 WHERE a = 'c' COLLATE nocase + } {USING INDEX i1} + } + + do_execsql_test 2.$tn.2.1 { + SELECT * FROM v2; + } { + A 1 4 1 A 2 4 2 A 3 4 3 A 4 4 4 + B 5 8 5 B 6 8 6 B 7 8 7 B 8 8 8 + C 9 12 9 C 10 12 10 C 11 12 11 C 12 12 12 + } + + do_execsql_test 2.$tn.2.2 { + SELECT * FROM v2 WHERE a = 'C'; + } { + C 9 12 9 C 10 12 10 C 11 12 11 C 12 12 12 + } + + do_execsql_test 2.$tn.3.1 { SELECT * FROM v3; } { + C 0.1 1.0 1 C 0.4 1.0 2 C 0.7 1.0 3 C 1.0 1.0 4 + D 0.2 1.1 1 D 0.5 1.1 2 D 0.8 1.1 3 D 1.1 1.1 4 + E 0.3 1.2 1 E 0.6 1.2 2 E 0.9 1.2 3 E 1.2 1.2 4 + } + + do_execsql_test 2.$tn.3.2 { SELECT * FROM v3 WHERE b<'E' } { + C 0.1 1.0 1 C 0.4 1.0 2 C 0.7 1.0 3 C 1.0 1.0 4 + D 0.2 1.1 1 D 0.5 1.1 2 D 0.8 1.1 3 D 1.1 1.1 4 + } + + if {$tn==1} { + do_eqp_test 2.$tn.3.3 { + SELECT * FROM v3 WHERE b='E' + } {SEARCH t1 USING INDEX i2 (b=?)} + do_eqp_test 2.$tn.3.4 { + SELECT * FROM v3 WHERE b>'C' + } {SEARCH t1 USING INDEX i2 (b>?)} + } + + do_execsql_test 2.$tn.3.5 { SELECT * FROM v3 WHERE d<0.55; } { + C 0.1 1.0 1 C 0.4 1.0 2 + D 0.2 1.1 1 D 0.5 1.1 2 + E 0.3 1.2 1 + } + if {$tn==1} { + do_eqp_test 2.$tn.3.6 { + SELECT * FROM v3 WHERE d<0.55 + } {SCAN t1 USING INDEX i2} + } + + do_execsql_test 2.$tn.4.1 { + SELECT * FROM ( + SELECT x, sum(y) AS s, max(z) AS m + FROM t2 GROUP BY x + ) + } { + W 5 2 + X 6 7 + Y 5 9 + Z 6 4 + } + + do_execsql_test 2.$tn.4.1 { + SELECT * FROM ( + SELECT x, sum(y) AS s, max(z) AS m, + max( max(z) ) OVER (PARTITION BY sum(y) + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) + FROM t2 GROUP BY x + ) + } { + W 5 2 9 + Y 5 9 9 + X 6 7 7 + Z 6 4 7 + } + + do_execsql_test 2.$tn.4.2 { + SELECT * FROM ( + SELECT x, sum(y) AS s, max(z) AS m, + max( max(z) ) OVER (PARTITION BY sum(y) + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) + FROM t2 GROUP BY x + ) WHERE s=6 + } { + X 6 7 7 + Z 6 4 7 + } + + do_execsql_test 2.$tn.4.3 { + SELECT * FROM ( + SELECT x, sum(y) AS s, max(z) AS m, + max( max(z) ) OVER (PARTITION BY sum(y) + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) + FROM t2 GROUP BY x + ) WHERE s<6 + } { + W 5 2 9 + Y 5 9 9 + } + +} + + + + +finish_test diff --git a/test/with1.test b/test/with1.test index 7345c5ceb3..5ddf9dce0b 100644 --- a/test/with1.test +++ b/test/with1.test @@ -352,7 +352,7 @@ do_catchsql_test 7.4 { SELECT i FROM tree WHERE p IN (SELECT id FROM t) ) SELECT id FROM t; -} {1 {recursive reference in a subquery: t}} +} {1 {circular reference: t}} do_catchsql_test 7.5 { WITH t(id) AS ( @@ -865,6 +865,27 @@ do_catchsql_test 16.1 { SELECT * FROM i; } {1 {recursive aggregate queries not supported}} +# Or window-function recursive queries. Ticket e8275b41. +# +ifcapable windowfunc { + do_catchsql_test 16.2 { + WITH RECURSIVE + i(x) AS (VALUES(1) UNION SELECT count(*) OVER () FROM i) + SELECT * FROM i; + } {1 {cannot use window functions in recursive queries}} + do_catchsql_test 16.3 { + WITH RECURSIVE + t(id, parent) AS (VALUES(1,2)), + q(id, parent, rn) AS ( + VALUES(1,2,3) + UNION ALL + SELECT t.*, ROW_NUMBER() OVER (ORDER BY t.id) AS rn + FROM q JOIN t ON t.parent = q.id + ) + SELECT * FROM q; + } {1 {cannot use window functions in recursive queries}} +} + #------------------------------------------------------------------------- do_execsql_test 17.1 { WITH x(a) AS ( @@ -990,5 +1011,230 @@ do_execsql_test 18.2 { SELECT 1 FROM xyz; } 1 +# EXPLAIN QUERY PLAN on a self-join of a CTE +# +do_execsql_test 19.1a { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(x); +} +do_eqp_test 19.1b { + WITH + x1(a) AS (values(100)) + INSERT INTO t1(x) + SELECT * FROM (WITH x2(y) AS (SELECT * FROM x1) SELECT y+a FROM x1, x2); + SELECT * FROM t1; +} { + QUERY PLAN + |--MATERIALIZE x1 + | `--SCAN CONSTANT ROW + |--SCAN x1 + `--SCAN x1 +} + +# 2017-10-28. +# See check-in https://sqlite.org/src/info/0926df095faf72c2 +# Tried to optimize co-routine processing by changing a Copy opcode +# into SCopy. But OSSFuzz found two (similar) cases where that optimization +# does not work. +# +do_execsql_test 20.1 { + WITH c(i)AS(VALUES(9)UNION SELECT~i FROM c)SELECT max(5)>i fROM c; +} {0} +do_execsql_test 20.2 { + WITH c(i)AS(VALUES(5)UNIoN SELECT 0)SELECT min(1)-i fROM c; +} {1} + +# 2018-12-26 +# Two different CTE tables with the same name appear in within a single FROM +# clause due to the query-flattener optimization. make sure this does not cause +# problems. This problem was discovered by Matt Denton. +# +do_execsql_test 21.1 { + WITH RECURSIVE t21(a,b) AS ( + WITH t21(x) AS (VALUES(1)) + SELECT x, x FROM t21 ORDER BY 1 + ) + SELECT * FROM t21 AS tA, t21 AS tB +} {1 1 1 1} +do_execsql_test 21.1b { + /* This variant from chromium bug 922312 on 2019-01-16 */ + WITH RECURSIVE t21(a,b) AS ( + WITH t21(x) AS (VALUES(1)) + SELECT x, x FROM t21 ORDER BY 1 LIMIT 5 + ) + SELECT * FROM t21 AS tA, t21 AS tB +} {1 1 1 1} +do_execsql_test 21.2 { + SELECT printf('', + EXISTS (WITH RECURSIVE Table0 AS (WITH Table0 AS (SELECT DISTINCT 1) + SELECT *, * FROM Table0 ORDER BY 1 DESC) + SELECT * FROM Table0 NATURAL JOIN Table0)); +} {{}} + +# 2019-01-17 +# Make sure crazy nexted CTE joins terminate with an error quickly. +# +do_catchsql_test 22.1 { + WITH RECURSIVE c AS NOT MATERIALIZED ( + WITH RECURSIVE c AS NOT MATERIALIZED ( + WITH RECURSIVE c AS NOT MATERIALIZED ( + WITH RECURSIVE c AS NOT MATERIALIZED ( + WITH c AS (VALUES(0)) + SELECT 1 FROM c LEFT JOIN c ON ltrim(1) + ) + SELECT 1 FROM c,c,c,c,c,c,c,c,c + ) + SELECT 2 FROM c,c,c,c,c,c,c,c,c + ) + SELECT 3 FROM c,c,c,c,c,c,c,c,c + ) + SELECT 4 FROM c,c,c,c,c,c,c,c,c; +} {1 {too many FROM clause terms, max: 200}} + +# 2019-05-22 +# ticket https://sqlite.org/src/tktview/ce823231949d3abf42453c8f20 +# +sqlite3 db :memory: +do_execsql_test 23.1 { + CREATE TABLE t1(id INTEGER NULL PRIMARY KEY, name Text); + INSERT INTO t1 VALUES (1, 'john'); + INSERT INTO t1 VALUES (2, 'james'); + INSERT INTO t1 VALUES (3, 'jingle'); + INSERT INTO t1 VALUES (4, 'himer'); + INSERT INTO t1 VALUES (5, 'smith'); + CREATE VIEW v2 AS + WITH t4(Name) AS (VALUES ('A'), ('B')) + SELECT Name Name FROM t4; + CREATE VIEW v3 AS + WITH t4(Att, Val, Act) AS (VALUES + ('C', 'D', 'E'), + ('F', 'G', 'H') + ) + SELECT D.Id Id, P.Name Protocol, T.Att Att, T.Val Val, T.Act Act + FROM t1 D + CROSS JOIN v2 P + CROSS JOIN t4 T; + SELECT * FROM v3; +} {1 A C D E 1 A F G H 1 B C D E 1 B F G H 2 A C D E 2 A F G H 2 B C D E 2 B F G H 3 A C D E 3 A F G H 3 B C D E 3 B F G H 4 A C D E 4 A F G H 4 B C D E 4 B F G H 5 A C D E 5 A F G H 5 B C D E 5 B F G H} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 24.1 { + CREATE TABLE t1(a, b, c); + CREATE VIEW v1 AS SELECT max(a), min(b) FROM t1 GROUP BY c; +} +do_test 24.1 { + set program [db eval {EXPLAIN SELECT * FROM v1 AS aa, v1 AS bb, v1 AS cc}] + expr [lsearch $program OpenDup]>0 +} {1} +do_execsql_test 24.2 { + ATTACH "" AS aux; + CREATE VIEW aux.v3 AS VALUES(1); + CREATE VIEW main.v3 AS VALUES(3); + + CREATE VIEW aux.v2 AS SELECT * FROM v3; + CREATE VIEW main.v2 AS SELECT * FROM v3; + + SELECT * FROM main.v2 AS a, aux.v2 AS b, aux.v2 AS c, main.v2 AS d; +} { + 3 1 1 3 +} + +# 2020-01-02 chromium ticket 1033461 +# Do not allow the generated name of a CTE be "true" or "false" as +# such a label might be later confused for the boolean literals of +# the same name, causing inconsistencies in the abstract syntax +# tree. This problem first arose in version 3.23.0 when SQLite +# began recognizing "true" and "false" as boolean literals, but also +# had to continue to recognize "true" and "false" as identifiers for +# backwards compatibility. +# +foreach {id dual} { + 1 {CREATE TABLE dual AS SELECT 'X' AS dummy} + 2 {CREATE TEMP TABLE dual AS SELECT 'X' AS dummy} + 3 {CREATE VIEW dual(dummy) AS VALUES('X')} + 4 {CREATE TEMP VIEW dual(dummy) AS VALUES('X')} +} { + reset_db + db eval $dual + do_execsql_test 25.$id { + WITH cte1 AS ( + SELECT TRUE, ( + WITH cte2 AS (SELECT avg(DISTINCT TRUE) FROM dual) + SELECT 2571 FROM cte2 + ) AS subquery1 + FROM dual + GROUP BY 1 + ) + SELECT (SELECT 1324 FROM cte1) FROM cte1; + } {1324} +} + +do_catchsql_test 26.0 { + WITH i(x) AS ( + VALUES(1) UNION ALL SELECT x+1 FRO, a.b,O. * ,I¬i O, a.b,O. * ORDER BY 1 + ) + SELECT x,O. * O FROM i ¬I,I? 10; +} {1 {near "O": syntax error}} + +# 2020-09-17 ticket c51489c3b8f919c5 +# DISTINCT cannot be ignored in a UNION ALL recursive CTE +# +reset_db +do_execsql_test 26.1 { + CREATE TABLE t (label VARCHAR(10), step INTEGER); + INSERT INTO T VALUES('a', 1); + INSERT INTO T VALUES('a', 1); + INSERT INTO T VALUES('b', 1); + WITH RECURSIVE cte(label, step) AS ( + SELECT DISTINCT * FROM t + UNION ALL + SELECT label, step + 1 FROM cte WHERE step < 3 + ) + SELECT * FROM cte ORDER BY +label, +step; +} {a 1 a 2 a 3 b 1 b 2 b 3} +do_execsql_test 26.2 { + WITH RECURSIVE cte(label, step) AS ( + SELECT * FROM t + UNION + SELECT label, step + 1 FROM cte WHERE step < 3 + ) + SELECT * FROM cte ORDER BY +label, +step; +} {a 1 a 2 a 3 b 1 b 2 b 3} +do_execsql_test 26.3 { + CREATE TABLE tworow(x); + INSERT INTO tworow(x) VALUES(1),(2); + DELETE FROM t WHERE rowid=2; + WITH RECURSIVE cte(label, step) AS ( + SELECT * FROM t + UNION ALL + SELECT DISTINCT label, step + 1 FROM cte, tworow WHERE step < 3 + ) + SELECT * FROM cte ORDER BY +label, +step; +} {a 1 a 2 a 3 b 1 b 2 b 3} + +# 2021-05-20 +# forum post https://sqlite.org/forum/forumpost/8590e3f6dc +# +reset_db +do_execsql_test 27.1 { + CREATE TABLE t1(k); + CREATE TABLE log(k, cte_map, main_map); + CREATE TABLE map(k, v); + INSERT INTO map VALUES(1, 'main1'), (2, 'main2'); + + CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN + INSERT INTO log + WITH map(k,v) AS (VALUES(1,'cte1'),(2,'cte2')) + SELECT + new.k, + (SELECT v FROM map WHERE k=new.k), + (SELECT v FROM main.map WHERE k=new.k); + END; + + INSERT INTO t1 VALUES(1); + INSERT INTO t1 VALUES(2); + SELECT k, cte_map, main_map, '|' FROM log ORDER BY k; +} {1 cte1 main1 | 2 cte2 main2 |} finish_test diff --git a/test/with2.test b/test/with2.test index 02d10b5112..68790fe860 100644 --- a/test/with2.test +++ b/test/with2.test @@ -156,6 +156,16 @@ do_execsql_test 1.15 { SELECT * FROM t4; } {4 5} +do_execsql_test 1.15.2 { + WITH + t4(x) AS ( + VALUES(4) + UNION ALL + SELECT x+1 FROM (SELECT * FROM main.t4) WHERE x<10 + ) + SELECT * FROM t4; +} {4 5} + do_catchsql_test 1.16 { WITH t4(x) AS ( @@ -326,7 +336,7 @@ do_catchsql_test 6.5 { do_catchsql_test 6.6 { WITH x AS (SELECT * FROM t1) DELETE FROM t2 WHERE -} {/1 {near .* syntax error}/} +} {1 {incomplete input}} do_catchsql_test 6.7 { WITH x AS (SELECT * FROM t1) DELETE FROM t2 WHRE 1; @@ -414,5 +424,215 @@ do_execsql_test 8.3 { SELECT * FROM q; } {1 2 3 4 5} +# 2021-03-18 +# Ticket bb8a9fd4a9b7fce5 +reset_db +do_execsql_test 9.1 { + WITH xyz(a) AS ( + WITH abc AS ( SELECT 1234 ) SELECT * FROM abc + ) + SELECT * FROM xyz AS one, xyz AS two, ( + SELECT * FROM xyz UNION ALL SELECT * FROM xyz + ); +} {1234 1234 1234 1234 1234 1234} +ifcapable vtab { +load_static_extension db series +do_execsql_test 9.2 { + WITH + cst(rsx, rsy) AS ( + SELECT 100, 100 + ), + cst2(minx, maxx, stepx, miny, maxy, stepy, qualitativex, qualitativey) AS ( + SELECT NULL, NULL, NULL, NULL, NULL, NULL, 0, 0 + ), + ds0(m, n, x, y, x2, y2, title, size, mark, label, markmode) AS ( + SELECT 1, 2, 3, 4, 5, 6, 7 , 8, 9, 10, 11 + ), + ds(m, n, x, y, x2, y2, title, size, mark, label, markmode) AS ( + SELECT m, n, x, + y, x2, + y2, + title, size, mark, label, markmode + FROM ds0 + WINDOW w AS (PARTITION BY m, x ORDER BY n) + ), + d(m, n, x, y, x2, y2, labelx,labely,title,size,mark,label,markmode) AS ( + SELECT m, n, x, y, x2, y2, x, y, title, size, mark, label, markmode + FROM ds, cst2 + ), + ylabels(y, label) AS ( + SELECT y, MIN(labely) FROM d GROUP BY y + ), + yaxis(maxy, miny, stepy , minstepy) AS ( + WITH + xt0(minx, maxx) AS ( + SELECT coalesce(miny, min(min(y2), + min(y))), coalesce(maxy, max(max(y2), + max(y))) + qualitativey + FROM d, cst2 + ), + xt1(mx, mn) AS (SELECT maxx, minx FROM xt0), + xt2(mx, mn, step) AS (SELECT mx, mn, (mx-mn) FROM xt1), + + xt3(mx, mn, ms) AS ( + SELECT mx, mn, first_value(rs) OVER (order by x desc) AS ms + FROM (SELECT mx, mn, step, f,(mx-mn) as rng, + 1.0*step/f as rs, 1.0*(mx-mn)/(step/f) AS x + FROM xt2, (SELECT 1 AS f UNION ALL SELECT 2 + UNION ALL SELECT 4 + UNION ALL SELECT 5)) AS src + WHERE x < 10 limit 1), + xt4(minstepy) AS ( + SELECT MIN(abs(y2-y)) FROM d WHERE y2 != y + ) + SELECT (mx/ms)*ms, (mn/ms)*ms, coalesce(stepy, ms), + coalesce(minstepy, ms, stepy) FROM xt3, cst2,xt4 + ), + distinct_mark_n_m(mark, ze, zem, title) AS ( + SELECT DISTINCT mark, n AS ze, m AS zem, title FROM ds0 + ), + facet0(m, mi, title, radial) AS ( + SELECT md, row_number() OVER () - 1, title, 'radial' + IN (SELECT mark FROM distinct_mark_n_m WHERE zem = md) + FROM (SELECT DISTINCT zem AS md, title AS title + FROM distinct_mark_n_m ORDER BY 2, 1) + ), + facet(m, mi, xorigin, yorigin, title, radial) AS ( + SELECT m, mi, + rsx * 1.2 * IFNULL(CASE WHEN ( + 0 + ) > 0 THEN mi / ( + 0 + ) ELSE mi % ( + 2 + ) END, mi), + rsy * 1.2 * IFNULL(CASE WHEN ( + 2 + ) > 0 THEN mi / ( + 2 + ) ELSE mi / ( + 0 + ) END, 0), + title, radial FROM facet0, cst + ), + radygrid(m, mi, tty, wty, ttx, ttx2, xorigin, yorigin) AS ( + SELECT m, mi, rsy / 2 / ((maxy-miny)/stepy) * (value-1) AS tty, + coalesce(NULL, miny + stepy * (value-1)) AS wty, + xorigin, xorigin+rsx, xorigin + rsx / 2, + yorigin + rsy / 2 + FROM generate_series(1), yaxis, cst, + facet LEFT JOIN ylabels ON ylabels.y = (miny + (value-1) * stepy) + WHERE radial AND stop = 1+1.0*(maxy-miny)/stepy + ), + ypos(m, mi, pcx, pcy, radial) AS ( + SELECT m, mi, xorigin, yorigin + CASE + WHEN 0 BETWEEN miny AND maxy THEN + rsy - (0 - miny) * rsy / (maxy-miny) + WHEN 0 >= maxy THEN 0 + ELSE rsy + END, radial FROM yaxis, cst, facet WHERE NOT radial + UNION ALL + SELECT m, mi, xorigin + rsx / 2, yorigin + (CASE + WHEN 0 BETWEEN miny AND maxy THEN + rsy - (0 - miny) * rsy / 2 / (maxy-miny) + WHEN 0 >= maxy THEN 0 + ELSE rsy + END ) / 2, radial FROM yaxis, cst, facet WHERE radial + ) + SELECT * FROM radygrid , ypos; +} {} +} ;# end ifcapable vtab + +# 2021-03-19 +# dbsqlfuzz 01b8355086998f0a452cb31208e80b9d29ca739a +# +# Correlated CTEs should not be materialized. +# +reset_db +do_execsql_test 10.1 { + SELECT 1 AS c WHERE ( + SELECT ( + WITH t1(a) AS (VALUES( c )) + SELECT ( SELECT t1a.a FROM t1 AS t1a, t1 AS t1x ) + FROM t1 AS xyz GROUP BY 1 + ) + ) +} {1} + +# 2021-05-21 +# Forum post https://sqlite.org/forum/forumpost/aa4a7a3980 +# +ifcapable altertable { +reset_db + do_execsql_test 11.1 { + CREATE TABLE t1(a); + CREATE VIEW v2(c) AS + WITH x AS ( + WITH y AS ( + WITH z AS(SELECT * FROM t1) + SELECT * FROM v2 + ) SELECT a + ) SELECT * from t1; + ALTER TABLE t1 RENAME COLUMN a TO b; + SELECT sql FROM sqlite_schema WHERE name='t1'; + } {{CREATE TABLE t1(b)}} + do_catchsql_test 11.2 { + INSERT INTO t1 VALUES(55); + SELECT * FROM v2; + } {0 55} + do_catchsql_test 11.3 { + DROP VIEW v2; + CREATE VIEW v2(c) AS + WITH x AS ( + WITH y AS ( + WITH z AS(SELECT * FROM t1) + SELECT * FROM v2 + ) SELECT a + ) SELECT * from t1, x; + SELECT * FROM v2; + } {1 {no such column: a}} + do_catchsql_test 11.4 { + DROP VIEW v2; + CREATE VIEW v2(c) AS + WITH x AS ( + WITH y AS ( + WITH z AS(SELECT * FROM t1) + SELECT * FROM v2 + ) SELECT * + ) SELECT * from t1, x; + SELECT * FROM v2; + } {1 {no tables specified}} + do_catchsql_test 11.5 { + WITH x AS ( + WITH y AS ( + WITH z AS(SELECT * FROM t1) + SELECT * FROM no_such_table + ) SELECT a + ) SELECT * from t1; + } {0 55} +} + +# 2021-05-23 dbsqlfuzz 6b7a144674e215f06ddfeb9042c873d9ee956ac0 */ +reset_db +ifcapable altertable { + do_execsql_test 12.1 { + CREATE TABLE t1(a); + INSERT INTO t1 VALUES(1),('hello'),(4.25),(NULL),(x'3c626c6f623e'); + CREATE VIEW v2(c) AS WITH x AS (WITH y AS (WITH z AS(SELECT * FROM t1) SELECT * FROM v2) SELECT a) SELECT * from t1; + CREATE VIEW v3(c) AS WITH x AS (WITH y AS (WITH z AS(SELECT * FROM v2) SELECT * FROM v3) SELECT a) SELECT * from t1; + ALTER TABLE t1 RENAME TO t1x; + SELECT quote(c) FROM v3; + } {1 'hello' 4.25 NULL X'3C626C6F623E'} +} + +# 2021-08-11 https://sqlite.org/forum/forumpost/d496c3d29bc93736 +reset_db +do_execsql_test 13.1 { + WITH + t1(x) AS (SELECT 111), + t2(y) AS (SELECT 222), + t3(z) AS (SELECT * FROM t2 WHERE false UNION ALL SELECT * FROM t2) + SELECT * FROM t1, t3; +} {111 222} finish_test diff --git a/test/with3.test b/test/with3.test index 62e88441ab..9b110debf3 100644 --- a/test/with3.test +++ b/test/with3.test @@ -32,6 +32,14 @@ do_catchsql_test 1.0 { SELECT * FROM i; } {1 {no such table: m}} +# 2019-11-09 dbfuzzcheck find +do_catchsql_test 1.1 { + CREATE VIEW v1(x,y) AS + WITH t1(a,b) AS (VALUES(1,2)) + SELECT * FROM nosuchtable JOIN t1; + SELECT * FROM v1; +} {1 {no such table: main.nosuchtable}} + # Additional test cases that came out of the work to # fix for Kostya's problem. # @@ -61,4 +69,180 @@ do_execsql_test 2.1 { SELECT * FROM t1; } {200} +#------------------------------------------------------------------------- +# Test that the planner notices LIMIT clauses on recursive WITH queries. +# + +ifcapable analyze { + do_execsql_test 3.1.1 { + CREATE TABLE y1(a, b); + CREATE INDEX y1a ON y1(a); + + WITH cnt(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM cnt LIMIT 1000) + INSERT INTO y1 SELECT i%10, i FROM cnt; + ANALYZE; + + } + + do_eqp_test 3.1.2 { + WITH cnt(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM cnt LIMIT 1) + SELECT * FROM cnt, y1 WHERE i=a + } [string map {"\n " \n} { + QUERY PLAN + |--CO-ROUTINE cnt + | |--SETUP + | | `--SCAN CONSTANT ROW + | `--RECURSIVE STEP + | `--SCAN cnt + |--SCAN cnt + `--SEARCH y1 USING INDEX y1a (a=?) + }] + + do_eqp_test 3.1.3 { + WITH cnt(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM cnt LIMIT 1000000) + SELECT * FROM cnt, y1 WHERE i=a + } [string map {"\n " \n} { + QUERY PLAN + |--CO-ROUTINE cnt + | |--SETUP + | | `--SCAN CONSTANT ROW + | `--RECURSIVE STEP + | `--SCAN cnt + |--SCAN y1 + |--BLOOM FILTER ON cnt (i=?) + `--SEARCH cnt USING AUTOMATIC COVERING INDEX (i=?) + }] +} + +do_execsql_test 3.2.1 { + CREATE TABLE w1(pk INTEGER PRIMARY KEY, x INTEGER); + CREATE TABLE w2(pk INTEGER PRIMARY KEY); +} + +do_eqp_test 3.2.2 { + WITH RECURSIVE c(w,id) AS (SELECT 0, (SELECT pk FROM w2 LIMIT 1) + UNION ALL SELECT c.w + 1, x FROM w1, c LIMIT 1) + SELECT * FROM c, w2, w1 + WHERE c.id=w2.pk AND c.id=w1.pk; +} { + QUERY PLAN + |--CO-ROUTINE c + | |--SETUP + | | |--SCAN CONSTANT ROW + | | `--SCALAR SUBQUERY xxxxxx + | | `--SCAN w2 + | `--RECURSIVE STEP + | |--SCAN w1 + | `--SCAN c + |--SCAN c + |--SEARCH w2 USING INTEGER PRIMARY KEY (rowid=?) + `--SEARCH w1 USING INTEGER PRIMARY KEY (rowid=?) +} + +do_execsql_test 4.0 { + WITH t5(t5col1) AS ( + SELECT ( + WITH t3(t3col1) AS ( + WITH t2 AS ( + WITH t1 AS (SELECT 1 AS c1 GROUP BY 1) + SELECT a.c1 FROM t1 AS a, t1 AS b + WHERE anoncol1 = 1 + ) + SELECT (SELECT 1 FROM t2) FROM t2 + ) + SELECT t3col1 FROM t3 WHERE t3col1 + ) FROM (SELECT 1 AS anoncol1) + ) + SELECT t5col1, t5col1 FROM t5 +} {1 1} +do_execsql_test 4.1 { + SELECT EXISTS ( + WITH RECURSIVE Table0 AS ( + WITH RECURSIVE Table0(Col0) AS (SELECT ALL 1 ) + SELECT ALL ( + WITH RECURSIVE Table0 AS ( + WITH RECURSIVE Table0 AS ( + WITH RECURSIVE Table0 AS (SELECT DISTINCT 1 GROUP BY 1 ) + SELECT DISTINCT * FROM Table0 NATURAL INNER JOIN Table0 + WHERE Col0 = 1 + ) + SELECT ALL (SELECT DISTINCT * FROM Table0) FROM Table0 WHERE Col0 = 1 + ) + SELECT ALL * FROM Table0 NATURAL INNER JOIN Table0 + ) FROM Table0 ) + SELECT DISTINCT * FROM Table0 NATURAL INNER JOIN Table0 + ); +} {1} + +# 2020-01-18 chrome ticket 1043236 +# Correct handling of the sequence: +# OP_OpenEphem +# OP_OpenDup +# Op_OpenEphem +# OP_OpenDup +# +do_execsql_test 4.2 { + SELECT ( + WITH t1(a) AS (VALUES(1)) + SELECT ( + WITH t2(b) AS ( + WITH t3(c) AS ( + WITH t4(d) AS (VALUES('elvis')) + SELECT t4a.d FROM t4 AS t4a JOIN t4 AS t4b LEFT JOIN t4 AS t4c + ) + SELECT c FROM t3 WHERE a = 1 + ) + SELECT t2a.b FROM t2 AS t2a JOIN t2 AS t2x + ) + FROM t1 GROUP BY 1 + ) + GROUP BY 1; +} {elvis} + +# 2021-02-13 +# Avoid manifesting the same CTE multiple times. +# +do_eqp_test 5.1 { + WITH RECURSIVE c(x) AS (VALUES(0) UNION ALL SELECT x+1 FROM c WHERE x<1) + SELECT x1.x||x2.x||x3.x||x4.x FROM c AS x1, c AS x2, c AS x3, c AS x4 + ORDER BY 1; +} { + QUERY PLAN + |--MATERIALIZE c + | |--SETUP + | | `--SCAN CONSTANT ROW + | `--RECURSIVE STEP + | `--SCAN c + |--SCAN x1 + |--SCAN x2 + |--SCAN x3 + |--SCAN x4 + `--USE TEMP B-TREE FOR ORDER BY +} +do_execsql_test 5.2 { + WITH RECURSIVE c(x) AS (VALUES(0) UNION ALL SELECT x+1 FROM c WHERE x<1) + SELECT x1.x||x2.x||x3.x||x4.x FROM c AS x1, c AS x2, c AS x3, c AS x4 + ORDER BY 1; +} {0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111} + +#------------------------------------------------------------------------- +# At one point this would incorrectly report "circular reference: cte1" +# +do_catchsql_test 6.0 { + with + cte1(x, y) AS ( select 1, 2, 3 ), + cte2(z) as ( select 1 from cte1 ) + select * from cte2, cte1; +} {1 {table cte1 has 3 values for 2 columns}} + +do_catchsql_test 6.1 { + with + cte1(x, y) AS ( select 1, 2, 3 ), + cte2(z) as ( select 1 from cte1 UNION ALL SELECT z+1 FROM cte2 WHERE z<5) + select * from cte2, cte1; +} {1 {table cte1 has 3 values for 2 columns}} + + + + finish_test diff --git a/test/with4.test b/test/with4.test new file mode 100644 index 0000000000..b0eeba6d14 --- /dev/null +++ b/test/with4.test @@ -0,0 +1,52 @@ +# 2018-02-15 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is testing the WITH clause in TRIGGERs and VIEWs. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set ::testprefix with4 + +ifcapable {!cte} { + finish_test + return +} + +do_execsql_test 100 { + ATTACH ':memory:' AS aux; + CREATE TABLE main.t1(a,b); + CREATE TABLE aux.t2(x,y); + INSERT INTO t1 VALUES(1,2); + INSERT INTO t2 VALUES(3,4); +} {} +do_catchsql_test 110 { + CREATE VIEW v1 AS SELECT * FROM t1, aux.t2; +} {1 {view v1 cannot reference objects in database aux}} +do_catchsql_test 120 { + CREATE VIEW v2 AS WITH v(m,n) AS (SELECT x,y FROM aux.t2) SELECT * FROM t1, v; +} {1 {view v2 cannot reference objects in database aux}} +do_catchsql_test 130 { + CREATE VIEW v2 AS WITH v(m,n) AS (SELECT 5,?2) SELECT * FROM t1, v; +} {1 {parameters are not allowed in views}} + +do_catchsql_test 200 { + CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN + WITH v(m,n) AS (SELECT x,y FROM aux.t2) SELECT * FROM t1, v; + END; +} {1 {trigger r1 cannot reference objects in database aux}} +do_catchsql_test 210 { + CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN + WITH v(m,n) AS (SELECT 5,?2) SELECT * FROM t1, v; + END; +} {1 {trigger cannot use variables}} + +finish_test diff --git a/test/with5.test b/test/with5.test new file mode 100644 index 0000000000..430c5f2de3 --- /dev/null +++ b/test/with5.test @@ -0,0 +1,194 @@ +# 2020-10-19 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is recursive common table expressions with +# multiple recursive terms in the compound select. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set ::testprefix with5 + +ifcapable {!cte} { + finish_test + return +} + +do_execsql_test 100 { + CREATE TABLE link(aa INT, bb INT); + CREATE INDEX link_f ON link(aa,bb); + CREATE INDEX link_t ON link(bb,aa); + INSERT INTO link(aa,bb) VALUES + (1,3), + (5,3), + (7,1), + (7,9), + (9,9), + (5,11), + (11,7), + (2,4), + (4,6), + (8,6); +} {} +do_execsql_test 110 { + WITH RECURSIVE closure(x) AS ( + VALUES(1) + UNION + SELECT aa FROM closure, link WHERE link.bb=closure.x + UNION + SELECT bb FROM closure, link WHERE link.aa=closure.x + ) + SELECT x FROM closure ORDER BY x; +} {1 3 5 7 9 11} +do_execsql_test 111 { + WITH RECURSIVE closure(x) AS ( + VALUES(1) + UNION + SELECT aa FROM link, closure WHERE link.bb=closure.x + UNION + SELECT bb FROM closure, link WHERE link.aa=closure.x + ) + SELECT x FROM closure ORDER BY x; +} {1 3 5 7 9 11} +do_execsql_test 112 { + WITH RECURSIVE closure(x) AS ( + VALUES(1) + UNION + SELECT bb FROM closure, link WHERE link.aa=closure.x + UNION + SELECT aa FROM link, closure WHERE link.bb=closure.x + ) + SELECT x FROM closure ORDER BY x; +} {1 3 5 7 9 11} +do_execsql_test 113 { + WITH RECURSIVE closure(x) AS ( + VALUES(1),(200),(300),(400) + INTERSECT + VALUES(1) + UNION + SELECT bb FROM closure, link WHERE link.aa=closure.x + UNION + SELECT aa FROM link, closure WHERE link.bb=closure.x + ) + SELECT x FROM closure ORDER BY x; +} {1 3 5 7 9 11} +do_execsql_test 114 { + WITH RECURSIVE closure(x) AS ( + VALUES(1),(200),(300),(400) + UNION ALL + VALUES(2) + UNION + SELECT bb FROM closure, link WHERE link.aa=closure.x + UNION + SELECT aa FROM link, closure WHERE link.bb=closure.x + ) + SELECT x FROM closure ORDER BY x; +} {1 2 3 4 5 6 7 8 9 11 200 300 400} + +do_catchsql_test 120 { + WITH RECURSIVE closure(x) AS ( + VALUES(1),(200),(300),(400) + UNION ALL + VALUES(2) + UNION ALL + SELECT bb FROM closure, link WHERE link.aa=closure.x + UNION + SELECT aa FROM link, closure WHERE link.bb=closure.x + ) + SELECT x FROM closure ORDER BY x; +} {1 {circular reference: closure}} +do_catchsql_test 121 { + WITH RECURSIVE closure(x) AS ( + VALUES(1),(200),(300),(400) + UNION ALL + VALUES(2) + UNION + SELECT bb FROM closure, link WHERE link.aa=closure.x + UNION ALL + SELECT aa FROM link, closure WHERE link.bb=closure.x + ) + SELECT x FROM closure ORDER BY x; +} {1 {circular reference: closure}} + +do_execsql_test 130 { + WITH RECURSIVE closure(x) AS ( + SELECT 1 AS x + UNION + SELECT aa FROM link JOIN closure ON bb=x + UNION + SELECT bb FROM link JOIN closure on aa=x + ORDER BY x LIMIT 4 + ) + SELECT * FROM closure; +} {1 3 5 7} +do_execsql_test 131 { + WITH RECURSIVE closure(x) AS ( + SELECT 1 AS x + UNION ALL + SELECT 2 + UNION + SELECT aa FROM link JOIN closure ON bb=x + UNION + SELECT bb FROM link JOIN closure on aa=x + ORDER BY x LIMIT 4 + ) + SELECT * FROM closure; +} {1 2 3 4} + +do_execsql_test 200 { + CREATE TABLE linkA(aa1,aa2); + INSERT INTO linkA(aa1,aa2) VALUES(1,3),(5,7),(9,11); + CREATE TABLE linkB(bb1,bb2); + INSERT INTO linkB(bb1,bb2) VALUES(7,9),(11,13),(3,5); + CREATE TABLE linkC(cc1,cc2); + INSERT INTO linkC(cc1,cc2) VALUES(1,2),(2,4),(6,8); + CREATE TABLE linkD(dd1,dd2); + INSERT INTO linkD(dd1,dd2) VALUES(4,6),(100,110); +} {} +do_execsql_test 210 { + WITH RECURSIVE closure(x) AS ( + VALUES(1) + UNION ALL + SELECT aa2 FROM linkA JOIN closure ON x=aa1 + UNION ALL + SELECT bb2 FROM linkB JOIN closure ON x=bb1 + UNION ALL + SELECT cc2 FROM linkC JOIN closure ON x=cc1 + UNION ALL + SELECT dd2 FROM linkD JOIN closure ON x=dd1 + ) + SELECT x FROM closure ORDER BY +x; +} {1 2 3 4 5 6 7 8 9 11 13} +do_execsql_test 220 { + CREATE TABLE linkA_ipk(aa1 INTEGER PRIMARY KEY,aa2); + INSERT INTO linkA_ipk(aa1,aa2) SELECT aa1, aa2 FROM linkA; + CREATE TABLE linkB_ipk(bb1 INTEGER PRIMARY KEY,bb2); + INSERT INTO linkB_ipk(bb1,bb2) SELECT bb1, bb2 FROM linkB; + CREATE TABLE linkC_ipk(cc1 INTEGER PRIMARY KEY,cc2); + INSERT INTO linkC_ipk(cc1,cc2) SELECT cc1, cc2 FROM linkC; + CREATE TABLE linkD_ipk(dd1 INTEGER PRIMARY KEY,dd2); + INSERT INTO linkD_ipk(dd1,dd2) SELECT dd1, dd2 FROM linkD; + WITH RECURSIVE closure(x) AS ( + VALUES(1) + UNION ALL + SELECT aa2 FROM linkA_ipk JOIN closure ON x=aa1 + UNION ALL + SELECT bb2 FROM linkB_ipk JOIN closure ON x=bb1 + UNION ALL + SELECT cc2 FROM linkC_ipk JOIN closure ON x=cc1 + UNION ALL + SELECT dd2 FROM linkD_ipk JOIN closure ON x=dd1 + ) + SELECT x FROM closure ORDER BY +x; +} {1 2 3 4 5 6 7 8 9 11 13} + + +finish_test diff --git a/test/with6.test b/test/with6.test new file mode 100644 index 0000000000..b95ec0b763 --- /dev/null +++ b/test/with6.test @@ -0,0 +1,427 @@ +# 2021-02-22 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is the MATERIALIZED hint to common table expressions +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set ::testprefix with6 + +ifcapable {!cte} { + finish_test + return +} + +do_execsql_test 100 { + WITH c(x) AS (VALUES(0),(1)) + SELECT c1.x||c2.x||c3.x FROM c c1, c c2, c c3; +} {000 001 010 011 100 101 110 111} +do_eqp_test 101 { + WITH c(x) AS (VALUES(0),(1)) + SELECT c1.x||c2.x||c3.x FROM c c1, c c2, c c3; +} { + QUERY PLAN + |--MATERIALIZE c + | `--SCAN 2 CONSTANT ROWS + |--SCAN c1 + |--SCAN c2 + `--SCAN c3 +} + +do_execsql_test 110 { + WITH c(x) AS MATERIALIZED (VALUES(0),(1)) + SELECT c1.x||c2.x||c3.x FROM c c1, c c2, c c3; +} {000 001 010 011 100 101 110 111} +do_eqp_test 111 { + WITH c(x) AS MATERIALIZED (VALUES(0),(1)) + SELECT c1.x||c2.x||c3.x FROM c c1, c c2, c c3; +} { + QUERY PLAN + |--MATERIALIZE c + | `--SCAN 2 CONSTANT ROWS + |--SCAN c1 + |--SCAN c2 + `--SCAN c3 +} + +# Even though the CTE is not materialized, the self-join optimization +# kicks in and does the materialization for us. +# +do_execsql_test 120 { + WITH c(x) AS NOT MATERIALIZED (VALUES(0),(1)) + SELECT c1.x||c2.x||c3.x FROM c c1, c c2, c c3; +} {000 001 010 011 100 101 110 111} +do_eqp_test 121 { + WITH c(x) AS NOT MATERIALIZED (VALUES(0),(1)) + SELECT c1.x||c2.x||c3.x FROM c c1, c c2, c c3; +} { + QUERY PLAN + |--MATERIALIZE c + | `--SCAN 2 CONSTANT ROWS + |--SCAN c1 + |--SCAN c2 + `--SCAN c3 +} + +do_execsql_test 130 { + WITH c(x) AS NOT MATERIALIZED (VALUES(0),(1)) + SELECT c1.x||c2.x||c3.x + FROM (SELECT x FROM c LIMIT 5) AS c1, + (SELECT x FROM c LIMIT 5) AS c2, + (SELECT x FROM c LIMIT 5) AS c3; +} {000 001 010 011 100 101 110 111} +do_eqp_test 131 { + WITH c(x) AS NOT MATERIALIZED (VALUES(0),(1)) + SELECT c1.x||c2.x||c3.x + FROM (SELECT x FROM c LIMIT 5) AS c1, + (SELECT x FROM c LIMIT 5) AS c2, + (SELECT x FROM c LIMIT 5) AS c3; +} { + QUERY PLAN + |--CO-ROUTINE c1 + | |--CO-ROUTINE c + | | `--SCAN 2 CONSTANT ROWS + | `--SCAN c + |--MATERIALIZE c2 + | |--CO-ROUTINE c + | | `--SCAN 2 CONSTANT ROWS + | `--SCAN c + |--MATERIALIZE c3 + | |--CO-ROUTINE c + | | `--SCAN 2 CONSTANT ROWS + | `--SCAN c + |--SCAN c1 + |--SCAN c2 + `--SCAN c3 +} + +# The (SELECT x FROM c LIMIT N) subqueries get materialized once each. +# Show multiple materializations are shown. But there is only one +# materialization for c, shown by the "SCAN 2 CONSTANT ROWS" line. +# +do_execsql_test 140 { + WITH c(x) AS MATERIALIZED (VALUES(0),(1)) + SELECT c1.x||c2.x||c3.x + FROM (SELECT x FROM c LIMIT 5) AS c1, + (SELECT x FROM c LIMIT 6) AS c2, + (SELECT x FROM c LIMIT 7) AS c3; +} {000 001 010 011 100 101 110 111} +do_eqp_test 141 { + WITH c(x) AS MATERIALIZED (VALUES(0),(1)) + SELECT c1.x||c2.x||c3.x + FROM (SELECT x FROM c LIMIT 5) AS c1, + (SELECT x FROM c LIMIT 6) AS c2, + (SELECT x FROM c LIMIT 7) AS c3; +} { + QUERY PLAN + |--CO-ROUTINE c1 + | |--MATERIALIZE c + | | `--SCAN 2 CONSTANT ROWS + | `--SCAN c + |--MATERIALIZE c2 + | `--SCAN c + |--MATERIALIZE c3 + | `--SCAN c + |--SCAN c1 + |--SCAN c2 + `--SCAN c3 +} + +do_execsql_test 150 { + WITH c(x) AS (VALUES(0),(1)) + SELECT c1.x||c2.x||c3.x + FROM (SELECT x FROM c LIMIT 5) AS c1, + (SELECT x FROM c LIMIT 6) AS c2, + (SELECT x FROM c LIMIT 7) AS c3; +} {000 001 010 011 100 101 110 111} +do_eqp_test 151 { + WITH c(x) AS (VALUES(0),(1)) + SELECT c1.x||c2.x||c3.x + FROM (SELECT x FROM c LIMIT 5) AS c1, + (SELECT x FROM c LIMIT 6) AS c2, + (SELECT x FROM c LIMIT 7) AS c3; +} { + QUERY PLAN + |--CO-ROUTINE c1 + | |--MATERIALIZE c + | | `--SCAN 2 CONSTANT ROWS + | `--SCAN c + |--MATERIALIZE c2 + | `--SCAN c + |--MATERIALIZE c3 + | `--SCAN c + |--SCAN c1 + |--SCAN c2 + `--SCAN c3 +} + +do_execsql_test 160 { + WITH c(x) AS (VALUES(0),(1)) + SELECT c2.x + 100*(SELECT sum(x+1) FROM c WHERE c.x<=c2.x) + FROM c AS c2 WHERE c2.x<10; +} {100 301} +do_eqp_test 161 { + WITH c(x) AS (VALUES(0),(1)) + SELECT c2.x + 100*(SELECT sum(x+1) FROM c WHERE c.x<=c2.x) + FROM c AS c2 WHERE c2.x<10; +} { + QUERY PLAN + |--MATERIALIZE c + | `--SCAN 2 CONSTANT ROWS + |--SCAN c2 + `--CORRELATED SCALAR SUBQUERY xxxxxx + `--SCAN c +} + +do_execsql_test 170 { + WITH c(x) AS NOT MATERIALIZED (VALUES(0),(1)) + SELECT c2.x + 100*(SELECT sum(x+1) FROM c WHERE c.x<=c2.x) + FROM c AS c2 WHERE c2.x<10; +} {100 301} +do_eqp_test 171 { + WITH c(x) AS NOT MATERIALIZED (VALUES(0),(1)) + SELECT c2.x + 100*(SELECT sum(x+1) FROM c WHERE c.x<=c2.x) + FROM c AS c2 WHERE c2.x<10; +} { + QUERY PLAN + |--CO-ROUTINE c + | `--SCAN 2 CONSTANT ROWS + |--SCAN c2 + `--CORRELATED SCALAR SUBQUERY xxxxxx + |--CO-ROUTINE c + | `--SCAN 2 CONSTANT ROWS + `--SCAN c +} + + +do_execsql_test 200 { + CREATE TABLE t1(x); + INSERT INTO t1(x) VALUES(4); + CREATE VIEW t2(y) AS + WITH c(z) AS (VALUES(4),(5),(6)) + SELECT c1.z+c2.z*100+t1.x*10000 + FROM t1, + (SELECT z FROM c LIMIT 5) AS c1, + (SELECT z FROM c LIMIT 5) AS c2; + SELECT y FROM t2 ORDER BY y; +} {40404 40405 40406 40504 40505 40506 40604 40605 40606} +do_execsql_test 210 { + DROP VIEW t2; + CREATE VIEW t2(y) AS + WITH c(z) AS NOT MATERIALIZED (VALUES(4),(5),(6)) + SELECT c1.z+c2.z*100+t1.x*10000 + FROM t1, + (SELECT z FROM c LIMIT 5) AS c1, + (SELECT z FROM c LIMIT 5) AS c2; + SELECT y FROM t2 ORDER BY y; +} {40404 40405 40406 40504 40505 40506 40604 40605 40606} +do_eqp_test 211 { + SELECT y FROM t2 ORDER BY y; +} { + QUERY PLAN + |--CO-ROUTINE c1 + | |--CO-ROUTINE c + | | `--SCAN 3 CONSTANT ROWS + | `--SCAN c + |--MATERIALIZE c2 + | |--CO-ROUTINE c + | | `--SCAN 3 CONSTANT ROWS + | `--SCAN c + |--SCAN c1 + |--SCAN c2 + |--SCAN t1 + `--USE TEMP B-TREE FOR ORDER BY +} +do_execsql_test 220 { + DROP VIEW t2; + CREATE VIEW t2(y) AS + WITH c(z) AS MATERIALIZED (VALUES(4),(5),(6)) + SELECT c1.z+c2.z*100+t1.x*10000 + FROM t1, + (SELECT z FROM c LIMIT 5) AS c1, + (SELECT z FROM c LIMIT 5) AS c2; + SELECT y FROM t2 ORDER BY y; +} {40404 40405 40406 40504 40505 40506 40604 40605 40606} + +# 2022-04-22: Do not allow flattening of a MATERIALIZED CTE into +# an outer query. +# +reset_db +db null - +do_execsql_test 300 { + CREATE TABLE t2(a INT,b INT,d INT); INSERT INTO t2 VALUES(4,5,6),(7,8,9); + CREATE TABLE t3(a INT,b INT,e INT); INSERT INTO t3 VALUES(3,3,3),(8,8,8); +} {} +do_execsql_test 310 { + WITH t23 AS MATERIALIZED (SELECT * FROM t2 FULL JOIN t3 USING(b)) + SELECT * FROM t23; +} { + 4 5 6 - - + 7 8 9 8 8 + - 3 - 3 3 +} +do_eqp_test 311 { + WITH t23 AS MATERIALIZED (SELECT * FROM t2 FULL JOIN t3 USING(b)) + SELECT * FROM t23; +} { + QUERY PLAN + |--MATERIALIZE t23 + | |--SCAN t2 + | |--SCAN t3 LEFT-JOIN + | `--RIGHT-JOIN t3 + | `--SCAN t3 + `--SCAN t23 +} +do_execsql_test 320 { + WITH t23 AS NOT MATERIALIZED (SELECT * FROM t2 FULL JOIN t3 USING(b)) + SELECT * FROM t23; +} { + 4 5 6 - - + 7 8 9 8 8 + - 3 - 3 3 +} +do_eqp_test 321 { + WITH t23 AS NOT MATERIALIZED (SELECT * FROM t2 FULL JOIN t3 USING(b)) + SELECT * FROM t23; +} { + QUERY PLAN + |--SCAN t2 + |--SCAN t3 LEFT-JOIN + `--RIGHT-JOIN t3 + `--SCAN t3 +} +do_execsql_test 330 { + WITH t23 AS (SELECT * FROM t2 FULL JOIN t3 USING(b)) + SELECT * FROM t23; +} { + 4 5 6 - - + 7 8 9 8 8 + - 3 - 3 3 +} +do_eqp_test 331 { + WITH t23 AS (SELECT * FROM t2 FULL JOIN t3 USING(b)) + SELECT * FROM t23; +} { + QUERY PLAN + |--SCAN t2 + |--SCAN t3 LEFT-JOIN + `--RIGHT-JOIN t3 + `--SCAN t3 +} + +# 2023-02-01 +# https://sqlite.org/forum/forumpost/1d571c02963355ed +# +# Just because a CTE is used more than once, does not mean it should be +# marked with M10d_Yes and hence prohibited from participating in the +# query flattening optimization. +# +# Updated 2025-01-02. +# https://sqlite.org/forum/forumpost/8f38fc9878a92aa9 +# +# The same optimization that made Grunthos's query fast made +# Jean-Noël Mayor's query slow. Bummer. +# +reset_db +db eval { + CREATE TABLE raw(country,date,total,delta, UNIQUE(country,date)); +} +do_eqp_test 400 { + with recursive + init(country, date, fin) AS (SELECT country, min(date), max(date) FROM raw WHERE total > 0 GROUP BY country), + src(country, date) AS (SELECT raw.country, raw.date + FROM raw JOIN init i on raw.country = i.country AND raw.date > i.date + ORDER BY raw.country, raw.date), + vals(country, date, x, y) AS (SELECT src.country, src.date, julianday(raw.date) - julianday(src.date), log(delta+1) + FROM src JOIN raw on raw.country = src.country AND raw.date > date(src.date,'-7 days') AND raw.date <= src.date AND delta >= 0), + sums(country, date, x2, x, n, xy, y) AS (SELECT country, date, sum(x*x*1.0), sum(x*1.0), sum(1.0), sum(x*y*1.0), sum(y*1.0) FROM vals GROUP BY 1, 2), + mult(country, date, m) AS (SELECT country, date, 1.0/(x2 * n - x * x) FROM sums), + inv(country, date, a,b,c,d) AS (SELECT mult.country, mult.date, n * m, -x * m, -x * m, x2 * m + FROM mult JOIN sums on sums.country=mult.country AND mult.date=sums.date), + fit(country, date, a, b) AS (SELECT inv.country, inv.date, a * xy + b * y, c * xy + d * y + FROM inv + JOIN mult on mult.country = inv.country AND mult.date = inv.date + JOIN sums on sums.country = mult.country AND sums.date = mult.date + ) + SELECT *, nFin/nPrev - 1 AS growth, log(2)/log(nFin/nPrev) AS doubling + FROM (SELECT f.*, exp(b) - 1 AS nFin, exp(a* (-1) + b) - 1 AS nPrev + FROM fit f JOIN init i on i.country = f.country AND f.date <= date(i.fin,'-3 days')) + WHERE nPrev > 0 AND nFin > 0; +} { + QUERY PLAN + |--MATERIALIZE sums + | |--MATERIALIZE src + | | |--MATERIALIZE init + | | | `--SCAN raw USING INDEX sqlite_autoindex_raw_1 + | | |--SCAN i + | | |--SEARCH raw USING COVERING INDEX sqlite_autoindex_raw_1 (country=? AND date>?) + | | `--USE TEMP B-TREE FOR ORDER BY + | |--SCAN src + | `--SEARCH raw USING INDEX sqlite_autoindex_raw_1 (country=? AND date>? AND date<?) + |--SCAN sums + |--BLOOM FILTER ON sums (country=? AND date=?) + |--SEARCH sums USING AUTOMATIC COVERING INDEX (country=? AND date=?) + |--BLOOM FILTER ON sums (country=? AND date=?) + |--SEARCH sums USING AUTOMATIC COVERING INDEX (country=? AND date=?) + |--BLOOM FILTER ON sums (country=? AND date=?) + |--SEARCH sums USING AUTOMATIC COVERING INDEX (country=? AND date=?) + |--BLOOM FILTER ON i (country=?) + `--SEARCH i USING AUTOMATIC COVERING INDEX (country=?) +} +optimization_control db order-by-subquery off +db cache flush +do_eqp_test 410 { + with recursive + init(country, date, fin) AS (SELECT country, min(date), max(date) FROM raw WHERE total > 0 GROUP BY country), + src(country, date) AS (SELECT raw.country, raw.date + FROM raw JOIN init i on raw.country = i.country AND raw.date > i.date + ORDER BY raw.country, raw.date), + vals(country, date, x, y) AS (SELECT src.country, src.date, julianday(raw.date) - julianday(src.date), log(delta+1) + FROM src JOIN raw on raw.country = src.country AND raw.date > date(src.date,'-7 days') AND raw.date <= src.date AND delta >= 0), + sums(country, date, x2, x, n, xy, y) AS (SELECT country, date, sum(x*x*1.0), sum(x*1.0), sum(1.0), sum(x*y*1.0), sum(y*1.0) FROM vals GROUP BY 1, 2), + mult(country, date, m) AS (SELECT country, date, 1.0/(x2 * n - x * x) FROM sums), + inv(country, date, a,b,c,d) AS (SELECT mult.country, mult.date, n * m, -x * m, -x * m, x2 * m + FROM mult JOIN sums on sums.country=mult.country AND mult.date=sums.date), + fit(country, date, a, b) AS (SELECT inv.country, inv.date, a * xy + b * y, c * xy + d * y + FROM inv + JOIN mult on mult.country = inv.country AND mult.date = inv.date + JOIN sums on sums.country = mult.country AND sums.date = mult.date + ) + SELECT *, nFin/nPrev - 1 AS growth, log(2)/log(nFin/nPrev) AS doubling + FROM (SELECT f.*, exp(b) - 1 AS nFin, exp(a* (-1) + b) - 1 AS nPrev + FROM fit f JOIN init i on i.country = f.country AND f.date <= date(i.fin,'-3 days')) + WHERE nPrev > 0 AND nFin > 0; +} { + QUERY PLAN + |--MATERIALIZE sums + | |--MATERIALIZE src + | | |--MATERIALIZE init + | | | `--SCAN raw USING INDEX sqlite_autoindex_raw_1 + | | |--SCAN i + | | |--SEARCH raw USING COVERING INDEX sqlite_autoindex_raw_1 (country=? AND date>?) + | | `--USE TEMP B-TREE FOR ORDER BY + | |--SCAN src + | |--SEARCH raw USING INDEX sqlite_autoindex_raw_1 (country=? AND date>? AND date<?) + | `--USE TEMP B-TREE FOR GROUP BY + |--SCAN sums + |--BLOOM FILTER ON sums (country=? AND date=?) + |--SEARCH sums USING AUTOMATIC COVERING INDEX (country=? AND date=?) + |--BLOOM FILTER ON sums (country=? AND date=?) + |--SEARCH sums USING AUTOMATIC COVERING INDEX (country=? AND date=?) + |--BLOOM FILTER ON sums (country=? AND date=?) + |--SEARCH sums USING AUTOMATIC COVERING INDEX (country=? AND date=?) + |--BLOOM FILTER ON i (country=?) + `--SEARCH i USING AUTOMATIC COVERING INDEX (country=?) +} + + +finish_test diff --git a/test/without_rowid1.test b/test/without_rowid1.test index 0c77773abb..5134e5e809 100644 --- a/test/without_rowid1.test +++ b/test/without_rowid1.test @@ -17,6 +17,10 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl set testprefix without_rowid1 +proc do_execsql_test_if_vtab {tn sql {res {}}} { + ifcapable vtab { uplevel [list do_execsql_test $tn $sql $res] } +} + # Create and query a WITHOUT ROWID table. # do_execsql_test without_rowid1-1.0 { @@ -31,6 +35,13 @@ do_execsql_test without_rowid1-1.0 { integrity_check without_rowid1-1.0ic +do_execsql_test_if_vtab without_rowid1-1.0ixi { + SELECT name, key FROM pragma_index_xinfo('t1'); +} {c 1 a 1 b 0 d 0} +do_execsql_test_if_vtab without_rowid1-1.0tl { + SELECT wr FROM pragma_table_list('t1'); +} {1} + do_execsql_test without_rowid1-1.1 { SELECT *, '|' FROM t1 ORDER BY +c, a; } {arctic sleep ammonia helena | journal sherman ammonia helena | dynamic juliet flipper command | journal sherman gamma patriot |} @@ -98,11 +109,6 @@ do_execsql_test without_rowid1-1.50 { ANALYZE; SELECT * FROM sqlite_stat1 ORDER BY idx; } {t1 t1 {4 2 1} t1 t1bd {4 2 2}} -ifcapable stat3 { - do_execsql_test without_rowid1-1.51 { - SELECT DISTINCT tbl, idx FROM sqlite_stat3 ORDER BY idx; - } {t1 t1 t1 t1bd} -} ifcapable stat4 { do_execsql_test without_rowid1-1.52 { SELECT DISTINCT tbl, idx FROM sqlite_stat4 ORDER BY idx; @@ -120,6 +126,9 @@ do_execsql_test 2.1.2 { UPDATE t4 SET a = 'ABC'; SELECT * FROM t4; } {ABC def} +do_execsql_test_if_vtab 2.1.3 { + SELECT name, coll, key FROM pragma_index_xinfo('t4'); +} {a nocase 1 b BINARY 0} do_execsql_test 2.2.1 { DROP TABLE t4; @@ -133,12 +142,22 @@ do_execsql_test 2.2.2 { SELECT * FROM t4; } {xyz ABC} +do_execsql_test_if_vtab 2.2.3 { + SELECT name, coll, key FROM pragma_index_xinfo('t4'); +} {a nocase 1 b BINARY 0} + + do_execsql_test 2.3.1 { CREATE TABLE t5 (a, b, PRIMARY KEY(b, a)) WITHOUT ROWID; INSERT INTO t5(a, b) VALUES('abc', 'def'); UPDATE t5 SET a='abc', b='def'; } {} +do_execsql_test_if_vtab 2.3.2 { + SELECT name, coll, key FROM pragma_index_xinfo('t5'); +} {b BINARY 1 a BINARY 1} + + do_execsql_test 2.4.1 { CREATE TABLE t6 ( a COLLATE nocase, b, c UNIQUE, PRIMARY KEY(b, a) @@ -153,6 +172,11 @@ do_execsql_test 2.4.2 { SELECT * FROM t6 ORDER BY c; } {ABC def ghi ABC def ghi} +do_execsql_test_if_vtab 2.4.3 { + SELECT name, coll, key FROM pragma_index_xinfo('t6'); +} {b BINARY 1 a nocase 1 c BINARY 0} + + #------------------------------------------------------------------------- # Unless the destination table is completely empty, the xfer optimization # is disabled for WITHOUT ROWID tables. The following tests check for @@ -238,7 +262,7 @@ do_execsql_test 5.0 { do_eqp_test 5.1 { SELECT * FROM t45 WHERE b=? AND a>? -} {/*USING INDEX i45 (b=? AND a>?)*/} +} {USING INDEX i45 (b=? AND a>?)} do_execsql_test 5.2 { SELECT * FROM t45 WHERE b='two' AND a>4 @@ -257,11 +281,11 @@ do_execsql_test 5.4 { } set queries { - 1 2 "c = 5 AND a = 1" {/*i46 (c=? AND a=?)*/} - 2 6 "c = 4 AND a < 3" {/*i46 (c=? AND a<?)*/} - 3 4 "c = 2 AND a >= 3" {/*i46 (c=? AND a>?)*/} - 4 1 "c = 2 AND a = 1 AND b<10" {/*i46 (c=? AND a=? AND b<?)*/} - 5 1 "c = 0 AND a = 0 AND b>5" {/*i46 (c=? AND a=? AND b>?)*/} + 1 2 "c = 5 AND a = 1" {i46 (c=? AND a=?)} + 2 6 "c = 4 AND a < 3" {i46 (c=? AND a<?)} + 3 4 "c = 2 AND a >= 3" {i46 (c=? AND a>?)} + 4 1 "c = 2 AND a = 1 AND b<10" {i46 (c=? AND a=? AND b<?)} + 5 1 "c = 0 AND a = 0 AND b>5" {i46 (c=? AND a=? AND b>?)} } foreach {tn cnt where eqp} $queries { @@ -320,7 +344,7 @@ do_execsql_test 7.1 { } {} do_catchsql_test 7.2 { INSERT INTO t70a(rowid,a,b) VALUES(33,99,'xyzzy'); -} {1 {CHECK constraint failed: t70a}} +} {1 {CHECK constraint failed: rowid!=33}} do_catchsql_test 7.3 { CREATE TABLE t70b( a INT CHECK( rowid!=33 ), @@ -328,5 +352,168 @@ do_catchsql_test 7.3 { ) WITHOUT ROWID; } {1 {no such column: rowid}} - +# 2017-07-30: OSSFuzz discovered that an extra entry was being +# added in the sqlite_master table for an "INTEGER PRIMARY KEY UNIQUE" +# WITHOUT ROWID table. Make sure this has now been fixed. +# +db close +sqlite3 db :memory: +do_execsql_test 8.1 { + CREATE TABLE t1(x INTEGER PRIMARY KEY UNIQUE, b) WITHOUT ROWID; + CREATE INDEX t1x ON t1(x); + INSERT INTO t1(x,b) VALUES('funny','buffalo'); + SELECT type, name, '|' FROM sqlite_master; +} {table t1 | index t1x |} + +# 2018-04-05: OSSFuzz found that the following was accessing an +# unintialized memory cell. Which was not actually causing a +# malfunction, but does cause an assert() to fail. +# +do_execsql_test 9.0 { + CREATE TABLE t2(b, c, PRIMARY KEY(b,c)) WITHOUT ROWID; + CREATE UNIQUE INDEX t2b ON t2(b); + UPDATE t2 SET b=1 WHERE b=''; +} + +do_execsql_test 10.1 { + DELETE FROM t2 WHERE b=1 +} + +#------------------------------------------------------------------------- +# UNIQUE constraint violation in an UPDATE with a multi-column PK. +# +reset_db +do_execsql_test 10.0 { + CREATE TABLE t1(a, b, c UNIQUE, PRIMARY KEY(a, b)) WITHOUT ROWID; + INSERT INTO t1 VALUES('a', 'a', 1); + INSERT INTO t1 VALUES('a', 'b', 2); + INSERT INTO t1 VALUES('b', 'a', 3); + INSERT INTO t1 VALUES('b', 'b', 4); +} + +do_catchsql_test 10.1 { + UPDATE t1 SET c=1 WHERE (a, b) = ('a', 'a'); +} {0 {}} +do_catchsql_test 10.2 { + UPDATE t1 SET c=1 WHERE (a, b) = ('a', 'b'); +} {1 {UNIQUE constraint failed: t1.c}} +do_catchsql_test 10.3 { + UPDATE t1 SET c=1 WHERE (a, b) = ('b', 'a'); +} {1 {UNIQUE constraint failed: t1.c}} +do_catchsql_test 10.4 { + UPDATE t1 SET c=1 WHERE (a, b) = ('b', 'b'); +} {1 {UNIQUE constraint failed: t1.c}} +do_catchsql_test 10.5 { + UPDATE t1 SET c=1 WHERE (a, b) = ('c', 'c'); +} {0 {}} + +do_execsql_test 10.6 { + CREATE TRIGGER t1_tr BEFORE UPDATE ON t1 BEGIN + DELETE FROM t1 WHERE a = new.a; + END; + UPDATE t1 SET c = c+1 WHERE a = 'a'; + SELECT * FROM t1; +} {b a 3 b b 4} + +# 2019-04-29 ticket https://sqlite.org/src/info/3182d3879020ef3 +do_execsql_test 11.1 { + CREATE TABLE t11(a TEXT PRIMARY KEY, b INT) WITHOUT ROWID; + CREATE INDEX t11a ON t11(a COLLATE NOCASE); + INSERT INTO t11(a,b) VALUES ('A',1),('a',2); + PRAGMA integrity_check; + SELECT a FROM t11 ORDER BY a COLLATE binary; +} {ok A a} + +# 2019-05-13 ticket https://sqlite.org/src/info/bba7b69f9849b5b +do_execsql_test 12.1 { + DROP TABLE IF EXISTS t0; + CREATE TABLE t0 (c0 INTEGER PRIMARY KEY DESC, c1 UNIQUE DEFAULT NULL) WITHOUT ROWID; + INSERT INTO t0(c0) VALUES (1), (2), (3), (4), (5); + REINDEX; + PRAGMA integrity_check; +} {ok} + +# 2019-11-07 ticket https://sqlite.org/src/info/302027baf1374498 +# The xferCompatibleIndex() function confuses a PRIMARY KEY index +# with a UNIQUE index. +# +do_execsql_test 13.10 { + DROP TABLE IF EXISTS t0; + DROP TABLE IF EXISTS t1; + CREATE TABLE t0( + c0, + c1 UNIQUE, + PRIMARY KEY(c1, c1) + ) WITHOUT ROWID; + INSERT INTO t0(c0,c1) VALUES('abc','xyz'); + CREATE TABLE t1( + c0, + c1 UNIQUE, + PRIMARY KEY(c1, c1) + ) WITHOUT ROWID; + INSERT INTO t1 SELECT * FROM t0; + PRAGMA integrity_check; + SELECT * FROM t0, t1; +} {ok abc xyz abc xyz} + +# 2021-05-13 https://sqlite.org/forum/forumpost/6c8960f545 +reset_db +ifcapable altertable { + do_execsql_test 14.1 { + CREATE TABLE t1(a INT PRIMARY KEY) WITHOUT ROWID; + INSERT INTO t1(a) VALUES(10); + ALTER TABLE t1 ADD COLUMN b INT; + SELECT * FROM t1 WHERE a=20 OR (a=10 AND b=10); + } {} + do_execsql_test 14.2 { + CREATE TABLE dual AS SELECT 'X' AS dummy; + EXPLAIN QUERY PLAN SELECT * FROM dual, t1 WHERE a=10 AND b=10; + } {~/b=/} +} + +# 2022-01-01 https://sqlite.org/forum/forumpost/b03d86f951 PoC #1 +# Omit an assert() from 2013 that no longer serves any purpose and +# is no longer always true. +# +ifcapable altertable { + reset_db + do_execsql_test 15.1 { + PRAGMA writable_schema=ON; + CREATE TABLE sqlite_sequence (name PRIMARY KEY) WITHOUT ROWID; + PRAGMA writable_schema=OFF; + CREATE TABLE c1(x); + INSERT INTO sqlite_sequence(name) VALUES('c0'),('c1'),('c2'); + ALTER TABLE c1 RENAME TO a; + SELECT name FROM sqlite_sequence ORDER BY +name; + } {a c0 c2} +} + +# Ensure that the number of columns in an index on a WITHOUT ROWID +# table does not exceed SQLITE_LIMIT_COLUMN. +# +reset_db +sqlite3_limit db SQLITE_LIMIT_COLUMN 8 +do_execsql_test 16.1 { + CREATE TABLE t1( + c1,c2,c3,c4,c5,c6,c7,c8, + PRIMARY KEY(c1,c2,c1 COLLATE NOCASE) + ) WITHOUT ROWID; +} {} +do_execsql_test 16.2 { + CREATE TABLE t2( + c1,c2,c3,c4,c5,c6,c7,c8, + PRIMARY KEY(c1 COLLATE nocase,c1 COLLATE rtrim, + c2 COLLATE nocase,c2 COLLATE rtrim, + c3 COLLATE nocase,c3 COLLATE rtrim, + c4 COLLATE nocase,c4 COLLATE rtrim) + ) WITHOUT ROWID; +} {} +do_execsql_test 16.3 { + CREATE TABLE t3( + c1,c2,c3,c4,c5,c6,c7,c8, + PRIMARY KEY(c1,c2), + UNIQUE(c3,c4,c5,c6,c7,c8,c3 COLLATE nocase) + ) WITHOUT ROWID; +} {} + finish_test diff --git a/test/without_rowid3.test b/test/without_rowid3.test index 2af43a9191..f7687455ec 100644 --- a/test/without_rowid3.test +++ b/test/without_rowid3.test @@ -417,14 +417,14 @@ do_test without_rowid3-3.1.2 { } {} do_test without_rowid3-3.1.3 { catchsql { UPDATE ab SET a = 5 } -} {1 {CHECK constraint failed: ef}} +} {1 {CHECK constraint failed: e!=5}} do_test without_rowid3-3.1.4 { execsql { SELECT * FROM ab } } {1 b} do_test without_rowid3-3.1.4 { execsql BEGIN; catchsql { UPDATE ab SET a = 5 } -} {1 {CHECK constraint failed: ef}} +} {1 {CHECK constraint failed: e!=5}} do_test without_rowid3-3.1.5 { execsql COMMIT; execsql { SELECT * FROM ab; SELECT * FROM cd; SELECT * FROM ef } @@ -921,6 +921,7 @@ ifcapable altertable { execsql { CREATE TABLE t1(a PRIMARY KEY) WITHOUT rowid; CREATE TABLE t2(a, b); + INSERT INTO t2(a,b) VALUES(1,2); } catchsql { ALTER TABLE t2 ADD COLUMN c REFERENCES t1 } } {0 {}} @@ -941,7 +942,7 @@ ifcapable altertable { PRAGMA foreign_keys = off; ALTER TABLE t2 ADD COLUMN h DEFAULT 'text' REFERENCES t1; PRAGMA foreign_keys = on; - SELECT sql FROM sqlite_master WHERE name='t2'; + SELECT sql FROM sqlite_schema WHERE name='t2'; } } {{CREATE TABLE t2(a, b, c REFERENCES t1, d DEFAULT NULL REFERENCES t1, e REFERENCES t1 DEFAULT NULL, h DEFAULT 'text' REFERENCES t1)}} @@ -949,8 +950,11 @@ ifcapable altertable { # Test the sqlite_rename_parent() function directly. # proc test_rename_parent {zCreate zOld zNew} { - db eval {SELECT sqlite_rename_parent($zCreate, $zOld, $zNew)} + db eval {SELECT sqlite_rename_table( + 'main', 'table', 't1', $zCreate, $zOld, $zNew, 0 + )} } + sqlite3_test_control SQLITE_TESTCTRL_INTERNAL_FUNCTIONS db do_test without_rowid3-14.2.1.1 { test_rename_parent {CREATE TABLE t1(a REFERENCES t2)} t2 t3 } {{CREATE TABLE t1(a REFERENCES "t3")}} @@ -960,6 +964,7 @@ ifcapable altertable { do_test without_rowid3-14.2.1.3 { test_rename_parent {CREATE TABLE t1(a REFERENCES "t2")} t2 t3 } {{CREATE TABLE t1(a REFERENCES "t3")}} + sqlite3_test_control SQLITE_TESTCTRL_INTERNAL_FUNCTIONS db # Test ALTER TABLE RENAME TABLE a bit. # @@ -971,7 +976,7 @@ ifcapable altertable { WITHOUT rowid; CREATE TABLE t3(a REFERENCES t1, b REFERENCES t2, c REFERENCES t1); } - execsql { SELECT sql FROM sqlite_master WHERE type = 'table'} + execsql { SELECT sql FROM sqlite_schema WHERE type = 'table'} } [list \ {CREATE TABLE t1(a PRIMARY KEY, b REFERENCES t1) WITHOUT rowid} \ {CREATE TABLE t2(a PRIMARY KEY, b REFERENCES t1, c REFERENCES t2) @@ -980,7 +985,7 @@ ifcapable altertable { ] do_test without_rowid3-14.2.2.2 { execsql { ALTER TABLE t1 RENAME TO t4 } - execsql { SELECT sql FROM sqlite_master WHERE type = 'table'} + execsql { SELECT sql FROM sqlite_schema WHERE type = 'table'} } [list \ {CREATE TABLE "t4"(a PRIMARY KEY, b REFERENCES "t4") WITHOUT rowid} \ {CREATE TABLE t2(a PRIMARY KEY, b REFERENCES "t4", c REFERENCES t2) @@ -1011,6 +1016,7 @@ ifcapable altertable { execsql { CREATE TEMP TABLE t1(a PRIMARY KEY) WITHOUT rowid; CREATE TEMP TABLE t2(a, b); + INSERT INTO temp.t2(a,b) VALUES(1,2); } catchsql { ALTER TABLE t2 ADD COLUMN c REFERENCES t1 } } {0 {}} @@ -1031,10 +1037,11 @@ ifcapable altertable { PRAGMA foreign_keys = off; ALTER TABLE t2 ADD COLUMN h DEFAULT 'text' REFERENCES t1; PRAGMA foreign_keys = on; - SELECT sql FROM sqlite_temp_master WHERE name='t2'; + SELECT sql FROM temp.sqlite_schema WHERE name='t2'; } } {{CREATE TABLE t2(a, b, c REFERENCES t1, d DEFAULT NULL REFERENCES t1, e REFERENCES t1 DEFAULT NULL, h DEFAULT 'text' REFERENCES t1)}} + sqlite3_test_control SQLITE_TESTCTRL_INTERNAL_FUNCTIONS db do_test without_rowid3-14.2tmp.1.1 { test_rename_parent {CREATE TABLE t1(a REFERENCES t2)} t2 t3 } {{CREATE TABLE t1(a REFERENCES "t3")}} @@ -1044,6 +1051,7 @@ ifcapable altertable { do_test without_rowid3-14.2tmp.1.3 { test_rename_parent {CREATE TABLE t1(a REFERENCES "t2")} t2 t3 } {{CREATE TABLE t1(a REFERENCES "t3")}} + sqlite3_test_control SQLITE_TESTCTRL_INTERNAL_FUNCTIONS db # Test ALTER TABLE RENAME TABLE a bit. # @@ -1055,7 +1063,7 @@ ifcapable altertable { WITHOUT rowid; CREATE TEMP TABLE t3(a REFERENCES t1, b REFERENCES t2, c REFERENCES t1); } - execsql { SELECT sql FROM sqlite_temp_master WHERE type = 'table'} + execsql { SELECT sql FROM sqlite_temp_schema WHERE type = 'table'} } [list \ {CREATE TABLE t1(a PRIMARY KEY, b REFERENCES t1) WITHOUT rowid} \ {CREATE TABLE t2(a PRIMARY KEY, b REFERENCES t1, c REFERENCES t2) @@ -1064,7 +1072,7 @@ ifcapable altertable { ] do_test without_rowid3-14.2tmp.2.2 { execsql { ALTER TABLE t1 RENAME TO t4 } - execsql { SELECT sql FROM sqlite_temp_master WHERE type = 'table'} + execsql { SELECT sql FROM temp.sqlite_schema WHERE type = 'table'} } [list \ {CREATE TABLE "t4"(a PRIMARY KEY, b REFERENCES "t4") WITHOUT rowid} \ {CREATE TABLE t2(a PRIMARY KEY, b REFERENCES "t4", c REFERENCES t2) @@ -1096,6 +1104,7 @@ ifcapable altertable { ATTACH ':memory:' AS aux; CREATE TABLE aux.t1(a PRIMARY KEY) WITHOUT rowid; CREATE TABLE aux.t2(a, b); + INSERT INTO aux.t2(a,b) VALUES(1,2); } catchsql { ALTER TABLE t2 ADD COLUMN c REFERENCES t1 } } {0 {}} @@ -1116,10 +1125,11 @@ ifcapable altertable { PRAGMA foreign_keys = off; ALTER TABLE t2 ADD COLUMN h DEFAULT 'text' REFERENCES t1; PRAGMA foreign_keys = on; - SELECT sql FROM aux.sqlite_master WHERE name='t2'; + SELECT sql FROM aux.sqlite_schema WHERE name='t2'; } } {{CREATE TABLE t2(a, b, c REFERENCES t1, d DEFAULT NULL REFERENCES t1, e REFERENCES t1 DEFAULT NULL, h DEFAULT 'text' REFERENCES t1)}} + sqlite3_test_control SQLITE_TESTCTRL_INTERNAL_FUNCTIONS db do_test without_rowid3-14.2aux.1.1 { test_rename_parent {CREATE TABLE t1(a REFERENCES t2)} t2 t3 } {{CREATE TABLE t1(a REFERENCES "t3")}} @@ -1129,6 +1139,7 @@ ifcapable altertable { do_test without_rowid3-14.2aux.1.3 { test_rename_parent {CREATE TABLE t1(a REFERENCES "t2")} t2 t3 } {{CREATE TABLE t1(a REFERENCES "t3")}} + sqlite3_test_control SQLITE_TESTCTRL_INTERNAL_FUNCTIONS db # Test ALTER TABLE RENAME TABLE a bit. # @@ -1140,7 +1151,7 @@ ifcapable altertable { WITHOUT rowid; CREATE TABLE aux.t3(a REFERENCES t1, b REFERENCES t2, c REFERENCES t1); } - execsql { SELECT sql FROM aux.sqlite_master WHERE type = 'table'} + execsql { SELECT sql FROM aux.sqlite_schema WHERE type = 'table'} } [list \ {CREATE TABLE t1(a PRIMARY KEY, b REFERENCES t1) WITHOUT rowid} \ {CREATE TABLE t2(a PRIMARY KEY, b REFERENCES t1, c REFERENCES t2) @@ -1149,7 +1160,7 @@ ifcapable altertable { ] do_test without_rowid3-14.2aux.2.2 { execsql { ALTER TABLE t1 RENAME TO t4 } - execsql { SELECT sql FROM aux.sqlite_master WHERE type = 'table'} + execsql { SELECT sql FROM aux.sqlite_schema WHERE type = 'table'} } [list \ {CREATE TABLE "t4"(a PRIMARY KEY, b REFERENCES "t4") WITHOUT rowid} \ {CREATE TABLE t2(a PRIMARY KEY, b REFERENCES "t4", c REFERENCES t2) diff --git a/test/without_rowid5.test b/test/without_rowid5.test index 31a440ad87..d1c494484c 100644 --- a/test/without_rowid5.test +++ b/test/without_rowid5.test @@ -15,6 +15,10 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl +ifcapable !incrblob { + finish_test + return +} # EVIDENCE-OF: R-36924-43758 By default, every row in SQLite has a # special column, usually called the "rowid", that uniquely identifies @@ -186,6 +190,87 @@ do_execsql_test without_rowid5-5.9 { SELECT count(*) FROM nnw; } {1} +# Ticket f2be158c57aaa8c6 (2021-08-18) +# NOT NULL ON CONFLICT clauses work on WITHOUT ROWID tables now. +# +do_test without_rowid5-5.100 { + db eval { + DROP TABLE IF EXISTS t5; + CREATE TABLE t5( + a INT NOT NULL ON CONFLICT ROLLBACK, + b TEXT, + c TEXT, + PRIMARY KEY(a,b) + ) WITHOUT ROWID; + BEGIN; + INSERT INTO t5(a,b,c) VALUES(1,2,3); + } + catch {db eval {INSERT INTO t5(a,b,c) VALUES(NULL,6,7);}} + db eval { + SELECT * FROM t5; + } +} {} +do_test without_rowid5-5.101 { + db eval { + DROP TABLE IF EXISTS t5; + CREATE TABLE t5( + a INT NOT NULL ON CONFLICT ABORT, + b TEXT, + c TEXT, + PRIMARY KEY(a,b) + ) WITHOUT ROWID; + BEGIN; + INSERT INTO t5(a,b,c) VALUES(1,2,3); + } + catch {db eval {INSERT INTO t5(a,b,c) VALUES(NULL,6,7);}} + db eval { + COMMIT; + SELECT * FROM t5; + } +} {1 2 3} +do_test without_rowid5-5.102 { + db eval { + DROP TABLE IF EXISTS t5; + CREATE TABLE t5( + a INT NOT NULL ON CONFLICT FAIL, + b TEXT, + c TEXT, + PRIMARY KEY(a,b) + ) WITHOUT ROWID; + } + catch {db eval {INSERT INTO t5(a,b,c) VALUES(1,2,3),(NULL,4,5),(6,7,8);}} + db eval { + SELECT * FROM t5; + } +} {1 2 3} +do_test without_rowid5-5.103 { + db eval { + DROP TABLE IF EXISTS t5; + CREATE TABLE t5( + a INT NOT NULL ON CONFLICT IGNORE, + b TEXT, + c TEXT, + PRIMARY KEY(a,b) + ) WITHOUT ROWID; + INSERT INTO t5(a,b,c) VALUES(1,2,3),(NULL,4,5),(6,7,8); + SELECT * FROM t5; + } +} {1 2 3 6 7 8} +do_test without_rowid5-5.104 { + db eval { + DROP TABLE IF EXISTS t5; + CREATE TABLE t5( + a INT NOT NULL ON CONFLICT REPLACE DEFAULT 3, + b TEXT, + c TEXT, + PRIMARY KEY(a,b) + ) WITHOUT ROWID; + INSERT INTO t5(a,b,c) VALUES(1,2,3),(NULL,4,5),(6,7,8); + SELECT * FROM t5; + } +} {1 2 3 3 4 5 6 7 8} + + # EVIDENCE-OF: R-12643-30541 The incremental blob I/O mechanism does not # work for WITHOUT ROWID tables. # diff --git a/test/without_rowid6.test b/test/without_rowid6.test index 3f9fe415d8..06fc7435b0 100644 --- a/test/without_rowid6.test +++ b/test/without_rowid6.test @@ -16,6 +16,10 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl +proc do_execsql_test_if_vtab {tn sql {res {}}} { + ifcapable vtab { uplevel [list do_execsql_test $tn $sql $res] } +} + do_execsql_test without_rowid6-100 { CREATE TABLE t1(a,b,c,d,e, PRIMARY KEY(a,b,c,a,b,c,d,a,b,c)) WITHOUT ROWID; CREATE INDEX t1a ON t1(b, b); @@ -24,6 +28,9 @@ do_execsql_test without_rowid6-100 { INSERT INTO t1(a,b,c,d,e) SELECT i, i+1000, printf('x%dy',i), 0, 0 FROM c; ANALYZE; } {} +do_execsql_test_if_vtab without_rowid6-101 { + SELECT name, key FROM pragma_index_xinfo('t1'); +} {a 1 b 1 c 1 d 1 e 0} do_execsql_test without_rowid6-110 { SELECT c FROM t1 WHERE a=123; } {x123y} @@ -51,10 +58,13 @@ do_execsql_test without_rowid6-200 { INSERT INTO t1(a,b,c) VALUES(1,8,3),(4,5,6),(7,2,9); SELECT a FROM t1 WHERE b>3 ORDER BY b; } {4 1} +do_execsql_test_if_vtab without_rowid6-201 { + SELECT name, key FROM pragma_index_xinfo('t1'); +} {b 1 a 0 c 0} do_execsql_test without_rowid6-210 { EXPLAIN QUERY PLAN SELECT a FROM t1 WHERE b>3 ORDER BY b; -} {/SEARCH TABLE t1 USING PRIMARY KEY .b>../} +} {/SEARCH t1 USING PRIMARY KEY .b>../} do_execsql_test without_rowid6-220 { PRAGMA index_list(t1); } {/sqlite_autoindex_t1_2 1 pk/} @@ -73,7 +83,7 @@ do_execsql_test without_rowid6-300 { do_execsql_test without_rowid6-310 { EXPLAIN QUERY PLAN SELECT a FROM t1 WHERE b>3 ORDER BY b; -} {/SEARCH TABLE t1 USING PRIMARY KEY .b>../} +} {/SEARCH t1 USING PRIMARY KEY .b>../} do_execsql_test without_rowid6-320 { PRAGMA index_list(t1); } {/sqlite_autoindex_t1_2 1 pk/} @@ -91,7 +101,7 @@ do_execsql_test without_rowid6-400 { do_execsql_test without_rowid6-410 { EXPLAIN QUERY PLAN SELECT a FROM t1 WHERE b>3 ORDER BY b; -} {/SEARCH TABLE t1 USING PRIMARY KEY .b>../} +} {/SEARCH t1 USING PRIMARY KEY .b>../} do_execsql_test without_rowid6-420 { PRAGMA index_list(t1); } {/sqlite_autoindex_t1_2 1 pk/} @@ -105,10 +115,13 @@ do_execsql_test without_rowid6-500 { INSERT INTO t1(a,b,c) VALUES(1,8,3),(4,5,6),(7,2,9); SELECT a FROM t1 WHERE b>3 ORDER BY b; } {4 1} +do_execsql_test_if_vtab without_rowid6-501 { + SELECT name, key FROM pragma_index_xinfo('t1'); +} {b 1 c 1 a 0} do_execsql_test without_rowid6-510 { EXPLAIN QUERY PLAN SELECT a FROM t1 WHERE b>3 ORDER BY b; -} {/SEARCH TABLE t1 USING PRIMARY KEY .b>../} +} {/SEARCH t1 USING PRIMARY KEY .b>../} do_execsql_test without_rowid6-520 { PRAGMA index_list(t1); } {/sqlite_autoindex_t1_1 1 pk/} diff --git a/test/without_rowid7.test b/test/without_rowid7.test new file mode 100644 index 0000000000..bf0273d705 --- /dev/null +++ b/test/without_rowid7.test @@ -0,0 +1,123 @@ +# 2019 July 17 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# This file implements regression tests for SQLite library. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix without_rowid7 + +proc do_execsql_test_if_vtab {tn sql {res {}}} { + ifcapable vtab { uplevel [list do_execsql_test $tn $sql $res] } +} + +do_execsql_test 1.0 { + CREATE TABLE t1(a, b COLLATE nocase, PRIMARY KEY(a, a, b)) WITHOUT ROWID; +} + +do_catchsql_test 1.1 { + INSERT INTO t1 VALUES(1, 'one'), (1, 'ONE'); +} {1 {UNIQUE constraint failed: t1.a, t1.b}} + + +do_execsql_test 2.0 { + CREATE TABLE t2(a, b, PRIMARY KEY(a COLLATE nocase, a)) WITHOUT ROWID; +} + +do_execsql_test 2.1 { + INSERT INTO t2 VALUES(1, 'one'); + SELECT b FROM t2; +} {one} + +do_execsql_test 2.2a { + PRAGMA index_info(t2); +} {0 0 a 1 0 a} +do_execsql_test_if_vtab 2.2b { + SELECT *, '|' FROM pragma_index_info('t2'); +} {0 0 a | 1 0 a |} +do_execsql_test 2.3a { + PRAGMA index_xinfo(t2); +} {0 0 a 0 nocase 1 1 0 a 0 BINARY 1 2 1 b 0 BINARY 0} +do_execsql_test_if_vtab 2.3b { + SELECT *, '|' FROM pragma_index_xinfo('t2'); +} {0 0 a 0 nocase 1 | 1 0 a 0 BINARY 1 | 2 1 b 0 BINARY 0 |} + +do_execsql_test 2.4 { + CREATE TABLE t3(a, b, PRIMARY KEY(a COLLATE nocase, a)); + PRAGMA index_info(t3); +} {} + +#------------------------------------------------------------------------- +reset_db +db collate mysort mysort +db collate mysort2 mysort +proc mysort {a b} { string compare $a $b } +do_execsql_test 3.0 { + CREATE TABLE t1( + a PRIMARY KEY COLLATE mysort, b COLLATE mysort2 + ) WITHOUT ROWID; + INSERT INTO t1 VALUES(1, 2); +} + +db close +sqlite3 db test.db + +do_catchsql_test 3.1.1 { + SELECT * FROM t1 WHERE a=1; +} {1 {no such collation sequence: mysort}} +do_test 3.1.2 { + sqlite3_extended_errcode db +} {SQLITE_ERROR_MISSING_COLLSEQ} + +db collate mysort mysort + +do_catchsql_test 3.2.1 { + CREATE UNIQUE INDEX i1 ON t1(b); +} {1 {no such collation sequence: mysort2}} +do_test 3.2.2 { + sqlite3_extended_errcode db +} {SQLITE_ERROR_MISSING_COLLSEQ} + +db close +sqlite3 db test.db + +do_catchsql_test 3.3.1 { + CREATE UNIQUE INDEX i1 ON t1(1); +} {1 {no such collation sequence: mysort}} +do_test 3.3.2 { + sqlite3_extended_errcode db +} {SQLITE_ERROR_MISSING_COLLSEQ} + +do_test 3.4.1 { + list [catch { + sqlite3_prepare_v3 db "CREATE UNIQUE INDEX i1 ON t1(1)" -1 0 + } msg] $msg +} {1 {(1) no such collation sequence: mysort}} +do_test 3.4.2 { + sqlite3_extended_errcode db +} {SQLITE_ERROR_MISSING_COLLSEQ} + +sqlite3_extended_result_codes db 1 + +do_test 3.5.1 { + list [catch { + sqlite3_prepare_v3 db "CREATE UNIQUE INDEX i1 ON t1(1)" -1 0 + } msg] $msg +} {1 {(257) no such collation sequence: mysort}} +do_test 3.5.2 { + sqlite3_extended_errcode db +} {SQLITE_ERROR_MISSING_COLLSEQ} + +do_catchsql_test 3.6 { + SELECT * FROM t1 WHERE a=1; +} {1 {no such collation sequence: mysort}} + +finish_test diff --git a/test/wordcount.c b/test/wordcount.c index 72aa6b2f0b..4a21d25ddd 100644 --- a/test/wordcount.c +++ b/test/wordcount.c @@ -13,23 +13,6 @@ ** ** Option: ** -** --without-rowid Use a WITHOUT ROWID table to store the words. -** --insert Use INSERT mode (the default) -** --replace Use REPLACE mode -** --select Use SELECT mode -** --update Use UPDATE mode -** --delete Use DELETE mode -** --query Use QUERY mode -** --nocase Add the NOCASE collating sequence to the words. -** --trace Enable sqlite3_trace() output. -** --summary Show summary information on the collected data. -** --stats Show sqlite3_status() results at the end. -** --pagesize NNN Use a page size of NNN -** --cachesize NNN Use a cache size of NNN -** --commit NNN Commit after every NNN operations -** --nosync Use PRAGMA synchronous=OFF -** --journal MMMM Use PRAGMA journal_mode=MMMM -** --timer Time the operation of this program ** ** Modes: ** @@ -45,6 +28,10 @@ ** (1) REPLACE INTO wordcount ** VALUES($new,ifnull((SELECT cnt FROM wordcount WHERE word=$new),0)+1); ** +** Upsert mode means: +** (1) INSERT INTO wordcount VALUES($new,1) +** ON CONFLICT(word) DO UPDATE SET cnt=cnt+1 +** ** Select mode means: ** (1) SELECT 1 FROM wordcount WHERE word=$new ** (2) INSERT INTO wordcount VALUES($new,1) -- if (1) returns nothing @@ -80,8 +67,40 @@ #include <stdlib.h> #include <stdarg.h> #include "sqlite3.h" +#ifndef _WIN32 +# include <unistd.h> +#else +# include <io.h> +#endif #define ISALPHA(X) isalpha((unsigned char)(X)) +const char zHelp[] = +"Usage: wordcount [OPTIONS] DATABASE [INPUT]\n" +" --all Repeat the test for all test modes\n" +" --cachesize NNN Use a cache size of NNN\n" +" --commit NNN Commit after every NNN operations\n" +" --delete Use DELETE mode\n" +" --insert Use INSERT mode (the default)\n" +" --journal MMMM Use PRAGMA journal_mode=MMMM\n" +" --nocase Add the NOCASE collating sequence to the words.\n" +" --nosync Use PRAGMA synchronous=OFF\n" +" --pagesize NNN Use a page size of NNN\n" +" --query Use QUERY mode\n" +" --replace Use REPLACE mode\n" +" --select Use SELECT mode\n" +" --stats Show sqlite3_status() results at the end.\n" +" --summary Show summary information on the collected data.\n" +" --tag NAME Tag all output using NAME. Use only stdout.\n" +" --timer Time the operation of this program\n" +" --trace Enable sqlite3_trace() output.\n" +" --update Use UPDATE mode\n" +" --upsert Use UPSERT mode\n" +" --without-rowid Use a WITHOUT ROWID table to store the words.\n" +; + +/* Output tag */ +char *zTag = "--"; + /* Return the current wall-clock time */ static sqlite3_int64 realTime(void){ static sqlite3_vfs *clockVfs = 0; @@ -106,6 +125,12 @@ static void fatal_error(const char *zMsg, ...){ exit(1); } +/* Print a usage message and quit */ +static void usage(void){ + printf("%s",zHelp); + exit(0); +} + /* The sqlite3_trace() callback function */ static void traceCallback(void *NotUsed, const char *zSql){ printf("%s;\n", zSql); @@ -115,7 +140,7 @@ static void traceCallback(void *NotUsed, const char *zSql){ ** each column separated by a single space. */ static int printResult(void *NotUsed, int nArg, char **azArg, char **azNm){ int i; - printf("--"); + printf("%s", zTag); for(i=0; i<nArg; i++){ printf(" %s", azArg[i] ? azArg[i] : "(null)"); } @@ -185,20 +210,59 @@ static void checksumFinalize(sqlite3_context *context){ } } - /* Define operating modes */ #define MODE_INSERT 0 #define MODE_REPLACE 1 -#define MODE_SELECT 2 -#define MODE_UPDATE 3 -#define MODE_DELETE 4 -#define MODE_QUERY 5 +#define MODE_UPSERT 2 +#define MODE_SELECT 3 +#define MODE_UPDATE 4 +#define MODE_DELETE 5 +#define MODE_QUERY 6 +#define MODE_COUNT 7 +#define MODE_ALL (-1) + +/* Mode names */ +static const char *azMode[] = { + "--insert", + "--replace", + "--upsert", + "--select", + "--update", + "--delete", + "--query" +}; + +/* +** Determine if another iteration of the test is required. Return true +** if so. Return zero if all iterations have finished. +*/ +static int allLoop( + int iMode, /* The selected test mode */ + int *piLoopCnt, /* Iteration loop counter */ + int *piMode2, /* The test mode to use on the next iteration */ + int *pUseWithoutRowid /* Whether or not to use --without-rowid */ +){ + int i; + if( iMode!=MODE_ALL ){ + if( *piLoopCnt ) return 0; + *piMode2 = iMode; + *piLoopCnt = 1; + return 1; + } + if( (*piLoopCnt)>=MODE_COUNT*2 ) return 0; + i = (*piLoopCnt)++; + *pUseWithoutRowid = i&1; + *piMode2 = i>>1; + return 1; +} int main(int argc, char **argv){ const char *zFileToRead = 0; /* Input file. NULL for stdin */ const char *zDbName = 0; /* Name of the database file to create */ int useWithoutRowid = 0; /* True for --without-rowid */ int iMode = MODE_INSERT; /* One of MODE_xxxxx */ + int iMode2; /* Mode to use for current --all iteration */ + int iLoopCnt = 0; /* Which iteration when running --all */ int useNocase = 0; /* True for --nocase */ int doTrace = 0; /* True for --trace */ int showStats = 0; /* True for --stats */ @@ -220,8 +284,10 @@ int main(int argc, char **argv){ FILE *in; /* The open input file */ int rc; /* Return code from an SQLite interface */ int iCur, iHiwtr; /* Statistics values, current and "highwater" */ + FILE *pTimer = stderr; /* Output channel for the timer */ sqlite3_int64 sumCnt = 0; /* Sum in QUERY mode */ - sqlite3_int64 startTime; + sqlite3_int64 startTime; /* Time of start */ + sqlite3_int64 totalTime = 0; /* Total time */ char zInput[2000]; /* A single line of input */ /* Process command-line arguments */ @@ -233,6 +299,8 @@ int main(int argc, char **argv){ useWithoutRowid = 1; }else if( strcmp(z,"replace")==0 ){ iMode = MODE_REPLACE; + }else if( strcmp(z,"upsert")==0 ){ + iMode = MODE_UPSERT; }else if( strcmp(z,"select")==0 ){ iMode = MODE_SELECT; }else if( strcmp(z,"insert")==0 ){ @@ -243,6 +311,9 @@ int main(int argc, char **argv){ iMode = MODE_DELETE; }else if( strcmp(z,"query")==0 ){ iMode = MODE_QUERY; + }else if( strcmp(z,"all")==0 ){ + iMode = MODE_ALL; + showTimer = -99; }else if( strcmp(z,"nocase")==0 ){ useNocase = 1; }else if( strcmp(z,"trace")==0 ){ @@ -266,23 +337,33 @@ int main(int argc, char **argv){ commitInterval = atoi(argv[i]); }else if( strcmp(z,"journal")==0 && i<argc-1 ){ zJMode = argv[++i]; + }else if( strcmp(z,"tag")==0 && i<argc-1 ){ + zTag = argv[++i]; + pTimer = stdout; + }else if( strcmp(z, "help")==0 || strcmp(z,"?")==0 ){ + usage(); }else{ - fatal_error("unknown option: %s\n", argv[i]); + fatal_error("unknown option: \"%s\"\n" + "Use --help for a list of options\n", + argv[i]); } }else if( zDbName==0 ){ zDbName = argv[i]; }else if( zFileToRead==0 ){ zFileToRead = argv[i]; }else{ - fatal_error("surplus argument: %s\n", argv[i]); + fatal_error("surplus argument: \"%s\"\n", argv[i]); } } if( zDbName==0 ){ - fatal_error("Usage: %s [--options] DATABASE [INPUTFILE]\n", argv[0]); + usage(); } startTime = realTime(); /* Open the database and the input file */ + if( zDbName[0] && strcmp(zDbName,":memory:")!=0 ){ + unlink(zDbName); + } if( sqlite3_open(zDbName, &db) ){ fatal_error("Cannot open database file: %s\n", zDbName); } @@ -292,6 +373,9 @@ int main(int argc, char **argv){ fatal_error("Could not open input file \"%s\"\n", zFileToRead); } }else{ + if( iMode==MODE_ALL ){ + fatal_error("The --all mode cannot be used with stdin\n"); + } in = stdin; } @@ -314,212 +398,245 @@ int main(int argc, char **argv){ sqlite3_free(zSql); } - - /* Construct the "wordcount" table into which to put the words */ - if( sqlite3_exec(db, "BEGIN IMMEDIATE", 0, 0, 0) ){ - fatal_error("Could not start a transaction\n"); - } - zSql = sqlite3_mprintf( - "CREATE TABLE IF NOT EXISTS wordcount(\n" - " word TEXT PRIMARY KEY COLLATE %s,\n" - " cnt INTEGER\n" - ")%s", - useNocase ? "nocase" : "binary", - useWithoutRowid ? " WITHOUT ROWID" : "" - ); - if( zSql==0 ) fatal_error("out of memory\n"); - rc = sqlite3_exec(db, zSql, 0, 0, 0); - if( rc ) fatal_error("Could not create the wordcount table: %s.\n", - sqlite3_errmsg(db)); - sqlite3_free(zSql); - - /* Prepare SQL statements that will be needed */ - if( iMode==MODE_QUERY ){ - rc = sqlite3_prepare_v2(db, - "SELECT cnt FROM wordcount WHERE word=?1", - -1, &pSelect, 0); - if( rc ) fatal_error("Could not prepare the SELECT statement: %s\n", - sqlite3_errmsg(db)); - } - if( iMode==MODE_SELECT ){ - rc = sqlite3_prepare_v2(db, - "SELECT 1 FROM wordcount WHERE word=?1", - -1, &pSelect, 0); - if( rc ) fatal_error("Could not prepare the SELECT statement: %s\n", - sqlite3_errmsg(db)); - rc = sqlite3_prepare_v2(db, - "INSERT INTO wordcount(word,cnt) VALUES(?1,1)", - -1, &pInsert, 0); - if( rc ) fatal_error("Could not prepare the INSERT statement: %s\n", - sqlite3_errmsg(db)); - } - if( iMode==MODE_SELECT || iMode==MODE_UPDATE || iMode==MODE_INSERT ){ - rc = sqlite3_prepare_v2(db, - "UPDATE wordcount SET cnt=cnt+1 WHERE word=?1", - -1, &pUpdate, 0); - if( rc ) fatal_error("Could not prepare the UPDATE statement: %s\n", - sqlite3_errmsg(db)); - } - if( iMode==MODE_INSERT ){ - rc = sqlite3_prepare_v2(db, - "INSERT OR IGNORE INTO wordcount(word,cnt) VALUES(?1,1)", - -1, &pInsert, 0); - if( rc ) fatal_error("Could not prepare the INSERT statement: %s\n", - sqlite3_errmsg(db)); - } - if( iMode==MODE_UPDATE ){ - rc = sqlite3_prepare_v2(db, - "INSERT OR IGNORE INTO wordcount(word,cnt) VALUES(?1,0)", - -1, &pInsert, 0); - if( rc ) fatal_error("Could not prepare the INSERT statement: %s\n", + iLoopCnt = 0; + while( allLoop(iMode, &iLoopCnt, &iMode2, &useWithoutRowid) ){ + /* Delete prior content in --all mode */ + if( iMode==MODE_ALL ){ + if( sqlite3_exec(db, "DROP TABLE IF EXISTS wordcount; VACUUM;",0,0,0) ){ + fatal_error("Could not clean up prior iteration\n"); + } + startTime = realTime(); + rewind(in); + } + + /* Construct the "wordcount" table into which to put the words */ + if( sqlite3_exec(db, "BEGIN IMMEDIATE", 0, 0, 0) ){ + fatal_error("Could not start a transaction\n"); + } + zSql = sqlite3_mprintf( + "CREATE TABLE IF NOT EXISTS wordcount(\n" + " word TEXT PRIMARY KEY COLLATE %s,\n" + " cnt INTEGER\n" + ")%s", + useNocase ? "nocase" : "binary", + useWithoutRowid ? " WITHOUT ROWID" : "" + ); + if( zSql==0 ) fatal_error("out of memory\n"); + rc = sqlite3_exec(db, zSql, 0, 0, 0); + if( rc ) fatal_error("Could not create the wordcount table: %s.\n", sqlite3_errmsg(db)); - } - if( iMode==MODE_REPLACE ){ - rc = sqlite3_prepare_v2(db, + sqlite3_free(zSql); + + /* Prepare SQL statements that will be needed */ + if( iMode2==MODE_QUERY ){ + rc = sqlite3_prepare_v2(db, + "SELECT cnt FROM wordcount WHERE word=?1", + -1, &pSelect, 0); + if( rc ) fatal_error("Could not prepare the SELECT statement: %s\n", + sqlite3_errmsg(db)); + } + if( iMode2==MODE_SELECT ){ + rc = sqlite3_prepare_v2(db, + "SELECT 1 FROM wordcount WHERE word=?1", + -1, &pSelect, 0); + if( rc ) fatal_error("Could not prepare the SELECT statement: %s\n", + sqlite3_errmsg(db)); + rc = sqlite3_prepare_v2(db, + "INSERT INTO wordcount(word,cnt) VALUES(?1,1)", + -1, &pInsert, 0); + if( rc ) fatal_error("Could not prepare the INSERT statement: %s\n", + sqlite3_errmsg(db)); + } + if( iMode2==MODE_SELECT || iMode2==MODE_UPDATE || iMode2==MODE_INSERT ){ + rc = sqlite3_prepare_v2(db, + "UPDATE wordcount SET cnt=cnt+1 WHERE word=?1", + -1, &pUpdate, 0); + if( rc ) fatal_error("Could not prepare the UPDATE statement: %s\n", + sqlite3_errmsg(db)); + } + if( iMode2==MODE_INSERT ){ + rc = sqlite3_prepare_v2(db, + "INSERT OR IGNORE INTO wordcount(word,cnt) VALUES(?1,1)", + -1, &pInsert, 0); + if( rc ) fatal_error("Could not prepare the INSERT statement: %s\n", + sqlite3_errmsg(db)); + } + if( iMode2==MODE_UPDATE ){ + rc = sqlite3_prepare_v2(db, + "INSERT OR IGNORE INTO wordcount(word,cnt) VALUES(?1,0)", + -1, &pInsert, 0); + if( rc ) fatal_error("Could not prepare the INSERT statement: %s\n", + sqlite3_errmsg(db)); + } + if( iMode2==MODE_REPLACE ){ + rc = sqlite3_prepare_v2(db, "REPLACE INTO wordcount(word,cnt)" "VALUES(?1,coalesce((SELECT cnt FROM wordcount WHERE word=?1),0)+1)", -1, &pInsert, 0); - if( rc ) fatal_error("Could not prepare the REPLACE statement: %s\n", - sqlite3_errmsg(db)); - } - if( iMode==MODE_DELETE ){ - rc = sqlite3_prepare_v2(db, - "DELETE FROM wordcount WHERE word=?1", - -1, &pDelete, 0); - if( rc ) fatal_error("Could not prepare the DELETE statement: %s\n", - sqlite3_errmsg(db)); - } - - /* Process the input file */ - while( fgets(zInput, sizeof(zInput), in) ){ - for(i=0; zInput[i]; i++){ - if( !ISALPHA(zInput[i]) ) continue; - for(j=i+1; ISALPHA(zInput[j]); j++){} - - /* Found a new word at zInput[i] that is j-i bytes long. - ** Process it into the wordcount table. */ - if( iMode==MODE_DELETE ){ - sqlite3_bind_text(pDelete, 1, zInput+i, j-i, SQLITE_STATIC); - if( sqlite3_step(pDelete)!=SQLITE_DONE ){ - fatal_error("DELETE failed: %s\n", sqlite3_errmsg(db)); - } - sqlite3_reset(pDelete); - }else if( iMode==MODE_SELECT ){ - sqlite3_bind_text(pSelect, 1, zInput+i, j-i, SQLITE_STATIC); - rc = sqlite3_step(pSelect); - sqlite3_reset(pSelect); - if( rc==SQLITE_ROW ){ - sqlite3_bind_text(pUpdate, 1, zInput+i, j-i, SQLITE_STATIC); - if( sqlite3_step(pUpdate)!=SQLITE_DONE ){ - fatal_error("UPDATE failed: %s\n", sqlite3_errmsg(db)); + if( rc ) fatal_error("Could not prepare the REPLACE statement: %s\n", + sqlite3_errmsg(db)); + } + if( iMode2==MODE_UPSERT ){ + rc = sqlite3_prepare_v2(db, + "INSERT INTO wordcount(word,cnt) VALUES(?1,1) " + "ON CONFLICT(word) DO UPDATE SET cnt=cnt+1", + -1, &pInsert, 0); + if( rc ) fatal_error("Could not prepare the UPSERT statement: %s\n", + sqlite3_errmsg(db)); + } + if( iMode2==MODE_DELETE ){ + rc = sqlite3_prepare_v2(db, + "DELETE FROM wordcount WHERE word=?1", + -1, &pDelete, 0); + if( rc ) fatal_error("Could not prepare the DELETE statement: %s\n", + sqlite3_errmsg(db)); + } + + /* Process the input file */ + while( fgets(zInput, sizeof(zInput), in) ){ + for(i=0; zInput[i]; i++){ + if( !ISALPHA(zInput[i]) ) continue; + for(j=i+1; ISALPHA(zInput[j]); j++){} + + /* Found a new word at zInput[i] that is j-i bytes long. + ** Process it into the wordcount table. */ + if( iMode2==MODE_DELETE ){ + sqlite3_bind_text(pDelete, 1, zInput+i, j-i, SQLITE_STATIC); + if( sqlite3_step(pDelete)!=SQLITE_DONE ){ + fatal_error("DELETE failed: %s\n", sqlite3_errmsg(db)); + } + sqlite3_reset(pDelete); + }else if( iMode2==MODE_SELECT ){ + sqlite3_bind_text(pSelect, 1, zInput+i, j-i, SQLITE_STATIC); + rc = sqlite3_step(pSelect); + sqlite3_reset(pSelect); + if( rc==SQLITE_ROW ){ + sqlite3_bind_text(pUpdate, 1, zInput+i, j-i, SQLITE_STATIC); + if( sqlite3_step(pUpdate)!=SQLITE_DONE ){ + fatal_error("UPDATE failed: %s\n", sqlite3_errmsg(db)); + } + sqlite3_reset(pUpdate); + }else if( rc==SQLITE_DONE ){ + sqlite3_bind_text(pInsert, 1, zInput+i, j-i, SQLITE_STATIC); + if( sqlite3_step(pInsert)!=SQLITE_DONE ){ + fatal_error("Insert failed: %s\n", sqlite3_errmsg(db)); + } + sqlite3_reset(pInsert); + }else{ + fatal_error("SELECT failed: %s\n", sqlite3_errmsg(db)); + } + }else if( iMode2==MODE_QUERY ){ + sqlite3_bind_text(pSelect, 1, zInput+i, j-i, SQLITE_STATIC); + if( sqlite3_step(pSelect)==SQLITE_ROW ){ + sumCnt += sqlite3_column_int64(pSelect, 0); } - sqlite3_reset(pUpdate); - }else if( rc==SQLITE_DONE ){ + sqlite3_reset(pSelect); + }else{ sqlite3_bind_text(pInsert, 1, zInput+i, j-i, SQLITE_STATIC); if( sqlite3_step(pInsert)!=SQLITE_DONE ){ - fatal_error("Insert failed: %s\n", sqlite3_errmsg(db)); + fatal_error("INSERT failed: %s\n", sqlite3_errmsg(db)); } sqlite3_reset(pInsert); - }else{ - fatal_error("SELECT failed: %s\n", sqlite3_errmsg(db)); - } - }else if( iMode==MODE_QUERY ){ - sqlite3_bind_text(pSelect, 1, zInput+i, j-i, SQLITE_STATIC); - if( sqlite3_step(pSelect)==SQLITE_ROW ){ - sumCnt += sqlite3_column_int64(pSelect, 0); - } - sqlite3_reset(pSelect); - }else{ - sqlite3_bind_text(pInsert, 1, zInput+i, j-i, SQLITE_STATIC); - if( sqlite3_step(pInsert)!=SQLITE_DONE ){ - fatal_error("INSERT failed: %s\n", sqlite3_errmsg(db)); - } - sqlite3_reset(pInsert); - if( iMode==MODE_UPDATE - || (iMode==MODE_INSERT && sqlite3_changes(db)==0) - ){ - sqlite3_bind_text(pUpdate, 1, zInput+i, j-i, SQLITE_STATIC); - if( sqlite3_step(pUpdate)!=SQLITE_DONE ){ - fatal_error("UPDATE failed: %s\n", sqlite3_errmsg(db)); + if( iMode2==MODE_UPDATE + || (iMode2==MODE_INSERT && sqlite3_changes(db)==0) + ){ + sqlite3_bind_text(pUpdate, 1, zInput+i, j-i, SQLITE_STATIC); + if( sqlite3_step(pUpdate)!=SQLITE_DONE ){ + fatal_error("UPDATE failed: %s\n", sqlite3_errmsg(db)); + } + sqlite3_reset(pUpdate); } - sqlite3_reset(pUpdate); + } + i = j-1; + + /* Increment the operation counter. Do a COMMIT if it is time. */ + nOp++; + if( commitInterval>0 && (nOp%commitInterval)==0 ){ + sqlite3_exec(db, "COMMIT; BEGIN IMMEDIATE", 0, 0, 0); } } - i = j-1; - - /* Increment the operation counter. Do a COMMIT if it is time. */ - nOp++; - if( commitInterval>0 && (nOp%commitInterval)==0 ){ - sqlite3_exec(db, "COMMIT; BEGIN IMMEDIATE", 0, 0, 0); + } + sqlite3_exec(db, "COMMIT", 0, 0, 0); + sqlite3_finalize(pInsert); pInsert = 0; + sqlite3_finalize(pUpdate); pUpdate = 0; + sqlite3_finalize(pSelect); pSelect = 0; + sqlite3_finalize(pDelete); pDelete = 0; + + if( iMode2==MODE_QUERY && iMode!=MODE_ALL ){ + printf("%s sum of cnt: %lld\n", zTag, sumCnt); + rc = sqlite3_prepare_v2(db,"SELECT sum(cnt*cnt) FROM wordcount", -1, + &pSelect, 0); + if( rc==SQLITE_OK && sqlite3_step(pSelect)==SQLITE_ROW ){ + printf("%s double-check: %lld\n", zTag,sqlite3_column_int64(pSelect,0)); } + sqlite3_finalize(pSelect); } - } - sqlite3_exec(db, "COMMIT", 0, 0, 0); - if( zFileToRead ) fclose(in); - sqlite3_finalize(pInsert); - sqlite3_finalize(pUpdate); - sqlite3_finalize(pSelect); - sqlite3_finalize(pDelete); - - if( iMode==MODE_QUERY ){ - printf("sum of cnt: %lld\n", sumCnt); - rc = sqlite3_prepare_v2(db,"SELECT sum(cnt*cnt) FROM wordcount", -1, - &pSelect, 0); - if( rc==SQLITE_OK && sqlite3_step(pSelect)==SQLITE_ROW ){ - printf("double-check: %lld\n", sqlite3_column_int64(pSelect, 0)); + + + if( showTimer ){ + sqlite3_int64 elapseTime = realTime() - startTime; + totalTime += elapseTime; + fprintf(pTimer, "%3d.%03d wordcount", (int)(elapseTime/1000), + (int)(elapseTime%1000)); + if( iMode==MODE_ALL ){ + fprintf(pTimer, " %s%s\n", azMode[iMode2], + useWithoutRowid? " --without-rowid" : ""); + }else{ + for(i=1; i<argc; i++) if( i!=showTimer ) fprintf(pTimer," %s",argv[i]); + fprintf(pTimer, "\n"); + } } - sqlite3_finalize(pSelect); - } - + + if( showSummary ){ + sqlite3_create_function(db, "checksum", -1, SQLITE_UTF8, 0, + 0, checksumStep, checksumFinalize); + sqlite3_exec(db, + "SELECT 'count(*): ', count(*) FROM wordcount;\n" + "SELECT 'sum(cnt): ', sum(cnt) FROM wordcount;\n" + "SELECT 'max(cnt): ', max(cnt) FROM wordcount;\n" + "SELECT 'avg(cnt): ', avg(cnt) FROM wordcount;\n" + "SELECT 'sum(cnt=1):', sum(cnt=1) FROM wordcount;\n" + "SELECT 'top 10: ', group_concat(word, ', ') FROM " + "(SELECT word FROM wordcount ORDER BY cnt DESC, word LIMIT 10);\n" + "SELECT 'checksum: ', checksum(word, cnt) FROM " + "(SELECT word, cnt FROM wordcount ORDER BY word);\n" + "PRAGMA integrity_check;\n", + printResult, 0, 0); + } + } /* End the --all loop */ - if( showTimer ){ - sqlite3_int64 elapseTime = realTime() - startTime; - fprintf(stderr, "%3d.%03d wordcount", (int)(elapseTime/1000), - (int)(elapseTime%1000)); - for(i=1; i<argc; i++) if( i!=showTimer ) fprintf(stderr, " %s", argv[i]); - fprintf(stderr, "\n"); - } + /* Close the input file after the last read */ + if( zFileToRead ) fclose(in); - if( showSummary ){ - sqlite3_create_function(db, "checksum", -1, SQLITE_UTF8, 0, - 0, checksumStep, checksumFinalize); - sqlite3_exec(db, - "SELECT 'count(*): ', count(*) FROM wordcount;\n" - "SELECT 'sum(cnt): ', sum(cnt) FROM wordcount;\n" - "SELECT 'max(cnt): ', max(cnt) FROM wordcount;\n" - "SELECT 'avg(cnt): ', avg(cnt) FROM wordcount;\n" - "SELECT 'sum(cnt=1):', sum(cnt=1) FROM wordcount;\n" - "SELECT 'top 10: ', group_concat(word, ', ') FROM " - "(SELECT word FROM wordcount ORDER BY cnt DESC, word LIMIT 10);\n" - "SELECT 'checksum: ', checksum(word, cnt) FROM " - "(SELECT word, cnt FROM wordcount ORDER BY word);\n" - "PRAGMA integrity_check;\n", - printResult, 0, 0); + /* In --all mode, so the total time */ + if( iMode==MODE_ALL && showTimer ){ + fprintf(pTimer, "%3d.%03d wordcount --all\n", (int)(totalTime/1000), + (int)(totalTime%1000)); } - + /* Database connection statistics printed after both prepared statements ** have been finalized */ if( showStats ){ sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED, &iCur, &iHiwtr, 0); - printf("-- Lookaside Slots Used: %d (max %d)\n", iCur,iHiwtr); + printf("%s Lookaside Slots Used: %d (max %d)\n", zTag, iCur,iHiwtr); sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT, &iCur, &iHiwtr, 0); - printf("-- Successful lookasides: %d\n", iHiwtr); + printf("%s Successful lookasides: %d\n", zTag, iHiwtr); sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, &iCur,&iHiwtr,0); - printf("-- Lookaside size faults: %d\n", iHiwtr); + printf("%s Lookaside size faults: %d\n", zTag, iHiwtr); sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, &iCur,&iHiwtr,0); - printf("-- Lookaside OOM faults: %d\n", iHiwtr); + printf("%s Lookaside OOM faults: %d\n", zTag, iHiwtr); sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, 0); - printf("-- Pager Heap Usage: %d bytes\n", iCur); + printf("%s Pager Heap Usage: %d bytes\n", zTag, iCur); sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1); - printf("-- Page cache hits: %d\n", iCur); + printf("%s Page cache hits: %d\n", zTag, iCur); sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1); - printf("-- Page cache misses: %d\n", iCur); + printf("%s Page cache misses: %d\n", zTag, iCur); sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1); - printf("-- Page cache writes: %d\n", iCur); + printf("%s Page cache writes: %d\n", zTag, iCur); sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, 0); - printf("-- Schema Heap Usage: %d bytes\n", iCur); + printf("%s Schema Heap Usage: %d bytes\n", zTag, iCur); sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, 0); - printf("-- Statement Heap Usage: %d bytes\n", iCur); + printf("%s Statement Heap Usage: %d bytes\n", zTag, iCur); } sqlite3_close(db); @@ -528,19 +645,15 @@ int main(int argc, char **argv){ ** has closed. Memory usage should be zero at this point. */ if( showStats ){ sqlite3_status(SQLITE_STATUS_MEMORY_USED, &iCur, &iHiwtr, 0); - printf("-- Memory Used (bytes): %d (max %d)\n", iCur,iHiwtr); + printf("%s Memory Used (bytes): %d (max %d)\n", zTag,iCur,iHiwtr); sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &iCur, &iHiwtr, 0); - printf("-- Outstanding Allocations: %d (max %d)\n", iCur,iHiwtr); + printf("%s Outstanding Allocations: %d (max %d)\n",zTag,iCur,iHiwtr); sqlite3_status(SQLITE_STATUS_PAGECACHE_OVERFLOW, &iCur, &iHiwtr, 0); - printf("-- Pcache Overflow Bytes: %d (max %d)\n", iCur,iHiwtr); - sqlite3_status(SQLITE_STATUS_SCRATCH_OVERFLOW, &iCur, &iHiwtr, 0); - printf("-- Scratch Overflow Bytes: %d (max %d)\n", iCur,iHiwtr); + printf("%s Pcache Overflow Bytes: %d (max %d)\n",zTag,iCur,iHiwtr); sqlite3_status(SQLITE_STATUS_MALLOC_SIZE, &iCur, &iHiwtr, 0); - printf("-- Largest Allocation: %d bytes\n",iHiwtr); + printf("%s Largest Allocation: %d bytes\n",zTag,iHiwtr); sqlite3_status(SQLITE_STATUS_PAGECACHE_SIZE, &iCur, &iHiwtr, 0); - printf("-- Largest Pcache Allocation: %d bytes\n",iHiwtr); - sqlite3_status(SQLITE_STATUS_SCRATCH_SIZE, &iCur, &iHiwtr, 0); - printf("-- Largest Scratch Allocation: %d bytes\n", iHiwtr); + printf("%s Largest Pcache Allocation: %d bytes\n",zTag,iHiwtr); } return 0; } diff --git a/test/writecrash.test b/test/writecrash.test new file mode 100644 index 0000000000..aca89aafb8 --- /dev/null +++ b/test/writecrash.test @@ -0,0 +1,68 @@ +# 2009 January 8 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# Test the outcome of a writer crashing within a call to the VFS +# xWrite function. +# + + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix writecrash + +do_not_use_codec + + +if {$tcl_platform(platform) eq "windows"} { + finish_test + return +} + +do_execsql_test 1.0 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b BLOB UNIQUE); + WITH s(i) AS ( + VALUES(1) UNION ALL SELECT i+1 FROM s WHERE i<100 + ) + INSERT INTO t1 SELECT NULL, randomblob(900) FROM s; +} {} + +set bGo 1 +for {set tn 1} {$bGo} {incr tn} { + +db close +sqlite3 db test.db + + do_test 1.$tn.1 { + set res [crash_on_write $tn { + UPDATE t1 SET b = randomblob(899) WHERE (a%3)==0 + }] + set bGo 0 + if {[string match {1 {child killed:*}} $res]} { + set res {0 {}} + set bGo 1 + } + set res + } {0 {}} + +#db close +#sqlite3 db test.db + + do_execsql_test 1.$tn.2 { PRAGMA integrity_check } {ok} + +db close +sqlite3 db test.db + + do_execsql_test 1.$tn.3 { PRAGMA integrity_check } {ok} +} + + + +finish_test diff --git a/test/zeroblob.test b/test/zeroblob.test index 0514644a28..df234eea44 100644 --- a/test/zeroblob.test +++ b/test/zeroblob.test @@ -19,10 +19,7 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl set testprefix zeroblob -ifcapable !incrblob { - finish_test - return -} +# ifcapable !incrblob { finish_test return } test_set_config_pagecache 0 0 @@ -42,12 +39,17 @@ do_test zeroblob-1.1 { execsql { INSERT INTO t1 VALUES(2,3,4,zeroblob(1000000)); } - set ::sqlite3_max_blobsize -} {10} +} {} + +ifcapable incrblob { + do_test zeroblob-1.1.1 { + set ::sqlite3_max_blobsize + } {10} + do_test zeroblob-1.1.2 { + expr {[sqlite3_memory_highwater]<$::memused+35000} + } {1} +} -do_test zeroblob-1.1.1 { - expr {[sqlite3_memory_highwater]<$::memused+35000} -} {1} do_test zeroblob-1.2 { execsql { SELECT length(d) FROM t1 @@ -78,8 +80,12 @@ do_test zeroblob-1.5 { execsql { INSERT INTO t1 VALUES(4,5,zeroblob(10000),zeroblob(10000)); } - set ::sqlite3_max_blobsize -} {11} +} {} +ifcapable incrblob { + do_test zeroblob-1.5.1 { + set ::sqlite3_max_blobsize + } {11} +} do_test zeroblob-1.6 { execsql { SELECT length(c), length(d) FROM t1 @@ -94,8 +100,12 @@ do_test zeroblob-1.7 { execsql { INSERT INTO t1 VALUES(5,zeroblob(10000),NULL,zeroblob(10000)); } - set ::sqlite3_max_blobsize -} {10} +} {} +ifcapable incrblob { + do_test zeroblob-1.7.1 { + set ::sqlite3_max_blobsize + } {10} +} do_test zeroblob-1.8 { execsql { SELECT length(b), length(d) FROM t1 WHERE a=5 @@ -214,12 +224,14 @@ do_test zeroblob-7.2 { do_test zeroblob-7.3 { sqlite3_finalize $::STMT } {SQLITE_OK} -do_test zeroblob-7.4 { - set ::sqlite3_max_blobsize -} {0} -do_test zeroblob-7.5 { - expr {[sqlite3_memory_highwater]<$::memused+10000} -} {1} +ifcapable incrblob { + do_test zeroblob-7.4 { + set ::sqlite3_max_blobsize + } {0} + do_test zeroblob-7.5 { + expr {[sqlite3_memory_highwater]<$::memused+10000} + } {1} +} # Test that MakeRecord can handle a value with some real content # and a zero-blob tail. @@ -316,5 +328,16 @@ do_test 12.5 { sqlite3_finalize $stmt +# 2019-01-25 https://sqlite.org/src/tktview/bb4bdb9f7f654b0bb9f34cfbac +# Zeroblob truncated by an index on expression +# +do_execsql_test 13.100 { + DROP TABLE IF EXISTS t1; + CREATE TABLE t1(a,b,c); + CREATE INDEX t1bbc ON t1(b, b+c); + INSERT INTO t1(a,b,c) VALUES(1,zeroblob(8),3); + SELECT a, quote(b), length(b), c FROM t1; +} {1 X'0000000000000000' 8 3} + test_restore_config_pagecache finish_test diff --git a/test/zeroblobfault.test b/test/zeroblobfault.test new file mode 100644 index 0000000000..0ae5beae01 --- /dev/null +++ b/test/zeroblobfault.test @@ -0,0 +1,28 @@ +# 2021 November 8 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +set testprefix zeroblobfault +set quoted_res [db one { SELECT quote(zeroblob(2000)) }] + +do_faultsim_test 1 -prep { + sqlite3 db test.db +} -body { + execsql { SELECT quote(zeroblob(2000)) } +} -test { + faultsim_test_result [list 0 $::quoted_res] +} + +finish_test diff --git a/test/zerodamage.test b/test/zerodamage.test index dccaba816e..83bae737df 100644 --- a/test/zerodamage.test +++ b/test/zerodamage.test @@ -74,7 +74,7 @@ do_test zerodamage-2.0 { UPDATE t1 SET y=randomblob(50) WHERE x=123; } concat [file_control_powersafe_overwrite db -1] [set ::max_journal_size] -} {0 1 2576} +} [list 0 1 [expr ([atomic_batch_write test.db]==0)*2576]] # Repeat the previous step with zero-damage turned off. This time the # maximum rollback journal size should be much larger. @@ -87,9 +87,9 @@ do_test zerodamage-2.1 { UPDATE t1 SET y=randomblob(50) WHERE x=124; } concat [file_control_powersafe_overwrite db -1] [set ::max_journal_size] -} {0 0 24704} +} [list 0 0 [expr ([atomic_batch_write test.db]==0)*24704]] -ifcapable wal { +if {[wal_is_capable]} { # Run a WAL-mode transaction with POWERSAFE_OVERWRITE on to verify that the # WAL file does not get too big. # @@ -112,6 +112,7 @@ ifcapable wal { db close sqlite3 db file:test.db?psow=FALSE -uri 1 db eval { + PRAGMA synchronous=FULL; UPDATE t1 SET y=randomblob(50) WHERE x=124; } file size test.db-wal diff --git a/test/zipfile.test b/test/zipfile.test new file mode 100644 index 0000000000..9bb35ea5db --- /dev/null +++ b/test/zipfile.test @@ -0,0 +1,911 @@ +# 2017 December 9 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +if {$tcl_version<8.6} { + puts "Requires TCL 8.6 or later" + return +} + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix zipfile + +ifcapable !vtab { + finish_test; return +} +if {[catch {load_static_extension db zipfile} error]} { + puts "Skipping zipfile tests, hit load error: $error" + finish_test; return +} +if {[catch {load_static_extension db fileio} error]} { + puts "Skipping zipfile tests, hit load error: $error" + finish_test; return +} + +proc readfile {f} { + set fd [open $f] + fconfigure $fd -translation binary + set data [read $fd] + close $fd + set data +} + +unset -nocomplain ::UNZIP + +if {[catch {exec unzip} msg]==0 && \ + [regexp -line {^UnZip \d+\.\d+ .*? Info-ZIP\.} $msg]} { + set ::UNZIP unzip + proc fix_stat_mode {name mode} { + if {$::tcl_platform(platform) eq "windows"} { + # + # NOTE: Set or unset the write bits of the file permissions + # based on the read-only attribute because the Win32 + # version of UnZip does this. + # + set writebits 0x12; # 0o22 + set result $mode + if {[file attributes $name -readonly]} { + set result [expr {$result | $writebits}] + } else { + set result [expr {$result & ~$writebits}] + } + return $result + } else { + return $mode + } + } + proc do_unzip {file} { + forcedelete test_unzip + file mkdir test_unzip + exec $::UNZIP -d test_unzip $file + + db func modefix fix_stat_mode + + set res [db eval { + SELECT replace(name,'test_unzip/',''),modefix(name,mode),mtime,data + FROM fsdir('test_unzip') + WHERE name!='test_unzip' + ORDER BY name + }] + set res + } +} + + +# The argument is a blob (not a hex string) containing a zip archive. +# This proc removes the extended timestamp fields from the archive +# and returns the result. +# +proc remove_timestamps {blob} { + set hex [binary encode hex $blob] + set hex [string map {55540500 00000500} $hex] + binary decode hex $hex +} + + +# Argument $file is the name of a zip archive on disk. This function +# executes test cases to check that the results of each of the following +# are the same: +# +# SELECT * FROM zipfile($file) +# SELECT * FROM zipfile( readfile($file) ) +# SELECT * FROM zipfile( +# (SELECT zipfile(name,mode,mtime,data,method) FROM zipfile($file)) +# ) +# +proc do_zipfile_blob_test {tn file} { + + db func r readfile + set q1 {SELECT name,mode,mtime,method,quote(data) FROM zipfile($file)} + set q2 {SELECT name,mode,mtime,method,quote(data) FROM zipfile( r($file) )} + set q3 {SELECT name,mode,mtime,method,quote(data) FROM zipfile( + ( SELECT zipfile(name,mode,mtime,data,method) FROM zipfile($file) ) + )} + + + set r1 [db eval $q1] + set r2 [db eval $q2] + set r3 [db eval $q3] + #puts $r1 + #puts $r2 + #puts $r3 + + uplevel [list do_test $tn.1 [list set {} $r2] $r1] + uplevel [list do_test $tn.2 [list set {} $r3] $r1] +} + +# Argument $file is a zip file on disk. This command runs tests to: +# +# 1. Unpack the archive with unix command [unzip] and compare the +# results to reading the same archive using the zipfile() table +# valued function. +# +# 2. Creates a new archive with the same contents using the zipfile() +# aggregate function as follows: +# +# SELECT writefile('test_unzip.zip', +# ( SELECT zipfile(name,mode,mtime,data,method) FROM zipfile($file) ) +# ); +# +# Then tests that unpacking the new archive using [unzip] produces +# the same results as in (1). +# +proc do_unzip_test {tn file} { + db func sss strip_slash + + db eval { + SELECT writefile('test_unzip.zip', + ( SELECT zipfile(name,mode,mtime,data,method) FROM zipfile($file) ) + ); + } + + set r1 [db eval { + SELECT sss(name),mode,mtime,data FROM zipfile($file) ORDER BY name + }] + set r2 [do_unzip $file] + set r3 [do_unzip test_unzip.zip] + + uplevel [list do_test $tn.1 [list set {} $r2] $r1] + uplevel [list do_test $tn.2 [list set {} $r3] $r1] +} +proc strip_slash {in} { regsub {/$} $in {} } + +proc do_zip_tests {tn file} { + uplevel do_zipfile_blob_test $tn.1 $file + if {[info exists ::UNZIP]} { + uplevel do_unzip_test $tn.2 $file + } +} + +forcedelete test.zip +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE temp.zz USING zipfile('test.zip'); + PRAGMA table_info(zz); +} { + 0 name {} 1 {} 1 + 1 mode {} 0 {} 0 + 2 mtime {} 0 {} 0 + 3 sz {} 0 {} 0 + 4 rawdata {} 0 {} 0 + 5 data {} 0 {} 0 + 6 method {} 0 {} 0 +} + +do_catchsql_test 1.1.0.1 { + INSERT INTO zz(name, mode, mtime, sz, rawdata, method) + VALUES('f.txt', '-rw-r--r--', 1000000000, 5, 'abcde', 0); +} {1 {rawdata must be NULL}} +do_catchsql_test 1.1.0.2 { + INSERT INTO zz(name, mtime, sz, data, method) + VALUES('g.txt', 1000000002, 5, '12345', 0); +} {1 {sz must be NULL}} +do_catchsql_test 1.1.0.3 { + INSERT INTO zz(name, mtime, rawdata, method) + VALUES('g.txt', 1000000002, '12345', 0); +} {1 {rawdata must be NULL}} +do_catchsql_test 1.1.0.4 { + INSERT INTO zz(name, data, method) + VALUES('g.txt', '12345', 7); +} {1 {unknown compression method: 7}} + +do_execsql_test 1.1.1 { + INSERT INTO zz(name, mode, mtime, data, method) + VALUES('f.txt', '-rw-r--r--', 1000000000, 'abcde', 0); +} +do_execsql_test 1.1.2 { + INSERT INTO zz(name, mode, mtime, data, method) + VALUES('g.txt', NULL, 1000000002, '12345', 0); +} + +do_execsql_test 1.2 { + SELECT name, mtime, data FROM zipfile('test.zip') +} { + f.txt 1000000000 abcde + g.txt 1000000002 12345 +} +do_zip_tests 1.2a test.zip + +do_execsql_test 1.3 { + INSERT INTO zz(name, mode, mtime, data) VALUES('h.txt', + '-rw-r--r--', 1000000004, 'aaaaaaaaaabbbbbbbbbb' + ); +} +do_zip_tests 1.3a test.zip + +do_execsql_test 1.4 { + SELECT name, mtime, data, method FROM zipfile('test.zip'); +} { + f.txt 1000000000 abcde 0 + g.txt 1000000002 12345 0 + h.txt 1000000004 aaaaaaaaaabbbbbbbbbb 8 +} + +ifcapable json1 { + do_execsql_test 1.4.1 { + SELECT name, json_extract( zipfile_cds(z) , '$.crc32')!=0 + FROM zipfile('test.zip'); + } { + f.txt 1 + g.txt 1 + h.txt 1 + } +} +do_catchsql_test 1.4.2 { + SELECT zipfile_cds(mode) FROM zipfile('test.zip'); +} {0 {{} {} {}}} + +do_execsql_test 1.5.1 { + BEGIN; + INSERT INTO zz(name, mode, mtime, data, method) + VALUES('i.txt', '-rw-r--r--', 1000000006, 'zxcvb', 0); + SELECT name FROM zz; + COMMIT; +} {f.txt g.txt h.txt i.txt} +do_execsql_test 1.5.2 { + SELECT name FROM zz; +} {f.txt g.txt h.txt i.txt} +do_execsql_test 1.5.3 { + SELECT data FROM zz WHERE name='i.txt'; +} {zxcvb} + +do_execsql_test 1.6.0 { + DELETE FROM zz WHERE name='g.txt'; + SELECT name FROM zz; +} {f.txt h.txt i.txt} + +do_execsql_test 1.6.1 { + SELECT name, mode, mtime, data, method FROM zipfile('test.zip'); +} { + f.txt 33188 1000000000 abcde 0 + h.txt 33188 1000000004 aaaaaaaaaabbbbbbbbbb 8 + i.txt 33188 1000000006 zxcvb 0 +} +do_zip_tests 1.6.1a test.zip + +do_execsql_test 1.6.2 { + UPDATE zz SET mtime=4 WHERE name='i.txt'; + SELECT name, mode, mtime, data, method FROM zipfile('test.zip'); +} { + f.txt 33188 1000000000 abcde 0 + h.txt 33188 1000000004 aaaaaaaaaabbbbbbbbbb 8 + i.txt 33188 4 zxcvb 0 +} + +if {$::tcl_platform(platform) eq "unix"} { + set modes -rw-r--r-x + set perms 33189 +} else { + set modes -rw-r--r--; # no execute bits on Win32 + set perms 33188 +} + +do_execsql_test 1.6.3 { + UPDATE zz SET mode=$modes WHERE name='h.txt'; + SELECT name, mode, mtime, data, method FROM zipfile('test.zip'); +} [string map [list %perms% $perms] { + f.txt 33188 1000000000 abcde 0 + h.txt %perms% 1000000004 aaaaaaaaaabbbbbbbbbb 8 + i.txt 33188 4 zxcvb 0 +}] +do_zip_tests 1.6.3a test.zip + +do_execsql_test 1.6.4 { + UPDATE zz SET name = 'blue.txt' WHERE name='f.txt'; + SELECT name, mode, mtime, data, method FROM zipfile('test.zip'); +} [string map [list %perms% $perms] { + blue.txt 33188 1000000000 abcde 0 + h.txt %perms% 1000000004 aaaaaaaaaabbbbbbbbbb 8 + i.txt 33188 4 zxcvb 0 +}] +do_zip_tests 1.6.4a test.zip + +do_execsql_test 1.6.5 { + UPDATE zz SET data = 'edcba' WHERE name='blue.txt'; + SELECT name, mode, mtime, data, method FROM zipfile('test.zip'); +} [string map [list %perms% $perms] { + blue.txt 33188 1000000000 edcba 0 + h.txt %perms% 1000000004 aaaaaaaaaabbbbbbbbbb 8 + i.txt 33188 4 zxcvb 0 +}] + +do_execsql_test 1.6.6 { + UPDATE zz SET mode=NULL, data = NULL WHERE name='blue.txt'; + SELECT name, mode, mtime, data, method FROM zipfile('test.zip'); +} [string map [list %perms% $perms] { + blue.txt/ 16877 1000000000 {} 0 + h.txt %perms% 1000000004 aaaaaaaaaabbbbbbbbbb 8 + i.txt 33188 4 zxcvb 0 +}] + +do_catchsql_test 1.6.7 { + UPDATE zz SET data=NULL WHERE name='i.txt' +} {1 {zipfile: mode does not match data}} +do_execsql_test 1.6.8 { + SELECT name, mode, mtime, data, method FROM zipfile('test.zip'); +} [string map [list %perms% $perms] { + blue.txt/ 16877 1000000000 {} 0 + h.txt %perms% 1000000004 aaaaaaaaaabbbbbbbbbb 8 + i.txt 33188 4 zxcvb 0 +}] + +do_execsql_test 1.6.9 { + UPDATE zz SET data = '' WHERE name='i.txt'; + SELECT name,mode,mtime,data,method from zipfile('test.zip'); +} [string map [list %perms% $perms] { + blue.txt/ 16877 1000000000 {} 0 + h.txt %perms% 1000000004 aaaaaaaaaabbbbbbbbbb 8 + i.txt 33188 4 {} 0 +}] + +do_execsql_test 1.6.10 { + SELECT a.name, a.data + FROM zz AS a, zz AS b + WHERE a.name=+b.name AND +a.mode=b.mode +} { + blue.txt/ {} + h.txt aaaaaaaaaabbbbbbbbbb + i.txt {} +} + +do_execsql_test 1.6.11 { + SELECT name, data FROM zz WHERE name LIKE '%txt' +} { + h.txt aaaaaaaaaabbbbbbbbbb + i.txt {} +} + +do_execsql_test 1.7 { + DELETE FROM zz; + SELECT * FROM zz; +} {} + +#------------------------------------------------------------------------- +db close +forcedelete test.zip +reset_db +load_static_extension db fileio +load_static_extension db zipfile +do_execsql_test 2.1 { + CREATE VIRTUAL TABLE zzz USING zipfile('test.zip'); + INSERT INTO zzz(name, mode) VALUES('dirname', 'drwxr-xr-x'); + SELECT name, mode, data FROM zzz; +} {dirname/ 16877 {}} +do_execsql_test 2.2 { + INSERT INTO zzz(name, data) VALUES('dirname2', NULL); + INSERT INTO zzz(name, data) VALUES('dirname2/file1.txt', 'abcdefghijklmnop'); + SELECT name, mode, data FROM zzz; +} { + dirname/ 16877 {} + dirname2/ 16877 {} + dirname2/file1.txt 33188 abcdefghijklmnop +} + +do_catchsql_test 2.3 { + UPDATE zzz SET name = 'dirname3' WHERE name = 'dirname/'; +} {0 {}} +do_execsql_test 2.4 { + SELECT name, mode, data FROM zzz; +} { + dirname3/ 16877 {} + dirname2/ 16877 {} + dirname2/file1.txt 33188 abcdefghijklmnop +} +do_zip_tests 2.4a test.zip + +# Check that the [unzip] utility can unpack our archive. +# +if {[info exists ::UNZIP]} { + do_test 2.5.1 { + forcedelete dirname + forcedelete dirname2 + if {$::tcl_platform(platform) eq "unix"} { + set null /dev/null + } else { + set null NUL + } + set rc [catch { exec $::UNZIP test.zip > $null } msg] + list $rc $msg + } {0 {}} + do_test 2.5.2 { file isdir dirname3 } 1 + do_test 2.5.3 { file isdir dirname2 } 1 + do_test 2.5.4 { file isdir dirname2/file1.txt } 0 + do_test 2.5.5 { + set fd [open dirname2/file1.txt] + set data [read $fd] + close $fd + set data + } {abcdefghijklmnop} +} + +#------------------------------------------------------------------------- +reset_db +forcedelete test.zip +load_static_extension db zipfile +load_static_extension db fileio + +do_execsql_test 3.0 { + CREATE VIRTUAL TABLE temp.x1 USING zipfile('test.zip'); + INSERT INTO x1(name, data) VALUES('dir1/', NULL); + INSERT INTO x1(name, data) VALUES('file1', '1234'); + INSERT INTO x1(name, data) VALUES('dir1/file2', '5678'); +} +foreach {tn fname} { + 1 dir1 + 2 file1 + 3 dir1/file2 +} { + do_catchsql_test 3.1.$tn.0 { + INSERT INTO x1(name, data) VALUES($fname, NULL); + } [list 1 "duplicate name: \"$fname/\""] + do_catchsql_test 3.1.$tn.1 { + INSERT INTO x1(name, data) VALUES($fname || '/', NULL); + } [list 1 "duplicate name: \"$fname/\""] + do_catchsql_test 3.1.$tn.2 { + INSERT INTO x1(name, data) VALUES($fname, 'abcd'); + } [list 1 "duplicate name: \"$fname\""] +} + +do_catchsql_test 3.2 { + SELECT rowid FROM x1 +} {1 {no such column: rowid}} + +#------------------------------------------------------------------------- +# Test some error conditions. +# +do_catchsql_test 4.1 { + CREATE VIRTUAL TABLE yyy USING zipfile(); +} {1 {zipfile constructor requires one argument}} +do_catchsql_test 4.2 { + CREATE VIRTUAL TABLE yyy USING zipfile('test.zip', 'test.zip'); +} {1 {zipfile constructor requires one argument}} + +do_catchsql_test 4.3 { + SELECT * FROM zipfile() +} {1 {zipfile() function requires an argument}} + +do_catchsql_test 4.4 { + SELECT * FROM zipfile('/path/that/does/not/exist') +} {1 {cannot open file: /path/that/does/not/exist}} + +foreach {tn mode} { + 1 abcd + 2 brwxrwxrwx + 3 lrwxrrxrwx +} { + do_catchsql_test 4.5.$tn { + WITH m(m) AS ( SELECT $mode) + SELECT zipfile('a.txt', m, 1000, 'xyz') FROM m + } [list 1 "zipfile: parse error in mode: $mode"] +} + +do_catchsql_test 4.6 { + WITH c(name,data) AS ( SELECT 'a.txt', 'abc') + SELECT zipfile(name) FROM c +} {1 {wrong number of arguments to function zipfile()}} + +do_catchsql_test 4.7 { + WITH c(name,data) AS ( + SELECT 'a.txt', 'abc' UNION ALL + SELECT NULL, 'def' + ) + SELECT zipfile(name,data) FROM c +} {1 {first argument to zipfile() must be non-NULL}} + +do_catchsql_test 4.8 { + WITH c(name,data,method) AS ( + SELECT 'a.txt', 'abc', 0 + UNION SELECT 'b.txt', 'def', 8 + UNION SELECT 'c.txt', 'ghi', 16 + ) + SELECT zipfile(name,NULL,NULL,data,method) FROM c +} {1 {illegal method value: 16}} + +do_catchsql_test 4.9 { + WITH c(name,data) AS ( + SELECT 'a.txt', 'abc' + UNION SELECT 'b.txt', 'def' + UNION SELECT 'c.txt/', 'ghi' + ) + SELECT zipfile(name,NULL,NULL,data) FROM c +} {1 {non-directory name must not end with /}} + +#-------------------------------------------------------------------------- + +db func rt remove_timestamps +do_execsql_test 5.0 { + WITH c(name,mtime,data) AS ( + SELECT 'a.txt', 946684800, 'abc' + ) + SELECT name,mtime,data FROM zipfile( + ( SELECT rt( zipfile(name,NULL,mtime,data,NULL) ) FROM c ) + ) +} { + a.txt 946684800 abc +} + +if {[info exists ::UNZIP]} { +ifcapable datetime { + forcedelete test1.zip test2.zip + do_test 6.0 { + execsql { + WITH c(name,mtime,data) AS ( + SELECT 'a.txt', 946684800, 'abc' UNION ALL + SELECT 'b.txt', 1000000000, 'abc' UNION ALL + SELECT 'c.txt', 1111111000, 'abc' + ) + SELECT writefile('test1.zip', rt( zipfile(name, NULL, mtime, data) ) ), + writefile('test2.zip', ( zipfile(name, NULL, mtime, data) ) ) + FROM c; + } + forcedelete test_unzip + file mkdir test_unzip + exec $::UNZIP -d test_unzip test1.zip + + db eval { + SELECT name, strftime('%s', mtime, 'unixepoch', 'localtime') + FROM fsdir('test_unzip') WHERE name!='test_unzip' + ORDER BY name + } + } [list {*}{ + test_unzip/a.txt 946684800 + test_unzip/b.txt 1000000000 + test_unzip/c.txt 1111111000 + }] + + # fsdir() issue reported on the mailing list on 2018-03-14 by Jack Thaw. + do_test 6.0b { + db eval { + SELECT sum(name LIKE '%/a.txt') + FROM (VALUES(1),(2),(3)) CROSS JOIN fsdir('test_unzip') + } + } {3} + + do_execsql_test 6.1 { + SELECT name, mtime, data FROM zipfile('test1.zip') + } { + a.txt 946684800 abc + b.txt 1000000000 abc + c.txt 1111111000 abc + } + + do_test 6.2 { + forcedelete test_unzip + file mkdir test_unzip + exec $::UNZIP -d test_unzip test2.zip + + db eval { + SELECT name, mtime + FROM fsdir('test_unzip') WHERE name!='test_unzip' + ORDER BY name + } + } [list {*}{ + test_unzip/a.txt 946684800 + test_unzip/b.txt 1000000000 + test_unzip/c.txt 1111111000 + }] + + do_execsql_test 6.3 { + SELECT name, mtime, sz, rawdata, data FROM zipfile('test2.zip') + } { + a.txt 946684800 3 abc abc + b.txt 1000000000 3 abc abc + c.txt 1111111000 3 abc abc + } +} +} + +#------------------------------------------------------------------------- +# Force an IO error by truncating the zip archive to zero bytes in size +# while it is being read. +forcedelete test.zip +do_test 7.0 { + execsql { + WITH c(name,data) AS ( + SELECT '1', randomblob(1000000) UNION ALL + SELECT '2', randomblob(1000000) UNION ALL + SELECT '3', randomblob(1000000) + ) + SELECT writefile('test.zip', zipfile(name, data) ) FROM c; + } + + list [catch { + db eval { SELECT name, data FROM zipfile('test.zip') } { + if {$name==2} { close [open test.zip w+] } + } + } msg] $msg +} {1 {error in fread()}} + +forcedelete test.zip +do_execsql_test 8.0.1 { + CREATE VIRTUAL TABLE zz USING zipfile('test.zip'); + BEGIN; + INSERT INTO zz(name, data) VALUES('a.txt', '1'); + INSERT INTO zz(name, data) VALUES('b.txt', '2'); + INSERT INTO zz(name, data) VALUES('c.txt', '1'); + INSERT INTO zz(name, data) VALUES('d.txt', '2'); + SELECT name, data FROM zz; +} { + a.txt 1 b.txt 2 c.txt 1 d.txt 2 +} +do_test 8.0.2 { + db eval { SELECT name, data FROM zz } { + if { $data=="2" } { db eval { DELETE FROM zz WHERE name=$name } } + } + execsql { SELECT name, data FROM zz } +} {a.txt 1 c.txt 1} +do_test 8.0.3 { + db eval { SELECT name, data FROM zz } { + db eval { DELETE FROM zz WHERE name=$name } + } + execsql { SELECT name, data FROM zz } +} {} +execsql COMMIT + +catch { forcedelete test_unzip } +catch { file mkdir test_unzip } +do_execsql_test 8.1.1 { + CREATE VIRTUAL TABLE nogood USING zipfile('test_unzip'); +} +do_catchsql_test 8.1.2 { + INSERT INTO nogood(name, data) VALUES('abc', 'def'); +} {1 {zipfile: failed to open file test_unzip for writing}} + +do_execsql_test 8.2.1 { + DROP TABLE nogood; + BEGIN; + CREATE VIRTUAL TABLE nogood USING zipfile('test_unzip'); +} +do_catchsql_test 8.2.2 { + INSERT INTO nogood(name, data) VALUES('abc', 'def'); +} {1 {zipfile: failed to open file test_unzip for writing}} +do_execsql_test 8.2.3 { + COMMIT; +} + +forcedelete test.zip +do_execsql_test 8.3.1 { + BEGIN; + CREATE VIRTUAL TABLE ok USING zipfile('test.zip'); + INSERT INTO ok(name, data) VALUES ('sqlite3', 'elf'); + COMMIT; +} + +#------------------------------------------------------------------------- +# Test that the zipfile aggregate correctly adds and removes "/" from +# the ends of directory file names. +do_execsql_test 9.0 { + WITH src(nm) AS ( + VALUES('dir1') UNION ALL + VALUES('dir2/') UNION ALL + VALUES('dir3//') UNION ALL + VALUES('dir4///') UNION ALL + VALUES('/') + ) + SELECT name FROM zipfile((SELECT zipfile(nm, NULL) FROM src)) +} {dir1/ dir2/ dir3/ dir4/ /} + +#------------------------------------------------------------------------- +# INSERT OR REPLACE and INSERT OR IGNORE +# +catch {db close} +forcedelete test.zip test.db +sqlite3 db :memory: +load_static_extension db zipfile +load_static_extension db fileio + +do_execsql_test 10.0 { + CREATE VIRTUAL TABLE z USING zipfile('test.zip'); +} {} +do_catchsql_test 10.1 { + INSERT INTO z(name,data) VALUES('a0','one'),('a0','two'); +} {1 {duplicate name: "a0"}} +do_execsql_test 10.2 { + SELECT name, data FROM z; +} {a0 one} +do_execsql_test 10.3 { + REPLACE INTO z(name,data) VALUES('a0','three'),('a0','four'); +} {} +do_execsql_test 10.4 { + SELECT name, data FROM z; +} {a0 four} +do_execsql_test 10.5 { + INSERT OR IGNORE INTO z(name,data) VALUES('a0','five'),('a0','six'); +} {} +do_execsql_test 10.6 { + SELECT name, data FROM z; +} {a0 four} + +do_execsql_test 11.1 { + DELETE FROM z; +} {} +do_execsql_test 11.2 { + SELECT name, data FROM z; +} {} +do_execsql_test 11.3 { + INSERT INTO z (name,data) VALUES ('b0','one'); + SELECT name, data FROM z; +} {b0 one} +do_execsql_test 11.4 { + UPDATE z SET name = 'b1' WHERE name = 'b0'; + SELECT name, data FROM z; +} {b1 one} +do_execsql_test 11.5 { + INSERT INTO z (name,data) VALUES ('b0','one'); + SELECT name, data FROM z ORDER BY name; +} {b0 one b1 one} +do_catchsql_test 11.6 { + UPDATE z SET name = 'b1' WHERE name = 'b0'; +} {1 {duplicate name: "b1"}} +do_execsql_test 11.7 { + UPDATE z SET data = 'two' WHERE name = 'b0'; + SELECT name, data FROM z ORDER BY name; +} {b0 two b1 one} +do_catchsql_test 11.8 { + UPDATE z SET name = 'b1'; +} {1 {duplicate name: "b1"}} +do_catchsql_test 11.9 { + UPDATE z SET name = 'b2'; +} {1 {duplicate name: "b2"}} +do_execsql_test 11.10 { + UPDATE z SET name = name; + SELECT name, data FROM z ORDER BY name; +} {b0 two b2 one} +do_execsql_test 11.11 { + UPDATE z SET name = name || 'suffix'; + SELECT name, data FROM z ORDER BY name; +} {b0suffix two b2suffix one} + + +if {$tcl_platform(platform) ne "windows"} { + do_test 12.0 { + catch { file delete -force subdir } + foreach {path sz} { + subdir/x1.txt 143 + subdir/x2.txt 153 + } { + set dir [file dirname $path] + catch { file mkdir $dir } + set fd [open $path w] + puts -nonewline $fd [string repeat 1 $sz] + close $fd + } + } {} + + do_execsql_test 12.1 { + SELECT name FROM fsdir('subdir') ORDER BY 1; + } {subdir subdir/x1.txt subdir/x2.txt} + + do_execsql_test 12.2 { + CREATE TABLE d AS SELECT 'subdir' d; + CREATE TABLE x AS SELECT 1 x; + } + + do_execsql_test 12.4 { + SELECT name FROM d JOIN x JOIN fsdir(d) ORDER BY 1; + } {subdir subdir/x1.txt subdir/x2.txt} + + do_execsql_test 12.5 { + SELECT name FROM d JOIN x JOIN fsdir('.', d) ORDER BY 1; + } {. ./x1.txt ./x2.txt} +} + +# 2019-12-18 Yongheng and Rui fuzzer +# +do_execsql_test 13.10 { + DROP TABLE IF EXISTS t0; + DROP TABLE IF EXISTS t1; + CREATE TABLE t0(a,b,c,d,e,f,g); + REPLACE INTO t0(c,b,f) VALUES(10,10,10); + CREATE VIRTUAL TABLE t1 USING zipfile('h.zip'); + REPLACE INTO t1 SELECT * FROM t0; + SELECT quote(name),quote(mode),quote(mtime),quote(sz),quote(rawdata), + quote(data),quote(method) FROM t1; +} {'' 10 10 2 X'3130' X'3130' 0} + +# 2019-12-23 Yongheng and Rui fuzzer +# Run using valgrind to see the problem. +# +do_execsql_test 14.10 { + DROP TABLE t1; + CREATE TABLE t1(x char); + INSERT INTO t1(x) VALUES('1'); + INSERT INTO t1(x) SELECT zipfile(x, 'xyz') FROM t1; + INSERT INTO t1(x) SELECT zipfile(x, 'uvw') FROM t1; + SELECT count(*) FROM t1; + PRAGMA integrity_check; +} {3 ok} + +# 2019-12-26 More problems in zipfile from the Yongheng and Rui fuzzer +# +do_execsql_test 15.10 { + DROP TABLE IF EXISTS t1; + CREATE VIRTUAL TABLE t1 USING zipfile(null); + REPLACE INTO t1 VALUES(null,null,0,null,null,null,null); +} {} +do_execsql_test 15.20 { + DROP TABLE IF EXISTS t2; + CREATE VIRTUAL TABLE t2 USING zipfile(null); + REPLACE INTO t2 values(null,null,null,null,null,10,null); +} {} + +# 2020-01-02 Yongheng fuzzer discovery +# +do_catchsql_test 16.10 { + DELETE FROM zipfile; +} {1 {zipfile: missing filename}} +do_catchsql_test 16.20 { + REPLACE INTO zipfile VALUES(null,null,null,null,null,123,null); +} {1 {zipfile: missing filename}} + +# 2021-04-22 forum https://sqlite.org/forum/forumpost/d82289d69f +do_execsql_test 17.1 { + WITH vlist(x) AS ( + VALUES(9223372036854775807), + (-9223372036854775808), + (9223372036854775806), + (-9223372036854775807) + ) + SELECT DISTINCT typeof(zipfile(0,0,x,0)) FROM vlist; +} {blob} + +# 2023-01-04 +# https://sqlite.org/forum/forumpost/d1c96a9032e564f8 +# Call to fopen() with a NULL filename. +# +do_catchsql_test 18.1 { + SELECT * FROM zipfile(NULL); +} {1 {cannot open file: }} + +# 2023-05-03 https://sqlite.org/forum/info/f03f1e4c5a5c9959 +# +do_test 19.1 { + sqlite3 db :memory: + load_static_extension db zipfile + forcedelete zipfile19.zip + db eval { + CREATE VIRTUAL TABLE t1 USING zipfile('zipfile19.zip'); + INSERT INTO t1 DEFAULT VALUES; + } + db close + sqlite3 db :memory: + load_static_extension db zipfile + db eval { + CREATE VIRTUAL TABLE v0 USING zipfile('zipfile19.zip'); + SAVEPOINT y; + DELETE FROM v0 WHERE 9; + INSERT INTO v0 DEFAULT VALUES; + } +} {} +db close +forcedelete zipfile19.zip +sqlite3 db :memory: +load_static_extension db zipfile + +#------------------------------------------------------------------------- +do_catchsql_test 20.0 { + SELECT * FROM zipfile(X'504b050600000000010001004000000000a3e1110000'); +} {1 {zip archive is corrupt}} + +do_catchsql_test 20.1 { + SELECT * FROM zipfile(unhex(' +504b0304140000080000a60d3e5bd42728f602000000020000000500090068 +2e74787455540500012836db682120504b01021e03140000080000a60d3e5b +d42728f602000000020000000500ffff0000000000000000a4810000000068 +2e74787455540500012836db68504b050600000000010001003c0000002e00 +00000000',char(0x0a,0x0d))); +} {1 {zip archive is corrupt}} + +# https://sqlite.org/forum/forumpost/2025-12-30T23:57:19z +do_catchsql_test 20.2 { + SELECT * FROM zipfile(unhex('504b0304140000000000000000008b9ed9d30100000001000000010000007841504b01021e03140000000000000000008b9ed9d3010000000100000001001e000000000000000000a4810000000078504b050600000000010001002f000000200000000000')); +} {1 {zip archive is corrupt}} +finish_test diff --git a/test/zipfile2.test b/test/zipfile2.test new file mode 100644 index 0000000000..8ee90d310b --- /dev/null +++ b/test/zipfile2.test @@ -0,0 +1,305 @@ +# 2018 January 30 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +if {$tcl_version<8.6} { + puts "Requires TCL 8.6 or later" + return +} + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix zipfile2 + +ifcapable !vtab { + finish_test; return +} +if {[catch {load_static_extension db zipfile} error]} { + puts "Skipping zipfile2 tests, hit load error: $error" + finish_test; return +} + +proc blobliteral {str} { + set concat [string map {" " "" "\n" ""} $str] + return "X'$concat'" +} + +proc blob {str} { + binary decode hex $str +} + +proc findall {needle haystack} { + set L [list] + set start 0 + while { [set idx [string first $needle $haystack $start]]>=0 } { + lappend L $idx + set start [expr $idx+1] + } + set L +} + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE aaa USING zipfile('testzip'); + CREATE VIRTUAL TABLE bbb USING zipfile("testzip"); + CREATE VIRTUAL TABLE ccc USING zipfile(`testzip`); + CREATE VIRTUAL TABLE ddd USING zipfile([testzip]); + CREATE VIRTUAL TABLE eee USING zipfile(testzip); + CREATE VIRTUAL TABLE fff USING zipfile('test''zip'); +} + +do_test 2.0 { + forcedelete testdir + file mkdir testdir + execsql { CREATE VIRTUAL TABLE hhh USING zipfile('testdir') } + lindex [catchsql { + SELECT * FROM hhh; + INSERT INTO hhh(name, data) VALUES('1.txt', 'file data'); + }] 0 +} 1 + + +set archive { + 504B0304140000080000D4A52BEC09F3B6E0110000001100000005000900612E + 747874555405000140420F00636F6E74656E7473206F6620612E747874504B03 + 04140000080000D4A52BECD98916A7110000001100000005000900622E747874 + 555405000140420F00636F6E74656E7473206F6620622E747874504B01021E03 + 140000080000D4A52BEC09F3B6E0110000001100000005000900000000000000 + 0000A48100000000612E747874555405000140420F00504B01021E0314000008 + 0000D4A52BECD98916A71100000011000000050009000000000000000000A481 + 3D000000622E747874555405000140420F00504B050600000000020002007800 + 00007A0000000000 +} + +if 0 { + # This test is broken - the archive generated is slightly different + # depending on the zlib version used. + do_execsql_test 3.1 { + WITH contents(name,mtime,data) AS ( + VALUES('a.txt', 1000000, 'contents of a.txt') UNION ALL + VALUES('b.txt', 1000000, 'contents of b.txt') + ) SELECT quote( zipfile(name,NULL,mtime,data) ) FROM contents; + } [blobliteral $archive] +} + + +set blob [blob $archive] +do_execsql_test 3.2 { + SELECT name,mtime,data FROM zipfile($blob) +} { + a.txt 1000000 {contents of a.txt} + b.txt 1000000 {contents of b.txt} +} + +# Corrupt each of the 0x50 0x4B (ascii "PK") headers in the file +# Test that in each case this causes an error. +# +set L [findall 504B $archive] +for {set i 0} {$i < [llength $L]} {incr i} { + set idx [lindex $L $i] + set a [string replace $archive $idx [expr $idx+3] 0000] + set blob [blob $a] + do_catchsql_test 3.3.$i { + SELECT name,mtime,data FROM zipfile($blob) + } {/1 .*/} +} + +# Change the "extra info id" for all extended-timestamp fields. +set L [findall 5554 $archive] +for {set i 0} {$i < [llength $L]} {incr i} { + set idx [lindex $L $i] + set a [string replace $archive $idx [expr $idx+3] 1234] + set blob [blob $a] + do_execsql_test 3.4.$i { + SELECT name,data FROM zipfile($blob) + } { + a.txt {contents of a.txt} + b.txt {contents of b.txt} + } +} + +for {set i 0} {$i < [llength $L]} {incr i} { + set idx [lindex $L $i] + set a [string replace $archive [expr $idx+8] [expr $idx+9] 00] + set blob [blob $a] + do_execsql_test 3.5.$i { + SELECT name,data FROM zipfile($blob) + } { + a.txt {contents of a.txt} + b.txt {contents of b.txt} + } +} + +# set blob [db one { +# WITH contents(name,mtime,data) AS ( +# VALUES('a.txt', 1000000, 'aaaaaaaaaaaaaaaaaaaaaaa') +# ) SELECT quote( zipfile(name,NULL,mtime,data) ) FROM contents; +# }] +# set blob [string range $blob 2 end] +# set blob [string range $blob 0 end-1] +# while {[string length $blob]>0} { +# puts [string range $blob 0 63] +# set blob [string range $blob 64 end] +# } +# exit + +set archive2 { + 504B0304140000080800D4A52BEC08F54C6E050000001700000005000900612E + 747874555405000140420F004B4CC40A00504B01021E03140000080800D4A52B + EC08F54C6E0500000017000000050009000000000000000000A4810000000061 + 2E747874555405000140420F00504B050600000000010001003C000000310000 + 000000 +} +set blob [blob $archive2] +do_execsql_test 4.0 { + SELECT name,mtime,data,method FROM zipfile($blob) +} { + a.txt 1000000 aaaaaaaaaaaaaaaaaaaaaaa 8 +} + +set L [findall 17000000 $archive2] +set a $archive2 +foreach i $L { set a [string replace $a $i [expr $i+7] 16000000] } +set blob [blob $a] +do_catchsql_test 4.1 { + SELECT name,mtime,data,method FROM zipfile($blob) +} {1 {inflate() failed (0)}} + +# Check the response to an unknown compression method (set data to NULL). +set blob [blob [string map {0800 0900} $archive2]] +do_execsql_test 4.2 { + SELECT name,mtime,data IS NULL,method FROM zipfile($blob) +} {a.txt 1000000 1 9} + +# Corrupt the EOCDS signature bytes in various ways. +foreach {tn sub} { + 1 {504B0500} + 2 {504B0006} + 3 {50000506} + 4 {004B0506} +} { + set blob [blob [string map [list 504B0506 $sub] $archive2]] + do_catchsql_test 4.3.$tn { + SELECT * FROM zipfile($blob) + } {1 {cannot find end of central directory record}} +} + +#------------------------------------------------------------------------- +# Test that a zero-length file with a '/' at the end is treated as +# a directory (data IS NULL). Even if the mode doesn't indicate +# that it is a directory. + +do_test 5.0 { + set blob [db one { + WITH c(n, d) AS ( + SELECT 'notadir', '' + ) + SELECT zipfile(n, d) FROM c + }] + + set hex [binary encode hex $blob] + set hex [string map {6e6f7461646972 6e6f746164692f} $hex] + set blob2 [binary decode hex $hex] + + execsql { SELECT name, data IS NULL FROM zipfile($blob2) } +} {notadi/ 1} + +#------------------------------------------------------------------------- +# Test that duplicate entries may not be created using UPDATE +# statements. +# +forcedelete test.zip +do_execsql_test 6.0 { + CREATE VIRTUAL TABLE temp.zip USING zipfile('test.zip'); + INSERT INTO temp.zip (name,data) VALUES ('test1','test'); + INSERT INTO temp.zip (name,data) VALUES ('test2','test'); +} +do_catchsql_test 6.1 { + UPDATE temp.zip SET name='test1' WHERE name='test2' +} {1 {duplicate name: "test1"}} + +forcedelete test.zip +do_catchsql_test 6.2 { + DROP TABLE zip; + CREATE VIRTUAL TABLE temp.zip USING zipfile('test.zip'); + INSERT INTO temp.zip (name,data) VALUES ('test','test'); + UPDATE temp.zip set name=name||'new' where name='test'; + INSERT INTO temp.zip (name,data) VALUES ('test','test'); + UPDATE temp.zip set name=name||'new' where name='test'; +} {1 {duplicate name: "testnew"}} + +forcedelete test.zip +do_execsql_test 6.3 { + INSERT INTO temp.zip (name,data) VALUES ('test1','test'); + INSERT INTO temp.zip (name,data) VALUES ('test2','test'); + UPDATE OR REPLACE zip SET name='test2' WHERE name='test1'; + SELECT name FROM zip; +} {test2} + +#------------------------------------------------------------------------- +# Test a crafted corrupt file. +# +proc make_corrupt_file {fname} { + set central_dir_offset 1000 + set lfh_offset 200 + set nFile 60000 + set nExtra 60000 + set nComment 0 + + # Build local file header at lfh_offset + set lfh "" + append lfh [binary format isssssiiiss 0x04034b50 20 0 0 0 0 0 0 0 1 0] + append lfh "A" + + # Build central directory structure (CDS) at central_dir_offset + set cds "" + append cds [binary format issssssiiisssssii 0x02014b50 0 20 0 0 0 0 0 0 0 $nFile $nExtra $nComment 0 0 0 $lfh_offset] + + # Payload following CDS: filename + extra + comment + set payload "" + append payload [string repeat "B" $nFile] + append payload [string repeat "C" $nExtra] + + # EOCD at end + set cdsize [expr {[string length $cds] + [string length $payload]}] + set eocd "" + append eocd [binary format issssiis 0x06054b50 0 0 1 1 $cdsize $central_dir_offset 0] + + # Assemble file + set buf [string repeat "X" $lfh_offset] + append buf $lfh + + set buflen [string length $buf] + if {$central_dir_offset > $buflen} { + append buf [string repeat "\x00" [expr {$central_dir_offset - $buflen}]] + } + + append buf $cds + append buf $payload + append buf $eocd + + # Write to file + set f [open $fname wb] + fconfigure $f -translation binary + puts -nonewline $f $buf + close $f +} + +forcedelete test.zip +make_corrupt_file test.zip +do_execsql_test 7.0 { + DROP TABLE IF EXISTS t1; + CREATE VIRTUAL TABLE t1 USING zipfile('test.zip'); +} +do_execsql_test 7.1 { + SELECT length(name) FROM t1; +} {60000} + +finish_test diff --git a/test/zipfilefault.test b/test/zipfilefault.test new file mode 100644 index 0000000000..f5e2cd5cb6 --- /dev/null +++ b/test/zipfilefault.test @@ -0,0 +1,166 @@ +# 2018 January 30 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/malloc_common.tcl +set testprefix zipfilefault + +ifcapable !vtab { + finish_test; return +} +if {[catch {load_static_extension db zipfile} error]} { + puts "Skipping zipfile2 tests, hit load error: $error" + finish_test; return +} + +faultsim_save_and_close +do_faultsim_test 1 -prep { + faultsim_restore_and_reopen + load_static_extension db zipfile + execsql { DROP TABLE IF EXISTS aaa } +} -body { + execsql { CREATE VIRTUAL TABLE aaa USING zipfile('test.zip') } +} -test { + faultsim_test_result {0 {}} +} + +forcedelete test.zip +sqlite3 db test.db +load_static_extension db zipfile +do_execsql_test 2.0 { + CREATE VIRTUAL TABLE setup USING zipfile('test.zip'); + INSERT INTO setup(name, data) VALUES('a.txt', '1234567890'); +} + +do_faultsim_test 2.1 -faults oom* -body { + execsql { SELECT name,data FROM zipfile('test.zip') } +} -test { + faultsim_test_result {0 {a.txt 1234567890}} +} +ifcapable json1 { + do_faultsim_test 2.2 -faults oom* -body { + execsql { + SELECT json_extract( zipfile_cds(z), '$.version-made-by' ) + FROM zipfile('test.zip') + } + } -test { + faultsim_test_result {0 798} + } +} + +forcedelete test.zip +reset_db +load_static_extension db zipfile +do_execsql_test 3.0 { + CREATE VIRTUAL TABLE setup USING zipfile('test.zip'); + INSERT INTO setup(name, data) VALUES('a.txt', 'aaaaaaaaaaaaaaaaaaaaaaaaaaaa'); +} + +do_faultsim_test 3 -faults oom* -body { + execsql { SELECT name,data FROM zipfile('test.zip') } +} -test { + faultsim_test_result {0 {a.txt aaaaaaaaaaaaaaaaaaaaaaaaaaaa}} +} + +do_faultsim_test 4 -faults oom* -body { + execsql { + WITH c(n, d) AS ( + SELECT 1, 'aaaaaaaaaaabbbbbbbbbbaaaaaaaaaabbbbbbbbbb' + ) + SELECT name, data FROM zipfile( + (SELECT zipfile(n, d) FROM c) + ); + } +} -test { + faultsim_test_result {0 {1 aaaaaaaaaaabbbbbbbbbbaaaaaaaaaabbbbbbbbbb}} +} + +reset_db +sqlite3_db_config_lookaside db 0 0 0 +load_static_extension db zipfile + +do_execsql_test 5.0 { + CREATE VIRTUAL TABLE setup USING zipfile('test.zip') +} + +do_faultsim_test 5.1 -faults oom* -prep { + forcedelete test.zip +} -body { + execsql { + INSERT INTO setup(name, data) + VALUES('a.txt', 'aaaaaaaaaaaaaaaaaaaaaaaaaaaa'); + } +} -test { + faultsim_test_result {0 {}} +} + +do_faultsim_test 5.2 -faults oom* -prep { + forcedelete test.zip +} -body { + execsql { + INSERT INTO setup(name, data) VALUES('dir', NULL) + } +} -test { + faultsim_test_result {0 {}} +} + +do_faultsim_test 5.3 -faults oom* -prep { + forcedelete test.zip + execsql { + DROP TABLE IF EXISTS setup; + BEGIN; + CREATE VIRTUAL TABLE setup USING zipfile('test.zip') + } +} -body { + execsql { + INSERT INTO setup(name, data) VALUES('dir', NULL) + } +} -test { + catchsql { COMMIT } + faultsim_test_result {0 {}} +} + +do_faultsim_test 6.1 -faults oom* -body { + execsql { + WITH c(n, d) AS ( + VALUES('a.txt', '1234567890') UNION ALL + VALUES('dir', NULL) + ) + SELECT zipfile(n, d) IS NULL FROM c; + } +} -test { + faultsim_test_result {0 0} +} + +set big [string repeat 0123456789 1000] +do_faultsim_test 6.2 -faults oom* -body { + execsql { + WITH c(n, d) AS ( + VALUES('a.txt', $big) + ) + SELECT zipfile(n, NULL, NULL, d, 0) IS NULL FROM c; + } +} -test { + faultsim_test_result {0 0} +} + +do_faultsim_test 7.0 -faults oom* -prep { + catch { db close } + sqlite3 db "" +} -body { + load_static_extension db zipfile +} -test { +} + + +finish_test diff --git a/tool/GetFile.cs b/tool/GetFile.cs index 56601f3e85..1784a79263 100644 --- a/tool/GetFile.cs +++ b/tool/GetFile.cs @@ -167,7 +167,8 @@ bool usage string fileName = Path.GetFileName( Process.GetCurrentProcess().MainModule.FileName); - Console.WriteLine(String.Format("usage: {0} <uri>", fileName)); + Console.WriteLine(String.Format( + "usage: {0} <uri> [fileName]", fileName)); } /////////////////////////////////////////////////////////////////////// @@ -336,7 +337,7 @@ string[] args return (int)ExitCode.MissingArgs; } - if (args.Length != 1) + if ((args.Length < 1) || (args.Length > 2)) { Error(null, true); return (int)ExitCode.WrongNumArgs; @@ -355,15 +356,26 @@ string[] args } // - // NOTE: Attempt to extract the file name portion of the URI we - // just created. + // NOTE: If a file name was specified on the command line, try to + // use it (without its directory name); otherwise, fallback + // to using the file name portion of the URI. // - string fileName = GetFileName(uri); + string fileName = (args.Length == 2) ? + Path.GetFileName(args[1]) : null; - if (fileName == null) + if (String.IsNullOrEmpty(fileName)) { - Error("Could not extract the file name from the URI.", false); - return (int)ExitCode.BadFileName; + // + // NOTE: Attempt to extract the file name portion of the URI + // we just created. + // + fileName = GetFileName(uri); + + if (fileName == null) + { + Error("Could not extract file name from URI.", false); + return (int)ExitCode.BadFileName; + } } // @@ -381,6 +393,15 @@ string[] args try { + // + // HACK: For use of the TLS 1.2 security protocol because some + // web servers fail without it. In order to support the + // .NET Framework 2.0+ at compilation time, must use its + // integer constant here. + // + ServicePointManager.SecurityProtocol = + (SecurityProtocolType)0xC00; + using (WebClient webClient = new WebClient()) { // diff --git a/tool/GetTclKit.bat b/tool/GetTclKit.bat index 84235cf97a..cd295a0d00 100644 --- a/tool/GetTclKit.bat +++ b/tool/GetTclKit.bat @@ -13,6 +13,8 @@ REM SET __ECHO2=ECHO REM SET __ECHO3=ECHO IF NOT DEFINED _AECHO (SET _AECHO=REM) IF NOT DEFINED _CECHO (SET _CECHO=REM) +IF NOT DEFINED _CECHO2 (SET _CECHO2=REM) +IF NOT DEFINED _CECHO3 (SET _CECHO3=REM) IF NOT DEFINED _VECHO (SET _VECHO=REM) SET OVERWRITE=^> @@ -29,6 +31,8 @@ IF DEFINED PROCESSOR ( GOTO usage ) +SET PROCESSOR=%PROCESSOR:AMD64=x64% + %_VECHO% Processor = '%PROCESSOR%' SET DUMMY2=%2 @@ -37,10 +41,11 @@ IF DEFINED DUMMY2 ( GOTO usage ) -SET ROOT=%~dp0\.. -SET ROOT=%ROOT:\\=\% +IF NOT DEFINED ENVDIR ( + SET ENVDIR=%CD% +) -%_VECHO% Root = '%ROOT%' +%_VECHO% EnvDir = '%ENVDIR%' SET TOOLS=%~dp0 SET TOOLS=%TOOLS:~0,-1% @@ -62,21 +67,43 @@ IF NOT DEFINED TEMP ( %_VECHO% Temp = '%TEMP%' IF NOT DEFINED TCLKIT_URI ( - SET TCLKIT_URI=https://tclsh.com/ + SET TCLKIT_URI=https://urn.to/r/tclsh/ ) %_VECHO% TclKitUri = '%TCLKIT_URI%' +IF NOT DEFINED TCLKIT_PATCHLEVEL ( + SET TCLKIT_PATCHLEVEL=8.6.6 +) + +%_VECHO% TclKitPatchLevel = '%TCLKIT_PATCHLEVEL%' + +IF NOT DEFINED TCLKIT_EXE_PATCHLEVEL ( + SET TCLKIT_EXE_PATCHLEVEL=8.6.4 +) + +%_VECHO% TclKitExePatchLevel = '%TCLKIT_EXE_PATCHLEVEL%' + IF /I "%PROCESSOR%" == "x86" ( CALL :fn_TclKitX86Variables + + IF ERRORLEVEL 1 ( + GOTO errors + ) ) ELSE IF /I "%PROCESSOR%" == "x64" ( CALL :fn_TclKitX64Variables + + IF ERRORLEVEL 1 ( + GOTO errors + ) ) ELSE ( GOTO usage ) %_VECHO% TclKitVersion = '%TCLKIT_VERSION%' %_VECHO% TclKitPatchLevel = '%TCLKIT_PATCHLEVEL%' +%_VECHO% TclKitExePatchLevel = '%TCLKIT_EXE_PATCHLEVEL%' +%_VECHO% TclKitNoEnv = '%TCLKIT_NOENV%' %_VECHO% TclKitNoSdk = '%TCLKIT_NOSDK%' %_VECHO% TclKitExe = '%TCLKIT_EXE%' %_VECHO% TclKitLib = '%TCLKIT_LIB%' @@ -123,7 +150,7 @@ IF NOT EXIST "%FRAMEWORKDIR%\csc.exe" ( GOTO errors ) -SET PATH=%FRAMEWORKDIR%;%PATH% +CALL :fn_PrependToPath FRAMEWORKDIR :skip_addToPath @@ -147,6 +174,7 @@ FOR %%F IN (%TCLKIT_FILES%) DO ( ) ) +IF DEFINED TCLKIT_NOENV GOTO skip_sdkUnZip IF DEFINED TCLKIT_NOSDK GOTO skip_sdkUnZip IF NOT EXIST "%TEMP%\%TCLKIT_SDK%" ( @@ -167,63 +195,83 @@ IF ERRORLEVEL 1 ( :skip_sdkUnZip -%__ECHO% ECHO SET TCLSH_CMD=%TEMP%\%TCLKIT_EXE%%OVERWRITE%"%ROOT%\SetTclKitEnv.bat" +IF DEFINED TCLKIT_NOENV GOTO skip_sdkEnvironment + +%__ECHO% ECHO SET TCLSH_CMD=%TEMP%\%TCLKIT_EXE%%OVERWRITE%"%ENVDIR%\SetTclKitEnv.bat" IF DEFINED TCLKIT_NOSDK GOTO skip_sdkVariables -%__ECHO% ECHO SET TCLINCDIR=%TEMP%\%TCLKIT_SDK%\include%APPEND%"%ROOT%\SetTclKitEnv.bat" -%__ECHO% ECHO SET TCLLIBDIR=%TEMP%\%TCLKIT_SDK%\lib%APPEND%"%ROOT%\SetTclKitEnv.bat" -%__ECHO% ECHO SET LIBTCLPATH=%TEMP%\%TCLKIT_SDK%\lib%APPEND%"%ROOT%\SetTclKitEnv.bat" -%__ECHO% ECHO SET LIBTCL=%TCLKIT_LIB%%APPEND%"%ROOT%\SetTclKitEnv.bat" -%__ECHO% ECHO SET LIBTCLSTUB=%TCLKIT_LIB_STUB%%APPEND%"%ROOT%\SetTclKitEnv.bat" +%__ECHO% ECHO SET TCLINCDIR=%TEMP%\%TCLKIT_SDK%\include%APPEND%"%ENVDIR%\SetTclKitEnv.bat" +%__ECHO% ECHO SET TCLLIBDIR=%TEMP%\%TCLKIT_SDK%\lib%APPEND%"%ENVDIR%\SetTclKitEnv.bat" +%__ECHO% ECHO SET LIBTCLPATH=%TEMP%\%TCLKIT_SDK%\lib%APPEND%"%ENVDIR%\SetTclKitEnv.bat" +%__ECHO% ECHO SET LIBTCL=%TCLKIT_LIB%%APPEND%"%ENVDIR%\SetTclKitEnv.bat" +%__ECHO% ECHO SET LIBTCLSTUB=%TCLKIT_LIB_STUB%%APPEND%"%ENVDIR%\SetTclKitEnv.bat" :skip_sdkVariables ECHO. -ECHO Wrote "%ROOT%\SetTclKitEnv.bat". +ECHO Wrote "%ENVDIR%\SetTclKitEnv.bat". ECHO Please run it to set the necessary Tcl environment variables. ECHO. +:skip_sdkEnvironment + GOTO no_errors :fn_TclKitX86Variables + REM + REM NOTE: By default, use latest available version of the TclKit SDK + REM for x86. However, the "default" TclKit executable for x86 + REM is still used here because it is the only one "well-known" + REM to be available for download. + REM IF NOT DEFINED TCLKIT_PATCHLEVEL ( - SET TCLKIT_PATCHLEVEL=8.6.4 + ECHO The TCLKIT_PATCHLEVEL environment variable must be set first. + CALL :fn_SetErrorLevel + GOTO :EOF ) SET TCLKIT_VERSION=%TCLKIT_PATCHLEVEL:.=% SET TCLKIT_VERSION=%TCLKIT_VERSION:~0,2% - SET TCLKIT_EXE=tclkit-%TCLKIT_PATCHLEVEL%.exe + IF DEFINED TCLKIT_EXE_PATCHLEVEL ( + SET TCLKIT_EXE=tclkit-%TCLKIT_EXE_PATCHLEVEL%.exe + ) ELSE ( + SET TCLKIT_EXE=tclkit-%TCLKIT_PATCHLEVEL%.exe + ) SET TCLKIT_LIB=libtclkit%TCLKIT_PATCHLEVEL:.=%.lib SET TCLKIT_LIB_STUB=libtclstub%TCLKIT_VERSION:.=%.a SET TCLKIT_SDK=libtclkit-sdk-x86-%TCLKIT_PATCHLEVEL% SET TCLKIT_SDK_ZIP=%TCLKIT_SDK%.zip SET TCLKIT_FILES=%TCLKIT_EXE% - IF NOT DEFINED TCLKIT_NOSDK ( + IF NOT DEFINED TCLKIT_NOENV IF NOT DEFINED TCLKIT_NOSDK ( SET TCLKIT_FILES=%TCLKIT_FILES% unzip.exe %TCLKIT_SDK_ZIP% ) GOTO :EOF :fn_TclKitX64Variables + REM + REM NOTE: By default, use latest available version of the TclKit SDK + REM for x64. However, the "default" TclKit executable for x86 + REM is still used here because it is the only one "well-known" + REM to be available for download. + REM IF NOT DEFINED TCLKIT_PATCHLEVEL ( - REM - REM NOTE: By default, use latest available version of the TclKit SDK - REM for x64. However, the "default" TclKit executable for x86 - REM is still used here because it is the only one "well-known" - REM to be available for download. - REM - SET TCLKIT_PATCHLEVEL=8.6.3 - SET TCLKIT_EXE=tclkit-8.6.4.exe - ) ELSE ( - SET TCLKIT_EXE=tclkit-%TCLKIT_PATCHLEVEL%.exe + ECHO The TCLKIT_PATCHLEVEL environment variable must be set first. + CALL :fn_SetErrorLevel + GOTO :EOF ) SET TCLKIT_VERSION=%TCLKIT_PATCHLEVEL:.=% SET TCLKIT_VERSION=%TCLKIT_VERSION:~0,2% + IF DEFINED TCLKIT_EXE_PATCHLEVEL ( + SET TCLKIT_EXE=tclkit-%TCLKIT_EXE_PATCHLEVEL%.exe + ) ELSE ( + SET TCLKIT_EXE=tclkit-%TCLKIT_PATCHLEVEL%.exe + ) SET TCLKIT_LIB=libtclkit%TCLKIT_PATCHLEVEL:.=%.lib SET TCLKIT_LIB_STUB=libtclstub%TCLKIT_VERSION:.=%.a SET TCLKIT_SDK=libtclkit-sdk-x64-%TCLKIT_PATCHLEVEL% SET TCLKIT_SDK_ZIP=%TCLKIT_SDK%.zip SET TCLKIT_FILES=%TCLKIT_EXE% - IF NOT DEFINED TCLKIT_NOSDK ( + IF NOT DEFINED TCLKIT_NOENV IF NOT DEFINED TCLKIT_NOSDK ( SET TCLKIT_FILES=%TCLKIT_FILES% unzip.exe %TCLKIT_SDK_ZIP% ) GOTO :EOF @@ -240,6 +288,18 @@ GOTO no_errors ENDLOCAL && SET %1=%VALUE% GOTO :EOF +:fn_PrependToPath + IF NOT DEFINED %1 GOTO :EOF + SETLOCAL + SET __ECHO_CMD=ECHO %%%1%% + FOR /F "delims=" %%V IN ('%__ECHO_CMD%') DO ( + SET VALUE=%%V + ) + SET VALUE=%VALUE:"=% + REM " + ENDLOCAL && SET PATH=%VALUE%;%PATH% + GOTO :EOF + :fn_ResetErrorLevel VERIFY > NUL GOTO :EOF diff --git a/tool/Replace.cs b/tool/Replace.cs new file mode 100644 index 0000000000..3475a47e6e --- /dev/null +++ b/tool/Replace.cs @@ -0,0 +1,223 @@ +/* +** 2016 February 26 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains C# code to perform regular expression replacements +** using the standard input and output channels. +*/ + +using System; +using System.Diagnostics; +using System.IO; +using System.Reflection; +using System.Runtime.InteropServices; +using System.Text.RegularExpressions; + +/////////////////////////////////////////////////////////////////////////////// + +#region Assembly Metadata +[assembly: AssemblyTitle("Replace Tool")] +[assembly: AssemblyDescription("Replace text using standard input/output.")] +[assembly: AssemblyCompany("SQLite Development Team")] +[assembly: AssemblyProduct("SQLite")] +[assembly: AssemblyCopyright("Public Domain")] +[assembly: ComVisible(false)] +[assembly: Guid("95a0513f-8863-48cd-a76f-cb80868cb578")] +[assembly: AssemblyVersion("1.0.*")] + +#if DEBUG +[assembly: AssemblyConfiguration("Debug")] +#else +[assembly: AssemblyConfiguration("Release")] +#endif +#endregion + +/////////////////////////////////////////////////////////////////////////////// + +namespace Replace +{ + /// <summary> + /// This enumeration is used to represent all the possible exit codes from + /// this tool. + /// </summary> + internal enum ExitCode + { + /// <summary> + /// The file download was a success. + /// </summary> + Success = 0, + + /// <summary> + /// The command line arguments are missing (i.e. null). Generally, + /// this should not happen. + /// </summary> + MissingArgs = 1, + + /// <summary> + /// The wrong number of command line arguments was supplied. + /// </summary> + WrongNumArgs = 2, + + /// <summary> + /// The "matchingOnly" flag could not be converted to a value of the + /// <see cref="Boolean"/> type. + /// </summary> + BadMatchingOnlyFlag = 3, + + /// <summary> + /// An exception was caught in <see cref="Main" />. Generally, this + /// should not happen. + /// </summary> + Exception = 4 + } + + /////////////////////////////////////////////////////////////////////////// + + internal static class Replace + { + #region Private Support Methods + /// <summary> + /// This method displays an error message to the console and/or + /// displays the command line usage information for this tool. + /// </summary> + /// <param name="message"> + /// The error message to display, if any. + /// </param> + /// <param name="usage"> + /// Non-zero to display the command line usage information. + /// </param> + private static void Error( + string message, + bool usage + ) + { + if (message != null) + Console.WriteLine(message); + + string fileName = Path.GetFileName( + Process.GetCurrentProcess().MainModule.FileName); + + Console.WriteLine(String.Format( + "usage: {0} <regExPattern> <regExSubSpec> <matchingOnly>", + fileName)); + } + #endregion + + /////////////////////////////////////////////////////////////////////// + + #region Program Entry Point + /// <summary> + /// This is the entry-point for this tool. It handles processing the + /// command line arguments, reading from the standard input channel, + /// replacing any matching lines of text, and writing to the standard + /// output channel. + /// </summary> + /// <param name="args"> + /// The command line arguments. + /// </param> + /// <returns> + /// Zero upon success; non-zero on failure. This will be one of the + /// values from the <see cref="ExitCode" /> enumeration. + /// </returns> + private static int Main( + string[] args + ) + { + // + // NOTE: Sanity check the command line arguments. + // + if (args == null) + { + Error(null, true); + return (int)ExitCode.MissingArgs; + } + + if (args.Length != 3) + { + Error(null, true); + return (int)ExitCode.WrongNumArgs; + } + + try + { + // + // NOTE: Create a regular expression from the first command + // line argument. Then, grab the replacement string, + // which is the second argument. + // + Regex regEx = new Regex(args[0]); + string replacement = args[1]; + + // + // NOTE: Attempt to convert the third argument to a boolean. + // + bool matchingOnly; + + if (!bool.TryParse(args[2], out matchingOnly)) + { + Error(null, true); + return (int)ExitCode.BadMatchingOnlyFlag; + } + + // + // NOTE: Grab the standard input and output channels from the + // console. + // + TextReader inputTextReader = Console.In; + TextWriter outputTextWriter = Console.Out; + + // + // NOTE: Loop until end-of-file is hit on the standard input + // stream. + // + while (true) + { + // + // NOTE: Read a line from the standard input channel. If + // null is returned here, there is no more input and + // we are done. + // + string inputLine = inputTextReader.ReadLine(); + + if (inputLine == null) + break; + + // + // NOTE: Perform regular expression replacements on this + // line, if any. Then, write the modified line to + // the standard output channel. + // + string outputLine = regEx.Replace(inputLine, replacement); + + if (!matchingOnly || !String.Equals( + inputLine, outputLine, StringComparison.Ordinal)) + { + outputTextWriter.WriteLine(outputLine); + } + } + + // + // NOTE: At this point, everything has succeeded. + // + return (int)ExitCode.Success; + } + catch (Exception e) + { + // + // NOTE: An exception was caught. Report it via the console + // and return failure. + // + Error(e.ToString(), false); + return (int)ExitCode.Exception; + } + } + #endregion + } +} diff --git a/tool/addopcodes.tcl b/tool/addopcodes.tcl deleted file mode 100644 index 84e3994ce8..0000000000 --- a/tool/addopcodes.tcl +++ /dev/null @@ -1,63 +0,0 @@ -#!/usr/bin/tclsh -# -# This script appends additional token codes to the end of the -# parse.h file that lemon generates. These extra token codes are -# not used by the parser. But they are used by the tokenizer and/or -# the code generator. -# -# -set in [open [lindex $argv 0] rb] -set max 0 -while {![eof $in]} { - set line [gets $in] - if {[regexp {^#define TK_} $line]} { - puts $line - set x [lindex $line 2] - if {$x>$max} {set max $x} - } -} -close $in - -# The following are the extra token codes to be added. SPACE and -# ILLEGAL *must* be the last two token codes and they must be in that order. -# -set extras { - TO_TEXT - TO_BLOB - TO_NUMERIC - TO_INT - TO_REAL - ISNOT - END_OF_FILE - UNCLOSED_STRING - FUNCTION - COLUMN - AGG_FUNCTION - AGG_COLUMN - UMINUS - UPLUS - REGISTER - ASTERISK - SPACE - ILLEGAL -} -if {[lrange $extras end-1 end]!="SPACE ILLEGAL"} { - error "SPACE and ILLEGAL must be the last two token codes and they\ - must be in that order" -} -foreach x $extras { - incr max - puts [format "#define TK_%-29s %4d" $x $max] -} - -# Some additional #defines related to token codes. -# -puts "\n/* The token codes above must all fit in 8 bits */" -puts [format "#define %-20s %-6s" TKFLG_MASK 0xff] -puts "\n/* Flags that can be added to a token code when it is not" -puts "** being stored in a u8: */" -foreach {fg val comment} { - TKFLG_DONTFOLD 0x100 {/* Omit constant folding optimizations */} -} { - puts [format "#define %-20s %-6s %s" $fg $val $comment] -} diff --git a/tool/build-all-msvc.bat b/tool/build-all-msvc.bat index 84528e37c2..83d660deb0 100755 --- a/tool/build-all-msvc.bat +++ b/tool/build-all-msvc.bat @@ -22,7 +22,7 @@ REM REM Example: REM REM CD /D C:\dev\sqlite\core -REM tool\build-all-msvc.bat C:\Temp +REM CALL tool\build-all-msvc.bat C:\Temp REM REM In the example above, "C:\dev\sqlite\core" represents the root of the REM source tree for SQLite and "C:\Temp" represents the final destination @@ -54,6 +54,11 @@ REM REM There are a few other environment variables that impact the build process REM when set ^(to anything^), they are: REM +REM USE_AUTOCONF_MAKEFILE +REM +REM When set, the "autoconf" Makefile for MSVC will be used instead of the main +REM Makefile for MSVC. It must exist at "%ROOT%\autoconf\Makefile.msc". +REM REM NOCLEAN REM REM When set, the "clean" target will not be used during each build iteration. @@ -69,6 +74,11 @@ REM be skipped and they will not appear in the final destination directory. REM Setting this environment variable is never strictly needed and could cause REM issues in some circumstances; therefore, setting it is not recommended. REM +REM NOMEMDEBUG +REM +REM When set, disables use of MEMDEBUG when building binaries for the "Debug" +REM configuration. +REM REM BUILD_ALL_SHELL REM REM When set, the command line shell will be built for each selected platform @@ -88,17 +98,30 @@ REM on the WindowsSdkDir environment variable. It causes this batch script to REM assume the Windows 10.0 SDK location should be used. REM REM NMAKE_ARGS +REM NMAKE_ARGS_DEBUG +REM NMAKE_ARGS_RETAIL REM -REM When set, the value is expanded and passed to the NMAKE command line, after -REM its other arguments. This is used to specify additional NMAKE options, for -REM example: +REM When set, these values are expanded and passed to the NMAKE command line, +REM after its other arguments. These may be used to specify additional NMAKE +REM options, for example: REM REM SET NMAKE_ARGS=FOR_WINRT=1 +REM SET NMAKE_ARGS_DEBUG=MEMDEBUG=1 +REM SET NMAKE_ARGS_RETAIL=WIN32HEAP=1 REM REM Using the above command before running this tool will cause the compiled REM binaries to target the WinRT environment, which provides a subset of the REM Win32 API. REM +REM DLL_FILE_NAME +REM DLL_PDB_FILE_NAME +REM LIB_FILE_NAME +REM EXE_FILE_NAME +REM EXE_PDB_FILE_NAME +REM +REM When set, these values will override the associated target file name used +REM for the build. +REM SETLOCAL REM SET __ECHO=ECHO @@ -106,6 +129,8 @@ REM SET __ECHO2=ECHO REM SET __ECHO3=ECHO IF NOT DEFINED _AECHO (SET _AECHO=REM) IF NOT DEFINED _CECHO (SET _CECHO=REM) +IF NOT DEFINED _CECHO2 (SET _CECHO2=REM) +IF NOT DEFINED _CECHO3 (SET _CECHO3=REM) IF NOT DEFINED _VECHO (SET _VECHO=REM) SET REDIRECT=^> @@ -154,6 +179,7 @@ REM REM NOTE: Change the current directory to the root of the source tree, saving REM the current directory on the directory stack. REM +%_CECHO2% PUSHD "%ROOT%" %__ECHO2% PUSHD "%ROOT%" IF ERRORLEVEL 1 ( @@ -172,7 +198,7 @@ IF NOT DEFINED ComSpec ( REM REM NOTE: This batch file requires the VcInstallDir environment variable to be -REM set. Tyipcally, this means this batch file needs to be run from an +REM set. Typically, this means this batch file needs to be run from an REM MSVC command prompt. REM IF NOT DEFINED VCINSTALLDIR ( @@ -204,11 +230,17 @@ REM NOTE: If the command used to invoke NMAKE is not already set, use the REM default. REM IF NOT DEFINED NMAKE_CMD ( - SET NMAKE_CMD=nmake -B -f Makefile.msc + IF DEFINED USE_AUTOCONF_MAKEFILE ( + SET NMAKE_CMD=nmake -B -f autoconf\Makefile.msc + ) ELSE ( + SET NMAKE_CMD=nmake -B -f Makefile.msc + ) ) %_VECHO% NmakeCmd = '%NMAKE_CMD%' %_VECHO% NmakeArgs = '%NMAKE_ARGS%' +%_VECHO% NmakeArgsDebug = '%NMAKE_ARGS_DEBUG%' +%_VECHO% NmakeArgsRetail = '%NMAKE_ARGS_RETAIL%' REM REM NOTE: Setup environment variables to translate between the MSVC platform @@ -239,7 +271,7 @@ REM IF DEFINED TCLSH_CMD ( SET TCLSH_FILE=%TCLSH_CMD% ) ELSE ( - SET TCLSH_FILE=tclsh85.exe + SET TCLSH_FILE=tclsh.exe ) FOR %%T IN (%TCLSH_FILE%) DO ( @@ -255,6 +287,30 @@ IF NOT DEFINED %TCLSH_FILE%_PATH ( GOTO errors ) +REM +REM NOTE: Setup the default names for the build targets we are creating. Any +REM ^(or all^) of these may end up being overridden. +REM +IF NOT DEFINED DLL_FILE_NAME ( + SET DLL_FILE_NAME=sqlite3.dll +) + +IF NOT DEFINED DLL_PDB_FILE_NAME ( + SET DLL_PDB_FILE_NAME=sqlite3.pdb +) + +IF NOT DEFINED LIB_FILE_NAME ( + SET LIB_FILE_NAME=sqlite3.lib +) + +IF NOT DEFINED EXE_FILE_NAME ( + SET EXE_FILE_NAME=sqlite3.exe +) + +IF NOT DEFINED EXE_PDB_FILE_NAME ( + SET EXE_PDB_FILE_NAME=sqlite3sh.pdb +) + REM REM NOTE: Set the TOOLPATH variable to contain all the directories where the REM external tools were found in the search above. @@ -407,7 +463,7 @@ FOR %%P IN (%PLATFORMS%) DO ( REM REM NOTE: Reset the PATH here to the absolute bare minimum required. REM - SET PATH=%TOOLPATH%;%SystemRoot%\System32;%SystemRoot% + CALL :fn_ResetPath REM REM NOTE: This is the inner loop. There are normally two iterations, one @@ -434,12 +490,20 @@ FOR %%P IN (%PLATFORMS%) DO ( REM NOTE: Setting this to non-zero should enable the SQLITE_MEMDEBUG REM define. REM - SET MEMDEBUG=1 + IF NOT DEFINED NOMEMDEBUG ( + SET MEMDEBUG=1 + ) ) ELSE ( CALL :fn_UnsetVariable DEBUG CALL :fn_UnsetVariable MEMDEBUG ) + REM + REM NOTE: Copy the extra NMAKE arguments for this configuration into the + REM common variable used by the actual commands. + REM + CALL :fn_CopyVariable NMAKE_ARGS_%%B NMAKE_ARGS_CFG + REM REM NOTE: Launch a nested command shell to perform the following steps: REM @@ -463,6 +527,7 @@ FOR %%P IN (%PLATFORMS%) DO ( REM REM NOTE: Attempt to setup the MSVC environment for this platform. REM + %_CECHO3% CALL "%VCVARSALL%" %%P %__ECHO3% CALL "%VCVARSALL%" %%P IF ERRORLEVEL 1 ( @@ -546,7 +611,7 @@ FOR %%P IN (%PLATFORMS%) DO ( REM file, etc. REM IF NOT DEFINED NOCLEAN ( - %__ECHO% %NMAKE_CMD% clean + CALL :fn_MakeClean %%D IF ERRORLEVEL 1 ( ECHO Failed to clean for platform %%P. @@ -559,7 +624,7 @@ FOR %%P IN (%PLATFORMS%) DO ( REM specifically wanting to build for each platform. REM %_AECHO% Cleaning final core library output files only... - %__ECHO% DEL /Q *.lo sqlite3.dll sqlite3.lib sqlite3.pdb 2%REDIRECT% NUL + %__ECHO% DEL /Q *.lo "%DLL_FILE_NAME%" "%LIB_FILE_NAME%" "%DLL_PDB_FILE_NAME%" 2%REDIRECT% NUL ) REM @@ -569,10 +634,10 @@ FOR %%P IN (%PLATFORMS%) DO ( REM Also, disable looking for and/or linking to the native Tcl REM runtime library. REM - %__ECHO% %NMAKE_CMD% sqlite3.dll XCOMPILE=1 USE_NATIVE_LIBPATHS=1 NO_TCL=1 %NMAKE_ARGS% + CALL :fn_MakeDll %%D IF ERRORLEVEL 1 ( - ECHO Failed to build %%B "sqlite3.dll" for platform %%P. + ECHO Failed to build %%B "%DLL_FILE_NAME%" for platform %%P. GOTO errors ) @@ -580,10 +645,10 @@ FOR %%P IN (%PLATFORMS%) DO ( REM NOTE: Copy the "sqlite3.dll" file to the appropriate directory for REM the build and platform beneath the binary directory. REM - %__ECHO% XCOPY sqlite3.dll "%BINARYDIRECTORY%\%%B\%%D\" %FFLAGS% %DFLAGS% + %__ECHO% XCOPY "%DLL_FILE_NAME%" "%BINARYDIRECTORY%\%%B\%%D\" %FFLAGS% %DFLAGS% IF ERRORLEVEL 1 ( - ECHO Failed to copy "sqlite3.dll" to "%BINARYDIRECTORY%\%%B\%%D\". + ECHO Failed to copy "%DLL_FILE_NAME%" to "%BINARYDIRECTORY%\%%B\%%D\". GOTO errors ) @@ -591,10 +656,10 @@ FOR %%P IN (%PLATFORMS%) DO ( REM NOTE: Copy the "sqlite3.lib" file to the appropriate directory for REM the build and platform beneath the binary directory. REM - %__ECHO% XCOPY sqlite3.lib "%BINARYDIRECTORY%\%%B\%%D\" %FFLAGS% %DFLAGS% + %__ECHO% XCOPY "%LIB_FILE_NAME%" "%BINARYDIRECTORY%\%%B\%%D\" %FFLAGS% %DFLAGS% IF ERRORLEVEL 1 ( - ECHO Failed to copy "sqlite3.lib" to "%BINARYDIRECTORY%\%%B\%%D\". + ECHO Failed to copy "%LIB_FILE_NAME%" to "%BINARYDIRECTORY%\%%B\%%D\". GOTO errors ) @@ -604,11 +669,13 @@ FOR %%P IN (%PLATFORMS%) DO ( REM are prevented from doing so. REM IF NOT DEFINED NOSYMBOLS ( - %__ECHO% XCOPY sqlite3.pdb "%BINARYDIRECTORY%\%%B\%%D\" %FFLAGS% %DFLAGS% + IF EXIST "%DLL_PDB_FILE_NAME%" ( + %__ECHO% XCOPY "%DLL_PDB_FILE_NAME%" "%BINARYDIRECTORY%\%%B\%%D\" %FFLAGS% %DFLAGS% - IF ERRORLEVEL 1 ( - ECHO Failed to copy "sqlite3.pdb" to "%BINARYDIRECTORY%\%%B\%%D\". - GOTO errors + IF ERRORLEVEL 1 ( + ECHO Failed to copy "%DLL_PDB_FILE_NAME%" to "%BINARYDIRECTORY%\%%B\%%D\". + GOTO errors + ) ) ) @@ -627,7 +694,7 @@ FOR %%P IN (%PLATFORMS%) DO ( REM specifically wanting to build for each platform. REM %_AECHO% Cleaning final shell executable output files only... - %__ECHO% DEL /Q sqlite3.exe sqlite3sh.pdb 2%REDIRECT% NUL + %__ECHO% DEL /Q "%EXE_FILE_NAME%" "%EXE_PDB_FILE_NAME%" 2%REDIRECT% NUL ) REM @@ -637,10 +704,10 @@ FOR %%P IN (%PLATFORMS%) DO ( REM Also, disable looking for and/or linking to the native Tcl REM runtime library. REM - %__ECHO% %NMAKE_CMD% sqlite3.exe XCOMPILE=1 USE_NATIVE_LIBPATHS=1 NO_TCL=1 %NMAKE_ARGS% + CALL :fn_MakeExe %%D IF ERRORLEVEL 1 ( - ECHO Failed to build %%B "sqlite3.exe" for platform %%P. + ECHO Failed to build %%B "%EXE_FILE_NAME%" for platform %%P. GOTO errors ) @@ -648,10 +715,10 @@ FOR %%P IN (%PLATFORMS%) DO ( REM NOTE: Copy the "sqlite3.exe" file to the appropriate directory REM for the build and platform beneath the binary directory. REM - %__ECHO% XCOPY sqlite3.exe "%BINARYDIRECTORY%\%%B\%%D\" %FFLAGS% %DFLAGS% + %__ECHO% XCOPY "%EXE_FILE_NAME%" "%BINARYDIRECTORY%\%%B\%%D\" %FFLAGS% %DFLAGS% IF ERRORLEVEL 1 ( - ECHO Failed to copy "sqlite3.exe" to "%BINARYDIRECTORY%\%%B\%%D\". + ECHO Failed to copy "%EXE_FILE_NAME%" to "%BINARYDIRECTORY%\%%B\%%D\". GOTO errors ) @@ -661,11 +728,13 @@ FOR %%P IN (%PLATFORMS%) DO ( REM unless we are prevented from doing so. REM IF NOT DEFINED NOSYMBOLS ( - %__ECHO% XCOPY sqlite3sh.pdb "%BINARYDIRECTORY%\%%B\%%D\" %FFLAGS% %DFLAGS% + IF EXIST "%EXE_PDB_FILE_NAME%" ( + %__ECHO% XCOPY "%EXE_PDB_FILE_NAME%" "%BINARYDIRECTORY%\%%B\%%D\" %FFLAGS% %DFLAGS% - IF ERRORLEVEL 1 ( - ECHO Failed to copy "sqlite3sh.pdb" to "%BINARYDIRECTORY%\%%B\%%D\". - GOTO errors + IF ERRORLEVEL 1 ( + ECHO Failed to copy "%EXE_PDB_FILE_NAME%" to "%BINARYDIRECTORY%\%%B\%%D\". + GOTO errors + ) ) ) ) @@ -684,6 +753,7 @@ FOR %%P IN (%PLATFORMS%) DO ( REM REM NOTE: Restore the saved current directory from the directory stack. REM +%_CECHO2% POPD %__ECHO2% POPD IF ERRORLEVEL 1 ( @@ -696,6 +766,18 @@ REM NOTE: If we get to this point, we have succeeded. REM GOTO no_errors +:fn_MakeClean + %__ECHO% %NMAKE_CMD% clean "PLATFORM=%1" XCOMPILE=1 USE_NATIVE_LIBPATHS=1 NO_TCL=1 %NMAKE_ARGS% %NMAKE_ARGS_CFG% + GOTO :EOF + +:fn_MakeDll + %__ECHO% %NMAKE_CMD% "%DLL_FILE_NAME%" "PLATFORM=%1" XCOMPILE=1 USE_NATIVE_LIBPATHS=1 NO_TCL=1 %NMAKE_ARGS% %NMAKE_ARGS_CFG% + GOTO :EOF + +:fn_MakeExe + %__ECHO% %NMAKE_CMD% "%EXE_FILE_NAME%" "PLATFORM=%1" XCOMPILE=1 USE_NATIVE_LIBPATHS=1 NO_TCL=1 %NMAKE_ARGS% %NMAKE_ARGS_CFG% + GOTO :EOF + :fn_ShowVariable SETLOCAL SET __ECHO_CMD=ECHO %%%2%% @@ -741,6 +823,10 @@ GOTO no_errors CALL :fn_ResetErrorLevel GOTO :EOF +:fn_ResetPath + SET PATH=%TOOLPATH%;%SystemRoot%\System32;%SystemRoot% + GOTO :EOF + :fn_AppendVariable SET __ECHO_CMD=ECHO %%%1%% IF DEFINED %1 ( diff --git a/tool/build-shell.sh b/tool/build-shell.sh index 6a48299d73..7899080ebe 100644 --- a/tool/build-shell.sh +++ b/tool/build-shell.sh @@ -16,7 +16,6 @@ gcc -o sqlite3 -g -Os -I. \ -DSQLITE_ENABLE_FTS4 \ -DSQLITE_ENABLE_RTREE \ -DHAVE_READLINE \ - -DHAVE_USLEEP=1 \ ../sqlite/src/shell.c \ - ../sqlite/src/test_vfstrace.c \ + ../sqlite/ext/misc/vfstrace.c \ sqlite3.c -ldl -lreadline -lncurses diff --git a/tool/buildtclext.tcl b/tool/buildtclext.tcl new file mode 100644 index 0000000000..535ed37e72 --- /dev/null +++ b/tool/buildtclext.tcl @@ -0,0 +1,334 @@ +#!/usr/bin/tclsh +# +set help \ +{Run this TCL script to build and install the TCL interface library for +SQLite. Run the script with the specific "tclsh" for which the installation +should occur. + +There must be a valid "tclsqlite3.c" file in the working directory prior +to running this script. Use "make tclsqlite3.c" to generate that file. + +Options: + + --build-only Only build the extension, don't install it + --cc COMPILER Build using this compiler + --info Show info on existing SQLite TCL extension installs + --install-only Install an extension previously build + --uninstall Uninstall the extension + --version-check Check extension version against this source tree + --destdir DIR Installation root (used by "make install DESTDIR=...") + --tclConfig.sh FILE Use this tclConfig.sh instead of looking for one + +Other options are retained and passed through into the compiler.} + + +set build 1 +set install 1 +set uninstall 0 +set infoonly 0 +set versioncheck 0 +set CC {} +set OPTS {} +set DESTDIR ""; # --destdir "$(DESTDIR)" +set tclConfigSh ""; # --tclConfig.sh FILE +for {set ii 0} {$ii<[llength $argv]} {incr ii} { + set a0 [lindex $argv $ii] + if {$a0=="--install-only"} { + set build 0 + } elseif {$a0=="--build-only"} { + set install 0 + } elseif {$a0=="--uninstall"} { + set build 0 + set install 0 + set versioncheck 0 + set uninstall 1 + } elseif {$a0=="--info"} { + set build 0 + set install 0 + set versioncheck 0 + set infoonly 1 + } elseif {$a0=="--version-check"} { + set build 0 + set install 0 + set infoonly 0 + set versioncheck 1 + } elseif {$a0=="--cc" && $ii+1<[llength $argv]} { + incr ii + set CC [lindex $argv $ii] + } elseif {$a0=="--destdir" && $ii+1<[llength $argv]} { + incr ii + set DESTDIR [lindex $argv $ii] + } elseif {$a0=="--tclConfig.sh" && $ii+1<[llength $argv]} { + incr ii + set tclConfigSh [lindex $argv $ii] + } elseif {[string match -* $a0]} { + append OPTS " $a0" + } else { + puts stderr "Unknown option: \"$a0\"\n" + puts stderr $help + exit 1 + } +} + +# Find the root of the SQLite source tree +# +set srcdir [file normalize [file dir $argv0]/..] + +# Get the SQLite version number into $VERSION +# +set fd [open $srcdir/VERSION] +set VERSION [string trim [read $fd]] +close $fd + +if {$tcl_platform(platform) eq "windows"} { + # We are only able to install, uninstall, and list on Windows. + # The build process is handled by the Makefile.msc, specifically + # using "nmake /f Makefile.msc pkgIndex.tcl tclsqlite3.dll" + # + if {$build} { + puts "Unable to build on Windows using the builttclext.tcl script." + puts "To build, run\n" + puts " \"nmake /f Makefile.msc pkgIndex.tcl tclsqlite3.dll" + exit 1 + } + set OUT tclsqlite3.dll +} else { + # Read the tclConfig.sh file into the $tclConfig variable + # + if {"" eq $tclConfigSh} { + # Figure out the location of the tclConfig.sh file used by the + # tclsh that is executing this script. + # + if {[catch { + set LIBDIR [tcl::pkgconfig get libdir,install] + }]} { + puts stderr "$argv0: tclsh does not support tcl::pkgconfig." + exit 1 + } + if {![file exists $LIBDIR]} { + puts stderr "$argv0: cannot find the tclConfig.sh file." + puts stderr "$argv0: tclsh reported library directory \"$LIBDIR\"\ + does not exist." + exit 1 + } + if {![file exists $LIBDIR/tclConfig.sh] + || [file size $LIBDIR/tclConfig.sh]<5000} { + set n1 $LIBDIR/tcl$::tcl_version + if {[file exists $n1/tclConfig.sh] + && [file size $n1/tclConfig.sh]>5000} { + set LIBDIR $n1 + } else { + puts stderr "$argv0: cannot find tclConfig.sh in either $LIBDIR or $n1" + exit 1 + } + } + #puts "using $LIBDIR/tclConfig.sh" + set fd [open $LIBDIR/tclConfig.sh rb] + set tclConfig [read $fd] + close $fd + } else { + # User-provided tclConfig.sh + # + set fd [open $tclConfigSh rb] + set tclConfig [read $fd] + close $fd + } + + # Extract parameter we will need from the tclConfig.sh file + # + set TCLMAJOR 8 + regexp {TCL_MAJOR_VERSION='(\d)'} $tclConfig all TCLMAJOR + set SUFFIX so + regexp {TCL_SHLIB_SUFFIX='\.([^']+)'} $tclConfig all SUFFIX + if {$CC==""} { + set cc {} + regexp {TCL_CC='([^']+)'} $tclConfig all cc + if {$cc!=""} { + set CC $cc + } + } + if {$CC==""} { + set CC gcc + } + set CFLAGS -fPIC + regexp {TCL_SHLIB_CFLAGS='([^']+)'} $tclConfig all CFLAGS + set LIBS {} + regexp {TCL_STUB_LIB_SPEC='([^']+)'} $tclConfig all LIBS + set INC "-I$srcdir/src" + set inc {} + regexp {TCL_INCLUDE_SPEC='([^']+)'} $tclConfig all inc + if {$inc!=""} { + append INC " $inc" + } + set cmd {${CC} ${CFLAGS} ${LDFLAGS} -shared} + regexp {TCL_SHLIB_LD='([^']+)(-Wl,--out-implib.*)?'} $tclConfig all cmd + set LDFLAGS "$INC -DUSE_TCL_STUBS" + if {[string length $OPTS]>1} { + append LDFLAGS $OPTS + } + if {$tcl_platform(os) eq "Windows NT"} { + set OUT cyg + } else { + set OUT lib + } + if {$TCLMAJOR>8} { + set OUT ${OUT}tcl9sqlite$VERSION.$SUFFIX + } else { + set OUT ${OUT}sqlite$VERSION.$SUFFIX + } + set @ $OUT; # Workaround for https://sqlite.org/forum/forumpost/0683a49cb02f31a1 + # in which Gentoo edits their tclConfig.sh to include an soname + # linker flag which includes ${@} (the target file's name). + set CMD [subst $cmd] +} + +# Check the SQLite TCL extension that is loaded by default by this running +# TCL interpreter to see if it has the same SQLITE_SOURCE_ID as the source +# code in the directory holding this script. +# +if {$versioncheck} { + if {[catch {package require sqlite3} msg]} { + puts stderr "No SQLite TCL extension available: $msg" + exit 1 + } + sqlite3 db :memory: + set extvers [db one {SELECT sqlite_source_id()}] + db close + set fd [open sqlite3.h rb] + set sqlite3h [read $fd] + close $fd + regexp {#define SQLITE_SOURCE_ID +"([^"]+)"} $sqlite3h all srcvers + set srcvers [string range $srcvers 0 78] + set extvers [string range $extvers 0 78] + if {$srcvers==$extvers} { + puts "source code and extension versions aligned:\n$extvers" + exit 0 + } + puts stderr "source code and extension versions differ" + puts stderr "source: $srcvers\nextension: $extvers" + exit 1 +} + +# Show information about prior installs +# +if {$infoonly} { + set cnt 0 + foreach dir $auto_path { + foreach subdir [glob -nocomplain -types d $dir/sqlite3*] { + if {[file exists $subdir/pkgIndex.tcl]} { + puts $subdir + incr cnt + } + } + } + if {$cnt==0} { + puts "no current installations of the SQLite TCL extension" + } + exit +} + +# Uninstall the extension +# +if {$uninstall} { + set cnt 0 + foreach dir $auto_path { + if {[file isdirectory $dir/sqlite$VERSION]} { + incr cnt + if {![file writable $dir] || ![file writable $dir/sqlite$VERSION]} { + puts "cannot uninstall $dir/sqlite$VERSION - permission denied" + } else { + puts "uninstalling $dir/sqlite$VERSION..." + file delete -force $dir/sqlite$VERSION + } + } + } + if {$cnt==0} { + puts "nothing to uninstall" + } + exit +} + +if {$install} { + # Figure out where the extension will be installed. Put the extension + # in the first writable directory on $auto_path. + # + set DEST {} + foreach dir $auto_path { + if {[string match //*:* $dir]} { + # We can't install to //zipfs: paths + continue + } elseif {"" ne $DESTDIR && ![file writable $DESTDIR]} { + # In the common case, ${DESTDIR}${dir} will not exist when we + # get to this point of the installation, and the "is writable?" + # check just below this will fail for that case. + # + # Assumption made for simplification's sake: if ${DESTDIR} is + # not writable, no part of the remaining path will + # be. ${DESTDIR} is typically used by OS package maintainers, + # not normal installations, and it "shouldn't" ever happen that + # the DESTDIR is read-only while the target ${DESTDIR}${prefix} + # is not, as it's typical for such installations to create + # ${prefix} on-demand under ${DESTDIR}. + break + } + set dir ${DESTDIR}$dir + if {[file writable $dir] || "" ne $DESTDIR} { + # the dir will be created later ^^^^^^^^ + set DEST $dir + break + } elseif {[glob -nocomplain $dir/sqlite3*/pkgIndex.tcl]!=""} { + set conflict [lindex [glob $dir/sqlite3*/pkgIndex.tcl] 0] + puts "Unable to install. There is already a conflicting version" + puts "of the SQLite TCL Extension that cannot be overwritten at\n" + puts " [file dirname $conflict]\n" + puts "Consider running using sudo to work around this problem." + exit 1 + } + } + if {$DEST==""} { + puts "None of the directories on \$auto_path are writable by this process," + puts "so the installation cannot take place. Consider running using sudo" + puts "to work around this problem.\n" + puts "These are the (unwritable) \$auto_path directories:\n" + foreach dir $auto_path { + puts " * ${DESTDIR}$dir" + } + exit 1 + } +} + +if {$build} { + # Generate the pkgIndex.tcl file + # + puts "generating pkgIndex.tcl..." + set fd [open pkgIndex.tcl w] + puts $fd [subst -nocommands {# -*- tcl -*- +# Tcl package index file, version ??? +# +package ifneeded sqlite3 $VERSION \\ + [list load [file join \$dir $OUT] Sqlite3] +}] + close $fd + + # Generate and execute the command with which to do the compilation. + # + set cmd "$CMD -DUSE_TCL_STUBS tclsqlite3.c -o $OUT $LIBS" + puts $cmd + file delete -force $OUT + catch {exec {*}$cmd} errmsg + if {$errmsg!="" && ![file exists $OUT]} { + puts $errmsg + exit 1 + } +} + + +if {$install} { + # Install the extension + set DEST2 $DEST/sqlite$VERSION + file mkdir $DEST2 + puts "installing $DEST2/pkgIndex.tcl" + file copy -force pkgIndex.tcl $DEST2 + puts "installing $DEST2/$OUT" + file copy -force $OUT $DEST2 +} diff --git a/tool/cg_anno.tcl b/tool/cg_anno.tcl index f806c5a5af..50a3ca8558 100755 --- a/tool/cg_anno.tcl +++ b/tool/cg_anno.tcl @@ -1,24 +1,79 @@ -#!/usr/bin/tclsh +#!/bin/sh +# \ +exec tclsh "$0" ${1+"$@"} # # A wrapper around cg_annotate that sets appropriate command-line options # and rearranges the output so that annotated files occur in a consistent -# sorted order. Used by the run-speed-test.tcl script. +# sorted order. Used by the speed-check.tcl script. # set in [open "|cg_annotate --show=Ir --auto=yes --context=40 $argv" r] set dest ! set out(!) {} +set linenum 0 +set cntlines 0 ;# true to remember cycle counts on each line +set seenSqlite3 0 ;# true if we have seen the sqlite3.c file while {![eof $in]} { set line [string map {\t { }} [gets $in]] if {[regexp {^-- Auto-annotated source: (.*)} $line all name]} { set dest $name - } elseif {[regexp {^-- line \d+ ------} $line]} { + if {[string match */sqlite3.c $dest]} { + set cntlines 1 + set seenSqlite3 1 + } else { + set cntlines 0 + } + } elseif {[regexp {^-- line (\d+) ------} $line all ln]} { set line [lreplace $line 2 2 {#}] + set linenum [expr {$ln-1}] } elseif {[regexp {^The following files chosen for } $line]} { set dest ! } append out($dest) $line\n + if {$cntlines} { + incr linenum + if {[regexp {^ *([0-9,]+) } $line all x]} { + set x [string map {, {}} $x] + set cycles($linenum) $x + } + } } foreach x [lsort [array names out]] { puts $out($x) } + +# If the sqlite3.c file has been seen, then output a summary of the +# cycle counts for each file that went into making up sqlite3.c +# +if {$seenSqlite3} { + close $in + set in [open sqlite3.c] + set linenum 0 + set fn sqlite3.c + set pattern1 {^/\*+ Begin file ([^ ]+) \*} + set pattern2 {^/\*+ Continuing where we left off in ([^ ]+) \*} + while {![eof $in]} { + set line [gets $in] + incr linenum + if {[regexp $pattern1 $line all newfn]} { + set fn $newfn + } elseif {[regexp $pattern2 $line all newfn]} { + set fn $newfn + } elseif {[info exists cycles($linenum)]} { + incr fcycles($fn) $cycles($linenum) + } + } + close $in + puts {**********************************************************************} + set lx {} + set sum 0 + foreach {fn cnt} [array get fcycles] { + lappend lx [list $cnt $fn] + incr sum $cnt + } + puts [format {%20s %14d %8.3f%%} TOTAL $sum 100] + foreach entry [lsort -index 0 -integer -decreasing $lx] { + foreach {cnt fn} $entry break + puts [format {%20s %14d %8.3f%%} $fn $cnt [expr {$cnt*100.0/$sum}]] + } +} diff --git a/tool/cktclsh.sh b/tool/cktclsh.sh new file mode 100644 index 0000000000..1928a40998 --- /dev/null +++ b/tool/cktclsh.sh @@ -0,0 +1,11 @@ +# Fail with an error if the TCLSH named in $2 is not tclsh version $1 or later. +# +echo "set vers $1" >cktclsh$1.tcl +echo 'if {$tcl_version<$vers} {exit 1}' >>cktclsh$1.tcl +if ! $2 cktclsh$1.tcl +then + echo "ERROR: This makefile target requires tclsh $1 or later." + rm cktclsh$1.tcl + exit 1 +fi +rm cktclsh$1.tcl diff --git a/tool/cp.tcl b/tool/cp.tcl new file mode 100644 index 0000000000..80e4adf107 --- /dev/null +++ b/tool/cp.tcl @@ -0,0 +1,28 @@ +#/usr/bin/tclsh +# +# This is a TCL script that copies multiple files into a common directory. +# The "cp" command will do this on unix, but no such command is available +# by default on Windows, so we have to use this script. +# +# tclsh cp.tcl FILE1 FILE2 ... FILEN DIR +# + +# This should be as simple as +# +# file copy -force -- {*}$argv +# +# But jimtcl doesn't support that. So we have to do it the hard way. + +if {[llength $argv]<2} { + error "Usage: $argv0 SRC... DESTDIR" +} +set n [llength $argv] +set destdir [lindex $argv [expr {$n-1}]] +if {![file isdir $destdir]} { + error "$argv0: not a directory: \"$destdir\"" +} +for {set i 0} {$i<$n-1} {incr i} { + set fn [file normalize [lindex $argv $i]] + set tail [file tail $fn] + file copy -force $fn [file normalize $destdir/$tail] +} diff --git a/tool/custom.txt b/tool/custom.txt new file mode 100644 index 0000000000..a9fc4807e7 --- /dev/null +++ b/tool/custom.txt @@ -0,0 +1,1254 @@ +aa +aaa +abc +abcdefg +abd +abf +Abortable +acc +accessor +accum +acd +activecpu +Adaptor +Additionallly +addop +addoptrace +addr +adjustements +af +aff +afp +afterward +Agg +agg +agginfo +alikes +Alloc +alloc +alloca +allocator +allocators +alphabetics +alphanumerics +alternateform +altertab +altform +amalgamator +amongst +analyse +antipenultimate +antirez +ap +api +appdef +appendall +appendchar +appendf +ar +arg +argc +argcount +arglist +argn +args +argv +arrayname +ascii +asm +async +atoi +atomics +auth +authorizer +autocheckpoint +autocommit +autoconf +autoext +autoextension +autoinc +autoincrement +autoincremented +autoindex +autoinstall +autovac +autovacuum +autovacuumed +autovacuuming +auxdata +awk +Ax +backend +backends +backfill +backfilled +backfilling +backtrace +backtraces +backtracing +bb +bba +bcb +bcc +beginthreadex +behavior +behavioral +behaviors +behaviour +benigncnt +bg +bigblob +bitcount +bitfield +bitmask +bitmasks +bitset +bitvec +bitvecs +bitwise +blobwrite +blockquote +Bloomfilter +bom +boolean +booleans +Borland +br +breadthfirstsearch +breakpoint +bt +btree +btrees +buf +bufpt +butoindex +bytearray +bytecode +bytecodevtab +byteorder +cacheflush +cachegrind +Cachesize +calc'ing +callgrind +cardinalities +cardinality +carray +cb +cd +cdbaa +ce +ceil +cellpadding +cellspacing +center +chacha +changecounter +changeset +characgter +characterset +checkpointed +Checkpointer +checkpointer +checkpointers +checkpointing +checksummed +checksums +chmod +chng +chown +chroot +chsize +chunksize +cid +cis +ckalloc'd +ckpt +cksum +cksumvfs +clientdata +closedir +clownshoe +cmd +cmp +cmpaff +Cmpr +cnt +codec +codepage +collseq +colname +compileoption +concat +config +confstr +connetion +consective +considertion +const +coredump +coroutine +coroutines +cov +crashparams +csr +csv +Cte +ctime +Ctrl +ctrl +ctx +ctype +cume +Cx +cx +Cygwin +cygwin +dan +darkstar +databasename +databse +datasource +datatypes +datetime +dbfuzz +dbinfo +dbname +dbpage +dbpagevfs +dbs +dbsize +dbsqlfuzz +dbstat +dbtotxt +De +de +deadman +deallocate +deallocated +deallocates +deallocating +deallocation +decltype +decrementing +defense +defenses +defn +defragment +defragmentation +defragmented +deinitialization +Deinitialize +deinitialize +demovfs +dependences +dequote +dequoted +dequoting +dereference +dereferenced +dereferences +desc +deserialization +deserialize +deserialized +deserializes +deserializing +dest +destructor +destructors +deterministically +dev +devsim +devsym +df +Dfdatasync +dflt +dir +directonly +dirent +dirs +disjunct +disjunction +disjuncts +diskfull +divy +dl +dll +dlopen +dlsym +docid +docids +dont +dontcache +dotfile +dotlock +doublearray +drh +dryrun +dstr +dt +Duint +dup +Durian +dword +Dx +dylib +Dyn +Ec +ec +ecel +editability +ef +efc +eg +Ei +elif +emcc +emscripten +encodings +endeavor +endfor +endian +endianness +endif +endthreadex +enum +eof +eph +Ephem +eq +eqp +equaling +equalities +errcode +errmsg +errno +errorcode +erroroffset +errstr +esc +esign +et +etfs +etilqs +eval +exe +expander +explainer +expmask +expr +exprlist +extern +fakeheap +fallocate +fanout +faultsim +favor +favors +fb +fc +fchmod +fchown +fclose +fcntl +fd +fdatasync +feb +fef +Feijoa +ferror +ffff +ffffeff +ffffffe +fffffff +ffffffff +fffffffffffffff +fflush +fg +fgets +fi +fibonacci +fid +Fifo +filecount +filectrl +filemapping +filesize +filesystem +filesystems +finalised +finalizer +findfirst +findnext +fixup +fk +fkctr +fkey +flattener +fmt +fopen +foreach +formatter +fprintf +fputs +fread +fred +fred's +freeblock +freeblocks +freelist +freepage +freespace +frombind +fs +fsanitize +fsctl +fsdir +fseek +fstat +fstree +fsync +fsynced +fsyncs +ftell +ftruncate +fts +fullfsync +fullname +fullschema +fullshm +fullsync +fullsyncs +func +funcs +fuzzer +fuzzers +fwrite +Fx +gcc +gcov +gdb +getcwd +getenv +gethostuuid +getpagesize +getpid +getrusage +getsubtype +getter +getters +gibabyte +gid +glibc +globbing +gmtime +Gosub +Goto +goto +groupbyparent +Groupid +growable +grp +hdl +hdr +hexdb +hexdouble +hexio +highwater +hijklmno +honor +honored +honoring +hostid +href +html +htsize +hw +Hwtime +icecube +ideographical +idx +idxaff +idxnum +idxstr +idx'th +ieee +ifdef +iff +ifndef +imm +impl +imposter +incr +incrblob +incrementing +indexable +indexname +infop +ing +init +initfail +initializer +initializers +initiallly +inlined +inlines +inlining +ino +inode +inodes +inopertune +installable +intarray +inteface +interoperate +interp +interpretable +intkey +intptr +intreal +intrinsics +invalidations +invariants +io +ioerr +iotrace +ipk +iplan +isalnum +isalpha +isatty +isdigit +isempty +isexplain +ismemdb +isnan +isspace +isxdigit +i'th +iversion +jfd +jj +jointype +jointypes +journaled +journaling +journalled +journalling +journalmode +jrnl +jsl +json +jt +julian +julianday +keyinfo +keywordhash +kibibytes +kvstorage +kvvfs +lappend +lasterrno +lbl +ldl +le +leafdata +leftjustify +len +leveling +lexeme +lexemes +lhs +li +libm +libversion +lifecycle +lindex +lineno +Linenoise +linenoise +linux +lised +lld +llvm +lm +ln +lncurses +loadext +localhost +localtime +lockd +lockdata +lockingcontext +lockless +lockproxy +locktype +logmsg +longvalue +longwords +lookahead +Lookaside +lookaside +lookups +losslessly +lpthread +lrc +lreadline +lru +lseek +lt +lvalue +lwr +makefile +makefiles +malloc +malloc'd +malloced +mallocs +manditory +manpage +matchinfo +materializations +mathfuncs +maxsize +mbcs +Mcafee +md +Meeus +mem +memcmp +memcpy +memdb +memdebug +memget +memmove +mempool +memset +memstatus +memsys +memvfs +mergeable +mergepatch +middleware +millisec +mincore +mingw +mis +miscoded +mj +mkctimec +mkdir +mkkeywordhash +mkopcodec +mkopcodeh +mkpragmatab +mmap +mmapped +mno +modeof +Movepage +mprintf +msd +msdos +msec +msg +msgs +msize +msteveb +msvc +mtime +mult +multibyte +multiplex'ed +multiplexor +multithreaded +multiwrite +mutex +mutexes +mutexing +mx +mxpathname +myprefix +nal +namecontext +namespace +natively +nbr +nbsp +ncell +ncol +ncycle +nd +ndlt +neighbors +neq +nestable +newrowid +nfs +nlt +nnn +nocase +nochange +nochng +nofollow +nolock +nomem +nomutex +nonblocking +nonroot +noop +noshm +notational +notheld +notnull +nowrap +nr +ntile +nul +nullable +nullif +nullvalue +Num +objproc +oc +offsetof +ofst +ogham +oid +Ok +ok +ol +onecolumn +onepass +onoff +onwards +oom +opcodesnever +openable +opendir +optimizers +optimizible +orconf +orderby +orderbylist +os +Oswrite +overread +overreads +overrideable +oversize +overwriteable +ovewrite +ovfl +pagecache +pagecaches +pagecount +pagelist +pageno +pagesize +pagetype +Param +params +passwd +patchset +pathname +pathnames +pc +pcache +pclose +pcx +pgno +pgoffset +pgsize +pgsz +pid +pluggable +pmasz +pn +popen +pos +posix +Postgres +Powersafe +powersafe +pq +pqr +pqrstuvw +pragma +pragmaed +pragma's +pragmas +pre +pread +preallocate +preallocated +precisions +precompiled +precomputed +prefetch +prefetched +preformated +preformatted +prepend +prepended +prepending +prepends +prepopulate +preprocess +preprocessed +preprocessing +preprocessor +prereq +prereqs +preupdate +primarykey +printf +printfs +prng +proc +procs +profiler +proleptic +proxying +proxys +pseudocode +pseudotable +psow +psz +pthread +pthreads +ptr +ptrmap +ptrs +purgeable +putsnl +pwrite +pz +qbox +Qcircle +quotaed +quotefix +radix +randomblob +randstr +rarr +rc +rcauth +Rcvr +rds +readdir +readline +readlock +readonly +Readr +realloc +reallocs +realvalue +rebalance +rebalanced +recoverability +redefinable +redux +reenabled +reentrant +refcount +refcounts +regs +reindexed +reinitializer +reinitializes +rekey +rekeyed +relevancies +relink +relink +relock +renormalize +reoptimize +reparse +reparse +reparsed +reparsing +reportable +reprepare +reprepare +reprepared +reprepares +representable +repurpose +Req'd +requeries +requote +reregister +reseek +reservebytes +resumable +retarget +retargeted +retrys +returntype +rfc +rhs +Ri +Rivest +ro +rootpage +rowid +rowids +Rowkey +rownumber +rowset +rsync +runtime +rw +rwc +samplelib +sampleno +sandboxed +sandboxing +savepoint +savepoints +scanf +scanstats +scanstatus +schemas +sectorsize +selecttrace +selftest +setrlimit +setsubtype +settitle +sharable +shm +shmlock +sibs +sig +signaling +significand +sizehint +Sizeof +sizeof +snprintf +Solaris +sorterref +soundex +sourceid +speedtest +sprintf +sql +sqlar +sqlite +sqliteplocks +sqliterc +sqllog +sqllogglobal +sqlthread +sqr +sqrt +src +srcck +statfs +stderr +stdin +stdout +stmt +stmts +str +strace +strcasecmp +strcmp +strdup +strerror +strftime +strglob +stricmp +stringify +strlen +strlike +strncmp +strncpy +strnicmp +stronly +strstr +struct +structs +subbitmap +subcases +subclassed +subclauses +subcomponents +subdirectory +subelement +subexpression +subexpressions +subfunction +subfunctions +subitem +subjournals +sublevels +subnode +suboptimal +subpages +subprocedures +subprog +subprogs +subq +subqueries +subquery +Subrtn +subsec +subsecond +subsequence +substr +substring +substrings +subsubstructure +subsubterms +Subtask +subtasks +subterm +subterms +subtransaction +subtransactions +subtree +subtrees +subtype +subtypes +sumint +superclass +superlock +superset +superunlock +symlinks +synching +sys +syscall +sz +szosfile +tablename +tailval +tailvar +tbl +tblname +Tcl +tcl +Tclcmd +tclcmd +tclsh +tclsqlite +tclvar +td +tempfilename +testcase +testctrl +testfixture +testtag +testvfs +textarray +th +threadid +threadsafe +threadsafety +throughs +tht +timediff +tkt +tm +tmp +tmpdir +tmpfs +tnum +Todo +tokenize +tokenizer +tokenizing +tolower +toobig +toupper +treetrace +treeview +trimleft +trimright +truesize +trys +Tsd +Ts'o +tunable +tvfs +txn +txt +Typecheck +typedef +typedefed +typedefs +typename +typenames +typeof +tz +uber +uid +uint +ul +umask +Un +un +unallocated +unanalyzed +unary +unbuffered +unclosed +uncompiled +uncomputed +undefining +underfull +unexpanded +unfinalized +unfreed +unhex +unicode +unindexed +uninit +uninitialize +unintuitive +unioned +unissued +unix +unixepoch +unlink +unlinked +unlinking +unlinks +unmap +unmapped +unmapping +unoptimized +unparsed +unreduced +unref +unreferenced +unrefs +unregister +unregistering +unregisters +unresolvable +unsynced +unterminated +untracked +untrusted +Upfrom +uppercasing +upr +Upsert +upsert +upto +uptr +uri +userauth +userdata +Userid +usleep +utc +Utf +utf +util +uuu +uuuuu +uuzzzz +va +valgrind +vanishingly +vappendf +vararg +varargs +varint +varints +varname +vcolumn +vdbe +vdbeapi +vdbe's +vdbes +vdbesort +ve +verifications +vfs +vfslog +vfsname +vfs's +vfstrace +vm +vmprintf +vmstep +vsnprintf +vt +vtab +vtabs +Vugt +vvv +vvvv +vvvvv +vvvvvv +vwait +vxworks +wal +wasm +wherecode +whereexpr +wheretrace +whitespace +Willmann +withoutrowid +workalike +wr +wrapup +writeable +writecrash +writefile +wsd +ww +wwww +wwzzzzyy +wxyz +xa +xac +xb +xbf +xc +xd +xdg +xe +xf +xfe +xfer +xff +xfff +xfffd +xfffe +xffffffff +x'hhhhhh +xinfo +xlc +xtype +xxxx +xxxxx +xxxxxx +xxxxxxx +xxxxxxxx +xyz +xyzzy +yy +yyxxxxxx +yyy +yyyyy +yyyyyy +zeroblob +Zeroblobs +zerodata +zeropad +zipfile +zipvfs +zplan +zulu +zzzz +zzzzyyyy diff --git a/tool/dbhash.c b/tool/dbhash.c new file mode 100644 index 0000000000..78685dcd6c --- /dev/null +++ b/tool/dbhash.c @@ -0,0 +1,491 @@ +/* +** 2016-06-07 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** This is a utility program that computes an SHA1 hash on the content +** of an SQLite database. +** +** The hash is computed over just the content of the database. Free +** space inside of the database file, and alternative on-disk representations +** of the same content (ex: UTF8 vs UTF16) do not affect the hash. So, +** for example, the database file page size, encoding, and auto_vacuum setting +** can all be changed without changing the hash. +*/ +#include <stdio.h> +#include <stdlib.h> +#include <stdarg.h> +#include <ctype.h> +#include <string.h> +#include <assert.h> +#include "sqlite3.h" + +/* Context for the SHA1 hash */ +typedef struct SHA1Context SHA1Context; +struct SHA1Context { + unsigned int state[5]; + unsigned int count[2]; + unsigned char buffer[64]; +}; + +/* +** All global variables are gathered into the "g" singleton. +*/ +struct GlobalVars { + const char *zArgv0; /* Name of program */ + unsigned fDebug; /* Debug flags */ + sqlite3 *db; /* The database connection */ + SHA1Context cx; /* SHA1 hash context */ +} g; + +/* +** Debugging flags +*/ +#define DEBUG_FULLTRACE 0x00000001 /* Trace hash to stderr */ + +/****************************************************************************** +** The Hash Engine +** +** Modify these routines (and appropriate state fields in global variable 'g') +** in order to compute a different (better?) hash of the database. +*/ +/* + * blk0() and blk() perform the initial expand. + * I got the idea of expanding during the round function from SSLeay + * + * blk0le() for little-endian and blk0be() for big-endian. + */ +#define SHA_ROT(x,l,r) ((x) << (l) | (x) >> (r)) +#define rol(x,k) SHA_ROT(x,k,32-(k)) +#define ror(x,k) SHA_ROT(x,32-(k),k) + +#define blk0le(i) (block[i] = (ror(block[i],8)&0xFF00FF00) \ + |(rol(block[i],8)&0x00FF00FF)) +#define blk0be(i) block[i] +#define blk(i) (block[i&15] = rol(block[(i+13)&15]^block[(i+8)&15] \ + ^block[(i+2)&15]^block[i&15],1)) + +/* + * (R0+R1), R2, R3, R4 are the different operations (rounds) used in SHA1 + * + * Rl0() for little-endian and Rb0() for big-endian. Endianness is + * determined at run-time. + */ +#define Rl0(v,w,x,y,z,i) \ + z+=((w&(x^y))^y)+blk0le(i)+0x5A827999+rol(v,5);w=ror(w,2); +#define Rb0(v,w,x,y,z,i) \ + z+=((w&(x^y))^y)+blk0be(i)+0x5A827999+rol(v,5);w=ror(w,2); +#define R1(v,w,x,y,z,i) \ + z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=ror(w,2); +#define R2(v,w,x,y,z,i) \ + z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=ror(w,2); +#define R3(v,w,x,y,z,i) \ + z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=ror(w,2); +#define R4(v,w,x,y,z,i) \ + z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=ror(w,2); + +/* + * Hash a single 512-bit block. This is the core of the algorithm. + */ +#define a qq[0] +#define b qq[1] +#define c qq[2] +#define d qq[3] +#define e qq[4] + +void SHA1Transform(unsigned int state[5], const unsigned char buffer[64]){ + unsigned int qq[5]; /* a, b, c, d, e; */ + static int one = 1; + unsigned int block[16]; + memcpy(block, buffer, 64); + memcpy(qq,state,5*sizeof(unsigned int)); + + /* Copy g.cx.state[] to working vars */ + /* + a = state[0]; + b = state[1]; + c = state[2]; + d = state[3]; + e = state[4]; + */ + + /* 4 rounds of 20 operations each. Loop unrolled. */ + if( 1 == *(unsigned char*)&one ){ + Rl0(a,b,c,d,e, 0); Rl0(e,a,b,c,d, 1); Rl0(d,e,a,b,c, 2); Rl0(c,d,e,a,b, 3); + Rl0(b,c,d,e,a, 4); Rl0(a,b,c,d,e, 5); Rl0(e,a,b,c,d, 6); Rl0(d,e,a,b,c, 7); + Rl0(c,d,e,a,b, 8); Rl0(b,c,d,e,a, 9); Rl0(a,b,c,d,e,10); Rl0(e,a,b,c,d,11); + Rl0(d,e,a,b,c,12); Rl0(c,d,e,a,b,13); Rl0(b,c,d,e,a,14); Rl0(a,b,c,d,e,15); + }else{ + Rb0(a,b,c,d,e, 0); Rb0(e,a,b,c,d, 1); Rb0(d,e,a,b,c, 2); Rb0(c,d,e,a,b, 3); + Rb0(b,c,d,e,a, 4); Rb0(a,b,c,d,e, 5); Rb0(e,a,b,c,d, 6); Rb0(d,e,a,b,c, 7); + Rb0(c,d,e,a,b, 8); Rb0(b,c,d,e,a, 9); Rb0(a,b,c,d,e,10); Rb0(e,a,b,c,d,11); + Rb0(d,e,a,b,c,12); Rb0(c,d,e,a,b,13); Rb0(b,c,d,e,a,14); Rb0(a,b,c,d,e,15); + } + R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19); + R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23); + R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27); + R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31); + R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35); + R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39); + R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43); + R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47); + R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51); + R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55); + R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59); + R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63); + R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67); + R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71); + R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75); + R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79); + + /* Add the working vars back into context.state[] */ + state[0] += a; + state[1] += b; + state[2] += c; + state[3] += d; + state[4] += e; +} + + +/* Initialize the SHA1 hash */ +static void hash_init(void){ + /* SHA1 initialization constants */ + g.cx.state[0] = 0x67452301; + g.cx.state[1] = 0xEFCDAB89; + g.cx.state[2] = 0x98BADCFE; + g.cx.state[3] = 0x10325476; + g.cx.state[4] = 0xC3D2E1F0; + g.cx.count[0] = g.cx.count[1] = 0; +} + +/* Add new content to the SHA1 hash */ +static void hash_step(const unsigned char *data, unsigned int len){ + unsigned int i, j; + + j = g.cx.count[0]; + if( (g.cx.count[0] += len << 3) < j ){ + g.cx.count[1] += (len>>29)+1; + } + j = (j >> 3) & 63; + if( (j + len) > 63 ){ + (void)memcpy(&g.cx.buffer[j], data, (i = 64-j)); + SHA1Transform(g.cx.state, g.cx.buffer); + for(; i + 63 < len; i += 64){ + SHA1Transform(g.cx.state, &data[i]); + } + j = 0; + }else{ + i = 0; + } + (void)memcpy(&g.cx.buffer[j], &data[i], len - i); +} + + +/* Add padding and compute and output the message digest. */ +static void hash_finish(const char *zName){ + unsigned int i; + unsigned char finalcount[8]; + unsigned char digest[20]; + static const char zEncode[] = "0123456789abcdef"; + char zOut[41]; + + for (i = 0; i < 8; i++){ + finalcount[i] = (unsigned char)((g.cx.count[(i >= 4 ? 0 : 1)] + >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */ + } + hash_step((const unsigned char *)"\200", 1); + while ((g.cx.count[0] & 504) != 448){ + hash_step((const unsigned char *)"\0", 1); + } + hash_step(finalcount, 8); /* Should cause a SHA1Transform() */ + for (i = 0; i < 20; i++){ + digest[i] = (unsigned char)((g.cx.state[i>>2] >> ((3-(i & 3)) * 8) ) & 255); + } + for(i=0; i<20; i++){ + zOut[i*2] = zEncode[(digest[i]>>4)&0xf]; + zOut[i*2+1] = zEncode[digest[i] & 0xf]; + } + zOut[i*2]= 0; + printf("%s %s\n", zOut, zName); +} +/* End of the hashing logic +*******************************************************************************/ + +/* +** Print an error resulting from faulting command-line arguments and +** abort the program. +*/ +static void cmdlineError(const char *zFormat, ...){ + va_list ap; + fprintf(stderr, "%s: ", g.zArgv0); + va_start(ap, zFormat); + vfprintf(stderr, zFormat, ap); + va_end(ap); + fprintf(stderr, "\n\"%s --help\" for more help\n", g.zArgv0); + exit(1); +} + +/* +** Print an error message for an error that occurs at runtime, then +** abort the program. +*/ +static void runtimeError(const char *zFormat, ...){ + va_list ap; + fprintf(stderr, "%s: ", g.zArgv0); + va_start(ap, zFormat); + vfprintf(stderr, zFormat, ap); + va_end(ap); + fprintf(stderr, "\n"); + exit(1); +} + +/* +** Prepare a new SQL statement. Print an error and abort if anything +** goes wrong. +*/ +static sqlite3_stmt *db_vprepare(const char *zFormat, va_list ap){ + char *zSql; + int rc; + sqlite3_stmt *pStmt; + + zSql = sqlite3_vmprintf(zFormat, ap); + if( zSql==0 ) runtimeError("out of memory"); + rc = sqlite3_prepare_v2(g.db, zSql, -1, &pStmt, 0); + if( rc ){ + runtimeError("SQL statement error: %s\n\"%s\"", sqlite3_errmsg(g.db), + zSql); + } + sqlite3_free(zSql); + return pStmt; +} +static sqlite3_stmt *db_prepare(const char *zFormat, ...){ + va_list ap; + sqlite3_stmt *pStmt; + va_start(ap, zFormat); + pStmt = db_vprepare(zFormat, ap); + va_end(ap); + return pStmt; +} + +/* +** Compute the hash for all rows of the query formed from the printf-style +** zFormat and its argument. +*/ +static void hash_one_query(const char *zFormat, ...){ + va_list ap; + sqlite3_stmt *pStmt; /* The query defined by zFormat and "..." */ + int nCol; /* Number of columns in the result set */ + int i; /* Loop counter */ + + /* Prepare the query defined by zFormat and "..." */ + va_start(ap, zFormat); + pStmt = db_vprepare(zFormat, ap); + va_end(ap); + nCol = sqlite3_column_count(pStmt); + + /* Compute a hash over the result of the query */ + while( SQLITE_ROW==sqlite3_step(pStmt) ){ + for(i=0; i<nCol; i++){ + switch( sqlite3_column_type(pStmt,i) ){ + case SQLITE_NULL: { + hash_step((const unsigned char*)"0",1); + if( g.fDebug & DEBUG_FULLTRACE ) fprintf(stderr, "NULL\n"); + break; + } + case SQLITE_INTEGER: { + sqlite3_uint64 u; + int j; + unsigned char x[8]; + sqlite3_int64 v = sqlite3_column_int64(pStmt,i); + memcpy(&u, &v, 8); + for(j=7; j>=0; j--){ + x[j] = u & 0xff; + u >>= 8; + } + hash_step((const unsigned char*)"1",1); + hash_step(x,8); + if( g.fDebug & DEBUG_FULLTRACE ){ + fprintf(stderr, "INT %s\n", sqlite3_column_text(pStmt,i)); + } + break; + } + case SQLITE_FLOAT: { + sqlite3_uint64 u; + int j; + unsigned char x[8]; + double r = sqlite3_column_double(pStmt,i); + memcpy(&u, &r, 8); + for(j=7; j>=0; j--){ + x[j] = u & 0xff; + u >>= 8; + } + hash_step((const unsigned char*)"2",1); + hash_step(x,8); + if( g.fDebug & DEBUG_FULLTRACE ){ + fprintf(stderr, "FLOAT %s\n", sqlite3_column_text(pStmt,i)); + } + break; + } + case SQLITE_TEXT: { + int n = sqlite3_column_bytes(pStmt, i); + const unsigned char *z = sqlite3_column_text(pStmt, i); + hash_step((const unsigned char*)"3", 1); + hash_step(z, n); + if( g.fDebug & DEBUG_FULLTRACE ){ + fprintf(stderr, "TEXT '%s'\n", sqlite3_column_text(pStmt,i)); + } + break; + } + case SQLITE_BLOB: { + int n = sqlite3_column_bytes(pStmt, i); + const unsigned char *z = sqlite3_column_blob(pStmt, i); + hash_step((const unsigned char*)"4", 1); + hash_step(z, n); + if( g.fDebug & DEBUG_FULLTRACE ){ + fprintf(stderr, "BLOB (%d bytes)\n", n); + } + break; + } + } + } + } + sqlite3_finalize(pStmt); +} + + +/* +** Print sketchy documentation for this utility program +*/ +static void showHelp(void){ + printf("Usage: %s [options] FILE ...\n", g.zArgv0); + printf( +"Compute a SHA1 hash on the content of database FILE. System tables such as\n" +"sqlite_stat1, sqlite_stat4, and sqlite_sequence are omitted from the hash.\n" +"Options:\n" +" --debug N Set debugging flags to N (experts only)\n" +" --like PATTERN Only hash tables whose name is LIKE the pattern\n" +" --schema-only Only hash the schema - omit table content\n" +" --without-schema Only hash table content - omit the schema\n" + ); +} + +int main(int argc, char **argv){ + const char *zDb = 0; /* Name of the database currently being hashed */ + int i; /* Loop counter */ + int rc; /* Subroutine return code */ + char *zErrMsg; /* Error message when opening database */ + sqlite3_stmt *pStmt; /* An SQLite query */ + const char *zLike = 0; /* LIKE pattern of tables to hash */ + int omitSchema = 0; /* True to compute hash on content only */ + int omitContent = 0; /* True to compute hash on schema only */ + int nFile = 0; /* Number of input filenames seen */ + + g.zArgv0 = argv[0]; + sqlite3_config(SQLITE_CONFIG_SINGLETHREAD); + for(i=1; i<argc; i++){ + const char *z = argv[i]; + if( z[0]=='-' ){ + z++; + if( z[0]=='-' ) z++; + if( strcmp(z,"debug")==0 ){ + if( i==argc-1 ) cmdlineError("missing argument to %s", argv[i]); + g.fDebug = strtol(argv[++i], 0, 0); + }else + if( strcmp(z,"help")==0 ){ + showHelp(); + return 0; + }else + if( strcmp(z,"like")==0 ){ + if( i==argc-1 ) cmdlineError("missing argument to %s", argv[i]); + if( zLike!=0 ) cmdlineError("only one --like allowed"); + zLike = argv[++i]; + }else + if( strcmp(z,"schema-only")==0 ){ + omitContent = 1; + }else + if( strcmp(z,"without-schema")==0 ){ + omitSchema = 1; + }else + { + cmdlineError("unknown option: %s", argv[i]); + } + }else{ + nFile++; + if( nFile<i ) argv[nFile] = argv[i]; + } + } + if( nFile==0 ){ + cmdlineError("no input files specified - nothing to do"); + } + if( omitSchema && omitContent ){ + cmdlineError("only one of --without-schema and --omit-schema allowed"); + } + if( zLike==0 ) zLike = "%"; + + for(i=1; i<=nFile; i++){ + static const int openFlags = + SQLITE_OPEN_READWRITE | /* Read/write so hot journals can recover */ + SQLITE_OPEN_URI + ; + zDb = argv[i]; + rc = sqlite3_open_v2(zDb, &g.db, openFlags, 0); + if( rc ){ + fprintf(stderr, "cannot open database file '%s'\n", zDb); + continue; + } + rc = sqlite3_exec(g.db, "SELECT * FROM sqlite_schema", 0, 0, &zErrMsg); + if( rc || zErrMsg ){ + sqlite3_close(g.db); + g.db = 0; + fprintf(stderr, "'%s' is not a valid SQLite database\n", zDb); + continue; + } + + /* Start the hash */ + hash_init(); + + /* Hash table content */ + if( !omitContent ){ + pStmt = db_prepare( + "SELECT name FROM sqlite_schema\n" + " WHERE type='table' AND sql NOT LIKE 'CREATE VIRTUAL%%'\n" + " AND name NOT LIKE 'sqlite_%%'\n" + " AND name LIKE '%q'\n" + " ORDER BY name COLLATE nocase;\n", + zLike + ); + while( SQLITE_ROW==sqlite3_step(pStmt) ){ + /* We want rows of the table to be hashed in PRIMARY KEY order. + ** Technically, an ORDER BY clause is required to guarantee that + ** order. However, though not guaranteed by the documentation, every + ** historical version of SQLite has always output rows in PRIMARY KEY + ** order when there is no WHERE or GROUP BY clause, so the ORDER BY + ** can be safely omitted. */ + hash_one_query("SELECT * FROM \"%w\"", sqlite3_column_text(pStmt,0)); + } + sqlite3_finalize(pStmt); + } + + /* Hash the database schema */ + if( !omitSchema ){ + hash_one_query( + "SELECT type, name, tbl_name, sql FROM sqlite_schema\n" + " WHERE tbl_name LIKE '%q'\n" + " ORDER BY name COLLATE nocase;\n", + zLike + ); + } + + /* Finish and output the hash and close the database connection. */ + hash_finish(zDb); + sqlite3_close(g.db); + } + return 0; +} diff --git a/tool/dbtotxt.c b/tool/dbtotxt.c new file mode 100644 index 0000000000..fbd6e3d519 --- /dev/null +++ b/tool/dbtotxt.c @@ -0,0 +1,188 @@ +/* +** Copyright 2008 D. Richard Hipp and Hipp, Wyrick & Company, Inc. +** All Rights Reserved +** +****************************************************************************** +** +** This file implements a stand-alone utility program that converts +** a binary file (usually an SQLite database) into a text format that +** is compact and friendly to human-readers. +** +** Usage: +** +** dbtotxt [OPTIONS] FILENAME +** +** where OPTIONS are zero or more of: +** +** --for-cli prepending '.open --hexdb' to the output +** +** --script The input file is expected to start with a +** zero-terminated SQL string. Output the +** ".open --hexdb" header, then the database +** then the SQL. +** +** --pagesize N set the database page size for later reading +** +** The translation of the database appears on standard output. If the +** --pagesize command-line option is omitted, then the page size is taken +** from the database header. +** +** Compactness is achieved by suppressing lines of all zero bytes. This +** works well at compressing test databases that are mostly empty. But +** the output will probably be lengthy for a real database containing lots +** of real content. For maximum compactness, it is suggested that test +** databases be constructed with "zeroblob()" rather than "randomblob()" +** used for filler content and with "PRAGMA secure_delete=ON" selected to +** zero-out deleted content. +*/ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <ctype.h> + +/* Return true if the line is all zeros */ +static int allZero(unsigned char *aLine){ + int i; + for(i=0; i<16 && aLine[i]==0; i++){} + return i==16; +} + +int main(int argc, char **argv){ + int pgsz = 0; /* page size */ + int forCli = 0; /* whether to prepend with .open */ + int bSQL = 0; /* Expect and SQL prefix */ + long szFile; /* Size of the input file in bytes */ + FILE *in; /* Input file */ + int nSQL; /* Number of bytes of script */ + int i, j; /* Loop counters */ + int nErr = 0; /* Number of errors */ + const char *zInputFile = 0; /* Name of the input file */ + const char *zBaseName = 0; /* Base name of the file */ + int lastPage = 0; /* Last page number shown */ + int iPage; /* Current page number */ + unsigned char *aData = 0; /* All data */ + unsigned char *aLine; /* A single line of the file */ + unsigned char *aHdr; /* File header */ + unsigned char bShow[256]; /* Characters ok to display */ + memset(bShow, '.', sizeof(bShow)); + for(i=' '; i<='~'; i++){ + if( i!='{' && i!='}' && i!='"' && i!='\\' ) bShow[i] = (unsigned char)i; + } + for(i=1; i<argc; i++){ + if( argv[i][0]=='-' ){ + const char *z = argv[i]; + z++; + if( z[0]=='-' ) z++; + if( strcmp(z,"pagesize")==0 ){ + i++; + pgsz = atoi(argv[i]); + if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ){ + fprintf(stderr, "Page size must be a power of two between" + " 512 and 65536.\n"); + nErr++; + } + continue; + }else if( strcmp(z,"for-cli")==0 ){ + forCli = 1; + continue; + }else if( strcmp(z,"script")==0 ){ + forCli = 1; + bSQL = 1; + continue; + } + fprintf(stderr, "Unknown option: %s\n", argv[i]); + nErr++; + }else if( zInputFile ){ + fprintf(stderr, "Already using a different input file: [%s]\n", argv[i]); + nErr++; + }else{ + zInputFile = argv[i]; + } + } + if( zInputFile==0 ){ + fprintf(stderr, "No input file specified.\n"); + nErr++; + } + if( nErr ){ + fprintf(stderr, + "Usage: %s [--pagesize N] [--script] [--for-cli] FILENAME\n", argv[0]); + exit(1); + } + in = fopen(zInputFile, "rb"); + if( in==0 ){ + fprintf(stderr, "Cannot open input file [%s]\n", zInputFile); + exit(1); + } + fseek(in, 0, SEEK_END); + szFile = ftell(in); + rewind(in); + if( szFile<100 ){ + fprintf(stderr, "File too short. Minimum size is 100 bytes.\n"); + exit(1); + } + aData = malloc( szFile+16 ); + if( aData==0 ){ + fprintf(stderr, "Failed to allocate %ld bytes\n", szFile); + exit(1); + } + if( fread(aData, szFile, 1, in)!=1 ){ + fprintf(stderr, "Cannot read file info memory\n"); + exit(1); + } + memset(aData+szFile, 0, 16); + fclose(in); + if( bSQL ){ + for(i=0; i<szFile && aData[i]!=0; i++){} + if( i==szFile ){ + fprintf(stderr, "No zero terminator on SQL script\n"); + exit(1); + } + nSQL = i+1; + if( szFile - nSQL<100 ){ + fprintf(stderr, "Less than 100 bytes in the database\n"); + exit(1); + } + }else{ + nSQL = 0; + } + aHdr = aData + nSQL; + if( pgsz==0 ){ + pgsz = (aHdr[16]<<8) | aHdr[17]; + if( pgsz==1 ) pgsz = 65536; + if( pgsz<512 || (pgsz&(pgsz-1))!=0 ){ + fprintf(stderr, "Invalid page size in header: %d\n", pgsz); + exit(1); + } + } + zBaseName = zInputFile; + for(i=0; zInputFile[i]; i++){ + if( zInputFile[i]=='/' && zInputFile[i+1]!=0 ) zBaseName = zInputFile+i+1; + } + if( forCli ){ + printf(".open --hexdb\n"); + } + printf("| size %d pagesize %d filename %s\n",(int)szFile,pgsz,zBaseName); + for(i=nSQL; i<szFile; i+=16){ + aLine = aData+i; + if( allZero(aLine) ) continue; + iPage = i/pgsz + 1; + if( lastPage!=iPage ){ + printf("| page %d offset %d\n", iPage, (iPage-1)*pgsz); + lastPage = iPage; + } + printf("| %5d:", i-(iPage-1)*pgsz); + for(j=0; j<16; j++) printf(" %02x", aLine[j]); + printf(" "); + for(j=0; j<16; j++){ + unsigned char c = (unsigned char)aLine[j]; + fputc( bShow[c], stdout); + } + fputc('\n', stdout); + } + printf("| end %s\n", zBaseName); + if( nSQL>0 ){ + printf("%s\n", aData); + } + free( aData ); + return 0; +} diff --git a/tool/dbtotxt.md b/tool/dbtotxt.md new file mode 100644 index 0000000000..f2bd7c9c25 --- /dev/null +++ b/tool/dbtotxt.md @@ -0,0 +1,56 @@ +<h1 align="center">The dbtotxt Tool</h1> + +The dbtotxt utility program reads an SQLite database file and writes its +raw binary content to screen as a hex dump for testing and debugging +purposes. + +The hex-dump output is formatted in such a way as to be easily readable +both by humans and by software. The dbtotxt utility has long been a part +of the TH3 test suite. The output of dbtotxt can be embedded in TH3 test +scripts and used to generate very specific database files, perhaps with +deliberately introduced corruption. The cov1/corrupt*.test modules in +TH3 make extensive use of dbtotxt. + +More recently (2018-12-13) the dbtotxt utility has been added to the SQLite +core and the command-line shell (CLI) has been augmented to be able to read +dbtotxt output. The CLI dot-command is: + +> .open --hexdb ?OPTIONAL-FILENAME? + +If the OPTIONAL-FILENAME is included, then content is read from that file. +If OPTIONAL-FILENAME is omitted, then the text is taken from the input stream, +terminated by the "| end" line of the dbtotxt text. This allows small test +databases to be embedded directly in scripts. Consider this example: + +> + .open --hexdb + | size 8192 pagesize 4096 filename x9.db + | page 1 offset 0 + | 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. + | 16: 10 00 01 01 00 40 20 20 00 00 00 04 00 00 00 02 .....@ ........ + | 32: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 04 ................ + | 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ + | 80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 04 ................ + | 96: 00 2e 30 38 0d 00 00 00 01 0f c0 00 0f c0 00 00 ..08............ + | 4032: 3e 01 06 17 11 11 01 69 74 61 62 6c 65 74 31 74 >......itablet1t + | 4048: 31 02 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 1.CREATE TABLE t + | 4064: 31 28 78 2c 79 20 44 45 46 41 55 4c 54 20 78 27 1(x,y DEFAULT x' + | 4080: 66 66 27 2c 7a 20 44 45 46 41 55 4c 54 20 30 29 ff',z DEFAULT 0) + | page 2 offset 4096 + | 0: 0d 08 14 00 04 00 10 00 0e 05 0a 0f 04 15 00 10 ................ + | 16: 88 02 03 05 90 04 0e 08 00 00 00 00 00 00 00 00 ................ + | 1040: 00 00 00 00 ff 87 7c 02 05 8f 78 0e 08 00 00 00 ......|...x..... + | 2064: 00 00 00 ff 0c 0a 01 fb 00 00 00 00 00 00 00 00 ................ + | 2560: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 83 ................ + | 2576: 78 01 05 87 70 0e 08 00 00 00 00 00 00 00 00 00 x...p........... + | 3072: 00 00 00 00 00 00 00 00 00 ff 00 00 01 fb 00 00 ................ + | 3584: 00 00 00 00 00 83 78 00 05 87 70 0e 08 00 00 00 ......x...p..... + | 4080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ff ................ + | end x9.db + SELECT rowid FROM t1; + PRAGMA integrity_check; + +You can run this script to see that the database file is correctly decoded +and loaded. Furthermore, you can make subtle corruptions to the input +database simply by editing the hexadecimal description, then rerun the +script to verify that SQLite correctly handles the corruption. diff --git a/tool/emcc.sh.in b/tool/emcc.sh.in new file mode 100644 index 0000000000..1264df5376 --- /dev/null +++ b/tool/emcc.sh.in @@ -0,0 +1,66 @@ +#!/bin/sh +# ^^^^^^^ Please try to keep this script Bourne-compatible. +######################################################################## +# WARNING: emcc.sh is generated from emcc.sh.in by the configure +# process. Do not edit emcc.sh directly, as it may be deleted or +# overwritten by the configure script. +# +# A wrapper around the emcc compiler which uses configure-time state +# to locate the Emscripten SDK and import the SDK's environment +# script, if needed. +######################################################################## +# EMSDK_HOME comes from the configure --with-emsdk=/dir flag. +# EMSDK_ENV_SH is ${thatDir}/emsdk_env.sh and is also set by the +# configure process. +EMSDK_HOME="@EMSDK_HOME@" +EMSDK_ENV_SH="@EMSDK_ENV_SH@" +emcc="@BIN_EMCC@" + +if [ x = "x${emcc}" ]; then + emcc=`which emcc 2>/dev/null` +fi + +if [ x = "x${emcc}" ]; then + # If emcc is not found in the path, try to find it via an emsdk + # installation. The SDK variant is the official installation style + # supported by the Emscripten project, but emcc is also available + # via package managers on some OSes. + if [ x = "x${EMSDK_HOME}" ]; then + echo "EMSDK_HOME is not set. Pass --with-emsdk=/path/to/emsdk" \ + "to the configure script." 1>&2 + exit 1 + fi + + if [ x = "x${EMSDK_ENV_SH}" ]; then + if [ -f "${EMSDK_HOME}/emsdk_env.sh" ]; then + EMSDK_ENV_SH="${EMSDK_HOME}/emsdk_env.sh" + else + echo "EMSDK_ENV_SH is not set. Expecting configure script to set it." 1>&2 + exit 2 + fi + fi + + if [ ! -f "${EMSDK_ENV_SH}" ]; then + echo "emsdk_env script not found: $EMSDK_ENV_SH" 1>&2 + exit 3 + fi + + # $EMSDK is part of the state set by emsdk_env.sh. + if [ x = "x${EMSDK}" ]; then + EMSDK_QUIET=1 + export EMSDK_QUIET + # ^^^ Squelches informational output from ${EMSDK_ENV_SH}. + source "${EMSDK_ENV_SH}" || { + rc=$? + echo "Error sourcing ${EMSDK_ENV_SH}" + exit $rc + } + fi + emcc=`which emcc 2>/dev/null` + if [ x = "x${emcc}" ]; then + echo "emcc not found in PATH. Normally that's set up by ${EMSDK_ENV_SH}." 1>&2 + exit 4 + fi +fi + +exec $emcc "$@" diff --git a/tool/enlargedb.c b/tool/enlargedb.c new file mode 100644 index 0000000000..dab5ef1f1c --- /dev/null +++ b/tool/enlargedb.c @@ -0,0 +1,68 @@ +/* +** Try to enlarge an SQLite database by appending many unused pages. +** The resulting database will fail PRAGMA integrity_check due to the +** appended unused pages, but it should work otherwise. +** +** Usage: +** +** enlargedb DATABASE N +** +** Adds N blank pages onto the end of DATABASE. N can be decimal +** or hex. The total number of pages after adding must be no greater +** than 4294967297 +*/ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +int main(int argc, char **argv){ + char *zEnd; + long long int toAppend; + long long int currentSz; + long long int newSz; + FILE *f; + size_t got; + int pgsz; + char zero = 0; + unsigned char buf[100]; + + if( argc!=3 ) goto usage_error; + toAppend = strtoll(argv[2], &zEnd, 0); + if( zEnd==argv[2] || zEnd[0] ) goto usage_error; + if( toAppend<1 ){ + fprintf(stderr, "N must be at least 1\n"); + exit(1); + } + f = fopen(argv[1], "r+b"); + if( f==0 ){ + fprintf(stderr, "cannot open \"%s\" for reading and writing\n", argv[1]); + exit(1); + } + got = fread(buf, 1, sizeof(buf), f); + if( got!=sizeof(buf) ) goto not_valid_db; + if( strcmp((char*)buf,"SQLite format 3")!=0 ) goto not_valid_db; + pgsz = (buf[16]<<8) + buf[17]; + if( pgsz==1 ) pgsz = 65536; + if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto not_valid_db; + currentSz = (buf[28]<<24) + (buf[29]<<16) + (buf[30]<<8) + buf[31]; + newSz = currentSz + toAppend; + if( newSz > 0xffffffff ) newSz = 0xffffffff; + buf[28] = (newSz>>24) & 0xff; + buf[29] = (newSz>>16) & 0xff; + buf[30] = (newSz>>8) & 0xff; + buf[31] = newSz & 0xff; + fseek(f, 28, SEEK_SET); + fwrite(&buf[28],4,1,f); + fseek(f, (long)(newSz*pgsz - 1), SEEK_SET); + fwrite(&zero,1,1,f); + fclose(f); + return 0; + +not_valid_db: + fprintf(stderr,"not a valid database: %s\n", argv[1]); + exit(1); + +usage_error: + fprintf(stderr,"Usage: %s DATABASE N\n", argv[0]); + exit(1); +} diff --git a/tool/extract-sqlite3h.tcl b/tool/extract-sqlite3h.tcl new file mode 100755 index 0000000000..a0f7c4e389 --- /dev/null +++ b/tool/extract-sqlite3h.tcl @@ -0,0 +1,21 @@ +#!/usr/bin/tclsh +# +# Given an sqlite3.c source file identified by the command-line +# argument, extract the "sqlite3.h" header file that is embedded inside +# the sqlite3.c source file and write it to standard output. +# +if {[llength $argv]!=1} { + puts stderr "Usage: $argv0 sqlite3.c >sqlite3.h" + exit 1 +} +set in [open [lindex $argv 0] rb] +while {![eof $in]} { + set line [gets $in] + if {[string match {* Begin file sqlite3.h *} $line]} break +} +while {![eof $in]} { + set line [gets $in] + if {[string match {* End of sqlite3.h *} $line]} break + puts $line +} +close $in diff --git a/tool/fast_vacuum.c b/tool/fast_vacuum.c index 6a50dcc680..5ca0271dc9 100644 --- a/tool/fast_vacuum.c +++ b/tool/fast_vacuum.c @@ -150,7 +150,7 @@ int main(int argc, char **argv){ */ /* The vacuum will occur inside of a transaction. Set writable_schema - ** to ON so that we can directly update the sqlite_master table in the + ** to ON so that we can directly update the sqlite_schema table in the ** zTempDb database. */ execSql(db, "PRAGMA writable_schema=ON"); @@ -162,16 +162,16 @@ int main(int argc, char **argv){ */ execExecSql(db, "SELECT 'CREATE TABLE vacuum_db.' || substr(sql,14) " - " FROM sqlite_master WHERE type='table' AND name!='sqlite_sequence'" + " FROM sqlite_schema WHERE type='table' AND name!='sqlite_sequence'" " AND rootpage>0" ); execExecSql(db, "SELECT 'CREATE INDEX vacuum_db.' || substr(sql,14)" - " FROM sqlite_master WHERE sql LIKE 'CREATE INDEX %'" + " FROM sqlite_schema WHERE sql LIKE 'CREATE INDEX %'" ); execExecSql(db, "SELECT 'CREATE UNIQUE INDEX vacuum_db.' || substr(sql,21) " - " FROM sqlite_master WHERE sql LIKE 'CREATE UNIQUE INDEX %'" + " FROM sqlite_schema WHERE sql LIKE 'CREATE UNIQUE INDEX %'" ); /* Loop through the tables in the main database. For each, do @@ -181,7 +181,7 @@ int main(int argc, char **argv){ execExecSql(db, "SELECT 'INSERT INTO vacuum_db.' || quote(name) " "|| ' SELECT * FROM main.' || quote(name) " - "FROM main.sqlite_master " + "FROM main.sqlite_schema " "WHERE type = 'table' AND name!='sqlite_sequence' " " AND rootpage>0" ); @@ -190,12 +190,12 @@ int main(int argc, char **argv){ */ execExecSql(db, "SELECT 'DELETE FROM vacuum_db.' || quote(name) " - "FROM vacuum_db.sqlite_master WHERE name='sqlite_sequence'" + "FROM vacuum_db.sqlite_schema WHERE name='sqlite_sequence'" ); execExecSql(db, "SELECT 'INSERT INTO vacuum_db.' || quote(name) " "|| ' SELECT * FROM main.' || quote(name) " - "FROM vacuum_db.sqlite_master WHERE name=='sqlite_sequence'" + "FROM vacuum_db.sqlite_schema WHERE name=='sqlite_sequence'" ); /* Copy the triggers, views, and virtual tables from the main database @@ -204,9 +204,9 @@ int main(int argc, char **argv){ ** from the SQLITE_MASTER table. */ execSql(db, - "INSERT INTO vacuum_db.sqlite_master " + "INSERT INTO vacuum_db.sqlite_schema " " SELECT type, name, tbl_name, rootpage, sql" - " FROM main.sqlite_master" + " FROM main.sqlite_schema" " WHERE type='view' OR type='trigger'" " OR (type='table' AND rootpage=0)" ); diff --git a/tool/fuzzershell.c b/tool/fuzzershell.c index 2c778bce6e..7a7aef0290 100644 --- a/tool/fuzzershell.c +++ b/tool/fuzzershell.c @@ -195,12 +195,14 @@ static int execCallback(void *NotUsed, int argc, char **argv, char **colv){ int i; static unsigned cnt = 0; printf("ROW #%u:\n", ++cnt); - for(i=0; i<argc; i++){ - printf(" %s=", colv[i]); - if( argv[i] ){ - printf("[%s]\n", argv[i]); - }else{ - printf("NULL\n"); + if( argv ){ + for(i=0; i<argc; i++){ + printf(" %s=", colv[i]); + if( argv[i] ){ + printf("[%s]\n", argv[i]); + }else{ + printf("NULL\n"); + } } } fflush(stdout); @@ -224,6 +226,56 @@ static void traceNoop(void *NotUsed, const char *zMsg){ } #endif +/*************************************************************************** +** String accumulator object +*/ +typedef struct Str Str; +struct Str { + char *z; /* The string. Memory from malloc() */ + sqlite3_uint64 n; /* Bytes of input used */ + sqlite3_uint64 nAlloc; /* Bytes allocated to z[] */ + int oomErr; /* OOM error has been seen */ +}; + +/* Initialize a Str object */ +static void StrInit(Str *p){ + memset(p, 0, sizeof(*p)); +} + +/* Append text to the end of a Str object */ +static void StrAppend(Str *p, const char *z){ + sqlite3_uint64 n = strlen(z); + if( p->n + n >= p->nAlloc ){ + char *zNew; + sqlite3_uint64 nNew; + if( p->oomErr ) return; + nNew = p->nAlloc*2 + 100 + n; + zNew = sqlite3_realloc(p->z, (int)nNew); + if( zNew==0 ){ + sqlite3_free(p->z); + memset(p, 0, sizeof(*p)); + p->oomErr = 1; + return; + } + p->z = zNew; + p->nAlloc = nNew; + } + memcpy(p->z + p->n, z, (size_t)n); + p->n += n; + p->z[p->n] = 0; +} + +/* Return the current string content */ +static char *StrStr(Str *p){ + return p->z; +} + +/* Free the string */ +static void StrFree(Str *p){ + sqlite3_free(p->z); + StrInit(p); +} + /*************************************************************************** ** eval() implementation copied from ../ext/misc/eval.c */ @@ -312,6 +364,331 @@ static void sqlEvalFunc( /* End of the eval() implementation ******************************************************************************/ +/****************************************************************************** +** The generate_series(START,END,STEP) eponymous table-valued function. +** +** This code is copy/pasted from ext/misc/series.c in the SQLite source tree. +*/ +/* series_cursor is a subclass of sqlite3_vtab_cursor which will +** serve as the underlying representation of a cursor that scans +** over rows of the result +*/ +typedef struct series_cursor series_cursor; +struct series_cursor { + sqlite3_vtab_cursor base; /* Base class - must be first */ + int isDesc; /* True to count down rather than up */ + sqlite3_int64 iRowid; /* The rowid */ + sqlite3_int64 iValue; /* Current value ("value") */ + sqlite3_int64 mnValue; /* Mimimum value ("start") */ + sqlite3_int64 mxValue; /* Maximum value ("stop") */ + sqlite3_int64 iStep; /* Increment ("step") */ +}; + +/* +** The seriesConnect() method is invoked to create a new +** series_vtab that describes the generate_series virtual table. +** +** Think of this routine as the constructor for series_vtab objects. +** +** All this routine needs to do is: +** +** (1) Allocate the series_vtab object and initialize all fields. +** +** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the +** result set of queries against generate_series will look like. +*/ +static int seriesConnect( + sqlite3 *db, + void *pAux, + int argc, const char *const*argv, + sqlite3_vtab **ppVtab, + char **pzErr +){ + sqlite3_vtab *pNew; + int rc; + +/* Column numbers */ +#define SERIES_COLUMN_VALUE 0 +#define SERIES_COLUMN_START 1 +#define SERIES_COLUMN_STOP 2 +#define SERIES_COLUMN_STEP 3 + + rc = sqlite3_declare_vtab(db, + "CREATE TABLE x(value,start hidden,stop hidden,step hidden)"); + if( rc==SQLITE_OK ){ + pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) ); + if( pNew==0 ) return SQLITE_NOMEM; + memset(pNew, 0, sizeof(*pNew)); + } + return rc; +} + +/* +** This method is the destructor for series_cursor objects. +*/ +static int seriesDisconnect(sqlite3_vtab *pVtab){ + sqlite3_free(pVtab); + return SQLITE_OK; +} + +/* +** Constructor for a new series_cursor object. +*/ +static int seriesOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){ + series_cursor *pCur; + pCur = sqlite3_malloc( sizeof(*pCur) ); + if( pCur==0 ) return SQLITE_NOMEM; + memset(pCur, 0, sizeof(*pCur)); + *ppCursor = &pCur->base; + return SQLITE_OK; +} + +/* +** Destructor for a series_cursor. +*/ +static int seriesClose(sqlite3_vtab_cursor *cur){ + sqlite3_free(cur); + return SQLITE_OK; +} + + +/* +** Advance a series_cursor to its next row of output. +*/ +static int seriesNext(sqlite3_vtab_cursor *cur){ + series_cursor *pCur = (series_cursor*)cur; + if( pCur->isDesc ){ + pCur->iValue -= pCur->iStep; + }else{ + pCur->iValue += pCur->iStep; + } + pCur->iRowid++; + return SQLITE_OK; +} + +/* +** Return values of columns for the row at which the series_cursor +** is currently pointing. +*/ +static int seriesColumn( + sqlite3_vtab_cursor *cur, /* The cursor */ + sqlite3_context *ctx, /* First argument to sqlite3_result_...() */ + int i /* Which column to return */ +){ + series_cursor *pCur = (series_cursor*)cur; + sqlite3_int64 x = 0; + switch( i ){ + case SERIES_COLUMN_START: x = pCur->mnValue; break; + case SERIES_COLUMN_STOP: x = pCur->mxValue; break; + case SERIES_COLUMN_STEP: x = pCur->iStep; break; + default: x = pCur->iValue; break; + } + sqlite3_result_int64(ctx, x); + return SQLITE_OK; +} + +/* +** Return the rowid for the current row. In this implementation, the +** rowid is the same as the output value. +*/ +static int seriesRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ + series_cursor *pCur = (series_cursor*)cur; + *pRowid = pCur->iRowid; + return SQLITE_OK; +} + +/* +** Return TRUE if the cursor has been moved off of the last +** row of output. +*/ +static int seriesEof(sqlite3_vtab_cursor *cur){ + series_cursor *pCur = (series_cursor*)cur; + if( pCur->isDesc ){ + return pCur->iValue < pCur->mnValue; + }else{ + return pCur->iValue > pCur->mxValue; + } +} + +/* True to cause run-time checking of the start=, stop=, and/or step= +** parameters. The only reason to do this is for testing the +** constraint checking logic for virtual tables in the SQLite core. +*/ +#ifndef SQLITE_SERIES_CONSTRAINT_VERIFY +# define SQLITE_SERIES_CONSTRAINT_VERIFY 0 +#endif + +/* +** This method is called to "rewind" the series_cursor object back +** to the first row of output. This method is always called at least +** once prior to any call to seriesColumn() or seriesRowid() or +** seriesEof(). +** +** The query plan selected by seriesBestIndex is passed in the idxNum +** parameter. (idxStr is not used in this implementation.) idxNum +** is a bitmask showing which constraints are available: +** +** 1: start=VALUE +** 2: stop=VALUE +** 4: step=VALUE +** +** Also, if bit 8 is set, that means that the series should be output +** in descending order rather than in ascending order. +** +** This routine should initialize the cursor and position it so that it +** is pointing at the first row, or pointing off the end of the table +** (so that seriesEof() will return true) if the table is empty. +*/ +static int seriesFilter( + sqlite3_vtab_cursor *pVtabCursor, + int idxNum, const char *idxStr, + int argc, sqlite3_value **argv +){ + series_cursor *pCur = (series_cursor *)pVtabCursor; + int i = 0; + if( idxNum & 1 ){ + pCur->mnValue = sqlite3_value_int64(argv[i++]); + }else{ + pCur->mnValue = 0; + } + if( idxNum & 2 ){ + pCur->mxValue = sqlite3_value_int64(argv[i++]); + }else{ + pCur->mxValue = 0xffffffff; + } + if( idxNum & 4 ){ + pCur->iStep = sqlite3_value_int64(argv[i++]); + if( pCur->iStep<1 ) pCur->iStep = 1; + }else{ + pCur->iStep = 1; + } + if( idxNum & 8 ){ + pCur->isDesc = 1; + pCur->iValue = pCur->mxValue; + if( pCur->iStep>0 ){ + pCur->iValue -= (pCur->mxValue - pCur->mnValue)%pCur->iStep; + } + }else{ + pCur->isDesc = 0; + pCur->iValue = pCur->mnValue; + } + pCur->iRowid = 1; + return SQLITE_OK; +} + +/* +** SQLite will invoke this method one or more times while planning a query +** that uses the generate_series virtual table. This routine needs to create +** a query plan for each invocation and compute an estimated cost for that +** plan. +** +** In this implementation idxNum is used to represent the +** query plan. idxStr is unused. +** +** The query plan is represented by bits in idxNum: +** +** (1) start = $value -- constraint exists +** (2) stop = $value -- constraint exists +** (4) step = $value -- constraint exists +** (8) output in descending order +*/ +static int seriesBestIndex( + sqlite3_vtab *tab, + sqlite3_index_info *pIdxInfo +){ + int i; /* Loop over constraints */ + int idxNum = 0; /* The query plan bitmask */ + int startIdx = -1; /* Index of the start= constraint, or -1 if none */ + int stopIdx = -1; /* Index of the stop= constraint, or -1 if none */ + int stepIdx = -1; /* Index of the step= constraint, or -1 if none */ + int nArg = 0; /* Number of arguments that seriesFilter() expects */ + + const struct sqlite3_index_constraint *pConstraint; + pConstraint = pIdxInfo->aConstraint; + for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){ + if( pConstraint->usable==0 ) continue; + if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue; + switch( pConstraint->iColumn ){ + case SERIES_COLUMN_START: + startIdx = i; + idxNum |= 1; + break; + case SERIES_COLUMN_STOP: + stopIdx = i; + idxNum |= 2; + break; + case SERIES_COLUMN_STEP: + stepIdx = i; + idxNum |= 4; + break; + } + } + if( startIdx>=0 ){ + pIdxInfo->aConstraintUsage[startIdx].argvIndex = ++nArg; + pIdxInfo->aConstraintUsage[startIdx].omit= !SQLITE_SERIES_CONSTRAINT_VERIFY; + } + if( stopIdx>=0 ){ + pIdxInfo->aConstraintUsage[stopIdx].argvIndex = ++nArg; + pIdxInfo->aConstraintUsage[stopIdx].omit = !SQLITE_SERIES_CONSTRAINT_VERIFY; + } + if( stepIdx>=0 ){ + pIdxInfo->aConstraintUsage[stepIdx].argvIndex = ++nArg; + pIdxInfo->aConstraintUsage[stepIdx].omit = !SQLITE_SERIES_CONSTRAINT_VERIFY; + } + if( (idxNum & 3)==3 ){ + /* Both start= and stop= boundaries are available. This is the + ** the preferred case */ + pIdxInfo->estimatedCost = (double)(2 - ((idxNum&4)!=0)); + pIdxInfo->estimatedRows = 1000; + if( pIdxInfo->nOrderBy==1 ){ + if( pIdxInfo->aOrderBy[0].desc ) idxNum |= 8; + pIdxInfo->orderByConsumed = 1; + } + }else{ + /* If either boundary is missing, we have to generate a huge span + ** of numbers. Make this case very expensive so that the query + ** planner will work hard to avoid it. */ + pIdxInfo->estimatedCost = (double)2147483647; + pIdxInfo->estimatedRows = 2147483647; + } + pIdxInfo->idxNum = idxNum; + return SQLITE_OK; +} + +/* +** This following structure defines all the methods for the +** generate_series virtual table. +*/ +static sqlite3_module seriesModule = { + 0, /* iVersion */ + 0, /* xCreate */ + seriesConnect, /* xConnect */ + seriesBestIndex, /* xBestIndex */ + seriesDisconnect, /* xDisconnect */ + 0, /* xDestroy */ + seriesOpen, /* xOpen - open a cursor */ + seriesClose, /* xClose - close a cursor */ + seriesFilter, /* xFilter - configure scan constraints */ + seriesNext, /* xNext - advance a cursor */ + seriesEof, /* xEof - check for end of scan */ + seriesColumn, /* xColumn - read data */ + seriesRowid, /* xRowid - read data */ + 0, /* xUpdate */ + 0, /* xBegin */ + 0, /* xSync */ + 0, /* xCommit */ + 0, /* xRollback */ + 0, /* xFindMethod */ + 0, /* xRename */ + 0, /* xSavepoint */ + 0, /* xRelease */ + 0, /* xRollbackTo */ + 0, /* xShadowName */ + 0 /* xIntegrity */ +}; +/* END the generate_series(START,END,STEP) implementation +*********************************************************************************/ + /* ** Print sketchy documentation for this utility program */ @@ -706,6 +1083,8 @@ int main(int argc, char **argv){ oomCnt = 0; } do{ + Str sql; + StrInit(&sql); if( zDbName ){ rc = sqlite3_open_v2(zDbName, &db, SQLITE_OPEN_READWRITE, 0); if( rc!=SQLITE_OK ){ @@ -729,11 +1108,31 @@ int main(int argc, char **argv){ #endif sqlite3_create_function(db, "eval", 1, SQLITE_UTF8, 0, sqlEvalFunc, 0, 0); sqlite3_create_function(db, "eval", 2, SQLITE_UTF8, 0, sqlEvalFunc, 0, 0); + sqlite3_create_module(db, "generate_series", &seriesModule, 0); sqlite3_limit(db, SQLITE_LIMIT_LENGTH, 1000000); if( zEncoding ) sqlexec(db, "PRAGMA encoding=%s", zEncoding); if( pageSize ) sqlexec(db, "PRAGMA pagesize=%d", pageSize); if( doAutovac ) sqlexec(db, "PRAGMA auto_vacuum=FULL"); iStart = timeOfDay(); + + /* If using an input database file and that database contains a table + ** named "autoexec" with a column "sql", then replace the input SQL + ** with the concatenated text of the autoexec table. In this way, + ** if the database file is the input being fuzzed, the SQL text is + ** fuzzed at the same time. */ + if( sqlite3_table_column_metadata(db,0,"autoexec","sql",0,0,0,0,0)==0 ){ + sqlite3_stmt *pStmt2; + rc = sqlite3_prepare_v2(db,"SELECT sql FROM autoexec",-1,&pStmt2,0); + if( rc==SQLITE_OK ){ + while( sqlite3_step(pStmt2)==SQLITE_ROW ){ + StrAppend(&sql, (const char*)sqlite3_column_text(pStmt2, 0)); + StrAppend(&sql, "\n"); + } + } + sqlite3_finalize(pStmt2); + zSql = StrStr(&sql); + } + g.bOomEnable = 1; if( verboseFlag ){ zErrMsg = 0; @@ -747,6 +1146,7 @@ int main(int argc, char **argv){ } g.bOomEnable = 0; iEnd = timeOfDay(); + StrFree(&sql); rc = sqlite3_close(db); if( rc ){ abendError("sqlite3_close() failed with rc=%d", rc); diff --git a/tool/genfkey.README b/tool/genfkey.README index 57cdff87f8..5609945042 100644 --- a/tool/genfkey.README +++ b/tool/genfkey.README @@ -63,7 +63,7 @@ CAPABILITIES LIMITATIONS - Apart from those limitiations described above: + Apart from those limitations described above: * Implicit mapping to composite primary keys is not supported. If a parent table has a composite primary key, then any child table @@ -108,7 +108,7 @@ USAGE If errors are found and the --ignore-errors option is passed, then no error messages are printed. No "CREATE TRIGGER" statements are generated - for foriegn-key definitions that contained errors, they are silently + for foreign-key definitions that contained errors, they are silently ignored by subsequent processing. All triggers generated by this command have names that match the pattern @@ -129,7 +129,7 @@ USAGE they will be printed to stdout, but this can be configured using other dot-commands (i.e. ".output"). - The simplest way to activate the foriegn key definitions in a database + The simplest way to activate the foreign key definitions in a database is simply to open it using the shell tool and enter the command ".genfkey --exec": diff --git a/tool/genfkey.test b/tool/genfkey.test index 3c0073e953..16c67bb3cc 100644 --- a/tool/genfkey.test +++ b/tool/genfkey.test @@ -351,4 +351,3 @@ do_test genfkey-6.7 { SELECT * FROM child; } } {1 1} - diff --git a/tool/index_usage.c b/tool/index_usage.c new file mode 100644 index 0000000000..9bd3c9fdce --- /dev/null +++ b/tool/index_usage.c @@ -0,0 +1,233 @@ +/* +** 2018-12-04 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** This file implements a utility program used to help determine which +** indexes in a database schema are used and unused, and how often specific +** indexes are used. +*/ +#include "sqlite3.h" +#include <stdio.h> +#include <stdlib.h> +#include <assert.h> +#include <string.h> + +static void usage(const char *argv0){ + printf("Usage: %s [OPTIONS] DATABASE LOG\n\n", argv0); + printf( + "DATABASE is an SQLite database against which various statements\n" + "have been run. The SQL text is stored in LOG. LOG is an SQLite\n" + "database with this schema:\n" + "\n" + " CREATE TABLE sqllog(sql TEXT);\n" + "\n" + "This utility program analyzes statements contained in LOG and prints\n" + "a report showing how many times each index in DATABASE is used by the\n" + "statements in LOG.\n" + "\n" + "DATABASE only needs to contain the schema used by the statements in\n" + "LOG. The content can be removed from DATABASE.\n" + ); + printf( + "\nOPTIONS:\n\n" + " --progress N Show a progress message after every N input rows\n" + " -q Omit error message when parsing log entries\n" + " --using NAME Print SQL statements that use index NAME\n" + ); + printf("\nAnalysis will be done by SQLite version %s dated %.20s\n" + "checkin number %.40s. Different versions\n" + "of SQLite might use different indexes.\n", + sqlite3_libversion(), sqlite3_sourceid(), sqlite3_sourceid()+21); + exit(1); +} + +int main(int argc, char **argv){ + sqlite3 *db = 0; /* The main database */ + sqlite3_stmt *pStmt = 0; /* a query */ + char *zSql; + int nErr = 0; + int rc; + int bQuiet = 0; + int i, j; + const char *zUsing = 0; + sqlite3_stmt *pIncrCnt = 0; + int nRow = 0; + int iProgress = 0; + + for(i=j=1; i<argc; i++){ + const char *z = argv[i]; + if( z[0]=='-' ){ + z++; + if( z[0]=='-' ) z++; + if( strcmp(z,"progress")==0 ){ + if( i+1<argc ){ + iProgress = strtol(argv[++i],0,0); + continue; + } + printf("The --progress option requires an argument\n"); + exit(0); + } + if( strcmp(z,"q")==0 ){ + bQuiet = 1; + continue; + } + if( strcmp(z,"using")==0 ){ + if( i+1<argc ){ + zUsing = argv[++i]; + continue; + } + printf("The --using option requires an argument\n"); + exit(0); + } + if( strcmp(z, "help")==0 || strcmp(z, "?")==0 ){ + usage(argv[0]); + } + printf("Unknown command-line option: \"%s\"\n", argv[i]); + exit(0); + }else{ + if( j<i ) argv[j++] = argv[i]; + } + } + argc = j; + + if( argc!=3 ) usage(argv[0]); + rc = sqlite3_open_v2(argv[1], &db, SQLITE_OPEN_READONLY, 0); + if( rc ){ + printf("Cannot open \"%s\" for reading: %s\n", argv[1], sqlite3_errmsg(db)); + goto errorOut; + } + rc = sqlite3_prepare_v2(db, "SELECT * FROM sqlite_schema", -1, &pStmt, 0); + if( rc ){ + printf("Cannot read the schema from \"%s\" - %s\n", argv[1], + sqlite3_errmsg(db)); + goto errorOut; + } + sqlite3_finalize(pStmt); + pStmt = 0; + rc = sqlite3_exec(db, + "CREATE TABLE temp.idxu(\n" + " tbl TEXT COLLATE nocase,\n" + " idx TEXT COLLATE nocase,\n" + " cnt INT,\n" + " PRIMARY KEY(idx)\n" + ") WITHOUT ROWID;", 0, 0, 0); + if( rc ){ + printf("Cannot create the result table - %s\n", + sqlite3_errmsg(db)); + goto errorOut; + } + rc = sqlite3_exec(db, + "INSERT INTO temp.idxu(tbl,idx,cnt)" + " SELECT tbl_name, name, 0 FROM sqlite_schema" + " WHERE type='index' AND sql IS NOT NULL", 0, 0, 0); + + /* Open the LOG database */ + zSql = sqlite3_mprintf("ATTACH %Q AS log", argv[2]); + rc = sqlite3_exec(db, zSql, 0, 0, 0); + sqlite3_free(zSql); + if( rc ){ + printf("Cannot open the LOG database \"%s\" - %s\n", + argv[2], sqlite3_errmsg(db)); + goto errorOut; + } + rc = sqlite3_prepare_v2(db, + "SELECT sql, rowid FROM log.sqllog" + " WHERE upper(substr(sql,1,5)) NOT IN ('BEGIN','COMMI','ROLLB','PRAGM')", + -1, &pStmt, 0); + if( rc ){ + printf("Cannot read the SQLLOG table in the LOG database \"%s\" - %s\n", + argv[2], sqlite3_errmsg(db)); + goto errorOut; + } + + rc = sqlite3_prepare_v2(db, + "UPDATE temp.idxu SET cnt=cnt+1 WHERE idx=?1", + -1, &pIncrCnt, 0); + if( rc ){ + printf("Cannot prepare a statement to increment a counter for " + "indexes used\n"); + goto errorOut; + } + + /* Update the counts based on LOG */ + while( sqlite3_step(pStmt)==SQLITE_ROW ){ + const char *zLog = (const char*)sqlite3_column_text(pStmt, 0); + sqlite3_stmt *pS2; + if( zLog==0 ) continue; + zSql = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zLog); + rc = sqlite3_prepare_v2(db, zSql, -1, &pS2, 0); + sqlite3_free(zSql); + if( rc ){ + if( !bQuiet ){ + printf("Cannot compile LOG entry %d (%s): %s\n", + sqlite3_column_int(pStmt, 1), zLog, sqlite3_errmsg(db)); + fflush(stdout); + } + nErr++; + }else{ + nRow++; + if( iProgress>0 && (nRow%iProgress)==0 ){ + printf("%d...\n", nRow); + fflush(stdout); + } + while( sqlite3_step(pS2)==SQLITE_ROW ){ + const char *zExplain = (const char*)sqlite3_column_text(pS2,3); + const char *z1, *z2; + int n; + /* printf("EXPLAIN: %s\n", zExplain); */ + z1 = strstr(zExplain, " USING INDEX "); + if( z1==0 ) continue; + z1 += 13; + for(z2=z1+1; z2[0] && z2[1]!='('; z2++){} + n = z2 - z1; + if( zUsing && sqlite3_strnicmp(zUsing, z1, n)==0 ){ + printf("Using %s:\n%s\n", zUsing, zLog); + fflush(stdout); + } + sqlite3_bind_text(pIncrCnt,1,z1,n,SQLITE_STATIC); + sqlite3_step(pIncrCnt); + sqlite3_reset(pIncrCnt); + } + } + sqlite3_finalize(pS2); + } + sqlite3_finalize(pStmt); + + /* Generate the report */ + rc = sqlite3_prepare_v2(db, + "SELECT tbl, idx, cnt, " + " (SELECT group_concat(name,',') FROM pragma_index_info(idx))" + " FROM temp.idxu, main.sqlite_schema" + " WHERE temp.idxu.tbl=main.sqlite_schema.tbl_name" + " AND temp.idxu.idx=main.sqlite_schema.name" + " ORDER BY cnt DESC, tbl, idx", + -1, &pStmt, 0); + if( rc ){ + printf("Cannot query the result table - %s\n", + sqlite3_errmsg(db)); + goto errorOut; + } + while( sqlite3_step(pStmt)==SQLITE_ROW ){ + printf("%10d %s on %s(%s)\n", + sqlite3_column_int(pStmt, 2), + sqlite3_column_text(pStmt, 1), + sqlite3_column_text(pStmt, 0), + sqlite3_column_text(pStmt, 3)); + } + sqlite3_finalize(pStmt); + pStmt = 0; + +errorOut: + sqlite3_finalize(pIncrCnt); + sqlite3_finalize(pStmt); + sqlite3_close(db); + return nErr; +} diff --git a/tool/lemon.c b/tool/lemon.c index d704deb624..324dda0c5f 100644 --- a/tool/lemon.c +++ b/tool/lemon.c @@ -48,6 +48,7 @@ extern int access(const char *path, int mode); #define MAXRHS 1000 #endif +extern void memory_error(); static int showPrecedenceConflict = 0; static char *msort(char*,char**,int(*)(const char*,const char*)); @@ -58,6 +59,82 @@ static char *msort(char*,char**,int(*)(const char*,const char*)); */ #define lemonStrlen(X) ((int)strlen(X)) +/* +** Header on the linked list of memory allocations. +*/ +typedef struct MemChunk MemChunk; +struct MemChunk { + MemChunk *pNext; + size_t sz; + /* Actually memory follows */ +}; + +/* +** Global linked list of all memory allocations. +*/ +static MemChunk *memChunkList = 0; + +/* +** Wrappers around malloc(), calloc(), realloc() and free(). +** +** All memory allocations are kept on a doubly-linked list. The +** lemon_free_all() function can be called prior to exit to clean +** up any memory leaks. +** +** This is not necessary. But compilers and getting increasingly +** fussy about memory leaks, even in command-line programs like Lemon +** where they do not matter. So this code is provided to hush the +** warnings. +*/ +static void *lemon_malloc(size_t nByte){ + MemChunk *p; + if( nByte<0 ) return 0; + p = malloc( nByte + sizeof(MemChunk) ); + if( p==0 ){ + fprintf(stderr, "Out of memory. Failed to allocate %lld bytes.\n", + (long long int)nByte); + exit(1); + } + p->pNext = memChunkList; + p->sz = nByte; + memChunkList = p; + return (void*)&p[1]; +} +static void *lemon_calloc(size_t nElem, size_t sz){ + void *p = lemon_malloc(nElem*sz); + memset(p, 0, nElem*sz); + return p; +} +static void lemon_free(void *pOld){ + if( pOld ){ + MemChunk *p = (MemChunk*)pOld; + p--; + memset(pOld, 0, p->sz); + } +} +static void *lemon_realloc(void *pOld, size_t nNew){ + void *pNew; + MemChunk *p; + if( pOld==0 ) return lemon_malloc(nNew); + p = (MemChunk*)pOld; + p--; + if( p->sz>=nNew ) return pOld; + pNew = lemon_malloc( nNew ); + memcpy(pNew, pOld, p->sz); + return pNew; +} + +/* Free all outstanding memory allocations. +** Do this right before exiting. +*/ +static void lemon_free_all(void){ + while( memChunkList ){ + MemChunk *pNext = memChunkList->pNext; + free( memChunkList ); + memChunkList = pNext; + } +} + /* ** Compilers are starting to complain about the use of sprintf() and strcpy(), ** saying they are unsafe. So we define our own versions of those routines too. @@ -168,12 +245,12 @@ static struct action *Action_new(void); static struct action *Action_sort(struct action *); /********** From the file "build.h" ************************************/ -void FindRulePrecedences(); -void FindFirstSets(); -void FindStates(); -void FindLinks(); -void FindFollowSets(); -void FindActions(); +void FindRulePrecedences(struct lemon*); +void FindFirstSets(struct lemon*); +void FindStates(struct lemon*); +void FindLinks(struct lemon*); +void FindFollowSets(struct lemon*); +void FindActions(struct lemon*); /********* From the file "configlist.h" *********************************/ void Configlist_init(void); @@ -217,7 +294,7 @@ void Plink_delete(struct plink *); /********** From the file "report.h" *************************************/ void Reprint(struct lemon *); void ReportOutput(struct lemon *); -void ReportTable(struct lemon *, int); +void ReportTable(struct lemon *, int, int); void ReportHeader(struct lemon *); void CompressTables(struct lemon *); void ResortStates(struct lemon *); @@ -263,12 +340,15 @@ struct symbol { int useCnt; /* Number of times used */ char *destructor; /* Code which executes whenever this symbol is ** popped from the stack during error processing */ - int destLineno; /* Line number for start of destructor */ + int destLineno; /* Line number for start of destructor. Set to + ** -1 for duplicate destructors. */ char *datatype; /* The data type of information held by this ** object. Only used if type==NONTERMINAL */ int dtnum; /* The data type number. In the parser, the value ** stack is a union. The .yy%d element of this ** union is the correct data type for this object */ + int bContent; /* True if this symbol ever carries content - if + ** it is ever more than just syntax */ /* The following fields are used by MULTITERMINALs only */ int nsubsym; /* Number of constituent symbols in the MULTI */ struct symbol **subsym; /* Array of constituent symbols */ @@ -286,9 +366,17 @@ struct rule { const char **rhsalias; /* An alias for each RHS symbol (NULL if none) */ int line; /* Line number at which code begins */ const char *code; /* The code executed when this rule is reduced */ + const char *codePrefix; /* Setup code before code[] above */ + const char *codeSuffix; /* Breakdown code after code[] above */ struct symbol *precsym; /* Precedence symbol for this rule */ int index; /* An index number for this rule */ + int iRule; /* Rule number as used in the generated tables */ + Boolean noCode; /* True if this rule has no associated C code */ + Boolean codeEmitted; /* True if the code has been emitted already */ Boolean canReduce; /* True if this rule is ever reduced */ + Boolean doesReduce; /* Reduce actions occur after optimization */ + Boolean neverReduce; /* Reduce is theoretically possible, but prevented + ** by actions or other outside implementation */ struct rule *nextlhs; /* Next rule with the same LHS */ struct rule *next; /* Next rule in the global list */ }; @@ -336,6 +424,7 @@ struct action { struct state *stp; /* The new state, if a shift */ struct rule *rp; /* The rule, if a reduce */ } x; + struct symbol *spOpt; /* SHIFTREDUCE optimization to this symbol */ struct action *next; /* Next action for this state */ struct action *collide; /* Next action with the same hash */ }; @@ -346,7 +435,7 @@ struct state { struct config *bp; /* The basis configurations for this state */ struct config *cfp; /* All configurations in this set */ int statenum; /* Sequential number for this state */ - struct action *ap; /* Array of actions for this state */ + struct action *ap; /* List of actions for this state */ int nTknAct, nNtAct; /* Number of actions on terminals and nonterminals */ int iTknOfst, iNtOfst; /* yy_action[] offset for terminals and nonterms */ int iDfltReduce; /* Default action is to REDUCE by this rule */ @@ -370,17 +459,26 @@ struct plink { struct lemon { struct state **sorted; /* Table of states sorted by state number */ struct rule *rule; /* List of all rules */ + struct rule *startRule; /* First rule */ int nstate; /* Number of states */ int nxstate; /* nstate with tail degenerate states removed */ int nrule; /* Number of rules */ + int nruleWithAction; /* Number of rules with actions */ int nsymbol; /* Number of terminal and nonterminal symbols */ int nterminal; /* Number of terminal symbols */ + int minShiftReduce; /* Minimum shift-reduce action value */ + int errAction; /* Error action value */ + int accAction; /* Accept action value */ + int noAction; /* No-op action value */ + int minReduce; /* Minimum reduce action */ + int maxAction; /* Maximum action value of any kind */ struct symbol **symbols; /* Sorted array of pointers to symbols */ int errorcnt; /* Number of errors */ struct symbol *errsym; /* The error symbol */ struct symbol *wildcard; /* Token that matches anything */ char *name; /* Name of the generated parser */ - char *arg; /* Declaration of the 3th argument to parser */ + char *arg; /* Declaration of the 3rd argument to parser */ + char *ctx; /* Declaration of 2nd argument to constructor */ char *tokentype; /* Type of terminal symbols in the parser stack */ char *vartype; /* The default type of non-terminal symbols */ char *start; /* Name of the start symbol for the grammar */ @@ -396,13 +494,18 @@ struct lemon { char *filename; /* Name of the input file */ char *outname; /* Name of the current output file */ char *tokenprefix; /* A prefix added to token names in the .h file */ + char *reallocFunc; /* Function to use to allocate stack space */ + char *freeFunc; /* Function to use to free stack space */ int nconflict; /* Number of parsing conflicts */ int nactiontab; /* Number of entries in the yy_action[] table */ + int nlookaheadtab; /* Number of entries in yy_lookahead[] */ int tablesize; /* Total table size of all tables in bytes */ int basisflag; /* Print only basis configurations */ + int printPreprocessed; /* Show preprocessor output on stdout */ int has_fallback; /* True if any %fallback is seen in the grammar */ int nolinenosflag; /* True if #line statements should not be printed */ - char *argv0; /* Name of the program */ + int argc; /* Number of command-line arguments */ + char **argv; /* Command-line arguments */ }; #define MemoryCheck(X) if((X)==0){ \ @@ -448,7 +551,7 @@ struct state *State_new(void); void State_init(void); int State_insert(struct state *, struct config *); struct state *State_find(struct config *); -struct state **State_arrayof(/* */); +struct state **State_arrayof(void); /* Routines used for efficiency in Configlist_add */ @@ -464,22 +567,22 @@ void Configtable_clear(int(*)(struct config *)); /* Allocate a new parser action */ static struct action *Action_new(void){ - static struct action *freelist = 0; + static struct action *actionfreelist = 0; struct action *newaction; - if( freelist==0 ){ + if( actionfreelist==0 ){ int i; int amt = 100; - freelist = (struct action *)calloc(amt, sizeof(struct action)); - if( freelist==0 ){ + actionfreelist = (struct action *)lemon_calloc(amt, sizeof(struct action)); + if( actionfreelist==0 ){ fprintf(stderr,"Unable to allocate memory for a new parser action."); exit(1); } - for(i=0; i<amt-1; i++) freelist[i].next = &freelist[i+1]; - freelist[amt-1].next = 0; + for(i=0; i<amt-1; i++) actionfreelist[i].next = &actionfreelist[i+1]; + actionfreelist[amt-1].next = 0; } - newaction = freelist; - freelist = freelist->next; + newaction = actionfreelist; + actionfreelist = actionfreelist->next; return newaction; } @@ -526,6 +629,7 @@ void Action_add( *app = newaction; newaction->type = type; newaction->sp = sp; + newaction->spOpt = 0; if( type==SHIFT ){ newaction->x.stp = (struct state *)arg; }else{ @@ -551,8 +655,8 @@ void Action_add( ** default action for the state_number is returned. ** ** All actions associated with a single state_number are first entered -** into aLookahead[] using multiple calls to acttab_action(). Then the -** actions for that single state_number are placed into the aAction[] +** into aLookahead[] using multiple calls to acttab_action(). Then the +** actions for that single state_number are placed into the aAction[] ** array with a single call to acttab_insert(). The acttab_insert() call ** also resets the aLookahead[] array in preparation for the next ** state number. @@ -573,10 +677,12 @@ struct acttab { int mxLookahead; /* Maximum aLookahead[].lookahead */ int nLookahead; /* Used slots in aLookahead[] */ int nLookaheadAlloc; /* Slots allocated in aLookahead[] */ + int nterminal; /* Number of terminal symbols */ + int nsymbol; /* total number of symbols */ }; /* Return the number of entries in the yy_action table */ -#define acttab_size(X) ((X)->nAction) +#define acttab_lookahead_size(X) ((X)->nAction) /* The value for the N-th entry in yy_action */ #define acttab_yyaction(X,N) ((X)->aAction[N].action) @@ -586,23 +692,25 @@ struct acttab { /* Free all memory associated with the given acttab */ void acttab_free(acttab *p){ - free( p->aAction ); - free( p->aLookahead ); - free( p ); + lemon_free( p->aAction ); + lemon_free( p->aLookahead ); + lemon_free( p ); } /* Allocate a new acttab structure */ -acttab *acttab_alloc(void){ - acttab *p = (acttab *) calloc( 1, sizeof(*p) ); +acttab *acttab_alloc(int nsymbol, int nterminal){ + acttab *p = (acttab *) lemon_calloc( 1, sizeof(*p) ); if( p==0 ){ fprintf(stderr,"Unable to allocate memory for a new acttab."); exit(1); } memset(p, 0, sizeof(*p)); + p->nsymbol = nsymbol; + p->nterminal = nterminal; return p; } -/* Add a new action to the current transaction set. +/* Add a new action to the current transaction set. ** ** This routine is called once for each lookahead for a particular ** state. @@ -610,7 +718,7 @@ acttab *acttab_alloc(void){ void acttab_action(acttab *p, int lookahead, int action){ if( p->nLookahead>=p->nLookaheadAlloc ){ p->nLookaheadAlloc += 25; - p->aLookahead = (struct lookahead_action *) realloc( p->aLookahead, + p->aLookahead = (struct lookahead_action *) lemon_realloc( p->aLookahead, sizeof(p->aLookahead[0])*p->nLookaheadAlloc ); if( p->aLookahead==0 ){ fprintf(stderr,"malloc failed\n"); @@ -639,20 +747,28 @@ void acttab_action(acttab *p, int lookahead, int action){ ** to an empty set in preparation for a new round of acttab_action() calls. ** ** Return the offset into the action table of the new transaction. +** +** If the makeItSafe parameter is true, then the offset is chosen so that +** it is impossible to overread the yy_lookaside[] table regardless of +** the lookaside token. This is done for the terminal symbols, as they +** come from external inputs and can contain syntax errors. When makeItSafe +** is false, there is more flexibility in selecting offsets, resulting in +** a smaller table. For non-terminal symbols, which are never syntax errors, +** makeItSafe can be false. */ -int acttab_insert(acttab *p){ - int i, j, k, n; +int acttab_insert(acttab *p, int makeItSafe){ + int i, j, k, n, end; assert( p->nLookahead>0 ); /* Make sure we have enough space to hold the expanded action table ** in the worst case. The worst case occurs if the transaction set ** must be appended to the current action table */ - n = p->mxLookahead + 1; + n = p->nsymbol + 1; if( p->nAction + n >= p->nActionAlloc ){ int oldAlloc = p->nActionAlloc; p->nActionAlloc = p->nAction + n + p->nActionAlloc + 20; - p->aAction = (struct lookahead_action *) realloc( p->aAction, + p->aAction = (struct lookahead_action *) lemon_realloc( p->aAction, sizeof(p->aAction[0])*p->nActionAlloc); if( p->aAction==0 ){ fprintf(stderr,"malloc failed\n"); @@ -664,13 +780,14 @@ int acttab_insert(acttab *p){ } } - /* Scan the existing action table looking for an offset that is a + /* Scan the existing action table looking for an offset that is a ** duplicate of the current transaction set. Fall out of the loop ** if and when the duplicate is found. ** ** i is the index in p->aAction[] where p->mnLookahead is inserted. */ - for(i=p->nAction-1; i>=0; i--){ + end = makeItSafe ? p->mnLookahead : 0; + for(i=p->nAction-1; i>=end; i--){ if( p->aAction[i].lookahead==p->mnLookahead ){ /* All lookaheads and actions in the aLookahead[] transaction ** must match against the candidate aAction[i] entry. */ @@ -700,12 +817,13 @@ int acttab_insert(acttab *p){ ** an empty offset in the aAction[] table in which we can add the ** aLookahead[] transaction. */ - if( i<0 ){ + if( i<end ){ /* Look for holes in the aAction[] table that fit the current ** aLookahead[] transaction. Leave i set to the offset of the hole. ** If no holes are found, i is left at p->nAction, which means the ** transaction will be appended. */ - for(i=0; i<p->nActionAlloc - p->mxLookahead; i++){ + i = makeItSafe ? p->mnLookahead : 0; + for(; i<p->nActionAlloc - p->mxLookahead; i++){ if( p->aAction[i].lookahead<0 ){ for(j=0; j<p->nLookahead; j++){ k = p->aLookahead[j].lookahead - p->mnLookahead + i; @@ -723,11 +841,19 @@ int acttab_insert(acttab *p){ } } /* Insert transaction set at index i. */ +#if 0 + printf("Acttab:"); + for(j=0; j<p->nLookahead; j++){ + printf(" %d", p->aLookahead[j].lookahead); + } + printf(" inserted at %d\n", i); +#endif for(j=0; j<p->nLookahead; j++){ k = p->aLookahead[j].lookahead - p->mnLookahead + i; p->aAction[k] = p->aLookahead[j]; if( k>=p->nAction ) p->nAction = k+1; } + if( makeItSafe && i+p->nterminal>=p->nAction ) p->nAction = i+p->nterminal+1; p->nLookahead = 0; /* Return the offset that is added to the lookahead in order to get the @@ -735,6 +861,16 @@ int acttab_insert(acttab *p){ return i - p->mnLookahead; } +/* +** Return the size of the action table without the trailing syntax error +** entries. +*/ +int acttab_action_size(acttab *p){ + int n = p->nAction; + while( n>0 && p->aAction[n-1].lookahead<0 ){ n--; } + return n; +} + /********************** From the file "build.c" *****************************/ /* ** Routines to construction the finite state machine for the LEMON @@ -742,7 +878,7 @@ int acttab_insert(acttab *p){ */ /* Find a precedence symbol of every rule in the grammar. -** +** ** Those rules which have a precedence symbol coded in the input ** grammar using the "[symbol]" construct will already have the ** rp->precsym field filled. Other rules take as their precedence @@ -854,14 +990,17 @@ void FindStates(struct lemon *lemp) sp = Symbol_find(lemp->start); if( sp==0 ){ ErrorMsg(lemp->filename,0, -"The specified start symbol \"%s\" is not \ -in a nonterminal of the grammar. \"%s\" will be used as the start \ -symbol instead.",lemp->start,lemp->rule->lhs->name); + "The specified start symbol \"%s\" is not " + "in a nonterminal of the grammar. \"%s\" will be used as the start " + "symbol instead.",lemp->start,lemp->startRule->lhs->name); lemp->errorcnt++; - sp = lemp->rule->lhs; + sp = lemp->startRule->lhs; } + }else if( lemp->startRule ){ + sp = lemp->startRule->lhs; }else{ - sp = lemp->rule->lhs; + ErrorMsg(lemp->filename,0,"Internal error - no start rule\n"); + exit(1); } /* Make sure the start symbol doesn't occur on the right-hand side of @@ -872,9 +1011,9 @@ symbol instead.",lemp->start,lemp->rule->lhs->name); for(i=0; i<rp->nrhs; i++){ if( rp->rhs[i]==sp ){ /* FIX ME: Deal with multiterminals */ ErrorMsg(lemp->filename,0, -"The start symbol \"%s\" occurs on the \ -right-hand side of a rule. This will result in a parser which \ -does not work properly.",sp->name); + "The start symbol \"%s\" occurs on the " + "right-hand side of a rule. This will result in a parser which " + "does not work properly.",sp->name); lemp->errorcnt++; } } @@ -970,7 +1109,7 @@ PRIVATE void buildshifts(struct lemon *lemp, struct state *stp) struct symbol *bsp; /* Symbol following the dot in configuration "bcfp" */ struct state *newstp; /* A pointer to a successor state */ - /* Each configuration becomes complete after it contibutes to a successor + /* Each configuration becomes complete after it contributes to a successor ** state. Initially, all configurations are incomplete */ for(cfp=stp->cfp; cfp; cfp=cfp->next) cfp->status = INCOMPLETE; @@ -1026,7 +1165,7 @@ void FindLinks(struct lemon *lemp) ** which the link is attached. */ for(i=0; i<lemp->nstate; i++){ stp = lemp->sorted[i]; - for(cfp=stp->cfp; cfp; cfp=cfp->next){ + for(cfp=stp?stp->cfp:0; cfp; cfp=cfp->next){ cfp->stp = stp; } } @@ -1035,7 +1174,7 @@ void FindLinks(struct lemon *lemp) ** links are used in the follow-set computation. */ for(i=0; i<lemp->nstate; i++){ stp = lemp->sorted[i]; - for(cfp=stp->cfp; cfp; cfp=cfp->next){ + for(cfp=stp?stp->cfp:0; cfp; cfp=cfp->next){ for(plp=cfp->bplp; plp; plp=plp->next){ other = plp->cfp; Plink_add(&other->fplp,cfp); @@ -1058,14 +1197,16 @@ void FindFollowSets(struct lemon *lemp) int change; for(i=0; i<lemp->nstate; i++){ + assert( lemp->sorted[i]!=0 ); for(cfp=lemp->sorted[i]->cfp; cfp; cfp=cfp->next){ cfp->status = INCOMPLETE; } } - + do{ progress = 0; for(i=0; i<lemp->nstate; i++){ + assert( lemp->sorted[i]!=0 ); for(cfp=lemp->sorted[i]->cfp; cfp; cfp=cfp->next){ if( cfp->status==COMPLETE ) continue; for(plp=cfp->fplp; plp; plp=plp->next){ @@ -1093,7 +1234,7 @@ void FindActions(struct lemon *lemp) struct symbol *sp; struct rule *rp; - /* Add all of the reduce actions + /* Add all of the reduce actions ** A reduce action is added for each element of the followset of ** a configuration which has its dot at the extreme right. */ @@ -1115,9 +1256,16 @@ void FindActions(struct lemon *lemp) /* Add the accepting token */ if( lemp->start ){ sp = Symbol_find(lemp->start); - if( sp==0 ) sp = lemp->rule->lhs; + if( sp==0 ){ + if( lemp->startRule==0 ){ + fprintf(stderr, "internal error on source line %d: no start rule\n", + __LINE__); + exit(1); + } + sp = lemp->startRule->lhs; + } }else{ - sp = lemp->rule->lhs; + sp = lemp->startRule->lhs; } /* Add to the first state (which is always the starting state of the ** finite state machine) an action to ACCEPT if the lookahead is the @@ -1210,7 +1358,7 @@ static int resolve_conflict( apx->type = RD_RESOLVED; } }else{ - assert( + assert( apx->type==SH_RESOLVED || apx->type==RD_RESOLVED || apx->type==SSCONFLICT || @@ -1241,22 +1389,8 @@ static struct config *basis = 0; /* Top of list of basis configs */ static struct config **basisend = 0; /* End of list of basis configs */ /* Return a pointer to a new configuration */ -PRIVATE struct config *newconfig(){ - struct config *newcfg; - if( freelist==0 ){ - int i; - int amt = 3; - freelist = (struct config *)calloc( amt, sizeof(struct config) ); - if( freelist==0 ){ - fprintf(stderr,"Unable to allocate memory for a new configuration."); - exit(1); - } - for(i=0; i<amt-1; i++) freelist[i].next = &freelist[i+1]; - freelist[amt-1].next = 0; - } - newcfg = freelist; - freelist = freelist->next; - return newcfg; +PRIVATE struct config *newconfig(void){ + return (struct config*)lemon_calloc(1, sizeof(struct config)); } /* The configuration "old" is no longer used */ @@ -1267,7 +1401,7 @@ PRIVATE void deleteconfig(struct config *old) } /* Initialized the configuration list builder */ -void Configlist_init(){ +void Configlist_init(void){ current = 0; currentend = &current; basis = 0; @@ -1277,7 +1411,7 @@ void Configlist_init(){ } /* Initialized the configuration list builder */ -void Configlist_reset(){ +void Configlist_reset(void){ current = 0; currentend = &current; basis = 0; @@ -1387,7 +1521,7 @@ void Configlist_closure(struct lemon *lemp) } /* Sort the configuration list */ -void Configlist_sort(){ +void Configlist_sort(void){ current = (struct config*)msort((char*)current,(char**)&(current->next), Configcmp); currentend = 0; @@ -1395,7 +1529,7 @@ void Configlist_sort(){ } /* Sort the basis configuration list */ -void Configlist_sortbasis(){ +void Configlist_sortbasis(void){ basis = (struct config*)msort((char*)current,(char**)&(current->bp), Configcmp); basisend = 0; @@ -1404,7 +1538,7 @@ void Configlist_sortbasis(){ /* Return a pointer to the head of the configuration list and ** reset the list */ -struct config *Configlist_return(){ +struct config *Configlist_return(void){ struct config *old; old = current; current = 0; @@ -1414,7 +1548,7 @@ struct config *Configlist_return(){ /* Return a pointer to the head of the configuration list and ** reset the list */ -struct config *Configlist_basis(){ +struct config *Configlist_basis(void){ struct config *old; old = basis; basis = 0; @@ -1456,13 +1590,15 @@ void ErrorMsg(const char *filename, int lineno, const char *format, ...){ /* Report an out-of-memory condition and abort. This function ** is used mostly by the "MemoryCheck" macro in struct.h */ -void memory_error(){ +void memory_error(void){ fprintf(stderr,"Out of memory. Aborting...\n"); exit(1); } -static int nDefine = 0; /* Number of -D options on the command line */ -static char **azDefine = 0; /* Name of the -D macros */ +static int nDefine = 0; /* Number of -D options on the command line */ +static int nDefineUsed = 0; /* Number of -D options actually used */ +static char **azDefine = 0; /* Name of the -D macros */ +static char *bDefineUsed = 0; /* True for every -D macro actually used */ /* This routine is called with the argument to each -D command-line option. ** Add the macro defined to the azDefine array. @@ -1470,13 +1606,19 @@ static char **azDefine = 0; /* Name of the -D macros */ static void handle_D_option(char *z){ char **paz; nDefine++; - azDefine = (char **) realloc(azDefine, sizeof(azDefine[0])*nDefine); + azDefine = (char **) lemon_realloc(azDefine, sizeof(azDefine[0])*nDefine); if( azDefine==0 ){ fprintf(stderr,"out of memory\n"); exit(1); } + bDefineUsed = (char*)lemon_realloc(bDefineUsed, nDefine); + if( bDefineUsed==0 ){ + fprintf(stderr,"out of memory\n"); + exit(1); + } + bDefineUsed[nDefine-1] = 0; paz = &azDefine[nDefine-1]; - *paz = (char *) malloc( lemonStrlen(z)+1 ); + *paz = (char *) lemon_malloc( lemonStrlen(z)+1 ); if( *paz==0 ){ fprintf(stderr,"out of memory\n"); exit(1); @@ -1486,15 +1628,92 @@ static void handle_D_option(char *z){ *z = 0; } +/* This routine is called with the argument to each -U command-line option. +** Omit a previously defined macro. +*/ +static void handle_U_option(char *z){ + int i; + for(i=0; i<nDefine; i++){ + if( strcmp(azDefine[i],z)==0 ){ + nDefine--; + if( i<nDefine ){ + azDefine[i] = azDefine[nDefine]; + bDefineUsed[i] = bDefineUsed[nDefine]; + } + break; + } + } +} + +/* Rember the name of the output directory +*/ +static char *outputDir = NULL; +static void handle_d_option(char *z){ + outputDir = (char *) lemon_malloc( lemonStrlen(z)+1 ); + if( outputDir==0 ){ + fprintf(stderr,"out of memory\n"); + exit(1); + } + lemon_strcpy(outputDir, z); +} + static char *user_templatename = NULL; static void handle_T_option(char *z){ - user_templatename = (char *) malloc( lemonStrlen(z)+1 ); + user_templatename = (char *) lemon_malloc( lemonStrlen(z)+1 ); if( user_templatename==0 ){ memory_error(); } lemon_strcpy(user_templatename, z); } +/* Merge together to lists of rules ordered by rule.iRule */ +static struct rule *Rule_merge(struct rule *pA, struct rule *pB){ + struct rule *pFirst = 0; + struct rule **ppPrev = &pFirst; + while( pA && pB ){ + if( pA->iRule<pB->iRule ){ + *ppPrev = pA; + ppPrev = &pA->next; + pA = pA->next; + }else{ + *ppPrev = pB; + ppPrev = &pB->next; + pB = pB->next; + } + } + if( pA ){ + *ppPrev = pA; + }else{ + *ppPrev = pB; + } + return pFirst; +} + +/* +** Sort a list of rules in order of increasing iRule value +*/ +static struct rule *Rule_sort(struct rule *rp){ + unsigned int i; + struct rule *pNext; + struct rule *x[32]; + memset(x, 0, sizeof(x)); + while( rp ){ + pNext = rp->next; + rp->next = 0; + for(i=0; i<sizeof(x)/sizeof(x[0])-1 && x[i]; i++){ + rp = Rule_merge(x[i], rp); + x[i] = 0; + } + x[i] = rp; + rp = pNext; + } + rp = 0; + for(i=0; i<sizeof(x)/sizeof(x[0]); i++){ + rp = Rule_merge(x[i], rp); + } + return rp; +} + /* forward reference */ static const char *minimum_size_type(int lwr, int upr, int *pnByte); @@ -1507,9 +1726,17 @@ static void stats_line(const char *zLabel, int iValue){ iValue); } +/* +** Comparison function used by qsort() to sort the azDefine[] array. +*/ +static int defineCmp(const void *pA, const void *pB){ + const char *zA = *(const char**)pA; + const char *zB = *(const char**)pB; + return strcmp(zA,zB); +} + /* The main program. Parse the command line and do it... */ -int main(int argc, char **argv) -{ +int main(int argc, char **argv){ static int version = 0; static int rpflag = 0; static int basisflag = 0; @@ -1519,10 +1746,15 @@ int main(int argc, char **argv) static int mhflag = 0; static int nolinenosflag = 0; static int noResort = 0; + static int sqlFlag = 0; + static int printPP = 0; + static struct s_options options[] = { {OPT_FLAG, "b", (char*)&basisflag, "Print only the basis in report."}, {OPT_FLAG, "c", (char*)&compress, "Don't compress the action table."}, + {OPT_FSTR, "d", (char*)&handle_d_option, "Output directory. Default '.'"}, {OPT_FSTR, "D", (char*)handle_D_option, "Define an %ifdef macro."}, + {OPT_FLAG, "E", (char*)&printPP, "Print input file after preprocessing."}, {OPT_FSTR, "f", 0, "Ignored. (Placeholder for -f compiler options.)"}, {OPT_FLAG, "g", (char*)&rpflag, "Print grammar without actions."}, {OPT_FSTR, "I", 0, "Ignored. (Placeholder for '-I' compiler options.)"}, @@ -1535,19 +1767,23 @@ int main(int argc, char **argv) {OPT_FLAG, "r", (char*)&noResort, "Do not sort or renumber states"}, {OPT_FLAG, "s", (char*)&statistics, "Print parser stats to standard output."}, + {OPT_FLAG, "S", (char*)&sqlFlag, + "Generate the *.sql file describing the parser tables."}, {OPT_FLAG, "x", (char*)&version, "Print the version number."}, {OPT_FSTR, "T", (char*)handle_T_option, "Specify a template file."}, + {OPT_FSTR, "U", (char*)handle_U_option, "Undefine a macro."}, {OPT_FSTR, "W", 0, "Ignored. (Placeholder for '-W' compiler options.)"}, {OPT_FLAG,0,0,0} }; int i; int exitcode; struct lemon lem; + struct rule *rp; OptInit(argv,options,stderr); if( version ){ printf("Lemon version 1.0\n"); - exit(0); + exit(0); } if( OptNArgs()!=1 ){ fprintf(stderr,"Exactly one filename argument is required.\n"); @@ -1555,26 +1791,28 @@ int main(int argc, char **argv) } memset(&lem, 0, sizeof(lem)); lem.errorcnt = 0; + qsort(azDefine, nDefine, sizeof(azDefine[0]), defineCmp); /* Initialize the machine */ Strsafe_init(); Symbol_init(); State_init(); - lem.argv0 = argv[0]; + lem.argv = argv; + lem.argc = argc; lem.filename = OptArg(0); lem.basisflag = basisflag; lem.nolinenosflag = nolinenosflag; + lem.printPreprocessed = printPP; Symbol_new("$"); - lem.errsym = Symbol_new("error"); - lem.errsym->useCnt = 0; /* Parse the input file */ Parse(&lem); - if( lem.errorcnt ) exit(lem.errorcnt); + if( lem.printPreprocessed || lem.errorcnt ) exit(lem.errorcnt); if( lem.nrule==0 ){ fprintf(stderr,"Empty grammar.\n"); exit(1); } + lem.errsym = Symbol_find("error"); /* Count and index the symbols of the grammar */ Symbol_new("{default}"); @@ -1589,6 +1827,20 @@ int main(int argc, char **argv) for(i=1; ISUPPER(lem.symbols[i]->name[0]); i++); lem.nterminal = i; + /* Assign sequential rule numbers. Start with 0. Put rules that have no + ** reduce action C-code associated with them last, so that the switch() + ** statement that selects reduction actions will have a smaller jump table. + */ + for(i=0, rp=lem.rule; rp; rp=rp->next){ + rp->iRule = rp->code ? i++ : -1; + } + lem.nruleWithAction = i; + for(rp=lem.rule; rp; rp=rp->next){ + if( rp->iRule<0 ) rp->iRule = i++; + } + lem.startRule = lem.rule; + lem.rule = Rule_sort(lem.rule); + /* Generate a reprint of the grammar, if requested on the command line */ if( rpflag ){ Reprint(&lem); @@ -1630,7 +1882,7 @@ int main(int argc, char **argv) if( !quiet ) ReportOutput(&lem); /* Generate the source code for the parser */ - ReportTable(&lem, mhflag); + ReportTable(&lem, mhflag, sqlFlag); /* Produce a header file for use by the scanner. (This step is ** omitted if the "-m" option is used because makeheaders will @@ -1646,6 +1898,7 @@ int main(int argc, char **argv) stats_line("states", lem.nxstate); stats_line("conflicts", lem.nconflict); stats_line("action table entries", lem.nactiontab); + stats_line("lookahead table entries", lem.nlookaheadtab); stats_line("total table size (bytes)", lem.tablesize); } if( lem.nconflict > 0 ){ @@ -1654,6 +1907,7 @@ int main(int argc, char **argv) /* return 0 on success, 1 on failure. */ exitcode = ((lem.errorcnt > 0) || (lem.nconflict > 0)) ? 1 : 0; + lemon_free_all(); exit(exitcode); return (exitcode); } @@ -1747,7 +2001,7 @@ static char *merge( ** ** Return Value: ** A pointer to the head of a sorted list containing the elements -** orginally in list. +** originally in list. ** ** Side effects: ** The "next" pointers for elements in list are changed. @@ -1769,17 +2023,17 @@ static char *msort( list = NEXT(list); NEXT(ep) = 0; for(i=0; i<LISTSIZE-1 && set[i]!=0; i++){ - ep = merge(ep,set[i],cmp,offset); + ep = merge(set[i],ep,cmp,offset); set[i] = 0; } - set[i] = ep; + set[i] = merge(set[i],ep,cmp,offset); } ep = 0; for(i=0; i<LISTSIZE; i++) if( set[i] ) ep = merge(set[i],ep,cmp,offset); return ep; } /************************ From the file "option.c" **************************/ -static char **argv; +static char **g_argv; static struct s_options *op; static FILE *errstream; @@ -1792,14 +2046,18 @@ static FILE *errstream; static void errline(int n, int k, FILE *err) { int spcnt, i; - if( argv[0] ) fprintf(err,"%s",argv[0]); - spcnt = lemonStrlen(argv[0]) + 1; - for(i=1; i<n && argv[i]; i++){ - fprintf(err," %s",argv[i]); - spcnt += lemonStrlen(argv[i])+1; + if( g_argv[0] ){ + fprintf(err,"%s",g_argv[0]); + spcnt = lemonStrlen(g_argv[0]) + 1; + }else{ + spcnt = 0; + } + for(i=1; i<n && g_argv[i]; i++){ + fprintf(err," %s",g_argv[i]); + spcnt += lemonStrlen(g_argv[i])+1; } spcnt += k; - for(; argv[i]; i++) fprintf(err," %s",argv[i]); + for(; g_argv[i]; i++) fprintf(err," %s",g_argv[i]); if( spcnt<20 ){ fprintf(err,"\n%*s^-- here\n",spcnt,""); }else{ @@ -1815,13 +2073,13 @@ static int argindex(int n) { int i; int dashdash = 0; - if( argv!=0 && *argv!=0 ){ - for(i=1; argv[i]; i++){ - if( dashdash || !ISOPT(argv[i]) ){ + if( g_argv!=0 && *g_argv!=0 ){ + for(i=1; g_argv[i]; i++){ + if( dashdash || !ISOPT(g_argv[i]) ){ if( n==0 ) return i; n--; } - if( strcmp(argv[i],"--")==0 ) dashdash = 1; + if( strcmp(g_argv[i],"--")==0 ) dashdash = 1; } } return -1; @@ -1838,9 +2096,9 @@ static int handleflags(int i, FILE *err) int errcnt = 0; int j; for(j=0; op[j].label; j++){ - if( strncmp(&argv[i][1],op[j].label,lemonStrlen(op[j].label))==0 ) break; + if( strncmp(&g_argv[i][1],op[j].label,lemonStrlen(op[j].label))==0 ) break; } - v = argv[i][0]=='-' ? 1 : 0; + v = g_argv[i][0]=='-' ? 1 : 0; if( op[j].label==0 ){ if( err ){ fprintf(err,"%sundefined option.\n",emsg); @@ -1854,7 +2112,7 @@ static int handleflags(int i, FILE *err) }else if( op[j].type==OPT_FFLAG ){ (*(void(*)(int))(op[j].arg))(v); }else if( op[j].type==OPT_FSTR ){ - (*(void(*)(char *))(op[j].arg))(&argv[i][2]); + (*(void(*)(char *))(op[j].arg))(&g_argv[i][2]); }else{ if( err ){ fprintf(err,"%smissing argument on switch.\n",emsg); @@ -1876,11 +2134,11 @@ static int handleswitch(int i, FILE *err) char *cp; int j; int errcnt = 0; - cp = strchr(argv[i],'='); + cp = strchr(g_argv[i],'='); assert( cp!=0 ); *cp = 0; for(j=0; op[j].label; j++){ - if( strcmp(argv[i],op[j].label)==0 ) break; + if( strcmp(g_argv[i],op[j].label)==0 ) break; } *cp = '='; if( op[j].label==0 ){ @@ -1907,7 +2165,7 @@ static int handleswitch(int i, FILE *err) if( err ){ fprintf(err, "%sillegal character in floating-point argument.\n",emsg); - errline(i,(int)((char*)end-(char*)argv[i]),err); + errline(i,(int)((char*)end-(char*)g_argv[i]),err); } errcnt++; } @@ -1918,7 +2176,7 @@ static int handleswitch(int i, FILE *err) if( *end ){ if( err ){ fprintf(err,"%sillegal character in integer argument.\n",emsg); - errline(i,(int)((char*)end-(char*)argv[i]),err); + errline(i,(int)((char*)end-(char*)g_argv[i]),err); } errcnt++; } @@ -1958,15 +2216,15 @@ static int handleswitch(int i, FILE *err) int OptInit(char **a, struct s_options *o, FILE *err) { int errcnt = 0; - argv = a; + g_argv = a; op = o; errstream = err; - if( argv && *argv && op ){ + if( g_argv && *g_argv && op ){ int i; - for(i=1; argv[i]; i++){ - if( argv[i][0]=='+' || argv[i][0]=='-' ){ + for(i=1; g_argv[i]; i++){ + if( g_argv[i][0]=='+' || g_argv[i][0]=='-' ){ errcnt += handleflags(i,err); - }else if( strchr(argv[i],'=') ){ + }else if( strchr(g_argv[i],'=') ){ errcnt += handleswitch(i,err); } } @@ -1979,14 +2237,14 @@ int OptInit(char **a, struct s_options *o, FILE *err) return 0; } -int OptNArgs(){ +int OptNArgs(void){ int cnt = 0; int dashdash = 0; int i; - if( argv!=0 && argv[0]!=0 ){ - for(i=1; argv[i]; i++){ - if( dashdash || !ISOPT(argv[i]) ) cnt++; - if( strcmp(argv[i],"--")==0 ) dashdash = 1; + if( g_argv!=0 && g_argv[0]!=0 ){ + for(i=1; g_argv[i]; i++){ + if( dashdash || !ISOPT(g_argv[i]) ) cnt++; + if( strcmp(g_argv[i],"--")==0 ) dashdash = 1; } } return cnt; @@ -1996,7 +2254,7 @@ char *OptArg(int n) { int i; i = argindex(n); - return i>=0 ? argv[i] : 0; + return i>=0 ? g_argv[i] : 0; } void OptErr(int n) @@ -2006,7 +2264,7 @@ void OptErr(int n) if( i>=0 ) errline(i,0,errstream); } -void OptPrint(){ +void OptPrint(void){ int i; int max, len; max = 0; @@ -2083,7 +2341,8 @@ enum e_state { WAITING_FOR_FALLBACK_ID, WAITING_FOR_WILDCARD_ID, WAITING_FOR_CLASS_ID, - WAITING_FOR_CLASS_TOKEN + WAITING_FOR_CLASS_TOKEN, + WAITING_FOR_TOKEN_NAME }; struct pstate { char *filename; /* Name of the input file */ @@ -2125,7 +2384,7 @@ static void parseonetoken(struct pstate *psp) psp->preccounter = 0; psp->firstrule = psp->lastrule = 0; psp->gp->nrule = 0; - /* Fall thru to next case */ + /* fall through */ case WAITING_FOR_DECL_OR_RULE: if( x[0]=='%' ){ psp->state = WAITING_FOR_DECL_KEYWORD; @@ -2137,17 +2396,20 @@ static void parseonetoken(struct pstate *psp) }else if( x[0]=='{' ){ if( psp->prevrule==0 ){ ErrorMsg(psp->filename,psp->tokenlineno, -"There is no prior rule upon which to attach the code \ -fragment which begins on this line."); + "There is no prior rule upon which to attach the code " + "fragment which begins on this line."); psp->errorcnt++; }else if( psp->prevrule->code!=0 ){ ErrorMsg(psp->filename,psp->tokenlineno, -"Code fragment beginning on this line is not the first \ -to follow the previous rule."); + "Code fragment beginning on this line is not the first " + "to follow the previous rule."); psp->errorcnt++; + }else if( strcmp(x, "{NEVER-REDUCE")==0 ){ + psp->prevrule->neverReduce = 1; }else{ psp->prevrule->line = psp->tokenlineno; psp->prevrule->code = &x[1]; + psp->prevrule->noCode = 0; } }else if( x[0]=='[' ){ psp->state = PRECEDENCE_MARK_1; @@ -2169,8 +2431,8 @@ to follow the previous rule."); psp->errorcnt++; }else if( psp->prevrule->precsym!=0 ){ ErrorMsg(psp->filename,psp->tokenlineno, -"Precedence mark on this line is not the first \ -to follow the previous rule."); + "Precedence mark on this line is not the first " + "to follow the previous rule."); psp->errorcnt++; }else{ psp->prevrule->precsym = Symbol_new(x); @@ -2234,7 +2496,7 @@ to follow the previous rule."); case IN_RHS: if( x[0]=='.' ){ struct rule *rp; - rp = (struct rule *)calloc( sizeof(struct rule) + + rp = (struct rule *)lemon_calloc( sizeof(struct rule) + sizeof(struct symbol*)*psp->nrhs + sizeof(char*)*psp->nrhs, 1); if( rp==0 ){ ErrorMsg(psp->filename,psp->tokenlineno, @@ -2249,11 +2511,13 @@ to follow the previous rule."); for(i=0; i<psp->nrhs; i++){ rp->rhs[i] = psp->rhs[i]; rp->rhsalias[i] = psp->alias[i]; + if( rp->rhsalias[i]!=0 ){ rp->rhs[i]->bContent = 1; } } rp->lhs = psp->lhs; rp->lhsalias = psp->lhsalias; rp->nrhs = psp->nrhs; rp->code = 0; + rp->noCode = 1; rp->precsym = 0; rp->index = psp->gp->nrule++; rp->nextlhs = rp->lhs->rule; @@ -2280,21 +2544,21 @@ to follow the previous rule."); psp->alias[psp->nrhs] = 0; psp->nrhs++; } - }else if( (x[0]=='|' || x[0]=='/') && psp->nrhs>0 ){ + }else if( (x[0]=='|' || x[0]=='/') && psp->nrhs>0 && ISUPPER(x[1]) ){ struct symbol *msp = psp->rhs[psp->nrhs-1]; if( msp->type!=MULTITERMINAL ){ struct symbol *origsp = msp; - msp = (struct symbol *) calloc(1,sizeof(*msp)); + msp = (struct symbol *) lemon_calloc(1,sizeof(*msp)); memset(msp, 0, sizeof(*msp)); msp->type = MULTITERMINAL; msp->nsubsym = 1; - msp->subsym = (struct symbol **) calloc(1,sizeof(struct symbol*)); + msp->subsym = (struct symbol**)lemon_calloc(1,sizeof(struct symbol*)); msp->subsym[0] = origsp; msp->name = origsp->name; psp->rhs[psp->nrhs-1] = msp; } msp->nsubsym++; - msp->subsym = (struct symbol **) realloc(msp->subsym, + msp->subsym = (struct symbol **) lemon_realloc(msp->subsym, sizeof(struct symbol*)*msp->nsubsym); msp->subsym[msp->nsubsym-1] = Symbol_new(&x[1]); if( ISLOWER(x[1]) || ISLOWER(msp->subsym[0]->name[0]) ){ @@ -2365,12 +2629,21 @@ to follow the previous rule."); }else if( strcmp(x,"extra_argument")==0 ){ psp->declargslot = &(psp->gp->arg); psp->insertLineMacro = 0; + }else if( strcmp(x,"extra_context")==0 ){ + psp->declargslot = &(psp->gp->ctx); + psp->insertLineMacro = 0; }else if( strcmp(x,"token_type")==0 ){ psp->declargslot = &(psp->gp->tokentype); psp->insertLineMacro = 0; }else if( strcmp(x,"default_type")==0 ){ psp->declargslot = &(psp->gp->vartype); psp->insertLineMacro = 0; + }else if( strcmp(x,"realloc")==0 ){ + psp->declargslot = &(psp->gp->reallocFunc); + psp->insertLineMacro = 0; + }else if( strcmp(x,"free")==0 ){ + psp->declargslot = &(psp->gp->freeFunc); + psp->insertLineMacro = 0; }else if( strcmp(x,"stack_size")==0 ){ psp->declargslot = &(psp->gp->stacksize); psp->insertLineMacro = 0; @@ -2396,6 +2669,8 @@ to follow the previous rule."); }else if( strcmp(x,"fallback")==0 ){ psp->fallback = 0; psp->state = WAITING_FOR_FALLBACK_ID; + }else if( strcmp(x,"token")==0 ){ + psp->state = WAITING_FOR_TOKEN_NAME; }else if( strcmp(x,"wildcard")==0 ){ psp->state = WAITING_FOR_WILDCARD_ID; }else if( strcmp(x,"token_class")==0 ){ @@ -2487,8 +2762,10 @@ to follow the previous rule."); } nOld = lemonStrlen(zOld); n = nOld + nNew + 20; - addLineMacro = !psp->gp->nolinenosflag && psp->insertLineMacro && - (psp->decllinenoslot==0 || psp->decllinenoslot[0]!=0); + addLineMacro = !psp->gp->nolinenosflag + && psp->insertLineMacro + && psp->tokenlineno>1 + && (psp->decllinenoslot==0 || psp->decllinenoslot[0]!=0); if( addLineMacro ){ for(z=psp->filename, nBack=0; *z; z++){ if( *z=='\\' ) nBack++; @@ -2497,7 +2774,7 @@ to follow the previous rule."); nLine = lemonStrlen(zLine); n += nLine + lemonStrlen(psp->filename) + nBack; } - *psp->declargslot = (char *) realloc(*psp->declargslot, n); + *psp->declargslot = (char *) lemon_realloc(*psp->declargslot, n); zBuf = *psp->declargslot + nOld; if( addLineMacro ){ if( nOld && zBuf[-1]!='\n' ){ @@ -2550,6 +2827,26 @@ to follow the previous rule."); } } break; + case WAITING_FOR_TOKEN_NAME: + /* Tokens do not have to be declared before use. But they can be + ** in order to control their assigned integer number. The number for + ** each token is assigned when it is first seen. So by including + ** + ** %token ONE TWO THREE. + ** + ** early in the grammar file, that assigns small consecutive values + ** to each of the tokens ONE TWO and THREE. + */ + if( x[0]=='.' ){ + psp->state = WAITING_FOR_DECL_OR_RULE; + }else if( !ISUPPER(x[0]) ){ + ErrorMsg(psp->filename, psp->tokenlineno, + "%%token argument \"%s\" should be a token", x); + psp->errorcnt++; + }else{ + (void)Symbol_new(x); + } + break; case WAITING_FOR_WILDCARD_ID: if( x[0]=='.' ){ psp->state = WAITING_FOR_DECL_OR_RULE; @@ -2571,7 +2868,7 @@ to follow the previous rule."); case WAITING_FOR_CLASS_ID: if( !ISLOWER(x[0]) ){ ErrorMsg(psp->filename, psp->tokenlineno, - "%%token_class must be followed by an identifier: ", x); + "%%token_class must be followed by an identifier: %s", x); psp->errorcnt++; psp->state = RESYNC_AFTER_DECL_ERROR; }else if( Symbol_find(x) ){ @@ -2591,7 +2888,7 @@ to follow the previous rule."); }else if( ISUPPER(x[0]) || ((x[0]=='|' || x[0]=='/') && ISUPPER(x[1])) ){ struct symbol *msp = psp->tkclass; msp->nsubsym++; - msp->subsym = (struct symbol **) realloc(msp->subsym, + msp->subsym = (struct symbol **) lemon_realloc(msp->subsym, sizeof(struct symbol*)*msp->nsubsym); if( !ISUPPER(x[0]) ) x++; msp->subsym[msp->nsubsym-1] = Symbol_new(x); @@ -2612,13 +2909,112 @@ to follow the previous rule."); } } +/* The text in the input is part of the argument to an %ifdef or %ifndef. +** Evaluate the text as a boolean expression. Return true or false. +*/ +static int eval_preprocessor_boolean(char *z, int lineno){ + int neg = 0; + int res = 0; + int okTerm = 1; + int i; + for(i=0; z[i]!=0; i++){ + if( ISSPACE(z[i]) ) continue; + if( z[i]=='!' ){ + if( !okTerm ) goto pp_syntax_error; + neg = !neg; + continue; + } + if( z[i]=='|' && z[i+1]=='|' ){ + if( okTerm ) goto pp_syntax_error; + if( res ) return 1; + i++; + okTerm = 1; + continue; + } + if( z[i]=='&' && z[i+1]=='&' ){ + if( okTerm ) goto pp_syntax_error; + if( !res ) return 0; + i++; + okTerm = 1; + continue; + } + if( z[i]=='(' ){ + int k; + int n = 1; + if( !okTerm ) goto pp_syntax_error; + for(k=i+1; z[k]; k++){ + if( z[k]==')' ){ + n--; + if( n==0 ){ + z[k] = 0; + res = eval_preprocessor_boolean(&z[i+1], -1); + z[k] = ')'; + if( res<0 ){ + i = i-res; + goto pp_syntax_error; + } + i = k; + break; + } + }else if( z[k]=='(' ){ + n++; + }else if( z[k]==0 ){ + i = k; + goto pp_syntax_error; + } + } + if( neg ){ + res = !res; + neg = 0; + } + okTerm = 0; + continue; + } + if( ISALPHA(z[i]) ){ + int j, k, n; + if( !okTerm ) goto pp_syntax_error; + for(k=i+1; ISALNUM(z[k]) || z[k]=='_'; k++){} + n = k - i; + res = 0; + for(j=0; j<nDefine; j++){ + if( strncmp(azDefine[j],&z[i],n)==0 && azDefine[j][n]==0 ){ + if( !bDefineUsed[j] ){ + bDefineUsed[j] = 1; + nDefineUsed++; + } + res = 1; + break; + } + } + i = k-1; + if( neg ){ + res = !res; + neg = 0; + } + okTerm = 0; + continue; + } + goto pp_syntax_error; + } + return res; + +pp_syntax_error: + if( lineno>0 ){ + fprintf(stderr, "%%if syntax error on line %d.\n", lineno); + fprintf(stderr, " %.*s <-- syntax error here\n", i+1, z); + exit(1); + }else{ + return -(i+1); + } +} + /* Run the preprocessor over the input file text. The global variables ** azDefine[0] through azDefine[nDefine-1] contains the names of all defined ** macros. This routine looks for "%ifdef" and "%ifndef" and "%endif" and ** comments them out. Text in between is also commented out as appropriate. */ static void preprocess_input(char *z){ - int i, j, k, n; + int i, j, k; int exclude = 0; int start = 0; int lineno = 1; @@ -2634,21 +3030,33 @@ static void preprocess_input(char *z){ } } for(j=i; z[j] && z[j]!='\n'; j++) z[j] = ' '; - }else if( (strncmp(&z[i],"%ifdef",6)==0 && ISSPACE(z[i+6])) - || (strncmp(&z[i],"%ifndef",7)==0 && ISSPACE(z[i+7])) ){ + }else if( strncmp(&z[i],"%else",5)==0 && ISSPACE(z[i+5]) ){ + if( exclude==1){ + exclude = 0; + for(j=start; j<i; j++) if( z[j]!='\n' ) z[j] = ' '; + }else if( exclude==0 ){ + exclude = 1; + start = i; + start_lineno = lineno; + } + for(j=i; z[j] && z[j]!='\n'; j++) z[j] = ' '; + }else if( strncmp(&z[i],"%ifdef ",7)==0 + || strncmp(&z[i],"%if ",4)==0 + || strncmp(&z[i],"%ifndef ",8)==0 ){ if( exclude ){ exclude++; }else{ - for(j=i+7; ISSPACE(z[j]); j++){} - for(n=0; z[j+n] && !ISSPACE(z[j+n]); n++){} - exclude = 1; - for(k=0; k<nDefine; k++){ - if( strncmp(azDefine[k],&z[j],n)==0 && lemonStrlen(azDefine[k])==n ){ - exclude = 0; - break; - } - } - if( z[i+3]=='n' ) exclude = !exclude; + int isNot; + int iBool; + for(j=i; z[j] && !ISSPACE(z[j]); j++){} + iBool = j; + isNot = (j==i+7); + while( z[j] && z[j]!='\n' ){ j++; } + k = z[j]; + z[j] = 0; + exclude = eval_preprocessor_boolean(&z[iBool], lineno); + z[j] = k; + if( !isNot ) exclude = !exclude; if( exclude ){ start = i; start_lineno = lineno; @@ -2695,9 +3103,10 @@ void Parse(struct lemon *gp) fseek(fp,0,2); filesize = ftell(fp); rewind(fp); - filebuf = (char *)malloc( filesize+1 ); + filebuf = (char *)lemon_malloc( filesize+1 ); if( filesize>100000000 || filebuf==0 ){ ErrorMsg(ps.filename,0,"Input file too large."); + lemon_free(filebuf); gp->errorcnt++; fclose(fp); return; @@ -2705,7 +3114,7 @@ void Parse(struct lemon *gp) if( fread(filebuf,1,filesize,fp)!=filesize ){ ErrorMsg(ps.filename,0,"Can't read in all %d bytes of this file.", filesize); - free(filebuf); + lemon_free(filebuf); gp->errorcnt++; fclose(fp); return; @@ -2715,6 +3124,10 @@ void Parse(struct lemon *gp) /* Make an initial pass through the file to handle %ifdef and %ifndef */ preprocess_input(filebuf); + if( gp->printPreprocessed ){ + printf("%s\n", filebuf); + return; + } /* Now scan the text of the input file */ lineno = 1; @@ -2728,6 +3141,7 @@ void Parse(struct lemon *gp) } if( c=='/' && cp[1]=='*' ){ /* Skip C style comments */ cp+=2; + if( (*cp)=='/' ) cp++; while( (c= *cp)!=0 && (c!='/' || cp[-1]!='*') ){ if( c=='\n' ) lineno++; cp++; @@ -2745,7 +3159,8 @@ void Parse(struct lemon *gp) } if( c==0 ){ ErrorMsg(ps.filename,startline, -"String starting on this line is not terminated before the end of the file."); + "String starting on this line is not terminated before " + "the end of the file."); ps.errorcnt++; nextcp = cp; }else{ @@ -2784,7 +3199,8 @@ void Parse(struct lemon *gp) } if( c==0 ){ ErrorMsg(ps.filename,ps.tokenlineno, -"C code starting on this line is not terminated before the end of the file."); + "C code starting on this line is not terminated before " + "the end of the file."); ps.errorcnt++; nextcp = cp; }else{ @@ -2810,7 +3226,7 @@ void Parse(struct lemon *gp) *cp = (char)c; /* Restore the buffer */ cp = nextcp; } - free(filebuf); /* Release the buffer after parsing */ + lemon_free(filebuf); /* Release the buffer after parsing */ gp->rule = ps.firstrule; gp->errorcnt = ps.errorcnt; } @@ -2822,13 +3238,13 @@ void Parse(struct lemon *gp) static struct plink *plink_freelist = 0; /* Allocate a new plink */ -struct plink *Plink_new(){ +struct plink *Plink_new(void){ struct plink *newlink; if( plink_freelist==0 ){ int i; int amt = 100; - plink_freelist = (struct plink *)calloc( amt, sizeof(struct plink) ); + plink_freelist = (struct plink *)lemon_calloc( amt, sizeof(struct plink) ); if( plink_freelist==0 ){ fprintf(stderr, "Unable to allocate memory for a new follow-set propagation link.\n"); @@ -2881,21 +3297,34 @@ void Plink_delete(struct plink *plp) ** Procedures for generating reports and tables in the LEMON parser generator. */ -/* Generate a filename with the given suffix. Space to hold the -** name comes from malloc() and must be freed by the calling -** function. +/* Generate a filename with the given suffix. */ PRIVATE char *file_makename(struct lemon *lemp, const char *suffix) { char *name; char *cp; - - name = (char*)malloc( lemonStrlen(lemp->filename) + lemonStrlen(suffix) + 5 ); + char *filename = lemp->filename; + int sz; + + if( outputDir ){ + cp = strrchr(filename, '/'); + if( cp ) filename = cp + 1; + } + sz = lemonStrlen(filename); + sz += lemonStrlen(suffix); + if( outputDir ) sz += lemonStrlen(outputDir) + 1; + sz += 5; + name = (char*)lemon_malloc( sz ); if( name==0 ){ fprintf(stderr,"Can't allocate space for a filename.\n"); exit(1); } - lemon_strcpy(name,lemp->filename); + name[0] = 0; + if( outputDir ){ + lemon_strcpy(name, outputDir); + lemon_strcat(name, "/"); + } + lemon_strcat(name,filename); cp = strrchr(name,'.'); if( cp ) *cp = 0; lemon_strcat(name,suffix); @@ -2912,7 +3341,7 @@ PRIVATE FILE *file_open( ){ FILE *fp; - if( lemp->outname ) free(lemp->outname); + if( lemp->outname ) lemon_free(lemp->outname); lemp->outname = file_makename(lemp, suffix); fp = fopen(lemp->outname,mode); if( fp==0 && *mode=='w' ){ @@ -2923,7 +3352,28 @@ PRIVATE FILE *file_open( return fp; } -/* Duplicate the input file without comments and without actions +/* Print the text of a rule +*/ +void rule_print(FILE *out, struct rule *rp){ + int i, j; + fprintf(out, "%s",rp->lhs->name); + /* if( rp->lhsalias ) fprintf(out,"(%s)",rp->lhsalias); */ + fprintf(out," ::="); + for(i=0; i<rp->nrhs; i++){ + struct symbol *sp = rp->rhs[i]; + if( sp->type==MULTITERMINAL ){ + fprintf(out," %s", sp->subsym[0]->name); + for(j=1; j<sp->nsubsym; j++){ + fprintf(out,"|%s", sp->subsym[j]->name); + } + }else{ + fprintf(out," %s", sp->name); + } + /* if( rp->rhsalias[i] ) fprintf(out,"(%s)",rp->rhsalias[i]); */ + } +} + +/* Duplicate the input file without comments and without actions ** on rules */ void Reprint(struct lemon *lemp) { @@ -2950,21 +3400,7 @@ void Reprint(struct lemon *lemp) printf("\n"); } for(rp=lemp->rule; rp; rp=rp->next){ - printf("%s",rp->lhs->name); - /* if( rp->lhsalias ) printf("(%s)",rp->lhsalias); */ - printf(" ::="); - for(i=0; i<rp->nrhs; i++){ - sp = rp->rhs[i]; - if( sp->type==MULTITERMINAL ){ - printf(" %s", sp->subsym[0]->name); - for(j=1; j<sp->nsubsym; j++){ - printf("|%s", sp->subsym[j]->name); - } - }else{ - printf(" %s", sp->name); - } - /* if( rp->rhsalias[i] ) printf("(%s)",rp->rhsalias[i]); */ - } + rule_print(stdout, rp); printf("."); if( rp->precsym ) printf(" [%s]",rp->precsym->name); /* if( rp->code ) printf("\n %s",rp->code); */ @@ -3052,13 +3488,13 @@ int PrintAction( } case REDUCE: { struct rule *rp = ap->x.rp; - fprintf(fp,"%*s reduce %-7d",indent,ap->sp->name,rp->index); + fprintf(fp,"%*s reduce %-7d",indent,ap->sp->name,rp->iRule); RulePrint(fp, rp, -1); break; } case SHIFTREDUCE: { struct rule *rp = ap->x.rp; - fprintf(fp,"%*s shift-reduce %-7d",indent,ap->sp->name,rp->index); + fprintf(fp,"%*s shift-reduce %-7d",indent,ap->sp->name,rp->iRule); RulePrint(fp, rp, -1); break; } @@ -3071,10 +3507,10 @@ int PrintAction( case SRCONFLICT: case RRCONFLICT: fprintf(fp,"%*s reduce %-7d ** Parsing conflict **", - indent,ap->sp->name,ap->x.rp->index); + indent,ap->sp->name,ap->x.rp->iRule); break; case SSCONFLICT: - fprintf(fp,"%*s shift %-7d ** Parsing conflict **", + fprintf(fp,"%*s shift %-7d ** Parsing conflict **", indent,ap->sp->name,ap->x.stp->statenum); break; case SH_RESOLVED: @@ -3088,7 +3524,7 @@ int PrintAction( case RD_RESOLVED: if( showPrecedenceConflict ){ fprintf(fp,"%*s reduce %-7d -- dropped by precedence", - indent,ap->sp->name,ap->x.rp->index); + indent,ap->sp->name,ap->x.rp->iRule); }else{ result = 0; } @@ -3097,16 +3533,20 @@ int PrintAction( result = 0; break; } + if( result && ap->spOpt ){ + fprintf(fp," /* because %s==%s */", ap->sp->name, ap->spOpt->name); + } return result; } /* Generate the "*.out" log file */ void ReportOutput(struct lemon *lemp) { - int i; + int i, n; struct state *stp; struct config *cfp; struct action *ap; + struct rule *rp; FILE *fp; fp = file_open(lemp,".out","wb"); @@ -3119,7 +3559,7 @@ void ReportOutput(struct lemon *lemp) while( cfp ){ char buf[20]; if( cfp->dot==cfp->rp->nrhs ){ - lemon_sprintf(buf,"(%d)",cfp->rp->index); + lemon_sprintf(buf,"(%d)",cfp->rp->iRule); fprintf(fp," %5s ",buf); }else{ fprintf(fp," "); @@ -3142,6 +3582,7 @@ void ReportOutput(struct lemon *lemp) } fprintf(fp, "----------------------------------------------------\n"); fprintf(fp, "Symbols:\n"); + fprintf(fp, "The first-set of non-terminals is shown after the name.\n\n"); for(i=0; i<lemp->nsymbol; i++){ int j; struct symbol *sp; @@ -3159,19 +3600,52 @@ void ReportOutput(struct lemon *lemp) } } } + if( sp->prec>=0 ) fprintf(fp," (precedence=%d)", sp->prec); fprintf(fp, "\n"); } + fprintf(fp, "----------------------------------------------------\n"); + fprintf(fp, "Syntax-only Symbols:\n"); + fprintf(fp, "The following symbols never carry semantic content.\n\n"); + for(i=n=0; i<lemp->nsymbol; i++){ + int w; + struct symbol *sp = lemp->symbols[i]; + if( sp->bContent ) continue; + w = (int)strlen(sp->name); + if( n>0 && n+w>75 ){ + fprintf(fp,"\n"); + n = 0; + } + if( n>0 ){ + fprintf(fp, " "); + n++; + } + fprintf(fp, "%s", sp->name); + n += w; + } + if( n>0 ) fprintf(fp, "\n"); + fprintf(fp, "----------------------------------------------------\n"); + fprintf(fp, "Rules:\n"); + for(rp=lemp->rule; rp; rp=rp->next){ + fprintf(fp, "%4d: ", rp->iRule); + rule_print(fp, rp); + fprintf(fp,"."); + if( rp->precsym ){ + fprintf(fp," [%s precedence=%d]", + rp->precsym->name, rp->precsym->prec); + } + fprintf(fp,"\n"); + } fclose(fp); return; } /* Search for the file "name" which is in the same directory as -** the exacutable */ +** the executable */ PRIVATE char *pathsearch(char *argv0, char *name, int modemask) { const char *pathlist; - char *pathbufptr; - char *pathbuf; + char *pathbufptr = 0; + char *pathbuf = 0; char *path,*cp; char c; @@ -3183,14 +3657,14 @@ PRIVATE char *pathsearch(char *argv0, char *name, int modemask) if( cp ){ c = *cp; *cp = 0; - path = (char *)malloc( lemonStrlen(argv0) + lemonStrlen(name) + 2 ); + path = (char *)lemon_malloc( lemonStrlen(argv0) + lemonStrlen(name) + 2 ); if( path ) lemon_sprintf(path,"%s/%s",argv0,name); *cp = c; }else{ pathlist = getenv("PATH"); if( pathlist==0 ) pathlist = ".:/bin:/usr/bin"; - pathbuf = (char *) malloc( lemonStrlen(pathlist) + 1 ); - path = (char *)malloc( lemonStrlen(pathlist)+lemonStrlen(name)+2 ); + pathbuf = (char *) lemon_malloc( lemonStrlen(pathlist) + 1 ); + path = (char *)lemon_malloc( lemonStrlen(pathlist)+lemonStrlen(name)+2 ); if( (pathbuf != 0) && (path!=0) ){ pathbufptr = pathbuf; lemon_strcpy(pathbuf, pathlist); @@ -3205,8 +3679,8 @@ PRIVATE char *pathsearch(char *argv0, char *name, int modemask) else pathbuf = &cp[1]; if( access(path,modemask)==0 ) break; } - free(pathbufptr); } + lemon_free(pathbufptr); } return path; } @@ -3220,10 +3694,22 @@ PRIVATE int compute_action(struct lemon *lemp, struct action *ap) int act; switch( ap->type ){ case SHIFT: act = ap->x.stp->statenum; break; - case SHIFTREDUCE: act = ap->x.rp->index + lemp->nstate; break; - case REDUCE: act = ap->x.rp->index + lemp->nstate+lemp->nrule; break; - case ERROR: act = lemp->nstate + lemp->nrule*2; break; - case ACCEPT: act = lemp->nstate + lemp->nrule*2 + 1; break; + case SHIFTREDUCE: { + /* Since a SHIFT is inherent after a prior REDUCE, convert any + ** SHIFTREDUCE action with a nonterminal on the LHS into a simple + ** REDUCE action: */ + if( ap->sp->index>=lemp->nterminal + && (lemp->errsym==0 || ap->sp->index!=lemp->errsym->index) + ){ + act = lemp->minReduce + ap->x.rp->iRule; + }else{ + act = lemp->minShiftReduce + ap->x.rp->iRule; + } + break; + } + case REDUCE: act = lemp->minReduce + ap->x.rp->iRule; break; + case ERROR: act = lemp->errAction; break; + case ACCEPT: act = lemp->accAction; break; default: act = -1; break; } return act; @@ -3262,6 +3748,16 @@ PRIVATE void tplt_xfer(char *name, FILE *in, FILE *out, int *lineno) } } +/* Skip forward past the header of the template file to the first "%%" +*/ +PRIVATE void tplt_skip_header(FILE *in, int *lineno) +{ + char line[LINESIZE]; + while( fgets(line,LINESIZE,in) && (line[0]!='%' || line[1]!='%') ){ + (*lineno)++; + } +} + /* The next function finds the template file and opens it, returning ** a pointer to the opened file. */ PRIVATE FILE *tplt_open(struct lemon *lemp) @@ -3270,6 +3766,7 @@ PRIVATE FILE *tplt_open(struct lemon *lemp) char buf[1000]; FILE *in; char *tpltname; + char *toFree = 0; char *cp; /* first, see if user specified a template filename on the command line. */ @@ -3301,7 +3798,7 @@ PRIVATE FILE *tplt_open(struct lemon *lemp) }else if( access(templatename,004)==0 ){ tpltname = templatename; }else{ - tpltname = pathsearch(lemp->argv0,templatename,0); + toFree = tpltname = pathsearch(lemp->argv[0],templatename,0); } if( tpltname==0 ){ fprintf(stderr,"Can't find the parser driver template file \"%s\".\n", @@ -3311,10 +3808,10 @@ PRIVATE FILE *tplt_open(struct lemon *lemp) } in = fopen(tpltname,"rb"); if( in==0 ){ - fprintf(stderr,"Can't open the template file \"%s\".\n",templatename); + fprintf(stderr,"Can't open the template file \"%s\".\n",tpltname); lemp->errorcnt++; - return 0; } + lemon_free(toFree); return in; } @@ -3344,7 +3841,7 @@ PRIVATE void tplt_print(FILE *out, struct lemon *lemp, char *str, int *lineno) (*lineno)++; } if (!lemp->nolinenosflag) { - (*lineno)++; tplt_linedir(out,*lineno,lemp->outname); + (*lineno)++; tplt_linedir(out,*lineno,lemp->outname); } return; } @@ -3389,8 +3886,8 @@ void emit_destructor_code( fputc(*cp,out); } fprintf(out,"\n"); (*lineno)++; - if (!lemp->nolinenosflag) { - (*lineno)++; tplt_linedir(out,*lineno,lemp->outname); + if (!lemp->nolinenosflag) { + (*lineno)++; tplt_linedir(out,*lineno,lemp->outname); } fprintf(out,"}\n"); (*lineno)++; return; @@ -3430,6 +3927,7 @@ PRIVATE char *append_str(const char *zText, int n, int p1, int p2){ int c; char zInt[40]; if( zText==0 ){ + if( used==0 && z!=0 ) z[0] = 0; used = 0; return z; } @@ -3442,7 +3940,7 @@ PRIVATE char *append_str(const char *zText, int n, int p1, int p2){ } if( (int) (n+sizeof(zInt)*2+used) >= alloced ){ alloced = n + sizeof(zInt)*2 + used + 200; - z = (char *) realloc(z, alloced); + z = (char *) lemon_realloc(z, alloced); } if( z==0 ) return empty; while( n-- > 0 ){ @@ -3463,15 +3961,23 @@ PRIVATE char *append_str(const char *zText, int n, int p1, int p2){ } /* -** zCode is a string that is the action associated with a rule. Expand -** the symbols in this string so that the refer to elements of the parser -** stack. +** Write and transform the rp->code string so that symbols are expanded. +** Populate the rp->codePrefix and rp->codeSuffix strings, as appropriate. +** +** Return 1 if the expanded code requires that "yylhsminor" local variable +** to be defined. */ -PRIVATE void translate_code(struct lemon *lemp, struct rule *rp){ +PRIVATE int translate_code(struct lemon *lemp, struct rule *rp){ char *cp, *xp; int i; - char lhsused = 0; /* True if the LHS element has been used */ - char used[MAXRHS]; /* True for each RHS element which is used */ + int rc = 0; /* True if yylhsminor is used */ + int dontUseRhs0 = 0; /* If true, use of left-most RHS label is illegal */ + const char *zSkip = 0; /* The zOvwrt comment within rp->code, or NULL */ + char lhsused = 0; /* True if the LHS element has been used */ + char lhsdirect; /* True if LHS writes directly into stack */ + char used[MAXRHS]; /* True for each RHS element which is used */ + char zLhs[50]; /* Convert the LHS symbol into this string */ + char zOvwrt[900]; /* Comment that to allow LHS to overwrite RHS */ for(i=0; i<rp->nrhs; i++) used[i] = 0; lhsused = 0; @@ -3480,25 +3986,89 @@ PRIVATE void translate_code(struct lemon *lemp, struct rule *rp){ static char newlinestr[2] = { '\n', '\0' }; rp->code = newlinestr; rp->line = rp->ruleline; + rp->noCode = 1; + }else{ + rp->noCode = 0; + } + + + if( rp->nrhs==0 ){ + /* If there are no RHS symbols, then writing directly to the LHS is ok */ + lhsdirect = 1; + }else if( rp->rhsalias[0]==0 ){ + /* The left-most RHS symbol has no value. LHS direct is ok. But + ** we have to call the destructor on the RHS symbol first. */ + lhsdirect = 1; + if( has_destructor(rp->rhs[0],lemp) ){ + append_str(0,0,0,0); + append_str(" yy_destructor(yypParser,%d,&yymsp[%d].minor);\n", 0, + rp->rhs[0]->index,1-rp->nrhs); + rp->codePrefix = Strsafe(append_str(0,0,0,0)); + rp->noCode = 0; + } + }else if( rp->lhsalias==0 ){ + /* There is no LHS value symbol. */ + lhsdirect = 1; + }else if( strcmp(rp->lhsalias,rp->rhsalias[0])==0 ){ + /* The LHS symbol and the left-most RHS symbol are the same, so + ** direct writing is allowed */ + lhsdirect = 1; + lhsused = 1; + used[0] = 1; + if( rp->lhs->dtnum!=rp->rhs[0]->dtnum ){ + ErrorMsg(lemp->filename,rp->ruleline, + "%s(%s) and %s(%s) share the same label but have " + "different datatypes.", + rp->lhs->name, rp->lhsalias, rp->rhs[0]->name, rp->rhsalias[0]); + lemp->errorcnt++; + } + }else{ + lemon_sprintf(zOvwrt, "/*%s-overwrites-%s*/", + rp->lhsalias, rp->rhsalias[0]); + zSkip = strstr(rp->code, zOvwrt); + if( zSkip!=0 ){ + /* The code contains a special comment that indicates that it is safe + ** for the LHS label to overwrite left-most RHS label. */ + lhsdirect = 1; + }else{ + lhsdirect = 0; + } + } + if( lhsdirect ){ + lemon_sprintf(zLhs, "yymsp[%d].minor.yy%d",1-rp->nrhs,rp->lhs->dtnum); + }else{ + rc = 1; + lemon_sprintf(zLhs, "yylhsminor.yy%d",rp->lhs->dtnum); } append_str(0,0,0,0); /* This const cast is wrong but harmless, if we're careful. */ for(cp=(char *)rp->code; *cp; cp++){ + if( cp==zSkip ){ + append_str(zOvwrt,0,0,0); + cp += lemonStrlen(zOvwrt)-1; + dontUseRhs0 = 1; + continue; + } if( ISALPHA(*cp) && (cp==rp->code || (!ISALNUM(cp[-1]) && cp[-1]!='_')) ){ char saved; for(xp= &cp[1]; ISALNUM(*xp) || *xp=='_'; xp++); saved = *xp; *xp = 0; if( rp->lhsalias && strcmp(cp,rp->lhsalias)==0 ){ - append_str("yygotominor.yy%d",0,rp->lhs->dtnum,0); + append_str(zLhs,0,0,0); cp = xp; lhsused = 1; }else{ for(i=0; i<rp->nrhs; i++){ if( rp->rhsalias[i] && strcmp(cp,rp->rhsalias[i])==0 ){ - if( cp!=rp->code && cp[-1]=='@' ){ + if( i==0 && dontUseRhs0 ){ + ErrorMsg(lemp->filename,rp->ruleline, + "Label %s used after '%s'.", + rp->rhsalias[0], zOvwrt); + lemp->errorcnt++; + }else if( cp!=rp->code && cp[-1]=='@' ){ /* If the argument is of the form @X then substituted ** the token number of X, not the value of X */ append_str("yymsp[%d].major",-1,i-rp->nrhs+1,0); @@ -3523,6 +4093,11 @@ PRIVATE void translate_code(struct lemon *lemp, struct rule *rp){ append_str(cp, 1, 0, 0); } /* End loop */ + /* Main code generation completed */ + cp = append_str(0,0,0,0); + if( cp && cp[0] ) rp->code = Strsafe(cp); + append_str(0,0,0,0); + /* Check to make sure the LHS has been used */ if( rp->lhsalias && !lhsused ){ ErrorMsg(lemp->filename,rp->ruleline, @@ -3531,30 +4106,61 @@ PRIVATE void translate_code(struct lemon *lemp, struct rule *rp){ lemp->errorcnt++; } - /* Generate destructor code for RHS symbols which are not used in the - ** reduce code */ + /* Generate destructor code for RHS minor values which are not referenced. + ** Generate error messages for unused labels and duplicate labels. + */ for(i=0; i<rp->nrhs; i++){ - if( rp->rhsalias[i] && !used[i] ){ - ErrorMsg(lemp->filename,rp->ruleline, - "Label %s for \"%s(%s)\" is never used.", - rp->rhsalias[i],rp->rhs[i]->name,rp->rhsalias[i]); - lemp->errorcnt++; - }else if( rp->rhsalias[i]==0 ){ - if( has_destructor(rp->rhs[i],lemp) ){ - append_str(" yy_destructor(yypParser,%d,&yymsp[%d].minor);\n", 0, - rp->rhs[i]->index,i-rp->nrhs+1); - }else{ - /* No destructor defined for this term */ + if( rp->rhsalias[i] ){ + if( i>0 ){ + int j; + if( rp->lhsalias && strcmp(rp->lhsalias,rp->rhsalias[i])==0 ){ + ErrorMsg(lemp->filename,rp->ruleline, + "%s(%s) has the same label as the LHS but is not the left-most " + "symbol on the RHS.", + rp->rhs[i]->name, rp->rhsalias[i]); + lemp->errorcnt++; + } + for(j=0; j<i; j++){ + if( rp->rhsalias[j] && strcmp(rp->rhsalias[j],rp->rhsalias[i])==0 ){ + ErrorMsg(lemp->filename,rp->ruleline, + "Label %s used for multiple symbols on the RHS of a rule.", + rp->rhsalias[i]); + lemp->errorcnt++; + break; + } + } } + if( !used[i] ){ + ErrorMsg(lemp->filename,rp->ruleline, + "Label %s for \"%s(%s)\" is never used.", + rp->rhsalias[i],rp->rhs[i]->name,rp->rhsalias[i]); + lemp->errorcnt++; + } + }else if( i>0 && has_destructor(rp->rhs[i],lemp) ){ + append_str(" yy_destructor(yypParser,%d,&yymsp[%d].minor);\n", 0, + rp->rhs[i]->index,i-rp->nrhs+1); } } - if( rp->code ){ - cp = append_str(0,0,0,0); - rp->code = Strsafe(cp?cp:""); + + /* If unable to write LHS values directly into the stack, write the + ** saved LHS value now. */ + if( lhsdirect==0 ){ + append_str(" yymsp[%d].minor.yy%d = ", 0, 1-rp->nrhs, rp->lhs->dtnum); + append_str(zLhs, 0, 0, 0); + append_str(";\n", 0, 0, 0); + } + + /* Suffix code generation complete */ + cp = append_str(0,0,0,0); + if( cp && cp[0] ){ + rp->codeSuffix = Strsafe(cp); + rp->noCode = 0; } + + return rc; } -/* +/* ** Generate code which executes when the rule "rp" is reduced. Write ** the code to "out". Make sure lineno stays up-to-date. */ @@ -3566,6 +4172,12 @@ PRIVATE void emit_code( ){ const char *cp; + /* Setup code prior to the #line directive */ + if( rp->codePrefix && rp->codePrefix[0] ){ + fprintf(out, "{%s", rp->codePrefix); + for(cp=rp->codePrefix; *cp; cp++){ if( *cp=='\n' ) (*lineno)++; } + } + /* Generate code to do the reduce action */ if( rp->code ){ if( !lemp->nolinenosflag ){ @@ -3573,15 +4185,23 @@ PRIVATE void emit_code( tplt_linedir(out,rp->line,lemp->filename); } fprintf(out,"{%s",rp->code); - for(cp=rp->code; *cp; cp++){ - if( *cp=='\n' ) (*lineno)++; - } /* End loop */ + for(cp=rp->code; *cp; cp++){ if( *cp=='\n' ) (*lineno)++; } fprintf(out,"}\n"); (*lineno)++; if( !lemp->nolinenosflag ){ (*lineno)++; tplt_linedir(out,*lineno,lemp->outname); } - } /* End if( rp->code ) */ + } + + /* Generate breakdown code that occurs after the #line directive */ + if( rp->codeSuffix && rp->codeSuffix[0] ){ + fprintf(out, "%s", rp->codeSuffix); + for(cp=rp->codeSuffix; *cp; cp++){ if( *cp=='\n' ) (*lineno)++; } + } + + if( rp->codePrefix ){ + fprintf(out, "}\n"); (*lineno)++; + } return; } @@ -3599,7 +4219,7 @@ void print_stack_union( int *plineno, /* Pointer to the line number */ int mhflag /* True if generating makeheaders output */ ){ - int lineno = *plineno; /* The line number of the output */ + int lineno; /* The line number of the output */ char **types; /* A hash table of datatypes */ int arraysize; /* Size of the "types" array */ int maxdtlength; /* Maximum length of any ".datatype" field. */ @@ -3610,7 +4230,7 @@ void print_stack_union( /* Allocate and initialize types[] and allocate stddt[] */ arraysize = lemp->nsymbol * 2; - types = (char**)calloc( arraysize, sizeof(char*) ); + types = (char**)lemon_calloc( arraysize, sizeof(char*) ); if( types==0 ){ fprintf(stderr,"Out of memory.\n"); exit(1); @@ -3627,7 +4247,7 @@ void print_stack_union( len = lemonStrlen(sp->datatype); if( len>maxdtlength ) maxdtlength = len; } - stddt = (char*)malloc( maxdtlength*2 + 1 ); + stddt = (char*)lemon_malloc( maxdtlength*2 + 1 ); if( stddt==0 ){ fprintf(stderr,"Out of memory.\n"); exit(1); @@ -3676,7 +4296,7 @@ void print_stack_union( } if( types[hash]==0 ){ sp->dtnum = hash + 1; - types[hash] = (char*)malloc( lemonStrlen(stddt)+1 ); + types[hash] = (char*)lemon_malloc( lemonStrlen(stddt)+1 ); if( types[hash]==0 ){ fprintf(stderr,"Out of memory.\n"); exit(1); @@ -3698,13 +4318,13 @@ void print_stack_union( for(i=0; i<arraysize; i++){ if( types[i]==0 ) continue; fprintf(out," %s yy%d;\n",types[i],i+1); lineno++; - free(types[i]); + lemon_free(types[i]); } - if( lemp->errsym->useCnt ){ + if( lemp->errsym && lemp->errsym->useCnt ){ fprintf(out," int yy%d;\n",lemp->errsym->dtnum); lineno++; } - free(stddt); - free(types); + lemon_free(stddt); + lemon_free(types); fprintf(out,"} YYMINORTYPE;\n"); lineno++; *plineno = lineno; } @@ -3791,22 +4411,31 @@ static void writeRuleText(FILE *out, struct rule *rp){ /* Generate C source code for the parser */ void ReportTable( struct lemon *lemp, - int mhflag /* Output in makeheaders format if true */ + int mhflag, /* Output in makeheaders format if true */ + int sqlFlag /* Generate the *.sql file too */ ){ - FILE *out, *in; - char line[LINESIZE]; + FILE *out, *in, *sql; int lineno; struct state *stp; struct action *ap; struct rule *rp; struct acttab *pActtab; - int i, j, n, sz; + int i, j, n, sz, mn, mx; + int nLookAhead; int szActionType; /* sizeof(YYACTIONTYPE) */ int szCodeType; /* sizeof(YYCODETYPE) */ const char *name; int mnTknOfst, mxTknOfst; int mnNtOfst, mxNtOfst; struct axset *ax; + char *prefix; + + lemp->minShiftReduce = lemp->nstate; + lemp->errAction = lemp->minShiftReduce + lemp->nrule; + lemp->accAction = lemp->errAction + 1; + lemp->noAction = lemp->accAction + 1; + lemp->minReduce = lemp->noAction + 1; + lemp->maxAction = lemp->minReduce + lemp->nrule; in = tplt_open(lemp); if( in==0 ) return; @@ -3815,38 +4444,140 @@ void ReportTable( fclose(in); return; } + if( sqlFlag==0 ){ + sql = 0; + }else{ + sql = file_open(lemp, ".sql", "wb"); + if( sql==0 ){ + fclose(in); + fclose(out); + return; + } + fprintf(sql, + "BEGIN;\n" + "CREATE TABLE symbol(\n" + " id INTEGER PRIMARY KEY,\n" + " name TEXT NOT NULL,\n" + " isTerminal BOOLEAN NOT NULL,\n" + " fallback INTEGER REFERENCES symbol" + " DEFERRABLE INITIALLY DEFERRED\n" + ");\n" + ); + for(i=0; i<lemp->nsymbol; i++){ + fprintf(sql, + "INSERT INTO symbol(id,name,isTerminal,fallback)" + "VALUES(%d,'%s',%s", + i, lemp->symbols[i]->name, + i<lemp->nterminal ? "TRUE" : "FALSE" + ); + if( lemp->symbols[i]->fallback ){ + fprintf(sql, ",%d);\n", lemp->symbols[i]->fallback->index); + }else{ + fprintf(sql, ",NULL);\n"); + } + } + fprintf(sql, + "CREATE TABLE rule(\n" + " ruleid INTEGER PRIMARY KEY,\n" + " lhs INTEGER REFERENCES symbol(id),\n" + " txt TEXT\n" + ");\n" + "CREATE TABLE rulerhs(\n" + " ruleid INTEGER REFERENCES rule(ruleid),\n" + " pos INTEGER,\n" + " sym INTEGER REFERENCES symbol(id)\n" + ");\n" + ); + for(i=0, rp=lemp->rule; rp; rp=rp->next, i++){ + assert( i==rp->iRule ); + fprintf(sql, + "INSERT INTO rule(ruleid,lhs,txt)VALUES(%d,%d,'", + rp->iRule, rp->lhs->index + ); + writeRuleText(sql, rp); + fprintf(sql,"');\n"); + for(j=0; j<rp->nrhs; j++){ + struct symbol *sp = rp->rhs[j]; + if( sp->type!=MULTITERMINAL ){ + fprintf(sql, + "INSERT INTO rulerhs(ruleid,pos,sym)VALUES(%d,%d,%d);\n", + i,j,sp->index + ); + }else{ + int k; + for(k=0; k<sp->nsubsym; k++){ + fprintf(sql, + "INSERT INTO rulerhs(ruleid,pos,sym)VALUES(%d,%d,%d);\n", + i,j,sp->subsym[k]->index + ); + } + } + } + } + fprintf(sql, "COMMIT;\n"); + } lineno = 1; - tplt_xfer(lemp->name,in,out,&lineno); + + fprintf(out, + "/* This file is automatically generated by Lemon from input grammar\n" + "** source file \"%s\"", lemp->filename); lineno++; + if( nDefineUsed==0 ){ + fprintf(out, ".\n*/\n"); lineno += 2; + }else{ + fprintf(out, " with these options:\n**\n"); lineno += 2; + for(i=0; i<nDefine; i++){ + if( !bDefineUsed[i] ) continue; + fprintf(out, "** -D%s\n", azDefine[i]); lineno++; + } + fprintf(out, "*/\n"); lineno++; + } + + /* The first %include directive begins with a C-language comment, + ** then skip over the header comment of the template file + */ + if( lemp->include==0 ) lemp->include = ""; + for(i=0; ISSPACE(lemp->include[i]); i++){ + if( lemp->include[i]=='\n' ){ + lemp->include += i+1; + i = -1; + } + } + if( lemp->include[0]=='/' ){ + tplt_skip_header(in,&lineno); + }else{ + tplt_xfer(lemp->name,in,out,&lineno); + } /* Generate the include code, if any */ tplt_print(out,lemp,lemp->include,&lineno); if( mhflag ){ char *incName = file_makename(lemp, ".h"); fprintf(out,"#include \"%s\"\n", incName); lineno++; - free(incName); + lemon_free(incName); } tplt_xfer(lemp->name,in,out,&lineno); /* Generate #defines for all tokens */ + if( lemp->tokenprefix ) prefix = lemp->tokenprefix; + else prefix = ""; if( mhflag ){ - const char *prefix; fprintf(out,"#if INTERFACE\n"); lineno++; - if( lemp->tokenprefix ) prefix = lemp->tokenprefix; - else prefix = ""; - for(i=1; i<lemp->nterminal; i++){ - fprintf(out,"#define %s%-30s %2d\n",prefix,lemp->symbols[i]->name,i); - lineno++; - } - fprintf(out,"#endif\n"); lineno++; + }else{ + fprintf(out,"#ifndef %s%s\n", prefix, lemp->symbols[1]->name); + } + for(i=1; i<lemp->nterminal; i++){ + fprintf(out,"#define %s%-30s %2d\n",prefix,lemp->symbols[i]->name,i); + lineno++; } + fprintf(out,"#endif\n"); lineno++; tplt_xfer(lemp->name,in,out,&lineno); /* Generate the defines */ fprintf(out,"#define YYCODETYPE %s\n", - minimum_size_type(0, lemp->nsymbol+1, &szCodeType)); lineno++; - fprintf(out,"#define YYNOCODE %d\n",lemp->nsymbol+1); lineno++; + minimum_size_type(0, lemp->nsymbol, &szCodeType)); lineno++; + fprintf(out,"#define YYNOCODE %d\n",lemp->nsymbol); lineno++; fprintf(out,"#define YYACTIONTYPE %s\n", - minimum_size_type(0,lemp->nstate+lemp->nrule*2+5,&szActionType)); lineno++; + minimum_size_type(0,lemp->maxAction,&szActionType)); lineno++; if( lemp->wildcard ){ fprintf(out,"#define YYWILDCARD %d\n", lemp->wildcard->index); lineno++; @@ -3869,20 +4600,55 @@ void ReportTable( while( i>=1 && (ISALNUM(lemp->arg[i-1]) || lemp->arg[i-1]=='_') ) i--; fprintf(out,"#define %sARG_SDECL %s;\n",name,lemp->arg); lineno++; fprintf(out,"#define %sARG_PDECL ,%s\n",name,lemp->arg); lineno++; - fprintf(out,"#define %sARG_FETCH %s = yypParser->%s\n", + fprintf(out,"#define %sARG_PARAM ,%s\n",name,&lemp->arg[i]); lineno++; + fprintf(out,"#define %sARG_FETCH %s=yypParser->%s;\n", name,lemp->arg,&lemp->arg[i]); lineno++; - fprintf(out,"#define %sARG_STORE yypParser->%s = %s\n", + fprintf(out,"#define %sARG_STORE yypParser->%s=%s;\n", name,&lemp->arg[i],&lemp->arg[i]); lineno++; }else{ - fprintf(out,"#define %sARG_SDECL\n",name); lineno++; - fprintf(out,"#define %sARG_PDECL\n",name); lineno++; + fprintf(out,"#define %sARG_SDECL\n",name); lineno++; + fprintf(out,"#define %sARG_PDECL\n",name); lineno++; + fprintf(out,"#define %sARG_PARAM\n",name); lineno++; fprintf(out,"#define %sARG_FETCH\n",name); lineno++; fprintf(out,"#define %sARG_STORE\n",name); lineno++; } + if( lemp->reallocFunc ){ + fprintf(out,"#define YYREALLOC %s\n", lemp->reallocFunc); lineno++; + }else{ + fprintf(out,"#define YYREALLOC realloc\n"); lineno++; + } + if( lemp->freeFunc ){ + fprintf(out,"#define YYFREE %s\n", lemp->freeFunc); lineno++; + }else{ + fprintf(out,"#define YYFREE free\n"); lineno++; + } + if( lemp->reallocFunc && lemp->freeFunc ){ + fprintf(out,"#define YYDYNSTACK 1\n"); lineno++; + }else{ + fprintf(out,"#define YYDYNSTACK 0\n"); lineno++; + } + if( lemp->ctx && lemp->ctx[0] ){ + i = lemonStrlen(lemp->ctx); + while( i>=1 && ISSPACE(lemp->ctx[i-1]) ) i--; + while( i>=1 && (ISALNUM(lemp->ctx[i-1]) || lemp->ctx[i-1]=='_') ) i--; + fprintf(out,"#define %sCTX_SDECL %s;\n",name,lemp->ctx); lineno++; + fprintf(out,"#define %sCTX_PDECL ,%s\n",name,lemp->ctx); lineno++; + fprintf(out,"#define %sCTX_PARAM ,%s\n",name,&lemp->ctx[i]); lineno++; + fprintf(out,"#define %sCTX_FETCH %s=yypParser->%s;\n", + name,lemp->ctx,&lemp->ctx[i]); lineno++; + fprintf(out,"#define %sCTX_STORE yypParser->%s=%s;\n", + name,&lemp->ctx[i],&lemp->ctx[i]); lineno++; + }else{ + fprintf(out,"#define %sCTX_SDECL\n",name); lineno++; + fprintf(out,"#define %sCTX_PDECL\n",name); lineno++; + fprintf(out,"#define %sCTX_PARAM\n",name); lineno++; + fprintf(out,"#define %sCTX_FETCH\n",name); lineno++; + fprintf(out,"#define %sCTX_STORE\n",name); lineno++; + } if( mhflag ){ fprintf(out,"#endif\n"); lineno++; } - if( lemp->errsym->useCnt ){ + if( lemp->errsym && lemp->errsym->useCnt ){ fprintf(out,"#define YYERRORSYMBOL %d\n",lemp->errsym->index); lineno++; fprintf(out,"#define YYERRSYMDT yy%d\n",lemp->errsym->dtnum); lineno++; } @@ -3894,7 +4660,7 @@ void ReportTable( ** table must be computed before generating the YYNSTATE macro because ** we need to know how many states can be eliminated. */ - ax = (struct axset *) calloc(lemp->nxstate*2, sizeof(ax[0])); + ax = (struct axset *) lemon_calloc(lemp->nxstate*2, sizeof(ax[0])); if( ax==0 ){ fprintf(stderr,"malloc failed\n"); exit(1); @@ -3914,7 +4680,7 @@ void ReportTable( ** of placing the largest action sets first */ for(i=0; i<lemp->nxstate*2; i++) ax[i].iOrder = i; qsort(ax, lemp->nxstate*2, sizeof(ax[0]), axset_compare); - pActtab = acttab_alloc(); + pActtab = acttab_alloc(lemp->nsymbol, lemp->nterminal); for(i=0; i<lemp->nxstate*2 && ax[i].nAction>0; i++){ stp = ax[i].stp; if( ax[i].isTkn ){ @@ -3925,7 +4691,7 @@ void ReportTable( if( action<0 ) continue; acttab_action(pActtab, ap->sp->index, action); } - stp->iTknOfst = acttab_insert(pActtab); + stp->iTknOfst = acttab_insert(pActtab, 1); if( stp->iTknOfst<mnTknOfst ) mnTknOfst = stp->iTknOfst; if( stp->iTknOfst>mxTknOfst ) mxTknOfst = stp->iTknOfst; }else{ @@ -3937,7 +4703,7 @@ void ReportTable( if( action<0 ) continue; acttab_action(pActtab, ap->sp->index, action); } - stp->iNtOfst = acttab_insert(pActtab); + stp->iNtOfst = acttab_insert(pActtab, 0); if( stp->iNtOfst<mnNtOfst ) mnNtOfst = stp->iNtOfst; if( stp->iNtOfst>mxNtOfst ) mxNtOfst = stp->iNtOfst; } @@ -3952,22 +4718,54 @@ void ReportTable( } #endif } - free(ax); + lemon_free(ax); + + /* Mark rules that are actually used for reduce actions after all + ** optimizations have been applied + */ + for(rp=lemp->rule; rp; rp=rp->next) rp->doesReduce = LEMON_FALSE; + for(i=0; i<lemp->nxstate; i++){ + for(ap=lemp->sorted[i]->ap; ap; ap=ap->next){ + if( ap->type==REDUCE || ap->type==SHIFTREDUCE ){ + ap->x.rp->doesReduce = 1; + } + } + } /* Finish rendering the constants now that the action table has ** been computed */ fprintf(out,"#define YYNSTATE %d\n",lemp->nxstate); lineno++; fprintf(out,"#define YYNRULE %d\n",lemp->nrule); lineno++; + fprintf(out,"#define YYNRULE_WITH_ACTION %d\n",lemp->nruleWithAction); + lineno++; + fprintf(out,"#define YYNTOKEN %d\n",lemp->nterminal); lineno++; fprintf(out,"#define YY_MAX_SHIFT %d\n",lemp->nxstate-1); lineno++; - fprintf(out,"#define YY_MIN_SHIFTREDUCE %d\n",lemp->nstate); lineno++; - i = lemp->nstate + lemp->nrule; + i = lemp->minShiftReduce; + fprintf(out,"#define YY_MIN_SHIFTREDUCE %d\n",i); lineno++; + i += lemp->nrule; fprintf(out,"#define YY_MAX_SHIFTREDUCE %d\n", i-1); lineno++; - fprintf(out,"#define YY_MIN_REDUCE %d\n", i); lineno++; - i = lemp->nstate + lemp->nrule*2; + fprintf(out,"#define YY_ERROR_ACTION %d\n", lemp->errAction); lineno++; + fprintf(out,"#define YY_ACCEPT_ACTION %d\n", lemp->accAction); lineno++; + fprintf(out,"#define YY_NO_ACTION %d\n", lemp->noAction); lineno++; + fprintf(out,"#define YY_MIN_REDUCE %d\n", lemp->minReduce); lineno++; + i = lemp->minReduce + lemp->nrule; fprintf(out,"#define YY_MAX_REDUCE %d\n", i-1); lineno++; - fprintf(out,"#define YY_ERROR_ACTION %d\n", i); lineno++; - fprintf(out,"#define YY_ACCEPT_ACTION %d\n", i+1); lineno++; - fprintf(out,"#define YY_NO_ACTION %d\n", i+2); lineno++; + + /* Minimum and maximum token values that have a destructor */ + mn = mx = 0; + for(i=0; i<lemp->nsymbol; i++){ + struct symbol *sp = lemp->symbols[i]; + + if( sp && sp->type!=TERMINAL && sp->destructor ){ + if( mn==0 || sp->index<mn ) mn = sp->index; + if( sp->index>mx ) mx = sp->index; + } + } + if( lemp->tokendest ) mn = 0; + if( lemp->vardest ) mx = lemp->nsymbol-1; + fprintf(out,"#define YY_MIN_DSTRCTR %d\n", mn); lineno++; + fprintf(out,"#define YY_MAX_DSTRCTR %d\n", mx); lineno++; + tplt_xfer(lemp->name,in,out,&lineno); /* Now output the action table and its associates: @@ -3983,13 +4781,13 @@ void ReportTable( */ /* Output the yy_action table */ - lemp->nactiontab = n = acttab_size(pActtab); + lemp->nactiontab = n = acttab_action_size(pActtab); lemp->tablesize += n*szActionType; fprintf(out,"#define YY_ACTTAB_COUNT (%d)\n", n); lineno++; fprintf(out,"static const YYACTIONTYPE yy_action[] = {\n"); lineno++; for(i=j=0; i<n; i++){ int action = acttab_yyaction(pActtab, i); - if( action<0 ) action = lemp->nstate + lemp->nrule + 2; + if( action<0 ) action = lemp->noAction; if( j==0 ) fprintf(out," /* %5d */ ", i); fprintf(out, " %4d,", action); if( j==9 || i==n-1 ){ @@ -4002,6 +4800,7 @@ void ReportTable( fprintf(out, "};\n"); lineno++; /* Output the yy_lookahead table */ + lemp->nlookaheadtab = n = acttab_lookahead_size(pActtab); lemp->tablesize += n*szCodeType; fprintf(out,"static const YYCODETYPE yy_lookahead[] = {\n"); lineno++; for(i=j=0; i<n; i++){ @@ -4009,30 +4808,46 @@ void ReportTable( if( la<0 ) la = lemp->nsymbol; if( j==0 ) fprintf(out," /* %5d */ ", i); fprintf(out, " %4d,", la); - if( j==9 || i==n-1 ){ + if( j==9 ){ + fprintf(out, "\n"); lineno++; + j = 0; + }else{ + j++; + } + } + /* Add extra entries to the end of the yy_lookahead[] table so that + ** yy_shift_ofst[]+iToken will always be a valid index into the array, + ** even for the largest possible value of yy_shift_ofst[] and iToken. */ + nLookAhead = lemp->nterminal + lemp->nactiontab; + while( i<nLookAhead ){ + if( j==0 ) fprintf(out," /* %5d */ ", i); + fprintf(out, " %4d,", lemp->nterminal); + if( j==9 ){ fprintf(out, "\n"); lineno++; j = 0; }else{ j++; } + i++; } + if( j>0 ){ fprintf(out, "\n"); lineno++; } fprintf(out, "};\n"); lineno++; /* Output the yy_shift_ofst[] table */ - fprintf(out, "#define YY_SHIFT_USE_DFLT (%d)\n", mnTknOfst-1); lineno++; n = lemp->nxstate; while( n>0 && lemp->sorted[n-1]->iTknOfst==NO_OFFSET ) n--; - fprintf(out, "#define YY_SHIFT_COUNT (%d)\n", n-1); lineno++; - fprintf(out, "#define YY_SHIFT_MIN (%d)\n", mnTknOfst); lineno++; - fprintf(out, "#define YY_SHIFT_MAX (%d)\n", mxTknOfst); lineno++; - fprintf(out, "static const %s yy_shift_ofst[] = {\n", - minimum_size_type(mnTknOfst-1, mxTknOfst, &sz)); lineno++; + fprintf(out, "#define YY_SHIFT_COUNT (%d)\n", n-1); lineno++; + fprintf(out, "#define YY_SHIFT_MIN (%d)\n", mnTknOfst); lineno++; + fprintf(out, "#define YY_SHIFT_MAX (%d)\n", mxTknOfst); lineno++; + fprintf(out, "static const %s yy_shift_ofst[] = {\n", + minimum_size_type(mnTknOfst, lemp->nterminal+lemp->nactiontab, &sz)); + lineno++; lemp->tablesize += n*sz; for(i=j=0; i<n; i++){ int ofst; stp = lemp->sorted[i]; ofst = stp->iTknOfst; - if( ofst==NO_OFFSET ) ofst = mnTknOfst - 1; + if( ofst==NO_OFFSET ) ofst = lemp->nactiontab; if( j==0 ) fprintf(out," /* %5d */ ", i); fprintf(out, " %4d,", ofst); if( j==9 || i==n-1 ){ @@ -4045,13 +4860,12 @@ void ReportTable( fprintf(out, "};\n"); lineno++; /* Output the yy_reduce_ofst[] table */ - fprintf(out, "#define YY_REDUCE_USE_DFLT (%d)\n", mnNtOfst-1); lineno++; n = lemp->nxstate; while( n>0 && lemp->sorted[n-1]->iNtOfst==NO_OFFSET ) n--; fprintf(out, "#define YY_REDUCE_COUNT (%d)\n", n-1); lineno++; fprintf(out, "#define YY_REDUCE_MIN (%d)\n", mnNtOfst); lineno++; fprintf(out, "#define YY_REDUCE_MAX (%d)\n", mxNtOfst); lineno++; - fprintf(out, "static const %s yy_reduce_ofst[] = {\n", + fprintf(out, "static const %s yy_reduce_ofst[] = {\n", minimum_size_type(mnNtOfst-1, mxNtOfst, &sz)); lineno++; lemp->tablesize += n*sz; for(i=j=0; i<n; i++){ @@ -4077,7 +4891,11 @@ void ReportTable( for(i=j=0; i<n; i++){ stp = lemp->sorted[i]; if( j==0 ) fprintf(out," /* %5d */ ", i); - fprintf(out, " %4d,", stp->iDfltReduce+lemp->nstate+lemp->nrule); + if( stp->iDfltReduce<0 ){ + fprintf(out, " %4d,", lemp->errAction); + }else{ + fprintf(out, " %4d,", stp->iDfltReduce + lemp->minReduce); + } if( j==9 || i==n-1 ){ fprintf(out, "\n"); lineno++; j = 0; @@ -4091,8 +4909,10 @@ void ReportTable( /* Generate the table of fallback tokens. */ if( lemp->has_fallback ){ - int mx = lemp->nterminal - 1; - while( mx>0 && lemp->symbols[mx]->fallback==0 ){ mx--; } + mx = lemp->nterminal - 1; + /* 2019-08-28: Generate fallback entries for every token to avoid + ** having to do a range check on the index */ + /* while( mx>0 && lemp->symbols[mx]->fallback==0 ){ mx--; } */ lemp->tablesize += (mx+1)*szCodeType; for(i=0; i<=mx; i++){ struct symbol *p = lemp->symbols[i]; @@ -4110,11 +4930,8 @@ void ReportTable( /* Generate a table containing the symbolic name of every symbol */ for(i=0; i<lemp->nsymbol; i++){ - lemon_sprintf(line,"\"%s\",",lemp->symbols[i]->name); - fprintf(out," %-15s",line); - if( (i&3)==3 ){ fprintf(out,"\n"); lineno++; } + fprintf(out," /* %4d */ \"%s\",\n",i, lemp->symbols[i]->name); lineno++; } - if( (i&3)!=0 ){ fprintf(out,"\n"); lineno++; } tplt_xfer(lemp->name,in,out,&lineno); /* Generate a table containing a text string that describes every @@ -4122,7 +4939,7 @@ void ReportTable( ** when tracing REDUCE actions. */ for(i=0, rp=lemp->rule; rp; rp=rp->next, i++){ - assert( rp->index==i ); + assert( rp->iRule==i ); fprintf(out," /* %3d */ \"", i); writeRuleText(out, rp); fprintf(out,"\",\n"); lineno++; @@ -4130,7 +4947,7 @@ void ReportTable( tplt_xfer(lemp->name,in,out,&lineno); /* Generate code which executes every time a symbol is popped from - ** the stack while processing errors or while destroying the parser. + ** the stack while processing errors or while destroying the parser. ** (In other words, generate the %destructor actions) */ if( lemp->tokendest ){ @@ -4158,7 +4975,7 @@ void ReportTable( if( sp==0 || sp->type==TERMINAL || sp->index<=0 || sp->destructor!=0 ) continue; if( once ){ - fprintf(out, " /* Default NON-TERMINAL Destructor */\n"); lineno++; + fprintf(out, " /* Default NON-TERMINAL Destructor */\n");lineno++; once = 0; } fprintf(out," case %d: /* %s */\n", sp->index, sp->name); lineno++; @@ -4172,6 +4989,7 @@ void ReportTable( for(i=0; i<lemp->nsymbol; i++){ struct symbol *sp = lemp->symbols[i]; if( sp==0 || sp->type==TERMINAL || sp->destructor==0 ) continue; + if( sp->destLineno<0 ) continue; /* Already emitted */ fprintf(out," case %d: /* %s */\n", sp->index, sp->name); lineno++; /* Combine duplicate destructors into a single case */ @@ -4182,7 +5000,7 @@ void ReportTable( && strcmp(sp->destructor,sp2->destructor)==0 ){ fprintf(out," case %d: /* %s */\n", sp2->index, sp2->name); lineno++; - sp2->destructor = 0; + sp2->destLineno = -1; /* Avoid emitting this destructor again */ } } @@ -4195,49 +5013,74 @@ void ReportTable( tplt_print(out,lemp,lemp->overflow,&lineno); tplt_xfer(lemp->name,in,out,&lineno); - /* Generate the table of rule information + /* Generate the tables of rule information. yyRuleInfoLhs[] and + ** yyRuleInfoNRhs[]. ** ** Note: This code depends on the fact that rules are number - ** sequentually beginning with 0. + ** sequentially beginning with 0. */ - for(rp=lemp->rule; rp; rp=rp->next){ - fprintf(out," { %d, %d },\n",rp->lhs->index,rp->nrhs); lineno++; + for(i=0, rp=lemp->rule; rp; rp=rp->next, i++){ + fprintf(out," %4d, /* (%d) ", rp->lhs->index, i); + rule_print(out, rp); + fprintf(out," */\n"); lineno++; + } + tplt_xfer(lemp->name,in,out,&lineno); + for(i=0, rp=lemp->rule; rp; rp=rp->next, i++){ + fprintf(out," %3d, /* (%d) ", -rp->nrhs, i); + rule_print(out, rp); + fprintf(out," */\n"); lineno++; } tplt_xfer(lemp->name,in,out,&lineno); /* Generate code which execution during each REDUCE action */ + i = 0; for(rp=lemp->rule; rp; rp=rp->next){ - translate_code(lemp, rp); + i += translate_code(lemp, rp); + } + if( i ){ + fprintf(out," YYMINORTYPE yylhsminor;\n"); lineno++; } /* First output rules other than the default: rule */ for(rp=lemp->rule; rp; rp=rp->next){ struct rule *rp2; /* Other rules with the same action */ - if( rp->code==0 ) continue; - if( rp->code[0]=='\n' && rp->code[1]==0 ) continue; /* Will be default: */ - fprintf(out," case %d: /* ", rp->index); + if( rp->codeEmitted ) continue; + if( rp->noCode ){ + /* No C code actions, so this will be part of the "default:" rule */ + continue; + } + fprintf(out," case %d: /* ", rp->iRule); writeRuleText(out, rp); fprintf(out, " */\n"); lineno++; for(rp2=rp->next; rp2; rp2=rp2->next){ - if( rp2->code==rp->code ){ - fprintf(out," case %d: /* ", rp2->index); + if( rp2->code==rp->code && rp2->codePrefix==rp->codePrefix + && rp2->codeSuffix==rp->codeSuffix ){ + fprintf(out," case %d: /* ", rp2->iRule); writeRuleText(out, rp2); - fprintf(out," */ yytestcase(yyruleno==%d);\n", rp2->index); lineno++; - rp2->code = 0; + fprintf(out," */ yytestcase(yyruleno==%d);\n", rp2->iRule); lineno++; + rp2->codeEmitted = 1; } } emit_code(out,rp,lemp,&lineno); fprintf(out," break;\n"); lineno++; - rp->code = 0; + rp->codeEmitted = 1; } /* Finally, output the default: rule. We choose as the default: all ** empty actions. */ fprintf(out," default:\n"); lineno++; for(rp=lemp->rule; rp; rp=rp->next){ - if( rp->code==0 ) continue; - assert( rp->code[0]=='\n' && rp->code[1]==0 ); - fprintf(out," /* (%d) ", rp->index); + if( rp->codeEmitted ) continue; + assert( rp->noCode ); + fprintf(out," /* (%d) ", rp->iRule); writeRuleText(out, rp); - fprintf(out, " */ yytestcase(yyruleno==%d);\n", rp->index); lineno++; + if( rp->neverReduce ){ + fprintf(out, " (NEVER REDUCES) */ assert(yyruleno!=%d);\n", + rp->iRule); lineno++; + }else if( rp->doesReduce ){ + fprintf(out, " */ yytestcase(yyruleno==%d);\n", rp->iRule); lineno++; + }else{ + fprintf(out, " (OPTIMIZED OUT) */ assert(yyruleno!=%d);\n", + rp->iRule); lineno++; + } } fprintf(out," break;\n"); lineno++; tplt_xfer(lemp->name,in,out,&lineno); @@ -4257,8 +5100,10 @@ void ReportTable( /* Append any addition code the user desires */ tplt_print(out,lemp,lemp->extracode,&lineno); + acttab_free(pActtab); fclose(in); fclose(out); + if( sql ) fclose(sql); return; } @@ -4293,7 +5138,7 @@ void ReportHeader(struct lemon *lemp) for(i=1; i<lemp->nterminal; i++){ fprintf(out,"#define %s%-30s %3d\n",prefix,lemp->symbols[i]->name,i); } - fclose(out); + fclose(out); } return; } @@ -4308,7 +5153,7 @@ void ReportHeader(struct lemon *lemp) void CompressTables(struct lemon *lemp) { struct state *stp; - struct action *ap, *ap2; + struct action *ap, *ap2, *nextap; struct rule *rp, *rp2, *rbest; int nbest, n; int i; @@ -4340,7 +5185,7 @@ void CompressTables(struct lemon *lemp) rbest = rp; } } - + /* Do not make a default if the number of rules to default ** is not at least 1 or if the wildcard token is a possible ** lookahead. @@ -4385,6 +5230,36 @@ void CompressTables(struct lemon *lemp) } } } + + /* If a SHIFTREDUCE action specifies a rule that has a single RHS term + ** (meaning that the SHIFTREDUCE will land back in the state where it + ** started) and if there is no C-code associated with the reduce action, + ** then we can go ahead and convert the action to be the same as the + ** action for the RHS of the rule. + */ + for(i=0; i<lemp->nstate; i++){ + stp = lemp->sorted[i]; + for(ap=stp->ap; ap; ap=nextap){ + nextap = ap->next; + if( ap->type!=SHIFTREDUCE ) continue; + rp = ap->x.rp; + if( rp->noCode==0 ) continue; + if( rp->nrhs!=1 ) continue; +#if 1 + /* Only apply this optimization to non-terminals. It would be OK to + ** apply it to terminal symbols too, but that makes the parser tables + ** larger. */ + if( ap->sp->index<lemp->nterminal ) continue; +#endif + /* If we reach this point, it means the optimization can be applied */ + nextap = ap; + for(ap2=stp->ap; ap2 && (ap2==ap || ap2->sp!=rp->lhs); ap2=ap2->next){} + assert( ap2!=0 ); + ap->spOpt = ap2->sp; + ap->type = ap2->type; + ap->x = ap2->x; + } + } } @@ -4424,7 +5299,7 @@ void ResortStates(struct lemon *lemp) for(i=0; i<lemp->nstate; i++){ stp = lemp->sorted[i]; stp->nTknAct = stp->nNtAct = 0; - stp->iDfltReduce = lemp->nrule; /* Init dflt action to "syntax error" */ + stp->iDfltReduce = -1; /* Init dflt action to "syntax error" */ stp->iTknOfst = NO_OFFSET; stp->iNtOfst = NO_OFFSET; for(ap=stp->ap; ap; ap=ap->next){ @@ -4436,7 +5311,7 @@ void ResortStates(struct lemon *lemp) stp->nNtAct++; }else{ assert( stp->autoReduce==0 || stp->pDfltReduce==ap->x.rp ); - stp->iDfltReduce = iAction - lemp->nstate - lemp->nrule; + stp->iDfltReduce = iAction; } } } @@ -4467,11 +5342,10 @@ void SetSize(int n) } /* Allocate a new set */ -char *SetNew(){ +char *SetNew(void){ char *s; - s = (char*)calloc( size, 1); + s = (char*)lemon_calloc( size, 1); if( s==0 ){ - extern void memory_error(); memory_error(); } return s; @@ -4480,7 +5354,7 @@ char *SetNew(){ /* Deallocate a set */ void SetFree(char *s) { - free(s); + lemon_free(s); } /* Add a new element to the set. Return TRUE if the element was added @@ -4539,7 +5413,7 @@ const char *Strsafe(const char *y) if( y==0 ) return 0; z = Strsafe_find(y); - if( z==0 && (cpy=(char *)malloc( lemonStrlen(y)+1 ))!=0 ){ + if( z==0 && (cpy=(char *)lemon_malloc( lemonStrlen(y)+1 ))!=0 ){ lemon_strcpy(cpy,y); z = cpy; Strsafe_insert(z); @@ -4573,15 +5447,15 @@ typedef struct s_x1node { static struct s_x1 *x1a; /* Allocate a new associative array */ -void Strsafe_init(){ +void Strsafe_init(void){ if( x1a ) return; - x1a = (struct s_x1*)malloc( sizeof(struct s_x1) ); + x1a = (struct s_x1*)lemon_malloc( sizeof(struct s_x1) ); if( x1a ){ x1a->size = 1024; x1a->count = 0; - x1a->tbl = (x1node*)calloc(1024, sizeof(x1node) + sizeof(x1node*)); + x1a->tbl = (x1node*)lemon_calloc(1024, sizeof(x1node) + sizeof(x1node*)); if( x1a->tbl==0 ){ - free(x1a); + lemon_free(x1a); x1a = 0; }else{ int i; @@ -4616,7 +5490,7 @@ int Strsafe_insert(const char *data) struct s_x1 array; array.size = arrSize = x1a->size*2; array.count = x1a->count; - array.tbl = (x1node*)calloc(arrSize, sizeof(x1node) + sizeof(x1node*)); + array.tbl = (x1node*)lemon_calloc(arrSize, sizeof(x1node)+sizeof(x1node*)); if( array.tbl==0 ) return 0; /* Fail due to malloc failure */ array.ht = (x1node**)&(array.tbl[arrSize]); for(i=0; i<arrSize; i++) array.ht[i] = 0; @@ -4631,7 +5505,8 @@ int Strsafe_insert(const char *data) newnp->from = &(array.ht[h]); array.ht[h] = newnp; } - free(x1a->tbl); + /* lemon_free(x1a->tbl); // This program was originally for 16-bit machines. + ** Don't worry about freeing memory on modern platforms. */ *x1a = array; } /* Insert the new data */ @@ -4671,7 +5546,7 @@ struct symbol *Symbol_new(const char *x) sp = Symbol_find(x); if( sp==0 ){ - sp = (struct symbol *)calloc(1, sizeof(struct symbol) ); + sp = (struct symbol *)lemon_calloc(1, sizeof(struct symbol) ); MemoryCheck(sp); sp->name = Strsafe(x); sp->type = ISUPPER(*x) ? TERMINAL : NONTERMINAL; @@ -4740,15 +5615,15 @@ typedef struct s_x2node { static struct s_x2 *x2a; /* Allocate a new associative array */ -void Symbol_init(){ +void Symbol_init(void){ if( x2a ) return; - x2a = (struct s_x2*)malloc( sizeof(struct s_x2) ); + x2a = (struct s_x2*)lemon_malloc( sizeof(struct s_x2) ); if( x2a ){ x2a->size = 128; x2a->count = 0; - x2a->tbl = (x2node*)calloc(128, sizeof(x2node) + sizeof(x2node*)); + x2a->tbl = (x2node*)lemon_calloc(128, sizeof(x2node) + sizeof(x2node*)); if( x2a->tbl==0 ){ - free(x2a); + lemon_free(x2a); x2a = 0; }else{ int i; @@ -4783,7 +5658,7 @@ int Symbol_insert(struct symbol *data, const char *key) struct s_x2 array; array.size = arrSize = x2a->size*2; array.count = x2a->count; - array.tbl = (x2node*)calloc(arrSize, sizeof(x2node) + sizeof(x2node*)); + array.tbl = (x2node*)lemon_calloc(arrSize, sizeof(x2node)+sizeof(x2node*)); if( array.tbl==0 ) return 0; /* Fail due to malloc failure */ array.ht = (x2node**)&(array.tbl[arrSize]); for(i=0; i<arrSize; i++) array.ht[i] = 0; @@ -4799,7 +5674,9 @@ int Symbol_insert(struct symbol *data, const char *key) newnp->from = &(array.ht[h]); array.ht[h] = newnp; } - free(x2a->tbl); + /* lemon_free(x2a->tbl); // This program was originally written for 16-bit + ** machines. Don't worry about freeing this trivial amount of memory + ** on modern platforms. Just leak it. */ *x2a = array; } /* Insert the new data */ @@ -4858,7 +5735,7 @@ struct symbol **Symbol_arrayof() int i,arrSize; if( x2a==0 ) return 0; arrSize = x2a->count; - array = (struct symbol **)calloc(arrSize, sizeof(struct symbol *)); + array = (struct symbol **)lemon_calloc(arrSize, sizeof(struct symbol *)); if( array ){ for(i=0; i<arrSize; i++) array[i] = x2a->tbl[i].data; } @@ -4906,7 +5783,7 @@ PRIVATE unsigned statehash(struct config *a) struct state *State_new() { struct state *newstate; - newstate = (struct state *)calloc(1, sizeof(struct state) ); + newstate = (struct state *)lemon_calloc(1, sizeof(struct state) ); MemoryCheck(newstate); return newstate; } @@ -4937,15 +5814,15 @@ typedef struct s_x3node { static struct s_x3 *x3a; /* Allocate a new associative array */ -void State_init(){ +void State_init(void){ if( x3a ) return; - x3a = (struct s_x3*)malloc( sizeof(struct s_x3) ); + x3a = (struct s_x3*)lemon_malloc( sizeof(struct s_x3) ); if( x3a ){ x3a->size = 128; x3a->count = 0; - x3a->tbl = (x3node*)calloc(128, sizeof(x3node) + sizeof(x3node*)); + x3a->tbl = (x3node*)lemon_calloc(128, sizeof(x3node) + sizeof(x3node*)); if( x3a->tbl==0 ){ - free(x3a); + lemon_free(x3a); x3a = 0; }else{ int i; @@ -4980,7 +5857,7 @@ int State_insert(struct state *data, struct config *key) struct s_x3 array; array.size = arrSize = x3a->size*2; array.count = x3a->count; - array.tbl = (x3node*)calloc(arrSize, sizeof(x3node) + sizeof(x3node*)); + array.tbl = (x3node*)lemon_calloc(arrSize, sizeof(x3node)+sizeof(x3node*)); if( array.tbl==0 ) return 0; /* Fail due to malloc failure */ array.ht = (x3node**)&(array.tbl[arrSize]); for(i=0; i<arrSize; i++) array.ht[i] = 0; @@ -4996,7 +5873,7 @@ int State_insert(struct state *data, struct config *key) newnp->from = &(array.ht[h]); array.ht[h] = newnp; } - free(x3a->tbl); + lemon_free(x3a->tbl); *x3a = array; } /* Insert the new data */ @@ -5031,13 +5908,13 @@ struct state *State_find(struct config *key) /* Return an array of pointers to all data in the table. ** The array is obtained from malloc. Return NULL if memory allocation ** problems, or if the array is empty. */ -struct state **State_arrayof() +struct state **State_arrayof(void) { struct state **array; int i,arrSize; if( x3a==0 ) return 0; arrSize = x3a->count; - array = (struct state **)calloc(arrSize, sizeof(struct state *)); + array = (struct state **)lemon_calloc(arrSize, sizeof(struct state *)); if( array ){ for(i=0; i<arrSize; i++) array[i] = x3a->tbl[i].data; } @@ -5048,7 +5925,7 @@ struct state **State_arrayof() PRIVATE unsigned confighash(struct config *a) { unsigned h=0; - h = h*571 + a->rp->index*37 + a->dot; + h = a->rp->index*37 + a->dot; return h; } @@ -5077,15 +5954,15 @@ typedef struct s_x4node { static struct s_x4 *x4a; /* Allocate a new associative array */ -void Configtable_init(){ +void Configtable_init(void){ if( x4a ) return; - x4a = (struct s_x4*)malloc( sizeof(struct s_x4) ); + x4a = (struct s_x4*)lemon_malloc( sizeof(struct s_x4) ); if( x4a ){ x4a->size = 64; x4a->count = 0; - x4a->tbl = (x4node*)calloc(64, sizeof(x4node) + sizeof(x4node*)); + x4a->tbl = (x4node*)lemon_calloc(64, sizeof(x4node) + sizeof(x4node*)); if( x4a->tbl==0 ){ - free(x4a); + lemon_free(x4a); x4a = 0; }else{ int i; @@ -5120,7 +5997,8 @@ int Configtable_insert(struct config *data) struct s_x4 array; array.size = arrSize = x4a->size*2; array.count = x4a->count; - array.tbl = (x4node*)calloc(arrSize, sizeof(x4node) + sizeof(x4node*)); + array.tbl = (x4node*)lemon_calloc(arrSize, + sizeof(x4node) + sizeof(x4node*)); if( array.tbl==0 ) return 0; /* Fail due to malloc failure */ array.ht = (x4node**)&(array.tbl[arrSize]); for(i=0; i<arrSize; i++) array.ht[i] = 0; @@ -5135,7 +6013,6 @@ int Configtable_insert(struct config *data) newnp->from = &(array.ht[h]); array.ht[h] = newnp; } - free(x4a->tbl); *x4a = array; } /* Insert the new data */ diff --git a/tool/lempar.c b/tool/lempar.c index 312507e5f1..74314efeaa 100644 --- a/tool/lempar.c +++ b/tool/lempar.c @@ -13,7 +13,7 @@ ** ** The "lemon" program processes an LALR(1) input grammar file, then uses ** this template to construct a parser. The "lemon" program inserts text -** at each "%%" line. Also, any "P-a-r-s-e" identifer prefix (without the +** at each "%%" line. Also, any "P-a-r-s-e" identifier prefix (without the ** interstitial "-" characters) contained in this template is changed into ** the value of the %name directive from the grammar. Otherwise, the content ** of this template is copied straight through into the generate parser @@ -22,16 +22,13 @@ ** The following is the concatenation of all %include directives from the ** input grammar file: */ -#include <stdio.h> /************ Begin %include sections from the grammar ************************/ %% /**************** End of %include directives **********************************/ -/* These constants specify the various numeric values for terminal symbols -** in a format understandable to "makeheaders". This section is blank unless -** "lemon" is run with the "-m" command-line option. -***************** Begin makeheaders token definitions *************************/ +/* These constants specify the various numeric values for terminal symbols. +***************** Begin token definitions *************************************/ %% -/**************** End makeheaders token definitions ***************************/ +/**************** End token definitions ***************************************/ /* The next sections is a series of control #defines. ** various aspects of the generated parser. @@ -66,19 +63,28 @@ ** zero the stack is dynamically sized using realloc() ** ParseARG_SDECL A static variable declaration for the %extra_argument ** ParseARG_PDECL A parameter declaration for the %extra_argument +** ParseARG_PARAM Code to pass %extra_argument as a subroutine parameter ** ParseARG_STORE Code to store %extra_argument into yypParser ** ParseARG_FETCH Code to extract %extra_argument from yypParser +** ParseCTX_* As ParseARG_ except for %extra_context +** YYREALLOC Name of the realloc() function to use +** YYFREE Name of the free() function to use +** YYDYNSTACK True if stack space should be extended on heap ** YYERRORSYMBOL is the code number of the error symbol. If not ** defined, then do no error processing. ** YYNSTATE the combined number of states. ** YYNRULE the number of rules in the grammar +** YYNTOKEN Number of terminal symbols ** YY_MAX_SHIFT Maximum value for shift actions ** YY_MIN_SHIFTREDUCE Minimum value for shift-reduce actions ** YY_MAX_SHIFTREDUCE Maximum value for shift-reduce actions -** YY_MIN_REDUCE Maximum value for reduce actions ** YY_ERROR_ACTION The yy_action[] code for syntax error ** YY_ACCEPT_ACTION The yy_action[] code for accept ** YY_NO_ACTION The yy_action[] code for no-op +** YY_MIN_REDUCE Minimum value for reduce actions +** YY_MAX_REDUCE Maximum value for reduce actions +** YY_MIN_DSTRCTR Minimum symbol value that has a destructor +** YY_MAX_DSTRCTR Maximum symbol value that has a destructor */ #ifndef INTERFACE # define INTERFACE 1 @@ -86,10 +92,7 @@ /************* Begin control #defines *****************************************/ %% /************* End control #defines *******************************************/ - -/* The yyzerominor constant is used to initialize instances of -** YYMINORTYPE objects to zero. */ -static const YYMINORTYPE yyzerominor = { 0 }; +#define YY_NLOOKAHEAD ((int)(sizeof(yy_lookahead)/sizeof(yy_lookahead[0]))) /* Define the yytestcase() macro to be a no-op if is not already defined ** otherwise. @@ -103,6 +106,22 @@ static const YYMINORTYPE yyzerominor = { 0 }; # define yytestcase(X) #endif +/* Macro to determine if stack space has the ability to grow using +** heap memory. +*/ +#if YYSTACKDEPTH<=0 || YYDYNSTACK +# define YYGROWABLESTACK 1 +#else +# define YYGROWABLESTACK 0 +#endif + +/* Guarantee a minimum number of initial stack slots. +*/ +#if YYSTACKDEPTH<=0 +# undef YYSTACKDEPTH +# define YYSTACKDEPTH 2 /* Need a minimum stack size */ +#endif + /* Next are the tables used to determine what action to take based on the ** current state and lookahead token. These tables are used to implement @@ -118,9 +137,6 @@ static const YYMINORTYPE yyzerominor = { 0 }; ** N between YY_MIN_SHIFTREDUCE Shift to an arbitrary state then ** and YY_MAX_SHIFTREDUCE reduce by rule N-YY_MIN_SHIFTREDUCE. ** -** N between YY_MIN_REDUCE Reduce by rule N-YY_MIN_REDUCE -** and YY_MAX_REDUCE - ** N == YY_ERROR_ACTION A syntax error has occurred. ** ** N == YY_ACCEPT_ACTION The parser accepts its input. @@ -128,21 +144,22 @@ static const YYMINORTYPE yyzerominor = { 0 }; ** N == YY_NO_ACTION No such action. Denotes unused ** slots in the yy_action[] table. ** +** N between YY_MIN_REDUCE Reduce by rule N-YY_MIN_REDUCE +** and YY_MAX_REDUCE +** ** The action table is constructed as a single large table named yy_action[]. -** Given state S and lookahead X, the action is computed as +** Given state S and lookahead X, the action is computed as either: ** -** yy_action[ yy_shift_ofst[S] + X ] +** (A) N = yy_action[ yy_shift_ofst[S] + X ] +** (B) N = yy_default[S] ** -** If the index value yy_shift_ofst[S]+X is out of range or if the value -** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X or if yy_shift_ofst[S] -** is equal to YY_SHIFT_USE_DFLT, it means that the action is not in the table -** and that yy_default[S] should be used instead. +** The (A) formula is preferred. The B formula is used instead if +** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X. ** -** The formula above is for computing the action when the lookahead is +** The formulas above are for computing the action when the lookahead is ** a terminal symbol. If the lookahead is a non-terminal (as occurs after ** a reduce action) then the yy_reduce_ofst[] array is used in place of -** the yy_shift_ofst[] array and YY_REDUCE_USE_DFLT is used in place of -** YY_SHIFT_USE_DFLT. +** the yy_shift_ofst[] array. ** ** The following are the tables generated in this section: ** @@ -207,21 +224,22 @@ typedef struct yyStackEntry yyStackEntry; /* The state of the parser is completely contained in an instance of ** the following structure */ struct yyParser { - int yyidx; /* Index of top element in stack */ + yyStackEntry *yytos; /* Pointer to top element of the stack */ #ifdef YYTRACKMAXSTACKDEPTH - int yyidxMax; /* Maximum value of yyidx */ + int yyhwm; /* High-water mark of the stack */ #endif +#ifndef YYNOERRORRECOVERY int yyerrcnt; /* Shifts left before out of the error */ - ParseARG_SDECL /* A place to hold %extra_argument */ -#if YYSTACKDEPTH<=0 - int yystksz; /* Current side of the stack */ - yyStackEntry *yystack; /* The parser's stack */ -#else - yyStackEntry yystack[YYSTACKDEPTH]; /* The parser's stack */ #endif + ParseARG_SDECL /* A place to hold %extra_argument */ + ParseCTX_SDECL /* A place to hold %extra_context */ + yyStackEntry *yystackEnd; /* Last entry in the stack */ + yyStackEntry *yystack; /* The parser stack */ + yyStackEntry yystk0[YYSTACKDEPTH]; /* Initial stack space */ }; typedef struct yyParser yyParser; +#include <assert.h> #ifndef NDEBUG #include <stdio.h> static FILE *yyTraceFILE = 0; @@ -254,13 +272,13 @@ void ParseTrace(FILE *TraceFILE, char *zTracePrompt){ } #endif /* NDEBUG */ -#ifndef NDEBUG +#if defined(YYCOVERAGE) || !defined(NDEBUG) /* For tracing shifts, the names of all terminals and nonterminals ** are required. The following table supplies these names */ static const char *const yyTokenName[] = { %% }; -#endif /* NDEBUG */ +#endif /* defined(YYCOVERAGE) || !defined(NDEBUG) */ #ifndef NDEBUG /* For tracing reduce actions, the names of all rules are required. @@ -271,27 +289,45 @@ static const char *const yyRuleName[] = { #endif /* NDEBUG */ -#if YYSTACKDEPTH<=0 +#if YYGROWABLESTACK /* -** Try to increase the size of the parser stack. +** Try to increase the size of the parser stack. Return the number +** of errors. Return 0 on success. */ -static void yyGrowStack(yyParser *p){ +static int yyGrowStack(yyParser *p){ + int oldSize = 1 + (int)(p->yystackEnd - p->yystack); int newSize; + int idx; yyStackEntry *pNew; - newSize = p->yystksz*2 + 100; - pNew = realloc(p->yystack, newSize*sizeof(pNew[0])); - if( pNew ){ - p->yystack = pNew; - p->yystksz = newSize; + newSize = oldSize*2 + 100; + idx = (int)(p->yytos - p->yystack); + if( p->yystack==p->yystk0 ){ + pNew = YYREALLOC(0, newSize*sizeof(pNew[0])); + if( pNew==0 ) return 1; + memcpy(pNew, p->yystack, oldSize*sizeof(pNew[0])); + }else{ + pNew = YYREALLOC(p->yystack, newSize*sizeof(pNew[0])); + if( pNew==0 ) return 1; + } + p->yystack = pNew; + p->yytos = &p->yystack[idx]; #ifndef NDEBUG - if( yyTraceFILE ){ - fprintf(yyTraceFILE,"%sStack grows to %d entries!\n", - yyTracePrompt, p->yystksz); - } -#endif + if( yyTraceFILE ){ + fprintf(yyTraceFILE,"%sStack grows from %d to %d entries.\n", + yyTracePrompt, oldSize, newSize); } +#endif + p->yystackEnd = &p->yystack[newSize-1]; + return 0; } +#endif /* YYGROWABLESTACK */ + +#if !YYGROWABLESTACK +/* For builds that do no have a growable stack, yyGrowStack always +** returns an error. +*/ +# define yyGrowStack(X) 1 #endif /* Datatype of the argument to the memory allocated passed as the @@ -303,6 +339,25 @@ static void yyGrowStack(yyParser *p){ # define YYMALLOCARGTYPE size_t #endif +/* Initialize a new parser that has already been allocated. +*/ +void ParseInit(void *yypRawParser ParseCTX_PDECL){ + yyParser *yypParser = (yyParser*)yypRawParser; + ParseCTX_STORE +#ifdef YYTRACKMAXSTACKDEPTH + yypParser->yyhwm = 0; +#endif + yypParser->yystack = yypParser->yystk0; + yypParser->yystackEnd = &yypParser->yystack[YYSTACKDEPTH-1]; +#ifndef YYNOERRORRECOVERY + yypParser->yyerrcnt = -1; +#endif + yypParser->yytos = yypParser->yystack; + yypParser->yystack[0].stateno = 0; + yypParser->yystack[0].major = 0; +} + +#ifndef Parse_ENGINEALWAYSONSTACK /* ** This function allocates a new parser. ** The only argument is a pointer to a function which works like @@ -315,22 +370,17 @@ static void yyGrowStack(yyParser *p){ ** A pointer to a parser. This pointer is used in subsequent calls ** to Parse and ParseFree. */ -void *ParseAlloc(void *(*mallocProc)(YYMALLOCARGTYPE)){ - yyParser *pParser; - pParser = (yyParser*)(*mallocProc)( (YYMALLOCARGTYPE)sizeof(yyParser) ); - if( pParser ){ - pParser->yyidx = -1; -#ifdef YYTRACKMAXSTACKDEPTH - pParser->yyidxMax = 0; -#endif -#if YYSTACKDEPTH<=0 - pParser->yystack = NULL; - pParser->yystksz = 0; - yyGrowStack(pParser); -#endif +void *ParseAlloc(void *(*mallocProc)(YYMALLOCARGTYPE) ParseCTX_PDECL){ + yyParser *yypParser; + yypParser = (yyParser*)(*mallocProc)( (YYMALLOCARGTYPE)sizeof(yyParser) ); + if( yypParser ){ + ParseCTX_STORE + ParseInit(yypParser ParseCTX_PARAM); } - return pParser; + return (void*)yypParser; } +#endif /* Parse_ENGINEALWAYSONSTACK */ + /* The following function deletes the "minor type" or semantic value ** associated with a symbol. The symbol can be either a terminal @@ -344,7 +394,8 @@ static void yy_destructor( YYCODETYPE yymajor, /* Type code for object to destroy */ YYMINORTYPE *yypminor /* The object to be destroyed */ ){ - ParseARG_FETCH; + ParseARG_FETCH + ParseCTX_FETCH switch( yymajor ){ /* Here is inserted the actions which take place when a ** terminal or non-terminal is destroyed. This can happen @@ -371,8 +422,9 @@ static void yy_destructor( */ static void yy_pop_parser_stack(yyParser *pParser){ yyStackEntry *yytos; - assert( pParser->yyidx>=0 ); - yytos = &pParser->yystack[pParser->yyidx--]; + assert( pParser->yytos!=0 ); + assert( pParser->yytos > pParser->yystack ); + yytos = pParser->yytos--; #ifndef NDEBUG if( yyTraceFILE ){ fprintf(yyTraceFILE,"%sPopping %s\n", @@ -383,6 +435,35 @@ static void yy_pop_parser_stack(yyParser *pParser){ yy_destructor(pParser, yytos->major, &yytos->minor); } +/* +** Clear all secondary memory allocations from the parser +*/ +void ParseFinalize(void *p){ + yyParser *pParser = (yyParser*)p; + + /* In-lined version of calling yy_pop_parser_stack() for each + ** element left in the stack */ + yyStackEntry *yytos = pParser->yytos; + while( yytos>pParser->yystack ){ +#ifndef NDEBUG + if( yyTraceFILE ){ + fprintf(yyTraceFILE,"%sPopping %s\n", + yyTracePrompt, + yyTokenName[yytos->major]); + } +#endif + if( yytos->major>=YY_MIN_DSTRCTR ){ + yy_destructor(pParser, yytos->major, &yytos->minor); + } + yytos--; + } + +#if YYGROWABLESTACK + if( pParser->yystack!=pParser->yystk0 ) YYFREE(pParser->yystack); +#endif +} + +#ifndef Parse_ENGINEALWAYSONSTACK /* ** Deallocate and destroy a parser. Destructors are called for ** all stack elements before shutting the parser down. @@ -395,16 +476,13 @@ void ParseFree( void *p, /* The parser to be deleted */ void (*freeProc)(void*) /* Function used to reclaim memory */ ){ - yyParser *pParser = (yyParser*)p; #ifndef YYPARSEFREENEVERNULL - if( pParser==0 ) return; + if( p==0 ) return; #endif - while( pParser->yyidx>=0 ) yy_pop_parser_stack(pParser); -#if YYSTACKDEPTH<=0 - free(pParser->yystack); -#endif - (*freeProc)((void*)pParser); + ParseFinalize(p); + (*freeProc)(p); } +#endif /* Parse_ENGINEALWAYSONSTACK */ /* ** Return the peak depth of the stack for a parser. @@ -412,7 +490,44 @@ void ParseFree( #ifdef YYTRACKMAXSTACKDEPTH int ParseStackPeak(void *p){ yyParser *pParser = (yyParser*)p; - return pParser->yyidxMax; + return pParser->yyhwm; +} +#endif + +/* This array of booleans keeps track of the parser statement +** coverage. The element yycoverage[X][Y] is set when the parser +** is in state X and has a lookahead token Y. In a well-tested +** systems, every element of this matrix should end up being set. +*/ +#if defined(YYCOVERAGE) +static unsigned char yycoverage[YYNSTATE][YYNTOKEN]; +#endif + +/* +** Write into out a description of every state/lookahead combination that +** +** (1) has not been used by the parser, and +** (2) is not a syntax error. +** +** Return the number of missed state/lookahead combinations. +*/ +#if defined(YYCOVERAGE) +int ParseCoverage(FILE *out){ + int stateno, iLookAhead, i; + int nMissed = 0; + for(stateno=0; stateno<YYNSTATE; stateno++){ + i = yy_shift_ofst[stateno]; + for(iLookAhead=0; iLookAhead<YYNTOKEN; iLookAhead++){ + if( yy_lookahead[i+iLookAhead]!=iLookAhead ) continue; + if( yycoverage[stateno][iLookAhead]==0 ) nMissed++; + if( out ){ + fprintf(out,"State %d lookahead %s %s\n", stateno, + yyTokenName[iLookAhead], + yycoverage[stateno][iLookAhead] ? "ok" : "missed"); + } + } + } + return nMissed; } #endif @@ -420,63 +535,62 @@ int ParseStackPeak(void *p){ ** Find the appropriate action for a parser given the terminal ** look-ahead token iLookAhead. */ -static int yy_find_shift_action( - yyParser *pParser, /* The parser */ - YYCODETYPE iLookAhead /* The look-ahead token */ +static YYACTIONTYPE yy_find_shift_action( + YYCODETYPE iLookAhead, /* The look-ahead token */ + YYACTIONTYPE stateno /* Current state number */ ){ int i; - int stateno = pParser->yystack[pParser->yyidx].stateno; - - if( stateno>=YY_MIN_REDUCE ) return stateno; + + if( stateno>YY_MAX_SHIFT ) return stateno; assert( stateno <= YY_SHIFT_COUNT ); +#if defined(YYCOVERAGE) + yycoverage[stateno][iLookAhead] = 1; +#endif do{ i = yy_shift_ofst[stateno]; - if( i==YY_SHIFT_USE_DFLT ) return yy_default[stateno]; + assert( i>=0 ); + assert( i<=YY_ACTTAB_COUNT ); + assert( i+YYNTOKEN<=(int)YY_NLOOKAHEAD ); assert( iLookAhead!=YYNOCODE ); + assert( iLookAhead < YYNTOKEN ); i += iLookAhead; - if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){ - if( iLookAhead>0 ){ + assert( i<(int)YY_NLOOKAHEAD ); + if( yy_lookahead[i]!=iLookAhead ){ #ifdef YYFALLBACK - YYCODETYPE iFallback; /* Fallback token */ - if( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0]) - && (iFallback = yyFallback[iLookAhead])!=0 ){ + YYCODETYPE iFallback; /* Fallback token */ + assert( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0]) ); + iFallback = yyFallback[iLookAhead]; + if( iFallback!=0 ){ #ifndef NDEBUG - if( yyTraceFILE ){ - fprintf(yyTraceFILE, "%sFALLBACK %s => %s\n", - yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]); - } -#endif - assert( yyFallback[iFallback]==0 ); /* Fallback loop must terminate */ - iLookAhead = iFallback; - continue; + if( yyTraceFILE ){ + fprintf(yyTraceFILE, "%sFALLBACK %s => %s\n", + yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]); } #endif -#ifdef YYWILDCARD - { - int j = i - iLookAhead + YYWILDCARD; - if( -#if YY_SHIFT_MIN+YYWILDCARD<0 - j>=0 && -#endif -#if YY_SHIFT_MAX+YYWILDCARD>=YY_ACTTAB_COUNT - j<YY_ACTTAB_COUNT && + assert( yyFallback[iFallback]==0 ); /* Fallback loop must terminate */ + iLookAhead = iFallback; + continue; + } #endif - yy_lookahead[j]==YYWILDCARD - ){ +#ifdef YYWILDCARD + { + int j = i - iLookAhead + YYWILDCARD; + assert( j<(int)(sizeof(yy_lookahead)/sizeof(yy_lookahead[0])) ); + if( yy_lookahead[j]==YYWILDCARD && iLookAhead>0 ){ #ifndef NDEBUG - if( yyTraceFILE ){ - fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n", - yyTracePrompt, yyTokenName[iLookAhead], - yyTokenName[YYWILDCARD]); - } -#endif /* NDEBUG */ - return yy_action[j]; + if( yyTraceFILE ){ + fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n", + yyTracePrompt, yyTokenName[iLookAhead], + yyTokenName[YYWILDCARD]); } +#endif /* NDEBUG */ + return yy_action[j]; } -#endif /* YYWILDCARD */ } +#endif /* YYWILDCARD */ return yy_default[stateno]; }else{ + assert( i>=0 && i<(int)(sizeof(yy_action)/sizeof(yy_action[0])) ); return yy_action[i]; } }while(1); @@ -486,8 +600,8 @@ static int yy_find_shift_action( ** Find the appropriate action for a parser given the non-terminal ** look-ahead token iLookAhead. */ -static int yy_find_reduce_action( - int stateno, /* Current state number */ +static YYACTIONTYPE yy_find_reduce_action( + YYACTIONTYPE stateno, /* Current state number */ YYCODETYPE iLookAhead /* The look-ahead token */ ){ int i; @@ -499,7 +613,6 @@ static int yy_find_reduce_action( assert( stateno<=YY_REDUCE_COUNT ); #endif i = yy_reduce_ofst[stateno]; - assert( i!=YY_REDUCE_USE_DFLT ); assert( iLookAhead!=YYNOCODE ); i += iLookAhead; #ifdef YYERRORSYMBOL @@ -516,41 +629,43 @@ static int yy_find_reduce_action( /* ** The following routine is called if the stack overflows. */ -static void yyStackOverflow(yyParser *yypParser, YYMINORTYPE *yypMinor){ - ParseARG_FETCH; - yypParser->yyidx--; +static void yyStackOverflow(yyParser *yypParser){ + ParseARG_FETCH + ParseCTX_FETCH #ifndef NDEBUG if( yyTraceFILE ){ fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt); } #endif - while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser); + while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser); /* Here code is inserted which will execute if the parser ** stack every overflows */ /******** Begin %stack_overflow code ******************************************/ %% /******** End %stack_overflow code ********************************************/ - ParseARG_STORE; /* Suppress warning about unused %extra_argument var */ + ParseARG_STORE /* Suppress warning about unused %extra_argument var */ + ParseCTX_STORE } /* ** Print tracing information for a SHIFT action */ #ifndef NDEBUG -static void yyTraceShift(yyParser *yypParser, int yyNewState){ +static void yyTraceShift(yyParser *yypParser, int yyNewState, const char *zTag){ if( yyTraceFILE ){ if( yyNewState<YYNSTATE ){ - fprintf(yyTraceFILE,"%sShift '%s', go to state %d\n", - yyTracePrompt,yyTokenName[yypParser->yystack[yypParser->yyidx].major], + fprintf(yyTraceFILE,"%s%s '%s', go to state %d\n", + yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major], yyNewState); }else{ - fprintf(yyTraceFILE,"%sShift '%s'\n", - yyTracePrompt,yyTokenName[yypParser->yystack[yypParser->yyidx].major]); + fprintf(yyTraceFILE,"%s%s '%s', pending reduce %d\n", + yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major], + yyNewState - YY_MIN_REDUCE); } } } #else -# define yyTraceShift(X,Y) +# define yyTraceShift(X,Y,Z) #endif /* @@ -558,45 +673,46 @@ static void yyTraceShift(yyParser *yypParser, int yyNewState){ */ static void yy_shift( yyParser *yypParser, /* The parser to be shifted */ - int yyNewState, /* The new state to shift in */ - int yyMajor, /* The major token to shift in */ - YYMINORTYPE *yypMinor /* Pointer to the minor token to shift in */ + YYACTIONTYPE yyNewState, /* The new state to shift in */ + YYCODETYPE yyMajor, /* The major token to shift in */ + ParseTOKENTYPE yyMinor /* The minor token to shift in */ ){ yyStackEntry *yytos; - yypParser->yyidx++; + yypParser->yytos++; #ifdef YYTRACKMAXSTACKDEPTH - if( yypParser->yyidx>yypParser->yyidxMax ){ - yypParser->yyidxMax = yypParser->yyidx; + if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){ + yypParser->yyhwm++; + assert( yypParser->yyhwm == (int)(yypParser->yytos - yypParser->yystack) ); } #endif -#if YYSTACKDEPTH>0 - if( yypParser->yyidx>=YYSTACKDEPTH ){ - yyStackOverflow(yypParser, yypMinor); - return; - } -#else - if( yypParser->yyidx>=yypParser->yystksz ){ - yyGrowStack(yypParser); - if( yypParser->yyidx>=yypParser->yystksz ){ - yyStackOverflow(yypParser, yypMinor); + yytos = yypParser->yytos; + if( yytos>yypParser->yystackEnd ){ + if( yyGrowStack(yypParser) ){ + yypParser->yytos--; + yyStackOverflow(yypParser); return; } + yytos = yypParser->yytos; + assert( yytos <= yypParser->yystackEnd ); } -#endif - yytos = &yypParser->yystack[yypParser->yyidx]; - yytos->stateno = (YYACTIONTYPE)yyNewState; - yytos->major = (YYCODETYPE)yyMajor; - yytos->minor = *yypMinor; - yyTraceShift(yypParser, yyNewState); + if( yyNewState > YY_MAX_SHIFT ){ + yyNewState += YY_MIN_REDUCE - YY_MIN_SHIFTREDUCE; + } + yytos->stateno = yyNewState; + yytos->major = yyMajor; + yytos->minor.yy0 = yyMinor; + yyTraceShift(yypParser, yyNewState, "Shift"); } -/* The following table contains information about every rule that -** is used during the reduce. -*/ -static const struct { - YYCODETYPE lhs; /* Symbol on the left-hand side of the rule */ - unsigned char nrhs; /* Number of right-hand side symbols in the rule */ -} yyRuleInfo[] = { +/* For rule J, yyRuleInfoLhs[J] contains the symbol on the left-hand side +** of that rule */ +static const YYCODETYPE yyRuleInfoLhs[] = { +%% +}; + +/* For rule J, yyRuleInfoNRhs[J] contains the negative of the number +** of symbols on the right-hand side of that rule. */ +static const signed char yyRuleInfoNRhs[] = { %% }; @@ -605,27 +721,28 @@ static void yy_accept(yyParser*); /* Forward Declaration */ /* ** Perform a reduce action and the shift that must immediately ** follow the reduce. +** +** The yyLookahead and yyLookaheadToken parameters provide reduce actions +** access to the lookahead token (if any). The yyLookahead will be YYNOCODE +** if the lookahead token has already been consumed. As this procedure is +** only called from one place, optimizing compilers will in-line it, which +** means that the extra parameters have no performance impact. */ -static void yy_reduce( +static YYACTIONTYPE yy_reduce( yyParser *yypParser, /* The parser */ - int yyruleno /* Number of the rule by which to reduce */ + unsigned int yyruleno, /* Number of the rule by which to reduce */ + int yyLookahead, /* Lookahead token, or YYNOCODE if none */ + ParseTOKENTYPE yyLookaheadToken /* Value of the lookahead token */ + ParseCTX_PDECL /* %extra_context */ ){ int yygoto; /* The next state */ - int yyact; /* The next action */ - YYMINORTYPE yygotominor; /* The LHS of the rule reduced */ + YYACTIONTYPE yyact; /* The next action */ yyStackEntry *yymsp; /* The top of the parser's stack */ int yysize; /* Amount to pop the stack */ - ParseARG_FETCH; - yymsp = &yypParser->yystack[yypParser->yyidx]; -#ifndef NDEBUG - if( yyTraceFILE && yyruleno>=0 - && yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) ){ - yysize = yyRuleInfo[yyruleno].nrhs; - fprintf(yyTraceFILE, "%sReduce [%s], go to state %d.\n", yyTracePrompt, - yyRuleName[yyruleno], yymsp[-yysize].stateno); - } -#endif /* NDEBUG */ - yygotominor = yyzerominor; + ParseARG_FETCH + (void)yyLookahead; + (void)yyLookaheadToken; + yymsp = yypParser->yytos; switch( yyruleno ){ /* Beginning here are the reduction cases. A typical example @@ -640,31 +757,24 @@ static void yy_reduce( %% /********** End reduce actions ************************************************/ }; - assert( yyruleno>=0 && yyruleno<sizeof(yyRuleInfo)/sizeof(yyRuleInfo[0]) ); - yygoto = yyRuleInfo[yyruleno].lhs; - yysize = yyRuleInfo[yyruleno].nrhs; - yypParser->yyidx -= yysize; - yyact = yy_find_reduce_action(yymsp[-yysize].stateno,(YYCODETYPE)yygoto); - if( yyact <= YY_MAX_SHIFTREDUCE ){ - if( yyact>YY_MAX_SHIFT ) yyact += YY_MIN_REDUCE - YY_MIN_SHIFTREDUCE; - /* If the reduce action popped at least - ** one element off the stack, then we can push the new element back - ** onto the stack here, and skip the stack overflow test in yy_shift(). - ** That gives a significant speed improvement. */ - if( yysize ){ - yypParser->yyidx++; - yymsp -= yysize-1; - yymsp->stateno = (YYACTIONTYPE)yyact; - yymsp->major = (YYCODETYPE)yygoto; - yymsp->minor = yygotominor; - yyTraceShift(yypParser, yyact); - }else{ - yy_shift(yypParser,yyact,yygoto,&yygotominor); - } - }else{ - assert( yyact == YY_ACCEPT_ACTION ); - yy_accept(yypParser); - } + assert( yyruleno<sizeof(yyRuleInfoLhs)/sizeof(yyRuleInfoLhs[0]) ); + yygoto = yyRuleInfoLhs[yyruleno]; + yysize = yyRuleInfoNRhs[yyruleno]; + yyact = yy_find_reduce_action(yymsp[yysize].stateno,(YYCODETYPE)yygoto); + + /* There are no SHIFTREDUCE actions on nonterminals because the table + ** generator has simplified them to pure REDUCE actions. */ + assert( !(yyact>YY_MAX_SHIFT && yyact<=YY_MAX_SHIFTREDUCE) ); + + /* It is not possible for a REDUCE to be followed by an error */ + assert( yyact!=YY_ERROR_ACTION ); + + yymsp += yysize+1; + yypParser->yytos = yymsp; + yymsp->stateno = (YYACTIONTYPE)yyact; + yymsp->major = (YYCODETYPE)yygoto; + yyTraceShift(yypParser, yyact, "... then shift"); + return yyact; } /* @@ -674,19 +784,21 @@ static void yy_reduce( static void yy_parse_failed( yyParser *yypParser /* The parser */ ){ - ParseARG_FETCH; + ParseARG_FETCH + ParseCTX_FETCH #ifndef NDEBUG if( yyTraceFILE ){ fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt); } #endif - while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser); + while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser); /* Here code is inserted which will be executed whenever the ** parser fails */ /************ Begin %parse_failure code ***************************************/ %% /************ End %parse_failure code *****************************************/ - ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */ + ParseARG_STORE /* Suppress warning about unused %extra_argument variable */ + ParseCTX_STORE } #endif /* YYNOERRORRECOVERY */ @@ -696,14 +808,16 @@ static void yy_parse_failed( static void yy_syntax_error( yyParser *yypParser, /* The parser */ int yymajor, /* The major type of the error token */ - YYMINORTYPE yyminor /* The minor type of the error token */ + ParseTOKENTYPE yyminor /* The minor type of the error token */ ){ - ParseARG_FETCH; -#define TOKEN (yyminor.yy0) + ParseARG_FETCH + ParseCTX_FETCH +#define TOKEN yyminor /************ Begin %syntax_error code ****************************************/ %% /************ End %syntax_error code ******************************************/ - ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */ + ParseARG_STORE /* Suppress warning about unused %extra_argument variable */ + ParseCTX_STORE } /* @@ -712,19 +826,24 @@ static void yy_syntax_error( static void yy_accept( yyParser *yypParser /* The parser */ ){ - ParseARG_FETCH; + ParseARG_FETCH + ParseCTX_FETCH #ifndef NDEBUG if( yyTraceFILE ){ fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt); } #endif - while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser); +#ifndef YYNOERRORRECOVERY + yypParser->yyerrcnt = -1; +#endif + assert( yypParser->yytos==yypParser->yystack ); /* Here code is inserted which will be executed whenever the ** parser accepts */ /*********** Begin %parse_accept code *****************************************/ %% /*********** End %parse_accept code *******************************************/ - ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */ + ParseARG_STORE /* Suppress warning about unused %extra_argument variable */ + ParseCTX_STORE } /* The main parser program. @@ -753,60 +872,91 @@ void Parse( ParseARG_PDECL /* Optional %extra_argument parameter */ ){ YYMINORTYPE yyminorunion; - int yyact; /* The parser action. */ + YYACTIONTYPE yyact; /* The parser action. */ #if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY) int yyendofinput; /* True if we are at the end of input */ #endif #ifdef YYERRORSYMBOL int yyerrorhit = 0; /* True if yymajor has invoked an error */ #endif - yyParser *yypParser; /* The parser */ + yyParser *yypParser = (yyParser*)yyp; /* The parser */ + ParseCTX_FETCH + ParseARG_STORE - /* (re)initialize the parser, if necessary */ - yypParser = (yyParser*)yyp; - if( yypParser->yyidx<0 ){ -#if YYSTACKDEPTH<=0 - if( yypParser->yystksz <=0 ){ - /*memset(&yyminorunion, 0, sizeof(yyminorunion));*/ - yyminorunion = yyzerominor; - yyStackOverflow(yypParser, &yyminorunion); - return; - } -#endif - yypParser->yyidx = 0; - yypParser->yyerrcnt = -1; - yypParser->yystack[0].stateno = 0; - yypParser->yystack[0].major = 0; -#ifndef NDEBUG - if( yyTraceFILE ){ - fprintf(yyTraceFILE,"%sInitialize. Empty stack. State 0\n", - yyTracePrompt); - } -#endif - } - yyminorunion.yy0 = yyminor; + assert( yypParser->yytos!=0 ); #if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY) yyendofinput = (yymajor==0); #endif - ParseARG_STORE; + yyact = yypParser->yytos->stateno; #ifndef NDEBUG if( yyTraceFILE ){ - fprintf(yyTraceFILE,"%sInput '%s'\n",yyTracePrompt,yyTokenName[yymajor]); + if( yyact < YY_MIN_REDUCE ){ + fprintf(yyTraceFILE,"%sInput '%s' in state %d\n", + yyTracePrompt,yyTokenName[yymajor],yyact); + }else{ + fprintf(yyTraceFILE,"%sInput '%s' with pending reduce %d\n", + yyTracePrompt,yyTokenName[yymajor],yyact-YY_MIN_REDUCE); + } } #endif - do{ - yyact = yy_find_shift_action(yypParser,(YYCODETYPE)yymajor); - if( yyact <= YY_MAX_SHIFTREDUCE ){ - if( yyact > YY_MAX_SHIFT ) yyact += YY_MIN_REDUCE - YY_MIN_SHIFTREDUCE; - yy_shift(yypParser,yyact,yymajor,&yyminorunion); + while(1){ /* Exit by "break" */ + assert( yypParser->yytos>=yypParser->yystack ); + assert( yyact==yypParser->yytos->stateno ); + yyact = yy_find_shift_action((YYCODETYPE)yymajor,yyact); + if( yyact >= YY_MIN_REDUCE ){ + unsigned int yyruleno = yyact - YY_MIN_REDUCE; /* Reduce by this rule */ +#ifndef NDEBUG + assert( yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) ); + if( yyTraceFILE ){ + int yysize = yyRuleInfoNRhs[yyruleno]; + if( yysize ){ + fprintf(yyTraceFILE, "%sReduce %d [%s]%s, pop back to state %d.\n", + yyTracePrompt, + yyruleno, yyRuleName[yyruleno], + yyruleno<YYNRULE_WITH_ACTION ? "" : " without external action", + yypParser->yytos[yysize].stateno); + }else{ + fprintf(yyTraceFILE, "%sReduce %d [%s]%s.\n", + yyTracePrompt, yyruleno, yyRuleName[yyruleno], + yyruleno<YYNRULE_WITH_ACTION ? "" : " without external action"); + } + } +#endif /* NDEBUG */ + + /* Check that the stack is large enough to grow by a single entry + ** if the RHS of the rule is empty. This ensures that there is room + ** enough on the stack to push the LHS value */ + if( yyRuleInfoNRhs[yyruleno]==0 ){ +#ifdef YYTRACKMAXSTACKDEPTH + if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){ + yypParser->yyhwm++; + assert( yypParser->yyhwm == + (int)(yypParser->yytos - yypParser->yystack)); + } +#endif + if( yypParser->yytos>=yypParser->yystackEnd ){ + if( yyGrowStack(yypParser) ){ + yyStackOverflow(yypParser); + break; + } + } + } + yyact = yy_reduce(yypParser,yyruleno,yymajor,yyminor ParseCTX_PARAM); + }else if( yyact <= YY_MAX_SHIFTREDUCE ){ + yy_shift(yypParser,yyact,(YYCODETYPE)yymajor,yyminor); +#ifndef YYNOERRORRECOVERY yypParser->yyerrcnt--; - yymajor = YYNOCODE; - }else if( yyact <= YY_MAX_REDUCE ){ - yy_reduce(yypParser,yyact-YY_MIN_REDUCE); +#endif + break; + }else if( yyact==YY_ACCEPT_ACTION ){ + yypParser->yytos--; + yy_accept(yypParser); + return; }else{ assert( yyact == YY_ERROR_ACTION ); + yyminorunion.yy0 = yyminor; #ifdef YYERRORSYMBOL int yymx; #endif @@ -836,9 +986,9 @@ void Parse( ** */ if( yypParser->yyerrcnt<0 ){ - yy_syntax_error(yypParser,yymajor,yyminorunion); + yy_syntax_error(yypParser,yymajor,yyminor); } - yymx = yypParser->yystack[yypParser->yyidx].major; + yymx = yypParser->yytos->major; if( yymx==YYERRORSYMBOL || yyerrorhit ){ #ifndef NDEBUG if( yyTraceFILE ){ @@ -846,30 +996,30 @@ void Parse( yyTracePrompt,yyTokenName[yymajor]); } #endif - yy_destructor(yypParser, (YYCODETYPE)yymajor,&yyminorunion); + yy_destructor(yypParser, (YYCODETYPE)yymajor, &yyminorunion); yymajor = YYNOCODE; }else{ - while( - yypParser->yyidx >= 0 && - yymx != YYERRORSYMBOL && - (yyact = yy_find_reduce_action( - yypParser->yystack[yypParser->yyidx].stateno, - YYERRORSYMBOL)) >= YY_MIN_REDUCE - ){ + while( yypParser->yytos > yypParser->yystack ){ + yyact = yy_find_reduce_action(yypParser->yytos->stateno, + YYERRORSYMBOL); + if( yyact<=YY_MAX_SHIFTREDUCE ) break; yy_pop_parser_stack(yypParser); } - if( yypParser->yyidx < 0 || yymajor==0 ){ + if( yypParser->yytos <= yypParser->yystack || yymajor==0 ){ yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion); yy_parse_failed(yypParser); +#ifndef YYNOERRORRECOVERY + yypParser->yyerrcnt = -1; +#endif yymajor = YYNOCODE; }else if( yymx!=YYERRORSYMBOL ){ - YYMINORTYPE u2; - u2.YYERRSYMDT = 0; - yy_shift(yypParser,yyact,YYERRORSYMBOL,&u2); + yy_shift(yypParser,yyact,YYERRORSYMBOL,yyminor); } } yypParser->yyerrcnt = 3; yyerrorhit = 1; + if( yymajor==YYNOCODE ) break; + yyact = yypParser->yytos->stateno; #elif defined(YYNOERRORRECOVERY) /* If the YYNOERRORRECOVERY macro is defined, then do not attempt to ** do any kind of error recovery. Instead, simply invoke the syntax @@ -878,10 +1028,9 @@ void Parse( ** Applications can set this macro (for example inside %include) if ** they intend to abandon the parse upon the first syntax error seen. */ - yy_syntax_error(yypParser,yymajor,yyminorunion); + yy_syntax_error(yypParser,yymajor, yyminor); yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion); - yymajor = YYNOCODE; - + break; #else /* YYERRORSYMBOL is not defined */ /* This is what we do if the grammar does not define ERROR: ** @@ -893,26 +1042,45 @@ void Parse( ** three input tokens have been successfully shifted. */ if( yypParser->yyerrcnt<=0 ){ - yy_syntax_error(yypParser,yymajor,yyminorunion); + yy_syntax_error(yypParser,yymajor, yyminor); } yypParser->yyerrcnt = 3; yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion); if( yyendofinput ){ yy_parse_failed(yypParser); +#ifndef YYNOERRORRECOVERY + yypParser->yyerrcnt = -1; +#endif } - yymajor = YYNOCODE; + break; #endif } - }while( yymajor!=YYNOCODE && yypParser->yyidx>=0 ); + } #ifndef NDEBUG if( yyTraceFILE ){ - int i; + yyStackEntry *i; + char cDiv = '['; fprintf(yyTraceFILE,"%sReturn. Stack=",yyTracePrompt); - for(i=1; i<=yypParser->yyidx; i++) - fprintf(yyTraceFILE,"%c%s", i==1 ? '[' : ' ', - yyTokenName[yypParser->yystack[i].major]); + for(i=&yypParser->yystack[1]; i<=yypParser->yytos; i++){ + fprintf(yyTraceFILE,"%c%s", cDiv, yyTokenName[i->major]); + cDiv = ' '; + } fprintf(yyTraceFILE,"]\n"); } #endif return; } + +/* +** Return the fallback token corresponding to canonical token iToken, or +** 0 if iToken has no fallback. +*/ +int ParseFallback(int iToken){ +#ifdef YYFALLBACK + assert( iToken<(int)(sizeof(yyFallback)/sizeof(yyFallback[0])) ); + return yyFallback[iToken]; +#else + (void)iToken; + return 0; +#endif +} diff --git a/tool/libvers.c b/tool/libvers.c new file mode 100644 index 0000000000..6911dbd07e --- /dev/null +++ b/tool/libvers.c @@ -0,0 +1,15 @@ +/* +** Compile this program against an SQLite library of unknown version +** and then run this program, and it will print out the SQLite version +** information. +*/ +#include <stdio.h> + +extern const char *sqlite3_libversion(void); +extern const char *sqlite3_sourceid(void); + +int main(int argc, char **argv){ + printf("SQLite version %s\n", sqlite3_libversion()); + printf("SQLite source %s\n", sqlite3_sourceid()); + return 0; +} diff --git a/tool/loadfts.c b/tool/loadfts.c index 0000797b88..5ed03b7bdc 100644 --- a/tool/loadfts.c +++ b/tool/loadfts.c @@ -145,6 +145,7 @@ static void traverse( for(e=readdir(d); e; e=readdir(d)){ if( strcmp(e->d_name, ".")==0 || strcmp(e->d_name, "..")==0 ) continue; char *zPath = sqlite3_mprintf("%s/%s", zDir, e->d_name); + if( zPath==0 ){ fprintf(stderr, "out of memory\n"); abort(); } if (e->d_type & DT_DIR) { traverse(zPath, pCtx, xCallback); }else{ diff --git a/tool/logest.c b/tool/logest.c index 347fa68a4f..d916b43d39 100644 --- a/tool/logest.c +++ b/tool/logest.c @@ -75,6 +75,7 @@ static sqlite3_uint64 logEstToInt(LogEst x){ x /= 10; if( n>=5 ) n -= 2; else if( n>=1 ) n -= 1; + if( x>60 ) return (((sqlite3_uint64)0xffffffff)<<32)+(sqlite3_uint64)0xffffffff; if( x>=3 ) return (n+8)<<(x-3); return (n+8)>>(3-x); } @@ -147,9 +148,9 @@ int main(int argc, char **argv){ }else if( strcmp(z,"inv")==0 ){ if( n>0 ) a[n-1] = -a[n-1]; }else if( z[0]=='^' ){ - a[n++] = atoi(z+1); + a[n++] = (LogEst)atoi(z+1); }else if( isInteger(z) ){ - a[n++] = logEstFromInteger(atoi(z)); + a[n++] = logEstFromInteger(atoll(z)); }else if( isFloat(z) && z[0]!='-' ){ a[n++] = logEstFromDouble(atof(z)); }else{ @@ -161,6 +162,8 @@ int main(int argc, char **argv){ printf("%5d (%f)\n", a[i], 1.0/(double)logEstToInt(-a[i])); }else if( a[i]<10 ){ printf("%5d (%f)\n", a[i], logEstToInt(a[i]+100)/1024.0); + }else if( a[i]>100 ){ + printf("%5d (%lld)\n", a[i], logEstToInt(a[i])); }else{ sqlite3_uint64 x = logEstToInt(a[i]+100)*100/1024; printf("%5d (%lld.%02lld)\n", a[i], x/100, x%100); diff --git a/tool/max-limits.c b/tool/max-limits.c new file mode 100644 index 0000000000..d019974426 --- /dev/null +++ b/tool/max-limits.c @@ -0,0 +1,41 @@ +/* +** Link this program against an SQLite library of unknown provenance in order +** to display the compile-time maximum values for various settings. +*/ +#include "sqlite3.h" +#include <stdio.h> + +static const struct { + int eCode; + char *zName; +} aLimit[] = { + { SQLITE_LIMIT_LENGTH, "SQLITE_MAX_LENGTH" }, + { SQLITE_LIMIT_SQL_LENGTH, "SQLITE_MAX_SQL_LENGTH" }, + { SQLITE_LIMIT_COLUMN, "SQLITE_MAX_COLUMN" }, + { SQLITE_LIMIT_EXPR_DEPTH, "SQLITE_MAX_EXPR_DEPTH" }, + { SQLITE_LIMIT_COMPOUND_SELECT, "SQLITE_MAX_COMPOUND_SELECT" }, + { SQLITE_LIMIT_VDBE_OP, "SQLITE_MAX_VDBE_OP" }, + { SQLITE_LIMIT_FUNCTION_ARG, "SQLITE_MAX_FUNCTION_ARG" }, + { SQLITE_LIMIT_ATTACHED, "SQLITE_MAX_ATTACHED" }, + { SQLITE_LIMIT_LIKE_PATTERN_LENGTH, "SQLITE_MAX_LIKE_PATTERN_LENGTH" }, + { SQLITE_LIMIT_VARIABLE_NUMBER, "SQLITE_MAX_VARIABLE_NUMBER" }, + { SQLITE_LIMIT_TRIGGER_DEPTH, "SQLITE_MAX_TRIGGER_DEPTH" }, + { SQLITE_LIMIT_WORKER_THREADS, "SQLITE_MAX_WORKER_THREADS" }, +}; + +static int maxLimit(sqlite3 *db, int eCode){ + int iOrig = sqlite3_limit(db, eCode, 0x7fffffff); + return sqlite3_limit(db, eCode, iOrig); +} + +int main(int argc, char **argv){ + sqlite3 *db; + int j, rc; + rc = sqlite3_open(":memory:", &db); + if( rc==SQLITE_OK ){ + for(j=0; j<sizeof(aLimit)/sizeof(aLimit[0]); j++){ + printf("%-35s %10d\n", aLimit[j].zName, maxLimit(db, aLimit[j].eCode)); + } + sqlite3_close(db); + } +} diff --git a/tool/merge-test.tcl b/tool/merge-test.tcl new file mode 100644 index 0000000000..2010d67657 --- /dev/null +++ b/tool/merge-test.tcl @@ -0,0 +1,99 @@ +#!/usr/bin/tcl +# +# Run this script to test to see that the latest trunk changes can be +# merged into LTS branches without breaking anything. +# +# To Use: +# +# * Copy this script into a directory above the sqlite checkout +# * Run "fossil update trunk" and "fossil revert" +# * Run "tclsh ../merge-test.tcl" (in other words run this script) +# +# Operation: +# +# This script changes to each LTS branch to be tested, merges the latest +# trunk changes into the branch (without committing them) and then +# runs "make test". Any errors are stored in local files. +# +# Limitations: +# +# Some LTS branches are not synced directly from trunk but rather from +# other LTS branches. These other branches cannot be tested because +# there is no good way to generate the intermediate merges. +# +############################################################################### + +# Run a shell command contained in arguments. Put the return code in +# global variable ::res and the output string in global variable ::result +# +proc safeexec {args} { + global res result + set res [catch "exec $args" result] +} + +# Run the shell command contained in arguments. Print an error and exit +# if anything goes wrong. +# +proc mustbeok {args} { + global res result + set res [catch "exec $args" result] + if {$res} { + puts "FAILED: $args" + puts $result + exit 1 + } +} + +# Write $content into a file named $filename. The file is overwritten if it +# already exist. The file is create if it does not already exist. +# +proc writefile {filename content} { + set fd [open $filename wb] + puts $fd $content + close $fd +} + +# Run the merge-test +# +foreach {branch configopts} { + begin-concurrent {--enable-json1} + begin-concurrent-pnu {--enable-json1} + wal2 {--enable-all} + reuse-schema {--enable-all} +} { + puts $branch + set errorfile ${branch}-error.txt + mustbeok fossil revert + mustbeok fossil up $branch + safeexec fossil merge trunk + if {$res} { + puts " merge failed - see $errorfile" + writefile $errorfile $result + } else { + puts " merge ok" + safeexec ./configure --enable-debug {*}$configopts + if {$res} { + puts " configure failed - see $errorfile" + writefile $errorfile $result + } else { + puts " configure ok" + safeexec make fuzzcheck sqlite3 testfixture + if {$res} { + puts " build failed - see $errorfile" + writefile $errorfile $result + } else { + puts " build ok" + safeexec make test + if {$res} { + puts " test failed - see $errorfile" + writefile $errorfile $result + } else { + puts " test ok" + } + } + } + } +} +mustbeok fossil revert +mustbeok fossil up trunk +puts "reset back to trunk" diff --git a/tool/mkamalzip.tcl b/tool/mkamalzip.tcl new file mode 100644 index 0000000000..92feb4122e --- /dev/null +++ b/tool/mkamalzip.tcl @@ -0,0 +1,23 @@ +#!/usr/bin/tclsh +# +# Build a ZIP archive for the amalgamation source code found in the current +# directory. +# +set VERSION-file [file dirname [file dirname [file normalize $argv0]]]/VERSION +set fd [open ${VERSION-file} rb] +set vers [read $fd] +close $fd +scan $vers %d.%d.%d major minor patch +set numvers [format {3%02d%02d00} $minor $patch] +set dir sqlite-amalgamation-$numvers +file delete -force $dir +file mkdir $dir +set filelist {sqlite3.c sqlite3.h shell.c sqlite3ext.h} +foreach f $filelist { + file copy $f $dir/$f +} +set cmd "zip -r $dir.zip $dir" +puts $cmd +file delete -force $dir.zip +exec {*}$cmd +file delete -force $dir diff --git a/tool/mkautoconfamal.sh b/tool/mkautoconfamal.sh index 75a0b09a4c..3835799a6f 100644 --- a/tool/mkautoconfamal.sh +++ b/tool/mkautoconfamal.sh @@ -2,8 +2,8 @@ # This script is used to build the amalgamation autoconf package. # It assumes the following: # -# 1. The files "sqlite3.c", "sqlite3.h" and "sqlite3ext.h" -# are available in the current directory. +# 1. The files "sqlite3.c", "sqlite3.h", "sqlite3ext.h", "shell.c", +# and "sqlite3rc.h" are available in the current directory. # # 2. Variable $TOP is set to the full path of the root directory # of the SQLite source tree. @@ -13,7 +13,7 @@ # -# Bail out of the script if any command returns a non-zero exit +# Bail out of the script if any command returns a non-zero exit # status. Or if the script tries to use an unset variable. These # may fail for old /bin/sh interpreters. # @@ -22,16 +22,16 @@ set -u TMPSPACE=./mkpkg_tmp_dir VERSION=`cat $TOP/VERSION` -HASH=`sed 's/^\(..........\).*/\1/' $TOP/manifest.uuid` -DATETIME=`grep '^D' $TOP/manifest | sed -e 's/[^0-9]//g' -e 's/\(............\).*/\1/'` +HASH=`cut -c1-10 $TOP/manifest.uuid` +DATETIME=`grep '^D' $TOP/manifest | tr -c -d '[0-9]' | cut -c1-12` # If this script is given an argument of --snapshot, then generate a -# snapshot tarball named for the current checkout SHA1 hash, rather than +# snapshot tarball named for the current checkout SHA hash, rather than # the version number. # if test "$#" -ge 1 -a x$1 != x--snapshot then - # Set global variable $ARTIFACT to the "3xxyyzz" string incorporated + # Set global variable $ARTIFACT to the "3xxyyzz" string incorporated # into artifact filenames. And $VERSION2 to the "3.x.y[.z]" form. xx=`echo $VERSION|sed 's/3\.\([0-9]*\)\..*/\1/'` yy=`echo $VERSION|sed 's/3\.[^.]*\.\([0-9]*\).*/\1/'` @@ -46,46 +46,50 @@ fi rm -rf $TMPSPACE cp -R $TOP/autoconf $TMPSPACE +cp -R $TOP/autosetup $TMPSPACE +cp -p $TOP/configure $TMPSPACE cp sqlite3.c $TMPSPACE cp sqlite3.h $TMPSPACE cp sqlite3ext.h $TMPSPACE +cp sqlite3rc.h $TMPSPACE cp $TOP/sqlite3.1 $TMPSPACE cp $TOP/sqlite3.pc.in $TMPSPACE -cp $TOP/src/shell.c $TMPSPACE +cp shell.c $TMPSPACE cp $TOP/src/sqlite3.rc $TMPSPACE - -cat $TMPSPACE/configure.ac | -sed "s/--SQLITE-VERSION--/$VERSION/" > $TMPSPACE/tmp -mv $TMPSPACE/tmp $TMPSPACE/configure.ac +cp $TOP/tool/Replace.cs $TMPSPACE +cp $TOP/VERSION $TMPSPACE +cp $TOP/main.mk $TMPSPACE cd $TMPSPACE -autoreconf -i -#libtoolize -#aclocal -#autoconf -#automake --add-missing + +#if true; then + # Clean up *~ files (emacs-generated backups). + # This bit is only for use during development of + # the autoconf bundle. +# find . -name '*~' -exec rm \{} \; +#fi mkdir -p tea/generic -echo "#ifdef USE_SYSTEM_SQLITE" > tea/generic/tclsqlite3.c -echo "# include <sqlite3.h>" >> tea/generic/tclsqlite3.c -echo "#else" >> tea/generic/tclsqlite3.c -echo "#include \"sqlite3.c\"" >> tea/generic/tclsqlite3.c -echo "#endif" >> tea/generic/tclsqlite3.c +cat <<EOF > tea/generic/tclsqlite3.c +#ifdef USE_SYSTEM_SQLITE +# include <sqlite3.h> +#else +# include "sqlite3.c" +#endif +EOF cat $TOP/src/tclsqlite.c >> tea/generic/tclsqlite3.c -cat tea/configure.ac | - sed "s/AC_INIT(\[sqlite\], .*)/AC_INIT([sqlite], [$VERSION])/" > tmp -mv tmp tea/configure.ac - -cd tea -autoconf -rm -rf autom4te.cache +# Clean up some local remnants from the tarball. +rm -f tea/.env-* # autosetup environment overrides +find . -type f -name '*~' -exec rm -f \{} \; # backup files +find . -type f -name '#*#' -exec rm -f \{} \; # emacs lock files +find . -type f -name '*.o' -exec rm -f \{} \; +find . -type f -name '*.so' -exec rm -f \{} \; -cd ../ -./configure && make dist -tar -xzf sqlite-$VERSION.tar.gz +./configure && ${MAKE-make} dist +tar xzf sqlite-$VERSION.tar.gz mv sqlite-$VERSION $TARBALLNAME -tar -czf $TARBALLNAME.tar.gz $TARBALLNAME +tar czf $TARBALLNAME.tar.gz $TARBALLNAME mv $TARBALLNAME.tar.gz .. cd .. ls -l $TARBALLNAME.tar.gz diff --git a/tool/mkccode.tcl b/tool/mkccode.tcl new file mode 100755 index 0000000000..8b4fae82c9 --- /dev/null +++ b/tool/mkccode.tcl @@ -0,0 +1,166 @@ +#!/bin/env tclsh +# +# This script is used to amalgamate C source code files into a single +# unit. +# +# Usage example: +# +# tclsh mkccode.tcl -DENABLE_FEATURE_XYZ demoapp.c.in >demoapp.c +# +# The demoapp.c.in file contains a mixture of C code, TCL script, and +# processing directives used by mkccode.tcl to build the final C-code +# output file. Most lines of demoapp.c.in are copied straight through +# into the output. The following control directives are recognized: +# +# BEGIN_STRING +# +# This marks the beginning of large string literal - usually a TCL +# script of some kind. Subsequent lines of text through the first +# line that begins with END_STRING are converted into a C-language +# string literal. +# +# INCLUDE path +# +# The path argument is the name of a file to be inserted in place of +# the INCLUDE line. The path can begin with $ROOT to signify the +# root of the SQLite source tree, or $HOME to signify the directory +# that contains the demoapp.c.in input script itself. If the path does +# not begin with either $ROOT or $HOME, then it is interpreted relative +# to the current working directory. +# +# If the INCLUDE occurs in the middle of BEGIN_STRING...END_STRING +# then all of the text in the input file is converted into C-language +# string literals. +# +# IFDEF macro +# IFNDEF macro +# ELSE +# ENDIF +# +# The text from "IFDEF macro" down to the next ELSE or ENDIF is +# included only if -Dmacro appears as a command-line argument. +# The "IFNDEF macro" simply inverts the initial test. +# +# None of the control directives described above will nest. Only the +# top-level input file ("demoapp.c.in" in the example) is interpreted. +# referenced files are copied verbatim. +# +proc usage {} { + puts stderr "Usage: $::argv0 \[OPTIONS\] TEMPLATE >OUTPUT" + exit 1 +} +set infile {} +foreach ax $argv { + if {[string match -D* $ax]} { + if {[string match *=* $ax]} { + regexp -- {-D([^=]+)=(.*)} $ax all name value + set DEF($name) $value + } else { + set DEF([string range $ax 2 end]) 1 + } + continue + } + if {[string match -* $ax]} { + puts stderr "$::argv0: Unknown option \"$ax\"" + usage + } + if {$infile!=""} { + puts stderr "$::argv0: Surplus argument: \"$ax\"" + usage + } + set infile $ax +} +set ROOT [file normalize [file dir $argv0]/..] +set HOME [file normalize [file dir $infile]] +set in [open $infile rb] +puts [subst {/* DO NOT EDIT +** +** This file was generated by \"$argv0 $argv\". +** To make changes, edit $infile then rerun the generator +** command. +*/}] +set instr 0 +set omit {} +set nomit 0 +set ln 0 +while {1} { + set line [gets $in] + incr ln + if {[eof $in]} break + if {[regexp {^INCLUDE (.*)} $line all path]} { + if {$nomit>0 && [string match *1* $omit]} continue + if {0} { + # https://github.com/msteveb/jimtcl/issues/320 + regsub {^\$ROOT\y} $path $ROOT path + regsub {^\$HOME\y} $path $HOME path + } else { + set path [string map "\$ROOT $ROOT" $path] + set path [string map "\$HOME $HOME" $path] + # or: set path [string map "\$HOME $HOME \$ROOT $ROOT" $path] + } + set in2 [open $path rb] + puts "/* INCLUDE $path */" + if {$instr} { + while {1} { + set line [gets $in2] + if {[eof $in2]} break + set x [string map "\\\\ \\\\\\\\ \\\" \\\\\"" $line] + puts "\"$x\\n\"" + } + } else { + puts [read $in2] + } + puts "/* END $path */" + close $in2 + continue + } + if {[regexp {^BEGIN_STRING} $line]} { + set instr 1 + puts "/* BEGIN_STRING */" + continue + } + if {[regexp {^END_STRING} $line]} { + set instr 0 + puts "/* END_STRING */" + continue + } + if {[regexp {^IFNDEF +([A-Za-z_0-9]+)} $line all name]} { + set omit $omit[info exists DEF($name)] + incr nomit + continue + } + if {[regexp {^IFDEF +([A-Za-z_0-9]+)} $line all name]} { + set omit $omit[expr {![info exists DEF($name)]}] + incr nomit + continue + } + if {[regexp {^ELSE} $line]} { + if {!$nomit} { + puts stderr "$infile:$ln: ELSE without a prior IFDEF" + exit 1 + } + set omit [string range $omit 0 end-1][expr {![string index $omit end]}] + continue + } + if {[regexp {^ENDIF} $line]} { + if {!$nomit} { + puts stderr "$infile:$ln: ENDIF without a prior IFDEF" + exit 1 + } + incr nomit -1 + set omit [string range $omit 0 [expr {$nomit-1}]] + continue + } + if {$nomit>0 && [string match *1* $omit]} { + # noop + } elseif {$instr} { + set x [string map "\\\\ \\\\\\\\ \\\" \\\\\"" $line] + puts "\"$x\\n\"" + } else { + puts $line + } +} +if {$nomit} { + puts stderr "$infile:$ln: One or more unterminated IFDEFs" + exit 1 +} diff --git a/tool/mkctimec.tcl b/tool/mkctimec.tcl new file mode 100755 index 0000000000..56d6b5095c --- /dev/null +++ b/tool/mkctimec.tcl @@ -0,0 +1,466 @@ +#!/usr/bin/tclsh +# +# To build the +# +# const char **azCompileOpt[] +# +# definition used in ctime.c, run this script from +# the checkout root. It generates ctime.c . +# +# Results are normally written into ctime.c. But if an argument is +# provided, results are written there instead. Examples: +# +# tclsh tool/mkctimec.tcl ;# <-- ctime.c +# +# tclsh tool/mkctimec.tcl /dev/tty ;# <-- results to the terminal +# + + +set ::headWarning {/* DO NOT EDIT! +** This file is automatically generated by the script in the canonical +** SQLite source tree at tool/mkctimec.tcl. +** +** To modify this header, edit any of the various lists in that script +** which specify categories of generated conditionals in this file. +*/} + +# Make { and } easier to put into literals (even on EBCDIC machines.) +regexp {(\{)(\})} "{}" ma ::lb ::rb + +set ::headCode " +/* +** 2010 February 23 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** This file implements routines used to report what compile-time options +** SQLite was built with. +*/ +#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS /* IMP: R-16824-07538 */ + +/* +** Include the configuration header output by 'configure' if we're using the +** autoconf-based build +*/ +#if defined(_HAVE_SQLITE_CONFIG_H) && !defined(SQLITECONFIG_H) +#include \"sqlite_cfg.h\" +#define SQLITECONFIG_H 1 +#endif + +/* These macros are provided to \"stringify\" the value of the define +** for those options in which the value is meaningful. */ +#define CTIMEOPT_VAL_(opt) #opt +#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt) + +/* Like CTIMEOPT_VAL, but especially for SQLITE_DEFAULT_LOOKASIDE. This +** option requires a separate macro because legal values contain a single +** comma. e.g. (-DSQLITE_DEFAULT_LOOKASIDE=\"100,100\") */ +#define CTIMEOPT_VAL2_(opt1,opt2) #opt1 \",\" #opt2 +#define CTIMEOPT_VAL2(opt) CTIMEOPT_VAL2_(opt) +#include \"sqliteInt.h\" + +/* +** An array of names of all compile-time options. This array should +** be sorted A-Z. +** +** This array looks large, but in a typical installation actually uses +** only a handful of compile-time options, so most times this array is usually +** rather short and uses little memory space. +*/ +static const char * const sqlite3azCompileOpt\[\] = $::lb +" + +set ::tailCode " +$::rb ; + +const char **sqlite3CompileOptions(int *pnOpt){ + *pnOpt = sizeof(sqlite3azCompileOpt) / sizeof(sqlite3azCompileOpt\[0\]); + return (const char**)sqlite3azCompileOpt; +} + +#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */ +" + +# All Boolean compile time options which default to something +# other than 0 or empty. The default is paired with the PP +# symbol so that a differing define can be detected. +# +set boolean_defnnz_options { + {SQLITE_HOMEGROWN_RECURSIVE_MUTEX 1} + {SQLITE_POWERSAFE_OVERWRITE 1} + {SQLITE_DEFAULT_MEMSTATUS 1} + {SQLITE_OMIT_TRACE 1} + {SQLITE_ALLOW_COVERING_INDEX_SCAN 1} +} + +# All Boolean compile time options which default to 0 or empty. +# +set boolean_defnil_options { + SQLITE_32BIT_ROWID + SQLITE_4_BYTE_ALIGNED_MALLOC + SQLITE_ALLOW_ROWID_IN_VIEW + SQLITE_ALLOW_URI_AUTHORITY + SQLITE_BUG_COMPATIBLE_20160819 + SQLITE_BUG_COMPATIBLE_20250510 + SQLITE_CASE_SENSITIVE_LIKE + SQLITE_CHECK_PAGES + SQLITE_COVERAGE_TEST + SQLITE_DEBUG + SQLITE_DEFAULT_AUTOMATIC_INDEX + SQLITE_DEFAULT_AUTOVACUUM + SQLITE_DEFAULT_CKPTFULLFSYNC + SQLITE_DEFAULT_FOREIGN_KEYS + SQLITE_DEFAULT_LOCKING_MODE + SQLITE_DEFAULT_RECURSIVE_TRIGGERS + SQLITE_DEFAULT_SYNCHRONOUS + SQLITE_DEFAULT_WAL_SYNCHRONOUS + SQLITE_DIRECT_OVERFLOW_READ + SQLITE_DISABLE_DIRSYNC + SQLITE_DISABLE_FTS3_UNICODE + SQLITE_DISABLE_FTS4_DEFERRED + SQLITE_DISABLE_INTRINSIC + SQLITE_DISABLE_LFS + SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS + SQLITE_DISABLE_SKIPAHEAD_DISTINCT + SQLITE_ENABLE_8_3_NAMES + SQLITE_ENABLE_API_ARMOR + SQLITE_ENABLE_ATOMIC_WRITE + SQLITE_ENABLE_BATCH_ATOMIC_WRITE + SQLITE_ENABLE_BYTECODE_VTAB + SQLITE_ENABLE_CARRAY + SQLITE_ENABLE_COLUMN_METADATA + SQLITE_ENABLE_COLUMN_USED_MASK + SQLITE_ENABLE_COSTMULT + SQLITE_ENABLE_CURSOR_HINTS + SQLITE_ENABLE_DBPAGE_VTAB + SQLITE_ENABLE_DBSTAT_VTAB + SQLITE_ENABLE_EXPENSIVE_ASSERT + SQLITE_ENABLE_EXPLAIN_COMMENTS + SQLITE_ENABLE_FTS3 + SQLITE_ENABLE_FTS3_PARENTHESIS + SQLITE_ENABLE_FTS3_TOKENIZER + SQLITE_ENABLE_FTS4 + SQLITE_ENABLE_FTS5 + SQLITE_ENABLE_GEOPOLY + SQLITE_ENABLE_HIDDEN_COLUMNS + SQLITE_ENABLE_ICU + SQLITE_ENABLE_IOTRACE + SQLITE_ENABLE_LOAD_EXTENSION + SQLITE_ENABLE_LOCKING_STYLE + SQLITE_ENABLE_MATH_FUNCTIONS + SQLITE_ENABLE_MEMORY_MANAGEMENT + SQLITE_ENABLE_MEMSYS3 + SQLITE_ENABLE_MEMSYS5 + SQLITE_ENABLE_MULTIPLEX + SQLITE_ENABLE_NORMALIZE + SQLITE_ENABLE_NULL_TRIM + SQLITE_ENABLE_ORDERED_SET_AGGREGATES + SQLITE_ENABLE_OFFSET_SQL_FUNC + SQLITE_ENABLE_OVERSIZE_CELL_CHECK + SQLITE_ENABLE_PERCENTILE + SQLITE_ENABLE_PREUPDATE_HOOK + SQLITE_ENABLE_QPSG + SQLITE_ENABLE_RBU + SQLITE_ENABLE_RTREE + SQLITE_ENABLE_SESSION + SQLITE_ENABLE_SETLK_TIMEOUT + SQLITE_ENABLE_SNAPSHOT + SQLITE_ENABLE_SORTER_REFERENCES + SQLITE_ENABLE_SQLLOG + SQLITE_ENABLE_STAT4 + SQLITE_ENABLE_STMT_SCANSTATUS + SQLITE_ENABLE_STMTVTAB + SQLITE_ENABLE_TREETRACE + SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION + SQLITE_ENABLE_UNLOCK_NOTIFY + SQLITE_ENABLE_UPDATE_DELETE_LIMIT + SQLITE_ENABLE_URI_00_ERROR + SQLITE_ENABLE_VFSTRACE + SQLITE_ENABLE_WHERETRACE + SQLITE_ENABLE_ZIPVFS + SQLITE_EXPLAIN_ESTIMATED_ROWS + SQLITE_EXTRA_IFNULLROW + SQLITE_FTS5_ENABLE_TEST_MI + SQLITE_FTS5_NO_WITHOUT_ROWID + SQLITE_IGNORE_AFP_LOCK_ERRORS + SQLITE_IGNORE_FLOCK_LOCK_ERRORS + SQLITE_INLINE_MEMCPY + SQLITE_INT64_TYPE + SQLITE_LEGACY_JSON_VALID + SQLITE_LIKE_DOESNT_MATCH_BLOBS + SQLITE_LOCK_TRACE + SQLITE_LOG_CACHE_SPILL + SQLITE_MEMDEBUG + SQLITE_MIXED_ENDIAN_64BIT_FLOAT + SQLITE_MMAP_READWRITE + SQLITE_MUTEX_NOOP + SQLITE_MUTEX_OMIT + SQLITE_MUTEX_PTHREADS + SQLITE_MUTEX_W32 + SQLITE_NEED_ERR_NAME + SQLITE_NO_SYNC + SQLITE_OMIT_ALTERTABLE + SQLITE_OMIT_ANALYZE + SQLITE_OMIT_ATTACH + SQLITE_OMIT_AUTHORIZATION + SQLITE_OMIT_AUTOINCREMENT + SQLITE_OMIT_AUTOINIT + SQLITE_OMIT_AUTOMATIC_INDEX + SQLITE_OMIT_AUTORESET + SQLITE_OMIT_AUTOVACUUM + SQLITE_OMIT_BETWEEN_OPTIMIZATION + SQLITE_OMIT_BLOB_LITERAL + SQLITE_OMIT_CAST + SQLITE_OMIT_CHECK + SQLITE_OMIT_COMPLETE + SQLITE_OMIT_COMPOUND_SELECT + SQLITE_OMIT_CONFLICT_CLAUSE + SQLITE_OMIT_CTE + SQLITE_OMIT_DECLTYPE + SQLITE_OMIT_DEPRECATED + SQLITE_OMIT_DESERIALIZE + SQLITE_OMIT_DISKIO + SQLITE_OMIT_EXPLAIN + SQLITE_OMIT_FLAG_PRAGMAS + SQLITE_OMIT_FLOATING_POINT + SQLITE_OMIT_FOREIGN_KEY + SQLITE_OMIT_GET_TABLE + SQLITE_OMIT_HEX_INTEGER + SQLITE_OMIT_INCRBLOB + SQLITE_OMIT_INTEGRITY_CHECK + SQLITE_OMIT_INTROSPECTION_PRAGMAS + SQLITE_OMIT_JSON + SQLITE_OMIT_LIKE_OPTIMIZATION + SQLITE_OMIT_LOAD_EXTENSION + SQLITE_OMIT_LOCALTIME + SQLITE_OMIT_LOOKASIDE + SQLITE_OMIT_MEMORYDB + SQLITE_OMIT_OR_OPTIMIZATION + SQLITE_OMIT_PAGER_PRAGMAS + SQLITE_OMIT_PARSER_TRACE + SQLITE_OMIT_POPEN + SQLITE_OMIT_PRAGMA + SQLITE_OMIT_PROGRESS_CALLBACK + SQLITE_OMIT_QUICKBALANCE + SQLITE_OMIT_REINDEX + SQLITE_OMIT_SCHEMA_PRAGMAS + SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS + SQLITE_OMIT_SEH + SQLITE_OMIT_SHARED_CACHE + SQLITE_OMIT_SHUTDOWN_DIRECTORIES + SQLITE_OMIT_SUBQUERY + SQLITE_OMIT_TCL_VARIABLE + SQLITE_OMIT_TEMPDB + SQLITE_OMIT_TEST_CONTROL + SQLITE_OMIT_TRIGGER + SQLITE_OMIT_TRUNCATE_OPTIMIZATION + SQLITE_OMIT_UTF16 + SQLITE_OMIT_VACUUM + SQLITE_OMIT_VIEW + SQLITE_OMIT_VIRTUALTABLE + SQLITE_OMIT_WAL + SQLITE_OMIT_WSD + SQLITE_OMIT_XFER_OPT + SQLITE_PERFORMANCE_TRACE + SQLITE_PREFER_PROXY_LOCKING + SQLITE_PROXY_DEBUG + SQLITE_REVERSE_UNORDERED_SELECTS + SQLITE_RTREE_INT_ONLY + SQLITE_SECURE_DELETE + SQLITE_SMALL_STACK + SQLITE_SOUNDEX + SQLITE_SUBSTR_COMPATIBILITY + SQLITE_TCL + SQLITE_TEST + SQLITE_UNLINK_AFTER_CLOSE + SQLITE_UNTESTABLE + SQLITE_USE_ALLOCA + SQLITE_USE_FCNTL_TRACE + SQLITE_USE_URI + SQLITE_VDBE_COVERAGE + SQLITE_WIN32_MALLOC + SQLITE_ZERO_MALLOC + SQLITE_HAS_CODEC +} + +# All compile time options for which the assigned value is other than boolean +# and is a comma-separated scalar pair. +# +set value2_options { + SQLITE_DEFAULT_LOOKASIDE +} + +# All compile time options for which the assigned value is other than boolean +# and is a single scalar. +# +set value_options { + SQLITE_ATOMIC_INTRINSICS + SQLITE_BITMASK_TYPE + SQLITE_DEFAULT_CACHE_SIZE + SQLITE_DEFAULT_FILE_FORMAT + SQLITE_DEFAULT_FILE_PERMISSIONS + SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT + SQLITE_DEFAULT_LOCKING_MODE + SQLITE_DEFAULT_MMAP_SIZE + SQLITE_DEFAULT_PAGE_SIZE + SQLITE_DEFAULT_PCACHE_INITSZ + SQLITE_DEFAULT_PROXYDIR_PERMISSIONS + SQLITE_DEFAULT_ROWEST + SQLITE_DEFAULT_SECTOR_SIZE + SQLITE_DEFAULT_SYNCHRONOUS + SQLITE_DEFAULT_WAL_AUTOCHECKPOINT + SQLITE_DEFAULT_WAL_SYNCHRONOUS + SQLITE_DEFAULT_WORKER_THREADS + SQLITE_DQS + SQLITE_ENABLE_8_3_NAMES + SQLITE_ENABLE_CEROD + SQLITE_ENABLE_LOCKING_STYLE + SQLITE_EXTRA_AUTOEXT + SQLITE_EXTRA_INIT + SQLITE_EXTRA_INIT_MUTEXED + SQLITE_EXTRA_SHUTDOWN + SQLITE_FTS3_MAX_EXPR_DEPTH + SQLITE_INTEGRITY_CHECK_ERROR_MAX + SQLITE_MALLOC_SOFT_LIMIT + SQLITE_MAX_ATTACHED + SQLITE_MAX_COLUMN + SQLITE_MAX_COMPOUND_SELECT + SQLITE_MAX_DEFAULT_PAGE_SIZE + SQLITE_MAX_EXPR_DEPTH + SQLITE_MAX_FUNCTION_ARG + SQLITE_MAX_LENGTH + SQLITE_MAX_LIKE_PATTERN_LENGTH + SQLITE_MAX_MEMORY + SQLITE_MAX_MMAP_SIZE + SQLITE_MAX_MMAP_SIZE_ + SQLITE_MAX_PAGE_COUNT + SQLITE_MAX_PAGE_SIZE + SQLITE_MAX_SCHEMA_RETRY + SQLITE_MAX_SQL_LENGTH + SQLITE_MAX_TRIGGER_DEPTH + SQLITE_MAX_VARIABLE_NUMBER + SQLITE_MAX_VDBE_OP + SQLITE_MAX_WORKER_THREADS + SQLITE_SORTER_PMASZ + SQLITE_STAT4_SAMPLES + SQLITE_STMTJRNL_SPILL + SQLITE_TEMP_STORE +} + +# Options that require custom code. +# +set options(COMPILER) { +#if defined(__clang__) && defined(__clang_major__) + "COMPILER=clang-" CTIMEOPT_VAL(__clang_major__) "." + CTIMEOPT_VAL(__clang_minor__) "." + CTIMEOPT_VAL(__clang_patchlevel__), +#elif defined(_MSC_VER) + "COMPILER=msvc-" CTIMEOPT_VAL(_MSC_VER), +#elif defined(__GNUC__) && defined(__VERSION__) + "COMPILER=gcc-" __VERSION__, +#endif +} +set options(HAVE_ISNAN) { +#if HAVE_ISNAN || SQLITE_HAVE_ISNAN + "HAVE_ISNAN", +#endif +} +set options(OMIT_DATETIME_FUNCS) { +#if defined(SQLITE_OMIT_DATETIME_FUNCS) || defined(SQLITE_OMIT_FLOATING_POINT) + "OMIT_DATETIME_FUNCS", +#endif +} +set options(SYSTEM_MALLOC) "\ +#if (!defined(SQLITE_WIN32_MALLOC) \\ + && !defined(SQLITE_ZERO_MALLOC) \\ + && !defined(SQLITE_MEMDEBUG) \\ + ) || defined(SQLITE_SYSTEM_MALLOC) + \"SYSTEM_MALLOC\", +#endif +" +set options(THREADSAFE) { +#if defined(SQLITE_THREADSAFE) + "THREADSAFE=" CTIMEOPT_VAL(SQLITE_THREADSAFE), +#elif defined(THREADSAFE) + "THREADSAFE=" CTIMEOPT_VAL(THREADSAFE), +#else + "THREADSAFE=1", +#endif +} + +proc trim_name {in} { + set ret $in + if {[string range $in 0 6]=="SQLITE_"} { + set ret [string range $in 7 end] + } + return $ret +} + +foreach name_defval $boolean_defnnz_options { + set b [lindex $name_defval 0] + set defval [lindex $name_defval 1] + set name [trim_name $b] + set options($name) [subst { +#ifdef $b +# if $b != $defval + "$name=" CTIMEOPT_VAL($b), +# endif +#endif +}] +} + +foreach b $boolean_defnil_options { + set name [trim_name $b] + set options($name) [subst { +#ifdef $b + "$name", +#endif +}] +} + +foreach v $value_options { + set name [trim_name $v] + set options($name) [subst { +#ifdef $v + "$name=" CTIMEOPT_VAL($v), +#endif +}] +} + +foreach v $value2_options { + set name [trim_name $v] + set options($name) [subst { +#ifdef $v + "$name=" CTIMEOPT_VAL2($v), +#endif +}] +} + +if {$argc>0} { + set destfile [lindex $argv 0] +} else { + set destfile ctime.c + puts "Overwriting $destfile..." +} + +if {[catch {set cfd [open $destfile w]}]!=0} { + puts stderr "File '$destfile' unwritable." + exit 1; +} +fconfigure $cfd -translation binary + +puts $cfd $::headWarning; +puts $cfd $::headCode; +foreach o [lsort [array names options]] { + puts $cfd [string trim $options($o)] +} +puts -nonewline $cfd $::tailCode; + +close $cfd diff --git a/tool/mkkeywordhash.c b/tool/mkkeywordhash.c index 7e5287ea54..188c0a29ac 100644 --- a/tool/mkkeywordhash.c +++ b/tool/mkkeywordhash.c @@ -36,6 +36,7 @@ struct Keyword { char *zName; /* The keyword name */ char *zTokenType; /* Token value for this keyword */ int mask; /* Code this keyword if non-zero */ + int priority; /* Put higher priorities earlier in the hash chain */ int id; /* Unique ID for this record */ int hash; /* Hash on the keyword */ int offset; /* Offset to start of name string */ @@ -51,7 +52,7 @@ struct Keyword { /* ** Define masks used to determine which keywords are allowed */ -#ifdef SQLITE_OMIT_ALTERTABLE +#if defined(SQLITE_OMIT_ALTERTABLE) || defined(SQLITE_OMIT_VIRTUALTABLE) # define ALTER 0 #else # define ALTER 0x00000001 @@ -143,135 +144,185 @@ struct Keyword { #else # define CTE 0x00040000 #endif +#ifdef SQLITE_OMIT_UPSERT +# define UPSERT 0 +#else +# define UPSERT 0x00080000 +#endif +#ifdef SQLITE_OMIT_WINDOWFUNC +# define WINDOWFUNC 0 +#else +# define WINDOWFUNC 0x00100000 +#endif +#ifdef SQLITE_OMIT_GENERATED_COLUMNS +# define GENCOL 0 +#else +# define GENCOL 0x00200000 +#endif +#ifdef SQLITE_OMIT_RETURNING +# define RETURNING 0 +#else +# define RETURNING 0x00400000 +#endif +#ifndef SQLITE_ENABLE_ORDERED_SET_AGGREGATES +# define ORDERSET 0 +#else +# define ORDERSET 0x00800000 +#endif + /* ** These are the keywords */ static Keyword aKeywordTable[] = { - { "ABORT", "TK_ABORT", CONFLICT|TRIGGER }, - { "ACTION", "TK_ACTION", FKEY }, - { "ADD", "TK_ADD", ALTER }, - { "AFTER", "TK_AFTER", TRIGGER }, - { "ALL", "TK_ALL", ALWAYS }, - { "ALTER", "TK_ALTER", ALTER }, - { "ANALYZE", "TK_ANALYZE", ANALYZE }, - { "AND", "TK_AND", ALWAYS }, - { "AS", "TK_AS", ALWAYS }, - { "ASC", "TK_ASC", ALWAYS }, - { "ATTACH", "TK_ATTACH", ATTACH }, - { "AUTOINCREMENT", "TK_AUTOINCR", AUTOINCR }, - { "BEFORE", "TK_BEFORE", TRIGGER }, - { "BEGIN", "TK_BEGIN", ALWAYS }, - { "BETWEEN", "TK_BETWEEN", ALWAYS }, - { "BY", "TK_BY", ALWAYS }, - { "CASCADE", "TK_CASCADE", FKEY }, - { "CASE", "TK_CASE", ALWAYS }, - { "CAST", "TK_CAST", CAST }, - { "CHECK", "TK_CHECK", ALWAYS }, - { "COLLATE", "TK_COLLATE", ALWAYS }, - { "COLUMN", "TK_COLUMNKW", ALTER }, - { "COMMIT", "TK_COMMIT", ALWAYS }, - { "CONFLICT", "TK_CONFLICT", CONFLICT }, - { "CONSTRAINT", "TK_CONSTRAINT", ALWAYS }, - { "CREATE", "TK_CREATE", ALWAYS }, - { "CROSS", "TK_JOIN_KW", ALWAYS }, - { "CURRENT_DATE", "TK_CTIME_KW", ALWAYS }, - { "CURRENT_TIME", "TK_CTIME_KW", ALWAYS }, - { "CURRENT_TIMESTAMP","TK_CTIME_KW", ALWAYS }, - { "DATABASE", "TK_DATABASE", ATTACH }, - { "DEFAULT", "TK_DEFAULT", ALWAYS }, - { "DEFERRED", "TK_DEFERRED", ALWAYS }, - { "DEFERRABLE", "TK_DEFERRABLE", FKEY }, - { "DELETE", "TK_DELETE", ALWAYS }, - { "DESC", "TK_DESC", ALWAYS }, - { "DETACH", "TK_DETACH", ATTACH }, - { "DISTINCT", "TK_DISTINCT", ALWAYS }, - { "DROP", "TK_DROP", ALWAYS }, - { "END", "TK_END", ALWAYS }, - { "EACH", "TK_EACH", TRIGGER }, - { "ELSE", "TK_ELSE", ALWAYS }, - { "ESCAPE", "TK_ESCAPE", ALWAYS }, - { "EXCEPT", "TK_EXCEPT", COMPOUND }, - { "EXCLUSIVE", "TK_EXCLUSIVE", ALWAYS }, - { "EXISTS", "TK_EXISTS", ALWAYS }, - { "EXPLAIN", "TK_EXPLAIN", EXPLAIN }, - { "FAIL", "TK_FAIL", CONFLICT|TRIGGER }, - { "FOR", "TK_FOR", TRIGGER }, - { "FOREIGN", "TK_FOREIGN", FKEY }, - { "FROM", "TK_FROM", ALWAYS }, - { "FULL", "TK_JOIN_KW", ALWAYS }, - { "GLOB", "TK_LIKE_KW", ALWAYS }, - { "GROUP", "TK_GROUP", ALWAYS }, - { "HAVING", "TK_HAVING", ALWAYS }, - { "IF", "TK_IF", ALWAYS }, - { "IGNORE", "TK_IGNORE", CONFLICT|TRIGGER }, - { "IMMEDIATE", "TK_IMMEDIATE", ALWAYS }, - { "IN", "TK_IN", ALWAYS }, - { "INDEX", "TK_INDEX", ALWAYS }, - { "INDEXED", "TK_INDEXED", ALWAYS }, - { "INITIALLY", "TK_INITIALLY", FKEY }, - { "INNER", "TK_JOIN_KW", ALWAYS }, - { "INSERT", "TK_INSERT", ALWAYS }, - { "INSTEAD", "TK_INSTEAD", TRIGGER }, - { "INTERSECT", "TK_INTERSECT", COMPOUND }, - { "INTO", "TK_INTO", ALWAYS }, - { "IS", "TK_IS", ALWAYS }, - { "ISNULL", "TK_ISNULL", ALWAYS }, - { "JOIN", "TK_JOIN", ALWAYS }, - { "KEY", "TK_KEY", ALWAYS }, - { "LEFT", "TK_JOIN_KW", ALWAYS }, - { "LIKE", "TK_LIKE_KW", ALWAYS }, - { "LIMIT", "TK_LIMIT", ALWAYS }, - { "MATCH", "TK_MATCH", ALWAYS }, - { "NATURAL", "TK_JOIN_KW", ALWAYS }, - { "NO", "TK_NO", FKEY }, - { "NOT", "TK_NOT", ALWAYS }, - { "NOTNULL", "TK_NOTNULL", ALWAYS }, - { "NULL", "TK_NULL", ALWAYS }, - { "OF", "TK_OF", ALWAYS }, - { "OFFSET", "TK_OFFSET", ALWAYS }, - { "ON", "TK_ON", ALWAYS }, - { "OR", "TK_OR", ALWAYS }, - { "ORDER", "TK_ORDER", ALWAYS }, - { "OUTER", "TK_JOIN_KW", ALWAYS }, - { "PLAN", "TK_PLAN", EXPLAIN }, - { "PRAGMA", "TK_PRAGMA", PRAGMA }, - { "PRIMARY", "TK_PRIMARY", ALWAYS }, - { "QUERY", "TK_QUERY", EXPLAIN }, - { "RAISE", "TK_RAISE", TRIGGER }, - { "RECURSIVE", "TK_RECURSIVE", CTE }, - { "REFERENCES", "TK_REFERENCES", FKEY }, - { "REGEXP", "TK_LIKE_KW", ALWAYS }, - { "REINDEX", "TK_REINDEX", REINDEX }, - { "RELEASE", "TK_RELEASE", ALWAYS }, - { "RENAME", "TK_RENAME", ALTER }, - { "REPLACE", "TK_REPLACE", CONFLICT }, - { "RESTRICT", "TK_RESTRICT", FKEY }, - { "RIGHT", "TK_JOIN_KW", ALWAYS }, - { "ROLLBACK", "TK_ROLLBACK", ALWAYS }, - { "ROW", "TK_ROW", TRIGGER }, - { "SAVEPOINT", "TK_SAVEPOINT", ALWAYS }, - { "SELECT", "TK_SELECT", ALWAYS }, - { "SET", "TK_SET", ALWAYS }, - { "TABLE", "TK_TABLE", ALWAYS }, - { "TEMP", "TK_TEMP", ALWAYS }, - { "TEMPORARY", "TK_TEMP", ALWAYS }, - { "THEN", "TK_THEN", ALWAYS }, - { "TO", "TK_TO", ALWAYS }, - { "TRANSACTION", "TK_TRANSACTION", ALWAYS }, - { "TRIGGER", "TK_TRIGGER", TRIGGER }, - { "UNION", "TK_UNION", COMPOUND }, - { "UNIQUE", "TK_UNIQUE", ALWAYS }, - { "UPDATE", "TK_UPDATE", ALWAYS }, - { "USING", "TK_USING", ALWAYS }, - { "VACUUM", "TK_VACUUM", VACUUM }, - { "VALUES", "TK_VALUES", ALWAYS }, - { "VIEW", "TK_VIEW", VIEW }, - { "VIRTUAL", "TK_VIRTUAL", VTAB }, - { "WITH", "TK_WITH", CTE }, - { "WITHOUT", "TK_WITHOUT", ALWAYS }, - { "WHEN", "TK_WHEN", ALWAYS }, - { "WHERE", "TK_WHERE", ALWAYS }, + { "ABORT", "TK_ABORT", CONFLICT|TRIGGER, 0 }, + { "ACTION", "TK_ACTION", FKEY, 0 }, + { "ADD", "TK_ADD", ALTER, 1 }, + { "AFTER", "TK_AFTER", TRIGGER, 0 }, + { "ALL", "TK_ALL", ALWAYS, 0 }, + { "ALTER", "TK_ALTER", ALTER, 0 }, + { "ALWAYS", "TK_ALWAYS", GENCOL, 0 }, + { "ANALYZE", "TK_ANALYZE", ANALYZE, 0 }, + { "AND", "TK_AND", ALWAYS, 10 }, + { "AS", "TK_AS", ALWAYS, 10 }, + { "ASC", "TK_ASC", ALWAYS, 0 }, + { "ATTACH", "TK_ATTACH", ATTACH, 1 }, + { "AUTOINCREMENT", "TK_AUTOINCR", AUTOINCR, 0 }, + { "BEFORE", "TK_BEFORE", TRIGGER, 0 }, + { "BEGIN", "TK_BEGIN", ALWAYS, 1 }, + { "BETWEEN", "TK_BETWEEN", ALWAYS, 5 }, + { "BY", "TK_BY", ALWAYS, 10 }, + { "CASCADE", "TK_CASCADE", FKEY, 1 }, + { "CASE", "TK_CASE", ALWAYS, 5 }, + { "CAST", "TK_CAST", CAST, 5 }, + { "CHECK", "TK_CHECK", ALWAYS, 1 }, + { "COLLATE", "TK_COLLATE", ALWAYS, 1 }, + { "COLUMN", "TK_COLUMNKW", ALTER, 1 }, + { "COMMIT", "TK_COMMIT", ALWAYS, 1 }, + { "CONFLICT", "TK_CONFLICT", CONFLICT, 0 }, + { "CONSTRAINT", "TK_CONSTRAINT", ALWAYS, 1 }, + { "CREATE", "TK_CREATE", ALWAYS, 2 }, + { "CROSS", "TK_JOIN_KW", ALWAYS, 3 }, + { "CURRENT", "TK_CURRENT", WINDOWFUNC, 1 }, + { "CURRENT_DATE", "TK_CTIME_KW", ALWAYS, 1 }, + { "CURRENT_TIME", "TK_CTIME_KW", ALWAYS, 1 }, + { "CURRENT_TIMESTAMP","TK_CTIME_KW", ALWAYS, 1 }, + { "DATABASE", "TK_DATABASE", ATTACH, 0 }, + { "DEFAULT", "TK_DEFAULT", ALWAYS, 1 }, + { "DEFERRED", "TK_DEFERRED", ALWAYS, 1 }, + { "DEFERRABLE", "TK_DEFERRABLE", FKEY, 1 }, + { "DELETE", "TK_DELETE", ALWAYS, 10 }, + { "DESC", "TK_DESC", ALWAYS, 3 }, + { "DETACH", "TK_DETACH", ATTACH, 0 }, + { "DISTINCT", "TK_DISTINCT", ALWAYS, 5 }, + { "DO", "TK_DO", UPSERT, 2 }, + { "DROP", "TK_DROP", ALWAYS, 1 }, + { "END", "TK_END", ALWAYS, 1 }, + { "EACH", "TK_EACH", TRIGGER, 1 }, + { "ELSE", "TK_ELSE", ALWAYS, 2 }, + { "ESCAPE", "TK_ESCAPE", ALWAYS, 4 }, + { "EXCEPT", "TK_EXCEPT", COMPOUND, 4 }, + { "EXCLUSIVE", "TK_EXCLUSIVE", ALWAYS, 1 }, + { "EXCLUDE", "TK_EXCLUDE", WINDOWFUNC, 1 }, + { "EXISTS", "TK_EXISTS", ALWAYS, 4 }, + { "EXPLAIN", "TK_EXPLAIN", EXPLAIN, 1 }, + { "FAIL", "TK_FAIL", CONFLICT|TRIGGER, 1 }, + { "FILTER", "TK_FILTER", WINDOWFUNC, 4 }, + { "FIRST", "TK_FIRST", ALWAYS, 4 }, + { "FOLLOWING", "TK_FOLLOWING", WINDOWFUNC, 4 }, + { "FOR", "TK_FOR", TRIGGER, 2 }, + { "FOREIGN", "TK_FOREIGN", FKEY, 1 }, + { "FROM", "TK_FROM", ALWAYS, 10 }, + { "FULL", "TK_JOIN_KW", ALWAYS, 3 }, + { "GENERATED", "TK_GENERATED", ALWAYS, 1 }, + { "GLOB", "TK_LIKE_KW", ALWAYS, 3 }, + { "GROUP", "TK_GROUP", ALWAYS, 5 }, + { "GROUPS", "TK_GROUPS", WINDOWFUNC, 2 }, + { "HAVING", "TK_HAVING", ALWAYS, 5 }, + { "IF", "TK_IF", ALWAYS, 2 }, + { "IGNORE", "TK_IGNORE", CONFLICT|TRIGGER, 1 }, + { "IMMEDIATE", "TK_IMMEDIATE", ALWAYS, 1 }, + { "IN", "TK_IN", ALWAYS, 10 }, + { "INDEX", "TK_INDEX", ALWAYS, 1 }, + { "INDEXED", "TK_INDEXED", ALWAYS, 0 }, + { "INITIALLY", "TK_INITIALLY", FKEY, 1 }, + { "INNER", "TK_JOIN_KW", ALWAYS, 1 }, + { "INSERT", "TK_INSERT", ALWAYS, 10 }, + { "INSTEAD", "TK_INSTEAD", TRIGGER, 1 }, + { "INTERSECT", "TK_INTERSECT", COMPOUND, 5 }, + { "INTO", "TK_INTO", ALWAYS, 10 }, + { "IS", "TK_IS", ALWAYS, 5 }, + { "ISNULL", "TK_ISNULL", ALWAYS, 5 }, + { "JOIN", "TK_JOIN", ALWAYS, 5 }, + { "KEY", "TK_KEY", ALWAYS, 1 }, + { "LAST", "TK_LAST", ALWAYS, 4 }, + { "LEFT", "TK_JOIN_KW", ALWAYS, 5 }, + { "LIKE", "TK_LIKE_KW", ALWAYS, 5 }, + { "LIMIT", "TK_LIMIT", ALWAYS, 3 }, + { "MATCH", "TK_MATCH", ALWAYS, 2 }, + { "MATERIALIZED", "TK_MATERIALIZED", CTE, 12 }, + { "NATURAL", "TK_JOIN_KW", ALWAYS, 3 }, + { "NO", "TK_NO", FKEY|WINDOWFUNC, 2 }, + { "NOT", "TK_NOT", ALWAYS, 10 }, + { "NOTHING", "TK_NOTHING", UPSERT, 1 }, + { "NOTNULL", "TK_NOTNULL", ALWAYS, 3 }, + { "NULL", "TK_NULL", ALWAYS, 10 }, + { "NULLS", "TK_NULLS", ALWAYS, 3 }, + { "OF", "TK_OF", ALWAYS, 3 }, + { "OFFSET", "TK_OFFSET", ALWAYS, 1 }, + { "ON", "TK_ON", ALWAYS, 1 }, + { "OR", "TK_OR", ALWAYS, 9 }, + { "ORDER", "TK_ORDER", ALWAYS, 10 }, + { "OTHERS", "TK_OTHERS", WINDOWFUNC, 3 }, + { "OUTER", "TK_JOIN_KW", ALWAYS, 5 }, + { "OVER", "TK_OVER", WINDOWFUNC, 3 }, + { "PARTITION", "TK_PARTITION", WINDOWFUNC, 3 }, + { "PLAN", "TK_PLAN", EXPLAIN, 0 }, + { "PRAGMA", "TK_PRAGMA", PRAGMA, 0 }, + { "PRECEDING", "TK_PRECEDING", WINDOWFUNC, 3 }, + { "PRIMARY", "TK_PRIMARY", ALWAYS, 1 }, + { "QUERY", "TK_QUERY", EXPLAIN, 0 }, + { "RAISE", "TK_RAISE", TRIGGER, 1 }, + { "RANGE", "TK_RANGE", WINDOWFUNC, 3 }, + { "RECURSIVE", "TK_RECURSIVE", CTE, 3 }, + { "REFERENCES", "TK_REFERENCES", FKEY, 1 }, + { "REGEXP", "TK_LIKE_KW", ALWAYS, 3 }, + { "REINDEX", "TK_REINDEX", REINDEX, 1 }, + { "RELEASE", "TK_RELEASE", ALWAYS, 1 }, + { "RENAME", "TK_RENAME", ALTER, 1 }, + { "REPLACE", "TK_REPLACE", CONFLICT, 10 }, + { "RESTRICT", "TK_RESTRICT", FKEY, 1 }, + { "RETURNING", "TK_RETURNING", RETURNING, 10 }, + { "RIGHT", "TK_JOIN_KW", ALWAYS, 0 }, + { "ROLLBACK", "TK_ROLLBACK", ALWAYS, 1 }, + { "ROW", "TK_ROW", TRIGGER, 1 }, + { "ROWS", "TK_ROWS", ALWAYS, 1 }, + { "SAVEPOINT", "TK_SAVEPOINT", ALWAYS, 1 }, + { "SELECT", "TK_SELECT", ALWAYS, 10 }, + { "SET", "TK_SET", ALWAYS, 10 }, + { "TABLE", "TK_TABLE", ALWAYS, 1 }, + { "TEMP", "TK_TEMP", ALWAYS, 1 }, + { "TEMPORARY", "TK_TEMP", ALWAYS, 1 }, + { "THEN", "TK_THEN", ALWAYS, 3 }, + { "TIES", "TK_TIES", WINDOWFUNC, 3 }, + { "TO", "TK_TO", ALWAYS, 3 }, + { "TRANSACTION", "TK_TRANSACTION", ALWAYS, 1 }, + { "TRIGGER", "TK_TRIGGER", TRIGGER, 1 }, + { "UNBOUNDED", "TK_UNBOUNDED", WINDOWFUNC, 3 }, + { "UNION", "TK_UNION", COMPOUND, 3 }, + { "UNIQUE", "TK_UNIQUE", ALWAYS, 1 }, + { "UPDATE", "TK_UPDATE", ALWAYS, 10 }, + { "USING", "TK_USING", ALWAYS, 8 }, + { "VACUUM", "TK_VACUUM", VACUUM, 1 }, + { "VALUES", "TK_VALUES", ALWAYS, 10 }, + { "VIEW", "TK_VIEW", VIEW, 1 }, + { "VIRTUAL", "TK_VIRTUAL", VTAB, 1 }, + { "WHEN", "TK_WHEN", ALWAYS, 1 }, + { "WHERE", "TK_WHERE", ALWAYS, 10 }, + { "WINDOW", "TK_WINDOW", WINDOWFUNC, 3 }, + { "WITH", "TK_WITH", CTE, 4 }, + { "WITHIN", "TK_WITHIN", ORDERSET, 1 }, + { "WITHOUT", "TK_WITHOUT", ALWAYS, 1 }, }; /* Number of keywords */ @@ -326,6 +377,32 @@ static Keyword *findById(int id){ return &aKeywordTable[i]; } +/* +** If aKeyword[*pFrom-1].iNext has a higher priority that aKeyword[*pFrom-1] +** itself, then swap them. +*/ +static void reorder(int *pFrom){ + int i = *pFrom - 1; + int j; + if( i<0 ) return; + j = aKeywordTable[i].iNext; + if( j==0 ) return; + j--; + if( aKeywordTable[i].priority >= aKeywordTable[j].priority ) return; + aKeywordTable[i].iNext = aKeywordTable[j].iNext; + aKeywordTable[j].iNext = i+1; + *pFrom = j+1; + reorder(&aKeywordTable[i].iNext); +} + +/* Parameter to the hash function +*/ +#define HASH_OP ^ +#define HASH_CC '^' +#define HASH_C0 4 +#define HASH_C1 3 +#define HASH_C2 1 + /* ** This routine does the work. The generated code is printed on standard ** output. @@ -336,8 +413,8 @@ int main(int argc, char **argv){ int count; int nChar; int totalLen = 0; - int aHash[1000]; /* 1000 is much bigger than nKeyword */ - char zText[2000]; + int aKWHash[1000]; /* 1000 is much bigger than nKeyword */ + char zKWText[2000]; /* Remove entries from the list of keywords that have mask==0 */ for(i=j=0; i<nKeyword; i++){ @@ -356,8 +433,9 @@ int main(int argc, char **argv){ assert( p->len<sizeof(p->zOrigName) ); memcpy(p->zOrigName, p->zName, p->len+1); totalLen += p->len; - p->hash = (charMap(p->zName[0])*4) ^ - (charMap(p->zName[p->len-1])*3) ^ (p->len*1); + p->hash = (charMap(p->zName[0])*HASH_C0) HASH_OP + (charMap(p->zName[p->len-1])*HASH_C1) HASH_OP + (p->len*HASH_C2); p->id = i+1; } @@ -441,13 +519,14 @@ int main(int argc, char **argv){ bestSize = nKeyword; bestCount = nKeyword*nKeyword; for(i=nKeyword/2; i<=2*nKeyword; i++){ - for(j=0; j<i; j++) aHash[j] = 0; + if( i<=0 ) continue; + for(j=0; j<i; j++) aKWHash[j] = 0; for(j=0; j<nKeyword; j++){ h = aKeywordTable[j].hash % i; - aHash[h] *= 2; - aHash[h]++; + aKWHash[h] *= 2; + aKWHash[h]++; } - for(j=count=0; j<i; j++) count += aHash[j]; + for(j=count=0; j<i; j++) count += aKWHash[j]; if( count<bestCount ){ bestCount = count; bestSize = i; @@ -455,30 +534,30 @@ int main(int argc, char **argv){ } /* Compute the hash */ - for(i=0; i<bestSize; i++) aHash[i] = 0; + for(i=0; i<bestSize; i++) aKWHash[i] = 0; for(i=0; i<nKeyword; i++){ h = aKeywordTable[i].hash % bestSize; - aKeywordTable[i].iNext = aHash[h]; - aHash[h] = i+1; + aKeywordTable[i].iNext = aKWHash[h]; + aKWHash[h] = i+1; + reorder(&aKWHash[h]); } /* Begin generating code */ printf("%s", zHdr); printf("/* Hash score: %d */\n", bestCount); - printf("static int keywordCode(const char *z, int n, int *pType){\n"); - printf(" /* zText[] encodes %d bytes of keywords in %d bytes */\n", + printf("/* zKWText[] encodes %d bytes of keyword text in %d bytes */\n", totalLen + nKeyword, nChar+1 ); for(i=j=k=0; i<nKeyword; i++){ Keyword *p = &aKeywordTable[i]; if( p->substrId ) continue; - memcpy(&zText[k], p->zName, p->len); + memcpy(&zKWText[k], p->zName, p->len); k += p->len; if( j+p->len>70 ){ printf("%*s */\n", 74-j, ""); j = 0; } if( j==0 ){ - printf(" /* "); + printf("/* "); j = 8; } printf("%s", p->zName); @@ -487,16 +566,16 @@ int main(int argc, char **argv){ if( j>0 ){ printf("%*s */\n", 74-j, ""); } - printf(" static const char zText[%d] = {\n", nChar); - zText[nChar] = 0; + printf("static const char zKWText[%d] = {\n", nChar); + zKWText[nChar] = 0; for(i=j=0; i<k; i++){ if( j==0 ){ - printf(" "); + printf(" "); } - if( zText[i]==0 ){ + if( zKWText[i]==0 ){ printf("0"); }else{ - printf("'%c',", zText[i]); + printf("'%c',", zKWText[i]); } j += 4; if( j>68 ){ @@ -505,23 +584,27 @@ int main(int argc, char **argv){ } } if( j>0 ) printf("\n"); - printf(" };\n"); + printf("};\n"); - printf(" static const unsigned char aHash[%d] = {\n", bestSize); + printf("/* aKWHash[i] is the hash value for the i-th keyword */\n"); + printf("static const unsigned char aKWHash[%d] = {\n", bestSize); for(i=j=0; i<bestSize; i++){ - if( j==0 ) printf(" "); - printf(" %3d,", aHash[i]); + if( j==0 ) printf(" "); + printf(" %3d,", aKWHash[i]); j++; if( j>12 ){ printf("\n"); j = 0; } } - printf("%s };\n", j==0 ? "" : "\n"); + printf("%s};\n", j==0 ? "" : "\n"); - printf(" static const unsigned char aNext[%d] = {\n", nKeyword); + printf("/* aKWNext[] forms the hash collision chain. If aKWHash[i]==0\n"); + printf("** then the i-th keyword has no more hash collisions. Otherwise,\n"); + printf("** the next keyword with the same hash is aKWHash[i]-1. */\n"); + printf("static const unsigned char aKWNext[%d] = {0,\n", nKeyword+1); for(i=j=0; i<nKeyword; i++){ - if( j==0 ) printf(" "); + if( j==0 ) printf(" "); printf(" %3d,", aKeywordTable[i].iNext); j++; if( j>12 ){ @@ -529,11 +612,12 @@ int main(int argc, char **argv){ j = 0; } } - printf("%s };\n", j==0 ? "" : "\n"); + printf("%s};\n", j==0 ? "" : "\n"); - printf(" static const unsigned char aLen[%d] = {\n", nKeyword); + printf("/* aKWLen[i] is the length (in bytes) of the i-th keyword */\n"); + printf("static const unsigned char aKWLen[%d] = {0,\n", nKeyword+1); for(i=j=0; i<nKeyword; i++){ - if( j==0 ) printf(" "); + if( j==0 ) printf(" "); printf(" %3d,", aKeywordTable[i].len+aKeywordTable[i].prefix); j++; if( j>12 ){ @@ -541,11 +625,13 @@ int main(int argc, char **argv){ j = 0; } } - printf("%s };\n", j==0 ? "" : "\n"); + printf("%s};\n", j==0 ? "" : "\n"); - printf(" static const unsigned short int aOffset[%d] = {\n", nKeyword); + printf("/* aKWOffset[i] is the index into zKWText[] of the start of\n"); + printf("** the text for the i-th keyword. */\n"); + printf("static const unsigned short int aKWOffset[%d] = {0,\n", nKeyword+1); for(i=j=0; i<nKeyword; i++){ - if( j==0 ) printf(" "); + if( j==0 ) printf(" "); printf(" %3d,", aKeywordTable[i].offset); j++; if( j>12 ){ @@ -553,12 +639,13 @@ int main(int argc, char **argv){ j = 0; } } - printf("%s };\n", j==0 ? "" : "\n"); + printf("%s};\n", j==0 ? "" : "\n"); - printf(" static const unsigned char aCode[%d] = {\n", nKeyword); + printf("/* aKWCode[i] is the parser symbol code for the i-th keyword */\n"); + printf("static const unsigned char aKWCode[%d] = {0,\n", nKeyword+1); for(i=j=0; i<nKeyword; i++){ char *zToken = aKeywordTable[i].zTokenType; - if( j==0 ) printf(" "); + if( j==0 ) printf(" "); printf("%s,%*s", zToken, (int)(14-strlen(zToken)), ""); j++; if( j>=5 ){ @@ -566,40 +653,70 @@ int main(int argc, char **argv){ j = 0; } } - printf("%s };\n", j==0 ? "" : "\n"); - + printf("%s};\n", j==0 ? "" : "\n"); + printf("/* Hash table decoded:\n"); + for(i=0; i<bestSize; i++){ + j = aKWHash[i]; + printf("** %3d:", i); + while( j ){ + printf(" %s", aKeywordTable[j-1].zOrigName); + j = aKeywordTable[j-1].iNext; + } + printf("\n"); + } + printf("*/\n"); + printf("/* Check to see if z[0..n-1] is a keyword. If it is, write the\n"); + printf("** parser symbol code for that keyword into *pType. Always\n"); + printf("** return the integer n (the length of the token). */\n"); + printf("static int keywordCode(const char *z, int n, int *pType){\n"); printf(" int i, j;\n"); printf(" const char *zKW;\n"); - printf(" if( n>=2 ){\n"); - printf(" i = ((charMap(z[0])*4) ^ (charMap(z[n-1])*3) ^ n) %% %d;\n", - bestSize); - printf(" for(i=((int)aHash[i])-1; i>=0; i=((int)aNext[i])-1){\n"); - printf(" if( aLen[i]!=n ) continue;\n"); - printf(" j = 0;\n"); - printf(" zKW = &zText[aOffset[i]];\n"); + printf(" assert( n>=2 );\n"); + printf(" i = ((charMap(z[0])*%d) %c", HASH_C0, HASH_CC); + printf(" (charMap(z[n-1])*%d) %c", HASH_C1, HASH_CC); + printf(" n*%d) %% %d;\n", HASH_C2, bestSize); + printf(" for(i=(int)aKWHash[i]; i>0; i=aKWNext[i]){\n"); + printf(" if( aKWLen[i]!=n ) continue;\n"); + printf(" zKW = &zKWText[aKWOffset[i]];\n"); printf("#ifdef SQLITE_ASCII\n"); - printf(" while( j<n && (z[j]&~0x20)==zKW[j] ){ j++; }\n"); + printf(" if( (z[0]&~0x20)!=zKW[0] ) continue;\n"); + printf(" if( (z[1]&~0x20)!=zKW[1] ) continue;\n"); + printf(" j = 2;\n"); + printf(" while( j<n && (z[j]&~0x20)==zKW[j] ){ j++; }\n"); printf("#endif\n"); printf("#ifdef SQLITE_EBCDIC\n"); - printf(" while( j<n && toupper(z[j])==zKW[j] ){ j++; }\n"); + printf(" if( toupper(z[0])!=zKW[0] ) continue;\n"); + printf(" if( toupper(z[1])!=zKW[1] ) continue;\n"); + printf(" j = 2;\n"); + printf(" while( j<n && toupper(z[j])==zKW[j] ){ j++; }\n"); printf("#endif\n"); - printf(" if( j<n ) continue;\n"); + printf(" if( j<n ) continue;\n"); for(i=0; i<nKeyword; i++){ - printf(" testcase( i==%d ); /* %s */\n", - i, aKeywordTable[i].zOrigName); + printf(" testcase( i==%d ); /* %s */\n", + i+1, aKeywordTable[i].zOrigName); } - printf(" *pType = aCode[i];\n"); - printf(" break;\n"); - printf(" }\n"); + printf(" *pType = aKWCode[i];\n"); + printf(" break;\n"); printf(" }\n"); printf(" return n;\n"); printf("}\n"); printf("int sqlite3KeywordCode(const unsigned char *z, int n){\n"); printf(" int id = TK_ID;\n"); - printf(" keywordCode((char*)z, n, &id);\n"); + printf(" if( n>=2 ) keywordCode((char*)z, n, &id);\n"); printf(" return id;\n"); printf("}\n"); printf("#define SQLITE_N_KEYWORD %d\n", nKeyword); + printf("int sqlite3_keyword_name(int i,const char **pzName,int *pnName){\n"); + printf(" if( i<0 || i>=SQLITE_N_KEYWORD ) return SQLITE_ERROR;\n"); + printf(" i++;\n"); + printf(" *pzName = zKWText + aKWOffset[i];\n"); + printf(" *pnName = aKWLen[i];\n"); + printf(" return SQLITE_OK;\n"); + printf("}\n"); + printf("int sqlite3_keyword_count(void){ return SQLITE_N_KEYWORD; }\n"); + printf("int sqlite3_keyword_check(const char *zName, int nName){\n"); + printf(" return TK_ID!=sqlite3KeywordCode((const u8*)zName, nName);\n"); + printf("}\n"); return 0; } diff --git a/tool/mkmsvcmin.tcl b/tool/mkmsvcmin.tcl index da0cd6283c..7926952554 100644 --- a/tool/mkmsvcmin.tcl +++ b/tool/mkmsvcmin.tcl @@ -23,7 +23,7 @@ if {$argc==0} { proc readFile { fileName } { set file_id [open $fileName RDONLY] - fconfigure $file_id -encoding binary -translation binary + fconfigure $file_id -translation binary set result [read $file_id] close $file_id return $result @@ -31,7 +31,7 @@ proc readFile { fileName } { proc writeFile { fileName data } { set file_id [open $fileName {WRONLY CREAT TRUNC}] - fconfigure $file_id -encoding binary -translation binary + fconfigure $file_id -translation binary puts -nonewline $file_id $data close $file_id return "" @@ -54,7 +54,7 @@ proc substVars { data } { set blocks(1) [string trimleft [string map [list \\\\ \\] { _HASHCHAR=^# !IF ![echo !IFNDEF VERSION > rcver.vc] && \\ - ![for /F "delims=" %V in ('type "$(SQLITE3H)" ^| find "$(_HASHCHAR)define SQLITE_VERSION "') do (echo VERSION = ^^%V >> rcver.vc)] && \\ + ![for /F "delims=" %V in ('type "$(SQLITE3H)" ^| "%SystemRoot%\System32\find.exe" "$(_HASHCHAR)define SQLITE_VERSION "') do (echo VERSION = ^^%V >> rcver.vc)] && \\ ![echo !ENDIF >> rcver.vc] !INCLUDE rcver.vc !ENDIF @@ -72,6 +72,21 @@ $(LIBRESOBJS): $(TOP)\sqlite3.rc rcver.vc $(SQLITE3H) $(LTRCOMPILE) -fo $(LIBRESOBJS) -DRC_VERONLY $(TOP)\sqlite3.rc }]] +# +# NOTE: This block is used to replace the section marked <<block2>> in +# the Makefile, if it exists. +# +set blocks(2) [string trimleft [string map [list \\\\ \\] { +Replace.exe: + $(CSC) /target:exe $(TOP)\Replace.cs + +sqlite3.def: Replace.exe $(LIBOBJ) + echo EXPORTS > sqlite3.def + dumpbin /all $(LIBOBJ) \\ + | .\Replace.exe "^\s+/EXPORT:_?(sqlite3(?:session|changeset|changegroup|rebaser|rbu)?_[^@,]*)(?:@\d+|,DATA)?$$" $$1 true \\ + | sort >> sqlite3.def +}]] + set data "#### DO NOT EDIT ####\n" append data "# This makefile is automatically " append data "generated from the [file tail $fromFileName] at\n" @@ -90,9 +105,9 @@ foreach i [lsort -integer [array names blocks]] { } set data [string map [list " -I\$(TOP)\\src" ""] $data] -set data [string map [list " /DEF:sqlite3.def" ""] $data] -set data [string map [list " sqlite3.def" ""] $data] +set data [string map [list " libsqlite3.lib" ""] $data] set data [string map [list " \$(ALL_TCL_TARGETS)" ""] $data] set data [string map [list "\$(TOP)\\src\\" "\$(TOP)\\"] $data] writeFile $toFileName $data +puts "generated $toFileName from $fromFileName" diff --git a/tool/mkopcodec.tcl b/tool/mkopcodec.tcl index abdeaaeb32..5eac05fc02 100644 --- a/tool/mkopcodec.tcl +++ b/tool/mkopcodec.tcl @@ -22,7 +22,8 @@ puts "const char *sqlite3OpcodeName(int i)\173" puts " static const char *const azName\[\] = \173" set mx 0 -set in [open [lindex $argv 0] rb] +set in [open [lindex $argv 0]] +fconfigure $in -translation binary while {![eof $in]} { set line [gets $in] if {[regexp {^#define OP_} $line]} { diff --git a/tool/mkopcodeh.tcl b/tool/mkopcodeh.tcl index 053c7f8984..18fe1a2658 100644 --- a/tool/mkopcodeh.tcl +++ b/tool/mkopcodeh.tcl @@ -20,12 +20,11 @@ # during code generation, we need to generate corresponding opcodes like # OP_Add and OP_Divide. By making TK_ADD==OP_Add and TK_DIVIDE==OP_Divide, # code to translate from one to the other is avoided. This makes the -# code generator run (infinitesimally) faster and more importantly it makes -# the library footprint smaller. +# code generator smaller and faster. # # This script also scans for lines of the form: # -# case OP_aaaa: /* jump, in1, in2, in3, out2-prerelease, out3 */ +# case OP_aaaa: /* jump, in1, in2, in3, out2, out3 */ # # When such comments are found on an opcode, it means that certain # properties apply to that opcode. Set corresponding flags using the @@ -34,7 +33,9 @@ set in stdin set currentOp {} +set prevName {} set nOp 0 +set nGroup 0 while {![eof $in]} { set line [gets $in] @@ -76,13 +77,17 @@ while {![eof $in]} { if {[regexp {^case OP_} $line]} { set line [split $line] set name [string trim [lindex $line 1] :] + if {$name=="OP_Abortable"} continue; # put OP_Abortable last set op($name) -1 + set group($name) 0 set jump($name) 0 + set jump0($name) 0 set in1($name) 0 set in2($name) 0 set in3($name) 0 set out2($name) 0 set out3($name) 0 + set ncycle($name) 0 for {set i 3} {$i<[llength $line]-1} {incr i} { switch [string trim [lindex $line $i] ,] { same { @@ -97,15 +102,33 @@ while {![eof $in]} { set def($val) $name } } - jump {set jump($name) 1} - in1 {set in1($name) 1} - in2 {set in2($name) 1} - in3 {set in3($name) 1} - out2 {set out2($name) 1} - out3 {set out3($name) 1} + group {set group($name) 1} + jump {set jump($name) 1} + in1 {set in1($name) 1} + in2 {set in2($name) 1} + in3 {set in3($name) 1} + out2 {set out2($name) 1} + out3 {set out3($name) 1} + ncycle {set ncycle($name) 1} + jump0 {set jump($name) 1; set jump0($name) 1;} } } + if {$group($name)} { + set newGroup 0 + if {[info exists groups($nGroup)]} { + if {$prevName=="" || !$group($prevName)} { + set newGroup 1 + } + } + lappend groups($nGroup) $name + if {$newGroup} {incr nGroup} + } else { + if {$prevName!="" && $group($prevName)} { + incr nGroup + } + } set order($nOp) $name + set prevName $name incr nOp } } @@ -114,19 +137,23 @@ while {![eof $in]} { # puts "/* Automatically generated. Do not edit */" puts "/* See the tool/mkopcodeh.tcl script for details */" -foreach name {OP_Noop OP_Explain} { +foreach name {OP_Noop OP_Explain OP_Abortable} { set jump($name) 0 + set jump0($name) 0 set in1($name) 0 set in2($name) 0 set in3($name) 0 set out2($name) 0 set out3($name) 0 + set ncycle($name) 0 set op($name) -1 set order($nOp) $name incr nOp } -# The following are the opcodes that are processed by resolveP2Values() +# The following are the opcodes that receive special processing in the +# resolveP2Values() routine. Update this list whenever new cases are +# added to the pOp->opcode switch within resolveP2Values(). # set rp2v_ops { OP_Transaction @@ -137,14 +164,10 @@ set rp2v_ops { OP_JournalMode OP_VUpdate OP_VFilter - OP_Next - OP_NextIfOpen - OP_SorterNext - OP_Prev - OP_PrevIfOpen + OP_Init } -# Assign small values to opcodes that are processed by resolveP2Values() +# Assign the smallest values to opcodes that are processed by resolveP2Values() # to make code generation for the switch() statement smaller and faster. # set cnt -1 @@ -158,9 +181,64 @@ for {set i 0} {$i<$nOp} {incr i} { set def($cnt) $name } } +set mxCase1 $cnt + +# Assign the next group of values to JUMP opcodes +# +for {set i 0} {$i<$nOp} {incr i} { + set name $order($i) + if {$op($name)>=0} continue + if {!$jump($name)} continue + incr cnt + while {[info exists used($cnt)]} {incr cnt} + set op($name) $cnt + set used($cnt) 1 + set def($cnt) $name +} -# Generate the numeric values for remaining opcodes +# Find the numeric value for the largest JUMP opcode # +set mxJump -1 +for {set i 0} {$i<$nOp} {incr i} { + set name $order($i) + if {$jump($name) && $op($name)>$mxJump} {set mxJump $op($name)} +} + + +# Generate the numeric values for all remaining opcodes, while +# preserving any groupings of opcodes (i.e. those that must be +# together). +# +for {set g 0} {$g<$nGroup} {incr g} { + set gLen [llength $groups($g)] + set ok 0; set start -1 + set seek $cnt + while {!$ok} { + incr seek + while {[info exists used($seek)]} {incr seek} + set ok 1; set start $seek + for {set j 0} {$j<$gLen} {incr j} { + incr seek + if {[info exists used($seek)]} { + set ok 0; break + } + } + } + if {$ok} { + set next $start + for {set j 0} {$j<$gLen} {incr j} { + set name [lindex $groups($g) $j] + if {$op($name)>=0} continue + set op($name) $next + set used($next) 1 + set def($next) $name + incr next + } + } else { + error "cannot find opcodes for group: $groups($g)" + } +} + for {set i 0} {$i<$nOp} {incr i} { set name $order($i) if {$op($name)<0} { @@ -171,43 +249,53 @@ for {set i 0} {$i<$nOp} {incr i} { set def($cnt) $name } } -set max $cnt -for {set i 0} {$i<$nOp} {incr i} { + +set max [lindex [lsort -decr -integer [array names used]] 0] +for {set i 0} {$i<=$max} {incr i} { if {![info exists used($i)]} { set def($i) "OP_NotUsed_$i" } + if {$i>$max} {set max $i} set name $def($i) puts -nonewline [format {#define %-16s %3d} $name $i] set com {} + if {[info exists jump0($name)] && $jump0($name)} { + lappend com "jump0" + } elseif {[info exists jump($name)] && $jump($name)} { + lappend com "jump" + } if {[info exists sameas($i)]} { - set com "same as $sameas($i)" + lappend com "same as $sameas($i)" } if {[info exists synopsis($name)]} { - set x $synopsis($name) - if {$com==""} { - set com "synopsis: $x" - } else { - append com ", synopsis: $x" - } + lappend com "synopsis: $synopsis($name)" } - if {$com!=""} { - puts -nonewline [format " /* %-42s */" $com] + if {[llength $com]} { + puts -nonewline [format " /* %-42s */" [join $com {, }]] } puts "" } +if {$max>255} { + error "More than 255 opcodes - VdbeOp.opcode is of type u8!" +} + # Generate the bitvectors: # set bv(0) 0 for {set i 0} {$i<=$max} {incr i} { - set name $def($i) set x 0 - if {$jump($name)} {incr x 1} - if {$in1($name)} {incr x 2} - if {$in2($name)} {incr x 4} - if {$in3($name)} {incr x 8} - if {$out2($name)} {incr x 16} - if {$out3($name)} {incr x 32} + set name $def($i) + if {[string match OP_NotUsed* $name]==0} { + if {$jump($name)} {incr x 1} + if {$in1($name)} {incr x 2} + if {$in2($name)} {incr x 4} + if {$in3($name)} {incr x 8} + if {$out2($name)} {incr x 16} + if {$out3($name)} {incr x 32} + if {$ncycle($name)} {incr x 64} + if {$jump0($name)} {incr x 128} + } set bv($i) $x } puts "" @@ -221,6 +309,8 @@ puts "#define OPFLG_IN2 0x04 /* in2: P2 is an input */" puts "#define OPFLG_IN3 0x08 /* in3: P3 is an input */" puts "#define OPFLG_OUT2 0x10 /* out2: P2 is an output */" puts "#define OPFLG_OUT3 0x20 /* out3: P3 is an output */" +puts "#define OPFLG_NCYCLE 0x40 /* ncycle:Cycles count against P1 */" +puts "#define OPFLG_JUMP0 0x80 /* jump0: P2 might be zero */" puts "#define OPFLG_INITIALIZER \173\\" for {set i 0} {$i<=$max} {incr i} { if {$i%8==0} { @@ -232,3 +322,11 @@ for {set i 0} {$i<=$max} {incr i} { } } puts "\175" +puts "" +puts "/* The resolve3P2Values() routine is able to run faster if it knows" +puts "** the value of the largest JUMP opcode. The smaller the maximum" +puts "** JUMP opcode the better, so the mkopcodeh.tcl script that" +puts "** generated this include file strives to group all JUMP opcodes" +puts "** together near the beginning of the list." +puts "*/" +puts "#define SQLITE_MX_JUMP_OPCODE $mxJump /* Maximum JUMP opcode */" diff --git a/tool/mkopts.tcl b/tool/mkopts.tcl index e3ddcb9eeb..88f645bbe3 100644 --- a/tool/mkopts.tcl +++ b/tool/mkopts.tcl @@ -11,7 +11,7 @@ while {![eof stdin]} { if {$line==""} continue regsub -all "\[ \t\n,\]+" [string trim $line] { } line foreach token [split $line { }] { - if {![regexp {(([a-zA-Z]+)_)?([_a-zA-Z]+)} $token all px p2 name]} continue + if {![regexp {(([a-zA-Z]+)_)?([_a-zA-Z0-9]+)} $token all px p2 name]} continue lappend namelist [string tolower $name] if {$px!=""} {set prefix $p2} } @@ -23,7 +23,7 @@ proc put_item x { global col if {$col==0} {puts -nonewline " "} if {$col<2} { - puts -nonewline [format " %-21s" $x] + puts -nonewline [format " %-25s" $x] incr col } else { puts $x diff --git a/tool/mkpragmatab.tcl b/tool/mkpragmatab.tcl index 145a365c54..b55e5eba0f 100644 --- a/tool/mkpragmatab.tcl +++ b/tool/mkpragmatab.tcl @@ -4,11 +4,29 @@ # # To add new pragmas, first add the name and other relevant attributes # of the pragma to the "pragma_def" object below. Then run this script -# to generate the ../src/pragma.h header file that contains macros and +# to generate the pragma.h header file that contains macros and # the lookup table needed for pragma name lookup in the pragma.c module. # Then add the extra "case PragTyp_XXXXX:" and subsequent code for the # new pragma in ../src/pragma.c. # +# The results are normally written into the pragma.h file. However, +# if an alternative output file name is provided as an argument, then +# results are written into the alternative. For example: +# +# tclsh tool/mkpragmatab.tcl ;# <--- Results to pragma.h +# +# tclsh tool/mkpragmatab.tcl /dev/tty ;# <-- results to terminal +# + +# Flag meanings: +set flagMeaning(NeedSchema) {Force schema load before running} +set flagMeaning(ReadOnly) {Read-only HEADER_VALUE} +set flagMeaning(Result0) {Acts as query when no argument} +set flagMeaning(Result1) {Acts as query when has one argument} +set flagMeaning(SchemaReq) {Schema required - "main" is default} +set flagMeaning(SchemaOpt) {Schema restricts name search if present} +set flagMeaning(NoColumns) {OP_ResultRow called with zero columns} +set flagMeaning(NoColumns1) {zero columns if RHS argument is present} set pragma_def { NAME: full_column_names @@ -31,11 +49,6 @@ set pragma_def { ARG: SQLITE_NullCallback IF: !defined(SQLITE_OMIT_FLAG_PRAGMAS) - NAME: legacy_file_format - TYPE: FLAG - ARG: SQLITE_LegacyFileFmt - IF: !defined(SQLITE_OMIT_FLAG_PRAGMAS) - NAME: fullfsync TYPE: FLAG ARG: SQLITE_FullFSync @@ -47,6 +60,7 @@ set pragma_def { IF: !defined(SQLITE_OMIT_FLAG_PRAGMAS) NAME: cache_spill + FLAG: Result0 SchemaReq NoColumns1 IF: !defined(SQLITE_OMIT_FLAG_PRAGMAS) NAME: reverse_unordered_selects @@ -109,12 +123,12 @@ set pragma_def { NAME: writable_schema TYPE: FLAG - ARG: SQLITE_WriteSchema|SQLITE_RecoveryMode + ARG: SQLITE_WriteSchema|SQLITE_NoSchemaError IF: !defined(SQLITE_OMIT_FLAG_PRAGMAS) NAME: read_uncommitted TYPE: FLAG - ARG: SQLITE_ReadUncommitted + ARG: SQLITE_ReadUncommit IF: !defined(SQLITE_OMIT_FLAG_PRAGMAS) NAME: recursive_triggers @@ -122,6 +136,11 @@ set pragma_def { ARG: SQLITE_RecTriggers IF: !defined(SQLITE_OMIT_FLAG_PRAGMAS) + NAME: trusted_schema + TYPE: FLAG + ARG: SQLITE_TrustedSchema + IF: !defined(SQLITE_OMIT_FLAG_PRAGMAS) + NAME: foreign_keys TYPE: FLAG ARG: SQLITE_ForeignKeys @@ -139,190 +158,294 @@ set pragma_def { ARG: SQLITE_CellSizeCk NAME: default_cache_size - FLAG: NeedSchema + FLAG: NeedSchema Result0 SchemaReq NoColumns1 + COLS: cache_size IF: !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED) NAME: page_size + FLAG: Result0 SchemaReq NoColumns1 IF: !defined(SQLITE_OMIT_PAGER_PRAGMAS) NAME: secure_delete + FLAG: Result0 IF: !defined(SQLITE_OMIT_PAGER_PRAGMAS) NAME: page_count - FLAG: NeedSchema + FLAG: NeedSchema Result0 SchemaReq IF: !defined(SQLITE_OMIT_PAGER_PRAGMAS) NAME: max_page_count TYPE: PAGE_COUNT - FLAG: NeedSchema + FLAG: NeedSchema Result0 SchemaReq IF: !defined(SQLITE_OMIT_PAGER_PRAGMAS) NAME: locking_mode + FLAG: Result0 SchemaReq IF: !defined(SQLITE_OMIT_PAGER_PRAGMAS) NAME: journal_mode - FLAG: NeedSchema + FLAG: NeedSchema Result0 SchemaReq IF: !defined(SQLITE_OMIT_PAGER_PRAGMAS) NAME: journal_size_limit + FLAG: Result0 SchemaReq IF: !defined(SQLITE_OMIT_PAGER_PRAGMAS) NAME: cache_size - FLAG: NeedSchema + FLAG: NeedSchema Result0 SchemaReq NoColumns1 IF: !defined(SQLITE_OMIT_PAGER_PRAGMAS) NAME: mmap_size IF: !defined(SQLITE_OMIT_PAGER_PRAGMAS) NAME: auto_vacuum - FLAG: NeedSchema + FLAG: NeedSchema Result0 SchemaReq NoColumns1 IF: !defined(SQLITE_OMIT_AUTOVACUUM) NAME: incremental_vacuum - FLAG: NeedSchema + FLAG: NeedSchema NoColumns IF: !defined(SQLITE_OMIT_AUTOVACUUM) NAME: temp_store + FLAG: Result0 NoColumns1 IF: !defined(SQLITE_OMIT_PAGER_PRAGMAS) NAME: temp_store_directory + FLAG: NoColumns1 IF: !defined(SQLITE_OMIT_PAGER_PRAGMAS) NAME: data_store_directory + FLAG: NoColumns1 IF: !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_OS_WIN NAME: lock_proxy_file + FLAG: NoColumns1 IF: !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_ENABLE_LOCKING_STYLE NAME: synchronous - FLAG: NeedSchema + FLAG: NeedSchema Result0 SchemaReq NoColumns1 IF: !defined(SQLITE_OMIT_PAGER_PRAGMAS) NAME: table_info - FLAG: NeedSchema + FLAG: NeedSchema Result1 SchemaOpt + ARG: 0 + COLS: cid name type notnull dflt_value pk IF: !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) - NAME: stats - FLAG: NeedSchema + NAME: table_xinfo + TYPE: TABLE_INFO + FLAG: NeedSchema Result1 SchemaOpt + ARG: 1 + COLS: cid name type notnull dflt_value pk hidden IF: !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) + NAME: table_list + TYPE: TABLE_LIST + FLAG: NeedSchema Result1 + COLS: schema name type ncol wr strict + IF: !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) + + NAME: stats + FLAG: NeedSchema Result0 SchemaReq + COLS: tbl idx wdth hght flgs + IF: !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) && defined(SQLITE_DEBUG) + NAME: index_info TYPE: INDEX_INFO ARG: 0 - FLAG: NeedSchema + FLAG: NeedSchema Result1 SchemaOpt + COLS: seqno cid name IF: !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) NAME: index_xinfo TYPE: INDEX_INFO ARG: 1 - FLAG: NeedSchema + FLAG: NeedSchema Result1 SchemaOpt + COLS: seqno cid name desc coll key IF: !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) NAME: index_list - FLAG: NeedSchema + FLAG: NeedSchema Result1 SchemaOpt + COLS: seq name unique origin partial IF: !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) NAME: database_list - FLAG: NeedSchema + FLAG: Result0 + COLS: seq name file IF: !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) + NAME: function_list + FLAG: Result0 + COLS: name builtin type enc narg flags + IF: !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) + IF: !defined(SQLITE_OMIT_INTROSPECTION_PRAGMAS) + + NAME: module_list + FLAG: Result0 + COLS: name + IF: !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) + IF: !defined(SQLITE_OMIT_VIRTUALTABLE) + IF: !defined(SQLITE_OMIT_INTROSPECTION_PRAGMAS) + + NAME: pragma_list + FLAG: Result0 + COLS: name + IF: !defined(SQLITE_OMIT_INTROSPECTION_PRAGMAS) + NAME: collation_list + FLAG: Result0 + COLS: seq name IF: !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) NAME: foreign_key_list - FLAG: NeedSchema + FLAG: NeedSchema Result1 SchemaOpt + COLS: id seq table from to on_update on_delete match IF: !defined(SQLITE_OMIT_FOREIGN_KEY) NAME: foreign_key_check - FLAG: NeedSchema + FLAG: NeedSchema Result0 Result1 SchemaOpt + COLS: table rowid parent fkid IF: !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER) NAME: parser_trace - IF: defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_PARSER_TRACE) + TYPE: FLAG + ARG: SQLITE_ParserTrace + IF: !defined(SQLITE_OMIT_FLAG_PRAGMAS) + IF: defined(SQLITE_DEBUG) NAME: case_sensitive_like + FLAG: NoColumns + IF: !defined(SQLITE_OMIT_CASE_SENSITIVE_LIKE_PRAGMA) NAME: integrity_check - FLAG: NeedSchema + FLAG: NeedSchema Result0 Result1 SchemaOpt IF: !defined(SQLITE_OMIT_INTEGRITY_CHECK) NAME: quick_check TYPE: INTEGRITY_CHECK - FLAG: NeedSchema + FLAG: NeedSchema Result0 Result1 SchemaOpt IF: !defined(SQLITE_OMIT_INTEGRITY_CHECK) NAME: encoding + FLAG: Result0 NoColumns1 IF: !defined(SQLITE_OMIT_UTF16) NAME: schema_version TYPE: HEADER_VALUE ARG: BTREE_SCHEMA_VERSION + FLAG: NoColumns1 Result0 IF: !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS) NAME: user_version TYPE: HEADER_VALUE ARG: BTREE_USER_VERSION + FLAG: NoColumns1 Result0 IF: !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS) NAME: data_version TYPE: HEADER_VALUE ARG: BTREE_DATA_VERSION - FLAG: ReadOnly + FLAG: ReadOnly Result0 IF: !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS) NAME: freelist_count TYPE: HEADER_VALUE ARG: BTREE_FREE_PAGE_COUNT - FLAG: ReadOnly + FLAG: ReadOnly Result0 IF: !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS) NAME: application_id TYPE: HEADER_VALUE ARG: BTREE_APPLICATION_ID + FLAG: NoColumns1 Result0 IF: !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS) NAME: compile_options + FLAG: Result0 IF: !defined(SQLITE_OMIT_COMPILEOPTION_DIAGS) NAME: wal_checkpoint FLAG: NeedSchema + COLS: busy log checkpointed IF: !defined(SQLITE_OMIT_WAL) NAME: wal_autocheckpoint IF: !defined(SQLITE_OMIT_WAL) NAME: shrink_memory + FLAG: NoColumns NAME: busy_timeout + FLAG: Result0 + COLS: timeout NAME: lock_status + FLAG: Result0 + COLS: database status IF: defined(SQLITE_DEBUG) || defined(SQLITE_TEST) NAME: key + TYPE: KEY + ARG: 0 IF: defined(SQLITE_HAS_CODEC) NAME: rekey + TYPE: KEY + ARG: 1 IF: defined(SQLITE_HAS_CODEC) NAME: hexkey + TYPE: KEY + ARG: 2 IF: defined(SQLITE_HAS_CODEC) NAME: hexrekey - TYPE: HEXKEY + TYPE: KEY + ARG: 3 + IF: defined(SQLITE_HAS_CODEC) + + NAME: textkey + TYPE: KEY + ARG: 4 + IF: defined(SQLITE_HAS_CODEC) + + NAME: textrekey + TYPE: KEY + ARG: 5 IF: defined(SQLITE_HAS_CODEC) NAME: activate_extensions - IF: defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD) + IF: defined(SQLITE_ENABLE_CEROD) NAME: soft_heap_limit + FLAG: Result0 + + NAME: hard_heap_limit + FLAG: Result0 NAME: threads + FLAG: Result0 + + NAME: analysis_limit + FLAG: Result0 + + NAME: optimize + FLAG: Result1 NeedSchema + + NAME: legacy_alter_table + TYPE: FLAG + ARG: SQLITE_LegacyAlter + IF: !defined(SQLITE_OMIT_FLAG_PRAGMAS) } # Open the output file # -set destfile "[file dir [file dir [file normal $argv0]]]/src/pragma.h" -puts "Overwriting $destfile with new pragma table..." +if {$argc>0} { + set destfile [lindex $argv 0] +} else { + set destfile "pragma.h" + puts "Overwriting $destfile with new pragma table..." +} set fd [open $destfile wb] puts $fd {/* DO NOT EDIT! ** This file is automatically generated by the script at @@ -336,15 +459,29 @@ set name {} set type {} set if {} set flags {} +set cols {} +set cols_list {} set arg 0 proc record_one {} { - global name type if arg allbyname typebyif flags + global name type if arg allbyname typebyif flags cols all_cols + global cols_list colUsedBy if {$name==""} return - set allbyname($name) [list $type $arg $if $flags] + if {$cols!=""} { + if {![info exists all_cols($cols)]} { + set all_cols($cols) 1 + lappend cols_list $cols + } + set cx $cols + lappend colUsedBy($cols) $name + } else { + set cx 0 + } + set allbyname($name) [list $type $arg $if $flags $cols] set name {} set type {} set if {} set flags {} + set cols {} set arg 0 } foreach line [split $pragma_def \n] { @@ -358,8 +495,13 @@ foreach line [split $pragma_def \n] { set type [string toupper $val] } elseif {$id=="TYPE"} { set type $val + if {$type=="FLAG"} { + lappend flags Result0 NoColumns1 + } } elseif {$id=="ARG"} { set arg $val + } elseif {$id=="COLS"} { + set cols $val } elseif {$id=="IF"} { lappend if $val } elseif {$id=="FLAG"} { @@ -375,9 +517,10 @@ record_one set allnames [lsort [array names allbyname]] # Generate #defines for all pragma type names. Group the pragmas that are -# omit in default builds (defined(SQLITE_DEBUG) and defined(SQLITE_HAS_CODEC)) +# omit in default builds (ex: defined(SQLITE_DEBUG)) # at the end. # +puts $fd "\n/* The various pragma types */" set pnum 0 foreach name $allnames { set type [lindex $allbyname($name) 0] @@ -407,25 +550,81 @@ foreach name $allnames { # Generate #defines for flags # +puts $fd "\n/* Property flags associated with various pragma. */" set fv 1 foreach f [lsort [array names allflags]] { - puts $fd [format {#define PragFlag_%-20s 0x%02x} $f $fv] + puts $fd [format {#define PragFlg_%-10s 0x%02x /* %s */} \ + $f $fv $flagMeaning($f)] set fv [expr {$fv*2}] } +# Sort the column lists so that longer column lists occur first. +# In the event of a tie, sort column lists lexicographically. +# +proc colscmp {a b} { + set rc [expr {[llength $b] - [llength $a]}] + if {$rc} {return $rc} + return [string compare $a $b] +} +set cols_list [lsort -command colscmp $cols_list] + +# Generate the array of column names used by pragmas that act like +# queries. +# +puts $fd "\n/* Names of columns for pragmas that return multi-column result" +puts $fd "** or that return single-column results where the name of the" +puts $fd "** result column is different from the name of the pragma\n*/" +puts $fd "static const char *const pragCName\[\] = {" +set offset 0 +set allcollist {} +foreach cols $cols_list { + set n [llength $cols] + set limit [expr {[llength $allcollist] - $n}] + for {set i 0} {$i<$limit} {incr i} { + set sublist [lrange $allcollist $i [expr {$i+$n-1}]] + if {$sublist==$cols} { + puts $fd [format "%27s/* $colUsedBy($cols) reuses $i */" ""] + set cols_offset($cols) $i + break + } + } + if {$i<$limit} continue + set cols_offset($cols) $offset + set ub " /* Used by: $colUsedBy($cols) */" + foreach c $cols { + lappend allcollist $c + puts $fd [format " /* %3d */ %-14s%s" $offset \"$c\", $ub] + set ub "" + incr offset + } +} +puts $fd "\175;" + # Generate the lookup table # -puts $fd "static const struct sPragmaNames \173" -puts $fd " const char *const zName; /* Name of pragma */" -puts $fd " u8 ePragTyp; /* PragTyp_XXX value */" -puts $fd " u8 mPragFlag; /* Zero or more PragFlag_XXX values */" -puts $fd " u32 iArg; /* Extra argument */" -puts $fd "\175 aPragmaNames\[\] = \173" +puts $fd "\n/* Definitions of all built-in pragmas */" +puts $fd "typedef struct PragmaName \173" +puts $fd " const char *const zName; /* Name of pragma */" +puts $fd " u8 ePragTyp; /* PragTyp_XXX value */" +puts $fd " u8 mPragFlg; /* Zero or more PragFlg_XXX values */" +puts $fd { u8 iPragCName; /* Start of column names in pragCName[] */} +puts $fd " u8 nPragCName; \ +/* Num of col names. 0 means use pragma name */" +puts $fd " u64 iArg; /* Extra argument */" +puts $fd "\175 PragmaName;" +puts $fd "static const PragmaName aPragmaName\[\] = \173" set current_if {} set spacer [format { %26s } {}] foreach name $allnames { - foreach {type arg if flag} $allbyname($name) break + foreach {type arg if flag cx} $allbyname($name) break + if {$cx==0 || $cx==""} { + set cy 0 + set nx 0 + } else { + set cy $cols_offset($cx) + set nx [llength $cx] + } if {$if!=$current_if} { if {$current_if!=""} { foreach this_if $current_if { @@ -443,12 +642,13 @@ foreach name $allnames { if {$flag==""} { set flagx "0" } else { - set flagx PragFlag_[join $flag {|PragFlag_}] + set flagx PragFlg_[join $flag {|PragFlg_}] } - puts $fd " \173 /* zName: */ \"$name\"," - puts $fd " /* ePragTyp: */ PragTyp_$type," - puts $fd " /* ePragFlag: */ $flagx," - puts $fd " /* iArg: */ $arg \175," + puts $fd " \173/* zName: */ \"$name\"," + puts $fd " /* ePragTyp: */ PragTyp_$type," + puts $fd " /* ePragFlg: */ $flagx," + puts $fd " /* ColNames: */ $cy, $nx," + puts $fd " /* iArg: */ $arg \175," } if {$current_if!=""} { foreach this_if $current_if { diff --git a/tool/mkshellc.tcl b/tool/mkshellc.tcl new file mode 100644 index 0000000000..2f7a6ea256 --- /dev/null +++ b/tool/mkshellc.tcl @@ -0,0 +1,79 @@ +#!/usr/bin/tclsh +# +# Run this script to generate the "shell.c" source file from +# constituent parts. +# +# No arguments are required. This script determines the location +# of its input files relative to the location of the script itself. +# This script should be tool/mkshellc.tcl. If the directory holding +# the script is $DIR, then the component parts are located in $DIR/../src +# and $DIR/../ext/misc. +# +set topdir [file dir [file dir [file normal $argv0]]] +set out stdout +fconfigure stdout -translation binary +if {[lindex $argv 0]!=""} { + set out [open [lindex $argv 0] wb] +} +puts $out {/* DO NOT EDIT! +** This file is automatically generated by the script in the canonical +** SQLite source tree at tool/mkshellc.tcl. That script combines source +** code from various constituent source files of SQLite into this single +** "shell.c" file used to implement the SQLite command-line shell. +** +** Most of the code found below comes from the "src/shell.c.in" file in +** the canonical SQLite source tree. That main file contains "INCLUDE" +** lines that specify other files in the canonical source tree that are +** inserted to getnerate this complete program source file. +** +** The code from multiple files is combined into this single "shell.c" +** source file to help make the command-line program easier to compile. +** +** To modify this program, get a copy of the canonical SQLite source tree, +** edit the src/shell.c.in" and/or some of the other files that are included +** by "src/shell.c.in", then rerun the tool/mkshellc.tcl script. +*/} +set in [open $topdir/src/shell.c.in] +fconfigure $in -translation binary +proc omit_redundant_typedefs {line} { + global typedef_seen + if {[regexp {^typedef .* ([a-zA-Z0-9_]+);} $line all typename]} { + # --------------------\y jimtcl does not support \y + if {[info exists typedef_seen($typename)]} { + return "/* [string map {/* // */ //} $line] */" + } + set typedef_seen($typename) 1 + } + return $line +} +set iLine 0 +while {1} { + set lx [omit_redundant_typedefs [gets $in]] + if {[eof $in]} break; + incr iLine + if {[regexp {^INCLUDE } $lx]} { + set cfile [lindex $lx 1] + puts $out "/************************* Begin $cfile ******************/" +# puts $out "#line 1 \"$cfile\"" + set in2 [open $topdir/src/$cfile] + fconfigure $in2 -translation binary + while {![eof $in2]} { + set lx [omit_redundant_typedefs [gets $in2]] + if {[regexp {^# *include "sqlite} $lx]} { + set lx "/* $lx */" + } + if {[regexp {^# *include "windirent.h"} $lx]} { + set lx "/* $lx */" + } + set lx [string map [list __declspec(dllexport) {}] $lx] + puts $out $lx + } + close $in2 + puts $out "/************************* End $cfile ********************/" +# puts $out "#line [expr $iLine+1] \"shell.c.in\"" + continue + } + puts $out $lx +} +close $in +close $out diff --git a/tool/mksourceid.c b/tool/mksourceid.c new file mode 100644 index 0000000000..dd153997df --- /dev/null +++ b/tool/mksourceid.c @@ -0,0 +1,835 @@ +/* +** Run this program with a single argument which is the name of the +** Fossil "manifest" file for a project, and this program will emit on +** standard output the "source id" for for the program. +** +** (1) The "source id" is the date of check-in together with the +** SHA3 hash of the manifest file. +** +** (2) All individual file hashes in the manifest are verified. If any +** source file has changed, the SHA3 hash ends with "modified". +** +*/ +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <sys/types.h> +#include <ctype.h> + +/* Portable 64-bit unsigned integers */ +#if defined(_MSC_VER) || defined(__BORLANDC__) + typedef unsigned __int64 u64; +#else + typedef unsigned long long int u64; +#endif + + +/* +** Macros to determine whether the machine is big or little endian, +** and whether or not that determination is run-time or compile-time. +** +** For best performance, an attempt is made to guess at the byte-order +** using C-preprocessor macros. If that is unsuccessful, or if +** -DBYTEORDER=0 is set, then byte-order is determined +** at run-time. +*/ +#ifndef BYTEORDER +# if defined(i386) || defined(__i386__) || defined(_M_IX86) || \ + defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \ + defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \ + defined(__arm__) +# define BYTEORDER 1234 +# elif defined(sparc) || defined(__ppc__) +# define BYTEORDER 4321 +# else +# define BYTEORDER 0 +# endif +#endif + + + +/* +** State structure for a SHA3 hash in progress +*/ +typedef struct SHA3Context SHA3Context; +struct SHA3Context { + union { + u64 s[25]; /* Keccak state. 5x5 lines of 64 bits each */ + unsigned char x[1600]; /* ... or 1600 bytes */ + } u; + unsigned nRate; /* Bytes of input accepted per Keccak iteration */ + unsigned nLoaded; /* Input bytes loaded into u.x[] so far this cycle */ + unsigned ixMask; /* Insert next input into u.x[nLoaded^ixMask]. */ +}; + +/* +** A single step of the Keccak mixing function for a 1600-bit state +*/ +static void KeccakF1600Step(SHA3Context *p){ + int i; + u64 B0, B1, B2, B3, B4; + u64 C0, C1, C2, C3, C4; + u64 D0, D1, D2, D3, D4; + static const u64 RC[] = { + 0x0000000000000001ULL, 0x0000000000008082ULL, + 0x800000000000808aULL, 0x8000000080008000ULL, + 0x000000000000808bULL, 0x0000000080000001ULL, + 0x8000000080008081ULL, 0x8000000000008009ULL, + 0x000000000000008aULL, 0x0000000000000088ULL, + 0x0000000080008009ULL, 0x000000008000000aULL, + 0x000000008000808bULL, 0x800000000000008bULL, + 0x8000000000008089ULL, 0x8000000000008003ULL, + 0x8000000000008002ULL, 0x8000000000000080ULL, + 0x000000000000800aULL, 0x800000008000000aULL, + 0x8000000080008081ULL, 0x8000000000008080ULL, + 0x0000000080000001ULL, 0x8000000080008008ULL + }; +# define A00 (p->u.s[0]) +# define A01 (p->u.s[1]) +# define A02 (p->u.s[2]) +# define A03 (p->u.s[3]) +# define A04 (p->u.s[4]) +# define A10 (p->u.s[5]) +# define A11 (p->u.s[6]) +# define A12 (p->u.s[7]) +# define A13 (p->u.s[8]) +# define A14 (p->u.s[9]) +# define A20 (p->u.s[10]) +# define A21 (p->u.s[11]) +# define A22 (p->u.s[12]) +# define A23 (p->u.s[13]) +# define A24 (p->u.s[14]) +# define A30 (p->u.s[15]) +# define A31 (p->u.s[16]) +# define A32 (p->u.s[17]) +# define A33 (p->u.s[18]) +# define A34 (p->u.s[19]) +# define A40 (p->u.s[20]) +# define A41 (p->u.s[21]) +# define A42 (p->u.s[22]) +# define A43 (p->u.s[23]) +# define A44 (p->u.s[24]) +# define ROL64(a,x) ((a<<x)|(a>>(64-x))) + + for(i=0; i<24; i+=4){ + C0 = A00^A10^A20^A30^A40; + C1 = A01^A11^A21^A31^A41; + C2 = A02^A12^A22^A32^A42; + C3 = A03^A13^A23^A33^A43; + C4 = A04^A14^A24^A34^A44; + D0 = C4^ROL64(C1, 1); + D1 = C0^ROL64(C2, 1); + D2 = C1^ROL64(C3, 1); + D3 = C2^ROL64(C4, 1); + D4 = C3^ROL64(C0, 1); + + B0 = (A00^D0); + B1 = ROL64((A11^D1), 44); + B2 = ROL64((A22^D2), 43); + B3 = ROL64((A33^D3), 21); + B4 = ROL64((A44^D4), 14); + A00 = B0 ^((~B1)& B2 ); + A00 ^= RC[i]; + A11 = B1 ^((~B2)& B3 ); + A22 = B2 ^((~B3)& B4 ); + A33 = B3 ^((~B4)& B0 ); + A44 = B4 ^((~B0)& B1 ); + + B2 = ROL64((A20^D0), 3); + B3 = ROL64((A31^D1), 45); + B4 = ROL64((A42^D2), 61); + B0 = ROL64((A03^D3), 28); + B1 = ROL64((A14^D4), 20); + A20 = B0 ^((~B1)& B2 ); + A31 = B1 ^((~B2)& B3 ); + A42 = B2 ^((~B3)& B4 ); + A03 = B3 ^((~B4)& B0 ); + A14 = B4 ^((~B0)& B1 ); + + B4 = ROL64((A40^D0), 18); + B0 = ROL64((A01^D1), 1); + B1 = ROL64((A12^D2), 6); + B2 = ROL64((A23^D3), 25); + B3 = ROL64((A34^D4), 8); + A40 = B0 ^((~B1)& B2 ); + A01 = B1 ^((~B2)& B3 ); + A12 = B2 ^((~B3)& B4 ); + A23 = B3 ^((~B4)& B0 ); + A34 = B4 ^((~B0)& B1 ); + + B1 = ROL64((A10^D0), 36); + B2 = ROL64((A21^D1), 10); + B3 = ROL64((A32^D2), 15); + B4 = ROL64((A43^D3), 56); + B0 = ROL64((A04^D4), 27); + A10 = B0 ^((~B1)& B2 ); + A21 = B1 ^((~B2)& B3 ); + A32 = B2 ^((~B3)& B4 ); + A43 = B3 ^((~B4)& B0 ); + A04 = B4 ^((~B0)& B1 ); + + B3 = ROL64((A30^D0), 41); + B4 = ROL64((A41^D1), 2); + B0 = ROL64((A02^D2), 62); + B1 = ROL64((A13^D3), 55); + B2 = ROL64((A24^D4), 39); + A30 = B0 ^((~B1)& B2 ); + A41 = B1 ^((~B2)& B3 ); + A02 = B2 ^((~B3)& B4 ); + A13 = B3 ^((~B4)& B0 ); + A24 = B4 ^((~B0)& B1 ); + + C0 = A00^A20^A40^A10^A30; + C1 = A11^A31^A01^A21^A41; + C2 = A22^A42^A12^A32^A02; + C3 = A33^A03^A23^A43^A13; + C4 = A44^A14^A34^A04^A24; + D0 = C4^ROL64(C1, 1); + D1 = C0^ROL64(C2, 1); + D2 = C1^ROL64(C3, 1); + D3 = C2^ROL64(C4, 1); + D4 = C3^ROL64(C0, 1); + + B0 = (A00^D0); + B1 = ROL64((A31^D1), 44); + B2 = ROL64((A12^D2), 43); + B3 = ROL64((A43^D3), 21); + B4 = ROL64((A24^D4), 14); + A00 = B0 ^((~B1)& B2 ); + A00 ^= RC[i+1]; + A31 = B1 ^((~B2)& B3 ); + A12 = B2 ^((~B3)& B4 ); + A43 = B3 ^((~B4)& B0 ); + A24 = B4 ^((~B0)& B1 ); + + B2 = ROL64((A40^D0), 3); + B3 = ROL64((A21^D1), 45); + B4 = ROL64((A02^D2), 61); + B0 = ROL64((A33^D3), 28); + B1 = ROL64((A14^D4), 20); + A40 = B0 ^((~B1)& B2 ); + A21 = B1 ^((~B2)& B3 ); + A02 = B2 ^((~B3)& B4 ); + A33 = B3 ^((~B4)& B0 ); + A14 = B4 ^((~B0)& B1 ); + + B4 = ROL64((A30^D0), 18); + B0 = ROL64((A11^D1), 1); + B1 = ROL64((A42^D2), 6); + B2 = ROL64((A23^D3), 25); + B3 = ROL64((A04^D4), 8); + A30 = B0 ^((~B1)& B2 ); + A11 = B1 ^((~B2)& B3 ); + A42 = B2 ^((~B3)& B4 ); + A23 = B3 ^((~B4)& B0 ); + A04 = B4 ^((~B0)& B1 ); + + B1 = ROL64((A20^D0), 36); + B2 = ROL64((A01^D1), 10); + B3 = ROL64((A32^D2), 15); + B4 = ROL64((A13^D3), 56); + B0 = ROL64((A44^D4), 27); + A20 = B0 ^((~B1)& B2 ); + A01 = B1 ^((~B2)& B3 ); + A32 = B2 ^((~B3)& B4 ); + A13 = B3 ^((~B4)& B0 ); + A44 = B4 ^((~B0)& B1 ); + + B3 = ROL64((A10^D0), 41); + B4 = ROL64((A41^D1), 2); + B0 = ROL64((A22^D2), 62); + B1 = ROL64((A03^D3), 55); + B2 = ROL64((A34^D4), 39); + A10 = B0 ^((~B1)& B2 ); + A41 = B1 ^((~B2)& B3 ); + A22 = B2 ^((~B3)& B4 ); + A03 = B3 ^((~B4)& B0 ); + A34 = B4 ^((~B0)& B1 ); + + C0 = A00^A40^A30^A20^A10; + C1 = A31^A21^A11^A01^A41; + C2 = A12^A02^A42^A32^A22; + C3 = A43^A33^A23^A13^A03; + C4 = A24^A14^A04^A44^A34; + D0 = C4^ROL64(C1, 1); + D1 = C0^ROL64(C2, 1); + D2 = C1^ROL64(C3, 1); + D3 = C2^ROL64(C4, 1); + D4 = C3^ROL64(C0, 1); + + B0 = (A00^D0); + B1 = ROL64((A21^D1), 44); + B2 = ROL64((A42^D2), 43); + B3 = ROL64((A13^D3), 21); + B4 = ROL64((A34^D4), 14); + A00 = B0 ^((~B1)& B2 ); + A00 ^= RC[i+2]; + A21 = B1 ^((~B2)& B3 ); + A42 = B2 ^((~B3)& B4 ); + A13 = B3 ^((~B4)& B0 ); + A34 = B4 ^((~B0)& B1 ); + + B2 = ROL64((A30^D0), 3); + B3 = ROL64((A01^D1), 45); + B4 = ROL64((A22^D2), 61); + B0 = ROL64((A43^D3), 28); + B1 = ROL64((A14^D4), 20); + A30 = B0 ^((~B1)& B2 ); + A01 = B1 ^((~B2)& B3 ); + A22 = B2 ^((~B3)& B4 ); + A43 = B3 ^((~B4)& B0 ); + A14 = B4 ^((~B0)& B1 ); + + B4 = ROL64((A10^D0), 18); + B0 = ROL64((A31^D1), 1); + B1 = ROL64((A02^D2), 6); + B2 = ROL64((A23^D3), 25); + B3 = ROL64((A44^D4), 8); + A10 = B0 ^((~B1)& B2 ); + A31 = B1 ^((~B2)& B3 ); + A02 = B2 ^((~B3)& B4 ); + A23 = B3 ^((~B4)& B0 ); + A44 = B4 ^((~B0)& B1 ); + + B1 = ROL64((A40^D0), 36); + B2 = ROL64((A11^D1), 10); + B3 = ROL64((A32^D2), 15); + B4 = ROL64((A03^D3), 56); + B0 = ROL64((A24^D4), 27); + A40 = B0 ^((~B1)& B2 ); + A11 = B1 ^((~B2)& B3 ); + A32 = B2 ^((~B3)& B4 ); + A03 = B3 ^((~B4)& B0 ); + A24 = B4 ^((~B0)& B1 ); + + B3 = ROL64((A20^D0), 41); + B4 = ROL64((A41^D1), 2); + B0 = ROL64((A12^D2), 62); + B1 = ROL64((A33^D3), 55); + B2 = ROL64((A04^D4), 39); + A20 = B0 ^((~B1)& B2 ); + A41 = B1 ^((~B2)& B3 ); + A12 = B2 ^((~B3)& B4 ); + A33 = B3 ^((~B4)& B0 ); + A04 = B4 ^((~B0)& B1 ); + + C0 = A00^A30^A10^A40^A20; + C1 = A21^A01^A31^A11^A41; + C2 = A42^A22^A02^A32^A12; + C3 = A13^A43^A23^A03^A33; + C4 = A34^A14^A44^A24^A04; + D0 = C4^ROL64(C1, 1); + D1 = C0^ROL64(C2, 1); + D2 = C1^ROL64(C3, 1); + D3 = C2^ROL64(C4, 1); + D4 = C3^ROL64(C0, 1); + + B0 = (A00^D0); + B1 = ROL64((A01^D1), 44); + B2 = ROL64((A02^D2), 43); + B3 = ROL64((A03^D3), 21); + B4 = ROL64((A04^D4), 14); + A00 = B0 ^((~B1)& B2 ); + A00 ^= RC[i+3]; + A01 = B1 ^((~B2)& B3 ); + A02 = B2 ^((~B3)& B4 ); + A03 = B3 ^((~B4)& B0 ); + A04 = B4 ^((~B0)& B1 ); + + B2 = ROL64((A10^D0), 3); + B3 = ROL64((A11^D1), 45); + B4 = ROL64((A12^D2), 61); + B0 = ROL64((A13^D3), 28); + B1 = ROL64((A14^D4), 20); + A10 = B0 ^((~B1)& B2 ); + A11 = B1 ^((~B2)& B3 ); + A12 = B2 ^((~B3)& B4 ); + A13 = B3 ^((~B4)& B0 ); + A14 = B4 ^((~B0)& B1 ); + + B4 = ROL64((A20^D0), 18); + B0 = ROL64((A21^D1), 1); + B1 = ROL64((A22^D2), 6); + B2 = ROL64((A23^D3), 25); + B3 = ROL64((A24^D4), 8); + A20 = B0 ^((~B1)& B2 ); + A21 = B1 ^((~B2)& B3 ); + A22 = B2 ^((~B3)& B4 ); + A23 = B3 ^((~B4)& B0 ); + A24 = B4 ^((~B0)& B1 ); + + B1 = ROL64((A30^D0), 36); + B2 = ROL64((A31^D1), 10); + B3 = ROL64((A32^D2), 15); + B4 = ROL64((A33^D3), 56); + B0 = ROL64((A34^D4), 27); + A30 = B0 ^((~B1)& B2 ); + A31 = B1 ^((~B2)& B3 ); + A32 = B2 ^((~B3)& B4 ); + A33 = B3 ^((~B4)& B0 ); + A34 = B4 ^((~B0)& B1 ); + + B3 = ROL64((A40^D0), 41); + B4 = ROL64((A41^D1), 2); + B0 = ROL64((A42^D2), 62); + B1 = ROL64((A43^D3), 55); + B2 = ROL64((A44^D4), 39); + A40 = B0 ^((~B1)& B2 ); + A41 = B1 ^((~B2)& B3 ); + A42 = B2 ^((~B3)& B4 ); + A43 = B3 ^((~B4)& B0 ); + A44 = B4 ^((~B0)& B1 ); + } +} + +/* +** Initialize a new hash. iSize determines the size of the hash +** in bits and should be one of 224, 256, 384, or 512. Or iSize +** can be zero to use the default hash size of 256 bits. +*/ +static void SHA3Init(SHA3Context *p, int iSize){ + memset(p, 0, sizeof(*p)); + if( iSize>=128 && iSize<=512 ){ + p->nRate = (1600 - ((iSize + 31)&~31)*2)/8; + }else{ + p->nRate = (1600 - 2*256)/8; + } +#if BYTEORDER==1234 + /* Known to be little-endian at compile-time. No-op */ +#elif BYTEORDER==4321 + p->ixMask = 7; /* Big-endian */ +#else + { + static unsigned int one = 1; + if( 1==*(unsigned char*)&one ){ + /* Little endian. No byte swapping. */ + p->ixMask = 0; + }else{ + /* Big endian. Byte swap. */ + p->ixMask = 7; + } + } +#endif +} + +/* +** Make consecutive calls to the SHA3Update function to add new content +** to the hash +*/ +static void SHA3Update( + SHA3Context *p, + const unsigned char *aData, + unsigned int nData +){ + unsigned int i = 0; +#if BYTEORDER==1234 + if( (p->nLoaded % 8)==0 && ((aData - (const unsigned char*)0)&7)==0 ){ + for(; i+7<nData; i+=8){ + p->u.s[p->nLoaded/8] ^= *(u64*)&aData[i]; + p->nLoaded += 8; + if( p->nLoaded>=p->nRate ){ + KeccakF1600Step(p); + p->nLoaded = 0; + } + } + } +#endif + for(; i<nData; i++){ +#if BYTEORDER==1234 + p->u.x[p->nLoaded] ^= aData[i]; +#elif BYTEORDER==4321 + p->u.x[p->nLoaded^0x07] ^= aData[i]; +#else + p->u.x[p->nLoaded^p->ixMask] ^= aData[i]; +#endif + p->nLoaded++; + if( p->nLoaded==p->nRate ){ + KeccakF1600Step(p); + p->nLoaded = 0; + } + } +} + +/* +** After all content has been added, invoke SHA3Final() to compute +** the final hash. The function returns a pointer to the binary +** hash value. +*/ +static unsigned char *SHA3Final(SHA3Context *p){ + unsigned int i; + if( p->nLoaded==p->nRate-1 ){ + const unsigned char c1 = 0x86; + SHA3Update(p, &c1, 1); + }else{ + const unsigned char c2 = 0x06; + const unsigned char c3 = 0x80; + SHA3Update(p, &c2, 1); + p->nLoaded = p->nRate - 1; + SHA3Update(p, &c3, 1); + } + for(i=0; i<p->nRate; i++){ + p->u.x[i+p->nRate] = p->u.x[i^p->ixMask]; + } + return &p->u.x[p->nRate]; +} + +/* +** Convert a digest into base-16. digest should be declared as +** "unsigned char digest[20]" in the calling function. The SHA3 +** digest is stored in the first 20 bytes. zBuf should +** be "char zBuf[41]". +*/ +static void DigestToBase16(unsigned char *digest, char *zBuf, int nByte){ + static const char zEncode[] = "0123456789abcdef"; + int ix; + + for(ix=0; ix<nByte; ix++){ + *zBuf++ = zEncode[(*digest>>4)&0xf]; + *zBuf++ = zEncode[*digest++ & 0xf]; + } + *zBuf = '\0'; +} + + +/* +** Compute the SHA3 checksum of a file on disk. Store the resulting +** checksum in the blob pCksum. pCksum is assumed to be initialized. +** +** Return the number of errors. +*/ +static int sha3sum_file(const char *zFilename, int iSize, char *pCksum){ + FILE *in; + SHA3Context ctx; + char zBuf[10240]; + + in = fopen(zFilename,"rb"); + if( in==0 ){ + return 1; + } + SHA3Init(&ctx, iSize); + for(;;){ + int n = (int)fread(zBuf, 1, sizeof(zBuf), in); + if( n<=0 ) break; + SHA3Update(&ctx, (unsigned char*)zBuf, (unsigned)n); + } + fclose(in); + DigestToBase16(SHA3Final(&ctx), pCksum, iSize/8); + return 0; +} + +/* +** The SHA1 implementation below is adapted from: +** +** $NetBSD: sha1.c,v 1.6 2009/11/06 20:31:18 joerg Exp $ +** $OpenBSD: sha1.c,v 1.9 1997/07/23 21:12:32 kstailey Exp $ +** +** SHA-1 in C +** By Steve Reid <steve@edmweb.com> +** 100% Public Domain +*/ +typedef struct SHA1Context SHA1Context; +struct SHA1Context { + unsigned int state[5]; + unsigned int count[2]; + unsigned char buffer[64]; +}; + +/* + * blk0() and blk() perform the initial expand. + * I got the idea of expanding during the round function from SSLeay + * + * blk0le() for little-endian and blk0be() for big-endian. + */ +#define SHA_ROT(x,l,r) ((x) << (l) | (x) >> (r)) +#define rol(x,k) SHA_ROT(x,k,32-(k)) +#define ror(x,k) SHA_ROT(x,32-(k),k) + +#define blk0le(i) (block[i] = (ror(block[i],8)&0xFF00FF00) \ + |(rol(block[i],8)&0x00FF00FF)) +#define blk0be(i) block[i] +#define blk(i) (block[i&15] = rol(block[(i+13)&15]^block[(i+8)&15] \ + ^block[(i+2)&15]^block[i&15],1)) + +/* + * (R0+R1), R2, R3, R4 are the different operations (rounds) used in SHA1 + * + * Rl0() for little-endian and Rb0() for big-endian. Endianness is + * determined at run-time. + */ +#define Rl0(v,w,x,y,z,i) \ + z+=((w&(x^y))^y)+blk0le(i)+0x5A827999+rol(v,5);w=ror(w,2); +#define Rb0(v,w,x,y,z,i) \ + z+=((w&(x^y))^y)+blk0be(i)+0x5A827999+rol(v,5);w=ror(w,2); +#define R1(v,w,x,y,z,i) \ + z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=ror(w,2); +#define R2(v,w,x,y,z,i) \ + z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=ror(w,2); +#define R3(v,w,x,y,z,i) \ + z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=ror(w,2); +#define R4(v,w,x,y,z,i) \ + z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=ror(w,2); + +/* + * Hash a single 512-bit block. This is the core of the algorithm. + */ +#define a qq[0] +#define b qq[1] +#define c qq[2] +#define d qq[3] +#define e qq[4] + +static void SHA1Transform( + unsigned int state[5], + const unsigned char buffer[64] +){ + unsigned int qq[5]; /* a, b, c, d, e; */ + static int one = 1; + unsigned int block[16]; + memcpy(block, buffer, 64); + memcpy(qq,state,5*sizeof(unsigned int)); + + /* Copy context->state[] to working vars */ + /* + a = state[0]; + b = state[1]; + c = state[2]; + d = state[3]; + e = state[4]; + */ + + /* 4 rounds of 20 operations each. Loop unrolled. */ + if( 1 == *(unsigned char*)&one ){ + Rl0(a,b,c,d,e, 0); Rl0(e,a,b,c,d, 1); Rl0(d,e,a,b,c, 2); Rl0(c,d,e,a,b, 3); + Rl0(b,c,d,e,a, 4); Rl0(a,b,c,d,e, 5); Rl0(e,a,b,c,d, 6); Rl0(d,e,a,b,c, 7); + Rl0(c,d,e,a,b, 8); Rl0(b,c,d,e,a, 9); Rl0(a,b,c,d,e,10); Rl0(e,a,b,c,d,11); + Rl0(d,e,a,b,c,12); Rl0(c,d,e,a,b,13); Rl0(b,c,d,e,a,14); Rl0(a,b,c,d,e,15); + }else{ + Rb0(a,b,c,d,e, 0); Rb0(e,a,b,c,d, 1); Rb0(d,e,a,b,c, 2); Rb0(c,d,e,a,b, 3); + Rb0(b,c,d,e,a, 4); Rb0(a,b,c,d,e, 5); Rb0(e,a,b,c,d, 6); Rb0(d,e,a,b,c, 7); + Rb0(c,d,e,a,b, 8); Rb0(b,c,d,e,a, 9); Rb0(a,b,c,d,e,10); Rb0(e,a,b,c,d,11); + Rb0(d,e,a,b,c,12); Rb0(c,d,e,a,b,13); Rb0(b,c,d,e,a,14); Rb0(a,b,c,d,e,15); + } + R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19); + R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23); + R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27); + R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31); + R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35); + R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39); + R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43); + R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47); + R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51); + R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55); + R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59); + R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63); + R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67); + R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71); + R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75); + R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79); + + /* Add the working vars back into context.state[] */ + state[0] += a; + state[1] += b; + state[2] += c; + state[3] += d; + state[4] += e; +} + + +/* + * SHA1Init - Initialize new context + */ +static void SHA1Init(SHA1Context *context){ + /* SHA1 initialization constants */ + context->state[0] = 0x67452301; + context->state[1] = 0xEFCDAB89; + context->state[2] = 0x98BADCFE; + context->state[3] = 0x10325476; + context->state[4] = 0xC3D2E1F0; + context->count[0] = context->count[1] = 0; +} + + +/* + * Run your data through this. + */ +static void SHA1Update( + SHA1Context *context, + const unsigned char *data, + unsigned int len +){ + unsigned int i, j; + + j = context->count[0]; + if ((context->count[0] += len << 3) < j) + context->count[1] += (len>>29)+1; + j = (j >> 3) & 63; + if ((j + len) > 63) { + (void)memcpy(&context->buffer[j], data, (i = 64-j)); + SHA1Transform(context->state, context->buffer); + for ( ; i + 63 < len; i += 64) + SHA1Transform(context->state, &data[i]); + j = 0; + } else { + i = 0; + } + (void)memcpy(&context->buffer[j], &data[i], len - i); +} + + +/* + * Add padding and return the message digest. + */ +static void SHA1Final(unsigned char *digest, SHA1Context *context){ + unsigned int i; + unsigned char finalcount[8]; + + for (i = 0; i < 8; i++) { + finalcount[i] = (unsigned char)((context->count[(i >= 4 ? 0 : 1)] + >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */ + } + SHA1Update(context, (const unsigned char *)"\200", 1); + while ((context->count[0] & 504) != 448) + SHA1Update(context, (const unsigned char *)"\0", 1); + SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */ + + if (digest) { + for (i = 0; i < 20; i++) + digest[i] = (unsigned char) + ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255); + } +} + + +/* +** Compute the SHA1 checksum of a file on disk. Store the resulting +** checksum in the blob pCksum. pCksum is assumed to be initialized. +** +** Return the number of errors. +*/ +static int sha1sum_file(const char *zFilename, char *pCksum){ + FILE *in; + SHA1Context ctx; + unsigned char zResult[20]; + char zBuf[10240]; + + in = fopen(zFilename,"rb"); + if( in==0 ){ + return 1; + } + SHA1Init(&ctx); + for(;;){ + int n = (int)fread(zBuf, 1, sizeof(zBuf), in); + if( n<=0 ) break; + SHA1Update(&ctx, (unsigned char*)zBuf, (unsigned)n); + } + fclose(in); + SHA1Final(zResult, &ctx); + DigestToBase16(zResult, pCksum, 20); + return 0; +} + +/* +** Print a usage comment and quit. +*/ +static void usage(const char *argv0){ + fprintf(stderr, + "Usage: %s manifest\n" + "Options:\n" + " -v Diagnostic output\n" + , argv0); + exit(1); +} + +/* +** Find the first whitespace character in a string. Set that whitespace +** to a \000 terminator and return a pointer to the next character. +*/ +static char *nextToken(char *z){ + while( *z && !isspace(*z) ) z++; + if( *z==0 ) return z; + *z = 0; + return &z[1]; +} + + +int main(int argc, char **argv){ + const char *zManifest = 0; + int i; + int bVerbose = 0; + FILE *in; + int allValid = 1; + int rc; + SHA3Context ctx; + char zDate[50]; + char zHash[100]; + char zLine[20000]; + + for(i=1; i<argc; i++){ + const char *z = argv[i]; + if( z[0]=='-' ){ + if( z[1]=='-' ) z++; + if( strcmp(z, "-v")==0 ){ + bVerbose = 1; + }else + { + fprintf(stderr, "unknown option \"%s\"", argv[i]); + exit(1); + } + }else if( zManifest!=0 ){ + usage(argv[0]); + }else{ + zManifest = z; + } + } + if( zManifest==0 ) usage(argv[0]); + zDate[0] = 0; + in = fopen(zManifest, "rb"); + if( in==0 ){ + fprintf(stderr, "cannot open \"%s\" for reading\n", zManifest); + exit(1); + } + SHA3Init(&ctx, 256); + while( fgets(zLine, sizeof(zLine), in) ){ + if( strncmp(zLine,"# Remove this line", 18)!=0 ){ + SHA3Update(&ctx, (unsigned char*)zLine, (unsigned)strlen(zLine)); + } + if( strncmp(zLine, "D 20", 4)==0 ){ + memcpy(zDate, &zLine[2], 10); + zDate[10] = ' '; + memcpy(&zDate[11], &zLine[13], 8); + zDate[19] = 0; + continue; + } + if( strncmp(zLine, "F ", 2)==0 ){ + char *zFilename = &zLine[2]; + char *zMHash = nextToken(zFilename); + nextToken(zMHash); + if( strlen(zMHash)==40 ){ + rc = sha1sum_file(zFilename, zHash); + }else{ + rc = sha3sum_file(zFilename, 256, zHash); + } + if( rc ){ + allValid = 0; + if( bVerbose ){ + printf("hash failed: %s\n", zFilename); + } + }else if( strcmp(zHash, zMHash)!=0 ){ + allValid = 0; + if( bVerbose ){ + printf("wrong hash: %s\n", zFilename); + printf("... expected: %s\n", zMHash); + printf("... got: %s\n", zHash); + } + } + } + } + fclose(in); + DigestToBase16(SHA3Final(&ctx), zHash, 256/8); + if( !allValid ){ + printf("%s %.60salt1\n", zDate, zHash); + }else{ + printf("%s %s\n", zDate, zHash); + } + return 0; +} diff --git a/tool/mkspeedsql.tcl b/tool/mkspeedsql.tcl deleted file mode 100644 index 04bafc04c1..0000000000 --- a/tool/mkspeedsql.tcl +++ /dev/null @@ -1,237 +0,0 @@ -# 2008 October 9 -# -# The author disclaims copyright to this source code. In place of -# a legal notice, here is a blessing: -# -# May you do good and not evil. -# May you find forgiveness for yourself and forgive others. -# May you share freely, never taking more than you give. -# -#************************************************************************* -# This file generates SQL text used for performance testing. -# -# $Id: mkspeedsql.tcl,v 1.1 2008/10/09 17:57:34 drh Exp $ -# - -# Set a uniform random seed -expr srand(0) - -# The number_name procedure below converts its argment (an integer) -# into a string which is the English-language name for that number. -# -# Example: -# -# puts [number_name 123] -> "one hundred twenty three" -# -set ones {zero one two three four five six seven eight nine - ten eleven twelve thirteen fourteen fifteen sixteen seventeen - eighteen nineteen} -set tens {{} ten twenty thirty forty fifty sixty seventy eighty ninety} -proc number_name {n} { - if {$n>=1000} { - set txt "[number_name [expr {$n/1000}]] thousand" - set n [expr {$n%1000}] - } else { - set txt {} - } - if {$n>=100} { - append txt " [lindex $::ones [expr {$n/100}]] hundred" - set n [expr {$n%100}] - } - if {$n>=20} { - append txt " [lindex $::tens [expr {$n/10}]]" - set n [expr {$n%10}] - } - if {$n>0} { - append txt " [lindex $::ones $n]" - } - set txt [string trim $txt] - if {$txt==""} {set txt zero} - return $txt -} - -# Create a database schema. -# -puts { - PRAGMA page_size=1024; - PRAGMA cache_size=8192; - PRAGMA locking_mode=EXCLUSIVE; - CREATE TABLE t1(a INTEGER, b INTEGER, c TEXT); - CREATE TABLE t2(a INTEGER, b INTEGER, c TEXT); - CREATE INDEX i2a ON t2(a); - CREATE INDEX i2b ON t2(b); - SELECT name FROM sqlite_master ORDER BY 1; -} - - -# 50000 INSERTs on an unindexed table -# -set t1c_list {} -puts {BEGIN;} -for {set i 1} {$i<=50000} {incr i} { - set r [expr {int(rand()*500000)}] - set x [number_name $r] - lappend t1c_list $x - puts "INSERT INTO t1 VALUES($i,$r,'$x');" -} -puts {COMMIT;} - -# 50000 INSERTs on an indexed table -# -puts {BEGIN;} -for {set i 1} {$i<=50000} {incr i} { - set r [expr {int(rand()*500000)}] - puts "INSERT INTO t2 VALUES($i,$r,'[number_name $r]');" -} -puts {COMMIT;} - - -# 50 SELECTs on an integer comparison. There is no index so -# a full table scan is required. -# -for {set i 0} {$i<50} {incr i} { - set lwr [expr {$i*100}] - set upr [expr {($i+10)*100}] - puts "SELECT count(*), avg(b) FROM t1 WHERE b>=$lwr AND b<$upr;" -} - -# 50 SELECTs on an LIKE comparison. There is no index so a full -# table scan is required. -# -for {set i 0} {$i<50} {incr i} { - puts "SELECT count(*), avg(b) FROM t1 WHERE c LIKE '%[number_name $i]%';" -} - -# Create indices -# -puts {BEGIN;} -puts { - CREATE INDEX i1a ON t1(a); - CREATE INDEX i1b ON t1(b); - CREATE INDEX i1c ON t1(c); -} -puts {COMMIT;} - -# 5000 SELECTs on an integer comparison where the integer is -# indexed. -# -set sql {} -for {set i 0} {$i<5000} {incr i} { - set lwr [expr {$i*100}] - set upr [expr {($i+10)*100}] - puts "SELECT count(*), avg(b) FROM t1 WHERE b>=$lwr AND b<$upr;" -} - -# 100000 random SELECTs against rowid. -# -for {set i 1} {$i<=100000} {incr i} { - set id [expr {int(rand()*50000)+1}] - puts "SELECT c FROM t1 WHERE rowid=$id;" -} - -# 100000 random SELECTs against a unique indexed column. -# -for {set i 1} {$i<=100000} {incr i} { - set id [expr {int(rand()*50000)+1}] - puts "SELECT c FROM t1 WHERE a=$id;" -} - -# 50000 random SELECTs against an indexed column text column -# -set nt1c [llength $t1c_list] -for {set i 0} {$i<50000} {incr i} { - set r [expr {int(rand()*$nt1c)}] - set c [lindex $t1c_list $i] - puts "SELECT c FROM t1 WHERE c='$c';" -} - - -# Vacuum -puts {VACUUM;} - -# 5000 updates of ranges where the field being compared is indexed. -# -puts {BEGIN;} -for {set i 0} {$i<5000} {incr i} { - set lwr [expr {$i*2}] - set upr [expr {($i+1)*2}] - puts "UPDATE t1 SET b=b*2 WHERE a>=$lwr AND a<$upr;" -} -puts {COMMIT;} - -# 50000 single-row updates. An index is used to find the row quickly. -# -puts {BEGIN;} -for {set i 0} {$i<50000} {incr i} { - set r [expr {int(rand()*500000)}] - puts "UPDATE t1 SET b=$r WHERE a=$i;" -} -puts {COMMIT;} - -# 1 big text update that touches every row in the table. -# -puts { - UPDATE t1 SET c=a; -} - -# Many individual text updates. Each row in the table is -# touched through an index. -# -puts {BEGIN;} -for {set i 1} {$i<=50000} {incr i} { - set r [expr {int(rand()*500000)}] - puts "UPDATE t1 SET c='[number_name $r]' WHERE a=$i;" -} -puts {COMMIT;} - -# Delete all content in a table. -# -puts {DELETE FROM t1;} - -# Copy one table into another -# -puts {INSERT INTO t1 SELECT * FROM t2;} - -# Delete all content in a table, one row at a time. -# -puts {DELETE FROM t1 WHERE 1;} - -# Refill the table yet again -# -puts {INSERT INTO t1 SELECT * FROM t2;} - -# Drop the table and recreate it without its indices. -# -puts {BEGIN;} -puts { - DROP TABLE t1; - CREATE TABLE t1(a INTEGER, b INTEGER, c TEXT); -} -puts {COMMIT;} - -# Refill the table yet again. This copy should be faster because -# there are no indices to deal with. -# -puts {INSERT INTO t1 SELECT * FROM t2;} - -# Select 20000 rows from the table at random. -# -puts { - SELECT rowid FROM t1 ORDER BY random() LIMIT 20000; -} - -# Delete 20000 random rows from the table. -# -puts { - DELETE FROM t1 WHERE rowid IN - (SELECT rowid FROM t1 ORDER BY random() LIMIT 20000); -} -puts {SELECT count(*) FROM t1;} - -# Delete 20000 more rows at random from the table. -# -puts { - DELETE FROM t1 WHERE rowid IN - (SELECT rowid FROM t1 ORDER BY random() LIMIT 20000); -} -puts {SELECT count(*) FROM t1;} diff --git a/tool/mksqlite3c-noext.tcl b/tool/mksqlite3c-noext.tcl index 601b8cce8c..1148b1c0d5 100644 --- a/tool/mksqlite3c-noext.tcl +++ b/tool/mksqlite3c-noext.tcl @@ -1,7 +1,7 @@ #!/usr/bin/tclsh # # To build a single huge source file holding all of SQLite (or at -# least the core components - the test harness, shell, and TCL +# least the core components - the test harness, shell, and TCL # interface are omitted.) first do # # make target_source @@ -11,7 +11,7 @@ # there and will not work if they are not found.) There are a few # generated C code files that are also added to the tsrc directory. # For example, the "parse.c" and "parse.h" files to implement the -# the parser are derived from "parse.y" using lemon. And the +# the parser are derived from "parse.y" using lemon. And the # "keywordhash.h" files is generated by a program named "mkkeywordhash". # # After the "tsrc" directory has been created and populated, run @@ -26,15 +26,20 @@ # from in this file. The version number is needed to generate the header # comment of the amalgamation. # -if {[lsearch $argv --nostatic]>=0} { - set addstatic 0 -} else { - set addstatic 1 -} -if {[lsearch $argv --linemacros]>=0} { - set linemacros 1 -} else { - set linemacros 0 +set addstatic 1 +set linemacros 0 +set useapicall 0 +for {set i 0} {$i<[llength $argv]} {incr i} { + set x [lindex $argv $i] + if {[regexp {^-+nostatic$} $x]} { + set addstatic 0 + } elseif {[regexp {^-+linemacros} $x]} { + set linemacros 1 + } elseif {[regexp {^-+useapicall} $x]} { + set useapicall 1 + } else { + error "unknown command-line option: $x" + } } set in [open tsrc/sqlite3.h] set cnt 0 @@ -52,12 +57,12 @@ close $in # set out [open sqlite3.c w] # Force the output to use unix line endings, even on Windows. -fconfigure $out -translation lf +fconfigure $out -translation binary set today [clock format [clock seconds] -format "%Y-%m-%d %H:%M:%S UTC" -gmt 1] puts $out [subst \ {/****************************************************************************** ** This file is an amalgamation of many separate C source files from SQLite -** version $VERSION. By combining all the individual C code files into this +** version $VERSION. By combining all the individual C code files into this ** single large file, the entire code can be compiled as a single translation ** unit. This allows many compilers to do optimizations that would not be ** possible if the files were compiled separately. Performance improvements @@ -66,7 +71,7 @@ puts $out [subst \ ** ** This file is all you need to compile SQLite. To use SQLite in other ** programs, you need this file and the "sqlite3.h" header file that defines -** the programming interface to the SQLite library. (If you do not have +** the programming interface to the SQLite library. (If you do not have ** the "sqlite3.h" header file at hand, you will find a copy embedded within ** the text of this file. Search for "Begin file sqlite3.h" to find the start ** of the embedded sqlite3.h header file.) Additional code files may be needed @@ -83,7 +88,7 @@ if {$addstatic} { #endif} } -# These are the header files used by SQLite. The first time any of these +# These are the header files used by SQLite. The first time any of these # files are seen in a #include statement in the C code, include the complete # text of the file in-line. The file only needs to be included once. # @@ -104,8 +109,8 @@ foreach hdr { parse.h pcache.h pragma.h - sqlite3ext.h sqlite3.h + sqlite3ext.h sqliteicu.h sqliteInt.h sqliteLimit.h @@ -155,7 +160,8 @@ proc section_comment {text} { # process them appropriately. # proc copy_file {filename} { - global seen_hdr available_hdr varonly_hdr cdecllist out addstatic linemacros + global seen_hdr available_hdr varonly_hdr cdecllist out + global addstatic linemacros useapicall set ln 0 set tail [file tail $filename] section_comment "Begin file $tail" @@ -203,7 +209,8 @@ proc copy_file {filename} { puts $out "#if 0" } elseif {!$linemacros && [regexp {^#line} $line]} { # Skip #line directives. - } elseif {$addstatic && ![regexp {^(static|typedef)} $line]} { + } elseif {$addstatic + && ![regexp {^(static|typedef|SQLITE_PRIVATE)} $line]} { # Skip adding the SQLITE_PRIVATE or SQLITE_API keyword before # functions if this header file does not need it. if {![info exists varonly_hdr($tail)] @@ -211,18 +218,20 @@ proc copy_file {filename} { regsub {^SQLITE_API } $line {} line # Add the SQLITE_PRIVATE or SQLITE_API keyword before functions. # so that linkage can be modified at compile-time. - if {[regexp {^sqlite3_} $funcname]} { + if {[regexp {^sqlite3[a-z]*_} $funcname]} { set line SQLITE_API append line " " [string trim $rettype] if {[string index $rettype end] ne "*"} { append line " " } - if {[lsearch -exact $cdecllist $funcname] >= 0} { - append line SQLITE_CDECL - } else { - append line SQLITE_STDCALL + if {$useapicall} { + if {[lsearch -exact $cdecllist $funcname] >= 0} { + append line SQLITE_CDECL " " + } else { + append line SQLITE_APICALL " " + } } - append line " " $funcname $rest + append line $funcname $rest puts $out $line } else { puts $out "SQLITE_PRIVATE $line" @@ -285,6 +294,7 @@ foreach file { mutex_w32.c malloc.c printf.c + treeview.c random.c threads.c utf.c @@ -313,7 +323,6 @@ foreach file { vdbe.c vdbeblob.c vdbesort.c - journal.c memjournal.c walker.c @@ -339,7 +348,10 @@ foreach file { update.c vacuum.c vtab.c + wherecode.c + whereexpr.c where.c + window.c parse.c diff --git a/tool/mksqlite3c.tcl b/tool/mksqlite3c.tcl index 65cc10dfd9..93af239944 100644 --- a/tool/mksqlite3c.tcl +++ b/tool/mksqlite3c.tcl @@ -1,7 +1,7 @@ #!/usr/bin/tclsh # # To build a single huge source file holding all of SQLite (or at -# least the core components - the test harness, shell, and TCL +# least the core components - the test harness, shell, and TCL # interface are omitted.) first do # # make target_source @@ -11,34 +11,66 @@ # there and will not work if they are not found.) There are a few # generated C code files that are also added to the tsrc directory. # For example, the "parse.c" and "parse.h" files to implement the -# the parser are derived from "parse.y" using lemon. And the +# the parser are derived from "parse.y" using lemon. And the # "keywordhash.h" files is generated by a program named "mkkeywordhash". # # After the "tsrc" directory has been created and populated, run # this script: # -# tclsh mksqlite3c.tcl --srcdir $SRC +# tclsh mksqlite3c.tcl [flags] [extra source files] # # The amalgamated SQLite code will be written into sqlite3.c # +set help {Usage: tclsh mksqlite3c.tcl <options> + where <options> is zero or more of the following with these effects: + --nostatic => Do not generate with compile-time modifiable linkage. + --linemacros=? => Emit #line directives into output or not. (? = 1 or 0) + --useapicall => Prepend functions with SQLITE_APICALL or SQLITE_CDECL. + --srcdir $SRC => Specify the directory containing constituent sources. + --help => See this. + The value setting options default to --linemacros=1 and '--srcdir tsrc' . +} + # Begin by reading the "sqlite3.h" header file. Extract the version number # from in this file. The version number is needed to generate the header # comment of the amalgamation. # + set addstatic 1 set linemacros 0 +set useapicall 0 +set enable_recover 0 +set srcdir tsrc +set extrasrc [list] + for {set i 0} {$i<[llength $argv]} {incr i} { set x [lindex $argv $i] - if {[regexp {^-+nostatic$} $x]} { + if {[regexp {^-?-enable-recover$} $x]} { + set enable_recover 1 + } elseif {[regexp {^-?-nostatic$} $x]} { set addstatic 0 - } elseif {[regexp {^-+linemacros} $x]} { - set linemacros 1 - } else { + } elseif {[regexp {^-?-linemacros(?:=([01]))?$} $x ma ulm]} { + if {$ulm == ""} {set ulm 1} + set linemacros $ulm + } elseif {[regexp {^-?-useapicall$} $x]} { + set useapicall 1 + } elseif {[regexp {^-?-srcdir$} $x]} { + incr i + if {$i==[llength $argv]} { + error "No argument following $x" + } + set srcdir [lindex $argv $i] + } elseif {[regexp {^-?-((help)|\?)$} $x]} { + puts $help + exit 0 + } elseif {[regexp {^-?-} $x]} { error "unknown command-line option: $x" + } else { + lappend extrasrc $x } } -set in [open tsrc/sqlite3.h] +set in [open $srcdir/sqlite3.h rb] set cnt 0 set VERSION ????? while {![eof $in]} { @@ -52,14 +84,16 @@ close $in # Open the output file and write a header comment at the beginning # of the file. # -set out [open sqlite3.c w] +set fname sqlite3.c +if {$enable_recover} { set fname sqlite3r.c } +set out [open $fname wb] # Force the output to use unix line endings, even on Windows. -fconfigure $out -translation lf +fconfigure $out -translation binary set today [clock format [clock seconds] -format "%Y-%m-%d %H:%M:%S UTC" -gmt 1] puts $out [subst \ {/****************************************************************************** ** This file is an amalgamation of many separate C source files from SQLite -** version $VERSION. By combining all the individual C code files into this +** version $VERSION. By combining all the individual C code files into this ** single large file, the entire code can be compiled as a single translation ** unit. This allows many compilers to do optimizations that would not be ** possible if the files were compiled separately. Performance improvements @@ -68,14 +102,43 @@ puts $out [subst \ ** ** This file is all you need to compile SQLite. To use SQLite in other ** programs, you need this file and the "sqlite3.h" header file that defines -** the programming interface to the SQLite library. (If you do not have +** the programming interface to the SQLite library. (If you do not have ** the "sqlite3.h" header file at hand, you will find a copy embedded within ** the text of this file. Search for "Begin file sqlite3.h" to find the start ** of the embedded sqlite3.h header file.) Additional code files may be needed ** if you want a wrapper to interface SQLite with your choice of programming ** language. The code for the "sqlite3" command-line shell is also in a ** separate file. This file contains only code for the core SQLite library. -*/ +**}] +set srcroot [file dirname [file dirname [info script]]] +if {$tcl_platform(platform) eq "windows"} { + set vsrcprog src-verify.exe +} else { + set vsrcprog ./src-verify +} +if {[file executable $vsrcprog] && [file readable $srcroot/manifest]} { + set tmpfile tmp-[clock millisec]-[expr {int(rand()*100000000000)}].txt + exec $vsrcprog -x $srcroot > $tmpfile + set fd [open $tmpfile rb] + set res [string trim [split [read $fd] \n]] + close $fd + file delete -force $tmpfile + puts $out "** The content in this amalgamation comes from Fossil check-in" + puts -nonewline $out "** [string range [lindex $res 0] 0 35]" + if {[llength $res]==1} { + puts $out "." + } else { + puts $out " with changes in files:\n**" + foreach f [lrange $res 1 end] { + puts $out "** [string trim $f]" + } + } +} else { + puts $out "** The origin of the sources used to build this amalgamation" + puts $out "** is unknown." +} +puts $out [subst {*/ +#ifndef SQLITE_AMALGAMATION #define SQLITE_CORE 1 #define SQLITE_AMALGAMATION 1}] if {$addstatic} { @@ -85,12 +148,23 @@ if {$addstatic} { #endif} } -# These are the header files used by SQLite. The first time any of these +# Examine the parse.c file. If it contains lines of the form: +# +# "#ifndef SQLITE_ENABLE_UPDATE_LIMIT +# +# then set the SQLITE_UDL_CAPABLE_PARSER flag in the amalgamation. +# +set in [open $srcdir/parse.c rb] +if {[regexp {ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT} [read $in]]} { + puts $out "#define SQLITE_UDL_CAPABLE_PARSER 1" +} +close $in + +# These are the header files used by SQLite. The first time any of these # files are seen in a #include statement in the C code, include the complete # text of the file in-line. The file only needs to be included once. # foreach hdr { - crypto.h sqlcipher.h btree.h btreeInt.h @@ -98,6 +172,7 @@ foreach hdr { fts3Int.h fts3_hash.h fts3_tokenizer.h + geopoly.c hash.h hwtime.h keywordhash.h @@ -113,6 +188,7 @@ foreach hdr { pcache.h pragma.h rtree.h + sqlite3session.h sqlite3.h sqlite3ext.h sqlite3rbu.h @@ -124,10 +200,13 @@ foreach hdr { vxworks.h wal.h whereInt.h + sqlite3recover.h } { set available_hdr($hdr) 1 } set available_hdr(sqliteInt.h) 0 +set available_hdr(os_common.h) 0 +set available_hdr(sqlite3session.h) 0 # These headers should be copied into the amalgamation without modifying any # of their function declarations or definitions. @@ -165,12 +244,13 @@ proc section_comment {text} { # process them appropriately. # proc copy_file {filename} { - global seen_hdr available_hdr varonly_hdr cdecllist out addstatic linemacros + global seen_hdr available_hdr varonly_hdr cdecllist out + global addstatic linemacros useapicall srcdir set ln 0 set tail [file tail $filename] section_comment "Begin file $tail" if {$linemacros} {puts $out "#line 1 \"$filename\""} - set in [open $filename r] + set in [open $filename rb] set varpattern {^[a-zA-Z][a-zA-Z_0-9 *]+(sqlite3[_a-zA-Z0-9]+)(\[|;| =)} set declpattern {([a-zA-Z][a-zA-Z_0-9 ]+ \**)(sqlite3[_a-zA-Z0-9]+)(\(.*)} if {[file extension $filename]==".h"} { @@ -178,16 +258,14 @@ proc copy_file {filename} { } set declpattern ^$declpattern\$ while {![eof $in]} { - set line [gets $in] + set line [string trimright [gets $in]] incr ln if {[regexp {^\s*#\s*include\s+["<]([^">]+)[">]} $line all hdr]} { if {[info exists available_hdr($hdr)]} { if {$available_hdr($hdr)} { - if {$hdr!="os_common.h" && $hdr!="hwtime.h"} { - set available_hdr($hdr) 0 - } + set available_hdr($hdr) 0 section_comment "Include $hdr in the middle of $tail" - copy_file tsrc/$hdr + copy_file $srcdir/$hdr section_comment "Continuing where we left off in $tail" if {$linemacros} {puts $out "#line [expr {$ln+1}] \"$filename\""} } else { @@ -220,21 +298,31 @@ proc copy_file {filename} { if {![info exists varonly_hdr($tail)] && [regexp $declpattern $line all rettype funcname rest]} { regsub {^SQLITE_API } $line {} line + regsub {^SQLITE_API } $rettype {} rettype + # Add the SQLITE_PRIVATE or SQLITE_API keyword before functions. # so that linkage can be modified at compile-time. - if {[regexp {^sqlite3(_|rbu_)} $funcname]} { + if {[regexp {^sqlite3[a-z]*_} $funcname]} { set line SQLITE_API append line " " [string trim $rettype] if {[string index $rettype end] ne "*"} { append line " " } - if {[lsearch -exact $cdecllist $funcname] >= 0} { - append line SQLITE_CDECL + if {$useapicall} { + if {[lsearch -exact $cdecllist $funcname] >= 0} { + append line SQLITE_CDECL " " + } else { + append line SQLITE_APICALL " " + } + } + append line $funcname $rest + if {$funcname=="sqlite3_sourceid"} { + # The sqlite3_sourceid() routine is synthesized at the end of + # the amalgamation + puts $out "/* $line */" } else { - append line SQLITE_STDCALL + puts $out $line } - append line " " $funcname $rest - puts $out $line } else { puts $out "SQLITE_PRIVATE $line" } @@ -242,7 +330,8 @@ proc copy_file {filename} { # Add the SQLITE_PRIVATE before variable declarations or # definitions for internal use regsub {^SQLITE_API } $line {} line - if {![regexp {^sqlite3_} $varname]} { + if {![regexp {^sqlite3_} $varname] + && ![regexp {^sqlite3Show[A-Z]} $varname]} { regsub {^extern } $line {} line puts $out "SQLITE_PRIVATE $line" } else { @@ -270,23 +359,32 @@ proc copy_file {filename} { section_comment "End of $tail" } +# Read the source file named $filename and write it into the +# sqlite3.c output file. The only transformation is the trimming +# of EOL whitespace. +# +proc copy_file_verbatim {filename} { + global out + set in [open $filename rb] + set tail [file tail $filename] + section_comment "Begin EXTRA_SRC file $tail" + while {![eof $in]} { + set line [string trimright [gets $in]] + puts $out $line + } + section_comment "End of EXTRA_SRC $tail" +} # Process the source files. Process files containing commonly # used subroutines first in order to help the compiler find # inlining opportunities. # - -foreach file { +set flist { sqliteInt.h - - crypto.c - crypto_impl.c - crypto_libtomcrypt.c - crypto_openssl.c - crypto_cc.c + os_common.h + ctime.c global.c - ctime.c status.c date.c os.c @@ -311,8 +409,10 @@ foreach file { hash.c opcodes.c + os_kv.c os_unix.c os_win.c + memdb.c bitvec.c pcache.c @@ -332,9 +432,13 @@ foreach file { vdbe.c vdbeblob.c vdbesort.c - journal.c + vdbevtab.c memjournal.c + sqlcipher.c + crypto_openssl.c + crypto_cc.c + walker.c resolve.c expr.c @@ -356,11 +460,13 @@ foreach file { table.c trigger.c update.c + upsert.c vacuum.c vtab.c wherecode.c whereexpr.c where.c + window.c parse.c @@ -383,16 +489,35 @@ foreach file { fts3_unicode.c fts3_unicode2.c + json.c rtree.c icu.c fts3_icu.c sqlite3rbu.c dbstat.c - json1.c + dbpage.c + carray.c + sqlite3session.c fts5.c -} { - copy_file tsrc/$file + stmt.c } +if {$enable_recover} { + lappend flist sqlite3recover.c dbdata.c +} +foreach file $flist { + copy_file $srcdir/$file +} +foreach file $extrasrc { + copy_file_verbatim $file +} + +puts $out \ +"/* Return the source-id for this library */ +SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }" + +puts $out \ +"#endif /* SQLITE_AMALGAMATION */ +/************************** End of sqlite3.c ******************************/" close $out diff --git a/tool/mksqlite3h.tcl b/tool/mksqlite3h.tcl index 3f59aef467..c661cd7a33 100644 --- a/tool/mksqlite3h.tcl +++ b/tool/mksqlite3h.tcl @@ -10,69 +10,146 @@ # # Run this script by specifying the root directory of the source tree # on the command-line. -# +# # This script performs processing on src/sqlite.h.in. It: # # 1) Adds SQLITE_EXTERN in front of the declaration of global variables, # 2) Adds SQLITE_API in front of the declaration of API functions, -# 3) Replaces the string --VERS-- with the current library version, +# 3) Replaces the string --VERS-- with the current library version, # formatted as a string (e.g. "3.6.17"), and # 4) Replaces the string --VERSION-NUMBER-- with current library version, # formatted as an integer (e.g. "3006017"). -# 5) Replaces the string --SOURCE-ID-- with the date and time and sha1 +# 5) Replaces the string --SOURCE-ID-- with the date and time and sha1 # hash of the fossil-scm manifest for the source tree. +# 6) Adds the SQLITE_CALLBACK calling convention macro in front of all +# callback declarations. # -# This script outputs to stdout. +# This script outputs to stdout unless the -o FILENAME option is used. # # Example usage: # -# tclsh mksqlite3h.tcl ../sqlite >sqlite3.h +# tclsh mksqlite3h.tcl ../sqlite [OPTIONS] +# ^^^^^^^^^ +# Root of source tree +# +# Where options are: +# +# --enable-recover Include the sqlite3recover extension +# -o FILENAME Write results to FILENAME instead of stdout +# --useapicall SQLITE_APICALL instead of SQLITE_CDECL # +# Default output stream +set out stdout # Get the source tree root directory from the command-line # set TOP [lindex $argv 0] +# If the -o FILENAME option is present, use FILENAME for output. +# +set x [lsearch $argv -o] +if {$x>0} { + incr x + set out [open [lindex $argv $x] wb] +} + +# Enable use of SQLITE_APICALL macros at the right points? +# +set useapicall 0 + +# Include sqlite3recover.h? +# +set enable_recover 0 + +# Process command-line arguments +if {[lsearch -regexp [lrange $argv 1 end] {^-+useapicall}] != -1} { + set useapicall 1 +} +if {[lsearch -regexp [lrange $argv 1 end] {^-+enable-recover}] != -1} { + set enable_recover 1 +} + # Get the SQLite version number (ex: 3.6.18) from the $TOP/VERSION file. # -set in [open $TOP/VERSION] +set in [open [file normalize $TOP/VERSION] rb] set zVersion [string trim [read $in]] close $in set nVersion [eval format "%d%03d%03d" [split $zVersion .]] -# Get the fossil-scm version number from $TOP/manifest.uuid. +# Get the source-id # -set in [open $TOP/manifest.uuid] -set zUuid [string trim [read $in]] -close $in +proc file-content {fn} { + set fd [open $fn rb] + set rv [string trim [read $fd]] + close $fd + return $rv +} -# Get the fossil-scm check-in date from the "D" card of $TOP/manifest. -# -set in [open $TOP/manifest] -set zDate {} -while {![eof $in]} { - set line [gets $in] - if {[regexp {^D (2[-0-9T:]+)} $line all date]} { - set zDate [string map {T { }} $date] - break +set PWD [pwd] +cd $TOP +set tmpfile $PWD/tmp-[clock millisec]-[expr {int(rand()*100000000000)}].txt +set mksourceid $PWD/mksourceid +if {![file exists $mksourceid] && [file exists ${mksourceid}.exe]} { + # Workaround for Windows-based Unix-like environments + # https://sqlite.org/forum/forumpost/41ba710dd9943453 + set mksourceid ${mksourceid}.exe +} +exec $mksourceid manifest > $tmpfile +set zSourceId [file-content $tmpfile] +file delete -force $tmpfile +cd $PWD + +# Collect SQLITE_SCM_BRANCH, SQLITE_SCM_TAGS, and SQLITE_SCM_DATETIME +set zVerTime [lindex [lindex [split [file-content $TOP/manifest] "\n"] 1] 1]Z; # D-card +if {![file exists $TOP/manifest.tags]} { + puts stderr "WARNING: building sqlite3.h without manifest.tags, which is generated by the SCM." + puts stderr "This means that we cannot record the tag/branch info. We will continue with " + puts stderr "a placeholder value. To remedy this, run the following command from a " + puts stderr "check-out:\n" + puts stderr " fossil set manifest urt\n" + set zSourceId [string range $zSourceId 0 end-13]-experimental; # Keep SHA3 hash length + set zBranch "unknown" + set zTags "unknown" +} else { + # Read the list of branch/tags from manifest.tags + set content [file-content $TOP/manifest.tags]; + set zTags {} + foreach {x tag} [lassign $content - zBranch] { + if {$tag eq $zBranch} continue + lappend zTags $tag } } -close $in # Set up patterns for recognizing API declarations. # set varpattern {^[a-zA-Z][a-zA-Z_0-9 *]+sqlite3_[_a-zA-Z0-9]+(\[|;| =)} -set declpattern {^ *([a-zA-Z][a-zA-Z_0-9 ]+ \**)(sqlite3_[_a-zA-Z0-9]+)(\(.*)$} +set declpattern1 {^ *([a-zA-Z][a-zA-Z_0-9 ]+ \**)(sqlite3_[_a-zA-Z0-9]+)(\(.*)$} + +set declpattern2 \ + {^ *([a-zA-Z][a-zA-Z_0-9 ]+ \**)(sqlite3session_[_a-zA-Z0-9]+)(\(.*)$} + +set declpattern3 \ + {^ *([a-zA-Z][a-zA-Z_0-9 ]+ \**)(sqlite3changeset_[_a-zA-Z0-9]+)(\(.*)$} + +set declpattern4 \ + {^ *([a-zA-Z][a-zA-Z_0-9 ]+ \**)(sqlite3changegroup_[_a-zA-Z0-9]+)(\(.*)$} + +set declpattern5 \ + {^ *([a-zA-Z][a-zA-Z_0-9 ]+ \**)(sqlite3rebaser_[_a-zA-Z0-9]+)(\(.*)$} # Force the output to use unix line endings, even on Windows. -fconfigure stdout -translation lf +fconfigure stdout -translation binary set filelist [subst { $TOP/src/sqlite.h.in $TOP/ext/rtree/sqlite3rtree.h + $TOP/ext/session/sqlite3session.h $TOP/ext/fts5/fts5.h }] +if {$enable_recover} { + lappend filelist "$TOP/ext/recover/sqlite3recover.h" +} # These are the functions that accept a variable number of arguments. They # always need to use the "cdecl" calling convention even when another calling @@ -90,38 +167,59 @@ set cdecllist { # Process the source files. # foreach file $filelist { - set in [open $file] + set in [open $file rb] + if {![regexp {sqlite\.h\.in} $file]} { + puts $out "/******** Begin file [file tail $file] *********/" + } while {![eof $in]} { - - set line [gets $in] + + set line [string trimright [gets $in]] # File sqlite3rtree.h contains a line "#include <sqlite3.h>". Omit this # line when copying sqlite3rtree.h into sqlite3.h. # if {[string match {*#include*[<"]sqlite3.h[>"]*} $line]} continue - + regsub -- --VERS-- $line $zVersion line regsub -- --VERSION-NUMBER-- $line $nVersion line - regsub -- --SOURCE-ID-- $line "$zDate $zUuid" line + regsub -- --SOURCE-ID-- $line "$zSourceId" line + regsub -- --SCM-BRANCH-- $line "$zBranch" line + regsub -- --SCM-TAGS-- $line "$zTags" line + regsub -- --SCM-DATETIME-- $line "$zVerTime" line if {[regexp $varpattern $line] && ![regexp {^ *typedef} $line]} { set line "SQLITE_API $line" } else { - if {[regexp $declpattern $line all rettype funcname rest]} { + if {[regexp $declpattern1 $line all rettype funcname rest] || \ + [regexp $declpattern2 $line all rettype funcname rest] || \ + [regexp $declpattern3 $line all rettype funcname rest] || \ + [regexp $declpattern4 $line all rettype funcname rest] || \ + [regexp $declpattern5 $line all rettype funcname rest]} { set line SQLITE_API append line " " [string trim $rettype] if {[string index $rettype end] ne "*"} { append line " " } - if {[lsearch -exact $cdecllist $funcname] >= 0} { - append line SQLITE_CDECL - } else { - append line SQLITE_STDCALL + if {$useapicall} { + if {[lsearch -exact $cdecllist $funcname] >= 0} { + append line SQLITE_CDECL " " + } else { + append line SQLITE_APICALL " " + } } - append line " " $funcname $rest + append line $funcname $rest } } - puts $line + if {$useapicall} { + set line [string map [list (*sqlite3_syscall_ptr) \ + "(SQLITE_SYSAPI *sqlite3_syscall_ptr)"] $line] + regsub {\(\*} $line {(SQLITE_CALLBACK *} line + } + puts $out $line } close $in + if {![regexp {sqlite\.h\.in} $file]} { + puts $out "/******** End of [file tail $file] *********/" + } } +puts $out "#endif /* SQLITE3_H */" diff --git a/tool/mksqlite3internalh.tcl b/tool/mksqlite3internalh.tcl index 8db593fe75..e1a42ee776 100644 --- a/tool/mksqlite3internalh.tcl +++ b/tool/mksqlite3internalh.tcl @@ -92,7 +92,7 @@ proc section_comment {text} { # Read the source file named $filename and write it into the # sqlite3.c output file. If any #include statements are seen, -# process them approprately. +# process them appropriately. # proc copy_file {filename} { global seen_hdr available_hdr out diff --git a/tool/mksrczip.tcl b/tool/mksrczip.tcl new file mode 100644 index 0000000000..4431c3d666 --- /dev/null +++ b/tool/mksrczip.tcl @@ -0,0 +1,14 @@ +#!/usr/bin/tclsh +# +# Build a ZIP archive for the complete, unedited source code that +# corresponds to the current check-out. +# +set VERSION-file [file dirname [file dirname [file normalize $argv0]]]/VERSION +set fd [open ${VERSION-file} rb] +set vers [read $fd] +close $fd +scan $vers %d.%d.%d major minor patch +set numvers [format {3%02d%02d00} $minor $patch] +set cmd "fossil zip current sqlite-src-$numvers.zip --name sqlite-src-$numvers" +puts $cmd +exec {*}$cmd diff --git a/tool/mktoolzip.tcl b/tool/mktoolzip.tcl new file mode 100644 index 0000000000..041bf28cdd --- /dev/null +++ b/tool/mktoolzip.tcl @@ -0,0 +1,105 @@ +#!/usr/bin/tclsh +# +# Run this script in order to generate a ZIP archive containing various +# command-line tools. +# +# The makefile that invokes this script must first build the following +# binaries: +# +# testfixture -- used to run this script +# sqlite3 -- the SQLite CLI +# sqldiff -- Program to diff two databases +# sqlite3_analyzer -- Space analyzer +# sqlite3_rsync -- Remote db sync +# +# On Windows, add: +# +# sqlite3.def +# sqlite3.dll +# +# Add the --snapshot option to generate a snapshot ZIP archive instead of +# a release ZIP archive. +# +set bSnapshot 0 +for {set i 0} {$i<[llength $argv]} {incr i} { + set a [lindex $argv $i] + if {$a eq "-snapshot" || $a eq "--snapshot"} { + set bSnapshot 1 + continue + } + puts stderr "unknown argument: $a" + exit 1 +} +switch $tcl_platform(os) { + {Windows NT} { + set OS win + set EXE .exe + } + Linux { + set OS linux + set EXE {} + } + Darwin { + set OS osx + set EXE {} + } + default { + set OS unknown + set EXE {} + } +} +switch $tcl_platform(machine) { + arm64 { + set ARCH arm64 + } + x86_64 { + set ARCH x64 + } + amd64 - + intel { + if {$tcl_platform(pointerSize)==4} { + set ARCH x86 + } else { + set ARCH x64 + } + } + default { + set ARCH unk + } +} +if {$bSnapshot} { + set in [open [file join [file dirname [file dirname [info script]]] manifest]] + set manifest [read $in] + close $in + regexp {\nD (.{16})} $manifest all date + regsub -all {[-:T]} $date {} v2 +} else { + set in [open [file join [file dirname [file dirname [info script]]] VERSION]] + set vers [read $in] + close $in + scan $vers %d.%d.%d v1 v2 v3 + set v2 [format 3%02d%02d00 $v2 $v3] +} + +set name sqlite-tools-$OS-$ARCH-$v2.zip +set filelist "sqlite3$EXE sqldiff$EXE sqlite3_analyzer$EXE sqlite3_rsync$EXE" +proc make_zip_archive {name filelist} { + file delete -force $name + puts "fossil test-filezip $name $filelist" + if {[catch {exec fossil test-filezip $name {*}$filelist}]} { + puts "^--- Unable. Trying again as:" + puts "zip $name $filelist" + file delete -force $name + exec zip $name {*}$filelist + } + puts "$name: [file size $name] bytes" +} +make_zip_archive $name $filelist + +# On Windows, also try to construct a DLL +# +if {$OS eq "win" && [file exists sqlite3.dll] && [file exists sqlite3.def]} { + set name sqlite-dll-win-$ARCH-$v2.zip + set filelist [list sqlite3.def sqlite3.dll] + make_zip_archive $name $filelist +} diff --git a/tool/mkvsix.tcl b/tool/mkvsix.tcl index a14fd230d9..0663213639 100644 --- a/tool/mkvsix.tcl +++ b/tool/mkvsix.tcl @@ -77,7 +77,7 @@ # command prompt: # # CD /D C:\dev\sqlite\core -# tclsh85 tool\mkvsix.tcl C:\Temp +# tclsh tool\mkvsix.tcl C:\Temp # # In the example above, "C:\dev\sqlite\core" represents the root of the source # tree for SQLite and "C:\Temp" represents the top-level directory containing @@ -156,7 +156,7 @@ proc readFile { fileName } { # may contain binary data. # set file_id [open $fileName RDONLY] - fconfigure $file_id -encoding binary -translation binary + fconfigure $file_id -translation binary set result [read $file_id] close $file_id return $result @@ -168,7 +168,7 @@ proc writeFile { fileName data } { # binary data. # set file_id [open $fileName {WRONLY CREAT TRUNC}] - fconfigure $file_id -encoding binary -translation binary + fconfigure $file_id -translation binary puts -nonewline $file_id $data close $file_id return "" @@ -463,7 +463,7 @@ if {[string equal $packageFlavor WinRT]} then { } set shortName $shortNames($packageFlavor,$vsVersion) set displayName $displayNames($packageFlavor,$vsVersion) - set targetPlatformIdentifier UWP + set targetPlatformIdentifier UAP; # NOTE: Not "UWP". set targetPlatformVersion v0.8.0.0 set minVsVersion [getMinVsVersionXmlChunk $vsVersion] set maxPlatformVersion \ diff --git a/tool/mkwinarm64ec.tcl b/tool/mkwinarm64ec.tcl new file mode 100644 index 0000000000..388dd47a83 --- /dev/null +++ b/tool/mkwinarm64ec.tcl @@ -0,0 +1,45 @@ +# Script to build ARM64-EC binaries on Windows. +# +# Run from the top-level of a check-out, on a Windows11 ARM machine that +# has Fossil installed, using a command like this: +# +# tclsh tool/mmkwinarm64ec.tcl +# +# Output will be a file named: +# +# sqlite-win-arm64ec-3MMPP00.zip +# +# Where MM is the minor version number and PP is that patch level. +# +puts "Running nmake..." +flush stdout +exec nmake /f Makefile.msc "PLATFORM=ARM64EC" "BCC=cl -nologo -arm64EC" "TCC=cl -nologo -arm64EC -I." "CFLAGS=-nologo -arm64EC -I." clean sqlite3.exe sqldiff.exe sqlite3_rsync.exe sqlite3.dll >@ stdout 2>@ stderr + +proc check-type {file} { + set in [open "| link /dump /headers $file" rb] + set txt [read $in] + close $in + if {![string match {*8664 machine (x64) (ARM64X)*} $txt]} { + puts "$file is not an ARM64-EC binary" + puts "OUTPUT:\n$txt" + flush stdout + exit 1 + } + puts "Confirmed: $file is an ARM64-EC binary" +} +check-type sqlite3.exe +check-type sqldiff.exe +check-type sqlite3_rsync.exe +check-type sqlite3.dll + +set in [open VERSION rb] +set VERSION [read $in] +close $in +regexp {3.(\d+).(\d+)} $VERSION all minor patch +set filename [format sqlite-win-arm64ec-3%02d%02d00.zip $minor $patch] + +puts "Constructing $filename..." +file delete -force $filename +exec fossil test-filezip $filename sqlite3.def sqlite3.dll sqlite3.exe sqldiff.exe sqlite3_rsync.exe >@ stdout 2>@ stderr +puts "$filename: [file size $filename] bytes" +exec fossil test-filezip -l $filename >@ stdout 2>@ stderr diff --git a/tool/offsets.c b/tool/offsets.c index 8e098e71cb..26ee9fcef2 100644 --- a/tool/offsets.c +++ b/tool/offsets.c @@ -75,7 +75,7 @@ static void ofstRootAndColumn( ofstError(p, "cannot open database file \"%s\"", zFile); goto rootAndColumn_exit; } - zSql = sqlite3_mprintf("SELECT rootpage FROM sqlite_master WHERE name=%Q", + zSql = sqlite3_mprintf("SELECT rootpage FROM sqlite_schema WHERE name=%Q", zTable); rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); if( rc ) ofstError(p, "%s: [%s]", sqlite3_errmsg(db), zSql); diff --git a/tool/omittest-msvc.tcl b/tool/omittest-msvc.tcl new file mode 100644 index 0000000000..28c4b98317 --- /dev/null +++ b/tool/omittest-msvc.tcl @@ -0,0 +1,99 @@ +# Run this TCL script in order to build using MSVC multiple times +# with various compile-time options. Use this to verify that the various +# compile-time options all work with MSVC. +# +set OPTIONS [list \ + SQLITE_ALLOW_ROWID_IN_VIEW \ + SQLITE_ENABLE_COLUMN_METADATA \ + SQLITE_ENABLE_EXPENSIVE_ASSERT \ + SQLITE_ENABLE_IOTRACE \ + SQLITE_ENABLE_MEMORY_MANAGEMENT \ + SQLITE_ENABLE_MEMSYS3 \ + SQLITE_ENABLE_MEMSYS5 \ + SQLITE_ENABLE_OVERSIZE_CELL_CHECK \ + SQLITE_ENABLE_UNLOCK_NOTIFY \ + SQLITE_ENABLE_UPDATE_DELETE_LIMIT \ + SQLITE_OMIT_ALTERTABLE-x \ + SQLITE_OMIT_ATTACH-x \ + SQLITE_OMIT_AUTHORIZATION \ + SQLITE_OMIT_AUTOINCREMENT \ + SQLITE_OMIT_AUTOINIT \ + SQLITE_OMIT_AUTOMATIC_INDEX \ + SQLITE_OMIT_AUTORESET \ + SQLITE_OMIT_AUTOVACUUM \ + SQLITE_OMIT_BETWEEN_OPTIMIZATION \ + SQLITE_OMIT_BLOB_LITERAL \ + SQLITE_OMIT_BTREECOUNT \ + SQLITE_OMIT_CASE_SENSITIVE_LIKE_PRAGMA \ + SQLITE_OMIT_CAST \ + SQLITE_OMIT_CHECK \ + SQLITE_OMIT_COMPILEOPTION_DIAGS \ + SQLITE_OMIT_COMPLETE \ + SQLITE_OMIT_COMPOUND_SELECT \ + SQLITE_OMIT_CONFLICT_CLAUSE \ + SQLITE_OMIT_CTE \ + SQLITE_OMIT_DATETIME_FUNCS \ + SQLITE_OMIT_DECLTYPE \ + SQLITE_OMIT_DEPRECATED \ + SQLITE_OMIT_DESERIALIZE \ + SQLITE_OMIT_DISKIO-x \ + SQLITE_OMIT_EXPLAIN-x \ + SQLITE_OMIT_FLAG_PRAGMAS \ + SQLITE_OMIT_FLOATING_POINT \ + SQLITE_OMIT_FOREIGN_KEY \ + SQLITE_OMIT_GENERATED_COLUMNS \ + SQLITE_OMIT_GET_TABLE \ + SQLITE_OMIT_HEX_INTEGER \ + SQLITE_OMIT_INCRBLOB-x \ + SQLITE_OMIT_INTEGRITY_CHECK \ + SQLITE_OMIT_INTROSPECTION_PRAGMAS \ + SQLITE_OMIT_JSON \ + SQLITE_OMIT_LIKE_OPTIMIZATION \ + SQLITE_OMIT_LOAD_EXTENSION \ + SQLITE_OMIT_LOCALTIME \ + SQLITE_OMIT_LOOKASIDE \ + SQLITE_OMIT_MEMORYDB \ + SQLITE_OMIT_OR_OPTIMIZATION \ + SQLITE_OMIT_PAGER_PRAGMAS-x \ + SQLITE_OMIT_PARSER_TRACE \ + SQLITE_OMIT_POPEN \ + SQLITE_OMIT_PRAGMA-x \ + SQLITE_OMIT_PROGRESS_CALLBACK \ + SQLITE_OMIT_QUICKBALANCE \ + SQLITE_OMIT_RANDOMNESS \ + SQLITE_OMIT_REINDEX-x \ + SQLITE_OMIT_SCHEMA_PRAGMAS \ + SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS \ + SQLITE_OMIT_SHARED_CACHE \ + SQLITE_OMIT_SHUTDOWN_DIRECTORIES \ + SQLITE_OMIT_SUBQUERY-x \ + SQLITE_OMIT_TCL_VARIABLE \ + SQLITE_OMIT_TEMPDB \ + SQLITE_OMIT_TEST_CONTROL \ + SQLITE_OMIT_TRACE \ + SQLITE_OMIT_TRIGGER \ + SQLITE_OMIT_TRUNCATE_OPTIMIZATION \ + SQLITE_OMIT_UPSERT \ + SQLITE_OMIT_UTF16 \ + SQLITE_OMIT_VACUUM-x \ + SQLITE_OMIT_VIEW-x \ + SQLITE_OMIT_VIRTUALTABLE-x \ + SQLITE_OMIT_WAL \ + SQLITE_OMIT_WINDOWFUNC-x \ + SQLITE_OMIT_WSD \ + SQLITE_OMIT_XFER_OPT \ +] +set start [lindex $argv 0] +foreach opt $OPTIONS { + if {[regexp x $opt]} continue + if {[string compare $opt $start]<0} continue + exec fossil clean -x + set cmd "nmake /f Makefile.msc USE_FULLWARN=0 OPTIMIZATIONS=0" + append cmd " CCOPTS=-D$opt" + puts $cmd + set res [catch {eval exec $cmd} outtxt] + if {[regexp {sqlite3.c.\d+.: error} $outtxt]} { + puts "FAILED:\n$outtxt" + exit + } +} diff --git a/tool/omittest.tcl b/tool/omittest.tcl index 5437f2eb01..0452a4c6f6 100644 --- a/tool/omittest.tcl +++ b/tool/omittest.tcl @@ -1,299 +1,220 @@ - -set rcsid {$Id: omittest.tcl,v 1.8 2008/10/13 15:35:09 drh Exp $} - -# Documentation for this script. This may be output to stderr +#!/usr/bin/tclsh +# +# Documentation for this script. This may be output to # if the script is invoked incorrectly. +# set ::USAGE_MESSAGE { This Tcl script is used to test the various compile time options -available for omitting code (the SQLITE_OMIT_xxx options). It -should be invoked as follows: - - <script> ?test-symbol? ?-makefile PATH-TO-MAKEFILE? ?-skip_run? - -The default value for ::MAKEFILE is "../Makefile.linux.gcc". - -If -skip_run option is given then only the compile part is attempted. +available for building SQLite, especially options taht omit +features (the SQLITE_OMIT_xxx options). It should be invoked as follows: -This script builds the testfixture program and runs the SQLite test suite -once with each SQLITE_OMIT_ option defined and then once with all options -defined together. Each run is performed in a seperate directory created -as a sub-directory of the current directory by the script. The output -of the build is saved in <sub-directory>/build.log. The output of the -test-suite is saved in <sub-directory>/test.log. + ./configure CFLAGS=-O0 + tclsh test/omittest.tcl -Almost any SQLite makefile (except those generated by configure - see below) -should work. The following properties are required: - - * The makefile should support the "testfixture" target. - * The makefile should support the "test" target. - * The makefile should support the variable "OPTS" as a way to pass - options from the make command line to lemon and the C compiler. - -More precisely, the following two invocations must be supported: - - $::MAKEBIN -f $::MAKEFILE testfixture OPTS="-DSQLITE_OMIT_ALTERTABLE=1" - $::MAKEBIN -f $::MAKEFILE test - -Makefiles generated by the sqlite configure program cannot be used as -they do not respect the OPTS variable. } - -# Build a testfixture executable and run quick.test using it. The first -# parameter is the name of the directory to create and use to run the -# test in. The second parameter is a list of OMIT symbols to define -# when doing so. For example: -# -# run_quick_test /tmp/testdir {SQLITE_OMIT_TRIGGER SQLITE_OMIT_VIEW} +# List of all options to be tested. # -# -proc run_quick_test {dir omit_symbol_list} { - # Compile the value of the OPTS Makefile variable. - set opts "" - if {$::tcl_platform(platform)=="windows"} { - append opts "OPTS += -DSQLITE_OS_WIN=1\n" - set target "testfixture.exe" - } else { - append opts "OPTS += -DSQLITE_OS_UNIX=1\n" - } - foreach sym $omit_symbol_list { - append opts "OPTS += -D${sym}=1\n" - } - - # Create the directory and do the build. If an error occurs return - # early without attempting to run the test suite. - file mkdir $dir - puts -nonewline "Building $dir..." - flush stdout - catch { - file copy -force ./config.h $dir - file copy -force ./libtool $dir - } - set fd [open $::MAKEFILE] - set mkfile [read $fd] - close $fd - regsub {\ninclude} $mkfile "\n$opts\ninclude" mkfile - set fd [open $dir/makefile w] - puts $fd $mkfile - close $fd - - set rc [catch { - exec $::MAKEBIN -C $dir -f makefile clean $::TARGET >& $dir/build.log - }] - if {$rc} { - puts "No good. See $dir/build.log." - return - } else { - puts "Ok" - } - - # Create an empty file "$dir/sqlite3". This is to trick the makefile out - # of trying to build the sqlite shell. The sqlite shell won't build - # with some of the OMIT options (i.e OMIT_COMPLETE). - set sqlite3_dummy $dir/sqlite3 - if {$::tcl_platform(platform)=="windows"} { - append sqlite3_dummy ".exe" - } - if {![file exists $sqlite3_dummy]} { - set wr [open $sqlite3_dummy w] - puts $wr "dummy" - close $wr - } - - if {$::SKIP_RUN} { - puts "Skip testing $dir." - } else { - # Run the test suite. - puts -nonewline "Testing $dir..." - flush stdout - set rc [catch { - exec $::MAKEBIN -C $dir -f makefile test >& $dir/test.log - }] - if {$rc} { - puts "No good. See $dir/test.log." - } else { - puts "Ok" - } - } +set CompileOptionsToTest { + SQLITE_OMIT_ALTERTABLE + SQLITE_OMIT_ANALYZE + SQLITE_OMIT_ATTACH + SQLITE_OMIT_AUTHORIZATION + SQLITE_OMIT_AUTOINCREMENT + SQLITE_OMIT_AUTOINIT + SQLITE_OMIT_AUTOMATIC_INDEX + SQLITE_OMIT_AUTORESET + SQLITE_OMIT_AUTOVACUUM + SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS + SQLITE_OMIT_BETWEEN_OPTIMIZATION + SQLITE_OMIT_BLOB_LITERAL + SQLITE_OMIT_CASE_SENSITIVE_LIKE_PRAGMA + SQLITE_OMIT_CAST + SQLITE_OMIT_CHECK + SQLITE_OMIT_COMPILEOPTION_DIAGS + SQLITE_OMIT_COMPLETE + SQLITE_OMIT_COMPOUND_SELECT + SQLITE_OMIT_CONFLICT_CLAUSE + SQLITE_OMIT_CTE + SQLITE_OMIT_DATETIME_FUNCS + SQLITE_OMIT_DECLTYPE + SQLITE_OMIT_DEPRECATED + SQLITE_OMIT_DESERIALIZE + SQLITE_OMIT_DISKIO + SQLITE_OMIT_EXPLAIN + SQLITE_OMIT_FLAG_PRAGMAS + SQLITE_OMIT_FLOATING_POINT + SQLITE_OMIT_FOREIGN_KEY + SQLITE_OMIT_GENERATED_COLUMNS + SQLITE_OMIT_GET_TABLE + SQLITE_OMIT_HEX_INTEGER + SQLITE_OMIT_INCRBLOB + SQLITE_OMIT_INTEGRITY_CHECK + SQLITE_OMIT_INTROSPECTION_PRAGMAS + SQLITE_OMIT_JSON + SQLITE_OMIT_LIKE_OPTIMIZATION + SQLITE_OMIT_LOAD_EXTENSION + SQLITE_OMIT_LOCALTIME + SQLITE_OMIT_LOOKASIDE + SQLITE_OMIT_MEMORYDB + SQLITE_OMIT_OR_OPTIMIZATION + SQLITE_OMIT_PAGER_PRAGMAS + SQLITE_OMIT_PARSER_TRACE + SQLITE_OMIT_POPEN + SQLITE_OMIT_PRAGMA + SQLITE_OMIT_PROGRESS_CALLBACK + SQLITE_OMIT_QUICKBALANCE + SQLITE_OMIT_RANDOMNESS + SQLITE_OMIT_REINDEX + SQLITE_OMIT_SCHEMA_PRAGMAS + SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS + SQLITE_OMIT_SHARED_CACHE + SQLITE_OMIT_SHUTDOWN_DIRECTORIES + SQLITE_OMIT_SUBQUERY + SQLITE_OMIT_TCL_VARIABLE + SQLITE_OMIT_TEMPDB + SQLITE_OMIT_TEST_CONTROL + SQLITE_OMIT_TRACE + SQLITE_OMIT_TRIGGER + SQLITE_OMIT_TRUNCATE_OPTIMIZATION + SQLITE_OMIT_TWOSIZE_LOOKASIDE + SQLITE_OMIT_UPSERT + SQLITE_OMIT_UTF + SQLITE_OMIT_VACUUM + SQLITE_OMIT_VIEW + SQLITE_OMIT_VIRTUALTABLE + SQLITE_OMIT_WAL + SQLITE_OMIT_WINDOWFUNC + SQLITE_OMIT_WSD + SQLITE_OMIT_XFER_OPT + SQLITE_ALLOW_ROWID_IN_VIEW + SQLITE_DISABLE_DIRSYNC + SQLITE_DISABLE_FTS + SQLITE_DISABLE_INTRINSIC + SQLITE_DISABLE_LFS + SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS + SQLITE_DISABLE_SKIPAHEAD_DISTINCT + SQLITE_ENABLE_API_ARMOR + SQLITE_ENABLE_ATOMIC_WRITE + SQLITE_ENABLE_BATCH_ATOMIC_WRITE + SQLITE_ENABLE_BYTECODE_VTAB + SQLITE_ENABLE_CEROD + SQLITE_ENABLE_COLUMN_METADATA + SQLITE_ENABLE_COLUMN_USED_MASK + SQLITE_ENABLE_COMMENTS + SQLITE_ENABLE_CORRUPT_PGNO + SQLITE_ENABLE_COSTMULT + SQLITE_ENABLE_CURSOR_HINTS + SQLITE_ENABLE_DBPAGE_VTAB + SQLITE_ENABLE_DBSTAT_VTAB + SQLITE_ENABLE_EXPENSIVE_ASSERT + SQLITE_ENABLE_EXPLAIN_COMMENTS + SQLITE_ENABLE_FTS + SQLITE_ENABLE_GEOPOLY + SQLITE_ENABLE_HIDDEN_COLUMNS + SQLITE_ENABLE_ICU + SQLITE_ENABLE_ICU_COLLATIONS + SQLITE_ENABLE_INTERNAL_FUNCTIONS + SQLITE_ENABLE_IOTRACE + SQLITE_ENABLE_LOAD_EXTENSION + SQLITE_ENABLE_LOCKING_STYLE + SQLITE_ENABLE_MATH_FUNCTIONS + SQLITE_ENABLE_MEMORY_MANAGEMENT + SQLITE_ENABLE_MEMSYS + SQLITE_ENABLE_MODULE_COMMENTS + SQLITE_ENABLE_MULTIPLEX + SQLITE_ENABLE_MULTITHREADED_CHECKS + SQLITE_ENABLE_NORMALIZE + SQLITE_ENABLE_NULL_TRIM + SQLITE_ENABLE_OFFSET_SQL_FUNC + SQLITE_ENABLE_OVERSIZE_CELL_CHECK + SQLITE_ENABLE_PREUPDATE_HOOK + SQLITE_ENABLE_QPSG + SQLITE_ENABLE_RBU + SQLITE_ENABLE_RTREE + SQLITE_ENABLE_SELECTTRACE + SQLITE_ENABLE_SESSION + SQLITE_ENABLE_SETLK_TIMEOUT + SQLITE_ENABLE_SNAPSHOT + SQLITE_ENABLE_SORTER_MMAP + SQLITE_ENABLE_SORTER_REFERENCE + SQLITE_ENABLE_SORTER_REFERENCES + SQLITE_ENABLE_SQLLOG + SQLITE_ENABLE_STAT + SQLITE_ENABLE_STMT_SCANSTATUS + SQLITE_ENABLE_STMTVTAB + SQLITE_ENABLE_TREETRACE + SQLITE_ENABLE_UNKNOWN_FUNCTION + SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION + SQLITE_ENABLE_UNLOCK_NOTIFY + SQLITE_ENABLE_UPDATE_DELETE_LIMIT + SQLITE_ENABLE_URI_00_ERROR + SQLITE_ENABLE_VFSTRACE + SQLITE_ENABLE_WHERETRACE + SQLITE_ENABLE_ZIPVFS } - -# This proc processes the command line options passed to this script. -# Currently the only option supported is "-makefile", default -# "../Makefile.linux-gcc". Set the ::MAKEFILE variable to the value of this -# option. +# Parse command-line options. # -proc process_options {argv} { - set ::MAKEBIN make ;# Default value - if {$::tcl_platform(platform)=="windows"} { - set ::MAKEFILE ./Makefile ;# Default value on Windows - } else { - set ::MAKEFILE ./Makefile.linux-gcc ;# Default value - } - set ::SKIP_RUN 0 ;# Default to attempt test - set ::TARGET testfixture ;# Default thing to build - - for {set i 0} {$i < [llength $argv]} {incr i} { - switch -- [lindex $argv $i] { - -makefile { - incr i - set ::MAKEFILE [lindex $argv $i] - } - - -nmake { - set ::MAKEBIN nmake - set ::MAKEFILE ./Makefile.msc - } - - -target { - incr i - set ::TARGET [lindex $argv $i] - } - - -skip_run { - set ::SKIP_RUN 1 - } - - default { - if {[info exists ::SYMBOL]} { - puts stderr [string trim $::USAGE_MESSAGE] - exit -1 - } - set ::SYMBOL [lindex $argv $i] - } +for {set i 0} {$i<[llength $argv]} {incr i} { + set arg [lindex $argv $i] + switch -- $arg { + -start - + --start { + incr i + set startat [lindex $argv $i] } - set ::MAKEFILE [file normalize $::MAKEFILE] } } -# Main routine. +# Additional options required for some settings. # +set More(SQLITE_OMIT_DISKIO) {-DSQLITE_OMIT_WAL} -proc main {argv} { - # List of SQLITE_OMIT_XXX symbols supported by SQLite. - set ::OMIT_SYMBOLS [list \ - SQLITE_OMIT_ALTERTABLE \ - SQLITE_OMIT_ANALYZE \ - SQLITE_OMIT_ATTACH \ - SQLITE_OMIT_AUTHORIZATION \ - SQLITE_OMIT_AUTOINCREMENT \ - SQLITE_OMIT_AUTOINIT \ - SQLITE_OMIT_AUTOMATIC_INDEX \ - SQLITE_OMIT_AUTORESET \ - SQLITE_OMIT_AUTOVACUUM \ - SQLITE_OMIT_BETWEEN_OPTIMIZATION \ - SQLITE_OMIT_BLOB_LITERAL \ - SQLITE_OMIT_BTREECOUNT \ - SQLITE_OMIT_BUILTIN_TEST \ - SQLITE_OMIT_CAST \ - SQLITE_OMIT_CHECK \ - SQLITE_OMIT_COMPILEOPTION_DIAGS \ - SQLITE_OMIT_COMPLETE \ - SQLITE_OMIT_COMPOUND_SELECT \ - SQLITE_OMIT_CTE \ - SQLITE_OMIT_DATETIME_FUNCS \ - SQLITE_OMIT_DECLTYPE \ - SQLITE_OMIT_DEPRECATED \ - SQLITE_OMIT_EXPLAIN \ - SQLITE_OMIT_FLAG_PRAGMAS \ - SQLITE_OMIT_FLOATING_POINT \ - SQLITE_OMIT_FOREIGN_KEY \ - SQLITE_OMIT_GET_TABLE \ - SQLITE_OMIT_INCRBLOB \ - SQLITE_OMIT_INTEGRITY_CHECK \ - SQLITE_OMIT_LIKE_OPTIMIZATION \ - SQLITE_OMIT_LOAD_EXTENSION \ - SQLITE_OMIT_LOCALTIME \ - SQLITE_OMIT_LOOKASIDE \ - SQLITE_OMIT_MEMORYDB \ - SQLITE_OMIT_OR_OPTIMIZATION \ - SQLITE_OMIT_PAGER_PRAGMAS \ - SQLITE_OMIT_PRAGMA \ - SQLITE_OMIT_PROGRESS_CALLBACK \ - SQLITE_OMIT_QUICKBALANCE \ - SQLITE_OMIT_REINDEX \ - SQLITE_OMIT_SCHEMA_PRAGMAS \ - SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS \ - SQLITE_OMIT_SHARED_CACHE \ - SQLITE_OMIT_SUBQUERY \ - SQLITE_OMIT_TCL_VARIABLE \ - SQLITE_OMIT_TEMPDB \ - SQLITE_OMIT_TRACE \ - SQLITE_OMIT_TRIGGER \ - SQLITE_OMIT_TRUNCATE_OPTIMIZATION \ - SQLITE_OMIT_UNIQUE_ENFORCEMENT \ - SQLITE_OMIT_UTF16 \ - SQLITE_OMIT_VACUUM \ - SQLITE_OMIT_VIEW \ - SQLITE_OMIT_VIRTUALTABLE \ - SQLITE_OMIT_WAL \ - SQLITE_OMIT_WSD \ - SQLITE_OMIT_XFER_OPT \ - ] - - set ::ENABLE_SYMBOLS [list \ - SQLITE_DISABLE_DIRSYNC \ - SQLITE_DISABLE_LFS \ - SQLITE_ENABLE_ATOMIC_WRITE \ - SQLITE_ENABLE_COLUMN_METADATA \ - SQLITE_ENABLE_EXPENSIVE_ASSERT \ - SQLITE_ENABLE_FTS3 \ - SQLITE_ENABLE_FTS3_PARENTHESIS \ - SQLITE_ENABLE_FTS4 \ - SQLITE_ENABLE_IOTRACE \ - SQLITE_ENABLE_LOAD_EXTENSION \ - SQLITE_ENABLE_LOCKING_STYLE \ - SQLITE_ENABLE_MEMORY_MANAGEMENT \ - SQLITE_ENABLE_MEMSYS3 \ - SQLITE_ENABLE_MEMSYS5 \ - SQLITE_ENABLE_OVERSIZE_CELL_CHECK \ - SQLITE_ENABLE_RTREE \ - SQLITE_ENABLE_STAT3 \ - SQLITE_ENABLE_UNLOCK_NOTIFY \ - SQLITE_ENABLE_UPDATE_DELETE_LIMIT \ - ] - - # Process any command line options. - process_options $argv - - if {[info exists ::SYMBOL] } { - set sym $::SYMBOL +# Compile-time options for Mac only +# +set MacOnly(SQLITE_ENABLE_LOCKING_STYLE) 1 - if {[lsearch $::OMIT_SYMBOLS $sym]<0 && [lsearch $::ENABLE_SYMBOLS $sym]<0} { - puts stderr "No such symbol: $sym" - exit -1 +# Compile-time options that might fail, depending on what libraries +# are installed. Failures on these tests issue a warning, but testing +# continues. +# +set FailIsOk(SQLITE_ENABLE_ICU) 1 +set FailIsOk(SQLITE_ENABLE_ICU_COLLATIONS) 1 + +file mkdir omittest +foreach sym $CompileOptionsToTest { + if {[info exists startat]} { + if {$startat==$sym} { + unset startat + } else { + continue } - - set dirname "test_[regsub -nocase {^x*SQLITE_} $sym {}]" - run_quick_test $dirname $sym + } + if {[info exists MacOnly($sym)] && $tcl_platform(os)!="Darwin"} { + continue + } + set logfile "omittest/$sym.log" + if {[info exists More($sym)]} { + append opts "OPT_FEATURE_FLAGS=-D$sym $More($sym)" } else { - # First try a test with all OMIT symbols except SQLITE_OMIT_FLOATING_POINT - # and SQLITE_OMIT_PRAGMA defined. The former doesn't work (causes segfaults) - # and the latter is currently incompatible with the test suite (this should - # be fixed, but it will be a lot of work). - set allsyms [list] - foreach s $::OMIT_SYMBOLS { - if {$s!="SQLITE_OMIT_FLOATING_POINT" && $s!="SQLITE_OMIT_PRAGMA"} { - lappend allsyms $s - } - } - run_quick_test test_OMIT_EVERYTHING $allsyms - - # Now try one quick.test with each of the OMIT symbols defined. Included - # are the OMIT_FLOATING_POINT and OMIT_PRAGMA symbols, even though we - # know they will fail. It's good to be reminded of this from time to time. - foreach sym $::OMIT_SYMBOLS { - set dirname "test_[regsub -nocase {^x*SQLITE_} $sym {}]" - run_quick_test $dirname $sym - } - - # Try the ENABLE/DISABLE symbols one at a time. - # We don't do them all at once since some are conflicting. - foreach sym $::ENABLE_SYMBOLS { - set dirname "test_[regsub -nocase {^x*SQLITE_} $sym {}]" - run_quick_test $dirname $sym + set opts OPT_FEATURE_FLAGS=-D$sym + } + puts "make tidy sqlite3.o $opts" + if {[catch {exec make tidy sqlite3.o $opts >& $logfile}]} { + puts "BUILD FAILED: see $logfile for details" + if {[info exists FailIsOk($sym)]} { + set Failure($sym) 1 + } else { + puts "Note: After fixes, continue the test using:\n" + puts " [info nameofexe] $argv0 --start $sym\n" + exit 1 } } } - -main $argv +if {[llength [array names Failure]]>0} { + puts "BUILD FAILED on the following:" + foreach sym [array names Failure] { + puts " * $sym" + } +} diff --git a/tool/opcodesum.tcl b/tool/opcodesum.tcl new file mode 100644 index 0000000000..47dff32b90 --- /dev/null +++ b/tool/opcodesum.tcl @@ -0,0 +1,34 @@ +#!/usr/bin/tclsh +# +# Run this script, redirecting input from cachegrind output, to compute the +# number of CPU cycles used by each VDBE opcode. +# +# The cachegrind output should be configured so that it reports a single +# column of Ir at the left margin. Ex: +# +# cg_annotation --show=Ir --auto=yes cachegrind.out.* | tclsh opcodesum.tcl +# +set currentop x +set ncycle(x) 0 +while {![eof stdin]} { + set line [string map {\173 x \175 x \042 x} [gets stdin]] + if {[regexp { \. case OP_.*:} $line]} { + regexp {OP_(.+):} $line all currentop + set ncycle($currentop) 0 + } elseif {[lindex $line 1]=="default:" + && [regexp {really OP_Noop and OP_Explain} $line]} { + break + } elseif {[lindex $line 0]!="."} { + regsub -all {[^0-9]} [lindex $line 0] {} n + if {$n!=""} {incr ncycle($currentop) $n} + } +} +unset ncycle(x) +set results {} +foreach op [lsort [array names ncycle]] { + if {$ncycle($op)==0} continue + lappend results [list $ncycle($op) $op] +} +foreach entry [lsort -index 0 -int -decr $results] { + puts [format {%-16s %10d} [lindex $entry 1] [lindex $entry 0]] +} diff --git a/tool/pagesig.c b/tool/pagesig.c index 540c9d7226..37cc804a53 100644 --- a/tool/pagesig.c +++ b/tool/pagesig.c @@ -26,7 +26,7 @@ ** the entire block. ** ** For blocks of more than 16 bytes, the signature is a hex dump of the -** first 8 bytes followed by a 64-bit has of the entire block. +** first 8 bytes followed by a 64-bit hash of the entire block. */ static void vlogSignature(unsigned char *p, int n, char *zCksum){ unsigned int s0 = 0, s1 = 0; diff --git a/tool/replace.tcl b/tool/replace.tcl index b01a83accb..6a462d95ff 100644 --- a/tool/replace.tcl +++ b/tool/replace.tcl @@ -4,16 +4,19 @@ # only lines successfully modified with a regular # expression. # +fconfigure stdout -translation binary +fconfigure stderr -translation binary set mode [string tolower [lindex $argv 0]] set from [lindex $argv 1] set to [lindex $argv 2] -if {$mode ni [list exact include]} {exit 1} +if {-1 == [lsearch -exact [list exact regsub include] $mode]} {exit 1} if {[string length $from]==0} {exit 2} while {![eof stdin]} { set line [gets stdin] if {[eof stdin]} break switch -exact $mode { exact {set line [string map [list $from $to] $line]} + regsub {regsub -all -- $from $line $to line} include {if {[regsub -all -- $from $line $to line]==0} continue} } puts stdout $line diff --git a/tool/restore_jrnl.tcl b/tool/restore_jrnl.tcl index 05af4f9a2a..200f9b1d29 100644 --- a/tool/restore_jrnl.tcl +++ b/tool/restore_jrnl.tcl @@ -114,40 +114,40 @@ proc dump_jrnl_page {jrnl_pgno} { set db_pgno [hexio_get_int [hexio_read $jrnl_name [expr $jrnl_pg_offset] 4]] set chksum [hexio_get_int [hexio_read $jrnl_name [expr $jrnl_pg_offset+4+$db_pgsz] 4]] set nonce [calc_nonce $jrnl_pgno] - puts [ format {jrnl_pg_offset: %08x (%d) jrnl_pgno: %d db_pgno: %d} \ - $jrnl_pg_offset $jrnl_pg_offset \ - $jrnl_pgno $db_pgno] - puts [ format {nonce: %08x chksum: %08x} \ - $nonce $chksum] + puts [ format {jrnl_pg_offset: %08x (%d) jrnl_pgno: %d db_pgno: %d} \ + $jrnl_pg_offset $jrnl_pg_offset \ + $jrnl_pgno $db_pgno] + puts [ format {nonce: %08x chksum: %08x} \ + $nonce $chksum] # now hex dump the data - # This is derived from the Tcler's WIKI - set fid [open $jrnl_name r] - fconfigure $fid -translation binary -encoding binary - seek $fid [expr $jrnl_pg_offset+4] - set data [read $fid $db_pgsz] - close $fid + # This is derived from the Tcler's WIKI + set fid [open $jrnl_name r] + fconfigure $fid -translation binary + seek $fid [expr $jrnl_pg_offset+4] + set data [read $fid $db_pgsz] + close $fid for {set addr 0} {$addr<$db_pgsz} {set addr [expr $addr+16]} { - # get 16 bytes of data - set s [string range $data $addr [expr $addr+16]] - - # Convert the data to hex and to characters. - binary scan $s H*@0a* hex ascii - - # Replace non-printing characters in the data. - regsub -all -- {[^[:graph:] ]} $ascii {.} ascii - - # Split the 16 bytes into two 8-byte chunks - regexp -- {(.{16})(.{0,16})} $hex -> hex1 hex2 - - # Convert the hex to pairs of hex digits - regsub -all -- {..} $hex1 {& } hex1 - regsub -all -- {..} $hex2 {& } hex2 - - # Print the hex and ascii data - puts [ format {%08x %-24s %-24s %-16s} \ - $addr $hex1 $hex2 $ascii ] - } + # get 16 bytes of data + set s [string range $data $addr [expr $addr+16]] + + # Convert the data to hex and to characters. + binary scan $s H*@0a* hex ascii + + # Replace non-printing characters in the data. + regsub -all -- {[^[:graph:] ]} $ascii {.} ascii + + # Split the 16 bytes into two 8-byte chunks + regexp -- {(.{16})(.{0,16})} $hex -> hex1 hex2 + + # Convert the hex to pairs of hex digits + regsub -all -- {..} $hex1 {& } hex1 + regsub -all -- {..} $hex2 {& } hex2 + + # Print the hex and ascii data + puts [ format {%08x %-24s %-24s %-16s} \ + $addr $hex1 $hex2 $ascii ] + } } # Setup for the tests. Make a backup copy of the files. @@ -230,4 +230,3 @@ do_test restore_jrnl-1.0 { catchsql {PRAGMA integrity_check} } {0 ok} db close - diff --git a/tool/run-speed-test.sh b/tool/run-speed-test.sh deleted file mode 100644 index ee2ceac660..0000000000 --- a/tool/run-speed-test.sh +++ /dev/null @@ -1,68 +0,0 @@ -#!/bin/bash -# -# This is a template for a script used for day-to-day size and -# performance monitoring of SQLite. Typical usage: -# -# sh run-speed-test.sh trunk # Baseline measurement of trunk -# sh run-speed-test.sh x1 # Measure some experimental change -# fossil test-diff --tk cout-trunk.txt cout-x1.txt # View chanages -# -# There are multiple output files, all with a base name given by -# the first argument: -# -# summary-$BASE.txt # Copy of standard output -# cout-$BASE.txt # cachegrind output -# explain-$BASE.txt # EXPLAIN listings -# -if test "$1" = "" -then - echo "Usage: $0 OUTPUTFILE [OPTIONS]" - exit -fi -NAME=$1 -shift -CC_OPTS="-DSQLITE_ENABLE_RTREE" -SPEEDTEST_OPTS="--shrink-memory --reprepare" -SIZE=5 -while test "$1" != ""; do - case $1 in - --reprepare) - SPEEDTEST_OPTS="$SPEEDTEST_OPTS $1" - ;; - --autovacuum) - SPEEDTEST_OPTS="$SPEEDTEST_OPTS $1" - ;; - --utf16be) - SPEEDTEST_OPTS="$SPEEDTEST_OPTS $1" - ;; - --without-rowid) - SPEEDTEST_OPTS="$SPEEDTEST_OPTS $1" - ;; - --size) - shift; SIZE=$1 - ;; - *) - CC_OPTS="$CC_OPTS $1" - ;; - esac - shift -done -SPEEDTEST_OPTS="$SPEEDTEST_OPTS --size $SIZE" -echo "NAME = $NAME" | tee summary-$NAME.txt -echo "SPEEDTEST_OPTS = $SPEEDTEST_OPTS" | tee -a summary-$NAME.txt -echo "CC_OPTS = $CC_OPTS" | tee -a summary-$NAME.txt -rm -f cachegrind.out.* speedtest1 speedtest1.db sqlite3.o -gcc -g -Os -Wall -I. $CC_OPTS -c sqlite3.c -size sqlite3.o | tee -a summary-$NAME.txt -gcc -g -Os -Wall -I. $CC_OPTS \ - -DSQLITE_ENABLE_EXPLAIN_COMMENTS \ - ./shell.c ./sqlite3.c -o sqlite3 -ldl -lpthread -SRC=./speedtest1.c -gcc -g -Os -Wall -I. $CC_OPTS $SRC ./sqlite3.o -o speedtest1 -ldl -lpthread -ls -l speedtest1 | tee -a summary-$NAME.txt -valgrind --tool=cachegrind ./speedtest1 speedtest1.db \ - $SPEEDTEST_OPTS 2>&1 | tee -a summary-$NAME.txt -size sqlite3.o | tee -a summary-$NAME.txt -wc sqlite3.c -cg_anno.tcl cachegrind.out.* >cout-$NAME.txt -./speedtest1 --explain $SPEEDTEST_OPTS | ./sqlite3 >explain-$NAME.txt diff --git a/tool/showdb.c b/tool/showdb.c index 06cd36cd2c..f0bd9737cf 100644 --- a/tool/showdb.c +++ b/tool/showdb.c @@ -20,21 +20,22 @@ #include <assert.h> #include "sqlite3.h" +typedef unsigned char u8; /* unsigned 8-bit */ +typedef unsigned int u32; /* unsigned 32-bit */ +typedef sqlite3_int64 i64; /* signed 64-bit */ +typedef sqlite3_uint64 u64; /* unsigned 64-bit */ + static struct GlobalData { - int pagesize; /* Size of a database page */ + i64 pagesize; /* Size of a database page */ int dbfd; /* File descriptor for reading the DB */ - int mxPage; /* Last page number */ + u32 mxPage; /* Last page number */ int perLine; /* HEX elements to print per line */ int bRaw; /* True to access db file via OS APIs */ sqlite3_file *pFd; /* File descriptor for non-raw mode */ sqlite3 *pDb; /* Database handle that owns pFd */ } g = {1024, -1, 0, 16, 0, 0, 0}; - -typedef long long int i64; /* Datatype for 64-bit integers */ - - /* ** Convert the var-int format into i64. Return the number of bytes ** in the var-int. Write the var-int value into *pVal. @@ -54,7 +55,7 @@ static int decodeVarint(const unsigned char *z, i64 *pVal){ /* ** Extract a big-endian 32-bit integer */ -static unsigned int decodeInt32(const unsigned char *z){ +static u32 decodeInt32(const u8 *z){ return (z[0]<<24) + (z[1]<<16) + (z[2]<<8) + z[3]; } @@ -141,7 +142,7 @@ static void fileClose(){ static unsigned char *fileRead(sqlite3_int64 ofst, int nByte){ unsigned char *aData; int got; - aData = sqlite3_malloc(nByte+32); + aData = sqlite3_malloc64(32+(i64)nByte); if( aData==0 ) out_of_memory(); memset(aData, 0, nByte+32); if( g.bRaw==0 ){ @@ -151,7 +152,7 @@ static unsigned char *fileRead(sqlite3_int64 ofst, int nByte){ exit(1); } }else{ - lseek(g.dbfd, ofst, SEEK_SET); + lseek(g.dbfd, (long)ofst, SEEK_SET); got = read(g.dbfd, aData, nByte); if( got>0 && got<nByte ) memset(aData+got, 0, nByte-got); } @@ -161,8 +162,8 @@ static unsigned char *fileRead(sqlite3_int64 ofst, int nByte){ /* ** Return the size of the file in byte. */ -static sqlite3_int64 fileGetsize(void){ - sqlite3_int64 res = 0; +static i64 fileGetsize(void){ + i64 res = 0; if( g.bRaw==0 ){ int rc = g.pFd->pMethods->xFileSize(g.pFd, &res); if( rc!=SQLITE_OK ){ @@ -185,9 +186,9 @@ static sqlite3_int64 fileGetsize(void){ ** Print a range of bytes as hex and as ascii. */ static unsigned char *print_byte_range( - int ofst, /* First byte in the range of bytes to print */ - int nByte, /* Number of bytes to print */ - int printOfst /* Add this amount to the index on the left column */ + sqlite3_int64 ofst, /* First byte in the range of bytes to print */ + int nByte, /* Number of bytes to print */ + int printOfst /* Add this amount to the index on the left column */ ){ unsigned char *aData; int i, j; @@ -207,6 +208,12 @@ static unsigned char *print_byte_range( aData = fileRead(ofst, nByte); for(i=0; i<nByte; i += g.perLine){ + int go = 0; + for(j=0; j<g.perLine; j++){ + if( i+j>nByte ){ break; } + if( aData[i+j] ){ go = 1; break; } + } + if( !go && i>0 && i+g.perLine<nByte ) continue; fprintf(stdout, zOfstFmt, i+printOfst); for(j=0; j<g.perLine; j++){ if( i+j>nByte ){ @@ -230,18 +237,18 @@ static unsigned char *print_byte_range( /* ** Print an entire page of content as hex */ -static void print_page(int iPg){ - int iStart; +static void print_page(u32 iPg){ + i64 iStart; unsigned char *aData; - iStart = (iPg-1)*g.pagesize; - fprintf(stdout, "Page %d: (offsets 0x%x..0x%x)\n", + iStart = ((i64)(iPg-1))*g.pagesize; + fprintf(stdout, "Page %u: (offsets 0x%llx..0x%llx)\n", iPg, iStart, iStart+g.pagesize-1); aData = print_byte_range(iStart, g.pagesize, 0); sqlite3_free(aData); } -/* Print a line of decode output showing a 4-byte integer. +/* Print a line of decoded output showing a 4-byte unsigned integer. */ static void print_decode_line( unsigned char *aData, /* Content being decoded */ @@ -249,7 +256,7 @@ static void print_decode_line( const char *zMsg /* Message to append */ ){ int i, j; - int val = aData[ofst]; + u32 val = aData[ofst]; char zBuf[100]; sprintf(zBuf, " %03x: %02x", ofst, aData[ofst]); i = (int)strlen(zBuf); @@ -262,7 +269,7 @@ static void print_decode_line( } i += (int)strlen(&zBuf[i]); } - sprintf(&zBuf[i], " %9d", val); + sprintf(&zBuf[i], " %10u", val); printf("%s %s\n", zBuf, zMsg); } @@ -296,6 +303,7 @@ static void print_db_header(void){ print_decode_line(aData, 88, 4, "meta[12]"); print_decode_line(aData, 92, 4, "Change counter for version number"); print_decode_line(aData, 96, 4, "SQLite version number"); + sqlite3_free(aData); } /* @@ -408,7 +416,7 @@ static i64 describeCell( int i; i64 nDesc = 0; int n = 0; - int leftChild; + u32 leftChild; i64 nPayload; i64 rowid; i64 nLocal; @@ -418,7 +426,7 @@ static i64 describeCell( leftChild = ((a[0]*256 + a[1])*256 + a[2])*256 + a[3]; a += 4; n += 4; - sprintf(zDesc, "lx: %d ", leftChild); + sprintf(zDesc, "lx: %u ", leftChild); nDesc = strlen(zDesc); } if( cType!=5 ){ @@ -439,10 +447,10 @@ static i64 describeCell( nDesc += strlen(&zDesc[nDesc]); } if( nLocal<nPayload ){ - int ovfl; + u32 ovfl; unsigned char *b = &a[nLocal]; ovfl = ((b[0]*256 + b[1])*256 + b[2])*256 + b[3]; - sprintf(&zDesc[nDesc], "ov: %d ", ovfl); + sprintf(&zDesc[nDesc], "ov: %u ", ovfl); nDesc += strlen(&zDesc[nDesc]); n += 4; } @@ -485,7 +493,7 @@ static void decodeCell( int ofst /* Cell begins at a[ofst] */ ){ int i, j = 0; - int leftChild; + u32 leftChild; i64 k; i64 nPayload; i64 rowid; @@ -504,7 +512,7 @@ static void decodeCell( if( cType<=5 ){ leftChild = ((x[0]*256 + x[1])*256 + x[2])*256 + x[3]; printBytes(a, x, 4); - printf("left child page:: %d\n", leftChild); + printf("left child page:: %u\n", leftChild); x += 4; } if( cType!=5 ){ @@ -535,7 +543,7 @@ static void decodeCell( j = i; nCol = 0; k = nHdr; - while( x+j<end && j<nHdr ){ + while( x+j<=end && j<nHdr ){ const char *zTypeName; int sz = 0; char zNm[30]; @@ -622,7 +630,7 @@ static void decodeCell( } if( nLocal<nPayload ){ printBytes(a, x+nLocal, 4); - printf("overflow-page: %d\n", decodeInt32(x+nLocal)); + printf("overflow-page: %u\n", decodeInt32(x+nLocal)); } } @@ -718,7 +726,7 @@ static void decode_btree_page( } if( showMap ){ printf("Page map: (H=header P=cell-index 1=page-1-header .=free-space)\n"); - for(i=0; i<g.pagesize; i+=64){ + for(i=0; (u32)i<g.pagesize; i+=64){ printf(" %03x: %.64s\n", i, &zMap[i]); } sqlite3_free(zMap); @@ -729,11 +737,12 @@ static void decode_btree_page( ** Decode a freelist trunk page. */ static void decode_trunk_page( - int pgno, /* The page number */ + u32 pgno, /* The page number */ int detail, /* Show leaf pages if true */ int recursive /* Follow the trunk change if true */ ){ - int n, i; + u32 i; + u32 n; unsigned char *a; while( pgno>0 ){ a = fileRead((pgno-1)*g.pagesize, g.pagesize); @@ -741,10 +750,10 @@ static void decode_trunk_page( print_decode_line(a, 0, 4, "Next freelist trunk page"); print_decode_line(a, 4, 4, "Number of entries on this page"); if( detail ){ - n = (int)decodeInt32(&a[4]); - for(i=0; i<n; i++){ - unsigned int x = decodeInt32(&a[8+4*i]); - char zIdx[10]; + n = decodeInt32(&a[4]); + for(i=0; i<n && i<g.pagesize/4; i++){ + u32 x = decodeInt32(&a[8+4*i]); + char zIdx[13]; sprintf(zIdx, "[%d]", i); printf(" %5s %7u", zIdx, x); if( i%5==4 ) printf("\n"); @@ -754,7 +763,7 @@ static void decode_trunk_page( if( !recursive ){ pgno = 0; }else{ - pgno = (int)decodeInt32(&a[0]); + pgno = decodeInt32(&a[0]); } sqlite3_free(a); } @@ -768,7 +777,7 @@ static char **zPageUse; /* ** Add a comment on the use of a page. */ -static void page_usage_msg(int pgno, const char *zFormat, ...){ +static void page_usage_msg(u32 pgno, const char *zFormat, ...){ va_list ap; char *zMsg; @@ -776,7 +785,7 @@ static void page_usage_msg(int pgno, const char *zFormat, ...){ zMsg = sqlite3_vmprintf(zFormat, ap); va_end(ap); if( pgno<=0 || pgno>g.mxPage ){ - printf("ERROR: page %d out of range 1..%d: %s\n", + printf("ERROR: page %d out of range 1..%u: %s\n", pgno, g.mxPage, zMsg); sqlite3_free(zMsg); return; @@ -796,7 +805,7 @@ static void page_usage_msg(int pgno, const char *zFormat, ...){ static void page_usage_cell( unsigned char cType, /* Page type */ unsigned char *a, /* Cell content */ - int pgno, /* page containing the cell */ + u32 pgno, /* page containing the cell */ int cellno /* Index of the cell on the page */ ){ int i; @@ -823,24 +832,35 @@ static void page_usage_cell( n += i; } if( nLocal<nPayload ){ - int ovfl = decodeInt32(a+nLocal); - int cnt = 0; + u32 ovfl = decodeInt32(a+nLocal); + u32 cnt = 0; while( ovfl && (cnt++)<g.mxPage ){ - page_usage_msg(ovfl, "overflow %d from cell %d of page %d", + page_usage_msg(ovfl, "overflow %d from cell %d of page %u", cnt, cellno, pgno); - a = fileRead((ovfl-1)*g.pagesize, 4); + a = fileRead((ovfl-1)*(sqlite3_int64)g.pagesize, 4); ovfl = decodeInt32(a); sqlite3_free(a); } } } +/* +** True if the memory is all zeros +*/ +static int allZero(unsigned char *a, int n){ + while( n && (a++)[0]==0 ){ n--; } + return n==0; +} + /* -** Describe the usages of a b-tree page +** Describe the usages of a b-tree page. +** +** If parent==0, then this is the root of a btree. If parent<0 then +** this is an orphan page. */ static void page_usage_btree( - int pgno, /* Page to describe */ + u32 pgno, /* Page to describe */ int parent, /* Parent of this page. 0 for root pages */ int idx, /* Which child of the parent */ const char *zName /* Name of the table */ @@ -850,30 +870,61 @@ static void page_usage_btree( int nCell; int i; int hdr = pgno==1 ? 100 : 0; + char zEntry[30]; if( pgno<=0 || pgno>g.mxPage ) return; a = fileRead((pgno-1)*g.pagesize, g.pagesize); switch( a[hdr] ){ + case 0: { + if( allZero(a, g.pagesize) ){ + zType = "zeroed page"; + }else if( parent<0 ){ + return; + }else{ + zType = "corrupt node"; + } + break; + } case 2: zType = "interior node of index"; break; case 5: zType = "interior node of table"; break; case 10: zType = "leaf of index"; break; case 13: zType = "leaf of table"; break; + default: { + if( parent<0 ) return; + zType = "corrupt node"; + } } - if( parent ){ - page_usage_msg(pgno, "%s [%s], child %d of page %d", - zType, zName, idx, parent); + nCell = a[hdr+3]*256 + a[hdr+4]; + if( nCell==1 ){ + sqlite3_snprintf(sizeof(zEntry),zEntry,"1 row"); }else{ - page_usage_msg(pgno, "root %s [%s]", zType, zName); + sqlite3_snprintf(sizeof(zEntry),zEntry,"%d rows", nCell); + } + if( parent>0 ){ + page_usage_msg(pgno, "%s [%s], child %d of page %d, %s", + zType, zName, idx, parent, zEntry); + }else if( parent==0 ){ + page_usage_msg(pgno, "root %s [%s], %s", zType, zName, zEntry); + }else{ + page_usage_msg(pgno, "orphaned %s, %s", zType, zEntry); } - nCell = a[hdr+3]*256 + a[hdr+4]; if( a[hdr]==2 || a[hdr]==5 ){ int cellstart = hdr+12; - unsigned int child; + u32 child; for(i=0; i<nCell; i++){ - int ofst; + u32 cellidx; + u32 ofst; - ofst = cellstart + i*2; - ofst = a[ofst]*256 + a[ofst+1]; + cellidx = cellstart + i*2; + if( cellidx+1 >= g.pagesize ){ + printf("ERROR: page %d too many cells (%d)\n", pgno, nCell); + break; + } + ofst = a[cellidx]*256 + a[cellidx+1]; + if( ofst<cellidx+2 || ofst+4>=g.pagesize ){ + printf("ERROR: page %d cell %d out of bounds\n", pgno, i); + continue; + } child = decodeInt32(a+ofst); page_usage_btree(child, pgno, i, zName); } @@ -895,7 +946,7 @@ static void page_usage_btree( /* ** Determine page usage by the freelist */ -static void page_usage_freelist(int pgno){ +static void page_usage_freelist(u32 pgno){ unsigned char *a; int cnt = 0; int i; @@ -903,11 +954,15 @@ static void page_usage_freelist(int pgno){ int iNext; int parent = 1; - while( pgno>0 && pgno<=g.mxPage && (cnt++)<g.mxPage ){ + while( pgno>0 && pgno<=g.mxPage && (u32)(cnt++)<g.mxPage ){ page_usage_msg(pgno, "freelist trunk #%d child of %d", cnt, parent); a = fileRead((pgno-1)*g.pagesize, g.pagesize); iNext = decodeInt32(a); n = decodeInt32(a+4); + if( n>(g.pagesize - 8)/4 ){ + printf("ERROR: page %d too many freelist entries (%d)\n", pgno, n); + n = (g.pagesize - 8)/4; + } for(i=0; i<n; i++){ int child = decodeInt32(a + (i*4+8)); page_usage_msg(child, "freelist leaf, child %d of trunk page %d", @@ -922,13 +977,13 @@ static void page_usage_freelist(int pgno){ /* ** Determine pages used as PTRMAP pages */ -static void page_usage_ptrmap(unsigned char *a){ - if( a[55] ){ +static void page_usage_ptrmap(u8 *a){ + if( decodeInt32(a+52) ){ int usable = g.pagesize - a[20]; - int pgno = 2; + u64 pgno = 2; int perPage = usable/5; while( pgno<=g.mxPage ){ - page_usage_msg(pgno, "PTRMAP page covering %d..%d", + page_usage_msg((u32)pgno, "PTRMAP page covering %llu..%llu", pgno+1, pgno+perPage); pgno += perPage + 1; } @@ -939,7 +994,7 @@ static void page_usage_ptrmap(unsigned char *a){ ** Try to figure out how every page in the database file is being used. */ static void page_usage_report(const char *zPrg, const char *zDbName){ - int i, j; + u32 i, j; int rc; sqlite3 *db; sqlite3_stmt *pStmt; @@ -957,7 +1012,7 @@ static void page_usage_report(const char *zPrg, const char *zDbName){ /* Set up global variables zPageUse[] and g.mxPage to record page ** usages */ - zPageUse = sqlite3_malloc( sizeof(zPageUse[0])*(g.mxPage+1) ); + zPageUse = sqlite3_malloc64( sizeof(zPageUse[0])*(g.mxPage+1) ); if( zPageUse==0 ) out_of_memory(); memset(zPageUse, 0, sizeof(zPageUse[0])*(g.mxPage+1)); @@ -966,7 +1021,7 @@ static void page_usage_report(const char *zPrg, const char *zDbName){ page_usage_freelist(decodeInt32(a+32)); page_usage_ptrmap(a); sqlite3_free(a); - page_usage_btree(1, 0, 0, "sqlite_master"); + page_usage_btree(1, 0, 0, "sqlite_schema"); sqlite3_exec(db, "PRAGMA writable_schema=ON", 0, 0, 0); for(j=0; j<2; j++){ sqlite3_snprintf(sizeof(zQuery), zQuery, @@ -975,7 +1030,7 @@ static void page_usage_report(const char *zPrg, const char *zDbName){ rc = sqlite3_prepare_v2(db, zQuery, -1, &pStmt, 0); if( rc==SQLITE_OK ){ while( sqlite3_step(pStmt)==SQLITE_ROW ){ - int pgno = sqlite3_column_int(pStmt, 2); + u32 pgno = (u32)sqlite3_column_int64(pStmt, 2); page_usage_btree(pgno, 0, 0, (const char*)sqlite3_column_text(pStmt,1)); } }else{ @@ -988,7 +1043,10 @@ static void page_usage_report(const char *zPrg, const char *zDbName){ /* Print the report and free memory used */ for(i=1; i<=g.mxPage; i++){ - printf("%5d: %s\n", i, zPageUse[i] ? zPageUse[i] : "???"); + if( zPageUse[i]==0 ) page_usage_btree(i, -1, 0, 0); + printf("%5u: %s\n", i, zPageUse[i] ? zPageUse[i] : "???"); + } + for(i=1; i<=g.mxPage; i++){ sqlite3_free(zPageUse[i]); } sqlite3_free(zPageUse); @@ -999,7 +1057,7 @@ static void page_usage_report(const char *zPrg, const char *zDbName){ ** Try to figure out how every page in the database file is being used. */ static void ptrmap_coverage_report(const char *zDbName){ - int pgno; + u64 pgno; unsigned char *aHdr; unsigned char *a; int usable; @@ -1021,27 +1079,50 @@ static void ptrmap_coverage_report(const char *zDbName){ usable = g.pagesize - aHdr[20]; perPage = usable/5; sqlite3_free(aHdr); - printf("%5d: root of sqlite_master\n", 1); + printf("%5d: root of sqlite_schema\n", 1); for(pgno=2; pgno<=g.mxPage; pgno += perPage+1){ - printf("%5d: PTRMAP page covering %d..%d\n", pgno, + printf("%5llu: PTRMAP page covering %llu..%llu\n", pgno, pgno+1, pgno+perPage); a = fileRead((pgno-1)*g.pagesize, usable); - for(i=0; i+5<=usable && pgno+1+i/5<=g.mxPage; i+=5){ - const char *zType = "???"; - unsigned int iFrom = decodeInt32(&a[i+1]); + for(i=0; i+5<=usable; i+=5){ + const char *zType; + u32 iFrom = decodeInt32(&a[i+1]); + const char *zExtra = pgno+1+i/5>g.mxPage ? " (off end of DB)" : ""; switch( a[i] ){ case 1: zType = "b-tree root page"; break; case 2: zType = "freelist page"; break; case 3: zType = "first page of overflow"; break; case 4: zType = "later page of overflow"; break; case 5: zType = "b-tree non-root page"; break; + default: { + if( zExtra[0]==0 ){ + printf("%5llu: invalid (0x%02x), parent=%u\n", + pgno+1+i/5, a[i], iFrom); + } + zType = 0; + break; + } + } + if( zType ){ + printf("%5llu: %s, parent=%u%s\n", pgno+1+i/5, zType, iFrom, zExtra); } - printf("%5d: %s, parent=%u\n", pgno+1+i/5, zType, iFrom); } sqlite3_free(a); } } +/* +** Check the range validity for a page number. Print an error and +** exit if the page is out of range. +*/ +static void checkPageValidity(int iPage){ + if( iPage<1 || iPage>g.mxPage ){ + fprintf(stderr, "Invalid page number %d: valid range is 1..%d\n", + iPage, g.mxPage); + exit(1); + } +} + /* ** Print a usage comment */ @@ -1097,17 +1178,17 @@ int main(int argc, char **argv){ if( g.pagesize==0 ) g.pagesize = 1024; sqlite3_free(zPgSz); - printf("Pagesize: %d\n", g.pagesize); - g.mxPage = (szFile+g.pagesize-1)/g.pagesize; + printf("Pagesize: %d\n", (int)g.pagesize); + g.mxPage = (u32)((szFile+g.pagesize-1)/g.pagesize); - printf("Available pages: 1..%d\n", g.mxPage); + printf("Available pages: 1..%u\n", g.mxPage); if( nArg==2 ){ - int i; + u32 i; for(i=1; i<=g.mxPage; i++) print_page(i); }else{ int i; for(i=2; i<nArg; i++){ - int iStart, iEnd; + u32 iStart, iEnd; char *zLeft; if( strcmp(azArg[i], "dbheader")==0 ){ print_db_header(); @@ -1129,13 +1210,16 @@ int main(int argc, char **argv){ fprintf(stderr, "%s: unknown option: [%s]\n", zPrg, azArg[i]); continue; } - iStart = strtol(azArg[i], &zLeft, 0); + iStart = strtoul(azArg[i], &zLeft, 0); + checkPageValidity(iStart); if( zLeft && strcmp(zLeft,"..end")==0 ){ iEnd = g.mxPage; }else if( zLeft && zLeft[0]=='.' && zLeft[1]=='.' ){ iEnd = strtol(&zLeft[2], 0, 0); + checkPageValidity(iEnd); }else if( zLeft && zLeft[0]=='b' ){ - int ofst, nByte, hdrSize; + i64 ofst; + int nByte, hdrSize; unsigned char *a; if( iStart==1 ){ ofst = hdrSize = 100; diff --git a/tool/showlocks.c b/tool/showlocks.c index 752c535cc3..4159a71f8a 100644 --- a/tool/showlocks.c +++ b/tool/showlocks.c @@ -24,23 +24,58 @@ static int showLocksInRange(int fd, off_t lwr, off_t upr){ int cnt = 0; struct flock x; + struct lockRange { + off_t lwr; + off_t upr; + } *aPending = 0; + int nAlloc = 1; + int nPending = 0; + int nDone = 0; - x.l_type = F_WRLCK; - x.l_whence = SEEK_SET; - x.l_start = lwr; - x.l_len = upr-lwr; - fcntl(fd, F_GETLK, &x); - if( x.l_type==F_UNLCK ) return 0; - printf("start: %-12d len: %-5d pid: %-5d type: %s\n", - (int)x.l_start, (int)x.l_len, - x.l_pid, x.l_type==F_WRLCK ? "WRLCK" : "RDLCK"); - cnt++; - if( x.l_start>lwr ){ - cnt += showLocksInRange(fd, lwr, x.l_start-1); + nPending = 1; + aPending = malloc( sizeof(aPending[0]) ); + if( aPending==0 ){ + fprintf(stderr, "out of memory\n"); + exit(1); } - if( x.l_start+x.l_len<upr ){ - cnt += showLocksInRange(fd, x.l_start+x.l_len+1, upr); + aPending[0].lwr = lwr; + aPending[0].upr = upr; + + for(nDone=0; nDone<nPending; nDone++){ + lwr = aPending[nDone].lwr; + upr = aPending[nDone].upr; + if( lwr>=upr ) continue; + x.l_type = F_WRLCK; + x.l_whence = SEEK_SET; + x.l_start = lwr; + x.l_len = upr - lwr; + fcntl(fd, F_GETLK, &x); + if( x.l_type==F_UNLCK ) continue; + printf("start: %-12d len: %-5d pid: %-5d type: %s\n", + (int)x.l_start, (int)x.l_len, + x.l_pid, x.l_type==F_WRLCK ? "WRLCK" : "RDLCK"); + cnt++; + if( nPending+2 > nAlloc ){ + nAlloc = nAlloc*2 + 2; + aPending = realloc(aPending, sizeof(aPending[0])*nAlloc ); + } + if( aPending==0 ){ + fprintf(stderr, "unable to realloc for %d bytes\n", + (int)sizeof(aPending[0])*(nPending+2)); + exit(1); + } + if( lwr<x.l_start ){ + aPending[nPending].lwr = lwr; + aPending[nPending].upr = x.l_start; + nPending++; + } + if( x.l_start+x.l_len<=upr ){ + aPending[nPending].lwr = x.l_start + x.l_len; + aPending[nPending].upr = upr; + nPending++; + } } + free(aPending); return cnt; } diff --git a/tool/showshm.c b/tool/showshm.c new file mode 100644 index 0000000000..03e0fc460c --- /dev/null +++ b/tool/showshm.c @@ -0,0 +1,158 @@ +/* +** A utility for printing content from the wal-index or "shm" file. +*/ +#include <stdio.h> +#include <ctype.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <assert.h> + +#define ISDIGIT(X) isdigit((unsigned char)(X)) +#define ISPRINT(X) isprint((unsigned char)(X)) + +#if !defined(_MSC_VER) +#include <unistd.h> +#include <sys/types.h> +#else +#include <io.h> +#endif + +#include <stdlib.h> +#include <string.h> + +static int fd = -1; /* The open SHM file */ + +/* Report an out-of-memory error and die. +*/ +static void out_of_memory(void){ + fprintf(stderr,"Out of memory...\n"); + exit(1); +} + +/* +** Read content from the file. +** +** Space to hold the content is obtained from malloc() and needs to be +** freed by the caller. +*/ +static unsigned char *getContent(int ofst, int nByte){ + unsigned char *aData; + aData = malloc(nByte); + if( aData==0 ) out_of_memory(); + lseek(fd, ofst, SEEK_SET); + read(fd, aData, nByte); + return aData; +} + +/* +** Flags values +*/ +#define FG_HEX 1 /* Show as hex */ +#define FG_NBO 2 /* Native byte order */ +#define FG_PGSZ 4 /* Show as page-size */ + +/* Print a line of decode output showing a 4-byte integer. +*/ +static void print_decode_line( + unsigned char *aData, /* Content being decoded */ + int ofst, int nByte, /* Start and size of decode */ + unsigned flg, /* Display flags */ + const char *zMsg /* Message to append */ +){ + int i, j; + int val = aData[ofst]; + char zBuf[100]; + sprintf(zBuf, " %03x: %02x", ofst, aData[ofst]); + i = (int)strlen(zBuf); + for(j=1; j<4; j++){ + if( j>=nByte ){ + sprintf(&zBuf[i], " "); + }else{ + sprintf(&zBuf[i], " %02x", aData[ofst+j]); + val = val*256 + aData[ofst+j]; + } + i += (int)strlen(&zBuf[i]); + } + if( nByte==8 ){ + for(j=4; j<8; j++){ + sprintf(&zBuf[i], " %02x", aData[ofst+j]); + i += (int)strlen(&zBuf[i]); + } + } + if( flg & FG_NBO ){ + assert( nByte==4 ); + memcpy(&val, aData+ofst, 4); + } + sprintf(&zBuf[i], " "); + i += 12; + if( flg & FG_PGSZ ){ + unsigned short sz; + memcpy(&sz, aData+ofst, 2); + sprintf(&zBuf[i], " %9d", sz==1 ? 65536 : sz); + }else if( flg & FG_HEX ){ + sprintf(&zBuf[i], " 0x%08x", val); + }else if( nByte<8 ){ + sprintf(&zBuf[i], " %9d", val); + } + printf("%s %s\n", zBuf, zMsg); +} + +/* +** Print an instance of the WalIndexHdr object. ix is either 0 or 1 +** to select which header to print. +*/ +static void print_index_hdr(unsigned char *aData, int ix){ + int i; + assert( ix==0 || ix==1 ); + i = ix ? 48 : 0; + print_decode_line(aData, 0+i, 4, FG_NBO, "Wal-index version"); + print_decode_line(aData, 4+i, 4, 0, "unused padding"); + print_decode_line(aData, 8+i, 4, FG_NBO, "transaction counter"); + print_decode_line(aData,12+i, 1, 0, "1 when initialized"); + print_decode_line(aData,13+i, 1, 0, "true if WAL cksums are bigendian"); + print_decode_line(aData,14+i, 2, FG_PGSZ, "database page size"); + print_decode_line(aData,16+i, 4, FG_NBO, "mxFrame"); + print_decode_line(aData,20+i, 4, FG_NBO, "Size of database in pages"); + print_decode_line(aData,24+i, 8, 0, "Cksum of last frame in -wal"); + print_decode_line(aData,32+i, 8, 0, "Salt values from the -wal"); + print_decode_line(aData,40+i, 8, 0, "Cksum over all prior fields"); +} + +/* +** Print the WalCkptInfo object +*/ +static void print_ckpt_info(unsigned char *aData){ + const int i = 96; + int j; + print_decode_line(aData, 0+i, 4, FG_NBO, "nBackfill"); + for(j=0; j<5; j++){ + char zLabel[100]; + sprintf(zLabel, "aReadMark[%d]", j); + print_decode_line(aData, 4*j+4+i, 4, FG_NBO, zLabel); + } + print_decode_line(aData,24+i, 8, 0, "aLock"); + print_decode_line(aData,32+i, 4, FG_NBO, "nBackfillAttempted"); + print_decode_line(aData,36+i, 4, FG_NBO, "notUsed0"); +} + + +int main(int argc, char **argv){ + unsigned char *aData; + if( argc<2 ){ + fprintf(stderr,"Usage: %s FILENAME\n", argv[0]); + exit(1); + } + fd = open(argv[1], O_RDONLY); + if( fd<0 ){ + fprintf(stderr,"%s: can't open %s\n", argv[0], argv[1]); + exit(1); + } + aData = getContent(0, 136); + print_index_hdr(aData, 0); + print_index_hdr(aData, 1); + print_ckpt_info(aData); + free(aData); + close(fd); + return 0; +} diff --git a/tool/showstat4.c b/tool/showstat4.c index c7f9b10fbc..d1ff4961bb 100644 --- a/tool/showstat4.c +++ b/tool/showstat4.c @@ -67,6 +67,7 @@ int main(int argc, char **argv){ "**************\n\n"); sqlite3_free(zIdx); zIdx = sqlite3_mprintf("%s", sqlite3_column_text(pStmt,0)); + if( zIdx==0 ){ fprintf(stderr, "out of memory\n"); abort(); } iRow = 0; } printf("%s sample %d ------------------------------------\n", zIdx, ++iRow); @@ -118,8 +119,11 @@ int main(int argc, char **argv){ } if( iVal==7 ){ double r; + char *z; memcpy(&r, &v, sizeof(r)); - printf("%s%#g", zSep, r); + z = sqlite3_mprintf("%s%!.15g", zSep, r); + printf("%s", z); + sqlite3_free(z); }else{ printf("%s%lld", zSep, v); } @@ -130,11 +134,11 @@ int main(int argc, char **argv){ } printf("'"); }else{ - printf("%s\"", zSep); + printf("%s'", zSep); for(j=0; j<sz; j++){ char c = (char)aSample[y+j]; if( ISPRINT(c) ){ - if( c=='"' || c=='\\' ) putchar('\\'); + if( c=='\'' || c=='\\' ) putchar('\\'); putchar(c); }else if( c=='\n' ){ printf("\\n"); @@ -146,7 +150,7 @@ int main(int argc, char **argv){ printf("\\%03o", c); } } - printf("\""); + printf("'"); } zSep = ","; y += sz; diff --git a/tool/showwal.c b/tool/showwal.c index 33cc21896b..7e6c0e17cd 100644 --- a/tool/showwal.c +++ b/tool/showwal.c @@ -12,6 +12,7 @@ #if !defined(_MSC_VER) #include <unistd.h> +#include <sys/types.h> #else #include <io.h> #endif @@ -116,7 +117,7 @@ static void out_of_memory(void){ ** Space to hold the content is obtained from malloc() and needs to be ** freed by the caller. */ -static unsigned char *getContent(int ofst, int nByte){ +static unsigned char *getContent(i64 ofst, int nByte){ unsigned char *aData; aData = malloc(nByte); if( aData==0 ) out_of_memory(); @@ -203,10 +204,10 @@ static void print_decode_line( ** Print an entire page of content as hex */ static void print_frame(int iFrame){ - int iStart; + i64 iStart; unsigned char *aData; - iStart = 32 + (iFrame-1)*(pagesize+24); - fprintf(stdout, "Frame %d: (offsets 0x%x..0x%x)\n", + iStart = 32 + (i64)(iFrame-1)*(pagesize+24); + fprintf(stdout, "Frame %d: (offsets 0x%llx..0x%llx)\n", iFrame, iStart, iStart+pagesize+24); aData = getContent(iStart, pagesize+24); print_decode_line(aData, 0, 4, 0, "Page number"); @@ -223,25 +224,30 @@ static void print_frame(int iFrame){ ** Summarize a single frame on a single line. */ static void print_oneline_frame(int iFrame, Cksum *pCksum){ - int iStart; + i64 iStart; unsigned char *aData; unsigned int s0, s1; - iStart = 32 + (iFrame-1)*(pagesize+24); + iStart = 32 + (i64)(iFrame-1)*(pagesize+24); aData = getContent(iStart, 24); extendCksum(pCksum, aData, 8, 0); extendCksum(pCksum, getContent(iStart+24, pagesize), pagesize, 0); s0 = getInt32(aData+16); s1 = getInt32(aData+20); - fprintf(stdout, "Frame %4d: %6d %6d 0x%08x,%08x 0x%08x,%08x %s\n", + fprintf(stdout, "Frame %4d: %6d %6d 0x%08x,%08x 0x%08x,%08x", iFrame, getInt32(aData), getInt32(aData+4), getInt32(aData+8), getInt32(aData+12), s0, - s1, - (s0==pCksum->s0 && s1==pCksum->s1) ? "" : "cksum-fail" + s1 ); + if( s0==pCksum->s0 && s1==pCksum->s1 ){ + fprintf(stdout, "\n"); + }else{ + fprintf(stdout, " should be 0x%08x,%08x\n", + pCksum->s0, pCksum->s1); + } /* Reset the checksum so that a single frame checksum failure will not ** cause all subsequent frames to also show a failure. */ @@ -511,6 +517,18 @@ static void decode_btree_page( } } +/* +** Check the range validity for a page number. Print an error and +** exit if the page is out of range. +*/ +static void checkPageValidity(int iPage, int mxPage){ + if( iPage<1 || iPage>mxPage ){ + fprintf(stderr, "Invalid page number %d: valid range is 1..%d\n", + iPage, mxPage); + exit(1); + } +} + int main(int argc, char **argv){ struct stat sbuf; unsigned char zPgSz[4]; @@ -525,16 +543,27 @@ int main(int argc, char **argv){ } zPgSz[0] = 0; zPgSz[1] = 0; - lseek(fd, 8, SEEK_SET); - read(fd, zPgSz, 4); - pagesize = zPgSz[1]*65536 + zPgSz[2]*256 + zPgSz[3]; - if( pagesize==0 ) pagesize = 1024; - printf("Pagesize: %d\n", pagesize); fstat(fd, &sbuf); if( sbuf.st_size<32 ){ - printf("file too small to be a WAL\n"); + printf("%s: file too small to be a WAL - only %d bytes\n", + argv[1], (int)sbuf.st_size); return 0; } + if( lseek(fd, 8, SEEK_SET)!=8 ){ + printf("\"%s\" seems to not be a valid WAL file\n", argv[1]); + return 1; + } + if( read(fd, zPgSz, 4)!=4 ){ + printf("\"%s\": cannot read the page size\n", argv[1]); + return 1; + } + pagesize = zPgSz[1]*65536 + zPgSz[2]*256 + zPgSz[3]; + if( pagesize==0 ) pagesize = 1024; + printf("Pagesize: %d\n", pagesize); + if( (pagesize & (pagesize-1))!=0 || pagesize<512 || pagesize>65536 ){ + printf("\"%s\": invalid page size.\n", argv[1]); + return 1; + } mxFrame = (sbuf.st_size - 32)/(pagesize + 24); printf("Available pages: 1..%d\n", mxFrame); if( argc==2 ){ @@ -558,12 +587,15 @@ int main(int argc, char **argv){ continue; } iStart = strtol(argv[i], &zLeft, 0); + checkPageValidity(iStart, mxFrame); if( zLeft && strcmp(zLeft,"..end")==0 ){ iEnd = mxFrame; }else if( zLeft && zLeft[0]=='.' && zLeft[1]=='.' ){ iEnd = strtol(&zLeft[2], 0, 0); + checkPageValidity(iEnd, mxFrame); }else if( zLeft && zLeft[0]=='b' ){ - int ofst, nByte, hdrSize; + i64 ofst; + int nByte, hdrSize; unsigned char *a; if( iStart==1 ){ hdrSize = 100; @@ -571,14 +603,22 @@ int main(int argc, char **argv){ nByte = pagesize-100; }else{ hdrSize = 0; - ofst = (iStart-1)*pagesize; + ofst = (i64)(iStart-1)*pagesize; nByte = pagesize; } - ofst = 32 + hdrSize + (iStart-1)*(pagesize+24) + 24; + ofst = 32 + hdrSize + (i64)(iStart-1)*(pagesize+24) + 24; a = getContent(ofst, nByte); decode_btree_page(a, iStart, hdrSize, zLeft+1); free(a); continue; +#if !defined(_MSC_VER) + }else if( zLeft && strcmp(zLeft,"truncate")==0 ){ + /* Frame number followed by "truncate" truncates the WAL file + ** after that frame */ + off_t newSize = 32 + iStart*(pagesize+24); + truncate(argv[1], newSize); + continue; +#endif }else{ iEnd = iStart; } diff --git a/tool/soak1.tcl b/tool/soak1.tcl index 846f905935..e09c566b86 100644 --- a/tool/soak1.tcl +++ b/tool/soak1.tcl @@ -4,7 +4,7 @@ # # tclsh soak1.tcl local-makefile.mk ?target? ?scenario? # -# This generates many variations on local-makefile.mk (by modifing +# This generates many variations on local-makefile.mk (by modifying # the OPT = lines) and runs them will fulltest, one by one. The # constructed makefiles are named "soak1.mk". # diff --git a/tool/spaceanal.tcl b/tool/spaceanal.tcl index 38d954162e..e50415900a 100644 --- a/tool/spaceanal.tcl +++ b/tool/spaceanal.tcl @@ -1,7 +1,9 @@ -# Run this TCL script using "testfixture" in order get a report that shows -# how much disk space is used by a particular data to actually store data +# Run this TCL script using an SQLite-enabled TCL interpreter to get a report +# on how much disk space is used by a particular data to actually store data # versus how much space is unused. # +# The dbstat virtual table is required. +# if {[catch { @@ -14,7 +16,7 @@ proc is_without_rowid {tname} { db eval "PRAGMA index_list = '$t'" o { if {$o(origin) == "pk"} { set n $o(name) - if {0==[db one { SELECT count(*) FROM sqlite_master WHERE name=$n }]} { + if {0==[db one { SELECT count(*) FROM sqlite_schema WHERE name=$n }]} { return 1 } } @@ -22,6 +24,33 @@ proc is_without_rowid {tname} { return 0 } +# Read and run TCL commands from standard input. Used to implement +# the --tclsh option. +# +proc tclsh {} { + set line {} + while {![eof stdin]} { + if {$line!=""} { + puts -nonewline "> " + } else { + puts -nonewline "% " + } + flush stdout + append line [gets stdin] + if {[info complete $line]} { + if {[catch {uplevel #0 $line} result]} { + puts stderr "Error: $result" + } elseif {$result!=""} { + puts $result + } + set line {} + } else { + append line \n + } + } +} + + # Get the name of the database to analyze # proc usage {} { @@ -34,22 +63,47 @@ information for the database and its constituent tables and indexes. Options: - --stats Output SQL text that creates a new database containing - statistics about the database that was analyzed + --pageinfo Show how each page of the database-file is used + + --stats Output SQL text that creates a new database containing + statistics about the database that was analyzed - --pageinfo Show how each page of the database-file is used + --tclsh Run the built-in TCL interpreter interactively (for debugging) + + --version Show the version number of SQLite } exit 1 } + +# Exit with given code, but first close db if open. +# +proc exit_clean {exit_code} { + if {0 < [llength [info commands db]]} { + db close + } + exit $exit_code +} + set file_to_analyze {} set flags(-pageinfo) 0 set flags(-stats) 0 +set flags(-debug) 0 append argv {} foreach arg $argv { if {[regexp {^-+pageinfo$} $arg]} { set flags(-pageinfo) 1 } elseif {[regexp {^-+stats$} $arg]} { set flags(-stats) 1 + } elseif {[regexp {^-+debug$} $arg]} { + set flags(-debug) 1 + } elseif {[regexp {^-+tclsh$} $arg]} { + tclsh + exit 0 + } elseif {[regexp {^-+version$} $arg]} { + sqlite3 mem :memory: + puts [mem one {SELECT sqlite_version()||' '||sqlite_source_id()}] + mem close + exit 0 } elseif {[regexp {^-} $arg]} { puts stderr "Unknown option: $arg" usage @@ -100,8 +154,23 @@ if {[catch {sqlite3 db $file_to_analyze -uri 1} msg]} { puts stderr "error trying to open $file_to_analyze: $msg" exit 1 } +if {$flags(-debug)} { + proc dbtrace {txt} {puts $txt; flush stdout;} + db trace ::dbtrace +} -db eval {SELECT count(*) FROM sqlite_master} +# Make sure all required compile-time options are available +# +if {![db exists {SELECT 1 FROM pragma_compile_options + WHERE compile_options='ENABLE_DBSTAT_VTAB'}]} { + puts "The SQLite database engine linked with this application\ + lacks required capabilities. Recompile using the\ + -DSQLITE_ENABLE_DBSTAT_VTAB compile-time option to fix\ + this problem." + exit_clean 1 +} + +db eval {SELECT count(*) FROM sqlite_schema} set pageSize [expr {wide([db one {PRAGMA page_size}])}] if {$flags(-pageinfo)} { @@ -109,7 +178,7 @@ if {$flags(-pageinfo)} { db eval {SELECT name, path, pageno FROM temp.stat ORDER BY pageno} { puts "$pageno $name $path" } - exit 0 + exit_clean 0 } if {$flags(-stats)} { db eval {CREATE VIRTUAL TABLE temp.stat USING dbstat} @@ -139,19 +208,25 @@ if {$flags(-stats)} { puts "INSERT INTO stats VALUES($x);" } puts "COMMIT;" - exit 0 + exit_clean 0 } + # In-memory database for collecting statistics. This script loops through # the tables and indices in the database being analyzed, adding a row for each # to an in-memory database (for which the schema is shown below). It then # queries the in-memory db to produce the space-analysis report. # sqlite3 mem :memory: +if {$flags(-debug)} { + proc dbtrace {txt} {puts $txt; flush stdout;} + mem trace ::dbtrace +} set tabledef {CREATE TABLE space_used( name clob, -- Name of a table or index in the database file tblname clob, -- Name of associated table is_index boolean, -- TRUE if it is an index, false for a table + is_without_rowid boolean, -- TRUE if WITHOUT ROWID table nentry int, -- Number of entries in the BTree leaf_entries int, -- Number of leaf entries depth int, -- Depth of the b-tree @@ -180,11 +255,11 @@ db eval {DROP TABLE temp.stat} set isCompressed 0 set compressOverhead 0 set depth 0 -set sql { SELECT name, tbl_name FROM sqlite_master WHERE rootpage>0 } -foreach {name tblname} [concat sqlite_master sqlite_master [db eval $sql]] { +set sql { SELECT name, tbl_name FROM sqlite_schema WHERE rootpage>0 } +foreach {name tblname} [concat sqlite_schema sqlite_schema [db eval $sql]] { set is_index [expr {$name!=$tblname}] - set idx_btree [expr {$is_index || [is_without_rowid $name]}] + set is_without_rowid [is_without_rowid $name] db eval { SELECT sum(ncell) AS nentry, @@ -235,6 +310,7 @@ foreach {name tblname} [concat sqlite_master sqlite_master [db eval $sql]] { $name, $tblname, $is_index, + $is_without_rowid, $nentry, $leaf_entries, $depth, @@ -330,12 +406,15 @@ proc subreport {title where showFrag} { # following query returns exactly one row (because it is an aggregate). # # The results of the query are stored directly by SQLite into local - # variables (i.e. $nentry, $nleaf etc.). + # variables (i.e. $nentry, $payload etc.). # mem eval " SELECT - int(sum(nentry)) AS nentry, - int(sum(leaf_entries)) AS nleaf, + int(sum( + CASE WHEN (is_without_rowid OR is_index) THEN nentry + ELSE leaf_entries + END + )) AS nentry, int(sum(payload)) AS payload, int(sum(ovfl_payload)) AS ovfl_payload, max(mx_payload) AS mx_payload, @@ -368,6 +447,7 @@ proc subreport {title where showFrag} { # avg_payload: Average payload per btree entry. # avg_fanout: Average fanout for internal pages. # avg_unused: Average unused bytes per btree entry. + # avg_meta: Average metadata overhead per entry. # ovfl_cnt_percent: Percentage of btree entries that use overflow pages. # set total_pages [expr {$leaf_pages+$int_pages+$ovfl_pages}] @@ -375,8 +455,12 @@ proc subreport {title where showFrag} { set storage [expr {$total_pages*$pageSize}] set payload_percent [percent $payload $storage {of storage consumed}] set total_unused [expr {$ovfl_unused+$int_unused+$leaf_unused}] - set avg_payload [divide $payload $nleaf] - set avg_unused [divide $total_unused $nleaf] + set avg_payload [divide $payload $nentry] + set avg_unused [divide $total_unused $nentry] + set total_meta [expr {$storage - $payload - $total_unused}] + set total_meta [expr {$total_meta + 4*($ovfl_pages - $ovfl_cnt)}] + set meta_percent [percent $total_meta $storage {of metadata}] + set avg_meta [divide $total_meta $nentry] if {$int_pages>0} { # TODO: Is this formula correct? set nTab [mem eval " @@ -390,12 +474,12 @@ proc subreport {title where showFrag} { "] set avg_fanout [format %.2f $avg_fanout] } - set ovfl_cnt_percent [percent $ovfl_cnt $nleaf {of all entries}] + set ovfl_cnt_percent [percent $ovfl_cnt $nentry {of all entries}] # Print out the sub-report statistics. # statline {Percentage of total database} $total_pages_percent - statline {Number of entries} $nleaf + statline {Number of entries} $nentry statline {Bytes of storage consumed} $storage if {$compressed_size!=$storage} { set compressed_size [expr {$compressed_size+$compressOverhead*$total_pages}] @@ -404,9 +488,11 @@ proc subreport {title where showFrag} { statline {Bytes used after compression} $compressed_size $pct } statline {Bytes of payload} $payload $payload_percent + statline {Bytes of metadata} $total_meta $meta_percent if {$cnt==1} {statline {B-tree depth} $depth} statline {Average payload per entry} $avg_payload statline {Average unused bytes per entry} $avg_unused + statline {Average metadata per entry} $avg_meta if {[info exists avg_fanout]} { statline {Average fanout} $avg_fanout } @@ -484,7 +570,7 @@ proc autovacuum_overhead {filePages pageSize} { # nautoindex: Number of indices created automatically. # nmanindex: Number of indices created manually. # user_payload: Number of bytes of payload in table btrees -# (not including sqlite_master) +# (not including sqlite_schema) # user_percent: $user_payload as a percentage of total file size. ### The following, setting $file_bytes based on the actual size of the file @@ -505,21 +591,26 @@ set inuse_pgcnt [expr wide([mem eval $sql])] set inuse_percent [percent $inuse_pgcnt $file_pgcnt] set free_pgcnt [expr {$file_pgcnt-$inuse_pgcnt-$av_pgcnt}] +if {$file_bytes>1073741824 && $free_pgcnt>0} {incr free_pgcnt -1} set free_percent [percent $free_pgcnt $file_pgcnt] set free_pgcnt2 [db one {PRAGMA freelist_count}] set free_percent2 [percent $free_pgcnt2 $file_pgcnt] set file_pgcnt2 [expr {$inuse_pgcnt+$free_pgcnt2+$av_pgcnt}] -set ntable [db eval {SELECT count(*)+1 FROM sqlite_master WHERE type='table'}] -set nindex [db eval {SELECT count(*) FROM sqlite_master WHERE type='index'}] -set sql {SELECT count(*) FROM sqlite_master WHERE name LIKE 'sqlite_autoindex%'} +# Account for the lockbyte page +if {$file_pgcnt2*$pageSize>1073742335} {incr file_pgcnt2} + +set ntable [db eval {SELECT count(*)+1 FROM sqlite_schema WHERE type='table'}] +set nindex [db eval {SELECT count(*) FROM sqlite_schema WHERE type='index'}] +set sql {SELECT count(*) FROM sqlite_schema WHERE name LIKE 'sqlite_autoindex%'} set nautoindex [db eval $sql] set nmanindex [expr {$nindex-$nautoindex}] +set nwithoutrowid [db eval {SELECT count(*) FROM pragma_table_list WHERE wr}] # set total_payload [mem eval "SELECT sum(payload) FROM space_used"] set user_payload [mem one {SELECT int(sum(payload)) FROM space_used - WHERE NOT is_index AND name NOT LIKE 'sqlite_master'}] + WHERE NOT is_index AND name NOT LIKE 'sqlite_schema'}] set user_percent [percent $user_payload $file_bytes] # Output the summary statistics calculated above. @@ -534,6 +625,7 @@ statline {Pages on the freelist (per header)} $free_pgcnt2 $free_percent2 statline {Pages on the freelist (calculated)} $free_pgcnt $free_percent statline {Pages of auto-vacuum overhead} $av_pgcnt $av_percent statline {Number of tables in the database} $ntable +statline {Number of WITHOUT ROWID tables} $nwithoutrowid statline {Number of indices} $nindex statline {Number of defined indices} $nmanindex statline {Number of implied indices} $nautoindex @@ -592,6 +684,14 @@ if {$nindex>0} { subreport {All tables and indices} 1 0 } subreport {All tables} {NOT is_index} 0 +if {$nwithoutrowid>0} { + subreport {All WITHOUT ROWID tables} {is_without_rowid} 0 + set nrowidtab [db eval {SELECT count(*) FROM pragma_table_list + WHERE type='table' AND NOT wr}] + if {$nrowidtab>0} { + subreport {ALL rowid tables} {NOT is_without_rowid AND NOT is_index} 0 + } +} if {$nindex>0} { subreport {All indices} {is_index} 0 } @@ -652,7 +752,7 @@ Pages of auto-vacuum overhead Number of tables in the database - The number of tables in the database, including the SQLITE_MASTER table + The number of tables in the database, including the SQLITE_SCHEMA table used to store schema information. Number of indices @@ -675,7 +775,7 @@ Size of the file in bytes Bytes of user payload stored The total number of bytes of user payload stored in the database. The - schema information in the SQLITE_MASTER table is not counted when + schema information in the SQLITE_SCHEMA table is not counted when computing this number. The percentage at the right shows the payload divided by the total file size. @@ -701,6 +801,16 @@ Bytes of payload at the right is the bytes of payload divided by the bytes of storage consumed. +Bytes of metadata + + The amount of formatting and structural information stored in the + table or index. Metadata includes the btree page header, the cell pointer + array, the size field for each cell, the left child pointer or non-leaf + cells, the overflow pointers for overflow cells, and the rowid value for + rowid table cells. In other words, metadata is everything that is neither + unused space nor content. The record header in the payload is counted as + content, not metadata. + Average payload per entry The average amount of payload on each entry. This is just the bytes of @@ -801,5 +911,7 @@ puts "COMMIT;" } err]} { puts "ERROR: $err" puts $errorInfo - exit 1 + exit_clean 1 } + +exit_clean 0 diff --git a/tool/speedtest.tcl b/tool/speedtest.tcl deleted file mode 100644 index ef39dc5461..0000000000 --- a/tool/speedtest.tcl +++ /dev/null @@ -1,275 +0,0 @@ -#!/usr/bin/tclsh -# -# Run this script using TCLSH to do a speed comparison between -# various versions of SQLite and PostgreSQL and MySQL -# - -# Run a test -# -set cnt 1 -proc runtest {title} { - global cnt - set sqlfile test$cnt.sql - puts "<h2>Test $cnt: $title</h2>" - incr cnt - set fd [open $sqlfile r] - set sql [string trim [read $fd [file size $sqlfile]]] - close $fd - set sx [split $sql \n] - set n [llength $sx] - if {$n>8} { - set sql {} - for {set i 0} {$i<3} {incr i} {append sql [lindex $sx $i]<br>\n} - append sql "<i>... [expr {$n-6}] lines omitted</i><br>\n" - for {set i [expr {$n-3}]} {$i<$n} {incr i} { - append sql [lindex $sx $i]<br>\n - } - } else { - regsub -all \n [string trim $sql] <br> sql - } - puts "<blockquote>" - puts "$sql" - puts "</blockquote><table border=0 cellpadding=0 cellspacing=0>" - set format {<tr><td>%s</td><td align="right">&nbsp;&nbsp;&nbsp;%.3f</td></tr>} - set delay 1000 -# exec sync; after $delay; -# set t [time "exec psql drh <$sqlfile" 1] -# set t [expr {[lindex $t 0]/1000000.0}] -# puts [format $format PostgreSQL: $t] - exec sync; after $delay; - set t [time "exec mysql -f drh <$sqlfile" 1] - set t [expr {[lindex $t 0]/1000000.0}] - puts [format $format MySQL: $t] -# set t [time "exec ./sqlite232 s232.db <$sqlfile" 1] -# set t [expr {[lindex $t 0]/1000000.0}] -# puts [format $format {SQLite 2.3.2:} $t] -# set t [time "exec ./sqlite-100 s100.db <$sqlfile" 1] -# set t [expr {[lindex $t 0]/1000000.0}] -# puts [format $format {SQLite 2.4 (cache=100):} $t] - exec sync; after $delay; - set t [time "exec ./sqlite248 s2k.db <$sqlfile" 1] - set t [expr {[lindex $t 0]/1000000.0}] - puts [format $format {SQLite 2.4.8:} $t] - exec sync; after $delay; - set t [time "exec ./sqlite248 sns.db <$sqlfile" 1] - set t [expr {[lindex $t 0]/1000000.0}] - puts [format $format {SQLite 2.4.8 (nosync):} $t] - exec sync; after $delay; - set t [time "exec ./sqlite2412 s2kb.db <$sqlfile" 1] - set t [expr {[lindex $t 0]/1000000.0}] - puts [format $format {SQLite 2.4.12:} $t] - exec sync; after $delay; - set t [time "exec ./sqlite2412 snsb.db <$sqlfile" 1] - set t [expr {[lindex $t 0]/1000000.0}] - puts [format $format {SQLite 2.4.12 (nosync):} $t] -# set t [time "exec ./sqlite-t1 st1.db <$sqlfile" 1] -# set t [expr {[lindex $t 0]/1000000.0}] -# puts [format $format {SQLite 2.4 (test):} $t] - puts "</table>" -} - -# Initialize the environment -# -expr srand(1) -catch {exec /bin/sh -c {rm -f s*.db}} -set fd [open clear.sql w] -puts $fd { - drop table t1; - drop table t2; -} -close $fd -catch {exec psql drh <clear.sql} -catch {exec mysql drh <clear.sql} -set fd [open 2kinit.sql w] -puts $fd { - PRAGMA default_cache_size=2000; - PRAGMA default_synchronous=on; -} -close $fd -exec ./sqlite248 s2k.db <2kinit.sql -exec ./sqlite2412 s2kb.db <2kinit.sql -set fd [open nosync-init.sql w] -puts $fd { - PRAGMA default_cache_size=2000; - PRAGMA default_synchronous=off; -} -close $fd -exec ./sqlite248 sns.db <nosync-init.sql -exec ./sqlite2412 snsb.db <nosync-init.sql -set ones {zero one two three four five six seven eight nine - ten eleven twelve thirteen fourteen fifteen sixteen seventeen - eighteen nineteen} -set tens {{} ten twenty thirty forty fifty sixty seventy eighty ninety} -proc number_name {n} { - if {$n>=1000} { - set txt "[number_name [expr {$n/1000}]] thousand" - set n [expr {$n%1000}] - } else { - set txt {} - } - if {$n>=100} { - append txt " [lindex $::ones [expr {$n/100}]] hundred" - set n [expr {$n%100}] - } - if {$n>=20} { - append txt " [lindex $::tens [expr {$n/10}]]" - set n [expr {$n%10}] - } - if {$n>0} { - append txt " [lindex $::ones $n]" - } - set txt [string trim $txt] - if {$txt==""} {set txt zero} - return $txt -} - - - -set fd [open test$cnt.sql w] -puts $fd "CREATE TABLE t1(a INTEGER, b INTEGER, c VARCHAR(100));" -for {set i 1} {$i<=1000} {incr i} { - set r [expr {int(rand()*100000)}] - puts $fd "INSERT INTO t1 VALUES($i,$r,'[number_name $r]');" -} -close $fd -runtest {1000 INSERTs} - - - -set fd [open test$cnt.sql w] -puts $fd "BEGIN;" -puts $fd "CREATE TABLE t2(a INTEGER, b INTEGER, c VARCHAR(100));" -for {set i 1} {$i<=25000} {incr i} { - set r [expr {int(rand()*500000)}] - puts $fd "INSERT INTO t2 VALUES($i,$r,'[number_name $r]');" -} -puts $fd "COMMIT;" -close $fd -runtest {25000 INSERTs in a transaction} - - - -set fd [open test$cnt.sql w] -for {set i 0} {$i<100} {incr i} { - set lwr [expr {$i*100}] - set upr [expr {($i+10)*100}] - puts $fd "SELECT count(*), avg(b) FROM t2 WHERE b>=$lwr AND b<$upr;" -} -close $fd -runtest {100 SELECTs without an index} - - - -set fd [open test$cnt.sql w] -for {set i 1} {$i<=100} {incr i} { - puts $fd "SELECT count(*), avg(b) FROM t2 WHERE c LIKE '%[number_name $i]%';" -} -close $fd -runtest {100 SELECTs on a string comparison} - - - -set fd [open test$cnt.sql w] -puts $fd {CREATE INDEX i2a ON t2(a);} -puts $fd {CREATE INDEX i2b ON t2(b);} -close $fd -runtest {Creating an index} - - - -set fd [open test$cnt.sql w] -for {set i 0} {$i<5000} {incr i} { - set lwr [expr {$i*100}] - set upr [expr {($i+1)*100}] - puts $fd "SELECT count(*), avg(b) FROM t2 WHERE b>=$lwr AND b<$upr;" -} -close $fd -runtest {5000 SELECTs with an index} - - - -set fd [open test$cnt.sql w] -puts $fd "BEGIN;" -for {set i 0} {$i<1000} {incr i} { - set lwr [expr {$i*10}] - set upr [expr {($i+1)*10}] - puts $fd "UPDATE t1 SET b=b*2 WHERE a>=$lwr AND a<$upr;" -} -puts $fd "COMMIT;" -close $fd -runtest {1000 UPDATEs without an index} - - - -set fd [open test$cnt.sql w] -puts $fd "BEGIN;" -for {set i 1} {$i<=25000} {incr i} { - set r [expr {int(rand()*500000)}] - puts $fd "UPDATE t2 SET b=$r WHERE a=$i;" -} -puts $fd "COMMIT;" -close $fd -runtest {25000 UPDATEs with an index} - - -set fd [open test$cnt.sql w] -puts $fd "BEGIN;" -for {set i 1} {$i<=25000} {incr i} { - set r [expr {int(rand()*500000)}] - puts $fd "UPDATE t2 SET c='[number_name $r]' WHERE a=$i;" -} -puts $fd "COMMIT;" -close $fd -runtest {25000 text UPDATEs with an index} - - - -set fd [open test$cnt.sql w] -puts $fd "BEGIN;" -puts $fd "INSERT INTO t1 SELECT * FROM t2;" -puts $fd "INSERT INTO t2 SELECT * FROM t1;" -puts $fd "COMMIT;" -close $fd -runtest {INSERTs from a SELECT} - - - -set fd [open test$cnt.sql w] -puts $fd {DELETE FROM t2 WHERE c LIKE '%fifty%';} -close $fd -runtest {DELETE without an index} - - - -set fd [open test$cnt.sql w] -puts $fd {DELETE FROM t2 WHERE a>10 AND a<20000;} -close $fd -runtest {DELETE with an index} - - - -set fd [open test$cnt.sql w] -puts $fd {INSERT INTO t2 SELECT * FROM t1;} -close $fd -runtest {A big INSERT after a big DELETE} - - - -set fd [open test$cnt.sql w] -puts $fd {BEGIN;} -puts $fd {DELETE FROM t1;} -for {set i 1} {$i<=3000} {incr i} { - set r [expr {int(rand()*100000)}] - puts $fd "INSERT INTO t1 VALUES($i,$r,'[number_name $r]');" -} -puts $fd {COMMIT;} -close $fd -runtest {A big DELETE followed by many small INSERTs} - - - -set fd [open test$cnt.sql w] -puts $fd {DROP TABLE t1;} -puts $fd {DROP TABLE t2;} -close $fd -runtest {DROP TABLE} diff --git a/tool/speedtest16.c b/tool/speedtest16.c deleted file mode 100644 index 993cc19268..0000000000 --- a/tool/speedtest16.c +++ /dev/null @@ -1,171 +0,0 @@ -/* -** Performance test for SQLite. -** -** This program reads ASCII text from a file named on the command-line. -** It converts each SQL statement into UTF16 and submits it to SQLite -** for evaluation. A new UTF16 database is created at the beginning of -** the program. All statements are timed using the high-resolution timer -** built into Intel-class processors. -** -** To compile this program, first compile the SQLite library separately -** will full optimizations. For example: -** -** gcc -c -O6 -DSQLITE_THREADSAFE=0 sqlite3.c -** -** Then link against this program. But to do optimize this program -** because that defeats the hi-res timer. -** -** gcc speedtest16.c sqlite3.o -ldl -I../src -** -** Then run this program with a single argument which is the name of -** a file containing SQL script that you want to test: -** -** ./a.out database.db test.sql -*/ -#include <stdio.h> -#include <string.h> -#include <stdlib.h> -#include <ctype.h> -#include <unistd.h> -#include "sqlite3.h" - -#define ISSPACE(X) isspace((unsigned char)(X)) - -/* -** hwtime.h contains inline assembler code for implementing -** high-performance timing routines. -*/ -#include "hwtime.h" - -/* -** Convert a zero-terminated ASCII string into a zero-terminated -** UTF-16le string. Memory to hold the returned string comes -** from malloc() and should be freed by the caller. -*/ -static void *asciiToUtf16le(const char *z){ - int n = strlen(z); - char *z16; - int i, j; - - z16 = malloc( n*2 + 2 ); - for(i=j=0; i<=n; i++){ - z16[j++] = z[i]; - z16[j++] = 0; - } - return (void*)z16; -} - -/* -** Timers -*/ -static sqlite_uint64 prepTime = 0; -static sqlite_uint64 runTime = 0; -static sqlite_uint64 finalizeTime = 0; - -/* -** Prepare and run a single statement of SQL. -*/ -static void prepareAndRun(sqlite3 *db, const char *zSql){ - void *utf16; - sqlite3_stmt *pStmt; - const void *stmtTail; - sqlite_uint64 iStart, iElapse; - int rc; - - printf("****************************************************************\n"); - printf("SQL statement: [%s]\n", zSql); - utf16 = asciiToUtf16le(zSql); - iStart = sqlite3Hwtime(); - rc = sqlite3_prepare16_v2(db, utf16, -1, &pStmt, &stmtTail); - iElapse = sqlite3Hwtime() - iStart; - prepTime += iElapse; - printf("sqlite3_prepare16_v2() returns %d in %llu cycles\n", rc, iElapse); - if( rc==SQLITE_OK ){ - int nRow = 0; - iStart = sqlite3Hwtime(); - while( (rc=sqlite3_step(pStmt))==SQLITE_ROW ){ nRow++; } - iElapse = sqlite3Hwtime() - iStart; - runTime += iElapse; - printf("sqlite3_step() returns %d after %d rows in %llu cycles\n", - rc, nRow, iElapse); - iStart = sqlite3Hwtime(); - rc = sqlite3_finalize(pStmt); - iElapse = sqlite3Hwtime() - iStart; - finalizeTime += iElapse; - printf("sqlite3_finalize() returns %d in %llu cycles\n", rc, iElapse); - } - free(utf16); -} - -int main(int argc, char **argv){ - void *utf16; - sqlite3 *db; - int rc; - int nSql; - char *zSql; - int i, j; - FILE *in; - sqlite_uint64 iStart, iElapse; - sqlite_uint64 iSetup = 0; - int nStmt = 0; - int nByte = 0; - - if( argc!=3 ){ - fprintf(stderr, "Usage: %s FILENAME SQL-SCRIPT\n" - "Runs SQL-SCRIPT as UTF16 against a UTF16 database\n", - argv[0]); - exit(1); - } - in = fopen(argv[2], "r"); - fseek(in, 0L, SEEK_END); - nSql = ftell(in); - zSql = malloc( nSql+1 ); - fseek(in, 0L, SEEK_SET); - nSql = fread(zSql, 1, nSql, in); - zSql[nSql] = 0; - - printf("SQLite version: %d\n", sqlite3_libversion_number()); - unlink(argv[1]); - utf16 = asciiToUtf16le(argv[1]); - iStart = sqlite3Hwtime(); - rc = sqlite3_open16(utf16, &db); - iElapse = sqlite3Hwtime() - iStart; - iSetup = iElapse; - printf("sqlite3_open16() returns %d in %llu cycles\n", rc, iElapse); - free(utf16); - for(i=j=0; j<nSql; j++){ - if( zSql[j]==';' ){ - int isComplete; - char c = zSql[j+1]; - zSql[j+1] = 0; - isComplete = sqlite3_complete(&zSql[i]); - zSql[j+1] = c; - if( isComplete ){ - zSql[j] = 0; - while( i<j && ISSPACE(zSql[i]) ){ i++; } - if( i<j ){ - nStmt++; - nByte += j-i; - prepareAndRun(db, &zSql[i]); - } - zSql[j] = ';'; - i = j+1; - } - } - } - iStart = sqlite3Hwtime(); - sqlite3_close(db); - iElapse = sqlite3Hwtime() - iStart; - iSetup += iElapse; - printf("sqlite3_close() returns in %llu cycles\n", iElapse); - printf("\n"); - printf("Statements run: %15d\n", nStmt); - printf("Bytes of SQL text: %15d\n", nByte); - printf("Total prepare time: %15llu cycles\n", prepTime); - printf("Total run time: %15llu cycles\n", runTime); - printf("Total finalize time: %15llu cycles\n", finalizeTime); - printf("Open/Close time: %15llu cycles\n", iSetup); - printf("Total Time: %15llu cycles\n", - prepTime + runTime + finalizeTime + iSetup); - return 0; -} diff --git a/tool/speedtest2.tcl b/tool/speedtest2.tcl deleted file mode 100644 index 4fd632d4c7..0000000000 --- a/tool/speedtest2.tcl +++ /dev/null @@ -1,207 +0,0 @@ -#!/usr/bin/tclsh -# -# Run this script using TCLSH to do a speed comparison between -# various versions of SQLite and PostgreSQL and MySQL -# - -# Run a test -# -set cnt 1 -proc runtest {title} { - global cnt - set sqlfile test$cnt.sql - puts "<h2>Test $cnt: $title</h2>" - incr cnt - set fd [open $sqlfile r] - set sql [string trim [read $fd [file size $sqlfile]]] - close $fd - set sx [split $sql \n] - set n [llength $sx] - if {$n>8} { - set sql {} - for {set i 0} {$i<3} {incr i} {append sql [lindex $sx $i]<br>\n} - append sql "<i>... [expr {$n-6}] lines omitted</i><br>\n" - for {set i [expr {$n-3}]} {$i<$n} {incr i} { - append sql [lindex $sx $i]<br>\n - } - } else { - regsub -all \n [string trim $sql] <br> sql - } - puts "<blockquote>" - puts "$sql" - puts "</blockquote><table border=0 cellpadding=0 cellspacing=0>" - set format {<tr><td>%s</td><td align="right">&nbsp;&nbsp;&nbsp;%.3f</td></tr>} - set delay 1000 - exec sync; after $delay; - set t [time "exec psql drh <$sqlfile" 1] - set t [expr {[lindex $t 0]/1000000.0}] - puts [format $format PostgreSQL: $t] - exec sync; after $delay; - set t [time "exec mysql -f drh <$sqlfile" 1] - set t [expr {[lindex $t 0]/1000000.0}] - puts [format $format MySQL: $t] -# set t [time "exec ./sqlite232 s232.db <$sqlfile" 1] -# set t [expr {[lindex $t 0]/1000000.0}] -# puts [format $format {SQLite 2.3.2:} $t] -# set t [time "exec ./sqlite-100 s100.db <$sqlfile" 1] -# set t [expr {[lindex $t 0]/1000000.0}] -# puts [format $format {SQLite 2.4 (cache=100):} $t] - exec sync; after $delay; - set t [time "exec ./sqlite240 s2k.db <$sqlfile" 1] - set t [expr {[lindex $t 0]/1000000.0}] - puts [format $format {SQLite 2.4:} $t] - exec sync; after $delay; - set t [time "exec ./sqlite240 sns.db <$sqlfile" 1] - set t [expr {[lindex $t 0]/1000000.0}] - puts [format $format {SQLite 2.4 (nosync):} $t] -# set t [time "exec ./sqlite-t1 st1.db <$sqlfile" 1] -# set t [expr {[lindex $t 0]/1000000.0}] -# puts [format $format {SQLite 2.4 (test):} $t] - puts "</table>" -} - -# Initialize the environment -# -expr srand(1) -catch {exec /bin/sh -c {rm -f s*.db}} -set fd [open clear.sql w] -puts $fd { - drop table t1; - drop table t2; -} -close $fd -catch {exec psql drh <clear.sql} -catch {exec mysql drh <clear.sql} -set fd [open 2kinit.sql w] -puts $fd { - PRAGMA default_cache_size=2000; - PRAGMA default_synchronous=on; -} -close $fd -exec ./sqlite240 s2k.db <2kinit.sql -exec ./sqlite-t1 st1.db <2kinit.sql -set fd [open nosync-init.sql w] -puts $fd { - PRAGMA default_cache_size=2000; - PRAGMA default_synchronous=off; -} -close $fd -exec ./sqlite240 sns.db <nosync-init.sql -set ones {zero one two three four five six seven eight nine - ten eleven twelve thirteen fourteen fifteen sixteen seventeen - eighteen nineteen} -set tens {{} ten twenty thirty forty fifty sixty seventy eighty ninety} -proc number_name {n} { - if {$n>=1000} { - set txt "[number_name [expr {$n/1000}]] thousand" - set n [expr {$n%1000}] - } else { - set txt {} - } - if {$n>=100} { - append txt " [lindex $::ones [expr {$n/100}]] hundred" - set n [expr {$n%100}] - } - if {$n>=20} { - append txt " [lindex $::tens [expr {$n/10}]]" - set n [expr {$n%10}] - } - if {$n>0} { - append txt " [lindex $::ones $n]" - } - set txt [string trim $txt] - if {$txt==""} {set txt zero} - return $txt -} - - -set fd [open test$cnt.sql w] -puts $fd "BEGIN;" -puts $fd "CREATE TABLE t1(a INTEGER, b INTEGER, c VARCHAR(100));" -for {set i 1} {$i<=25000} {incr i} { - set r [expr {int(rand()*500000)}] - puts $fd "INSERT INTO t1 VALUES($i,$r,'[number_name $r]');" -} -puts $fd "COMMIT;" -close $fd -runtest {25000 INSERTs in a transaction} - - -set fd [open test$cnt.sql w] -puts $fd "DELETE FROM t1;" -close $fd -runtest {DELETE everything} - - -set fd [open test$cnt.sql w] -puts $fd "BEGIN;" -for {set i 1} {$i<=25000} {incr i} { - set r [expr {int(rand()*500000)}] - puts $fd "INSERT INTO t1 VALUES($i,$r,'[number_name $r]');" -} -puts $fd "COMMIT;" -close $fd -runtest {25000 INSERTs in a transaction} - - -set fd [open test$cnt.sql w] -puts $fd "DELETE FROM t1;" -close $fd -runtest {DELETE everything} - - -set fd [open test$cnt.sql w] -puts $fd "BEGIN;" -for {set i 1} {$i<=25000} {incr i} { - set r [expr {int(rand()*500000)}] - puts $fd "INSERT INTO t1 VALUES($i,$r,'[number_name $r]');" -} -puts $fd "COMMIT;" -close $fd -runtest {25000 INSERTs in a transaction} - - -set fd [open test$cnt.sql w] -puts $fd "DELETE FROM t1;" -close $fd -runtest {DELETE everything} - - -set fd [open test$cnt.sql w] -puts $fd "BEGIN;" -for {set i 1} {$i<=25000} {incr i} { - set r [expr {int(rand()*500000)}] - puts $fd "INSERT INTO t1 VALUES($i,$r,'[number_name $r]');" -} -puts $fd "COMMIT;" -close $fd -runtest {25000 INSERTs in a transaction} - - -set fd [open test$cnt.sql w] -puts $fd "DELETE FROM t1;" -close $fd -runtest {DELETE everything} - - -set fd [open test$cnt.sql w] -puts $fd "BEGIN;" -for {set i 1} {$i<=25000} {incr i} { - set r [expr {int(rand()*500000)}] - puts $fd "INSERT INTO t1 VALUES($i,$r,'[number_name $r]');" -} -puts $fd "COMMIT;" -close $fd -runtest {25000 INSERTs in a transaction} - - -set fd [open test$cnt.sql w] -puts $fd "DELETE FROM t1;" -close $fd -runtest {DELETE everything} - - -set fd [open test$cnt.sql w] -puts $fd {DROP TABLE t1;} -close $fd -runtest {DROP TABLE} diff --git a/tool/speedtest8.c b/tool/speedtest8.c deleted file mode 100644 index 051fc89819..0000000000 --- a/tool/speedtest8.c +++ /dev/null @@ -1,260 +0,0 @@ -/* -** Performance test for SQLite. -** -** This program reads ASCII text from a file named on the command-line -** and submits that text to SQLite for evaluation. A new database -** is created at the beginning of the program. All statements are -** timed using the high-resolution timer built into Intel-class processors. -** -** To compile this program, first compile the SQLite library separately -** will full optimizations. For example: -** -** gcc -c -O6 -DSQLITE_THREADSAFE=0 sqlite3.c -** -** Then link against this program. But to do optimize this program -** because that defeats the hi-res timer. -** -** gcc speedtest8.c sqlite3.o -ldl -I../src -** -** Then run this program with a single argument which is the name of -** a file containing SQL script that you want to test: -** -** ./a.out test.db test.sql -*/ -#include <stdio.h> -#include <string.h> -#include <stdlib.h> -#include <ctype.h> -#include <time.h> - -#if defined(_MSC_VER) -#include <windows.h> -#else -#include <unistd.h> -#include <sys/times.h> -#include <sched.h> -#endif - -#include "sqlite3.h" - -/* -** hwtime.h contains inline assembler code for implementing -** high-performance timing routines. -*/ -#include "hwtime.h" - -/* -** Timers -*/ -static sqlite_uint64 prepTime = 0; -static sqlite_uint64 runTime = 0; -static sqlite_uint64 finalizeTime = 0; - -/* -** Prepare and run a single statement of SQL. -*/ -static void prepareAndRun(sqlite3 *db, const char *zSql, int bQuiet){ - sqlite3_stmt *pStmt; - const char *stmtTail; - sqlite_uint64 iStart, iElapse; - int rc; - - if (!bQuiet){ - printf("***************************************************************\n"); - } - if (!bQuiet) printf("SQL statement: [%s]\n", zSql); - iStart = sqlite3Hwtime(); - rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &stmtTail); - iElapse = sqlite3Hwtime() - iStart; - prepTime += iElapse; - if (!bQuiet){ - printf("sqlite3_prepare_v2() returns %d in %llu cycles\n", rc, iElapse); - } - if( rc==SQLITE_OK ){ - int nRow = 0; - iStart = sqlite3Hwtime(); - while( (rc=sqlite3_step(pStmt))==SQLITE_ROW ){ nRow++; } - iElapse = sqlite3Hwtime() - iStart; - runTime += iElapse; - if (!bQuiet){ - printf("sqlite3_step() returns %d after %d rows in %llu cycles\n", - rc, nRow, iElapse); - } - iStart = sqlite3Hwtime(); - rc = sqlite3_finalize(pStmt); - iElapse = sqlite3Hwtime() - iStart; - finalizeTime += iElapse; - if (!bQuiet){ - printf("sqlite3_finalize() returns %d in %llu cycles\n", rc, iElapse); - } - } -} - -int main(int argc, char **argv){ - sqlite3 *db; - int rc; - int nSql; - char *zSql; - int i, j; - FILE *in; - sqlite_uint64 iStart, iElapse; - sqlite_uint64 iSetup = 0; - int nStmt = 0; - int nByte = 0; - const char *zArgv0 = argv[0]; - int bQuiet = 0; -#if !defined(_MSC_VER) - struct tms tmsStart, tmsEnd; - clock_t clkStart, clkEnd; -#endif - -#ifdef HAVE_OSINST - extern sqlite3_vfs *sqlite3_instvfs_binarylog(char *, char *, char *); - extern void sqlite3_instvfs_destroy(sqlite3_vfs *); - sqlite3_vfs *pVfs = 0; -#endif - - while (argc>3) - { -#ifdef HAVE_OSINST - if( argc>4 && (strcmp(argv[1], "-log")==0) ){ - pVfs = sqlite3_instvfs_binarylog("oslog", 0, argv[2]); - sqlite3_vfs_register(pVfs, 1); - argv += 2; - argc -= 2; - continue; - } -#endif - - /* - ** Increasing the priority slightly above normal can help with - ** repeatability of testing. Note that with Cygwin, -5 equates - ** to "High", +5 equates to "Low", and anything in between - ** equates to "Normal". - */ - if( argc>4 && (strcmp(argv[1], "-priority")==0) ){ -#if defined(_MSC_VER) - int new_priority = atoi(argv[2]); - if(!SetPriorityClass(GetCurrentProcess(), - (new_priority<=-5) ? HIGH_PRIORITY_CLASS : - (new_priority<=0) ? ABOVE_NORMAL_PRIORITY_CLASS : - (new_priority==0) ? NORMAL_PRIORITY_CLASS : - (new_priority<5) ? BELOW_NORMAL_PRIORITY_CLASS : - IDLE_PRIORITY_CLASS)){ - printf ("error setting priority\n"); - exit(2); - } -#else - struct sched_param myParam; - sched_getparam(0, &myParam); - printf ("Current process priority is %d.\n", (int)myParam.sched_priority); - myParam.sched_priority = atoi(argv[2]); - printf ("Setting process priority to %d.\n", (int)myParam.sched_priority); - if (sched_setparam (0, &myParam) != 0){ - printf ("error setting priority\n"); - exit(2); - } -#endif - argv += 2; - argc -= 2; - continue; - } - - if( argc>3 && strcmp(argv[1], "-quiet")==0 ){ - bQuiet = -1; - argv++; - argc--; - continue; - } - - break; - } - - if( argc!=3 ){ - fprintf(stderr, "Usage: %s [options] FILENAME SQL-SCRIPT\n" - "Runs SQL-SCRIPT against a UTF8 database\n" - "\toptions:\n" -#ifdef HAVE_OSINST - "\t-log <log>\n" -#endif - "\t-priority <value> : set priority of task\n" - "\t-quiet : only display summary results\n", - zArgv0); - exit(1); - } - - in = fopen(argv[2], "r"); - fseek(in, 0L, SEEK_END); - nSql = ftell(in); - zSql = malloc( nSql+1 ); - fseek(in, 0L, SEEK_SET); - nSql = fread(zSql, 1, nSql, in); - zSql[nSql] = 0; - - printf("SQLite version: %d\n", sqlite3_libversion_number()); - unlink(argv[1]); -#if !defined(_MSC_VER) - clkStart = times(&tmsStart); -#endif - iStart = sqlite3Hwtime(); - rc = sqlite3_open(argv[1], &db); - iElapse = sqlite3Hwtime() - iStart; - iSetup = iElapse; - if (!bQuiet) printf("sqlite3_open() returns %d in %llu cycles\n", rc, iElapse); - for(i=j=0; j<nSql; j++){ - if( zSql[j]==';' ){ - int isComplete; - char c = zSql[j+1]; - zSql[j+1] = 0; - isComplete = sqlite3_complete(&zSql[i]); - zSql[j+1] = c; - if( isComplete ){ - zSql[j] = 0; - while( i<j && isspace(zSql[i]) ){ i++; } - if( i<j ){ - int n = j - i; - if( n>=6 && memcmp(&zSql[i], ".crash",6)==0 ) exit(1); - nStmt++; - nByte += n; - prepareAndRun(db, &zSql[i], bQuiet); - } - zSql[j] = ';'; - i = j+1; - } - } - } - iStart = sqlite3Hwtime(); - sqlite3_close(db); - iElapse = sqlite3Hwtime() - iStart; -#if !defined(_MSC_VER) - clkEnd = times(&tmsEnd); -#endif - iSetup += iElapse; - if (!bQuiet) printf("sqlite3_close() returns in %llu cycles\n", iElapse); - - printf("\n"); - printf("Statements run: %15d stmts\n", nStmt); - printf("Bytes of SQL text: %15d bytes\n", nByte); - printf("Total prepare time: %15llu cycles\n", prepTime); - printf("Total run time: %15llu cycles\n", runTime); - printf("Total finalize time: %15llu cycles\n", finalizeTime); - printf("Open/Close time: %15llu cycles\n", iSetup); - printf("Total time: %15llu cycles\n", - prepTime + runTime + finalizeTime + iSetup); - -#if !defined(_MSC_VER) - printf("\n"); - printf("Total user CPU time: %15.3g secs\n", (tmsEnd.tms_utime - tmsStart.tms_utime)/(double)CLOCKS_PER_SEC ); - printf("Total system CPU time: %15.3g secs\n", (tmsEnd.tms_stime - tmsStart.tms_stime)/(double)CLOCKS_PER_SEC ); - printf("Total real time: %15.3g secs\n", (clkEnd -clkStart)/(double)CLOCKS_PER_SEC ); -#endif - -#ifdef HAVE_OSINST - if( pVfs ){ - sqlite3_instvfs_destroy(pVfs); - printf("vfs log written to %s\n", argv[0]); - } -#endif - - return 0; -} diff --git a/tool/speedtest8inst1.c b/tool/speedtest8inst1.c deleted file mode 100644 index ceaeca0f16..0000000000 --- a/tool/speedtest8inst1.c +++ /dev/null @@ -1,218 +0,0 @@ -/* -** Performance test for SQLite. -** -** This program reads ASCII text from a file named on the command-line -** and submits that text to SQLite for evaluation. A new database -** is created at the beginning of the program. All statements are -** timed using the high-resolution timer built into Intel-class processors. -** -** To compile this program, first compile the SQLite library separately -** will full optimizations. For example: -** -** gcc -c -O6 -DSQLITE_THREADSAFE=0 sqlite3.c -** -** Then link against this program. But to do optimize this program -** because that defeats the hi-res timer. -** -** gcc speedtest8.c sqlite3.o -ldl -I../src -** -** Then run this program with a single argument which is the name of -** a file containing SQL script that you want to test: -** -** ./a.out test.db test.sql -*/ -#include <stdio.h> -#include <string.h> -#include <stdlib.h> -#include <ctype.h> -#include <unistd.h> -#include <stdarg.h> -#include "sqlite3.h" - -#define ISSPACE(X) isspace((unsigned char)(X)) - -#include "test_osinst.c" - -/* -** Prepare and run a single statement of SQL. -*/ -static void prepareAndRun(sqlite3_vfs *pInstVfs, sqlite3 *db, const char *zSql){ - sqlite3_stmt *pStmt; - const char *stmtTail; - int rc; - char zMessage[1024]; - zMessage[1023] = '\0'; - - sqlite3_uint64 iTime; - - sqlite3_snprintf(1023, zMessage, "sqlite3_prepare_v2: %s", zSql); - sqlite3_instvfs_binarylog_marker(pInstVfs, zMessage); - - iTime = sqlite3Hwtime(); - rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &stmtTail); - iTime = sqlite3Hwtime() - iTime; - sqlite3_instvfs_binarylog_call(pInstVfs,BINARYLOG_PREPARE_V2,iTime,rc,zSql); - - if( rc==SQLITE_OK ){ - int nRow = 0; - - sqlite3_snprintf(1023, zMessage, "sqlite3_step loop: %s", zSql); - sqlite3_instvfs_binarylog_marker(pInstVfs, zMessage); - iTime = sqlite3Hwtime(); - while( (rc=sqlite3_step(pStmt))==SQLITE_ROW ){ nRow++; } - iTime = sqlite3Hwtime() - iTime; - sqlite3_instvfs_binarylog_call(pInstVfs, BINARYLOG_STEP, iTime, rc, zSql); - - sqlite3_snprintf(1023, zMessage, "sqlite3_finalize: %s", zSql); - sqlite3_instvfs_binarylog_marker(pInstVfs, zMessage); - iTime = sqlite3Hwtime(); - rc = sqlite3_finalize(pStmt); - iTime = sqlite3Hwtime() - iTime; - sqlite3_instvfs_binarylog_call(pInstVfs, BINARYLOG_FINALIZE, iTime, rc, zSql); - } -} - -static int stringcompare(const char *zLeft, const char *zRight){ - int ii; - for(ii=0; zLeft[ii] && zRight[ii]; ii++){ - if( zLeft[ii]!=zRight[ii] ) return 0; - } - return( zLeft[ii]==zRight[ii] ); -} - -static char *readScriptFile(const char *zFile, int *pnScript){ - sqlite3_vfs *pVfs = sqlite3_vfs_find(0); - sqlite3_file *p; - int rc; - sqlite3_int64 nByte; - char *zData = 0; - int flags = SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_DB; - - p = (sqlite3_file *)malloc(pVfs->szOsFile); - rc = pVfs->xOpen(pVfs, zFile, p, flags, &flags); - if( rc!=SQLITE_OK ){ - goto error_out; - } - - rc = p->pMethods->xFileSize(p, &nByte); - if( rc!=SQLITE_OK ){ - goto close_out; - } - - zData = (char *)malloc(nByte+1); - rc = p->pMethods->xRead(p, zData, nByte, 0); - if( rc!=SQLITE_OK ){ - goto close_out; - } - zData[nByte] = '\0'; - - p->pMethods->xClose(p); - free(p); - *pnScript = nByte; - return zData; - -close_out: - p->pMethods->xClose(p); - -error_out: - free(p); - free(zData); - return 0; -} - -int main(int argc, char **argv){ - - const char zUsageMsg[] = - "Usage: %s options...\n" - " where available options are:\n" - "\n" - " -db DATABASE-FILE (database file to operate on)\n" - " -script SCRIPT-FILE (script file to read sql from)\n" - " -log LOG-FILE (log file to create)\n" - " -logdata (log all data to log file)\n" - "\n" - " Options -db, -script and -log are compulsory\n" - "\n" - ; - - const char *zDb = 0; - const char *zScript = 0; - const char *zLog = 0; - int logdata = 0; - - int ii; - int i, j; - int rc; - - sqlite3_vfs *pInstVfs; /* Instrumentation VFS */ - - char *zSql = 0; - int nSql; - - sqlite3 *db; - - for(ii=1; ii<argc; ii++){ - if( stringcompare("-db", argv[ii]) && (ii+1)<argc ){ - zDb = argv[++ii]; - } - - else if( stringcompare("-script", argv[ii]) && (ii+1)<argc ){ - zScript = argv[++ii]; - } - - else if( stringcompare("-log", argv[ii]) && (ii+1)<argc ){ - zLog = argv[++ii]; - } - - else if( stringcompare("-logdata", argv[ii]) ){ - logdata = 1; - } - - else { - goto usage; - } - } - if( !zDb || !zScript || !zLog ) goto usage; - - zSql = readScriptFile(zScript, &nSql); - if( !zSql ){ - fprintf(stderr, "Failed to read script file\n"); - return -1; - } - - pInstVfs = sqlite3_instvfs_binarylog("logging", 0, zLog, logdata); - - rc = sqlite3_open_v2( - zDb, &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, "logging" - ); - if( rc!=SQLITE_OK ){ - fprintf(stderr, "Failed to open db: %s\n", sqlite3_errmsg(db)); - return -2; - } - - for(i=j=0; j<nSql; j++){ - if( zSql[j]==';' ){ - int isComplete; - char c = zSql[j+1]; - zSql[j+1] = 0; - isComplete = sqlite3_complete(&zSql[i]); - zSql[j+1] = c; - if( isComplete ){ - zSql[j] = 0; - while( i<j && ISSPACE(zSql[i]) ){ i++; } - if( i<j ){ - prepareAndRun(pInstVfs, db, &zSql[i]); - } - zSql[j] = ';'; - i = j+1; - } - } - } - - sqlite3_instvfs_destroy(pInstVfs); - return 0; - -usage: - fprintf(stderr, zUsageMsg, argv[0]); - return -3; -} diff --git a/tool/spellsift.tcl b/tool/spellsift.tcl new file mode 100755 index 0000000000..4e67c3e264 --- /dev/null +++ b/tool/spellsift.tcl @@ -0,0 +1,74 @@ +#!/usr/bin/tclsh + +set usage { + Usage: spellsift.tcl <source_filenames> + The named .c and .h source files comment blocks are spell-checked. +} + +if {[llength $argv] == 0} { + puts stderr $usage + exit 0 +} + +# Want a Tcl version with 3-argument close. +package require Tcl 8.6 + +set ::spellchk "aspell --extra-dicts ./custom.rws list" + +# Run text through aspell with custom dictionary, return finds. +proc misspelled {text} { + set spellerr [open "|$::spellchk" r+] + puts $spellerr $text + flush $spellerr + close $spellerr write + set huhq [regsub {\s*$} [read $spellerr] {}] + close $spellerr read + return [split $huhq "\n"] +} + +# Eliminate some common patterns that need not be well spelled. +proc decruft {text} { + set nopp [regsub -all "\n *#\[^\n\]*\n" $text "\n\n" ] + set noticket [regsub -all {Ticket \[?[0-9a-f]+\]?} $nopp "" ] + return $noticket +} + +# Sift out common variable spellings not in normal dictionaries. +proc varsift {words} { + set rv [list] + foreach w $words { + set n [string length $w] + set cr [string range $w 1 end] + if {[string tolower $cr] ne $cr} continue + lappend rv $w; + } + return $rv +} + +foreach fname $argv { + set ich [open $fname r] + set dtext [decruft [read $ich]] + close $ich + set cbounds [regexp -indices -inline -all {(/\*)|(\*/)} $dtext] + set ccb -1 + set cblocks [list] + foreach {ap cb ce} $cbounds { + set cib [lindex $cb 1] + set cie [lindex $ce 0] + if {$cie != -1} { + if {$ccb != -1} { + set cce [expr $cie - 1] + set destar [string map [list * " "] [string range $dtext $ccb $cce]] + lappend cblocks $destar + set ccb -1 + } else continue + } elseif {$cib != -1} { + set ccb [expr $cib + 1] + } + } + set oddspells [varsift [misspelled [join $cblocks "\n"]]] + if {[llength $oddspells] > 0} { + puts "!? Misspellings from $fname:" + puts [join [lsort -nocase -unique $oddspells] "\n"] + } +} diff --git a/tool/split-sqlite3c.tcl b/tool/split-sqlite3c.tcl index 287b752828..3554933cd9 100644 --- a/tool/split-sqlite3c.tcl +++ b/tool/split-sqlite3c.tcl @@ -15,6 +15,7 @@ set END {^/\*+ End of %s \*+/} set in [open sqlite3.c] set out1 [open sqlite3-all.c w] +fconfigure $out1 -translation binary # Copy the header from sqlite3.c into sqlite3-all.c # @@ -47,7 +48,15 @@ set filecnt 0 proc write_one_file {content} { global filecnt incr filecnt - set out [open sqlite3-$filecnt.c w] + set label $filecnt + if {$filecnt>9} { + set label [string index ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnop \ + [expr {$filecnt-10}]] + } else { + set label $filecnt + } + set out [open sqlite3-$label.c w] + fconfigure $out -translation text puts -nonewline $out $content close $out puts $::out1 "#include \"sqlite3-$filecnt.c\"" @@ -72,6 +81,11 @@ while {[regexp $BEGIN $line]} { incr N $n while {[gets $in line]>=0} { if {[regexp $BEGIN $line]} break + if {$N>0} { + write_one_file $all + set N 0 + set all {} + } puts $out1 $line } } diff --git a/tool/sqldiff.c b/tool/sqldiff.c index ae01cd3c4d..44bf488f86 100644 --- a/tool/sqldiff.c +++ b/tool/sqldiff.c @@ -13,7 +13,8 @@ ** This is a utility program that computes the differences in content ** between two SQLite databases. ** -** To compile, simply link against SQLite. +** To compile, simply link against SQLite. (Windows builds must also link +** against ext/misc/sqlite3_stdio.c.) ** ** See the showHelp() routine below for a brief description of how to ** run the utility. @@ -25,6 +26,7 @@ #include <string.h> #include <assert.h> #include "sqlite3.h" +#include "sqlite3_stdio.h" /* ** All global variables are gathered into the "g" singleton. @@ -33,7 +35,9 @@ struct GlobalVars { const char *zArgv0; /* Name of program */ int bSchemaOnly; /* Only show schema differences */ int bSchemaPK; /* Use the schema-defined PK, not the true PK */ + int bHandleVtab; /* Handle fts3, fts4, fts5 and rtree vtabs */ unsigned fDebug; /* Debug flags */ + int bSchemaCompare; /* Doing single-table sqlite_schema compare */ sqlite3 *db; /* The database connection */ } g; @@ -44,35 +48,25 @@ struct GlobalVars { #define DEBUG_DIFF_SQL 0x000002 /* -** Dynamic string object +** Clear and free an sqlite3_str object */ -typedef struct Str Str; -struct Str { - char *z; /* Text of the string */ - int nAlloc; /* Bytes allocated in z[] */ - int nUsed; /* Bytes actually used in z[] */ -}; - -/* -** Initialize a Str object -*/ -static void strInit(Str *p){ - p->z = 0; - p->nAlloc = 0; - p->nUsed = 0; +static void strFree(sqlite3_str *pStr){ + sqlite3_free(sqlite3_str_finish(pStr)); } - + /* ** Print an error resulting from faulting command-line arguments and ** abort the program. */ static void cmdlineError(const char *zFormat, ...){ + sqlite3_str *pOut = sqlite3_str_new(0); va_list ap; - fprintf(stderr, "%s: ", g.zArgv0); va_start(ap, zFormat); - vfprintf(stderr, zFormat, ap); + sqlite3_str_vappendf(pOut, zFormat, ap); va_end(ap); - fprintf(stderr, "\n\"%s --help\" for more help\n", g.zArgv0); + sqlite3_fprintf(stderr, "%s: %s\n", g.zArgv0, sqlite3_str_value(pOut)); + strFree(pOut); + sqlite3_fprintf(stderr, "\"%s --help\" for more help\n", g.zArgv0); exit(1); } @@ -81,49 +75,16 @@ static void cmdlineError(const char *zFormat, ...){ ** abort the program. */ static void runtimeError(const char *zFormat, ...){ + sqlite3_str *pOut = sqlite3_str_new(0); va_list ap; - fprintf(stderr, "%s: ", g.zArgv0); va_start(ap, zFormat); - vfprintf(stderr, zFormat, ap); + sqlite3_str_vappendf(pOut, zFormat, ap); va_end(ap); - fprintf(stderr, "\n"); + sqlite3_fprintf(stderr, "%s: %s\n", g.zArgv0, sqlite3_str_value(pOut)); + strFree(pOut); exit(1); } -/* -** Free all memory held by a Str object -*/ -static void strFree(Str *p){ - sqlite3_free(p->z); - strInit(p); -} - -/* -** Add formatted text to the end of a Str object -*/ -static void strPrintf(Str *p, const char *zFormat, ...){ - int nNew; - for(;;){ - if( p->z ){ - va_list ap; - va_start(ap, zFormat); - sqlite3_vsnprintf(p->nAlloc-p->nUsed, p->z+p->nUsed, zFormat, ap); - va_end(ap); - nNew = (int)strlen(p->z + p->nUsed); - }else{ - nNew = p->nAlloc; - } - if( p->nUsed+nNew < p->nAlloc-1 ){ - p->nUsed += nNew; - break; - } - p->nAlloc = p->nAlloc*2 + 1000; - p->z = sqlite3_realloc(p->z, p->nAlloc); - if( p->z==0 ) runtimeError("out of memory"); - } -} - - /* Safely quote an SQL identifier. Use the minimum amount of transformation ** necessary to allow the string to be used with %s. @@ -133,28 +94,8 @@ static void strPrintf(Str *p, const char *zFormat, ...){ ** needed. */ static char *safeId(const char *zId){ - /* All SQLite keywords, in alphabetical order */ - static const char *azKeywords[] = { - "ABORT", "ACTION", "ADD", "AFTER", "ALL", "ALTER", "ANALYZE", "AND", "AS", - "ASC", "ATTACH", "AUTOINCREMENT", "BEFORE", "BEGIN", "BETWEEN", "BY", - "CASCADE", "CASE", "CAST", "CHECK", "COLLATE", "COLUMN", "COMMIT", - "CONFLICT", "CONSTRAINT", "CREATE", "CROSS", "CURRENT_DATE", - "CURRENT_TIME", "CURRENT_TIMESTAMP", "DATABASE", "DEFAULT", "DEFERRABLE", - "DEFERRED", "DELETE", "DESC", "DETACH", "DISTINCT", "DROP", "EACH", - "ELSE", "END", "ESCAPE", "EXCEPT", "EXCLUSIVE", "EXISTS", "EXPLAIN", - "FAIL", "FOR", "FOREIGN", "FROM", "FULL", "GLOB", "GROUP", "HAVING", "IF", - "IGNORE", "IMMEDIATE", "IN", "INDEX", "INDEXED", "INITIALLY", "INNER", - "INSERT", "INSTEAD", "INTERSECT", "INTO", "IS", "ISNULL", "JOIN", "KEY", - "LEFT", "LIKE", "LIMIT", "MATCH", "NATURAL", "NO", "NOT", "NOTNULL", - "NULL", "OF", "OFFSET", "ON", "OR", "ORDER", "OUTER", "PLAN", "PRAGMA", - "PRIMARY", "QUERY", "RAISE", "RECURSIVE", "REFERENCES", "REGEXP", - "REINDEX", "RELEASE", "RENAME", "REPLACE", "RESTRICT", "RIGHT", - "ROLLBACK", "ROW", "SAVEPOINT", "SELECT", "SET", "TABLE", "TEMP", - "TEMPORARY", "THEN", "TO", "TRANSACTION", "TRIGGER", "UNION", "UNIQUE", - "UPDATE", "USING", "VACUUM", "VALUES", "VIEW", "VIRTUAL", "WHEN", "WHERE", - "WITH", "WITHOUT", - }; - int lwr, upr, mid, c, i, x; + int i, x; + char c; if( zId[0]==0 ) return sqlite3_mprintf("\"\""); for(i=x=0; (c = zId[i])!=0; i++){ if( !isalpha(c) && c!='_' ){ @@ -165,20 +106,10 @@ static char *safeId(const char *zId){ } } } - if( x ) return sqlite3_mprintf("%s", zId); - lwr = 0; - upr = sizeof(azKeywords)/sizeof(azKeywords[0]) - 1; - while( lwr<=upr ){ - mid = (lwr+upr)/2; - c = sqlite3_stricmp(azKeywords[mid], zId); - if( c==0 ) return sqlite3_mprintf("\"%w\"", zId); - if( c<0 ){ - lwr = mid+1; - }else{ - upr = mid-1; - } + if( x || !sqlite3_keyword_check(zId,i) ){ + return sqlite3_mprintf("%s", zId); } - return sqlite3_mprintf("%s", zId); + return sqlite3_mprintf("\"%w\"", zId); } /* @@ -221,14 +152,14 @@ static void namelistFree(char **az){ } /* -** Return a list of column names for the table zDb.zTab. Space to +** Return a list of column names [a] for the table zDb.zTab. Space to ** hold the list is obtained from sqlite3_malloc() and should released ** using namelistFree() when no longer needed. ** ** Primary key columns are listed first, followed by data columns. ** The number of columns in the primary key is returned in *pnPkey. ** -** Normally, the "primary key" in the previous sentence is the true +** Normally [a], the "primary key" in the previous sentence is the true ** primary key - the rowid or INTEGER PRIMARY KEY for ordinary tables ** or the declared PRIMARY KEY for WITHOUT ROWID tables. However, if ** the g.bSchemaPK flag is set, then the schema-defined PRIMARY KEY is @@ -238,6 +169,9 @@ static void namelistFree(char **az){ ** If the primary key for a table is the rowid but rowid is inaccessible, ** then this routine returns a NULL pointer. ** +** [a. If the lone, named table is "sqlite_schema", "rootpage" column is +** omitted and the "type" and "name" columns are made to be the PK.] +** ** Examples: ** CREATE TABLE t1(a INT UNIQUE, b INTEGER, c TEXT, PRIMARY KEY(c)); ** *pnPKey = 1; @@ -271,7 +205,7 @@ static char **columnNames( int naz = 0; /* Number of entries in az[] */ sqlite3_stmt *pStmt; /* SQL statement being run */ char *zPkIdxName = 0; /* Name of the PRIMARY KEY index */ - int truePk = 0; /* PRAGMA table_info indentifies the PK to use */ + int truePk = 0; /* PRAGMA table_info identifies the PK to use */ int nPK = 0; /* Number of PRIMARY KEY columns */ int i, j; /* Loop counters */ @@ -328,19 +262,36 @@ static char **columnNames( if( nPK==0 ) nPK = 1; truePk = 1; } + if( g.bSchemaCompare ){ + assert( sqlite3_stricmp(zTab,"sqlite_schema")==0 + || sqlite3_stricmp(zTab,"sqlite_master")==0 ); + /* For sqlite_schema, will use type and name as the PK. */ + nPK = 2; + truePk = 0; + } *pnPKey = nPK; naz = nPK; az = sqlite3_malloc( sizeof(char*)*(nPK+1) ); if( az==0 ) runtimeError("out of memory"); memset(az, 0, sizeof(char*)*(nPK+1)); + if( g.bSchemaCompare ){ + az[0] = sqlite3_mprintf("%s", "type"); + az[1] = sqlite3_mprintf("%s", "name"); + } while( SQLITE_ROW==sqlite3_step(pStmt) ){ + char * sid = safeId((char*)sqlite3_column_text(pStmt,1)); int iPKey; if( truePk && (iPKey = sqlite3_column_int(pStmt,5))>0 ){ - az[iPKey-1] = safeId((char*)sqlite3_column_text(pStmt,1)); + az[iPKey-1] = sid; }else{ - az = sqlite3_realloc(az, sizeof(char*)*(naz+2) ); - if( az==0 ) runtimeError("out of memory"); - az[naz++] = safeId((char*)sqlite3_column_text(pStmt,1)); + if( !g.bSchemaCompare + || !(strcmp(sid,"rootpage")==0 + ||strcmp(sid,"name")==0 + ||strcmp(sid,"type")==0)){ + az = sqlite3_realloc(az, sizeof(char*)*(naz+2) ); + if( az==0 ) runtimeError("out of memory"); + az[naz++] = sid; + } } } sqlite3_finalize(pStmt); @@ -351,9 +302,11 @@ static char **columnNames( if( pbRowid ) *pbRowid = (az[0]==0); /* If this table has an implicit rowid for a PK, figure out how to refer - ** to it. There are three options - "rowid", "_rowid_" and "oid". Any - ** of these will work, unless the table has an explicit column of the - ** same name. */ + ** to it. There are usually three options - "rowid", "_rowid_" and "oid". + ** Any of these will work, unless the table has an explicit column of the + ** same name or the sqlite_schema tables are to be compared. In the latter + ** case, pretend that the "true" primary key is the name column, which + ** avoids extraneous diffs against the schemas due to rowid variance. */ if( az[0]==0 ){ const char *azRowid[] = { "rowid", "_rowid_", "oid" }; for(i=0; i<sizeof(azRowid)/sizeof(azRowid[0]); i++){ @@ -384,11 +337,11 @@ static void printQuoted(FILE *out, sqlite3_value *X){ char zBuf[50]; r1 = sqlite3_value_double(X); sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.15g", r1); - fprintf(out, "%s", zBuf); + sqlite3_fprintf(out, "%s", zBuf); break; } case SQLITE_INTEGER: { - fprintf(out, "%lld", sqlite3_value_int64(X)); + sqlite3_fprintf(out, "%lld", sqlite3_value_int64(X)); break; } case SQLITE_BLOB: { @@ -396,36 +349,53 @@ static void printQuoted(FILE *out, sqlite3_value *X){ int nBlob = sqlite3_value_bytes(X); if( zBlob ){ int i; - fprintf(out, "x'"); + sqlite3_fprintf(out, "x'"); for(i=0; i<nBlob; i++){ - fprintf(out, "%02x", zBlob[i]); + sqlite3_fprintf(out, "%02x", zBlob[i]); } - fprintf(out, "'"); + sqlite3_fprintf(out, "'"); }else{ - fprintf(out, "NULL"); + /* Could be an OOM, could be a zero-byte blob */ + sqlite3_fprintf(out, "X''"); } break; } case SQLITE_TEXT: { const unsigned char *zArg = sqlite3_value_text(X); - int i, j; if( zArg==0 ){ - fprintf(out, "NULL"); + sqlite3_fprintf(out, "NULL"); }else{ - fprintf(out, "'"); + int inctl = 0; + int i, j; + sqlite3_fprintf(out, "'"); for(i=j=0; zArg[i]; i++){ - if( zArg[i]=='\'' ){ - fprintf(out, "%.*s'", i-j+1, &zArg[j]); + char c = zArg[i]; + int ctl = iscntrl((unsigned char)c); + if( ctl>inctl ){ + inctl = ctl; + sqlite3_fprintf(out, "%.*s'||X'%02x", i-j, &zArg[j], c); + j = i+1; + }else if( ctl ){ + sqlite3_fprintf(out, "%02x", c); j = i+1; + }else{ + if( inctl ){ + inctl = 0; + sqlite3_fprintf(out, "'\n||'"); + } + if( c=='\'' ){ + sqlite3_fprintf(out, "%.*s'", i-j+1, &zArg[j]); + j = i+1; + } } } - fprintf(out, "%s'", &zArg[j]); + sqlite3_fprintf(out, "%s'", &zArg[j]); } break; } case SQLITE_NULL: { - fprintf(out, "NULL"); + sqlite3_fprintf(out, "NULL"); break; } } @@ -442,70 +412,71 @@ static void dump_table(const char *zTab, FILE *out){ int i; /* Loop counter */ sqlite3_stmt *pStmt; /* SQL statement */ const char *zSep; /* Separator string */ - Str ins; /* Beginning of the INSERT statement */ + sqlite3_str *pIns; /* Beginning of the INSERT statement */ - pStmt = db_prepare("SELECT sql FROM aux.sqlite_master WHERE name=%Q", zTab); + pStmt = db_prepare("SELECT sql FROM aux.sqlite_schema WHERE name=%Q", zTab); if( SQLITE_ROW==sqlite3_step(pStmt) ){ - fprintf(out, "%s;\n", sqlite3_column_text(pStmt,0)); + sqlite3_fprintf(out, "%s;\n", sqlite3_column_text(pStmt,0)); } sqlite3_finalize(pStmt); if( !g.bSchemaOnly ){ az = columnNames("aux", zTab, &nPk, 0); - strInit(&ins); + pIns = sqlite3_str_new(0); if( az==0 ){ pStmt = db_prepare("SELECT * FROM aux.%s", zId); - strPrintf(&ins,"INSERT INTO %s VALUES", zId); + sqlite3_str_appendf(pIns,"INSERT INTO %s VALUES", zId); }else{ - Str sql; - strInit(&sql); + sqlite3_str *pSql = sqlite3_str_new(0); zSep = "SELECT"; for(i=0; az[i]; i++){ - strPrintf(&sql, "%s %s", zSep, az[i]); + sqlite3_str_appendf(pSql, "%s %s", zSep, az[i]); zSep = ","; } - strPrintf(&sql," FROM aux.%s", zId); + sqlite3_str_appendf(pSql," FROM aux.%s", zId); zSep = " ORDER BY"; for(i=1; i<=nPk; i++){ - strPrintf(&sql, "%s %d", zSep, i); + sqlite3_str_appendf(pSql, "%s %d", zSep, i); zSep = ","; } - pStmt = db_prepare("%s", sql.z); - strFree(&sql); - strPrintf(&ins, "INSERT INTO %s", zId); + pStmt = db_prepare("%s", sqlite3_str_value(pSql)); + strFree(pSql); + sqlite3_str_appendf(pIns, "INSERT INTO %s", zId); zSep = "("; for(i=0; az[i]; i++){ - strPrintf(&ins, "%s%s", zSep, az[i]); + sqlite3_str_appendf(pIns, "%s%s", zSep, az[i]); zSep = ","; } - strPrintf(&ins,") VALUES"); + sqlite3_str_appendf(pIns,") VALUES"); namelistFree(az); } nCol = sqlite3_column_count(pStmt); while( SQLITE_ROW==sqlite3_step(pStmt) ){ - fprintf(out, "%s",ins.z); + sqlite3_fprintf(out, "%s",sqlite3_str_value(pIns)); zSep = "("; for(i=0; i<nCol; i++){ - fprintf(out, "%s",zSep); + sqlite3_fprintf(out, "%s",zSep); printQuoted(out, sqlite3_column_value(pStmt,i)); zSep = ","; } - fprintf(out, ");\n"); + sqlite3_fprintf(out, ");\n"); } sqlite3_finalize(pStmt); - strFree(&ins); + strFree(pIns); } /* endif !g.bSchemaOnly */ - pStmt = db_prepare("SELECT sql FROM aux.sqlite_master" + pStmt = db_prepare("SELECT sql FROM aux.sqlite_schema" " WHERE type='index' AND tbl_name=%Q AND sql IS NOT NULL", zTab); while( SQLITE_ROW==sqlite3_step(pStmt) ){ - fprintf(out, "%s;\n", sqlite3_column_text(pStmt,0)); + sqlite3_fprintf(out, "%s;\n", sqlite3_column_text(pStmt,0)); } sqlite3_finalize(pStmt); + sqlite3_free(zId); } /* -** Compute all differences for a single table. +** Compute all differences for a single table, except if the +** table name is sqlite_schema, ignore the rootpage column. */ static void diff_one_table(const char *zTab, FILE *out){ char *zId = safeId(zTab); /* Name of table (translated for us in SQL) */ @@ -518,10 +489,12 @@ static void diff_one_table(const char *zTab, FILE *out){ int nQ; /* Number of output columns in the diff query */ int i; /* Loop counter */ const char *zSep; /* Separator string */ - Str sql; /* Comparison query */ + sqlite3_str *pSql; /* Comparison query */ sqlite3_stmt *pStmt; /* Query statement to do the diff */ + const char *zLead = /* Becomes line-comment for sqlite_schema */ + (g.bSchemaCompare)? "-- " : ""; - strInit(&sql); + pSql = sqlite3_str_new(0); if( g.fDebug==DEBUG_COLUMN_NAMES ){ /* Simply run columnNames() on all tables of the origin ** database and show the results. This is used for testing @@ -529,30 +502,36 @@ static void diff_one_table(const char *zTab, FILE *out){ */ az = columnNames("aux",zTab, &nPk, 0); if( az==0 ){ - printf("Rowid not accessible for %s\n", zId); + sqlite3_fprintf(stdout, "Rowid not accessible for %s\n", zId); }else{ - printf("%s:", zId); + sqlite3_fprintf(stdout, "%s:", zId); for(i=0; az[i]; i++){ - printf(" %s", az[i]); - if( i+1==nPk ) printf(" *"); + sqlite3_fprintf(stdout, " %s", az[i]); + if( i+1==nPk ) sqlite3_fprintf(stdout, " *"); } - printf("\n"); + sqlite3_fprintf(stdout, "\n"); } goto end_diff_one_table; } - if( sqlite3_table_column_metadata(g.db,"aux",zTab,0,0,0,0,0,0) ){ if( !sqlite3_table_column_metadata(g.db,"main",zTab,0,0,0,0,0,0) ){ /* Table missing from second database. */ - fprintf(out, "DROP TABLE %s;\n", zId); + if( g.bSchemaCompare ) + sqlite3_fprintf(out, "-- 2nd DB has no %s table\n", zTab); + else + sqlite3_fprintf(out, "DROP TABLE %s;\n", zId); } goto end_diff_one_table; } if( sqlite3_table_column_metadata(g.db,"main",zTab,0,0,0,0,0,0) ){ /* Table missing from source */ - dump_table(zTab, out); + if( g.bSchemaCompare ){ + sqlite3_fprintf(out, "-- 1st DB has no %s table\n", zTab); + }else{ + dump_table(zTab, out); + } goto end_diff_one_table; } @@ -569,158 +548,160 @@ static void diff_one_table(const char *zTab, FILE *out){ || az[n] ){ /* Schema mismatch */ - fprintf(out, "DROP TABLE %s; -- due to schema mismatch\n", zId); + sqlite3_fprintf(out, "%sDROP TABLE %s; -- due to schema mismatch\n", zLead, zId); dump_table(zTab, out); goto end_diff_one_table; } /* Build the comparison query */ for(n2=n; az2[n2]; n2++){ - fprintf(out, "ALTER TABLE %s ADD COLUMN %s;\n", zId, safeId(az2[n2])); + char *zNTab = safeId(az2[n2]); + sqlite3_fprintf(out, "ALTER TABLE %s ADD COLUMN %s;\n", zId, zNTab); + sqlite3_free(zNTab); } nQ = nPk2+1+2*(n2-nPk2); if( n2>nPk2 ){ zSep = "SELECT "; for(i=0; i<nPk; i++){ - strPrintf(&sql, "%sB.%s", zSep, az[i]); + sqlite3_str_appendf(pSql, "%sB.%s", zSep, az[i]); zSep = ", "; } - strPrintf(&sql, ", 1%s -- changed row\n", nPk==n ? "" : ","); + sqlite3_str_appendf(pSql, ", 1 /* changed row */"); while( az[i] ){ - strPrintf(&sql, " A.%s IS NOT B.%s, B.%s%s\n", - az[i], az2[i], az2[i], az2[i+1]==0 ? "" : ","); + sqlite3_str_appendf(pSql, ", A.%s IS NOT B.%s, B.%s", + az[i], az2[i], az2[i]); i++; } while( az2[i] ){ - strPrintf(&sql, " B.%s IS NOT NULL, B.%s%s\n", - az2[i], az2[i], az2[i+1]==0 ? "" : ","); + sqlite3_str_appendf(pSql, ", B.%s IS NOT NULL, B.%s", + az2[i], az2[i]); i++; } - strPrintf(&sql, " FROM main.%s A, aux.%s B\n", zId, zId); + sqlite3_str_appendf(pSql, "\n FROM main.%s A, aux.%s B\n", zId, zId); zSep = " WHERE"; for(i=0; i<nPk; i++){ - strPrintf(&sql, "%s A.%s=B.%s", zSep, az[i], az[i]); + sqlite3_str_appendf(pSql, "%s A.%s=B.%s", zSep, az[i], az[i]); zSep = " AND"; } zSep = "\n AND ("; while( az[i] ){ - strPrintf(&sql, "%sA.%s IS NOT B.%s%s\n", + sqlite3_str_appendf(pSql, "%sA.%s IS NOT B.%s%s\n", zSep, az[i], az2[i], az2[i+1]==0 ? ")" : ""); zSep = " OR "; i++; } while( az2[i] ){ - strPrintf(&sql, "%sB.%s IS NOT NULL%s\n", + sqlite3_str_appendf(pSql, "%sB.%s IS NOT NULL%s\n", zSep, az2[i], az2[i+1]==0 ? ")" : ""); zSep = " OR "; i++; } - strPrintf(&sql, " UNION ALL\n"); + sqlite3_str_appendf(pSql, " UNION ALL\n"); } zSep = "SELECT "; for(i=0; i<nPk; i++){ - strPrintf(&sql, "%sA.%s", zSep, az[i]); + sqlite3_str_appendf(pSql, "%sA.%s", zSep, az[i]); zSep = ", "; } - strPrintf(&sql, ", 2%s -- deleted row\n", nPk==n ? "" : ","); + sqlite3_str_appendf(pSql, ", 2 /* deleted row */"); while( az2[i] ){ - strPrintf(&sql, " NULL, NULL%s\n", i==n2-1 ? "" : ","); + sqlite3_str_appendf(pSql, ", NULL, NULL"); i++; } - strPrintf(&sql, " FROM main.%s A\n", zId); - strPrintf(&sql, " WHERE NOT EXISTS(SELECT 1 FROM aux.%s B\n", zId); + sqlite3_str_appendf(pSql, "\n FROM main.%s A\n", zId); + sqlite3_str_appendf(pSql, " WHERE NOT EXISTS(SELECT 1 FROM aux.%s B\n", zId); zSep = " WHERE"; for(i=0; i<nPk; i++){ - strPrintf(&sql, "%s A.%s=B.%s", zSep, az[i], az[i]); + sqlite3_str_appendf(pSql, "%s A.%s=B.%s", zSep, az[i], az[i]); zSep = " AND"; } - strPrintf(&sql, ")\n"); + sqlite3_str_appendf(pSql, ")\n"); zSep = " UNION ALL\nSELECT "; for(i=0; i<nPk; i++){ - strPrintf(&sql, "%sB.%s", zSep, az[i]); + sqlite3_str_appendf(pSql, "%sB.%s", zSep, az[i]); zSep = ", "; } - strPrintf(&sql, ", 3%s -- inserted row\n", nPk==n ? "" : ","); + sqlite3_str_appendf(pSql, ", 3 /* inserted row */"); while( az2[i] ){ - strPrintf(&sql, " 1, B.%s%s\n", az2[i], az2[i+1]==0 ? "" : ","); + sqlite3_str_appendf(pSql, ", 1, B.%s", az2[i]); i++; } - strPrintf(&sql, " FROM aux.%s B\n", zId); - strPrintf(&sql, " WHERE NOT EXISTS(SELECT 1 FROM main.%s A\n", zId); + sqlite3_str_appendf(pSql, "\n FROM aux.%s B\n", zId); + sqlite3_str_appendf(pSql, " WHERE NOT EXISTS(SELECT 1 FROM main.%s A\n", zId); zSep = " WHERE"; for(i=0; i<nPk; i++){ - strPrintf(&sql, "%s A.%s=B.%s", zSep, az[i], az[i]); + sqlite3_str_appendf(pSql, "%s A.%s=B.%s", zSep, az[i], az[i]); zSep = " AND"; } - strPrintf(&sql, ")\n ORDER BY"); + sqlite3_str_appendf(pSql, ")\n ORDER BY"); zSep = " "; for(i=1; i<=nPk; i++){ - strPrintf(&sql, "%s%d", zSep, i); + sqlite3_str_appendf(pSql, "%s%d", zSep, i); zSep = ", "; } - strPrintf(&sql, ";\n"); + sqlite3_str_appendf(pSql, ";\n"); if( g.fDebug & DEBUG_DIFF_SQL ){ - printf("SQL for %s:\n%s\n", zId, sql.z); + printf("SQL for %s:\n%s\n", zId, sqlite3_str_value(pSql)); goto end_diff_one_table; } /* Drop indexes that are missing in the destination */ pStmt = db_prepare( - "SELECT name FROM main.sqlite_master" + "SELECT name FROM main.sqlite_schema" " WHERE type='index' AND tbl_name=%Q" " AND sql IS NOT NULL" - " AND sql NOT IN (SELECT sql FROM aux.sqlite_master" + " AND sql NOT IN (SELECT sql FROM aux.sqlite_schema" " WHERE type='index' AND tbl_name=%Q" " AND sql IS NOT NULL)", zTab, zTab); while( SQLITE_ROW==sqlite3_step(pStmt) ){ char *z = safeId((const char*)sqlite3_column_text(pStmt,0)); - fprintf(out, "DROP INDEX %s;\n", z); + sqlite3_fprintf(out, "DROP INDEX %s;\n", z); sqlite3_free(z); } sqlite3_finalize(pStmt); /* Run the query and output differences */ if( !g.bSchemaOnly ){ - pStmt = db_prepare(sql.z); + pStmt = db_prepare("%s", sqlite3_str_value(pSql)); while( SQLITE_ROW==sqlite3_step(pStmt) ){ int iType = sqlite3_column_int(pStmt, nPk); if( iType==1 || iType==2 ){ if( iType==1 ){ /* Change the content of a row */ - fprintf(out, "UPDATE %s", zId); + sqlite3_fprintf(out, "%sUPDATE %s", zLead, zId); zSep = " SET"; for(i=nPk+1; i<nQ; i+=2){ if( sqlite3_column_int(pStmt,i)==0 ) continue; - fprintf(out, "%s %s=", zSep, az2[(i+nPk-1)/2]); + sqlite3_fprintf(out, "%s %s=", zSep, az2[(i+nPk-1)/2]); zSep = ","; printQuoted(out, sqlite3_column_value(pStmt,i+1)); } }else{ /* Delete a row */ - fprintf(out, "DELETE FROM %s", zId); + sqlite3_fprintf(out, "%sDELETE FROM %s", zLead, zId); } zSep = " WHERE"; for(i=0; i<nPk; i++){ - fprintf(out, "%s %s=", zSep, az2[i]); + sqlite3_fprintf(out, "%s %s=", zSep, az2[i]); printQuoted(out, sqlite3_column_value(pStmt,i)); zSep = " AND"; } - fprintf(out, ";\n"); + sqlite3_fprintf(out, ";\n"); }else{ /* Insert a row */ - fprintf(out, "INSERT INTO %s(%s", zId, az2[0]); - for(i=1; az2[i]; i++) fprintf(out, ",%s", az2[i]); - fprintf(out, ") VALUES"); + sqlite3_fprintf(out, "%sINSERT INTO %s(%s", zLead, zId, az2[0]); + for(i=1; az2[i]; i++) sqlite3_fprintf(out, ",%s", az2[i]); + sqlite3_fprintf(out, ") VALUES"); zSep = "("; for(i=0; i<nPk2; i++){ - fprintf(out, "%s", zSep); + sqlite3_fprintf(out, "%s", zSep); zSep = ","; printQuoted(out, sqlite3_column_value(pStmt,i)); } for(i=nPk2+2; i<nQ; i+=2){ - fprintf(out, ","); + sqlite3_fprintf(out, ","); printQuoted(out, sqlite3_column_value(pStmt,i)); } - fprintf(out, ");\n"); + sqlite3_fprintf(out, ");\n"); } } sqlite3_finalize(pStmt); @@ -728,20 +709,20 @@ static void diff_one_table(const char *zTab, FILE *out){ /* Create indexes that are missing in the source */ pStmt = db_prepare( - "SELECT sql FROM aux.sqlite_master" + "SELECT sql FROM aux.sqlite_schema" " WHERE type='index' AND tbl_name=%Q" " AND sql IS NOT NULL" - " AND sql NOT IN (SELECT sql FROM main.sqlite_master" + " AND sql NOT IN (SELECT sql FROM main.sqlite_schema" " WHERE type='index' AND tbl_name=%Q" " AND sql IS NOT NULL)", zTab, zTab); while( SQLITE_ROW==sqlite3_step(pStmt) ){ - fprintf(out, "%s;\n", sqlite3_column_text(pStmt,0)); + sqlite3_fprintf(out, "%s;\n", sqlite3_column_text(pStmt,0)); } sqlite3_finalize(pStmt); end_diff_one_table: - strFree(&sql); + strFree(pSql); sqlite3_free(zId); namelistFree(az); namelistFree(az2); @@ -756,7 +737,7 @@ static void diff_one_table(const char *zTab, FILE *out){ */ static void checkSchemasMatch(const char *zTab){ sqlite3_stmt *pStmt = db_prepare( - "SELECT A.sql=B.sql FROM main.sqlite_master A, aux.sqlite_master B" + "SELECT A.sql=B.sql FROM main.sqlite_schema A, aux.sqlite_schema B" " WHERE A.name=%Q AND B.name=%Q", zTab, zTab ); if( SQLITE_ROW==sqlite3_step(pStmt) ){ @@ -1149,15 +1130,15 @@ static int rbuDeltaCreate( **************************************************************************/ static void strPrintfArray( - Str *pStr, /* String object to append to */ + sqlite3_str *pStr, /* String object to append to */ const char *zSep, /* Separator string */ const char *zFmt, /* Format for each entry */ char **az, int n /* Array of strings & its size (or -1) */ ){ int i; for(i=0; az[i] && (i<n || n<0); i++){ - if( i!=0 ) strPrintf(pStr, "%s", zSep); - strPrintf(pStr, zFmt, az[i], az[i], az[i]); + if( i!=0 ) sqlite3_str_appendf(pStr, "%s", zSep); + sqlite3_str_appendf(pStr, zFmt, az[i], az[i], az[i]); } } @@ -1166,72 +1147,75 @@ static void getRbudiffQuery( char **azCol, int nPK, int bOtaRowid, - Str *pSql + sqlite3_str *pSql ){ int i; /* First the newly inserted rows: **/ - strPrintf(pSql, "SELECT "); + sqlite3_str_appendf(pSql, "SELECT "); strPrintfArray(pSql, ", ", "%s", azCol, -1); - strPrintf(pSql, ", 0, "); /* Set ota_control to 0 for an insert */ + sqlite3_str_appendf(pSql, ", 0, "); /* Set ota_control to 0 for an insert */ strPrintfArray(pSql, ", ", "NULL", azCol, -1); - strPrintf(pSql, " FROM aux.%Q AS n WHERE NOT EXISTS (\n", zTab); - strPrintf(pSql, " SELECT 1 FROM ", zTab); - strPrintf(pSql, " main.%Q AS o WHERE ", zTab); - strPrintfArray(pSql, " AND ", "(n.%Q IS o.%Q)", azCol, nPK); - strPrintf(pSql, "\n)"); + sqlite3_str_appendf(pSql, " FROM aux.%Q AS n WHERE NOT EXISTS (\n", zTab); + sqlite3_str_appendf(pSql, " SELECT 1 FROM ", zTab); + sqlite3_str_appendf(pSql, " main.%Q AS o WHERE ", zTab); + strPrintfArray(pSql, " AND ", "(n.%Q = o.%Q)", azCol, nPK); + sqlite3_str_appendf(pSql, "\n) AND "); + strPrintfArray(pSql, " AND ", "(n.%Q IS NOT NULL)", azCol, nPK); /* Deleted rows: */ - strPrintf(pSql, "\nUNION ALL\nSELECT "); + sqlite3_str_appendf(pSql, "\nUNION ALL\nSELECT "); strPrintfArray(pSql, ", ", "%s", azCol, nPK); if( azCol[nPK] ){ - strPrintf(pSql, ", "); + sqlite3_str_appendf(pSql, ", "); strPrintfArray(pSql, ", ", "NULL", &azCol[nPK], -1); } - strPrintf(pSql, ", 1, "); /* Set ota_control to 1 for a delete */ + sqlite3_str_appendf(pSql, ", 1, "); /* Set ota_control to 1 for a delete */ strPrintfArray(pSql, ", ", "NULL", azCol, -1); - strPrintf(pSql, " FROM main.%Q AS n WHERE NOT EXISTS (\n", zTab); - strPrintf(pSql, " SELECT 1 FROM ", zTab); - strPrintf(pSql, " aux.%Q AS o WHERE ", zTab); - strPrintfArray(pSql, " AND ", "(n.%Q IS o.%Q)", azCol, nPK); - strPrintf(pSql, "\n) "); + sqlite3_str_appendf(pSql, " FROM main.%Q AS n WHERE NOT EXISTS (\n", zTab); + sqlite3_str_appendf(pSql, " SELECT 1 FROM ", zTab); + sqlite3_str_appendf(pSql, " aux.%Q AS o WHERE ", zTab); + strPrintfArray(pSql, " AND ", "(n.%Q = o.%Q)", azCol, nPK); + sqlite3_str_appendf(pSql, "\n) AND "); + strPrintfArray(pSql, " AND ", "(n.%Q IS NOT NULL)", azCol, nPK); /* Updated rows. If all table columns are part of the primary key, there ** can be no updates. In this case this part of the compound SELECT can ** be omitted altogether. */ if( azCol[nPK] ){ - strPrintf(pSql, "\nUNION ALL\nSELECT "); + sqlite3_str_appendf(pSql, "\nUNION ALL\nSELECT "); strPrintfArray(pSql, ", ", "n.%s", azCol, nPK); - strPrintf(pSql, ",\n"); + sqlite3_str_appendf(pSql, ",\n"); strPrintfArray(pSql, " ,\n", " CASE WHEN n.%s IS o.%s THEN NULL ELSE n.%s END", &azCol[nPK], -1 ); if( bOtaRowid==0 ){ - strPrintf(pSql, ", '"); + sqlite3_str_appendf(pSql, ", '"); strPrintfArray(pSql, "", ".", azCol, nPK); - strPrintf(pSql, "' ||\n"); + sqlite3_str_appendf(pSql, "' ||\n"); }else{ - strPrintf(pSql, ",\n"); + sqlite3_str_appendf(pSql, ",\n"); } strPrintfArray(pSql, " ||\n", " CASE WHEN n.%s IS o.%s THEN '.' ELSE 'x' END", &azCol[nPK], -1 ); - strPrintf(pSql, "\nAS ota_control, "); + sqlite3_str_appendf(pSql, "\nAS ota_control, "); strPrintfArray(pSql, ", ", "NULL", azCol, nPK); - strPrintf(pSql, ",\n"); + sqlite3_str_appendf(pSql, ",\n"); strPrintfArray(pSql, " ,\n", " CASE WHEN n.%s IS o.%s THEN NULL ELSE o.%s END", &azCol[nPK], -1 ); - strPrintf(pSql, "\nFROM main.%Q AS o, aux.%Q AS n\nWHERE ", zTab, zTab); - strPrintfArray(pSql, " AND ", "(n.%Q IS o.%Q)", azCol, nPK); - strPrintf(pSql, " AND ota_control LIKE '%%x%%'"); + sqlite3_str_appendf(pSql, "\nFROM main.%Q AS o, aux.%Q AS n\nWHERE ", + zTab, zTab); + strPrintfArray(pSql, " AND ", "(n.%Q = o.%Q)", azCol, nPK); + sqlite3_str_appendf(pSql, " AND ota_control LIKE '%%x%%'"); } /* Now add an ORDER BY clause to sort everything by PK. */ - strPrintf(pSql, "\nORDER BY "); - for(i=1; i<=nPK; i++) strPrintf(pSql, "%s%d", ((i>1)?", ":""), i); + sqlite3_str_appendf(pSql, "\nORDER BY "); + for(i=1; i<=nPK; i++) sqlite3_str_appendf(pSql, "%s%d", ((i>1)?", ":""), i); } static void rbudiff_one_table(const char *zTab, FILE *out){ @@ -1240,13 +1224,17 @@ static void rbudiff_one_table(const char *zTab, FILE *out){ char **azCol; /* NULL terminated array of col names */ int i; int nCol; - Str ct = {0, 0, 0}; /* The "CREATE TABLE data_xxx" statement */ - Str sql = {0, 0, 0}; /* Query to find differences */ - Str insert = {0, 0, 0}; /* First part of output INSERT statement */ + sqlite3_str *pCt; /* The "CREATE TABLE data_xxx" statement */ + sqlite3_str *pSql; /* Query to find differences */ + sqlite3_str *pInsert; /* First part of output INSERT statement */ sqlite3_stmt *pStmt = 0; + int nRow = 0; /* Total rows in data_xxx table */ /* --rbu mode must use real primary keys. */ g.bSchemaPK = 1; + pCt = sqlite3_str_new(0); + pSql = sqlite3_str_new(0); + pInsert = sqlite3_str_new(0); /* Check that the schemas of the two tables match. Exit early otherwise. */ checkSchemasMatch(zTab); @@ -1260,46 +1248,47 @@ static void rbudiff_one_table(const char *zTab, FILE *out){ for(nCol=0; azCol[nCol]; nCol++); /* Build and output the CREATE TABLE statement for the data_xxx table */ - strPrintf(&ct, "CREATE TABLE IF NOT EXISTS 'data_%q'(", zTab); - if( bOtaRowid ) strPrintf(&ct, "rbu_rowid, "); - strPrintfArray(&ct, ", ", "%s", &azCol[bOtaRowid], -1); - strPrintf(&ct, ", rbu_control);"); + sqlite3_str_appendf(pCt, "CREATE TABLE IF NOT EXISTS 'data_%q'(", zTab); + if( bOtaRowid ) sqlite3_str_appendf(pCt, "rbu_rowid, "); + strPrintfArray(pCt, ", ", "%s", &azCol[bOtaRowid], -1); + sqlite3_str_appendf(pCt, ", rbu_control);"); /* Get the SQL for the query to retrieve data from the two databases */ - getRbudiffQuery(zTab, azCol, nPK, bOtaRowid, &sql); + getRbudiffQuery(zTab, azCol, nPK, bOtaRowid, pSql); /* Build the first part of the INSERT statement output for each row ** in the data_xxx table. */ - strPrintf(&insert, "INSERT INTO 'data_%q' (", zTab); - if( bOtaRowid ) strPrintf(&insert, "rbu_rowid, "); - strPrintfArray(&insert, ", ", "%s", &azCol[bOtaRowid], -1); - strPrintf(&insert, ", rbu_control) VALUES("); + sqlite3_str_appendf(pInsert, "INSERT INTO 'data_%q' (", zTab); + if( bOtaRowid ) sqlite3_str_appendf(pInsert, "rbu_rowid, "); + strPrintfArray(pInsert, ", ", "%s", &azCol[bOtaRowid], -1); + sqlite3_str_appendf(pInsert, ", rbu_control) VALUES("); - pStmt = db_prepare("%s", sql.z); + pStmt = db_prepare("%s", sqlite3_str_value(pSql)); while( sqlite3_step(pStmt)==SQLITE_ROW ){ /* If this is the first row output, print out the CREATE TABLE - ** statement first. And then set ct.z to NULL so that it is not + ** statement first. And reset pCt so that it will not be ** printed again. */ - if( ct.z ){ - fprintf(out, "%s\n", ct.z); - strFree(&ct); + if( sqlite3_str_length(pCt) ){ + sqlite3_fprintf(out, "%s\n", sqlite3_str_value(pCt)); + sqlite3_str_reset(pCt); } /* Output the first part of the INSERT statement */ - fprintf(out, "%s", insert.z); + sqlite3_fprintf(out, "%s", sqlite3_str_value(pInsert)); + nRow++; if( sqlite3_column_type(pStmt, nCol)==SQLITE_INTEGER ){ for(i=0; i<=nCol; i++){ - if( i>0 ) fprintf(out, ", "); + if( i>0 ) sqlite3_fprintf(out, ", "); printQuoted(out, sqlite3_column_value(pStmt, i)); } }else{ char *zOtaControl; int nOtaControl = sqlite3_column_bytes(pStmt, nCol); - zOtaControl = (char*)sqlite3_malloc(nOtaControl); + zOtaControl = (char*)sqlite3_malloc(nOtaControl+1); memcpy(zOtaControl, sqlite3_column_text(pStmt, nCol), nOtaControl+1); for(i=0; i<nCol; i++){ @@ -1319,9 +1308,9 @@ static void rbudiff_one_table(const char *zTab, FILE *out){ nDelta = rbuDeltaCreate(aSrc, nSrc, aFinal, nFinal, aDelta); if( nDelta<nFinal ){ int j; - fprintf(out, "x'"); - for(j=0; j<nDelta; j++) fprintf(out, "%02x", (u8)aDelta[j]); - fprintf(out, "'"); + sqlite3_fprintf(out, "x'"); + for(j=0; j<nDelta; j++) sqlite3_fprintf(out, "%02x", (u8)aDelta[j]); + sqlite3_fprintf(out, "'"); zOtaControl[i-bOtaRowid] = 'f'; bDone = 1; } @@ -1331,21 +1320,28 @@ static void rbudiff_one_table(const char *zTab, FILE *out){ if( bDone==0 ){ printQuoted(out, sqlite3_column_value(pStmt, i)); } - fprintf(out, ", "); + sqlite3_fprintf(out, ", "); } - fprintf(out, "'%s'", zOtaControl); + sqlite3_fprintf(out, "'%s'", zOtaControl); sqlite3_free(zOtaControl); } /* And the closing bracket of the insert statement */ - fprintf(out, ");\n"); + sqlite3_fprintf(out, ");\n"); } sqlite3_finalize(pStmt); + if( nRow>0 ){ + sqlite3_str *pCnt = sqlite3_str_new(0); + sqlite3_str_appendf(pCnt, + "INSERT INTO rbu_count VALUES('data_%q', %d);", zTab, nRow); + sqlite3_fprintf(out, "%s\n", sqlite3_str_value(pCnt)); + strFree(pCnt); + } - strFree(&ct); - strFree(&sql); - strFree(&insert); + strFree(pCt); + strFree(pSql); + strFree(pInsert); } /* @@ -1367,25 +1363,25 @@ static void summarize_one_table(const char *zTab, FILE *out){ int n2; /* Number of columns in aux */ int i; /* Loop counter */ const char *zSep; /* Separator string */ - Str sql; /* Comparison query */ + sqlite3_str *pSql; /* Comparison query */ sqlite3_stmt *pStmt; /* Query statement to do the diff */ sqlite3_int64 nUpdate; /* Number of updated rows */ sqlite3_int64 nUnchanged; /* Number of unmodified rows */ sqlite3_int64 nDelete; /* Number of deleted rows */ sqlite3_int64 nInsert; /* Number of inserted rows */ - strInit(&sql); + pSql = sqlite3_str_new(0); if( sqlite3_table_column_metadata(g.db,"aux",zTab,0,0,0,0,0,0) ){ if( !sqlite3_table_column_metadata(g.db,"main",zTab,0,0,0,0,0,0) ){ /* Table missing from second database. */ - fprintf(out, "%s: missing from second database\n", zTab); + sqlite3_fprintf(out, "%s: missing from second database\n", zTab); } goto end_summarize_one_table; } if( sqlite3_table_column_metadata(g.db,"main",zTab,0,0,0,0,0,0) ){ /* Table missing from source */ - fprintf(out, "%s: missing from first database\n", zTab); + sqlite3_fprintf(out, "%s: missing from first database\n", zTab); goto end_summarize_one_table; } @@ -1402,57 +1398,57 @@ static void summarize_one_table(const char *zTab, FILE *out){ || az[n] ){ /* Schema mismatch */ - fprintf(out, "%s: incompatible schema\n", zTab); + sqlite3_fprintf(out, "%s: incompatible schema\n", zTab); goto end_summarize_one_table; } /* Build the comparison query */ for(n2=n; az[n2]; n2++){} - strPrintf(&sql, "SELECT 1, count(*)"); + sqlite3_str_appendf(pSql, "SELECT 1, count(*)"); if( n2==nPk2 ){ - strPrintf(&sql, ", 0\n"); + sqlite3_str_appendf(pSql, ", 0\n"); }else{ zSep = ", sum("; for(i=nPk; az[i]; i++){ - strPrintf(&sql, "%sA.%s IS NOT B.%s", zSep, az[i], az[i]); + sqlite3_str_appendf(pSql, "%sA.%s IS NOT B.%s", zSep, az[i], az[i]); zSep = " OR "; } - strPrintf(&sql, ")\n"); + sqlite3_str_appendf(pSql, ")\n"); } - strPrintf(&sql, " FROM main.%s A, aux.%s B\n", zId, zId); + sqlite3_str_appendf(pSql, " FROM main.%s A, aux.%s B\n", zId, zId); zSep = " WHERE"; for(i=0; i<nPk; i++){ - strPrintf(&sql, "%s A.%s=B.%s", zSep, az[i], az[i]); + sqlite3_str_appendf(pSql, "%s A.%s=B.%s", zSep, az[i], az[i]); zSep = " AND"; } - strPrintf(&sql, " UNION ALL\n"); - strPrintf(&sql, "SELECT 2, count(*), 0\n"); - strPrintf(&sql, " FROM main.%s A\n", zId); - strPrintf(&sql, " WHERE NOT EXISTS(SELECT 1 FROM aux.%s B ", zId); + sqlite3_str_appendf(pSql, " UNION ALL\n"); + sqlite3_str_appendf(pSql, "SELECT 2, count(*), 0\n"); + sqlite3_str_appendf(pSql, " FROM main.%s A\n", zId); + sqlite3_str_appendf(pSql, " WHERE NOT EXISTS(SELECT 1 FROM aux.%s B ", zId); zSep = "WHERE"; for(i=0; i<nPk; i++){ - strPrintf(&sql, "%s A.%s=B.%s", zSep, az[i], az[i]); + sqlite3_str_appendf(pSql, "%s A.%s=B.%s", zSep, az[i], az[i]); zSep = " AND"; } - strPrintf(&sql, ")\n"); - strPrintf(&sql, " UNION ALL\n"); - strPrintf(&sql, "SELECT 3, count(*), 0\n"); - strPrintf(&sql, " FROM aux.%s B\n", zId); - strPrintf(&sql, " WHERE NOT EXISTS(SELECT 1 FROM main.%s A ", zId); + sqlite3_str_appendf(pSql, ")\n"); + sqlite3_str_appendf(pSql, " UNION ALL\n"); + sqlite3_str_appendf(pSql, "SELECT 3, count(*), 0\n"); + sqlite3_str_appendf(pSql, " FROM aux.%s B\n", zId); + sqlite3_str_appendf(pSql, " WHERE NOT EXISTS(SELECT 1 FROM main.%s A ", zId); zSep = "WHERE"; for(i=0; i<nPk; i++){ - strPrintf(&sql, "%s A.%s=B.%s", zSep, az[i], az[i]); + sqlite3_str_appendf(pSql, "%s A.%s=B.%s", zSep, az[i], az[i]); zSep = " AND"; } - strPrintf(&sql, ")\n ORDER BY 1;\n"); + sqlite3_str_appendf(pSql, ")\n ORDER BY 1;\n"); if( (g.fDebug & DEBUG_DIFF_SQL)!=0 ){ - printf("SQL for %s:\n%s\n", zId, sql.z); + sqlite3_fprintf(stdout, "SQL for %s:\n%s\n", zId, sqlite3_str_value(pSql)); goto end_summarize_one_table; } /* Run the query and output difference summary */ - pStmt = db_prepare(sql.z); + pStmt = db_prepare("%s", sqlite3_str_value(pSql)); nUpdate = 0; nInsert = 0; nDelete = 0; @@ -1472,11 +1468,12 @@ static void summarize_one_table(const char *zTab, FILE *out){ } } sqlite3_finalize(pStmt); - fprintf(out, "%s: %lld changes, %lld inserts, %lld deletes, %lld unchanged\n", + sqlite3_fprintf(out, + "%s: %lld changes, %lld inserts, %lld deletes, %lld unchanged\n", zTab, nUpdate, nInsert, nDelete, nUnchanged); end_summarize_one_table: - strFree(&sql); + strFree(pSql); sqlite3_free(zId); namelistFree(az); namelistFree(az2); @@ -1511,8 +1508,8 @@ static void putsVarint(FILE *out, sqlite3_uint64 v){ /* ** Write an SQLite value onto out. */ -static void putValue(FILE *out, sqlite3_value *pVal){ - int iDType = sqlite3_value_type(pVal); +static void putValue(FILE *out, sqlite3_stmt *pStmt, int k){ + int iDType = sqlite3_column_type(pStmt, k); sqlite3_int64 iX; double rX; sqlite3_uint64 uX; @@ -1521,24 +1518,24 @@ static void putValue(FILE *out, sqlite3_value *pVal){ putc(iDType, out); switch( iDType ){ case SQLITE_INTEGER: - iX = sqlite3_value_int64(pVal); + iX = sqlite3_column_int64(pStmt, k); memcpy(&uX, &iX, 8); for(j=56; j>=0; j-=8) putc((uX>>j)&0xff, out); break; case SQLITE_FLOAT: - rX = sqlite3_value_double(pVal); + rX = sqlite3_column_double(pStmt, k); memcpy(&uX, &rX, 8); for(j=56; j>=0; j-=8) putc((uX>>j)&0xff, out); break; case SQLITE_TEXT: - iX = sqlite3_value_bytes(pVal); + iX = sqlite3_column_bytes(pStmt, k); putsVarint(out, (sqlite3_uint64)iX); - fwrite(sqlite3_value_text(pVal),1,(size_t)iX,out); + fwrite(sqlite3_column_text(pStmt, k),1,(size_t)iX,out); break; case SQLITE_BLOB: - iX = sqlite3_value_bytes(pVal); + iX = sqlite3_column_bytes(pStmt, k); putsVarint(out, (sqlite3_uint64)iX); - fwrite(sqlite3_value_blob(pVal),1,(size_t)iX,out); + fwrite(sqlite3_column_blob(pStmt, k),1,(size_t)iX,out); break; case SQLITE_NULL: break; @@ -1556,12 +1553,13 @@ static void changeset_one_table(const char *zTab, FILE *out){ int *aiFlg = 0; /* 0 if column is not part of PK */ int *aiPk = 0; /* Column numbers for each PK column */ int nPk = 0; /* Number of PRIMARY KEY columns */ - Str sql; /* SQL for the diff query */ + sqlite3_str *pSql; /* SQL for the diff query */ int i, k; /* Loop counters */ const char *zSep; /* List separator */ /* Check that the schemas of the two tables match. Exit early otherwise. */ checkSchemasMatch(zTab); + pSql = sqlite3_str_new(0); pStmt = db_prepare("PRAGMA main.table_info=%Q", zTab); while( SQLITE_ROW==sqlite3_step(pStmt) ){ @@ -1583,83 +1581,85 @@ static void changeset_one_table(const char *zTab, FILE *out){ } sqlite3_finalize(pStmt); if( nPk==0 ) goto end_changeset_one_table; - strInit(&sql); if( nCol>nPk ){ - strPrintf(&sql, "SELECT %d", SQLITE_UPDATE); + sqlite3_str_appendf(pSql, "SELECT %d", SQLITE_UPDATE); for(i=0; i<nCol; i++){ if( aiFlg[i] ){ - strPrintf(&sql, ",\n A.%s", azCol[i]); + sqlite3_str_appendf(pSql, ",\n A.%s", azCol[i]); }else{ - strPrintf(&sql, ",\n A.%s IS NOT B.%s, A.%s, B.%s", + sqlite3_str_appendf(pSql, ",\n A.%s IS NOT B.%s, A.%s, B.%s", azCol[i], azCol[i], azCol[i], azCol[i]); } } - strPrintf(&sql,"\n FROM main.%s A, aux.%s B\n", zId, zId); + sqlite3_str_appendf(pSql,"\n FROM main.%s A, aux.%s B\n", zId, zId); zSep = " WHERE"; for(i=0; i<nPk; i++){ - strPrintf(&sql, "%s A.%s=B.%s", zSep, azCol[aiPk[i]], azCol[aiPk[i]]); + sqlite3_str_appendf(pSql, "%s A.%s=B.%s", + zSep, azCol[aiPk[i]], azCol[aiPk[i]]); zSep = " AND"; } zSep = "\n AND ("; for(i=0; i<nCol; i++){ if( aiFlg[i] ) continue; - strPrintf(&sql, "%sA.%s IS NOT B.%s", zSep, azCol[i], azCol[i]); + sqlite3_str_appendf(pSql, "%sA.%s IS NOT B.%s", zSep, azCol[i], azCol[i]); zSep = " OR\n "; } - strPrintf(&sql,")\n UNION ALL\n"); + sqlite3_str_appendf(pSql,")\n UNION ALL\n"); } - strPrintf(&sql, "SELECT %d", SQLITE_DELETE); + sqlite3_str_appendf(pSql, "SELECT %d", SQLITE_DELETE); for(i=0; i<nCol; i++){ if( aiFlg[i] ){ - strPrintf(&sql, ",\n A.%s", azCol[i]); + sqlite3_str_appendf(pSql, ",\n A.%s", azCol[i]); }else{ - strPrintf(&sql, ",\n 1, A.%s, NULL", azCol[i]); + sqlite3_str_appendf(pSql, ",\n 1, A.%s, NULL", azCol[i]); } } - strPrintf(&sql, "\n FROM main.%s A\n", zId); - strPrintf(&sql, " WHERE NOT EXISTS(SELECT 1 FROM aux.%s B\n", zId); + sqlite3_str_appendf(pSql, "\n FROM main.%s A\n", zId); + sqlite3_str_appendf(pSql, " WHERE NOT EXISTS(SELECT 1 FROM aux.%s B\n", zId); zSep = " WHERE"; for(i=0; i<nPk; i++){ - strPrintf(&sql, "%s A.%s=B.%s", zSep, azCol[aiPk[i]], azCol[aiPk[i]]); + sqlite3_str_appendf(pSql, "%s A.%s=B.%s", + zSep, azCol[aiPk[i]], azCol[aiPk[i]]); zSep = " AND"; } - strPrintf(&sql, ")\n UNION ALL\n"); - strPrintf(&sql, "SELECT %d", SQLITE_INSERT); + sqlite3_str_appendf(pSql, ")\n UNION ALL\n"); + sqlite3_str_appendf(pSql, "SELECT %d", SQLITE_INSERT); for(i=0; i<nCol; i++){ if( aiFlg[i] ){ - strPrintf(&sql, ",\n B.%s", azCol[i]); + sqlite3_str_appendf(pSql, ",\n B.%s", azCol[i]); }else{ - strPrintf(&sql, ",\n 1, NULL, B.%s", azCol[i]); + sqlite3_str_appendf(pSql, ",\n 1, NULL, B.%s", azCol[i]); } } - strPrintf(&sql, "\n FROM aux.%s B\n", zId); - strPrintf(&sql, " WHERE NOT EXISTS(SELECT 1 FROM main.%s A\n", zId); + sqlite3_str_appendf(pSql, "\n FROM aux.%s B\n", zId); + sqlite3_str_appendf(pSql, " WHERE NOT EXISTS(SELECT 1 FROM main.%s A\n", zId); zSep = " WHERE"; for(i=0; i<nPk; i++){ - strPrintf(&sql, "%s A.%s=B.%s", zSep, azCol[aiPk[i]], azCol[aiPk[i]]); + sqlite3_str_appendf(pSql, "%s A.%s=B.%s", + zSep, azCol[aiPk[i]], azCol[aiPk[i]]); zSep = " AND"; } - strPrintf(&sql, ")\n"); - strPrintf(&sql, " ORDER BY"); + sqlite3_str_appendf(pSql, ")\n"); + sqlite3_str_appendf(pSql, " ORDER BY"); zSep = " "; for(i=0; i<nPk; i++){ - strPrintf(&sql, "%s %d", zSep, aiPk[i]+2); + sqlite3_str_appendf(pSql, "%s %d", zSep, aiPk[i]+2); zSep = ","; } - strPrintf(&sql, ";\n"); + sqlite3_str_appendf(pSql, ";\n"); if( g.fDebug & DEBUG_DIFF_SQL ){ - printf("SQL for %s:\n%s\n", zId, sql.z); + sqlite3_fprintf(stdout, "SQL for %s:\n%s\n", zId, sqlite3_str_value(pSql)); goto end_changeset_one_table; } putc('T', out); putsVarint(out, (sqlite3_uint64)nCol); - for(i=0; i<nCol; i++) putc(aiFlg[i]!=0, out); + for(i=0; i<nCol; i++) putc(aiFlg[i], out); fwrite(zTab, 1, strlen(zTab), out); putc(0, out); - pStmt = db_prepare("%s", sql.z); + pStmt = db_prepare("%s", sqlite3_str_value(pSql)); while( SQLITE_ROW==sqlite3_step(pStmt) ){ int iType = sqlite3_column_int(pStmt,0); putc(iType, out); @@ -1668,10 +1668,10 @@ static void changeset_one_table(const char *zTab, FILE *out){ case SQLITE_UPDATE: { for(k=1, i=0; i<nCol; i++){ if( aiFlg[i] ){ - putValue(out, sqlite3_column_value(pStmt,k)); + putValue(out, pStmt, k); k++; }else if( sqlite3_column_int(pStmt,k) ){ - putValue(out, sqlite3_column_value(pStmt,k+1)); + putValue(out, pStmt, k+1); k += 3; }else{ putc(0, out); @@ -1683,7 +1683,7 @@ static void changeset_one_table(const char *zTab, FILE *out){ putc(0, out); k++; }else if( sqlite3_column_int(pStmt,k) ){ - putValue(out, sqlite3_column_value(pStmt,k+2)); + putValue(out, pStmt, k+2); k += 3; }else{ putc(0, out); @@ -1695,10 +1695,10 @@ static void changeset_one_table(const char *zTab, FILE *out){ case SQLITE_INSERT: { for(k=1, i=0; i<nCol; i++){ if( aiFlg[i] ){ - putValue(out, sqlite3_column_value(pStmt,k)); + putValue(out, pStmt, k); k++; }else{ - putValue(out, sqlite3_column_value(pStmt,k+2)); + putValue(out, pStmt, k+2); k += 3; } } @@ -1707,10 +1707,10 @@ static void changeset_one_table(const char *zTab, FILE *out){ case SQLITE_DELETE: { for(k=1, i=0; i<nCol; i++){ if( aiFlg[i] ){ - putValue(out, sqlite3_column_value(pStmt,k)); + putValue(out, pStmt, k); k++; }else{ - putValue(out, sqlite3_column_value(pStmt,k+1)); + putValue(out, pStmt, k+1); k += 3; } } @@ -1725,14 +1725,162 @@ static void changeset_one_table(const char *zTab, FILE *out){ sqlite3_free(azCol); sqlite3_free(aiPk); sqlite3_free(zId); + sqlite3_free(aiFlg); + strFree(pSql); +} + +/* +** Return true if the ascii character passed as the only argument is a +** whitespace character. Otherwise return false. +*/ +static int is_whitespace(char x){ + return (x==' ' || x=='\t' || x=='\n' || x=='\r'); +} + +/* +** Extract the next SQL keyword or quoted string from buffer zIn and copy it +** (or a prefix of it if it will not fit) into buffer zBuf, size nBuf bytes. +** Return a pointer to the character within zIn immediately following +** the token or quoted string just extracted. +*/ +static const char *gobble_token(const char *zIn, char *zBuf, int nBuf){ + const char *p = zIn; + char *pOut = zBuf; + char *pEnd = &pOut[nBuf-1]; + char q = 0; /* quote character, if any */ + + if( p==0 ) return 0; + while( is_whitespace(*p) ) p++; + switch( *p ){ + case '"': q = '"'; break; + case '\'': q = '\''; break; + case '`': q = '`'; break; + case '[': q = ']'; break; + } + + if( q ){ + p++; + while( *p && pOut<pEnd ){ + if( *p==q ){ + p++; + if( *p!=q ) break; + } + if( pOut<pEnd ) *pOut++ = *p; + p++; + } + }else{ + while( *p && !is_whitespace(*p) && *p!='(' ){ + if( pOut<pEnd ) *pOut++ = *p; + p++; + } + } + + *pOut = '\0'; + return p; +} + +/* +** This function is the implementation of SQL scalar function "module_name": +** +** module_name(SQL) +** +** The only argument should be an SQL statement of the type that may appear +** in the sqlite_schema table. If the statement is a "CREATE VIRTUAL TABLE" +** statement, then the value returned is the name of the module that it +** uses. Otherwise, if the statement is not a CVT, NULL is returned. +*/ +static void module_name_func( + sqlite3_context *pCtx, + int nVal, sqlite3_value **apVal +){ + const char *zSql; + char zToken[32]; + + assert( nVal==1 ); + zSql = (const char*)sqlite3_value_text(apVal[0]); + + zSql = gobble_token(zSql, zToken, sizeof(zToken)); + if( zSql==0 || sqlite3_stricmp(zToken, "create") ) return; + zSql = gobble_token(zSql, zToken, sizeof(zToken)); + if( zSql==0 || sqlite3_stricmp(zToken, "virtual") ) return; + zSql = gobble_token(zSql, zToken, sizeof(zToken)); + if( zSql==0 || sqlite3_stricmp(zToken, "table") ) return; + zSql = gobble_token(zSql, zToken, sizeof(zToken)); + if( zSql==0 ) return; + zSql = gobble_token(zSql, zToken, sizeof(zToken)); + if( zSql==0 || sqlite3_stricmp(zToken, "using") ) return; + zSql = gobble_token(zSql, zToken, sizeof(zToken)); + + sqlite3_result_text(pCtx, zToken, -1, SQLITE_TRANSIENT); +} + +/* +** Return the text of an SQL statement that itself returns the list of +** tables to process within the database. +*/ +const char *all_tables_sql(){ + if( g.bHandleVtab ){ + int rc; + + rc = sqlite3_exec(g.db, + "CREATE TEMP TABLE tblmap(module COLLATE nocase, postfix);" + "INSERT INTO temp.tblmap VALUES" + "('fts3', '_content'), ('fts3', '_segments'), ('fts3', '_segdir')," + + "('fts4', '_content'), ('fts4', '_segments'), ('fts4', '_segdir')," + "('fts4', '_docsize'), ('fts4', '_stat')," + + "('fts5', '_data'), ('fts5', '_idx'), ('fts5', '_content')," + "('fts5', '_docsize'), ('fts5', '_config')," + + "('rtree', '_node'), ('rtree', '_rowid'), ('rtree', '_parent');" + , 0, 0, 0 + ); + assert( rc==SQLITE_OK ); + + rc = sqlite3_create_function( + g.db, "module_name", 1, SQLITE_UTF8, 0, module_name_func, 0, 0 + ); + assert( rc==SQLITE_OK ); + + return + "SELECT name FROM main.sqlite_schema\n" + " WHERE type='table' AND (\n" + " module_name(sql) IS NULL OR \n" + " module_name(sql) IN (SELECT module FROM temp.tblmap)\n" + " ) AND name NOT IN (\n" + " SELECT a.name || b.postfix \n" + "FROM main.sqlite_schema AS a, temp.tblmap AS b \n" + "WHERE module_name(a.sql) = b.module\n" + " )\n" + "UNION \n" + "SELECT name FROM aux.sqlite_schema\n" + " WHERE type='table' AND (\n" + " module_name(sql) IS NULL OR \n" + " module_name(sql) IN (SELECT module FROM temp.tblmap)\n" + " ) AND name NOT IN (\n" + " SELECT a.name || b.postfix \n" + "FROM aux.sqlite_schema AS a, temp.tblmap AS b \n" + "WHERE module_name(a.sql) = b.module\n" + " )\n" + " ORDER BY name"; + }else{ + return + "SELECT name FROM main.sqlite_schema\n" + " WHERE type='table' AND sql NOT LIKE 'CREATE VIRTUAL%%'\n" + " UNION\n" + "SELECT name FROM aux.sqlite_schema\n" + " WHERE type='table' AND sql NOT LIKE 'CREATE VIRTUAL%%'\n" + " ORDER BY name"; + } } /* ** Print sketchy documentation for this utility program */ static void showHelp(void){ - printf("Usage: %s [options] DB1 DB2\n", g.zArgv0); - printf( + sqlite3_fprintf(stdout, "Usage: %s [options] DB1 DB2\n", g.zArgv0); + sqlite3_fprintf(stdout, "Output SQL text that would transform DB1 into DB2.\n" "Options:\n" " --changeset FILE Write a CHANGESET into FILE\n" @@ -1743,6 +1891,8 @@ static void showHelp(void){ " --summary Show only a summary of the differences\n" " --table TAB Show only differences in table TAB\n" " --transaction Show SQL output inside a transaction\n" +" --vtab Handle fts3, fts4, fts5 and rtree tables\n" +"See https://sqlite.org/sqldiff.html for detailed explanation.\n" ); } @@ -1757,8 +1907,10 @@ int main(int argc, char **argv){ char *zTab = 0; FILE *out = stdout; void (*xDiff)(const char*,FILE*) = diff_one_table; +#ifndef SQLITE_OMIT_LOAD_EXTENSION int nExt = 0; char **azExt = 0; +#endif int useTransaction = 0; int neverUseTransaction = 0; @@ -1771,7 +1923,7 @@ int main(int argc, char **argv){ if( z[0]=='-' ) z++; if( strcmp(z,"changeset")==0 ){ if( i==argc-1 ) cmdlineError("missing argument to %s", argv[i]); - out = fopen(argv[++i], "wb"); + out = sqlite3_fopen(argv[++i], "wb"); if( out==0 ) cmdlineError("cannot open: %s", argv[i]); xDiff = changeset_one_table; neverUseTransaction = 1; @@ -1807,10 +1959,16 @@ int main(int argc, char **argv){ if( strcmp(z,"table")==0 ){ if( i==argc-1 ) cmdlineError("missing argument to %s", argv[i]); zTab = argv[++i]; + g.bSchemaCompare = + sqlite3_stricmp(zTab, "sqlite_schema")==0 + || sqlite3_stricmp(zTab, "sqlite_master")==0; }else if( strcmp(z,"transaction")==0 ){ useTransaction = 1; }else + if( strcmp(z,"vtab")==0 ){ + g.bHandleVtab = 1; + }else { cmdlineError("unknown option: %s", argv[i]); } @@ -1825,14 +1983,24 @@ int main(int argc, char **argv){ if( zDb2==0 ){ cmdlineError("two database arguments required"); } - rc = sqlite3_open(zDb1, &g.db); + if( g.bSchemaOnly && g.bSchemaCompare ){ + cmdlineError("The --schema option is useless with --table %s .", zTab); + } + rc = sqlite3_open_v2(zDb1, &g.db, SQLITE_OPEN_READONLY, 0); if( rc ){ cmdlineError("cannot open database file \"%s\"", zDb1); } - rc = sqlite3_exec(g.db, "SELECT * FROM sqlite_master", 0, 0, &zErrMsg); + rc = sqlite3_exec(g.db, "SELECT * FROM sqlite_schema", 0, 0, &zErrMsg); if( rc || zErrMsg ){ cmdlineError("\"%s\" does not appear to be a valid SQLite database", zDb1); } + { + sqlite3 *db2 = 0; + if( sqlite3_open_v2(zDb2, &db2, SQLITE_OPEN_READONLY, 0) ){ + cmdlineError("cannot open database file \"%s\"", zDb2); + } + sqlite3_close(db2); + } #ifndef SQLITE_OMIT_LOAD_EXTENSION sqlite3_enable_load_extension(g.db, 1); for(i=0; i<nExt; i++){ @@ -1841,38 +2009,39 @@ int main(int argc, char **argv){ cmdlineError("error loading %s: %s", azExt[i], zErrMsg); } } -#endif free(azExt); +#endif zSql = sqlite3_mprintf("ATTACH %Q as aux;", zDb2); rc = sqlite3_exec(g.db, zSql, 0, 0, &zErrMsg); + sqlite3_free(zSql); + zSql = 0; if( rc || zErrMsg ){ cmdlineError("cannot attach database \"%s\"", zDb2); } - rc = sqlite3_exec(g.db, "SELECT * FROM aux.sqlite_master", 0, 0, &zErrMsg); + rc = sqlite3_exec(g.db, "SELECT * FROM aux.sqlite_schema", 0, 0, &zErrMsg); if( rc || zErrMsg ){ cmdlineError("\"%s\" does not appear to be a valid SQLite database", zDb2); } if( neverUseTransaction ) useTransaction = 0; - if( useTransaction ) printf("BEGIN TRANSACTION;\n"); + if( useTransaction ) sqlite3_fprintf(out, "BEGIN TRANSACTION;\n"); + if( xDiff==rbudiff_one_table ){ + sqlite3_fprintf(out, "CREATE TABLE IF NOT EXISTS rbu_count" + "(tbl TEXT PRIMARY KEY COLLATE NOCASE, cnt INTEGER) " + "WITHOUT ROWID;\n" + ); + } if( zTab ){ xDiff(zTab, out); }else{ /* Handle tables one by one */ - pStmt = db_prepare( - "SELECT name FROM main.sqlite_master\n" - " WHERE type='table' AND sql NOT LIKE 'CREATE VIRTUAL%%'\n" - " UNION\n" - "SELECT name FROM aux.sqlite_master\n" - " WHERE type='table' AND sql NOT LIKE 'CREATE VIRTUAL%%'\n" - " ORDER BY name" - ); + pStmt = db_prepare("%s", all_tables_sql() ); while( SQLITE_ROW==sqlite3_step(pStmt) ){ xDiff((const char*)sqlite3_column_text(pStmt,0), out); } sqlite3_finalize(pStmt); } - if( useTransaction ) printf("COMMIT;\n"); + if( useTransaction ) sqlite3_fprintf(stdout,"COMMIT;\n"); /* TBD: Handle trigger differences */ /* TBD: Handle view differences */ diff --git a/tool/sqlite3_analyzer.c.in b/tool/sqlite3_analyzer.c.in new file mode 100644 index 0000000000..9860c0b0fc --- /dev/null +++ b/tool/sqlite3_analyzer.c.in @@ -0,0 +1,80 @@ +/* +** Read an SQLite database file and analyze its space utilization. Generate +** text on standard output. +*/ +#define TCLSH_INIT_PROC sqlite3_analyzer_init_proc +IFDEF INCLUDE_SQLITE3_C +#undef SQLITE_ENABLE_DBSTAT_VTAB +#define SQLITE_ENABLE_DBSTAT_VTAB 1 +#undef SQLITE_THREADSAFE +#define SQLITE_THREADSAFE 0 +#undef SQLITE_ENABLE_COLUMN_METADATA +#define SQLITE_OMIT_DECLTYPE 1 +#define SQLITE_OMIT_DEPRECATED 1 +#define SQLITE_OMIT_PROGRESS_CALLBACK 1 +#define SQLITE_OMIT_SHARED_CACHE 1 +#define SQLITE_DEFAULT_MEMSTATUS 0 +#define SQLITE_MAX_EXPR_DEPTH 0 +#define SQLITE_OMIT_LOAD_EXTENSION 1 +INCLUDE sqlite3.c +ELSE +#include "sqlite3.h" +ENDIF +INCLUDE $ROOT/src/tclsqlite.c + +#if defined(_WIN32) +INCLUDE $ROOT/ext/misc/sqlite3_stdio.h +INCLUDE $ROOT/ext/misc/sqlite3_stdio.c + +/* Substitute "puts" command. Only these forms recognized: +** +** puts STRING +** puts stderr STRING +** puts -nonewline STRING +*/ +static int subst_puts( + void *NotUsed, + Tcl_Interp *interp, + int objc, + Tcl_Obj *const*objv +){ + FILE *pOut = stdout; + const char *zOut; + int addNewLine = 1; + if( objc==2 ){ + zOut = Tcl_GetString(objv[1]); + }else if( objc!=3 ){ + Tcl_WrongNumArgs(interp, 1, objv, "?stderr|-nonewline? STRING"); + return TCL_ERROR; + }else{ + const char *zArg = Tcl_GetString(objv[1]); + if( zArg==0 ) return TCL_ERROR; + zOut = Tcl_GetString(objv[2]); + if( strcmp(zArg, "stderr")==0 ){ + pOut = stderr; + }else if( strcmp(zArg, "-nonewline")==0 ){ + addNewLine = 0; + }else{ + Tcl_AppendResult(interp, "bad argument: ", zArg, NULL); + return TCL_ERROR; + } + } + sqlite3_fputs(zOut, pOut); + if( addNewLine ) sqlite3_fputs("\n", pOut); + fflush(pOut); + return TCL_OK; +} +#endif /* defined(_WIN32) */ + +const char *sqlite3_analyzer_init_proc(Tcl_Interp *interp){ +#if defined(_WIN32) + Tcl_CreateObjCommand(interp, "puts", subst_puts, 0, 0); +#else + (void)interp; +#endif + return +BEGIN_STRING +INCLUDE $ROOT/tool/spaceanal.tcl +END_STRING +; +} diff --git a/tool/sqlite3_rsync.c b/tool/sqlite3_rsync.c new file mode 100644 index 0000000000..ad9f132bb4 --- /dev/null +++ b/tool/sqlite3_rsync.c @@ -0,0 +1,2397 @@ +/* +** 2024-09-10 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** This is a utility program that makes a copy of a live SQLite database +** using a bandwidth-efficient protocol, similar to "rsync". +*/ +#include <stdio.h> +#include <stdlib.h> +#include <ctype.h> +#include <string.h> +#include <stdarg.h> +#include "sqlite3.h" + +static const char zUsage[] = + "sqlite3_rsync ORIGIN REPLICA ?OPTIONS?\n" + "\n" + "One of ORIGIN or REPLICA is a pathname to a database on the local\n" + "machine and the other is of the form \"USER@HOST:PATH\" describing\n" + "a database on a remote machine. This utility makes REPLICA into a\n" + "copy of ORIGIN\n" + "\n" + "OPTIONS:\n" + "\n" + " --exe PATH Name of the sqlite3_rsync program on the remote side\n" + " --help Show this help screen\n" + " --protocol N Use sync protocol version N.\n" + " --ssh PATH Name of the SSH program used to reach the remote side\n" + " -v Verbose. Multiple v's for increasing output\n" + " --version Show detailed version information\n" + " --wal-only Do not sync unless both databases are in WAL mode\n" +; + +typedef unsigned char u8; +typedef sqlite3_uint64 u64; + +/* Context for the run */ +typedef struct SQLiteRsync SQLiteRsync; +struct SQLiteRsync { + const char *zOrigin; /* Name of the origin */ + const char *zReplica; /* Name of the replica */ + const char *zErrFile; /* Append error messages to this file */ + const char *zDebugFile; /* Append debugging messages to this file */ + FILE *pOut; /* Transmit to the other side */ + FILE *pIn; /* Receive from the other side */ + FILE *pLog; /* Duplicate output here if not NULL */ + FILE *pDebug; /* Write debug info here if not NULL */ + sqlite3 *db; /* Database connection */ + int nErr; /* Number of errors encountered */ + int nWrErr; /* Number of failed attempts to write on the pipe */ + u8 eVerbose; /* Bigger for more output. 0 means none. */ + u8 bCommCheck; /* True to debug the communication protocol */ + u8 isRemote; /* On the remote side of a connection */ + u8 isReplica; /* True if running on the replica side */ + u8 iProtocol; /* Protocol version number */ + u8 wrongEncoding; /* ATTACH failed due to wrong encoding */ + u8 bWalOnly; /* Require WAL mode */ + sqlite3_uint64 nOut; /* Bytes transmitted */ + sqlite3_uint64 nIn; /* Bytes received */ + unsigned int nPage; /* Total number of pages in the database */ + unsigned int szPage; /* Database page size */ + u64 nHashSent; /* Hashes sent (replica to origin) */ + unsigned int nRound; /* Number of hash batches (replica to origin) */ + unsigned int nPageSent; /* Page contents sent (origin to replica) */ +}; + +/* The version number of the protocol. Sent in the *_BEGIN message +** to verify that both sides speak the same dialect. +*/ +#define PROTOCOL_VERSION 2 + + +/* Magic numbers to identify particular messages sent over the wire. +*/ +/**** Baseline: protocol version 1 ****/ +#define ORIGIN_BEGIN 0x41 /* Initial message */ +#define ORIGIN_END 0x42 /* Time to quit */ +#define ORIGIN_ERROR 0x43 /* Error message from the remote */ +#define ORIGIN_PAGE 0x44 /* New page data */ +#define ORIGIN_TXN 0x45 /* Transaction commit */ +#define ORIGIN_MSG 0x46 /* Informational message */ +/**** Added in protocol version 2 ****/ +#define ORIGIN_DETAIL 0x47 /* Request finer-grain hash info */ +#define ORIGIN_READY 0x48 /* Ready for next round of hash exchanges */ + +/**** Baseline: protocol version 1 ****/ +#define REPLICA_BEGIN 0x61 /* Welcome message */ +#define REPLICA_ERROR 0x62 /* Error. Report and quit. */ +#define REPLICA_END 0x63 /* Replica wants to stop */ +#define REPLICA_HASH 0x64 /* One or more pages hashes to report */ +#define REPLICA_READY 0x65 /* Read to receive page content */ +#define REPLICA_MSG 0x66 /* Informational message */ +/**** Added in protocol version 2 ****/ +#define REPLICA_CONFIG 0x67 /* Hash exchange configuration */ + +/**************************************************************************** +** Beginning of the popen2() implementation copied from Fossil ************* +****************************************************************************/ +#ifdef _WIN32 +#include <windows.h> +#include <io.h> +#include <fcntl.h> +/* +** Print a fatal error and quit. +*/ +static void win32_fatal_error(const char *zMsg){ + fprintf(stderr, "%s", zMsg); + exit(1); +} +extern int _open_osfhandle(intptr_t,int); +#else +#include <unistd.h> +#include <signal.h> +#include <sys/wait.h> +#endif + +/* +** The following macros are used to cast pointers to integers and +** integers to pointers. The way you do this varies from one compiler +** to the next, so we have developed the following set of #if statements +** to generate appropriate macros for a wide range of compilers. +** +** The correct "ANSI" way to do this is to use the intptr_t type. +** Unfortunately, that typedef is not available on all compilers, or +** if it is available, it requires an #include of specific headers +** that vary from one machine to the next. +** +** This code is copied out of SQLite. +*/ +#if defined(__PTRDIFF_TYPE__) /* This case should work for GCC */ +# define INT_TO_PTR(X) ((void*)(__PTRDIFF_TYPE__)(X)) +# define PTR_TO_INT(X) ((int)(__PTRDIFF_TYPE__)(X)) +#elif !defined(__GNUC__) /* Works for compilers other than LLVM */ +# define INT_TO_PTR(X) ((void*)&((char*)0)[X]) +# define PTR_TO_INT(X) ((int)(((char*)X)-(char*)0)) +#elif defined(HAVE_STDINT_H) /* Use this case if we have ANSI headers */ +# define INT_TO_PTR(X) ((void*)(intptr_t)(X)) +# define PTR_TO_INT(X) ((int)(intptr_t)(X)) +#else /* Generates a warning - but it always works */ +# define INT_TO_PTR(X) ((void*)(X)) +# define PTR_TO_INT(X) ((int)(X)) +#endif + +/* Register SQL functions provided by ext/misc/sha1.c */ +extern int sqlite3_sha_init( + sqlite3 *db, + char **pzErrMsg, + const sqlite3_api_routines *pApi +); + +#ifdef _WIN32 +/* +** On windows, create a child process and specify the stdin, stdout, +** and stderr channels for that process to use. +** +** Return the number of errors. +*/ +static int win32_create_child_process( + wchar_t *zCmd, /* The command that the child process will run */ + HANDLE hIn, /* Standard input */ + HANDLE hOut, /* Standard output */ + HANDLE hErr, /* Standard error */ + DWORD *pChildPid /* OUT: Child process handle */ +){ + STARTUPINFOW si; + PROCESS_INFORMATION pi; + BOOL rc; + + memset(&si, 0, sizeof(si)); + si.cb = sizeof(si); + si.dwFlags = STARTF_USESTDHANDLES; + SetHandleInformation(hIn, HANDLE_FLAG_INHERIT, TRUE); + si.hStdInput = hIn; + SetHandleInformation(hOut, HANDLE_FLAG_INHERIT, TRUE); + si.hStdOutput = hOut; + SetHandleInformation(hErr, HANDLE_FLAG_INHERIT, TRUE); + si.hStdError = hErr; + rc = CreateProcessW( + NULL, /* Application Name */ + zCmd, /* Command-line */ + NULL, /* Process attributes */ + NULL, /* Thread attributes */ + TRUE, /* Inherit Handles */ + 0, /* Create flags */ + NULL, /* Environment */ + NULL, /* Current directory */ + &si, /* Startup Info */ + &pi /* Process Info */ + ); + if( rc ){ + CloseHandle( pi.hProcess ); + CloseHandle( pi.hThread ); + *pChildPid = pi.dwProcessId; + }else{ + win32_fatal_error("cannot create child process"); + } + return rc!=0; +} +void *win32_utf8_to_unicode(const char *zUtf8){ + int nByte = MultiByteToWideChar(CP_UTF8, 0, zUtf8, -1, 0, 0); + wchar_t *zUnicode = malloc( nByte*2 ); + MultiByteToWideChar(CP_UTF8, 0, zUtf8, -1, zUnicode, nByte); + return zUnicode; +} +#endif + +/* +** Create a child process running shell command "zCmd". *ppOut is +** a FILE that becomes the standard input of the child process. +** (The caller writes to *ppOut in order to send text to the child.) +** *ppIn is stdout from the child process. (The caller +** reads from *ppIn in order to receive input from the child.) +** Note that *ppIn is an unbuffered file descriptor, not a FILE. +** The process ID of the child is written into *pChildPid. +** +** Return the number of errors. +*/ +static int popen2( + const char *zCmd, /* Command to run in the child process */ + FILE **ppIn, /* Read from child using this file descriptor */ + FILE **ppOut, /* Write to child using this file descriptor */ + int *pChildPid, /* PID of the child process */ + int bDirect /* 0: run zCmd as a shell cmd. 1: run directly */ +){ +#ifdef _WIN32 + HANDLE hStdinRd, hStdinWr, hStdoutRd, hStdoutWr, hStderr; + SECURITY_ATTRIBUTES saAttr; + DWORD childPid = 0; + int fd; + + saAttr.nLength = sizeof(saAttr); + saAttr.bInheritHandle = TRUE; + saAttr.lpSecurityDescriptor = NULL; + hStderr = GetStdHandle(STD_ERROR_HANDLE); + if( !CreatePipe(&hStdoutRd, &hStdoutWr, &saAttr, 4096) ){ + win32_fatal_error("cannot create pipe for stdout"); + } + SetHandleInformation( hStdoutRd, HANDLE_FLAG_INHERIT, FALSE); + + if( !CreatePipe(&hStdinRd, &hStdinWr, &saAttr, 4096) ){ + win32_fatal_error("cannot create pipe for stdin"); + } + SetHandleInformation( hStdinWr, HANDLE_FLAG_INHERIT, FALSE); + + win32_create_child_process(win32_utf8_to_unicode(zCmd), + hStdinRd, hStdoutWr, hStderr,&childPid); + *pChildPid = childPid; + fd = _open_osfhandle(PTR_TO_INT(hStdoutRd), 0); + *ppIn = fdopen(fd, "rb"); + fd = _open_osfhandle(PTR_TO_INT(hStdinWr), 0); + *ppOut = _fdopen(fd, "wb"); + CloseHandle(hStdinRd); + CloseHandle(hStdoutWr); + return 0; +#else + int pin[2], pout[2]; + *ppIn = 0; + *ppOut = 0; + *pChildPid = 0; + + if( pipe(pin)<0 ){ + return 1; + } + if( pipe(pout)<0 ){ + close(pin[0]); + close(pin[1]); + return 1; + } + *pChildPid = fork(); + if( *pChildPid<0 ){ + close(pin[0]); + close(pin[1]); + close(pout[0]); + close(pout[1]); + *pChildPid = 0; + return 1; + } + signal(SIGPIPE,SIG_IGN); + if( *pChildPid==0 ){ + int fd; + /* This is the child process */ + close(0); + fd = dup(pout[0]); + if( fd!=0 ) { + fprintf(stderr,"popen2() failed to open file descriptor 0"); + exit(1); + } + close(pout[0]); + close(pout[1]); + close(1); + fd = dup(pin[1]); + if( fd!=1 ){ + fprintf(stderr,"popen() failed to open file descriptor 1"); + exit(1); + } + close(pin[0]); + close(pin[1]); + if( bDirect ){ + execl(zCmd, zCmd, (char*)0); + }else{ + execl("/bin/sh", "/bin/sh", "-c", zCmd, (char*)0); + } + return 1; + }else{ + /* This is the parent process */ + close(pin[1]); + *ppIn = fdopen(pin[0], "r"); + close(pout[0]); + *ppOut = fdopen(pout[1], "w"); + return 0; + } +#endif +} + +/* +** Close the connection to a child process previously created using +** popen2(). +*/ +static void pclose2(FILE *pIn, FILE *pOut, int childPid){ +#ifdef _WIN32 + /* Not implemented, yet */ + fclose(pIn); + fclose(pOut); +#else + fclose(pIn); + fclose(pOut); + while( waitpid(0, 0, WNOHANG)>0 ) {} +#endif +} +/***************************************************************************** +** End of the popen2() implementation copied from Fossil ********************* +*****************************************************************************/ + +/***************************************************************************** +** Beginning of the append_escaped_arg() routine, adapted from the Fossil ** +** subroutine nameed blob_append_escaped_arg() ** +*****************************************************************************/ +/* +** ASCII (for reference): +** x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf +** 0x ^` ^a ^b ^c ^d ^e ^f ^g \b \t \n () \f \r ^n ^o +** 1x ^p ^q ^r ^s ^t ^u ^v ^w ^x ^y ^z ^{ ^| ^} ^~ ^ +** 2x () ! " # $ % & ' ( ) * + , - . / +** 3x 0 1 2 3 4 5 6 7 8 9 : ; < = > ? +** 4x @ A B C D E F G H I J K L M N O +** 5x P Q R S T U V W X Y Z [ \ ] ^ _ +** 6x ` a b c d e f g h i j k l m n o +** 7x p q r s t u v w x y z { | } ~ ^_ +*/ + +/* +** Meanings for bytes in a filename: +** +** 0 Ordinary character. No encoding required +** 1 Needs to be escaped +** 2 Illegal character. Do not allow in a filename +** 3 First byte of a 2-byte UTF-8 +** 4 First byte of a 3-byte UTF-8 +** 5 First byte of a 4-byte UTF-8 +*/ +static const char aSafeChar[256] = { +#ifdef _WIN32 +/* Windows +** Prohibit: all control characters, including tab, \r and \n. +** Escape: (space) " # $ % & ' ( ) * ; < > ? [ ] ^ ` { | } +*/ +/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */ + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0x */ + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 1x */ + 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 2x */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, /* 3x */ + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 4x */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, /* 5x */ + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 6x */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, /* 7x */ +#else +/* Unix +** Prohibit: all control characters, including tab, \r and \n +** Escape: (space) ! " # $ % & ' ( ) * ; < > ? [ \ ] ^ ` { | } +*/ +/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */ + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0x */ + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 1x */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 2x */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, /* 3x */ + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 4x */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, /* 5x */ + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 6x */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, /* 7x */ +#endif + /* all bytes 0x80 through 0xbf are unescaped, being secondary + ** bytes to UTF8 characters. Bytes 0xc0 through 0xff are the + ** first byte of a UTF8 character and do get escaped */ + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 8x */ + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 9x */ + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* ax */ + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* bx */ + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* cx */ + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* dx */ + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, /* ex */ + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 /* fx */ +}; + +/* +** pStr is a shell command under construction. This routine safely +** appends filename argument zIn. It returns 0 on success or non-zero +** on any error. +** +** The argument is escaped if it contains white space or other characters +** that need to be escaped for the shell. If zIn contains characters +** that cannot be safely escaped, then throw a fatal error. +** +** If the isFilename argument is true, then the argument is expected +** to be a filename. As shell commands commonly have command-line +** options that begin with "-" and since we do not want an attacker +** to be able to invoke these switches using filenames that begin +** with "-", if zIn begins with "-", prepend an additional "./" +** (or ".\\" on Windows). +*/ +int append_escaped_arg(sqlite3_str *pStr, const char *zIn, int isFilename){ + int i; + unsigned char c; + int needEscape = 0; + int n = sqlite3_str_length(pStr); + char *z = sqlite3_str_value(pStr); + + /* Look for illegal byte-sequences and byte-sequences that require + ** escaping. No control-characters are allowed. All spaces and + ** non-ASCII unicode characters and some punctuation characters require + ** escaping. */ + for(i=0; (c = (unsigned char)zIn[i])!=0; i++){ + if( aSafeChar[c] ){ + unsigned char x = aSafeChar[c]; + needEscape = 1; + if( x==2 ){ + /* Bad ASCII character */ + return 1; + }else if( x>2 ){ + if( (zIn[i+1]&0xc0)!=0x80 + || (x>=4 && (zIn[i+2]&0xc0)!=0x80) + || (x==5 && (zIn[i+3]&0xc0)!=0x80) + ){ + /* Bad UTF8 character */ + return 1; + } + i += x-2; + } + } + } + + /* Separate from the previous argument by a space */ + if( n>0 && !isspace(z[n-1]) ){ + sqlite3_str_appendchar(pStr, 1, ' '); + } + + /* Check for characters that need quoting */ + if( !needEscape ){ + if( isFilename && zIn[0]=='-' ){ + sqlite3_str_appendchar(pStr, 1, '.'); +#if defined(_WIN32) + sqlite3_str_appendchar(pStr, 1, '\\'); +#else + sqlite3_str_appendchar(pStr, 1, '/'); +#endif + } + sqlite3_str_appendall(pStr, zIn); + }else{ +#if defined(_WIN32) + /* Quoting strategy for windows: + ** Put the entire name inside of "...". Any " characters within + ** the name get doubled. + */ + sqlite3_str_appendchar(pStr, 1, '"'); + if( isFilename && zIn[0]=='-' ){ + sqlite3_str_appendchar(pStr, 1, '.'); + sqlite3_str_appendchar(pStr, 1, '\\'); + }else if( zIn[0]=='/' ){ + sqlite3_str_appendchar(pStr, 1, '.'); + } + for(i=0; (c = (unsigned char)zIn[i])!=0; i++){ + sqlite3_str_appendchar(pStr, 1, (char)c); + if( c=='"' ) sqlite3_str_appendchar(pStr, 1, '"'); + if( c=='\\' ) sqlite3_str_appendchar(pStr, 1, '\\'); + if( c=='%' && isFilename ) sqlite3_str_append(pStr, "%cd:~,%", 7); + } + sqlite3_str_appendchar(pStr, 1, '"'); +#else + /* Quoting strategy for unix: + ** If the name does not contain ', then surround the whole thing + ** with '...'. If there is one or more ' characters within the + ** name, then put \ before each special character. + */ + if( strchr(zIn,'\'') ){ + if( isFilename && zIn[0]=='-' ){ + sqlite3_str_appendchar(pStr, 1, '.'); + sqlite3_str_appendchar(pStr, 1, '/'); + } + for(i=0; (c = (unsigned char)zIn[i])!=0; i++){ + if( aSafeChar[c] && aSafeChar[c]!=2 ){ + sqlite3_str_appendchar(pStr, 1, '\\'); + } + sqlite3_str_appendchar(pStr, 1, (char)c); + } + }else{ + sqlite3_str_appendchar(pStr, 1, '\''); + if( isFilename && zIn[0]=='-' ){ + sqlite3_str_appendchar(pStr, 1, '.'); + sqlite3_str_appendchar(pStr, 1, '/'); + } + sqlite3_str_appendall(pStr, zIn); + sqlite3_str_appendchar(pStr, 1, '\''); + } +#endif + } + return 0; +} + +/* Add an approprate PATH= argument to the SSH command under construction +** in pStr +** +** About This Feature +** ================== +** +** On some ssh servers (Macs in particular are guilty of this) the PATH +** variable in the shell that runs the command that is sent to the remote +** host contains a limited number of read-only system directories: +** +** /usr/bin:/bin:/usr/sbin:/sbin +** +** The sqlite3_rsync executable cannot be installed into any of those +** directories because they are locked down, and so the "sqlite3_rsync" +** command cannot run. +** +** To work around this, the sqlite3_rsync command is prefixed with a PATH= +** argument, inserted by this function, to augment the PATH with additional +** directories in which the sqlite3_rsync executable can be installed. +** +** But other ssh servers are confused by this initial PATH= argument. +** Some ssh servers have a list of programs that they are allowed to run +** and will fail if the first argument is not on that list, and PATH=.... +** is not on that list. +** +** So that sqlite3_rsync can invoke itself on a remote system using ssh +** on a variety of platforms, the following algorithm is used: +** +** * First try running the sqlite3_rsync without any PATH= argument. +** If that works (and it does on a majority of systems) then we are +** done. +** +** * If the first attempt fails, then try again after adding the +** PATH= prefix argument. (This function is what adds that +** argument.) +** +** A consequence of this is that if the remote system is a Mac, the +** "ssh" command always ends up being invoked twice. If anybody knows a +** way around that problem, please bring it to the attention of the +** developers. +*/ +void add_path_argument(sqlite3_str *pStr){ + append_escaped_arg(pStr, + "PATH=$HOME/bin:/usr/local/bin:/opt/homebrew/bin" + ":/opt/local/bin:$PATH", 0); +} + +/***************************************************************************** +** End of the append_escaped_arg() routine, adapted from the Fossil ** +*****************************************************************************/ + +/***************************************************************************** +** The Hash Engine +** +** This is basically SHA3, though with a 160-bit hash, and reducing the +** number of rounds in the KeccakF1600 step function from 24 to 6. +*/ +/* +** Macros to determine whether the machine is big or little endian, +** and whether or not that determination is run-time or compile-time. +** +** For best performance, an attempt is made to guess at the byte-order +** using C-preprocessor macros. If that is unsuccessful, or if +** -DHash_BYTEORDER=0 is set, then byte-order is determined +** at run-time. +*/ +#ifndef Hash_BYTEORDER +# if defined(i386) || defined(__i386__) || defined(_M_IX86) || \ + defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \ + defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \ + defined(__arm__) +# define Hash_BYTEORDER 1234 +# elif defined(sparc) || defined(__ppc__) +# define Hash_BYTEORDER 4321 +# else +# define Hash_BYTEORDER 0 +# endif +#endif + +/* +** State structure for a Hash hash in progress +*/ +typedef struct HashContext HashContext; +struct HashContext { + union { + u64 s[25]; /* Keccak state. 5x5 lines of 64 bits each */ + unsigned char x[1600]; /* ... or 1600 bytes */ + } u; + unsigned nRate; /* Bytes of input accepted per Keccak iteration */ + unsigned nLoaded; /* Input bytes loaded into u.x[] so far this cycle */ + unsigned ixMask; /* Insert next input into u.x[nLoaded^ixMask]. */ + unsigned iSize; /* 224, 256, 358, or 512 */ +}; + +/* +** A single step of the Keccak mixing function for a 1600-bit state +*/ +static void KeccakF1600Step(HashContext *p){ + int i; + u64 b0, b1, b2, b3, b4; + u64 c0, c1, c2, c3, c4; + u64 d0, d1, d2, d3, d4; + static const u64 RC[] = { + 0x0000000000000001ULL, 0x0000000000008082ULL, + 0x800000000000808aULL, 0x8000000080008000ULL, + 0x000000000000808bULL, 0x0000000080000001ULL, + 0x8000000080008081ULL, 0x8000000000008009ULL, + 0x000000000000008aULL, 0x0000000000000088ULL, + 0x0000000080008009ULL, 0x000000008000000aULL, + 0x000000008000808bULL, 0x800000000000008bULL, + 0x8000000000008089ULL, 0x8000000000008003ULL, + 0x8000000000008002ULL, 0x8000000000000080ULL, + 0x000000000000800aULL, 0x800000008000000aULL, + 0x8000000080008081ULL, 0x8000000000008080ULL, + 0x0000000080000001ULL, 0x8000000080008008ULL + }; +# define a00 (p->u.s[0]) +# define a01 (p->u.s[1]) +# define a02 (p->u.s[2]) +# define a03 (p->u.s[3]) +# define a04 (p->u.s[4]) +# define a10 (p->u.s[5]) +# define a11 (p->u.s[6]) +# define a12 (p->u.s[7]) +# define a13 (p->u.s[8]) +# define a14 (p->u.s[9]) +# define a20 (p->u.s[10]) +# define a21 (p->u.s[11]) +# define a22 (p->u.s[12]) +# define a23 (p->u.s[13]) +# define a24 (p->u.s[14]) +# define a30 (p->u.s[15]) +# define a31 (p->u.s[16]) +# define a32 (p->u.s[17]) +# define a33 (p->u.s[18]) +# define a34 (p->u.s[19]) +# define a40 (p->u.s[20]) +# define a41 (p->u.s[21]) +# define a42 (p->u.s[22]) +# define a43 (p->u.s[23]) +# define a44 (p->u.s[24]) +# define ROL64(a,x) ((a<<x)|(a>>(64-x))) + + /* v---- Number of rounds. SHA3 has 24 here. */ + for(i=0; i<6; i++){ + c0 = a00^a10^a20^a30^a40; + c1 = a01^a11^a21^a31^a41; + c2 = a02^a12^a22^a32^a42; + c3 = a03^a13^a23^a33^a43; + c4 = a04^a14^a24^a34^a44; + d0 = c4^ROL64(c1, 1); + d1 = c0^ROL64(c2, 1); + d2 = c1^ROL64(c3, 1); + d3 = c2^ROL64(c4, 1); + d4 = c3^ROL64(c0, 1); + + b0 = (a00^d0); + b1 = ROL64((a11^d1), 44); + b2 = ROL64((a22^d2), 43); + b3 = ROL64((a33^d3), 21); + b4 = ROL64((a44^d4), 14); + a00 = b0 ^((~b1)& b2 ); + a00 ^= RC[i]; + a11 = b1 ^((~b2)& b3 ); + a22 = b2 ^((~b3)& b4 ); + a33 = b3 ^((~b4)& b0 ); + a44 = b4 ^((~b0)& b1 ); + + b2 = ROL64((a20^d0), 3); + b3 = ROL64((a31^d1), 45); + b4 = ROL64((a42^d2), 61); + b0 = ROL64((a03^d3), 28); + b1 = ROL64((a14^d4), 20); + a20 = b0 ^((~b1)& b2 ); + a31 = b1 ^((~b2)& b3 ); + a42 = b2 ^((~b3)& b4 ); + a03 = b3 ^((~b4)& b0 ); + a14 = b4 ^((~b0)& b1 ); + + b4 = ROL64((a40^d0), 18); + b0 = ROL64((a01^d1), 1); + b1 = ROL64((a12^d2), 6); + b2 = ROL64((a23^d3), 25); + b3 = ROL64((a34^d4), 8); + a40 = b0 ^((~b1)& b2 ); + a01 = b1 ^((~b2)& b3 ); + a12 = b2 ^((~b3)& b4 ); + a23 = b3 ^((~b4)& b0 ); + a34 = b4 ^((~b0)& b1 ); + + b1 = ROL64((a10^d0), 36); + b2 = ROL64((a21^d1), 10); + b3 = ROL64((a32^d2), 15); + b4 = ROL64((a43^d3), 56); + b0 = ROL64((a04^d4), 27); + a10 = b0 ^((~b1)& b2 ); + a21 = b1 ^((~b2)& b3 ); + a32 = b2 ^((~b3)& b4 ); + a43 = b3 ^((~b4)& b0 ); + a04 = b4 ^((~b0)& b1 ); + + b3 = ROL64((a30^d0), 41); + b4 = ROL64((a41^d1), 2); + b0 = ROL64((a02^d2), 62); + b1 = ROL64((a13^d3), 55); + b2 = ROL64((a24^d4), 39); + a30 = b0 ^((~b1)& b2 ); + a41 = b1 ^((~b2)& b3 ); + a02 = b2 ^((~b3)& b4 ); + a13 = b3 ^((~b4)& b0 ); + a24 = b4 ^((~b0)& b1 ); + } +} + +/* +** Initialize a new hash. iSize determines the size of the hash +** in bits and should be one of 224, 256, 384, or 512. Or iSize +** can be zero to use the default hash size of 256 bits. +*/ +static void HashInit(HashContext *p, int iSize){ + memset(p, 0, sizeof(*p)); + p->iSize = iSize; + if( iSize>=128 && iSize<=512 ){ + p->nRate = (1600 - ((iSize + 31)&~31)*2)/8; + }else{ + p->nRate = (1600 - 2*256)/8; + } +#if Hash_BYTEORDER==1234 + /* Known to be little-endian at compile-time. No-op */ +#elif Hash_BYTEORDER==4321 + p->ixMask = 7; /* Big-endian */ +#else + { + static unsigned int one = 1; + if( 1==*(unsigned char*)&one ){ + /* Little endian. No byte swapping. */ + p->ixMask = 0; + }else{ + /* Big endian. Byte swap. */ + p->ixMask = 7; + } + } +#endif +} + +/* +** Make consecutive calls to the HashUpdate function to add new content +** to the hash +*/ +static void HashUpdate( + HashContext *p, + const unsigned char *aData, + unsigned int nData +){ + unsigned int i = 0; + if( aData==0 ) return; +#if Hash_BYTEORDER==1234 + if( (p->nLoaded % 8)==0 && ((aData - (const unsigned char*)0)&7)==0 ){ + for(; i+7<nData; i+=8){ + p->u.s[p->nLoaded/8] ^= *(u64*)&aData[i]; + p->nLoaded += 8; + if( p->nLoaded>=p->nRate ){ + KeccakF1600Step(p); + p->nLoaded = 0; + } + } + } +#endif + for(; i<nData; i++){ +#if Hash_BYTEORDER==1234 + p->u.x[p->nLoaded] ^= aData[i]; +#elif Hash_BYTEORDER==4321 + p->u.x[p->nLoaded^0x07] ^= aData[i]; +#else + p->u.x[p->nLoaded^p->ixMask] ^= aData[i]; +#endif + p->nLoaded++; + if( p->nLoaded==p->nRate ){ + KeccakF1600Step(p); + p->nLoaded = 0; + } + } +} + +/* +** After all content has been added, invoke HashFinal() to compute +** the final hash. The function returns a pointer to the binary +** hash value. +*/ +static unsigned char *HashFinal(HashContext *p){ + unsigned int i; + if( p->nLoaded==p->nRate-1 ){ + const unsigned char c1 = 0x86; + HashUpdate(p, &c1, 1); + }else{ + const unsigned char c2 = 0x06; + const unsigned char c3 = 0x80; + HashUpdate(p, &c2, 1); + p->nLoaded = p->nRate - 1; + HashUpdate(p, &c3, 1); + } + for(i=0; i<p->nRate; i++){ + p->u.x[i+p->nRate] = p->u.x[i^p->ixMask]; + } + return &p->u.x[p->nRate]; +} + +/* +** Implementation of the hash(X) function. +** +** Return a 160-bit BLOB which is the hash of X. +*/ +static void hashFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + HashContext cx; + int eType = sqlite3_value_type(argv[0]); + int nByte = sqlite3_value_bytes(argv[0]); + if( eType==SQLITE_NULL ) return; + HashInit(&cx, 160); + if( eType==SQLITE_BLOB ){ + HashUpdate(&cx, sqlite3_value_blob(argv[0]), nByte); + }else{ + HashUpdate(&cx, sqlite3_value_text(argv[0]), nByte); + } + sqlite3_result_blob(context, HashFinal(&cx), 160/8, SQLITE_TRANSIENT); +} + +/* +** Implementation of the agghash(X) function. +** +** Return a 160-bit BLOB which is the hash of the concatenation +** of all X inputs. +*/ +static void agghashStep( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + HashContext *pCx; + int eType = sqlite3_value_type(argv[0]); + int nByte = sqlite3_value_bytes(argv[0]); + if( eType==SQLITE_NULL ) return; + pCx = (HashContext*)sqlite3_aggregate_context(context, sizeof(*pCx)); + if( pCx==0 ) return; + if( pCx->iSize==0 ) HashInit(pCx, 160); + if( eType==SQLITE_BLOB ){ + HashUpdate(pCx, sqlite3_value_blob(argv[0]), nByte); + }else{ + HashUpdate(pCx, sqlite3_value_text(argv[0]), nByte); + } +} +static void agghashFinal(sqlite3_context *context){ + HashContext *pCx = (HashContext*)sqlite3_aggregate_context(context, 0); + if( pCx ){ + sqlite3_result_blob(context, HashFinal(pCx), 160/8, SQLITE_TRANSIENT); + } +} + +/* Register the hash function */ +static int hashRegister(sqlite3 *db){ + int rc; + rc = sqlite3_create_function(db, "hash", 1, + SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC, + 0, hashFunc, 0, 0); + if( rc==SQLITE_OK ){ + rc = sqlite3_create_function(db, "agghash", 1, + SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC, + 0, 0, agghashStep, agghashFinal); + } + return rc; +} + +/* End of the hashing logic +*****************************************************************************/ + +/* +** Return the tail of a file pathname. The tail is the last component +** of the path. For example, the tail of "/a/b/c.d" is "c.d". +*/ +const char *file_tail(const char *z){ + const char *zTail = z; + if( !zTail ) return 0; + while( z[0] ){ + if( z[0]=='/' ) zTail = &z[1]; + z++; + } + return zTail; +} + +/* +** Append error message text to the error file, if an error file is +** specified. In any case, increment the error count. +*/ +static void logError(SQLiteRsync *p, const char *zFormat, ...){ + if( p->zErrFile ){ + FILE *pErr = fopen(p->zErrFile, "a"); + if( pErr ){ + va_list ap; + va_start(ap, zFormat); + vfprintf(pErr, zFormat, ap); + va_end(ap); + fclose(pErr); + } + } + p->nErr++; +} + +/* +** Append text to the debugging mesage file, if an that file is +** specified. +*/ +static void debugMessage(SQLiteRsync *p, const char *zFormat, ...){ + if( p->zDebugFile ){ + if( p->pDebug==0 ){ + p->pDebug = fopen(p->zDebugFile, "wb"); + } + if( p->pDebug ){ + va_list ap; + va_start(ap, zFormat); + vfprintf(p->pDebug, zFormat, ap); + va_end(ap); + fflush(p->pDebug); + } + } +} + + +/* Read a single big-endian 32-bit unsigned integer from the input +** stream. Return 0 on success and 1 if there are any errors. +*/ +static int readUint32(SQLiteRsync *p, unsigned int *pU){ + unsigned char buf[4]; + if( fread(buf, sizeof(buf), 1, p->pIn)==1 ){ + *pU = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3]; + p->nIn += 4; + return 0; + }else{ + logError(p, "failed to read a 32-bit integer\n"); + return 1; + } +} + +/* Write a single big-endian 32-bit unsigned integer to the output stream. +** Return 0 on success and 1 if there are any errors. +*/ +static int writeUint32(SQLiteRsync *p, unsigned int x){ + unsigned char buf[4]; + buf[3] = x & 0xff; + x >>= 8; + buf[2] = x & 0xff; + x >>= 8; + buf[1] = x & 0xff; + x >>= 8; + buf[0] = x; + if( p->pLog ) fwrite(buf, sizeof(buf), 1, p->pLog); + if( fwrite(buf, sizeof(buf), 1, p->pOut)!=1 ){ + logError(p, "failed to write 32-bit integer 0x%x\n", x); + p->nWrErr++; + return 1; + } + p->nOut += 4; + return 0; +} + +/* Read a single byte from the wire. +*/ +int readByte(SQLiteRsync *p){ + int c = fgetc(p->pIn); + if( c!=EOF ) p->nIn++; + return c; +} + +/* Write a single byte into the wire. +*/ +void writeByte(SQLiteRsync *p, int c){ + if( p->pLog ) fputc(c, p->pLog); + fputc(c, p->pOut); + p->nOut++; +} + +/* Read a power of two encoded as a single byte. +*/ +int readPow2(SQLiteRsync *p){ + int x = readByte(p); + if( x<0 || x>=32 ){ + logError(p, "read invalid page size %d\n", x); + return 0; + } + return 1<<x; +} + +/* Write a power-of-two value onto the wire as a single byte. +*/ +void writePow2(SQLiteRsync *p, int c){ + int n; + if( c<0 || (c&(c-1))!=0 ){ + logError(p, "trying to read invalid page size %d\n", c); + } + for(n=0; c>1; n++){ c /= 2; } + writeByte(p, n); +} + +/* Read an array of bytes from the wire. +*/ +void readBytes(SQLiteRsync *p, int nByte, void *pData){ + if( fread(pData, 1, nByte, p->pIn)==nByte ){ + p->nIn += nByte; + }else{ + logError(p, "failed to read %d bytes\n", nByte); + } +} + +/* Write an array of bytes onto the wire. +*/ +void writeBytes(SQLiteRsync *p, int nByte, const void *pData){ + if( p->pLog ) fwrite(pData, 1, nByte, p->pLog); + if( fwrite(pData, 1, nByte, p->pOut)==nByte ){ + p->nOut += nByte; + }else{ + logError(p, "failed to write %d bytes\n", nByte); + p->nWrErr++; + } +} + +/* Report an error. +** +** If this happens on the remote side, we send back a *_ERROR +** message. On the local side, the error message goes to stderr. +*/ +static void reportError(SQLiteRsync *p, const char *zFormat, ...){ + va_list ap; + char *zMsg; + unsigned int nMsg; + va_start(ap, zFormat); + zMsg = sqlite3_vmprintf(zFormat, ap); + va_end(ap); + nMsg = zMsg ? (unsigned int)strlen(zMsg) : 0; + if( p->isRemote ){ + if( p->isReplica ){ + putc(REPLICA_ERROR, p->pOut); + }else{ + putc(ORIGIN_ERROR, p->pOut); + } + writeUint32(p, nMsg); + writeBytes(p, nMsg, zMsg); + fflush(p->pOut); + }else{ + fprintf(stderr, "%s\n", zMsg); + } + logError(p, "%s\n", zMsg); + sqlite3_free(zMsg); +} + +/* Send an informational message. +** +** If this happens on the remote side, we send back a *_MSG +** message. On the local side, the message goes to stdout. +*/ +static void infoMsg(SQLiteRsync *p, const char *zFormat, ...){ + va_list ap; + char *zMsg; + unsigned int nMsg; + va_start(ap, zFormat); + zMsg = sqlite3_vmprintf(zFormat, ap); + va_end(ap); + nMsg = zMsg ? (unsigned int)strlen(zMsg) : 0; + if( p->isRemote ){ + if( p->isReplica ){ + putc(REPLICA_MSG, p->pOut); + }else{ + putc(ORIGIN_MSG, p->pOut); + } + writeUint32(p, nMsg); + writeBytes(p, nMsg, zMsg); + fflush(p->pOut); + }else{ + printf("%s\n", zMsg); + } + sqlite3_free(zMsg); +} + +/* Receive and report an error message coming from the other side. +*/ +static void readAndDisplayMessage(SQLiteRsync *p, int c){ + unsigned int n = 0; + char *zMsg; + const char *zPrefix; + if( c==ORIGIN_ERROR || c==REPLICA_ERROR ){ + zPrefix = "ERROR: "; + }else{ + zPrefix = ""; + } + readUint32(p, &n); + if( n==0 ){ + fprintf(stderr,"ERROR: unknown (possibly out-of-memory)\n"); + }else{ + zMsg = sqlite3_malloc64( n+1 ); + if( zMsg==0 ){ + fprintf(stderr, "ERROR: out-of-memory\n"); + return; + } + memset(zMsg, 0, n+1); + readBytes(p, n, zMsg); + fprintf(stderr,"%s%s\n", zPrefix, zMsg); + if( zPrefix[0] ) logError(p, "%s%s\n", zPrefix, zMsg); + sqlite3_free(zMsg); + } +} + +/* Construct a new prepared statement. Report an error and return NULL +** if anything goes wrong. +*/ +static sqlite3_stmt *prepareStmtVA( + SQLiteRsync *p, + char *zFormat, + va_list ap +){ + sqlite3_stmt *pStmt = 0; + char *zSql; + char *zToFree = 0; + int rc; + + if( strchr(zFormat,'%') ){ + zSql = sqlite3_vmprintf(zFormat, ap); + if( zSql==0 ){ + reportError(p, "out-of-memory"); + return 0; + }else{ + zToFree = zSql; + } + }else{ + zSql = zFormat; + } + rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); + if( rc || pStmt==0 ){ + reportError(p, "unable to prepare SQL [%s]: %s", zSql, + sqlite3_errmsg(p->db)); + sqlite3_finalize(pStmt); + pStmt = 0; + } + if( zToFree ) sqlite3_free(zToFree); + return pStmt; +} +static sqlite3_stmt *prepareStmt( + SQLiteRsync *p, + char *zFormat, + ... +){ + sqlite3_stmt *pStmt; + va_list ap; + va_start(ap, zFormat); + pStmt = prepareStmtVA(p, zFormat, ap); + va_end(ap); + return pStmt; +} + +/* Run a single SQL statement. Report an error if something goes +** wrong. +** +** As a special case, if the statement starts with "ATTACH" (but not +** "Attach") and if the error message is about an incorrect encoding, +** then do not report the error, but instead set the wrongEncoding flag. +** This is a kludgy work-around to the problem of attaching a database +** with a non-UTF8 encoding to the empty :memory: database that is +** opened on the replica. +*/ +static void runSql(SQLiteRsync *p, char *zSql, ...){ + sqlite3_stmt *pStmt; + va_list ap; + + va_start(ap, zSql); + pStmt = prepareStmtVA(p, zSql, ap); + va_end(ap); + if( pStmt ){ + int rc = sqlite3_step(pStmt); + if( rc==SQLITE_ROW ) rc = sqlite3_step(pStmt); + if( rc!=SQLITE_OK && rc!=SQLITE_DONE ){ + const char *zErr = sqlite3_errmsg(p->db); + if( strncmp(zSql,"ATTACH ", 7)==0 + && strstr(zErr,"must use the same text encoding")!=0 + ){ + p->wrongEncoding = 1; + }else{ + reportError(p, "SQL statement [%s] failed: %s", zSql, + sqlite3_errmsg(p->db)); + } + } + sqlite3_finalize(pStmt); + } +} + +/* Run an SQL statement that returns a single unsigned 32-bit integer result +*/ +static int runSqlReturnUInt( + SQLiteRsync *p, + unsigned int *pRes, + char *zSql, + ... +){ + sqlite3_stmt *pStmt; + int res = 0; + va_list ap; + + va_start(ap, zSql); + pStmt = prepareStmtVA(p, zSql, ap); + va_end(ap); + if( pStmt==0 ){ + res = 1; + }else{ + int rc = sqlite3_step(pStmt); + if( rc==SQLITE_ROW ){ + *pRes = (unsigned int)(sqlite3_column_int64(pStmt, 0)&0xffffffff); + }else{ + reportError(p, "SQL statement [%s] failed: %s", zSql, + sqlite3_errmsg(p->db)); + res = 1; + } + sqlite3_finalize(pStmt); + } + return res; +} + +/* Run an SQL statement that returns a single TEXT value that is no more +** than 99 bytes in length. +*/ +static int runSqlReturnText( + SQLiteRsync *p, + char *pRes, + char *zSql, + ... +){ + sqlite3_stmt *pStmt; + int res = 0; + va_list ap; + + va_start(ap, zSql); + pStmt = prepareStmtVA(p, zSql, ap); + va_end(ap); + pRes[0] = 0; + if( pStmt==0 ){ + res = 1; + }else{ + int rc = sqlite3_step(pStmt); + if( rc==SQLITE_ROW ){ + const unsigned char *a = sqlite3_column_text(pStmt, 0); + int n; + if( a==0 ){ + pRes[0] = 0; + }else{ + n = sqlite3_column_bytes(pStmt, 0); + if( n>99 ) n = 99; + memcpy(pRes, a, n); + pRes[n] = 0; + } + }else{ + reportError(p, "SQL statement [%s] failed: %s", zSql, + sqlite3_errmsg(p->db)); + res = 1; + } + sqlite3_finalize(pStmt); + } + return res; +} + +/* Close the database connection associated with p +*/ +static void closeDb(SQLiteRsync *p){ + if( p->db ){ + sqlite3_close(p->db); + p->db = 0; + } +} + +/* +** Run the origin-side protocol. +** +** Begin by sending the ORIGIN_BEGIN message with two arguments, +** nPage, and szPage. Then enter a loop responding to message from +** the replica: +** +** REPLICA_BEGIN iProtocol +** +** An optional message sent by the replica in response to the +** prior ORIGIN_BEGIN with a counter-proposal for the protocol +** level. If seen, try to reduce the protocol level to what is +** requested and send a new ORGIN_BEGIN. +** +** REPLICA_ERROR size text +** +** Report an error from the replica and quit +** +** REPLICA_END +** +** The replica is terminating. Stop processing now. +** +** REPLICA_HASH hash +** +** The argument is the 20-byte SHA1 hash for the next page or +** block of pages. Hashes appear in sequential order with no gaps, +** unless there is an intervening REPLICA_CONFIG message. +** +** REPLICA_CONFIG pgno cnt +** +** Set counters used by REPLICA_HASH. The next hash will start +** on page pgno and all subsequent hashes will cover cnt pages +** each. Note that for a multi-page hash, the hash value is +** actually a hash of the individual page hashes. +** +** REPLICA_READY +** +** The replica has sent all the hashes that it intends to send. +** This side (the origin) can now start responding with page +** content for pages that do not have a matching hash or with +** ORIGIN_DETAIL messages with requests for more detail. +*/ +static void originSide(SQLiteRsync *p){ + int rc = 0; + int c = 0; + unsigned int nPage = 0; + unsigned int iHash = 1; /* Pgno for next hash to receive */ + unsigned int nHash = 1; /* Number of pages per hash received */ + unsigned int mxHash = 0; /* Maximum hash value received */ + unsigned int lockBytePage = 0; + unsigned int szPg = 0; + sqlite3_stmt *pCkHash = 0; /* Verify hash on a single page */ + sqlite3_stmt *pCkHashN = 0; /* Verify a multi-page hash */ + sqlite3_stmt *pInsHash = 0; /* Record a bad hash */ + char buf[200]; + + p->isReplica = 0; + if( p->bCommCheck ){ + infoMsg(p, "origin zOrigin=%Q zReplica=%Q isRemote=%d protocol=%d", + p->zOrigin, p->zReplica, p->isRemote, p->iProtocol); + writeByte(p, ORIGIN_END); + fflush(p->pOut); + }else{ + /* Open the ORIGIN database. */ + rc = sqlite3_open_v2(p->zOrigin, &p->db, SQLITE_OPEN_READWRITE, 0); + if( rc ){ + reportError(p, "cannot open origin \"%s\": %s", + p->zOrigin, sqlite3_errmsg(p->db)); + closeDb(p); + return; + } + hashRegister(p->db); + runSql(p, "BEGIN"); + if( p->bWalOnly ){ + runSqlReturnText(p, buf, "PRAGMA journal_mode"); + if( sqlite3_stricmp(buf,"wal")!=0 ){ + reportError(p, "Origin database is not in WAL mode"); + } + } + runSqlReturnUInt(p, &nPage, "PRAGMA page_count"); + runSqlReturnUInt(p, &szPg, "PRAGMA page_size"); + + if( p->nErr==0 ){ + /* Send the ORIGIN_BEGIN message */ + writeByte(p, ORIGIN_BEGIN); + writeByte(p, p->iProtocol); + writePow2(p, szPg); + writeUint32(p, nPage); + fflush(p->pOut); + if( p->zDebugFile ){ + debugMessage(p, "-> ORIGIN_BEGIN %u %u %u\n", p->iProtocol,szPg,nPage); + } + p->nPage = nPage; + p->szPage = szPg; + lockBytePage = (1<<30)/szPg + 1; + } + } + + /* Respond to message from the replica */ + while( p->nErr<=p->nWrErr && (c = readByte(p))!=EOF && c!=REPLICA_END ){ + switch( c ){ + case REPLICA_BEGIN: { + /* This message is only sent if the replica received an origin-protocol + ** that is larger than what it knows about. The replica sends back + ** a counter-proposal of an earlier protocol which the origin can + ** accept by resending a new ORIGIN_BEGIN. */ + u8 newProtocol = readByte(p); + if( p->zDebugFile ){ + debugMessage(p, "<- REPLICA_BEGIN %d\n", (int)newProtocol); + } + if( newProtocol < p->iProtocol ){ + p->iProtocol = newProtocol; + writeByte(p, ORIGIN_BEGIN); + writeByte(p, p->iProtocol); + writePow2(p, p->szPage); + writeUint32(p, p->nPage); + fflush(p->pOut); + if( p->zDebugFile ){ + debugMessage(p, "-> ORIGIN_BEGIN %d %d %u\n", p->iProtocol, + p->szPage, p->nPage); + } + }else{ + reportError(p, "Invalid REPLICA_BEGIN reply"); + } + break; + } + case REPLICA_MSG: + case REPLICA_ERROR: { + readAndDisplayMessage(p, c); + break; + } + case REPLICA_CONFIG: { + readUint32(p, &iHash); + readUint32(p, &nHash); + if( p->zDebugFile ){ + debugMessage(p, "<- REPLICA_CONFIG %u %u\n", iHash, nHash); + } + break; + } + case REPLICA_HASH: { + int bMatch = 0; + if( pCkHash==0 ){ + runSql(p, "CREATE TEMP TABLE badHash(" + " pgno INTEGER PRIMARY KEY," + " sz INT)"); + pCkHash = prepareStmt(p, + "SELECT hash(data)==?3 FROM sqlite_dbpage('main')" + " WHERE pgno=?1" + ); + if( pCkHash==0 ) break; + pInsHash = prepareStmt(p, "INSERT INTO badHash VALUES(?1,?2)"); + if( pInsHash==0 ) break; + } + p->nHashSent++; + readBytes(p, 20, buf); + if( nHash>1 ){ + if( pCkHashN==0 ){ + pCkHashN = prepareStmt(p, + "WITH c(n) AS " + " (VALUES(?1) UNION ALL SELECT n+1 FROM c WHERE n<?2)" + "SELECT agghash(hash(data))==?3" + " FROM c CROSS JOIN sqlite_dbpage('main') ON pgno=n" + ); + if( pCkHashN==0 ) break; + } + sqlite3_bind_int64(pCkHashN, 1, iHash); + sqlite3_bind_int64(pCkHashN, 2, iHash + nHash - 1); + sqlite3_bind_blob(pCkHashN, 3, buf, 20, SQLITE_STATIC); + rc = sqlite3_step(pCkHashN); + if( rc==SQLITE_ROW ){ + bMatch = sqlite3_column_int(pCkHashN,0); + }else if( rc==SQLITE_ERROR ){ + reportError(p, "SQL statement [%s] failed: %s", + sqlite3_sql(pCkHashN), sqlite3_errmsg(p->db)); + } + sqlite3_reset(pCkHashN); + }else{ + sqlite3_bind_int64(pCkHash, 1, iHash); + sqlite3_bind_blob(pCkHash, 3, buf, 20, SQLITE_STATIC); + rc = sqlite3_step(pCkHash); + if( rc==SQLITE_ERROR ){ + reportError(p, "SQL statement [%s] failed: %s", + sqlite3_sql(pCkHash), sqlite3_errmsg(p->db)); + }else if( rc==SQLITE_ROW && sqlite3_column_int(pCkHash,0) ){ + bMatch = 1; + } + sqlite3_reset(pCkHash); + } + if( p->zDebugFile ){ + debugMessage(p, "<- REPLICA_HASH %u %u %s %08x...\n", + iHash, nHash, + bMatch ? "match" : "fail", + *(unsigned int*)buf + ); + } + if( !bMatch ){ + sqlite3_bind_int64(pInsHash, 1, iHash); + sqlite3_bind_int64(pInsHash, 2, nHash); + rc = sqlite3_step(pInsHash); + if( rc!=SQLITE_DONE ){ + reportError(p, "SQL statement [%s] failed: %s", + sqlite3_sql(pInsHash), sqlite3_errmsg(p->db)); + } + sqlite3_reset(pInsHash); + } + if( iHash+nHash>mxHash ) mxHash = iHash+nHash; + iHash += nHash; + break; + } + case REPLICA_READY: { + int nMulti = 0; + sqlite3_stmt *pStmt; + if( p->zDebugFile ){ + debugMessage(p, "<- REPLICA_READY\n"); + } + p->nRound++; + pStmt = prepareStmt(p,"SELECT pgno, sz FROM badHash WHERE sz>1"); + if( pStmt==0 ) break; + while( sqlite3_step(pStmt)==SQLITE_ROW ){ + unsigned int pgno = (unsigned int)sqlite3_column_int64(pStmt,0); + unsigned int cnt = (unsigned int)sqlite3_column_int64(pStmt,1); + writeByte(p, ORIGIN_DETAIL); + writeUint32(p, pgno); + writeUint32(p, cnt); + nMulti++; + if( p->zDebugFile ){ + debugMessage(p, "-> ORIGIN_DETAIL %u %u\n", pgno, cnt); + } + } + sqlite3_finalize(pStmt); + if( nMulti ){ + runSql(p, "DELETE FROM badHash WHERE sz>1"); + writeByte(p, ORIGIN_READY); + if( p->zDebugFile ) debugMessage(p, "-> ORIGIN_READY\n"); + }else{ + sqlite3_finalize(pCkHash); + sqlite3_finalize(pCkHashN); + sqlite3_finalize(pInsHash); + pCkHash = 0; + pInsHash = 0; + if( mxHash<=p->nPage ){ + runSql(p, "WITH RECURSIVE c(n) AS" + " (VALUES(%d) UNION ALL SELECT n+1 FROM c WHERE n<%d)" + " INSERT INTO badHash SELECT n, 1 FROM c", + mxHash, p->nPage); + } + runSql(p, "DELETE FROM badHash WHERE pgno=%d", lockBytePage); + pStmt = prepareStmt(p, + "SELECT pgno, data" + " FROM badHash JOIN sqlite_dbpage('main') USING(pgno)"); + if( pStmt==0 ) break; + while( sqlite3_step(pStmt)==SQLITE_ROW + && p->nErr==0 + && p->nWrErr==0 + ){ + unsigned int pgno = (unsigned int)sqlite3_column_int64(pStmt,0); + const void *pContent = sqlite3_column_blob(pStmt, 1); + writeByte(p, ORIGIN_PAGE); + writeUint32(p, pgno); + writeBytes(p, szPg, pContent); + p->nPageSent++; + if( p->zDebugFile ){ + debugMessage(p, "-> ORIGIN_PAGE %u\n", pgno); + } + } + sqlite3_finalize(pStmt); + writeByte(p, ORIGIN_TXN); + writeUint32(p, nPage); + if( p->zDebugFile ){ + debugMessage(p, "-> ORIGIN_TXN %u\n", nPage); + } + writeByte(p, ORIGIN_END); + } + fflush(p->pOut); + break; + } + default: { + reportError(p, "Unknown message 0x%02x %lld bytes into conversation", + c, p->nIn); + break; + } + } + } + + if( pCkHash ) sqlite3_finalize(pCkHash); + if( pInsHash ) sqlite3_finalize(pInsHash); + closeDb(p); +} + +/* +** Send a REPLICA_HASH message for each entry in the sendHash table. +** The sendHash table looks like this: +** +** CREATE TABLE sendHash( +** fpg INTEGER PRIMARY KEY, -- Page number of the hash +** npg INT -- Number of pages in this hash +** ); +** +** If iHash is page number for the next page that the origin will +** be expecting, and nHash is the number of pages that the origin will +** be expecting in the hash that follows. Send a REPLICA_CONFIG message +** if either of these values if not correct. +*/ +static void sendHashMessages( + SQLiteRsync *p, /* The replica-side of the sync */ + unsigned int iHash, /* Next page expected by origin */ + unsigned int nHash /* Next number of pages expected by origin */ +){ + sqlite3_stmt *pStmt; + pStmt = prepareStmt(p, + "SELECT if(npg==1," + " (SELECT hash(data) FROM sqlite_dbpage('replica') WHERE pgno=fpg)," + " (WITH RECURSIVE c(n) AS" + " (SELECT fpg UNION ALL SELECT n+1 FROM c WHERE n<fpg+npg-1)" + " SELECT agghash(hash(data))" + " FROM c CROSS JOIN sqlite_dbpage('replica') ON pgno=n)) AS hash," + " fpg," + " npg" + " FROM sendHash ORDER BY fpg" + ); + while( sqlite3_step(pStmt)==SQLITE_ROW && p->nErr==0 && p->nWrErr==0 ){ + const unsigned char *a = sqlite3_column_blob(pStmt, 0); + unsigned int pgno = (unsigned int)sqlite3_column_int64(pStmt, 1); + unsigned int npg = (unsigned int)sqlite3_column_int64(pStmt, 2); + if( pgno!=iHash || npg!=nHash ){ + writeByte(p, REPLICA_CONFIG); + writeUint32(p, pgno); + writeUint32(p, npg); + if( p->zDebugFile ){ + debugMessage(p, "-> REPLICA_CONFIG %u %u\n", pgno, npg); + } + } + if( a==0 ){ + if( p->zDebugFile ){ + debugMessage(p, "# Oops: No hash for %u %u\n", pgno, npg); + } + }else{ + writeByte(p, REPLICA_HASH); + writeBytes(p, 20, a); + if( p->zDebugFile ){ + debugMessage(p, "-> REPLICA_HASH %u %u (%08x...)\n", + pgno, npg, *(unsigned int*)a); + } + } + p->nHashSent++; + iHash = pgno + npg; + nHash = npg; + } + sqlite3_finalize(pStmt); + runSql(p, "DELETE FROM sendHash"); + writeByte(p, REPLICA_READY); + fflush(p->pOut); + p->nRound++; + if( p->zDebugFile ) debugMessage(p, "-> REPLICA_READY\n", iHash); +} + +/* +** Make entries in the sendHash table to send hashes for +** npg (mnemonic: Number of PaGes) pages starting with fpg +** (mnemonic: First PaGe). +*/ +static void subdivideHashRange( + SQLiteRsync *p, /* The replica-side of the sync */ + unsigned int fpg, /* First page of the range */ + unsigned int npg /* Number of pages */ +){ + unsigned int nChunk; /* How many pages to request per hash */ + sqlite3_uint64 iEnd; /* One more than the last page */ + if( npg<=30 ){ + nChunk = 1; + }else if( npg<=1000 ){ + nChunk = 30; + }else{ + nChunk = 1000; + } + iEnd = fpg; + iEnd += npg; + runSql(p, + "WITH RECURSIVE c(n) AS" + " (VALUES(%u) UNION ALL SELECT n+%u FROM c WHERE n<%llu)" + "REPLACE INTO sendHash(fpg,npg)" + " SELECT n, min(%llu-n,%u) FROM c", + fpg, nChunk, iEnd-nChunk, iEnd, nChunk + ); +} + +/* +** Run the replica-side protocol. The protocol is passive in the sense +** that it only response to message from the origin side. +** +** ORIGIN_BEGIN idProtocol szPage nPage +** +** The origin is reporting the protocol version number, the size of +** each page in the origin database (sent as a single-byte power-of-2), +** and the number of pages in the origin database. +** This procedure checks compatibility, and if everything is ok, +** it starts sending hashes back to the origin using REPLICA_HASH +** and/or REPLICA_CONFIG message, followed by a single REPLICA_READY. +** REPLICA_CONFIG is only sent if the protocol is 2 or greater. +** +** ORIGIN_ERROR size text +** +** Report an error and quit. +** +** ORIGIN_DETAIL pgno cnt +** +** The origin reports that a multi-page hash starting at pgno and +** spanning cnt pages failed to match. The origin is requesting +** details (more REPLICA_HASH message with a smaller cnt). The +** replica must wait on ORIGIN_READY before sending its reply. +** +** ORIGIN_READY +** +** After sending one or more ORIGIN_DETAIL messages, the ORIGIN_READY +** is sent by the origin to indicate that it has finished sending +** requests for detail and is ready for the replicate to reply +** with a new round of REPLICA_CONFIG and REPLICA_HASH messages. +** +** ORIGIN_PAGE pgno content +** +** Once the origin believes it knows exactly which pages need to be +** updated in the replica, it starts sending those pages using these +** messages. These messages will only appear immediately after +** REPLICA_READY. The origin never mixes ORIGIN_DETAIL and +** ORIGIN_PAGE messages in the same batch. +** +** ORIGIN_TXN pgno +** +** Close the update transaction. The total database size is pgno +** pages. +** +** ORIGIN_END +** +** Expect no more transmissions from the origin. +*/ +static void replicaSide(SQLiteRsync *p){ + int c; + sqlite3_stmt *pIns = 0; + unsigned int szOPage = 0; + char eJMode = 0; /* Journal mode prior to sync */ + char buf[65536]; + + p->isReplica = 1; + if( p->bCommCheck ){ + infoMsg(p, "replica zOrigin=%Q zReplica=%Q isRemote=%d protocol=%d", + p->zOrigin, p->zReplica, p->isRemote, p->iProtocol); + writeByte(p, REPLICA_END); + fflush(p->pOut); + } + if( p->iProtocol<=0 ) p->iProtocol = PROTOCOL_VERSION; + + /* Respond to message from the origin. The origin will initiate the + ** the conversation with an ORIGIN_BEGIN message. + */ + while( p->nErr<=p->nWrErr && (c = readByte(p))!=EOF && c!=ORIGIN_END ){ + switch( c ){ + case ORIGIN_MSG: + case ORIGIN_ERROR: { + readAndDisplayMessage(p, c); + break; + } + case ORIGIN_BEGIN: { + unsigned int nOPage = 0; + unsigned int nRPage = 0, szRPage = 0; + int rc = 0; + u8 iProtocol; + + closeDb(p); + iProtocol = readByte(p); + szOPage = readPow2(p); + readUint32(p, &nOPage); + if( p->zDebugFile ){ + debugMessage(p, "<- ORIGIN_BEGIN %d %d %u\n", iProtocol, szOPage, + nOPage); + } + if( p->nErr ) break; + if( iProtocol>p->iProtocol ){ + /* If the protocol version on the origin side is larger, send back + ** a REPLICA_BEGIN message with the protocol version number of the + ** replica side. This gives the origin an opportunity to resend + ** a new ORIGIN_BEGIN with a reduced protocol version. */ + writeByte(p, REPLICA_BEGIN); + writeByte(p, p->iProtocol); + fflush(p->pOut); + if( p->zDebugFile ){ + debugMessage(p, "-> REPLICA_BEGIN %u\n", p->iProtocol); + } + break; + } + p->iProtocol = iProtocol; + p->nPage = nOPage; + p->szPage = szOPage; + rc = sqlite3_open(":memory:", &p->db); + if( rc ){ + reportError(p, "cannot open in-memory database: %s", + sqlite3_errmsg(p->db)); + closeDb(p); + break; + } + sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0); + runSql(p, "ATTACH %Q AS 'replica'", p->zReplica); + if( p->wrongEncoding ){ + p->wrongEncoding = 0; + runSql(p, "PRAGMA encoding=utf16le"); + runSql(p, "ATTACH %Q AS 'replica'", p->zReplica); + if( p->wrongEncoding ){ + p->wrongEncoding = 0; + runSql(p, "PRAGMA encoding=utf16be"); + runSql(p, "Attach %Q AS 'replica'", p->zReplica); + } + } + if( p->nErr ){ + closeDb(p); + break; + } + runSql(p, + "CREATE TABLE sendHash(" + " fpg INTEGER PRIMARY KEY," /* The page number of hash to send */ + " npg INT" /* Number of pages in this hash */ + ")" + ); + hashRegister(p->db); + if( runSqlReturnUInt(p, &nRPage, "PRAGMA replica.page_count") ){ + break; + } + if( nRPage==0 ){ + runSql(p, "PRAGMA replica.page_size=%u", szOPage); + runSql(p, "SELECT * FROM replica.sqlite_schema"); + } + runSql(p, "BEGIN IMMEDIATE"); + runSqlReturnText(p, buf, "PRAGMA replica.journal_mode"); + if( strcmp(buf, "wal")!=0 ){ + if( p->bWalOnly && nRPage>0 ){ + reportError(p, "replica is not in WAL mode"); + break; + } + eJMode = 1; /* Non-WAL mode prior to sync */ + }else{ + eJMode = 2; /* WAL-mode prior to sync */ + } + runSqlReturnUInt(p, &nRPage, "PRAGMA replica.page_count"); + runSqlReturnUInt(p, &szRPage, "PRAGMA replica.page_size"); + if( szRPage!=szOPage ){ + reportError(p, "page size mismatch; origin is %d bytes and " + "replica is %d bytes", szOPage, szRPage); + break; + } + if( p->iProtocol<2 || nRPage<=100 ){ + runSql(p, + "WITH RECURSIVE c(n) AS" + "(VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<%d)" + "INSERT INTO sendHash(fpg, npg) SELECT n, 1 FROM c", + nRPage); + }else{ + runSql(p,"INSERT INTO sendHash VALUES(1,1)"); + subdivideHashRange(p, 2, nRPage-1); + } + sendHashMessages(p, 1, 1); + runSql(p, "PRAGMA writable_schema=ON"); + break; + } + case ORIGIN_DETAIL: { + unsigned int fpg, npg; + readUint32(p, &fpg); + readUint32(p, &npg); + if( p->zDebugFile ){ + debugMessage(p, "<- ORIGIN_DETAIL %u %u\n", fpg, npg); + } + subdivideHashRange(p, fpg, npg); + break; + } + case ORIGIN_READY: { + if( p->zDebugFile ){ + debugMessage(p, "<- ORIGIN_READY\n"); + } + sendHashMessages(p, 0, 0); + break; + } + case ORIGIN_TXN: { + unsigned int nOPage = 0; + readUint32(p, &nOPage); + if( p->zDebugFile ){ + debugMessage(p, "<- ORIGIN_TXN %u\n", nOPage); + } + if( pIns==0 ){ + /* Nothing has changed */ + runSql(p, "COMMIT"); + }else if( p->nErr ){ + runSql(p, "ROLLBACK"); + }else{ + if( nOPage<0xffffffff ){ + int rc; + sqlite3_bind_int64(pIns, 1, nOPage+1); + sqlite3_bind_null(pIns, 2); + rc = sqlite3_step(pIns); + if( rc!=SQLITE_DONE ){ + reportError(p, + "SQL statement [%s] failed (pgno=%u, data=NULL): %s", + sqlite3_sql(pIns), nOPage, sqlite3_errmsg(p->db)); + } + sqlite3_reset(pIns); + } + p->nPage = nOPage; + runSql(p, "COMMIT"); + } + break; + } + case ORIGIN_PAGE: { + unsigned int pgno = 0; + int rc; + readUint32(p, &pgno); + if( p->zDebugFile ){ + debugMessage(p, "<- ORIGIN_PAGE %u\n", pgno); + } + if( p->nErr ) break; + if( pIns==0 ){ + pIns = prepareStmt(p, + "INSERT INTO sqlite_dbpage(pgno,data,schema)VALUES(?1,?2,'replica')" + ); + if( pIns==0 ) break; + } + readBytes(p, szOPage, buf); + if( p->nErr ) break; + if( pgno==1 && eJMode==2 && buf[18]==1 ){ + /* Do not switch the replica out of WAL mode if it started in + ** WAL mode */ + buf[18] = buf[19] = 2; + } + p->nPageSent++; + sqlite3_bind_int64(pIns, 1, pgno); + sqlite3_bind_blob(pIns, 2, buf, szOPage, SQLITE_STATIC); + rc = sqlite3_step(pIns); + if( rc!=SQLITE_DONE ){ + reportError(p, "SQL statement [%s] failed (pgno=%u): %s", + sqlite3_sql(pIns), pgno, sqlite3_errmsg(p->db)); + } + sqlite3_reset(pIns); + break; + } + default: { + reportError(p, "Unknown message 0x%02x %lld bytes into conversation", + c, p->nIn); + break; + } + } + } + + if( pIns ) sqlite3_finalize(pIns); + closeDb(p); +} + +/* +** The argument might be -vvv...vv with any number of "v"s. Return +** the number of "v"s. Return 0 if the argument is not a -vvv...v. +*/ +static int numVs(const char *z){ + int n = 0; + if( z[0]!='-' ) return 0; + z++; + if( z[0]=='-' ) z++; + while( z[0]=='v' ){ n++; z++; } + if( z[0]==0 ) return n; + return 0; +} + +/* +** Get the argument to an --option. Throw an error and die if no argument +** is available. +*/ +static const char *cmdline_option_value(int argc, const char * const*argv, + int i){ + if( i==argc ){ + fprintf(stderr,"%s: Error: missing argument to %s\n", + argv[0], argv[argc-1]); + exit(1); + } + return argv[i]; +} + +/* +** Return the current time in milliseconds since the Julian epoch. +*/ +sqlite3_int64 currentTime(void){ + sqlite3_int64 now = 0; + sqlite3_vfs *pVfs = sqlite3_vfs_find(0); + if( pVfs && pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64!=0 ){ + pVfs->xCurrentTimeInt64(pVfs, &now); + } + return now; +} + +/* +** Input string zIn might be in any of these formats: +** +** (1) PATH +** (2) HOST:PATH +** (3) USER@HOST:PATH +** +** For format 1, return NULL. For formats 2 and 3, return +** a pointer to the ':' character that separates the hostname +** from the path. +*/ +static char *hostSeparator(const char *zIn){ + char *zPath = strchr(zIn, ':'); + if( zPath==0 ) return 0; +#ifdef _WIN32 + if( isalpha(zIn[0]) && zIn[1]==':' && (zIn[2]=='/' || zIn[2]=='\\') ){ + return 0; + } +#endif + while( zIn<zPath ){ + if( zIn[0]=='/' ) return 0; + if( zIn[0]=='\\' ) return 0; + zIn++; + } + return zPath; +} + + +/* +** Parse command-line arguments. Dispatch subroutines to do the +** requested work. +** +** Input formats: +** +** (1) sqlite3_rsync FILENAME1 USER@HOST:FILENAME2 +** +** (2) sqlite3_rsync USER@HOST:FILENAME1 FILENAME2 +** +** (3) sqlite3_rsync --origin FILENAME1 +** +** (4) sqlite3_rsync --replica FILENAME2 +** +** The user types (1) or (2). SSH launches (3) or (4). +** +** If (1) is seen then popen2 is used launch (4) on the remote and +** originSide() is called locally. +** +** If (2) is seen, then popen2() is used to launch (3) on the remote +** and replicaSide() is run locally. +** +** If (3) is seen, call originSide() on stdin and stdout. +** +q** If (4) is seen, call replicaSide() on stdin and stdout. +*/ +int main(int argc, char const * const *argv){ + int isOrigin = 0; + int isReplica = 0; + int i; + SQLiteRsync ctx; + char *zDiv; + FILE *pIn = 0; + FILE *pOut = 0; + int childPid = 0; + const char *zSsh = "ssh"; + const char *zExe = "sqlite3_rsync"; + char *zCmd = 0; + sqlite3_int64 tmStart; + sqlite3_int64 tmEnd; + sqlite3_int64 tmElapse; + const char *zRemoteErrFile = 0; + const char *zRemoteDebugFile = 0; + +#define cli_opt_val cmdline_option_value(argc, argv, ++i) + memset(&ctx, 0, sizeof(ctx)); + ctx.iProtocol = PROTOCOL_VERSION; + sqlite3_initialize(); + for(i=1; i<argc; i++){ + const char *z = argv[i]; + if( z[0]=='-' && z[1]=='-' && z[2]!=0 ) z++; + if( strcmp(z,"-origin")==0 ){ + isOrigin = 1; + continue; + } + if( strcmp(z,"-replica")==0 ){ + isReplica = 1; + continue; + } + if( numVs(z) ){ + ctx.eVerbose += numVs(z); + continue; + } + if( strcmp(z, "-protocol")==0 ){ + ctx.iProtocol = atoi(cli_opt_val); + if( ctx.iProtocol<1 ){ + ctx.iProtocol = 1; + }else if( ctx.iProtocol>PROTOCOL_VERSION ){ + ctx.iProtocol = PROTOCOL_VERSION; + } + continue; + } + if( strcmp(z, "-ssh")==0 ){ + zSsh = cli_opt_val; + continue; + } + if( strcmp(z, "-exe")==0 ){ + zExe = cli_opt_val; + continue; + } + if( strcmp(z, "-wal-only")==0 ){ + ctx.bWalOnly = 1; + continue; + } + if( strcmp(z, "-version")==0 ){ + printf("%s\n", sqlite3_sourceid()); + return 0; + } + if( strcmp(z, "-help")==0 || strcmp(z, "--help")==0 + || strcmp(z, "-?")==0 + ){ + printf("%s", zUsage); + return 0; + } + if( strcmp(z, "-logfile")==0 ){ + /* DEBUG OPTION: --logfile FILENAME + ** Cause all local output traffic to be duplicated in FILENAME */ + const char *zLog = cli_opt_val; + if( ctx.pLog ) fclose(ctx.pLog); + ctx.pLog = fopen(zLog, "wb"); + if( ctx.pLog==0 ){ + fprintf(stderr, "cannot open \"%s\" for writing\n", argv[i]); + return 1; + } + continue; + } + if( strcmp(z, "-errorfile")==0 ){ + /* DEBUG OPTION: --errorfile FILENAME + ** Error messages on the local side are written into FILENAME */ + ctx.zErrFile = cli_opt_val; + continue; + } + if( strcmp(z, "-remote-errorfile")==0 ){ + /* DEBUG OPTION: --remote-errorfile FILENAME + ** Error messages on the remote side are written into FILENAME on + ** the remote side. */ + zRemoteErrFile = cli_opt_val; + continue; + } + if( strcmp(z, "-debugfile")==0 ){ + /* DEBUG OPTION: --debugfile FILENAME + ** Debugging messages on the local side are written into FILENAME */ + ctx.zDebugFile = cli_opt_val; + continue; + } + if( strcmp(z, "-remote-debugfile")==0 ){ + /* DEBUG OPTION: --remote-debugfile FILENAME + ** Error messages on the remote side are written into FILENAME on + ** the remote side. */ + zRemoteDebugFile = cli_opt_val; + continue; + } + if( strcmp(z,"-commcheck")==0 ){ /* DEBUG ONLY */ + /* Run a communication check with the remote side. Do not attempt + ** to exchange any database connection */ + ctx.bCommCheck = 1; + continue; + } + if( strcmp(z,"-arg-escape-check")==0 ){ /* DEBUG ONLY */ + /* Test the append_escaped_arg() routine by using it to render a + ** copy of the input command-line, assuming all arguments except + ** this one are filenames. */ + sqlite3_str *pStr = sqlite3_str_new(0); + int k; + for(k=0; k<argc; k++){ + append_escaped_arg(pStr, argv[k], i!=k); + } + printf("%s\n", sqlite3_str_value(pStr)); + return 0; + } + if( z[i]=='-' ){ + fprintf(stderr, + "unknown option: \"%s\". Use --help for more detail.\n", z); + return 1; + } + if( ctx.zOrigin==0 ){ + ctx.zOrigin = z; + }else if( ctx.zReplica==0 ){ + ctx.zReplica = z; + }else{ + fprintf(stderr, "Unknown argument: \"%s\"\n", z); + return 1; + } + } + if( ctx.zOrigin==0 ){ + fprintf(stderr, "missing ORIGIN database filename\n"); + return 1; + } + if( ctx.zReplica==0 ){ + fprintf(stderr, "missing REPLICA database filename\n"); + return 1; + } + if( isOrigin && isReplica ){ + fprintf(stderr, "bad option combination\n"); + return 1; + } + if( isOrigin ){ + ctx.pIn = stdin; + ctx.pOut = stdout; + ctx.isRemote = 1; +#ifdef _WIN32 + _setmode(_fileno(ctx.pIn), _O_BINARY); + _setmode(_fileno(ctx.pOut), _O_BINARY); +#endif + originSide(&ctx); + return 0; + } + if( isReplica ){ + ctx.pIn = stdin; + ctx.pOut = stdout; + ctx.isRemote = 1; +#ifdef _WIN32 + _setmode(_fileno(ctx.pIn), _O_BINARY); + _setmode(_fileno(ctx.pOut), _O_BINARY); +#endif + replicaSide(&ctx); + return 0; + } + if( ctx.zReplica==0 ){ + fprintf(stderr, "missing REPLICA database filename\n"); + return 1; + } + tmStart = currentTime(); + zDiv = hostSeparator(ctx.zOrigin); + if( zDiv ){ + int iRetry; + if( hostSeparator(ctx.zReplica)!=0 ){ + fprintf(stderr, + "At least one of ORIGIN and REPLICA must be a local database\n" + "You provided two remote databases.\n"); + return 1; + } + *(zDiv++) = 0; + /* Remote ORIGIN and local REPLICA */ + for(iRetry=0; 1 /*exit-via-break*/; iRetry++){ + sqlite3_str *pStr = sqlite3_str_new(0); + append_escaped_arg(pStr, zSsh, 1); + sqlite3_str_appendf(pStr, " -e none"); + append_escaped_arg(pStr, ctx.zOrigin, 0); + if( iRetry ) add_path_argument(pStr); + append_escaped_arg(pStr, zExe, 1); + append_escaped_arg(pStr, "--origin", 0); + if( ctx.bCommCheck ){ + append_escaped_arg(pStr, "--commcheck", 0); + if( ctx.eVerbose==0 ) ctx.eVerbose = 1; + } + if( zRemoteErrFile ){ + append_escaped_arg(pStr, "--errorfile", 0); + append_escaped_arg(pStr, zRemoteErrFile, 1); + } + if( zRemoteDebugFile ){ + append_escaped_arg(pStr, "--debugfile", 0); + append_escaped_arg(pStr, zRemoteDebugFile, 1); + } + if( ctx.bWalOnly ){ + append_escaped_arg(pStr, "--wal-only", 0); + } + append_escaped_arg(pStr, zDiv, 1); + append_escaped_arg(pStr, file_tail(ctx.zReplica), 1); + if( ctx.eVerbose<2 && iRetry==0 ){ + append_escaped_arg(pStr, "2>/dev/null", 0); + } + zCmd = sqlite3_str_finish(pStr); + if( ctx.eVerbose>=2 ) printf("%s\n", zCmd); + if( popen2(zCmd, &ctx.pIn, &ctx.pOut, &childPid, 0) ){ + if( iRetry>=1 ){ + fprintf(stderr, "Could not start auxiliary process: %s\n", zCmd); + return 1; + } + if( ctx.eVerbose>=2 ){ + printf("ssh FAILED. Retry with a PATH= argument...\n"); + } + continue; + } + replicaSide(&ctx); + if( ctx.nHashSent==0 && iRetry==0 ) continue; + break; + } + }else if( (zDiv = hostSeparator(ctx.zReplica))!=0 ){ + /* Local ORIGIN and remote REPLICA */ + int iRetry; + *(zDiv++) = 0; + for(iRetry=0; 1 /*exit-by-break*/; iRetry++){ + sqlite3_str *pStr = sqlite3_str_new(0); + append_escaped_arg(pStr, zSsh, 1); + sqlite3_str_appendf(pStr, " -e none"); + append_escaped_arg(pStr, ctx.zReplica, 0); + if( iRetry==1 ) add_path_argument(pStr); + append_escaped_arg(pStr, zExe, 1); + append_escaped_arg(pStr, "--replica", 0); + if( ctx.bCommCheck ){ + append_escaped_arg(pStr, "--commcheck", 0); + if( ctx.eVerbose==0 ) ctx.eVerbose = 1; + } + if( zRemoteErrFile ){ + append_escaped_arg(pStr, "--errorfile", 0); + append_escaped_arg(pStr, zRemoteErrFile, 1); + } + if( zRemoteDebugFile ){ + append_escaped_arg(pStr, "--debugfile", 0); + append_escaped_arg(pStr, zRemoteDebugFile, 1); + } + if( ctx.bWalOnly ){ + append_escaped_arg(pStr, "--wal-only", 0); + } + append_escaped_arg(pStr, file_tail(ctx.zOrigin), 1); + append_escaped_arg(pStr, zDiv, 1); + if( ctx.eVerbose<2 && iRetry==0 ){ + append_escaped_arg(pStr, "2>/dev/null", 0); + } + zCmd = sqlite3_str_finish(pStr); + if( ctx.eVerbose>=2 ) printf("%s\n", zCmd); + if( popen2(zCmd, &ctx.pIn, &ctx.pOut, &childPid, 0) ){ + if( iRetry>=1 ){ + fprintf(stderr, "Could not start auxiliary process: %s\n", zCmd); + return 1; + }else if( ctx.eVerbose>=2 ){ + printf("ssh FAILED. Retry with a PATH= argument...\n"); + } + continue; + } + originSide(&ctx); + if( ctx.nHashSent==0 && iRetry==0 ) continue; + break; + } + }else{ + /* Local ORIGIN and REPLICA */ + sqlite3_str *pStr = sqlite3_str_new(0); + append_escaped_arg(pStr, argv[0], 1); + append_escaped_arg(pStr, "--replica", 0); + if( ctx.bCommCheck ){ + append_escaped_arg(pStr, "--commcheck", 0); + } + if( zRemoteErrFile ){ + append_escaped_arg(pStr, "--errorfile", 0); + append_escaped_arg(pStr, zRemoteErrFile, 1); + } + if( zRemoteDebugFile ){ + append_escaped_arg(pStr, "--debugfile", 0); + append_escaped_arg(pStr, zRemoteDebugFile, 1); + } + append_escaped_arg(pStr, ctx.zOrigin, 1); + append_escaped_arg(pStr, ctx.zReplica, 1); + zCmd = sqlite3_str_finish(pStr); + if( ctx.eVerbose>=2 ) printf("%s\n", zCmd); + if( popen2(zCmd, &ctx.pIn, &ctx.pOut, &childPid, 0) ){ + fprintf(stderr, "Could not start auxiliary process: %s\n", zCmd); + return 1; + } + originSide(&ctx); + } + pclose2(ctx.pIn, ctx.pOut, childPid); + if( ctx.pLog ) fclose(ctx.pLog); + tmEnd = currentTime(); + tmElapse = tmEnd - tmStart; /* Elapse time in milliseconds */ + if( ctx.nErr ){ + printf("Databases were not synced due to errors\n"); + } + if( ctx.eVerbose>=1 ){ + char *zMsg; + sqlite3_int64 szTotal = (sqlite3_int64)ctx.nPage*(sqlite3_int64)ctx.szPage; + sqlite3_int64 nIO = ctx.nOut +ctx.nIn; + zMsg = sqlite3_mprintf("sent %,lld bytes, received %,lld bytes", + ctx.nOut, ctx.nIn); + printf("%s", zMsg); + sqlite3_free(zMsg); + if( tmElapse>0 ){ + zMsg = sqlite3_mprintf(", %,.2f bytes/sec", + 1000.0*(double)nIO/(double)tmElapse); + printf("%s\n", zMsg); + sqlite3_free(zMsg); + }else{ + printf("\n"); + } + if( ctx.nErr==0 ){ + if( nIO<=szTotal && nIO>0 ){ + zMsg = sqlite3_mprintf("total size %,lld speedup is %.2f", + szTotal, (double)szTotal/(double)nIO); + }else{ + zMsg = sqlite3_mprintf("total size %,lld", szTotal); + } + printf("%s\n", zMsg); + sqlite3_free(zMsg); + } + if( ctx.eVerbose>=3 ){ + printf("hashes: %lld hash-rounds: %d" + " page updates: %d protocol: %d\n", + ctx.nHashSent, ctx.nRound, ctx.nPageSent, ctx.iProtocol); + } + } + sqlite3_free(zCmd); + if( pIn!=0 && pOut!=0 ){ + pclose2(pIn, pOut, childPid); + } + sqlite3_shutdown(); + return ctx.nErr; +} diff --git a/tool/sqltclsh.c.in b/tool/sqltclsh.c.in new file mode 100644 index 0000000000..da354ee935 --- /dev/null +++ b/tool/sqltclsh.c.in @@ -0,0 +1,51 @@ +/* +** This is the source code to a "tclsh" that has SQLite built-in. +** +** The startup script is located as follows: +** +** (1) Open the executable as an appended SQLite database and try to +** read the startup script out of that database. +** +** (2) If the first argument is a readable file, try to open that file +** as an SQLite database and read the startup script out of that +** database. +** +** (3) If the first argument is a readable file with a ".tcl" extension, +** then try to run that script directly. +** +** If none of the above steps work, then the program runs as an interactive +** tclsh. +*/ +#define TCLSH_INIT_PROC sqlite3_tclapp_init_proc +#define SQLITE_ENABLE_DBSTAT_VTAB 1 +#undef SQLITE_THREADSAFE +#define SQLITE_THREADSAFE 0 +#undef SQLITE_ENABLE_COLUMN_METADATA +#define SQLITE_OMIT_DECLTYPE 1 +#define SQLITE_OMIT_DEPRECATED 1 +#define SQLITE_OMIT_PROGRESS_CALLBACK 1 +#define SQLITE_OMIT_SHARED_CACHE 1 +#define SQLITE_DEFAULT_MEMSTATUS 0 +#define SQLITE_MAX_EXPR_DEPTH 0 +INCLUDE sqlite3.c +INCLUDE $ROOT/ext/misc/appendvfs.c +#ifdef SQLITE_HAVE_ZLIB +INCLUDE $ROOT/ext/misc/zipfile.c +INCLUDE $ROOT/ext/misc/sqlar.c +#endif +INCLUDE $ROOT/src/tclsqlite.c + +const char *sqlite3_tclapp_init_proc(Tcl_Interp *interp){ + (void)interp; + sqlite3_appendvfs_init(0,0,0); +#ifdef SQLITE_HAVE_ZLIB + sqlite3_auto_extension((void(*)(void))sqlite3_sqlar_init); + sqlite3_auto_extension((void(*)(void))sqlite3_zipfile_init); +#endif + + return +BEGIN_STRING +INCLUDE $ROOT/tool/sqltclsh.tcl +END_STRING +; +} diff --git a/tool/sqltclsh.tcl b/tool/sqltclsh.tcl new file mode 100644 index 0000000000..6a4b1fe1f0 --- /dev/null +++ b/tool/sqltclsh.tcl @@ -0,0 +1,71 @@ +# Try to open the executable as a database and read the "scripts.data" +# field where "scripts.name" is 'main.tcl' +# +catch { + if {![file exists $argv0] && [file exists $argv0.exe]} { + append argv0 .exe + } + sqlite3 db $argv0 -vfs apndvfs -create 0 + set mainscript [db one { + SELECT sqlar_uncompress(data,sz) FROM sqlar WHERE name='main.tcl' + }] +} +if {[info exists mainscript]} { + eval $mainscript + return +} else { + catch {db close} +} + +# Try to open file named in the first argument as a database and +# read the "scripts.data" field where "scripts.name" is 'main.tcl' +# +if {[llength $argv]>0 && [file readable [lindex $argv 0]]} { + catch { + sqlite3 db [lindex $argv 0] -vfs apndvfs -create 0 + set mainscript [db one {SELECT data FROM scripts WHERE name='main.tcl'}] + set argv0 [lindex $argv 0] + set argv [lrange $argv 1 end] + } + if {[info exists mainscript]} { + eval $mainscript + return + } else { + catch {db close} + } + if {[string match *.tcl [lindex $argv 0]]} { + set fd [open [lindex $argv 0] rb] + set mainscript [read $fd] + close $fd + unset fd + set argv0 [lindex $argv 0] + set argv [lrange $argv 1 end] + } + if {[info exists mainscript]} { + eval $mainscript + return + } +} + +# If all else fails, do an interactive loop +# +set line {} +while {![eof stdin]} { + if {$line!=""} { + puts -nonewline "> " + } else { + puts -nonewline "% " + } + flush stdout + append line [gets stdin] + if {[info complete $line]} { + if {[catch {uplevel #0 $line} result]} { + puts stderr "Error: $result" + } elseif {$result!=""} { + puts $result + } + set line {} + } else { + append line \\n" + } +} diff --git a/tool/src-verify.c b/tool/src-verify.c new file mode 100644 index 0000000000..6dc9f72598 --- /dev/null +++ b/tool/src-verify.c @@ -0,0 +1,960 @@ +/* +** This utility program reads the "manifest" and "manifest.uuid" files +** in a Fossil-generated source tree (where the repository has the +** "manifest" setting turned on - this is true for SQLite and Fossil itself) +** and verifies that the source code files are complete and unaltered by +** checking the SHA1 and SHA3 hashes of the source files contained in the +** "manifest" file. +** +** On success it prints: "OK $HASH" where $HASH is the SHA3-256 hash of +** the check-in for the source tree. If it finds any discrepencies, it +** prints "Derived from $HASH with changes to:" followed by a list of files +** which have been altered. +** +** USAGE: +** +** src-verify [-x] [-v] $(ROOT) +** +** Where ROOT is the root of the source tree - the directory that contains +** the "manifest" and "manifest.uuid" files. Add the "-v" option for +** some debugging output. With the -x option, the output is in a format +** that is intended to be read by a script rather by a human. The -x output +** format always has the SHA3 hash of the source check-in on the first line +** and lists files that have changed on subsequent lines. +** +** Additional debugging options: +** +** src-verify --sha1 FILE ... +** src-verify --sha3 FILE ... +** +** Compute the SHA1 or SHA3-256 hashes for all of the FILEs named +** +** COMPILING: +** +** This utility is self-contained. It uses only the standard library. +** There are no other dependencies. Just compile it and run it. +** +** LIMITATIONS: +** +** * This utility assumes that the check-in hash uses SHA3-256. +** It is ok for individual file hashes to be SHA1, but the +** check-in itself must use a SHA3-256 hash. +*/ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#if !defined(_WIN32) +# include <unistd.h> +#else +# include <io.h> +# ifndef R_OK +# define R_OK 04 +# endif +# ifndef access +# define access(f,m) _access((f),(m)) +# endif +#endif +typedef unsigned long long int u64; + +/* +** The SHA1 implementation below is adapted from: +** +** $NetBSD: sha1.c,v 1.6 2009/11/06 20:31:18 joerg Exp $ +** $OpenBSD: sha1.c,v 1.9 1997/07/23 21:12:32 kstailey Exp $ +** +** SHA-1 in C +** By Steve Reid <steve@edmweb.com> +** 100% Public Domain +*/ +typedef struct SHA1Context SHA1Context; +struct SHA1Context { + unsigned int state[5]; + unsigned int count[2]; + unsigned char buffer[64]; +}; + +/* + * blk0() and blk() perform the initial expand. + * I got the idea of expanding during the round function from SSLeay + * + * blk0le() for little-endian and blk0be() for big-endian. + */ +#define SHA_ROT(x,l,r) ((x) << (l) | (x) >> (r)) +#define rol(x,k) SHA_ROT(x,k,32-(k)) +#define ror(x,k) SHA_ROT(x,32-(k),k) +#define blk0le(i) (block[i] = (ror(block[i],8)&0xFF00FF00) \ + |(rol(block[i],8)&0x00FF00FF)) +#define blk0be(i) block[i] +#define blk(i) (block[i&15] = rol(block[(i+13)&15]^block[(i+8)&15] \ + ^block[(i+2)&15]^block[i&15],1)) + +/* + * (R0+R1), R2, R3, R4 are the different operations (rounds) used in SHA1 + * + * Rl0() for little-endian and Rb0() for big-endian. Endianness is + * determined at run-time. + */ +#define Rl0(v,w,x,y,z,i) \ + z+=((w&(x^y))^y)+blk0le(i)+0x5A827999+rol(v,5);w=ror(w,2); +#define Rb0(v,w,x,y,z,i) \ + z+=((w&(x^y))^y)+blk0be(i)+0x5A827999+rol(v,5);w=ror(w,2); +#define R1(v,w,x,y,z,i) \ + z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=ror(w,2); +#define R2(v,w,x,y,z,i) \ + z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=ror(w,2); +#define R3(v,w,x,y,z,i) \ + z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=ror(w,2); +#define R4(v,w,x,y,z,i) \ + z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=ror(w,2); + +/* + * Hash a single 512-bit block. This is the core of the algorithm. + */ +#define a qq[0] +#define b qq[1] +#define c qq[2] +#define d qq[3] +#define e qq[4] + +void SHA1Transform(unsigned int state[5], const unsigned char buffer[64]) +{ + unsigned int qq[5]; /* a, b, c, d, e; */ + static int one = 1; + unsigned int block[16]; + memcpy(block, buffer, 64); + memcpy(qq,state,5*sizeof(unsigned int)); + + /* Copy context->state[] to working vars */ + /* + a = state[0]; + b = state[1]; + c = state[2]; + d = state[3]; + e = state[4]; + */ + + /* 4 rounds of 20 operations each. Loop unrolled. */ + if( 1 == *(unsigned char*)&one ){ + Rl0(a,b,c,d,e, 0); Rl0(e,a,b,c,d, 1); Rl0(d,e,a,b,c, 2); Rl0(c,d,e,a,b, 3); + Rl0(b,c,d,e,a, 4); Rl0(a,b,c,d,e, 5); Rl0(e,a,b,c,d, 6); Rl0(d,e,a,b,c, 7); + Rl0(c,d,e,a,b, 8); Rl0(b,c,d,e,a, 9); Rl0(a,b,c,d,e,10); Rl0(e,a,b,c,d,11); + Rl0(d,e,a,b,c,12); Rl0(c,d,e,a,b,13); Rl0(b,c,d,e,a,14); Rl0(a,b,c,d,e,15); + }else{ + Rb0(a,b,c,d,e, 0); Rb0(e,a,b,c,d, 1); Rb0(d,e,a,b,c, 2); Rb0(c,d,e,a,b, 3); + Rb0(b,c,d,e,a, 4); Rb0(a,b,c,d,e, 5); Rb0(e,a,b,c,d, 6); Rb0(d,e,a,b,c, 7); + Rb0(c,d,e,a,b, 8); Rb0(b,c,d,e,a, 9); Rb0(a,b,c,d,e,10); Rb0(e,a,b,c,d,11); + Rb0(d,e,a,b,c,12); Rb0(c,d,e,a,b,13); Rb0(b,c,d,e,a,14); Rb0(a,b,c,d,e,15); + } + R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19); + R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23); + R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27); + R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31); + R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35); + R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39); + R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43); + R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47); + R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51); + R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55); + R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59); + R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63); + R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67); + R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71); + R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75); + R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79); + + /* Add the working vars back into context.state[] */ + state[0] += a; + state[1] += b; + state[2] += c; + state[3] += d; + state[4] += e; +} + +/* + * SHA1Init - Initialize new context + */ +static void SHA1Init(SHA1Context *context){ + /* SHA1 initialization constants */ + context->state[0] = 0x67452301; + context->state[1] = 0xEFCDAB89; + context->state[2] = 0x98BADCFE; + context->state[3] = 0x10325476; + context->state[4] = 0xC3D2E1F0; + context->count[0] = context->count[1] = 0; +} + + +/* + * Run your data through this. + */ +static void SHA1Update( + SHA1Context *context, + const unsigned char *data, + unsigned int len +){ + unsigned int i, j; + + j = context->count[0]; + if ((context->count[0] += len << 3) < j) + context->count[1] += (len>>29)+1; + j = (j >> 3) & 63; + if ((j + len) > 63) { + (void)memcpy(&context->buffer[j], data, (i = 64-j)); + SHA1Transform(context->state, context->buffer); + for ( ; i + 63 < len; i += 64) + SHA1Transform(context->state, &data[i]); + j = 0; + } else { + i = 0; + } + (void)memcpy(&context->buffer[j], &data[i], len - i); +} + + +/* + * Add padding and return the message digest. + */ +static void SHA1Final(unsigned char *digest, SHA1Context *context){ + unsigned int i; + unsigned char finalcount[8]; + + for (i = 0; i < 8; i++) { + finalcount[i] = (unsigned char)((context->count[(i >= 4 ? 0 : 1)] + >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */ + } + SHA1Update(context, (const unsigned char *)"\200", 1); + while ((context->count[0] & 504) != 448) + SHA1Update(context, (const unsigned char *)"\0", 1); + SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */ + + if (digest) { + for (i = 0; i < 20; i++) + digest[i] = (unsigned char) + ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255); + } +} + + +/* +** Macros to determine whether the machine is big or little endian, +** and whether or not that determination is run-time or compile-time. +** +** For best performance, an attempt is made to guess at the byte-order +** using C-preprocessor macros. If that is unsuccessful, or if +** -DSHA3_BYTEORDER=0 is set, then byte-order is determined +** at run-time. +*/ +#ifndef SHA3_BYTEORDER +# if defined(i386) || defined(__i386__) || defined(_M_IX86) || \ + defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \ + defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \ + defined(__arm__) +# define SHA3_BYTEORDER 1234 +# elif defined(sparc) || defined(__ppc__) +# define SHA3_BYTEORDER 4321 +# else +# define SHA3_BYTEORDER 0 +# endif +#endif + + +/* +** State structure for a SHA3 hash in progress +*/ +typedef struct SHA3Context SHA3Context; +struct SHA3Context { + union { + u64 s[25]; /* Keccak state. 5x5 lines of 64 bits each */ + unsigned char x[1600]; /* ... or 1600 bytes */ + } u; + unsigned nRate; /* Bytes of input accepted per Keccak iteration */ + unsigned nLoaded; /* Input bytes loaded into u.x[] so far this cycle */ + unsigned ixMask; /* Insert next input into u.x[nLoaded^ixMask]. */ +}; + +/* +** A single step of the Keccak mixing function for a 1600-bit state +*/ +static void KeccakF1600Step(SHA3Context *p){ + int i; + u64 B0, B1, B2, B3, B4; + u64 C0, C1, C2, C3, C4; + u64 D0, D1, D2, D3, D4; + static const u64 RC[] = { + 0x0000000000000001ULL, 0x0000000000008082ULL, + 0x800000000000808aULL, 0x8000000080008000ULL, + 0x000000000000808bULL, 0x0000000080000001ULL, + 0x8000000080008081ULL, 0x8000000000008009ULL, + 0x000000000000008aULL, 0x0000000000000088ULL, + 0x0000000080008009ULL, 0x000000008000000aULL, + 0x000000008000808bULL, 0x800000000000008bULL, + 0x8000000000008089ULL, 0x8000000000008003ULL, + 0x8000000000008002ULL, 0x8000000000000080ULL, + 0x000000000000800aULL, 0x800000008000000aULL, + 0x8000000080008081ULL, 0x8000000000008080ULL, + 0x0000000080000001ULL, 0x8000000080008008ULL + }; +# define A00 (p->u.s[0]) +# define A01 (p->u.s[1]) +# define A02 (p->u.s[2]) +# define A03 (p->u.s[3]) +# define A04 (p->u.s[4]) +# define A10 (p->u.s[5]) +# define A11 (p->u.s[6]) +# define A12 (p->u.s[7]) +# define A13 (p->u.s[8]) +# define A14 (p->u.s[9]) +# define A20 (p->u.s[10]) +# define A21 (p->u.s[11]) +# define A22 (p->u.s[12]) +# define A23 (p->u.s[13]) +# define A24 (p->u.s[14]) +# define A30 (p->u.s[15]) +# define A31 (p->u.s[16]) +# define A32 (p->u.s[17]) +# define A33 (p->u.s[18]) +# define A34 (p->u.s[19]) +# define A40 (p->u.s[20]) +# define A41 (p->u.s[21]) +# define A42 (p->u.s[22]) +# define A43 (p->u.s[23]) +# define A44 (p->u.s[24]) +# define ROL64(a,x) ((a<<x)|(a>>(64-x))) + + for(i=0; i<24; i+=4){ + C0 = A00^A10^A20^A30^A40; + C1 = A01^A11^A21^A31^A41; + C2 = A02^A12^A22^A32^A42; + C3 = A03^A13^A23^A33^A43; + C4 = A04^A14^A24^A34^A44; + D0 = C4^ROL64(C1, 1); + D1 = C0^ROL64(C2, 1); + D2 = C1^ROL64(C3, 1); + D3 = C2^ROL64(C4, 1); + D4 = C3^ROL64(C0, 1); + + B0 = (A00^D0); + B1 = ROL64((A11^D1), 44); + B2 = ROL64((A22^D2), 43); + B3 = ROL64((A33^D3), 21); + B4 = ROL64((A44^D4), 14); + A00 = B0 ^((~B1)& B2 ); + A00 ^= RC[i]; + A11 = B1 ^((~B2)& B3 ); + A22 = B2 ^((~B3)& B4 ); + A33 = B3 ^((~B4)& B0 ); + A44 = B4 ^((~B0)& B1 ); + + B2 = ROL64((A20^D0), 3); + B3 = ROL64((A31^D1), 45); + B4 = ROL64((A42^D2), 61); + B0 = ROL64((A03^D3), 28); + B1 = ROL64((A14^D4), 20); + A20 = B0 ^((~B1)& B2 ); + A31 = B1 ^((~B2)& B3 ); + A42 = B2 ^((~B3)& B4 ); + A03 = B3 ^((~B4)& B0 ); + A14 = B4 ^((~B0)& B1 ); + + B4 = ROL64((A40^D0), 18); + B0 = ROL64((A01^D1), 1); + B1 = ROL64((A12^D2), 6); + B2 = ROL64((A23^D3), 25); + B3 = ROL64((A34^D4), 8); + A40 = B0 ^((~B1)& B2 ); + A01 = B1 ^((~B2)& B3 ); + A12 = B2 ^((~B3)& B4 ); + A23 = B3 ^((~B4)& B0 ); + A34 = B4 ^((~B0)& B1 ); + + B1 = ROL64((A10^D0), 36); + B2 = ROL64((A21^D1), 10); + B3 = ROL64((A32^D2), 15); + B4 = ROL64((A43^D3), 56); + B0 = ROL64((A04^D4), 27); + A10 = B0 ^((~B1)& B2 ); + A21 = B1 ^((~B2)& B3 ); + A32 = B2 ^((~B3)& B4 ); + A43 = B3 ^((~B4)& B0 ); + A04 = B4 ^((~B0)& B1 ); + + B3 = ROL64((A30^D0), 41); + B4 = ROL64((A41^D1), 2); + B0 = ROL64((A02^D2), 62); + B1 = ROL64((A13^D3), 55); + B2 = ROL64((A24^D4), 39); + A30 = B0 ^((~B1)& B2 ); + A41 = B1 ^((~B2)& B3 ); + A02 = B2 ^((~B3)& B4 ); + A13 = B3 ^((~B4)& B0 ); + A24 = B4 ^((~B0)& B1 ); + + C0 = A00^A20^A40^A10^A30; + C1 = A11^A31^A01^A21^A41; + C2 = A22^A42^A12^A32^A02; + C3 = A33^A03^A23^A43^A13; + C4 = A44^A14^A34^A04^A24; + D0 = C4^ROL64(C1, 1); + D1 = C0^ROL64(C2, 1); + D2 = C1^ROL64(C3, 1); + D3 = C2^ROL64(C4, 1); + D4 = C3^ROL64(C0, 1); + + B0 = (A00^D0); + B1 = ROL64((A31^D1), 44); + B2 = ROL64((A12^D2), 43); + B3 = ROL64((A43^D3), 21); + B4 = ROL64((A24^D4), 14); + A00 = B0 ^((~B1)& B2 ); + A00 ^= RC[i+1]; + A31 = B1 ^((~B2)& B3 ); + A12 = B2 ^((~B3)& B4 ); + A43 = B3 ^((~B4)& B0 ); + A24 = B4 ^((~B0)& B1 ); + + B2 = ROL64((A40^D0), 3); + B3 = ROL64((A21^D1), 45); + B4 = ROL64((A02^D2), 61); + B0 = ROL64((A33^D3), 28); + B1 = ROL64((A14^D4), 20); + A40 = B0 ^((~B1)& B2 ); + A21 = B1 ^((~B2)& B3 ); + A02 = B2 ^((~B3)& B4 ); + A33 = B3 ^((~B4)& B0 ); + A14 = B4 ^((~B0)& B1 ); + + B4 = ROL64((A30^D0), 18); + B0 = ROL64((A11^D1), 1); + B1 = ROL64((A42^D2), 6); + B2 = ROL64((A23^D3), 25); + B3 = ROL64((A04^D4), 8); + A30 = B0 ^((~B1)& B2 ); + A11 = B1 ^((~B2)& B3 ); + A42 = B2 ^((~B3)& B4 ); + A23 = B3 ^((~B4)& B0 ); + A04 = B4 ^((~B0)& B1 ); + + B1 = ROL64((A20^D0), 36); + B2 = ROL64((A01^D1), 10); + B3 = ROL64((A32^D2), 15); + B4 = ROL64((A13^D3), 56); + B0 = ROL64((A44^D4), 27); + A20 = B0 ^((~B1)& B2 ); + A01 = B1 ^((~B2)& B3 ); + A32 = B2 ^((~B3)& B4 ); + A13 = B3 ^((~B4)& B0 ); + A44 = B4 ^((~B0)& B1 ); + + B3 = ROL64((A10^D0), 41); + B4 = ROL64((A41^D1), 2); + B0 = ROL64((A22^D2), 62); + B1 = ROL64((A03^D3), 55); + B2 = ROL64((A34^D4), 39); + A10 = B0 ^((~B1)& B2 ); + A41 = B1 ^((~B2)& B3 ); + A22 = B2 ^((~B3)& B4 ); + A03 = B3 ^((~B4)& B0 ); + A34 = B4 ^((~B0)& B1 ); + + C0 = A00^A40^A30^A20^A10; + C1 = A31^A21^A11^A01^A41; + C2 = A12^A02^A42^A32^A22; + C3 = A43^A33^A23^A13^A03; + C4 = A24^A14^A04^A44^A34; + D0 = C4^ROL64(C1, 1); + D1 = C0^ROL64(C2, 1); + D2 = C1^ROL64(C3, 1); + D3 = C2^ROL64(C4, 1); + D4 = C3^ROL64(C0, 1); + + B0 = (A00^D0); + B1 = ROL64((A21^D1), 44); + B2 = ROL64((A42^D2), 43); + B3 = ROL64((A13^D3), 21); + B4 = ROL64((A34^D4), 14); + A00 = B0 ^((~B1)& B2 ); + A00 ^= RC[i+2]; + A21 = B1 ^((~B2)& B3 ); + A42 = B2 ^((~B3)& B4 ); + A13 = B3 ^((~B4)& B0 ); + A34 = B4 ^((~B0)& B1 ); + + B2 = ROL64((A30^D0), 3); + B3 = ROL64((A01^D1), 45); + B4 = ROL64((A22^D2), 61); + B0 = ROL64((A43^D3), 28); + B1 = ROL64((A14^D4), 20); + A30 = B0 ^((~B1)& B2 ); + A01 = B1 ^((~B2)& B3 ); + A22 = B2 ^((~B3)& B4 ); + A43 = B3 ^((~B4)& B0 ); + A14 = B4 ^((~B0)& B1 ); + + B4 = ROL64((A10^D0), 18); + B0 = ROL64((A31^D1), 1); + B1 = ROL64((A02^D2), 6); + B2 = ROL64((A23^D3), 25); + B3 = ROL64((A44^D4), 8); + A10 = B0 ^((~B1)& B2 ); + A31 = B1 ^((~B2)& B3 ); + A02 = B2 ^((~B3)& B4 ); + A23 = B3 ^((~B4)& B0 ); + A44 = B4 ^((~B0)& B1 ); + + B1 = ROL64((A40^D0), 36); + B2 = ROL64((A11^D1), 10); + B3 = ROL64((A32^D2), 15); + B4 = ROL64((A03^D3), 56); + B0 = ROL64((A24^D4), 27); + A40 = B0 ^((~B1)& B2 ); + A11 = B1 ^((~B2)& B3 ); + A32 = B2 ^((~B3)& B4 ); + A03 = B3 ^((~B4)& B0 ); + A24 = B4 ^((~B0)& B1 ); + + B3 = ROL64((A20^D0), 41); + B4 = ROL64((A41^D1), 2); + B0 = ROL64((A12^D2), 62); + B1 = ROL64((A33^D3), 55); + B2 = ROL64((A04^D4), 39); + A20 = B0 ^((~B1)& B2 ); + A41 = B1 ^((~B2)& B3 ); + A12 = B2 ^((~B3)& B4 ); + A33 = B3 ^((~B4)& B0 ); + A04 = B4 ^((~B0)& B1 ); + + C0 = A00^A30^A10^A40^A20; + C1 = A21^A01^A31^A11^A41; + C2 = A42^A22^A02^A32^A12; + C3 = A13^A43^A23^A03^A33; + C4 = A34^A14^A44^A24^A04; + D0 = C4^ROL64(C1, 1); + D1 = C0^ROL64(C2, 1); + D2 = C1^ROL64(C3, 1); + D3 = C2^ROL64(C4, 1); + D4 = C3^ROL64(C0, 1); + + B0 = (A00^D0); + B1 = ROL64((A01^D1), 44); + B2 = ROL64((A02^D2), 43); + B3 = ROL64((A03^D3), 21); + B4 = ROL64((A04^D4), 14); + A00 = B0 ^((~B1)& B2 ); + A00 ^= RC[i+3]; + A01 = B1 ^((~B2)& B3 ); + A02 = B2 ^((~B3)& B4 ); + A03 = B3 ^((~B4)& B0 ); + A04 = B4 ^((~B0)& B1 ); + + B2 = ROL64((A10^D0), 3); + B3 = ROL64((A11^D1), 45); + B4 = ROL64((A12^D2), 61); + B0 = ROL64((A13^D3), 28); + B1 = ROL64((A14^D4), 20); + A10 = B0 ^((~B1)& B2 ); + A11 = B1 ^((~B2)& B3 ); + A12 = B2 ^((~B3)& B4 ); + A13 = B3 ^((~B4)& B0 ); + A14 = B4 ^((~B0)& B1 ); + + B4 = ROL64((A20^D0), 18); + B0 = ROL64((A21^D1), 1); + B1 = ROL64((A22^D2), 6); + B2 = ROL64((A23^D3), 25); + B3 = ROL64((A24^D4), 8); + A20 = B0 ^((~B1)& B2 ); + A21 = B1 ^((~B2)& B3 ); + A22 = B2 ^((~B3)& B4 ); + A23 = B3 ^((~B4)& B0 ); + A24 = B4 ^((~B0)& B1 ); + + B1 = ROL64((A30^D0), 36); + B2 = ROL64((A31^D1), 10); + B3 = ROL64((A32^D2), 15); + B4 = ROL64((A33^D3), 56); + B0 = ROL64((A34^D4), 27); + A30 = B0 ^((~B1)& B2 ); + A31 = B1 ^((~B2)& B3 ); + A32 = B2 ^((~B3)& B4 ); + A33 = B3 ^((~B4)& B0 ); + A34 = B4 ^((~B0)& B1 ); + + B3 = ROL64((A40^D0), 41); + B4 = ROL64((A41^D1), 2); + B0 = ROL64((A42^D2), 62); + B1 = ROL64((A43^D3), 55); + B2 = ROL64((A44^D4), 39); + A40 = B0 ^((~B1)& B2 ); + A41 = B1 ^((~B2)& B3 ); + A42 = B2 ^((~B3)& B4 ); + A43 = B3 ^((~B4)& B0 ); + A44 = B4 ^((~B0)& B1 ); + } +} + +/* +** Initialize a new hash. iSize determines the size of the hash +** in bits and should be one of 224, 256, 384, or 512. Or iSize +** can be zero to use the default hash size of 256 bits. +*/ +static void SHA3Init(SHA3Context *p, int iSize){ + memset(p, 0, sizeof(*p)); + if( iSize>=128 && iSize<=512 ){ + p->nRate = (1600 - ((iSize + 31)&~31)*2)/8; + }else{ + p->nRate = (1600 - 2*256)/8; + } +#if SHA3_BYTEORDER==1234 + /* Known to be little-endian at compile-time. No-op */ +#elif SHA3_BYTEORDER==4321 + p->ixMask = 7; /* Big-endian */ +#else + { + static unsigned int one = 1; + if( 1==*(unsigned char*)&one ){ + /* Little endian. No byte swapping. */ + p->ixMask = 0; + }else{ + /* Big endian. Byte swap. */ + p->ixMask = 7; + } + } +#endif +} + +/* +** Make consecutive calls to the SHA3Update function to add new content +** to the hash +*/ +static void SHA3Update( + SHA3Context *p, + const unsigned char *aData, + unsigned int nData +){ + unsigned int i = 0; +#if SHA3_BYTEORDER==1234 + if( (p->nLoaded % 8)==0 && ((aData - (const unsigned char*)0)&7)==0 ){ + for(; i+7<nData; i+=8){ + p->u.s[p->nLoaded/8] ^= *(u64*)&aData[i]; + p->nLoaded += 8; + if( p->nLoaded>=p->nRate ){ + KeccakF1600Step(p); + p->nLoaded = 0; + } + } + } +#endif + for(; i<nData; i++){ +#if SHA3_BYTEORDER==1234 + p->u.x[p->nLoaded] ^= aData[i]; +#elif SHA3_BYTEORDER==4321 + p->u.x[p->nLoaded^0x07] ^= aData[i]; +#else + p->u.x[p->nLoaded^p->ixMask] ^= aData[i]; +#endif + p->nLoaded++; + if( p->nLoaded==p->nRate ){ + KeccakF1600Step(p); + p->nLoaded = 0; + } + } +} + +/* +** After all content has been added, invoke SHA3Final() to compute +** the final hash. The function returns a pointer to the binary +** hash value. +*/ +static unsigned char *SHA3Final(SHA3Context *p){ + unsigned int i; + if( p->nLoaded==p->nRate-1 ){ + const unsigned char c1 = 0x86; + SHA3Update(p, &c1, 1); + }else{ + const unsigned char c2 = 0x06; + const unsigned char c3 = 0x80; + SHA3Update(p, &c2, 1); + p->nLoaded = p->nRate - 1; + SHA3Update(p, &c3, 1); + } + for(i=0; i<p->nRate; i++){ + p->u.x[i+p->nRate] = p->u.x[i^p->ixMask]; + } + return &p->u.x[p->nRate]; +} + +/* +** Convert a digest into base-16. +*/ +static void DigestToBase16(unsigned char *digest, char *zBuf, int nByte){ + static const char zEncode[] = "0123456789abcdef"; + int ix; + + for(ix=0; ix<nByte; ix++){ + *zBuf++ = zEncode[(*digest>>4)&0xf]; + *zBuf++ = zEncode[*digest++ & 0xf]; + } + *zBuf = '\0'; +} + +/* +** Compute the SHA3-256 checksum of a file on disk. Store the resulting +** checksum in the zCksum. +** +** Return the number of errors. +*/ +void sha3sum_file(const char *zFilename, char *zCksum){ + FILE *in; + SHA3Context ctx; + char zBuf[10240]; + + in = fopen(zFilename,"rb"); + if( in==0 ){ + zCksum[0] = 0; + return; + } + SHA3Init(&ctx, 256); + for(;;){ + size_t n; + n = fread(zBuf, 1, sizeof(zBuf), in); + if( n<=0 ) break; + SHA3Update(&ctx, (unsigned char*)zBuf, (unsigned)n); + } + fclose(in); + DigestToBase16(SHA3Final(&ctx), zCksum, 32); +} + +/* +** Compute the SHA1 checksum of a file on disk. Store the resulting +** checksum in the zCksum. +** +** Return the number of errors. +*/ +void sha1sum_file(const char *zFilename, char *zCksum){ + FILE *in; + SHA1Context ctx; + unsigned char zResult[20]; + char zBuf[10240]; + + in = fopen(zFilename,"rb"); + if( in==0 ){ + zCksum[0] = 0; + return; + } + SHA1Init(&ctx); + for(;;){ + size_t n; + n = fread(zBuf, 1, sizeof(zBuf), in); + if( n<=0 ) break; + SHA1Update(&ctx, (unsigned char*)zBuf, (unsigned)n); + } + fclose(in); + SHA1Final(zResult, &ctx); + DigestToBase16(zResult, zCksum, 20); +} + +/* +** Decode a fossilized string in-place. +*/ +void defossilize(char *z){ + int i, j, cc; + char *zSlash = strchr(z, '\\'); + if( zSlash==0 ) return; + i = zSlash - z; + for(j=i; (cc=z[i])!=0; i++){ + if( cc=='\\' && z[i+1] ){ + i++; + switch( z[i] ){ + case 'n': cc = '\n'; break; + case 's': cc = ' '; break; + case 't': cc = '\t'; break; + case 'r': cc = '\r'; break; + case 'v': cc = '\v'; break; + case 'f': cc = '\f'; break; + case '0': cc = 0; break; + case '\\': cc = '\\'; break; + default: cc = z[i]; break; + } + } + z[j++] = cc; + } + if( z[j] ) z[j] = 0; +} + +/* +** Report that a single file is incorrect. +*/ +static void errorMsg(int *pnErr, const char *zVers, const char *zFile){ + if( *pnErr==0 ){ + printf("Derived from %.25s with changes to:\n", zVers); + } + printf(" %s\n", zFile); + (*pnErr)++; +} +static void errorMsgNH(int *pnErr, const char *zVers, const char *zFile){ + if( *pnErr==0 ){ + printf("%s\n", zVers); + } + printf("%s\n", zFile); + (*pnErr)++; +} + +int main(int argc, char **argv){ + int i, j; + int nDir; + FILE *in; + int bDebug = 0; + int bNonHuman = 0; + int bSeenManifestErr = 0; + int nErr = 0; + SHA3Context ctx3; + const char *zDir = 0; + void (*xErr)(int*,const char*,const char*); + char zHash[100]; + char zCk[100]; + char zVers[100]; + char zLine[40000]; + char zFile[40000]; + xErr = errorMsg; + for(i=1; i<argc; i++){ + const char *z = argv[i]; + if( z[0]!='-' ){ + if( zDir!=0 ){ + fprintf(stderr, "bad argument: %s\n", z); + return 1; + } + zDir = z; + continue; + } + if( z[1]=='-' && z[2]!=0 ) z++; + if( strcmp(argv[1],"-sha1")==0 ){ + /* For testing purposes, if the first argument is --sha1, then simply + ** compute and print the SHA1 checksum of all subsequent arguments. */ + for(i++; i<argc; i++){ + sha1sum_file(argv[i], zHash); + printf("%s %s\n", zHash, argv[i]); + } + return 0; + } + if( strcmp(argv[1], "-sha3")==0 ){ + /* For testing purposes, if the first argument is --sha3, then simply + ** compute and print the SHA3-256 checksum of all subsequent arguments. */ + for(i++; i<argc; i++){ + sha3sum_file(argv[i], zHash); + printf("%s %s\n", zHash, argv[i]); + } + return 0; + } + if( strcmp(z,"-v")==0 ){ + bDebug = 1; + continue; + } + if( strcmp(z,"-x")==0 ){ + bNonHuman = 1; + xErr = errorMsgNH; + continue; + } + usage: + fprintf(stderr, "Usage: %s DIRECTORY\n" + " or: %s --sha1 FILE ...\n" + " or: %s --sha3 FILE ...\n", + argv[0], argv[0], argv[0]); + return 1; + } + if( !zDir ){ + goto usage; + } + if( strlen(zDir)>1000 ){ + fprintf(stderr, "Directory argument too big: [%s]\n", zDir); + return 1; + } + nDir = (int)strlen(zDir); + if( nDir<0 ){ + fprintf(stderr, "Directory argument too short.\n"); + return 1; + } + memcpy(zFile, zDir, nDir); + if( zFile[nDir-1]!='/' ){ + zFile[nDir++] = '/'; + } + memcpy(&zFile[nDir], "manifest", 9); + if( bDebug ){ + printf("manifest file: [%s]\n", zFile); + } + in = fopen(zFile, "rb"); + if( in==0 ){ + fprintf(stderr, "missing manifest: \"%s\"\n", zFile); + return 1; + } + SHA3Init(&ctx3, 256); + while( fgets(zLine, sizeof(zLine), in) ){ + if( zLine[0]=='#' ) break; + SHA3Update(&ctx3, (unsigned char*)zLine, (int)strlen(zLine)); + } + DigestToBase16(SHA3Final(&ctx3), zVers, 32); + + rewind(in); + while( fgets(zLine, sizeof(zLine), in) ){ + if( zLine[0]!='F' ) continue; + if( zLine[1]!=' ' ) continue; + for(i=2, j=nDir; zLine[i]!=0 && zLine[i]!=' '; i++, j++){ + if( j<sizeof(zFile) ) zFile[j] = zLine[i]; + } + if( j<sizeof(zFile) ) zFile[j] = 0; + zFile[sizeof(zFile)-1] = 0; + defossilize(&zFile[nDir]); + if( zLine[i]!=' ' ){ + bSeenManifestErr = 1; + continue; + } + for(i++, j=0; zLine[i]>='0' && zLine[i]<='f'; i++, j++){ + if( j<sizeof(zHash) ) zHash[j] = zLine[i]; + } + if( j<sizeof(zHash) ) zHash[j] = 0; + zHash[sizeof(zHash)-1] = 0; + if( bDebug ){ + printf("%s %s\n", zFile, zHash); + } + if( access(zFile, R_OK)!=0 ){ + xErr(&nErr, zVers, &zFile[nDir]); + continue; + } + if( strlen(zHash)==40 ){ + sha1sum_file(zFile, zCk); + if( strcmp(zHash, zCk)!=0 ){ + xErr(&nErr, zVers, &zFile[nDir]); + } + }else if( strlen(zHash)==64 ){ + sha3sum_file(zFile, zCk); + if( strcmp(zHash, zCk)!=0 ){ + xErr(&nErr, zVers, &zFile[nDir]); + } + }else{ + bSeenManifestErr = 1; + xErr(&nErr, zVers, &zFile[nDir]); + } + } + fclose(in); + in = 0; + if( bSeenManifestErr ) xErr(&nErr, zVers, "manifest"); + memcpy(&zFile[nDir], "manifest.uuid", 14); + if( access(zFile, R_OK)!=0 + || (in = fopen(zFile,"rb"))==0 + || fgets(zLine, sizeof(zLine), in)==0 + || strlen(zLine)!=65 + || zLine[64]!='\n' + || memcmp(zLine, zVers, 64)!=0 + ){ + xErr(&nErr, zVers, &zFile[nDir]); + } + if( in ) fclose(in); + + if( bNonHuman ){ + if( nErr ) return 0; + printf("%s\n", zVers); + }else{ + if( nErr ) return nErr; + printf("OK %.25s\n", zVers); + } + return 0; +} diff --git a/tool/srcck1.c b/tool/srcck1.c index e95765185f..5a1158beb9 100644 --- a/tool/srcck1.c +++ b/tool/srcck1.c @@ -13,7 +13,7 @@ ** The aim of this utility is to prevent recurrences of errors such ** as the one fixed at: ** -** https://www.sqlite.org/src/info/a2952231ac7abe16 +** https://sqlite.org/src/info/a2952231ac7abe16 ** ** Note that another similar error was found by this utility when it was ** first written. That other error was fixed by the same check-in that @@ -58,7 +58,7 @@ static char *readFile(const char *zFilename){ return z; } -/* Change the C code in the argument to see if it might have +/* Check the C code in the argument to see if it might have ** side effects. The only accurate way to know this is to do a full ** parse of the C code, which this routine does not do. This routine ** uses a simple heuristic of looking for: diff --git a/tool/srctree-check.tcl b/tool/srctree-check.tcl new file mode 100644 index 0000000000..921bb0976e --- /dev/null +++ b/tool/srctree-check.tcl @@ -0,0 +1,47 @@ +#!/usr/bin/tclsh +# +# Run this script from the top of the source tree in order to confirm that +# various aspects of the source tree are up-to-date. Items checked include: +# +# * Makefile.msc and autoconf/Makefile.msc agree +# * VERSION agrees with autoconf/tea/configure.ac +# +# Other tests might be added later. +# +# Error messages are printed and the process exists non-zero if problems +# are found. If everything is ok, no output is generated and the process +# exits with 0. +# + +# Read an entire file. +# +proc readfile {filename} { + set fd [open $filename rb] + set txt [read $fd] + close $fd + return $txt +} + +# Find the root of the tree. +# +set ROOT [file dir [file dir [file normalize $argv0]]] + +# Name of the TCL interpreter +# +set TCLSH [info nameofexe] + +# Number of errors seen. +# +set NERR 0 + +######################### autoconf/Makefile.msc ############################### + +set f1 [readfile $ROOT/autoconf/Makefile.msc] +exec $TCLSH $ROOT/tool/mkmsvcmin.tcl $ROOT/Makefile.msc tmp1.txt +set f2 [readfile tmp1.txt] +file delete tmp1.txt +if {$f1 != $f2} { + puts "ERROR: ./autoconf/Makefile.msc does not agree with ./Makefile.msc" + puts "...... Fix: tclsh tool/mkmsvcmin.tcl" + incr NERR +} diff --git a/tool/stripccomments.c b/tool/stripccomments.c new file mode 100644 index 0000000000..1d85252784 --- /dev/null +++ b/tool/stripccomments.c @@ -0,0 +1,245 @@ +/** + Strips C- and C++-style comments from stdin, sending the results to + stdout. It assumes that its input is legal C-like code, and does + only little error handling. + + It treats string literals as anything starting and ending with + matching double OR single quotes OR backticks (for use with + scripting languages which use those). It assumes that a quote + character within a string which uses the same quote type is escaped + by a backslash. It should not be used on any code which might + contain C/C++ comments inside heredocs, and similar constructs, as + it will strip those out. + + Usage: $0 [--keep-first|-k] < input > output + + The --keep-first (-k) flag tells it to retain the first comment in the + input stream (which is often a license or attribution block). It + may be given repeatedly, each one incrementing the number of + retained comments by one. + + License: Public Domain + Author: Stephan Beal (stephan@wanderinghorse.net) +*/ +#include <stdio.h> +#include <assert.h> +#include <string.h> + +#if 1 +#define MARKER(pfexp) \ + do{ printf("MARKER: %s:%d:\t",__FILE__,__LINE__); \ + printf pfexp; \ + } while(0) +#else +#define MARKER(exp) if(0) printf +#endif + +struct { + FILE * input; + FILE * output; + int rc; + int keepFirst; +} App = { + 0/*input*/, + 0/*output*/, + 0/*rc*/, + 0/*keepFirst*/ +}; + +void do_it_all(void){ + enum states { + S_NONE = 0 /* not in comment */, + S_SLASH1 = 1 /* slash - possibly comment prefix */, + S_CPP = 2 /* in C++ comment */, + S_C = 3 /* in C comment */ + }; + int ch, prev = EOF; + FILE * out = App.output; + int const slash = '/'; + int const star = '*'; + int line = 1; + int col = 0; + enum states state = S_NONE /* current state */; + int elide = 0 /* true if currently eliding output */; + int state3Col = -99 + /* huge kludge for odd corner case: */ + /*/ <--- here. state3Col marks the source column in which a C-style + comment starts, so that it can tell if star-slash inside a + C-style comment is the end of the comment or is the weird corner + case marked at the start of _this_ comment block. */; + for( ; EOF != (ch = fgetc(App.input)); prev = ch, + ++col){ + switch(state){ + case S_NONE: + if('\''==ch || '"'==ch || '`'==ch){ + /* Read string literal... + needed to properly catch comments in strings. */ + int const quote = ch, + startLine = line, startCol = col; + int ch2, escaped = 0, endOfString = 0; + fputc(ch, out); + for( ++col; !endOfString && EOF != (ch2 = fgetc(App.input)); + ++col ){ + switch(ch2){ + case '\\': escaped = !escaped; + break; + case '`': + case '\'': + case '"': + if(!escaped && quote == ch2) endOfString = 1; + escaped = 0; + break; + default: + escaped = 0; + break; + } + if('\n'==ch2){ + ++line; + col = 0; + } + fputc(ch2, out); + } + if(EOF == ch2){ + fprintf(stderr, "Unexpected EOF while reading %s literal " + "on line %d column %d.\n", + ('\''==ch) ? "char" : "string", + startLine, startCol); + App.rc = 1; + return; + } + break; + } + else if(slash == ch){ + /* MARKER(("state 0 ==> 1 @ %d:%d\n", line, col)); */ + if( '\\'==prev ){ + /** + JS regexes may contain slash-asterisks, as happened at: + + https://github.com/emscripten-core/emscripten/issues/23412 + + Such regexes will always necessarily be preceded by a + backslash, though. + + It is hypothetically possible for a legitimate comment + slash-asterisk to appear immediately before a + backslash, but that seems like an even rarer corner + case than the JS regex case. + */ + fputc(ch, out); + }else{ + state = S_SLASH1; + } + break; + } + fputc(ch, out); + break; + case S_SLASH1: /* 1 slash */ + /* MARKER(("SLASH1 @ %d:%d App.keepFirst=%d\n", + line, col, App.keepFirst)); */ + switch(ch){ + case '*': + /* Enter C comment */ + if(App.keepFirst>0){ + elide = 0; + --App.keepFirst; + }else{ + elide = 1; + } + /*MARKER(("state 1 ==> 3 @ %d:%d\n", line, col));*/ + state = S_C; + state3Col = col-1; + if(!elide){ + fputc(prev, out); + fputc(ch, out); + } + break; + case '/': + /* Enter C++ comment */ + if(App.keepFirst>0){ + elide = 0; + --App.keepFirst; + }else{ + elide = 1; + } + /*MARKER(("state 1 ==> 2 @ %d:%d\n", line, col));*/ + state = S_CPP; + if(!elide){ + fputc(prev, out); + fputc(ch, out); + } + break; + default: + /* It wasn't a comment after all. */ + state = S_NONE; + if(!elide){ + fputc(prev, out); + fputc(ch, out); + } + } + break; + case S_CPP: /* C++ comment */ + if('\n' == ch){ + /* MARKER(("state 2 ==> 0 @ %d:%d\n", line, col)); */ + state = S_NONE; + elide = 0; + } + if(!elide){ + fputc(ch, out); + } + break; + case S_C: /* C comment */ + if(!elide){ + fputc(ch, out); + } + if(slash == ch){ + if(star == prev){ + /* MARKER(("state 3 ==> 0 @ %d:%d\n", line, col)); */ + /* Corner case which breaks this: */ + /*/ <-- slash there */ + /* That shows up twice in a piece of 3rd-party + code i use. */ + /* And thus state3Col was introduced :/ */ + if(col!=state3Col+2){ + state = S_NONE; + elide = 0; + state3Col = -99; + } + } + } + break; + default: + assert(!"impossible!"); + break; + } + if('\n' == ch){ + ++line; + col = 0; + state3Col = -99; + } + } +} + +static void usage(char const *zAppName){ + fprintf(stderr, "Strips C- and C++-style comments from stdin and sends " + "the results to stdout.\n"); + fprintf(stderr, "Usage: %s [--keep-first|-k] < input > output\n", zAppName); +} + +int main( int argc, char const * const * argv ){ + int i; + for(i = 1; i < argc; ++i){ + char const * zArg = argv[i]; + while( '-'==*zArg ) ++zArg; + if( 0==strcmp(zArg,"k") + || 0==strcmp(zArg,"keep-first") ){ + ++App.keepFirst; + }else{ + usage(argv[0]); + return 1; + } + } + App.input = stdin; + App.output = stdout; + do_it_all(); + return App.rc ? 1 : 0; +} diff --git a/tool/symbols.sh b/tool/symbols.sh index befffce5c4..67d578e1f1 100644 --- a/tool/symbols.sh +++ b/tool/symbols.sh @@ -5,17 +5,19 @@ # make sqlite3.c -echo '****** Exported symbols from a build including RTREE, FTS4 & ICU ******' +echo '****** Exported symbols from a build including RTREE, FTS4 & FTS5 ******' gcc -c -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_RTREE \ -DSQLITE_ENABLE_MEMORY_MANAGEMENT -DSQLITE_ENABLE_STAT3 \ -DSQLITE_ENABLE_MEMSYS5 -DSQLITE_ENABLE_UNLOCK_NOTIFY \ -DSQLITE_ENABLE_COLUMN_METADATA -DSQLITE_ENABLE_ATOMIC_WRITE \ - -DSQLITE_ENABLE_ICU \ + -DSQLITE_ENABLE_PREUPDATE_HOOK -DSQLITE_ENABLE_SESSION \ + -DSQLITE_ENABLE_FTS5 -DSQLITE_ENABLE_GEOPOLY \ sqlite3.c nm sqlite3.o | grep ' [TD] ' | sort -k 3 -echo '****** Surplus symbols from a build including RTREE, FTS4 & ICU ******' -nm sqlite3.o | grep ' [TD] ' | grep -v ' .*sqlite3_' +echo '****** Surplus symbols from a build including RTREE, FTS4 & FTS5 ******' +nm sqlite3.o | grep ' [TD] ' | + egrep -v ' .*sqlite3(session|rebaser|changeset|changegroup)?_' echo '****** Dependencies of the core. No extensions. No OS interface *******' gcc -c -DSQLITE_ENABLE_MEMORY_MANAGEMENT -DSQLITE_ENABLE_STAT3 \ diff --git a/tool/tclConfigShToMake.sh b/tool/tclConfigShToMake.sh new file mode 100755 index 0000000000..89ca15ad5b --- /dev/null +++ b/tool/tclConfigShToMake.sh @@ -0,0 +1,31 @@ +#!/bin/sh +# +# A level of indirection for use soley by main.mk to extract info +# from tclConfig.sh if it's not provided by the configure script. +# +# Expects to be passed a full path to a tclConfig.sh. It sources it +# and emits TCL code which sets some vars which are exported by +# tclConfig.sh. +# +# This script expects that the caller has already validated that the +# file exists, is not a directory, and is readable. +# +# If passed no filename, or an empty one, then it emits config code +# suitable for the "config not found" case. +if test x = "x$1"; then + TCL_INCLUDE_SPEC= + TCL_LIB_SPEC= + TCL_STUB_LIB_SPEC= + TCL_EXEC_PREFIX= + TCL_VERSION= +else + . "$1" +fi + +cat <<EOF +TCL_INCLUDE_SPEC = $TCL_INCLUDE_SPEC +TCL_LIB_SPEC = $TCL_LIB_SPEC +TCL_STUB_LIB_SPEC = $TCL_STUB_LIB_SPEC +TCL_EXEC_PREFIX = $TCL_EXEC_PREFIX +TCL_VERSION = $TCL_VERSION +EOF diff --git a/tool/tostr.tcl b/tool/tostr.tcl deleted file mode 100644 index cb06ee947f..0000000000 --- a/tool/tostr.tcl +++ /dev/null @@ -1,12 +0,0 @@ -#!/usr/bin/tcl -# -# Convert input text into a C string -# -set in [open [lindex $argv 0] rb] -while {![eof $in]} { - set line [gets $in] - if {[eof $in]} break; - set x [string map "\\\\ \\\\\\\\ \\\" \\\\\"" $line] - puts "\"$x\\n\"" -} -close $in diff --git a/tool/vdbe-compress.tcl b/tool/vdbe-compress.tcl index 9477f4afe6..eaafc7a956 100644 --- a/tool/vdbe-compress.tcl +++ b/tool/vdbe-compress.tcl @@ -65,11 +65,11 @@ while {![eof stdin]} { # set vlist {} set seenDecl 0 -set namechars {abcdefghijklmnopqrstuvwxyz} +set namechars {abcefghjklmnopqrstuvwxyz} set nnc [string length $namechars] while {![eof stdin]} { set line [gets stdin] - if {[regexp "^case (OP_\\w+): \173" $line all operator]} { + if {[regexp "^case (OP_\\w+): \\x7B" $line all operator]} { append afterUnion $line\n set vlist {} while {![eof stdin]} { diff --git a/tool/vdbe_profile.tcl b/tool/vdbe_profile.tcl index a0dc99ec33..b7240e3567 100644 --- a/tool/vdbe_profile.tcl +++ b/tool/vdbe_profile.tcl @@ -66,6 +66,8 @@ foreach stmt $allstmt { puts "********************************************************************" puts [string trim $sql($stmt)] puts "Execution count: $cnt($stmt)" + set tcx 0 + set ttx 0 for {set i 0} {[info exists stat($stmt,$i)]} {incr i} { foreach {cx tx detail} $stat($stmt,$i) break if {$cx==0} { @@ -74,7 +76,11 @@ foreach stmt $allstmt { set ax [expr {$tx/$cx}] } puts [format {%8d %12d %12d %4d %s} $cx $tx $ax $i $detail] + incr tcx $cx + incr ttx $tx } + set tax [expr {$tcx>0?$ttx/$tcx:0}] + puts [format {%8d %12d %12d TOTAL} $tcx $ttx $tax] } puts "********************************************************************" puts "OPCODES:" diff --git a/tool/version-info.c b/tool/version-info.c new file mode 100644 index 0000000000..149bad4f28 --- /dev/null +++ b/tool/version-info.c @@ -0,0 +1,114 @@ +/* +** 2022-10-16 +** +** The author disclaims copyright to this source code. In place of a +** legal notice, here is a blessing: +** +** * May you do good and not evil. +** * May you find forgiveness for yourself and forgive others. +** * May you share freely, never taking more than you give. +** +************************************************************************* +** This file simply outputs sqlite3 version information in JSON form, +** intended for embedding in the sqlite3 JS API build. +*/ +#ifdef TEST_VERSION +/*3029003 3039012*/ +#define SQLITE_VERSION "X.Y.Z" +#define SQLITE_VERSION_NUMBER TEST_VERSION +#define SQLITE_SOURCE_ID "dummy" +#else +#include "sqlite3.h" +#endif +#include <stdio.h> +#include <string.h> +static void usage(const char *zAppName){ + puts("Emits version info about the sqlite3 it is built against."); + printf("Usage: %s [--quote] --INFO-FLAG:\n\n", zAppName); + puts(" --version Emit SQLITE_VERSION (3.X.Y)"); + puts(" --version-number Emit SQLITE_VERSION_NUMBER (30XXYYZZ)"); + puts(" --download-version Emit /download.html version number (3XXYYZZ)"); + puts(" --source-id Emit SQLITE_SOURCE_ID"); + puts(" --json Emit all info in JSON form"); + puts("\nThe non-JSON formats may be modified by:\n"); + puts(" --quote Add double quotes around output."); +} + +int main(int argc, char const * const * argv){ + int fJson = 0; + int fVersion = 0; + int fVersionNumber = 0; + int fDlVersion = 0; + int dlVersion = 0; + int fSourceInfo = 0; + int fQuote = 0; + int nFlags = 0; + int i; + + for( i = 1; i < argc; ++i ){ + const char * zArg = argv[i]; + while('-'==*zArg) ++zArg; + if( 0==strcmp("version", zArg) ){ + fVersion = 1; + }else if( 0==strcmp("version-number", zArg) ){ + fVersionNumber = 1; + }else if( 0==strcmp("download-version", zArg) ){ + fDlVersion = 1; + }else if( 0==strcmp("source-id", zArg) ){ + fSourceInfo = 1; + }else if( 0==strcmp("json", zArg) ){ + fJson = 1; + }else if( 0==strcmp("quote", zArg) ){ + fQuote = 1; + --nFlags; + }else{ + printf("Unhandled flag: %s\n", argv[i]); + usage(argv[0]); + return 1; + } + ++nFlags; + } + + if( 0==nFlags ) fJson = 1; + + { + const int v = SQLITE_VERSION_NUMBER; + int ver[4] = {0,0,0,0}; + ver[0] = (v / 1000000) * 1000000; + ver[1] = v % 1000000 / 100 * 1000; + ver[2] = v % 100 * 100; + dlVersion = ver[0] + ver[1] + ver[2] + ver[3]; + } + if( fJson ){ + printf("{\"libVersion\": \"%s\", " + "\"libVersionNumber\": %d, " + "\"sourceId\": \"%s\"," + "\"downloadVersion\": %d," + "\"scm\":{ " + "\"sha3-256\": \"%s\"," + "\"branch\": \"" SQLITE_SCM_BRANCH "\"," + "\"tags\": \"" SQLITE_SCM_TAGS "\"," + "\"datetime\": \"" SQLITE_SCM_DATETIME "\"" + "}" + "}"/*missing newline is intentional*/, + SQLITE_VERSION, + SQLITE_VERSION_NUMBER, + SQLITE_SOURCE_ID, + dlVersion, + SQLITE_SOURCE_ID+20); + }else{ + if(fQuote) printf("%c", '"'); + if( fVersion ){ + printf("%s", SQLITE_VERSION); + }else if( fVersionNumber ){ + printf("%d", SQLITE_VERSION_NUMBER); + }else if( fSourceInfo ){ + printf("%s", SQLITE_SOURCE_ID); + }else if( fDlVersion ){ + printf("%d", dlVersion); + } + if(fQuote) printf("%c", '"'); + puts(""); + } + return 0; +} diff --git a/tool/warnings-clang.sh b/tool/warnings-clang.sh index 7a0aa4bce7..6dcc086d2f 100644 --- a/tool/warnings-clang.sh +++ b/tool/warnings-clang.sh @@ -3,12 +3,12 @@ # Run this script in a directory with a working makefile to check for # compiler warnings in SQLite. # -rm -f sqlite3.c -make sqlite3.c +rm -f sqlite3.c shell.c +make sqlite3.c shell.c echo '************* FTS4 and RTREE ****************' scan-build gcc -c -DHAVE_STDINT_H -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_RTREE \ -DSQLITE_DEBUG -DSQLITE_ENABLE_STAT3 sqlite3.c 2>&1 | grep -v 'ANALYZE:' echo '********** ENABLE_STAT3. THREADSAFE=0 *******' scan-build gcc -c -I. -DSQLITE_ENABLE_STAT3 -DSQLITE_THREADSAFE=0 \ -DSQLITE_DEBUG \ - sqlite3.c ../sqlite/src/shell.c -ldl 2>&1 | grep -v 'ANALYZE:' + sqlite3.c shell.c -ldl 2>&1 | grep -v 'ANALYZE:' diff --git a/tool/warnings.sh b/tool/warnings.sh index 6c7cb3a8f8..3619ce70a3 100644 --- a/tool/warnings.sh +++ b/tool/warnings.sh @@ -3,16 +3,42 @@ # Run this script in a directory with a working makefile to check for # compiler warnings in SQLite. # + +if uname | grep -i openbsd ; then + # Use these for testing on OpenBSD: + WARNING_OPTS=-Wall + WARNING_ANDROID_OPTS=-Wall +else + # Use these for testing on Linux and Mac OSX: + WARNING_OPTS="-Wshadow -Wall -Wextra -pedantic" + gccvers=`gcc -v 2>&1 | grep '^gcc version'` + if test "$gccvers" '<' 'gcc version 6' + then + WARNING_ANDROID_OPTS="-Wshadow -Wall -Wextra" + else + WARNING_ANDROID_OPTS="-Wshadow -Wall -Wextra -Wimplicit-fallthrough=0" + fi +fi + rm -f sqlite3.c make sqlite3.c -echo '********** No optimizations. Includes FTS4/5, RTREE, JSON1 ***' -gcc -c -Wshadow -Wall -Wextra -pedantic-errors -Wno-long-long -std=c89 \ - -ansi -DHAVE_STDINT_H -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_RTREE \ - -DSQLITE_ENABLE_FTS5 -DSQLITE_ENABLE_JSON1 \ +echo '**** No optimizations. Includes FTS4/5, GEOPOLY, and others ***' +echo '****' $WARNING_OPTS +gcc -c $WARNING_OPTS -std=c99 \ + -DHAVE_STDINT_H \ + -DSQLITE_ENABLE_FTS4 \ + -DSQLITE_ENABLE_FTS5 \ + -DSQLITE_ENABLE_GEOPOLY \ + -DSQLITE_ENABLE_DBSTAT_VTAB \ + -DSQLITE_ENABLE_EXPLAIN_COMMENTS \ + -DSQLITE_ENABLE_MATH_FUNCTIONS_fixme \ + -DSQLITE_ENABLE_STMTVTAB \ + -DSQLITE_ENABLE_DBPAGE_VTAB \ sqlite3.c -echo '********** Android configuration ******************************' +if test x`uname` = 'xLinux'; then +echo '**** Android configuration ******************************' +echo '****' $WARNING_ANDROID_OPTS gcc -c \ - -DHAVE_USLEEP=1 \ -DSQLITE_HAVE_ISNAN \ -DSQLITE_DEFAULT_JOURNAL_SIZE_LIMIT=1048576 \ -DSQLITE_THREADSAFE=2 \ @@ -30,14 +56,20 @@ gcc -c \ -DSQLITE_DEFAULT_FILE_PERMISSIONS=0600 \ -DSQLITE_ENABLE_ICU \ -DUSE_PREAD64 \ - -Wshadow -Wall -Wextra \ + $WARNING_ANDROID_OPTS \ -Os sqlite3.c shell.c -echo '********** No optimizations. ENABLE_STAT4. THREADSAFE=0 *******' -gcc -c -Wshadow -Wall -Wextra -pedantic-errors -Wno-long-long -std=c89 \ - -ansi -DSQLITE_ENABLE_STAT4 -DSQLITE_THREADSAFE=0 \ +fi +echo '**** No optimizations. ENABLE_STAT4. THREADSAFE=0 *******' +echo '****' $WARNING_OPTS +gcc -c $WARNING_OPTS -std=c99 \ + -DSQLITE_ENABLE_STAT4 \ + -DSQLITE_THREADSAFE=0 \ sqlite3.c -echo '********** Optimized -O3. Includes FTS4/5, RTREE, JSON1 ******' -gcc -O3 -c -Wshadow -Wall -Wextra -pedantic-errors -Wno-long-long -std=c89 \ - -ansi -DHAVE_STDINT_H -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_RTREE \ - -DSQLITE_ENABLE_FTS5 -DSQLITE_ENABLE_JSON1 \ +echo '**** Optimized -O3. Includes FTS4/5, GEOPOLY, JSON1 ******' +echo '****' $WARNING_OPTS +gcc -O3 -c $WARNING_OPTS -std=c99 \ + -DHAVE_STDINT_H \ + -DSQLITE_ENABLE_FTS4 \ + -DSQLITE_ENABLE_FTS5 \ + -DSQLITE_ENABLE_GEOPOLY \ sqlite3.c